tcmodules.pas 943 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806
  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. // Async/AWait
  896. Procedure TestAsync_Proc;
  897. Procedure TestAsync_CallResultIsPromise;
  898. Procedure TestAsync_ConstructorFail;
  899. Procedure TestAsync_PropertyGetterFail;
  900. Procedure TestAwait_NonPromiseWithTypeFail;
  901. Procedure TestAwait_AsyncCallTypeMismatch;
  902. Procedure TestAWait_OutsideAsyncFail;
  903. Procedure TestAWait_IntegerFail;
  904. Procedure TestAWait_ExternalClassPromise;
  905. Procedure TestAWait_JSValue;
  906. Procedure TestAWait_Result;
  907. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  908. Procedure TestAsync_AnonymousProc;
  909. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  910. Procedure TestAsync_ProcType;
  911. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  912. Procedure TestAsync_Inherited;
  913. Procedure TestAsync_ClassInterface;
  914. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  915. Procedure TestAWait_ClassAs;
  916. // Library
  917. Procedure TestLibrary_Empty;
  918. Procedure TestLibrary_ExportFunc;
  919. Procedure TestLibrary_ExportFuncOverloadedFail;
  920. Procedure TestLibrary_Export_Index_Fail;
  921. Procedure TestLibrary_ExportVar;
  922. Procedure TestLibrary_ExportUnitFunc;
  923. end;
  924. function LinesToStr(Args: array of const): string;
  925. function ExtractFileUnitName(aFilename: string): string;
  926. function JSToStr(El: TJSElement): string;
  927. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  928. implementation
  929. function LinesToStr(Args: array of const): string;
  930. var
  931. s: String;
  932. i: Integer;
  933. begin
  934. s:='';
  935. for i:=Low(Args) to High(Args) do
  936. case Args[i].VType of
  937. vtChar: s += Args[i].VChar+LineEnding;
  938. vtString: s += Args[i].VString^+LineEnding;
  939. vtPChar: s += Args[i].VPChar+LineEnding;
  940. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  941. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  942. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  943. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  944. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  945. end;
  946. Result:=s;
  947. end;
  948. function ExtractFileUnitName(aFilename: string): string;
  949. var
  950. p: Integer;
  951. begin
  952. Result:=ExtractFileName(aFilename);
  953. if Result='' then exit;
  954. for p:=length(Result) downto 1 do
  955. case Result[p] of
  956. '/','\': exit;
  957. '.':
  958. begin
  959. Delete(Result,p,length(Result));
  960. exit;
  961. end;
  962. end;
  963. end;
  964. function JSToStr(El: TJSElement): string;
  965. var
  966. aWriter: TBufferWriter;
  967. aJSWriter: TJSWriter;
  968. begin
  969. aJSWriter:=nil;
  970. aWriter:=TBufferWriter.Create(1000);
  971. try
  972. aJSWriter:=TJSWriter.Create(aWriter);
  973. aJSWriter.IndentSize:=2;
  974. aJSWriter.WriteJS(El);
  975. Result:=aWriter.AsString;
  976. finally
  977. aJSWriter.Free;
  978. aWriter.Free;
  979. end;
  980. end;
  981. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  982. // search diff, ignore changes in spaces
  983. const
  984. SpaceChars = [#9,#10,#13,' '];
  985. var
  986. ExpectedP, ActualP: PChar;
  987. function FindLineEnd(p: PChar): PChar;
  988. begin
  989. Result:=p;
  990. while not (Result^ in [#0,#10,#13]) do inc(Result);
  991. end;
  992. function FindLineStart(p, MinP: PChar): PChar;
  993. begin
  994. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  995. Result:=p;
  996. end;
  997. procedure SkipLineEnd(var p: PChar);
  998. begin
  999. if p^ in [#10,#13] then
  1000. begin
  1001. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1002. inc(p,2)
  1003. else
  1004. inc(p);
  1005. end;
  1006. end;
  1007. function HasSpecialChar(s: string): boolean;
  1008. var
  1009. i: Integer;
  1010. begin
  1011. for i:=1 to length(s) do
  1012. if s[i] in [#0..#31,#127..#255] then
  1013. exit(true);
  1014. Result:=false;
  1015. end;
  1016. function HashSpecialChars(s: string): string;
  1017. var
  1018. i: Integer;
  1019. begin
  1020. Result:='';
  1021. for i:=1 to length(s) do
  1022. if s[i] in [#0..#31,#127..#255] then
  1023. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1024. else
  1025. Result:=Result+s[i];
  1026. end;
  1027. procedure DiffFound;
  1028. var
  1029. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1030. ExpLine, ActLine: String;
  1031. i, LineNo, DiffLineNo: Integer;
  1032. begin
  1033. writeln('Diff found "',Msg,'". Lines:');
  1034. // write correct lines
  1035. p:=PChar(Expected);
  1036. LineNo:=0;
  1037. DiffLineNo:=0;
  1038. repeat
  1039. StartPos:=p;
  1040. while not (p^ in [#0,#10,#13]) do inc(p);
  1041. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1042. SkipLineEnd(p);
  1043. inc(LineNo);
  1044. if (p<=ExpectedP) and (p^<>#0) then
  1045. begin
  1046. writeln('= ',ExpLine);
  1047. end else begin
  1048. // diff line
  1049. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1050. // write actual line
  1051. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1052. ActLineEndP:=FindLineEnd(ActualP);
  1053. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1054. writeln('- ',ActLine);
  1055. if HasSpecialChar(ActLine) then
  1056. writeln('- ',HashSpecialChars(ActLine));
  1057. // write expected line
  1058. writeln('+ ',ExpLine);
  1059. if HasSpecialChar(ExpLine) then
  1060. writeln('- ',HashSpecialChars(ExpLine));
  1061. // write empty line with pointer ^
  1062. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1063. writeln('^');
  1064. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1065. CheckSrcDiff:=false;
  1066. // write up to three following actual lines to get some context
  1067. for i:=1 to 3 do begin
  1068. ActLineStartP:=ActLineEndP;
  1069. SkipLineEnd(ActLineStartP);
  1070. if ActLineStartP^=#0 then break;
  1071. ActLineEndP:=FindLineEnd(ActLineStartP);
  1072. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1073. writeln('~ ',ActLine);
  1074. end;
  1075. exit;
  1076. end;
  1077. until p^=#0;
  1078. writeln('DiffFound Actual:-----------------------');
  1079. writeln(Actual);
  1080. writeln('DiffFound Expected:---------------------');
  1081. writeln(Expected);
  1082. writeln('DiffFound ------------------------------');
  1083. Msg:='diff found, but lines are the same, internal error';
  1084. CheckSrcDiff:=false;
  1085. end;
  1086. var
  1087. IsSpaceNeeded: Boolean;
  1088. LastChar, Quote: Char;
  1089. begin
  1090. Result:=true;
  1091. Msg:='';
  1092. if Expected='' then Expected:=' ';
  1093. if Actual='' then Actual:=' ';
  1094. ExpectedP:=PChar(Expected);
  1095. ActualP:=PChar(Actual);
  1096. repeat
  1097. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1098. case ExpectedP^ of
  1099. #0:
  1100. begin
  1101. // check that rest of Actual has only spaces
  1102. while ActualP^ in SpaceChars do inc(ActualP);
  1103. if ActualP^<>#0 then
  1104. begin
  1105. DiffFound;
  1106. exit;
  1107. end;
  1108. exit(true);
  1109. end;
  1110. ' ',#9,#10,#13:
  1111. begin
  1112. // skip space in Expected
  1113. IsSpaceNeeded:=false;
  1114. if ExpectedP>PChar(Expected) then
  1115. LastChar:=ExpectedP[-1]
  1116. else
  1117. LastChar:=#0;
  1118. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1119. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1120. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1121. IsSpaceNeeded:=true;
  1122. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1123. begin
  1124. DiffFound;
  1125. exit;
  1126. end;
  1127. while ActualP^ in SpaceChars do inc(ActualP);
  1128. end;
  1129. '''','"':
  1130. begin
  1131. while ActualP^ in SpaceChars do inc(ActualP);
  1132. if ExpectedP^<>ActualP^ then
  1133. begin
  1134. DiffFound;
  1135. exit;
  1136. end;
  1137. Quote:=ExpectedP^;
  1138. repeat
  1139. inc(ExpectedP);
  1140. inc(ActualP);
  1141. if ExpectedP^<>ActualP^ then
  1142. begin
  1143. DiffFound;
  1144. exit;
  1145. end;
  1146. if (ExpectedP^ in [#0,#10,#13]) then
  1147. break
  1148. else if (ExpectedP^=Quote) then
  1149. begin
  1150. inc(ExpectedP);
  1151. inc(ActualP);
  1152. break;
  1153. end;
  1154. until false;
  1155. end;
  1156. else
  1157. while ActualP^ in SpaceChars do inc(ActualP);
  1158. if ExpectedP^<>ActualP^ then
  1159. begin
  1160. DiffFound;
  1161. exit;
  1162. end;
  1163. inc(ExpectedP);
  1164. inc(ActualP);
  1165. end;
  1166. until false;
  1167. end;
  1168. { TTestEnginePasResolver }
  1169. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1170. begin
  1171. if FModule=AValue then Exit;
  1172. FModule:=AValue;
  1173. end;
  1174. destructor TTestEnginePasResolver.Destroy;
  1175. begin
  1176. FreeAndNil(FStreamResolver);
  1177. FreeAndNil(FParser);
  1178. FreeAndNil(FScanner);
  1179. FreeAndNil(FStreamResolver);
  1180. Module:=nil;
  1181. inherited Destroy;
  1182. end;
  1183. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1184. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1185. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1186. begin
  1187. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1188. TypeParams);
  1189. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1190. Module:=TPasModule(Result);
  1191. end;
  1192. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1193. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1194. begin
  1195. Result:=nil;
  1196. if InFilename<>'' then
  1197. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1198. if Assigned(OnFindUnit) then
  1199. Result:=OnFindUnit(AName);
  1200. if NameExpr=nil then ;
  1201. end;
  1202. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1203. begin
  1204. // do not parse recursively
  1205. // parse via the queue
  1206. if Section=nil then ;
  1207. end;
  1208. { TCustomTestModule }
  1209. procedure TCustomTestModule.FreeSrcMarkers;
  1210. var
  1211. aMarker, Last: PSrcMarker;
  1212. begin
  1213. aMarker:=FirstSrcMarker;
  1214. while aMarker<>nil do
  1215. begin
  1216. Last:=aMarker;
  1217. aMarker:=aMarker^.Next;
  1218. Dispose(Last);
  1219. end;
  1220. FirstSrcMarker:=nil;
  1221. LastSrcMarker:=nil;
  1222. end;
  1223. function TCustomTestModule.GetResolverCount: integer;
  1224. begin
  1225. Result:=FResolvers.Count;
  1226. end;
  1227. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1228. begin
  1229. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1230. end;
  1231. function TCustomTestModule.GetMsgCount: integer;
  1232. begin
  1233. Result:=FHintMsgs.Count;
  1234. end;
  1235. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1236. begin
  1237. Result:=TTestHintMessage(FHintMsgs[Index]);
  1238. end;
  1239. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1240. ): TPasModule;
  1241. var
  1242. DefNamespace: String;
  1243. begin
  1244. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1245. if (Pos('.',aUnitName)<1) then
  1246. begin
  1247. DefNamespace:=GetDefaultNamespace;
  1248. if DefNamespace<>'' then
  1249. begin
  1250. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1251. if Result<>nil then exit;
  1252. end;
  1253. end;
  1254. Result:=LoadUnit(aUnitName);
  1255. if Result<>nil then exit;
  1256. {$IFDEF VerbosePas2JS}
  1257. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1258. {$ENDIF}
  1259. Fail('can''t find unit "'+aUnitName+'"');
  1260. end;
  1261. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1262. var
  1263. aParser: TPasParser;
  1264. Item: TTestHintMessage;
  1265. begin
  1266. aParser:=Sender as TPasParser;
  1267. Item:=TTestHintMessage.Create;
  1268. Item.Id:=aParser.LastMsgNumber;
  1269. Item.MsgType:=aParser.LastMsgType;
  1270. Item.MsgNumber:=aParser.LastMsgNumber;
  1271. Item.Msg:=Msg;
  1272. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1273. {$IFDEF VerbosePas2JS}
  1274. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1275. {$ENDIF}
  1276. FHintMsgs.Add(Item);
  1277. end;
  1278. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1279. );
  1280. var
  1281. aResolver: TTestEnginePasResolver;
  1282. Item: TTestHintMessage;
  1283. begin
  1284. aResolver:=Sender as TTestEnginePasResolver;
  1285. Item:=TTestHintMessage.Create;
  1286. Item.Id:=aResolver.LastMsgId;
  1287. Item.MsgType:=aResolver.LastMsgType;
  1288. Item.MsgNumber:=aResolver.LastMsgNumber;
  1289. Item.Msg:=Msg;
  1290. Item.SourcePos:=aResolver.LastSourcePos;
  1291. {$IFDEF VerbosePas2JS}
  1292. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1293. {$ENDIF}
  1294. FHintMsgs.Add(Item);
  1295. end;
  1296. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1297. var
  1298. Item: TTestHintMessage;
  1299. aScanner: TPas2jsPasScanner;
  1300. begin
  1301. aScanner:=Sender as TPas2jsPasScanner;
  1302. Item:=TTestHintMessage.Create;
  1303. Item.Id:=aScanner.LastMsgNumber;
  1304. Item.MsgType:=aScanner.LastMsgType;
  1305. Item.MsgNumber:=aScanner.LastMsgNumber;
  1306. Item.Msg:=Msg;
  1307. Item.SourcePos:=aScanner.CurSourcePos;
  1308. {$IFDEF VerbosePas2JS}
  1309. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1310. {$ENDIF}
  1311. FHintMsgs.Add(Item);
  1312. end;
  1313. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1314. var
  1315. SubEl: TPasElement;
  1316. i: Integer;
  1317. procedure E(Msg: string);
  1318. var
  1319. s: String;
  1320. begin
  1321. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1322. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1323. writeln('ERROR: ',s);
  1324. Fail(s);
  1325. end;
  1326. begin
  1327. if arg=nil then ;
  1328. if El=nil then exit;
  1329. if El.Parent=El then
  1330. E('El.Parent=El='+GetObjName(El));
  1331. if El is TBinaryExpr then
  1332. begin
  1333. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1334. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1335. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1336. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1337. end
  1338. else if El is TParamsExpr then
  1339. begin
  1340. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1341. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1342. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1343. if TParamsExpr(El).Params[i].Parent<>El then
  1344. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1345. end
  1346. else if El is TProcedureExpr then
  1347. begin
  1348. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1349. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1350. end
  1351. else if El is TPasDeclarations then
  1352. begin
  1353. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1354. begin
  1355. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1356. if SubEl.Parent<>El then
  1357. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1358. end;
  1359. end
  1360. else if El is TPasImplBlock then
  1361. begin
  1362. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1363. begin
  1364. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1365. if SubEl.Parent<>El then
  1366. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1367. end;
  1368. end
  1369. else if El is TPasImplWithDo then
  1370. begin
  1371. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1372. begin
  1373. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1374. if SubEl.Parent<>El then
  1375. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1376. end;
  1377. end
  1378. else if El is TPasProcedure then
  1379. begin
  1380. if TPasProcedure(El).ProcType.Parent<>El then
  1381. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1382. end
  1383. else if El is TPasProcedureType then
  1384. begin
  1385. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1386. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1387. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1388. end;
  1389. end;
  1390. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1391. var
  1392. Data: PTestResolverReferenceData absolute FindData;
  1393. Line, Col: integer;
  1394. begin
  1395. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1396. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1397. if (Data^.Filename=El.SourceFilename)
  1398. and (Data^.Row=Line)
  1399. and (Data^.StartCol<=Col)
  1400. and (Data^.EndCol>=Col)
  1401. then
  1402. Data^.Found.Add(El);
  1403. end;
  1404. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1405. begin
  1406. if FWithTypeInfo=AValue then Exit;
  1407. FWithTypeInfo:=AValue;
  1408. if AValue then
  1409. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1410. else
  1411. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1412. end;
  1413. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1414. var
  1415. i: Integer;
  1416. CurEngine: TTestEnginePasResolver;
  1417. CurUnitName: String;
  1418. begin
  1419. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1420. Result:=nil;
  1421. if (Module.ClassType=TPasModule)
  1422. and (CompareText(Module.Name,aUnitName)=0) then
  1423. exit(Module);
  1424. for i:=0 to ResolverCount-1 do
  1425. begin
  1426. CurEngine:=Resolvers[i];
  1427. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1428. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1429. if CompareText(aUnitName,CurUnitName)=0 then
  1430. begin
  1431. Result:=CurEngine.Module;
  1432. if Result<>nil then exit;
  1433. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1434. FileResolver.FindSourceFile(aUnitName);
  1435. CurEngine.StreamResolver:=TStreamResolver.Create;
  1436. CurEngine.StreamResolver.OwnsStreams:=True;
  1437. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1438. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1439. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1440. InitScanner(CurEngine.Scanner);
  1441. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1442. CurEngine.Parser.Options:=po_tcmodules;
  1443. if CompareText(CurUnitName,'System')=0 then
  1444. CurEngine.Parser.ImplicitUses.Clear;
  1445. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1446. try
  1447. CurEngine.Parser.NextToken;
  1448. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1449. except
  1450. on E: Exception do
  1451. HandleException(E);
  1452. end;
  1453. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1454. Result:=CurEngine.Module;
  1455. exit;
  1456. end;
  1457. end;
  1458. end;
  1459. procedure TCustomTestModule.SetUp;
  1460. begin
  1461. {$IFDEF EnablePasTreeGlobalRefCount}
  1462. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1463. {$ENDIF}
  1464. if FResolvers<>nil then
  1465. begin
  1466. writeln('TCustomTestModule.SetUp FModules<>nil');
  1467. Halt;
  1468. end;
  1469. inherited SetUp;
  1470. FSkipTests:=false;
  1471. FWithTypeInfo:=false;
  1472. FSource:=TStringList.Create;
  1473. FHub:=TPas2JSResolverHub.Create(Self);
  1474. FResolvers:=TObjectList.Create(true);
  1475. FFilename:='test1.pp';
  1476. FFileResolver:=TStreamResolver.Create;
  1477. FFileResolver.OwnsStreams:=True;
  1478. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1479. InitScanner(FScanner);
  1480. FEngine:=AddModule(Filename);
  1481. FEngine.Scanner:=FScanner;
  1482. FScanner.Resolver:=FEngine;
  1483. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1484. FParser.OnLog:=@OnParserLog;
  1485. FEngine.Parser:=FParser;
  1486. Parser.Options:=po_tcmodules;
  1487. FModule:=Nil;
  1488. FConverter:=CreateConverter;
  1489. FExpectedErrorClass:=nil;
  1490. end;
  1491. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1492. var
  1493. Options: TPasToJsConverterOptions;
  1494. begin
  1495. Result:=TPasToJSConverter.Create;
  1496. Options:=co_tcmodules;
  1497. if WithTypeInfo then
  1498. Exclude(Options,coNoTypeInfo)
  1499. else
  1500. Include(Options,coNoTypeInfo);
  1501. Result.Options:=Options;
  1502. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1503. end;
  1504. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1505. begin
  1506. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1507. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1508. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1509. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1510. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1511. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1512. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1513. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1514. aScanner.OnLog:=@OnScannerLog;
  1515. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1516. end;
  1517. procedure TCustomTestModule.TearDown;
  1518. {$IFDEF CheckPasTreeRefCount}
  1519. var
  1520. El: TPasElement;
  1521. {$ENDIF}
  1522. var
  1523. i: Integer;
  1524. CurModule: TPasModule;
  1525. begin
  1526. FreeSrcMarkers;
  1527. FHintMsgs.Clear;
  1528. FHintMsgsGood.Clear;
  1529. FSkipTests:=false;
  1530. FWithTypeInfo:=false;
  1531. FJSRegModuleCall:=nil;
  1532. FJSModuleCallArgs:=nil;
  1533. FJSImplentationUses:=nil;
  1534. FJSInterfaceUses:=nil;
  1535. FJSModuleSrc:=nil;
  1536. FJSInitBody:=nil;
  1537. FreeAndNil(FJSSource);
  1538. FreeAndNil(FJSModule);
  1539. FreeAndNil(FConverter);
  1540. ResolverEngine.Clear;
  1541. FreeAndNil(FSource);
  1542. FreeAndNil(FFileResolver);
  1543. if FResolvers<>nil then
  1544. begin
  1545. for i:=0 to FResolvers.Count-1 do
  1546. begin
  1547. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1548. if CurModule=nil then continue;
  1549. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1550. end;
  1551. for i:=0 to FResolvers.Count-1 do
  1552. begin
  1553. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1554. if CurModule=nil then continue;
  1555. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1556. end;
  1557. FreeAndNil(FResolvers);
  1558. FModule:=nil;
  1559. FEngine:=nil;
  1560. end;
  1561. FreeAndNil(FHub);
  1562. inherited TearDown;
  1563. {$IFDEF EnablePasTreeGlobalRefCount}
  1564. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1565. begin
  1566. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1567. {$IFDEF CheckPasTreeRefCount}
  1568. El:=TPasElement.FirstRefEl;
  1569. while El<>nil do
  1570. begin
  1571. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1572. for i:=0 to El.RefIds.Count-1 do
  1573. writeln(' ',El.RefIds[i]);
  1574. El:=El.NextRefEl;
  1575. end;
  1576. {$ENDIF}
  1577. Halt;
  1578. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1579. end;
  1580. {$ENDIF}
  1581. end;
  1582. procedure TCustomTestModule.Add(Line: string);
  1583. begin
  1584. Source.Add(Line);
  1585. end;
  1586. procedure TCustomTestModule.Add(const Lines: array of string);
  1587. var
  1588. i: Integer;
  1589. begin
  1590. for i:=low(Lines) to high(Lines) do
  1591. Add(Lines[i]);
  1592. end;
  1593. procedure TCustomTestModule.StartParsing;
  1594. var
  1595. Src: String;
  1596. begin
  1597. Src:=Source.Text;
  1598. FEngine.Source:=Src;
  1599. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1600. Scanner.OpenFile(FileName);
  1601. Writeln('// Test : ',Self.TestName);
  1602. Writeln(Src);
  1603. end;
  1604. procedure TCustomTestModule.ParseModuleQueue;
  1605. var
  1606. i: Integer;
  1607. CurResolver: TTestEnginePasResolver;
  1608. Found: Boolean;
  1609. Section: TPasSection;
  1610. begin
  1611. // parse til exception or all Resolvers finished
  1612. while not SkipTests do
  1613. begin
  1614. Found:=false;
  1615. for i:=0 to ResolverCount-1 do
  1616. begin
  1617. CurResolver:=Resolvers[i];
  1618. if CurResolver.CurrentParser=nil then continue;
  1619. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1620. continue;
  1621. CurResolver.Parser.ParseContinue;
  1622. Found:=true;
  1623. break;
  1624. end;
  1625. if not Found then break;
  1626. end;
  1627. for i:=0 to ResolverCount-1 do
  1628. begin
  1629. CurResolver:=Resolvers[i];
  1630. if CurResolver.Parser=nil then
  1631. begin
  1632. if CurResolver.CurrentParser<>nil then
  1633. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1634. continue;
  1635. end;
  1636. if CurResolver.Parser.CurModule<>nil then
  1637. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1638. end;
  1639. end;
  1640. procedure TCustomTestModule.ParseModule;
  1641. begin
  1642. if SkipTests then exit;
  1643. FFirstPasStatement:=nil;
  1644. try
  1645. StartParsing;
  1646. Parser.ParseMain(FModule);
  1647. ParseModuleQueue;
  1648. except
  1649. on E: Exception do
  1650. HandleException(E);
  1651. end;
  1652. if SkipTests then exit;
  1653. AssertNotNull('Module resulted in Module',Module);
  1654. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1655. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1656. end;
  1657. procedure TCustomTestModule.ParseProgram;
  1658. begin
  1659. if SkipTests then exit;
  1660. ParseModule;
  1661. if SkipTests then exit;
  1662. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1663. FPasProgram:=TPasProgram(Module);
  1664. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1665. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1666. if (PasProgram.InitializationSection.Elements.Count>0) then
  1667. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1668. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1669. end;
  1670. procedure TCustomTestModule.ParseLibrary;
  1671. var
  1672. Init: TInitializationSection;
  1673. begin
  1674. if SkipTests then exit;
  1675. ParseModule;
  1676. if SkipTests then exit;
  1677. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1678. FPasLibrary:=TPasLibrary(Module);
  1679. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1680. Init:=PasLibrary.InitializationSection;
  1681. if (Init<>nil) and (Init.Elements.Count>0) then
  1682. if TObject(Init.Elements[0]) is TPasImplBlock then
  1683. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1684. end;
  1685. procedure TCustomTestModule.ParseUnit;
  1686. begin
  1687. if SkipTests then exit;
  1688. ParseModule;
  1689. if SkipTests then exit;
  1690. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1691. AssertNotNull('Has interface section',Module.InterfaceSection);
  1692. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1693. if (Module.InitializationSection<>nil)
  1694. and (Module.InitializationSection.Elements.Count>0)
  1695. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1696. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1697. end;
  1698. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1699. ): TTestEnginePasResolver;
  1700. var
  1701. i: Integer;
  1702. begin
  1703. for i:=0 to ResolverCount-1 do
  1704. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1705. exit(Resolvers[i]);
  1706. Result:=nil;
  1707. end;
  1708. function TCustomTestModule.AddModule(aFilename: string
  1709. ): TTestEnginePasResolver;
  1710. begin
  1711. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1712. if FindModuleWithFilename(aFilename)<>nil then
  1713. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1714. Result:=TTestEnginePasResolver.Create;
  1715. Result.Filename:=aFilename;
  1716. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1717. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1718. Result.OnLog:=@OnPasResolverLog;
  1719. Result.Hub:=Hub;
  1720. FResolvers.Add(Result);
  1721. end;
  1722. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1723. ): TTestEnginePasResolver;
  1724. begin
  1725. Result:=AddModule(aFilename);
  1726. Result.Source:=Src;
  1727. end;
  1728. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1729. ImplementationSrc: string): TTestEnginePasResolver;
  1730. var
  1731. Src: String;
  1732. begin
  1733. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1734. Src+=LineEnding;
  1735. Src+='interface'+LineEnding;
  1736. Src+=LineEnding;
  1737. Src+=InterfaceSrc;
  1738. Src+='implementation'+LineEnding;
  1739. Src+=LineEnding;
  1740. Src+=ImplementationSrc;
  1741. Src+='end.'+LineEnding;
  1742. Result:=AddModuleWithSrc(aFilename,Src);
  1743. end;
  1744. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1745. var
  1746. Intf, Impl: TStringList;
  1747. begin
  1748. Intf:=TStringList.Create;
  1749. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1750. // unit interface
  1751. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1752. Intf.Add('{$modeswitch externalclass}');
  1753. Intf.Add('type');
  1754. Intf.Add(' integer=longint;');
  1755. Intf.Add(' sizeint=nativeint;');
  1756. //'const',
  1757. //' LineEnding = #10;',
  1758. //' DirectorySeparator = ''/'';',
  1759. //' DriveSeparator = '''';',
  1760. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1761. //' AllowDriveSeparators : set of char = [];',
  1762. if supTObject in Parts then
  1763. Intf.AddStrings([
  1764. 'type',
  1765. ' TClass = class of TObject;',
  1766. ' TObject = class',
  1767. ' constructor Create;',
  1768. ' destructor Destroy; virtual;',
  1769. ' class function ClassType: TClass; assembler;',
  1770. ' class function ClassName: String; assembler;',
  1771. ' class function ClassNameIs(const Name: string): boolean;',
  1772. ' class function ClassParent: TClass; assembler;',
  1773. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1774. ' class function UnitName: String; assembler;',
  1775. ' procedure AfterConstruction; virtual;',
  1776. ' procedure BeforeDestruction;virtual;',
  1777. ' function Equals(Obj: TObject): boolean; virtual;',
  1778. ' function ToString: String; virtual;',
  1779. ' end;']);
  1780. if supTInterfacedObject in Parts then
  1781. Intf.AddStrings([
  1782. ' {$Interfaces COM}',
  1783. ' IUnknown = interface',
  1784. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1785. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1786. ' function _AddRef: Integer;',
  1787. ' function _Release: Integer;',
  1788. ' end;',
  1789. ' IInterface = IUnknown;',
  1790. ' TInterfacedObject = class(TObject,IUnknown)',
  1791. ' protected',
  1792. ' fRefCount: Integer;',
  1793. ' { implement methods of IUnknown }',
  1794. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1795. ' function _AddRef: Integer; virtual;',
  1796. ' function _Release: Integer; virtual;',
  1797. ' end;',
  1798. ' TInterfacedClass = class of TInterfacedObject;',
  1799. '',
  1800. '']);
  1801. if supTVarRec in Parts then
  1802. Intf.AddStrings([
  1803. 'const',
  1804. ' vtInteger = 0;',
  1805. ' vtBoolean = 1;',
  1806. ' vtJSValue = 19;',
  1807. 'type',
  1808. ' PVarRec = ^TVarRec;',
  1809. ' TVarRec = record',
  1810. ' VType : byte;',
  1811. ' VJSValue: JSValue;',
  1812. ' vInteger: longint external name ''VJSValue'';',
  1813. ' vBoolean: boolean external name ''VJSValue'';',
  1814. ' end;',
  1815. ' TVarRecArray = array of TVarRec;',
  1816. 'function VarRecs: TVarRecArray; varargs;',
  1817. '']);
  1818. if supTypeInfo in Parts then
  1819. begin
  1820. Intf.AddStrings([
  1821. 'type',
  1822. ' TTypeKind = (',
  1823. ' tkUnknown, // 0',
  1824. ' tkInteger, // 1',
  1825. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1826. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1827. ' tkEnumeration, // 4',
  1828. ' tkSet, // 5',
  1829. ' tkDouble, // 6',
  1830. ' tkBool, // 7',
  1831. ' tkProcVar, // 8 function or procedure',
  1832. ' tkMethod, // 9 proc var of object',
  1833. ' tkArray, // 10 static array',
  1834. ' tkDynArray, // 11',
  1835. ' tkRecord, // 12',
  1836. ' tkClass, // 13',
  1837. ' tkClassRef, // 14',
  1838. ' tkPointer, // 15',
  1839. ' tkJSValue, // 16',
  1840. ' tkRefToProcVar, // 17 variable of procedure type',
  1841. ' tkInterface, // 18',
  1842. ' //tkObject,',
  1843. ' //tkSString,tkLString,tkAString,tkWString,',
  1844. ' //tkVariant,',
  1845. ' //tkWChar,',
  1846. ' //tkInt64,',
  1847. ' //tkQWord,',
  1848. ' //tkInterfaceRaw,',
  1849. ' //tkUString,tkUChar,',
  1850. ' tkHelper, // 19',
  1851. ' //tkFile,',
  1852. ' tkExtClass // 20',
  1853. ' );',
  1854. ' TTypeKinds = set of TTypeKind;',
  1855. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1856. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1857. ' end;',
  1858. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1859. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1860. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1861. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1862. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1863. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1864. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1865. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1866. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1867. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1868. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1869. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1870. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1871. '']);
  1872. end;
  1873. if supWriteln in Parts then
  1874. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1875. Intf.Add('var');
  1876. Intf.Add(' ExitCode: Longint = 0;');
  1877. // unit implementation
  1878. Impl:=TStringList.Create;
  1879. if supTObject in Parts then
  1880. Impl.AddStrings([
  1881. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1882. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1883. 'asm',
  1884. 'end;',
  1885. 'constructor TObject.Create; begin end;',
  1886. 'destructor TObject.Destroy; begin end;',
  1887. 'class function TObject.ClassType: TClass; assembler;',
  1888. 'asm',
  1889. 'end;',
  1890. 'class function TObject.ClassName: String; assembler;',
  1891. 'asm',
  1892. 'end;',
  1893. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1894. 'begin',
  1895. ' Result:=SameText(Name,ClassName);',
  1896. 'end;',
  1897. 'class function TObject.ClassParent: TClass; assembler;',
  1898. 'asm',
  1899. 'end;',
  1900. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1901. 'asm',
  1902. 'end;',
  1903. 'class function TObject.UnitName: String; assembler;',
  1904. 'asm',
  1905. 'end;',
  1906. 'procedure TObject.AfterConstruction; begin end;',
  1907. 'procedure TObject.BeforeDestruction; begin end;',
  1908. 'function TObject.Equals(Obj: TObject): boolean;',
  1909. 'begin',
  1910. ' Result:=Obj=Self;',
  1911. 'end;',
  1912. 'function TObject.ToString: String;',
  1913. 'begin',
  1914. ' Result:=ClassName;',
  1915. 'end;'
  1916. ]);
  1917. if supTInterfacedObject in Parts then
  1918. Impl.AddStrings([
  1919. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1920. //'begin',
  1921. //'end;',
  1922. 'function TInterfacedObject._AddRef: Integer;',
  1923. 'begin',
  1924. 'end;',
  1925. 'function TInterfacedObject._Release: Integer;',
  1926. 'begin',
  1927. 'end;',
  1928. '']);
  1929. if supTVarRec in Parts then
  1930. Impl.AddStrings([
  1931. 'function VarRecs: TVarRecArray; varargs;',
  1932. 'var',
  1933. ' v: PVarRec;',
  1934. 'begin',
  1935. ' v^.VType:=1;',
  1936. ' v^.VJSValue:=2;',
  1937. 'end;',
  1938. '']);
  1939. try
  1940. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1941. finally
  1942. Intf.Free;
  1943. Impl.Free;
  1944. end;
  1945. end;
  1946. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1947. SystemUnitParts: TSystemUnitParts);
  1948. begin
  1949. if NeedSystemUnit then
  1950. AddSystemUnit(SystemUnitParts)
  1951. else
  1952. Parser.ImplicitUses.Clear;
  1953. Add('program '+ExtractFileUnitName(Filename)+';');
  1954. Add('');
  1955. end;
  1956. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1957. SystemUnitParts: TSystemUnitParts);
  1958. begin
  1959. if NeedSystemUnit then
  1960. AddSystemUnit(SystemUnitParts)
  1961. else
  1962. Parser.ImplicitUses.Clear;
  1963. Add('library '+ExtractFileUnitName(Filename)+';');
  1964. Add('');
  1965. end;
  1966. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1967. SystemUnitParts: TSystemUnitParts);
  1968. begin
  1969. if NeedSystemUnit then
  1970. AddSystemUnit(SystemUnitParts)
  1971. else
  1972. Parser.ImplicitUses.Clear;
  1973. Add('unit Test1;');
  1974. Add('');
  1975. end;
  1976. procedure TCustomTestModule.ConvertModule;
  1977. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1978. out UsesLit: TJSArrayLiteral);
  1979. var
  1980. i: Integer;
  1981. Item: TJSElement;
  1982. Lit: TJSLiteral;
  1983. begin
  1984. UsesLit:=nil;
  1985. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1986. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1987. exit; // null is ok
  1988. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1989. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1990. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1991. begin
  1992. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1993. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1994. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1995. Lit:=TJSLiteral(Item);
  1996. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1997. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1998. end;
  1999. end;
  2000. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2001. out Src: TJSSourceElements);
  2002. var
  2003. FunDecl: TJSFunctionDeclarationStatement;
  2004. FunDef: TJSFuncDef;
  2005. FunBody: TJSFunctionBody;
  2006. begin
  2007. Src:=nil;
  2008. AssertNotNull(ParamName,Arg.Expr);
  2009. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2010. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2011. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2012. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2013. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2014. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2015. AssertNotNull(ParamName+' body',FunDef.Body);
  2016. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2017. FunBody:=FunDef.Body as TJSFunctionBody;
  2018. AssertNotNull(ParamName+' body.A',FunBody.A);
  2019. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2020. Src:=FunBody.A as TJSSourceElements;
  2021. end;
  2022. var
  2023. ModuleNameExpr: TJSLiteral;
  2024. InitFunction: TJSFunctionDeclarationStatement;
  2025. InitAssign: TJSSimpleAssignStatement;
  2026. InitName: String;
  2027. LastNode, FirstNode: TJSElement;
  2028. Arg: TJSArrayLiteralElement;
  2029. IsProg, IsLib: Boolean;
  2030. begin
  2031. if SkipTests then exit;
  2032. IsProg:=false;
  2033. IsLib:=false;
  2034. if Module is TPasProgram then
  2035. IsProg:=true
  2036. else if Module is TPasLibrary then
  2037. IsLib:=true;
  2038. try
  2039. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2040. except
  2041. on E: Exception do
  2042. HandleException(E);
  2043. end;
  2044. if SkipTests then exit;
  2045. if ExpectedErrorClass<>nil then
  2046. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2047. FJSSource:=TStringList.Create;
  2048. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2049. {$IFDEF VerbosePas2JS}
  2050. writeln('TTestModule.ConvertModule JS:');
  2051. write(FJSSource.Text);
  2052. {$ENDIF}
  2053. // rtl.module(...
  2054. if JSModule.Statements.Count<1 then
  2055. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2056. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2057. AssertNotNull('register module call',FirstNode);
  2058. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2059. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2060. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2061. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2062. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2063. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2064. // parameter 'unitname'
  2065. if JSModuleCallArgs.Elements.Count<1 then
  2066. Fail('rtl.module first param unit missing');
  2067. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2068. AssertNotNull('module name param',Arg.Expr);
  2069. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2070. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2071. if IsProg then
  2072. begin
  2073. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2074. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2075. end
  2076. else if IsLib then
  2077. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2078. else
  2079. begin
  2080. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2081. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2082. end;
  2083. // main uses section
  2084. if JSModuleCallArgs.Elements.Count<2 then
  2085. Fail('rtl.module second param main uses missing');
  2086. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2087. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2088. // program/library/interface function()
  2089. if JSModuleCallArgs.Elements.Count<3 then
  2090. Fail('rtl.module third param intf-function missing');
  2091. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2092. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2093. // search for $mod.$init or $mod.$main - the last statement
  2094. if IsProg or IsLib then
  2095. begin
  2096. InitName:='$main';
  2097. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2098. end
  2099. else
  2100. InitName:='$init';
  2101. InitAssign:=nil;
  2102. InitFunction:=nil;
  2103. FJSInitBody:=nil;
  2104. if JSModuleSrc.Statements.Count>0 then
  2105. begin
  2106. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2107. if LastNode is TJSSimpleAssignStatement then
  2108. begin
  2109. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2110. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2111. begin
  2112. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2113. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2114. end
  2115. else if IsProg or IsLib then
  2116. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2117. end;
  2118. end;
  2119. // optional: implementation uses section
  2120. if JSModuleCallArgs.Elements.Count<4 then
  2121. exit;
  2122. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2123. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2124. end;
  2125. procedure TCustomTestModule.ConvertProgram;
  2126. begin
  2127. Add('end.');
  2128. ParseProgram;
  2129. ConvertModule;
  2130. end;
  2131. procedure TCustomTestModule.ConvertLibrary;
  2132. begin
  2133. Add('end.');
  2134. ParseLibrary;
  2135. ConvertModule;
  2136. end;
  2137. procedure TCustomTestModule.ConvertUnit;
  2138. begin
  2139. Add('end.');
  2140. ParseUnit;
  2141. ConvertModule;
  2142. end;
  2143. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2144. begin
  2145. Result:=tcmodules.JSToStr(El);
  2146. end;
  2147. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2148. DottedName: string);
  2149. begin
  2150. if DottedName='' then
  2151. begin
  2152. AssertNull(Msg,El);
  2153. end
  2154. else
  2155. begin
  2156. AssertNotNull(Msg,El);
  2157. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2158. end;
  2159. end;
  2160. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2161. begin
  2162. if El=nil then
  2163. Result:=''
  2164. else if El is TJSPrimaryExpressionIdent then
  2165. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2166. else if El is TJSDotMemberExpression then
  2167. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2168. else
  2169. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2170. end;
  2171. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2172. InitStatements: string; ImplStatements: string);
  2173. var
  2174. ActualSrc, ExpectedSrc, InitName: String;
  2175. IsProg, IsLib: Boolean;
  2176. begin
  2177. ActualSrc:=JSToStr(JSModuleSrc);
  2178. if coUseStrict in Converter.Options then
  2179. ExpectedSrc:='"use strict";'+LineEnding
  2180. else
  2181. ExpectedSrc:='';
  2182. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2183. ExpectedSrc:=ExpectedSrc+Statements;
  2184. // unit implementation
  2185. if (Trim(ImplStatements)<>'') then
  2186. ExpectedSrc:=ExpectedSrc+LineEnding
  2187. +'$mod.$implcode = function () {'+LineEnding
  2188. +ImplStatements
  2189. +'};'+LineEnding;
  2190. // program main or unit initialization
  2191. IsProg:=false;
  2192. IsLib:=false;
  2193. if Module is TPasProgram then
  2194. IsProg:=true
  2195. else if Module is TPasLibrary then
  2196. IsLib:=true;
  2197. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2198. begin
  2199. if IsProg or IsLib then
  2200. InitName:='$main'
  2201. else
  2202. InitName:='$init';
  2203. ExpectedSrc:=ExpectedSrc+LineEnding
  2204. +'$mod.'+InitName+' = function () {'+LineEnding
  2205. +InitStatements
  2206. +'};'+LineEnding;
  2207. end;
  2208. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2209. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2210. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2211. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2212. end;
  2213. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2214. var
  2215. ActualSrc: String;
  2216. begin
  2217. ActualSrc:=JSToStr(JSModule);
  2218. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2219. end;
  2220. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2221. // search diff, ignore changes in spaces
  2222. var
  2223. s: string;
  2224. begin
  2225. if CheckSrcDiff(Expected,Actual,s) then exit;
  2226. Fail(Msg+': '+s);
  2227. end;
  2228. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2229. var
  2230. aResolver: TTestEnginePasResolver;
  2231. aConverter: TPasToJSConverter;
  2232. aJSModule: TJSSourceElements;
  2233. ActualSrc: String;
  2234. begin
  2235. aResolver:=GetResolver(Filename);
  2236. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2237. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2238. {$IFDEF VerbosePas2JS}
  2239. writeln('CheckUnit '+Filename+' converting ...');
  2240. {$ENDIF}
  2241. aConverter:=CreateConverter;
  2242. aJSModule:=nil;
  2243. try
  2244. try
  2245. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2246. except
  2247. on E: Exception do
  2248. HandleException(E);
  2249. end;
  2250. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2251. {$IFDEF VerbosePas2JS}
  2252. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2253. write(aResolver.Source);
  2254. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2255. write(ActualSrc);
  2256. {$ENDIF}
  2257. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2258. finally
  2259. aJSModule.Free;
  2260. aConverter.Free;
  2261. end;
  2262. end;
  2263. procedure TCustomTestModule.CheckReferenceDirectives;
  2264. var
  2265. CurFilename: string;
  2266. LineNumber: Integer;
  2267. SrcLine: String;
  2268. CommentStartP, CommentEndP: PChar;
  2269. procedure RaiseError(Msg: string; p: PChar);
  2270. begin
  2271. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2272. end;
  2273. procedure AddMarker(Marker: PSrcMarker);
  2274. begin
  2275. if LastSrcMarker<>nil then
  2276. LastSrcMarker^.Next:=Marker
  2277. else
  2278. FirstSrcMarker:=Marker;
  2279. LastSrcMarker:=Marker;
  2280. end;
  2281. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2282. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2283. begin
  2284. New(Result);
  2285. Result^.Kind:=Kind;
  2286. Result^.Filename:=aFilename;
  2287. Result^.Row:=aLine;
  2288. Result^.StartCol:=aStartCol;
  2289. Result^.EndCol:=aEndCol;
  2290. Result^.Identifier:=Identifier;
  2291. Result^.Next:=nil;
  2292. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2293. AddMarker(Result);
  2294. end;
  2295. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2296. const Identifier: string): PSrcMarker;
  2297. var
  2298. TokenStart, p: PChar;
  2299. begin
  2300. p:=CommentEndP;
  2301. ReadNextPascalToken(p,TokenStart,false,false);
  2302. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2303. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2304. end;
  2305. function ReadIdentifier(var p: PChar): string;
  2306. var
  2307. StartP: PChar;
  2308. begin
  2309. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2310. RaiseError('identifier expected',p);
  2311. StartP:=p;
  2312. inc(p);
  2313. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2314. Result:='';
  2315. SetLength(Result,p-StartP);
  2316. Move(StartP^,Result[1],length(Result));
  2317. end;
  2318. procedure AddLabel;
  2319. var
  2320. Identifier: String;
  2321. p: PChar;
  2322. begin
  2323. p:=CommentStartP+2;
  2324. Identifier:=ReadIdentifier(p);
  2325. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2326. if FindSrcLabel(Identifier)<>nil then
  2327. RaiseError('duplicate label "'+Identifier+'"',p);
  2328. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2329. end;
  2330. procedure AddResolverReference;
  2331. var
  2332. Identifier: String;
  2333. p: PChar;
  2334. begin
  2335. p:=CommentStartP+2;
  2336. Identifier:=ReadIdentifier(p);
  2337. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2338. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2339. end;
  2340. procedure AddDirectReference;
  2341. var
  2342. Identifier: String;
  2343. p: PChar;
  2344. begin
  2345. p:=CommentStartP+2;
  2346. Identifier:=ReadIdentifier(p);
  2347. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2348. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2349. end;
  2350. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2351. var
  2352. p: PChar;
  2353. IsDirective: Boolean;
  2354. begin
  2355. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2356. CurFilename:=aFilename;
  2357. // parse code, find all labels
  2358. LineNumber:=0;
  2359. while LineNumber<SrcLines.Count do
  2360. begin
  2361. inc(LineNumber);
  2362. SrcLine:=SrcLines[LineNumber-1];
  2363. if SrcLine='' then continue;
  2364. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2365. p:=PChar(SrcLine);
  2366. repeat
  2367. case p^ of
  2368. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2369. '{':
  2370. begin
  2371. CommentStartP:=p;
  2372. inc(p);
  2373. IsDirective:=p^ in ['#','@','='];
  2374. // skip to end of comment
  2375. repeat
  2376. case p^ of
  2377. #0:
  2378. if (p-PChar(SrcLine)=length(SrcLine)) then
  2379. begin
  2380. // multi line comment
  2381. if IsDirective then
  2382. RaiseError('directive missing closing bracket',CommentStartP);
  2383. repeat
  2384. inc(LineNumber);
  2385. if LineNumber>SrcLines.Count then exit;
  2386. SrcLine:=SrcLines[LineNumber-1];
  2387. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2388. until SrcLine<>'';
  2389. p:=PChar(SrcLine);
  2390. continue;
  2391. end;
  2392. '}':
  2393. begin
  2394. inc(p);
  2395. break;
  2396. end;
  2397. end;
  2398. inc(p);
  2399. until false;
  2400. CommentEndP:=p;
  2401. case CommentStartP[1] of
  2402. '#': AddLabel;
  2403. '@': AddResolverReference;
  2404. '=': AddDirectReference;
  2405. end;
  2406. p:=CommentEndP;
  2407. continue;
  2408. end;
  2409. '/':
  2410. if p[1]='/' then
  2411. break; // rest of line is comment -> skip
  2412. end;
  2413. inc(p);
  2414. until false;
  2415. end;
  2416. end;
  2417. procedure CheckResolverReference(aMarker: PSrcMarker);
  2418. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2419. var
  2420. aLabel: PSrcMarker;
  2421. ReferenceElements, LabelElements: TFPList;
  2422. i, j, aLine, aCol: Integer;
  2423. El, Ref, LabelEl: TPasElement;
  2424. begin
  2425. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2426. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2427. if aLabel=nil then
  2428. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2429. LabelElements:=nil;
  2430. ReferenceElements:=nil;
  2431. try
  2432. LabelElements:=FindElementsAt(aLabel);
  2433. ReferenceElements:=FindElementsAt(aMarker);
  2434. for i:=0 to ReferenceElements.Count-1 do
  2435. begin
  2436. El:=TPasElement(ReferenceElements[i]);
  2437. Ref:=nil;
  2438. if El.CustomData is TResolvedReference then
  2439. Ref:=TResolvedReference(El.CustomData).Declaration
  2440. else if El.CustomData is TPasPropertyScope then
  2441. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2442. else if El.CustomData is TPasSpecializeTypeData then
  2443. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2444. if Ref<>nil then
  2445. for j:=0 to LabelElements.Count-1 do
  2446. begin
  2447. LabelEl:=TPasElement(LabelElements[j]);
  2448. if Ref=LabelEl then
  2449. exit; // success
  2450. end;
  2451. end;
  2452. // failure write candidates
  2453. for i:=0 to ReferenceElements.Count-1 do
  2454. begin
  2455. El:=TPasElement(ReferenceElements[i]);
  2456. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2457. write(' El=',GetObjName(El));
  2458. if EL is TPrimitiveExpr then
  2459. begin
  2460. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2461. end;
  2462. Ref:=nil;
  2463. if El.CustomData is TResolvedReference then
  2464. Ref:=TResolvedReference(El.CustomData).Declaration
  2465. else if El.CustomData is TPasPropertyScope then
  2466. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2467. else if El.CustomData is TPasSpecializeTypeData then
  2468. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2469. if Ref<>nil then
  2470. begin
  2471. write(' Decl=',GetObjName(Ref));
  2472. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2473. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2474. end
  2475. else
  2476. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2477. writeln;
  2478. end;
  2479. for i:=0 to LabelElements.Count-1 do
  2480. begin
  2481. El:=TPasElement(LabelElements[i]);
  2482. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2483. write(' El=',GetObjName(El));
  2484. writeln;
  2485. end;
  2486. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2487. finally
  2488. LabelElements.Free;
  2489. ReferenceElements.Free;
  2490. end;
  2491. end;
  2492. procedure CheckDirectReference(aMarker: PSrcMarker);
  2493. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2494. var
  2495. aLabel: PSrcMarker;
  2496. ReferenceElements, LabelElements: TFPList;
  2497. i, LabelLine, LabelCol, j: Integer;
  2498. El, LabelEl: TPasElement;
  2499. DeclEl, TypeEl: TPasType;
  2500. begin
  2501. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2502. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2503. if aLabel=nil then
  2504. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2505. LabelElements:=nil;
  2506. ReferenceElements:=nil;
  2507. try
  2508. //writeln('CheckDirectReference finding elements at label ...');
  2509. LabelElements:=FindElementsAt(aLabel);
  2510. //writeln('CheckDirectReference finding elements at reference ...');
  2511. ReferenceElements:=FindElementsAt(aMarker);
  2512. for i:=0 to ReferenceElements.Count-1 do
  2513. begin
  2514. El:=TPasElement(ReferenceElements[i]);
  2515. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2516. if El.ClassType=TPasVariable then
  2517. begin
  2518. if TPasVariable(El).VarType=nil then
  2519. begin
  2520. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2521. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2522. end;
  2523. TypeEl:=TPasVariable(El).VarType;
  2524. for j:=0 to LabelElements.Count-1 do
  2525. begin
  2526. LabelEl:=TPasElement(LabelElements[j]);
  2527. if TypeEl=LabelEl then
  2528. exit; // success
  2529. end;
  2530. end
  2531. else if El is TPasAliasType then
  2532. begin
  2533. DeclEl:=TPasAliasType(El).DestType;
  2534. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2535. if (aLabel^.Filename=DeclEl.SourceFilename)
  2536. and (integer(aLabel^.Row)=LabelLine)
  2537. and (aLabel^.StartCol<=LabelCol)
  2538. and (aLabel^.EndCol>=LabelCol) then
  2539. exit; // success
  2540. end
  2541. else if El.ClassType=TPasArgument then
  2542. begin
  2543. TypeEl:=TPasArgument(El).ArgType;
  2544. for j:=0 to LabelElements.Count-1 do
  2545. begin
  2546. LabelEl:=TPasElement(LabelElements[j]);
  2547. if TypeEl=LabelEl then
  2548. exit; // success
  2549. end;
  2550. end;
  2551. end;
  2552. // failed -> show candidates
  2553. writeln('CheckDirectReference failed: Labels:');
  2554. for j:=0 to LabelElements.Count-1 do
  2555. begin
  2556. LabelEl:=TPasElement(LabelElements[j]);
  2557. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2558. end;
  2559. writeln('CheckDirectReference failed: References:');
  2560. for i:=0 to ReferenceElements.Count-1 do
  2561. begin
  2562. El:=TPasElement(ReferenceElements[i]);
  2563. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2564. //if EL is TPasVariable then
  2565. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2566. end;
  2567. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2568. finally
  2569. LabelElements.Free;
  2570. ReferenceElements.Free;
  2571. end;
  2572. end;
  2573. var
  2574. aMarker: PSrcMarker;
  2575. i: Integer;
  2576. SrcLines: TStringList;
  2577. begin
  2578. Module.ForEachCall(@OnCheckElementParent,nil);
  2579. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2580. // find all markers
  2581. for i:=0 to FileResolver.Streams.Count-1 do
  2582. begin
  2583. GetSrc(i,SrcLines,CurFilename);
  2584. ParseCode(SrcLines,CurFilename);
  2585. SrcLines.Free;
  2586. end;
  2587. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2588. // check references
  2589. aMarker:=FirstSrcMarker;
  2590. while aMarker<>nil do
  2591. begin
  2592. case aMarker^.Kind of
  2593. mkResolverReference: CheckResolverReference(aMarker);
  2594. mkDirectReference: CheckDirectReference(aMarker);
  2595. end;
  2596. aMarker:=aMarker^.Next;
  2597. end;
  2598. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2599. end;
  2600. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2601. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2602. var
  2603. i: Integer;
  2604. Item: TTestHintMessage;
  2605. Expected,Actual: string;
  2606. begin
  2607. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2608. for i:=0 to MsgCount-1 do
  2609. begin
  2610. Item:=Msgs[i];
  2611. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2612. if (Marker<>nil) then
  2613. begin
  2614. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2615. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2616. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2617. end;
  2618. // found
  2619. FHintMsgsGood.Add(Item);
  2620. str(Item.MsgType,Actual);
  2621. str(MsgType,Expected);
  2622. AssertEquals('MsgType',Expected,Actual);
  2623. exit;
  2624. end;
  2625. // needed message missing -> show emitted messages
  2626. WriteSources('',0,0);
  2627. for i:=0 to MsgCount-1 do
  2628. begin
  2629. Item:=Msgs[i];
  2630. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2631. ' ('+IntToStr(Item.MsgNumber),')');
  2632. if Marker<>nil then
  2633. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2634. writeln(' {',Item.Msg,'}');
  2635. end;
  2636. str(MsgType,Expected);
  2637. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2638. if Marker<>nil then
  2639. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2640. Actual:=Actual+' '+Msg;
  2641. Fail(Actual);
  2642. end;
  2643. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2644. );
  2645. var
  2646. i: Integer;
  2647. s, Txt: String;
  2648. Msg: TTestHintMessage;
  2649. begin
  2650. for i:=0 to MsgCount-1 do
  2651. begin
  2652. Msg:=Msgs[i];
  2653. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2654. s:='';
  2655. str(Msg.MsgType,s);
  2656. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2657. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2658. if WithSourcePos then
  2659. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2660. Txt:=Txt+' {'+Msg.Msg+'}';
  2661. Fail(Txt);
  2662. end;
  2663. end;
  2664. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2665. MsgNumber: integer);
  2666. begin
  2667. ExpectedErrorClass:=EScannerError;
  2668. ExpectedErrorMsg:=Msg;
  2669. ExpectedErrorNumber:=MsgNumber;
  2670. end;
  2671. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2672. MsgNumber: integer);
  2673. begin
  2674. ExpectedErrorClass:=EParserError;
  2675. ExpectedErrorMsg:=Msg;
  2676. ExpectedErrorNumber:=MsgNumber;
  2677. end;
  2678. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2679. MsgNumber: integer);
  2680. begin
  2681. ExpectedErrorClass:=EPasResolve;
  2682. ExpectedErrorMsg:=Msg;
  2683. ExpectedErrorNumber:=MsgNumber;
  2684. end;
  2685. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2686. MsgNumber: integer);
  2687. begin
  2688. ExpectedErrorClass:=EPas2JS;
  2689. ExpectedErrorMsg:=Msg;
  2690. ExpectedErrorNumber:=MsgNumber;
  2691. end;
  2692. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2693. var
  2694. MsgNumber: Integer;
  2695. Msg: String;
  2696. begin
  2697. Result:=false;
  2698. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2699. Msg:=E.Message;
  2700. if E is EPas2JS then
  2701. MsgNumber:=EPas2JS(E).MsgNumber
  2702. else if E is EPasResolve then
  2703. MsgNumber:=EPasResolve(E).MsgNumber
  2704. else if E is EParserError then
  2705. MsgNumber:=Parser.LastMsgNumber
  2706. else if E is EScannerError then
  2707. begin
  2708. MsgNumber:=Scanner.LastMsgNumber;
  2709. Msg:=Scanner.LastMsg;
  2710. end
  2711. else
  2712. MsgNumber:=0;
  2713. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2714. if Result then
  2715. SkipTests:=true;
  2716. end;
  2717. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2718. const aFilename: string; aRow, aCol: integer);
  2719. var
  2720. s: String;
  2721. begin
  2722. WriteSources(aFilename,aRow,aCol);
  2723. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2724. writeln('ERROR: ',s);
  2725. Fail(s);
  2726. end;
  2727. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2728. aMarker: PSrcMarker);
  2729. begin
  2730. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2731. end;
  2732. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2733. begin
  2734. if IsErrorExpected(E) then exit;
  2735. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2736. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2737. +' '+Scanner.CurFilename
  2738. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2739. FailException(E);
  2740. end;
  2741. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2742. begin
  2743. if IsErrorExpected(E) then exit;
  2744. WriteSources(E.Filename,E.Row,E.Column);
  2745. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2746. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2747. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2748. );
  2749. FailException(E);
  2750. end;
  2751. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2752. var
  2753. P: TPasSourcePos;
  2754. begin
  2755. if IsErrorExpected(E) then exit;
  2756. P:=E.SourcePos;
  2757. WriteSources(P.FileName,P.Row,P.Column);
  2758. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2759. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2760. FailException(E);
  2761. end;
  2762. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2763. var
  2764. Row, Col: integer;
  2765. begin
  2766. if IsErrorExpected(E) then exit;
  2767. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2768. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2769. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2770. +' '+E.PasElement.SourceFilename
  2771. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2772. FailException(E);
  2773. end;
  2774. procedure TCustomTestModule.HandleException(E: Exception);
  2775. begin
  2776. if E is EScannerError then
  2777. HandleScannerError(EScannerError(E))
  2778. else if E is EParserError then
  2779. HandleParserError(EParserError(E))
  2780. else if E is EPasResolve then
  2781. HandlePasResolveError(EPasResolve(E))
  2782. else if E is EPas2JS then
  2783. HandlePas2JSError(EPas2JS(E))
  2784. else
  2785. begin
  2786. if IsErrorExpected(E) then exit;
  2787. if not (E is EAssertionFailedError) then
  2788. begin
  2789. WriteSources('',0,0);
  2790. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2791. end;
  2792. FailException(E);
  2793. end;
  2794. end;
  2795. procedure TCustomTestModule.FailException(E: Exception);
  2796. var
  2797. MsgNumber: Integer;
  2798. begin
  2799. if ExpectedErrorClass<>nil then
  2800. begin
  2801. if FExpectedErrorClass=E.ClassType then
  2802. begin
  2803. if E is EPas2JS then
  2804. MsgNumber:=EPas2JS(E).MsgNumber
  2805. else if E is EPasResolve then
  2806. MsgNumber:=EPasResolve(E).MsgNumber
  2807. else if E is EParserError then
  2808. MsgNumber:=Parser.LastMsgNumber
  2809. else if E is EScannerError then
  2810. MsgNumber:=Scanner.LastMsgNumber
  2811. else
  2812. MsgNumber:=0;
  2813. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2814. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2815. ExpectedErrorNumber,MsgNumber);
  2816. end else begin
  2817. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2818. end;
  2819. end;
  2820. Fail(E.Message);
  2821. end;
  2822. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2823. aCol: integer);
  2824. var
  2825. IsSrc: Boolean;
  2826. i, j: Integer;
  2827. SrcLines: TStringList;
  2828. Line: string;
  2829. aModule: TTestEnginePasResolver;
  2830. begin
  2831. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2832. for i:=0 to ResolverCount-1 do
  2833. begin
  2834. aModule:=Resolvers[i];
  2835. SrcLines:=TStringList.Create;
  2836. try
  2837. SrcLines.Text:=aModule.Source;
  2838. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2839. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2840. for j:=1 to SrcLines.Count do
  2841. begin
  2842. Line:=SrcLines[j-1];
  2843. if IsSrc and (j=aRow) then
  2844. begin
  2845. write('*');
  2846. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2847. end;
  2848. writeln(Format('%:4d: ',[j]),Line);
  2849. end;
  2850. finally
  2851. SrcLines.Free;
  2852. end;
  2853. end;
  2854. end;
  2855. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2856. var
  2857. i: Integer;
  2858. begin
  2859. for i:=0 to ResolverCount-1 do
  2860. if Filename=Resolvers[i].Filename then exit(i);
  2861. Result:=-1;
  2862. end;
  2863. function TCustomTestModule.GetResolver(const Filename: string
  2864. ): TTestEnginePasResolver;
  2865. var
  2866. i: Integer;
  2867. begin
  2868. i:=IndexOfResolver(Filename);
  2869. if i<0 then exit(nil);
  2870. Result:=Resolvers[i];
  2871. end;
  2872. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2873. out aFilename: string);
  2874. var
  2875. aStream: TStream;
  2876. begin
  2877. SrcLines:=TStringList.Create;
  2878. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2879. aStream.Position:=0;
  2880. SrcLines.LoadFromStream(aStream);
  2881. aFilename:=FileResolver.Streams[Index];
  2882. end;
  2883. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2884. aEndCol: integer): TFPList;
  2885. var
  2886. ok: Boolean;
  2887. FoundRefs: TTestResolverReferenceData;
  2888. i: Integer;
  2889. CurResolver: TTestEnginePasResolver;
  2890. begin
  2891. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2892. FoundRefs:=Default(TTestResolverReferenceData);
  2893. FoundRefs.Filename:=aFilename;
  2894. FoundRefs.Row:=aLine;
  2895. FoundRefs.StartCol:=aStartCol;
  2896. FoundRefs.EndCol:=aEndCol;
  2897. FoundRefs.Found:=TFPList.Create;
  2898. ok:=false;
  2899. try
  2900. // find all markers
  2901. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2902. for i:=0 to ResolverCount-1 do
  2903. begin
  2904. CurResolver:=Resolvers[i];
  2905. if CurResolver.Module=Module then continue;
  2906. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2907. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2908. end;
  2909. ok:=true;
  2910. finally
  2911. if not ok then
  2912. FreeAndNil(FoundRefs.Found);
  2913. end;
  2914. Result:=FoundRefs.Found;
  2915. FoundRefs.Found:=nil;
  2916. end;
  2917. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2918. ErrorOnNoElements: boolean): TFPList;
  2919. begin
  2920. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2921. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2922. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2923. end;
  2924. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2925. begin
  2926. Result:=FirstSrcMarker;
  2927. while Result<>nil do
  2928. begin
  2929. if (Result^.Kind=mkLabel)
  2930. and (CompareText(Result^.Identifier,Identifier)=0) then
  2931. exit;
  2932. Result:=Result^.Next;
  2933. end;
  2934. end;
  2935. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2936. ErrorOnNoElements: boolean): TFPList;
  2937. var
  2938. SrcLabel: PSrcMarker;
  2939. begin
  2940. SrcLabel:=FindSrcLabel(Identifier);
  2941. if SrcLabel=nil then
  2942. Fail('missing label "'+Identifier+'"');
  2943. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2944. end;
  2945. function TCustomTestModule.GetDefaultNamespace: string;
  2946. var
  2947. C: TClass;
  2948. begin
  2949. Result:='';
  2950. if FModule=nil then exit;
  2951. C:=FModule.ClassType;
  2952. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2953. Result:=ResolverEngine.DefaultNameSpace;
  2954. end;
  2955. constructor TCustomTestModule.Create;
  2956. begin
  2957. inherited Create;
  2958. FHintMsgs:=TObjectList.Create(true);
  2959. FHintMsgsGood:=TFPList.Create;
  2960. end;
  2961. destructor TCustomTestModule.Destroy;
  2962. begin
  2963. FreeAndNil(FHintMsgs);
  2964. FreeAndNil(FHintMsgsGood);
  2965. inherited Destroy;
  2966. end;
  2967. { TTestModule }
  2968. procedure TTestModule.TestReservedWords;
  2969. var
  2970. i: integer;
  2971. begin
  2972. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2973. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2974. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2975. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2976. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2977. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2978. end;
  2979. procedure TTestModule.TestEmptyProgram;
  2980. begin
  2981. StartProgram(false);
  2982. Add('begin');
  2983. ConvertProgram;
  2984. CheckSource('TestEmptyProgram','','');
  2985. end;
  2986. procedure TTestModule.TestEmptyProgramUseStrict;
  2987. begin
  2988. Converter.Options:=Converter.Options+[coUseStrict];
  2989. StartProgram(false);
  2990. Add('begin');
  2991. ConvertProgram;
  2992. CheckSource('TestEmptyProgramUseStrict','','');
  2993. end;
  2994. procedure TTestModule.TestEmptyUnit;
  2995. begin
  2996. StartUnit(false);
  2997. Add('interface');
  2998. Add('implementation');
  2999. ConvertUnit;
  3000. CheckSource('TestEmptyUnit',
  3001. LinesToStr([
  3002. ]),
  3003. '');
  3004. end;
  3005. procedure TTestModule.TestEmptyUnitUseStrict;
  3006. begin
  3007. Converter.Options:=Converter.Options+[coUseStrict];
  3008. StartUnit(false);
  3009. Add('interface');
  3010. Add('implementation');
  3011. ConvertUnit;
  3012. CheckSource('TestEmptyUnitUseStrict',
  3013. LinesToStr([
  3014. ''
  3015. ]),
  3016. '');
  3017. end;
  3018. procedure TTestModule.TestDottedUnitNames;
  3019. begin
  3020. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3021. LinesToStr([
  3022. 'var iV: longint;'
  3023. ]),
  3024. '');
  3025. FFilename:='ns1.test1.pp';
  3026. StartProgram(true);
  3027. Add('uses unIt2;');
  3028. Add('var');
  3029. Add(' i: longint;');
  3030. Add('begin');
  3031. Add(' i:=iv;');
  3032. Add(' i:=uNit2.iv;');
  3033. Add(' i:=Ns1.TEst1.i;');
  3034. ConvertProgram;
  3035. CheckSource('TestDottedUnitNames',
  3036. LinesToStr([
  3037. 'this.i = 0;',
  3038. '']),
  3039. LinesToStr([ // this.$init
  3040. '$mod.i = pas["NS1.Unit2"].iV;',
  3041. '$mod.i = pas["NS1.Unit2"].iV;',
  3042. '$mod.i = $mod.i;',
  3043. '']) );
  3044. end;
  3045. procedure TTestModule.TestDottedUnitNameImpl;
  3046. begin
  3047. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3048. LinesToStr([
  3049. 'type',
  3050. ' TObject = class end;',
  3051. ' TTestA = class',
  3052. ' end;'
  3053. ]),
  3054. LinesToStr(['uses TEST.UnitB;'])
  3055. );
  3056. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3057. LinesToStr([
  3058. 'uses TEST.UnitA;',
  3059. 'type TTestB = class(TTestA);'
  3060. ]),
  3061. ''
  3062. );
  3063. StartProgram(true);
  3064. Add('uses TEST.UnitA;');
  3065. Add('begin');
  3066. ConvertProgram;
  3067. CheckSource('TestDottedUnitNameImpl',
  3068. LinesToStr([
  3069. '']),
  3070. LinesToStr([ // this.$init
  3071. '']) );
  3072. CheckUnit('TEST.UnitA.pas',
  3073. LinesToStr([
  3074. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3075. ' var $mod = this;',
  3076. ' rtl.createClass(this, "TObject", null, function () {',
  3077. ' this.$init = function () {',
  3078. ' };',
  3079. ' this.$final = function () {',
  3080. ' };',
  3081. ' });',
  3082. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3083. ' });',
  3084. '}, ["TEST.UnitB"]);'
  3085. ]));
  3086. CheckUnit('TEST.UnitB.pas',
  3087. LinesToStr([
  3088. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3089. ' var $mod = this;',
  3090. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3091. ' });',
  3092. '});'
  3093. ]));
  3094. end;
  3095. procedure TTestModule.TestDottedUnitExpr;
  3096. begin
  3097. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3098. LinesToStr([
  3099. 'procedure DoIt;'
  3100. ]),
  3101. 'procedure DoIt; begin end;');
  3102. FFilename:='Ns1.SubNs1.Test1.pp';
  3103. StartProgram(true);
  3104. Add('uses Ns2.sUbnS2.unIt2;');
  3105. Add('var');
  3106. Add(' i: longint;');
  3107. Add('begin');
  3108. Add(' ns2.subns2.unit2.doit;');
  3109. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3110. ConvertProgram;
  3111. CheckSource('TestDottedUnitExpr',
  3112. LinesToStr([
  3113. 'this.i = 0;',
  3114. '']),
  3115. LinesToStr([ // this.$init
  3116. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3117. '$mod.i = $mod.i;',
  3118. '']) );
  3119. end;
  3120. procedure TTestModule.Test_ModeFPCFail;
  3121. begin
  3122. StartProgram(false);
  3123. Add('{$mode FPC}');
  3124. Add('begin');
  3125. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3126. ConvertProgram;
  3127. end;
  3128. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3129. begin
  3130. StartProgram(false);
  3131. Add('{$modeswitch cblocks-}');
  3132. Add('begin');
  3133. ConvertProgram;
  3134. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3135. CheckResolverUnexpectedHints();
  3136. end;
  3137. procedure TTestModule.TestUnit_UseSystem;
  3138. begin
  3139. StartUnit(true);
  3140. Add([
  3141. 'interface',
  3142. 'var i: integer;',
  3143. 'implementation']);
  3144. ConvertUnit;
  3145. CheckSource('TestUnit_UseSystem',
  3146. LinesToStr([
  3147. 'this.i = 0;',
  3148. '']),
  3149. LinesToStr([
  3150. '']) );
  3151. end;
  3152. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3153. begin
  3154. AddModuleWithIntfImplSrc('unit1.pp',
  3155. LinesToStr([
  3156. 'type number = longint;']),
  3157. LinesToStr([
  3158. 'uses test1;',
  3159. 'procedure DoIt;',
  3160. 'begin',
  3161. ' i:=3;',
  3162. 'end;']));
  3163. StartUnit(true);
  3164. Add([
  3165. 'interface',
  3166. 'uses unit1;',
  3167. 'var i: number;',
  3168. 'implementation']);
  3169. ConvertUnit;
  3170. CheckSource('TestUnit_Intf1Impl2Intf1',
  3171. LinesToStr([
  3172. 'this.i = 0;',
  3173. '']),
  3174. LinesToStr([
  3175. '']) );
  3176. end;
  3177. procedure TTestModule.TestIncludeVersion;
  3178. begin
  3179. StartProgram(false);
  3180. Add([
  3181. 'var',
  3182. ' s: string;',
  3183. ' i: word;',
  3184. 'begin',
  3185. ' s:={$I %line%};',
  3186. ' i:={$I %linenum%};',
  3187. ' s:={$I %currentroutine%};',
  3188. ' s:={$I %pas2jsversion%};',
  3189. ' s:={$I %pas2jstarget%};',
  3190. ' s:={$I %pas2jstargetos%};',
  3191. ' s:={$I %pas2jstargetcpu%};',
  3192. ' s:={$I %file%};',
  3193. '']);
  3194. ConvertProgram;
  3195. CheckSource('TestIncludeVersion',
  3196. LinesToStr([
  3197. 'this.s="";',
  3198. 'this.i = 0;']),
  3199. LinesToStr([
  3200. '$mod.s = "7";',
  3201. '$mod.i = 8;',
  3202. '$mod.s = "<anonymous>";',
  3203. '$mod.s = "Comp.Ver.tcmodules";',
  3204. '$mod.s = "Browser";',
  3205. '$mod.s = "Browser";',
  3206. '$mod.s = "ECMAScript5";',
  3207. '$mod.s = "test1.pp";',
  3208. '']));
  3209. end;
  3210. procedure TTestModule.TestVarInt;
  3211. begin
  3212. StartProgram(false);
  3213. Add('var MyI: longint;');
  3214. Add('begin');
  3215. ConvertProgram;
  3216. CheckSource('TestVarInt','this.MyI=0;','');
  3217. end;
  3218. procedure TTestModule.TestVarBaseTypes;
  3219. begin
  3220. StartProgram(false);
  3221. Add('var');
  3222. Add(' i: longint;');
  3223. Add(' s: string;');
  3224. Add(' c: char;');
  3225. Add(' b: boolean;');
  3226. Add(' d: double;');
  3227. Add(' i2: longint = 3;');
  3228. Add(' s2: string = ''foo'';');
  3229. Add(' c2: char = ''4'';');
  3230. Add(' b2: boolean = true;');
  3231. Add(' d2: double = 5.6;');
  3232. Add(' i3: longint = $707;');
  3233. Add(' i4: nativeint = 9007199254740991;');
  3234. Add(' i5: nativeint = -9007199254740991-1;');
  3235. Add(' i6: nativeint = $fffffffffffff;');
  3236. Add(' i7: nativeint = -$fffffffffffff-1;');
  3237. Add(' i8: byte = 00;');
  3238. Add(' u8: nativeuint = $fffffffffffff;');
  3239. Add(' u9: nativeuint = $0000000000000;');
  3240. Add(' u10: nativeuint = $00ff00;');
  3241. Add('begin');
  3242. ConvertProgram;
  3243. CheckSource('TestVarBaseTypes',
  3244. LinesToStr([
  3245. 'this.i = 0;',
  3246. 'this.s = "";',
  3247. 'this.c = "";',
  3248. 'this.b = false;',
  3249. 'this.d = 0.0;',
  3250. 'this.i2 = 3;',
  3251. 'this.s2 = "foo";',
  3252. 'this.c2 = "4";',
  3253. 'this.b2 = true;',
  3254. 'this.d2 = 5.6;',
  3255. 'this.i3 = 0x707;',
  3256. 'this.i4 = 9007199254740991;',
  3257. 'this.i5 = -9007199254740991-1;',
  3258. 'this.i6 = 0xfffffffffffff;',
  3259. 'this.i7 =-0xfffffffffffff-1;',
  3260. 'this.i8 = 0;',
  3261. 'this.u8 = 0xfffffffffffff;',
  3262. 'this.u9 = 0x0;',
  3263. 'this.u10 = 0xff00;'
  3264. ]),
  3265. '');
  3266. end;
  3267. procedure TTestModule.TestBaseTypeSingleFail;
  3268. begin
  3269. StartProgram(false);
  3270. Add('var s: single;');
  3271. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3272. ConvertProgram;
  3273. end;
  3274. procedure TTestModule.TestBaseTypeExtendedFail;
  3275. begin
  3276. StartProgram(false);
  3277. Add('var e: extended;');
  3278. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3279. ConvertProgram;
  3280. end;
  3281. procedure TTestModule.TestConstBaseTypes;
  3282. begin
  3283. StartProgram(false);
  3284. Add('const');
  3285. Add(' i: longint = 3;');
  3286. Add(' s: string = ''foo'';');
  3287. Add(' c: char = ''4'';');
  3288. Add(' b: boolean = true;');
  3289. Add(' d: double = 5.6;');
  3290. Add(' e = low(word);');
  3291. Add(' f = high(word);');
  3292. Add('begin');
  3293. ConvertProgram;
  3294. CheckSource('TestVarBaseTypes',
  3295. LinesToStr([
  3296. 'this.i=3;',
  3297. 'this.s="foo";',
  3298. 'this.c="4";',
  3299. 'this.b=true;',
  3300. 'this.d=5.6;',
  3301. 'this.e = 0;',
  3302. 'this.f = 65535;'
  3303. ]),
  3304. '');
  3305. end;
  3306. procedure TTestModule.TestAliasTypeRef;
  3307. begin
  3308. StartProgram(false);
  3309. Add('type');
  3310. Add(' a=longint;');
  3311. Add(' b=a;');
  3312. Add('var');
  3313. Add(' c: A;');
  3314. Add(' d: B;');
  3315. Add('begin');
  3316. ConvertProgram;
  3317. CheckSource('TestAliasTypeRef',
  3318. LinesToStr([ // statements
  3319. 'this.c = 0;',
  3320. 'this.d = 0;'
  3321. ]),
  3322. LinesToStr([ // this.$main
  3323. ''
  3324. ]));
  3325. end;
  3326. procedure TTestModule.TestTypeCast_BaseTypes;
  3327. begin
  3328. StartProgram(false);
  3329. Add([
  3330. 'var',
  3331. ' i: longint;',
  3332. ' b: boolean;',
  3333. ' d: double;',
  3334. ' s: string;',
  3335. ' c: char;',
  3336. 'begin',
  3337. ' i:=longint(i);',
  3338. ' i:=longint(b);',
  3339. ' b:=boolean(b);',
  3340. ' b:=boolean(i);',
  3341. ' d:=double(d);',
  3342. ' d:=double(i);',
  3343. ' s:=string(s);',
  3344. ' s:=string(c);',
  3345. ' c:=char(c);',
  3346. ' c:=char(i);',
  3347. ' c:=char(65);',
  3348. ' c:=char(#10);',
  3349. ' c:=char(#$E000);',
  3350. '']);
  3351. ConvertProgram;
  3352. CheckSource('TestAliasTypeRef',
  3353. LinesToStr([ // statements
  3354. 'this.i = 0;',
  3355. 'this.b = false;',
  3356. 'this.d = 0.0;',
  3357. 'this.s = "";',
  3358. 'this.c = "";',
  3359. '']),
  3360. LinesToStr([ // this.$main
  3361. '$mod.i = $mod.i;',
  3362. '$mod.i = ($mod.b ? 1 : 0);',
  3363. '$mod.b = $mod.b;',
  3364. '$mod.b = $mod.i != 0;',
  3365. '$mod.d = $mod.d;',
  3366. '$mod.d = $mod.i;',
  3367. '$mod.s = $mod.s;',
  3368. '$mod.s = $mod.c;',
  3369. '$mod.c = $mod.c;',
  3370. '$mod.c = String.fromCharCode($mod.i);',
  3371. '$mod.c = "A";',
  3372. '$mod.c = "\n";',
  3373. '$mod.c = "";',
  3374. '']));
  3375. end;
  3376. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3377. begin
  3378. StartProgram(false);
  3379. Add('type');
  3380. Add(' integer = longint;');
  3381. Add(' TYesNo = boolean;');
  3382. Add(' TFloat = double;');
  3383. Add(' TCaption = string;');
  3384. Add(' TChar = char;');
  3385. Add('var');
  3386. Add(' i: integer;');
  3387. Add(' b: TYesNo;');
  3388. Add(' d: TFloat;');
  3389. Add(' s: TCaption;');
  3390. Add(' c: TChar;');
  3391. Add('begin');
  3392. Add(' i:=integer(i);');
  3393. Add(' i:=integer(b);');
  3394. Add(' b:=TYesNo(b);');
  3395. Add(' b:=TYesNo(i);');
  3396. Add(' d:=TFloat(d);');
  3397. Add(' d:=TFloat(i);');
  3398. Add(' s:=TCaption(s);');
  3399. Add(' s:=TCaption(c);');
  3400. Add(' c:=TChar(c);');
  3401. ConvertProgram;
  3402. CheckSource('TestAliasTypeRef',
  3403. LinesToStr([ // statements
  3404. 'this.i = 0;',
  3405. 'this.b = false;',
  3406. 'this.d = 0.0;',
  3407. 'this.s = "";',
  3408. 'this.c = "";',
  3409. '']),
  3410. LinesToStr([ // this.$main
  3411. '$mod.i = $mod.i;',
  3412. '$mod.i = ($mod.b ? 1 : 0);',
  3413. '$mod.b = $mod.b;',
  3414. '$mod.b = $mod.i != 0;',
  3415. '$mod.d = $mod.d;',
  3416. '$mod.d = $mod.i;',
  3417. '$mod.s = $mod.s;',
  3418. '$mod.s = $mod.c;',
  3419. '$mod.c = $mod.c;',
  3420. '']));
  3421. end;
  3422. procedure TTestModule.TestEmptyProc;
  3423. begin
  3424. StartProgram(false);
  3425. Add('procedure Test;');
  3426. Add('begin');
  3427. Add('end;');
  3428. Add('begin');
  3429. ConvertProgram;
  3430. CheckSource('TestEmptyProc',
  3431. LinesToStr([ // statements
  3432. 'this.Test = function () {',
  3433. '};'
  3434. ]),
  3435. LinesToStr([ // this.$main
  3436. ''
  3437. ]));
  3438. end;
  3439. procedure TTestModule.TestProcOneParam;
  3440. begin
  3441. StartProgram(false);
  3442. Add('procedure ProcA(i: longint);');
  3443. Add('begin');
  3444. Add('end;');
  3445. Add('begin');
  3446. Add(' PROCA(3);');
  3447. ConvertProgram;
  3448. CheckSource('TestProcOneParam',
  3449. LinesToStr([ // statements
  3450. 'this.ProcA = function (i) {',
  3451. '};'
  3452. ]),
  3453. LinesToStr([ // this.$main
  3454. '$mod.ProcA(3);'
  3455. ]));
  3456. end;
  3457. procedure TTestModule.TestFunctionWithoutParams;
  3458. begin
  3459. StartProgram(false);
  3460. Add('function FuncA: longint;');
  3461. Add('begin');
  3462. Add('end;');
  3463. Add('var i: longint;');
  3464. Add('begin');
  3465. Add(' I:=FUNCA();');
  3466. Add(' I:=FUNCA;');
  3467. Add(' FUNCA();');
  3468. Add(' FUNCA;');
  3469. ConvertProgram;
  3470. CheckSource('TestProcWithoutParams',
  3471. LinesToStr([ // statements
  3472. 'this.FuncA = function () {',
  3473. ' var Result = 0;',
  3474. ' return Result;',
  3475. '};',
  3476. 'this.i=0;'
  3477. ]),
  3478. LinesToStr([ // this.$main
  3479. '$mod.i=$mod.FuncA();',
  3480. '$mod.i=$mod.FuncA();',
  3481. '$mod.FuncA();',
  3482. '$mod.FuncA();'
  3483. ]));
  3484. end;
  3485. procedure TTestModule.TestProcedureWithoutParams;
  3486. begin
  3487. StartProgram(false);
  3488. Add('procedure ProcA;');
  3489. Add('begin');
  3490. Add('end;');
  3491. Add('begin');
  3492. Add(' PROCA();');
  3493. Add(' PROCA;');
  3494. ConvertProgram;
  3495. CheckSource('TestProcWithoutParams',
  3496. LinesToStr([ // statements
  3497. 'this.ProcA = function () {',
  3498. '};'
  3499. ]),
  3500. LinesToStr([ // this.$main
  3501. '$mod.ProcA();',
  3502. '$mod.ProcA();'
  3503. ]));
  3504. end;
  3505. procedure TTestModule.TestIncDec;
  3506. begin
  3507. StartProgram(false);
  3508. Add([
  3509. 'procedure DoIt(var i: longint);',
  3510. 'begin',
  3511. ' inc(i);',
  3512. ' inc(i,2);',
  3513. 'end;',
  3514. 'var',
  3515. ' Bar: longint;',
  3516. 'begin',
  3517. ' inc(bar);',
  3518. ' inc(bar,2);',
  3519. ' dec(bar);',
  3520. ' dec(bar,3);',
  3521. '']);
  3522. ConvertProgram;
  3523. CheckSource('TestIncDec',
  3524. LinesToStr([ // statements
  3525. 'this.DoIt = function (i) {',
  3526. ' i.set(i.get()+1);',
  3527. ' i.set(i.get()+2);',
  3528. '};',
  3529. 'this.Bar = 0;'
  3530. ]),
  3531. LinesToStr([ // this.$main
  3532. '$mod.Bar+=1;',
  3533. '$mod.Bar+=2;',
  3534. '$mod.Bar-=1;',
  3535. '$mod.Bar-=3;'
  3536. ]));
  3537. end;
  3538. procedure TTestModule.TestLoHiFpcMode;
  3539. begin
  3540. StartProgram(false);
  3541. Add([
  3542. '{$mode objfpc}',
  3543. 'const',
  3544. ' LoByte1 = Lo(Word($1234));',
  3545. ' HiByte1 = Hi(Word($1234));',
  3546. ' LoByte2 = Lo(SmallInt($1234));',
  3547. ' HiByte2 = Hi(SmallInt($1234));',
  3548. ' LoWord1 = Lo($1234CDEF);',
  3549. ' HiWord1 = Hi($1234CDEF);',
  3550. ' LoWord2 = Lo(-$1234CDEF);',
  3551. ' HiWord2 = Hi(-$1234CDEF);',
  3552. ' lo4:byte=lo(byte($34));',
  3553. ' hi4:byte=hi(byte($34));',
  3554. ' lo5:byte=lo(shortint(-$34));',
  3555. ' hi5:byte=hi(shortint(-$34));',
  3556. ' lo6:longword=lo($123456789ABCD);',
  3557. ' hi6:longword=hi($123456789ABCD);',
  3558. ' lo7:longword=lo(-$123456789ABCD);',
  3559. ' hi7:longword=hi(-$123456789ABCD);',
  3560. 'var',
  3561. ' b: Byte;',
  3562. ' ss: shortint;',
  3563. ' w: Word;',
  3564. ' si: SmallInt;',
  3565. ' lw: LongWord;',
  3566. ' li: LongInt;',
  3567. ' b2: Byte;',
  3568. ' ni: nativeint;',
  3569. 'begin',
  3570. ' w := $1234;',
  3571. ' ss := -$12;',
  3572. ' b := lo(ss);',
  3573. ' b := HI(ss);',
  3574. ' b := lo(w);',
  3575. ' b := HI(w);',
  3576. ' b2 := lo(b);',
  3577. ' b2 := hi(b);',
  3578. ' lw := $1234CDEF;',
  3579. ' w := lo(lw);',
  3580. ' w := hi(lw);',
  3581. ' ni := $123456789ABCD;',
  3582. ' lw := lo(ni);',
  3583. ' lw := hi(ni);',
  3584. '']);
  3585. ConvertProgram;
  3586. CheckSource('TestLoHiFpcMode',
  3587. LinesToStr([ // statements
  3588. 'this.LoByte1 = 0x1234 & 0xFF;',
  3589. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3590. 'this.LoByte2 = 0x1234 & 0xFF;',
  3591. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3592. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3593. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3594. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3595. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3596. 'this.lo4 = 0x34 & 0xF;',
  3597. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3598. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3599. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3600. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3601. 'this.hi6 = 74565 >>> 0;',
  3602. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3603. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3604. 'this.b = 0;',
  3605. 'this.ss = 0;',
  3606. 'this.w = 0;',
  3607. 'this.si = 0;',
  3608. 'this.lw = 0;',
  3609. 'this.li = 0;',
  3610. 'this.b2 = 0;',
  3611. 'this.ni = 0;',
  3612. '']),
  3613. LinesToStr([ // this.$main
  3614. '$mod.w = 0x1234;',
  3615. '$mod.ss = -0x12;',
  3616. '$mod.b = $mod.ss & 0xFF;',
  3617. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3618. '$mod.b = $mod.w & 0xFF;',
  3619. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3620. '$mod.b2 = $mod.b & 0xF;',
  3621. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3622. '$mod.lw = 0x1234CDEF;',
  3623. '$mod.w = $mod.lw & 0xFFFF;',
  3624. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3625. '$mod.ni = 0x123456789ABCD;',
  3626. '$mod.lw = $mod.ni >>> 0;',
  3627. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3628. '']));
  3629. end;
  3630. procedure TTestModule.TestLoHiDelphiMode;
  3631. begin
  3632. StartProgram(false);
  3633. Add([
  3634. '{$mode delphi}',
  3635. 'const',
  3636. ' LoByte1 = Lo(Word($1234));',
  3637. ' HiByte1 = Hi(Word($1234));',
  3638. ' LoByte2 = Lo(SmallInt($1234));',
  3639. ' HiByte2 = Hi(SmallInt($1234));',
  3640. ' LoByte3 = Lo($1234CDEF);',
  3641. ' HiByte3 = Hi($1234CDEF);',
  3642. ' LoByte4 = Lo(-$1234CDEF);',
  3643. ' HiByte4 = Hi(-$1234CDEF);',
  3644. 'var',
  3645. ' b: Byte;',
  3646. ' w: Word;',
  3647. ' si: SmallInt;',
  3648. ' lw: LongWord;',
  3649. ' li: LongInt;',
  3650. 'begin',
  3651. ' w := $1234;',
  3652. ' b := lo(w);',
  3653. ' b := HI(w);',
  3654. ' lw := $1234CDEF;',
  3655. ' b := lo(lw);',
  3656. ' b := hi(lw);',
  3657. '']);
  3658. ConvertProgram;
  3659. CheckSource('TestLoHiDelphiMode',
  3660. LinesToStr([ // statements
  3661. 'this.LoByte1 = 0x1234 & 0xFF;',
  3662. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3663. 'this.LoByte2 = 0x1234 & 0xFF;',
  3664. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3665. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3666. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3667. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3668. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3669. 'this.b = 0;',
  3670. 'this.w = 0;',
  3671. 'this.si = 0;',
  3672. 'this.lw = 0;',
  3673. 'this.li = 0;'
  3674. ]),
  3675. LinesToStr([ // this.$main
  3676. '$mod.w = 0x1234;',
  3677. '$mod.b = $mod.w & 0xFF;',
  3678. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3679. '$mod.lw = 0x1234CDEF;',
  3680. '$mod.b = $mod.lw & 0xFF;',
  3681. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3682. ]));
  3683. end;
  3684. procedure TTestModule.TestAssignments;
  3685. begin
  3686. StartProgram(false);
  3687. Parser.Options:=Parser.Options+[po_cassignments];
  3688. Add('var');
  3689. Add(' Bar:longint;');
  3690. Add('begin');
  3691. Add(' bar:=3;');
  3692. Add(' bar+=4;');
  3693. Add(' bar-=5;');
  3694. Add(' bar*=6;');
  3695. ConvertProgram;
  3696. CheckSource('TestAssignments',
  3697. LinesToStr([ // statements
  3698. 'this.Bar = 0;'
  3699. ]),
  3700. LinesToStr([ // this.$main
  3701. '$mod.Bar=3;',
  3702. '$mod.Bar+=4;',
  3703. '$mod.Bar-=5;',
  3704. '$mod.Bar*=6;'
  3705. ]));
  3706. end;
  3707. procedure TTestModule.TestArithmeticOperators1;
  3708. begin
  3709. StartProgram(false);
  3710. Add('var');
  3711. Add(' vA,vB,vC:longint;');
  3712. Add('begin');
  3713. Add(' va:=1;');
  3714. Add(' vb:=va+va;');
  3715. Add(' vb:=va div vb;');
  3716. Add(' vb:=va mod vb;');
  3717. Add(' vb:=va+va*vb+va div vb;');
  3718. Add(' vc:=-va;');
  3719. Add(' va:=va-vb;');
  3720. Add(' vb:=va;');
  3721. Add(' if va<vb then vc:=va else vc:=vb;');
  3722. ConvertProgram;
  3723. CheckSource('TestArithmeticOperators1',
  3724. LinesToStr([ // statements
  3725. 'this.vA = 0;',
  3726. 'this.vB = 0;',
  3727. 'this.vC = 0;'
  3728. ]),
  3729. LinesToStr([ // this.$main
  3730. '$mod.vA = 1;',
  3731. '$mod.vB = $mod.vA + $mod.vA;',
  3732. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3733. '$mod.vB = $mod.vA % $mod.vB;',
  3734. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3735. '$mod.vC = -$mod.vA;',
  3736. '$mod.vA = $mod.vA - $mod.vB;',
  3737. '$mod.vB = $mod.vA;',
  3738. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3739. ]));
  3740. end;
  3741. procedure TTestModule.TestMultiAdd;
  3742. begin
  3743. StartProgram(false);
  3744. Add([
  3745. 'function Fly: string; external name ''fly'';',
  3746. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3747. 'var',
  3748. ' Date: double;',
  3749. 'begin',
  3750. ' Result:=(Year>0) and (Year<10000) and',
  3751. ' (Month >= 1) and (Month<=12) and',
  3752. ' (Day>0) and (Day<=31);',
  3753. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3754. 'end;',
  3755. 'var s: string;',
  3756. 'begin',
  3757. ' s:=''a''+''b''+''c''+''d'';',
  3758. ' s:=s+Fly+''e'';',
  3759. ' s:=Fly+Fly+Fly;',
  3760. '']);
  3761. ConvertProgram;
  3762. CheckSource('TestMultiAdd',
  3763. LinesToStr([ // statements
  3764. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3765. ' var Result = false;',
  3766. ' var date = 0.0;',
  3767. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3768. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3769. ' return Result;',
  3770. '};',
  3771. 'this.s = "";',
  3772. '']),
  3773. LinesToStr([ // this.$main
  3774. '$mod.s = "a" + "b" + "c" + "d";',
  3775. '$mod.s = $mod.s + fly() + "e";',
  3776. '$mod.s = fly() + fly() + fly();',
  3777. '']));
  3778. end;
  3779. procedure TTestModule.TestLogicalOperators;
  3780. begin
  3781. StartProgram(false);
  3782. Add('var');
  3783. Add(' vA,vB,vC:boolean;');
  3784. Add('begin');
  3785. Add(' va:=vb and vc;');
  3786. Add(' va:=vb or vc;');
  3787. Add(' va:=vb xor vc;');
  3788. Add(' va:=true and vc;');
  3789. Add(' va:=(vb and vc) or (va and vb);');
  3790. Add(' va:=not vb;');
  3791. ConvertProgram;
  3792. CheckSource('TestLogicalOperators',
  3793. LinesToStr([ // statements
  3794. 'this.vA = false;',
  3795. 'this.vB = false;',
  3796. 'this.vC = false;'
  3797. ]),
  3798. LinesToStr([ // this.$main
  3799. '$mod.vA = $mod.vB && $mod.vC;',
  3800. '$mod.vA = $mod.vB || $mod.vC;',
  3801. '$mod.vA = $mod.vB ^ $mod.vC;',
  3802. '$mod.vA = true && $mod.vC;',
  3803. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3804. '$mod.vA = !$mod.vB;'
  3805. ]));
  3806. end;
  3807. procedure TTestModule.TestBitwiseOperators;
  3808. begin
  3809. StartProgram(false);
  3810. Add([
  3811. 'var',
  3812. ' vA,vB,vC:longint;',
  3813. ' X,Y,Z: nativeint;',
  3814. 'begin',
  3815. ' va:=vb and vc;',
  3816. ' va:=vb or vc;',
  3817. ' va:=vb xor vc;',
  3818. ' va:=vb shl vc;',
  3819. ' va:=vb shr vc;',
  3820. ' va:=3 and vc;',
  3821. ' va:=(vb and vc) or (va and vb);',
  3822. ' va:=not vb;',
  3823. ' X:=Y and Z;',
  3824. ' X:=Y and va;',
  3825. ' X:=Y or Z;',
  3826. ' X:=Y or va;',
  3827. ' X:=Y xor Z;',
  3828. ' X:=Y xor va;',
  3829. '']);
  3830. ConvertProgram;
  3831. CheckSource('TestBitwiseOperators',
  3832. LinesToStr([ // statements
  3833. 'this.vA = 0;',
  3834. 'this.vB = 0;',
  3835. 'this.vC = 0;',
  3836. 'this.X = 0;',
  3837. 'this.Y = 0;',
  3838. 'this.Z = 0;',
  3839. '']),
  3840. LinesToStr([ // this.$main
  3841. '$mod.vA = $mod.vB & $mod.vC;',
  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 = 3 & $mod.vC;',
  3847. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3848. '$mod.vA = ~$mod.vB;',
  3849. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3850. '$mod.X = $mod.Y & $mod.vA;',
  3851. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3852. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3853. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3854. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3855. '']));
  3856. end;
  3857. procedure TTestModule.TestBitwiseOperatorsLongword;
  3858. begin
  3859. StartProgram(false);
  3860. Add([
  3861. 'var',
  3862. ' a,b,c:longword;',
  3863. ' i: longint;',
  3864. 'begin',
  3865. ' a:=$12345678;',
  3866. ' b:=$EDCBA987;',
  3867. ' c:=not a;',
  3868. ' c:=a and b;',
  3869. ' c:=a and $ffff0000;',
  3870. ' c:=a or b;',
  3871. ' c:=a or $ff00ff00;',
  3872. ' c:=a xor b;',
  3873. ' c:=a xor $f0f0f0f0;',
  3874. ' c:=a shl 1;',
  3875. ' c:=a shl 16;',
  3876. ' c:=a shl 24;',
  3877. ' c:=a shl b;',
  3878. ' c:=a shr 1;',
  3879. ' c:=a shr 16;',
  3880. ' c:=a shr 24;',
  3881. ' c:=a shr b;',
  3882. ' c:=(b and c) or (a and b);',
  3883. ' c:=i and a;',
  3884. ' c:=i or a;',
  3885. ' c:=i xor a;',
  3886. '']);
  3887. ConvertProgram;
  3888. CheckSource('TestBitwiseOperatorsLongword',
  3889. LinesToStr([ // statements
  3890. 'this.a = 0;',
  3891. 'this.b = 0;',
  3892. 'this.c = 0;',
  3893. 'this.i = 0;',
  3894. '']),
  3895. LinesToStr([ // this.$main
  3896. '$mod.a = 0x12345678;',
  3897. '$mod.b = 0xEDCBA987;',
  3898. '$mod.c = rtl.lw(~$mod.a);',
  3899. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3900. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3901. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3902. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3903. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3904. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3905. '$mod.c = rtl.lw($mod.a << 1);',
  3906. '$mod.c = rtl.lw($mod.a << 16);',
  3907. '$mod.c = rtl.lw($mod.a << 24);',
  3908. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3909. '$mod.c = rtl.lw($mod.a >>> 1);',
  3910. '$mod.c = rtl.lw($mod.a >>> 16);',
  3911. '$mod.c = rtl.lw($mod.a >>> 24);',
  3912. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3913. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3914. '$mod.c = $mod.i & $mod.a;',
  3915. '$mod.c = $mod.i | $mod.a;',
  3916. '$mod.c = $mod.i ^ $mod.a;',
  3917. '']));
  3918. end;
  3919. procedure TTestModule.TestPrgProcVar;
  3920. begin
  3921. StartProgram(false);
  3922. Add('procedure Proc1;');
  3923. Add('type');
  3924. Add(' t1=longint;');
  3925. Add('var');
  3926. Add(' vA:t1;');
  3927. Add('begin');
  3928. Add('end;');
  3929. Add('begin');
  3930. ConvertProgram;
  3931. CheckSource('TestPrgProcVar',
  3932. LinesToStr([ // statements
  3933. 'this.Proc1 = function () {',
  3934. ' var vA=0;',
  3935. '};'
  3936. ]),
  3937. LinesToStr([ // this.$main
  3938. ''
  3939. ]));
  3940. end;
  3941. procedure TTestModule.TestUnitProcVar;
  3942. begin
  3943. StartUnit(false);
  3944. Add('interface');
  3945. Add('');
  3946. Add('type tA=string; // unit scope');
  3947. Add('procedure Proc1;');
  3948. Add('');
  3949. Add('implementation');
  3950. Add('');
  3951. Add('procedure Proc1;');
  3952. Add('type tA=longint; // local proc scope');
  3953. Add('var v1:tA; // using local tA');
  3954. Add('begin');
  3955. Add('end;');
  3956. Add('var v2:tA; // using interface tA');
  3957. ConvertUnit;
  3958. CheckSource('TestUnitProcVar',
  3959. LinesToStr([ // statements
  3960. 'var $impl = $mod.$impl;',
  3961. 'this.Proc1 = function () {',
  3962. ' var v1 = 0;',
  3963. '};',
  3964. '']),
  3965. // this.$init
  3966. '',
  3967. // implementation
  3968. LinesToStr([
  3969. '$impl.v2 = "";',
  3970. '']));
  3971. end;
  3972. procedure TTestModule.TestImplProc;
  3973. begin
  3974. StartUnit(false);
  3975. Add('interface');
  3976. Add('');
  3977. Add('procedure Proc1;');
  3978. Add('');
  3979. Add('implementation');
  3980. Add('');
  3981. Add('procedure Proc1; begin end;');
  3982. Add('procedure Proc2; begin end;');
  3983. Add('initialization');
  3984. Add(' Proc1;');
  3985. Add(' Proc2;');
  3986. ConvertUnit;
  3987. CheckSource('TestImplProc',
  3988. LinesToStr([ // statements
  3989. 'var $impl = $mod.$impl;',
  3990. 'this.Proc1 = function () {',
  3991. '};',
  3992. '']),
  3993. LinesToStr([ // this.$init
  3994. '$mod.Proc1();',
  3995. '$impl.Proc2();',
  3996. '']),
  3997. LinesToStr([ // implementation
  3998. '$impl.Proc2 = function () {',
  3999. '};',
  4000. ''])
  4001. );
  4002. end;
  4003. procedure TTestModule.TestFunctionResult;
  4004. begin
  4005. StartProgram(false);
  4006. Add('function Func1: longint;');
  4007. Add('begin');
  4008. Add(' Result:=3;');
  4009. Add(' Func1:=4;');
  4010. Add('end;');
  4011. Add('begin');
  4012. ConvertProgram;
  4013. CheckSource('TestFunctionResult',
  4014. LinesToStr([ // statements
  4015. 'this.Func1 = function () {',
  4016. ' var Result = 0;',
  4017. ' Result = 3;',
  4018. ' Result = 4;',
  4019. ' return Result;',
  4020. '};'
  4021. ]),
  4022. '');
  4023. end;
  4024. procedure TTestModule.TestNestedProc;
  4025. begin
  4026. StartProgram(false);
  4027. Add([
  4028. 'var vInUnit: longint;',
  4029. 'function DoIt(pA,pD: longint): longint;',
  4030. 'var',
  4031. ' vB: longint;',
  4032. ' vC: longint;',
  4033. ' function Nesty(pA: longint): longint; ',
  4034. ' var vB: longint;',
  4035. ' begin',
  4036. ' Result:=pa+vb+vc+pd+vInUnit;',
  4037. ' nesty:=3;',
  4038. ' doit:=4;',
  4039. ' exit;',
  4040. ' end;',
  4041. 'begin',
  4042. ' Result:=pa+vb+vc;',
  4043. ' doit:=6;',
  4044. ' exit;',
  4045. 'end;',
  4046. 'begin']);
  4047. ConvertProgram;
  4048. CheckSource('TestNestedProc',
  4049. LinesToStr([ // statements
  4050. 'this.vInUnit = 0;',
  4051. 'this.DoIt = function (pA, pD) {',
  4052. ' var Result = 0;',
  4053. ' var vB = 0;',
  4054. ' var vC = 0;',
  4055. ' function Nesty(pA) {',
  4056. ' var Result$1 = 0;',
  4057. ' var vB = 0;',
  4058. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4059. ' Result$1 = 3;',
  4060. ' Result = 4;',
  4061. ' return Result$1;',
  4062. ' return Result$1;',
  4063. ' };',
  4064. ' Result = pA + vB + vC;',
  4065. ' Result = 6;',
  4066. ' return Result;',
  4067. ' return Result;',
  4068. '};'
  4069. ]),
  4070. '');
  4071. end;
  4072. procedure TTestModule.TestNestedProc_ResultString;
  4073. begin
  4074. StartProgram(false);
  4075. Add([
  4076. 'function DoIt: string;',
  4077. ' function Nesty: string; ',
  4078. ' begin',
  4079. ' nesty:=#65#66;',
  4080. ' nesty[1]:=#67;',
  4081. ' doit:=#68;',
  4082. ' doit[2]:=#69;',
  4083. ' end;',
  4084. 'begin',
  4085. ' doit:=#70;',
  4086. ' doit[3]:=#71;',
  4087. 'end;',
  4088. 'begin']);
  4089. ConvertProgram;
  4090. CheckSource('TestNestedProc_ResultString',
  4091. LinesToStr([ // statements
  4092. 'this.DoIt = function () {',
  4093. ' var Result = "";',
  4094. ' function Nesty() {',
  4095. ' var Result$1 = "";',
  4096. ' Result$1 = "AB";',
  4097. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4098. ' Result = "D";',
  4099. ' Result = rtl.setCharAt(Result, 1, "E");',
  4100. ' return Result$1;',
  4101. ' };',
  4102. ' Result = "F";',
  4103. ' Result = rtl.setCharAt(Result, 2, "G");',
  4104. ' return Result;',
  4105. '};'
  4106. ]),
  4107. '');
  4108. end;
  4109. procedure TTestModule.TestForwardProc;
  4110. begin
  4111. StartProgram(false);
  4112. Add('procedure FuncA(Bar: longint); forward;');
  4113. Add('procedure FuncB(Bar: longint);');
  4114. Add('begin');
  4115. Add(' funca(bar);');
  4116. Add('end;');
  4117. Add('procedure funca(bar: longint);');
  4118. Add('begin');
  4119. Add(' if bar=3 then ;');
  4120. Add('end;');
  4121. Add('begin');
  4122. Add(' funca(4);');
  4123. Add(' funcb(5);');
  4124. ConvertProgram;
  4125. CheckSource('TestForwardProc',
  4126. LinesToStr([ // statements'
  4127. 'this.FuncB = function (Bar) {',
  4128. ' $mod.FuncA(Bar);',
  4129. '};',
  4130. 'this.FuncA = function (Bar) {',
  4131. ' if (Bar === 3);',
  4132. '};'
  4133. ]),
  4134. LinesToStr([
  4135. '$mod.FuncA(4);',
  4136. '$mod.FuncB(5);'
  4137. ])
  4138. );
  4139. end;
  4140. procedure TTestModule.TestNestedForwardProc;
  4141. begin
  4142. StartProgram(false);
  4143. Add('procedure FuncA;');
  4144. Add(' procedure FuncB(i: longint); forward;');
  4145. Add(' procedure FuncC(i: longint);');
  4146. Add(' begin');
  4147. Add(' funcb(i);');
  4148. Add(' end;');
  4149. Add(' procedure FuncB(i: longint);');
  4150. Add(' begin');
  4151. Add(' if i=3 then ;');
  4152. Add(' end;');
  4153. Add('begin');
  4154. Add(' funcc(4)');
  4155. Add('end;');
  4156. Add('begin');
  4157. Add(' funca;');
  4158. ConvertProgram;
  4159. CheckSource('TestNestedForwardProc',
  4160. LinesToStr([ // statements'
  4161. 'this.FuncA = function () {',
  4162. ' function FuncC(i) {',
  4163. ' FuncB(i);',
  4164. ' };',
  4165. ' function FuncB(i) {',
  4166. ' if (i === 3);',
  4167. ' };',
  4168. ' FuncC(4);',
  4169. '};'
  4170. ]),
  4171. LinesToStr([
  4172. '$mod.FuncA();'
  4173. ])
  4174. );
  4175. end;
  4176. procedure TTestModule.TestAssignFunctionResult;
  4177. begin
  4178. StartProgram(false);
  4179. Add('function Func1: longint;');
  4180. Add('begin');
  4181. Add('end;');
  4182. Add('var i: longint;');
  4183. Add('begin');
  4184. Add(' i:=func1();');
  4185. Add(' i:=func1()+func1();');
  4186. ConvertProgram;
  4187. CheckSource('TestAssignFunctionResult',
  4188. LinesToStr([ // statements
  4189. 'this.Func1 = function () {',
  4190. ' var Result = 0;',
  4191. ' return Result;',
  4192. '};',
  4193. 'this.i = 0;'
  4194. ]),
  4195. LinesToStr([
  4196. '$mod.i = $mod.Func1();',
  4197. '$mod.i = $mod.Func1() + $mod.Func1();'
  4198. ]));
  4199. end;
  4200. procedure TTestModule.TestFunctionResultInCondition;
  4201. begin
  4202. StartProgram(false);
  4203. Add('function Func1: longint;');
  4204. Add('begin');
  4205. Add('end;');
  4206. Add('function Func2: boolean;');
  4207. Add('begin');
  4208. Add('end;');
  4209. Add('var i: longint;');
  4210. Add('begin');
  4211. Add(' if func2 then ;');
  4212. Add(' if i=func1() then ;');
  4213. Add(' if i=func1 then ;');
  4214. ConvertProgram;
  4215. CheckSource('TestFunctionResultInCondition',
  4216. LinesToStr([ // statements
  4217. 'this.Func1 = function () {',
  4218. ' var Result = 0;',
  4219. ' return Result;',
  4220. '};',
  4221. 'this.Func2 = function () {',
  4222. ' var Result = false;',
  4223. ' return Result;',
  4224. '};',
  4225. 'this.i = 0;'
  4226. ]),
  4227. LinesToStr([
  4228. 'if ($mod.Func2());',
  4229. 'if ($mod.i === $mod.Func1());',
  4230. 'if ($mod.i === $mod.Func1());'
  4231. ]));
  4232. end;
  4233. procedure TTestModule.TestFunctionResultInForLoop;
  4234. begin
  4235. StartProgram(false);
  4236. Add([
  4237. 'function Func1(a: array of longint): longint;',
  4238. 'begin',
  4239. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4240. ' for Result in a do if a[Result]=0 then exit;',
  4241. 'end;',
  4242. 'begin',
  4243. ' Func1([1,2,3])']);
  4244. ConvertProgram;
  4245. CheckSource('TestFunctionResultInForLoop',
  4246. LinesToStr([ // statements
  4247. 'this.Func1 = function (a) {',
  4248. ' var Result = 0;',
  4249. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4250. ' Result = $l;',
  4251. ' if (a[Result] === 0) return Result;',
  4252. ' };',
  4253. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4254. ' Result = $in[$l1];',
  4255. ' if (a[Result] === 0) return Result;',
  4256. ' };',
  4257. ' return Result;',
  4258. '};',
  4259. '']),
  4260. LinesToStr([
  4261. '$mod.Func1([1, 2, 3]);'
  4262. ]));
  4263. end;
  4264. procedure TTestModule.TestFunctionResultInTypeCast;
  4265. begin
  4266. StartProgram(false);
  4267. Add([
  4268. 'function GetInt: longint;',
  4269. 'begin',
  4270. 'end;',
  4271. 'begin',
  4272. ' if Byte(GetInt)=0 then ;',
  4273. '']);
  4274. ConvertProgram;
  4275. CheckSource('TestFunctionResultInTypeCast',
  4276. LinesToStr([ // statements
  4277. 'this.GetInt = function () {',
  4278. ' var Result = 0;',
  4279. ' return Result;',
  4280. '};',
  4281. '']),
  4282. LinesToStr([
  4283. 'if (($mod.GetInt() & 255) === 0) ;'
  4284. ]));
  4285. end;
  4286. procedure TTestModule.TestExit;
  4287. begin
  4288. StartProgram(false);
  4289. Add('procedure ProcA;');
  4290. Add('begin');
  4291. Add(' exit;');
  4292. Add('end;');
  4293. Add('function FuncB: longint;');
  4294. Add('begin');
  4295. Add(' exit;');
  4296. Add(' exit(3);');
  4297. Add('end;');
  4298. Add('function FuncC: string;');
  4299. Add('begin');
  4300. Add(' exit;');
  4301. Add(' exit(''a'');');
  4302. Add(' exit(''abc'');');
  4303. Add('end;');
  4304. Add('begin');
  4305. Add(' exit;');
  4306. Add(' exit(1);');
  4307. ConvertProgram;
  4308. CheckSource('TestExit',
  4309. LinesToStr([ // statements
  4310. 'this.ProcA = function () {',
  4311. ' return;',
  4312. '};',
  4313. 'this.FuncB = function () {',
  4314. ' var Result = 0;',
  4315. ' return Result;',
  4316. ' return 3;',
  4317. ' return Result;',
  4318. '};',
  4319. 'this.FuncC = function () {',
  4320. ' var Result = "";',
  4321. ' return Result;',
  4322. ' return "a";',
  4323. ' return "abc";',
  4324. ' return Result;',
  4325. '};'
  4326. ]),
  4327. LinesToStr([
  4328. 'return;',
  4329. 'return 1;',
  4330. '']));
  4331. end;
  4332. procedure TTestModule.TestExit_ResultInFinally;
  4333. begin
  4334. StartProgram(false);
  4335. Add([
  4336. 'function Run: word;',
  4337. 'begin',
  4338. ' try',
  4339. ' exit(3);', // no Result in finally -> use return 3
  4340. ' finally',
  4341. ' end;',
  4342. 'end;',
  4343. 'function Fly: word;',
  4344. 'begin',
  4345. ' try',
  4346. ' exit(3);',
  4347. ' finally',
  4348. ' if Result>0 then ;',
  4349. ' end;',
  4350. 'end;',
  4351. 'function Jump: word;',
  4352. 'begin',
  4353. ' try',
  4354. ' try',
  4355. ' exit(4);',
  4356. ' finally',
  4357. ' end;',
  4358. ' finally',
  4359. ' if Result>0 then ;',
  4360. ' end;',
  4361. 'end;',
  4362. 'begin',
  4363. '']);
  4364. ConvertProgram;
  4365. CheckSource('TestExit_ResultInFinally',
  4366. LinesToStr([ // statements
  4367. 'this.Run = function () {',
  4368. ' var Result = 0;',
  4369. ' try {',
  4370. ' return 3;',
  4371. ' } finally {',
  4372. ' };',
  4373. ' return Result;',
  4374. '};',
  4375. 'this.Fly = function () {',
  4376. ' var Result = 0;',
  4377. ' try {',
  4378. ' Result = 3;',
  4379. ' return Result;',
  4380. ' } finally {',
  4381. ' if (Result > 0) ;',
  4382. ' };',
  4383. ' return Result;',
  4384. '};',
  4385. 'this.Jump = function () {',
  4386. ' var Result = 0;',
  4387. ' try {',
  4388. ' try {',
  4389. ' Result = 4;',
  4390. ' return Result;',
  4391. ' } finally {',
  4392. ' };',
  4393. ' } finally {',
  4394. ' if (Result > 0) ;',
  4395. ' };',
  4396. ' return Result;',
  4397. '};',
  4398. '']),
  4399. LinesToStr([
  4400. '']));
  4401. end;
  4402. procedure TTestModule.TestBreak;
  4403. begin
  4404. StartProgram(false);
  4405. Add([
  4406. 'var',
  4407. ' i: longint;',
  4408. 'begin',
  4409. ' repeat',
  4410. ' break;',
  4411. ' until true;',
  4412. ' while true do',
  4413. ' break;',
  4414. ' for i:=1 to 2 do',
  4415. ' break;']);
  4416. ConvertProgram;
  4417. CheckSource('TestBreak',
  4418. LinesToStr([ // statements
  4419. 'this.i = 0;'
  4420. ]),
  4421. LinesToStr([
  4422. 'do {',
  4423. ' break;',
  4424. '} while (!true);',
  4425. 'while (true) break;',
  4426. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4427. '']));
  4428. end;
  4429. procedure TTestModule.TestBreakAsVar;
  4430. begin
  4431. StartProgram(false);
  4432. Add([
  4433. 'procedure DoIt(break: boolean);',
  4434. 'begin',
  4435. ' if break then ;',
  4436. 'end;',
  4437. 'var',
  4438. ' break: boolean;',
  4439. 'begin',
  4440. ' if break then ;']);
  4441. ConvertProgram;
  4442. CheckSource('TestBreakAsVar',
  4443. LinesToStr([ // statements
  4444. 'this.DoIt = function (Break) {',
  4445. ' if (Break) ;',
  4446. '};',
  4447. 'this.Break = false;',
  4448. '']),
  4449. LinesToStr([
  4450. 'if($mod.Break) ;',
  4451. '']));
  4452. end;
  4453. procedure TTestModule.TestContinue;
  4454. begin
  4455. StartProgram(false);
  4456. Add('var i: longint;');
  4457. Add('begin');
  4458. Add(' repeat');
  4459. Add(' continue;');
  4460. Add(' until true;');
  4461. Add(' while true do');
  4462. Add(' continue;');
  4463. Add(' for i:=1 to 2 do');
  4464. Add(' continue;');
  4465. ConvertProgram;
  4466. CheckSource('TestContinue',
  4467. LinesToStr([ // statements
  4468. 'this.i = 0;'
  4469. ]),
  4470. LinesToStr([
  4471. 'do {',
  4472. ' continue;',
  4473. '} while (!true);',
  4474. 'while (true) continue;',
  4475. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4476. '']));
  4477. end;
  4478. procedure TTestModule.TestProc_External;
  4479. begin
  4480. StartProgram(false);
  4481. Add('procedure Foo; external name ''console.log'';');
  4482. Add('function Bar: longint; external name ''get.item'';');
  4483. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4484. Add('var');
  4485. Add(' i: longint;');
  4486. Add('begin');
  4487. Add(' Foo;');
  4488. Add(' i:=Bar;');
  4489. Add(' i:=Bla(''abc'');');
  4490. ConvertProgram;
  4491. CheckSource('TestProc_External',
  4492. LinesToStr([ // statements
  4493. 'this.i = 0;'
  4494. ]),
  4495. LinesToStr([
  4496. 'console.log();',
  4497. '$mod.i = get.item();',
  4498. '$mod.i = apply.something("abc");'
  4499. ]));
  4500. end;
  4501. procedure TTestModule.TestProc_ExternalOtherUnit;
  4502. begin
  4503. AddModuleWithIntfImplSrc('unit2.pas',
  4504. LinesToStr([
  4505. 'procedure Now; external name ''Date.now'';',
  4506. 'procedure DoIt;'
  4507. ]),
  4508. 'procedure doit; begin end;');
  4509. StartUnit(true);
  4510. Add('interface');
  4511. Add('uses unit2;');
  4512. Add('implementation');
  4513. Add('begin');
  4514. Add(' now;');
  4515. Add(' now();');
  4516. Add(' uNit2.now;');
  4517. Add(' uNit2.now();');
  4518. Add(' doit;');
  4519. Add(' uNit2.doit;');
  4520. ConvertUnit;
  4521. CheckSource('TestProc_ExternalOtherUnit',
  4522. LinesToStr([
  4523. '']),
  4524. LinesToStr([
  4525. 'Date.now();',
  4526. 'Date.now();',
  4527. 'Date.now();',
  4528. 'Date.now();',
  4529. 'pas.unit2.DoIt();',
  4530. 'pas.unit2.DoIt();',
  4531. '']));
  4532. end;
  4533. procedure TTestModule.TestProc_Asm;
  4534. begin
  4535. StartProgram(false);
  4536. Add([
  4537. '{$mode delphi}',
  4538. 'function DoIt: longint;',
  4539. 'begin;',
  4540. ' asm',
  4541. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4542. ' end;',
  4543. ' asm console.log(); end;',
  4544. ' asm',
  4545. ' s = "'' ";',
  4546. ' s = ''" '';',
  4547. ' s = s + "world" + "''";',
  4548. ' // end',
  4549. ' s = ''end'';',
  4550. ' s = "end";',
  4551. ' s = "foo\"bar";',
  4552. ' s = ''a\''b'';',
  4553. ' s = `${expr}\`-"-''-`;',
  4554. ' s = `multi',
  4555. 'line`;',
  4556. ' end;',
  4557. 'end;',
  4558. 'procedure Fly;',
  4559. 'asm',
  4560. ' return;',
  4561. 'end;',
  4562. 'begin']);
  4563. ConvertProgram;
  4564. CheckSource('TestProc_Asm',
  4565. LinesToStr([ // statements
  4566. 'this.DoIt = function () {',
  4567. ' var Result = 0;',
  4568. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4569. ' console.log();',
  4570. ' s = "'' ";',
  4571. ' s = ''" '';',
  4572. ' s = s + "world" + "''";',
  4573. ' // end',
  4574. ' s = ''end'';',
  4575. ' s = "end";',
  4576. ' s = "foo\"bar";',
  4577. ' s = ''a\''b'';',
  4578. ' s = `${expr}\`-"-''-`;',
  4579. ' s = `multi',
  4580. 'line`;',
  4581. ' return Result;',
  4582. '};',
  4583. 'this.Fly = function () {',
  4584. ' return;',
  4585. '};',
  4586. '']),
  4587. LinesToStr([
  4588. ''
  4589. ]));
  4590. end;
  4591. procedure TTestModule.TestProc_AsmSubBlock;
  4592. begin
  4593. StartProgram(true,[supTObject]);
  4594. Add([
  4595. '{$mode delphi}',
  4596. 'type',
  4597. ' TBird = class end;',
  4598. 'procedure Run(w: word);',
  4599. 'begin;',
  4600. ' if true then asm console.log(); end;',
  4601. ' if w>3 then asm',
  4602. ' var a = w+1;',
  4603. ' w = a+3;',
  4604. ' end;',
  4605. ' while (w>7) do asm',
  4606. ' w+=3; w*=2;',
  4607. ' end;',
  4608. ' try',
  4609. ' except',
  4610. ' on E: TBird do',
  4611. ' asm console.log(E); end;',
  4612. ' on E: TObject do',
  4613. ' asm var i=3; i--; end;',
  4614. ' else asm Fly; High; end;',
  4615. ' end;',
  4616. 'end;',
  4617. 'begin']);
  4618. ConvertProgram;
  4619. CheckSource('TestProc_AsmSubBlock',
  4620. LinesToStr([ // statements
  4621. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4622. '});',
  4623. 'this.Run = function (w) {',
  4624. ' if (true) console.log();',
  4625. ' if (w > 3) {',
  4626. ' var a = w+1;',
  4627. ' w = a+3;',
  4628. ' };',
  4629. ' while (w > 7) {',
  4630. ' w+=3; w*=2;',
  4631. ' };',
  4632. ' try {} catch ($e) {',
  4633. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4634. ' var E = $e;',
  4635. ' console.log(E);',
  4636. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4637. ' var E = $e;',
  4638. ' var i=3; i--;',
  4639. ' } else {',
  4640. ' Fly; High;',
  4641. ' }',
  4642. ' };',
  4643. '};',
  4644. '']),
  4645. LinesToStr([
  4646. ''
  4647. ]));
  4648. end;
  4649. procedure TTestModule.TestProc_Assembler;
  4650. begin
  4651. StartProgram(false);
  4652. Add('function DoIt: longint; assembler;');
  4653. Add('asm');
  4654. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4655. Add('end;');
  4656. Add('begin');
  4657. ConvertProgram;
  4658. CheckSource('TestProc_Assembler',
  4659. LinesToStr([ // statements
  4660. 'this.DoIt = function () {',
  4661. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4662. '};'
  4663. ]),
  4664. LinesToStr([
  4665. ''
  4666. ]));
  4667. end;
  4668. procedure TTestModule.TestProc_VarParam;
  4669. begin
  4670. StartProgram(false);
  4671. Add('type integer = longint;');
  4672. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4673. Add('var vJ: integer;');
  4674. Add('begin');
  4675. Add(' vg:=vg+1;');
  4676. Add(' vj:=vh+2;');
  4677. Add(' vi:=vi+3;');
  4678. Add(' doit(vg,vg,vg);');
  4679. Add(' doit(vh,vh,vj);');
  4680. Add(' doit(vi,vi,vi);');
  4681. Add(' doit(vj,vj,vj);');
  4682. Add('end;');
  4683. Add('var i: integer;');
  4684. Add('begin');
  4685. Add(' doit(i,i,i);');
  4686. ConvertProgram;
  4687. CheckSource('TestProc_VarParam',
  4688. LinesToStr([ // statements
  4689. 'this.DoIt = function (vG,vH,vI) {',
  4690. ' var vJ = 0;',
  4691. ' vG = vG + 1;',
  4692. ' vJ = vH + 2;',
  4693. ' vI.set(vI.get()+3);',
  4694. ' $mod.DoIt(vG, vG, {',
  4695. ' get: function () {',
  4696. ' return vG;',
  4697. ' },',
  4698. ' set: function (v) {',
  4699. ' vG = v;',
  4700. ' }',
  4701. ' });',
  4702. ' $mod.DoIt(vH, vH, {',
  4703. ' get: function () {',
  4704. ' return vJ;',
  4705. ' },',
  4706. ' set: function (v) {',
  4707. ' vJ = v;',
  4708. ' }',
  4709. ' });',
  4710. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4711. ' $mod.DoIt(vJ, vJ, {',
  4712. ' get: function () {',
  4713. ' return vJ;',
  4714. ' },',
  4715. ' set: function (v) {',
  4716. ' vJ = v;',
  4717. ' }',
  4718. ' });',
  4719. '};',
  4720. 'this.i = 0;'
  4721. ]),
  4722. LinesToStr([
  4723. '$mod.DoIt($mod.i,$mod.i,{',
  4724. ' p: $mod,',
  4725. ' get: function () {',
  4726. ' return this.p.i;',
  4727. ' },',
  4728. ' set: function (v) {',
  4729. ' this.p.i = v;',
  4730. ' }',
  4731. '});'
  4732. ]));
  4733. end;
  4734. procedure TTestModule.TestProc_VarParamString;
  4735. begin
  4736. StartProgram(false);
  4737. Add(['type TCaption = string;',
  4738. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4739. 'var c: char;',
  4740. 'begin',
  4741. ' va[1]:=c;',
  4742. ' vb[2]:=c;',
  4743. ' vc[3]:=c;',
  4744. 'end;',
  4745. 'begin']);
  4746. ConvertProgram;
  4747. CheckSource('TestProc_VarParamString',
  4748. LinesToStr([ // statements
  4749. 'this.DoIt = function (vA,vB,vC) {',
  4750. ' var c = "";',
  4751. ' vA = rtl.setCharAt(vA, 0, c);',
  4752. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4753. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4754. '};',
  4755. '']),
  4756. LinesToStr([
  4757. ]));
  4758. end;
  4759. procedure TTestModule.TestProc_VarParamV;
  4760. begin
  4761. StartProgram(false);
  4762. Add([
  4763. 'procedure Inc2(var i: longint);',
  4764. 'begin',
  4765. ' i:=i+2;',
  4766. 'end;',
  4767. 'procedure DoIt(v: longint);',
  4768. 'var p: array of longint;',
  4769. 'begin',
  4770. ' Inc2(v);',
  4771. ' Inc2(p[v]);',
  4772. 'end;',
  4773. 'begin']);
  4774. ConvertProgram;
  4775. CheckSource('TestProc_VarParamV',
  4776. LinesToStr([ // statements
  4777. 'this.Inc2 = function (i) {',
  4778. ' i.set(i.get()+2);',
  4779. '};',
  4780. 'this.DoIt = function (v) {',
  4781. ' var p = [];',
  4782. ' $mod.Inc2({get: function () {',
  4783. ' return v;',
  4784. ' }, set: function (w) {',
  4785. ' v = w;',
  4786. ' }});',
  4787. ' $mod.Inc2({',
  4788. ' a: v,',
  4789. ' p: p,',
  4790. ' get: function () {',
  4791. ' return this.p[this.a];',
  4792. ' },',
  4793. ' set: function (v) {',
  4794. ' this.p[this.a] = v;',
  4795. ' }',
  4796. ' });',
  4797. '};',
  4798. '']),
  4799. LinesToStr([
  4800. '']));
  4801. end;
  4802. procedure TTestModule.TestProc_Overload;
  4803. begin
  4804. StartProgram(false);
  4805. Add('procedure DoIt(vI: longint); begin end;');
  4806. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4807. Add('procedure DoIt(vD: double); begin end;');
  4808. Add('begin');
  4809. Add(' DoIt(1);');
  4810. Add(' DoIt(2,3);');
  4811. Add(' DoIt(4.5);');
  4812. ConvertProgram;
  4813. CheckSource('TestProcedureOverload',
  4814. LinesToStr([ // statements
  4815. 'this.DoIt = function (vI) {',
  4816. '};',
  4817. 'this.DoIt$1 = function (vI, vJ) {',
  4818. '};',
  4819. 'this.DoIt$2 = function (vD) {',
  4820. '};',
  4821. '']),
  4822. LinesToStr([
  4823. '$mod.DoIt(1);',
  4824. '$mod.DoIt$1(2, 3);',
  4825. '$mod.DoIt$2(4.5);',
  4826. '']));
  4827. end;
  4828. procedure TTestModule.TestProc_OverloadForward;
  4829. begin
  4830. StartProgram(false);
  4831. Add('procedure DoIt(vI: longint); forward;');
  4832. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4833. Add('procedure doit(vi: longint); begin end;');
  4834. Add('begin');
  4835. Add(' doit(1);');
  4836. Add(' doit(2,3);');
  4837. ConvertProgram;
  4838. CheckSource('TestProcedureOverloadForward',
  4839. LinesToStr([ // statements
  4840. 'this.DoIt$1 = function (vI, vJ) {',
  4841. '};',
  4842. 'this.DoIt = function (vI) {',
  4843. '};',
  4844. '']),
  4845. LinesToStr([
  4846. '$mod.DoIt(1);',
  4847. '$mod.DoIt$1(2, 3);',
  4848. '']));
  4849. end;
  4850. procedure TTestModule.TestProc_OverloadIntfImpl;
  4851. begin
  4852. StartUnit(false);
  4853. Add('interface');
  4854. Add('procedure DoIt(vI: longint);');
  4855. Add('procedure DoIt(vI, vJ: longint);');
  4856. Add('implementation');
  4857. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4858. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4859. Add('procedure DoIt(vi: longint); begin end;');
  4860. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4861. Add('procedure DoIt(vi, vj: longint); begin end;');
  4862. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4863. Add('begin');
  4864. Add(' doit(1);');
  4865. Add(' doit(2,3);');
  4866. Add(' doit(4,5,6);');
  4867. Add(' doit(7,8,9,10);');
  4868. Add(' doit(11,12,13,14,15);');
  4869. ConvertUnit;
  4870. CheckSource('TestProcedureOverloadUnit',
  4871. LinesToStr([ // statements
  4872. 'var $impl = $mod.$impl;',
  4873. 'this.DoIt = function (vI) {',
  4874. '};',
  4875. 'this.DoIt$1 = function (vI, vJ) {',
  4876. '};',
  4877. '']),
  4878. LinesToStr([ // this.$init
  4879. '$mod.DoIt(1);',
  4880. '$mod.DoIt$1(2, 3);',
  4881. '$impl.DoIt$3(4,5,6);',
  4882. '$impl.DoIt$4(7,8,9,10);',
  4883. '$impl.DoIt$2(11,12,13,14,15);',
  4884. '']),
  4885. LinesToStr([ // implementation
  4886. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4887. '};',
  4888. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4889. '};',
  4890. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4891. '};',
  4892. '']));
  4893. end;
  4894. procedure TTestModule.TestProc_OverloadNested;
  4895. begin
  4896. StartProgram(false);
  4897. Add([
  4898. 'procedure doit(vA: longint);',
  4899. ' procedure DoIt(vA, vB: longint); overload;',
  4900. ' begin',
  4901. ' doit(1);',
  4902. ' doit(1,2);',
  4903. ' end;',
  4904. ' procedure doit(vA, vB, vC: longint);',
  4905. ' begin',
  4906. ' doit(1);',
  4907. ' doit(1,2);',
  4908. ' doit(1,2,3);',
  4909. ' end;',
  4910. 'begin',
  4911. ' doit(1);',
  4912. ' doit(1,2);',
  4913. ' doit(1,2,3);',
  4914. 'end;',
  4915. 'begin // main',
  4916. ' doit(1);']);
  4917. ConvertProgram;
  4918. CheckSource('TestProcedureOverloadNested',
  4919. LinesToStr([ // statements
  4920. 'this.doit = function (vA) {',
  4921. ' function DoIt$1(vA, vB) {',
  4922. ' $mod.doit(1);',
  4923. ' DoIt$1(1, 2);',
  4924. ' };',
  4925. ' function doit$2(vA, vB, vC) {',
  4926. ' $mod.doit(1);',
  4927. ' DoIt$1(1, 2);',
  4928. ' doit$2(1, 2, 3);',
  4929. ' };',
  4930. ' $mod.doit(1);',
  4931. ' DoIt$1(1, 2);',
  4932. ' doit$2(1, 2, 3);',
  4933. '};',
  4934. '']),
  4935. LinesToStr([
  4936. '$mod.doit(1);',
  4937. '']));
  4938. end;
  4939. procedure TTestModule.TestProc_OverloadNestedForward;
  4940. begin
  4941. StartProgram(false);
  4942. Add([
  4943. 'procedure DoIt(vA: longint); overload; forward;',
  4944. 'procedure DoIt(vB, vC: longint); overload;',
  4945. 'begin // 2 param overload',
  4946. ' doit(1);',
  4947. ' doit(1,2);',
  4948. 'end;',
  4949. 'procedure doit(vA: longint);',
  4950. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4951. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4952. ' begin // 4 param overload',
  4953. ' doit(1);',
  4954. ' doit(1,2);',
  4955. ' doit(1,2,3);',
  4956. ' doit(1,2,3,4);',
  4957. ' end;',
  4958. ' procedure doit(vA, vB, vC: longint);',
  4959. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4960. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4961. ' begin // 6 param overload',
  4962. ' doit(1);',
  4963. ' doit(1,2);',
  4964. ' doit(1,2,3);',
  4965. ' doit(1,2,3,4);',
  4966. ' doit(1,2,3,4,5);',
  4967. ' doit(1,2,3,4,5,6);',
  4968. ' end;',
  4969. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4970. ' begin // 5 param overload',
  4971. ' doit(1);',
  4972. ' doit(1,2);',
  4973. ' doit(1,2,3);',
  4974. ' doit(1,2,3,4);',
  4975. ' doit(1,2,3,4,5);',
  4976. ' doit(1,2,3,4,5,6);',
  4977. ' end;',
  4978. ' begin // 3 param overload',
  4979. ' doit(1);',
  4980. ' doit(1,2);',
  4981. ' doit(1,2,3);',
  4982. ' doit(1,2,3,4);',
  4983. ' doit(1,2,3,4,5);',
  4984. ' doit(1,2,3,4,5,6);',
  4985. ' end;',
  4986. 'begin // 1 param overload',
  4987. ' doit(1);',
  4988. ' doit(1,2);',
  4989. ' doit(1,2,3);',
  4990. ' doit(1,2,3,4);',
  4991. 'end;',
  4992. 'begin // main',
  4993. ' doit(1);',
  4994. ' doit(1,2);']);
  4995. ConvertProgram;
  4996. CheckSource('TestProc_OverloadNestedForward',
  4997. LinesToStr([ // statements
  4998. 'this.DoIt$1 = function (vB, vC) {',
  4999. ' $mod.DoIt(1);',
  5000. ' $mod.DoIt$1(1, 2);',
  5001. '};',
  5002. 'this.DoIt = function (vA) {',
  5003. ' function DoIt$3(vA, vB, vC, vD) {',
  5004. ' $mod.DoIt(1);',
  5005. ' $mod.DoIt$1(1, 2);',
  5006. ' DoIt$2(1, 2, 3);',
  5007. ' DoIt$3(1, 2, 3, 4);',
  5008. ' };',
  5009. ' function DoIt$2(vA, vB, vC) {',
  5010. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5011. ' $mod.DoIt(1);',
  5012. ' $mod.DoIt$1(1, 2);',
  5013. ' DoIt$2(1, 2, 3);',
  5014. ' DoIt$3(1, 2, 3, 4);',
  5015. ' DoIt$4(1, 2, 3, 4, 5);',
  5016. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5017. ' };',
  5018. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5019. ' $mod.DoIt(1);',
  5020. ' $mod.DoIt$1(1, 2);',
  5021. ' DoIt$2(1, 2, 3);',
  5022. ' DoIt$3(1, 2, 3, 4);',
  5023. ' DoIt$4(1, 2, 3, 4, 5);',
  5024. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5025. ' };',
  5026. ' $mod.DoIt(1);',
  5027. ' $mod.DoIt$1(1, 2);',
  5028. ' DoIt$2(1, 2, 3);',
  5029. ' DoIt$3(1, 2, 3, 4);',
  5030. ' DoIt$4(1, 2, 3, 4, 5);',
  5031. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5032. ' };',
  5033. ' $mod.DoIt(1);',
  5034. ' $mod.DoIt$1(1, 2);',
  5035. ' DoIt$2(1, 2, 3);',
  5036. ' DoIt$3(1, 2, 3, 4);',
  5037. '};',
  5038. '']),
  5039. LinesToStr([
  5040. '$mod.DoIt(1);',
  5041. '$mod.DoIt$1(1, 2);',
  5042. '']));
  5043. end;
  5044. procedure TTestModule.TestProc_OverloadUnitCycle;
  5045. begin
  5046. AddModuleWithIntfImplSrc('Unit2.pas',
  5047. LinesToStr([
  5048. 'type',
  5049. ' TObject = class',
  5050. ' procedure DoIt(b: boolean); virtual; abstract;',
  5051. ' procedure DoIt(i: longint); virtual; abstract;',
  5052. ' end;',
  5053. '']),
  5054. 'uses test1;');
  5055. StartUnit(true);
  5056. Add([
  5057. 'interface',
  5058. 'uses unit2;',
  5059. 'type',
  5060. ' TEagle = class(TObject)',
  5061. ' procedure DoIt(b: boolean); override;',
  5062. ' procedure DoIt(i: longint); override;',
  5063. ' end;',
  5064. 'implementation',
  5065. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5066. 'procedure TEagle.DoIt(i: longint); begin end;',
  5067. '']);
  5068. ConvertUnit;
  5069. CheckSource('TestProc_OverloadUnitCycle',
  5070. LinesToStr([ // statements
  5071. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5072. ' this.DoIt = function (b) {',
  5073. ' };',
  5074. ' this.DoIt$1 = function (i) {',
  5075. ' };',
  5076. '});',
  5077. '']),
  5078. '',
  5079. LinesToStr([
  5080. '']));
  5081. end;
  5082. procedure TTestModule.TestProc_Varargs;
  5083. begin
  5084. StartProgram(false);
  5085. Add([
  5086. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5087. 'procedure ProcB; varargs; external name ''ProcB'';',
  5088. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5089. 'function GetIt: longint; begin end;',
  5090. 'begin',
  5091. ' ProcA(1);',
  5092. ' ProcA(1,2);',
  5093. ' ProcA(1,2.0);',
  5094. ' ProcA(1,2,3);',
  5095. ' ProcA(1,''2'');',
  5096. ' ProcA(2,'''');',
  5097. ' ProcA(3,false);',
  5098. ' ProcB;',
  5099. ' ProcB();',
  5100. ' ProcB(4);',
  5101. ' ProcB(''foo'');',
  5102. ' ProcC;',
  5103. ' ProcC();',
  5104. ' ProcC(4);',
  5105. ' ProcC(5,''foo'');',
  5106. ' ProcB(GetIt);',
  5107. ' ProcB(GetIt());',
  5108. ' ProcB(GetIt,GetIt());']);
  5109. ConvertProgram;
  5110. CheckSource('TestProc_Varargs',
  5111. LinesToStr([ // statements
  5112. 'this.GetIt = function () {',
  5113. ' var Result = 0;',
  5114. ' return Result;',
  5115. '};',
  5116. '']),
  5117. LinesToStr([
  5118. 'ProcA(1);',
  5119. 'ProcA(1, 2);',
  5120. 'ProcA(1, 2.0);',
  5121. 'ProcA(1, 2, 3);',
  5122. 'ProcA(1, "2");',
  5123. 'ProcA(2, "");',
  5124. 'ProcA(3, false);',
  5125. 'ProcB();',
  5126. 'ProcB();',
  5127. 'ProcB(4);',
  5128. 'ProcB("foo");',
  5129. 'ProcC(17);',
  5130. 'ProcC(17);',
  5131. 'ProcC(4);',
  5132. 'ProcC(5, "foo");',
  5133. 'ProcB($mod.GetIt());',
  5134. 'ProcB($mod.GetIt());',
  5135. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5136. '']));
  5137. end;
  5138. procedure TTestModule.TestProc_ConstOrder;
  5139. begin
  5140. StartProgram(false);
  5141. Add([
  5142. 'const A = 3;',
  5143. 'const B = A+1;',
  5144. 'procedure DoIt;',
  5145. 'const C = A+1;',
  5146. 'const D = B+1;',
  5147. 'const E = D+C+B+A;',
  5148. 'begin',
  5149. 'end;',
  5150. 'begin'
  5151. ]);
  5152. ConvertProgram;
  5153. CheckSource('TestProc_ConstOrder',
  5154. LinesToStr([ // statements
  5155. 'this.A = 3;',
  5156. 'this.B = 3 + 1;',
  5157. 'var C = 3 + 1;',
  5158. 'var D = 4 + 1;',
  5159. 'var E = 5 + 4 + 4 + 3;',
  5160. 'this.DoIt = function () {',
  5161. '};',
  5162. '']),
  5163. LinesToStr([
  5164. ''
  5165. ]));
  5166. end;
  5167. procedure TTestModule.TestProc_DuplicateConst;
  5168. begin
  5169. StartProgram(false);
  5170. Add([
  5171. 'const A = 1;',
  5172. 'procedure DoIt;',
  5173. 'const A = 2;',
  5174. ' procedure SubIt;',
  5175. ' const A = 21;',
  5176. ' begin',
  5177. ' end;',
  5178. 'begin',
  5179. 'end;',
  5180. 'procedure DoSome;',
  5181. 'const A = 3;',
  5182. 'begin',
  5183. 'end;',
  5184. 'begin'
  5185. ]);
  5186. ConvertProgram;
  5187. CheckSource('TestProc_DuplicateConst',
  5188. LinesToStr([ // statements
  5189. 'this.A = 1;',
  5190. 'var A$1 = 2;',
  5191. 'var A$2 = 21;',
  5192. 'this.DoIt = function () {',
  5193. ' function SubIt() {',
  5194. ' };',
  5195. '};',
  5196. 'var A$3 = 3;',
  5197. 'this.DoSome = function () {',
  5198. '};',
  5199. '']),
  5200. LinesToStr([
  5201. ''
  5202. ]));
  5203. end;
  5204. procedure TTestModule.TestProc_LocalVarAbsolute;
  5205. begin
  5206. StartProgram(false);
  5207. Add([
  5208. 'type',
  5209. ' TObject = class',
  5210. ' Index: longint;',
  5211. ' procedure DoAbs(Item: pointer);',
  5212. ' end;',
  5213. 'procedure TObject.DoAbs(Item: pointer);',
  5214. 'var',
  5215. ' o: TObject absolute Item;',
  5216. 'begin',
  5217. ' if o.Index<o.Index then o.Index:=o.Index;',
  5218. 'end;',
  5219. 'procedure DoIt(i: longint; p: pointer);',
  5220. 'var',
  5221. ' d: double absolute i;',
  5222. ' s: string absolute d;',
  5223. ' oi: TObject absolute i;',
  5224. ' op: TObject absolute p;',
  5225. 'begin',
  5226. ' if d=d then d:=d;',
  5227. ' if s=s then s:=s;',
  5228. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5229. ' if op.Index=op.Index then op.Index:=op.Index;',
  5230. 'end;',
  5231. 'begin']);
  5232. ConvertProgram;
  5233. CheckSource('TestProc_LocalVarAbsolute',
  5234. LinesToStr([ // statements
  5235. 'rtl.createClass(this, "TObject", null, function () {',
  5236. ' this.$init = function () {',
  5237. ' this.Index = 0;',
  5238. ' };',
  5239. ' this.$final = function () {',
  5240. ' };',
  5241. ' this.DoAbs = function (Item) {',
  5242. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5243. ' };',
  5244. '});',
  5245. 'this.DoIt = function (i, p) {',
  5246. ' if (i === i) i = i;',
  5247. ' if (i === i) i = i;',
  5248. ' if (i.Index < i.Index) i.Index = i.Index;',
  5249. ' if (p.Index === p.Index) p.Index = p.Index;',
  5250. '};'
  5251. ]),
  5252. LinesToStr([
  5253. ]));
  5254. end;
  5255. procedure TTestModule.TestProc_ResultAbsolute;
  5256. begin
  5257. StartProgram(false);
  5258. Add([
  5259. 'type',
  5260. ' TObject = class',
  5261. ' Index: longint;',
  5262. ' function DoAbs: pointer;',
  5263. ' end;',
  5264. 'function TObject.DoAbs: pointer;',
  5265. 'var',
  5266. ' o: TObject absolute Result;',
  5267. 'begin',
  5268. ' if o.Index<o.Index then o.Index:=o.Index;',
  5269. 'end;',
  5270. 'function DoIt: jsvalue;',
  5271. 'var',
  5272. ' d: double absolute Result;',
  5273. ' s: string absolute Result;',
  5274. ' o: TObject absolute Result;',
  5275. 'begin',
  5276. ' if d=d then d:=d;',
  5277. ' if s=s then s:=s;',
  5278. ' if o.Index<o.Index then o.Index:=o.Index;',
  5279. 'end;',
  5280. 'begin']);
  5281. ConvertProgram;
  5282. CheckSource('TestProc_ResultAbsolute',
  5283. LinesToStr([ // statements
  5284. 'rtl.createClass(this, "TObject", null, function () {',
  5285. ' this.$init = function () {',
  5286. ' this.Index = 0;',
  5287. ' };',
  5288. ' this.$final = function () {',
  5289. ' };',
  5290. ' this.DoAbs = function () {',
  5291. ' var Result = null;',
  5292. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5293. ' return Result;',
  5294. ' };',
  5295. '});',
  5296. 'this.DoIt = function () {',
  5297. ' var Result = undefined;',
  5298. ' if (Result === Result) Result = Result;',
  5299. ' if (Result === Result) Result = Result;',
  5300. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5301. ' return Result;',
  5302. '};',
  5303. '']),
  5304. LinesToStr([
  5305. ]));
  5306. end;
  5307. procedure TTestModule.TestProc_LocalVarInit;
  5308. begin
  5309. StartProgram(false);
  5310. Add([
  5311. 'type TBytes = array of byte;',
  5312. 'procedure DoIt;',
  5313. 'const c = 4;',
  5314. 'var',
  5315. ' b: byte = 1;',
  5316. ' w: word = 2+c;',
  5317. ' p: pointer = nil;',
  5318. ' Buffer: TBytes = nil;',
  5319. 'begin',
  5320. 'end;',
  5321. 'begin']);
  5322. ConvertProgram;
  5323. CheckSource('TestProc_LocalVarInit',
  5324. LinesToStr([ // statements
  5325. 'var c = 4;',
  5326. 'this.DoIt = function () {',
  5327. ' var b = 1;',
  5328. ' var w = 2 + 4;',
  5329. ' var p = null;',
  5330. ' var Buffer = [];',
  5331. '};',
  5332. '']),
  5333. LinesToStr([
  5334. ]));
  5335. end;
  5336. procedure TTestModule.TestProc_ReservedWords;
  5337. begin
  5338. StartProgram(false);
  5339. Add([
  5340. 'procedure Date(ArrayBuffer: longint);',
  5341. 'const',
  5342. ' NaN: longint = 3;',
  5343. 'var',
  5344. ' &Boolean: longint;',
  5345. ' procedure Error(ArrayBuffer: longint);',
  5346. ' begin',
  5347. ' end;',
  5348. 'begin',
  5349. ' Nan:=&bOolean;',
  5350. 'end;',
  5351. 'begin',
  5352. ' Date(1);']);
  5353. ConvertProgram;
  5354. CheckSource('TestProc_ReservedWords',
  5355. LinesToStr([ // statements
  5356. 'var naN = 3;',
  5357. 'this.Date = function (arrayBuffer) {',
  5358. ' var boolean = 0;',
  5359. ' function error(arrayBuffer) {',
  5360. ' };',
  5361. ' naN = boolean;',
  5362. '};',
  5363. '']),
  5364. LinesToStr([
  5365. ' $mod.Date(1);'
  5366. ]));
  5367. end;
  5368. procedure TTestModule.TestProc_ConstRefWord;
  5369. begin
  5370. StartProgram(false);
  5371. Add([
  5372. 'procedure Run(constref w: word);',
  5373. 'var l: word;',
  5374. 'begin',
  5375. ' l:=w;',
  5376. ' Run(w);',
  5377. ' Run(l);',
  5378. 'end;',
  5379. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5380. 'begin',
  5381. ' Run(a);',
  5382. ' Run(b);',
  5383. ' Run(c);',
  5384. ' Run(d);',
  5385. ' Run(e);',
  5386. 'end;',
  5387. 'begin',
  5388. ' Run(1);']);
  5389. ConvertProgram;
  5390. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5391. CheckSource('TestProc_ConstRefWord',
  5392. LinesToStr([ // statements
  5393. 'this.Run = function (w) {',
  5394. ' var l = 0;',
  5395. ' l = w;',
  5396. ' $mod.Run(w);',
  5397. ' $mod.Run(l);',
  5398. '};',
  5399. 'this.Fly = function (a, b, c, d, e) {',
  5400. ' $mod.Run(a);',
  5401. ' $mod.Run(b.get());',
  5402. ' $mod.Run(c.get());',
  5403. ' $mod.Run(d);',
  5404. ' $mod.Run(e);',
  5405. '};',
  5406. '']),
  5407. LinesToStr([
  5408. '$mod.Run(1);'
  5409. ]));
  5410. end;
  5411. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5412. begin
  5413. StartProgram(false);
  5414. Add([
  5415. '{$mode objfpc}',
  5416. 'type',
  5417. ' TFunc = reference to function(x: word): word;',
  5418. 'var Func: TFunc;',
  5419. 'procedure DoIt(a: word);',
  5420. 'begin',
  5421. ' Func:=function(b:word): word',
  5422. ' begin',
  5423. ' Result:=a+b;',
  5424. ' exit(b);',
  5425. ' exit(Result);',
  5426. ' end;',// test semicolon
  5427. ' a:=3;',
  5428. 'end;',
  5429. 'begin',
  5430. ' Func:=function(c:word):word begin',
  5431. ' Result:=3+c;',
  5432. ' exit(c);',
  5433. ' exit(Result);',
  5434. ' end;']);
  5435. ConvertProgram;
  5436. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5437. LinesToStr([ // statements
  5438. 'this.Func = null;',
  5439. 'this.DoIt = function (a) {',
  5440. ' $mod.Func = function (b) {',
  5441. ' var Result = 0;',
  5442. ' Result = a + b;',
  5443. ' return b;',
  5444. ' return Result;',
  5445. ' return Result;',
  5446. ' };',
  5447. ' a = 3;',
  5448. '};',
  5449. '']),
  5450. LinesToStr([
  5451. '$mod.Func = function (c) {',
  5452. ' var Result = 0;',
  5453. ' Result = 3 + c;',
  5454. ' return c;',
  5455. ' return Result;',
  5456. ' return Result;',
  5457. '};',
  5458. '']));
  5459. end;
  5460. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5461. begin
  5462. StartProgram(false);
  5463. Add([
  5464. '{$mode delphi}',
  5465. 'type',
  5466. ' TProc = reference to procedure(x: word);',
  5467. 'procedure DoIt(a: word);',
  5468. 'var Proc: TProc;',
  5469. 'begin',
  5470. ' Proc:=procedure(b:word) begin end;',
  5471. 'end;',
  5472. 'var Proc: TProc;',
  5473. 'begin',
  5474. ' Proc:=procedure(c:word) begin end;',
  5475. '']);
  5476. ConvertProgram;
  5477. CheckSource('TestAnonymousProc_Assign_Delphi',
  5478. LinesToStr([ // statements
  5479. 'this.DoIt = function (a) {',
  5480. ' var Proc = null;',
  5481. ' Proc = function (b) {',
  5482. ' };',
  5483. '};',
  5484. 'this.Proc = null;',
  5485. '']),
  5486. LinesToStr([
  5487. '$mod.Proc = function (c) {',
  5488. '};',
  5489. '']));
  5490. end;
  5491. procedure TTestModule.TestAnonymousProc_Arg;
  5492. begin
  5493. StartProgram(false);
  5494. Add([
  5495. 'type',
  5496. ' TProc = reference to procedure;',
  5497. ' TFunc = reference to function(x: word): word;',
  5498. 'procedure DoMore(f,g: TProc);',
  5499. 'begin',
  5500. 'end;',
  5501. 'procedure DoOdd(v: jsvalue);',
  5502. 'begin',
  5503. 'end;',
  5504. 'procedure DoIt(f: TFunc);',
  5505. 'begin',
  5506. ' DoIt(function(b:word): word',
  5507. ' begin',
  5508. ' Result:=1+b;',
  5509. ' end);',
  5510. ' DoMore(procedure begin end, procedure begin end);',
  5511. ' DoOdd(procedure begin end);',
  5512. 'end;',
  5513. 'begin',
  5514. ' DoMore(procedure begin end,',
  5515. ' procedure assembler asm',
  5516. ' console.log("c");',
  5517. ' end);',
  5518. '']);
  5519. ConvertProgram;
  5520. CheckSource('TestAnonymousProc_Arg',
  5521. LinesToStr([ // statements
  5522. 'this.DoMore = function (f, g) {',
  5523. '};',
  5524. 'this.DoOdd = function (v) {',
  5525. '};',
  5526. 'this.DoIt = function (f) {',
  5527. ' $mod.DoIt(function (b) {',
  5528. ' var Result = 0;',
  5529. ' Result = 1 + b;',
  5530. ' return Result;',
  5531. ' });',
  5532. ' $mod.DoMore(function () {',
  5533. ' }, function () {',
  5534. ' });',
  5535. ' $mod.DoOdd(function () {',
  5536. ' });',
  5537. '};',
  5538. '']),
  5539. LinesToStr([
  5540. '$mod.DoMore(function () {',
  5541. '}, function () {',
  5542. ' console.log("c");',
  5543. '});',
  5544. '']));
  5545. end;
  5546. procedure TTestModule.TestAnonymousProc_Typecast;
  5547. begin
  5548. StartProgram(false);
  5549. Add([
  5550. 'type',
  5551. ' TProc = reference to procedure(w: word);',
  5552. ' TArr = array of word;',
  5553. ' TFuncArr = reference to function: TArr;',
  5554. 'procedure DoIt(p: TProc);',
  5555. 'var',
  5556. ' w: word;',
  5557. ' a: TArr;',
  5558. 'begin',
  5559. ' p:=TProc(procedure(b: smallint) begin end);',
  5560. ' a:=TFuncArr(function: TArr begin end)();',
  5561. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5562. 'end;',
  5563. 'begin']);
  5564. ConvertProgram;
  5565. CheckSource('TestAnonymousProc_Typecast',
  5566. LinesToStr([ // statements
  5567. 'this.DoIt = function (p) {',
  5568. ' var w = 0;',
  5569. ' var a = [];',
  5570. ' p = function (b) {',
  5571. ' };',
  5572. ' a = function () {',
  5573. ' var Result = [];',
  5574. ' return Result;',
  5575. ' }();',
  5576. ' w = function () {',
  5577. ' var Result = [];',
  5578. ' return Result;',
  5579. ' }()[3];',
  5580. '};',
  5581. '']),
  5582. LinesToStr([
  5583. '']));
  5584. end;
  5585. procedure TTestModule.TestAnonymousProc_With;
  5586. begin
  5587. StartProgram(false);
  5588. Add([
  5589. 'type',
  5590. ' TProc = reference to procedure(w: word);',
  5591. ' TObject = class',
  5592. ' b: boolean;',
  5593. ' end;',
  5594. 'var',
  5595. ' p: TProc;',
  5596. ' bird: TObject;',
  5597. 'begin',
  5598. ' with bird do',
  5599. ' p:=procedure(w: word)',
  5600. ' begin',
  5601. ' b:=w>2;',
  5602. ' end;',
  5603. '']);
  5604. ConvertProgram;
  5605. CheckSource('TestAnonymousProc_With',
  5606. LinesToStr([ // statements
  5607. 'rtl.createClass(this, "TObject", null, function () {',
  5608. ' this.$init = function () {',
  5609. ' this.b = false;',
  5610. ' };',
  5611. ' this.$final = function () {',
  5612. ' };',
  5613. '});',
  5614. 'this.p = null;',
  5615. 'this.bird = null;',
  5616. '']),
  5617. LinesToStr([
  5618. 'var $with = $mod.bird;',
  5619. '$mod.p = function (w) {',
  5620. ' $with.b = w > 2;',
  5621. '};',
  5622. '']));
  5623. end;
  5624. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5625. begin
  5626. StartProgram(false);
  5627. Add([
  5628. 'type',
  5629. ' TProc = reference to procedure;',
  5630. ' TObject = class',
  5631. ' b: boolean;',
  5632. ' end;',
  5633. 'procedure DoIt;',
  5634. 'var',
  5635. ' p: TProc;',
  5636. 'begin',
  5637. ' try',
  5638. ' except',
  5639. ' on E: TObject do',
  5640. ' p:=procedure',
  5641. ' begin',
  5642. ' E.b:=true;',
  5643. ' end;',
  5644. ' end;',
  5645. 'end;',
  5646. 'begin']);
  5647. ConvertProgram;
  5648. CheckSource('TestAnonymousProc_ExceptOn',
  5649. LinesToStr([ // statements
  5650. 'rtl.createClass(this, "TObject", null, function () {',
  5651. ' this.$init = function () {',
  5652. ' this.b = false;',
  5653. ' };',
  5654. ' this.$final = function () {',
  5655. ' };',
  5656. '});',
  5657. 'this.DoIt = function () {',
  5658. ' var p = null;',
  5659. ' try {} catch ($e) {',
  5660. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5661. ' var E = $e;',
  5662. ' p = function () {',
  5663. ' E.b = true;',
  5664. ' };',
  5665. ' } else throw $e',
  5666. ' };',
  5667. '};',
  5668. '']),
  5669. LinesToStr([
  5670. '']));
  5671. end;
  5672. procedure TTestModule.TestAnonymousProc_Nested;
  5673. begin
  5674. StartProgram(false);
  5675. Add([
  5676. 'type',
  5677. ' TProc = reference to procedure;',
  5678. ' TObject = class',
  5679. ' i: byte;',
  5680. ' procedure DoIt;',
  5681. ' end;',
  5682. 'procedure TObject.DoIt;',
  5683. 'var',
  5684. ' p: TProc;',
  5685. ' procedure Sub;',
  5686. ' begin',
  5687. ' p:=procedure',
  5688. ' begin',
  5689. ' i:=3;',
  5690. ' Self.i:=4;',
  5691. ' p:=procedure',
  5692. ' procedure SubSub;',
  5693. ' begin',
  5694. ' i:=13;',
  5695. ' Self.i:=14;',
  5696. ' end;',
  5697. ' begin',
  5698. ' i:=13;',
  5699. ' Self.i:=14;',
  5700. ' end;',
  5701. ' end;',
  5702. ' end;',
  5703. 'begin',
  5704. 'end;',
  5705. 'begin']);
  5706. ConvertProgram;
  5707. CheckSource('TestAnonymousProc_Nested',
  5708. LinesToStr([ // statements
  5709. 'rtl.createClass(this, "TObject", null, function () {',
  5710. ' this.$init = function () {',
  5711. ' this.i = 0;',
  5712. ' };',
  5713. ' this.$final = function () {',
  5714. ' };',
  5715. ' this.DoIt = function () {',
  5716. ' var $Self = this;',
  5717. ' var p = null;',
  5718. ' function Sub() {',
  5719. ' p = function () {',
  5720. ' $Self.i = 3;',
  5721. ' $Self.i = 4;',
  5722. ' p = function () {',
  5723. ' function SubSub() {',
  5724. ' $Self.i = 13;',
  5725. ' $Self.i = 14;',
  5726. ' };',
  5727. ' $Self.i = 13;',
  5728. ' $Self.i = 14;',
  5729. ' };',
  5730. ' };',
  5731. ' };',
  5732. ' };',
  5733. '});',
  5734. '']),
  5735. LinesToStr([
  5736. '']));
  5737. end;
  5738. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5739. begin
  5740. StartProgram(false);
  5741. Add([
  5742. 'type',
  5743. ' TProc = reference to procedure;',
  5744. 'function DoIt: TProc;',
  5745. ' function Sub: TProc;',
  5746. ' begin',
  5747. ' Result:=procedure',
  5748. ' begin',
  5749. ' Sub:=procedure',
  5750. ' procedure SubSub;',
  5751. ' begin',
  5752. ' Result:=nil;',
  5753. ' Sub:=nil;',
  5754. ' DoIt:=nil;',
  5755. ' end;',
  5756. ' begin',
  5757. ' Result:=nil;',
  5758. ' Sub:=nil;',
  5759. ' DoIt:=nil;',
  5760. ' end;',
  5761. ' end;',
  5762. ' end;',
  5763. 'begin',
  5764. 'end;',
  5765. 'begin']);
  5766. ConvertProgram;
  5767. CheckSource('TestAnonymousProc_NestedAssignResult',
  5768. LinesToStr([ // statements
  5769. 'this.DoIt = function () {',
  5770. ' var Result = null;',
  5771. ' function Sub() {',
  5772. ' var Result$1 = null;',
  5773. ' Result$1 = function () {',
  5774. ' Result$1 = function () {',
  5775. ' function SubSub() {',
  5776. ' Result$1 = null;',
  5777. ' Result$1 = null;',
  5778. ' Result = null;',
  5779. ' };',
  5780. ' Result$1 = null;',
  5781. ' Result$1 = null;',
  5782. ' Result = null;',
  5783. ' };',
  5784. ' };',
  5785. ' return Result$1;',
  5786. ' };',
  5787. ' return Result;',
  5788. '};',
  5789. '']),
  5790. LinesToStr([
  5791. '']));
  5792. end;
  5793. procedure TTestModule.TestAnonymousProc_Class;
  5794. begin
  5795. StartProgram(false);
  5796. Add([
  5797. 'type',
  5798. ' TProc = reference to procedure;',
  5799. ' TEvent = procedure of object;',
  5800. ' TObject = class',
  5801. ' Size: word;',
  5802. ' function GetIt: TProc;',
  5803. ' procedure DoIt; virtual; abstract;',
  5804. ' end;',
  5805. 'function TObject.GetIt: TProc;',
  5806. 'begin',
  5807. ' Result:=procedure',
  5808. ' var p: TEvent;',
  5809. ' begin',
  5810. ' Size:=Size;',
  5811. ' Size:=Self.Size;',
  5812. ' p:=@DoIt;',
  5813. ' p:[email protected];',
  5814. ' end;',
  5815. 'end;',
  5816. 'begin']);
  5817. ConvertProgram;
  5818. CheckSource('TestAnonymousProc_Class',
  5819. LinesToStr([ // statements
  5820. 'rtl.createClass(this, "TObject", null, function () {',
  5821. ' this.$init = function () {',
  5822. ' this.Size = 0;',
  5823. ' };',
  5824. ' this.$final = function () {',
  5825. ' };',
  5826. ' this.GetIt = function () {',
  5827. ' var $Self = this;',
  5828. ' var Result = null;',
  5829. ' Result = function () {',
  5830. ' var p = null;',
  5831. ' $Self.Size = $Self.Size;',
  5832. ' $Self.Size = $Self.Size;',
  5833. ' p = rtl.createCallback($Self, "DoIt");',
  5834. ' p = rtl.createCallback($Self, "DoIt");',
  5835. ' };',
  5836. ' return Result;',
  5837. ' };',
  5838. '});',
  5839. '']),
  5840. LinesToStr([
  5841. '']));
  5842. end;
  5843. procedure TTestModule.TestAnonymousProc_ForLoop;
  5844. begin
  5845. StartProgram(false);
  5846. Add([
  5847. 'type TProc = reference to procedure;',
  5848. 'procedure Foo(p: TProc);',
  5849. 'begin',
  5850. 'end;',
  5851. 'procedure DoIt;',
  5852. 'var i: word;',
  5853. ' a: word;',
  5854. 'begin',
  5855. ' for i:=1 to 10 do begin',
  5856. ' Foo(procedure begin a:=3; end);',
  5857. ' end;',
  5858. 'end;',
  5859. 'begin',
  5860. ' DoIt;']);
  5861. ConvertProgram;
  5862. CheckSource('TestAnonymousProc_ForLoop',
  5863. LinesToStr([ // statements
  5864. 'this.Foo = function (p) {',
  5865. '};',
  5866. 'this.DoIt = function () {',
  5867. ' var i = 0;',
  5868. ' var a = 0;',
  5869. ' for (i = 1; i <= 10; i++) {',
  5870. ' $mod.Foo(function () {',
  5871. ' a = 3;',
  5872. ' });',
  5873. ' };',
  5874. '};',
  5875. '']),
  5876. LinesToStr([
  5877. '$mod.DoIt();'
  5878. ]));
  5879. end;
  5880. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5881. begin
  5882. StartProgram(false);
  5883. Add([
  5884. '{$mode delphi}',
  5885. 'type',
  5886. ' TProc = reference to procedure;',
  5887. ' TFunc = reference to function(x: word): word;',
  5888. 'procedure Run;',
  5889. 'asm',
  5890. 'end;',
  5891. 'procedure Walk(p: TProc; f: TFunc);',
  5892. 'begin',
  5893. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5894. 'end;',
  5895. 'begin',
  5896. ' Walk(procedure',
  5897. ' asm',
  5898. ' console.log("a");',
  5899. ' end,',
  5900. ' function(x: word): word asm',
  5901. ' console.log("c");',
  5902. ' end);',
  5903. '']);
  5904. ConvertProgram;
  5905. CheckSource('TestAnonymousProc_AsmDelphi',
  5906. LinesToStr([ // statements
  5907. 'this.Run = function () {',
  5908. '};',
  5909. 'this.Walk = function (p, f) {',
  5910. ' $mod.Walk(function () {',
  5911. ' }, function (b) {',
  5912. ' return 1+b;',
  5913. ' });',
  5914. '};',
  5915. '']),
  5916. LinesToStr([
  5917. '$mod.Walk(function () {',
  5918. ' console.log("a");',
  5919. '}, function (x) {',
  5920. ' console.log("c");',
  5921. '});',
  5922. '']));
  5923. end;
  5924. procedure TTestModule.TestEnum_Name;
  5925. begin
  5926. StartProgram(false);
  5927. Add('type TMyEnum = (Red, Green, Blue);');
  5928. Add('var e: TMyEnum;');
  5929. Add('var f: TMyEnum = Blue;');
  5930. Add('begin');
  5931. Add(' e:=green;');
  5932. Add(' e:=default(TMyEnum);');
  5933. ConvertProgram;
  5934. CheckSource('TestEnum_Name',
  5935. LinesToStr([ // statements
  5936. 'this.TMyEnum = {',
  5937. ' "0":"Red",',
  5938. ' Red:0,',
  5939. ' "1":"Green",',
  5940. ' Green:1,',
  5941. ' "2":"Blue",',
  5942. ' Blue:2',
  5943. ' };',
  5944. 'this.e = 0;',
  5945. 'this.f = this.TMyEnum.Blue;'
  5946. ]),
  5947. LinesToStr([
  5948. '$mod.e=$mod.TMyEnum.Green;',
  5949. '$mod.e=$mod.TMyEnum.Red;'
  5950. ]));
  5951. end;
  5952. procedure TTestModule.TestEnum_Number;
  5953. begin
  5954. Converter.Options:=Converter.Options+[coEnumNumbers];
  5955. StartProgram(false);
  5956. Add('type TMyEnum = (Red, Green);');
  5957. Add('var');
  5958. Add(' e: TMyEnum;');
  5959. Add(' f: TMyEnum = Green;');
  5960. Add(' i: longint;');
  5961. Add('begin');
  5962. Add(' e:=green;');
  5963. Add(' i:=longint(e);');
  5964. ConvertProgram;
  5965. CheckSource('TestEnumNumber',
  5966. LinesToStr([ // statements
  5967. 'this.TMyEnum = {',
  5968. ' "0":"Red",',
  5969. ' Red:0,',
  5970. ' "1":"Green",',
  5971. ' Green:1',
  5972. ' };',
  5973. 'this.e = 0;',
  5974. 'this.f = 1;',
  5975. 'this.i = 0;'
  5976. ]),
  5977. LinesToStr([
  5978. '$mod.e=1;',
  5979. '$mod.i=$mod.e;'
  5980. ]));
  5981. end;
  5982. procedure TTestModule.TestEnum_ConstFail;
  5983. begin
  5984. StartProgram(false);
  5985. Add([
  5986. 'type TMyEnum = (Red = 100, Green = 101);',
  5987. 'var',
  5988. ' e: TMyEnum;',
  5989. ' f: TMyEnum = Green;',
  5990. 'begin',
  5991. ' e:=green;']);
  5992. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5993. ConvertProgram;
  5994. end;
  5995. procedure TTestModule.TestEnum_Functions;
  5996. begin
  5997. StartProgram(false);
  5998. Add([
  5999. 'type TMyEnum = (Red, Green);',
  6000. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6001. 'var',
  6002. ' v: longint;',
  6003. ' s: string;',
  6004. 'begin',
  6005. ' val(s,e,v);',
  6006. ' val(s,e,i);',
  6007. 'end;',
  6008. 'var',
  6009. ' e: TMyEnum;',
  6010. ' i: longint;',
  6011. ' s: string;',
  6012. ' b: boolean;',
  6013. 'begin',
  6014. ' i:=ord(red);',
  6015. ' i:=ord(green);',
  6016. ' i:=ord(e);',
  6017. ' i:=ord(b);',
  6018. ' e:=low(tmyenum);',
  6019. ' e:=low(e);',
  6020. ' b:=low(boolean);',
  6021. ' e:=high(tmyenum);',
  6022. ' e:=high(e);',
  6023. ' b:=high(boolean);',
  6024. ' e:=pred(green);',
  6025. ' e:=pred(e);',
  6026. ' b:=pred(b);',
  6027. ' e:=succ(red);',
  6028. ' e:=succ(e);',
  6029. ' b:=succ(b);',
  6030. ' e:=tmyenum(1);',
  6031. ' e:=tmyenum(i);',
  6032. ' s:=str(e);',
  6033. ' str(e,s);',
  6034. ' str(red,s);',
  6035. ' s:=str(e:3);',
  6036. ' writestr(s,e:3,red);',
  6037. ' val(s,e,i);',
  6038. ' i:=longint(e);']);
  6039. ConvertProgram;
  6040. CheckSource('TestEnum_Functions',
  6041. LinesToStr([ // statements
  6042. 'this.TMyEnum = {',
  6043. ' "0":"Red",',
  6044. ' Red:0,',
  6045. ' "1":"Green",',
  6046. ' Green:1',
  6047. ' };',
  6048. 'this.DoIt = function (e, i) {',
  6049. ' var v = 0;',
  6050. ' var s = "";',
  6051. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6052. ' v = w;',
  6053. ' }));',
  6054. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6055. '};',
  6056. 'this.e = 0;',
  6057. 'this.i = 0;',
  6058. 'this.s = "";',
  6059. 'this.b = false;',
  6060. '']),
  6061. LinesToStr([
  6062. '$mod.i=$mod.TMyEnum.Red;',
  6063. '$mod.i=$mod.TMyEnum.Green;',
  6064. '$mod.i=$mod.e;',
  6065. '$mod.i=$mod.b+0;',
  6066. '$mod.e=$mod.TMyEnum.Red;',
  6067. '$mod.e=$mod.TMyEnum.Red;',
  6068. '$mod.b=false;',
  6069. '$mod.e=$mod.TMyEnum.Green;',
  6070. '$mod.e=$mod.TMyEnum.Green;',
  6071. '$mod.b=true;',
  6072. '$mod.e=$mod.TMyEnum.Green-1;',
  6073. '$mod.e=$mod.e-1;',
  6074. '$mod.b=false;',
  6075. '$mod.e=$mod.TMyEnum.Red+1;',
  6076. '$mod.e=$mod.e+1;',
  6077. '$mod.b=true;',
  6078. '$mod.e=1;',
  6079. '$mod.e=$mod.i;',
  6080. '$mod.s = $mod.TMyEnum[$mod.e];',
  6081. '$mod.s = $mod.TMyEnum[$mod.e];',
  6082. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6083. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6084. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6085. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6086. ' $mod.i = v;',
  6087. '});',
  6088. '$mod.i=$mod.e;',
  6089. '']));
  6090. end;
  6091. procedure TTestModule.TestEnumRg_Functions;
  6092. begin
  6093. StartProgram(false);
  6094. Add([
  6095. 'type',
  6096. ' TEnum = (Red, Green, Blue);',
  6097. ' TEnumRg = Green..Blue;',
  6098. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6099. 'var',
  6100. ' v: longint;',
  6101. ' s: string;',
  6102. 'begin',
  6103. ' val(s,e,v);',
  6104. ' val(s,e,i);',
  6105. 'end;',
  6106. 'var',
  6107. ' e: TEnumRg;',
  6108. ' i: longint;',
  6109. ' s: string;',
  6110. 'begin',
  6111. ' i:=ord(green);',
  6112. ' i:=ord(e);',
  6113. ' e:=low(tenumrg);',
  6114. ' e:=low(e);',
  6115. ' e:=high(tenumrg);',
  6116. ' e:=high(e);',
  6117. ' e:=pred(blue);',
  6118. ' e:=pred(e);',
  6119. ' e:=succ(green);',
  6120. ' e:=succ(e);',
  6121. ' e:=tenumrg(1);',
  6122. ' e:=tenumrg(i);',
  6123. ' s:=str(e);',
  6124. ' str(e,s);',
  6125. ' str(red,s);',
  6126. ' s:=str(e:3);',
  6127. ' writestr(s,e:3,blue);',
  6128. ' val(s,e,i);',
  6129. ' i:=longint(e);']);
  6130. ConvertProgram;
  6131. CheckSource('TestEnumRg_Functions',
  6132. LinesToStr([ // statements
  6133. 'this.TEnum = {',
  6134. ' "0":"Red",',
  6135. ' Red:0,',
  6136. ' "1":"Green",',
  6137. ' Green:1,',
  6138. ' "2":"Blue",',
  6139. ' Blue:2',
  6140. ' };',
  6141. 'this.DoIt = function (e, i) {',
  6142. ' var v = 0;',
  6143. ' var s = "";',
  6144. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6145. ' v = w;',
  6146. ' }));',
  6147. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6148. '};',
  6149. 'this.e = this.TEnum.Green;',
  6150. 'this.i = 0;',
  6151. 'this.s = "";',
  6152. '']),
  6153. LinesToStr([
  6154. '$mod.i=$mod.TEnum.Green;',
  6155. '$mod.i=$mod.e;',
  6156. '$mod.e=$mod.TEnum.Green;',
  6157. '$mod.e=$mod.TEnum.Green;',
  6158. '$mod.e=$mod.TEnum.Blue;',
  6159. '$mod.e=$mod.TEnum.Blue;',
  6160. '$mod.e=$mod.TEnum.Blue-1;',
  6161. '$mod.e=$mod.e-1;',
  6162. '$mod.e=$mod.TEnum.Green+1;',
  6163. '$mod.e=$mod.e+1;',
  6164. '$mod.e=1;',
  6165. '$mod.e=$mod.i;',
  6166. '$mod.s = $mod.TEnum[$mod.e];',
  6167. '$mod.s = $mod.TEnum[$mod.e];',
  6168. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6169. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6170. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6171. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6172. ' $mod.i = v;',
  6173. '});',
  6174. '$mod.i=$mod.e;',
  6175. '']));
  6176. end;
  6177. procedure TTestModule.TestEnum_AsParams;
  6178. begin
  6179. StartProgram(false);
  6180. Add('type TEnum = (Red,Blue);');
  6181. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6182. Add('var vJ: TEnum;');
  6183. Add('begin');
  6184. Add(' vg:=vg;');
  6185. Add(' vj:=vh;');
  6186. Add(' vi:=vi;');
  6187. Add(' doit(vg,vg,vg);');
  6188. Add(' doit(vh,vh,vj);');
  6189. Add(' doit(vi,vi,vi);');
  6190. Add(' doit(vj,vj,vj);');
  6191. Add('end;');
  6192. Add('var i: TEnum;');
  6193. Add('begin');
  6194. Add(' doit(i,i,i);');
  6195. ConvertProgram;
  6196. CheckSource('TestEnum_AsParams',
  6197. LinesToStr([ // statements
  6198. 'this.TEnum = {',
  6199. ' "0": "Red",',
  6200. ' Red: 0,',
  6201. ' "1": "Blue",',
  6202. ' Blue: 1',
  6203. '};',
  6204. 'this.DoIt = function (vG,vH,vI) {',
  6205. ' var vJ = 0;',
  6206. ' vG = vG;',
  6207. ' vJ = vH;',
  6208. ' vI.set(vI.get());',
  6209. ' $mod.DoIt(vG, vG, {',
  6210. ' get: function () {',
  6211. ' return vG;',
  6212. ' },',
  6213. ' set: function (v) {',
  6214. ' vG = v;',
  6215. ' }',
  6216. ' });',
  6217. ' $mod.DoIt(vH, vH, {',
  6218. ' get: function () {',
  6219. ' return vJ;',
  6220. ' },',
  6221. ' set: function (v) {',
  6222. ' vJ = v;',
  6223. ' }',
  6224. ' });',
  6225. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6226. ' $mod.DoIt(vJ, vJ, {',
  6227. ' get: function () {',
  6228. ' return vJ;',
  6229. ' },',
  6230. ' set: function (v) {',
  6231. ' vJ = v;',
  6232. ' }',
  6233. ' });',
  6234. '};',
  6235. 'this.i = 0;'
  6236. ]),
  6237. LinesToStr([
  6238. '$mod.DoIt($mod.i,$mod.i,{',
  6239. ' p: $mod,',
  6240. ' get: function () {',
  6241. ' return this.p.i;',
  6242. ' },',
  6243. ' set: function (v) {',
  6244. ' this.p.i = v;',
  6245. ' }',
  6246. '});'
  6247. ]));
  6248. end;
  6249. procedure TTestModule.TestEnumRange_Array;
  6250. begin
  6251. StartProgram(false);
  6252. Add([
  6253. 'type',
  6254. ' TEnum = (Red, Green, Blue);',
  6255. ' TEnumRg = green..blue;',
  6256. ' TArr = array[TEnumRg] of byte;',
  6257. ' TArr2 = array[green..blue] of byte;',
  6258. 'var',
  6259. ' a: TArr;',
  6260. ' b: TArr = (3,4);',
  6261. ' c: TArr2 = (5,6);',
  6262. 'begin',
  6263. ' a[green] := b[blue];',
  6264. ' c[green] := c[blue];',
  6265. '']);
  6266. ConvertProgram;
  6267. CheckSource('TestEnumRange_Array',
  6268. LinesToStr([ // statements
  6269. 'this.TEnum = {',
  6270. ' "0": "Red",',
  6271. ' Red: 0,',
  6272. ' "1": "Green",',
  6273. ' Green: 1,',
  6274. ' "2": "Blue",',
  6275. ' Blue: 2',
  6276. '};',
  6277. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6278. 'this.b = [3, 4];',
  6279. 'this.c = [5, 6];',
  6280. '']),
  6281. LinesToStr([
  6282. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6283. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6284. '']));
  6285. end;
  6286. procedure TTestModule.TestEnum_ForIn;
  6287. begin
  6288. StartProgram(false);
  6289. Add([
  6290. 'type',
  6291. ' TEnum = (Red, Green, Blue);',
  6292. ' TEnumRg = green..blue;',
  6293. ' TArr = array[TEnum] of byte;',
  6294. ' TArrRg = array[TEnumRg] of byte;',
  6295. 'var',
  6296. ' e: TEnum;',
  6297. ' a1: TArr = (3,4,5);',
  6298. ' a2: TArrRg = (11,12);',
  6299. ' b: byte;',
  6300. 'begin',
  6301. ' for e in TEnum do ;',
  6302. ' for e in TEnumRg do ;',
  6303. ' for e in TArr do ;',
  6304. ' for e in TArrRg do ;',
  6305. ' for b in a1 do ;',
  6306. ' for b in a2 do ;',
  6307. '']);
  6308. ConvertProgram;
  6309. CheckSource('TestEnum_ForIn',
  6310. LinesToStr([ // statements
  6311. 'this.TEnum = {',
  6312. ' "0": "Red",',
  6313. ' Red: 0,',
  6314. ' "1": "Green",',
  6315. ' Green: 1,',
  6316. ' "2": "Blue",',
  6317. ' Blue: 2',
  6318. '};',
  6319. 'this.e = 0;',
  6320. 'this.a1 = [3, 4, 5];',
  6321. 'this.a2 = [11, 12];',
  6322. 'this.b = 0;',
  6323. '']),
  6324. LinesToStr([
  6325. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6326. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6327. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6328. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6329. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6330. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6331. '']));
  6332. end;
  6333. procedure TTestModule.TestEnum_ScopedNumber;
  6334. begin
  6335. Converter.Options:=Converter.Options+[coEnumNumbers];
  6336. StartProgram(false);
  6337. Add([
  6338. 'type',
  6339. ' TEnum = (Red, Green);',
  6340. 'var',
  6341. ' e: TEnum;',
  6342. 'begin',
  6343. ' e:=TEnum.Green;',
  6344. '']);
  6345. ConvertProgram;
  6346. CheckSource('TestEnum_ScopedNumber',
  6347. LinesToStr([ // statements
  6348. 'this.TEnum = {',
  6349. ' "0": "Red",',
  6350. ' Red: 0,',
  6351. ' "1": "Green",',
  6352. ' Green: 1',
  6353. '};',
  6354. 'this.e = 0;',
  6355. '']),
  6356. LinesToStr([
  6357. '$mod.e = 1;']));
  6358. end;
  6359. procedure TTestModule.TestEnum_InFunction;
  6360. begin
  6361. StartProgram(false);
  6362. Add([
  6363. 'const TEnum = 3;',
  6364. 'procedure DoIt;',
  6365. 'type',
  6366. ' TEnum = (Red, Green, Blue);',
  6367. ' procedure Sub;',
  6368. ' type',
  6369. ' TEnumSub = (Left, Right);',
  6370. ' var',
  6371. ' es: TEnumSub;',
  6372. ' begin',
  6373. ' es:=Left;',
  6374. ' end;',
  6375. 'var',
  6376. ' e, e2: TEnum;',
  6377. 'begin',
  6378. ' if e in [red,blue] then e2:=e;',
  6379. 'end;',
  6380. 'begin']);
  6381. ConvertProgram;
  6382. CheckSource('TestEnum_InFunction',
  6383. LinesToStr([ // statements
  6384. 'this.TEnum = 3;',
  6385. 'var TEnum$1 = {',
  6386. ' "0":"Red",',
  6387. ' Red:0,',
  6388. ' "1":"Green",',
  6389. ' Green:1,',
  6390. ' "2":"Blue",',
  6391. ' Blue:2',
  6392. ' };',
  6393. 'var TEnumSub = {',
  6394. ' "0": "Left",',
  6395. ' Left: 0,',
  6396. ' "1": "Right",',
  6397. ' Right: 1',
  6398. '};',
  6399. 'this.DoIt = function () {',
  6400. ' function Sub() {',
  6401. ' var es = 0;',
  6402. ' es = TEnumSub.Left;',
  6403. ' };',
  6404. ' var e = 0;',
  6405. ' var e2 = 0;',
  6406. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6407. '};',
  6408. '']),
  6409. LinesToStr([
  6410. '']));
  6411. end;
  6412. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6413. begin
  6414. StartUnit(true);
  6415. Add([
  6416. 'interface',
  6417. 'var color: (red, green);',
  6418. 'implementation',
  6419. 'initialization',
  6420. ' color:=green;',
  6421. '']);
  6422. ConvertUnit;
  6423. CheckSource('TestEnum_Name_Anonymous_Unit',
  6424. LinesToStr([
  6425. 'this.color$a = {',
  6426. ' "0": "red",',
  6427. ' red: 0,',
  6428. ' "1": "green",',
  6429. ' green: 1',
  6430. '};',
  6431. 'this.color = 0;',
  6432. '']),
  6433. LinesToStr([ // this.$init
  6434. '$mod.color = $mod.color$a.green;',
  6435. '']),
  6436. LinesToStr([ // implementation
  6437. '']) );
  6438. end;
  6439. procedure TTestModule.TestSet_Enum;
  6440. begin
  6441. StartProgram(false);
  6442. Add([
  6443. 'type',
  6444. ' TColor = (Red, Green, Blue);',
  6445. ' TColors = set of TColor;',
  6446. 'var',
  6447. ' c: TColor;',
  6448. ' s: TColors;',
  6449. ' t: TColors = [];',
  6450. ' u: TColors = [Red];',
  6451. 'begin',
  6452. ' s:=[];',
  6453. ' s:=[Green];',
  6454. ' s:=[Green,Blue];',
  6455. ' s:=[Red..Blue];',
  6456. ' s:=[Red,Green..Blue];',
  6457. ' s:=[Red,c];',
  6458. ' s:=t;',
  6459. ' s:=default(TColors);',
  6460. '']);
  6461. ConvertProgram;
  6462. CheckSource('TestSet',
  6463. LinesToStr([ // statements
  6464. 'this.TColor = {',
  6465. ' "0":"Red",',
  6466. ' Red:0,',
  6467. ' "1":"Green",',
  6468. ' Green:1,',
  6469. ' "2":"Blue",',
  6470. ' Blue:2',
  6471. ' };',
  6472. 'this.c = 0;',
  6473. 'this.s = {};',
  6474. 'this.t = {};',
  6475. 'this.u = rtl.createSet(this.TColor.Red);'
  6476. ]),
  6477. LinesToStr([
  6478. '$mod.s={};',
  6479. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6480. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6481. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6482. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6483. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6484. '$mod.s=rtl.refSet($mod.t);',
  6485. '$mod.s={};',
  6486. '']));
  6487. end;
  6488. procedure TTestModule.TestSet_Operators;
  6489. begin
  6490. StartProgram(false);
  6491. Add('type');
  6492. Add(' TColor = (Red, Green, Blue);');
  6493. Add(' TColors = set of tcolor;');
  6494. Add('var');
  6495. Add(' vC: TColor;');
  6496. Add(' vS: TColors;');
  6497. Add(' vT: TColors;');
  6498. Add(' vU: TColors;');
  6499. Add(' B: boolean;');
  6500. Add('begin');
  6501. Add(' include(vs,green);');
  6502. Add(' exclude(vs,vc);');
  6503. Add(' vs:=vt+vu;');
  6504. Add(' vs:=vt+[red];');
  6505. Add(' vs:=[red]+vt;');
  6506. Add(' vs:=[red]+[green];');
  6507. Add(' vs:=vt-vu;');
  6508. Add(' vs:=vt-[red];');
  6509. Add(' vs:=[red]-vt;');
  6510. Add(' vs:=[red]-[green];');
  6511. Add(' vs:=vt*vu;');
  6512. Add(' vs:=vt*[red];');
  6513. Add(' vs:=[red]*vt;');
  6514. Add(' vs:=[red]*[green];');
  6515. Add(' vs:=vt><vu;');
  6516. Add(' vs:=vt><[red];');
  6517. Add(' vs:=[red]><vt;');
  6518. Add(' vs:=[red]><[green];');
  6519. Add(' b:=vt=vu;');
  6520. Add(' b:=vt=[red];');
  6521. Add(' b:=[red]=vt;');
  6522. Add(' b:=[red]=[green];');
  6523. Add(' b:=vt<>vu;');
  6524. Add(' b:=vt<>[red];');
  6525. Add(' b:=[red]<>vt;');
  6526. Add(' b:=[red]<>[green];');
  6527. Add(' b:=vt<=vu;');
  6528. Add(' b:=vt<=[red];');
  6529. Add(' b:=[red]<=vt;');
  6530. Add(' b:=[red]<=[green];');
  6531. Add(' b:=vt>=vu;');
  6532. Add(' b:=vt>=[red];');
  6533. Add(' b:=[red]>=vt;');
  6534. Add(' b:=[red]>=[green];');
  6535. ConvertProgram;
  6536. CheckSource('TestSet_Operators',
  6537. LinesToStr([ // statements
  6538. 'this.TColor = {',
  6539. ' "0":"Red",',
  6540. ' Red:0,',
  6541. ' "1":"Green",',
  6542. ' Green:1,',
  6543. ' "2":"Blue",',
  6544. ' Blue:2',
  6545. ' };',
  6546. 'this.vC = 0;',
  6547. 'this.vS = {};',
  6548. 'this.vT = {};',
  6549. 'this.vU = {};',
  6550. 'this.B = false;'
  6551. ]),
  6552. LinesToStr([
  6553. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6554. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6555. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6556. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6557. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6558. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6559. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6560. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6561. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6562. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6563. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6564. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6565. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6566. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6567. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6568. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6569. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6570. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6571. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6572. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6573. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6574. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6575. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6576. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6577. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6578. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6579. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6580. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6581. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6582. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6583. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6584. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6585. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6586. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6587. '']));
  6588. end;
  6589. procedure TTestModule.TestSet_Operator_In;
  6590. begin
  6591. StartProgram(false);
  6592. Add([
  6593. 'type',
  6594. ' TColor = (Red, Green, Blue);',
  6595. ' TColors = set of tcolor;',
  6596. ' TColorRg = green..blue;',
  6597. 'var',
  6598. ' vC: tcolor;',
  6599. ' vT: tcolors;',
  6600. ' B: boolean;',
  6601. ' rg: TColorRg;',
  6602. 'begin',
  6603. ' b:=red in vt;',
  6604. ' b:=vc in vt;',
  6605. ' b:=green in [red..blue];',
  6606. ' b:=vc in [red..blue];',
  6607. ' ',
  6608. ' if red in vt then ;',
  6609. ' while vC in vt do ;',
  6610. ' repeat',
  6611. ' until vC in vt;',
  6612. ' if rg in [green..blue] then ;',
  6613. '']);
  6614. ConvertProgram;
  6615. CheckSource('TestSet_Operator_In',
  6616. LinesToStr([ // statements
  6617. 'this.TColor = {',
  6618. ' "0":"Red",',
  6619. ' Red:0,',
  6620. ' "1":"Green",',
  6621. ' Green:1,',
  6622. ' "2":"Blue",',
  6623. ' Blue:2',
  6624. ' };',
  6625. 'this.vC = 0;',
  6626. 'this.vT = {};',
  6627. 'this.B = false;',
  6628. 'this.rg = this.TColor.Green;',
  6629. '']),
  6630. LinesToStr([
  6631. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6632. '$mod.B = $mod.vC in $mod.vT;',
  6633. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6634. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6635. 'if ($mod.TColor.Red in $mod.vT) ;',
  6636. 'while ($mod.vC in $mod.vT) {',
  6637. '};',
  6638. 'do {',
  6639. '} while (!($mod.vC in $mod.vT));',
  6640. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6641. '']));
  6642. end;
  6643. procedure TTestModule.TestSet_Functions;
  6644. begin
  6645. StartProgram(false);
  6646. Add('type');
  6647. Add(' TMyEnum = (Red, Green);');
  6648. Add(' TMyEnums = set of TMyEnum;');
  6649. Add('var');
  6650. Add(' e: TMyEnum;');
  6651. Add(' s: TMyEnums;');
  6652. Add('begin');
  6653. Add(' e:=Low(TMyEnums);');
  6654. Add(' e:=Low(s);');
  6655. Add(' e:=High(TMyEnums);');
  6656. Add(' e:=High(s);');
  6657. ConvertProgram;
  6658. CheckSource('TestSetFunctions',
  6659. LinesToStr([ // statements
  6660. 'this.TMyEnum = {',
  6661. ' "0":"Red",',
  6662. ' Red:0,',
  6663. ' "1":"Green",',
  6664. ' Green:1',
  6665. ' };',
  6666. 'this.e = 0;',
  6667. 'this.s = {};'
  6668. ]),
  6669. LinesToStr([
  6670. '$mod.e=$mod.TMyEnum.Red;',
  6671. '$mod.e=$mod.TMyEnum.Red;',
  6672. '$mod.e=$mod.TMyEnum.Green;',
  6673. '$mod.e=$mod.TMyEnum.Green;',
  6674. '']));
  6675. end;
  6676. procedure TTestModule.TestSet_PassAsArgClone;
  6677. begin
  6678. StartProgram(false);
  6679. Add('type');
  6680. Add(' TMyEnum = (Red, Green);');
  6681. Add(' TMyEnums = set of TMyEnum;');
  6682. Add('procedure DoDefault(s: tmyenums); begin end;');
  6683. Add('procedure DoConst(const s: tmyenums); begin end;');
  6684. Add('var');
  6685. Add(' aSet: tmyenums;');
  6686. Add('begin');
  6687. Add(' dodefault(aset);');
  6688. Add(' doconst(aset);');
  6689. ConvertProgram;
  6690. CheckSource('TestSetFunctions',
  6691. LinesToStr([ // statements
  6692. 'this.TMyEnum = {',
  6693. ' "0":"Red",',
  6694. ' Red:0,',
  6695. ' "1":"Green",',
  6696. ' Green:1',
  6697. ' };',
  6698. 'this.DoDefault = function (s) {',
  6699. '};',
  6700. 'this.DoConst = function (s) {',
  6701. '};',
  6702. 'this.aSet = {};'
  6703. ]),
  6704. LinesToStr([
  6705. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6706. '$mod.DoConst($mod.aSet);',
  6707. '']));
  6708. end;
  6709. procedure TTestModule.TestSet_AsParams;
  6710. begin
  6711. StartProgram(false);
  6712. Add([
  6713. 'type TEnum = (Red,Blue);',
  6714. 'type TEnums = set of TEnum;',
  6715. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6716. 'var vJ: TEnums;',
  6717. 'begin',
  6718. ' Include(vg,red);',
  6719. ' Include(result,blue);',
  6720. ' vg:=vg;',
  6721. ' vj:=vh;',
  6722. ' vi:=vi;',
  6723. ' doit(vg,vg,vg);',
  6724. ' doit(vh,vh,vj);',
  6725. ' doit(vi,vi,vi);',
  6726. ' doit(vj,vj,vj);',
  6727. 'end;',
  6728. 'var i: TEnums;',
  6729. 'begin',
  6730. ' doit(i,i,i);']);
  6731. ConvertProgram;
  6732. CheckSource('TestSet_AsParams',
  6733. LinesToStr([ // statements
  6734. 'this.TEnum = {',
  6735. ' "0": "Red",',
  6736. ' Red: 0,',
  6737. ' "1": "Blue",',
  6738. ' Blue: 1',
  6739. '};',
  6740. 'this.DoIt = function (vG,vH,vI) {',
  6741. ' var Result = {};',
  6742. ' var vJ = {};',
  6743. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6744. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6745. ' vG = rtl.refSet(vG);',
  6746. ' vJ = rtl.refSet(vH);',
  6747. ' vI.set(rtl.refSet(vI.get()));',
  6748. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6749. ' get: function () {',
  6750. ' return vG;',
  6751. ' },',
  6752. ' set: function (v) {',
  6753. ' vG = v;',
  6754. ' }',
  6755. ' });',
  6756. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6757. ' get: function () {',
  6758. ' return vJ;',
  6759. ' },',
  6760. ' set: function (v) {',
  6761. ' vJ = v;',
  6762. ' }',
  6763. ' });',
  6764. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6765. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6766. ' get: function () {',
  6767. ' return vJ;',
  6768. ' },',
  6769. ' set: function (v) {',
  6770. ' vJ = v;',
  6771. ' }',
  6772. ' });',
  6773. ' return Result;',
  6774. '};',
  6775. 'this.i = {};'
  6776. ]),
  6777. LinesToStr([
  6778. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6779. ' p: $mod,',
  6780. ' get: function () {',
  6781. ' return this.p.i;',
  6782. ' },',
  6783. ' set: function (v) {',
  6784. ' this.p.i = v;',
  6785. ' }',
  6786. '});'
  6787. ]));
  6788. end;
  6789. procedure TTestModule.TestSet_Property;
  6790. begin
  6791. StartProgram(false);
  6792. Add('type');
  6793. Add(' TEnum = (Red,Blue);');
  6794. Add(' TEnums = set of TEnum;');
  6795. Add(' TObject = class');
  6796. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6797. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6798. Add(' property Colors: TEnums read GetColors write SetColors;');
  6799. Add(' end;');
  6800. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6801. Add('begin end;');
  6802. Add('var Obj: TObject;');
  6803. Add('begin');
  6804. Add(' Include(Obj.Colors,Red);');
  6805. Add(' Exclude(Obj.Colors,Red);');
  6806. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6807. ConvertProgram;
  6808. CheckSource('TestSet_Property',
  6809. LinesToStr([ // statements
  6810. 'this.TEnum = {',
  6811. ' "0": "Red",',
  6812. ' Red: 0,',
  6813. ' "1": "Blue",',
  6814. ' Blue: 1',
  6815. '};',
  6816. 'rtl.createClass(this, "TObject", null, function () {',
  6817. ' this.$init = function () {',
  6818. ' };',
  6819. ' this.$final = function () {',
  6820. ' };',
  6821. '});',
  6822. 'this.DoIt = function (i, j, k, l) {',
  6823. '};',
  6824. 'this.Obj = null;',
  6825. '']),
  6826. LinesToStr([
  6827. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6828. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6829. '']));
  6830. end;
  6831. procedure TTestModule.TestSet_EnumConst;
  6832. begin
  6833. StartProgram(false);
  6834. Add([
  6835. 'type',
  6836. ' TEnum = (Red,Blue);',
  6837. ' TEnums = set of TEnum;',
  6838. 'const',
  6839. ' Orange = red;',
  6840. 'var',
  6841. ' Enum: tenum;',
  6842. ' Enums: tenums;',
  6843. 'begin',
  6844. ' Include(enums,orange);',
  6845. ' Exclude(enums,orange);',
  6846. ' if orange in enums then;',
  6847. ' if orange in [orange,red] then;']);
  6848. ConvertProgram;
  6849. CheckSource('TestSet_EnumConst',
  6850. LinesToStr([ // statements
  6851. 'this.TEnum = {',
  6852. ' "0": "Red",',
  6853. ' Red: 0,',
  6854. ' "1": "Blue",',
  6855. ' Blue: 1',
  6856. '};',
  6857. 'this.Orange = this.TEnum.Red;',
  6858. 'this.Enum = 0;',
  6859. 'this.Enums = {};',
  6860. '']),
  6861. LinesToStr([
  6862. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6863. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6864. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6865. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6866. '']));
  6867. end;
  6868. procedure TTestModule.TestSet_IntConst;
  6869. begin
  6870. StartProgram(false);
  6871. Add([
  6872. 'type',
  6873. ' TEnums = set of Byte;',
  6874. 'const',
  6875. ' Orange = 0;',
  6876. 'var',
  6877. ' Enum: byte;',
  6878. ' Enums: tenums;',
  6879. 'begin',
  6880. ' Enums:=[];',
  6881. ' Enums:=[0];',
  6882. ' Enums:=[1..2];',
  6883. //' Include(enums,orange);',
  6884. //' Exclude(enums,orange);',
  6885. ' if orange in enums then;',
  6886. ' if orange in [orange,1] then;']);
  6887. ConvertProgram;
  6888. CheckSource('TestSet_IntConst',
  6889. LinesToStr([ // statements
  6890. 'this.Orange = 0;',
  6891. 'this.Enum = 0;',
  6892. 'this.Enums = {};',
  6893. '']),
  6894. LinesToStr([
  6895. '$mod.Enums = {};',
  6896. '$mod.Enums = rtl.createSet(0);',
  6897. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6898. 'if (0 in $mod.Enums) ;',
  6899. 'if (0 in rtl.createSet(0, 1)) ;',
  6900. '']));
  6901. end;
  6902. procedure TTestModule.TestSet_IntRange;
  6903. begin
  6904. StartProgram(false);
  6905. Add([
  6906. 'type',
  6907. ' TRange = 1..3;',
  6908. ' TEnums = set of TRange;',
  6909. 'const',
  6910. ' Orange = 2;',
  6911. 'var',
  6912. ' Enum: byte;',
  6913. ' Enums: TEnums;',
  6914. 'begin',
  6915. ' Enums:=[];',
  6916. ' Enums:=[1];',
  6917. ' Enums:=[2..3];',
  6918. ' Include(enums,orange);',
  6919. ' Exclude(enums,orange);',
  6920. ' if orange in enums then;',
  6921. ' if orange in [orange,1] then;']);
  6922. ConvertProgram;
  6923. CheckSource('TestSet_IntRange',
  6924. LinesToStr([ // statements
  6925. 'this.Orange = 2;',
  6926. 'this.Enum = 0;',
  6927. 'this.Enums = {};',
  6928. '']),
  6929. LinesToStr([
  6930. '$mod.Enums = {};',
  6931. '$mod.Enums = rtl.createSet(1);',
  6932. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6933. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6934. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6935. 'if (2 in $mod.Enums) ;',
  6936. 'if (2 in rtl.createSet(2, 1)) ;',
  6937. '']));
  6938. end;
  6939. procedure TTestModule.TestSet_AnonymousEnumType;
  6940. begin
  6941. StartProgram(false);
  6942. Add('type');
  6943. Add(' TFlags = set of (red, green);');
  6944. Add('const');
  6945. Add(' favorite = red;');
  6946. Add('var');
  6947. Add(' f: TFlags;');
  6948. Add(' i: longint;');
  6949. Add('begin');
  6950. Add(' Include(f,red);');
  6951. Add(' Include(f,favorite);');
  6952. Add(' i:=ord(red);');
  6953. Add(' i:=ord(favorite);');
  6954. Add(' i:=ord(low(TFlags));');
  6955. Add(' i:=ord(low(f));');
  6956. Add(' i:=ord(low(favorite));');
  6957. Add(' i:=ord(high(TFlags));');
  6958. Add(' i:=ord(high(f));');
  6959. Add(' i:=ord(high(favorite));');
  6960. Add(' f:=[green,favorite];');
  6961. ConvertProgram;
  6962. CheckSource('TestSet_AnonymousEnumType',
  6963. LinesToStr([ // statements
  6964. 'this.TFlags$a = {',
  6965. ' "0": "red",',
  6966. ' red: 0,',
  6967. ' "1": "green",',
  6968. ' green: 1',
  6969. '};',
  6970. 'this.favorite = this.TFlags$a.red;',
  6971. 'this.f = {};',
  6972. 'this.i = 0;',
  6973. '']),
  6974. LinesToStr([
  6975. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6976. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6977. '$mod.i = $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.green;',
  6983. '$mod.i = $mod.TFlags$a.green;',
  6984. '$mod.i = $mod.TFlags$a.green;',
  6985. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6986. '']));
  6987. end;
  6988. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6989. begin
  6990. exit;
  6991. StartProgram(false);
  6992. Add([
  6993. 'type',
  6994. ' TAtoZ = ''A''..''Z'';',
  6995. ' TSetOfAZ = set of TAtoZ;',
  6996. 'var',
  6997. ' c: char;',
  6998. ' a: TAtoZ;',
  6999. ' s: TSetOfAZ = [''P'',''A''];',
  7000. ' i: longint;',
  7001. 'begin',
  7002. ' Include(s,''S'');',
  7003. ' Include(s,c);',
  7004. ' Include(s,a);',
  7005. ' c:=low(TAtoZ);',
  7006. ' i:=ord(low(TAtoZ));',
  7007. ' a:=high(TAtoZ);',
  7008. ' a:=high(TSetOfAtoZ);',
  7009. ' s:=[a,c,''M''];',
  7010. '']);
  7011. ConvertProgram;
  7012. CheckSource('TestSet_AnonymousEnumTypeChar',
  7013. LinesToStr([ // statements
  7014. '']),
  7015. LinesToStr([
  7016. '']));
  7017. end;
  7018. procedure TTestModule.TestSet_ConstEnum;
  7019. begin
  7020. StartProgram(false);
  7021. Add([
  7022. 'type',
  7023. ' TEnum = (red,blue,green);',
  7024. ' TEnums = set of TEnum;',
  7025. 'const',
  7026. ' teAny = [low(TEnum)..high(TEnum)];',
  7027. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7028. 'var',
  7029. ' e: TEnum;',
  7030. ' s: TEnums;',
  7031. 'begin',
  7032. ' if blue in teAny then;',
  7033. ' if blue in teAny+[e] then;',
  7034. ' if blue in teAny+teRedBlue then;',
  7035. ' if e in [red,blue] then;',
  7036. ' s:=teAny;',
  7037. ' s:=teAny+[e];',
  7038. ' s:=[e]+teAny;',
  7039. ' s:=teAny+teRedBlue;',
  7040. ' s:=teAny+teRedBlue+[e];',
  7041. '']);
  7042. ConvertProgram;
  7043. CheckSource('TestSet_ConstEnum',
  7044. LinesToStr([ // statements
  7045. 'this.TEnum = {',
  7046. ' "0": "red",',
  7047. ' red: 0,',
  7048. ' "1": "blue",',
  7049. ' blue: 1,',
  7050. ' "2": "green",',
  7051. ' green: 2',
  7052. '};',
  7053. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7054. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7055. 'this.e = 0;',
  7056. 'this.s = {};',
  7057. '']),
  7058. LinesToStr([
  7059. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7060. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7061. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7062. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7063. '$mod.s = rtl.refSet($mod.teAny);',
  7064. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7065. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7066. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7067. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7068. '']));
  7069. end;
  7070. procedure TTestModule.TestSet_ConstChar;
  7071. begin
  7072. StartProgram(false);
  7073. Add([
  7074. 'const',
  7075. ' LowChars = [''a''..''z''];',
  7076. ' Chars = LowChars+[''A''..''Z''];',
  7077. ' sc = [''А'', ''Я''];',
  7078. 'var',
  7079. ' c: char;',
  7080. ' s: string;',
  7081. 'begin',
  7082. ' if c in lowchars then ;',
  7083. ' if ''a'' in lowchars then ;',
  7084. ' if s[1] in lowchars then ;',
  7085. ' if c in chars then ;',
  7086. ' if c in [''a''..''z'',''_''] then ;',
  7087. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7088. ' if ''Я'' in sc then ;',
  7089. ' if 3=ord('' '') then ;',
  7090. '']);
  7091. ConvertProgram;
  7092. CheckSource('TestSet_ConstChar',
  7093. LinesToStr([ // statements
  7094. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7095. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7096. 'this.sc = rtl.createSet(1040, 1071);',
  7097. 'this.c = "";',
  7098. 'this.s = "";',
  7099. '']),
  7100. LinesToStr([
  7101. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7102. 'if (97 in $mod.LowChars) ;',
  7103. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7104. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7105. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7106. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7107. 'if (1071 in $mod.sc) ;',
  7108. 'if (3 === 32) ;',
  7109. '']));
  7110. end;
  7111. procedure TTestModule.TestSet_ConstInt;
  7112. begin
  7113. StartProgram(false);
  7114. Add([
  7115. 'const',
  7116. ' Months = [1..12];',
  7117. ' Mirror = [-12..-1]+Months;',
  7118. 'var',
  7119. ' i: smallint;',
  7120. 'begin',
  7121. ' if 3 in Months then;',
  7122. ' if i in Months+[i] then;',
  7123. ' if i in Months+Mirror then;',
  7124. ' if i in [4..6,8] then;',
  7125. '']);
  7126. ConvertProgram;
  7127. CheckSource('TestSet_ConstInt',
  7128. LinesToStr([ // statements
  7129. 'this.Months = rtl.createSet(null, 1, 12);',
  7130. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7131. 'this.i = 0;',
  7132. '']),
  7133. LinesToStr([
  7134. 'if (3 in $mod.Months) ;',
  7135. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7136. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7137. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7138. '']));
  7139. end;
  7140. procedure TTestModule.TestSet_InFunction;
  7141. begin
  7142. StartProgram(false);
  7143. Add([
  7144. 'const',
  7145. ' TEnum = 3;',
  7146. ' TSetOfEnum = 4;',
  7147. ' TSetOfAno = 5;',
  7148. 'procedure DoIt;',
  7149. 'type',
  7150. ' TEnum = (red, blue);',
  7151. ' TSetOfEnum = set of TEnum;',
  7152. ' TSetOfAno = set of (up,down);',
  7153. 'var',
  7154. ' e: TEnum;',
  7155. ' se: TSetOfEnum;',
  7156. ' sa: TSetOfAno;',
  7157. 'begin',
  7158. ' se:=[e];',
  7159. ' sa:=[up];',
  7160. 'end;',
  7161. 'begin',
  7162. '']);
  7163. ConvertProgram;
  7164. CheckSource('TestSet_InFunction',
  7165. LinesToStr([ // statements
  7166. 'this.TEnum = 3;',
  7167. 'this.TSetOfEnum = 4;',
  7168. 'this.TSetOfAno = 5;',
  7169. 'var TEnum$1 = {',
  7170. ' "0": "red",',
  7171. ' red: 0,',
  7172. ' "1": "blue",',
  7173. ' blue: 1',
  7174. '};',
  7175. 'var TSetOfAno$a = {',
  7176. ' "0": "up",',
  7177. ' up: 0,',
  7178. ' "1": "down",',
  7179. ' down: 1',
  7180. '};',
  7181. 'this.DoIt = function () {',
  7182. ' var e = 0;',
  7183. ' var se = {};',
  7184. ' var sa = {};',
  7185. ' se = rtl.createSet(e);',
  7186. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7187. '};',
  7188. '']),
  7189. LinesToStr([
  7190. '']));
  7191. end;
  7192. procedure TTestModule.TestSet_ForIn;
  7193. begin
  7194. StartProgram(false);
  7195. Add([
  7196. 'type',
  7197. ' TEnum = (Red, Green, Blue);',
  7198. ' TEnumRg = green..blue;',
  7199. ' TSetOfEnum = set of TEnum;',
  7200. ' TSetOfEnumRg = set of TEnumRg;',
  7201. 'var',
  7202. ' e, e2: TEnum;',
  7203. ' er: TEnum;',
  7204. ' s: TSetOfEnum;',
  7205. 'begin',
  7206. ' for e in TSetOfEnum do ;',
  7207. ' for e in TSetOfEnumRg do ;',
  7208. ' for e in [] do e2:=e;',
  7209. ' for e in [red..green] do e2:=e;',
  7210. ' for e in [green,blue] do e2:=e;',
  7211. ' for e in [red,blue] do e2:=e;',
  7212. ' for e in s do e2:=e;',
  7213. ' for er in TSetOfEnumRg do ;',
  7214. '']);
  7215. ConvertProgram;
  7216. CheckSource('TestSet_ForIn',
  7217. LinesToStr([ // statements
  7218. 'this.TEnum = {',
  7219. ' "0":"Red",',
  7220. ' Red:0,',
  7221. ' "1":"Green",',
  7222. ' Green:1,',
  7223. ' "2":"Blue",',
  7224. ' Blue:2',
  7225. ' };',
  7226. 'this.e = 0;',
  7227. 'this.e2 = 0;',
  7228. 'this.er = 0;',
  7229. 'this.s = {};',
  7230. '']),
  7231. LinesToStr([
  7232. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7233. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7234. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7235. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7236. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7237. 'for (var $l in $mod.s){',
  7238. ' $mod.e = +$l;',
  7239. ' $mod.e2 = $mod.e;',
  7240. '};',
  7241. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7242. '']));
  7243. end;
  7244. procedure TTestModule.TestNestBegin;
  7245. begin
  7246. StartProgram(false);
  7247. Add('begin');
  7248. Add(' begin');
  7249. Add(' begin');
  7250. Add(' end;');
  7251. Add(' begin');
  7252. Add(' if true then ;');
  7253. Add(' end;');
  7254. Add(' end;');
  7255. ConvertProgram;
  7256. CheckSource('TestNestBegin',
  7257. '',
  7258. 'if (true) ;');
  7259. end;
  7260. procedure TTestModule.TestUnitImplVars;
  7261. begin
  7262. StartUnit(false);
  7263. Add('interface');
  7264. Add('implementation');
  7265. Add('var');
  7266. Add(' V1:longint;');
  7267. Add(' V2:longint = 3;');
  7268. Add(' V3:string = ''abc'';');
  7269. ConvertUnit;
  7270. CheckSource('TestUnitImplVars',
  7271. LinesToStr([ // statements
  7272. 'var $impl = $mod.$impl;',
  7273. '']),
  7274. '', // this.$init
  7275. LinesToStr([ // implementation
  7276. '$impl.V1 = 0;',
  7277. '$impl.V2 = 3;',
  7278. '$impl.V3 = "abc";',
  7279. '']) );
  7280. end;
  7281. procedure TTestModule.TestUnitImplConsts;
  7282. begin
  7283. StartUnit(false);
  7284. Add('interface');
  7285. Add('implementation');
  7286. Add('const');
  7287. Add(' v1 = 3;');
  7288. Add(' v2:longint = 4;');
  7289. Add(' v3:string = ''abc'';');
  7290. ConvertUnit;
  7291. CheckSource('TestUnitImplConsts',
  7292. LinesToStr([ // statements
  7293. 'var $impl = $mod.$impl;',
  7294. '']),
  7295. '', // this.$init
  7296. LinesToStr([ // implementation
  7297. '$impl.v1 = 3;',
  7298. '$impl.v2 = 4;',
  7299. '$impl.v3 = "abc";',
  7300. '']) );
  7301. end;
  7302. procedure TTestModule.TestUnitImplRecord;
  7303. begin
  7304. StartUnit(false);
  7305. Add('interface');
  7306. Add('implementation');
  7307. Add('type');
  7308. Add(' TMyRecord = record');
  7309. Add(' i: longint;');
  7310. Add(' end;');
  7311. Add('var aRec: TMyRecord;');
  7312. Add('initialization');
  7313. Add(' arec.i:=3;');
  7314. ConvertUnit;
  7315. CheckSource('TestUnitImplRecord',
  7316. LinesToStr([ // statements
  7317. 'var $impl = $mod.$impl;',
  7318. '']),
  7319. // this.$init
  7320. '$impl.aRec.i = 3;',
  7321. LinesToStr([ // implementation
  7322. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7323. ' this.i = 0;',
  7324. ' this.$eq = function (b) {',
  7325. ' return this.i === b.i;',
  7326. ' };',
  7327. ' this.$assign = function (s) {',
  7328. ' this.i = s.i;',
  7329. ' return this;',
  7330. ' };',
  7331. '});',
  7332. '$impl.aRec = $impl.TMyRecord.$new();',
  7333. '']) );
  7334. end;
  7335. procedure TTestModule.TestRenameJSNameConflict;
  7336. begin
  7337. StartProgram(false);
  7338. Add('var apply: longint;');
  7339. Add('var bind: longint;');
  7340. Add('var call: longint;');
  7341. Add('begin');
  7342. ConvertProgram;
  7343. CheckSource('TestRenameJSNameConflict',
  7344. LinesToStr([ // statements
  7345. 'this.Apply = 0;',
  7346. 'this.Bind = 0;',
  7347. 'this.Call = 0;'
  7348. ]),
  7349. LinesToStr([ // this.$main
  7350. ''
  7351. ]));
  7352. end;
  7353. procedure TTestModule.TestLocalConst;
  7354. begin
  7355. StartProgram(false);
  7356. Add('procedure DoIt;');
  7357. Add('const');
  7358. Add(' cA: longint = 1;');
  7359. Add(' cB = 2;');
  7360. Add(' procedure Sub;');
  7361. Add(' const');
  7362. Add(' csA = 3;');
  7363. Add(' cB: double = 4;');
  7364. Add(' begin');
  7365. Add(' cb:=cb+csa;');
  7366. Add(' ca:=ca+csa+5;');
  7367. Add(' end;');
  7368. Add('begin');
  7369. Add(' ca:=ca+cb+6;');
  7370. Add('end;');
  7371. Add('begin');
  7372. ConvertProgram;
  7373. CheckSource('TestLocalConst',
  7374. LinesToStr([
  7375. 'var cA = 1;',
  7376. 'var cB = 2;',
  7377. 'var csA = 3;',
  7378. 'var cB$1 = 4;',
  7379. 'this.DoIt = function () {',
  7380. ' function Sub() {',
  7381. ' cB$1 = cB$1 + 3;',
  7382. ' cA = cA + 3 + 5;',
  7383. ' };',
  7384. ' cA = cA + 2 + 6;',
  7385. '};'
  7386. ]),
  7387. LinesToStr([
  7388. ]));
  7389. end;
  7390. procedure TTestModule.TestVarExternal;
  7391. begin
  7392. StartProgram(false);
  7393. Add('var');
  7394. Add(' NaN: double; external name ''Global.NaN'';');
  7395. Add(' d: double;');
  7396. Add('begin');
  7397. Add(' d:=NaN;');
  7398. ConvertProgram;
  7399. CheckSource('TestVarExternal',
  7400. LinesToStr([
  7401. 'this.d = 0.0;'
  7402. ]),
  7403. LinesToStr([
  7404. '$mod.d = Global.NaN;'
  7405. ]));
  7406. end;
  7407. procedure TTestModule.TestVarExternalOtherUnit;
  7408. begin
  7409. AddModuleWithIntfImplSrc('unit2.pas',
  7410. LinesToStr([
  7411. 'var NaN: double; external name ''Global.NaN'';',
  7412. 'var iV: longint;'
  7413. ]),
  7414. '');
  7415. StartUnit(true);
  7416. Add('interface');
  7417. Add('uses unit2;');
  7418. Add('implementation');
  7419. Add('var');
  7420. Add(' d: double;');
  7421. Add(' i: longint; external name ''$i'';');
  7422. Add('begin');
  7423. Add(' d:=nan;');
  7424. Add(' d:=uNit2.nan;');
  7425. Add(' d:=test1.d;');
  7426. Add(' i:=iv;');
  7427. Add(' i:=uNit2.iv;');
  7428. Add(' i:=test1.i;');
  7429. ConvertUnit;
  7430. CheckSource('TestVarExternalOtherUnit',
  7431. LinesToStr([
  7432. 'var $impl = $mod.$impl;',
  7433. '']),
  7434. LinesToStr([ // this.$init
  7435. '$impl.d = Global.NaN;',
  7436. '$impl.d = Global.NaN;',
  7437. '$impl.d = $impl.d;',
  7438. '$i = pas.unit2.iV;',
  7439. '$i = pas.unit2.iV;',
  7440. '$i = $i;',
  7441. '']),
  7442. LinesToStr([ // implementation
  7443. '$impl.d = 0.0;',
  7444. '']) );
  7445. end;
  7446. procedure TTestModule.TestVarAbsoluteFail;
  7447. begin
  7448. StartProgram(false);
  7449. Add([
  7450. 'var',
  7451. ' a: longint;',
  7452. ' b: longword absolute a;',
  7453. 'begin']);
  7454. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7455. ConvertProgram;
  7456. end;
  7457. procedure TTestModule.TestConstExternal;
  7458. begin
  7459. StartProgram(false);
  7460. Add([
  7461. 'const',
  7462. ' PI: double; external name ''Global.PI'';',
  7463. ' Tau = 2*pi;',
  7464. 'var d: double;',
  7465. 'begin',
  7466. ' d:=pi;',
  7467. ' d:=tau+pi;']);
  7468. ConvertProgram;
  7469. CheckSource('TestConstExternal',
  7470. LinesToStr([
  7471. 'this.Tau = 2*Global.PI;',
  7472. 'this.d = 0.0;'
  7473. ]),
  7474. LinesToStr([
  7475. '$mod.d = Global.PI;',
  7476. '$mod.d = $mod.Tau + Global.PI;'
  7477. ]));
  7478. end;
  7479. procedure TTestModule.TestDouble;
  7480. begin
  7481. StartProgram(false);
  7482. Add([
  7483. 'type',
  7484. ' TDateTime = double;',
  7485. 'const',
  7486. ' a = TDateTime(2.7);',
  7487. ' b = a + TDateTime(1.7);',
  7488. ' c = 0.9 + 0.1;',
  7489. ' f0_1 = 0.1;',
  7490. ' f0_3 = 0.3;',
  7491. ' fn0_1 = -0.1;',
  7492. ' fn0_3 = -0.3;',
  7493. ' fn0_003 = -0.003;',
  7494. ' fn0_123456789 = -0.123456789;',
  7495. ' fn300_0 = -300.0;',
  7496. ' fn123456_0 = -123456.0;',
  7497. ' fn1234567_8 = -1234567.8;',
  7498. ' fn12345678_9 = -12345678.9;',
  7499. ' f1_0En12 = 1E-12;',
  7500. ' fn1_0En12 = -1E-12;',
  7501. ' maxdouble = 1.7e+308;',
  7502. ' mindouble = -1.7e+308;',
  7503. ' MinSafeIntDouble = -$1fffffffffffff;',
  7504. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7505. ' MaxSafeIntDouble = $1fffffffffffff;',
  7506. ' DZeroResolution = 1E-12;',
  7507. ' Minus1 = -1E-12;',
  7508. ' EPS = 1E-9;',
  7509. ' DELTA = 0.001;',
  7510. ' Big = 129.789E+100;',
  7511. ' Test0_15 = 0.15;',
  7512. ' Test999 = 2.9999999999999;',
  7513. ' Test111999 = 211199999999999000.0;',
  7514. ' TestMinus111999 = -211199999999999000.0;',
  7515. ' Inf = 1.0 / 0.0;',
  7516. ' NegInf = -1.0 / 0.0;',
  7517. 'procedure Run(d: double); external name ''Run'';',
  7518. 'var',
  7519. ' d: double = b;',
  7520. 'begin',
  7521. ' d:=1.0;',
  7522. ' d:=1.0/3.0;',
  7523. ' d:=1.0/(3-2-1);',
  7524. ' d:=1/3;',
  7525. ' d:=5.0E-324;',
  7526. ' d:=1.7E308;',
  7527. ' d:=001.00E00;',
  7528. ' d:=002.00E001;',
  7529. ' d:=003.000E000;',
  7530. ' d:=-004.00E-00;',
  7531. ' d:=-005.00E-001;',
  7532. ' d:=10**3;',
  7533. ' d:=10 mod 3;',
  7534. ' d:=10 div 3;',
  7535. ' d:=c;',
  7536. ' d:=f0_1;',
  7537. ' d:=f0_3;',
  7538. ' d:=fn0_1;',
  7539. ' d:=fn0_3;',
  7540. ' d:=fn0_003;',
  7541. ' d:=fn0_123456789;',
  7542. ' d:=fn300_0;',
  7543. ' d:=fn123456_0;',
  7544. ' d:=fn1234567_8;',
  7545. ' d:=fn12345678_9;',
  7546. ' d:=f1_0En12;',
  7547. ' d:=fn1_0En12;',
  7548. ' d:=maxdouble;',
  7549. ' d:=mindouble;',
  7550. ' d:=MinSafeIntDouble;',
  7551. ' d:=double(MinSafeIntDouble);',
  7552. ' d:=MinSafeIntDouble2;',
  7553. ' d:=double(MinSafeIntDouble2);',
  7554. ' d:=MaxSafeIntDouble;',
  7555. ' d:=default(double);',
  7556. ' Run(Inf);',
  7557. ' Run(NegInf);',
  7558. '']);
  7559. ConvertProgram;
  7560. CheckSource('TestDouble',
  7561. LinesToStr([
  7562. 'this.a = 2.7;',
  7563. 'this.b = 2.7 + 1.7;',
  7564. 'this.c = 0.9 + 0.1;',
  7565. 'this.f0_1 = 0.1;',
  7566. 'this.f0_3 = 0.3;',
  7567. 'this.fn0_1 = -0.1;',
  7568. 'this.fn0_3 = -0.3;',
  7569. 'this.fn0_003 = -0.003;',
  7570. 'this.fn0_123456789 = -0.123456789;',
  7571. 'this.fn300_0 = -300.0;',
  7572. 'this.fn123456_0 = -123456.0;',
  7573. 'this.fn1234567_8 = -1234567.8;',
  7574. 'this.fn12345678_9 = -12345678.9;',
  7575. 'this.f1_0En12 = 1E-12;',
  7576. 'this.fn1_0En12 = -1E-12;',
  7577. 'this.maxdouble = 1.7e+308;',
  7578. 'this.mindouble = -1.7e+308;',
  7579. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7580. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7581. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7582. 'this.DZeroResolution = 1E-12;',
  7583. 'this.Minus1 = -1E-12;',
  7584. 'this.EPS = 1E-9;',
  7585. 'this.DELTA = 0.001;',
  7586. 'this.Big = 129.789E+100;',
  7587. 'this.Test0_15 = 0.15;',
  7588. 'this.Test999 = 2.9999999999999;',
  7589. 'this.Test111999 = 211199999999999000.0;',
  7590. 'this.TestMinus111999 = -211199999999999000.0;',
  7591. 'this.Inf = 1.0 / 0.0;',
  7592. 'this.NegInf = -1.0 / 0.0;',
  7593. 'this.d = 4.4;',
  7594. '']),
  7595. LinesToStr([
  7596. '$mod.d = 1.0;',
  7597. '$mod.d = 1.0 / 3.0;',
  7598. '$mod.d = 1.0 / (3 - 2 - 1);',
  7599. '$mod.d = 1 / 3;',
  7600. '$mod.d = 5.0E-324;',
  7601. '$mod.d = 1.7E308;',
  7602. '$mod.d = 1.00E0;',
  7603. '$mod.d = 2.00E1;',
  7604. '$mod.d = 3.000E0;',
  7605. '$mod.d = -4.00E-0;',
  7606. '$mod.d = -5.00E-1;',
  7607. '$mod.d = Math.pow(10, 3);',
  7608. '$mod.d = 10 % 3;',
  7609. '$mod.d = rtl.trunc(10 / 3);',
  7610. '$mod.d = 1;',
  7611. '$mod.d = 0.1;',
  7612. '$mod.d = 0.3;',
  7613. '$mod.d = -0.1;',
  7614. '$mod.d = -0.3;',
  7615. '$mod.d = -3E-3;',
  7616. '$mod.d = -0.123456789;',
  7617. '$mod.d = -300;',
  7618. '$mod.d = -123456;',
  7619. '$mod.d = -1234567.8;',
  7620. '$mod.d = -1.23456789E7;',
  7621. '$mod.d = 1E-12;',
  7622. '$mod.d = -1E-12;',
  7623. '$mod.d = 1.7E308;',
  7624. '$mod.d = -1.7E308;',
  7625. '$mod.d = -9007199254740991;',
  7626. '$mod.d = -9007199254740991;',
  7627. '$mod.d = -9.007199254740992E15;',
  7628. '$mod.d = -9.007199254740992E15;',
  7629. '$mod.d = 9007199254740991;',
  7630. '$mod.d = 0.0;',
  7631. 'Run(1 / 0);',
  7632. 'Run(-1 / 0);',
  7633. '']));
  7634. end;
  7635. procedure TTestModule.TestDoubleSmall;
  7636. begin
  7637. StartProgram(false);
  7638. Add([
  7639. 'const',
  7640. ' a = 1e-1;',
  7641. ' b = 1e-2;',
  7642. ' c = 1e-3;',
  7643. ' d = 1e-4;',
  7644. ' e = 1e-5;',
  7645. ' f = 1e-6;',
  7646. ' g = 1e-7;',
  7647. ' h = -1e-1;',
  7648. ' i = -1e-2;',
  7649. 'procedure Fly(d: double);',
  7650. 'begin',
  7651. 'end;',
  7652. 'begin',
  7653. ' Fly(a);',
  7654. ' Fly(b);',
  7655. ' Fly(c);',
  7656. ' Fly(d);',
  7657. ' Fly(e);',
  7658. ' Fly(f);',
  7659. ' Fly(g);',
  7660. ' Fly(h);',
  7661. ' Fly(i);',
  7662. '']);
  7663. ConvertProgram;
  7664. CheckSource('TestDoubleSmall',
  7665. LinesToStr([
  7666. 'this.a = 1e-1;',
  7667. 'this.b = 1e-2;',
  7668. 'this.c = 1e-3;',
  7669. 'this.d = 1e-4;',
  7670. 'this.e = 1e-5;',
  7671. 'this.f = 1e-6;',
  7672. 'this.g = 1e-7;',
  7673. 'this.h = -1e-1;',
  7674. 'this.i = -1e-2;',
  7675. 'this.Fly = function (d) {',
  7676. '};',
  7677. '']),
  7678. LinesToStr([
  7679. '$mod.Fly(0.1);',
  7680. '$mod.Fly(0.01);',
  7681. '$mod.Fly(1E-3);',
  7682. '$mod.Fly(1E-4);',
  7683. '$mod.Fly(1E-5);',
  7684. '$mod.Fly(1E-6);',
  7685. '$mod.Fly(1E-7);',
  7686. '$mod.Fly(-0.1);',
  7687. '$mod.Fly(-0.01);',
  7688. '']));
  7689. end;
  7690. procedure TTestModule.TestInteger;
  7691. begin
  7692. StartProgram(false);
  7693. Add([
  7694. 'const',
  7695. ' MinInt = low(NativeInt);',
  7696. ' MaxInt = high(NativeInt);',
  7697. 'type',
  7698. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7699. 'const',
  7700. ' a = low(TMyInt)+High(TMyInt);',
  7701. 'var',
  7702. ' i: TMyInt;',
  7703. 'begin',
  7704. ' i:=-MinInt;',
  7705. ' i:=default(TMyInt);',
  7706. ' i:=low(i)+high(i);',
  7707. '']);
  7708. ConvertProgram;
  7709. CheckSource('TestIntegerRange',
  7710. LinesToStr([
  7711. 'this.MinInt = -9007199254740991;',
  7712. 'this.MaxInt = 9007199254740991;',
  7713. 'this.a = -9007199254740991 + 9007199254740991;',
  7714. 'this.i = 0;',
  7715. '']),
  7716. LinesToStr([
  7717. '$mod.i = - -9007199254740991;',
  7718. '$mod.i = -9007199254740991;',
  7719. '$mod.i = -9007199254740991 + 9007199254740991;',
  7720. '']));
  7721. end;
  7722. procedure TTestModule.TestIntegerRange;
  7723. begin
  7724. StartProgram(false);
  7725. Add([
  7726. 'const',
  7727. ' MinInt = -1;',
  7728. ' MaxInt = +1;',
  7729. 'type',
  7730. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7731. ' TInt2 = 1..3;',
  7732. 'const',
  7733. ' a = low(TMyInt)+High(TMyInt);',
  7734. ' b = low(TInt2)+High(TInt2);',
  7735. ' s1 = [1];',
  7736. ' s2 = [1,2];',
  7737. ' s3 = [1..3];',
  7738. ' s4 = [low(shortint)..high(shortint)];',
  7739. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7740. ' s6 = 1 in s2;',
  7741. 'var',
  7742. ' i: TMyInt;',
  7743. ' i2: TInt2;',
  7744. 'begin',
  7745. ' i:=i2;',
  7746. ' i:=default(TMyInt);',
  7747. ' if i=i2 then ;',
  7748. ' i:=ord(i2);',
  7749. '']);
  7750. ConvertProgram;
  7751. CheckSource('TestIntegerRange',
  7752. LinesToStr([
  7753. 'this.MinInt = -1;',
  7754. 'this.MaxInt = +1;',
  7755. 'this.a = -1 + 1;',
  7756. 'this.b = 1 + 3;',
  7757. 'this.s1 = rtl.createSet(1);',
  7758. 'this.s2 = rtl.createSet(1, 2);',
  7759. 'this.s3 = rtl.createSet(null, 1, 3);',
  7760. 'this.s4 = rtl.createSet(null, -128, 127);',
  7761. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7762. 'this.s6 = 1 in this.s2;',
  7763. 'this.i = 0;',
  7764. 'this.i2 = 0;',
  7765. '']),
  7766. LinesToStr([
  7767. '$mod.i = $mod.i2;',
  7768. '$mod.i = -1;',
  7769. 'if ($mod.i === $mod.i2) ;',
  7770. '$mod.i = $mod.i2;',
  7771. '']));
  7772. end;
  7773. procedure TTestModule.TestIntegerTypecasts;
  7774. begin
  7775. StartProgram(false);
  7776. Add([
  7777. 'var',
  7778. ' i: nativeint;',
  7779. ' b: byte;',
  7780. ' sh: shortint;',
  7781. ' w: word;',
  7782. ' sm: smallint;',
  7783. ' lw: longword;',
  7784. ' li: longint;',
  7785. 'begin',
  7786. ' b:=byte(i);',
  7787. ' sh:=shortint(i);',
  7788. ' w:=word(i);',
  7789. ' sm:=smallint(i);',
  7790. ' lw:=longword(i);',
  7791. ' li:=longint(i);',
  7792. '']);
  7793. ConvertProgram;
  7794. CheckSource('TestIntegerTypecasts',
  7795. LinesToStr([
  7796. 'this.i = 0;',
  7797. 'this.b = 0;',
  7798. 'this.sh = 0;',
  7799. 'this.w = 0;',
  7800. 'this.sm = 0;',
  7801. 'this.lw = 0;',
  7802. 'this.li = 0;',
  7803. '']),
  7804. LinesToStr([
  7805. '$mod.b = $mod.i & 255;',
  7806. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7807. '$mod.w = $mod.i & 65535;',
  7808. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7809. '$mod.lw = $mod.i >>> 0;',
  7810. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7811. '']));
  7812. end;
  7813. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7814. begin
  7815. StartProgram(false);
  7816. Add([
  7817. 'var',
  7818. ' i,j: nativeint;',
  7819. 'begin',
  7820. ' i:=i shr 0;',
  7821. ' i:=i shr 1;',
  7822. ' i:=i shr 3;',
  7823. ' i:=i shr 54;',
  7824. ' i:=j shr i;',
  7825. '']);
  7826. ConvertProgram;
  7827. CheckResolverUnexpectedHints;
  7828. CheckSource('TestInteger_BitwiseShrNativeInt',
  7829. LinesToStr([
  7830. 'this.i = 0;',
  7831. 'this.j = 0;',
  7832. '']),
  7833. LinesToStr([
  7834. '$mod.i = $mod.i;',
  7835. '$mod.i = Math.floor($mod.i / 2);',
  7836. '$mod.i = Math.floor($mod.i / 8);',
  7837. '$mod.i = 0;',
  7838. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7839. '']));
  7840. end;
  7841. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7842. begin
  7843. StartProgram(false);
  7844. Add([
  7845. 'var',
  7846. ' i: nativeint;',
  7847. 'begin',
  7848. ' i:=i shl 0;',
  7849. ' i:=i shl 54;',
  7850. ' i:=123456789012 shl 1;',
  7851. ' i:=i shl 1;',
  7852. '']);
  7853. ConvertProgram;
  7854. CheckResolverUnexpectedHints;
  7855. CheckSource('TestInteger_BitwiseShrNativeInt',
  7856. LinesToStr([
  7857. 'this.i = 0;',
  7858. '']),
  7859. LinesToStr([
  7860. '$mod.i = $mod.i;',
  7861. '$mod.i = 0;',
  7862. '$mod.i = 246913578024;',
  7863. '$mod.i = rtl.shl($mod.i, 1);',
  7864. '']));
  7865. end;
  7866. procedure TTestModule.TestInteger_SystemFunc;
  7867. begin
  7868. StartProgram(true);
  7869. Add([
  7870. 'var',
  7871. ' i: byte;',
  7872. ' s: string;',
  7873. 'begin',
  7874. ' system.inc(i);',
  7875. ' system.str(i,s);',
  7876. ' s:=system.str(i);',
  7877. ' i:=system.low(i);',
  7878. ' i:=system.high(i);',
  7879. ' i:=system.pred(i);',
  7880. ' i:=system.succ(i);',
  7881. ' i:=system.ord(i);',
  7882. '']);
  7883. ConvertProgram;
  7884. CheckResolverUnexpectedHints;
  7885. CheckSource('TestInteger_SystemFunc',
  7886. LinesToStr([
  7887. 'this.i = 0;',
  7888. 'this.s = "";',
  7889. '']),
  7890. LinesToStr([
  7891. '$mod.i += 1;',
  7892. '$mod.s = "" + $mod.i;',
  7893. '$mod.s = "" + $mod.i;',
  7894. '$mod.i = 0;',
  7895. '$mod.i = 255;',
  7896. '$mod.i = $mod.i - 1;',
  7897. '$mod.i = $mod.i + 1;',
  7898. '$mod.i = $mod.i;',
  7899. '']));
  7900. end;
  7901. procedure TTestModule.TestInteger_AssignOutsideConst;
  7902. begin
  7903. StartProgram(false);
  7904. Add([
  7905. 'const',
  7906. ' MinInt = low(longint);',
  7907. ' MaxInt = high(longint);',
  7908. 'type',
  7909. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7910. 'var',
  7911. ' i: TMyInt;',
  7912. ' aByte: byte;',
  7913. ' aShortInt: shortint;',
  7914. ' aWord: word;',
  7915. ' aSmallInt: smallint;',
  7916. ' aLongWord: longword;',
  7917. ' aLongInt: longint;',
  7918. ' aNativeInt: nativeint;',
  7919. ' aNativeUInt: nativeuint;',
  7920. 'begin',
  7921. ' aByte:=$FF;',
  7922. ' aByte:=$100;',
  7923. ' aByte:=-1;',
  7924. ' aByte:=-127;',
  7925. ' aByte:=-128;',
  7926. ' aByte:=-254;',
  7927. ' aByte:=-255;',
  7928. ' aByte:=-256;',
  7929. ' aShortInt:=127;',
  7930. ' aShortInt:=128;',
  7931. ' aShortInt:=-128;',
  7932. ' aShortInt:=-129;',
  7933. ' aWord:=$ffff;',
  7934. ' aWord:=$10000;',
  7935. ' aWord:=-1;',
  7936. ' aWord:=-$ffff;',
  7937. ' aWord:=-$10000;',
  7938. ' aWord:=-$10001;',
  7939. ' aSmallInt:=$7fff;',
  7940. ' aSmallInt:=$8000;',
  7941. ' aSmallInt:=-$8000;',
  7942. ' aSmallInt:=-$8001;',
  7943. ' aLongWord:=$ffffffff;',
  7944. ' aLongWord:=$100000000;',
  7945. ' aLongWord:=-1;',
  7946. ' aLongWord:=-$ffffffff;',
  7947. ' aNativeInt:=$1fffffffffffff;',
  7948. ' aNativeInt:=-$1fffffffffffff;',
  7949. ' aNativeUInt:=$1fffffffffffff;',
  7950. ' aNativeUInt:=-$1fffffffffffff;',
  7951. '']);
  7952. ConvertProgram;
  7953. CheckSource('TestInteger_AssignOutsideConst',
  7954. LinesToStr([
  7955. 'this.MinInt = -2147483648;',
  7956. 'this.MaxInt = 2147483647;',
  7957. 'this.i = 0;',
  7958. 'this.aByte = 0;',
  7959. 'this.aShortInt = 0;',
  7960. 'this.aWord = 0;',
  7961. 'this.aSmallInt = 0;',
  7962. 'this.aLongWord = 0;',
  7963. 'this.aLongInt = 0;',
  7964. 'this.aNativeInt = 0;',
  7965. 'this.aNativeUInt = 0;',
  7966. '']),
  7967. LinesToStr([
  7968. '$mod.aByte = 0xFF;',
  7969. '$mod.aByte = 0;',
  7970. '$mod.aByte = 255;',
  7971. '$mod.aByte = 129;',
  7972. '$mod.aByte = 128;',
  7973. '$mod.aByte = 2;',
  7974. '$mod.aByte = 1;',
  7975. '$mod.aByte = 0;',
  7976. '$mod.aShortInt = 127;',
  7977. '$mod.aShortInt = -128;',
  7978. '$mod.aShortInt = -128;',
  7979. '$mod.aShortInt = 127;',
  7980. '$mod.aWord = 0xffff;',
  7981. '$mod.aWord = 0;',
  7982. '$mod.aWord = 65535;',
  7983. '$mod.aWord = 1;',
  7984. '$mod.aWord = 0;',
  7985. '$mod.aWord = 65535;',
  7986. '$mod.aSmallInt = 0x7fff;',
  7987. '$mod.aSmallInt = -32768;',
  7988. '$mod.aSmallInt = -0x8000;',
  7989. '$mod.aSmallInt = 32767;',
  7990. '$mod.aLongWord = 0xffffffff;',
  7991. '$mod.aLongWord = 0;',
  7992. '$mod.aLongWord = 4294967295;',
  7993. '$mod.aLongWord = 1;',
  7994. '$mod.aNativeInt = 0x1fffffffffffff;',
  7995. '$mod.aNativeInt = -0x1fffffffffffff;',
  7996. '$mod.aNativeUInt = 0x1fffffffffffff;',
  7997. '$mod.aNativeUInt = 1;',
  7998. '']));
  7999. end;
  8000. procedure TTestModule.TestCurrency;
  8001. begin
  8002. StartProgram(false);
  8003. Add([
  8004. 'type',
  8005. ' TCoin = currency;',
  8006. 'const',
  8007. ' a = TCoin(2.7);',
  8008. ' b = a + TCoin(1.7);',
  8009. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  8010. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  8011. 'var',
  8012. ' c: TCoin = b;',
  8013. ' i: nativeint;',
  8014. ' d: double;',
  8015. ' j: jsvalue;',
  8016. 'function DoIt(c: currency): currency; begin end;',
  8017. 'function GetIt(d: double): double; begin end;',
  8018. 'procedure Write(v: jsvalue); begin end;',
  8019. 'begin',
  8020. ' c:=1.0;',
  8021. ' c:=0.1;',
  8022. ' c:=1.0/3.0;',
  8023. ' c:=1/3;',
  8024. ' c:=a;',
  8025. ' d:=c;',
  8026. ' c:=d;',
  8027. ' c:=currency(c);',
  8028. ' c:=currency(d);',
  8029. ' d:=double(c);',
  8030. ' c:=i;',
  8031. ' c:=currency(i);',
  8032. //' i:=c;', not allowed
  8033. ' i:=nativeint(c);',
  8034. ' c:=c+a;',
  8035. ' c:=-c-a;',
  8036. ' c:=d+c;',
  8037. ' c:=c+d;',
  8038. ' c:=d-c;',
  8039. ' c:=c-d;',
  8040. ' c:=c*a;',
  8041. ' c:=a*c;',
  8042. ' c:=d*c;',
  8043. ' c:=c*d;',
  8044. ' c:=c/a;',
  8045. ' c:=a/c;',
  8046. ' c:=d/c;',
  8047. ' c:=c/d;',
  8048. ' c:=c**a;',
  8049. ' c:=a**c;',
  8050. ' c:=d**c;',
  8051. ' c:=c**d;',
  8052. ' if c=c then ;',
  8053. ' if c=a then ;',
  8054. ' if a=c then ;',
  8055. ' if d=c then ;',
  8056. ' if c=d then ;',
  8057. ' c:=DoIt(c);',
  8058. ' c:=DoIt(i);',
  8059. ' c:=DoIt(d);',
  8060. ' c:=GetIt(c);',
  8061. ' j:=c;',
  8062. ' Write(c);',
  8063. ' c:=default(currency);',
  8064. ' j:=str(c);',
  8065. ' j:=str(c:0:3);',
  8066. '']);
  8067. ConvertProgram;
  8068. CheckSource('TestCurrency',
  8069. LinesToStr([
  8070. 'this.a = 27000;',
  8071. 'this.b = this.a + 17000;',
  8072. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8073. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8074. 'this.c = this.b;',
  8075. 'this.i = 0;',
  8076. 'this.d = 0.0;',
  8077. 'this.j = undefined;',
  8078. 'this.DoIt = function (c) {',
  8079. ' var Result = 0;',
  8080. ' return Result;',
  8081. '};',
  8082. 'this.GetIt = function (d) {',
  8083. ' var Result = 0.0;',
  8084. ' return Result;',
  8085. '};',
  8086. 'this.Write = function (v) {',
  8087. '};',
  8088. '']),
  8089. LinesToStr([
  8090. '$mod.c = 10000;',
  8091. '$mod.c = 1000;',
  8092. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8093. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8094. '$mod.c = $mod.a;',
  8095. '$mod.d = $mod.c / 10000;',
  8096. '$mod.c = rtl.trunc($mod.d * 10000);',
  8097. '$mod.c = $mod.c;',
  8098. '$mod.c = $mod.d * 10000;',
  8099. '$mod.d = $mod.c / 10000;',
  8100. '$mod.c = $mod.i * 10000;',
  8101. '$mod.c = $mod.i * 10000;',
  8102. '$mod.i = rtl.trunc($mod.c / 10000);',
  8103. '$mod.c = $mod.c + $mod.a;',
  8104. '$mod.c = -$mod.c - $mod.a;',
  8105. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8106. '$mod.c = $mod.c + ($mod.d * 10000);',
  8107. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8108. '$mod.c = $mod.c - ($mod.d * 10000);',
  8109. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8110. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8111. '$mod.c = $mod.d * $mod.c;',
  8112. '$mod.c = $mod.c * $mod.d;',
  8113. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8114. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8115. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8116. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8117. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8118. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8119. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8120. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8121. 'if ($mod.c === $mod.c) ;',
  8122. 'if ($mod.c === $mod.a) ;',
  8123. 'if ($mod.a === $mod.c) ;',
  8124. 'if (($mod.d * 10000) === $mod.c) ;',
  8125. 'if ($mod.c === ($mod.d * 10000)) ;',
  8126. '$mod.c = $mod.DoIt($mod.c);',
  8127. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8128. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8129. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8130. '$mod.j = $mod.c / 10000;',
  8131. '$mod.Write($mod.c / 10000);',
  8132. '$mod.c = 0;',
  8133. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8134. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8135. '']));
  8136. end;
  8137. procedure TTestModule.TestForBoolDo;
  8138. begin
  8139. StartProgram(false);
  8140. Add([
  8141. 'var b: boolean;',
  8142. 'begin',
  8143. ' for b:=false to true do ;',
  8144. ' for b:=b downto false do ;',
  8145. ' for b in boolean do ;',
  8146. '']);
  8147. ConvertProgram;
  8148. CheckSource('TestForBoolDo',
  8149. LinesToStr([ // statements
  8150. 'this.b = false;']),
  8151. LinesToStr([ // this.$main
  8152. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8153. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8154. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8155. '']));
  8156. end;
  8157. procedure TTestModule.TestForIntDo;
  8158. begin
  8159. StartProgram(false);
  8160. Add([
  8161. 'var i: longint;',
  8162. 'begin',
  8163. ' for i:=3 to 5 do ;',
  8164. ' for i:=i downto 2 do ;',
  8165. ' for i in byte do ;',
  8166. '']);
  8167. ConvertProgram;
  8168. CheckSource('TestForIntDo',
  8169. LinesToStr([ // statements
  8170. 'this.i = 0;']),
  8171. LinesToStr([ // this.$main
  8172. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8173. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8174. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8175. '']));
  8176. end;
  8177. procedure TTestModule.TestForIntInDo;
  8178. begin
  8179. StartProgram(false);
  8180. Add([
  8181. 'type',
  8182. ' TSetOfInt = set of byte;',
  8183. ' TIntRg = 3..7;',
  8184. ' TSetOfIntRg = set of TIntRg;',
  8185. 'var',
  8186. ' i,i2: longint;',
  8187. ' a1: array of byte;',
  8188. ' a2: array[1..3] of byte;',
  8189. ' soi: TSetOfInt;',
  8190. ' soir: TSetOfIntRg;',
  8191. ' ir: TIntRg;',
  8192. 'begin',
  8193. ' for i in byte do ;',
  8194. ' for i in a1 do ;',
  8195. ' for i in a2 do ;',
  8196. ' for i in [11..13] do ;',
  8197. ' for i in TSetOfInt do ;',
  8198. ' for i in TIntRg do ;',
  8199. ' for i in soi do i2:=i;',
  8200. ' for i in TSetOfIntRg do ;',
  8201. ' for i in soir do ;',
  8202. ' for ir in TIntRg do ;',
  8203. ' for ir in TSetOfIntRg do ;',
  8204. ' for ir in soir do ;',
  8205. '']);
  8206. ConvertProgram;
  8207. CheckSource('TestForIntInDo',
  8208. LinesToStr([ // statements
  8209. 'this.i = 0;',
  8210. 'this.i2 = 0;',
  8211. 'this.a1 = [];',
  8212. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8213. 'this.soi = {};',
  8214. 'this.soir = {};',
  8215. 'this.ir = 0;',
  8216. '']),
  8217. LinesToStr([ // this.$main
  8218. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8219. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8220. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8221. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8222. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8223. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8224. 'for (var $l6 in $mod.soi) {',
  8225. ' $mod.i = +$l6;',
  8226. ' $mod.i2 = $mod.i;',
  8227. '};',
  8228. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8229. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8230. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8231. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8232. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8233. '']));
  8234. end;
  8235. procedure TTestModule.TestCharConst;
  8236. begin
  8237. StartProgram(false);
  8238. Add([
  8239. 'const',
  8240. ' a = #$00F3;',
  8241. ' c: char = ''1'';',
  8242. ' wc: widechar = ''ä'';',
  8243. 'begin',
  8244. ' c:=#0;',
  8245. ' c:=#1;',
  8246. ' c:=#9;',
  8247. ' c:=#10;',
  8248. ' c:=#13;',
  8249. ' c:=#31;',
  8250. ' c:=#32;',
  8251. ' c:=#$A;',
  8252. ' c:=#$0A;',
  8253. ' c:=#$b;',
  8254. ' c:=#$0b;',
  8255. ' c:=^A;',
  8256. ' c:=''"'';',
  8257. ' c:=default(char);',
  8258. ' c:=#$00E4;', // ä
  8259. ' c:=''ä'';',
  8260. ' c:=#$E4;', // ä
  8261. ' c:=#$D800;', // invalid UTF-16
  8262. ' c:=#$DFFF;', // invalid UTF-16
  8263. ' c:=#$FFFF;', // last UCS-2
  8264. ' c:=high(c);', // last UCS-2
  8265. ' c:=#269;',
  8266. '']);
  8267. ConvertProgram;
  8268. CheckSource('TestCharConst',
  8269. LinesToStr([
  8270. 'this.a="ó";',
  8271. 'this.c="1";',
  8272. 'this.wc="ä";'
  8273. ]),
  8274. LinesToStr([
  8275. '$mod.c="\x00";',
  8276. '$mod.c="\x01";',
  8277. '$mod.c="\t";',
  8278. '$mod.c="\n";',
  8279. '$mod.c="\r";',
  8280. '$mod.c="\x1F";',
  8281. '$mod.c=" ";',
  8282. '$mod.c="\n";',
  8283. '$mod.c="\n";',
  8284. '$mod.c="\x0B";',
  8285. '$mod.c="\x0B";',
  8286. '$mod.c="\x01";',
  8287. '$mod.c=''"'';',
  8288. '$mod.c="\x00";',
  8289. '$mod.c = "ä";',
  8290. '$mod.c = "ä";',
  8291. '$mod.c = "ä";',
  8292. '$mod.c="\uD800";',
  8293. '$mod.c="\uDFFF";',
  8294. '$mod.c="\uFFFF";',
  8295. '$mod.c="\uFFFF";',
  8296. '$mod.c = "č";',
  8297. '']));
  8298. end;
  8299. procedure TTestModule.TestChar_Compare;
  8300. begin
  8301. StartProgram(false);
  8302. Add('var');
  8303. Add(' c: char;');
  8304. Add(' b: boolean;');
  8305. Add('begin');
  8306. Add(' b:=c=''1'';');
  8307. Add(' b:=''2''=c;');
  8308. Add(' b:=''3''=''4'';');
  8309. Add(' b:=c<>''5'';');
  8310. Add(' b:=''6''<>c;');
  8311. Add(' b:=c>''7'';');
  8312. Add(' b:=''8''>c;');
  8313. Add(' b:=c>=''9'';');
  8314. Add(' b:=''A''>=c;');
  8315. Add(' b:=c<''B'';');
  8316. Add(' b:=''C''<c;');
  8317. Add(' b:=c<=''D'';');
  8318. Add(' b:=''E''<=c;');
  8319. ConvertProgram;
  8320. CheckSource('TestChar_Compare',
  8321. LinesToStr([
  8322. 'this.c="";',
  8323. 'this.b = false;'
  8324. ]),
  8325. LinesToStr([
  8326. '$mod.b = $mod.c === "1";',
  8327. '$mod.b = "2" === $mod.c;',
  8328. '$mod.b = "3" === "4";',
  8329. '$mod.b = $mod.c !== "5";',
  8330. '$mod.b = "6" !== $mod.c;',
  8331. '$mod.b = $mod.c > "7";',
  8332. '$mod.b = "8" > $mod.c;',
  8333. '$mod.b = $mod.c >= "9";',
  8334. '$mod.b = "A" >= $mod.c;',
  8335. '$mod.b = $mod.c < "B";',
  8336. '$mod.b = "C" < $mod.c;',
  8337. '$mod.b = $mod.c <= "D";',
  8338. '$mod.b = "E" <= $mod.c;',
  8339. '']));
  8340. end;
  8341. procedure TTestModule.TestChar_BuiltInProcs;
  8342. begin
  8343. StartProgram(false);
  8344. Add([
  8345. 'var',
  8346. ' c: char;',
  8347. ' i: longint;',
  8348. ' s: string;',
  8349. 'begin',
  8350. ' i:=ord(c);',
  8351. ' i:=ord(s[i]);',
  8352. ' c:=chr(i);',
  8353. ' c:=pred(c);',
  8354. ' c:=succ(c);',
  8355. ' c:=low(c);',
  8356. ' c:=high(c);',
  8357. ' i:=byte(c);',
  8358. ' i:=word(c);',
  8359. ' i:=longint(c);',
  8360. '']);
  8361. ConvertProgram;
  8362. CheckSource('TestChar_BuiltInProcs',
  8363. LinesToStr([
  8364. 'this.c = "";',
  8365. 'this.i = 0;',
  8366. 'this.s = "";'
  8367. ]),
  8368. LinesToStr([
  8369. '$mod.i = $mod.c.charCodeAt();',
  8370. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8371. '$mod.c = String.fromCharCode($mod.i);',
  8372. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8373. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8374. '$mod.c = "\x00";',
  8375. '$mod.c = "\uFFFF";',
  8376. '$mod.i = $mod.c.charCodeAt() & 255;',
  8377. '$mod.i = $mod.c.charCodeAt();',
  8378. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8379. '']));
  8380. end;
  8381. procedure TTestModule.TestStringConst;
  8382. begin
  8383. StartProgram(false);
  8384. Add([
  8385. '{$H+}',
  8386. 'const',
  8387. ' a = #$00F3#$017C;', // first <256, then >=256
  8388. ' b = string(''a'');',
  8389. ' c = string(''ä'');',
  8390. ' d = UnicodeString(''b'');',
  8391. ' e = UnicodeString(''ö'');',
  8392. ' f = low(a)+high(b);',
  8393. ' g: word = low(a);',
  8394. 'var',
  8395. ' s: string = ''abc'';',
  8396. ' i: longint;',
  8397. 'begin',
  8398. ' s:='''';',
  8399. ' s:=#13#10;',
  8400. ' s:=#9''foo'';',
  8401. ' s:=#$A9;',
  8402. ' s:=''foo''#13''bar'';',
  8403. ' s:=''"'';',
  8404. ' s:=''"''''"'';',
  8405. ' s:=#$20AC;', // euro
  8406. ' s:=#$10437;', // outside BMP
  8407. ' s:=''abc''#$20AC;', // ascii,#
  8408. ' s:=''ä''#$20AC;', // non ascii,#
  8409. ' s:=#$20AC''abc'';', // #, ascii
  8410. ' s:=#$20AC''ä'';', // #, non ascii
  8411. ' s:=default(string);',
  8412. ' s:=concat(s);',
  8413. ' s:=concat(s,''a'',s);',
  8414. ' s:=#250#269;',
  8415. ' i:=low(s)+high(a);',
  8416. ' s:=''a/b'';',
  8417. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8418. '']);
  8419. ConvertProgram;
  8420. CheckSource('TestStringConst',
  8421. LinesToStr([
  8422. 'this.a = "óż";',
  8423. 'this.b = "a";',
  8424. 'this.c = "ä";',
  8425. 'this.d = "b";',
  8426. 'this.e = "ö";',
  8427. 'this.f = 1 + this.b.length;',
  8428. 'this.g = 1;',
  8429. 'this.s="abc";',
  8430. 'this.i = 0;',
  8431. '']),
  8432. LinesToStr([
  8433. '$mod.s="";',
  8434. '$mod.s="\r\n";',
  8435. '$mod.s="\tfoo";',
  8436. '$mod.s="©";',
  8437. '$mod.s="foo\rbar";',
  8438. '$mod.s=''"'';',
  8439. '$mod.s=''"\''"'';',
  8440. '$mod.s="€";',
  8441. '$mod.s="'#$F0#$90#$90#$B7'";',
  8442. '$mod.s = "abc€";',
  8443. '$mod.s = "ä€";',
  8444. '$mod.s = "€abc";',
  8445. '$mod.s = "ۊ";',
  8446. '$mod.s="";',
  8447. '$mod.s = $mod.s;',
  8448. '$mod.s = $mod.s.concat("a", $mod.s);',
  8449. '$mod.s = "úč";',
  8450. '$mod.i = 1 + $mod.a.length;',
  8451. '$mod.s = "a/b";',
  8452. '']));
  8453. end;
  8454. procedure TTestModule.TestStringConst_InvalidUTF16;
  8455. begin
  8456. StartProgram(false);
  8457. Add([
  8458. 'const',
  8459. ' a: char = #$D87E;',
  8460. ' b: string = #$D87E;',
  8461. ' c: string = #$D87E#43;',
  8462. 'begin',
  8463. ' c:=''abc''#$D87E;',
  8464. ' c:=#0#1#2;',
  8465. ' c:=#127;',
  8466. ' c:=#128;',
  8467. ' c:=#255;',
  8468. ' c:=#256;',
  8469. '']);
  8470. ConvertProgram;
  8471. CheckSource('TestStringConst',
  8472. LinesToStr([
  8473. 'this.a = "\uD87E";',
  8474. 'this.b = "\uD87E";',
  8475. 'this.c = "\uD87E+";',
  8476. '']),
  8477. LinesToStr([
  8478. '$mod.c = "abc\uD87E";',
  8479. '$mod.c = "\x00\x01\x02";',
  8480. '$mod.c = "'#127'";',
  8481. '$mod.c = "'#$c2#$80'";',
  8482. '$mod.c = "'#$c3#$BF'";',
  8483. '$mod.c = "'#$c4#$80'";',
  8484. '']));
  8485. end;
  8486. procedure TTestModule.TestStringConstSurrogate;
  8487. begin
  8488. StartProgram(false);
  8489. Add([
  8490. 'var',
  8491. ' s: string;',
  8492. 'begin',
  8493. ' s:=''😊'';', // 1F60A
  8494. '']);
  8495. ConvertProgram;
  8496. CheckSource('TestStringConstSurrogate',
  8497. LinesToStr([
  8498. 'this.s="";'
  8499. ]),
  8500. LinesToStr([
  8501. '$mod.s="😊";'
  8502. ]));
  8503. end;
  8504. procedure TTestModule.TestStringConst_Multiline;
  8505. begin
  8506. StartProgram(false);
  8507. Add([
  8508. '{$modeswitch multilinestrings}',
  8509. 'const',
  8510. ' a = ``;',
  8511. ' b = `',
  8512. 'line`;',
  8513. ' c = `Single`;',
  8514. ' d = ````;',
  8515. ' e = `abc``xyz`;',
  8516. ' f = `first''line',
  8517. ' second''line`#10;',
  8518. 'begin',
  8519. '']);
  8520. ConvertProgram;
  8521. CheckSource('TestStringConst_Multiline',
  8522. LinesToStr([
  8523. 'this.a = "";',
  8524. 'this.b = "\nline";',
  8525. 'this.c = "Single";',
  8526. 'this.d = "`";',
  8527. 'this.e = "abc`xyz";',
  8528. 'this.f = "first''line\n second''line\n";',
  8529. '']),
  8530. LinesToStr([
  8531. ]));
  8532. end;
  8533. procedure TTestModule.TestString_Length;
  8534. begin
  8535. StartProgram(false);
  8536. Add('const c = ''foo'';');
  8537. Add('var');
  8538. Add(' s: string;');
  8539. Add(' i: longint;');
  8540. Add('begin');
  8541. Add(' i:=length(s);');
  8542. Add(' i:=length(s+s);');
  8543. Add(' i:=length(''abc'');');
  8544. Add(' i:=length(c);');
  8545. ConvertProgram;
  8546. CheckSource('TestString_Length',
  8547. LinesToStr([
  8548. 'this.c = "foo";',
  8549. 'this.s = "";',
  8550. 'this.i = 0;',
  8551. '']),
  8552. LinesToStr([
  8553. '$mod.i = $mod.s.length;',
  8554. '$mod.i = ($mod.s+$mod.s).length;',
  8555. '$mod.i = "abc".length;',
  8556. '$mod.i = $mod.c.length;',
  8557. '']));
  8558. end;
  8559. procedure TTestModule.TestString_Compare;
  8560. begin
  8561. StartProgram(false);
  8562. Add('var');
  8563. Add(' s, t: string;');
  8564. Add(' b: boolean;');
  8565. Add('begin');
  8566. Add(' b:=s=t;');
  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. ConvertProgram;
  8573. CheckSource('TestString_Compare',
  8574. LinesToStr([ // statements
  8575. 'this.s = "";',
  8576. 'this.t = "";',
  8577. 'this.b =false;'
  8578. ]),
  8579. LinesToStr([ // this.$main
  8580. '$mod.b = $mod.s === $mod.t;',
  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. '']));
  8587. end;
  8588. procedure TTestModule.TestString_SetLength;
  8589. begin
  8590. StartProgram(false);
  8591. Add([
  8592. 'procedure Fly(var s: string);',
  8593. 'begin',
  8594. ' SetLength(s,1);',
  8595. 'end;',
  8596. 'procedure Run(var s: unicodestring);',
  8597. 'begin',
  8598. ' SetLength(s,2);',
  8599. 'end;',
  8600. 'var s: string;',
  8601. ' u: unicodestring;',
  8602. 'begin',
  8603. ' SetLength(s,3);',
  8604. ' SetLength(u,4);',
  8605. '']);
  8606. ConvertProgram;
  8607. CheckSource('TestString_SetLength',
  8608. LinesToStr([ // statements
  8609. 'this.Fly = function (s) {',
  8610. ' s.set(rtl.strSetLength(s.get(), 1));',
  8611. '};',
  8612. 'this.Run = function (s) {',
  8613. ' s.set(rtl.strSetLength(s.get(), 2));',
  8614. '};',
  8615. 'this.s = "";',
  8616. 'this.u = "";',
  8617. '']),
  8618. LinesToStr([ // this.$main
  8619. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8620. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8621. ]));
  8622. end;
  8623. procedure TTestModule.TestString_CharAt;
  8624. begin
  8625. StartProgram(false);
  8626. Add([
  8627. 'var',
  8628. ' s: string;',
  8629. ' c: char;',
  8630. ' b: boolean;',
  8631. 'begin',
  8632. ' b:= s[1] = c;',
  8633. ' b:= c = s[1];',
  8634. ' b:= c <> s[1];',
  8635. ' b:= c > s[1];',
  8636. ' b:= c >= s[1];',
  8637. ' b:= c < s[2];',
  8638. ' b:= c <= s[1];',
  8639. ' s[1] := c;',
  8640. ' s[2+3] := c;']);
  8641. ConvertProgram;
  8642. CheckSource('TestString_CharAt',
  8643. LinesToStr([ // statements
  8644. 'this.s = "";',
  8645. 'this.c = "";',
  8646. 'this.b = false;'
  8647. ]),
  8648. LinesToStr([ // this.$main
  8649. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8650. '$mod.b = $mod.c === $mod.s.charAt(0);',
  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(1);',
  8655. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8656. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8657. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8658. '']));
  8659. end;
  8660. procedure TTestModule.TestStringHMinusFail;
  8661. begin
  8662. StartProgram(false);
  8663. Add([
  8664. '{$H-}',
  8665. 'var s: string;',
  8666. 'begin']);
  8667. ConvertProgram;
  8668. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8669. end;
  8670. procedure TTestModule.TestStr;
  8671. begin
  8672. StartProgram(false);
  8673. Add('var');
  8674. Add(' b: boolean;');
  8675. Add(' i: longint;');
  8676. Add(' d: double;');
  8677. Add(' s: string;');
  8678. Add('begin');
  8679. Add(' str(b,s);');
  8680. Add(' str(i,s);');
  8681. Add(' str(d,s);');
  8682. Add(' str(i:3,s);');
  8683. Add(' str(d:3:2,s);');
  8684. Add(' Str(12.456:12:1,s);');
  8685. Add(' Str(12.456:12,s);');
  8686. Add(' s:=str(b);');
  8687. Add(' s:=str(i);');
  8688. Add(' s:=str(d);');
  8689. Add(' s:=str(i,i);');
  8690. Add(' s:=str(i:3);');
  8691. Add(' s:=str(d:3:2);');
  8692. Add(' s:=str(i:4,i);');
  8693. Add(' s:=str(i,i:5);');
  8694. Add(' s:=str(i:4,i:5);');
  8695. Add(' s:=str(s,s);');
  8696. Add(' s:=str(s,''foo'');');
  8697. ConvertProgram;
  8698. CheckSource('TestStr',
  8699. LinesToStr([ // statements
  8700. 'this.b = false;',
  8701. 'this.i = 0;',
  8702. 'this.d = 0.0;',
  8703. 'this.s = "";',
  8704. '']),
  8705. LinesToStr([ // this.$main
  8706. '$mod.s = ""+$mod.b;',
  8707. '$mod.s = ""+$mod.i;',
  8708. '$mod.s = rtl.floatToStr($mod.d);',
  8709. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8710. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8711. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8712. '$mod.s = rtl.floatToStr(12.456,12);',
  8713. '$mod.s = ""+$mod.b;',
  8714. '$mod.s = ""+$mod.i;',
  8715. '$mod.s = rtl.floatToStr($mod.d);',
  8716. '$mod.s = ""+$mod.i+$mod.i;',
  8717. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8718. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8719. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8720. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8721. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8722. '$mod.s = $mod.s + $mod.s;',
  8723. '$mod.s = $mod.s + "foo";',
  8724. '']));
  8725. end;
  8726. procedure TTestModule.TestBaseType_AnsiStringFail;
  8727. begin
  8728. StartProgram(false);
  8729. Add('var s: AnsiString');
  8730. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8731. ConvertProgram;
  8732. end;
  8733. procedure TTestModule.TestBaseType_WideStringFail;
  8734. begin
  8735. StartProgram(false);
  8736. Add('var s: WideString');
  8737. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8738. ConvertProgram;
  8739. end;
  8740. procedure TTestModule.TestBaseType_ShortStringFail;
  8741. begin
  8742. StartProgram(false);
  8743. Add('var s: ShortString');
  8744. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8745. ConvertProgram;
  8746. end;
  8747. procedure TTestModule.TestBaseType_RawByteStringFail;
  8748. begin
  8749. StartProgram(false);
  8750. Add('var s: RawByteString');
  8751. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8752. ConvertProgram;
  8753. end;
  8754. procedure TTestModule.TestTypeShortstring_Fail;
  8755. begin
  8756. StartProgram(false);
  8757. Add('type t = string[12];');
  8758. Add('var s: t;');
  8759. Add('begin');
  8760. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8761. ConvertProgram;
  8762. end;
  8763. procedure TTestModule.TestCharSet_Custom;
  8764. begin
  8765. StartProgram(false);
  8766. Add([
  8767. 'type',
  8768. ' TCharRg = ''a''..''z'';',
  8769. ' TSetOfCharRg = set of TCharRg;',
  8770. ' TCharRg2 = ''m''..''p'';',
  8771. 'const',
  8772. ' crg: TCharRg = ''b'';',
  8773. 'var',
  8774. ' c: char;',
  8775. ' crg2: TCharRg2;',
  8776. ' s: TSetOfCharRg;',
  8777. 'begin',
  8778. ' c:=crg;',
  8779. ' crg:=c;',
  8780. ' crg2:=crg;',
  8781. ' if c=crg then ;',
  8782. ' if crg=c then ;',
  8783. ' if crg=crg2 then ;',
  8784. ' if c in s then ;',
  8785. ' if crg2 in s then ;',
  8786. ' c:=default(TCharRg);',
  8787. '']);
  8788. ConvertProgram;
  8789. CheckSource('TestCharSet_Custom',
  8790. LinesToStr([ // statements
  8791. 'this.crg = "b";',
  8792. 'this.c = "";',
  8793. 'this.crg2 = "m";',
  8794. 'this.s = {};',
  8795. '']),
  8796. LinesToStr([ // this.$main
  8797. '$mod.c = $mod.crg;',
  8798. '$mod.crg = $mod.c;',
  8799. '$mod.crg2 = $mod.crg;',
  8800. 'if ($mod.c === $mod.crg) ;',
  8801. 'if ($mod.crg === $mod.c) ;',
  8802. 'if ($mod.crg === $mod.crg2) ;',
  8803. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8804. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8805. '$mod.c = "a";',
  8806. '']));
  8807. end;
  8808. procedure TTestModule.TestWideChar;
  8809. begin
  8810. StartProgram(false);
  8811. Add([
  8812. 'procedure Fly(var c: char);',
  8813. 'begin',
  8814. 'end;',
  8815. 'procedure Run(var c: widechar);',
  8816. 'begin',
  8817. 'end;',
  8818. 'var',
  8819. ' c: char;',
  8820. ' wc: widechar;',
  8821. ' w: word;',
  8822. 'begin',
  8823. ' Fly(wc);',
  8824. ' Run(c);',
  8825. ' wc:=WideChar(w);',
  8826. ' w:=ord(wc);',
  8827. '']);
  8828. ConvertProgram;
  8829. CheckSource('TestWideChar_VarArg',
  8830. LinesToStr([ // statements
  8831. 'this.Fly = function (c) {',
  8832. '};',
  8833. 'this.Run = function (c) {',
  8834. '};',
  8835. 'this.c = "";',
  8836. 'this.wc = "";',
  8837. 'this.w = 0;',
  8838. '']),
  8839. LinesToStr([ // this.$main
  8840. '$mod.Fly({',
  8841. ' p: $mod,',
  8842. ' get: function () {',
  8843. ' return this.p.wc;',
  8844. ' },',
  8845. ' set: function (v) {',
  8846. ' this.p.wc = v;',
  8847. ' }',
  8848. '});',
  8849. '$mod.Run({',
  8850. ' p: $mod,',
  8851. ' get: function () {',
  8852. ' return this.p.c;',
  8853. ' },',
  8854. ' set: function (v) {',
  8855. ' this.p.c = v;',
  8856. ' }',
  8857. '});',
  8858. '$mod.wc = String.fromCharCode($mod.w);',
  8859. '$mod.w = $mod.wc.charCodeAt();',
  8860. '',
  8861. '']));
  8862. end;
  8863. procedure TTestModule.TestForCharDo;
  8864. begin
  8865. StartProgram(false);
  8866. Add([
  8867. 'var c: char;',
  8868. 'begin',
  8869. ' for c:=''a'' to ''c'' do ;',
  8870. ' for c:=c downto ''a'' do ;',
  8871. ' for c:=''Б'' to ''Я'' do ;',
  8872. '']);
  8873. ConvertProgram;
  8874. CheckSource('TestForCharDo',
  8875. LinesToStr([ // statements
  8876. 'this.c = "";']),
  8877. LinesToStr([ // this.$main
  8878. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8879. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8880. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8881. '']));
  8882. end;
  8883. procedure TTestModule.TestForCharInDo;
  8884. begin
  8885. StartProgram(false);
  8886. Add([
  8887. 'type',
  8888. ' TSetOfChar = set of char;',
  8889. ' TCharRg = ''a''..''z'';',
  8890. ' TSetOfCharRg = set of TCharRg;',
  8891. 'const Foo = ''foo'';',
  8892. 'var',
  8893. ' c,c2: char;',
  8894. ' s: string;',
  8895. ' a1: array of char;',
  8896. ' a2: array[1..3] of char;',
  8897. ' soc: TSetOfChar;',
  8898. ' socr: TSetOfCharRg;',
  8899. ' cr: TCharRg;',
  8900. 'begin',
  8901. ' for c in foo do ;',
  8902. ' for c in s do ;',
  8903. ' for c in char do ;',
  8904. ' for c in a1 do ;',
  8905. ' for c in a2 do ;',
  8906. ' for c in [''1''..''3''] do ;',
  8907. ' for c in TSetOfChar do ;',
  8908. ' for c in TCharRg do ;',
  8909. ' for c in soc do c2:=c;',
  8910. ' for c in TSetOfCharRg do ;',
  8911. ' for c in socr do ;',
  8912. ' for cr in TCharRg do ;',
  8913. ' for cr in TSetOfCharRg do ;',
  8914. ' for cr in socr do ;',
  8915. '']);
  8916. ConvertProgram;
  8917. CheckSource('TestForCharInDo',
  8918. LinesToStr([ // statements
  8919. 'this.Foo = "foo";',
  8920. 'this.c = "";',
  8921. 'this.c2 = "";',
  8922. 'this.s = "";',
  8923. 'this.a1 = [];',
  8924. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8925. 'this.soc = {};',
  8926. 'this.socr = {};',
  8927. 'this.cr = "a";',
  8928. '']),
  8929. LinesToStr([ // this.$main
  8930. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8931. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8932. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8933. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8934. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8935. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8936. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8937. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8938. 'for (var $l8 in $mod.soc) {',
  8939. ' $mod.c = String.fromCharCode($l8);',
  8940. ' $mod.c2 = $mod.c;',
  8941. '};',
  8942. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8943. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8944. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8945. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8946. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8947. '']));
  8948. end;
  8949. procedure TTestModule.TestProcTwoArgs;
  8950. begin
  8951. StartProgram(false);
  8952. Add('procedure Test(a,b: longint);');
  8953. Add('begin');
  8954. Add('end;');
  8955. Add('begin');
  8956. ConvertProgram;
  8957. CheckSource('TestProcTwoArgs',
  8958. LinesToStr([ // statements
  8959. 'this.Test = function (a,b) {',
  8960. '};'
  8961. ]),
  8962. LinesToStr([ // this.$main
  8963. ''
  8964. ]));
  8965. end;
  8966. procedure TTestModule.TestProc_DefaultValue;
  8967. begin
  8968. StartProgram(false);
  8969. Add('procedure p1(i: longint = 1);');
  8970. Add('begin');
  8971. Add('end;');
  8972. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8973. Add('begin');
  8974. Add('end;');
  8975. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8976. Add('begin');
  8977. Add('end;');
  8978. Add('begin');
  8979. Add(' p1;');
  8980. Add(' p1();');
  8981. Add(' p1(11);');
  8982. Add(' p2;');
  8983. Add(' p2();');
  8984. Add(' p2(12);');
  8985. Add(' p2(13,''b'');');
  8986. Add(' p3();');
  8987. ConvertProgram;
  8988. CheckSource('TestProc_DefaultValue',
  8989. LinesToStr([ // statements
  8990. 'this.p1 = function (i) {',
  8991. '};',
  8992. 'this.p2 = function (i,c) {',
  8993. '};',
  8994. 'this.p3 = function (d,b,s) {',
  8995. '};'
  8996. ]),
  8997. LinesToStr([ // this.$main
  8998. ' $mod.p1(1);',
  8999. ' $mod.p1(1);',
  9000. ' $mod.p1(11);',
  9001. ' $mod.p2(1,"a");',
  9002. ' $mod.p2(1,"a");',
  9003. ' $mod.p2(12,"a");',
  9004. ' $mod.p2(13,"b");',
  9005. ' $mod.p3(1.0,false,"abc");'
  9006. ]));
  9007. end;
  9008. procedure TTestModule.TestFunctionInt;
  9009. begin
  9010. StartProgram(false);
  9011. Add('function MyTest(Bar: longint): longint;');
  9012. Add('begin');
  9013. Add(' Result:=2*bar');
  9014. Add('end;');
  9015. Add('begin');
  9016. ConvertProgram;
  9017. CheckSource('TestFunctionInt',
  9018. LinesToStr([ // statements
  9019. 'this.MyTest = function (Bar) {',
  9020. ' var Result = 0;',
  9021. ' Result = 2*Bar;',
  9022. ' return Result;',
  9023. '};'
  9024. ]),
  9025. LinesToStr([ // this.$main
  9026. ''
  9027. ]));
  9028. end;
  9029. procedure TTestModule.TestFunctionString;
  9030. begin
  9031. StartProgram(false);
  9032. Add('function Test(Bar: string): string;');
  9033. Add('begin');
  9034. Add(' Result:=bar+BAR');
  9035. Add('end;');
  9036. Add('begin');
  9037. ConvertProgram;
  9038. CheckSource('TestFunctionString',
  9039. LinesToStr([ // statements
  9040. 'this.Test = function (Bar) {',
  9041. ' var Result = "";',
  9042. ' Result = Bar+Bar;',
  9043. ' return Result;',
  9044. '};'
  9045. ]),
  9046. LinesToStr([ // this.$main
  9047. ''
  9048. ]));
  9049. end;
  9050. procedure TTestModule.TestIfThen;
  9051. begin
  9052. StartProgram(false);
  9053. Add([
  9054. 'var b: boolean;',
  9055. 'begin',
  9056. ' if b then ;',
  9057. ' if b then else ;']);
  9058. ConvertProgram;
  9059. CheckSource('TestIfThen',
  9060. LinesToStr([ // statements
  9061. 'this.b = false;',
  9062. '']),
  9063. LinesToStr([ // this.$main
  9064. 'if ($mod.b) ;',
  9065. 'if ($mod.b) ;',
  9066. '']));
  9067. end;
  9068. procedure TTestModule.TestForLoop;
  9069. begin
  9070. StartProgram(false);
  9071. Add('var');
  9072. Add(' vI, vJ, vN: longint;');
  9073. Add('begin');
  9074. Add(' VJ:=0;');
  9075. Add(' VN:=3;');
  9076. Add(' for VI:=1 to VN do');
  9077. Add(' begin');
  9078. Add(' VJ:=VJ+VI;');
  9079. Add(' end;');
  9080. ConvertProgram;
  9081. CheckSource('TestForLoop',
  9082. LinesToStr([ // statements
  9083. 'this.vI = 0;',
  9084. 'this.vJ = 0;',
  9085. 'this.vN = 0;'
  9086. ]),
  9087. LinesToStr([ // this.$main
  9088. ' $mod.vJ = 0;',
  9089. ' $mod.vN = 3;',
  9090. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9091. ' $mod.vI = $l;',
  9092. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9093. ' };',
  9094. '']));
  9095. end;
  9096. procedure TTestModule.TestForLoopInsideFunction;
  9097. begin
  9098. StartProgram(false);
  9099. Add('function SumNumbers(Count: longint): longint;');
  9100. Add('var');
  9101. Add(' vI, vJ: longint;');
  9102. Add('begin');
  9103. Add(' vj:=0;');
  9104. Add(' for vi:=1 to count do');
  9105. Add(' begin');
  9106. Add(' vj:=vj+vi;');
  9107. Add(' end;');
  9108. Add('end;');
  9109. Add('begin');
  9110. Add(' sumnumbers(3);');
  9111. ConvertProgram;
  9112. CheckSource('TestForLoopInsideFunction',
  9113. LinesToStr([ // statements
  9114. 'this.SumNumbers = function (Count) {',
  9115. ' var Result = 0;',
  9116. ' var vI = 0;',
  9117. ' var vJ = 0;',
  9118. ' vJ = 0;',
  9119. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9120. ' vI = $l;',
  9121. ' vJ = vJ + vI;',
  9122. ' };',
  9123. ' return Result;',
  9124. '};'
  9125. ]),
  9126. LinesToStr([ // $mod.$main
  9127. ' $mod.SumNumbers(3);'
  9128. ]));
  9129. end;
  9130. procedure TTestModule.TestForLoop_ReadVarAfter;
  9131. begin
  9132. StartProgram(false);
  9133. Add('var');
  9134. Add(' vI: longint;');
  9135. Add('begin');
  9136. Add(' for vi:=1 to 2 do ;');
  9137. Add(' if vi=3 then ;');
  9138. ConvertProgram;
  9139. CheckSource('TestForLoop',
  9140. LinesToStr([ // statements
  9141. 'this.vI = 0;'
  9142. ]),
  9143. LinesToStr([ // this.$main
  9144. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9145. ' if ($mod.vI===3) ;'
  9146. ]));
  9147. end;
  9148. procedure TTestModule.TestForLoop_Nested;
  9149. begin
  9150. StartProgram(false);
  9151. Add('function SumNumbers(Count: longint): longint;');
  9152. Add('var');
  9153. Add(' vI, vJ, vK: longint;');
  9154. Add('begin');
  9155. Add(' VK:=0;');
  9156. Add(' for VI:=1 to count do');
  9157. Add(' begin');
  9158. Add(' for vj:=1 to vi do');
  9159. Add(' begin');
  9160. Add(' vk:=VK+VI;');
  9161. Add(' end;');
  9162. Add(' end;');
  9163. Add('end;');
  9164. Add('begin');
  9165. Add(' sumnumbers(3);');
  9166. ConvertProgram;
  9167. CheckSource('TestForLoopInFunction',
  9168. LinesToStr([ // statements
  9169. 'this.SumNumbers = function (Count) {',
  9170. ' var Result = 0;',
  9171. ' var vI = 0;',
  9172. ' var vJ = 0;',
  9173. ' var vK = 0;',
  9174. ' vK = 0;',
  9175. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9176. ' vI = $l;',
  9177. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9178. ' vJ = $l1;',
  9179. ' vK = vK + vI;',
  9180. ' };',
  9181. ' };',
  9182. ' return Result;',
  9183. '};'
  9184. ]),
  9185. LinesToStr([ // $mod.$main
  9186. ' $mod.SumNumbers(3);'
  9187. ]));
  9188. end;
  9189. procedure TTestModule.TestRepeatUntil;
  9190. begin
  9191. StartProgram(false);
  9192. Add('var');
  9193. Add(' vI, vJ, vN: longint;');
  9194. Add('begin');
  9195. Add(' vn:=3;');
  9196. Add(' vj:=0;');
  9197. Add(' VI:=0;');
  9198. Add(' repeat');
  9199. Add(' VI:=vi+1;');
  9200. Add(' vj:=VJ+vI;');
  9201. Add(' until vi>=vn');
  9202. ConvertProgram;
  9203. CheckSource('TestRepeatUntil',
  9204. LinesToStr([ // statements
  9205. 'this.vI = 0;',
  9206. 'this.vJ = 0;',
  9207. 'this.vN = 0;'
  9208. ]),
  9209. LinesToStr([ // $mod.$main
  9210. ' $mod.vN = 3;',
  9211. ' $mod.vJ = 0;',
  9212. ' $mod.vI = 0;',
  9213. ' do{',
  9214. ' $mod.vI = $mod.vI + 1;',
  9215. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9216. ' }while(!($mod.vI>=$mod.vN));'
  9217. ]));
  9218. end;
  9219. procedure TTestModule.TestAsmBlock;
  9220. begin
  9221. StartProgram(false);
  9222. Add([
  9223. 'var',
  9224. ' vI: longint;',
  9225. 'begin',
  9226. ' vi:=1;',
  9227. ' asm',
  9228. ' if (vI===1) {',
  9229. ' vI=2;',
  9230. //' console.log(''end;'');', ToDo
  9231. ' }',
  9232. ' if (vI===2){ vI=3; }',
  9233. ' end;',
  9234. ' VI:=4;']);
  9235. ConvertProgram;
  9236. CheckSource('TestAsmBlock',
  9237. LinesToStr([ // statements
  9238. 'this.vI = 0;'
  9239. ]),
  9240. LinesToStr([ // $mod.$main
  9241. '$mod.vI = 1;',
  9242. 'if (vI===1) {',
  9243. ' vI=2;',
  9244. '}',
  9245. 'if (vI===2){ vI=3; }',
  9246. ';',
  9247. '$mod.vI = 4;'
  9248. ]));
  9249. end;
  9250. procedure TTestModule.TestAsmPas_Impl;
  9251. begin
  9252. StartUnit(false);
  9253. Add('interface');
  9254. Add('const cIntf: longint = 1;');
  9255. Add('var vIntf: longint;');
  9256. Add('implementation');
  9257. Add('const cImpl: longint = 2;');
  9258. Add('var vImpl: longint;');
  9259. Add('procedure DoIt;');
  9260. Add('const cLoc: longint = 3;');
  9261. Add('var vLoc: longint;');
  9262. Add('begin;');
  9263. Add(' asm');
  9264. //Add(' pas(vIntf)=pas(cIntf);');
  9265. //Add(' pas(vImpl)=pas(cImpl);');
  9266. //Add(' pas(vLoc)=pas(cLoc);');
  9267. Add(' end;');
  9268. Add('end;');
  9269. ConvertUnit;
  9270. CheckSource('TestAsmPas_Impl',
  9271. LinesToStr([
  9272. 'var $impl = $mod.$impl;',
  9273. 'this.cIntf = 1;',
  9274. 'this.vIntf = 0;',
  9275. '']),
  9276. '', // this.$init
  9277. LinesToStr([ // implementation
  9278. '$impl.cImpl = 2;',
  9279. '$impl.vImpl = 0;',
  9280. 'var cLoc = 3;',
  9281. '$impl.DoIt = function () {',
  9282. ' var vLoc = 0;',
  9283. '};',
  9284. '']) );
  9285. end;
  9286. procedure TTestModule.TestTryFinally;
  9287. begin
  9288. StartProgram(false);
  9289. Add('var i: longint;');
  9290. Add('begin');
  9291. Add(' try');
  9292. Add(' i:=0; i:=2 div i;');
  9293. Add(' finally');
  9294. Add(' i:=3');
  9295. Add(' end;');
  9296. ConvertProgram;
  9297. CheckSource('TestTryFinally',
  9298. LinesToStr([ // statements
  9299. 'this.i = 0;'
  9300. ]),
  9301. LinesToStr([ // $mod.$main
  9302. 'try {',
  9303. ' $mod.i = 0;',
  9304. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9305. '} finally {',
  9306. ' $mod.i = 3;',
  9307. '};'
  9308. ]));
  9309. end;
  9310. procedure TTestModule.TestTryExcept;
  9311. begin
  9312. StartProgram(false);
  9313. Add([
  9314. 'type',
  9315. ' TObject = class end;',
  9316. ' Exception = class Msg: string; end;',
  9317. ' EInvalidCast = class(Exception) end;',
  9318. 'var vI: longint;',
  9319. 'begin',
  9320. ' try',
  9321. ' vi:=1;',
  9322. ' except',
  9323. ' vi:=2',
  9324. ' end;',
  9325. ' try',
  9326. ' vi:=3;',
  9327. ' except',
  9328. ' raise;',
  9329. ' end;',
  9330. ' try',
  9331. ' VI:=4;',
  9332. ' except',
  9333. ' on einvalidcast do',
  9334. ' raise;',
  9335. ' on E: exception do',
  9336. ' if e.msg='''' then',
  9337. ' raise e;',
  9338. ' else',
  9339. ' vi:=5',
  9340. ' end;',
  9341. ' try',
  9342. ' VI:=6;',
  9343. ' except',
  9344. ' on einvalidcast do ;',
  9345. ' end;',
  9346. '']);
  9347. ConvertProgram;
  9348. CheckSource('TestTryExcept',
  9349. LinesToStr([ // statements
  9350. 'rtl.createClass(this, "TObject", null, function () {',
  9351. ' this.$init = function () {',
  9352. ' };',
  9353. ' this.$final = function () {',
  9354. ' };',
  9355. '});',
  9356. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9357. ' this.$init = function () {',
  9358. ' $mod.TObject.$init.call(this);',
  9359. ' this.Msg = "";',
  9360. ' };',
  9361. '});',
  9362. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9363. '});',
  9364. 'this.vI = 0;'
  9365. ]),
  9366. LinesToStr([ // $mod.$main
  9367. 'try {',
  9368. ' $mod.vI = 1;',
  9369. '} catch ($e) {',
  9370. ' $mod.vI = 2;',
  9371. '};',
  9372. 'try {',
  9373. ' $mod.vI = 3;',
  9374. '} catch ($e) {',
  9375. ' throw $e;',
  9376. '};',
  9377. 'try {',
  9378. ' $mod.vI = 4;',
  9379. '} catch ($e) {',
  9380. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9381. ' throw $e',
  9382. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9383. ' var E = $e;',
  9384. ' if (E.Msg === "") throw E;',
  9385. ' } else {',
  9386. ' $mod.vI = 5;',
  9387. ' }',
  9388. '};',
  9389. 'try {',
  9390. ' $mod.vI = 6;',
  9391. '} catch ($e) {',
  9392. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9393. ' } else throw $e',
  9394. '};',
  9395. '']));
  9396. end;
  9397. procedure TTestModule.TestTryExcept_ReservedWords;
  9398. begin
  9399. StartProgram(false);
  9400. Add([
  9401. 'type',
  9402. ' TObject = class end;',
  9403. ' Exception = class',
  9404. ' Symbol: string;',
  9405. ' end;',
  9406. 'var &try: longint;',
  9407. 'begin',
  9408. ' try',
  9409. ' &try:=4;',
  9410. ' except',
  9411. ' on Error: exception do',
  9412. ' if errOR.symBol='''' then',
  9413. ' raise ERRor;',
  9414. ' end;',
  9415. '']);
  9416. ConvertProgram;
  9417. CheckSource('TestTryExcept_ReservedWords',
  9418. LinesToStr([ // statements
  9419. 'rtl.createClass(this, "TObject", null, function () {',
  9420. ' this.$init = function () {',
  9421. ' };',
  9422. ' this.$final = function () {',
  9423. ' };',
  9424. '});',
  9425. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9426. ' this.$init = function () {',
  9427. ' $mod.TObject.$init.call(this);',
  9428. ' this.Symbol = "";',
  9429. ' };',
  9430. '});',
  9431. 'this.Try = 0;',
  9432. '']),
  9433. LinesToStr([ // $mod.$main
  9434. 'try {',
  9435. ' $mod.Try = 4;',
  9436. '} catch ($e) {',
  9437. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9438. ' var error = $e;',
  9439. ' if (error.Symbol === "") throw error;',
  9440. ' } else throw $e',
  9441. '};',
  9442. '']));
  9443. end;
  9444. procedure TTestModule.TestIfThenRaiseElse;
  9445. begin
  9446. StartProgram(false);
  9447. Add([
  9448. 'type',
  9449. ' TObject = class',
  9450. ' constructor Create;',
  9451. ' end;',
  9452. 'constructor TObject.Create;',
  9453. 'begin',
  9454. 'end;',
  9455. 'var b: boolean;',
  9456. 'begin',
  9457. ' if b then',
  9458. ' raise TObject.Create',
  9459. ' else',
  9460. ' b:=false;',
  9461. '']);
  9462. ConvertProgram;
  9463. CheckSource('TestIfThenRaiseElse',
  9464. LinesToStr([ // statements
  9465. 'rtl.createClass(this, "TObject", null, function () {',
  9466. ' this.$init = function () {',
  9467. ' };',
  9468. ' this.$final = function () {',
  9469. ' };',
  9470. ' this.Create = function () {',
  9471. ' return this;',
  9472. ' };',
  9473. '});',
  9474. 'this.b = false;',
  9475. '']),
  9476. LinesToStr([ // $mod.$main
  9477. 'if ($mod.b) {',
  9478. ' throw $mod.TObject.$create("Create")}',
  9479. ' else $mod.b = false;',
  9480. '']));
  9481. end;
  9482. procedure TTestModule.TestCaseOf;
  9483. begin
  9484. StartProgram(false);
  9485. Add([
  9486. 'const e: longint; external name ''$e'';',
  9487. 'var vI: longint;',
  9488. 'begin',
  9489. ' case vi of',
  9490. ' 1: ;',
  9491. ' 2: vi:=3;',
  9492. ' e: ;',
  9493. ' else',
  9494. ' VI:=4',
  9495. ' end;']);
  9496. ConvertProgram;
  9497. CheckSource('TestCaseOf',
  9498. LinesToStr([ // statements
  9499. 'this.vI = 0;'
  9500. ]),
  9501. LinesToStr([ // $mod.$main
  9502. 'var $tmp = $mod.vI;',
  9503. 'if ($tmp === 1) {}',
  9504. 'else if ($tmp === 2) {',
  9505. ' $mod.vI = 3}',
  9506. ' else if ($tmp === $e) {}',
  9507. 'else {',
  9508. ' $mod.vI = 4;',
  9509. '};'
  9510. ]));
  9511. end;
  9512. procedure TTestModule.TestCaseOf_UseSwitch;
  9513. begin
  9514. StartProgram(false);
  9515. Converter.UseSwitchStatement:=true;
  9516. Add('var Vi: longint;');
  9517. Add('begin');
  9518. Add(' case vi of');
  9519. Add(' 1: ;');
  9520. Add(' 2: VI:=3;');
  9521. Add(' else');
  9522. Add(' vi:=4');
  9523. Add(' end;');
  9524. ConvertProgram;
  9525. CheckSource('TestCaseOf_UseSwitch',
  9526. LinesToStr([ // statements
  9527. 'this.Vi = 0;'
  9528. ]),
  9529. LinesToStr([ // $mod.$main
  9530. 'switch ($mod.Vi) {',
  9531. 'case 1:',
  9532. ' break;',
  9533. 'case 2:',
  9534. ' $mod.Vi = 3;',
  9535. ' break;',
  9536. 'default:',
  9537. ' $mod.Vi = 4;',
  9538. '};'
  9539. ]));
  9540. end;
  9541. procedure TTestModule.TestCaseOfNoElse;
  9542. begin
  9543. StartProgram(false);
  9544. Add('var Vi: longint;');
  9545. Add('begin');
  9546. Add(' case vi of');
  9547. Add(' 1: begin vi:=2; VI:=3; end;');
  9548. Add(' end;');
  9549. ConvertProgram;
  9550. CheckSource('TestCaseOfNoElse',
  9551. LinesToStr([ // statements
  9552. 'this.Vi = 0;'
  9553. ]),
  9554. LinesToStr([ // $mod.$main
  9555. 'var $tmp = $mod.Vi;',
  9556. 'if ($tmp === 1) {',
  9557. ' $mod.Vi = 2;',
  9558. ' $mod.Vi = 3;',
  9559. '};'
  9560. ]));
  9561. end;
  9562. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9563. begin
  9564. StartProgram(false);
  9565. Converter.UseSwitchStatement:=true;
  9566. Add('var vI: longint;');
  9567. Add('begin');
  9568. Add(' case vi of');
  9569. Add(' 1: begin VI:=2; vi:=3; end;');
  9570. Add(' end;');
  9571. ConvertProgram;
  9572. CheckSource('TestCaseOfNoElse_UseSwitch',
  9573. LinesToStr([ // statements
  9574. 'this.vI = 0;'
  9575. ]),
  9576. LinesToStr([ // $mod.$main
  9577. 'switch ($mod.vI) {',
  9578. 'case 1:',
  9579. ' $mod.vI = 2;',
  9580. ' $mod.vI = 3;',
  9581. ' break;',
  9582. '};'
  9583. ]));
  9584. end;
  9585. procedure TTestModule.TestCaseOfRange;
  9586. begin
  9587. StartProgram(false);
  9588. Add('var vI: longint;');
  9589. Add('begin');
  9590. Add(' case vi of');
  9591. Add(' 1..3: vi:=14;');
  9592. Add(' 4,5: vi:=16;');
  9593. Add(' 6..7,9..10: ;');
  9594. Add(' else ;');
  9595. Add(' end;');
  9596. ConvertProgram;
  9597. CheckSource('TestCaseOfRange',
  9598. LinesToStr([ // statements
  9599. 'this.vI = 0;'
  9600. ]),
  9601. LinesToStr([ // $mod.$main
  9602. 'var $tmp = $mod.vI;',
  9603. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9604. ' $mod.vI = 14',
  9605. '} else if (($tmp === 4) || ($tmp === 5)){',
  9606. ' $mod.vI = 16',
  9607. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9608. ]));
  9609. end;
  9610. procedure TTestModule.TestCaseOfString;
  9611. begin
  9612. StartProgram(false);
  9613. Add([
  9614. 'var s,h: string;',
  9615. 'begin',
  9616. ' case s of',
  9617. ' ''foo'': s:=h;',
  9618. ' ''a''..''z'': h:=s;',
  9619. ' ''ў'', ''ё'': ;',
  9620. ' ''Б''..''Я'': ;',
  9621. ' end;',
  9622. '']);
  9623. ConvertProgram;
  9624. CheckSource('TestCaseOfString',
  9625. LinesToStr([ // statements
  9626. 'this.s = "";',
  9627. 'this.h = "";',
  9628. '']),
  9629. LinesToStr([ // $mod.$main
  9630. 'var $tmp = $mod.s;',
  9631. 'if ($tmp === "foo") {',
  9632. ' $mod.s = $mod.h}',
  9633. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9634. ' $mod.h = $mod.s}',
  9635. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9636. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9637. '']));
  9638. end;
  9639. procedure TTestModule.TestCaseOfChar;
  9640. begin
  9641. StartProgram(false);
  9642. Add([
  9643. 'var s,h: char;',
  9644. 'begin',
  9645. ' case s of',
  9646. ' ''a''..''z'': h:=s;',
  9647. ' ''ä'': ;',
  9648. ' ''ў'', ''ё'': ;',
  9649. ' ''Б''..''Я'': ;',
  9650. ' end;',
  9651. '']);
  9652. ConvertProgram;
  9653. CheckSource('TestCaseOfString',
  9654. LinesToStr([ // statements
  9655. 'this.s = "";',
  9656. 'this.h = "";',
  9657. '']),
  9658. LinesToStr([ // $mod.$main
  9659. 'var $tmp = $mod.s;',
  9660. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9661. ' $mod.h = $mod.s}',
  9662. ' else if ($tmp === "ä") {}',
  9663. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9664. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9665. '']));
  9666. end;
  9667. procedure TTestModule.TestCaseOfExternalClassConst;
  9668. begin
  9669. StartProgram(false);
  9670. Add([
  9671. '{$modeswitch externalclass}',
  9672. 'type',
  9673. ' TBird = class external name ''Bird''',
  9674. ' const e: longint;',
  9675. ' end;',
  9676. 'var vI: longint;',
  9677. 'begin',
  9678. ' case vi of',
  9679. ' 1: vi:=3;',
  9680. ' TBird.e: ;',
  9681. ' end;']);
  9682. ConvertProgram;
  9683. CheckSource('TestCaseOfExternalClassConst',
  9684. LinesToStr([ // statements
  9685. 'this.vI = 0;'
  9686. ]),
  9687. LinesToStr([ // $mod.$main
  9688. 'var $tmp = $mod.vI;',
  9689. 'if ($tmp === 1) {',
  9690. ' $mod.vI = 3}',
  9691. ' else if ($tmp === Bird.e) ;'
  9692. ]));
  9693. end;
  9694. procedure TTestModule.TestDebugger;
  9695. begin
  9696. StartProgram(false);
  9697. Add([
  9698. 'procedure DoIt;',
  9699. 'begin',
  9700. ' deBugger;',
  9701. ' DeBugger();',
  9702. 'end;',
  9703. 'begin',
  9704. ' Debugger;']);
  9705. ConvertProgram;
  9706. CheckSource('TestDebugger',
  9707. LinesToStr([ // statements
  9708. 'this.DoIt = function () {',
  9709. ' debugger;',
  9710. ' debugger;',
  9711. '};',
  9712. '']),
  9713. LinesToStr([ // $mod.$main
  9714. 'debugger;',
  9715. '']));
  9716. end;
  9717. procedure TTestModule.TestArray_Dynamic;
  9718. begin
  9719. StartProgram(false);
  9720. Add([
  9721. 'type',
  9722. ' TArrayInt = array of longint;',
  9723. 'var',
  9724. ' Arr: TArrayInt;',
  9725. ' i: longint;',
  9726. ' b: boolean;',
  9727. 'begin',
  9728. ' SetLength(arr,3);',
  9729. ' arr[0]:=4;',
  9730. ' arr[1]:=length(arr)+arr[0];',
  9731. ' arr[i]:=5;',
  9732. ' arr[arr[i]]:=arr[6];',
  9733. ' i:=low(arr);',
  9734. ' i:=high(arr);',
  9735. ' b:=Assigned(arr);',
  9736. ' Arr:=default(TArrayInt);']);
  9737. ConvertProgram;
  9738. CheckSource('TestArray_Dynamic',
  9739. LinesToStr([ // statements
  9740. 'this.Arr = [];',
  9741. 'this.i = 0;',
  9742. 'this.b = false;'
  9743. ]),
  9744. LinesToStr([ // $mod.$main
  9745. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9746. '$mod.Arr[0] = 4;',
  9747. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9748. '$mod.Arr[$mod.i] = 5;',
  9749. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9750. '$mod.i = 0;',
  9751. '$mod.i = rtl.length($mod.Arr) - 1;',
  9752. '$mod.b = rtl.length($mod.Arr) > 0;',
  9753. '$mod.Arr = [];',
  9754. '']));
  9755. end;
  9756. procedure TTestModule.TestArray_Dynamic_Nil;
  9757. begin
  9758. StartProgram(false);
  9759. Add('type');
  9760. Add(' TArrayInt = array of longint;');
  9761. Add('var');
  9762. Add(' Arr: TArrayInt;');
  9763. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9764. Add('begin');
  9765. Add(' arr:=nil;');
  9766. Add(' if arr=nil then;');
  9767. Add(' if nil=arr then;');
  9768. Add(' if arr<>nil then;');
  9769. Add(' if nil<>arr then;');
  9770. Add(' DoIt(nil,nil);');
  9771. ConvertProgram;
  9772. CheckSource('TestArray_Dynamic',
  9773. LinesToStr([ // statements
  9774. 'this.Arr = [];',
  9775. 'this.DoIt = function(i,j){',
  9776. '};'
  9777. ]),
  9778. LinesToStr([ // $mod.$main
  9779. '$mod.Arr = [];',
  9780. 'if (rtl.length($mod.Arr) === 0) ;',
  9781. 'if (rtl.length($mod.Arr) === 0) ;',
  9782. 'if (rtl.length($mod.Arr) > 0) ;',
  9783. 'if (rtl.length($mod.Arr) > 0) ;',
  9784. '$mod.DoIt([],[]);',
  9785. '']));
  9786. end;
  9787. procedure TTestModule.TestArray_DynMultiDimensional;
  9788. begin
  9789. StartProgram(false);
  9790. Add([
  9791. 'type',
  9792. ' TArrayInt = array of longint;',
  9793. ' TArrayArrayInt = array of TArrayInt;',
  9794. 'var',
  9795. ' Arr: TArrayInt;',
  9796. ' Arr2: TArrayArrayInt;',
  9797. ' i: longint;',
  9798. 'begin',
  9799. ' arr2:=nil;',
  9800. ' if arr2=nil then;',
  9801. ' if nil=arr2 then;',
  9802. ' i:=low(arr2);',
  9803. ' i:=low(arr2[1]);',
  9804. ' i:=high(arr2);',
  9805. ' i:=high(arr2[2]);',
  9806. ' arr2[3]:=arr;',
  9807. ' arr2[4][5]:=i;',
  9808. ' i:=arr2[6][7];',
  9809. ' arr2[8,9]:=i;',
  9810. ' i:=arr2[10,11];',
  9811. ' SetLength(arr2,14);',
  9812. ' SetLength(arr2[15],16);']);
  9813. ConvertProgram;
  9814. CheckSource('TestArray_Dynamic',
  9815. LinesToStr([ // statements
  9816. 'this.Arr = [];',
  9817. 'this.Arr2 = [];',
  9818. 'this.i = 0;'
  9819. ]),
  9820. LinesToStr([ // $mod.$main
  9821. '$mod.Arr2 = [];',
  9822. 'if (rtl.length($mod.Arr2) === 0) ;',
  9823. 'if (rtl.length($mod.Arr2) === 0) ;',
  9824. '$mod.i = 0;',
  9825. '$mod.i = 0;',
  9826. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9827. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9828. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9829. '$mod.Arr2[4][5] = $mod.i;',
  9830. '$mod.i = $mod.Arr2[6][7];',
  9831. '$mod.Arr2[8][9] = $mod.i;',
  9832. '$mod.i = $mod.Arr2[10][11];',
  9833. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9834. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9835. '']));
  9836. end;
  9837. procedure TTestModule.TestArray_DynamicAssign;
  9838. begin
  9839. StartProgram(false);
  9840. Add([
  9841. 'type',
  9842. ' TArrayInt = array of longint;',
  9843. ' TArrayArrayInt = array of TArrayInt;',
  9844. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9845. 'begin',
  9846. 'end;',
  9847. 'procedure Fly(var a: TArrayInt);',
  9848. 'begin',
  9849. 'end;',
  9850. 'var',
  9851. ' Arr: TArrayInt;',
  9852. ' Arr2: TArrayArrayInt;',
  9853. 'begin',
  9854. ' arr:=nil;',
  9855. ' arr2:=nil;',
  9856. ' arr2[1]:=nil;',
  9857. ' arr2[2]:=arr;',
  9858. ' Run(arr,arr,arr);',
  9859. ' Fly(arr);',
  9860. ' Run(arr2[4],arr2[5],arr2[6]);',
  9861. ' Fly(arr2[7]);',
  9862. '']);
  9863. ConvertProgram;
  9864. CheckSource('TestArray_DynamicAssign',
  9865. LinesToStr([ // statements
  9866. 'this.Run = function (a, b, c) {',
  9867. '};',
  9868. 'this.Fly = function (a) {',
  9869. '};',
  9870. 'this.Arr = [];',
  9871. 'this.Arr2 = [];',
  9872. '']),
  9873. LinesToStr([ // $mod.$main
  9874. '$mod.Arr = [];',
  9875. '$mod.Arr2 = [];',
  9876. '$mod.Arr2[1] = [];',
  9877. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9878. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9879. '$mod.Fly({',
  9880. ' p: $mod,',
  9881. ' get: function () {',
  9882. ' return this.p.Arr;',
  9883. ' },',
  9884. ' set: function (v) {',
  9885. ' this.p.Arr = v;',
  9886. ' }',
  9887. '});',
  9888. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9889. '$mod.Fly({',
  9890. ' a: 7,',
  9891. ' p: $mod.Arr2,',
  9892. ' get: function () {',
  9893. ' return this.p[this.a];',
  9894. ' },',
  9895. ' set: function (v) {',
  9896. ' this.p[this.a] = v;',
  9897. ' }',
  9898. '});',
  9899. '']));
  9900. end;
  9901. procedure TTestModule.TestArray_StaticInt;
  9902. begin
  9903. StartProgram(false);
  9904. Add('type');
  9905. Add(' TArrayInt = array[2..4] of longint;');
  9906. Add('var');
  9907. Add(' Arr: TArrayInt;');
  9908. Add(' Arr2: TArrayInt = (5,6,7);');
  9909. Add(' i: longint;');
  9910. Add(' b: boolean;');
  9911. Add('begin');
  9912. Add(' arr[2]:=4;');
  9913. Add(' arr[3]:=arr[2]+arr[3];');
  9914. Add(' arr[i]:=5;');
  9915. Add(' arr[arr[i]]:=arr[high(arr)];');
  9916. Add(' i:=low(arr);');
  9917. Add(' i:=high(arr);');
  9918. Add(' b:=arr[2]=arr[3];');
  9919. Add(' arr:=default(TArrayInt);');
  9920. ConvertProgram;
  9921. CheckSource('TestArray_StaticInt',
  9922. LinesToStr([ // statements
  9923. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9924. 'this.Arr2 = [5, 6, 7];',
  9925. 'this.i = 0;',
  9926. 'this.b = false;'
  9927. ]),
  9928. LinesToStr([ // $mod.$main
  9929. '$mod.Arr[0] = 4;',
  9930. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9931. '$mod.Arr[$mod.i-2] = 5;',
  9932. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9933. '$mod.i = 2;',
  9934. '$mod.i = 4;',
  9935. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9936. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9937. '']));
  9938. end;
  9939. procedure TTestModule.TestArray_StaticBool;
  9940. begin
  9941. StartProgram(false);
  9942. Add('type');
  9943. Add(' TBools = array[boolean] of boolean;');
  9944. Add(' TBool2 = array[true..true] of boolean;');
  9945. Add('var');
  9946. Add(' Arr: TBools;');
  9947. Add(' Arr2: TBool2;');
  9948. Add(' Arr3: TBools = (true,false);');
  9949. Add(' b: boolean;');
  9950. Add('begin');
  9951. Add(' b:=low(arr);');
  9952. Add(' b:=high(arr);');
  9953. Add(' arr[true]:=false;');
  9954. Add(' arr[false]:=arr[b] or arr[true];');
  9955. Add(' arr[b]:=true;');
  9956. Add(' arr[arr[b]]:=arr[high(arr)];');
  9957. Add(' b:=arr[false]=arr[true];');
  9958. Add(' b:=low(arr2);');
  9959. Add(' b:=high(arr2);');
  9960. Add(' arr2[true]:=true;');
  9961. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9962. Add(' arr2[b]:=false;');
  9963. ConvertProgram;
  9964. CheckSource('TestArray_StaticBool',
  9965. LinesToStr([ // statements
  9966. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9967. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9968. 'this.Arr3 = [true, false];',
  9969. 'this.b = false;'
  9970. ]),
  9971. LinesToStr([ // $mod.$main
  9972. '$mod.b = false;',
  9973. '$mod.b = true;',
  9974. '$mod.Arr[1] = false;',
  9975. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9976. '$mod.Arr[+$mod.b] = true;',
  9977. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9978. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9979. '$mod.b = true;',
  9980. '$mod.b = true;',
  9981. '$mod.Arr2[0] = true;',
  9982. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9983. '$mod.Arr2[1-$mod.b] = false;',
  9984. '']));
  9985. end;
  9986. procedure TTestModule.TestArray_StaticChar;
  9987. begin
  9988. StartProgram(false);
  9989. Add([
  9990. 'type',
  9991. ' TChars = array[char] of char;',
  9992. ' TChars2 = array[''a''..''z''] of char;',
  9993. 'var',
  9994. ' Arr: TChars;',
  9995. ' Arr2: TChars2;',
  9996. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9997. ' Arr4: array[11..13] of char = ''pas'';',
  9998. ' Arr5: array[21..22] of char = ''äö'';',
  9999. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  10000. ' c: char;',
  10001. ' b: boolean;',
  10002. 'begin',
  10003. ' c:=low(arr);',
  10004. ' c:=high(arr);',
  10005. ' arr[''B'']:=''a'';',
  10006. ' arr[''D'']:=arr[c];',
  10007. ' arr[c]:=arr[''d''];',
  10008. ' arr[arr[c]]:=arr[high(arr)];',
  10009. ' b:=arr[low(arr)]=arr[''e''];',
  10010. ' c:=low(arr2);',
  10011. ' c:=high(arr2);',
  10012. ' arr2[''b'']:=''f'';',
  10013. ' arr2[''a'']:=arr2[c];',
  10014. ' arr2[c]:=arr2[''g''];']);
  10015. ConvertProgram;
  10016. CheckSource('TestArray_StaticChar',
  10017. LinesToStr([ // statements
  10018. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  10019. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  10020. 'this.Arr3 = ["p", "a", "s"];',
  10021. 'this.Arr4 = ["p", "a", "s"];',
  10022. 'this.Arr5 = ["ä", "ö"];',
  10023. 'this.Arr6 = ["ä", "ö"];',
  10024. 'this.c = "";',
  10025. 'this.b = false;',
  10026. '']),
  10027. LinesToStr([ // $mod.$main
  10028. '$mod.c = "\x00";',
  10029. '$mod.c = "\uFFFF";',
  10030. '$mod.Arr[66] = "a";',
  10031. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10032. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10033. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10034. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10035. '$mod.c = "a";',
  10036. '$mod.c = "z";',
  10037. '$mod.Arr2[1] = "f";',
  10038. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10039. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10040. '']));
  10041. end;
  10042. procedure TTestModule.TestArray_StaticMultiDim;
  10043. begin
  10044. StartProgram(false);
  10045. Add([
  10046. 'type',
  10047. ' TArrayInt = array[1..3] of longint;',
  10048. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10049. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10050. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10051. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10052. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10053. 'var',
  10054. ' Arr: TArrayInt;',
  10055. ' Arr2: TArrayArrayInt;',
  10056. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10057. ' Arr4: TArrayArrayInt;',
  10058. ' ArrDim2: TArrayDim2Int;',
  10059. ' ArrDim3: TArrayDim3Int;',
  10060. ' ArrDim4: TArrayDim4Int;',
  10061. ' i: longint;',
  10062. 'begin',
  10063. ' i:=low(arr);',
  10064. ' i:=low(arr2);',
  10065. ' i:=low(arr2[5]);',
  10066. ' i:=high(arr);',
  10067. ' i:=high(arr2);',
  10068. ' i:=high(arr2[6]);',
  10069. ' arr2[5]:=arr;',
  10070. ' arr2[6][2]:=i;',
  10071. ' i:=arr2[6][3];',
  10072. ' arr2[6,3]:=i;',
  10073. ' i:=arr2[5,2];',
  10074. ' arr2:=arr2;',// clone multi dim static array
  10075. ' arr3:=arr3;',// clone anonymous multi dim static array
  10076. ' arr4:=arr4;',
  10077. ' Arr:=Arr;',
  10078. ' ArrDim2:=ArrDim2;',
  10079. ' ArrDim3:=ArrDim3;',
  10080. ' ArrDim4:=ArrDim4;',
  10081. '']);
  10082. ConvertProgram;
  10083. CheckSource('TestArray_StaticMultiDim',
  10084. LinesToStr([ // statements
  10085. 'this.TArrayArrayInt$clone = function (a) {',
  10086. ' var b = [];',
  10087. ' b.length = 2;',
  10088. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10089. ' return b;',
  10090. '};',
  10091. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10092. ' var b = [];',
  10093. ' b.length = 2;',
  10094. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10095. ' return b;',
  10096. '};',
  10097. 'this.TArrayDim2Int$clone = function (a) {',
  10098. ' var b = [];',
  10099. ' b.length = 2;',
  10100. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10101. ' return b;',
  10102. '};',
  10103. 'this.TArrayDim3Int$clone = function (a) {',
  10104. ' var b = [];',
  10105. ' b.length = 2;',
  10106. ' for (var c = 0; c < 2; c++) {',
  10107. ' var d = b[c] = [];',
  10108. ' d.length = 3;',
  10109. ' var e = a[c];',
  10110. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10111. ' };',
  10112. ' return b;',
  10113. '};',
  10114. 'this.TArrayDim4Int$clone = function (a) {',
  10115. ' var b = [];',
  10116. ' b.length = 2;',
  10117. ' for (var c = 0; c < 2; c++) {',
  10118. ' var d = b[c] = [];',
  10119. ' d.length = 3;',
  10120. ' var e = a[c];',
  10121. ' for (var f = 0; f < 3; f++) {',
  10122. ' var g = d[f] = [];',
  10123. ' g.length = 4;',
  10124. ' var h = e[f];',
  10125. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10126. ' };',
  10127. ' };',
  10128. ' return b;',
  10129. '};',
  10130. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10131. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10132. 'this.Arr3$a$clone = function (a) {',
  10133. ' var b = [];',
  10134. ' b.length = 2;',
  10135. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10136. ' return b;',
  10137. '};',
  10138. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10139. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10140. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10141. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10142. 'this.ArrDim4 = rtl.arraySetLength(',
  10143. ' null,',
  10144. ' 0,',
  10145. ' 2,',
  10146. ' 3,',
  10147. ' 4,',
  10148. ' 5',
  10149. ');',
  10150. 'this.i = 0;'
  10151. ]),
  10152. LinesToStr([ // $mod.$main
  10153. '$mod.i = 1;',
  10154. '$mod.i = 5;',
  10155. '$mod.i = 1;',
  10156. '$mod.i = 3;',
  10157. '$mod.i = 6;',
  10158. '$mod.i = 3;',
  10159. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10160. '$mod.Arr2[1][1] = $mod.i;',
  10161. '$mod.i = $mod.Arr2[1][2];',
  10162. '$mod.Arr2[1][2] = $mod.i;',
  10163. '$mod.i = $mod.Arr2[0][1];',
  10164. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10165. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10166. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10167. '$mod.Arr = $mod.Arr.slice(0);',
  10168. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10169. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10170. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10171. '']));
  10172. end;
  10173. procedure TTestModule.TestArray_StaticInFunction;
  10174. begin
  10175. StartProgram(false);
  10176. Add([
  10177. 'const TArrayInt = 3;',
  10178. 'const TArrayArrayInt = 4;',
  10179. 'procedure DoIt;',
  10180. 'type',
  10181. ' TArrayInt = array[1..3] of longint;',
  10182. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10183. 'var',
  10184. ' Arr: TArrayInt;',
  10185. ' Arr2: TArrayArrayInt;',
  10186. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10187. ' i: longint;',
  10188. 'begin',
  10189. ' arr2[5]:=arr;',
  10190. ' arr2:=arr2;',// clone multi dim static array
  10191. ' arr3:=arr3;',// clone multi dim anonymous static array
  10192. 'end;',
  10193. 'begin',
  10194. '']);
  10195. ConvertProgram;
  10196. CheckSource('TestArray_StaticInFunction',
  10197. LinesToStr([ // statements
  10198. 'this.TArrayInt = 3;',
  10199. 'this.TArrayArrayInt = 4;',
  10200. 'var TArrayArrayInt$1$clone = function (a) {',
  10201. ' var b = [];',
  10202. ' b.length = 2;',
  10203. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10204. ' return b;',
  10205. '};',
  10206. 'var Arr3$a$clone = function (a) {',
  10207. ' var b = [];',
  10208. ' b.length = 2;',
  10209. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10210. ' return b;',
  10211. '};',
  10212. 'this.DoIt = function () {',
  10213. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10214. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10215. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10216. ' var i = 0;',
  10217. ' Arr2[0] = Arr.slice(0);',
  10218. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10219. ' Arr3 = Arr3$a$clone(Arr3);',
  10220. '};',
  10221. '']),
  10222. LinesToStr([ // $mod.$main
  10223. '']));
  10224. end;
  10225. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10226. begin
  10227. StartProgram(false);
  10228. Add([
  10229. 'type',
  10230. ' TArrayInt = array[1..3,1..2] of longint;',
  10231. 'var',
  10232. ' a,b: TArrayInt;',
  10233. 'begin',
  10234. ' if a=b then ;',
  10235. '']);
  10236. SetExpectedPasResolverError('compare static array is not supported',
  10237. nXIsNotSupported);
  10238. ConvertProgram;
  10239. end;
  10240. procedure TTestModule.TestArrayOfRecord;
  10241. begin
  10242. StartProgram(false);
  10243. Add([
  10244. 'type',
  10245. ' TRec = record',
  10246. ' Int: longint;',
  10247. ' end;',
  10248. ' TArrayRec = array of TRec;',
  10249. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10250. 'begin',
  10251. 'end;',
  10252. 'var',
  10253. ' Arr: TArrayRec;',
  10254. ' r: TRec;',
  10255. ' i: longint;',
  10256. 'begin',
  10257. ' SetLength(arr,3);',
  10258. ' arr[0].int:=4;',
  10259. ' arr[1].int:=length(arr)+arr[2].int;',
  10260. ' arr[arr[i].int].int:=arr[5].int;',
  10261. ' arr[7]:=r;',
  10262. ' r:=arr[8];',
  10263. ' i:=low(arr);',
  10264. ' i:=high(arr);',
  10265. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10266. ConvertProgram;
  10267. CheckSource('TestArrayOfRecord',
  10268. LinesToStr([ // statements
  10269. 'rtl.recNewT(this, "TRec", function () {',
  10270. ' this.Int = 0;',
  10271. ' this.$eq = function (b) {',
  10272. ' return this.Int === b.Int;',
  10273. ' };',
  10274. ' this.$assign = function (s) {',
  10275. ' this.Int = s.Int;',
  10276. ' return this;',
  10277. ' };',
  10278. '});',
  10279. 'this.DoIt = function (vd, vc, vv) {',
  10280. '};',
  10281. 'this.Arr = [];',
  10282. 'this.r = this.TRec.$new();',
  10283. 'this.i = 0;'
  10284. ]),
  10285. LinesToStr([ // $mod.$main
  10286. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10287. '$mod.Arr[0].Int = 4;',
  10288. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10289. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10290. '$mod.Arr[7].$assign($mod.r);',
  10291. '$mod.r.$assign($mod.Arr[8]);',
  10292. '$mod.i = 0;',
  10293. '$mod.i = rtl.length($mod.Arr)-1;',
  10294. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10295. '']));
  10296. end;
  10297. procedure TTestModule.TestArray_StaticRecord;
  10298. begin
  10299. StartProgram(false);
  10300. Add([
  10301. 'type',
  10302. ' TRec = record',
  10303. ' Int: longint;',
  10304. ' end;',
  10305. ' TArrayRec = array[1..2] of TRec;',
  10306. 'var',
  10307. ' Arr: TArrayRec;',
  10308. 'begin',
  10309. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10310. '']);
  10311. ConvertProgram;
  10312. CheckSource('TestArray_StaticRecord',
  10313. LinesToStr([ // statements
  10314. 'rtl.recNewT(this, "TRec", function () {',
  10315. ' this.Int = 0;',
  10316. ' this.$eq = function (b) {',
  10317. ' return this.Int === b.Int;',
  10318. ' };',
  10319. ' this.$assign = function (s) {',
  10320. ' this.Int = s.Int;',
  10321. ' return this;',
  10322. ' };',
  10323. '});',
  10324. 'this.TArrayRec$clone = function (a) {',
  10325. ' var b = [];',
  10326. ' b.length = 2;',
  10327. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10328. ' return b;',
  10329. '};',
  10330. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10331. '']),
  10332. LinesToStr([ // $mod.$main
  10333. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10334. end;
  10335. procedure TTestModule.TestArrayOfSet;
  10336. begin
  10337. StartProgram(false);
  10338. Add([
  10339. 'type',
  10340. ' TFlag = (big,small);',
  10341. ' TSetOfFlag = set of tflag;',
  10342. ' TArrayFlag = array of TSetOfFlag;',
  10343. 'procedure DoIt(const a: Tarrayflag);',
  10344. 'begin',
  10345. 'end;',
  10346. 'var',
  10347. ' f: TFlag;',
  10348. ' s: TSetOfFlag;',
  10349. ' Arr: TArrayFlag;',
  10350. ' i: longint;',
  10351. 'begin',
  10352. ' SetLength(arr,3);',
  10353. ' arr[0]:=s;',
  10354. ' arr[1]:=[big];',
  10355. ' arr[2]:=[big]+s;',
  10356. ' arr[3]:=s+[big];',
  10357. ' arr[4]:=arr[5];',
  10358. ' s:=arr[6];',
  10359. ' i:=low(arr);',
  10360. ' i:=high(arr);',
  10361. ' DoIt(arr);',
  10362. ' DoIt([s]);',
  10363. ' DoIt([[],s]);',
  10364. ' DoIt([s,[]]);',
  10365. '']);
  10366. ConvertProgram;
  10367. CheckSource('TestArrayOfSet',
  10368. LinesToStr([ // statements
  10369. 'this.TFlag = {',
  10370. ' "0": "big",',
  10371. ' big: 0,',
  10372. ' "1": "small",',
  10373. ' small: 1',
  10374. '};',
  10375. 'this.DoIt = function (a) {',
  10376. '};',
  10377. 'this.f = 0;',
  10378. 'this.s = {};',
  10379. 'this.Arr = [];',
  10380. 'this.i = 0;',
  10381. '']),
  10382. LinesToStr([ // $mod.$main
  10383. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10384. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10385. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10386. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10387. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10388. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10389. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10390. '$mod.i = 0;',
  10391. '$mod.i = rtl.length($mod.Arr) - 1;',
  10392. '$mod.DoIt($mod.Arr);',
  10393. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10394. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10395. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10396. '']));
  10397. end;
  10398. procedure TTestModule.TestArray_DynAsParam;
  10399. begin
  10400. StartProgram(false);
  10401. Add([
  10402. 'type integer = longint;',
  10403. 'type TArrInt = array of integer;',
  10404. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10405. 'var vJ: TArrInt;',
  10406. 'begin',
  10407. ' vg:=vg;',
  10408. ' vj:=vh;',
  10409. ' vi:=vi;',
  10410. ' doit(vg,vg,vg);',
  10411. ' doit(vh,vh,vj);',
  10412. ' doit(vi,vi,vi);',
  10413. ' doit(vj,vj,vj);',
  10414. 'end;',
  10415. 'var i: TArrInt;',
  10416. 'begin',
  10417. ' doit(i,i,i);']);
  10418. ConvertProgram;
  10419. CheckSource('TestArray_DynAsParams',
  10420. LinesToStr([ // statements
  10421. 'this.DoIt = function (vG,vH,vI) {',
  10422. ' var vJ = [];',
  10423. ' vG = rtl.arrayRef(vG);',
  10424. ' vJ = rtl.arrayRef(vH);',
  10425. ' vI.set(rtl.arrayRef(vI.get()));',
  10426. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10427. ' get: function () {',
  10428. ' return vG;',
  10429. ' },',
  10430. ' set: function (v) {',
  10431. ' vG = v;',
  10432. ' }',
  10433. ' });',
  10434. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10435. ' get: function () {',
  10436. ' return vJ;',
  10437. ' },',
  10438. ' set: function (v) {',
  10439. ' vJ = v;',
  10440. ' }',
  10441. ' });',
  10442. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10443. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10444. ' get: function () {',
  10445. ' return vJ;',
  10446. ' },',
  10447. ' set: function (v) {',
  10448. ' vJ = v;',
  10449. ' }',
  10450. ' });',
  10451. '};',
  10452. 'this.i = [];'
  10453. ]),
  10454. LinesToStr([
  10455. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10456. ' p: $mod,',
  10457. ' get: function () {',
  10458. ' return this.p.i;',
  10459. ' },',
  10460. ' set: function (v) {',
  10461. ' this.p.i = v;',
  10462. ' }',
  10463. '});'
  10464. ]));
  10465. end;
  10466. procedure TTestModule.TestArray_StaticAsParam;
  10467. begin
  10468. StartProgram(false);
  10469. Add([
  10470. 'type integer = longint;',
  10471. 'type TArrInt = array[1..2] of integer;',
  10472. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10473. 'var vJ: TArrInt;',
  10474. 'begin',
  10475. ' vg:=vg;',
  10476. ' vj:=vh;',
  10477. ' vi:=vi;',
  10478. ' doit(vg,vg,vg);',
  10479. ' doit(vh,vh,vj);',
  10480. ' doit(vi,vi,vi);',
  10481. ' doit(vj,vj,vj);',
  10482. 'end;',
  10483. 'var i: TArrInt;',
  10484. 'begin',
  10485. ' doit(i,i,i);']);
  10486. ConvertProgram;
  10487. CheckSource('TestArray_StaticAsParams',
  10488. LinesToStr([ // statements
  10489. 'this.DoIt = function (vG,vH,vI) {',
  10490. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10491. ' vG = vG.slice(0);',
  10492. ' vJ = vH.slice(0);',
  10493. ' vI.set(vI.get().slice(0));',
  10494. ' $mod.DoIt(vG.slice(0), vG, {',
  10495. ' get: function () {',
  10496. ' return vG;',
  10497. ' },',
  10498. ' set: function (v) {',
  10499. ' vG = v;',
  10500. ' }',
  10501. ' });',
  10502. ' $mod.DoIt(vH.slice(0), vH, {',
  10503. ' get: function () {',
  10504. ' return vJ;',
  10505. ' },',
  10506. ' set: function (v) {',
  10507. ' vJ = v;',
  10508. ' }',
  10509. ' });',
  10510. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10511. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10512. ' get: function () {',
  10513. ' return vJ;',
  10514. ' },',
  10515. ' set: function (v) {',
  10516. ' vJ = v;',
  10517. ' }',
  10518. ' });',
  10519. '};',
  10520. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10521. ]),
  10522. LinesToStr([
  10523. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10524. ' p: $mod,',
  10525. ' get: function () {',
  10526. ' return this.p.i;',
  10527. ' },',
  10528. ' set: function (v) {',
  10529. ' this.p.i = v;',
  10530. ' }',
  10531. '});'
  10532. ]));
  10533. end;
  10534. procedure TTestModule.TestArrayElement_AsParams;
  10535. begin
  10536. StartProgram(false);
  10537. Add('type integer = longint;');
  10538. Add('type TArrayInt = array of integer;');
  10539. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10540. Add('var vJ: tarrayint;');
  10541. Add('begin');
  10542. Add(' vi:=vi;');
  10543. Add(' doit(vi,vi,vi);');
  10544. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10545. Add('end;');
  10546. Add('var a: TArrayInt;');
  10547. Add('begin');
  10548. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10549. ConvertProgram;
  10550. CheckSource('TestArrayElement_AsParams',
  10551. LinesToStr([ // statements
  10552. 'this.DoIt = function (vG,vH,vI) {',
  10553. ' var vJ = [];',
  10554. ' vI.set(vI.get());',
  10555. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10556. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10557. ' a:1+3,',
  10558. ' p:vJ,',
  10559. ' get: function () {',
  10560. ' return this.p[this.a];',
  10561. ' },',
  10562. ' set: function (v) {',
  10563. ' this.p[this.a] = v;',
  10564. ' }',
  10565. ' });',
  10566. '};',
  10567. 'this.a = [];'
  10568. ]),
  10569. LinesToStr([
  10570. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10571. ' a: 1+6,',
  10572. ' p: $mod.a,',
  10573. ' get: function () {',
  10574. ' return this.p[this.a];',
  10575. ' },',
  10576. ' set: function (v) {',
  10577. ' this.p[this.a] = v;',
  10578. ' }',
  10579. '});'
  10580. ]));
  10581. end;
  10582. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10583. begin
  10584. StartProgram(false);
  10585. Add('type Integer = longint;');
  10586. Add('type TArrayInt = array of integer;');
  10587. Add('function GetArr(vB: integer = 0): tarrayint;');
  10588. Add('begin');
  10589. Add('end;');
  10590. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10591. Add('begin');
  10592. Add('end;');
  10593. Add('begin');
  10594. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10595. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10596. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10597. ConvertProgram;
  10598. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10599. LinesToStr([ // statements
  10600. 'this.GetArr = function (vB) {',
  10601. ' var Result = [];',
  10602. ' return Result;',
  10603. '};',
  10604. 'this.DoIt = function (vG,vH,vI) {',
  10605. '};'
  10606. ]),
  10607. LinesToStr([
  10608. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10609. ' a: 1+3,',
  10610. ' p: $mod.GetArr(0),',
  10611. ' get: function () {',
  10612. ' return this.p[this.a];',
  10613. ' },',
  10614. ' set: function (v) {',
  10615. ' this.p[this.a] = v;',
  10616. ' }',
  10617. '});',
  10618. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10619. ' a: 2+3,',
  10620. ' p: $mod.GetArr(0),',
  10621. ' get: function () {',
  10622. ' return this.p[this.a];',
  10623. ' },',
  10624. ' set: function (v) {',
  10625. ' this.p[this.a] = v;',
  10626. ' }',
  10627. '});',
  10628. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10629. ' a: 3+3,',
  10630. ' p: $mod.GetArr(9),',
  10631. ' get: function () {',
  10632. ' return this.p[this.a];',
  10633. ' },',
  10634. ' set: function (v) {',
  10635. ' this.p[this.a] = v;',
  10636. ' }',
  10637. '});',
  10638. '']));
  10639. end;
  10640. procedure TTestModule.TestArrayEnumTypeRange;
  10641. begin
  10642. StartProgram(false);
  10643. Add([
  10644. 'type',
  10645. ' TEnum = (red,blue);',
  10646. ' TEnumArray = array[TEnum] of longint;',
  10647. 'var',
  10648. ' e: TEnum;',
  10649. ' i: longint;',
  10650. ' a: TEnumArray;',
  10651. ' numbers: TEnumArray = (1,2);',
  10652. ' names: array[TEnum] of string = (''red'',''blue'');',
  10653. 'begin',
  10654. ' e:=low(a);',
  10655. ' e:=high(a);',
  10656. ' i:=a[red];',
  10657. ' a[e]:=a[e];']);
  10658. ConvertProgram;
  10659. CheckSource('TestArrayEnumTypeRange',
  10660. LinesToStr([ // statements
  10661. ' this.TEnum = {',
  10662. ' "0": "red",',
  10663. ' red: 0,',
  10664. ' "1": "blue",',
  10665. ' blue: 1',
  10666. '};',
  10667. 'this.e = 0;',
  10668. 'this.i = 0;',
  10669. 'this.a = rtl.arraySetLength(null,0,2);',
  10670. 'this.numbers = [1, 2];',
  10671. 'this.names = ["red", "blue"];',
  10672. '']),
  10673. LinesToStr([ // $mod.$main
  10674. '$mod.e = $mod.TEnum.red;',
  10675. '$mod.e = $mod.TEnum.blue;',
  10676. '$mod.i = $mod.a[$mod.TEnum.red];',
  10677. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10678. '']));
  10679. end;
  10680. procedure TTestModule.TestArray_SetLengthOutArg;
  10681. begin
  10682. StartProgram(false);
  10683. Add([
  10684. 'type TArrInt = array of longint;',
  10685. 'procedure DoIt(out a: TArrInt);',
  10686. 'begin',
  10687. ' SetLength(a,2);',
  10688. 'end;',
  10689. 'begin',
  10690. '']);
  10691. ConvertProgram;
  10692. CheckSource('TestArray_SetLengthOutArg',
  10693. LinesToStr([ // statements
  10694. 'this.DoIt = function (a) {',
  10695. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10696. '};',
  10697. '']),
  10698. LinesToStr([
  10699. '']));
  10700. end;
  10701. procedure TTestModule.TestArray_SetLengthProperty;
  10702. begin
  10703. StartProgram(false);
  10704. Add('type');
  10705. Add(' TArrInt = array of longint;');
  10706. Add(' TObject = class');
  10707. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10708. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10709. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10710. Add(' end;');
  10711. Add('var Obj: TObject;');
  10712. Add('begin');
  10713. Add(' SetLength(Obj.Colors,2);');
  10714. ConvertProgram;
  10715. CheckSource('TestArray_SetLengthProperty',
  10716. LinesToStr([ // statements
  10717. 'rtl.createClass(this, "TObject", null, function () {',
  10718. ' this.$init = function () {',
  10719. ' };',
  10720. ' this.$final = function () {',
  10721. ' };',
  10722. '});',
  10723. 'this.Obj = null;',
  10724. '']),
  10725. LinesToStr([
  10726. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10727. '']));
  10728. end;
  10729. procedure TTestModule.TestArray_SetLengthMultiDim;
  10730. begin
  10731. StartProgram(false);
  10732. Add([
  10733. 'type',
  10734. ' TArrArrInt = array of array of longint;',
  10735. ' TArrStaInt = array of array[1..2] of longint;',
  10736. 'var',
  10737. ' a: TArrArrInt;',
  10738. ' b: TArrStaInt;',
  10739. 'begin',
  10740. ' SetLength(a,2);',
  10741. ' SetLength(a,3,4);',
  10742. ' SetLength(b,5);',
  10743. '']);
  10744. ConvertProgram;
  10745. CheckSource('TestArray_SetLengthMultiDim',
  10746. LinesToStr([ // statements
  10747. 'this.a = [];',
  10748. 'this.b = [];',
  10749. '']),
  10750. LinesToStr([
  10751. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10752. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10753. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10754. '']));
  10755. end;
  10756. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10757. begin
  10758. StartProgram(false);
  10759. Add([
  10760. 'type',
  10761. ' TStaArr1 = array[1..3] of boolean;',
  10762. //' TStaArr2 = array[5..6] of TStaArr1;',
  10763. ' TDynArr1StaArr1 = array of TStaArr1;',
  10764. //' TDynArr1StaArr2 = array of TStaArr2;',
  10765. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10766. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10767. 'var',
  10768. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10769. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10770. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10771. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10772. 'begin',
  10773. ' SetLength(DynArr1StaArr1,11);',
  10774. ' SetLength(DynArr2StaArr1,12);',
  10775. ' SetLength(DynArr2StaArr1[13],14);',
  10776. ' SetLength(DynArr2StaArr1,15,16);',
  10777. //' SetLength(DynArr1StaArr2,21);',
  10778. //' SetLength(DynArr2StaArr2,22);',
  10779. //' SetLength(DynArr2StaArr2[23],24);',
  10780. //' SetLength(DynArr2StaArr2,25,26);',
  10781. '']);
  10782. ConvertProgram;
  10783. CheckSource('TestArray_DynOfStatic',
  10784. LinesToStr([ // statements
  10785. 'this.DynArr1StaArr1 = [];',
  10786. 'this.DynArr2StaArr1 = [];',
  10787. '']),
  10788. LinesToStr([ // $mod.$main
  10789. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10790. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10791. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10792. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10793. ' $mod.DynArr2StaArr1,',
  10794. ' false,',
  10795. ' 15,',
  10796. ' 16,',
  10797. ' "s",',
  10798. ' 3',
  10799. ');',
  10800. '']));
  10801. end;
  10802. procedure TTestModule.TestArray_OpenArrayOfString;
  10803. begin
  10804. StartProgram(false);
  10805. Add('procedure DoIt(const a: array of String);');
  10806. Add('var');
  10807. Add(' i: longint;');
  10808. Add(' s: string;');
  10809. Add('begin');
  10810. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10811. Add('end;');
  10812. Add('var s: string;');
  10813. Add('begin');
  10814. Add(' DoIt([]);');
  10815. Add(' DoIt([s,''foo'','''',s+s]);');
  10816. ConvertProgram;
  10817. CheckSource('TestArray_OpenArrayOfString',
  10818. LinesToStr([ // statements
  10819. 'this.DoIt = function (a) {',
  10820. ' var i = 0;',
  10821. ' var s = "";',
  10822. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10823. ' i = $l;',
  10824. ' s = a[rtl.length(a) - i - 1];',
  10825. ' };',
  10826. '};',
  10827. 'this.s = "";',
  10828. '']),
  10829. LinesToStr([
  10830. '$mod.DoIt([]);',
  10831. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10832. '']));
  10833. end;
  10834. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10835. begin
  10836. StartProgram(false);
  10837. Add([
  10838. 'type TArr = array of char;',
  10839. 'var',
  10840. ' c: char;',
  10841. ' s: string;',
  10842. ' a: TArr;',
  10843. 'procedure Run(const a: array of char);',
  10844. 'begin',
  10845. ' Run(c);',
  10846. ' Run(s);',
  10847. 'end;',
  10848. 'begin',
  10849. ' a:=c;',
  10850. ' a:=s;',
  10851. ' a:=#13;',
  10852. ' a:=''Foo'';',
  10853. ' Run(c);',
  10854. ' Run(s);',
  10855. '']);
  10856. ConvertProgram;
  10857. CheckSource('TestArray_ArrayOfCharAssignString',
  10858. LinesToStr([ // statements
  10859. 'this.c = "";',
  10860. 'this.s = "";',
  10861. 'this.a = [];',
  10862. 'this.Run = function (a) {',
  10863. ' $mod.Run($mod.c.split(""));',
  10864. ' $mod.Run($mod.s.split(""));',
  10865. '};',
  10866. '']),
  10867. LinesToStr([
  10868. '$mod.a = $mod.c.split("");',
  10869. '$mod.a = $mod.s.split("");',
  10870. '$mod.a = "\r".split("");',
  10871. '$mod.a = "Foo".split("");',
  10872. '$mod.Run($mod.c.split(""));',
  10873. '$mod.Run($mod.s.split(""));',
  10874. '']));
  10875. end;
  10876. procedure TTestModule.TestArray_ConstRef;
  10877. begin
  10878. StartProgram(false);
  10879. Add([
  10880. 'type TArr = array of word;',
  10881. 'procedure Run(constref a: TArr);',
  10882. 'begin',
  10883. 'end;',
  10884. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10885. 'var l: TArr;',
  10886. 'begin',
  10887. ' Run(l);',
  10888. ' Run(a);',
  10889. ' Run(b);',
  10890. ' Run(c);',
  10891. ' Run(d);',
  10892. ' Run(e);',
  10893. 'end;',
  10894. 'begin',
  10895. '']);
  10896. ConvertProgram;
  10897. CheckResolverUnexpectedHints();
  10898. CheckSource('TestArray_ConstRef',
  10899. LinesToStr([ // statements
  10900. 'this.Run = function (a) {',
  10901. '};',
  10902. 'this.Fly = function (a, b, c, d, e) {',
  10903. ' var l = [];',
  10904. ' $mod.Run(l);',
  10905. ' $mod.Run(a);',
  10906. ' $mod.Run(b.get());',
  10907. ' $mod.Run(c.get());',
  10908. ' $mod.Run(d);',
  10909. ' $mod.Run(e);',
  10910. '};',
  10911. '']),
  10912. LinesToStr([
  10913. '']));
  10914. end;
  10915. procedure TTestModule.TestArray_Concat;
  10916. begin
  10917. StartProgram(false);
  10918. Add([
  10919. 'type',
  10920. ' integer = longint;',
  10921. ' TFlag = (big,small);',
  10922. ' TFlags = set of TFlag;',
  10923. ' TRec = record',
  10924. ' i: integer;',
  10925. ' end;',
  10926. ' TArrInt = array of integer;',
  10927. ' TArrRec = array of TRec;',
  10928. ' TArrFlag = array of TFlag;',
  10929. ' TArrSet = array of TFlags;',
  10930. ' TArrJSValue = array of jsvalue;',
  10931. 'var',
  10932. ' ArrInt1, ArrInt2: tarrint;',
  10933. ' ArrRec1, ArrRec2: tarrrec;',
  10934. ' ArrFlag1, ArrFlag2: tarrflag;',
  10935. ' ArrSet1, ArrSet2: tarrset;',
  10936. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10937. 'begin',
  10938. ' arrint1:=concat(arrint2);',
  10939. ' arrint1:=concat(arrint2,arrint2);',
  10940. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10941. ' arrrec1:=concat(arrrec2);',
  10942. ' arrrec1:=concat(arrrec2,arrrec2);',
  10943. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10944. ' arrset1:=concat(arrset2);',
  10945. ' arrset1:=concat(arrset2,arrset2);',
  10946. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10947. ' arrjsvalue1:=concat(arrjsvalue2);',
  10948. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10949. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10950. ' arrint1:=concat([1],arrint2);',
  10951. ' arrflag1:=concat([big]);',
  10952. ' arrflag1:=concat([big],arrflag2);',
  10953. ' arrflag1:=concat(arrflag2,[small]);',
  10954. '']);
  10955. ConvertProgram;
  10956. CheckSource('TestArray_Concat',
  10957. LinesToStr([ // statements
  10958. 'this.TFlag = {',
  10959. ' "0": "big",',
  10960. ' big: 0,',
  10961. ' "1": "small",',
  10962. ' small: 1',
  10963. '};',
  10964. 'rtl.recNewT(this, "TRec", function () {',
  10965. ' this.i = 0;',
  10966. ' this.$eq = function (b) {',
  10967. ' return this.i === b.i;',
  10968. ' };',
  10969. ' this.$assign = function (s) {',
  10970. ' this.i = s.i;',
  10971. ' return this;',
  10972. ' };',
  10973. '});',
  10974. 'this.ArrInt1 = [];',
  10975. 'this.ArrInt2 = [];',
  10976. 'this.ArrRec1 = [];',
  10977. 'this.ArrRec2 = [];',
  10978. 'this.ArrFlag1 = [];',
  10979. 'this.ArrFlag2 = [];',
  10980. 'this.ArrSet1 = [];',
  10981. 'this.ArrSet2 = [];',
  10982. 'this.ArrJSValue1 = [];',
  10983. 'this.ArrJSValue2 = [];',
  10984. '']),
  10985. LinesToStr([ // $mod.$main
  10986. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  10987. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  10988. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  10989. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  10990. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  10991. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  10992. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  10993. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  10994. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  10995. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  10996. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  10997. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  10998. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  10999. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  11000. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11001. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11002. '']));
  11003. end;
  11004. procedure TTestModule.TestArray_Concat_Append;
  11005. begin
  11006. StartProgram(false);
  11007. Add([
  11008. 'type',
  11009. ' integer = longint;',
  11010. ' TFlag = (big,small);',
  11011. ' TFlags = set of TFlag;',
  11012. ' TRec = record',
  11013. ' i: integer;',
  11014. ' end;',
  11015. ' TArrInt = array of integer;',
  11016. ' TArrRec = array of TRec;',
  11017. ' TArrFlag = array of TFlag;',
  11018. ' TArrSet = array of TFlags;',
  11019. ' TArrJSValue = array of jsvalue;',
  11020. 'var',
  11021. ' ArrInt: tarrint;',
  11022. ' ArrRec: tarrrec;',
  11023. ' ArrFlag: tarrflag;',
  11024. ' ArrSet: tarrset;',
  11025. ' ArrJSValue: tarrjsvalue;',
  11026. ' r: TRec;',
  11027. ' f: TFlags;',
  11028. 'begin',
  11029. ' // append',
  11030. ' arrint:=concat(arrint);',
  11031. ' arrint:=concat(arrint,[2]);',
  11032. ' arrint:=concat(arrint,[3,4]);',
  11033. ' arrrec:=concat(arrrec);',
  11034. ' arrrec:=concat(arrrec,[r]);',
  11035. ' arrrec:=concat(arrrec,[r,r]);',
  11036. ' arrset:=concat(arrset);',
  11037. ' arrset:=concat(arrset,[f]);',
  11038. ' arrset:=concat(arrset,[f,f]);',
  11039. ' arrjsvalue:=concat(arrjsvalue);',
  11040. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11041. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11042. ' arrflag:=concat(arrflag);',
  11043. ' arrflag:=concat(arrflag,[small]);',
  11044. ' arrflag:=concat(arrflag,[small,big]);',
  11045. '']);
  11046. ConvertProgram;
  11047. CheckSource('TestArray_Concat_Append',
  11048. LinesToStr([ // statements
  11049. 'this.TFlag = {',
  11050. ' "0": "big",',
  11051. ' big: 0,',
  11052. ' "1": "small",',
  11053. ' small: 1',
  11054. '};',
  11055. 'rtl.recNewT(this, "TRec", function () {',
  11056. ' this.i = 0;',
  11057. ' this.$eq = function (b) {',
  11058. ' return this.i === b.i;',
  11059. ' };',
  11060. ' this.$assign = function (s) {',
  11061. ' this.i = s.i;',
  11062. ' return this;',
  11063. ' };',
  11064. '});',
  11065. 'this.ArrInt = [];',
  11066. 'this.ArrRec = [];',
  11067. 'this.ArrFlag = [];',
  11068. 'this.ArrSet = [];',
  11069. 'this.ArrJSValue = [];',
  11070. 'this.r = this.TRec.$new();',
  11071. 'this.f = {};',
  11072. '']),
  11073. LinesToStr([ // $mod.$main
  11074. '$mod.ArrInt = $mod.ArrInt;',
  11075. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11076. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11077. '$mod.ArrRec = $mod.ArrRec;',
  11078. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11079. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11080. '$mod.ArrSet = $mod.ArrSet;',
  11081. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11082. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11083. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11084. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11085. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11086. '$mod.ArrFlag = $mod.ArrFlag;',
  11087. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11088. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11089. '']));
  11090. end;
  11091. procedure TTestModule.TestArray_Concat_Append_Var;
  11092. begin
  11093. StartProgram(false);
  11094. Add([
  11095. 'type',
  11096. ' TArrInt = array of word;',
  11097. '',
  11098. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11099. 'begin',
  11100. ' a:=concat(a,[2]);',
  11101. ' b:=concat(b,[2]);',
  11102. 'end;',
  11103. 'var',
  11104. ' ArrInt: tarrint;',
  11105. 'begin',
  11106. ' Fly(ArrInt,ArrInt);',
  11107. '']);
  11108. ConvertProgram;
  11109. CheckSource('TestArray_Concat_Append_Var',
  11110. LinesToStr([ // statements
  11111. 'this.Fly = function (a, b) {',
  11112. ' a = rtl.arrayPushN(a, 2);',
  11113. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11114. '};',
  11115. 'this.ArrInt = [];',
  11116. '']),
  11117. LinesToStr([ // $mod.$main
  11118. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11119. ' p: $mod,',
  11120. ' get: function () {',
  11121. ' return this.p.ArrInt;',
  11122. ' },',
  11123. ' set: function (v) {',
  11124. ' this.p.ArrInt = v;',
  11125. ' }',
  11126. '});',
  11127. '']));
  11128. end;
  11129. procedure TTestModule.TestArray_Copy;
  11130. begin
  11131. StartProgram(false);
  11132. Add([
  11133. 'type',
  11134. ' integer = longint;',
  11135. ' TFlag = (big,small);',
  11136. ' TFlags = set of TFlag;',
  11137. ' TRec = record',
  11138. ' i: integer;',
  11139. ' end;',
  11140. ' TArrInt = array of integer;',
  11141. ' TArrRec = array of TRec;',
  11142. ' TArrSet = array of TFlags;',
  11143. ' TArrJSValue = array of jsvalue;',
  11144. 'var',
  11145. ' ArrInt: tarrint;',
  11146. ' ArrRec: tarrrec;',
  11147. ' ArrSet: tarrset;',
  11148. ' ArrJSValue: tarrjsvalue;',
  11149. 'begin',
  11150. ' arrint:=copy(arrint);',
  11151. ' arrint:=copy(arrint,2);',
  11152. ' arrint:=copy(arrint,3,4);',
  11153. ' arrint:=copy([1,1],1,2);',
  11154. ' arrrec:=copy(arrrec);',
  11155. ' arrrec:=copy(arrrec,5);',
  11156. ' arrrec:=copy(arrrec,6,7);',
  11157. ' arrset:=copy(arrset);',
  11158. ' arrset:=copy(arrset,8);',
  11159. ' arrset:=copy(arrset,9,10);',
  11160. ' arrjsvalue:=copy(arrjsvalue);',
  11161. ' arrjsvalue:=copy(arrjsvalue,11);',
  11162. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11163. ' ']);
  11164. ConvertProgram;
  11165. CheckSource('TestArray_Copy',
  11166. LinesToStr([ // statements
  11167. 'this.TFlag = {',
  11168. ' "0": "big",',
  11169. ' big: 0,',
  11170. ' "1": "small",',
  11171. ' small: 1',
  11172. '};',
  11173. 'rtl.recNewT(this, "TRec", function () {',
  11174. ' this.i = 0;',
  11175. ' this.$eq = function (b) {',
  11176. ' return this.i === b.i;',
  11177. ' };',
  11178. ' this.$assign = function (s) {',
  11179. ' this.i = s.i;',
  11180. ' return this;',
  11181. ' };',
  11182. '});',
  11183. 'this.ArrInt = [];',
  11184. 'this.ArrRec = [];',
  11185. 'this.ArrSet = [];',
  11186. 'this.ArrJSValue = [];',
  11187. '']),
  11188. LinesToStr([ // $mod.$main
  11189. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11190. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11191. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11192. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11193. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11194. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11195. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11196. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11197. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11198. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11199. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11200. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11201. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11202. '']));
  11203. end;
  11204. procedure TTestModule.TestArray_InsertDelete;
  11205. begin
  11206. StartProgram(false);
  11207. Add([
  11208. 'type',
  11209. ' integer = longint;',
  11210. ' TFlag = (big,small);',
  11211. ' TFlags = set of TFlag;',
  11212. ' TRec = record',
  11213. ' i: integer;',
  11214. ' end;',
  11215. ' TArrInt = array of integer;',
  11216. ' TArrRec = array of TRec;',
  11217. ' TArrSet = array of TFlags;',
  11218. ' TArrJSValue = array of jsvalue;',
  11219. ' TArrArrInt = array of TArrInt;',
  11220. 'var',
  11221. ' ArrInt: tarrint;',
  11222. ' ArrRec: tarrrec;',
  11223. ' ArrSet: tarrset;',
  11224. ' ArrJSValue: tarrjsvalue;',
  11225. ' ArrArrInt: TArrArrInt;',
  11226. 'begin',
  11227. ' Insert(1,arrint,2);',
  11228. ' Insert(arrint[3],arrint,4);',
  11229. ' Insert(arrrec[5],arrrec,6);',
  11230. ' Insert(arrset[7],arrset,7);',
  11231. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11232. ' Insert(10,arrjsvalue,11);',
  11233. ' Insert([23],arrarrint,22);',
  11234. ' Delete(arrint,12,13);',
  11235. ' Delete(arrrec,14,15);',
  11236. ' Delete(arrset,17,18);',
  11237. ' Delete(arrjsvalue,19,10);']);
  11238. ConvertProgram;
  11239. CheckSource('TestArray_InsertDelete',
  11240. LinesToStr([ // statements
  11241. 'this.TFlag = {',
  11242. ' "0": "big",',
  11243. ' big: 0,',
  11244. ' "1": "small",',
  11245. ' small: 1',
  11246. '};',
  11247. 'rtl.recNewT(this, "TRec", function () {',
  11248. ' this.i = 0;',
  11249. ' this.$eq = function (b) {',
  11250. ' return this.i === b.i;',
  11251. ' };',
  11252. ' this.$assign = function (s) {',
  11253. ' this.i = s.i;',
  11254. ' return this;',
  11255. ' };',
  11256. '});',
  11257. 'this.ArrInt = [];',
  11258. 'this.ArrRec = [];',
  11259. 'this.ArrSet = [];',
  11260. 'this.ArrJSValue = [];',
  11261. 'this.ArrArrInt = [];',
  11262. '']),
  11263. LinesToStr([ // $mod.$main
  11264. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11265. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11266. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11267. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11268. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11269. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11270. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11271. '$mod.ArrInt.splice(12, 13);',
  11272. '$mod.ArrRec.splice(14, 15);',
  11273. '$mod.ArrSet.splice(17, 18);',
  11274. '$mod.ArrJSValue.splice(19, 10);',
  11275. '']));
  11276. end;
  11277. procedure TTestModule.TestArray_Add_Append;
  11278. begin
  11279. StartProgram(false);
  11280. Add([
  11281. '{$modeswitch arrayoperators}',
  11282. 'type',
  11283. ' integer = longint;',
  11284. ' TFlag = (big,small);',
  11285. ' TFlags = set of TFlag;',
  11286. ' TRec = record',
  11287. ' i: integer;',
  11288. ' end;',
  11289. ' TArrInt = array of integer;',
  11290. ' TArrRec = array of TRec;',
  11291. ' TArrFlag = array of TFlag;',
  11292. ' TArrSet = array of TFlags;',
  11293. ' TArrJSValue = array of jsvalue;',
  11294. 'var',
  11295. ' ArrInt: tarrint;',
  11296. ' ArrRec: tarrrec;',
  11297. ' ArrFlag: tarrflag;',
  11298. ' ArrSet: tarrset;',
  11299. ' ArrJSValue: tarrjsvalue;',
  11300. ' r: TRec;',
  11301. ' f: TFlags;',
  11302. 'begin',
  11303. ' // append',
  11304. ' arrint:=arrint+[2];',
  11305. ' arrint:=arrint+[3,4];',
  11306. ' arrrec:=arrrec+[r];',
  11307. ' arrrec:=arrrec+[r,r];',
  11308. ' arrset:=arrset+[f];',
  11309. ' arrset:=arrset+[f,f];',
  11310. ' arrjsvalue:=arrjsvalue+[11];',
  11311. ' arrjsvalue:=arrjsvalue+[12,13];',
  11312. ' arrflag:=arrflag+[small];',
  11313. ' arrflag:=arrflag+[small,big];',
  11314. '']);
  11315. ConvertProgram;
  11316. CheckSource('TestArray_Add_Append',
  11317. LinesToStr([ // statements
  11318. 'this.TFlag = {',
  11319. ' "0": "big",',
  11320. ' big: 0,',
  11321. ' "1": "small",',
  11322. ' small: 1',
  11323. '};',
  11324. 'rtl.recNewT(this, "TRec", function () {',
  11325. ' this.i = 0;',
  11326. ' this.$eq = function (b) {',
  11327. ' return this.i === b.i;',
  11328. ' };',
  11329. ' this.$assign = function (s) {',
  11330. ' this.i = s.i;',
  11331. ' return this;',
  11332. ' };',
  11333. '});',
  11334. 'this.ArrInt = [];',
  11335. 'this.ArrRec = [];',
  11336. 'this.ArrFlag = [];',
  11337. 'this.ArrSet = [];',
  11338. 'this.ArrJSValue = [];',
  11339. 'this.r = this.TRec.$new();',
  11340. 'this.f = {};',
  11341. '']),
  11342. LinesToStr([ // $mod.$main
  11343. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11344. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11345. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11346. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11347. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11348. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11349. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11350. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11351. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11352. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11353. '']));
  11354. end;
  11355. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11356. begin
  11357. Parser.Options:=Parser.Options+[po_cassignments];
  11358. StartProgram(false);
  11359. Add([
  11360. '{$modeswitch arrayoperators}',
  11361. 'type',
  11362. ' integer = longint;',
  11363. ' TArrInt = array of integer;',
  11364. ' TArrStr = array of string;',
  11365. 'const',
  11366. ' Ints: TArrInt = (1,2,3);',
  11367. ' Aliases: TarrStr = (''foo'',''b'');',
  11368. ' OneInt: TArrInt = (7);',
  11369. ' OneStr: array of integer = (7);',
  11370. ' Chars: array of char = ''aoc'';',
  11371. ' Names: array of string = (''a'',''foo'');',
  11372. ' NameCount = low(Names)+high(Names)+length(Names);',
  11373. 'var i: integer;',
  11374. 'begin',
  11375. ' Ints:=[];',
  11376. ' Ints:=[1,1];',
  11377. ' Ints:=[1]+[2];',
  11378. ' Ints:=[2];',
  11379. ' Ints:=[]+ints;',
  11380. ' Ints:=Ints+[];',
  11381. ' Ints:=Ints+OneInt;',
  11382. ' Ints:=Ints+[1,1];',
  11383. ' Ints:=[i,i]+Ints;',
  11384. ' Ints:=[1]+[i]+[3];',
  11385. '']);
  11386. ConvertProgram;
  11387. CheckSource('TestArray_DynArrayConstObjFPC',
  11388. LinesToStr([ // statements
  11389. 'this.Ints = [1, 2, 3];',
  11390. 'this.Aliases = ["foo", "b"];',
  11391. 'this.OneInt = [7];',
  11392. 'this.OneStr = [7];',
  11393. 'this.Chars = ["a", "o", "c"];',
  11394. 'this.Names = ["a", "foo"];',
  11395. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11396. 'this.i = 0;',
  11397. '']),
  11398. LinesToStr([ // $mod.$main
  11399. '$mod.Ints = [];',
  11400. '$mod.Ints = [1, 1];',
  11401. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11402. '$mod.Ints = [2];',
  11403. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11404. '$mod.Ints = $mod.Ints;',
  11405. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11406. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11407. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11408. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11409. '']));
  11410. end;
  11411. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11412. begin
  11413. StartProgram(false);
  11414. // Note: const c = [1,1]; defines a set!
  11415. Add([
  11416. '{$mode delphi}',
  11417. 'type',
  11418. ' integer = longint;',
  11419. ' TArrInt = array of integer;',
  11420. ' TArrStr = array of string;',
  11421. 'const',
  11422. ' Ints: TArrInt = [1,1,2];',
  11423. ' Aliases: TarrStr = [''foo'',''b''];',
  11424. ' OneInt: TArrInt = [7];',
  11425. ' OneStr: array of integer = [7]+[8];',
  11426. ' Chars: array of char = ''aoc'';',
  11427. ' Names: array of string = [''a'',''a''];',
  11428. ' NameCount = low(Names)+high(Names)+length(Names);',
  11429. 'begin',
  11430. '']);
  11431. ConvertProgram;
  11432. CheckSource('TestArray_DynArrayConstDelphi',
  11433. LinesToStr([ // statements
  11434. 'this.Ints = [1, 1, 2];',
  11435. 'this.Aliases = ["foo", "b"];',
  11436. 'this.OneInt = [7];',
  11437. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11438. 'this.Chars = ["a", "o", "c"];',
  11439. 'this.Names = ["a", "a"];',
  11440. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11441. '']),
  11442. LinesToStr([ // $mod.$main
  11443. '']));
  11444. end;
  11445. procedure TTestModule.TestArray_ArrayLitAsParam;
  11446. begin
  11447. StartProgram(false);
  11448. Add([
  11449. '{$modeswitch arrayoperators}',
  11450. 'type',
  11451. ' integer = longint;',
  11452. ' TArrInt = array of integer;',
  11453. ' TArrSet = array of (red,green,blue);',
  11454. 'procedure DoOpenInt(const a: array of integer); forward;',
  11455. 'procedure DoInt(const a: TArrInt);',
  11456. 'begin',
  11457. ' DoInt(a+[1]);',
  11458. ' DoInt([1]+a);',
  11459. ' DoOpenInt(a);',
  11460. ' DoOpenInt(a+[1]);',
  11461. ' DoOpenInt([1]+a);',
  11462. 'end;',
  11463. 'procedure DoOpenInt(const a: array of integer);',
  11464. 'begin',
  11465. ' DoOpenInt(a+[1]);',
  11466. ' DoOpenInt([1]+a);',
  11467. ' DoInt(a);',
  11468. ' DoInt(a+[1]);',
  11469. ' DoInt([1]+a);',
  11470. 'end;',
  11471. 'procedure DoSet(const a: TArrSet);',
  11472. 'begin',
  11473. ' DoSet(a+[red]);',
  11474. ' DoSet([blue]+a);',
  11475. 'end;',
  11476. 'var',
  11477. ' i: TArrInt;',
  11478. ' s: TArrSet;',
  11479. 'begin',
  11480. ' DoInt([1]);',
  11481. ' DoInt([1]+[2]);',
  11482. ' DoInt(i+[1]);',
  11483. ' DoInt([1]+i);',
  11484. ' DoOpenInt([1]);',
  11485. ' DoOpenInt([1]+[2]);',
  11486. ' DoOpenInt(i+[1]);',
  11487. ' DoOpenInt([1]+i);',
  11488. ' DoSet([red]);',
  11489. ' DoSet([blue]+[green]);',
  11490. ' DoSet(s+[blue]);',
  11491. ' DoSet([red]+s);',
  11492. '']);
  11493. ConvertProgram;
  11494. CheckSource('TestArray_ArrayLitAsParam',
  11495. LinesToStr([ // statements
  11496. 'this.TArrSet$a = {',
  11497. ' "0": "red",',
  11498. ' red: 0,',
  11499. ' "1": "green",',
  11500. ' green: 1,',
  11501. ' "2": "blue",',
  11502. ' blue: 2',
  11503. '};',
  11504. 'this.DoInt = function (a) {',
  11505. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11506. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11507. ' $mod.DoOpenInt(a);',
  11508. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11509. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11510. '};',
  11511. 'this.DoOpenInt = function (a) {',
  11512. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11513. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11514. ' $mod.DoInt(a);',
  11515. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11516. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11517. '};',
  11518. 'this.DoSet = function (a) {',
  11519. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11520. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11521. '};',
  11522. 'this.i = [];',
  11523. 'this.s = [];',
  11524. '']),
  11525. LinesToStr([ // $mod.$main
  11526. '$mod.DoInt([1]);',
  11527. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11528. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11529. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11530. '$mod.DoOpenInt([1]);',
  11531. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11532. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11533. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11534. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11535. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11536. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11537. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11538. '']));
  11539. end;
  11540. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11541. begin
  11542. StartProgram(false);
  11543. Add([
  11544. '{$modeswitch arrayoperators}',
  11545. 'type',
  11546. ' integer = longint;',
  11547. ' TArrInt = array of integer;',
  11548. ' TArrArrInt = array of TArrInt;',
  11549. 'procedure DoInt(const a: TArrArrInt);',
  11550. 'begin',
  11551. ' DoInt(a+[[1]]);',
  11552. ' DoInt([[1]]+a);',
  11553. ' DoInt(a);',
  11554. 'end;',
  11555. 'var',
  11556. ' i: TArrInt;',
  11557. ' a: TArrArrInt;',
  11558. 'begin',
  11559. ' a:=[[1]];',
  11560. ' a:=[i];',
  11561. ' a:=a+[i];',
  11562. ' a:=[i]+a;',
  11563. ' a:=[[1]+i];',
  11564. ' a:=[[1]+[2]];',
  11565. ' a:=[i+[2]];',
  11566. ' DoInt([[1]]);',
  11567. ' DoInt([[1]+[2],[3,4],[5]]);',
  11568. ' DoInt([i+[1]]+a);',
  11569. ' DoInt([i]+a);',
  11570. '']);
  11571. ConvertProgram;
  11572. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11573. LinesToStr([ // statements
  11574. 'this.DoInt = function (a) {',
  11575. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11576. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11577. ' $mod.DoInt(a);',
  11578. '};',
  11579. 'this.i = [];',
  11580. 'this.a = [];',
  11581. '']),
  11582. LinesToStr([ // $mod.$main
  11583. '$mod.a = [[1]];',
  11584. '$mod.a = [$mod.i];',
  11585. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11586. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11587. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11588. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11589. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11590. '$mod.DoInt([[1]]);',
  11591. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11592. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11593. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11594. '']));
  11595. end;
  11596. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11597. begin
  11598. StartProgram(false);
  11599. Add([
  11600. '{$modeswitch arrayoperators}',
  11601. 'type',
  11602. ' integer = longint;',
  11603. ' TArrInt = array[1..2] of integer;',
  11604. ' TArrArrInt = array of TArrInt;',
  11605. 'procedure DoInt(const a: TArrArrInt);',
  11606. 'begin',
  11607. ' DoInt(a+[[1,2]]);',
  11608. ' DoInt([[1,2]]+a);',
  11609. ' DoInt(a);',
  11610. 'end;',
  11611. 'var',
  11612. ' i: TArrInt;',
  11613. ' a: TArrArrInt;',
  11614. 'begin',
  11615. ' a:=[[1,1]];',
  11616. ' a:=[i];',
  11617. ' a:=a+[i];',
  11618. ' a:=[i]+a;',
  11619. ' DoInt([[1,1]]);',
  11620. ' DoInt([[1,2],[3,4]]);',
  11621. '']);
  11622. ConvertProgram;
  11623. CheckSource('TestArray_ArrayLitStaticAsParam',
  11624. LinesToStr([ // statements
  11625. 'this.DoInt = function (a) {',
  11626. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11627. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11628. ' $mod.DoInt(a);',
  11629. '};',
  11630. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11631. 'this.a = [];',
  11632. '']),
  11633. LinesToStr([ // $mod.$main
  11634. '$mod.a = [[1, 1]];',
  11635. '$mod.a = [$mod.i.slice(0)];',
  11636. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11637. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11638. '$mod.DoInt([[1, 1]]);',
  11639. '$mod.DoInt([[1, 2], [3, 4]]);',
  11640. '']));
  11641. end;
  11642. procedure TTestModule.TestArray_ForInArrOfString;
  11643. begin
  11644. StartProgram(false);
  11645. Add([
  11646. 'type',
  11647. 'type',
  11648. ' TMonthNameArray = array [1..12] of string;',
  11649. ' TMonthNames = TMonthNameArray;',
  11650. ' TObject = class',
  11651. ' private',
  11652. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11653. ' public',
  11654. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11655. ' end;',
  11656. 'var',
  11657. ' f: TObject;',
  11658. ' Month: string;',
  11659. ' Names: array of string = (''a'',''foo'',''bar'');',
  11660. ' i: longint;',
  11661. 'begin',
  11662. ' for Month in f.LongMonthNames do ;',
  11663. ' for Month in Names do ;',
  11664. ' for i:=low(Names) to high(Names) do ;',
  11665. '']);
  11666. ConvertProgram;
  11667. CheckSource('TestArray_ForInArrOfString',
  11668. LinesToStr([ // statements
  11669. 'rtl.createClass(this, "TObject", null, function () {',
  11670. ' this.$init = function () {',
  11671. ' };',
  11672. ' this.$final = function () {',
  11673. ' };',
  11674. '});',
  11675. 'this.f = null;',
  11676. 'this.Month = "";',
  11677. 'this.Names = ["a", "foo", "bar"];',
  11678. 'this.i = 0;',
  11679. '']),
  11680. LinesToStr([ // $mod.$main
  11681. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11682. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11683. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11684. '']));
  11685. end;
  11686. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11687. begin
  11688. StartProgram(false);
  11689. Add([
  11690. '{$modeswitch externalclass}',
  11691. 'type',
  11692. ' TJSObject = class external name ''Object''',
  11693. ' end;',
  11694. ' TJSArray = class external name ''Array''',
  11695. ' class function isArray(Value: JSValue) : boolean;',
  11696. ' function concat() : TJSArray; varargs;',
  11697. ' end;',
  11698. 'var',
  11699. ' aObj: TJSArray;',
  11700. ' a: array of longint;',
  11701. ' o: TJSObject;',
  11702. 'begin',
  11703. ' if TJSArray.isArray(65) then ;',
  11704. ' aObj:=TJSArray(a).concat(a);',
  11705. ' o:=TJSObject(a);',
  11706. ' aObj:=TJSArray([''bird'',''ant'']);',
  11707. '']);
  11708. ConvertProgram;
  11709. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11710. LinesToStr([ // statements
  11711. 'this.aObj = null;',
  11712. 'this.a = [];',
  11713. 'this.o = null;',
  11714. '']),
  11715. LinesToStr([ // $mod.$main
  11716. 'if (Array.isArray(65)) ;',
  11717. '$mod.aObj = $mod.a.concat($mod.a);',
  11718. '$mod.o = $mod.a;',
  11719. '$mod.aObj = ["bird", "ant"];',
  11720. '']));
  11721. end;
  11722. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11723. begin
  11724. StartProgram(false);
  11725. Add([
  11726. '{$modeswitch externalclass}',
  11727. 'type',
  11728. ' TArrStr = array of string;',
  11729. ' TJSArray = class external name ''Array''',
  11730. ' end;',
  11731. ' TJSObject = class external name ''Object''',
  11732. ' end;',
  11733. 'var',
  11734. ' aObj: TJSArray;',
  11735. ' a: TArrStr;',
  11736. ' jo: TJSObject;',
  11737. 'begin',
  11738. ' a:=TArrStr(aObj);',
  11739. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11740. ' a:=TarrStr(jo);',
  11741. '']);
  11742. ConvertProgram;
  11743. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11744. LinesToStr([ // statements
  11745. 'this.aObj = null;',
  11746. 'this.a = [];',
  11747. 'this.jo = null;',
  11748. '']),
  11749. LinesToStr([ // $mod.$main
  11750. '$mod.a = $mod.aObj;',
  11751. '$mod.aObj[1] = $mod.aObj[2];',
  11752. '$mod.a = $mod.jo;',
  11753. '']));
  11754. end;
  11755. procedure TTestModule.TestArrayOfConst_TVarRec;
  11756. begin
  11757. StartProgram(true,[supTVarRec]);
  11758. Add([
  11759. 'procedure Say(args: array of const);',
  11760. 'var',
  11761. ' i: longint;',
  11762. ' v: TVarRec;',
  11763. 'begin',
  11764. ' for i:=low(args) to high(args) do begin',
  11765. ' v:=args[i];',
  11766. ' case v.vtype of',
  11767. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11768. ' end;',
  11769. ' end;',
  11770. ' for v in args do ;',
  11771. ' args:=nil;',
  11772. ' SetLength(args,2);',
  11773. 'end;',
  11774. 'begin']);
  11775. ConvertProgram;
  11776. CheckSource('TestArrayOfConst_TVarRec',
  11777. LinesToStr([ // statements
  11778. 'this.Say = function (args) {',
  11779. ' var i = 0;',
  11780. ' var v = pas.system.TVarRec.$new();',
  11781. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11782. ' i = $l;',
  11783. ' v.$assign(args[i]);',
  11784. ' var $tmp = v.VType;',
  11785. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11786. ' };',
  11787. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11788. ' args = [];',
  11789. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11790. '};',
  11791. '']),
  11792. LinesToStr([ // $mod.$main
  11793. ]));
  11794. end;
  11795. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11796. begin
  11797. StartProgram(true,[supTVarRec]);
  11798. Add([
  11799. 'procedure Say(args: array of const);',
  11800. 'begin',
  11801. ' Say(args);',
  11802. 'end;',
  11803. 'var',
  11804. ' p: Pointer;',
  11805. ' j: jsvalue;',
  11806. ' c: currency;',
  11807. 'begin',
  11808. ' Say([]);',
  11809. ' Say([1]);',
  11810. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11811. '']);
  11812. ConvertProgram;
  11813. CheckSource('TestArrayOfConst_PassBaseTypes',
  11814. LinesToStr([ // statements
  11815. 'this.Say = function (args) {',
  11816. ' $mod.Say(args);',
  11817. '};',
  11818. 'this.p = null;',
  11819. 'this.j = undefined;',
  11820. 'this.c = 0;',
  11821. '']),
  11822. LinesToStr([ // $mod.$main
  11823. '$mod.Say([]);',
  11824. '$mod.Say(pas.system.VarRecs(0, 1));',
  11825. '$mod.Say(pas.system.VarRecs(',
  11826. ' 9,',
  11827. ' "c",',
  11828. ' 18,',
  11829. ' "foo",',
  11830. ' 5,',
  11831. ' null,',
  11832. ' 1,',
  11833. ' true,',
  11834. ' 3,',
  11835. ' 1.3,',
  11836. ' 5,',
  11837. ' $mod.p,',
  11838. ' 20,',
  11839. ' $mod.j,',
  11840. ' 12,',
  11841. ' $mod.c',
  11842. ' ));',
  11843. '']));
  11844. end;
  11845. procedure TTestModule.TestArrayOfConst_PassObj;
  11846. begin
  11847. StartProgram(true,[supTVarRec]);
  11848. Add([
  11849. '{$interfaces corba}',
  11850. 'type',
  11851. ' TObject = class',
  11852. ' end;',
  11853. ' TClass = class of TObject;',
  11854. ' IUnknown = interface',
  11855. ' end;',
  11856. 'procedure Say(args: array of const);',
  11857. 'begin',
  11858. 'end;',
  11859. 'var',
  11860. ' o: TObject;',
  11861. ' c: TClass;',
  11862. ' i: IUnknown;',
  11863. 'begin',
  11864. ' Say([o,c,TObject]);',
  11865. ' Say([nil,i]);',
  11866. '']);
  11867. ConvertProgram;
  11868. CheckSource('TestArrayOfConst_PassObj',
  11869. LinesToStr([ // statements
  11870. 'rtl.createClass(this, "TObject", null, function () {',
  11871. ' this.$init = function () {',
  11872. ' };',
  11873. ' this.$final = function () {',
  11874. ' };',
  11875. '});',
  11876. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11877. 'this.Say = function (args) {',
  11878. '};',
  11879. 'this.o = null;',
  11880. 'this.c = null;',
  11881. 'this.i = null;',
  11882. '']),
  11883. LinesToStr([ // $mod.$main
  11884. '$mod.Say(pas.system.VarRecs(',
  11885. ' 7,',
  11886. ' $mod.o,',
  11887. ' 8,',
  11888. ' $mod.c,',
  11889. ' 8,',
  11890. ' $mod.TObject',
  11891. '));',
  11892. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11893. '']));
  11894. end;
  11895. procedure TTestModule.TestRecord_Empty;
  11896. begin
  11897. StartProgram(false);
  11898. Add([
  11899. 'type',
  11900. ' TRecA = record',
  11901. ' end;',
  11902. 'var a,b: TRecA;',
  11903. 'begin',
  11904. ' if a=b then ;']);
  11905. ConvertProgram;
  11906. CheckSource('TestRecord_Empty',
  11907. LinesToStr([ // statements
  11908. 'rtl.recNewT(this, "TRecA", function () {',
  11909. ' this.$eq = function (b) {',
  11910. ' return true;',
  11911. ' };',
  11912. ' this.$assign = function (s) {',
  11913. ' return this;',
  11914. ' };',
  11915. '});',
  11916. 'this.a = this.TRecA.$new();',
  11917. 'this.b = this.TRecA.$new();',
  11918. '']),
  11919. LinesToStr([ // $mod.$main
  11920. 'if ($mod.a.$eq($mod.b)) ;'
  11921. ]));
  11922. end;
  11923. procedure TTestModule.TestRecord_Var;
  11924. begin
  11925. StartProgram(false);
  11926. Add('type');
  11927. Add(' TRecA = record');
  11928. Add(' Bold: longint;');
  11929. Add(' end;');
  11930. Add('var Rec: TRecA;');
  11931. Add('begin');
  11932. Add(' rec.bold:=123');
  11933. ConvertProgram;
  11934. CheckSource('TestRecord_Var',
  11935. LinesToStr([ // statements
  11936. 'rtl.recNewT(this, "TRecA", function () {',
  11937. ' this.Bold = 0;',
  11938. ' this.$eq = function (b) {',
  11939. ' return this.Bold === b.Bold;',
  11940. ' };',
  11941. ' this.$assign = function (s) {',
  11942. ' this.Bold = s.Bold;',
  11943. ' return this;',
  11944. ' };',
  11945. '});',
  11946. 'this.Rec = this.TRecA.$new();',
  11947. '']),
  11948. LinesToStr([ // $mod.$main
  11949. '$mod.Rec.Bold = 123;'
  11950. ]));
  11951. end;
  11952. procedure TTestModule.TestRecord_VarExternal;
  11953. begin
  11954. StartProgram(false);
  11955. Add([
  11956. '{$modeswitch externalclass}',
  11957. 'type',
  11958. ' TRecA = record',
  11959. ' i: byte;',
  11960. ' length_: longint external name ''length'';',
  11961. ' end;',
  11962. 'var Rec: TRecA;',
  11963. 'begin',
  11964. ' rec.length_ := rec.length_',
  11965. '']);
  11966. ConvertProgram;
  11967. CheckSource('TestRecord_VarExternal',
  11968. LinesToStr([ // statements
  11969. 'rtl.recNewT(this, "TRecA", function () {',
  11970. ' this.i = 0;',
  11971. ' this.$eq = function (b) {',
  11972. ' return (this.i === b.i) && (this.length === b.length);',
  11973. ' };',
  11974. ' this.$assign = function (s) {',
  11975. ' this.i = s.i;',
  11976. ' this.length = s.length;',
  11977. ' return this;',
  11978. ' };',
  11979. '});',
  11980. 'this.Rec = this.TRecA.$new();',
  11981. '']),
  11982. LinesToStr([ // $mod.$main
  11983. '$mod.Rec.length = $mod.Rec.length;'
  11984. ]));
  11985. end;
  11986. procedure TTestModule.TestRecord_WithDo;
  11987. begin
  11988. StartProgram(false);
  11989. Add('type');
  11990. Add(' TRec = record');
  11991. Add(' vI: longint;');
  11992. Add(' end;');
  11993. Add('var');
  11994. Add(' Int: longint;');
  11995. Add(' r: TRec;');
  11996. Add('begin');
  11997. Add(' with r do');
  11998. Add(' int:=vi;');
  11999. Add(' with r do begin');
  12000. Add(' int:=vi;');
  12001. Add(' vi:=int;');
  12002. Add(' end;');
  12003. ConvertProgram;
  12004. CheckSource('TestWithRecordDo',
  12005. LinesToStr([ // statements
  12006. 'rtl.recNewT(this, "TRec", function () {',
  12007. ' this.vI = 0;',
  12008. ' this.$eq = function (b) {',
  12009. ' return this.vI === b.vI;',
  12010. ' };',
  12011. ' this.$assign = function (s) {',
  12012. ' this.vI = s.vI;',
  12013. ' return this;',
  12014. ' };',
  12015. '});',
  12016. 'this.Int = 0;',
  12017. 'this.r = this.TRec.$new();',
  12018. '']),
  12019. LinesToStr([ // $mod.$main
  12020. 'var $with = $mod.r;',
  12021. '$mod.Int = $with.vI;',
  12022. 'var $with1 = $mod.r;',
  12023. '$mod.Int = $with1.vI;',
  12024. '$with1.vI = $mod.Int;'
  12025. ]));
  12026. end;
  12027. procedure TTestModule.TestRecord_Assign;
  12028. begin
  12029. StartProgram(false);
  12030. Add([
  12031. 'type',
  12032. ' TEnum = (red,green);',
  12033. ' TEnums = set of TEnum;',
  12034. ' TSmallRec = record',
  12035. ' N: longint;',
  12036. ' end;',
  12037. ' TBigRec = record',
  12038. ' Int: longint;',
  12039. ' D: double;',
  12040. ' Arr: array of longint;',
  12041. ' Arr2: array[1..2] of longint;',
  12042. ' Small: TSmallRec;',
  12043. ' Enums: TEnums;',
  12044. ' end;',
  12045. 'var',
  12046. ' r, s: TBigRec;',
  12047. 'begin',
  12048. ' r:=s;',
  12049. ' r:=default(TBigRec);',
  12050. ' r:=default(s);',
  12051. '']);
  12052. ConvertProgram;
  12053. CheckSource('TestRecord_Assign',
  12054. LinesToStr([ // statements
  12055. 'this.TEnum = {',
  12056. ' "0": "red",',
  12057. ' red: 0,',
  12058. ' "1": "green",',
  12059. ' green: 1',
  12060. '};',
  12061. 'rtl.recNewT(this, "TSmallRec", function () {',
  12062. ' this.N = 0;',
  12063. ' this.$eq = function (b) {',
  12064. ' return this.N === b.N;',
  12065. ' };',
  12066. ' this.$assign = function (s) {',
  12067. ' this.N = s.N;',
  12068. ' return this;',
  12069. ' };',
  12070. '});',
  12071. 'rtl.recNewT(this, "TBigRec", function () {',
  12072. ' this.Int = 0;',
  12073. ' this.D = 0.0;',
  12074. ' this.$new = function () {',
  12075. ' var r = Object.create(this);',
  12076. ' r.Arr = [];',
  12077. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12078. ' r.Small = $mod.TSmallRec.$new();',
  12079. ' r.Enums = {};',
  12080. ' return r;',
  12081. ' };',
  12082. ' this.$eq = function (b) {',
  12083. ' 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);',
  12084. ' };',
  12085. ' this.$assign = function (s) {',
  12086. ' this.Int = s.Int;',
  12087. ' this.D = s.D;',
  12088. ' this.Arr = rtl.arrayRef(s.Arr);',
  12089. ' this.Arr2 = s.Arr2.slice(0);',
  12090. ' this.Small.$assign(s.Small);',
  12091. ' this.Enums = rtl.refSet(s.Enums);',
  12092. ' return this;',
  12093. ' };',
  12094. '});',
  12095. 'this.r = this.TBigRec.$new();',
  12096. 'this.s = this.TBigRec.$new();',
  12097. '']),
  12098. LinesToStr([ // $mod.$main
  12099. '$mod.r.$assign($mod.s);',
  12100. '$mod.r.$assign($mod.TBigRec.$new());',
  12101. '$mod.r.$assign($mod.TBigRec.$new());',
  12102. '']));
  12103. end;
  12104. procedure TTestModule.TestRecord_AsParams;
  12105. begin
  12106. StartProgram(false);
  12107. Add([
  12108. 'type',
  12109. ' integer = longint;',
  12110. ' TRecord = record',
  12111. ' i: integer;',
  12112. ' end;',
  12113. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12114. 'var vL: TRecord;',
  12115. 'begin',
  12116. ' vd:=vd;',
  12117. ' vd.i:=vd.i;',
  12118. ' vl:=vc;',
  12119. ' vv:=vv;',
  12120. ' vv.i:=vv.i;',
  12121. ' U:=vl;',
  12122. ' U:=vd;',
  12123. ' U:=vc;',
  12124. ' U:=vv;',
  12125. ' vl:=TRecord(U);',
  12126. ' vd:=TRecord(U);',
  12127. ' vv:=TRecord(U);',
  12128. ' doit(vd,vd,vd,vd);',
  12129. ' doit(vc,vc,vl,vl);',
  12130. ' doit(vv,vv,vv,vv);',
  12131. ' doit(vl,vl,vl,vl);',
  12132. ' TRecord(U).i:=3;',
  12133. 'end;',
  12134. 'var i: TRecord;',
  12135. 'begin',
  12136. ' doit(i,i,i,i);',
  12137. '']);
  12138. ConvertProgram;
  12139. CheckSource('TestRecord_AsParams',
  12140. LinesToStr([ // statements
  12141. 'rtl.recNewT(this, "TRecord", function () {',
  12142. ' this.i = 0;',
  12143. ' this.$eq = function (b) {',
  12144. ' return this.i === b.i;',
  12145. ' };',
  12146. ' this.$assign = function (s) {',
  12147. ' this.i = s.i;',
  12148. ' return this;',
  12149. ' };',
  12150. '});',
  12151. 'this.DoIt = function (vD, vC, vV, U) {',
  12152. ' var vL = $mod.TRecord.$new();',
  12153. ' vD.$assign(vD);',
  12154. ' vD.i = vD.i;',
  12155. ' vL.$assign(vC);',
  12156. ' vV.$assign(vV);',
  12157. ' vV.i = vV.i;',
  12158. ' U.$assign(vL);',
  12159. ' U.$assign(vD);',
  12160. ' U.$assign(vC);',
  12161. ' U.$assign(vV);',
  12162. ' vL.$assign(U);',
  12163. ' vD.$assign(U);',
  12164. ' vV.$assign(U);',
  12165. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12166. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12167. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12168. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12169. ' U.i = 3;',
  12170. '};',
  12171. 'this.i = this.TRecord.$new();'
  12172. ]),
  12173. LinesToStr([
  12174. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12175. '']));
  12176. end;
  12177. procedure TTestModule.TestRecord_ConstRef;
  12178. begin
  12179. StartProgram(false);
  12180. Add([
  12181. 'type TRec = record i: word; end;',
  12182. 'procedure Run(constref a: TRec);',
  12183. 'begin',
  12184. 'end;',
  12185. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12186. 'var l: TRec;',
  12187. 'begin',
  12188. ' Run(l);',
  12189. ' Run(a);',
  12190. ' Run(b);',
  12191. ' Run(c);',
  12192. ' Run(d);',
  12193. ' Run(e);',
  12194. 'end;',
  12195. 'begin',
  12196. '']);
  12197. ConvertProgram;
  12198. CheckResolverUnexpectedHints();
  12199. CheckSource('TestRecord_ConstRef',
  12200. LinesToStr([ // statements
  12201. 'rtl.recNewT(this, "TRec", function () {',
  12202. ' this.i = 0;',
  12203. ' this.$eq = function (b) {',
  12204. ' return this.i === b.i;',
  12205. ' };',
  12206. ' this.$assign = function (s) {',
  12207. ' this.i = s.i;',
  12208. ' return this;',
  12209. ' };',
  12210. '});',
  12211. 'this.Run = function (a) {',
  12212. '};',
  12213. 'this.Fly = function (a, b, c, d, e) {',
  12214. ' var l = $mod.TRec.$new();',
  12215. ' $mod.Run(l);',
  12216. ' $mod.Run(a);',
  12217. ' $mod.Run(b);',
  12218. ' $mod.Run(c);',
  12219. ' $mod.Run(d);',
  12220. ' $mod.Run(e);',
  12221. '};',
  12222. '']),
  12223. LinesToStr([
  12224. '']));
  12225. end;
  12226. procedure TTestModule.TestRecordElement_AsParams;
  12227. begin
  12228. StartProgram(false);
  12229. Add('type');
  12230. Add(' integer = longint;');
  12231. Add(' TRecord = record');
  12232. Add(' i: integer;');
  12233. Add(' end;');
  12234. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12235. Add('var vJ: TRecord;');
  12236. Add('begin');
  12237. Add(' doit(vj.i,vj.i,vj.i);');
  12238. Add('end;');
  12239. Add('var r: TRecord;');
  12240. Add('begin');
  12241. Add(' doit(r.i,r.i,r.i);');
  12242. ConvertProgram;
  12243. CheckSource('TestRecordElement_AsParams',
  12244. LinesToStr([ // statements
  12245. 'rtl.recNewT(this, "TRecord", function () {',
  12246. ' this.i = 0;',
  12247. ' this.$eq = function (b) {',
  12248. ' return this.i === b.i;',
  12249. ' };',
  12250. ' this.$assign = function (s) {',
  12251. ' this.i = s.i;',
  12252. ' return this;',
  12253. ' };',
  12254. '});',
  12255. 'this.DoIt = function (vG,vH,vI) {',
  12256. ' var vJ = $mod.TRecord.$new();',
  12257. ' $mod.DoIt(vJ.i, vJ.i, {',
  12258. ' p: vJ,',
  12259. ' get: function () {',
  12260. ' return this.p.i;',
  12261. ' },',
  12262. ' set: function (v) {',
  12263. ' this.p.i = v;',
  12264. ' }',
  12265. ' });',
  12266. '};',
  12267. 'this.r = this.TRecord.$new();'
  12268. ]),
  12269. LinesToStr([
  12270. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12271. ' p: $mod.r,',
  12272. ' get: function () {',
  12273. ' return this.p.i;',
  12274. ' },',
  12275. ' set: function (v) {',
  12276. ' this.p.i = v;',
  12277. ' }',
  12278. '});'
  12279. ]));
  12280. end;
  12281. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12282. begin
  12283. StartProgram(false);
  12284. Add('type');
  12285. Add(' integer = longint;');
  12286. Add(' TRecord = record');
  12287. Add(' i: integer;');
  12288. Add(' end;');
  12289. Add('function GetRec(vB: integer = 0): TRecord;');
  12290. Add('begin');
  12291. Add('end;');
  12292. Add('procedure DoIt(vG: integer; const vH: integer);');
  12293. Add('begin');
  12294. Add('end;');
  12295. Add('begin');
  12296. Add(' doit(getrec.i,getrec.i);');
  12297. Add(' doit(getrec().i,getrec().i);');
  12298. Add(' doit(getrec(1).i,getrec(2).i);');
  12299. ConvertProgram;
  12300. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12301. LinesToStr([ // statements
  12302. 'rtl.recNewT(this, "TRecord", function () {',
  12303. ' this.i = 0;',
  12304. ' this.$eq = function (b) {',
  12305. ' return this.i === b.i;',
  12306. ' };',
  12307. ' this.$assign = function (s) {',
  12308. ' this.i = s.i;',
  12309. ' return this;',
  12310. ' };',
  12311. '});',
  12312. 'this.GetRec = function (vB) {',
  12313. ' var Result = $mod.TRecord.$new();',
  12314. ' return Result;',
  12315. '};',
  12316. 'this.DoIt = function (vG, vH) {',
  12317. '};',
  12318. '']),
  12319. LinesToStr([
  12320. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12321. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12322. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12323. '']));
  12324. end;
  12325. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12326. begin
  12327. StartProgram(false);
  12328. Add('type');
  12329. Add(' integer = longint;');
  12330. Add(' TRecord = record');
  12331. Add(' i: integer;');
  12332. Add(' end;');
  12333. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12334. Add('begin');
  12335. Add('end;');
  12336. Add('var r: trecord;');
  12337. Add('begin');
  12338. Add(' with r do ');
  12339. Add(' doit(i,i,i);');
  12340. ConvertProgram;
  12341. CheckSource('TestRecordElementFromWith_AsParams',
  12342. LinesToStr([ // statements
  12343. 'rtl.recNewT(this, "TRecord", function () {',
  12344. ' this.i = 0;',
  12345. ' this.$eq = function (b) {',
  12346. ' return this.i === b.i;',
  12347. ' };',
  12348. ' this.$assign = function (s) {',
  12349. ' this.i = s.i;',
  12350. ' return this;',
  12351. ' };',
  12352. '});',
  12353. 'this.DoIt = function (vG,vH,vI) {',
  12354. '};',
  12355. 'this.r = this.TRecord.$new();'
  12356. ]),
  12357. LinesToStr([
  12358. 'var $with = $mod.r;',
  12359. '$mod.DoIt($with.i,$with.i,{',
  12360. ' p: $with,',
  12361. ' get: function () {',
  12362. ' return this.p.i;',
  12363. ' },',
  12364. ' set: function (v) {',
  12365. ' this.p.i = v;',
  12366. ' }',
  12367. '});',
  12368. '']));
  12369. end;
  12370. procedure TTestModule.TestRecord_Equal;
  12371. begin
  12372. StartProgram(false);
  12373. Add('type');
  12374. Add(' integer = longint;');
  12375. Add(' TFlag = (red,blue);');
  12376. Add(' TFlags = set of TFlag;');
  12377. Add(' TProc = procedure;');
  12378. Add(' TRecord = record');
  12379. Add(' i: integer;');
  12380. Add(' Event: TProc;');
  12381. Add(' f: TFlags;');
  12382. Add(' end;');
  12383. Add(' TNested = record');
  12384. Add(' r: TRecord;');
  12385. Add(' end;');
  12386. Add('var');
  12387. Add(' b: boolean;');
  12388. Add(' r,s: trecord;');
  12389. Add('begin');
  12390. Add(' b:=r=s;');
  12391. Add(' b:=r<>s;');
  12392. ConvertProgram;
  12393. CheckSource('TestRecord_Equal',
  12394. LinesToStr([ // statements
  12395. 'this.TFlag = {',
  12396. ' "0": "red",',
  12397. ' red: 0,',
  12398. ' "1": "blue",',
  12399. ' blue: 1',
  12400. '};',
  12401. 'rtl.recNewT(this, "TRecord", function () {',
  12402. ' this.i = 0;',
  12403. ' this.Event = null;',
  12404. ' this.$new = function () {',
  12405. ' var r = Object.create(this);',
  12406. ' r.f = {};',
  12407. ' return r;',
  12408. ' };',
  12409. ' this.$eq = function (b) {',
  12410. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12411. ' };',
  12412. ' this.$assign = function (s) {',
  12413. ' this.i = s.i;',
  12414. ' this.Event = s.Event;',
  12415. ' this.f = rtl.refSet(s.f);',
  12416. ' return this;',
  12417. ' };',
  12418. '});',
  12419. 'rtl.recNewT(this, "TNested", function () {',
  12420. ' this.$new = function () {',
  12421. ' var r = Object.create(this);',
  12422. ' r.r = $mod.TRecord.$new();',
  12423. ' return r;',
  12424. ' };',
  12425. ' this.$eq = function (b) {',
  12426. ' return this.r.$eq(b.r);',
  12427. ' };',
  12428. ' this.$assign = function (s) {',
  12429. ' this.r.$assign(s.r);',
  12430. ' return this;',
  12431. ' };',
  12432. '});',
  12433. 'this.b = false;',
  12434. 'this.r = this.TRecord.$new();',
  12435. 'this.s = this.TRecord.$new();',
  12436. '']),
  12437. LinesToStr([
  12438. '$mod.b = $mod.r.$eq($mod.s);',
  12439. '$mod.b = !$mod.r.$eq($mod.s);',
  12440. '']));
  12441. end;
  12442. procedure TTestModule.TestRecord_JSValue;
  12443. begin
  12444. StartProgram(false);
  12445. Add([
  12446. 'type',
  12447. ' TRecord = record',
  12448. ' i: longint;',
  12449. ' end;',
  12450. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12451. 'begin',
  12452. 'end;',
  12453. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12454. 'begin',
  12455. ' if jsvalue(d) then ;',
  12456. ' if jsvalue(c) then ;',
  12457. ' if jsvalue(v) then ;',
  12458. 'end;',
  12459. 'var',
  12460. ' Jv: jsvalue;',
  12461. ' Rec: trecord;',
  12462. 'begin',
  12463. ' rec:=trecord(jv);',
  12464. ' jv:=rec;',
  12465. ' Fly(rec,rec);',
  12466. ' Fly(@rec,@rec);',
  12467. ' if jsvalue(Rec) then ;',
  12468. ' Run(trecord(jv),trecord(jv),rec);',
  12469. '']);
  12470. ConvertProgram;
  12471. CheckSource('TestRecord_JSValue',
  12472. LinesToStr([ // statements
  12473. 'rtl.recNewT(this, "TRecord", function () {',
  12474. ' this.i = 0;',
  12475. ' this.$eq = function (b) {',
  12476. ' return this.i === b.i;',
  12477. ' };',
  12478. ' this.$assign = function (s) {',
  12479. ' this.i = s.i;',
  12480. ' return this;',
  12481. ' };',
  12482. '});',
  12483. 'this.Fly = function (d, c) {',
  12484. '};',
  12485. 'this.Run = function (d, c, v) {',
  12486. ' if (d) ;',
  12487. ' if (c) ;',
  12488. ' if (v) ;',
  12489. '};',
  12490. 'this.Jv = undefined;',
  12491. 'this.Rec = this.TRecord.$new();',
  12492. '']),
  12493. LinesToStr([
  12494. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12495. '$mod.Jv = $mod.Rec;',
  12496. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12497. '$mod.Fly($mod.Rec, $mod.Rec);',
  12498. 'if ($mod.Rec) ;',
  12499. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12500. '']));
  12501. end;
  12502. procedure TTestModule.TestRecord_VariantFail;
  12503. begin
  12504. StartProgram(false);
  12505. Add([
  12506. 'type',
  12507. ' TRec = record',
  12508. ' case word of',
  12509. ' 0: (b0, b1: Byte);',
  12510. ' 1: (i: word);',
  12511. ' end;',
  12512. 'begin']);
  12513. SetExpectedPasResolverError('Not supported: variant record',
  12514. nNotSupportedX);
  12515. ConvertProgram;
  12516. end;
  12517. procedure TTestModule.TestRecord_FieldArray;
  12518. begin
  12519. StartProgram(false);
  12520. Add([
  12521. 'type',
  12522. ' TArrInt = array[3..4] of longint;',
  12523. ' TArrArrInt = array[3..4] of longint;',
  12524. ' TRec = record',
  12525. ' a: array of longint;',
  12526. ' s: array[1..2] of longint;',
  12527. ' m: array[1..2,3..4] of longint;',
  12528. ' o: TArrArrInt;',
  12529. ' end;',
  12530. 'begin']);
  12531. ConvertProgram;
  12532. CheckSource('TestRecord_FieldArray',
  12533. LinesToStr([ // statements
  12534. 'rtl.recNewT(this, "TRec", function () {',
  12535. ' this.m$a$clone = function (a) {',
  12536. ' var b = [];',
  12537. ' b.length = 2;',
  12538. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12539. ' return b;',
  12540. ' };',
  12541. ' this.$new = function () {',
  12542. ' var r = Object.create(this);',
  12543. ' r.a = [];',
  12544. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12545. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12546. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12547. ' return r;',
  12548. ' };',
  12549. ' this.$eq = function (b) {',
  12550. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12551. ' };',
  12552. ' this.$assign = function (s) {',
  12553. ' this.a = rtl.arrayRef(s.a);',
  12554. ' this.s = s.s.slice(0);',
  12555. ' this.m = this.m$a$clone(s.m);',
  12556. ' this.o = s.o.slice(0);',
  12557. ' return this;',
  12558. ' };',
  12559. '});',
  12560. '']),
  12561. LinesToStr([ // $mod.$main
  12562. '']));
  12563. end;
  12564. procedure TTestModule.TestRecord_Const;
  12565. begin
  12566. StartProgram(false);
  12567. Add([
  12568. 'type',
  12569. ' TArrInt = array[3..4] of longint;',
  12570. ' TPoint = record x,y: longint; end;',
  12571. ' TRec = record',
  12572. ' i: longint;',
  12573. ' a: array of longint;',
  12574. ' s: array[1..2] of longint;',
  12575. ' m: array[1..2,3..4] of longint;',
  12576. ' p: TPoint;',
  12577. ' end;',
  12578. ' TPoints = array of TPoint;',
  12579. 'const',
  12580. ' r: TRec = (',
  12581. ' i:1;',
  12582. ' a:(2,3);',
  12583. ' s:(4,5);',
  12584. ' m:( (11,12), (13,14) );',
  12585. ' p: (x:21; y:22)',
  12586. ' );',
  12587. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12588. 'begin']);
  12589. ConvertProgram;
  12590. CheckSource('TestRecord_Const',
  12591. LinesToStr([ // statements
  12592. 'rtl.recNewT(this, "TPoint", function () {',
  12593. ' this.x = 0;',
  12594. ' this.y = 0;',
  12595. ' this.$eq = function (b) {',
  12596. ' return (this.x === b.x) && (this.y === b.y);',
  12597. ' };',
  12598. ' this.$assign = function (s) {',
  12599. ' this.x = s.x;',
  12600. ' this.y = s.y;',
  12601. ' return this;',
  12602. ' };',
  12603. '});',
  12604. 'rtl.recNewT(this, "TRec", function () {',
  12605. ' this.i = 0;',
  12606. ' this.m$a$clone = function (a) {',
  12607. ' var b = [];',
  12608. ' b.length = 2;',
  12609. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12610. ' return b;',
  12611. ' };',
  12612. ' this.$new = function () {',
  12613. ' var r = Object.create(this);',
  12614. ' r.a = [];',
  12615. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12616. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12617. ' r.p = $mod.TPoint.$new();',
  12618. ' return r;',
  12619. ' };',
  12620. ' this.$eq = function (b) {',
  12621. ' 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);',
  12622. ' };',
  12623. ' this.$assign = function (s) {',
  12624. ' this.i = s.i;',
  12625. ' this.a = rtl.arrayRef(s.a);',
  12626. ' this.s = s.s.slice(0);',
  12627. ' this.m = this.m$a$clone(s.m);',
  12628. ' this.p.$assign(s.p);',
  12629. ' return this;',
  12630. ' };',
  12631. '});',
  12632. 'this.r = this.TRec.$clone({',
  12633. ' i: 1,',
  12634. ' a: [2, 3],',
  12635. ' s: [4, 5],',
  12636. ' m: [[11, 12], [13, 14]],',
  12637. ' p: this.TPoint.$clone({',
  12638. ' x: 21,',
  12639. ' y: 22',
  12640. ' })',
  12641. '});',
  12642. 'this.p = [this.TPoint.$clone({',
  12643. ' x: 1,',
  12644. ' y: 2',
  12645. '}), this.TPoint.$clone({',
  12646. ' x: 3,',
  12647. ' y: 4',
  12648. '})];',
  12649. '']),
  12650. LinesToStr([ // $mod.$main
  12651. '']));
  12652. end;
  12653. procedure TTestModule.TestRecord_TypecastFail;
  12654. begin
  12655. StartProgram(false);
  12656. Add([
  12657. 'type',
  12658. ' TPoint = record x,y: longint; end;',
  12659. ' TRec = record l: longint end;',
  12660. 'var p: TPoint;',
  12661. 'begin',
  12662. ' if TRec(p).l=2 then ;']);
  12663. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12664. nIllegalTypeConversionTo);
  12665. ConvertProgram;
  12666. end;
  12667. procedure TTestModule.TestRecord_InFunction;
  12668. begin
  12669. StartProgram(false);
  12670. Add([
  12671. 'var TPoint: longint = 3;',
  12672. 'procedure DoIt;',
  12673. 'type',
  12674. ' TPoint = record x,y: longint; end;',
  12675. ' TPoints = array of TPoint;',
  12676. 'var',
  12677. ' r: TPoint;',
  12678. ' p: TPoints;',
  12679. 'begin',
  12680. ' SetLength(p,2);',
  12681. 'end;',
  12682. 'begin']);
  12683. ConvertProgram;
  12684. CheckSource('TestRecord_InFunction',
  12685. LinesToStr([ // statements
  12686. 'this.TPoint = 3;',
  12687. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12688. ' this.x = 0;',
  12689. ' this.y = 0;',
  12690. ' this.$eq = function (b) {',
  12691. ' return (this.x === b.x) && (this.y === b.y);',
  12692. ' };',
  12693. ' this.$assign = function (s) {',
  12694. ' this.x = s.x;',
  12695. ' this.y = s.y;',
  12696. ' return this;',
  12697. ' };',
  12698. '});',
  12699. 'this.DoIt = function () {',
  12700. ' var r = TPoint$1.$new();',
  12701. ' var p = [];',
  12702. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12703. '};',
  12704. '']),
  12705. LinesToStr([ // $mod.$main
  12706. '']));
  12707. end;
  12708. procedure TTestModule.TestRecordAnonym_Field;
  12709. begin
  12710. StartProgram(false);
  12711. Add(['',
  12712. 'var Rec: record',
  12713. ' Bold: longint;',
  12714. ' end;',
  12715. 'begin',
  12716. ' rec.bold:=123;',
  12717. ' rec.bold:=rec.bold+7;',
  12718. '']);
  12719. ConvertProgram;
  12720. CheckSource('TestRecordAnonym_Field',
  12721. LinesToStr([ // statements
  12722. 'rtl.recNewT(this, "Rec$a", function () {',
  12723. ' this.Bold = 0;',
  12724. ' this.$eq = function (b) {',
  12725. ' return this.Bold === b.Bold;',
  12726. ' };',
  12727. ' this.$assign = function (s) {',
  12728. ' this.Bold = s.Bold;',
  12729. ' return this;',
  12730. ' };',
  12731. '});',
  12732. 'this.Rec = this.Rec$a.$new();',
  12733. '']),
  12734. LinesToStr([ // $mod.$main
  12735. '$mod.Rec.Bold = 123;',
  12736. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12737. '']));
  12738. end;
  12739. procedure TTestModule.TestRecordAnonym_Assign;
  12740. begin
  12741. StartProgram(false);
  12742. Add(['',
  12743. 'var S,T: record',
  12744. ' Bold: longint;',
  12745. ' end;',
  12746. ' b: boolean;',
  12747. 'begin',
  12748. ' S:=T;',
  12749. ' b:=s=t;',
  12750. '']);
  12751. ConvertProgram;
  12752. CheckSource('TestRecordAnonym_Assign',
  12753. LinesToStr([ // statements
  12754. 'rtl.recNewT(this, "T$a", function () {',
  12755. ' this.Bold = 0;',
  12756. ' this.$eq = function (b) {',
  12757. ' return this.Bold === b.Bold;',
  12758. ' };',
  12759. ' this.$assign = function (s) {',
  12760. ' this.Bold = s.Bold;',
  12761. ' return this;',
  12762. ' };',
  12763. '});',
  12764. 'this.S = this.T$a.$new();',
  12765. 'this.T = this.T$a.$new();',
  12766. 'this.b = false;',
  12767. '']),
  12768. LinesToStr([ // $mod.$main
  12769. '$mod.S.$assign($mod.T);',
  12770. '$mod.b = $mod.S.$eq($mod.T);',
  12771. '']));
  12772. end;
  12773. procedure TTestModule.TestRecordAnonym_Nested;
  12774. begin
  12775. StartProgram(false);
  12776. Add(['',
  12777. 'var S,T: record',
  12778. ' Bold: longint;',
  12779. ' Sub: record',
  12780. ' Color: word;',
  12781. ' end;',
  12782. ' end;',
  12783. ' b: boolean;',
  12784. 'begin',
  12785. ' S:=T;',
  12786. ' S.Sub:=T.Sub;',
  12787. ' S.Sub.Color:=T.Sub.Color+3;',
  12788. ' b:=s=t;',
  12789. ' b:=s.Sub=t.Sub;',
  12790. '']);
  12791. ConvertProgram;
  12792. CheckSource('TestRecordAnonym_Nested',
  12793. LinesToStr([ // statements
  12794. 'rtl.recNewT(this, "T$a", function () {',
  12795. ' this.Bold = 0;',
  12796. ' rtl.recNewT(this, "Sub$a", function () {',
  12797. ' this.Color = 0;',
  12798. ' this.$eq = function (b) {',
  12799. ' return this.Color === b.Color;',
  12800. ' };',
  12801. ' this.$assign = function (s) {',
  12802. ' this.Color = s.Color;',
  12803. ' return this;',
  12804. ' };',
  12805. ' });',
  12806. ' this.$new = function () {',
  12807. ' var r = Object.create(this);',
  12808. ' r.Sub = this.Sub$a.$new();',
  12809. ' return r;',
  12810. ' };',
  12811. ' this.$eq = function (b) {',
  12812. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12813. ' };',
  12814. ' this.$assign = function (s) {',
  12815. ' this.Bold = s.Bold;',
  12816. ' this.Sub.$assign(s.Sub);',
  12817. ' return this;',
  12818. ' };',
  12819. '}, true);',
  12820. 'this.S = this.T$a.$new();',
  12821. 'this.T = this.T$a.$new();',
  12822. 'this.b = false;',
  12823. '']),
  12824. LinesToStr([ // $mod.$main
  12825. '$mod.S.$assign($mod.T);',
  12826. '$mod.S.Sub.$assign($mod.T.Sub);',
  12827. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12828. '$mod.b = $mod.S.$eq($mod.T);',
  12829. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12830. '']));
  12831. end;
  12832. procedure TTestModule.TestRecordAnonym_Const;
  12833. begin
  12834. StartProgram(false);
  12835. Add(['',
  12836. 'var T: record',
  12837. ' Bold: longint;',
  12838. ' Sub: record',
  12839. ' Color: word;',
  12840. ' end;',
  12841. ' end = (Bold: 2; Sub: (Color: 3));',
  12842. 'begin',
  12843. '']);
  12844. ConvertProgram;
  12845. CheckSource('TestRecordAnonym_Const',
  12846. LinesToStr([ // statements
  12847. 'rtl.recNewT(this, "T$a", function () {',
  12848. ' this.Bold = 0;',
  12849. ' rtl.recNewT(this, "Sub$a", function () {',
  12850. ' this.Color = 0;',
  12851. ' this.$eq = function (b) {',
  12852. ' return this.Color === b.Color;',
  12853. ' };',
  12854. ' this.$assign = function (s) {',
  12855. ' this.Color = s.Color;',
  12856. ' return this;',
  12857. ' };',
  12858. ' });',
  12859. ' this.$new = function () {',
  12860. ' var r = Object.create(this);',
  12861. ' r.Sub = this.Sub$a.$new();',
  12862. ' return r;',
  12863. ' };',
  12864. ' this.$eq = function (b) {',
  12865. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12866. ' };',
  12867. ' this.$assign = function (s) {',
  12868. ' this.Bold = s.Bold;',
  12869. ' this.Sub.$assign(s.Sub);',
  12870. ' return this;',
  12871. ' };',
  12872. '}, true);',
  12873. 'this.T = this.T$a.$clone({',
  12874. ' Bold: 2,',
  12875. ' Sub: this.T$a.Sub$a.$clone({',
  12876. ' Color: 3',
  12877. ' })',
  12878. '});',
  12879. '']),
  12880. LinesToStr([ // $mod.$main
  12881. '']));
  12882. end;
  12883. procedure TTestModule.TestRecordAnonym_InFunction;
  12884. begin
  12885. StartProgram(false);
  12886. Add(['',
  12887. 'procedure Fly;',
  12888. 'var T: record',
  12889. ' Bold: longint;',
  12890. ' Sub: record',
  12891. ' Color: word;',
  12892. ' end;',
  12893. ' end = (Bold: 2; Sub: (Color: 3));',
  12894. 'begin',
  12895. 'end;',
  12896. 'begin',
  12897. '']);
  12898. ConvertProgram;
  12899. CheckSource('TestRecordAnonym_InFunction',
  12900. LinesToStr([ // statements
  12901. 'var T$a = rtl.recNewT(null, "", function () {',
  12902. ' this.Bold = 0;',
  12903. ' rtl.recNewT(this, "Sub$a", function () {',
  12904. ' this.Color = 0;',
  12905. ' this.$eq = function (b) {',
  12906. ' return this.Color === b.Color;',
  12907. ' };',
  12908. ' this.$assign = function (s) {',
  12909. ' this.Color = s.Color;',
  12910. ' return this;',
  12911. ' };',
  12912. ' });',
  12913. ' this.$new = function () {',
  12914. ' var r = Object.create(this);',
  12915. ' r.Sub = this.Sub$a.$new();',
  12916. ' return r;',
  12917. ' };',
  12918. ' this.$eq = function (b) {',
  12919. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12920. ' };',
  12921. ' this.$assign = function (s) {',
  12922. ' this.Bold = s.Bold;',
  12923. ' this.Sub.$assign(s.Sub);',
  12924. ' return this;',
  12925. ' };',
  12926. '}, true);',
  12927. 'this.Fly = function () {',
  12928. ' var T = T$a.$clone({',
  12929. ' Bold: 2,',
  12930. ' Sub: T$a.Sub$a.$clone({',
  12931. ' Color: 3',
  12932. ' })',
  12933. ' });',
  12934. '};',
  12935. '']),
  12936. LinesToStr([ // $mod.$main
  12937. '']));
  12938. end;
  12939. procedure TTestModule.TestAdvRecord_Function;
  12940. begin
  12941. StartProgram(false);
  12942. Parser.Options:=Parser.Options+[po_cassignments];
  12943. Add([
  12944. '{$modeswitch AdvancedRecords}',
  12945. 'type',
  12946. ' TPoint = record',
  12947. ' x,y: word;',
  12948. ' function Add(const apt: TPoint): TPoint;',
  12949. ' end;',
  12950. 'function TPoint.Add(const apt: TPoint): TPoint;',
  12951. 'begin',
  12952. ' Result:=Self;',
  12953. ' Result.x+=apt.x;',
  12954. ' Result.y:=Result.y+apt.y;',
  12955. ' Self:=apt;',
  12956. 'end;',
  12957. 'var p,q: TPoint;',
  12958. 'begin',
  12959. ' p.add(q);',
  12960. ' p:=default(TPoint);',
  12961. ' p:=q;',
  12962. '']);
  12963. ConvertProgram;
  12964. CheckSource('TestAdvRecord_Function',
  12965. LinesToStr([ // statements
  12966. 'rtl.recNewT(this, "TPoint", function () {',
  12967. ' this.x = 0;',
  12968. ' this.y = 0;',
  12969. ' this.$eq = function (b) {',
  12970. ' return (this.x === b.x) && (this.y === b.y);',
  12971. ' };',
  12972. ' this.$assign = function (s) {',
  12973. ' this.x = s.x;',
  12974. ' this.y = s.y;',
  12975. ' return this;',
  12976. ' };',
  12977. ' this.Add = function (apt) {',
  12978. ' var Result = $mod.TPoint.$new();',
  12979. ' Result.$assign(this);',
  12980. ' Result.x += apt.x;',
  12981. ' Result.y = Result.y + apt.y;',
  12982. ' this.$assign(apt);',
  12983. ' return Result;',
  12984. ' };',
  12985. '});',
  12986. 'this.p = this.TPoint.$new();',
  12987. 'this.q = this.TPoint.$new();',
  12988. '']),
  12989. LinesToStr([ // $mod.$main
  12990. '$mod.p.Add($mod.q);',
  12991. '$mod.p.$assign($mod.TPoint.$new());',
  12992. '$mod.p.$assign($mod.q);',
  12993. '']));
  12994. end;
  12995. procedure TTestModule.TestAdvRecord_Property;
  12996. begin
  12997. StartProgram(false);
  12998. Add([
  12999. '{$modeswitch AdvancedRecords}',
  13000. 'type',
  13001. ' TPoint = record',
  13002. ' x,y: word;',
  13003. ' strict private',
  13004. ' function GetSize: longword;',
  13005. ' procedure SetSize(Value: longword);',
  13006. ' public',
  13007. ' property Size: longword read GetSize write SetSize;',
  13008. ' property Left: word read x write y;',
  13009. ' end;',
  13010. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13011. 'function TPoint.GetSize: longword;',
  13012. 'begin',
  13013. ' x:=y;',
  13014. ' Size:=Size;',
  13015. ' Left:=Left;',
  13016. 'end;',
  13017. 'procedure TPoint.SetSize(Value: longword);',
  13018. 'begin',
  13019. 'end;',
  13020. 'var p,q: TPoint;',
  13021. 'begin',
  13022. ' p.Size:=q.Size;',
  13023. ' p.Left:=q.Left;',
  13024. '']);
  13025. ConvertProgram;
  13026. CheckSource('TestAdvRecord_Property',
  13027. LinesToStr([ // statements
  13028. 'rtl.recNewT(this, "TPoint", function () {',
  13029. ' this.x = 0;',
  13030. ' this.y = 0;',
  13031. ' this.$eq = function (b) {',
  13032. ' return (this.x === b.x) && (this.y === b.y);',
  13033. ' };',
  13034. ' this.$assign = function (s) {',
  13035. ' this.x = s.x;',
  13036. ' this.y = s.y;',
  13037. ' return this;',
  13038. ' };',
  13039. ' this.GetSize = function () {',
  13040. ' var Result = 0;',
  13041. ' this.x = this.y;',
  13042. ' this.SetSize(this.GetSize());',
  13043. ' this.y = this.x;',
  13044. ' return Result;',
  13045. ' };',
  13046. ' this.SetSize = function (Value) {',
  13047. ' };',
  13048. '});',
  13049. 'this.SetSize = function (Value) {',
  13050. '};',
  13051. 'this.p = this.TPoint.$new();',
  13052. 'this.q = this.TPoint.$new();',
  13053. '']),
  13054. LinesToStr([ // $mod.$main
  13055. '$mod.p.SetSize($mod.q.GetSize());',
  13056. '$mod.p.y = $mod.q.x;',
  13057. '']));
  13058. end;
  13059. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13060. begin
  13061. StartProgram(false);
  13062. Add([
  13063. '{$modeswitch AdvancedRecords}',
  13064. 'type',
  13065. ' TPoint = record',
  13066. ' strict private',
  13067. ' function GetItems(Index: word): word;',
  13068. ' procedure SetItems(Index: word; Value: word);',
  13069. ' public',
  13070. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13071. ' end;',
  13072. 'function TPoint.GetItems(Index: word): word;',
  13073. 'begin',
  13074. ' Items[index]:=Items[index];',
  13075. ' self.Items[index]:=self.Items[index];',
  13076. 'end;',
  13077. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13078. 'begin',
  13079. 'end;',
  13080. 'var p: TPoint;',
  13081. 'begin',
  13082. ' p[1]:=p[2];',
  13083. ' p.Items[3]:=p.Items[4];',
  13084. '']);
  13085. ConvertProgram;
  13086. CheckSource('TestAdvRecord_PropertyDefault',
  13087. LinesToStr([ // statements
  13088. 'rtl.recNewT(this, "TPoint", function () {',
  13089. ' this.$eq = function (b) {',
  13090. ' return true;',
  13091. ' };',
  13092. ' this.$assign = function (s) {',
  13093. ' return this;',
  13094. ' };',
  13095. ' this.GetItems = function (Index) {',
  13096. ' var Result = 0;',
  13097. ' this.SetItems(Index, this.GetItems(Index));',
  13098. ' this.SetItems(Index, this.GetItems(Index));',
  13099. ' return Result;',
  13100. ' };',
  13101. ' this.SetItems = function (Index, Value) {',
  13102. ' };',
  13103. '});',
  13104. 'this.p = this.TPoint.$new();',
  13105. '']),
  13106. LinesToStr([ // $mod.$main
  13107. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13108. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13109. '']));
  13110. end;
  13111. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13112. begin
  13113. StartProgram(false);
  13114. Add([
  13115. '{$modeswitch AdvancedRecords}',
  13116. 'type',
  13117. ' TRec = record',
  13118. ' class var',
  13119. ' Fx: longint;',
  13120. ' Fy: longint;',
  13121. ' class function GetInt: longint; static;',
  13122. ' class procedure SetInt(Value: longint); static;',
  13123. ' class procedure DoIt; static;',
  13124. ' class property IntA: longint read Fx write Fy;',
  13125. ' class property IntB: longint read GetInt write SetInt;',
  13126. ' end;',
  13127. 'class function trec.getint: longint;',
  13128. 'begin',
  13129. ' result:=fx;',
  13130. 'end;',
  13131. 'class procedure trec.setint(value: longint);',
  13132. 'begin',
  13133. 'end;',
  13134. 'class procedure trec.doit;',
  13135. 'begin',
  13136. ' IntA:=IntA+1;',
  13137. ' IntB:=IntB+1;',
  13138. 'end;',
  13139. 'var r: trec;',
  13140. 'begin',
  13141. ' trec.inta:=trec.inta+1;',
  13142. ' if trec.intb=2 then;',
  13143. ' trec.intb:=trec.intb+2;',
  13144. ' trec.setint(trec.inta);',
  13145. ' r.inta:=r.inta+1;',
  13146. ' if r.intb=2 then;',
  13147. ' r.intb:=r.intb+2;',
  13148. ' r.setint(r.inta);']);
  13149. ConvertProgram;
  13150. CheckSource('TestAdvRecord_Property_ClassMethod',
  13151. LinesToStr([ // statements
  13152. 'rtl.recNewT(this, "TRec", function () {',
  13153. ' this.Fx = 0;',
  13154. ' this.Fy = 0;',
  13155. ' this.$eq = function (b) {',
  13156. ' return true;',
  13157. ' };',
  13158. ' this.$assign = function (s) {',
  13159. ' return this;',
  13160. ' };',
  13161. ' this.GetInt = function () {',
  13162. ' var Result = 0;',
  13163. ' Result = $mod.TRec.Fx;',
  13164. ' return Result;',
  13165. ' };',
  13166. ' this.SetInt = function (Value) {',
  13167. ' };',
  13168. ' this.DoIt = function () {',
  13169. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13170. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13171. ' };',
  13172. '}, true);',
  13173. 'this.r = this.TRec.$new();',
  13174. '']),
  13175. LinesToStr([ // $mod.$main
  13176. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13177. 'if ($mod.TRec.GetInt() === 2) ;',
  13178. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13179. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13180. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13181. 'if ($mod.TRec.GetInt() === 2) ;',
  13182. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13183. '$mod.TRec.SetInt($mod.r.Fx);',
  13184. '']));
  13185. end;
  13186. procedure TTestModule.TestAdvRecord_Const;
  13187. begin
  13188. StartProgram(false);
  13189. Add([
  13190. '{$modeswitch AdvancedRecords}',
  13191. 'type',
  13192. ' TArrInt = array[3..4] of longint;',
  13193. ' TPoint = record',
  13194. ' x,y: longint;',
  13195. ' class var Count: nativeint;',
  13196. ' end;',
  13197. ' TRec = record',
  13198. ' i: longint;',
  13199. ' a: array of longint;',
  13200. ' s: array[1..2] of longint;',
  13201. ' m: array[1..2,3..4] of longint;',
  13202. ' p: TPoint;',
  13203. ' end;',
  13204. ' TPoints = array of TPoint;',
  13205. 'const',
  13206. ' r: TRec = (',
  13207. ' i:1;',
  13208. ' a:(2,3);',
  13209. ' s:(4,5);',
  13210. ' m:( (11,12), (13,14) );',
  13211. ' p: (x:21)',
  13212. ' );',
  13213. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13214. 'begin']);
  13215. ConvertProgram;
  13216. CheckSource('TestAdvRecord_Const',
  13217. LinesToStr([ // statements
  13218. 'rtl.recNewT(this, "TPoint", function () {',
  13219. ' this.x = 0;',
  13220. ' this.y = 0;',
  13221. ' this.Count = 0;',
  13222. ' this.$eq = function (b) {',
  13223. ' return (this.x === b.x) && (this.y === b.y);',
  13224. ' };',
  13225. ' this.$assign = function (s) {',
  13226. ' this.x = s.x;',
  13227. ' this.y = s.y;',
  13228. ' return this;',
  13229. ' };',
  13230. '}, true);',
  13231. 'rtl.recNewT(this, "TRec", function () {',
  13232. ' this.i = 0;',
  13233. ' this.m$a$clone = function (a) {',
  13234. ' var b = [];',
  13235. ' b.length = 2;',
  13236. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13237. ' return b;',
  13238. ' };',
  13239. ' this.$new = function () {',
  13240. ' var r = Object.create(this);',
  13241. ' r.a = [];',
  13242. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13243. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13244. ' r.p = $mod.TPoint.$new();',
  13245. ' return r;',
  13246. ' };',
  13247. ' this.$eq = function (b) {',
  13248. ' 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);',
  13249. ' };',
  13250. ' this.$assign = function (s) {',
  13251. ' this.i = s.i;',
  13252. ' this.a = rtl.arrayRef(s.a);',
  13253. ' this.s = s.s.slice(0);',
  13254. ' this.m = this.m$a$clone(s.m);',
  13255. ' this.p.$assign(s.p);',
  13256. ' return this;',
  13257. ' };',
  13258. '});',
  13259. 'this.r = this.TRec.$clone({',
  13260. ' i: 1,',
  13261. ' a: [2, 3],',
  13262. ' s: [4, 5],',
  13263. ' m: [[11, 12], [13, 14]],',
  13264. ' p: this.TPoint.$clone({',
  13265. ' x: 21,',
  13266. ' y: 0',
  13267. ' })',
  13268. '});',
  13269. 'this.p = [this.TPoint.$clone({',
  13270. ' x: 1,',
  13271. ' y: 2',
  13272. '}), this.TPoint.$clone({',
  13273. ' x: 3,',
  13274. ' y: 4',
  13275. '})];',
  13276. '']),
  13277. LinesToStr([ // $mod.$main
  13278. '']));
  13279. end;
  13280. procedure TTestModule.TestAdvRecord_ExternalField;
  13281. begin
  13282. StartProgram(false);
  13283. Add([
  13284. '{$modeswitch AdvancedRecords}',
  13285. '{$modeswitch externalclass}',
  13286. 'type',
  13287. ' TCar = record',
  13288. ' public',
  13289. ' Intern: longint external name ''$Intern'';',
  13290. ' Intern2: longint external name ''$Intern2'';',
  13291. ' Bracket: longint external name ''["A B"]'';',
  13292. ' procedure DoIt;',
  13293. ' end;',
  13294. 'procedure tcar.doit;',
  13295. 'begin',
  13296. ' Intern:=Intern+1;',
  13297. ' Intern2:=Intern2+2;',
  13298. ' Bracket:=Bracket+3;',
  13299. 'end;',
  13300. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13301. 'begin',
  13302. ' Rec.intern:=Rec.intern+1;',
  13303. ' Rec.intern2:=Rec.intern2+2;',
  13304. ' Rec.Bracket:=Rec.Bracket+3;',
  13305. ' with Rec do begin',
  13306. ' intern:=intern+1;',
  13307. ' intern2:=intern2+2;',
  13308. ' Bracket:=Bracket+3;',
  13309. ' end;']);
  13310. ConvertProgram;
  13311. CheckSource('TestAdvRecord_ExternalField',
  13312. LinesToStr([ // statements
  13313. 'rtl.recNewT(this, "TCar", function () {',
  13314. ' this.$eq = function (b) {',
  13315. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13316. ' };',
  13317. ' this.$assign = function (s) {',
  13318. ' this.$Intern = s.$Intern;',
  13319. ' this.$Intern2 = s.$Intern2;',
  13320. ' this["A B"] = s["A B"];',
  13321. ' return this;',
  13322. ' };',
  13323. ' this.DoIt = function () {',
  13324. ' this.$Intern = this.$Intern + 1;',
  13325. ' this.$Intern2 = this.$Intern2 + 2;',
  13326. ' this["A B"] = this["A B"] + 3;',
  13327. ' };',
  13328. '});',
  13329. 'this.Rec = this.TCar.$clone({',
  13330. ' $Intern: 11,',
  13331. ' $Intern2: 12,',
  13332. ' "A B": 13',
  13333. '});',
  13334. '']),
  13335. LinesToStr([ // $mod.$main
  13336. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13337. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13338. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13339. 'var $with = $mod.Rec;',
  13340. '$with.$Intern = $with.$Intern + 1;',
  13341. '$with.$Intern2 = $with.$Intern2 + 2;',
  13342. '$with["A B"] = $with["A B"] + 3;',
  13343. '']));
  13344. end;
  13345. procedure TTestModule.TestAdvRecord_SubRecord;
  13346. begin
  13347. StartProgram(false);
  13348. Add([
  13349. '{$modeswitch AdvancedRecords}',
  13350. 'type',
  13351. ' TRec = record',
  13352. ' type',
  13353. ' TPoint = record',
  13354. ' x,y: longint;',
  13355. ' class var Count: nativeint;',
  13356. ' procedure DoIt;',
  13357. ' class procedure DoThat; static;',
  13358. ' end;',
  13359. ' var',
  13360. ' i: longint;',
  13361. ' p: TPoint;',
  13362. ' procedure DoSome;',
  13363. ' end;',
  13364. 'const',
  13365. ' r: TRec = (',
  13366. ' i:1;',
  13367. ' p: (x:21;y:22)',
  13368. ' );',
  13369. 'procedure TRec.DoSome;',
  13370. 'begin',
  13371. ' p.x:=p.y+1;',
  13372. ' p.Count:=p.Count+2;',
  13373. 'end;',
  13374. 'procedure TRec.TPoint.DoIt;',
  13375. 'begin',
  13376. ' Count:=Count+3;',
  13377. 'end;',
  13378. 'class procedure TRec.TPoint.DoThat;',
  13379. 'begin',
  13380. ' Count:=Count+4;',
  13381. 'end;',
  13382. 'begin']);
  13383. ConvertProgram;
  13384. CheckSource('TestAdvRecord_SubRecord',
  13385. LinesToStr([ // statements
  13386. 'rtl.recNewT(this, "TRec", function () {',
  13387. ' rtl.recNewT(this, "TPoint", function () {',
  13388. ' this.x = 0;',
  13389. ' this.y = 0;',
  13390. ' this.Count = 0;',
  13391. ' this.$eq = function (b) {',
  13392. ' return (this.x === b.x) && (this.y === b.y);',
  13393. ' };',
  13394. ' this.$assign = function (s) {',
  13395. ' this.x = s.x;',
  13396. ' this.y = s.y;',
  13397. ' return this;',
  13398. ' };',
  13399. ' this.DoIt = function () {',
  13400. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13401. ' };',
  13402. ' this.DoThat = function () {',
  13403. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13404. ' };',
  13405. ' }, true);',
  13406. ' this.i = 0;',
  13407. ' this.$new = function () {',
  13408. ' var r = Object.create(this);',
  13409. ' r.p = this.TPoint.$new();',
  13410. ' return r;',
  13411. ' };',
  13412. ' this.$eq = function (b) {',
  13413. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13414. ' };',
  13415. ' this.$assign = function (s) {',
  13416. ' this.i = s.i;',
  13417. ' this.p.$assign(s.p);',
  13418. ' return this;',
  13419. ' };',
  13420. ' this.DoSome = function () {',
  13421. ' this.p.x = this.p.y + 1;',
  13422. ' this.TPoint.Count = this.p.Count + 2;',
  13423. ' };',
  13424. '}, true);',
  13425. 'this.r = this.TRec.$clone({',
  13426. ' i: 1,',
  13427. ' p: this.TRec.TPoint.$clone({',
  13428. ' x: 21,',
  13429. ' y: 22',
  13430. ' })',
  13431. '});',
  13432. '']),
  13433. LinesToStr([ // $mod.$main
  13434. '']));
  13435. end;
  13436. procedure TTestModule.TestAdvRecord_SubClass;
  13437. begin
  13438. StartProgram(false);
  13439. Add([
  13440. '{$modeswitch AdvancedRecords}',
  13441. 'type',
  13442. ' TObject = class end;',
  13443. ' TPoint = record',
  13444. ' type',
  13445. ' TBird = class',
  13446. ' procedure DoIt;',
  13447. ' class procedure Glob;',
  13448. ' end;',
  13449. ' procedure DoIt(b: TBird);',
  13450. ' end;',
  13451. 'procedure TPoint.TBird.DoIt;',
  13452. 'begin',
  13453. ' doit;',
  13454. ' self.doit;',
  13455. ' glob;',
  13456. ' self.glob;',
  13457. 'end;',
  13458. 'class procedure TPoint.TBird.Glob;',
  13459. 'begin',
  13460. ' glob;',
  13461. ' self.glob;',
  13462. 'end;',
  13463. 'procedure TPoint.DoIt(b: TBird);',
  13464. 'begin',
  13465. ' b.doit;',
  13466. ' b.glob;',
  13467. ' TBird.glob;',
  13468. 'end;',
  13469. 'begin',
  13470. '']);
  13471. ConvertProgram;
  13472. CheckSource('TestAdvRecord_SubClass',
  13473. LinesToStr([ // statements
  13474. 'rtl.createClass(this, "TObject", null, function () {',
  13475. ' this.$init = function () {',
  13476. ' };',
  13477. ' this.$final = function () {',
  13478. ' };',
  13479. '});',
  13480. 'rtl.recNewT(this, "TPoint", function () {',
  13481. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13482. ' this.DoIt = function () {',
  13483. ' this.DoIt();',
  13484. ' this.DoIt();',
  13485. ' this.$class.Glob();',
  13486. ' this.$class.Glob();',
  13487. ' };',
  13488. ' this.Glob = function () {',
  13489. ' this.Glob();',
  13490. ' this.Glob();',
  13491. ' };',
  13492. ' }, "TPoint.TBird");',
  13493. ' this.$eq = function (b) {',
  13494. ' return true;',
  13495. ' };',
  13496. ' this.$assign = function (s) {',
  13497. ' return this;',
  13498. ' };',
  13499. ' this.DoIt = function (b) {',
  13500. ' b.DoIt();',
  13501. ' b.$class.Glob();',
  13502. ' this.TBird.Glob();',
  13503. ' };',
  13504. '}, true);',
  13505. '']),
  13506. LinesToStr([ // $mod.$main
  13507. '']));
  13508. end;
  13509. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13510. begin
  13511. StartProgram(false);
  13512. Add([
  13513. '{$modeswitch AdvancedRecords}',
  13514. 'type',
  13515. ' IUnknown = interface end;',
  13516. ' TPoint = record',
  13517. ' type IBird = interface end;',
  13518. ' end;',
  13519. 'begin',
  13520. '']);
  13521. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13522. nNotYetImplemented);
  13523. ParseProgram;
  13524. end;
  13525. procedure TTestModule.TestAdvRecord_Constructor;
  13526. begin
  13527. StartProgram(false);
  13528. Add([
  13529. '{$modeswitch AdvancedRecords}',
  13530. 'type',
  13531. ' TPoint = record',
  13532. ' x,y: longint;',
  13533. ' class procedure Run(w: longint = 13); static;',
  13534. ' constructor Create(ax: longint; ay: longint = -1);',
  13535. ' end;',
  13536. 'class procedure tpoint.run(w: longint);',
  13537. 'begin',
  13538. ' run;',
  13539. ' run();',
  13540. 'end;',
  13541. 'constructor tpoint.create(ax,ay: longint);',
  13542. 'begin',
  13543. ' x:=ax;',
  13544. ' self.y:=ay;',
  13545. ' run;',
  13546. ' run(ax);',
  13547. 'end;',
  13548. 'var r: TPoint;',
  13549. 'begin',
  13550. ' r:=TPoint.Create(1,2);',
  13551. ' with TPoint do r:=Create(1,2);',
  13552. ' r.Create(3);',
  13553. ' r:=r.Create(4);',
  13554. '']);
  13555. ConvertProgram;
  13556. CheckSource('TestAdvRecord_Constructor',
  13557. LinesToStr([ // statements
  13558. 'rtl.recNewT(this, "TPoint", function () {',
  13559. ' this.x = 0;',
  13560. ' this.y = 0;',
  13561. ' this.$eq = function (b) {',
  13562. ' return (this.x === b.x) && (this.y === b.y);',
  13563. ' };',
  13564. ' this.$assign = function (s) {',
  13565. ' this.x = s.x;',
  13566. ' this.y = s.y;',
  13567. ' return this;',
  13568. ' };',
  13569. ' this.Run = function (w) {',
  13570. ' $mod.TPoint.Run(13);',
  13571. ' $mod.TPoint.Run(13);',
  13572. ' };',
  13573. ' this.Create = function (ax, ay) {',
  13574. ' this.x = ax;',
  13575. ' this.y = ay;',
  13576. ' this.Run(13);',
  13577. ' this.Run(ax);',
  13578. ' return this;',
  13579. ' };',
  13580. '});',
  13581. 'this.r = this.TPoint.$new();',
  13582. '']),
  13583. LinesToStr([ // $mod.$main
  13584. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13585. 'var $with = $mod.TPoint;',
  13586. '$mod.r.$assign($with.$new().Create(1, 2));',
  13587. '$mod.r.Create(3, -1);',
  13588. '$mod.r.$assign($mod.r.Create(4, -1));',
  13589. '']));
  13590. end;
  13591. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13592. begin
  13593. StartProgram(false);
  13594. Add([
  13595. '{$modeswitch AdvancedRecords}',
  13596. 'type',
  13597. ' TPoint = record',
  13598. ' class var x: longint;',
  13599. ' class procedure Fly; static;',
  13600. ' class constructor Init;',
  13601. ' end;',
  13602. 'var count: word;',
  13603. 'class procedure Tpoint.Fly;',
  13604. 'begin',
  13605. 'end;',
  13606. 'class constructor tpoint.init;',
  13607. 'begin',
  13608. ' count:=count+1;',
  13609. ' x:=x+3;',
  13610. ' tpoint.x:=tpoint.x+4;',
  13611. ' fly;',
  13612. ' tpoint.fly;',
  13613. 'end;',
  13614. 'var r: TPoint;',
  13615. 'begin',
  13616. ' r.x:=r.x+10;',
  13617. ' r.Fly;',
  13618. ' r.Fly();',
  13619. '']);
  13620. ConvertProgram;
  13621. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13622. LinesToStr([ // statements
  13623. 'rtl.recNewT(this, "TPoint", function () {',
  13624. ' this.x = 0;',
  13625. ' this.$eq = function (b) {',
  13626. ' return true;',
  13627. ' };',
  13628. ' this.$assign = function (s) {',
  13629. ' return this;',
  13630. ' };',
  13631. ' this.Fly = function () {',
  13632. ' };',
  13633. '}, true);',
  13634. 'this.count = 0;',
  13635. 'this.r = this.TPoint.$new();',
  13636. '']),
  13637. LinesToStr([ // $mod.$main
  13638. '(function () {',
  13639. ' $mod.count = $mod.count + 1;',
  13640. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13641. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13642. ' $mod.TPoint.Fly();',
  13643. ' $mod.TPoint.Fly();',
  13644. '})();',
  13645. '$mod.TPoint.x = $mod.r.x + 10;',
  13646. '$mod.TPoint.Fly();',
  13647. '$mod.TPoint.Fly();',
  13648. '']));
  13649. end;
  13650. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13651. begin
  13652. StartUnit(false);
  13653. Add([
  13654. 'interface',
  13655. '{$modeswitch AdvancedRecords}',
  13656. 'type',
  13657. ' TPoint = record',
  13658. ' class var x: longint;',
  13659. ' class procedure Fly; static;',
  13660. ' class constructor Init;',
  13661. ' end;',
  13662. 'implementation',
  13663. 'var count: word;',
  13664. 'class procedure Tpoint.Fly;',
  13665. 'begin',
  13666. 'end;',
  13667. 'class constructor tpoint.init;',
  13668. 'begin',
  13669. ' count:=count+1;',
  13670. ' x:=3;',
  13671. ' tpoint.x:=4;',
  13672. ' fly;',
  13673. ' tpoint.fly;',
  13674. 'end;',
  13675. '']);
  13676. ConvertUnit;
  13677. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13678. LinesToStr([ // statements
  13679. 'var $impl = $mod.$impl;',
  13680. 'rtl.recNewT(this, "TPoint", function () {',
  13681. ' this.x = 0;',
  13682. ' this.$eq = function (b) {',
  13683. ' return true;',
  13684. ' };',
  13685. ' this.$assign = function (s) {',
  13686. ' return this;',
  13687. ' };',
  13688. ' this.Fly = function () {',
  13689. ' };',
  13690. '}, true);',
  13691. '']),
  13692. LinesToStr([ // $mod.$init
  13693. '(function () {',
  13694. ' $impl.count = $impl.count + 1;',
  13695. ' $mod.TPoint.x = 3;',
  13696. ' $mod.TPoint.x = 4;',
  13697. ' $mod.TPoint.Fly();',
  13698. ' $mod.TPoint.Fly();',
  13699. '})();',
  13700. '']),
  13701. LinesToStr([ // $mod.$main
  13702. '$impl.count = 0;',
  13703. '']));
  13704. end;
  13705. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13706. begin
  13707. StartProgram(false);
  13708. Add(['type',
  13709. ' TObject = class',
  13710. ' public',
  13711. ' constructor Create;',
  13712. ' destructor Destroy;',
  13713. ' end;',
  13714. ' TBird = TObject;',
  13715. 'constructor tobject.create;',
  13716. 'begin end;',
  13717. 'destructor tobject.destroy;',
  13718. 'begin end;',
  13719. 'var Obj: tobject;',
  13720. 'begin',
  13721. ' obj:=tobject.create;',
  13722. ' obj:=tobject.create();',
  13723. ' obj:=tbird.create;',
  13724. ' obj:=tbird.create();',
  13725. ' obj:=obj.create();',
  13726. ' obj.destroy;',
  13727. '']);
  13728. ConvertProgram;
  13729. CheckSource('TestClass_TObjectDefaultConstructor',
  13730. LinesToStr([ // statements
  13731. 'rtl.createClass(this,"TObject",null,function(){',
  13732. ' this.$init = function () {',
  13733. ' };',
  13734. ' this.$final = function () {',
  13735. ' };',
  13736. ' this.Create = function(){',
  13737. ' return this;',
  13738. ' };',
  13739. ' this.Destroy = function(){',
  13740. ' };',
  13741. '});',
  13742. 'this.Obj = null;'
  13743. ]),
  13744. LinesToStr([ // $mod.$main
  13745. '$mod.Obj = $mod.TObject.$create("Create");',
  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.Obj.Create();',
  13750. '$mod.Obj.$destroy("Destroy");',
  13751. '']));
  13752. end;
  13753. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13754. begin
  13755. StartProgram(false);
  13756. Add('type');
  13757. Add(' TObject = class');
  13758. Add(' public');
  13759. Add(' constructor Create(Par: longint);');
  13760. Add(' end;');
  13761. Add('constructor tobject.create(par: longint);');
  13762. Add('begin end;');
  13763. Add('var Obj: tobject;');
  13764. Add('begin');
  13765. Add(' obj:=tobject.create(3);');
  13766. ConvertProgram;
  13767. CheckSource('TestClass_TObjectConstructorWithParams',
  13768. LinesToStr([ // statements
  13769. 'rtl.createClass(this,"TObject",null,function(){',
  13770. ' this.$init = function () {',
  13771. ' };',
  13772. ' this.$final = function () {',
  13773. ' };',
  13774. ' this.Create = function(Par){',
  13775. ' return this;',
  13776. ' };',
  13777. '});',
  13778. 'this.Obj = null;'
  13779. ]),
  13780. LinesToStr([ // $mod.$main
  13781. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13782. ]));
  13783. end;
  13784. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13785. begin
  13786. StartProgram(false);
  13787. Add('type');
  13788. Add(' TObject = class');
  13789. Add(' public');
  13790. Add(' constructor Create;');
  13791. Add(' end;');
  13792. Add(' TTest = class(TObject)');
  13793. Add(' public');
  13794. Add(' constructor Create(const Par: longint = 1);');
  13795. Add(' end;');
  13796. Add('constructor tobject.create;');
  13797. Add('begin end;');
  13798. Add('constructor ttest.create(const par: longint);');
  13799. Add('begin end;');
  13800. Add('var t: ttest;');
  13801. Add('begin');
  13802. Add(' t:=ttest.create;');
  13803. Add(' t:=ttest.create(2);');
  13804. ConvertProgram;
  13805. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13806. LinesToStr([ // statements
  13807. 'rtl.createClass(this,"TObject",null,function(){',
  13808. ' this.$init = function () {',
  13809. ' };',
  13810. ' this.$final = function () {',
  13811. ' };',
  13812. ' this.Create = function(){',
  13813. ' return this;',
  13814. ' };',
  13815. '});',
  13816. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13817. ' this.Create$1 = function (Par) {',
  13818. ' return this;',
  13819. ' };',
  13820. '});',
  13821. 'this.t = null;'
  13822. ]),
  13823. LinesToStr([ // $mod.$main
  13824. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13825. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13826. ]));
  13827. end;
  13828. procedure TTestModule.TestClass_Var;
  13829. begin
  13830. StartProgram(false);
  13831. Add([
  13832. 'type',
  13833. ' TObject = class',
  13834. ' public',
  13835. ' vI: longint;',
  13836. ' constructor Create(Par: longint);',
  13837. ' end;',
  13838. 'constructor tobject.create(par: longint);',
  13839. 'begin',
  13840. ' vi:=par+3',
  13841. 'end;',
  13842. 'var Obj: tobject;',
  13843. 'begin',
  13844. ' obj:=tobject.create(4);',
  13845. ' obj.vi:=obj.VI+5;']);
  13846. ConvertProgram;
  13847. CheckSource('TestClass_Var',
  13848. LinesToStr([ // statements
  13849. 'rtl.createClass(this,"TObject",null,function(){',
  13850. ' this.$init = function () {',
  13851. ' this.vI = 0;',
  13852. ' };',
  13853. ' this.$final = function () {',
  13854. ' };',
  13855. ' this.Create = function(Par){',
  13856. ' this.vI = Par+3;',
  13857. ' return this;',
  13858. ' };',
  13859. '});',
  13860. 'this.Obj = null;'
  13861. ]),
  13862. LinesToStr([ // $mod.$main
  13863. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13864. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13865. ]));
  13866. end;
  13867. procedure TTestModule.TestClass_Method;
  13868. begin
  13869. StartProgram(false);
  13870. Add('type');
  13871. Add(' TObject = class');
  13872. Add(' public');
  13873. Add(' vI: longint;');
  13874. Add(' Sub: TObject;');
  13875. Add(' constructor Create;');
  13876. Add(' function GetIt(Par: longint): tobject;');
  13877. Add(' end;');
  13878. Add('constructor tobject.create; begin end;');
  13879. Add('function tobject.getit(par: longint): tobject;');
  13880. Add('begin');
  13881. Add(' Self.vi:=par+3;');
  13882. Add(' Result:=self.sub;');
  13883. Add('end;');
  13884. Add('var Obj: tobject;');
  13885. Add('begin');
  13886. Add(' obj:=tobject.create;');
  13887. Add(' obj.getit(4);');
  13888. Add(' obj.sub.sub:=nil;');
  13889. Add(' obj.sub.getit(5);');
  13890. Add(' obj.sub.getit(6).SUB:=nil;');
  13891. Add(' obj.sub.getit(7).GETIT(8);');
  13892. Add(' obj.sub.getit(9).SuB.getit(10);');
  13893. ConvertProgram;
  13894. CheckSource('TestClass_Method',
  13895. LinesToStr([ // statements
  13896. 'rtl.createClass(this,"TObject",null,function(){',
  13897. ' this.$init = function () {',
  13898. ' this.vI = 0;',
  13899. ' this.Sub = null;',
  13900. ' };',
  13901. ' this.$final = function () {',
  13902. ' this.Sub = undefined;',
  13903. ' };',
  13904. ' this.Create = function(){',
  13905. ' return this;',
  13906. ' };',
  13907. ' this.GetIt = function(Par){',
  13908. ' var Result = null;',
  13909. ' this.vI = Par + 3;',
  13910. ' Result = this.Sub;',
  13911. ' return Result;',
  13912. ' };',
  13913. '});',
  13914. 'this.Obj = null;'
  13915. ]),
  13916. LinesToStr([ // $mod.$main
  13917. '$mod.Obj = $mod.TObject.$create("Create");',
  13918. '$mod.Obj.GetIt(4);',
  13919. '$mod.Obj.Sub.Sub=null;',
  13920. '$mod.Obj.Sub.GetIt(5);',
  13921. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13922. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13923. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13924. ]));
  13925. end;
  13926. procedure TTestModule.TestClass_Implementation;
  13927. begin
  13928. StartUnit(false);
  13929. Add([
  13930. 'interface',
  13931. 'type',
  13932. ' TObject = class',
  13933. ' constructor Create;',
  13934. ' end;',
  13935. 'implementation',
  13936. 'type',
  13937. ' TIntClass = class',
  13938. ' constructor Create; reintroduce;',
  13939. ' class procedure DoGlob;',
  13940. ' end;',
  13941. 'constructor tintclass.create;',
  13942. 'begin',
  13943. ' inherited;',
  13944. ' inherited create;',
  13945. ' doglob;',
  13946. 'end;',
  13947. 'class procedure tintclass.doglob;',
  13948. 'begin',
  13949. 'end;',
  13950. 'constructor tobject.create;',
  13951. 'var',
  13952. ' iC: tintclass;',
  13953. 'begin',
  13954. ' ic:=tintclass.create;',
  13955. ' tintclass.doglob;',
  13956. ' ic.doglob;',
  13957. 'end;',
  13958. 'initialization',
  13959. ' tintclass.doglob;',
  13960. '']);
  13961. ConvertUnit;
  13962. CheckSource('TestClass_Implementation',
  13963. LinesToStr([ // statements
  13964. 'var $impl = $mod.$impl;',
  13965. 'rtl.createClass(this, "TObject", null, function () {',
  13966. ' this.$init = function () {',
  13967. ' };',
  13968. ' this.$final = function () {',
  13969. ' };',
  13970. ' this.Create = function () {',
  13971. ' var iC = null;',
  13972. ' iC = $impl.TIntClass.$create("Create$1");',
  13973. ' $impl.TIntClass.DoGlob();',
  13974. ' iC.$class.DoGlob();',
  13975. ' return this;',
  13976. ' };',
  13977. '});',
  13978. '']),
  13979. LinesToStr([ // $mod.$main
  13980. '$impl.TIntClass.DoGlob();',
  13981. '']),
  13982. LinesToStr([
  13983. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  13984. ' this.Create$1 = function () {',
  13985. ' $mod.TObject.Create.call(this);',
  13986. ' $mod.TObject.Create.call(this);',
  13987. ' this.$class.DoGlob();',
  13988. ' return this;',
  13989. ' };',
  13990. ' this.DoGlob = function () {',
  13991. ' };',
  13992. '});',
  13993. '']));
  13994. end;
  13995. procedure TTestModule.TestClass_Inheritance;
  13996. begin
  13997. StartProgram(false);
  13998. Add('type');
  13999. Add(' TObject = class');
  14000. Add(' public');
  14001. Add(' constructor Create;');
  14002. Add(' end;');
  14003. Add(' TClassA = class');
  14004. Add(' end;');
  14005. Add(' TClassB = class(TObject)');
  14006. Add(' procedure ProcB;');
  14007. Add(' end;');
  14008. Add('constructor tobject.create; begin end;');
  14009. Add('procedure tclassb.procb; begin end;');
  14010. Add('var');
  14011. Add(' oO: TObject;');
  14012. Add(' oA: TClassA;');
  14013. Add(' oB: TClassB;');
  14014. Add('begin');
  14015. Add(' oO:=tobject.Create;');
  14016. Add(' oA:=tclassa.Create;');
  14017. Add(' ob:=tclassb.Create;');
  14018. Add(' if oo is tclassa then ;');
  14019. Add(' ob:=oo as tclassb;');
  14020. Add(' (oo as tclassb).procb;');
  14021. ConvertProgram;
  14022. CheckSource('TestClass_Inheritance',
  14023. LinesToStr([ // statements
  14024. 'rtl.createClass(this,"TObject",null,function(){',
  14025. ' this.$init = function () {',
  14026. ' };',
  14027. ' this.$final = function () {',
  14028. ' };',
  14029. ' this.Create = function () {',
  14030. ' return this;',
  14031. ' };',
  14032. '});',
  14033. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14034. '});',
  14035. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14036. ' this.ProcB = function () {',
  14037. ' };',
  14038. '});',
  14039. 'this.oO = null;',
  14040. 'this.oA = null;',
  14041. 'this.oB = null;'
  14042. ]),
  14043. LinesToStr([ // $mod.$main
  14044. '$mod.oO = $mod.TObject.$create("Create");',
  14045. '$mod.oA = $mod.TClassA.$create("Create");',
  14046. '$mod.oB = $mod.TClassB.$create("Create");',
  14047. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14048. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14049. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14050. ]));
  14051. end;
  14052. procedure TTestModule.TestClass_TypeAlias;
  14053. begin
  14054. StartProgram(false);
  14055. Add([
  14056. '{$interfaces corba}',
  14057. 'type',
  14058. ' IObject = interface',
  14059. ' end;',
  14060. ' IBird = type IObject;',
  14061. ' TObject = class',
  14062. ' end;',
  14063. ' TBird = type TObject;',
  14064. 'var',
  14065. ' oObj: TObject;',
  14066. ' oBird: TBird;',
  14067. ' IntfObj: IObject;',
  14068. ' IntfBird: IBird;',
  14069. 'begin',
  14070. ' oObj:=oBird;',
  14071. '']);
  14072. ConvertProgram;
  14073. CheckSource('TestClass_TypeAlias',
  14074. LinesToStr([ // statements
  14075. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14076. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14077. 'rtl.createClass(this, "TObject", null, function () {',
  14078. ' this.$init = function () {',
  14079. ' };',
  14080. ' this.$final = function () {',
  14081. ' };',
  14082. '});',
  14083. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14084. '});',
  14085. 'this.oObj = null;',
  14086. 'this.oBird = null;',
  14087. 'this.IntfObj = null;',
  14088. 'this.IntfBird = null;',
  14089. '']),
  14090. LinesToStr([ // $mod.$main
  14091. '$mod.oObj = $mod.oBird;',
  14092. '']));
  14093. end;
  14094. procedure TTestModule.TestClass_AbstractMethod;
  14095. begin
  14096. StartProgram(false);
  14097. Add('type');
  14098. Add(' TObject = class');
  14099. Add(' public');
  14100. Add(' procedure DoIt; virtual; abstract;');
  14101. Add(' end;');
  14102. Add('begin');
  14103. ConvertProgram;
  14104. CheckSource('TestClass_AbstractMethod',
  14105. LinesToStr([ // statements
  14106. 'rtl.createClass(this,"TObject",null,function(){',
  14107. ' this.$init = function () {',
  14108. ' };',
  14109. ' this.$final = function () {',
  14110. ' };',
  14111. '});'
  14112. ]),
  14113. LinesToStr([ // this.$main
  14114. ''
  14115. ]));
  14116. end;
  14117. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14118. begin
  14119. StartProgram(false);
  14120. Add([
  14121. 'type',
  14122. ' TObject = class',
  14123. ' procedure DoAbstract; virtual; abstract;',
  14124. ' procedure DoVirtual; virtual;',
  14125. ' procedure DoIt;',
  14126. ' end;',
  14127. ' TA = class',
  14128. ' procedure doabstract; override;',
  14129. ' procedure dovirtual; override;',
  14130. ' procedure DoSome;',
  14131. ' end;',
  14132. 'procedure tobject.dovirtual;',
  14133. 'begin',
  14134. ' inherited; // call non existing ancestor -> ignore silently',
  14135. 'end;',
  14136. 'procedure tobject.doit;',
  14137. 'begin',
  14138. 'end;',
  14139. 'procedure ta.doabstract;',
  14140. 'begin',
  14141. ' inherited dovirtual; // call TObject.DoVirtual',
  14142. 'end;',
  14143. 'procedure ta.dovirtual;',
  14144. 'begin',
  14145. ' inherited; // call TObject.DoVirtual',
  14146. ' inherited dovirtual; // call TObject.DoVirtual',
  14147. ' inherited dovirtual(); // call TObject.DoVirtual',
  14148. ' doit;',
  14149. ' doit();',
  14150. 'end;',
  14151. 'procedure ta.dosome;',
  14152. 'begin',
  14153. ' inherited; // call non existing ancestor method -> silently ignore',
  14154. 'end;',
  14155. 'begin']);
  14156. ConvertProgram;
  14157. CheckSource('TestClass_CallInherited_ProcNoParams',
  14158. LinesToStr([ // statements
  14159. 'rtl.createClass(this,"TObject",null,function(){',
  14160. ' this.$init = function () {',
  14161. ' };',
  14162. ' this.$final = function () {',
  14163. ' };',
  14164. ' this.DoVirtual = function () {',
  14165. ' };',
  14166. ' this.DoIt = function () {',
  14167. ' };',
  14168. '});',
  14169. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14170. ' this.DoAbstract = function () {',
  14171. ' $mod.TObject.DoVirtual.call(this);',
  14172. ' };',
  14173. ' this.DoVirtual = function () {',
  14174. ' $mod.TObject.DoVirtual.call(this);',
  14175. ' $mod.TObject.DoVirtual.call(this);',
  14176. ' $mod.TObject.DoVirtual.call(this);',
  14177. ' this.DoIt();',
  14178. ' this.DoIt();',
  14179. ' };',
  14180. ' this.DoSome = function () {',
  14181. ' };',
  14182. '});'
  14183. ]),
  14184. LinesToStr([ // this.$main
  14185. ''
  14186. ]));
  14187. end;
  14188. procedure TTestModule.TestClass_CallInherited_WithParams;
  14189. begin
  14190. StartProgram(false);
  14191. Add([
  14192. 'type',
  14193. ' TObject = class',
  14194. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14195. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14196. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14197. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14198. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14199. ' end;',
  14200. ' TClassA = class',
  14201. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14202. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14203. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14204. ' end;',
  14205. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14206. 'begin',
  14207. 'end;',
  14208. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14209. 'begin',
  14210. 'end;',
  14211. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14212. 'begin',
  14213. 'end;',
  14214. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14215. 'begin',
  14216. 'end;',
  14217. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14218. 'begin',
  14219. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14220. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14221. 'end;',
  14222. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14223. 'begin',
  14224. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14225. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14226. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14227. ' doit(pa,pb);',
  14228. ' doit(pa);',
  14229. ' doit2(pa);',
  14230. ' doit2;',
  14231. 'end;',
  14232. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14233. 'begin',
  14234. ' pa:=inherited;',
  14235. 'end;',
  14236. 'begin']);
  14237. ConvertProgram;
  14238. CheckSource('TestClass_CallInherited_WithParams',
  14239. LinesToStr([ // statements
  14240. 'rtl.createClass(this,"TObject",null,function(){',
  14241. ' this.$init = function () {',
  14242. ' };',
  14243. ' this.$final = function () {',
  14244. ' };',
  14245. ' this.DoVirtual = function (pA,pB) {',
  14246. ' };',
  14247. ' this.DoIt = function (pA,pB) {',
  14248. ' };',
  14249. ' this.DoIt2 = function (pA,pB) {',
  14250. ' };',
  14251. ' this.GetIt = function (pA, pB) {',
  14252. ' var Result = 0;',
  14253. ' return Result;',
  14254. ' };',
  14255. '});',
  14256. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14257. ' this.DoAbstract = function (pA,pB) {',
  14258. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14259. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14260. ' };',
  14261. ' this.DoVirtual = function (pA,pB) {',
  14262. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14263. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14264. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14265. ' this.DoIt(pA,pB);',
  14266. ' this.DoIt(pA,0);',
  14267. ' this.DoIt2(pA,2);',
  14268. ' this.DoIt2(1,2);',
  14269. ' };',
  14270. ' this.GetIt$1 = function (pA, pB) {',
  14271. ' var Result = 0;',
  14272. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14273. ' return Result;',
  14274. ' };',
  14275. '});'
  14276. ]),
  14277. LinesToStr([ // this.$main
  14278. ''
  14279. ]));
  14280. end;
  14281. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14282. begin
  14283. StartProgram(false);
  14284. Add('type');
  14285. Add(' TObject = class');
  14286. Add(' constructor Create; virtual;');
  14287. Add(' constructor CreateWithB(b: boolean);');
  14288. Add(' end;');
  14289. Add(' TA = class');
  14290. Add(' constructor Create; override;');
  14291. Add(' constructor CreateWithC(c: char);');
  14292. Add(' procedure DoIt;');
  14293. Add(' class function DoSome: TObject;');
  14294. Add(' end;');
  14295. Add('constructor tobject.create;');
  14296. Add('begin');
  14297. Add(' inherited; // call non existing ancestor -> ignore silently');
  14298. Add('end;');
  14299. Add('constructor tobject.createwithb(b: boolean);');
  14300. Add('begin');
  14301. Add(' inherited; // call non existing ancestor -> ignore silently');
  14302. Add(' create; // normal call');
  14303. Add('end;');
  14304. Add('constructor ta.create;');
  14305. Add('begin');
  14306. Add(' inherited; // normal call TObject.Create');
  14307. Add(' inherited create; // normal call TObject.Create');
  14308. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14309. Add('end;');
  14310. Add('constructor ta.createwithc(c: char);');
  14311. Add('begin');
  14312. Add(' inherited create; // call TObject.Create');
  14313. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14314. Add(' doit;');
  14315. Add(' doit();');
  14316. Add(' dosome;');
  14317. Add('end;');
  14318. Add('procedure ta.doit;');
  14319. Add('begin');
  14320. Add(' create; // normal call');
  14321. Add(' createwithb(false); // normal call');
  14322. Add(' createwithc(''c''); // normal call');
  14323. Add('end;');
  14324. Add('class function ta.dosome: TObject;');
  14325. Add('begin');
  14326. Add(' Result:=create; // constructor');
  14327. Add(' Result:=createwithb(true); // constructor');
  14328. Add(' Result:=createwithc(''c''); // constructor');
  14329. Add('end;');
  14330. Add('begin');
  14331. ConvertProgram;
  14332. CheckSource('TestClass_CallInheritedConstructor',
  14333. LinesToStr([ // statements
  14334. 'rtl.createClass(this,"TObject",null,function(){',
  14335. ' this.$init = function () {',
  14336. ' };',
  14337. ' this.$final = function () {',
  14338. ' };',
  14339. ' this.Create = function () {',
  14340. ' return this;',
  14341. ' };',
  14342. ' this.CreateWithB = function (b) {',
  14343. ' this.Create();',
  14344. ' return this;',
  14345. ' };',
  14346. '});',
  14347. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14348. ' this.Create = function () {',
  14349. ' $mod.TObject.Create.call(this);',
  14350. ' $mod.TObject.Create.call(this);',
  14351. ' $mod.TObject.CreateWithB.call(this, false);',
  14352. ' return this;',
  14353. ' };',
  14354. ' this.CreateWithC = function (c) {',
  14355. ' $mod.TObject.Create.call(this);',
  14356. ' $mod.TObject.CreateWithB.call(this, true);',
  14357. ' this.DoIt();',
  14358. ' this.DoIt();',
  14359. ' this.$class.DoSome();',
  14360. ' return this;',
  14361. ' };',
  14362. ' this.DoIt = function () {',
  14363. ' this.Create();',
  14364. ' this.CreateWithB(false);',
  14365. ' this.CreateWithC("c");',
  14366. ' };',
  14367. ' this.DoSome = function () {',
  14368. ' var Result = null;',
  14369. ' Result = this.$create("Create");',
  14370. ' Result = this.$create("CreateWithB", [true]);',
  14371. ' Result = this.$create("CreateWithC", ["c"]);',
  14372. ' return Result;',
  14373. ' };',
  14374. '});'
  14375. ]),
  14376. LinesToStr([ // this.$main
  14377. ''
  14378. ]));
  14379. end;
  14380. procedure TTestModule.TestClass_ClassVar_Assign;
  14381. begin
  14382. StartProgram(false);
  14383. Add([
  14384. 'type',
  14385. ' TObject = class',
  14386. ' public',
  14387. ' class var vI: longint;',
  14388. ' class var Sub: TObject;',
  14389. ' constructor Create;',
  14390. ' class function GetIt(var Par: longint): tobject;',
  14391. ' end;',
  14392. 'constructor tobject.create;',
  14393. 'begin',
  14394. ' vi:=vi+1;',
  14395. ' Self.vi:=Self.vi+1;',
  14396. ' inc(vi);',
  14397. 'end;',
  14398. 'class function tobject.getit(var par: longint): tobject;',
  14399. 'begin',
  14400. ' vi:=vi+3;',
  14401. ' Self.vi:=Self.vi+4;',
  14402. ' inc(vi);',
  14403. ' Result:=self.sub;',
  14404. ' GetIt(vi);',
  14405. 'end;',
  14406. 'var Obj: tobject;',
  14407. 'begin',
  14408. ' obj:=tobject.create;',
  14409. ' tobject.vi:=3;',
  14410. ' if tobject.vi=4 then ;',
  14411. ' tobject.sub:=nil;',
  14412. ' obj.sub:=nil;',
  14413. ' obj.sub.sub:=nil;']);
  14414. ConvertProgram;
  14415. CheckSource('TestClass_ClassVar_Assign',
  14416. LinesToStr([ // statements
  14417. 'rtl.createClass(this,"TObject",null,function(){',
  14418. ' this.vI = 0;',
  14419. ' this.Sub = null;',
  14420. ' this.$init = function () {',
  14421. ' };',
  14422. ' this.$final = function () {',
  14423. ' };',
  14424. ' this.Create = function(){',
  14425. ' $mod.TObject.vI = this.vI+1;',
  14426. ' $mod.TObject.vI = this.vI+1;',
  14427. ' $mod.TObject.vI += 1;',
  14428. ' return this;',
  14429. ' };',
  14430. ' this.GetIt = function(Par){',
  14431. ' var Result = null;',
  14432. ' $mod.TObject.vI = this.vI + 3;',
  14433. ' $mod.TObject.vI = this.vI + 4;',
  14434. ' $mod.TObject.vI += 1;',
  14435. ' Result = this.Sub;',
  14436. ' this.GetIt({',
  14437. ' p: $mod.TObject,',
  14438. ' get: function () {',
  14439. ' return this.p.vI;',
  14440. ' },',
  14441. ' set: function (v) {',
  14442. ' this.p.vI = v;',
  14443. ' }',
  14444. ' });',
  14445. ' return Result;',
  14446. ' };',
  14447. '});',
  14448. 'this.Obj = null;'
  14449. ]),
  14450. LinesToStr([ // $mod.$main
  14451. '$mod.Obj = $mod.TObject.$create("Create");',
  14452. '$mod.TObject.vI = 3;',
  14453. 'if ($mod.TObject.vI === 4);',
  14454. '$mod.TObject.Sub=null;',
  14455. '$mod.TObject.Sub=null;',
  14456. '$mod.TObject.Sub=null;',
  14457. '']));
  14458. end;
  14459. procedure TTestModule.TestClass_CallClassMethod;
  14460. begin
  14461. StartProgram(false);
  14462. Add('type');
  14463. Add(' TObject = class');
  14464. Add(' public');
  14465. Add(' class var vI: longint;');
  14466. Add(' class var Sub: TObject;');
  14467. Add(' constructor Create;');
  14468. Add(' function GetMore(Par: longint): longint;');
  14469. Add(' class function GetIt(Par: longint): tobject;');
  14470. Add(' end;');
  14471. Add('constructor tobject.create;');
  14472. Add('begin');
  14473. Add(' sub:=getit(3);');
  14474. Add(' vi:=getmore(4);');
  14475. Add(' sub:=Self.getit(5);');
  14476. Add(' vi:=Self.getmore(6);');
  14477. Add('end;');
  14478. Add('function tobject.getmore(par: longint): longint;');
  14479. Add('begin');
  14480. Add(' sub:=getit(11);');
  14481. Add(' vi:=getmore(12);');
  14482. Add(' sub:=self.getit(13);');
  14483. Add(' vi:=self.getmore(14);');
  14484. Add('end;');
  14485. Add('class function tobject.getit(par: longint): tobject;');
  14486. Add('begin');
  14487. Add(' sub:=getit(21);');
  14488. Add(' vi:=sub.getmore(22);');
  14489. Add(' sub:=self.getit(23);');
  14490. Add(' vi:=self.sub.getmore(24);');
  14491. Add('end;');
  14492. Add('var Obj: tobject;');
  14493. Add('begin');
  14494. Add(' obj:=tobject.create;');
  14495. Add(' tobject.getit(5);');
  14496. Add(' obj.getit(6);');
  14497. Add(' obj.sub.getit(7);');
  14498. Add(' obj.sub.getit(8).SUB:=nil;');
  14499. Add(' obj.sub.getit(9).GETIT(10);');
  14500. Add(' obj.sub.getit(11).SuB.getit(12);');
  14501. ConvertProgram;
  14502. CheckSource('TestClass_CallClassMethod',
  14503. LinesToStr([ // statements
  14504. 'rtl.createClass(this,"TObject",null,function(){',
  14505. ' this.vI = 0;',
  14506. ' this.Sub = null;',
  14507. ' this.$init = function () {',
  14508. ' };',
  14509. ' this.$final = function () {',
  14510. ' };',
  14511. ' this.Create = function(){',
  14512. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14513. ' $mod.TObject.vI = this.GetMore(4);',
  14514. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14515. ' $mod.TObject.vI = this.GetMore(6);',
  14516. ' return this;',
  14517. ' };',
  14518. ' this.GetMore = function(Par){',
  14519. ' var Result = 0;',
  14520. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14521. ' $mod.TObject.vI = this.GetMore(12);',
  14522. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14523. ' $mod.TObject.vI = this.GetMore(14);',
  14524. ' return Result;',
  14525. ' };',
  14526. ' this.GetIt = function(Par){',
  14527. ' var Result = null;',
  14528. ' $mod.TObject.Sub = this.GetIt(21);',
  14529. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14530. ' $mod.TObject.Sub = this.GetIt(23);',
  14531. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14532. ' return Result;',
  14533. ' };',
  14534. '});',
  14535. 'this.Obj = null;'
  14536. ]),
  14537. LinesToStr([ // $mod.$main
  14538. '$mod.Obj = $mod.TObject.$create("Create");',
  14539. '$mod.TObject.GetIt(5);',
  14540. '$mod.Obj.$class.GetIt(6);',
  14541. '$mod.Obj.Sub.$class.GetIt(7);',
  14542. '$mod.TObject.Sub=null;',
  14543. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14544. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14545. '']));
  14546. end;
  14547. procedure TTestModule.TestClass_CallClassMethodStatic;
  14548. begin
  14549. StartProgram(false);
  14550. Add([
  14551. 'type',
  14552. ' TObject = class',
  14553. ' public',
  14554. ' class function Fly: tobject; static;',
  14555. ' end;',
  14556. 'class function tobject.Fly: tobject;',
  14557. 'begin',
  14558. ' Result.Fly;',
  14559. ' Result.Fly();',
  14560. ' Fly;',
  14561. ' Fly();',
  14562. ' Fly.Fly;',
  14563. ' Fly.Fly();',
  14564. 'end;',
  14565. 'var Obj: tobject;',
  14566. 'begin',
  14567. ' obj.Fly;',
  14568. ' obj.Fly();',
  14569. ' with obj do begin',
  14570. ' Fly;',
  14571. ' Fly();',
  14572. ' end;',
  14573. '']);
  14574. ConvertProgram;
  14575. CheckSource('TestClass_CallClassMethodStatic',
  14576. LinesToStr([ // statements
  14577. 'rtl.createClass(this, "TObject", null, function () {',
  14578. ' this.$init = function () {',
  14579. ' };',
  14580. ' this.$final = function () {',
  14581. ' };',
  14582. ' this.Fly = function () {',
  14583. ' var Result = null;',
  14584. ' $mod.TObject.Fly();',
  14585. ' $mod.TObject.Fly();',
  14586. ' $mod.TObject.Fly();',
  14587. ' $mod.TObject.Fly();',
  14588. ' $mod.TObject.Fly();',
  14589. ' $mod.TObject.Fly();',
  14590. ' return Result;',
  14591. ' };',
  14592. '});',
  14593. 'this.Obj = null;'
  14594. ]),
  14595. LinesToStr([ // $mod.$main
  14596. '$mod.TObject.Fly();',
  14597. '$mod.TObject.Fly();',
  14598. 'var $with = $mod.Obj;',
  14599. '$with.Fly();',
  14600. '$with.Fly();',
  14601. '']));
  14602. end;
  14603. procedure TTestModule.TestClass_Property;
  14604. begin
  14605. StartProgram(false);
  14606. Add('type');
  14607. Add(' TObject = class');
  14608. Add(' Fx: longint;');
  14609. Add(' Fy: longint;');
  14610. Add(' function GetInt: longint;');
  14611. Add(' procedure SetInt(Value: longint);');
  14612. Add(' procedure DoIt;');
  14613. Add(' property IntA: longint read Fx write Fy;');
  14614. Add(' property IntB: longint read GetInt write SetInt;');
  14615. Add(' end;');
  14616. Add('function tobject.getint: longint;');
  14617. Add('begin');
  14618. Add(' result:=fx;');
  14619. Add('end;');
  14620. Add('procedure tobject.setint(value: longint);');
  14621. Add('begin');
  14622. Add(' if value=fy then exit;');
  14623. Add(' fy:=value;');
  14624. Add('end;');
  14625. Add('procedure tobject.doit;');
  14626. Add('begin');
  14627. Add(' IntA:=IntA+1;');
  14628. Add(' Self.IntA:=Self.IntA+1;');
  14629. Add(' IntB:=IntB+1;');
  14630. Add(' Self.IntB:=Self.IntB+1;');
  14631. Add('end;');
  14632. Add('var Obj: tobject;');
  14633. Add('begin');
  14634. Add(' obj.inta:=obj.inta+1;');
  14635. Add(' if obj.intb=2 then;');
  14636. Add(' obj.intb:=obj.intb+2;');
  14637. Add(' obj.setint(obj.inta);');
  14638. ConvertProgram;
  14639. CheckSource('TestClass_Property',
  14640. LinesToStr([ // statements
  14641. 'rtl.createClass(this, "TObject", null, function () {',
  14642. ' this.$init = function () {',
  14643. ' this.Fx = 0;',
  14644. ' this.Fy = 0;',
  14645. ' };',
  14646. ' this.$final = function () {',
  14647. ' };',
  14648. ' this.GetInt = function () {',
  14649. ' var Result = 0;',
  14650. ' Result = this.Fx;',
  14651. ' return Result;',
  14652. ' };',
  14653. ' this.SetInt = function (Value) {',
  14654. ' if (Value === this.Fy) return;',
  14655. ' this.Fy = Value;',
  14656. ' };',
  14657. ' this.DoIt = function () {',
  14658. ' this.Fy = this.Fx + 1;',
  14659. ' this.Fy = this.Fx + 1;',
  14660. ' this.SetInt(this.GetInt() + 1);',
  14661. ' this.SetInt(this.GetInt() + 1);',
  14662. ' };',
  14663. '});',
  14664. 'this.Obj = null;'
  14665. ]),
  14666. LinesToStr([ // $mod.$main
  14667. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14668. 'if ($mod.Obj.GetInt() === 2);',
  14669. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14670. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14671. ]));
  14672. end;
  14673. procedure TTestModule.TestClass_Property_ClassMethod;
  14674. begin
  14675. StartProgram(false);
  14676. Add([
  14677. 'type',
  14678. ' TObject = class',
  14679. ' class var Fx: longint;',
  14680. ' class var Fy: longint;',
  14681. ' class function GetInt: longint;',
  14682. ' class procedure SetInt(Value: longint);',
  14683. ' end;',
  14684. ' TBird = class',
  14685. ' class procedure DoIt;',
  14686. ' class property IntA: longint read Fx write Fy;',
  14687. ' class property IntB: longint read GetInt write SetInt;',
  14688. ' end;',
  14689. 'class function tobject.getint: longint;',
  14690. 'begin',
  14691. ' result:=fx;',
  14692. 'end;',
  14693. 'class procedure tobject.setint(value: longint);',
  14694. 'begin',
  14695. 'end;',
  14696. 'class procedure tbird.doit;',
  14697. 'begin',
  14698. ' FX:=3;',
  14699. ' IntA:=IntA+1;',
  14700. ' Self.IntA:=Self.IntA+1;',
  14701. ' IntB:=IntB+1;',
  14702. ' Self.IntB:=Self.IntB+1;',
  14703. ' with Self do begin',
  14704. ' FX:=11;',
  14705. ' IntA:=IntA+12;',
  14706. ' IntB:=IntB+13;',
  14707. ' end;',
  14708. 'end;',
  14709. 'var Obj: tbird;',
  14710. 'begin',
  14711. ' tbird.fx:=tbird.fx+1;',
  14712. ' tbird.inta:=tbird.inta+1;',
  14713. ' if tbird.intb=2 then;',
  14714. ' tbird.intb:=tbird.intb+2;',
  14715. ' tbird.setint(tbird.inta);',
  14716. ' obj.inta:=obj.inta+1;',
  14717. ' if obj.intb=2 then;',
  14718. ' obj.intb:=obj.intb+2;',
  14719. ' obj.setint(obj.inta);',
  14720. ' with Tbird do begin',
  14721. ' FX:=FY+1;',
  14722. ' inta:=inta+2;',
  14723. ' intb:=intb+3;',
  14724. ' end;',
  14725. ' with Obj do begin',
  14726. ' FX:=FY+1;',
  14727. ' inta:=inta+2;',
  14728. ' intb:=intb+3;',
  14729. ' end;',
  14730. '']);
  14731. ConvertProgram;
  14732. CheckSource('TestClass_Property_ClassMethod',
  14733. LinesToStr([ // statements
  14734. 'rtl.createClass(this, "TObject", null, function () {',
  14735. ' this.Fx = 0;',
  14736. ' this.Fy = 0;',
  14737. ' this.$init = function () {',
  14738. ' };',
  14739. ' this.$final = function () {',
  14740. ' };',
  14741. ' this.GetInt = function () {',
  14742. ' var Result = 0;',
  14743. ' Result = this.Fx;',
  14744. ' return Result;',
  14745. ' };',
  14746. ' this.SetInt = function (Value) {',
  14747. ' };',
  14748. '});',
  14749. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14750. ' this.DoIt = function () {',
  14751. ' $mod.TObject.Fx = 3;',
  14752. ' $mod.TObject.Fy = this.Fx + 1;',
  14753. ' $mod.TObject.Fy = this.Fx + 1;',
  14754. ' this.SetInt(this.GetInt() + 1);',
  14755. ' this.SetInt(this.GetInt() + 1);',
  14756. ' $mod.TObject.Fx = 11;',
  14757. ' $mod.TObject.Fy = this.Fx + 12;',
  14758. ' this.SetInt(this.GetInt() + 13);',
  14759. ' };',
  14760. '});',
  14761. 'this.Obj = null;'
  14762. ]),
  14763. LinesToStr([ // $mod.$main
  14764. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14765. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14766. 'if ($mod.TBird.GetInt() === 2);',
  14767. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14768. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14769. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14770. 'if ($mod.Obj.$class.GetInt() === 2);',
  14771. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14772. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14773. 'var $with = $mod.TBird;',
  14774. '$mod.TObject.Fx = $with.Fy + 1;',
  14775. '$mod.TObject.Fy = $with.Fx + 2;',
  14776. '$with.SetInt($with.GetInt() + 3);',
  14777. 'var $with1 = $mod.Obj;',
  14778. '$mod.TObject.Fx = $with1.Fy + 1;',
  14779. '$mod.TObject.Fy = $with1.Fx + 2;',
  14780. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14781. '']));
  14782. end;
  14783. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14784. begin
  14785. StartProgram(false);
  14786. Add([
  14787. 'type',
  14788. ' TObject = class',
  14789. ' class function GetInt: longint; static;',
  14790. ' class procedure SetInt(Value: longint); static;',
  14791. ' class function GetItems(Index: word): longint; static;',
  14792. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14793. ' end;',
  14794. ' TBird = class',
  14795. ' class procedure Fly;',
  14796. ' class property IntA: longint read GetInt write SetInt;',
  14797. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14798. ' end;',
  14799. 'class function tobject.getint: longint;',
  14800. 'begin',
  14801. 'end;',
  14802. 'class procedure tobject.setint(value: longint);',
  14803. 'begin',
  14804. 'end;',
  14805. 'class function tobject.GetItems(Index: word): longint;',
  14806. 'begin',
  14807. 'end;',
  14808. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14809. 'begin',
  14810. 'end;',
  14811. 'class procedure tbird.fly;',
  14812. 'var w: longint;',
  14813. 'begin',
  14814. ' inta:=inta+51;',
  14815. ' w:=items[52];',
  14816. ' items[53]:=54;',
  14817. 'end;',
  14818. 'var Obj: tbird;',
  14819. ' i: longint;',
  14820. 'begin',
  14821. ' tbird.inta:=tbird.inta+1;',
  14822. ' i:=tbird.items[2];',
  14823. ' tbird.items[3]:=4;',
  14824. ' obj.inta:=obj.inta+11;',
  14825. ' i:=obj.items[12];',
  14826. ' obj.items[13]:=14;',
  14827. ' with Tbird do begin',
  14828. ' inta:=inta+21;',
  14829. ' i:=items[22];',
  14830. ' items[23]:=24;',
  14831. ' end;',
  14832. ' with Obj do begin',
  14833. ' inta:=inta+31;',
  14834. ' i:=items[32];',
  14835. ' items[33]:=34;',
  14836. ' end;',
  14837. '']);
  14838. ConvertProgram;
  14839. CheckSource('TestClass_Property_ClassMethod',
  14840. LinesToStr([ // statements
  14841. 'rtl.createClass(this, "TObject", null, function () {',
  14842. ' this.$init = function () {',
  14843. ' };',
  14844. ' this.$final = function () {',
  14845. ' };',
  14846. ' this.GetInt = function () {',
  14847. ' var Result = 0;',
  14848. ' return Result;',
  14849. ' };',
  14850. ' this.SetInt = function (Value) {',
  14851. ' };',
  14852. ' this.GetItems = function (Index) {',
  14853. ' var Result = 0;',
  14854. ' return Result;',
  14855. ' };',
  14856. ' this.SetItems = function (Index, Value) {',
  14857. ' };',
  14858. '});',
  14859. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14860. ' this.Fly = function () {',
  14861. ' var w = 0;',
  14862. ' this.SetInt(this.GetInt() + 51);',
  14863. ' w = this.GetItems(52);',
  14864. ' this.SetItems(53, 54);',
  14865. ' };',
  14866. '});',
  14867. 'this.Obj = null;',
  14868. 'this.i = 0;',
  14869. '']),
  14870. LinesToStr([ // $mod.$main
  14871. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14872. '$mod.i = $mod.TObject.GetItems(2);',
  14873. '$mod.TObject.SetItems(3, 4);',
  14874. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14875. '$mod.i = $mod.TObject.GetItems(12);',
  14876. '$mod.TObject.SetItems(13, 14);',
  14877. 'var $with = $mod.TBird;',
  14878. '$with.SetInt($with.GetInt() + 21);',
  14879. '$mod.i = $with.GetItems(22);',
  14880. '$with.SetItems(23, 24);',
  14881. 'var $with1 = $mod.Obj;',
  14882. '$with1.SetInt($with1.GetInt() + 31);',
  14883. '$mod.i = $with1.GetItems(32);',
  14884. '$with1.SetItems(33, 34);',
  14885. '']));
  14886. end;
  14887. procedure TTestModule.TestClass_Property_Indexed;
  14888. begin
  14889. StartProgram(false);
  14890. Add([
  14891. 'type',
  14892. ' TObject = class',
  14893. ' FItems: array of longint;',
  14894. ' function GetItems(Index: longint): longint;',
  14895. ' procedure SetItems(Index: longint; Value: longint);',
  14896. ' procedure DoIt;',
  14897. ' property Items[Index: longint]: longint read getitems write setitems;',
  14898. ' end;',
  14899. 'function tobject.getitems(index: longint): longint;',
  14900. 'begin',
  14901. ' Result:=fitems[index];',
  14902. 'end;',
  14903. 'procedure tobject.setitems(index: longint; value: longint);',
  14904. 'begin',
  14905. ' fitems[index]:=value;',
  14906. 'end;',
  14907. 'procedure tobject.doit;',
  14908. 'begin',
  14909. ' items[1]:=2;',
  14910. ' items[3]:=items[4];',
  14911. ' self.items[5]:=self.items[6];',
  14912. ' items[items[7]]:=items[items[8]];',
  14913. 'end;',
  14914. 'var Obj: tobject;',
  14915. 'begin',
  14916. ' obj.Items[11]:=obj.Items[12];',
  14917. '']);
  14918. ConvertProgram;
  14919. CheckSource('TestClass_Property_Indexed',
  14920. LinesToStr([ // statements
  14921. 'rtl.createClass(this, "TObject", null, function () {',
  14922. ' this.$init = function () {',
  14923. ' this.FItems = [];',
  14924. ' };',
  14925. ' this.$final = function () {',
  14926. ' this.FItems = undefined;',
  14927. ' };',
  14928. ' this.GetItems = function (Index) {',
  14929. ' var Result = 0;',
  14930. ' Result = this.FItems[Index];',
  14931. ' return Result;',
  14932. ' };',
  14933. ' this.SetItems = function (Index, Value) {',
  14934. ' this.FItems[Index] = Value;',
  14935. ' };',
  14936. ' this.DoIt = function () {',
  14937. ' this.SetItems(1, 2);',
  14938. ' this.SetItems(3,this.GetItems(4));',
  14939. ' this.SetItems(5,this.GetItems(6));',
  14940. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14941. ' };',
  14942. '});',
  14943. 'this.Obj = null;'
  14944. ]),
  14945. LinesToStr([ // $mod.$main
  14946. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  14947. ]));
  14948. end;
  14949. procedure TTestModule.TestClass_Property_IndexSpec;
  14950. begin
  14951. StartProgram(false);
  14952. Add([
  14953. 'type',
  14954. ' TEnum = (red, blue);',
  14955. ' TObject = class',
  14956. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  14957. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  14958. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  14959. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  14960. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  14961. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  14962. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  14963. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  14964. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  14965. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  14966. ' end;',
  14967. 'procedure DoIt(b: boolean); begin end;',
  14968. 'var',
  14969. ' o: TObject;',
  14970. 'begin',
  14971. ' o.B1:=o.B1;',
  14972. ' o.B2:=o.B2;',
  14973. ' o.B3:=o.B3;',
  14974. ' o.I1[''a'']:=o.I1[''b''];',
  14975. ' doit(o.b1);',
  14976. ' doit(o.b2);',
  14977. ' doit(o.i1[''c'']);',
  14978. '']);
  14979. ConvertProgram;
  14980. CheckSource('TestClass_Property_IndexSpec',
  14981. LinesToStr([ // statements
  14982. 'this.TEnum = {',
  14983. ' "0": "red",',
  14984. ' red: 0,',
  14985. ' "1": "blue",',
  14986. ' blue: 1',
  14987. '};',
  14988. 'rtl.createClass(this, "TObject", null, function () {',
  14989. ' this.$init = function () {',
  14990. ' };',
  14991. ' this.$final = function () {',
  14992. ' };',
  14993. '});',
  14994. 'this.DoIt = function (b) {',
  14995. '};',
  14996. 'this.o = null;',
  14997. '']),
  14998. LinesToStr([ // $mod.$main
  14999. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  15000. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15001. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15002. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15003. '$mod.DoIt($mod.o.GetIntBool(1));',
  15004. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15005. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15006. '']));
  15007. end;
  15008. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15009. begin
  15010. StartProgram(false);
  15011. Add('type');
  15012. Add(' TArray = array of longint;');
  15013. Add(' TObject = class');
  15014. Add(' FItems: TArray;');
  15015. Add(' function GetItems: tarray;');
  15016. Add(' procedure SetItems(Value: tarray);');
  15017. Add(' property Items: tarray read getitems write setitems;');
  15018. Add(' procedure SetNumbers(const Value: tarray);');
  15019. Add(' property Numbers: tarray write setnumbers;');
  15020. Add(' end;');
  15021. Add('function tobject.getitems: tarray;');
  15022. Add('begin');
  15023. Add(' Result:=fitems;');
  15024. Add('end;');
  15025. Add('procedure tobject.setitems(value: tarray);');
  15026. Add('begin');
  15027. Add(' fitems:=value;');
  15028. Add(' fitems:=nil;');
  15029. Add(' Items:=nil;');
  15030. Add(' Items:=Items;');
  15031. Add(' Items[1]:=2;');
  15032. Add(' fitems[3]:=Items[4];');
  15033. Add(' Items[5]:=Items[6];');
  15034. Add(' Self.Items[7]:=8;');
  15035. Add(' Self.Items[9]:=Self.Items[10];');
  15036. Add(' Items[Items[11]]:=Items[Items[12]];');
  15037. Add('end;');
  15038. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15039. Add('begin;');
  15040. Add(' Numbers:=nil;');
  15041. Add(' Numbers:=Value;');
  15042. Add(' Self.Numbers:=Value;');
  15043. Add('end;');
  15044. Add('var Obj: tobject;');
  15045. Add('begin');
  15046. Add(' obj.items:=nil;');
  15047. Add(' obj.items:=obj.items;');
  15048. Add(' obj.items[11]:=obj.items[12];');
  15049. ConvertProgram;
  15050. CheckSource('TestClass_PropertyOfTypeArray',
  15051. LinesToStr([ // statements
  15052. 'rtl.createClass(this, "TObject", null, function () {',
  15053. ' this.$init = function () {',
  15054. ' this.FItems = [];',
  15055. ' };',
  15056. ' this.$final = function () {',
  15057. ' this.FItems = undefined;',
  15058. ' };',
  15059. ' this.GetItems = function () {',
  15060. ' var Result = [];',
  15061. ' Result = rtl.arrayRef(this.FItems);',
  15062. ' return Result;',
  15063. ' };',
  15064. ' this.SetItems = function (Value) {',
  15065. ' this.FItems = rtl.arrayRef(Value);',
  15066. ' this.FItems = [];',
  15067. ' this.SetItems([]);',
  15068. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15069. ' this.GetItems()[1] = 2;',
  15070. ' this.FItems[3] = this.GetItems()[4];',
  15071. ' this.GetItems()[5] = this.GetItems()[6];',
  15072. ' this.GetItems()[7] = 8;',
  15073. ' this.GetItems()[9] = this.GetItems()[10];',
  15074. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15075. ' };',
  15076. ' this.SetNumbers = function (Value) {',
  15077. ' this.SetNumbers([]);',
  15078. ' this.SetNumbers(Value);',
  15079. ' this.SetNumbers(Value);',
  15080. ' };',
  15081. '});',
  15082. 'this.Obj = null;'
  15083. ]),
  15084. LinesToStr([ // $mod.$main
  15085. '$mod.Obj.SetItems([]);',
  15086. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15087. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15088. ]));
  15089. end;
  15090. procedure TTestModule.TestClass_PropertyDefault;
  15091. begin
  15092. StartProgram(false);
  15093. Add([
  15094. 'type',
  15095. ' TArray = array of longint;',
  15096. ' TObject = class',
  15097. ' end;',
  15098. ' TBird = class',
  15099. ' FItems: TArray;',
  15100. ' function GetItems(Index: longint): longint;',
  15101. ' procedure SetItems(Index, Value: longint);',
  15102. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15103. ' end;',
  15104. 'function TBird.getitems(index: longint): longint;',
  15105. 'begin',
  15106. 'end;',
  15107. 'procedure TBird.setitems(index, value: longint);',
  15108. 'begin',
  15109. ' Self[1]:=2;',
  15110. ' Self[3]:=Self[index];',
  15111. ' Self[index]:=Self[Self[value]];',
  15112. ' Self[Self[4]]:=value;',
  15113. 'end;',
  15114. 'var',
  15115. ' Bird: TBird;',
  15116. ' Obj: TObject;',
  15117. 'begin',
  15118. ' bird[11]:=12;',
  15119. ' bird[13]:=bird[14];',
  15120. ' bird[Bird[15]]:=bird[Bird[15]];',
  15121. ' TBird(obj)[16]:=TBird(obj)[17];',
  15122. ' (obj as tbird)[18]:=19;',
  15123. '']);
  15124. ConvertProgram;
  15125. CheckSource('TestClass_PropertyDefault',
  15126. LinesToStr([ // statements
  15127. 'rtl.createClass(this, "TObject", null, function () {',
  15128. ' this.$init = function () {',
  15129. ' };',
  15130. ' this.$final = function () {',
  15131. ' };',
  15132. '});',
  15133. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15134. ' this.$init = function () {',
  15135. ' $mod.TObject.$init.call(this);',
  15136. ' this.FItems = [];',
  15137. ' };',
  15138. ' this.$final = function () {',
  15139. ' this.FItems = undefined;',
  15140. ' $mod.TObject.$final.call(this);',
  15141. ' };',
  15142. ' this.GetItems = function (Index) {',
  15143. ' var Result = 0;',
  15144. ' return Result;',
  15145. ' };',
  15146. ' this.SetItems = function (Index, Value) {',
  15147. ' this.SetItems(1, 2);',
  15148. ' this.SetItems(3, this.GetItems(Index));',
  15149. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15150. ' this.SetItems(this.GetItems(4), Value);',
  15151. ' };',
  15152. '});',
  15153. 'this.Bird = null;',
  15154. 'this.Obj = null;',
  15155. '']),
  15156. LinesToStr([ // $mod.$main
  15157. '$mod.Bird.SetItems(11, 12);',
  15158. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15159. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15160. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15161. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15162. '']));
  15163. end;
  15164. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15165. begin
  15166. StartProgram(false);
  15167. Add([
  15168. 'type',
  15169. ' TObject = class end;',
  15170. ' TAlphaList = class',
  15171. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15172. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15173. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15174. ' end;',
  15175. ' TBetaList = class',
  15176. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15177. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15178. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15179. ' end;',
  15180. ' TBird = class',
  15181. ' procedure DoIt;',
  15182. ' end;',
  15183. 'procedure TBird.DoIt;',
  15184. 'var',
  15185. ' List: TAlphaList;',
  15186. 'begin',
  15187. ' if TBetaList(List[true])[3]=nil then ;',
  15188. ' TBetaList(List[false])[5]:=nil;',
  15189. 'end;',
  15190. 'var',
  15191. ' List: TAlphaList;',
  15192. 'begin',
  15193. ' if TBetaList(List[true])[3]=nil then ;',
  15194. ' TBetaList(List[false])[5]:=nil;',
  15195. '']);
  15196. ConvertProgram;
  15197. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15198. LinesToStr([ // statements
  15199. 'rtl.createClass(this, "TObject", null, function () {',
  15200. ' this.$init = function () {',
  15201. ' };',
  15202. ' this.$final = function () {',
  15203. ' };',
  15204. '});',
  15205. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15206. '});',
  15207. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15208. '});',
  15209. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15210. ' this.DoIt = function () {',
  15211. ' var List = null;',
  15212. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15213. ' List.GetAlphas(false).SetBetas(5, null);',
  15214. ' };',
  15215. '});',
  15216. 'this.List = null;',
  15217. '']),
  15218. LinesToStr([ // $mod.$main
  15219. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15220. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15221. '']));
  15222. end;
  15223. procedure TTestModule.TestClass_PropertyOverride;
  15224. begin
  15225. StartProgram(false);
  15226. Add('type');
  15227. Add(' integer = longint;');
  15228. Add(' TObject = class');
  15229. Add(' FItem: integer;');
  15230. Add(' function GetItem: integer; external name ''GetItem'';');
  15231. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15232. Add(' property Item: integer read getitem write setitem;');
  15233. Add(' end;');
  15234. Add(' TCar = class');
  15235. Add(' FBag: integer;');
  15236. Add(' function GetBag: integer; external name ''GetBag'';');
  15237. Add(' property Item read getbag;');
  15238. Add(' end;');
  15239. Add('var');
  15240. Add(' Obj: tobject;');
  15241. Add(' Car: tcar;');
  15242. Add('begin');
  15243. Add(' Obj.Item:=Obj.Item;');
  15244. Add(' Car.Item:=Car.Item;');
  15245. ConvertProgram;
  15246. CheckSource('TestClass_PropertyOverride',
  15247. LinesToStr([ // statements
  15248. 'rtl.createClass(this, "TObject", null, function () {',
  15249. ' this.$init = function () {',
  15250. ' this.FItem = 0;',
  15251. ' };',
  15252. ' this.$final = function () {',
  15253. ' };',
  15254. '});',
  15255. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15256. ' this.$init = function () {',
  15257. ' $mod.TObject.$init.call(this);',
  15258. ' this.FBag = 0;',
  15259. ' };',
  15260. '});',
  15261. 'this.Obj = null;',
  15262. 'this.Car = null;',
  15263. '']),
  15264. LinesToStr([ // $mod.$main
  15265. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15266. '$mod.Car.SetItem($mod.Car.GetBag());',
  15267. '']));
  15268. end;
  15269. procedure TTestModule.TestClass_PropertyIncVisibility;
  15270. begin
  15271. AddModuleWithIntfImplSrc('unit1.pp',
  15272. LinesToStr([
  15273. 'type',
  15274. ' TNumber = longint;',
  15275. ' TInteger = longint;',
  15276. ' TObject = class',
  15277. ' private',
  15278. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15279. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15280. ' protected',
  15281. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15282. ' end;']),
  15283. LinesToStr([
  15284. '']));
  15285. StartProgram(true);
  15286. Add([
  15287. 'uses unit1;',
  15288. 'type',
  15289. ' TBird = class',
  15290. ' public',
  15291. ' property Items;',
  15292. ' end;',
  15293. 'procedure DoIt(i: TInteger);',
  15294. 'begin',
  15295. 'end;',
  15296. 'var b: TBird;',
  15297. 'begin',
  15298. ' b.Items[1]:=2;',
  15299. ' b.Items[3]:=b.Items[4];',
  15300. ' DoIt(b.Items[5]);',
  15301. '']);
  15302. ConvertProgram;
  15303. CheckSource('TestClass_PropertyIncVisibility',
  15304. LinesToStr([ // statements
  15305. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15306. '});',
  15307. 'this.DoIt = function (i) {',
  15308. '};',
  15309. 'this.b = null;'
  15310. ]),
  15311. LinesToStr([ // $mod.$main
  15312. '$mod.b.SetItems(1, 2);',
  15313. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15314. '$mod.DoIt($mod.b.GetItems(5));'
  15315. ]));
  15316. end;
  15317. procedure TTestModule.TestClass_Assigned;
  15318. begin
  15319. StartProgram(false);
  15320. Add('type');
  15321. Add(' TObject = class');
  15322. Add(' end;');
  15323. Add('var');
  15324. Add(' Obj: tobject;');
  15325. Add(' b: boolean;');
  15326. Add('begin');
  15327. Add(' if Assigned(obj) then ;');
  15328. Add(' b:=Assigned(obj) or false;');
  15329. ConvertProgram;
  15330. CheckSource('TestClass_Assigned',
  15331. LinesToStr([ // statements
  15332. 'rtl.createClass(this, "TObject", null, function () {',
  15333. ' this.$init = function () {',
  15334. ' };',
  15335. ' this.$final = function () {',
  15336. ' };',
  15337. '});',
  15338. 'this.Obj = null;',
  15339. 'this.b = false;'
  15340. ]),
  15341. LinesToStr([ // $mod.$main
  15342. 'if ($mod.Obj != null);',
  15343. '$mod.b = ($mod.Obj != null) || false;'
  15344. ]));
  15345. end;
  15346. procedure TTestModule.TestClass_WithClassDoCreate;
  15347. begin
  15348. StartProgram(false);
  15349. Add('type');
  15350. Add(' TObject = class');
  15351. Add(' aBool: boolean;');
  15352. Add(' Arr: array of boolean;');
  15353. Add(' constructor Create;');
  15354. Add(' end;');
  15355. Add('constructor TObject.Create; begin end;');
  15356. Add('var');
  15357. Add(' Obj: tobject;');
  15358. Add(' b: boolean;');
  15359. Add('begin');
  15360. Add(' with tobject.create do begin');
  15361. Add(' b:=abool;');
  15362. Add(' abool:=b;');
  15363. Add(' b:=arr[1];');
  15364. Add(' arr[2]:=b;');
  15365. Add(' end;');
  15366. Add(' with tobject do');
  15367. Add(' obj:=create;');
  15368. Add(' with obj do begin');
  15369. Add(' create;');
  15370. Add(' b:=abool;');
  15371. Add(' abool:=b;');
  15372. Add(' b:=arr[3];');
  15373. Add(' arr[4]:=b;');
  15374. Add(' end;');
  15375. ConvertProgram;
  15376. CheckSource('TestClass_WithClassDoCreate',
  15377. LinesToStr([ // statements
  15378. 'rtl.createClass(this, "TObject", null, function () {',
  15379. ' this.$init = function () {',
  15380. ' this.aBool = false;',
  15381. ' this.Arr = [];',
  15382. ' };',
  15383. ' this.$final = function () {',
  15384. ' this.Arr = undefined;',
  15385. ' };',
  15386. ' this.Create = function () {',
  15387. ' return this;',
  15388. ' };',
  15389. '});',
  15390. 'this.Obj = null;',
  15391. 'this.b = false;'
  15392. ]),
  15393. LinesToStr([ // $mod.$main
  15394. 'var $with = $mod.TObject.$create("Create");',
  15395. '$mod.b = $with.aBool;',
  15396. '$with.aBool = $mod.b;',
  15397. '$mod.b = $with.Arr[1];',
  15398. '$with.Arr[2] = $mod.b;',
  15399. 'var $with1 = $mod.TObject;',
  15400. '$mod.Obj = $with1.$create("Create");',
  15401. 'var $with2 = $mod.Obj;',
  15402. '$with2.Create();',
  15403. '$mod.b = $with2.aBool;',
  15404. '$with2.aBool = $mod.b;',
  15405. '$mod.b = $with2.Arr[3];',
  15406. '$with2.Arr[4] = $mod.b;',
  15407. '']));
  15408. end;
  15409. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15410. begin
  15411. StartProgram(false);
  15412. Add('type');
  15413. Add(' TObject = class');
  15414. Add(' FInt: longint;');
  15415. Add(' constructor Create;');
  15416. Add(' function GetSize: longint;');
  15417. Add(' procedure SetSize(Value: longint);');
  15418. Add(' property Int: longint read FInt write FInt;');
  15419. Add(' property Size: longint read GetSize write SetSize;');
  15420. Add(' end;');
  15421. Add('constructor TObject.Create; begin end;');
  15422. Add('function TObject.GetSize: longint; begin; end;');
  15423. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15424. Add('var');
  15425. Add(' Obj: tobject;');
  15426. Add(' i: longint;');
  15427. Add('begin');
  15428. Add(' with TObject.Create do begin');
  15429. Add(' i:=int;');
  15430. Add(' int:=i;');
  15431. Add(' i:=size;');
  15432. Add(' size:=i;');
  15433. Add(' end;');
  15434. Add(' with obj do begin');
  15435. Add(' i:=int;');
  15436. Add(' int:=i;');
  15437. Add(' i:=size;');
  15438. Add(' size:=i;');
  15439. Add(' end;');
  15440. ConvertProgram;
  15441. CheckSource('TestClass_WithClassInstDoProperty',
  15442. LinesToStr([ // statements
  15443. 'rtl.createClass(this, "TObject", null, function () {',
  15444. ' this.$init = function () {',
  15445. ' this.FInt = 0;',
  15446. ' };',
  15447. ' this.$final = function () {',
  15448. ' };',
  15449. ' this.Create = function () {',
  15450. ' return this;',
  15451. ' };',
  15452. ' this.GetSize = function () {',
  15453. ' var Result = 0;',
  15454. ' return Result;',
  15455. ' };',
  15456. ' this.SetSize = function (Value) {',
  15457. ' };',
  15458. '});',
  15459. 'this.Obj = null;',
  15460. 'this.i = 0;'
  15461. ]),
  15462. LinesToStr([ // $mod.$main
  15463. 'var $with = $mod.TObject.$create("Create");',
  15464. '$mod.i = $with.FInt;',
  15465. '$with.FInt = $mod.i;',
  15466. '$mod.i = $with.GetSize();',
  15467. '$with.SetSize($mod.i);',
  15468. 'var $with1 = $mod.Obj;',
  15469. '$mod.i = $with1.FInt;',
  15470. '$with1.FInt = $mod.i;',
  15471. '$mod.i = $with1.GetSize();',
  15472. '$with1.SetSize($mod.i);',
  15473. '']));
  15474. end;
  15475. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15476. begin
  15477. StartProgram(false);
  15478. Add('type');
  15479. Add(' TObject = class');
  15480. Add(' constructor Create;');
  15481. Add(' function GetItems(Index: longint): longint;');
  15482. Add(' procedure SetItems(Index, Value: longint);');
  15483. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15484. Add(' end;');
  15485. Add('constructor TObject.Create; begin end;');
  15486. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15487. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15488. Add('var');
  15489. Add(' Obj: tobject;');
  15490. Add(' i: longint;');
  15491. Add('begin');
  15492. Add(' with TObject.Create do begin');
  15493. Add(' i:=Items[1];');
  15494. Add(' Items[2]:=i;');
  15495. Add(' end;');
  15496. Add(' with obj do begin');
  15497. Add(' i:=Items[3];');
  15498. Add(' Items[4]:=i;');
  15499. Add(' end;');
  15500. ConvertProgram;
  15501. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15502. LinesToStr([ // statements
  15503. 'rtl.createClass(this, "TObject", null, function () {',
  15504. ' this.$init = function () {',
  15505. ' };',
  15506. ' this.$final = function () {',
  15507. ' };',
  15508. ' this.Create = function () {',
  15509. ' return this;',
  15510. ' };',
  15511. ' this.GetItems = function (Index) {',
  15512. ' var Result = 0;',
  15513. ' return Result;',
  15514. ' };',
  15515. ' this.SetItems = function (Index, Value) {',
  15516. ' };',
  15517. '});',
  15518. 'this.Obj = null;',
  15519. 'this.i = 0;'
  15520. ]),
  15521. LinesToStr([ // $mod.$main
  15522. 'var $with = $mod.TObject.$create("Create");',
  15523. '$mod.i = $with.GetItems(1);',
  15524. '$with.SetItems(2, $mod.i);',
  15525. 'var $with1 = $mod.Obj;',
  15526. '$mod.i = $with1.GetItems(3);',
  15527. '$with1.SetItems(4, $mod.i);',
  15528. '']));
  15529. end;
  15530. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15531. begin
  15532. StartProgram(false);
  15533. Add('type');
  15534. Add(' TObject = class');
  15535. Add(' constructor Create;');
  15536. Add(' function GetSize: longint;');
  15537. Add(' procedure SetSize(Value: longint);');
  15538. Add(' end;');
  15539. Add('constructor TObject.Create; begin end;');
  15540. Add('function TObject.GetSize: longint; begin; end;');
  15541. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15542. Add('var');
  15543. Add(' Obj: tobject;');
  15544. Add(' i: longint;');
  15545. Add('begin');
  15546. Add(' with TObject.Create do begin');
  15547. Add(' i:=GetSize;');
  15548. Add(' i:=GetSize();');
  15549. Add(' SetSize(i);');
  15550. Add(' end;');
  15551. Add(' with obj do begin');
  15552. Add(' i:=GetSize;');
  15553. Add(' i:=GetSize();');
  15554. Add(' SetSize(i);');
  15555. Add(' end;');
  15556. ConvertProgram;
  15557. CheckSource('TestClass_WithClassInstDoFunc',
  15558. LinesToStr([ // statements
  15559. 'rtl.createClass(this, "TObject", null, function () {',
  15560. ' this.$init = function () {',
  15561. ' };',
  15562. ' this.$final = function () {',
  15563. ' };',
  15564. ' this.Create = function () {',
  15565. ' return this;',
  15566. ' };',
  15567. ' this.GetSize = function () {',
  15568. ' var Result = 0;',
  15569. ' return Result;',
  15570. ' };',
  15571. ' this.SetSize = function (Value) {',
  15572. ' };',
  15573. '});',
  15574. 'this.Obj = null;',
  15575. 'this.i = 0;'
  15576. ]),
  15577. LinesToStr([ // $mod.$main
  15578. 'var $with = $mod.TObject.$create("Create");',
  15579. '$mod.i = $with.GetSize();',
  15580. '$mod.i = $with.GetSize();',
  15581. '$with.SetSize($mod.i);',
  15582. 'var $with1 = $mod.Obj;',
  15583. '$mod.i = $with1.GetSize();',
  15584. '$mod.i = $with1.GetSize();',
  15585. '$with1.SetSize($mod.i);',
  15586. '']));
  15587. end;
  15588. procedure TTestModule.TestClass_TypeCast;
  15589. begin
  15590. StartProgram(false);
  15591. Add('type');
  15592. Add(' TObject = class');
  15593. Add(' Next: TObject;');
  15594. Add(' constructor Create;');
  15595. Add(' end;');
  15596. Add(' TControl = class(TObject)');
  15597. Add(' Arr: array of TObject;');
  15598. Add(' function GetIt(vI: longint = 0): TObject;');
  15599. Add(' end;');
  15600. Add('constructor tobject.create; begin end;');
  15601. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15602. Add('var');
  15603. Add(' Obj: tobject;');
  15604. Add('begin');
  15605. Add(' obj:=tcontrol(obj).next;');
  15606. Add(' tcontrol(obj):=nil;');
  15607. Add(' obj:=tcontrol(obj);');
  15608. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15609. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15610. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15611. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15612. Add(' obj:=tcontrol(nil);');
  15613. ConvertProgram;
  15614. CheckSource('TestClass_TypeCast',
  15615. LinesToStr([ // statements
  15616. 'rtl.createClass(this, "TObject", null, function () {',
  15617. ' this.$init = function () {',
  15618. ' this.Next = null;',
  15619. ' };',
  15620. ' this.$final = function () {',
  15621. ' this.Next = undefined;',
  15622. ' };',
  15623. ' this.Create = function () {',
  15624. ' return this;',
  15625. ' };',
  15626. '});',
  15627. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15628. ' this.$init = function () {',
  15629. ' $mod.TObject.$init.call(this);',
  15630. ' this.Arr = [];',
  15631. ' };',
  15632. ' this.$final = function () {',
  15633. ' this.Arr = undefined;',
  15634. ' $mod.TObject.$final.call(this);',
  15635. ' };',
  15636. ' this.GetIt = function (vI) {',
  15637. ' var Result = null;',
  15638. ' return Result;',
  15639. ' };',
  15640. '});',
  15641. 'this.Obj = null;'
  15642. ]),
  15643. LinesToStr([ // $mod.$main
  15644. '$mod.Obj = $mod.Obj.Next;',
  15645. '$mod.Obj = null;',
  15646. '$mod.Obj = $mod.Obj;',
  15647. '$mod.Obj = $mod.Obj.GetIt(0);',
  15648. '$mod.Obj = $mod.Obj.GetIt(0);',
  15649. '$mod.Obj = $mod.Obj.GetIt(1);',
  15650. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15651. '$mod.Obj = null;',
  15652. '']));
  15653. end;
  15654. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15655. begin
  15656. StartProgram(false);
  15657. Add('type');
  15658. Add(' TObject = class end;');
  15659. Add('procedure ProcA(var A);');
  15660. Add('begin');
  15661. Add(' TObject(A):=nil;');
  15662. Add(' TObject(A):=TObject(A);');
  15663. Add(' if TObject(A)=nil then ;');
  15664. Add(' if nil=TObject(A) then ;');
  15665. Add('end;');
  15666. Add('procedure ProcB(out A);');
  15667. Add('begin');
  15668. Add(' TObject(A):=nil;');
  15669. Add(' TObject(A):=TObject(A);');
  15670. Add(' if TObject(A)=nil then ;');
  15671. Add(' if nil=TObject(A) then ;');
  15672. Add('end;');
  15673. Add('procedure ProcC(const A);');
  15674. Add('begin');
  15675. Add(' if TObject(A)=nil then ;');
  15676. Add(' if nil=TObject(A) then ;');
  15677. Add('end;');
  15678. Add('var o: TObject;');
  15679. Add('begin');
  15680. Add(' ProcA(o);');
  15681. Add(' ProcB(o);');
  15682. Add(' ProcC(o);');
  15683. ConvertProgram;
  15684. CheckSource('TestClass_TypeCastUntypedParam',
  15685. LinesToStr([ // statements
  15686. 'rtl.createClass(this, "TObject", null, function () {',
  15687. ' this.$init = function () {',
  15688. ' };',
  15689. ' this.$final = function () {',
  15690. ' };',
  15691. '});',
  15692. 'this.ProcA = function (A) {',
  15693. ' A.set(null);',
  15694. ' A.set(A.get());',
  15695. ' if (A.get() === null);',
  15696. ' if (null === A.get());',
  15697. '};',
  15698. 'this.ProcB = function (A) {',
  15699. ' A.set(null);',
  15700. ' A.set(A.get());',
  15701. ' if (A.get() === null);',
  15702. ' if (null === A.get());',
  15703. '};',
  15704. 'this.ProcC = function (A) {',
  15705. ' if (A === null);',
  15706. ' if (null === A);',
  15707. '};',
  15708. 'this.o = null;',
  15709. '']),
  15710. LinesToStr([ // $mod.$main
  15711. '$mod.ProcA({',
  15712. ' p: $mod,',
  15713. ' get: function () {',
  15714. ' return this.p.o;',
  15715. ' },',
  15716. ' set: function (v) {',
  15717. ' this.p.o = v;',
  15718. ' }',
  15719. '});',
  15720. '$mod.ProcB({',
  15721. ' p: $mod,',
  15722. ' get: function () {',
  15723. ' return this.p.o;',
  15724. ' },',
  15725. ' set: function (v) {',
  15726. ' this.p.o = v;',
  15727. ' }',
  15728. '});',
  15729. '$mod.ProcC($mod.o);',
  15730. '']));
  15731. end;
  15732. procedure TTestModule.TestClass_Overloads;
  15733. begin
  15734. StartProgram(false);
  15735. Add('type');
  15736. Add(' TObject = class');
  15737. Add(' procedure DoIt;');
  15738. Add(' procedure DoIt(vI: longint);');
  15739. Add(' end;');
  15740. Add('procedure TObject.DoIt;');
  15741. Add('begin');
  15742. Add(' DoIt;');
  15743. Add(' DoIt(1);');
  15744. Add('end;');
  15745. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15746. Add('begin');
  15747. ConvertProgram;
  15748. CheckSource('TestClass_Overloads',
  15749. LinesToStr([ // statements
  15750. 'rtl.createClass(this, "TObject", null, function () {',
  15751. ' this.$init = function () {',
  15752. ' };',
  15753. ' this.$final = function () {',
  15754. ' };',
  15755. ' this.DoIt = function () {',
  15756. ' this.DoIt();',
  15757. ' this.DoIt$1(1);',
  15758. ' };',
  15759. ' this.DoIt$1 = function (vI) {',
  15760. ' };',
  15761. '});',
  15762. '']),
  15763. LinesToStr([ // $mod.$main
  15764. '']));
  15765. end;
  15766. procedure TTestModule.TestClass_OverloadsAncestor;
  15767. begin
  15768. StartProgram(false);
  15769. Add('type');
  15770. Add(' TObject = class;');
  15771. Add(' TObject = class');
  15772. Add(' procedure DoIt(vA: longint);');
  15773. Add(' procedure DoIt(vA, vB: longint);');
  15774. Add(' end;');
  15775. Add(' TCar = class;');
  15776. Add(' TCar = class');
  15777. Add(' procedure DoIt(vA: longint);');
  15778. Add(' procedure DoIt(vA, vB: longint);');
  15779. Add(' end;');
  15780. Add('procedure tobject.doit(va: longint);');
  15781. Add('begin');
  15782. Add(' doit(1);');
  15783. Add(' doit(1,2);');
  15784. Add('end;');
  15785. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15786. Add('procedure tcar.doit(va: longint);');
  15787. Add('begin');
  15788. Add(' doit(1);');
  15789. Add(' doit(1,2);');
  15790. Add(' inherited doit(1);');
  15791. Add(' inherited doit(1,2);');
  15792. Add('end;');
  15793. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15794. Add('begin');
  15795. ConvertProgram;
  15796. CheckSource('TestClass_OverloadsAncestor',
  15797. LinesToStr([ // statements
  15798. 'rtl.createClass(this, "TObject", null, function () {',
  15799. ' this.$init = function () {',
  15800. ' };',
  15801. ' this.$final = function () {',
  15802. ' };',
  15803. ' this.DoIt = function (vA) {',
  15804. ' this.DoIt(1);',
  15805. ' this.DoIt$1(1,2);',
  15806. ' };',
  15807. ' this.DoIt$1 = function (vA, vB) {',
  15808. ' };',
  15809. '});',
  15810. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15811. ' this.DoIt$2 = function (vA) {',
  15812. ' this.DoIt$2(1);',
  15813. ' this.DoIt$3(1, 2);',
  15814. ' $mod.TObject.DoIt.call(this, 1);',
  15815. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15816. ' };',
  15817. ' this.DoIt$3 = function (vA, vB) {',
  15818. ' };',
  15819. '});',
  15820. '']),
  15821. LinesToStr([ // $mod.$main
  15822. '']));
  15823. end;
  15824. procedure TTestModule.TestClass_OverloadConstructor;
  15825. begin
  15826. StartProgram(false);
  15827. Add('type');
  15828. Add(' TObject = class');
  15829. Add(' constructor Create(vA: longint);');
  15830. Add(' constructor Create(vA, vB: longint);');
  15831. Add(' end;');
  15832. Add(' TCar = class');
  15833. Add(' constructor Create(vA: longint);');
  15834. Add(' constructor Create(vA, vB: longint);');
  15835. Add(' end;');
  15836. Add('constructor tobject.create(va: longint);');
  15837. Add('begin');
  15838. Add(' create(1);');
  15839. Add(' create(1,2);');
  15840. Add('end;');
  15841. Add('constructor tobject.create(va, vb: longint); begin end;');
  15842. Add('constructor tcar.create(va: longint);');
  15843. Add('begin');
  15844. Add(' create(1);');
  15845. Add(' create(1,2);');
  15846. Add(' inherited create(1);');
  15847. Add(' inherited create(1,2);');
  15848. Add('end;');
  15849. Add('constructor tcar.create(va, vb: longint); begin end;');
  15850. Add('begin');
  15851. Add(' tobject.create(1);');
  15852. Add(' tobject.create(1,2);');
  15853. Add(' tcar.create(1);');
  15854. Add(' tcar.create(1,2);');
  15855. ConvertProgram;
  15856. CheckSource('TestClass_OverloadConstructor',
  15857. LinesToStr([ // statements
  15858. 'rtl.createClass(this, "TObject", null, function () {',
  15859. ' this.$init = function () {',
  15860. ' };',
  15861. ' this.$final = function () {',
  15862. ' };',
  15863. ' this.Create = function (vA) {',
  15864. ' this.Create(1);',
  15865. ' this.Create$1(1,2);',
  15866. ' return this;',
  15867. ' };',
  15868. ' this.Create$1 = function (vA, vB) {',
  15869. ' return this;',
  15870. ' };',
  15871. '});',
  15872. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15873. ' this.Create$2 = function (vA) {',
  15874. ' this.Create$2(1);',
  15875. ' this.Create$3(1, 2);',
  15876. ' $mod.TObject.Create.call(this, 1);',
  15877. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15878. ' return this;',
  15879. ' };',
  15880. ' this.Create$3 = function (vA, vB) {',
  15881. ' return this;',
  15882. ' };',
  15883. '});',
  15884. '']),
  15885. LinesToStr([ // $mod.$main
  15886. '$mod.TObject.$create("Create", [1]);',
  15887. '$mod.TObject.$create("Create$1", [1, 2]);',
  15888. '$mod.TCar.$create("Create$2", [1]);',
  15889. '$mod.TCar.$create("Create$3", [1, 2]);',
  15890. '']));
  15891. end;
  15892. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15893. begin
  15894. StartProgram(false);
  15895. Add([
  15896. '{$mode delphi}',
  15897. 'type',
  15898. ' TObject = class end;',
  15899. ' TBird = class',
  15900. ' function {#a}GetValue: longint; overload; virtual;',
  15901. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15902. ' end;',
  15903. ' TEagle = class(TBird)',
  15904. ' function {#c}GetValue: longint; overload; override;',
  15905. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15906. ' end;',
  15907. 'function TBird.GetValue: longint;',
  15908. 'begin',
  15909. ' if 3={@a}GetValue then ;',
  15910. ' if 4={@b}GetValue(5) then ;',
  15911. 'end;',
  15912. 'function TBird.GetValue(AValue: longint): longint;',
  15913. 'begin',
  15914. 'end;',
  15915. 'function TEagle.GetValue: longint;',
  15916. 'begin',
  15917. ' if 13={@c}GetValue then ;',
  15918. ' if 14={@d}GetValue(15) then ;',
  15919. ' if 15=inherited {@a}GetValue then ;',
  15920. ' if 16=inherited {@b}GetValue(17) then ;',
  15921. 'end;',
  15922. 'function TEagle.GetValue(AValue: longint): longint;',
  15923. 'begin',
  15924. 'end;',
  15925. 'var',
  15926. ' e: TEagle;',
  15927. 'begin',
  15928. ' if 23=e.{@c}GetValue then ;',
  15929. ' if 24=e.{@d}GetValue(25) then ;']);
  15930. ConvertProgram;
  15931. CheckSource('TestClass_OverloadDelphiOverride',
  15932. LinesToStr([ // statements
  15933. 'rtl.createClass(this, "TObject", null, function () {',
  15934. ' this.$init = function () {',
  15935. ' };',
  15936. ' this.$final = function () {',
  15937. ' };',
  15938. '});',
  15939. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15940. ' this.GetValue = function () {',
  15941. ' var Result = 0;',
  15942. ' if (3 === this.GetValue()) ;',
  15943. ' if (4 === this.GetValue$1(5)) ;',
  15944. ' return Result;',
  15945. ' };',
  15946. ' this.GetValue$1 = function (AValue) {',
  15947. ' var Result = 0;',
  15948. ' return Result;',
  15949. ' };',
  15950. '});',
  15951. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15952. ' this.GetValue = function () {',
  15953. ' var Result = 0;',
  15954. ' if (13 === this.GetValue()) ;',
  15955. ' if (14 === this.GetValue$1(15)) ;',
  15956. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  15957. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  15958. ' return Result;',
  15959. ' };',
  15960. ' this.GetValue$1 = function (AValue) {',
  15961. ' var Result = 0;',
  15962. ' return Result;',
  15963. ' };',
  15964. '});',
  15965. 'this.e = null;',
  15966. '']),
  15967. LinesToStr([ // $mod.$main
  15968. 'if (23 === $mod.e.GetValue()) ;',
  15969. 'if (24 === $mod.e.GetValue$1(25)) ;',
  15970. '']));
  15971. end;
  15972. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  15973. begin
  15974. StartProgram(false);
  15975. Add([
  15976. '{$mode delphi}',
  15977. 'type',
  15978. ' TObject = class end;',
  15979. ' TAnimal = class',
  15980. ' public',
  15981. ' {#animal_a}A: longint;',
  15982. ' function {#animal_b}B: longint;',
  15983. ' end;',
  15984. ' TBird = class(TAnimal)',
  15985. ' public',
  15986. ' {#bird_a}A: double;',
  15987. ' {#bird_b}B: boolean;',
  15988. ' end;',
  15989. ' TEagle = class(TBird)',
  15990. ' public',
  15991. ' function {#eagle_a}A: boolean;',
  15992. ' {#eagle_b}B: double;',
  15993. ' end;',
  15994. 'function TAnimal.B: longint;',
  15995. 'begin',
  15996. 'end;',
  15997. 'function TEagle.A: boolean;',
  15998. 'begin',
  15999. ' {@eagle_b}B:=3.3;',
  16000. ' {@eagle_a}A();',
  16001. ' TBird(Self).{@bird_b}B:=true;',
  16002. ' TAnimal(Self).{@animal_a}A:=17;',
  16003. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16004. 'end;',
  16005. 'var',
  16006. ' e: TEagle;',
  16007. 'begin',
  16008. ' e.{@eagle_b}B:=5.3;',
  16009. ' if e.{@eagle_a}A then ;',
  16010. '']);
  16011. ConvertProgram;
  16012. CheckSource('TestClass_ReintroduceVarDelphi',
  16013. LinesToStr([ // statements
  16014. 'rtl.createClass(this, "TObject", null, function () {',
  16015. ' this.$init = function () {',
  16016. ' };',
  16017. ' this.$final = function () {',
  16018. ' };',
  16019. '});',
  16020. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16021. ' this.$init = function () {',
  16022. ' $mod.TObject.$init.call(this);',
  16023. ' this.A = 0;',
  16024. ' };',
  16025. ' this.B = function () {',
  16026. ' var Result = 0;',
  16027. ' return Result;',
  16028. ' };',
  16029. '});',
  16030. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16031. ' this.$init = function () {',
  16032. ' $mod.TAnimal.$init.call(this);',
  16033. ' this.A$1 = 0.0;',
  16034. ' this.B$1 = false;',
  16035. ' };',
  16036. '});',
  16037. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16038. ' this.$init = function () {',
  16039. ' $mod.TBird.$init.call(this);',
  16040. ' this.B$2 = 0.0;',
  16041. ' };',
  16042. ' this.A$2 = function () {',
  16043. ' var Result = false;',
  16044. ' this.B$2 = 3.3;',
  16045. ' this.A$2();',
  16046. ' this.B$1 = true;',
  16047. ' this.A = 17;',
  16048. ' this.B$1 = this.A$1 > 1;',
  16049. ' return Result;',
  16050. ' };',
  16051. '});',
  16052. 'this.e = null;',
  16053. '']),
  16054. LinesToStr([ // $mod.$main
  16055. '$mod.e.B$2 = 5.3;',
  16056. 'if ($mod.e.A$2()) ;',
  16057. '']));
  16058. end;
  16059. procedure TTestModule.TestClass_ReintroducedVar;
  16060. begin
  16061. StartProgram(false);
  16062. Add('type');
  16063. Add(' TObject = class');
  16064. Add(' strict private');
  16065. Add(' Some: longint;');
  16066. Add(' end;');
  16067. Add(' TMobile = class');
  16068. Add(' strict private');
  16069. Add(' Some: string;');
  16070. Add(' end;');
  16071. Add(' TCar = class(tmobile)');
  16072. Add(' procedure Some;');
  16073. Add(' procedure Some(vA: longint);');
  16074. Add(' end;');
  16075. Add('procedure tcar.some;');
  16076. Add('begin');
  16077. Add(' Some;');
  16078. Add(' Some(1);');
  16079. Add('end;');
  16080. Add('procedure tcar.some(va: longint); begin end;');
  16081. Add('begin');
  16082. ConvertProgram;
  16083. CheckSource('TestClass_ReintroducedVar',
  16084. LinesToStr([ // statements
  16085. 'rtl.createClass(this, "TObject", null, function () {',
  16086. ' this.$init = function () {',
  16087. ' this.Some = 0;',
  16088. ' };',
  16089. ' this.$final = function () {',
  16090. ' };',
  16091. '});',
  16092. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16093. ' this.$init = function () {',
  16094. ' $mod.TObject.$init.call(this);',
  16095. ' this.Some$1 = "";',
  16096. ' };',
  16097. '});',
  16098. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16099. ' this.Some$2 = function () {',
  16100. ' this.Some$2();',
  16101. ' this.Some$3(1);',
  16102. ' };',
  16103. ' this.Some$3 = function (vA) {',
  16104. ' };',
  16105. '});',
  16106. '']),
  16107. LinesToStr([ // $mod.$main
  16108. '']));
  16109. end;
  16110. procedure TTestModule.TestClass_RaiseDescendant;
  16111. begin
  16112. StartProgram(false);
  16113. Add([
  16114. 'type',
  16115. ' TObject = class',
  16116. ' constructor Create(Msg: string);',
  16117. ' end;',
  16118. ' Exception = class',
  16119. ' end;',
  16120. ' EConvertError = class(Exception)',
  16121. ' end;',
  16122. 'constructor TObject.Create(Msg: string); begin end;',
  16123. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16124. 'begin',
  16125. ' raise Exception.Create(''Bar1'');',
  16126. ' raise EConvertError.Create(''Bar2'');',
  16127. ' raise AssertConv(''Bar2'');',
  16128. ' raise AssertConv;',
  16129. '']);
  16130. ConvertProgram;
  16131. CheckSource('TestClass_RaiseDescendant',
  16132. LinesToStr([ // statements
  16133. 'rtl.createClass(this, "TObject", null, function () {',
  16134. ' this.$init = function () {',
  16135. ' };',
  16136. ' this.$final = function () {',
  16137. ' };',
  16138. ' this.Create = function (Msg) {',
  16139. ' return this;',
  16140. ' };',
  16141. '});',
  16142. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16143. '});',
  16144. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16145. '});',
  16146. 'this.AssertConv = function (Msg) {',
  16147. ' var Result = null;',
  16148. ' return Result;',
  16149. '};',
  16150. '']),
  16151. LinesToStr([ // $mod.$main
  16152. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16153. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16154. 'throw $mod.AssertConv("Bar2");',
  16155. 'throw $mod.AssertConv("def");',
  16156. '']));
  16157. end;
  16158. procedure TTestModule.TestClass_ExternalMethod;
  16159. begin
  16160. AddModuleWithIntfImplSrc('unit2.pas',
  16161. LinesToStr([
  16162. 'type',
  16163. ' TObject = class',
  16164. ' public',
  16165. ' procedure Intern; external name ''$DoIntern'';',
  16166. ' end;',
  16167. '']),
  16168. LinesToStr([
  16169. '']));
  16170. StartUnit(true);
  16171. Add('interface');
  16172. Add('uses unit2;');
  16173. Add('type');
  16174. Add(' TCar = class(TObject)');
  16175. Add(' public');
  16176. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16177. Add(' procedure DoIt;');
  16178. Add(' end;');
  16179. Add('implementation');
  16180. Add('procedure tcar.doit;');
  16181. Add('begin');
  16182. Add(' Intern;');
  16183. Add(' Intern();');
  16184. Add(' Intern2;');
  16185. Add(' Intern2();');
  16186. Add('end;');
  16187. Add('var Obj: TCar;');
  16188. Add('begin');
  16189. Add(' obj.intern;');
  16190. Add(' obj.intern();');
  16191. Add(' obj.intern2;');
  16192. Add(' obj.intern2();');
  16193. Add(' obj.doit;');
  16194. Add(' obj.doit();');
  16195. Add(' with obj do begin');
  16196. Add(' Intern;');
  16197. Add(' Intern();');
  16198. Add(' Intern2;');
  16199. Add(' Intern2();');
  16200. Add(' end;');
  16201. ConvertUnit;
  16202. CheckSource('TestClass_ExternalMethod',
  16203. LinesToStr([
  16204. 'var $impl = $mod.$impl;',
  16205. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16206. ' this.DoIt = function () {',
  16207. ' this.$DoIntern();',
  16208. ' this.$DoIntern();',
  16209. ' this.$DoIntern2();',
  16210. ' this.$DoIntern2();',
  16211. ' };',
  16212. ' });',
  16213. '']),
  16214. LinesToStr([ // this.$init
  16215. '$impl.Obj.$DoIntern();',
  16216. '$impl.Obj.$DoIntern();',
  16217. '$impl.Obj.$DoIntern2();',
  16218. '$impl.Obj.$DoIntern2();',
  16219. '$impl.Obj.DoIt();',
  16220. '$impl.Obj.DoIt();',
  16221. 'var $with = $impl.Obj;',
  16222. '$with.$DoIntern();',
  16223. '$with.$DoIntern();',
  16224. '$with.$DoIntern2();',
  16225. '$with.$DoIntern2();',
  16226. '']),
  16227. LinesToStr([ // implementation
  16228. '$impl.Obj = null;',
  16229. '']) );
  16230. end;
  16231. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16232. begin
  16233. StartProgram(false);
  16234. Add('type');
  16235. Add(' TObject = class');
  16236. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16237. Add(' end;');
  16238. Add('begin');
  16239. SetExpectedPasResolverError('Virtual method name must match external',
  16240. nVirtualMethodNameMustMatchExternal);
  16241. ConvertProgram;
  16242. end;
  16243. procedure TTestModule.TestClass_ExternalOverrideFail;
  16244. begin
  16245. StartProgram(false);
  16246. Add('type');
  16247. Add(' TObject = class');
  16248. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16249. Add(' end;');
  16250. Add(' TCar = class');
  16251. Add(' procedure DoIt; override; external name ''DoIt'';');
  16252. Add(' end;');
  16253. Add('begin');
  16254. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16255. nInvalidXModifierY);
  16256. ConvertProgram;
  16257. end;
  16258. procedure TTestModule.TestClass_ExternalVar;
  16259. begin
  16260. AddModuleWithIntfImplSrc('unit2.pas',
  16261. LinesToStr([
  16262. '{$modeswitch externalclass}',
  16263. 'type',
  16264. ' TObject = class',
  16265. ' public',
  16266. ' Intern: longint external name ''$Intern'';',
  16267. ' Bracket: longint external name ''["A B"]'';',
  16268. ' end;',
  16269. '']),
  16270. LinesToStr([
  16271. '']));
  16272. StartUnit(true);
  16273. Add([
  16274. 'interface',
  16275. 'uses unit2;',
  16276. '{$modeswitch externalclass}',
  16277. 'type',
  16278. ' TCar = class(tobject)',
  16279. ' public',
  16280. ' Intern2: longint external name ''$Intern2'';',
  16281. ' procedure DoIt;',
  16282. ' end;',
  16283. 'implementation',
  16284. 'procedure tcar.doit;',
  16285. 'begin',
  16286. ' Intern:=Intern+1;',
  16287. ' Intern2:=Intern2+2;',
  16288. ' Bracket:=Bracket+3;',
  16289. 'end;',
  16290. 'var Obj: TCar;',
  16291. 'begin',
  16292. ' obj.intern:=obj.intern+1;',
  16293. ' obj.intern2:=obj.intern2+2;',
  16294. ' obj.Bracket:=obj.Bracket+3;',
  16295. ' with obj do begin',
  16296. ' intern:=intern+1;',
  16297. ' intern2:=intern2+2;',
  16298. ' Bracket:=Bracket+3;',
  16299. ' end;']);
  16300. ConvertUnit;
  16301. CheckSource('TestClass_ExternalVar',
  16302. LinesToStr([
  16303. 'var $impl = $mod.$impl;',
  16304. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16305. ' this.DoIt = function () {',
  16306. ' this.$Intern = this.$Intern + 1;',
  16307. ' this.$Intern2 = this.$Intern2 + 2;',
  16308. ' this["A B"] = this["A B"] + 3;',
  16309. ' };',
  16310. ' });',
  16311. '']),
  16312. LinesToStr([
  16313. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16314. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16315. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16316. 'var $with = $impl.Obj;',
  16317. '$with.$Intern = $with.$Intern + 1;',
  16318. '$with.$Intern2 = $with.$Intern2 + 2;',
  16319. '$with["A B"] = $with["A B"] + 3;',
  16320. '']),
  16321. LinesToStr([ // implementation
  16322. '$impl.Obj = null;',
  16323. '']));
  16324. end;
  16325. procedure TTestModule.TestClass_Const;
  16326. begin
  16327. StartProgram(false);
  16328. Add([
  16329. 'type',
  16330. ' integer = longint;',
  16331. ' TClass = class of TObject;',
  16332. ' TObject = class',
  16333. ' public',
  16334. ' const cI: integer = 3;',
  16335. ' procedure DoIt;',
  16336. ' class procedure DoMore;',
  16337. ' end;',
  16338. 'procedure tobject.doit;',
  16339. 'begin',
  16340. ' if cI=4 then;',
  16341. ' if 5=cI then;',
  16342. ' if Self.cI=6 then;',
  16343. ' if 7=Self.cI then;',
  16344. ' with Self do begin',
  16345. ' if cI=11 then;',
  16346. ' if 12=cI then;',
  16347. ' end;',
  16348. 'end;',
  16349. 'class procedure tobject.domore;',
  16350. 'begin',
  16351. ' if cI=8 then;',
  16352. ' if Self.cI=9 then;',
  16353. ' if 10=cI then;',
  16354. ' if 11=Self.cI then;',
  16355. ' with Self do begin',
  16356. ' if cI=13 then;',
  16357. ' if 14=cI then;',
  16358. ' end;',
  16359. 'end;',
  16360. 'var',
  16361. ' Obj: TObject;',
  16362. ' Cla: TClass;',
  16363. 'begin',
  16364. ' if TObject.cI=21 then ;',
  16365. ' if Obj.cI=22 then ;',
  16366. ' if Cla.cI=23 then ;',
  16367. ' with obj do if ci=24 then;',
  16368. ' with TObject do if ci=25 then;',
  16369. ' with Cla do if ci=26 then;']);
  16370. ConvertProgram;
  16371. CheckSource('TestClass_Const',
  16372. LinesToStr([
  16373. 'rtl.createClass(this, "TObject", null, function () {',
  16374. ' this.cI = 3;',
  16375. ' this.$init = function () {',
  16376. ' };',
  16377. ' this.$final = function () {',
  16378. ' };',
  16379. ' this.DoIt = function () {',
  16380. ' if (this.cI === 4) ;',
  16381. ' if (5 === this.cI) ;',
  16382. ' if (this.cI === 6) ;',
  16383. ' if (7 === this.cI) ;',
  16384. ' if (this.cI === 11) ;',
  16385. ' if (12 === this.cI) ;',
  16386. ' };',
  16387. ' this.DoMore = function () {',
  16388. ' if (this.cI === 8) ;',
  16389. ' if (this.cI === 9) ;',
  16390. ' if (10 === this.cI) ;',
  16391. ' if (11 === this.cI) ;',
  16392. ' if (this.cI === 13) ;',
  16393. ' if (14 === this.cI) ;',
  16394. ' };',
  16395. '});',
  16396. 'this.Obj = null;',
  16397. 'this.Cla = null;',
  16398. '']),
  16399. LinesToStr([
  16400. 'if ($mod.TObject.cI === 21) ;',
  16401. 'if ($mod.Obj.cI === 22) ;',
  16402. 'if ($mod.Cla.cI === 23) ;',
  16403. 'var $with = $mod.Obj;',
  16404. 'if ($with.cI === 24) ;',
  16405. 'var $with1 = $mod.TObject;',
  16406. 'if ($with1.cI === 25) ;',
  16407. 'var $with2 = $mod.Cla;',
  16408. 'if ($with2.cI === 26) ;',
  16409. '']));
  16410. end;
  16411. procedure TTestModule.TestClass_ConstEnum;
  16412. begin
  16413. StartProgram(false);
  16414. Add([
  16415. 'type',
  16416. ' TEnum = (red,blue);',
  16417. ' TObject = class',
  16418. ' end;',
  16419. ' TAnimal = class',
  16420. ' public',
  16421. ' type TSubEnum = (light,dark);',
  16422. ' const a = high(TEnum);',
  16423. ' const b = high(TSubEnum);',
  16424. ' end;',
  16425. ' TBird = class(TAnimal)',
  16426. ' public',
  16427. ' const c = high(TEnum);',
  16428. ' const d = high(TSubEnum);',
  16429. ' end;',
  16430. ' TAnt = class',
  16431. ' public',
  16432. ' const e = high(TEnum);',
  16433. ' const f = high(TBird.TSubEnum);',
  16434. ' end;',
  16435. 'begin',
  16436. '']);
  16437. ConvertProgram;
  16438. CheckSource('TestClass_ConstEnum',
  16439. LinesToStr([
  16440. 'this.TEnum = {',
  16441. ' "0": "red",',
  16442. ' red: 0,',
  16443. ' "1": "blue",',
  16444. ' blue: 1',
  16445. '};',
  16446. 'rtl.createClass(this, "TObject", null, function () {',
  16447. ' this.$init = function () {',
  16448. ' };',
  16449. ' this.$final = function () {',
  16450. ' };',
  16451. '});',
  16452. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16453. ' this.TSubEnum = {',
  16454. ' "0": "light",',
  16455. ' light: 0,',
  16456. ' "1": "dark",',
  16457. ' dark: 1',
  16458. ' };',
  16459. ' this.a = $mod.TEnum.blue;',
  16460. ' this.b = this.TSubEnum.dark;',
  16461. '});',
  16462. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16463. ' this.c = $mod.TEnum.blue;',
  16464. ' this.d = this.TSubEnum.dark;',
  16465. '});',
  16466. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16467. ' this.e = $mod.TEnum.blue;',
  16468. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16469. '});',
  16470. '']),
  16471. LinesToStr([
  16472. '']));
  16473. end;
  16474. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16475. begin
  16476. StartProgram(false);
  16477. Add([
  16478. 'type',
  16479. ' TObject = class',
  16480. ' const cI: longint = 3;',
  16481. ' procedure Fly;',
  16482. ' procedure Run;',
  16483. ' end;',
  16484. ' TBird = class',
  16485. ' procedure Go;',
  16486. ' end;',
  16487. 'procedure tobject.fly;',
  16488. 'const cI: word = 4;',
  16489. 'begin',
  16490. ' if cI=Self.cI then ;',
  16491. 'end;',
  16492. 'procedure tobject.run;',
  16493. 'const cI: word = 5;',
  16494. 'begin',
  16495. ' if cI=Self.cI then ;',
  16496. 'end;',
  16497. 'procedure tbird.go;',
  16498. 'const cI: word = 6;',
  16499. 'begin',
  16500. ' if cI=Self.cI then ;',
  16501. 'end;',
  16502. 'begin',
  16503. '']);
  16504. ConvertProgram;
  16505. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16506. LinesToStr([
  16507. 'rtl.createClass(this, "TObject", null, function () {',
  16508. ' this.cI = 3;',
  16509. ' this.$init = function () {',
  16510. ' };',
  16511. ' this.$final = function () {',
  16512. ' };',
  16513. ' var cI$1 = 4;',
  16514. ' this.Fly = function () {',
  16515. ' if (cI$1 === this.cI) ;',
  16516. ' };',
  16517. ' var cI$2 = 5;',
  16518. ' this.Run = function () {',
  16519. ' if (cI$2 === this.cI) ;',
  16520. ' };',
  16521. '});',
  16522. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16523. ' var cI$3 = 6;',
  16524. ' this.Go = function () {',
  16525. ' if (cI$3 === this.cI) ;',
  16526. ' };',
  16527. '});',
  16528. '']),
  16529. LinesToStr([
  16530. '']));
  16531. end;
  16532. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16533. begin
  16534. StartUnit(false);
  16535. Add([
  16536. 'interface',
  16537. 'type',
  16538. ' TObject = class',
  16539. ' const cI: longint = 3;',
  16540. ' procedure Fly;',
  16541. ' procedure Run;',
  16542. ' end;',
  16543. ' TBird = class',
  16544. ' procedure Go;',
  16545. ' end;',
  16546. 'implementation',
  16547. 'procedure tobject.fly;',
  16548. 'const cI: word = 4;',
  16549. 'begin',
  16550. ' if cI=Self.cI then ;',
  16551. 'end;',
  16552. 'procedure tobject.run;',
  16553. 'const cI: word = 5;',
  16554. 'begin',
  16555. ' if cI=Self.cI then ;',
  16556. 'end;',
  16557. 'procedure tbird.go;',
  16558. 'const cI: word = 6;',
  16559. 'begin',
  16560. ' if cI=Self.cI then ;',
  16561. 'end;',
  16562. '']);
  16563. ConvertUnit;
  16564. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16565. LinesToStr([
  16566. 'rtl.createClass(this, "TObject", null, function () {',
  16567. ' this.cI = 3;',
  16568. ' this.$init = function () {',
  16569. ' };',
  16570. ' this.$final = function () {',
  16571. ' };',
  16572. ' var cI$1 = 4;',
  16573. ' this.Fly = function () {',
  16574. ' if (cI$1 === this.cI) ;',
  16575. ' };',
  16576. ' var cI$2 = 5;',
  16577. ' this.Run = function () {',
  16578. ' if (cI$2 === this.cI) ;',
  16579. ' };',
  16580. '});',
  16581. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16582. ' var cI$3 = 6;',
  16583. ' this.Go = function () {',
  16584. ' if (cI$3 === this.cI) ;',
  16585. ' };',
  16586. '});',
  16587. '']),
  16588. '',
  16589. '');
  16590. end;
  16591. procedure TTestModule.TestClass_LocalVarSelfFail;
  16592. begin
  16593. StartProgram(false);
  16594. Add([
  16595. 'type',
  16596. ' TObject = class',
  16597. ' constructor Create;',
  16598. ' end;',
  16599. 'constructor tobject.create;',
  16600. 'var self: longint;',
  16601. 'begin',
  16602. 'end',
  16603. 'begin',
  16604. '']);
  16605. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16606. ConvertProgram;
  16607. end;
  16608. procedure TTestModule.TestClass_ArgSelfFail;
  16609. begin
  16610. StartProgram(false);
  16611. Add([
  16612. 'type',
  16613. ' TObject = class',
  16614. ' procedure DoIt(Self: longint);',
  16615. ' end;',
  16616. 'procedure tobject.doit(self: longint);',
  16617. 'begin',
  16618. 'end',
  16619. 'begin',
  16620. '']);
  16621. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16622. ConvertProgram;
  16623. end;
  16624. procedure TTestModule.TestClass_NestedProcSelf;
  16625. begin
  16626. StartProgram(false);
  16627. Add([
  16628. 'type',
  16629. ' TObject = class',
  16630. ' Key: longint;',
  16631. ' class var State: longint;',
  16632. ' procedure DoIt;',
  16633. ' function GetSize: longint; virtual; abstract;',
  16634. ' procedure SetSize(Value: longint); virtual; abstract;',
  16635. ' property Size: longint read GetSize write SetSize;',
  16636. ' end;',
  16637. 'procedure tobject.doit;',
  16638. ' procedure Sub;',
  16639. ' begin',
  16640. ' key:=key+2;',
  16641. ' self.key:=self.key+3;',
  16642. ' state:=state+4;',
  16643. ' self.state:=self.state+5;',
  16644. ' tobject.state:=tobject.state+6;',
  16645. ' size:=size+7;',
  16646. ' self.size:=self.size+8;',
  16647. ' end;',
  16648. 'begin',
  16649. ' sub;',
  16650. ' key:=key+12;',
  16651. ' self.key:=self.key+13;',
  16652. ' state:=state+14;',
  16653. ' self.state:=self.state+15;',
  16654. ' tobject.state:=tobject.state+16;',
  16655. ' size:=size+17;',
  16656. ' self.size:=self.size+18;',
  16657. 'end;',
  16658. 'begin',
  16659. '']);
  16660. ConvertProgram;
  16661. CheckSource('TestClass_NestedProcSelf',
  16662. LinesToStr([ // statements
  16663. 'rtl.createClass(this, "TObject", null, function () {',
  16664. ' this.State = 0;',
  16665. ' this.$init = function () {',
  16666. ' this.Key = 0;',
  16667. ' };',
  16668. ' this.$final = function () {',
  16669. ' };',
  16670. ' this.DoIt = function () {',
  16671. ' var $Self = this;',
  16672. ' function Sub() {',
  16673. ' $Self.Key = $Self.Key + 2;',
  16674. ' $Self.Key = $Self.Key + 3;',
  16675. ' $mod.TObject.State = $Self.State + 4;',
  16676. ' $mod.TObject.State = $Self.State + 5;',
  16677. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16678. ' $Self.SetSize($Self.GetSize() + 7);',
  16679. ' $Self.SetSize($Self.GetSize() + 8);',
  16680. ' };',
  16681. ' Sub();',
  16682. ' this.Key = this.Key + 12;',
  16683. ' $Self.Key = $Self.Key + 13;',
  16684. ' $mod.TObject.State = this.State + 14;',
  16685. ' $mod.TObject.State = $Self.State + 15;',
  16686. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16687. ' this.SetSize(this.GetSize() + 17);',
  16688. ' $Self.SetSize($Self.GetSize() + 18);',
  16689. ' };',
  16690. '});',
  16691. '']),
  16692. LinesToStr([ // $mod.$main
  16693. '']));
  16694. end;
  16695. procedure TTestModule.TestClass_NestedProcSelf2;
  16696. begin
  16697. StartProgram(false);
  16698. Add([
  16699. 'type',
  16700. ' TObject = class',
  16701. ' Key: longint;',
  16702. ' class var State: longint;',
  16703. ' function GetSize: longint; virtual; abstract;',
  16704. ' procedure SetSize(Value: longint); virtual; abstract;',
  16705. ' property Size: longint read GetSize write SetSize;',
  16706. ' end;',
  16707. ' TBird = class',
  16708. ' procedure DoIt;',
  16709. ' end;',
  16710. 'procedure tbird.doit;',
  16711. ' procedure Sub;',
  16712. ' begin',
  16713. ' key:=key+2;',
  16714. ' self.key:=self.key+3;',
  16715. ' state:=state+4;',
  16716. ' self.state:=self.state+5;',
  16717. ' tobject.state:=tobject.state+6;',
  16718. ' size:=size+7;',
  16719. ' self.size:=self.size+8;',
  16720. ' end;',
  16721. 'begin',
  16722. ' sub;',
  16723. ' key:=key+12;',
  16724. ' self.key:=self.key+13;',
  16725. ' state:=state+14;',
  16726. ' self.state:=self.state+15;',
  16727. ' tobject.state:=tobject.state+16;',
  16728. ' size:=size+17;',
  16729. ' self.size:=self.size+18;',
  16730. 'end;',
  16731. 'begin',
  16732. '']);
  16733. ConvertProgram;
  16734. CheckSource('TestClass_NestedProcSelf2',
  16735. LinesToStr([ // statements
  16736. 'rtl.createClass(this, "TObject", null, function () {',
  16737. ' this.State = 0;',
  16738. ' this.$init = function () {',
  16739. ' this.Key = 0;',
  16740. ' };',
  16741. ' this.$final = function () {',
  16742. ' };',
  16743. '});',
  16744. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16745. ' this.DoIt = function () {',
  16746. ' var $Self = this;',
  16747. ' function Sub() {',
  16748. ' $Self.Key = $Self.Key + 2;',
  16749. ' $Self.Key = $Self.Key + 3;',
  16750. ' $mod.TObject.State = $Self.State + 4;',
  16751. ' $mod.TObject.State = $Self.State + 5;',
  16752. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16753. ' $Self.SetSize($Self.GetSize() + 7);',
  16754. ' $Self.SetSize($Self.GetSize() + 8);',
  16755. ' };',
  16756. ' Sub();',
  16757. ' this.Key = this.Key + 12;',
  16758. ' $Self.Key = $Self.Key + 13;',
  16759. ' $mod.TObject.State = this.State + 14;',
  16760. ' $mod.TObject.State = $Self.State + 15;',
  16761. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16762. ' this.SetSize(this.GetSize() + 17);',
  16763. ' $Self.SetSize($Self.GetSize() + 18);',
  16764. ' };',
  16765. '});',
  16766. '']),
  16767. LinesToStr([ // $mod.$main
  16768. '']));
  16769. end;
  16770. procedure TTestModule.TestClass_NestedProcClassSelf;
  16771. begin
  16772. StartProgram(false);
  16773. Add([
  16774. 'type',
  16775. ' TObject = class',
  16776. ' class var State: longint;',
  16777. ' class procedure DoIt;',
  16778. ' class function GetSize: longint; virtual; abstract;',
  16779. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16780. ' class property Size: longint read GetSize write SetSize;',
  16781. ' end;',
  16782. 'class procedure tobject.doit;',
  16783. ' procedure Sub;',
  16784. ' begin',
  16785. ' state:=state+2;',
  16786. ' self.state:=self.state+3;',
  16787. ' tobject.state:=tobject.state+4;',
  16788. ' size:=size+5;',
  16789. ' self.size:=self.size+6;',
  16790. ' tobject.size:=tobject.size+7;',
  16791. ' end;',
  16792. 'begin',
  16793. ' sub;',
  16794. ' state:=state+12;',
  16795. ' self.state:=self.state+13;',
  16796. ' tobject.state:=tobject.state+14;',
  16797. ' size:=size+15;',
  16798. ' self.size:=self.size+16;',
  16799. ' tobject.size:=tobject.size+17;',
  16800. 'end;',
  16801. 'begin',
  16802. '']);
  16803. ConvertProgram;
  16804. CheckSource('TestClass_NestedProcClassSelf',
  16805. LinesToStr([ // statements
  16806. 'rtl.createClass(this, "TObject", null, function () {',
  16807. ' this.State = 0;',
  16808. ' this.$init = function () {',
  16809. ' };',
  16810. ' this.$final = function () {',
  16811. ' };',
  16812. ' this.DoIt = function () {',
  16813. ' var $Self = this;',
  16814. ' function Sub() {',
  16815. ' $mod.TObject.State = $Self.State + 2;',
  16816. ' $mod.TObject.State = $Self.State + 3;',
  16817. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16818. ' $Self.SetSize($Self.GetSize() + 5);',
  16819. ' $Self.SetSize($Self.GetSize() + 6);',
  16820. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16821. ' };',
  16822. ' Sub();',
  16823. ' $mod.TObject.State = this.State + 12;',
  16824. ' $mod.TObject.State = $Self.State + 13;',
  16825. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16826. ' this.SetSize(this.GetSize() + 15);',
  16827. ' $Self.SetSize($Self.GetSize() + 16);',
  16828. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16829. ' };',
  16830. '});',
  16831. '']),
  16832. LinesToStr([ // $mod.$main
  16833. '']));
  16834. end;
  16835. procedure TTestModule.TestClass_NestedProcCallInherited;
  16836. begin
  16837. StartProgram(false);
  16838. Add([
  16839. 'type',
  16840. ' TObject = class',
  16841. ' function DoIt(k: boolean): longint; virtual;',
  16842. ' end;',
  16843. ' TBird = class',
  16844. ' function DoIt(k: boolean): longint; override;',
  16845. ' end;',
  16846. 'function tobject.doit(k: boolean): longint;',
  16847. 'begin',
  16848. 'end;',
  16849. 'function tbird.doit(k: boolean): longint;',
  16850. ' procedure Sub;',
  16851. ' begin',
  16852. ' inherited DoIt(true);',
  16853. //' if inherited DoIt(false)=4 then ;',
  16854. ' end;',
  16855. 'begin',
  16856. ' Sub;',
  16857. ' inherited;',
  16858. ' inherited DoIt(true);',
  16859. //' if inherited DoIt(false)=14 then ;',
  16860. 'end;',
  16861. 'begin',
  16862. '']);
  16863. ConvertProgram;
  16864. CheckSource('TestClass_NestedProcCallInherited',
  16865. LinesToStr([ // statements
  16866. 'rtl.createClass(this, "TObject", null, function () {',
  16867. ' this.$init = function () {',
  16868. ' };',
  16869. ' this.$final = function () {',
  16870. ' };',
  16871. ' this.DoIt = function (k) {',
  16872. ' var Result = 0;',
  16873. ' return Result;',
  16874. ' };',
  16875. '});',
  16876. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16877. ' this.DoIt = function (k) {',
  16878. ' var $Self = this;',
  16879. ' var Result = 0;',
  16880. ' function Sub() {',
  16881. ' $mod.TObject.DoIt.call($Self, true);',
  16882. ' };',
  16883. ' Sub();',
  16884. ' $mod.TObject.DoIt.apply(this, arguments);',
  16885. ' $mod.TObject.DoIt.call(this, true);',
  16886. ' return Result;',
  16887. ' };',
  16888. '});',
  16889. '']),
  16890. LinesToStr([ // $mod.$main
  16891. '']));
  16892. end;
  16893. procedure TTestModule.TestClass_TObjectFree;
  16894. begin
  16895. StartProgram(false);
  16896. Add([
  16897. 'type',
  16898. ' TObject = class',
  16899. ' Obj: tobject;',
  16900. ' procedure Free;',
  16901. ' procedure Release;',
  16902. ' end;',
  16903. 'procedure tobject.free;',
  16904. 'begin',
  16905. 'end;',
  16906. 'procedure tobject.release;',
  16907. 'begin',
  16908. ' free;',
  16909. ' if true then free;',
  16910. 'end;',
  16911. 'function DoIt(o: tobject): tobject;',
  16912. 'var l: tobject;',
  16913. 'begin',
  16914. ' o.free;',
  16915. ' o.free();',
  16916. ' l.free;',
  16917. ' l.free();',
  16918. ' o.obj.free;',
  16919. ' o.obj.free();',
  16920. ' with o do obj.free;',
  16921. ' with o do obj.free();',
  16922. ' result.Free;',
  16923. ' result.Free();',
  16924. 'end;',
  16925. 'var o: tobject;',
  16926. ' a: array of tobject;',
  16927. 'begin',
  16928. ' o.free;',
  16929. ' o.obj.free;',
  16930. ' a[1+2].free;',
  16931. '']);
  16932. ConvertProgram;
  16933. CheckSource('TestClass_TObjectFree',
  16934. LinesToStr([ // statements
  16935. 'rtl.createClass(this, "TObject", null, function () {',
  16936. ' this.$init = function () {',
  16937. ' this.Obj = null;',
  16938. ' };',
  16939. ' this.$final = function () {',
  16940. ' this.Obj = undefined;',
  16941. ' };',
  16942. ' this.Free = function () {',
  16943. ' };',
  16944. ' this.Release = function () {',
  16945. ' this.Free();',
  16946. ' if (true) this.Free();',
  16947. ' };',
  16948. '});',
  16949. 'this.DoIt = function (o) {',
  16950. ' var Result = null;',
  16951. ' var l = null;',
  16952. ' o = rtl.freeLoc(o);',
  16953. ' o = rtl.freeLoc(o);',
  16954. ' l = rtl.freeLoc(l);',
  16955. ' l = rtl.freeLoc(l);',
  16956. ' rtl.free(o, "Obj");',
  16957. ' rtl.free(o, "Obj");',
  16958. ' rtl.free(o, "Obj");',
  16959. ' rtl.free(o, "Obj");',
  16960. ' Result = rtl.freeLoc(Result);',
  16961. ' Result = rtl.freeLoc(Result);',
  16962. ' return Result;',
  16963. '};',
  16964. 'this.o = null;',
  16965. 'this.a = [];',
  16966. '']),
  16967. LinesToStr([ // $mod.$main
  16968. 'rtl.free($mod, "o");',
  16969. 'rtl.free($mod.o, "Obj");',
  16970. 'rtl.free($mod.a, 1 + 2);',
  16971. '']));
  16972. end;
  16973. procedure TTestModule.TestClass_TObjectFree_VarArg;
  16974. begin
  16975. StartProgram(false);
  16976. Add([
  16977. 'type',
  16978. ' TObject = class',
  16979. ' Obj: tobject;',
  16980. ' procedure Free;',
  16981. ' end;',
  16982. 'procedure tobject.free;',
  16983. 'begin',
  16984. 'end;',
  16985. 'procedure DoIt(var o: tobject);',
  16986. 'begin',
  16987. ' o.free;',
  16988. ' o.free();',
  16989. 'end;',
  16990. 'begin',
  16991. '']);
  16992. ConvertProgram;
  16993. CheckSource('TestClass_TObjectFree_VarArg',
  16994. LinesToStr([ // statements
  16995. 'rtl.createClass(this, "TObject", null, function () {',
  16996. ' this.$init = function () {',
  16997. ' this.Obj = null;',
  16998. ' };',
  16999. ' this.$final = function () {',
  17000. ' this.Obj = undefined;',
  17001. ' };',
  17002. ' this.Free = function () {',
  17003. ' };',
  17004. '});',
  17005. 'this.DoIt = function (o) {',
  17006. ' o.set(rtl.freeLoc(o.get()));',
  17007. ' o.set(rtl.freeLoc(o.get()));',
  17008. '};',
  17009. '']),
  17010. LinesToStr([ // $mod.$main
  17011. '']));
  17012. end;
  17013. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17014. begin
  17015. StartProgram(false);
  17016. Add([
  17017. 'type',
  17018. ' TObject = class',
  17019. ' constructor Create;',
  17020. ' procedure Free;',
  17021. ' end;',
  17022. 'constructor TObject.Create; begin end;',
  17023. 'procedure tobject.free; begin end;',
  17024. 'begin',
  17025. ' with tobject.create do free;',
  17026. '']);
  17027. ConvertProgram;
  17028. CheckSource('TestClass_TObjectFreeNewInstance',
  17029. LinesToStr([ // statements
  17030. 'rtl.createClass(this, "TObject", null, function () {',
  17031. ' this.$init = function () {',
  17032. ' };',
  17033. ' this.$final = function () {',
  17034. ' };',
  17035. ' this.Create = function () {',
  17036. ' return this;',
  17037. ' };',
  17038. ' this.Free = function () {',
  17039. ' };',
  17040. '});',
  17041. '']),
  17042. LinesToStr([ // $mod.$main
  17043. 'var $with = $mod.TObject.$create("Create");',
  17044. '$with=rtl.freeLoc($with);',
  17045. '']));
  17046. end;
  17047. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17048. begin
  17049. StartProgram(false);
  17050. Add([
  17051. 'type',
  17052. ' TObject = class',
  17053. ' destructor Destroy;',
  17054. ' procedure Free;',
  17055. ' end;',
  17056. 'destructor TObject.Destroy; begin end;',
  17057. 'procedure tobject.free; begin end;',
  17058. 'var o: tobject;',
  17059. 'begin',
  17060. ' o.free;',
  17061. '']);
  17062. Converter.UseLowerCase:=true;
  17063. ConvertProgram;
  17064. CheckSource('TestClass_TObjectFreeLowerCase',
  17065. LinesToStr([ // statements
  17066. 'rtl.createClass(this, "tobject", null, function () {',
  17067. ' this.$init = function () {',
  17068. ' };',
  17069. ' this.$final = function () {',
  17070. ' };',
  17071. ' rtl.tObjectDestroy = "destroy";',
  17072. ' this.destroy = function () {',
  17073. ' };',
  17074. ' this.free = function () {',
  17075. ' };',
  17076. '});',
  17077. 'this.o = null;',
  17078. '']),
  17079. LinesToStr([ // $mod.$main
  17080. 'rtl.free($mod, "o");',
  17081. '']));
  17082. end;
  17083. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17084. begin
  17085. StartProgram(false);
  17086. Add([
  17087. 'type',
  17088. ' TObject = class',
  17089. ' procedure Free;',
  17090. ' function GetObj: tobject; virtual; abstract;',
  17091. ' end;',
  17092. 'procedure tobject.free;',
  17093. 'begin',
  17094. 'end;',
  17095. 'var o: tobject;',
  17096. 'begin',
  17097. ' o.getobj.free;',
  17098. '']);
  17099. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17100. ConvertProgram;
  17101. end;
  17102. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17103. begin
  17104. StartProgram(false);
  17105. Add([
  17106. 'type',
  17107. ' TObject = class',
  17108. ' procedure Free;',
  17109. ' FObj: TObject;',
  17110. ' property Obj: tobject read FObj write FObj;',
  17111. ' end;',
  17112. 'procedure tobject.free;',
  17113. 'begin',
  17114. 'end;',
  17115. 'var o: tobject;',
  17116. 'begin',
  17117. ' o.obj.free;',
  17118. '']);
  17119. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17120. ConvertProgram;
  17121. end;
  17122. procedure TTestModule.TestClass_ForIn;
  17123. begin
  17124. StartProgram(false);
  17125. Add([
  17126. 'type',
  17127. ' TObject = class end;',
  17128. ' TItem = TObject;',
  17129. ' TEnumerator = class',
  17130. ' FCurrent: TItem;',
  17131. ' property Current: TItem read FCurrent;',
  17132. ' function MoveNext: boolean;',
  17133. ' end;',
  17134. ' TBird = class',
  17135. ' function GetEnumerator: TEnumerator;',
  17136. ' end;',
  17137. 'function TEnumerator.MoveNext: boolean;',
  17138. 'begin',
  17139. 'end;',
  17140. 'function TBird.GetEnumerator: TEnumerator;',
  17141. 'begin',
  17142. 'end;',
  17143. 'var',
  17144. ' b: TBird;',
  17145. ' i, i2: TItem;',
  17146. 'begin',
  17147. ' for i in b do i2:=i;']);
  17148. ConvertProgram;
  17149. CheckSource('TestClass_ForIn',
  17150. LinesToStr([ // statements
  17151. 'rtl.createClass(this, "TObject", null, function () {',
  17152. ' this.$init = function () {',
  17153. ' };',
  17154. ' this.$final = function () {',
  17155. ' };',
  17156. '});',
  17157. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17158. ' this.$init = function () {',
  17159. ' $mod.TObject.$init.call(this);',
  17160. ' this.FCurrent = null;',
  17161. ' };',
  17162. ' this.$final = function () {',
  17163. ' this.FCurrent = undefined;',
  17164. ' $mod.TObject.$final.call(this);',
  17165. ' };',
  17166. ' this.MoveNext = function () {',
  17167. ' var Result = false;',
  17168. ' return Result;',
  17169. ' };',
  17170. '});',
  17171. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17172. ' this.GetEnumerator = function () {',
  17173. ' var Result = null;',
  17174. ' return Result;',
  17175. ' };',
  17176. '});',
  17177. 'this.b = null;',
  17178. 'this.i = null;',
  17179. 'this.i2 = null;'
  17180. ]),
  17181. LinesToStr([ // $mod.$main
  17182. 'var $in = $mod.b.GetEnumerator();',
  17183. 'try {',
  17184. ' while ($in.MoveNext()){',
  17185. ' $mod.i = $in.FCurrent;',
  17186. ' $mod.i2 = $mod.i;',
  17187. ' }',
  17188. '} finally {',
  17189. ' $in = rtl.freeLoc($in)',
  17190. '};',
  17191. '']));
  17192. end;
  17193. procedure TTestModule.TestClass_DispatchMessage;
  17194. begin
  17195. StartProgram(false);
  17196. Add([
  17197. 'type',
  17198. ' {$DispatchField DispInt}',
  17199. ' {$DispatchStrField DispStr}',
  17200. ' TObject = class',
  17201. ' procedure Dispatch(var Msg); virtual; abstract;',
  17202. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17203. ' end;',
  17204. ' THopMsg = record',
  17205. ' DispInt: longint;',
  17206. ' end;',
  17207. ' TPutMsg = record',
  17208. ' DispStr: string;',
  17209. ' end;',
  17210. ' TBird = class',
  17211. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17212. ' procedure Run; overload; virtual; abstract;',
  17213. ' procedure Run(var Msg); overload; message ''Fast'';',
  17214. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17215. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17216. ' end;',
  17217. 'procedure TBird.Run(var Msg);',
  17218. 'begin',
  17219. 'end;',
  17220. 'begin',
  17221. '']);
  17222. ConvertProgram;
  17223. CheckResolverUnexpectedHints(true);
  17224. CheckSource('TestClass_Message',
  17225. LinesToStr([ // statements
  17226. 'rtl.createClass(this, "TObject", null, function () {',
  17227. ' this.$init = function () {',
  17228. ' };',
  17229. ' this.$final = function () {',
  17230. ' };',
  17231. '});',
  17232. 'rtl.recNewT(this, "THopMsg", function () {',
  17233. ' this.DispInt = 0;',
  17234. ' this.$eq = function (b) {',
  17235. ' return this.DispInt === b.DispInt;',
  17236. ' };',
  17237. ' this.$assign = function (s) {',
  17238. ' this.DispInt = s.DispInt;',
  17239. ' return this;',
  17240. ' };',
  17241. '});',
  17242. 'rtl.recNewT(this, "TPutMsg", function () {',
  17243. ' this.DispStr = "";',
  17244. ' this.$eq = function (b) {',
  17245. ' return this.DispStr === b.DispStr;',
  17246. ' };',
  17247. ' this.$assign = function (s) {',
  17248. ' this.DispStr = s.DispStr;',
  17249. ' return this;',
  17250. ' };',
  17251. '});',
  17252. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17253. ' this.Run$1 = function (Msg) {',
  17254. ' };',
  17255. ' this.$msgint = {',
  17256. ' "2": "Fly",',
  17257. ' "3": "Hop"',
  17258. ' };',
  17259. ' this.$msgstr = {',
  17260. ' Fast: "Run$1",',
  17261. ' foo: "Put"',
  17262. ' };',
  17263. '});',
  17264. '']),
  17265. LinesToStr([ // $mod.$main
  17266. '']));
  17267. end;
  17268. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17269. begin
  17270. StartProgram(false);
  17271. Add([
  17272. 'type',
  17273. ' TObject = class',
  17274. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17275. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17276. ' end;',
  17277. 'begin',
  17278. '']);
  17279. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17280. ConvertProgram;
  17281. end;
  17282. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17283. begin
  17284. StartProgram(false);
  17285. Add([
  17286. 'type',
  17287. ' TObject = class',
  17288. ' procedure Dispatch(var Msg); virtual; abstract;',
  17289. ' end;',
  17290. ' TFlyMsg = record',
  17291. ' FlyId: longint;',
  17292. ' end;',
  17293. ' TBird = class',
  17294. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17295. ' end;',
  17296. 'begin',
  17297. '']);
  17298. ConvertProgram;
  17299. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17300. end;
  17301. procedure TTestModule.TestClassOf_Create;
  17302. begin
  17303. StartProgram(false);
  17304. Add('type');
  17305. Add(' TObject = class');
  17306. Add(' constructor Create;');
  17307. Add(' end;');
  17308. Add(' TClass = class of TObject;');
  17309. Add('constructor tobject.create; begin end;');
  17310. Add('var');
  17311. Add(' Obj: tobject;');
  17312. Add(' C: tclass;');
  17313. Add('begin');
  17314. Add(' obj:=C.create;');
  17315. Add(' with c do obj:=create;');
  17316. ConvertProgram;
  17317. CheckSource('TestClassOf_Create',
  17318. LinesToStr([ // statements
  17319. 'rtl.createClass(this, "TObject", null, function () {',
  17320. ' this.$init = function () {',
  17321. ' };',
  17322. ' this.$final = function () {',
  17323. ' };',
  17324. ' this.Create = function () {',
  17325. ' return this;',
  17326. ' };',
  17327. '});',
  17328. 'this.Obj = null;',
  17329. 'this.C = null;'
  17330. ]),
  17331. LinesToStr([ // $mod.$main
  17332. '$mod.Obj = $mod.C.$create("Create");',
  17333. 'var $with = $mod.C;',
  17334. '$mod.Obj = $with.$create("Create");',
  17335. '']));
  17336. end;
  17337. procedure TTestModule.TestClassOf_Call;
  17338. begin
  17339. StartProgram(false);
  17340. Add('type');
  17341. Add(' TObject = class');
  17342. Add(' class procedure DoIt;');
  17343. Add(' end;');
  17344. Add(' TClass = class of TObject;');
  17345. Add('class procedure tobject.doit; begin end;');
  17346. Add('var');
  17347. Add(' C: tclass;');
  17348. Add('begin');
  17349. Add(' c.doit;');
  17350. Add(' with c do doit;');
  17351. ConvertProgram;
  17352. CheckSource('TestClassOf_Call',
  17353. LinesToStr([ // statements
  17354. 'rtl.createClass(this, "TObject", null, function () {',
  17355. ' this.$init = function () {',
  17356. ' };',
  17357. ' this.$final = function () {',
  17358. ' };',
  17359. ' this.DoIt = function () {',
  17360. ' };',
  17361. '});',
  17362. 'this.C = null;'
  17363. ]),
  17364. LinesToStr([ // $mod.$main
  17365. '$mod.C.DoIt();',
  17366. 'var $with = $mod.C;',
  17367. '$with.DoIt();',
  17368. '']));
  17369. end;
  17370. procedure TTestModule.TestClassOf_Assign;
  17371. begin
  17372. StartProgram(false);
  17373. Add('type');
  17374. Add(' TClass = class of TObject;');
  17375. Add(' TObject = class');
  17376. Add(' ClassType: TClass; ');
  17377. Add(' end;');
  17378. Add('var');
  17379. Add(' Obj: tobject;');
  17380. Add(' C: tclass;');
  17381. Add('begin');
  17382. Add(' c:=nil;');
  17383. Add(' c:=obj.classtype;');
  17384. ConvertProgram;
  17385. CheckSource('TestClassOf_Assign',
  17386. LinesToStr([ // statements
  17387. 'rtl.createClass(this, "TObject", null, function () {',
  17388. ' this.$init = function () {',
  17389. ' this.ClassType = null;',
  17390. ' };',
  17391. ' this.$final = function () {',
  17392. ' this.ClassType = undefined;',
  17393. ' };',
  17394. '});',
  17395. 'this.Obj = null;',
  17396. 'this.C = null;'
  17397. ]),
  17398. LinesToStr([ // $mod.$main
  17399. '$mod.C = null;',
  17400. '$mod.C = $mod.Obj.ClassType;',
  17401. '']));
  17402. end;
  17403. procedure TTestModule.TestClassOf_Is;
  17404. begin
  17405. StartProgram(false);
  17406. Add('type');
  17407. Add(' TClass = class of TObject;');
  17408. Add(' TObject = class');
  17409. Add(' end;');
  17410. Add(' TCar = class');
  17411. Add(' end;');
  17412. Add(' TCars = class of TCar;');
  17413. Add('var');
  17414. Add(' Obj: tobject;');
  17415. Add(' C: tclass;');
  17416. Add(' Cars: tcars;');
  17417. Add('begin');
  17418. Add(' if c is tcar then ;');
  17419. Add(' if c is tcars then ;');
  17420. ConvertProgram;
  17421. CheckSource('TestClassOf_Is',
  17422. LinesToStr([ // statements
  17423. 'rtl.createClass(this, "TObject", null, function () {',
  17424. ' this.$init = function () {',
  17425. ' };',
  17426. ' this.$final = function () {',
  17427. ' };',
  17428. '});',
  17429. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17430. '});',
  17431. 'this.Obj = null;',
  17432. 'this.C = null;',
  17433. 'this.Cars = null;'
  17434. ]),
  17435. LinesToStr([ // $mod.$main
  17436. 'if(rtl.is($mod.C,$mod.TCar));',
  17437. 'if(rtl.is($mod.C,$mod.TCar));',
  17438. '']));
  17439. end;
  17440. procedure TTestModule.TestClassOf_Compare;
  17441. begin
  17442. StartProgram(false);
  17443. Add('type');
  17444. Add(' TClass = class of TObject;');
  17445. Add(' TObject = class');
  17446. Add(' ClassType: TClass; ');
  17447. Add(' end;');
  17448. Add('var');
  17449. Add(' b: boolean;');
  17450. Add(' Obj: tobject;');
  17451. Add(' C: tclass;');
  17452. Add('begin');
  17453. Add(' b:=c=nil;');
  17454. Add(' b:=nil=c;');
  17455. Add(' b:=c=obj.classtype;');
  17456. Add(' b:=obj.classtype=c;');
  17457. Add(' b:=c=TObject;');
  17458. Add(' b:=TObject=c;');
  17459. Add(' b:=c<>nil;');
  17460. Add(' b:=nil<>c;');
  17461. Add(' b:=c<>obj.classtype;');
  17462. Add(' b:=obj.classtype<>c;');
  17463. Add(' b:=c<>TObject;');
  17464. Add(' b:=TObject<>c;');
  17465. ConvertProgram;
  17466. CheckSource('TestClassOf_Compare',
  17467. LinesToStr([ // statements
  17468. 'rtl.createClass(this, "TObject", null, function () {',
  17469. ' this.$init = function () {',
  17470. ' this.ClassType = null;',
  17471. ' };',
  17472. ' this.$final = function () {',
  17473. ' this.ClassType = undefined;',
  17474. ' };',
  17475. '});',
  17476. 'this.b = false;',
  17477. 'this.Obj = null;',
  17478. 'this.C = null;'
  17479. ]),
  17480. LinesToStr([ // $mod.$main
  17481. '$mod.b = $mod.C === null;',
  17482. '$mod.b = null === $mod.C;',
  17483. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17484. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17485. '$mod.b = $mod.C === $mod.TObject;',
  17486. '$mod.b = $mod.TObject === $mod.C;',
  17487. '$mod.b = $mod.C !== null;',
  17488. '$mod.b = null !== $mod.C;',
  17489. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17490. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17491. '$mod.b = $mod.C !== $mod.TObject;',
  17492. '$mod.b = $mod.TObject !== $mod.C;',
  17493. '']));
  17494. end;
  17495. procedure TTestModule.TestClassOf_ClassVar;
  17496. begin
  17497. StartProgram(false);
  17498. Add('type');
  17499. Add(' TObject = class');
  17500. Add(' class var id: longint;');
  17501. Add(' end;');
  17502. Add(' TClass = class of TObject;');
  17503. Add('var');
  17504. Add(' C: tclass;');
  17505. Add('begin');
  17506. Add(' C.id:=C.id;');
  17507. ConvertProgram;
  17508. CheckSource('TestClassOf_ClassVar',
  17509. LinesToStr([ // statements
  17510. 'rtl.createClass(this, "TObject", null, function () {',
  17511. ' this.id = 0;',
  17512. ' this.$init = function () {',
  17513. ' };',
  17514. ' this.$final = function () {',
  17515. ' };',
  17516. '});',
  17517. 'this.C = null;'
  17518. ]),
  17519. LinesToStr([ // $mod.$main
  17520. '$mod.TObject.id = $mod.C.id;',
  17521. '']));
  17522. end;
  17523. procedure TTestModule.TestClassOf_ClassMethod;
  17524. begin
  17525. StartProgram(false);
  17526. Add('type');
  17527. Add(' TObject = class');
  17528. Add(' class function DoIt(i: longint = 0): longint;');
  17529. Add(' end;');
  17530. Add(' TClass = class of TObject;');
  17531. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17532. Add('var');
  17533. Add(' i: longint;');
  17534. Add(' C: tclass;');
  17535. Add('begin');
  17536. Add(' C.DoIt;');
  17537. Add(' C.DoIt();');
  17538. Add(' i:=C.DoIt;');
  17539. Add(' i:=C.DoIt();');
  17540. ConvertProgram;
  17541. CheckSource('TestClassOf_ClassMethod',
  17542. LinesToStr([ // statements
  17543. 'rtl.createClass(this, "TObject", null, function () {',
  17544. ' this.$init = function () {',
  17545. ' };',
  17546. ' this.$final = function () {',
  17547. ' };',
  17548. ' this.DoIt = function (i) {',
  17549. ' var Result = 0;',
  17550. ' return Result;',
  17551. ' };',
  17552. '});',
  17553. 'this.i = 0;',
  17554. 'this.C = null;'
  17555. ]),
  17556. LinesToStr([ // $mod.$main
  17557. '$mod.C.DoIt(0);',
  17558. '$mod.C.DoIt(0);',
  17559. '$mod.i = $mod.C.DoIt(0);',
  17560. '$mod.i = $mod.C.DoIt(0);',
  17561. '']));
  17562. end;
  17563. procedure TTestModule.TestClassOf_ClassProperty;
  17564. begin
  17565. StartProgram(false);
  17566. Add([
  17567. 'type',
  17568. ' TObject = class',
  17569. ' class var FA: longint;',
  17570. ' class function GetA: longint;',
  17571. ' class procedure SetA(Value: longint);',
  17572. ' class property pA: longint read fa write fa;',
  17573. ' class property pB: longint read geta write seta;',
  17574. ' end;',
  17575. ' TObjectClass = class of tobject;',
  17576. 'class function tobject.geta: longint; begin end;',
  17577. 'class procedure tobject.seta(value: longint); begin end;',
  17578. 'var',
  17579. ' b: boolean;',
  17580. ' Obj: tobject;',
  17581. ' Cla: tobjectclass;',
  17582. 'begin',
  17583. ' obj.pa:=obj.pa;',
  17584. ' obj.pb:=obj.pb;',
  17585. ' b:=obj.pa=4;',
  17586. ' b:=obj.pb=obj.pb;',
  17587. ' b:=5=obj.pa;',
  17588. ' cla.pa:=6;',
  17589. ' cla.pa:=cla.pa;',
  17590. ' cla.pb:=cla.pb;',
  17591. ' b:=cla.pa=7;',
  17592. ' b:=cla.pb=cla.pb;',
  17593. ' b:=8=cla.pa;',
  17594. ' tobject.pa:=9;',
  17595. ' tobject.pb:=tobject.pb;',
  17596. ' b:=tobject.pa=10;',
  17597. ' b:=11=tobject.pa;',
  17598. '']);
  17599. ConvertProgram;
  17600. CheckSource('TestClassOf_ClassProperty',
  17601. LinesToStr([ // statements
  17602. 'rtl.createClass(this, "TObject", null, function () {',
  17603. ' this.FA = 0;',
  17604. ' this.$init = function () {',
  17605. ' };',
  17606. ' this.$final = function () {',
  17607. ' };',
  17608. ' this.GetA = function () {',
  17609. ' var Result = 0;',
  17610. ' return Result;',
  17611. ' };',
  17612. ' this.SetA = function (Value) {',
  17613. ' };',
  17614. '});',
  17615. 'this.b = false;',
  17616. 'this.Obj = null;',
  17617. 'this.Cla = null;'
  17618. ]),
  17619. LinesToStr([ // $mod.$main
  17620. '$mod.TObject.FA = $mod.Obj.FA;',
  17621. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17622. '$mod.b = $mod.Obj.FA === 4;',
  17623. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17624. '$mod.b = 5 === $mod.Obj.FA;',
  17625. '$mod.TObject.FA = 6;',
  17626. '$mod.TObject.FA = $mod.Cla.FA;',
  17627. '$mod.Cla.SetA($mod.Cla.GetA());',
  17628. '$mod.b = $mod.Cla.FA === 7;',
  17629. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17630. '$mod.b = 8 === $mod.Cla.FA;',
  17631. '$mod.TObject.FA = 9;',
  17632. '$mod.TObject.SetA($mod.TObject.GetA());',
  17633. '$mod.b = $mod.TObject.FA === 10;',
  17634. '$mod.b = 11 === $mod.TObject.FA;',
  17635. '']));
  17636. end;
  17637. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17638. begin
  17639. StartProgram(false);
  17640. Add('type');
  17641. Add(' TObject = class');
  17642. Add(' class var GlobalId: longint;');
  17643. Add(' class procedure ProcA;');
  17644. Add(' end;');
  17645. Add('class procedure tobject.proca;');
  17646. Add('var b: boolean;');
  17647. Add('begin');
  17648. Add(' b:=self=nil;');
  17649. Add(' b:=self.globalid=3;');
  17650. Add(' b:=4=self.globalid;');
  17651. Add(' self.globalid:=5;');
  17652. Add(' self.proca;');
  17653. Add('end;');
  17654. Add('begin');
  17655. ConvertProgram;
  17656. CheckSource('TestClassOf_ClassMethodSelf',
  17657. LinesToStr([ // statements
  17658. 'rtl.createClass(this, "TObject", null, function () {',
  17659. ' this.GlobalId = 0;',
  17660. ' this.$init = function () {',
  17661. ' };',
  17662. ' this.$final = function () {',
  17663. ' };',
  17664. ' this.ProcA = function () {',
  17665. ' var b = false;',
  17666. ' b = this === null;',
  17667. ' b = this.GlobalId === 3;',
  17668. ' b = 4 === this.GlobalId;',
  17669. ' $mod.TObject.GlobalId = 5;',
  17670. ' this.ProcA();',
  17671. ' };',
  17672. '});'
  17673. ]),
  17674. LinesToStr([ // $mod.$main
  17675. '']));
  17676. end;
  17677. procedure TTestModule.TestClassOf_TypeCast;
  17678. begin
  17679. StartProgram(false);
  17680. Add('type');
  17681. Add(' TObject = class');
  17682. Add(' class procedure {#TObject_DoIt}DoIt;');
  17683. Add(' end;');
  17684. Add(' TClass = class of TObject;');
  17685. Add(' TMobile = class');
  17686. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17687. Add(' end;');
  17688. Add(' TMobileClass = class of TMobile;');
  17689. Add(' TCar = class(TMobile)');
  17690. Add(' class procedure {#TCar_DoIt}DoIt;');
  17691. Add(' end;');
  17692. Add(' TCarClass = class of TCar;');
  17693. Add('class procedure TObject.DoIt;');
  17694. Add('begin');
  17695. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17696. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17697. Add('end;');
  17698. Add('class procedure TMobile.DoIt;');
  17699. Add('begin');
  17700. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17701. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17702. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17703. Add('end;');
  17704. Add('class procedure TCar.DoIt; begin end;');
  17705. Add('var');
  17706. Add(' ObjC: TClass;');
  17707. Add(' MobileC: TMobileClass;');
  17708. Add(' CarC: TCarClass;');
  17709. Add('begin');
  17710. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17711. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17712. Add(' CarC.{@TCar_DoIt}DoIt;');
  17713. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17714. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17715. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17716. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17717. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17718. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17719. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17720. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17721. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17722. ConvertProgram;
  17723. CheckSource('TestClassOf_TypeCast',
  17724. LinesToStr([ // statements
  17725. 'rtl.createClass(this, "TObject", null, function () {',
  17726. ' this.$init = function () {',
  17727. ' };',
  17728. ' this.$final = function () {',
  17729. ' };',
  17730. ' this.DoIt = function () {',
  17731. ' this.DoIt();',
  17732. ' this.DoIt$1();',
  17733. ' };',
  17734. '});',
  17735. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17736. ' this.DoIt$1 = function () {',
  17737. ' this.DoIt();',
  17738. ' this.DoIt$1();',
  17739. ' this.DoIt$2();',
  17740. ' };',
  17741. '});',
  17742. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17743. ' this.DoIt$2 = function () {',
  17744. ' };',
  17745. '});',
  17746. 'this.ObjC = null;',
  17747. 'this.MobileC = null;',
  17748. 'this.CarC = null;',
  17749. '']),
  17750. LinesToStr([ // $mod.$main
  17751. '$mod.ObjC.DoIt();',
  17752. '$mod.MobileC.DoIt$1();',
  17753. '$mod.CarC.DoIt$2();',
  17754. '$mod.ObjC.DoIt();',
  17755. '$mod.ObjC.DoIt$1();',
  17756. '$mod.ObjC.DoIt$2();',
  17757. '$mod.MobileC.DoIt();',
  17758. '$mod.MobileC.DoIt$1();',
  17759. '$mod.MobileC.DoIt$2();',
  17760. '$mod.CarC.DoIt();',
  17761. '$mod.CarC.DoIt$1();',
  17762. '$mod.CarC.DoIt$2();',
  17763. '']));
  17764. end;
  17765. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17766. begin
  17767. StartProgram(false);
  17768. Add('type');
  17769. Add(' TObject = class');
  17770. Add(' function CurNow: longint; ');
  17771. Add(' class function Now: longint; ');
  17772. Add(' end;');
  17773. Add('function TObject.CurNow: longint; begin end;');
  17774. Add('class function TObject.Now: longint; begin end;');
  17775. Add('var');
  17776. Add(' Obj: tobject;');
  17777. Add(' vI: longint;');
  17778. Add('begin');
  17779. Add(' obj.curnow;');
  17780. Add(' vi:=obj.curnow;');
  17781. Add(' tobject.now;');
  17782. Add(' vi:=tobject.now;');
  17783. ConvertProgram;
  17784. CheckSource('TestClassOf_ImplicitFunctionCall',
  17785. LinesToStr([ // statements
  17786. 'rtl.createClass(this, "TObject", null, function () {',
  17787. ' this.$init = function () {',
  17788. ' };',
  17789. ' this.$final = function () {',
  17790. ' };',
  17791. ' this.CurNow = function () {',
  17792. ' var Result = 0;',
  17793. ' return Result;',
  17794. ' };',
  17795. ' this.Now = function () {',
  17796. ' var Result = 0;',
  17797. ' return Result;',
  17798. ' };',
  17799. '});',
  17800. 'this.Obj = null;',
  17801. 'this.vI = 0;',
  17802. '']),
  17803. LinesToStr([ // $mod.$main
  17804. '$mod.Obj.CurNow();',
  17805. '$mod.vI = $mod.Obj.CurNow();',
  17806. '$mod.TObject.Now();',
  17807. '$mod.vI = $mod.TObject.Now();',
  17808. '']));
  17809. end;
  17810. procedure TTestModule.TestClassOf_Const;
  17811. begin
  17812. StartProgram(false);
  17813. Add([
  17814. 'type',
  17815. ' TObject = class',
  17816. ' end;',
  17817. ' TBird = TObject;',
  17818. ' TBirds = class of TBird;',
  17819. ' TEagles = TBirds;',
  17820. ' THawk = class(TBird);',
  17821. 'const',
  17822. ' Hawk: TEagles = THawk;',
  17823. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17824. ' TBird,',
  17825. ' THawk',
  17826. ' );',
  17827. 'begin']);
  17828. ConvertProgram;
  17829. CheckSource('TestClassOf_Const',
  17830. LinesToStr([ // statements
  17831. 'rtl.createClass(this, "TObject", null, function () {',
  17832. ' this.$init = function () {',
  17833. ' };',
  17834. ' this.$final = function () {',
  17835. ' };',
  17836. '});',
  17837. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17838. '});',
  17839. 'this.Hawk = this.THawk;',
  17840. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17841. '']),
  17842. LinesToStr([ // $mod.$main
  17843. '']));
  17844. end;
  17845. procedure TTestModule.TestNestedClass_Alias;
  17846. begin
  17847. WithTypeInfo:=true;
  17848. StartProgram(false);
  17849. Add([
  17850. 'type',
  17851. ' TObject = class',
  17852. ' type TNested = type longint;',
  17853. ' end;',
  17854. 'type TAlias = type tobject.tnested;',
  17855. 'var i: tobject.tnested = 3;',
  17856. 'var j: TAlias = 4;',
  17857. 'begin',
  17858. ' if typeinfo(TAlias)=nil then ;',
  17859. ' if typeinfo(tobject.tnested)=nil then ;',
  17860. '']);
  17861. ConvertProgram;
  17862. CheckSource('TestNestedClass_Alias',
  17863. LinesToStr([ // statements
  17864. 'rtl.createClass(this, "TObject", null, function () {',
  17865. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17866. ' this.$init = function () {',
  17867. ' };',
  17868. ' this.$final = function () {',
  17869. ' };',
  17870. '});',
  17871. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17872. 'this.i = 3;',
  17873. 'this.j = 4;',
  17874. '']),
  17875. LinesToStr([ // $mod.$main
  17876. 'if ($mod.$rtti["TAlias"] === null) ;',
  17877. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17878. '']));
  17879. end;
  17880. procedure TTestModule.TestNestedClass_Record;
  17881. begin
  17882. WithTypeInfo:=true;
  17883. StartProgram(false);
  17884. Add([
  17885. 'type',
  17886. ' TObject = class',
  17887. ' type TPoint = record',
  17888. ' x,y: byte;',
  17889. ' end;',
  17890. ' procedure DoIt(t: TPoint);',
  17891. ' end;',
  17892. 'procedure tobject.DoIt(t: TPoint);',
  17893. 'var p: TPoint;',
  17894. 'begin',
  17895. ' t.x:=t.y;',
  17896. ' p:=t;',
  17897. 'end;',
  17898. 'var',
  17899. ' p: tobject.tpoint = (x:2; y:4);',
  17900. ' o: TObject;',
  17901. 'begin',
  17902. ' p:=p;',
  17903. ' o.doit(p);',
  17904. '']);
  17905. ConvertProgram;
  17906. CheckSource('TestNestedClass_Record',
  17907. LinesToStr([ // statements
  17908. 'rtl.createClass(this, "TObject", null, function () {',
  17909. ' rtl.recNewT(this, "TPoint", function () {',
  17910. ' this.x = 0;',
  17911. ' this.y = 0;',
  17912. ' this.$eq = function (b) {',
  17913. ' return (this.x === b.x) && (this.y === b.y);',
  17914. ' };',
  17915. ' this.$assign = function (s) {',
  17916. ' this.x = s.x;',
  17917. ' this.y = s.y;',
  17918. ' return this;',
  17919. ' };',
  17920. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17921. ' $r.addField("x", rtl.byte);',
  17922. ' $r.addField("y", rtl.byte);',
  17923. ' });',
  17924. ' this.$init = function () {',
  17925. ' };',
  17926. ' this.$final = function () {',
  17927. ' };',
  17928. ' this.DoIt = function (t) {',
  17929. ' var p = this.TPoint.$new();',
  17930. ' t.x = t.y;',
  17931. ' p.$assign(t);',
  17932. ' };',
  17933. '});',
  17934. 'this.p = this.TObject.TPoint.$clone({',
  17935. ' x: 2,',
  17936. ' y: 4',
  17937. '});',
  17938. 'this.o = null;',
  17939. '']),
  17940. LinesToStr([ // $mod.$main
  17941. '$mod.p.$assign($mod.p);',
  17942. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17943. '']));
  17944. end;
  17945. procedure TTestModule.TestNestedClass_Class;
  17946. begin
  17947. StartProgram(false);
  17948. Add([
  17949. 'type',
  17950. ' TObject = class end;',
  17951. ' TBird = class',
  17952. ' type TLeg = class',
  17953. ' FId: longint;',
  17954. ' constructor Create;',
  17955. ' function Create(i: longint): TLeg;',
  17956. ' end;',
  17957. ' function DoIt(b: TBird): Tleg;',
  17958. ' end;',
  17959. 'constructor tbird.tleg.create;',
  17960. 'begin',
  17961. ' FId:=3;',
  17962. 'end;',
  17963. 'function tbird.tleg.Create(i: longint): TLeg;',
  17964. 'begin',
  17965. ' Create;',
  17966. ' Result:=TLeg.Create;',
  17967. ' Result:=TBird.TLeg.Create;',
  17968. ' Result:=Create(3);',
  17969. ' FId:=i;',
  17970. 'end;',
  17971. 'function tbird.DoIt(b: tbird): tleg;',
  17972. 'begin',
  17973. ' Result.Create;',
  17974. ' Result:=TLeg.Create;',
  17975. ' Result:=TBird.TLeg.Create;',
  17976. ' Result:=Result.Create(3);',
  17977. 'end;',
  17978. 'var',
  17979. ' b: Tbird.tleg;',
  17980. 'begin',
  17981. ' b.Create;',
  17982. ' b:=TBird.TLeg.Create;',
  17983. ' b:=b.Create(3);',
  17984. '']);
  17985. ConvertProgram;
  17986. CheckSource('TestNestedClass_Class',
  17987. LinesToStr([ // statements
  17988. 'rtl.createClass(this, "TObject", null, function () {',
  17989. ' this.$init = function () {',
  17990. ' };',
  17991. ' this.$final = function () {',
  17992. ' };',
  17993. '});',
  17994. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17995. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  17996. ' this.$init = function () {',
  17997. ' $mod.TObject.$init.call(this);',
  17998. ' this.FId = 0;',
  17999. ' };',
  18000. ' this.Create = function () {',
  18001. ' this.FId = 3;',
  18002. ' return this;',
  18003. ' };',
  18004. ' this.Create$1 = function (i) {',
  18005. ' var Result = null;',
  18006. ' this.Create();',
  18007. ' Result = $mod.TBird.TLeg.$create("Create");',
  18008. ' Result = $mod.TBird.TLeg.$create("Create");',
  18009. ' Result = this.Create$1(3);',
  18010. ' this.FId = i;',
  18011. ' return Result;',
  18012. ' };',
  18013. ' }, "TBird.TLeg");',
  18014. ' this.DoIt = function (b) {',
  18015. ' var Result = null;',
  18016. ' Result.Create();',
  18017. ' Result = this.TLeg.$create("Create");',
  18018. ' Result = $mod.TBird.TLeg.$create("Create");',
  18019. ' Result = Result.Create$1(3);',
  18020. ' return Result;',
  18021. ' };',
  18022. '});',
  18023. 'this.b = null;',
  18024. '']),
  18025. LinesToStr([ // $mod.$main
  18026. '$mod.b.Create();',
  18027. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18028. '$mod.b = $mod.b.Create$1(3);',
  18029. '']));
  18030. end;
  18031. procedure TTestModule.TestNestedClass_CallInherited;
  18032. begin
  18033. StartProgram(false);
  18034. Add([
  18035. 'type',
  18036. ' TObject = class end;',
  18037. ' TBird = class',
  18038. ' type',
  18039. ' TWing = class',
  18040. ' function Fly(w: word = 17): word; virtual;',
  18041. ' end;',
  18042. ' end;',
  18043. ' TEagle = class(TBird)',
  18044. ' type',
  18045. ' TEagleWing = class(TWing)',
  18046. ' function Fly(w: word): word; override;',
  18047. ' end;',
  18048. ' end;',
  18049. 'function TBird.TWing.Fly(w: word): word;',
  18050. 'begin',
  18051. 'end;',
  18052. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18053. 'begin',
  18054. ' inherited;',
  18055. ' inherited Fly;',
  18056. ' inherited Fly(3);',
  18057. ' Result:=inherited Fly;',
  18058. ' Result:=inherited Fly(4);',
  18059. 'end;',
  18060. 'begin',
  18061. '']);
  18062. ConvertProgram;
  18063. CheckSource('TestNestedClass_CallInherited',
  18064. LinesToStr([ // statements
  18065. 'rtl.createClass(this, "TObject", null, function () {',
  18066. ' this.$init = function () {',
  18067. ' };',
  18068. ' this.$final = function () {',
  18069. ' };',
  18070. '});',
  18071. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18072. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18073. ' this.Fly = function (w) {',
  18074. ' var Result = 0;',
  18075. ' return Result;',
  18076. ' };',
  18077. ' }, "TBird.TWing");',
  18078. '});',
  18079. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18080. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18081. ' this.Fly = function (w) {',
  18082. ' var Result = 0;',
  18083. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18084. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18085. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18086. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18087. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18088. ' return Result;',
  18089. ' };',
  18090. ' }, "TEagle.TEagleWing");',
  18091. '});',
  18092. '']),
  18093. LinesToStr([ // $mod.$main
  18094. '']));
  18095. end;
  18096. procedure TTestModule.TestExternalClass_Var;
  18097. begin
  18098. StartProgram(false);
  18099. Add([
  18100. '{$modeswitch externalclass}',
  18101. 'type',
  18102. ' TExtA = class external name ''ExtObj''',
  18103. ' Id: longint external name ''$Id'';',
  18104. ' B: longint;',
  18105. ' end;',
  18106. 'var Obj: TExtA;',
  18107. 'begin',
  18108. ' obj.id:=obj.id+1;',
  18109. ' obj.B:=obj.B+1;']);
  18110. ConvertProgram;
  18111. CheckSource('TestExternalClass_Var',
  18112. LinesToStr([ // statements
  18113. 'this.Obj = null;',
  18114. '']),
  18115. LinesToStr([ // $mod.$main
  18116. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18117. '$mod.Obj.B = $mod.Obj.B + 1;',
  18118. '']));
  18119. end;
  18120. procedure TTestModule.TestExternalClass_Const;
  18121. begin
  18122. StartProgram(false);
  18123. Add([
  18124. '{$modeswitch externalclass}',
  18125. 'type',
  18126. ' TExtA = class external name ''ExtObj''',
  18127. ' const Two: longint = 2;',
  18128. ' const Three = 3;',
  18129. ' const Id: longint;',
  18130. ' end;',
  18131. ' TExtB = class external name ''ExtB''',
  18132. ' A: TExtA;',
  18133. ' end;',
  18134. 'var',
  18135. ' A: texta;',
  18136. ' B: textb;',
  18137. ' i: longint;',
  18138. 'begin',
  18139. ' i:=a.two;',
  18140. ' i:=texta.two;',
  18141. ' i:=a.three;',
  18142. ' i:=texta.three;',
  18143. ' i:=a.id;',
  18144. ' i:=texta.id;',
  18145. '']);
  18146. ConvertProgram;
  18147. CheckSource('TestExternalClass_Const',
  18148. LinesToStr([ // statements
  18149. 'this.A = null;',
  18150. 'this.B = null;',
  18151. 'this.i = 0;',
  18152. '']),
  18153. LinesToStr([ // $mod.$main
  18154. '$mod.i = 2;',
  18155. '$mod.i = 2;',
  18156. '$mod.i = 3;',
  18157. '$mod.i = 3;',
  18158. '$mod.i = $mod.A.Id;',
  18159. '$mod.i = ExtObj.Id;',
  18160. '']));
  18161. end;
  18162. procedure TTestModule.TestExternalClass_Dollar;
  18163. begin
  18164. StartProgram(false);
  18165. Add([
  18166. '{$modeswitch externalclass}',
  18167. 'type',
  18168. ' TExtA = class external name ''$''',
  18169. ' Id: longint external name ''$'';',
  18170. ' function Bla(i: longint): longint; external name ''$'';',
  18171. ' end;',
  18172. 'function dollar(k: longint): longint; external name ''$'';',
  18173. 'var Obj: TExtA;',
  18174. 'begin',
  18175. ' dollar(1);',
  18176. ' obj.id:=obj.id+2;',
  18177. ' obj.Bla(3);',
  18178. '']);
  18179. ConvertProgram;
  18180. CheckSource('TestExternalClass_Dollar',
  18181. LinesToStr([ // statements
  18182. 'this.Obj = null;',
  18183. '']),
  18184. LinesToStr([ // $mod.$main
  18185. '$(1);',
  18186. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18187. '$mod.Obj.$(3);',
  18188. '']));
  18189. end;
  18190. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18191. begin
  18192. StartProgram(false);
  18193. Add('{$modeswitch externalclass}');
  18194. Add('type');
  18195. Add(' TExtA = class external name ''ExtA''');
  18196. Add(' Id: longint external name ''$Id'';');
  18197. Add(' end;');
  18198. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18199. Add(' Id: longint;');
  18200. Add(' end;');
  18201. Add('begin');
  18202. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18203. ConvertProgram;
  18204. end;
  18205. procedure TTestModule.TestExternalClass_Method;
  18206. begin
  18207. StartProgram(false);
  18208. Add(['{$modeswitch externalclass}',
  18209. 'type',
  18210. ' TExtA = class external name ''ExtObj''',
  18211. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18212. ' procedure DoSome(Id: longint = 1);',
  18213. ' end;',
  18214. 'var Obj: texta;',
  18215. 'begin',
  18216. ' obj.doit;',
  18217. ' obj.doit();',
  18218. ' obj.doit(2);',
  18219. ' with obj do begin',
  18220. ' doit;',
  18221. ' doit();',
  18222. ' doit(3);',
  18223. ' end;']);
  18224. ConvertProgram;
  18225. CheckSource('TestExternalClass_Method',
  18226. LinesToStr([ // statements
  18227. 'this.Obj = null;',
  18228. '']),
  18229. LinesToStr([ // $mod.$main
  18230. '$mod.Obj.$Execute(1);',
  18231. '$mod.Obj.$Execute(1);',
  18232. '$mod.Obj.$Execute(2);',
  18233. 'var $with = $mod.Obj;',
  18234. '$with.$Execute(1);',
  18235. '$with.$Execute(1);',
  18236. '$with.$Execute(3);',
  18237. '']));
  18238. end;
  18239. procedure TTestModule.TestExternalClass_ClassMethod;
  18240. begin
  18241. StartProgram(false);
  18242. Add([
  18243. '{$modeswitch externalclass}',
  18244. 'type',
  18245. ' TExtA = class external name ''ExtObj''',
  18246. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18247. ' end;',
  18248. ' TExtB = TExtA;',
  18249. 'var p: Pointer;',
  18250. 'begin',
  18251. ' texta.doit;',
  18252. ' texta.doit();',
  18253. ' texta.doit(2);',
  18254. ' p:[email protected];',
  18255. ' with texta do begin',
  18256. ' doit;',
  18257. ' doit();',
  18258. ' doit(3);',
  18259. ' p:=@DoIt;',
  18260. ' end;',
  18261. ' textb.doit;',
  18262. ' textb.doit();',
  18263. ' textb.doit(4);',
  18264. ' with textb do begin',
  18265. ' doit;',
  18266. ' doit();',
  18267. ' doit(5);',
  18268. ' end;',
  18269. '']);
  18270. ConvertProgram;
  18271. CheckSource('TestExternalClass_ClassMethod',
  18272. LinesToStr([ // statements
  18273. 'this.p = null;',
  18274. '']),
  18275. LinesToStr([ // $mod.$main
  18276. 'ExtObj.$Execute(1);',
  18277. 'ExtObj.$Execute(1);',
  18278. 'ExtObj.$Execute(2);',
  18279. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18280. 'ExtObj.$Execute(1);',
  18281. 'ExtObj.$Execute(1);',
  18282. 'ExtObj.$Execute(3);',
  18283. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18284. 'ExtObj.$Execute(1);',
  18285. 'ExtObj.$Execute(1);',
  18286. 'ExtObj.$Execute(4);',
  18287. 'ExtObj.$Execute(1);',
  18288. 'ExtObj.$Execute(1);',
  18289. 'ExtObj.$Execute(5);',
  18290. '']));
  18291. end;
  18292. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18293. begin
  18294. StartProgram(false);
  18295. Add([
  18296. '{$modeswitch externalclass}',
  18297. 'type',
  18298. ' TExtA = class external name ''ExtObj''',
  18299. ' class procedure DoIt(Id: longint = 1); static;',
  18300. ' end;',
  18301. 'var p: Pointer;',
  18302. 'begin',
  18303. ' texta.doit;',
  18304. ' texta.doit();',
  18305. ' texta.doit(2);',
  18306. ' p:[email protected];',
  18307. ' with texta do begin',
  18308. ' doit;',
  18309. ' doit();',
  18310. ' doit(3);',
  18311. ' p:=@DoIt;',
  18312. ' end;',
  18313. '']);
  18314. ConvertProgram;
  18315. CheckSource('TestExternalClass_ClassMethodStatic',
  18316. LinesToStr([ // statements
  18317. 'this.p = null;',
  18318. '']),
  18319. LinesToStr([ // $mod.$main
  18320. 'ExtObj.DoIt(1);',
  18321. 'ExtObj.DoIt(1);',
  18322. 'ExtObj.DoIt(2);',
  18323. '$mod.p = ExtObj.DoIt;',
  18324. 'ExtObj.DoIt(1);',
  18325. 'ExtObj.DoIt(1);',
  18326. 'ExtObj.DoIt(3);',
  18327. '$mod.p = ExtObj.DoIt;',
  18328. '']));
  18329. end;
  18330. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18331. begin
  18332. StartProgram(false);
  18333. Add([
  18334. '{$modeswitch externalclass}',
  18335. 'type',
  18336. ' TBird = class external name ''Array''',
  18337. ' end;',
  18338. 'function GetPtr: Pointer;',
  18339. 'begin',
  18340. 'end;',
  18341. 'procedure Write(const p);',
  18342. 'begin',
  18343. 'end;',
  18344. 'procedure WriteLn; varargs;',
  18345. 'begin',
  18346. 'end;',
  18347. 'begin',
  18348. ' if TBird(GetPtr)=nil then ;',
  18349. ' Write(GetPtr);',
  18350. ' WriteLn(GetPtr);',
  18351. ' Write(TBird(GetPtr));',
  18352. ' WriteLn(TBird(GetPtr));',
  18353. '']);
  18354. ConvertProgram;
  18355. CheckSource('TestFunctionResultInTypeCast',
  18356. LinesToStr([ // statements
  18357. 'this.GetPtr = function () {',
  18358. ' var Result = null;',
  18359. ' return Result;',
  18360. '};',
  18361. 'this.Write = function (p) {',
  18362. '};',
  18363. 'this.WriteLn = function () {',
  18364. '};',
  18365. '']),
  18366. LinesToStr([
  18367. 'if ($mod.GetPtr() === null) ;',
  18368. '$mod.Write($mod.GetPtr());',
  18369. '$mod.WriteLn($mod.GetPtr());',
  18370. '$mod.Write($mod.GetPtr());',
  18371. '$mod.WriteLn($mod.GetPtr());',
  18372. '']));
  18373. end;
  18374. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18375. begin
  18376. StartProgram(false);
  18377. Add([
  18378. '{$modeswitch externalclass}',
  18379. 'type',
  18380. ' TExtA = class external name ''ExtObjA''',
  18381. ' procedure ProcA; virtual;',
  18382. ' procedure ProcB; virtual;',
  18383. ' end;',
  18384. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18385. ' end;',
  18386. ' TExtC = class (TExtB)',
  18387. ' procedure ProcA; override;',
  18388. ' end;',
  18389. 'procedure TExtC.ProcA;',
  18390. 'begin',
  18391. ' ProcA;',
  18392. ' Self.ProcA;',
  18393. ' ProcB;',
  18394. ' Self.ProcB;',
  18395. 'end;',
  18396. 'var',
  18397. ' A: texta;',
  18398. ' B: textb;',
  18399. ' C: textc;',
  18400. 'begin',
  18401. ' a.proca;',
  18402. ' b.proca;',
  18403. ' c.proca;']);
  18404. ConvertProgram;
  18405. CheckSource('TestExternalClass_NonExternalOverride',
  18406. LinesToStr([ // statements
  18407. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18408. ' this.$init = function () {',
  18409. ' };',
  18410. ' this.$final = function () {',
  18411. ' };',
  18412. ' this.ProcA = function () {',
  18413. ' this.ProcA();',
  18414. ' this.ProcA();',
  18415. ' this.ProcB();',
  18416. ' this.ProcB();',
  18417. ' };',
  18418. '});',
  18419. 'this.A = null;',
  18420. 'this.B = null;',
  18421. 'this.C = null;',
  18422. '']),
  18423. LinesToStr([ // $mod.$main
  18424. '$mod.A.ProcA();',
  18425. '$mod.B.ProcA();',
  18426. '$mod.C.ProcA();',
  18427. '']));
  18428. end;
  18429. procedure TTestModule.TestExternalClass_OverloadHint;
  18430. begin
  18431. StartProgram(false);
  18432. Add([
  18433. '{$modeswitch externalclass}',
  18434. 'type',
  18435. ' TExtA = class external name ''ExtObjA''',
  18436. ' procedure DoIt;',
  18437. ' procedure DoIt(i: longint);',
  18438. ' end;',
  18439. 'begin',
  18440. '']);
  18441. ConvertProgram;
  18442. CheckResolverUnexpectedHints(true);
  18443. CheckSource('TestExternalClass_OverloadHint',
  18444. LinesToStr([ // statements
  18445. '']),
  18446. LinesToStr([ // $mod.$main
  18447. '']));
  18448. end;
  18449. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18450. begin
  18451. WithTypeInfo:=true;
  18452. StartProgram(false);
  18453. Add([
  18454. '{$modeswitch externalclass}',
  18455. 'type',
  18456. ' JSwiper = class external name ''Swiper''',
  18457. ' constructor New;',
  18458. ' end;',
  18459. ' TObject = class',
  18460. ' private',
  18461. ' FSwiper: JSwiper;',
  18462. ' published',
  18463. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18464. ' end;',
  18465. 'begin',
  18466. ' JSwiper.new;',
  18467. '']);
  18468. ConvertProgram;
  18469. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18470. LinesToStr([ // statements
  18471. 'this.$rtti.$ExtClass("JSwiper", {',
  18472. ' jsclass: "Swiper"',
  18473. '});',
  18474. 'rtl.createClass(this, "TObject", null, function () {',
  18475. ' this.$init = function () {',
  18476. ' this.FSwiper = null;',
  18477. ' };',
  18478. ' this.$final = function () {',
  18479. ' this.FSwiper = undefined;',
  18480. ' };',
  18481. ' var $r = this.$rtti;',
  18482. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18483. '});',
  18484. '']),
  18485. LinesToStr([ // $mod.$main
  18486. 'new Swiper();',
  18487. '']));
  18488. end;
  18489. procedure TTestModule.TestExternalClass_Property;
  18490. begin
  18491. StartProgram(false);
  18492. Add([
  18493. '{$modeswitch externalclass}',
  18494. 'type',
  18495. ' TExtA = class external name ''ExtA''',
  18496. ' function getYear: longint;',
  18497. ' procedure setYear(Value: longint);',
  18498. ' property Year: longint read getyear write setyear;',
  18499. ' end;',
  18500. ' TExtB = class (TExtA)',
  18501. ' procedure OtherSetYear(Value: longint);',
  18502. ' property year write othersetyear;',
  18503. ' end;',
  18504. 'procedure textb.othersetyear(value: longint);',
  18505. 'begin',
  18506. ' setYear(Value+4);',
  18507. 'end;',
  18508. 'var',
  18509. ' A: texta;',
  18510. ' B: textb;',
  18511. 'begin',
  18512. ' a.year:=a.year+1;',
  18513. ' b.year:=b.year+2;']);
  18514. ConvertProgram;
  18515. CheckSource('TestExternalClass_NonExternalOverride',
  18516. LinesToStr([ // statements
  18517. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18518. ' this.$init = function () {',
  18519. ' };',
  18520. ' this.$final = function () {',
  18521. ' };',
  18522. ' this.OtherSetYear = function (Value) {',
  18523. ' this.setYear(Value+4);',
  18524. ' };',
  18525. '});',
  18526. 'this.A = null;',
  18527. 'this.B = null;',
  18528. '']),
  18529. LinesToStr([ // $mod.$main
  18530. '$mod.A.setYear($mod.A.getYear()+1);',
  18531. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18532. '']));
  18533. end;
  18534. procedure TTestModule.TestExternalClass_PropertyDate;
  18535. begin
  18536. StartProgram(false);
  18537. Add([
  18538. '{$modeswitch externalclass}',
  18539. 'type',
  18540. ' TExtA = class external name ''ExtA''',
  18541. ' end;',
  18542. ' TExtB = class (TExtA)',
  18543. ' FDate: string;',
  18544. ' property Date: string read FDate write FDate;',
  18545. ' property ExtA: string read FDate write FDate;',
  18546. ' end;',
  18547. ' {$M+}',
  18548. ' TObject = class',
  18549. ' FDate: string;',
  18550. ' published',
  18551. ' property Date: string read FDate write FDate;',
  18552. ' property ExtA: string read FDate write FDate;',
  18553. ' end;',
  18554. 'var',
  18555. ' B: textb;',
  18556. ' o: TObject;',
  18557. 'begin',
  18558. ' b.date:=b.exta;',
  18559. ' o.date:=o.exta;']);
  18560. ConvertProgram;
  18561. CheckSource('TestExternalClass_PropertyDate',
  18562. LinesToStr([ // statements
  18563. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18564. ' this.$init = function () {',
  18565. ' this.FDate = "";',
  18566. ' };',
  18567. ' this.$final = function () {',
  18568. ' };',
  18569. '});',
  18570. 'rtl.createClass(this, "TObject", null, function () {',
  18571. ' this.$init = function () {',
  18572. ' this.FDate = "";',
  18573. ' };',
  18574. ' this.$final = function () {',
  18575. ' };',
  18576. ' var $r = this.$rtti;',
  18577. ' $r.addField("FDate", rtl.string);',
  18578. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18579. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18580. '});',
  18581. 'this.B = null;',
  18582. 'this.o = null;',
  18583. '']),
  18584. LinesToStr([ // $mod.$main
  18585. '$mod.B.FDate = $mod.B.FDate;',
  18586. '$mod.o.FDate = $mod.o.FDate;',
  18587. '']));
  18588. end;
  18589. procedure TTestModule.TestExternalClass_ClassProperty;
  18590. begin
  18591. StartProgram(false);
  18592. Add('{$modeswitch externalclass}');
  18593. Add('type');
  18594. Add(' TExtA = class external name ''ExtA''');
  18595. Add(' class function getYear: longint;');
  18596. Add(' class procedure setYear(Value: longint);');
  18597. Add(' class property Year: longint read getyear write setyear;');
  18598. Add(' end;');
  18599. Add(' TExtB = class (TExtA)');
  18600. Add(' class function GetCentury: longint;');
  18601. Add(' class procedure SetCentury(Value: longint);');
  18602. Add(' class property Century: longint read getcentury write setcentury;');
  18603. Add(' end;');
  18604. Add('class function textb.getcentury: longint;');
  18605. Add('begin');
  18606. Add('end;');
  18607. Add('class procedure textb.setcentury(value: longint);');
  18608. Add('begin');
  18609. Add(' setyear(value+11);');
  18610. Add(' texta.year:=texta.year+12;');
  18611. Add(' year:=year+13;');
  18612. Add(' textb.century:=textb.century+14;');
  18613. Add(' century:=century+15;');
  18614. Add('end;');
  18615. Add('var');
  18616. Add(' A: texta;');
  18617. Add(' B: textb;');
  18618. Add('begin');
  18619. Add(' texta.year:=texta.year+1;');
  18620. Add(' textb.year:=textb.year+2;');
  18621. Add(' TextA.year:=TextA.year+3;');
  18622. Add(' b.year:=b.year+4;');
  18623. Add(' textb.century:=textb.century+5;');
  18624. Add(' b.century:=b.century+6;');
  18625. ConvertProgram;
  18626. CheckSource('TestExternalClass_ClassProperty',
  18627. LinesToStr([ // statements
  18628. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18629. ' this.$init = function () {',
  18630. ' };',
  18631. ' this.$final = function () {',
  18632. ' };',
  18633. ' this.GetCentury = function () {',
  18634. ' var Result = 0;',
  18635. ' return Result;',
  18636. ' };',
  18637. ' this.SetCentury = function (Value) {',
  18638. ' this.setYear(Value + 11);',
  18639. ' ExtA.setYear(ExtA.getYear() + 12);',
  18640. ' this.setYear(this.getYear() + 13);',
  18641. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18642. ' this.SetCentury(this.GetCentury() + 15);',
  18643. ' };',
  18644. '});',
  18645. 'this.A = null;',
  18646. 'this.B = null;',
  18647. '']),
  18648. LinesToStr([ // $mod.$main
  18649. 'ExtA.setYear(ExtA.getYear() + 1);',
  18650. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18651. 'ExtA.setYear(ExtA.getYear() + 3);',
  18652. '$mod.B.setYear($mod.B.getYear() + 4);',
  18653. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18654. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18655. '']));
  18656. end;
  18657. procedure TTestModule.TestExternalClass_ClassOf;
  18658. begin
  18659. StartProgram(false);
  18660. Add('{$modeswitch externalclass}');
  18661. Add('type');
  18662. Add(' TExtA = class external name ''ExtA''');
  18663. Add(' procedure ProcA; virtual;');
  18664. Add(' procedure ProcB; virtual;');
  18665. Add(' end;');
  18666. Add(' TExtAClass = class of TExtA;');
  18667. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18668. Add(' end;');
  18669. Add(' TExtBClass = class of TExtB;');
  18670. Add(' TExtC = class (TExtB)');
  18671. Add(' procedure ProcA; override;');
  18672. Add(' end;');
  18673. Add(' TExtCClass = class of TExtC;');
  18674. Add('procedure TExtC.ProcA; begin end;');
  18675. Add('var');
  18676. Add(' A: texta; ClA: TExtAClass;');
  18677. Add(' B: textb; ClB: TExtBClass;');
  18678. Add(' C: textc; ClC: TExtCClass;');
  18679. Add('begin');
  18680. Add(' ClA:=texta;');
  18681. Add(' ClA:=textb;');
  18682. Add(' ClA:=textc;');
  18683. Add(' ClB:=textb;');
  18684. Add(' ClB:=textc;');
  18685. Add(' ClC:=textc;');
  18686. ConvertProgram;
  18687. CheckSource('TestExternalClass_ClassOf',
  18688. LinesToStr([ // statements
  18689. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18690. ' this.$init = function () {',
  18691. ' };',
  18692. ' this.$final = function () {',
  18693. ' };',
  18694. ' this.ProcA = function () {',
  18695. ' };',
  18696. '});',
  18697. 'this.A = null;',
  18698. 'this.ClA = null;',
  18699. 'this.B = null;',
  18700. 'this.ClB = null;',
  18701. 'this.C = null;',
  18702. 'this.ClC = null;',
  18703. '']),
  18704. LinesToStr([ // $mod.$main
  18705. '$mod.ClA = ExtA;',
  18706. '$mod.ClA = ExtB;',
  18707. '$mod.ClA = $mod.TExtC;',
  18708. '$mod.ClB = ExtB;',
  18709. '$mod.ClB = $mod.TExtC;',
  18710. '$mod.ClC = $mod.TExtC;',
  18711. '']));
  18712. end;
  18713. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18714. begin
  18715. AddModuleWithIntfImplSrc('unit2.pas',
  18716. LinesToStr([
  18717. '{$modeswitch externalclass}',
  18718. 'type',
  18719. ' TExtA = class external name ''ExtA''',
  18720. ' class var Id: longint;',
  18721. ' end;',
  18722. '']),
  18723. '');
  18724. StartUnit(true);
  18725. Add('interface');
  18726. Add('uses unit2;');
  18727. Add('implementation');
  18728. Add('begin');
  18729. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18730. ConvertUnit;
  18731. CheckSource('TestExternalClass_ClassOtherUnit',
  18732. LinesToStr([
  18733. '']),
  18734. LinesToStr([
  18735. 'ExtA.Id = ExtA.Id + 1;',
  18736. '']));
  18737. end;
  18738. procedure TTestModule.TestExternalClass_Is;
  18739. begin
  18740. StartProgram(false);
  18741. Add([
  18742. '{$modeswitch externalclass}',
  18743. 'type',
  18744. ' TExtA = class external name ''ExtA''',
  18745. ' end;',
  18746. ' TExtAClass = class of TExtA;',
  18747. ' TExtB = class external name ''ExtB'' (TExtA)',
  18748. ' end;',
  18749. ' TExtBClass = class of TExtB;',
  18750. ' TExtC = class (TExtB)',
  18751. ' end;',
  18752. ' TExtCClass = class of TExtC;',
  18753. 'var',
  18754. ' A: texta; ClA: TExtAClass;',
  18755. ' B: textb; ClB: TExtBClass;',
  18756. ' C: textc; ClC: TExtCClass;',
  18757. 'begin',
  18758. ' if a is textb then ;',
  18759. ' if a is textc then ;',
  18760. ' if b is textc then ;',
  18761. ' if cla is textb then ;',
  18762. ' if cla is textc then ;',
  18763. ' if clb is textc then ;',
  18764. ' try',
  18765. ' except',
  18766. ' on TExtA do ;',
  18767. ' on e: TExtB do ;',
  18768. ' end;',
  18769. '']);
  18770. ConvertProgram;
  18771. CheckSource('TestExternalClass_Is',
  18772. LinesToStr([ // statements
  18773. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18774. ' this.$init = function () {',
  18775. ' };',
  18776. ' this.$final = function () {',
  18777. ' };',
  18778. '});',
  18779. 'this.A = null;',
  18780. 'this.ClA = null;',
  18781. 'this.B = null;',
  18782. 'this.ClB = null;',
  18783. 'this.C = null;',
  18784. 'this.ClC = null;',
  18785. '']),
  18786. LinesToStr([ // $mod.$main
  18787. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18788. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18789. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18790. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18791. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18792. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18793. 'try {} catch ($e) {',
  18794. ' if (rtl.isExt($e,ExtA)) {}',
  18795. ' else if (rtl.isExt($e,ExtB)) {',
  18796. ' var e = $e;',
  18797. ' } else throw $e',
  18798. '};',
  18799. '']));
  18800. end;
  18801. procedure TTestModule.TestExternalClass_As;
  18802. begin
  18803. StartProgram(false);
  18804. Add('{$modeswitch externalclass}');
  18805. Add('type');
  18806. Add(' TExtA = class external name ''ExtA''');
  18807. Add(' end;');
  18808. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18809. Add(' end;');
  18810. Add(' TExtC = class (TExtB)');
  18811. Add(' end;');
  18812. Add('var');
  18813. Add(' A: texta;');
  18814. Add(' B: textb;');
  18815. Add(' C: textc;');
  18816. Add('begin');
  18817. Add(' b:=a as textb;');
  18818. Add(' c:=a as textc;');
  18819. Add(' c:=b as textc;');
  18820. ConvertProgram;
  18821. CheckSource('TestExternalClass_Is',
  18822. LinesToStr([ // statements
  18823. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18824. ' this.$init = function () {',
  18825. ' };',
  18826. ' this.$final = function () {',
  18827. ' };',
  18828. '});',
  18829. 'this.A = null;',
  18830. 'this.B = null;',
  18831. 'this.C = null;',
  18832. '']),
  18833. LinesToStr([ // $mod.$main
  18834. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18835. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18836. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18837. '']));
  18838. end;
  18839. procedure TTestModule.TestExternalClass_DestructorFail;
  18840. begin
  18841. StartProgram(false);
  18842. Add('{$modeswitch externalclass}');
  18843. Add('type');
  18844. Add(' TExtA = class external name ''ExtA''');
  18845. Add(' destructor Free;');
  18846. Add(' end;');
  18847. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18848. nPasElementNotSupported);
  18849. ConvertProgram;
  18850. end;
  18851. procedure TTestModule.TestExternalClass_New;
  18852. begin
  18853. StartProgram(false);
  18854. Add([
  18855. '{$modeswitch externalclass}',
  18856. 'type',
  18857. ' TExtA = class external name ''ExtA''',
  18858. ' constructor New;',
  18859. ' constructor New(i: longint; j: longint = 2);',
  18860. ' end;',
  18861. 'var',
  18862. ' A: texta;',
  18863. 'begin',
  18864. ' a:=texta.new;',
  18865. ' a:=texta(texta.new);',
  18866. ' a:=texta.new();',
  18867. ' a:=texta.new(1);',
  18868. ' with texta do begin',
  18869. ' a:=new;',
  18870. ' a:=new();',
  18871. ' a:=new(2);',
  18872. ' end;',
  18873. ' a:=test1.texta.new;',
  18874. ' a:=test1.texta.new();',
  18875. ' a:=test1.texta.new(3);',
  18876. '']);
  18877. ConvertProgram;
  18878. CheckSource('TestExternalClass_New',
  18879. LinesToStr([ // statements
  18880. 'this.A = null;',
  18881. '']),
  18882. LinesToStr([ // $mod.$main
  18883. '$mod.A = new ExtA();',
  18884. '$mod.A = new ExtA();',
  18885. '$mod.A = new ExtA();',
  18886. '$mod.A = new ExtA(1,2);',
  18887. '$mod.A = new ExtA();',
  18888. '$mod.A = new ExtA();',
  18889. '$mod.A = new ExtA(2,2);',
  18890. '$mod.A = new ExtA();',
  18891. '$mod.A = new ExtA();',
  18892. '$mod.A = new ExtA(3,2);',
  18893. '']));
  18894. end;
  18895. procedure TTestModule.TestExternalClass_ClassOf_New;
  18896. begin
  18897. StartProgram(false);
  18898. Add('{$modeswitch externalclass}');
  18899. Add('type');
  18900. Add(' TExtAClass = class of TExtA;');
  18901. Add(' TExtA = class external name ''ExtA''');
  18902. Add(' C: TExtAClass;');
  18903. Add(' constructor New;');
  18904. Add(' end;');
  18905. Add('var');
  18906. Add(' A: texta;');
  18907. Add(' C: textaclass;');
  18908. Add('begin');
  18909. Add(' a:=c.new;');
  18910. Add(' a:=c.new();');
  18911. Add(' with C do begin');
  18912. Add(' a:=new;');
  18913. Add(' a:=new();');
  18914. Add(' end;');
  18915. Add(' a:=test1.c.new;');
  18916. Add(' a:=test1.c.new();');
  18917. Add(' a:=A.c.new();');
  18918. ConvertProgram;
  18919. CheckSource('TestExternalClass_ClassOf_New',
  18920. LinesToStr([ // statements
  18921. 'this.A = null;',
  18922. 'this.C = null;',
  18923. '']),
  18924. LinesToStr([ // $mod.$main
  18925. '$mod.A = new $mod.C();',
  18926. '$mod.A = new $mod.C();',
  18927. 'var $with = $mod.C;',
  18928. '$mod.A = new $with();',
  18929. '$mod.A = new $with();',
  18930. '$mod.A = new $mod.C();',
  18931. '$mod.A = new $mod.C();',
  18932. '$mod.A = new $mod.A.C();',
  18933. '']));
  18934. end;
  18935. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18936. begin
  18937. StartProgram(false);
  18938. Add([
  18939. '{$modeswitch externalclass}',
  18940. 'type',
  18941. ' TExtAClass = class of TExtA;',
  18942. ' TExtA = class external name ''ExtA''',
  18943. ' constructor New;',
  18944. ' end;',
  18945. 'function GetCreator: TExtAClass;',
  18946. 'begin',
  18947. ' Result:=TExtA;',
  18948. 'end;',
  18949. 'var',
  18950. ' A: texta;',
  18951. 'begin',
  18952. ' a:=getcreator.new;',
  18953. ' a:=getcreator().new;',
  18954. ' a:=getcreator().new();',
  18955. ' a:=getcreator.new();',
  18956. ' with getcreator do begin',
  18957. ' a:=new;',
  18958. ' a:=new();',
  18959. ' end;']);
  18960. ConvertProgram;
  18961. CheckSource('TestExternalClass_FuncClassOf_New',
  18962. LinesToStr([ // statements
  18963. 'this.GetCreator = function () {',
  18964. ' var Result = null;',
  18965. ' Result = ExtA;',
  18966. ' return Result;',
  18967. '};',
  18968. 'this.A = null;',
  18969. '']),
  18970. LinesToStr([ // $mod.$main
  18971. '$mod.A = new ($mod.GetCreator())();',
  18972. '$mod.A = new ($mod.GetCreator())();',
  18973. '$mod.A = new ($mod.GetCreator())();',
  18974. '$mod.A = new ($mod.GetCreator())();',
  18975. 'var $with = $mod.GetCreator();',
  18976. '$mod.A = new $with();',
  18977. '$mod.A = new $with();',
  18978. '']));
  18979. end;
  18980. procedure TTestModule.TestExternalClass_New_PasClassFail;
  18981. begin
  18982. StartProgram(false);
  18983. Add([
  18984. '{$modeswitch externalclass}',
  18985. 'type',
  18986. ' TExtA = class external name ''ExtA''',
  18987. ' constructor New;',
  18988. ' end;',
  18989. ' TBird = class(TExtA)',
  18990. ' end;',
  18991. 'begin',
  18992. ' TBird.new;',
  18993. '']);
  18994. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18995. ConvertProgram;
  18996. end;
  18997. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  18998. begin
  18999. StartProgram(false);
  19000. Add([
  19001. '{$modeswitch externalclass}',
  19002. 'type',
  19003. ' TExtA = class external name ''ExtA''',
  19004. ' constructor New;',
  19005. ' end;',
  19006. ' TBird = class(TExtA)',
  19007. ' end;',
  19008. 'begin',
  19009. ' TBird.new();',
  19010. '']);
  19011. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19012. ConvertProgram;
  19013. end;
  19014. procedure TTestModule.TestExternalClass_NewExtName;
  19015. begin
  19016. StartProgram(false);
  19017. Add([
  19018. '{$modeswitch externalclass}',
  19019. 'type',
  19020. ' TExtA = class external name ''ExtA''',
  19021. ' constructor New; external name ''Other'';',
  19022. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19023. ' end;',
  19024. 'var',
  19025. ' A: texta;',
  19026. 'begin',
  19027. ' a:=texta.new;',
  19028. ' a:=texta(texta.new);',
  19029. ' a:=texta.new();',
  19030. ' a:=texta.new(1);',
  19031. ' with texta do begin',
  19032. ' a:=new;',
  19033. ' a:=new();',
  19034. ' a:=new(2);',
  19035. ' end;',
  19036. ' a:=test1.texta.new;',
  19037. ' a:=test1.texta.new();',
  19038. ' a:=test1.texta.new(3);',
  19039. '']);
  19040. ConvertProgram;
  19041. CheckSource('TestExternalClass_NewExtName',
  19042. LinesToStr([ // statements
  19043. 'this.A = null;',
  19044. '']),
  19045. LinesToStr([ // $mod.$main
  19046. '$mod.A = new Other();',
  19047. '$mod.A = new Other();',
  19048. '$mod.A = new Other();',
  19049. '$mod.A = new A.B(1,2);',
  19050. '$mod.A = new Other();',
  19051. '$mod.A = new Other();',
  19052. '$mod.A = new A.B(2,2);',
  19053. '$mod.A = new Other();',
  19054. '$mod.A = new Other();',
  19055. '$mod.A = new A.B(3,2);',
  19056. '']));
  19057. end;
  19058. procedure TTestModule.TestExternalClass_Constructor;
  19059. begin
  19060. StartProgram(false);
  19061. Add([
  19062. '{$modeswitch externalclass}',
  19063. 'type',
  19064. ' TExtA = class external name ''ExtA''',
  19065. ' public type',
  19066. ' TExtB = class external name ''ExtB''',
  19067. ' public type',
  19068. ' TExtC = class external name ''ExtC''',
  19069. ' constructor New;',
  19070. ' constructor New(i: word);',
  19071. ' end;',
  19072. ' end;',
  19073. ' constructor Create;',
  19074. ' constructor Create(i: longint; j: longint = 2);',
  19075. ' end;',
  19076. 'var',
  19077. ' A: texta;',
  19078. ' C: texta.textb.textc;',
  19079. 'begin',
  19080. ' a:=texta.create;',
  19081. ' a:=texta(texta.create);',
  19082. ' a:=texta.create();',
  19083. ' a:=texta.create(1);',
  19084. ' with texta do begin',
  19085. ' a:=create;',
  19086. ' a:=create();',
  19087. ' a:=create(2);',
  19088. ' end;',
  19089. ' a:=test1.texta.create;',
  19090. ' a:=test1.texta.create();',
  19091. ' a:=test1.texta.create(3);',
  19092. ' c:=texta.textb.textc.new;',
  19093. ' c:=texta.textb.textc.new();',
  19094. ' c:=texta.textb.textc.new(4);',
  19095. '']);
  19096. ConvertProgram;
  19097. CheckSource('TestExternalClass_Constructor',
  19098. LinesToStr([ // statements
  19099. 'this.A = null;',
  19100. 'this.C = null;',
  19101. '']),
  19102. LinesToStr([ // $mod.$main
  19103. '$mod.A = new ExtA.Create();',
  19104. '$mod.A = new ExtA.Create();',
  19105. '$mod.A = new ExtA.Create();',
  19106. '$mod.A = new ExtA.Create(1,2);',
  19107. '$mod.A = new ExtA.Create();',
  19108. '$mod.A = new ExtA.Create();',
  19109. '$mod.A = new ExtA.Create(2,2);',
  19110. '$mod.A = new ExtA.Create();',
  19111. '$mod.A = new ExtA.Create();',
  19112. '$mod.A = new ExtA.Create(3,2);',
  19113. '$mod.C = new ExtA.ExtB.ExtC();',
  19114. '$mod.C = new ExtA.ExtB.ExtC();',
  19115. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19116. '']));
  19117. end;
  19118. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19119. begin
  19120. StartProgram(false);
  19121. Add([
  19122. '{$modeswitch externalclass}',
  19123. 'type',
  19124. ' TExtA = class external name ''ExtA''',
  19125. ' constructor Create; external name ''{}'';',
  19126. ' end;',
  19127. 'var',
  19128. ' A: texta;',
  19129. 'begin',
  19130. ' a:=texta.create;',
  19131. ' a:=texta(texta.create);',
  19132. ' a:=texta.create();',
  19133. ' with texta do begin',
  19134. ' a:=create;',
  19135. ' a:=create();',
  19136. ' end;',
  19137. ' a:=test1.texta.create;',
  19138. ' a:=test1.texta.create();',
  19139. '']);
  19140. ConvertProgram;
  19141. CheckSource('TestExternalClass_ConstructorBrackets',
  19142. LinesToStr([ // statements
  19143. 'this.A = null;',
  19144. '']),
  19145. LinesToStr([ // $mod.$main
  19146. '$mod.A = {};',
  19147. '$mod.A = {};',
  19148. '$mod.A = {};',
  19149. '$mod.A = {};',
  19150. '$mod.A = {};',
  19151. '$mod.A = {};',
  19152. '$mod.A = {};',
  19153. '']));
  19154. end;
  19155. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19156. begin
  19157. StartProgram(false);
  19158. Add('{$modeswitch externalclass}');
  19159. Add('type');
  19160. Add(' TExtA = class external name ''ExtA''');
  19161. Add(' constructor New;');
  19162. Add(' end;');
  19163. Add('function DoIt: longint;');
  19164. Add('const ExtA: longint = 3;');
  19165. Add('begin');
  19166. Add(' Result:=ExtA;');
  19167. Add('end;');
  19168. Add('var');
  19169. Add(' A: texta;');
  19170. Add('begin');
  19171. Add(' a:=texta.new;');
  19172. ConvertProgram;
  19173. CheckSource('TestExternalClass_LocalConstSameName',
  19174. LinesToStr([ // statements
  19175. 'var ExtA$1 = 3;',
  19176. 'this.DoIt = function () {',
  19177. ' var Result = 0;',
  19178. ' Result = ExtA$1;',
  19179. ' return Result;',
  19180. '};',
  19181. 'this.A = null;',
  19182. '']),
  19183. LinesToStr([ // $mod.$main
  19184. '$mod.A = new ExtA();',
  19185. '']));
  19186. end;
  19187. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19188. begin
  19189. StartProgram(false);
  19190. Add('{$modeswitch externalclass}');
  19191. Add('type');
  19192. Add(' TExtA = class external name ''ExtA''');
  19193. Add(' procedure DoIt;');
  19194. Add(' end;');
  19195. Add(' TMyA = class(TExtA)');
  19196. Add(' procedure DoIt;');
  19197. Add(' end;');
  19198. Add('procedure TMyA.DoIt; begin end;');
  19199. Add('begin');
  19200. ConvertProgram;
  19201. CheckSource('TestExternalClass_ReintroduceOverload',
  19202. LinesToStr([ // statements
  19203. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19204. ' this.$init = function () {',
  19205. ' };',
  19206. ' this.$final = function () {',
  19207. ' };',
  19208. ' this.DoIt$1 = function () {',
  19209. ' };',
  19210. '});',
  19211. '']),
  19212. LinesToStr([ // $mod.$main
  19213. '']));
  19214. end;
  19215. procedure TTestModule.TestExternalClass_Inherited;
  19216. begin
  19217. StartProgram(false);
  19218. Add('{$modeswitch externalclass}');
  19219. Add('type');
  19220. Add(' TExtA = class external name ''ExtA''');
  19221. Add(' procedure DoIt(i: longint = 1); virtual;');
  19222. Add(' procedure DoSome(j: longint = 2);');
  19223. Add(' end;');
  19224. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19225. Add(' end;');
  19226. Add(' TMyC = class(TExtB)');
  19227. Add(' procedure DoIt(i: longint = 1); override;');
  19228. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19229. Add(' end;');
  19230. Add('procedure TMyC.DoIt(i: longint);');
  19231. Add('begin');
  19232. Add(' inherited;');
  19233. Add(' inherited DoIt;');
  19234. Add(' inherited DoIt();');
  19235. Add(' inherited DoIt(3);');
  19236. Add(' inherited DoSome;');
  19237. Add(' inherited DoSome();');
  19238. Add(' inherited DoSome(4);');
  19239. Add('end;');
  19240. Add('procedure TMyC.DoSome(j: longint);');
  19241. Add('begin');
  19242. Add(' inherited;');
  19243. Add('end;');
  19244. Add('begin');
  19245. ConvertProgram;
  19246. CheckSource('TestExternalClass_ReintroduceOverload',
  19247. LinesToStr([ // statements
  19248. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19249. ' this.$init = function () {',
  19250. ' };',
  19251. ' this.$final = function () {',
  19252. ' };',
  19253. ' this.DoIt = function (i) {',
  19254. ' ExtB.DoIt.apply(this, arguments);',
  19255. ' ExtB.DoIt.call(this, 1);',
  19256. ' ExtB.DoIt.call(this, 1);',
  19257. ' ExtB.DoIt.call(this, 3);',
  19258. ' ExtB.DoSome.call(this, 2);',
  19259. ' ExtB.DoSome.call(this, 2);',
  19260. ' ExtB.DoSome.call(this, 4);',
  19261. ' };',
  19262. ' this.DoSome$1 = function (j) {',
  19263. ' ExtB.DoSome.apply(this, arguments);',
  19264. ' };',
  19265. '});',
  19266. '']),
  19267. LinesToStr([ // $mod.$main
  19268. '']));
  19269. end;
  19270. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19271. begin
  19272. StartProgram(false);
  19273. Add('{$modeswitch externalclass}');
  19274. Add('type');
  19275. Add(' TObject = class');
  19276. Add(' end;');
  19277. Add(' TExtA = class external name ''ExtA''(TObject)');
  19278. Add(' end;');
  19279. Add('begin');
  19280. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19281. ConvertProgram;
  19282. end;
  19283. procedure TTestModule.TestExternalClass_NewInstance;
  19284. begin
  19285. StartProgram(false);
  19286. Add('{$modeswitch externalclass}');
  19287. Add('type');
  19288. Add(' TExtA = class external name ''ExtA''');
  19289. Add(' end;');
  19290. Add(' TMyB = class(TExtA)');
  19291. Add(' protected');
  19292. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19293. Add(' end;');
  19294. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19295. Add('begin end;');
  19296. Add('begin');
  19297. ConvertProgram;
  19298. CheckSource('TestExternalClass_NewInstance',
  19299. LinesToStr([ // statements
  19300. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19301. ' this.$init = function () {',
  19302. ' };',
  19303. ' this.$final = function () {',
  19304. ' };',
  19305. ' this.NewInstance = function (fnname, paramarray) {',
  19306. ' var Result = null;',
  19307. ' return Result;',
  19308. ' };',
  19309. '});',
  19310. '']),
  19311. LinesToStr([ // $mod.$main
  19312. '']));
  19313. end;
  19314. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19315. begin
  19316. StartProgram(false);
  19317. Add('{$modeswitch externalclass}');
  19318. Add('type');
  19319. Add(' TExtA = class external name ''ExtA''');
  19320. Add(' end;');
  19321. Add(' TMyB = class(TExtA)');
  19322. Add(' protected');
  19323. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19324. Add(' end;');
  19325. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19326. Add('begin end;');
  19327. Add('begin');
  19328. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19329. ConvertProgram;
  19330. end;
  19331. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19332. begin
  19333. StartProgram(false);
  19334. Add('{$modeswitch externalclass}');
  19335. Add('type');
  19336. Add(' TExtA = class external name ''ExtA''');
  19337. Add(' end;');
  19338. Add(' TMyB = class(TExtA)');
  19339. Add(' protected');
  19340. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19341. Add(' end;');
  19342. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19343. Add('begin end;');
  19344. Add('begin');
  19345. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19346. nIncompatibleTypeArgNo);
  19347. ConvertProgram;
  19348. end;
  19349. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19350. begin
  19351. StartProgram(false);
  19352. Add('{$modeswitch externalclass}');
  19353. Add('type');
  19354. Add(' TExtA = class external name ''ExtA''');
  19355. Add(' end;');
  19356. Add(' TMyB = class(TExtA)');
  19357. Add(' protected');
  19358. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19359. Add(' end;');
  19360. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19361. Add('begin end;');
  19362. Add('begin');
  19363. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19364. nIncompatibleTypeArgNo);
  19365. ConvertProgram;
  19366. end;
  19367. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19368. begin
  19369. StartProgram(false);
  19370. Add([
  19371. '{$modeswitch externalclass}',
  19372. 'type',
  19373. ' TJSFunction = class external name ''Function''',
  19374. ' end;',
  19375. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19376. ' constructor New(w: word);',
  19377. ' end;',
  19378. ' TBird = class (TExtA)',
  19379. ' public',
  19380. ' Size: word;',
  19381. ' class var Legs: word;',
  19382. ' constructor Create(a: word);',
  19383. ' end;',
  19384. ' TEagle = class (TBird)',
  19385. ' public',
  19386. ' constructor Create(b: word); reintroduce;',
  19387. ' end;',
  19388. 'constructor TBird.Create(a: word);',
  19389. 'begin',
  19390. ' inherited;', // silently ignored
  19391. ' inherited New(a);', // this.$func(a)
  19392. 'end;',
  19393. 'constructor TEagle.Create(b: word);',
  19394. 'begin',
  19395. ' inherited Create(b);',
  19396. 'end;',
  19397. 'var',
  19398. ' Bird: TBird;',
  19399. ' Eagle: TEagle;',
  19400. 'begin',
  19401. ' Bird:=TBird.Create(3);',
  19402. ' Eagle:=TEagle.Create(4);',
  19403. ' Bird.Size:=Bird.Size+5;',
  19404. ' Bird.Legs:=Bird.Legs+6;',
  19405. ' Eagle.Size:=Eagle.Size+5;',
  19406. ' Eagle.Legs:=Eagle.Legs+6;',
  19407. '']);
  19408. ConvertProgram;
  19409. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19410. LinesToStr([ // statements
  19411. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19412. ' this.Legs = 0;',
  19413. ' this.$init = function () {',
  19414. ' this.Size = 0;',
  19415. ' };',
  19416. ' this.$final = function () {',
  19417. ' };',
  19418. ' this.Create = function (a) {',
  19419. ' this.$ancestorfunc(a);',
  19420. ' return this;',
  19421. ' };',
  19422. '});',
  19423. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19424. ' this.Create$1 = function (b) {',
  19425. ' $mod.TBird.Create.call(this, b);',
  19426. ' return this;',
  19427. ' };',
  19428. '});',
  19429. 'this.Bird = null;',
  19430. 'this.Eagle = null;',
  19431. '']),
  19432. LinesToStr([ // $mod.$main
  19433. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19434. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19435. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19436. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19437. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19438. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19439. '']));
  19440. end;
  19441. procedure TTestModule.TestExternalClass_PascalProperty;
  19442. begin
  19443. StartProgram(false);
  19444. Add('{$modeswitch externalclass}');
  19445. Add('type');
  19446. Add(' TJSElement = class;');
  19447. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19448. Add(' TJSElement = class external name ''ExtA''');
  19449. Add(' end;');
  19450. Add(' TControl = class(TJSElement)');
  19451. Add(' private');
  19452. Add(' FOnClick: TJSNotifyEvent;');
  19453. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19454. Add(' procedure Click(Sender: TJSElement);');
  19455. Add(' end;');
  19456. Add('procedure TControl.Click(Sender: TJSElement);');
  19457. Add('begin');
  19458. Add(' OnClick(Self);');
  19459. Add('end;');
  19460. Add('var');
  19461. Add(' Ctrl: TControl;');
  19462. Add('begin');
  19463. Add(' Ctrl.OnClick:[email protected];');
  19464. Add(' Ctrl.OnClick(Ctrl);');
  19465. ConvertProgram;
  19466. CheckSource('TestExternalClass_PascalProperty',
  19467. LinesToStr([ // statements
  19468. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19469. ' this.$init = function () {',
  19470. ' this.FOnClick = null;',
  19471. ' };',
  19472. ' this.$final = function () {',
  19473. ' this.FOnClick = undefined;',
  19474. ' };',
  19475. ' this.Click = function (Sender) {',
  19476. ' this.FOnClick(this);',
  19477. ' };',
  19478. '});',
  19479. 'this.Ctrl = null;',
  19480. '']),
  19481. LinesToStr([ // $mod.$main
  19482. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19483. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19484. '']));
  19485. end;
  19486. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19487. begin
  19488. StartProgram(false);
  19489. Add([
  19490. '{$modeswitch externalclass}',
  19491. 'type',
  19492. ' IUnknown = interface end;',
  19493. ' TObject = class',
  19494. ' end;',
  19495. ' TChild = class',
  19496. ' end;',
  19497. ' TExtRootA = class external name ''ExtRootA''',
  19498. ' end;',
  19499. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19500. ' end;',
  19501. ' TExtRootB = class external name ''ExtRootB''',
  19502. ' end;',
  19503. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19504. ' end;',
  19505. ' TExtString = class external name ''String''',
  19506. ' function charAt(aIndex : NativeInt) : string;',
  19507. ' end;',
  19508. 'var',
  19509. ' Obj: TObject;',
  19510. ' Child: TChild;',
  19511. ' RootA: TExtRootA;',
  19512. ' ChildA: TExtChildA;',
  19513. ' RootB: TExtRootB;',
  19514. ' ChildB: TExtChildB;',
  19515. ' i: IUnknown;',
  19516. ' s: string;',
  19517. ' v: jsvalue;',
  19518. 'begin',
  19519. ' obj:=tobject(roota);',
  19520. ' obj:=tobject(childa);',
  19521. ' child:=tchild(tobject(roota));',
  19522. ' roota:=textroota(obj);',
  19523. ' roota:=textroota(child);',
  19524. ' roota:=textroota(rootb);',
  19525. ' roota:=textroota(childb);',
  19526. ' childa:=textchilda(textroota(obj));',
  19527. ' roota:=TExtRootA(i);',
  19528. ' s:=TExtString(s).charAt(7);',
  19529. ' s:=TExtString(v).charAt(8);',
  19530. '']);
  19531. ConvertProgram;
  19532. CheckSource('TestExternalClass_TypeCastToRootClass',
  19533. LinesToStr([ // statements
  19534. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19535. 'rtl.createClass(this, "TObject", null, function () {',
  19536. ' this.$init = function () {',
  19537. ' };',
  19538. ' this.$final = function () {',
  19539. ' };',
  19540. '});',
  19541. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19542. '});',
  19543. 'this.Obj = null;',
  19544. 'this.Child = null;',
  19545. 'this.RootA = null;',
  19546. 'this.ChildA = null;',
  19547. 'this.RootB = null;',
  19548. 'this.ChildB = null;',
  19549. 'this.i = null;',
  19550. 'this.s = "";',
  19551. 'this.v = undefined;',
  19552. '']),
  19553. LinesToStr([ // $mod.$main
  19554. '$mod.Obj = $mod.RootA;',
  19555. '$mod.Obj = $mod.ChildA;',
  19556. '$mod.Child = $mod.RootA;',
  19557. '$mod.RootA = $mod.Obj;',
  19558. '$mod.RootA = $mod.Child;',
  19559. '$mod.RootA = $mod.RootB;',
  19560. '$mod.RootA = $mod.ChildB;',
  19561. '$mod.ChildA = $mod.Obj;',
  19562. '$mod.RootA = $mod.i;',
  19563. '$mod.s = $mod.s.charAt(7);',
  19564. '$mod.s = $mod.v.charAt(8);',
  19565. '']));
  19566. end;
  19567. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19568. begin
  19569. StartProgram(false);
  19570. Add([
  19571. '{$modeswitch externalclass}',
  19572. 'type',
  19573. ' IUnknown = interface end;',
  19574. ' IBird = interface(IUnknown) end;',
  19575. ' TClass = class of TObject;',
  19576. ' TObject = class',
  19577. ' end;',
  19578. ' TChild = class',
  19579. ' end;',
  19580. ' TJSObject = class external name ''Object''',
  19581. ' end;',
  19582. ' TRec = record end;',
  19583. 'var',
  19584. ' Obj: TObject;',
  19585. ' Child: TChild;',
  19586. ' i: IUnknown;',
  19587. ' Bird: IBird;',
  19588. ' j: TJSObject;',
  19589. ' r: TRec;',
  19590. ' c: TClass;',
  19591. 'begin',
  19592. ' j:=tjsobject(IUnknown);',
  19593. ' j:=tjsobject(IBird);',
  19594. ' j:=tjsobject(TObject);',
  19595. ' j:=tjsobject(TChild);',
  19596. ' j:=tjsobject(TRec);',
  19597. ' j:=tjsobject(Obj);',
  19598. ' j:=tjsobject(Child);',
  19599. ' j:=tjsobject(i);',
  19600. ' j:=tjsobject(Bird);',
  19601. ' j:=tjsobject(r);',
  19602. ' j:=tjsobject(c);',
  19603. '']);
  19604. ConvertProgram;
  19605. CheckSource('TestExternalClass_TypeCastToJSObject',
  19606. LinesToStr([ // statements
  19607. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19608. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19609. 'rtl.createClass(this, "TObject", null, function () {',
  19610. ' this.$init = function () {',
  19611. ' };',
  19612. ' this.$final = function () {',
  19613. ' };',
  19614. '});',
  19615. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19616. '});',
  19617. 'rtl.recNewT(this, "TRec", function () {',
  19618. ' this.$eq = function (b) {',
  19619. ' return true;',
  19620. ' };',
  19621. ' this.$assign = function (s) {',
  19622. ' return this;',
  19623. ' };',
  19624. '});',
  19625. 'this.Obj = null;',
  19626. 'this.Child = null;',
  19627. 'this.i = null;',
  19628. 'this.Bird = null;',
  19629. 'this.j = null;',
  19630. 'this.r = this.TRec.$new();',
  19631. 'this.c = null;',
  19632. '']),
  19633. LinesToStr([ // $mod.$main
  19634. '$mod.j = $mod.IUnknown;',
  19635. '$mod.j = $mod.IBird;',
  19636. '$mod.j = $mod.TObject;',
  19637. '$mod.j = $mod.TChild;',
  19638. '$mod.j = $mod.TRec;',
  19639. '$mod.j = $mod.Obj;',
  19640. '$mod.j = $mod.Child;',
  19641. '$mod.j = $mod.i;',
  19642. '$mod.j = $mod.Bird;',
  19643. '$mod.j = $mod.r;',
  19644. '$mod.j = $mod.c;',
  19645. '']));
  19646. end;
  19647. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19648. begin
  19649. StartProgram(false);
  19650. Add('{$modeswitch externalclass}');
  19651. Add('type');
  19652. Add(' TJSString = class external name ''String''');
  19653. Add(' class function fromCharCode() : string; varargs;');
  19654. Add(' function anchor(const aName : string) : string;');
  19655. Add(' end;');
  19656. Add('var');
  19657. Add(' s: string;');
  19658. Add('begin');
  19659. Add(' s:=TJSString.fromCharCode(65,66);');
  19660. Add(' s:=TJSString(s).anchor(s);');
  19661. Add(' s:=TJSString(''foo'').anchor(s);');
  19662. ConvertProgram;
  19663. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19664. LinesToStr([ // statements
  19665. 'this.s = "";',
  19666. '']),
  19667. LinesToStr([ // $mod.$main
  19668. '$mod.s = String.fromCharCode(65, 66);',
  19669. '$mod.s = $mod.s.anchor($mod.s);',
  19670. '$mod.s = "foo".anchor($mod.s);',
  19671. '']));
  19672. end;
  19673. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19674. begin
  19675. StartProgram(false);
  19676. Add([
  19677. '{$modeswitch externalclass}',
  19678. 'type',
  19679. ' TJSObject = class external name ''Object'' end;',
  19680. ' TJSFunction = class external name ''Function''',
  19681. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19682. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19683. ' end;',
  19684. ' TObject = class',
  19685. ' procedure DoIt(i: longint);',
  19686. ' end;',
  19687. ' TFuncInt = function(o: TObject): longint;',
  19688. 'function GetIt(o: TObject): longint;',
  19689. ' procedure Sub; begin end;',
  19690. 'var',
  19691. ' f: TJSFunction;',
  19692. ' fi: TFuncInt;',
  19693. 'begin',
  19694. ' fi:=TFuncInt(f);',
  19695. ' f:=TJSFunction(fi);',
  19696. ' f:=TJSFunction(@GetIt);',
  19697. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19698. ' f:=TJSFunction(@Sub);',
  19699. ' f:=TJSFunction(@o.doit);',
  19700. ' f:=TJSFunction(fi).bind(nil,4)',
  19701. 'end;',
  19702. 'procedure TObject.DoIt(i: longint);',
  19703. ' procedure Sub; begin end;',
  19704. 'var f: TJSFunction;',
  19705. 'begin',
  19706. ' f:=TJSFunction(@DoIt);',
  19707. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19708. ' f:=TJSFunction(@Sub);',
  19709. ' f:=TJSFunction(@GetIt);',
  19710. 'end;',
  19711. 'begin']);
  19712. ConvertProgram;
  19713. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19714. LinesToStr([ // statements
  19715. 'rtl.createClass(this, "TObject", null, function () {',
  19716. ' this.$init = function () {',
  19717. ' };',
  19718. ' this.$final = function () {',
  19719. ' };',
  19720. ' this.DoIt = function (i) {',
  19721. ' var $Self = this;',
  19722. ' function Sub() {',
  19723. ' };',
  19724. ' var f = null;',
  19725. ' f = this.DoIt;',
  19726. ' f = this.DoIt.bind(null, 13);',
  19727. ' f = Sub;',
  19728. ' f = $mod.GetIt;',
  19729. ' };',
  19730. '});',
  19731. 'this.GetIt = function (o) {',
  19732. ' var Result = 0;',
  19733. ' function Sub() {',
  19734. ' };',
  19735. ' var f = null;',
  19736. ' var fi = null;',
  19737. ' fi = f;',
  19738. ' f = fi;',
  19739. ' f = $mod.GetIt;',
  19740. ' f = $mod.GetIt.bind(null, 3);',
  19741. ' f = Sub;',
  19742. ' f = $mod.TObject.DoIt;',
  19743. ' f = fi.bind(null, 4);',
  19744. ' return Result;',
  19745. '};',
  19746. '']),
  19747. LinesToStr([ // $mod.$main
  19748. '']));
  19749. end;
  19750. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19751. begin
  19752. StartProgram(false);
  19753. Add([
  19754. '{$mode delphi}',
  19755. '{$modeswitch externalclass}',
  19756. 'type',
  19757. ' TJSObject = class external name ''Object'' end;',
  19758. ' TJSWindow = class external name ''Window''(TJSObject)',
  19759. ' procedure Open;',
  19760. ' end;',
  19761. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19762. ' procedure Execute;',
  19763. ' end;',
  19764. 'procedure Fly;',
  19765. 'var',
  19766. ' w: TJSWindow;',
  19767. ' e: TJSEventTarget;',
  19768. 'begin',
  19769. ' w:=TJSWindow(e);',
  19770. ' e:=TJSEventTarget(w);',
  19771. 'end;',
  19772. 'begin']);
  19773. ConvertProgram;
  19774. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19775. LinesToStr([ // statements
  19776. 'this.Fly = function () {',
  19777. ' var w = null;',
  19778. ' var e = null;',
  19779. ' w = e;',
  19780. ' e = w;',
  19781. '};',
  19782. '']),
  19783. LinesToStr([ // $mod.$main
  19784. '']));
  19785. end;
  19786. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19787. begin
  19788. StartProgram(false);
  19789. Add('{$modeswitch externalclass}');
  19790. Add('type');
  19791. Add(' TJSString = class external name ''String''');
  19792. Add(' class function fromCharCode() : string; varargs;');
  19793. Add(' end;');
  19794. Add('var');
  19795. Add(' s: string;');
  19796. Add(' sObj: TJSString;');
  19797. Add('begin');
  19798. Add(' s:=sObj.fromCharCode(65,66);');
  19799. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19800. nExternalClassInstanceCannotAccessStaticX);
  19801. ConvertProgram;
  19802. end;
  19803. procedure TTestModule.TestExternalClass_BracketAccessor;
  19804. begin
  19805. StartProgram(false);
  19806. Add([
  19807. '{$modeswitch externalclass}',
  19808. 'type',
  19809. ' TJSArray = class external name ''Array2''',
  19810. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19811. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19812. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19813. ' end;',
  19814. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19815. 'begin end;',
  19816. 'var',
  19817. ' Arr: tjsarray;',
  19818. ' s: string;',
  19819. ' i: longint;',
  19820. ' v: jsvalue;',
  19821. 'begin',
  19822. ' v:=arr[0];',
  19823. ' v:=arr.items[1];',
  19824. ' arr[2]:=s;',
  19825. ' arr.items[3]:=s;',
  19826. ' arr[4]:=i;',
  19827. ' arr[5]:=arr[6];',
  19828. ' arr.items[7]:=arr.items[8];',
  19829. ' with arr do items[9]:=items[10];',
  19830. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19831. ' with arr do begin',
  19832. ' v:=GetItems(14);',
  19833. ' setitems(15,16);',
  19834. ' end;',
  19835. ' v:=test1.arr.items[17];',
  19836. ' test1.arr.items[18]:=v;',
  19837. '']);
  19838. ConvertProgram;
  19839. CheckSource('TestExternalClass_BracketAccessor',
  19840. LinesToStr([ // statements
  19841. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19842. '};',
  19843. 'this.Arr = null;',
  19844. 'this.s = "";',
  19845. 'this.i = 0;',
  19846. 'this.v = undefined;',
  19847. '']),
  19848. LinesToStr([ // $mod.$main
  19849. '$mod.v = $mod.Arr[0];',
  19850. '$mod.v = $mod.Arr[1];',
  19851. '$mod.Arr[2] = $mod.s;',
  19852. '$mod.Arr[3] = $mod.s;',
  19853. '$mod.Arr[4] = $mod.i;',
  19854. '$mod.Arr[5] = $mod.Arr[6];',
  19855. '$mod.Arr[7] = $mod.Arr[8];',
  19856. 'var $with = $mod.Arr;',
  19857. '$with[9] = $with[10];',
  19858. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19859. ' a: 9,',
  19860. ' p: $mod.Arr,',
  19861. ' get: function () {',
  19862. ' return this.p[this.a];',
  19863. ' },',
  19864. ' set: function (v) {',
  19865. ' this.p[this.a] = v;',
  19866. ' }',
  19867. '}, {',
  19868. ' a: 10,',
  19869. ' p: $mod.Arr,',
  19870. ' get: function () {',
  19871. ' return this.p[this.a];',
  19872. ' },',
  19873. ' set: function (v) {',
  19874. ' this.p[this.a] = v;',
  19875. ' }',
  19876. '});',
  19877. 'var $with1 = $mod.Arr;',
  19878. '$mod.v = $with1[14];',
  19879. '$with1[15] = 16;',
  19880. '$mod.v = $mod.Arr[17];',
  19881. '$mod.Arr[18] = $mod.v;',
  19882. '']));
  19883. end;
  19884. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19885. begin
  19886. StartProgram(false);
  19887. Add([
  19888. '{$modeswitch externalclass}',
  19889. 'type',
  19890. ' TJSArray = class external name ''Array2''',
  19891. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19892. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19893. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19894. ' end;',
  19895. ' TMyArr = class(TJSArray)',
  19896. ' procedure DoIt;',
  19897. ' end;',
  19898. 'procedure tmyarr.DoIt;',
  19899. 'begin',
  19900. ' Items[1]:=Items[2];',
  19901. ' SetItems(3,getItems(4));',
  19902. 'end;',
  19903. 'var',
  19904. ' Arr: tmyarr;',
  19905. ' s: string;',
  19906. ' i: longint;',
  19907. ' v: jsvalue;',
  19908. 'begin',
  19909. ' v:=arr[0];',
  19910. ' v:=arr.items[1];',
  19911. ' arr[2]:=s;',
  19912. ' arr.items[3]:=s;',
  19913. ' arr[4]:=i;',
  19914. ' arr[5]:=arr[6];',
  19915. ' arr.items[7]:=arr.items[8];',
  19916. ' with arr do items[9]:=items[10];',
  19917. ' with arr do begin',
  19918. ' v:=GetItems(14);',
  19919. ' setitems(15,16);',
  19920. ' end;',
  19921. '']);
  19922. ConvertProgram;
  19923. CheckSource('TestExternalClass_BracketAccessor_Call',
  19924. LinesToStr([ // statements
  19925. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19926. ' this.$init = function () {',
  19927. ' };',
  19928. ' this.$final = function () {',
  19929. ' };',
  19930. ' this.DoIt = function () {',
  19931. ' this[1] = this[2];',
  19932. ' this[3] = this[4];',
  19933. ' };',
  19934. '});',
  19935. 'this.Arr = null;',
  19936. 'this.s = "";',
  19937. 'this.i = 0;',
  19938. 'this.v = undefined;',
  19939. '']),
  19940. LinesToStr([ // $mod.$main
  19941. '$mod.v = $mod.Arr[0];',
  19942. '$mod.v = $mod.Arr[1];',
  19943. '$mod.Arr[2] = $mod.s;',
  19944. '$mod.Arr[3] = $mod.s;',
  19945. '$mod.Arr[4] = $mod.i;',
  19946. '$mod.Arr[5] = $mod.Arr[6];',
  19947. '$mod.Arr[7] = $mod.Arr[8];',
  19948. 'var $with = $mod.Arr;',
  19949. '$with[9] = $with[10];',
  19950. 'var $with1 = $mod.Arr;',
  19951. '$mod.v = $with1[14];',
  19952. '$with1[15] = 16;',
  19953. '']));
  19954. end;
  19955. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  19956. begin
  19957. StartProgram(false);
  19958. Add('{$modeswitch externalclass}');
  19959. Add('type');
  19960. Add(' TJSArray = class external name ''Array2''');
  19961. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  19962. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  19963. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  19964. Add(' end;');
  19965. Add('begin');
  19966. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  19967. nBracketAccessorOfExternalClassMustHaveOneParameter);
  19968. ConvertProgram;
  19969. end;
  19970. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  19971. begin
  19972. StartProgram(false);
  19973. Add('{$modeswitch externalclass}');
  19974. Add('type');
  19975. Add(' TJSArray = class external name ''Array2''');
  19976. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19977. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  19978. Add(' end;');
  19979. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  19980. Add('begin end;');
  19981. Add('var');
  19982. Add(' Arr: tjsarray;');
  19983. Add(' v: jsvalue;');
  19984. Add('begin');
  19985. Add(' v:=arr[0];');
  19986. Add(' v:=arr.items[1];');
  19987. Add(' with arr do v:=items[2];');
  19988. Add(' doit(arr[3],arr[4]);');
  19989. ConvertProgram;
  19990. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  19991. LinesToStr([ // statements
  19992. 'this.DoIt = function (vI, vJ) {',
  19993. '};',
  19994. 'this.Arr = null;',
  19995. 'this.v = undefined;',
  19996. '']),
  19997. LinesToStr([ // $mod.$main
  19998. '$mod.v = $mod.Arr[0];',
  19999. '$mod.v = $mod.Arr[1];',
  20000. 'var $with = $mod.Arr;',
  20001. '$mod.v = $with[2];',
  20002. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20003. '']));
  20004. end;
  20005. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20006. begin
  20007. StartProgram(false);
  20008. Add('{$modeswitch externalclass}');
  20009. Add('type');
  20010. Add(' TJSArray = class external name ''Array2''');
  20011. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20012. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20013. Add(' end;');
  20014. Add('var');
  20015. Add(' Arr: tjsarray;');
  20016. Add(' s: string;');
  20017. Add(' i: longint;');
  20018. Add(' v: jsvalue;');
  20019. Add('begin');
  20020. Add(' arr[2]:=s;');
  20021. Add(' arr.items[3]:=s;');
  20022. Add(' arr[4]:=i;');
  20023. Add(' with arr do items[5]:=i;');
  20024. ConvertProgram;
  20025. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20026. LinesToStr([ // statements
  20027. 'this.Arr = null;',
  20028. 'this.s = "";',
  20029. 'this.i = 0;',
  20030. 'this.v = undefined;',
  20031. '']),
  20032. LinesToStr([ // $mod.$main
  20033. '$mod.Arr[2] = $mod.s;',
  20034. '$mod.Arr[3] = $mod.s;',
  20035. '$mod.Arr[4] = $mod.i;',
  20036. 'var $with = $mod.Arr;',
  20037. '$with[5] = $mod.i;',
  20038. '']));
  20039. end;
  20040. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20041. begin
  20042. StartProgram(false);
  20043. Add('{$modeswitch externalclass}');
  20044. Add('type');
  20045. Add(' TJSArray = class external name ''Array2''');
  20046. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20047. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20048. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20049. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20050. Add(' end;');
  20051. Add('var');
  20052. Add(' Arr: tjsarray;');
  20053. Add(' s: string;');
  20054. Add(' i: longint;');
  20055. Add(' v: jsvalue;');
  20056. Add('begin');
  20057. Add(' arr[2]:=s;');
  20058. Add(' arr.items[3]:=s;');
  20059. Add(' arr.numbers[4]:=i;');
  20060. Add(' with arr do items[5]:=i;');
  20061. Add(' with arr do numbers[6]:=i;');
  20062. ConvertProgram;
  20063. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20064. LinesToStr([ // statements
  20065. 'this.Arr = null;',
  20066. 'this.s = "";',
  20067. 'this.i = 0;',
  20068. 'this.v = undefined;',
  20069. '']),
  20070. LinesToStr([ // $mod.$main
  20071. '$mod.Arr[2] = $mod.s;',
  20072. '$mod.Arr[3] = $mod.s;',
  20073. '$mod.Arr[4] = $mod.i;',
  20074. 'var $with = $mod.Arr;',
  20075. '$with[5] = $mod.i;',
  20076. 'var $with1 = $mod.Arr;',
  20077. '$with1[6] = $mod.i;',
  20078. '']));
  20079. end;
  20080. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20081. begin
  20082. StartProgram(false);
  20083. Add('{$modeswitch externalclass}');
  20084. Add('type');
  20085. Add(' TJSArray = class external name ''Array2''');
  20086. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20087. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20088. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20089. Add(' end;');
  20090. Add('var');
  20091. Add(' Arr: tjsarray;');
  20092. Add(' i: longint;');
  20093. Add(' IntArr: array of longint;');
  20094. Add(' v: jsvalue;');
  20095. Add('begin');
  20096. Add(' v:=arr.items[i];');
  20097. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20098. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20099. ConvertProgram;
  20100. CheckSource('TestExternalClass_BracketAccessor_Index',
  20101. LinesToStr([ // statements
  20102. 'this.Arr = null;',
  20103. 'this.i = 0;',
  20104. 'this.IntArr = [];',
  20105. 'this.v = undefined;',
  20106. '']),
  20107. LinesToStr([ // $mod.$main
  20108. '$mod.v = $mod.Arr[$mod.i];',
  20109. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20110. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20111. '']));
  20112. end;
  20113. procedure TTestModule.TestExternalClass_ForInJSObject;
  20114. begin
  20115. StartProgram(false);
  20116. Add([
  20117. '{$modeswitch externalclass}',
  20118. 'type',
  20119. ' TJSObject = class external name ''Object''',
  20120. ' end;',
  20121. 'var',
  20122. ' o: TJSObject;',
  20123. ' key: string;',
  20124. 'begin',
  20125. ' for key in o do',
  20126. ' if key=''abc'' then ;',
  20127. '']);
  20128. ConvertProgram;
  20129. CheckSource('TestExternalClass_ForInJSObject',
  20130. LinesToStr([ // statements
  20131. 'this.o = null;',
  20132. 'this.key = "";',
  20133. '']),
  20134. LinesToStr([ // $mod.$main
  20135. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20136. '']));
  20137. end;
  20138. procedure TTestModule.TestExternalClass_ForInJSArray;
  20139. begin
  20140. StartProgram(false);
  20141. Add([
  20142. '{$modeswitch externalclass}',
  20143. 'type',
  20144. ' TJSInt8Array = class external name ''Int8Array''',
  20145. ' private',
  20146. ' flength: NativeInt external name ''length'';',
  20147. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20148. ' public',
  20149. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20150. ' property Length: NativeInt read flength;',
  20151. ' end;',
  20152. 'var',
  20153. ' a: TJSInt8Array;',
  20154. ' value: shortint;',
  20155. 'begin',
  20156. ' for value in a do',
  20157. ' if value=3 then ;',
  20158. '']);
  20159. ConvertProgram;
  20160. CheckSource('TestExternalClass_ForInJSArray',
  20161. LinesToStr([ // statements
  20162. 'this.a = null;',
  20163. 'this.value = 0;',
  20164. '']),
  20165. LinesToStr([ // $mod.$main
  20166. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20167. ' $mod.value = $in[$l];',
  20168. ' if ($mod.value === 3) ;',
  20169. '};',
  20170. '']));
  20171. end;
  20172. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20173. begin
  20174. AddModuleWithIntfImplSrc('unit2.pas',
  20175. LinesToStr([
  20176. '{$modeswitch externalclass}',
  20177. 'type',
  20178. ' TJSBufferSource = class external name ''BufferSource''',
  20179. ' end;',
  20180. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20181. '']),
  20182. '');
  20183. AddModuleWithIntfImplSrc('unit3.pas',
  20184. LinesToStr([
  20185. '{$modeswitch externalclass}',
  20186. 'type',
  20187. ' TJSBufferSource = class external name ''BufferSource''',
  20188. ' end;',
  20189. '']),
  20190. '');
  20191. StartUnit(true);
  20192. Add([
  20193. 'interface',
  20194. 'uses unit2, unit3;',
  20195. 'procedure DoSome(s: TJSBufferSource);',
  20196. 'implementation',
  20197. 'procedure DoSome(s: TJSBufferSource);',
  20198. 'begin',
  20199. ' DoIt(s);',
  20200. 'end;',
  20201. '']);
  20202. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20203. nIncompatibleTypeArgNo);
  20204. ConvertUnit;
  20205. end;
  20206. procedure TTestModule.TestExternalClass_NestedConstructor;
  20207. begin
  20208. StartProgram(false);
  20209. Add([
  20210. '{$modeswitch externalclass}',
  20211. 'type',
  20212. ' TJSObject = class external name ''Object''',
  20213. ' type TBird = class external name ''Bird''',
  20214. ' type TWing = class external name ''Wing''',
  20215. ' constructor New;',
  20216. ' constructor Create(w: word = 3);',
  20217. ' end;',
  20218. ' end;',
  20219. ' end;',
  20220. 'var',
  20221. ' w: TJSObject.TBird.TWing;',
  20222. 'begin',
  20223. ' w:=tjsobject.tbird.twing.new;',
  20224. ' w:=tjsobject.tbird.twing.new();',
  20225. ' w:=tjsobject.tbird.twing.create;',
  20226. ' w:=tjsobject.tbird.twing.create(4);',
  20227. ' with tjsobject do begin',
  20228. ' w:=tbird.twing.new;',
  20229. ' w:=tbird.twing.new();',
  20230. ' w:=tbird.twing.create;',
  20231. ' w:=tbird.twing.create(11);',
  20232. ' end;',
  20233. ' with tjsobject.tbird do begin',
  20234. ' w:=twing.new;',
  20235. ' w:=twing.new();',
  20236. ' w:=twing.create;',
  20237. ' w:=twing.create(21);',
  20238. ' end;',
  20239. ' with tjsobject.tbird.twing do begin',
  20240. ' w:=new;',
  20241. ' w:=new();',
  20242. ' w:=create;',
  20243. ' w:=create(31);',
  20244. ' end;',
  20245. '']);
  20246. ConvertProgram;
  20247. CheckSource('TestExternalClass_NestedConstructor',
  20248. LinesToStr([ // statements
  20249. 'this.w = null;',
  20250. '']),
  20251. LinesToStr([ // $mod.$main
  20252. '$mod.w = new Object.Bird.Wing();',
  20253. '$mod.w = new Object.Bird.Wing();',
  20254. '$mod.w = new Object.Bird.Wing.Create();',
  20255. '$mod.w = new Object.Bird.Wing.Create(4);',
  20256. '$mod.w = new Object.Bird.Wing();',
  20257. '$mod.w = new Object.Bird.Wing();',
  20258. '$mod.w = new Object.Bird.Wing.Create();',
  20259. '$mod.w = new Object.Bird.Wing.Create(11);',
  20260. 'var $with = Object.Bird;',
  20261. '$mod.w = new Object.Bird.Wing();',
  20262. '$mod.w = new Object.Bird.Wing();',
  20263. '$mod.w = new Object.Bird.Wing.Create();',
  20264. '$mod.w = new Object.Bird.Wing.Create(21);',
  20265. 'var $with1 = Object.Bird.Wing;',
  20266. '$mod.w = new $with1();',
  20267. '$mod.w = new $with1();',
  20268. '$mod.w = new Object.Bird.Wing.Create();',
  20269. '$mod.w = new Object.Bird.Wing.Create(31);',
  20270. '']));
  20271. end;
  20272. procedure TTestModule.TestClassInterface_Corba;
  20273. begin
  20274. StartProgram(false);
  20275. Add([
  20276. '{$interfaces corba}',
  20277. 'type',
  20278. ' IUnknown = interface;',
  20279. ' IUnknown = interface',
  20280. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20281. ' end;',
  20282. ' IInterface = IUnknown;',
  20283. ' IBird = interface(IInterface)',
  20284. ' function GetSize: longint;',
  20285. ' procedure SetSize(i: longint);',
  20286. ' property Size: longint read GetSize write SetSize;',
  20287. ' procedure DoIt(i: longint);',
  20288. ' end;',
  20289. ' TObject = class',
  20290. ' end;',
  20291. ' TBird = class(TObject,IBird)',
  20292. ' function GetSize: longint; virtual; abstract;',
  20293. ' procedure SetSize(i: longint); virtual; abstract;',
  20294. ' procedure DoIt(i: longint); virtual; abstract;',
  20295. ' end;',
  20296. 'var',
  20297. ' BirdIntf: IBird;',
  20298. 'begin',
  20299. ' BirdIntf.Size:=BirdIntf.Size;',
  20300. '']);
  20301. ConvertProgram;
  20302. CheckSource('TestClassInterface_Corba',
  20303. LinesToStr([ // statements
  20304. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20305. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20306. 'rtl.createClass(this, "TObject", null, function () {',
  20307. ' this.$init = function () {',
  20308. ' };',
  20309. ' this.$final = function () {',
  20310. ' };',
  20311. '});',
  20312. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20313. ' rtl.addIntf(this, $mod.IBird);',
  20314. '});',
  20315. 'this.BirdIntf = null;',
  20316. '']),
  20317. LinesToStr([ // $mod.$main
  20318. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20319. '']));
  20320. end;
  20321. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20322. begin
  20323. StartProgram(false);
  20324. Add([
  20325. '{$interfaces corba}',
  20326. 'type',
  20327. ' IUnknown = interface',
  20328. ' procedure DoIt; external name ''foo'';',
  20329. ' end;',
  20330. 'begin']);
  20331. SetExpectedParserError(
  20332. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20333. nParserNoFieldsAllowed);
  20334. ConvertProgram;
  20335. end;
  20336. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20337. begin
  20338. StartProgram(false);
  20339. Add([
  20340. '{$interfaces corba}',
  20341. 'type',
  20342. ' integer = longint;',
  20343. ' IUnknown = interface',
  20344. ' procedure DoIt(i: integer);',
  20345. ' procedure DoIt(s: string);',
  20346. ' end;',
  20347. ' IBird = interface(IUnknown)',
  20348. ' procedure DoIt(b: boolean); overload;',
  20349. ' end;',
  20350. ' TObject = class',
  20351. ' end;',
  20352. ' TBird = class(TObject,IBird)',
  20353. ' procedure DoIt(o: TObject);',
  20354. ' procedure DoIt(s: string);',
  20355. ' procedure DoIt(i: integer);',
  20356. ' procedure DoIt(b: boolean);',
  20357. ' end;',
  20358. 'procedure TBird.DoIt(o: TObject); begin end;',
  20359. 'procedure TBird.DoIt(s: string); begin end;',
  20360. 'procedure TBird.DoIt(i: integer); begin end;',
  20361. 'procedure TBird.DoIt(b: boolean); begin end;',
  20362. 'var',
  20363. ' BirdIntf: IBird;',
  20364. 'begin',
  20365. ' BirdIntf.DoIt(3);',
  20366. ' BirdIntf.DoIt(''abc'');',
  20367. ' BirdIntf.DoIt(true);',
  20368. '']);
  20369. ConvertProgram;
  20370. CheckSource('TestClassInterface_Overloads',
  20371. LinesToStr([ // statements
  20372. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20373. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20374. 'rtl.createClass(this, "TObject", null, function () {',
  20375. ' this.$init = function () {',
  20376. ' };',
  20377. ' this.$final = function () {',
  20378. ' };',
  20379. '});',
  20380. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20381. ' this.DoIt = function (o) {',
  20382. ' };',
  20383. ' this.DoIt$1 = function (s) {',
  20384. ' };',
  20385. ' this.DoIt$2 = function (i) {',
  20386. ' };',
  20387. ' this.DoIt$3 = function (b) {',
  20388. ' };',
  20389. ' rtl.addIntf(this, $mod.IBird, {',
  20390. ' DoIt$2: "DoIt$3",',
  20391. ' DoIt: "DoIt$2"',
  20392. ' });',
  20393. '});',
  20394. 'this.BirdIntf = null;',
  20395. '']),
  20396. LinesToStr([ // $mod.$main
  20397. '$mod.BirdIntf.DoIt(3);',
  20398. '$mod.BirdIntf.DoIt$1("abc");',
  20399. '$mod.BirdIntf.DoIt$2(true);',
  20400. '']));
  20401. end;
  20402. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20403. begin
  20404. StartProgram(false);
  20405. Add([
  20406. '{$interfaces corba}',
  20407. 'type',
  20408. ' IBird = interface',
  20409. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20410. ' end;',
  20411. ' IDog = interface',
  20412. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20413. ' end;',
  20414. ' TObject = class(IBird,IDog)',
  20415. ' end;',
  20416. 'begin']);
  20417. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20418. nDuplicateGUIDXInYZ);
  20419. ConvertProgram;
  20420. end;
  20421. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20422. begin
  20423. StartProgram(false);
  20424. Add([
  20425. '{$interfaces corba}',
  20426. 'type',
  20427. ' IAnimal = interface',
  20428. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20429. ' end;',
  20430. ' IBird = interface(IAnimal)',
  20431. ' end;',
  20432. ' IHawk = interface(IBird)',
  20433. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20434. ' end;',
  20435. 'begin']);
  20436. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20437. nDuplicateGUIDXInYZ);
  20438. ConvertProgram;
  20439. end;
  20440. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20441. begin
  20442. StartProgram(false);
  20443. Add([
  20444. '{$interfaces corba}',
  20445. 'type',
  20446. ' integer = longint;',
  20447. ' IUnknown = interface',
  20448. ' procedure DoIt(i: integer);',
  20449. ' end;',
  20450. ' IBird = interface',
  20451. ' procedure Fly(i: integer);',
  20452. ' end;',
  20453. ' TObject = class(IUnknown)',
  20454. ' procedure DoIt(i: integer);',
  20455. ' end;',
  20456. ' TBird = class(IBird)',
  20457. ' procedure Fly(i: integer);',
  20458. ' end;',
  20459. 'procedure TObject.DoIt(i: integer); begin end;',
  20460. 'procedure TBird.Fly(i: integer); begin end;',
  20461. 'begin',
  20462. '']);
  20463. ConvertProgram;
  20464. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20465. LinesToStr([ // statements
  20466. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20467. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20468. 'rtl.createClass(this, "TObject", null, function () {',
  20469. ' this.$init = function () {',
  20470. ' };',
  20471. ' this.$final = function () {',
  20472. ' };',
  20473. ' this.DoIt = function (i) {',
  20474. ' };',
  20475. ' rtl.addIntf(this, $mod.IUnknown);',
  20476. '});',
  20477. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20478. ' this.Fly = function (i) {',
  20479. ' };',
  20480. ' rtl.addIntf(this, $mod.IBird);',
  20481. ' rtl.addIntf(this, $mod.IUnknown);',
  20482. '});',
  20483. '']),
  20484. LinesToStr([ // $mod.$main
  20485. '']));
  20486. end;
  20487. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20488. begin
  20489. StartProgram(false);
  20490. Add([
  20491. '{$interfaces corba}',
  20492. 'type',
  20493. ' integer = longint;',
  20494. ' IBird = interface',
  20495. ' procedure DoIt(i: integer);',
  20496. ' end;',
  20497. ' TObject = class',
  20498. ' procedure DoIt(i: integer);',
  20499. ' end;',
  20500. ' TBird = class(IBird)',
  20501. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20502. ' end;',
  20503. 'procedure TObject.DoIt(i: integer); begin end;',
  20504. 'procedure TBird.DoIt(i: integer); begin end;',
  20505. 'begin',
  20506. '']);
  20507. ConvertProgram;
  20508. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20509. LinesToStr([ // statements
  20510. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20511. 'rtl.createClass(this, "TObject", null, function () {',
  20512. ' this.$init = function () {',
  20513. ' };',
  20514. ' this.$final = function () {',
  20515. ' };',
  20516. ' this.DoIt = function (i) {',
  20517. ' };',
  20518. '});',
  20519. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20520. ' this.DoIt$1 = function (i) {',
  20521. ' };',
  20522. ' rtl.addIntf(this, $mod.IBird, {',
  20523. ' DoIt: "DoIt$1"',
  20524. ' });',
  20525. '});',
  20526. '']),
  20527. LinesToStr([ // $mod.$main
  20528. '']));
  20529. end;
  20530. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20531. begin
  20532. StartProgram(false);
  20533. Add([
  20534. '{$interfaces corba}',
  20535. 'type',
  20536. ' IUnknown = interface',
  20537. ' procedure Walk(i: longint);',
  20538. ' end;',
  20539. ' IBird = interface(IUnknown)',
  20540. ' procedure Walk(b: boolean); overload;',
  20541. ' procedure Fly(s: string);',
  20542. ' end;',
  20543. ' TObject = class',
  20544. ' end;',
  20545. ' TBird = class(TObject,IBird)',
  20546. ' procedure IBird.Fly = Move;',
  20547. ' procedure IBird.Walk = Hop;',
  20548. ' procedure Hop(i: longint);',
  20549. ' procedure Move(s: string);',
  20550. ' procedure Hop(b: boolean);',
  20551. ' end;',
  20552. 'procedure TBird.Move(s: string); begin end;',
  20553. 'procedure TBird.Hop(i: longint); begin end;',
  20554. 'procedure TBird.Hop(b: boolean); begin end;',
  20555. 'var',
  20556. ' BirdIntf: IBird;',
  20557. 'begin',
  20558. ' BirdIntf.Walk(3);',
  20559. ' BirdIntf.Walk(true);',
  20560. ' BirdIntf.Fly(''abc'');',
  20561. '']);
  20562. ConvertProgram;
  20563. CheckSource('TestClassInterface_Corba_MethodResolution',
  20564. LinesToStr([ // statements
  20565. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20566. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20567. 'rtl.createClass(this, "TObject", null, function () {',
  20568. ' this.$init = function () {',
  20569. ' };',
  20570. ' this.$final = function () {',
  20571. ' };',
  20572. '});',
  20573. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20574. ' this.Hop = function (i) {',
  20575. ' };',
  20576. ' this.Move = function (s) {',
  20577. ' };',
  20578. ' this.Hop$1 = function (b) {',
  20579. ' };',
  20580. ' rtl.addIntf(this, $mod.IBird, {',
  20581. ' Walk$1: "Hop$1",',
  20582. ' Fly: "Move",',
  20583. ' Walk: "Hop"',
  20584. ' });',
  20585. '});',
  20586. 'this.BirdIntf = null;',
  20587. '']),
  20588. LinesToStr([ // $mod.$main
  20589. '$mod.BirdIntf.Walk(3);',
  20590. '$mod.BirdIntf.Walk$1(true);',
  20591. '$mod.BirdIntf.Fly("abc");',
  20592. '']));
  20593. end;
  20594. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20595. begin
  20596. StartProgram(false);
  20597. Add([
  20598. '{$interfaces com}',
  20599. 'type',
  20600. ' IUnknown = interface',
  20601. ' function _AddRef: longint;',
  20602. ' procedure Walk;',
  20603. ' end;',
  20604. ' IBird = interface end;',
  20605. ' IDog = interface end;',
  20606. ' TObject = class(IBird,IDog)',
  20607. ' function _AddRef: longint; virtual; abstract;',
  20608. ' procedure Walk; virtual; abstract;',
  20609. ' end;',
  20610. ' TBird = class(IUnknown)',
  20611. ' end;',
  20612. 'begin',
  20613. '']);
  20614. ConvertProgram;
  20615. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20616. LinesToStr([ // statements
  20617. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20618. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20619. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20620. 'rtl.createClass(this, "TObject", null, function () {',
  20621. ' this.$init = function () {',
  20622. ' };',
  20623. ' this.$final = function () {',
  20624. ' };',
  20625. ' rtl.addIntf(this, $mod.IBird);',
  20626. ' rtl.addIntf(this, $mod.IDog);',
  20627. '});',
  20628. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20629. ' rtl.addIntf(this, $mod.IUnknown);',
  20630. ' rtl.addIntf(this, $mod.IBird);',
  20631. ' rtl.addIntf(this, $mod.IDog);',
  20632. '});',
  20633. '']),
  20634. LinesToStr([ // $mod.$main
  20635. '']));
  20636. end;
  20637. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20638. begin
  20639. StartProgram(false);
  20640. Add([
  20641. '{$interfaces corba}',
  20642. 'type',
  20643. ' IUnknown = interface',
  20644. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20645. ' procedure Go;',
  20646. ' end;',
  20647. ' TObject = class(IUnknown)',
  20648. ' procedure Go; virtual; abstract;',
  20649. ' end;',
  20650. ' TBird = class',
  20651. ' procedure Go; override;',
  20652. ' end;',
  20653. ' TCat = class(TObject)',
  20654. ' procedure Go; override;',
  20655. ' end;',
  20656. ' TDog = class(TObject, IUnknown)',
  20657. ' procedure Go; override;',
  20658. ' end;',
  20659. 'procedure TBird.Go; begin end;',
  20660. 'procedure TCat.Go; begin end;',
  20661. 'procedure TDog.Go; begin end;',
  20662. 'begin',
  20663. '']);
  20664. ConvertProgram;
  20665. CheckSource('TestClassInterface_Corba_MethodOverride',
  20666. LinesToStr([ // statements
  20667. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20668. 'rtl.createClass(this, "TObject", null, function () {',
  20669. ' this.$init = function () {',
  20670. ' };',
  20671. ' this.$final = function () {',
  20672. ' };',
  20673. ' rtl.addIntf(this, $mod.IUnknown);',
  20674. '});',
  20675. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20676. ' this.Go = function () {',
  20677. ' };',
  20678. ' rtl.addIntf(this, $mod.IUnknown);',
  20679. '});',
  20680. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20681. ' this.Go = function () {',
  20682. ' };',
  20683. ' rtl.addIntf(this, $mod.IUnknown);',
  20684. '});',
  20685. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20686. ' this.Go = function () {',
  20687. ' };',
  20688. ' rtl.addIntf(this, $mod.IUnknown);',
  20689. '});',
  20690. '']),
  20691. LinesToStr([ // $mod.$main
  20692. '']));
  20693. end;
  20694. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20695. begin
  20696. StartProgram(false);
  20697. Add([
  20698. '{$interfaces corba}',
  20699. 'type',
  20700. ' IUnknown = interface',
  20701. ' end;',
  20702. ' IBird = interface(IUnknown)',
  20703. ' procedure Fly(s: string);',
  20704. ' end;',
  20705. ' IEagle = interface(IBird)',
  20706. ' end;',
  20707. ' IDove = interface(IBird)',
  20708. ' end;',
  20709. ' ISwallow = interface(IBird)',
  20710. ' end;',
  20711. ' TObject = class',
  20712. ' end;',
  20713. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20714. ' procedure Fly(s: string); virtual; abstract;',
  20715. ' end;',
  20716. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20717. ' FBirdIntf: IBird;',
  20718. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20719. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20720. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20721. ' FDoveObj: TBird;',
  20722. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20723. ' function GetSwallowObj: TBird; virtual; abstract;',
  20724. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20725. ' end;',
  20726. 'begin',
  20727. '']);
  20728. ConvertProgram;
  20729. CheckSource('TestClassInterface_Corba_Delegation',
  20730. LinesToStr([ // statements
  20731. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20732. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20733. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20734. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20735. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20736. 'rtl.createClass(this, "TObject", null, function () {',
  20737. ' this.$init = function () {',
  20738. ' };',
  20739. ' this.$final = function () {',
  20740. ' };',
  20741. '});',
  20742. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20743. ' rtl.addIntf(this, $mod.IBird);',
  20744. ' rtl.addIntf(this, $mod.IEagle);',
  20745. ' rtl.addIntf(this, $mod.IDove);',
  20746. ' rtl.addIntf(this, $mod.ISwallow);',
  20747. '});',
  20748. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20749. ' this.$init = function () {',
  20750. ' $mod.TObject.$init.call(this);',
  20751. ' this.FBirdIntf = null;',
  20752. ' this.FDoveObj = null;',
  20753. ' };',
  20754. ' this.$final = function () {',
  20755. ' this.FBirdIntf = undefined;',
  20756. ' this.FDoveObj = undefined;',
  20757. ' $mod.TObject.$final.call(this);',
  20758. ' };',
  20759. ' this.$intfmaps = {',
  20760. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20761. ' return this.FBirdIntf;',
  20762. ' },',
  20763. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20764. ' return this.GetEagleIntf();',
  20765. ' },',
  20766. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20767. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20768. ' },',
  20769. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20770. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20771. ' }',
  20772. ' };',
  20773. '});',
  20774. '']),
  20775. LinesToStr([ // $mod.$main
  20776. '']));
  20777. end;
  20778. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20779. begin
  20780. StartProgram(false);
  20781. Add([
  20782. '{$interfaces corba}',
  20783. 'type',
  20784. ' IUnknown = interface',
  20785. ' end;',
  20786. ' IBird = interface(IUnknown)',
  20787. ' procedure Fly(s: string);',
  20788. ' end;',
  20789. ' IEagle = interface(IBird)',
  20790. ' end;',
  20791. ' IDove = interface(IBird)',
  20792. ' end;',
  20793. ' ISwallow = interface(IBird)',
  20794. ' end;',
  20795. ' TObject = class',
  20796. ' end;',
  20797. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20798. ' procedure Fly(s: string); virtual; abstract;',
  20799. ' end;',
  20800. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20801. ' private',
  20802. ' class var FBirdIntf: IBird;',
  20803. ' class var FDoveObj: TBird;',
  20804. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20805. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20806. ' protected',
  20807. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20808. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20809. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20810. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20811. ' end;',
  20812. 'begin',
  20813. '']);
  20814. ConvertProgram;
  20815. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20816. LinesToStr([ // statements
  20817. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20818. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20819. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20820. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20821. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20822. 'rtl.createClass(this, "TObject", null, function () {',
  20823. ' this.$init = function () {',
  20824. ' };',
  20825. ' this.$final = function () {',
  20826. ' };',
  20827. '});',
  20828. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20829. ' rtl.addIntf(this, $mod.IBird);',
  20830. ' rtl.addIntf(this, $mod.IEagle);',
  20831. ' rtl.addIntf(this, $mod.IDove);',
  20832. ' rtl.addIntf(this, $mod.ISwallow);',
  20833. '});',
  20834. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20835. ' this.FBirdIntf = null;',
  20836. ' this.FDoveObj = null;',
  20837. ' this.$intfmaps = {',
  20838. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20839. ' return this.FBirdIntf;',
  20840. ' },',
  20841. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20842. ' return this.GetEagleIntf();',
  20843. ' },',
  20844. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20845. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20846. ' },',
  20847. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20848. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20849. ' }',
  20850. ' };',
  20851. '});',
  20852. '']),
  20853. LinesToStr([ // $mod.$main
  20854. '']));
  20855. end;
  20856. procedure TTestModule.TestClassInterface_Corba_Operators;
  20857. begin
  20858. StartProgram(false);
  20859. Add([
  20860. '{$interfaces corba}',
  20861. 'type',
  20862. ' IUnknown = interface',
  20863. ' end;',
  20864. ' IBird = interface(IUnknown)',
  20865. ' function GetItems(Index: longint): longint;',
  20866. ' procedure SetItems(Index: longint; Value: longint);',
  20867. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20868. ' end;',
  20869. ' TObject = class',
  20870. ' end;',
  20871. ' TBird = class(TObject,IBird)',
  20872. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20873. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20874. ' end;',
  20875. 'var',
  20876. ' IntfVar: IBird = nil;',
  20877. ' IntfVar2: IBird;',
  20878. ' ObjVar: TBird;',
  20879. ' v: JSValue;',
  20880. 'begin',
  20881. ' IntfVar:=nil;',
  20882. ' IntfVar[3]:=IntfVar[4];',
  20883. ' if Assigned(IntfVar) then ;',
  20884. ' IntfVar:=IntfVar2;',
  20885. ' IntfVar:=ObjVar;',
  20886. ' if IntfVar=IntfVar2 then ;',
  20887. ' if IntfVar<>IntfVar2 then ;',
  20888. ' if IntfVar is IBird then ;',
  20889. ' if IntfVar is TBird then ;',
  20890. ' if ObjVar is IBird then ;',
  20891. ' IntfVar:=IntfVar2 as IBird;',
  20892. ' ObjVar:=IntfVar2 as TBird;',
  20893. ' IntfVar:=ObjVar as IBird;',
  20894. ' IntfVar:=IBird(IntfVar2);',
  20895. ' ObjVar:=TBird(IntfVar);',
  20896. ' IntfVar:=IBird(ObjVar);',
  20897. ' v:=IntfVar;',
  20898. ' IntfVar:=IBird(v);',
  20899. ' if v is IBird then ;',
  20900. ' v:=JSValue(IntfVar);',
  20901. ' v:=IBird;',
  20902. '']);
  20903. ConvertProgram;
  20904. CheckSource('TestClassInterface_Corba_Operators',
  20905. LinesToStr([ // statements
  20906. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20907. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20908. 'rtl.createClass(this, "TObject", null, function () {',
  20909. ' this.$init = function () {',
  20910. ' };',
  20911. ' this.$final = function () {',
  20912. ' };',
  20913. '});',
  20914. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20915. ' rtl.addIntf(this, $mod.IBird);',
  20916. '});',
  20917. 'this.IntfVar = null;',
  20918. 'this.IntfVar2 = null;',
  20919. 'this.ObjVar = null;',
  20920. 'this.v = undefined;',
  20921. '']),
  20922. LinesToStr([ // $mod.$main
  20923. '$mod.IntfVar = null;',
  20924. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20925. 'if ($mod.IntfVar != null) ;',
  20926. '$mod.IntfVar = $mod.IntfVar2;',
  20927. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20928. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20929. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20930. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20931. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20932. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20933. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20934. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20935. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20936. '$mod.IntfVar = $mod.IntfVar2;',
  20937. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20938. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20939. '$mod.v = $mod.IntfVar;',
  20940. '$mod.IntfVar = rtl.getObject($mod.v);',
  20941. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20942. '$mod.v = $mod.IntfVar;',
  20943. '$mod.v = $mod.IBird;',
  20944. '']));
  20945. end;
  20946. procedure TTestModule.TestClassInterface_Corba_Args;
  20947. begin
  20948. StartProgram(false);
  20949. Add([
  20950. '{$interfaces corba}',
  20951. 'type',
  20952. ' IUnknown = interface',
  20953. ' end;',
  20954. ' IBird = interface(IUnknown)',
  20955. ' end;',
  20956. ' TObject = class',
  20957. ' end;',
  20958. ' TBird = class(TObject,IBird)',
  20959. ' end;',
  20960. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  20961. 'begin',
  20962. ' DoIt(i,i,i);',
  20963. 'end;',
  20964. 'procedure Change(var i: IBird; out j: IBird);',
  20965. 'begin',
  20966. ' DoIt(i,i,i);',
  20967. ' Change(i,i);',
  20968. 'end;',
  20969. 'var',
  20970. ' i: IBird;',
  20971. ' o: TBird;',
  20972. 'begin',
  20973. ' DoIt(i,i,i);',
  20974. ' Change(i,i);',
  20975. ' DoIt(o,o,o);',
  20976. '']);
  20977. ConvertProgram;
  20978. CheckSource('TestClassInterface_Corba_Args',
  20979. LinesToStr([ // statements
  20980. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20981. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  20982. 'rtl.createClass(this, "TObject", null, function () {',
  20983. ' this.$init = function () {',
  20984. ' };',
  20985. ' this.$final = function () {',
  20986. ' };',
  20987. '});',
  20988. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20989. ' rtl.addIntf(this, $mod.IBird);',
  20990. '});',
  20991. 'this.DoIt = function (u, i, j) {',
  20992. ' $mod.DoIt({',
  20993. ' get: function () {',
  20994. ' return i;',
  20995. ' },',
  20996. ' set: function (v) {',
  20997. ' i = v;',
  20998. ' }',
  20999. ' }, i, i);',
  21000. '};',
  21001. 'this.Change = function (i, j) {',
  21002. ' $mod.DoIt(i, i.get(), i.get());',
  21003. ' $mod.Change(i, i);',
  21004. '};',
  21005. 'this.i = null;',
  21006. 'this.o = null;',
  21007. '']),
  21008. LinesToStr([ // $mod.$main
  21009. '$mod.DoIt({',
  21010. ' p: $mod,',
  21011. ' get: function () {',
  21012. ' return this.p.i;',
  21013. ' },',
  21014. ' set: function (v) {',
  21015. ' this.p.i = v;',
  21016. ' }',
  21017. '}, $mod.i, $mod.i);',
  21018. '$mod.Change({',
  21019. ' p: $mod,',
  21020. ' get: function () {',
  21021. ' return this.p.i;',
  21022. ' },',
  21023. ' set: function (v) {',
  21024. ' this.p.i = v;',
  21025. ' }',
  21026. '}, {',
  21027. ' p: $mod,',
  21028. ' get: function () {',
  21029. ' return this.p.i;',
  21030. ' },',
  21031. ' set: function (v) {',
  21032. ' this.p.i = v;',
  21033. ' }',
  21034. '});',
  21035. '$mod.DoIt({',
  21036. ' p: $mod,',
  21037. ' get: function () {',
  21038. ' return this.p.o;',
  21039. ' },',
  21040. ' set: function (v) {',
  21041. ' this.p.o = v;',
  21042. ' }',
  21043. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21044. '']));
  21045. end;
  21046. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21047. begin
  21048. StartProgram(false);
  21049. Add([
  21050. '{$interfaces corba}',
  21051. 'type',
  21052. ' IUnknown = interface end;',
  21053. ' TObject = class',
  21054. ' Id: longint;',
  21055. ' end;',
  21056. ' IEnumerator = interface(IUnknown)',
  21057. ' function GetCurrent: TObject;',
  21058. ' function MoveNext: Boolean;',
  21059. ' property Current: TObject read GetCurrent;',
  21060. ' end;',
  21061. ' IEnumerable = interface(IUnknown)',
  21062. ' function GetEnumerator: IEnumerator;',
  21063. ' end;',
  21064. 'var',
  21065. ' o: TObject;',
  21066. ' i: IEnumerable;',
  21067. 'begin',
  21068. ' for o in i do o.Id:=3;',
  21069. '']);
  21070. ConvertProgram;
  21071. CheckSource('TestClassInterface_Corba_ForIn',
  21072. LinesToStr([ // statements
  21073. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21074. 'rtl.createClass(this, "TObject", null, function () {',
  21075. ' this.$init = function () {',
  21076. ' this.Id = 0;',
  21077. ' };',
  21078. ' this.$final = function () {',
  21079. ' };',
  21080. '});',
  21081. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21082. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21083. 'this.o = null;',
  21084. 'this.i = null;',
  21085. '']),
  21086. LinesToStr([ // $mod.$main
  21087. 'var $in = $mod.i.GetEnumerator();',
  21088. 'while ($in.MoveNext()) {',
  21089. ' $mod.o = $in.GetCurrent();',
  21090. ' $mod.o.Id = 3;',
  21091. '};',
  21092. '']));
  21093. end;
  21094. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21095. begin
  21096. StartProgram(false);
  21097. Add([
  21098. '{$interfaces corba}',
  21099. 'type',
  21100. ' IUnknown = interface end;',
  21101. ' IBird = interface(IUnknown)',
  21102. ' function Fly(w: word): word;',
  21103. ' end;',
  21104. ' TBirdArray = array of IBird;',
  21105. 'var',
  21106. ' i: IBird;',
  21107. ' a: TBirdArray;',
  21108. 'begin',
  21109. ' SetLength(a,3);',
  21110. ' i:=a[1];',
  21111. ' a[2]:=i;',
  21112. ' for i in a do i.fly(3);',
  21113. '']);
  21114. ConvertProgram;
  21115. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21116. LinesToStr([ // statements
  21117. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21118. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21119. 'this.i = null;',
  21120. 'this.a = [];',
  21121. '']),
  21122. LinesToStr([ // $mod.$main
  21123. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21124. '$mod.i = $mod.a[1];',
  21125. '$mod.a[2] = $mod.i;',
  21126. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21127. ' $mod.i = $in[$l];',
  21128. ' $mod.i.Fly(3);',
  21129. '};',
  21130. '']));
  21131. end;
  21132. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21133. begin
  21134. StartProgram(false);
  21135. Add([
  21136. '{$interfaces com}',
  21137. 'type',
  21138. ' IUnknown = interface',
  21139. ' function _AddRef: longint;',
  21140. ' function _Release: longint;',
  21141. ' end;',
  21142. ' TObject = class(IUnknown)',
  21143. ' function _AddRef: longint; virtual; abstract;',
  21144. ' function _Release: longint; virtual; abstract;',
  21145. ' end;',
  21146. 'var',
  21147. ' i: IUnknown;',
  21148. 'procedure DoGlobal(o: TObject);',
  21149. 'begin',
  21150. ' i:=nil;',
  21151. ' i:=o;',
  21152. ' i:=i;',
  21153. 'end;',
  21154. 'procedure DoLocal(o: TObject);',
  21155. 'const k: IUnknown = nil;',
  21156. 'var j: IUnknown;',
  21157. 'begin',
  21158. ' k:=o;',
  21159. ' k:=i;',
  21160. ' j:=o;',
  21161. ' j:=i;',
  21162. 'end;',
  21163. 'var o: TObject;',
  21164. 'begin',
  21165. ' i:=nil;',
  21166. ' i:=o;',
  21167. '']);
  21168. ConvertProgram;
  21169. CheckSource('TestClassInterface_COM_AssignVar',
  21170. LinesToStr([ // statements
  21171. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21172. 'rtl.createClass(this, "TObject", null, function () {',
  21173. ' this.$init = function () {',
  21174. ' };',
  21175. ' this.$final = function () {',
  21176. ' };',
  21177. ' rtl.addIntf(this, $mod.IUnknown);',
  21178. '});',
  21179. 'this.i = null;',
  21180. 'this.DoGlobal = function (o) {',
  21181. ' rtl.setIntfP($mod, "i", null);',
  21182. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21183. ' rtl.setIntfP($mod, "i", $mod.i);',
  21184. '};',
  21185. 'var k = null;',
  21186. 'this.DoLocal = function (o) {',
  21187. ' var j = null;',
  21188. ' try{',
  21189. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21190. ' k = rtl.setIntfL(k, $mod.i);',
  21191. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21192. ' j = rtl.setIntfL(j, $mod.i);',
  21193. ' }finally{',
  21194. ' rtl._Release(j);',
  21195. ' };',
  21196. '};',
  21197. 'this.o = null;',
  21198. '']),
  21199. LinesToStr([ // $mod.$main
  21200. 'rtl.setIntfP($mod, "i", null);',
  21201. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21202. '']));
  21203. end;
  21204. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21205. begin
  21206. StartProgram(false);
  21207. Add([
  21208. '{$interfaces com}',
  21209. 'type',
  21210. ' IUnknown = interface',
  21211. ' function _AddRef: longint;',
  21212. ' function _Release: longint;',
  21213. ' end;',
  21214. ' TObject = class(IUnknown)',
  21215. ' function _AddRef: longint; virtual; abstract;',
  21216. ' function _Release: longint; virtual; abstract;',
  21217. ' end;',
  21218. 'procedure DoDefault(i, j: IUnknown);',
  21219. 'begin',
  21220. ' i:=nil;',
  21221. ' i:=j;',
  21222. 'end;',
  21223. 'begin',
  21224. '']);
  21225. ConvertProgram;
  21226. CheckSource('TestClassInterface_COM_AssignArg',
  21227. LinesToStr([ // statements
  21228. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21229. 'rtl.createClass(this, "TObject", null, function () {',
  21230. ' this.$init = function () {',
  21231. ' };',
  21232. ' this.$final = function () {',
  21233. ' };',
  21234. ' rtl.addIntf(this, $mod.IUnknown);',
  21235. '});',
  21236. 'this.DoDefault = function (i, j) {',
  21237. ' rtl._AddRef(i);',
  21238. ' try {',
  21239. ' i = rtl.setIntfL(i, null);',
  21240. ' i = rtl.setIntfL(i, j);',
  21241. ' } finally {',
  21242. ' rtl._Release(i);',
  21243. ' };',
  21244. '};',
  21245. '']),
  21246. LinesToStr([ // $mod.$main
  21247. '']));
  21248. end;
  21249. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21250. begin
  21251. StartProgram(false);
  21252. Add([
  21253. '{$interfaces com}',
  21254. 'type',
  21255. ' IUnknown = interface',
  21256. ' function _AddRef: longint;',
  21257. ' function _Release: longint;',
  21258. ' end;',
  21259. ' TObject = class(IUnknown)',
  21260. ' function _AddRef: longint; virtual; abstract;',
  21261. ' function _Release: longint; virtual; abstract;',
  21262. ' end;',
  21263. 'function DoDefault(i: IUnknown): IUnknown;',
  21264. 'begin',
  21265. ' Result:=i;',
  21266. ' if Result<>nil then exit;',
  21267. 'end;',
  21268. 'begin',
  21269. '']);
  21270. ConvertProgram;
  21271. CheckSource('TestClassInterface_COM_FunctionResult',
  21272. LinesToStr([ // statements
  21273. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21274. 'rtl.createClass(this, "TObject", null, function () {',
  21275. ' this.$init = function () {',
  21276. ' };',
  21277. ' this.$final = function () {',
  21278. ' };',
  21279. ' rtl.addIntf(this, $mod.IUnknown);',
  21280. '});',
  21281. 'this.DoDefault = function (i) {',
  21282. ' var Result = null;',
  21283. ' var $ok = false;',
  21284. ' try {',
  21285. ' Result = rtl.setIntfL(Result, i);',
  21286. ' if(Result !== null){',
  21287. ' $ok = true;',
  21288. ' return Result;',
  21289. ' };',
  21290. ' $ok = true;',
  21291. ' } finally {',
  21292. ' if(!$ok) rtl._Release(Result);',
  21293. ' };',
  21294. ' return Result;',
  21295. '};',
  21296. '']),
  21297. LinesToStr([ // $mod.$main
  21298. '']));
  21299. end;
  21300. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21301. begin
  21302. StartProgram(false);
  21303. Add([
  21304. '{$interfaces com}',
  21305. 'type',
  21306. ' IUnknown = interface',
  21307. ' function _AddRef: longint;',
  21308. ' function _Release: longint;',
  21309. ' end;',
  21310. ' TObject = class(IUnknown)',
  21311. ' function _AddRef: longint; virtual; abstract;',
  21312. ' function _Release: longint; virtual; abstract;',
  21313. ' function GetIntf: IUnknown; virtual;',
  21314. ' end;',
  21315. ' TMouse = class',
  21316. ' function GetIntf: IUnknown; override;',
  21317. ' end;',
  21318. 'function TObject.GetIntf: IUnknown; begin end;',
  21319. 'function TMouse.GetIntf: IUnknown;',
  21320. 'var i: IUnknown;',
  21321. 'begin',
  21322. ' inherited;',
  21323. ' inherited GetIntf;',
  21324. ' inherited GetIntf();',
  21325. ' Result:=inherited GetIntf;',
  21326. ' Result:=inherited GetIntf();',
  21327. ' i:=inherited GetIntf;',
  21328. ' i:=inherited GetIntf();',
  21329. 'end;',
  21330. 'begin',
  21331. '']);
  21332. ConvertProgram;
  21333. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21334. LinesToStr([ // statements
  21335. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21336. 'rtl.createClass(this, "TObject", null, function () {',
  21337. ' this.$init = function () {',
  21338. ' };',
  21339. ' this.$final = function () {',
  21340. ' };',
  21341. ' this.GetIntf = function () {',
  21342. ' var Result = null;',
  21343. ' return Result;',
  21344. ' };',
  21345. ' rtl.addIntf(this, $mod.IUnknown);',
  21346. '});',
  21347. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21348. ' this.GetIntf = function () {',
  21349. ' var Result = null;',
  21350. ' var i = null;',
  21351. ' var $ir = rtl.createIntfRefs();',
  21352. ' var $ok = false;',
  21353. ' try {',
  21354. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21355. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21356. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21357. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21358. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21359. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21360. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21361. ' $ok = true;',
  21362. ' } finally {',
  21363. ' $ir.free();',
  21364. ' rtl._Release(i);',
  21365. ' if (!$ok) rtl._Release(Result);',
  21366. ' };',
  21367. ' return Result;',
  21368. ' };',
  21369. ' rtl.addIntf(this, $mod.IUnknown);',
  21370. '});',
  21371. '']),
  21372. LinesToStr([ // $mod.$main
  21373. '']));
  21374. end;
  21375. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21376. begin
  21377. StartProgram(false);
  21378. Add([
  21379. '{$interfaces com}',
  21380. 'type',
  21381. ' IUnknown = interface',
  21382. ' function _AddRef: longint;',
  21383. ' function _Release: longint;',
  21384. ' end;',
  21385. ' TObject = class(IUnknown)',
  21386. ' function _AddRef: longint; virtual; abstract;',
  21387. ' function _Release: longint; virtual; abstract;',
  21388. ' end;',
  21389. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21390. 'begin',
  21391. ' if i is IUnknown then ;',
  21392. ' if o is IUnknown then ;',
  21393. ' if i is TObject then ;',
  21394. ' i:=j as IUnknown;',
  21395. ' i:=o as IUnknown;',
  21396. ' o:=j as TObject;',
  21397. ' i:=IUnknown(j);',
  21398. ' i:=IUnknown(o);',
  21399. ' o:=TObject(i);',
  21400. 'end;',
  21401. 'begin',
  21402. '']);
  21403. ConvertProgram;
  21404. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21405. LinesToStr([ // statements
  21406. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21407. 'rtl.createClass(this, "TObject", null, function () {',
  21408. ' this.$init = function () {',
  21409. ' };',
  21410. ' this.$final = function () {',
  21411. ' };',
  21412. ' rtl.addIntf(this, $mod.IUnknown);',
  21413. '});',
  21414. 'this.DoDefault = function (i, j, o) {',
  21415. ' rtl._AddRef(i);',
  21416. ' try {',
  21417. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21418. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21419. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21420. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21421. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21422. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21423. ' i = rtl.setIntfL(i, j);',
  21424. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21425. ' o = rtl.intfToClass(i, $mod.TObject);',
  21426. ' } finally {',
  21427. ' rtl._Release(i);',
  21428. ' };',
  21429. '};',
  21430. '']),
  21431. LinesToStr([ // $mod.$main
  21432. '']));
  21433. end;
  21434. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21435. begin
  21436. StartProgram(false);
  21437. Add([
  21438. '{$interfaces com}',
  21439. 'type',
  21440. ' IUnknown = interface',
  21441. ' function _AddRef: longint;',
  21442. ' function _Release: longint;',
  21443. ' end;',
  21444. ' TObject = class(IUnknown)',
  21445. ' function _AddRef: longint; virtual; abstract;',
  21446. ' function _Release: longint; virtual; abstract;',
  21447. ' end;',
  21448. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21449. 'var o: TObject;',
  21450. 'begin',
  21451. ' DoIt(v,v,v,v);',
  21452. ' DoIt(o,o,k,k);',
  21453. 'end;',
  21454. 'procedure DoSome;',
  21455. 'var v: IUnknown;',
  21456. 'begin',
  21457. ' DoIt(v,v,v,v);',
  21458. 'end;',
  21459. 'var i: IUnknown;',
  21460. 'begin',
  21461. ' DoIt(i,i,i,i);',
  21462. '']);
  21463. ConvertProgram;
  21464. CheckSource('TestClassInterface_COM_PassAsArg',
  21465. LinesToStr([ // statements
  21466. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21467. 'rtl.createClass(this, "TObject", null, function () {',
  21468. ' this.$init = function () {',
  21469. ' };',
  21470. ' this.$final = function () {',
  21471. ' };',
  21472. ' rtl.addIntf(this, $mod.IUnknown);',
  21473. '});',
  21474. 'this.DoIt = function (v, j, k, l) {',
  21475. ' var o = null;',
  21476. ' var $ir = rtl.createIntfRefs();',
  21477. ' rtl._AddRef(v);',
  21478. ' try {',
  21479. ' $mod.DoIt(v, v, {',
  21480. ' get: function () {',
  21481. ' return v;',
  21482. ' },',
  21483. ' set: function (w) {',
  21484. ' v = rtl.setIntfL(v, w);',
  21485. ' }',
  21486. ' }, {',
  21487. ' get: function () {',
  21488. ' return v;',
  21489. ' },',
  21490. ' set: function (w) {',
  21491. ' v = rtl.setIntfL(v, w);',
  21492. ' }',
  21493. ' });',
  21494. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21495. ' } finally {',
  21496. ' $ir.free();',
  21497. ' rtl._Release(v);',
  21498. ' };',
  21499. '};',
  21500. 'this.DoSome = function () {',
  21501. ' var v = null;',
  21502. ' try {',
  21503. ' $mod.DoIt(v, v, {',
  21504. ' get: function () {',
  21505. ' return v;',
  21506. ' },',
  21507. ' set: function (w) {',
  21508. ' v = rtl.setIntfL(v, w);',
  21509. ' }',
  21510. ' }, {',
  21511. ' get: function () {',
  21512. ' return v;',
  21513. ' },',
  21514. ' set: function (w) {',
  21515. ' v = rtl.setIntfL(v, w);',
  21516. ' }',
  21517. ' });',
  21518. ' } finally {',
  21519. ' rtl._Release(v);',
  21520. ' };',
  21521. '};',
  21522. 'this.i = null;',
  21523. '']),
  21524. LinesToStr([ // $mod.$main
  21525. '$mod.DoIt($mod.i, $mod.i, {',
  21526. ' p: $mod,',
  21527. ' get: function () {',
  21528. ' return this.p.i;',
  21529. ' },',
  21530. ' set: function (v) {',
  21531. ' rtl.setIntfP(this.p, "i", v);',
  21532. ' }',
  21533. '}, {',
  21534. ' p: $mod,',
  21535. ' get: function () {',
  21536. ' return this.p.i;',
  21537. ' },',
  21538. ' set: function (v) {',
  21539. ' rtl.setIntfP(this.p, "i", v);',
  21540. ' }',
  21541. '});',
  21542. '']));
  21543. end;
  21544. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21545. begin
  21546. StartProgram(false);
  21547. Add([
  21548. '{$interfaces com}',
  21549. 'type',
  21550. ' IUnknown = interface',
  21551. ' function _AddRef: longint;',
  21552. ' function _Release: longint;',
  21553. ' end;',
  21554. ' TObject = class(IUnknown)',
  21555. ' function _AddRef: longint; virtual; abstract;',
  21556. ' function _Release: longint; virtual; abstract;',
  21557. ' end;',
  21558. 'procedure DoIt(out i);',
  21559. 'begin end;',
  21560. 'procedure DoSome;',
  21561. 'var v: IUnknown;',
  21562. 'begin',
  21563. ' DoIt(v);',
  21564. 'end;',
  21565. 'function GetIt: IUnknown;',
  21566. 'begin',
  21567. ' DoIt(Result);',
  21568. 'end;',
  21569. 'var i: IUnknown;',
  21570. 'begin',
  21571. ' DoIt(i);',
  21572. '']);
  21573. ConvertProgram;
  21574. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21575. LinesToStr([ // statements
  21576. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21577. 'rtl.createClass(this, "TObject", null, function () {',
  21578. ' this.$init = function () {',
  21579. ' };',
  21580. ' this.$final = function () {',
  21581. ' };',
  21582. ' rtl.addIntf(this, $mod.IUnknown);',
  21583. '});',
  21584. 'this.DoIt = function (i) {',
  21585. '};',
  21586. 'this.DoSome = function () {',
  21587. ' var v = null;',
  21588. ' try {',
  21589. ' $mod.DoIt({',
  21590. ' get: function () {',
  21591. ' return v;',
  21592. ' },',
  21593. ' set: function (w) {',
  21594. ' v = w;',
  21595. ' }',
  21596. ' });',
  21597. ' } finally {',
  21598. ' rtl._Release(v);',
  21599. ' };',
  21600. '};',
  21601. 'this.GetIt = function () {',
  21602. ' var Result = null;',
  21603. ' var $ok = false;',
  21604. ' try {',
  21605. ' $mod.DoIt({',
  21606. ' get: function () {',
  21607. ' return Result;',
  21608. ' },',
  21609. ' set: function (v) {',
  21610. ' Result = v;',
  21611. ' }',
  21612. ' });',
  21613. ' $ok = true;',
  21614. ' } finally {',
  21615. ' if (!$ok) rtl._Release(Result);',
  21616. ' };',
  21617. ' return Result;',
  21618. '};',
  21619. 'this.i = null;',
  21620. '']),
  21621. LinesToStr([ // $mod.$main
  21622. 'try {',
  21623. ' $mod.DoIt({',
  21624. ' p: $mod,',
  21625. ' get: function () {',
  21626. ' return this.p.i;',
  21627. ' },',
  21628. ' set: function (v) {',
  21629. ' this.p.i = v;',
  21630. ' }',
  21631. ' });',
  21632. '} finally {',
  21633. ' rtl._Release($mod.i);',
  21634. '};',
  21635. '']));
  21636. end;
  21637. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21638. begin
  21639. StartProgram(false);
  21640. Add([
  21641. '{$interfaces com}',
  21642. 'type',
  21643. ' IUnknown = interface',
  21644. ' function _AddRef: longint;',
  21645. ' function _Release: longint;',
  21646. ' end;',
  21647. ' TObject = class(IUnknown)',
  21648. ' function _AddRef: longint; virtual; abstract;',
  21649. ' function _Release: longint; virtual; abstract;',
  21650. ' end;',
  21651. 'function GetIt: IUnknown;',
  21652. 'begin',
  21653. 'end;',
  21654. 'procedure DoSome;',
  21655. 'var v: IUnknown;',
  21656. ' i: longint;',
  21657. 'begin',
  21658. ' v:=GetIt;',
  21659. ' v:=GetIt();',
  21660. ' GetIt()._AddRef;',
  21661. ' i:=GetIt()._AddRef;',
  21662. 'end;',
  21663. 'var v: IUnknown;',
  21664. ' i: longint;',
  21665. 'begin',
  21666. ' v:=GetIt;',
  21667. ' v:=GetIt();',
  21668. ' GetIt()._AddRef;',
  21669. ' i:=GetIt()._AddRef;',
  21670. '']);
  21671. ConvertProgram;
  21672. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21673. LinesToStr([ // statements
  21674. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21675. 'rtl.createClass(this, "TObject", null, function () {',
  21676. ' this.$init = function () {',
  21677. ' };',
  21678. ' this.$final = function () {',
  21679. ' };',
  21680. ' rtl.addIntf(this, $mod.IUnknown);',
  21681. '});',
  21682. 'this.GetIt = function () {',
  21683. ' var Result = null;',
  21684. ' return Result;',
  21685. '};',
  21686. 'this.DoSome = function () {',
  21687. ' var v = null;',
  21688. ' var i = 0;',
  21689. ' var $ir = rtl.createIntfRefs();',
  21690. ' try {',
  21691. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21692. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21693. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21694. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21695. ' } finally {',
  21696. ' $ir.free();',
  21697. ' rtl._Release(v);',
  21698. ' };',
  21699. '};',
  21700. 'this.v = null;',
  21701. 'this.i = 0;',
  21702. '']),
  21703. LinesToStr([ // $mod.$main
  21704. 'var $ir = rtl.createIntfRefs();',
  21705. 'try {',
  21706. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21707. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21708. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21709. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21710. '} finally {',
  21711. ' $ir.free();',
  21712. '};',
  21713. '']));
  21714. end;
  21715. procedure TTestModule.TestClassInterface_COM_Property;
  21716. begin
  21717. StartProgram(false);
  21718. Add([
  21719. '{$interfaces com}',
  21720. 'type',
  21721. ' IUnknown = interface',
  21722. ' function _AddRef: longint;',
  21723. ' function _Release: longint;',
  21724. ' end;',
  21725. ' TObject = class(IUnknown)',
  21726. ' FAnt: IUnknown;',
  21727. ' function _AddRef: longint; virtual; abstract;',
  21728. ' function _Release: longint; virtual; abstract;',
  21729. ' function GetBird: IUnknown; virtual; abstract;',
  21730. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21731. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21732. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21733. ' property Ant: IUnknown read FAnt write FAnt;',
  21734. ' property Bird: IUnknown read GetBird write SetBird;',
  21735. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21736. ' end;',
  21737. 'procedure DoIt;',
  21738. 'var',
  21739. ' o: TObject;',
  21740. ' v: IUnknown;',
  21741. 'begin',
  21742. ' v:=o.Ant;',
  21743. ' o.Ant:=v;',
  21744. ' o.Ant:=o.Ant;',
  21745. ' v:=o.Bird;',
  21746. ' o.Bird:=v;',
  21747. ' o.Bird:=o.Bird;',
  21748. ' v:=o.Items[1];',
  21749. ' o.Items[2]:=v;',
  21750. ' o.Items[3]:=o.Items[4];',
  21751. ' v:=o[5];',
  21752. ' o[6]:=v;',
  21753. ' o[7]:=o[8];',
  21754. 'end;',
  21755. 'begin',
  21756. '']);
  21757. ConvertProgram;
  21758. CheckSource('TestClassInterface_COM_Property',
  21759. LinesToStr([ // statements
  21760. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21761. 'rtl.createClass(this, "TObject", null, function () {',
  21762. ' this.$init = function () {',
  21763. ' this.FAnt = null;',
  21764. ' };',
  21765. ' this.$final = function () {',
  21766. ' rtl.setIntfP(this, "FAnt", null);',
  21767. ' };',
  21768. ' rtl.addIntf(this, $mod.IUnknown);',
  21769. '});',
  21770. 'this.DoIt = function () {',
  21771. ' var o = null;',
  21772. ' var v = null;',
  21773. ' var $ir = rtl.createIntfRefs();',
  21774. ' try {',
  21775. ' v = rtl.setIntfL(v, o.FAnt);',
  21776. ' rtl.setIntfP(o, "FAnt", v);',
  21777. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21778. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21779. ' o.SetBird(v);',
  21780. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21781. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21782. ' o.SetItems(2, v);',
  21783. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21784. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21785. ' o.SetItems(6, v);',
  21786. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21787. ' } finally {',
  21788. ' $ir.free();',
  21789. ' rtl._Release(v);',
  21790. ' };',
  21791. '};',
  21792. '']),
  21793. LinesToStr([ // $mod.$main
  21794. '']));
  21795. end;
  21796. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21797. begin
  21798. StartProgram(false);
  21799. Add([
  21800. '{$interfaces com}',
  21801. 'type',
  21802. ' IUnknown = interface',
  21803. ' function _AddRef: longint;',
  21804. ' function _Release: longint;',
  21805. ' function GetBird: IUnknown;',
  21806. ' procedure SetBird(Value: IUnknown);',
  21807. ' function GetItems(Index: longint): IUnknown;',
  21808. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21809. ' property Bird: IUnknown read GetBird write SetBird;',
  21810. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21811. ' end;',
  21812. ' TObject = class(IUnknown)',
  21813. ' function _AddRef: longint; virtual; abstract;',
  21814. ' function _Release: longint; virtual; abstract;',
  21815. ' function GetBird: IUnknown; virtual; abstract;',
  21816. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21817. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21818. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21819. ' end;',
  21820. 'procedure DoIt;',
  21821. 'var',
  21822. ' o: TObject;',
  21823. ' v: IUnknown;',
  21824. 'begin',
  21825. ' v:=v.Items[1];',
  21826. ' v.Items[2]:=v;',
  21827. ' v.Items[3]:=v.Items[4];',
  21828. ' v:=v[5];',
  21829. ' v[6]:=v;',
  21830. ' v[7]:=v[8];',
  21831. ' v[9].Bird.Bird:=v;',
  21832. ' v:=v.Bird[10].Bird',
  21833. 'end;',
  21834. 'begin',
  21835. '']);
  21836. ConvertProgram;
  21837. CheckSource('TestClassInterface_COM_IntfProperty',
  21838. LinesToStr([ // statements
  21839. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21840. ' "_AddRef",',
  21841. ' "_Release",',
  21842. ' "GetBird",',
  21843. ' "SetBird",',
  21844. ' "GetItems",',
  21845. ' "SetItems"',
  21846. '], null);',
  21847. 'rtl.createClass(this, "TObject", null, function () {',
  21848. ' this.$init = function () {',
  21849. ' };',
  21850. ' this.$final = function () {',
  21851. ' };',
  21852. ' rtl.addIntf(this, $mod.IUnknown);',
  21853. '});',
  21854. 'this.DoIt = function () {',
  21855. ' var o = null;',
  21856. ' var v = null;',
  21857. ' var $ir = rtl.createIntfRefs();',
  21858. ' try {',
  21859. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21860. ' v.SetItems(2, v);',
  21861. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21862. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21863. ' v.SetItems(6, v);',
  21864. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21865. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21866. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21867. ' } finally {',
  21868. ' $ir.free();',
  21869. ' rtl._Release(v);',
  21870. ' };',
  21871. '};',
  21872. '']),
  21873. LinesToStr([ // $mod.$main
  21874. '']));
  21875. end;
  21876. procedure TTestModule.TestClassInterface_COM_Delegation;
  21877. begin
  21878. StartProgram(false);
  21879. Add([
  21880. '{$interfaces com}',
  21881. 'type',
  21882. ' IUnknown = interface',
  21883. ' function _AddRef: longint;',
  21884. ' function _Release: longint;',
  21885. ' end;',
  21886. ' IBird = interface(IUnknown)',
  21887. ' procedure Fly(s: string);',
  21888. ' end;',
  21889. ' IEagle = interface(IBird) end;',
  21890. ' IDove = interface(IBird) end;',
  21891. ' ISwallow = interface(IBird) end;',
  21892. ' TObject = class',
  21893. ' end;',
  21894. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21895. ' function _AddRef: longint; virtual; abstract;',
  21896. ' function _Release: longint; virtual; abstract;',
  21897. ' procedure Fly(s: string); virtual; abstract;',
  21898. ' end;',
  21899. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21900. ' function _AddRef: longint; virtual; abstract;',
  21901. ' function _Release: longint; virtual; abstract;',
  21902. ' FBirdIntf: IBird;',
  21903. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21904. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21905. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21906. ' FDoveObj: TBird;',
  21907. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21908. ' function GetSwallowObj: TBird; virtual; abstract;',
  21909. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21910. ' end;',
  21911. 'begin',
  21912. '']);
  21913. ConvertProgram;
  21914. CheckSource('TestClassInterface_COM_Delegation',
  21915. LinesToStr([ // statements
  21916. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21917. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21918. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21919. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21920. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21921. 'rtl.createClass(this, "TObject", null, function () {',
  21922. ' this.$init = function () {',
  21923. ' };',
  21924. ' this.$final = function () {',
  21925. ' };',
  21926. '});',
  21927. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21928. ' rtl.addIntf(this, $mod.IBird);',
  21929. ' rtl.addIntf(this, $mod.IEagle);',
  21930. ' rtl.addIntf(this, $mod.IDove);',
  21931. ' rtl.addIntf(this, $mod.ISwallow);',
  21932. '});',
  21933. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21934. ' this.$init = function () {',
  21935. ' $mod.TObject.$init.call(this);',
  21936. ' this.FBirdIntf = null;',
  21937. ' this.FDoveObj = null;',
  21938. ' };',
  21939. ' this.$final = function () {',
  21940. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21941. ' this.FDoveObj = undefined;',
  21942. ' $mod.TObject.$final.call(this);',
  21943. ' };',
  21944. ' this.$intfmaps = {',
  21945. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  21946. ' return rtl._AddRef(this.FBirdIntf);',
  21947. ' },',
  21948. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  21949. ' return this.GetEagleIntf();',
  21950. ' },',
  21951. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  21952. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  21953. ' },',
  21954. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  21955. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  21956. ' }',
  21957. ' };',
  21958. '});',
  21959. '']),
  21960. LinesToStr([ // $mod.$main
  21961. '']));
  21962. end;
  21963. procedure TTestModule.TestClassInterface_COM_With;
  21964. begin
  21965. StartProgram(false);
  21966. Add([
  21967. '{$interfaces com}',
  21968. 'type',
  21969. ' IUnknown = interface',
  21970. ' function _AddRef: longint;',
  21971. ' function _Release: longint;',
  21972. ' function GetAnt: IUnknown;',
  21973. ' property Ant: IUnknown read GetAnt;',
  21974. ' end;',
  21975. ' TObject = class(IUnknown)',
  21976. ' function _AddRef: longint; virtual; abstract;',
  21977. ' function _Release: longint; virtual; abstract;',
  21978. ' function GetAnt: IUnknown; virtual; abstract;',
  21979. ' property Ant: IUnknown read GetAnt;',
  21980. ' end;',
  21981. 'procedure DoIt;',
  21982. 'var',
  21983. ' i: IUnknown;',
  21984. 'begin',
  21985. ' with i do ',
  21986. ' GetAnt;',
  21987. ' with i.Ant, Ant do ',
  21988. ' GetAnt;',
  21989. 'end;',
  21990. 'begin',
  21991. '']);
  21992. ConvertProgram;
  21993. CheckSource('TestClassInterface_COM_With',
  21994. LinesToStr([ // statements
  21995. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  21996. 'rtl.createClass(this, "TObject", null, function () {',
  21997. ' this.$init = function () {',
  21998. ' };',
  21999. ' this.$final = function () {',
  22000. ' };',
  22001. ' rtl.addIntf(this, $mod.IUnknown);',
  22002. '});',
  22003. 'this.DoIt = function () {',
  22004. ' var i = null;',
  22005. ' var $ir = rtl.createIntfRefs();',
  22006. ' try {',
  22007. ' $ir.ref(1, i.GetAnt());',
  22008. ' var $with = $ir.ref(2, i.GetAnt());',
  22009. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22010. ' $ir.ref(4, $with1.GetAnt());',
  22011. ' } finally {',
  22012. ' $ir.free();',
  22013. ' };',
  22014. '};',
  22015. '']),
  22016. LinesToStr([ // $mod.$main
  22017. '']));
  22018. end;
  22019. procedure TTestModule.TestClassInterface_COM_ForIn;
  22020. begin
  22021. StartProgram(false);
  22022. Add([
  22023. '{$interfaces com}',
  22024. 'type',
  22025. ' IUnknown = interface end;',
  22026. ' TObject = class',
  22027. ' Id: longint;',
  22028. ' end;',
  22029. ' IEnumerator = interface(IUnknown)',
  22030. ' function GetCurrent: TObject;',
  22031. ' function MoveNext: Boolean;',
  22032. ' property Current: TObject read GetCurrent;',
  22033. ' end;',
  22034. ' IEnumerable = interface(IUnknown)',
  22035. ' function GetEnumerator: IEnumerator;',
  22036. ' end;',
  22037. 'var',
  22038. ' o: TObject;',
  22039. ' i: IEnumerable;',
  22040. 'begin',
  22041. ' for o in i do o.Id:=3;',
  22042. '']);
  22043. ConvertProgram;
  22044. CheckSource('TestClassInterface_COM_ForIn',
  22045. LinesToStr([ // statements
  22046. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22047. 'rtl.createClass(this, "TObject", null, function () {',
  22048. ' this.$init = function () {',
  22049. ' this.Id = 0;',
  22050. ' };',
  22051. ' this.$final = function () {',
  22052. ' };',
  22053. '});',
  22054. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22055. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22056. 'this.o = null;',
  22057. 'this.i = null;',
  22058. '']),
  22059. LinesToStr([ // $mod.$main
  22060. 'var $in = $mod.i.GetEnumerator();',
  22061. 'try {',
  22062. ' while ($in.MoveNext()) {',
  22063. ' $mod.o = $in.GetCurrent();',
  22064. ' $mod.o.Id = 3;',
  22065. ' }',
  22066. '} finally {',
  22067. ' rtl._Release($in)',
  22068. '};',
  22069. '']));
  22070. end;
  22071. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf;
  22072. begin
  22073. {$IFNDEF EnableCOMArrayOfIntf}
  22074. exit;
  22075. {$ENDIF}
  22076. StartProgram(false);
  22077. Add([
  22078. '{$interfaces com}',
  22079. 'type',
  22080. ' IUnknown = interface end;',
  22081. ' IBird = interface(IUnknown)',
  22082. ' function Fly(w: word): word;',
  22083. ' end;',
  22084. ' TBirdArray = array of IBird;',
  22085. 'procedure Run;',
  22086. 'var',
  22087. ' i: IBird;',
  22088. ' a,b: TBirdArray;',
  22089. 'begin',
  22090. //' SetLength(a,3);',
  22091. ' a:=b;',
  22092. ' i:=a[1];',
  22093. ' a[2]:=i;',
  22094. //' for i in a do i.fly(3);',
  22095. // a:=copy(b,1,2);
  22096. // a:=concat(b,a);
  22097. // insert(i,b,1);
  22098. // a:=[i,i];
  22099. 'end;',
  22100. // ToDo: pass TBirdArray as arg
  22101. 'begin',
  22102. '']);
  22103. ConvertProgram;
  22104. CheckSource('TestClassInterface_COM_ArrayOfIntf',
  22105. LinesToStr([ // statements
  22106. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22107. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22108. 'this.Run = function () {',
  22109. ' var i = null;',
  22110. ' var a = [];',
  22111. ' var b = [];',
  22112. ' try {',
  22113. ' a = rtl.arrayRef(b);',
  22114. ' i = rtl.setIntfL(i, a[1]);',
  22115. ' rtl.setIntfP(a, 2, i);',
  22116. ' } finally {',
  22117. ' rtl._Release(i);',
  22118. ' rtl._ReleaseArray(a,1);',
  22119. ' };',
  22120. '};',
  22121. '']),
  22122. LinesToStr([ // $mod.$main
  22123. '']));
  22124. end;
  22125. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  22126. begin
  22127. StartProgram(false);
  22128. Add([
  22129. '{$interfaces com}',
  22130. 'type',
  22131. ' IUnknown = interface',
  22132. ' function _AddRef: longint;',
  22133. ' function _Release: longint;',
  22134. ' end;',
  22135. ' TObject = class',
  22136. ' end;',
  22137. ' TArrOfIntf = array of IUnknown;',
  22138. 'begin',
  22139. '']);
  22140. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  22141. ConvertProgram;
  22142. end;
  22143. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22144. begin
  22145. StartProgram(false);
  22146. Add([
  22147. '{$interfaces com}',
  22148. 'type',
  22149. ' IUnknown = interface',
  22150. ' function _AddRef: longint;',
  22151. ' function _Release: longint;',
  22152. ' end;',
  22153. ' TRec = record',
  22154. ' i: IUnknown;',
  22155. ' end;',
  22156. 'begin',
  22157. '']);
  22158. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22159. ConvertProgram;
  22160. end;
  22161. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22162. begin
  22163. StartUnit(false);
  22164. Add([
  22165. '{$interfaces com}',
  22166. 'interface',
  22167. 'implementation',
  22168. 'type',
  22169. ' IUnknown = interface',
  22170. ' function _AddRef: longint;',
  22171. ' end;',
  22172. ' TObject = class(IUnknown)',
  22173. ' function _AddRef: longint;',
  22174. ' end;',
  22175. 'function TObject._AddRef: longint; begin end;',
  22176. 'var i: IUnknown;',
  22177. ' o: TObject;',
  22178. 'initialization',
  22179. ' i:=nil;',
  22180. ' i:=i;',
  22181. ' i:=o;',
  22182. ' if (o as IUnknown)=nil then ;',
  22183. '']);
  22184. ConvertUnit;
  22185. CheckSource('TestClassInterface_COM_UnitInitialization',
  22186. LinesToStr([ // statements
  22187. 'var $impl = $mod.$impl;',
  22188. '']),
  22189. LinesToStr([ // this.$init
  22190. 'var $ir = rtl.createIntfRefs();',
  22191. 'try {',
  22192. ' rtl.setIntfP($impl, "i", null);',
  22193. ' rtl.setIntfP($impl, "i", $impl.i);',
  22194. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22195. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22196. '} finally {',
  22197. ' $ir.free();',
  22198. '};',
  22199. '']),
  22200. LinesToStr([ // implementation
  22201. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22202. 'rtl.createClass($impl, "TObject", null, function () {',
  22203. ' this.$init = function () {',
  22204. ' };',
  22205. ' this.$final = function () {',
  22206. ' };',
  22207. ' this._AddRef = function () {',
  22208. ' var Result = 0;',
  22209. ' return Result;',
  22210. ' };',
  22211. ' rtl.addIntf(this, $impl.IUnknown);',
  22212. '});',
  22213. '$impl.i = null;',
  22214. '$impl.o = null;',
  22215. ''])
  22216. );
  22217. end;
  22218. procedure TTestModule.TestClassInterface_Corba_GUID;
  22219. begin
  22220. StartProgram(false);
  22221. Add([
  22222. '{$interfaces corba}',
  22223. 'type',
  22224. ' IUnknown = interface',
  22225. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22226. ' end;',
  22227. ' TObject = class end;',
  22228. ' TGUID = record D1, D2, D3, D4: word; end;',
  22229. ' TAliasGUID = TGUID;',
  22230. ' TGUIDString = type string;',
  22231. ' TAliasGUIDString = TGUIDString;',
  22232. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  22233. 'begin end;',
  22234. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  22235. 'begin end;',
  22236. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  22237. 'begin end;',
  22238. 'var',
  22239. ' i: IUnknown;',
  22240. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  22241. ' s: TAliasGUIDString;',
  22242. 'begin',
  22243. ' DoConstGUIDIt(IUnknown);',
  22244. ' DoDefGUID(IUnknown);',
  22245. ' DoStr(IUnknown);',
  22246. ' DoConstGUIDIt(i);',
  22247. ' DoDefGUID(i);',
  22248. ' DoStr(i);',
  22249. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22250. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22251. ' DoStr(g);',
  22252. ' g:=i;',
  22253. ' g:=IUnknown;',
  22254. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22255. ' s:=i;',
  22256. ' s:=IUnknown;',
  22257. ' s:=g;',
  22258. ' if g=i then ;',
  22259. ' if i=g then ;',
  22260. ' if g=IUnknown then ;',
  22261. ' if IUnknown=g then ;',
  22262. ' if s=i then ;',
  22263. ' if i=s then ;',
  22264. ' if s=IUnknown then ;',
  22265. ' if IUnknown=s then ;',
  22266. ' if s=g then ;',
  22267. ' if g=s then ;',
  22268. '']);
  22269. ConvertProgram;
  22270. CheckSource('TestClassInterface_Corba_GUID',
  22271. LinesToStr([ // statements
  22272. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22273. 'rtl.createClass(this, "TObject", null, function () {',
  22274. ' this.$init = function () {',
  22275. ' };',
  22276. ' this.$final = function () {',
  22277. ' };',
  22278. '});',
  22279. 'rtl.recNewT(this, "TGUID", function () {',
  22280. ' this.D1 = 0;',
  22281. ' this.D2 = 0;',
  22282. ' this.D3 = 0;',
  22283. ' this.D4 = 0;',
  22284. ' this.$eq = function (b) {',
  22285. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22286. ' };',
  22287. ' this.$assign = function (s) {',
  22288. ' this.D1 = s.D1;',
  22289. ' this.D2 = s.D2;',
  22290. ' this.D3 = s.D3;',
  22291. ' this.D4 = s.D4;',
  22292. ' return this;',
  22293. ' };',
  22294. '});',
  22295. 'this.DoConstGUIDIt = function (g) {',
  22296. '};',
  22297. 'this.DoDefGUID = function (g) {',
  22298. '};',
  22299. 'this.DoStr = function (s) {',
  22300. '};',
  22301. 'this.i = null;',
  22302. 'this.g = this.TGUID.$clone({',
  22303. ' D1: 0xD91C9AF4,',
  22304. ' D2: 0x3C93,',
  22305. ' D3: 0x420F,',
  22306. ' D4: [',
  22307. ' 0xA3,',
  22308. ' 0x03,',
  22309. ' 0xBF,',
  22310. ' 0x5B,',
  22311. ' 0xA8,',
  22312. ' 0x2B,',
  22313. ' 0xFD,',
  22314. ' 0x23',
  22315. ' ]',
  22316. '});',
  22317. 'this.s = "";',
  22318. '']),
  22319. LinesToStr([ // $mod.$main
  22320. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  22321. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  22322. '$mod.DoStr($mod.IUnknown.$guid);',
  22323. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  22324. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  22325. '$mod.DoStr($mod.i.$guid);',
  22326. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22327. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22328. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  22329. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  22330. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  22331. '$mod.g.$assign({',
  22332. ' D1: 0xD91C9AF4,',
  22333. ' D2: 0x3C93,',
  22334. ' D3: 0x420F,',
  22335. ' D4: [',
  22336. ' 0xA3,',
  22337. ' 0x03,',
  22338. ' 0xBF,',
  22339. ' 0x5B,',
  22340. ' 0xA8,',
  22341. ' 0x2B,',
  22342. ' 0xFD,',
  22343. ' 0x23',
  22344. ' ]',
  22345. '});',
  22346. '$mod.s = $mod.i.$guid;',
  22347. '$mod.s = $mod.IUnknown.$guid;',
  22348. '$mod.s = rtl.guidrToStr($mod.g);',
  22349. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22350. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22351. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22352. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22353. 'if ($mod.s === $mod.i.$guid) ;',
  22354. 'if ($mod.i.$guid === $mod.s) ;',
  22355. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22356. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22357. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22358. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22359. '']));
  22360. end;
  22361. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  22362. begin
  22363. StartProgram(false);
  22364. Add([
  22365. '{$interfaces corba}',
  22366. 'type',
  22367. ' IUnknown = interface',
  22368. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22369. ' end;',
  22370. ' TGUID = record D1, D2, D3, D4: word; end;',
  22371. ' TAliasGUID = TGUID;',
  22372. ' TGUIDString = type string;',
  22373. ' TAliasGUIDString = TGUIDString;',
  22374. ' TObject = class',
  22375. ' function GetG: TAliasGUID; virtual; abstract;',
  22376. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22377. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22378. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22379. ' property g: TAliasGUID read GetG write SetG;',
  22380. ' property s: TAliasGUIDString read GetS write SetS;',
  22381. ' end;',
  22382. 'var o: TObject;',
  22383. 'begin',
  22384. ' o.g:=IUnknown;',
  22385. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22386. ' o.s:=IUnknown;',
  22387. ' o.s:=o.g;',
  22388. '']);
  22389. ConvertProgram;
  22390. CheckSource('TestClassInterface_GUIDProperty',
  22391. LinesToStr([ // statements
  22392. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22393. 'rtl.recNewT(this, "TGUID", function () {',
  22394. ' this.D1 = 0;',
  22395. ' this.D2 = 0;',
  22396. ' this.D3 = 0;',
  22397. ' this.D4 = 0;',
  22398. ' this.$eq = function (b) {',
  22399. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22400. ' };',
  22401. ' this.$assign = function (s) {',
  22402. ' this.D1 = s.D1;',
  22403. ' this.D2 = s.D2;',
  22404. ' this.D3 = s.D3;',
  22405. ' this.D4 = s.D4;',
  22406. ' return this;',
  22407. ' };',
  22408. '});',
  22409. 'rtl.createClass(this, "TObject", null, function () {',
  22410. ' this.$init = function () {',
  22411. ' };',
  22412. ' this.$final = function () {',
  22413. ' };',
  22414. '});',
  22415. 'this.o = null;',
  22416. '']),
  22417. LinesToStr([ // $mod.$main
  22418. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22419. '$mod.o.SetG({',
  22420. ' D1: 0xD91C9AF4,',
  22421. ' D2: 0x3C93,',
  22422. ' D3: 0x420F,',
  22423. ' D4: [',
  22424. ' 0xA3,',
  22425. ' 0x03,',
  22426. ' 0xBF,',
  22427. ' 0x5B,',
  22428. ' 0xA8,',
  22429. ' 0x2B,',
  22430. ' 0xFD,',
  22431. ' 0x23',
  22432. ' ]',
  22433. '});',
  22434. '$mod.o.SetS($mod.IUnknown.$guid);',
  22435. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22436. '']));
  22437. end;
  22438. procedure TTestModule.TestClassHelper_ClassVar;
  22439. begin
  22440. StartProgram(false);
  22441. Add([
  22442. 'type',
  22443. ' TObject = class',
  22444. ' end;',
  22445. ' THelper = class helper for TObject',
  22446. ' const',
  22447. ' One = 1;',
  22448. ' Two: word = 2;',
  22449. ' class var',
  22450. ' Glob: word;',
  22451. ' function Foo(w: word): word;',
  22452. ' class function Bar(w: word): word;',
  22453. ' end;',
  22454. 'function THelper.foo(w: word): word;',
  22455. 'begin',
  22456. ' Result:=w;',
  22457. ' Two:=One+w;',
  22458. ' Glob:=Glob;',
  22459. ' Result:=Self.Glob;',
  22460. ' Self.Glob:=Self.Glob;',
  22461. ' with Self do Glob:=Glob;',
  22462. 'end;',
  22463. 'class function THelper.bar(w: word): word;',
  22464. 'begin',
  22465. ' Result:=w;',
  22466. ' Two:=One;',
  22467. ' Glob:=Glob;',
  22468. ' Self.Glob:=Self.Glob;',
  22469. ' with Self do Glob:=Glob;',
  22470. 'end;',
  22471. 'var o: TObject;',
  22472. 'begin',
  22473. ' tobject.two:=tobject.one;',
  22474. ' tobject.Glob:=tobject.Glob;',
  22475. ' with tobject do begin',
  22476. ' two:=one;',
  22477. ' Glob:=Glob;',
  22478. ' end;',
  22479. ' o.two:=o.one;',
  22480. ' o.Glob:=o.Glob;',
  22481. ' with o do begin',
  22482. ' two:=one;',
  22483. ' Glob:=Glob;',
  22484. ' end;',
  22485. '']);
  22486. ConvertProgram;
  22487. CheckSource('TestClassHelper_ClassVar',
  22488. LinesToStr([ // statements
  22489. 'rtl.createClass(this, "TObject", null, function () {',
  22490. ' this.$init = function () {',
  22491. ' };',
  22492. ' this.$final = function () {',
  22493. ' };',
  22494. '});',
  22495. 'rtl.createHelper(this, "THelper", null, function () {',
  22496. ' this.One = 1;',
  22497. ' this.Two = 2;',
  22498. ' this.Glob = 0;',
  22499. ' this.Foo = function (w) {',
  22500. ' var Result = 0;',
  22501. ' Result = w;',
  22502. ' $mod.THelper.Two = 1 + w;',
  22503. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22504. ' Result = $mod.THelper.Glob;',
  22505. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22506. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22507. ' return Result;',
  22508. ' };',
  22509. ' this.Bar = function (w) {',
  22510. ' var Result = 0;',
  22511. ' Result = w;',
  22512. ' $mod.THelper.Two = 1;',
  22513. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22514. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22515. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22516. ' return Result;',
  22517. ' };',
  22518. '});',
  22519. 'this.o = null;',
  22520. '']),
  22521. LinesToStr([ // $mod.$main
  22522. '$mod.THelper.Two = 1;',
  22523. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22524. 'var $with = $mod.TObject;',
  22525. '$mod.THelper.Two = 1;',
  22526. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22527. '$mod.THelper.Two = 1;',
  22528. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22529. 'var $with1 = $mod.o;',
  22530. '$mod.THelper.Two = 1;',
  22531. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22532. '']));
  22533. end;
  22534. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22535. begin
  22536. StartProgram(false);
  22537. Add([
  22538. 'type',
  22539. ' TObject = class',
  22540. ' FSize: word;',
  22541. ' property Size: word read FSize write FSize;',
  22542. ' end;',
  22543. ' THelper = class helper for TObject',
  22544. ' function Foo(w: word = 1): word;',
  22545. ' end;',
  22546. 'function THelper.foo(w: word): word;',
  22547. 'begin',
  22548. ' Result:=Size;',
  22549. ' Size:=Size+2;',
  22550. ' Self.Size:=Self.Size+3;',
  22551. ' FSize:=FSize+4;',
  22552. ' Self.FSize:=Self.FSize+5;',
  22553. ' with Self do begin',
  22554. ' Size:=Size+6;',
  22555. ' FSize:=FSize+7;',
  22556. ' FSize:=FSize+8;',
  22557. ' end;',
  22558. 'end;',
  22559. 'begin',
  22560. '']);
  22561. ConvertProgram;
  22562. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22563. LinesToStr([ // statements
  22564. 'rtl.createClass(this, "TObject", null, function () {',
  22565. ' this.$init = function () {',
  22566. ' this.FSize = 0;',
  22567. ' };',
  22568. ' this.$final = function () {',
  22569. ' };',
  22570. '});',
  22571. 'rtl.createHelper(this, "THelper", null, function () {',
  22572. ' this.Foo = function (w) {',
  22573. ' var Result = 0;',
  22574. ' Result = this.FSize;',
  22575. ' this.FSize = this.FSize + 2;',
  22576. ' this.FSize = this.FSize + 3;',
  22577. ' this.FSize = this.FSize + 4;',
  22578. ' this.FSize = this.FSize + 5;',
  22579. ' this.FSize = this.FSize + 6;',
  22580. ' this.FSize = this.FSize + 7;',
  22581. ' this.FSize = this.FSize + 8;',
  22582. ' return Result;',
  22583. ' };',
  22584. '});',
  22585. '']),
  22586. LinesToStr([ // $mod.$main
  22587. '']));
  22588. end;
  22589. procedure TTestModule.TestClassHelper_Method_Call;
  22590. begin
  22591. StartProgram(false);
  22592. Add([
  22593. 'type',
  22594. ' TObject = class',
  22595. ' procedure Run(w: word = 10);',
  22596. ' end;',
  22597. ' THelper = class helper for TObject',
  22598. ' function Foo(w: word = 1): word;',
  22599. ' end;',
  22600. 'procedure TObject.Run(w: word);',
  22601. 'var o: TObject;',
  22602. 'begin',
  22603. ' Foo;',
  22604. ' Foo();',
  22605. ' Foo(2);',
  22606. ' Self.Foo;',
  22607. ' Self.Foo();',
  22608. ' Self.Foo(3);',
  22609. ' with Self do begin',
  22610. ' Foo;',
  22611. ' Foo();',
  22612. ' Foo(4);',
  22613. ' end;',
  22614. ' with o do Foo(5);',
  22615. 'end;',
  22616. 'function THelper.foo(w: word): word;',
  22617. 'begin',
  22618. ' Run;',
  22619. ' Run();',
  22620. ' Run(11);',
  22621. ' Foo;',
  22622. ' Foo();',
  22623. ' Foo(12);',
  22624. ' Self.Foo;',
  22625. ' Self.Foo();',
  22626. ' Self.Foo(13);',
  22627. ' with Self do begin',
  22628. ' Foo;',
  22629. ' Foo();',
  22630. ' Foo(14);',
  22631. ' end;',
  22632. 'end;',
  22633. 'var Obj: TObject;',
  22634. 'begin',
  22635. ' obj.Foo;',
  22636. ' obj.Foo();',
  22637. ' obj.Foo(21);',
  22638. ' with obj do begin',
  22639. ' Foo;',
  22640. ' Foo();',
  22641. ' Foo(22);',
  22642. ' end;',
  22643. '']);
  22644. ConvertProgram;
  22645. CheckSource('TestClassHelper_Method_Call',
  22646. LinesToStr([ // statements
  22647. 'rtl.createClass(this, "TObject", null, function () {',
  22648. ' this.$init = function () {',
  22649. ' };',
  22650. ' this.$final = function () {',
  22651. ' };',
  22652. ' this.Run = function (w) {',
  22653. ' var o = null;',
  22654. ' $mod.THelper.Foo.call(this, 1);',
  22655. ' $mod.THelper.Foo.call(this, 1);',
  22656. ' $mod.THelper.Foo.call(this, 2);',
  22657. ' $mod.THelper.Foo.call(this, 1);',
  22658. ' $mod.THelper.Foo.call(this, 1);',
  22659. ' $mod.THelper.Foo.call(this, 3);',
  22660. ' $mod.THelper.Foo.call(this, 1);',
  22661. ' $mod.THelper.Foo.call(this, 1);',
  22662. ' $mod.THelper.Foo.call(this, 4);',
  22663. ' $mod.THelper.Foo.call(o, 5);',
  22664. ' };',
  22665. '});',
  22666. 'rtl.createHelper(this, "THelper", null, function () {',
  22667. ' this.Foo = function (w) {',
  22668. ' var Result = 0;',
  22669. ' this.Run(10);',
  22670. ' this.Run(10);',
  22671. ' this.Run(11);',
  22672. ' $mod.THelper.Foo.call(this, 1);',
  22673. ' $mod.THelper.Foo.call(this, 1);',
  22674. ' $mod.THelper.Foo.call(this, 12);',
  22675. ' $mod.THelper.Foo.call(this, 1);',
  22676. ' $mod.THelper.Foo.call(this, 1);',
  22677. ' $mod.THelper.Foo.call(this, 13);',
  22678. ' $mod.THelper.Foo.call(this, 1);',
  22679. ' $mod.THelper.Foo.call(this, 1);',
  22680. ' $mod.THelper.Foo.call(this, 14);',
  22681. ' return Result;',
  22682. ' };',
  22683. '});',
  22684. 'this.Obj = null;',
  22685. '']),
  22686. LinesToStr([ // $mod.$main
  22687. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22688. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22689. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22690. 'var $with = $mod.Obj;',
  22691. '$mod.THelper.Foo.call($with, 1);',
  22692. '$mod.THelper.Foo.call($with, 1);',
  22693. '$mod.THelper.Foo.call($with, 22);',
  22694. '']));
  22695. end;
  22696. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22697. begin
  22698. StartProgram(false);
  22699. Add([
  22700. 'type',
  22701. ' TObject = class',
  22702. ' procedure Run(w: word = 10);',
  22703. ' end;',
  22704. ' THelper = class helper for TObject',
  22705. ' function Foo(w: word = 1): word;',
  22706. ' end;',
  22707. 'procedure TObject.Run(w: word);',
  22708. ' procedure Sub(Self: TObject);',
  22709. ' begin',
  22710. ' Foo;',
  22711. ' Foo();',
  22712. ' Self.Foo;',
  22713. ' Self.Foo();',
  22714. ' with Self do begin',
  22715. ' Foo;',
  22716. ' Foo();',
  22717. ' end;',
  22718. ' end;',
  22719. 'begin',
  22720. 'end;',
  22721. 'function THelper.foo(w: word): word;',
  22722. ' procedure Sub(Self: TObject);',
  22723. ' begin',
  22724. ' Run;',
  22725. ' Run();',
  22726. ' Foo;',
  22727. ' Foo();',
  22728. ' Self.Foo;',
  22729. ' Self.Foo();',
  22730. ' with Self do begin',
  22731. ' Foo;',
  22732. ' Foo();',
  22733. ' end;',
  22734. ' end;',
  22735. 'begin',
  22736. 'end;',
  22737. 'begin',
  22738. '']);
  22739. ConvertProgram;
  22740. CheckSource('TestClassHelper_Method_Nested_Call',
  22741. LinesToStr([ // statements
  22742. 'rtl.createClass(this, "TObject", null, function () {',
  22743. ' this.$init = function () {',
  22744. ' };',
  22745. ' this.$final = function () {',
  22746. ' };',
  22747. ' this.Run = function (w) {',
  22748. ' var $Self = this;',
  22749. ' function Sub(Self) {',
  22750. ' $mod.THelper.Foo.call($Self, 1);',
  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. ' };',
  22757. ' };',
  22758. '});',
  22759. 'rtl.createHelper(this, "THelper", null, function () {',
  22760. ' this.Foo = function (w) {',
  22761. ' var $Self = this;',
  22762. ' var Result = 0;',
  22763. ' function Sub(Self) {',
  22764. ' $Self.Run(10);',
  22765. ' $Self.Run(10);',
  22766. ' $mod.THelper.Foo.call($Self, 1);',
  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. ' };',
  22773. ' return Result;',
  22774. ' };',
  22775. '});',
  22776. '']),
  22777. LinesToStr([ // $mod.$main
  22778. '']));
  22779. end;
  22780. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22781. begin
  22782. StartProgram(false);
  22783. Add([
  22784. 'type',
  22785. ' TObject = class',
  22786. ' class procedure Run(w: word = 10);',
  22787. ' end;',
  22788. ' THelper = class helper for TObject',
  22789. ' class function Foo(w: word = 1): word;',
  22790. ' end;',
  22791. 'class procedure TObject.Run(w: word);',
  22792. 'begin',
  22793. ' Foo;',
  22794. ' Foo();',
  22795. ' Self.Foo;',
  22796. ' Self.Foo();',
  22797. ' with Self do begin',
  22798. ' Foo;',
  22799. ' Foo();',
  22800. ' end;',
  22801. 'end;',
  22802. 'class function THelper.foo(w: word): word;',
  22803. 'begin',
  22804. ' Run;',
  22805. ' Run();',
  22806. ' Foo;',
  22807. ' Foo();',
  22808. ' Self.Foo;',
  22809. ' Self.Foo();',
  22810. ' with Self do begin',
  22811. ' Foo;',
  22812. ' Foo();',
  22813. ' end;',
  22814. 'end;',
  22815. 'var',
  22816. ' Obj: TObject;',
  22817. 'begin',
  22818. ' obj.Foo;',
  22819. ' obj.Foo();',
  22820. ' with obj do begin',
  22821. ' Foo;',
  22822. ' Foo();',
  22823. ' end;',
  22824. ' tobject.Foo;',
  22825. ' tobject.Foo();',
  22826. ' with tobject do begin',
  22827. ' Foo;',
  22828. ' Foo();',
  22829. ' end;',
  22830. '']);
  22831. ConvertProgram;
  22832. CheckSource('TestClassHelper_ClassMethod_Call',
  22833. LinesToStr([ // statements
  22834. 'rtl.createClass(this, "TObject", null, function () {',
  22835. ' this.$init = function () {',
  22836. ' };',
  22837. ' this.$final = function () {',
  22838. ' };',
  22839. ' this.Run = function (w) {',
  22840. ' $mod.THelper.Foo.call(this, 1);',
  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. ' };',
  22847. '});',
  22848. 'rtl.createHelper(this, "THelper", null, function () {',
  22849. ' this.Foo = function (w) {',
  22850. ' var Result = 0;',
  22851. ' this.Run(10);',
  22852. ' this.Run(10);',
  22853. ' $mod.THelper.Foo.call(this, 1);',
  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. ' return Result;',
  22860. ' };',
  22861. '});',
  22862. 'this.Obj = null;',
  22863. '']),
  22864. LinesToStr([ // $mod.$main
  22865. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22866. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22867. 'var $with = $mod.Obj;',
  22868. '$mod.THelper.Foo.call($with.$class, 1);',
  22869. '$mod.THelper.Foo.call($with.$class, 1);',
  22870. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22871. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22872. 'var $with1 = $mod.TObject;',
  22873. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22874. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22875. '']));
  22876. end;
  22877. procedure TTestModule.TestClassHelper_ClassOf;
  22878. begin
  22879. StartProgram(false);
  22880. Add([
  22881. 'type',
  22882. ' TObject = class',
  22883. ' end;',
  22884. ' TClass = class of TObject;',
  22885. ' THelper = class helper for TObject',
  22886. ' class function Foo(w: word = 1): word;',
  22887. ' end;',
  22888. 'class function THelper.foo(w: word): word;',
  22889. 'begin',
  22890. 'end;',
  22891. 'var',
  22892. ' c: TClass;',
  22893. 'begin',
  22894. ' c.Foo;',
  22895. ' c.Foo();',
  22896. ' with c do begin',
  22897. ' Foo;',
  22898. ' Foo();',
  22899. ' end;',
  22900. '']);
  22901. ConvertProgram;
  22902. CheckSource('TestClassHelper_ClassOf',
  22903. LinesToStr([ // statements
  22904. 'rtl.createClass(this, "TObject", null, function () {',
  22905. ' this.$init = function () {',
  22906. ' };',
  22907. ' this.$final = function () {',
  22908. ' };',
  22909. '});',
  22910. 'rtl.createHelper(this, "THelper", null, function () {',
  22911. ' this.Foo = function (w) {',
  22912. ' var Result = 0;',
  22913. ' return Result;',
  22914. ' };',
  22915. '});',
  22916. 'this.c = null;',
  22917. '']),
  22918. LinesToStr([ // $mod.$main
  22919. '$mod.THelper.Foo.call($mod.c, 1);',
  22920. '$mod.THelper.Foo.call($mod.c, 1);',
  22921. 'var $with = $mod.c;',
  22922. '$mod.THelper.Foo.call($with, 1);',
  22923. '$mod.THelper.Foo.call($with, 1);',
  22924. '']));
  22925. end;
  22926. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22927. begin
  22928. StartProgram(false);
  22929. Add([
  22930. '{$mode objfpc}',
  22931. 'type',
  22932. ' TObject = class',
  22933. ' procedure DoIt;',
  22934. ' end;',
  22935. ' THelper = class helper for TObject',
  22936. ' procedure Fly(w: word = 1);',
  22937. ' class procedure Glide(w: word = 1);',
  22938. ' class procedure Run(w: word = 1); static;',
  22939. ' end;',
  22940. ' TFly = procedure(w: word) of object;',
  22941. ' TGlide = TFly;',
  22942. ' TRun = procedure(w: word);',
  22943. 'var',
  22944. ' f: TFly;',
  22945. ' g: TGlide;',
  22946. ' r: TRun;',
  22947. 'procedure TObject.DoIt;',
  22948. 'begin',
  22949. ' f:=@fly;',
  22950. ' g:=@glide;',
  22951. ' r:=@run;',
  22952. ' f:[email protected];',
  22953. ' g:[email protected];',
  22954. ' r:[email protected];',
  22955. ' with self do begin',
  22956. ' f:=@fly;',
  22957. ' g:=@glide;',
  22958. ' r:=@run;',
  22959. ' end;',
  22960. 'end;',
  22961. 'procedure THelper.fly(w: word);',
  22962. 'begin',
  22963. ' f:=@fly;',
  22964. ' g:=@glide;',
  22965. ' r:=@run;',
  22966. 'end;',
  22967. 'class procedure THelper.glide(w: word);',
  22968. 'begin',
  22969. ' g:=@glide;',
  22970. ' r:=@run;',
  22971. 'end;',
  22972. 'class procedure THelper.run(w: word);',
  22973. 'begin',
  22974. ' g:=@glide;',
  22975. ' r:=@run;',
  22976. 'end;',
  22977. 'var',
  22978. ' Obj: TObject;',
  22979. 'begin',
  22980. ' f:[email protected];',
  22981. ' g:[email protected];',
  22982. ' r:[email protected];',
  22983. ' with obj do begin',
  22984. ' f:=@fly;',
  22985. ' g:=@glide;',
  22986. ' r:=@run;',
  22987. ' end;',
  22988. ' g:[email protected];',
  22989. ' r:[email protected];',
  22990. ' with tobject do begin',
  22991. ' g:=@glide;',
  22992. ' r:=@run;',
  22993. ' end;',
  22994. '']);
  22995. ConvertProgram;
  22996. CheckSource('TestClassHelper_MethodRefObjFPC',
  22997. LinesToStr([ // statements
  22998. 'rtl.createClass(this, "TObject", null, function () {',
  22999. ' this.$init = function () {',
  23000. ' };',
  23001. ' this.$final = function () {',
  23002. ' };',
  23003. ' this.DoIt = function () {',
  23004. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23005. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23006. ' $mod.r = $mod.THelper.Run;',
  23007. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23008. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23009. ' $mod.r = $mod.THelper.Run;',
  23010. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23011. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23012. ' $mod.r = $mod.THelper.Run;',
  23013. ' };',
  23014. '});',
  23015. 'rtl.createHelper(this, "THelper", null, function () {',
  23016. ' this.Fly = function (w) {',
  23017. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23018. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23019. ' $mod.r = $mod.THelper.Run;',
  23020. ' };',
  23021. ' this.Glide = function (w) {',
  23022. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23023. ' $mod.r = $mod.THelper.Run;',
  23024. ' };',
  23025. ' this.Run = function (w) {',
  23026. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23027. ' $mod.r = $mod.THelper.Run;',
  23028. ' };',
  23029. '});',
  23030. 'this.f = null;',
  23031. 'this.g = null;',
  23032. 'this.r = null;',
  23033. 'this.Obj = null;',
  23034. '']),
  23035. LinesToStr([ // $mod.$main
  23036. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23037. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23038. '$mod.r = $mod.THelper.Run;',
  23039. 'var $with = $mod.Obj;',
  23040. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23041. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23042. '$mod.r = $mod.THelper.Run;',
  23043. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23044. '$mod.r = $mod.THelper.Run;',
  23045. 'var $with1 = $mod.TObject;',
  23046. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23047. '$mod.r = $mod.THelper.Run;',
  23048. '']));
  23049. end;
  23050. procedure TTestModule.TestClassHelper_Constructor;
  23051. begin
  23052. StartProgram(false);
  23053. Add([
  23054. 'type',
  23055. ' TObject = class',
  23056. ' constructor Create;',
  23057. ' end;',
  23058. ' TClass = class of TObject;',
  23059. ' THelper = class helper for TObject',
  23060. ' constructor NewHlp(w: word);',
  23061. ' end;',
  23062. 'var',
  23063. ' obj: TObject;',
  23064. ' c: TClass;',
  23065. 'constructor TObject.Create;',
  23066. 'begin',
  23067. ' NewHlp(2);', // normal call
  23068. ' tobject.NewHlp(3);', // new instance
  23069. ' c.newhlp(4);', // new instance
  23070. 'end;',
  23071. 'constructor THelper.NewHlp(w: word);',
  23072. 'begin',
  23073. ' create;', // normal call
  23074. ' tobject.create;', // new instance
  23075. ' NewHlp(2);', // normal call
  23076. ' tobject.NewHlp(3);', // new instance
  23077. ' c.newhlp(4);', // new instance
  23078. 'end;',
  23079. 'begin',
  23080. ' obj.newhlp(2);', // normal call
  23081. ' with Obj do newhlp(12);', // normal call
  23082. ' tobject.newhlp(3);', // new instance
  23083. ' with tobject do newhlp(13);', // new instance
  23084. ' c.newhlp(4);', // new instance
  23085. ' with c do newhlp(14);', // new instance
  23086. '']);
  23087. ConvertProgram;
  23088. CheckSource('TestClassHelper_Constructor',
  23089. LinesToStr([ // statements
  23090. 'rtl.createClass(this, "TObject", null, function () {',
  23091. ' this.$init = function () {',
  23092. ' };',
  23093. ' this.$final = function () {',
  23094. ' };',
  23095. ' this.Create = function () {',
  23096. ' $mod.THelper.NewHlp.call(this, 2);',
  23097. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23098. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23099. ' return this;',
  23100. ' };',
  23101. '});',
  23102. 'rtl.createHelper(this, "THelper", null, function () {',
  23103. ' this.NewHlp = function (w) {',
  23104. ' this.Create();',
  23105. ' $mod.TObject.$create("Create");',
  23106. ' $mod.THelper.NewHlp.call(this, 2);',
  23107. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23108. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23109. ' return this;',
  23110. ' };',
  23111. '});',
  23112. 'this.obj = null;',
  23113. 'this.c = null;',
  23114. '']),
  23115. LinesToStr([ // $mod.$main
  23116. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23117. 'var $with = $mod.obj;',
  23118. '$mod.THelper.NewHlp.call($with, 12);',
  23119. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23120. 'var $with1 = $mod.TObject;',
  23121. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23122. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23123. 'var $with2 = $mod.c;',
  23124. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23125. '']));
  23126. end;
  23127. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23128. begin
  23129. StartProgram(false);
  23130. Add([
  23131. 'type',
  23132. ' TObject = class',
  23133. ' procedure Fly;',
  23134. ' end;',
  23135. ' TObjHelper = class helper for TObject',
  23136. ' procedure Fly;',
  23137. ' end;',
  23138. ' TBird = class',
  23139. ' procedure Fly;',
  23140. ' end;',
  23141. ' TBirdHelper = class helper for TBird',
  23142. ' procedure Fly;',
  23143. ' procedure Walk(w: word);',
  23144. ' end;',
  23145. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23146. ' procedure Fly;',
  23147. ' procedure Walk(w: word);',
  23148. ' end;',
  23149. 'procedure Tobject.fly;',
  23150. 'begin',
  23151. ' inherited;', // ignore
  23152. 'end;',
  23153. 'procedure Tobjhelper.fly;',
  23154. 'begin',
  23155. ' {@TObject_Fly}inherited;',
  23156. ' inherited {@TObject_Fly}Fly;',
  23157. 'end;',
  23158. 'procedure Tbird.fly;',
  23159. 'begin',
  23160. ' {@TObjHelper_Fly}inherited;',
  23161. ' inherited {@TObjHelper_Fly}Fly;',
  23162. 'end;',
  23163. 'procedure Tbirdhelper.fly;',
  23164. 'begin',
  23165. ' {@TBird_Fly}inherited;',
  23166. ' inherited {@TBird_Fly}Fly;',
  23167. 'end;',
  23168. 'procedure Tbirdhelper.walk(w: word);',
  23169. 'begin',
  23170. 'end;',
  23171. 'procedure teagleHelper.fly;',
  23172. 'begin',
  23173. ' {@TBird_Fly}inherited;',
  23174. ' inherited {@TBird_Fly}Fly;',
  23175. 'end;',
  23176. 'procedure teagleHelper.walk(w: word);',
  23177. 'begin',
  23178. ' {@TBirdHelper_Walk}inherited;',
  23179. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23180. 'end;',
  23181. 'begin',
  23182. '']);
  23183. ConvertProgram;
  23184. CheckSource('TestClassHelper_InheritedObjFPC',
  23185. LinesToStr([ // statements
  23186. 'rtl.createClass(this, "TObject", null, function () {',
  23187. ' this.$init = function () {',
  23188. ' };',
  23189. ' this.$final = function () {',
  23190. ' };',
  23191. ' this.Fly = function () {',
  23192. ' };',
  23193. '});',
  23194. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23195. ' this.Fly = function () {',
  23196. ' $mod.TObject.Fly.call(this);',
  23197. ' $mod.TObject.Fly.call(this);',
  23198. ' };',
  23199. '});',
  23200. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23201. ' this.Fly$1 = function () {',
  23202. ' $mod.TObjHelper.Fly.call(this);',
  23203. ' $mod.TObjHelper.Fly.call(this);',
  23204. ' };',
  23205. '});',
  23206. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23207. ' this.Fly = function () {',
  23208. ' $mod.TBird.Fly$1.call(this);',
  23209. ' $mod.TBird.Fly$1.call(this);',
  23210. ' };',
  23211. ' this.Walk = function (w) {',
  23212. ' };',
  23213. '});',
  23214. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23215. ' this.Fly$1 = function () {',
  23216. ' $mod.TBird.Fly$1.call(this);',
  23217. ' $mod.TBird.Fly$1.call(this);',
  23218. ' };',
  23219. ' this.Walk$1 = function (w) {',
  23220. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  23221. ' $mod.TBirdHelper.Walk.call(this, 3);',
  23222. ' };',
  23223. '});',
  23224. '']),
  23225. LinesToStr([ // $mod.$main
  23226. '']));
  23227. end;
  23228. procedure TTestModule.TestClassHelper_Property;
  23229. begin
  23230. StartProgram(false);
  23231. Add([
  23232. 'type',
  23233. ' TObject = class',
  23234. ' FSize: word;',
  23235. ' function GetSpeed: word;',
  23236. ' procedure SetSpeed(Value: word);',
  23237. ' end;',
  23238. ' TObjHelper = class helper for TObject',
  23239. ' function GetLeft: word;',
  23240. ' procedure SetLeft(Value: word);',
  23241. ' property Size: word read FSize write FSize;',
  23242. ' property Speed: word read GetSpeed write SetSpeed;',
  23243. ' property Left: word read GetLeft write SetLeft;',
  23244. ' end;',
  23245. ' TBird = class',
  23246. ' property NotRight: word read GetLeft write SetLeft;',
  23247. ' procedure DoIt;',
  23248. ' end;',
  23249. 'var',
  23250. ' b: TBird;',
  23251. 'function Tobject.GetSpeed: word;',
  23252. 'begin',
  23253. ' Size:=Size+11;',
  23254. ' Speed:=Speed+12;',
  23255. ' Result:=Left+13;',
  23256. ' Left:=13;',
  23257. ' Left:=Left+13;',
  23258. ' Self.Size:=Self.Size+21;',
  23259. ' Self.Speed:=Self.Speed+22;',
  23260. ' Self.Left:=Self.Left+23;',
  23261. ' with Self do begin',
  23262. ' Size:=Size+31;',
  23263. ' Speed:=Speed+32;',
  23264. ' Left:=Left+33;',
  23265. ' end;',
  23266. 'end;',
  23267. 'procedure Tobject.SetSpeed(Value: word);',
  23268. 'begin',
  23269. 'end;',
  23270. 'function TObjHelper.GetLeft: word;',
  23271. 'begin',
  23272. ' Size:=Size+11;',
  23273. ' Speed:=Speed+12;',
  23274. ' Left:=Left+13;',
  23275. ' Self.Size:=Self.Size+21;',
  23276. ' Self.Speed:=Self.Speed+22;',
  23277. ' Self.Left:=Self.Left+23;',
  23278. ' with Self do begin',
  23279. ' Size:=Size+31;',
  23280. ' Speed:=Speed+32;',
  23281. ' Left:=Left+33;',
  23282. ' end;',
  23283. 'end;',
  23284. 'procedure TObjHelper.SetLeft(Value: word);',
  23285. 'begin',
  23286. 'end;',
  23287. 'procedure TBird.DoIt;',
  23288. 'begin',
  23289. ' NotRight:=NotRight+11;',
  23290. ' Self.NotRight:=Self.NotRight+21;',
  23291. ' with Self do begin',
  23292. ' NotRight:=NotRight+31;',
  23293. ' end;',
  23294. 'end;',
  23295. 'begin',
  23296. ' b.Size:=b.Size+11;',
  23297. ' b.Speed:=b.Speed+12;',
  23298. ' b.Left:=b.Left+13;',
  23299. ' b.NotRight:=b.NotRight+14;',
  23300. ' with b do begin',
  23301. ' Size:=Size+31;',
  23302. ' Speed:=Speed+32;',
  23303. ' Left:=Left+33;',
  23304. ' NotRight:=NotRight+34;',
  23305. ' end;',
  23306. '']);
  23307. ConvertProgram;
  23308. CheckSource('TestClassHelper_Property',
  23309. LinesToStr([ // statements
  23310. 'rtl.createClass(this, "TObject", null, function () {',
  23311. ' this.$init = function () {',
  23312. ' this.FSize = 0;',
  23313. ' };',
  23314. ' this.$final = function () {',
  23315. ' };',
  23316. ' this.GetSpeed = function () {',
  23317. ' var Result = 0;',
  23318. ' this.FSize = this.FSize + 11;',
  23319. ' this.SetSpeed(this.GetSpeed() + 12);',
  23320. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  23321. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  23322. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23323. ' this.FSize = this.FSize + 21;',
  23324. ' this.SetSpeed(this.GetSpeed() + 22);',
  23325. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23326. ' this.FSize = this.FSize + 31;',
  23327. ' this.SetSpeed(this.GetSpeed() + 32);',
  23328. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23329. ' return Result;',
  23330. ' };',
  23331. ' this.SetSpeed = function (Value) {',
  23332. ' };',
  23333. '});',
  23334. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23335. ' this.GetLeft = function () {',
  23336. ' var Result = 0;',
  23337. ' this.FSize = this.FSize + 11;',
  23338. ' this.SetSpeed(this.GetSpeed() + 12);',
  23339. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23340. ' this.FSize = this.FSize + 21;',
  23341. ' this.SetSpeed(this.GetSpeed() + 22);',
  23342. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23343. ' this.FSize = this.FSize + 31;',
  23344. ' this.SetSpeed(this.GetSpeed() + 32);',
  23345. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23346. ' return Result;',
  23347. ' };',
  23348. ' this.SetLeft = function (Value) {',
  23349. ' };',
  23350. '});',
  23351. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23352. ' this.DoIt = function () {',
  23353. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23354. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23355. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23356. ' };',
  23357. '});',
  23358. 'this.b = null;',
  23359. '']),
  23360. LinesToStr([ // $mod.$main
  23361. '$mod.b.FSize = $mod.b.FSize + 11;',
  23362. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23363. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23364. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23365. 'var $with = $mod.b;',
  23366. '$with.FSize = $with.FSize + 31;',
  23367. '$with.SetSpeed($with.GetSpeed() + 32);',
  23368. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23369. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23370. '']));
  23371. end;
  23372. procedure TTestModule.TestClassHelper_Property_Array;
  23373. begin
  23374. StartProgram(false);
  23375. Add([
  23376. 'type',
  23377. ' TObject = class',
  23378. ' function GetSpeed(Index: boolean): word;',
  23379. ' procedure SetSpeed(Index: boolean; Value: word);',
  23380. ' end;',
  23381. ' TObjHelper = class helper for TObject',
  23382. ' function GetSize(Index: boolean): word;',
  23383. ' procedure SetSize(Index: boolean; Value: word);',
  23384. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23385. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23386. ' end;',
  23387. ' TBird = class',
  23388. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23389. ' procedure DoIt;',
  23390. ' end;',
  23391. 'var',
  23392. ' b: TBird;',
  23393. 'function Tobject.GetSpeed(Index: boolean): word;',
  23394. 'begin',
  23395. ' Result:=Size[false];',
  23396. ' Size[true]:=Size[false]+11;',
  23397. ' Speed[true]:=Speed[false]+12;',
  23398. ' Self.Size[true]:=Self.Size[false]+21;',
  23399. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23400. ' with Self do begin',
  23401. ' Size[true]:=Size[false]+31;',
  23402. ' Speed[true]:=Speed[false]+32;',
  23403. ' end;',
  23404. 'end;',
  23405. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23406. 'begin',
  23407. 'end;',
  23408. 'function TObjHelper.GetSize(Index: boolean): word;',
  23409. 'begin',
  23410. ' Size[true]:=Size[false]+11;',
  23411. ' Speed[true]:=Speed[false]+12;',
  23412. ' Self.Size[true]:=Self.Size[false]+21;',
  23413. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23414. ' with Self do begin',
  23415. ' Size[true]:=Size[false]+31;',
  23416. ' Speed[true]:=Speed[false]+32;',
  23417. ' end;',
  23418. 'end;',
  23419. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23420. 'begin',
  23421. 'end;',
  23422. 'procedure TBird.DoIt;',
  23423. 'begin',
  23424. ' Items[true]:=Items[false]+11;',
  23425. ' Self.Items[true]:=Self.Items[false]+21;',
  23426. ' with Self do Items[true]:=Items[false]+31;',
  23427. 'end;',
  23428. 'begin',
  23429. ' b.Size[true]:=b.Size[false]+11;',
  23430. ' b.Speed[true]:=b.Speed[false]+12;',
  23431. ' b.Items[true]:=b.Items[false]+13;',
  23432. ' with b do begin',
  23433. ' Size[true]:=Size[false]+21;',
  23434. ' Speed[true]:=Speed[false]+22;',
  23435. ' Items[true]:=Items[false]+23;',
  23436. ' end;',
  23437. '']);
  23438. ConvertProgram;
  23439. CheckSource('TestClassHelper_Property_Array',
  23440. LinesToStr([ // statements
  23441. 'rtl.createClass(this, "TObject", null, function () {',
  23442. ' this.$init = function () {',
  23443. ' };',
  23444. ' this.$final = function () {',
  23445. ' };',
  23446. ' this.GetSpeed = function (Index) {',
  23447. ' var Result = 0;',
  23448. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23449. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23450. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23451. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23452. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23453. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23454. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23455. ' return Result;',
  23456. ' };',
  23457. ' this.SetSpeed = function (Index, Value) {',
  23458. ' };',
  23459. '});',
  23460. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23461. ' this.GetSize = function (Index) {',
  23462. ' var Result = 0;',
  23463. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23464. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23465. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23466. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23467. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23468. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23469. ' return Result;',
  23470. ' };',
  23471. ' this.SetSize = function (Index, Value) {',
  23472. ' };',
  23473. '});',
  23474. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23475. ' this.DoIt = function () {',
  23476. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23477. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23478. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23479. ' };',
  23480. '});',
  23481. 'this.b = null;',
  23482. '']),
  23483. LinesToStr([ // $mod.$main
  23484. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23485. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23486. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23487. 'var $with = $mod.b;',
  23488. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23489. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23490. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23491. '']));
  23492. end;
  23493. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23494. begin
  23495. StartProgram(false);
  23496. Add([
  23497. 'type',
  23498. ' TObject = class',
  23499. ' function GetSpeed(Index: boolean): word;',
  23500. ' procedure SetSpeed(Index: boolean; Value: word);',
  23501. ' end;',
  23502. ' TObjHelper = class helper for TObject',
  23503. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23504. ' end;',
  23505. ' TBird = class',
  23506. ' end;',
  23507. ' TBirdHelper = class helper for TBird',
  23508. ' function GetSize(Index: word): boolean;',
  23509. ' procedure SetSize(Index: word; Value: boolean);',
  23510. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23511. ' end;',
  23512. 'function Tobject.GetSpeed(Index: boolean): word;',
  23513. 'begin',
  23514. ' Self[true]:=Self[false]+1;',
  23515. 'end;',
  23516. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23517. 'begin',
  23518. 'end;',
  23519. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23520. 'begin',
  23521. ' Self[1]:=not Self[2];',
  23522. 'end;',
  23523. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23524. 'begin',
  23525. 'end;',
  23526. 'var',
  23527. ' o: TObject;',
  23528. ' b: TBird;',
  23529. 'begin',
  23530. ' o[true]:=o[false]+1;',
  23531. ' b[3]:=not b[4];',
  23532. '']);
  23533. ConvertProgram;
  23534. CheckSource('TestClassHelper_Property_Array_Default',
  23535. LinesToStr([ // statements
  23536. 'rtl.createClass(this, "TObject", null, function () {',
  23537. ' this.$init = function () {',
  23538. ' };',
  23539. ' this.$final = function () {',
  23540. ' };',
  23541. ' this.GetSpeed = function (Index) {',
  23542. ' var Result = 0;',
  23543. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23544. ' return Result;',
  23545. ' };',
  23546. ' this.SetSpeed = function (Index, Value) {',
  23547. ' };',
  23548. '});',
  23549. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23550. '});',
  23551. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23552. '});',
  23553. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23554. ' this.GetSize = function (Index) {',
  23555. ' var Result = false;',
  23556. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23557. ' return Result;',
  23558. ' };',
  23559. ' this.SetSize = function (Index, Value) {',
  23560. ' };',
  23561. '});',
  23562. 'this.o = null;',
  23563. 'this.b = null;',
  23564. '']),
  23565. LinesToStr([ // $mod.$main
  23566. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23567. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23568. '']));
  23569. end;
  23570. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23571. begin
  23572. StartProgram(false);
  23573. Add([
  23574. 'type',
  23575. ' TObject = class',
  23576. ' end;',
  23577. ' TObjHelper = class helper for TObject',
  23578. ' function GetItems(Index: word): TObject;',
  23579. ' procedure SetItems(Index: word; Value: TObject);',
  23580. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23581. ' end;',
  23582. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23583. 'begin',
  23584. ' Self[1][2]:=Self[3][4];',
  23585. 'end;',
  23586. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23587. 'begin',
  23588. 'end;',
  23589. 'var',
  23590. ' o: TObject;',
  23591. 'begin',
  23592. ' o[1][2]:=o[3][4];',
  23593. '']);
  23594. ConvertProgram;
  23595. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23596. LinesToStr([ // statements
  23597. 'rtl.createClass(this, "TObject", null, function () {',
  23598. ' this.$init = function () {',
  23599. ' };',
  23600. ' this.$final = function () {',
  23601. ' };',
  23602. '});',
  23603. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23604. ' this.GetItems = function (Index) {',
  23605. ' var Result = null;',
  23606. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23607. ' return Result;',
  23608. ' };',
  23609. ' this.SetItems = function (Index, Value) {',
  23610. ' };',
  23611. '});',
  23612. 'this.o = null;',
  23613. '']),
  23614. LinesToStr([ // $mod.$main
  23615. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23616. '']));
  23617. end;
  23618. procedure TTestModule.TestClassHelper_ClassProperty;
  23619. begin
  23620. StartProgram(false);
  23621. Add([
  23622. 'type',
  23623. ' TObject = class',
  23624. ' class var FSize: word;',
  23625. ' class function GetSpeed: word;',
  23626. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23627. ' end;',
  23628. ' TObjHelper = class helper for TObject',
  23629. ' class function GetLeft: word;',
  23630. ' class procedure SetLeft(Value: word);',
  23631. ' class property Size: word read FSize write FSize;',
  23632. ' class property Speed: word read GetSpeed write SetSpeed;',
  23633. ' class property Left: word read GetLeft write SetLeft;',
  23634. ' end;',
  23635. ' TBird = class',
  23636. ' class property NotRight: word read GetLeft write SetLeft;',
  23637. ' class procedure DoIt;',
  23638. ' end;',
  23639. ' TBirdClass = class of TBird;',
  23640. 'class function Tobject.GetSpeed: word;',
  23641. 'begin',
  23642. ' Size:=Size+11;',
  23643. ' Speed:=Speed+12;',
  23644. ' Left:=Left+13;',
  23645. ' Self.Size:=Self.Size+21;',
  23646. ' Self.Speed:=Self.Speed+22;',
  23647. ' Self.Left:=Self.Left+23;',
  23648. ' with Self do begin',
  23649. ' Size:=Size+31;',
  23650. ' Speed:=Speed+32;',
  23651. ' Left:=Left+33;',
  23652. ' end;',
  23653. 'end;',
  23654. 'class function TObjHelper.GetLeft: word;',
  23655. 'begin',
  23656. ' Size:=Size+11;',
  23657. ' Speed:=Speed+12;',
  23658. ' Left:=Left+13;',
  23659. ' Self.Size:=Self.Size+21;',
  23660. ' Self.Speed:=Self.Speed+22;',
  23661. ' Self.Left:=Self.Left+23;',
  23662. ' with Self do begin',
  23663. ' Size:=Size+31;',
  23664. ' Speed:=Speed+32;',
  23665. ' Left:=Left+33;',
  23666. ' end;',
  23667. 'end;',
  23668. 'class procedure TObjHelper.SetLeft(Value: word);',
  23669. 'begin',
  23670. 'end;',
  23671. 'class procedure TBird.DoIt;',
  23672. 'begin',
  23673. ' NotRight:=NotRight+11;',
  23674. ' Self.NotRight:=Self.NotRight+21;',
  23675. ' with Self do NotRight:=NotRight+31;',
  23676. 'end;',
  23677. 'var',
  23678. ' b: TBird;',
  23679. ' c: TBirdClass;',
  23680. 'begin',
  23681. ' b.Size:=b.Size+11;',
  23682. ' b.Speed:=b.Speed+12;',
  23683. ' b.Left:=b.Left+13;',
  23684. ' b.NotRight:=b.NotRight+14;',
  23685. ' with b do begin',
  23686. ' Size:=Size+31;',
  23687. ' Speed:=Speed+32;',
  23688. ' Left:=Left+33;',
  23689. ' NotRight:=NotRight+34;',
  23690. ' end;',
  23691. ' c.Size:=c.Size+11;',
  23692. ' c.Speed:=c.Speed+12;',
  23693. ' c.Left:=c.Left+13;',
  23694. ' c.NotRight:=c.NotRight+14;',
  23695. ' with c do begin',
  23696. ' Size:=Size+31;',
  23697. ' Speed:=Speed+32;',
  23698. ' Left:=Left+33;',
  23699. ' NotRight:=NotRight+34;',
  23700. ' end;',
  23701. ' tbird.Size:=tbird.Size+11;',
  23702. ' tbird.Speed:=tbird.Speed+12;',
  23703. ' tbird.Left:=tbird.Left+13;',
  23704. ' tbird.NotRight:=tbird.NotRight+14;',
  23705. ' with tbird do begin',
  23706. ' Size:=Size+31;',
  23707. ' Speed:=Speed+32;',
  23708. ' Left:=Left+33;',
  23709. ' NotRight:=NotRight+34;',
  23710. ' end;',
  23711. '']);
  23712. ConvertProgram;
  23713. CheckSource('TestClassHelper_ClassProperty',
  23714. LinesToStr([ // statements
  23715. 'rtl.createClass(this, "TObject", null, function () {',
  23716. ' this.FSize = 0;',
  23717. ' this.$init = function () {',
  23718. ' };',
  23719. ' this.$final = function () {',
  23720. ' };',
  23721. ' this.GetSpeed = function () {',
  23722. ' var Result = 0;',
  23723. ' $mod.TObject.FSize = this.FSize + 11;',
  23724. ' this.SetSpeed(this.GetSpeed() + 12);',
  23725. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23726. ' $mod.TObject.FSize = this.FSize + 21;',
  23727. ' this.SetSpeed(this.GetSpeed() + 22);',
  23728. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23729. ' $mod.TObject.FSize = this.FSize + 31;',
  23730. ' this.SetSpeed(this.GetSpeed() + 32);',
  23731. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23732. ' return Result;',
  23733. ' };',
  23734. '});',
  23735. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23736. ' this.GetLeft = function () {',
  23737. ' var Result = 0;',
  23738. ' $mod.TObject.FSize = this.FSize + 11;',
  23739. ' this.SetSpeed(this.GetSpeed() + 12);',
  23740. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23741. ' $mod.TObject.FSize = this.FSize + 21;',
  23742. ' this.SetSpeed(this.GetSpeed() + 22);',
  23743. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23744. ' $mod.TObject.FSize = this.FSize + 31;',
  23745. ' this.SetSpeed(this.GetSpeed() + 32);',
  23746. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23747. ' return Result;',
  23748. ' };',
  23749. ' this.SetLeft = function (Value) {',
  23750. ' };',
  23751. '});',
  23752. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23753. ' this.DoIt = function () {',
  23754. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23755. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23756. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23757. ' };',
  23758. '});',
  23759. 'this.b = null;',
  23760. 'this.c = null;',
  23761. '']),
  23762. LinesToStr([ // $mod.$main
  23763. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23764. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23765. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23766. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23767. 'var $with = $mod.b;',
  23768. '$mod.TObject.FSize = $with.FSize + 31;',
  23769. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23770. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23771. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23772. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23773. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23774. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23775. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23776. 'var $with1 = $mod.c;',
  23777. '$mod.TObject.FSize = $with1.FSize + 31;',
  23778. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23779. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23780. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23781. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23782. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23783. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23784. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23785. 'var $with2 = $mod.TBird;',
  23786. '$mod.TObject.FSize = $with2.FSize + 31;',
  23787. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23788. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23789. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23790. '']));
  23791. end;
  23792. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23793. begin
  23794. StartProgram(false);
  23795. Add([
  23796. 'type',
  23797. ' TObject = class',
  23798. ' class function GetSpeed: word; static;',
  23799. ' class procedure SetSpeed(Value: word); static;',
  23800. ' end;',
  23801. ' TObjHelper = class helper for TObject',
  23802. ' class function GetLeft: word; static;',
  23803. ' class procedure SetLeft(Value: word); static;',
  23804. ' class property Speed: word read GetSpeed write SetSpeed;',
  23805. ' class property Left: word read GetLeft write SetLeft;',
  23806. ' end;',
  23807. ' TBird = class',
  23808. ' class property NotRight: word read GetLeft write SetLeft;',
  23809. ' class procedure DoIt; static;',
  23810. ' class procedure DoSome;',
  23811. ' end;',
  23812. ' TBirdClass = class of TBird;',
  23813. 'class function Tobject.GetSpeed: word;',
  23814. 'begin',
  23815. ' Speed:=Speed+12;',
  23816. ' Left:=Left+13;',
  23817. 'end;',
  23818. 'class procedure TObject.SetSpeed(Value: word);',
  23819. 'begin',
  23820. 'end;',
  23821. 'class function TObjHelper.GetLeft: word;',
  23822. 'begin',
  23823. ' Speed:=Speed+12;',
  23824. ' Left:=Left+13;',
  23825. 'end;',
  23826. 'class procedure TObjHelper.SetLeft(Value: word);',
  23827. 'begin',
  23828. 'end;',
  23829. 'class procedure TBird.DoIt;',
  23830. 'begin',
  23831. ' NotRight:=NotRight+11;',
  23832. 'end;',
  23833. 'class procedure TBird.DoSome;',
  23834. 'begin',
  23835. ' Speed:=Speed+12;',
  23836. ' Left:=Left+13;',
  23837. ' Self.Speed:=Self.Speed+22;',
  23838. ' Self.Left:=Self.Left+23;',
  23839. ' with Self do begin',
  23840. ' Speed:=Speed+32;',
  23841. ' Left:=Left+33;',
  23842. ' end;',
  23843. ' NotRight:=NotRight+11;',
  23844. ' Self.NotRight:=Self.NotRight+21;',
  23845. ' with Self do NotRight:=NotRight+31;',
  23846. 'end;',
  23847. 'var',
  23848. ' b: TBird;',
  23849. ' c: TBirdClass;',
  23850. 'begin',
  23851. ' b.Speed:=b.Speed+12;',
  23852. ' b.Left:=b.Left+13;',
  23853. ' b.NotRight:=b.NotRight+14;',
  23854. ' with b do begin',
  23855. ' Speed:=Speed+32;',
  23856. ' Left:=Left+33;',
  23857. ' NotRight:=NotRight+34;',
  23858. ' end;',
  23859. ' c.Speed:=c.Speed+12;',
  23860. ' c.Left:=c.Left+13;',
  23861. ' c.NotRight:=c.NotRight+14;',
  23862. ' with c do begin',
  23863. ' Speed:=Speed+32;',
  23864. ' Left:=Left+33;',
  23865. ' NotRight:=NotRight+34;',
  23866. ' end;',
  23867. ' tbird.Speed:=tbird.Speed+12;',
  23868. ' tbird.Left:=tbird.Left+13;',
  23869. ' tbird.NotRight:=tbird.NotRight+14;',
  23870. ' with tbird do begin',
  23871. ' Speed:=Speed+32;',
  23872. ' Left:=Left+33;',
  23873. ' NotRight:=NotRight+34;',
  23874. ' end;',
  23875. '']);
  23876. ConvertProgram;
  23877. CheckSource('TestClassHelper_ClassPropertyStatic',
  23878. LinesToStr([ // statements
  23879. 'rtl.createClass(this, "TObject", null, function () {',
  23880. ' this.$init = function () {',
  23881. ' };',
  23882. ' this.$final = function () {',
  23883. ' };',
  23884. ' this.GetSpeed = function () {',
  23885. ' var Result = 0;',
  23886. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23887. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23888. ' return Result;',
  23889. ' };',
  23890. ' this.SetSpeed = function (Value) {',
  23891. ' };',
  23892. '});',
  23893. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23894. ' this.GetLeft = function () {',
  23895. ' var Result = 0;',
  23896. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23897. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23898. ' return Result;',
  23899. ' };',
  23900. ' this.SetLeft = function (Value) {',
  23901. ' };',
  23902. '});',
  23903. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23904. ' this.DoIt = function () {',
  23905. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23906. ' };',
  23907. ' this.DoSome = function () {',
  23908. ' this.SetSpeed(this.GetSpeed() + 12);',
  23909. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23910. ' this.SetSpeed(this.GetSpeed() + 22);',
  23911. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23912. ' this.SetSpeed(this.GetSpeed() + 32);',
  23913. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23914. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23915. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23916. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23917. ' };',
  23918. '});',
  23919. 'this.b = null;',
  23920. 'this.c = null;',
  23921. '']),
  23922. LinesToStr([ // $mod.$main
  23923. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23924. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23925. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23926. 'var $with = $mod.b;',
  23927. '$with.SetSpeed($with.GetSpeed() + 32);',
  23928. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23929. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23930. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23931. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23932. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23933. 'var $with1 = $mod.c;',
  23934. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23935. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23936. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23937. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23938. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23939. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23940. 'var $with2 = $mod.TBird;',
  23941. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23942. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23943. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23944. '']));
  23945. end;
  23946. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  23947. begin
  23948. StartProgram(false);
  23949. Add([
  23950. 'type',
  23951. ' TObject = class',
  23952. ' class function GetSpeed(Index: boolean): word;',
  23953. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  23954. ' end;',
  23955. ' TObjHelper = class helper for TObject',
  23956. ' class function GetSize(Index: boolean): word;',
  23957. ' class procedure SetSize(Index: boolean; Value: word);',
  23958. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  23959. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23960. ' end;',
  23961. ' TBird = class',
  23962. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  23963. ' class procedure DoIt;',
  23964. ' end;',
  23965. ' TBirdClass = class of TBird;',
  23966. 'class function Tobject.GetSpeed(Index: boolean): word;',
  23967. 'begin',
  23968. ' Size[true]:=Size[false]+11;',
  23969. ' Speed[true]:=Speed[false]+12;',
  23970. ' Self.Size[true]:=Self.Size[false]+21;',
  23971. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23972. ' with Self do begin',
  23973. ' Size[true]:=Size[false]+31;',
  23974. ' Speed[true]:=Speed[false]+32;',
  23975. ' end;',
  23976. 'end;',
  23977. 'class function TObjHelper.GetSize(Index: boolean): word;',
  23978. 'begin',
  23979. ' Size[true]:=Size[false]+11;',
  23980. ' Speed[true]:=Speed[false]+12;',
  23981. ' Self.Size[true]:=Self.Size[false]+21;',
  23982. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23983. ' with Self do begin',
  23984. ' Size[true]:=Size[false]+31;',
  23985. ' Speed[true]:=Speed[false]+32;',
  23986. ' end;',
  23987. 'end;',
  23988. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23989. 'begin',
  23990. 'end;',
  23991. 'class procedure TBird.DoIt;',
  23992. 'begin',
  23993. ' Items[true]:=Items[false]+11;',
  23994. ' Self.Items[true]:=Self.Items[false]+21;',
  23995. ' with Self do Items[true]:=Items[false]+31;',
  23996. 'end;',
  23997. 'var',
  23998. ' b: TBird;',
  23999. ' c: TBirdClass;',
  24000. 'begin',
  24001. ' b.Size[true]:=b.Size[false]+11;',
  24002. ' b.Speed[true]:=b.Speed[false]+12;',
  24003. ' b.Items[true]:=b.Items[false]+13;',
  24004. ' with b do begin',
  24005. ' Size[true]:=Size[false]+21;',
  24006. ' Speed[true]:=Speed[false]+22;',
  24007. ' Items[true]:=Items[false]+23;',
  24008. ' end;',
  24009. ' c.Size[true]:=c.Size[false]+11;',
  24010. ' c.Speed[true]:=c.Speed[false]+12;',
  24011. ' c.Items[true]:=c.Items[false]+13;',
  24012. ' with c do begin',
  24013. ' Size[true]:=Size[false]+21;',
  24014. ' Speed[true]:=Speed[false]+22;',
  24015. ' Items[true]:=Items[false]+23;',
  24016. ' end;',
  24017. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24018. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24019. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24020. ' with TBird do begin',
  24021. ' Size[true]:=Size[false]+21;',
  24022. ' Speed[true]:=Speed[false]+22;',
  24023. ' Items[true]:=Items[false]+23;',
  24024. ' end;',
  24025. '']);
  24026. ConvertProgram;
  24027. CheckSource('TestClassHelper_ClassProperty_Array',
  24028. LinesToStr([ // statements
  24029. 'rtl.createClass(this, "TObject", null, function () {',
  24030. ' this.$init = function () {',
  24031. ' };',
  24032. ' this.$final = function () {',
  24033. ' };',
  24034. ' this.GetSpeed = function (Index) {',
  24035. ' var Result = 0;',
  24036. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24037. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24038. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24039. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24040. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24041. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24042. ' return Result;',
  24043. ' };',
  24044. '});',
  24045. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24046. ' this.GetSize = function (Index) {',
  24047. ' var Result = 0;',
  24048. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24049. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24050. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24051. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24052. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24053. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24054. ' return Result;',
  24055. ' };',
  24056. ' this.SetSize = function (Index, Value) {',
  24057. ' };',
  24058. '});',
  24059. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24060. ' this.DoIt = function () {',
  24061. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24062. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24063. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24064. ' };',
  24065. '});',
  24066. 'this.b = null;',
  24067. 'this.c = null;',
  24068. '']),
  24069. LinesToStr([ // $mod.$main
  24070. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24071. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24072. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24073. 'var $with = $mod.b;',
  24074. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24075. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24076. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24077. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24078. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24079. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24080. 'var $with1 = $mod.c;',
  24081. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24082. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24083. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24084. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24085. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24086. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24087. 'var $with2 = $mod.TBird;',
  24088. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24089. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24090. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24091. '']));
  24092. end;
  24093. procedure TTestModule.TestClassHelper_ForIn;
  24094. begin
  24095. StartProgram(false);
  24096. Add([
  24097. 'type',
  24098. ' TObject = class end;',
  24099. ' TItem = TObject;',
  24100. ' TEnumerator = class',
  24101. ' FCurrent: TItem;',
  24102. ' property Current: TItem read FCurrent;',
  24103. ' function MoveNext: boolean;',
  24104. ' end;',
  24105. ' TBird = class',
  24106. ' end;',
  24107. ' TBirdHelper = class helper for TBird',
  24108. ' function GetEnumerator: TEnumerator;',
  24109. ' end;',
  24110. 'function TEnumerator.MoveNext: boolean;',
  24111. 'begin',
  24112. 'end;',
  24113. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24114. 'begin',
  24115. 'end;',
  24116. 'var',
  24117. ' b: TBird;',
  24118. ' i, i2: TItem;',
  24119. 'begin',
  24120. ' for i in b do i2:=i;']);
  24121. ConvertProgram;
  24122. CheckSource('TestClassHelper_ForIn',
  24123. LinesToStr([ // statements
  24124. 'rtl.createClass(this, "TObject", null, function () {',
  24125. ' this.$init = function () {',
  24126. ' };',
  24127. ' this.$final = function () {',
  24128. ' };',
  24129. '});',
  24130. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24131. ' this.$init = function () {',
  24132. ' $mod.TObject.$init.call(this);',
  24133. ' this.FCurrent = null;',
  24134. ' };',
  24135. ' this.$final = function () {',
  24136. ' this.FCurrent = undefined;',
  24137. ' $mod.TObject.$final.call(this);',
  24138. ' };',
  24139. ' this.MoveNext = function () {',
  24140. ' var Result = false;',
  24141. ' return Result;',
  24142. ' };',
  24143. '});',
  24144. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24145. '});',
  24146. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24147. ' this.GetEnumerator = function () {',
  24148. ' var Result = null;',
  24149. ' return Result;',
  24150. ' };',
  24151. '});',
  24152. 'this.b = null;',
  24153. 'this.i = null;',
  24154. 'this.i2 = null;'
  24155. ]),
  24156. LinesToStr([ // $mod.$main
  24157. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24158. 'try {',
  24159. ' while ($in.MoveNext()){',
  24160. ' $mod.i = $in.FCurrent;',
  24161. ' $mod.i2 = $mod.i;',
  24162. ' }',
  24163. '} finally {',
  24164. ' $in = rtl.freeLoc($in)',
  24165. '};',
  24166. '']));
  24167. end;
  24168. procedure TTestModule.TestClassHelper_PassProperty;
  24169. begin
  24170. StartProgram(false);
  24171. Add([
  24172. 'type',
  24173. ' TObject = class',
  24174. ' FField: TObject;',
  24175. ' property Field: TObject read FField write FField;',
  24176. ' end;',
  24177. ' THelper = class helper for TObject',
  24178. ' procedure Fly;',
  24179. ' class procedure Run;',
  24180. ' class procedure Jump; static;',
  24181. ' end;',
  24182. 'procedure THelper.Fly;',
  24183. 'begin',
  24184. ' Field.Fly;',
  24185. ' Field.Run;',
  24186. ' Field.Jump;',
  24187. ' with Field do begin',
  24188. ' Fly;',
  24189. ' Run;',
  24190. ' Jump;',
  24191. ' end;',
  24192. 'end;',
  24193. 'class procedure THelper.Run;',
  24194. 'begin',
  24195. 'end;',
  24196. 'class procedure THelper.Jump;',
  24197. 'begin',
  24198. 'end;',
  24199. 'var',
  24200. ' b: TObject;',
  24201. 'begin',
  24202. ' b.Field.Fly;',
  24203. ' b.Field.Run;',
  24204. ' b.Field.Jump;',
  24205. ' with b do begin',
  24206. ' Field.Run;',
  24207. ' Field.Fly;',
  24208. ' Field.Jump;',
  24209. ' end;',
  24210. ' with b.Field do begin',
  24211. ' Run;',
  24212. ' Fly;',
  24213. ' Jump;',
  24214. ' end;',
  24215. '']);
  24216. ConvertProgram;
  24217. CheckSource('TestClassHelper_PassProperty',
  24218. LinesToStr([ // statements
  24219. 'rtl.createClass(this, "TObject", null, function () {',
  24220. ' this.$init = function () {',
  24221. ' this.FField = null;',
  24222. ' };',
  24223. ' this.$final = function () {',
  24224. ' this.FField = undefined;',
  24225. ' };',
  24226. '});',
  24227. 'rtl.createHelper(this, "THelper", null, function () {',
  24228. ' this.Fly = function () {',
  24229. ' $mod.THelper.Fly.call(this.FField);',
  24230. ' $mod.THelper.Run.call(this.FField.$class);',
  24231. ' $mod.THelper.Jump();',
  24232. ' var $with = this.FField;',
  24233. ' $mod.THelper.Fly.call($with);',
  24234. ' $mod.THelper.Run.call($with.$class);',
  24235. ' $mod.THelper.Jump();',
  24236. ' };',
  24237. ' this.Run = function () {',
  24238. ' };',
  24239. ' this.Jump = function () {',
  24240. ' };',
  24241. '});',
  24242. 'this.b = null;',
  24243. '']),
  24244. LinesToStr([ // $mod.$main
  24245. '$mod.THelper.Fly.call($mod.b.FField);',
  24246. '$mod.THelper.Run.call($mod.b.FField.$class);',
  24247. '$mod.THelper.Jump();',
  24248. 'var $with = $mod.b;',
  24249. '$mod.THelper.Run.call($with.FField.$class);',
  24250. '$mod.THelper.Fly.call($with.FField);',
  24251. '$mod.THelper.Jump();',
  24252. 'var $with1 = $mod.b.FField;',
  24253. '$mod.THelper.Run.call($with1.$class);',
  24254. '$mod.THelper.Fly.call($with1);',
  24255. '$mod.THelper.Jump();',
  24256. '']));
  24257. end;
  24258. procedure TTestModule.TestExtClassHelper_ClassVar;
  24259. begin
  24260. StartProgram(false);
  24261. Add([
  24262. '{$modeswitch externalclass}',
  24263. 'type',
  24264. ' TExtA = class external name ''ExtObj''',
  24265. ' end;',
  24266. ' THelper = class helper for TExtA',
  24267. ' const',
  24268. ' One = 1;',
  24269. ' Two: word = 2;',
  24270. ' class var',
  24271. ' Glob: word;',
  24272. ' function Foo(w: word): word;',
  24273. ' class function Bar(w: word): word; static;',
  24274. ' end;',
  24275. 'function THelper.foo(w: word): word;',
  24276. 'begin',
  24277. ' Result:=w;',
  24278. ' Two:=One+w;',
  24279. ' Glob:=Glob;',
  24280. ' Result:=Self.Glob;',
  24281. ' Self.Glob:=Self.Glob;',
  24282. ' with Self do Glob:=Glob;',
  24283. 'end;',
  24284. 'class function THelper.bar(w: word): word;',
  24285. 'begin',
  24286. ' Result:=w;',
  24287. ' Two:=One;',
  24288. ' Glob:=Glob;',
  24289. 'end;',
  24290. 'var o: TExtA;',
  24291. 'begin',
  24292. ' texta.two:=texta.one;',
  24293. ' texta.Glob:=texta.Glob;',
  24294. ' with texta do begin',
  24295. ' two:=one;',
  24296. ' Glob:=Glob;',
  24297. ' end;',
  24298. ' o.two:=o.one;',
  24299. ' o.Glob:=o.Glob;',
  24300. ' with o do begin',
  24301. ' two:=one;',
  24302. ' Glob:=Glob;',
  24303. ' end;',
  24304. '']);
  24305. ConvertProgram;
  24306. CheckSource('TestExtClassHelper_ClassVar',
  24307. LinesToStr([ // statements
  24308. 'rtl.createHelper(this, "THelper", null, function () {',
  24309. ' this.One = 1;',
  24310. ' this.Two = 2;',
  24311. ' this.Glob = 0;',
  24312. ' this.Foo = function (w) {',
  24313. ' var Result = 0;',
  24314. ' Result = w;',
  24315. ' $mod.THelper.Two = 1 + w;',
  24316. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24317. ' Result = $mod.THelper.Glob;',
  24318. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24319. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24320. ' return Result;',
  24321. ' };',
  24322. ' this.Bar = function (w) {',
  24323. ' var Result = 0;',
  24324. ' Result = w;',
  24325. ' $mod.THelper.Two = 1;',
  24326. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24327. ' return Result;',
  24328. ' };',
  24329. '});',
  24330. 'this.o = null;',
  24331. '']),
  24332. LinesToStr([ // $mod.$main
  24333. '$mod.THelper.Two = 1;',
  24334. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24335. '$mod.THelper.Two = 1;',
  24336. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24337. '$mod.THelper.Two = 1;',
  24338. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24339. 'var $with = $mod.o;',
  24340. '$mod.THelper.Two = 1;',
  24341. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24342. '']));
  24343. end;
  24344. procedure TTestModule.TestExtClassHelper_Method_Call;
  24345. begin
  24346. StartProgram(false);
  24347. Add([
  24348. '{$modeswitch externalclass}',
  24349. 'type',
  24350. ' TFly = function(w: word): word of object;',
  24351. ' TExtA = class external name ''ExtObj''',
  24352. ' procedure Run(w: word = 10);',
  24353. ' end;',
  24354. ' THelper = class helper for TExtA',
  24355. ' function Foo(w: word = 1): word;',
  24356. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24357. ' end;',
  24358. 'var p: TFly;',
  24359. 'function THelper.foo(w: word): word;',
  24360. 'begin',
  24361. ' Run;',
  24362. ' Run();',
  24363. ' Run(11);',
  24364. ' Foo;',
  24365. ' Foo();',
  24366. ' Foo(12);',
  24367. ' Self.Foo;',
  24368. ' Self.Foo();',
  24369. ' Self.Foo(13);',
  24370. ' Fly;',
  24371. ' Fly();',
  24372. ' with Self do begin',
  24373. ' Foo;',
  24374. ' Foo();',
  24375. ' Foo(14);',
  24376. ' Fly;',
  24377. ' Fly();',
  24378. ' end;',
  24379. ' p:=@Fly;',
  24380. 'end;',
  24381. 'var Obj: TExtA;',
  24382. 'begin',
  24383. ' obj.Foo;',
  24384. ' obj.Foo();',
  24385. ' obj.Foo(21);',
  24386. ' obj.Fly;',
  24387. ' obj.Fly();',
  24388. ' with obj do begin',
  24389. ' Foo;',
  24390. ' Foo();',
  24391. ' Foo(22);',
  24392. ' Fly;',
  24393. ' Fly();',
  24394. ' end;',
  24395. ' p:[email protected];',
  24396. '']);
  24397. ConvertProgram;
  24398. CheckSource('TestExtClassHelper_Method_Call',
  24399. LinesToStr([ // statements
  24400. 'rtl.createHelper(this, "THelper", null, function () {',
  24401. ' this.Foo = function (w) {',
  24402. ' var Result = 0;',
  24403. ' this.Run(10);',
  24404. ' this.Run(10);',
  24405. ' this.Run(11);',
  24406. ' $mod.THelper.Foo.call(this, 1);',
  24407. ' $mod.THelper.Foo.call(this, 1);',
  24408. ' $mod.THelper.Foo.call(this, 12);',
  24409. ' $mod.THelper.Foo.call(this, 1);',
  24410. ' $mod.THelper.Foo.call(this, 1);',
  24411. ' $mod.THelper.Foo.call(this, 13);',
  24412. ' this.Fly(2);',
  24413. ' this.Fly(2);',
  24414. ' $mod.THelper.Foo.call(this, 1);',
  24415. ' $mod.THelper.Foo.call(this, 1);',
  24416. ' $mod.THelper.Foo.call(this, 14);',
  24417. ' this.Fly(2);',
  24418. ' this.Fly(2);',
  24419. ' $mod.p = rtl.createCallback(this, "Fly");',
  24420. ' return Result;',
  24421. ' };',
  24422. '});',
  24423. 'this.p = null;',
  24424. 'this.Obj = null;',
  24425. '']),
  24426. LinesToStr([ // $mod.$main
  24427. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24428. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24429. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24430. '$mod.Obj.Fly(2);',
  24431. '$mod.Obj.Fly(2);',
  24432. 'var $with = $mod.Obj;',
  24433. '$mod.THelper.Foo.call($with, 1);',
  24434. '$mod.THelper.Foo.call($with, 1);',
  24435. '$mod.THelper.Foo.call($with, 22);',
  24436. '$with.Fly(2);',
  24437. '$with.Fly(2);',
  24438. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24439. '']));
  24440. end;
  24441. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24442. begin
  24443. StartProgram(false);
  24444. Add([
  24445. '{$modeswitch externalclass}',
  24446. 'type',
  24447. ' TExtA = class external name ''ExtObj''',
  24448. ' procedure Run(w: word = 10);',
  24449. ' end;',
  24450. ' THelper = class helper for TExtA',
  24451. ' class procedure Fly;',
  24452. ' end;',
  24453. 'class procedure THelper.Fly;',
  24454. 'begin end;',
  24455. 'begin',
  24456. '']);
  24457. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24458. nHelperClassMethodForExtClassMustBeStatic);
  24459. ConvertProgram;
  24460. end;
  24461. procedure TTestModule.TestRecordHelper_ClassVar;
  24462. begin
  24463. StartProgram(false);
  24464. Add([
  24465. 'type',
  24466. ' TRec = record',
  24467. ' end;',
  24468. ' THelper = record helper for TRec',
  24469. ' const',
  24470. ' One = 1;',
  24471. ' Two: word = 2;',
  24472. ' class var',
  24473. ' Glob: word;',
  24474. ' function Foo(w: word): word;',
  24475. ' class function Bar(w: word): word; static;',
  24476. ' end;',
  24477. 'function THelper.foo(w: word): word;',
  24478. 'begin',
  24479. ' Result:=w;',
  24480. ' Two:=One+w;',
  24481. ' Glob:=Glob;',
  24482. ' Result:=Self.Glob;',
  24483. ' Self.Glob:=Self.Glob;',
  24484. ' with Self do Glob:=Glob;',
  24485. ' Self:=Self;',
  24486. 'end;',
  24487. 'class function THelper.bar(w: word): word;',
  24488. 'begin',
  24489. ' Result:=w;',
  24490. ' Two:=One;',
  24491. ' Glob:=Glob;',
  24492. 'end;',
  24493. 'var r: TRec;',
  24494. 'begin',
  24495. ' trec.two:=trec.one;',
  24496. ' trec.Glob:=trec.Glob;',
  24497. ' with trec do begin',
  24498. ' two:=one;',
  24499. ' Glob:=Glob;',
  24500. ' end;',
  24501. ' r.two:=r.one;',
  24502. ' r.Glob:=r.Glob;',
  24503. ' with r do begin',
  24504. ' two:=one;',
  24505. ' Glob:=Glob;',
  24506. ' end;',
  24507. '']);
  24508. ConvertProgram;
  24509. CheckSource('TestRecordHelper_ClassVar',
  24510. LinesToStr([ // statements
  24511. 'rtl.recNewT(this, "TRec", function () {',
  24512. ' this.$eq = function (b) {',
  24513. ' return true;',
  24514. ' };',
  24515. ' this.$assign = function (s) {',
  24516. ' return this;',
  24517. ' };',
  24518. '});',
  24519. 'rtl.createHelper(this, "THelper", null, function () {',
  24520. ' this.One = 1;',
  24521. ' this.Two = 2;',
  24522. ' this.Glob = 0;',
  24523. ' this.Foo = function (w) {',
  24524. ' var Result = 0;',
  24525. ' Result = w;',
  24526. ' $mod.THelper.Two = 1 + w;',
  24527. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24528. ' Result = $mod.THelper.Glob;',
  24529. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24530. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24531. ' this.$assign(this);',
  24532. ' return Result;',
  24533. ' };',
  24534. ' this.Bar = function (w) {',
  24535. ' var Result = 0;',
  24536. ' Result = w;',
  24537. ' $mod.THelper.Two = 1;',
  24538. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24539. ' return Result;',
  24540. ' };',
  24541. '});',
  24542. 'this.r = this.TRec.$new();',
  24543. '']),
  24544. LinesToStr([ // $mod.$main
  24545. '$mod.THelper.Two = 1;',
  24546. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24547. 'var $with = $mod.TRec;',
  24548. '$mod.THelper.Two = 1;',
  24549. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24550. '$mod.THelper.Two = 1;',
  24551. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24552. 'var $with1 = $mod.r;',
  24553. '$mod.THelper.Two = 1;',
  24554. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24555. '']));
  24556. end;
  24557. procedure TTestModule.TestRecordHelper_Method_Call;
  24558. begin
  24559. StartProgram(false);
  24560. Add([
  24561. '{$modeswitch AdvancedRecords}',
  24562. 'type',
  24563. ' TRec = record',
  24564. ' procedure Run(w: word = 10);',
  24565. ' end;',
  24566. ' THelper = record helper for TRec',
  24567. ' function Foo(w: word = 1): word;',
  24568. ' end;',
  24569. 'procedure TRec.Run(w: word);',
  24570. 'begin',
  24571. ' Foo;',
  24572. ' Foo();',
  24573. ' Foo(2);',
  24574. ' Self.Foo;',
  24575. ' Self.Foo();',
  24576. ' Self.Foo(3);',
  24577. ' with Self do begin',
  24578. ' Foo;',
  24579. ' Foo();',
  24580. ' Foo(4);',
  24581. ' end;',
  24582. 'end;',
  24583. 'function THelper.foo(w: word): word;',
  24584. 'begin',
  24585. ' Run;',
  24586. ' Run();',
  24587. ' Run(11);',
  24588. ' Foo;',
  24589. ' Foo();',
  24590. ' Foo(12);',
  24591. ' Self.Foo;',
  24592. ' Self.Foo();',
  24593. ' Self.Foo(13);',
  24594. ' with Self do begin',
  24595. ' Foo;',
  24596. ' Foo();',
  24597. ' Foo(14);',
  24598. ' end;',
  24599. 'end;',
  24600. 'var Rec: TRec;',
  24601. 'begin',
  24602. ' Rec.Foo;',
  24603. ' Rec.Foo();',
  24604. ' Rec.Foo(21);',
  24605. ' with Rec do begin',
  24606. ' Foo;',
  24607. ' Foo();',
  24608. ' Foo(22);',
  24609. ' end;',
  24610. '']);
  24611. ConvertProgram;
  24612. CheckSource('TestRecordHelper_Method_Call',
  24613. LinesToStr([ // statements
  24614. 'rtl.recNewT(this, "TRec", function () {',
  24615. ' this.$eq = function (b) {',
  24616. ' return true;',
  24617. ' };',
  24618. ' this.$assign = function (s) {',
  24619. ' return this;',
  24620. ' };',
  24621. ' this.Run = function (w) {',
  24622. ' $mod.THelper.Foo.call(this, 1);',
  24623. ' $mod.THelper.Foo.call(this, 1);',
  24624. ' $mod.THelper.Foo.call(this, 2);',
  24625. ' $mod.THelper.Foo.call(this, 1);',
  24626. ' $mod.THelper.Foo.call(this, 1);',
  24627. ' $mod.THelper.Foo.call(this, 3);',
  24628. ' $mod.THelper.Foo.call(this, 1);',
  24629. ' $mod.THelper.Foo.call(this, 1);',
  24630. ' $mod.THelper.Foo.call(this, 4);',
  24631. ' };',
  24632. '});',
  24633. 'rtl.createHelper(this, "THelper", null, function () {',
  24634. ' this.Foo = function (w) {',
  24635. ' var Result = 0;',
  24636. ' this.Run(10);',
  24637. ' this.Run(10);',
  24638. ' this.Run(11);',
  24639. ' $mod.THelper.Foo.call(this, 1);',
  24640. ' $mod.THelper.Foo.call(this, 1);',
  24641. ' $mod.THelper.Foo.call(this, 12);',
  24642. ' $mod.THelper.Foo.call(this, 1);',
  24643. ' $mod.THelper.Foo.call(this, 1);',
  24644. ' $mod.THelper.Foo.call(this, 13);',
  24645. ' $mod.THelper.Foo.call(this, 1);',
  24646. ' $mod.THelper.Foo.call(this, 1);',
  24647. ' $mod.THelper.Foo.call(this, 14);',
  24648. ' return Result;',
  24649. ' };',
  24650. '});',
  24651. 'this.Rec = this.TRec.$new();',
  24652. '']),
  24653. LinesToStr([ // $mod.$main
  24654. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24655. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24656. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24657. 'var $with = $mod.Rec;',
  24658. '$mod.THelper.Foo.call($with, 1);',
  24659. '$mod.THelper.Foo.call($with, 1);',
  24660. '$mod.THelper.Foo.call($with, 22);',
  24661. '']));
  24662. end;
  24663. procedure TTestModule.TestRecordHelper_Constructor;
  24664. begin
  24665. StartProgram(false);
  24666. Add([
  24667. '{$modeswitch AdvancedRecords}',
  24668. 'type',
  24669. ' TRec = record',
  24670. ' constructor Create(w: word);',
  24671. ' end;',
  24672. ' THelper = record helper for TRec',
  24673. ' constructor NewHlp(w: word);',
  24674. ' end;',
  24675. 'var',
  24676. ' Rec: TRec;',
  24677. 'constructor TRec.Create(w: word);',
  24678. 'begin',
  24679. ' NewHlp(2);', // normal call
  24680. ' trec.NewHlp(3);', // new instance
  24681. 'end;',
  24682. 'constructor THelper.NewHlp(w: word);',
  24683. 'begin',
  24684. ' create(2);', // normal call
  24685. ' trec.create(3);', // new instance
  24686. ' NewHlp(4);', // normal call
  24687. ' trec.NewHlp(5);', // new instance
  24688. 'end;',
  24689. 'begin',
  24690. ' rec.newhlp(2);', // normal call
  24691. ' with rec do newhlp(12);', // normal call
  24692. ' trec.newhlp(3);', // new instance
  24693. ' with trec do newhlp(13);', // new instance
  24694. '']);
  24695. ConvertProgram;
  24696. CheckSource('TestRecordHelper_Constructor',
  24697. LinesToStr([ // statements
  24698. 'rtl.recNewT(this, "TRec", function () {',
  24699. ' this.$eq = function (b) {',
  24700. ' return true;',
  24701. ' };',
  24702. ' this.$assign = function (s) {',
  24703. ' return this;',
  24704. ' };',
  24705. ' this.Create = function (w) {',
  24706. ' $mod.THelper.NewHlp.call(this, 2);',
  24707. ' $mod.THelper.$new("NewHlp", [3]);',
  24708. ' return this;',
  24709. ' };',
  24710. '});',
  24711. 'rtl.createHelper(this, "THelper", null, function () {',
  24712. ' this.NewHlp = function (w) {',
  24713. ' this.Create(2);',
  24714. ' $mod.TRec.$new().Create(3);',
  24715. ' $mod.THelper.NewHlp.call(this, 4);',
  24716. ' $mod.THelper.$new("NewHlp", [5]);',
  24717. ' return this;',
  24718. ' };',
  24719. ' this.$new = function (fn, args) {',
  24720. ' return this[fn].apply($mod.TRec.$new(), args);',
  24721. ' };',
  24722. '});',
  24723. 'this.Rec = this.TRec.$new();',
  24724. '']),
  24725. LinesToStr([ // $mod.$main
  24726. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24727. 'var $with = $mod.Rec;',
  24728. '$mod.THelper.NewHlp.call($with, 12);',
  24729. '$mod.THelper.$new("NewHlp", [3]);',
  24730. 'var $with1 = $mod.TRec;',
  24731. '$mod.THelper.$new("NewHlp", [13]);',
  24732. '']));
  24733. end;
  24734. procedure TTestModule.TestTypeHelper_ClassVar;
  24735. begin
  24736. StartProgram(false);
  24737. Add([
  24738. '{$modeswitch typehelpers}',
  24739. 'type',
  24740. ' THelper = type helper for byte',
  24741. ' const',
  24742. ' One = 1;',
  24743. ' Two: word = 2;',
  24744. ' class var',
  24745. ' Glob: word;',
  24746. ' function Foo(w: word): word;',
  24747. ' class function Bar(w: word): word; static;',
  24748. ' end;',
  24749. 'function THelper.foo(w: word): word;',
  24750. 'begin',
  24751. ' Result:=w;',
  24752. ' Two:=One+w;',
  24753. ' Glob:=Glob;',
  24754. ' Result:=Self.Glob;',
  24755. ' Self.Glob:=Self.Glob;',
  24756. ' with Self do Glob:=Glob;',
  24757. 'end;',
  24758. 'class function THelper.bar(w: word): word;',
  24759. 'begin',
  24760. ' Result:=w;',
  24761. ' Two:=One;',
  24762. ' Glob:=Glob;',
  24763. 'end;',
  24764. 'var b: byte;',
  24765. 'begin',
  24766. ' byte.two:=byte.one;',
  24767. ' byte.Glob:=byte.Glob;',
  24768. ' with byte do begin',
  24769. ' two:=one;',
  24770. ' Glob:=Glob;',
  24771. ' end;',
  24772. ' b.two:=b.one;',
  24773. ' b.Glob:=b.Glob;',
  24774. ' with b do begin',
  24775. ' two:=one;',
  24776. ' Glob:=Glob;',
  24777. ' end;',
  24778. '']);
  24779. ConvertProgram;
  24780. CheckSource('TestTypeHelper_ClassVar',
  24781. LinesToStr([ // statements
  24782. 'rtl.createHelper(this, "THelper", null, function () {',
  24783. ' this.One = 1;',
  24784. ' this.Two = 2;',
  24785. ' this.Glob = 0;',
  24786. ' this.Foo = function (w) {',
  24787. ' var Result = 0;',
  24788. ' Result = w;',
  24789. ' $mod.THelper.Two = 1 + w;',
  24790. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24791. ' Result = $mod.THelper.Glob;',
  24792. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24793. ' var $with = this.get();',
  24794. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24795. ' return Result;',
  24796. ' };',
  24797. ' this.Bar = function (w) {',
  24798. ' var Result = 0;',
  24799. ' Result = w;',
  24800. ' $mod.THelper.Two = 1;',
  24801. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24802. ' return Result;',
  24803. ' };',
  24804. '});',
  24805. 'this.b = 0;',
  24806. '']),
  24807. LinesToStr([ // $mod.$main
  24808. '$mod.THelper.Two = 1;',
  24809. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24810. '$mod.THelper.Two = 1;',
  24811. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24812. '$mod.THelper.Two = 1;',
  24813. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24814. 'var $with = $mod.b;',
  24815. '$mod.THelper.Two = 1;',
  24816. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24817. '']));
  24818. end;
  24819. procedure TTestModule.TestTypeHelper_PassResultElement;
  24820. begin
  24821. StartProgram(false);
  24822. Add([
  24823. '{$modeswitch typehelpers}',
  24824. 'type',
  24825. ' THelper = type helper for word',
  24826. ' procedure DoIt(e: byte = 123);',
  24827. ' class procedure DoSome(e: byte = 456); static;',
  24828. ' end;',
  24829. 'procedure THelper.DoIt(e: byte);',
  24830. 'begin',
  24831. 'end;',
  24832. 'class procedure THelper.DoSome(e: byte);',
  24833. 'begin',
  24834. 'end;',
  24835. 'function Foo(w: word): word;',
  24836. 'begin',
  24837. ' Result.DoIt;',
  24838. ' Result.DoIt();',
  24839. ' Result.DoSome;',
  24840. ' Result.DoSome();',
  24841. ' with Result do begin',
  24842. ' DoIt;',
  24843. ' DoIt();',
  24844. ' DoSome;',
  24845. ' DoSome();',
  24846. ' end;',
  24847. 'end;',
  24848. 'begin',
  24849. '']);
  24850. ConvertProgram;
  24851. CheckSource('TestTypeHelper_PassResultElement',
  24852. LinesToStr([ // statements
  24853. 'rtl.createHelper(this, "THelper", null, function () {',
  24854. ' this.DoIt = function (e) {',
  24855. ' };',
  24856. ' this.DoSome = function (e) {',
  24857. ' };',
  24858. '});',
  24859. 'this.Foo = function (w) {',
  24860. ' var Result = 0;',
  24861. ' $mod.THelper.DoIt.call({',
  24862. ' get: function () {',
  24863. ' return Result;',
  24864. ' },',
  24865. ' set: function (v) {',
  24866. ' Result = v;',
  24867. ' }',
  24868. ' }, 123);',
  24869. ' $mod.THelper.DoIt.call({',
  24870. ' get: function () {',
  24871. ' return Result;',
  24872. ' },',
  24873. ' set: function (v) {',
  24874. ' Result = v;',
  24875. ' }',
  24876. ' }, 123);',
  24877. ' $mod.THelper.DoSome(456);',
  24878. ' $mod.THelper.DoSome(456);',
  24879. ' $mod.THelper.DoIt.call({',
  24880. ' get: function () {',
  24881. ' return Result;',
  24882. ' },',
  24883. ' set: function (v) {',
  24884. ' Result = v;',
  24885. ' }',
  24886. ' }, 123);',
  24887. ' $mod.THelper.DoIt.call({',
  24888. ' get: function () {',
  24889. ' return Result;',
  24890. ' },',
  24891. ' set: function (v) {',
  24892. ' Result = v;',
  24893. ' }',
  24894. ' }, 123);',
  24895. ' $mod.THelper.DoSome(456);',
  24896. ' $mod.THelper.DoSome(456);',
  24897. ' return Result;',
  24898. '};',
  24899. '']),
  24900. LinesToStr([ // $mod.$main
  24901. '']));
  24902. end;
  24903. procedure TTestModule.TestTypeHelper_PassArgs;
  24904. begin
  24905. StartProgram(false);
  24906. Add([
  24907. '{$modeswitch typehelpers}',
  24908. 'type',
  24909. ' THelper = type helper for word',
  24910. ' procedure DoIt(e: byte = 123);',
  24911. ' end;',
  24912. 'procedure THelper.DoIt(e: byte);',
  24913. 'begin',
  24914. 'end;',
  24915. 'procedure FooDefault(a: word);',
  24916. 'begin',
  24917. ' a.DoIt;',
  24918. ' with a do DoIt;',
  24919. 'end;',
  24920. 'procedure FooConst(const a: word);',
  24921. 'begin',
  24922. ' a.DoIt;',
  24923. ' with a do DoIt;',
  24924. 'end;',
  24925. 'procedure FooVar(var a: word);',
  24926. 'begin',
  24927. ' a.DoIt;',
  24928. ' with a do DoIt;',
  24929. 'end;',
  24930. 'begin',
  24931. '']);
  24932. ConvertProgram;
  24933. CheckSource('TestTypeHelper_PassArgs',
  24934. LinesToStr([ // statements
  24935. 'rtl.createHelper(this, "THelper", null, function () {',
  24936. ' this.DoIt = function (e) {',
  24937. ' };',
  24938. '});',
  24939. 'this.FooDefault = function (a) {',
  24940. ' $mod.THelper.DoIt.call({',
  24941. ' get: function () {',
  24942. ' return a;',
  24943. ' },',
  24944. ' set: function (v) {',
  24945. ' a = v;',
  24946. ' }',
  24947. ' }, 123);',
  24948. ' $mod.THelper.DoIt.call({',
  24949. ' get: function () {',
  24950. ' return a;',
  24951. ' },',
  24952. ' set: function (v) {',
  24953. ' a = v;',
  24954. ' }',
  24955. ' }, 123);',
  24956. '};',
  24957. 'this.FooConst = function (a) {',
  24958. ' $mod.THelper.DoIt.call({',
  24959. ' get: function () {',
  24960. ' return a;',
  24961. ' },',
  24962. ' set: function (v) {',
  24963. ' rtl.raiseE("EPropReadOnly");',
  24964. ' }',
  24965. ' }, 123);',
  24966. ' $mod.THelper.DoIt.call({',
  24967. ' get: function () {',
  24968. ' return a;',
  24969. ' },',
  24970. ' set: function () {',
  24971. ' rtl.raiseE("EPropReadOnly");',
  24972. ' }',
  24973. ' }, 123);',
  24974. '};',
  24975. 'this.FooVar = function (a) {',
  24976. ' $mod.THelper.DoIt.call(a, 123);',
  24977. ' var $with = a.get();',
  24978. ' $mod.THelper.DoIt.call(a, 123);',
  24979. '};',
  24980. '']),
  24981. LinesToStr([ // $mod.$main
  24982. '']));
  24983. end;
  24984. procedure TTestModule.TestTypeHelper_PassVarConst;
  24985. begin
  24986. StartProgram(false);
  24987. Add([
  24988. '{$modeswitch typehelpers}',
  24989. 'type',
  24990. ' THelper = type helper for word',
  24991. ' procedure DoIt(e: byte = 123);',
  24992. ' end;',
  24993. 'procedure THelper.DoIt(e: byte);',
  24994. 'begin',
  24995. 'end;',
  24996. 'var a: word;',
  24997. 'const c: word = 2;',
  24998. '{$writeableconst off}',
  24999. 'const r: word = 3;',
  25000. 'begin',
  25001. ' a.DoIt;',
  25002. ' with a do DoIt;',
  25003. ' c.DoIt;',
  25004. ' with c do DoIt;',
  25005. ' r.DoIt;',
  25006. ' with r do DoIt;',
  25007. '']);
  25008. ConvertProgram;
  25009. CheckSource('TestTypeHelper_PassVarConst',
  25010. LinesToStr([ // statements
  25011. 'rtl.createHelper(this, "THelper", null, function () {',
  25012. ' this.DoIt = function (e) {',
  25013. ' };',
  25014. '});',
  25015. 'this.a = 0;',
  25016. 'this.c = 2;',
  25017. 'this.r = 3;',
  25018. '']),
  25019. LinesToStr([ // $mod.$main
  25020. '$mod.THelper.DoIt.call({',
  25021. ' p: $mod,',
  25022. ' get: function () {',
  25023. ' return this.p.a;',
  25024. ' },',
  25025. ' set: function (v) {',
  25026. ' this.p.a = v;',
  25027. ' }',
  25028. '}, 123);',
  25029. 'var $with = $mod.a;',
  25030. '$mod.THelper.DoIt.call({',
  25031. ' get: function () {',
  25032. ' return $with;',
  25033. ' },',
  25034. ' set: function (v) {',
  25035. ' $with = v;',
  25036. ' }',
  25037. '}, 123);',
  25038. '$mod.THelper.DoIt.call({',
  25039. ' p: $mod,',
  25040. ' get: function () {',
  25041. ' return this.p.c;',
  25042. ' },',
  25043. ' set: function (v) {',
  25044. ' this.p.c = v;',
  25045. ' }',
  25046. '}, 123);',
  25047. 'var $with1 = $mod.c;',
  25048. '$mod.THelper.DoIt.call({',
  25049. ' get: function () {',
  25050. ' return $with1;',
  25051. ' },',
  25052. ' set: function (v) {',
  25053. ' $with1 = v;',
  25054. ' }',
  25055. '}, 123);',
  25056. '$mod.THelper.DoIt.call({',
  25057. ' get: function () {',
  25058. ' return 3;',
  25059. ' },',
  25060. ' set: function (v) {',
  25061. ' rtl.raiseE("EPropReadOnly");',
  25062. ' }',
  25063. '}, 123);',
  25064. 'var $with2 = 3;',
  25065. ' $mod.THelper.DoIt.call({',
  25066. ' get: function () {',
  25067. ' return $with2;',
  25068. ' },',
  25069. ' set: function () {',
  25070. ' rtl.raiseE("EPropReadOnly");',
  25071. ' }',
  25072. ' }, 123);',
  25073. '']));
  25074. end;
  25075. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25076. begin
  25077. StartProgram(false);
  25078. Add([
  25079. '{$modeswitch typehelpers}',
  25080. 'type',
  25081. ' THelper = type helper for word',
  25082. ' procedure DoIt(e: byte = 123);',
  25083. ' end;',
  25084. 'procedure THelper.DoIt(e: byte);',
  25085. 'begin',
  25086. 'end;',
  25087. 'function Foo(b: byte = 1): word;',
  25088. 'begin',
  25089. 'end;',
  25090. 'begin',
  25091. ' Foo.DoIt;',
  25092. ' Foo().DoIt;',
  25093. ' with Foo do DoIt;',
  25094. ' with Foo() do DoIt;',
  25095. '']);
  25096. ConvertProgram;
  25097. CheckSource('TestTypeHelper_PassFuncResult',
  25098. LinesToStr([ // statements
  25099. 'rtl.createHelper(this, "THelper", null, function () {',
  25100. ' this.DoIt = function (e) {',
  25101. ' };',
  25102. '});',
  25103. 'this.Foo = function (b) {',
  25104. ' var Result = 0;',
  25105. ' return Result;',
  25106. '};',
  25107. '']),
  25108. LinesToStr([ // $mod.$main
  25109. '$mod.THelper.DoIt.call({',
  25110. ' a: $mod.Foo(1),',
  25111. ' get: function () {',
  25112. ' return this.a;',
  25113. ' },',
  25114. ' set: function (v) {',
  25115. ' this.a = v;',
  25116. ' }',
  25117. '}, 123);',
  25118. '$mod.THelper.DoIt.call({',
  25119. ' a: $mod.Foo(1),',
  25120. ' get: function () {',
  25121. ' return this.a;',
  25122. ' },',
  25123. ' set: function (v) {',
  25124. ' this.a = v;',
  25125. ' }',
  25126. '}, 123);',
  25127. 'var $with = $mod.Foo(1);',
  25128. '$mod.THelper.DoIt.call({',
  25129. ' get: function () {',
  25130. ' return $with;',
  25131. ' },',
  25132. ' set: function (v) {',
  25133. ' $with = v;',
  25134. ' }',
  25135. '}, 123);',
  25136. 'var $with1 = $mod.Foo(1);',
  25137. '$mod.THelper.DoIt.call({',
  25138. ' get: function () {',
  25139. ' return $with1;',
  25140. ' },',
  25141. ' set: function (v) {',
  25142. ' $with1 = v;',
  25143. ' }',
  25144. '}, 123);',
  25145. '']));
  25146. end;
  25147. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25148. begin
  25149. StartProgram(false);
  25150. Add([
  25151. '{$modeswitch typehelpers}',
  25152. 'type',
  25153. ' TObject = class',
  25154. ' FField: word;',
  25155. ' procedure SetField(Value: word);',
  25156. ' property Field: word read FField write SetField;',
  25157. ' end;',
  25158. ' THelper = type helper for word',
  25159. ' procedure Fly;',
  25160. ' class procedure Run; static;',
  25161. ' end;',
  25162. 'procedure TObject.SetField(Value: word);',
  25163. 'begin',
  25164. ' Field.Fly;',
  25165. ' Field.Run;',
  25166. ' Self.Field.Fly;',
  25167. ' Self.Field.Run;',
  25168. ' with Self do begin',
  25169. ' Field.Fly;',
  25170. ' Field.Run;',
  25171. ' end;',
  25172. ' with Self.Field do begin',
  25173. ' Fly;',
  25174. ' Run;',
  25175. ' end;',
  25176. 'end;',
  25177. 'procedure THelper.Fly;',
  25178. 'begin',
  25179. 'end;',
  25180. 'class procedure THelper.Run;',
  25181. 'begin',
  25182. 'end;',
  25183. 'var',
  25184. ' o: TObject;',
  25185. 'begin',
  25186. ' o.Field.Fly;',
  25187. ' o.Field.Run;',
  25188. ' with o do begin',
  25189. ' Field.Fly;',
  25190. ' Field.Run;',
  25191. ' end;',
  25192. ' with o.Field do begin',
  25193. ' Fly;',
  25194. ' Run;',
  25195. ' end;',
  25196. '']);
  25197. ConvertProgram;
  25198. CheckSource('TestTypeHelper_PassPropertyField',
  25199. LinesToStr([ // statements
  25200. 'rtl.createClass(this, "TObject", null, function () {',
  25201. ' this.$init = function () {',
  25202. ' this.FField = 0;',
  25203. ' };',
  25204. ' this.$final = function () {',
  25205. ' };',
  25206. ' this.SetField = function (Value) {',
  25207. ' $mod.THelper.Fly.call({',
  25208. ' p: this,',
  25209. ' get: function () {',
  25210. ' return this.p.FField;',
  25211. ' },',
  25212. ' set: function (v) {',
  25213. ' this.p.FField = v;',
  25214. ' }',
  25215. ' });',
  25216. ' $mod.THelper.Run();',
  25217. ' $mod.THelper.Fly.call({',
  25218. ' p: this,',
  25219. ' get: function () {',
  25220. ' return this.p.FField;',
  25221. ' },',
  25222. ' set: function (v) {',
  25223. ' this.p.FField = v;',
  25224. ' }',
  25225. ' });',
  25226. ' $mod.THelper.Run();',
  25227. ' $mod.THelper.Fly.call({',
  25228. ' p: this,',
  25229. ' get: function () {',
  25230. ' return this.p.FField;',
  25231. ' },',
  25232. ' set: function (v) {',
  25233. ' this.p.FField = v;',
  25234. ' }',
  25235. ' });',
  25236. ' $mod.THelper.Run();',
  25237. ' var $with = this.FField;',
  25238. ' $mod.THelper.Fly.call({',
  25239. ' get: function () {',
  25240. ' return $with;',
  25241. ' },',
  25242. ' set: function (v) {',
  25243. ' $with = v;',
  25244. ' }',
  25245. ' });',
  25246. ' $mod.THelper.Run();',
  25247. ' };',
  25248. '});',
  25249. 'rtl.createHelper(this, "THelper", null, function () {',
  25250. ' this.Fly = function () {',
  25251. ' };',
  25252. ' this.Run = function () {',
  25253. ' };',
  25254. '});',
  25255. 'this.o = null;',
  25256. '']),
  25257. LinesToStr([ // $mod.$main
  25258. '$mod.THelper.Fly.call({',
  25259. ' p: $mod.o,',
  25260. ' get: function () {',
  25261. ' return this.p.FField;',
  25262. ' },',
  25263. ' set: function (v) {',
  25264. ' this.p.FField = v;',
  25265. ' }',
  25266. '});',
  25267. '$mod.THelper.Run();',
  25268. 'var $with = $mod.o;',
  25269. '$mod.THelper.Fly.call({',
  25270. ' p: $with,',
  25271. ' get: function () {',
  25272. ' return this.p.FField;',
  25273. ' },',
  25274. ' set: function (v) {',
  25275. ' this.p.FField = v;',
  25276. ' }',
  25277. '});',
  25278. '$mod.THelper.Run();',
  25279. 'var $with1 = $mod.o.FField;',
  25280. '$mod.THelper.Fly.call({',
  25281. ' get: function () {',
  25282. ' return $with1;',
  25283. ' },',
  25284. ' set: function (v) {',
  25285. ' $with1 = v;',
  25286. ' }',
  25287. '});',
  25288. '$mod.THelper.Run();',
  25289. '']));
  25290. end;
  25291. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  25292. begin
  25293. StartProgram(false);
  25294. Add([
  25295. '{$modeswitch typehelpers}',
  25296. 'type',
  25297. ' TObject = class',
  25298. ' FField: word;',
  25299. ' function GetField: word;',
  25300. ' property Field: word read GetField write FField;',
  25301. ' end;',
  25302. ' THelper = type helper for word',
  25303. ' procedure Fly;',
  25304. ' class procedure Run; static;',
  25305. ' end;',
  25306. 'function TObject.GetField: word;',
  25307. 'begin',
  25308. ' Field.Fly;',
  25309. ' Field.Run;',
  25310. ' Self.Field.Fly;',
  25311. ' Self.Field.Run;',
  25312. ' with Self do begin',
  25313. ' Field.Fly;',
  25314. ' Field.Run;',
  25315. ' end;',
  25316. ' with Self.Field do begin',
  25317. ' Fly;',
  25318. ' Run;',
  25319. ' end;',
  25320. 'end;',
  25321. 'procedure THelper.Fly;',
  25322. 'begin',
  25323. 'end;',
  25324. 'class procedure THelper.Run;',
  25325. 'begin',
  25326. 'end;',
  25327. 'var',
  25328. ' o: TObject;',
  25329. 'begin',
  25330. ' o.Field.Fly;',
  25331. ' o.Field.Run;',
  25332. ' with o do begin',
  25333. ' Field.Fly;',
  25334. ' Field.Run;',
  25335. ' end;',
  25336. ' with o.Field do begin',
  25337. ' Fly;',
  25338. ' Run;',
  25339. ' end;',
  25340. '']);
  25341. ConvertProgram;
  25342. CheckSource('TestTypeHelper_PassPropertyGetter',
  25343. LinesToStr([ // statements
  25344. 'rtl.createClass(this, "TObject", null, function () {',
  25345. ' this.$init = function () {',
  25346. ' this.FField = 0;',
  25347. ' };',
  25348. ' this.$final = function () {',
  25349. ' };',
  25350. ' this.GetField = function () {',
  25351. ' var Result = 0;',
  25352. ' $mod.THelper.Fly.call({',
  25353. ' p: this.GetField(),',
  25354. ' get: function () {',
  25355. ' return this.p;',
  25356. ' },',
  25357. ' set: function (v) {',
  25358. ' this.p = v;',
  25359. ' }',
  25360. ' });',
  25361. ' $mod.THelper.Run();',
  25362. ' $mod.THelper.Fly.call({',
  25363. ' p: this.GetField(),',
  25364. ' get: function () {',
  25365. ' return this.p;',
  25366. ' },',
  25367. ' set: function (v) {',
  25368. ' this.p = v;',
  25369. ' }',
  25370. ' });',
  25371. ' $mod.THelper.Run();',
  25372. ' $mod.THelper.Fly.call({',
  25373. ' p: this.GetField(),',
  25374. ' get: function () {',
  25375. ' return this.p;',
  25376. ' },',
  25377. ' set: function (v) {',
  25378. ' this.p = v;',
  25379. ' }',
  25380. ' });',
  25381. ' $mod.THelper.Run();',
  25382. ' var $with = this.GetField();',
  25383. ' $mod.THelper.Fly.call({',
  25384. ' get: function () {',
  25385. ' return $with;',
  25386. ' },',
  25387. ' set: function (v) {',
  25388. ' $with = v;',
  25389. ' }',
  25390. ' });',
  25391. ' $mod.THelper.Run();',
  25392. ' return Result;',
  25393. ' };',
  25394. '});',
  25395. 'rtl.createHelper(this, "THelper", null, function () {',
  25396. ' this.Fly = function () {',
  25397. ' };',
  25398. ' this.Run = function () {',
  25399. ' };',
  25400. '});',
  25401. 'this.o = null;',
  25402. '']),
  25403. LinesToStr([ // $mod.$main
  25404. '$mod.THelper.Fly.call({',
  25405. ' p: $mod.o.GetField(),',
  25406. ' get: function () {',
  25407. ' return this.p;',
  25408. ' },',
  25409. ' set: function (v) {',
  25410. ' this.p = v;',
  25411. ' }',
  25412. '});',
  25413. '$mod.THelper.Run();',
  25414. 'var $with = $mod.o;',
  25415. '$mod.THelper.Fly.call({',
  25416. ' p: $with.GetField(),',
  25417. ' get: function () {',
  25418. ' return this.p;',
  25419. ' },',
  25420. ' set: function (v) {',
  25421. ' this.p = v;',
  25422. ' }',
  25423. '});',
  25424. '$mod.THelper.Run();',
  25425. 'var $with1 = $mod.o.GetField();',
  25426. '$mod.THelper.Fly.call({',
  25427. ' get: function () {',
  25428. ' return $with1;',
  25429. ' },',
  25430. ' set: function (v) {',
  25431. ' $with1 = v;',
  25432. ' }',
  25433. '});',
  25434. '$mod.THelper.Run();',
  25435. '']));
  25436. end;
  25437. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25438. begin
  25439. StartProgram(false);
  25440. Add([
  25441. '{$modeswitch typehelpers}',
  25442. 'type',
  25443. ' TObject = class',
  25444. ' class var FField: word;',
  25445. ' class procedure SetField(Value: word);',
  25446. ' class property Field: word read FField write SetField;',
  25447. ' end;',
  25448. ' THelper = type helper for word',
  25449. ' procedure Fly(n: byte);',
  25450. ' end;',
  25451. 'class procedure TObject.SetField(Value: word);',
  25452. 'begin',
  25453. ' Field.Fly(1);',
  25454. ' Self.Field.Fly(2);',
  25455. ' with Self do Field.Fly(3);',
  25456. ' with Self.Field do Fly(4);',
  25457. ' TObject.Field.Fly(5);',
  25458. ' with TObject do Field.Fly(6);',
  25459. ' with TObject.Field do Fly(7);',
  25460. 'end;',
  25461. 'procedure THelper.Fly(n: byte);',
  25462. 'begin',
  25463. 'end;',
  25464. 'var',
  25465. ' o: TObject;',
  25466. 'begin',
  25467. ' o.Field.Fly(11);',
  25468. ' with o do Field.Fly(12);',
  25469. ' with o.Field do Fly(13);',
  25470. ' TObject.Field.Fly(14);',
  25471. ' with TObject do Field.Fly(15);',
  25472. ' with TObject.Field do Fly(16);',
  25473. '']);
  25474. ConvertProgram;
  25475. CheckSource('TestTypeHelper_PassClassPropertyField',
  25476. LinesToStr([ // statements
  25477. 'rtl.createClass(this, "TObject", null, function () {',
  25478. ' this.FField = 0;',
  25479. ' this.$init = function () {',
  25480. ' };',
  25481. ' this.$final = function () {',
  25482. ' };',
  25483. ' this.SetField = function (Value) {',
  25484. ' $mod.THelper.Fly.call({',
  25485. ' p: this,',
  25486. ' get: function () {',
  25487. ' return this.p.FField;',
  25488. ' },',
  25489. ' set: function (v) {',
  25490. ' $mod.TObject.FField = v;',
  25491. ' }',
  25492. ' }, 1);',
  25493. ' $mod.THelper.Fly.call({',
  25494. ' p: this,',
  25495. ' get: function () {',
  25496. ' return this.p.FField;',
  25497. ' },',
  25498. ' set: function (v) {',
  25499. ' $mod.TObject.FField = v;',
  25500. ' }',
  25501. ' }, 2);',
  25502. ' $mod.THelper.Fly.call({',
  25503. ' p: this,',
  25504. ' get: function () {',
  25505. ' return this.p.FField;',
  25506. ' },',
  25507. ' set: function (v) {',
  25508. ' $mod.TObject.FField = v;',
  25509. ' }',
  25510. ' }, 3);',
  25511. ' var $with = this.FField;',
  25512. ' $mod.THelper.Fly.call({',
  25513. ' get: function () {',
  25514. ' return $with;',
  25515. ' },',
  25516. ' set: function (v) {',
  25517. ' $with = v;',
  25518. ' }',
  25519. ' }, 4);',
  25520. ' $mod.THelper.Fly.call({',
  25521. ' p: $mod.TObject,',
  25522. ' get: function () {',
  25523. ' return this.p.FField;',
  25524. ' },',
  25525. ' set: function (v) {',
  25526. ' $mod.TObject.FField = v;',
  25527. ' }',
  25528. ' }, 5);',
  25529. ' var $with1 = $mod.TObject;',
  25530. ' $mod.THelper.Fly.call({',
  25531. ' p: $with1,',
  25532. ' get: function () {',
  25533. ' return this.p.FField;',
  25534. ' },',
  25535. ' set: function (v) {',
  25536. ' $mod.TObject.FField = v;',
  25537. ' }',
  25538. ' }, 6);',
  25539. ' var $with2 = $mod.TObject.FField;',
  25540. ' $mod.THelper.Fly.call({',
  25541. ' get: function () {',
  25542. ' return $with2;',
  25543. ' },',
  25544. ' set: function (v) {',
  25545. ' $with2 = v;',
  25546. ' }',
  25547. ' }, 7);',
  25548. ' };',
  25549. '});',
  25550. 'rtl.createHelper(this, "THelper", null, function () {',
  25551. ' this.Fly = function (n) {',
  25552. ' };',
  25553. '});',
  25554. 'this.o = null;',
  25555. '']),
  25556. LinesToStr([ // $mod.$main
  25557. '$mod.THelper.Fly.call({',
  25558. ' p: $mod.o,',
  25559. ' get: function () {',
  25560. ' return this.p.FField;',
  25561. ' },',
  25562. ' set: function (v) {',
  25563. ' $mod.TObject.FField = v;',
  25564. ' }',
  25565. '}, 11);',
  25566. 'var $with = $mod.o;',
  25567. '$mod.THelper.Fly.call({',
  25568. ' p: $with,',
  25569. ' get: function () {',
  25570. ' return this.p.FField;',
  25571. ' },',
  25572. ' set: function (v) {',
  25573. ' $mod.TObject.FField = v;',
  25574. ' }',
  25575. '}, 12);',
  25576. 'var $with1 = $mod.o.FField;',
  25577. '$mod.THelper.Fly.call({',
  25578. ' get: function () {',
  25579. ' return $with1;',
  25580. ' },',
  25581. ' set: function (v) {',
  25582. ' $with1 = v;',
  25583. ' }',
  25584. '}, 13);',
  25585. '$mod.THelper.Fly.call({',
  25586. ' p: $mod.TObject,',
  25587. ' get: function () {',
  25588. ' return this.p.FField;',
  25589. ' },',
  25590. ' set: function (v) {',
  25591. ' $mod.TObject.FField = v;',
  25592. ' }',
  25593. '}, 14);',
  25594. 'var $with2 = $mod.TObject;',
  25595. '$mod.THelper.Fly.call({',
  25596. ' p: $with2,',
  25597. ' get: function () {',
  25598. ' return this.p.FField;',
  25599. ' },',
  25600. ' set: function (v) {',
  25601. ' $mod.TObject.FField = v;',
  25602. ' }',
  25603. '}, 15);',
  25604. 'var $with3 = $mod.TObject.FField;',
  25605. '$mod.THelper.Fly.call({',
  25606. ' get: function () {',
  25607. ' return $with3;',
  25608. ' },',
  25609. ' set: function (v) {',
  25610. ' $with3 = v;',
  25611. ' }',
  25612. '}, 16);',
  25613. '']));
  25614. end;
  25615. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25616. begin
  25617. StartProgram(false);
  25618. Add([
  25619. '{$modeswitch typehelpers}',
  25620. 'type',
  25621. ' TObject = class',
  25622. ' class var FField: word;',
  25623. ' class function GetField: word; static;',
  25624. ' class property Field: word read GetField write FField;',
  25625. ' end;',
  25626. ' THelper = type helper for word',
  25627. ' procedure Fly(n: byte);',
  25628. ' end;',
  25629. 'class function TObject.GetField: word;',
  25630. 'begin',
  25631. ' Field.Fly(1);',
  25632. ' TObject.Field.Fly(5);',
  25633. ' with TObject do Field.Fly(6);',
  25634. ' with TObject.Field do Fly(7);',
  25635. 'end;',
  25636. 'procedure THelper.Fly(n: byte);',
  25637. 'begin',
  25638. 'end;',
  25639. 'var',
  25640. ' o: TObject;',
  25641. 'begin',
  25642. ' o.Field.Fly(11);',
  25643. ' with o do Field.Fly(12);',
  25644. ' with o.Field do Fly(13);',
  25645. '']);
  25646. ConvertProgram;
  25647. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25648. LinesToStr([ // statements
  25649. 'rtl.createClass(this, "TObject", null, function () {',
  25650. ' this.FField = 0;',
  25651. ' this.$init = function () {',
  25652. ' };',
  25653. ' this.$final = function () {',
  25654. ' };',
  25655. ' this.GetField = function () {',
  25656. ' var Result = 0;',
  25657. ' $mod.THelper.Fly.call({',
  25658. ' p: $mod.TObject.GetField(),',
  25659. ' get: function () {',
  25660. ' return this.p;',
  25661. ' },',
  25662. ' set: function (v) {',
  25663. ' this.p = v;',
  25664. ' }',
  25665. ' }, 1);',
  25666. ' $mod.THelper.Fly.call({',
  25667. ' p: $mod.TObject.GetField(),',
  25668. ' get: function () {',
  25669. ' return this.p;',
  25670. ' },',
  25671. ' set: function (v) {',
  25672. ' this.p = v;',
  25673. ' }',
  25674. ' }, 5);',
  25675. ' var $with = $mod.TObject;',
  25676. ' $mod.THelper.Fly.call({',
  25677. ' p: $with.GetField(),',
  25678. ' get: function () {',
  25679. ' return this.p;',
  25680. ' },',
  25681. ' set: function (v) {',
  25682. ' this.p = v;',
  25683. ' }',
  25684. ' }, 6);',
  25685. ' var $with1 = $mod.TObject.GetField();',
  25686. ' $mod.THelper.Fly.call({',
  25687. ' get: function () {',
  25688. ' return $with1;',
  25689. ' },',
  25690. ' set: function (v) {',
  25691. ' $with1 = v;',
  25692. ' }',
  25693. ' }, 7);',
  25694. ' return Result;',
  25695. ' };',
  25696. '});',
  25697. 'rtl.createHelper(this, "THelper", null, function () {',
  25698. ' this.Fly = function (n) {',
  25699. ' };',
  25700. '});',
  25701. 'this.o = null;',
  25702. '']),
  25703. LinesToStr([ // $mod.$main
  25704. '$mod.THelper.Fly.call({',
  25705. ' p: $mod.TObject.GetField(),',
  25706. ' get: function () {',
  25707. ' return this.p;',
  25708. ' },',
  25709. ' set: function (v) {',
  25710. ' this.p = v;',
  25711. ' }',
  25712. '}, 11);',
  25713. 'var $with = $mod.o;',
  25714. '$mod.THelper.Fly.call({',
  25715. ' p: $with.GetField(),',
  25716. ' get: function () {',
  25717. ' return this.p;',
  25718. ' },',
  25719. ' set: function (v) {',
  25720. ' this.p = v;',
  25721. ' }',
  25722. '}, 12);',
  25723. 'var $with1 = $mod.TObject.GetField();',
  25724. '$mod.THelper.Fly.call({',
  25725. ' get: function () {',
  25726. ' return $with1;',
  25727. ' },',
  25728. ' set: function (v) {',
  25729. ' $with1 = v;',
  25730. ' }',
  25731. '}, 13);',
  25732. '']));
  25733. end;
  25734. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25735. begin
  25736. StartProgram(false);
  25737. Add([
  25738. '{$modeswitch typehelpers}',
  25739. 'type',
  25740. ' TObject = class',
  25741. ' class var FField: word;',
  25742. ' class function GetField: word;',
  25743. ' class property Field: word read GetField write FField;',
  25744. ' end;',
  25745. ' TClass = class of TObject;',
  25746. ' THelper = type helper for word',
  25747. ' procedure Fly(n: byte);',
  25748. ' end;',
  25749. 'class function TObject.GetField: word;',
  25750. 'begin',
  25751. ' Field.Fly(1);',
  25752. ' Self.Field.Fly(5);',
  25753. ' with Self do Field.Fly(6);',
  25754. ' with Self.Field do Fly(7);',
  25755. 'end;',
  25756. 'procedure THelper.Fly(n: byte);',
  25757. 'begin',
  25758. 'end;',
  25759. 'var',
  25760. ' o: TObject;',
  25761. ' c: TClass;',
  25762. 'begin',
  25763. ' o.Field.Fly(11);',
  25764. ' with o do Field.Fly(12);',
  25765. ' with o.Field do Fly(13);',
  25766. ' c.Field.Fly(14);',
  25767. ' with c do Field.Fly(15);',
  25768. ' with c.Field do Fly(16);',
  25769. '']);
  25770. ConvertProgram;
  25771. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25772. LinesToStr([ // statements
  25773. 'rtl.createClass(this, "TObject", null, function () {',
  25774. ' this.FField = 0;',
  25775. ' this.$init = function () {',
  25776. ' };',
  25777. ' this.$final = function () {',
  25778. ' };',
  25779. ' this.GetField = function () {',
  25780. ' var Result = 0;',
  25781. ' $mod.THelper.Fly.call({',
  25782. ' p: this.GetField(),',
  25783. ' get: function () {',
  25784. ' return this.p;',
  25785. ' },',
  25786. ' set: function (v) {',
  25787. ' this.p = v;',
  25788. ' }',
  25789. ' }, 1);',
  25790. ' $mod.THelper.Fly.call({',
  25791. ' p: this.GetField(),',
  25792. ' get: function () {',
  25793. ' return this.p;',
  25794. ' },',
  25795. ' set: function (v) {',
  25796. ' this.p = v;',
  25797. ' }',
  25798. ' }, 5);',
  25799. ' $mod.THelper.Fly.call({',
  25800. ' p: this.GetField(),',
  25801. ' get: function () {',
  25802. ' return this.p;',
  25803. ' },',
  25804. ' set: function (v) {',
  25805. ' this.p = v;',
  25806. ' }',
  25807. ' }, 6);',
  25808. ' var $with = this.GetField();',
  25809. ' $mod.THelper.Fly.call({',
  25810. ' get: function () {',
  25811. ' return $with;',
  25812. ' },',
  25813. ' set: function (v) {',
  25814. ' $with = v;',
  25815. ' }',
  25816. ' }, 7);',
  25817. ' return Result;',
  25818. ' };',
  25819. '});',
  25820. 'rtl.createHelper(this, "THelper", null, function () {',
  25821. ' this.Fly = function (n) {',
  25822. ' };',
  25823. '});',
  25824. 'this.o = null;',
  25825. 'this.c = null;',
  25826. '']),
  25827. LinesToStr([ // $mod.$main
  25828. '$mod.THelper.Fly.call({',
  25829. ' p: $mod.o.$class.GetField(),',
  25830. ' get: function () {',
  25831. ' return this.p;',
  25832. ' },',
  25833. ' set: function (v) {',
  25834. ' this.p = v;',
  25835. ' }',
  25836. '}, 11);',
  25837. 'var $with = $mod.o;',
  25838. '$mod.THelper.Fly.call({',
  25839. ' p: $with.$class.GetField(),',
  25840. ' get: function () {',
  25841. ' return this.p;',
  25842. ' },',
  25843. ' set: function (v) {',
  25844. ' this.p = v;',
  25845. ' }',
  25846. '}, 12);',
  25847. 'var $with1 = $mod.o.$class.GetField();',
  25848. '$mod.THelper.Fly.call({',
  25849. ' get: function () {',
  25850. ' return $with1;',
  25851. ' },',
  25852. ' set: function (v) {',
  25853. ' $with1 = v;',
  25854. ' }',
  25855. '}, 13);',
  25856. '$mod.THelper.Fly.call({',
  25857. ' p: $mod.c.GetField(),',
  25858. ' get: function () {',
  25859. ' return this.p;',
  25860. ' },',
  25861. ' set: function (v) {',
  25862. ' this.p = v;',
  25863. ' }',
  25864. '}, 14);',
  25865. 'var $with2 = $mod.c;',
  25866. '$mod.THelper.Fly.call({',
  25867. ' p: $with2.GetField(),',
  25868. ' get: function () {',
  25869. ' return this.p;',
  25870. ' },',
  25871. ' set: function (v) {',
  25872. ' this.p = v;',
  25873. ' }',
  25874. '}, 15);',
  25875. 'var $with3 = $mod.c.GetField();',
  25876. '$mod.THelper.Fly.call({',
  25877. ' get: function () {',
  25878. ' return $with3;',
  25879. ' },',
  25880. ' set: function (v) {',
  25881. ' $with3 = v;',
  25882. ' }',
  25883. '}, 16);',
  25884. '']));
  25885. end;
  25886. procedure TTestModule.TestTypeHelper_Property;
  25887. begin
  25888. StartProgram(false);
  25889. Add([
  25890. '{$modeswitch typehelpers}',
  25891. 'type',
  25892. ' THelper = type helper for word',
  25893. ' function GetSize: longint;',
  25894. ' procedure SetSize(Value: longint);',
  25895. ' property Size: longint read GetSize write SetSize;',
  25896. ' end;',
  25897. 'function THelper.GetSize: longint;',
  25898. 'begin',
  25899. ' Result:=Size+1;',
  25900. ' Size:=2;',
  25901. ' Result:=Self.Size+3;',
  25902. ' Self.Size:=4;',
  25903. ' with Self do begin',
  25904. ' Result:=Size+5;',
  25905. ' Size:=6;',
  25906. ' end;',
  25907. 'end;',
  25908. 'procedure THelper.SetSize(Value: longint);',
  25909. 'begin',
  25910. 'end;',
  25911. 'var w: word;',
  25912. 'begin',
  25913. ' w:=w.Size+7;',
  25914. ' w.Size:=w+8;',
  25915. ' with w do begin',
  25916. ' w:=Size+9;',
  25917. ' Size:=w+10;',
  25918. ' end;',
  25919. '']);
  25920. ConvertProgram;
  25921. CheckSource('TestTypeHelper_Property',
  25922. LinesToStr([ // statements
  25923. 'rtl.createHelper(this, "THelper", null, function () {',
  25924. ' this.GetSize = function () {',
  25925. ' var Result = 0;',
  25926. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25927. ' $mod.THelper.SetSize.call(this, 2);',
  25928. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25929. ' $mod.THelper.SetSize.call(this, 4);',
  25930. ' var $with = this.get();',
  25931. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25932. ' $mod.THelper.SetSize.call(this, 6);',
  25933. ' return Result;',
  25934. ' };',
  25935. ' this.SetSize = function (Value) {',
  25936. ' };',
  25937. '});',
  25938. 'this.w = 0;',
  25939. '']),
  25940. LinesToStr([ // $mod.$main
  25941. '$mod.w = $mod.THelper.GetSize.call({',
  25942. ' p: $mod,',
  25943. ' get: function () {',
  25944. ' return this.p.w;',
  25945. ' },',
  25946. ' set: function (v) {',
  25947. ' this.p.w = v;',
  25948. ' }',
  25949. '}) + 7;',
  25950. '$mod.THelper.SetSize.call({',
  25951. ' p: $mod,',
  25952. ' get: function () {',
  25953. ' return this.p.w;',
  25954. ' },',
  25955. ' set: function (v) {',
  25956. ' this.p.w = v;',
  25957. ' }',
  25958. '}, $mod.w + 8);',
  25959. 'var $with = $mod.w;',
  25960. '$mod.w = $mod.THelper.GetSize.call({',
  25961. ' get: function () {',
  25962. ' return $with;',
  25963. ' },',
  25964. ' set: function (v) {',
  25965. ' $with = v;',
  25966. ' }',
  25967. '}) + 9;',
  25968. '$mod.THelper.SetSize.call({',
  25969. ' get: function () {',
  25970. ' return $with;',
  25971. ' },',
  25972. ' set: function (v) {',
  25973. ' $with = v;',
  25974. ' }',
  25975. '}, $mod.w + 10);',
  25976. '']));
  25977. end;
  25978. procedure TTestModule.TestTypeHelper_Property_Array;
  25979. begin
  25980. StartProgram(false);
  25981. Add([
  25982. '{$modeswitch typehelpers}',
  25983. 'type',
  25984. ' THelper = type helper for word',
  25985. ' function GetItems(Index: byte): boolean;',
  25986. ' procedure SetItems(Index: byte; Value: boolean);',
  25987. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  25988. ' end;',
  25989. 'function THelper.GetItems(Index: byte): boolean;',
  25990. 'begin',
  25991. ' Result:=Items[1];',
  25992. ' Items[2]:=false;',
  25993. ' Result:=Self.Items[3];',
  25994. ' Self.Items[4]:=true;',
  25995. ' with Self do begin',
  25996. ' Result:=Items[5];',
  25997. ' Items[6]:=false;',
  25998. ' end;',
  25999. 'end;',
  26000. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26001. 'begin',
  26002. 'end;',
  26003. 'var',
  26004. ' w: word;',
  26005. ' b: boolean;',
  26006. 'begin',
  26007. ' b:=w.Items[1];',
  26008. ' w.Items[2]:=b;',
  26009. ' with w do begin',
  26010. ' b:=Items[3];',
  26011. ' Items[4]:=b;',
  26012. ' end;',
  26013. '']);
  26014. ConvertProgram;
  26015. CheckSource('TestTypeHelper_Property_Array',
  26016. LinesToStr([ // statements
  26017. 'rtl.createHelper(this, "THelper", null, function () {',
  26018. ' this.GetItems = function (Index) {',
  26019. ' var Result = false;',
  26020. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26021. ' $mod.THelper.SetItems.call(this, 2, false);',
  26022. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26023. ' $mod.THelper.SetItems.call(this, 4, true);',
  26024. ' var $with = this.get();',
  26025. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26026. ' $mod.THelper.SetItems.call(this, 6, false);',
  26027. ' return Result;',
  26028. ' };',
  26029. ' this.SetItems = function (Index, Value) {',
  26030. ' };',
  26031. '});',
  26032. 'this.w = 0;',
  26033. 'this.b = false;',
  26034. '']),
  26035. LinesToStr([ // $mod.$main
  26036. '$mod.b = $mod.THelper.GetItems.call({',
  26037. ' p: $mod,',
  26038. ' get: function () {',
  26039. ' return this.p.w;',
  26040. ' },',
  26041. ' set: function (v) {',
  26042. ' this.p.w = v;',
  26043. ' }',
  26044. '}, 1);',
  26045. '$mod.THelper.SetItems.call({',
  26046. ' p: $mod,',
  26047. ' get: function () {',
  26048. ' return this.p.w;',
  26049. ' },',
  26050. ' set: function (v) {',
  26051. ' this.p.w = v;',
  26052. ' }',
  26053. '}, 2, $mod.b);',
  26054. 'var $with = $mod.w;',
  26055. '$mod.b = $mod.THelper.GetItems.call({',
  26056. ' get: function () {',
  26057. ' return $with;',
  26058. ' },',
  26059. ' set: function (v) {',
  26060. ' $with = v;',
  26061. ' }',
  26062. '}, 3);',
  26063. '$mod.THelper.SetItems.call({',
  26064. ' get: function () {',
  26065. ' return $with;',
  26066. ' },',
  26067. ' set: function (v) {',
  26068. ' $with = v;',
  26069. ' }',
  26070. '}, 4, $mod.b);',
  26071. '']));
  26072. end;
  26073. procedure TTestModule.TestTypeHelper_ClassProperty;
  26074. begin
  26075. StartProgram(false);
  26076. Add([
  26077. '{$modeswitch typehelpers}',
  26078. 'type',
  26079. ' THelper = type helper for word',
  26080. ' class function GetSize: longint; static;',
  26081. ' class procedure SetSize(Value: longint); static;',
  26082. ' class property Size: longint read GetSize write SetSize;',
  26083. ' end;',
  26084. 'class function THelper.GetSize: longint;',
  26085. 'begin',
  26086. ' Result:=Size+1;',
  26087. ' Size:=2;',
  26088. 'end;',
  26089. 'class procedure THelper.SetSize(Value: longint);',
  26090. 'begin',
  26091. 'end;',
  26092. 'begin',
  26093. '']);
  26094. ConvertProgram;
  26095. CheckSource('TestTypeHelper_ClassProperty',
  26096. LinesToStr([ // statements
  26097. 'rtl.createHelper(this, "THelper", null, function () {',
  26098. ' this.GetSize = function () {',
  26099. ' var Result = 0;',
  26100. ' Result = $mod.THelper.GetSize() + 1;',
  26101. ' $mod.THelper.SetSize(2);',
  26102. ' return Result;',
  26103. ' };',
  26104. ' this.SetSize = function (Value) {',
  26105. ' };',
  26106. '});',
  26107. '']),
  26108. LinesToStr([ // $mod.$main
  26109. '']));
  26110. end;
  26111. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26112. begin
  26113. StartProgram(false);
  26114. Add([
  26115. '{$modeswitch typehelpers}',
  26116. 'type',
  26117. ' THelper = type helper for word',
  26118. ' class function GetItems(Index: byte): boolean; static;',
  26119. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26120. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26121. ' end;',
  26122. 'class function THelper.GetItems(Index: byte): boolean;',
  26123. 'begin',
  26124. ' Result:=Items[1];',
  26125. ' Items[2]:=false;',
  26126. 'end;',
  26127. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26128. 'begin',
  26129. 'end;',
  26130. 'var',
  26131. ' w: word;',
  26132. ' b: boolean;',
  26133. 'begin',
  26134. ' b:=w.Items[1];',
  26135. ' w.Items[2]:=b;',
  26136. ' with w do begin',
  26137. ' b:=Items[3];',
  26138. ' Items[4]:=b;',
  26139. ' end;',
  26140. '']);
  26141. ConvertProgram;
  26142. CheckSource('TestTypeHelper_ClassProperty_Array',
  26143. LinesToStr([ // statements
  26144. 'rtl.createHelper(this, "THelper", null, function () {',
  26145. ' this.GetItems = function (Index) {',
  26146. ' var Result = false;',
  26147. ' Result = $mod.THelper.GetItems(1);',
  26148. ' $mod.THelper.SetItems(2, false);',
  26149. ' return Result;',
  26150. ' };',
  26151. ' this.SetItems = function (Index, Value) {',
  26152. ' };',
  26153. '});',
  26154. 'this.w = 0;',
  26155. 'this.b = false;',
  26156. '']),
  26157. LinesToStr([ // $mod.$main
  26158. '$mod.b = $mod.THelper.GetItems(1);',
  26159. '$mod.THelper.SetItems(2, $mod.b);',
  26160. 'var $with = $mod.w;',
  26161. '$mod.b = $mod.THelper.GetItems(3);',
  26162. '$mod.THelper.SetItems(4, $mod.b);',
  26163. '']));
  26164. end;
  26165. procedure TTestModule.TestTypeHelper_ClassMethod;
  26166. begin
  26167. StartProgram(false);
  26168. Add([
  26169. '{$modeswitch typehelpers}',
  26170. 'type',
  26171. ' THelper = type helper for word',
  26172. ' class procedure DoStatic; static;',
  26173. ' end;',
  26174. 'class procedure THelper.DoStatic;',
  26175. 'begin',
  26176. ' DoStatic;',
  26177. ' DoStatic();',
  26178. 'end;',
  26179. 'var w: word;',
  26180. 'begin',
  26181. ' w.DoStatic;',
  26182. ' w.DoStatic();',
  26183. '']);
  26184. ConvertProgram;
  26185. CheckSource('TestTypeHelper_ClassMethod',
  26186. LinesToStr([ // statements
  26187. 'rtl.createHelper(this, "THelper", null, function () {',
  26188. ' this.DoStatic = function () {',
  26189. ' $mod.THelper.DoStatic();',
  26190. ' $mod.THelper.DoStatic();',
  26191. ' };',
  26192. '});',
  26193. 'this.w = 0;',
  26194. '']),
  26195. LinesToStr([ // $mod.$main
  26196. '$mod.THelper.DoStatic();',
  26197. '$mod.THelper.DoStatic();',
  26198. '']));
  26199. end;
  26200. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26201. begin
  26202. StartProgram(false);
  26203. Add([
  26204. '{$modeswitch typehelpers}',
  26205. 'type',
  26206. ' THelper = type helper for word',
  26207. ' procedure Run; external name ''Run'';',
  26208. ' end;',
  26209. 'var w: word;',
  26210. 'begin',
  26211. ' w.Run;',
  26212. '']);
  26213. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26214. ConvertProgram;
  26215. end;
  26216. procedure TTestModule.TestTypeHelper_Constructor;
  26217. begin
  26218. StartProgram(false);
  26219. Add([
  26220. '{$modeswitch typehelpers}',
  26221. 'type',
  26222. ' THelper = type helper for word',
  26223. ' constructor Init(e: longint);',
  26224. ' end;',
  26225. 'constructor THelper.Init(e: longint);',
  26226. 'begin',
  26227. ' Self:=e;',
  26228. ' Init(e+1);',
  26229. 'end;',
  26230. 'var w: word;',
  26231. 'begin',
  26232. ' w:=word.Init(2);',
  26233. ' w:=w.Init(3);',
  26234. ' with word do w:=Init(4);',
  26235. ' with w do w:=Init(5);',
  26236. '']);
  26237. ConvertProgram;
  26238. CheckSource('TestTypeHelper_Constructor',
  26239. LinesToStr([ // statements
  26240. 'rtl.createHelper(this, "THelper", null, function () {',
  26241. ' this.Init = function (e) {',
  26242. ' this.set(e);',
  26243. ' $mod.THelper.Init.call(this, e + 1);',
  26244. ' return this.get();',
  26245. ' };',
  26246. ' this.$new = function (fn, args) {',
  26247. ' return this[fn].apply({',
  26248. ' p: 0,',
  26249. ' get: function () {',
  26250. ' return this.p;',
  26251. ' },',
  26252. ' set: function (v) {',
  26253. ' this.p = v;',
  26254. ' }',
  26255. ' }, args);',
  26256. ' };',
  26257. '});',
  26258. 'this.w = 0;',
  26259. '']),
  26260. LinesToStr([ // $mod.$main
  26261. '$mod.w = $mod.THelper.$new("Init", [2]);',
  26262. '$mod.w = $mod.THelper.Init.call({',
  26263. ' p: $mod,',
  26264. ' get: function () {',
  26265. ' return this.p.w;',
  26266. ' },',
  26267. ' set: function (v) {',
  26268. ' this.p.w = v;',
  26269. ' }',
  26270. '}, 3);',
  26271. '$mod.w = $mod.THelper.$new("Init", [4]);',
  26272. 'var $with = $mod.w;',
  26273. '$mod.w = $mod.THelper.Init.call({',
  26274. ' get: function () {',
  26275. ' return $with;',
  26276. ' },',
  26277. ' set: function (v) {',
  26278. ' $with = v;',
  26279. ' }',
  26280. '}, 5);',
  26281. '']));
  26282. end;
  26283. procedure TTestModule.TestTypeHelper_Word;
  26284. begin
  26285. StartProgram(false);
  26286. Add([
  26287. '{$modeswitch typehelpers}',
  26288. 'type',
  26289. ' THelper = type helper for word',
  26290. ' procedure DoIt(e: byte = 123);',
  26291. ' end;',
  26292. 'procedure THelper.DoIt(e: byte);',
  26293. 'begin',
  26294. ' Self:=e;',
  26295. ' Self:=Self+1;',
  26296. ' with Self do Doit;',
  26297. 'end;',
  26298. 'begin',
  26299. ' word(3).DoIt;',
  26300. '']);
  26301. ConvertProgram;
  26302. CheckSource('TestTypeHelper_Word',
  26303. LinesToStr([ // statements
  26304. 'rtl.createHelper(this, "THelper", null, function () {',
  26305. ' this.DoIt = function (e) {',
  26306. ' this.set(e);',
  26307. ' this.set(this.get() + 1);',
  26308. ' var $with = this.get();',
  26309. ' $mod.THelper.DoIt.call(this, 123);',
  26310. ' };',
  26311. '});',
  26312. '']),
  26313. LinesToStr([ // $mod.$main
  26314. '$mod.THelper.DoIt.call({',
  26315. ' get: function () {',
  26316. ' return 3;',
  26317. ' },',
  26318. ' set: function (v) {',
  26319. ' rtl.raiseE("EPropReadOnly");',
  26320. ' }',
  26321. '}, 123);',
  26322. '']));
  26323. end;
  26324. procedure TTestModule.TestTypeHelper_Boolean;
  26325. begin
  26326. StartProgram(false);
  26327. Add([
  26328. '{$modeswitch typehelpers}',
  26329. 'type',
  26330. ' Integer = longint;',
  26331. ' THelper = type helper for boolean',
  26332. ' procedure Run(e: wordbool = true);',
  26333. ' end;',
  26334. 'procedure THelper.Run(e: wordbool);',
  26335. 'begin',
  26336. ' Self:=e;',
  26337. ' Self:=not Self;',
  26338. ' with Self do Run;',
  26339. ' if Integer(Self)=0 then ;',
  26340. 'end;',
  26341. 'begin',
  26342. ' boolean(3).Run;',
  26343. '']);
  26344. ConvertProgram;
  26345. CheckSource('TestTypeHelper_Boolean',
  26346. LinesToStr([ // statements
  26347. 'rtl.createHelper(this, "THelper", null, function () {',
  26348. ' this.Run = function (e) {',
  26349. ' this.set(e);',
  26350. ' this.set(!this.get());',
  26351. ' var $with = this.get();',
  26352. ' $mod.THelper.Run.call(this, true);',
  26353. ' if ((this.get() ? 1 : 0) === 0) ;',
  26354. ' };',
  26355. '});',
  26356. '']),
  26357. LinesToStr([ // $mod.$main
  26358. '$mod.THelper.Run.call({',
  26359. ' a: 3 != 0,',
  26360. ' get: function () {',
  26361. ' return this.a;',
  26362. ' },',
  26363. ' set: function (v) {',
  26364. ' rtl.raiseE("EPropReadOnly");',
  26365. ' }',
  26366. '}, true);',
  26367. '']));
  26368. end;
  26369. procedure TTestModule.TestTypeHelper_WordBool;
  26370. begin
  26371. StartProgram(false);
  26372. Add([
  26373. '{$modeswitch typehelpers}',
  26374. 'type',
  26375. ' Integer = longint;',
  26376. ' THelper = type helper for WordBool',
  26377. ' procedure Run(e: wordbool = true);',
  26378. ' end;',
  26379. 'procedure THelper.Run(e: wordbool);',
  26380. 'var i: integer;',
  26381. 'begin',
  26382. ' i:=Integer(Self);',
  26383. 'end;',
  26384. 'var w: wordbool;',
  26385. 'begin',
  26386. ' w.Run;',
  26387. ' wordbool(3).Run;',
  26388. '']);
  26389. ConvertProgram;
  26390. CheckSource('TestTypeHelper_WordBool',
  26391. LinesToStr([ // statements
  26392. 'rtl.createHelper(this, "THelper", null, function () {',
  26393. ' this.Run = function (e) {',
  26394. ' var i = 0;',
  26395. ' i = (this.get() ? 1 : 0);',
  26396. ' };',
  26397. '});',
  26398. 'this.w = false;',
  26399. '']),
  26400. LinesToStr([ // $mod.$main
  26401. '$mod.THelper.Run.call({',
  26402. ' p: $mod,',
  26403. ' get: function () {',
  26404. ' return this.p.w;',
  26405. ' },',
  26406. ' set: function (v) {',
  26407. ' this.p.w = v;',
  26408. ' }',
  26409. '}, true);',
  26410. '$mod.THelper.Run.call({',
  26411. ' a: 3 != 0,',
  26412. ' get: function () {',
  26413. ' return this.a;',
  26414. ' },',
  26415. ' set: function (v) {',
  26416. ' rtl.raiseE("EPropReadOnly");',
  26417. ' }',
  26418. '}, true);',
  26419. '']));
  26420. end;
  26421. procedure TTestModule.TestTypeHelper_Double;
  26422. begin
  26423. StartProgram(false);
  26424. Add([
  26425. '{$modeswitch typehelpers}',
  26426. 'type',
  26427. ' Float = type double;',
  26428. ' THelper = type helper for Float',
  26429. ' const NPI = 3.141592;',
  26430. ' function ToStr: String;',
  26431. ' end;',
  26432. 'function THelper.ToStr: String;',
  26433. 'begin',
  26434. 'end;',
  26435. 'procedure DoIt(s: string);',
  26436. 'begin',
  26437. 'end;',
  26438. 'var f: Float;',
  26439. 'begin',
  26440. ' DoIt(f.toStr);',
  26441. ' DoIt(f.toStr());',
  26442. ' (f*f).toStr;',
  26443. ' DoIt((f*f).toStr);',
  26444. '']);
  26445. ConvertProgram;
  26446. CheckSource('TestTypeHelper_Double',
  26447. LinesToStr([ // statements
  26448. 'rtl.createHelper(this, "THelper", null, function () {',
  26449. ' this.NPI = 3.141592;',
  26450. ' this.ToStr = function () {',
  26451. ' var Result = "";',
  26452. ' return Result;',
  26453. ' };',
  26454. '});',
  26455. 'this.DoIt = function (s) {',
  26456. '};',
  26457. 'this.f = 0.0;',
  26458. '']),
  26459. LinesToStr([ // $mod.$main
  26460. '$mod.DoIt($mod.THelper.ToStr.call({',
  26461. ' p: $mod,',
  26462. ' get: function () {',
  26463. ' return this.p.f;',
  26464. ' },',
  26465. ' set: function (v) {',
  26466. ' this.p.f = v;',
  26467. ' }',
  26468. '}));',
  26469. '$mod.DoIt($mod.THelper.ToStr.call({',
  26470. ' p: $mod,',
  26471. ' get: function () {',
  26472. ' return this.p.f;',
  26473. ' },',
  26474. ' set: function (v) {',
  26475. ' this.p.f = v;',
  26476. ' }',
  26477. '}));',
  26478. '$mod.THelper.ToStr.call({',
  26479. ' a: $mod.f * $mod.f,',
  26480. ' get: function () {',
  26481. ' return this.a;',
  26482. ' },',
  26483. ' set: function (v) {',
  26484. ' rtl.raiseE("EPropReadOnly");',
  26485. ' }',
  26486. '});',
  26487. '$mod.DoIt($mod.THelper.ToStr.call({',
  26488. ' a: $mod.f * $mod.f,',
  26489. ' get: function () {',
  26490. ' return this.a;',
  26491. ' },',
  26492. ' set: function (v) {',
  26493. ' rtl.raiseE("EPropReadOnly");',
  26494. ' }',
  26495. '}));',
  26496. '']));
  26497. end;
  26498. procedure TTestModule.TestTypeHelper_NativeInt;
  26499. begin
  26500. StartProgram(false);
  26501. Add([
  26502. '{$modeswitch typehelpers}',
  26503. 'type',
  26504. ' MaxInt = type nativeint;',
  26505. ' THelperI = type helper for MaxInt',
  26506. ' function ToStr: String;',
  26507. ' end;',
  26508. ' MaxUInt = type nativeuint;',
  26509. ' THelperU = type helper for MaxUInt',
  26510. ' function ToStr: String;',
  26511. ' end;',
  26512. 'function THelperI.ToStr: String;',
  26513. 'begin',
  26514. ' Result:=str(Self);',
  26515. 'end;',
  26516. 'function THelperU.ToStr: String;',
  26517. 'begin',
  26518. ' Result:=str(Self);',
  26519. 'end;',
  26520. 'procedure DoIt(s: string);',
  26521. 'begin',
  26522. 'end;',
  26523. 'var i: MaxInt;',
  26524. 'begin',
  26525. ' DoIt(i.toStr);',
  26526. ' DoIt(i.toStr());',
  26527. ' (i*i).toStr;',
  26528. ' DoIt((i*i).toStr);',
  26529. '']);
  26530. ConvertProgram;
  26531. CheckSource('TestTypeHelper_NativeInt',
  26532. LinesToStr([ // statements
  26533. 'rtl.createHelper(this, "THelperI", null, function () {',
  26534. ' this.ToStr = function () {',
  26535. ' var Result = "";',
  26536. ' Result = "" + this.get();',
  26537. ' return Result;',
  26538. ' };',
  26539. '});',
  26540. 'rtl.createHelper(this, "THelperU", null, function () {',
  26541. ' this.ToStr = function () {',
  26542. ' var Result = "";',
  26543. ' Result = "" + this.get();',
  26544. ' return Result;',
  26545. ' };',
  26546. '});',
  26547. 'this.DoIt = function (s) {',
  26548. '};',
  26549. 'this.i = 0;',
  26550. '']),
  26551. LinesToStr([ // $mod.$main
  26552. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26553. ' p: $mod,',
  26554. ' get: function () {',
  26555. ' return this.p.i;',
  26556. ' },',
  26557. ' set: function (v) {',
  26558. ' this.p.i = v;',
  26559. ' }',
  26560. '}));',
  26561. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26562. ' p: $mod,',
  26563. ' get: function () {',
  26564. ' return this.p.i;',
  26565. ' },',
  26566. ' set: function (v) {',
  26567. ' this.p.i = v;',
  26568. ' }',
  26569. '}));',
  26570. '$mod.THelperI.ToStr.call({',
  26571. ' a: $mod.i * $mod.i,',
  26572. ' get: function () {',
  26573. ' return this.a;',
  26574. ' },',
  26575. ' set: function (v) {',
  26576. ' rtl.raiseE("EPropReadOnly");',
  26577. ' }',
  26578. '});',
  26579. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26580. ' a: $mod.i * $mod.i,',
  26581. ' get: function () {',
  26582. ' return this.a;',
  26583. ' },',
  26584. ' set: function (v) {',
  26585. ' rtl.raiseE("EPropReadOnly");',
  26586. ' }',
  26587. '}));',
  26588. '']));
  26589. end;
  26590. procedure TTestModule.TestTypeHelper_StringChar;
  26591. begin
  26592. StartProgram(false);
  26593. Add([
  26594. '{$modeswitch typehelpers}',
  26595. 'type',
  26596. ' TStringHelper = type helper for string',
  26597. ' procedure DoIt(e: byte = 123);',
  26598. ' end;',
  26599. ' TCharHelper = type helper for char',
  26600. ' procedure Fly;',
  26601. ' end;',
  26602. 'procedure TStringHelper.DoIt(e: byte);',
  26603. 'begin',
  26604. ' Self[1]:=''c'';',
  26605. ' Self[2]:=Self[3];',
  26606. 'end;',
  26607. 'procedure TCharHelper.Fly;',
  26608. 'begin',
  26609. ' Self:=''c'';',
  26610. 'end;',
  26611. 'begin',
  26612. ' ''abc''.DoIt;',
  26613. ' ''xyz''.DoIt();',
  26614. ' ''c''.Fly();',
  26615. '']);
  26616. ConvertProgram;
  26617. CheckSource('TestTypeHelper_StringChar',
  26618. LinesToStr([ // statements
  26619. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26620. ' this.DoIt = function (e) {',
  26621. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26622. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26623. ' };',
  26624. '});',
  26625. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26626. ' this.Fly = function () {',
  26627. ' this.set("c");',
  26628. ' };',
  26629. '});',
  26630. '']),
  26631. LinesToStr([ // $mod.$main
  26632. '$mod.TStringHelper.DoIt.call({',
  26633. ' get: function () {',
  26634. ' return "abc";',
  26635. ' },',
  26636. ' set: function (v) {',
  26637. ' rtl.raiseE("EPropReadOnly");',
  26638. ' }',
  26639. '}, 123);',
  26640. '$mod.TStringHelper.DoIt.call({',
  26641. ' get: function () {',
  26642. ' return "xyz";',
  26643. ' },',
  26644. ' set: function (v) {',
  26645. ' rtl.raiseE("EPropReadOnly");',
  26646. ' }',
  26647. '}, 123);',
  26648. '$mod.TCharHelper.Fly.call({',
  26649. ' get: function () {',
  26650. ' return "c";',
  26651. ' },',
  26652. ' set: function (v) {',
  26653. ' rtl.raiseE("EPropReadOnly");',
  26654. ' }',
  26655. '});',
  26656. '']));
  26657. end;
  26658. procedure TTestModule.TestTypeHelper_JSValue;
  26659. begin
  26660. StartProgram(false);
  26661. Add([
  26662. '{$modeswitch typehelpers}',
  26663. 'type',
  26664. ' TExtValue = type jsvalue;',
  26665. ' THelper = type helper for TExtValue',
  26666. ' function ToStr: String;',
  26667. ' end;',
  26668. 'function THelper.ToStr: String;',
  26669. 'begin',
  26670. 'end;',
  26671. 'var',
  26672. ' s: string;',
  26673. ' v: TExtValue;',
  26674. 'begin',
  26675. ' s:=v.toStr;',
  26676. ' s:=v.toStr();',
  26677. ' TExtValue(s).toStr;',
  26678. '']);
  26679. ConvertProgram;
  26680. CheckSource('TestTypeHelper_JSValue',
  26681. LinesToStr([ // statements
  26682. 'rtl.createHelper(this, "THelper", null, function () {',
  26683. ' this.ToStr = function () {',
  26684. ' var Result = "";',
  26685. ' return Result;',
  26686. ' };',
  26687. '});',
  26688. 'this.s = "";',
  26689. 'this.v = undefined;',
  26690. '']),
  26691. LinesToStr([ // $mod.$main
  26692. '$mod.s = $mod.THelper.ToStr.call({',
  26693. ' p: $mod,',
  26694. ' get: function () {',
  26695. ' return this.p.v;',
  26696. ' },',
  26697. ' set: function (v) {',
  26698. ' this.p.v = v;',
  26699. ' }',
  26700. '});',
  26701. '$mod.s = $mod.THelper.ToStr.call({',
  26702. ' p: $mod,',
  26703. ' get: function () {',
  26704. ' return this.p.v;',
  26705. ' },',
  26706. ' set: function (v) {',
  26707. ' this.p.v = v;',
  26708. ' }',
  26709. '});',
  26710. '$mod.THelper.ToStr.call({',
  26711. ' p: $mod,',
  26712. ' get: function () {',
  26713. ' return this.p.s;',
  26714. ' },',
  26715. ' set: function (v) {',
  26716. ' rtl.raiseE("EPropReadOnly");',
  26717. ' }',
  26718. '});',
  26719. '']));
  26720. end;
  26721. procedure TTestModule.TestTypeHelper_Array;
  26722. begin
  26723. StartProgram(false);
  26724. Add([
  26725. '{$modeswitch typehelpers}',
  26726. 'type',
  26727. ' TArrOfBool = array of boolean;',
  26728. ' TArrOfJS = array of jsvalue;',
  26729. ' THelper = type helper for TArrOfBool',
  26730. ' procedure DoIt(e: byte = 123);',
  26731. ' end;',
  26732. 'procedure THelper.DoIt(e: byte);',
  26733. 'begin',
  26734. ' Self[1]:=true;',
  26735. ' Self[2]:=not Self[3];',
  26736. ' SetLength(Self,4);',
  26737. ' Self:=Concat(Self,[true]);',
  26738. 'end;',
  26739. 'var',
  26740. ' b: TArrOfBool;',
  26741. ' j: TArrOfJS;',
  26742. 'begin',
  26743. ' b.DoIt;',
  26744. ' TArrOfBool(j).DoIt();',
  26745. '']);
  26746. ConvertProgram;
  26747. CheckSource('TestTypeHelper_Array',
  26748. LinesToStr([ // statements
  26749. 'rtl.createHelper(this, "THelper", null, function () {',
  26750. ' this.DoIt = function (e) {',
  26751. ' this.get()[1] = true;',
  26752. ' this.get()[2] = !this.get()[3];',
  26753. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26754. ' this.set(rtl.arrayPushN(this.get(), true));',
  26755. ' };',
  26756. '});',
  26757. 'this.b = [];',
  26758. 'this.j = [];',
  26759. '']),
  26760. LinesToStr([ // $mod.$main
  26761. '$mod.THelper.DoIt.call({',
  26762. ' p: $mod,',
  26763. ' get: function () {',
  26764. ' return this.p.b;',
  26765. ' },',
  26766. ' set: function (v) {',
  26767. ' this.p.b = v;',
  26768. ' }',
  26769. '}, 123);',
  26770. '$mod.THelper.DoIt.call({',
  26771. ' p: $mod,',
  26772. ' get: function () {',
  26773. ' return this.p.j;',
  26774. ' },',
  26775. ' set: function (v) {',
  26776. ' this.p.j = v;',
  26777. ' }',
  26778. '}, 123);',
  26779. '']));
  26780. end;
  26781. procedure TTestModule.TestTypeHelper_EnumType;
  26782. begin
  26783. StartProgram(false);
  26784. Add([
  26785. '{$modeswitch typehelpers}',
  26786. 'type',
  26787. ' TEnum = (red,blue);',
  26788. ' THelper = type helper for TEnum',
  26789. ' procedure DoIt(e: byte = 123);',
  26790. ' class procedure Swing(w: word); static;',
  26791. ' end;',
  26792. 'procedure THelper.DoIt(e: byte);',
  26793. 'begin',
  26794. ' Self:=red;',
  26795. ' Self:=succ(Self);',
  26796. ' with Self do Doit;',
  26797. 'end;',
  26798. 'class procedure THelper.Swing(w: word);',
  26799. 'begin',
  26800. 'end;',
  26801. 'var e: TEnum;',
  26802. 'begin',
  26803. ' e.DoIt;',
  26804. ' red.DoIt;',
  26805. ' TEnum.blue.DoIt;',
  26806. ' TEnum(1).DoIt;',
  26807. ' TEnum.Swing(3);',
  26808. '']);
  26809. ConvertProgram;
  26810. CheckSource('TestTypeHelper_EnumType',
  26811. LinesToStr([ // statements
  26812. 'this.TEnum = {',
  26813. ' "0": "red",',
  26814. ' red: 0,',
  26815. ' "1": "blue",',
  26816. ' blue: 1',
  26817. '};',
  26818. 'rtl.createHelper(this, "THelper", null, function () {',
  26819. ' this.DoIt = function (e) {',
  26820. ' this.set($mod.TEnum.red);',
  26821. ' this.set(this.get() + 1);',
  26822. ' var $with = this.get();',
  26823. ' $mod.THelper.DoIt.call(this, 123);',
  26824. ' };',
  26825. ' this.Swing = function (w) {',
  26826. ' };',
  26827. '});',
  26828. 'this.e = 0;',
  26829. '']),
  26830. LinesToStr([ // $mod.$main
  26831. '$mod.THelper.DoIt.call({',
  26832. ' p: $mod,',
  26833. ' get: function () {',
  26834. ' return this.p.e;',
  26835. ' },',
  26836. ' set: function (v) {',
  26837. ' this.p.e = v;',
  26838. ' }',
  26839. '}, 123);',
  26840. '$mod.THelper.DoIt.call({',
  26841. ' p: $mod.TEnum,',
  26842. ' get: function () {',
  26843. ' return this.p.red;',
  26844. ' },',
  26845. ' set: function (v) {',
  26846. ' rtl.raiseE("EPropReadOnly");',
  26847. ' }',
  26848. '}, 123);',
  26849. '$mod.THelper.DoIt.call({',
  26850. ' p: $mod.TEnum,',
  26851. ' get: function () {',
  26852. ' return this.p.blue;',
  26853. ' },',
  26854. ' set: function (v) {',
  26855. ' rtl.raiseE("EPropReadOnly");',
  26856. ' }',
  26857. '}, 123);',
  26858. '$mod.THelper.DoIt.call({',
  26859. ' get: function () {',
  26860. ' return 1;',
  26861. ' },',
  26862. ' set: function (v) {',
  26863. ' rtl.raiseE("EPropReadOnly");',
  26864. ' }',
  26865. '}, 123);',
  26866. '$mod.THelper.Swing(3);',
  26867. '']));
  26868. end;
  26869. procedure TTestModule.TestTypeHelper_SetType;
  26870. begin
  26871. StartProgram(false);
  26872. Add([
  26873. '{$modeswitch typehelpers}',
  26874. 'type',
  26875. ' TEnum = (red,blue);',
  26876. ' TSetOfEnum = set of TEnum;',
  26877. ' THelper = type helper for TSetOfEnum',
  26878. ' procedure DoIt(e: byte = 123);',
  26879. ' constructor Init(e: TEnum);',
  26880. ' constructor InitEmpty;',
  26881. ' end;',
  26882. 'procedure THelper.DoIt(e: byte);',
  26883. 'begin',
  26884. ' Self:=[];',
  26885. ' Self:=[red];',
  26886. ' Include(Self,blue);',
  26887. 'end;',
  26888. 'constructor THelper.Init(e: TEnum);',
  26889. 'begin',
  26890. ' Self:=[];',
  26891. ' Self:=[e];',
  26892. ' Include(Self,blue);',
  26893. 'end;',
  26894. 'constructor THelper.InitEmpty;',
  26895. 'begin',
  26896. 'end;',
  26897. 'var s: TSetOfEnum;',
  26898. 'begin',
  26899. ' s.DoIt;',
  26900. //' [red].DoIt;',
  26901. //' with s do DoIt;',
  26902. //' with [red,blue] do DoIt;',
  26903. ' s:=TSetOfEnum.Init(blue);',
  26904. ' s:=s.Init(blue);',
  26905. '']);
  26906. ConvertProgram;
  26907. CheckSource('TestTypeHelper_SetType',
  26908. LinesToStr([ // statements
  26909. 'this.TEnum = {',
  26910. ' "0": "red",',
  26911. ' red: 0,',
  26912. ' "1": "blue",',
  26913. ' blue: 1',
  26914. '};',
  26915. 'rtl.createHelper(this, "THelper", null, function () {',
  26916. ' this.DoIt = function (e) {',
  26917. ' this.set({});',
  26918. ' this.set(rtl.createSet($mod.TEnum.red));',
  26919. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26920. ' };',
  26921. ' this.Init = function (e) {',
  26922. ' this.set({});',
  26923. ' this.set(rtl.createSet(e));',
  26924. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26925. ' return this.get();',
  26926. ' };',
  26927. ' this.InitEmpty = function () {',
  26928. ' return this.get();',
  26929. ' };',
  26930. ' this.$new = function (fn, args) {',
  26931. ' return this[fn].apply({',
  26932. ' p: {},',
  26933. ' get: function () {',
  26934. ' return this.p;',
  26935. ' },',
  26936. ' set: function (v) {',
  26937. ' this.p = v;',
  26938. ' }',
  26939. ' }, args);',
  26940. ' };',
  26941. '});',
  26942. 'this.s = {};',
  26943. '']),
  26944. LinesToStr([ // $mod.$main
  26945. '$mod.THelper.DoIt.call({',
  26946. ' p: $mod,',
  26947. ' get: function () {',
  26948. ' return this.p.s;',
  26949. ' },',
  26950. ' set: function (v) {',
  26951. ' this.p.s = v;',
  26952. ' }',
  26953. '}, 123);',
  26954. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  26955. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  26956. ' p: $mod,',
  26957. ' get: function () {',
  26958. ' return this.p.s;',
  26959. ' },',
  26960. ' set: function (v) {',
  26961. ' this.p.s = v;',
  26962. ' }',
  26963. '}, $mod.TEnum.blue));',
  26964. '']));
  26965. end;
  26966. procedure TTestModule.TestTypeHelper_InterfaceType;
  26967. begin
  26968. StartProgram(false);
  26969. Add([
  26970. '{$interfaces com}',
  26971. '{$modeswitch typehelpers}',
  26972. 'type',
  26973. ' IUnknown = interface',
  26974. ' function _AddRef: longint;',
  26975. ' function _Release: longint;',
  26976. ' end;',
  26977. ' TObject = class(IUnknown)',
  26978. ' function _AddRef: longint; virtual; abstract;',
  26979. ' function _Release: longint; virtual; abstract;',
  26980. ' end;',
  26981. ' THelper = type helper for IUnknown',
  26982. ' procedure Fly(e: byte = 123);',
  26983. ' class procedure Run; static;',
  26984. ' end;',
  26985. 'var',
  26986. ' i: IUnknown;',
  26987. ' o: TObject;',
  26988. 'procedure THelper.Fly(e: byte);',
  26989. 'begin',
  26990. ' i:=Self;',
  26991. ' o:=Self as TObject;',
  26992. ' Self:=nil;',
  26993. ' Self:=i;',
  26994. ' Self:=o;',
  26995. ' with Self do begin',
  26996. ' Fly;',
  26997. ' Fly();',
  26998. ' end;',
  26999. 'end;',
  27000. 'class procedure THelper.Run;',
  27001. 'var l: IUnknown;',
  27002. 'begin',
  27003. ' l.Fly;',
  27004. ' l.Fly();',
  27005. 'end;',
  27006. 'begin',
  27007. ' i.Fly;',
  27008. ' i.Fly();',
  27009. ' i.Run;',
  27010. ' i.Run();',
  27011. ' IUnknown.Run;',
  27012. ' IUnknown.Run();',
  27013. '']);
  27014. ConvertProgram;
  27015. CheckSource('TestTypeHelper_InterfaceType',
  27016. LinesToStr([ // statements
  27017. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27018. 'rtl.createClass(this, "TObject", null, function () {',
  27019. ' this.$init = function () {',
  27020. ' };',
  27021. ' this.$final = function () {',
  27022. ' };',
  27023. ' rtl.addIntf(this, $mod.IUnknown);',
  27024. '});',
  27025. 'rtl.createHelper(this, "THelper", null, function () {',
  27026. ' this.Fly = function (e) {',
  27027. ' var $ir = rtl.createIntfRefs();',
  27028. ' try {',
  27029. ' rtl.setIntfP($mod, "i", this.get());',
  27030. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27031. ' this.set(null);',
  27032. ' this.set($mod.i);',
  27033. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27034. ' var $with = this.get();',
  27035. ' $mod.THelper.Fly.call(this, 123);',
  27036. ' $mod.THelper.Fly.call(this, 123);',
  27037. ' } finally {',
  27038. ' $ir.free();',
  27039. ' };',
  27040. ' };',
  27041. ' this.Run = function () {',
  27042. ' var l = null;',
  27043. ' try {',
  27044. ' $mod.THelper.Fly.call({',
  27045. ' get: function () {',
  27046. ' return l;',
  27047. ' },',
  27048. ' set: function (v) {',
  27049. ' l = rtl.setIntfL(l, v);',
  27050. ' }',
  27051. ' }, 123);',
  27052. ' $mod.THelper.Fly.call({',
  27053. ' get: function () {',
  27054. ' return l;',
  27055. ' },',
  27056. ' set: function (v) {',
  27057. ' l = rtl.setIntfL(l, v);',
  27058. ' }',
  27059. ' }, 123);',
  27060. ' } finally {',
  27061. ' rtl._Release(l);',
  27062. ' };',
  27063. ' };',
  27064. '});',
  27065. 'this.i = null;',
  27066. 'this.o = null;',
  27067. '']),
  27068. LinesToStr([ // $mod.$main
  27069. '$mod.THelper.Fly.call({',
  27070. ' p: $mod,',
  27071. ' get: function () {',
  27072. ' return this.p.i;',
  27073. ' },',
  27074. ' set: function (v) {',
  27075. ' rtl.setIntfP(this.p, "i", v);',
  27076. ' }',
  27077. '}, 123);',
  27078. '$mod.THelper.Fly.call({',
  27079. ' p: $mod,',
  27080. ' get: function () {',
  27081. ' return this.p.i;',
  27082. ' },',
  27083. ' set: function (v) {',
  27084. ' rtl.setIntfP(this.p, "i", v);',
  27085. ' }',
  27086. '}, 123);',
  27087. '$mod.THelper.Run();',
  27088. '$mod.THelper.Run();',
  27089. '$mod.THelper.Run();',
  27090. '$mod.THelper.Run();',
  27091. '']));
  27092. end;
  27093. procedure TTestModule.TestTypeHelper_NestedSelf;
  27094. begin
  27095. StartProgram(false);
  27096. Add([
  27097. '{$modeswitch typehelpers}',
  27098. 'type',
  27099. ' THelper = type helper for string',
  27100. ' procedure Run(Value: string);',
  27101. ' end;',
  27102. 'procedure THelper.Run(Value: string);',
  27103. ' function Sub(i: nativeint): boolean;',
  27104. ' begin',
  27105. ' Result:=Self[i+1]=Value[i];',
  27106. ' end;',
  27107. 'begin',
  27108. ' if Self[3]=Value[4] then ;',
  27109. 'end;',
  27110. 'begin',
  27111. '']);
  27112. ConvertProgram;
  27113. CheckSource('TestTypeHelper_NestedSelf',
  27114. LinesToStr([ // statements
  27115. 'rtl.createHelper(this, "THelper", null, function () {',
  27116. ' this.Run = function (Value) {',
  27117. ' var $Self = this;',
  27118. ' function Sub(i) {',
  27119. ' var Result = false;',
  27120. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27121. ' return Result;',
  27122. ' };',
  27123. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27124. ' };',
  27125. '});',
  27126. '']),
  27127. LinesToStr([ // $mod.$main
  27128. '']));
  27129. end;
  27130. procedure TTestModule.TestProcType;
  27131. begin
  27132. StartProgram(false);
  27133. Add([
  27134. 'type',
  27135. ' TProcInt = procedure(vI: longint = 1);',
  27136. 'procedure DoIt(vJ: longint);',
  27137. 'begin end;',
  27138. 'var',
  27139. ' b: boolean;',
  27140. ' vP, vQ: tprocint;',
  27141. 'begin',
  27142. ' vp:=nil;',
  27143. ' vp:=vp;',
  27144. ' vp:=@doit;',
  27145. ' vp;',
  27146. ' vp();',
  27147. ' vp(2);',
  27148. ' b:=vp=nil;',
  27149. ' b:=nil=vp;',
  27150. ' b:=vp=vq;',
  27151. ' b:=vp=@doit;',
  27152. ' b:=@doit=vp;',
  27153. ' b:=vp<>nil;',
  27154. ' b:=nil<>vp;',
  27155. ' b:=vp<>vq;',
  27156. ' b:=vp<>@doit;',
  27157. ' b:=@doit<>vp;',
  27158. ' b:=Assigned(vp);',
  27159. ' if Assigned(vp) then ;']);
  27160. ConvertProgram;
  27161. CheckSource('TestProcType',
  27162. LinesToStr([ // statements
  27163. 'this.DoIt = function(vJ) {',
  27164. '};',
  27165. 'this.b = false;',
  27166. 'this.vP = null;',
  27167. 'this.vQ = null;'
  27168. ]),
  27169. LinesToStr([ // $mod.$main
  27170. '$mod.vP = null;',
  27171. '$mod.vP = $mod.vP;',
  27172. '$mod.vP = $mod.DoIt;',
  27173. '$mod.vP(1);',
  27174. '$mod.vP(1);',
  27175. '$mod.vP(2);',
  27176. '$mod.b = $mod.vP === null;',
  27177. '$mod.b = null === $mod.vP;',
  27178. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27179. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27180. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27181. '$mod.b = $mod.vP !== null;',
  27182. '$mod.b = null !== $mod.vP;',
  27183. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27184. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27185. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27186. '$mod.b = $mod.vP != null;',
  27187. 'if ($mod.vP != null) ;',
  27188. '']));
  27189. end;
  27190. procedure TTestModule.TestProcType_Arg;
  27191. begin
  27192. StartProgram(false);
  27193. Add([
  27194. 'type',
  27195. ' TProcInt = procedure(vI: longint = 1);',
  27196. 'procedure DoIt(vJ: longint); begin end;',
  27197. 'procedure DoSome(vP, vQ: TProcInt);',
  27198. 'var',
  27199. ' b: boolean;',
  27200. 'begin',
  27201. ' vp:=nil;',
  27202. ' vp:=vp;',
  27203. ' vp:=@doit;',
  27204. ' vp;',
  27205. ' vp();',
  27206. ' vp(2);',
  27207. ' b:=vp=nil;',
  27208. ' b:=nil=vp;',
  27209. ' b:=vp=vq;',
  27210. ' b:=vp=@doit;',
  27211. ' b:=@doit=vp;',
  27212. ' b:=vp<>nil;',
  27213. ' b:=nil<>vp;',
  27214. ' b:=vp<>vq;',
  27215. ' b:=vp<>@doit;',
  27216. ' b:=@doit<>vp;',
  27217. ' b:=Assigned(vp);',
  27218. ' if Assigned(vp) then ;',
  27219. 'end;',
  27220. 'begin',
  27221. ' DoSome(@DoIt,nil);']);
  27222. ConvertProgram;
  27223. CheckSource('TestProcType_Arg',
  27224. LinesToStr([ // statements
  27225. 'this.DoIt = function(vJ) {',
  27226. '};',
  27227. 'this.DoSome = function(vP, vQ) {',
  27228. ' var b = false;',
  27229. ' vP = null;',
  27230. ' vP = vP;',
  27231. ' vP = $mod.DoIt;',
  27232. ' vP(1);',
  27233. ' vP(1);',
  27234. ' vP(2);',
  27235. ' b = vP === null;',
  27236. ' b = null === vP;',
  27237. ' b = rtl.eqCallback(vP,vQ);',
  27238. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  27239. ' b = rtl.eqCallback($mod.DoIt, vP);',
  27240. ' b = vP !== null;',
  27241. ' b = null !== vP;',
  27242. ' b = !rtl.eqCallback(vP, vQ);',
  27243. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  27244. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  27245. ' b = vP != null;',
  27246. ' if (vP != null) ;',
  27247. '};',
  27248. '']),
  27249. LinesToStr([ // $mod.$main
  27250. '$mod.DoSome($mod.DoIt,null);',
  27251. '']));
  27252. end;
  27253. procedure TTestModule.TestProcType_FunctionFPC;
  27254. begin
  27255. StartProgram(false);
  27256. Add('type');
  27257. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27258. Add('function DoIt(vI: longint): longint;');
  27259. Add('begin end;');
  27260. Add('var');
  27261. Add(' b: boolean;');
  27262. Add(' vP, vQ: tfuncint;');
  27263. Add('begin');
  27264. Add(' vp:=nil;');
  27265. Add(' vp:=vp;');
  27266. Add(' vp:=@doit;'); // ok in fpc and delphi
  27267. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27268. Add(' vp;'); // ok in fpc and delphi
  27269. Add(' vp();');
  27270. Add(' vp(2);');
  27271. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27272. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27273. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27274. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27275. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27276. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27277. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27278. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27279. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27280. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27281. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27282. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27283. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27284. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27285. Add(' b:=Assigned(vp);');
  27286. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27287. Add(' doit(vp());'); // ok in fpc and delphi
  27288. Add(' doit(vp(2));'); // ok in fpc and delphi
  27289. ConvertProgram;
  27290. CheckSource('TestProcType_FunctionFPC',
  27291. LinesToStr([ // statements
  27292. 'this.DoIt = function(vI) {',
  27293. ' var Result = 0;',
  27294. ' return Result;',
  27295. '};',
  27296. 'this.b = false;',
  27297. 'this.vP = null;',
  27298. 'this.vQ = null;'
  27299. ]),
  27300. LinesToStr([ // $mod.$main
  27301. '$mod.vP = null;',
  27302. '$mod.vP = $mod.vP;',
  27303. '$mod.vP = $mod.DoIt;',
  27304. '$mod.vP(1);',
  27305. '$mod.vP(1);',
  27306. '$mod.vP(2);',
  27307. '$mod.b = $mod.vP === null;',
  27308. '$mod.b = null === $mod.vP;',
  27309. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27310. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27311. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27312. '$mod.b = 4 === $mod.vP(1);',
  27313. '$mod.b = $mod.vP !== null;',
  27314. '$mod.b = null !== $mod.vP;',
  27315. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27316. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27317. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27318. '$mod.b = 6 !== $mod.vP(1);',
  27319. '$mod.b = $mod.vP != null;',
  27320. '$mod.DoIt($mod.vP(1));',
  27321. '$mod.DoIt($mod.vP(2));',
  27322. '']));
  27323. end;
  27324. procedure TTestModule.TestProcType_FunctionDelphi;
  27325. begin
  27326. StartProgram(false);
  27327. Add('{$mode Delphi}');
  27328. Add('type');
  27329. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27330. Add('function DoIt(vI: longint): longint;');
  27331. Add('begin end;');
  27332. Add('var');
  27333. Add(' b: boolean;');
  27334. Add(' vP, vQ: tfuncint;');
  27335. Add('begin');
  27336. Add(' vp:=nil;');
  27337. Add(' vp:=vp;');
  27338. Add(' vp:=@doit;'); // ok in fpc and delphi
  27339. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27340. Add(' vp;'); // ok in fpc and delphi
  27341. Add(' vp();');
  27342. Add(' vp(2);');
  27343. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27344. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27345. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27346. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27347. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27348. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27349. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27350. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27351. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27352. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27353. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27354. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27355. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27356. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27357. Add(' b:=Assigned(vp);');
  27358. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27359. Add(' doit(vp());'); // ok in fpc and delphi
  27360. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27361. ConvertProgram;
  27362. CheckSource('TestProcType_FunctionDelphi',
  27363. LinesToStr([ // statements
  27364. 'this.DoIt = function(vI) {',
  27365. ' var Result = 0;',
  27366. ' return Result;',
  27367. '};',
  27368. 'this.b = false;',
  27369. 'this.vP = null;',
  27370. 'this.vQ = null;'
  27371. ]),
  27372. LinesToStr([ // $mod.$main
  27373. '$mod.vP = null;',
  27374. '$mod.vP = $mod.vP;',
  27375. '$mod.vP = $mod.DoIt;',
  27376. '$mod.vP = $mod.DoIt;',
  27377. '$mod.vP(1);',
  27378. '$mod.vP(1);',
  27379. '$mod.vP(2);',
  27380. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27381. '$mod.b = $mod.vP(1) === 3;',
  27382. '$mod.b = 4 === $mod.vP(1);',
  27383. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27384. '$mod.b = $mod.vP(1) !== 5;',
  27385. '$mod.b = 6 !== $mod.vP(1);',
  27386. '$mod.b = $mod.vP != null;',
  27387. '$mod.DoIt($mod.vP(1));',
  27388. '$mod.DoIt($mod.vP(1));',
  27389. '$mod.DoIt($mod.vP(2));',
  27390. '']));
  27391. end;
  27392. procedure TTestModule.TestProcType_ProcedureDelphi;
  27393. begin
  27394. StartProgram(false);
  27395. Add('{$mode Delphi}');
  27396. Add('type');
  27397. Add(' TProc = procedure;');
  27398. Add('procedure DoIt;');
  27399. Add('begin end;');
  27400. Add('var');
  27401. Add(' b: boolean;');
  27402. Add(' vP, vQ: tproc;');
  27403. Add('begin');
  27404. Add(' vp:=nil;');
  27405. Add(' vp:=vp;');
  27406. Add(' vp:=vq;');
  27407. 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
  27408. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27409. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27410. Add(' vp;'); // ok in fpc and delphi
  27411. Add(' vp();');
  27412. // equal
  27413. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27414. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27415. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27416. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27417. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27418. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27419. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27420. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27421. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27422. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27423. // unequal
  27424. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27425. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27426. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27427. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27428. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27429. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27430. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27431. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27432. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27433. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27434. Add(' b:=Assigned(vp);');
  27435. ConvertProgram;
  27436. CheckSource('TestProcType_ProcedureDelphi',
  27437. LinesToStr([ // statements
  27438. 'this.DoIt = function() {',
  27439. '};',
  27440. 'this.b = false;',
  27441. 'this.vP = null;',
  27442. 'this.vQ = null;'
  27443. ]),
  27444. LinesToStr([ // $mod.$main
  27445. '$mod.vP = null;',
  27446. '$mod.vP = $mod.vP;',
  27447. '$mod.vP = $mod.vQ;',
  27448. '$mod.vP = $mod.DoIt;',
  27449. '$mod.vP = $mod.DoIt;',
  27450. '$mod.vP();',
  27451. '$mod.vP();',
  27452. '$mod.b = $mod.vP === null;',
  27453. '$mod.b = null === $mod.vP;',
  27454. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27455. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27456. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27457. '$mod.b = $mod.vP !== null;',
  27458. '$mod.b = null !== $mod.vP;',
  27459. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27460. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27461. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27462. '$mod.b = $mod.vP != null;',
  27463. '']));
  27464. end;
  27465. procedure TTestModule.TestProcType_AsParam;
  27466. begin
  27467. StartProgram(false);
  27468. Add('type');
  27469. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27470. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27471. Add('var vJ: tfuncint;');
  27472. Add('begin');
  27473. Add(' vg:=vg;');
  27474. Add(' vj:=vh;');
  27475. Add(' vi:=vi;');
  27476. Add(' doit(vg,vg,vg);');
  27477. Add(' doit(vh,vh,vj);');
  27478. Add(' doit(vi,vi,vi);');
  27479. Add(' doit(vj,vj,vj);');
  27480. Add('end;');
  27481. Add('var i: tfuncint;');
  27482. Add('begin');
  27483. Add(' doit(i,i,i);');
  27484. ConvertProgram;
  27485. CheckSource('TestProcType_AsParam',
  27486. LinesToStr([ // statements
  27487. 'this.DoIt = function (vG,vH,vI) {',
  27488. ' var vJ = null;',
  27489. ' vG = vG;',
  27490. ' vJ = vH;',
  27491. ' vI.set(vI.get());',
  27492. ' $mod.DoIt(vG, vG, {',
  27493. ' get: function () {',
  27494. ' return vG;',
  27495. ' },',
  27496. ' set: function (v) {',
  27497. ' vG = v;',
  27498. ' }',
  27499. ' });',
  27500. ' $mod.DoIt(vH, vH, {',
  27501. ' get: function () {',
  27502. ' return vJ;',
  27503. ' },',
  27504. ' set: function (v) {',
  27505. ' vJ = v;',
  27506. ' }',
  27507. ' });',
  27508. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27509. ' $mod.DoIt(vJ, vJ, {',
  27510. ' get: function () {',
  27511. ' return vJ;',
  27512. ' },',
  27513. ' set: function (v) {',
  27514. ' vJ = v;',
  27515. ' }',
  27516. ' });',
  27517. '};',
  27518. 'this.i = null;'
  27519. ]),
  27520. LinesToStr([
  27521. '$mod.DoIt($mod.i,$mod.i,{',
  27522. ' p: $mod,',
  27523. ' get: function () {',
  27524. ' return this.p.i;',
  27525. ' },',
  27526. ' set: function (v) {',
  27527. ' this.p.i = v;',
  27528. ' }',
  27529. '});'
  27530. ]));
  27531. end;
  27532. procedure TTestModule.TestProcType_MethodFPC;
  27533. begin
  27534. StartProgram(false);
  27535. Add('type');
  27536. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27537. Add(' TObject = class');
  27538. Add(' function DoIt(vA: longint = 1): longint;');
  27539. Add(' end;');
  27540. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27541. Add('begin');
  27542. Add('end;');
  27543. Add('var');
  27544. Add(' Obj: TObject;');
  27545. Add(' vP: tfuncint;');
  27546. Add(' b: boolean;');
  27547. Add('begin');
  27548. Add(' vp:[email protected];'); // ok in fpc and delphi
  27549. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27550. Add(' vp;'); // ok in fpc and delphi
  27551. Add(' vp();');
  27552. Add(' vp(2);');
  27553. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27554. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27555. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27556. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27557. ConvertProgram;
  27558. CheckSource('TestProcType_MethodFPC',
  27559. LinesToStr([ // statements
  27560. 'rtl.createClass(this, "TObject", null, function () {',
  27561. ' this.$init = function () {',
  27562. ' };',
  27563. ' this.$final = function () {',
  27564. ' };',
  27565. ' this.DoIt = function (vA) {',
  27566. ' var Result = 0;',
  27567. ' return Result;',
  27568. ' };',
  27569. '});',
  27570. 'this.Obj = null;',
  27571. 'this.vP = null;',
  27572. 'this.b = false;'
  27573. ]),
  27574. LinesToStr([
  27575. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27576. '$mod.vP(1);',
  27577. '$mod.vP(1);',
  27578. '$mod.vP(2);',
  27579. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27580. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27581. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27582. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27583. '']));
  27584. end;
  27585. procedure TTestModule.TestProcType_MethodDelphi;
  27586. begin
  27587. StartProgram(false);
  27588. Add([
  27589. '{$mode delphi}',
  27590. 'type',
  27591. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27592. ' TObject = class',
  27593. ' function DoIt(vA: longint = 1): longint;',
  27594. ' end;',
  27595. 'function TObject.DoIt(vA: longint = 1): longint;',
  27596. 'begin',
  27597. 'end;',
  27598. 'var',
  27599. ' Obj: TObject;',
  27600. ' vP: tfuncint;',
  27601. ' b: boolean;',
  27602. 'begin',
  27603. ' vp:[email protected];', // ok in fpc and delphi
  27604. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27605. ' vp;', // ok in fpc and delphi
  27606. ' vp();',
  27607. ' vp(2);',
  27608. //' b:[email protected];', // ok in fpc, illegal in delphi
  27609. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27610. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27611. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27612. '']);
  27613. ConvertProgram;
  27614. CheckSource('TestProcType_MethodDelphi',
  27615. LinesToStr([ // statements
  27616. 'rtl.createClass(this, "TObject", null, function () {',
  27617. ' this.$init = function () {',
  27618. ' };',
  27619. ' this.$final = function () {',
  27620. ' };',
  27621. ' this.DoIt = function (vA) {',
  27622. ' var Result = 0;',
  27623. ' return Result;',
  27624. ' };',
  27625. '});',
  27626. 'this.Obj = null;',
  27627. 'this.vP = null;',
  27628. 'this.b = false;'
  27629. ]),
  27630. LinesToStr([
  27631. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27632. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27633. '$mod.vP(1);',
  27634. '$mod.vP(1);',
  27635. '$mod.vP(2);',
  27636. '']));
  27637. end;
  27638. procedure TTestModule.TestProcType_PropertyFPC;
  27639. begin
  27640. StartProgram(false);
  27641. Add('type');
  27642. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27643. Add(' TObject = class');
  27644. Add(' FOnFoo: TFuncInt;');
  27645. Add(' function DoIt(vA: longint = 1): longint;');
  27646. Add(' function GetFoo: TFuncInt;');
  27647. Add(' procedure SetFoo(const Value: TFuncInt);');
  27648. Add(' function GetEvents(Index: longint): TFuncInt;');
  27649. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27650. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27651. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27652. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27653. Add(' end;');
  27654. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27655. Add('function tobject.getfoo: tfuncint; begin end;');
  27656. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27657. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27658. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27659. Add('var');
  27660. Add(' Obj: TObject;');
  27661. Add(' vP: tfuncint;');
  27662. Add(' b: boolean;');
  27663. Add('begin');
  27664. Add(' obj.onfoo:=nil;');
  27665. Add(' obj.onbar:=nil;');
  27666. Add(' obj.events[1]:=nil;');
  27667. Add(' obj.onfoo:=obj.onfoo;');
  27668. Add(' obj.onbar:=obj.onbar;');
  27669. Add(' obj.events[2]:=obj.events[3];');
  27670. Add(' obj.onfoo:[email protected];');
  27671. Add(' obj.onbar:[email protected];');
  27672. Add(' obj.events[4]:[email protected];');
  27673. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27674. //Add(' obj.onbar:=obj.doit;'); // delphi
  27675. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27676. Add(' obj.onfoo;');
  27677. Add(' obj.onbar;');
  27678. //Add(' obj.events[5];'); ToDo in pasresolver
  27679. Add(' obj.onfoo();');
  27680. Add(' obj.onbar();');
  27681. Add(' obj.events[6]();');
  27682. Add(' b:=obj.onfoo=nil;');
  27683. Add(' b:=obj.onbar=nil;');
  27684. Add(' b:=obj.events[7]=nil;');
  27685. Add(' b:=obj.onfoo<>nil;');
  27686. Add(' b:=obj.onbar<>nil;');
  27687. Add(' b:=obj.events[8]<>nil;');
  27688. Add(' b:=obj.onfoo=vp;');
  27689. Add(' b:=obj.onbar=vp;');
  27690. Add(' b:=obj.events[9]=vp;');
  27691. Add(' b:=obj.onfoo=obj.onfoo;');
  27692. Add(' b:=obj.onbar=obj.onfoo;');
  27693. Add(' b:=obj.events[10]=obj.onfoo;');
  27694. Add(' b:=obj.onfoo<>obj.onfoo;');
  27695. Add(' b:=obj.onbar<>obj.onfoo;');
  27696. Add(' b:=obj.events[11]<>obj.onfoo;');
  27697. Add(' b:[email protected];');
  27698. Add(' b:[email protected];');
  27699. Add(' b:=obj.events[12][email protected];');
  27700. Add(' b:=obj.onfoo<>@obj.doit;');
  27701. Add(' b:=obj.onbar<>@obj.doit;');
  27702. Add(' b:=obj.events[12]<>@obj.doit;');
  27703. Add(' b:=Assigned(obj.onfoo);');
  27704. Add(' b:=Assigned(obj.onbar);');
  27705. Add(' b:=Assigned(obj.events[13]);');
  27706. ConvertProgram;
  27707. CheckSource('TestProcType_PropertyFPC',
  27708. LinesToStr([ // statements
  27709. 'rtl.createClass(this, "TObject", null, function () {',
  27710. ' this.$init = function () {',
  27711. ' this.FOnFoo = null;',
  27712. ' };',
  27713. ' this.$final = function () {',
  27714. ' this.FOnFoo = undefined;',
  27715. ' };',
  27716. ' this.DoIt = function (vA) {',
  27717. ' var Result = 0;',
  27718. ' return Result;',
  27719. ' };',
  27720. 'this.GetFoo = function () {',
  27721. ' var Result = null;',
  27722. ' return Result;',
  27723. '};',
  27724. 'this.SetFoo = function (Value) {',
  27725. '};',
  27726. 'this.GetEvents = function (Index) {',
  27727. ' var Result = null;',
  27728. ' return Result;',
  27729. '};',
  27730. 'this.SetEvents = function (Index, Value) {',
  27731. '};',
  27732. '});',
  27733. 'this.Obj = null;',
  27734. 'this.vP = null;',
  27735. 'this.b = false;'
  27736. ]),
  27737. LinesToStr([
  27738. '$mod.Obj.FOnFoo = null;',
  27739. '$mod.Obj.SetFoo(null);',
  27740. '$mod.Obj.SetEvents(1, null);',
  27741. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27742. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27743. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27744. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27745. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27746. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27747. '$mod.Obj.FOnFoo(1);',
  27748. '$mod.Obj.GetFoo();',
  27749. '$mod.Obj.FOnFoo(1);',
  27750. '$mod.Obj.GetFoo()(1);',
  27751. '$mod.Obj.GetEvents(6)(1);',
  27752. '$mod.b = $mod.Obj.FOnFoo === null;',
  27753. '$mod.b = $mod.Obj.GetFoo() === null;',
  27754. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27755. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27756. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27757. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27758. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27759. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27760. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27761. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27762. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27763. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27764. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27765. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27766. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27767. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27768. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27769. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27770. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27771. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27772. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27773. '$mod.b = $mod.Obj.FOnFoo != null;',
  27774. '$mod.b = $mod.Obj.GetFoo() != null;',
  27775. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27776. '']));
  27777. end;
  27778. procedure TTestModule.TestProcType_PropertyDelphi;
  27779. begin
  27780. StartProgram(false);
  27781. Add('{$mode delphi}');
  27782. Add('type');
  27783. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27784. Add(' TObject = class');
  27785. Add(' FOnFoo: TFuncInt;');
  27786. Add(' function DoIt(vA: longint = 1): longint;');
  27787. Add(' function GetFoo: TFuncInt;');
  27788. Add(' procedure SetFoo(const Value: TFuncInt);');
  27789. Add(' function GetEvents(Index: longint): TFuncInt;');
  27790. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27791. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27792. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27793. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27794. Add(' end;');
  27795. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27796. Add('function tobject.getfoo: tfuncint; begin end;');
  27797. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27798. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27799. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27800. Add('var');
  27801. Add(' Obj: TObject;');
  27802. Add(' vP: tfuncint;');
  27803. Add(' b: boolean;');
  27804. Add('begin');
  27805. Add(' obj.onfoo:=nil;');
  27806. Add(' obj.onbar:=nil;');
  27807. Add(' obj.events[1]:=nil;');
  27808. Add(' obj.onfoo:=obj.onfoo;');
  27809. Add(' obj.onbar:=obj.onbar;');
  27810. Add(' obj.events[2]:=obj.events[3];');
  27811. Add(' obj.onfoo:[email protected];');
  27812. Add(' obj.onbar:[email protected];');
  27813. Add(' obj.events[4]:[email protected];');
  27814. Add(' obj.onfoo:=obj.doit;'); // delphi
  27815. Add(' obj.onbar:=obj.doit;'); // delphi
  27816. Add(' obj.events[4]:=obj.doit;'); // delphi
  27817. Add(' obj.onfoo;');
  27818. Add(' obj.onbar;');
  27819. //Add(' obj.events[5];'); ToDo in pasresolver
  27820. Add(' obj.onfoo();');
  27821. Add(' obj.onbar();');
  27822. Add(' obj.events[6]();');
  27823. //Add(' b:=obj.onfoo=nil;'); // fpc
  27824. //Add(' b:=obj.onbar=nil;'); // fpc
  27825. //Add(' b:=obj.events[7]=nil;'); // fpc
  27826. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27827. //Add(' b:=obj.onbar<>nil;'); // fpc
  27828. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27829. Add(' b:=obj.onfoo=vp;');
  27830. Add(' b:=obj.onbar=vp;');
  27831. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27832. Add(' b:=obj.onfoo=obj.onfoo;');
  27833. Add(' b:=obj.onbar=obj.onfoo;');
  27834. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27835. Add(' b:=obj.onfoo<>obj.onfoo;');
  27836. Add(' b:=obj.onbar<>obj.onfoo;');
  27837. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27838. //Add(' b:[email protected];'); // fpc
  27839. //Add(' b:[email protected];'); // fpc
  27840. //Add(' b:=obj.events[12][email protected];'); // fpc
  27841. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27842. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27843. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27844. Add(' b:=Assigned(obj.onfoo);');
  27845. Add(' b:=Assigned(obj.onbar);');
  27846. Add(' b:=Assigned(obj.events[13]);');
  27847. ConvertProgram;
  27848. CheckSource('TestProcType_PropertyDelphi',
  27849. LinesToStr([ // statements
  27850. 'rtl.createClass(this, "TObject", null, function () {',
  27851. ' this.$init = function () {',
  27852. ' this.FOnFoo = null;',
  27853. ' };',
  27854. ' this.$final = function () {',
  27855. ' this.FOnFoo = undefined;',
  27856. ' };',
  27857. ' this.DoIt = function (vA) {',
  27858. ' var Result = 0;',
  27859. ' return Result;',
  27860. ' };',
  27861. 'this.GetFoo = function () {',
  27862. ' var Result = null;',
  27863. ' return Result;',
  27864. '};',
  27865. 'this.SetFoo = function (Value) {',
  27866. '};',
  27867. 'this.GetEvents = function (Index) {',
  27868. ' var Result = null;',
  27869. ' return Result;',
  27870. '};',
  27871. 'this.SetEvents = function (Index, Value) {',
  27872. '};',
  27873. '});',
  27874. 'this.Obj = null;',
  27875. 'this.vP = null;',
  27876. 'this.b = false;'
  27877. ]),
  27878. LinesToStr([
  27879. '$mod.Obj.FOnFoo = null;',
  27880. '$mod.Obj.SetFoo(null);',
  27881. '$mod.Obj.SetEvents(1, null);',
  27882. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27883. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27884. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27885. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27886. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27887. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27888. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27889. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27890. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27891. '$mod.Obj.FOnFoo(1);',
  27892. '$mod.Obj.GetFoo();',
  27893. '$mod.Obj.FOnFoo(1);',
  27894. '$mod.Obj.GetFoo()(1);',
  27895. '$mod.Obj.GetEvents(6)(1);',
  27896. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27897. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27898. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27899. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27900. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27901. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27902. '$mod.b = $mod.Obj.FOnFoo != null;',
  27903. '$mod.b = $mod.Obj.GetFoo() != null;',
  27904. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27905. '']));
  27906. end;
  27907. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27908. begin
  27909. StartProgram(false);
  27910. Add('type');
  27911. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27912. Add(' TObject = class');
  27913. Add(' FOnFoo: TFuncInt;');
  27914. Add(' function DoIt(vA: longint = 1): longint;');
  27915. Add(' function GetFoo: TFuncInt;');
  27916. Add(' procedure SetFoo(const Value: TFuncInt);');
  27917. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27918. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27919. Add(' end;');
  27920. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27921. Add('function tobject.getfoo: tfuncint; begin end;');
  27922. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27923. Add('var');
  27924. Add(' Obj: TObject;');
  27925. Add(' vP: tfuncint;');
  27926. Add(' b: boolean;');
  27927. Add('begin');
  27928. Add('with obj do begin');
  27929. Add(' fonfoo:=nil;');
  27930. Add(' onfoo:=nil;');
  27931. Add(' onbar:=nil;');
  27932. Add(' fonfoo:=fonfoo;');
  27933. Add(' onfoo:=onfoo;');
  27934. Add(' onbar:=onbar;');
  27935. Add(' fonfoo:=@doit;');
  27936. Add(' onfoo:=@doit;');
  27937. Add(' onbar:=@doit;');
  27938. //Add(' fonfoo:=doit;'); // delphi
  27939. //Add(' onfoo:=doit;'); // delphi
  27940. //Add(' onbar:=doit;'); // delphi
  27941. Add(' fonfoo;');
  27942. Add(' onfoo;');
  27943. Add(' onbar;');
  27944. Add(' fonfoo();');
  27945. Add(' onfoo();');
  27946. Add(' onbar();');
  27947. Add(' b:=fonfoo=nil;');
  27948. Add(' b:=onfoo=nil;');
  27949. Add(' b:=onbar=nil;');
  27950. Add(' b:=fonfoo<>nil;');
  27951. Add(' b:=onfoo<>nil;');
  27952. Add(' b:=onbar<>nil;');
  27953. Add(' b:=fonfoo=vp;');
  27954. Add(' b:=onfoo=vp;');
  27955. Add(' b:=onbar=vp;');
  27956. Add(' b:=fonfoo=fonfoo;');
  27957. Add(' b:=onfoo=onfoo;');
  27958. Add(' b:=onbar=onfoo;');
  27959. Add(' b:=fonfoo<>fonfoo;');
  27960. Add(' b:=onfoo<>onfoo;');
  27961. Add(' b:=onbar<>onfoo;');
  27962. Add(' b:=fonfoo=@doit;');
  27963. Add(' b:=onfoo=@doit;');
  27964. Add(' b:=onbar=@doit;');
  27965. Add(' b:=fonfoo<>@doit;');
  27966. Add(' b:=onfoo<>@doit;');
  27967. Add(' b:=onbar<>@doit;');
  27968. Add(' b:=Assigned(fonfoo);');
  27969. Add(' b:=Assigned(onfoo);');
  27970. Add(' b:=Assigned(onbar);');
  27971. Add('end;');
  27972. ConvertProgram;
  27973. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  27974. LinesToStr([ // statements
  27975. 'rtl.createClass(this, "TObject", null, function () {',
  27976. ' this.$init = function () {',
  27977. ' this.FOnFoo = null;',
  27978. ' };',
  27979. ' this.$final = function () {',
  27980. ' this.FOnFoo = undefined;',
  27981. ' };',
  27982. ' this.DoIt = function (vA) {',
  27983. ' var Result = 0;',
  27984. ' return Result;',
  27985. ' };',
  27986. ' this.GetFoo = function () {',
  27987. ' var Result = null;',
  27988. ' return Result;',
  27989. ' };',
  27990. ' this.SetFoo = function (Value) {',
  27991. ' };',
  27992. '});',
  27993. 'this.Obj = null;',
  27994. 'this.vP = null;',
  27995. 'this.b = false;'
  27996. ]),
  27997. LinesToStr([
  27998. 'var $with = $mod.Obj;',
  27999. '$with.FOnFoo = null;',
  28000. '$with.FOnFoo = null;',
  28001. '$with.SetFoo(null);',
  28002. '$with.FOnFoo = $with.FOnFoo;',
  28003. '$with.FOnFoo = $with.FOnFoo;',
  28004. '$with.SetFoo($with.GetFoo());',
  28005. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28006. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28007. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28008. '$with.FOnFoo(1);',
  28009. '$with.FOnFoo(1);',
  28010. '$with.GetFoo();',
  28011. '$with.FOnFoo(1);',
  28012. '$with.FOnFoo(1);',
  28013. '$with.GetFoo()(1);',
  28014. '$mod.b = $with.FOnFoo === null;',
  28015. '$mod.b = $with.FOnFoo === null;',
  28016. '$mod.b = $with.GetFoo() === null;',
  28017. '$mod.b = $with.FOnFoo !== null;',
  28018. '$mod.b = $with.FOnFoo !== null;',
  28019. '$mod.b = $with.GetFoo() !== null;',
  28020. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28021. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28022. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28023. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28024. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28025. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28026. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28027. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28028. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28029. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28030. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28031. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28032. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28033. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28034. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28035. '$mod.b = $with.FOnFoo != null;',
  28036. '$mod.b = $with.FOnFoo != null;',
  28037. '$mod.b = $with.GetFoo() != null;',
  28038. '']));
  28039. end;
  28040. procedure TTestModule.TestProcType_Nested;
  28041. begin
  28042. StartProgram(false);
  28043. Add([
  28044. 'type',
  28045. ' TProcInt = procedure(vI: longint = 1);',
  28046. 'procedure DoIt(vJ: longint);',
  28047. 'var aProc: TProcInt;',
  28048. ' b: boolean;',
  28049. ' procedure Sub(vK: longint);',
  28050. ' var aSub: TProcInt;',
  28051. ' procedure SubSub(vK: longint);',
  28052. ' var aSubSub: TProcInt;',
  28053. ' begin;',
  28054. ' aProc:=@DoIt;',
  28055. ' aSub:=@DoIt;',
  28056. ' aSubSub:=@DoIt;',
  28057. ' aProc:=@Sub;',
  28058. ' aSub:=@Sub;',
  28059. ' aSubSub:=@Sub;',
  28060. ' aProc:=@SubSub;',
  28061. ' aSub:=@SubSub;',
  28062. ' aSubSub:=@SubSub;',
  28063. ' end;',
  28064. ' begin;',
  28065. ' end;',
  28066. 'begin;',
  28067. ' aProc:=@Sub;',
  28068. ' b:=aProc=@Sub;',
  28069. ' b:=@Sub=aProc;',
  28070. 'end;',
  28071. 'begin',
  28072. '']);
  28073. ConvertProgram;
  28074. CheckSource('TestProcType_Nested',
  28075. LinesToStr([ // statements
  28076. 'this.DoIt = function (vJ) {',
  28077. ' var aProc = null;',
  28078. ' var b = false;',
  28079. ' function Sub(vK) {',
  28080. ' var aSub = null;',
  28081. ' function SubSub(vK) {',
  28082. ' var aSubSub = null;',
  28083. ' aProc = $mod.DoIt;',
  28084. ' aSub = $mod.DoIt;',
  28085. ' aSubSub = $mod.DoIt;',
  28086. ' aProc = Sub;',
  28087. ' aSub = Sub;',
  28088. ' aSubSub = Sub;',
  28089. ' aProc = SubSub;',
  28090. ' aSub = SubSub;',
  28091. ' aSubSub = SubSub;',
  28092. ' };',
  28093. ' };',
  28094. ' aProc = Sub;',
  28095. ' b = rtl.eqCallback(aProc, Sub);',
  28096. ' b = rtl.eqCallback(Sub, aProc);',
  28097. '};',
  28098. '']),
  28099. LinesToStr([ // $mod.$main
  28100. '']));
  28101. end;
  28102. procedure TTestModule.TestProcType_NestedOfObject;
  28103. begin
  28104. StartProgram(false);
  28105. Add([
  28106. 'type',
  28107. ' TProcInt = procedure(vI: longint = 1) of object;',
  28108. ' TObject = class',
  28109. ' procedure DoIt(vJ: longint);',
  28110. ' end;',
  28111. 'procedure TObject.DoIt(vJ: longint);',
  28112. 'var aProc: TProcInt;',
  28113. ' b: boolean;',
  28114. ' procedure Sub(vK: longint);',
  28115. ' var aSub: TProcInt;',
  28116. ' procedure SubSub(vK: longint);',
  28117. ' var aSubSub: TProcInt;',
  28118. ' begin;',
  28119. ' aProc:=@DoIt;',
  28120. ' aSub:=@DoIt;',
  28121. ' aSubSub:=@DoIt;',
  28122. ' aProc:=@Sub;',
  28123. ' aSub:=@Sub;',
  28124. ' aSubSub:=@Sub;',
  28125. ' aProc:=@SubSub;',
  28126. ' aSub:=@SubSub;',
  28127. ' aSubSub:=@SubSub;',
  28128. ' end;',
  28129. ' begin;',
  28130. ' end;',
  28131. 'begin;',
  28132. ' aProc:=@Sub;',
  28133. ' b:=aProc=@Sub;',
  28134. ' b:=@Sub=aProc;',
  28135. 'end;',
  28136. 'begin',
  28137. '']);
  28138. ConvertProgram;
  28139. CheckSource('TestProcType_Nested',
  28140. LinesToStr([ // statements
  28141. 'rtl.createClass(this, "TObject", null, function () {',
  28142. ' this.$init = function () {',
  28143. ' };',
  28144. ' this.$final = function () {',
  28145. ' };',
  28146. ' this.DoIt = function (vJ) {',
  28147. ' var $Self = this;',
  28148. ' var aProc = null;',
  28149. ' var b = false;',
  28150. ' function Sub(vK) {',
  28151. ' var aSub = null;',
  28152. ' function SubSub(vK) {',
  28153. ' var aSubSub = null;',
  28154. ' aProc = rtl.createCallback($Self, "DoIt");',
  28155. ' aSub = rtl.createCallback($Self, "DoIt");',
  28156. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28157. ' aProc = Sub;',
  28158. ' aSub = Sub;',
  28159. ' aSubSub = Sub;',
  28160. ' aProc = SubSub;',
  28161. ' aSub = SubSub;',
  28162. ' aSubSub = SubSub;',
  28163. ' };',
  28164. ' };',
  28165. ' aProc = Sub;',
  28166. ' b = rtl.eqCallback(aProc, Sub);',
  28167. ' b = rtl.eqCallback(Sub, aProc);',
  28168. ' };',
  28169. '});',
  28170. '']),
  28171. LinesToStr([ // $mod.$main
  28172. '']));
  28173. end;
  28174. procedure TTestModule.TestProcType_ReferenceToProc;
  28175. begin
  28176. StartProgram(false);
  28177. Add([
  28178. 'type',
  28179. ' TProcRef = reference to procedure(i: longint = 0);',
  28180. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28181. 'var',
  28182. ' p: TProcRef;',
  28183. ' f: TFuncRef;',
  28184. 'procedure DoIt(i: longint);',
  28185. 'begin',
  28186. 'end;',
  28187. 'function GetIt(i: longint): longint;',
  28188. 'begin',
  28189. ' p:=@DoIt;',
  28190. ' f:=@GetIt;',
  28191. ' f;',
  28192. ' f();',
  28193. ' f(1);',
  28194. 'end;',
  28195. 'begin',
  28196. ' p:=@DoIt;',
  28197. ' f:=@GetIt;',
  28198. ' f;',
  28199. ' f();',
  28200. ' f(1);',
  28201. ' p:=TProcRef(f);',
  28202. '']);
  28203. ConvertProgram;
  28204. CheckSource('TestProcType_ReferenceToProc',
  28205. LinesToStr([ // statements
  28206. 'this.p = null;',
  28207. 'this.f = null;',
  28208. 'this.DoIt = function (i) {',
  28209. '};',
  28210. 'this.GetIt = function (i) {',
  28211. ' var Result = 0;',
  28212. ' $mod.p = $mod.DoIt;',
  28213. ' $mod.f = $mod.GetIt;',
  28214. ' $mod.f(0);',
  28215. ' $mod.f(0);',
  28216. ' $mod.f(1);',
  28217. ' return Result;',
  28218. '};',
  28219. '']),
  28220. LinesToStr([ // $mod.$main
  28221. '$mod.p = $mod.DoIt;',
  28222. '$mod.f = $mod.GetIt;',
  28223. '$mod.f(0);',
  28224. '$mod.f(0);',
  28225. '$mod.f(1);',
  28226. '$mod.p = $mod.f;',
  28227. '']));
  28228. end;
  28229. procedure TTestModule.TestProcType_ReferenceToMethod;
  28230. begin
  28231. StartProgram(false);
  28232. Add([
  28233. 'type',
  28234. ' TFuncRef = reference to function(i: longint = 5): longint;',
  28235. ' TObject = class',
  28236. ' function Grow(s: longint): longint;',
  28237. ' end;',
  28238. 'var',
  28239. ' f: tfuncref;',
  28240. 'function tobject.grow(s: longint): longint;',
  28241. ' function GrowSub(i: longint): longint;',
  28242. ' begin',
  28243. ' f:=@grow;',
  28244. ' f:=@growsub;',
  28245. ' end;',
  28246. 'begin',
  28247. ' f:=@grow;',
  28248. ' f:=@growsub;',
  28249. 'end;',
  28250. 'begin',
  28251. '']);
  28252. ConvertProgram;
  28253. CheckSource('TestProcType_ReferenceToMethod',
  28254. LinesToStr([ // statements
  28255. 'rtl.createClass(this, "TObject", null, function () {',
  28256. ' this.$init = function () {',
  28257. ' };',
  28258. ' this.$final = function () {',
  28259. ' };',
  28260. ' this.Grow = function (s) {',
  28261. ' var $Self = this;',
  28262. ' var Result = 0;',
  28263. ' function GrowSub(i) {',
  28264. ' var Result = 0;',
  28265. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28266. ' $mod.f = GrowSub;',
  28267. ' return Result;',
  28268. ' };',
  28269. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28270. ' $mod.f = GrowSub;',
  28271. ' return Result;',
  28272. ' };',
  28273. '});',
  28274. 'this.f = null;',
  28275. '']),
  28276. LinesToStr([ // $mod.$main
  28277. '']));
  28278. end;
  28279. procedure TTestModule.TestProcType_Typecast;
  28280. begin
  28281. StartProgram(false);
  28282. Add([
  28283. 'type',
  28284. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  28285. ' TEvent = procedure of object;',
  28286. ' TGetter = function:longint of object;',
  28287. ' TProcA = procedure(i: longint);',
  28288. ' TFuncB = function(i, j: longint): longint;',
  28289. 'procedure DoIt(); varargs; begin end;',
  28290. 'var',
  28291. ' Notify: tnotifyevent;',
  28292. ' Event: tevent;',
  28293. ' Getter: tgetter;',
  28294. ' ProcA: tproca;',
  28295. ' FuncB: tfuncb;',
  28296. ' p: pointer;',
  28297. 'begin',
  28298. ' notify:=tnotifyevent(event);',
  28299. ' event:=tevent(event);',
  28300. ' event:=tevent(notify);',
  28301. ' event:=tevent(getter);',
  28302. ' event:=tevent(proca);',
  28303. ' proca:=tproca(funcb);',
  28304. ' funcb:=tfuncb(funcb);',
  28305. ' funcb:=tfuncb(proca);',
  28306. ' funcb:=tfuncb(getter);',
  28307. ' proca:=tproca(p);',
  28308. ' funcb:=tfuncb(p);',
  28309. ' getter:=tgetter(p);',
  28310. ' p:=pointer(notify);',
  28311. ' p:=notify;',
  28312. ' p:=pointer(proca);',
  28313. ' p:=proca;',
  28314. ' p:=pointer(funcb);',
  28315. ' p:=funcb;',
  28316. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  28317. '']);
  28318. ConvertProgram;
  28319. CheckSource('TestProcType_Typecast',
  28320. LinesToStr([ // statements
  28321. 'this.DoIt = function () {',
  28322. '};',
  28323. 'this.Notify = null;',
  28324. 'this.Event = null;',
  28325. 'this.Getter = null;',
  28326. 'this.ProcA = null;',
  28327. 'this.FuncB = null;',
  28328. 'this.p = null;',
  28329. '']),
  28330. LinesToStr([ // $mod.$main
  28331. '$mod.Notify = $mod.Event;',
  28332. '$mod.Event = $mod.Event;',
  28333. '$mod.Event = $mod.Notify;',
  28334. '$mod.Event = $mod.Getter;',
  28335. '$mod.Event = $mod.ProcA;',
  28336. '$mod.ProcA = $mod.FuncB;',
  28337. '$mod.FuncB = $mod.FuncB;',
  28338. '$mod.FuncB = $mod.ProcA;',
  28339. '$mod.FuncB = $mod.Getter;',
  28340. '$mod.ProcA = $mod.p;',
  28341. '$mod.FuncB = $mod.p;',
  28342. '$mod.Getter = $mod.p;',
  28343. '$mod.p = $mod.Notify;',
  28344. '$mod.p = $mod.Notify;',
  28345. '$mod.p = $mod.ProcA;',
  28346. '$mod.p = $mod.ProcA;',
  28347. '$mod.p = $mod.FuncB;',
  28348. '$mod.p = $mod.FuncB;',
  28349. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  28350. '']));
  28351. end;
  28352. procedure TTestModule.TestProcType_PassProcToUntyped;
  28353. begin
  28354. StartProgram(false);
  28355. Add([
  28356. 'type',
  28357. ' TEvent = procedure of object;',
  28358. ' TFunc = function: longint;',
  28359. 'procedure DoIt(); varargs; begin end;',
  28360. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28361. 'var',
  28362. ' Event: tevent;',
  28363. ' Func: TFunc;',
  28364. 'begin',
  28365. ' doit(event,func);',
  28366. ' dosome(event,event,event);',
  28367. ' dosome(func,func,func);',
  28368. '']);
  28369. ConvertProgram;
  28370. CheckSource('TestProcType_PassProcToUntyped',
  28371. LinesToStr([ // statements
  28372. 'this.DoIt = function () {',
  28373. '};',
  28374. 'this.DoSome = function (a, b, p) {',
  28375. '};',
  28376. 'this.Event = null;',
  28377. 'this.Func = null;',
  28378. '']),
  28379. LinesToStr([ // $mod.$main
  28380. '$mod.DoIt($mod.Event, $mod.Func);',
  28381. '$mod.DoSome($mod.Event, {',
  28382. ' p: $mod,',
  28383. ' get: function () {',
  28384. ' return this.p.Event;',
  28385. ' },',
  28386. ' set: function (v) {',
  28387. ' this.p.Event = v;',
  28388. ' }',
  28389. '}, $mod.Event);',
  28390. '$mod.DoSome($mod.Func, {',
  28391. ' p: $mod,',
  28392. ' get: function () {',
  28393. ' return this.p.Func;',
  28394. ' },',
  28395. ' set: function (v) {',
  28396. ' this.p.Func = v;',
  28397. ' }',
  28398. '}, $mod.Func);',
  28399. '']));
  28400. end;
  28401. procedure TTestModule.TestProcType_PassProcToArray;
  28402. begin
  28403. StartProgram(false);
  28404. Add([
  28405. 'type',
  28406. ' TFunc = function: longint;',
  28407. ' TArrFunc = array of TFunc;',
  28408. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28409. 'function GetIt: longint; begin end;',
  28410. 'var',
  28411. ' Func: tfunc;',
  28412. 'begin',
  28413. ' doit([]);',
  28414. ' doit([@GetIt]);',
  28415. ' doit([Func]);',
  28416. '']);
  28417. ConvertProgram;
  28418. CheckSource('TestProcType_PassProcToArray',
  28419. LinesToStr([ // statements
  28420. 'this.DoIt = function (Arr) {',
  28421. '};',
  28422. 'this.GetIt = function () {',
  28423. ' var Result = 0;',
  28424. ' return Result;',
  28425. '};',
  28426. 'this.Func = null;',
  28427. '']),
  28428. LinesToStr([ // $mod.$main
  28429. '$mod.DoIt([]);',
  28430. '$mod.DoIt([$mod.GetIt]);',
  28431. '$mod.DoIt([$mod.Func]);',
  28432. '']));
  28433. end;
  28434. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28435. begin
  28436. StartProgram(false);
  28437. Add([
  28438. '{$modeswitch externalclass}',
  28439. 'type',
  28440. ' TProc = reference to procedure(i: longint); safecall;',
  28441. ' TEvent = procedure(i: longint) of object; safecall;',
  28442. ' TExtA = class external name ''ExtObj''',
  28443. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28444. ' procedure DoSome(Id: longint = 1);',
  28445. ' procedure SetOnClick(const e: TEvent);',
  28446. ' property OnClick: TEvent write SetOnClick;',
  28447. ' class procedure Fly(Id: longint = 1); static;',
  28448. ' procedure SetOnShow(const p: TProc);',
  28449. ' property OnShow: TProc write SetOnShow;',
  28450. ' end;',
  28451. 'procedure Run(i: longint = 1);',
  28452. 'begin',
  28453. 'end;',
  28454. 'var',
  28455. ' Obj: texta;',
  28456. ' e: TEvent;',
  28457. ' p: TProc;',
  28458. 'begin',
  28459. ' e:=e;',
  28460. ' e:[email protected];',
  28461. ' e:[email protected];',
  28462. ' e:=TEvent(@obj.dosome);', // no safecall
  28463. ' obj.OnClick:[email protected];',
  28464. ' obj.OnClick:[email protected];',
  28465. ' obj.setonclick(@obj.doit);',
  28466. ' obj.setonclick(@obj.dosome);',
  28467. ' p:=@Run;',
  28468. ' p:[email protected];',
  28469. ' obj.OnShow:=@Run;',
  28470. ' obj.OnShow:[email protected];',
  28471. ' obj.setOnShow(@Run);',
  28472. ' obj.setOnShow(@TExtA.Fly);',
  28473. ' with obj do begin',
  28474. ' e:=@doit;',
  28475. ' e:=@dosome;',
  28476. ' OnClick:=@doit;',
  28477. ' OnClick:=@dosome;',
  28478. ' setonclick(@doit);',
  28479. ' setonclick(@dosome);',
  28480. ' OnShow:=@Run;',
  28481. ' setOnShow(@Run);',
  28482. ' end;']);
  28483. ConvertProgram;
  28484. CheckSource('TestProcType_SafeCallObjFPC',
  28485. LinesToStr([ // statements
  28486. 'this.Run = function (i) {',
  28487. '};',
  28488. 'this.Obj = null;',
  28489. 'this.e = null;',
  28490. 'this.p = null;',
  28491. '']),
  28492. LinesToStr([ // $mod.$main
  28493. '$mod.e = $mod.e;',
  28494. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28495. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28496. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28497. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28498. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28499. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28500. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28501. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28502. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28503. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28504. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28505. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28506. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28507. 'var $with = $mod.Obj;',
  28508. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28509. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28510. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28511. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28512. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28513. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28514. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28515. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28516. '']));
  28517. end;
  28518. procedure TTestModule.TestProcType_SafeCallDelphi;
  28519. begin
  28520. StartProgram(false);
  28521. Add([
  28522. '{$mode delphi}',
  28523. '{$modeswitch externalclass}',
  28524. 'type',
  28525. ' TProc = reference to procedure(i: longint); safecall;',
  28526. ' TEvent = procedure(i: longint) of object; safecall;',
  28527. ' TExtA = class external name ''ExtObj''',
  28528. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28529. ' procedure DoSome(Id: longint = 1);',
  28530. ' procedure SetOnClick(const e: TEvent);',
  28531. ' property OnClick: TEvent write SetOnClick;',
  28532. ' class procedure Fly(Id: longint = 1); static;',
  28533. ' procedure SetOnShow(const p: TProc);',
  28534. ' property OnShow: TProc write SetOnShow;',
  28535. ' end;',
  28536. 'procedure Run(i: longint = 1);',
  28537. 'begin',
  28538. 'end;',
  28539. 'var',
  28540. ' Obj: texta;',
  28541. ' e: TEvent;',
  28542. ' p: TProc;',
  28543. 'begin',
  28544. ' e:=e;',
  28545. ' e:=obj.doit;',
  28546. ' e:=obj.dosome;',
  28547. ' e:=TEvent(@obj.dosome);', // no safecall
  28548. ' obj.OnClick:=obj.doit;',
  28549. ' obj.OnClick:=obj.dosome;',
  28550. ' obj.setonclick(obj.doit);',
  28551. ' obj.setonclick(obj.dosome);',
  28552. ' p:=Run;',
  28553. ' p:=TExtA.Fly;',
  28554. ' obj.OnShow:=Run;',
  28555. ' obj.OnShow:=TExtA.Fly;',
  28556. ' obj.setOnShow(Run);',
  28557. ' obj.setOnShow(TExtA.Fly);',
  28558. ' with obj do begin',
  28559. ' e:=doit;',
  28560. ' e:=dosome;',
  28561. ' OnClick:=doit;',
  28562. ' OnClick:=dosome;',
  28563. ' setonclick(doit);',
  28564. ' setonclick(dosome);',
  28565. ' OnShow:=@Run;',
  28566. ' setOnShow(@Run);',
  28567. ' end;']);
  28568. ConvertProgram;
  28569. CheckSource('TestProcType_SafeCallDelphi',
  28570. LinesToStr([ // statements
  28571. 'this.Run = function (i) {',
  28572. '};',
  28573. 'this.Obj = null;',
  28574. 'this.e = null;',
  28575. 'this.p = null;',
  28576. '']),
  28577. LinesToStr([ // $mod.$main
  28578. '$mod.e = $mod.e;',
  28579. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28580. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28581. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28582. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28583. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28584. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28585. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28586. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28587. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28588. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28589. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28590. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28591. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28592. 'var $with = $mod.Obj;',
  28593. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28594. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28595. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28596. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28597. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28598. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28599. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28600. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28601. '']));
  28602. end;
  28603. procedure TTestModule.TestProcType_SafeCall_Arg;
  28604. begin
  28605. StartProgram(false);
  28606. Add([
  28607. 'type',
  28608. ' TSafecallProc = reference to procedure; safecall;',
  28609. 'procedure Fly(const aHandler: TSafecallProc);',
  28610. 'var',
  28611. ' P: TSafecallProc;',
  28612. 'begin',
  28613. ' P := aHandler;',
  28614. ' Fly(P);',
  28615. ' Fly(aHandler);',
  28616. 'end;',
  28617. 'begin',
  28618. ' Fly(nil);',
  28619. '']);
  28620. ConvertProgram;
  28621. CheckSource('TestProcType_SafeCall_Arg',
  28622. LinesToStr([ // statements
  28623. 'this.Fly = function (aHandler) {',
  28624. ' var P = null;',
  28625. ' P = aHandler;',
  28626. ' $mod.Fly(P);',
  28627. ' $mod.Fly(aHandler);',
  28628. '};',
  28629. '']),
  28630. LinesToStr([ // $mod.$main
  28631. '$mod.Fly(null);',
  28632. '']));
  28633. end;
  28634. procedure TTestModule.TestPointer;
  28635. begin
  28636. StartProgram(false);
  28637. Add(['type',
  28638. ' TObject = class end;',
  28639. ' TClass = class of TObject;',
  28640. ' TArrInt = array of longint;',
  28641. 'const',
  28642. ' n = nil;',
  28643. 'var',
  28644. ' v: jsvalue;',
  28645. ' Obj: tobject;',
  28646. ' C: tclass;',
  28647. ' a: tarrint;',
  28648. ' p: Pointer = nil;',
  28649. ' s: string;',
  28650. 'begin',
  28651. ' p:=p;',
  28652. ' p:=nil;',
  28653. ' if p=nil then;',
  28654. ' if nil=p then;',
  28655. ' if Assigned(p) then;',
  28656. ' p:=Pointer(v);',
  28657. ' p:=obj;',
  28658. ' p:=c;',
  28659. ' p:=a;',
  28660. ' p:=tobject;',
  28661. ' obj:=TObject(p);',
  28662. ' c:=TClass(p);',
  28663. ' a:=TArrInt(p);',
  28664. ' p:=n;',
  28665. ' p:=Pointer(a);',
  28666. ' p:=pointer(s);',
  28667. ' s:=string(p);',
  28668. '']);
  28669. ConvertProgram;
  28670. CheckSource('TestPointer',
  28671. LinesToStr([ // statements
  28672. 'rtl.createClass(this, "TObject", null, function () {',
  28673. ' this.$init = function () {',
  28674. ' };',
  28675. ' this.$final = function () {',
  28676. ' };',
  28677. '});',
  28678. 'this.n = null;',
  28679. 'this.v = undefined;',
  28680. 'this.Obj = null;',
  28681. 'this.C = null;',
  28682. 'this.a = [];',
  28683. 'this.p = null;',
  28684. 'this.s = "";',
  28685. '']),
  28686. LinesToStr([ // $mod.$main
  28687. '$mod.p = $mod.p;',
  28688. '$mod.p = null;',
  28689. 'if ($mod.p === null) ;',
  28690. 'if (null === $mod.p) ;',
  28691. 'if ($mod.p != null) ;',
  28692. '$mod.p = $mod.v;',
  28693. '$mod.p = $mod.Obj;',
  28694. '$mod.p = $mod.C;',
  28695. '$mod.p = $mod.a;',
  28696. '$mod.p = $mod.TObject;',
  28697. '$mod.Obj = $mod.p;',
  28698. '$mod.C = $mod.p;',
  28699. '$mod.a = $mod.p;',
  28700. '$mod.p = null;',
  28701. '$mod.p = $mod.a;',
  28702. '$mod.p = $mod.s;',
  28703. '$mod.s = $mod.p;',
  28704. '']));
  28705. end;
  28706. procedure TTestModule.TestPointer_Proc;
  28707. begin
  28708. StartProgram(false);
  28709. Add('type');
  28710. Add(' TObject = class');
  28711. Add(' procedure DoIt; virtual; abstract;');
  28712. Add(' end;');
  28713. Add('procedure DoSome; begin end;');
  28714. Add('var');
  28715. Add(' o: TObject;');
  28716. Add(' p: Pointer;');
  28717. Add('begin');
  28718. Add(' p:=@DoSome;');
  28719. Add(' p:[email protected];');
  28720. ConvertProgram;
  28721. CheckSource('TestPointer_Proc',
  28722. LinesToStr([ // statements
  28723. 'rtl.createClass(this, "TObject", null, function () {',
  28724. ' this.$init = function () {',
  28725. ' };',
  28726. ' this.$final = function () {',
  28727. ' };',
  28728. '});',
  28729. 'this.DoSome = function () {',
  28730. '};',
  28731. 'this.o = null;',
  28732. 'this.p = null;',
  28733. '']),
  28734. LinesToStr([ // $mod.$main
  28735. '$mod.p = $mod.DoSome;',
  28736. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28737. '']));
  28738. end;
  28739. procedure TTestModule.TestPointer_AssignRecordFail;
  28740. begin
  28741. StartProgram(false);
  28742. Add('type');
  28743. Add(' TRec = record end;');
  28744. Add('var');
  28745. Add(' p: Pointer;');
  28746. Add(' r: TRec;');
  28747. Add('begin');
  28748. Add(' p:=r;');
  28749. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28750. nIncompatibleTypesGotExpected);
  28751. ConvertProgram;
  28752. end;
  28753. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28754. begin
  28755. StartProgram(false);
  28756. Add('type');
  28757. Add(' TArr = array[boolean] of longint;');
  28758. Add('var');
  28759. Add(' p: Pointer;');
  28760. Add(' a: TArr;');
  28761. Add('begin');
  28762. Add(' p:=a;');
  28763. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28764. nIncompatibleTypesGotExpected);
  28765. ConvertProgram;
  28766. end;
  28767. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28768. begin
  28769. StartProgram(false);
  28770. Add([
  28771. 'procedure DoIt(args: array of jsvalue); begin end;',
  28772. 'procedure DoAll; varargs; begin end;',
  28773. 'var',
  28774. ' v: jsvalue;',
  28775. 'begin',
  28776. ' DoIt([pointer(v)]);',
  28777. ' DoAll(pointer(v));',
  28778. '']);
  28779. ConvertProgram;
  28780. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28781. LinesToStr([ // statements
  28782. 'this.DoIt = function (args) {',
  28783. '};',
  28784. 'this.DoAll = function () {',
  28785. '};',
  28786. 'this.v = undefined;',
  28787. '']),
  28788. LinesToStr([ // $mod.$main
  28789. '$mod.DoIt([$mod.v]);',
  28790. '$mod.DoAll($mod.v);',
  28791. '']));
  28792. end;
  28793. procedure TTestModule.TestPointer_NonRecordFail;
  28794. begin
  28795. StartProgram(false);
  28796. Add([
  28797. 'type',
  28798. ' p = ^longint;',
  28799. 'begin',
  28800. '']);
  28801. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28802. ConvertProgram;
  28803. end;
  28804. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28805. begin
  28806. StartProgram(false);
  28807. Add([
  28808. 'procedure DoIt(p: ^longint); begin end;',
  28809. 'begin',
  28810. '']);
  28811. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28812. ConvertProgram;
  28813. end;
  28814. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28815. begin
  28816. StartProgram(false);
  28817. Add([
  28818. 'var p: ^longint;',
  28819. 'begin',
  28820. '']);
  28821. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28822. ConvertProgram;
  28823. end;
  28824. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28825. begin
  28826. StartProgram(false);
  28827. Add([
  28828. 'function DoIt: ^longint; begin end;',
  28829. 'begin',
  28830. '']);
  28831. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28832. ConvertProgram;
  28833. end;
  28834. procedure TTestModule.TestPointer_AddrOperatorFail;
  28835. begin
  28836. StartProgram(false);
  28837. Add([
  28838. 'var i: longint;',
  28839. 'begin',
  28840. ' if @i=nil then ;',
  28841. '']);
  28842. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28843. ConvertProgram;
  28844. end;
  28845. procedure TTestModule.TestPointer_ArrayParamsFail;
  28846. begin
  28847. StartProgram(false);
  28848. Add([
  28849. 'var',
  28850. ' p: Pointer;',
  28851. 'begin',
  28852. ' p:=p[1];',
  28853. '']);
  28854. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28855. ConvertProgram;
  28856. end;
  28857. procedure TTestModule.TestPointer_PointerAddFail;
  28858. begin
  28859. StartProgram(false);
  28860. Add([
  28861. 'var',
  28862. ' p: Pointer;',
  28863. 'begin',
  28864. ' p:=p+1;',
  28865. '']);
  28866. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28867. ConvertProgram;
  28868. end;
  28869. procedure TTestModule.TestPointer_IncPointerFail;
  28870. begin
  28871. StartProgram(false);
  28872. Add([
  28873. 'var',
  28874. ' p: Pointer;',
  28875. 'begin',
  28876. ' inc(p,1);',
  28877. '']);
  28878. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28879. nIncompatibleTypeArgNo);
  28880. ConvertProgram;
  28881. end;
  28882. procedure TTestModule.TestPointer_Record;
  28883. begin
  28884. StartProgram(false);
  28885. Add([
  28886. 'type',
  28887. ' TRec = record x: longint; end;',
  28888. ' PRec = ^TRec;',
  28889. 'var',
  28890. ' r: TRec;',
  28891. ' p: PRec;',
  28892. ' q: ^TRec;',
  28893. ' Ptr: pointer;',
  28894. 'begin',
  28895. ' new(p);',
  28896. ' p:=@r;',
  28897. ' r:=p^;',
  28898. ' r.x:=p^.x;',
  28899. ' p^.x:=r.x;',
  28900. ' if p^.x=3 then ;',
  28901. ' if 4=p^.x then ;',
  28902. ' dispose(p);',
  28903. ' new(q);',
  28904. ' dispose(q);',
  28905. ' Ptr:=p;',
  28906. ' p:=PRec(ptr);',
  28907. '']);
  28908. ConvertProgram;
  28909. CheckSource('TestPointer_Record',
  28910. LinesToStr([ // statements
  28911. 'rtl.recNewT(this, "TRec", function () {',
  28912. ' this.x = 0;',
  28913. ' this.$eq = function (b) {',
  28914. ' return this.x === b.x;',
  28915. ' };',
  28916. ' this.$assign = function (s) {',
  28917. ' this.x = s.x;',
  28918. ' return this;',
  28919. ' };',
  28920. '});',
  28921. 'this.r = this.TRec.$new();',
  28922. 'this.p = null;',
  28923. 'this.q = null;',
  28924. 'this.Ptr = null;',
  28925. '']),
  28926. LinesToStr([ // $mod.$main
  28927. '$mod.p = $mod.TRec.$new();',
  28928. '$mod.p = $mod.r;',
  28929. '$mod.r.$assign($mod.p);',
  28930. '$mod.r.x = $mod.p.x;',
  28931. '$mod.p.x = $mod.r.x;',
  28932. 'if ($mod.p.x === 3) ;',
  28933. 'if (4 === $mod.p.x) ;',
  28934. '$mod.p = null;',
  28935. '$mod.q = $mod.TRec.$new();',
  28936. '$mod.q = null;',
  28937. '$mod.Ptr = $mod.p;',
  28938. '$mod.p = $mod.Ptr;',
  28939. '']));
  28940. end;
  28941. procedure TTestModule.TestPointer_RecordArg;
  28942. begin
  28943. StartProgram(false);
  28944. Add([
  28945. '{$modeswitch autoderef}',
  28946. 'type',
  28947. ' TRec = record x: longint; end;',
  28948. ' PRec = ^TRec;',
  28949. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  28950. 'begin',
  28951. ' a.x:=a.x;',
  28952. ' a^.x:=a^.x;',
  28953. ' with a^ do',
  28954. ' x:=x;',
  28955. 'end;',
  28956. 'function GetIt(p: PRec): PRec;',
  28957. 'begin',
  28958. ' p.x:=p.x;',
  28959. ' p^.x:=p^.x;',
  28960. ' with p^ do',
  28961. ' x:=x;',
  28962. 'end;',
  28963. 'var',
  28964. ' r: TRec;',
  28965. ' p: PRec;',
  28966. 'begin',
  28967. ' p:=GetIt(p);',
  28968. ' p^:=GetIt(@r)^;',
  28969. ' DoIt(p,p,p);',
  28970. ' DoIt(@r,p,p);',
  28971. '']);
  28972. ConvertProgram;
  28973. CheckSource('TestPointer_RecordArg',
  28974. LinesToStr([ // statements
  28975. 'rtl.recNewT(this, "TRec", function () {',
  28976. ' this.x = 0;',
  28977. ' this.$eq = function (b) {',
  28978. ' return this.x === b.x;',
  28979. ' };',
  28980. ' this.$assign = function (s) {',
  28981. ' this.x = s.x;',
  28982. ' return this;',
  28983. ' };',
  28984. '});',
  28985. 'this.DoIt = function (a, b, c) {',
  28986. ' var Result = $mod.TRec.$new();',
  28987. ' a.x = a.x;',
  28988. ' a.x = a.x;',
  28989. ' a.x = a.x;',
  28990. ' return Result;',
  28991. '};',
  28992. 'this.GetIt = function (p) {',
  28993. ' var Result = null;',
  28994. ' p.x = p.x;',
  28995. ' p.x = p.x;',
  28996. ' p.x = p.x;',
  28997. ' return Result;',
  28998. '};',
  28999. 'this.r = this.TRec.$new();',
  29000. 'this.p = null;',
  29001. '']),
  29002. LinesToStr([ // $mod.$main
  29003. '$mod.p = $mod.GetIt($mod.p);',
  29004. '$mod.p.$assign($mod.GetIt($mod.r));',
  29005. '$mod.DoIt($mod.p, {',
  29006. ' p: $mod,',
  29007. ' get: function () {',
  29008. ' return this.p.p;',
  29009. ' },',
  29010. ' set: function (v) {',
  29011. ' this.p.p = v;',
  29012. ' }',
  29013. '}, {',
  29014. ' p: $mod,',
  29015. ' get: function () {',
  29016. ' return this.p.p;',
  29017. ' },',
  29018. ' set: function (v) {',
  29019. ' this.p.p = v;',
  29020. ' }',
  29021. '});',
  29022. '$mod.DoIt($mod.r, {',
  29023. ' p: $mod,',
  29024. ' get: function () {',
  29025. ' return this.p.p;',
  29026. ' },',
  29027. ' set: function (v) {',
  29028. ' this.p.p = v;',
  29029. ' }',
  29030. '}, {',
  29031. ' p: $mod,',
  29032. ' get: function () {',
  29033. ' return this.p.p;',
  29034. ' },',
  29035. ' set: function (v) {',
  29036. ' this.p.p = v;',
  29037. ' }',
  29038. '});',
  29039. '']));
  29040. end;
  29041. procedure TTestModule.TestJSValue_AssignToJSValue;
  29042. begin
  29043. StartProgram(false);
  29044. Add('var');
  29045. Add(' v: jsvalue;');
  29046. Add(' i: longint;');
  29047. Add(' s: string;');
  29048. Add(' b: boolean;');
  29049. Add(' d: double;');
  29050. Add(' p: pointer;');
  29051. Add('begin');
  29052. Add(' v:=v;');
  29053. Add(' v:=1;');
  29054. Add(' v:=i;');
  29055. Add(' v:='''';');
  29056. Add(' v:=''c'';');
  29057. Add(' v:=''foo'';');
  29058. Add(' v:=s;');
  29059. Add(' v:=false;');
  29060. Add(' v:=true;');
  29061. Add(' v:=b;');
  29062. Add(' v:=0.1;');
  29063. Add(' v:=d;');
  29064. Add(' v:=nil;');
  29065. Add(' v:=p;');
  29066. ConvertProgram;
  29067. CheckSource('TestJSValue_AssignToJSValue',
  29068. LinesToStr([ // statements
  29069. 'this.v = undefined;',
  29070. 'this.i = 0;',
  29071. 'this.s = "";',
  29072. 'this.b = false;',
  29073. 'this.d = 0.0;',
  29074. 'this.p = null;',
  29075. '']),
  29076. LinesToStr([ // $mod.$main
  29077. '$mod.v = $mod.v;',
  29078. '$mod.v = 1;',
  29079. '$mod.v = $mod.i;',
  29080. '$mod.v = "";',
  29081. '$mod.v = "c";',
  29082. '$mod.v = "foo";',
  29083. '$mod.v = $mod.s;',
  29084. '$mod.v = false;',
  29085. '$mod.v = true;',
  29086. '$mod.v = $mod.b;',
  29087. '$mod.v = 0.1;',
  29088. '$mod.v = $mod.d;',
  29089. '$mod.v = null;',
  29090. '$mod.v = $mod.p;',
  29091. '']));
  29092. end;
  29093. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29094. begin
  29095. StartProgram(false);
  29096. Add('type');
  29097. Add(' integer = longint;');
  29098. Add(' TYesNo = boolean;');
  29099. Add(' TFloat = double;');
  29100. Add(' TCaption = string;');
  29101. Add(' TChar = char;');
  29102. Add('var');
  29103. Add(' v: jsvalue;');
  29104. Add(' i: integer;');
  29105. Add(' s: TCaption;');
  29106. Add(' b: TYesNo;');
  29107. Add(' d: TFloat;');
  29108. Add(' c: char;');
  29109. Add('begin');
  29110. Add(' i:=longint(v);');
  29111. Add(' i:=integer(v);');
  29112. Add(' s:=string(v);');
  29113. Add(' s:=TCaption(v);');
  29114. Add(' b:=boolean(v);');
  29115. Add(' b:=TYesNo(v);');
  29116. Add(' d:=double(v);');
  29117. Add(' d:=TFloat(v);');
  29118. Add(' c:=char(v);');
  29119. Add(' c:=TChar(v);');
  29120. ConvertProgram;
  29121. CheckSource('TestJSValue_TypeCastToBaseType',
  29122. LinesToStr([ // statements
  29123. 'this.v = undefined;',
  29124. 'this.i = 0;',
  29125. 'this.s = "";',
  29126. 'this.b = false;',
  29127. 'this.d = 0.0;',
  29128. 'this.c = "";',
  29129. '']),
  29130. LinesToStr([ // $mod.$main
  29131. '$mod.i = rtl.trunc($mod.v);',
  29132. '$mod.i = rtl.trunc($mod.v);',
  29133. '$mod.s = "" + $mod.v;',
  29134. '$mod.s = "" + $mod.v;',
  29135. '$mod.b = !($mod.v == false);',
  29136. '$mod.b = !($mod.v == false);',
  29137. '$mod.d = rtl.getNumber($mod.v);',
  29138. '$mod.d = rtl.getNumber($mod.v);',
  29139. '$mod.c = rtl.getChar($mod.v);',
  29140. '$mod.c = rtl.getChar($mod.v);',
  29141. '']));
  29142. end;
  29143. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29144. begin
  29145. StartProgram(false);
  29146. Add([
  29147. 'type',
  29148. ' TArr = array of word;',
  29149. ' TRec = record end;',
  29150. ' TSet = set of boolean;',
  29151. 'procedure Fly(v: jsvalue);',
  29152. 'begin',
  29153. 'end;',
  29154. 'var',
  29155. ' a: TArr;',
  29156. ' r: TRec;',
  29157. ' s: TSet;',
  29158. 'begin',
  29159. ' Fly(jsvalue(a));',
  29160. ' Fly(jsvalue(r));',
  29161. ' Fly(jsvalue(s));',
  29162. '']);
  29163. ConvertProgram;
  29164. CheckSource('TestJSValue_TypecastToJSValue',
  29165. LinesToStr([ // statements
  29166. 'rtl.recNewT(this, "TRec", function () {',
  29167. ' this.$eq = function (b) {',
  29168. ' return true;',
  29169. ' };',
  29170. ' this.$assign = function (s) {',
  29171. ' return this;',
  29172. ' };',
  29173. '});',
  29174. 'this.Fly = function (v) {',
  29175. '};',
  29176. 'this.a = [];',
  29177. 'this.r = this.TRec.$new();',
  29178. 'this.s = {};',
  29179. '']),
  29180. LinesToStr([ // $mod.$main
  29181. '$mod.Fly($mod.a);',
  29182. '$mod.Fly($mod.r);',
  29183. '$mod.Fly($mod.s);',
  29184. '']));
  29185. end;
  29186. procedure TTestModule.TestJSValue_Equal;
  29187. begin
  29188. StartProgram(false);
  29189. Add('type');
  29190. Add(' integer = longint;');
  29191. Add(' TYesNo = boolean;');
  29192. Add(' TFloat = double;');
  29193. Add(' TCaption = string;');
  29194. Add(' TChar = char;');
  29195. Add(' TMulti = JSValue;');
  29196. Add('var');
  29197. Add(' v: jsvalue;');
  29198. Add(' i: integer;');
  29199. Add(' s: TCaption;');
  29200. Add(' b: TYesNo;');
  29201. Add(' d: TFloat;');
  29202. Add(' c: char;');
  29203. Add(' m: TMulti;');
  29204. Add('begin');
  29205. Add(' b:=v=v;');
  29206. Add(' b:=v<>v;');
  29207. Add(' b:=v=1;');
  29208. Add(' b:=v<>1;');
  29209. Add(' b:=2=v;');
  29210. Add(' b:=2<>v;');
  29211. Add(' b:=v=i;');
  29212. Add(' b:=i=v;');
  29213. Add(' b:=v=nil;');
  29214. Add(' b:=nil=v;');
  29215. Add(' b:=v=false;');
  29216. Add(' b:=true=v;');
  29217. Add(' b:=v=b;');
  29218. Add(' b:=b=v;');
  29219. Add(' b:=v=s;');
  29220. Add(' b:=s=v;');
  29221. Add(' b:=v=''foo'';');
  29222. Add(' b:=''''=v;');
  29223. Add(' b:=v=d;');
  29224. Add(' b:=d=v;');
  29225. Add(' b:=v=3.4;');
  29226. Add(' b:=5.6=v;');
  29227. Add(' b:=v=c;');
  29228. Add(' b:=c=v;');
  29229. Add(' b:=m=m;');
  29230. Add(' b:=v=m;');
  29231. Add(' b:=m=v;');
  29232. ConvertProgram;
  29233. CheckSource('TestJSValue_Equal',
  29234. LinesToStr([ // statements
  29235. 'this.v = undefined;',
  29236. 'this.i = 0;',
  29237. 'this.s = "";',
  29238. 'this.b = false;',
  29239. 'this.d = 0.0;',
  29240. 'this.c = "";',
  29241. 'this.m = undefined;',
  29242. '']),
  29243. LinesToStr([ // $mod.$main
  29244. '$mod.b = $mod.v == $mod.v;',
  29245. '$mod.b = $mod.v != $mod.v;',
  29246. '$mod.b = $mod.v == 1;',
  29247. '$mod.b = $mod.v != 1;',
  29248. '$mod.b = 2 == $mod.v;',
  29249. '$mod.b = 2 != $mod.v;',
  29250. '$mod.b = $mod.v == $mod.i;',
  29251. '$mod.b = $mod.i == $mod.v;',
  29252. '$mod.b = $mod.v == null;',
  29253. '$mod.b = null == $mod.v;',
  29254. '$mod.b = $mod.v == false;',
  29255. '$mod.b = true == $mod.v;',
  29256. '$mod.b = $mod.v == $mod.b;',
  29257. '$mod.b = $mod.b == $mod.v;',
  29258. '$mod.b = $mod.v == $mod.s;',
  29259. '$mod.b = $mod.s == $mod.v;',
  29260. '$mod.b = $mod.v == "foo";',
  29261. '$mod.b = "" == $mod.v;',
  29262. '$mod.b = $mod.v == $mod.d;',
  29263. '$mod.b = $mod.d == $mod.v;',
  29264. '$mod.b = $mod.v == 3.4;',
  29265. '$mod.b = 5.6 == $mod.v;',
  29266. '$mod.b = $mod.v == $mod.c;',
  29267. '$mod.b = $mod.c == $mod.v;',
  29268. '$mod.b = $mod.m == $mod.m;',
  29269. '$mod.b = $mod.v == $mod.m;',
  29270. '$mod.b = $mod.m == $mod.v;',
  29271. '']));
  29272. end;
  29273. procedure TTestModule.TestJSValue_If;
  29274. begin
  29275. StartProgram(false);
  29276. Add([
  29277. 'procedure Fly(var u);',
  29278. 'begin',
  29279. ' if jsvalue(u) then ;',
  29280. 'end;',
  29281. 'var',
  29282. ' v: jsvalue;',
  29283. 'begin',
  29284. ' if v then ;',
  29285. ' while v do ;',
  29286. ' repeat until v;',
  29287. '']);
  29288. ConvertProgram;
  29289. CheckSource('TestJSValue_If',
  29290. LinesToStr([ // statements
  29291. 'this.Fly = function (u) {',
  29292. ' if (u.get()) ;',
  29293. '};',
  29294. 'this.v = undefined;',
  29295. '']),
  29296. LinesToStr([ // $mod.$main
  29297. 'if ($mod.v) ;',
  29298. 'while($mod.v){',
  29299. '};',
  29300. 'do{',
  29301. '} while(!$mod.v);',
  29302. '']));
  29303. end;
  29304. procedure TTestModule.TestJSValue_Not;
  29305. begin
  29306. StartProgram(false);
  29307. Add([
  29308. 'var',
  29309. ' v: jsvalue;',
  29310. ' b: boolean;',
  29311. 'begin',
  29312. ' b:=not v;',
  29313. ' if not v then ;',
  29314. ' while not v do ;',
  29315. ' repeat until not v;',
  29316. '']);
  29317. ConvertProgram;
  29318. CheckSource('TestJSValue_If',
  29319. LinesToStr([ // statements
  29320. 'this.v = undefined;',
  29321. 'this.b = false;',
  29322. '']),
  29323. LinesToStr([ // $mod.$main
  29324. '$mod.b=!$mod.v;',
  29325. 'if (!$mod.v) ;',
  29326. 'while(!$mod.v){',
  29327. '};',
  29328. 'do{',
  29329. '} while($mod.v);',
  29330. '']));
  29331. end;
  29332. procedure TTestModule.TestJSValue_Enum;
  29333. begin
  29334. StartProgram(false);
  29335. Add('type');
  29336. Add(' TColor = (red, blue);');
  29337. Add(' TRedBlue = TColor;');
  29338. Add('var');
  29339. Add(' v: jsvalue;');
  29340. Add(' e: TColor;');
  29341. Add('begin');
  29342. Add(' v:=e;');
  29343. Add(' v:=TColor(e);');
  29344. Add(' v:=TRedBlue(e);');
  29345. Add(' e:=TColor(v);');
  29346. Add(' e:=TRedBlue(v);');
  29347. ConvertProgram;
  29348. CheckSource('TestJSValue_Enum',
  29349. LinesToStr([ // statements
  29350. 'this.TColor = {',
  29351. ' "0": "red",',
  29352. ' red: 0,',
  29353. ' "1": "blue",',
  29354. ' blue: 1',
  29355. '};',
  29356. 'this.v = undefined;',
  29357. 'this.e = 0;',
  29358. '']),
  29359. LinesToStr([ // $mod.$main
  29360. '$mod.v = $mod.e;',
  29361. '$mod.v = $mod.e;',
  29362. '$mod.v = $mod.e;',
  29363. '$mod.e = $mod.v;',
  29364. '$mod.e = $mod.v;',
  29365. '']));
  29366. end;
  29367. procedure TTestModule.TestJSValue_ClassInstance;
  29368. begin
  29369. StartProgram(false);
  29370. Add([
  29371. 'type',
  29372. ' TObject = class',
  29373. ' end;',
  29374. ' TBirdObject = TObject;',
  29375. 'var',
  29376. ' v: jsvalue;',
  29377. ' o: TObject;',
  29378. 'begin',
  29379. ' v:=o;',
  29380. ' v:=TObject(o);',
  29381. ' v:=TBirdObject(o);',
  29382. ' o:=TObject(v);',
  29383. ' o:=TBirdObject(v);',
  29384. ' if v is TObject then ;',
  29385. '']);
  29386. ConvertProgram;
  29387. CheckSource('TestJSValue_ClassInstance',
  29388. LinesToStr([ // statements
  29389. 'rtl.createClass(this, "TObject", null, function () {',
  29390. ' this.$init = function () {',
  29391. ' };',
  29392. ' this.$final = function () {',
  29393. ' };',
  29394. '});',
  29395. 'this.v = undefined;',
  29396. 'this.o = null;',
  29397. '']),
  29398. LinesToStr([ // $mod.$main
  29399. '$mod.v = $mod.o;',
  29400. '$mod.v = $mod.o;',
  29401. '$mod.v = $mod.o;',
  29402. '$mod.o = rtl.getObject($mod.v);',
  29403. '$mod.o = rtl.getObject($mod.v);',
  29404. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29405. '']));
  29406. end;
  29407. procedure TTestModule.TestJSValue_ClassOf;
  29408. begin
  29409. StartProgram(false);
  29410. Add([
  29411. 'type',
  29412. ' TClass = class of TObject;',
  29413. ' TObject = class',
  29414. ' end;',
  29415. ' TBirds = class of TBird;',
  29416. ' TBird = class(TObject) end;',
  29417. 'var',
  29418. ' v: jsvalue;',
  29419. ' c: TClass;',
  29420. 'begin',
  29421. ' v:=c;',
  29422. ' v:=TObject;',
  29423. ' v:=TClass(c);',
  29424. ' v:=TBirds(c);',
  29425. ' c:=TClass(v);',
  29426. ' c:=TBirds(v);',
  29427. ' if v is TClass then ;',
  29428. '']);
  29429. ConvertProgram;
  29430. CheckSource('TestJSValue_ClassOf',
  29431. LinesToStr([ // statements
  29432. 'rtl.createClass(this, "TObject", null, function () {',
  29433. ' this.$init = function () {',
  29434. ' };',
  29435. ' this.$final = function () {',
  29436. ' };',
  29437. '});',
  29438. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29439. '});',
  29440. 'this.v = undefined;',
  29441. 'this.c = null;',
  29442. '']),
  29443. LinesToStr([ // $mod.$main
  29444. '$mod.v = $mod.c;',
  29445. '$mod.v = $mod.TObject;',
  29446. '$mod.v = $mod.c;',
  29447. '$mod.v = $mod.c;',
  29448. '$mod.c = rtl.getObject($mod.v);',
  29449. '$mod.c = rtl.getObject($mod.v);',
  29450. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29451. '']));
  29452. end;
  29453. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29454. begin
  29455. StartProgram(false);
  29456. Add([
  29457. 'type',
  29458. ' integer = longint;',
  29459. ' TArray = array of JSValue;',
  29460. ' TArrgh = tarray;',
  29461. ' TArrInt = array of integer;',
  29462. 'var',
  29463. ' v: jsvalue;',
  29464. ' TheArray: tarray = (1,''2'');',
  29465. ' Arr: tarrgh;',
  29466. ' i: integer;',
  29467. ' ArrInt: tarrint;',
  29468. 'begin',
  29469. ' arr:=thearray;',
  29470. ' thearray:=arr;',
  29471. ' setlength(arr,2);',
  29472. ' setlength(thearray,3);',
  29473. ' arr[4]:=v;',
  29474. ' arr[5]:=length(thearray);',
  29475. ' arr[6]:=nil;',
  29476. ' arr[7]:=thearray[8];',
  29477. ' arr[low(arr)]:=high(thearray);',
  29478. ' arr:=arrint;',
  29479. ' arrInt:=tarrint(arr);',
  29480. ' if TheArray = nil then ;',
  29481. ' if nil = TheArray then ;',
  29482. ' if TheArray <> nil then ;',
  29483. ' if nil <> TheArray then ;',
  29484. '']);
  29485. ConvertProgram;
  29486. CheckSource('TestJSValue_ArrayOfJSValue',
  29487. LinesToStr([ // statements
  29488. 'this.v = undefined;',
  29489. 'this.TheArray = [1, "2"];',
  29490. 'this.Arr = [];',
  29491. 'this.i = 0;',
  29492. 'this.ArrInt = [];',
  29493. '']),
  29494. LinesToStr([ // $mod.$main
  29495. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29496. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29497. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29498. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29499. '$mod.Arr[4] = $mod.v;',
  29500. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29501. '$mod.Arr[6] = null;',
  29502. '$mod.Arr[7] = $mod.TheArray[8];',
  29503. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29504. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29505. '$mod.ArrInt = $mod.Arr;',
  29506. 'if (rtl.length($mod.TheArray) === 0) ;',
  29507. 'if (rtl.length($mod.TheArray) === 0) ;',
  29508. 'if (rtl.length($mod.TheArray) > 0) ;',
  29509. 'if (rtl.length($mod.TheArray) > 0) ;',
  29510. '']));
  29511. end;
  29512. procedure TTestModule.TestJSValue_ArrayLit;
  29513. begin
  29514. StartProgram(false);
  29515. Add([
  29516. 'type',
  29517. ' TFlag = (big,small);',
  29518. ' TArray = array of JSValue;',
  29519. ' TObject = class end;',
  29520. ' TClass = class of TObject;',
  29521. 'var',
  29522. ' v: jsvalue;',
  29523. ' a: TArray;',
  29524. ' o: TObject;',
  29525. 'begin',
  29526. ' a:=[];',
  29527. ' a:=[1];',
  29528. ' a:=[1,2];',
  29529. ' a:=[big];',
  29530. ' a:=[1,big];',
  29531. ' a:=[o,nil];',
  29532. '']);
  29533. ConvertProgram;
  29534. CheckSource('TestJSValue_ArrayLit',
  29535. LinesToStr([ // statements
  29536. 'this.TFlag = {',
  29537. ' "0": "big",',
  29538. ' big: 0,',
  29539. ' "1": "small",',
  29540. ' small: 1',
  29541. '};',
  29542. 'rtl.createClass(this, "TObject", null, function () {',
  29543. ' this.$init = function () {',
  29544. ' };',
  29545. ' this.$final = function () {',
  29546. ' };',
  29547. '});',
  29548. 'this.v = undefined;',
  29549. 'this.a = [];',
  29550. 'this.o = null;',
  29551. '']),
  29552. LinesToStr([ // $mod.$main
  29553. '$mod.a = [];',
  29554. '$mod.a = [1];',
  29555. '$mod.a = [1, 2];',
  29556. '$mod.a = [$mod.TFlag.big];',
  29557. '$mod.a = [1, $mod.TFlag.big];',
  29558. '$mod.a = [$mod.o, null];',
  29559. '']));
  29560. end;
  29561. procedure TTestModule.TestJSValue_Params;
  29562. begin
  29563. StartProgram(false);
  29564. Add('type');
  29565. Add(' integer = longint;');
  29566. Add(' TYesNo = boolean;');
  29567. Add(' TFloat = double;');
  29568. Add(' TCaption = string;');
  29569. Add(' TChar = char;');
  29570. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29571. Add('var');
  29572. Add(' l: jsvalue;');
  29573. Add('begin');
  29574. Add(' a:=a;');
  29575. Add(' l:=b;');
  29576. Add(' c:=c;');
  29577. Add(' d:=d;');
  29578. Add(' Result:=l;');
  29579. Add('end;');
  29580. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29581. Add('var');
  29582. Add(' v: jsvalue;');
  29583. Add(' i: integer;');
  29584. Add(' b: TYesNo;');
  29585. Add(' d: TFloat;');
  29586. Add(' s: TCaption;');
  29587. Add(' c: TChar;');
  29588. Add('begin');
  29589. Add(' v:=doit(v,v,v,v);');
  29590. Add(' i:=integer(dosome(i,i));');
  29591. Add(' b:=TYesNo(dosome(b,b));');
  29592. Add(' d:=TFloat(dosome(d,d));');
  29593. Add(' s:=TCaption(dosome(s,s));');
  29594. Add(' c:=TChar(dosome(c,c));');
  29595. ConvertProgram;
  29596. CheckSource('TestJSValue_Params',
  29597. LinesToStr([ // statements
  29598. 'this.DoIt = function (a, b, c, d) {',
  29599. ' var Result = undefined;',
  29600. ' var l = undefined;',
  29601. ' a = a;',
  29602. ' l = b;',
  29603. ' c.set(c.get());',
  29604. ' d.set(d.get());',
  29605. ' Result = l;',
  29606. ' return Result;',
  29607. '};',
  29608. 'this.DoSome = function (a, b) {',
  29609. ' var Result = undefined;',
  29610. ' return Result;',
  29611. '};',
  29612. 'this.v = undefined;',
  29613. 'this.i = 0;',
  29614. 'this.b = false;',
  29615. 'this.d = 0.0;',
  29616. 'this.s = "";',
  29617. 'this.c = "";',
  29618. '']),
  29619. LinesToStr([ // $mod.$main
  29620. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29621. ' p: $mod,',
  29622. ' get: function () {',
  29623. ' return this.p.v;',
  29624. ' },',
  29625. ' set: function (v) {',
  29626. ' this.p.v = v;',
  29627. ' }',
  29628. '}, {',
  29629. ' p: $mod,',
  29630. ' get: function () {',
  29631. ' return this.p.v;',
  29632. ' },',
  29633. ' set: function (v) {',
  29634. ' this.p.v = v;',
  29635. ' }',
  29636. '});',
  29637. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29638. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29639. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29640. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29641. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29642. '']));
  29643. end;
  29644. procedure TTestModule.TestJSValue_UntypedParam;
  29645. begin
  29646. StartProgram(false);
  29647. Add('function DoIt(const a; var b; out c): jsvalue;');
  29648. Add('begin');
  29649. Add(' Result:=a;');
  29650. Add(' Result:=b;');
  29651. Add(' Result:=c;');
  29652. Add(' b:=Result;');
  29653. Add(' c:=Result;');
  29654. Add('end;');
  29655. Add('var i: longint;');
  29656. Add('begin');
  29657. Add(' doit(i,i,i);');
  29658. ConvertProgram;
  29659. CheckSource('TestJSValue_UntypedParam',
  29660. LinesToStr([ // statements
  29661. 'this.DoIt = function (a, b, c) {',
  29662. ' var Result = undefined;',
  29663. ' Result = a;',
  29664. ' Result = b.get();',
  29665. ' Result = c.get();',
  29666. ' b.set(Result);',
  29667. ' c.set(Result);',
  29668. ' return Result;',
  29669. '};',
  29670. 'this.i = 0;',
  29671. '']),
  29672. LinesToStr([ // $mod.$main
  29673. '$mod.DoIt($mod.i, {',
  29674. ' p: $mod,',
  29675. ' get: function () {',
  29676. ' return this.p.i;',
  29677. ' },',
  29678. ' set: function (v) {',
  29679. ' this.p.i = v;',
  29680. ' }',
  29681. '}, {',
  29682. ' p: $mod,',
  29683. ' get: function () {',
  29684. ' return this.p.i;',
  29685. ' },',
  29686. ' set: function (v) {',
  29687. ' this.p.i = v;',
  29688. ' }',
  29689. '});',
  29690. '']));
  29691. end;
  29692. procedure TTestModule.TestJSValue_FuncResultType;
  29693. begin
  29694. StartProgram(false);
  29695. Add('type');
  29696. Add(' integer = longint;');
  29697. Add(' TJSValueArray = array of JSValue;');
  29698. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29699. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29700. Add('begin');
  29701. Add(' while Compare(P,aList[0])>0 do ;');
  29702. Add('end;');
  29703. Add('var');
  29704. Add(' Compare: TListSortCompare;');
  29705. Add(' V: JSValue;');
  29706. Add(' i: integer;');
  29707. Add('begin');
  29708. Add(' if Compare(V,V)>0 then ;');
  29709. Add(' if Compare(i,i)>1 then ;');
  29710. Add(' if Compare(nil,false)>2 then ;');
  29711. Add(' if Compare(1,true)>3 then ;');
  29712. ConvertProgram;
  29713. CheckSource('TestJSValue_UntypedParam',
  29714. LinesToStr([ // statements
  29715. 'this.Sort = function (P, aList, Compare) {',
  29716. ' while (Compare(P, aList[0]) > 0) {',
  29717. ' };',
  29718. '};',
  29719. 'this.Compare = null;',
  29720. 'this.V = undefined;',
  29721. 'this.i = 0;',
  29722. '']),
  29723. LinesToStr([ // $mod.$main
  29724. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29725. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29726. 'if ($mod.Compare(null, false) > 2) ;',
  29727. 'if ($mod.Compare(1, true) > 3) ;',
  29728. '']));
  29729. end;
  29730. procedure TTestModule.TestJSValue_ProcType_Assign;
  29731. begin
  29732. StartProgram(false);
  29733. Add('type');
  29734. Add(' integer = longint;');
  29735. Add(' TObject = class');
  29736. Add(' class function GetGlob: integer;');
  29737. Add(' function Getter: integer;');
  29738. Add(' end;');
  29739. Add('class function TObject.GetGlob: integer;');
  29740. Add('var v1: jsvalue;');
  29741. Add('begin');
  29742. Add(' v1:=@GetGlob;');
  29743. Add(' v1:[email protected];');
  29744. Add('end;');
  29745. Add('function TObject.Getter: integer;');
  29746. Add('var v2: jsvalue;');
  29747. Add('begin');
  29748. Add(' v2:=@Getter;');
  29749. Add(' v2:[email protected];');
  29750. Add(' v2:=@GetGlob;');
  29751. Add(' v2:[email protected];');
  29752. Add('end;');
  29753. Add('function GetIt(i: integer): integer;');
  29754. Add('var v3: jsvalue;');
  29755. Add('begin');
  29756. Add(' v3:=@GetIt;');
  29757. Add('end;');
  29758. Add('var');
  29759. Add(' V: JSValue;');
  29760. Add(' o: TObject;');
  29761. Add('begin');
  29762. Add(' v:=@GetIt;');
  29763. Add(' v:[email protected];');
  29764. Add(' v:[email protected];');
  29765. ConvertProgram;
  29766. CheckSource('TestJSValue_ProcType_Assign',
  29767. LinesToStr([ // statements
  29768. 'rtl.createClass(this, "TObject", null, function () {',
  29769. ' this.$init = function () {',
  29770. ' };',
  29771. ' this.$final = function () {',
  29772. ' };',
  29773. ' this.GetGlob = function () {',
  29774. ' var Result = 0;',
  29775. ' var v1 = undefined;',
  29776. ' v1 = rtl.createCallback(this, "GetGlob");',
  29777. ' v1 = rtl.createCallback(this, "GetGlob");',
  29778. ' return Result;',
  29779. ' };',
  29780. ' this.Getter = function () {',
  29781. ' var Result = 0;',
  29782. ' var v2 = undefined;',
  29783. ' v2 = rtl.createCallback(this, "Getter");',
  29784. ' v2 = rtl.createCallback(this, "Getter");',
  29785. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29786. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29787. ' return Result;',
  29788. ' };',
  29789. '});',
  29790. 'this.GetIt = function (i) {',
  29791. ' var Result = 0;',
  29792. ' var v3 = undefined;',
  29793. ' v3 = $mod.GetIt;',
  29794. ' return Result;',
  29795. '};',
  29796. 'this.V = undefined;',
  29797. 'this.o = null;',
  29798. '']),
  29799. LinesToStr([ // $mod.$main
  29800. '$mod.V = $mod.GetIt;',
  29801. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29802. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29803. '']));
  29804. end;
  29805. procedure TTestModule.TestJSValue_ProcType_Equal;
  29806. begin
  29807. StartProgram(false);
  29808. Add('type');
  29809. Add(' integer = longint;');
  29810. Add(' TObject = class');
  29811. Add(' class function GetGlob: integer;');
  29812. Add(' function Getter: integer;');
  29813. Add(' end;');
  29814. Add('class function TObject.GetGlob: integer;');
  29815. Add('var v1: jsvalue;');
  29816. Add('begin');
  29817. Add(' if v1=@GetGlob then;');
  29818. Add(' if [email protected] then ;');
  29819. Add('end;');
  29820. Add('function TObject.Getter: integer;');
  29821. Add('var v2: jsvalue;');
  29822. Add('begin');
  29823. Add(' if v2=@Getter then;');
  29824. Add(' if [email protected] then ;');
  29825. Add(' if v2=@GetGlob then;');
  29826. Add(' if [email protected] then;');
  29827. Add('end;');
  29828. Add('function GetIt(i: integer): integer;');
  29829. Add('var v3: jsvalue;');
  29830. Add('begin');
  29831. Add(' if v3=@GetIt then;');
  29832. Add('end;');
  29833. Add('var');
  29834. Add(' V: JSValue;');
  29835. Add(' o: TObject;');
  29836. Add('begin');
  29837. Add(' if v=@GetIt then;');
  29838. Add(' if [email protected] then;');
  29839. Add(' if [email protected] then;');
  29840. Add(' if @GetIt=v then;');
  29841. Add(' if @o.Getter=v then;');
  29842. Add(' if @o.GetGlob=v then;');
  29843. ConvertProgram;
  29844. CheckSource('TestJSValue_ProcType_Equal',
  29845. LinesToStr([ // statements
  29846. 'rtl.createClass(this, "TObject", null, function () {',
  29847. ' this.$init = function () {',
  29848. ' };',
  29849. ' this.$final = function () {',
  29850. ' };',
  29851. ' this.GetGlob = function () {',
  29852. ' var Result = 0;',
  29853. ' var v1 = undefined;',
  29854. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29855. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29856. ' return Result;',
  29857. ' };',
  29858. ' this.Getter = function () {',
  29859. ' var Result = 0;',
  29860. ' var v2 = undefined;',
  29861. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29862. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29863. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29864. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29865. ' return Result;',
  29866. ' };',
  29867. '});',
  29868. 'this.GetIt = function (i) {',
  29869. ' var Result = 0;',
  29870. ' var v3 = undefined;',
  29871. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29872. ' return Result;',
  29873. '};',
  29874. 'this.V = undefined;',
  29875. 'this.o = null;',
  29876. '']),
  29877. LinesToStr([ // $mod.$main
  29878. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29879. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29880. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29881. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29882. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29883. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29884. '']));
  29885. end;
  29886. procedure TTestModule.TestJSValue_ProcType_Param;
  29887. begin
  29888. StartProgram(false);
  29889. Add([
  29890. 'type',
  29891. ' variant = jsvalue;',
  29892. ' TArrVariant = array of variant;',
  29893. ' TArrVar2 = TArrVariant;',
  29894. ' TFuncInt = function: longint;',
  29895. 'function GetIt: longint;',
  29896. 'begin',
  29897. 'end;',
  29898. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29899. 'var v: variant;',
  29900. 'begin',
  29901. ' v:=arr[1];',
  29902. 'end;',
  29903. 'var s: string;',
  29904. 'begin',
  29905. ' DoIt(GetIt,[]);',
  29906. ' DoIt(@GetIt,[]);',
  29907. ' DoIt(1,[s,GetIt]);',
  29908. ' DoIt(1,[s,@GetIt]);',
  29909. '']);
  29910. ConvertProgram;
  29911. CheckSource('TestJSValue_ProcType_Param',
  29912. LinesToStr([ // statements
  29913. 'this.GetIt = function () {',
  29914. ' var Result = 0;',
  29915. ' return Result;',
  29916. '};',
  29917. 'this.DoIt = function (p, Arr) {',
  29918. ' var v = undefined;',
  29919. ' v = Arr[1];',
  29920. '};',
  29921. 'this.s = "";',
  29922. '']),
  29923. LinesToStr([ // $mod.$main
  29924. '$mod.DoIt($mod.GetIt(), []);',
  29925. '$mod.DoIt($mod.GetIt, []);',
  29926. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29927. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29928. '']));
  29929. end;
  29930. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29931. begin
  29932. StartProgram(false);
  29933. Add([
  29934. 'var',
  29935. ' v: JSValue;',
  29936. ' p: Pointer;',
  29937. 'begin',
  29938. ' p:=v;',
  29939. '']);
  29940. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29941. nIncompatibleTypesGotExpected);
  29942. ConvertProgram;
  29943. end;
  29944. procedure TTestModule.TestJSValue_OverloadDouble;
  29945. begin
  29946. StartProgram(false);
  29947. Add([
  29948. 'type',
  29949. ' integer = longint;',
  29950. ' tdatetime = double;',
  29951. 'procedure DoIt(d: double); begin end;',
  29952. 'procedure DoIt(v: jsvalue); begin end;',
  29953. 'var',
  29954. ' d: double;',
  29955. ' dt: tdatetime;',
  29956. ' i: integer;',
  29957. ' b: byte;',
  29958. ' shi: shortint;',
  29959. ' w: word;',
  29960. ' smi: smallint;',
  29961. ' lw: longword;',
  29962. ' li: longint;',
  29963. ' ni: nativeint;',
  29964. ' nu: nativeuint;',
  29965. 'begin',
  29966. ' DoIt(d);',
  29967. ' DoIt(dt);',
  29968. ' DoIt(i);',
  29969. ' DoIt(b);',
  29970. ' DoIt(shi);',
  29971. ' DoIt(w);',
  29972. ' DoIt(smi);',
  29973. ' DoIt(lw);',
  29974. ' DoIt(li);',
  29975. ' DoIt(ni);',
  29976. ' DoIt(nu);',
  29977. '']);
  29978. ConvertProgram;
  29979. CheckSource('TestJSValue_OverloadDouble',
  29980. LinesToStr([ // statements
  29981. 'this.DoIt = function (d) {',
  29982. '};',
  29983. 'this.DoIt$1 = function (v) {',
  29984. '};',
  29985. 'this.d = 0.0;',
  29986. 'this.dt = 0.0;',
  29987. 'this.i = 0;',
  29988. 'this.b = 0;',
  29989. 'this.shi = 0;',
  29990. 'this.w = 0;',
  29991. 'this.smi = 0;',
  29992. 'this.lw = 0;',
  29993. 'this.li = 0;',
  29994. 'this.ni = 0;',
  29995. 'this.nu = 0;',
  29996. '']),
  29997. LinesToStr([ // $mod.$main
  29998. '$mod.DoIt($mod.d);',
  29999. '$mod.DoIt($mod.dt);',
  30000. '$mod.DoIt$1($mod.i);',
  30001. '$mod.DoIt$1($mod.b);',
  30002. '$mod.DoIt$1($mod.shi);',
  30003. '$mod.DoIt$1($mod.w);',
  30004. '$mod.DoIt$1($mod.smi);',
  30005. '$mod.DoIt$1($mod.lw);',
  30006. '$mod.DoIt$1($mod.li);',
  30007. '$mod.DoIt$1($mod.ni);',
  30008. '$mod.DoIt$1($mod.nu);',
  30009. '']));
  30010. end;
  30011. procedure TTestModule.TestJSValue_OverloadNativeInt;
  30012. begin
  30013. StartProgram(false);
  30014. Add([
  30015. 'type',
  30016. ' integer = longint;',
  30017. ' int53 = nativeint;',
  30018. ' tdatetime = double;',
  30019. 'procedure DoIt(n: nativeint); begin end;',
  30020. 'procedure DoIt(v: jsvalue); begin end;',
  30021. 'var',
  30022. ' d: double;',
  30023. ' dt: tdatetime;',
  30024. ' i: integer;',
  30025. ' b: byte;',
  30026. ' shi: shortint;',
  30027. ' w: word;',
  30028. ' smi: smallint;',
  30029. ' lw: longword;',
  30030. ' li: longint;',
  30031. ' ni: nativeint;',
  30032. ' nu: nativeuint;',
  30033. 'begin',
  30034. ' DoIt(d);',
  30035. ' DoIt(dt);',
  30036. ' DoIt(i);',
  30037. ' DoIt(b);',
  30038. ' DoIt(shi);',
  30039. ' DoIt(w);',
  30040. ' DoIt(smi);',
  30041. ' DoIt(lw);',
  30042. ' DoIt(li);',
  30043. ' DoIt(ni);',
  30044. ' DoIt(nu);',
  30045. '']);
  30046. ConvertProgram;
  30047. CheckSource('TestJSValue_OverloadNativeInt',
  30048. LinesToStr([ // statements
  30049. 'this.DoIt = function (n) {',
  30050. '};',
  30051. 'this.DoIt$1 = function (v) {',
  30052. '};',
  30053. 'this.d = 0.0;',
  30054. 'this.dt = 0.0;',
  30055. 'this.i = 0;',
  30056. 'this.b = 0;',
  30057. 'this.shi = 0;',
  30058. 'this.w = 0;',
  30059. 'this.smi = 0;',
  30060. 'this.lw = 0;',
  30061. 'this.li = 0;',
  30062. 'this.ni = 0;',
  30063. 'this.nu = 0;',
  30064. '']),
  30065. LinesToStr([ // $mod.$main
  30066. '$mod.DoIt$1($mod.d);',
  30067. '$mod.DoIt$1($mod.dt);',
  30068. '$mod.DoIt($mod.i);',
  30069. '$mod.DoIt($mod.b);',
  30070. '$mod.DoIt($mod.shi);',
  30071. '$mod.DoIt($mod.w);',
  30072. '$mod.DoIt($mod.smi);',
  30073. '$mod.DoIt($mod.lw);',
  30074. '$mod.DoIt($mod.li);',
  30075. '$mod.DoIt($mod.ni);',
  30076. '$mod.DoIt($mod.nu);',
  30077. '']));
  30078. end;
  30079. procedure TTestModule.TestJSValue_OverloadWord;
  30080. begin
  30081. StartProgram(false);
  30082. Add([
  30083. 'type',
  30084. ' integer = longint;',
  30085. ' int53 = nativeint;',
  30086. ' tdatetime = double;',
  30087. 'procedure DoIt(w: word); begin end;',
  30088. 'procedure DoIt(v: jsvalue); begin end;',
  30089. 'var',
  30090. ' d: double;',
  30091. ' dt: tdatetime;',
  30092. ' i: integer;',
  30093. ' b: byte;',
  30094. ' shi: shortint;',
  30095. ' w: word;',
  30096. ' smi: smallint;',
  30097. ' lw: longword;',
  30098. ' li: longint;',
  30099. ' ni: nativeint;',
  30100. ' nu: nativeuint;',
  30101. 'begin',
  30102. ' DoIt(d);',
  30103. ' DoIt(dt);',
  30104. ' DoIt(i);',
  30105. ' DoIt(b);',
  30106. ' DoIt(shi);',
  30107. ' DoIt(w);',
  30108. ' DoIt(smi);',
  30109. ' DoIt(lw);',
  30110. ' DoIt(li);',
  30111. ' DoIt(ni);',
  30112. ' DoIt(nu);',
  30113. '']);
  30114. ConvertProgram;
  30115. CheckSource('TestJSValue_OverloadWord',
  30116. LinesToStr([ // statements
  30117. 'this.DoIt = function (w) {',
  30118. '};',
  30119. 'this.DoIt$1 = function (v) {',
  30120. '};',
  30121. 'this.d = 0.0;',
  30122. 'this.dt = 0.0;',
  30123. 'this.i = 0;',
  30124. 'this.b = 0;',
  30125. 'this.shi = 0;',
  30126. 'this.w = 0;',
  30127. 'this.smi = 0;',
  30128. 'this.lw = 0;',
  30129. 'this.li = 0;',
  30130. 'this.ni = 0;',
  30131. 'this.nu = 0;',
  30132. '']),
  30133. LinesToStr([ // $mod.$main
  30134. '$mod.DoIt$1($mod.d);',
  30135. '$mod.DoIt$1($mod.dt);',
  30136. '$mod.DoIt$1($mod.i);',
  30137. '$mod.DoIt($mod.b);',
  30138. '$mod.DoIt($mod.shi);',
  30139. '$mod.DoIt($mod.w);',
  30140. '$mod.DoIt$1($mod.smi);',
  30141. '$mod.DoIt$1($mod.lw);',
  30142. '$mod.DoIt$1($mod.li);',
  30143. '$mod.DoIt$1($mod.ni);',
  30144. '$mod.DoIt$1($mod.nu);',
  30145. '']));
  30146. end;
  30147. procedure TTestModule.TestJSValue_OverloadString;
  30148. begin
  30149. StartProgram(false);
  30150. Add([
  30151. 'type',
  30152. ' uni = string;',
  30153. ' WChar = char;',
  30154. 'procedure DoIt(s: string); begin end;',
  30155. 'procedure DoIt(v: jsvalue); begin end;',
  30156. 'var',
  30157. ' s: string;',
  30158. ' c: char;',
  30159. ' u: uni;',
  30160. 'begin',
  30161. ' DoIt(s);',
  30162. ' DoIt(c);',
  30163. ' DoIt(u);',
  30164. '']);
  30165. ConvertProgram;
  30166. CheckSource('TestJSValue_OverloadString',
  30167. LinesToStr([ // statements
  30168. 'this.DoIt = function (s) {',
  30169. '};',
  30170. 'this.DoIt$1 = function (v) {',
  30171. '};',
  30172. 'this.s = "";',
  30173. 'this.c = "";',
  30174. 'this.u = "";',
  30175. '']),
  30176. LinesToStr([ // $mod.$main
  30177. '$mod.DoIt($mod.s);',
  30178. '$mod.DoIt($mod.c);',
  30179. '$mod.DoIt($mod.u);',
  30180. '']));
  30181. end;
  30182. procedure TTestModule.TestJSValue_OverloadChar;
  30183. begin
  30184. StartProgram(false);
  30185. Add([
  30186. 'type',
  30187. ' uni = string;',
  30188. ' WChar = char;',
  30189. 'procedure DoIt(c: char); begin end;',
  30190. 'procedure DoIt(v: jsvalue); begin end;',
  30191. 'var',
  30192. ' s: string;',
  30193. ' c: char;',
  30194. ' u: uni;',
  30195. 'begin',
  30196. ' DoIt(s);',
  30197. ' DoIt(c);',
  30198. ' DoIt(u);',
  30199. '']);
  30200. ConvertProgram;
  30201. CheckSource('TestJSValue_OverloadChar',
  30202. LinesToStr([ // statements
  30203. 'this.DoIt = function (c) {',
  30204. '};',
  30205. 'this.DoIt$1 = function (v) {',
  30206. '};',
  30207. 'this.s = "";',
  30208. 'this.c = "";',
  30209. 'this.u = "";',
  30210. '']),
  30211. LinesToStr([ // $mod.$main
  30212. '$mod.DoIt$1($mod.s);',
  30213. '$mod.DoIt($mod.c);',
  30214. '$mod.DoIt$1($mod.u);',
  30215. '']));
  30216. end;
  30217. procedure TTestModule.TestJSValue_OverloadPointer;
  30218. begin
  30219. StartProgram(false);
  30220. Add([
  30221. 'type',
  30222. ' TObject = class end;',
  30223. 'procedure DoIt(p: pointer); begin end;',
  30224. 'procedure DoIt(v: jsvalue); begin end;',
  30225. 'var',
  30226. ' o: TObject;',
  30227. 'begin',
  30228. ' DoIt(o);',
  30229. '']);
  30230. ConvertProgram;
  30231. CheckSource('TestJSValue_OverloadPointer',
  30232. LinesToStr([ // statements
  30233. 'rtl.createClass(this, "TObject", null, function () {',
  30234. ' this.$init = function () {',
  30235. ' };',
  30236. ' this.$final = function () {',
  30237. ' };',
  30238. '});',
  30239. 'this.DoIt = function (p) {',
  30240. '};',
  30241. 'this.DoIt$1 = function (v) {',
  30242. '};',
  30243. 'this.o = null;',
  30244. '']),
  30245. LinesToStr([ // $mod.$main
  30246. '$mod.DoIt($mod.o);',
  30247. '']));
  30248. end;
  30249. procedure TTestModule.TestJSValue_ForIn;
  30250. begin
  30251. StartProgram(false);
  30252. Add([
  30253. 'var',
  30254. ' v: JSValue;',
  30255. ' key: string;',
  30256. 'begin',
  30257. ' for key in v do begin',
  30258. ' if key=''abc'' then ;',
  30259. ' end;',
  30260. '']);
  30261. ConvertProgram;
  30262. CheckSource('TestJSValue_ForIn',
  30263. LinesToStr([ // statements
  30264. 'this.v = undefined;',
  30265. 'this.key = "";',
  30266. '']),
  30267. LinesToStr([ // $mod.$main
  30268. 'for ($mod.key in $mod.v) {',
  30269. ' if ($mod.key === "abc") ;',
  30270. '};',
  30271. '']));
  30272. end;
  30273. procedure TTestModule.TestRTTI_IntRange;
  30274. begin
  30275. WithTypeInfo:=true;
  30276. StartProgram(true,[supTypeInfo]);
  30277. Add([
  30278. '{$modeswitch externalclass}',
  30279. 'type',
  30280. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  30281. ' TColor = type TGraphicsColor;',
  30282. 'var',
  30283. ' p: TTypeInfo;',
  30284. ' k: TTypeKind;',
  30285. 'begin',
  30286. ' p:=typeinfo(TGraphicsColor);',
  30287. ' p:=typeinfo(TColor);',
  30288. ' k:=GetTypeKind(TGraphicsColor);',
  30289. ' k:=GetTypeKind(TColor);',
  30290. '']);
  30291. ConvertProgram;
  30292. CheckSource('TestRTTI_IntRange',
  30293. LinesToStr([ // statements
  30294. 'this.$rtti.$Int("TGraphicsColor", {',
  30295. ' minvalue: -2147483648,',
  30296. ' maxvalue: 2147483647,',
  30297. ' ordtype: 4',
  30298. '});',
  30299. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  30300. 'this.p = null;',
  30301. 'this.k = 0;',
  30302. '']),
  30303. LinesToStr([ // $mod.$main
  30304. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  30305. '$mod.p = $mod.$rtti["TColor"];',
  30306. '$mod.k = 1;',
  30307. '$mod.k = 1;',
  30308. '']));
  30309. end;
  30310. procedure TTestModule.TestRTTI_Double;
  30311. begin
  30312. WithTypeInfo:=true;
  30313. StartProgram(true,[supTypeInfo]);
  30314. Add([
  30315. '{$modeswitch externalclass}',
  30316. 'type',
  30317. ' TFloat = type double;',
  30318. 'var',
  30319. ' p: TTypeInfo;',
  30320. 'begin',
  30321. ' p:=typeinfo(double);',
  30322. ' p:=typeinfo(TFloat);',
  30323. '']);
  30324. ConvertProgram;
  30325. CheckSource('TestRTTI_Double',
  30326. LinesToStr([ // statements
  30327. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30328. 'this.p = null;',
  30329. '']),
  30330. LinesToStr([ // $mod.$main
  30331. '$mod.p = rtl.double;',
  30332. '$mod.p = $mod.$rtti["TFloat"];',
  30333. '']));
  30334. end;
  30335. procedure TTestModule.TestRTTI_ProcType;
  30336. begin
  30337. WithTypeInfo:=true;
  30338. StartProgram(false);
  30339. Add('type');
  30340. Add(' TProcA = procedure;');
  30341. Add(' TMethodB = procedure of object;');
  30342. Add(' TProcC = procedure; varargs;');
  30343. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  30344. Add(' TProcE = function: nativeint;');
  30345. Add(' TProcF = function(const p: TProcA): nativeuint;');
  30346. Add('var p: pointer;');
  30347. Add('begin');
  30348. Add(' p:=typeinfo(tproca);');
  30349. ConvertProgram;
  30350. CheckSource('TestRTTI_ProcType',
  30351. LinesToStr([ // statements
  30352. 'this.$rtti.$ProcVar("TProcA", {',
  30353. ' procsig: rtl.newTIProcSig([])',
  30354. '});',
  30355. 'this.$rtti.$MethodVar("TMethodB", {',
  30356. ' procsig: rtl.newTIProcSig([]),',
  30357. ' methodkind: 0',
  30358. '});',
  30359. 'this.$rtti.$ProcVar("TProcC", {',
  30360. ' procsig: rtl.newTIProcSig([], null, 2)',
  30361. '});',
  30362. 'this.$rtti.$ProcVar("TProcD", {',
  30363. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30364. '});',
  30365. 'this.$rtti.$ProcVar("TProcE", {',
  30366. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30367. '});',
  30368. 'this.$rtti.$ProcVar("TProcF", {',
  30369. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  30370. '});',
  30371. 'this.p = null;',
  30372. '']),
  30373. LinesToStr([ // $mod.$main
  30374. '$mod.p = $mod.$rtti["TProcA"];',
  30375. '']));
  30376. end;
  30377. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  30378. begin
  30379. WithTypeInfo:=true;
  30380. AddModuleWithIntfImplSrc('unit2.pas',
  30381. LinesToStr([
  30382. 'type',
  30383. ' TObject = class end;'
  30384. ]),
  30385. '');
  30386. StartUnit(true);
  30387. Add('interface');
  30388. Add('uses unit2;');
  30389. Add('type');
  30390. Add(' TProcA = function(o: tobject): tobject;');
  30391. Add('implementation');
  30392. Add('type');
  30393. Add(' TProcB = function(o: tobject): tobject;');
  30394. Add('var p: Pointer;');
  30395. Add('initialization');
  30396. Add(' p:=typeinfo(tproca);');
  30397. Add(' p:=typeinfo(tprocb);');
  30398. ConvertUnit;
  30399. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30400. LinesToStr([ // statements
  30401. 'var $impl = $mod.$impl;',
  30402. 'this.$rtti.$ProcVar("TProcA", {',
  30403. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30404. '});',
  30405. '']),
  30406. LinesToStr([ // this.$init
  30407. '$impl.p = $mod.$rtti["TProcA"];',
  30408. '$impl.p = $mod.$rtti["TProcB"];',
  30409. '']),
  30410. LinesToStr([ // implementation
  30411. '$mod.$rtti.$ProcVar("TProcB", {',
  30412. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30413. '});',
  30414. '$impl.p = null;',
  30415. '']) );
  30416. end;
  30417. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30418. begin
  30419. WithTypeInfo:=true;
  30420. StartProgram(false);
  30421. Add(['var',
  30422. ' ProcA: procedure;',
  30423. ' MethodB: procedure of object;',
  30424. ' ProcC: procedure; varargs;',
  30425. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  30426. ' ProcE: function: nativeint;',
  30427. ' p: pointer;',
  30428. 'begin',
  30429. ' p:=typeinfo(proca);']);
  30430. ConvertProgram;
  30431. CheckSource('TestRTTI_ProcTypeAnonymous',
  30432. LinesToStr([ // statements
  30433. 'this.$rtti.$ProcVar("ProcA$a", {',
  30434. ' procsig: rtl.newTIProcSig([])',
  30435. '});',
  30436. 'this.ProcA = null;',
  30437. 'this.$rtti.$MethodVar("MethodB$a", {',
  30438. ' procsig: rtl.newTIProcSig([]),',
  30439. ' methodkind: 0',
  30440. '});',
  30441. 'this.MethodB = null;',
  30442. 'this.$rtti.$ProcVar("ProcC$a", {',
  30443. ' procsig: rtl.newTIProcSig([], null, 2)',
  30444. '});',
  30445. 'this.ProcC = null;',
  30446. 'this.$rtti.$ProcVar("ProcD$a", {',
  30447. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30448. '});',
  30449. 'this.ProcD = null;',
  30450. 'this.$rtti.$ProcVar("ProcE$a", {',
  30451. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30452. '});',
  30453. 'this.ProcE = null;',
  30454. 'this.p = null;',
  30455. '']),
  30456. LinesToStr([ // $mod.$main
  30457. '$mod.p = $mod.$rtti["ProcA$a"];',
  30458. '']));
  30459. end;
  30460. procedure TTestModule.TestRTTI_EnumAndSetType;
  30461. begin
  30462. WithTypeInfo:=true;
  30463. StartProgram(false);
  30464. Add('type');
  30465. Add(' TFlag = (light,dark);');
  30466. Add(' TFlags = set of TFlag;');
  30467. Add(' TProc = function(f: TFlags): TFlag;');
  30468. Add('var p: pointer;');
  30469. Add('begin');
  30470. Add(' p:=typeinfo(tflag);');
  30471. Add(' p:=typeinfo(tflags);');
  30472. ConvertProgram;
  30473. CheckSource('TestRTTI_EnumAndType',
  30474. LinesToStr([ // statements
  30475. 'this.TFlag = {',
  30476. ' "0": "light",',
  30477. ' light: 0,',
  30478. ' "1": "dark",',
  30479. ' dark: 1',
  30480. '};',
  30481. 'this.$rtti.$Enum("TFlag", {',
  30482. ' minvalue: 0,',
  30483. ' maxvalue: 1,',
  30484. ' ordtype: 1,',
  30485. ' enumtype: this.TFlag',
  30486. '});',
  30487. 'this.$rtti.$Set("TFlags", {',
  30488. ' comptype: this.$rtti["TFlag"]',
  30489. '});',
  30490. 'this.$rtti.$ProcVar("TProc", {',
  30491. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30492. '});',
  30493. 'this.p = null;',
  30494. '']),
  30495. LinesToStr([ // $mod.$main
  30496. '$mod.p = $mod.$rtti["TFlag"];',
  30497. '$mod.p = $mod.$rtti["TFlags"];',
  30498. '']));
  30499. end;
  30500. procedure TTestModule.TestRTTI_EnumRange;
  30501. begin
  30502. WithTypeInfo:=true;
  30503. StartProgram(false);
  30504. Add([
  30505. 'type',
  30506. ' TCol = (red,green,blue);',
  30507. ' TColRg = green..blue;',
  30508. ' TSetOfColRg = set of TColRg;',
  30509. 'var p: pointer;',
  30510. 'begin',
  30511. ' p:=typeinfo(tcolrg);',
  30512. ' p:=typeinfo(tsetofcolrg);',
  30513. '']);
  30514. ConvertProgram;
  30515. end;
  30516. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30517. begin
  30518. WithTypeInfo:=true;
  30519. StartProgram(false);
  30520. Add('type');
  30521. Add(' TFlags = set of (red, green);');
  30522. Add('var');
  30523. Add(' f: TFlags;');
  30524. Add('begin');
  30525. Add(' Include(f,red);');
  30526. ConvertProgram;
  30527. CheckSource('TestRTTI_AnonymousEnumType',
  30528. LinesToStr([ // statements
  30529. 'this.TFlags$a = {',
  30530. ' "0": "red",',
  30531. ' red: 0,',
  30532. ' "1": "green",',
  30533. ' green: 1',
  30534. '};',
  30535. 'this.$rtti.$Enum("TFlags$a", {',
  30536. ' minvalue: 0,',
  30537. ' maxvalue: 1,',
  30538. ' ordtype: 1,',
  30539. ' enumtype: this.TFlags$a',
  30540. '});',
  30541. 'this.$rtti.$Set("TFlags", {',
  30542. ' comptype: this.$rtti["TFlags$a"]',
  30543. '});',
  30544. 'this.f = {};',
  30545. '']),
  30546. LinesToStr([
  30547. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30548. '']));
  30549. end;
  30550. procedure TTestModule.TestRTTI_StaticArray;
  30551. begin
  30552. WithTypeInfo:=true;
  30553. StartProgram(false);
  30554. Add('type');
  30555. Add(' TFlag = (light,dark);');
  30556. Add(' TFlagNames = array[TFlag] of string;');
  30557. Add(' TBoolNames = array[boolean] of string;');
  30558. Add(' TByteArray = array[1..32768] of byte;');
  30559. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30560. Add('var p: pointer;');
  30561. Add('begin');
  30562. Add(' p:=typeinfo(TFlagNames);');
  30563. Add(' p:=typeinfo(TBoolNames);');
  30564. ConvertProgram;
  30565. CheckSource('TestRTTI_StaticArray',
  30566. LinesToStr([ // statements
  30567. 'this.TFlag = {',
  30568. ' "0": "light",',
  30569. ' light: 0,',
  30570. ' "1": "dark",',
  30571. ' dark: 1',
  30572. '};',
  30573. 'this.$rtti.$Enum("TFlag", {',
  30574. ' minvalue: 0,',
  30575. ' maxvalue: 1,',
  30576. ' ordtype: 1,',
  30577. ' enumtype: this.TFlag',
  30578. '});',
  30579. 'this.$rtti.$StaticArray("TFlagNames", {',
  30580. ' dims: [2],',
  30581. ' eltype: rtl.string',
  30582. '});',
  30583. 'this.$rtti.$StaticArray("TBoolNames", {',
  30584. ' dims: [2],',
  30585. ' eltype: rtl.string',
  30586. '});',
  30587. 'this.$rtti.$StaticArray("TByteArray", {',
  30588. ' dims: [32768],',
  30589. ' eltype: rtl.byte',
  30590. '});',
  30591. 'this.$rtti.$ProcVar("TProc", {',
  30592. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30593. '});',
  30594. 'this.p = null;',
  30595. '']),
  30596. LinesToStr([ // $mod.$main
  30597. '$mod.p = $mod.$rtti["TFlagNames"];',
  30598. '$mod.p = $mod.$rtti["TBoolNames"];',
  30599. '']));
  30600. end;
  30601. procedure TTestModule.TestRTTI_DynArray;
  30602. begin
  30603. WithTypeInfo:=true;
  30604. StartProgram(false);
  30605. Add('type');
  30606. Add(' TArrStr = array of string;');
  30607. Add(' TArr2Dim = array of tarrstr;');
  30608. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30609. Add('var p: pointer;');
  30610. Add('begin');
  30611. Add(' p:=typeinfo(tarrstr);');
  30612. Add(' p:=typeinfo(tarr2dim);');
  30613. ConvertProgram;
  30614. CheckSource('TestRTTI_DynArray',
  30615. LinesToStr([ // statements
  30616. 'this.$rtti.$DynArray("TArrStr", {',
  30617. ' eltype: rtl.string',
  30618. '});',
  30619. 'this.$rtti.$DynArray("TArr2Dim", {',
  30620. ' eltype: this.$rtti["TArrStr"]',
  30621. '});',
  30622. 'this.$rtti.$ProcVar("TProc", {',
  30623. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30624. '});',
  30625. 'this.p = null;',
  30626. '']),
  30627. LinesToStr([ // $mod.$main
  30628. '$mod.p = $mod.$rtti["TArrStr"];',
  30629. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30630. '']));
  30631. end;
  30632. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30633. begin
  30634. WithTypeInfo:=true;
  30635. StartProgram(false);
  30636. Add('type');
  30637. Add(' TArr = array of array of longint;');
  30638. Add('var a: TArr;');
  30639. Add('begin');
  30640. ConvertProgram;
  30641. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30642. LinesToStr([ // statements
  30643. 'this.$rtti.$DynArray("TArr$a", {',
  30644. ' eltype: rtl.longint',
  30645. '});',
  30646. 'this.$rtti.$DynArray("TArr", {',
  30647. ' eltype: this.$rtti["TArr$a"]',
  30648. '});',
  30649. 'this.a = [];',
  30650. '']),
  30651. LinesToStr([ // $mod.$main
  30652. ]));
  30653. end;
  30654. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30655. begin
  30656. WithTypeInfo:=true;
  30657. StartProgram(false);
  30658. Add('type');
  30659. Add(' TObject = class');
  30660. Add(' published');
  30661. Add(' procedure Proc; virtual; abstract;');
  30662. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30663. Add(' end;');
  30664. Add('begin');
  30665. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30666. nDuplicatePublishedMethodXAtY);
  30667. ConvertProgram;
  30668. end;
  30669. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30670. begin
  30671. WithTypeInfo:=true;
  30672. StartUnit(false);
  30673. Add([
  30674. 'interface',
  30675. 'type',
  30676. ' TObject = class',
  30677. ' end;',
  30678. ' {$M+}',
  30679. ' TBird = class',
  30680. ' procedure Fly;',
  30681. ' end;',
  30682. ' {$M-}',
  30683. 'type',
  30684. ' TEagle = class(TBird)',
  30685. ' procedure Fly;',
  30686. ' end;',
  30687. 'implementation',
  30688. 'procedure TBird.Fly;',
  30689. 'begin',
  30690. 'end;',
  30691. 'procedure TEagle.Fly;',
  30692. 'begin',
  30693. 'end;',
  30694. '']);
  30695. ConvertUnit;
  30696. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30697. LinesToStr([ // statements
  30698. 'rtl.createClass(this, "TObject", null, function () {',
  30699. ' this.$init = function () {',
  30700. ' };',
  30701. ' this.$final = function () {',
  30702. ' };',
  30703. '});',
  30704. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30705. ' this.Fly = function () {',
  30706. ' };',
  30707. ' var $r = this.$rtti;',
  30708. ' $r.addMethod("Fly", 0, []);',
  30709. '});',
  30710. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30711. ' this.Fly = function () {',
  30712. ' };',
  30713. ' var $r = this.$rtti;',
  30714. ' $r.addMethod("Fly", 0, []);',
  30715. '});',
  30716. '']),
  30717. LinesToStr([ // $mod.$main
  30718. ]));
  30719. CheckResolverUnexpectedHints(true);
  30720. end;
  30721. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30722. begin
  30723. WithTypeInfo:=true;
  30724. StartProgram(false);
  30725. Add('type');
  30726. Add(' TObject = class');
  30727. Add(' published');
  30728. Add(' procedure Proc; external name ''foo'';');
  30729. Add(' end;');
  30730. Add('begin');
  30731. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30732. nPublishedNameMustMatchExternal);
  30733. ConvertProgram;
  30734. end;
  30735. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30736. begin
  30737. WithTypeInfo:=true;
  30738. StartProgram(false);
  30739. Add('type');
  30740. Add(' TObject = class');
  30741. Add(' class var FA: longint;');
  30742. Add(' published');
  30743. Add(' class property A: longint read FA;');
  30744. Add(' end;');
  30745. Add('begin');
  30746. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30747. nInvalidXModifierY);
  30748. ConvertProgram;
  30749. end;
  30750. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30751. begin
  30752. WithTypeInfo:=true;
  30753. StartProgram(false);
  30754. Add('type');
  30755. Add(' TObject = class');
  30756. Add(' published');
  30757. Add(' class var FA: longint;');
  30758. Add(' end;');
  30759. Add('begin');
  30760. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30761. nSymbolCannotBePublished);
  30762. ConvertProgram;
  30763. end;
  30764. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30765. begin
  30766. WithTypeInfo:=true;
  30767. StartProgram(false);
  30768. Add('{$modeswitch externalclass}');
  30769. Add('type');
  30770. Add(' TObject = class');
  30771. Add(' published');
  30772. Add(' V: longint; external name ''foo'';');
  30773. Add(' end;');
  30774. Add('begin');
  30775. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30776. nPublishedNameMustMatchExternal);
  30777. ConvertProgram;
  30778. end;
  30779. procedure TTestModule.TestRTTI_Class_Field;
  30780. begin
  30781. WithTypeInfo:=true;
  30782. StartProgram(false);
  30783. Add('{$modeswitch externalclass}');
  30784. Add('type');
  30785. Add(' TObject = class');
  30786. Add(' private');
  30787. Add(' FPropA: string;');
  30788. Add(' published');
  30789. Add(' VarLI: longint;');
  30790. Add(' VarC: char;');
  30791. Add(' VarS: string;');
  30792. Add(' VarD: double;');
  30793. Add(' VarB: boolean;');
  30794. Add(' VarLW: longword;');
  30795. Add(' VarSmI: smallint;');
  30796. Add(' VarW: word;');
  30797. Add(' VarShI: shortint;');
  30798. Add(' VarBy: byte;');
  30799. Add(' VarExt: longint external name ''VarExt'';');
  30800. Add(' ArrA, ArrB: array of byte;');
  30801. Add(' end;');
  30802. Add('var p: pointer;');
  30803. Add(' Obj: tobject;');
  30804. Add('begin');
  30805. Add(' p:=typeinfo(tobject);');
  30806. Add(' p:=typeinfo(p);');
  30807. Add(' p:=typeinfo(obj);');
  30808. ConvertProgram;
  30809. CheckSource('TestRTTI_Class_Field',
  30810. LinesToStr([ // statements
  30811. 'rtl.createClass(this, "TObject", null, function () {',
  30812. ' this.$init = function () {',
  30813. ' this.FPropA = "";',
  30814. ' this.VarLI = 0;',
  30815. ' this.VarC = "";',
  30816. ' this.VarS = "";',
  30817. ' this.VarD = 0.0;',
  30818. ' this.VarB = false;',
  30819. ' this.VarLW = 0;',
  30820. ' this.VarSmI = 0;',
  30821. ' this.VarW = 0;',
  30822. ' this.VarShI = 0;',
  30823. ' this.VarBy = 0;',
  30824. ' this.ArrA = [];',
  30825. ' this.ArrB = [];',
  30826. ' };',
  30827. ' this.$final = function () {',
  30828. ' this.ArrA = undefined;',
  30829. ' this.ArrB = undefined;',
  30830. ' };',
  30831. ' var $r = this.$rtti;',
  30832. ' $r.addField("VarLI", rtl.longint);',
  30833. ' $r.addField("VarC", rtl.char);',
  30834. ' $r.addField("VarS", rtl.string);',
  30835. ' $r.addField("VarD", rtl.double);',
  30836. ' $r.addField("VarB", rtl.boolean);',
  30837. ' $r.addField("VarLW", rtl.longword);',
  30838. ' $r.addField("VarSmI", rtl.smallint);',
  30839. ' $r.addField("VarW", rtl.word);',
  30840. ' $r.addField("VarShI", rtl.shortint);',
  30841. ' $r.addField("VarBy", rtl.byte);',
  30842. ' $r.addField("VarExt", rtl.longint);',
  30843. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30844. ' eltype: rtl.byte',
  30845. ' });',
  30846. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30847. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30848. '});',
  30849. 'this.p = null;',
  30850. 'this.Obj = null;',
  30851. '']),
  30852. LinesToStr([ // $mod.$main
  30853. '$mod.p = $mod.$rtti["TObject"];',
  30854. '$mod.p = rtl.pointer;',
  30855. '$mod.p = $mod.Obj.$rtti;',
  30856. '']));
  30857. end;
  30858. procedure TTestModule.TestRTTI_Class_Method;
  30859. begin
  30860. WithTypeInfo:=true;
  30861. StartProgram(false);
  30862. Add([
  30863. 'type',
  30864. ' TObject = class',
  30865. ' private',
  30866. ' procedure Internal; external name ''$intern'';',
  30867. ' published',
  30868. ' procedure Click; virtual; abstract;',
  30869. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30870. ' function GetNotify: boolean; external name ''GetNotify'';',
  30871. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30872. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30873. ' end;',
  30874. 'begin']);
  30875. ConvertProgram;
  30876. CheckSource('TestRTTI_Class_Method',
  30877. LinesToStr([ // statements
  30878. 'rtl.createClass(this, "TObject", null, function () {',
  30879. ' this.$init = function () {',
  30880. ' };',
  30881. ' this.$final = function () {',
  30882. ' };',
  30883. ' var $r = this.$rtti;',
  30884. ' $r.addMethod("Click", 0, []);',
  30885. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30886. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30887. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30888. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30889. '});',
  30890. '']),
  30891. LinesToStr([ // $mod.$main
  30892. '']));
  30893. end;
  30894. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30895. begin
  30896. WithTypeInfo:=true;
  30897. StartProgram(false);
  30898. Add('type');
  30899. Add(' TObject = class');
  30900. Add(' published');
  30901. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30902. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30903. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30904. Add(' end;');
  30905. Add('begin');
  30906. ConvertProgram;
  30907. CheckSource('TestRTTI_Class_MethodOpenArray',
  30908. LinesToStr([ // statements
  30909. 'rtl.createClass(this, "TObject", null, function () {',
  30910. ' this.$init = function () {',
  30911. ' };',
  30912. ' this.$final = function () {',
  30913. ' };',
  30914. ' var $r = this.$rtti;',
  30915. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30916. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30917. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30918. '});',
  30919. '']),
  30920. LinesToStr([ // $mod.$main
  30921. '']));
  30922. end;
  30923. procedure TTestModule.TestRTTI_Class_Property;
  30924. begin
  30925. WithTypeInfo:=true;
  30926. StartProgram(false);
  30927. Add('{$modeswitch externalclass}');
  30928. Add('type');
  30929. Add(' TObject = class');
  30930. Add(' private');
  30931. Add(' FColor: longint;');
  30932. Add(' FColorStored: boolean;');
  30933. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30934. Add(' function GetColor: longint; virtual; abstract;');
  30935. Add(' function GetColorStored: boolean; virtual; abstract;');
  30936. Add(' FExtSize: longint external name ''$extSize'';');
  30937. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30938. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30939. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30940. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30941. Add(' published');
  30942. Add(' property ColorA: longint read FColor;');
  30943. Add(' property ColorB: longint write FColor;');
  30944. Add(' property ColorC: longint read GetColor write SetColor;');
  30945. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  30946. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  30947. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  30948. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  30949. Add(' end;');
  30950. Add('begin');
  30951. ConvertProgram;
  30952. CheckSource('TestRTTI_Class_Property',
  30953. LinesToStr([ // statements
  30954. 'rtl.createClass(this, "TObject", null, function () {',
  30955. ' this.$init = function () {',
  30956. ' this.FColor = 0;',
  30957. ' this.FColorStored = false;',
  30958. ' };',
  30959. ' this.$final = function () {',
  30960. ' };',
  30961. ' var $r = this.$rtti;',
  30962. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  30963. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  30964. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  30965. ' $r.addProperty(',
  30966. ' "ColorD",',
  30967. ' 8,',
  30968. ' rtl.longint,',
  30969. ' "FColor",',
  30970. ' "FColor",',
  30971. ' {',
  30972. ' stored: "FColorStored"',
  30973. ' }',
  30974. ' );',
  30975. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  30976. ' $r.addProperty(',
  30977. ' "ExtSizeB",',
  30978. ' 11,',
  30979. ' rtl.longint,',
  30980. ' "$getSize",',
  30981. ' "$setSize",',
  30982. ' {',
  30983. ' stored: "$extSizeStored"',
  30984. ' }',
  30985. ' );',
  30986. ' $r.addProperty(',
  30987. ' "ExtSizeC",',
  30988. ' 12,',
  30989. ' rtl.longint,',
  30990. ' "$extSize",',
  30991. ' "$extSize",',
  30992. ' {',
  30993. ' stored: "$getExtSizeStored"',
  30994. ' }',
  30995. ' );',
  30996. '});',
  30997. '']),
  30998. LinesToStr([ // $mod.$main
  30999. '']));
  31000. end;
  31001. procedure TTestModule.TestRTTI_Class_PropertyParams;
  31002. begin
  31003. WithTypeInfo:=true;
  31004. StartProgram(false);
  31005. Add('{$modeswitch externalclass}');
  31006. Add('type');
  31007. Add(' integer = longint;');
  31008. Add(' TObject = class');
  31009. Add(' private');
  31010. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  31011. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  31012. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  31013. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  31014. Add(' published');
  31015. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  31016. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  31017. Add(' end;');
  31018. Add('begin');
  31019. ConvertProgram;
  31020. CheckSource('TestRTTI_Class_PropertyParams',
  31021. LinesToStr([ // statements
  31022. 'rtl.createClass(this, "TObject", null, function () {',
  31023. ' this.$init = function () {',
  31024. ' };',
  31025. ' this.$final = function () {',
  31026. ' };',
  31027. ' var $r = this.$rtti;',
  31028. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  31029. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  31030. '});',
  31031. '']),
  31032. LinesToStr([ // $mod.$main
  31033. '']));
  31034. end;
  31035. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  31036. begin
  31037. WithTypeInfo:=true;
  31038. AddModuleWithIntfImplSrc('unit1.pas',
  31039. 'type TColor = -5..5;',
  31040. '');
  31041. StartProgram(true);
  31042. Add([
  31043. 'uses unit1;',
  31044. 'type',
  31045. ' TColorAlias = TColor;',
  31046. ' TColorTypeAlias = type TColor;',
  31047. ' TObject = class',
  31048. ' private',
  31049. ' fColor: TColor;',
  31050. ' fAlias: TColorAlias;',
  31051. ' fTypeAlias: TColorTypeAlias;',
  31052. ' published',
  31053. ' property Color: TColor read fcolor;',
  31054. ' property Alias: TColorAlias read falias;',
  31055. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  31056. ' end;',
  31057. 'begin',
  31058. '']);
  31059. ConvertProgram;
  31060. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  31061. LinesToStr([ // statements
  31062. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  31063. 'rtl.createClass(this, "TObject", null, function () {',
  31064. ' this.$init = function () {',
  31065. ' this.fColor = 0;',
  31066. ' this.fAlias = 0;',
  31067. ' this.fTypeAlias = 0;',
  31068. ' };',
  31069. ' this.$final = function () {',
  31070. ' };',
  31071. ' var $r = this.$rtti;',
  31072. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  31073. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  31074. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  31075. '});',
  31076. '']),
  31077. LinesToStr([ // $mod.$main
  31078. '']));
  31079. end;
  31080. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  31081. begin
  31082. WithTypeInfo:=true;
  31083. StartProgram(false);
  31084. Add([
  31085. '{$modeswitch omitrtti}',
  31086. 'type',
  31087. ' TObject = class',
  31088. ' private',
  31089. ' FA: byte;',
  31090. ' published',
  31091. ' property A: byte read FA write FA;',
  31092. ' end;',
  31093. 'begin']);
  31094. ConvertProgram;
  31095. CheckSource('TestRTTI_Class_OmitRTTI',
  31096. LinesToStr([ // statements
  31097. 'rtl.createClass(this, "TObject", null, function () {',
  31098. ' this.$init = function () {',
  31099. ' this.FA = 0;',
  31100. ' };',
  31101. ' this.$final = function () {',
  31102. ' };',
  31103. '});',
  31104. '']),
  31105. LinesToStr([ // $mod.$main
  31106. '']));
  31107. end;
  31108. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  31109. begin
  31110. WithTypeInfo:=true;
  31111. StartUnit(true,[supTObject]);
  31112. Add([
  31113. 'interface',
  31114. 'type',
  31115. ' {$M+}',
  31116. ' TBird = class',
  31117. ' published',
  31118. ' Swarm: array of TBird;',
  31119. ' end;',
  31120. 'implementation',
  31121. '']);
  31122. ConvertUnit;
  31123. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  31124. LinesToStr([ // statements
  31125. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  31126. ' this.$init = function () {',
  31127. ' pas.system.TObject.$init.call(this);',
  31128. ' this.Swarm = [];',
  31129. ' };',
  31130. ' this.$final = function () {',
  31131. ' this.Swarm = undefined;',
  31132. ' pas.system.TObject.$final.call(this);',
  31133. ' };',
  31134. ' var $r = this.$rtti;',
  31135. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  31136. ' eltype: $r',
  31137. ' });',
  31138. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  31139. '});',
  31140. '']),
  31141. LinesToStr([ // $mod.$main
  31142. '']));
  31143. end;
  31144. procedure TTestModule.TestRTTI_IndexModifier;
  31145. begin
  31146. WithTypeInfo:=true;
  31147. StartProgram(false);
  31148. Add([
  31149. 'type',
  31150. ' TEnum = (red, blue);',
  31151. ' TObject = class',
  31152. ' FB: boolean;',
  31153. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  31154. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  31155. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  31156. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  31157. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  31158. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  31159. ' published',
  31160. ' property B1: boolean index 1 read FB write SetIntBool;',
  31161. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  31162. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  31163. ' end;',
  31164. 'begin']);
  31165. ConvertProgram;
  31166. CheckSource('TestRTTI_IndexModifier',
  31167. LinesToStr([ // statements
  31168. 'this.TEnum = {',
  31169. ' "0": "red",',
  31170. ' red: 0,',
  31171. ' "1": "blue",',
  31172. ' blue: 1',
  31173. '};',
  31174. 'this.$rtti.$Enum("TEnum", {',
  31175. ' minvalue: 0,',
  31176. ' maxvalue: 1,',
  31177. ' ordtype: 1,',
  31178. ' enumtype: this.TEnum',
  31179. '});',
  31180. 'rtl.createClass(this, "TObject", null, function () {',
  31181. ' this.$init = function () {',
  31182. ' this.FB = false;',
  31183. ' };',
  31184. ' this.$final = function () {',
  31185. ' };',
  31186. ' var $r = this.$rtti;',
  31187. ' $r.addProperty(',
  31188. ' "B1",',
  31189. ' 18,',
  31190. ' rtl.boolean,',
  31191. ' "FB",',
  31192. ' "SetIntBool",',
  31193. ' {',
  31194. ' index: 1',
  31195. ' }',
  31196. ' );',
  31197. ' $r.addProperty(',
  31198. ' "B2",',
  31199. ' 17,',
  31200. ' rtl.boolean,',
  31201. ' "GetEnumBool",',
  31202. ' "FB",',
  31203. ' {',
  31204. ' index: $mod.TEnum.blue',
  31205. ' }',
  31206. ' );',
  31207. ' $r.addProperty(',
  31208. ' "I1",',
  31209. ' 19,',
  31210. ' rtl.boolean,',
  31211. ' "GetStrIntBool",',
  31212. ' "SetStrIntBool",',
  31213. ' {',
  31214. ' index: 2',
  31215. ' }',
  31216. ' );',
  31217. '});',
  31218. '']),
  31219. LinesToStr([ // $mod.$main
  31220. '']));
  31221. end;
  31222. procedure TTestModule.TestRTTI_StoredModifier;
  31223. begin
  31224. WithTypeInfo:=true;
  31225. StartProgram(false);
  31226. Add([
  31227. 'const',
  31228. ' ConstB = true;',
  31229. 'type',
  31230. ' TObject = class',
  31231. ' private',
  31232. ' FB: boolean;',
  31233. ' function IsBStored: boolean; virtual; abstract;',
  31234. ' published',
  31235. ' property BoolA: boolean read FB stored true;',
  31236. ' property BoolB: boolean read FB stored false;',
  31237. ' property BoolC: boolean read FB stored FB;',
  31238. ' property BoolD: boolean read FB stored ConstB;',
  31239. ' property BoolE: boolean read FB stored IsBStored;',
  31240. ' end;',
  31241. 'begin']);
  31242. ConvertProgram;
  31243. CheckSource('TestRTTI_StoredModifier',
  31244. LinesToStr([ // statements
  31245. 'this.ConstB = true;',
  31246. 'rtl.createClass(this, "TObject", null, function () {',
  31247. ' this.$init = function () {',
  31248. ' this.FB = false;',
  31249. ' };',
  31250. ' this.$final = function () {',
  31251. ' };',
  31252. ' var $r = this.$rtti;',
  31253. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  31254. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  31255. ' $r.addProperty(',
  31256. ' "BoolC",',
  31257. ' 8,',
  31258. ' rtl.boolean,',
  31259. ' "FB",',
  31260. ' "",',
  31261. ' {',
  31262. ' stored: "FB"',
  31263. ' }',
  31264. ' );',
  31265. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  31266. ' $r.addProperty(',
  31267. ' "BoolE",',
  31268. ' 12,',
  31269. ' rtl.boolean,',
  31270. ' "FB",',
  31271. ' "",',
  31272. ' {',
  31273. ' stored: "IsBStored"',
  31274. ' }',
  31275. ' );',
  31276. '});',
  31277. '']),
  31278. LinesToStr([ // $mod.$main
  31279. '']));
  31280. end;
  31281. procedure TTestModule.TestRTTI_DefaultValue;
  31282. begin
  31283. WithTypeInfo:=true;
  31284. StartProgram(false);
  31285. Add([
  31286. 'type',
  31287. ' TEnum = (red, blue);',
  31288. 'const',
  31289. ' CB = true or false;',
  31290. ' CI = 1+2;',
  31291. 'type',
  31292. ' TObject = class',
  31293. ' FB: boolean;',
  31294. ' FI: longint;',
  31295. ' FE: TEnum;',
  31296. ' published',
  31297. ' property B1: boolean read FB default true;',
  31298. ' property B2: boolean read FB default CB;',
  31299. ' property B3: boolean read FB default test1.cb;',
  31300. ' property I1: longint read FI default 2;',
  31301. ' property I2: longint read FI default CI;',
  31302. ' property E1: TEnum read FE default red;',
  31303. ' property E2: TEnum read FE default TEnum.blue;',
  31304. ' end;',
  31305. 'begin']);
  31306. ConvertProgram;
  31307. CheckSource('TestRTTI_DefaultValue',
  31308. LinesToStr([ // statements
  31309. 'this.TEnum = {',
  31310. ' "0": "red",',
  31311. ' red: 0,',
  31312. ' "1": "blue",',
  31313. ' blue: 1',
  31314. '};',
  31315. 'this.$rtti.$Enum("TEnum", {',
  31316. ' minvalue: 0,',
  31317. ' maxvalue: 1,',
  31318. ' ordtype: 1,',
  31319. ' enumtype: this.TEnum',
  31320. '});',
  31321. 'this.CB = true || false;',
  31322. 'this.CI = 1 + 2;',
  31323. 'rtl.createClass(this, "TObject", null, function () {',
  31324. ' this.$init = function () {',
  31325. ' this.FB = false;',
  31326. ' this.FI = 0;',
  31327. ' this.FE = 0;',
  31328. ' };',
  31329. ' this.$final = function () {',
  31330. ' };',
  31331. ' var $r = this.$rtti;',
  31332. ' $r.addProperty(',
  31333. ' "B1",',
  31334. ' 0,',
  31335. ' rtl.boolean,',
  31336. ' "FB",',
  31337. ' "",',
  31338. ' {',
  31339. ' Default: true',
  31340. ' }',
  31341. ' );',
  31342. ' $r.addProperty(',
  31343. ' "B2",',
  31344. ' 0,',
  31345. ' rtl.boolean,',
  31346. ' "FB",',
  31347. ' "",',
  31348. ' {',
  31349. ' Default: true',
  31350. ' }',
  31351. ' );',
  31352. ' $r.addProperty(',
  31353. ' "B3",',
  31354. ' 0,',
  31355. ' rtl.boolean,',
  31356. ' "FB",',
  31357. ' "",',
  31358. ' {',
  31359. ' Default: true',
  31360. ' }',
  31361. ' );',
  31362. ' $r.addProperty(',
  31363. ' "I1",',
  31364. ' 0,',
  31365. ' rtl.longint,',
  31366. ' "FI",',
  31367. ' "",',
  31368. ' {',
  31369. ' Default: 2',
  31370. ' }',
  31371. ' );',
  31372. ' $r.addProperty(',
  31373. ' "I2",',
  31374. ' 0,',
  31375. ' rtl.longint,',
  31376. ' "FI",',
  31377. ' "",',
  31378. ' {',
  31379. ' Default: 3',
  31380. ' }',
  31381. ' );',
  31382. ' $r.addProperty(',
  31383. ' "E1",',
  31384. ' 0,',
  31385. ' $mod.$rtti["TEnum"],',
  31386. ' "FE",',
  31387. ' "",',
  31388. ' {',
  31389. ' Default: $mod.TEnum.red',
  31390. ' }',
  31391. ' );',
  31392. ' $r.addProperty(',
  31393. ' "E2",',
  31394. ' 0,',
  31395. ' $mod.$rtti["TEnum"],',
  31396. ' "FE",',
  31397. ' "",',
  31398. ' {',
  31399. ' Default: $mod.TEnum.blue',
  31400. ' }',
  31401. ' );',
  31402. '});',
  31403. '']),
  31404. LinesToStr([ // $mod.$main
  31405. '']));
  31406. end;
  31407. procedure TTestModule.TestRTTI_DefaultValueSet;
  31408. begin
  31409. WithTypeInfo:=true;
  31410. StartProgram(false);
  31411. Add([
  31412. 'type',
  31413. ' TEnum = (red, blue);',
  31414. ' TSet = set of TEnum;',
  31415. 'const',
  31416. ' CSet = [red,blue];',
  31417. 'type',
  31418. ' TObject = class',
  31419. ' FSet: TSet;',
  31420. ' published',
  31421. ' property Set1: TSet read FSet default [];',
  31422. ' property Set2: TSet read FSet default [red];',
  31423. ' property Set3: TSet read FSet default [red,blue];',
  31424. ' property Set4: TSet read FSet default CSet;',
  31425. ' end;',
  31426. 'begin']);
  31427. ConvertProgram;
  31428. CheckSource('TestRTTI_DefaultValueSet',
  31429. LinesToStr([ // statements
  31430. 'this.TEnum = {',
  31431. ' "0": "red",',
  31432. ' red: 0,',
  31433. ' "1": "blue",',
  31434. ' blue: 1',
  31435. '};',
  31436. 'this.$rtti.$Enum("TEnum", {',
  31437. ' minvalue: 0,',
  31438. ' maxvalue: 1,',
  31439. ' ordtype: 1,',
  31440. ' enumtype: this.TEnum',
  31441. '});',
  31442. 'this.$rtti.$Set("TSet", {',
  31443. ' comptype: this.$rtti["TEnum"]',
  31444. '});',
  31445. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31446. 'rtl.createClass(this, "TObject", null, function () {',
  31447. ' this.$init = function () {',
  31448. ' this.FSet = {};',
  31449. ' };',
  31450. ' this.$final = function () {',
  31451. ' this.FSet = undefined;',
  31452. ' };',
  31453. ' var $r = this.$rtti;',
  31454. ' $r.addProperty(',
  31455. ' "Set1",',
  31456. ' 0,',
  31457. ' $mod.$rtti["TSet"],',
  31458. ' "FSet",',
  31459. ' "",',
  31460. ' {',
  31461. ' Default: {}',
  31462. ' }',
  31463. ' );',
  31464. ' $r.addProperty(',
  31465. ' "Set2",',
  31466. ' 0,',
  31467. ' $mod.$rtti["TSet"],',
  31468. ' "FSet",',
  31469. ' "",',
  31470. ' {',
  31471. ' Default: rtl.createSet($mod.TEnum.red)',
  31472. ' }',
  31473. ' );',
  31474. ' $r.addProperty(',
  31475. ' "Set3",',
  31476. ' 0,',
  31477. ' $mod.$rtti["TSet"],',
  31478. ' "FSet",',
  31479. ' "",',
  31480. ' {',
  31481. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31482. ' }',
  31483. ' );',
  31484. ' $r.addProperty(',
  31485. ' "Set4",',
  31486. ' 0,',
  31487. ' $mod.$rtti["TSet"],',
  31488. ' "FSet",',
  31489. ' "",',
  31490. ' {',
  31491. ' Default: $mod.CSet',
  31492. ' }',
  31493. ' );',
  31494. '});',
  31495. '']),
  31496. LinesToStr([ // $mod.$main
  31497. '']));
  31498. end;
  31499. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31500. begin
  31501. WithTypeInfo:=true;
  31502. StartProgram(false);
  31503. Add([
  31504. 'type',
  31505. ' TRg = -1..1;',
  31506. 'const',
  31507. ' l = low(TRg);',
  31508. ' h = high(TRg);',
  31509. 'type',
  31510. ' TObject = class',
  31511. ' FV: TRg;',
  31512. ' published',
  31513. ' property V1: TRg read FV default -1;',
  31514. ' end;',
  31515. 'begin']);
  31516. ConvertProgram;
  31517. CheckSource('TestRTTI_DefaultValueRangeType',
  31518. LinesToStr([ // statements
  31519. 'this.$rtti.$Int("TRg", {',
  31520. ' minvalue: -1,',
  31521. ' maxvalue: 1,',
  31522. ' ordtype: 0',
  31523. '});',
  31524. 'this.l = -1;',
  31525. 'this.h = 1;',
  31526. 'rtl.createClass(this, "TObject", null, function () {',
  31527. ' this.$init = function () {',
  31528. ' this.FV = 0;',
  31529. ' };',
  31530. ' this.$final = function () {',
  31531. ' };',
  31532. ' var $r = this.$rtti;',
  31533. ' $r.addProperty(',
  31534. ' "V1",',
  31535. ' 0,',
  31536. ' $mod.$rtti["TRg"],',
  31537. ' "FV",',
  31538. ' "",',
  31539. ' {',
  31540. ' Default: -1',
  31541. ' }',
  31542. ' );',
  31543. '});',
  31544. '']),
  31545. LinesToStr([ // $mod.$main
  31546. '']));
  31547. end;
  31548. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31549. begin
  31550. WithTypeInfo:=true;
  31551. StartProgram(false);
  31552. Add([
  31553. 'type',
  31554. ' TObject = class',
  31555. ' FA, FB: byte;',
  31556. ' property A: byte read FA default 1;',
  31557. ' property B: byte read FB default 2;',
  31558. ' end;',
  31559. ' TBird = class',
  31560. ' published',
  31561. ' property A;',
  31562. ' property B nodefault;',
  31563. ' end;',
  31564. 'begin']);
  31565. ConvertProgram;
  31566. CheckSource('TestRTTI_DefaultValueInherit',
  31567. LinesToStr([ // statements
  31568. 'rtl.createClass(this, "TObject", null, function () {',
  31569. ' this.$init = function () {',
  31570. ' this.FA = 0;',
  31571. ' this.FB = 0;',
  31572. ' };',
  31573. ' this.$final = function () {',
  31574. ' };',
  31575. '});',
  31576. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31577. ' var $r = this.$rtti;',
  31578. ' $r.addProperty(',
  31579. ' "A",',
  31580. ' 0,',
  31581. ' rtl.byte,',
  31582. ' "FA",',
  31583. ' "",',
  31584. ' {',
  31585. ' Default: 1',
  31586. ' }',
  31587. ' );',
  31588. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31589. '});',
  31590. '']),
  31591. LinesToStr([ // $mod.$main
  31592. '']));
  31593. end;
  31594. procedure TTestModule.TestRTTI_OverrideMethod;
  31595. begin
  31596. WithTypeInfo:=true;
  31597. StartProgram(false);
  31598. Add('type');
  31599. Add(' TObject = class');
  31600. Add(' published');
  31601. Add(' procedure DoIt; virtual; abstract;');
  31602. Add(' end;');
  31603. Add(' TSky = class');
  31604. Add(' published');
  31605. Add(' procedure DoIt; override;');
  31606. Add(' end;');
  31607. Add('procedure TSky.DoIt; begin end;');
  31608. Add('begin');
  31609. ConvertProgram;
  31610. CheckSource('TestRTTI_OverrideMethod',
  31611. LinesToStr([ // statements
  31612. 'rtl.createClass(this, "TObject", null, function () {',
  31613. ' this.$init = function () {',
  31614. ' };',
  31615. ' this.$final = function () {',
  31616. ' };',
  31617. ' var $r = this.$rtti;',
  31618. ' $r.addMethod("DoIt", 0, []);',
  31619. '});',
  31620. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31621. ' this.DoIt = function () {',
  31622. ' };',
  31623. '});',
  31624. '']),
  31625. LinesToStr([ // $mod.$main
  31626. '']));
  31627. end;
  31628. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31629. begin
  31630. WithTypeInfo:=true;
  31631. StartProgram(false);
  31632. Add([
  31633. 'type',
  31634. ' TObject = class',
  31635. ' published',
  31636. ' procedure DoIt;',
  31637. ' end;',
  31638. ' TSky = class',
  31639. ' published',
  31640. ' procedure DoIt; reintroduce;',
  31641. ' end;',
  31642. 'procedure TObject.DoIt; begin end;',
  31643. 'procedure TSky.DoIt;',
  31644. 'begin',
  31645. ' inherited DoIt;',
  31646. 'end;',
  31647. 'begin']);
  31648. ConvertProgram;
  31649. CheckSource('TestRTTI_ReintroduceMethod',
  31650. LinesToStr([ // statements
  31651. 'rtl.createClass(this, "TObject", null, function () {',
  31652. ' this.$init = function () {',
  31653. ' };',
  31654. ' this.$final = function () {',
  31655. ' };',
  31656. ' this.DoIt = function () {',
  31657. ' };',
  31658. ' var $r = this.$rtti;',
  31659. ' $r.addMethod("DoIt", 0, []);',
  31660. '});',
  31661. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31662. ' this.DoIt = function () {',
  31663. ' $mod.TObject.DoIt.call(this);',
  31664. ' };',
  31665. ' var $r = this.$rtti;',
  31666. ' $r.addMethod("DoIt", 0, []);',
  31667. '});',
  31668. '']),
  31669. LinesToStr([ // $mod.$main
  31670. '']));
  31671. end;
  31672. procedure TTestModule.TestRTTI_OverloadProperty;
  31673. begin
  31674. WithTypeInfo:=true;
  31675. StartProgram(false);
  31676. Add('type');
  31677. Add(' TObject = class');
  31678. Add(' protected');
  31679. Add(' FFlag: longint;');
  31680. Add(' published');
  31681. Add(' property Flag: longint read fflag;');
  31682. Add(' end;');
  31683. Add(' TSky = class');
  31684. Add(' published');
  31685. Add(' property FLAG: longint write fflag;');
  31686. Add(' end;');
  31687. Add('begin');
  31688. ConvertProgram;
  31689. CheckSource('TestRTTI_OverrideMethod',
  31690. LinesToStr([ // statements
  31691. 'rtl.createClass(this, "TObject", null, function () {',
  31692. ' this.$init = function () {',
  31693. ' this.FFlag = 0;',
  31694. ' };',
  31695. ' this.$final = function () {',
  31696. ' };',
  31697. ' var $r = this.$rtti;',
  31698. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  31699. '});',
  31700. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31701. ' var $r = this.$rtti;',
  31702. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  31703. '});',
  31704. '']),
  31705. LinesToStr([ // $mod.$main
  31706. '']));
  31707. end;
  31708. procedure TTestModule.TestRTTI_ClassForward;
  31709. begin
  31710. WithTypeInfo:=true;
  31711. StartProgram(false);
  31712. Add('type');
  31713. Add(' TObject = class end;');
  31714. Add(' tbridge = class;');
  31715. Add(' TProc = function: tbridge;');
  31716. Add(' TOger = class');
  31717. Add(' published');
  31718. Add(' FBridge: tbridge;');
  31719. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  31720. Add(' property Bridge: tbridge read fbridge write setbridge;');
  31721. Add(' end;');
  31722. Add(' TBridge = class');
  31723. Add(' FOger: toger;');
  31724. Add(' end;');
  31725. Add('var p: Pointer;');
  31726. Add(' b: tbridge;');
  31727. Add('begin');
  31728. Add(' p:=typeinfo(tbridge);');
  31729. Add(' p:=typeinfo(b);');
  31730. ConvertProgram;
  31731. CheckSource('TestRTTI_ClassForward',
  31732. LinesToStr([ // statements
  31733. 'rtl.createClass(this, "TObject", null, function () {',
  31734. ' this.$init = function () {',
  31735. ' };',
  31736. ' this.$final = function () {',
  31737. ' };',
  31738. '});',
  31739. 'this.$rtti.$Class("TBridge");',
  31740. 'this.$rtti.$ProcVar("TProc", {',
  31741. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  31742. '});',
  31743. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  31744. ' this.$init = function () {',
  31745. ' $mod.TObject.$init.call(this);',
  31746. ' this.FBridge = null;',
  31747. ' };',
  31748. ' this.$final = function () {',
  31749. ' this.FBridge = undefined;',
  31750. ' $mod.TObject.$final.call(this);',
  31751. ' };',
  31752. ' var $r = this.$rtti;',
  31753. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  31754. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  31755. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  31756. '});',
  31757. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  31758. ' this.$init = function () {',
  31759. ' $mod.TObject.$init.call(this);',
  31760. ' this.FOger = null;',
  31761. ' };',
  31762. ' this.$final = function () {',
  31763. ' this.FOger = undefined;',
  31764. ' $mod.TObject.$final.call(this);',
  31765. ' };',
  31766. '});',
  31767. 'this.p = null;',
  31768. 'this.b = null;',
  31769. '']),
  31770. LinesToStr([ // $mod.$main
  31771. '$mod.p = $mod.$rtti["TBridge"];',
  31772. '$mod.p = $mod.b.$rtti;',
  31773. '']));
  31774. end;
  31775. procedure TTestModule.TestRTTI_ClassOf;
  31776. begin
  31777. WithTypeInfo:=true;
  31778. StartProgram(false);
  31779. Add('type');
  31780. Add(' TClass = class of tobject;');
  31781. Add(' TProcA = function: TClass;');
  31782. Add(' TObject = class');
  31783. Add(' published');
  31784. Add(' C: tclass;');
  31785. Add(' end;');
  31786. Add(' tfox = class;');
  31787. Add(' TBird = class end;');
  31788. Add(' TBirds = class of tbird;');
  31789. Add(' TFox = class end;');
  31790. Add(' TFoxes = class of tfox;');
  31791. Add(' TCows = class of TCow;');
  31792. Add(' TCow = class;');
  31793. Add(' TCow = class end;');
  31794. Add('begin');
  31795. ConvertProgram;
  31796. CheckSource('TestRTTI_ClassOf',
  31797. LinesToStr([ // statements
  31798. 'this.$rtti.$Class("TObject");',
  31799. 'this.$rtti.$ClassRef("TClass", {',
  31800. ' instancetype: this.$rtti["TObject"]',
  31801. '});',
  31802. 'this.$rtti.$ProcVar("TProcA", {',
  31803. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  31804. '});',
  31805. 'rtl.createClass(this, "TObject", null, function () {',
  31806. ' this.$init = function () {',
  31807. ' this.C = null;',
  31808. ' };',
  31809. ' this.$final = function () {',
  31810. ' this.C = undefined;',
  31811. ' };',
  31812. ' var $r = this.$rtti;',
  31813. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31814. '});',
  31815. 'this.$rtti.$Class("TFox");',
  31816. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31817. '});',
  31818. 'this.$rtti.$ClassRef("TBirds", {',
  31819. ' instancetype: this.$rtti["TBird"]',
  31820. '});',
  31821. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31822. '});',
  31823. 'this.$rtti.$ClassRef("TFoxes", {',
  31824. ' instancetype: this.$rtti["TFox"]',
  31825. '});',
  31826. 'this.$rtti.$Class("TCow");',
  31827. 'this.$rtti.$ClassRef("TCows", {',
  31828. ' instancetype: this.$rtti["TCow"]',
  31829. '});',
  31830. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31831. '});',
  31832. '']),
  31833. LinesToStr([ // $mod.$main
  31834. '']));
  31835. end;
  31836. procedure TTestModule.TestRTTI_Record;
  31837. begin
  31838. WithTypeInfo:=true;
  31839. StartProgram(false);
  31840. Add('type');
  31841. Add(' integer = longint;');
  31842. Add(' TPoint = record');
  31843. Add(' x,y: integer;');
  31844. Add(' end;');
  31845. Add('var p: pointer;');
  31846. Add(' r: tpoint;');
  31847. Add('begin');
  31848. Add(' p:=typeinfo(tpoint);');
  31849. Add(' p:=typeinfo(r);');
  31850. Add(' p:=typeinfo(r.x);');
  31851. ConvertProgram;
  31852. CheckSource('TestRTTI_Record',
  31853. LinesToStr([ // statements
  31854. 'rtl.recNewT(this, "TPoint", function () {',
  31855. ' this.x = 0;',
  31856. ' this.y = 0;',
  31857. ' this.$eq = function (b) {',
  31858. ' return (this.x === b.x) && (this.y === b.y);',
  31859. ' };',
  31860. ' this.$assign = function (s) {',
  31861. ' this.x = s.x;',
  31862. ' this.y = s.y;',
  31863. ' return this;',
  31864. ' };',
  31865. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31866. ' $r.addField("x", rtl.longint);',
  31867. ' $r.addField("y", rtl.longint);',
  31868. '});',
  31869. 'this.p = null;',
  31870. 'this.r = this.TPoint.$new();',
  31871. '']),
  31872. LinesToStr([ // $mod.$main
  31873. '$mod.p = $mod.$rtti["TPoint"];',
  31874. '$mod.p = $mod.$rtti["TPoint"];',
  31875. '$mod.p = rtl.longint;',
  31876. '']));
  31877. end;
  31878. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31879. begin
  31880. WithTypeInfo:=true;
  31881. StartProgram(false);
  31882. Add('type');
  31883. Add(' TFloatRec = record');
  31884. Add(' c,d: array of char;');
  31885. // Add(' i: array of array of longint;');
  31886. Add(' end;');
  31887. Add('var p: pointer;');
  31888. Add(' r: tfloatrec;');
  31889. Add('begin');
  31890. Add(' p:=typeinfo(tfloatrec);');
  31891. Add(' p:=typeinfo(r);');
  31892. Add(' p:=typeinfo(r.d);');
  31893. ConvertProgram;
  31894. CheckSource('TestRTTI_Record',
  31895. LinesToStr([ // statements
  31896. 'rtl.recNewT(this, "TFloatRec", function () {',
  31897. ' this.$new = function () {',
  31898. ' var r = Object.create(this);',
  31899. ' r.c = [];',
  31900. ' r.d = [];',
  31901. ' return r;',
  31902. ' };',
  31903. ' this.$eq = function (b) {',
  31904. ' return (this.c === b.c) && (this.d === b.d);',
  31905. ' };',
  31906. ' this.$assign = function (s) {',
  31907. ' this.c = rtl.arrayRef(s.c);',
  31908. ' this.d = rtl.arrayRef(s.d);',
  31909. ' return this;',
  31910. ' };',
  31911. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31912. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31913. ' eltype: rtl.char',
  31914. ' });',
  31915. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31916. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31917. '});',
  31918. 'this.p = null;',
  31919. 'this.r = this.TFloatRec.$new();',
  31920. '']),
  31921. LinesToStr([ // $mod.$main
  31922. '$mod.p = $mod.$rtti["TFloatRec"];',
  31923. '$mod.p = $mod.$rtti["TFloatRec"];',
  31924. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31925. '']));
  31926. end;
  31927. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31928. begin
  31929. WithTypeInfo:=true;
  31930. StartProgram(false);
  31931. Add([
  31932. '{$modeswitch AdvancedRecords}',
  31933. 'type',
  31934. ' TPoint = record',
  31935. ' type TProc = procedure(w: word);',
  31936. ' class var p: TProc;',
  31937. ' end;',
  31938. 'begin',
  31939. '']);
  31940. ConvertProgram;
  31941. CheckSource('TestRTTI_Record_ClassVarType',
  31942. LinesToStr([ // statements
  31943. 'rtl.recNewT(this, "TPoint", function () {',
  31944. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  31945. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  31946. ' });',
  31947. ' this.p = null;',
  31948. ' this.$eq = function (b) {',
  31949. ' return true;',
  31950. ' };',
  31951. ' this.$assign = function (s) {',
  31952. ' return this;',
  31953. ' };',
  31954. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31955. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  31956. '}, true);',
  31957. '']),
  31958. LinesToStr([ // $mod.$main
  31959. '']));
  31960. end;
  31961. procedure TTestModule.TestRTTI_LocalTypes;
  31962. begin
  31963. WithTypeInfo:=true;
  31964. StartProgram(false);
  31965. Add([
  31966. 'procedure DoIt;',
  31967. 'type',
  31968. ' integer = longint;',
  31969. ' TPoint = record',
  31970. ' x,y: integer;',
  31971. ' end;',
  31972. 'var p: TPoint;',
  31973. 'begin',
  31974. 'end;',
  31975. 'begin']);
  31976. ConvertProgram;
  31977. CheckSource('TestRTTI_LocalTypes',
  31978. LinesToStr([ // statements
  31979. 'var TPoint = rtl.recNewT(null, "", function () {',
  31980. ' this.x = 0;',
  31981. ' this.y = 0;',
  31982. ' this.$eq = function (b) {',
  31983. ' return (this.x === b.x) && (this.y === b.y);',
  31984. ' };',
  31985. ' this.$assign = function (s) {',
  31986. ' this.x = s.x;',
  31987. ' this.y = s.y;',
  31988. ' return this;',
  31989. ' };',
  31990. '});',
  31991. 'this.DoIt = function () {',
  31992. ' var p = TPoint.$new();',
  31993. '};',
  31994. '']),
  31995. LinesToStr([ // $mod.$main
  31996. '']));
  31997. end;
  31998. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  31999. begin
  32000. WithTypeInfo:=true;
  32001. StartProgram(false);
  32002. Add([
  32003. 'type',
  32004. ' TCaption = string;',
  32005. ' TYesNo = boolean;',
  32006. ' TLetter = char;',
  32007. ' TFloat = double;',
  32008. ' TPtr = pointer;',
  32009. ' TShortInt = shortint;',
  32010. ' TByte = byte;',
  32011. ' TSmallInt = smallint;',
  32012. ' TWord = word;',
  32013. ' TInt32 = longint;',
  32014. ' TDWord = longword;',
  32015. ' TValue = jsvalue;',
  32016. 'var p: TPtr;',
  32017. 'begin',
  32018. ' p:=typeinfo(string);',
  32019. ' p:=typeinfo(tcaption);',
  32020. ' p:=typeinfo(boolean);',
  32021. ' p:=typeinfo(tyesno);',
  32022. ' p:=typeinfo(char);',
  32023. ' p:=typeinfo(tletter);',
  32024. ' p:=typeinfo(double);',
  32025. ' p:=typeinfo(tfloat);',
  32026. ' p:=typeinfo(pointer);',
  32027. ' p:=typeinfo(tptr);',
  32028. ' p:=typeinfo(shortint);',
  32029. ' p:=typeinfo(tshortint);',
  32030. ' p:=typeinfo(byte);',
  32031. ' p:=typeinfo(tbyte);',
  32032. ' p:=typeinfo(smallint);',
  32033. ' p:=typeinfo(tsmallint);',
  32034. ' p:=typeinfo(word);',
  32035. ' p:=typeinfo(tword);',
  32036. ' p:=typeinfo(longword);',
  32037. ' p:=typeinfo(tdword);',
  32038. ' p:=typeinfo(jsvalue);',
  32039. ' p:=typeinfo(tvalue);',
  32040. '']);
  32041. ConvertProgram;
  32042. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  32043. LinesToStr([ // statements
  32044. 'this.p = null;',
  32045. '']),
  32046. LinesToStr([ // $mod.$main
  32047. '$mod.p = rtl.string;',
  32048. '$mod.p = rtl.string;',
  32049. '$mod.p = rtl.boolean;',
  32050. '$mod.p = rtl.boolean;',
  32051. '$mod.p = rtl.char;',
  32052. '$mod.p = rtl.char;',
  32053. '$mod.p = rtl.double;',
  32054. '$mod.p = rtl.double;',
  32055. '$mod.p = rtl.pointer;',
  32056. '$mod.p = rtl.pointer;',
  32057. '$mod.p = rtl.shortint;',
  32058. '$mod.p = rtl.shortint;',
  32059. '$mod.p = rtl.byte;',
  32060. '$mod.p = rtl.byte;',
  32061. '$mod.p = rtl.smallint;',
  32062. '$mod.p = rtl.smallint;',
  32063. '$mod.p = rtl.word;',
  32064. '$mod.p = rtl.word;',
  32065. '$mod.p = rtl.longword;',
  32066. '$mod.p = rtl.longword;',
  32067. '$mod.p = rtl.jsvalue;',
  32068. '$mod.p = rtl.jsvalue;',
  32069. '']));
  32070. end;
  32071. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  32072. begin
  32073. WithTypeInfo:=true;
  32074. StartProgram(false);
  32075. Add([
  32076. 'type',
  32077. ' TCaption = type string;',
  32078. ' TYesNo = type boolean;',
  32079. ' TLetter = type char;',
  32080. ' TFloat = type double;',
  32081. ' TPtr = type pointer;',
  32082. ' TShortInt = type shortint;',
  32083. ' TByte = type byte;',
  32084. ' TSmallInt = type smallint;',
  32085. ' TWord = type word;',
  32086. ' TInt32 = type longint;',
  32087. ' TDWord = type longword;',
  32088. ' TValue = type jsvalue;',
  32089. ' TAliasValue = type TValue;',
  32090. 'var',
  32091. ' p: TPtr;',
  32092. ' a: TAliasValue;',
  32093. 'begin',
  32094. ' p:=typeinfo(tcaption);',
  32095. ' p:=typeinfo(tyesno);',
  32096. ' p:=typeinfo(tletter);',
  32097. ' p:=typeinfo(tfloat);',
  32098. ' p:=typeinfo(tptr);',
  32099. ' p:=typeinfo(tshortint);',
  32100. ' p:=typeinfo(tbyte);',
  32101. ' p:=typeinfo(tsmallint);',
  32102. ' p:=typeinfo(tword);',
  32103. ' p:=typeinfo(tdword);',
  32104. ' p:=typeinfo(tvalue);',
  32105. ' p:=typeinfo(taliasvalue);',
  32106. ' p:=typeinfo(a);',
  32107. '']);
  32108. ConvertProgram;
  32109. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  32110. LinesToStr([ // statements
  32111. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  32112. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  32113. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  32114. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  32115. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  32116. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  32117. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  32118. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  32119. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  32120. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  32121. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  32122. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  32123. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  32124. 'this.p = null;',
  32125. 'this.a = undefined;',
  32126. '']),
  32127. LinesToStr([ // $mod.$main
  32128. '$mod.p = $mod.$rtti["TCaption"];',
  32129. '$mod.p = $mod.$rtti["TYesNo"];',
  32130. '$mod.p = $mod.$rtti["TLetter"];',
  32131. '$mod.p = $mod.$rtti["TFloat"];',
  32132. '$mod.p = $mod.$rtti["TPtr"];',
  32133. '$mod.p = $mod.$rtti["TShortInt"];',
  32134. '$mod.p = $mod.$rtti["TByte"];',
  32135. '$mod.p = $mod.$rtti["TSmallInt"];',
  32136. '$mod.p = $mod.$rtti["TWord"];',
  32137. '$mod.p = $mod.$rtti["TDWord"];',
  32138. '$mod.p = $mod.$rtti["TValue"];',
  32139. '$mod.p = $mod.$rtti["TAliasValue"];',
  32140. '$mod.p = $mod.$rtti["TAliasValue"];',
  32141. '']));
  32142. end;
  32143. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  32144. begin
  32145. WithTypeInfo:=true;
  32146. StartProgram(false);
  32147. Add('procedure DoIt;');
  32148. Add('type');
  32149. Add(' integer = longint;');
  32150. Add(' TPoint = record');
  32151. Add(' x,y: integer;');
  32152. Add(' end;');
  32153. Add('var p: pointer;');
  32154. Add('begin');
  32155. Add(' p:=typeinfo(tpoint);');
  32156. Add('end;');
  32157. Add('begin');
  32158. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  32159. ConvertProgram;
  32160. end;
  32161. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  32162. begin
  32163. WithTypeInfo:=true;
  32164. StartProgram(true,[supTypeInfo]);
  32165. Add([
  32166. '{$modeswitch externalclass}',
  32167. 'type',
  32168. ' TFlag = (up,down);',
  32169. ' TFlags = set of TFlag;',
  32170. 'var',
  32171. ' ti: TTypeInfo;',
  32172. ' tiInt: TTypeInfoInteger;',
  32173. ' tiEnum: TTypeInfoEnum;',
  32174. ' tiSet: TTypeInfoSet;',
  32175. 'begin',
  32176. ' ti:=typeinfo(string);',
  32177. ' ti:=typeinfo(boolean);',
  32178. ' ti:=typeinfo(char);',
  32179. ' ti:=typeinfo(double);',
  32180. ' tiInt:=typeinfo(shortint);',
  32181. ' tiInt:=typeinfo(byte);',
  32182. ' tiInt:=typeinfo(smallint);',
  32183. ' tiInt:=typeinfo(word);',
  32184. ' tiInt:=typeinfo(longint);',
  32185. ' tiInt:=typeinfo(longword);',
  32186. ' ti:=typeinfo(jsvalue);',
  32187. ' tiEnum:=typeinfo(tflag);',
  32188. ' tiSet:=typeinfo(tflags);']);
  32189. ConvertProgram;
  32190. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  32191. LinesToStr([ // statements
  32192. 'this.TFlag = {',
  32193. ' "0": "up",',
  32194. ' up: 0,',
  32195. ' "1": "down",',
  32196. ' down: 1',
  32197. '};',
  32198. 'this.$rtti.$Enum("TFlag", {',
  32199. ' minvalue: 0,',
  32200. ' maxvalue: 1,',
  32201. ' ordtype: 1,',
  32202. ' enumtype: this.TFlag',
  32203. '});',
  32204. 'this.$rtti.$Set("TFlags", {',
  32205. ' comptype: this.$rtti["TFlag"]',
  32206. '});',
  32207. 'this.ti = null;',
  32208. 'this.tiInt = null;',
  32209. 'this.tiEnum = null;',
  32210. 'this.tiSet = null;',
  32211. '']),
  32212. LinesToStr([ // $mod.$main
  32213. '$mod.ti = rtl.string;',
  32214. '$mod.ti = rtl.boolean;',
  32215. '$mod.ti = rtl.char;',
  32216. '$mod.ti = rtl.double;',
  32217. '$mod.tiInt = rtl.shortint;',
  32218. '$mod.tiInt = rtl.byte;',
  32219. '$mod.tiInt = rtl.smallint;',
  32220. '$mod.tiInt = rtl.word;',
  32221. '$mod.tiInt = rtl.longint;',
  32222. '$mod.tiInt = rtl.longword;',
  32223. '$mod.ti = rtl.jsvalue;',
  32224. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  32225. '$mod.tiSet = $mod.$rtti["TFlags"];',
  32226. '']));
  32227. end;
  32228. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  32229. begin
  32230. WithTypeInfo:=true;
  32231. StartProgram(true,[supTypeInfo]);
  32232. Add('{$modeswitch externalclass}');
  32233. Add('type');
  32234. Add(' TStaticArr = array[boolean] of string;');
  32235. Add(' TDynArr = array of string;');
  32236. Add(' TProc = procedure;');
  32237. Add(' TMethod = procedure of object;');
  32238. Add('var');
  32239. Add(' StaticArray: TStaticArr;');
  32240. Add(' tiStaticArray: TTypeInfoStaticArray;');
  32241. Add(' DynArray: TDynArr;');
  32242. Add(' tiDynArray: TTypeInfoDynArray;');
  32243. Add(' ProcVar: TProc;');
  32244. Add(' tiProcVar: TTypeInfoProcVar;');
  32245. Add(' MethodVar: TMethod;');
  32246. Add(' tiMethodVar: TTypeInfoMethodVar;');
  32247. Add('begin');
  32248. Add(' tiStaticArray:=typeinfo(StaticArray);');
  32249. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  32250. Add(' tiDynArray:=typeinfo(DynArray);');
  32251. Add(' tiDynArray:=typeinfo(TDynArr);');
  32252. Add(' tiProcVar:=typeinfo(ProcVar);');
  32253. Add(' tiProcVar:=typeinfo(TProc);');
  32254. Add(' tiMethodVar:=typeinfo(MethodVar);');
  32255. Add(' tiMethodVar:=typeinfo(TMethod);');
  32256. ConvertProgram;
  32257. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  32258. LinesToStr([ // statements
  32259. 'this.$rtti.$StaticArray("TStaticArr", {',
  32260. ' dims: [2],',
  32261. ' eltype: rtl.string',
  32262. '});',
  32263. 'this.$rtti.$DynArray("TDynArr", {',
  32264. ' eltype: rtl.string',
  32265. '});',
  32266. 'this.$rtti.$ProcVar("TProc", {',
  32267. ' procsig: rtl.newTIProcSig([])',
  32268. '});',
  32269. 'this.$rtti.$MethodVar("TMethod", {',
  32270. ' procsig: rtl.newTIProcSig([]),',
  32271. ' methodkind: 0',
  32272. '});',
  32273. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  32274. 'this.tiStaticArray = null;',
  32275. 'this.DynArray = [];',
  32276. 'this.tiDynArray = null;',
  32277. 'this.ProcVar = null;',
  32278. 'this.tiProcVar = null;',
  32279. 'this.MethodVar = null;',
  32280. 'this.tiMethodVar = null;',
  32281. '']),
  32282. LinesToStr([ // $mod.$main
  32283. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32284. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32285. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32286. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32287. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32288. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32289. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32290. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32291. '']));
  32292. end;
  32293. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  32294. begin
  32295. WithTypeInfo:=true;
  32296. StartProgram(true,[supTypeInfo]);
  32297. Add('{$modeswitch externalclass}');
  32298. Add('type');
  32299. Add(' TRec = record end;');
  32300. // ToDo: ^TRec
  32301. Add(' TObject = class end;');
  32302. Add(' TClass = class of tobject;');
  32303. Add('var');
  32304. Add(' Rec: trec;');
  32305. Add(' tiRecord: ttypeinforecord;');
  32306. Add(' Obj: tobject;');
  32307. Add(' tiClass: ttypeinfoclass;');
  32308. Add(' aClass: tclass;');
  32309. Add(' tiClassRef: ttypeinfoclassref;');
  32310. // ToDo: ^TRec
  32311. Add(' tiPointer: ttypeinfopointer;');
  32312. Add('begin');
  32313. Add(' tirecord:=typeinfo(trec);');
  32314. Add(' tirecord:=typeinfo(trec);');
  32315. Add(' ticlass:=typeinfo(obj);');
  32316. Add(' ticlass:=typeinfo(tobject);');
  32317. Add(' ticlass:=typeinfo(aclass);');
  32318. Add(' ticlassref:=typeinfo(tclass);');
  32319. ConvertProgram;
  32320. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  32321. LinesToStr([ // statements
  32322. 'rtl.recNewT(this, "TRec", function () {',
  32323. ' this.$eq = function (b) {',
  32324. ' return true;',
  32325. ' };',
  32326. ' this.$assign = function (s) {',
  32327. ' return this;',
  32328. ' };',
  32329. ' $mod.$rtti.$Record("TRec", {});',
  32330. '});',
  32331. 'rtl.createClass(this, "TObject", null, function () {',
  32332. ' this.$init = function () {',
  32333. ' };',
  32334. ' this.$final = function () {',
  32335. ' };',
  32336. '});',
  32337. 'this.$rtti.$ClassRef("TClass", {',
  32338. ' instancetype: this.$rtti["TObject"]',
  32339. '});',
  32340. 'this.Rec = this.TRec.$new();',
  32341. 'this.tiRecord = null;',
  32342. 'this.Obj = null;',
  32343. 'this.tiClass = null;',
  32344. 'this.aClass = null;',
  32345. 'this.tiClassRef = null;',
  32346. 'this.tiPointer = null;',
  32347. '']),
  32348. LinesToStr([ // $mod.$main
  32349. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32350. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32351. '$mod.tiClass = $mod.Obj.$rtti;',
  32352. '$mod.tiClass = $mod.$rtti["TObject"];',
  32353. '$mod.tiClass = $mod.aClass.$rtti;',
  32354. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  32355. '']));
  32356. end;
  32357. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  32358. begin
  32359. WithTypeInfo:=true;
  32360. StartProgram(true,[supTypeInfo]);
  32361. Add([
  32362. '{$modeswitch externalclass}',
  32363. 'type',
  32364. ' TClass = class of tobject;',
  32365. ' TObject = class',
  32366. ' function MyClass: TClass;',
  32367. ' class function ClassType: TClass;',
  32368. ' end;',
  32369. 'function TObject.MyClass: TClass;',
  32370. 'var t: TTypeInfoClass;',
  32371. 'begin',
  32372. ' t:=TypeInfo(Self);',
  32373. ' t:=TypeInfo(Result);',
  32374. ' t:=TypeInfo(TObject);',
  32375. 'end;',
  32376. 'class function TObject.ClassType: TClass;',
  32377. 'var t: TTypeInfoClass;',
  32378. 'begin',
  32379. ' t:=TypeInfo(Self);',
  32380. ' t:=TypeInfo(Result);',
  32381. 'end;',
  32382. 'var',
  32383. ' Obj: TObject;',
  32384. ' t: TTypeInfoClass;',
  32385. 'begin',
  32386. ' t:=TypeInfo(TObject.ClassType);',
  32387. ' t:=TypeInfo(Obj.ClassType);',
  32388. ' t:=TypeInfo(Obj.MyClass);',
  32389. '']);
  32390. ConvertProgram;
  32391. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32392. LinesToStr([ // statements
  32393. 'this.$rtti.$Class("TObject");',
  32394. 'this.$rtti.$ClassRef("TClass", {',
  32395. ' instancetype: this.$rtti["TObject"]',
  32396. '});',
  32397. 'rtl.createClass(this, "TObject", null, function () {',
  32398. ' this.$init = function () {',
  32399. ' };',
  32400. ' this.$final = function () {',
  32401. ' };',
  32402. ' this.MyClass = function () {',
  32403. ' var Result = null;',
  32404. ' var t = null;',
  32405. ' t = this.$rtti;',
  32406. ' t = Result.$rtti;',
  32407. ' t = $mod.$rtti["TObject"];',
  32408. ' return Result;',
  32409. ' };',
  32410. ' this.ClassType = function () {',
  32411. ' var Result = null;',
  32412. ' var t = null;',
  32413. ' t = this.$rtti;',
  32414. ' t = Result.$rtti;',
  32415. ' return Result;',
  32416. ' };',
  32417. '});',
  32418. 'this.Obj = null;',
  32419. 'this.t = null;',
  32420. '']),
  32421. LinesToStr([ // $mod.$main
  32422. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32423. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32424. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32425. '']));
  32426. end;
  32427. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32428. begin
  32429. WithTypeInfo:=true;
  32430. AddModuleWithIntfImplSrc('typinfo.pas',
  32431. LinesToStr([
  32432. '{$modeswitch externalclass}',
  32433. 'type',
  32434. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32435. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32436. '']),
  32437. '');
  32438. AddModuleWithIntfImplSrc('unit2.pas',
  32439. LinesToStr([
  32440. 'uses typinfo;',
  32441. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32442. 'procedure DoPtr(p: PTypeInfo);',
  32443. 'procedure DoInfo(t: TTypeInfo);',
  32444. 'procedure DoInt(t: TTypeInfoInteger);',
  32445. '']),
  32446. LinesToStr([
  32447. 'procedure DoPtr(p: PTypeInfo);',
  32448. 'begin end;',
  32449. 'procedure DoInfo(t: TTypeInfo);',
  32450. 'begin end;',
  32451. 'procedure DoInt(t: TTypeInfoInteger);',
  32452. 'begin end;',
  32453. '']));
  32454. StartUnit(true);
  32455. Add([
  32456. 'interface',
  32457. 'uses unit2;', // does not use unit typinfo
  32458. 'implementation',
  32459. 'var',
  32460. ' i: byte;',
  32461. ' p: pointer;',
  32462. ' t: PTypeInfo;',
  32463. 'initialization',
  32464. ' p:=typeinfo(i);',
  32465. ' t:=typeinfo(i);',
  32466. ' if p=t then ;',
  32467. ' if p=typeinfo(i) then ;',
  32468. ' if typeinfo(i)=p then ;',
  32469. ' if t=typeinfo(i) then ;',
  32470. ' if typeinfo(i)=t then ;',
  32471. ' DoPtr(p);',
  32472. ' DoPtr(t);',
  32473. ' DoPtr(typeinfo(i));',
  32474. ' DoInfo(p);',
  32475. ' DoInfo(t);',
  32476. ' DoInfo(typeinfo(i));',
  32477. ' DoInt(typeinfo(i));',
  32478. '']);
  32479. ConvertUnit;
  32480. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32481. LinesToStr([ // statements
  32482. 'var $impl = $mod.$impl;',
  32483. '']),
  32484. LinesToStr([ // this.$init
  32485. '$impl.p = rtl.byte;',
  32486. '$impl.t = rtl.byte;',
  32487. 'if ($impl.p === $impl.t) ;',
  32488. 'if ($impl.p === rtl.byte) ;',
  32489. 'if (rtl.byte === $impl.p) ;',
  32490. 'if ($impl.t === rtl.byte) ;',
  32491. 'if (rtl.byte === $impl.t) ;',
  32492. 'pas.unit2.DoPtr($impl.p);',
  32493. 'pas.unit2.DoPtr($impl.t);',
  32494. 'pas.unit2.DoPtr(rtl.byte);',
  32495. 'pas.unit2.DoInfo($impl.p);',
  32496. 'pas.unit2.DoInfo($impl.t);',
  32497. 'pas.unit2.DoInfo(rtl.byte);',
  32498. 'pas.unit2.DoInt(rtl.byte);',
  32499. '']),
  32500. LinesToStr([ // implementation
  32501. '$impl.i = 0;',
  32502. '$impl.p = null;',
  32503. '$impl.t = null;',
  32504. '']) );
  32505. end;
  32506. procedure TTestModule.TestRTTI_Interface_Corba;
  32507. begin
  32508. WithTypeInfo:=true;
  32509. StartProgram(true,[supTypeInfo]);
  32510. Add([
  32511. '{$interfaces corba}',
  32512. '{$modeswitch externalclass}',
  32513. 'type',
  32514. ' IUnknown = interface',
  32515. ' end;',
  32516. ' IBird = interface',
  32517. ' function GetItem: longint;',
  32518. ' procedure SetItem(Value: longint);',
  32519. ' property Item: longint read GetItem write SetItem;',
  32520. ' end;',
  32521. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32522. 'var',
  32523. ' i: IBird;',
  32524. ' t: TTypeInfoInterface;',
  32525. 'begin',
  32526. ' t:=TypeInfo(IBird);',
  32527. ' t:=TypeInfo(i);',
  32528. ' DoIt(t);',
  32529. ' DoIt(TypeInfo(IBird));',
  32530. '']);
  32531. ConvertProgram;
  32532. CheckSource('TestRTTI_Interface_Corba',
  32533. LinesToStr([ // statements
  32534. 'rtl.createInterface(',
  32535. ' this,',
  32536. ' "IUnknown",',
  32537. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32538. ' [],',
  32539. ' null,',
  32540. ' function () {',
  32541. ' }',
  32542. ');',
  32543. 'rtl.createInterface(',
  32544. ' this,',
  32545. ' "IBird",',
  32546. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32547. ' ["GetItem", "SetItem"],',
  32548. ' null,',
  32549. ' function () {',
  32550. ' var $r = this.$rtti;',
  32551. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32552. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32553. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32554. ' }',
  32555. ');',
  32556. 'this.DoIt = function (t) {',
  32557. '}; ',
  32558. 'this.i = null;',
  32559. 'this.t = null;',
  32560. '']),
  32561. LinesToStr([ // $mod.$main
  32562. '$mod.t = $mod.$rtti["IBird"];',
  32563. '$mod.t = $mod.i.$rtti;',
  32564. '$mod.DoIt($mod.t);',
  32565. '$mod.DoIt($mod.$rtti["IBird"]);',
  32566. '']));
  32567. end;
  32568. procedure TTestModule.TestRTTI_Interface_COM;
  32569. begin
  32570. WithTypeInfo:=true;
  32571. StartProgram(true,[supTypeInfo]);
  32572. Add([
  32573. '{$interfaces com}',
  32574. '{$modeswitch externalclass}',
  32575. 'type',
  32576. ' TGuid = record end;',
  32577. ' integer = longint;',
  32578. ' IUnknown = interface',
  32579. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32580. ' function _AddRef: Integer;',
  32581. ' function _Release: Integer;',
  32582. ' end;',
  32583. ' IBird = interface',
  32584. ' function GetItem: longint;',
  32585. ' procedure SetItem(Value: longint);',
  32586. ' property Item: longint read GetItem write SetItem;',
  32587. ' end;',
  32588. 'var',
  32589. ' i: IBird;',
  32590. ' t: TTypeInfoInterface;',
  32591. 'begin',
  32592. ' t:=TypeInfo(IBird);',
  32593. ' t:=TypeInfo(i);',
  32594. '']);
  32595. ConvertProgram;
  32596. CheckSource('TestRTTI_Interface_COM',
  32597. LinesToStr([ // statements
  32598. 'rtl.recNewT(this, "TGuid", function () {',
  32599. ' this.$eq = function (b) {',
  32600. ' return true;',
  32601. ' };',
  32602. ' this.$assign = function (s) {',
  32603. ' return this;',
  32604. ' };',
  32605. ' $mod.$rtti.$Record("TGuid", {});',
  32606. '});',
  32607. 'rtl.createInterface(',
  32608. ' this,',
  32609. ' "IUnknown",',
  32610. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32611. ' ["QueryInterface", "_AddRef", "_Release"],',
  32612. ' null,',
  32613. ' function () {',
  32614. ' this.$kind = "com";',
  32615. ' var $r = this.$rtti;',
  32616. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32617. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32618. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32619. ' }',
  32620. ');',
  32621. 'rtl.createInterface(',
  32622. ' this,',
  32623. ' "IBird",',
  32624. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32625. ' ["GetItem", "SetItem"],',
  32626. ' this.IUnknown,',
  32627. ' function () {',
  32628. ' var $r = this.$rtti;',
  32629. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32630. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32631. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32632. ' }',
  32633. ');',
  32634. 'this.i = null;',
  32635. 'this.t = null;',
  32636. '']),
  32637. LinesToStr([ // $mod.$main
  32638. '$mod.t = $mod.$rtti["IBird"];',
  32639. '$mod.t = $mod.i.$rtti;',
  32640. '']));
  32641. end;
  32642. procedure TTestModule.TestRTTI_ClassHelper;
  32643. begin
  32644. WithTypeInfo:=true;
  32645. StartProgram(true,[supTypeInfo]);
  32646. Add([
  32647. '{$interfaces com}',
  32648. '{$modeswitch externalclass}',
  32649. 'type',
  32650. ' TObject = class',
  32651. ' end;',
  32652. ' THelper = class helper for TObject',
  32653. ' published',
  32654. ' function GetItem: longint;',
  32655. ' property Item: longint read GetItem;',
  32656. ' end;',
  32657. 'function THelper.GetItem: longint;',
  32658. 'begin',
  32659. 'end;',
  32660. 'var',
  32661. ' t: TTypeInfoHelper;',
  32662. 'begin',
  32663. ' t:=TypeInfo(THelper);',
  32664. '']);
  32665. ConvertProgram;
  32666. CheckSource('TestRTTI_ClassHelper',
  32667. LinesToStr([ // statements
  32668. 'rtl.createClass(this, "TObject", null, function () {',
  32669. ' this.$init = function () {',
  32670. ' };',
  32671. ' this.$final = function () {',
  32672. ' };',
  32673. '});',
  32674. 'rtl.createHelper(this, "THelper", null, function () {',
  32675. ' this.GetItem = function () {',
  32676. ' var Result = 0;',
  32677. ' return Result;',
  32678. ' };',
  32679. ' var $r = this.$rtti;',
  32680. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32681. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32682. '});',
  32683. 'this.t = null;',
  32684. '']),
  32685. LinesToStr([ // $mod.$main
  32686. '$mod.t = $mod.$rtti["THelper"];',
  32687. '']));
  32688. end;
  32689. procedure TTestModule.TestRTTI_ExternalClass;
  32690. begin
  32691. WithTypeInfo:=true;
  32692. StartProgram(true,[supTypeInfo]);
  32693. Add([
  32694. '{$modeswitch externalclass}',
  32695. 'type',
  32696. ' TJSObject = class external name ''Object''',
  32697. ' end;',
  32698. ' TJSArray = class external name ''Array'' (TJSObject)',
  32699. ' end;',
  32700. 'var',
  32701. ' p: Pointer;',
  32702. ' tc: TTypeInfoExtClass;',
  32703. 'begin',
  32704. ' p:=typeinfo(TJSArray);']);
  32705. ConvertProgram;
  32706. CheckSource('TestRTTI_ExternalClass',
  32707. LinesToStr([ // statements
  32708. 'this.$rtti.$ExtClass("TJSObject", {',
  32709. ' jsclass: "Object"',
  32710. '});',
  32711. 'this.$rtti.$ExtClass("TJSArray", {',
  32712. ' ancestor: this.$rtti["TJSObject"],',
  32713. ' jsclass: "Array"',
  32714. '});',
  32715. 'this.p = null;',
  32716. 'this.tc = null;',
  32717. '']),
  32718. LinesToStr([ // $mod.$main
  32719. '$mod.p = $mod.$rtti["TJSArray"];',
  32720. '']));
  32721. end;
  32722. procedure TTestModule.TestRTTI_Unit;
  32723. begin
  32724. WithTypeInfo:=true;
  32725. AddModuleWithIntfImplSrc('unit2.pas',
  32726. LinesToStr([
  32727. '{$mode delphi}',
  32728. 'type',
  32729. ' TWordArray = array of word;',
  32730. ' TArray<T> = array of T;',
  32731. '']),
  32732. '');
  32733. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  32734. Add([
  32735. '{$mode delphi}',
  32736. 'interface',
  32737. 'uses unit2;',
  32738. 'type',
  32739. ' IBird = interface',
  32740. ' function Swoop: TWordArray;',
  32741. ' function Glide: TArray<word>;',
  32742. ' end;',
  32743. 'procedure Fly;',
  32744. 'implementation',
  32745. 'procedure Fly;',
  32746. 'var',
  32747. ' ta: tTypeInfoDynArray;',
  32748. ' ti: tTypeInfoInterface;',
  32749. 'begin',
  32750. ' ta:=typeinfo(TWordArray);',
  32751. ' ta:=typeinfo(TArray<word>);',
  32752. ' ti:=typeinfo(IBird);',
  32753. 'end;',
  32754. '']);
  32755. ConvertUnit;
  32756. CheckSource('TestRTTI_ExternalClass',
  32757. LinesToStr([ // statements
  32758. 'rtl.createInterface(',
  32759. ' this,',
  32760. ' "IBird",',
  32761. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  32762. ' ["Swoop", "Glide"],',
  32763. ' pas.system.IUnknown,',
  32764. ' function () {',
  32765. ' var $r = this.$rtti;',
  32766. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  32767. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  32768. ' }',
  32769. ');',
  32770. 'this.Fly = function () {',
  32771. ' var ta = null;',
  32772. ' var ti = null;',
  32773. ' ta = pas.unit2.$rtti["TWordArray"];',
  32774. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  32775. ' ti = $mod.$rtti["IBird"];',
  32776. '};',
  32777. '']),
  32778. LinesToStr([ // $mod.$main
  32779. '']));
  32780. end;
  32781. procedure TTestModule.TestResourcestringProgram;
  32782. begin
  32783. AddModuleWithIntfImplSrc('unit2.pas',
  32784. LinesToStr([
  32785. 'resourcestring Title = ''Nice'';',
  32786. '']),
  32787. '');
  32788. StartProgram(true);
  32789. Add([
  32790. 'uses unit2;',
  32791. 'const Bar = ''bar'';',
  32792. 'resourcestring',
  32793. ' Red = ''red'';',
  32794. ' Foobar = ''fOo''+bar;',
  32795. 'var s: string;',
  32796. ' c: char;',
  32797. 'begin',
  32798. ' s:=red;',
  32799. ' s:=test1.red;',
  32800. ' s:=Title;',
  32801. ' c:=red[1];',
  32802. ' c:=test1.red[2];',
  32803. ' if red=foobar then ;',
  32804. ' if red[3]=red[4] then ;']);
  32805. ConvertProgram;
  32806. CheckSource('TestResourcestringProgram',
  32807. LinesToStr([ // statements
  32808. 'this.Bar = "bar";',
  32809. 'this.s = "";',
  32810. 'this.c = "";',
  32811. '$mod.$resourcestrings = {',
  32812. ' Red: {',
  32813. ' org: "red"',
  32814. ' },',
  32815. ' Foobar: {',
  32816. ' org: "fOobar"',
  32817. ' }',
  32818. '};',
  32819. '']),
  32820. LinesToStr([ // $mod.$main
  32821. '$mod.s = rtl.getResStr($mod, "Red");',
  32822. '$mod.s = rtl.getResStr($mod, "Red");',
  32823. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32824. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32825. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32826. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32827. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32828. '']));
  32829. end;
  32830. procedure TTestModule.TestResourcestringUnit;
  32831. begin
  32832. AddModuleWithIntfImplSrc('unit2.pas',
  32833. LinesToStr([
  32834. 'resourcestring Title = ''Nice'';',
  32835. '']),
  32836. '');
  32837. StartUnit(true);
  32838. Add([
  32839. 'interface',
  32840. 'uses unit2;',
  32841. 'const Red = ''rEd'';',
  32842. 'resourcestring',
  32843. ' Blue = ''blue'';',
  32844. ' NotRed = ''not''+Red;',
  32845. 'var s: string;',
  32846. 'implementation',
  32847. 'resourcestring',
  32848. ' ImplGreen = ''green'';',
  32849. 'initialization',
  32850. ' s:=blue+ImplGreen;',
  32851. ' s:=test1.blue+test1.implgreen;',
  32852. ' s:=blue[1]+implgreen[2];',
  32853. ' s:=Title;',
  32854. '']);
  32855. ConvertUnit;
  32856. CheckSource('TestResourcestringUnit',
  32857. LinesToStr([ // statements
  32858. 'this.Red = "rEd";',
  32859. 'this.s = "";',
  32860. '$mod.$resourcestrings = {',
  32861. ' Blue: {',
  32862. ' org: "blue"',
  32863. ' },',
  32864. ' NotRed: {',
  32865. ' org: "notrEd"',
  32866. ' },',
  32867. ' ImplGreen: {',
  32868. ' org: "green"',
  32869. ' }',
  32870. '};',
  32871. '']),
  32872. LinesToStr([ // $mod.$main
  32873. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32874. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32875. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32876. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32877. '']));
  32878. end;
  32879. procedure TTestModule.TestResourcestringImplementation;
  32880. begin
  32881. StartUnit(false);
  32882. Add([
  32883. 'interface',
  32884. 'implementation',
  32885. 'resourcestring',
  32886. ' ImplRed = ''red'';']);
  32887. ConvertUnit;
  32888. CheckSource('TestResourcestringImplementation',
  32889. LinesToStr([ // intf statements
  32890. 'var $impl = $mod.$impl;']),
  32891. LinesToStr([ // $mod.$init
  32892. '']),
  32893. LinesToStr([ // impl statements
  32894. '$mod.$resourcestrings = {',
  32895. ' ImplRed: {',
  32896. ' org: "red"',
  32897. ' }',
  32898. '};',
  32899. '']));
  32900. end;
  32901. procedure TTestModule.TestAttributes_Members;
  32902. begin
  32903. WithTypeInfo:=true;
  32904. StartProgram(false);
  32905. Add([
  32906. '{$modeswitch PrefixedAttributes}',
  32907. 'type',
  32908. ' TObject = class',
  32909. ' constructor Create;',
  32910. ' end;',
  32911. ' TCustomAttribute = class',
  32912. ' constructor Create(Id: word);',
  32913. ' end;',
  32914. ' [Missing]',
  32915. ' TBird = class',
  32916. ' published',
  32917. ' [Tcustom]',
  32918. ' FField: word;',
  32919. ' [tcustom(14)]',
  32920. ' property Size: word read FField;',
  32921. ' [Tcustom(15)]',
  32922. ' procedure Fly; virtual; abstract;',
  32923. ' end;',
  32924. ' TRec = record',
  32925. ' [Tcustom,tcustom(14)]',
  32926. ' Size: word;',
  32927. ' end;',
  32928. 'constructor TObject.Create; begin end;',
  32929. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32930. 'begin',
  32931. '']);
  32932. ConvertProgram;
  32933. CheckSource('TestAttributes_Members',
  32934. LinesToStr([ // statements
  32935. 'rtl.createClass(this, "TObject", null, function () {',
  32936. ' this.$init = function () {',
  32937. ' };',
  32938. ' this.$final = function () {',
  32939. ' };',
  32940. ' this.Create = function () {',
  32941. ' return this;',
  32942. ' };',
  32943. '});',
  32944. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32945. ' this.Create$1 = function (Id) {',
  32946. ' return this;',
  32947. ' };',
  32948. '});',
  32949. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32950. ' this.$init = function () {',
  32951. ' $mod.TObject.$init.call(this);',
  32952. ' this.FField = 0;',
  32953. ' };',
  32954. ' var $r = this.$rtti;',
  32955. ' $r.addField("FField", rtl.word, {',
  32956. ' attr: [$mod.TCustomAttribute, "Create"]',
  32957. ' });',
  32958. ' $r.addProperty(',
  32959. ' "Size",',
  32960. ' 0,',
  32961. ' rtl.word,',
  32962. ' "FField",',
  32963. ' "",',
  32964. ' {',
  32965. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  32966. ' }',
  32967. ' );',
  32968. ' $r.addMethod("Fly", 0, [], null, 0, {',
  32969. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  32970. ' });',
  32971. '});',
  32972. 'rtl.recNewT(this, "TRec", function () {',
  32973. ' this.Size = 0;',
  32974. ' this.$eq = function (b) {',
  32975. ' return this.Size === b.Size;',
  32976. ' };',
  32977. ' this.$assign = function (s) {',
  32978. ' this.Size = s.Size;',
  32979. ' return this;',
  32980. ' };',
  32981. ' var $r = $mod.$rtti.$Record("TRec", {});',
  32982. ' $r.addField("Size", rtl.word, {',
  32983. ' attr: [',
  32984. ' $mod.TCustomAttribute,',
  32985. ' "Create",',
  32986. ' $mod.TCustomAttribute,',
  32987. ' "Create$1",',
  32988. ' [14]',
  32989. ' ]',
  32990. ' });',
  32991. '});',
  32992. '']),
  32993. LinesToStr([ // $mod.$main
  32994. '']));
  32995. end;
  32996. procedure TTestModule.TestAttributes_Types;
  32997. begin
  32998. WithTypeInfo:=true;
  32999. StartProgram(false);
  33000. Add([
  33001. '{$modeswitch PrefixedAttributes}',
  33002. 'type',
  33003. ' TObject = class',
  33004. ' constructor Create(Id: word);',
  33005. ' end;',
  33006. ' TCustomAttribute = class',
  33007. ' end;',
  33008. ' [TCustom(1)]',
  33009. ' TMyClass = class',
  33010. ' end;',
  33011. ' [TCustom(11)]',
  33012. ' TMyDescendant = class(TMyClass)',
  33013. ' end;',
  33014. ' [TCustom(2)]',
  33015. ' TRec = record',
  33016. ' end;',
  33017. ' [TCustom(3)]',
  33018. ' TInt = type word;',
  33019. 'constructor TObject.Create(Id: word);',
  33020. 'begin',
  33021. 'end;',
  33022. 'var p: pointer;',
  33023. 'begin',
  33024. ' p:=typeinfo(TMyClass);',
  33025. ' p:=typeinfo(TRec);',
  33026. ' p:=typeinfo(TInt);',
  33027. '']);
  33028. ConvertProgram;
  33029. CheckSource('TestAttributes_Types',
  33030. LinesToStr([ // statements
  33031. 'rtl.createClass(this, "TObject", null, function () {',
  33032. ' this.$init = function () {',
  33033. ' };',
  33034. ' this.$final = function () {',
  33035. ' };',
  33036. ' this.Create = function (Id) {',
  33037. ' return this;',
  33038. ' };',
  33039. '});',
  33040. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33041. '});',
  33042. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  33043. ' var $r = this.$rtti;',
  33044. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  33045. '});',
  33046. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  33047. ' var $r = this.$rtti;',
  33048. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  33049. '});',
  33050. 'rtl.recNewT(this, "TRec", function () {',
  33051. ' this.$eq = function (b) {',
  33052. ' return true;',
  33053. ' };',
  33054. ' this.$assign = function (s) {',
  33055. ' return this;',
  33056. ' };',
  33057. ' $mod.$rtti.$Record("TRec", {',
  33058. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  33059. ' });',
  33060. '});',
  33061. 'this.$rtti.$inherited("TInt", rtl.word, {',
  33062. ' attr: [this.TCustomAttribute, "Create", [3]]',
  33063. '});',
  33064. 'this.p = null;',
  33065. '']),
  33066. LinesToStr([ // $mod.$main
  33067. '$mod.p = $mod.$rtti["TMyClass"];',
  33068. '$mod.p = $mod.$rtti["TRec"];',
  33069. '$mod.p = $mod.$rtti["TInt"];',
  33070. '']));
  33071. end;
  33072. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  33073. begin
  33074. WithTypeInfo:=true;
  33075. StartProgram(false);
  33076. Add([
  33077. '{$modeswitch PrefixedAttributes}',
  33078. 'type',
  33079. ' TObject = class',
  33080. ' constructor Create;',
  33081. ' end;',
  33082. ' TCustomAttribute = class',
  33083. ' end;',
  33084. ' THelper = class helper for TCustomAttribute',
  33085. ' constructor Create(Id: word);',
  33086. ' end;',
  33087. ' [TCustom(3)]',
  33088. ' TMyInt = word;',
  33089. 'constructor TObject.Create; begin end;',
  33090. 'constructor THelper.Create(Id: word); begin end;',
  33091. 'begin',
  33092. ' if typeinfo(TMyInt)=nil then ;']);
  33093. ConvertProgram;
  33094. end;
  33095. procedure TTestModule.TestAttributes_InterfacesList;
  33096. begin
  33097. WithTypeInfo:=true;
  33098. StartProgram(false);
  33099. Add([
  33100. '{$mode Delphi}',
  33101. 'type',
  33102. ' TObject = class',
  33103. ' constructor Create;',
  33104. ' end;',
  33105. ' IInterface = interface end;',
  33106. ' TCustomAttribute = class',
  33107. ' end;',
  33108. ' Red = class(TCustomAttribute);',
  33109. ' Blue = class(TCustomAttribute);',
  33110. ' [Red]',
  33111. ' IBird<T> = interface',
  33112. ' procedure Fly;',
  33113. ' end;',
  33114. ' [Blue]',
  33115. ' IEagle = interface(IBird<Word>)',
  33116. ' procedure Dive;',
  33117. ' end;',
  33118. ' TAnt = class(TObject, IEagle)',
  33119. ' procedure Fly; virtual; abstract;',
  33120. ' procedure Dive; virtual; abstract;',
  33121. ' end;',
  33122. 'constructor TObject.Create;',
  33123. 'begin',
  33124. 'end;',
  33125. 'begin',
  33126. '']);
  33127. ConvertProgram;
  33128. CheckSource('TestAttributes_InterfacesList',
  33129. LinesToStr([ // statements
  33130. '$mod.$rtti.$Interface("IBird<System.Word>");',
  33131. 'rtl.createClass(this, "TObject", null, function () {',
  33132. ' this.$init = function () {',
  33133. ' };',
  33134. ' this.$final = function () {',
  33135. ' };',
  33136. ' this.Create = function () {',
  33137. ' return this;',
  33138. ' };',
  33139. '});',
  33140. 'rtl.createInterface(',
  33141. ' this,',
  33142. ' "IInterface",',
  33143. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  33144. ' [],',
  33145. ' null,',
  33146. ' function () {',
  33147. ' this.$kind = "com";',
  33148. ' }',
  33149. ');',
  33150. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33151. '});',
  33152. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  33153. '});',
  33154. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  33155. '});',
  33156. 'rtl.createInterface(',
  33157. ' this,',
  33158. ' "IBird$G1",',
  33159. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  33160. ' ["Fly"],',
  33161. ' this.IInterface,',
  33162. ' function () {',
  33163. ' var $r = this.$rtti;',
  33164. ' $r.addMethod("Fly", 0, []);',
  33165. ' $r.attr = [$mod.Red, "Create"];',
  33166. ' },',
  33167. ' "IBird<System.Word>"',
  33168. ');',
  33169. 'rtl.createInterface(',
  33170. ' this,',
  33171. ' "IEagle",',
  33172. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  33173. ' ["Dive"],',
  33174. ' this.IBird$G1,',
  33175. ' function () {',
  33176. ' var $r = this.$rtti;',
  33177. ' $r.addMethod("Dive", 0, []);',
  33178. ' $r.attr = [$mod.Blue, "Create"];',
  33179. ' }',
  33180. ');',
  33181. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  33182. ' rtl.addIntf(this, $mod.IEagle);',
  33183. '});',
  33184. '']),
  33185. LinesToStr([ // $mod.$main
  33186. '']));
  33187. end;
  33188. procedure TTestModule.TestAssert;
  33189. begin
  33190. StartProgram(false);
  33191. Add([
  33192. 'procedure DoIt;',
  33193. 'var',
  33194. ' b: boolean;',
  33195. ' s: string;',
  33196. 'begin',
  33197. ' {$Assertions on}',
  33198. ' Assert(b);',
  33199. 'end;',
  33200. 'begin',
  33201. ' DoIt;',
  33202. '']);
  33203. ConvertProgram;
  33204. CheckSource('TestAssert',
  33205. LinesToStr([ // statements
  33206. 'this.DoIt = function () {',
  33207. ' var b = false;',
  33208. ' var s = "";',
  33209. ' if (!b) throw "assert failed";',
  33210. '};',
  33211. '']),
  33212. LinesToStr([ // $mod.$main
  33213. '$mod.DoIt();',
  33214. '']));
  33215. end;
  33216. procedure TTestModule.TestAssert_SysUtils;
  33217. begin
  33218. AddModuleWithIntfImplSrc('SysUtils.pas',
  33219. LinesToStr([
  33220. 'type',
  33221. ' TObject = class',
  33222. ' constructor Create;',
  33223. ' end;',
  33224. ' EAssertionFailed = class',
  33225. ' constructor Create(s: string);',
  33226. ' end;',
  33227. '']),
  33228. LinesToStr([
  33229. 'constructor TObject.Create;',
  33230. 'begin end;',
  33231. 'constructor EAssertionFailed.Create(s: string);',
  33232. 'begin end;',
  33233. '']) );
  33234. StartProgram(true);
  33235. Add([
  33236. 'uses sysutils;',
  33237. 'procedure DoIt;',
  33238. 'var',
  33239. ' b: boolean;',
  33240. ' s: string;',
  33241. 'begin',
  33242. ' {$Assertions on}',
  33243. ' Assert(b);',
  33244. ' Assert(b,''msg'');',
  33245. 'end;',
  33246. 'begin',
  33247. ' DoIt;',
  33248. '']);
  33249. ConvertProgram;
  33250. CheckSource('TestAssert_SysUtils',
  33251. LinesToStr([ // statements
  33252. 'this.DoIt = function () {',
  33253. ' var b = false;',
  33254. ' var s = "";',
  33255. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  33256. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  33257. '};',
  33258. '']),
  33259. LinesToStr([ // $mod.$main
  33260. '$mod.DoIt();',
  33261. '']));
  33262. end;
  33263. procedure TTestModule.TestObjectChecks;
  33264. begin
  33265. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  33266. StartProgram(false);
  33267. Add([
  33268. 'type',
  33269. ' TObject = class',
  33270. ' procedure DoIt;',
  33271. ' end;',
  33272. ' TClass = class of tobject;',
  33273. ' TBird = class',
  33274. ' end;',
  33275. ' TBirdClass = class of TBird;',
  33276. 'var',
  33277. ' o : TObject;',
  33278. ' c: TClass;',
  33279. ' b: TBird;',
  33280. ' bc: TBirdClass;',
  33281. 'procedure TObject.DoIt;',
  33282. 'begin',
  33283. ' b:=TBird(o);',
  33284. 'end;',
  33285. 'begin',
  33286. ' o.DoIt;',
  33287. ' b:=TBird(o);',
  33288. ' bc:=TBirdClass(c);',
  33289. '']);
  33290. ConvertProgram;
  33291. CheckSource('TestCheckMethodCall',
  33292. LinesToStr([ // statements
  33293. 'rtl.createClass(this, "TObject", null, function () {',
  33294. ' this.$init = function () {',
  33295. ' };',
  33296. ' this.$final = function () {',
  33297. ' };',
  33298. ' this.DoIt = function () {',
  33299. ' rtl.checkMethodCall(this,$mod.TObject);',
  33300. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  33301. ' };',
  33302. '});',
  33303. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33304. '});',
  33305. 'this.o = null;',
  33306. 'this.c = null;',
  33307. 'this.b = null;',
  33308. 'this.bc = null;',
  33309. '']),
  33310. LinesToStr([ // $mod.$main
  33311. '$mod.o.DoIt();',
  33312. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  33313. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  33314. '']));
  33315. end;
  33316. procedure TTestModule.TestOverflowChecks_Int;
  33317. begin
  33318. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  33319. StartProgram(false);
  33320. Add([
  33321. 'procedure DoIt;',
  33322. 'var',
  33323. ' b: byte;',
  33324. ' n: nativeint;',
  33325. ' u: nativeuint;',
  33326. ' c: currency;',
  33327. 'begin',
  33328. ' n:=n+n;',
  33329. ' n:=n-n;',
  33330. ' n:=n+b;',
  33331. ' n:=b-n;',
  33332. ' n:=n*n;',
  33333. ' n:=n*u;',
  33334. ' c:=c+b;',
  33335. ' c:=b+c;',
  33336. ' c:=c*b;',
  33337. ' c:=b*c;',
  33338. 'end;',
  33339. 'begin',
  33340. '']);
  33341. ConvertProgram;
  33342. CheckSource('TestOverflowChecks_Int',
  33343. LinesToStr([ // statements
  33344. 'this.DoIt = function () {',
  33345. ' var b = 0;',
  33346. ' var n = 0;',
  33347. ' var u = 0;',
  33348. ' var c = 0;',
  33349. ' n = rtl.oc(n + n);',
  33350. ' n = rtl.oc(n - n);',
  33351. ' n = rtl.oc(n + b);',
  33352. ' n = rtl.oc(b - n);',
  33353. ' n = rtl.oc(n * n);',
  33354. ' n = rtl.oc(n * u);',
  33355. ' c = rtl.oc(c + (b * 10000));',
  33356. ' c = rtl.oc((b * 10000) + c);',
  33357. ' c = rtl.oc(c * b);',
  33358. ' c = rtl.oc(b * c);',
  33359. '};',
  33360. '']),
  33361. LinesToStr([ // $mod.$main
  33362. '']));
  33363. end;
  33364. procedure TTestModule.TestRangeChecks_AssignInt;
  33365. begin
  33366. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33367. StartProgram(false);
  33368. Add([
  33369. '{$R+}',
  33370. 'var',
  33371. ' b: byte = 2;',
  33372. ' w: word = 3;',
  33373. 'procedure DoIt(p: byte);',
  33374. 'begin',
  33375. ' b:=w;',
  33376. ' b+=w;',
  33377. ' b:=1;',
  33378. 'end;',
  33379. '{$R-}',
  33380. 'procedure DoSome;',
  33381. 'begin',
  33382. ' DoIt(w);',
  33383. ' b:=w;',
  33384. ' b:=2;',
  33385. 'end;',
  33386. 'begin',
  33387. '{$R+}',
  33388. '']);
  33389. ConvertProgram;
  33390. CheckSource('TestRangeChecks_AssignInt',
  33391. LinesToStr([ // statements
  33392. 'this.b = 2;',
  33393. 'this.w = 3;',
  33394. 'this.DoIt = function (p) {',
  33395. ' rtl.rc(p, 0, 255);',
  33396. ' $mod.b = rtl.rc($mod.w,0,255);',
  33397. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33398. ' $mod.b = 1;',
  33399. '};',
  33400. 'this.DoSome = function () {',
  33401. ' $mod.DoIt($mod.w);',
  33402. ' $mod.b = $mod.w;',
  33403. ' $mod.b = 2;',
  33404. '};',
  33405. '']),
  33406. LinesToStr([ // $mod.$main
  33407. '']));
  33408. end;
  33409. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33410. begin
  33411. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33412. StartProgram(false);
  33413. Add([
  33414. '{$R+}',
  33415. 'type Ten = 1..10;',
  33416. 'var',
  33417. ' b: Ten = 2;',
  33418. ' w: Ten = 3;',
  33419. 'procedure DoIt(p: Ten);',
  33420. 'begin',
  33421. ' b:=w;',
  33422. ' b+=w;',
  33423. ' b:=1;',
  33424. 'end;',
  33425. '{$R-}',
  33426. 'procedure DoSome;',
  33427. 'begin',
  33428. ' DoIt(w);',
  33429. ' b:=w;',
  33430. ' b:=2;',
  33431. 'end;',
  33432. 'begin',
  33433. '{$R+}',
  33434. '']);
  33435. ConvertProgram;
  33436. CheckSource('TestRangeChecks_AssignIntRange',
  33437. LinesToStr([ // statements
  33438. 'this.b = 2;',
  33439. 'this.w = 3;',
  33440. 'this.DoIt = function (p) {',
  33441. ' rtl.rc(p, 1, 10);',
  33442. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33443. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33444. ' $mod.b = 1;',
  33445. '};',
  33446. 'this.DoSome = function () {',
  33447. ' $mod.DoIt($mod.w);',
  33448. ' $mod.b = $mod.w;',
  33449. ' $mod.b = 2;',
  33450. '};',
  33451. '']),
  33452. LinesToStr([ // $mod.$main
  33453. '']));
  33454. end;
  33455. procedure TTestModule.TestRangeChecks_AssignEnum;
  33456. begin
  33457. StartProgram(false);
  33458. Add([
  33459. '{$R+}',
  33460. 'type TEnum = (red,green);',
  33461. 'var',
  33462. ' e: TEnum = red;',
  33463. 'procedure DoIt(p: TEnum);',
  33464. 'begin',
  33465. ' e:=p;',
  33466. ' p:=TEnum(0);',
  33467. ' p:=succ(e);',
  33468. 'end;',
  33469. '{$R-}',
  33470. 'procedure DoSome;',
  33471. 'begin',
  33472. ' DoIt(e);',
  33473. ' e:=TEnum(1);',
  33474. ' e:=pred(e);',
  33475. 'end;',
  33476. 'begin',
  33477. '{$R+}',
  33478. '']);
  33479. ConvertProgram;
  33480. CheckSource('TestRangeChecks_AssignEnum',
  33481. LinesToStr([ // statements
  33482. 'this.TEnum = {',
  33483. ' "0": "red",',
  33484. ' red: 0,',
  33485. ' "1": "green",',
  33486. ' green: 1',
  33487. '};',
  33488. 'this.e = this.TEnum.red;',
  33489. 'this.DoIt = function (p) {',
  33490. ' rtl.rc(p, 0, 1);',
  33491. ' $mod.e = rtl.rc(p, 0, 1);',
  33492. ' p = 0;',
  33493. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33494. '};',
  33495. 'this.DoSome = function () {',
  33496. ' $mod.DoIt($mod.e);',
  33497. ' $mod.e = 1;',
  33498. ' $mod.e = $mod.e - 1;',
  33499. '};',
  33500. '']),
  33501. LinesToStr([ // $mod.$main
  33502. '']));
  33503. end;
  33504. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33505. begin
  33506. StartProgram(false);
  33507. Add([
  33508. '{$R+}',
  33509. 'type',
  33510. ' TEnum = (red,green);',
  33511. ' TEnumRg = red..green;',
  33512. 'var',
  33513. ' e: TEnumRg = red;',
  33514. 'procedure DoIt(p: TEnumRg);',
  33515. 'begin',
  33516. ' e:=p;',
  33517. ' p:=TEnumRg(0);',
  33518. ' p:=succ(e);',
  33519. 'end;',
  33520. '{$R-}',
  33521. 'procedure DoSome;',
  33522. 'begin',
  33523. ' DoIt(e);',
  33524. ' e:=TEnum(1);',
  33525. ' e:=pred(e);',
  33526. 'end;',
  33527. 'begin',
  33528. '{$R+}',
  33529. '']);
  33530. ConvertProgram;
  33531. CheckSource('TestRangeChecks_AssignEnumRange',
  33532. LinesToStr([ // statements
  33533. 'this.TEnum = {',
  33534. ' "0": "red",',
  33535. ' red: 0,',
  33536. ' "1": "green",',
  33537. ' green: 1',
  33538. '};',
  33539. 'this.e = this.TEnum.red;',
  33540. 'this.DoIt = function (p) {',
  33541. ' rtl.rc(p, 0, 1);',
  33542. ' $mod.e = rtl.rc(p, 0, 1);',
  33543. ' p = 0;',
  33544. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33545. '};',
  33546. 'this.DoSome = function () {',
  33547. ' $mod.DoIt($mod.e);',
  33548. ' $mod.e = 1;',
  33549. ' $mod.e = $mod.e - 1;',
  33550. '};',
  33551. '']),
  33552. LinesToStr([ // $mod.$main
  33553. '']));
  33554. end;
  33555. procedure TTestModule.TestRangeChecks_AssignChar;
  33556. begin
  33557. StartProgram(false);
  33558. Add([
  33559. '{$R+}',
  33560. 'type',
  33561. ' TLetter = char;',
  33562. 'var',
  33563. ' b: TLetter = ''2'';',
  33564. ' w: TLetter = ''3'';',
  33565. 'procedure DoIt(p: TLetter);',
  33566. 'begin',
  33567. ' b:=w;',
  33568. ' b:=''1'';',
  33569. 'end;',
  33570. '{$R-}',
  33571. 'procedure DoSome;',
  33572. 'begin',
  33573. ' DoIt(w);',
  33574. ' b:=w;',
  33575. ' b:=''2'';',
  33576. 'end;',
  33577. 'begin',
  33578. '{$R+}',
  33579. '']);
  33580. ConvertProgram;
  33581. CheckSource('TestRangeChecks_AssignChar',
  33582. LinesToStr([ // statements
  33583. 'this.b = "2";',
  33584. 'this.w = "3";',
  33585. 'this.DoIt = function (p) {',
  33586. ' rtl.rcc(p, 0, 65535);',
  33587. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33588. ' $mod.b = "1";',
  33589. '};',
  33590. 'this.DoSome = function () {',
  33591. ' $mod.DoIt($mod.w);',
  33592. ' $mod.b = $mod.w;',
  33593. ' $mod.b = "2";',
  33594. '};',
  33595. '']),
  33596. LinesToStr([ // $mod.$main
  33597. '']));
  33598. end;
  33599. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33600. begin
  33601. StartProgram(false);
  33602. Add([
  33603. '{$R+}',
  33604. 'type TDigit = ''0''..''9'';',
  33605. 'var',
  33606. ' b: TDigit = ''2'';',
  33607. ' w: TDigit = ''3'';',
  33608. 'procedure DoIt(p: TDigit);',
  33609. 'begin',
  33610. ' b:=w;',
  33611. ' b:=''1'';',
  33612. 'end;',
  33613. '{$R-}',
  33614. 'procedure DoSome;',
  33615. 'begin',
  33616. ' DoIt(w);',
  33617. ' b:=w;',
  33618. ' b:=''2'';',
  33619. 'end;',
  33620. 'begin',
  33621. '{$R+}',
  33622. '']);
  33623. ConvertProgram;
  33624. CheckSource('TestRangeChecks_AssignCharRange',
  33625. LinesToStr([ // statements
  33626. 'this.b = "2";',
  33627. 'this.w = "3";',
  33628. 'this.DoIt = function (p) {',
  33629. ' rtl.rcc(p, 48, 57);',
  33630. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33631. ' $mod.b = "1";',
  33632. '};',
  33633. 'this.DoSome = function () {',
  33634. ' $mod.DoIt($mod.w);',
  33635. ' $mod.b = $mod.w;',
  33636. ' $mod.b = "2";',
  33637. '};',
  33638. '']),
  33639. LinesToStr([ // $mod.$main
  33640. '']));
  33641. end;
  33642. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33643. begin
  33644. StartProgram(false);
  33645. Add([
  33646. '{$R+}',
  33647. 'type',
  33648. ' Ten = 1..10;',
  33649. ' TArr = array of Ten;',
  33650. ' TArrArr = array of TArr;',
  33651. ' TArrByte = array[byte] of Ten;',
  33652. ' TArrChar = array[''0''..''9''] of Ten;',
  33653. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33654. ' TObject = class',
  33655. ' A: TArr;',
  33656. ' end;',
  33657. 'procedure DoIt;',
  33658. 'var',
  33659. ' Arr: TArr;',
  33660. ' ArrArr: TArrArr;',
  33661. ' ArrByte: TArrByte;',
  33662. ' ArrChar: TArrChar;',
  33663. ' ArrByteChar: TArrByteChar;',
  33664. ' i: Ten;',
  33665. ' c: char;',
  33666. ' o: tobject;',
  33667. 'begin',
  33668. ' i:=Arr[1];',
  33669. ' i:=ArrByteChar[1,''2''];',
  33670. ' Arr[1]:=Arr[1];',
  33671. ' Arr[i]:=Arr[i];',
  33672. ' ArrByte[3]:=ArrByte[3];',
  33673. ' ArrByte[i]:=ArrByte[i];',
  33674. ' ArrChar[''5'']:=ArrChar[''5''];',
  33675. ' ArrChar[c]:=ArrChar[c];',
  33676. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  33677. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  33678. ' o.a[i]:=o.a[i];',
  33679. 'end;',
  33680. 'begin',
  33681. '']);
  33682. ConvertProgram;
  33683. CheckSource('TestRangeChecks_ArrayIndex',
  33684. LinesToStr([ // statements
  33685. 'this.TArrByteChar$clone = function (a) {',
  33686. ' var b = [];',
  33687. ' b.length = 256;',
  33688. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  33689. ' return b;',
  33690. '};',
  33691. 'rtl.createClass(this, "TObject", null, function () {',
  33692. ' this.$init = function () {',
  33693. ' this.A = [];',
  33694. ' };',
  33695. ' this.$final = function () {',
  33696. ' this.A = undefined;',
  33697. ' };',
  33698. '});',
  33699. 'this.DoIt = function () {',
  33700. ' var Arr = [];',
  33701. ' var ArrArr = [];',
  33702. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  33703. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  33704. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  33705. ' var i = 0;',
  33706. ' var c = "";',
  33707. ' var o = null;',
  33708. ' i = rtl.rc(Arr[1], 1, 10);',
  33709. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  33710. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  33711. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  33712. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  33713. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  33714. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  33715. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  33716. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  33717. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  33718. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  33719. '};',
  33720. '']),
  33721. LinesToStr([ // $mod.$main
  33722. '']));
  33723. end;
  33724. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  33725. begin
  33726. StartProgram(false);
  33727. Add([
  33728. '{$R+}',
  33729. 'type',
  33730. ' Ten = 1..10;',
  33731. ' TRec = record x: Ten end;',
  33732. ' TArr = array of TRec;',
  33733. ' TArrArr = array of TArr;',
  33734. ' TObject = class',
  33735. ' A: TArr;',
  33736. ' end;',
  33737. 'procedure DoIt;',
  33738. 'var',
  33739. ' Arr: TArr;',
  33740. ' ArrArr: TArrArr;',
  33741. ' i: Ten;',
  33742. ' o: tobject;',
  33743. 'begin',
  33744. ' Arr[1]:=Arr[1];',
  33745. ' Arr[i]:=Arr[i+1];',
  33746. ' o.a[i]:=o.a[i+2];',
  33747. 'end;',
  33748. 'begin',
  33749. '']);
  33750. ConvertProgram;
  33751. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  33752. LinesToStr([ // statements
  33753. 'rtl.recNewT(this, "TRec", function () {',
  33754. ' this.x = 0;',
  33755. ' this.$eq = function (b) {',
  33756. ' return this.x === b.x;',
  33757. ' };',
  33758. ' this.$assign = function (s) {',
  33759. ' this.x = s.x;',
  33760. ' return this;',
  33761. ' };',
  33762. '});',
  33763. 'rtl.createClass(this, "TObject", null, function () {',
  33764. ' this.$init = function () {',
  33765. ' this.A = [];',
  33766. ' };',
  33767. ' this.$final = function () {',
  33768. ' this.A = undefined;',
  33769. ' };',
  33770. '});',
  33771. 'this.DoIt = function () {',
  33772. ' var Arr = [];',
  33773. ' var ArrArr = [];',
  33774. ' var i = 0;',
  33775. ' var o = null;',
  33776. ' Arr[1].$assign(Arr[1]);',
  33777. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  33778. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  33779. '};',
  33780. '']),
  33781. LinesToStr([ // $mod.$main
  33782. '']));
  33783. end;
  33784. procedure TTestModule.TestRangeChecks_StringIndex;
  33785. begin
  33786. StartProgram(false);
  33787. Add([
  33788. 'type',
  33789. ' TObject = class',
  33790. ' S: string;',
  33791. ' end;',
  33792. '{$R+}',
  33793. 'procedure DoIt(var h: string);',
  33794. 'var',
  33795. ' s: string;',
  33796. ' i: longint;',
  33797. ' c: char;',
  33798. ' o: tobject;',
  33799. 'begin',
  33800. ' c:=s[1];',
  33801. ' s[i]:=s[i];',
  33802. ' h[i]:=h[i];',
  33803. ' c:=o.s[i];',
  33804. ' o.s[i]:=c;',
  33805. 'end;',
  33806. 'begin',
  33807. '']);
  33808. ConvertProgram;
  33809. CheckSource('TestRangeChecks_StringIndex',
  33810. LinesToStr([ // statements
  33811. 'rtl.createClass(this, "TObject", null, function () {',
  33812. ' this.$init = function () {',
  33813. ' this.S = "";',
  33814. ' };',
  33815. ' this.$final = function () {',
  33816. ' };',
  33817. '});',
  33818. 'this.DoIt = function (h) {',
  33819. ' var s = "";',
  33820. ' var i = 0;',
  33821. ' var c = "";',
  33822. ' var o = null;',
  33823. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33824. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33825. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33826. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33827. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33828. '};',
  33829. '']),
  33830. LinesToStr([ // $mod.$main
  33831. '']));
  33832. end;
  33833. procedure TTestModule.TestRangeChecks_TypecastInt;
  33834. begin
  33835. StartProgram(false);
  33836. Add([
  33837. '{$R+}',
  33838. 'var',
  33839. ' i: nativeint;',
  33840. ' b: byte;',
  33841. ' sh: shortint;',
  33842. ' w: word;',
  33843. ' sm: smallint;',
  33844. ' lw: longword;',
  33845. ' li: longint;',
  33846. 'begin',
  33847. ' b:=12+byte(i);',
  33848. ' sh:=12+shortint(i);',
  33849. ' w:=12+word(i);',
  33850. ' sm:=12+smallint(i);',
  33851. ' lw:=12+longword(i);',
  33852. ' li:=12+longint(i);',
  33853. '']);
  33854. ConvertProgram;
  33855. CheckSource('TestRangeChecks_TypecastInt',
  33856. LinesToStr([
  33857. 'this.i = 0;',
  33858. 'this.b = 0;',
  33859. 'this.sh = 0;',
  33860. 'this.w = 0;',
  33861. 'this.sm = 0;',
  33862. 'this.lw = 0;',
  33863. 'this.li = 0;',
  33864. '']),
  33865. LinesToStr([
  33866. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33867. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33868. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33869. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33870. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33871. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33872. '']));
  33873. end;
  33874. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33875. begin
  33876. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33877. StartProgram(false);
  33878. Add([
  33879. '{$modeswitch typehelpers}',
  33880. '{$R+}',
  33881. 'type',
  33882. ' TObject = class',
  33883. ' FSize: byte;',
  33884. ' property Size: byte read FSize;',
  33885. ' end;',
  33886. ' THelper = type helper for byte',
  33887. ' procedure SetIt(w: word);',
  33888. ' end;',
  33889. 'procedure THelper.SetIt(w: word);',
  33890. 'begin',
  33891. ' Self:=w;',
  33892. 'end;',
  33893. 'function GetIt: byte;',
  33894. 'begin',
  33895. ' Result.SetIt(2);',
  33896. 'end;',
  33897. 'var',
  33898. ' b: byte = 3;',
  33899. ' o: TObject;',
  33900. 'begin',
  33901. ' b.SetIt(14);',
  33902. ' with b do SetIt(15);',
  33903. ' o.Size.SetIt(16);',
  33904. '']);
  33905. ConvertProgram;
  33906. CheckSource('TestRangeChecks_AssignInt',
  33907. LinesToStr([ // statements
  33908. 'rtl.createClass(this, "TObject", null, function () {',
  33909. ' this.$init = function () {',
  33910. ' this.FSize = 0;',
  33911. ' };',
  33912. ' this.$final = function () {',
  33913. ' };',
  33914. '});',
  33915. 'rtl.createHelper(this, "THelper", null, function () {',
  33916. ' this.SetIt = function (w) {',
  33917. ' rtl.rc(w, 0, 65535);',
  33918. ' this.set(w);',
  33919. ' };',
  33920. '});',
  33921. 'this.GetIt = function () {',
  33922. ' var Result = 0;',
  33923. ' $mod.THelper.SetIt.call({',
  33924. ' get: function () {',
  33925. ' return Result;',
  33926. ' },',
  33927. ' set: function (v) {',
  33928. ' rtl.rc(v, 0, 255);',
  33929. ' Result = v;',
  33930. ' }',
  33931. ' }, 2);',
  33932. ' return Result;',
  33933. '};',
  33934. 'this.b = 3;',
  33935. 'this.o = null;',
  33936. '']),
  33937. LinesToStr([ // $mod.$main
  33938. '$mod.THelper.SetIt.call({',
  33939. ' p: $mod,',
  33940. ' get: function () {',
  33941. ' return this.p.b;',
  33942. ' },',
  33943. ' set: function (v) {',
  33944. ' rtl.rc(v, 0, 255);',
  33945. ' this.p.b = v;',
  33946. ' }',
  33947. '}, 14);',
  33948. 'var $with = $mod.b;',
  33949. '$mod.THelper.SetIt.call({',
  33950. ' get: function () {',
  33951. ' return $with;',
  33952. ' },',
  33953. ' set: function (v) {',
  33954. ' rtl.rc(v, 0, 255);',
  33955. ' $with = v;',
  33956. ' }',
  33957. '}, 15);',
  33958. '$mod.THelper.SetIt.call({',
  33959. ' p: $mod.o,',
  33960. ' get: function () {',
  33961. ' return this.p.FSize;',
  33962. ' },',
  33963. ' set: function (v) {',
  33964. ' rtl.rc(v, 0, 255);',
  33965. ' this.p.FSize = v;',
  33966. ' }',
  33967. '}, 16);',
  33968. '']));
  33969. end;
  33970. procedure TTestModule.TestAsync_Proc;
  33971. begin
  33972. StartProgram(false);
  33973. Add([
  33974. 'procedure Fly(w: word = 1); async; forward;',
  33975. 'procedure Run(w: word = 2); async;',
  33976. 'begin',
  33977. ' Fly(w);',
  33978. ' Fly;',
  33979. ' await(Fly(w));',
  33980. ' await(Fly);',
  33981. 'end;',
  33982. 'procedure Fly(w: word); ',
  33983. 'begin',
  33984. 'end;',
  33985. 'begin',
  33986. ' Run;',
  33987. ' Run(3);',
  33988. '']);
  33989. CheckResolverUnexpectedHints();
  33990. ConvertProgram;
  33991. CheckSource('TestAsync_Proc',
  33992. LinesToStr([ // statements
  33993. 'this.Run = async function (w) {',
  33994. ' $mod.Fly(w);',
  33995. ' $mod.Fly(1);',
  33996. ' await $mod.Fly(w);',
  33997. ' await $mod.Fly(1);',
  33998. '};',
  33999. 'this.Fly = async function (w) {',
  34000. '};',
  34001. '']),
  34002. LinesToStr([
  34003. '$mod.Run(2);',
  34004. '$mod.Run(3);',
  34005. '']));
  34006. end;
  34007. procedure TTestModule.TestAsync_CallResultIsPromise;
  34008. begin
  34009. StartProgram(false);
  34010. Add([
  34011. '{$modeswitch externalclass}',
  34012. 'type',
  34013. ' TObject = class',
  34014. ' end;',
  34015. ' TJSPromise = class external name ''Promise''',
  34016. ' end;',
  34017. ' TBird = class',
  34018. ' function Fly: word; async; ',
  34019. ' end;',
  34020. 'function TBird.Fly: word; async; ',
  34021. 'begin',
  34022. ' Result:=3;',
  34023. ' Fly:=4+Result;',
  34024. ' if Result=5 then ;',
  34025. ' exit(6);',
  34026. 'end;',
  34027. 'function Run: word; async;',
  34028. 'begin',
  34029. ' Result:=11+Result;',
  34030. ' inc(Result);',
  34031. 'end;',
  34032. 'var',
  34033. ' p: TJSPromise;',
  34034. ' o: TBird;',
  34035. 'begin',
  34036. ' p:=Run;',
  34037. ' p:=Run();',
  34038. ' if Run=p then ;',
  34039. ' if p=Run then ;',
  34040. ' if Run()=p then ;',
  34041. ' if p=Run() then ;',
  34042. ' p:=o.Fly;',
  34043. ' p:=o.Fly();',
  34044. ' if o.Fly=p then ;',
  34045. ' if o.Fly()=p then ;',
  34046. ' with o do begin',
  34047. ' p:=Fly;',
  34048. ' p:=Fly();',
  34049. ' if Fly=p then ;',
  34050. ' if Fly()=p then ;',
  34051. ' end;',
  34052. '']);
  34053. CheckResolverUnexpectedHints();
  34054. ConvertProgram;
  34055. CheckSource('TestAsync_CallResultIsPromise',
  34056. LinesToStr([ // statements
  34057. 'rtl.createClass(this, "TObject", null, function () {',
  34058. ' this.$init = function () {',
  34059. ' };',
  34060. ' this.$final = function () {',
  34061. ' };',
  34062. '});',
  34063. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34064. ' this.Fly = async function () {',
  34065. ' var Result = 0;',
  34066. ' Result = 3;',
  34067. ' Result = 4 + Result;',
  34068. ' if (Result === 5) ;',
  34069. ' return 6;',
  34070. ' return Result;',
  34071. ' };',
  34072. '});',
  34073. 'this.Run = async function () {',
  34074. ' var Result = 0;',
  34075. ' Result = 11 + Result;',
  34076. ' Result += 1;',
  34077. ' return Result;',
  34078. '};',
  34079. 'this.p = null;',
  34080. 'this.o = null;',
  34081. '']),
  34082. LinesToStr([
  34083. '$mod.p = $mod.Run();',
  34084. '$mod.p = $mod.Run();',
  34085. 'if ($mod.Run() === $mod.p) ;',
  34086. 'if ($mod.p === $mod.Run()) ;',
  34087. 'if ($mod.Run() === $mod.p) ;',
  34088. 'if ($mod.p === $mod.Run()) ;',
  34089. '$mod.p = $mod.o.Fly();',
  34090. '$mod.p = $mod.o.Fly();',
  34091. 'if ($mod.o.Fly() === $mod.p) ;',
  34092. 'if ($mod.o.Fly() === $mod.p) ;',
  34093. 'var $with = $mod.o;',
  34094. '$mod.p = $with.Fly();',
  34095. '$mod.p = $with.Fly();',
  34096. 'if ($with.Fly() === $mod.p) ;',
  34097. 'if ($with.Fly() === $mod.p) ;',
  34098. '']));
  34099. end;
  34100. procedure TTestModule.TestAsync_ConstructorFail;
  34101. begin
  34102. StartProgram(false);
  34103. Add([
  34104. 'type',
  34105. ' TObject = class',
  34106. ' end;',
  34107. ' TBird = class',
  34108. ' constructor Create; async;',
  34109. ' end;',
  34110. 'constructor TBird.Create; async;',
  34111. 'begin',
  34112. 'end;',
  34113. 'begin',
  34114. '']);
  34115. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  34116. ConvertProgram;
  34117. end;
  34118. procedure TTestModule.TestAsync_PropertyGetterFail;
  34119. begin
  34120. StartProgram(false);
  34121. Add([
  34122. 'type',
  34123. ' TObject = class',
  34124. ' end;',
  34125. ' TBird = class',
  34126. ' function GetSize: word; async;',
  34127. ' property Size: word read GetSize;',
  34128. ' end;',
  34129. 'function TBird.GetSize: word; async;',
  34130. 'begin',
  34131. 'end;',
  34132. 'begin',
  34133. '']);
  34134. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  34135. ConvertProgram;
  34136. end;
  34137. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  34138. begin
  34139. StartProgram(false);
  34140. Add([
  34141. 'procedure Run; async;',
  34142. 'begin',
  34143. ' await(word,1);',
  34144. 'end;',
  34145. 'begin',
  34146. '']);
  34147. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  34148. ConvertProgram;
  34149. end;
  34150. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  34151. begin
  34152. StartProgram(false);
  34153. Add([
  34154. 'type',
  34155. ' TObject = class',
  34156. ' end;',
  34157. ' TBird = class',
  34158. ' end;',
  34159. 'function Fly: TObject; async;',
  34160. 'begin',
  34161. 'end;',
  34162. 'procedure Run; async;',
  34163. 'begin',
  34164. ' await(TBird,Fly);',
  34165. 'end;',
  34166. 'begin',
  34167. '']);
  34168. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  34169. ConvertProgram;
  34170. end;
  34171. procedure TTestModule.TestAWait_OutsideAsyncFail;
  34172. begin
  34173. StartProgram(false);
  34174. Add([
  34175. 'procedure Crawl(w: double); ',
  34176. 'begin',
  34177. 'end;',
  34178. 'procedure Run(w: double);',
  34179. 'begin',
  34180. ' await(Crawl(w));',
  34181. 'end;',
  34182. 'begin',
  34183. ' Run(1);']);
  34184. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  34185. ConvertProgram;
  34186. end;
  34187. procedure TTestModule.TestAWait_IntegerFail;
  34188. begin
  34189. StartProgram(false);
  34190. Add([
  34191. 'function Run: word;',
  34192. 'begin',
  34193. 'end;',
  34194. 'procedure Fly(w: word); async;',
  34195. 'begin',
  34196. ' await(Run());',
  34197. 'end;',
  34198. 'begin',
  34199. ' Fly(1);']);
  34200. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  34201. ConvertProgram;
  34202. end;
  34203. procedure TTestModule.TestAWait_ExternalClassPromise;
  34204. begin
  34205. StartProgram(false);
  34206. Add([
  34207. '{$modeswitch externalclass}',
  34208. 'type',
  34209. ' TJSPromise = class external name ''Promise''',
  34210. ' end;',
  34211. ' TJSThenable = class external name ''Thenable''',
  34212. ' end;',
  34213. 'function Fly(w: word): TJSPromise;',
  34214. 'begin',
  34215. 'end;',
  34216. 'function Jump(w: word): word; async;',
  34217. 'begin',
  34218. 'end;',
  34219. 'function Eat(w: word): TJSPromise; async;',
  34220. 'begin',
  34221. 'end;',
  34222. 'function Run(d: double): word; async;',
  34223. 'var',
  34224. ' p: TJSPromise;',
  34225. 'begin',
  34226. ' Result:=await(word,p);', // promise needs type
  34227. ' Result:=await(word,Fly(3));', // promise needs type
  34228. ' Result:=await(Jump(4));', // async non promise must omit the type
  34229. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  34230. ' Result:=await(word,Eat(6));', // promise needs type
  34231. 'end;',
  34232. 'begin',
  34233. '']);
  34234. ConvertProgram;
  34235. CheckSource('TestAWait_ExternalClassPromise',
  34236. LinesToStr([ // statements
  34237. 'this.Fly = function (w) {',
  34238. ' var Result = null;',
  34239. ' return Result;',
  34240. '};',
  34241. 'this.Jump = async function (w) {',
  34242. ' var Result = 0;',
  34243. ' return Result;',
  34244. '};',
  34245. 'this.Eat = async function (w) {',
  34246. ' var Result = null;',
  34247. ' return Result;',
  34248. '};',
  34249. 'this.Run = async function (d) {',
  34250. ' var Result = 0;',
  34251. ' var p = null;',
  34252. ' Result = await p;',
  34253. ' Result = await $mod.Fly(3);',
  34254. ' Result = await $mod.Jump(4);',
  34255. ' Result = await $mod.Jump(5);',
  34256. ' Result = await $mod.Eat(6);',
  34257. ' return Result;',
  34258. '};',
  34259. '']),
  34260. LinesToStr([
  34261. ]));
  34262. CheckResolverUnexpectedHints();
  34263. end;
  34264. procedure TTestModule.TestAWait_JSValue;
  34265. begin
  34266. StartProgram(false);
  34267. Add([
  34268. '{$modeswitch externalclass}',
  34269. 'type',
  34270. ' TJSPromise = class external name ''Promise''',
  34271. ' end;',
  34272. 'function Fly(w: word): jsvalue; async;',
  34273. 'begin',
  34274. 'end;',
  34275. 'function Run(d: jsvalue; var e): word; async;',
  34276. 'begin',
  34277. ' Result:=await(word,d);', // promise needs type
  34278. ' d:=await(Fly(4));', // async non promise must omit the type
  34279. ' Result:=await(word,e);', // promise needs type
  34280. 'end;',
  34281. 'begin',
  34282. '']);
  34283. ConvertProgram;
  34284. CheckSource('TestAWait_JSValue',
  34285. LinesToStr([ // statements
  34286. 'this.Fly = async function (w) {',
  34287. ' var Result = undefined;',
  34288. ' return Result;',
  34289. '};',
  34290. 'this.Run = async function (d, e) {',
  34291. ' var Result = 0;',
  34292. ' Result = await d;',
  34293. ' d = await $mod.Fly(4);',
  34294. ' Result = await e.get();',
  34295. ' return Result;',
  34296. '};',
  34297. '']),
  34298. LinesToStr([
  34299. ]));
  34300. CheckResolverUnexpectedHints();
  34301. end;
  34302. procedure TTestModule.TestAWait_Result;
  34303. begin
  34304. StartProgram(false);
  34305. Add([
  34306. '{$modeswitch externalclass}',
  34307. 'type',
  34308. ' TJSPromise = class external name ''Promise''',
  34309. ' end;',
  34310. 'function Crawl(d: double = 1.3): TJSPromise; ',
  34311. 'begin',
  34312. 'end;',
  34313. 'function Run(d: double = 1.6): word; async;',
  34314. 'begin',
  34315. ' Result:=await(word,Crawl);',
  34316. ' Result:=await(word,Crawl(4.5));',
  34317. ' Result:=await(Run);',
  34318. ' Result:=await(Run(6.7));',
  34319. 'end;',
  34320. 'begin',
  34321. ' Run(1);']);
  34322. ConvertProgram;
  34323. CheckSource('TestAWait_Result',
  34324. LinesToStr([ // statements
  34325. 'this.Crawl = function (d) {',
  34326. ' var Result = null;',
  34327. ' return Result;',
  34328. '};',
  34329. 'this.Run = async function (d) {',
  34330. ' var Result = 0;',
  34331. ' Result = await $mod.Crawl(1.3);',
  34332. ' Result = await $mod.Crawl(4.5);',
  34333. ' Result = await $mod.Run(1.6);',
  34334. ' Result = await $mod.Run(6.7);',
  34335. ' return Result;',
  34336. '};',
  34337. '']),
  34338. LinesToStr([
  34339. '$mod.Run(1);'
  34340. ]));
  34341. CheckResolverUnexpectedHints();
  34342. end;
  34343. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  34344. begin
  34345. StartProgram(false);
  34346. Add([
  34347. '{$mode objfpc}',
  34348. '{$modeswitch externalclass}',
  34349. 'type',
  34350. ' TJSPromise = class external name ''Promise''',
  34351. ' end;',
  34352. 'function Run: TJSPromise; async;',
  34353. 'begin',
  34354. 'end;',
  34355. 'procedure Fly(w: word); async;',
  34356. 'begin',
  34357. ' await(Run());',
  34358. 'end;',
  34359. 'begin',
  34360. ' Fly(1);']);
  34361. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  34362. nWrongNumberOfParametersForCallTo);
  34363. ConvertProgram;
  34364. end;
  34365. procedure TTestModule.TestAsync_AnonymousProc;
  34366. begin
  34367. StartProgram(false);
  34368. Add([
  34369. '{$mode objfpc}',
  34370. '{$modeswitch externalclass}',
  34371. 'type',
  34372. ' TJSPromise = class external name ''Promise''',
  34373. ' end;',
  34374. 'type',
  34375. ' TFunc = reference to function(x: double): word; async;',
  34376. 'function Crawl(d: double = 1.3): word; async;',
  34377. 'begin',
  34378. 'end;',
  34379. 'var Func: TFunc;',
  34380. 'begin',
  34381. ' Func:=function(c:double):word async begin',
  34382. ' Result:=await(Crawl(c));',
  34383. ' end;',
  34384. ' Func:=function(c:double):word async assembler asm',
  34385. ' end;',
  34386. '']);
  34387. ConvertProgram;
  34388. CheckSource('TestAsync_AnonymousProc',
  34389. LinesToStr([ // statements
  34390. 'this.Crawl = async function (d) {',
  34391. ' var Result = 0;',
  34392. ' return Result;',
  34393. '};',
  34394. 'this.Func = null;',
  34395. '']),
  34396. LinesToStr([
  34397. '$mod.Func = async function (c) {',
  34398. ' var Result = 0;',
  34399. ' Result = await $mod.Crawl(c);',
  34400. ' return Result;',
  34401. '};',
  34402. '$mod.Func = async function (c) {',
  34403. '};',
  34404. '']));
  34405. CheckResolverUnexpectedHints();
  34406. end;
  34407. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34408. begin
  34409. StartProgram(false);
  34410. Add([
  34411. '{$mode objfpc}',
  34412. '{$modeswitch externalclass}',
  34413. 'type',
  34414. ' TJSPromise = class external name ''Promise''',
  34415. ' end;',
  34416. ' TObject = class',
  34417. ' public',
  34418. ' procedure Fly(Prom: TJSPromise);',
  34419. ' end;',
  34420. ' TFunc = reference to procedure(Bird: TObject);',
  34421. 'procedure TObject.Fly(Prom: TJSPromise);',
  34422. 'begin',
  34423. 'end;',
  34424. 'function Crawl: jsvalue; async;',
  34425. 'begin',
  34426. 'end;',
  34427. 'procedure Add(Func: TFunc);',
  34428. 'begin',
  34429. 'end;',
  34430. 'begin',
  34431. ' Add(procedure(Bird: TObject)',
  34432. ' begin',
  34433. ' Bird.Fly(Crawl());',
  34434. ' end);',
  34435. '']);
  34436. ConvertProgram;
  34437. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34438. LinesToStr([ // statements
  34439. 'rtl.createClass(this, "TObject", null, function () {',
  34440. ' this.$init = function () {',
  34441. ' };',
  34442. ' this.$final = function () {',
  34443. ' };',
  34444. ' this.Fly = function (Prom) {',
  34445. ' };',
  34446. '});',
  34447. 'this.Crawl = async function () {',
  34448. ' var Result = undefined;',
  34449. ' return Result;',
  34450. '};',
  34451. 'this.Add = function (Func) {',
  34452. '};',
  34453. '']),
  34454. LinesToStr([
  34455. '$mod.Add(function (Bird) {',
  34456. ' Bird.Fly($mod.Crawl());',
  34457. '});',
  34458. '']));
  34459. end;
  34460. procedure TTestModule.TestAsync_ProcType;
  34461. begin
  34462. StartProgram(false);
  34463. Add([
  34464. '{$mode objfpc}',
  34465. 'type',
  34466. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34467. ' TFunc = function(x: double = 1.1): word; async;',
  34468. ' TProc = procedure(x: longint = 7); async;',
  34469. 'function Crawl(d: double): word; async;',
  34470. 'begin',
  34471. 'end;',
  34472. 'procedure Run(e:longint); async;',
  34473. 'begin',
  34474. 'end;',
  34475. 'procedure Fly(p: TProc); async;',
  34476. 'begin',
  34477. ' await(p);',
  34478. ' await(p());',
  34479. 'end;',
  34480. 'var',
  34481. ' RefFunc: TRefFunc;',
  34482. ' Func: TFunc;',
  34483. ' Proc, ProcB: TProc;',
  34484. 'begin',
  34485. ' Func:=@Crawl;',
  34486. ' RefFunc:=@Crawl;',
  34487. ' RefFunc:=function(c:double):word async begin',
  34488. ' Result:=await(RefFunc);',
  34489. ' Result:=await(RefFunc());',
  34490. ' Result:=await(Func);',
  34491. ' Result:=await(Func());',
  34492. ' await(Proc);',
  34493. ' await(Proc());',
  34494. ' await(Proc(13));',
  34495. ' end;',
  34496. ' Proc:=@Run;',
  34497. ' if Proc=ProcB then ;',
  34498. ' ']);
  34499. ConvertProgram;
  34500. CheckResolverUnexpectedHints();
  34501. CheckSource('TestAsync_ProcType',
  34502. LinesToStr([ // statements
  34503. 'this.Crawl = async function (d) {',
  34504. ' var Result = 0;',
  34505. ' return Result;',
  34506. '};',
  34507. 'this.Run = async function (e) {',
  34508. '};',
  34509. 'this.Fly = async function (p) {',
  34510. ' await p(7);',
  34511. ' await p(7);',
  34512. '};',
  34513. 'this.RefFunc = null;',
  34514. 'this.Func = null;',
  34515. 'this.Proc = null;',
  34516. 'this.ProcB = null;',
  34517. '']),
  34518. LinesToStr([
  34519. '$mod.Func = $mod.Crawl;',
  34520. '$mod.RefFunc = $mod.Crawl;',
  34521. '$mod.RefFunc = async function (c) {',
  34522. ' var Result = 0;',
  34523. ' Result = await $mod.RefFunc(1.3);',
  34524. ' Result = await $mod.RefFunc(1.3);',
  34525. ' Result = await $mod.Func(1.1);',
  34526. ' Result = await $mod.Func(1.1);',
  34527. ' await $mod.Proc(7);',
  34528. ' await $mod.Proc(7);',
  34529. ' await $mod.Proc(13);',
  34530. ' return Result;',
  34531. '};',
  34532. '$mod.Proc = $mod.Run;',
  34533. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34534. '']));
  34535. end;
  34536. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34537. begin
  34538. StartProgram(false);
  34539. Add([
  34540. '{$mode objfpc}',
  34541. 'type',
  34542. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34543. 'function Crawl(d: double): word; async;',
  34544. 'begin',
  34545. 'end;',
  34546. 'var',
  34547. ' RefFunc: TRefFunc;',
  34548. 'begin',
  34549. ' RefFunc:=@Crawl;',
  34550. ' ']);
  34551. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34552. ConvertProgram;
  34553. end;
  34554. procedure TTestModule.TestAsync_Inherited;
  34555. begin
  34556. StartProgram(false);
  34557. Add([
  34558. '{$mode objfpc}',
  34559. '{$modeswitch externalclass}',
  34560. 'type',
  34561. ' TJSPromise = class external name ''Promise''',
  34562. ' end;',
  34563. ' TObject = class',
  34564. ' function Run(w: word = 3): word; async; virtual;',
  34565. ' end;',
  34566. ' TBird = class',
  34567. ' function Run(w: word = 3): word; async; override;',
  34568. ' end;',
  34569. 'function TObject.Run(w: word = 3): word; async;',
  34570. 'begin',
  34571. 'end;',
  34572. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34573. 'var p: TJSPromise;',
  34574. 'begin',
  34575. ' p:=inherited;',
  34576. ' p:=inherited Run;',
  34577. ' p:=inherited Run();',
  34578. ' p:=inherited Run(4);',
  34579. ' exit(p);',
  34580. ' exit(inherited);',
  34581. ' exit(inherited Run);',
  34582. ' exit(inherited Run(5));',
  34583. ' exit(6);',
  34584. 'end;',
  34585. 'begin',
  34586. ' ']);
  34587. ConvertProgram;
  34588. CheckSource('TestAsync_Inherited',
  34589. LinesToStr([ // statements
  34590. 'rtl.createClass(this, "TObject", null, function () {',
  34591. ' this.$init = function () {',
  34592. ' };',
  34593. ' this.$final = function () {',
  34594. ' };',
  34595. ' this.Run = async function (w) {',
  34596. ' var Result = 0;',
  34597. ' return Result;',
  34598. ' };',
  34599. '});',
  34600. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34601. ' this.Run = async function (w) {',
  34602. ' var Result = 0;',
  34603. ' var p = null;',
  34604. ' p = $mod.TObject.Run.apply(this, arguments);',
  34605. ' p = $mod.TObject.Run.call(this, 3);',
  34606. ' p = $mod.TObject.Run.call(this, 3);',
  34607. ' p = $mod.TObject.Run.call(this, 4);',
  34608. ' return p;',
  34609. ' return $mod.TObject.Run.apply(this, arguments);',
  34610. ' return $mod.TObject.Run.call(this, 3);',
  34611. ' return $mod.TObject.Run.call(this, 5);',
  34612. ' return 6;',
  34613. ' return Result;',
  34614. ' };',
  34615. '});',
  34616. '']),
  34617. LinesToStr([
  34618. '']));
  34619. CheckResolverUnexpectedHints();
  34620. end;
  34621. procedure TTestModule.TestAsync_ClassInterface;
  34622. begin
  34623. StartProgram(false);
  34624. Add([
  34625. '{$mode objfpc}',
  34626. '{$modeswitch externalclass}',
  34627. 'type',
  34628. ' TJSPromise = class external name ''Promise''',
  34629. ' end;',
  34630. ' IUnknown = interface',
  34631. ' function _AddRef: longint;',
  34632. ' function _Release: longint;',
  34633. ' end;',
  34634. 'function Say(i: IUnknown): IUnknown; async;',
  34635. 'begin',
  34636. 'end;',
  34637. 'function Run: IUnknown; async;',
  34638. 'begin',
  34639. ' Result:=await(Run);',
  34640. ' Result:=await(Run());',
  34641. ' Result:=await(Run) as IUnknown;',
  34642. ' Result:=await(Say(nil));',
  34643. ' Result:=await(Say(await(Run())));',
  34644. ' Result:=await(Say(await(Run()) as IUnknown));',
  34645. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  34646. 'end;',
  34647. 'procedure Fly;',
  34648. 'var p: TJSPromise;',
  34649. 'begin',
  34650. ' Run;',
  34651. ' Run();',
  34652. ' p:=Run;',
  34653. ' p:=Run();',
  34654. 'end;',
  34655. 'begin',
  34656. ' ']);
  34657. ConvertProgram;
  34658. CheckSource('TestAsync_ClassInterface',
  34659. LinesToStr([ // statements
  34660. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  34661. 'this.Say = async function (i) {',
  34662. ' var Result = null;',
  34663. ' return Result;',
  34664. '};',
  34665. 'this.Run = async function () {',
  34666. ' var Result = null;',
  34667. ' var $ok = false;',
  34668. ' try {',
  34669. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34670. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34671. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  34672. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  34673. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  34674. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  34675. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  34676. ' $ok = true;',
  34677. ' } finally {',
  34678. ' if (!$ok) rtl._Release(Result);',
  34679. ' };',
  34680. ' return Result;',
  34681. '};',
  34682. 'this.Fly = function () {',
  34683. ' var p = null;',
  34684. ' $mod.Run();',
  34685. ' $mod.Run();',
  34686. ' p = $mod.Run();',
  34687. ' p = $mod.Run();',
  34688. '};',
  34689. '']),
  34690. LinesToStr([
  34691. '']));
  34692. CheckResolverUnexpectedHints();
  34693. end;
  34694. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  34695. begin
  34696. StartProgram(true,[supTInterfacedObject]);
  34697. Add([
  34698. '{$mode objfpc}',
  34699. '{$modeswitch externalclass}',
  34700. 'type',
  34701. ' TJSPromise = class external name ''Promise''',
  34702. ' end;',
  34703. ' IBird = interface',
  34704. ' procedure Run;',
  34705. ' end;',
  34706. ' TBird = class(TInterfacedObject,IBird)',
  34707. ' procedure Run; async;',
  34708. ' end;',
  34709. 'procedure TBird.Run;',
  34710. 'begin',
  34711. 'end;',
  34712. 'begin',
  34713. ' ']);
  34714. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34715. ConvertProgram;
  34716. end;
  34717. procedure TTestModule.TestAWait_ClassAs;
  34718. begin
  34719. StartProgram(false);
  34720. Add([
  34721. '{$mode objfpc}',
  34722. '{$modeswitch externalclass}',
  34723. 'type',
  34724. ' TJSPromise = class external name ''Promise''',
  34725. ' end;',
  34726. ' TObject = class',
  34727. ' function Run: TObject; async;',
  34728. ' end;',
  34729. ' TBird = class',
  34730. ' function Fly: TBird; async;',
  34731. ' end;',
  34732. 'function TObject.Run: TObject; async;',
  34733. 'begin',
  34734. 'end;',
  34735. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  34736. 'var o: TObject;',
  34737. 'begin',
  34738. ' o:=await(TObject,Run);',
  34739. ' o:=await(TObject,Fly);',
  34740. ' o:=await(TBird,Fly);',
  34741. ' o:=await(TObject,inherited Run);',
  34742. ' o:=await(TObject,inherited Run) as TBird;',
  34743. 'end;',
  34744. 'begin',
  34745. ' ']);
  34746. ConvertProgram;
  34747. CheckSource('TestAWait_ClassAs',
  34748. LinesToStr([ // statements
  34749. 'rtl.createClass(this, "TObject", null, function () {',
  34750. ' this.$init = function () {',
  34751. ' };',
  34752. ' this.$final = function () {',
  34753. ' };',
  34754. ' this.Run = async function () {',
  34755. ' var Result = null;',
  34756. ' return Result;',
  34757. ' };',
  34758. '});',
  34759. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34760. ' this.Fly = async function () {',
  34761. ' var Result = null;',
  34762. ' var o = null;',
  34763. ' o = await this.Run();',
  34764. ' o = await this.Fly();',
  34765. ' o = await this.Fly();',
  34766. ' o = await $mod.TObject.Run.call(this);',
  34767. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  34768. ' return Result;',
  34769. ' };',
  34770. '});',
  34771. '']),
  34772. LinesToStr([
  34773. '']));
  34774. CheckResolverUnexpectedHints();
  34775. end;
  34776. procedure TTestModule.TestLibrary_Empty;
  34777. begin
  34778. StartLibrary(false);
  34779. Add([
  34780. '']);
  34781. ConvertLibrary;
  34782. CheckFullSource('TestLibrary_Empty',
  34783. LinesToStr([ // statements
  34784. 'rtl.module("library", [], function () {',
  34785. ' var $mod = this;',
  34786. ' $mod.$main = function () {',
  34787. ' };',
  34788. '});',
  34789. 'rtl.run("library");',
  34790. '']));
  34791. CheckResolverUnexpectedHints();
  34792. end;
  34793. procedure TTestModule.TestLibrary_ExportFunc;
  34794. begin
  34795. StartLibrary(false);
  34796. Add([
  34797. 'procedure Run(w: word);',
  34798. 'begin',
  34799. 'end;',
  34800. 'exports',
  34801. ' Run;',
  34802. ' run name ''Foo'';',
  34803. ' test1.run name ''Test1Run'';',
  34804. '']);
  34805. ConvertLibrary;
  34806. CheckFullSource('TestLibrary_ExportFunc',
  34807. LinesToStr([ // statements
  34808. 'rtl.module("library", [], function () {',
  34809. ' var $mod = this;',
  34810. ' this.Run = function (w) {',
  34811. ' };',
  34812. ' $mod.$main = function () {',
  34813. ' };',
  34814. '});',
  34815. 'rtl.run("library");',
  34816. 'export const Run = pas.library.Run;',
  34817. 'export const Foo = pas.library.Run;',
  34818. 'export const Test1Run = pas.library.Run;',
  34819. '']));
  34820. CheckResolverUnexpectedHints();
  34821. end;
  34822. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  34823. begin
  34824. StartLibrary(false);
  34825. Add([
  34826. 'procedure Run(w: word); overload;',
  34827. 'begin',
  34828. 'end;',
  34829. 'procedure Run(s: string); overload;',
  34830. 'begin',
  34831. 'end;',
  34832. 'exports',
  34833. ' Run;',
  34834. '']);
  34835. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  34836. nCantDetermineWhichOverloadedFunctionToCall);
  34837. ConvertLibrary;
  34838. end;
  34839. procedure TTestModule.TestLibrary_Export_Index_Fail;
  34840. begin
  34841. StartLibrary(false);
  34842. Add([
  34843. 'procedure Run(w: word);',
  34844. 'begin',
  34845. 'end;',
  34846. 'exports',
  34847. ' Run index 3;',
  34848. '']);
  34849. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  34850. ConvertLibrary;
  34851. end;
  34852. procedure TTestModule.TestLibrary_ExportVar;
  34853. begin
  34854. StartLibrary(false);
  34855. Add([
  34856. 'var Wing: word;',
  34857. 'exports',
  34858. ' Wing, wing name ''BirdArm'';',
  34859. '']);
  34860. ConvertLibrary;
  34861. CheckFullSource('TestLibrary_ExportVar',
  34862. LinesToStr([ // statements
  34863. 'rtl.module("library", [], function () {',
  34864. ' var $mod = this;',
  34865. ' this.Wing = 0;',
  34866. ' $mod.$main = function () {',
  34867. ' };',
  34868. '});',
  34869. 'rtl.run("library");',
  34870. 'export const vars = {};',
  34871. 'Object.defineProperties(vars, {',
  34872. ' Wing: {',
  34873. ' enumerable: true,',
  34874. ' get: function () {',
  34875. ' return pas.library.Wing;',
  34876. ' },',
  34877. ' set: function (v) {',
  34878. ' pas.library.Wing = v;',
  34879. ' }',
  34880. ' },',
  34881. ' BirdArm: {',
  34882. ' enumerable: true,',
  34883. ' get: function () {',
  34884. ' return pas.library.Wing;',
  34885. ' },',
  34886. ' set: function (v) {',
  34887. ' pas.library.Wing = v;',
  34888. ' }',
  34889. ' }',
  34890. '});',
  34891. '']));
  34892. CheckResolverUnexpectedHints();
  34893. end;
  34894. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34895. begin
  34896. AddModuleWithIntfImplSrc('Unit1.pas',
  34897. LinesToStr([
  34898. 'type',
  34899. ' TAnt = class',
  34900. ' class function Crawl: word; static;',
  34901. ' end;',
  34902. 'function Fly: word;',
  34903. '']),
  34904. LinesToStr([
  34905. 'function Fly: word;',
  34906. 'begin',
  34907. 'end;',
  34908. 'class function TAnt.Crawl: word;',
  34909. 'begin',
  34910. 'end;',
  34911. '']));
  34912. StartLibrary(true,[supTObject]);
  34913. Add([
  34914. 'uses unit1;',
  34915. 'exports',
  34916. ' Fly;',
  34917. ' TAnt.Crawl;',
  34918. '']);
  34919. ConvertLibrary;
  34920. CheckFullSource('TestLibrary_ExportUnitFunc',
  34921. LinesToStr([ // statements
  34922. 'rtl.module("library", ["system", "Unit1"], function () {',
  34923. ' var $mod = this;',
  34924. ' $mod.$main = function () {',
  34925. ' };',
  34926. '});',
  34927. 'rtl.run("library");',
  34928. 'export const Fly = pas.Unit1.Fly;',
  34929. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  34930. '']));
  34931. CheckResolverUnexpectedHints();
  34932. end;
  34933. Initialization
  34934. RegisterTests([TTestModule]);
  34935. end.