tcmodules.pas 942 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716
  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 TestInteger;
  283. Procedure TestIntegerRange;
  284. Procedure TestIntegerTypecasts;
  285. Procedure TestInteger_BitwiseShrNativeInt;
  286. Procedure TestInteger_BitwiseShlNativeInt;
  287. Procedure TestInteger_SystemFunc;
  288. Procedure TestInteger_AssignOutsideConst;
  289. Procedure TestCurrency;
  290. Procedure TestForBoolDo;
  291. Procedure TestForIntDo;
  292. Procedure TestForIntInDo;
  293. // strings
  294. Procedure TestCharConst;
  295. Procedure TestChar_Compare;
  296. Procedure TestChar_BuiltInProcs;
  297. Procedure TestStringConst;
  298. Procedure TestStringConst_InvalidUTF16;
  299. Procedure TestStringConstSurrogate;
  300. Procedure TestStringConst_Multiline;
  301. Procedure TestString_Length;
  302. Procedure TestString_Compare;
  303. Procedure TestString_SetLength;
  304. Procedure TestString_CharAt;
  305. Procedure TestStringHMinusFail;
  306. Procedure TestStr;
  307. Procedure TestBaseType_AnsiStringFail;
  308. Procedure TestBaseType_WideStringFail;
  309. Procedure TestBaseType_ShortStringFail;
  310. Procedure TestBaseType_RawByteStringFail;
  311. Procedure TestTypeShortstring_Fail;
  312. Procedure TestCharSet_Custom;
  313. Procedure TestWideChar;
  314. Procedure TestForCharDo;
  315. Procedure TestForCharInDo;
  316. // alias types
  317. Procedure TestAliasTypeRef;
  318. Procedure TestTypeCast_BaseTypes;
  319. Procedure TestTypeCast_AliasBaseTypes;
  320. // functions
  321. Procedure TestEmptyProc;
  322. Procedure TestProcOneParam;
  323. Procedure TestFunctionWithoutParams;
  324. Procedure TestProcedureWithoutParams;
  325. Procedure TestPrgProcVar;
  326. Procedure TestProcTwoArgs;
  327. Procedure TestProc_DefaultValue;
  328. Procedure TestUnitProcVar;
  329. Procedure TestImplProc;
  330. Procedure TestFunctionResult;
  331. Procedure TestNestedProc;
  332. Procedure TestNestedProc_ResultString;
  333. Procedure TestForwardProc;
  334. Procedure TestNestedForwardProc;
  335. Procedure TestAssignFunctionResult;
  336. Procedure TestFunctionResultInCondition;
  337. Procedure TestFunctionResultInForLoop;
  338. Procedure TestFunctionResultInTypeCast;
  339. Procedure TestExit;
  340. Procedure TestExit_ResultInFinally;
  341. Procedure TestBreak;
  342. Procedure TestBreakAsVar;
  343. Procedure TestContinue;
  344. Procedure TestProc_External;
  345. Procedure TestProc_ExternalOtherUnit;
  346. Procedure TestProc_Asm;
  347. Procedure TestProc_AsmSubBlock;
  348. Procedure TestProc_Assembler;
  349. Procedure TestProc_VarParam;
  350. Procedure TestProc_VarParamString;
  351. Procedure TestProc_VarParamV;
  352. Procedure TestProc_Overload;
  353. Procedure TestProc_OverloadForward;
  354. Procedure TestProc_OverloadIntfImpl;
  355. Procedure TestProc_OverloadNested;
  356. Procedure TestProc_OverloadNestedForward;
  357. Procedure TestProc_OverloadUnitCycle;
  358. Procedure TestProc_Varargs;
  359. Procedure TestProc_ConstOrder;
  360. Procedure TestProc_DuplicateConst;
  361. Procedure TestProc_LocalVarAbsolute;
  362. Procedure TestProc_ResultAbsolute;
  363. Procedure TestProc_LocalVarInit;
  364. Procedure TestProc_ReservedWords;
  365. Procedure TestProc_ConstRefWord;
  366. // anonymous functions
  367. Procedure TestAnonymousProc_Assign_ObjFPC;
  368. Procedure TestAnonymousProc_Assign_Delphi;
  369. Procedure TestAnonymousProc_Arg;
  370. Procedure TestAnonymousProc_Typecast;
  371. Procedure TestAnonymousProc_With;
  372. Procedure TestAnonymousProc_ExceptOn;
  373. Procedure TestAnonymousProc_Nested;
  374. Procedure TestAnonymousProc_NestedAssignResult;
  375. Procedure TestAnonymousProc_Class;
  376. Procedure TestAnonymousProc_ForLoop;
  377. Procedure TestAnonymousProc_AsmDelphi;
  378. // enums, sets
  379. Procedure TestEnum_Name;
  380. Procedure TestEnum_Number;
  381. Procedure TestEnum_ConstFail;
  382. Procedure TestEnum_Functions;
  383. Procedure TestEnumRg_Functions;
  384. Procedure TestEnum_AsParams;
  385. Procedure TestEnumRange_Array;
  386. Procedure TestEnum_ForIn;
  387. Procedure TestEnum_ScopedNumber;
  388. Procedure TestEnum_InFunction;
  389. Procedure TestEnum_Name_Anonymous_Unit;
  390. Procedure TestSet_Enum;
  391. Procedure TestSet_Operators;
  392. Procedure TestSet_Operator_In;
  393. Procedure TestSet_Functions;
  394. Procedure TestSet_PassAsArgClone;
  395. Procedure TestSet_AsParams;
  396. Procedure TestSet_Property;
  397. Procedure TestSet_EnumConst;
  398. Procedure TestSet_IntConst;
  399. Procedure TestSet_IntRange;
  400. Procedure TestSet_AnonymousEnumType;
  401. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  402. Procedure TestSet_ConstEnum;
  403. Procedure TestSet_ConstChar;
  404. Procedure TestSet_ConstInt;
  405. Procedure TestSet_InFunction;
  406. Procedure TestSet_ForIn;
  407. // statements
  408. Procedure TestNestBegin;
  409. Procedure TestIncDec;
  410. Procedure TestLoHiFpcMode;
  411. Procedure TestLoHiDelphiMode;
  412. Procedure TestAssignments;
  413. Procedure TestArithmeticOperators1;
  414. Procedure TestMultiAdd;
  415. Procedure TestLogicalOperators;
  416. Procedure TestBitwiseOperators;
  417. Procedure TestBitwiseOperatorsLongword;
  418. Procedure TestFunctionInt;
  419. Procedure TestFunctionString;
  420. Procedure TestIfThen;
  421. Procedure TestForLoop;
  422. Procedure TestForLoopInsideFunction;
  423. Procedure TestForLoop_ReadVarAfter;
  424. Procedure TestForLoop_Nested;
  425. Procedure TestRepeatUntil;
  426. Procedure TestAsmBlock;
  427. Procedure TestAsmPas_Impl; // ToDo
  428. Procedure TestTryFinally;
  429. Procedure TestTryExcept;
  430. Procedure TestTryExcept_ReservedWords;
  431. Procedure TestIfThenRaiseElse;
  432. Procedure TestCaseOf;
  433. Procedure TestCaseOf_UseSwitch;
  434. Procedure TestCaseOfNoElse;
  435. Procedure TestCaseOfNoElse_UseSwitch;
  436. Procedure TestCaseOfRange;
  437. Procedure TestCaseOfString;
  438. Procedure TestCaseOfChar;
  439. Procedure TestCaseOfExternalClassConst;
  440. Procedure TestDebugger;
  441. // arrays
  442. Procedure TestArray_Dynamic;
  443. Procedure TestArray_Dynamic_Nil;
  444. Procedure TestArray_DynMultiDimensional;
  445. Procedure TestArray_DynamicAssign;
  446. Procedure TestArray_StaticInt;
  447. Procedure TestArray_StaticBool;
  448. Procedure TestArray_StaticChar;
  449. Procedure TestArray_StaticMultiDim;
  450. Procedure TestArray_StaticInFunction;
  451. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  452. Procedure TestArrayOfRecord;
  453. Procedure TestArray_StaticRecord;
  454. Procedure TestArrayOfSet;
  455. Procedure TestArray_DynAsParam;
  456. Procedure TestArray_StaticAsParam;
  457. Procedure TestArrayElement_AsParams;
  458. Procedure TestArrayElementFromFuncResult_AsParams;
  459. Procedure TestArrayEnumTypeRange;
  460. Procedure TestArray_SetLengthOutArg;
  461. Procedure TestArray_SetLengthProperty;
  462. Procedure TestArray_SetLengthMultiDim;
  463. Procedure TestArray_SetLengthDynOfStatic;
  464. Procedure TestArray_OpenArrayOfString;
  465. Procedure TestArray_ArrayOfCharAssignString;
  466. Procedure TestArray_ConstRef;
  467. Procedure TestArray_Concat;
  468. Procedure TestArray_Concat_Append;
  469. Procedure TestArray_Concat_Append_Var;
  470. Procedure TestArray_Copy;
  471. Procedure TestArray_InsertDelete;
  472. Procedure TestArray_Add_Append;
  473. Procedure TestArray_DynArrayConstObjFPC;
  474. Procedure TestArray_DynArrayConstDelphi;
  475. Procedure TestArray_ArrayLitAsParam;
  476. Procedure TestArray_ArrayLitMultiDimAsParam;
  477. Procedure TestArray_ArrayLitStaticAsParam;
  478. Procedure TestArray_ForInArrOfString;
  479. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  480. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  481. Procedure TestArrayOfConst_TVarRec;
  482. Procedure TestArrayOfConst_PassBaseTypes;
  483. Procedure TestArrayOfConst_PassObj;
  484. // record
  485. Procedure TestRecord_Empty;
  486. Procedure TestRecord_Var;
  487. Procedure TestRecord_VarExternal;
  488. Procedure TestRecord_WithDo;
  489. Procedure TestRecord_Assign;
  490. Procedure TestRecord_AsParams;
  491. Procedure TestRecord_ConstRef;
  492. Procedure TestRecordElement_AsParams;
  493. Procedure TestRecordElementFromFuncResult_AsParams;
  494. Procedure TestRecordElementFromWith_AsParams;
  495. Procedure TestRecord_Equal;
  496. Procedure TestRecord_JSValue;
  497. Procedure TestRecord_VariantFail;
  498. Procedure TestRecord_FieldArray;
  499. Procedure TestRecord_Const;
  500. Procedure TestRecord_TypecastFail;
  501. Procedure TestRecord_InFunction;
  502. // anonymous record
  503. Procedure TestRecordAnonym_Field;
  504. Procedure TestRecordAnonym_Assign;
  505. Procedure TestRecordAnonym_Nested;
  506. Procedure TestRecordAnonym_Const;
  507. Procedure TestRecordAnonym_InFunction;
  508. // advanced record
  509. Procedure TestAdvRecord_Function;
  510. Procedure TestAdvRecord_Property;
  511. Procedure TestAdvRecord_PropertyDefault;
  512. Procedure TestAdvRecord_Property_ClassMethod;
  513. Procedure TestAdvRecord_Const;
  514. Procedure TestAdvRecord_ExternalField;
  515. Procedure TestAdvRecord_SubRecord;
  516. Procedure TestAdvRecord_SubClass;
  517. Procedure TestAdvRecord_SubInterfaceFail;
  518. Procedure TestAdvRecord_Constructor;
  519. Procedure TestAdvRecord_ClassConstructor_Program;
  520. Procedure TestAdvRecord_ClassConstructor_Unit;
  521. // classes
  522. Procedure TestClass_TObjectDefaultConstructor;
  523. Procedure TestClass_TObjectConstructorWithParams;
  524. Procedure TestClass_TObjectConstructorWithDefaultParam;
  525. Procedure TestClass_Var;
  526. Procedure TestClass_Method;
  527. Procedure TestClass_Implementation;
  528. Procedure TestClass_Inheritance;
  529. Procedure TestClass_TypeAlias;
  530. Procedure TestClass_AbstractMethod;
  531. Procedure TestClass_CallInherited_ProcNoParams;
  532. Procedure TestClass_CallInherited_WithParams;
  533. Procedure TestClasS_CallInheritedConstructor;
  534. Procedure TestClass_ClassVar_Assign;
  535. Procedure TestClass_CallClassMethod;
  536. Procedure TestClass_CallClassMethodStatic;
  537. Procedure TestClass_Property;
  538. Procedure TestClass_Property_ClassMethod;
  539. Procedure TestClass_Property_ClassMethodStatic;
  540. Procedure TestClass_Property_Indexed;
  541. Procedure TestClass_Property_IndexSpec;
  542. Procedure TestClass_PropertyOfTypeArray;
  543. Procedure TestClass_PropertyDefault;
  544. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  545. //Procedure TestClass_PropertyDefault;
  546. Procedure TestClass_PropertyOverride;
  547. Procedure TestClass_PropertyIncVisibility;
  548. Procedure TestClass_Assigned;
  549. Procedure TestClass_WithClassDoCreate;
  550. Procedure TestClass_WithClassInstDoProperty;
  551. Procedure TestClass_WithClassInstDoPropertyWithParams;
  552. Procedure TestClass_WithClassInstDoFunc;
  553. Procedure TestClass_TypeCast;
  554. Procedure TestClass_TypeCastUntypedParam;
  555. Procedure TestClass_Overloads;
  556. Procedure TestClass_OverloadsAncestor;
  557. Procedure TestClass_OverloadConstructor;
  558. Procedure TestClass_OverloadDelphiOverride;
  559. Procedure TestClass_ReintroduceVarDelphi;
  560. Procedure TestClass_ReintroducedVar;
  561. Procedure TestClass_RaiseDescendant;
  562. Procedure TestClass_ExternalMethod;
  563. Procedure TestClass_ExternalVirtualNameMismatchFail;
  564. Procedure TestClass_ExternalOverrideFail;
  565. Procedure TestClass_ExternalVar;
  566. Procedure TestClass_Const;
  567. Procedure TestClass_ConstEnum;
  568. Procedure TestClass_LocalConstDuplicate_Prg;
  569. Procedure TestClass_LocalConstDuplicate_Unit;
  570. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  571. Procedure TestClass_LocalVarSelfFail;
  572. Procedure TestClass_ArgSelfFail;
  573. Procedure TestClass_NestedProcSelf;
  574. Procedure TestClass_NestedProcSelf2;
  575. Procedure TestClass_NestedProcClassSelf;
  576. Procedure TestClass_NestedProcCallInherited;
  577. Procedure TestClass_TObjectFree;
  578. Procedure TestClass_TObjectFree_VarArg;
  579. Procedure TestClass_TObjectFreeNewInstance;
  580. Procedure TestClass_TObjectFreeLowerCase;
  581. Procedure TestClass_TObjectFreeFunctionFail;
  582. Procedure TestClass_TObjectFreePropertyFail;
  583. Procedure TestClass_ForIn;
  584. Procedure TestClass_DispatchMessage;
  585. Procedure TestClass_Message_DuplicateIntFail;
  586. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  587. // class of
  588. Procedure TestClassOf_Create;
  589. Procedure TestClassOf_Call;
  590. Procedure TestClassOf_Assign;
  591. Procedure TestClassOf_Is;
  592. Procedure TestClassOf_Compare;
  593. Procedure TestClassOf_ClassVar;
  594. Procedure TestClassOf_ClassMethod;
  595. Procedure TestClassOf_ClassProperty;
  596. Procedure TestClassOf_ClassMethodSelf;
  597. Procedure TestClassOf_TypeCast;
  598. Procedure TestClassOf_ImplicitFunctionCall;
  599. Procedure TestClassOf_Const;
  600. // nested class
  601. Procedure TestNestedClass_Alias;
  602. Procedure TestNestedClass_Record;
  603. Procedure TestNestedClass_Class;
  604. Procedure TestNestedClass_CallInherited;
  605. // external class
  606. Procedure TestExternalClass_Var;
  607. Procedure TestExternalClass_Const;
  608. Procedure TestExternalClass_Dollar;
  609. Procedure TestExternalClass_DuplicateVarFail;
  610. Procedure TestExternalClass_Method;
  611. Procedure TestExternalClass_ClassMethod;
  612. Procedure TestExternalClass_ClassMethodStatic;
  613. Procedure TestExternalClass_FunctionResultInTypeCast;
  614. Procedure TestExternalClass_NonExternalOverride;
  615. Procedure TestExternalClass_OverloadHint;
  616. Procedure TestExternalClass_SameNamePublishedProperty;
  617. Procedure TestExternalClass_Property;
  618. Procedure TestExternalClass_PropertyDate;
  619. Procedure TestExternalClass_ClassProperty;
  620. Procedure TestExternalClass_ClassOf;
  621. Procedure TestExternalClass_ClassOtherUnit;
  622. Procedure TestExternalClass_Is;
  623. Procedure TestExternalClass_As;
  624. Procedure TestExternalClass_DestructorFail;
  625. Procedure TestExternalClass_New;
  626. Procedure TestExternalClass_ClassOf_New;
  627. Procedure TestExternalClass_FuncClassOf_New;
  628. Procedure TestExternalClass_New_PasClassFail;
  629. Procedure TestExternalClass_New_PasClassBracketsFail;
  630. Procedure TestExternalClass_NewExtName;
  631. Procedure TestExternalClass_Constructor;
  632. Procedure TestExternalClass_ConstructorBrackets;
  633. Procedure TestExternalClass_LocalConstSameName;
  634. Procedure TestExternalClass_ReintroduceOverload;
  635. Procedure TestExternalClass_Inherited;
  636. Procedure TestExternalClass_PascalAncestorFail;
  637. Procedure TestExternalClass_NewInstance;
  638. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  639. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  640. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  641. Procedure TestExternalClass_JSFunctionPasDescendant;
  642. Procedure TestExternalClass_PascalProperty;
  643. Procedure TestExternalClass_TypeCastToRootClass;
  644. Procedure TestExternalClass_TypeCastToJSObject;
  645. Procedure TestExternalClass_TypeCastStringToExternalString;
  646. Procedure TestExternalClass_TypeCastToJSFunction;
  647. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  648. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  649. Procedure TestExternalClass_BracketAccessor;
  650. Procedure TestExternalClass_BracketAccessor_Call;
  651. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  652. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  653. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  654. Procedure TestExternalClass_BracketAccessor_MultiType;
  655. Procedure TestExternalClass_BracketAccessor_Index;
  656. Procedure TestExternalClass_ForInJSObject;
  657. Procedure TestExternalClass_ForInJSArray;
  658. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  659. Procedure TestExternalClass_NestedConstructor;
  660. // class interfaces
  661. Procedure TestClassInterface_Corba;
  662. Procedure TestClassInterface_Corba_ProcExternalFail;
  663. Procedure TestClassInterface_Corba_Overloads;
  664. Procedure TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  665. Procedure TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  666. Procedure TestClassInterface_Corba_AncestorImpl;
  667. Procedure TestClassInterface_Corba_ImplReintroduce;
  668. Procedure TestClassInterface_Corba_MethodResolution;
  669. Procedure TestClassInterface_COM_AncestorMoreInterfaces;
  670. Procedure TestClassInterface_Corba_MethodOverride;
  671. Procedure TestClassInterface_Corba_Delegation;
  672. Procedure TestClassInterface_Corba_DelegationStatic;
  673. Procedure TestClassInterface_Corba_Operators;
  674. Procedure TestClassInterface_Corba_Args;
  675. Procedure TestClassInterface_Corba_ForIn;
  676. Procedure TestClassInterface_Corba_ArrayOfIntf;
  677. Procedure TestClassInterface_COM_AssignVar;
  678. Procedure TestClassInterface_COM_AssignArg;
  679. Procedure TestClassInterface_COM_FunctionResult;
  680. Procedure TestClassInterface_COM_InheritedFuncResult;
  681. Procedure TestClassInterface_COM_IsAsTypeCasts;
  682. Procedure TestClassInterface_COM_PassAsArg;
  683. Procedure TestClassInterface_COM_PassToUntypedParam;
  684. Procedure TestClassInterface_COM_FunctionInExpr;
  685. Procedure TestClassInterface_COM_Property;
  686. Procedure TestClassInterface_COM_IntfProperty;
  687. Procedure TestClassInterface_COM_Delegation;
  688. Procedure TestClassInterface_COM_With;
  689. Procedure TestClassInterface_COM_ForIn;
  690. Procedure TestClassInterface_COM_ArrayOfIntf;
  691. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  692. Procedure TestClassInterface_COM_RecordIntfFail;
  693. Procedure TestClassInterface_COM_UnitInitialization;
  694. Procedure TestClassInterface_Corba_GUID;
  695. Procedure TestClassInterface_Corba_GUIDProperty;
  696. // helpers
  697. Procedure TestClassHelper_ClassVar;
  698. Procedure TestClassHelper_Method_AccessInstanceFields;
  699. Procedure TestClassHelper_Method_Call;
  700. Procedure TestClassHelper_Method_Nested_Call;
  701. Procedure TestClassHelper_ClassMethod_Call;
  702. Procedure TestClassHelper_ClassOf;
  703. Procedure TestClassHelper_MethodRefObjFPC;
  704. Procedure TestClassHelper_Constructor;
  705. Procedure TestClassHelper_InheritedObjFPC;
  706. Procedure TestClassHelper_Property;
  707. Procedure TestClassHelper_Property_Array;
  708. Procedure TestClassHelper_Property_Array_Default;
  709. Procedure TestClassHelper_Property_Array_DefaultDefault;
  710. Procedure TestClassHelper_ClassProperty;
  711. Procedure TestClassHelper_ClassPropertyStatic;
  712. Procedure TestClassHelper_ClassProperty_Array;
  713. Procedure TestClassHelper_ForIn;
  714. Procedure TestClassHelper_PassProperty;
  715. Procedure TestExtClassHelper_ClassVar;
  716. Procedure TestExtClassHelper_Method_Call;
  717. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  718. Procedure TestRecordHelper_ClassVar;
  719. Procedure TestRecordHelper_Method_Call;
  720. Procedure TestRecordHelper_Constructor;
  721. Procedure TestTypeHelper_ClassVar;
  722. Procedure TestTypeHelper_PassResultElement;
  723. Procedure TestTypeHelper_PassArgs;
  724. Procedure TestTypeHelper_PassVarConst;
  725. Procedure TestTypeHelper_PassFuncResult;
  726. Procedure TestTypeHelper_PassPropertyField;
  727. Procedure TestTypeHelper_PassPropertyGetter;
  728. Procedure TestTypeHelper_PassClassPropertyField;
  729. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  730. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  731. Procedure TestTypeHelper_Property;
  732. Procedure TestTypeHelper_Property_Array;
  733. Procedure TestTypeHelper_ClassProperty;
  734. Procedure TestTypeHelper_ClassProperty_Array;
  735. Procedure TestTypeHelper_ClassMethod;
  736. Procedure TestTypeHelper_ExtClassMethodFail;
  737. Procedure TestTypeHelper_Constructor;
  738. Procedure TestTypeHelper_Word;
  739. Procedure TestTypeHelper_Boolean;
  740. Procedure TestTypeHelper_WordBool;
  741. Procedure TestTypeHelper_Double;
  742. Procedure TestTypeHelper_NativeInt;
  743. Procedure TestTypeHelper_StringChar;
  744. Procedure TestTypeHelper_JSValue;
  745. Procedure TestTypeHelper_Array;
  746. Procedure TestTypeHelper_EnumType;
  747. Procedure TestTypeHelper_SetType;
  748. Procedure TestTypeHelper_InterfaceType;
  749. Procedure TestTypeHelper_NestedSelf;
  750. // proc types
  751. Procedure TestProcType;
  752. Procedure TestProcType_Arg;
  753. Procedure TestProcType_FunctionFPC;
  754. Procedure TestProcType_FunctionDelphi;
  755. Procedure TestProcType_ProcedureDelphi;
  756. Procedure TestProcType_AsParam;
  757. Procedure TestProcType_MethodFPC;
  758. Procedure TestProcType_MethodDelphi;
  759. Procedure TestProcType_PropertyFPC;
  760. Procedure TestProcType_PropertyDelphi;
  761. Procedure TestProcType_WithClassInstDoPropertyFPC;
  762. Procedure TestProcType_Nested;
  763. Procedure TestProcType_NestedOfObject;
  764. Procedure TestProcType_ReferenceToProc;
  765. Procedure TestProcType_ReferenceToMethod;
  766. Procedure TestProcType_Typecast;
  767. Procedure TestProcType_PassProcToUntyped;
  768. Procedure TestProcType_PassProcToArray;
  769. Procedure TestProcType_SafeCallObjFPC;
  770. Procedure TestProcType_SafeCallDelphi;
  771. // pointer
  772. Procedure TestPointer;
  773. Procedure TestPointer_Proc;
  774. Procedure TestPointer_AssignRecordFail;
  775. Procedure TestPointer_AssignStaticArrayFail;
  776. Procedure TestPointer_TypeCastJSValueToPointer;
  777. Procedure TestPointer_NonRecordFail;
  778. Procedure TestPointer_AnonymousArgTypeFail;
  779. Procedure TestPointer_AnonymousVarTypeFail;
  780. Procedure TestPointer_AnonymousResultTypeFail;
  781. Procedure TestPointer_AddrOperatorFail;
  782. Procedure TestPointer_ArrayParamsFail;
  783. Procedure TestPointer_PointerAddFail;
  784. Procedure TestPointer_IncPointerFail;
  785. Procedure TestPointer_Record;
  786. Procedure TestPointer_RecordArg;
  787. // jsvalue
  788. Procedure TestJSValue_AssignToJSValue;
  789. Procedure TestJSValue_TypeCastToBaseType;
  790. Procedure TestJSValue_TypecastToJSValue;
  791. Procedure TestJSValue_Equal;
  792. Procedure TestJSValue_If;
  793. Procedure TestJSValue_Not;
  794. Procedure TestJSValue_Enum;
  795. Procedure TestJSValue_ClassInstance;
  796. Procedure TestJSValue_ClassOf;
  797. Procedure TestJSValue_ArrayOfJSValue;
  798. Procedure TestJSValue_ArrayLit;
  799. Procedure TestJSValue_Params;
  800. Procedure TestJSValue_UntypedParam;
  801. Procedure TestJSValue_FuncResultType;
  802. Procedure TestJSValue_ProcType_Assign;
  803. Procedure TestJSValue_ProcType_Equal;
  804. Procedure TestJSValue_ProcType_Param;
  805. Procedure TestJSValue_AssignToPointerFail;
  806. Procedure TestJSValue_OverloadDouble;
  807. Procedure TestJSValue_OverloadNativeInt;
  808. Procedure TestJSValue_OverloadWord;
  809. Procedure TestJSValue_OverloadString;
  810. Procedure TestJSValue_OverloadChar;
  811. Procedure TestJSValue_OverloadPointer;
  812. Procedure TestJSValue_ForIn;
  813. // RTTI
  814. Procedure TestRTTI_IntRange;
  815. Procedure TestRTTI_Double;
  816. Procedure TestRTTI_ProcType;
  817. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  818. Procedure TestRTTI_ProcTypeAnonymous;
  819. Procedure TestRTTI_EnumAndSetType;
  820. Procedure TestRTTI_EnumRange;
  821. Procedure TestRTTI_AnonymousEnumType;
  822. Procedure TestRTTI_StaticArray;
  823. Procedure TestRTTI_DynArray;
  824. Procedure TestRTTI_ArrayNestedAnonymous;
  825. Procedure TestRTTI_PublishedMethodOverloadFail;
  826. Procedure TestRTTI_PublishedMethodHideNoHint;
  827. Procedure TestRTTI_PublishedMethodExternalFail;
  828. Procedure TestRTTI_PublishedClassPropertyFail;
  829. Procedure TestRTTI_PublishedClassFieldFail;
  830. Procedure TestRTTI_PublishedFieldExternalFail;
  831. Procedure TestRTTI_Class_Field;
  832. Procedure TestRTTI_Class_Method;
  833. Procedure TestRTTI_Class_MethodArgFlags;
  834. Procedure TestRTTI_Class_Property;
  835. Procedure TestRTTI_Class_PropertyParams;
  836. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  837. Procedure TestRTTI_Class_OmitRTTI;
  838. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  839. Procedure TestRTTI_IndexModifier;
  840. Procedure TestRTTI_StoredModifier;
  841. Procedure TestRTTI_DefaultValue;
  842. Procedure TestRTTI_DefaultValueSet;
  843. Procedure TestRTTI_DefaultValueRangeType;
  844. Procedure TestRTTI_DefaultValueInherit;
  845. Procedure TestRTTI_OverrideMethod;
  846. Procedure TestRTTI_ReintroduceMethod;
  847. Procedure TestRTTI_OverloadProperty;
  848. // ToDo: array argument
  849. Procedure TestRTTI_ClassForward;
  850. Procedure TestRTTI_ClassOf;
  851. Procedure TestRTTI_Record;
  852. Procedure TestRTTI_RecordAnonymousArray;
  853. Procedure TestRTTI_Record_ClassVarType;
  854. Procedure TestRTTI_LocalTypes;
  855. Procedure TestRTTI_TypeInfo_BaseTypes;
  856. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  857. Procedure TestRTTI_TypeInfo_LocalFail;
  858. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  859. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  860. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  861. Procedure TestRTTI_TypeInfo_FunctionClassType;
  862. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  863. Procedure TestRTTI_Interface_Corba;
  864. Procedure TestRTTI_Interface_COM;
  865. Procedure TestRTTI_ClassHelper;
  866. Procedure TestRTTI_ExternalClass;
  867. Procedure TestRTTI_Unit;
  868. // Resourcestring
  869. Procedure TestResourcestringProgram;
  870. Procedure TestResourcestringUnit;
  871. Procedure TestResourcestringImplementation;
  872. // Attributes
  873. Procedure TestAttributes_Members;
  874. Procedure TestAttributes_Types;
  875. Procedure TestAttributes_HelperConstructor_Fail;
  876. Procedure TestAttributes_InterfacesList;
  877. // Assertions, checks
  878. procedure TestAssert;
  879. procedure TestAssert_SysUtils;
  880. procedure TestObjectChecks;
  881. procedure TestOverflowChecks_Int;
  882. procedure TestRangeChecks_AssignInt;
  883. procedure TestRangeChecks_AssignIntRange;
  884. procedure TestRangeChecks_AssignEnum;
  885. procedure TestRangeChecks_AssignEnumRange;
  886. procedure TestRangeChecks_AssignChar;
  887. procedure TestRangeChecks_AssignCharRange;
  888. procedure TestRangeChecks_ArrayIndex;
  889. procedure TestRangeChecks_ArrayOfRecIndex;
  890. procedure TestRangeChecks_StringIndex;
  891. procedure TestRangeChecks_TypecastInt;
  892. procedure TestRangeChecks_TypeHelperInt;
  893. // Async/AWait
  894. Procedure TestAsync_Proc;
  895. Procedure TestAsync_CallResultIsPromise;
  896. Procedure TestAsync_ConstructorFail;
  897. Procedure TestAsync_PropertyGetterFail;
  898. Procedure TestAwait_NonPromiseWithTypeFail;
  899. Procedure TestAwait_AsyncCallTypeMismatch;
  900. Procedure TestAWait_OutsideAsyncFail;
  901. Procedure TestAWait_IntegerFail;
  902. Procedure TestAWait_ExternalClassPromise;
  903. Procedure TestAWait_JSValue;
  904. Procedure TestAWait_Result;
  905. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  906. Procedure TestAsync_AnonymousProc;
  907. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  908. Procedure TestAsync_ProcType;
  909. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  910. Procedure TestAsync_Inherited;
  911. Procedure TestAsync_ClassInterface;
  912. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  913. Procedure TestAWait_ClassAs;
  914. // Library
  915. Procedure TestLibrary_Empty;
  916. Procedure TestLibrary_ExportFunc;
  917. Procedure TestLibrary_ExportFuncOverloadedFail;
  918. Procedure TestLibrary_Export_Index_Fail;
  919. Procedure TestLibrary_ExportVar;
  920. Procedure TestLibrary_ExportUnitFunc;
  921. end;
  922. function LinesToStr(Args: array of const): string;
  923. function ExtractFileUnitName(aFilename: string): string;
  924. function JSToStr(El: TJSElement): string;
  925. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  926. implementation
  927. function LinesToStr(Args: array of const): string;
  928. var
  929. s: String;
  930. i: Integer;
  931. begin
  932. s:='';
  933. for i:=Low(Args) to High(Args) do
  934. case Args[i].VType of
  935. vtChar: s += Args[i].VChar+LineEnding;
  936. vtString: s += Args[i].VString^+LineEnding;
  937. vtPChar: s += Args[i].VPChar+LineEnding;
  938. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  939. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  940. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  941. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  942. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  943. end;
  944. Result:=s;
  945. end;
  946. function ExtractFileUnitName(aFilename: string): string;
  947. var
  948. p: Integer;
  949. begin
  950. Result:=ExtractFileName(aFilename);
  951. if Result='' then exit;
  952. for p:=length(Result) downto 1 do
  953. case Result[p] of
  954. '/','\': exit;
  955. '.':
  956. begin
  957. Delete(Result,p,length(Result));
  958. exit;
  959. end;
  960. end;
  961. end;
  962. function JSToStr(El: TJSElement): string;
  963. var
  964. aWriter: TBufferWriter;
  965. aJSWriter: TJSWriter;
  966. begin
  967. aJSWriter:=nil;
  968. aWriter:=TBufferWriter.Create(1000);
  969. try
  970. aJSWriter:=TJSWriter.Create(aWriter);
  971. aJSWriter.IndentSize:=2;
  972. aJSWriter.WriteJS(El);
  973. Result:=aWriter.AsString;
  974. finally
  975. aJSWriter.Free;
  976. aWriter.Free;
  977. end;
  978. end;
  979. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  980. // search diff, ignore changes in spaces
  981. const
  982. SpaceChars = [#9,#10,#13,' '];
  983. var
  984. ExpectedP, ActualP: PChar;
  985. function FindLineEnd(p: PChar): PChar;
  986. begin
  987. Result:=p;
  988. while not (Result^ in [#0,#10,#13]) do inc(Result);
  989. end;
  990. function FindLineStart(p, MinP: PChar): PChar;
  991. begin
  992. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  993. Result:=p;
  994. end;
  995. procedure SkipLineEnd(var p: PChar);
  996. begin
  997. if p^ in [#10,#13] then
  998. begin
  999. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1000. inc(p,2)
  1001. else
  1002. inc(p);
  1003. end;
  1004. end;
  1005. function HasSpecialChar(s: string): boolean;
  1006. var
  1007. i: Integer;
  1008. begin
  1009. for i:=1 to length(s) do
  1010. if s[i] in [#0..#31,#127..#255] then
  1011. exit(true);
  1012. Result:=false;
  1013. end;
  1014. function HashSpecialChars(s: string): string;
  1015. var
  1016. i: Integer;
  1017. begin
  1018. Result:='';
  1019. for i:=1 to length(s) do
  1020. if s[i] in [#0..#31,#127..#255] then
  1021. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1022. else
  1023. Result:=Result+s[i];
  1024. end;
  1025. procedure DiffFound;
  1026. var
  1027. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1028. ExpLine, ActLine: String;
  1029. i, LineNo, DiffLineNo: Integer;
  1030. begin
  1031. writeln('Diff found "',Msg,'". Lines:');
  1032. // write correct lines
  1033. p:=PChar(Expected);
  1034. LineNo:=0;
  1035. DiffLineNo:=0;
  1036. repeat
  1037. StartPos:=p;
  1038. while not (p^ in [#0,#10,#13]) do inc(p);
  1039. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1040. SkipLineEnd(p);
  1041. inc(LineNo);
  1042. if (p<=ExpectedP) and (p^<>#0) then
  1043. begin
  1044. writeln('= ',ExpLine);
  1045. end else begin
  1046. // diff line
  1047. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1048. // write actual line
  1049. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1050. ActLineEndP:=FindLineEnd(ActualP);
  1051. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1052. writeln('- ',ActLine);
  1053. if HasSpecialChar(ActLine) then
  1054. writeln('- ',HashSpecialChars(ActLine));
  1055. // write expected line
  1056. writeln('+ ',ExpLine);
  1057. if HasSpecialChar(ExpLine) then
  1058. writeln('- ',HashSpecialChars(ExpLine));
  1059. // write empty line with pointer ^
  1060. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1061. writeln('^');
  1062. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1063. CheckSrcDiff:=false;
  1064. // write up to three following actual lines to get some context
  1065. for i:=1 to 3 do begin
  1066. ActLineStartP:=ActLineEndP;
  1067. SkipLineEnd(ActLineStartP);
  1068. if ActLineStartP^=#0 then break;
  1069. ActLineEndP:=FindLineEnd(ActLineStartP);
  1070. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1071. writeln('~ ',ActLine);
  1072. end;
  1073. exit;
  1074. end;
  1075. until p^=#0;
  1076. writeln('DiffFound Actual:-----------------------');
  1077. writeln(Actual);
  1078. writeln('DiffFound Expected:---------------------');
  1079. writeln(Expected);
  1080. writeln('DiffFound ------------------------------');
  1081. Msg:='diff found, but lines are the same, internal error';
  1082. CheckSrcDiff:=false;
  1083. end;
  1084. var
  1085. IsSpaceNeeded: Boolean;
  1086. LastChar, Quote: Char;
  1087. begin
  1088. Result:=true;
  1089. Msg:='';
  1090. if Expected='' then Expected:=' ';
  1091. if Actual='' then Actual:=' ';
  1092. ExpectedP:=PChar(Expected);
  1093. ActualP:=PChar(Actual);
  1094. repeat
  1095. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1096. case ExpectedP^ of
  1097. #0:
  1098. begin
  1099. // check that rest of Actual has only spaces
  1100. while ActualP^ in SpaceChars do inc(ActualP);
  1101. if ActualP^<>#0 then
  1102. begin
  1103. DiffFound;
  1104. exit;
  1105. end;
  1106. exit(true);
  1107. end;
  1108. ' ',#9,#10,#13:
  1109. begin
  1110. // skip space in Expected
  1111. IsSpaceNeeded:=false;
  1112. if ExpectedP>PChar(Expected) then
  1113. LastChar:=ExpectedP[-1]
  1114. else
  1115. LastChar:=#0;
  1116. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1117. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1118. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1119. IsSpaceNeeded:=true;
  1120. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1121. begin
  1122. DiffFound;
  1123. exit;
  1124. end;
  1125. while ActualP^ in SpaceChars do inc(ActualP);
  1126. end;
  1127. '''','"':
  1128. begin
  1129. while ActualP^ in SpaceChars do inc(ActualP);
  1130. if ExpectedP^<>ActualP^ then
  1131. begin
  1132. DiffFound;
  1133. exit;
  1134. end;
  1135. Quote:=ExpectedP^;
  1136. repeat
  1137. inc(ExpectedP);
  1138. inc(ActualP);
  1139. if ExpectedP^<>ActualP^ then
  1140. begin
  1141. DiffFound;
  1142. exit;
  1143. end;
  1144. if (ExpectedP^ in [#0,#10,#13]) then
  1145. break
  1146. else if (ExpectedP^=Quote) then
  1147. begin
  1148. inc(ExpectedP);
  1149. inc(ActualP);
  1150. break;
  1151. end;
  1152. until false;
  1153. end;
  1154. else
  1155. while ActualP^ in SpaceChars do inc(ActualP);
  1156. if ExpectedP^<>ActualP^ then
  1157. begin
  1158. DiffFound;
  1159. exit;
  1160. end;
  1161. inc(ExpectedP);
  1162. inc(ActualP);
  1163. end;
  1164. until false;
  1165. end;
  1166. { TTestEnginePasResolver }
  1167. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1168. begin
  1169. if FModule=AValue then Exit;
  1170. FModule:=AValue;
  1171. end;
  1172. destructor TTestEnginePasResolver.Destroy;
  1173. begin
  1174. FreeAndNil(FStreamResolver);
  1175. FreeAndNil(FParser);
  1176. FreeAndNil(FScanner);
  1177. FreeAndNil(FStreamResolver);
  1178. Module:=nil;
  1179. inherited Destroy;
  1180. end;
  1181. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1182. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1183. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1184. begin
  1185. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1186. TypeParams);
  1187. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1188. Module:=TPasModule(Result);
  1189. end;
  1190. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1191. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1192. begin
  1193. Result:=nil;
  1194. if InFilename<>'' then
  1195. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1196. if Assigned(OnFindUnit) then
  1197. Result:=OnFindUnit(AName);
  1198. if NameExpr=nil then ;
  1199. end;
  1200. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1201. begin
  1202. // do not parse recursively
  1203. // parse via the queue
  1204. if Section=nil then ;
  1205. end;
  1206. { TCustomTestModule }
  1207. procedure TCustomTestModule.FreeSrcMarkers;
  1208. var
  1209. aMarker, Last: PSrcMarker;
  1210. begin
  1211. aMarker:=FirstSrcMarker;
  1212. while aMarker<>nil do
  1213. begin
  1214. Last:=aMarker;
  1215. aMarker:=aMarker^.Next;
  1216. Dispose(Last);
  1217. end;
  1218. FirstSrcMarker:=nil;
  1219. LastSrcMarker:=nil;
  1220. end;
  1221. function TCustomTestModule.GetResolverCount: integer;
  1222. begin
  1223. Result:=FResolvers.Count;
  1224. end;
  1225. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1226. begin
  1227. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1228. end;
  1229. function TCustomTestModule.GetMsgCount: integer;
  1230. begin
  1231. Result:=FHintMsgs.Count;
  1232. end;
  1233. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1234. begin
  1235. Result:=TTestHintMessage(FHintMsgs[Index]);
  1236. end;
  1237. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1238. ): TPasModule;
  1239. var
  1240. DefNamespace: String;
  1241. begin
  1242. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1243. if (Pos('.',aUnitName)<1) then
  1244. begin
  1245. DefNamespace:=GetDefaultNamespace;
  1246. if DefNamespace<>'' then
  1247. begin
  1248. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1249. if Result<>nil then exit;
  1250. end;
  1251. end;
  1252. Result:=LoadUnit(aUnitName);
  1253. if Result<>nil then exit;
  1254. {$IFDEF VerbosePas2JS}
  1255. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1256. {$ENDIF}
  1257. Fail('can''t find unit "'+aUnitName+'"');
  1258. end;
  1259. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1260. var
  1261. aParser: TPasParser;
  1262. Item: TTestHintMessage;
  1263. begin
  1264. aParser:=Sender as TPasParser;
  1265. Item:=TTestHintMessage.Create;
  1266. Item.Id:=aParser.LastMsgNumber;
  1267. Item.MsgType:=aParser.LastMsgType;
  1268. Item.MsgNumber:=aParser.LastMsgNumber;
  1269. Item.Msg:=Msg;
  1270. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1271. {$IFDEF VerbosePas2JS}
  1272. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1273. {$ENDIF}
  1274. FHintMsgs.Add(Item);
  1275. end;
  1276. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1277. );
  1278. var
  1279. aResolver: TTestEnginePasResolver;
  1280. Item: TTestHintMessage;
  1281. begin
  1282. aResolver:=Sender as TTestEnginePasResolver;
  1283. Item:=TTestHintMessage.Create;
  1284. Item.Id:=aResolver.LastMsgId;
  1285. Item.MsgType:=aResolver.LastMsgType;
  1286. Item.MsgNumber:=aResolver.LastMsgNumber;
  1287. Item.Msg:=Msg;
  1288. Item.SourcePos:=aResolver.LastSourcePos;
  1289. {$IFDEF VerbosePas2JS}
  1290. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1291. {$ENDIF}
  1292. FHintMsgs.Add(Item);
  1293. end;
  1294. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1295. var
  1296. Item: TTestHintMessage;
  1297. aScanner: TPas2jsPasScanner;
  1298. begin
  1299. aScanner:=Sender as TPas2jsPasScanner;
  1300. Item:=TTestHintMessage.Create;
  1301. Item.Id:=aScanner.LastMsgNumber;
  1302. Item.MsgType:=aScanner.LastMsgType;
  1303. Item.MsgNumber:=aScanner.LastMsgNumber;
  1304. Item.Msg:=Msg;
  1305. Item.SourcePos:=aScanner.CurSourcePos;
  1306. {$IFDEF VerbosePas2JS}
  1307. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1308. {$ENDIF}
  1309. FHintMsgs.Add(Item);
  1310. end;
  1311. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1312. var
  1313. SubEl: TPasElement;
  1314. i: Integer;
  1315. procedure E(Msg: string);
  1316. var
  1317. s: String;
  1318. begin
  1319. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1320. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1321. writeln('ERROR: ',s);
  1322. Fail(s);
  1323. end;
  1324. begin
  1325. if arg=nil then ;
  1326. if El=nil then exit;
  1327. if El.Parent=El then
  1328. E('El.Parent=El='+GetObjName(El));
  1329. if El is TBinaryExpr then
  1330. begin
  1331. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1332. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1333. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1334. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1335. end
  1336. else if El is TParamsExpr then
  1337. begin
  1338. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1339. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1340. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1341. if TParamsExpr(El).Params[i].Parent<>El then
  1342. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1343. end
  1344. else if El is TProcedureExpr then
  1345. begin
  1346. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1347. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1348. end
  1349. else if El is TPasDeclarations then
  1350. begin
  1351. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1352. begin
  1353. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1354. if SubEl.Parent<>El then
  1355. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1356. end;
  1357. end
  1358. else if El is TPasImplBlock then
  1359. begin
  1360. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1361. begin
  1362. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1363. if SubEl.Parent<>El then
  1364. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1365. end;
  1366. end
  1367. else if El is TPasImplWithDo then
  1368. begin
  1369. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1370. begin
  1371. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1372. if SubEl.Parent<>El then
  1373. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1374. end;
  1375. end
  1376. else if El is TPasProcedure then
  1377. begin
  1378. if TPasProcedure(El).ProcType.Parent<>El then
  1379. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1380. end
  1381. else if El is TPasProcedureType then
  1382. begin
  1383. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1384. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1385. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1386. end;
  1387. end;
  1388. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1389. var
  1390. Data: PTestResolverReferenceData absolute FindData;
  1391. Line, Col: integer;
  1392. begin
  1393. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1394. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1395. if (Data^.Filename=El.SourceFilename)
  1396. and (Data^.Row=Line)
  1397. and (Data^.StartCol<=Col)
  1398. and (Data^.EndCol>=Col)
  1399. then
  1400. Data^.Found.Add(El);
  1401. end;
  1402. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1403. begin
  1404. if FWithTypeInfo=AValue then Exit;
  1405. FWithTypeInfo:=AValue;
  1406. if AValue then
  1407. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1408. else
  1409. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1410. end;
  1411. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1412. var
  1413. i: Integer;
  1414. CurEngine: TTestEnginePasResolver;
  1415. CurUnitName: String;
  1416. begin
  1417. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1418. Result:=nil;
  1419. if (Module.ClassType=TPasModule)
  1420. and (CompareText(Module.Name,aUnitName)=0) then
  1421. exit(Module);
  1422. for i:=0 to ResolverCount-1 do
  1423. begin
  1424. CurEngine:=Resolvers[i];
  1425. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1426. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1427. if CompareText(aUnitName,CurUnitName)=0 then
  1428. begin
  1429. Result:=CurEngine.Module;
  1430. if Result<>nil then exit;
  1431. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1432. FileResolver.FindSourceFile(aUnitName);
  1433. CurEngine.StreamResolver:=TStreamResolver.Create;
  1434. CurEngine.StreamResolver.OwnsStreams:=True;
  1435. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1436. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1437. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1438. InitScanner(CurEngine.Scanner);
  1439. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1440. CurEngine.Parser.Options:=po_tcmodules;
  1441. if CompareText(CurUnitName,'System')=0 then
  1442. CurEngine.Parser.ImplicitUses.Clear;
  1443. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1444. try
  1445. CurEngine.Parser.NextToken;
  1446. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1447. except
  1448. on E: Exception do
  1449. HandleException(E);
  1450. end;
  1451. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1452. Result:=CurEngine.Module;
  1453. exit;
  1454. end;
  1455. end;
  1456. end;
  1457. procedure TCustomTestModule.SetUp;
  1458. begin
  1459. {$IFDEF EnablePasTreeGlobalRefCount}
  1460. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1461. {$ENDIF}
  1462. if FResolvers<>nil then
  1463. begin
  1464. writeln('TCustomTestModule.SetUp FModules<>nil');
  1465. Halt;
  1466. end;
  1467. inherited SetUp;
  1468. FSkipTests:=false;
  1469. FWithTypeInfo:=false;
  1470. FSource:=TStringList.Create;
  1471. FHub:=TPas2JSResolverHub.Create(Self);
  1472. FResolvers:=TObjectList.Create(true);
  1473. FFilename:='test1.pp';
  1474. FFileResolver:=TStreamResolver.Create;
  1475. FFileResolver.OwnsStreams:=True;
  1476. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1477. InitScanner(FScanner);
  1478. FEngine:=AddModule(Filename);
  1479. FEngine.Scanner:=FScanner;
  1480. FScanner.Resolver:=FEngine;
  1481. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1482. FParser.OnLog:=@OnParserLog;
  1483. FEngine.Parser:=FParser;
  1484. Parser.Options:=po_tcmodules;
  1485. FModule:=Nil;
  1486. FConverter:=CreateConverter;
  1487. FExpectedErrorClass:=nil;
  1488. end;
  1489. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1490. var
  1491. Options: TPasToJsConverterOptions;
  1492. begin
  1493. Result:=TPasToJSConverter.Create;
  1494. Options:=co_tcmodules;
  1495. if WithTypeInfo then
  1496. Exclude(Options,coNoTypeInfo)
  1497. else
  1498. Include(Options,coNoTypeInfo);
  1499. Result.Options:=Options;
  1500. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1501. end;
  1502. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1503. begin
  1504. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1505. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1506. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1507. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1508. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1509. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1510. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1511. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1512. aScanner.OnLog:=@OnScannerLog;
  1513. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1514. end;
  1515. procedure TCustomTestModule.TearDown;
  1516. {$IFDEF CheckPasTreeRefCount}
  1517. var
  1518. El: TPasElement;
  1519. {$ENDIF}
  1520. var
  1521. i: Integer;
  1522. CurModule: TPasModule;
  1523. begin
  1524. FreeSrcMarkers;
  1525. FHintMsgs.Clear;
  1526. FHintMsgsGood.Clear;
  1527. FSkipTests:=false;
  1528. FWithTypeInfo:=false;
  1529. FJSRegModuleCall:=nil;
  1530. FJSModuleCallArgs:=nil;
  1531. FJSImplentationUses:=nil;
  1532. FJSInterfaceUses:=nil;
  1533. FJSModuleSrc:=nil;
  1534. FJSInitBody:=nil;
  1535. FreeAndNil(FJSSource);
  1536. FreeAndNil(FJSModule);
  1537. FreeAndNil(FConverter);
  1538. ResolverEngine.Clear;
  1539. FreeAndNil(FSource);
  1540. FreeAndNil(FFileResolver);
  1541. if FResolvers<>nil then
  1542. begin
  1543. for i:=0 to FResolvers.Count-1 do
  1544. begin
  1545. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1546. if CurModule=nil then continue;
  1547. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1548. end;
  1549. for i:=0 to FResolvers.Count-1 do
  1550. begin
  1551. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1552. if CurModule=nil then continue;
  1553. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1554. end;
  1555. FreeAndNil(FResolvers);
  1556. FModule:=nil;
  1557. FEngine:=nil;
  1558. end;
  1559. FreeAndNil(FHub);
  1560. inherited TearDown;
  1561. {$IFDEF EnablePasTreeGlobalRefCount}
  1562. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1563. begin
  1564. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1565. {$IFDEF CheckPasTreeRefCount}
  1566. El:=TPasElement.FirstRefEl;
  1567. while El<>nil do
  1568. begin
  1569. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1570. for i:=0 to El.RefIds.Count-1 do
  1571. writeln(' ',El.RefIds[i]);
  1572. El:=El.NextRefEl;
  1573. end;
  1574. {$ENDIF}
  1575. Halt;
  1576. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1577. end;
  1578. {$ENDIF}
  1579. end;
  1580. procedure TCustomTestModule.Add(Line: string);
  1581. begin
  1582. Source.Add(Line);
  1583. end;
  1584. procedure TCustomTestModule.Add(const Lines: array of string);
  1585. var
  1586. i: Integer;
  1587. begin
  1588. for i:=low(Lines) to high(Lines) do
  1589. Add(Lines[i]);
  1590. end;
  1591. procedure TCustomTestModule.StartParsing;
  1592. var
  1593. Src: String;
  1594. begin
  1595. Src:=Source.Text;
  1596. FEngine.Source:=Src;
  1597. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1598. Scanner.OpenFile(FileName);
  1599. Writeln('// Test : ',Self.TestName);
  1600. Writeln(Src);
  1601. end;
  1602. procedure TCustomTestModule.ParseModuleQueue;
  1603. var
  1604. i: Integer;
  1605. CurResolver: TTestEnginePasResolver;
  1606. Found: Boolean;
  1607. Section: TPasSection;
  1608. begin
  1609. // parse til exception or all Resolvers finished
  1610. while not SkipTests do
  1611. begin
  1612. Found:=false;
  1613. for i:=0 to ResolverCount-1 do
  1614. begin
  1615. CurResolver:=Resolvers[i];
  1616. if CurResolver.CurrentParser=nil then continue;
  1617. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1618. continue;
  1619. CurResolver.Parser.ParseContinue;
  1620. Found:=true;
  1621. break;
  1622. end;
  1623. if not Found then break;
  1624. end;
  1625. for i:=0 to ResolverCount-1 do
  1626. begin
  1627. CurResolver:=Resolvers[i];
  1628. if CurResolver.Parser=nil then
  1629. begin
  1630. if CurResolver.CurrentParser<>nil then
  1631. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1632. continue;
  1633. end;
  1634. if CurResolver.Parser.CurModule<>nil then
  1635. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1636. end;
  1637. end;
  1638. procedure TCustomTestModule.ParseModule;
  1639. begin
  1640. if SkipTests then exit;
  1641. FFirstPasStatement:=nil;
  1642. try
  1643. StartParsing;
  1644. Parser.ParseMain(FModule);
  1645. ParseModuleQueue;
  1646. except
  1647. on E: Exception do
  1648. HandleException(E);
  1649. end;
  1650. if SkipTests then exit;
  1651. AssertNotNull('Module resulted in Module',Module);
  1652. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1653. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1654. end;
  1655. procedure TCustomTestModule.ParseProgram;
  1656. begin
  1657. if SkipTests then exit;
  1658. ParseModule;
  1659. if SkipTests then exit;
  1660. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1661. FPasProgram:=TPasProgram(Module);
  1662. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1663. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1664. if (PasProgram.InitializationSection.Elements.Count>0) then
  1665. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1666. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1667. end;
  1668. procedure TCustomTestModule.ParseLibrary;
  1669. var
  1670. Init: TInitializationSection;
  1671. begin
  1672. if SkipTests then exit;
  1673. ParseModule;
  1674. if SkipTests then exit;
  1675. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1676. FPasLibrary:=TPasLibrary(Module);
  1677. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1678. Init:=PasLibrary.InitializationSection;
  1679. if (Init<>nil) and (Init.Elements.Count>0) then
  1680. if TObject(Init.Elements[0]) is TPasImplBlock then
  1681. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1682. end;
  1683. procedure TCustomTestModule.ParseUnit;
  1684. begin
  1685. if SkipTests then exit;
  1686. ParseModule;
  1687. if SkipTests then exit;
  1688. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1689. AssertNotNull('Has interface section',Module.InterfaceSection);
  1690. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1691. if (Module.InitializationSection<>nil)
  1692. and (Module.InitializationSection.Elements.Count>0)
  1693. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1694. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1695. end;
  1696. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1697. ): TTestEnginePasResolver;
  1698. var
  1699. i: Integer;
  1700. begin
  1701. for i:=0 to ResolverCount-1 do
  1702. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1703. exit(Resolvers[i]);
  1704. Result:=nil;
  1705. end;
  1706. function TCustomTestModule.AddModule(aFilename: string
  1707. ): TTestEnginePasResolver;
  1708. begin
  1709. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1710. if FindModuleWithFilename(aFilename)<>nil then
  1711. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1712. Result:=TTestEnginePasResolver.Create;
  1713. Result.Filename:=aFilename;
  1714. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1715. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1716. Result.OnLog:=@OnPasResolverLog;
  1717. Result.Hub:=Hub;
  1718. FResolvers.Add(Result);
  1719. end;
  1720. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1721. ): TTestEnginePasResolver;
  1722. begin
  1723. Result:=AddModule(aFilename);
  1724. Result.Source:=Src;
  1725. end;
  1726. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1727. ImplementationSrc: string): TTestEnginePasResolver;
  1728. var
  1729. Src: String;
  1730. begin
  1731. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1732. Src+=LineEnding;
  1733. Src+='interface'+LineEnding;
  1734. Src+=LineEnding;
  1735. Src+=InterfaceSrc;
  1736. Src+='implementation'+LineEnding;
  1737. Src+=LineEnding;
  1738. Src+=ImplementationSrc;
  1739. Src+='end.'+LineEnding;
  1740. Result:=AddModuleWithSrc(aFilename,Src);
  1741. end;
  1742. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1743. var
  1744. Intf, Impl: TStringList;
  1745. begin
  1746. Intf:=TStringList.Create;
  1747. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1748. // unit interface
  1749. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1750. Intf.Add('{$modeswitch externalclass}');
  1751. Intf.Add('type');
  1752. Intf.Add(' integer=longint;');
  1753. Intf.Add(' sizeint=nativeint;');
  1754. //'const',
  1755. //' LineEnding = #10;',
  1756. //' DirectorySeparator = ''/'';',
  1757. //' DriveSeparator = '''';',
  1758. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1759. //' AllowDriveSeparators : set of char = [];',
  1760. if supTObject in Parts then
  1761. Intf.AddStrings([
  1762. 'type',
  1763. ' TClass = class of TObject;',
  1764. ' TObject = class',
  1765. ' constructor Create;',
  1766. ' destructor Destroy; virtual;',
  1767. ' class function ClassType: TClass; assembler;',
  1768. ' class function ClassName: String; assembler;',
  1769. ' class function ClassNameIs(const Name: string): boolean;',
  1770. ' class function ClassParent: TClass; assembler;',
  1771. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1772. ' class function UnitName: String; assembler;',
  1773. ' procedure AfterConstruction; virtual;',
  1774. ' procedure BeforeDestruction;virtual;',
  1775. ' function Equals(Obj: TObject): boolean; virtual;',
  1776. ' function ToString: String; virtual;',
  1777. ' end;']);
  1778. if supTInterfacedObject in Parts then
  1779. Intf.AddStrings([
  1780. ' {$Interfaces COM}',
  1781. ' IUnknown = interface',
  1782. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1783. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1784. ' function _AddRef: Integer;',
  1785. ' function _Release: Integer;',
  1786. ' end;',
  1787. ' IInterface = IUnknown;',
  1788. ' TInterfacedObject = class(TObject,IUnknown)',
  1789. ' protected',
  1790. ' fRefCount: Integer;',
  1791. ' { implement methods of IUnknown }',
  1792. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1793. ' function _AddRef: Integer; virtual;',
  1794. ' function _Release: Integer; virtual;',
  1795. ' end;',
  1796. ' TInterfacedClass = class of TInterfacedObject;',
  1797. '',
  1798. '']);
  1799. if supTVarRec in Parts then
  1800. Intf.AddStrings([
  1801. 'const',
  1802. ' vtInteger = 0;',
  1803. ' vtBoolean = 1;',
  1804. ' vtJSValue = 19;',
  1805. 'type',
  1806. ' PVarRec = ^TVarRec;',
  1807. ' TVarRec = record',
  1808. ' VType : byte;',
  1809. ' VJSValue: JSValue;',
  1810. ' vInteger: longint external name ''VJSValue'';',
  1811. ' vBoolean: boolean external name ''VJSValue'';',
  1812. ' end;',
  1813. ' TVarRecArray = array of TVarRec;',
  1814. 'function VarRecs: TVarRecArray; varargs;',
  1815. '']);
  1816. if supTypeInfo in Parts then
  1817. begin
  1818. Intf.AddStrings([
  1819. 'type',
  1820. ' TTypeKind = (',
  1821. ' tkUnknown, // 0',
  1822. ' tkInteger, // 1',
  1823. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1824. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1825. ' tkEnumeration, // 4',
  1826. ' tkSet, // 5',
  1827. ' tkDouble, // 6',
  1828. ' tkBool, // 7',
  1829. ' tkProcVar, // 8 function or procedure',
  1830. ' tkMethod, // 9 proc var of object',
  1831. ' tkArray, // 10 static array',
  1832. ' tkDynArray, // 11',
  1833. ' tkRecord, // 12',
  1834. ' tkClass, // 13',
  1835. ' tkClassRef, // 14',
  1836. ' tkPointer, // 15',
  1837. ' tkJSValue, // 16',
  1838. ' tkRefToProcVar, // 17 variable of procedure type',
  1839. ' tkInterface, // 18',
  1840. ' //tkObject,',
  1841. ' //tkSString,tkLString,tkAString,tkWString,',
  1842. ' //tkVariant,',
  1843. ' //tkWChar,',
  1844. ' //tkInt64,',
  1845. ' //tkQWord,',
  1846. ' //tkInterfaceRaw,',
  1847. ' //tkUString,tkUChar,',
  1848. ' tkHelper, // 19',
  1849. ' //tkFile,',
  1850. ' tkExtClass // 20',
  1851. ' );',
  1852. ' TTypeKinds = set of TTypeKind;',
  1853. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1854. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1855. ' end;',
  1856. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1857. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1858. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1859. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1860. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1861. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1862. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1863. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1864. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1865. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1866. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1867. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1868. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1869. '']);
  1870. end;
  1871. if supWriteln in Parts then
  1872. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1873. Intf.Add('var');
  1874. Intf.Add(' ExitCode: Longint = 0;');
  1875. // unit implementation
  1876. Impl:=TStringList.Create;
  1877. if supTObject in Parts then
  1878. Impl.AddStrings([
  1879. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1880. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1881. 'asm',
  1882. 'end;',
  1883. 'constructor TObject.Create; begin end;',
  1884. 'destructor TObject.Destroy; begin end;',
  1885. 'class function TObject.ClassType: TClass; assembler;',
  1886. 'asm',
  1887. 'end;',
  1888. 'class function TObject.ClassName: String; assembler;',
  1889. 'asm',
  1890. 'end;',
  1891. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1892. 'begin',
  1893. ' Result:=SameText(Name,ClassName);',
  1894. 'end;',
  1895. 'class function TObject.ClassParent: TClass; assembler;',
  1896. 'asm',
  1897. 'end;',
  1898. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1899. 'asm',
  1900. 'end;',
  1901. 'class function TObject.UnitName: String; assembler;',
  1902. 'asm',
  1903. 'end;',
  1904. 'procedure TObject.AfterConstruction; begin end;',
  1905. 'procedure TObject.BeforeDestruction; begin end;',
  1906. 'function TObject.Equals(Obj: TObject): boolean;',
  1907. 'begin',
  1908. ' Result:=Obj=Self;',
  1909. 'end;',
  1910. 'function TObject.ToString: String;',
  1911. 'begin',
  1912. ' Result:=ClassName;',
  1913. 'end;'
  1914. ]);
  1915. if supTInterfacedObject in Parts then
  1916. Impl.AddStrings([
  1917. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1918. //'begin',
  1919. //'end;',
  1920. 'function TInterfacedObject._AddRef: Integer;',
  1921. 'begin',
  1922. 'end;',
  1923. 'function TInterfacedObject._Release: Integer;',
  1924. 'begin',
  1925. 'end;',
  1926. '']);
  1927. if supTVarRec in Parts then
  1928. Impl.AddStrings([
  1929. 'function VarRecs: TVarRecArray; varargs;',
  1930. 'var',
  1931. ' v: PVarRec;',
  1932. 'begin',
  1933. ' v^.VType:=1;',
  1934. ' v^.VJSValue:=2;',
  1935. 'end;',
  1936. '']);
  1937. try
  1938. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1939. finally
  1940. Intf.Free;
  1941. Impl.Free;
  1942. end;
  1943. end;
  1944. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1945. SystemUnitParts: TSystemUnitParts);
  1946. begin
  1947. if NeedSystemUnit then
  1948. AddSystemUnit(SystemUnitParts)
  1949. else
  1950. Parser.ImplicitUses.Clear;
  1951. Add('program '+ExtractFileUnitName(Filename)+';');
  1952. Add('');
  1953. end;
  1954. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1955. SystemUnitParts: TSystemUnitParts);
  1956. begin
  1957. if NeedSystemUnit then
  1958. AddSystemUnit(SystemUnitParts)
  1959. else
  1960. Parser.ImplicitUses.Clear;
  1961. Add('library '+ExtractFileUnitName(Filename)+';');
  1962. Add('');
  1963. end;
  1964. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1965. SystemUnitParts: TSystemUnitParts);
  1966. begin
  1967. if NeedSystemUnit then
  1968. AddSystemUnit(SystemUnitParts)
  1969. else
  1970. Parser.ImplicitUses.Clear;
  1971. Add('unit Test1;');
  1972. Add('');
  1973. end;
  1974. procedure TCustomTestModule.ConvertModule;
  1975. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1976. out UsesLit: TJSArrayLiteral);
  1977. var
  1978. i: Integer;
  1979. Item: TJSElement;
  1980. Lit: TJSLiteral;
  1981. begin
  1982. UsesLit:=nil;
  1983. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1984. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1985. exit; // null is ok
  1986. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1987. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1988. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1989. begin
  1990. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1991. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1992. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1993. Lit:=TJSLiteral(Item);
  1994. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1995. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1996. end;
  1997. end;
  1998. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1999. out Src: TJSSourceElements);
  2000. var
  2001. FunDecl: TJSFunctionDeclarationStatement;
  2002. FunDef: TJSFuncDef;
  2003. FunBody: TJSFunctionBody;
  2004. begin
  2005. Src:=nil;
  2006. AssertNotNull(ParamName,Arg.Expr);
  2007. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2008. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2009. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2010. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2011. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2012. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2013. AssertNotNull(ParamName+' body',FunDef.Body);
  2014. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2015. FunBody:=FunDef.Body as TJSFunctionBody;
  2016. AssertNotNull(ParamName+' body.A',FunBody.A);
  2017. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2018. Src:=FunBody.A as TJSSourceElements;
  2019. end;
  2020. var
  2021. ModuleNameExpr: TJSLiteral;
  2022. InitFunction: TJSFunctionDeclarationStatement;
  2023. InitAssign: TJSSimpleAssignStatement;
  2024. InitName: String;
  2025. LastNode, FirstNode: TJSElement;
  2026. Arg: TJSArrayLiteralElement;
  2027. IsProg, IsLib: Boolean;
  2028. begin
  2029. if SkipTests then exit;
  2030. IsProg:=false;
  2031. IsLib:=false;
  2032. if Module is TPasProgram then
  2033. IsProg:=true
  2034. else if Module is TPasLibrary then
  2035. IsLib:=true;
  2036. try
  2037. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2038. except
  2039. on E: Exception do
  2040. HandleException(E);
  2041. end;
  2042. if SkipTests then exit;
  2043. if ExpectedErrorClass<>nil then
  2044. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2045. FJSSource:=TStringList.Create;
  2046. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2047. {$IFDEF VerbosePas2JS}
  2048. writeln('TTestModule.ConvertModule JS:');
  2049. write(FJSSource.Text);
  2050. {$ENDIF}
  2051. // rtl.module(...
  2052. if JSModule.Statements.Count<1 then
  2053. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2054. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2055. AssertNotNull('register module call',FirstNode);
  2056. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2057. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2058. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2059. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2060. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2061. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2062. // parameter 'unitname'
  2063. if JSModuleCallArgs.Elements.Count<1 then
  2064. Fail('rtl.module first param unit missing');
  2065. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2066. AssertNotNull('module name param',Arg.Expr);
  2067. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2068. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2069. if IsProg then
  2070. begin
  2071. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2072. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2073. end
  2074. else if IsLib then
  2075. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2076. else
  2077. begin
  2078. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2079. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2080. end;
  2081. // main uses section
  2082. if JSModuleCallArgs.Elements.Count<2 then
  2083. Fail('rtl.module second param main uses missing');
  2084. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2085. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2086. // program/library/interface function()
  2087. if JSModuleCallArgs.Elements.Count<3 then
  2088. Fail('rtl.module third param intf-function missing');
  2089. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2090. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2091. // search for $mod.$init or $mod.$main - the last statement
  2092. if IsProg or IsLib then
  2093. begin
  2094. InitName:='$main';
  2095. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2096. end
  2097. else
  2098. InitName:='$init';
  2099. InitAssign:=nil;
  2100. InitFunction:=nil;
  2101. FJSInitBody:=nil;
  2102. if JSModuleSrc.Statements.Count>0 then
  2103. begin
  2104. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2105. if LastNode is TJSSimpleAssignStatement then
  2106. begin
  2107. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2108. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2109. begin
  2110. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2111. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2112. end
  2113. else if IsProg or IsLib then
  2114. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2115. end;
  2116. end;
  2117. // optional: implementation uses section
  2118. if JSModuleCallArgs.Elements.Count<4 then
  2119. exit;
  2120. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2121. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2122. end;
  2123. procedure TCustomTestModule.ConvertProgram;
  2124. begin
  2125. Add('end.');
  2126. ParseProgram;
  2127. ConvertModule;
  2128. end;
  2129. procedure TCustomTestModule.ConvertLibrary;
  2130. begin
  2131. Add('end.');
  2132. ParseLibrary;
  2133. ConvertModule;
  2134. end;
  2135. procedure TCustomTestModule.ConvertUnit;
  2136. begin
  2137. Add('end.');
  2138. ParseUnit;
  2139. ConvertModule;
  2140. end;
  2141. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2142. begin
  2143. Result:=tcmodules.JSToStr(El);
  2144. end;
  2145. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2146. DottedName: string);
  2147. begin
  2148. if DottedName='' then
  2149. begin
  2150. AssertNull(Msg,El);
  2151. end
  2152. else
  2153. begin
  2154. AssertNotNull(Msg,El);
  2155. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2156. end;
  2157. end;
  2158. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2159. begin
  2160. if El=nil then
  2161. Result:=''
  2162. else if El is TJSPrimaryExpressionIdent then
  2163. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2164. else if El is TJSDotMemberExpression then
  2165. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2166. else
  2167. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2168. end;
  2169. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2170. InitStatements: string; ImplStatements: string);
  2171. var
  2172. ActualSrc, ExpectedSrc, InitName: String;
  2173. IsProg, IsLib: Boolean;
  2174. begin
  2175. ActualSrc:=JSToStr(JSModuleSrc);
  2176. if coUseStrict in Converter.Options then
  2177. ExpectedSrc:='"use strict";'+LineEnding
  2178. else
  2179. ExpectedSrc:='';
  2180. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2181. ExpectedSrc:=ExpectedSrc+Statements;
  2182. // unit implementation
  2183. if (Trim(ImplStatements)<>'') then
  2184. ExpectedSrc:=ExpectedSrc+LineEnding
  2185. +'$mod.$implcode = function () {'+LineEnding
  2186. +ImplStatements
  2187. +'};'+LineEnding;
  2188. // program main or unit initialization
  2189. IsProg:=false;
  2190. IsLib:=false;
  2191. if Module is TPasProgram then
  2192. IsProg:=true
  2193. else if Module is TPasLibrary then
  2194. IsLib:=true;
  2195. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2196. begin
  2197. if IsProg or IsLib then
  2198. InitName:='$main'
  2199. else
  2200. InitName:='$init';
  2201. ExpectedSrc:=ExpectedSrc+LineEnding
  2202. +'$mod.'+InitName+' = function () {'+LineEnding
  2203. +InitStatements
  2204. +'};'+LineEnding;
  2205. end;
  2206. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2207. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2208. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2209. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2210. end;
  2211. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2212. var
  2213. ActualSrc: String;
  2214. begin
  2215. ActualSrc:=JSToStr(JSModule);
  2216. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2217. end;
  2218. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2219. // search diff, ignore changes in spaces
  2220. var
  2221. s: string;
  2222. begin
  2223. if CheckSrcDiff(Expected,Actual,s) then exit;
  2224. Fail(Msg+': '+s);
  2225. end;
  2226. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2227. var
  2228. aResolver: TTestEnginePasResolver;
  2229. aConverter: TPasToJSConverter;
  2230. aJSModule: TJSSourceElements;
  2231. ActualSrc: String;
  2232. begin
  2233. aResolver:=GetResolver(Filename);
  2234. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2235. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2236. {$IFDEF VerbosePas2JS}
  2237. writeln('CheckUnit '+Filename+' converting ...');
  2238. {$ENDIF}
  2239. aConverter:=CreateConverter;
  2240. aJSModule:=nil;
  2241. try
  2242. try
  2243. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2244. except
  2245. on E: Exception do
  2246. HandleException(E);
  2247. end;
  2248. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2249. {$IFDEF VerbosePas2JS}
  2250. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2251. write(aResolver.Source);
  2252. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2253. write(ActualSrc);
  2254. {$ENDIF}
  2255. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2256. finally
  2257. aJSModule.Free;
  2258. aConverter.Free;
  2259. end;
  2260. end;
  2261. procedure TCustomTestModule.CheckReferenceDirectives;
  2262. var
  2263. CurFilename: string;
  2264. LineNumber: Integer;
  2265. SrcLine: String;
  2266. CommentStartP, CommentEndP: PChar;
  2267. procedure RaiseError(Msg: string; p: PChar);
  2268. begin
  2269. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2270. end;
  2271. procedure AddMarker(Marker: PSrcMarker);
  2272. begin
  2273. if LastSrcMarker<>nil then
  2274. LastSrcMarker^.Next:=Marker
  2275. else
  2276. FirstSrcMarker:=Marker;
  2277. LastSrcMarker:=Marker;
  2278. end;
  2279. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2280. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2281. begin
  2282. New(Result);
  2283. Result^.Kind:=Kind;
  2284. Result^.Filename:=aFilename;
  2285. Result^.Row:=aLine;
  2286. Result^.StartCol:=aStartCol;
  2287. Result^.EndCol:=aEndCol;
  2288. Result^.Identifier:=Identifier;
  2289. Result^.Next:=nil;
  2290. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2291. AddMarker(Result);
  2292. end;
  2293. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2294. const Identifier: string): PSrcMarker;
  2295. var
  2296. TokenStart, p: PChar;
  2297. begin
  2298. p:=CommentEndP;
  2299. ReadNextPascalToken(p,TokenStart,false,false);
  2300. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2301. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2302. end;
  2303. function ReadIdentifier(var p: PChar): string;
  2304. var
  2305. StartP: PChar;
  2306. begin
  2307. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2308. RaiseError('identifier expected',p);
  2309. StartP:=p;
  2310. inc(p);
  2311. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2312. Result:='';
  2313. SetLength(Result,p-StartP);
  2314. Move(StartP^,Result[1],length(Result));
  2315. end;
  2316. procedure AddLabel;
  2317. var
  2318. Identifier: String;
  2319. p: PChar;
  2320. begin
  2321. p:=CommentStartP+2;
  2322. Identifier:=ReadIdentifier(p);
  2323. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2324. if FindSrcLabel(Identifier)<>nil then
  2325. RaiseError('duplicate label "'+Identifier+'"',p);
  2326. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2327. end;
  2328. procedure AddResolverReference;
  2329. var
  2330. Identifier: String;
  2331. p: PChar;
  2332. begin
  2333. p:=CommentStartP+2;
  2334. Identifier:=ReadIdentifier(p);
  2335. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2336. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2337. end;
  2338. procedure AddDirectReference;
  2339. var
  2340. Identifier: String;
  2341. p: PChar;
  2342. begin
  2343. p:=CommentStartP+2;
  2344. Identifier:=ReadIdentifier(p);
  2345. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2346. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2347. end;
  2348. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2349. var
  2350. p: PChar;
  2351. IsDirective: Boolean;
  2352. begin
  2353. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2354. CurFilename:=aFilename;
  2355. // parse code, find all labels
  2356. LineNumber:=0;
  2357. while LineNumber<SrcLines.Count do
  2358. begin
  2359. inc(LineNumber);
  2360. SrcLine:=SrcLines[LineNumber-1];
  2361. if SrcLine='' then continue;
  2362. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2363. p:=PChar(SrcLine);
  2364. repeat
  2365. case p^ of
  2366. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2367. '{':
  2368. begin
  2369. CommentStartP:=p;
  2370. inc(p);
  2371. IsDirective:=p^ in ['#','@','='];
  2372. // skip to end of comment
  2373. repeat
  2374. case p^ of
  2375. #0:
  2376. if (p-PChar(SrcLine)=length(SrcLine)) then
  2377. begin
  2378. // multi line comment
  2379. if IsDirective then
  2380. RaiseError('directive missing closing bracket',CommentStartP);
  2381. repeat
  2382. inc(LineNumber);
  2383. if LineNumber>SrcLines.Count then exit;
  2384. SrcLine:=SrcLines[LineNumber-1];
  2385. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2386. until SrcLine<>'';
  2387. p:=PChar(SrcLine);
  2388. continue;
  2389. end;
  2390. '}':
  2391. begin
  2392. inc(p);
  2393. break;
  2394. end;
  2395. end;
  2396. inc(p);
  2397. until false;
  2398. CommentEndP:=p;
  2399. case CommentStartP[1] of
  2400. '#': AddLabel;
  2401. '@': AddResolverReference;
  2402. '=': AddDirectReference;
  2403. end;
  2404. p:=CommentEndP;
  2405. continue;
  2406. end;
  2407. '/':
  2408. if p[1]='/' then
  2409. break; // rest of line is comment -> skip
  2410. end;
  2411. inc(p);
  2412. until false;
  2413. end;
  2414. end;
  2415. procedure CheckResolverReference(aMarker: PSrcMarker);
  2416. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2417. var
  2418. aLabel: PSrcMarker;
  2419. ReferenceElements, LabelElements: TFPList;
  2420. i, j, aLine, aCol: Integer;
  2421. El, Ref, LabelEl: TPasElement;
  2422. begin
  2423. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2424. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2425. if aLabel=nil then
  2426. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2427. LabelElements:=nil;
  2428. ReferenceElements:=nil;
  2429. try
  2430. LabelElements:=FindElementsAt(aLabel);
  2431. ReferenceElements:=FindElementsAt(aMarker);
  2432. for i:=0 to ReferenceElements.Count-1 do
  2433. begin
  2434. El:=TPasElement(ReferenceElements[i]);
  2435. Ref:=nil;
  2436. if El.CustomData is TResolvedReference then
  2437. Ref:=TResolvedReference(El.CustomData).Declaration
  2438. else if El.CustomData is TPasPropertyScope then
  2439. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2440. else if El.CustomData is TPasSpecializeTypeData then
  2441. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2442. if Ref<>nil then
  2443. for j:=0 to LabelElements.Count-1 do
  2444. begin
  2445. LabelEl:=TPasElement(LabelElements[j]);
  2446. if Ref=LabelEl then
  2447. exit; // success
  2448. end;
  2449. end;
  2450. // failure write candidates
  2451. for i:=0 to ReferenceElements.Count-1 do
  2452. begin
  2453. El:=TPasElement(ReferenceElements[i]);
  2454. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2455. write(' El=',GetObjName(El));
  2456. if EL is TPrimitiveExpr then
  2457. begin
  2458. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2459. end;
  2460. Ref:=nil;
  2461. if El.CustomData is TResolvedReference then
  2462. Ref:=TResolvedReference(El.CustomData).Declaration
  2463. else if El.CustomData is TPasPropertyScope then
  2464. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2465. else if El.CustomData is TPasSpecializeTypeData then
  2466. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2467. if Ref<>nil then
  2468. begin
  2469. write(' Decl=',GetObjName(Ref));
  2470. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2471. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2472. end
  2473. else
  2474. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2475. writeln;
  2476. end;
  2477. for i:=0 to LabelElements.Count-1 do
  2478. begin
  2479. El:=TPasElement(LabelElements[i]);
  2480. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2481. write(' El=',GetObjName(El));
  2482. writeln;
  2483. end;
  2484. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2485. finally
  2486. LabelElements.Free;
  2487. ReferenceElements.Free;
  2488. end;
  2489. end;
  2490. procedure CheckDirectReference(aMarker: PSrcMarker);
  2491. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2492. var
  2493. aLabel: PSrcMarker;
  2494. ReferenceElements, LabelElements: TFPList;
  2495. i, LabelLine, LabelCol, j: Integer;
  2496. El, LabelEl: TPasElement;
  2497. DeclEl, TypeEl: TPasType;
  2498. begin
  2499. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2500. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2501. if aLabel=nil then
  2502. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2503. LabelElements:=nil;
  2504. ReferenceElements:=nil;
  2505. try
  2506. //writeln('CheckDirectReference finding elements at label ...');
  2507. LabelElements:=FindElementsAt(aLabel);
  2508. //writeln('CheckDirectReference finding elements at reference ...');
  2509. ReferenceElements:=FindElementsAt(aMarker);
  2510. for i:=0 to ReferenceElements.Count-1 do
  2511. begin
  2512. El:=TPasElement(ReferenceElements[i]);
  2513. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2514. if El.ClassType=TPasVariable then
  2515. begin
  2516. if TPasVariable(El).VarType=nil then
  2517. begin
  2518. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2519. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2520. end;
  2521. TypeEl:=TPasVariable(El).VarType;
  2522. for j:=0 to LabelElements.Count-1 do
  2523. begin
  2524. LabelEl:=TPasElement(LabelElements[j]);
  2525. if TypeEl=LabelEl then
  2526. exit; // success
  2527. end;
  2528. end
  2529. else if El is TPasAliasType then
  2530. begin
  2531. DeclEl:=TPasAliasType(El).DestType;
  2532. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2533. if (aLabel^.Filename=DeclEl.SourceFilename)
  2534. and (integer(aLabel^.Row)=LabelLine)
  2535. and (aLabel^.StartCol<=LabelCol)
  2536. and (aLabel^.EndCol>=LabelCol) then
  2537. exit; // success
  2538. end
  2539. else if El.ClassType=TPasArgument then
  2540. begin
  2541. TypeEl:=TPasArgument(El).ArgType;
  2542. for j:=0 to LabelElements.Count-1 do
  2543. begin
  2544. LabelEl:=TPasElement(LabelElements[j]);
  2545. if TypeEl=LabelEl then
  2546. exit; // success
  2547. end;
  2548. end;
  2549. end;
  2550. // failed -> show candidates
  2551. writeln('CheckDirectReference failed: Labels:');
  2552. for j:=0 to LabelElements.Count-1 do
  2553. begin
  2554. LabelEl:=TPasElement(LabelElements[j]);
  2555. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2556. end;
  2557. writeln('CheckDirectReference failed: References:');
  2558. for i:=0 to ReferenceElements.Count-1 do
  2559. begin
  2560. El:=TPasElement(ReferenceElements[i]);
  2561. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2562. //if EL is TPasVariable then
  2563. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2564. end;
  2565. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2566. finally
  2567. LabelElements.Free;
  2568. ReferenceElements.Free;
  2569. end;
  2570. end;
  2571. var
  2572. aMarker: PSrcMarker;
  2573. i: Integer;
  2574. SrcLines: TStringList;
  2575. begin
  2576. Module.ForEachCall(@OnCheckElementParent,nil);
  2577. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2578. // find all markers
  2579. for i:=0 to FileResolver.Streams.Count-1 do
  2580. begin
  2581. GetSrc(i,SrcLines,CurFilename);
  2582. ParseCode(SrcLines,CurFilename);
  2583. SrcLines.Free;
  2584. end;
  2585. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2586. // check references
  2587. aMarker:=FirstSrcMarker;
  2588. while aMarker<>nil do
  2589. begin
  2590. case aMarker^.Kind of
  2591. mkResolverReference: CheckResolverReference(aMarker);
  2592. mkDirectReference: CheckDirectReference(aMarker);
  2593. end;
  2594. aMarker:=aMarker^.Next;
  2595. end;
  2596. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2597. end;
  2598. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2599. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2600. var
  2601. i: Integer;
  2602. Item: TTestHintMessage;
  2603. Expected,Actual: string;
  2604. begin
  2605. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2606. for i:=0 to MsgCount-1 do
  2607. begin
  2608. Item:=Msgs[i];
  2609. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2610. if (Marker<>nil) then
  2611. begin
  2612. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2613. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2614. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2615. end;
  2616. // found
  2617. FHintMsgsGood.Add(Item);
  2618. str(Item.MsgType,Actual);
  2619. str(MsgType,Expected);
  2620. AssertEquals('MsgType',Expected,Actual);
  2621. exit;
  2622. end;
  2623. // needed message missing -> show emitted messages
  2624. WriteSources('',0,0);
  2625. for i:=0 to MsgCount-1 do
  2626. begin
  2627. Item:=Msgs[i];
  2628. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2629. ' ('+IntToStr(Item.MsgNumber),')');
  2630. if Marker<>nil then
  2631. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2632. writeln(' {',Item.Msg,'}');
  2633. end;
  2634. str(MsgType,Expected);
  2635. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2636. if Marker<>nil then
  2637. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2638. Actual:=Actual+' '+Msg;
  2639. Fail(Actual);
  2640. end;
  2641. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2642. );
  2643. var
  2644. i: Integer;
  2645. s, Txt: String;
  2646. Msg: TTestHintMessage;
  2647. begin
  2648. for i:=0 to MsgCount-1 do
  2649. begin
  2650. Msg:=Msgs[i];
  2651. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2652. s:='';
  2653. str(Msg.MsgType,s);
  2654. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2655. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2656. if WithSourcePos then
  2657. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2658. Txt:=Txt+' {'+Msg.Msg+'}';
  2659. Fail(Txt);
  2660. end;
  2661. end;
  2662. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2663. MsgNumber: integer);
  2664. begin
  2665. ExpectedErrorClass:=EScannerError;
  2666. ExpectedErrorMsg:=Msg;
  2667. ExpectedErrorNumber:=MsgNumber;
  2668. end;
  2669. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2670. MsgNumber: integer);
  2671. begin
  2672. ExpectedErrorClass:=EParserError;
  2673. ExpectedErrorMsg:=Msg;
  2674. ExpectedErrorNumber:=MsgNumber;
  2675. end;
  2676. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2677. MsgNumber: integer);
  2678. begin
  2679. ExpectedErrorClass:=EPasResolve;
  2680. ExpectedErrorMsg:=Msg;
  2681. ExpectedErrorNumber:=MsgNumber;
  2682. end;
  2683. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2684. MsgNumber: integer);
  2685. begin
  2686. ExpectedErrorClass:=EPas2JS;
  2687. ExpectedErrorMsg:=Msg;
  2688. ExpectedErrorNumber:=MsgNumber;
  2689. end;
  2690. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2691. var
  2692. MsgNumber: Integer;
  2693. Msg: String;
  2694. begin
  2695. Result:=false;
  2696. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2697. Msg:=E.Message;
  2698. if E is EPas2JS then
  2699. MsgNumber:=EPas2JS(E).MsgNumber
  2700. else if E is EPasResolve then
  2701. MsgNumber:=EPasResolve(E).MsgNumber
  2702. else if E is EParserError then
  2703. MsgNumber:=Parser.LastMsgNumber
  2704. else if E is EScannerError then
  2705. begin
  2706. MsgNumber:=Scanner.LastMsgNumber;
  2707. Msg:=Scanner.LastMsg;
  2708. end
  2709. else
  2710. MsgNumber:=0;
  2711. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2712. if Result then
  2713. SkipTests:=true;
  2714. end;
  2715. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2716. const aFilename: string; aRow, aCol: integer);
  2717. var
  2718. s: String;
  2719. begin
  2720. WriteSources(aFilename,aRow,aCol);
  2721. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2722. writeln('ERROR: ',s);
  2723. Fail(s);
  2724. end;
  2725. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2726. aMarker: PSrcMarker);
  2727. begin
  2728. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2729. end;
  2730. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2731. begin
  2732. if IsErrorExpected(E) then exit;
  2733. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2734. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2735. +' '+Scanner.CurFilename
  2736. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2737. FailException(E);
  2738. end;
  2739. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2740. begin
  2741. if IsErrorExpected(E) then exit;
  2742. WriteSources(E.Filename,E.Row,E.Column);
  2743. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2744. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2745. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2746. );
  2747. FailException(E);
  2748. end;
  2749. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2750. var
  2751. P: TPasSourcePos;
  2752. begin
  2753. if IsErrorExpected(E) then exit;
  2754. P:=E.SourcePos;
  2755. WriteSources(P.FileName,P.Row,P.Column);
  2756. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2757. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2758. FailException(E);
  2759. end;
  2760. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2761. var
  2762. Row, Col: integer;
  2763. begin
  2764. if IsErrorExpected(E) then exit;
  2765. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2766. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2767. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2768. +' '+E.PasElement.SourceFilename
  2769. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2770. FailException(E);
  2771. end;
  2772. procedure TCustomTestModule.HandleException(E: Exception);
  2773. begin
  2774. if E is EScannerError then
  2775. HandleScannerError(EScannerError(E))
  2776. else if E is EParserError then
  2777. HandleParserError(EParserError(E))
  2778. else if E is EPasResolve then
  2779. HandlePasResolveError(EPasResolve(E))
  2780. else if E is EPas2JS then
  2781. HandlePas2JSError(EPas2JS(E))
  2782. else
  2783. begin
  2784. if IsErrorExpected(E) then exit;
  2785. if not (E is EAssertionFailedError) then
  2786. begin
  2787. WriteSources('',0,0);
  2788. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2789. end;
  2790. FailException(E);
  2791. end;
  2792. end;
  2793. procedure TCustomTestModule.FailException(E: Exception);
  2794. var
  2795. MsgNumber: Integer;
  2796. begin
  2797. if ExpectedErrorClass<>nil then
  2798. begin
  2799. if FExpectedErrorClass=E.ClassType then
  2800. begin
  2801. if E is EPas2JS then
  2802. MsgNumber:=EPas2JS(E).MsgNumber
  2803. else if E is EPasResolve then
  2804. MsgNumber:=EPasResolve(E).MsgNumber
  2805. else if E is EParserError then
  2806. MsgNumber:=Parser.LastMsgNumber
  2807. else if E is EScannerError then
  2808. MsgNumber:=Scanner.LastMsgNumber
  2809. else
  2810. MsgNumber:=0;
  2811. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2812. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2813. ExpectedErrorNumber,MsgNumber);
  2814. end else begin
  2815. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2816. end;
  2817. end;
  2818. Fail(E.Message);
  2819. end;
  2820. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2821. aCol: integer);
  2822. var
  2823. IsSrc: Boolean;
  2824. i, j: Integer;
  2825. SrcLines: TStringList;
  2826. Line: string;
  2827. aModule: TTestEnginePasResolver;
  2828. begin
  2829. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2830. for i:=0 to ResolverCount-1 do
  2831. begin
  2832. aModule:=Resolvers[i];
  2833. SrcLines:=TStringList.Create;
  2834. try
  2835. SrcLines.Text:=aModule.Source;
  2836. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2837. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2838. for j:=1 to SrcLines.Count do
  2839. begin
  2840. Line:=SrcLines[j-1];
  2841. if IsSrc and (j=aRow) then
  2842. begin
  2843. write('*');
  2844. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2845. end;
  2846. writeln(Format('%:4d: ',[j]),Line);
  2847. end;
  2848. finally
  2849. SrcLines.Free;
  2850. end;
  2851. end;
  2852. end;
  2853. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2854. var
  2855. i: Integer;
  2856. begin
  2857. for i:=0 to ResolverCount-1 do
  2858. if Filename=Resolvers[i].Filename then exit(i);
  2859. Result:=-1;
  2860. end;
  2861. function TCustomTestModule.GetResolver(const Filename: string
  2862. ): TTestEnginePasResolver;
  2863. var
  2864. i: Integer;
  2865. begin
  2866. i:=IndexOfResolver(Filename);
  2867. if i<0 then exit(nil);
  2868. Result:=Resolvers[i];
  2869. end;
  2870. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2871. out aFilename: string);
  2872. var
  2873. aStream: TStream;
  2874. begin
  2875. SrcLines:=TStringList.Create;
  2876. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2877. aStream.Position:=0;
  2878. SrcLines.LoadFromStream(aStream);
  2879. aFilename:=FileResolver.Streams[Index];
  2880. end;
  2881. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2882. aEndCol: integer): TFPList;
  2883. var
  2884. ok: Boolean;
  2885. FoundRefs: TTestResolverReferenceData;
  2886. i: Integer;
  2887. CurResolver: TTestEnginePasResolver;
  2888. begin
  2889. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2890. FoundRefs:=Default(TTestResolverReferenceData);
  2891. FoundRefs.Filename:=aFilename;
  2892. FoundRefs.Row:=aLine;
  2893. FoundRefs.StartCol:=aStartCol;
  2894. FoundRefs.EndCol:=aEndCol;
  2895. FoundRefs.Found:=TFPList.Create;
  2896. ok:=false;
  2897. try
  2898. // find all markers
  2899. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2900. for i:=0 to ResolverCount-1 do
  2901. begin
  2902. CurResolver:=Resolvers[i];
  2903. if CurResolver.Module=Module then continue;
  2904. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2905. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2906. end;
  2907. ok:=true;
  2908. finally
  2909. if not ok then
  2910. FreeAndNil(FoundRefs.Found);
  2911. end;
  2912. Result:=FoundRefs.Found;
  2913. FoundRefs.Found:=nil;
  2914. end;
  2915. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2916. ErrorOnNoElements: boolean): TFPList;
  2917. begin
  2918. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2919. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2920. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2921. end;
  2922. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2923. begin
  2924. Result:=FirstSrcMarker;
  2925. while Result<>nil do
  2926. begin
  2927. if (Result^.Kind=mkLabel)
  2928. and (CompareText(Result^.Identifier,Identifier)=0) then
  2929. exit;
  2930. Result:=Result^.Next;
  2931. end;
  2932. end;
  2933. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2934. ErrorOnNoElements: boolean): TFPList;
  2935. var
  2936. SrcLabel: PSrcMarker;
  2937. begin
  2938. SrcLabel:=FindSrcLabel(Identifier);
  2939. if SrcLabel=nil then
  2940. Fail('missing label "'+Identifier+'"');
  2941. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2942. end;
  2943. function TCustomTestModule.GetDefaultNamespace: string;
  2944. var
  2945. C: TClass;
  2946. begin
  2947. Result:='';
  2948. if FModule=nil then exit;
  2949. C:=FModule.ClassType;
  2950. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2951. Result:=ResolverEngine.DefaultNameSpace;
  2952. end;
  2953. constructor TCustomTestModule.Create;
  2954. begin
  2955. inherited Create;
  2956. FHintMsgs:=TObjectList.Create(true);
  2957. FHintMsgsGood:=TFPList.Create;
  2958. end;
  2959. destructor TCustomTestModule.Destroy;
  2960. begin
  2961. FreeAndNil(FHintMsgs);
  2962. FreeAndNil(FHintMsgsGood);
  2963. inherited Destroy;
  2964. end;
  2965. { TTestModule }
  2966. procedure TTestModule.TestReservedWords;
  2967. var
  2968. i: integer;
  2969. begin
  2970. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2971. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2972. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2973. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2974. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2975. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2976. end;
  2977. procedure TTestModule.TestEmptyProgram;
  2978. begin
  2979. StartProgram(false);
  2980. Add('begin');
  2981. ConvertProgram;
  2982. CheckSource('TestEmptyProgram','','');
  2983. end;
  2984. procedure TTestModule.TestEmptyProgramUseStrict;
  2985. begin
  2986. Converter.Options:=Converter.Options+[coUseStrict];
  2987. StartProgram(false);
  2988. Add('begin');
  2989. ConvertProgram;
  2990. CheckSource('TestEmptyProgramUseStrict','','');
  2991. end;
  2992. procedure TTestModule.TestEmptyUnit;
  2993. begin
  2994. StartUnit(false);
  2995. Add('interface');
  2996. Add('implementation');
  2997. ConvertUnit;
  2998. CheckSource('TestEmptyUnit',
  2999. LinesToStr([
  3000. ]),
  3001. '');
  3002. end;
  3003. procedure TTestModule.TestEmptyUnitUseStrict;
  3004. begin
  3005. Converter.Options:=Converter.Options+[coUseStrict];
  3006. StartUnit(false);
  3007. Add('interface');
  3008. Add('implementation');
  3009. ConvertUnit;
  3010. CheckSource('TestEmptyUnitUseStrict',
  3011. LinesToStr([
  3012. ''
  3013. ]),
  3014. '');
  3015. end;
  3016. procedure TTestModule.TestDottedUnitNames;
  3017. begin
  3018. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3019. LinesToStr([
  3020. 'var iV: longint;'
  3021. ]),
  3022. '');
  3023. FFilename:='ns1.test1.pp';
  3024. StartProgram(true);
  3025. Add('uses unIt2;');
  3026. Add('var');
  3027. Add(' i: longint;');
  3028. Add('begin');
  3029. Add(' i:=iv;');
  3030. Add(' i:=uNit2.iv;');
  3031. Add(' i:=Ns1.TEst1.i;');
  3032. ConvertProgram;
  3033. CheckSource('TestDottedUnitNames',
  3034. LinesToStr([
  3035. 'this.i = 0;',
  3036. '']),
  3037. LinesToStr([ // this.$init
  3038. '$mod.i = pas["NS1.Unit2"].iV;',
  3039. '$mod.i = pas["NS1.Unit2"].iV;',
  3040. '$mod.i = $mod.i;',
  3041. '']) );
  3042. end;
  3043. procedure TTestModule.TestDottedUnitNameImpl;
  3044. begin
  3045. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3046. LinesToStr([
  3047. 'type',
  3048. ' TObject = class end;',
  3049. ' TTestA = class',
  3050. ' end;'
  3051. ]),
  3052. LinesToStr(['uses TEST.UnitB;'])
  3053. );
  3054. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3055. LinesToStr([
  3056. 'uses TEST.UnitA;',
  3057. 'type TTestB = class(TTestA);'
  3058. ]),
  3059. ''
  3060. );
  3061. StartProgram(true);
  3062. Add('uses TEST.UnitA;');
  3063. Add('begin');
  3064. ConvertProgram;
  3065. CheckSource('TestDottedUnitNameImpl',
  3066. LinesToStr([
  3067. '']),
  3068. LinesToStr([ // this.$init
  3069. '']) );
  3070. CheckUnit('TEST.UnitA.pas',
  3071. LinesToStr([
  3072. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3073. ' var $mod = this;',
  3074. ' rtl.createClass(this, "TObject", null, function () {',
  3075. ' this.$init = function () {',
  3076. ' };',
  3077. ' this.$final = function () {',
  3078. ' };',
  3079. ' });',
  3080. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3081. ' });',
  3082. '}, ["TEST.UnitB"]);'
  3083. ]));
  3084. CheckUnit('TEST.UnitB.pas',
  3085. LinesToStr([
  3086. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3087. ' var $mod = this;',
  3088. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3089. ' });',
  3090. '});'
  3091. ]));
  3092. end;
  3093. procedure TTestModule.TestDottedUnitExpr;
  3094. begin
  3095. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3096. LinesToStr([
  3097. 'procedure DoIt;'
  3098. ]),
  3099. 'procedure DoIt; begin end;');
  3100. FFilename:='Ns1.SubNs1.Test1.pp';
  3101. StartProgram(true);
  3102. Add('uses Ns2.sUbnS2.unIt2;');
  3103. Add('var');
  3104. Add(' i: longint;');
  3105. Add('begin');
  3106. Add(' ns2.subns2.unit2.doit;');
  3107. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3108. ConvertProgram;
  3109. CheckSource('TestDottedUnitExpr',
  3110. LinesToStr([
  3111. 'this.i = 0;',
  3112. '']),
  3113. LinesToStr([ // this.$init
  3114. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3115. '$mod.i = $mod.i;',
  3116. '']) );
  3117. end;
  3118. procedure TTestModule.Test_ModeFPCFail;
  3119. begin
  3120. StartProgram(false);
  3121. Add('{$mode FPC}');
  3122. Add('begin');
  3123. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3124. ConvertProgram;
  3125. end;
  3126. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3127. begin
  3128. StartProgram(false);
  3129. Add('{$modeswitch cblocks-}');
  3130. Add('begin');
  3131. ConvertProgram;
  3132. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3133. CheckResolverUnexpectedHints();
  3134. end;
  3135. procedure TTestModule.TestUnit_UseSystem;
  3136. begin
  3137. StartUnit(true);
  3138. Add([
  3139. 'interface',
  3140. 'var i: integer;',
  3141. 'implementation']);
  3142. ConvertUnit;
  3143. CheckSource('TestUnit_UseSystem',
  3144. LinesToStr([
  3145. 'this.i = 0;',
  3146. '']),
  3147. LinesToStr([
  3148. '']) );
  3149. end;
  3150. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3151. begin
  3152. AddModuleWithIntfImplSrc('unit1.pp',
  3153. LinesToStr([
  3154. 'type number = longint;']),
  3155. LinesToStr([
  3156. 'uses test1;',
  3157. 'procedure DoIt;',
  3158. 'begin',
  3159. ' i:=3;',
  3160. 'end;']));
  3161. StartUnit(true);
  3162. Add([
  3163. 'interface',
  3164. 'uses unit1;',
  3165. 'var i: number;',
  3166. 'implementation']);
  3167. ConvertUnit;
  3168. CheckSource('TestUnit_Intf1Impl2Intf1',
  3169. LinesToStr([
  3170. 'this.i = 0;',
  3171. '']),
  3172. LinesToStr([
  3173. '']) );
  3174. end;
  3175. procedure TTestModule.TestIncludeVersion;
  3176. begin
  3177. StartProgram(false);
  3178. Add([
  3179. 'var',
  3180. ' s: string;',
  3181. ' i: word;',
  3182. 'begin',
  3183. ' s:={$I %line%};',
  3184. ' i:={$I %linenum%};',
  3185. ' s:={$I %currentroutine%};',
  3186. ' s:={$I %pas2jsversion%};',
  3187. ' s:={$I %pas2jstarget%};',
  3188. ' s:={$I %pas2jstargetos%};',
  3189. ' s:={$I %pas2jstargetcpu%};',
  3190. ' s:={$I %file%};',
  3191. '']);
  3192. ConvertProgram;
  3193. CheckSource('TestIncludeVersion',
  3194. LinesToStr([
  3195. 'this.s="";',
  3196. 'this.i = 0;']),
  3197. LinesToStr([
  3198. '$mod.s = "7";',
  3199. '$mod.i = 8;',
  3200. '$mod.s = "<anonymous>";',
  3201. '$mod.s = "Comp.Ver.tcmodules";',
  3202. '$mod.s = "Browser";',
  3203. '$mod.s = "Browser";',
  3204. '$mod.s = "ECMAScript5";',
  3205. '$mod.s = "test1.pp";',
  3206. '']));
  3207. end;
  3208. procedure TTestModule.TestVarInt;
  3209. begin
  3210. StartProgram(false);
  3211. Add('var MyI: longint;');
  3212. Add('begin');
  3213. ConvertProgram;
  3214. CheckSource('TestVarInt','this.MyI=0;','');
  3215. end;
  3216. procedure TTestModule.TestVarBaseTypes;
  3217. begin
  3218. StartProgram(false);
  3219. Add('var');
  3220. Add(' i: longint;');
  3221. Add(' s: string;');
  3222. Add(' c: char;');
  3223. Add(' b: boolean;');
  3224. Add(' d: double;');
  3225. Add(' i2: longint = 3;');
  3226. Add(' s2: string = ''foo'';');
  3227. Add(' c2: char = ''4'';');
  3228. Add(' b2: boolean = true;');
  3229. Add(' d2: double = 5.6;');
  3230. Add(' i3: longint = $707;');
  3231. Add(' i4: nativeint = 9007199254740991;');
  3232. Add(' i5: nativeint = -9007199254740991-1;');
  3233. Add(' i6: nativeint = $fffffffffffff;');
  3234. Add(' i7: nativeint = -$fffffffffffff-1;');
  3235. Add(' i8: byte = 00;');
  3236. Add(' u8: nativeuint = $fffffffffffff;');
  3237. Add(' u9: nativeuint = $0000000000000;');
  3238. Add(' u10: nativeuint = $00ff00;');
  3239. Add('begin');
  3240. ConvertProgram;
  3241. CheckSource('TestVarBaseTypes',
  3242. LinesToStr([
  3243. 'this.i = 0;',
  3244. 'this.s = "";',
  3245. 'this.c = "";',
  3246. 'this.b = false;',
  3247. 'this.d = 0.0;',
  3248. 'this.i2 = 3;',
  3249. 'this.s2 = "foo";',
  3250. 'this.c2 = "4";',
  3251. 'this.b2 = true;',
  3252. 'this.d2 = 5.6;',
  3253. 'this.i3 = 0x707;',
  3254. 'this.i4 = 9007199254740991;',
  3255. 'this.i5 = -9007199254740991-1;',
  3256. 'this.i6 = 0xfffffffffffff;',
  3257. 'this.i7 =-0xfffffffffffff-1;',
  3258. 'this.i8 = 0;',
  3259. 'this.u8 = 0xfffffffffffff;',
  3260. 'this.u9 = 0x0;',
  3261. 'this.u10 = 0xff00;'
  3262. ]),
  3263. '');
  3264. end;
  3265. procedure TTestModule.TestBaseTypeSingleFail;
  3266. begin
  3267. StartProgram(false);
  3268. Add('var s: single;');
  3269. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3270. ConvertProgram;
  3271. end;
  3272. procedure TTestModule.TestBaseTypeExtendedFail;
  3273. begin
  3274. StartProgram(false);
  3275. Add('var e: extended;');
  3276. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3277. ConvertProgram;
  3278. end;
  3279. procedure TTestModule.TestConstBaseTypes;
  3280. begin
  3281. StartProgram(false);
  3282. Add('const');
  3283. Add(' i: longint = 3;');
  3284. Add(' s: string = ''foo'';');
  3285. Add(' c: char = ''4'';');
  3286. Add(' b: boolean = true;');
  3287. Add(' d: double = 5.6;');
  3288. Add(' e = low(word);');
  3289. Add(' f = high(word);');
  3290. Add('begin');
  3291. ConvertProgram;
  3292. CheckSource('TestVarBaseTypes',
  3293. LinesToStr([
  3294. 'this.i=3;',
  3295. 'this.s="foo";',
  3296. 'this.c="4";',
  3297. 'this.b=true;',
  3298. 'this.d=5.6;',
  3299. 'this.e = 0;',
  3300. 'this.f = 65535;'
  3301. ]),
  3302. '');
  3303. end;
  3304. procedure TTestModule.TestAliasTypeRef;
  3305. begin
  3306. StartProgram(false);
  3307. Add('type');
  3308. Add(' a=longint;');
  3309. Add(' b=a;');
  3310. Add('var');
  3311. Add(' c: A;');
  3312. Add(' d: B;');
  3313. Add('begin');
  3314. ConvertProgram;
  3315. CheckSource('TestAliasTypeRef',
  3316. LinesToStr([ // statements
  3317. 'this.c = 0;',
  3318. 'this.d = 0;'
  3319. ]),
  3320. LinesToStr([ // this.$main
  3321. ''
  3322. ]));
  3323. end;
  3324. procedure TTestModule.TestTypeCast_BaseTypes;
  3325. begin
  3326. StartProgram(false);
  3327. Add([
  3328. 'var',
  3329. ' i: longint;',
  3330. ' b: boolean;',
  3331. ' d: double;',
  3332. ' s: string;',
  3333. ' c: char;',
  3334. 'begin',
  3335. ' i:=longint(i);',
  3336. ' i:=longint(b);',
  3337. ' b:=boolean(b);',
  3338. ' b:=boolean(i);',
  3339. ' d:=double(d);',
  3340. ' d:=double(i);',
  3341. ' s:=string(s);',
  3342. ' s:=string(c);',
  3343. ' c:=char(c);',
  3344. ' c:=char(i);',
  3345. ' c:=char(65);',
  3346. ' c:=char(#10);',
  3347. ' c:=char(#$E000);',
  3348. '']);
  3349. ConvertProgram;
  3350. CheckSource('TestAliasTypeRef',
  3351. LinesToStr([ // statements
  3352. 'this.i = 0;',
  3353. 'this.b = false;',
  3354. 'this.d = 0.0;',
  3355. 'this.s = "";',
  3356. 'this.c = "";',
  3357. '']),
  3358. LinesToStr([ // this.$main
  3359. '$mod.i = $mod.i;',
  3360. '$mod.i = ($mod.b ? 1 : 0);',
  3361. '$mod.b = $mod.b;',
  3362. '$mod.b = $mod.i != 0;',
  3363. '$mod.d = $mod.d;',
  3364. '$mod.d = $mod.i;',
  3365. '$mod.s = $mod.s;',
  3366. '$mod.s = $mod.c;',
  3367. '$mod.c = $mod.c;',
  3368. '$mod.c = String.fromCharCode($mod.i);',
  3369. '$mod.c = "A";',
  3370. '$mod.c = "\n";',
  3371. '$mod.c = "";',
  3372. '']));
  3373. end;
  3374. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3375. begin
  3376. StartProgram(false);
  3377. Add('type');
  3378. Add(' integer = longint;');
  3379. Add(' TYesNo = boolean;');
  3380. Add(' TFloat = double;');
  3381. Add(' TCaption = string;');
  3382. Add(' TChar = char;');
  3383. Add('var');
  3384. Add(' i: integer;');
  3385. Add(' b: TYesNo;');
  3386. Add(' d: TFloat;');
  3387. Add(' s: TCaption;');
  3388. Add(' c: TChar;');
  3389. Add('begin');
  3390. Add(' i:=integer(i);');
  3391. Add(' i:=integer(b);');
  3392. Add(' b:=TYesNo(b);');
  3393. Add(' b:=TYesNo(i);');
  3394. Add(' d:=TFloat(d);');
  3395. Add(' d:=TFloat(i);');
  3396. Add(' s:=TCaption(s);');
  3397. Add(' s:=TCaption(c);');
  3398. Add(' c:=TChar(c);');
  3399. ConvertProgram;
  3400. CheckSource('TestAliasTypeRef',
  3401. LinesToStr([ // statements
  3402. 'this.i = 0;',
  3403. 'this.b = false;',
  3404. 'this.d = 0.0;',
  3405. 'this.s = "";',
  3406. 'this.c = "";',
  3407. '']),
  3408. LinesToStr([ // this.$main
  3409. '$mod.i = $mod.i;',
  3410. '$mod.i = ($mod.b ? 1 : 0);',
  3411. '$mod.b = $mod.b;',
  3412. '$mod.b = $mod.i != 0;',
  3413. '$mod.d = $mod.d;',
  3414. '$mod.d = $mod.i;',
  3415. '$mod.s = $mod.s;',
  3416. '$mod.s = $mod.c;',
  3417. '$mod.c = $mod.c;',
  3418. '']));
  3419. end;
  3420. procedure TTestModule.TestEmptyProc;
  3421. begin
  3422. StartProgram(false);
  3423. Add('procedure Test;');
  3424. Add('begin');
  3425. Add('end;');
  3426. Add('begin');
  3427. ConvertProgram;
  3428. CheckSource('TestEmptyProc',
  3429. LinesToStr([ // statements
  3430. 'this.Test = function () {',
  3431. '};'
  3432. ]),
  3433. LinesToStr([ // this.$main
  3434. ''
  3435. ]));
  3436. end;
  3437. procedure TTestModule.TestProcOneParam;
  3438. begin
  3439. StartProgram(false);
  3440. Add('procedure ProcA(i: longint);');
  3441. Add('begin');
  3442. Add('end;');
  3443. Add('begin');
  3444. Add(' PROCA(3);');
  3445. ConvertProgram;
  3446. CheckSource('TestProcOneParam',
  3447. LinesToStr([ // statements
  3448. 'this.ProcA = function (i) {',
  3449. '};'
  3450. ]),
  3451. LinesToStr([ // this.$main
  3452. '$mod.ProcA(3);'
  3453. ]));
  3454. end;
  3455. procedure TTestModule.TestFunctionWithoutParams;
  3456. begin
  3457. StartProgram(false);
  3458. Add('function FuncA: longint;');
  3459. Add('begin');
  3460. Add('end;');
  3461. Add('var i: longint;');
  3462. Add('begin');
  3463. Add(' I:=FUNCA();');
  3464. Add(' I:=FUNCA;');
  3465. Add(' FUNCA();');
  3466. Add(' FUNCA;');
  3467. ConvertProgram;
  3468. CheckSource('TestProcWithoutParams',
  3469. LinesToStr([ // statements
  3470. 'this.FuncA = function () {',
  3471. ' var Result = 0;',
  3472. ' return Result;',
  3473. '};',
  3474. 'this.i=0;'
  3475. ]),
  3476. LinesToStr([ // this.$main
  3477. '$mod.i=$mod.FuncA();',
  3478. '$mod.i=$mod.FuncA();',
  3479. '$mod.FuncA();',
  3480. '$mod.FuncA();'
  3481. ]));
  3482. end;
  3483. procedure TTestModule.TestProcedureWithoutParams;
  3484. begin
  3485. StartProgram(false);
  3486. Add('procedure ProcA;');
  3487. Add('begin');
  3488. Add('end;');
  3489. Add('begin');
  3490. Add(' PROCA();');
  3491. Add(' PROCA;');
  3492. ConvertProgram;
  3493. CheckSource('TestProcWithoutParams',
  3494. LinesToStr([ // statements
  3495. 'this.ProcA = function () {',
  3496. '};'
  3497. ]),
  3498. LinesToStr([ // this.$main
  3499. '$mod.ProcA();',
  3500. '$mod.ProcA();'
  3501. ]));
  3502. end;
  3503. procedure TTestModule.TestIncDec;
  3504. begin
  3505. StartProgram(false);
  3506. Add([
  3507. 'procedure DoIt(var i: longint);',
  3508. 'begin',
  3509. ' inc(i);',
  3510. ' inc(i,2);',
  3511. 'end;',
  3512. 'var',
  3513. ' Bar: longint;',
  3514. 'begin',
  3515. ' inc(bar);',
  3516. ' inc(bar,2);',
  3517. ' dec(bar);',
  3518. ' dec(bar,3);',
  3519. '']);
  3520. ConvertProgram;
  3521. CheckSource('TestIncDec',
  3522. LinesToStr([ // statements
  3523. 'this.DoIt = function (i) {',
  3524. ' i.set(i.get()+1);',
  3525. ' i.set(i.get()+2);',
  3526. '};',
  3527. 'this.Bar = 0;'
  3528. ]),
  3529. LinesToStr([ // this.$main
  3530. '$mod.Bar+=1;',
  3531. '$mod.Bar+=2;',
  3532. '$mod.Bar-=1;',
  3533. '$mod.Bar-=3;'
  3534. ]));
  3535. end;
  3536. procedure TTestModule.TestLoHiFpcMode;
  3537. begin
  3538. StartProgram(false);
  3539. Add([
  3540. '{$mode objfpc}',
  3541. 'const',
  3542. ' LoByte1 = Lo(Word($1234));',
  3543. ' HiByte1 = Hi(Word($1234));',
  3544. ' LoByte2 = Lo(SmallInt($1234));',
  3545. ' HiByte2 = Hi(SmallInt($1234));',
  3546. ' LoWord1 = Lo($1234CDEF);',
  3547. ' HiWord1 = Hi($1234CDEF);',
  3548. ' LoWord2 = Lo(-$1234CDEF);',
  3549. ' HiWord2 = Hi(-$1234CDEF);',
  3550. ' lo4:byte=lo(byte($34));',
  3551. ' hi4:byte=hi(byte($34));',
  3552. ' lo5:byte=lo(shortint(-$34));',
  3553. ' hi5:byte=hi(shortint(-$34));',
  3554. ' lo6:longword=lo($123456789ABCD);',
  3555. ' hi6:longword=hi($123456789ABCD);',
  3556. ' lo7:longword=lo(-$123456789ABCD);',
  3557. ' hi7:longword=hi(-$123456789ABCD);',
  3558. 'var',
  3559. ' b: Byte;',
  3560. ' ss: shortint;',
  3561. ' w: Word;',
  3562. ' si: SmallInt;',
  3563. ' lw: LongWord;',
  3564. ' li: LongInt;',
  3565. ' b2: Byte;',
  3566. ' ni: nativeint;',
  3567. 'begin',
  3568. ' w := $1234;',
  3569. ' ss := -$12;',
  3570. ' b := lo(ss);',
  3571. ' b := HI(ss);',
  3572. ' b := lo(w);',
  3573. ' b := HI(w);',
  3574. ' b2 := lo(b);',
  3575. ' b2 := hi(b);',
  3576. ' lw := $1234CDEF;',
  3577. ' w := lo(lw);',
  3578. ' w := hi(lw);',
  3579. ' ni := $123456789ABCD;',
  3580. ' lw := lo(ni);',
  3581. ' lw := hi(ni);',
  3582. '']);
  3583. ConvertProgram;
  3584. CheckSource('TestLoHiFpcMode',
  3585. LinesToStr([ // statements
  3586. 'this.LoByte1 = 0x1234 & 0xFF;',
  3587. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3588. 'this.LoByte2 = 0x1234 & 0xFF;',
  3589. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3590. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3591. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3592. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3593. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3594. 'this.lo4 = 0x34 & 0xF;',
  3595. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3596. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3597. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3598. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3599. 'this.hi6 = 74565 >>> 0;',
  3600. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3601. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3602. 'this.b = 0;',
  3603. 'this.ss = 0;',
  3604. 'this.w = 0;',
  3605. 'this.si = 0;',
  3606. 'this.lw = 0;',
  3607. 'this.li = 0;',
  3608. 'this.b2 = 0;',
  3609. 'this.ni = 0;',
  3610. '']),
  3611. LinesToStr([ // this.$main
  3612. '$mod.w = 0x1234;',
  3613. '$mod.ss = -0x12;',
  3614. '$mod.b = $mod.ss & 0xFF;',
  3615. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3616. '$mod.b = $mod.w & 0xFF;',
  3617. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3618. '$mod.b2 = $mod.b & 0xF;',
  3619. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3620. '$mod.lw = 0x1234CDEF;',
  3621. '$mod.w = $mod.lw & 0xFFFF;',
  3622. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3623. '$mod.ni = 0x123456789ABCD;',
  3624. '$mod.lw = $mod.ni >>> 0;',
  3625. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3626. '']));
  3627. end;
  3628. procedure TTestModule.TestLoHiDelphiMode;
  3629. begin
  3630. StartProgram(false);
  3631. Add([
  3632. '{$mode delphi}',
  3633. 'const',
  3634. ' LoByte1 = Lo(Word($1234));',
  3635. ' HiByte1 = Hi(Word($1234));',
  3636. ' LoByte2 = Lo(SmallInt($1234));',
  3637. ' HiByte2 = Hi(SmallInt($1234));',
  3638. ' LoByte3 = Lo($1234CDEF);',
  3639. ' HiByte3 = Hi($1234CDEF);',
  3640. ' LoByte4 = Lo(-$1234CDEF);',
  3641. ' HiByte4 = Hi(-$1234CDEF);',
  3642. 'var',
  3643. ' b: Byte;',
  3644. ' w: Word;',
  3645. ' si: SmallInt;',
  3646. ' lw: LongWord;',
  3647. ' li: LongInt;',
  3648. 'begin',
  3649. ' w := $1234;',
  3650. ' b := lo(w);',
  3651. ' b := HI(w);',
  3652. ' lw := $1234CDEF;',
  3653. ' b := lo(lw);',
  3654. ' b := hi(lw);',
  3655. '']);
  3656. ConvertProgram;
  3657. CheckSource('TestLoHiDelphiMode',
  3658. LinesToStr([ // statements
  3659. 'this.LoByte1 = 0x1234 & 0xFF;',
  3660. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3661. 'this.LoByte2 = 0x1234 & 0xFF;',
  3662. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3663. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3664. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3665. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3666. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3667. 'this.b = 0;',
  3668. 'this.w = 0;',
  3669. 'this.si = 0;',
  3670. 'this.lw = 0;',
  3671. 'this.li = 0;'
  3672. ]),
  3673. LinesToStr([ // this.$main
  3674. '$mod.w = 0x1234;',
  3675. '$mod.b = $mod.w & 0xFF;',
  3676. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3677. '$mod.lw = 0x1234CDEF;',
  3678. '$mod.b = $mod.lw & 0xFF;',
  3679. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3680. ]));
  3681. end;
  3682. procedure TTestModule.TestAssignments;
  3683. begin
  3684. StartProgram(false);
  3685. Parser.Options:=Parser.Options+[po_cassignments];
  3686. Add('var');
  3687. Add(' Bar:longint;');
  3688. Add('begin');
  3689. Add(' bar:=3;');
  3690. Add(' bar+=4;');
  3691. Add(' bar-=5;');
  3692. Add(' bar*=6;');
  3693. ConvertProgram;
  3694. CheckSource('TestAssignments',
  3695. LinesToStr([ // statements
  3696. 'this.Bar = 0;'
  3697. ]),
  3698. LinesToStr([ // this.$main
  3699. '$mod.Bar=3;',
  3700. '$mod.Bar+=4;',
  3701. '$mod.Bar-=5;',
  3702. '$mod.Bar*=6;'
  3703. ]));
  3704. end;
  3705. procedure TTestModule.TestArithmeticOperators1;
  3706. begin
  3707. StartProgram(false);
  3708. Add('var');
  3709. Add(' vA,vB,vC:longint;');
  3710. Add('begin');
  3711. Add(' va:=1;');
  3712. Add(' vb:=va+va;');
  3713. Add(' vb:=va div vb;');
  3714. Add(' vb:=va mod vb;');
  3715. Add(' vb:=va+va*vb+va div vb;');
  3716. Add(' vc:=-va;');
  3717. Add(' va:=va-vb;');
  3718. Add(' vb:=va;');
  3719. Add(' if va<vb then vc:=va else vc:=vb;');
  3720. ConvertProgram;
  3721. CheckSource('TestArithmeticOperators1',
  3722. LinesToStr([ // statements
  3723. 'this.vA = 0;',
  3724. 'this.vB = 0;',
  3725. 'this.vC = 0;'
  3726. ]),
  3727. LinesToStr([ // this.$main
  3728. '$mod.vA = 1;',
  3729. '$mod.vB = $mod.vA + $mod.vA;',
  3730. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3731. '$mod.vB = $mod.vA % $mod.vB;',
  3732. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3733. '$mod.vC = -$mod.vA;',
  3734. '$mod.vA = $mod.vA - $mod.vB;',
  3735. '$mod.vB = $mod.vA;',
  3736. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3737. ]));
  3738. end;
  3739. procedure TTestModule.TestMultiAdd;
  3740. begin
  3741. StartProgram(false);
  3742. Add([
  3743. 'function Fly: string; external name ''fly'';',
  3744. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3745. 'var',
  3746. ' Date: double;',
  3747. 'begin',
  3748. ' Result:=(Year>0) and (Year<10000) and',
  3749. ' (Month >= 1) and (Month<=12) and',
  3750. ' (Day>0) and (Day<=31);',
  3751. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3752. 'end;',
  3753. 'var s: string;',
  3754. 'begin',
  3755. ' s:=''a''+''b''+''c''+''d'';',
  3756. ' s:=s+Fly+''e'';',
  3757. ' s:=Fly+Fly+Fly;',
  3758. '']);
  3759. ConvertProgram;
  3760. CheckSource('TestMultiAdd',
  3761. LinesToStr([ // statements
  3762. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3763. ' var Result = false;',
  3764. ' var date = 0.0;',
  3765. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3766. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3767. ' return Result;',
  3768. '};',
  3769. 'this.s = "";',
  3770. '']),
  3771. LinesToStr([ // this.$main
  3772. '$mod.s = "a" + "b" + "c" + "d";',
  3773. '$mod.s = $mod.s + fly() + "e";',
  3774. '$mod.s = fly() + fly() + fly();',
  3775. '']));
  3776. end;
  3777. procedure TTestModule.TestLogicalOperators;
  3778. begin
  3779. StartProgram(false);
  3780. Add('var');
  3781. Add(' vA,vB,vC:boolean;');
  3782. Add('begin');
  3783. Add(' va:=vb and vc;');
  3784. Add(' va:=vb or vc;');
  3785. Add(' va:=vb xor vc;');
  3786. Add(' va:=true and vc;');
  3787. Add(' va:=(vb and vc) or (va and vb);');
  3788. Add(' va:=not vb;');
  3789. ConvertProgram;
  3790. CheckSource('TestLogicalOperators',
  3791. LinesToStr([ // statements
  3792. 'this.vA = false;',
  3793. 'this.vB = false;',
  3794. 'this.vC = false;'
  3795. ]),
  3796. LinesToStr([ // this.$main
  3797. '$mod.vA = $mod.vB && $mod.vC;',
  3798. '$mod.vA = $mod.vB || $mod.vC;',
  3799. '$mod.vA = $mod.vB ^ $mod.vC;',
  3800. '$mod.vA = true && $mod.vC;',
  3801. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3802. '$mod.vA = !$mod.vB;'
  3803. ]));
  3804. end;
  3805. procedure TTestModule.TestBitwiseOperators;
  3806. begin
  3807. StartProgram(false);
  3808. Add([
  3809. 'var',
  3810. ' vA,vB,vC:longint;',
  3811. ' X,Y,Z: nativeint;',
  3812. 'begin',
  3813. ' va:=vb and vc;',
  3814. ' va:=vb or vc;',
  3815. ' va:=vb xor vc;',
  3816. ' va:=vb shl vc;',
  3817. ' va:=vb shr vc;',
  3818. ' va:=3 and vc;',
  3819. ' va:=(vb and vc) or (va and vb);',
  3820. ' va:=not vb;',
  3821. ' X:=Y and Z;',
  3822. ' X:=Y and va;',
  3823. ' X:=Y or Z;',
  3824. ' X:=Y or va;',
  3825. ' X:=Y xor Z;',
  3826. ' X:=Y xor va;',
  3827. '']);
  3828. ConvertProgram;
  3829. CheckSource('TestBitwiseOperators',
  3830. LinesToStr([ // statements
  3831. 'this.vA = 0;',
  3832. 'this.vB = 0;',
  3833. 'this.vC = 0;',
  3834. 'this.X = 0;',
  3835. 'this.Y = 0;',
  3836. 'this.Z = 0;',
  3837. '']),
  3838. LinesToStr([ // this.$main
  3839. '$mod.vA = $mod.vB & $mod.vC;',
  3840. '$mod.vA = $mod.vB | $mod.vC;',
  3841. '$mod.vA = $mod.vB ^ $mod.vC;',
  3842. '$mod.vA = $mod.vB << $mod.vC;',
  3843. '$mod.vA = $mod.vB >>> $mod.vC;',
  3844. '$mod.vA = 3 & $mod.vC;',
  3845. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3846. '$mod.vA = ~$mod.vB;',
  3847. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3848. '$mod.X = $mod.Y & $mod.vA;',
  3849. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3850. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3851. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3852. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3853. '']));
  3854. end;
  3855. procedure TTestModule.TestBitwiseOperatorsLongword;
  3856. begin
  3857. StartProgram(false);
  3858. Add([
  3859. 'var',
  3860. ' a,b,c:longword;',
  3861. ' i: longint;',
  3862. 'begin',
  3863. ' a:=$12345678;',
  3864. ' b:=$EDCBA987;',
  3865. ' c:=not a;',
  3866. ' c:=a and b;',
  3867. ' c:=a and $ffff0000;',
  3868. ' c:=a or b;',
  3869. ' c:=a or $ff00ff00;',
  3870. ' c:=a xor b;',
  3871. ' c:=a xor $f0f0f0f0;',
  3872. ' c:=a shl 1;',
  3873. ' c:=a shl 16;',
  3874. ' c:=a shl 24;',
  3875. ' c:=a shl b;',
  3876. ' c:=a shr 1;',
  3877. ' c:=a shr 16;',
  3878. ' c:=a shr 24;',
  3879. ' c:=a shr b;',
  3880. ' c:=(b and c) or (a and b);',
  3881. ' c:=i and a;',
  3882. ' c:=i or a;',
  3883. ' c:=i xor a;',
  3884. '']);
  3885. ConvertProgram;
  3886. CheckSource('TestBitwiseOperatorsLongword',
  3887. LinesToStr([ // statements
  3888. 'this.a = 0;',
  3889. 'this.b = 0;',
  3890. 'this.c = 0;',
  3891. 'this.i = 0;',
  3892. '']),
  3893. LinesToStr([ // this.$main
  3894. '$mod.a = 0x12345678;',
  3895. '$mod.b = 0xEDCBA987;',
  3896. '$mod.c = rtl.lw(~$mod.a);',
  3897. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3898. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3899. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3900. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3901. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3902. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3903. '$mod.c = rtl.lw($mod.a << 1);',
  3904. '$mod.c = rtl.lw($mod.a << 16);',
  3905. '$mod.c = rtl.lw($mod.a << 24);',
  3906. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3907. '$mod.c = rtl.lw($mod.a >>> 1);',
  3908. '$mod.c = rtl.lw($mod.a >>> 16);',
  3909. '$mod.c = rtl.lw($mod.a >>> 24);',
  3910. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3911. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3912. '$mod.c = $mod.i & $mod.a;',
  3913. '$mod.c = $mod.i | $mod.a;',
  3914. '$mod.c = $mod.i ^ $mod.a;',
  3915. '']));
  3916. end;
  3917. procedure TTestModule.TestPrgProcVar;
  3918. begin
  3919. StartProgram(false);
  3920. Add('procedure Proc1;');
  3921. Add('type');
  3922. Add(' t1=longint;');
  3923. Add('var');
  3924. Add(' vA:t1;');
  3925. Add('begin');
  3926. Add('end;');
  3927. Add('begin');
  3928. ConvertProgram;
  3929. CheckSource('TestPrgProcVar',
  3930. LinesToStr([ // statements
  3931. 'this.Proc1 = function () {',
  3932. ' var vA=0;',
  3933. '};'
  3934. ]),
  3935. LinesToStr([ // this.$main
  3936. ''
  3937. ]));
  3938. end;
  3939. procedure TTestModule.TestUnitProcVar;
  3940. begin
  3941. StartUnit(false);
  3942. Add('interface');
  3943. Add('');
  3944. Add('type tA=string; // unit scope');
  3945. Add('procedure Proc1;');
  3946. Add('');
  3947. Add('implementation');
  3948. Add('');
  3949. Add('procedure Proc1;');
  3950. Add('type tA=longint; // local proc scope');
  3951. Add('var v1:tA; // using local tA');
  3952. Add('begin');
  3953. Add('end;');
  3954. Add('var v2:tA; // using interface tA');
  3955. ConvertUnit;
  3956. CheckSource('TestUnitProcVar',
  3957. LinesToStr([ // statements
  3958. 'var $impl = $mod.$impl;',
  3959. 'this.Proc1 = function () {',
  3960. ' var v1 = 0;',
  3961. '};',
  3962. '']),
  3963. // this.$init
  3964. '',
  3965. // implementation
  3966. LinesToStr([
  3967. '$impl.v2 = "";',
  3968. '']));
  3969. end;
  3970. procedure TTestModule.TestImplProc;
  3971. begin
  3972. StartUnit(false);
  3973. Add('interface');
  3974. Add('');
  3975. Add('procedure Proc1;');
  3976. Add('');
  3977. Add('implementation');
  3978. Add('');
  3979. Add('procedure Proc1; begin end;');
  3980. Add('procedure Proc2; begin end;');
  3981. Add('initialization');
  3982. Add(' Proc1;');
  3983. Add(' Proc2;');
  3984. ConvertUnit;
  3985. CheckSource('TestImplProc',
  3986. LinesToStr([ // statements
  3987. 'var $impl = $mod.$impl;',
  3988. 'this.Proc1 = function () {',
  3989. '};',
  3990. '']),
  3991. LinesToStr([ // this.$init
  3992. '$mod.Proc1();',
  3993. '$impl.Proc2();',
  3994. '']),
  3995. LinesToStr([ // implementation
  3996. '$impl.Proc2 = function () {',
  3997. '};',
  3998. ''])
  3999. );
  4000. end;
  4001. procedure TTestModule.TestFunctionResult;
  4002. begin
  4003. StartProgram(false);
  4004. Add('function Func1: longint;');
  4005. Add('begin');
  4006. Add(' Result:=3;');
  4007. Add(' Func1:=4;');
  4008. Add('end;');
  4009. Add('begin');
  4010. ConvertProgram;
  4011. CheckSource('TestFunctionResult',
  4012. LinesToStr([ // statements
  4013. 'this.Func1 = function () {',
  4014. ' var Result = 0;',
  4015. ' Result = 3;',
  4016. ' Result = 4;',
  4017. ' return Result;',
  4018. '};'
  4019. ]),
  4020. '');
  4021. end;
  4022. procedure TTestModule.TestNestedProc;
  4023. begin
  4024. StartProgram(false);
  4025. Add([
  4026. 'var vInUnit: longint;',
  4027. 'function DoIt(pA,pD: longint): longint;',
  4028. 'var',
  4029. ' vB: longint;',
  4030. ' vC: longint;',
  4031. ' function Nesty(pA: longint): longint; ',
  4032. ' var vB: longint;',
  4033. ' begin',
  4034. ' Result:=pa+vb+vc+pd+vInUnit;',
  4035. ' nesty:=3;',
  4036. ' doit:=4;',
  4037. ' exit;',
  4038. ' end;',
  4039. 'begin',
  4040. ' Result:=pa+vb+vc;',
  4041. ' doit:=6;',
  4042. ' exit;',
  4043. 'end;',
  4044. 'begin']);
  4045. ConvertProgram;
  4046. CheckSource('TestNestedProc',
  4047. LinesToStr([ // statements
  4048. 'this.vInUnit = 0;',
  4049. 'this.DoIt = function (pA, pD) {',
  4050. ' var Result = 0;',
  4051. ' var vB = 0;',
  4052. ' var vC = 0;',
  4053. ' function Nesty(pA) {',
  4054. ' var Result$1 = 0;',
  4055. ' var vB = 0;',
  4056. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4057. ' Result$1 = 3;',
  4058. ' Result = 4;',
  4059. ' return Result$1;',
  4060. ' return Result$1;',
  4061. ' };',
  4062. ' Result = pA + vB + vC;',
  4063. ' Result = 6;',
  4064. ' return Result;',
  4065. ' return Result;',
  4066. '};'
  4067. ]),
  4068. '');
  4069. end;
  4070. procedure TTestModule.TestNestedProc_ResultString;
  4071. begin
  4072. StartProgram(false);
  4073. Add([
  4074. 'function DoIt: string;',
  4075. ' function Nesty: string; ',
  4076. ' begin',
  4077. ' nesty:=#65#66;',
  4078. ' nesty[1]:=#67;',
  4079. ' doit:=#68;',
  4080. ' doit[2]:=#69;',
  4081. ' end;',
  4082. 'begin',
  4083. ' doit:=#70;',
  4084. ' doit[3]:=#71;',
  4085. 'end;',
  4086. 'begin']);
  4087. ConvertProgram;
  4088. CheckSource('TestNestedProc_ResultString',
  4089. LinesToStr([ // statements
  4090. 'this.DoIt = function () {',
  4091. ' var Result = "";',
  4092. ' function Nesty() {',
  4093. ' var Result$1 = "";',
  4094. ' Result$1 = "AB";',
  4095. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4096. ' Result = "D";',
  4097. ' Result = rtl.setCharAt(Result, 1, "E");',
  4098. ' return Result$1;',
  4099. ' };',
  4100. ' Result = "F";',
  4101. ' Result = rtl.setCharAt(Result, 2, "G");',
  4102. ' return Result;',
  4103. '};'
  4104. ]),
  4105. '');
  4106. end;
  4107. procedure TTestModule.TestForwardProc;
  4108. begin
  4109. StartProgram(false);
  4110. Add('procedure FuncA(Bar: longint); forward;');
  4111. Add('procedure FuncB(Bar: longint);');
  4112. Add('begin');
  4113. Add(' funca(bar);');
  4114. Add('end;');
  4115. Add('procedure funca(bar: longint);');
  4116. Add('begin');
  4117. Add(' if bar=3 then ;');
  4118. Add('end;');
  4119. Add('begin');
  4120. Add(' funca(4);');
  4121. Add(' funcb(5);');
  4122. ConvertProgram;
  4123. CheckSource('TestForwardProc',
  4124. LinesToStr([ // statements'
  4125. 'this.FuncB = function (Bar) {',
  4126. ' $mod.FuncA(Bar);',
  4127. '};',
  4128. 'this.FuncA = function (Bar) {',
  4129. ' if (Bar === 3);',
  4130. '};'
  4131. ]),
  4132. LinesToStr([
  4133. '$mod.FuncA(4);',
  4134. '$mod.FuncB(5);'
  4135. ])
  4136. );
  4137. end;
  4138. procedure TTestModule.TestNestedForwardProc;
  4139. begin
  4140. StartProgram(false);
  4141. Add('procedure FuncA;');
  4142. Add(' procedure FuncB(i: longint); forward;');
  4143. Add(' procedure FuncC(i: longint);');
  4144. Add(' begin');
  4145. Add(' funcb(i);');
  4146. Add(' end;');
  4147. Add(' procedure FuncB(i: longint);');
  4148. Add(' begin');
  4149. Add(' if i=3 then ;');
  4150. Add(' end;');
  4151. Add('begin');
  4152. Add(' funcc(4)');
  4153. Add('end;');
  4154. Add('begin');
  4155. Add(' funca;');
  4156. ConvertProgram;
  4157. CheckSource('TestNestedForwardProc',
  4158. LinesToStr([ // statements'
  4159. 'this.FuncA = function () {',
  4160. ' function FuncC(i) {',
  4161. ' FuncB(i);',
  4162. ' };',
  4163. ' function FuncB(i) {',
  4164. ' if (i === 3);',
  4165. ' };',
  4166. ' FuncC(4);',
  4167. '};'
  4168. ]),
  4169. LinesToStr([
  4170. '$mod.FuncA();'
  4171. ])
  4172. );
  4173. end;
  4174. procedure TTestModule.TestAssignFunctionResult;
  4175. begin
  4176. StartProgram(false);
  4177. Add('function Func1: longint;');
  4178. Add('begin');
  4179. Add('end;');
  4180. Add('var i: longint;');
  4181. Add('begin');
  4182. Add(' i:=func1();');
  4183. Add(' i:=func1()+func1();');
  4184. ConvertProgram;
  4185. CheckSource('TestAssignFunctionResult',
  4186. LinesToStr([ // statements
  4187. 'this.Func1 = function () {',
  4188. ' var Result = 0;',
  4189. ' return Result;',
  4190. '};',
  4191. 'this.i = 0;'
  4192. ]),
  4193. LinesToStr([
  4194. '$mod.i = $mod.Func1();',
  4195. '$mod.i = $mod.Func1() + $mod.Func1();'
  4196. ]));
  4197. end;
  4198. procedure TTestModule.TestFunctionResultInCondition;
  4199. begin
  4200. StartProgram(false);
  4201. Add('function Func1: longint;');
  4202. Add('begin');
  4203. Add('end;');
  4204. Add('function Func2: boolean;');
  4205. Add('begin');
  4206. Add('end;');
  4207. Add('var i: longint;');
  4208. Add('begin');
  4209. Add(' if func2 then ;');
  4210. Add(' if i=func1() then ;');
  4211. Add(' if i=func1 then ;');
  4212. ConvertProgram;
  4213. CheckSource('TestFunctionResultInCondition',
  4214. LinesToStr([ // statements
  4215. 'this.Func1 = function () {',
  4216. ' var Result = 0;',
  4217. ' return Result;',
  4218. '};',
  4219. 'this.Func2 = function () {',
  4220. ' var Result = false;',
  4221. ' return Result;',
  4222. '};',
  4223. 'this.i = 0;'
  4224. ]),
  4225. LinesToStr([
  4226. 'if ($mod.Func2());',
  4227. 'if ($mod.i === $mod.Func1());',
  4228. 'if ($mod.i === $mod.Func1());'
  4229. ]));
  4230. end;
  4231. procedure TTestModule.TestFunctionResultInForLoop;
  4232. begin
  4233. StartProgram(false);
  4234. Add([
  4235. 'function Func1(a: array of longint): longint;',
  4236. 'begin',
  4237. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4238. ' for Result in a do if a[Result]=0 then exit;',
  4239. 'end;',
  4240. 'begin',
  4241. ' Func1([1,2,3])']);
  4242. ConvertProgram;
  4243. CheckSource('TestFunctionResultInForLoop',
  4244. LinesToStr([ // statements
  4245. 'this.Func1 = function (a) {',
  4246. ' var Result = 0;',
  4247. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4248. ' Result = $l;',
  4249. ' if (a[Result] === 0) return Result;',
  4250. ' };',
  4251. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4252. ' Result = $in[$l1];',
  4253. ' if (a[Result] === 0) return Result;',
  4254. ' };',
  4255. ' return Result;',
  4256. '};',
  4257. '']),
  4258. LinesToStr([
  4259. '$mod.Func1([1, 2, 3]);'
  4260. ]));
  4261. end;
  4262. procedure TTestModule.TestFunctionResultInTypeCast;
  4263. begin
  4264. StartProgram(false);
  4265. Add([
  4266. 'function GetInt: longint;',
  4267. 'begin',
  4268. 'end;',
  4269. 'begin',
  4270. ' if Byte(GetInt)=0 then ;',
  4271. '']);
  4272. ConvertProgram;
  4273. CheckSource('TestFunctionResultInTypeCast',
  4274. LinesToStr([ // statements
  4275. 'this.GetInt = function () {',
  4276. ' var Result = 0;',
  4277. ' return Result;',
  4278. '};',
  4279. '']),
  4280. LinesToStr([
  4281. 'if (($mod.GetInt() & 255) === 0) ;'
  4282. ]));
  4283. end;
  4284. procedure TTestModule.TestExit;
  4285. begin
  4286. StartProgram(false);
  4287. Add('procedure ProcA;');
  4288. Add('begin');
  4289. Add(' exit;');
  4290. Add('end;');
  4291. Add('function FuncB: longint;');
  4292. Add('begin');
  4293. Add(' exit;');
  4294. Add(' exit(3);');
  4295. Add('end;');
  4296. Add('function FuncC: string;');
  4297. Add('begin');
  4298. Add(' exit;');
  4299. Add(' exit(''a'');');
  4300. Add(' exit(''abc'');');
  4301. Add('end;');
  4302. Add('begin');
  4303. Add(' exit;');
  4304. Add(' exit(1);');
  4305. ConvertProgram;
  4306. CheckSource('TestExit',
  4307. LinesToStr([ // statements
  4308. 'this.ProcA = function () {',
  4309. ' return;',
  4310. '};',
  4311. 'this.FuncB = function () {',
  4312. ' var Result = 0;',
  4313. ' return Result;',
  4314. ' return 3;',
  4315. ' return Result;',
  4316. '};',
  4317. 'this.FuncC = function () {',
  4318. ' var Result = "";',
  4319. ' return Result;',
  4320. ' return "a";',
  4321. ' return "abc";',
  4322. ' return Result;',
  4323. '};'
  4324. ]),
  4325. LinesToStr([
  4326. 'return;',
  4327. 'return 1;',
  4328. '']));
  4329. end;
  4330. procedure TTestModule.TestExit_ResultInFinally;
  4331. begin
  4332. StartProgram(false);
  4333. Add([
  4334. 'function Run: word;',
  4335. 'begin',
  4336. ' try',
  4337. ' exit(3);', // no Result in finally -> use return 3
  4338. ' finally',
  4339. ' end;',
  4340. 'end;',
  4341. 'function Fly: word;',
  4342. 'begin',
  4343. ' try',
  4344. ' exit(3);',
  4345. ' finally',
  4346. ' if Result>0 then ;',
  4347. ' end;',
  4348. 'end;',
  4349. 'function Jump: word;',
  4350. 'begin',
  4351. ' try',
  4352. ' try',
  4353. ' exit(4);',
  4354. ' finally',
  4355. ' end;',
  4356. ' finally',
  4357. ' if Result>0 then ;',
  4358. ' end;',
  4359. 'end;',
  4360. 'begin',
  4361. '']);
  4362. ConvertProgram;
  4363. CheckSource('TestExit_ResultInFinally',
  4364. LinesToStr([ // statements
  4365. 'this.Run = function () {',
  4366. ' var Result = 0;',
  4367. ' try {',
  4368. ' return 3;',
  4369. ' } finally {',
  4370. ' };',
  4371. ' return Result;',
  4372. '};',
  4373. 'this.Fly = function () {',
  4374. ' var Result = 0;',
  4375. ' try {',
  4376. ' Result = 3;',
  4377. ' return Result;',
  4378. ' } finally {',
  4379. ' if (Result > 0) ;',
  4380. ' };',
  4381. ' return Result;',
  4382. '};',
  4383. 'this.Jump = function () {',
  4384. ' var Result = 0;',
  4385. ' try {',
  4386. ' try {',
  4387. ' Result = 4;',
  4388. ' return Result;',
  4389. ' } finally {',
  4390. ' };',
  4391. ' } finally {',
  4392. ' if (Result > 0) ;',
  4393. ' };',
  4394. ' return Result;',
  4395. '};',
  4396. '']),
  4397. LinesToStr([
  4398. '']));
  4399. end;
  4400. procedure TTestModule.TestBreak;
  4401. begin
  4402. StartProgram(false);
  4403. Add([
  4404. 'var',
  4405. ' i: longint;',
  4406. 'begin',
  4407. ' repeat',
  4408. ' break;',
  4409. ' until true;',
  4410. ' while true do',
  4411. ' break;',
  4412. ' for i:=1 to 2 do',
  4413. ' break;']);
  4414. ConvertProgram;
  4415. CheckSource('TestBreak',
  4416. LinesToStr([ // statements
  4417. 'this.i = 0;'
  4418. ]),
  4419. LinesToStr([
  4420. 'do {',
  4421. ' break;',
  4422. '} while (!true);',
  4423. 'while (true) break;',
  4424. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4425. '']));
  4426. end;
  4427. procedure TTestModule.TestBreakAsVar;
  4428. begin
  4429. StartProgram(false);
  4430. Add([
  4431. 'procedure DoIt(break: boolean);',
  4432. 'begin',
  4433. ' if break then ;',
  4434. 'end;',
  4435. 'var',
  4436. ' break: boolean;',
  4437. 'begin',
  4438. ' if break then ;']);
  4439. ConvertProgram;
  4440. CheckSource('TestBreakAsVar',
  4441. LinesToStr([ // statements
  4442. 'this.DoIt = function (Break) {',
  4443. ' if (Break) ;',
  4444. '};',
  4445. 'this.Break = false;',
  4446. '']),
  4447. LinesToStr([
  4448. 'if($mod.Break) ;',
  4449. '']));
  4450. end;
  4451. procedure TTestModule.TestContinue;
  4452. begin
  4453. StartProgram(false);
  4454. Add('var i: longint;');
  4455. Add('begin');
  4456. Add(' repeat');
  4457. Add(' continue;');
  4458. Add(' until true;');
  4459. Add(' while true do');
  4460. Add(' continue;');
  4461. Add(' for i:=1 to 2 do');
  4462. Add(' continue;');
  4463. ConvertProgram;
  4464. CheckSource('TestContinue',
  4465. LinesToStr([ // statements
  4466. 'this.i = 0;'
  4467. ]),
  4468. LinesToStr([
  4469. 'do {',
  4470. ' continue;',
  4471. '} while (!true);',
  4472. 'while (true) continue;',
  4473. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4474. '']));
  4475. end;
  4476. procedure TTestModule.TestProc_External;
  4477. begin
  4478. StartProgram(false);
  4479. Add('procedure Foo; external name ''console.log'';');
  4480. Add('function Bar: longint; external name ''get.item'';');
  4481. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4482. Add('var');
  4483. Add(' i: longint;');
  4484. Add('begin');
  4485. Add(' Foo;');
  4486. Add(' i:=Bar;');
  4487. Add(' i:=Bla(''abc'');');
  4488. ConvertProgram;
  4489. CheckSource('TestProc_External',
  4490. LinesToStr([ // statements
  4491. 'this.i = 0;'
  4492. ]),
  4493. LinesToStr([
  4494. 'console.log();',
  4495. '$mod.i = get.item();',
  4496. '$mod.i = apply.something("abc");'
  4497. ]));
  4498. end;
  4499. procedure TTestModule.TestProc_ExternalOtherUnit;
  4500. begin
  4501. AddModuleWithIntfImplSrc('unit2.pas',
  4502. LinesToStr([
  4503. 'procedure Now; external name ''Date.now'';',
  4504. 'procedure DoIt;'
  4505. ]),
  4506. 'procedure doit; begin end;');
  4507. StartUnit(true);
  4508. Add('interface');
  4509. Add('uses unit2;');
  4510. Add('implementation');
  4511. Add('begin');
  4512. Add(' now;');
  4513. Add(' now();');
  4514. Add(' uNit2.now;');
  4515. Add(' uNit2.now();');
  4516. Add(' doit;');
  4517. Add(' uNit2.doit;');
  4518. ConvertUnit;
  4519. CheckSource('TestProc_ExternalOtherUnit',
  4520. LinesToStr([
  4521. '']),
  4522. LinesToStr([
  4523. 'Date.now();',
  4524. 'Date.now();',
  4525. 'Date.now();',
  4526. 'Date.now();',
  4527. 'pas.unit2.DoIt();',
  4528. 'pas.unit2.DoIt();',
  4529. '']));
  4530. end;
  4531. procedure TTestModule.TestProc_Asm;
  4532. begin
  4533. StartProgram(false);
  4534. Add([
  4535. '{$mode delphi}',
  4536. 'function DoIt: longint;',
  4537. 'begin;',
  4538. ' asm',
  4539. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4540. ' end;',
  4541. ' asm console.log(); end;',
  4542. ' asm',
  4543. ' s = "'' ";',
  4544. ' s = ''" '';',
  4545. ' s = s + "world" + "''";',
  4546. ' // end',
  4547. ' s = ''end'';',
  4548. ' s = "end";',
  4549. ' s = "foo\"bar";',
  4550. ' s = ''a\''b'';',
  4551. ' s = `${expr}\`-"-''-`;',
  4552. ' s = `multi',
  4553. 'line`;',
  4554. ' end;',
  4555. 'end;',
  4556. 'procedure Fly;',
  4557. 'asm',
  4558. ' return;',
  4559. 'end;',
  4560. 'begin']);
  4561. ConvertProgram;
  4562. CheckSource('TestProc_Asm',
  4563. LinesToStr([ // statements
  4564. 'this.DoIt = function () {',
  4565. ' var Result = 0;',
  4566. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4567. ' console.log();',
  4568. ' s = "'' ";',
  4569. ' s = ''" '';',
  4570. ' s = s + "world" + "''";',
  4571. ' // end',
  4572. ' s = ''end'';',
  4573. ' s = "end";',
  4574. ' s = "foo\"bar";',
  4575. ' s = ''a\''b'';',
  4576. ' s = `${expr}\`-"-''-`;',
  4577. ' s = `multi',
  4578. 'line`;',
  4579. ' return Result;',
  4580. '};',
  4581. 'this.Fly = function () {',
  4582. ' return;',
  4583. '};',
  4584. '']),
  4585. LinesToStr([
  4586. ''
  4587. ]));
  4588. end;
  4589. procedure TTestModule.TestProc_AsmSubBlock;
  4590. begin
  4591. StartProgram(true,[supTObject]);
  4592. Add([
  4593. '{$mode delphi}',
  4594. 'type',
  4595. ' TBird = class end;',
  4596. 'procedure Run(w: word);',
  4597. 'begin;',
  4598. ' if true then asm console.log(); end;',
  4599. ' if w>3 then asm',
  4600. ' var a = w+1;',
  4601. ' w = a+3;',
  4602. ' end;',
  4603. ' while (w>7) do asm',
  4604. ' w+=3; w*=2;',
  4605. ' end;',
  4606. ' try',
  4607. ' except',
  4608. ' on E: TBird do',
  4609. ' asm console.log(E); end;',
  4610. ' on E: TObject do',
  4611. ' asm var i=3; i--; end;',
  4612. ' else asm Fly; High; end;',
  4613. ' end;',
  4614. 'end;',
  4615. 'begin']);
  4616. ConvertProgram;
  4617. CheckSource('TestProc_AsmSubBlock',
  4618. LinesToStr([ // statements
  4619. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4620. '});',
  4621. 'this.Run = function (w) {',
  4622. ' if (true) console.log();',
  4623. ' if (w > 3) {',
  4624. ' var a = w+1;',
  4625. ' w = a+3;',
  4626. ' };',
  4627. ' while (w > 7) {',
  4628. ' w+=3; w*=2;',
  4629. ' };',
  4630. ' try {} catch ($e) {',
  4631. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4632. ' var E = $e;',
  4633. ' console.log(E);',
  4634. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4635. ' var E = $e;',
  4636. ' var i=3; i--;',
  4637. ' } else {',
  4638. ' Fly; High;',
  4639. ' }',
  4640. ' };',
  4641. '};',
  4642. '']),
  4643. LinesToStr([
  4644. ''
  4645. ]));
  4646. end;
  4647. procedure TTestModule.TestProc_Assembler;
  4648. begin
  4649. StartProgram(false);
  4650. Add('function DoIt: longint; assembler;');
  4651. Add('asm');
  4652. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4653. Add('end;');
  4654. Add('begin');
  4655. ConvertProgram;
  4656. CheckSource('TestProc_Assembler',
  4657. LinesToStr([ // statements
  4658. 'this.DoIt = function () {',
  4659. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4660. '};'
  4661. ]),
  4662. LinesToStr([
  4663. ''
  4664. ]));
  4665. end;
  4666. procedure TTestModule.TestProc_VarParam;
  4667. begin
  4668. StartProgram(false);
  4669. Add('type integer = longint;');
  4670. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4671. Add('var vJ: integer;');
  4672. Add('begin');
  4673. Add(' vg:=vg+1;');
  4674. Add(' vj:=vh+2;');
  4675. Add(' vi:=vi+3;');
  4676. Add(' doit(vg,vg,vg);');
  4677. Add(' doit(vh,vh,vj);');
  4678. Add(' doit(vi,vi,vi);');
  4679. Add(' doit(vj,vj,vj);');
  4680. Add('end;');
  4681. Add('var i: integer;');
  4682. Add('begin');
  4683. Add(' doit(i,i,i);');
  4684. ConvertProgram;
  4685. CheckSource('TestProc_VarParam',
  4686. LinesToStr([ // statements
  4687. 'this.DoIt = function (vG,vH,vI) {',
  4688. ' var vJ = 0;',
  4689. ' vG = vG + 1;',
  4690. ' vJ = vH + 2;',
  4691. ' vI.set(vI.get()+3);',
  4692. ' $mod.DoIt(vG, vG, {',
  4693. ' get: function () {',
  4694. ' return vG;',
  4695. ' },',
  4696. ' set: function (v) {',
  4697. ' vG = v;',
  4698. ' }',
  4699. ' });',
  4700. ' $mod.DoIt(vH, vH, {',
  4701. ' get: function () {',
  4702. ' return vJ;',
  4703. ' },',
  4704. ' set: function (v) {',
  4705. ' vJ = v;',
  4706. ' }',
  4707. ' });',
  4708. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4709. ' $mod.DoIt(vJ, vJ, {',
  4710. ' get: function () {',
  4711. ' return vJ;',
  4712. ' },',
  4713. ' set: function (v) {',
  4714. ' vJ = v;',
  4715. ' }',
  4716. ' });',
  4717. '};',
  4718. 'this.i = 0;'
  4719. ]),
  4720. LinesToStr([
  4721. '$mod.DoIt($mod.i,$mod.i,{',
  4722. ' p: $mod,',
  4723. ' get: function () {',
  4724. ' return this.p.i;',
  4725. ' },',
  4726. ' set: function (v) {',
  4727. ' this.p.i = v;',
  4728. ' }',
  4729. '});'
  4730. ]));
  4731. end;
  4732. procedure TTestModule.TestProc_VarParamString;
  4733. begin
  4734. StartProgram(false);
  4735. Add(['type TCaption = string;',
  4736. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4737. 'var c: char;',
  4738. 'begin',
  4739. ' va[1]:=c;',
  4740. ' vb[2]:=c;',
  4741. ' vc[3]:=c;',
  4742. 'end;',
  4743. 'begin']);
  4744. ConvertProgram;
  4745. CheckSource('TestProc_VarParamString',
  4746. LinesToStr([ // statements
  4747. 'this.DoIt = function (vA,vB,vC) {',
  4748. ' var c = "";',
  4749. ' vA = rtl.setCharAt(vA, 0, c);',
  4750. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4751. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4752. '};',
  4753. '']),
  4754. LinesToStr([
  4755. ]));
  4756. end;
  4757. procedure TTestModule.TestProc_VarParamV;
  4758. begin
  4759. StartProgram(false);
  4760. Add([
  4761. 'procedure Inc2(var i: longint);',
  4762. 'begin',
  4763. ' i:=i+2;',
  4764. 'end;',
  4765. 'procedure DoIt(v: longint);',
  4766. 'var p: array of longint;',
  4767. 'begin',
  4768. ' Inc2(v);',
  4769. ' Inc2(p[v]);',
  4770. 'end;',
  4771. 'begin']);
  4772. ConvertProgram;
  4773. CheckSource('TestProc_VarParamV',
  4774. LinesToStr([ // statements
  4775. 'this.Inc2 = function (i) {',
  4776. ' i.set(i.get()+2);',
  4777. '};',
  4778. 'this.DoIt = function (v) {',
  4779. ' var p = [];',
  4780. ' $mod.Inc2({get: function () {',
  4781. ' return v;',
  4782. ' }, set: function (w) {',
  4783. ' v = w;',
  4784. ' }});',
  4785. ' $mod.Inc2({',
  4786. ' a: v,',
  4787. ' p: p,',
  4788. ' get: function () {',
  4789. ' return this.p[this.a];',
  4790. ' },',
  4791. ' set: function (v) {',
  4792. ' this.p[this.a] = v;',
  4793. ' }',
  4794. ' });',
  4795. '};',
  4796. '']),
  4797. LinesToStr([
  4798. '']));
  4799. end;
  4800. procedure TTestModule.TestProc_Overload;
  4801. begin
  4802. StartProgram(false);
  4803. Add('procedure DoIt(vI: longint); begin end;');
  4804. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4805. Add('procedure DoIt(vD: double); begin end;');
  4806. Add('begin');
  4807. Add(' DoIt(1);');
  4808. Add(' DoIt(2,3);');
  4809. Add(' DoIt(4.5);');
  4810. ConvertProgram;
  4811. CheckSource('TestProcedureOverload',
  4812. LinesToStr([ // statements
  4813. 'this.DoIt = function (vI) {',
  4814. '};',
  4815. 'this.DoIt$1 = function (vI, vJ) {',
  4816. '};',
  4817. 'this.DoIt$2 = function (vD) {',
  4818. '};',
  4819. '']),
  4820. LinesToStr([
  4821. '$mod.DoIt(1);',
  4822. '$mod.DoIt$1(2, 3);',
  4823. '$mod.DoIt$2(4.5);',
  4824. '']));
  4825. end;
  4826. procedure TTestModule.TestProc_OverloadForward;
  4827. begin
  4828. StartProgram(false);
  4829. Add('procedure DoIt(vI: longint); forward;');
  4830. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4831. Add('procedure doit(vi: longint); begin end;');
  4832. Add('begin');
  4833. Add(' doit(1);');
  4834. Add(' doit(2,3);');
  4835. ConvertProgram;
  4836. CheckSource('TestProcedureOverloadForward',
  4837. LinesToStr([ // statements
  4838. 'this.DoIt$1 = function (vI, vJ) {',
  4839. '};',
  4840. 'this.DoIt = function (vI) {',
  4841. '};',
  4842. '']),
  4843. LinesToStr([
  4844. '$mod.DoIt(1);',
  4845. '$mod.DoIt$1(2, 3);',
  4846. '']));
  4847. end;
  4848. procedure TTestModule.TestProc_OverloadIntfImpl;
  4849. begin
  4850. StartUnit(false);
  4851. Add('interface');
  4852. Add('procedure DoIt(vI: longint);');
  4853. Add('procedure DoIt(vI, vJ: longint);');
  4854. Add('implementation');
  4855. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4856. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4857. Add('procedure DoIt(vi: longint); begin end;');
  4858. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4859. Add('procedure DoIt(vi, vj: longint); begin end;');
  4860. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4861. Add('begin');
  4862. Add(' doit(1);');
  4863. Add(' doit(2,3);');
  4864. Add(' doit(4,5,6);');
  4865. Add(' doit(7,8,9,10);');
  4866. Add(' doit(11,12,13,14,15);');
  4867. ConvertUnit;
  4868. CheckSource('TestProcedureOverloadUnit',
  4869. LinesToStr([ // statements
  4870. 'var $impl = $mod.$impl;',
  4871. 'this.DoIt = function (vI) {',
  4872. '};',
  4873. 'this.DoIt$1 = function (vI, vJ) {',
  4874. '};',
  4875. '']),
  4876. LinesToStr([ // this.$init
  4877. '$mod.DoIt(1);',
  4878. '$mod.DoIt$1(2, 3);',
  4879. '$impl.DoIt$3(4,5,6);',
  4880. '$impl.DoIt$4(7,8,9,10);',
  4881. '$impl.DoIt$2(11,12,13,14,15);',
  4882. '']),
  4883. LinesToStr([ // implementation
  4884. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4885. '};',
  4886. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4887. '};',
  4888. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4889. '};',
  4890. '']));
  4891. end;
  4892. procedure TTestModule.TestProc_OverloadNested;
  4893. begin
  4894. StartProgram(false);
  4895. Add([
  4896. 'procedure doit(vA: longint);',
  4897. ' procedure DoIt(vA, vB: longint); overload;',
  4898. ' begin',
  4899. ' doit(1);',
  4900. ' doit(1,2);',
  4901. ' end;',
  4902. ' procedure doit(vA, vB, vC: longint);',
  4903. ' begin',
  4904. ' doit(1);',
  4905. ' doit(1,2);',
  4906. ' doit(1,2,3);',
  4907. ' end;',
  4908. 'begin',
  4909. ' doit(1);',
  4910. ' doit(1,2);',
  4911. ' doit(1,2,3);',
  4912. 'end;',
  4913. 'begin // main',
  4914. ' doit(1);']);
  4915. ConvertProgram;
  4916. CheckSource('TestProcedureOverloadNested',
  4917. LinesToStr([ // statements
  4918. 'this.doit = function (vA) {',
  4919. ' function DoIt$1(vA, vB) {',
  4920. ' $mod.doit(1);',
  4921. ' DoIt$1(1, 2);',
  4922. ' };',
  4923. ' function doit$2(vA, vB, vC) {',
  4924. ' $mod.doit(1);',
  4925. ' DoIt$1(1, 2);',
  4926. ' doit$2(1, 2, 3);',
  4927. ' };',
  4928. ' $mod.doit(1);',
  4929. ' DoIt$1(1, 2);',
  4930. ' doit$2(1, 2, 3);',
  4931. '};',
  4932. '']),
  4933. LinesToStr([
  4934. '$mod.doit(1);',
  4935. '']));
  4936. end;
  4937. procedure TTestModule.TestProc_OverloadNestedForward;
  4938. begin
  4939. StartProgram(false);
  4940. Add([
  4941. 'procedure DoIt(vA: longint); overload; forward;',
  4942. 'procedure DoIt(vB, vC: longint); overload;',
  4943. 'begin // 2 param overload',
  4944. ' doit(1);',
  4945. ' doit(1,2);',
  4946. 'end;',
  4947. 'procedure doit(vA: longint);',
  4948. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4949. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4950. ' begin // 4 param overload',
  4951. ' doit(1);',
  4952. ' doit(1,2);',
  4953. ' doit(1,2,3);',
  4954. ' doit(1,2,3,4);',
  4955. ' end;',
  4956. ' procedure doit(vA, vB, vC: longint);',
  4957. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4958. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4959. ' begin // 6 param overload',
  4960. ' doit(1);',
  4961. ' doit(1,2);',
  4962. ' doit(1,2,3);',
  4963. ' doit(1,2,3,4);',
  4964. ' doit(1,2,3,4,5);',
  4965. ' doit(1,2,3,4,5,6);',
  4966. ' end;',
  4967. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4968. ' begin // 5 param overload',
  4969. ' doit(1);',
  4970. ' doit(1,2);',
  4971. ' doit(1,2,3);',
  4972. ' doit(1,2,3,4);',
  4973. ' doit(1,2,3,4,5);',
  4974. ' doit(1,2,3,4,5,6);',
  4975. ' end;',
  4976. ' begin // 3 param overload',
  4977. ' doit(1);',
  4978. ' doit(1,2);',
  4979. ' doit(1,2,3);',
  4980. ' doit(1,2,3,4);',
  4981. ' doit(1,2,3,4,5);',
  4982. ' doit(1,2,3,4,5,6);',
  4983. ' end;',
  4984. 'begin // 1 param overload',
  4985. ' doit(1);',
  4986. ' doit(1,2);',
  4987. ' doit(1,2,3);',
  4988. ' doit(1,2,3,4);',
  4989. 'end;',
  4990. 'begin // main',
  4991. ' doit(1);',
  4992. ' doit(1,2);']);
  4993. ConvertProgram;
  4994. CheckSource('TestProc_OverloadNestedForward',
  4995. LinesToStr([ // statements
  4996. 'this.DoIt$1 = function (vB, vC) {',
  4997. ' $mod.DoIt(1);',
  4998. ' $mod.DoIt$1(1, 2);',
  4999. '};',
  5000. 'this.DoIt = function (vA) {',
  5001. ' function DoIt$3(vA, vB, vC, vD) {',
  5002. ' $mod.DoIt(1);',
  5003. ' $mod.DoIt$1(1, 2);',
  5004. ' DoIt$2(1, 2, 3);',
  5005. ' DoIt$3(1, 2, 3, 4);',
  5006. ' };',
  5007. ' function DoIt$2(vA, vB, vC) {',
  5008. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5009. ' $mod.DoIt(1);',
  5010. ' $mod.DoIt$1(1, 2);',
  5011. ' DoIt$2(1, 2, 3);',
  5012. ' DoIt$3(1, 2, 3, 4);',
  5013. ' DoIt$4(1, 2, 3, 4, 5);',
  5014. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5015. ' };',
  5016. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5017. ' $mod.DoIt(1);',
  5018. ' $mod.DoIt$1(1, 2);',
  5019. ' DoIt$2(1, 2, 3);',
  5020. ' DoIt$3(1, 2, 3, 4);',
  5021. ' DoIt$4(1, 2, 3, 4, 5);',
  5022. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5023. ' };',
  5024. ' $mod.DoIt(1);',
  5025. ' $mod.DoIt$1(1, 2);',
  5026. ' DoIt$2(1, 2, 3);',
  5027. ' DoIt$3(1, 2, 3, 4);',
  5028. ' DoIt$4(1, 2, 3, 4, 5);',
  5029. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5030. ' };',
  5031. ' $mod.DoIt(1);',
  5032. ' $mod.DoIt$1(1, 2);',
  5033. ' DoIt$2(1, 2, 3);',
  5034. ' DoIt$3(1, 2, 3, 4);',
  5035. '};',
  5036. '']),
  5037. LinesToStr([
  5038. '$mod.DoIt(1);',
  5039. '$mod.DoIt$1(1, 2);',
  5040. '']));
  5041. end;
  5042. procedure TTestModule.TestProc_OverloadUnitCycle;
  5043. begin
  5044. AddModuleWithIntfImplSrc('Unit2.pas',
  5045. LinesToStr([
  5046. 'type',
  5047. ' TObject = class',
  5048. ' procedure DoIt(b: boolean); virtual; abstract;',
  5049. ' procedure DoIt(i: longint); virtual; abstract;',
  5050. ' end;',
  5051. '']),
  5052. 'uses test1;');
  5053. StartUnit(true);
  5054. Add([
  5055. 'interface',
  5056. 'uses unit2;',
  5057. 'type',
  5058. ' TEagle = class(TObject)',
  5059. ' procedure DoIt(b: boolean); override;',
  5060. ' procedure DoIt(i: longint); override;',
  5061. ' end;',
  5062. 'implementation',
  5063. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5064. 'procedure TEagle.DoIt(i: longint); begin end;',
  5065. '']);
  5066. ConvertUnit;
  5067. CheckSource('TestProc_OverloadUnitCycle',
  5068. LinesToStr([ // statements
  5069. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5070. ' this.DoIt = function (b) {',
  5071. ' };',
  5072. ' this.DoIt$1 = function (i) {',
  5073. ' };',
  5074. '});',
  5075. '']),
  5076. '',
  5077. LinesToStr([
  5078. '']));
  5079. end;
  5080. procedure TTestModule.TestProc_Varargs;
  5081. begin
  5082. StartProgram(false);
  5083. Add([
  5084. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5085. 'procedure ProcB; varargs; external name ''ProcB'';',
  5086. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5087. 'function GetIt: longint; begin end;',
  5088. 'begin',
  5089. ' ProcA(1);',
  5090. ' ProcA(1,2);',
  5091. ' ProcA(1,2.0);',
  5092. ' ProcA(1,2,3);',
  5093. ' ProcA(1,''2'');',
  5094. ' ProcA(2,'''');',
  5095. ' ProcA(3,false);',
  5096. ' ProcB;',
  5097. ' ProcB();',
  5098. ' ProcB(4);',
  5099. ' ProcB(''foo'');',
  5100. ' ProcC;',
  5101. ' ProcC();',
  5102. ' ProcC(4);',
  5103. ' ProcC(5,''foo'');',
  5104. ' ProcB(GetIt);',
  5105. ' ProcB(GetIt());',
  5106. ' ProcB(GetIt,GetIt());']);
  5107. ConvertProgram;
  5108. CheckSource('TestProc_Varargs',
  5109. LinesToStr([ // statements
  5110. 'this.GetIt = function () {',
  5111. ' var Result = 0;',
  5112. ' return Result;',
  5113. '};',
  5114. '']),
  5115. LinesToStr([
  5116. 'ProcA(1);',
  5117. 'ProcA(1, 2);',
  5118. 'ProcA(1, 2.0);',
  5119. 'ProcA(1, 2, 3);',
  5120. 'ProcA(1, "2");',
  5121. 'ProcA(2, "");',
  5122. 'ProcA(3, false);',
  5123. 'ProcB();',
  5124. 'ProcB();',
  5125. 'ProcB(4);',
  5126. 'ProcB("foo");',
  5127. 'ProcC(17);',
  5128. 'ProcC(17);',
  5129. 'ProcC(4);',
  5130. 'ProcC(5, "foo");',
  5131. 'ProcB($mod.GetIt());',
  5132. 'ProcB($mod.GetIt());',
  5133. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5134. '']));
  5135. end;
  5136. procedure TTestModule.TestProc_ConstOrder;
  5137. begin
  5138. StartProgram(false);
  5139. Add([
  5140. 'const A = 3;',
  5141. 'const B = A+1;',
  5142. 'procedure DoIt;',
  5143. 'const C = A+1;',
  5144. 'const D = B+1;',
  5145. 'const E = D+C+B+A;',
  5146. 'begin',
  5147. 'end;',
  5148. 'begin'
  5149. ]);
  5150. ConvertProgram;
  5151. CheckSource('TestProc_ConstOrder',
  5152. LinesToStr([ // statements
  5153. 'this.A = 3;',
  5154. 'this.B = 3 + 1;',
  5155. 'var C = 3 + 1;',
  5156. 'var D = 4 + 1;',
  5157. 'var E = 5 + 4 + 4 + 3;',
  5158. 'this.DoIt = function () {',
  5159. '};',
  5160. '']),
  5161. LinesToStr([
  5162. ''
  5163. ]));
  5164. end;
  5165. procedure TTestModule.TestProc_DuplicateConst;
  5166. begin
  5167. StartProgram(false);
  5168. Add([
  5169. 'const A = 1;',
  5170. 'procedure DoIt;',
  5171. 'const A = 2;',
  5172. ' procedure SubIt;',
  5173. ' const A = 21;',
  5174. ' begin',
  5175. ' end;',
  5176. 'begin',
  5177. 'end;',
  5178. 'procedure DoSome;',
  5179. 'const A = 3;',
  5180. 'begin',
  5181. 'end;',
  5182. 'begin'
  5183. ]);
  5184. ConvertProgram;
  5185. CheckSource('TestProc_DuplicateConst',
  5186. LinesToStr([ // statements
  5187. 'this.A = 1;',
  5188. 'var A$1 = 2;',
  5189. 'var A$2 = 21;',
  5190. 'this.DoIt = function () {',
  5191. ' function SubIt() {',
  5192. ' };',
  5193. '};',
  5194. 'var A$3 = 3;',
  5195. 'this.DoSome = function () {',
  5196. '};',
  5197. '']),
  5198. LinesToStr([
  5199. ''
  5200. ]));
  5201. end;
  5202. procedure TTestModule.TestProc_LocalVarAbsolute;
  5203. begin
  5204. StartProgram(false);
  5205. Add([
  5206. 'type',
  5207. ' TObject = class',
  5208. ' Index: longint;',
  5209. ' procedure DoAbs(Item: pointer);',
  5210. ' end;',
  5211. 'procedure TObject.DoAbs(Item: pointer);',
  5212. 'var',
  5213. ' o: TObject absolute Item;',
  5214. 'begin',
  5215. ' if o.Index<o.Index then o.Index:=o.Index;',
  5216. 'end;',
  5217. 'procedure DoIt(i: longint; p: pointer);',
  5218. 'var',
  5219. ' d: double absolute i;',
  5220. ' s: string absolute d;',
  5221. ' oi: TObject absolute i;',
  5222. ' op: TObject absolute p;',
  5223. 'begin',
  5224. ' if d=d then d:=d;',
  5225. ' if s=s then s:=s;',
  5226. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5227. ' if op.Index=op.Index then op.Index:=op.Index;',
  5228. 'end;',
  5229. 'begin']);
  5230. ConvertProgram;
  5231. CheckSource('TestProc_LocalVarAbsolute',
  5232. LinesToStr([ // statements
  5233. 'rtl.createClass(this, "TObject", null, function () {',
  5234. ' this.$init = function () {',
  5235. ' this.Index = 0;',
  5236. ' };',
  5237. ' this.$final = function () {',
  5238. ' };',
  5239. ' this.DoAbs = function (Item) {',
  5240. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5241. ' };',
  5242. '});',
  5243. 'this.DoIt = function (i, p) {',
  5244. ' if (i === i) i = i;',
  5245. ' if (i === i) i = i;',
  5246. ' if (i.Index < i.Index) i.Index = i.Index;',
  5247. ' if (p.Index === p.Index) p.Index = p.Index;',
  5248. '};'
  5249. ]),
  5250. LinesToStr([
  5251. ]));
  5252. end;
  5253. procedure TTestModule.TestProc_ResultAbsolute;
  5254. begin
  5255. StartProgram(false);
  5256. Add([
  5257. 'type',
  5258. ' TObject = class',
  5259. ' Index: longint;',
  5260. ' function DoAbs: pointer;',
  5261. ' end;',
  5262. 'function TObject.DoAbs: pointer;',
  5263. 'var',
  5264. ' o: TObject absolute Result;',
  5265. 'begin',
  5266. ' if o.Index<o.Index then o.Index:=o.Index;',
  5267. 'end;',
  5268. 'function DoIt: jsvalue;',
  5269. 'var',
  5270. ' d: double absolute Result;',
  5271. ' s: string absolute Result;',
  5272. ' o: TObject absolute Result;',
  5273. 'begin',
  5274. ' if d=d then d:=d;',
  5275. ' if s=s then s:=s;',
  5276. ' if o.Index<o.Index then o.Index:=o.Index;',
  5277. 'end;',
  5278. 'begin']);
  5279. ConvertProgram;
  5280. CheckSource('TestProc_ResultAbsolute',
  5281. LinesToStr([ // statements
  5282. 'rtl.createClass(this, "TObject", null, function () {',
  5283. ' this.$init = function () {',
  5284. ' this.Index = 0;',
  5285. ' };',
  5286. ' this.$final = function () {',
  5287. ' };',
  5288. ' this.DoAbs = function () {',
  5289. ' var Result = null;',
  5290. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5291. ' return Result;',
  5292. ' };',
  5293. '});',
  5294. 'this.DoIt = function () {',
  5295. ' var Result = undefined;',
  5296. ' if (Result === Result) Result = Result;',
  5297. ' if (Result === Result) Result = Result;',
  5298. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5299. ' return Result;',
  5300. '};',
  5301. '']),
  5302. LinesToStr([
  5303. ]));
  5304. end;
  5305. procedure TTestModule.TestProc_LocalVarInit;
  5306. begin
  5307. StartProgram(false);
  5308. Add([
  5309. 'type TBytes = array of byte;',
  5310. 'procedure DoIt;',
  5311. 'const c = 4;',
  5312. 'var',
  5313. ' b: byte = 1;',
  5314. ' w: word = 2+c;',
  5315. ' p: pointer = nil;',
  5316. ' Buffer: TBytes = nil;',
  5317. 'begin',
  5318. 'end;',
  5319. 'begin']);
  5320. ConvertProgram;
  5321. CheckSource('TestProc_LocalVarInit',
  5322. LinesToStr([ // statements
  5323. 'var c = 4;',
  5324. 'this.DoIt = function () {',
  5325. ' var b = 1;',
  5326. ' var w = 2 + 4;',
  5327. ' var p = null;',
  5328. ' var Buffer = [];',
  5329. '};',
  5330. '']),
  5331. LinesToStr([
  5332. ]));
  5333. end;
  5334. procedure TTestModule.TestProc_ReservedWords;
  5335. begin
  5336. StartProgram(false);
  5337. Add([
  5338. 'procedure Date(ArrayBuffer: longint);',
  5339. 'const',
  5340. ' NaN: longint = 3;',
  5341. 'var',
  5342. ' &Boolean: longint;',
  5343. ' procedure Error(ArrayBuffer: longint);',
  5344. ' begin',
  5345. ' end;',
  5346. 'begin',
  5347. ' Nan:=&bOolean;',
  5348. 'end;',
  5349. 'begin',
  5350. ' Date(1);']);
  5351. ConvertProgram;
  5352. CheckSource('TestProc_ReservedWords',
  5353. LinesToStr([ // statements
  5354. 'var naN = 3;',
  5355. 'this.Date = function (arrayBuffer) {',
  5356. ' var boolean = 0;',
  5357. ' function error(arrayBuffer) {',
  5358. ' };',
  5359. ' naN = boolean;',
  5360. '};',
  5361. '']),
  5362. LinesToStr([
  5363. ' $mod.Date(1);'
  5364. ]));
  5365. end;
  5366. procedure TTestModule.TestProc_ConstRefWord;
  5367. begin
  5368. StartProgram(false);
  5369. Add([
  5370. 'procedure Run(constref w: word);',
  5371. 'var l: word;',
  5372. 'begin',
  5373. ' l:=w;',
  5374. ' Run(w);',
  5375. ' Run(l);',
  5376. 'end;',
  5377. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5378. 'begin',
  5379. ' Run(a);',
  5380. ' Run(b);',
  5381. ' Run(c);',
  5382. ' Run(d);',
  5383. ' Run(e);',
  5384. 'end;',
  5385. 'begin',
  5386. ' Run(1);']);
  5387. ConvertProgram;
  5388. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5389. CheckSource('TestProc_ConstRefWord',
  5390. LinesToStr([ // statements
  5391. 'this.Run = function (w) {',
  5392. ' var l = 0;',
  5393. ' l = w;',
  5394. ' $mod.Run(w);',
  5395. ' $mod.Run(l);',
  5396. '};',
  5397. 'this.Fly = function (a, b, c, d, e) {',
  5398. ' $mod.Run(a);',
  5399. ' $mod.Run(b.get());',
  5400. ' $mod.Run(c.get());',
  5401. ' $mod.Run(d);',
  5402. ' $mod.Run(e);',
  5403. '};',
  5404. '']),
  5405. LinesToStr([
  5406. '$mod.Run(1);'
  5407. ]));
  5408. end;
  5409. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5410. begin
  5411. StartProgram(false);
  5412. Add([
  5413. '{$mode objfpc}',
  5414. 'type',
  5415. ' TFunc = reference to function(x: word): word;',
  5416. 'var Func: TFunc;',
  5417. 'procedure DoIt(a: word);',
  5418. 'begin',
  5419. ' Func:=function(b:word): word',
  5420. ' begin',
  5421. ' Result:=a+b;',
  5422. ' exit(b);',
  5423. ' exit(Result);',
  5424. ' end;',// test semicolon
  5425. ' a:=3;',
  5426. 'end;',
  5427. 'begin',
  5428. ' Func:=function(c:word):word begin',
  5429. ' Result:=3+c;',
  5430. ' exit(c);',
  5431. ' exit(Result);',
  5432. ' end;']);
  5433. ConvertProgram;
  5434. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5435. LinesToStr([ // statements
  5436. 'this.Func = null;',
  5437. 'this.DoIt = function (a) {',
  5438. ' $mod.Func = function (b) {',
  5439. ' var Result = 0;',
  5440. ' Result = a + b;',
  5441. ' return b;',
  5442. ' return Result;',
  5443. ' return Result;',
  5444. ' };',
  5445. ' a = 3;',
  5446. '};',
  5447. '']),
  5448. LinesToStr([
  5449. '$mod.Func = function (c) {',
  5450. ' var Result = 0;',
  5451. ' Result = 3 + c;',
  5452. ' return c;',
  5453. ' return Result;',
  5454. ' return Result;',
  5455. '};',
  5456. '']));
  5457. end;
  5458. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5459. begin
  5460. StartProgram(false);
  5461. Add([
  5462. '{$mode delphi}',
  5463. 'type',
  5464. ' TProc = reference to procedure(x: word);',
  5465. 'procedure DoIt(a: word);',
  5466. 'var Proc: TProc;',
  5467. 'begin',
  5468. ' Proc:=procedure(b:word) begin end;',
  5469. 'end;',
  5470. 'var Proc: TProc;',
  5471. 'begin',
  5472. ' Proc:=procedure(c:word) begin end;',
  5473. '']);
  5474. ConvertProgram;
  5475. CheckSource('TestAnonymousProc_Assign_Delphi',
  5476. LinesToStr([ // statements
  5477. 'this.DoIt = function (a) {',
  5478. ' var Proc = null;',
  5479. ' Proc = function (b) {',
  5480. ' };',
  5481. '};',
  5482. 'this.Proc = null;',
  5483. '']),
  5484. LinesToStr([
  5485. '$mod.Proc = function (c) {',
  5486. '};',
  5487. '']));
  5488. end;
  5489. procedure TTestModule.TestAnonymousProc_Arg;
  5490. begin
  5491. StartProgram(false);
  5492. Add([
  5493. 'type',
  5494. ' TProc = reference to procedure;',
  5495. ' TFunc = reference to function(x: word): word;',
  5496. 'procedure DoMore(f,g: TProc);',
  5497. 'begin',
  5498. 'end;',
  5499. 'procedure DoOdd(v: jsvalue);',
  5500. 'begin',
  5501. 'end;',
  5502. 'procedure DoIt(f: TFunc);',
  5503. 'begin',
  5504. ' DoIt(function(b:word): word',
  5505. ' begin',
  5506. ' Result:=1+b;',
  5507. ' end);',
  5508. ' DoMore(procedure begin end, procedure begin end);',
  5509. ' DoOdd(procedure begin end);',
  5510. 'end;',
  5511. 'begin',
  5512. ' DoMore(procedure begin end,',
  5513. ' procedure assembler asm',
  5514. ' console.log("c");',
  5515. ' end);',
  5516. '']);
  5517. ConvertProgram;
  5518. CheckSource('TestAnonymousProc_Arg',
  5519. LinesToStr([ // statements
  5520. 'this.DoMore = function (f, g) {',
  5521. '};',
  5522. 'this.DoOdd = function (v) {',
  5523. '};',
  5524. 'this.DoIt = function (f) {',
  5525. ' $mod.DoIt(function (b) {',
  5526. ' var Result = 0;',
  5527. ' Result = 1 + b;',
  5528. ' return Result;',
  5529. ' });',
  5530. ' $mod.DoMore(function () {',
  5531. ' }, function () {',
  5532. ' });',
  5533. ' $mod.DoOdd(function () {',
  5534. ' });',
  5535. '};',
  5536. '']),
  5537. LinesToStr([
  5538. '$mod.DoMore(function () {',
  5539. '}, function () {',
  5540. ' console.log("c");',
  5541. '});',
  5542. '']));
  5543. end;
  5544. procedure TTestModule.TestAnonymousProc_Typecast;
  5545. begin
  5546. StartProgram(false);
  5547. Add([
  5548. 'type',
  5549. ' TProc = reference to procedure(w: word);',
  5550. ' TArr = array of word;',
  5551. ' TFuncArr = reference to function: TArr;',
  5552. 'procedure DoIt(p: TProc);',
  5553. 'var',
  5554. ' w: word;',
  5555. ' a: TArr;',
  5556. 'begin',
  5557. ' p:=TProc(procedure(b: smallint) begin end);',
  5558. ' a:=TFuncArr(function: TArr begin end)();',
  5559. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5560. 'end;',
  5561. 'begin']);
  5562. ConvertProgram;
  5563. CheckSource('TestAnonymousProc_Typecast',
  5564. LinesToStr([ // statements
  5565. 'this.DoIt = function (p) {',
  5566. ' var w = 0;',
  5567. ' var a = [];',
  5568. ' p = function (b) {',
  5569. ' };',
  5570. ' a = function () {',
  5571. ' var Result = [];',
  5572. ' return Result;',
  5573. ' }();',
  5574. ' w = function () {',
  5575. ' var Result = [];',
  5576. ' return Result;',
  5577. ' }()[3];',
  5578. '};',
  5579. '']),
  5580. LinesToStr([
  5581. '']));
  5582. end;
  5583. procedure TTestModule.TestAnonymousProc_With;
  5584. begin
  5585. StartProgram(false);
  5586. Add([
  5587. 'type',
  5588. ' TProc = reference to procedure(w: word);',
  5589. ' TObject = class',
  5590. ' b: boolean;',
  5591. ' end;',
  5592. 'var',
  5593. ' p: TProc;',
  5594. ' bird: TObject;',
  5595. 'begin',
  5596. ' with bird do',
  5597. ' p:=procedure(w: word)',
  5598. ' begin',
  5599. ' b:=w>2;',
  5600. ' end;',
  5601. '']);
  5602. ConvertProgram;
  5603. CheckSource('TestAnonymousProc_With',
  5604. LinesToStr([ // statements
  5605. 'rtl.createClass(this, "TObject", null, function () {',
  5606. ' this.$init = function () {',
  5607. ' this.b = false;',
  5608. ' };',
  5609. ' this.$final = function () {',
  5610. ' };',
  5611. '});',
  5612. 'this.p = null;',
  5613. 'this.bird = null;',
  5614. '']),
  5615. LinesToStr([
  5616. 'var $with = $mod.bird;',
  5617. '$mod.p = function (w) {',
  5618. ' $with.b = w > 2;',
  5619. '};',
  5620. '']));
  5621. end;
  5622. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5623. begin
  5624. StartProgram(false);
  5625. Add([
  5626. 'type',
  5627. ' TProc = reference to procedure;',
  5628. ' TObject = class',
  5629. ' b: boolean;',
  5630. ' end;',
  5631. 'procedure DoIt;',
  5632. 'var',
  5633. ' p: TProc;',
  5634. 'begin',
  5635. ' try',
  5636. ' except',
  5637. ' on E: TObject do',
  5638. ' p:=procedure',
  5639. ' begin',
  5640. ' E.b:=true;',
  5641. ' end;',
  5642. ' end;',
  5643. 'end;',
  5644. 'begin']);
  5645. ConvertProgram;
  5646. CheckSource('TestAnonymousProc_ExceptOn',
  5647. LinesToStr([ // statements
  5648. 'rtl.createClass(this, "TObject", null, function () {',
  5649. ' this.$init = function () {',
  5650. ' this.b = false;',
  5651. ' };',
  5652. ' this.$final = function () {',
  5653. ' };',
  5654. '});',
  5655. 'this.DoIt = function () {',
  5656. ' var p = null;',
  5657. ' try {} catch ($e) {',
  5658. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5659. ' var E = $e;',
  5660. ' p = function () {',
  5661. ' E.b = true;',
  5662. ' };',
  5663. ' } else throw $e',
  5664. ' };',
  5665. '};',
  5666. '']),
  5667. LinesToStr([
  5668. '']));
  5669. end;
  5670. procedure TTestModule.TestAnonymousProc_Nested;
  5671. begin
  5672. StartProgram(false);
  5673. Add([
  5674. 'type',
  5675. ' TProc = reference to procedure;',
  5676. ' TObject = class',
  5677. ' i: byte;',
  5678. ' procedure DoIt;',
  5679. ' end;',
  5680. 'procedure TObject.DoIt;',
  5681. 'var',
  5682. ' p: TProc;',
  5683. ' procedure Sub;',
  5684. ' begin',
  5685. ' p:=procedure',
  5686. ' begin',
  5687. ' i:=3;',
  5688. ' Self.i:=4;',
  5689. ' p:=procedure',
  5690. ' procedure SubSub;',
  5691. ' begin',
  5692. ' i:=13;',
  5693. ' Self.i:=14;',
  5694. ' end;',
  5695. ' begin',
  5696. ' i:=13;',
  5697. ' Self.i:=14;',
  5698. ' end;',
  5699. ' end;',
  5700. ' end;',
  5701. 'begin',
  5702. 'end;',
  5703. 'begin']);
  5704. ConvertProgram;
  5705. CheckSource('TestAnonymousProc_Nested',
  5706. LinesToStr([ // statements
  5707. 'rtl.createClass(this, "TObject", null, function () {',
  5708. ' this.$init = function () {',
  5709. ' this.i = 0;',
  5710. ' };',
  5711. ' this.$final = function () {',
  5712. ' };',
  5713. ' this.DoIt = function () {',
  5714. ' var $Self = this;',
  5715. ' var p = null;',
  5716. ' function Sub() {',
  5717. ' p = function () {',
  5718. ' $Self.i = 3;',
  5719. ' $Self.i = 4;',
  5720. ' p = function () {',
  5721. ' function SubSub() {',
  5722. ' $Self.i = 13;',
  5723. ' $Self.i = 14;',
  5724. ' };',
  5725. ' $Self.i = 13;',
  5726. ' $Self.i = 14;',
  5727. ' };',
  5728. ' };',
  5729. ' };',
  5730. ' };',
  5731. '});',
  5732. '']),
  5733. LinesToStr([
  5734. '']));
  5735. end;
  5736. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5737. begin
  5738. StartProgram(false);
  5739. Add([
  5740. 'type',
  5741. ' TProc = reference to procedure;',
  5742. 'function DoIt: TProc;',
  5743. ' function Sub: TProc;',
  5744. ' begin',
  5745. ' Result:=procedure',
  5746. ' begin',
  5747. ' Sub:=procedure',
  5748. ' procedure SubSub;',
  5749. ' begin',
  5750. ' Result:=nil;',
  5751. ' Sub:=nil;',
  5752. ' DoIt:=nil;',
  5753. ' end;',
  5754. ' begin',
  5755. ' Result:=nil;',
  5756. ' Sub:=nil;',
  5757. ' DoIt:=nil;',
  5758. ' end;',
  5759. ' end;',
  5760. ' end;',
  5761. 'begin',
  5762. 'end;',
  5763. 'begin']);
  5764. ConvertProgram;
  5765. CheckSource('TestAnonymousProc_NestedAssignResult',
  5766. LinesToStr([ // statements
  5767. 'this.DoIt = function () {',
  5768. ' var Result = null;',
  5769. ' function Sub() {',
  5770. ' var Result$1 = null;',
  5771. ' Result$1 = function () {',
  5772. ' Result$1 = function () {',
  5773. ' function SubSub() {',
  5774. ' Result$1 = null;',
  5775. ' Result$1 = null;',
  5776. ' Result = null;',
  5777. ' };',
  5778. ' Result$1 = null;',
  5779. ' Result$1 = null;',
  5780. ' Result = null;',
  5781. ' };',
  5782. ' };',
  5783. ' return Result$1;',
  5784. ' };',
  5785. ' return Result;',
  5786. '};',
  5787. '']),
  5788. LinesToStr([
  5789. '']));
  5790. end;
  5791. procedure TTestModule.TestAnonymousProc_Class;
  5792. begin
  5793. StartProgram(false);
  5794. Add([
  5795. 'type',
  5796. ' TProc = reference to procedure;',
  5797. ' TEvent = procedure of object;',
  5798. ' TObject = class',
  5799. ' Size: word;',
  5800. ' function GetIt: TProc;',
  5801. ' procedure DoIt; virtual; abstract;',
  5802. ' end;',
  5803. 'function TObject.GetIt: TProc;',
  5804. 'begin',
  5805. ' Result:=procedure',
  5806. ' var p: TEvent;',
  5807. ' begin',
  5808. ' Size:=Size;',
  5809. ' Size:=Self.Size;',
  5810. ' p:=@DoIt;',
  5811. ' p:[email protected];',
  5812. ' end;',
  5813. 'end;',
  5814. 'begin']);
  5815. ConvertProgram;
  5816. CheckSource('TestAnonymousProc_Class',
  5817. LinesToStr([ // statements
  5818. 'rtl.createClass(this, "TObject", null, function () {',
  5819. ' this.$init = function () {',
  5820. ' this.Size = 0;',
  5821. ' };',
  5822. ' this.$final = function () {',
  5823. ' };',
  5824. ' this.GetIt = function () {',
  5825. ' var $Self = this;',
  5826. ' var Result = null;',
  5827. ' Result = function () {',
  5828. ' var p = null;',
  5829. ' $Self.Size = $Self.Size;',
  5830. ' $Self.Size = $Self.Size;',
  5831. ' p = rtl.createCallback($Self, "DoIt");',
  5832. ' p = rtl.createCallback($Self, "DoIt");',
  5833. ' };',
  5834. ' return Result;',
  5835. ' };',
  5836. '});',
  5837. '']),
  5838. LinesToStr([
  5839. '']));
  5840. end;
  5841. procedure TTestModule.TestAnonymousProc_ForLoop;
  5842. begin
  5843. StartProgram(false);
  5844. Add([
  5845. 'type TProc = reference to procedure;',
  5846. 'procedure Foo(p: TProc);',
  5847. 'begin',
  5848. 'end;',
  5849. 'procedure DoIt;',
  5850. 'var i: word;',
  5851. ' a: word;',
  5852. 'begin',
  5853. ' for i:=1 to 10 do begin',
  5854. ' Foo(procedure begin a:=3; end);',
  5855. ' end;',
  5856. 'end;',
  5857. 'begin',
  5858. ' DoIt;']);
  5859. ConvertProgram;
  5860. CheckSource('TestAnonymousProc_ForLoop',
  5861. LinesToStr([ // statements
  5862. 'this.Foo = function (p) {',
  5863. '};',
  5864. 'this.DoIt = function () {',
  5865. ' var i = 0;',
  5866. ' var a = 0;',
  5867. ' for (i = 1; i <= 10; i++) {',
  5868. ' $mod.Foo(function () {',
  5869. ' a = 3;',
  5870. ' });',
  5871. ' };',
  5872. '};',
  5873. '']),
  5874. LinesToStr([
  5875. '$mod.DoIt();'
  5876. ]));
  5877. end;
  5878. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5879. begin
  5880. StartProgram(false);
  5881. Add([
  5882. '{$mode delphi}',
  5883. 'type',
  5884. ' TProc = reference to procedure;',
  5885. ' TFunc = reference to function(x: word): word;',
  5886. 'procedure Run;',
  5887. 'asm',
  5888. 'end;',
  5889. 'procedure Walk(p: TProc; f: TFunc);',
  5890. 'begin',
  5891. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5892. 'end;',
  5893. 'begin',
  5894. ' Walk(procedure',
  5895. ' asm',
  5896. ' console.log("a");',
  5897. ' end,',
  5898. ' function(x: word): word asm',
  5899. ' console.log("c");',
  5900. ' end);',
  5901. '']);
  5902. ConvertProgram;
  5903. CheckSource('TestAnonymousProc_AsmDelphi',
  5904. LinesToStr([ // statements
  5905. 'this.Run = function () {',
  5906. '};',
  5907. 'this.Walk = function (p, f) {',
  5908. ' $mod.Walk(function () {',
  5909. ' }, function (b) {',
  5910. ' return 1+b;',
  5911. ' });',
  5912. '};',
  5913. '']),
  5914. LinesToStr([
  5915. '$mod.Walk(function () {',
  5916. ' console.log("a");',
  5917. '}, function (x) {',
  5918. ' console.log("c");',
  5919. '});',
  5920. '']));
  5921. end;
  5922. procedure TTestModule.TestEnum_Name;
  5923. begin
  5924. StartProgram(false);
  5925. Add('type TMyEnum = (Red, Green, Blue);');
  5926. Add('var e: TMyEnum;');
  5927. Add('var f: TMyEnum = Blue;');
  5928. Add('begin');
  5929. Add(' e:=green;');
  5930. Add(' e:=default(TMyEnum);');
  5931. ConvertProgram;
  5932. CheckSource('TestEnum_Name',
  5933. LinesToStr([ // statements
  5934. 'this.TMyEnum = {',
  5935. ' "0":"Red",',
  5936. ' Red:0,',
  5937. ' "1":"Green",',
  5938. ' Green:1,',
  5939. ' "2":"Blue",',
  5940. ' Blue:2',
  5941. ' };',
  5942. 'this.e = 0;',
  5943. 'this.f = this.TMyEnum.Blue;'
  5944. ]),
  5945. LinesToStr([
  5946. '$mod.e=$mod.TMyEnum.Green;',
  5947. '$mod.e=$mod.TMyEnum.Red;'
  5948. ]));
  5949. end;
  5950. procedure TTestModule.TestEnum_Number;
  5951. begin
  5952. Converter.Options:=Converter.Options+[coEnumNumbers];
  5953. StartProgram(false);
  5954. Add('type TMyEnum = (Red, Green);');
  5955. Add('var');
  5956. Add(' e: TMyEnum;');
  5957. Add(' f: TMyEnum = Green;');
  5958. Add(' i: longint;');
  5959. Add('begin');
  5960. Add(' e:=green;');
  5961. Add(' i:=longint(e);');
  5962. ConvertProgram;
  5963. CheckSource('TestEnumNumber',
  5964. LinesToStr([ // statements
  5965. 'this.TMyEnum = {',
  5966. ' "0":"Red",',
  5967. ' Red:0,',
  5968. ' "1":"Green",',
  5969. ' Green:1',
  5970. ' };',
  5971. 'this.e = 0;',
  5972. 'this.f = 1;',
  5973. 'this.i = 0;'
  5974. ]),
  5975. LinesToStr([
  5976. '$mod.e=1;',
  5977. '$mod.i=$mod.e;'
  5978. ]));
  5979. end;
  5980. procedure TTestModule.TestEnum_ConstFail;
  5981. begin
  5982. StartProgram(false);
  5983. Add([
  5984. 'type TMyEnum = (Red = 100, Green = 101);',
  5985. 'var',
  5986. ' e: TMyEnum;',
  5987. ' f: TMyEnum = Green;',
  5988. 'begin',
  5989. ' e:=green;']);
  5990. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5991. ConvertProgram;
  5992. end;
  5993. procedure TTestModule.TestEnum_Functions;
  5994. begin
  5995. StartProgram(false);
  5996. Add([
  5997. 'type TMyEnum = (Red, Green);',
  5998. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5999. 'var',
  6000. ' v: longint;',
  6001. ' s: string;',
  6002. 'begin',
  6003. ' val(s,e,v);',
  6004. ' val(s,e,i);',
  6005. 'end;',
  6006. 'var',
  6007. ' e: TMyEnum;',
  6008. ' i: longint;',
  6009. ' s: string;',
  6010. ' b: boolean;',
  6011. 'begin',
  6012. ' i:=ord(red);',
  6013. ' i:=ord(green);',
  6014. ' i:=ord(e);',
  6015. ' i:=ord(b);',
  6016. ' e:=low(tmyenum);',
  6017. ' e:=low(e);',
  6018. ' b:=low(boolean);',
  6019. ' e:=high(tmyenum);',
  6020. ' e:=high(e);',
  6021. ' b:=high(boolean);',
  6022. ' e:=pred(green);',
  6023. ' e:=pred(e);',
  6024. ' b:=pred(b);',
  6025. ' e:=succ(red);',
  6026. ' e:=succ(e);',
  6027. ' b:=succ(b);',
  6028. ' e:=tmyenum(1);',
  6029. ' e:=tmyenum(i);',
  6030. ' s:=str(e);',
  6031. ' str(e,s);',
  6032. ' str(red,s);',
  6033. ' s:=str(e:3);',
  6034. ' writestr(s,e:3,red);',
  6035. ' val(s,e,i);',
  6036. ' i:=longint(e);']);
  6037. ConvertProgram;
  6038. CheckSource('TestEnum_Functions',
  6039. LinesToStr([ // statements
  6040. 'this.TMyEnum = {',
  6041. ' "0":"Red",',
  6042. ' Red:0,',
  6043. ' "1":"Green",',
  6044. ' Green:1',
  6045. ' };',
  6046. 'this.DoIt = function (e, i) {',
  6047. ' var v = 0;',
  6048. ' var s = "";',
  6049. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6050. ' v = w;',
  6051. ' }));',
  6052. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6053. '};',
  6054. 'this.e = 0;',
  6055. 'this.i = 0;',
  6056. 'this.s = "";',
  6057. 'this.b = false;',
  6058. '']),
  6059. LinesToStr([
  6060. '$mod.i=$mod.TMyEnum.Red;',
  6061. '$mod.i=$mod.TMyEnum.Green;',
  6062. '$mod.i=$mod.e;',
  6063. '$mod.i=$mod.b+0;',
  6064. '$mod.e=$mod.TMyEnum.Red;',
  6065. '$mod.e=$mod.TMyEnum.Red;',
  6066. '$mod.b=false;',
  6067. '$mod.e=$mod.TMyEnum.Green;',
  6068. '$mod.e=$mod.TMyEnum.Green;',
  6069. '$mod.b=true;',
  6070. '$mod.e=$mod.TMyEnum.Green-1;',
  6071. '$mod.e=$mod.e-1;',
  6072. '$mod.b=false;',
  6073. '$mod.e=$mod.TMyEnum.Red+1;',
  6074. '$mod.e=$mod.e+1;',
  6075. '$mod.b=true;',
  6076. '$mod.e=1;',
  6077. '$mod.e=$mod.i;',
  6078. '$mod.s = $mod.TMyEnum[$mod.e];',
  6079. '$mod.s = $mod.TMyEnum[$mod.e];',
  6080. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6081. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6082. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6083. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6084. ' $mod.i = v;',
  6085. '});',
  6086. '$mod.i=$mod.e;',
  6087. '']));
  6088. end;
  6089. procedure TTestModule.TestEnumRg_Functions;
  6090. begin
  6091. StartProgram(false);
  6092. Add([
  6093. 'type',
  6094. ' TEnum = (Red, Green, Blue);',
  6095. ' TEnumRg = Green..Blue;',
  6096. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6097. 'var',
  6098. ' v: longint;',
  6099. ' s: string;',
  6100. 'begin',
  6101. ' val(s,e,v);',
  6102. ' val(s,e,i);',
  6103. 'end;',
  6104. 'var',
  6105. ' e: TEnumRg;',
  6106. ' i: longint;',
  6107. ' s: string;',
  6108. 'begin',
  6109. ' i:=ord(green);',
  6110. ' i:=ord(e);',
  6111. ' e:=low(tenumrg);',
  6112. ' e:=low(e);',
  6113. ' e:=high(tenumrg);',
  6114. ' e:=high(e);',
  6115. ' e:=pred(blue);',
  6116. ' e:=pred(e);',
  6117. ' e:=succ(green);',
  6118. ' e:=succ(e);',
  6119. ' e:=tenumrg(1);',
  6120. ' e:=tenumrg(i);',
  6121. ' s:=str(e);',
  6122. ' str(e,s);',
  6123. ' str(red,s);',
  6124. ' s:=str(e:3);',
  6125. ' writestr(s,e:3,blue);',
  6126. ' val(s,e,i);',
  6127. ' i:=longint(e);']);
  6128. ConvertProgram;
  6129. CheckSource('TestEnumRg_Functions',
  6130. LinesToStr([ // statements
  6131. 'this.TEnum = {',
  6132. ' "0":"Red",',
  6133. ' Red:0,',
  6134. ' "1":"Green",',
  6135. ' Green:1,',
  6136. ' "2":"Blue",',
  6137. ' Blue:2',
  6138. ' };',
  6139. 'this.DoIt = function (e, i) {',
  6140. ' var v = 0;',
  6141. ' var s = "";',
  6142. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6143. ' v = w;',
  6144. ' }));',
  6145. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6146. '};',
  6147. 'this.e = this.TEnum.Green;',
  6148. 'this.i = 0;',
  6149. 'this.s = "";',
  6150. '']),
  6151. LinesToStr([
  6152. '$mod.i=$mod.TEnum.Green;',
  6153. '$mod.i=$mod.e;',
  6154. '$mod.e=$mod.TEnum.Green;',
  6155. '$mod.e=$mod.TEnum.Green;',
  6156. '$mod.e=$mod.TEnum.Blue;',
  6157. '$mod.e=$mod.TEnum.Blue;',
  6158. '$mod.e=$mod.TEnum.Blue-1;',
  6159. '$mod.e=$mod.e-1;',
  6160. '$mod.e=$mod.TEnum.Green+1;',
  6161. '$mod.e=$mod.e+1;',
  6162. '$mod.e=1;',
  6163. '$mod.e=$mod.i;',
  6164. '$mod.s = $mod.TEnum[$mod.e];',
  6165. '$mod.s = $mod.TEnum[$mod.e];',
  6166. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6167. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6168. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6169. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6170. ' $mod.i = v;',
  6171. '});',
  6172. '$mod.i=$mod.e;',
  6173. '']));
  6174. end;
  6175. procedure TTestModule.TestEnum_AsParams;
  6176. begin
  6177. StartProgram(false);
  6178. Add('type TEnum = (Red,Blue);');
  6179. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6180. Add('var vJ: TEnum;');
  6181. Add('begin');
  6182. Add(' vg:=vg;');
  6183. Add(' vj:=vh;');
  6184. Add(' vi:=vi;');
  6185. Add(' doit(vg,vg,vg);');
  6186. Add(' doit(vh,vh,vj);');
  6187. Add(' doit(vi,vi,vi);');
  6188. Add(' doit(vj,vj,vj);');
  6189. Add('end;');
  6190. Add('var i: TEnum;');
  6191. Add('begin');
  6192. Add(' doit(i,i,i);');
  6193. ConvertProgram;
  6194. CheckSource('TestEnum_AsParams',
  6195. LinesToStr([ // statements
  6196. 'this.TEnum = {',
  6197. ' "0": "Red",',
  6198. ' Red: 0,',
  6199. ' "1": "Blue",',
  6200. ' Blue: 1',
  6201. '};',
  6202. 'this.DoIt = function (vG,vH,vI) {',
  6203. ' var vJ = 0;',
  6204. ' vG = vG;',
  6205. ' vJ = vH;',
  6206. ' vI.set(vI.get());',
  6207. ' $mod.DoIt(vG, vG, {',
  6208. ' get: function () {',
  6209. ' return vG;',
  6210. ' },',
  6211. ' set: function (v) {',
  6212. ' vG = v;',
  6213. ' }',
  6214. ' });',
  6215. ' $mod.DoIt(vH, vH, {',
  6216. ' get: function () {',
  6217. ' return vJ;',
  6218. ' },',
  6219. ' set: function (v) {',
  6220. ' vJ = v;',
  6221. ' }',
  6222. ' });',
  6223. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6224. ' $mod.DoIt(vJ, vJ, {',
  6225. ' get: function () {',
  6226. ' return vJ;',
  6227. ' },',
  6228. ' set: function (v) {',
  6229. ' vJ = v;',
  6230. ' }',
  6231. ' });',
  6232. '};',
  6233. 'this.i = 0;'
  6234. ]),
  6235. LinesToStr([
  6236. '$mod.DoIt($mod.i,$mod.i,{',
  6237. ' p: $mod,',
  6238. ' get: function () {',
  6239. ' return this.p.i;',
  6240. ' },',
  6241. ' set: function (v) {',
  6242. ' this.p.i = v;',
  6243. ' }',
  6244. '});'
  6245. ]));
  6246. end;
  6247. procedure TTestModule.TestEnumRange_Array;
  6248. begin
  6249. StartProgram(false);
  6250. Add([
  6251. 'type',
  6252. ' TEnum = (Red, Green, Blue);',
  6253. ' TEnumRg = green..blue;',
  6254. ' TArr = array[TEnumRg] of byte;',
  6255. ' TArr2 = array[green..blue] of byte;',
  6256. 'var',
  6257. ' a: TArr;',
  6258. ' b: TArr = (3,4);',
  6259. ' c: TArr2 = (5,6);',
  6260. 'begin',
  6261. ' a[green] := b[blue];',
  6262. ' c[green] := c[blue];',
  6263. '']);
  6264. ConvertProgram;
  6265. CheckSource('TestEnumRange_Array',
  6266. LinesToStr([ // statements
  6267. 'this.TEnum = {',
  6268. ' "0": "Red",',
  6269. ' Red: 0,',
  6270. ' "1": "Green",',
  6271. ' Green: 1,',
  6272. ' "2": "Blue",',
  6273. ' Blue: 2',
  6274. '};',
  6275. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6276. 'this.b = [3, 4];',
  6277. 'this.c = [5, 6];',
  6278. '']),
  6279. LinesToStr([
  6280. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6281. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6282. '']));
  6283. end;
  6284. procedure TTestModule.TestEnum_ForIn;
  6285. begin
  6286. StartProgram(false);
  6287. Add([
  6288. 'type',
  6289. ' TEnum = (Red, Green, Blue);',
  6290. ' TEnumRg = green..blue;',
  6291. ' TArr = array[TEnum] of byte;',
  6292. ' TArrRg = array[TEnumRg] of byte;',
  6293. 'var',
  6294. ' e: TEnum;',
  6295. ' a1: TArr = (3,4,5);',
  6296. ' a2: TArrRg = (11,12);',
  6297. ' b: byte;',
  6298. 'begin',
  6299. ' for e in TEnum do ;',
  6300. ' for e in TEnumRg do ;',
  6301. ' for e in TArr do ;',
  6302. ' for e in TArrRg do ;',
  6303. ' for b in a1 do ;',
  6304. ' for b in a2 do ;',
  6305. '']);
  6306. ConvertProgram;
  6307. CheckSource('TestEnum_ForIn',
  6308. LinesToStr([ // statements
  6309. 'this.TEnum = {',
  6310. ' "0": "Red",',
  6311. ' Red: 0,',
  6312. ' "1": "Green",',
  6313. ' Green: 1,',
  6314. ' "2": "Blue",',
  6315. ' Blue: 2',
  6316. '};',
  6317. 'this.e = 0;',
  6318. 'this.a1 = [3, 4, 5];',
  6319. 'this.a2 = [11, 12];',
  6320. 'this.b = 0;',
  6321. '']),
  6322. LinesToStr([
  6323. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6324. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6325. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6326. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6327. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6328. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6329. '']));
  6330. end;
  6331. procedure TTestModule.TestEnum_ScopedNumber;
  6332. begin
  6333. Converter.Options:=Converter.Options+[coEnumNumbers];
  6334. StartProgram(false);
  6335. Add([
  6336. 'type',
  6337. ' TEnum = (Red, Green);',
  6338. 'var',
  6339. ' e: TEnum;',
  6340. 'begin',
  6341. ' e:=TEnum.Green;',
  6342. '']);
  6343. ConvertProgram;
  6344. CheckSource('TestEnum_ScopedNumber',
  6345. LinesToStr([ // statements
  6346. 'this.TEnum = {',
  6347. ' "0": "Red",',
  6348. ' Red: 0,',
  6349. ' "1": "Green",',
  6350. ' Green: 1',
  6351. '};',
  6352. 'this.e = 0;',
  6353. '']),
  6354. LinesToStr([
  6355. '$mod.e = 1;']));
  6356. end;
  6357. procedure TTestModule.TestEnum_InFunction;
  6358. begin
  6359. StartProgram(false);
  6360. Add([
  6361. 'const TEnum = 3;',
  6362. 'procedure DoIt;',
  6363. 'type',
  6364. ' TEnum = (Red, Green, Blue);',
  6365. ' procedure Sub;',
  6366. ' type',
  6367. ' TEnumSub = (Left, Right);',
  6368. ' var',
  6369. ' es: TEnumSub;',
  6370. ' begin',
  6371. ' es:=Left;',
  6372. ' end;',
  6373. 'var',
  6374. ' e, e2: TEnum;',
  6375. 'begin',
  6376. ' if e in [red,blue] then e2:=e;',
  6377. 'end;',
  6378. 'begin']);
  6379. ConvertProgram;
  6380. CheckSource('TestEnum_InFunction',
  6381. LinesToStr([ // statements
  6382. 'this.TEnum = 3;',
  6383. 'var TEnum$1 = {',
  6384. ' "0":"Red",',
  6385. ' Red:0,',
  6386. ' "1":"Green",',
  6387. ' Green:1,',
  6388. ' "2":"Blue",',
  6389. ' Blue:2',
  6390. ' };',
  6391. 'var TEnumSub = {',
  6392. ' "0": "Left",',
  6393. ' Left: 0,',
  6394. ' "1": "Right",',
  6395. ' Right: 1',
  6396. '};',
  6397. 'this.DoIt = function () {',
  6398. ' function Sub() {',
  6399. ' var es = 0;',
  6400. ' es = TEnumSub.Left;',
  6401. ' };',
  6402. ' var e = 0;',
  6403. ' var e2 = 0;',
  6404. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6405. '};',
  6406. '']),
  6407. LinesToStr([
  6408. '']));
  6409. end;
  6410. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6411. begin
  6412. StartUnit(true);
  6413. Add([
  6414. 'interface',
  6415. 'var color: (red, green);',
  6416. 'implementation',
  6417. 'initialization',
  6418. ' color:=green;',
  6419. '']);
  6420. ConvertUnit;
  6421. CheckSource('TestEnum_Name_Anonymous_Unit',
  6422. LinesToStr([
  6423. 'this.color$a = {',
  6424. ' "0": "red",',
  6425. ' red: 0,',
  6426. ' "1": "green",',
  6427. ' green: 1',
  6428. '};',
  6429. 'this.color = 0;',
  6430. '']),
  6431. LinesToStr([ // this.$init
  6432. '$mod.color = $mod.color$a.green;',
  6433. '']),
  6434. LinesToStr([ // implementation
  6435. '']) );
  6436. end;
  6437. procedure TTestModule.TestSet_Enum;
  6438. begin
  6439. StartProgram(false);
  6440. Add([
  6441. 'type',
  6442. ' TColor = (Red, Green, Blue);',
  6443. ' TColors = set of TColor;',
  6444. 'var',
  6445. ' c: TColor;',
  6446. ' s: TColors;',
  6447. ' t: TColors = [];',
  6448. ' u: TColors = [Red];',
  6449. 'begin',
  6450. ' s:=[];',
  6451. ' s:=[Green];',
  6452. ' s:=[Green,Blue];',
  6453. ' s:=[Red..Blue];',
  6454. ' s:=[Red,Green..Blue];',
  6455. ' s:=[Red,c];',
  6456. ' s:=t;',
  6457. ' s:=default(TColors);',
  6458. '']);
  6459. ConvertProgram;
  6460. CheckSource('TestSet',
  6461. LinesToStr([ // statements
  6462. 'this.TColor = {',
  6463. ' "0":"Red",',
  6464. ' Red:0,',
  6465. ' "1":"Green",',
  6466. ' Green:1,',
  6467. ' "2":"Blue",',
  6468. ' Blue:2',
  6469. ' };',
  6470. 'this.c = 0;',
  6471. 'this.s = {};',
  6472. 'this.t = {};',
  6473. 'this.u = rtl.createSet(this.TColor.Red);'
  6474. ]),
  6475. LinesToStr([
  6476. '$mod.s={};',
  6477. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6478. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6479. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6480. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6481. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6482. '$mod.s=rtl.refSet($mod.t);',
  6483. '$mod.s={};',
  6484. '']));
  6485. end;
  6486. procedure TTestModule.TestSet_Operators;
  6487. begin
  6488. StartProgram(false);
  6489. Add('type');
  6490. Add(' TColor = (Red, Green, Blue);');
  6491. Add(' TColors = set of tcolor;');
  6492. Add('var');
  6493. Add(' vC: TColor;');
  6494. Add(' vS: TColors;');
  6495. Add(' vT: TColors;');
  6496. Add(' vU: TColors;');
  6497. Add(' B: boolean;');
  6498. Add('begin');
  6499. Add(' include(vs,green);');
  6500. Add(' exclude(vs,vc);');
  6501. Add(' vs:=vt+vu;');
  6502. Add(' vs:=vt+[red];');
  6503. Add(' vs:=[red]+vt;');
  6504. Add(' vs:=[red]+[green];');
  6505. Add(' vs:=vt-vu;');
  6506. Add(' vs:=vt-[red];');
  6507. Add(' vs:=[red]-vt;');
  6508. Add(' vs:=[red]-[green];');
  6509. Add(' vs:=vt*vu;');
  6510. Add(' vs:=vt*[red];');
  6511. Add(' vs:=[red]*vt;');
  6512. Add(' vs:=[red]*[green];');
  6513. Add(' vs:=vt><vu;');
  6514. Add(' vs:=vt><[red];');
  6515. Add(' vs:=[red]><vt;');
  6516. Add(' vs:=[red]><[green];');
  6517. Add(' b:=vt=vu;');
  6518. Add(' b:=vt=[red];');
  6519. Add(' b:=[red]=vt;');
  6520. Add(' b:=[red]=[green];');
  6521. Add(' b:=vt<>vu;');
  6522. Add(' b:=vt<>[red];');
  6523. Add(' b:=[red]<>vt;');
  6524. Add(' b:=[red]<>[green];');
  6525. Add(' b:=vt<=vu;');
  6526. Add(' b:=vt<=[red];');
  6527. Add(' b:=[red]<=vt;');
  6528. Add(' b:=[red]<=[green];');
  6529. Add(' b:=vt>=vu;');
  6530. Add(' b:=vt>=[red];');
  6531. Add(' b:=[red]>=vt;');
  6532. Add(' b:=[red]>=[green];');
  6533. ConvertProgram;
  6534. CheckSource('TestSet_Operators',
  6535. LinesToStr([ // statements
  6536. 'this.TColor = {',
  6537. ' "0":"Red",',
  6538. ' Red:0,',
  6539. ' "1":"Green",',
  6540. ' Green:1,',
  6541. ' "2":"Blue",',
  6542. ' Blue:2',
  6543. ' };',
  6544. 'this.vC = 0;',
  6545. 'this.vS = {};',
  6546. 'this.vT = {};',
  6547. 'this.vU = {};',
  6548. 'this.B = false;'
  6549. ]),
  6550. LinesToStr([
  6551. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6552. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6553. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6554. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6555. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6556. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6557. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6558. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6559. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6560. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6561. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6562. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6563. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6564. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6565. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6566. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6567. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6568. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6569. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6570. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6571. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6572. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6573. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6574. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6575. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6576. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6577. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6578. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6579. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6580. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6581. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6582. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6583. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6584. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6585. '']));
  6586. end;
  6587. procedure TTestModule.TestSet_Operator_In;
  6588. begin
  6589. StartProgram(false);
  6590. Add([
  6591. 'type',
  6592. ' TColor = (Red, Green, Blue);',
  6593. ' TColors = set of tcolor;',
  6594. ' TColorRg = green..blue;',
  6595. 'var',
  6596. ' vC: tcolor;',
  6597. ' vT: tcolors;',
  6598. ' B: boolean;',
  6599. ' rg: TColorRg;',
  6600. 'begin',
  6601. ' b:=red in vt;',
  6602. ' b:=vc in vt;',
  6603. ' b:=green in [red..blue];',
  6604. ' b:=vc in [red..blue];',
  6605. ' ',
  6606. ' if red in vt then ;',
  6607. ' while vC in vt do ;',
  6608. ' repeat',
  6609. ' until vC in vt;',
  6610. ' if rg in [green..blue] then ;',
  6611. '']);
  6612. ConvertProgram;
  6613. CheckSource('TestSet_Operator_In',
  6614. LinesToStr([ // statements
  6615. 'this.TColor = {',
  6616. ' "0":"Red",',
  6617. ' Red:0,',
  6618. ' "1":"Green",',
  6619. ' Green:1,',
  6620. ' "2":"Blue",',
  6621. ' Blue:2',
  6622. ' };',
  6623. 'this.vC = 0;',
  6624. 'this.vT = {};',
  6625. 'this.B = false;',
  6626. 'this.rg = this.TColor.Green;',
  6627. '']),
  6628. LinesToStr([
  6629. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6630. '$mod.B = $mod.vC in $mod.vT;',
  6631. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6632. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6633. 'if ($mod.TColor.Red in $mod.vT) ;',
  6634. 'while ($mod.vC in $mod.vT) {',
  6635. '};',
  6636. 'do {',
  6637. '} while (!($mod.vC in $mod.vT));',
  6638. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6639. '']));
  6640. end;
  6641. procedure TTestModule.TestSet_Functions;
  6642. begin
  6643. StartProgram(false);
  6644. Add('type');
  6645. Add(' TMyEnum = (Red, Green);');
  6646. Add(' TMyEnums = set of TMyEnum;');
  6647. Add('var');
  6648. Add(' e: TMyEnum;');
  6649. Add(' s: TMyEnums;');
  6650. Add('begin');
  6651. Add(' e:=Low(TMyEnums);');
  6652. Add(' e:=Low(s);');
  6653. Add(' e:=High(TMyEnums);');
  6654. Add(' e:=High(s);');
  6655. ConvertProgram;
  6656. CheckSource('TestSetFunctions',
  6657. LinesToStr([ // statements
  6658. 'this.TMyEnum = {',
  6659. ' "0":"Red",',
  6660. ' Red:0,',
  6661. ' "1":"Green",',
  6662. ' Green:1',
  6663. ' };',
  6664. 'this.e = 0;',
  6665. 'this.s = {};'
  6666. ]),
  6667. LinesToStr([
  6668. '$mod.e=$mod.TMyEnum.Red;',
  6669. '$mod.e=$mod.TMyEnum.Red;',
  6670. '$mod.e=$mod.TMyEnum.Green;',
  6671. '$mod.e=$mod.TMyEnum.Green;',
  6672. '']));
  6673. end;
  6674. procedure TTestModule.TestSet_PassAsArgClone;
  6675. begin
  6676. StartProgram(false);
  6677. Add('type');
  6678. Add(' TMyEnum = (Red, Green);');
  6679. Add(' TMyEnums = set of TMyEnum;');
  6680. Add('procedure DoDefault(s: tmyenums); begin end;');
  6681. Add('procedure DoConst(const s: tmyenums); begin end;');
  6682. Add('var');
  6683. Add(' aSet: tmyenums;');
  6684. Add('begin');
  6685. Add(' dodefault(aset);');
  6686. Add(' doconst(aset);');
  6687. ConvertProgram;
  6688. CheckSource('TestSetFunctions',
  6689. LinesToStr([ // statements
  6690. 'this.TMyEnum = {',
  6691. ' "0":"Red",',
  6692. ' Red:0,',
  6693. ' "1":"Green",',
  6694. ' Green:1',
  6695. ' };',
  6696. 'this.DoDefault = function (s) {',
  6697. '};',
  6698. 'this.DoConst = function (s) {',
  6699. '};',
  6700. 'this.aSet = {};'
  6701. ]),
  6702. LinesToStr([
  6703. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6704. '$mod.DoConst($mod.aSet);',
  6705. '']));
  6706. end;
  6707. procedure TTestModule.TestSet_AsParams;
  6708. begin
  6709. StartProgram(false);
  6710. Add([
  6711. 'type TEnum = (Red,Blue);',
  6712. 'type TEnums = set of TEnum;',
  6713. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6714. 'var vJ: TEnums;',
  6715. 'begin',
  6716. ' Include(vg,red);',
  6717. ' Include(result,blue);',
  6718. ' vg:=vg;',
  6719. ' vj:=vh;',
  6720. ' vi:=vi;',
  6721. ' doit(vg,vg,vg);',
  6722. ' doit(vh,vh,vj);',
  6723. ' doit(vi,vi,vi);',
  6724. ' doit(vj,vj,vj);',
  6725. 'end;',
  6726. 'var i: TEnums;',
  6727. 'begin',
  6728. ' doit(i,i,i);']);
  6729. ConvertProgram;
  6730. CheckSource('TestSet_AsParams',
  6731. LinesToStr([ // statements
  6732. 'this.TEnum = {',
  6733. ' "0": "Red",',
  6734. ' Red: 0,',
  6735. ' "1": "Blue",',
  6736. ' Blue: 1',
  6737. '};',
  6738. 'this.DoIt = function (vG,vH,vI) {',
  6739. ' var Result = {};',
  6740. ' var vJ = {};',
  6741. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6742. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6743. ' vG = rtl.refSet(vG);',
  6744. ' vJ = rtl.refSet(vH);',
  6745. ' vI.set(rtl.refSet(vI.get()));',
  6746. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6747. ' get: function () {',
  6748. ' return vG;',
  6749. ' },',
  6750. ' set: function (v) {',
  6751. ' vG = v;',
  6752. ' }',
  6753. ' });',
  6754. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6755. ' get: function () {',
  6756. ' return vJ;',
  6757. ' },',
  6758. ' set: function (v) {',
  6759. ' vJ = v;',
  6760. ' }',
  6761. ' });',
  6762. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6763. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6764. ' get: function () {',
  6765. ' return vJ;',
  6766. ' },',
  6767. ' set: function (v) {',
  6768. ' vJ = v;',
  6769. ' }',
  6770. ' });',
  6771. ' return Result;',
  6772. '};',
  6773. 'this.i = {};'
  6774. ]),
  6775. LinesToStr([
  6776. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6777. ' p: $mod,',
  6778. ' get: function () {',
  6779. ' return this.p.i;',
  6780. ' },',
  6781. ' set: function (v) {',
  6782. ' this.p.i = v;',
  6783. ' }',
  6784. '});'
  6785. ]));
  6786. end;
  6787. procedure TTestModule.TestSet_Property;
  6788. begin
  6789. StartProgram(false);
  6790. Add('type');
  6791. Add(' TEnum = (Red,Blue);');
  6792. Add(' TEnums = set of TEnum;');
  6793. Add(' TObject = class');
  6794. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6795. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6796. Add(' property Colors: TEnums read GetColors write SetColors;');
  6797. Add(' end;');
  6798. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6799. Add('begin end;');
  6800. Add('var Obj: TObject;');
  6801. Add('begin');
  6802. Add(' Include(Obj.Colors,Red);');
  6803. Add(' Exclude(Obj.Colors,Red);');
  6804. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6805. ConvertProgram;
  6806. CheckSource('TestSet_Property',
  6807. LinesToStr([ // statements
  6808. 'this.TEnum = {',
  6809. ' "0": "Red",',
  6810. ' Red: 0,',
  6811. ' "1": "Blue",',
  6812. ' Blue: 1',
  6813. '};',
  6814. 'rtl.createClass(this, "TObject", null, function () {',
  6815. ' this.$init = function () {',
  6816. ' };',
  6817. ' this.$final = function () {',
  6818. ' };',
  6819. '});',
  6820. 'this.DoIt = function (i, j, k, l) {',
  6821. '};',
  6822. 'this.Obj = null;',
  6823. '']),
  6824. LinesToStr([
  6825. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6826. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6827. '']));
  6828. end;
  6829. procedure TTestModule.TestSet_EnumConst;
  6830. begin
  6831. StartProgram(false);
  6832. Add([
  6833. 'type',
  6834. ' TEnum = (Red,Blue);',
  6835. ' TEnums = set of TEnum;',
  6836. 'const',
  6837. ' Orange = red;',
  6838. 'var',
  6839. ' Enum: tenum;',
  6840. ' Enums: tenums;',
  6841. 'begin',
  6842. ' Include(enums,orange);',
  6843. ' Exclude(enums,orange);',
  6844. ' if orange in enums then;',
  6845. ' if orange in [orange,red] then;']);
  6846. ConvertProgram;
  6847. CheckSource('TestSet_EnumConst',
  6848. LinesToStr([ // statements
  6849. 'this.TEnum = {',
  6850. ' "0": "Red",',
  6851. ' Red: 0,',
  6852. ' "1": "Blue",',
  6853. ' Blue: 1',
  6854. '};',
  6855. 'this.Orange = this.TEnum.Red;',
  6856. 'this.Enum = 0;',
  6857. 'this.Enums = {};',
  6858. '']),
  6859. LinesToStr([
  6860. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6861. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6862. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6863. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6864. '']));
  6865. end;
  6866. procedure TTestModule.TestSet_IntConst;
  6867. begin
  6868. StartProgram(false);
  6869. Add([
  6870. 'type',
  6871. ' TEnums = set of Byte;',
  6872. 'const',
  6873. ' Orange = 0;',
  6874. 'var',
  6875. ' Enum: byte;',
  6876. ' Enums: tenums;',
  6877. 'begin',
  6878. ' Enums:=[];',
  6879. ' Enums:=[0];',
  6880. ' Enums:=[1..2];',
  6881. //' Include(enums,orange);',
  6882. //' Exclude(enums,orange);',
  6883. ' if orange in enums then;',
  6884. ' if orange in [orange,1] then;']);
  6885. ConvertProgram;
  6886. CheckSource('TestSet_IntConst',
  6887. LinesToStr([ // statements
  6888. 'this.Orange = 0;',
  6889. 'this.Enum = 0;',
  6890. 'this.Enums = {};',
  6891. '']),
  6892. LinesToStr([
  6893. '$mod.Enums = {};',
  6894. '$mod.Enums = rtl.createSet(0);',
  6895. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6896. 'if (0 in $mod.Enums) ;',
  6897. 'if (0 in rtl.createSet(0, 1)) ;',
  6898. '']));
  6899. end;
  6900. procedure TTestModule.TestSet_IntRange;
  6901. begin
  6902. StartProgram(false);
  6903. Add([
  6904. 'type',
  6905. ' TRange = 1..3;',
  6906. ' TEnums = set of TRange;',
  6907. 'const',
  6908. ' Orange = 2;',
  6909. 'var',
  6910. ' Enum: byte;',
  6911. ' Enums: TEnums;',
  6912. 'begin',
  6913. ' Enums:=[];',
  6914. ' Enums:=[1];',
  6915. ' Enums:=[2..3];',
  6916. ' Include(enums,orange);',
  6917. ' Exclude(enums,orange);',
  6918. ' if orange in enums then;',
  6919. ' if orange in [orange,1] then;']);
  6920. ConvertProgram;
  6921. CheckSource('TestSet_IntRange',
  6922. LinesToStr([ // statements
  6923. 'this.Orange = 2;',
  6924. 'this.Enum = 0;',
  6925. 'this.Enums = {};',
  6926. '']),
  6927. LinesToStr([
  6928. '$mod.Enums = {};',
  6929. '$mod.Enums = rtl.createSet(1);',
  6930. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6931. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6932. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6933. 'if (2 in $mod.Enums) ;',
  6934. 'if (2 in rtl.createSet(2, 1)) ;',
  6935. '']));
  6936. end;
  6937. procedure TTestModule.TestSet_AnonymousEnumType;
  6938. begin
  6939. StartProgram(false);
  6940. Add('type');
  6941. Add(' TFlags = set of (red, green);');
  6942. Add('const');
  6943. Add(' favorite = red;');
  6944. Add('var');
  6945. Add(' f: TFlags;');
  6946. Add(' i: longint;');
  6947. Add('begin');
  6948. Add(' Include(f,red);');
  6949. Add(' Include(f,favorite);');
  6950. Add(' i:=ord(red);');
  6951. Add(' i:=ord(favorite);');
  6952. Add(' i:=ord(low(TFlags));');
  6953. Add(' i:=ord(low(f));');
  6954. Add(' i:=ord(low(favorite));');
  6955. Add(' i:=ord(high(TFlags));');
  6956. Add(' i:=ord(high(f));');
  6957. Add(' i:=ord(high(favorite));');
  6958. Add(' f:=[green,favorite];');
  6959. ConvertProgram;
  6960. CheckSource('TestSet_AnonymousEnumType',
  6961. LinesToStr([ // statements
  6962. 'this.TFlags$a = {',
  6963. ' "0": "red",',
  6964. ' red: 0,',
  6965. ' "1": "green",',
  6966. ' green: 1',
  6967. '};',
  6968. 'this.favorite = this.TFlags$a.red;',
  6969. 'this.f = {};',
  6970. 'this.i = 0;',
  6971. '']),
  6972. LinesToStr([
  6973. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6974. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6975. '$mod.i = $mod.TFlags$a.red;',
  6976. '$mod.i = $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.green;',
  6981. '$mod.i = $mod.TFlags$a.green;',
  6982. '$mod.i = $mod.TFlags$a.green;',
  6983. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6984. '']));
  6985. end;
  6986. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6987. begin
  6988. exit;
  6989. StartProgram(false);
  6990. Add([
  6991. 'type',
  6992. ' TAtoZ = ''A''..''Z'';',
  6993. ' TSetOfAZ = set of TAtoZ;',
  6994. 'var',
  6995. ' c: char;',
  6996. ' a: TAtoZ;',
  6997. ' s: TSetOfAZ = [''P'',''A''];',
  6998. ' i: longint;',
  6999. 'begin',
  7000. ' Include(s,''S'');',
  7001. ' Include(s,c);',
  7002. ' Include(s,a);',
  7003. ' c:=low(TAtoZ);',
  7004. ' i:=ord(low(TAtoZ));',
  7005. ' a:=high(TAtoZ);',
  7006. ' a:=high(TSetOfAtoZ);',
  7007. ' s:=[a,c,''M''];',
  7008. '']);
  7009. ConvertProgram;
  7010. CheckSource('TestSet_AnonymousEnumTypeChar',
  7011. LinesToStr([ // statements
  7012. '']),
  7013. LinesToStr([
  7014. '']));
  7015. end;
  7016. procedure TTestModule.TestSet_ConstEnum;
  7017. begin
  7018. StartProgram(false);
  7019. Add([
  7020. 'type',
  7021. ' TEnum = (red,blue,green);',
  7022. ' TEnums = set of TEnum;',
  7023. 'const',
  7024. ' teAny = [low(TEnum)..high(TEnum)];',
  7025. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7026. 'var',
  7027. ' e: TEnum;',
  7028. ' s: TEnums;',
  7029. 'begin',
  7030. ' if blue in teAny then;',
  7031. ' if blue in teAny+[e] then;',
  7032. ' if blue in teAny+teRedBlue then;',
  7033. ' if e in [red,blue] then;',
  7034. ' s:=teAny;',
  7035. ' s:=teAny+[e];',
  7036. ' s:=[e]+teAny;',
  7037. ' s:=teAny+teRedBlue;',
  7038. ' s:=teAny+teRedBlue+[e];',
  7039. '']);
  7040. ConvertProgram;
  7041. CheckSource('TestSet_ConstEnum',
  7042. LinesToStr([ // statements
  7043. 'this.TEnum = {',
  7044. ' "0": "red",',
  7045. ' red: 0,',
  7046. ' "1": "blue",',
  7047. ' blue: 1,',
  7048. ' "2": "green",',
  7049. ' green: 2',
  7050. '};',
  7051. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7052. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7053. 'this.e = 0;',
  7054. 'this.s = {};',
  7055. '']),
  7056. LinesToStr([
  7057. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7058. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7059. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7060. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7061. '$mod.s = rtl.refSet($mod.teAny);',
  7062. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7063. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7064. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7065. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7066. '']));
  7067. end;
  7068. procedure TTestModule.TestSet_ConstChar;
  7069. begin
  7070. StartProgram(false);
  7071. Add([
  7072. 'const',
  7073. ' LowChars = [''a''..''z''];',
  7074. ' Chars = LowChars+[''A''..''Z''];',
  7075. ' sc = [''А'', ''Я''];',
  7076. 'var',
  7077. ' c: char;',
  7078. ' s: string;',
  7079. 'begin',
  7080. ' if c in lowchars then ;',
  7081. ' if ''a'' in lowchars then ;',
  7082. ' if s[1] in lowchars then ;',
  7083. ' if c in chars then ;',
  7084. ' if c in [''a''..''z'',''_''] then ;',
  7085. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7086. ' if ''Я'' in sc then ;',
  7087. ' if 3=ord('' '') then ;',
  7088. '']);
  7089. ConvertProgram;
  7090. CheckSource('TestSet_ConstChar',
  7091. LinesToStr([ // statements
  7092. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7093. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7094. 'this.sc = rtl.createSet(1040, 1071);',
  7095. 'this.c = "";',
  7096. 'this.s = "";',
  7097. '']),
  7098. LinesToStr([
  7099. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7100. 'if (97 in $mod.LowChars) ;',
  7101. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7102. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7103. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7104. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7105. 'if (1071 in $mod.sc) ;',
  7106. 'if (3 === 32) ;',
  7107. '']));
  7108. end;
  7109. procedure TTestModule.TestSet_ConstInt;
  7110. begin
  7111. StartProgram(false);
  7112. Add([
  7113. 'const',
  7114. ' Months = [1..12];',
  7115. ' Mirror = [-12..-1]+Months;',
  7116. 'var',
  7117. ' i: smallint;',
  7118. 'begin',
  7119. ' if 3 in Months then;',
  7120. ' if i in Months+[i] then;',
  7121. ' if i in Months+Mirror then;',
  7122. ' if i in [4..6,8] then;',
  7123. '']);
  7124. ConvertProgram;
  7125. CheckSource('TestSet_ConstInt',
  7126. LinesToStr([ // statements
  7127. 'this.Months = rtl.createSet(null, 1, 12);',
  7128. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7129. 'this.i = 0;',
  7130. '']),
  7131. LinesToStr([
  7132. 'if (3 in $mod.Months) ;',
  7133. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7134. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7135. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7136. '']));
  7137. end;
  7138. procedure TTestModule.TestSet_InFunction;
  7139. begin
  7140. StartProgram(false);
  7141. Add([
  7142. 'const',
  7143. ' TEnum = 3;',
  7144. ' TSetOfEnum = 4;',
  7145. ' TSetOfAno = 5;',
  7146. 'procedure DoIt;',
  7147. 'type',
  7148. ' TEnum = (red, blue);',
  7149. ' TSetOfEnum = set of TEnum;',
  7150. ' TSetOfAno = set of (up,down);',
  7151. 'var',
  7152. ' e: TEnum;',
  7153. ' se: TSetOfEnum;',
  7154. ' sa: TSetOfAno;',
  7155. 'begin',
  7156. ' se:=[e];',
  7157. ' sa:=[up];',
  7158. 'end;',
  7159. 'begin',
  7160. '']);
  7161. ConvertProgram;
  7162. CheckSource('TestSet_InFunction',
  7163. LinesToStr([ // statements
  7164. 'this.TEnum = 3;',
  7165. 'this.TSetOfEnum = 4;',
  7166. 'this.TSetOfAno = 5;',
  7167. 'var TEnum$1 = {',
  7168. ' "0": "red",',
  7169. ' red: 0,',
  7170. ' "1": "blue",',
  7171. ' blue: 1',
  7172. '};',
  7173. 'var TSetOfAno$a = {',
  7174. ' "0": "up",',
  7175. ' up: 0,',
  7176. ' "1": "down",',
  7177. ' down: 1',
  7178. '};',
  7179. 'this.DoIt = function () {',
  7180. ' var e = 0;',
  7181. ' var se = {};',
  7182. ' var sa = {};',
  7183. ' se = rtl.createSet(e);',
  7184. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7185. '};',
  7186. '']),
  7187. LinesToStr([
  7188. '']));
  7189. end;
  7190. procedure TTestModule.TestSet_ForIn;
  7191. begin
  7192. StartProgram(false);
  7193. Add([
  7194. 'type',
  7195. ' TEnum = (Red, Green, Blue);',
  7196. ' TEnumRg = green..blue;',
  7197. ' TSetOfEnum = set of TEnum;',
  7198. ' TSetOfEnumRg = set of TEnumRg;',
  7199. 'var',
  7200. ' e, e2: TEnum;',
  7201. ' er: TEnum;',
  7202. ' s: TSetOfEnum;',
  7203. 'begin',
  7204. ' for e in TSetOfEnum do ;',
  7205. ' for e in TSetOfEnumRg do ;',
  7206. ' for e in [] do e2:=e;',
  7207. ' for e in [red..green] do e2:=e;',
  7208. ' for e in [green,blue] do e2:=e;',
  7209. ' for e in [red,blue] do e2:=e;',
  7210. ' for e in s do e2:=e;',
  7211. ' for er in TSetOfEnumRg do ;',
  7212. '']);
  7213. ConvertProgram;
  7214. CheckSource('TestSet_ForIn',
  7215. LinesToStr([ // statements
  7216. 'this.TEnum = {',
  7217. ' "0":"Red",',
  7218. ' Red:0,',
  7219. ' "1":"Green",',
  7220. ' Green:1,',
  7221. ' "2":"Blue",',
  7222. ' Blue:2',
  7223. ' };',
  7224. 'this.e = 0;',
  7225. 'this.e2 = 0;',
  7226. 'this.er = 0;',
  7227. 'this.s = {};',
  7228. '']),
  7229. LinesToStr([
  7230. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7231. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7232. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7233. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7234. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7235. 'for (var $l in $mod.s){',
  7236. ' $mod.e = +$l;',
  7237. ' $mod.e2 = $mod.e;',
  7238. '};',
  7239. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7240. '']));
  7241. end;
  7242. procedure TTestModule.TestNestBegin;
  7243. begin
  7244. StartProgram(false);
  7245. Add('begin');
  7246. Add(' begin');
  7247. Add(' begin');
  7248. Add(' end;');
  7249. Add(' begin');
  7250. Add(' if true then ;');
  7251. Add(' end;');
  7252. Add(' end;');
  7253. ConvertProgram;
  7254. CheckSource('TestNestBegin',
  7255. '',
  7256. 'if (true) ;');
  7257. end;
  7258. procedure TTestModule.TestUnitImplVars;
  7259. begin
  7260. StartUnit(false);
  7261. Add('interface');
  7262. Add('implementation');
  7263. Add('var');
  7264. Add(' V1:longint;');
  7265. Add(' V2:longint = 3;');
  7266. Add(' V3:string = ''abc'';');
  7267. ConvertUnit;
  7268. CheckSource('TestUnitImplVars',
  7269. LinesToStr([ // statements
  7270. 'var $impl = $mod.$impl;',
  7271. '']),
  7272. '', // this.$init
  7273. LinesToStr([ // implementation
  7274. '$impl.V1 = 0;',
  7275. '$impl.V2 = 3;',
  7276. '$impl.V3 = "abc";',
  7277. '']) );
  7278. end;
  7279. procedure TTestModule.TestUnitImplConsts;
  7280. begin
  7281. StartUnit(false);
  7282. Add('interface');
  7283. Add('implementation');
  7284. Add('const');
  7285. Add(' v1 = 3;');
  7286. Add(' v2:longint = 4;');
  7287. Add(' v3:string = ''abc'';');
  7288. ConvertUnit;
  7289. CheckSource('TestUnitImplConsts',
  7290. LinesToStr([ // statements
  7291. 'var $impl = $mod.$impl;',
  7292. '']),
  7293. '', // this.$init
  7294. LinesToStr([ // implementation
  7295. '$impl.v1 = 3;',
  7296. '$impl.v2 = 4;',
  7297. '$impl.v3 = "abc";',
  7298. '']) );
  7299. end;
  7300. procedure TTestModule.TestUnitImplRecord;
  7301. begin
  7302. StartUnit(false);
  7303. Add('interface');
  7304. Add('implementation');
  7305. Add('type');
  7306. Add(' TMyRecord = record');
  7307. Add(' i: longint;');
  7308. Add(' end;');
  7309. Add('var aRec: TMyRecord;');
  7310. Add('initialization');
  7311. Add(' arec.i:=3;');
  7312. ConvertUnit;
  7313. CheckSource('TestUnitImplRecord',
  7314. LinesToStr([ // statements
  7315. 'var $impl = $mod.$impl;',
  7316. '']),
  7317. // this.$init
  7318. '$impl.aRec.i = 3;',
  7319. LinesToStr([ // implementation
  7320. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7321. ' this.i = 0;',
  7322. ' this.$eq = function (b) {',
  7323. ' return this.i === b.i;',
  7324. ' };',
  7325. ' this.$assign = function (s) {',
  7326. ' this.i = s.i;',
  7327. ' return this;',
  7328. ' };',
  7329. '});',
  7330. '$impl.aRec = $impl.TMyRecord.$new();',
  7331. '']) );
  7332. end;
  7333. procedure TTestModule.TestRenameJSNameConflict;
  7334. begin
  7335. StartProgram(false);
  7336. Add('var apply: longint;');
  7337. Add('var bind: longint;');
  7338. Add('var call: longint;');
  7339. Add('begin');
  7340. ConvertProgram;
  7341. CheckSource('TestRenameJSNameConflict',
  7342. LinesToStr([ // statements
  7343. 'this.Apply = 0;',
  7344. 'this.Bind = 0;',
  7345. 'this.Call = 0;'
  7346. ]),
  7347. LinesToStr([ // this.$main
  7348. ''
  7349. ]));
  7350. end;
  7351. procedure TTestModule.TestLocalConst;
  7352. begin
  7353. StartProgram(false);
  7354. Add('procedure DoIt;');
  7355. Add('const');
  7356. Add(' cA: longint = 1;');
  7357. Add(' cB = 2;');
  7358. Add(' procedure Sub;');
  7359. Add(' const');
  7360. Add(' csA = 3;');
  7361. Add(' cB: double = 4;');
  7362. Add(' begin');
  7363. Add(' cb:=cb+csa;');
  7364. Add(' ca:=ca+csa+5;');
  7365. Add(' end;');
  7366. Add('begin');
  7367. Add(' ca:=ca+cb+6;');
  7368. Add('end;');
  7369. Add('begin');
  7370. ConvertProgram;
  7371. CheckSource('TestLocalConst',
  7372. LinesToStr([
  7373. 'var cA = 1;',
  7374. 'var cB = 2;',
  7375. 'var csA = 3;',
  7376. 'var cB$1 = 4;',
  7377. 'this.DoIt = function () {',
  7378. ' function Sub() {',
  7379. ' cB$1 = cB$1 + 3;',
  7380. ' cA = cA + 3 + 5;',
  7381. ' };',
  7382. ' cA = cA + 2 + 6;',
  7383. '};'
  7384. ]),
  7385. LinesToStr([
  7386. ]));
  7387. end;
  7388. procedure TTestModule.TestVarExternal;
  7389. begin
  7390. StartProgram(false);
  7391. Add('var');
  7392. Add(' NaN: double; external name ''Global.NaN'';');
  7393. Add(' d: double;');
  7394. Add('begin');
  7395. Add(' d:=NaN;');
  7396. ConvertProgram;
  7397. CheckSource('TestVarExternal',
  7398. LinesToStr([
  7399. 'this.d = 0.0;'
  7400. ]),
  7401. LinesToStr([
  7402. '$mod.d = Global.NaN;'
  7403. ]));
  7404. end;
  7405. procedure TTestModule.TestVarExternalOtherUnit;
  7406. begin
  7407. AddModuleWithIntfImplSrc('unit2.pas',
  7408. LinesToStr([
  7409. 'var NaN: double; external name ''Global.NaN'';',
  7410. 'var iV: longint;'
  7411. ]),
  7412. '');
  7413. StartUnit(true);
  7414. Add('interface');
  7415. Add('uses unit2;');
  7416. Add('implementation');
  7417. Add('var');
  7418. Add(' d: double;');
  7419. Add(' i: longint; external name ''$i'';');
  7420. Add('begin');
  7421. Add(' d:=nan;');
  7422. Add(' d:=uNit2.nan;');
  7423. Add(' d:=test1.d;');
  7424. Add(' i:=iv;');
  7425. Add(' i:=uNit2.iv;');
  7426. Add(' i:=test1.i;');
  7427. ConvertUnit;
  7428. CheckSource('TestVarExternalOtherUnit',
  7429. LinesToStr([
  7430. 'var $impl = $mod.$impl;',
  7431. '']),
  7432. LinesToStr([ // this.$init
  7433. '$impl.d = Global.NaN;',
  7434. '$impl.d = Global.NaN;',
  7435. '$impl.d = $impl.d;',
  7436. '$i = pas.unit2.iV;',
  7437. '$i = pas.unit2.iV;',
  7438. '$i = $i;',
  7439. '']),
  7440. LinesToStr([ // implementation
  7441. '$impl.d = 0.0;',
  7442. '']) );
  7443. end;
  7444. procedure TTestModule.TestVarAbsoluteFail;
  7445. begin
  7446. StartProgram(false);
  7447. Add([
  7448. 'var',
  7449. ' a: longint;',
  7450. ' b: longword absolute a;',
  7451. 'begin']);
  7452. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7453. ConvertProgram;
  7454. end;
  7455. procedure TTestModule.TestConstExternal;
  7456. begin
  7457. StartProgram(false);
  7458. Add([
  7459. 'const',
  7460. ' PI: double; external name ''Global.PI'';',
  7461. ' Tau = 2*pi;',
  7462. 'var d: double;',
  7463. 'begin',
  7464. ' d:=pi;',
  7465. ' d:=tau+pi;']);
  7466. ConvertProgram;
  7467. CheckSource('TestConstExternal',
  7468. LinesToStr([
  7469. 'this.Tau = 2*Global.PI;',
  7470. 'this.d = 0.0;'
  7471. ]),
  7472. LinesToStr([
  7473. '$mod.d = Global.PI;',
  7474. '$mod.d = $mod.Tau + Global.PI;'
  7475. ]));
  7476. end;
  7477. procedure TTestModule.TestDouble;
  7478. begin
  7479. StartProgram(false);
  7480. Add([
  7481. 'type',
  7482. ' TDateTime = double;',
  7483. 'const',
  7484. ' a = TDateTime(2.7);',
  7485. ' b = a + TDateTime(1.7);',
  7486. ' c = 0.9 + 0.1;',
  7487. ' f0_1 = 0.1;',
  7488. ' f0_3 = 0.3;',
  7489. ' fn0_1 = -0.1;',
  7490. ' fn0_3 = -0.3;',
  7491. ' fn0_003 = -0.003;',
  7492. ' fn0_123456789 = -0.123456789;',
  7493. ' fn300_0 = -300.0;',
  7494. ' fn123456_0 = -123456.0;',
  7495. ' fn1234567_8 = -1234567.8;',
  7496. ' fn12345678_9 = -12345678.9;',
  7497. ' f1_0En12 = 1E-12;',
  7498. ' fn1_0En12 = -1E-12;',
  7499. ' maxdouble = 1.7e+308;',
  7500. ' mindouble = -1.7e+308;',
  7501. ' MinSafeIntDouble = -$1fffffffffffff;',
  7502. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7503. ' MaxSafeIntDouble = $1fffffffffffff;',
  7504. ' DZeroResolution = 1E-12;',
  7505. ' Minus1 = -1E-12;',
  7506. ' EPS = 1E-9;',
  7507. ' DELTA = 0.001;',
  7508. ' Big = 129.789E+100;',
  7509. ' Test0_15 = 0.15;',
  7510. ' Test999 = 2.9999999999999;',
  7511. ' Test111999 = 211199999999999000.0;',
  7512. ' TestMinus111999 = -211199999999999000.0;',
  7513. ' Inf = 1.0 / 0.0;',
  7514. ' NegInf = -1.0 / 0.0;',
  7515. 'procedure Run(d: double); external name ''Run'';',
  7516. 'var',
  7517. ' d: double = b;',
  7518. 'begin',
  7519. ' d:=1.0;',
  7520. ' d:=1.0/3.0;',
  7521. ' d:=1.0/(3-2-1);',
  7522. ' d:=1/3;',
  7523. ' d:=5.0E-324;',
  7524. ' d:=1.7E308;',
  7525. ' d:=001.00E00;',
  7526. ' d:=002.00E001;',
  7527. ' d:=003.000E000;',
  7528. ' d:=-004.00E-00;',
  7529. ' d:=-005.00E-001;',
  7530. ' d:=10**3;',
  7531. ' d:=10 mod 3;',
  7532. ' d:=10 div 3;',
  7533. ' d:=c;',
  7534. ' d:=f0_1;',
  7535. ' d:=f0_3;',
  7536. ' d:=fn0_1;',
  7537. ' d:=fn0_3;',
  7538. ' d:=fn0_003;',
  7539. ' d:=fn0_123456789;',
  7540. ' d:=fn300_0;',
  7541. ' d:=fn123456_0;',
  7542. ' d:=fn1234567_8;',
  7543. ' d:=fn12345678_9;',
  7544. ' d:=f1_0En12;',
  7545. ' d:=fn1_0En12;',
  7546. ' d:=maxdouble;',
  7547. ' d:=mindouble;',
  7548. ' d:=MinSafeIntDouble;',
  7549. ' d:=double(MinSafeIntDouble);',
  7550. ' d:=MinSafeIntDouble2;',
  7551. ' d:=double(MinSafeIntDouble2);',
  7552. ' d:=MaxSafeIntDouble;',
  7553. ' d:=default(double);',
  7554. ' Run(Inf);',
  7555. ' Run(NegInf);',
  7556. '']);
  7557. ConvertProgram;
  7558. CheckSource('TestDouble',
  7559. LinesToStr([
  7560. 'this.a = 2.7;',
  7561. 'this.b = 2.7 + 1.7;',
  7562. 'this.c = 0.9 + 0.1;',
  7563. 'this.f0_1 = 0.1;',
  7564. 'this.f0_3 = 0.3;',
  7565. 'this.fn0_1 = -0.1;',
  7566. 'this.fn0_3 = -0.3;',
  7567. 'this.fn0_003 = -0.003;',
  7568. 'this.fn0_123456789 = -0.123456789;',
  7569. 'this.fn300_0 = -300.0;',
  7570. 'this.fn123456_0 = -123456.0;',
  7571. 'this.fn1234567_8 = -1234567.8;',
  7572. 'this.fn12345678_9 = -12345678.9;',
  7573. 'this.f1_0En12 = 1E-12;',
  7574. 'this.fn1_0En12 = -1E-12;',
  7575. 'this.maxdouble = 1.7e+308;',
  7576. 'this.mindouble = -1.7e+308;',
  7577. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7578. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7579. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7580. 'this.DZeroResolution = 1E-12;',
  7581. 'this.Minus1 = -1E-12;',
  7582. 'this.EPS = 1E-9;',
  7583. 'this.DELTA = 0.001;',
  7584. 'this.Big = 129.789E+100;',
  7585. 'this.Test0_15 = 0.15;',
  7586. 'this.Test999 = 2.9999999999999;',
  7587. 'this.Test111999 = 211199999999999000.0;',
  7588. 'this.TestMinus111999 = -211199999999999000.0;',
  7589. 'this.Inf = 1.0 / 0.0;',
  7590. 'this.NegInf = -1.0 / 0.0;',
  7591. 'this.d = 4.4;',
  7592. '']),
  7593. LinesToStr([
  7594. '$mod.d = 1.0;',
  7595. '$mod.d = 1.0 / 3.0;',
  7596. '$mod.d = 1.0 / (3 - 2 - 1);',
  7597. '$mod.d = 1 / 3;',
  7598. '$mod.d = 5.0E-324;',
  7599. '$mod.d = 1.7E308;',
  7600. '$mod.d = 1.00E0;',
  7601. '$mod.d = 2.00E1;',
  7602. '$mod.d = 3.000E0;',
  7603. '$mod.d = -4.00E-0;',
  7604. '$mod.d = -5.00E-1;',
  7605. '$mod.d = Math.pow(10, 3);',
  7606. '$mod.d = 10 % 3;',
  7607. '$mod.d = rtl.trunc(10 / 3);',
  7608. '$mod.d = 1;',
  7609. '$mod.d = 0.1;',
  7610. '$mod.d = 0.3;',
  7611. '$mod.d = -0.1;',
  7612. '$mod.d = -0.3;',
  7613. '$mod.d = -0.003;',
  7614. '$mod.d = -0.123456789;',
  7615. '$mod.d = -300;',
  7616. '$mod.d = -123456;',
  7617. '$mod.d = -1234567.8;',
  7618. '$mod.d = -1.23456789E7;',
  7619. '$mod.d = 1E-12;',
  7620. '$mod.d = -1E-12;',
  7621. '$mod.d = 1.7E308;',
  7622. '$mod.d = -1.7E308;',
  7623. '$mod.d = -9007199254740991;',
  7624. '$mod.d = -9007199254740991;',
  7625. '$mod.d = -9.007199254740992E15;',
  7626. '$mod.d = -9.007199254740992E15;',
  7627. '$mod.d = 9007199254740991;',
  7628. '$mod.d = 0.0;',
  7629. 'Run(1 / 0);',
  7630. 'Run(-1 / 0);',
  7631. '']));
  7632. end;
  7633. procedure TTestModule.TestInteger;
  7634. begin
  7635. StartProgram(false);
  7636. Add([
  7637. 'const',
  7638. ' MinInt = low(NativeInt);',
  7639. ' MaxInt = high(NativeInt);',
  7640. 'type',
  7641. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7642. 'const',
  7643. ' a = low(TMyInt)+High(TMyInt);',
  7644. 'var',
  7645. ' i: TMyInt;',
  7646. 'begin',
  7647. ' i:=-MinInt;',
  7648. ' i:=default(TMyInt);',
  7649. ' i:=low(i)+high(i);',
  7650. '']);
  7651. ConvertProgram;
  7652. CheckSource('TestIntegerRange',
  7653. LinesToStr([
  7654. 'this.MinInt = -9007199254740991;',
  7655. 'this.MaxInt = 9007199254740991;',
  7656. 'this.a = -9007199254740991 + 9007199254740991;',
  7657. 'this.i = 0;',
  7658. '']),
  7659. LinesToStr([
  7660. '$mod.i = - -9007199254740991;',
  7661. '$mod.i = -9007199254740991;',
  7662. '$mod.i = -9007199254740991 + 9007199254740991;',
  7663. '']));
  7664. end;
  7665. procedure TTestModule.TestIntegerRange;
  7666. begin
  7667. StartProgram(false);
  7668. Add([
  7669. 'const',
  7670. ' MinInt = -1;',
  7671. ' MaxInt = +1;',
  7672. 'type',
  7673. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7674. ' TInt2 = 1..3;',
  7675. 'const',
  7676. ' a = low(TMyInt)+High(TMyInt);',
  7677. ' b = low(TInt2)+High(TInt2);',
  7678. ' s1 = [1];',
  7679. ' s2 = [1,2];',
  7680. ' s3 = [1..3];',
  7681. ' s4 = [low(shortint)..high(shortint)];',
  7682. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7683. ' s6 = 1 in s2;',
  7684. 'var',
  7685. ' i: TMyInt;',
  7686. ' i2: TInt2;',
  7687. 'begin',
  7688. ' i:=i2;',
  7689. ' i:=default(TMyInt);',
  7690. ' if i=i2 then ;',
  7691. ' i:=ord(i2);',
  7692. '']);
  7693. ConvertProgram;
  7694. CheckSource('TestIntegerRange',
  7695. LinesToStr([
  7696. 'this.MinInt = -1;',
  7697. 'this.MaxInt = +1;',
  7698. 'this.a = -1 + 1;',
  7699. 'this.b = 1 + 3;',
  7700. 'this.s1 = rtl.createSet(1);',
  7701. 'this.s2 = rtl.createSet(1, 2);',
  7702. 'this.s3 = rtl.createSet(null, 1, 3);',
  7703. 'this.s4 = rtl.createSet(null, -128, 127);',
  7704. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7705. 'this.s6 = 1 in this.s2;',
  7706. 'this.i = 0;',
  7707. 'this.i2 = 0;',
  7708. '']),
  7709. LinesToStr([
  7710. '$mod.i = $mod.i2;',
  7711. '$mod.i = -1;',
  7712. 'if ($mod.i === $mod.i2) ;',
  7713. '$mod.i = $mod.i2;',
  7714. '']));
  7715. end;
  7716. procedure TTestModule.TestIntegerTypecasts;
  7717. begin
  7718. StartProgram(false);
  7719. Add([
  7720. 'var',
  7721. ' i: nativeint;',
  7722. ' b: byte;',
  7723. ' sh: shortint;',
  7724. ' w: word;',
  7725. ' sm: smallint;',
  7726. ' lw: longword;',
  7727. ' li: longint;',
  7728. 'begin',
  7729. ' b:=byte(i);',
  7730. ' sh:=shortint(i);',
  7731. ' w:=word(i);',
  7732. ' sm:=smallint(i);',
  7733. ' lw:=longword(i);',
  7734. ' li:=longint(i);',
  7735. '']);
  7736. ConvertProgram;
  7737. CheckSource('TestIntegerTypecasts',
  7738. LinesToStr([
  7739. 'this.i = 0;',
  7740. 'this.b = 0;',
  7741. 'this.sh = 0;',
  7742. 'this.w = 0;',
  7743. 'this.sm = 0;',
  7744. 'this.lw = 0;',
  7745. 'this.li = 0;',
  7746. '']),
  7747. LinesToStr([
  7748. '$mod.b = $mod.i & 255;',
  7749. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7750. '$mod.w = $mod.i & 65535;',
  7751. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7752. '$mod.lw = $mod.i >>> 0;',
  7753. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7754. '']));
  7755. end;
  7756. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7757. begin
  7758. StartProgram(false);
  7759. Add([
  7760. 'var',
  7761. ' i,j: nativeint;',
  7762. 'begin',
  7763. ' i:=i shr 0;',
  7764. ' i:=i shr 1;',
  7765. ' i:=i shr 3;',
  7766. ' i:=i shr 54;',
  7767. ' i:=j shr i;',
  7768. '']);
  7769. ConvertProgram;
  7770. CheckResolverUnexpectedHints;
  7771. CheckSource('TestInteger_BitwiseShrNativeInt',
  7772. LinesToStr([
  7773. 'this.i = 0;',
  7774. 'this.j = 0;',
  7775. '']),
  7776. LinesToStr([
  7777. '$mod.i = $mod.i;',
  7778. '$mod.i = Math.floor($mod.i / 2);',
  7779. '$mod.i = Math.floor($mod.i / 8);',
  7780. '$mod.i = 0;',
  7781. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7782. '']));
  7783. end;
  7784. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7785. begin
  7786. StartProgram(false);
  7787. Add([
  7788. 'var',
  7789. ' i: nativeint;',
  7790. 'begin',
  7791. ' i:=i shl 0;',
  7792. ' i:=i shl 54;',
  7793. ' i:=123456789012 shl 1;',
  7794. ' i:=i shl 1;',
  7795. '']);
  7796. ConvertProgram;
  7797. CheckResolverUnexpectedHints;
  7798. CheckSource('TestInteger_BitwiseShrNativeInt',
  7799. LinesToStr([
  7800. 'this.i = 0;',
  7801. '']),
  7802. LinesToStr([
  7803. '$mod.i = $mod.i;',
  7804. '$mod.i = 0;',
  7805. '$mod.i = 246913578024;',
  7806. '$mod.i = rtl.shl($mod.i, 1);',
  7807. '']));
  7808. end;
  7809. procedure TTestModule.TestInteger_SystemFunc;
  7810. begin
  7811. StartProgram(true);
  7812. Add([
  7813. 'var',
  7814. ' i: byte;',
  7815. ' s: string;',
  7816. 'begin',
  7817. ' system.inc(i);',
  7818. ' system.str(i,s);',
  7819. ' s:=system.str(i);',
  7820. ' i:=system.low(i);',
  7821. ' i:=system.high(i);',
  7822. ' i:=system.pred(i);',
  7823. ' i:=system.succ(i);',
  7824. ' i:=system.ord(i);',
  7825. '']);
  7826. ConvertProgram;
  7827. CheckResolverUnexpectedHints;
  7828. CheckSource('TestInteger_SystemFunc',
  7829. LinesToStr([
  7830. 'this.i = 0;',
  7831. 'this.s = "";',
  7832. '']),
  7833. LinesToStr([
  7834. '$mod.i += 1;',
  7835. '$mod.s = "" + $mod.i;',
  7836. '$mod.s = "" + $mod.i;',
  7837. '$mod.i = 0;',
  7838. '$mod.i = 255;',
  7839. '$mod.i = $mod.i - 1;',
  7840. '$mod.i = $mod.i + 1;',
  7841. '$mod.i = $mod.i;',
  7842. '']));
  7843. end;
  7844. procedure TTestModule.TestInteger_AssignOutsideConst;
  7845. begin
  7846. StartProgram(false);
  7847. Add([
  7848. 'const',
  7849. ' MinInt = low(longint);',
  7850. ' MaxInt = high(longint);',
  7851. 'type',
  7852. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7853. 'var',
  7854. ' i: TMyInt;',
  7855. ' aByte: byte;',
  7856. ' aShortInt: shortint;',
  7857. ' aWord: word;',
  7858. ' aSmallInt: smallint;',
  7859. ' aLongWord: longword;',
  7860. ' aLongInt: longint;',
  7861. ' aNativeInt: nativeint;',
  7862. ' aNativeUInt: nativeuint;',
  7863. 'begin',
  7864. ' aByte:=$FF;',
  7865. ' aByte:=$100;',
  7866. ' aByte:=-1;',
  7867. ' aByte:=-127;',
  7868. ' aByte:=-128;',
  7869. ' aByte:=-254;',
  7870. ' aByte:=-255;',
  7871. ' aByte:=-256;',
  7872. ' aShortInt:=127;',
  7873. ' aShortInt:=128;',
  7874. ' aShortInt:=-128;',
  7875. ' aShortInt:=-129;',
  7876. ' aWord:=$ffff;',
  7877. ' aWord:=$10000;',
  7878. ' aWord:=-1;',
  7879. ' aWord:=-$ffff;',
  7880. ' aWord:=-$10000;',
  7881. ' aWord:=-$10001;',
  7882. ' aSmallInt:=$7fff;',
  7883. ' aSmallInt:=$8000;',
  7884. ' aSmallInt:=-$8000;',
  7885. ' aSmallInt:=-$8001;',
  7886. ' aLongWord:=$ffffffff;',
  7887. ' aLongWord:=$100000000;',
  7888. ' aLongWord:=-1;',
  7889. ' aLongWord:=-$ffffffff;',
  7890. ' aNativeInt:=$1fffffffffffff;',
  7891. ' aNativeInt:=-$1fffffffffffff;',
  7892. ' aNativeUInt:=$1fffffffffffff;',
  7893. ' aNativeUInt:=-$1fffffffffffff;',
  7894. '']);
  7895. ConvertProgram;
  7896. CheckSource('TestInteger_AssignOutsideConst',
  7897. LinesToStr([
  7898. 'this.MinInt = -2147483648;',
  7899. 'this.MaxInt = 2147483647;',
  7900. 'this.i = 0;',
  7901. 'this.aByte = 0;',
  7902. 'this.aShortInt = 0;',
  7903. 'this.aWord = 0;',
  7904. 'this.aSmallInt = 0;',
  7905. 'this.aLongWord = 0;',
  7906. 'this.aLongInt = 0;',
  7907. 'this.aNativeInt = 0;',
  7908. 'this.aNativeUInt = 0;',
  7909. '']),
  7910. LinesToStr([
  7911. '$mod.aByte = 0xFF;',
  7912. '$mod.aByte = 0;',
  7913. '$mod.aByte = 255;',
  7914. '$mod.aByte = 129;',
  7915. '$mod.aByte = 128;',
  7916. '$mod.aByte = 2;',
  7917. '$mod.aByte = 1;',
  7918. '$mod.aByte = 0;',
  7919. '$mod.aShortInt = 127;',
  7920. '$mod.aShortInt = -128;',
  7921. '$mod.aShortInt = -128;',
  7922. '$mod.aShortInt = 127;',
  7923. '$mod.aWord = 0xffff;',
  7924. '$mod.aWord = 0;',
  7925. '$mod.aWord = 65535;',
  7926. '$mod.aWord = 1;',
  7927. '$mod.aWord = 0;',
  7928. '$mod.aWord = 65535;',
  7929. '$mod.aSmallInt = 0x7fff;',
  7930. '$mod.aSmallInt = -32768;',
  7931. '$mod.aSmallInt = -0x8000;',
  7932. '$mod.aSmallInt = 32767;',
  7933. '$mod.aLongWord = 0xffffffff;',
  7934. '$mod.aLongWord = 0;',
  7935. '$mod.aLongWord = 4294967295;',
  7936. '$mod.aLongWord = 1;',
  7937. '$mod.aNativeInt = 0x1fffffffffffff;',
  7938. '$mod.aNativeInt = -0x1fffffffffffff;',
  7939. '$mod.aNativeUInt = 0x1fffffffffffff;',
  7940. '$mod.aNativeUInt = 1;',
  7941. '']));
  7942. end;
  7943. procedure TTestModule.TestCurrency;
  7944. begin
  7945. StartProgram(false);
  7946. Add([
  7947. 'type',
  7948. ' TCoin = currency;',
  7949. 'const',
  7950. ' a = TCoin(2.7);',
  7951. ' b = a + TCoin(1.7);',
  7952. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7953. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7954. 'var',
  7955. ' c: TCoin = b;',
  7956. ' i: nativeint;',
  7957. ' d: double;',
  7958. ' j: jsvalue;',
  7959. 'function DoIt(c: currency): currency; begin end;',
  7960. 'function GetIt(d: double): double; begin end;',
  7961. 'procedure Write(v: jsvalue); begin end;',
  7962. 'begin',
  7963. ' c:=1.0;',
  7964. ' c:=0.1;',
  7965. ' c:=1.0/3.0;',
  7966. ' c:=1/3;',
  7967. ' c:=a;',
  7968. ' d:=c;',
  7969. ' c:=d;',
  7970. ' c:=currency(c);',
  7971. ' c:=currency(d);',
  7972. ' d:=double(c);',
  7973. ' c:=i;',
  7974. ' c:=currency(i);',
  7975. //' i:=c;', not allowed
  7976. ' i:=nativeint(c);',
  7977. ' c:=c+a;',
  7978. ' c:=-c-a;',
  7979. ' c:=d+c;',
  7980. ' c:=c+d;',
  7981. ' c:=d-c;',
  7982. ' c:=c-d;',
  7983. ' c:=c*a;',
  7984. ' c:=a*c;',
  7985. ' c:=d*c;',
  7986. ' c:=c*d;',
  7987. ' c:=c/a;',
  7988. ' c:=a/c;',
  7989. ' c:=d/c;',
  7990. ' c:=c/d;',
  7991. ' c:=c**a;',
  7992. ' c:=a**c;',
  7993. ' c:=d**c;',
  7994. ' c:=c**d;',
  7995. ' if c=c then ;',
  7996. ' if c=a then ;',
  7997. ' if a=c then ;',
  7998. ' if d=c then ;',
  7999. ' if c=d then ;',
  8000. ' c:=DoIt(c);',
  8001. ' c:=DoIt(i);',
  8002. ' c:=DoIt(d);',
  8003. ' c:=GetIt(c);',
  8004. ' j:=c;',
  8005. ' Write(c);',
  8006. ' c:=default(currency);',
  8007. ' j:=str(c);',
  8008. ' j:=str(c:0:3);',
  8009. '']);
  8010. ConvertProgram;
  8011. CheckSource('TestCurrency',
  8012. LinesToStr([
  8013. 'this.a = 27000;',
  8014. 'this.b = this.a + 17000;',
  8015. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8016. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8017. 'this.c = this.b;',
  8018. 'this.i = 0;',
  8019. 'this.d = 0.0;',
  8020. 'this.j = undefined;',
  8021. 'this.DoIt = function (c) {',
  8022. ' var Result = 0;',
  8023. ' return Result;',
  8024. '};',
  8025. 'this.GetIt = function (d) {',
  8026. ' var Result = 0.0;',
  8027. ' return Result;',
  8028. '};',
  8029. 'this.Write = function (v) {',
  8030. '};',
  8031. '']),
  8032. LinesToStr([
  8033. '$mod.c = 10000;',
  8034. '$mod.c = 1000;',
  8035. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8036. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8037. '$mod.c = $mod.a;',
  8038. '$mod.d = $mod.c / 10000;',
  8039. '$mod.c = rtl.trunc($mod.d * 10000);',
  8040. '$mod.c = $mod.c;',
  8041. '$mod.c = $mod.d * 10000;',
  8042. '$mod.d = $mod.c / 10000;',
  8043. '$mod.c = $mod.i * 10000;',
  8044. '$mod.c = $mod.i * 10000;',
  8045. '$mod.i = rtl.trunc($mod.c / 10000);',
  8046. '$mod.c = $mod.c + $mod.a;',
  8047. '$mod.c = -$mod.c - $mod.a;',
  8048. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8049. '$mod.c = $mod.c + ($mod.d * 10000);',
  8050. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8051. '$mod.c = $mod.c - ($mod.d * 10000);',
  8052. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8053. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8054. '$mod.c = $mod.d * $mod.c;',
  8055. '$mod.c = $mod.c * $mod.d;',
  8056. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8057. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8058. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8059. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8060. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8061. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8062. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8063. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8064. 'if ($mod.c === $mod.c) ;',
  8065. 'if ($mod.c === $mod.a) ;',
  8066. 'if ($mod.a === $mod.c) ;',
  8067. 'if (($mod.d * 10000) === $mod.c) ;',
  8068. 'if ($mod.c === ($mod.d * 10000)) ;',
  8069. '$mod.c = $mod.DoIt($mod.c);',
  8070. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8071. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8072. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8073. '$mod.j = $mod.c / 10000;',
  8074. '$mod.Write($mod.c / 10000);',
  8075. '$mod.c = 0;',
  8076. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8077. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8078. '']));
  8079. end;
  8080. procedure TTestModule.TestForBoolDo;
  8081. begin
  8082. StartProgram(false);
  8083. Add([
  8084. 'var b: boolean;',
  8085. 'begin',
  8086. ' for b:=false to true do ;',
  8087. ' for b:=b downto false do ;',
  8088. ' for b in boolean do ;',
  8089. '']);
  8090. ConvertProgram;
  8091. CheckSource('TestForBoolDo',
  8092. LinesToStr([ // statements
  8093. 'this.b = false;']),
  8094. LinesToStr([ // this.$main
  8095. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8096. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8097. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8098. '']));
  8099. end;
  8100. procedure TTestModule.TestForIntDo;
  8101. begin
  8102. StartProgram(false);
  8103. Add([
  8104. 'var i: longint;',
  8105. 'begin',
  8106. ' for i:=3 to 5 do ;',
  8107. ' for i:=i downto 2 do ;',
  8108. ' for i in byte do ;',
  8109. '']);
  8110. ConvertProgram;
  8111. CheckSource('TestForIntDo',
  8112. LinesToStr([ // statements
  8113. 'this.i = 0;']),
  8114. LinesToStr([ // this.$main
  8115. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8116. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8117. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8118. '']));
  8119. end;
  8120. procedure TTestModule.TestForIntInDo;
  8121. begin
  8122. StartProgram(false);
  8123. Add([
  8124. 'type',
  8125. ' TSetOfInt = set of byte;',
  8126. ' TIntRg = 3..7;',
  8127. ' TSetOfIntRg = set of TIntRg;',
  8128. 'var',
  8129. ' i,i2: longint;',
  8130. ' a1: array of byte;',
  8131. ' a2: array[1..3] of byte;',
  8132. ' soi: TSetOfInt;',
  8133. ' soir: TSetOfIntRg;',
  8134. ' ir: TIntRg;',
  8135. 'begin',
  8136. ' for i in byte do ;',
  8137. ' for i in a1 do ;',
  8138. ' for i in a2 do ;',
  8139. ' for i in [11..13] do ;',
  8140. ' for i in TSetOfInt do ;',
  8141. ' for i in TIntRg do ;',
  8142. ' for i in soi do i2:=i;',
  8143. ' for i in TSetOfIntRg do ;',
  8144. ' for i in soir do ;',
  8145. ' for ir in TIntRg do ;',
  8146. ' for ir in TSetOfIntRg do ;',
  8147. ' for ir in soir do ;',
  8148. '']);
  8149. ConvertProgram;
  8150. CheckSource('TestForIntInDo',
  8151. LinesToStr([ // statements
  8152. 'this.i = 0;',
  8153. 'this.i2 = 0;',
  8154. 'this.a1 = [];',
  8155. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8156. 'this.soi = {};',
  8157. 'this.soir = {};',
  8158. 'this.ir = 0;',
  8159. '']),
  8160. LinesToStr([ // this.$main
  8161. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8162. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8163. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8164. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8165. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8166. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8167. 'for (var $l6 in $mod.soi) {',
  8168. ' $mod.i = +$l6;',
  8169. ' $mod.i2 = $mod.i;',
  8170. '};',
  8171. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8172. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8173. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8174. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8175. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8176. '']));
  8177. end;
  8178. procedure TTestModule.TestCharConst;
  8179. begin
  8180. StartProgram(false);
  8181. Add([
  8182. 'const',
  8183. ' a = #$00F3;',
  8184. ' c: char = ''1'';',
  8185. ' wc: widechar = ''ä'';',
  8186. 'begin',
  8187. ' c:=#0;',
  8188. ' c:=#1;',
  8189. ' c:=#9;',
  8190. ' c:=#10;',
  8191. ' c:=#13;',
  8192. ' c:=#31;',
  8193. ' c:=#32;',
  8194. ' c:=#$A;',
  8195. ' c:=#$0A;',
  8196. ' c:=#$b;',
  8197. ' c:=#$0b;',
  8198. ' c:=^A;',
  8199. ' c:=''"'';',
  8200. ' c:=default(char);',
  8201. ' c:=#$00E4;', // ä
  8202. ' c:=''ä'';',
  8203. ' c:=#$E4;', // ä
  8204. ' c:=#$D800;', // invalid UTF-16
  8205. ' c:=#$DFFF;', // invalid UTF-16
  8206. ' c:=#$FFFF;', // last UCS-2
  8207. ' c:=high(c);', // last UCS-2
  8208. ' c:=#269;',
  8209. '']);
  8210. ConvertProgram;
  8211. CheckSource('TestCharConst',
  8212. LinesToStr([
  8213. 'this.a="ó";',
  8214. 'this.c="1";',
  8215. 'this.wc="ä";'
  8216. ]),
  8217. LinesToStr([
  8218. '$mod.c="\x00";',
  8219. '$mod.c="\x01";',
  8220. '$mod.c="\t";',
  8221. '$mod.c="\n";',
  8222. '$mod.c="\r";',
  8223. '$mod.c="\x1F";',
  8224. '$mod.c=" ";',
  8225. '$mod.c="\n";',
  8226. '$mod.c="\n";',
  8227. '$mod.c="\x0B";',
  8228. '$mod.c="\x0B";',
  8229. '$mod.c="\x01";',
  8230. '$mod.c=''"'';',
  8231. '$mod.c="\x00";',
  8232. '$mod.c = "ä";',
  8233. '$mod.c = "ä";',
  8234. '$mod.c = "ä";',
  8235. '$mod.c="\uD800";',
  8236. '$mod.c="\uDFFF";',
  8237. '$mod.c="\uFFFF";',
  8238. '$mod.c="\uFFFF";',
  8239. '$mod.c = "č";',
  8240. '']));
  8241. end;
  8242. procedure TTestModule.TestChar_Compare;
  8243. begin
  8244. StartProgram(false);
  8245. Add('var');
  8246. Add(' c: char;');
  8247. Add(' b: boolean;');
  8248. Add('begin');
  8249. Add(' b:=c=''1'';');
  8250. Add(' b:=''2''=c;');
  8251. Add(' b:=''3''=''4'';');
  8252. Add(' b:=c<>''5'';');
  8253. Add(' b:=''6''<>c;');
  8254. Add(' b:=c>''7'';');
  8255. Add(' b:=''8''>c;');
  8256. Add(' b:=c>=''9'';');
  8257. Add(' b:=''A''>=c;');
  8258. Add(' b:=c<''B'';');
  8259. Add(' b:=''C''<c;');
  8260. Add(' b:=c<=''D'';');
  8261. Add(' b:=''E''<=c;');
  8262. ConvertProgram;
  8263. CheckSource('TestChar_Compare',
  8264. LinesToStr([
  8265. 'this.c="";',
  8266. 'this.b = false;'
  8267. ]),
  8268. LinesToStr([
  8269. '$mod.b = $mod.c === "1";',
  8270. '$mod.b = "2" === $mod.c;',
  8271. '$mod.b = "3" === "4";',
  8272. '$mod.b = $mod.c !== "5";',
  8273. '$mod.b = "6" !== $mod.c;',
  8274. '$mod.b = $mod.c > "7";',
  8275. '$mod.b = "8" > $mod.c;',
  8276. '$mod.b = $mod.c >= "9";',
  8277. '$mod.b = "A" >= $mod.c;',
  8278. '$mod.b = $mod.c < "B";',
  8279. '$mod.b = "C" < $mod.c;',
  8280. '$mod.b = $mod.c <= "D";',
  8281. '$mod.b = "E" <= $mod.c;',
  8282. '']));
  8283. end;
  8284. procedure TTestModule.TestChar_BuiltInProcs;
  8285. begin
  8286. StartProgram(false);
  8287. Add([
  8288. 'var',
  8289. ' c: char;',
  8290. ' i: longint;',
  8291. ' s: string;',
  8292. 'begin',
  8293. ' i:=ord(c);',
  8294. ' i:=ord(s[i]);',
  8295. ' c:=chr(i);',
  8296. ' c:=pred(c);',
  8297. ' c:=succ(c);',
  8298. ' c:=low(c);',
  8299. ' c:=high(c);',
  8300. ' i:=byte(c);',
  8301. ' i:=word(c);',
  8302. ' i:=longint(c);',
  8303. '']);
  8304. ConvertProgram;
  8305. CheckSource('TestChar_BuiltInProcs',
  8306. LinesToStr([
  8307. 'this.c = "";',
  8308. 'this.i = 0;',
  8309. 'this.s = "";'
  8310. ]),
  8311. LinesToStr([
  8312. '$mod.i = $mod.c.charCodeAt();',
  8313. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8314. '$mod.c = String.fromCharCode($mod.i);',
  8315. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8316. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8317. '$mod.c = "\x00";',
  8318. '$mod.c = "\uFFFF";',
  8319. '$mod.i = $mod.c.charCodeAt() & 255;',
  8320. '$mod.i = $mod.c.charCodeAt();',
  8321. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8322. '']));
  8323. end;
  8324. procedure TTestModule.TestStringConst;
  8325. begin
  8326. StartProgram(false);
  8327. Add([
  8328. '{$H+}',
  8329. 'const',
  8330. ' a = #$00F3#$017C;', // first <256, then >=256
  8331. ' b = string(''a'');',
  8332. ' c = string(''ä'');',
  8333. ' d = UnicodeString(''b'');',
  8334. ' e = UnicodeString(''ö'');',
  8335. ' f = low(a)+high(b);',
  8336. ' g: word = low(a);',
  8337. 'var',
  8338. ' s: string = ''abc'';',
  8339. ' i: longint;',
  8340. 'begin',
  8341. ' s:='''';',
  8342. ' s:=#13#10;',
  8343. ' s:=#9''foo'';',
  8344. ' s:=#$A9;',
  8345. ' s:=''foo''#13''bar'';',
  8346. ' s:=''"'';',
  8347. ' s:=''"''''"'';',
  8348. ' s:=#$20AC;', // euro
  8349. ' s:=#$10437;', // outside BMP
  8350. ' s:=''abc''#$20AC;', // ascii,#
  8351. ' s:=''ä''#$20AC;', // non ascii,#
  8352. ' s:=#$20AC''abc'';', // #, ascii
  8353. ' s:=#$20AC''ä'';', // #, non ascii
  8354. ' s:=default(string);',
  8355. ' s:=concat(s);',
  8356. ' s:=concat(s,''a'',s);',
  8357. ' s:=#250#269;',
  8358. ' i:=low(s)+high(a);',
  8359. ' s:=''a/b'';',
  8360. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8361. '']);
  8362. ConvertProgram;
  8363. CheckSource('TestStringConst',
  8364. LinesToStr([
  8365. 'this.a = "óż";',
  8366. 'this.b = "a";',
  8367. 'this.c = "ä";',
  8368. 'this.d = "b";',
  8369. 'this.e = "ö";',
  8370. 'this.f = 1 + this.b.length;',
  8371. 'this.g = 1;',
  8372. 'this.s="abc";',
  8373. 'this.i = 0;',
  8374. '']),
  8375. LinesToStr([
  8376. '$mod.s="";',
  8377. '$mod.s="\r\n";',
  8378. '$mod.s="\tfoo";',
  8379. '$mod.s="©";',
  8380. '$mod.s="foo\rbar";',
  8381. '$mod.s=''"'';',
  8382. '$mod.s=''"\''"'';',
  8383. '$mod.s="€";',
  8384. '$mod.s="'#$F0#$90#$90#$B7'";',
  8385. '$mod.s = "abc€";',
  8386. '$mod.s = "ä€";',
  8387. '$mod.s = "€abc";',
  8388. '$mod.s = "ۊ";',
  8389. '$mod.s="";',
  8390. '$mod.s = $mod.s;',
  8391. '$mod.s = $mod.s.concat("a", $mod.s);',
  8392. '$mod.s = "úč";',
  8393. '$mod.i = 1 + $mod.a.length;',
  8394. '$mod.s = "a/b";',
  8395. '']));
  8396. end;
  8397. procedure TTestModule.TestStringConst_InvalidUTF16;
  8398. begin
  8399. StartProgram(false);
  8400. Add([
  8401. 'const',
  8402. ' a: char = #$D87E;',
  8403. ' b: string = #$D87E;',
  8404. ' c: string = #$D87E#43;',
  8405. 'begin',
  8406. ' c:=''abc''#$D87E;',
  8407. ' c:=#0#1#2;',
  8408. ' c:=#127;',
  8409. ' c:=#128;',
  8410. ' c:=#255;',
  8411. ' c:=#256;',
  8412. '']);
  8413. ConvertProgram;
  8414. CheckSource('TestStringConst',
  8415. LinesToStr([
  8416. 'this.a = "\uD87E";',
  8417. 'this.b = "\uD87E";',
  8418. 'this.c = "\uD87E+";',
  8419. '']),
  8420. LinesToStr([
  8421. '$mod.c = "abc\uD87E";',
  8422. '$mod.c = "\x00\x01\x02";',
  8423. '$mod.c = "'#127'";',
  8424. '$mod.c = "'#$c2#$80'";',
  8425. '$mod.c = "'#$c3#$BF'";',
  8426. '$mod.c = "'#$c4#$80'";',
  8427. '']));
  8428. end;
  8429. procedure TTestModule.TestStringConstSurrogate;
  8430. begin
  8431. StartProgram(false);
  8432. Add([
  8433. 'var',
  8434. ' s: string;',
  8435. 'begin',
  8436. ' s:=''😊'';', // 1F60A
  8437. '']);
  8438. ConvertProgram;
  8439. CheckSource('TestStringConstSurrogate',
  8440. LinesToStr([
  8441. 'this.s="";'
  8442. ]),
  8443. LinesToStr([
  8444. '$mod.s="😊";'
  8445. ]));
  8446. end;
  8447. procedure TTestModule.TestStringConst_Multiline;
  8448. begin
  8449. StartProgram(false);
  8450. Add([
  8451. '{$modeswitch multilinestrings}',
  8452. 'const',
  8453. ' a = ``;',
  8454. ' b = `',
  8455. 'line`;',
  8456. ' c = `Single`;',
  8457. ' d = ````;',
  8458. ' e = `abc``xyz`;',
  8459. ' f = `first''line',
  8460. ' second''line`#10;',
  8461. 'begin',
  8462. '']);
  8463. ConvertProgram;
  8464. CheckSource('TestStringConst_Multiline',
  8465. LinesToStr([
  8466. 'this.a = "";',
  8467. 'this.b = "\nline";',
  8468. 'this.c = "Single";',
  8469. 'this.d = "`";',
  8470. 'this.e = "abc`xyz";',
  8471. 'this.f = "first''line\n second''line\n";',
  8472. '']),
  8473. LinesToStr([
  8474. ]));
  8475. end;
  8476. procedure TTestModule.TestString_Length;
  8477. begin
  8478. StartProgram(false);
  8479. Add('const c = ''foo'';');
  8480. Add('var');
  8481. Add(' s: string;');
  8482. Add(' i: longint;');
  8483. Add('begin');
  8484. Add(' i:=length(s);');
  8485. Add(' i:=length(s+s);');
  8486. Add(' i:=length(''abc'');');
  8487. Add(' i:=length(c);');
  8488. ConvertProgram;
  8489. CheckSource('TestString_Length',
  8490. LinesToStr([
  8491. 'this.c = "foo";',
  8492. 'this.s = "";',
  8493. 'this.i = 0;',
  8494. '']),
  8495. LinesToStr([
  8496. '$mod.i = $mod.s.length;',
  8497. '$mod.i = ($mod.s+$mod.s).length;',
  8498. '$mod.i = "abc".length;',
  8499. '$mod.i = $mod.c.length;',
  8500. '']));
  8501. end;
  8502. procedure TTestModule.TestString_Compare;
  8503. begin
  8504. StartProgram(false);
  8505. Add('var');
  8506. Add(' s, t: string;');
  8507. Add(' b: boolean;');
  8508. Add('begin');
  8509. Add(' b:=s=t;');
  8510. Add(' b:=s<>t;');
  8511. Add(' b:=s>t;');
  8512. Add(' b:=s>=t;');
  8513. Add(' b:=s<t;');
  8514. Add(' b:=s<=t;');
  8515. ConvertProgram;
  8516. CheckSource('TestString_Compare',
  8517. LinesToStr([ // statements
  8518. 'this.s = "";',
  8519. 'this.t = "";',
  8520. 'this.b =false;'
  8521. ]),
  8522. LinesToStr([ // this.$main
  8523. '$mod.b = $mod.s === $mod.t;',
  8524. '$mod.b = $mod.s !== $mod.t;',
  8525. '$mod.b = $mod.s > $mod.t;',
  8526. '$mod.b = $mod.s >= $mod.t;',
  8527. '$mod.b = $mod.s < $mod.t;',
  8528. '$mod.b = $mod.s <= $mod.t;',
  8529. '']));
  8530. end;
  8531. procedure TTestModule.TestString_SetLength;
  8532. begin
  8533. StartProgram(false);
  8534. Add([
  8535. 'procedure Fly(var s: string);',
  8536. 'begin',
  8537. ' SetLength(s,1);',
  8538. 'end;',
  8539. 'procedure Run(var s: unicodestring);',
  8540. 'begin',
  8541. ' SetLength(s,2);',
  8542. 'end;',
  8543. 'var s: string;',
  8544. ' u: unicodestring;',
  8545. 'begin',
  8546. ' SetLength(s,3);',
  8547. ' SetLength(u,4);',
  8548. '']);
  8549. ConvertProgram;
  8550. CheckSource('TestString_SetLength',
  8551. LinesToStr([ // statements
  8552. 'this.Fly = function (s) {',
  8553. ' s.set(rtl.strSetLength(s.get(), 1));',
  8554. '};',
  8555. 'this.Run = function (s) {',
  8556. ' s.set(rtl.strSetLength(s.get(), 2));',
  8557. '};',
  8558. 'this.s = "";',
  8559. 'this.u = "";',
  8560. '']),
  8561. LinesToStr([ // this.$main
  8562. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8563. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8564. ]));
  8565. end;
  8566. procedure TTestModule.TestString_CharAt;
  8567. begin
  8568. StartProgram(false);
  8569. Add([
  8570. 'var',
  8571. ' s: string;',
  8572. ' c: char;',
  8573. ' b: boolean;',
  8574. 'begin',
  8575. ' b:= s[1] = c;',
  8576. ' b:= c = s[1];',
  8577. ' b:= c <> s[1];',
  8578. ' b:= c > s[1];',
  8579. ' b:= c >= s[1];',
  8580. ' b:= c < s[2];',
  8581. ' b:= c <= s[1];',
  8582. ' s[1] := c;',
  8583. ' s[2+3] := c;']);
  8584. ConvertProgram;
  8585. CheckSource('TestString_CharAt',
  8586. LinesToStr([ // statements
  8587. 'this.s = "";',
  8588. 'this.c = "";',
  8589. 'this.b = false;'
  8590. ]),
  8591. LinesToStr([ // this.$main
  8592. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8593. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8594. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8595. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8596. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8597. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8598. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8599. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8600. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8601. '']));
  8602. end;
  8603. procedure TTestModule.TestStringHMinusFail;
  8604. begin
  8605. StartProgram(false);
  8606. Add([
  8607. '{$H-}',
  8608. 'var s: string;',
  8609. 'begin']);
  8610. ConvertProgram;
  8611. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8612. end;
  8613. procedure TTestModule.TestStr;
  8614. begin
  8615. StartProgram(false);
  8616. Add('var');
  8617. Add(' b: boolean;');
  8618. Add(' i: longint;');
  8619. Add(' d: double;');
  8620. Add(' s: string;');
  8621. Add('begin');
  8622. Add(' str(b,s);');
  8623. Add(' str(i,s);');
  8624. Add(' str(d,s);');
  8625. Add(' str(i:3,s);');
  8626. Add(' str(d:3:2,s);');
  8627. Add(' Str(12.456:12:1,s);');
  8628. Add(' Str(12.456:12,s);');
  8629. Add(' s:=str(b);');
  8630. Add(' s:=str(i);');
  8631. Add(' s:=str(d);');
  8632. Add(' s:=str(i,i);');
  8633. Add(' s:=str(i:3);');
  8634. Add(' s:=str(d:3:2);');
  8635. Add(' s:=str(i:4,i);');
  8636. Add(' s:=str(i,i:5);');
  8637. Add(' s:=str(i:4,i:5);');
  8638. Add(' s:=str(s,s);');
  8639. Add(' s:=str(s,''foo'');');
  8640. ConvertProgram;
  8641. CheckSource('TestStr',
  8642. LinesToStr([ // statements
  8643. 'this.b = false;',
  8644. 'this.i = 0;',
  8645. 'this.d = 0.0;',
  8646. 'this.s = "";',
  8647. '']),
  8648. LinesToStr([ // this.$main
  8649. '$mod.s = ""+$mod.b;',
  8650. '$mod.s = ""+$mod.i;',
  8651. '$mod.s = rtl.floatToStr($mod.d);',
  8652. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8653. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8654. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8655. '$mod.s = rtl.floatToStr(12.456,12);',
  8656. '$mod.s = ""+$mod.b;',
  8657. '$mod.s = ""+$mod.i;',
  8658. '$mod.s = rtl.floatToStr($mod.d);',
  8659. '$mod.s = ""+$mod.i+$mod.i;',
  8660. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8661. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8662. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8663. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8664. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8665. '$mod.s = $mod.s + $mod.s;',
  8666. '$mod.s = $mod.s + "foo";',
  8667. '']));
  8668. end;
  8669. procedure TTestModule.TestBaseType_AnsiStringFail;
  8670. begin
  8671. StartProgram(false);
  8672. Add('var s: AnsiString');
  8673. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8674. ConvertProgram;
  8675. end;
  8676. procedure TTestModule.TestBaseType_WideStringFail;
  8677. begin
  8678. StartProgram(false);
  8679. Add('var s: WideString');
  8680. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8681. ConvertProgram;
  8682. end;
  8683. procedure TTestModule.TestBaseType_ShortStringFail;
  8684. begin
  8685. StartProgram(false);
  8686. Add('var s: ShortString');
  8687. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8688. ConvertProgram;
  8689. end;
  8690. procedure TTestModule.TestBaseType_RawByteStringFail;
  8691. begin
  8692. StartProgram(false);
  8693. Add('var s: RawByteString');
  8694. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8695. ConvertProgram;
  8696. end;
  8697. procedure TTestModule.TestTypeShortstring_Fail;
  8698. begin
  8699. StartProgram(false);
  8700. Add('type t = string[12];');
  8701. Add('var s: t;');
  8702. Add('begin');
  8703. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8704. ConvertProgram;
  8705. end;
  8706. procedure TTestModule.TestCharSet_Custom;
  8707. begin
  8708. StartProgram(false);
  8709. Add([
  8710. 'type',
  8711. ' TCharRg = ''a''..''z'';',
  8712. ' TSetOfCharRg = set of TCharRg;',
  8713. ' TCharRg2 = ''m''..''p'';',
  8714. 'const',
  8715. ' crg: TCharRg = ''b'';',
  8716. 'var',
  8717. ' c: char;',
  8718. ' crg2: TCharRg2;',
  8719. ' s: TSetOfCharRg;',
  8720. 'begin',
  8721. ' c:=crg;',
  8722. ' crg:=c;',
  8723. ' crg2:=crg;',
  8724. ' if c=crg then ;',
  8725. ' if crg=c then ;',
  8726. ' if crg=crg2 then ;',
  8727. ' if c in s then ;',
  8728. ' if crg2 in s then ;',
  8729. ' c:=default(TCharRg);',
  8730. '']);
  8731. ConvertProgram;
  8732. CheckSource('TestCharSet_Custom',
  8733. LinesToStr([ // statements
  8734. 'this.crg = "b";',
  8735. 'this.c = "";',
  8736. 'this.crg2 = "m";',
  8737. 'this.s = {};',
  8738. '']),
  8739. LinesToStr([ // this.$main
  8740. '$mod.c = $mod.crg;',
  8741. '$mod.crg = $mod.c;',
  8742. '$mod.crg2 = $mod.crg;',
  8743. 'if ($mod.c === $mod.crg) ;',
  8744. 'if ($mod.crg === $mod.c) ;',
  8745. 'if ($mod.crg === $mod.crg2) ;',
  8746. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8747. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8748. '$mod.c = "a";',
  8749. '']));
  8750. end;
  8751. procedure TTestModule.TestWideChar;
  8752. begin
  8753. StartProgram(false);
  8754. Add([
  8755. 'procedure Fly(var c: char);',
  8756. 'begin',
  8757. 'end;',
  8758. 'procedure Run(var c: widechar);',
  8759. 'begin',
  8760. 'end;',
  8761. 'var',
  8762. ' c: char;',
  8763. ' wc: widechar;',
  8764. ' w: word;',
  8765. 'begin',
  8766. ' Fly(wc);',
  8767. ' Run(c);',
  8768. ' wc:=WideChar(w);',
  8769. ' w:=ord(wc);',
  8770. '']);
  8771. ConvertProgram;
  8772. CheckSource('TestWideChar_VarArg',
  8773. LinesToStr([ // statements
  8774. 'this.Fly = function (c) {',
  8775. '};',
  8776. 'this.Run = function (c) {',
  8777. '};',
  8778. 'this.c = "";',
  8779. 'this.wc = "";',
  8780. 'this.w = 0;',
  8781. '']),
  8782. LinesToStr([ // this.$main
  8783. '$mod.Fly({',
  8784. ' p: $mod,',
  8785. ' get: function () {',
  8786. ' return this.p.wc;',
  8787. ' },',
  8788. ' set: function (v) {',
  8789. ' this.p.wc = v;',
  8790. ' }',
  8791. '});',
  8792. '$mod.Run({',
  8793. ' p: $mod,',
  8794. ' get: function () {',
  8795. ' return this.p.c;',
  8796. ' },',
  8797. ' set: function (v) {',
  8798. ' this.p.c = v;',
  8799. ' }',
  8800. '});',
  8801. '$mod.wc = String.fromCharCode($mod.w);',
  8802. '$mod.w = $mod.wc.charCodeAt();',
  8803. '',
  8804. '']));
  8805. end;
  8806. procedure TTestModule.TestForCharDo;
  8807. begin
  8808. StartProgram(false);
  8809. Add([
  8810. 'var c: char;',
  8811. 'begin',
  8812. ' for c:=''a'' to ''c'' do ;',
  8813. ' for c:=c downto ''a'' do ;',
  8814. ' for c:=''Б'' to ''Я'' do ;',
  8815. '']);
  8816. ConvertProgram;
  8817. CheckSource('TestForCharDo',
  8818. LinesToStr([ // statements
  8819. 'this.c = "";']),
  8820. LinesToStr([ // this.$main
  8821. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8822. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8823. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8824. '']));
  8825. end;
  8826. procedure TTestModule.TestForCharInDo;
  8827. begin
  8828. StartProgram(false);
  8829. Add([
  8830. 'type',
  8831. ' TSetOfChar = set of char;',
  8832. ' TCharRg = ''a''..''z'';',
  8833. ' TSetOfCharRg = set of TCharRg;',
  8834. 'const Foo = ''foo'';',
  8835. 'var',
  8836. ' c,c2: char;',
  8837. ' s: string;',
  8838. ' a1: array of char;',
  8839. ' a2: array[1..3] of char;',
  8840. ' soc: TSetOfChar;',
  8841. ' socr: TSetOfCharRg;',
  8842. ' cr: TCharRg;',
  8843. 'begin',
  8844. ' for c in foo do ;',
  8845. ' for c in s do ;',
  8846. ' for c in char do ;',
  8847. ' for c in a1 do ;',
  8848. ' for c in a2 do ;',
  8849. ' for c in [''1''..''3''] do ;',
  8850. ' for c in TSetOfChar do ;',
  8851. ' for c in TCharRg do ;',
  8852. ' for c in soc do c2:=c;',
  8853. ' for c in TSetOfCharRg do ;',
  8854. ' for c in socr do ;',
  8855. ' for cr in TCharRg do ;',
  8856. ' for cr in TSetOfCharRg do ;',
  8857. ' for cr in socr do ;',
  8858. '']);
  8859. ConvertProgram;
  8860. CheckSource('TestForCharInDo',
  8861. LinesToStr([ // statements
  8862. 'this.Foo = "foo";',
  8863. 'this.c = "";',
  8864. 'this.c2 = "";',
  8865. 'this.s = "";',
  8866. 'this.a1 = [];',
  8867. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8868. 'this.soc = {};',
  8869. 'this.socr = {};',
  8870. 'this.cr = "a";',
  8871. '']),
  8872. LinesToStr([ // this.$main
  8873. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8874. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8875. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8876. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8877. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8878. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8879. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8880. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8881. 'for (var $l8 in $mod.soc) {',
  8882. ' $mod.c = String.fromCharCode($l8);',
  8883. ' $mod.c2 = $mod.c;',
  8884. '};',
  8885. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8886. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8887. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8888. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8889. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8890. '']));
  8891. end;
  8892. procedure TTestModule.TestProcTwoArgs;
  8893. begin
  8894. StartProgram(false);
  8895. Add('procedure Test(a,b: longint);');
  8896. Add('begin');
  8897. Add('end;');
  8898. Add('begin');
  8899. ConvertProgram;
  8900. CheckSource('TestProcTwoArgs',
  8901. LinesToStr([ // statements
  8902. 'this.Test = function (a,b) {',
  8903. '};'
  8904. ]),
  8905. LinesToStr([ // this.$main
  8906. ''
  8907. ]));
  8908. end;
  8909. procedure TTestModule.TestProc_DefaultValue;
  8910. begin
  8911. StartProgram(false);
  8912. Add('procedure p1(i: longint = 1);');
  8913. Add('begin');
  8914. Add('end;');
  8915. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8916. Add('begin');
  8917. Add('end;');
  8918. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8919. Add('begin');
  8920. Add('end;');
  8921. Add('begin');
  8922. Add(' p1;');
  8923. Add(' p1();');
  8924. Add(' p1(11);');
  8925. Add(' p2;');
  8926. Add(' p2();');
  8927. Add(' p2(12);');
  8928. Add(' p2(13,''b'');');
  8929. Add(' p3();');
  8930. ConvertProgram;
  8931. CheckSource('TestProc_DefaultValue',
  8932. LinesToStr([ // statements
  8933. 'this.p1 = function (i) {',
  8934. '};',
  8935. 'this.p2 = function (i,c) {',
  8936. '};',
  8937. 'this.p3 = function (d,b,s) {',
  8938. '};'
  8939. ]),
  8940. LinesToStr([ // this.$main
  8941. ' $mod.p1(1);',
  8942. ' $mod.p1(1);',
  8943. ' $mod.p1(11);',
  8944. ' $mod.p2(1,"a");',
  8945. ' $mod.p2(1,"a");',
  8946. ' $mod.p2(12,"a");',
  8947. ' $mod.p2(13,"b");',
  8948. ' $mod.p3(1.0,false,"abc");'
  8949. ]));
  8950. end;
  8951. procedure TTestModule.TestFunctionInt;
  8952. begin
  8953. StartProgram(false);
  8954. Add('function MyTest(Bar: longint): longint;');
  8955. Add('begin');
  8956. Add(' Result:=2*bar');
  8957. Add('end;');
  8958. Add('begin');
  8959. ConvertProgram;
  8960. CheckSource('TestFunctionInt',
  8961. LinesToStr([ // statements
  8962. 'this.MyTest = function (Bar) {',
  8963. ' var Result = 0;',
  8964. ' Result = 2*Bar;',
  8965. ' return Result;',
  8966. '};'
  8967. ]),
  8968. LinesToStr([ // this.$main
  8969. ''
  8970. ]));
  8971. end;
  8972. procedure TTestModule.TestFunctionString;
  8973. begin
  8974. StartProgram(false);
  8975. Add('function Test(Bar: string): string;');
  8976. Add('begin');
  8977. Add(' Result:=bar+BAR');
  8978. Add('end;');
  8979. Add('begin');
  8980. ConvertProgram;
  8981. CheckSource('TestFunctionString',
  8982. LinesToStr([ // statements
  8983. 'this.Test = function (Bar) {',
  8984. ' var Result = "";',
  8985. ' Result = Bar+Bar;',
  8986. ' return Result;',
  8987. '};'
  8988. ]),
  8989. LinesToStr([ // this.$main
  8990. ''
  8991. ]));
  8992. end;
  8993. procedure TTestModule.TestIfThen;
  8994. begin
  8995. StartProgram(false);
  8996. Add([
  8997. 'var b: boolean;',
  8998. 'begin',
  8999. ' if b then ;',
  9000. ' if b then else ;']);
  9001. ConvertProgram;
  9002. CheckSource('TestIfThen',
  9003. LinesToStr([ // statements
  9004. 'this.b = false;',
  9005. '']),
  9006. LinesToStr([ // this.$main
  9007. 'if ($mod.b) ;',
  9008. 'if ($mod.b) ;',
  9009. '']));
  9010. end;
  9011. procedure TTestModule.TestForLoop;
  9012. begin
  9013. StartProgram(false);
  9014. Add('var');
  9015. Add(' vI, vJ, vN: longint;');
  9016. Add('begin');
  9017. Add(' VJ:=0;');
  9018. Add(' VN:=3;');
  9019. Add(' for VI:=1 to VN do');
  9020. Add(' begin');
  9021. Add(' VJ:=VJ+VI;');
  9022. Add(' end;');
  9023. ConvertProgram;
  9024. CheckSource('TestForLoop',
  9025. LinesToStr([ // statements
  9026. 'this.vI = 0;',
  9027. 'this.vJ = 0;',
  9028. 'this.vN = 0;'
  9029. ]),
  9030. LinesToStr([ // this.$main
  9031. ' $mod.vJ = 0;',
  9032. ' $mod.vN = 3;',
  9033. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9034. ' $mod.vI = $l;',
  9035. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9036. ' };',
  9037. '']));
  9038. end;
  9039. procedure TTestModule.TestForLoopInsideFunction;
  9040. begin
  9041. StartProgram(false);
  9042. Add('function SumNumbers(Count: longint): longint;');
  9043. Add('var');
  9044. Add(' vI, vJ: longint;');
  9045. Add('begin');
  9046. Add(' vj:=0;');
  9047. Add(' for vi:=1 to count do');
  9048. Add(' begin');
  9049. Add(' vj:=vj+vi;');
  9050. Add(' end;');
  9051. Add('end;');
  9052. Add('begin');
  9053. Add(' sumnumbers(3);');
  9054. ConvertProgram;
  9055. CheckSource('TestForLoopInsideFunction',
  9056. LinesToStr([ // statements
  9057. 'this.SumNumbers = function (Count) {',
  9058. ' var Result = 0;',
  9059. ' var vI = 0;',
  9060. ' var vJ = 0;',
  9061. ' vJ = 0;',
  9062. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9063. ' vI = $l;',
  9064. ' vJ = vJ + vI;',
  9065. ' };',
  9066. ' return Result;',
  9067. '};'
  9068. ]),
  9069. LinesToStr([ // $mod.$main
  9070. ' $mod.SumNumbers(3);'
  9071. ]));
  9072. end;
  9073. procedure TTestModule.TestForLoop_ReadVarAfter;
  9074. begin
  9075. StartProgram(false);
  9076. Add('var');
  9077. Add(' vI: longint;');
  9078. Add('begin');
  9079. Add(' for vi:=1 to 2 do ;');
  9080. Add(' if vi=3 then ;');
  9081. ConvertProgram;
  9082. CheckSource('TestForLoop',
  9083. LinesToStr([ // statements
  9084. 'this.vI = 0;'
  9085. ]),
  9086. LinesToStr([ // this.$main
  9087. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9088. ' if ($mod.vI===3) ;'
  9089. ]));
  9090. end;
  9091. procedure TTestModule.TestForLoop_Nested;
  9092. begin
  9093. StartProgram(false);
  9094. Add('function SumNumbers(Count: longint): longint;');
  9095. Add('var');
  9096. Add(' vI, vJ, vK: longint;');
  9097. Add('begin');
  9098. Add(' VK:=0;');
  9099. Add(' for VI:=1 to count do');
  9100. Add(' begin');
  9101. Add(' for vj:=1 to vi do');
  9102. Add(' begin');
  9103. Add(' vk:=VK+VI;');
  9104. Add(' end;');
  9105. Add(' end;');
  9106. Add('end;');
  9107. Add('begin');
  9108. Add(' sumnumbers(3);');
  9109. ConvertProgram;
  9110. CheckSource('TestForLoopInFunction',
  9111. LinesToStr([ // statements
  9112. 'this.SumNumbers = function (Count) {',
  9113. ' var Result = 0;',
  9114. ' var vI = 0;',
  9115. ' var vJ = 0;',
  9116. ' var vK = 0;',
  9117. ' vK = 0;',
  9118. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9119. ' vI = $l;',
  9120. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9121. ' vJ = $l1;',
  9122. ' vK = vK + vI;',
  9123. ' };',
  9124. ' };',
  9125. ' return Result;',
  9126. '};'
  9127. ]),
  9128. LinesToStr([ // $mod.$main
  9129. ' $mod.SumNumbers(3);'
  9130. ]));
  9131. end;
  9132. procedure TTestModule.TestRepeatUntil;
  9133. begin
  9134. StartProgram(false);
  9135. Add('var');
  9136. Add(' vI, vJ, vN: longint;');
  9137. Add('begin');
  9138. Add(' vn:=3;');
  9139. Add(' vj:=0;');
  9140. Add(' VI:=0;');
  9141. Add(' repeat');
  9142. Add(' VI:=vi+1;');
  9143. Add(' vj:=VJ+vI;');
  9144. Add(' until vi>=vn');
  9145. ConvertProgram;
  9146. CheckSource('TestRepeatUntil',
  9147. LinesToStr([ // statements
  9148. 'this.vI = 0;',
  9149. 'this.vJ = 0;',
  9150. 'this.vN = 0;'
  9151. ]),
  9152. LinesToStr([ // $mod.$main
  9153. ' $mod.vN = 3;',
  9154. ' $mod.vJ = 0;',
  9155. ' $mod.vI = 0;',
  9156. ' do{',
  9157. ' $mod.vI = $mod.vI + 1;',
  9158. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9159. ' }while(!($mod.vI>=$mod.vN));'
  9160. ]));
  9161. end;
  9162. procedure TTestModule.TestAsmBlock;
  9163. begin
  9164. StartProgram(false);
  9165. Add([
  9166. 'var',
  9167. ' vI: longint;',
  9168. 'begin',
  9169. ' vi:=1;',
  9170. ' asm',
  9171. ' if (vI===1) {',
  9172. ' vI=2;',
  9173. //' console.log(''end;'');', ToDo
  9174. ' }',
  9175. ' if (vI===2){ vI=3; }',
  9176. ' end;',
  9177. ' VI:=4;']);
  9178. ConvertProgram;
  9179. CheckSource('TestAsmBlock',
  9180. LinesToStr([ // statements
  9181. 'this.vI = 0;'
  9182. ]),
  9183. LinesToStr([ // $mod.$main
  9184. '$mod.vI = 1;',
  9185. 'if (vI===1) {',
  9186. ' vI=2;',
  9187. '}',
  9188. 'if (vI===2){ vI=3; }',
  9189. ';',
  9190. '$mod.vI = 4;'
  9191. ]));
  9192. end;
  9193. procedure TTestModule.TestAsmPas_Impl;
  9194. begin
  9195. StartUnit(false);
  9196. Add('interface');
  9197. Add('const cIntf: longint = 1;');
  9198. Add('var vIntf: longint;');
  9199. Add('implementation');
  9200. Add('const cImpl: longint = 2;');
  9201. Add('var vImpl: longint;');
  9202. Add('procedure DoIt;');
  9203. Add('const cLoc: longint = 3;');
  9204. Add('var vLoc: longint;');
  9205. Add('begin;');
  9206. Add(' asm');
  9207. //Add(' pas(vIntf)=pas(cIntf);');
  9208. //Add(' pas(vImpl)=pas(cImpl);');
  9209. //Add(' pas(vLoc)=pas(cLoc);');
  9210. Add(' end;');
  9211. Add('end;');
  9212. ConvertUnit;
  9213. CheckSource('TestAsmPas_Impl',
  9214. LinesToStr([
  9215. 'var $impl = $mod.$impl;',
  9216. 'this.cIntf = 1;',
  9217. 'this.vIntf = 0;',
  9218. '']),
  9219. '', // this.$init
  9220. LinesToStr([ // implementation
  9221. '$impl.cImpl = 2;',
  9222. '$impl.vImpl = 0;',
  9223. 'var cLoc = 3;',
  9224. '$impl.DoIt = function () {',
  9225. ' var vLoc = 0;',
  9226. '};',
  9227. '']) );
  9228. end;
  9229. procedure TTestModule.TestTryFinally;
  9230. begin
  9231. StartProgram(false);
  9232. Add('var i: longint;');
  9233. Add('begin');
  9234. Add(' try');
  9235. Add(' i:=0; i:=2 div i;');
  9236. Add(' finally');
  9237. Add(' i:=3');
  9238. Add(' end;');
  9239. ConvertProgram;
  9240. CheckSource('TestTryFinally',
  9241. LinesToStr([ // statements
  9242. 'this.i = 0;'
  9243. ]),
  9244. LinesToStr([ // $mod.$main
  9245. 'try {',
  9246. ' $mod.i = 0;',
  9247. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9248. '} finally {',
  9249. ' $mod.i = 3;',
  9250. '};'
  9251. ]));
  9252. end;
  9253. procedure TTestModule.TestTryExcept;
  9254. begin
  9255. StartProgram(false);
  9256. Add([
  9257. 'type',
  9258. ' TObject = class end;',
  9259. ' Exception = class Msg: string; end;',
  9260. ' EInvalidCast = class(Exception) end;',
  9261. 'var vI: longint;',
  9262. 'begin',
  9263. ' try',
  9264. ' vi:=1;',
  9265. ' except',
  9266. ' vi:=2',
  9267. ' end;',
  9268. ' try',
  9269. ' vi:=3;',
  9270. ' except',
  9271. ' raise;',
  9272. ' end;',
  9273. ' try',
  9274. ' VI:=4;',
  9275. ' except',
  9276. ' on einvalidcast do',
  9277. ' raise;',
  9278. ' on E: exception do',
  9279. ' if e.msg='''' then',
  9280. ' raise e;',
  9281. ' else',
  9282. ' vi:=5',
  9283. ' end;',
  9284. ' try',
  9285. ' VI:=6;',
  9286. ' except',
  9287. ' on einvalidcast do ;',
  9288. ' end;',
  9289. '']);
  9290. ConvertProgram;
  9291. CheckSource('TestTryExcept',
  9292. LinesToStr([ // statements
  9293. 'rtl.createClass(this, "TObject", null, function () {',
  9294. ' this.$init = function () {',
  9295. ' };',
  9296. ' this.$final = function () {',
  9297. ' };',
  9298. '});',
  9299. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9300. ' this.$init = function () {',
  9301. ' $mod.TObject.$init.call(this);',
  9302. ' this.Msg = "";',
  9303. ' };',
  9304. '});',
  9305. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9306. '});',
  9307. 'this.vI = 0;'
  9308. ]),
  9309. LinesToStr([ // $mod.$main
  9310. 'try {',
  9311. ' $mod.vI = 1;',
  9312. '} catch ($e) {',
  9313. ' $mod.vI = 2;',
  9314. '};',
  9315. 'try {',
  9316. ' $mod.vI = 3;',
  9317. '} catch ($e) {',
  9318. ' throw $e;',
  9319. '};',
  9320. 'try {',
  9321. ' $mod.vI = 4;',
  9322. '} catch ($e) {',
  9323. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9324. ' throw $e',
  9325. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9326. ' var E = $e;',
  9327. ' if (E.Msg === "") throw E;',
  9328. ' } else {',
  9329. ' $mod.vI = 5;',
  9330. ' }',
  9331. '};',
  9332. 'try {',
  9333. ' $mod.vI = 6;',
  9334. '} catch ($e) {',
  9335. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9336. ' } else throw $e',
  9337. '};',
  9338. '']));
  9339. end;
  9340. procedure TTestModule.TestTryExcept_ReservedWords;
  9341. begin
  9342. StartProgram(false);
  9343. Add([
  9344. 'type',
  9345. ' TObject = class end;',
  9346. ' Exception = class',
  9347. ' Symbol: string;',
  9348. ' end;',
  9349. 'var &try: longint;',
  9350. 'begin',
  9351. ' try',
  9352. ' &try:=4;',
  9353. ' except',
  9354. ' on Error: exception do',
  9355. ' if errOR.symBol='''' then',
  9356. ' raise ERRor;',
  9357. ' end;',
  9358. '']);
  9359. ConvertProgram;
  9360. CheckSource('TestTryExcept_ReservedWords',
  9361. LinesToStr([ // statements
  9362. 'rtl.createClass(this, "TObject", null, function () {',
  9363. ' this.$init = function () {',
  9364. ' };',
  9365. ' this.$final = function () {',
  9366. ' };',
  9367. '});',
  9368. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9369. ' this.$init = function () {',
  9370. ' $mod.TObject.$init.call(this);',
  9371. ' this.Symbol = "";',
  9372. ' };',
  9373. '});',
  9374. 'this.Try = 0;',
  9375. '']),
  9376. LinesToStr([ // $mod.$main
  9377. 'try {',
  9378. ' $mod.Try = 4;',
  9379. '} catch ($e) {',
  9380. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9381. ' var error = $e;',
  9382. ' if (error.Symbol === "") throw error;',
  9383. ' } else throw $e',
  9384. '};',
  9385. '']));
  9386. end;
  9387. procedure TTestModule.TestIfThenRaiseElse;
  9388. begin
  9389. StartProgram(false);
  9390. Add([
  9391. 'type',
  9392. ' TObject = class',
  9393. ' constructor Create;',
  9394. ' end;',
  9395. 'constructor TObject.Create;',
  9396. 'begin',
  9397. 'end;',
  9398. 'var b: boolean;',
  9399. 'begin',
  9400. ' if b then',
  9401. ' raise TObject.Create',
  9402. ' else',
  9403. ' b:=false;',
  9404. '']);
  9405. ConvertProgram;
  9406. CheckSource('TestIfThenRaiseElse',
  9407. LinesToStr([ // statements
  9408. 'rtl.createClass(this, "TObject", null, function () {',
  9409. ' this.$init = function () {',
  9410. ' };',
  9411. ' this.$final = function () {',
  9412. ' };',
  9413. ' this.Create = function () {',
  9414. ' return this;',
  9415. ' };',
  9416. '});',
  9417. 'this.b = false;',
  9418. '']),
  9419. LinesToStr([ // $mod.$main
  9420. 'if ($mod.b) {',
  9421. ' throw $mod.TObject.$create("Create")}',
  9422. ' else $mod.b = false;',
  9423. '']));
  9424. end;
  9425. procedure TTestModule.TestCaseOf;
  9426. begin
  9427. StartProgram(false);
  9428. Add([
  9429. 'const e: longint; external name ''$e'';',
  9430. 'var vI: longint;',
  9431. 'begin',
  9432. ' case vi of',
  9433. ' 1: ;',
  9434. ' 2: vi:=3;',
  9435. ' e: ;',
  9436. ' else',
  9437. ' VI:=4',
  9438. ' end;']);
  9439. ConvertProgram;
  9440. CheckSource('TestCaseOf',
  9441. LinesToStr([ // statements
  9442. 'this.vI = 0;'
  9443. ]),
  9444. LinesToStr([ // $mod.$main
  9445. 'var $tmp = $mod.vI;',
  9446. 'if ($tmp === 1) {}',
  9447. 'else if ($tmp === 2) {',
  9448. ' $mod.vI = 3}',
  9449. ' else if ($tmp === $e) {}',
  9450. 'else {',
  9451. ' $mod.vI = 4;',
  9452. '};'
  9453. ]));
  9454. end;
  9455. procedure TTestModule.TestCaseOf_UseSwitch;
  9456. begin
  9457. StartProgram(false);
  9458. Converter.UseSwitchStatement:=true;
  9459. Add('var Vi: longint;');
  9460. Add('begin');
  9461. Add(' case vi of');
  9462. Add(' 1: ;');
  9463. Add(' 2: VI:=3;');
  9464. Add(' else');
  9465. Add(' vi:=4');
  9466. Add(' end;');
  9467. ConvertProgram;
  9468. CheckSource('TestCaseOf_UseSwitch',
  9469. LinesToStr([ // statements
  9470. 'this.Vi = 0;'
  9471. ]),
  9472. LinesToStr([ // $mod.$main
  9473. 'switch ($mod.Vi) {',
  9474. 'case 1:',
  9475. ' break;',
  9476. 'case 2:',
  9477. ' $mod.Vi = 3;',
  9478. ' break;',
  9479. 'default:',
  9480. ' $mod.Vi = 4;',
  9481. '};'
  9482. ]));
  9483. end;
  9484. procedure TTestModule.TestCaseOfNoElse;
  9485. begin
  9486. StartProgram(false);
  9487. Add('var Vi: longint;');
  9488. Add('begin');
  9489. Add(' case vi of');
  9490. Add(' 1: begin vi:=2; VI:=3; end;');
  9491. Add(' end;');
  9492. ConvertProgram;
  9493. CheckSource('TestCaseOfNoElse',
  9494. LinesToStr([ // statements
  9495. 'this.Vi = 0;'
  9496. ]),
  9497. LinesToStr([ // $mod.$main
  9498. 'var $tmp = $mod.Vi;',
  9499. 'if ($tmp === 1) {',
  9500. ' $mod.Vi = 2;',
  9501. ' $mod.Vi = 3;',
  9502. '};'
  9503. ]));
  9504. end;
  9505. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9506. begin
  9507. StartProgram(false);
  9508. Converter.UseSwitchStatement:=true;
  9509. Add('var vI: longint;');
  9510. Add('begin');
  9511. Add(' case vi of');
  9512. Add(' 1: begin VI:=2; vi:=3; end;');
  9513. Add(' end;');
  9514. ConvertProgram;
  9515. CheckSource('TestCaseOfNoElse_UseSwitch',
  9516. LinesToStr([ // statements
  9517. 'this.vI = 0;'
  9518. ]),
  9519. LinesToStr([ // $mod.$main
  9520. 'switch ($mod.vI) {',
  9521. 'case 1:',
  9522. ' $mod.vI = 2;',
  9523. ' $mod.vI = 3;',
  9524. ' break;',
  9525. '};'
  9526. ]));
  9527. end;
  9528. procedure TTestModule.TestCaseOfRange;
  9529. begin
  9530. StartProgram(false);
  9531. Add('var vI: longint;');
  9532. Add('begin');
  9533. Add(' case vi of');
  9534. Add(' 1..3: vi:=14;');
  9535. Add(' 4,5: vi:=16;');
  9536. Add(' 6..7,9..10: ;');
  9537. Add(' else ;');
  9538. Add(' end;');
  9539. ConvertProgram;
  9540. CheckSource('TestCaseOfRange',
  9541. LinesToStr([ // statements
  9542. 'this.vI = 0;'
  9543. ]),
  9544. LinesToStr([ // $mod.$main
  9545. 'var $tmp = $mod.vI;',
  9546. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9547. ' $mod.vI = 14',
  9548. '} else if (($tmp === 4) || ($tmp === 5)){',
  9549. ' $mod.vI = 16',
  9550. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9551. ]));
  9552. end;
  9553. procedure TTestModule.TestCaseOfString;
  9554. begin
  9555. StartProgram(false);
  9556. Add([
  9557. 'var s,h: string;',
  9558. 'begin',
  9559. ' case s of',
  9560. ' ''foo'': s:=h;',
  9561. ' ''a''..''z'': h:=s;',
  9562. ' ''ў'', ''ё'': ;',
  9563. ' ''Б''..''Я'': ;',
  9564. ' end;',
  9565. '']);
  9566. ConvertProgram;
  9567. CheckSource('TestCaseOfString',
  9568. LinesToStr([ // statements
  9569. 'this.s = "";',
  9570. 'this.h = "";',
  9571. '']),
  9572. LinesToStr([ // $mod.$main
  9573. 'var $tmp = $mod.s;',
  9574. 'if ($tmp === "foo") {',
  9575. ' $mod.s = $mod.h}',
  9576. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9577. ' $mod.h = $mod.s}',
  9578. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9579. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9580. '']));
  9581. end;
  9582. procedure TTestModule.TestCaseOfChar;
  9583. begin
  9584. StartProgram(false);
  9585. Add([
  9586. 'var s,h: char;',
  9587. 'begin',
  9588. ' case s of',
  9589. ' ''a''..''z'': h:=s;',
  9590. ' ''ä'': ;',
  9591. ' ''ў'', ''ё'': ;',
  9592. ' ''Б''..''Я'': ;',
  9593. ' end;',
  9594. '']);
  9595. ConvertProgram;
  9596. CheckSource('TestCaseOfString',
  9597. LinesToStr([ // statements
  9598. 'this.s = "";',
  9599. 'this.h = "";',
  9600. '']),
  9601. LinesToStr([ // $mod.$main
  9602. 'var $tmp = $mod.s;',
  9603. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9604. ' $mod.h = $mod.s}',
  9605. ' else if ($tmp === "ä") {}',
  9606. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9607. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9608. '']));
  9609. end;
  9610. procedure TTestModule.TestCaseOfExternalClassConst;
  9611. begin
  9612. StartProgram(false);
  9613. Add([
  9614. '{$modeswitch externalclass}',
  9615. 'type',
  9616. ' TBird = class external name ''Bird''',
  9617. ' const e: longint;',
  9618. ' end;',
  9619. 'var vI: longint;',
  9620. 'begin',
  9621. ' case vi of',
  9622. ' 1: vi:=3;',
  9623. ' TBird.e: ;',
  9624. ' end;']);
  9625. ConvertProgram;
  9626. CheckSource('TestCaseOfExternalClassConst',
  9627. LinesToStr([ // statements
  9628. 'this.vI = 0;'
  9629. ]),
  9630. LinesToStr([ // $mod.$main
  9631. 'var $tmp = $mod.vI;',
  9632. 'if ($tmp === 1) {',
  9633. ' $mod.vI = 3}',
  9634. ' else if ($tmp === Bird.e) ;'
  9635. ]));
  9636. end;
  9637. procedure TTestModule.TestDebugger;
  9638. begin
  9639. StartProgram(false);
  9640. Add([
  9641. 'procedure DoIt;',
  9642. 'begin',
  9643. ' deBugger;',
  9644. ' DeBugger();',
  9645. 'end;',
  9646. 'begin',
  9647. ' Debugger;']);
  9648. ConvertProgram;
  9649. CheckSource('TestDebugger',
  9650. LinesToStr([ // statements
  9651. 'this.DoIt = function () {',
  9652. ' debugger;',
  9653. ' debugger;',
  9654. '};',
  9655. '']),
  9656. LinesToStr([ // $mod.$main
  9657. 'debugger;',
  9658. '']));
  9659. end;
  9660. procedure TTestModule.TestArray_Dynamic;
  9661. begin
  9662. StartProgram(false);
  9663. Add([
  9664. 'type',
  9665. ' TArrayInt = array of longint;',
  9666. 'var',
  9667. ' Arr: TArrayInt;',
  9668. ' i: longint;',
  9669. ' b: boolean;',
  9670. 'begin',
  9671. ' SetLength(arr,3);',
  9672. ' arr[0]:=4;',
  9673. ' arr[1]:=length(arr)+arr[0];',
  9674. ' arr[i]:=5;',
  9675. ' arr[arr[i]]:=arr[6];',
  9676. ' i:=low(arr);',
  9677. ' i:=high(arr);',
  9678. ' b:=Assigned(arr);',
  9679. ' Arr:=default(TArrayInt);']);
  9680. ConvertProgram;
  9681. CheckSource('TestArray_Dynamic',
  9682. LinesToStr([ // statements
  9683. 'this.Arr = [];',
  9684. 'this.i = 0;',
  9685. 'this.b = false;'
  9686. ]),
  9687. LinesToStr([ // $mod.$main
  9688. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9689. '$mod.Arr[0] = 4;',
  9690. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9691. '$mod.Arr[$mod.i] = 5;',
  9692. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9693. '$mod.i = 0;',
  9694. '$mod.i = rtl.length($mod.Arr) - 1;',
  9695. '$mod.b = rtl.length($mod.Arr) > 0;',
  9696. '$mod.Arr = [];',
  9697. '']));
  9698. end;
  9699. procedure TTestModule.TestArray_Dynamic_Nil;
  9700. begin
  9701. StartProgram(false);
  9702. Add('type');
  9703. Add(' TArrayInt = array of longint;');
  9704. Add('var');
  9705. Add(' Arr: TArrayInt;');
  9706. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9707. Add('begin');
  9708. Add(' arr:=nil;');
  9709. Add(' if arr=nil then;');
  9710. Add(' if nil=arr then;');
  9711. Add(' if arr<>nil then;');
  9712. Add(' if nil<>arr then;');
  9713. Add(' DoIt(nil,nil);');
  9714. ConvertProgram;
  9715. CheckSource('TestArray_Dynamic',
  9716. LinesToStr([ // statements
  9717. 'this.Arr = [];',
  9718. 'this.DoIt = function(i,j){',
  9719. '};'
  9720. ]),
  9721. LinesToStr([ // $mod.$main
  9722. '$mod.Arr = [];',
  9723. 'if (rtl.length($mod.Arr) === 0) ;',
  9724. 'if (rtl.length($mod.Arr) === 0) ;',
  9725. 'if (rtl.length($mod.Arr) > 0) ;',
  9726. 'if (rtl.length($mod.Arr) > 0) ;',
  9727. '$mod.DoIt([],[]);',
  9728. '']));
  9729. end;
  9730. procedure TTestModule.TestArray_DynMultiDimensional;
  9731. begin
  9732. StartProgram(false);
  9733. Add([
  9734. 'type',
  9735. ' TArrayInt = array of longint;',
  9736. ' TArrayArrayInt = array of TArrayInt;',
  9737. 'var',
  9738. ' Arr: TArrayInt;',
  9739. ' Arr2: TArrayArrayInt;',
  9740. ' i: longint;',
  9741. 'begin',
  9742. ' arr2:=nil;',
  9743. ' if arr2=nil then;',
  9744. ' if nil=arr2 then;',
  9745. ' i:=low(arr2);',
  9746. ' i:=low(arr2[1]);',
  9747. ' i:=high(arr2);',
  9748. ' i:=high(arr2[2]);',
  9749. ' arr2[3]:=arr;',
  9750. ' arr2[4][5]:=i;',
  9751. ' i:=arr2[6][7];',
  9752. ' arr2[8,9]:=i;',
  9753. ' i:=arr2[10,11];',
  9754. ' SetLength(arr2,14);',
  9755. ' SetLength(arr2[15],16);']);
  9756. ConvertProgram;
  9757. CheckSource('TestArray_Dynamic',
  9758. LinesToStr([ // statements
  9759. 'this.Arr = [];',
  9760. 'this.Arr2 = [];',
  9761. 'this.i = 0;'
  9762. ]),
  9763. LinesToStr([ // $mod.$main
  9764. '$mod.Arr2 = [];',
  9765. 'if (rtl.length($mod.Arr2) === 0) ;',
  9766. 'if (rtl.length($mod.Arr2) === 0) ;',
  9767. '$mod.i = 0;',
  9768. '$mod.i = 0;',
  9769. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9770. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9771. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9772. '$mod.Arr2[4][5] = $mod.i;',
  9773. '$mod.i = $mod.Arr2[6][7];',
  9774. '$mod.Arr2[8][9] = $mod.i;',
  9775. '$mod.i = $mod.Arr2[10][11];',
  9776. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9777. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9778. '']));
  9779. end;
  9780. procedure TTestModule.TestArray_DynamicAssign;
  9781. begin
  9782. StartProgram(false);
  9783. Add([
  9784. 'type',
  9785. ' TArrayInt = array of longint;',
  9786. ' TArrayArrayInt = array of TArrayInt;',
  9787. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9788. 'begin',
  9789. 'end;',
  9790. 'procedure Fly(var a: TArrayInt);',
  9791. 'begin',
  9792. 'end;',
  9793. 'var',
  9794. ' Arr: TArrayInt;',
  9795. ' Arr2: TArrayArrayInt;',
  9796. 'begin',
  9797. ' arr:=nil;',
  9798. ' arr2:=nil;',
  9799. ' arr2[1]:=nil;',
  9800. ' arr2[2]:=arr;',
  9801. ' Run(arr,arr,arr);',
  9802. ' Fly(arr);',
  9803. ' Run(arr2[4],arr2[5],arr2[6]);',
  9804. ' Fly(arr2[7]);',
  9805. '']);
  9806. ConvertProgram;
  9807. CheckSource('TestArray_DynamicAssign',
  9808. LinesToStr([ // statements
  9809. 'this.Run = function (a, b, c) {',
  9810. '};',
  9811. 'this.Fly = function (a) {',
  9812. '};',
  9813. 'this.Arr = [];',
  9814. 'this.Arr2 = [];',
  9815. '']),
  9816. LinesToStr([ // $mod.$main
  9817. '$mod.Arr = [];',
  9818. '$mod.Arr2 = [];',
  9819. '$mod.Arr2[1] = [];',
  9820. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9821. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9822. '$mod.Fly({',
  9823. ' p: $mod,',
  9824. ' get: function () {',
  9825. ' return this.p.Arr;',
  9826. ' },',
  9827. ' set: function (v) {',
  9828. ' this.p.Arr = v;',
  9829. ' }',
  9830. '});',
  9831. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9832. '$mod.Fly({',
  9833. ' a: 7,',
  9834. ' p: $mod.Arr2,',
  9835. ' get: function () {',
  9836. ' return this.p[this.a];',
  9837. ' },',
  9838. ' set: function (v) {',
  9839. ' this.p[this.a] = v;',
  9840. ' }',
  9841. '});',
  9842. '']));
  9843. end;
  9844. procedure TTestModule.TestArray_StaticInt;
  9845. begin
  9846. StartProgram(false);
  9847. Add('type');
  9848. Add(' TArrayInt = array[2..4] of longint;');
  9849. Add('var');
  9850. Add(' Arr: TArrayInt;');
  9851. Add(' Arr2: TArrayInt = (5,6,7);');
  9852. Add(' i: longint;');
  9853. Add(' b: boolean;');
  9854. Add('begin');
  9855. Add(' arr[2]:=4;');
  9856. Add(' arr[3]:=arr[2]+arr[3];');
  9857. Add(' arr[i]:=5;');
  9858. Add(' arr[arr[i]]:=arr[high(arr)];');
  9859. Add(' i:=low(arr);');
  9860. Add(' i:=high(arr);');
  9861. Add(' b:=arr[2]=arr[3];');
  9862. Add(' arr:=default(TArrayInt);');
  9863. ConvertProgram;
  9864. CheckSource('TestArray_StaticInt',
  9865. LinesToStr([ // statements
  9866. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9867. 'this.Arr2 = [5, 6, 7];',
  9868. 'this.i = 0;',
  9869. 'this.b = false;'
  9870. ]),
  9871. LinesToStr([ // $mod.$main
  9872. '$mod.Arr[0] = 4;',
  9873. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9874. '$mod.Arr[$mod.i-2] = 5;',
  9875. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9876. '$mod.i = 2;',
  9877. '$mod.i = 4;',
  9878. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9879. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9880. '']));
  9881. end;
  9882. procedure TTestModule.TestArray_StaticBool;
  9883. begin
  9884. StartProgram(false);
  9885. Add('type');
  9886. Add(' TBools = array[boolean] of boolean;');
  9887. Add(' TBool2 = array[true..true] of boolean;');
  9888. Add('var');
  9889. Add(' Arr: TBools;');
  9890. Add(' Arr2: TBool2;');
  9891. Add(' Arr3: TBools = (true,false);');
  9892. Add(' b: boolean;');
  9893. Add('begin');
  9894. Add(' b:=low(arr);');
  9895. Add(' b:=high(arr);');
  9896. Add(' arr[true]:=false;');
  9897. Add(' arr[false]:=arr[b] or arr[true];');
  9898. Add(' arr[b]:=true;');
  9899. Add(' arr[arr[b]]:=arr[high(arr)];');
  9900. Add(' b:=arr[false]=arr[true];');
  9901. Add(' b:=low(arr2);');
  9902. Add(' b:=high(arr2);');
  9903. Add(' arr2[true]:=true;');
  9904. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9905. Add(' arr2[b]:=false;');
  9906. ConvertProgram;
  9907. CheckSource('TestArray_StaticBool',
  9908. LinesToStr([ // statements
  9909. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9910. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9911. 'this.Arr3 = [true, false];',
  9912. 'this.b = false;'
  9913. ]),
  9914. LinesToStr([ // $mod.$main
  9915. '$mod.b = false;',
  9916. '$mod.b = true;',
  9917. '$mod.Arr[1] = false;',
  9918. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9919. '$mod.Arr[+$mod.b] = true;',
  9920. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9921. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9922. '$mod.b = true;',
  9923. '$mod.b = true;',
  9924. '$mod.Arr2[0] = true;',
  9925. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9926. '$mod.Arr2[1-$mod.b] = false;',
  9927. '']));
  9928. end;
  9929. procedure TTestModule.TestArray_StaticChar;
  9930. begin
  9931. StartProgram(false);
  9932. Add([
  9933. 'type',
  9934. ' TChars = array[char] of char;',
  9935. ' TChars2 = array[''a''..''z''] of char;',
  9936. 'var',
  9937. ' Arr: TChars;',
  9938. ' Arr2: TChars2;',
  9939. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9940. ' Arr4: array[11..13] of char = ''pas'';',
  9941. ' Arr5: array[21..22] of char = ''äö'';',
  9942. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9943. ' c: char;',
  9944. ' b: boolean;',
  9945. 'begin',
  9946. ' c:=low(arr);',
  9947. ' c:=high(arr);',
  9948. ' arr[''B'']:=''a'';',
  9949. ' arr[''D'']:=arr[c];',
  9950. ' arr[c]:=arr[''d''];',
  9951. ' arr[arr[c]]:=arr[high(arr)];',
  9952. ' b:=arr[low(arr)]=arr[''e''];',
  9953. ' c:=low(arr2);',
  9954. ' c:=high(arr2);',
  9955. ' arr2[''b'']:=''f'';',
  9956. ' arr2[''a'']:=arr2[c];',
  9957. ' arr2[c]:=arr2[''g''];']);
  9958. ConvertProgram;
  9959. CheckSource('TestArray_StaticChar',
  9960. LinesToStr([ // statements
  9961. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9962. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9963. 'this.Arr3 = ["p", "a", "s"];',
  9964. 'this.Arr4 = ["p", "a", "s"];',
  9965. 'this.Arr5 = ["ä", "ö"];',
  9966. 'this.Arr6 = ["ä", "ö"];',
  9967. 'this.c = "";',
  9968. 'this.b = false;',
  9969. '']),
  9970. LinesToStr([ // $mod.$main
  9971. '$mod.c = "\x00";',
  9972. '$mod.c = "\uFFFF";',
  9973. '$mod.Arr[66] = "a";',
  9974. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9975. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9976. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9977. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9978. '$mod.c = "a";',
  9979. '$mod.c = "z";',
  9980. '$mod.Arr2[1] = "f";',
  9981. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9982. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9983. '']));
  9984. end;
  9985. procedure TTestModule.TestArray_StaticMultiDim;
  9986. begin
  9987. StartProgram(false);
  9988. Add([
  9989. 'type',
  9990. ' TArrayInt = array[1..3] of longint;',
  9991. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9992. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  9993. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  9994. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  9995. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  9996. 'var',
  9997. ' Arr: TArrayInt;',
  9998. ' Arr2: TArrayArrayInt;',
  9999. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10000. ' Arr4: TArrayArrayInt;',
  10001. ' ArrDim2: TArrayDim2Int;',
  10002. ' ArrDim3: TArrayDim3Int;',
  10003. ' ArrDim4: TArrayDim4Int;',
  10004. ' i: longint;',
  10005. 'begin',
  10006. ' i:=low(arr);',
  10007. ' i:=low(arr2);',
  10008. ' i:=low(arr2[5]);',
  10009. ' i:=high(arr);',
  10010. ' i:=high(arr2);',
  10011. ' i:=high(arr2[6]);',
  10012. ' arr2[5]:=arr;',
  10013. ' arr2[6][2]:=i;',
  10014. ' i:=arr2[6][3];',
  10015. ' arr2[6,3]:=i;',
  10016. ' i:=arr2[5,2];',
  10017. ' arr2:=arr2;',// clone multi dim static array
  10018. ' arr3:=arr3;',// clone anonymous multi dim static array
  10019. ' arr4:=arr4;',
  10020. ' Arr:=Arr;',
  10021. ' ArrDim2:=ArrDim2;',
  10022. ' ArrDim3:=ArrDim3;',
  10023. ' ArrDim4:=ArrDim4;',
  10024. '']);
  10025. ConvertProgram;
  10026. CheckSource('TestArray_StaticMultiDim',
  10027. LinesToStr([ // statements
  10028. 'this.TArrayArrayInt$clone = function (a) {',
  10029. ' var b = [];',
  10030. ' b.length = 2;',
  10031. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10032. ' return b;',
  10033. '};',
  10034. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10035. ' var b = [];',
  10036. ' b.length = 2;',
  10037. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10038. ' return b;',
  10039. '};',
  10040. 'this.TArrayDim2Int$clone = function (a) {',
  10041. ' var b = [];',
  10042. ' b.length = 2;',
  10043. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10044. ' return b;',
  10045. '};',
  10046. 'this.TArrayDim3Int$clone = function (a) {',
  10047. ' var b = [];',
  10048. ' b.length = 2;',
  10049. ' for (var c = 0; c < 2; c++) {',
  10050. ' var d = b[c] = [];',
  10051. ' d.length = 3;',
  10052. ' var e = a[c];',
  10053. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10054. ' };',
  10055. ' return b;',
  10056. '};',
  10057. 'this.TArrayDim4Int$clone = function (a) {',
  10058. ' var b = [];',
  10059. ' b.length = 2;',
  10060. ' for (var c = 0; c < 2; c++) {',
  10061. ' var d = b[c] = [];',
  10062. ' d.length = 3;',
  10063. ' var e = a[c];',
  10064. ' for (var f = 0; f < 3; f++) {',
  10065. ' var g = d[f] = [];',
  10066. ' g.length = 4;',
  10067. ' var h = e[f];',
  10068. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10069. ' };',
  10070. ' };',
  10071. ' return b;',
  10072. '};',
  10073. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10074. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10075. 'this.Arr3$a$clone = function (a) {',
  10076. ' var b = [];',
  10077. ' b.length = 2;',
  10078. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10079. ' return b;',
  10080. '};',
  10081. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10082. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10083. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10084. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10085. 'this.ArrDim4 = rtl.arraySetLength(',
  10086. ' null,',
  10087. ' 0,',
  10088. ' 2,',
  10089. ' 3,',
  10090. ' 4,',
  10091. ' 5',
  10092. ');',
  10093. 'this.i = 0;'
  10094. ]),
  10095. LinesToStr([ // $mod.$main
  10096. '$mod.i = 1;',
  10097. '$mod.i = 5;',
  10098. '$mod.i = 1;',
  10099. '$mod.i = 3;',
  10100. '$mod.i = 6;',
  10101. '$mod.i = 3;',
  10102. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10103. '$mod.Arr2[1][1] = $mod.i;',
  10104. '$mod.i = $mod.Arr2[1][2];',
  10105. '$mod.Arr2[1][2] = $mod.i;',
  10106. '$mod.i = $mod.Arr2[0][1];',
  10107. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10108. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10109. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10110. '$mod.Arr = $mod.Arr.slice(0);',
  10111. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10112. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10113. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10114. '']));
  10115. end;
  10116. procedure TTestModule.TestArray_StaticInFunction;
  10117. begin
  10118. StartProgram(false);
  10119. Add([
  10120. 'const TArrayInt = 3;',
  10121. 'const TArrayArrayInt = 4;',
  10122. 'procedure DoIt;',
  10123. 'type',
  10124. ' TArrayInt = array[1..3] of longint;',
  10125. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10126. 'var',
  10127. ' Arr: TArrayInt;',
  10128. ' Arr2: TArrayArrayInt;',
  10129. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10130. ' i: longint;',
  10131. 'begin',
  10132. ' arr2[5]:=arr;',
  10133. ' arr2:=arr2;',// clone multi dim static array
  10134. ' arr3:=arr3;',// clone multi dim anonymous static array
  10135. 'end;',
  10136. 'begin',
  10137. '']);
  10138. ConvertProgram;
  10139. CheckSource('TestArray_StaticInFunction',
  10140. LinesToStr([ // statements
  10141. 'this.TArrayInt = 3;',
  10142. 'this.TArrayArrayInt = 4;',
  10143. 'var TArrayArrayInt$1$clone = function (a) {',
  10144. ' var b = [];',
  10145. ' b.length = 2;',
  10146. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10147. ' return b;',
  10148. '};',
  10149. 'var Arr3$a$clone = function (a) {',
  10150. ' var b = [];',
  10151. ' b.length = 2;',
  10152. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10153. ' return b;',
  10154. '};',
  10155. 'this.DoIt = function () {',
  10156. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10157. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10158. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10159. ' var i = 0;',
  10160. ' Arr2[0] = Arr.slice(0);',
  10161. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10162. ' Arr3 = Arr3$a$clone(Arr3);',
  10163. '};',
  10164. '']),
  10165. LinesToStr([ // $mod.$main
  10166. '']));
  10167. end;
  10168. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10169. begin
  10170. StartProgram(false);
  10171. Add([
  10172. 'type',
  10173. ' TArrayInt = array[1..3,1..2] of longint;',
  10174. 'var',
  10175. ' a,b: TArrayInt;',
  10176. 'begin',
  10177. ' if a=b then ;',
  10178. '']);
  10179. SetExpectedPasResolverError('compare static array is not supported',
  10180. nXIsNotSupported);
  10181. ConvertProgram;
  10182. end;
  10183. procedure TTestModule.TestArrayOfRecord;
  10184. begin
  10185. StartProgram(false);
  10186. Add([
  10187. 'type',
  10188. ' TRec = record',
  10189. ' Int: longint;',
  10190. ' end;',
  10191. ' TArrayRec = array of TRec;',
  10192. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10193. 'begin',
  10194. 'end;',
  10195. 'var',
  10196. ' Arr: TArrayRec;',
  10197. ' r: TRec;',
  10198. ' i: longint;',
  10199. 'begin',
  10200. ' SetLength(arr,3);',
  10201. ' arr[0].int:=4;',
  10202. ' arr[1].int:=length(arr)+arr[2].int;',
  10203. ' arr[arr[i].int].int:=arr[5].int;',
  10204. ' arr[7]:=r;',
  10205. ' r:=arr[8];',
  10206. ' i:=low(arr);',
  10207. ' i:=high(arr);',
  10208. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10209. ConvertProgram;
  10210. CheckSource('TestArrayOfRecord',
  10211. LinesToStr([ // statements
  10212. 'rtl.recNewT(this, "TRec", function () {',
  10213. ' this.Int = 0;',
  10214. ' this.$eq = function (b) {',
  10215. ' return this.Int === b.Int;',
  10216. ' };',
  10217. ' this.$assign = function (s) {',
  10218. ' this.Int = s.Int;',
  10219. ' return this;',
  10220. ' };',
  10221. '});',
  10222. 'this.DoIt = function (vd, vc, vv) {',
  10223. '};',
  10224. 'this.Arr = [];',
  10225. 'this.r = this.TRec.$new();',
  10226. 'this.i = 0;'
  10227. ]),
  10228. LinesToStr([ // $mod.$main
  10229. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10230. '$mod.Arr[0].Int = 4;',
  10231. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10232. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10233. '$mod.Arr[7].$assign($mod.r);',
  10234. '$mod.r.$assign($mod.Arr[8]);',
  10235. '$mod.i = 0;',
  10236. '$mod.i = rtl.length($mod.Arr)-1;',
  10237. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10238. '']));
  10239. end;
  10240. procedure TTestModule.TestArray_StaticRecord;
  10241. begin
  10242. StartProgram(false);
  10243. Add([
  10244. 'type',
  10245. ' TRec = record',
  10246. ' Int: longint;',
  10247. ' end;',
  10248. ' TArrayRec = array[1..2] of TRec;',
  10249. 'var',
  10250. ' Arr: TArrayRec;',
  10251. 'begin',
  10252. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10253. '']);
  10254. ConvertProgram;
  10255. CheckSource('TestArray_StaticRecord',
  10256. LinesToStr([ // statements
  10257. 'rtl.recNewT(this, "TRec", function () {',
  10258. ' this.Int = 0;',
  10259. ' this.$eq = function (b) {',
  10260. ' return this.Int === b.Int;',
  10261. ' };',
  10262. ' this.$assign = function (s) {',
  10263. ' this.Int = s.Int;',
  10264. ' return this;',
  10265. ' };',
  10266. '});',
  10267. 'this.TArrayRec$clone = function (a) {',
  10268. ' var b = [];',
  10269. ' b.length = 2;',
  10270. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10271. ' return b;',
  10272. '};',
  10273. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10274. '']),
  10275. LinesToStr([ // $mod.$main
  10276. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10277. end;
  10278. procedure TTestModule.TestArrayOfSet;
  10279. begin
  10280. StartProgram(false);
  10281. Add([
  10282. 'type',
  10283. ' TFlag = (big,small);',
  10284. ' TSetOfFlag = set of tflag;',
  10285. ' TArrayFlag = array of TSetOfFlag;',
  10286. 'procedure DoIt(const a: Tarrayflag);',
  10287. 'begin',
  10288. 'end;',
  10289. 'var',
  10290. ' f: TFlag;',
  10291. ' s: TSetOfFlag;',
  10292. ' Arr: TArrayFlag;',
  10293. ' i: longint;',
  10294. 'begin',
  10295. ' SetLength(arr,3);',
  10296. ' arr[0]:=s;',
  10297. ' arr[1]:=[big];',
  10298. ' arr[2]:=[big]+s;',
  10299. ' arr[3]:=s+[big];',
  10300. ' arr[4]:=arr[5];',
  10301. ' s:=arr[6];',
  10302. ' i:=low(arr);',
  10303. ' i:=high(arr);',
  10304. ' DoIt(arr);',
  10305. ' DoIt([s]);',
  10306. ' DoIt([[],s]);',
  10307. ' DoIt([s,[]]);',
  10308. '']);
  10309. ConvertProgram;
  10310. CheckSource('TestArrayOfSet',
  10311. LinesToStr([ // statements
  10312. 'this.TFlag = {',
  10313. ' "0": "big",',
  10314. ' big: 0,',
  10315. ' "1": "small",',
  10316. ' small: 1',
  10317. '};',
  10318. 'this.DoIt = function (a) {',
  10319. '};',
  10320. 'this.f = 0;',
  10321. 'this.s = {};',
  10322. 'this.Arr = [];',
  10323. 'this.i = 0;',
  10324. '']),
  10325. LinesToStr([ // $mod.$main
  10326. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10327. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10328. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10329. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10330. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10331. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10332. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10333. '$mod.i = 0;',
  10334. '$mod.i = rtl.length($mod.Arr) - 1;',
  10335. '$mod.DoIt($mod.Arr);',
  10336. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10337. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10338. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10339. '']));
  10340. end;
  10341. procedure TTestModule.TestArray_DynAsParam;
  10342. begin
  10343. StartProgram(false);
  10344. Add([
  10345. 'type integer = longint;',
  10346. 'type TArrInt = array of integer;',
  10347. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10348. 'var vJ: TArrInt;',
  10349. 'begin',
  10350. ' vg:=vg;',
  10351. ' vj:=vh;',
  10352. ' vi:=vi;',
  10353. ' doit(vg,vg,vg);',
  10354. ' doit(vh,vh,vj);',
  10355. ' doit(vi,vi,vi);',
  10356. ' doit(vj,vj,vj);',
  10357. 'end;',
  10358. 'var i: TArrInt;',
  10359. 'begin',
  10360. ' doit(i,i,i);']);
  10361. ConvertProgram;
  10362. CheckSource('TestArray_DynAsParams',
  10363. LinesToStr([ // statements
  10364. 'this.DoIt = function (vG,vH,vI) {',
  10365. ' var vJ = [];',
  10366. ' vG = rtl.arrayRef(vG);',
  10367. ' vJ = rtl.arrayRef(vH);',
  10368. ' vI.set(rtl.arrayRef(vI.get()));',
  10369. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10370. ' get: function () {',
  10371. ' return vG;',
  10372. ' },',
  10373. ' set: function (v) {',
  10374. ' vG = v;',
  10375. ' }',
  10376. ' });',
  10377. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10378. ' get: function () {',
  10379. ' return vJ;',
  10380. ' },',
  10381. ' set: function (v) {',
  10382. ' vJ = v;',
  10383. ' }',
  10384. ' });',
  10385. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10386. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10387. ' get: function () {',
  10388. ' return vJ;',
  10389. ' },',
  10390. ' set: function (v) {',
  10391. ' vJ = v;',
  10392. ' }',
  10393. ' });',
  10394. '};',
  10395. 'this.i = [];'
  10396. ]),
  10397. LinesToStr([
  10398. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10399. ' p: $mod,',
  10400. ' get: function () {',
  10401. ' return this.p.i;',
  10402. ' },',
  10403. ' set: function (v) {',
  10404. ' this.p.i = v;',
  10405. ' }',
  10406. '});'
  10407. ]));
  10408. end;
  10409. procedure TTestModule.TestArray_StaticAsParam;
  10410. begin
  10411. StartProgram(false);
  10412. Add([
  10413. 'type integer = longint;',
  10414. 'type TArrInt = array[1..2] of integer;',
  10415. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10416. 'var vJ: TArrInt;',
  10417. 'begin',
  10418. ' vg:=vg;',
  10419. ' vj:=vh;',
  10420. ' vi:=vi;',
  10421. ' doit(vg,vg,vg);',
  10422. ' doit(vh,vh,vj);',
  10423. ' doit(vi,vi,vi);',
  10424. ' doit(vj,vj,vj);',
  10425. 'end;',
  10426. 'var i: TArrInt;',
  10427. 'begin',
  10428. ' doit(i,i,i);']);
  10429. ConvertProgram;
  10430. CheckSource('TestArray_StaticAsParams',
  10431. LinesToStr([ // statements
  10432. 'this.DoIt = function (vG,vH,vI) {',
  10433. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10434. ' vG = vG.slice(0);',
  10435. ' vJ = vH.slice(0);',
  10436. ' vI.set(vI.get().slice(0));',
  10437. ' $mod.DoIt(vG.slice(0), vG, {',
  10438. ' get: function () {',
  10439. ' return vG;',
  10440. ' },',
  10441. ' set: function (v) {',
  10442. ' vG = v;',
  10443. ' }',
  10444. ' });',
  10445. ' $mod.DoIt(vH.slice(0), vH, {',
  10446. ' get: function () {',
  10447. ' return vJ;',
  10448. ' },',
  10449. ' set: function (v) {',
  10450. ' vJ = v;',
  10451. ' }',
  10452. ' });',
  10453. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10454. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10455. ' get: function () {',
  10456. ' return vJ;',
  10457. ' },',
  10458. ' set: function (v) {',
  10459. ' vJ = v;',
  10460. ' }',
  10461. ' });',
  10462. '};',
  10463. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10464. ]),
  10465. LinesToStr([
  10466. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10467. ' p: $mod,',
  10468. ' get: function () {',
  10469. ' return this.p.i;',
  10470. ' },',
  10471. ' set: function (v) {',
  10472. ' this.p.i = v;',
  10473. ' }',
  10474. '});'
  10475. ]));
  10476. end;
  10477. procedure TTestModule.TestArrayElement_AsParams;
  10478. begin
  10479. StartProgram(false);
  10480. Add('type integer = longint;');
  10481. Add('type TArrayInt = array of integer;');
  10482. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10483. Add('var vJ: tarrayint;');
  10484. Add('begin');
  10485. Add(' vi:=vi;');
  10486. Add(' doit(vi,vi,vi);');
  10487. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10488. Add('end;');
  10489. Add('var a: TArrayInt;');
  10490. Add('begin');
  10491. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10492. ConvertProgram;
  10493. CheckSource('TestArrayElement_AsParams',
  10494. LinesToStr([ // statements
  10495. 'this.DoIt = function (vG,vH,vI) {',
  10496. ' var vJ = [];',
  10497. ' vI.set(vI.get());',
  10498. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10499. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10500. ' a:1+3,',
  10501. ' p:vJ,',
  10502. ' get: function () {',
  10503. ' return this.p[this.a];',
  10504. ' },',
  10505. ' set: function (v) {',
  10506. ' this.p[this.a] = v;',
  10507. ' }',
  10508. ' });',
  10509. '};',
  10510. 'this.a = [];'
  10511. ]),
  10512. LinesToStr([
  10513. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10514. ' a: 1+6,',
  10515. ' p: $mod.a,',
  10516. ' get: function () {',
  10517. ' return this.p[this.a];',
  10518. ' },',
  10519. ' set: function (v) {',
  10520. ' this.p[this.a] = v;',
  10521. ' }',
  10522. '});'
  10523. ]));
  10524. end;
  10525. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10526. begin
  10527. StartProgram(false);
  10528. Add('type Integer = longint;');
  10529. Add('type TArrayInt = array of integer;');
  10530. Add('function GetArr(vB: integer = 0): tarrayint;');
  10531. Add('begin');
  10532. Add('end;');
  10533. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10534. Add('begin');
  10535. Add('end;');
  10536. Add('begin');
  10537. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10538. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10539. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10540. ConvertProgram;
  10541. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10542. LinesToStr([ // statements
  10543. 'this.GetArr = function (vB) {',
  10544. ' var Result = [];',
  10545. ' return Result;',
  10546. '};',
  10547. 'this.DoIt = function (vG,vH,vI) {',
  10548. '};'
  10549. ]),
  10550. LinesToStr([
  10551. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10552. ' a: 1+3,',
  10553. ' p: $mod.GetArr(0),',
  10554. ' get: function () {',
  10555. ' return this.p[this.a];',
  10556. ' },',
  10557. ' set: function (v) {',
  10558. ' this.p[this.a] = v;',
  10559. ' }',
  10560. '});',
  10561. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10562. ' a: 2+3,',
  10563. ' p: $mod.GetArr(0),',
  10564. ' get: function () {',
  10565. ' return this.p[this.a];',
  10566. ' },',
  10567. ' set: function (v) {',
  10568. ' this.p[this.a] = v;',
  10569. ' }',
  10570. '});',
  10571. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10572. ' a: 3+3,',
  10573. ' p: $mod.GetArr(9),',
  10574. ' get: function () {',
  10575. ' return this.p[this.a];',
  10576. ' },',
  10577. ' set: function (v) {',
  10578. ' this.p[this.a] = v;',
  10579. ' }',
  10580. '});',
  10581. '']));
  10582. end;
  10583. procedure TTestModule.TestArrayEnumTypeRange;
  10584. begin
  10585. StartProgram(false);
  10586. Add([
  10587. 'type',
  10588. ' TEnum = (red,blue);',
  10589. ' TEnumArray = array[TEnum] of longint;',
  10590. 'var',
  10591. ' e: TEnum;',
  10592. ' i: longint;',
  10593. ' a: TEnumArray;',
  10594. ' numbers: TEnumArray = (1,2);',
  10595. ' names: array[TEnum] of string = (''red'',''blue'');',
  10596. 'begin',
  10597. ' e:=low(a);',
  10598. ' e:=high(a);',
  10599. ' i:=a[red];',
  10600. ' a[e]:=a[e];']);
  10601. ConvertProgram;
  10602. CheckSource('TestArrayEnumTypeRange',
  10603. LinesToStr([ // statements
  10604. ' this.TEnum = {',
  10605. ' "0": "red",',
  10606. ' red: 0,',
  10607. ' "1": "blue",',
  10608. ' blue: 1',
  10609. '};',
  10610. 'this.e = 0;',
  10611. 'this.i = 0;',
  10612. 'this.a = rtl.arraySetLength(null,0,2);',
  10613. 'this.numbers = [1, 2];',
  10614. 'this.names = ["red", "blue"];',
  10615. '']),
  10616. LinesToStr([ // $mod.$main
  10617. '$mod.e = $mod.TEnum.red;',
  10618. '$mod.e = $mod.TEnum.blue;',
  10619. '$mod.i = $mod.a[$mod.TEnum.red];',
  10620. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10621. '']));
  10622. end;
  10623. procedure TTestModule.TestArray_SetLengthOutArg;
  10624. begin
  10625. StartProgram(false);
  10626. Add([
  10627. 'type TArrInt = array of longint;',
  10628. 'procedure DoIt(out a: TArrInt);',
  10629. 'begin',
  10630. ' SetLength(a,2);',
  10631. 'end;',
  10632. 'begin',
  10633. '']);
  10634. ConvertProgram;
  10635. CheckSource('TestArray_SetLengthOutArg',
  10636. LinesToStr([ // statements
  10637. 'this.DoIt = function (a) {',
  10638. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10639. '};',
  10640. '']),
  10641. LinesToStr([
  10642. '']));
  10643. end;
  10644. procedure TTestModule.TestArray_SetLengthProperty;
  10645. begin
  10646. StartProgram(false);
  10647. Add('type');
  10648. Add(' TArrInt = array of longint;');
  10649. Add(' TObject = class');
  10650. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10651. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10652. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10653. Add(' end;');
  10654. Add('var Obj: TObject;');
  10655. Add('begin');
  10656. Add(' SetLength(Obj.Colors,2);');
  10657. ConvertProgram;
  10658. CheckSource('TestArray_SetLengthProperty',
  10659. LinesToStr([ // statements
  10660. 'rtl.createClass(this, "TObject", null, function () {',
  10661. ' this.$init = function () {',
  10662. ' };',
  10663. ' this.$final = function () {',
  10664. ' };',
  10665. '});',
  10666. 'this.Obj = null;',
  10667. '']),
  10668. LinesToStr([
  10669. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10670. '']));
  10671. end;
  10672. procedure TTestModule.TestArray_SetLengthMultiDim;
  10673. begin
  10674. StartProgram(false);
  10675. Add([
  10676. 'type',
  10677. ' TArrArrInt = array of array of longint;',
  10678. ' TArrStaInt = array of array[1..2] of longint;',
  10679. 'var',
  10680. ' a: TArrArrInt;',
  10681. ' b: TArrStaInt;',
  10682. 'begin',
  10683. ' SetLength(a,2);',
  10684. ' SetLength(a,3,4);',
  10685. ' SetLength(b,5);',
  10686. '']);
  10687. ConvertProgram;
  10688. CheckSource('TestArray_SetLengthMultiDim',
  10689. LinesToStr([ // statements
  10690. 'this.a = [];',
  10691. 'this.b = [];',
  10692. '']),
  10693. LinesToStr([
  10694. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10695. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10696. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10697. '']));
  10698. end;
  10699. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10700. begin
  10701. StartProgram(false);
  10702. Add([
  10703. 'type',
  10704. ' TStaArr1 = array[1..3] of boolean;',
  10705. //' TStaArr2 = array[5..6] of TStaArr1;',
  10706. ' TDynArr1StaArr1 = array of TStaArr1;',
  10707. //' TDynArr1StaArr2 = array of TStaArr2;',
  10708. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10709. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10710. 'var',
  10711. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10712. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10713. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10714. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10715. 'begin',
  10716. ' SetLength(DynArr1StaArr1,11);',
  10717. ' SetLength(DynArr2StaArr1,12);',
  10718. ' SetLength(DynArr2StaArr1[13],14);',
  10719. ' SetLength(DynArr2StaArr1,15,16);',
  10720. //' SetLength(DynArr1StaArr2,21);',
  10721. //' SetLength(DynArr2StaArr2,22);',
  10722. //' SetLength(DynArr2StaArr2[23],24);',
  10723. //' SetLength(DynArr2StaArr2,25,26);',
  10724. '']);
  10725. ConvertProgram;
  10726. CheckSource('TestArray_DynOfStatic',
  10727. LinesToStr([ // statements
  10728. 'this.DynArr1StaArr1 = [];',
  10729. 'this.DynArr2StaArr1 = [];',
  10730. '']),
  10731. LinesToStr([ // $mod.$main
  10732. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10733. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10734. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10735. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10736. ' $mod.DynArr2StaArr1,',
  10737. ' false,',
  10738. ' 15,',
  10739. ' 16,',
  10740. ' "s",',
  10741. ' 3',
  10742. ');',
  10743. '']));
  10744. end;
  10745. procedure TTestModule.TestArray_OpenArrayOfString;
  10746. begin
  10747. StartProgram(false);
  10748. Add('procedure DoIt(const a: array of String);');
  10749. Add('var');
  10750. Add(' i: longint;');
  10751. Add(' s: string;');
  10752. Add('begin');
  10753. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10754. Add('end;');
  10755. Add('var s: string;');
  10756. Add('begin');
  10757. Add(' DoIt([]);');
  10758. Add(' DoIt([s,''foo'','''',s+s]);');
  10759. ConvertProgram;
  10760. CheckSource('TestArray_OpenArrayOfString',
  10761. LinesToStr([ // statements
  10762. 'this.DoIt = function (a) {',
  10763. ' var i = 0;',
  10764. ' var s = "";',
  10765. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10766. ' i = $l;',
  10767. ' s = a[rtl.length(a) - i - 1];',
  10768. ' };',
  10769. '};',
  10770. 'this.s = "";',
  10771. '']),
  10772. LinesToStr([
  10773. '$mod.DoIt([]);',
  10774. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10775. '']));
  10776. end;
  10777. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10778. begin
  10779. StartProgram(false);
  10780. Add([
  10781. 'type TArr = array of char;',
  10782. 'var',
  10783. ' c: char;',
  10784. ' s: string;',
  10785. ' a: TArr;',
  10786. 'procedure Run(const a: array of char);',
  10787. 'begin',
  10788. ' Run(c);',
  10789. ' Run(s);',
  10790. 'end;',
  10791. 'begin',
  10792. ' a:=c;',
  10793. ' a:=s;',
  10794. ' a:=#13;',
  10795. ' a:=''Foo'';',
  10796. ' Run(c);',
  10797. ' Run(s);',
  10798. '']);
  10799. ConvertProgram;
  10800. CheckSource('TestArray_ArrayOfCharAssignString',
  10801. LinesToStr([ // statements
  10802. 'this.c = "";',
  10803. 'this.s = "";',
  10804. 'this.a = [];',
  10805. 'this.Run = function (a) {',
  10806. ' $mod.Run($mod.c.split(""));',
  10807. ' $mod.Run($mod.s.split(""));',
  10808. '};',
  10809. '']),
  10810. LinesToStr([
  10811. '$mod.a = $mod.c.split("");',
  10812. '$mod.a = $mod.s.split("");',
  10813. '$mod.a = "\r".split("");',
  10814. '$mod.a = "Foo".split("");',
  10815. '$mod.Run($mod.c.split(""));',
  10816. '$mod.Run($mod.s.split(""));',
  10817. '']));
  10818. end;
  10819. procedure TTestModule.TestArray_ConstRef;
  10820. begin
  10821. StartProgram(false);
  10822. Add([
  10823. 'type TArr = array of word;',
  10824. 'procedure Run(constref a: TArr);',
  10825. 'begin',
  10826. 'end;',
  10827. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10828. 'var l: TArr;',
  10829. 'begin',
  10830. ' Run(l);',
  10831. ' Run(a);',
  10832. ' Run(b);',
  10833. ' Run(c);',
  10834. ' Run(d);',
  10835. ' Run(e);',
  10836. 'end;',
  10837. 'begin',
  10838. '']);
  10839. ConvertProgram;
  10840. CheckResolverUnexpectedHints();
  10841. CheckSource('TestArray_ConstRef',
  10842. LinesToStr([ // statements
  10843. 'this.Run = function (a) {',
  10844. '};',
  10845. 'this.Fly = function (a, b, c, d, e) {',
  10846. ' var l = [];',
  10847. ' $mod.Run(l);',
  10848. ' $mod.Run(a);',
  10849. ' $mod.Run(b.get());',
  10850. ' $mod.Run(c.get());',
  10851. ' $mod.Run(d);',
  10852. ' $mod.Run(e);',
  10853. '};',
  10854. '']),
  10855. LinesToStr([
  10856. '']));
  10857. end;
  10858. procedure TTestModule.TestArray_Concat;
  10859. begin
  10860. StartProgram(false);
  10861. Add([
  10862. 'type',
  10863. ' integer = longint;',
  10864. ' TFlag = (big,small);',
  10865. ' TFlags = set of TFlag;',
  10866. ' TRec = record',
  10867. ' i: integer;',
  10868. ' end;',
  10869. ' TArrInt = array of integer;',
  10870. ' TArrRec = array of TRec;',
  10871. ' TArrFlag = array of TFlag;',
  10872. ' TArrSet = array of TFlags;',
  10873. ' TArrJSValue = array of jsvalue;',
  10874. 'var',
  10875. ' ArrInt1, ArrInt2: tarrint;',
  10876. ' ArrRec1, ArrRec2: tarrrec;',
  10877. ' ArrFlag1, ArrFlag2: tarrflag;',
  10878. ' ArrSet1, ArrSet2: tarrset;',
  10879. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10880. 'begin',
  10881. ' arrint1:=concat(arrint2);',
  10882. ' arrint1:=concat(arrint2,arrint2);',
  10883. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10884. ' arrrec1:=concat(arrrec2);',
  10885. ' arrrec1:=concat(arrrec2,arrrec2);',
  10886. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10887. ' arrset1:=concat(arrset2);',
  10888. ' arrset1:=concat(arrset2,arrset2);',
  10889. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10890. ' arrjsvalue1:=concat(arrjsvalue2);',
  10891. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10892. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10893. ' arrint1:=concat([1],arrint2);',
  10894. ' arrflag1:=concat([big]);',
  10895. ' arrflag1:=concat([big],arrflag2);',
  10896. ' arrflag1:=concat(arrflag2,[small]);',
  10897. '']);
  10898. ConvertProgram;
  10899. CheckSource('TestArray_Concat',
  10900. LinesToStr([ // statements
  10901. 'this.TFlag = {',
  10902. ' "0": "big",',
  10903. ' big: 0,',
  10904. ' "1": "small",',
  10905. ' small: 1',
  10906. '};',
  10907. 'rtl.recNewT(this, "TRec", function () {',
  10908. ' this.i = 0;',
  10909. ' this.$eq = function (b) {',
  10910. ' return this.i === b.i;',
  10911. ' };',
  10912. ' this.$assign = function (s) {',
  10913. ' this.i = s.i;',
  10914. ' return this;',
  10915. ' };',
  10916. '});',
  10917. 'this.ArrInt1 = [];',
  10918. 'this.ArrInt2 = [];',
  10919. 'this.ArrRec1 = [];',
  10920. 'this.ArrRec2 = [];',
  10921. 'this.ArrFlag1 = [];',
  10922. 'this.ArrFlag2 = [];',
  10923. 'this.ArrSet1 = [];',
  10924. 'this.ArrSet2 = [];',
  10925. 'this.ArrJSValue1 = [];',
  10926. 'this.ArrJSValue2 = [];',
  10927. '']),
  10928. LinesToStr([ // $mod.$main
  10929. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  10930. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  10931. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  10932. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  10933. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  10934. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  10935. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  10936. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  10937. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  10938. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  10939. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  10940. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  10941. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  10942. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  10943. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  10944. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  10945. '']));
  10946. end;
  10947. procedure TTestModule.TestArray_Concat_Append;
  10948. begin
  10949. StartProgram(false);
  10950. Add([
  10951. 'type',
  10952. ' integer = longint;',
  10953. ' TFlag = (big,small);',
  10954. ' TFlags = set of TFlag;',
  10955. ' TRec = record',
  10956. ' i: integer;',
  10957. ' end;',
  10958. ' TArrInt = array of integer;',
  10959. ' TArrRec = array of TRec;',
  10960. ' TArrFlag = array of TFlag;',
  10961. ' TArrSet = array of TFlags;',
  10962. ' TArrJSValue = array of jsvalue;',
  10963. 'var',
  10964. ' ArrInt: tarrint;',
  10965. ' ArrRec: tarrrec;',
  10966. ' ArrFlag: tarrflag;',
  10967. ' ArrSet: tarrset;',
  10968. ' ArrJSValue: tarrjsvalue;',
  10969. ' r: TRec;',
  10970. ' f: TFlags;',
  10971. 'begin',
  10972. ' // append',
  10973. ' arrint:=concat(arrint);',
  10974. ' arrint:=concat(arrint,[2]);',
  10975. ' arrint:=concat(arrint,[3,4]);',
  10976. ' arrrec:=concat(arrrec);',
  10977. ' arrrec:=concat(arrrec,[r]);',
  10978. ' arrrec:=concat(arrrec,[r,r]);',
  10979. ' arrset:=concat(arrset);',
  10980. ' arrset:=concat(arrset,[f]);',
  10981. ' arrset:=concat(arrset,[f,f]);',
  10982. ' arrjsvalue:=concat(arrjsvalue);',
  10983. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  10984. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  10985. ' arrflag:=concat(arrflag);',
  10986. ' arrflag:=concat(arrflag,[small]);',
  10987. ' arrflag:=concat(arrflag,[small,big]);',
  10988. '']);
  10989. ConvertProgram;
  10990. CheckSource('TestArray_Concat_Append',
  10991. LinesToStr([ // statements
  10992. 'this.TFlag = {',
  10993. ' "0": "big",',
  10994. ' big: 0,',
  10995. ' "1": "small",',
  10996. ' small: 1',
  10997. '};',
  10998. 'rtl.recNewT(this, "TRec", function () {',
  10999. ' this.i = 0;',
  11000. ' this.$eq = function (b) {',
  11001. ' return this.i === b.i;',
  11002. ' };',
  11003. ' this.$assign = function (s) {',
  11004. ' this.i = s.i;',
  11005. ' return this;',
  11006. ' };',
  11007. '});',
  11008. 'this.ArrInt = [];',
  11009. 'this.ArrRec = [];',
  11010. 'this.ArrFlag = [];',
  11011. 'this.ArrSet = [];',
  11012. 'this.ArrJSValue = [];',
  11013. 'this.r = this.TRec.$new();',
  11014. 'this.f = {};',
  11015. '']),
  11016. LinesToStr([ // $mod.$main
  11017. '$mod.ArrInt = $mod.ArrInt;',
  11018. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11019. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11020. '$mod.ArrRec = $mod.ArrRec;',
  11021. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11022. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11023. '$mod.ArrSet = $mod.ArrSet;',
  11024. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11025. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11026. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11027. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11028. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11029. '$mod.ArrFlag = $mod.ArrFlag;',
  11030. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11031. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11032. '']));
  11033. end;
  11034. procedure TTestModule.TestArray_Concat_Append_Var;
  11035. begin
  11036. StartProgram(false);
  11037. Add([
  11038. 'type',
  11039. ' TArrInt = array of word;',
  11040. '',
  11041. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11042. 'begin',
  11043. ' a:=concat(a,[2]);',
  11044. ' b:=concat(b,[2]);',
  11045. 'end;',
  11046. 'var',
  11047. ' ArrInt: tarrint;',
  11048. 'begin',
  11049. ' Fly(ArrInt,ArrInt);',
  11050. '']);
  11051. ConvertProgram;
  11052. CheckSource('TestArray_Concat_Append_Var',
  11053. LinesToStr([ // statements
  11054. 'this.Fly = function (a, b) {',
  11055. ' a = rtl.arrayPushN(a, 2);',
  11056. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11057. '};',
  11058. 'this.ArrInt = [];',
  11059. '']),
  11060. LinesToStr([ // $mod.$main
  11061. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11062. ' p: $mod,',
  11063. ' get: function () {',
  11064. ' return this.p.ArrInt;',
  11065. ' },',
  11066. ' set: function (v) {',
  11067. ' this.p.ArrInt = v;',
  11068. ' }',
  11069. '});',
  11070. '']));
  11071. end;
  11072. procedure TTestModule.TestArray_Copy;
  11073. begin
  11074. StartProgram(false);
  11075. Add([
  11076. 'type',
  11077. ' integer = longint;',
  11078. ' TFlag = (big,small);',
  11079. ' TFlags = set of TFlag;',
  11080. ' TRec = record',
  11081. ' i: integer;',
  11082. ' end;',
  11083. ' TArrInt = array of integer;',
  11084. ' TArrRec = array of TRec;',
  11085. ' TArrSet = array of TFlags;',
  11086. ' TArrJSValue = array of jsvalue;',
  11087. 'var',
  11088. ' ArrInt: tarrint;',
  11089. ' ArrRec: tarrrec;',
  11090. ' ArrSet: tarrset;',
  11091. ' ArrJSValue: tarrjsvalue;',
  11092. 'begin',
  11093. ' arrint:=copy(arrint);',
  11094. ' arrint:=copy(arrint,2);',
  11095. ' arrint:=copy(arrint,3,4);',
  11096. ' arrint:=copy([1,1],1,2);',
  11097. ' arrrec:=copy(arrrec);',
  11098. ' arrrec:=copy(arrrec,5);',
  11099. ' arrrec:=copy(arrrec,6,7);',
  11100. ' arrset:=copy(arrset);',
  11101. ' arrset:=copy(arrset,8);',
  11102. ' arrset:=copy(arrset,9,10);',
  11103. ' arrjsvalue:=copy(arrjsvalue);',
  11104. ' arrjsvalue:=copy(arrjsvalue,11);',
  11105. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11106. ' ']);
  11107. ConvertProgram;
  11108. CheckSource('TestArray_Copy',
  11109. LinesToStr([ // statements
  11110. 'this.TFlag = {',
  11111. ' "0": "big",',
  11112. ' big: 0,',
  11113. ' "1": "small",',
  11114. ' small: 1',
  11115. '};',
  11116. 'rtl.recNewT(this, "TRec", function () {',
  11117. ' this.i = 0;',
  11118. ' this.$eq = function (b) {',
  11119. ' return this.i === b.i;',
  11120. ' };',
  11121. ' this.$assign = function (s) {',
  11122. ' this.i = s.i;',
  11123. ' return this;',
  11124. ' };',
  11125. '});',
  11126. 'this.ArrInt = [];',
  11127. 'this.ArrRec = [];',
  11128. 'this.ArrSet = [];',
  11129. 'this.ArrJSValue = [];',
  11130. '']),
  11131. LinesToStr([ // $mod.$main
  11132. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11133. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11134. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11135. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11136. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11137. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11138. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11139. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11140. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11141. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11142. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11143. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11144. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11145. '']));
  11146. end;
  11147. procedure TTestModule.TestArray_InsertDelete;
  11148. begin
  11149. StartProgram(false);
  11150. Add([
  11151. 'type',
  11152. ' integer = longint;',
  11153. ' TFlag = (big,small);',
  11154. ' TFlags = set of TFlag;',
  11155. ' TRec = record',
  11156. ' i: integer;',
  11157. ' end;',
  11158. ' TArrInt = array of integer;',
  11159. ' TArrRec = array of TRec;',
  11160. ' TArrSet = array of TFlags;',
  11161. ' TArrJSValue = array of jsvalue;',
  11162. ' TArrArrInt = array of TArrInt;',
  11163. 'var',
  11164. ' ArrInt: tarrint;',
  11165. ' ArrRec: tarrrec;',
  11166. ' ArrSet: tarrset;',
  11167. ' ArrJSValue: tarrjsvalue;',
  11168. ' ArrArrInt: TArrArrInt;',
  11169. 'begin',
  11170. ' Insert(1,arrint,2);',
  11171. ' Insert(arrint[3],arrint,4);',
  11172. ' Insert(arrrec[5],arrrec,6);',
  11173. ' Insert(arrset[7],arrset,7);',
  11174. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11175. ' Insert(10,arrjsvalue,11);',
  11176. ' Insert([23],arrarrint,22);',
  11177. ' Delete(arrint,12,13);',
  11178. ' Delete(arrrec,14,15);',
  11179. ' Delete(arrset,17,18);',
  11180. ' Delete(arrjsvalue,19,10);']);
  11181. ConvertProgram;
  11182. CheckSource('TestArray_InsertDelete',
  11183. LinesToStr([ // statements
  11184. 'this.TFlag = {',
  11185. ' "0": "big",',
  11186. ' big: 0,',
  11187. ' "1": "small",',
  11188. ' small: 1',
  11189. '};',
  11190. 'rtl.recNewT(this, "TRec", function () {',
  11191. ' this.i = 0;',
  11192. ' this.$eq = function (b) {',
  11193. ' return this.i === b.i;',
  11194. ' };',
  11195. ' this.$assign = function (s) {',
  11196. ' this.i = s.i;',
  11197. ' return this;',
  11198. ' };',
  11199. '});',
  11200. 'this.ArrInt = [];',
  11201. 'this.ArrRec = [];',
  11202. 'this.ArrSet = [];',
  11203. 'this.ArrJSValue = [];',
  11204. 'this.ArrArrInt = [];',
  11205. '']),
  11206. LinesToStr([ // $mod.$main
  11207. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11208. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11209. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11210. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11211. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11212. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11213. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11214. '$mod.ArrInt.splice(12, 13);',
  11215. '$mod.ArrRec.splice(14, 15);',
  11216. '$mod.ArrSet.splice(17, 18);',
  11217. '$mod.ArrJSValue.splice(19, 10);',
  11218. '']));
  11219. end;
  11220. procedure TTestModule.TestArray_Add_Append;
  11221. begin
  11222. StartProgram(false);
  11223. Add([
  11224. '{$modeswitch arrayoperators}',
  11225. 'type',
  11226. ' integer = longint;',
  11227. ' TFlag = (big,small);',
  11228. ' TFlags = set of TFlag;',
  11229. ' TRec = record',
  11230. ' i: integer;',
  11231. ' end;',
  11232. ' TArrInt = array of integer;',
  11233. ' TArrRec = array of TRec;',
  11234. ' TArrFlag = array of TFlag;',
  11235. ' TArrSet = array of TFlags;',
  11236. ' TArrJSValue = array of jsvalue;',
  11237. 'var',
  11238. ' ArrInt: tarrint;',
  11239. ' ArrRec: tarrrec;',
  11240. ' ArrFlag: tarrflag;',
  11241. ' ArrSet: tarrset;',
  11242. ' ArrJSValue: tarrjsvalue;',
  11243. ' r: TRec;',
  11244. ' f: TFlags;',
  11245. 'begin',
  11246. ' // append',
  11247. ' arrint:=arrint+[2];',
  11248. ' arrint:=arrint+[3,4];',
  11249. ' arrrec:=arrrec+[r];',
  11250. ' arrrec:=arrrec+[r,r];',
  11251. ' arrset:=arrset+[f];',
  11252. ' arrset:=arrset+[f,f];',
  11253. ' arrjsvalue:=arrjsvalue+[11];',
  11254. ' arrjsvalue:=arrjsvalue+[12,13];',
  11255. ' arrflag:=arrflag+[small];',
  11256. ' arrflag:=arrflag+[small,big];',
  11257. '']);
  11258. ConvertProgram;
  11259. CheckSource('TestArray_Add_Append',
  11260. LinesToStr([ // statements
  11261. 'this.TFlag = {',
  11262. ' "0": "big",',
  11263. ' big: 0,',
  11264. ' "1": "small",',
  11265. ' small: 1',
  11266. '};',
  11267. 'rtl.recNewT(this, "TRec", function () {',
  11268. ' this.i = 0;',
  11269. ' this.$eq = function (b) {',
  11270. ' return this.i === b.i;',
  11271. ' };',
  11272. ' this.$assign = function (s) {',
  11273. ' this.i = s.i;',
  11274. ' return this;',
  11275. ' };',
  11276. '});',
  11277. 'this.ArrInt = [];',
  11278. 'this.ArrRec = [];',
  11279. 'this.ArrFlag = [];',
  11280. 'this.ArrSet = [];',
  11281. 'this.ArrJSValue = [];',
  11282. 'this.r = this.TRec.$new();',
  11283. 'this.f = {};',
  11284. '']),
  11285. LinesToStr([ // $mod.$main
  11286. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11287. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11288. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11289. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11290. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11291. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11292. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11293. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11294. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11295. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11296. '']));
  11297. end;
  11298. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11299. begin
  11300. Parser.Options:=Parser.Options+[po_cassignments];
  11301. StartProgram(false);
  11302. Add([
  11303. '{$modeswitch arrayoperators}',
  11304. 'type',
  11305. ' integer = longint;',
  11306. ' TArrInt = array of integer;',
  11307. ' TArrStr = array of string;',
  11308. 'const',
  11309. ' Ints: TArrInt = (1,2,3);',
  11310. ' Aliases: TarrStr = (''foo'',''b'');',
  11311. ' OneInt: TArrInt = (7);',
  11312. ' OneStr: array of integer = (7);',
  11313. ' Chars: array of char = ''aoc'';',
  11314. ' Names: array of string = (''a'',''foo'');',
  11315. ' NameCount = low(Names)+high(Names)+length(Names);',
  11316. 'var i: integer;',
  11317. 'begin',
  11318. ' Ints:=[];',
  11319. ' Ints:=[1,1];',
  11320. ' Ints:=[1]+[2];',
  11321. ' Ints:=[2];',
  11322. ' Ints:=[]+ints;',
  11323. ' Ints:=Ints+[];',
  11324. ' Ints:=Ints+OneInt;',
  11325. ' Ints:=Ints+[1,1];',
  11326. ' Ints:=[i,i]+Ints;',
  11327. ' Ints:=[1]+[i]+[3];',
  11328. '']);
  11329. ConvertProgram;
  11330. CheckSource('TestArray_DynArrayConstObjFPC',
  11331. LinesToStr([ // statements
  11332. 'this.Ints = [1, 2, 3];',
  11333. 'this.Aliases = ["foo", "b"];',
  11334. 'this.OneInt = [7];',
  11335. 'this.OneStr = [7];',
  11336. 'this.Chars = ["a", "o", "c"];',
  11337. 'this.Names = ["a", "foo"];',
  11338. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11339. 'this.i = 0;',
  11340. '']),
  11341. LinesToStr([ // $mod.$main
  11342. '$mod.Ints = [];',
  11343. '$mod.Ints = [1, 1];',
  11344. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11345. '$mod.Ints = [2];',
  11346. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11347. '$mod.Ints = $mod.Ints;',
  11348. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11349. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11350. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11351. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11352. '']));
  11353. end;
  11354. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11355. begin
  11356. StartProgram(false);
  11357. // Note: const c = [1,1]; defines a set!
  11358. Add([
  11359. '{$mode delphi}',
  11360. 'type',
  11361. ' integer = longint;',
  11362. ' TArrInt = array of integer;',
  11363. ' TArrStr = array of string;',
  11364. 'const',
  11365. ' Ints: TArrInt = [1,1,2];',
  11366. ' Aliases: TarrStr = [''foo'',''b''];',
  11367. ' OneInt: TArrInt = [7];',
  11368. ' OneStr: array of integer = [7]+[8];',
  11369. ' Chars: array of char = ''aoc'';',
  11370. ' Names: array of string = [''a'',''a''];',
  11371. ' NameCount = low(Names)+high(Names)+length(Names);',
  11372. 'begin',
  11373. '']);
  11374. ConvertProgram;
  11375. CheckSource('TestArray_DynArrayConstDelphi',
  11376. LinesToStr([ // statements
  11377. 'this.Ints = [1, 1, 2];',
  11378. 'this.Aliases = ["foo", "b"];',
  11379. 'this.OneInt = [7];',
  11380. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11381. 'this.Chars = ["a", "o", "c"];',
  11382. 'this.Names = ["a", "a"];',
  11383. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11384. '']),
  11385. LinesToStr([ // $mod.$main
  11386. '']));
  11387. end;
  11388. procedure TTestModule.TestArray_ArrayLitAsParam;
  11389. begin
  11390. StartProgram(false);
  11391. Add([
  11392. '{$modeswitch arrayoperators}',
  11393. 'type',
  11394. ' integer = longint;',
  11395. ' TArrInt = array of integer;',
  11396. ' TArrSet = array of (red,green,blue);',
  11397. 'procedure DoOpenInt(const a: array of integer); forward;',
  11398. 'procedure DoInt(const a: TArrInt);',
  11399. 'begin',
  11400. ' DoInt(a+[1]);',
  11401. ' DoInt([1]+a);',
  11402. ' DoOpenInt(a);',
  11403. ' DoOpenInt(a+[1]);',
  11404. ' DoOpenInt([1]+a);',
  11405. 'end;',
  11406. 'procedure DoOpenInt(const a: array of integer);',
  11407. 'begin',
  11408. ' DoOpenInt(a+[1]);',
  11409. ' DoOpenInt([1]+a);',
  11410. ' DoInt(a);',
  11411. ' DoInt(a+[1]);',
  11412. ' DoInt([1]+a);',
  11413. 'end;',
  11414. 'procedure DoSet(const a: TArrSet);',
  11415. 'begin',
  11416. ' DoSet(a+[red]);',
  11417. ' DoSet([blue]+a);',
  11418. 'end;',
  11419. 'var',
  11420. ' i: TArrInt;',
  11421. ' s: TArrSet;',
  11422. 'begin',
  11423. ' DoInt([1]);',
  11424. ' DoInt([1]+[2]);',
  11425. ' DoInt(i+[1]);',
  11426. ' DoInt([1]+i);',
  11427. ' DoOpenInt([1]);',
  11428. ' DoOpenInt([1]+[2]);',
  11429. ' DoOpenInt(i+[1]);',
  11430. ' DoOpenInt([1]+i);',
  11431. ' DoSet([red]);',
  11432. ' DoSet([blue]+[green]);',
  11433. ' DoSet(s+[blue]);',
  11434. ' DoSet([red]+s);',
  11435. '']);
  11436. ConvertProgram;
  11437. CheckSource('TestArray_ArrayLitAsParam',
  11438. LinesToStr([ // statements
  11439. 'this.TArrSet$a = {',
  11440. ' "0": "red",',
  11441. ' red: 0,',
  11442. ' "1": "green",',
  11443. ' green: 1,',
  11444. ' "2": "blue",',
  11445. ' blue: 2',
  11446. '};',
  11447. 'this.DoInt = function (a) {',
  11448. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11449. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11450. ' $mod.DoOpenInt(a);',
  11451. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11452. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11453. '};',
  11454. 'this.DoOpenInt = function (a) {',
  11455. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11456. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11457. ' $mod.DoInt(a);',
  11458. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11459. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11460. '};',
  11461. 'this.DoSet = function (a) {',
  11462. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11463. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11464. '};',
  11465. 'this.i = [];',
  11466. 'this.s = [];',
  11467. '']),
  11468. LinesToStr([ // $mod.$main
  11469. '$mod.DoInt([1]);',
  11470. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11471. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11472. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11473. '$mod.DoOpenInt([1]);',
  11474. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11475. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11476. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11477. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11478. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11479. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11480. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11481. '']));
  11482. end;
  11483. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11484. begin
  11485. StartProgram(false);
  11486. Add([
  11487. '{$modeswitch arrayoperators}',
  11488. 'type',
  11489. ' integer = longint;',
  11490. ' TArrInt = array of integer;',
  11491. ' TArrArrInt = array of TArrInt;',
  11492. 'procedure DoInt(const a: TArrArrInt);',
  11493. 'begin',
  11494. ' DoInt(a+[[1]]);',
  11495. ' DoInt([[1]]+a);',
  11496. ' DoInt(a);',
  11497. 'end;',
  11498. 'var',
  11499. ' i: TArrInt;',
  11500. ' a: TArrArrInt;',
  11501. 'begin',
  11502. ' a:=[[1]];',
  11503. ' a:=[i];',
  11504. ' a:=a+[i];',
  11505. ' a:=[i]+a;',
  11506. ' a:=[[1]+i];',
  11507. ' a:=[[1]+[2]];',
  11508. ' a:=[i+[2]];',
  11509. ' DoInt([[1]]);',
  11510. ' DoInt([[1]+[2],[3,4],[5]]);',
  11511. ' DoInt([i+[1]]+a);',
  11512. ' DoInt([i]+a);',
  11513. '']);
  11514. ConvertProgram;
  11515. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11516. LinesToStr([ // statements
  11517. 'this.DoInt = function (a) {',
  11518. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11519. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11520. ' $mod.DoInt(a);',
  11521. '};',
  11522. 'this.i = [];',
  11523. 'this.a = [];',
  11524. '']),
  11525. LinesToStr([ // $mod.$main
  11526. '$mod.a = [[1]];',
  11527. '$mod.a = [$mod.i];',
  11528. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11529. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11530. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11531. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11532. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11533. '$mod.DoInt([[1]]);',
  11534. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11535. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11536. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11537. '']));
  11538. end;
  11539. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11540. begin
  11541. StartProgram(false);
  11542. Add([
  11543. '{$modeswitch arrayoperators}',
  11544. 'type',
  11545. ' integer = longint;',
  11546. ' TArrInt = array[1..2] of integer;',
  11547. ' TArrArrInt = array of TArrInt;',
  11548. 'procedure DoInt(const a: TArrArrInt);',
  11549. 'begin',
  11550. ' DoInt(a+[[1,2]]);',
  11551. ' DoInt([[1,2]]+a);',
  11552. ' DoInt(a);',
  11553. 'end;',
  11554. 'var',
  11555. ' i: TArrInt;',
  11556. ' a: TArrArrInt;',
  11557. 'begin',
  11558. ' a:=[[1,1]];',
  11559. ' a:=[i];',
  11560. ' a:=a+[i];',
  11561. ' a:=[i]+a;',
  11562. ' DoInt([[1,1]]);',
  11563. ' DoInt([[1,2],[3,4]]);',
  11564. '']);
  11565. ConvertProgram;
  11566. CheckSource('TestArray_ArrayLitStaticAsParam',
  11567. LinesToStr([ // statements
  11568. 'this.DoInt = function (a) {',
  11569. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11570. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11571. ' $mod.DoInt(a);',
  11572. '};',
  11573. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11574. 'this.a = [];',
  11575. '']),
  11576. LinesToStr([ // $mod.$main
  11577. '$mod.a = [[1, 1]];',
  11578. '$mod.a = [$mod.i.slice(0)];',
  11579. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11580. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11581. '$mod.DoInt([[1, 1]]);',
  11582. '$mod.DoInt([[1, 2], [3, 4]]);',
  11583. '']));
  11584. end;
  11585. procedure TTestModule.TestArray_ForInArrOfString;
  11586. begin
  11587. StartProgram(false);
  11588. Add([
  11589. 'type',
  11590. 'type',
  11591. ' TMonthNameArray = array [1..12] of string;',
  11592. ' TMonthNames = TMonthNameArray;',
  11593. ' TObject = class',
  11594. ' private',
  11595. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11596. ' public',
  11597. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11598. ' end;',
  11599. 'var',
  11600. ' f: TObject;',
  11601. ' Month: string;',
  11602. ' Names: array of string = (''a'',''foo'',''bar'');',
  11603. ' i: longint;',
  11604. 'begin',
  11605. ' for Month in f.LongMonthNames do ;',
  11606. ' for Month in Names do ;',
  11607. ' for i:=low(Names) to high(Names) do ;',
  11608. '']);
  11609. ConvertProgram;
  11610. CheckSource('TestArray_ForInArrOfString',
  11611. LinesToStr([ // statements
  11612. 'rtl.createClass(this, "TObject", null, function () {',
  11613. ' this.$init = function () {',
  11614. ' };',
  11615. ' this.$final = function () {',
  11616. ' };',
  11617. '});',
  11618. 'this.f = null;',
  11619. 'this.Month = "";',
  11620. 'this.Names = ["a", "foo", "bar"];',
  11621. 'this.i = 0;',
  11622. '']),
  11623. LinesToStr([ // $mod.$main
  11624. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11625. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11626. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11627. '']));
  11628. end;
  11629. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11630. begin
  11631. StartProgram(false);
  11632. Add([
  11633. '{$modeswitch externalclass}',
  11634. 'type',
  11635. ' TJSObject = class external name ''Object''',
  11636. ' end;',
  11637. ' TJSArray = class external name ''Array''',
  11638. ' class function isArray(Value: JSValue) : boolean;',
  11639. ' function concat() : TJSArray; varargs;',
  11640. ' end;',
  11641. 'var',
  11642. ' aObj: TJSArray;',
  11643. ' a: array of longint;',
  11644. ' o: TJSObject;',
  11645. 'begin',
  11646. ' if TJSArray.isArray(65) then ;',
  11647. ' aObj:=TJSArray(a).concat(a);',
  11648. ' o:=TJSObject(a);',
  11649. ' aObj:=TJSArray([''bird'',''ant'']);',
  11650. '']);
  11651. ConvertProgram;
  11652. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11653. LinesToStr([ // statements
  11654. 'this.aObj = null;',
  11655. 'this.a = [];',
  11656. 'this.o = null;',
  11657. '']),
  11658. LinesToStr([ // $mod.$main
  11659. 'if (Array.isArray(65)) ;',
  11660. '$mod.aObj = $mod.a.concat($mod.a);',
  11661. '$mod.o = $mod.a;',
  11662. '$mod.aObj = ["bird", "ant"];',
  11663. '']));
  11664. end;
  11665. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11666. begin
  11667. StartProgram(false);
  11668. Add([
  11669. '{$modeswitch externalclass}',
  11670. 'type',
  11671. ' TArrStr = array of string;',
  11672. ' TJSArray = class external name ''Array''',
  11673. ' end;',
  11674. ' TJSObject = class external name ''Object''',
  11675. ' end;',
  11676. 'var',
  11677. ' aObj: TJSArray;',
  11678. ' a: TArrStr;',
  11679. ' jo: TJSObject;',
  11680. 'begin',
  11681. ' a:=TArrStr(aObj);',
  11682. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11683. ' a:=TarrStr(jo);',
  11684. '']);
  11685. ConvertProgram;
  11686. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11687. LinesToStr([ // statements
  11688. 'this.aObj = null;',
  11689. 'this.a = [];',
  11690. 'this.jo = null;',
  11691. '']),
  11692. LinesToStr([ // $mod.$main
  11693. '$mod.a = $mod.aObj;',
  11694. '$mod.aObj[1] = $mod.aObj[2];',
  11695. '$mod.a = $mod.jo;',
  11696. '']));
  11697. end;
  11698. procedure TTestModule.TestArrayOfConst_TVarRec;
  11699. begin
  11700. StartProgram(true,[supTVarRec]);
  11701. Add([
  11702. 'procedure Say(args: array of const);',
  11703. 'var',
  11704. ' i: longint;',
  11705. ' v: TVarRec;',
  11706. 'begin',
  11707. ' for i:=low(args) to high(args) do begin',
  11708. ' v:=args[i];',
  11709. ' case v.vtype of',
  11710. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11711. ' end;',
  11712. ' end;',
  11713. ' for v in args do ;',
  11714. ' args:=nil;',
  11715. ' SetLength(args,2);',
  11716. 'end;',
  11717. 'begin']);
  11718. ConvertProgram;
  11719. CheckSource('TestArrayOfConst_TVarRec',
  11720. LinesToStr([ // statements
  11721. 'this.Say = function (args) {',
  11722. ' var i = 0;',
  11723. ' var v = pas.system.TVarRec.$new();',
  11724. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11725. ' i = $l;',
  11726. ' v.$assign(args[i]);',
  11727. ' var $tmp = v.VType;',
  11728. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11729. ' };',
  11730. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11731. ' args = [];',
  11732. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11733. '};',
  11734. '']),
  11735. LinesToStr([ // $mod.$main
  11736. ]));
  11737. end;
  11738. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11739. begin
  11740. StartProgram(true,[supTVarRec]);
  11741. Add([
  11742. 'procedure Say(args: array of const);',
  11743. 'begin',
  11744. ' Say(args);',
  11745. 'end;',
  11746. 'var',
  11747. ' p: Pointer;',
  11748. ' j: jsvalue;',
  11749. ' c: currency;',
  11750. 'begin',
  11751. ' Say([]);',
  11752. ' Say([1]);',
  11753. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11754. '']);
  11755. ConvertProgram;
  11756. CheckSource('TestArrayOfConst_PassBaseTypes',
  11757. LinesToStr([ // statements
  11758. 'this.Say = function (args) {',
  11759. ' $mod.Say(args);',
  11760. '};',
  11761. 'this.p = null;',
  11762. 'this.j = undefined;',
  11763. 'this.c = 0;',
  11764. '']),
  11765. LinesToStr([ // $mod.$main
  11766. '$mod.Say([]);',
  11767. '$mod.Say(pas.system.VarRecs(0, 1));',
  11768. '$mod.Say(pas.system.VarRecs(',
  11769. ' 9,',
  11770. ' "c",',
  11771. ' 18,',
  11772. ' "foo",',
  11773. ' 5,',
  11774. ' null,',
  11775. ' 1,',
  11776. ' true,',
  11777. ' 3,',
  11778. ' 1.3,',
  11779. ' 5,',
  11780. ' $mod.p,',
  11781. ' 20,',
  11782. ' $mod.j,',
  11783. ' 12,',
  11784. ' $mod.c',
  11785. ' ));',
  11786. '']));
  11787. end;
  11788. procedure TTestModule.TestArrayOfConst_PassObj;
  11789. begin
  11790. StartProgram(true,[supTVarRec]);
  11791. Add([
  11792. '{$interfaces corba}',
  11793. 'type',
  11794. ' TObject = class',
  11795. ' end;',
  11796. ' TClass = class of TObject;',
  11797. ' IUnknown = interface',
  11798. ' end;',
  11799. 'procedure Say(args: array of const);',
  11800. 'begin',
  11801. 'end;',
  11802. 'var',
  11803. ' o: TObject;',
  11804. ' c: TClass;',
  11805. ' i: IUnknown;',
  11806. 'begin',
  11807. ' Say([o,c,TObject]);',
  11808. ' Say([nil,i]);',
  11809. '']);
  11810. ConvertProgram;
  11811. CheckSource('TestArrayOfConst_PassObj',
  11812. LinesToStr([ // statements
  11813. 'rtl.createClass(this, "TObject", null, function () {',
  11814. ' this.$init = function () {',
  11815. ' };',
  11816. ' this.$final = function () {',
  11817. ' };',
  11818. '});',
  11819. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11820. 'this.Say = function (args) {',
  11821. '};',
  11822. 'this.o = null;',
  11823. 'this.c = null;',
  11824. 'this.i = null;',
  11825. '']),
  11826. LinesToStr([ // $mod.$main
  11827. '$mod.Say(pas.system.VarRecs(',
  11828. ' 7,',
  11829. ' $mod.o,',
  11830. ' 8,',
  11831. ' $mod.c,',
  11832. ' 8,',
  11833. ' $mod.TObject',
  11834. '));',
  11835. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11836. '']));
  11837. end;
  11838. procedure TTestModule.TestRecord_Empty;
  11839. begin
  11840. StartProgram(false);
  11841. Add([
  11842. 'type',
  11843. ' TRecA = record',
  11844. ' end;',
  11845. 'var a,b: TRecA;',
  11846. 'begin',
  11847. ' if a=b then ;']);
  11848. ConvertProgram;
  11849. CheckSource('TestRecord_Empty',
  11850. LinesToStr([ // statements
  11851. 'rtl.recNewT(this, "TRecA", function () {',
  11852. ' this.$eq = function (b) {',
  11853. ' return true;',
  11854. ' };',
  11855. ' this.$assign = function (s) {',
  11856. ' return this;',
  11857. ' };',
  11858. '});',
  11859. 'this.a = this.TRecA.$new();',
  11860. 'this.b = this.TRecA.$new();',
  11861. '']),
  11862. LinesToStr([ // $mod.$main
  11863. 'if ($mod.a.$eq($mod.b)) ;'
  11864. ]));
  11865. end;
  11866. procedure TTestModule.TestRecord_Var;
  11867. begin
  11868. StartProgram(false);
  11869. Add('type');
  11870. Add(' TRecA = record');
  11871. Add(' Bold: longint;');
  11872. Add(' end;');
  11873. Add('var Rec: TRecA;');
  11874. Add('begin');
  11875. Add(' rec.bold:=123');
  11876. ConvertProgram;
  11877. CheckSource('TestRecord_Var',
  11878. LinesToStr([ // statements
  11879. 'rtl.recNewT(this, "TRecA", function () {',
  11880. ' this.Bold = 0;',
  11881. ' this.$eq = function (b) {',
  11882. ' return this.Bold === b.Bold;',
  11883. ' };',
  11884. ' this.$assign = function (s) {',
  11885. ' this.Bold = s.Bold;',
  11886. ' return this;',
  11887. ' };',
  11888. '});',
  11889. 'this.Rec = this.TRecA.$new();',
  11890. '']),
  11891. LinesToStr([ // $mod.$main
  11892. '$mod.Rec.Bold = 123;'
  11893. ]));
  11894. end;
  11895. procedure TTestModule.TestRecord_VarExternal;
  11896. begin
  11897. StartProgram(false);
  11898. Add([
  11899. '{$modeswitch externalclass}',
  11900. 'type',
  11901. ' TRecA = record',
  11902. ' i: byte;',
  11903. ' length_: longint external name ''length'';',
  11904. ' end;',
  11905. 'var Rec: TRecA;',
  11906. 'begin',
  11907. ' rec.length_ := rec.length_',
  11908. '']);
  11909. ConvertProgram;
  11910. CheckSource('TestRecord_VarExternal',
  11911. LinesToStr([ // statements
  11912. 'rtl.recNewT(this, "TRecA", function () {',
  11913. ' this.i = 0;',
  11914. ' this.$eq = function (b) {',
  11915. ' return (this.i === b.i) && (this.length === b.length);',
  11916. ' };',
  11917. ' this.$assign = function (s) {',
  11918. ' this.i = s.i;',
  11919. ' this.length = s.length;',
  11920. ' return this;',
  11921. ' };',
  11922. '});',
  11923. 'this.Rec = this.TRecA.$new();',
  11924. '']),
  11925. LinesToStr([ // $mod.$main
  11926. '$mod.Rec.length = $mod.Rec.length;'
  11927. ]));
  11928. end;
  11929. procedure TTestModule.TestRecord_WithDo;
  11930. begin
  11931. StartProgram(false);
  11932. Add('type');
  11933. Add(' TRec = record');
  11934. Add(' vI: longint;');
  11935. Add(' end;');
  11936. Add('var');
  11937. Add(' Int: longint;');
  11938. Add(' r: TRec;');
  11939. Add('begin');
  11940. Add(' with r do');
  11941. Add(' int:=vi;');
  11942. Add(' with r do begin');
  11943. Add(' int:=vi;');
  11944. Add(' vi:=int;');
  11945. Add(' end;');
  11946. ConvertProgram;
  11947. CheckSource('TestWithRecordDo',
  11948. LinesToStr([ // statements
  11949. 'rtl.recNewT(this, "TRec", function () {',
  11950. ' this.vI = 0;',
  11951. ' this.$eq = function (b) {',
  11952. ' return this.vI === b.vI;',
  11953. ' };',
  11954. ' this.$assign = function (s) {',
  11955. ' this.vI = s.vI;',
  11956. ' return this;',
  11957. ' };',
  11958. '});',
  11959. 'this.Int = 0;',
  11960. 'this.r = this.TRec.$new();',
  11961. '']),
  11962. LinesToStr([ // $mod.$main
  11963. 'var $with = $mod.r;',
  11964. '$mod.Int = $with.vI;',
  11965. 'var $with1 = $mod.r;',
  11966. '$mod.Int = $with1.vI;',
  11967. '$with1.vI = $mod.Int;'
  11968. ]));
  11969. end;
  11970. procedure TTestModule.TestRecord_Assign;
  11971. begin
  11972. StartProgram(false);
  11973. Add([
  11974. 'type',
  11975. ' TEnum = (red,green);',
  11976. ' TEnums = set of TEnum;',
  11977. ' TSmallRec = record',
  11978. ' N: longint;',
  11979. ' end;',
  11980. ' TBigRec = record',
  11981. ' Int: longint;',
  11982. ' D: double;',
  11983. ' Arr: array of longint;',
  11984. ' Arr2: array[1..2] of longint;',
  11985. ' Small: TSmallRec;',
  11986. ' Enums: TEnums;',
  11987. ' end;',
  11988. 'var',
  11989. ' r, s: TBigRec;',
  11990. 'begin',
  11991. ' r:=s;',
  11992. ' r:=default(TBigRec);',
  11993. ' r:=default(s);',
  11994. '']);
  11995. ConvertProgram;
  11996. CheckSource('TestRecord_Assign',
  11997. LinesToStr([ // statements
  11998. 'this.TEnum = {',
  11999. ' "0": "red",',
  12000. ' red: 0,',
  12001. ' "1": "green",',
  12002. ' green: 1',
  12003. '};',
  12004. 'rtl.recNewT(this, "TSmallRec", function () {',
  12005. ' this.N = 0;',
  12006. ' this.$eq = function (b) {',
  12007. ' return this.N === b.N;',
  12008. ' };',
  12009. ' this.$assign = function (s) {',
  12010. ' this.N = s.N;',
  12011. ' return this;',
  12012. ' };',
  12013. '});',
  12014. 'rtl.recNewT(this, "TBigRec", function () {',
  12015. ' this.Int = 0;',
  12016. ' this.D = 0.0;',
  12017. ' this.$new = function () {',
  12018. ' var r = Object.create(this);',
  12019. ' r.Arr = [];',
  12020. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12021. ' r.Small = $mod.TSmallRec.$new();',
  12022. ' r.Enums = {};',
  12023. ' return r;',
  12024. ' };',
  12025. ' this.$eq = function (b) {',
  12026. ' 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);',
  12027. ' };',
  12028. ' this.$assign = function (s) {',
  12029. ' this.Int = s.Int;',
  12030. ' this.D = s.D;',
  12031. ' this.Arr = rtl.arrayRef(s.Arr);',
  12032. ' this.Arr2 = s.Arr2.slice(0);',
  12033. ' this.Small.$assign(s.Small);',
  12034. ' this.Enums = rtl.refSet(s.Enums);',
  12035. ' return this;',
  12036. ' };',
  12037. '});',
  12038. 'this.r = this.TBigRec.$new();',
  12039. 'this.s = this.TBigRec.$new();',
  12040. '']),
  12041. LinesToStr([ // $mod.$main
  12042. '$mod.r.$assign($mod.s);',
  12043. '$mod.r.$assign($mod.TBigRec.$new());',
  12044. '$mod.r.$assign($mod.TBigRec.$new());',
  12045. '']));
  12046. end;
  12047. procedure TTestModule.TestRecord_AsParams;
  12048. begin
  12049. StartProgram(false);
  12050. Add([
  12051. 'type',
  12052. ' integer = longint;',
  12053. ' TRecord = record',
  12054. ' i: integer;',
  12055. ' end;',
  12056. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12057. 'var vL: TRecord;',
  12058. 'begin',
  12059. ' vd:=vd;',
  12060. ' vd.i:=vd.i;',
  12061. ' vl:=vc;',
  12062. ' vv:=vv;',
  12063. ' vv.i:=vv.i;',
  12064. ' U:=vl;',
  12065. ' U:=vd;',
  12066. ' U:=vc;',
  12067. ' U:=vv;',
  12068. ' vl:=TRecord(U);',
  12069. ' vd:=TRecord(U);',
  12070. ' vv:=TRecord(U);',
  12071. ' doit(vd,vd,vd,vd);',
  12072. ' doit(vc,vc,vl,vl);',
  12073. ' doit(vv,vv,vv,vv);',
  12074. ' doit(vl,vl,vl,vl);',
  12075. ' TRecord(U).i:=3;',
  12076. 'end;',
  12077. 'var i: TRecord;',
  12078. 'begin',
  12079. ' doit(i,i,i,i);',
  12080. '']);
  12081. ConvertProgram;
  12082. CheckSource('TestRecord_AsParams',
  12083. LinesToStr([ // statements
  12084. 'rtl.recNewT(this, "TRecord", function () {',
  12085. ' this.i = 0;',
  12086. ' this.$eq = function (b) {',
  12087. ' return this.i === b.i;',
  12088. ' };',
  12089. ' this.$assign = function (s) {',
  12090. ' this.i = s.i;',
  12091. ' return this;',
  12092. ' };',
  12093. '});',
  12094. 'this.DoIt = function (vD, vC, vV, U) {',
  12095. ' var vL = $mod.TRecord.$new();',
  12096. ' vD.$assign(vD);',
  12097. ' vD.i = vD.i;',
  12098. ' vL.$assign(vC);',
  12099. ' vV.$assign(vV);',
  12100. ' vV.i = vV.i;',
  12101. ' U.$assign(vL);',
  12102. ' U.$assign(vD);',
  12103. ' U.$assign(vC);',
  12104. ' U.$assign(vV);',
  12105. ' vL.$assign(U);',
  12106. ' vD.$assign(U);',
  12107. ' vV.$assign(U);',
  12108. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12109. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12110. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12111. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12112. ' U.i = 3;',
  12113. '};',
  12114. 'this.i = this.TRecord.$new();'
  12115. ]),
  12116. LinesToStr([
  12117. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12118. '']));
  12119. end;
  12120. procedure TTestModule.TestRecord_ConstRef;
  12121. begin
  12122. StartProgram(false);
  12123. Add([
  12124. 'type TRec = record i: word; end;',
  12125. 'procedure Run(constref a: TRec);',
  12126. 'begin',
  12127. 'end;',
  12128. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12129. 'var l: TRec;',
  12130. 'begin',
  12131. ' Run(l);',
  12132. ' Run(a);',
  12133. ' Run(b);',
  12134. ' Run(c);',
  12135. ' Run(d);',
  12136. ' Run(e);',
  12137. 'end;',
  12138. 'begin',
  12139. '']);
  12140. ConvertProgram;
  12141. CheckResolverUnexpectedHints();
  12142. CheckSource('TestRecord_ConstRef',
  12143. LinesToStr([ // statements
  12144. 'rtl.recNewT(this, "TRec", function () {',
  12145. ' this.i = 0;',
  12146. ' this.$eq = function (b) {',
  12147. ' return this.i === b.i;',
  12148. ' };',
  12149. ' this.$assign = function (s) {',
  12150. ' this.i = s.i;',
  12151. ' return this;',
  12152. ' };',
  12153. '});',
  12154. 'this.Run = function (a) {',
  12155. '};',
  12156. 'this.Fly = function (a, b, c, d, e) {',
  12157. ' var l = $mod.TRec.$new();',
  12158. ' $mod.Run(l);',
  12159. ' $mod.Run(a);',
  12160. ' $mod.Run(b);',
  12161. ' $mod.Run(c);',
  12162. ' $mod.Run(d);',
  12163. ' $mod.Run(e);',
  12164. '};',
  12165. '']),
  12166. LinesToStr([
  12167. '']));
  12168. end;
  12169. procedure TTestModule.TestRecordElement_AsParams;
  12170. begin
  12171. StartProgram(false);
  12172. Add('type');
  12173. Add(' integer = longint;');
  12174. Add(' TRecord = record');
  12175. Add(' i: integer;');
  12176. Add(' end;');
  12177. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12178. Add('var vJ: TRecord;');
  12179. Add('begin');
  12180. Add(' doit(vj.i,vj.i,vj.i);');
  12181. Add('end;');
  12182. Add('var r: TRecord;');
  12183. Add('begin');
  12184. Add(' doit(r.i,r.i,r.i);');
  12185. ConvertProgram;
  12186. CheckSource('TestRecordElement_AsParams',
  12187. LinesToStr([ // statements
  12188. 'rtl.recNewT(this, "TRecord", function () {',
  12189. ' this.i = 0;',
  12190. ' this.$eq = function (b) {',
  12191. ' return this.i === b.i;',
  12192. ' };',
  12193. ' this.$assign = function (s) {',
  12194. ' this.i = s.i;',
  12195. ' return this;',
  12196. ' };',
  12197. '});',
  12198. 'this.DoIt = function (vG,vH,vI) {',
  12199. ' var vJ = $mod.TRecord.$new();',
  12200. ' $mod.DoIt(vJ.i, vJ.i, {',
  12201. ' p: vJ,',
  12202. ' get: function () {',
  12203. ' return this.p.i;',
  12204. ' },',
  12205. ' set: function (v) {',
  12206. ' this.p.i = v;',
  12207. ' }',
  12208. ' });',
  12209. '};',
  12210. 'this.r = this.TRecord.$new();'
  12211. ]),
  12212. LinesToStr([
  12213. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12214. ' p: $mod.r,',
  12215. ' get: function () {',
  12216. ' return this.p.i;',
  12217. ' },',
  12218. ' set: function (v) {',
  12219. ' this.p.i = v;',
  12220. ' }',
  12221. '});'
  12222. ]));
  12223. end;
  12224. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12225. begin
  12226. StartProgram(false);
  12227. Add('type');
  12228. Add(' integer = longint;');
  12229. Add(' TRecord = record');
  12230. Add(' i: integer;');
  12231. Add(' end;');
  12232. Add('function GetRec(vB: integer = 0): TRecord;');
  12233. Add('begin');
  12234. Add('end;');
  12235. Add('procedure DoIt(vG: integer; const vH: integer);');
  12236. Add('begin');
  12237. Add('end;');
  12238. Add('begin');
  12239. Add(' doit(getrec.i,getrec.i);');
  12240. Add(' doit(getrec().i,getrec().i);');
  12241. Add(' doit(getrec(1).i,getrec(2).i);');
  12242. ConvertProgram;
  12243. CheckSource('TestRecordElementFromFuncResult_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.GetRec = function (vB) {',
  12256. ' var Result = $mod.TRecord.$new();',
  12257. ' return Result;',
  12258. '};',
  12259. 'this.DoIt = function (vG, vH) {',
  12260. '};',
  12261. '']),
  12262. LinesToStr([
  12263. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12264. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12265. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12266. '']));
  12267. end;
  12268. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12269. begin
  12270. StartProgram(false);
  12271. Add('type');
  12272. Add(' integer = longint;');
  12273. Add(' TRecord = record');
  12274. Add(' i: integer;');
  12275. Add(' end;');
  12276. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12277. Add('begin');
  12278. Add('end;');
  12279. Add('var r: trecord;');
  12280. Add('begin');
  12281. Add(' with r do ');
  12282. Add(' doit(i,i,i);');
  12283. ConvertProgram;
  12284. CheckSource('TestRecordElementFromWith_AsParams',
  12285. LinesToStr([ // statements
  12286. 'rtl.recNewT(this, "TRecord", function () {',
  12287. ' this.i = 0;',
  12288. ' this.$eq = function (b) {',
  12289. ' return this.i === b.i;',
  12290. ' };',
  12291. ' this.$assign = function (s) {',
  12292. ' this.i = s.i;',
  12293. ' return this;',
  12294. ' };',
  12295. '});',
  12296. 'this.DoIt = function (vG,vH,vI) {',
  12297. '};',
  12298. 'this.r = this.TRecord.$new();'
  12299. ]),
  12300. LinesToStr([
  12301. 'var $with = $mod.r;',
  12302. '$mod.DoIt($with.i,$with.i,{',
  12303. ' p: $with,',
  12304. ' get: function () {',
  12305. ' return this.p.i;',
  12306. ' },',
  12307. ' set: function (v) {',
  12308. ' this.p.i = v;',
  12309. ' }',
  12310. '});',
  12311. '']));
  12312. end;
  12313. procedure TTestModule.TestRecord_Equal;
  12314. begin
  12315. StartProgram(false);
  12316. Add('type');
  12317. Add(' integer = longint;');
  12318. Add(' TFlag = (red,blue);');
  12319. Add(' TFlags = set of TFlag;');
  12320. Add(' TProc = procedure;');
  12321. Add(' TRecord = record');
  12322. Add(' i: integer;');
  12323. Add(' Event: TProc;');
  12324. Add(' f: TFlags;');
  12325. Add(' end;');
  12326. Add(' TNested = record');
  12327. Add(' r: TRecord;');
  12328. Add(' end;');
  12329. Add('var');
  12330. Add(' b: boolean;');
  12331. Add(' r,s: trecord;');
  12332. Add('begin');
  12333. Add(' b:=r=s;');
  12334. Add(' b:=r<>s;');
  12335. ConvertProgram;
  12336. CheckSource('TestRecord_Equal',
  12337. LinesToStr([ // statements
  12338. 'this.TFlag = {',
  12339. ' "0": "red",',
  12340. ' red: 0,',
  12341. ' "1": "blue",',
  12342. ' blue: 1',
  12343. '};',
  12344. 'rtl.recNewT(this, "TRecord", function () {',
  12345. ' this.i = 0;',
  12346. ' this.Event = null;',
  12347. ' this.$new = function () {',
  12348. ' var r = Object.create(this);',
  12349. ' r.f = {};',
  12350. ' return r;',
  12351. ' };',
  12352. ' this.$eq = function (b) {',
  12353. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12354. ' };',
  12355. ' this.$assign = function (s) {',
  12356. ' this.i = s.i;',
  12357. ' this.Event = s.Event;',
  12358. ' this.f = rtl.refSet(s.f);',
  12359. ' return this;',
  12360. ' };',
  12361. '});',
  12362. 'rtl.recNewT(this, "TNested", function () {',
  12363. ' this.$new = function () {',
  12364. ' var r = Object.create(this);',
  12365. ' r.r = $mod.TRecord.$new();',
  12366. ' return r;',
  12367. ' };',
  12368. ' this.$eq = function (b) {',
  12369. ' return this.r.$eq(b.r);',
  12370. ' };',
  12371. ' this.$assign = function (s) {',
  12372. ' this.r.$assign(s.r);',
  12373. ' return this;',
  12374. ' };',
  12375. '});',
  12376. 'this.b = false;',
  12377. 'this.r = this.TRecord.$new();',
  12378. 'this.s = this.TRecord.$new();',
  12379. '']),
  12380. LinesToStr([
  12381. '$mod.b = $mod.r.$eq($mod.s);',
  12382. '$mod.b = !$mod.r.$eq($mod.s);',
  12383. '']));
  12384. end;
  12385. procedure TTestModule.TestRecord_JSValue;
  12386. begin
  12387. StartProgram(false);
  12388. Add([
  12389. 'type',
  12390. ' TRecord = record',
  12391. ' i: longint;',
  12392. ' end;',
  12393. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12394. 'begin',
  12395. 'end;',
  12396. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12397. 'begin',
  12398. ' if jsvalue(d) then ;',
  12399. ' if jsvalue(c) then ;',
  12400. ' if jsvalue(v) then ;',
  12401. 'end;',
  12402. 'var',
  12403. ' Jv: jsvalue;',
  12404. ' Rec: trecord;',
  12405. 'begin',
  12406. ' rec:=trecord(jv);',
  12407. ' jv:=rec;',
  12408. ' Fly(rec,rec);',
  12409. ' Fly(@rec,@rec);',
  12410. ' if jsvalue(Rec) then ;',
  12411. ' Run(trecord(jv),trecord(jv),rec);',
  12412. '']);
  12413. ConvertProgram;
  12414. CheckSource('TestRecord_JSValue',
  12415. LinesToStr([ // statements
  12416. 'rtl.recNewT(this, "TRecord", function () {',
  12417. ' this.i = 0;',
  12418. ' this.$eq = function (b) {',
  12419. ' return this.i === b.i;',
  12420. ' };',
  12421. ' this.$assign = function (s) {',
  12422. ' this.i = s.i;',
  12423. ' return this;',
  12424. ' };',
  12425. '});',
  12426. 'this.Fly = function (d, c) {',
  12427. '};',
  12428. 'this.Run = function (d, c, v) {',
  12429. ' if (d) ;',
  12430. ' if (c) ;',
  12431. ' if (v) ;',
  12432. '};',
  12433. 'this.Jv = undefined;',
  12434. 'this.Rec = this.TRecord.$new();',
  12435. '']),
  12436. LinesToStr([
  12437. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12438. '$mod.Jv = $mod.Rec;',
  12439. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12440. '$mod.Fly($mod.Rec, $mod.Rec);',
  12441. 'if ($mod.Rec) ;',
  12442. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12443. '']));
  12444. end;
  12445. procedure TTestModule.TestRecord_VariantFail;
  12446. begin
  12447. StartProgram(false);
  12448. Add([
  12449. 'type',
  12450. ' TRec = record',
  12451. ' case word of',
  12452. ' 0: (b0, b1: Byte);',
  12453. ' 1: (i: word);',
  12454. ' end;',
  12455. 'begin']);
  12456. SetExpectedPasResolverError('Not supported: variant record',
  12457. nNotSupportedX);
  12458. ConvertProgram;
  12459. end;
  12460. procedure TTestModule.TestRecord_FieldArray;
  12461. begin
  12462. StartProgram(false);
  12463. Add([
  12464. 'type',
  12465. ' TArrInt = array[3..4] of longint;',
  12466. ' TArrArrInt = array[3..4] of longint;',
  12467. ' TRec = record',
  12468. ' a: array of longint;',
  12469. ' s: array[1..2] of longint;',
  12470. ' m: array[1..2,3..4] of longint;',
  12471. ' o: TArrArrInt;',
  12472. ' end;',
  12473. 'begin']);
  12474. ConvertProgram;
  12475. CheckSource('TestRecord_FieldArray',
  12476. LinesToStr([ // statements
  12477. 'rtl.recNewT(this, "TRec", function () {',
  12478. ' this.m$a$clone = function (a) {',
  12479. ' var b = [];',
  12480. ' b.length = 2;',
  12481. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12482. ' return b;',
  12483. ' };',
  12484. ' this.$new = function () {',
  12485. ' var r = Object.create(this);',
  12486. ' r.a = [];',
  12487. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12488. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12489. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12490. ' return r;',
  12491. ' };',
  12492. ' this.$eq = function (b) {',
  12493. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12494. ' };',
  12495. ' this.$assign = function (s) {',
  12496. ' this.a = rtl.arrayRef(s.a);',
  12497. ' this.s = s.s.slice(0);',
  12498. ' this.m = this.m$a$clone(s.m);',
  12499. ' this.o = s.o.slice(0);',
  12500. ' return this;',
  12501. ' };',
  12502. '});',
  12503. '']),
  12504. LinesToStr([ // $mod.$main
  12505. '']));
  12506. end;
  12507. procedure TTestModule.TestRecord_Const;
  12508. begin
  12509. StartProgram(false);
  12510. Add([
  12511. 'type',
  12512. ' TArrInt = array[3..4] of longint;',
  12513. ' TPoint = record x,y: longint; end;',
  12514. ' TRec = record',
  12515. ' i: longint;',
  12516. ' a: array of longint;',
  12517. ' s: array[1..2] of longint;',
  12518. ' m: array[1..2,3..4] of longint;',
  12519. ' p: TPoint;',
  12520. ' end;',
  12521. ' TPoints = array of TPoint;',
  12522. 'const',
  12523. ' r: TRec = (',
  12524. ' i:1;',
  12525. ' a:(2,3);',
  12526. ' s:(4,5);',
  12527. ' m:( (11,12), (13,14) );',
  12528. ' p: (x:21; y:22)',
  12529. ' );',
  12530. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12531. 'begin']);
  12532. ConvertProgram;
  12533. CheckSource('TestRecord_Const',
  12534. LinesToStr([ // statements
  12535. 'rtl.recNewT(this, "TPoint", function () {',
  12536. ' this.x = 0;',
  12537. ' this.y = 0;',
  12538. ' this.$eq = function (b) {',
  12539. ' return (this.x === b.x) && (this.y === b.y);',
  12540. ' };',
  12541. ' this.$assign = function (s) {',
  12542. ' this.x = s.x;',
  12543. ' this.y = s.y;',
  12544. ' return this;',
  12545. ' };',
  12546. '});',
  12547. 'rtl.recNewT(this, "TRec", function () {',
  12548. ' this.i = 0;',
  12549. ' this.m$a$clone = function (a) {',
  12550. ' var b = [];',
  12551. ' b.length = 2;',
  12552. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12553. ' return b;',
  12554. ' };',
  12555. ' this.$new = function () {',
  12556. ' var r = Object.create(this);',
  12557. ' r.a = [];',
  12558. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12559. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12560. ' r.p = $mod.TPoint.$new();',
  12561. ' return r;',
  12562. ' };',
  12563. ' this.$eq = function (b) {',
  12564. ' 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);',
  12565. ' };',
  12566. ' this.$assign = function (s) {',
  12567. ' this.i = s.i;',
  12568. ' this.a = rtl.arrayRef(s.a);',
  12569. ' this.s = s.s.slice(0);',
  12570. ' this.m = this.m$a$clone(s.m);',
  12571. ' this.p.$assign(s.p);',
  12572. ' return this;',
  12573. ' };',
  12574. '});',
  12575. 'this.r = this.TRec.$clone({',
  12576. ' i: 1,',
  12577. ' a: [2, 3],',
  12578. ' s: [4, 5],',
  12579. ' m: [[11, 12], [13, 14]],',
  12580. ' p: this.TPoint.$clone({',
  12581. ' x: 21,',
  12582. ' y: 22',
  12583. ' })',
  12584. '});',
  12585. 'this.p = [this.TPoint.$clone({',
  12586. ' x: 1,',
  12587. ' y: 2',
  12588. '}), this.TPoint.$clone({',
  12589. ' x: 3,',
  12590. ' y: 4',
  12591. '})];',
  12592. '']),
  12593. LinesToStr([ // $mod.$main
  12594. '']));
  12595. end;
  12596. procedure TTestModule.TestRecord_TypecastFail;
  12597. begin
  12598. StartProgram(false);
  12599. Add([
  12600. 'type',
  12601. ' TPoint = record x,y: longint; end;',
  12602. ' TRec = record l: longint end;',
  12603. 'var p: TPoint;',
  12604. 'begin',
  12605. ' if TRec(p).l=2 then ;']);
  12606. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12607. nIllegalTypeConversionTo);
  12608. ConvertProgram;
  12609. end;
  12610. procedure TTestModule.TestRecord_InFunction;
  12611. begin
  12612. StartProgram(false);
  12613. Add([
  12614. 'var TPoint: longint = 3;',
  12615. 'procedure DoIt;',
  12616. 'type',
  12617. ' TPoint = record x,y: longint; end;',
  12618. ' TPoints = array of TPoint;',
  12619. 'var',
  12620. ' r: TPoint;',
  12621. ' p: TPoints;',
  12622. 'begin',
  12623. ' SetLength(p,2);',
  12624. 'end;',
  12625. 'begin']);
  12626. ConvertProgram;
  12627. CheckSource('TestRecord_InFunction',
  12628. LinesToStr([ // statements
  12629. 'this.TPoint = 3;',
  12630. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12631. ' this.x = 0;',
  12632. ' this.y = 0;',
  12633. ' this.$eq = function (b) {',
  12634. ' return (this.x === b.x) && (this.y === b.y);',
  12635. ' };',
  12636. ' this.$assign = function (s) {',
  12637. ' this.x = s.x;',
  12638. ' this.y = s.y;',
  12639. ' return this;',
  12640. ' };',
  12641. '});',
  12642. 'this.DoIt = function () {',
  12643. ' var r = TPoint$1.$new();',
  12644. ' var p = [];',
  12645. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12646. '};',
  12647. '']),
  12648. LinesToStr([ // $mod.$main
  12649. '']));
  12650. end;
  12651. procedure TTestModule.TestRecordAnonym_Field;
  12652. begin
  12653. StartProgram(false);
  12654. Add(['',
  12655. 'var Rec: record',
  12656. ' Bold: longint;',
  12657. ' end;',
  12658. 'begin',
  12659. ' rec.bold:=123;',
  12660. ' rec.bold:=rec.bold+7;',
  12661. '']);
  12662. ConvertProgram;
  12663. CheckSource('TestRecordAnonym_Field',
  12664. LinesToStr([ // statements
  12665. 'rtl.recNewT(this, "Rec$a", function () {',
  12666. ' this.Bold = 0;',
  12667. ' this.$eq = function (b) {',
  12668. ' return this.Bold === b.Bold;',
  12669. ' };',
  12670. ' this.$assign = function (s) {',
  12671. ' this.Bold = s.Bold;',
  12672. ' return this;',
  12673. ' };',
  12674. '});',
  12675. 'this.Rec = this.Rec$a.$new();',
  12676. '']),
  12677. LinesToStr([ // $mod.$main
  12678. '$mod.Rec.Bold = 123;',
  12679. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12680. '']));
  12681. end;
  12682. procedure TTestModule.TestRecordAnonym_Assign;
  12683. begin
  12684. StartProgram(false);
  12685. Add(['',
  12686. 'var S,T: record',
  12687. ' Bold: longint;',
  12688. ' end;',
  12689. ' b: boolean;',
  12690. 'begin',
  12691. ' S:=T;',
  12692. ' b:=s=t;',
  12693. '']);
  12694. ConvertProgram;
  12695. CheckSource('TestRecordAnonym_Assign',
  12696. LinesToStr([ // statements
  12697. 'rtl.recNewT(this, "T$a", function () {',
  12698. ' this.Bold = 0;',
  12699. ' this.$eq = function (b) {',
  12700. ' return this.Bold === b.Bold;',
  12701. ' };',
  12702. ' this.$assign = function (s) {',
  12703. ' this.Bold = s.Bold;',
  12704. ' return this;',
  12705. ' };',
  12706. '});',
  12707. 'this.S = this.T$a.$new();',
  12708. 'this.T = this.T$a.$new();',
  12709. 'this.b = false;',
  12710. '']),
  12711. LinesToStr([ // $mod.$main
  12712. '$mod.S.$assign($mod.T);',
  12713. '$mod.b = $mod.S.$eq($mod.T);',
  12714. '']));
  12715. end;
  12716. procedure TTestModule.TestRecordAnonym_Nested;
  12717. begin
  12718. StartProgram(false);
  12719. Add(['',
  12720. 'var S,T: record',
  12721. ' Bold: longint;',
  12722. ' Sub: record',
  12723. ' Color: word;',
  12724. ' end;',
  12725. ' end;',
  12726. ' b: boolean;',
  12727. 'begin',
  12728. ' S:=T;',
  12729. ' S.Sub:=T.Sub;',
  12730. ' S.Sub.Color:=T.Sub.Color+3;',
  12731. ' b:=s=t;',
  12732. ' b:=s.Sub=t.Sub;',
  12733. '']);
  12734. ConvertProgram;
  12735. CheckSource('TestRecordAnonym_Nested',
  12736. LinesToStr([ // statements
  12737. 'rtl.recNewT(this, "T$a", function () {',
  12738. ' this.Bold = 0;',
  12739. ' rtl.recNewT(this, "Sub$a", function () {',
  12740. ' this.Color = 0;',
  12741. ' this.$eq = function (b) {',
  12742. ' return this.Color === b.Color;',
  12743. ' };',
  12744. ' this.$assign = function (s) {',
  12745. ' this.Color = s.Color;',
  12746. ' return this;',
  12747. ' };',
  12748. ' });',
  12749. ' this.$new = function () {',
  12750. ' var r = Object.create(this);',
  12751. ' r.Sub = this.Sub$a.$new();',
  12752. ' return r;',
  12753. ' };',
  12754. ' this.$eq = function (b) {',
  12755. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12756. ' };',
  12757. ' this.$assign = function (s) {',
  12758. ' this.Bold = s.Bold;',
  12759. ' this.Sub.$assign(s.Sub);',
  12760. ' return this;',
  12761. ' };',
  12762. '}, true);',
  12763. 'this.S = this.T$a.$new();',
  12764. 'this.T = this.T$a.$new();',
  12765. 'this.b = false;',
  12766. '']),
  12767. LinesToStr([ // $mod.$main
  12768. '$mod.S.$assign($mod.T);',
  12769. '$mod.S.Sub.$assign($mod.T.Sub);',
  12770. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12771. '$mod.b = $mod.S.$eq($mod.T);',
  12772. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12773. '']));
  12774. end;
  12775. procedure TTestModule.TestRecordAnonym_Const;
  12776. begin
  12777. StartProgram(false);
  12778. Add(['',
  12779. 'var T: record',
  12780. ' Bold: longint;',
  12781. ' Sub: record',
  12782. ' Color: word;',
  12783. ' end;',
  12784. ' end = (Bold: 2; Sub: (Color: 3));',
  12785. 'begin',
  12786. '']);
  12787. ConvertProgram;
  12788. CheckSource('TestRecordAnonym_Const',
  12789. LinesToStr([ // statements
  12790. 'rtl.recNewT(this, "T$a", function () {',
  12791. ' this.Bold = 0;',
  12792. ' rtl.recNewT(this, "Sub$a", function () {',
  12793. ' this.Color = 0;',
  12794. ' this.$eq = function (b) {',
  12795. ' return this.Color === b.Color;',
  12796. ' };',
  12797. ' this.$assign = function (s) {',
  12798. ' this.Color = s.Color;',
  12799. ' return this;',
  12800. ' };',
  12801. ' });',
  12802. ' this.$new = function () {',
  12803. ' var r = Object.create(this);',
  12804. ' r.Sub = this.Sub$a.$new();',
  12805. ' return r;',
  12806. ' };',
  12807. ' this.$eq = function (b) {',
  12808. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12809. ' };',
  12810. ' this.$assign = function (s) {',
  12811. ' this.Bold = s.Bold;',
  12812. ' this.Sub.$assign(s.Sub);',
  12813. ' return this;',
  12814. ' };',
  12815. '}, true);',
  12816. 'this.T = this.T$a.$clone({',
  12817. ' Bold: 2,',
  12818. ' Sub: this.T$a.Sub$a.$clone({',
  12819. ' Color: 3',
  12820. ' })',
  12821. '});',
  12822. '']),
  12823. LinesToStr([ // $mod.$main
  12824. '']));
  12825. end;
  12826. procedure TTestModule.TestRecordAnonym_InFunction;
  12827. begin
  12828. StartProgram(false);
  12829. Add(['',
  12830. 'procedure Fly;',
  12831. 'var T: record',
  12832. ' Bold: longint;',
  12833. ' Sub: record',
  12834. ' Color: word;',
  12835. ' end;',
  12836. ' end = (Bold: 2; Sub: (Color: 3));',
  12837. 'begin',
  12838. 'end;',
  12839. 'begin',
  12840. '']);
  12841. ConvertProgram;
  12842. CheckSource('TestRecordAnonym_InFunction',
  12843. LinesToStr([ // statements
  12844. 'var T$a = rtl.recNewT(null, "", function () {',
  12845. ' this.Bold = 0;',
  12846. ' rtl.recNewT(this, "Sub$a", function () {',
  12847. ' this.Color = 0;',
  12848. ' this.$eq = function (b) {',
  12849. ' return this.Color === b.Color;',
  12850. ' };',
  12851. ' this.$assign = function (s) {',
  12852. ' this.Color = s.Color;',
  12853. ' return this;',
  12854. ' };',
  12855. ' });',
  12856. ' this.$new = function () {',
  12857. ' var r = Object.create(this);',
  12858. ' r.Sub = this.Sub$a.$new();',
  12859. ' return r;',
  12860. ' };',
  12861. ' this.$eq = function (b) {',
  12862. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12863. ' };',
  12864. ' this.$assign = function (s) {',
  12865. ' this.Bold = s.Bold;',
  12866. ' this.Sub.$assign(s.Sub);',
  12867. ' return this;',
  12868. ' };',
  12869. '}, true);',
  12870. 'this.Fly = function () {',
  12871. ' var T = T$a.$clone({',
  12872. ' Bold: 2,',
  12873. ' Sub: T$a.Sub$a.$clone({',
  12874. ' Color: 3',
  12875. ' })',
  12876. ' });',
  12877. '};',
  12878. '']),
  12879. LinesToStr([ // $mod.$main
  12880. '']));
  12881. end;
  12882. procedure TTestModule.TestAdvRecord_Function;
  12883. begin
  12884. StartProgram(false);
  12885. Parser.Options:=Parser.Options+[po_cassignments];
  12886. Add([
  12887. '{$modeswitch AdvancedRecords}',
  12888. 'type',
  12889. ' TPoint = record',
  12890. ' x,y: word;',
  12891. ' function Add(const apt: TPoint): TPoint;',
  12892. ' end;',
  12893. 'function TPoint.Add(const apt: TPoint): TPoint;',
  12894. 'begin',
  12895. ' Result:=Self;',
  12896. ' Result.x+=apt.x;',
  12897. ' Result.y:=Result.y+apt.y;',
  12898. ' Self:=apt;',
  12899. 'end;',
  12900. 'var p,q: TPoint;',
  12901. 'begin',
  12902. ' p.add(q);',
  12903. ' p:=default(TPoint);',
  12904. ' p:=q;',
  12905. '']);
  12906. ConvertProgram;
  12907. CheckSource('TestAdvRecord_Function',
  12908. LinesToStr([ // statements
  12909. 'rtl.recNewT(this, "TPoint", function () {',
  12910. ' this.x = 0;',
  12911. ' this.y = 0;',
  12912. ' this.$eq = function (b) {',
  12913. ' return (this.x === b.x) && (this.y === b.y);',
  12914. ' };',
  12915. ' this.$assign = function (s) {',
  12916. ' this.x = s.x;',
  12917. ' this.y = s.y;',
  12918. ' return this;',
  12919. ' };',
  12920. ' this.Add = function (apt) {',
  12921. ' var Result = $mod.TPoint.$new();',
  12922. ' Result.$assign(this);',
  12923. ' Result.x += apt.x;',
  12924. ' Result.y = Result.y + apt.y;',
  12925. ' this.$assign(apt);',
  12926. ' return Result;',
  12927. ' };',
  12928. '});',
  12929. 'this.p = this.TPoint.$new();',
  12930. 'this.q = this.TPoint.$new();',
  12931. '']),
  12932. LinesToStr([ // $mod.$main
  12933. '$mod.p.Add($mod.q);',
  12934. '$mod.p.$assign($mod.TPoint.$new());',
  12935. '$mod.p.$assign($mod.q);',
  12936. '']));
  12937. end;
  12938. procedure TTestModule.TestAdvRecord_Property;
  12939. begin
  12940. StartProgram(false);
  12941. Add([
  12942. '{$modeswitch AdvancedRecords}',
  12943. 'type',
  12944. ' TPoint = record',
  12945. ' x,y: word;',
  12946. ' strict private',
  12947. ' function GetSize: longword;',
  12948. ' procedure SetSize(Value: longword);',
  12949. ' public',
  12950. ' property Size: longword read GetSize write SetSize;',
  12951. ' property Left: word read x write y;',
  12952. ' end;',
  12953. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  12954. 'function TPoint.GetSize: longword;',
  12955. 'begin',
  12956. ' x:=y;',
  12957. ' Size:=Size;',
  12958. ' Left:=Left;',
  12959. 'end;',
  12960. 'procedure TPoint.SetSize(Value: longword);',
  12961. 'begin',
  12962. 'end;',
  12963. 'var p,q: TPoint;',
  12964. 'begin',
  12965. ' p.Size:=q.Size;',
  12966. ' p.Left:=q.Left;',
  12967. '']);
  12968. ConvertProgram;
  12969. CheckSource('TestAdvRecord_Property',
  12970. LinesToStr([ // statements
  12971. 'rtl.recNewT(this, "TPoint", function () {',
  12972. ' this.x = 0;',
  12973. ' this.y = 0;',
  12974. ' this.$eq = function (b) {',
  12975. ' return (this.x === b.x) && (this.y === b.y);',
  12976. ' };',
  12977. ' this.$assign = function (s) {',
  12978. ' this.x = s.x;',
  12979. ' this.y = s.y;',
  12980. ' return this;',
  12981. ' };',
  12982. ' this.GetSize = function () {',
  12983. ' var Result = 0;',
  12984. ' this.x = this.y;',
  12985. ' this.SetSize(this.GetSize());',
  12986. ' this.y = this.x;',
  12987. ' return Result;',
  12988. ' };',
  12989. ' this.SetSize = function (Value) {',
  12990. ' };',
  12991. '});',
  12992. 'this.SetSize = function (Value) {',
  12993. '};',
  12994. 'this.p = this.TPoint.$new();',
  12995. 'this.q = this.TPoint.$new();',
  12996. '']),
  12997. LinesToStr([ // $mod.$main
  12998. '$mod.p.SetSize($mod.q.GetSize());',
  12999. '$mod.p.y = $mod.q.x;',
  13000. '']));
  13001. end;
  13002. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13003. begin
  13004. StartProgram(false);
  13005. Add([
  13006. '{$modeswitch AdvancedRecords}',
  13007. 'type',
  13008. ' TPoint = record',
  13009. ' strict private',
  13010. ' function GetItems(Index: word): word;',
  13011. ' procedure SetItems(Index: word; Value: word);',
  13012. ' public',
  13013. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13014. ' end;',
  13015. 'function TPoint.GetItems(Index: word): word;',
  13016. 'begin',
  13017. ' Items[index]:=Items[index];',
  13018. ' self.Items[index]:=self.Items[index];',
  13019. 'end;',
  13020. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13021. 'begin',
  13022. 'end;',
  13023. 'var p: TPoint;',
  13024. 'begin',
  13025. ' p[1]:=p[2];',
  13026. ' p.Items[3]:=p.Items[4];',
  13027. '']);
  13028. ConvertProgram;
  13029. CheckSource('TestAdvRecord_PropertyDefault',
  13030. LinesToStr([ // statements
  13031. 'rtl.recNewT(this, "TPoint", function () {',
  13032. ' this.$eq = function (b) {',
  13033. ' return true;',
  13034. ' };',
  13035. ' this.$assign = function (s) {',
  13036. ' return this;',
  13037. ' };',
  13038. ' this.GetItems = function (Index) {',
  13039. ' var Result = 0;',
  13040. ' this.SetItems(Index, this.GetItems(Index));',
  13041. ' this.SetItems(Index, this.GetItems(Index));',
  13042. ' return Result;',
  13043. ' };',
  13044. ' this.SetItems = function (Index, Value) {',
  13045. ' };',
  13046. '});',
  13047. 'this.p = this.TPoint.$new();',
  13048. '']),
  13049. LinesToStr([ // $mod.$main
  13050. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13051. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13052. '']));
  13053. end;
  13054. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13055. begin
  13056. StartProgram(false);
  13057. Add([
  13058. '{$modeswitch AdvancedRecords}',
  13059. 'type',
  13060. ' TRec = record',
  13061. ' class var',
  13062. ' Fx: longint;',
  13063. ' Fy: longint;',
  13064. ' class function GetInt: longint; static;',
  13065. ' class procedure SetInt(Value: longint); static;',
  13066. ' class procedure DoIt; static;',
  13067. ' class property IntA: longint read Fx write Fy;',
  13068. ' class property IntB: longint read GetInt write SetInt;',
  13069. ' end;',
  13070. 'class function trec.getint: longint;',
  13071. 'begin',
  13072. ' result:=fx;',
  13073. 'end;',
  13074. 'class procedure trec.setint(value: longint);',
  13075. 'begin',
  13076. 'end;',
  13077. 'class procedure trec.doit;',
  13078. 'begin',
  13079. ' IntA:=IntA+1;',
  13080. ' IntB:=IntB+1;',
  13081. 'end;',
  13082. 'var r: trec;',
  13083. 'begin',
  13084. ' trec.inta:=trec.inta+1;',
  13085. ' if trec.intb=2 then;',
  13086. ' trec.intb:=trec.intb+2;',
  13087. ' trec.setint(trec.inta);',
  13088. ' r.inta:=r.inta+1;',
  13089. ' if r.intb=2 then;',
  13090. ' r.intb:=r.intb+2;',
  13091. ' r.setint(r.inta);']);
  13092. ConvertProgram;
  13093. CheckSource('TestAdvRecord_Property_ClassMethod',
  13094. LinesToStr([ // statements
  13095. 'rtl.recNewT(this, "TRec", function () {',
  13096. ' this.Fx = 0;',
  13097. ' this.Fy = 0;',
  13098. ' this.$eq = function (b) {',
  13099. ' return true;',
  13100. ' };',
  13101. ' this.$assign = function (s) {',
  13102. ' return this;',
  13103. ' };',
  13104. ' this.GetInt = function () {',
  13105. ' var Result = 0;',
  13106. ' Result = $mod.TRec.Fx;',
  13107. ' return Result;',
  13108. ' };',
  13109. ' this.SetInt = function (Value) {',
  13110. ' };',
  13111. ' this.DoIt = function () {',
  13112. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13113. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13114. ' };',
  13115. '}, true);',
  13116. 'this.r = this.TRec.$new();',
  13117. '']),
  13118. LinesToStr([ // $mod.$main
  13119. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13120. 'if ($mod.TRec.GetInt() === 2) ;',
  13121. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13122. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13123. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13124. 'if ($mod.TRec.GetInt() === 2) ;',
  13125. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13126. '$mod.TRec.SetInt($mod.r.Fx);',
  13127. '']));
  13128. end;
  13129. procedure TTestModule.TestAdvRecord_Const;
  13130. begin
  13131. StartProgram(false);
  13132. Add([
  13133. '{$modeswitch AdvancedRecords}',
  13134. 'type',
  13135. ' TArrInt = array[3..4] of longint;',
  13136. ' TPoint = record',
  13137. ' x,y: longint;',
  13138. ' class var Count: nativeint;',
  13139. ' end;',
  13140. ' TRec = record',
  13141. ' i: longint;',
  13142. ' a: array of longint;',
  13143. ' s: array[1..2] of longint;',
  13144. ' m: array[1..2,3..4] of longint;',
  13145. ' p: TPoint;',
  13146. ' end;',
  13147. ' TPoints = array of TPoint;',
  13148. 'const',
  13149. ' r: TRec = (',
  13150. ' i:1;',
  13151. ' a:(2,3);',
  13152. ' s:(4,5);',
  13153. ' m:( (11,12), (13,14) );',
  13154. ' p: (x:21)',
  13155. ' );',
  13156. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13157. 'begin']);
  13158. ConvertProgram;
  13159. CheckSource('TestAdvRecord_Const',
  13160. LinesToStr([ // statements
  13161. 'rtl.recNewT(this, "TPoint", function () {',
  13162. ' this.x = 0;',
  13163. ' this.y = 0;',
  13164. ' this.Count = 0;',
  13165. ' this.$eq = function (b) {',
  13166. ' return (this.x === b.x) && (this.y === b.y);',
  13167. ' };',
  13168. ' this.$assign = function (s) {',
  13169. ' this.x = s.x;',
  13170. ' this.y = s.y;',
  13171. ' return this;',
  13172. ' };',
  13173. '}, true);',
  13174. 'rtl.recNewT(this, "TRec", function () {',
  13175. ' this.i = 0;',
  13176. ' this.m$a$clone = function (a) {',
  13177. ' var b = [];',
  13178. ' b.length = 2;',
  13179. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13180. ' return b;',
  13181. ' };',
  13182. ' this.$new = function () {',
  13183. ' var r = Object.create(this);',
  13184. ' r.a = [];',
  13185. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13186. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13187. ' r.p = $mod.TPoint.$new();',
  13188. ' return r;',
  13189. ' };',
  13190. ' this.$eq = function (b) {',
  13191. ' 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);',
  13192. ' };',
  13193. ' this.$assign = function (s) {',
  13194. ' this.i = s.i;',
  13195. ' this.a = rtl.arrayRef(s.a);',
  13196. ' this.s = s.s.slice(0);',
  13197. ' this.m = this.m$a$clone(s.m);',
  13198. ' this.p.$assign(s.p);',
  13199. ' return this;',
  13200. ' };',
  13201. '});',
  13202. 'this.r = this.TRec.$clone({',
  13203. ' i: 1,',
  13204. ' a: [2, 3],',
  13205. ' s: [4, 5],',
  13206. ' m: [[11, 12], [13, 14]],',
  13207. ' p: this.TPoint.$clone({',
  13208. ' x: 21,',
  13209. ' y: 0',
  13210. ' })',
  13211. '});',
  13212. 'this.p = [this.TPoint.$clone({',
  13213. ' x: 1,',
  13214. ' y: 2',
  13215. '}), this.TPoint.$clone({',
  13216. ' x: 3,',
  13217. ' y: 4',
  13218. '})];',
  13219. '']),
  13220. LinesToStr([ // $mod.$main
  13221. '']));
  13222. end;
  13223. procedure TTestModule.TestAdvRecord_ExternalField;
  13224. begin
  13225. StartProgram(false);
  13226. Add([
  13227. '{$modeswitch AdvancedRecords}',
  13228. '{$modeswitch externalclass}',
  13229. 'type',
  13230. ' TCar = record',
  13231. ' public',
  13232. ' Intern: longint external name ''$Intern'';',
  13233. ' Intern2: longint external name ''$Intern2'';',
  13234. ' Bracket: longint external name ''["A B"]'';',
  13235. ' procedure DoIt;',
  13236. ' end;',
  13237. 'procedure tcar.doit;',
  13238. 'begin',
  13239. ' Intern:=Intern+1;',
  13240. ' Intern2:=Intern2+2;',
  13241. ' Bracket:=Bracket+3;',
  13242. 'end;',
  13243. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13244. 'begin',
  13245. ' Rec.intern:=Rec.intern+1;',
  13246. ' Rec.intern2:=Rec.intern2+2;',
  13247. ' Rec.Bracket:=Rec.Bracket+3;',
  13248. ' with Rec do begin',
  13249. ' intern:=intern+1;',
  13250. ' intern2:=intern2+2;',
  13251. ' Bracket:=Bracket+3;',
  13252. ' end;']);
  13253. ConvertProgram;
  13254. CheckSource('TestAdvRecord_ExternalField',
  13255. LinesToStr([ // statements
  13256. 'rtl.recNewT(this, "TCar", function () {',
  13257. ' this.$eq = function (b) {',
  13258. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13259. ' };',
  13260. ' this.$assign = function (s) {',
  13261. ' this.$Intern = s.$Intern;',
  13262. ' this.$Intern2 = s.$Intern2;',
  13263. ' this["A B"] = s["A B"];',
  13264. ' return this;',
  13265. ' };',
  13266. ' this.DoIt = function () {',
  13267. ' this.$Intern = this.$Intern + 1;',
  13268. ' this.$Intern2 = this.$Intern2 + 2;',
  13269. ' this["A B"] = this["A B"] + 3;',
  13270. ' };',
  13271. '});',
  13272. 'this.Rec = this.TCar.$clone({',
  13273. ' $Intern: 11,',
  13274. ' $Intern2: 12,',
  13275. ' "A B": 13',
  13276. '});',
  13277. '']),
  13278. LinesToStr([ // $mod.$main
  13279. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13280. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13281. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13282. 'var $with = $mod.Rec;',
  13283. '$with.$Intern = $with.$Intern + 1;',
  13284. '$with.$Intern2 = $with.$Intern2 + 2;',
  13285. '$with["A B"] = $with["A B"] + 3;',
  13286. '']));
  13287. end;
  13288. procedure TTestModule.TestAdvRecord_SubRecord;
  13289. begin
  13290. StartProgram(false);
  13291. Add([
  13292. '{$modeswitch AdvancedRecords}',
  13293. 'type',
  13294. ' TRec = record',
  13295. ' type',
  13296. ' TPoint = record',
  13297. ' x,y: longint;',
  13298. ' class var Count: nativeint;',
  13299. ' procedure DoIt;',
  13300. ' class procedure DoThat; static;',
  13301. ' end;',
  13302. ' var',
  13303. ' i: longint;',
  13304. ' p: TPoint;',
  13305. ' procedure DoSome;',
  13306. ' end;',
  13307. 'const',
  13308. ' r: TRec = (',
  13309. ' i:1;',
  13310. ' p: (x:21;y:22)',
  13311. ' );',
  13312. 'procedure TRec.DoSome;',
  13313. 'begin',
  13314. ' p.x:=p.y+1;',
  13315. ' p.Count:=p.Count+2;',
  13316. 'end;',
  13317. 'procedure TRec.TPoint.DoIt;',
  13318. 'begin',
  13319. ' Count:=Count+3;',
  13320. 'end;',
  13321. 'class procedure TRec.TPoint.DoThat;',
  13322. 'begin',
  13323. ' Count:=Count+4;',
  13324. 'end;',
  13325. 'begin']);
  13326. ConvertProgram;
  13327. CheckSource('TestAdvRecord_SubRecord',
  13328. LinesToStr([ // statements
  13329. 'rtl.recNewT(this, "TRec", function () {',
  13330. ' rtl.recNewT(this, "TPoint", function () {',
  13331. ' this.x = 0;',
  13332. ' this.y = 0;',
  13333. ' this.Count = 0;',
  13334. ' this.$eq = function (b) {',
  13335. ' return (this.x === b.x) && (this.y === b.y);',
  13336. ' };',
  13337. ' this.$assign = function (s) {',
  13338. ' this.x = s.x;',
  13339. ' this.y = s.y;',
  13340. ' return this;',
  13341. ' };',
  13342. ' this.DoIt = function () {',
  13343. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13344. ' };',
  13345. ' this.DoThat = function () {',
  13346. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13347. ' };',
  13348. ' }, true);',
  13349. ' this.i = 0;',
  13350. ' this.$new = function () {',
  13351. ' var r = Object.create(this);',
  13352. ' r.p = this.TPoint.$new();',
  13353. ' return r;',
  13354. ' };',
  13355. ' this.$eq = function (b) {',
  13356. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13357. ' };',
  13358. ' this.$assign = function (s) {',
  13359. ' this.i = s.i;',
  13360. ' this.p.$assign(s.p);',
  13361. ' return this;',
  13362. ' };',
  13363. ' this.DoSome = function () {',
  13364. ' this.p.x = this.p.y + 1;',
  13365. ' this.TPoint.Count = this.p.Count + 2;',
  13366. ' };',
  13367. '}, true);',
  13368. 'this.r = this.TRec.$clone({',
  13369. ' i: 1,',
  13370. ' p: this.TRec.TPoint.$clone({',
  13371. ' x: 21,',
  13372. ' y: 22',
  13373. ' })',
  13374. '});',
  13375. '']),
  13376. LinesToStr([ // $mod.$main
  13377. '']));
  13378. end;
  13379. procedure TTestModule.TestAdvRecord_SubClass;
  13380. begin
  13381. StartProgram(false);
  13382. Add([
  13383. '{$modeswitch AdvancedRecords}',
  13384. 'type',
  13385. ' TObject = class end;',
  13386. ' TPoint = record',
  13387. ' type',
  13388. ' TBird = class',
  13389. ' procedure DoIt;',
  13390. ' class procedure Glob;',
  13391. ' end;',
  13392. ' procedure DoIt(b: TBird);',
  13393. ' end;',
  13394. 'procedure TPoint.TBird.DoIt;',
  13395. 'begin',
  13396. ' doit;',
  13397. ' self.doit;',
  13398. ' glob;',
  13399. ' self.glob;',
  13400. 'end;',
  13401. 'class procedure TPoint.TBird.Glob;',
  13402. 'begin',
  13403. ' glob;',
  13404. ' self.glob;',
  13405. 'end;',
  13406. 'procedure TPoint.DoIt(b: TBird);',
  13407. 'begin',
  13408. ' b.doit;',
  13409. ' b.glob;',
  13410. ' TBird.glob;',
  13411. 'end;',
  13412. 'begin',
  13413. '']);
  13414. ConvertProgram;
  13415. CheckSource('TestAdvRecord_SubClass',
  13416. LinesToStr([ // statements
  13417. 'rtl.createClass(this, "TObject", null, function () {',
  13418. ' this.$init = function () {',
  13419. ' };',
  13420. ' this.$final = function () {',
  13421. ' };',
  13422. '});',
  13423. 'rtl.recNewT(this, "TPoint", function () {',
  13424. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13425. ' this.DoIt = function () {',
  13426. ' this.DoIt();',
  13427. ' this.DoIt();',
  13428. ' this.$class.Glob();',
  13429. ' this.$class.Glob();',
  13430. ' };',
  13431. ' this.Glob = function () {',
  13432. ' this.Glob();',
  13433. ' this.Glob();',
  13434. ' };',
  13435. ' }, "TPoint.TBird");',
  13436. ' this.$eq = function (b) {',
  13437. ' return true;',
  13438. ' };',
  13439. ' this.$assign = function (s) {',
  13440. ' return this;',
  13441. ' };',
  13442. ' this.DoIt = function (b) {',
  13443. ' b.DoIt();',
  13444. ' b.$class.Glob();',
  13445. ' this.TBird.Glob();',
  13446. ' };',
  13447. '}, true);',
  13448. '']),
  13449. LinesToStr([ // $mod.$main
  13450. '']));
  13451. end;
  13452. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13453. begin
  13454. StartProgram(false);
  13455. Add([
  13456. '{$modeswitch AdvancedRecords}',
  13457. 'type',
  13458. ' IUnknown = interface end;',
  13459. ' TPoint = record',
  13460. ' type IBird = interface end;',
  13461. ' end;',
  13462. 'begin',
  13463. '']);
  13464. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13465. nNotYetImplemented);
  13466. ParseProgram;
  13467. end;
  13468. procedure TTestModule.TestAdvRecord_Constructor;
  13469. begin
  13470. StartProgram(false);
  13471. Add([
  13472. '{$modeswitch AdvancedRecords}',
  13473. 'type',
  13474. ' TPoint = record',
  13475. ' x,y: longint;',
  13476. ' class procedure Run(w: longint = 13); static;',
  13477. ' constructor Create(ax: longint; ay: longint = -1);',
  13478. ' end;',
  13479. 'class procedure tpoint.run(w: longint);',
  13480. 'begin',
  13481. ' run;',
  13482. ' run();',
  13483. 'end;',
  13484. 'constructor tpoint.create(ax,ay: longint);',
  13485. 'begin',
  13486. ' x:=ax;',
  13487. ' self.y:=ay;',
  13488. ' run;',
  13489. ' run(ax);',
  13490. 'end;',
  13491. 'var r: TPoint;',
  13492. 'begin',
  13493. ' r:=TPoint.Create(1,2);',
  13494. ' with TPoint do r:=Create(1,2);',
  13495. ' r.Create(3);',
  13496. ' r:=r.Create(4);',
  13497. '']);
  13498. ConvertProgram;
  13499. CheckSource('TestAdvRecord_Constructor',
  13500. LinesToStr([ // statements
  13501. 'rtl.recNewT(this, "TPoint", function () {',
  13502. ' this.x = 0;',
  13503. ' this.y = 0;',
  13504. ' this.$eq = function (b) {',
  13505. ' return (this.x === b.x) && (this.y === b.y);',
  13506. ' };',
  13507. ' this.$assign = function (s) {',
  13508. ' this.x = s.x;',
  13509. ' this.y = s.y;',
  13510. ' return this;',
  13511. ' };',
  13512. ' this.Run = function (w) {',
  13513. ' $mod.TPoint.Run(13);',
  13514. ' $mod.TPoint.Run(13);',
  13515. ' };',
  13516. ' this.Create = function (ax, ay) {',
  13517. ' this.x = ax;',
  13518. ' this.y = ay;',
  13519. ' this.Run(13);',
  13520. ' this.Run(ax);',
  13521. ' return this;',
  13522. ' };',
  13523. '});',
  13524. 'this.r = this.TPoint.$new();',
  13525. '']),
  13526. LinesToStr([ // $mod.$main
  13527. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13528. 'var $with = $mod.TPoint;',
  13529. '$mod.r.$assign($with.$new().Create(1, 2));',
  13530. '$mod.r.Create(3, -1);',
  13531. '$mod.r.$assign($mod.r.Create(4, -1));',
  13532. '']));
  13533. end;
  13534. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13535. begin
  13536. StartProgram(false);
  13537. Add([
  13538. '{$modeswitch AdvancedRecords}',
  13539. 'type',
  13540. ' TPoint = record',
  13541. ' class var x: longint;',
  13542. ' class procedure Fly; static;',
  13543. ' class constructor Init;',
  13544. ' end;',
  13545. 'var count: word;',
  13546. 'class procedure Tpoint.Fly;',
  13547. 'begin',
  13548. 'end;',
  13549. 'class constructor tpoint.init;',
  13550. 'begin',
  13551. ' count:=count+1;',
  13552. ' x:=x+3;',
  13553. ' tpoint.x:=tpoint.x+4;',
  13554. ' fly;',
  13555. ' tpoint.fly;',
  13556. 'end;',
  13557. 'var r: TPoint;',
  13558. 'begin',
  13559. ' r.x:=r.x+10;',
  13560. ' r.Fly;',
  13561. ' r.Fly();',
  13562. '']);
  13563. ConvertProgram;
  13564. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13565. LinesToStr([ // statements
  13566. 'rtl.recNewT(this, "TPoint", function () {',
  13567. ' this.x = 0;',
  13568. ' this.$eq = function (b) {',
  13569. ' return true;',
  13570. ' };',
  13571. ' this.$assign = function (s) {',
  13572. ' return this;',
  13573. ' };',
  13574. ' this.Fly = function () {',
  13575. ' };',
  13576. '}, true);',
  13577. 'this.count = 0;',
  13578. 'this.r = this.TPoint.$new();',
  13579. '']),
  13580. LinesToStr([ // $mod.$main
  13581. '(function () {',
  13582. ' $mod.count = $mod.count + 1;',
  13583. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13584. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13585. ' $mod.TPoint.Fly();',
  13586. ' $mod.TPoint.Fly();',
  13587. '})();',
  13588. '$mod.TPoint.x = $mod.r.x + 10;',
  13589. '$mod.TPoint.Fly();',
  13590. '$mod.TPoint.Fly();',
  13591. '']));
  13592. end;
  13593. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13594. begin
  13595. StartUnit(false);
  13596. Add([
  13597. 'interface',
  13598. '{$modeswitch AdvancedRecords}',
  13599. 'type',
  13600. ' TPoint = record',
  13601. ' class var x: longint;',
  13602. ' class procedure Fly; static;',
  13603. ' class constructor Init;',
  13604. ' end;',
  13605. 'implementation',
  13606. 'var count: word;',
  13607. 'class procedure Tpoint.Fly;',
  13608. 'begin',
  13609. 'end;',
  13610. 'class constructor tpoint.init;',
  13611. 'begin',
  13612. ' count:=count+1;',
  13613. ' x:=3;',
  13614. ' tpoint.x:=4;',
  13615. ' fly;',
  13616. ' tpoint.fly;',
  13617. 'end;',
  13618. '']);
  13619. ConvertUnit;
  13620. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13621. LinesToStr([ // statements
  13622. 'var $impl = $mod.$impl;',
  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. '']),
  13635. LinesToStr([ // $mod.$init
  13636. '(function () {',
  13637. ' $impl.count = $impl.count + 1;',
  13638. ' $mod.TPoint.x = 3;',
  13639. ' $mod.TPoint.x = 4;',
  13640. ' $mod.TPoint.Fly();',
  13641. ' $mod.TPoint.Fly();',
  13642. '})();',
  13643. '']),
  13644. LinesToStr([ // $mod.$main
  13645. '$impl.count = 0;',
  13646. '']));
  13647. end;
  13648. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13649. begin
  13650. StartProgram(false);
  13651. Add(['type',
  13652. ' TObject = class',
  13653. ' public',
  13654. ' constructor Create;',
  13655. ' destructor Destroy;',
  13656. ' end;',
  13657. ' TBird = TObject;',
  13658. 'constructor tobject.create;',
  13659. 'begin end;',
  13660. 'destructor tobject.destroy;',
  13661. 'begin end;',
  13662. 'var Obj: tobject;',
  13663. 'begin',
  13664. ' obj:=tobject.create;',
  13665. ' obj:=tobject.create();',
  13666. ' obj:=tbird.create;',
  13667. ' obj:=tbird.create();',
  13668. ' obj:=obj.create();',
  13669. ' obj.destroy;',
  13670. '']);
  13671. ConvertProgram;
  13672. CheckSource('TestClass_TObjectDefaultConstructor',
  13673. LinesToStr([ // statements
  13674. 'rtl.createClass(this,"TObject",null,function(){',
  13675. ' this.$init = function () {',
  13676. ' };',
  13677. ' this.$final = function () {',
  13678. ' };',
  13679. ' this.Create = function(){',
  13680. ' return this;',
  13681. ' };',
  13682. ' this.Destroy = function(){',
  13683. ' };',
  13684. '});',
  13685. 'this.Obj = null;'
  13686. ]),
  13687. LinesToStr([ // $mod.$main
  13688. '$mod.Obj = $mod.TObject.$create("Create");',
  13689. '$mod.Obj = $mod.TObject.$create("Create");',
  13690. '$mod.Obj = $mod.TObject.$create("Create");',
  13691. '$mod.Obj = $mod.TObject.$create("Create");',
  13692. '$mod.Obj = $mod.Obj.Create();',
  13693. '$mod.Obj.$destroy("Destroy");',
  13694. '']));
  13695. end;
  13696. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13697. begin
  13698. StartProgram(false);
  13699. Add('type');
  13700. Add(' TObject = class');
  13701. Add(' public');
  13702. Add(' constructor Create(Par: longint);');
  13703. Add(' end;');
  13704. Add('constructor tobject.create(par: longint);');
  13705. Add('begin end;');
  13706. Add('var Obj: tobject;');
  13707. Add('begin');
  13708. Add(' obj:=tobject.create(3);');
  13709. ConvertProgram;
  13710. CheckSource('TestClass_TObjectConstructorWithParams',
  13711. LinesToStr([ // statements
  13712. 'rtl.createClass(this,"TObject",null,function(){',
  13713. ' this.$init = function () {',
  13714. ' };',
  13715. ' this.$final = function () {',
  13716. ' };',
  13717. ' this.Create = function(Par){',
  13718. ' return this;',
  13719. ' };',
  13720. '});',
  13721. 'this.Obj = null;'
  13722. ]),
  13723. LinesToStr([ // $mod.$main
  13724. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13725. ]));
  13726. end;
  13727. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13728. begin
  13729. StartProgram(false);
  13730. Add('type');
  13731. Add(' TObject = class');
  13732. Add(' public');
  13733. Add(' constructor Create;');
  13734. Add(' end;');
  13735. Add(' TTest = class(TObject)');
  13736. Add(' public');
  13737. Add(' constructor Create(const Par: longint = 1);');
  13738. Add(' end;');
  13739. Add('constructor tobject.create;');
  13740. Add('begin end;');
  13741. Add('constructor ttest.create(const par: longint);');
  13742. Add('begin end;');
  13743. Add('var t: ttest;');
  13744. Add('begin');
  13745. Add(' t:=ttest.create;');
  13746. Add(' t:=ttest.create(2);');
  13747. ConvertProgram;
  13748. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13749. LinesToStr([ // statements
  13750. 'rtl.createClass(this,"TObject",null,function(){',
  13751. ' this.$init = function () {',
  13752. ' };',
  13753. ' this.$final = function () {',
  13754. ' };',
  13755. ' this.Create = function(){',
  13756. ' return this;',
  13757. ' };',
  13758. '});',
  13759. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13760. ' this.Create$1 = function (Par) {',
  13761. ' return this;',
  13762. ' };',
  13763. '});',
  13764. 'this.t = null;'
  13765. ]),
  13766. LinesToStr([ // $mod.$main
  13767. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13768. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13769. ]));
  13770. end;
  13771. procedure TTestModule.TestClass_Var;
  13772. begin
  13773. StartProgram(false);
  13774. Add([
  13775. 'type',
  13776. ' TObject = class',
  13777. ' public',
  13778. ' vI: longint;',
  13779. ' constructor Create(Par: longint);',
  13780. ' end;',
  13781. 'constructor tobject.create(par: longint);',
  13782. 'begin',
  13783. ' vi:=par+3',
  13784. 'end;',
  13785. 'var Obj: tobject;',
  13786. 'begin',
  13787. ' obj:=tobject.create(4);',
  13788. ' obj.vi:=obj.VI+5;']);
  13789. ConvertProgram;
  13790. CheckSource('TestClass_Var',
  13791. LinesToStr([ // statements
  13792. 'rtl.createClass(this,"TObject",null,function(){',
  13793. ' this.$init = function () {',
  13794. ' this.vI = 0;',
  13795. ' };',
  13796. ' this.$final = function () {',
  13797. ' };',
  13798. ' this.Create = function(Par){',
  13799. ' this.vI = Par+3;',
  13800. ' return this;',
  13801. ' };',
  13802. '});',
  13803. 'this.Obj = null;'
  13804. ]),
  13805. LinesToStr([ // $mod.$main
  13806. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13807. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13808. ]));
  13809. end;
  13810. procedure TTestModule.TestClass_Method;
  13811. begin
  13812. StartProgram(false);
  13813. Add('type');
  13814. Add(' TObject = class');
  13815. Add(' public');
  13816. Add(' vI: longint;');
  13817. Add(' Sub: TObject;');
  13818. Add(' constructor Create;');
  13819. Add(' function GetIt(Par: longint): tobject;');
  13820. Add(' end;');
  13821. Add('constructor tobject.create; begin end;');
  13822. Add('function tobject.getit(par: longint): tobject;');
  13823. Add('begin');
  13824. Add(' Self.vi:=par+3;');
  13825. Add(' Result:=self.sub;');
  13826. Add('end;');
  13827. Add('var Obj: tobject;');
  13828. Add('begin');
  13829. Add(' obj:=tobject.create;');
  13830. Add(' obj.getit(4);');
  13831. Add(' obj.sub.sub:=nil;');
  13832. Add(' obj.sub.getit(5);');
  13833. Add(' obj.sub.getit(6).SUB:=nil;');
  13834. Add(' obj.sub.getit(7).GETIT(8);');
  13835. Add(' obj.sub.getit(9).SuB.getit(10);');
  13836. ConvertProgram;
  13837. CheckSource('TestClass_Method',
  13838. LinesToStr([ // statements
  13839. 'rtl.createClass(this,"TObject",null,function(){',
  13840. ' this.$init = function () {',
  13841. ' this.vI = 0;',
  13842. ' this.Sub = null;',
  13843. ' };',
  13844. ' this.$final = function () {',
  13845. ' this.Sub = undefined;',
  13846. ' };',
  13847. ' this.Create = function(){',
  13848. ' return this;',
  13849. ' };',
  13850. ' this.GetIt = function(Par){',
  13851. ' var Result = null;',
  13852. ' this.vI = Par + 3;',
  13853. ' Result = this.Sub;',
  13854. ' return Result;',
  13855. ' };',
  13856. '});',
  13857. 'this.Obj = null;'
  13858. ]),
  13859. LinesToStr([ // $mod.$main
  13860. '$mod.Obj = $mod.TObject.$create("Create");',
  13861. '$mod.Obj.GetIt(4);',
  13862. '$mod.Obj.Sub.Sub=null;',
  13863. '$mod.Obj.Sub.GetIt(5);',
  13864. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13865. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13866. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13867. ]));
  13868. end;
  13869. procedure TTestModule.TestClass_Implementation;
  13870. begin
  13871. StartUnit(false);
  13872. Add([
  13873. 'interface',
  13874. 'type',
  13875. ' TObject = class',
  13876. ' constructor Create;',
  13877. ' end;',
  13878. 'implementation',
  13879. 'type',
  13880. ' TIntClass = class',
  13881. ' constructor Create; reintroduce;',
  13882. ' class procedure DoGlob;',
  13883. ' end;',
  13884. 'constructor tintclass.create;',
  13885. 'begin',
  13886. ' inherited;',
  13887. ' inherited create;',
  13888. ' doglob;',
  13889. 'end;',
  13890. 'class procedure tintclass.doglob;',
  13891. 'begin',
  13892. 'end;',
  13893. 'constructor tobject.create;',
  13894. 'var',
  13895. ' iC: tintclass;',
  13896. 'begin',
  13897. ' ic:=tintclass.create;',
  13898. ' tintclass.doglob;',
  13899. ' ic.doglob;',
  13900. 'end;',
  13901. 'initialization',
  13902. ' tintclass.doglob;',
  13903. '']);
  13904. ConvertUnit;
  13905. CheckSource('TestClass_Implementation',
  13906. LinesToStr([ // statements
  13907. 'var $impl = $mod.$impl;',
  13908. 'rtl.createClass(this, "TObject", null, function () {',
  13909. ' this.$init = function () {',
  13910. ' };',
  13911. ' this.$final = function () {',
  13912. ' };',
  13913. ' this.Create = function () {',
  13914. ' var iC = null;',
  13915. ' iC = $impl.TIntClass.$create("Create$1");',
  13916. ' $impl.TIntClass.DoGlob();',
  13917. ' iC.$class.DoGlob();',
  13918. ' return this;',
  13919. ' };',
  13920. '});',
  13921. '']),
  13922. LinesToStr([ // $mod.$main
  13923. '$impl.TIntClass.DoGlob();',
  13924. '']),
  13925. LinesToStr([
  13926. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  13927. ' this.Create$1 = function () {',
  13928. ' $mod.TObject.Create.call(this);',
  13929. ' $mod.TObject.Create.call(this);',
  13930. ' this.$class.DoGlob();',
  13931. ' return this;',
  13932. ' };',
  13933. ' this.DoGlob = function () {',
  13934. ' };',
  13935. '});',
  13936. '']));
  13937. end;
  13938. procedure TTestModule.TestClass_Inheritance;
  13939. begin
  13940. StartProgram(false);
  13941. Add('type');
  13942. Add(' TObject = class');
  13943. Add(' public');
  13944. Add(' constructor Create;');
  13945. Add(' end;');
  13946. Add(' TClassA = class');
  13947. Add(' end;');
  13948. Add(' TClassB = class(TObject)');
  13949. Add(' procedure ProcB;');
  13950. Add(' end;');
  13951. Add('constructor tobject.create; begin end;');
  13952. Add('procedure tclassb.procb; begin end;');
  13953. Add('var');
  13954. Add(' oO: TObject;');
  13955. Add(' oA: TClassA;');
  13956. Add(' oB: TClassB;');
  13957. Add('begin');
  13958. Add(' oO:=tobject.Create;');
  13959. Add(' oA:=tclassa.Create;');
  13960. Add(' ob:=tclassb.Create;');
  13961. Add(' if oo is tclassa then ;');
  13962. Add(' ob:=oo as tclassb;');
  13963. Add(' (oo as tclassb).procb;');
  13964. ConvertProgram;
  13965. CheckSource('TestClass_Inheritance',
  13966. LinesToStr([ // statements
  13967. 'rtl.createClass(this,"TObject",null,function(){',
  13968. ' this.$init = function () {',
  13969. ' };',
  13970. ' this.$final = function () {',
  13971. ' };',
  13972. ' this.Create = function () {',
  13973. ' return this;',
  13974. ' };',
  13975. '});',
  13976. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  13977. '});',
  13978. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  13979. ' this.ProcB = function () {',
  13980. ' };',
  13981. '});',
  13982. 'this.oO = null;',
  13983. 'this.oA = null;',
  13984. 'this.oB = null;'
  13985. ]),
  13986. LinesToStr([ // $mod.$main
  13987. '$mod.oO = $mod.TObject.$create("Create");',
  13988. '$mod.oA = $mod.TClassA.$create("Create");',
  13989. '$mod.oB = $mod.TClassB.$create("Create");',
  13990. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  13991. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  13992. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  13993. ]));
  13994. end;
  13995. procedure TTestModule.TestClass_TypeAlias;
  13996. begin
  13997. StartProgram(false);
  13998. Add([
  13999. '{$interfaces corba}',
  14000. 'type',
  14001. ' IObject = interface',
  14002. ' end;',
  14003. ' IBird = type IObject;',
  14004. ' TObject = class',
  14005. ' end;',
  14006. ' TBird = type TObject;',
  14007. 'var',
  14008. ' oObj: TObject;',
  14009. ' oBird: TBird;',
  14010. ' IntfObj: IObject;',
  14011. ' IntfBird: IBird;',
  14012. 'begin',
  14013. ' oObj:=oBird;',
  14014. '']);
  14015. ConvertProgram;
  14016. CheckSource('TestClass_TypeAlias',
  14017. LinesToStr([ // statements
  14018. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14019. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14020. 'rtl.createClass(this, "TObject", null, function () {',
  14021. ' this.$init = function () {',
  14022. ' };',
  14023. ' this.$final = function () {',
  14024. ' };',
  14025. '});',
  14026. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14027. '});',
  14028. 'this.oObj = null;',
  14029. 'this.oBird = null;',
  14030. 'this.IntfObj = null;',
  14031. 'this.IntfBird = null;',
  14032. '']),
  14033. LinesToStr([ // $mod.$main
  14034. '$mod.oObj = $mod.oBird;',
  14035. '']));
  14036. end;
  14037. procedure TTestModule.TestClass_AbstractMethod;
  14038. begin
  14039. StartProgram(false);
  14040. Add('type');
  14041. Add(' TObject = class');
  14042. Add(' public');
  14043. Add(' procedure DoIt; virtual; abstract;');
  14044. Add(' end;');
  14045. Add('begin');
  14046. ConvertProgram;
  14047. CheckSource('TestClass_AbstractMethod',
  14048. LinesToStr([ // statements
  14049. 'rtl.createClass(this,"TObject",null,function(){',
  14050. ' this.$init = function () {',
  14051. ' };',
  14052. ' this.$final = function () {',
  14053. ' };',
  14054. '});'
  14055. ]),
  14056. LinesToStr([ // this.$main
  14057. ''
  14058. ]));
  14059. end;
  14060. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14061. begin
  14062. StartProgram(false);
  14063. Add([
  14064. 'type',
  14065. ' TObject = class',
  14066. ' procedure DoAbstract; virtual; abstract;',
  14067. ' procedure DoVirtual; virtual;',
  14068. ' procedure DoIt;',
  14069. ' end;',
  14070. ' TA = class',
  14071. ' procedure doabstract; override;',
  14072. ' procedure dovirtual; override;',
  14073. ' procedure DoSome;',
  14074. ' end;',
  14075. 'procedure tobject.dovirtual;',
  14076. 'begin',
  14077. ' inherited; // call non existing ancestor -> ignore silently',
  14078. 'end;',
  14079. 'procedure tobject.doit;',
  14080. 'begin',
  14081. 'end;',
  14082. 'procedure ta.doabstract;',
  14083. 'begin',
  14084. ' inherited dovirtual; // call TObject.DoVirtual',
  14085. 'end;',
  14086. 'procedure ta.dovirtual;',
  14087. 'begin',
  14088. ' inherited; // call TObject.DoVirtual',
  14089. ' inherited dovirtual; // call TObject.DoVirtual',
  14090. ' inherited dovirtual(); // call TObject.DoVirtual',
  14091. ' doit;',
  14092. ' doit();',
  14093. 'end;',
  14094. 'procedure ta.dosome;',
  14095. 'begin',
  14096. ' inherited; // call non existing ancestor method -> silently ignore',
  14097. 'end;',
  14098. 'begin']);
  14099. ConvertProgram;
  14100. CheckSource('TestClass_CallInherited_ProcNoParams',
  14101. LinesToStr([ // statements
  14102. 'rtl.createClass(this,"TObject",null,function(){',
  14103. ' this.$init = function () {',
  14104. ' };',
  14105. ' this.$final = function () {',
  14106. ' };',
  14107. ' this.DoVirtual = function () {',
  14108. ' };',
  14109. ' this.DoIt = function () {',
  14110. ' };',
  14111. '});',
  14112. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14113. ' this.DoAbstract = function () {',
  14114. ' $mod.TObject.DoVirtual.call(this);',
  14115. ' };',
  14116. ' this.DoVirtual = function () {',
  14117. ' $mod.TObject.DoVirtual.call(this);',
  14118. ' $mod.TObject.DoVirtual.call(this);',
  14119. ' $mod.TObject.DoVirtual.call(this);',
  14120. ' this.DoIt();',
  14121. ' this.DoIt();',
  14122. ' };',
  14123. ' this.DoSome = function () {',
  14124. ' };',
  14125. '});'
  14126. ]),
  14127. LinesToStr([ // this.$main
  14128. ''
  14129. ]));
  14130. end;
  14131. procedure TTestModule.TestClass_CallInherited_WithParams;
  14132. begin
  14133. StartProgram(false);
  14134. Add([
  14135. 'type',
  14136. ' TObject = class',
  14137. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14138. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14139. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14140. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14141. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14142. ' end;',
  14143. ' TClassA = class',
  14144. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14145. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14146. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14147. ' end;',
  14148. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14149. 'begin',
  14150. 'end;',
  14151. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14152. 'begin',
  14153. 'end;',
  14154. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14155. 'begin',
  14156. 'end;',
  14157. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14158. 'begin',
  14159. 'end;',
  14160. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14161. 'begin',
  14162. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14163. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14164. 'end;',
  14165. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14166. 'begin',
  14167. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14168. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14169. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14170. ' doit(pa,pb);',
  14171. ' doit(pa);',
  14172. ' doit2(pa);',
  14173. ' doit2;',
  14174. 'end;',
  14175. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14176. 'begin',
  14177. ' pa:=inherited;',
  14178. 'end;',
  14179. 'begin']);
  14180. ConvertProgram;
  14181. CheckSource('TestClass_CallInherited_WithParams',
  14182. LinesToStr([ // statements
  14183. 'rtl.createClass(this,"TObject",null,function(){',
  14184. ' this.$init = function () {',
  14185. ' };',
  14186. ' this.$final = function () {',
  14187. ' };',
  14188. ' this.DoVirtual = function (pA,pB) {',
  14189. ' };',
  14190. ' this.DoIt = function (pA,pB) {',
  14191. ' };',
  14192. ' this.DoIt2 = function (pA,pB) {',
  14193. ' };',
  14194. ' this.GetIt = function (pA, pB) {',
  14195. ' var Result = 0;',
  14196. ' return Result;',
  14197. ' };',
  14198. '});',
  14199. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14200. ' this.DoAbstract = function (pA,pB) {',
  14201. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14202. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14203. ' };',
  14204. ' this.DoVirtual = function (pA,pB) {',
  14205. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14206. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14207. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14208. ' this.DoIt(pA,pB);',
  14209. ' this.DoIt(pA,0);',
  14210. ' this.DoIt2(pA,2);',
  14211. ' this.DoIt2(1,2);',
  14212. ' };',
  14213. ' this.GetIt$1 = function (pA, pB) {',
  14214. ' var Result = 0;',
  14215. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14216. ' return Result;',
  14217. ' };',
  14218. '});'
  14219. ]),
  14220. LinesToStr([ // this.$main
  14221. ''
  14222. ]));
  14223. end;
  14224. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14225. begin
  14226. StartProgram(false);
  14227. Add('type');
  14228. Add(' TObject = class');
  14229. Add(' constructor Create; virtual;');
  14230. Add(' constructor CreateWithB(b: boolean);');
  14231. Add(' end;');
  14232. Add(' TA = class');
  14233. Add(' constructor Create; override;');
  14234. Add(' constructor CreateWithC(c: char);');
  14235. Add(' procedure DoIt;');
  14236. Add(' class function DoSome: TObject;');
  14237. Add(' end;');
  14238. Add('constructor tobject.create;');
  14239. Add('begin');
  14240. Add(' inherited; // call non existing ancestor -> ignore silently');
  14241. Add('end;');
  14242. Add('constructor tobject.createwithb(b: boolean);');
  14243. Add('begin');
  14244. Add(' inherited; // call non existing ancestor -> ignore silently');
  14245. Add(' create; // normal call');
  14246. Add('end;');
  14247. Add('constructor ta.create;');
  14248. Add('begin');
  14249. Add(' inherited; // normal call TObject.Create');
  14250. Add(' inherited create; // normal call TObject.Create');
  14251. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14252. Add('end;');
  14253. Add('constructor ta.createwithc(c: char);');
  14254. Add('begin');
  14255. Add(' inherited create; // call TObject.Create');
  14256. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14257. Add(' doit;');
  14258. Add(' doit();');
  14259. Add(' dosome;');
  14260. Add('end;');
  14261. Add('procedure ta.doit;');
  14262. Add('begin');
  14263. Add(' create; // normal call');
  14264. Add(' createwithb(false); // normal call');
  14265. Add(' createwithc(''c''); // normal call');
  14266. Add('end;');
  14267. Add('class function ta.dosome: TObject;');
  14268. Add('begin');
  14269. Add(' Result:=create; // constructor');
  14270. Add(' Result:=createwithb(true); // constructor');
  14271. Add(' Result:=createwithc(''c''); // constructor');
  14272. Add('end;');
  14273. Add('begin');
  14274. ConvertProgram;
  14275. CheckSource('TestClass_CallInheritedConstructor',
  14276. LinesToStr([ // statements
  14277. 'rtl.createClass(this,"TObject",null,function(){',
  14278. ' this.$init = function () {',
  14279. ' };',
  14280. ' this.$final = function () {',
  14281. ' };',
  14282. ' this.Create = function () {',
  14283. ' return this;',
  14284. ' };',
  14285. ' this.CreateWithB = function (b) {',
  14286. ' this.Create();',
  14287. ' return this;',
  14288. ' };',
  14289. '});',
  14290. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14291. ' this.Create = function () {',
  14292. ' $mod.TObject.Create.call(this);',
  14293. ' $mod.TObject.Create.call(this);',
  14294. ' $mod.TObject.CreateWithB.call(this, false);',
  14295. ' return this;',
  14296. ' };',
  14297. ' this.CreateWithC = function (c) {',
  14298. ' $mod.TObject.Create.call(this);',
  14299. ' $mod.TObject.CreateWithB.call(this, true);',
  14300. ' this.DoIt();',
  14301. ' this.DoIt();',
  14302. ' this.$class.DoSome();',
  14303. ' return this;',
  14304. ' };',
  14305. ' this.DoIt = function () {',
  14306. ' this.Create();',
  14307. ' this.CreateWithB(false);',
  14308. ' this.CreateWithC("c");',
  14309. ' };',
  14310. ' this.DoSome = function () {',
  14311. ' var Result = null;',
  14312. ' Result = this.$create("Create");',
  14313. ' Result = this.$create("CreateWithB", [true]);',
  14314. ' Result = this.$create("CreateWithC", ["c"]);',
  14315. ' return Result;',
  14316. ' };',
  14317. '});'
  14318. ]),
  14319. LinesToStr([ // this.$main
  14320. ''
  14321. ]));
  14322. end;
  14323. procedure TTestModule.TestClass_ClassVar_Assign;
  14324. begin
  14325. StartProgram(false);
  14326. Add([
  14327. 'type',
  14328. ' TObject = class',
  14329. ' public',
  14330. ' class var vI: longint;',
  14331. ' class var Sub: TObject;',
  14332. ' constructor Create;',
  14333. ' class function GetIt(var Par: longint): tobject;',
  14334. ' end;',
  14335. 'constructor tobject.create;',
  14336. 'begin',
  14337. ' vi:=vi+1;',
  14338. ' Self.vi:=Self.vi+1;',
  14339. ' inc(vi);',
  14340. 'end;',
  14341. 'class function tobject.getit(var par: longint): tobject;',
  14342. 'begin',
  14343. ' vi:=vi+3;',
  14344. ' Self.vi:=Self.vi+4;',
  14345. ' inc(vi);',
  14346. ' Result:=self.sub;',
  14347. ' GetIt(vi);',
  14348. 'end;',
  14349. 'var Obj: tobject;',
  14350. 'begin',
  14351. ' obj:=tobject.create;',
  14352. ' tobject.vi:=3;',
  14353. ' if tobject.vi=4 then ;',
  14354. ' tobject.sub:=nil;',
  14355. ' obj.sub:=nil;',
  14356. ' obj.sub.sub:=nil;']);
  14357. ConvertProgram;
  14358. CheckSource('TestClass_ClassVar_Assign',
  14359. LinesToStr([ // statements
  14360. 'rtl.createClass(this,"TObject",null,function(){',
  14361. ' this.vI = 0;',
  14362. ' this.Sub = null;',
  14363. ' this.$init = function () {',
  14364. ' };',
  14365. ' this.$final = function () {',
  14366. ' };',
  14367. ' this.Create = function(){',
  14368. ' $mod.TObject.vI = this.vI+1;',
  14369. ' $mod.TObject.vI = this.vI+1;',
  14370. ' $mod.TObject.vI += 1;',
  14371. ' return this;',
  14372. ' };',
  14373. ' this.GetIt = function(Par){',
  14374. ' var Result = null;',
  14375. ' $mod.TObject.vI = this.vI + 3;',
  14376. ' $mod.TObject.vI = this.vI + 4;',
  14377. ' $mod.TObject.vI += 1;',
  14378. ' Result = this.Sub;',
  14379. ' this.GetIt({',
  14380. ' p: $mod.TObject,',
  14381. ' get: function () {',
  14382. ' return this.p.vI;',
  14383. ' },',
  14384. ' set: function (v) {',
  14385. ' this.p.vI = v;',
  14386. ' }',
  14387. ' });',
  14388. ' return Result;',
  14389. ' };',
  14390. '});',
  14391. 'this.Obj = null;'
  14392. ]),
  14393. LinesToStr([ // $mod.$main
  14394. '$mod.Obj = $mod.TObject.$create("Create");',
  14395. '$mod.TObject.vI = 3;',
  14396. 'if ($mod.TObject.vI === 4);',
  14397. '$mod.TObject.Sub=null;',
  14398. '$mod.TObject.Sub=null;',
  14399. '$mod.TObject.Sub=null;',
  14400. '']));
  14401. end;
  14402. procedure TTestModule.TestClass_CallClassMethod;
  14403. begin
  14404. StartProgram(false);
  14405. Add('type');
  14406. Add(' TObject = class');
  14407. Add(' public');
  14408. Add(' class var vI: longint;');
  14409. Add(' class var Sub: TObject;');
  14410. Add(' constructor Create;');
  14411. Add(' function GetMore(Par: longint): longint;');
  14412. Add(' class function GetIt(Par: longint): tobject;');
  14413. Add(' end;');
  14414. Add('constructor tobject.create;');
  14415. Add('begin');
  14416. Add(' sub:=getit(3);');
  14417. Add(' vi:=getmore(4);');
  14418. Add(' sub:=Self.getit(5);');
  14419. Add(' vi:=Self.getmore(6);');
  14420. Add('end;');
  14421. Add('function tobject.getmore(par: longint): longint;');
  14422. Add('begin');
  14423. Add(' sub:=getit(11);');
  14424. Add(' vi:=getmore(12);');
  14425. Add(' sub:=self.getit(13);');
  14426. Add(' vi:=self.getmore(14);');
  14427. Add('end;');
  14428. Add('class function tobject.getit(par: longint): tobject;');
  14429. Add('begin');
  14430. Add(' sub:=getit(21);');
  14431. Add(' vi:=sub.getmore(22);');
  14432. Add(' sub:=self.getit(23);');
  14433. Add(' vi:=self.sub.getmore(24);');
  14434. Add('end;');
  14435. Add('var Obj: tobject;');
  14436. Add('begin');
  14437. Add(' obj:=tobject.create;');
  14438. Add(' tobject.getit(5);');
  14439. Add(' obj.getit(6);');
  14440. Add(' obj.sub.getit(7);');
  14441. Add(' obj.sub.getit(8).SUB:=nil;');
  14442. Add(' obj.sub.getit(9).GETIT(10);');
  14443. Add(' obj.sub.getit(11).SuB.getit(12);');
  14444. ConvertProgram;
  14445. CheckSource('TestClass_CallClassMethod',
  14446. LinesToStr([ // statements
  14447. 'rtl.createClass(this,"TObject",null,function(){',
  14448. ' this.vI = 0;',
  14449. ' this.Sub = null;',
  14450. ' this.$init = function () {',
  14451. ' };',
  14452. ' this.$final = function () {',
  14453. ' };',
  14454. ' this.Create = function(){',
  14455. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14456. ' $mod.TObject.vI = this.GetMore(4);',
  14457. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14458. ' $mod.TObject.vI = this.GetMore(6);',
  14459. ' return this;',
  14460. ' };',
  14461. ' this.GetMore = function(Par){',
  14462. ' var Result = 0;',
  14463. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14464. ' $mod.TObject.vI = this.GetMore(12);',
  14465. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14466. ' $mod.TObject.vI = this.GetMore(14);',
  14467. ' return Result;',
  14468. ' };',
  14469. ' this.GetIt = function(Par){',
  14470. ' var Result = null;',
  14471. ' $mod.TObject.Sub = this.GetIt(21);',
  14472. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14473. ' $mod.TObject.Sub = this.GetIt(23);',
  14474. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14475. ' return Result;',
  14476. ' };',
  14477. '});',
  14478. 'this.Obj = null;'
  14479. ]),
  14480. LinesToStr([ // $mod.$main
  14481. '$mod.Obj = $mod.TObject.$create("Create");',
  14482. '$mod.TObject.GetIt(5);',
  14483. '$mod.Obj.$class.GetIt(6);',
  14484. '$mod.Obj.Sub.$class.GetIt(7);',
  14485. '$mod.TObject.Sub=null;',
  14486. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14487. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14488. '']));
  14489. end;
  14490. procedure TTestModule.TestClass_CallClassMethodStatic;
  14491. begin
  14492. StartProgram(false);
  14493. Add([
  14494. 'type',
  14495. ' TObject = class',
  14496. ' public',
  14497. ' class function Fly: tobject; static;',
  14498. ' end;',
  14499. 'class function tobject.Fly: tobject;',
  14500. 'begin',
  14501. ' Result.Fly;',
  14502. ' Result.Fly();',
  14503. ' Fly;',
  14504. ' Fly();',
  14505. ' Fly.Fly;',
  14506. ' Fly.Fly();',
  14507. 'end;',
  14508. 'var Obj: tobject;',
  14509. 'begin',
  14510. ' obj.Fly;',
  14511. ' obj.Fly();',
  14512. ' with obj do begin',
  14513. ' Fly;',
  14514. ' Fly();',
  14515. ' end;',
  14516. '']);
  14517. ConvertProgram;
  14518. CheckSource('TestClass_CallClassMethodStatic',
  14519. LinesToStr([ // statements
  14520. 'rtl.createClass(this, "TObject", null, function () {',
  14521. ' this.$init = function () {',
  14522. ' };',
  14523. ' this.$final = function () {',
  14524. ' };',
  14525. ' this.Fly = function () {',
  14526. ' var Result = null;',
  14527. ' $mod.TObject.Fly();',
  14528. ' $mod.TObject.Fly();',
  14529. ' $mod.TObject.Fly();',
  14530. ' $mod.TObject.Fly();',
  14531. ' $mod.TObject.Fly();',
  14532. ' $mod.TObject.Fly();',
  14533. ' return Result;',
  14534. ' };',
  14535. '});',
  14536. 'this.Obj = null;'
  14537. ]),
  14538. LinesToStr([ // $mod.$main
  14539. '$mod.TObject.Fly();',
  14540. '$mod.TObject.Fly();',
  14541. 'var $with = $mod.Obj;',
  14542. '$with.Fly();',
  14543. '$with.Fly();',
  14544. '']));
  14545. end;
  14546. procedure TTestModule.TestClass_Property;
  14547. begin
  14548. StartProgram(false);
  14549. Add('type');
  14550. Add(' TObject = class');
  14551. Add(' Fx: longint;');
  14552. Add(' Fy: longint;');
  14553. Add(' function GetInt: longint;');
  14554. Add(' procedure SetInt(Value: longint);');
  14555. Add(' procedure DoIt;');
  14556. Add(' property IntA: longint read Fx write Fy;');
  14557. Add(' property IntB: longint read GetInt write SetInt;');
  14558. Add(' end;');
  14559. Add('function tobject.getint: longint;');
  14560. Add('begin');
  14561. Add(' result:=fx;');
  14562. Add('end;');
  14563. Add('procedure tobject.setint(value: longint);');
  14564. Add('begin');
  14565. Add(' if value=fy then exit;');
  14566. Add(' fy:=value;');
  14567. Add('end;');
  14568. Add('procedure tobject.doit;');
  14569. Add('begin');
  14570. Add(' IntA:=IntA+1;');
  14571. Add(' Self.IntA:=Self.IntA+1;');
  14572. Add(' IntB:=IntB+1;');
  14573. Add(' Self.IntB:=Self.IntB+1;');
  14574. Add('end;');
  14575. Add('var Obj: tobject;');
  14576. Add('begin');
  14577. Add(' obj.inta:=obj.inta+1;');
  14578. Add(' if obj.intb=2 then;');
  14579. Add(' obj.intb:=obj.intb+2;');
  14580. Add(' obj.setint(obj.inta);');
  14581. ConvertProgram;
  14582. CheckSource('TestClass_Property',
  14583. LinesToStr([ // statements
  14584. 'rtl.createClass(this, "TObject", null, function () {',
  14585. ' this.$init = function () {',
  14586. ' this.Fx = 0;',
  14587. ' this.Fy = 0;',
  14588. ' };',
  14589. ' this.$final = function () {',
  14590. ' };',
  14591. ' this.GetInt = function () {',
  14592. ' var Result = 0;',
  14593. ' Result = this.Fx;',
  14594. ' return Result;',
  14595. ' };',
  14596. ' this.SetInt = function (Value) {',
  14597. ' if (Value === this.Fy) return;',
  14598. ' this.Fy = Value;',
  14599. ' };',
  14600. ' this.DoIt = function () {',
  14601. ' this.Fy = this.Fx + 1;',
  14602. ' this.Fy = this.Fx + 1;',
  14603. ' this.SetInt(this.GetInt() + 1);',
  14604. ' this.SetInt(this.GetInt() + 1);',
  14605. ' };',
  14606. '});',
  14607. 'this.Obj = null;'
  14608. ]),
  14609. LinesToStr([ // $mod.$main
  14610. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14611. 'if ($mod.Obj.GetInt() === 2);',
  14612. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14613. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14614. ]));
  14615. end;
  14616. procedure TTestModule.TestClass_Property_ClassMethod;
  14617. begin
  14618. StartProgram(false);
  14619. Add([
  14620. 'type',
  14621. ' TObject = class',
  14622. ' class var Fx: longint;',
  14623. ' class var Fy: longint;',
  14624. ' class function GetInt: longint;',
  14625. ' class procedure SetInt(Value: longint);',
  14626. ' end;',
  14627. ' TBird = class',
  14628. ' class procedure DoIt;',
  14629. ' class property IntA: longint read Fx write Fy;',
  14630. ' class property IntB: longint read GetInt write SetInt;',
  14631. ' end;',
  14632. 'class function tobject.getint: longint;',
  14633. 'begin',
  14634. ' result:=fx;',
  14635. 'end;',
  14636. 'class procedure tobject.setint(value: longint);',
  14637. 'begin',
  14638. 'end;',
  14639. 'class procedure tbird.doit;',
  14640. 'begin',
  14641. ' FX:=3;',
  14642. ' IntA:=IntA+1;',
  14643. ' Self.IntA:=Self.IntA+1;',
  14644. ' IntB:=IntB+1;',
  14645. ' Self.IntB:=Self.IntB+1;',
  14646. ' with Self do begin',
  14647. ' FX:=11;',
  14648. ' IntA:=IntA+12;',
  14649. ' IntB:=IntB+13;',
  14650. ' end;',
  14651. 'end;',
  14652. 'var Obj: tbird;',
  14653. 'begin',
  14654. ' tbird.fx:=tbird.fx+1;',
  14655. ' tbird.inta:=tbird.inta+1;',
  14656. ' if tbird.intb=2 then;',
  14657. ' tbird.intb:=tbird.intb+2;',
  14658. ' tbird.setint(tbird.inta);',
  14659. ' obj.inta:=obj.inta+1;',
  14660. ' if obj.intb=2 then;',
  14661. ' obj.intb:=obj.intb+2;',
  14662. ' obj.setint(obj.inta);',
  14663. ' with Tbird do begin',
  14664. ' FX:=FY+1;',
  14665. ' inta:=inta+2;',
  14666. ' intb:=intb+3;',
  14667. ' end;',
  14668. ' with Obj do begin',
  14669. ' FX:=FY+1;',
  14670. ' inta:=inta+2;',
  14671. ' intb:=intb+3;',
  14672. ' end;',
  14673. '']);
  14674. ConvertProgram;
  14675. CheckSource('TestClass_Property_ClassMethod',
  14676. LinesToStr([ // statements
  14677. 'rtl.createClass(this, "TObject", null, function () {',
  14678. ' this.Fx = 0;',
  14679. ' this.Fy = 0;',
  14680. ' this.$init = function () {',
  14681. ' };',
  14682. ' this.$final = function () {',
  14683. ' };',
  14684. ' this.GetInt = function () {',
  14685. ' var Result = 0;',
  14686. ' Result = this.Fx;',
  14687. ' return Result;',
  14688. ' };',
  14689. ' this.SetInt = function (Value) {',
  14690. ' };',
  14691. '});',
  14692. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14693. ' this.DoIt = function () {',
  14694. ' $mod.TObject.Fx = 3;',
  14695. ' $mod.TObject.Fy = this.Fx + 1;',
  14696. ' $mod.TObject.Fy = this.Fx + 1;',
  14697. ' this.SetInt(this.GetInt() + 1);',
  14698. ' this.SetInt(this.GetInt() + 1);',
  14699. ' $mod.TObject.Fx = 11;',
  14700. ' $mod.TObject.Fy = this.Fx + 12;',
  14701. ' this.SetInt(this.GetInt() + 13);',
  14702. ' };',
  14703. '});',
  14704. 'this.Obj = null;'
  14705. ]),
  14706. LinesToStr([ // $mod.$main
  14707. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14708. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14709. 'if ($mod.TBird.GetInt() === 2);',
  14710. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14711. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14712. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14713. 'if ($mod.Obj.$class.GetInt() === 2);',
  14714. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14715. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14716. 'var $with = $mod.TBird;',
  14717. '$mod.TObject.Fx = $with.Fy + 1;',
  14718. '$mod.TObject.Fy = $with.Fx + 2;',
  14719. '$with.SetInt($with.GetInt() + 3);',
  14720. 'var $with1 = $mod.Obj;',
  14721. '$mod.TObject.Fx = $with1.Fy + 1;',
  14722. '$mod.TObject.Fy = $with1.Fx + 2;',
  14723. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14724. '']));
  14725. end;
  14726. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14727. begin
  14728. StartProgram(false);
  14729. Add([
  14730. 'type',
  14731. ' TObject = class',
  14732. ' class function GetInt: longint; static;',
  14733. ' class procedure SetInt(Value: longint); static;',
  14734. ' class function GetItems(Index: word): longint; static;',
  14735. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14736. ' end;',
  14737. ' TBird = class',
  14738. ' class procedure Fly;',
  14739. ' class property IntA: longint read GetInt write SetInt;',
  14740. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14741. ' end;',
  14742. 'class function tobject.getint: longint;',
  14743. 'begin',
  14744. 'end;',
  14745. 'class procedure tobject.setint(value: longint);',
  14746. 'begin',
  14747. 'end;',
  14748. 'class function tobject.GetItems(Index: word): longint;',
  14749. 'begin',
  14750. 'end;',
  14751. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14752. 'begin',
  14753. 'end;',
  14754. 'class procedure tbird.fly;',
  14755. 'var w: longint;',
  14756. 'begin',
  14757. ' inta:=inta+51;',
  14758. ' w:=items[52];',
  14759. ' items[53]:=54;',
  14760. 'end;',
  14761. 'var Obj: tbird;',
  14762. ' i: longint;',
  14763. 'begin',
  14764. ' tbird.inta:=tbird.inta+1;',
  14765. ' i:=tbird.items[2];',
  14766. ' tbird.items[3]:=4;',
  14767. ' obj.inta:=obj.inta+11;',
  14768. ' i:=obj.items[12];',
  14769. ' obj.items[13]:=14;',
  14770. ' with Tbird do begin',
  14771. ' inta:=inta+21;',
  14772. ' i:=items[22];',
  14773. ' items[23]:=24;',
  14774. ' end;',
  14775. ' with Obj do begin',
  14776. ' inta:=inta+31;',
  14777. ' i:=items[32];',
  14778. ' items[33]:=34;',
  14779. ' end;',
  14780. '']);
  14781. ConvertProgram;
  14782. CheckSource('TestClass_Property_ClassMethod',
  14783. LinesToStr([ // statements
  14784. 'rtl.createClass(this, "TObject", null, function () {',
  14785. ' this.$init = function () {',
  14786. ' };',
  14787. ' this.$final = function () {',
  14788. ' };',
  14789. ' this.GetInt = function () {',
  14790. ' var Result = 0;',
  14791. ' return Result;',
  14792. ' };',
  14793. ' this.SetInt = function (Value) {',
  14794. ' };',
  14795. ' this.GetItems = function (Index) {',
  14796. ' var Result = 0;',
  14797. ' return Result;',
  14798. ' };',
  14799. ' this.SetItems = function (Index, Value) {',
  14800. ' };',
  14801. '});',
  14802. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14803. ' this.Fly = function () {',
  14804. ' var w = 0;',
  14805. ' this.SetInt(this.GetInt() + 51);',
  14806. ' w = this.GetItems(52);',
  14807. ' this.SetItems(53, 54);',
  14808. ' };',
  14809. '});',
  14810. 'this.Obj = null;',
  14811. 'this.i = 0;',
  14812. '']),
  14813. LinesToStr([ // $mod.$main
  14814. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14815. '$mod.i = $mod.TObject.GetItems(2);',
  14816. '$mod.TObject.SetItems(3, 4);',
  14817. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14818. '$mod.i = $mod.TObject.GetItems(12);',
  14819. '$mod.TObject.SetItems(13, 14);',
  14820. 'var $with = $mod.TBird;',
  14821. '$with.SetInt($with.GetInt() + 21);',
  14822. '$mod.i = $with.GetItems(22);',
  14823. '$with.SetItems(23, 24);',
  14824. 'var $with1 = $mod.Obj;',
  14825. '$with1.SetInt($with1.GetInt() + 31);',
  14826. '$mod.i = $with1.GetItems(32);',
  14827. '$with1.SetItems(33, 34);',
  14828. '']));
  14829. end;
  14830. procedure TTestModule.TestClass_Property_Indexed;
  14831. begin
  14832. StartProgram(false);
  14833. Add([
  14834. 'type',
  14835. ' TObject = class',
  14836. ' FItems: array of longint;',
  14837. ' function GetItems(Index: longint): longint;',
  14838. ' procedure SetItems(Index: longint; Value: longint);',
  14839. ' procedure DoIt;',
  14840. ' property Items[Index: longint]: longint read getitems write setitems;',
  14841. ' end;',
  14842. 'function tobject.getitems(index: longint): longint;',
  14843. 'begin',
  14844. ' Result:=fitems[index];',
  14845. 'end;',
  14846. 'procedure tobject.setitems(index: longint; value: longint);',
  14847. 'begin',
  14848. ' fitems[index]:=value;',
  14849. 'end;',
  14850. 'procedure tobject.doit;',
  14851. 'begin',
  14852. ' items[1]:=2;',
  14853. ' items[3]:=items[4];',
  14854. ' self.items[5]:=self.items[6];',
  14855. ' items[items[7]]:=items[items[8]];',
  14856. 'end;',
  14857. 'var Obj: tobject;',
  14858. 'begin',
  14859. ' obj.Items[11]:=obj.Items[12];',
  14860. '']);
  14861. ConvertProgram;
  14862. CheckSource('TestClass_Property_Indexed',
  14863. LinesToStr([ // statements
  14864. 'rtl.createClass(this, "TObject", null, function () {',
  14865. ' this.$init = function () {',
  14866. ' this.FItems = [];',
  14867. ' };',
  14868. ' this.$final = function () {',
  14869. ' this.FItems = undefined;',
  14870. ' };',
  14871. ' this.GetItems = function (Index) {',
  14872. ' var Result = 0;',
  14873. ' Result = this.FItems[Index];',
  14874. ' return Result;',
  14875. ' };',
  14876. ' this.SetItems = function (Index, Value) {',
  14877. ' this.FItems[Index] = Value;',
  14878. ' };',
  14879. ' this.DoIt = function () {',
  14880. ' this.SetItems(1, 2);',
  14881. ' this.SetItems(3,this.GetItems(4));',
  14882. ' this.SetItems(5,this.GetItems(6));',
  14883. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14884. ' };',
  14885. '});',
  14886. 'this.Obj = null;'
  14887. ]),
  14888. LinesToStr([ // $mod.$main
  14889. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  14890. ]));
  14891. end;
  14892. procedure TTestModule.TestClass_Property_IndexSpec;
  14893. begin
  14894. StartProgram(false);
  14895. Add([
  14896. 'type',
  14897. ' TEnum = (red, blue);',
  14898. ' TObject = class',
  14899. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  14900. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  14901. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  14902. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  14903. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  14904. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  14905. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  14906. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  14907. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  14908. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  14909. ' end;',
  14910. 'procedure DoIt(b: boolean); begin end;',
  14911. 'var',
  14912. ' o: TObject;',
  14913. 'begin',
  14914. ' o.B1:=o.B1;',
  14915. ' o.B2:=o.B2;',
  14916. ' o.B3:=o.B3;',
  14917. ' o.I1[''a'']:=o.I1[''b''];',
  14918. ' doit(o.b1);',
  14919. ' doit(o.b2);',
  14920. ' doit(o.i1[''c'']);',
  14921. '']);
  14922. ConvertProgram;
  14923. CheckSource('TestClass_Property_IndexSpec',
  14924. LinesToStr([ // statements
  14925. 'this.TEnum = {',
  14926. ' "0": "red",',
  14927. ' red: 0,',
  14928. ' "1": "blue",',
  14929. ' blue: 1',
  14930. '};',
  14931. 'rtl.createClass(this, "TObject", null, function () {',
  14932. ' this.$init = function () {',
  14933. ' };',
  14934. ' this.$final = function () {',
  14935. ' };',
  14936. '});',
  14937. 'this.DoIt = function (b) {',
  14938. '};',
  14939. 'this.o = null;',
  14940. '']),
  14941. LinesToStr([ // $mod.$main
  14942. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  14943. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  14944. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  14945. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  14946. '$mod.DoIt($mod.o.GetIntBool(1));',
  14947. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  14948. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  14949. '']));
  14950. end;
  14951. procedure TTestModule.TestClass_PropertyOfTypeArray;
  14952. begin
  14953. StartProgram(false);
  14954. Add('type');
  14955. Add(' TArray = array of longint;');
  14956. Add(' TObject = class');
  14957. Add(' FItems: TArray;');
  14958. Add(' function GetItems: tarray;');
  14959. Add(' procedure SetItems(Value: tarray);');
  14960. Add(' property Items: tarray read getitems write setitems;');
  14961. Add(' procedure SetNumbers(const Value: tarray);');
  14962. Add(' property Numbers: tarray write setnumbers;');
  14963. Add(' end;');
  14964. Add('function tobject.getitems: tarray;');
  14965. Add('begin');
  14966. Add(' Result:=fitems;');
  14967. Add('end;');
  14968. Add('procedure tobject.setitems(value: tarray);');
  14969. Add('begin');
  14970. Add(' fitems:=value;');
  14971. Add(' fitems:=nil;');
  14972. Add(' Items:=nil;');
  14973. Add(' Items:=Items;');
  14974. Add(' Items[1]:=2;');
  14975. Add(' fitems[3]:=Items[4];');
  14976. Add(' Items[5]:=Items[6];');
  14977. Add(' Self.Items[7]:=8;');
  14978. Add(' Self.Items[9]:=Self.Items[10];');
  14979. Add(' Items[Items[11]]:=Items[Items[12]];');
  14980. Add('end;');
  14981. Add('procedure tobject.SetNumbers(const Value: tarray);');
  14982. Add('begin;');
  14983. Add(' Numbers:=nil;');
  14984. Add(' Numbers:=Value;');
  14985. Add(' Self.Numbers:=Value;');
  14986. Add('end;');
  14987. Add('var Obj: tobject;');
  14988. Add('begin');
  14989. Add(' obj.items:=nil;');
  14990. Add(' obj.items:=obj.items;');
  14991. Add(' obj.items[11]:=obj.items[12];');
  14992. ConvertProgram;
  14993. CheckSource('TestClass_PropertyOfTypeArray',
  14994. LinesToStr([ // statements
  14995. 'rtl.createClass(this, "TObject", null, function () {',
  14996. ' this.$init = function () {',
  14997. ' this.FItems = [];',
  14998. ' };',
  14999. ' this.$final = function () {',
  15000. ' this.FItems = undefined;',
  15001. ' };',
  15002. ' this.GetItems = function () {',
  15003. ' var Result = [];',
  15004. ' Result = rtl.arrayRef(this.FItems);',
  15005. ' return Result;',
  15006. ' };',
  15007. ' this.SetItems = function (Value) {',
  15008. ' this.FItems = rtl.arrayRef(Value);',
  15009. ' this.FItems = [];',
  15010. ' this.SetItems([]);',
  15011. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15012. ' this.GetItems()[1] = 2;',
  15013. ' this.FItems[3] = this.GetItems()[4];',
  15014. ' this.GetItems()[5] = this.GetItems()[6];',
  15015. ' this.GetItems()[7] = 8;',
  15016. ' this.GetItems()[9] = this.GetItems()[10];',
  15017. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15018. ' };',
  15019. ' this.SetNumbers = function (Value) {',
  15020. ' this.SetNumbers([]);',
  15021. ' this.SetNumbers(Value);',
  15022. ' this.SetNumbers(Value);',
  15023. ' };',
  15024. '});',
  15025. 'this.Obj = null;'
  15026. ]),
  15027. LinesToStr([ // $mod.$main
  15028. '$mod.Obj.SetItems([]);',
  15029. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15030. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15031. ]));
  15032. end;
  15033. procedure TTestModule.TestClass_PropertyDefault;
  15034. begin
  15035. StartProgram(false);
  15036. Add([
  15037. 'type',
  15038. ' TArray = array of longint;',
  15039. ' TObject = class',
  15040. ' end;',
  15041. ' TBird = class',
  15042. ' FItems: TArray;',
  15043. ' function GetItems(Index: longint): longint;',
  15044. ' procedure SetItems(Index, Value: longint);',
  15045. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15046. ' end;',
  15047. 'function TBird.getitems(index: longint): longint;',
  15048. 'begin',
  15049. 'end;',
  15050. 'procedure TBird.setitems(index, value: longint);',
  15051. 'begin',
  15052. ' Self[1]:=2;',
  15053. ' Self[3]:=Self[index];',
  15054. ' Self[index]:=Self[Self[value]];',
  15055. ' Self[Self[4]]:=value;',
  15056. 'end;',
  15057. 'var',
  15058. ' Bird: TBird;',
  15059. ' Obj: TObject;',
  15060. 'begin',
  15061. ' bird[11]:=12;',
  15062. ' bird[13]:=bird[14];',
  15063. ' bird[Bird[15]]:=bird[Bird[15]];',
  15064. ' TBird(obj)[16]:=TBird(obj)[17];',
  15065. ' (obj as tbird)[18]:=19;',
  15066. '']);
  15067. ConvertProgram;
  15068. CheckSource('TestClass_PropertyDefault',
  15069. LinesToStr([ // statements
  15070. 'rtl.createClass(this, "TObject", null, function () {',
  15071. ' this.$init = function () {',
  15072. ' };',
  15073. ' this.$final = function () {',
  15074. ' };',
  15075. '});',
  15076. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15077. ' this.$init = function () {',
  15078. ' $mod.TObject.$init.call(this);',
  15079. ' this.FItems = [];',
  15080. ' };',
  15081. ' this.$final = function () {',
  15082. ' this.FItems = undefined;',
  15083. ' $mod.TObject.$final.call(this);',
  15084. ' };',
  15085. ' this.GetItems = function (Index) {',
  15086. ' var Result = 0;',
  15087. ' return Result;',
  15088. ' };',
  15089. ' this.SetItems = function (Index, Value) {',
  15090. ' this.SetItems(1, 2);',
  15091. ' this.SetItems(3, this.GetItems(Index));',
  15092. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15093. ' this.SetItems(this.GetItems(4), Value);',
  15094. ' };',
  15095. '});',
  15096. 'this.Bird = null;',
  15097. 'this.Obj = null;',
  15098. '']),
  15099. LinesToStr([ // $mod.$main
  15100. '$mod.Bird.SetItems(11, 12);',
  15101. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15102. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15103. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15104. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15105. '']));
  15106. end;
  15107. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15108. begin
  15109. StartProgram(false);
  15110. Add([
  15111. 'type',
  15112. ' TObject = class end;',
  15113. ' TAlphaList = class',
  15114. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15115. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15116. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15117. ' end;',
  15118. ' TBetaList = class',
  15119. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15120. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15121. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15122. ' end;',
  15123. ' TBird = class',
  15124. ' procedure DoIt;',
  15125. ' end;',
  15126. 'procedure TBird.DoIt;',
  15127. 'var',
  15128. ' List: TAlphaList;',
  15129. 'begin',
  15130. ' if TBetaList(List[true])[3]=nil then ;',
  15131. ' TBetaList(List[false])[5]:=nil;',
  15132. 'end;',
  15133. 'var',
  15134. ' List: TAlphaList;',
  15135. 'begin',
  15136. ' if TBetaList(List[true])[3]=nil then ;',
  15137. ' TBetaList(List[false])[5]:=nil;',
  15138. '']);
  15139. ConvertProgram;
  15140. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15141. LinesToStr([ // statements
  15142. 'rtl.createClass(this, "TObject", null, function () {',
  15143. ' this.$init = function () {',
  15144. ' };',
  15145. ' this.$final = function () {',
  15146. ' };',
  15147. '});',
  15148. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15149. '});',
  15150. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15151. '});',
  15152. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15153. ' this.DoIt = function () {',
  15154. ' var List = null;',
  15155. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15156. ' List.GetAlphas(false).SetBetas(5, null);',
  15157. ' };',
  15158. '});',
  15159. 'this.List = null;',
  15160. '']),
  15161. LinesToStr([ // $mod.$main
  15162. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15163. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15164. '']));
  15165. end;
  15166. procedure TTestModule.TestClass_PropertyOverride;
  15167. begin
  15168. StartProgram(false);
  15169. Add('type');
  15170. Add(' integer = longint;');
  15171. Add(' TObject = class');
  15172. Add(' FItem: integer;');
  15173. Add(' function GetItem: integer; external name ''GetItem'';');
  15174. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15175. Add(' property Item: integer read getitem write setitem;');
  15176. Add(' end;');
  15177. Add(' TCar = class');
  15178. Add(' FBag: integer;');
  15179. Add(' function GetBag: integer; external name ''GetBag'';');
  15180. Add(' property Item read getbag;');
  15181. Add(' end;');
  15182. Add('var');
  15183. Add(' Obj: tobject;');
  15184. Add(' Car: tcar;');
  15185. Add('begin');
  15186. Add(' Obj.Item:=Obj.Item;');
  15187. Add(' Car.Item:=Car.Item;');
  15188. ConvertProgram;
  15189. CheckSource('TestClass_PropertyOverride',
  15190. LinesToStr([ // statements
  15191. 'rtl.createClass(this, "TObject", null, function () {',
  15192. ' this.$init = function () {',
  15193. ' this.FItem = 0;',
  15194. ' };',
  15195. ' this.$final = function () {',
  15196. ' };',
  15197. '});',
  15198. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15199. ' this.$init = function () {',
  15200. ' $mod.TObject.$init.call(this);',
  15201. ' this.FBag = 0;',
  15202. ' };',
  15203. '});',
  15204. 'this.Obj = null;',
  15205. 'this.Car = null;',
  15206. '']),
  15207. LinesToStr([ // $mod.$main
  15208. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15209. '$mod.Car.SetItem($mod.Car.GetBag());',
  15210. '']));
  15211. end;
  15212. procedure TTestModule.TestClass_PropertyIncVisibility;
  15213. begin
  15214. AddModuleWithIntfImplSrc('unit1.pp',
  15215. LinesToStr([
  15216. 'type',
  15217. ' TNumber = longint;',
  15218. ' TInteger = longint;',
  15219. ' TObject = class',
  15220. ' private',
  15221. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15222. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15223. ' protected',
  15224. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15225. ' end;']),
  15226. LinesToStr([
  15227. '']));
  15228. StartProgram(true);
  15229. Add([
  15230. 'uses unit1;',
  15231. 'type',
  15232. ' TBird = class',
  15233. ' public',
  15234. ' property Items;',
  15235. ' end;',
  15236. 'procedure DoIt(i: TInteger);',
  15237. 'begin',
  15238. 'end;',
  15239. 'var b: TBird;',
  15240. 'begin',
  15241. ' b.Items[1]:=2;',
  15242. ' b.Items[3]:=b.Items[4];',
  15243. ' DoIt(b.Items[5]);',
  15244. '']);
  15245. ConvertProgram;
  15246. CheckSource('TestClass_PropertyIncVisibility',
  15247. LinesToStr([ // statements
  15248. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15249. '});',
  15250. 'this.DoIt = function (i) {',
  15251. '};',
  15252. 'this.b = null;'
  15253. ]),
  15254. LinesToStr([ // $mod.$main
  15255. '$mod.b.SetItems(1, 2);',
  15256. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15257. '$mod.DoIt($mod.b.GetItems(5));'
  15258. ]));
  15259. end;
  15260. procedure TTestModule.TestClass_Assigned;
  15261. begin
  15262. StartProgram(false);
  15263. Add('type');
  15264. Add(' TObject = class');
  15265. Add(' end;');
  15266. Add('var');
  15267. Add(' Obj: tobject;');
  15268. Add(' b: boolean;');
  15269. Add('begin');
  15270. Add(' if Assigned(obj) then ;');
  15271. Add(' b:=Assigned(obj) or false;');
  15272. ConvertProgram;
  15273. CheckSource('TestClass_Assigned',
  15274. LinesToStr([ // statements
  15275. 'rtl.createClass(this, "TObject", null, function () {',
  15276. ' this.$init = function () {',
  15277. ' };',
  15278. ' this.$final = function () {',
  15279. ' };',
  15280. '});',
  15281. 'this.Obj = null;',
  15282. 'this.b = false;'
  15283. ]),
  15284. LinesToStr([ // $mod.$main
  15285. 'if ($mod.Obj != null);',
  15286. '$mod.b = ($mod.Obj != null) || false;'
  15287. ]));
  15288. end;
  15289. procedure TTestModule.TestClass_WithClassDoCreate;
  15290. begin
  15291. StartProgram(false);
  15292. Add('type');
  15293. Add(' TObject = class');
  15294. Add(' aBool: boolean;');
  15295. Add(' Arr: array of boolean;');
  15296. Add(' constructor Create;');
  15297. Add(' end;');
  15298. Add('constructor TObject.Create; begin end;');
  15299. Add('var');
  15300. Add(' Obj: tobject;');
  15301. Add(' b: boolean;');
  15302. Add('begin');
  15303. Add(' with tobject.create do begin');
  15304. Add(' b:=abool;');
  15305. Add(' abool:=b;');
  15306. Add(' b:=arr[1];');
  15307. Add(' arr[2]:=b;');
  15308. Add(' end;');
  15309. Add(' with tobject do');
  15310. Add(' obj:=create;');
  15311. Add(' with obj do begin');
  15312. Add(' create;');
  15313. Add(' b:=abool;');
  15314. Add(' abool:=b;');
  15315. Add(' b:=arr[3];');
  15316. Add(' arr[4]:=b;');
  15317. Add(' end;');
  15318. ConvertProgram;
  15319. CheckSource('TestClass_WithClassDoCreate',
  15320. LinesToStr([ // statements
  15321. 'rtl.createClass(this, "TObject", null, function () {',
  15322. ' this.$init = function () {',
  15323. ' this.aBool = false;',
  15324. ' this.Arr = [];',
  15325. ' };',
  15326. ' this.$final = function () {',
  15327. ' this.Arr = undefined;',
  15328. ' };',
  15329. ' this.Create = function () {',
  15330. ' return this;',
  15331. ' };',
  15332. '});',
  15333. 'this.Obj = null;',
  15334. 'this.b = false;'
  15335. ]),
  15336. LinesToStr([ // $mod.$main
  15337. 'var $with = $mod.TObject.$create("Create");',
  15338. '$mod.b = $with.aBool;',
  15339. '$with.aBool = $mod.b;',
  15340. '$mod.b = $with.Arr[1];',
  15341. '$with.Arr[2] = $mod.b;',
  15342. 'var $with1 = $mod.TObject;',
  15343. '$mod.Obj = $with1.$create("Create");',
  15344. 'var $with2 = $mod.Obj;',
  15345. '$with2.Create();',
  15346. '$mod.b = $with2.aBool;',
  15347. '$with2.aBool = $mod.b;',
  15348. '$mod.b = $with2.Arr[3];',
  15349. '$with2.Arr[4] = $mod.b;',
  15350. '']));
  15351. end;
  15352. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15353. begin
  15354. StartProgram(false);
  15355. Add('type');
  15356. Add(' TObject = class');
  15357. Add(' FInt: longint;');
  15358. Add(' constructor Create;');
  15359. Add(' function GetSize: longint;');
  15360. Add(' procedure SetSize(Value: longint);');
  15361. Add(' property Int: longint read FInt write FInt;');
  15362. Add(' property Size: longint read GetSize write SetSize;');
  15363. Add(' end;');
  15364. Add('constructor TObject.Create; begin end;');
  15365. Add('function TObject.GetSize: longint; begin; end;');
  15366. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15367. Add('var');
  15368. Add(' Obj: tobject;');
  15369. Add(' i: longint;');
  15370. Add('begin');
  15371. Add(' with TObject.Create do begin');
  15372. Add(' i:=int;');
  15373. Add(' int:=i;');
  15374. Add(' i:=size;');
  15375. Add(' size:=i;');
  15376. Add(' end;');
  15377. Add(' with obj do begin');
  15378. Add(' i:=int;');
  15379. Add(' int:=i;');
  15380. Add(' i:=size;');
  15381. Add(' size:=i;');
  15382. Add(' end;');
  15383. ConvertProgram;
  15384. CheckSource('TestClass_WithClassInstDoProperty',
  15385. LinesToStr([ // statements
  15386. 'rtl.createClass(this, "TObject", null, function () {',
  15387. ' this.$init = function () {',
  15388. ' this.FInt = 0;',
  15389. ' };',
  15390. ' this.$final = function () {',
  15391. ' };',
  15392. ' this.Create = function () {',
  15393. ' return this;',
  15394. ' };',
  15395. ' this.GetSize = function () {',
  15396. ' var Result = 0;',
  15397. ' return Result;',
  15398. ' };',
  15399. ' this.SetSize = function (Value) {',
  15400. ' };',
  15401. '});',
  15402. 'this.Obj = null;',
  15403. 'this.i = 0;'
  15404. ]),
  15405. LinesToStr([ // $mod.$main
  15406. 'var $with = $mod.TObject.$create("Create");',
  15407. '$mod.i = $with.FInt;',
  15408. '$with.FInt = $mod.i;',
  15409. '$mod.i = $with.GetSize();',
  15410. '$with.SetSize($mod.i);',
  15411. 'var $with1 = $mod.Obj;',
  15412. '$mod.i = $with1.FInt;',
  15413. '$with1.FInt = $mod.i;',
  15414. '$mod.i = $with1.GetSize();',
  15415. '$with1.SetSize($mod.i);',
  15416. '']));
  15417. end;
  15418. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15419. begin
  15420. StartProgram(false);
  15421. Add('type');
  15422. Add(' TObject = class');
  15423. Add(' constructor Create;');
  15424. Add(' function GetItems(Index: longint): longint;');
  15425. Add(' procedure SetItems(Index, Value: longint);');
  15426. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15427. Add(' end;');
  15428. Add('constructor TObject.Create; begin end;');
  15429. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15430. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15431. Add('var');
  15432. Add(' Obj: tobject;');
  15433. Add(' i: longint;');
  15434. Add('begin');
  15435. Add(' with TObject.Create do begin');
  15436. Add(' i:=Items[1];');
  15437. Add(' Items[2]:=i;');
  15438. Add(' end;');
  15439. Add(' with obj do begin');
  15440. Add(' i:=Items[3];');
  15441. Add(' Items[4]:=i;');
  15442. Add(' end;');
  15443. ConvertProgram;
  15444. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15445. LinesToStr([ // statements
  15446. 'rtl.createClass(this, "TObject", null, function () {',
  15447. ' this.$init = function () {',
  15448. ' };',
  15449. ' this.$final = function () {',
  15450. ' };',
  15451. ' this.Create = function () {',
  15452. ' return this;',
  15453. ' };',
  15454. ' this.GetItems = function (Index) {',
  15455. ' var Result = 0;',
  15456. ' return Result;',
  15457. ' };',
  15458. ' this.SetItems = function (Index, Value) {',
  15459. ' };',
  15460. '});',
  15461. 'this.Obj = null;',
  15462. 'this.i = 0;'
  15463. ]),
  15464. LinesToStr([ // $mod.$main
  15465. 'var $with = $mod.TObject.$create("Create");',
  15466. '$mod.i = $with.GetItems(1);',
  15467. '$with.SetItems(2, $mod.i);',
  15468. 'var $with1 = $mod.Obj;',
  15469. '$mod.i = $with1.GetItems(3);',
  15470. '$with1.SetItems(4, $mod.i);',
  15471. '']));
  15472. end;
  15473. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15474. begin
  15475. StartProgram(false);
  15476. Add('type');
  15477. Add(' TObject = class');
  15478. Add(' constructor Create;');
  15479. Add(' function GetSize: longint;');
  15480. Add(' procedure SetSize(Value: longint);');
  15481. Add(' end;');
  15482. Add('constructor TObject.Create; begin end;');
  15483. Add('function TObject.GetSize: longint; begin; end;');
  15484. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15485. Add('var');
  15486. Add(' Obj: tobject;');
  15487. Add(' i: longint;');
  15488. Add('begin');
  15489. Add(' with TObject.Create do begin');
  15490. Add(' i:=GetSize;');
  15491. Add(' i:=GetSize();');
  15492. Add(' SetSize(i);');
  15493. Add(' end;');
  15494. Add(' with obj do begin');
  15495. Add(' i:=GetSize;');
  15496. Add(' i:=GetSize();');
  15497. Add(' SetSize(i);');
  15498. Add(' end;');
  15499. ConvertProgram;
  15500. CheckSource('TestClass_WithClassInstDoFunc',
  15501. LinesToStr([ // statements
  15502. 'rtl.createClass(this, "TObject", null, function () {',
  15503. ' this.$init = function () {',
  15504. ' };',
  15505. ' this.$final = function () {',
  15506. ' };',
  15507. ' this.Create = function () {',
  15508. ' return this;',
  15509. ' };',
  15510. ' this.GetSize = function () {',
  15511. ' var Result = 0;',
  15512. ' return Result;',
  15513. ' };',
  15514. ' this.SetSize = function (Value) {',
  15515. ' };',
  15516. '});',
  15517. 'this.Obj = null;',
  15518. 'this.i = 0;'
  15519. ]),
  15520. LinesToStr([ // $mod.$main
  15521. 'var $with = $mod.TObject.$create("Create");',
  15522. '$mod.i = $with.GetSize();',
  15523. '$mod.i = $with.GetSize();',
  15524. '$with.SetSize($mod.i);',
  15525. 'var $with1 = $mod.Obj;',
  15526. '$mod.i = $with1.GetSize();',
  15527. '$mod.i = $with1.GetSize();',
  15528. '$with1.SetSize($mod.i);',
  15529. '']));
  15530. end;
  15531. procedure TTestModule.TestClass_TypeCast;
  15532. begin
  15533. StartProgram(false);
  15534. Add('type');
  15535. Add(' TObject = class');
  15536. Add(' Next: TObject;');
  15537. Add(' constructor Create;');
  15538. Add(' end;');
  15539. Add(' TControl = class(TObject)');
  15540. Add(' Arr: array of TObject;');
  15541. Add(' function GetIt(vI: longint = 0): TObject;');
  15542. Add(' end;');
  15543. Add('constructor tobject.create; begin end;');
  15544. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15545. Add('var');
  15546. Add(' Obj: tobject;');
  15547. Add('begin');
  15548. Add(' obj:=tcontrol(obj).next;');
  15549. Add(' tcontrol(obj):=nil;');
  15550. Add(' obj:=tcontrol(obj);');
  15551. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15552. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15553. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15554. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15555. Add(' obj:=tcontrol(nil);');
  15556. ConvertProgram;
  15557. CheckSource('TestClass_TypeCast',
  15558. LinesToStr([ // statements
  15559. 'rtl.createClass(this, "TObject", null, function () {',
  15560. ' this.$init = function () {',
  15561. ' this.Next = null;',
  15562. ' };',
  15563. ' this.$final = function () {',
  15564. ' this.Next = undefined;',
  15565. ' };',
  15566. ' this.Create = function () {',
  15567. ' return this;',
  15568. ' };',
  15569. '});',
  15570. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15571. ' this.$init = function () {',
  15572. ' $mod.TObject.$init.call(this);',
  15573. ' this.Arr = [];',
  15574. ' };',
  15575. ' this.$final = function () {',
  15576. ' this.Arr = undefined;',
  15577. ' $mod.TObject.$final.call(this);',
  15578. ' };',
  15579. ' this.GetIt = function (vI) {',
  15580. ' var Result = null;',
  15581. ' return Result;',
  15582. ' };',
  15583. '});',
  15584. 'this.Obj = null;'
  15585. ]),
  15586. LinesToStr([ // $mod.$main
  15587. '$mod.Obj = $mod.Obj.Next;',
  15588. '$mod.Obj = null;',
  15589. '$mod.Obj = $mod.Obj;',
  15590. '$mod.Obj = $mod.Obj.GetIt(0);',
  15591. '$mod.Obj = $mod.Obj.GetIt(0);',
  15592. '$mod.Obj = $mod.Obj.GetIt(1);',
  15593. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15594. '$mod.Obj = null;',
  15595. '']));
  15596. end;
  15597. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15598. begin
  15599. StartProgram(false);
  15600. Add('type');
  15601. Add(' TObject = class end;');
  15602. Add('procedure ProcA(var A);');
  15603. Add('begin');
  15604. Add(' TObject(A):=nil;');
  15605. Add(' TObject(A):=TObject(A);');
  15606. Add(' if TObject(A)=nil then ;');
  15607. Add(' if nil=TObject(A) then ;');
  15608. Add('end;');
  15609. Add('procedure ProcB(out A);');
  15610. Add('begin');
  15611. Add(' TObject(A):=nil;');
  15612. Add(' TObject(A):=TObject(A);');
  15613. Add(' if TObject(A)=nil then ;');
  15614. Add(' if nil=TObject(A) then ;');
  15615. Add('end;');
  15616. Add('procedure ProcC(const A);');
  15617. Add('begin');
  15618. Add(' if TObject(A)=nil then ;');
  15619. Add(' if nil=TObject(A) then ;');
  15620. Add('end;');
  15621. Add('var o: TObject;');
  15622. Add('begin');
  15623. Add(' ProcA(o);');
  15624. Add(' ProcB(o);');
  15625. Add(' ProcC(o);');
  15626. ConvertProgram;
  15627. CheckSource('TestClass_TypeCastUntypedParam',
  15628. LinesToStr([ // statements
  15629. 'rtl.createClass(this, "TObject", null, function () {',
  15630. ' this.$init = function () {',
  15631. ' };',
  15632. ' this.$final = function () {',
  15633. ' };',
  15634. '});',
  15635. 'this.ProcA = function (A) {',
  15636. ' A.set(null);',
  15637. ' A.set(A.get());',
  15638. ' if (A.get() === null);',
  15639. ' if (null === A.get());',
  15640. '};',
  15641. 'this.ProcB = function (A) {',
  15642. ' A.set(null);',
  15643. ' A.set(A.get());',
  15644. ' if (A.get() === null);',
  15645. ' if (null === A.get());',
  15646. '};',
  15647. 'this.ProcC = function (A) {',
  15648. ' if (A === null);',
  15649. ' if (null === A);',
  15650. '};',
  15651. 'this.o = null;',
  15652. '']),
  15653. LinesToStr([ // $mod.$main
  15654. '$mod.ProcA({',
  15655. ' p: $mod,',
  15656. ' get: function () {',
  15657. ' return this.p.o;',
  15658. ' },',
  15659. ' set: function (v) {',
  15660. ' this.p.o = v;',
  15661. ' }',
  15662. '});',
  15663. '$mod.ProcB({',
  15664. ' p: $mod,',
  15665. ' get: function () {',
  15666. ' return this.p.o;',
  15667. ' },',
  15668. ' set: function (v) {',
  15669. ' this.p.o = v;',
  15670. ' }',
  15671. '});',
  15672. '$mod.ProcC($mod.o);',
  15673. '']));
  15674. end;
  15675. procedure TTestModule.TestClass_Overloads;
  15676. begin
  15677. StartProgram(false);
  15678. Add('type');
  15679. Add(' TObject = class');
  15680. Add(' procedure DoIt;');
  15681. Add(' procedure DoIt(vI: longint);');
  15682. Add(' end;');
  15683. Add('procedure TObject.DoIt;');
  15684. Add('begin');
  15685. Add(' DoIt;');
  15686. Add(' DoIt(1);');
  15687. Add('end;');
  15688. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15689. Add('begin');
  15690. ConvertProgram;
  15691. CheckSource('TestClass_Overloads',
  15692. LinesToStr([ // statements
  15693. 'rtl.createClass(this, "TObject", null, function () {',
  15694. ' this.$init = function () {',
  15695. ' };',
  15696. ' this.$final = function () {',
  15697. ' };',
  15698. ' this.DoIt = function () {',
  15699. ' this.DoIt();',
  15700. ' this.DoIt$1(1);',
  15701. ' };',
  15702. ' this.DoIt$1 = function (vI) {',
  15703. ' };',
  15704. '});',
  15705. '']),
  15706. LinesToStr([ // $mod.$main
  15707. '']));
  15708. end;
  15709. procedure TTestModule.TestClass_OverloadsAncestor;
  15710. begin
  15711. StartProgram(false);
  15712. Add('type');
  15713. Add(' TObject = class;');
  15714. Add(' TObject = class');
  15715. Add(' procedure DoIt(vA: longint);');
  15716. Add(' procedure DoIt(vA, vB: longint);');
  15717. Add(' end;');
  15718. Add(' TCar = class;');
  15719. Add(' TCar = class');
  15720. Add(' procedure DoIt(vA: longint);');
  15721. Add(' procedure DoIt(vA, vB: longint);');
  15722. Add(' end;');
  15723. Add('procedure tobject.doit(va: longint);');
  15724. Add('begin');
  15725. Add(' doit(1);');
  15726. Add(' doit(1,2);');
  15727. Add('end;');
  15728. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15729. Add('procedure tcar.doit(va: longint);');
  15730. Add('begin');
  15731. Add(' doit(1);');
  15732. Add(' doit(1,2);');
  15733. Add(' inherited doit(1);');
  15734. Add(' inherited doit(1,2);');
  15735. Add('end;');
  15736. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15737. Add('begin');
  15738. ConvertProgram;
  15739. CheckSource('TestClass_OverloadsAncestor',
  15740. LinesToStr([ // statements
  15741. 'rtl.createClass(this, "TObject", null, function () {',
  15742. ' this.$init = function () {',
  15743. ' };',
  15744. ' this.$final = function () {',
  15745. ' };',
  15746. ' this.DoIt = function (vA) {',
  15747. ' this.DoIt(1);',
  15748. ' this.DoIt$1(1,2);',
  15749. ' };',
  15750. ' this.DoIt$1 = function (vA, vB) {',
  15751. ' };',
  15752. '});',
  15753. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15754. ' this.DoIt$2 = function (vA) {',
  15755. ' this.DoIt$2(1);',
  15756. ' this.DoIt$3(1, 2);',
  15757. ' $mod.TObject.DoIt.call(this, 1);',
  15758. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15759. ' };',
  15760. ' this.DoIt$3 = function (vA, vB) {',
  15761. ' };',
  15762. '});',
  15763. '']),
  15764. LinesToStr([ // $mod.$main
  15765. '']));
  15766. end;
  15767. procedure TTestModule.TestClass_OverloadConstructor;
  15768. begin
  15769. StartProgram(false);
  15770. Add('type');
  15771. Add(' TObject = class');
  15772. Add(' constructor Create(vA: longint);');
  15773. Add(' constructor Create(vA, vB: longint);');
  15774. Add(' end;');
  15775. Add(' TCar = class');
  15776. Add(' constructor Create(vA: longint);');
  15777. Add(' constructor Create(vA, vB: longint);');
  15778. Add(' end;');
  15779. Add('constructor tobject.create(va: longint);');
  15780. Add('begin');
  15781. Add(' create(1);');
  15782. Add(' create(1,2);');
  15783. Add('end;');
  15784. Add('constructor tobject.create(va, vb: longint); begin end;');
  15785. Add('constructor tcar.create(va: longint);');
  15786. Add('begin');
  15787. Add(' create(1);');
  15788. Add(' create(1,2);');
  15789. Add(' inherited create(1);');
  15790. Add(' inherited create(1,2);');
  15791. Add('end;');
  15792. Add('constructor tcar.create(va, vb: longint); begin end;');
  15793. Add('begin');
  15794. Add(' tobject.create(1);');
  15795. Add(' tobject.create(1,2);');
  15796. Add(' tcar.create(1);');
  15797. Add(' tcar.create(1,2);');
  15798. ConvertProgram;
  15799. CheckSource('TestClass_OverloadConstructor',
  15800. LinesToStr([ // statements
  15801. 'rtl.createClass(this, "TObject", null, function () {',
  15802. ' this.$init = function () {',
  15803. ' };',
  15804. ' this.$final = function () {',
  15805. ' };',
  15806. ' this.Create = function (vA) {',
  15807. ' this.Create(1);',
  15808. ' this.Create$1(1,2);',
  15809. ' return this;',
  15810. ' };',
  15811. ' this.Create$1 = function (vA, vB) {',
  15812. ' return this;',
  15813. ' };',
  15814. '});',
  15815. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15816. ' this.Create$2 = function (vA) {',
  15817. ' this.Create$2(1);',
  15818. ' this.Create$3(1, 2);',
  15819. ' $mod.TObject.Create.call(this, 1);',
  15820. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15821. ' return this;',
  15822. ' };',
  15823. ' this.Create$3 = function (vA, vB) {',
  15824. ' return this;',
  15825. ' };',
  15826. '});',
  15827. '']),
  15828. LinesToStr([ // $mod.$main
  15829. '$mod.TObject.$create("Create", [1]);',
  15830. '$mod.TObject.$create("Create$1", [1, 2]);',
  15831. '$mod.TCar.$create("Create$2", [1]);',
  15832. '$mod.TCar.$create("Create$3", [1, 2]);',
  15833. '']));
  15834. end;
  15835. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15836. begin
  15837. StartProgram(false);
  15838. Add([
  15839. '{$mode delphi}',
  15840. 'type',
  15841. ' TObject = class end;',
  15842. ' TBird = class',
  15843. ' function {#a}GetValue: longint; overload; virtual;',
  15844. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15845. ' end;',
  15846. ' TEagle = class(TBird)',
  15847. ' function {#c}GetValue: longint; overload; override;',
  15848. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15849. ' end;',
  15850. 'function TBird.GetValue: longint;',
  15851. 'begin',
  15852. ' if 3={@a}GetValue then ;',
  15853. ' if 4={@b}GetValue(5) then ;',
  15854. 'end;',
  15855. 'function TBird.GetValue(AValue: longint): longint;',
  15856. 'begin',
  15857. 'end;',
  15858. 'function TEagle.GetValue: longint;',
  15859. 'begin',
  15860. ' if 13={@c}GetValue then ;',
  15861. ' if 14={@d}GetValue(15) then ;',
  15862. ' if 15=inherited {@a}GetValue then ;',
  15863. ' if 16=inherited {@b}GetValue(17) then ;',
  15864. 'end;',
  15865. 'function TEagle.GetValue(AValue: longint): longint;',
  15866. 'begin',
  15867. 'end;',
  15868. 'var',
  15869. ' e: TEagle;',
  15870. 'begin',
  15871. ' if 23=e.{@c}GetValue then ;',
  15872. ' if 24=e.{@d}GetValue(25) then ;']);
  15873. ConvertProgram;
  15874. CheckSource('TestClass_OverloadDelphiOverride',
  15875. LinesToStr([ // statements
  15876. 'rtl.createClass(this, "TObject", null, function () {',
  15877. ' this.$init = function () {',
  15878. ' };',
  15879. ' this.$final = function () {',
  15880. ' };',
  15881. '});',
  15882. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15883. ' this.GetValue = function () {',
  15884. ' var Result = 0;',
  15885. ' if (3 === this.GetValue()) ;',
  15886. ' if (4 === this.GetValue$1(5)) ;',
  15887. ' return Result;',
  15888. ' };',
  15889. ' this.GetValue$1 = function (AValue) {',
  15890. ' var Result = 0;',
  15891. ' return Result;',
  15892. ' };',
  15893. '});',
  15894. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15895. ' this.GetValue = function () {',
  15896. ' var Result = 0;',
  15897. ' if (13 === this.GetValue()) ;',
  15898. ' if (14 === this.GetValue$1(15)) ;',
  15899. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  15900. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  15901. ' return Result;',
  15902. ' };',
  15903. ' this.GetValue$1 = function (AValue) {',
  15904. ' var Result = 0;',
  15905. ' return Result;',
  15906. ' };',
  15907. '});',
  15908. 'this.e = null;',
  15909. '']),
  15910. LinesToStr([ // $mod.$main
  15911. 'if (23 === $mod.e.GetValue()) ;',
  15912. 'if (24 === $mod.e.GetValue$1(25)) ;',
  15913. '']));
  15914. end;
  15915. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  15916. begin
  15917. StartProgram(false);
  15918. Add([
  15919. '{$mode delphi}',
  15920. 'type',
  15921. ' TObject = class end;',
  15922. ' TAnimal = class',
  15923. ' public',
  15924. ' {#animal_a}A: longint;',
  15925. ' function {#animal_b}B: longint;',
  15926. ' end;',
  15927. ' TBird = class(TAnimal)',
  15928. ' public',
  15929. ' {#bird_a}A: double;',
  15930. ' {#bird_b}B: boolean;',
  15931. ' end;',
  15932. ' TEagle = class(TBird)',
  15933. ' public',
  15934. ' function {#eagle_a}A: boolean;',
  15935. ' {#eagle_b}B: double;',
  15936. ' end;',
  15937. 'function TAnimal.B: longint;',
  15938. 'begin',
  15939. 'end;',
  15940. 'function TEagle.A: boolean;',
  15941. 'begin',
  15942. ' {@eagle_b}B:=3.3;',
  15943. ' {@eagle_a}A();',
  15944. ' TBird(Self).{@bird_b}B:=true;',
  15945. ' TAnimal(Self).{@animal_a}A:=17;',
  15946. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  15947. 'end;',
  15948. 'var',
  15949. ' e: TEagle;',
  15950. 'begin',
  15951. ' e.{@eagle_b}B:=5.3;',
  15952. ' if e.{@eagle_a}A then ;',
  15953. '']);
  15954. ConvertProgram;
  15955. CheckSource('TestClass_ReintroduceVarDelphi',
  15956. LinesToStr([ // statements
  15957. 'rtl.createClass(this, "TObject", null, function () {',
  15958. ' this.$init = function () {',
  15959. ' };',
  15960. ' this.$final = function () {',
  15961. ' };',
  15962. '});',
  15963. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  15964. ' this.$init = function () {',
  15965. ' $mod.TObject.$init.call(this);',
  15966. ' this.A = 0;',
  15967. ' };',
  15968. ' this.B = function () {',
  15969. ' var Result = 0;',
  15970. ' return Result;',
  15971. ' };',
  15972. '});',
  15973. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  15974. ' this.$init = function () {',
  15975. ' $mod.TAnimal.$init.call(this);',
  15976. ' this.A$1 = 0.0;',
  15977. ' this.B$1 = false;',
  15978. ' };',
  15979. '});',
  15980. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15981. ' this.$init = function () {',
  15982. ' $mod.TBird.$init.call(this);',
  15983. ' this.B$2 = 0.0;',
  15984. ' };',
  15985. ' this.A$2 = function () {',
  15986. ' var Result = false;',
  15987. ' this.B$2 = 3.3;',
  15988. ' this.A$2();',
  15989. ' this.B$1 = true;',
  15990. ' this.A = 17;',
  15991. ' this.B$1 = this.A$1 > 1;',
  15992. ' return Result;',
  15993. ' };',
  15994. '});',
  15995. 'this.e = null;',
  15996. '']),
  15997. LinesToStr([ // $mod.$main
  15998. '$mod.e.B$2 = 5.3;',
  15999. 'if ($mod.e.A$2()) ;',
  16000. '']));
  16001. end;
  16002. procedure TTestModule.TestClass_ReintroducedVar;
  16003. begin
  16004. StartProgram(false);
  16005. Add('type');
  16006. Add(' TObject = class');
  16007. Add(' strict private');
  16008. Add(' Some: longint;');
  16009. Add(' end;');
  16010. Add(' TMobile = class');
  16011. Add(' strict private');
  16012. Add(' Some: string;');
  16013. Add(' end;');
  16014. Add(' TCar = class(tmobile)');
  16015. Add(' procedure Some;');
  16016. Add(' procedure Some(vA: longint);');
  16017. Add(' end;');
  16018. Add('procedure tcar.some;');
  16019. Add('begin');
  16020. Add(' Some;');
  16021. Add(' Some(1);');
  16022. Add('end;');
  16023. Add('procedure tcar.some(va: longint); begin end;');
  16024. Add('begin');
  16025. ConvertProgram;
  16026. CheckSource('TestClass_ReintroducedVar',
  16027. LinesToStr([ // statements
  16028. 'rtl.createClass(this, "TObject", null, function () {',
  16029. ' this.$init = function () {',
  16030. ' this.Some = 0;',
  16031. ' };',
  16032. ' this.$final = function () {',
  16033. ' };',
  16034. '});',
  16035. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16036. ' this.$init = function () {',
  16037. ' $mod.TObject.$init.call(this);',
  16038. ' this.Some$1 = "";',
  16039. ' };',
  16040. '});',
  16041. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16042. ' this.Some$2 = function () {',
  16043. ' this.Some$2();',
  16044. ' this.Some$3(1);',
  16045. ' };',
  16046. ' this.Some$3 = function (vA) {',
  16047. ' };',
  16048. '});',
  16049. '']),
  16050. LinesToStr([ // $mod.$main
  16051. '']));
  16052. end;
  16053. procedure TTestModule.TestClass_RaiseDescendant;
  16054. begin
  16055. StartProgram(false);
  16056. Add([
  16057. 'type',
  16058. ' TObject = class',
  16059. ' constructor Create(Msg: string);',
  16060. ' end;',
  16061. ' Exception = class',
  16062. ' end;',
  16063. ' EConvertError = class(Exception)',
  16064. ' end;',
  16065. 'constructor TObject.Create(Msg: string); begin end;',
  16066. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16067. 'begin',
  16068. ' raise Exception.Create(''Bar1'');',
  16069. ' raise EConvertError.Create(''Bar2'');',
  16070. ' raise AssertConv(''Bar2'');',
  16071. ' raise AssertConv;',
  16072. '']);
  16073. ConvertProgram;
  16074. CheckSource('TestClass_RaiseDescendant',
  16075. LinesToStr([ // statements
  16076. 'rtl.createClass(this, "TObject", null, function () {',
  16077. ' this.$init = function () {',
  16078. ' };',
  16079. ' this.$final = function () {',
  16080. ' };',
  16081. ' this.Create = function (Msg) {',
  16082. ' return this;',
  16083. ' };',
  16084. '});',
  16085. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16086. '});',
  16087. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16088. '});',
  16089. 'this.AssertConv = function (Msg) {',
  16090. ' var Result = null;',
  16091. ' return Result;',
  16092. '};',
  16093. '']),
  16094. LinesToStr([ // $mod.$main
  16095. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16096. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16097. 'throw $mod.AssertConv("Bar2");',
  16098. 'throw $mod.AssertConv("def");',
  16099. '']));
  16100. end;
  16101. procedure TTestModule.TestClass_ExternalMethod;
  16102. begin
  16103. AddModuleWithIntfImplSrc('unit2.pas',
  16104. LinesToStr([
  16105. 'type',
  16106. ' TObject = class',
  16107. ' public',
  16108. ' procedure Intern; external name ''$DoIntern'';',
  16109. ' end;',
  16110. '']),
  16111. LinesToStr([
  16112. '']));
  16113. StartUnit(true);
  16114. Add('interface');
  16115. Add('uses unit2;');
  16116. Add('type');
  16117. Add(' TCar = class(TObject)');
  16118. Add(' public');
  16119. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16120. Add(' procedure DoIt;');
  16121. Add(' end;');
  16122. Add('implementation');
  16123. Add('procedure tcar.doit;');
  16124. Add('begin');
  16125. Add(' Intern;');
  16126. Add(' Intern();');
  16127. Add(' Intern2;');
  16128. Add(' Intern2();');
  16129. Add('end;');
  16130. Add('var Obj: TCar;');
  16131. Add('begin');
  16132. Add(' obj.intern;');
  16133. Add(' obj.intern();');
  16134. Add(' obj.intern2;');
  16135. Add(' obj.intern2();');
  16136. Add(' obj.doit;');
  16137. Add(' obj.doit();');
  16138. Add(' with obj do begin');
  16139. Add(' Intern;');
  16140. Add(' Intern();');
  16141. Add(' Intern2;');
  16142. Add(' Intern2();');
  16143. Add(' end;');
  16144. ConvertUnit;
  16145. CheckSource('TestClass_ExternalMethod',
  16146. LinesToStr([
  16147. 'var $impl = $mod.$impl;',
  16148. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16149. ' this.DoIt = function () {',
  16150. ' this.$DoIntern();',
  16151. ' this.$DoIntern();',
  16152. ' this.$DoIntern2();',
  16153. ' this.$DoIntern2();',
  16154. ' };',
  16155. ' });',
  16156. '']),
  16157. LinesToStr([ // this.$init
  16158. '$impl.Obj.$DoIntern();',
  16159. '$impl.Obj.$DoIntern();',
  16160. '$impl.Obj.$DoIntern2();',
  16161. '$impl.Obj.$DoIntern2();',
  16162. '$impl.Obj.DoIt();',
  16163. '$impl.Obj.DoIt();',
  16164. 'var $with = $impl.Obj;',
  16165. '$with.$DoIntern();',
  16166. '$with.$DoIntern();',
  16167. '$with.$DoIntern2();',
  16168. '$with.$DoIntern2();',
  16169. '']),
  16170. LinesToStr([ // implementation
  16171. '$impl.Obj = null;',
  16172. '']) );
  16173. end;
  16174. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16175. begin
  16176. StartProgram(false);
  16177. Add('type');
  16178. Add(' TObject = class');
  16179. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16180. Add(' end;');
  16181. Add('begin');
  16182. SetExpectedPasResolverError('Virtual method name must match external',
  16183. nVirtualMethodNameMustMatchExternal);
  16184. ConvertProgram;
  16185. end;
  16186. procedure TTestModule.TestClass_ExternalOverrideFail;
  16187. begin
  16188. StartProgram(false);
  16189. Add('type');
  16190. Add(' TObject = class');
  16191. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16192. Add(' end;');
  16193. Add(' TCar = class');
  16194. Add(' procedure DoIt; override; external name ''DoIt'';');
  16195. Add(' end;');
  16196. Add('begin');
  16197. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16198. nInvalidXModifierY);
  16199. ConvertProgram;
  16200. end;
  16201. procedure TTestModule.TestClass_ExternalVar;
  16202. begin
  16203. AddModuleWithIntfImplSrc('unit2.pas',
  16204. LinesToStr([
  16205. '{$modeswitch externalclass}',
  16206. 'type',
  16207. ' TObject = class',
  16208. ' public',
  16209. ' Intern: longint external name ''$Intern'';',
  16210. ' Bracket: longint external name ''["A B"]'';',
  16211. ' end;',
  16212. '']),
  16213. LinesToStr([
  16214. '']));
  16215. StartUnit(true);
  16216. Add([
  16217. 'interface',
  16218. 'uses unit2;',
  16219. '{$modeswitch externalclass}',
  16220. 'type',
  16221. ' TCar = class(tobject)',
  16222. ' public',
  16223. ' Intern2: longint external name ''$Intern2'';',
  16224. ' procedure DoIt;',
  16225. ' end;',
  16226. 'implementation',
  16227. 'procedure tcar.doit;',
  16228. 'begin',
  16229. ' Intern:=Intern+1;',
  16230. ' Intern2:=Intern2+2;',
  16231. ' Bracket:=Bracket+3;',
  16232. 'end;',
  16233. 'var Obj: TCar;',
  16234. 'begin',
  16235. ' obj.intern:=obj.intern+1;',
  16236. ' obj.intern2:=obj.intern2+2;',
  16237. ' obj.Bracket:=obj.Bracket+3;',
  16238. ' with obj do begin',
  16239. ' intern:=intern+1;',
  16240. ' intern2:=intern2+2;',
  16241. ' Bracket:=Bracket+3;',
  16242. ' end;']);
  16243. ConvertUnit;
  16244. CheckSource('TestClass_ExternalVar',
  16245. LinesToStr([
  16246. 'var $impl = $mod.$impl;',
  16247. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16248. ' this.DoIt = function () {',
  16249. ' this.$Intern = this.$Intern + 1;',
  16250. ' this.$Intern2 = this.$Intern2 + 2;',
  16251. ' this["A B"] = this["A B"] + 3;',
  16252. ' };',
  16253. ' });',
  16254. '']),
  16255. LinesToStr([
  16256. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16257. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16258. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16259. 'var $with = $impl.Obj;',
  16260. '$with.$Intern = $with.$Intern + 1;',
  16261. '$with.$Intern2 = $with.$Intern2 + 2;',
  16262. '$with["A B"] = $with["A B"] + 3;',
  16263. '']),
  16264. LinesToStr([ // implementation
  16265. '$impl.Obj = null;',
  16266. '']));
  16267. end;
  16268. procedure TTestModule.TestClass_Const;
  16269. begin
  16270. StartProgram(false);
  16271. Add([
  16272. 'type',
  16273. ' integer = longint;',
  16274. ' TClass = class of TObject;',
  16275. ' TObject = class',
  16276. ' public',
  16277. ' const cI: integer = 3;',
  16278. ' procedure DoIt;',
  16279. ' class procedure DoMore;',
  16280. ' end;',
  16281. 'procedure tobject.doit;',
  16282. 'begin',
  16283. ' if cI=4 then;',
  16284. ' if 5=cI then;',
  16285. ' if Self.cI=6 then;',
  16286. ' if 7=Self.cI then;',
  16287. ' with Self do begin',
  16288. ' if cI=11 then;',
  16289. ' if 12=cI then;',
  16290. ' end;',
  16291. 'end;',
  16292. 'class procedure tobject.domore;',
  16293. 'begin',
  16294. ' if cI=8 then;',
  16295. ' if Self.cI=9 then;',
  16296. ' if 10=cI then;',
  16297. ' if 11=Self.cI then;',
  16298. ' with Self do begin',
  16299. ' if cI=13 then;',
  16300. ' if 14=cI then;',
  16301. ' end;',
  16302. 'end;',
  16303. 'var',
  16304. ' Obj: TObject;',
  16305. ' Cla: TClass;',
  16306. 'begin',
  16307. ' if TObject.cI=21 then ;',
  16308. ' if Obj.cI=22 then ;',
  16309. ' if Cla.cI=23 then ;',
  16310. ' with obj do if ci=24 then;',
  16311. ' with TObject do if ci=25 then;',
  16312. ' with Cla do if ci=26 then;']);
  16313. ConvertProgram;
  16314. CheckSource('TestClass_Const',
  16315. LinesToStr([
  16316. 'rtl.createClass(this, "TObject", null, function () {',
  16317. ' this.cI = 3;',
  16318. ' this.$init = function () {',
  16319. ' };',
  16320. ' this.$final = function () {',
  16321. ' };',
  16322. ' this.DoIt = function () {',
  16323. ' if (this.cI === 4) ;',
  16324. ' if (5 === this.cI) ;',
  16325. ' if (this.cI === 6) ;',
  16326. ' if (7 === this.cI) ;',
  16327. ' if (this.cI === 11) ;',
  16328. ' if (12 === this.cI) ;',
  16329. ' };',
  16330. ' this.DoMore = function () {',
  16331. ' if (this.cI === 8) ;',
  16332. ' if (this.cI === 9) ;',
  16333. ' if (10 === this.cI) ;',
  16334. ' if (11 === this.cI) ;',
  16335. ' if (this.cI === 13) ;',
  16336. ' if (14 === this.cI) ;',
  16337. ' };',
  16338. '});',
  16339. 'this.Obj = null;',
  16340. 'this.Cla = null;',
  16341. '']),
  16342. LinesToStr([
  16343. 'if ($mod.TObject.cI === 21) ;',
  16344. 'if ($mod.Obj.cI === 22) ;',
  16345. 'if ($mod.Cla.cI === 23) ;',
  16346. 'var $with = $mod.Obj;',
  16347. 'if ($with.cI === 24) ;',
  16348. 'var $with1 = $mod.TObject;',
  16349. 'if ($with1.cI === 25) ;',
  16350. 'var $with2 = $mod.Cla;',
  16351. 'if ($with2.cI === 26) ;',
  16352. '']));
  16353. end;
  16354. procedure TTestModule.TestClass_ConstEnum;
  16355. begin
  16356. StartProgram(false);
  16357. Add([
  16358. 'type',
  16359. ' TEnum = (red,blue);',
  16360. ' TObject = class',
  16361. ' end;',
  16362. ' TAnimal = class',
  16363. ' public',
  16364. ' type TSubEnum = (light,dark);',
  16365. ' const a = high(TEnum);',
  16366. ' const b = high(TSubEnum);',
  16367. ' end;',
  16368. ' TBird = class(TAnimal)',
  16369. ' public',
  16370. ' const c = high(TEnum);',
  16371. ' const d = high(TSubEnum);',
  16372. ' end;',
  16373. ' TAnt = class',
  16374. ' public',
  16375. ' const e = high(TEnum);',
  16376. ' const f = high(TBird.TSubEnum);',
  16377. ' end;',
  16378. 'begin',
  16379. '']);
  16380. ConvertProgram;
  16381. CheckSource('TestClass_ConstEnum',
  16382. LinesToStr([
  16383. 'this.TEnum = {',
  16384. ' "0": "red",',
  16385. ' red: 0,',
  16386. ' "1": "blue",',
  16387. ' blue: 1',
  16388. '};',
  16389. 'rtl.createClass(this, "TObject", null, function () {',
  16390. ' this.$init = function () {',
  16391. ' };',
  16392. ' this.$final = function () {',
  16393. ' };',
  16394. '});',
  16395. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16396. ' this.TSubEnum = {',
  16397. ' "0": "light",',
  16398. ' light: 0,',
  16399. ' "1": "dark",',
  16400. ' dark: 1',
  16401. ' };',
  16402. ' this.a = $mod.TEnum.blue;',
  16403. ' this.b = this.TSubEnum.dark;',
  16404. '});',
  16405. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16406. ' this.c = $mod.TEnum.blue;',
  16407. ' this.d = this.TSubEnum.dark;',
  16408. '});',
  16409. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16410. ' this.e = $mod.TEnum.blue;',
  16411. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16412. '});',
  16413. '']),
  16414. LinesToStr([
  16415. '']));
  16416. end;
  16417. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16418. begin
  16419. StartProgram(false);
  16420. Add([
  16421. 'type',
  16422. ' TObject = class',
  16423. ' const cI: longint = 3;',
  16424. ' procedure Fly;',
  16425. ' procedure Run;',
  16426. ' end;',
  16427. ' TBird = class',
  16428. ' procedure Go;',
  16429. ' end;',
  16430. 'procedure tobject.fly;',
  16431. 'const cI: word = 4;',
  16432. 'begin',
  16433. ' if cI=Self.cI then ;',
  16434. 'end;',
  16435. 'procedure tobject.run;',
  16436. 'const cI: word = 5;',
  16437. 'begin',
  16438. ' if cI=Self.cI then ;',
  16439. 'end;',
  16440. 'procedure tbird.go;',
  16441. 'const cI: word = 6;',
  16442. 'begin',
  16443. ' if cI=Self.cI then ;',
  16444. 'end;',
  16445. 'begin',
  16446. '']);
  16447. ConvertProgram;
  16448. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16449. LinesToStr([
  16450. 'rtl.createClass(this, "TObject", null, function () {',
  16451. ' this.cI = 3;',
  16452. ' this.$init = function () {',
  16453. ' };',
  16454. ' this.$final = function () {',
  16455. ' };',
  16456. ' var cI$1 = 4;',
  16457. ' this.Fly = function () {',
  16458. ' if (cI$1 === this.cI) ;',
  16459. ' };',
  16460. ' var cI$2 = 5;',
  16461. ' this.Run = function () {',
  16462. ' if (cI$2 === this.cI) ;',
  16463. ' };',
  16464. '});',
  16465. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16466. ' var cI$3 = 6;',
  16467. ' this.Go = function () {',
  16468. ' if (cI$3 === this.cI) ;',
  16469. ' };',
  16470. '});',
  16471. '']),
  16472. LinesToStr([
  16473. '']));
  16474. end;
  16475. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16476. begin
  16477. StartUnit(false);
  16478. Add([
  16479. 'interface',
  16480. 'type',
  16481. ' TObject = class',
  16482. ' const cI: longint = 3;',
  16483. ' procedure Fly;',
  16484. ' procedure Run;',
  16485. ' end;',
  16486. ' TBird = class',
  16487. ' procedure Go;',
  16488. ' end;',
  16489. 'implementation',
  16490. 'procedure tobject.fly;',
  16491. 'const cI: word = 4;',
  16492. 'begin',
  16493. ' if cI=Self.cI then ;',
  16494. 'end;',
  16495. 'procedure tobject.run;',
  16496. 'const cI: word = 5;',
  16497. 'begin',
  16498. ' if cI=Self.cI then ;',
  16499. 'end;',
  16500. 'procedure tbird.go;',
  16501. 'const cI: word = 6;',
  16502. 'begin',
  16503. ' if cI=Self.cI then ;',
  16504. 'end;',
  16505. '']);
  16506. ConvertUnit;
  16507. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16508. LinesToStr([
  16509. 'rtl.createClass(this, "TObject", null, function () {',
  16510. ' this.cI = 3;',
  16511. ' this.$init = function () {',
  16512. ' };',
  16513. ' this.$final = function () {',
  16514. ' };',
  16515. ' var cI$1 = 4;',
  16516. ' this.Fly = function () {',
  16517. ' if (cI$1 === this.cI) ;',
  16518. ' };',
  16519. ' var cI$2 = 5;',
  16520. ' this.Run = function () {',
  16521. ' if (cI$2 === this.cI) ;',
  16522. ' };',
  16523. '});',
  16524. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16525. ' var cI$3 = 6;',
  16526. ' this.Go = function () {',
  16527. ' if (cI$3 === this.cI) ;',
  16528. ' };',
  16529. '});',
  16530. '']),
  16531. '',
  16532. '');
  16533. end;
  16534. procedure TTestModule.TestClass_LocalVarSelfFail;
  16535. begin
  16536. StartProgram(false);
  16537. Add([
  16538. 'type',
  16539. ' TObject = class',
  16540. ' constructor Create;',
  16541. ' end;',
  16542. 'constructor tobject.create;',
  16543. 'var self: longint;',
  16544. 'begin',
  16545. 'end',
  16546. 'begin',
  16547. '']);
  16548. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16549. ConvertProgram;
  16550. end;
  16551. procedure TTestModule.TestClass_ArgSelfFail;
  16552. begin
  16553. StartProgram(false);
  16554. Add([
  16555. 'type',
  16556. ' TObject = class',
  16557. ' procedure DoIt(Self: longint);',
  16558. ' end;',
  16559. 'procedure tobject.doit(self: longint);',
  16560. 'begin',
  16561. 'end',
  16562. 'begin',
  16563. '']);
  16564. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16565. ConvertProgram;
  16566. end;
  16567. procedure TTestModule.TestClass_NestedProcSelf;
  16568. begin
  16569. StartProgram(false);
  16570. Add([
  16571. 'type',
  16572. ' TObject = class',
  16573. ' Key: longint;',
  16574. ' class var State: longint;',
  16575. ' procedure DoIt;',
  16576. ' function GetSize: longint; virtual; abstract;',
  16577. ' procedure SetSize(Value: longint); virtual; abstract;',
  16578. ' property Size: longint read GetSize write SetSize;',
  16579. ' end;',
  16580. 'procedure tobject.doit;',
  16581. ' procedure Sub;',
  16582. ' begin',
  16583. ' key:=key+2;',
  16584. ' self.key:=self.key+3;',
  16585. ' state:=state+4;',
  16586. ' self.state:=self.state+5;',
  16587. ' tobject.state:=tobject.state+6;',
  16588. ' size:=size+7;',
  16589. ' self.size:=self.size+8;',
  16590. ' end;',
  16591. 'begin',
  16592. ' sub;',
  16593. ' key:=key+12;',
  16594. ' self.key:=self.key+13;',
  16595. ' state:=state+14;',
  16596. ' self.state:=self.state+15;',
  16597. ' tobject.state:=tobject.state+16;',
  16598. ' size:=size+17;',
  16599. ' self.size:=self.size+18;',
  16600. 'end;',
  16601. 'begin',
  16602. '']);
  16603. ConvertProgram;
  16604. CheckSource('TestClass_NestedProcSelf',
  16605. LinesToStr([ // statements
  16606. 'rtl.createClass(this, "TObject", null, function () {',
  16607. ' this.State = 0;',
  16608. ' this.$init = function () {',
  16609. ' this.Key = 0;',
  16610. ' };',
  16611. ' this.$final = function () {',
  16612. ' };',
  16613. ' this.DoIt = function () {',
  16614. ' var $Self = this;',
  16615. ' function Sub() {',
  16616. ' $Self.Key = $Self.Key + 2;',
  16617. ' $Self.Key = $Self.Key + 3;',
  16618. ' $mod.TObject.State = $Self.State + 4;',
  16619. ' $mod.TObject.State = $Self.State + 5;',
  16620. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16621. ' $Self.SetSize($Self.GetSize() + 7);',
  16622. ' $Self.SetSize($Self.GetSize() + 8);',
  16623. ' };',
  16624. ' Sub();',
  16625. ' this.Key = this.Key + 12;',
  16626. ' $Self.Key = $Self.Key + 13;',
  16627. ' $mod.TObject.State = this.State + 14;',
  16628. ' $mod.TObject.State = $Self.State + 15;',
  16629. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16630. ' this.SetSize(this.GetSize() + 17);',
  16631. ' $Self.SetSize($Self.GetSize() + 18);',
  16632. ' };',
  16633. '});',
  16634. '']),
  16635. LinesToStr([ // $mod.$main
  16636. '']));
  16637. end;
  16638. procedure TTestModule.TestClass_NestedProcSelf2;
  16639. begin
  16640. StartProgram(false);
  16641. Add([
  16642. 'type',
  16643. ' TObject = class',
  16644. ' Key: longint;',
  16645. ' class var State: longint;',
  16646. ' function GetSize: longint; virtual; abstract;',
  16647. ' procedure SetSize(Value: longint); virtual; abstract;',
  16648. ' property Size: longint read GetSize write SetSize;',
  16649. ' end;',
  16650. ' TBird = class',
  16651. ' procedure DoIt;',
  16652. ' end;',
  16653. 'procedure tbird.doit;',
  16654. ' procedure Sub;',
  16655. ' begin',
  16656. ' key:=key+2;',
  16657. ' self.key:=self.key+3;',
  16658. ' state:=state+4;',
  16659. ' self.state:=self.state+5;',
  16660. ' tobject.state:=tobject.state+6;',
  16661. ' size:=size+7;',
  16662. ' self.size:=self.size+8;',
  16663. ' end;',
  16664. 'begin',
  16665. ' sub;',
  16666. ' key:=key+12;',
  16667. ' self.key:=self.key+13;',
  16668. ' state:=state+14;',
  16669. ' self.state:=self.state+15;',
  16670. ' tobject.state:=tobject.state+16;',
  16671. ' size:=size+17;',
  16672. ' self.size:=self.size+18;',
  16673. 'end;',
  16674. 'begin',
  16675. '']);
  16676. ConvertProgram;
  16677. CheckSource('TestClass_NestedProcSelf2',
  16678. LinesToStr([ // statements
  16679. 'rtl.createClass(this, "TObject", null, function () {',
  16680. ' this.State = 0;',
  16681. ' this.$init = function () {',
  16682. ' this.Key = 0;',
  16683. ' };',
  16684. ' this.$final = function () {',
  16685. ' };',
  16686. '});',
  16687. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16688. ' this.DoIt = function () {',
  16689. ' var $Self = this;',
  16690. ' function Sub() {',
  16691. ' $Self.Key = $Self.Key + 2;',
  16692. ' $Self.Key = $Self.Key + 3;',
  16693. ' $mod.TObject.State = $Self.State + 4;',
  16694. ' $mod.TObject.State = $Self.State + 5;',
  16695. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16696. ' $Self.SetSize($Self.GetSize() + 7);',
  16697. ' $Self.SetSize($Self.GetSize() + 8);',
  16698. ' };',
  16699. ' Sub();',
  16700. ' this.Key = this.Key + 12;',
  16701. ' $Self.Key = $Self.Key + 13;',
  16702. ' $mod.TObject.State = this.State + 14;',
  16703. ' $mod.TObject.State = $Self.State + 15;',
  16704. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16705. ' this.SetSize(this.GetSize() + 17);',
  16706. ' $Self.SetSize($Self.GetSize() + 18);',
  16707. ' };',
  16708. '});',
  16709. '']),
  16710. LinesToStr([ // $mod.$main
  16711. '']));
  16712. end;
  16713. procedure TTestModule.TestClass_NestedProcClassSelf;
  16714. begin
  16715. StartProgram(false);
  16716. Add([
  16717. 'type',
  16718. ' TObject = class',
  16719. ' class var State: longint;',
  16720. ' class procedure DoIt;',
  16721. ' class function GetSize: longint; virtual; abstract;',
  16722. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16723. ' class property Size: longint read GetSize write SetSize;',
  16724. ' end;',
  16725. 'class procedure tobject.doit;',
  16726. ' procedure Sub;',
  16727. ' begin',
  16728. ' state:=state+2;',
  16729. ' self.state:=self.state+3;',
  16730. ' tobject.state:=tobject.state+4;',
  16731. ' size:=size+5;',
  16732. ' self.size:=self.size+6;',
  16733. ' tobject.size:=tobject.size+7;',
  16734. ' end;',
  16735. 'begin',
  16736. ' sub;',
  16737. ' state:=state+12;',
  16738. ' self.state:=self.state+13;',
  16739. ' tobject.state:=tobject.state+14;',
  16740. ' size:=size+15;',
  16741. ' self.size:=self.size+16;',
  16742. ' tobject.size:=tobject.size+17;',
  16743. 'end;',
  16744. 'begin',
  16745. '']);
  16746. ConvertProgram;
  16747. CheckSource('TestClass_NestedProcClassSelf',
  16748. LinesToStr([ // statements
  16749. 'rtl.createClass(this, "TObject", null, function () {',
  16750. ' this.State = 0;',
  16751. ' this.$init = function () {',
  16752. ' };',
  16753. ' this.$final = function () {',
  16754. ' };',
  16755. ' this.DoIt = function () {',
  16756. ' var $Self = this;',
  16757. ' function Sub() {',
  16758. ' $mod.TObject.State = $Self.State + 2;',
  16759. ' $mod.TObject.State = $Self.State + 3;',
  16760. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16761. ' $Self.SetSize($Self.GetSize() + 5);',
  16762. ' $Self.SetSize($Self.GetSize() + 6);',
  16763. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16764. ' };',
  16765. ' Sub();',
  16766. ' $mod.TObject.State = this.State + 12;',
  16767. ' $mod.TObject.State = $Self.State + 13;',
  16768. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16769. ' this.SetSize(this.GetSize() + 15);',
  16770. ' $Self.SetSize($Self.GetSize() + 16);',
  16771. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16772. ' };',
  16773. '});',
  16774. '']),
  16775. LinesToStr([ // $mod.$main
  16776. '']));
  16777. end;
  16778. procedure TTestModule.TestClass_NestedProcCallInherited;
  16779. begin
  16780. StartProgram(false);
  16781. Add([
  16782. 'type',
  16783. ' TObject = class',
  16784. ' function DoIt(k: boolean): longint; virtual;',
  16785. ' end;',
  16786. ' TBird = class',
  16787. ' function DoIt(k: boolean): longint; override;',
  16788. ' end;',
  16789. 'function tobject.doit(k: boolean): longint;',
  16790. 'begin',
  16791. 'end;',
  16792. 'function tbird.doit(k: boolean): longint;',
  16793. ' procedure Sub;',
  16794. ' begin',
  16795. ' inherited DoIt(true);',
  16796. //' if inherited DoIt(false)=4 then ;',
  16797. ' end;',
  16798. 'begin',
  16799. ' Sub;',
  16800. ' inherited;',
  16801. ' inherited DoIt(true);',
  16802. //' if inherited DoIt(false)=14 then ;',
  16803. 'end;',
  16804. 'begin',
  16805. '']);
  16806. ConvertProgram;
  16807. CheckSource('TestClass_NestedProcCallInherited',
  16808. LinesToStr([ // statements
  16809. 'rtl.createClass(this, "TObject", null, function () {',
  16810. ' this.$init = function () {',
  16811. ' };',
  16812. ' this.$final = function () {',
  16813. ' };',
  16814. ' this.DoIt = function (k) {',
  16815. ' var Result = 0;',
  16816. ' return Result;',
  16817. ' };',
  16818. '});',
  16819. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16820. ' this.DoIt = function (k) {',
  16821. ' var $Self = this;',
  16822. ' var Result = 0;',
  16823. ' function Sub() {',
  16824. ' $mod.TObject.DoIt.call($Self, true);',
  16825. ' };',
  16826. ' Sub();',
  16827. ' $mod.TObject.DoIt.apply(this, arguments);',
  16828. ' $mod.TObject.DoIt.call(this, true);',
  16829. ' return Result;',
  16830. ' };',
  16831. '});',
  16832. '']),
  16833. LinesToStr([ // $mod.$main
  16834. '']));
  16835. end;
  16836. procedure TTestModule.TestClass_TObjectFree;
  16837. begin
  16838. StartProgram(false);
  16839. Add([
  16840. 'type',
  16841. ' TObject = class',
  16842. ' Obj: tobject;',
  16843. ' procedure Free;',
  16844. ' procedure Release;',
  16845. ' end;',
  16846. 'procedure tobject.free;',
  16847. 'begin',
  16848. 'end;',
  16849. 'procedure tobject.release;',
  16850. 'begin',
  16851. ' free;',
  16852. ' if true then free;',
  16853. 'end;',
  16854. 'function DoIt(o: tobject): tobject;',
  16855. 'var l: tobject;',
  16856. 'begin',
  16857. ' o.free;',
  16858. ' o.free();',
  16859. ' l.free;',
  16860. ' l.free();',
  16861. ' o.obj.free;',
  16862. ' o.obj.free();',
  16863. ' with o do obj.free;',
  16864. ' with o do obj.free();',
  16865. ' result.Free;',
  16866. ' result.Free();',
  16867. 'end;',
  16868. 'var o: tobject;',
  16869. ' a: array of tobject;',
  16870. 'begin',
  16871. ' o.free;',
  16872. ' o.obj.free;',
  16873. ' a[1+2].free;',
  16874. '']);
  16875. ConvertProgram;
  16876. CheckSource('TestClass_TObjectFree',
  16877. LinesToStr([ // statements
  16878. 'rtl.createClass(this, "TObject", null, function () {',
  16879. ' this.$init = function () {',
  16880. ' this.Obj = null;',
  16881. ' };',
  16882. ' this.$final = function () {',
  16883. ' this.Obj = undefined;',
  16884. ' };',
  16885. ' this.Free = function () {',
  16886. ' };',
  16887. ' this.Release = function () {',
  16888. ' this.Free();',
  16889. ' if (true) this.Free();',
  16890. ' };',
  16891. '});',
  16892. 'this.DoIt = function (o) {',
  16893. ' var Result = null;',
  16894. ' var l = null;',
  16895. ' o = rtl.freeLoc(o);',
  16896. ' o = rtl.freeLoc(o);',
  16897. ' l = rtl.freeLoc(l);',
  16898. ' l = rtl.freeLoc(l);',
  16899. ' rtl.free(o, "Obj");',
  16900. ' rtl.free(o, "Obj");',
  16901. ' rtl.free(o, "Obj");',
  16902. ' rtl.free(o, "Obj");',
  16903. ' Result = rtl.freeLoc(Result);',
  16904. ' Result = rtl.freeLoc(Result);',
  16905. ' return Result;',
  16906. '};',
  16907. 'this.o = null;',
  16908. 'this.a = [];',
  16909. '']),
  16910. LinesToStr([ // $mod.$main
  16911. 'rtl.free($mod, "o");',
  16912. 'rtl.free($mod.o, "Obj");',
  16913. 'rtl.free($mod.a, 1 + 2);',
  16914. '']));
  16915. end;
  16916. procedure TTestModule.TestClass_TObjectFree_VarArg;
  16917. begin
  16918. StartProgram(false);
  16919. Add([
  16920. 'type',
  16921. ' TObject = class',
  16922. ' Obj: tobject;',
  16923. ' procedure Free;',
  16924. ' end;',
  16925. 'procedure tobject.free;',
  16926. 'begin',
  16927. 'end;',
  16928. 'procedure DoIt(var o: tobject);',
  16929. 'begin',
  16930. ' o.free;',
  16931. ' o.free();',
  16932. 'end;',
  16933. 'begin',
  16934. '']);
  16935. ConvertProgram;
  16936. CheckSource('TestClass_TObjectFree_VarArg',
  16937. LinesToStr([ // statements
  16938. 'rtl.createClass(this, "TObject", null, function () {',
  16939. ' this.$init = function () {',
  16940. ' this.Obj = null;',
  16941. ' };',
  16942. ' this.$final = function () {',
  16943. ' this.Obj = undefined;',
  16944. ' };',
  16945. ' this.Free = function () {',
  16946. ' };',
  16947. '});',
  16948. 'this.DoIt = function (o) {',
  16949. ' o.set(rtl.freeLoc(o.get()));',
  16950. ' o.set(rtl.freeLoc(o.get()));',
  16951. '};',
  16952. '']),
  16953. LinesToStr([ // $mod.$main
  16954. '']));
  16955. end;
  16956. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  16957. begin
  16958. StartProgram(false);
  16959. Add([
  16960. 'type',
  16961. ' TObject = class',
  16962. ' constructor Create;',
  16963. ' procedure Free;',
  16964. ' end;',
  16965. 'constructor TObject.Create; begin end;',
  16966. 'procedure tobject.free; begin end;',
  16967. 'begin',
  16968. ' with tobject.create do free;',
  16969. '']);
  16970. ConvertProgram;
  16971. CheckSource('TestClass_TObjectFreeNewInstance',
  16972. LinesToStr([ // statements
  16973. 'rtl.createClass(this, "TObject", null, function () {',
  16974. ' this.$init = function () {',
  16975. ' };',
  16976. ' this.$final = function () {',
  16977. ' };',
  16978. ' this.Create = function () {',
  16979. ' return this;',
  16980. ' };',
  16981. ' this.Free = function () {',
  16982. ' };',
  16983. '});',
  16984. '']),
  16985. LinesToStr([ // $mod.$main
  16986. 'var $with = $mod.TObject.$create("Create");',
  16987. '$with=rtl.freeLoc($with);',
  16988. '']));
  16989. end;
  16990. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  16991. begin
  16992. StartProgram(false);
  16993. Add([
  16994. 'type',
  16995. ' TObject = class',
  16996. ' destructor Destroy;',
  16997. ' procedure Free;',
  16998. ' end;',
  16999. 'destructor TObject.Destroy; begin end;',
  17000. 'procedure tobject.free; begin end;',
  17001. 'var o: tobject;',
  17002. 'begin',
  17003. ' o.free;',
  17004. '']);
  17005. Converter.UseLowerCase:=true;
  17006. ConvertProgram;
  17007. CheckSource('TestClass_TObjectFreeLowerCase',
  17008. LinesToStr([ // statements
  17009. 'rtl.createClass(this, "tobject", null, function () {',
  17010. ' this.$init = function () {',
  17011. ' };',
  17012. ' this.$final = function () {',
  17013. ' };',
  17014. ' rtl.tObjectDestroy = "destroy";',
  17015. ' this.destroy = function () {',
  17016. ' };',
  17017. ' this.free = function () {',
  17018. ' };',
  17019. '});',
  17020. 'this.o = null;',
  17021. '']),
  17022. LinesToStr([ // $mod.$main
  17023. 'rtl.free($mod, "o");',
  17024. '']));
  17025. end;
  17026. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17027. begin
  17028. StartProgram(false);
  17029. Add([
  17030. 'type',
  17031. ' TObject = class',
  17032. ' procedure Free;',
  17033. ' function GetObj: tobject; virtual; abstract;',
  17034. ' end;',
  17035. 'procedure tobject.free;',
  17036. 'begin',
  17037. 'end;',
  17038. 'var o: tobject;',
  17039. 'begin',
  17040. ' o.getobj.free;',
  17041. '']);
  17042. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17043. ConvertProgram;
  17044. end;
  17045. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17046. begin
  17047. StartProgram(false);
  17048. Add([
  17049. 'type',
  17050. ' TObject = class',
  17051. ' procedure Free;',
  17052. ' FObj: TObject;',
  17053. ' property Obj: tobject read FObj write FObj;',
  17054. ' end;',
  17055. 'procedure tobject.free;',
  17056. 'begin',
  17057. 'end;',
  17058. 'var o: tobject;',
  17059. 'begin',
  17060. ' o.obj.free;',
  17061. '']);
  17062. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17063. ConvertProgram;
  17064. end;
  17065. procedure TTestModule.TestClass_ForIn;
  17066. begin
  17067. StartProgram(false);
  17068. Add([
  17069. 'type',
  17070. ' TObject = class end;',
  17071. ' TItem = TObject;',
  17072. ' TEnumerator = class',
  17073. ' FCurrent: TItem;',
  17074. ' property Current: TItem read FCurrent;',
  17075. ' function MoveNext: boolean;',
  17076. ' end;',
  17077. ' TBird = class',
  17078. ' function GetEnumerator: TEnumerator;',
  17079. ' end;',
  17080. 'function TEnumerator.MoveNext: boolean;',
  17081. 'begin',
  17082. 'end;',
  17083. 'function TBird.GetEnumerator: TEnumerator;',
  17084. 'begin',
  17085. 'end;',
  17086. 'var',
  17087. ' b: TBird;',
  17088. ' i, i2: TItem;',
  17089. 'begin',
  17090. ' for i in b do i2:=i;']);
  17091. ConvertProgram;
  17092. CheckSource('TestClass_ForIn',
  17093. LinesToStr([ // statements
  17094. 'rtl.createClass(this, "TObject", null, function () {',
  17095. ' this.$init = function () {',
  17096. ' };',
  17097. ' this.$final = function () {',
  17098. ' };',
  17099. '});',
  17100. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17101. ' this.$init = function () {',
  17102. ' $mod.TObject.$init.call(this);',
  17103. ' this.FCurrent = null;',
  17104. ' };',
  17105. ' this.$final = function () {',
  17106. ' this.FCurrent = undefined;',
  17107. ' $mod.TObject.$final.call(this);',
  17108. ' };',
  17109. ' this.MoveNext = function () {',
  17110. ' var Result = false;',
  17111. ' return Result;',
  17112. ' };',
  17113. '});',
  17114. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17115. ' this.GetEnumerator = function () {',
  17116. ' var Result = null;',
  17117. ' return Result;',
  17118. ' };',
  17119. '});',
  17120. 'this.b = null;',
  17121. 'this.i = null;',
  17122. 'this.i2 = null;'
  17123. ]),
  17124. LinesToStr([ // $mod.$main
  17125. 'var $in = $mod.b.GetEnumerator();',
  17126. 'try {',
  17127. ' while ($in.MoveNext()){',
  17128. ' $mod.i = $in.FCurrent;',
  17129. ' $mod.i2 = $mod.i;',
  17130. ' }',
  17131. '} finally {',
  17132. ' $in = rtl.freeLoc($in)',
  17133. '};',
  17134. '']));
  17135. end;
  17136. procedure TTestModule.TestClass_DispatchMessage;
  17137. begin
  17138. StartProgram(false);
  17139. Add([
  17140. 'type',
  17141. ' {$DispatchField DispInt}',
  17142. ' {$DispatchStrField DispStr}',
  17143. ' TObject = class',
  17144. ' procedure Dispatch(var Msg); virtual; abstract;',
  17145. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17146. ' end;',
  17147. ' THopMsg = record',
  17148. ' DispInt: longint;',
  17149. ' end;',
  17150. ' TPutMsg = record',
  17151. ' DispStr: string;',
  17152. ' end;',
  17153. ' TBird = class',
  17154. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17155. ' procedure Run; overload; virtual; abstract;',
  17156. ' procedure Run(var Msg); overload; message ''Fast'';',
  17157. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17158. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17159. ' end;',
  17160. 'procedure TBird.Run(var Msg);',
  17161. 'begin',
  17162. 'end;',
  17163. 'begin',
  17164. '']);
  17165. ConvertProgram;
  17166. CheckResolverUnexpectedHints(true);
  17167. CheckSource('TestClass_Message',
  17168. LinesToStr([ // statements
  17169. 'rtl.createClass(this, "TObject", null, function () {',
  17170. ' this.$init = function () {',
  17171. ' };',
  17172. ' this.$final = function () {',
  17173. ' };',
  17174. '});',
  17175. 'rtl.recNewT(this, "THopMsg", function () {',
  17176. ' this.DispInt = 0;',
  17177. ' this.$eq = function (b) {',
  17178. ' return this.DispInt === b.DispInt;',
  17179. ' };',
  17180. ' this.$assign = function (s) {',
  17181. ' this.DispInt = s.DispInt;',
  17182. ' return this;',
  17183. ' };',
  17184. '});',
  17185. 'rtl.recNewT(this, "TPutMsg", function () {',
  17186. ' this.DispStr = "";',
  17187. ' this.$eq = function (b) {',
  17188. ' return this.DispStr === b.DispStr;',
  17189. ' };',
  17190. ' this.$assign = function (s) {',
  17191. ' this.DispStr = s.DispStr;',
  17192. ' return this;',
  17193. ' };',
  17194. '});',
  17195. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17196. ' this.Run$1 = function (Msg) {',
  17197. ' };',
  17198. ' this.$msgint = {',
  17199. ' "2": "Fly",',
  17200. ' "3": "Hop"',
  17201. ' };',
  17202. ' this.$msgstr = {',
  17203. ' Fast: "Run$1",',
  17204. ' foo: "Put"',
  17205. ' };',
  17206. '});',
  17207. '']),
  17208. LinesToStr([ // $mod.$main
  17209. '']));
  17210. end;
  17211. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17212. begin
  17213. StartProgram(false);
  17214. Add([
  17215. 'type',
  17216. ' TObject = class',
  17217. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17218. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17219. ' end;',
  17220. 'begin',
  17221. '']);
  17222. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17223. ConvertProgram;
  17224. end;
  17225. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17226. begin
  17227. StartProgram(false);
  17228. Add([
  17229. 'type',
  17230. ' TObject = class',
  17231. ' procedure Dispatch(var Msg); virtual; abstract;',
  17232. ' end;',
  17233. ' TFlyMsg = record',
  17234. ' FlyId: longint;',
  17235. ' end;',
  17236. ' TBird = class',
  17237. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17238. ' end;',
  17239. 'begin',
  17240. '']);
  17241. ConvertProgram;
  17242. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17243. end;
  17244. procedure TTestModule.TestClassOf_Create;
  17245. begin
  17246. StartProgram(false);
  17247. Add('type');
  17248. Add(' TObject = class');
  17249. Add(' constructor Create;');
  17250. Add(' end;');
  17251. Add(' TClass = class of TObject;');
  17252. Add('constructor tobject.create; begin end;');
  17253. Add('var');
  17254. Add(' Obj: tobject;');
  17255. Add(' C: tclass;');
  17256. Add('begin');
  17257. Add(' obj:=C.create;');
  17258. Add(' with c do obj:=create;');
  17259. ConvertProgram;
  17260. CheckSource('TestClassOf_Create',
  17261. LinesToStr([ // statements
  17262. 'rtl.createClass(this, "TObject", null, function () {',
  17263. ' this.$init = function () {',
  17264. ' };',
  17265. ' this.$final = function () {',
  17266. ' };',
  17267. ' this.Create = function () {',
  17268. ' return this;',
  17269. ' };',
  17270. '});',
  17271. 'this.Obj = null;',
  17272. 'this.C = null;'
  17273. ]),
  17274. LinesToStr([ // $mod.$main
  17275. '$mod.Obj = $mod.C.$create("Create");',
  17276. 'var $with = $mod.C;',
  17277. '$mod.Obj = $with.$create("Create");',
  17278. '']));
  17279. end;
  17280. procedure TTestModule.TestClassOf_Call;
  17281. begin
  17282. StartProgram(false);
  17283. Add('type');
  17284. Add(' TObject = class');
  17285. Add(' class procedure DoIt;');
  17286. Add(' end;');
  17287. Add(' TClass = class of TObject;');
  17288. Add('class procedure tobject.doit; begin end;');
  17289. Add('var');
  17290. Add(' C: tclass;');
  17291. Add('begin');
  17292. Add(' c.doit;');
  17293. Add(' with c do doit;');
  17294. ConvertProgram;
  17295. CheckSource('TestClassOf_Call',
  17296. LinesToStr([ // statements
  17297. 'rtl.createClass(this, "TObject", null, function () {',
  17298. ' this.$init = function () {',
  17299. ' };',
  17300. ' this.$final = function () {',
  17301. ' };',
  17302. ' this.DoIt = function () {',
  17303. ' };',
  17304. '});',
  17305. 'this.C = null;'
  17306. ]),
  17307. LinesToStr([ // $mod.$main
  17308. '$mod.C.DoIt();',
  17309. 'var $with = $mod.C;',
  17310. '$with.DoIt();',
  17311. '']));
  17312. end;
  17313. procedure TTestModule.TestClassOf_Assign;
  17314. begin
  17315. StartProgram(false);
  17316. Add('type');
  17317. Add(' TClass = class of TObject;');
  17318. Add(' TObject = class');
  17319. Add(' ClassType: TClass; ');
  17320. Add(' end;');
  17321. Add('var');
  17322. Add(' Obj: tobject;');
  17323. Add(' C: tclass;');
  17324. Add('begin');
  17325. Add(' c:=nil;');
  17326. Add(' c:=obj.classtype;');
  17327. ConvertProgram;
  17328. CheckSource('TestClassOf_Assign',
  17329. LinesToStr([ // statements
  17330. 'rtl.createClass(this, "TObject", null, function () {',
  17331. ' this.$init = function () {',
  17332. ' this.ClassType = null;',
  17333. ' };',
  17334. ' this.$final = function () {',
  17335. ' this.ClassType = undefined;',
  17336. ' };',
  17337. '});',
  17338. 'this.Obj = null;',
  17339. 'this.C = null;'
  17340. ]),
  17341. LinesToStr([ // $mod.$main
  17342. '$mod.C = null;',
  17343. '$mod.C = $mod.Obj.ClassType;',
  17344. '']));
  17345. end;
  17346. procedure TTestModule.TestClassOf_Is;
  17347. begin
  17348. StartProgram(false);
  17349. Add('type');
  17350. Add(' TClass = class of TObject;');
  17351. Add(' TObject = class');
  17352. Add(' end;');
  17353. Add(' TCar = class');
  17354. Add(' end;');
  17355. Add(' TCars = class of TCar;');
  17356. Add('var');
  17357. Add(' Obj: tobject;');
  17358. Add(' C: tclass;');
  17359. Add(' Cars: tcars;');
  17360. Add('begin');
  17361. Add(' if c is tcar then ;');
  17362. Add(' if c is tcars then ;');
  17363. ConvertProgram;
  17364. CheckSource('TestClassOf_Is',
  17365. LinesToStr([ // statements
  17366. 'rtl.createClass(this, "TObject", null, function () {',
  17367. ' this.$init = function () {',
  17368. ' };',
  17369. ' this.$final = function () {',
  17370. ' };',
  17371. '});',
  17372. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17373. '});',
  17374. 'this.Obj = null;',
  17375. 'this.C = null;',
  17376. 'this.Cars = null;'
  17377. ]),
  17378. LinesToStr([ // $mod.$main
  17379. 'if(rtl.is($mod.C,$mod.TCar));',
  17380. 'if(rtl.is($mod.C,$mod.TCar));',
  17381. '']));
  17382. end;
  17383. procedure TTestModule.TestClassOf_Compare;
  17384. begin
  17385. StartProgram(false);
  17386. Add('type');
  17387. Add(' TClass = class of TObject;');
  17388. Add(' TObject = class');
  17389. Add(' ClassType: TClass; ');
  17390. Add(' end;');
  17391. Add('var');
  17392. Add(' b: boolean;');
  17393. Add(' Obj: tobject;');
  17394. Add(' C: tclass;');
  17395. Add('begin');
  17396. Add(' b:=c=nil;');
  17397. Add(' b:=nil=c;');
  17398. Add(' b:=c=obj.classtype;');
  17399. Add(' b:=obj.classtype=c;');
  17400. Add(' b:=c=TObject;');
  17401. Add(' b:=TObject=c;');
  17402. Add(' b:=c<>nil;');
  17403. Add(' b:=nil<>c;');
  17404. Add(' b:=c<>obj.classtype;');
  17405. Add(' b:=obj.classtype<>c;');
  17406. Add(' b:=c<>TObject;');
  17407. Add(' b:=TObject<>c;');
  17408. ConvertProgram;
  17409. CheckSource('TestClassOf_Compare',
  17410. LinesToStr([ // statements
  17411. 'rtl.createClass(this, "TObject", null, function () {',
  17412. ' this.$init = function () {',
  17413. ' this.ClassType = null;',
  17414. ' };',
  17415. ' this.$final = function () {',
  17416. ' this.ClassType = undefined;',
  17417. ' };',
  17418. '});',
  17419. 'this.b = false;',
  17420. 'this.Obj = null;',
  17421. 'this.C = null;'
  17422. ]),
  17423. LinesToStr([ // $mod.$main
  17424. '$mod.b = $mod.C === null;',
  17425. '$mod.b = null === $mod.C;',
  17426. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17427. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17428. '$mod.b = $mod.C === $mod.TObject;',
  17429. '$mod.b = $mod.TObject === $mod.C;',
  17430. '$mod.b = $mod.C !== null;',
  17431. '$mod.b = null !== $mod.C;',
  17432. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17433. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17434. '$mod.b = $mod.C !== $mod.TObject;',
  17435. '$mod.b = $mod.TObject !== $mod.C;',
  17436. '']));
  17437. end;
  17438. procedure TTestModule.TestClassOf_ClassVar;
  17439. begin
  17440. StartProgram(false);
  17441. Add('type');
  17442. Add(' TObject = class');
  17443. Add(' class var id: longint;');
  17444. Add(' end;');
  17445. Add(' TClass = class of TObject;');
  17446. Add('var');
  17447. Add(' C: tclass;');
  17448. Add('begin');
  17449. Add(' C.id:=C.id;');
  17450. ConvertProgram;
  17451. CheckSource('TestClassOf_ClassVar',
  17452. LinesToStr([ // statements
  17453. 'rtl.createClass(this, "TObject", null, function () {',
  17454. ' this.id = 0;',
  17455. ' this.$init = function () {',
  17456. ' };',
  17457. ' this.$final = function () {',
  17458. ' };',
  17459. '});',
  17460. 'this.C = null;'
  17461. ]),
  17462. LinesToStr([ // $mod.$main
  17463. '$mod.TObject.id = $mod.C.id;',
  17464. '']));
  17465. end;
  17466. procedure TTestModule.TestClassOf_ClassMethod;
  17467. begin
  17468. StartProgram(false);
  17469. Add('type');
  17470. Add(' TObject = class');
  17471. Add(' class function DoIt(i: longint = 0): longint;');
  17472. Add(' end;');
  17473. Add(' TClass = class of TObject;');
  17474. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17475. Add('var');
  17476. Add(' i: longint;');
  17477. Add(' C: tclass;');
  17478. Add('begin');
  17479. Add(' C.DoIt;');
  17480. Add(' C.DoIt();');
  17481. Add(' i:=C.DoIt;');
  17482. Add(' i:=C.DoIt();');
  17483. ConvertProgram;
  17484. CheckSource('TestClassOf_ClassMethod',
  17485. LinesToStr([ // statements
  17486. 'rtl.createClass(this, "TObject", null, function () {',
  17487. ' this.$init = function () {',
  17488. ' };',
  17489. ' this.$final = function () {',
  17490. ' };',
  17491. ' this.DoIt = function (i) {',
  17492. ' var Result = 0;',
  17493. ' return Result;',
  17494. ' };',
  17495. '});',
  17496. 'this.i = 0;',
  17497. 'this.C = null;'
  17498. ]),
  17499. LinesToStr([ // $mod.$main
  17500. '$mod.C.DoIt(0);',
  17501. '$mod.C.DoIt(0);',
  17502. '$mod.i = $mod.C.DoIt(0);',
  17503. '$mod.i = $mod.C.DoIt(0);',
  17504. '']));
  17505. end;
  17506. procedure TTestModule.TestClassOf_ClassProperty;
  17507. begin
  17508. StartProgram(false);
  17509. Add([
  17510. 'type',
  17511. ' TObject = class',
  17512. ' class var FA: longint;',
  17513. ' class function GetA: longint;',
  17514. ' class procedure SetA(Value: longint);',
  17515. ' class property pA: longint read fa write fa;',
  17516. ' class property pB: longint read geta write seta;',
  17517. ' end;',
  17518. ' TObjectClass = class of tobject;',
  17519. 'class function tobject.geta: longint; begin end;',
  17520. 'class procedure tobject.seta(value: longint); begin end;',
  17521. 'var',
  17522. ' b: boolean;',
  17523. ' Obj: tobject;',
  17524. ' Cla: tobjectclass;',
  17525. 'begin',
  17526. ' obj.pa:=obj.pa;',
  17527. ' obj.pb:=obj.pb;',
  17528. ' b:=obj.pa=4;',
  17529. ' b:=obj.pb=obj.pb;',
  17530. ' b:=5=obj.pa;',
  17531. ' cla.pa:=6;',
  17532. ' cla.pa:=cla.pa;',
  17533. ' cla.pb:=cla.pb;',
  17534. ' b:=cla.pa=7;',
  17535. ' b:=cla.pb=cla.pb;',
  17536. ' b:=8=cla.pa;',
  17537. ' tobject.pa:=9;',
  17538. ' tobject.pb:=tobject.pb;',
  17539. ' b:=tobject.pa=10;',
  17540. ' b:=11=tobject.pa;',
  17541. '']);
  17542. ConvertProgram;
  17543. CheckSource('TestClassOf_ClassProperty',
  17544. LinesToStr([ // statements
  17545. 'rtl.createClass(this, "TObject", null, function () {',
  17546. ' this.FA = 0;',
  17547. ' this.$init = function () {',
  17548. ' };',
  17549. ' this.$final = function () {',
  17550. ' };',
  17551. ' this.GetA = function () {',
  17552. ' var Result = 0;',
  17553. ' return Result;',
  17554. ' };',
  17555. ' this.SetA = function (Value) {',
  17556. ' };',
  17557. '});',
  17558. 'this.b = false;',
  17559. 'this.Obj = null;',
  17560. 'this.Cla = null;'
  17561. ]),
  17562. LinesToStr([ // $mod.$main
  17563. '$mod.TObject.FA = $mod.Obj.FA;',
  17564. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17565. '$mod.b = $mod.Obj.FA === 4;',
  17566. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17567. '$mod.b = 5 === $mod.Obj.FA;',
  17568. '$mod.TObject.FA = 6;',
  17569. '$mod.TObject.FA = $mod.Cla.FA;',
  17570. '$mod.Cla.SetA($mod.Cla.GetA());',
  17571. '$mod.b = $mod.Cla.FA === 7;',
  17572. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17573. '$mod.b = 8 === $mod.Cla.FA;',
  17574. '$mod.TObject.FA = 9;',
  17575. '$mod.TObject.SetA($mod.TObject.GetA());',
  17576. '$mod.b = $mod.TObject.FA === 10;',
  17577. '$mod.b = 11 === $mod.TObject.FA;',
  17578. '']));
  17579. end;
  17580. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17581. begin
  17582. StartProgram(false);
  17583. Add('type');
  17584. Add(' TObject = class');
  17585. Add(' class var GlobalId: longint;');
  17586. Add(' class procedure ProcA;');
  17587. Add(' end;');
  17588. Add('class procedure tobject.proca;');
  17589. Add('var b: boolean;');
  17590. Add('begin');
  17591. Add(' b:=self=nil;');
  17592. Add(' b:=self.globalid=3;');
  17593. Add(' b:=4=self.globalid;');
  17594. Add(' self.globalid:=5;');
  17595. Add(' self.proca;');
  17596. Add('end;');
  17597. Add('begin');
  17598. ConvertProgram;
  17599. CheckSource('TestClassOf_ClassMethodSelf',
  17600. LinesToStr([ // statements
  17601. 'rtl.createClass(this, "TObject", null, function () {',
  17602. ' this.GlobalId = 0;',
  17603. ' this.$init = function () {',
  17604. ' };',
  17605. ' this.$final = function () {',
  17606. ' };',
  17607. ' this.ProcA = function () {',
  17608. ' var b = false;',
  17609. ' b = this === null;',
  17610. ' b = this.GlobalId === 3;',
  17611. ' b = 4 === this.GlobalId;',
  17612. ' $mod.TObject.GlobalId = 5;',
  17613. ' this.ProcA();',
  17614. ' };',
  17615. '});'
  17616. ]),
  17617. LinesToStr([ // $mod.$main
  17618. '']));
  17619. end;
  17620. procedure TTestModule.TestClassOf_TypeCast;
  17621. begin
  17622. StartProgram(false);
  17623. Add('type');
  17624. Add(' TObject = class');
  17625. Add(' class procedure {#TObject_DoIt}DoIt;');
  17626. Add(' end;');
  17627. Add(' TClass = class of TObject;');
  17628. Add(' TMobile = class');
  17629. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17630. Add(' end;');
  17631. Add(' TMobileClass = class of TMobile;');
  17632. Add(' TCar = class(TMobile)');
  17633. Add(' class procedure {#TCar_DoIt}DoIt;');
  17634. Add(' end;');
  17635. Add(' TCarClass = class of TCar;');
  17636. Add('class procedure TObject.DoIt;');
  17637. Add('begin');
  17638. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17639. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17640. Add('end;');
  17641. Add('class procedure TMobile.DoIt;');
  17642. Add('begin');
  17643. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17644. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17645. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17646. Add('end;');
  17647. Add('class procedure TCar.DoIt; begin end;');
  17648. Add('var');
  17649. Add(' ObjC: TClass;');
  17650. Add(' MobileC: TMobileClass;');
  17651. Add(' CarC: TCarClass;');
  17652. Add('begin');
  17653. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17654. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17655. Add(' CarC.{@TCar_DoIt}DoIt;');
  17656. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17657. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17658. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17659. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17660. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17661. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17662. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17663. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17664. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17665. ConvertProgram;
  17666. CheckSource('TestClassOf_TypeCast',
  17667. LinesToStr([ // statements
  17668. 'rtl.createClass(this, "TObject", null, function () {',
  17669. ' this.$init = function () {',
  17670. ' };',
  17671. ' this.$final = function () {',
  17672. ' };',
  17673. ' this.DoIt = function () {',
  17674. ' this.DoIt();',
  17675. ' this.DoIt$1();',
  17676. ' };',
  17677. '});',
  17678. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17679. ' this.DoIt$1 = function () {',
  17680. ' this.DoIt();',
  17681. ' this.DoIt$1();',
  17682. ' this.DoIt$2();',
  17683. ' };',
  17684. '});',
  17685. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17686. ' this.DoIt$2 = function () {',
  17687. ' };',
  17688. '});',
  17689. 'this.ObjC = null;',
  17690. 'this.MobileC = null;',
  17691. 'this.CarC = null;',
  17692. '']),
  17693. LinesToStr([ // $mod.$main
  17694. '$mod.ObjC.DoIt();',
  17695. '$mod.MobileC.DoIt$1();',
  17696. '$mod.CarC.DoIt$2();',
  17697. '$mod.ObjC.DoIt();',
  17698. '$mod.ObjC.DoIt$1();',
  17699. '$mod.ObjC.DoIt$2();',
  17700. '$mod.MobileC.DoIt();',
  17701. '$mod.MobileC.DoIt$1();',
  17702. '$mod.MobileC.DoIt$2();',
  17703. '$mod.CarC.DoIt();',
  17704. '$mod.CarC.DoIt$1();',
  17705. '$mod.CarC.DoIt$2();',
  17706. '']));
  17707. end;
  17708. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17709. begin
  17710. StartProgram(false);
  17711. Add('type');
  17712. Add(' TObject = class');
  17713. Add(' function CurNow: longint; ');
  17714. Add(' class function Now: longint; ');
  17715. Add(' end;');
  17716. Add('function TObject.CurNow: longint; begin end;');
  17717. Add('class function TObject.Now: longint; begin end;');
  17718. Add('var');
  17719. Add(' Obj: tobject;');
  17720. Add(' vI: longint;');
  17721. Add('begin');
  17722. Add(' obj.curnow;');
  17723. Add(' vi:=obj.curnow;');
  17724. Add(' tobject.now;');
  17725. Add(' vi:=tobject.now;');
  17726. ConvertProgram;
  17727. CheckSource('TestClassOf_ImplicitFunctionCall',
  17728. LinesToStr([ // statements
  17729. 'rtl.createClass(this, "TObject", null, function () {',
  17730. ' this.$init = function () {',
  17731. ' };',
  17732. ' this.$final = function () {',
  17733. ' };',
  17734. ' this.CurNow = function () {',
  17735. ' var Result = 0;',
  17736. ' return Result;',
  17737. ' };',
  17738. ' this.Now = function () {',
  17739. ' var Result = 0;',
  17740. ' return Result;',
  17741. ' };',
  17742. '});',
  17743. 'this.Obj = null;',
  17744. 'this.vI = 0;',
  17745. '']),
  17746. LinesToStr([ // $mod.$main
  17747. '$mod.Obj.CurNow();',
  17748. '$mod.vI = $mod.Obj.CurNow();',
  17749. '$mod.TObject.Now();',
  17750. '$mod.vI = $mod.TObject.Now();',
  17751. '']));
  17752. end;
  17753. procedure TTestModule.TestClassOf_Const;
  17754. begin
  17755. StartProgram(false);
  17756. Add([
  17757. 'type',
  17758. ' TObject = class',
  17759. ' end;',
  17760. ' TBird = TObject;',
  17761. ' TBirds = class of TBird;',
  17762. ' TEagles = TBirds;',
  17763. ' THawk = class(TBird);',
  17764. 'const',
  17765. ' Hawk: TEagles = THawk;',
  17766. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17767. ' TBird,',
  17768. ' THawk',
  17769. ' );',
  17770. 'begin']);
  17771. ConvertProgram;
  17772. CheckSource('TestClassOf_Const',
  17773. LinesToStr([ // statements
  17774. 'rtl.createClass(this, "TObject", null, function () {',
  17775. ' this.$init = function () {',
  17776. ' };',
  17777. ' this.$final = function () {',
  17778. ' };',
  17779. '});',
  17780. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17781. '});',
  17782. 'this.Hawk = this.THawk;',
  17783. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17784. '']),
  17785. LinesToStr([ // $mod.$main
  17786. '']));
  17787. end;
  17788. procedure TTestModule.TestNestedClass_Alias;
  17789. begin
  17790. WithTypeInfo:=true;
  17791. StartProgram(false);
  17792. Add([
  17793. 'type',
  17794. ' TObject = class',
  17795. ' type TNested = type longint;',
  17796. ' end;',
  17797. 'type TAlias = type tobject.tnested;',
  17798. 'var i: tobject.tnested = 3;',
  17799. 'var j: TAlias = 4;',
  17800. 'begin',
  17801. ' if typeinfo(TAlias)=nil then ;',
  17802. ' if typeinfo(tobject.tnested)=nil then ;',
  17803. '']);
  17804. ConvertProgram;
  17805. CheckSource('TestNestedClass_Alias',
  17806. LinesToStr([ // statements
  17807. 'rtl.createClass(this, "TObject", null, function () {',
  17808. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17809. ' this.$init = function () {',
  17810. ' };',
  17811. ' this.$final = function () {',
  17812. ' };',
  17813. '});',
  17814. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17815. 'this.i = 3;',
  17816. 'this.j = 4;',
  17817. '']),
  17818. LinesToStr([ // $mod.$main
  17819. 'if ($mod.$rtti["TAlias"] === null) ;',
  17820. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17821. '']));
  17822. end;
  17823. procedure TTestModule.TestNestedClass_Record;
  17824. begin
  17825. WithTypeInfo:=true;
  17826. StartProgram(false);
  17827. Add([
  17828. 'type',
  17829. ' TObject = class',
  17830. ' type TPoint = record',
  17831. ' x,y: byte;',
  17832. ' end;',
  17833. ' procedure DoIt(t: TPoint);',
  17834. ' end;',
  17835. 'procedure tobject.DoIt(t: TPoint);',
  17836. 'var p: TPoint;',
  17837. 'begin',
  17838. ' t.x:=t.y;',
  17839. ' p:=t;',
  17840. 'end;',
  17841. 'var',
  17842. ' p: tobject.tpoint = (x:2; y:4);',
  17843. ' o: TObject;',
  17844. 'begin',
  17845. ' p:=p;',
  17846. ' o.doit(p);',
  17847. '']);
  17848. ConvertProgram;
  17849. CheckSource('TestNestedClass_Record',
  17850. LinesToStr([ // statements
  17851. 'rtl.createClass(this, "TObject", null, function () {',
  17852. ' rtl.recNewT(this, "TPoint", function () {',
  17853. ' this.x = 0;',
  17854. ' this.y = 0;',
  17855. ' this.$eq = function (b) {',
  17856. ' return (this.x === b.x) && (this.y === b.y);',
  17857. ' };',
  17858. ' this.$assign = function (s) {',
  17859. ' this.x = s.x;',
  17860. ' this.y = s.y;',
  17861. ' return this;',
  17862. ' };',
  17863. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17864. ' $r.addField("x", rtl.byte);',
  17865. ' $r.addField("y", rtl.byte);',
  17866. ' });',
  17867. ' this.$init = function () {',
  17868. ' };',
  17869. ' this.$final = function () {',
  17870. ' };',
  17871. ' this.DoIt = function (t) {',
  17872. ' var p = this.TPoint.$new();',
  17873. ' t.x = t.y;',
  17874. ' p.$assign(t);',
  17875. ' };',
  17876. '});',
  17877. 'this.p = this.TObject.TPoint.$clone({',
  17878. ' x: 2,',
  17879. ' y: 4',
  17880. '});',
  17881. 'this.o = null;',
  17882. '']),
  17883. LinesToStr([ // $mod.$main
  17884. '$mod.p.$assign($mod.p);',
  17885. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17886. '']));
  17887. end;
  17888. procedure TTestModule.TestNestedClass_Class;
  17889. begin
  17890. StartProgram(false);
  17891. Add([
  17892. 'type',
  17893. ' TObject = class end;',
  17894. ' TBird = class',
  17895. ' type TLeg = class',
  17896. ' FId: longint;',
  17897. ' constructor Create;',
  17898. ' function Create(i: longint): TLeg;',
  17899. ' end;',
  17900. ' function DoIt(b: TBird): Tleg;',
  17901. ' end;',
  17902. 'constructor tbird.tleg.create;',
  17903. 'begin',
  17904. ' FId:=3;',
  17905. 'end;',
  17906. 'function tbird.tleg.Create(i: longint): TLeg;',
  17907. 'begin',
  17908. ' Create;',
  17909. ' Result:=TLeg.Create;',
  17910. ' Result:=TBird.TLeg.Create;',
  17911. ' Result:=Create(3);',
  17912. ' FId:=i;',
  17913. 'end;',
  17914. 'function tbird.DoIt(b: tbird): tleg;',
  17915. 'begin',
  17916. ' Result.Create;',
  17917. ' Result:=TLeg.Create;',
  17918. ' Result:=TBird.TLeg.Create;',
  17919. ' Result:=Result.Create(3);',
  17920. 'end;',
  17921. 'var',
  17922. ' b: Tbird.tleg;',
  17923. 'begin',
  17924. ' b.Create;',
  17925. ' b:=TBird.TLeg.Create;',
  17926. ' b:=b.Create(3);',
  17927. '']);
  17928. ConvertProgram;
  17929. CheckSource('TestNestedClass_Class',
  17930. LinesToStr([ // statements
  17931. 'rtl.createClass(this, "TObject", null, function () {',
  17932. ' this.$init = function () {',
  17933. ' };',
  17934. ' this.$final = function () {',
  17935. ' };',
  17936. '});',
  17937. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17938. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  17939. ' this.$init = function () {',
  17940. ' $mod.TObject.$init.call(this);',
  17941. ' this.FId = 0;',
  17942. ' };',
  17943. ' this.Create = function () {',
  17944. ' this.FId = 3;',
  17945. ' return this;',
  17946. ' };',
  17947. ' this.Create$1 = function (i) {',
  17948. ' var Result = null;',
  17949. ' this.Create();',
  17950. ' Result = $mod.TBird.TLeg.$create("Create");',
  17951. ' Result = $mod.TBird.TLeg.$create("Create");',
  17952. ' Result = this.Create$1(3);',
  17953. ' this.FId = i;',
  17954. ' return Result;',
  17955. ' };',
  17956. ' }, "TBird.TLeg");',
  17957. ' this.DoIt = function (b) {',
  17958. ' var Result = null;',
  17959. ' Result.Create();',
  17960. ' Result = this.TLeg.$create("Create");',
  17961. ' Result = $mod.TBird.TLeg.$create("Create");',
  17962. ' Result = Result.Create$1(3);',
  17963. ' return Result;',
  17964. ' };',
  17965. '});',
  17966. 'this.b = null;',
  17967. '']),
  17968. LinesToStr([ // $mod.$main
  17969. '$mod.b.Create();',
  17970. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  17971. '$mod.b = $mod.b.Create$1(3);',
  17972. '']));
  17973. end;
  17974. procedure TTestModule.TestNestedClass_CallInherited;
  17975. begin
  17976. StartProgram(false);
  17977. Add([
  17978. 'type',
  17979. ' TObject = class end;',
  17980. ' TBird = class',
  17981. ' type',
  17982. ' TWing = class',
  17983. ' function Fly(w: word = 17): word; virtual;',
  17984. ' end;',
  17985. ' end;',
  17986. ' TEagle = class(TBird)',
  17987. ' type',
  17988. ' TEagleWing = class(TWing)',
  17989. ' function Fly(w: word): word; override;',
  17990. ' end;',
  17991. ' end;',
  17992. 'function TBird.TWing.Fly(w: word): word;',
  17993. 'begin',
  17994. 'end;',
  17995. 'function TEagle.TEagleWing.Fly(w: word): word;',
  17996. 'begin',
  17997. ' inherited;',
  17998. ' inherited Fly;',
  17999. ' inherited Fly(3);',
  18000. ' Result:=inherited Fly;',
  18001. ' Result:=inherited Fly(4);',
  18002. 'end;',
  18003. 'begin',
  18004. '']);
  18005. ConvertProgram;
  18006. CheckSource('TestNestedClass_CallInherited',
  18007. LinesToStr([ // statements
  18008. 'rtl.createClass(this, "TObject", null, function () {',
  18009. ' this.$init = function () {',
  18010. ' };',
  18011. ' this.$final = function () {',
  18012. ' };',
  18013. '});',
  18014. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18015. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18016. ' this.Fly = function (w) {',
  18017. ' var Result = 0;',
  18018. ' return Result;',
  18019. ' };',
  18020. ' }, "TBird.TWing");',
  18021. '});',
  18022. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18023. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18024. ' this.Fly = function (w) {',
  18025. ' var Result = 0;',
  18026. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18027. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18028. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18029. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18030. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18031. ' return Result;',
  18032. ' };',
  18033. ' }, "TEagle.TEagleWing");',
  18034. '});',
  18035. '']),
  18036. LinesToStr([ // $mod.$main
  18037. '']));
  18038. end;
  18039. procedure TTestModule.TestExternalClass_Var;
  18040. begin
  18041. StartProgram(false);
  18042. Add([
  18043. '{$modeswitch externalclass}',
  18044. 'type',
  18045. ' TExtA = class external name ''ExtObj''',
  18046. ' Id: longint external name ''$Id'';',
  18047. ' B: longint;',
  18048. ' end;',
  18049. 'var Obj: TExtA;',
  18050. 'begin',
  18051. ' obj.id:=obj.id+1;',
  18052. ' obj.B:=obj.B+1;']);
  18053. ConvertProgram;
  18054. CheckSource('TestExternalClass_Var',
  18055. LinesToStr([ // statements
  18056. 'this.Obj = null;',
  18057. '']),
  18058. LinesToStr([ // $mod.$main
  18059. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18060. '$mod.Obj.B = $mod.Obj.B + 1;',
  18061. '']));
  18062. end;
  18063. procedure TTestModule.TestExternalClass_Const;
  18064. begin
  18065. StartProgram(false);
  18066. Add([
  18067. '{$modeswitch externalclass}',
  18068. 'type',
  18069. ' TExtA = class external name ''ExtObj''',
  18070. ' const Two: longint = 2;',
  18071. ' const Three = 3;',
  18072. ' const Id: longint;',
  18073. ' end;',
  18074. ' TExtB = class external name ''ExtB''',
  18075. ' A: TExtA;',
  18076. ' end;',
  18077. 'var',
  18078. ' A: texta;',
  18079. ' B: textb;',
  18080. ' i: longint;',
  18081. 'begin',
  18082. ' i:=a.two;',
  18083. ' i:=texta.two;',
  18084. ' i:=a.three;',
  18085. ' i:=texta.three;',
  18086. ' i:=a.id;',
  18087. ' i:=texta.id;',
  18088. '']);
  18089. ConvertProgram;
  18090. CheckSource('TestExternalClass_Const',
  18091. LinesToStr([ // statements
  18092. 'this.A = null;',
  18093. 'this.B = null;',
  18094. 'this.i = 0;',
  18095. '']),
  18096. LinesToStr([ // $mod.$main
  18097. '$mod.i = 2;',
  18098. '$mod.i = 2;',
  18099. '$mod.i = 3;',
  18100. '$mod.i = 3;',
  18101. '$mod.i = $mod.A.Id;',
  18102. '$mod.i = ExtObj.Id;',
  18103. '']));
  18104. end;
  18105. procedure TTestModule.TestExternalClass_Dollar;
  18106. begin
  18107. StartProgram(false);
  18108. Add([
  18109. '{$modeswitch externalclass}',
  18110. 'type',
  18111. ' TExtA = class external name ''$''',
  18112. ' Id: longint external name ''$'';',
  18113. ' function Bla(i: longint): longint; external name ''$'';',
  18114. ' end;',
  18115. 'function dollar(k: longint): longint; external name ''$'';',
  18116. 'var Obj: TExtA;',
  18117. 'begin',
  18118. ' dollar(1);',
  18119. ' obj.id:=obj.id+2;',
  18120. ' obj.Bla(3);',
  18121. '']);
  18122. ConvertProgram;
  18123. CheckSource('TestExternalClass_Dollar',
  18124. LinesToStr([ // statements
  18125. 'this.Obj = null;',
  18126. '']),
  18127. LinesToStr([ // $mod.$main
  18128. '$(1);',
  18129. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18130. '$mod.Obj.$(3);',
  18131. '']));
  18132. end;
  18133. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18134. begin
  18135. StartProgram(false);
  18136. Add('{$modeswitch externalclass}');
  18137. Add('type');
  18138. Add(' TExtA = class external name ''ExtA''');
  18139. Add(' Id: longint external name ''$Id'';');
  18140. Add(' end;');
  18141. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18142. Add(' Id: longint;');
  18143. Add(' end;');
  18144. Add('begin');
  18145. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18146. ConvertProgram;
  18147. end;
  18148. procedure TTestModule.TestExternalClass_Method;
  18149. begin
  18150. StartProgram(false);
  18151. Add(['{$modeswitch externalclass}',
  18152. 'type',
  18153. ' TExtA = class external name ''ExtObj''',
  18154. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18155. ' procedure DoSome(Id: longint = 1);',
  18156. ' end;',
  18157. 'var Obj: texta;',
  18158. 'begin',
  18159. ' obj.doit;',
  18160. ' obj.doit();',
  18161. ' obj.doit(2);',
  18162. ' with obj do begin',
  18163. ' doit;',
  18164. ' doit();',
  18165. ' doit(3);',
  18166. ' end;']);
  18167. ConvertProgram;
  18168. CheckSource('TestExternalClass_Method',
  18169. LinesToStr([ // statements
  18170. 'this.Obj = null;',
  18171. '']),
  18172. LinesToStr([ // $mod.$main
  18173. '$mod.Obj.$Execute(1);',
  18174. '$mod.Obj.$Execute(1);',
  18175. '$mod.Obj.$Execute(2);',
  18176. 'var $with = $mod.Obj;',
  18177. '$with.$Execute(1);',
  18178. '$with.$Execute(1);',
  18179. '$with.$Execute(3);',
  18180. '']));
  18181. end;
  18182. procedure TTestModule.TestExternalClass_ClassMethod;
  18183. begin
  18184. StartProgram(false);
  18185. Add([
  18186. '{$modeswitch externalclass}',
  18187. 'type',
  18188. ' TExtA = class external name ''ExtObj''',
  18189. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18190. ' end;',
  18191. ' TExtB = TExtA;',
  18192. 'var p: Pointer;',
  18193. 'begin',
  18194. ' texta.doit;',
  18195. ' texta.doit();',
  18196. ' texta.doit(2);',
  18197. ' p:[email protected];',
  18198. ' with texta do begin',
  18199. ' doit;',
  18200. ' doit();',
  18201. ' doit(3);',
  18202. ' p:=@DoIt;',
  18203. ' end;',
  18204. ' textb.doit;',
  18205. ' textb.doit();',
  18206. ' textb.doit(4);',
  18207. ' with textb do begin',
  18208. ' doit;',
  18209. ' doit();',
  18210. ' doit(5);',
  18211. ' end;',
  18212. '']);
  18213. ConvertProgram;
  18214. CheckSource('TestExternalClass_ClassMethod',
  18215. LinesToStr([ // statements
  18216. 'this.p = null;',
  18217. '']),
  18218. LinesToStr([ // $mod.$main
  18219. 'ExtObj.$Execute(1);',
  18220. 'ExtObj.$Execute(1);',
  18221. 'ExtObj.$Execute(2);',
  18222. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18223. 'ExtObj.$Execute(1);',
  18224. 'ExtObj.$Execute(1);',
  18225. 'ExtObj.$Execute(3);',
  18226. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18227. 'ExtObj.$Execute(1);',
  18228. 'ExtObj.$Execute(1);',
  18229. 'ExtObj.$Execute(4);',
  18230. 'ExtObj.$Execute(1);',
  18231. 'ExtObj.$Execute(1);',
  18232. 'ExtObj.$Execute(5);',
  18233. '']));
  18234. end;
  18235. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18236. begin
  18237. StartProgram(false);
  18238. Add([
  18239. '{$modeswitch externalclass}',
  18240. 'type',
  18241. ' TExtA = class external name ''ExtObj''',
  18242. ' class procedure DoIt(Id: longint = 1); static;',
  18243. ' end;',
  18244. 'var p: Pointer;',
  18245. 'begin',
  18246. ' texta.doit;',
  18247. ' texta.doit();',
  18248. ' texta.doit(2);',
  18249. ' p:[email protected];',
  18250. ' with texta do begin',
  18251. ' doit;',
  18252. ' doit();',
  18253. ' doit(3);',
  18254. ' p:=@DoIt;',
  18255. ' end;',
  18256. '']);
  18257. ConvertProgram;
  18258. CheckSource('TestExternalClass_ClassMethodStatic',
  18259. LinesToStr([ // statements
  18260. 'this.p = null;',
  18261. '']),
  18262. LinesToStr([ // $mod.$main
  18263. 'ExtObj.DoIt(1);',
  18264. 'ExtObj.DoIt(1);',
  18265. 'ExtObj.DoIt(2);',
  18266. '$mod.p = ExtObj.DoIt;',
  18267. 'ExtObj.DoIt(1);',
  18268. 'ExtObj.DoIt(1);',
  18269. 'ExtObj.DoIt(3);',
  18270. '$mod.p = ExtObj.DoIt;',
  18271. '']));
  18272. end;
  18273. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18274. begin
  18275. StartProgram(false);
  18276. Add([
  18277. '{$modeswitch externalclass}',
  18278. 'type',
  18279. ' TBird = class external name ''Array''',
  18280. ' end;',
  18281. 'function GetPtr: Pointer;',
  18282. 'begin',
  18283. 'end;',
  18284. 'procedure Write(const p);',
  18285. 'begin',
  18286. 'end;',
  18287. 'procedure WriteLn; varargs;',
  18288. 'begin',
  18289. 'end;',
  18290. 'begin',
  18291. ' if TBird(GetPtr)=nil then ;',
  18292. ' Write(GetPtr);',
  18293. ' WriteLn(GetPtr);',
  18294. ' Write(TBird(GetPtr));',
  18295. ' WriteLn(TBird(GetPtr));',
  18296. '']);
  18297. ConvertProgram;
  18298. CheckSource('TestFunctionResultInTypeCast',
  18299. LinesToStr([ // statements
  18300. 'this.GetPtr = function () {',
  18301. ' var Result = null;',
  18302. ' return Result;',
  18303. '};',
  18304. 'this.Write = function (p) {',
  18305. '};',
  18306. 'this.WriteLn = function () {',
  18307. '};',
  18308. '']),
  18309. LinesToStr([
  18310. 'if ($mod.GetPtr() === null) ;',
  18311. '$mod.Write($mod.GetPtr());',
  18312. '$mod.WriteLn($mod.GetPtr());',
  18313. '$mod.Write($mod.GetPtr());',
  18314. '$mod.WriteLn($mod.GetPtr());',
  18315. '']));
  18316. end;
  18317. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18318. begin
  18319. StartProgram(false);
  18320. Add([
  18321. '{$modeswitch externalclass}',
  18322. 'type',
  18323. ' TExtA = class external name ''ExtObjA''',
  18324. ' procedure ProcA; virtual;',
  18325. ' procedure ProcB; virtual;',
  18326. ' end;',
  18327. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18328. ' end;',
  18329. ' TExtC = class (TExtB)',
  18330. ' procedure ProcA; override;',
  18331. ' end;',
  18332. 'procedure TExtC.ProcA;',
  18333. 'begin',
  18334. ' ProcA;',
  18335. ' Self.ProcA;',
  18336. ' ProcB;',
  18337. ' Self.ProcB;',
  18338. 'end;',
  18339. 'var',
  18340. ' A: texta;',
  18341. ' B: textb;',
  18342. ' C: textc;',
  18343. 'begin',
  18344. ' a.proca;',
  18345. ' b.proca;',
  18346. ' c.proca;']);
  18347. ConvertProgram;
  18348. CheckSource('TestExternalClass_NonExternalOverride',
  18349. LinesToStr([ // statements
  18350. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18351. ' this.$init = function () {',
  18352. ' };',
  18353. ' this.$final = function () {',
  18354. ' };',
  18355. ' this.ProcA = function () {',
  18356. ' this.ProcA();',
  18357. ' this.ProcA();',
  18358. ' this.ProcB();',
  18359. ' this.ProcB();',
  18360. ' };',
  18361. '});',
  18362. 'this.A = null;',
  18363. 'this.B = null;',
  18364. 'this.C = null;',
  18365. '']),
  18366. LinesToStr([ // $mod.$main
  18367. '$mod.A.ProcA();',
  18368. '$mod.B.ProcA();',
  18369. '$mod.C.ProcA();',
  18370. '']));
  18371. end;
  18372. procedure TTestModule.TestExternalClass_OverloadHint;
  18373. begin
  18374. StartProgram(false);
  18375. Add([
  18376. '{$modeswitch externalclass}',
  18377. 'type',
  18378. ' TExtA = class external name ''ExtObjA''',
  18379. ' procedure DoIt;',
  18380. ' procedure DoIt(i: longint);',
  18381. ' end;',
  18382. 'begin',
  18383. '']);
  18384. ConvertProgram;
  18385. CheckResolverUnexpectedHints(true);
  18386. CheckSource('TestExternalClass_OverloadHint',
  18387. LinesToStr([ // statements
  18388. '']),
  18389. LinesToStr([ // $mod.$main
  18390. '']));
  18391. end;
  18392. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18393. begin
  18394. WithTypeInfo:=true;
  18395. StartProgram(false);
  18396. Add([
  18397. '{$modeswitch externalclass}',
  18398. 'type',
  18399. ' JSwiper = class external name ''Swiper''',
  18400. ' constructor New;',
  18401. ' end;',
  18402. ' TObject = class',
  18403. ' private',
  18404. ' FSwiper: JSwiper;',
  18405. ' published',
  18406. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18407. ' end;',
  18408. 'begin',
  18409. ' JSwiper.new;',
  18410. '']);
  18411. ConvertProgram;
  18412. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18413. LinesToStr([ // statements
  18414. 'this.$rtti.$ExtClass("JSwiper", {',
  18415. ' jsclass: "Swiper"',
  18416. '});',
  18417. 'rtl.createClass(this, "TObject", null, function () {',
  18418. ' this.$init = function () {',
  18419. ' this.FSwiper = null;',
  18420. ' };',
  18421. ' this.$final = function () {',
  18422. ' this.FSwiper = undefined;',
  18423. ' };',
  18424. ' var $r = this.$rtti;',
  18425. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18426. '});',
  18427. '']),
  18428. LinesToStr([ // $mod.$main
  18429. 'new Swiper();',
  18430. '']));
  18431. end;
  18432. procedure TTestModule.TestExternalClass_Property;
  18433. begin
  18434. StartProgram(false);
  18435. Add([
  18436. '{$modeswitch externalclass}',
  18437. 'type',
  18438. ' TExtA = class external name ''ExtA''',
  18439. ' function getYear: longint;',
  18440. ' procedure setYear(Value: longint);',
  18441. ' property Year: longint read getyear write setyear;',
  18442. ' end;',
  18443. ' TExtB = class (TExtA)',
  18444. ' procedure OtherSetYear(Value: longint);',
  18445. ' property year write othersetyear;',
  18446. ' end;',
  18447. 'procedure textb.othersetyear(value: longint);',
  18448. 'begin',
  18449. ' setYear(Value+4);',
  18450. 'end;',
  18451. 'var',
  18452. ' A: texta;',
  18453. ' B: textb;',
  18454. 'begin',
  18455. ' a.year:=a.year+1;',
  18456. ' b.year:=b.year+2;']);
  18457. ConvertProgram;
  18458. CheckSource('TestExternalClass_NonExternalOverride',
  18459. LinesToStr([ // statements
  18460. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18461. ' this.$init = function () {',
  18462. ' };',
  18463. ' this.$final = function () {',
  18464. ' };',
  18465. ' this.OtherSetYear = function (Value) {',
  18466. ' this.setYear(Value+4);',
  18467. ' };',
  18468. '});',
  18469. 'this.A = null;',
  18470. 'this.B = null;',
  18471. '']),
  18472. LinesToStr([ // $mod.$main
  18473. '$mod.A.setYear($mod.A.getYear()+1);',
  18474. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18475. '']));
  18476. end;
  18477. procedure TTestModule.TestExternalClass_PropertyDate;
  18478. begin
  18479. StartProgram(false);
  18480. Add([
  18481. '{$modeswitch externalclass}',
  18482. 'type',
  18483. ' TExtA = class external name ''ExtA''',
  18484. ' end;',
  18485. ' TExtB = class (TExtA)',
  18486. ' FDate: string;',
  18487. ' property Date: string read FDate write FDate;',
  18488. ' property ExtA: string read FDate write FDate;',
  18489. ' end;',
  18490. ' {$M+}',
  18491. ' TObject = class',
  18492. ' FDate: string;',
  18493. ' published',
  18494. ' property Date: string read FDate write FDate;',
  18495. ' property ExtA: string read FDate write FDate;',
  18496. ' end;',
  18497. 'var',
  18498. ' B: textb;',
  18499. ' o: TObject;',
  18500. 'begin',
  18501. ' b.date:=b.exta;',
  18502. ' o.date:=o.exta;']);
  18503. ConvertProgram;
  18504. CheckSource('TestExternalClass_PropertyDate',
  18505. LinesToStr([ // statements
  18506. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18507. ' this.$init = function () {',
  18508. ' this.FDate = "";',
  18509. ' };',
  18510. ' this.$final = function () {',
  18511. ' };',
  18512. '});',
  18513. 'rtl.createClass(this, "TObject", null, function () {',
  18514. ' this.$init = function () {',
  18515. ' this.FDate = "";',
  18516. ' };',
  18517. ' this.$final = function () {',
  18518. ' };',
  18519. ' var $r = this.$rtti;',
  18520. ' $r.addField("FDate", rtl.string);',
  18521. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18522. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18523. '});',
  18524. 'this.B = null;',
  18525. 'this.o = null;',
  18526. '']),
  18527. LinesToStr([ // $mod.$main
  18528. '$mod.B.FDate = $mod.B.FDate;',
  18529. '$mod.o.FDate = $mod.o.FDate;',
  18530. '']));
  18531. end;
  18532. procedure TTestModule.TestExternalClass_ClassProperty;
  18533. begin
  18534. StartProgram(false);
  18535. Add('{$modeswitch externalclass}');
  18536. Add('type');
  18537. Add(' TExtA = class external name ''ExtA''');
  18538. Add(' class function getYear: longint;');
  18539. Add(' class procedure setYear(Value: longint);');
  18540. Add(' class property Year: longint read getyear write setyear;');
  18541. Add(' end;');
  18542. Add(' TExtB = class (TExtA)');
  18543. Add(' class function GetCentury: longint;');
  18544. Add(' class procedure SetCentury(Value: longint);');
  18545. Add(' class property Century: longint read getcentury write setcentury;');
  18546. Add(' end;');
  18547. Add('class function textb.getcentury: longint;');
  18548. Add('begin');
  18549. Add('end;');
  18550. Add('class procedure textb.setcentury(value: longint);');
  18551. Add('begin');
  18552. Add(' setyear(value+11);');
  18553. Add(' texta.year:=texta.year+12;');
  18554. Add(' year:=year+13;');
  18555. Add(' textb.century:=textb.century+14;');
  18556. Add(' century:=century+15;');
  18557. Add('end;');
  18558. Add('var');
  18559. Add(' A: texta;');
  18560. Add(' B: textb;');
  18561. Add('begin');
  18562. Add(' texta.year:=texta.year+1;');
  18563. Add(' textb.year:=textb.year+2;');
  18564. Add(' TextA.year:=TextA.year+3;');
  18565. Add(' b.year:=b.year+4;');
  18566. Add(' textb.century:=textb.century+5;');
  18567. Add(' b.century:=b.century+6;');
  18568. ConvertProgram;
  18569. CheckSource('TestExternalClass_ClassProperty',
  18570. LinesToStr([ // statements
  18571. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18572. ' this.$init = function () {',
  18573. ' };',
  18574. ' this.$final = function () {',
  18575. ' };',
  18576. ' this.GetCentury = function () {',
  18577. ' var Result = 0;',
  18578. ' return Result;',
  18579. ' };',
  18580. ' this.SetCentury = function (Value) {',
  18581. ' this.setYear(Value + 11);',
  18582. ' ExtA.setYear(ExtA.getYear() + 12);',
  18583. ' this.setYear(this.getYear() + 13);',
  18584. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18585. ' this.SetCentury(this.GetCentury() + 15);',
  18586. ' };',
  18587. '});',
  18588. 'this.A = null;',
  18589. 'this.B = null;',
  18590. '']),
  18591. LinesToStr([ // $mod.$main
  18592. 'ExtA.setYear(ExtA.getYear() + 1);',
  18593. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18594. 'ExtA.setYear(ExtA.getYear() + 3);',
  18595. '$mod.B.setYear($mod.B.getYear() + 4);',
  18596. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18597. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18598. '']));
  18599. end;
  18600. procedure TTestModule.TestExternalClass_ClassOf;
  18601. begin
  18602. StartProgram(false);
  18603. Add('{$modeswitch externalclass}');
  18604. Add('type');
  18605. Add(' TExtA = class external name ''ExtA''');
  18606. Add(' procedure ProcA; virtual;');
  18607. Add(' procedure ProcB; virtual;');
  18608. Add(' end;');
  18609. Add(' TExtAClass = class of TExtA;');
  18610. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18611. Add(' end;');
  18612. Add(' TExtBClass = class of TExtB;');
  18613. Add(' TExtC = class (TExtB)');
  18614. Add(' procedure ProcA; override;');
  18615. Add(' end;');
  18616. Add(' TExtCClass = class of TExtC;');
  18617. Add('procedure TExtC.ProcA; begin end;');
  18618. Add('var');
  18619. Add(' A: texta; ClA: TExtAClass;');
  18620. Add(' B: textb; ClB: TExtBClass;');
  18621. Add(' C: textc; ClC: TExtCClass;');
  18622. Add('begin');
  18623. Add(' ClA:=texta;');
  18624. Add(' ClA:=textb;');
  18625. Add(' ClA:=textc;');
  18626. Add(' ClB:=textb;');
  18627. Add(' ClB:=textc;');
  18628. Add(' ClC:=textc;');
  18629. ConvertProgram;
  18630. CheckSource('TestExternalClass_ClassOf',
  18631. LinesToStr([ // statements
  18632. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18633. ' this.$init = function () {',
  18634. ' };',
  18635. ' this.$final = function () {',
  18636. ' };',
  18637. ' this.ProcA = function () {',
  18638. ' };',
  18639. '});',
  18640. 'this.A = null;',
  18641. 'this.ClA = null;',
  18642. 'this.B = null;',
  18643. 'this.ClB = null;',
  18644. 'this.C = null;',
  18645. 'this.ClC = null;',
  18646. '']),
  18647. LinesToStr([ // $mod.$main
  18648. '$mod.ClA = ExtA;',
  18649. '$mod.ClA = ExtB;',
  18650. '$mod.ClA = $mod.TExtC;',
  18651. '$mod.ClB = ExtB;',
  18652. '$mod.ClB = $mod.TExtC;',
  18653. '$mod.ClC = $mod.TExtC;',
  18654. '']));
  18655. end;
  18656. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18657. begin
  18658. AddModuleWithIntfImplSrc('unit2.pas',
  18659. LinesToStr([
  18660. '{$modeswitch externalclass}',
  18661. 'type',
  18662. ' TExtA = class external name ''ExtA''',
  18663. ' class var Id: longint;',
  18664. ' end;',
  18665. '']),
  18666. '');
  18667. StartUnit(true);
  18668. Add('interface');
  18669. Add('uses unit2;');
  18670. Add('implementation');
  18671. Add('begin');
  18672. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18673. ConvertUnit;
  18674. CheckSource('TestExternalClass_ClassOtherUnit',
  18675. LinesToStr([
  18676. '']),
  18677. LinesToStr([
  18678. 'ExtA.Id = ExtA.Id + 1;',
  18679. '']));
  18680. end;
  18681. procedure TTestModule.TestExternalClass_Is;
  18682. begin
  18683. StartProgram(false);
  18684. Add([
  18685. '{$modeswitch externalclass}',
  18686. 'type',
  18687. ' TExtA = class external name ''ExtA''',
  18688. ' end;',
  18689. ' TExtAClass = class of TExtA;',
  18690. ' TExtB = class external name ''ExtB'' (TExtA)',
  18691. ' end;',
  18692. ' TExtBClass = class of TExtB;',
  18693. ' TExtC = class (TExtB)',
  18694. ' end;',
  18695. ' TExtCClass = class of TExtC;',
  18696. 'var',
  18697. ' A: texta; ClA: TExtAClass;',
  18698. ' B: textb; ClB: TExtBClass;',
  18699. ' C: textc; ClC: TExtCClass;',
  18700. 'begin',
  18701. ' if a is textb then ;',
  18702. ' if a is textc then ;',
  18703. ' if b is textc then ;',
  18704. ' if cla is textb then ;',
  18705. ' if cla is textc then ;',
  18706. ' if clb is textc then ;',
  18707. ' try',
  18708. ' except',
  18709. ' on TExtA do ;',
  18710. ' on e: TExtB do ;',
  18711. ' end;',
  18712. '']);
  18713. ConvertProgram;
  18714. CheckSource('TestExternalClass_Is',
  18715. LinesToStr([ // statements
  18716. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18717. ' this.$init = function () {',
  18718. ' };',
  18719. ' this.$final = function () {',
  18720. ' };',
  18721. '});',
  18722. 'this.A = null;',
  18723. 'this.ClA = null;',
  18724. 'this.B = null;',
  18725. 'this.ClB = null;',
  18726. 'this.C = null;',
  18727. 'this.ClC = null;',
  18728. '']),
  18729. LinesToStr([ // $mod.$main
  18730. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18731. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18732. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18733. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18734. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18735. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18736. 'try {} catch ($e) {',
  18737. ' if (rtl.isExt($e,ExtA)) {}',
  18738. ' else if (rtl.isExt($e,ExtB)) {',
  18739. ' var e = $e;',
  18740. ' } else throw $e',
  18741. '};',
  18742. '']));
  18743. end;
  18744. procedure TTestModule.TestExternalClass_As;
  18745. begin
  18746. StartProgram(false);
  18747. Add('{$modeswitch externalclass}');
  18748. Add('type');
  18749. Add(' TExtA = class external name ''ExtA''');
  18750. Add(' end;');
  18751. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18752. Add(' end;');
  18753. Add(' TExtC = class (TExtB)');
  18754. Add(' end;');
  18755. Add('var');
  18756. Add(' A: texta;');
  18757. Add(' B: textb;');
  18758. Add(' C: textc;');
  18759. Add('begin');
  18760. Add(' b:=a as textb;');
  18761. Add(' c:=a as textc;');
  18762. Add(' c:=b as textc;');
  18763. ConvertProgram;
  18764. CheckSource('TestExternalClass_Is',
  18765. LinesToStr([ // statements
  18766. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18767. ' this.$init = function () {',
  18768. ' };',
  18769. ' this.$final = function () {',
  18770. ' };',
  18771. '});',
  18772. 'this.A = null;',
  18773. 'this.B = null;',
  18774. 'this.C = null;',
  18775. '']),
  18776. LinesToStr([ // $mod.$main
  18777. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18778. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18779. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18780. '']));
  18781. end;
  18782. procedure TTestModule.TestExternalClass_DestructorFail;
  18783. begin
  18784. StartProgram(false);
  18785. Add('{$modeswitch externalclass}');
  18786. Add('type');
  18787. Add(' TExtA = class external name ''ExtA''');
  18788. Add(' destructor Free;');
  18789. Add(' end;');
  18790. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18791. nPasElementNotSupported);
  18792. ConvertProgram;
  18793. end;
  18794. procedure TTestModule.TestExternalClass_New;
  18795. begin
  18796. StartProgram(false);
  18797. Add([
  18798. '{$modeswitch externalclass}',
  18799. 'type',
  18800. ' TExtA = class external name ''ExtA''',
  18801. ' constructor New;',
  18802. ' constructor New(i: longint; j: longint = 2);',
  18803. ' end;',
  18804. 'var',
  18805. ' A: texta;',
  18806. 'begin',
  18807. ' a:=texta.new;',
  18808. ' a:=texta(texta.new);',
  18809. ' a:=texta.new();',
  18810. ' a:=texta.new(1);',
  18811. ' with texta do begin',
  18812. ' a:=new;',
  18813. ' a:=new();',
  18814. ' a:=new(2);',
  18815. ' end;',
  18816. ' a:=test1.texta.new;',
  18817. ' a:=test1.texta.new();',
  18818. ' a:=test1.texta.new(3);',
  18819. '']);
  18820. ConvertProgram;
  18821. CheckSource('TestExternalClass_New',
  18822. LinesToStr([ // statements
  18823. 'this.A = null;',
  18824. '']),
  18825. LinesToStr([ // $mod.$main
  18826. '$mod.A = new ExtA();',
  18827. '$mod.A = new ExtA();',
  18828. '$mod.A = new ExtA();',
  18829. '$mod.A = new ExtA(1,2);',
  18830. '$mod.A = new ExtA();',
  18831. '$mod.A = new ExtA();',
  18832. '$mod.A = new ExtA(2,2);',
  18833. '$mod.A = new ExtA();',
  18834. '$mod.A = new ExtA();',
  18835. '$mod.A = new ExtA(3,2);',
  18836. '']));
  18837. end;
  18838. procedure TTestModule.TestExternalClass_ClassOf_New;
  18839. begin
  18840. StartProgram(false);
  18841. Add('{$modeswitch externalclass}');
  18842. Add('type');
  18843. Add(' TExtAClass = class of TExtA;');
  18844. Add(' TExtA = class external name ''ExtA''');
  18845. Add(' C: TExtAClass;');
  18846. Add(' constructor New;');
  18847. Add(' end;');
  18848. Add('var');
  18849. Add(' A: texta;');
  18850. Add(' C: textaclass;');
  18851. Add('begin');
  18852. Add(' a:=c.new;');
  18853. Add(' a:=c.new();');
  18854. Add(' with C do begin');
  18855. Add(' a:=new;');
  18856. Add(' a:=new();');
  18857. Add(' end;');
  18858. Add(' a:=test1.c.new;');
  18859. Add(' a:=test1.c.new();');
  18860. Add(' a:=A.c.new();');
  18861. ConvertProgram;
  18862. CheckSource('TestExternalClass_ClassOf_New',
  18863. LinesToStr([ // statements
  18864. 'this.A = null;',
  18865. 'this.C = null;',
  18866. '']),
  18867. LinesToStr([ // $mod.$main
  18868. '$mod.A = new $mod.C();',
  18869. '$mod.A = new $mod.C();',
  18870. 'var $with = $mod.C;',
  18871. '$mod.A = new $with();',
  18872. '$mod.A = new $with();',
  18873. '$mod.A = new $mod.C();',
  18874. '$mod.A = new $mod.C();',
  18875. '$mod.A = new $mod.A.C();',
  18876. '']));
  18877. end;
  18878. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18879. begin
  18880. StartProgram(false);
  18881. Add([
  18882. '{$modeswitch externalclass}',
  18883. 'type',
  18884. ' TExtAClass = class of TExtA;',
  18885. ' TExtA = class external name ''ExtA''',
  18886. ' constructor New;',
  18887. ' end;',
  18888. 'function GetCreator: TExtAClass;',
  18889. 'begin',
  18890. ' Result:=TExtA;',
  18891. 'end;',
  18892. 'var',
  18893. ' A: texta;',
  18894. 'begin',
  18895. ' a:=getcreator.new;',
  18896. ' a:=getcreator().new;',
  18897. ' a:=getcreator().new();',
  18898. ' a:=getcreator.new();',
  18899. ' with getcreator do begin',
  18900. ' a:=new;',
  18901. ' a:=new();',
  18902. ' end;']);
  18903. ConvertProgram;
  18904. CheckSource('TestExternalClass_FuncClassOf_New',
  18905. LinesToStr([ // statements
  18906. 'this.GetCreator = function () {',
  18907. ' var Result = null;',
  18908. ' Result = ExtA;',
  18909. ' return Result;',
  18910. '};',
  18911. 'this.A = null;',
  18912. '']),
  18913. LinesToStr([ // $mod.$main
  18914. '$mod.A = new ($mod.GetCreator())();',
  18915. '$mod.A = new ($mod.GetCreator())();',
  18916. '$mod.A = new ($mod.GetCreator())();',
  18917. '$mod.A = new ($mod.GetCreator())();',
  18918. 'var $with = $mod.GetCreator();',
  18919. '$mod.A = new $with();',
  18920. '$mod.A = new $with();',
  18921. '']));
  18922. end;
  18923. procedure TTestModule.TestExternalClass_New_PasClassFail;
  18924. begin
  18925. StartProgram(false);
  18926. Add([
  18927. '{$modeswitch externalclass}',
  18928. 'type',
  18929. ' TExtA = class external name ''ExtA''',
  18930. ' constructor New;',
  18931. ' end;',
  18932. ' TBird = class(TExtA)',
  18933. ' end;',
  18934. 'begin',
  18935. ' TBird.new;',
  18936. '']);
  18937. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18938. ConvertProgram;
  18939. end;
  18940. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  18941. begin
  18942. StartProgram(false);
  18943. Add([
  18944. '{$modeswitch externalclass}',
  18945. 'type',
  18946. ' TExtA = class external name ''ExtA''',
  18947. ' constructor New;',
  18948. ' end;',
  18949. ' TBird = class(TExtA)',
  18950. ' end;',
  18951. 'begin',
  18952. ' TBird.new();',
  18953. '']);
  18954. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18955. ConvertProgram;
  18956. end;
  18957. procedure TTestModule.TestExternalClass_NewExtName;
  18958. begin
  18959. StartProgram(false);
  18960. Add([
  18961. '{$modeswitch externalclass}',
  18962. 'type',
  18963. ' TExtA = class external name ''ExtA''',
  18964. ' constructor New; external name ''Other'';',
  18965. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  18966. ' end;',
  18967. 'var',
  18968. ' A: texta;',
  18969. 'begin',
  18970. ' a:=texta.new;',
  18971. ' a:=texta(texta.new);',
  18972. ' a:=texta.new();',
  18973. ' a:=texta.new(1);',
  18974. ' with texta do begin',
  18975. ' a:=new;',
  18976. ' a:=new();',
  18977. ' a:=new(2);',
  18978. ' end;',
  18979. ' a:=test1.texta.new;',
  18980. ' a:=test1.texta.new();',
  18981. ' a:=test1.texta.new(3);',
  18982. '']);
  18983. ConvertProgram;
  18984. CheckSource('TestExternalClass_NewExtName',
  18985. LinesToStr([ // statements
  18986. 'this.A = null;',
  18987. '']),
  18988. LinesToStr([ // $mod.$main
  18989. '$mod.A = new Other();',
  18990. '$mod.A = new Other();',
  18991. '$mod.A = new Other();',
  18992. '$mod.A = new A.B(1,2);',
  18993. '$mod.A = new Other();',
  18994. '$mod.A = new Other();',
  18995. '$mod.A = new A.B(2,2);',
  18996. '$mod.A = new Other();',
  18997. '$mod.A = new Other();',
  18998. '$mod.A = new A.B(3,2);',
  18999. '']));
  19000. end;
  19001. procedure TTestModule.TestExternalClass_Constructor;
  19002. begin
  19003. StartProgram(false);
  19004. Add([
  19005. '{$modeswitch externalclass}',
  19006. 'type',
  19007. ' TExtA = class external name ''ExtA''',
  19008. ' public type',
  19009. ' TExtB = class external name ''ExtB''',
  19010. ' public type',
  19011. ' TExtC = class external name ''ExtC''',
  19012. ' constructor New;',
  19013. ' constructor New(i: word);',
  19014. ' end;',
  19015. ' end;',
  19016. ' constructor Create;',
  19017. ' constructor Create(i: longint; j: longint = 2);',
  19018. ' end;',
  19019. 'var',
  19020. ' A: texta;',
  19021. ' C: texta.textb.textc;',
  19022. 'begin',
  19023. ' a:=texta.create;',
  19024. ' a:=texta(texta.create);',
  19025. ' a:=texta.create();',
  19026. ' a:=texta.create(1);',
  19027. ' with texta do begin',
  19028. ' a:=create;',
  19029. ' a:=create();',
  19030. ' a:=create(2);',
  19031. ' end;',
  19032. ' a:=test1.texta.create;',
  19033. ' a:=test1.texta.create();',
  19034. ' a:=test1.texta.create(3);',
  19035. ' c:=texta.textb.textc.new;',
  19036. ' c:=texta.textb.textc.new();',
  19037. ' c:=texta.textb.textc.new(4);',
  19038. '']);
  19039. ConvertProgram;
  19040. CheckSource('TestExternalClass_Constructor',
  19041. LinesToStr([ // statements
  19042. 'this.A = null;',
  19043. 'this.C = null;',
  19044. '']),
  19045. LinesToStr([ // $mod.$main
  19046. '$mod.A = new ExtA.Create();',
  19047. '$mod.A = new ExtA.Create();',
  19048. '$mod.A = new ExtA.Create();',
  19049. '$mod.A = new ExtA.Create(1,2);',
  19050. '$mod.A = new ExtA.Create();',
  19051. '$mod.A = new ExtA.Create();',
  19052. '$mod.A = new ExtA.Create(2,2);',
  19053. '$mod.A = new ExtA.Create();',
  19054. '$mod.A = new ExtA.Create();',
  19055. '$mod.A = new ExtA.Create(3,2);',
  19056. '$mod.C = new ExtA.ExtB.ExtC();',
  19057. '$mod.C = new ExtA.ExtB.ExtC();',
  19058. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19059. '']));
  19060. end;
  19061. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19062. begin
  19063. StartProgram(false);
  19064. Add([
  19065. '{$modeswitch externalclass}',
  19066. 'type',
  19067. ' TExtA = class external name ''ExtA''',
  19068. ' constructor Create; external name ''{}'';',
  19069. ' end;',
  19070. 'var',
  19071. ' A: texta;',
  19072. 'begin',
  19073. ' a:=texta.create;',
  19074. ' a:=texta(texta.create);',
  19075. ' a:=texta.create();',
  19076. ' with texta do begin',
  19077. ' a:=create;',
  19078. ' a:=create();',
  19079. ' end;',
  19080. ' a:=test1.texta.create;',
  19081. ' a:=test1.texta.create();',
  19082. '']);
  19083. ConvertProgram;
  19084. CheckSource('TestExternalClass_ConstructorBrackets',
  19085. LinesToStr([ // statements
  19086. 'this.A = null;',
  19087. '']),
  19088. LinesToStr([ // $mod.$main
  19089. '$mod.A = {};',
  19090. '$mod.A = {};',
  19091. '$mod.A = {};',
  19092. '$mod.A = {};',
  19093. '$mod.A = {};',
  19094. '$mod.A = {};',
  19095. '$mod.A = {};',
  19096. '']));
  19097. end;
  19098. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19099. begin
  19100. StartProgram(false);
  19101. Add('{$modeswitch externalclass}');
  19102. Add('type');
  19103. Add(' TExtA = class external name ''ExtA''');
  19104. Add(' constructor New;');
  19105. Add(' end;');
  19106. Add('function DoIt: longint;');
  19107. Add('const ExtA: longint = 3;');
  19108. Add('begin');
  19109. Add(' Result:=ExtA;');
  19110. Add('end;');
  19111. Add('var');
  19112. Add(' A: texta;');
  19113. Add('begin');
  19114. Add(' a:=texta.new;');
  19115. ConvertProgram;
  19116. CheckSource('TestExternalClass_LocalConstSameName',
  19117. LinesToStr([ // statements
  19118. 'var ExtA$1 = 3;',
  19119. 'this.DoIt = function () {',
  19120. ' var Result = 0;',
  19121. ' Result = ExtA$1;',
  19122. ' return Result;',
  19123. '};',
  19124. 'this.A = null;',
  19125. '']),
  19126. LinesToStr([ // $mod.$main
  19127. '$mod.A = new ExtA();',
  19128. '']));
  19129. end;
  19130. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19131. begin
  19132. StartProgram(false);
  19133. Add('{$modeswitch externalclass}');
  19134. Add('type');
  19135. Add(' TExtA = class external name ''ExtA''');
  19136. Add(' procedure DoIt;');
  19137. Add(' end;');
  19138. Add(' TMyA = class(TExtA)');
  19139. Add(' procedure DoIt;');
  19140. Add(' end;');
  19141. Add('procedure TMyA.DoIt; begin end;');
  19142. Add('begin');
  19143. ConvertProgram;
  19144. CheckSource('TestExternalClass_ReintroduceOverload',
  19145. LinesToStr([ // statements
  19146. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19147. ' this.$init = function () {',
  19148. ' };',
  19149. ' this.$final = function () {',
  19150. ' };',
  19151. ' this.DoIt$1 = function () {',
  19152. ' };',
  19153. '});',
  19154. '']),
  19155. LinesToStr([ // $mod.$main
  19156. '']));
  19157. end;
  19158. procedure TTestModule.TestExternalClass_Inherited;
  19159. begin
  19160. StartProgram(false);
  19161. Add('{$modeswitch externalclass}');
  19162. Add('type');
  19163. Add(' TExtA = class external name ''ExtA''');
  19164. Add(' procedure DoIt(i: longint = 1); virtual;');
  19165. Add(' procedure DoSome(j: longint = 2);');
  19166. Add(' end;');
  19167. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19168. Add(' end;');
  19169. Add(' TMyC = class(TExtB)');
  19170. Add(' procedure DoIt(i: longint = 1); override;');
  19171. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19172. Add(' end;');
  19173. Add('procedure TMyC.DoIt(i: longint);');
  19174. Add('begin');
  19175. Add(' inherited;');
  19176. Add(' inherited DoIt;');
  19177. Add(' inherited DoIt();');
  19178. Add(' inherited DoIt(3);');
  19179. Add(' inherited DoSome;');
  19180. Add(' inherited DoSome();');
  19181. Add(' inherited DoSome(4);');
  19182. Add('end;');
  19183. Add('procedure TMyC.DoSome(j: longint);');
  19184. Add('begin');
  19185. Add(' inherited;');
  19186. Add('end;');
  19187. Add('begin');
  19188. ConvertProgram;
  19189. CheckSource('TestExternalClass_ReintroduceOverload',
  19190. LinesToStr([ // statements
  19191. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19192. ' this.$init = function () {',
  19193. ' };',
  19194. ' this.$final = function () {',
  19195. ' };',
  19196. ' this.DoIt = function (i) {',
  19197. ' ExtB.DoIt.apply(this, arguments);',
  19198. ' ExtB.DoIt.call(this, 1);',
  19199. ' ExtB.DoIt.call(this, 1);',
  19200. ' ExtB.DoIt.call(this, 3);',
  19201. ' ExtB.DoSome.call(this, 2);',
  19202. ' ExtB.DoSome.call(this, 2);',
  19203. ' ExtB.DoSome.call(this, 4);',
  19204. ' };',
  19205. ' this.DoSome$1 = function (j) {',
  19206. ' ExtB.DoSome.apply(this, arguments);',
  19207. ' };',
  19208. '});',
  19209. '']),
  19210. LinesToStr([ // $mod.$main
  19211. '']));
  19212. end;
  19213. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19214. begin
  19215. StartProgram(false);
  19216. Add('{$modeswitch externalclass}');
  19217. Add('type');
  19218. Add(' TObject = class');
  19219. Add(' end;');
  19220. Add(' TExtA = class external name ''ExtA''(TObject)');
  19221. Add(' end;');
  19222. Add('begin');
  19223. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19224. ConvertProgram;
  19225. end;
  19226. procedure TTestModule.TestExternalClass_NewInstance;
  19227. begin
  19228. StartProgram(false);
  19229. Add('{$modeswitch externalclass}');
  19230. Add('type');
  19231. Add(' TExtA = class external name ''ExtA''');
  19232. Add(' end;');
  19233. Add(' TMyB = class(TExtA)');
  19234. Add(' protected');
  19235. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19236. Add(' end;');
  19237. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19238. Add('begin end;');
  19239. Add('begin');
  19240. ConvertProgram;
  19241. CheckSource('TestExternalClass_NewInstance',
  19242. LinesToStr([ // statements
  19243. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19244. ' this.$init = function () {',
  19245. ' };',
  19246. ' this.$final = function () {',
  19247. ' };',
  19248. ' this.NewInstance = function (fnname, paramarray) {',
  19249. ' var Result = null;',
  19250. ' return Result;',
  19251. ' };',
  19252. '});',
  19253. '']),
  19254. LinesToStr([ // $mod.$main
  19255. '']));
  19256. end;
  19257. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19258. begin
  19259. StartProgram(false);
  19260. Add('{$modeswitch externalclass}');
  19261. Add('type');
  19262. Add(' TExtA = class external name ''ExtA''');
  19263. Add(' end;');
  19264. Add(' TMyB = class(TExtA)');
  19265. Add(' protected');
  19266. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19267. Add(' end;');
  19268. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19269. Add('begin end;');
  19270. Add('begin');
  19271. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19272. ConvertProgram;
  19273. end;
  19274. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19275. begin
  19276. StartProgram(false);
  19277. Add('{$modeswitch externalclass}');
  19278. Add('type');
  19279. Add(' TExtA = class external name ''ExtA''');
  19280. Add(' end;');
  19281. Add(' TMyB = class(TExtA)');
  19282. Add(' protected');
  19283. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19284. Add(' end;');
  19285. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19286. Add('begin end;');
  19287. Add('begin');
  19288. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19289. nIncompatibleTypeArgNo);
  19290. ConvertProgram;
  19291. end;
  19292. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19293. begin
  19294. StartProgram(false);
  19295. Add('{$modeswitch externalclass}');
  19296. Add('type');
  19297. Add(' TExtA = class external name ''ExtA''');
  19298. Add(' end;');
  19299. Add(' TMyB = class(TExtA)');
  19300. Add(' protected');
  19301. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19302. Add(' end;');
  19303. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19304. Add('begin end;');
  19305. Add('begin');
  19306. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19307. nIncompatibleTypeArgNo);
  19308. ConvertProgram;
  19309. end;
  19310. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19311. begin
  19312. StartProgram(false);
  19313. Add([
  19314. '{$modeswitch externalclass}',
  19315. 'type',
  19316. ' TJSFunction = class external name ''Function''',
  19317. ' end;',
  19318. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19319. ' constructor New(w: word);',
  19320. ' end;',
  19321. ' TBird = class (TExtA)',
  19322. ' public',
  19323. ' Size: word;',
  19324. ' class var Legs: word;',
  19325. ' constructor Create(a: word);',
  19326. ' end;',
  19327. ' TEagle = class (TBird)',
  19328. ' public',
  19329. ' constructor Create(b: word); reintroduce;',
  19330. ' end;',
  19331. 'constructor TBird.Create(a: word);',
  19332. 'begin',
  19333. ' inherited;', // silently ignored
  19334. ' inherited New(a);', // this.$func(a)
  19335. 'end;',
  19336. 'constructor TEagle.Create(b: word);',
  19337. 'begin',
  19338. ' inherited Create(b);',
  19339. 'end;',
  19340. 'var',
  19341. ' Bird: TBird;',
  19342. ' Eagle: TEagle;',
  19343. 'begin',
  19344. ' Bird:=TBird.Create(3);',
  19345. ' Eagle:=TEagle.Create(4);',
  19346. ' Bird.Size:=Bird.Size+5;',
  19347. ' Bird.Legs:=Bird.Legs+6;',
  19348. ' Eagle.Size:=Eagle.Size+5;',
  19349. ' Eagle.Legs:=Eagle.Legs+6;',
  19350. '']);
  19351. ConvertProgram;
  19352. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19353. LinesToStr([ // statements
  19354. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19355. ' this.Legs = 0;',
  19356. ' this.$init = function () {',
  19357. ' this.Size = 0;',
  19358. ' };',
  19359. ' this.$final = function () {',
  19360. ' };',
  19361. ' this.Create = function (a) {',
  19362. ' this.$ancestorfunc(a);',
  19363. ' return this;',
  19364. ' };',
  19365. '});',
  19366. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19367. ' this.Create$1 = function (b) {',
  19368. ' $mod.TBird.Create.call(this, b);',
  19369. ' return this;',
  19370. ' };',
  19371. '});',
  19372. 'this.Bird = null;',
  19373. 'this.Eagle = null;',
  19374. '']),
  19375. LinesToStr([ // $mod.$main
  19376. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19377. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19378. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19379. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19380. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19381. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19382. '']));
  19383. end;
  19384. procedure TTestModule.TestExternalClass_PascalProperty;
  19385. begin
  19386. StartProgram(false);
  19387. Add('{$modeswitch externalclass}');
  19388. Add('type');
  19389. Add(' TJSElement = class;');
  19390. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19391. Add(' TJSElement = class external name ''ExtA''');
  19392. Add(' end;');
  19393. Add(' TControl = class(TJSElement)');
  19394. Add(' private');
  19395. Add(' FOnClick: TJSNotifyEvent;');
  19396. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19397. Add(' procedure Click(Sender: TJSElement);');
  19398. Add(' end;');
  19399. Add('procedure TControl.Click(Sender: TJSElement);');
  19400. Add('begin');
  19401. Add(' OnClick(Self);');
  19402. Add('end;');
  19403. Add('var');
  19404. Add(' Ctrl: TControl;');
  19405. Add('begin');
  19406. Add(' Ctrl.OnClick:[email protected];');
  19407. Add(' Ctrl.OnClick(Ctrl);');
  19408. ConvertProgram;
  19409. CheckSource('TestExternalClass_PascalProperty',
  19410. LinesToStr([ // statements
  19411. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19412. ' this.$init = function () {',
  19413. ' this.FOnClick = null;',
  19414. ' };',
  19415. ' this.$final = function () {',
  19416. ' this.FOnClick = undefined;',
  19417. ' };',
  19418. ' this.Click = function (Sender) {',
  19419. ' this.FOnClick(this);',
  19420. ' };',
  19421. '});',
  19422. 'this.Ctrl = null;',
  19423. '']),
  19424. LinesToStr([ // $mod.$main
  19425. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19426. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19427. '']));
  19428. end;
  19429. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19430. begin
  19431. StartProgram(false);
  19432. Add([
  19433. '{$modeswitch externalclass}',
  19434. 'type',
  19435. ' IUnknown = interface end;',
  19436. ' TObject = class',
  19437. ' end;',
  19438. ' TChild = class',
  19439. ' end;',
  19440. ' TExtRootA = class external name ''ExtRootA''',
  19441. ' end;',
  19442. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19443. ' end;',
  19444. ' TExtRootB = class external name ''ExtRootB''',
  19445. ' end;',
  19446. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19447. ' end;',
  19448. ' TExtString = class external name ''String''',
  19449. ' function charAt(aIndex : NativeInt) : string;',
  19450. ' end;',
  19451. 'var',
  19452. ' Obj: TObject;',
  19453. ' Child: TChild;',
  19454. ' RootA: TExtRootA;',
  19455. ' ChildA: TExtChildA;',
  19456. ' RootB: TExtRootB;',
  19457. ' ChildB: TExtChildB;',
  19458. ' i: IUnknown;',
  19459. ' s: string;',
  19460. ' v: jsvalue;',
  19461. 'begin',
  19462. ' obj:=tobject(roota);',
  19463. ' obj:=tobject(childa);',
  19464. ' child:=tchild(tobject(roota));',
  19465. ' roota:=textroota(obj);',
  19466. ' roota:=textroota(child);',
  19467. ' roota:=textroota(rootb);',
  19468. ' roota:=textroota(childb);',
  19469. ' childa:=textchilda(textroota(obj));',
  19470. ' roota:=TExtRootA(i);',
  19471. ' s:=TExtString(s).charAt(7);',
  19472. ' s:=TExtString(v).charAt(8);',
  19473. '']);
  19474. ConvertProgram;
  19475. CheckSource('TestExternalClass_TypeCastToRootClass',
  19476. LinesToStr([ // statements
  19477. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19478. 'rtl.createClass(this, "TObject", null, function () {',
  19479. ' this.$init = function () {',
  19480. ' };',
  19481. ' this.$final = function () {',
  19482. ' };',
  19483. '});',
  19484. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19485. '});',
  19486. 'this.Obj = null;',
  19487. 'this.Child = null;',
  19488. 'this.RootA = null;',
  19489. 'this.ChildA = null;',
  19490. 'this.RootB = null;',
  19491. 'this.ChildB = null;',
  19492. 'this.i = null;',
  19493. 'this.s = "";',
  19494. 'this.v = undefined;',
  19495. '']),
  19496. LinesToStr([ // $mod.$main
  19497. '$mod.Obj = $mod.RootA;',
  19498. '$mod.Obj = $mod.ChildA;',
  19499. '$mod.Child = $mod.RootA;',
  19500. '$mod.RootA = $mod.Obj;',
  19501. '$mod.RootA = $mod.Child;',
  19502. '$mod.RootA = $mod.RootB;',
  19503. '$mod.RootA = $mod.ChildB;',
  19504. '$mod.ChildA = $mod.Obj;',
  19505. '$mod.RootA = $mod.i;',
  19506. '$mod.s = $mod.s.charAt(7);',
  19507. '$mod.s = $mod.v.charAt(8);',
  19508. '']));
  19509. end;
  19510. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19511. begin
  19512. StartProgram(false);
  19513. Add([
  19514. '{$modeswitch externalclass}',
  19515. 'type',
  19516. ' IUnknown = interface end;',
  19517. ' IBird = interface(IUnknown) end;',
  19518. ' TClass = class of TObject;',
  19519. ' TObject = class',
  19520. ' end;',
  19521. ' TChild = class',
  19522. ' end;',
  19523. ' TJSObject = class external name ''Object''',
  19524. ' end;',
  19525. ' TRec = record end;',
  19526. 'var',
  19527. ' Obj: TObject;',
  19528. ' Child: TChild;',
  19529. ' i: IUnknown;',
  19530. ' Bird: IBird;',
  19531. ' j: TJSObject;',
  19532. ' r: TRec;',
  19533. ' c: TClass;',
  19534. 'begin',
  19535. ' j:=tjsobject(IUnknown);',
  19536. ' j:=tjsobject(IBird);',
  19537. ' j:=tjsobject(TObject);',
  19538. ' j:=tjsobject(TChild);',
  19539. ' j:=tjsobject(TRec);',
  19540. ' j:=tjsobject(Obj);',
  19541. ' j:=tjsobject(Child);',
  19542. ' j:=tjsobject(i);',
  19543. ' j:=tjsobject(Bird);',
  19544. ' j:=tjsobject(r);',
  19545. ' j:=tjsobject(c);',
  19546. '']);
  19547. ConvertProgram;
  19548. CheckSource('TestExternalClass_TypeCastToJSObject',
  19549. LinesToStr([ // statements
  19550. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19551. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19552. 'rtl.createClass(this, "TObject", null, function () {',
  19553. ' this.$init = function () {',
  19554. ' };',
  19555. ' this.$final = function () {',
  19556. ' };',
  19557. '});',
  19558. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19559. '});',
  19560. 'rtl.recNewT(this, "TRec", function () {',
  19561. ' this.$eq = function (b) {',
  19562. ' return true;',
  19563. ' };',
  19564. ' this.$assign = function (s) {',
  19565. ' return this;',
  19566. ' };',
  19567. '});',
  19568. 'this.Obj = null;',
  19569. 'this.Child = null;',
  19570. 'this.i = null;',
  19571. 'this.Bird = null;',
  19572. 'this.j = null;',
  19573. 'this.r = this.TRec.$new();',
  19574. 'this.c = null;',
  19575. '']),
  19576. LinesToStr([ // $mod.$main
  19577. '$mod.j = $mod.IUnknown;',
  19578. '$mod.j = $mod.IBird;',
  19579. '$mod.j = $mod.TObject;',
  19580. '$mod.j = $mod.TChild;',
  19581. '$mod.j = $mod.TRec;',
  19582. '$mod.j = $mod.Obj;',
  19583. '$mod.j = $mod.Child;',
  19584. '$mod.j = $mod.i;',
  19585. '$mod.j = $mod.Bird;',
  19586. '$mod.j = $mod.r;',
  19587. '$mod.j = $mod.c;',
  19588. '']));
  19589. end;
  19590. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19591. begin
  19592. StartProgram(false);
  19593. Add('{$modeswitch externalclass}');
  19594. Add('type');
  19595. Add(' TJSString = class external name ''String''');
  19596. Add(' class function fromCharCode() : string; varargs;');
  19597. Add(' function anchor(const aName : string) : string;');
  19598. Add(' end;');
  19599. Add('var');
  19600. Add(' s: string;');
  19601. Add('begin');
  19602. Add(' s:=TJSString.fromCharCode(65,66);');
  19603. Add(' s:=TJSString(s).anchor(s);');
  19604. Add(' s:=TJSString(''foo'').anchor(s);');
  19605. ConvertProgram;
  19606. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19607. LinesToStr([ // statements
  19608. 'this.s = "";',
  19609. '']),
  19610. LinesToStr([ // $mod.$main
  19611. '$mod.s = String.fromCharCode(65, 66);',
  19612. '$mod.s = $mod.s.anchor($mod.s);',
  19613. '$mod.s = "foo".anchor($mod.s);',
  19614. '']));
  19615. end;
  19616. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19617. begin
  19618. StartProgram(false);
  19619. Add([
  19620. '{$modeswitch externalclass}',
  19621. 'type',
  19622. ' TJSObject = class external name ''Object'' end;',
  19623. ' TJSFunction = class external name ''Function''',
  19624. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19625. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19626. ' end;',
  19627. ' TObject = class',
  19628. ' procedure DoIt(i: longint);',
  19629. ' end;',
  19630. ' TFuncInt = function(o: TObject): longint;',
  19631. 'function GetIt(o: TObject): longint;',
  19632. ' procedure Sub; begin end;',
  19633. 'var',
  19634. ' f: TJSFunction;',
  19635. ' fi: TFuncInt;',
  19636. 'begin',
  19637. ' fi:=TFuncInt(f);',
  19638. ' f:=TJSFunction(fi);',
  19639. ' f:=TJSFunction(@GetIt);',
  19640. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19641. ' f:=TJSFunction(@Sub);',
  19642. ' f:=TJSFunction(@o.doit);',
  19643. ' f:=TJSFunction(fi).bind(nil,4)',
  19644. 'end;',
  19645. 'procedure TObject.DoIt(i: longint);',
  19646. ' procedure Sub; begin end;',
  19647. 'var f: TJSFunction;',
  19648. 'begin',
  19649. ' f:=TJSFunction(@DoIt);',
  19650. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19651. ' f:=TJSFunction(@Sub);',
  19652. ' f:=TJSFunction(@GetIt);',
  19653. 'end;',
  19654. 'begin']);
  19655. ConvertProgram;
  19656. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19657. LinesToStr([ // statements
  19658. 'rtl.createClass(this, "TObject", null, function () {',
  19659. ' this.$init = function () {',
  19660. ' };',
  19661. ' this.$final = function () {',
  19662. ' };',
  19663. ' this.DoIt = function (i) {',
  19664. ' var $Self = this;',
  19665. ' function Sub() {',
  19666. ' };',
  19667. ' var f = null;',
  19668. ' f = this.DoIt;',
  19669. ' f = this.DoIt.bind(null, 13);',
  19670. ' f = Sub;',
  19671. ' f = $mod.GetIt;',
  19672. ' };',
  19673. '});',
  19674. 'this.GetIt = function (o) {',
  19675. ' var Result = 0;',
  19676. ' function Sub() {',
  19677. ' };',
  19678. ' var f = null;',
  19679. ' var fi = null;',
  19680. ' fi = f;',
  19681. ' f = fi;',
  19682. ' f = $mod.GetIt;',
  19683. ' f = $mod.GetIt.bind(null, 3);',
  19684. ' f = Sub;',
  19685. ' f = $mod.TObject.DoIt;',
  19686. ' f = fi.bind(null, 4);',
  19687. ' return Result;',
  19688. '};',
  19689. '']),
  19690. LinesToStr([ // $mod.$main
  19691. '']));
  19692. end;
  19693. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19694. begin
  19695. StartProgram(false);
  19696. Add([
  19697. '{$mode delphi}',
  19698. '{$modeswitch externalclass}',
  19699. 'type',
  19700. ' TJSObject = class external name ''Object'' end;',
  19701. ' TJSWindow = class external name ''Window''(TJSObject)',
  19702. ' procedure Open;',
  19703. ' end;',
  19704. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19705. ' procedure Execute;',
  19706. ' end;',
  19707. 'procedure Fly;',
  19708. 'var',
  19709. ' w: TJSWindow;',
  19710. ' e: TJSEventTarget;',
  19711. 'begin',
  19712. ' w:=TJSWindow(e);',
  19713. ' e:=TJSEventTarget(w);',
  19714. 'end;',
  19715. 'begin']);
  19716. ConvertProgram;
  19717. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19718. LinesToStr([ // statements
  19719. 'this.Fly = function () {',
  19720. ' var w = null;',
  19721. ' var e = null;',
  19722. ' w = e;',
  19723. ' e = w;',
  19724. '};',
  19725. '']),
  19726. LinesToStr([ // $mod.$main
  19727. '']));
  19728. end;
  19729. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19730. begin
  19731. StartProgram(false);
  19732. Add('{$modeswitch externalclass}');
  19733. Add('type');
  19734. Add(' TJSString = class external name ''String''');
  19735. Add(' class function fromCharCode() : string; varargs;');
  19736. Add(' end;');
  19737. Add('var');
  19738. Add(' s: string;');
  19739. Add(' sObj: TJSString;');
  19740. Add('begin');
  19741. Add(' s:=sObj.fromCharCode(65,66);');
  19742. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19743. nExternalClassInstanceCannotAccessStaticX);
  19744. ConvertProgram;
  19745. end;
  19746. procedure TTestModule.TestExternalClass_BracketAccessor;
  19747. begin
  19748. StartProgram(false);
  19749. Add([
  19750. '{$modeswitch externalclass}',
  19751. 'type',
  19752. ' TJSArray = class external name ''Array2''',
  19753. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19754. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19755. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19756. ' end;',
  19757. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19758. 'begin end;',
  19759. 'var',
  19760. ' Arr: tjsarray;',
  19761. ' s: string;',
  19762. ' i: longint;',
  19763. ' v: jsvalue;',
  19764. 'begin',
  19765. ' v:=arr[0];',
  19766. ' v:=arr.items[1];',
  19767. ' arr[2]:=s;',
  19768. ' arr.items[3]:=s;',
  19769. ' arr[4]:=i;',
  19770. ' arr[5]:=arr[6];',
  19771. ' arr.items[7]:=arr.items[8];',
  19772. ' with arr do items[9]:=items[10];',
  19773. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19774. ' with arr do begin',
  19775. ' v:=GetItems(14);',
  19776. ' setitems(15,16);',
  19777. ' end;',
  19778. ' v:=test1.arr.items[17];',
  19779. ' test1.arr.items[18]:=v;',
  19780. '']);
  19781. ConvertProgram;
  19782. CheckSource('TestExternalClass_BracketAccessor',
  19783. LinesToStr([ // statements
  19784. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19785. '};',
  19786. 'this.Arr = null;',
  19787. 'this.s = "";',
  19788. 'this.i = 0;',
  19789. 'this.v = undefined;',
  19790. '']),
  19791. LinesToStr([ // $mod.$main
  19792. '$mod.v = $mod.Arr[0];',
  19793. '$mod.v = $mod.Arr[1];',
  19794. '$mod.Arr[2] = $mod.s;',
  19795. '$mod.Arr[3] = $mod.s;',
  19796. '$mod.Arr[4] = $mod.i;',
  19797. '$mod.Arr[5] = $mod.Arr[6];',
  19798. '$mod.Arr[7] = $mod.Arr[8];',
  19799. 'var $with = $mod.Arr;',
  19800. '$with[9] = $with[10];',
  19801. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19802. ' a: 9,',
  19803. ' p: $mod.Arr,',
  19804. ' get: function () {',
  19805. ' return this.p[this.a];',
  19806. ' },',
  19807. ' set: function (v) {',
  19808. ' this.p[this.a] = v;',
  19809. ' }',
  19810. '}, {',
  19811. ' a: 10,',
  19812. ' p: $mod.Arr,',
  19813. ' get: function () {',
  19814. ' return this.p[this.a];',
  19815. ' },',
  19816. ' set: function (v) {',
  19817. ' this.p[this.a] = v;',
  19818. ' }',
  19819. '});',
  19820. 'var $with1 = $mod.Arr;',
  19821. '$mod.v = $with1[14];',
  19822. '$with1[15] = 16;',
  19823. '$mod.v = $mod.Arr[17];',
  19824. '$mod.Arr[18] = $mod.v;',
  19825. '']));
  19826. end;
  19827. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19828. begin
  19829. StartProgram(false);
  19830. Add([
  19831. '{$modeswitch externalclass}',
  19832. 'type',
  19833. ' TJSArray = class external name ''Array2''',
  19834. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19835. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19836. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19837. ' end;',
  19838. ' TMyArr = class(TJSArray)',
  19839. ' procedure DoIt;',
  19840. ' end;',
  19841. 'procedure tmyarr.DoIt;',
  19842. 'begin',
  19843. ' Items[1]:=Items[2];',
  19844. ' SetItems(3,getItems(4));',
  19845. 'end;',
  19846. 'var',
  19847. ' Arr: tmyarr;',
  19848. ' s: string;',
  19849. ' i: longint;',
  19850. ' v: jsvalue;',
  19851. 'begin',
  19852. ' v:=arr[0];',
  19853. ' v:=arr.items[1];',
  19854. ' arr[2]:=s;',
  19855. ' arr.items[3]:=s;',
  19856. ' arr[4]:=i;',
  19857. ' arr[5]:=arr[6];',
  19858. ' arr.items[7]:=arr.items[8];',
  19859. ' with arr do items[9]:=items[10];',
  19860. ' with arr do begin',
  19861. ' v:=GetItems(14);',
  19862. ' setitems(15,16);',
  19863. ' end;',
  19864. '']);
  19865. ConvertProgram;
  19866. CheckSource('TestExternalClass_BracketAccessor_Call',
  19867. LinesToStr([ // statements
  19868. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19869. ' this.$init = function () {',
  19870. ' };',
  19871. ' this.$final = function () {',
  19872. ' };',
  19873. ' this.DoIt = function () {',
  19874. ' this[1] = this[2];',
  19875. ' this[3] = this[4];',
  19876. ' };',
  19877. '});',
  19878. 'this.Arr = null;',
  19879. 'this.s = "";',
  19880. 'this.i = 0;',
  19881. 'this.v = undefined;',
  19882. '']),
  19883. LinesToStr([ // $mod.$main
  19884. '$mod.v = $mod.Arr[0];',
  19885. '$mod.v = $mod.Arr[1];',
  19886. '$mod.Arr[2] = $mod.s;',
  19887. '$mod.Arr[3] = $mod.s;',
  19888. '$mod.Arr[4] = $mod.i;',
  19889. '$mod.Arr[5] = $mod.Arr[6];',
  19890. '$mod.Arr[7] = $mod.Arr[8];',
  19891. 'var $with = $mod.Arr;',
  19892. '$with[9] = $with[10];',
  19893. 'var $with1 = $mod.Arr;',
  19894. '$mod.v = $with1[14];',
  19895. '$with1[15] = 16;',
  19896. '']));
  19897. end;
  19898. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  19899. begin
  19900. StartProgram(false);
  19901. Add('{$modeswitch externalclass}');
  19902. Add('type');
  19903. Add(' TJSArray = class external name ''Array2''');
  19904. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  19905. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  19906. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  19907. Add(' end;');
  19908. Add('begin');
  19909. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  19910. nBracketAccessorOfExternalClassMustHaveOneParameter);
  19911. ConvertProgram;
  19912. end;
  19913. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  19914. begin
  19915. StartProgram(false);
  19916. Add('{$modeswitch externalclass}');
  19917. Add('type');
  19918. Add(' TJSArray = class external name ''Array2''');
  19919. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19920. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  19921. Add(' end;');
  19922. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  19923. Add('begin end;');
  19924. Add('var');
  19925. Add(' Arr: tjsarray;');
  19926. Add(' v: jsvalue;');
  19927. Add('begin');
  19928. Add(' v:=arr[0];');
  19929. Add(' v:=arr.items[1];');
  19930. Add(' with arr do v:=items[2];');
  19931. Add(' doit(arr[3],arr[4]);');
  19932. ConvertProgram;
  19933. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  19934. LinesToStr([ // statements
  19935. 'this.DoIt = function (vI, vJ) {',
  19936. '};',
  19937. 'this.Arr = null;',
  19938. 'this.v = undefined;',
  19939. '']),
  19940. LinesToStr([ // $mod.$main
  19941. '$mod.v = $mod.Arr[0];',
  19942. '$mod.v = $mod.Arr[1];',
  19943. 'var $with = $mod.Arr;',
  19944. '$mod.v = $with[2];',
  19945. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  19946. '']));
  19947. end;
  19948. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  19949. begin
  19950. StartProgram(false);
  19951. Add('{$modeswitch externalclass}');
  19952. Add('type');
  19953. Add(' TJSArray = class external name ''Array2''');
  19954. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19955. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  19956. Add(' end;');
  19957. Add('var');
  19958. Add(' Arr: tjsarray;');
  19959. Add(' s: string;');
  19960. Add(' i: longint;');
  19961. Add(' v: jsvalue;');
  19962. Add('begin');
  19963. Add(' arr[2]:=s;');
  19964. Add(' arr.items[3]:=s;');
  19965. Add(' arr[4]:=i;');
  19966. Add(' with arr do items[5]:=i;');
  19967. ConvertProgram;
  19968. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  19969. LinesToStr([ // statements
  19970. 'this.Arr = null;',
  19971. 'this.s = "";',
  19972. 'this.i = 0;',
  19973. 'this.v = undefined;',
  19974. '']),
  19975. LinesToStr([ // $mod.$main
  19976. '$mod.Arr[2] = $mod.s;',
  19977. '$mod.Arr[3] = $mod.s;',
  19978. '$mod.Arr[4] = $mod.i;',
  19979. 'var $with = $mod.Arr;',
  19980. '$with[5] = $mod.i;',
  19981. '']));
  19982. end;
  19983. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  19984. begin
  19985. StartProgram(false);
  19986. Add('{$modeswitch externalclass}');
  19987. Add('type');
  19988. Add(' TJSArray = class external name ''Array2''');
  19989. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19990. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  19991. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  19992. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  19993. Add(' end;');
  19994. Add('var');
  19995. Add(' Arr: tjsarray;');
  19996. Add(' s: string;');
  19997. Add(' i: longint;');
  19998. Add(' v: jsvalue;');
  19999. Add('begin');
  20000. Add(' arr[2]:=s;');
  20001. Add(' arr.items[3]:=s;');
  20002. Add(' arr.numbers[4]:=i;');
  20003. Add(' with arr do items[5]:=i;');
  20004. Add(' with arr do numbers[6]:=i;');
  20005. ConvertProgram;
  20006. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20007. LinesToStr([ // statements
  20008. 'this.Arr = null;',
  20009. 'this.s = "";',
  20010. 'this.i = 0;',
  20011. 'this.v = undefined;',
  20012. '']),
  20013. LinesToStr([ // $mod.$main
  20014. '$mod.Arr[2] = $mod.s;',
  20015. '$mod.Arr[3] = $mod.s;',
  20016. '$mod.Arr[4] = $mod.i;',
  20017. 'var $with = $mod.Arr;',
  20018. '$with[5] = $mod.i;',
  20019. 'var $with1 = $mod.Arr;',
  20020. '$with1[6] = $mod.i;',
  20021. '']));
  20022. end;
  20023. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20024. begin
  20025. StartProgram(false);
  20026. Add('{$modeswitch externalclass}');
  20027. Add('type');
  20028. Add(' TJSArray = class external name ''Array2''');
  20029. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20030. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20031. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20032. Add(' end;');
  20033. Add('var');
  20034. Add(' Arr: tjsarray;');
  20035. Add(' i: longint;');
  20036. Add(' IntArr: array of longint;');
  20037. Add(' v: jsvalue;');
  20038. Add('begin');
  20039. Add(' v:=arr.items[i];');
  20040. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20041. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20042. ConvertProgram;
  20043. CheckSource('TestExternalClass_BracketAccessor_Index',
  20044. LinesToStr([ // statements
  20045. 'this.Arr = null;',
  20046. 'this.i = 0;',
  20047. 'this.IntArr = [];',
  20048. 'this.v = undefined;',
  20049. '']),
  20050. LinesToStr([ // $mod.$main
  20051. '$mod.v = $mod.Arr[$mod.i];',
  20052. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20053. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20054. '']));
  20055. end;
  20056. procedure TTestModule.TestExternalClass_ForInJSObject;
  20057. begin
  20058. StartProgram(false);
  20059. Add([
  20060. '{$modeswitch externalclass}',
  20061. 'type',
  20062. ' TJSObject = class external name ''Object''',
  20063. ' end;',
  20064. 'var',
  20065. ' o: TJSObject;',
  20066. ' key: string;',
  20067. 'begin',
  20068. ' for key in o do',
  20069. ' if key=''abc'' then ;',
  20070. '']);
  20071. ConvertProgram;
  20072. CheckSource('TestExternalClass_ForInJSObject',
  20073. LinesToStr([ // statements
  20074. 'this.o = null;',
  20075. 'this.key = "";',
  20076. '']),
  20077. LinesToStr([ // $mod.$main
  20078. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20079. '']));
  20080. end;
  20081. procedure TTestModule.TestExternalClass_ForInJSArray;
  20082. begin
  20083. StartProgram(false);
  20084. Add([
  20085. '{$modeswitch externalclass}',
  20086. 'type',
  20087. ' TJSInt8Array = class external name ''Int8Array''',
  20088. ' private',
  20089. ' flength: NativeInt external name ''length'';',
  20090. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20091. ' public',
  20092. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20093. ' property Length: NativeInt read flength;',
  20094. ' end;',
  20095. 'var',
  20096. ' a: TJSInt8Array;',
  20097. ' value: shortint;',
  20098. 'begin',
  20099. ' for value in a do',
  20100. ' if value=3 then ;',
  20101. '']);
  20102. ConvertProgram;
  20103. CheckSource('TestExternalClass_ForInJSArray',
  20104. LinesToStr([ // statements
  20105. 'this.a = null;',
  20106. 'this.value = 0;',
  20107. '']),
  20108. LinesToStr([ // $mod.$main
  20109. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20110. ' $mod.value = $in[$l];',
  20111. ' if ($mod.value === 3) ;',
  20112. '};',
  20113. '']));
  20114. end;
  20115. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20116. begin
  20117. AddModuleWithIntfImplSrc('unit2.pas',
  20118. LinesToStr([
  20119. '{$modeswitch externalclass}',
  20120. 'type',
  20121. ' TJSBufferSource = class external name ''BufferSource''',
  20122. ' end;',
  20123. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20124. '']),
  20125. '');
  20126. AddModuleWithIntfImplSrc('unit3.pas',
  20127. LinesToStr([
  20128. '{$modeswitch externalclass}',
  20129. 'type',
  20130. ' TJSBufferSource = class external name ''BufferSource''',
  20131. ' end;',
  20132. '']),
  20133. '');
  20134. StartUnit(true);
  20135. Add([
  20136. 'interface',
  20137. 'uses unit2, unit3;',
  20138. 'procedure DoSome(s: TJSBufferSource);',
  20139. 'implementation',
  20140. 'procedure DoSome(s: TJSBufferSource);',
  20141. 'begin',
  20142. ' DoIt(s);',
  20143. 'end;',
  20144. '']);
  20145. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20146. nIncompatibleTypeArgNo);
  20147. ConvertUnit;
  20148. end;
  20149. procedure TTestModule.TestExternalClass_NestedConstructor;
  20150. begin
  20151. StartProgram(false);
  20152. Add([
  20153. '{$modeswitch externalclass}',
  20154. 'type',
  20155. ' TJSObject = class external name ''Object''',
  20156. ' type TBird = class external name ''Bird''',
  20157. ' type TWing = class external name ''Wing''',
  20158. ' constructor New;',
  20159. ' constructor Create(w: word = 3);',
  20160. ' end;',
  20161. ' end;',
  20162. ' end;',
  20163. 'var',
  20164. ' w: TJSObject.TBird.TWing;',
  20165. 'begin',
  20166. ' w:=tjsobject.tbird.twing.new;',
  20167. ' w:=tjsobject.tbird.twing.new();',
  20168. ' w:=tjsobject.tbird.twing.create;',
  20169. ' w:=tjsobject.tbird.twing.create(4);',
  20170. ' with tjsobject do begin',
  20171. ' w:=tbird.twing.new;',
  20172. ' w:=tbird.twing.new();',
  20173. ' w:=tbird.twing.create;',
  20174. ' w:=tbird.twing.create(11);',
  20175. ' end;',
  20176. ' with tjsobject.tbird do begin',
  20177. ' w:=twing.new;',
  20178. ' w:=twing.new();',
  20179. ' w:=twing.create;',
  20180. ' w:=twing.create(21);',
  20181. ' end;',
  20182. ' with tjsobject.tbird.twing do begin',
  20183. ' w:=new;',
  20184. ' w:=new();',
  20185. ' w:=create;',
  20186. ' w:=create(31);',
  20187. ' end;',
  20188. '']);
  20189. ConvertProgram;
  20190. CheckSource('TestExternalClass_NestedConstructor',
  20191. LinesToStr([ // statements
  20192. 'this.w = null;',
  20193. '']),
  20194. LinesToStr([ // $mod.$main
  20195. '$mod.w = new Object.Bird.Wing();',
  20196. '$mod.w = new Object.Bird.Wing();',
  20197. '$mod.w = new Object.Bird.Wing.Create();',
  20198. '$mod.w = new Object.Bird.Wing.Create(4);',
  20199. '$mod.w = new Object.Bird.Wing();',
  20200. '$mod.w = new Object.Bird.Wing();',
  20201. '$mod.w = new Object.Bird.Wing.Create();',
  20202. '$mod.w = new Object.Bird.Wing.Create(11);',
  20203. 'var $with = Object.Bird;',
  20204. '$mod.w = new Object.Bird.Wing();',
  20205. '$mod.w = new Object.Bird.Wing();',
  20206. '$mod.w = new Object.Bird.Wing.Create();',
  20207. '$mod.w = new Object.Bird.Wing.Create(21);',
  20208. 'var $with1 = Object.Bird.Wing;',
  20209. '$mod.w = new $with1();',
  20210. '$mod.w = new $with1();',
  20211. '$mod.w = new Object.Bird.Wing.Create();',
  20212. '$mod.w = new Object.Bird.Wing.Create(31);',
  20213. '']));
  20214. end;
  20215. procedure TTestModule.TestClassInterface_Corba;
  20216. begin
  20217. StartProgram(false);
  20218. Add([
  20219. '{$interfaces corba}',
  20220. 'type',
  20221. ' IUnknown = interface;',
  20222. ' IUnknown = interface',
  20223. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20224. ' end;',
  20225. ' IInterface = IUnknown;',
  20226. ' IBird = interface(IInterface)',
  20227. ' function GetSize: longint;',
  20228. ' procedure SetSize(i: longint);',
  20229. ' property Size: longint read GetSize write SetSize;',
  20230. ' procedure DoIt(i: longint);',
  20231. ' end;',
  20232. ' TObject = class',
  20233. ' end;',
  20234. ' TBird = class(TObject,IBird)',
  20235. ' function GetSize: longint; virtual; abstract;',
  20236. ' procedure SetSize(i: longint); virtual; abstract;',
  20237. ' procedure DoIt(i: longint); virtual; abstract;',
  20238. ' end;',
  20239. 'var',
  20240. ' BirdIntf: IBird;',
  20241. 'begin',
  20242. ' BirdIntf.Size:=BirdIntf.Size;',
  20243. '']);
  20244. ConvertProgram;
  20245. CheckSource('TestClassInterface_Corba',
  20246. LinesToStr([ // statements
  20247. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20248. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20249. 'rtl.createClass(this, "TObject", null, function () {',
  20250. ' this.$init = function () {',
  20251. ' };',
  20252. ' this.$final = function () {',
  20253. ' };',
  20254. '});',
  20255. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20256. ' rtl.addIntf(this, $mod.IBird);',
  20257. '});',
  20258. 'this.BirdIntf = null;',
  20259. '']),
  20260. LinesToStr([ // $mod.$main
  20261. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20262. '']));
  20263. end;
  20264. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20265. begin
  20266. StartProgram(false);
  20267. Add([
  20268. '{$interfaces corba}',
  20269. 'type',
  20270. ' IUnknown = interface',
  20271. ' procedure DoIt; external name ''foo'';',
  20272. ' end;',
  20273. 'begin']);
  20274. SetExpectedParserError(
  20275. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20276. nParserNoFieldsAllowed);
  20277. ConvertProgram;
  20278. end;
  20279. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20280. begin
  20281. StartProgram(false);
  20282. Add([
  20283. '{$interfaces corba}',
  20284. 'type',
  20285. ' integer = longint;',
  20286. ' IUnknown = interface',
  20287. ' procedure DoIt(i: integer);',
  20288. ' procedure DoIt(s: string);',
  20289. ' end;',
  20290. ' IBird = interface(IUnknown)',
  20291. ' procedure DoIt(b: boolean); overload;',
  20292. ' end;',
  20293. ' TObject = class',
  20294. ' end;',
  20295. ' TBird = class(TObject,IBird)',
  20296. ' procedure DoIt(o: TObject);',
  20297. ' procedure DoIt(s: string);',
  20298. ' procedure DoIt(i: integer);',
  20299. ' procedure DoIt(b: boolean);',
  20300. ' end;',
  20301. 'procedure TBird.DoIt(o: TObject); begin end;',
  20302. 'procedure TBird.DoIt(s: string); begin end;',
  20303. 'procedure TBird.DoIt(i: integer); begin end;',
  20304. 'procedure TBird.DoIt(b: boolean); begin end;',
  20305. 'var',
  20306. ' BirdIntf: IBird;',
  20307. 'begin',
  20308. ' BirdIntf.DoIt(3);',
  20309. ' BirdIntf.DoIt(''abc'');',
  20310. ' BirdIntf.DoIt(true);',
  20311. '']);
  20312. ConvertProgram;
  20313. CheckSource('TestClassInterface_Overloads',
  20314. LinesToStr([ // statements
  20315. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20316. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20317. 'rtl.createClass(this, "TObject", null, function () {',
  20318. ' this.$init = function () {',
  20319. ' };',
  20320. ' this.$final = function () {',
  20321. ' };',
  20322. '});',
  20323. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20324. ' this.DoIt = function (o) {',
  20325. ' };',
  20326. ' this.DoIt$1 = function (s) {',
  20327. ' };',
  20328. ' this.DoIt$2 = function (i) {',
  20329. ' };',
  20330. ' this.DoIt$3 = function (b) {',
  20331. ' };',
  20332. ' rtl.addIntf(this, $mod.IBird, {',
  20333. ' DoIt$2: "DoIt$3",',
  20334. ' DoIt: "DoIt$2"',
  20335. ' });',
  20336. '});',
  20337. 'this.BirdIntf = null;',
  20338. '']),
  20339. LinesToStr([ // $mod.$main
  20340. '$mod.BirdIntf.DoIt(3);',
  20341. '$mod.BirdIntf.DoIt$1("abc");',
  20342. '$mod.BirdIntf.DoIt$2(true);',
  20343. '']));
  20344. end;
  20345. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20346. begin
  20347. StartProgram(false);
  20348. Add([
  20349. '{$interfaces corba}',
  20350. 'type',
  20351. ' IBird = interface',
  20352. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20353. ' end;',
  20354. ' IDog = interface',
  20355. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20356. ' end;',
  20357. ' TObject = class(IBird,IDog)',
  20358. ' end;',
  20359. 'begin']);
  20360. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20361. nDuplicateGUIDXInYZ);
  20362. ConvertProgram;
  20363. end;
  20364. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20365. begin
  20366. StartProgram(false);
  20367. Add([
  20368. '{$interfaces corba}',
  20369. 'type',
  20370. ' IAnimal = interface',
  20371. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20372. ' end;',
  20373. ' IBird = interface(IAnimal)',
  20374. ' end;',
  20375. ' IHawk = interface(IBird)',
  20376. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20377. ' end;',
  20378. 'begin']);
  20379. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20380. nDuplicateGUIDXInYZ);
  20381. ConvertProgram;
  20382. end;
  20383. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20384. begin
  20385. StartProgram(false);
  20386. Add([
  20387. '{$interfaces corba}',
  20388. 'type',
  20389. ' integer = longint;',
  20390. ' IUnknown = interface',
  20391. ' procedure DoIt(i: integer);',
  20392. ' end;',
  20393. ' IBird = interface',
  20394. ' procedure Fly(i: integer);',
  20395. ' end;',
  20396. ' TObject = class(IUnknown)',
  20397. ' procedure DoIt(i: integer);',
  20398. ' end;',
  20399. ' TBird = class(IBird)',
  20400. ' procedure Fly(i: integer);',
  20401. ' end;',
  20402. 'procedure TObject.DoIt(i: integer); begin end;',
  20403. 'procedure TBird.Fly(i: integer); begin end;',
  20404. 'begin',
  20405. '']);
  20406. ConvertProgram;
  20407. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20408. LinesToStr([ // statements
  20409. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20410. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20411. 'rtl.createClass(this, "TObject", null, function () {',
  20412. ' this.$init = function () {',
  20413. ' };',
  20414. ' this.$final = function () {',
  20415. ' };',
  20416. ' this.DoIt = function (i) {',
  20417. ' };',
  20418. ' rtl.addIntf(this, $mod.IUnknown);',
  20419. '});',
  20420. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20421. ' this.Fly = function (i) {',
  20422. ' };',
  20423. ' rtl.addIntf(this, $mod.IBird);',
  20424. ' rtl.addIntf(this, $mod.IUnknown);',
  20425. '});',
  20426. '']),
  20427. LinesToStr([ // $mod.$main
  20428. '']));
  20429. end;
  20430. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20431. begin
  20432. StartProgram(false);
  20433. Add([
  20434. '{$interfaces corba}',
  20435. 'type',
  20436. ' integer = longint;',
  20437. ' IBird = interface',
  20438. ' procedure DoIt(i: integer);',
  20439. ' end;',
  20440. ' TObject = class',
  20441. ' procedure DoIt(i: integer);',
  20442. ' end;',
  20443. ' TBird = class(IBird)',
  20444. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20445. ' end;',
  20446. 'procedure TObject.DoIt(i: integer); begin end;',
  20447. 'procedure TBird.DoIt(i: integer); begin end;',
  20448. 'begin',
  20449. '']);
  20450. ConvertProgram;
  20451. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20452. LinesToStr([ // statements
  20453. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20454. 'rtl.createClass(this, "TObject", null, function () {',
  20455. ' this.$init = function () {',
  20456. ' };',
  20457. ' this.$final = function () {',
  20458. ' };',
  20459. ' this.DoIt = function (i) {',
  20460. ' };',
  20461. '});',
  20462. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20463. ' this.DoIt$1 = function (i) {',
  20464. ' };',
  20465. ' rtl.addIntf(this, $mod.IBird, {',
  20466. ' DoIt: "DoIt$1"',
  20467. ' });',
  20468. '});',
  20469. '']),
  20470. LinesToStr([ // $mod.$main
  20471. '']));
  20472. end;
  20473. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20474. begin
  20475. StartProgram(false);
  20476. Add([
  20477. '{$interfaces corba}',
  20478. 'type',
  20479. ' IUnknown = interface',
  20480. ' procedure Walk(i: longint);',
  20481. ' end;',
  20482. ' IBird = interface(IUnknown)',
  20483. ' procedure Walk(b: boolean); overload;',
  20484. ' procedure Fly(s: string);',
  20485. ' end;',
  20486. ' TObject = class',
  20487. ' end;',
  20488. ' TBird = class(TObject,IBird)',
  20489. ' procedure IBird.Fly = Move;',
  20490. ' procedure IBird.Walk = Hop;',
  20491. ' procedure Hop(i: longint);',
  20492. ' procedure Move(s: string);',
  20493. ' procedure Hop(b: boolean);',
  20494. ' end;',
  20495. 'procedure TBird.Move(s: string); begin end;',
  20496. 'procedure TBird.Hop(i: longint); begin end;',
  20497. 'procedure TBird.Hop(b: boolean); begin end;',
  20498. 'var',
  20499. ' BirdIntf: IBird;',
  20500. 'begin',
  20501. ' BirdIntf.Walk(3);',
  20502. ' BirdIntf.Walk(true);',
  20503. ' BirdIntf.Fly(''abc'');',
  20504. '']);
  20505. ConvertProgram;
  20506. CheckSource('TestClassInterface_Corba_MethodResolution',
  20507. LinesToStr([ // statements
  20508. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20509. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20510. 'rtl.createClass(this, "TObject", null, function () {',
  20511. ' this.$init = function () {',
  20512. ' };',
  20513. ' this.$final = function () {',
  20514. ' };',
  20515. '});',
  20516. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20517. ' this.Hop = function (i) {',
  20518. ' };',
  20519. ' this.Move = function (s) {',
  20520. ' };',
  20521. ' this.Hop$1 = function (b) {',
  20522. ' };',
  20523. ' rtl.addIntf(this, $mod.IBird, {',
  20524. ' Walk$1: "Hop$1",',
  20525. ' Fly: "Move",',
  20526. ' Walk: "Hop"',
  20527. ' });',
  20528. '});',
  20529. 'this.BirdIntf = null;',
  20530. '']),
  20531. LinesToStr([ // $mod.$main
  20532. '$mod.BirdIntf.Walk(3);',
  20533. '$mod.BirdIntf.Walk$1(true);',
  20534. '$mod.BirdIntf.Fly("abc");',
  20535. '']));
  20536. end;
  20537. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20538. begin
  20539. StartProgram(false);
  20540. Add([
  20541. '{$interfaces com}',
  20542. 'type',
  20543. ' IUnknown = interface',
  20544. ' function _AddRef: longint;',
  20545. ' procedure Walk;',
  20546. ' end;',
  20547. ' IBird = interface end;',
  20548. ' IDog = interface end;',
  20549. ' TObject = class(IBird,IDog)',
  20550. ' function _AddRef: longint; virtual; abstract;',
  20551. ' procedure Walk; virtual; abstract;',
  20552. ' end;',
  20553. ' TBird = class(IUnknown)',
  20554. ' end;',
  20555. 'begin',
  20556. '']);
  20557. ConvertProgram;
  20558. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20559. LinesToStr([ // statements
  20560. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20561. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20562. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20563. 'rtl.createClass(this, "TObject", null, function () {',
  20564. ' this.$init = function () {',
  20565. ' };',
  20566. ' this.$final = function () {',
  20567. ' };',
  20568. ' rtl.addIntf(this, $mod.IBird);',
  20569. ' rtl.addIntf(this, $mod.IDog);',
  20570. '});',
  20571. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20572. ' rtl.addIntf(this, $mod.IUnknown);',
  20573. ' rtl.addIntf(this, $mod.IBird);',
  20574. ' rtl.addIntf(this, $mod.IDog);',
  20575. '});',
  20576. '']),
  20577. LinesToStr([ // $mod.$main
  20578. '']));
  20579. end;
  20580. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20581. begin
  20582. StartProgram(false);
  20583. Add([
  20584. '{$interfaces corba}',
  20585. 'type',
  20586. ' IUnknown = interface',
  20587. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20588. ' procedure Go;',
  20589. ' end;',
  20590. ' TObject = class(IUnknown)',
  20591. ' procedure Go; virtual; abstract;',
  20592. ' end;',
  20593. ' TBird = class',
  20594. ' procedure Go; override;',
  20595. ' end;',
  20596. ' TCat = class(TObject)',
  20597. ' procedure Go; override;',
  20598. ' end;',
  20599. ' TDog = class(TObject, IUnknown)',
  20600. ' procedure Go; override;',
  20601. ' end;',
  20602. 'procedure TBird.Go; begin end;',
  20603. 'procedure TCat.Go; begin end;',
  20604. 'procedure TDog.Go; begin end;',
  20605. 'begin',
  20606. '']);
  20607. ConvertProgram;
  20608. CheckSource('TestClassInterface_Corba_MethodOverride',
  20609. LinesToStr([ // statements
  20610. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20611. 'rtl.createClass(this, "TObject", null, function () {',
  20612. ' this.$init = function () {',
  20613. ' };',
  20614. ' this.$final = function () {',
  20615. ' };',
  20616. ' rtl.addIntf(this, $mod.IUnknown);',
  20617. '});',
  20618. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20619. ' this.Go = function () {',
  20620. ' };',
  20621. ' rtl.addIntf(this, $mod.IUnknown);',
  20622. '});',
  20623. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20624. ' this.Go = function () {',
  20625. ' };',
  20626. ' rtl.addIntf(this, $mod.IUnknown);',
  20627. '});',
  20628. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20629. ' this.Go = function () {',
  20630. ' };',
  20631. ' rtl.addIntf(this, $mod.IUnknown);',
  20632. '});',
  20633. '']),
  20634. LinesToStr([ // $mod.$main
  20635. '']));
  20636. end;
  20637. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20638. begin
  20639. StartProgram(false);
  20640. Add([
  20641. '{$interfaces corba}',
  20642. 'type',
  20643. ' IUnknown = interface',
  20644. ' end;',
  20645. ' IBird = interface(IUnknown)',
  20646. ' procedure Fly(s: string);',
  20647. ' end;',
  20648. ' IEagle = interface(IBird)',
  20649. ' end;',
  20650. ' IDove = interface(IBird)',
  20651. ' end;',
  20652. ' ISwallow = interface(IBird)',
  20653. ' end;',
  20654. ' TObject = class',
  20655. ' end;',
  20656. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20657. ' procedure Fly(s: string); virtual; abstract;',
  20658. ' end;',
  20659. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20660. ' FBirdIntf: IBird;',
  20661. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20662. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20663. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20664. ' FDoveObj: TBird;',
  20665. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20666. ' function GetSwallowObj: TBird; virtual; abstract;',
  20667. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20668. ' end;',
  20669. 'begin',
  20670. '']);
  20671. ConvertProgram;
  20672. CheckSource('TestClassInterface_Corba_Delegation',
  20673. LinesToStr([ // statements
  20674. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20675. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20676. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20677. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20678. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20679. 'rtl.createClass(this, "TObject", null, function () {',
  20680. ' this.$init = function () {',
  20681. ' };',
  20682. ' this.$final = function () {',
  20683. ' };',
  20684. '});',
  20685. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20686. ' rtl.addIntf(this, $mod.IBird);',
  20687. ' rtl.addIntf(this, $mod.IEagle);',
  20688. ' rtl.addIntf(this, $mod.IDove);',
  20689. ' rtl.addIntf(this, $mod.ISwallow);',
  20690. '});',
  20691. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20692. ' this.$init = function () {',
  20693. ' $mod.TObject.$init.call(this);',
  20694. ' this.FBirdIntf = null;',
  20695. ' this.FDoveObj = null;',
  20696. ' };',
  20697. ' this.$final = function () {',
  20698. ' this.FBirdIntf = undefined;',
  20699. ' this.FDoveObj = undefined;',
  20700. ' $mod.TObject.$final.call(this);',
  20701. ' };',
  20702. ' this.$intfmaps = {',
  20703. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20704. ' return this.FBirdIntf;',
  20705. ' },',
  20706. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20707. ' return this.GetEagleIntf();',
  20708. ' },',
  20709. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20710. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20711. ' },',
  20712. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20713. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20714. ' }',
  20715. ' };',
  20716. '});',
  20717. '']),
  20718. LinesToStr([ // $mod.$main
  20719. '']));
  20720. end;
  20721. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20722. begin
  20723. StartProgram(false);
  20724. Add([
  20725. '{$interfaces corba}',
  20726. 'type',
  20727. ' IUnknown = interface',
  20728. ' end;',
  20729. ' IBird = interface(IUnknown)',
  20730. ' procedure Fly(s: string);',
  20731. ' end;',
  20732. ' IEagle = interface(IBird)',
  20733. ' end;',
  20734. ' IDove = interface(IBird)',
  20735. ' end;',
  20736. ' ISwallow = interface(IBird)',
  20737. ' end;',
  20738. ' TObject = class',
  20739. ' end;',
  20740. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20741. ' procedure Fly(s: string); virtual; abstract;',
  20742. ' end;',
  20743. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20744. ' private',
  20745. ' class var FBirdIntf: IBird;',
  20746. ' class var FDoveObj: TBird;',
  20747. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20748. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20749. ' protected',
  20750. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20751. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20752. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20753. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20754. ' end;',
  20755. 'begin',
  20756. '']);
  20757. ConvertProgram;
  20758. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20759. LinesToStr([ // statements
  20760. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20761. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20762. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20763. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20764. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20765. 'rtl.createClass(this, "TObject", null, function () {',
  20766. ' this.$init = function () {',
  20767. ' };',
  20768. ' this.$final = function () {',
  20769. ' };',
  20770. '});',
  20771. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20772. ' rtl.addIntf(this, $mod.IBird);',
  20773. ' rtl.addIntf(this, $mod.IEagle);',
  20774. ' rtl.addIntf(this, $mod.IDove);',
  20775. ' rtl.addIntf(this, $mod.ISwallow);',
  20776. '});',
  20777. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20778. ' this.FBirdIntf = null;',
  20779. ' this.FDoveObj = null;',
  20780. ' this.$intfmaps = {',
  20781. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20782. ' return this.FBirdIntf;',
  20783. ' },',
  20784. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20785. ' return this.GetEagleIntf();',
  20786. ' },',
  20787. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20788. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20789. ' },',
  20790. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20791. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20792. ' }',
  20793. ' };',
  20794. '});',
  20795. '']),
  20796. LinesToStr([ // $mod.$main
  20797. '']));
  20798. end;
  20799. procedure TTestModule.TestClassInterface_Corba_Operators;
  20800. begin
  20801. StartProgram(false);
  20802. Add([
  20803. '{$interfaces corba}',
  20804. 'type',
  20805. ' IUnknown = interface',
  20806. ' end;',
  20807. ' IBird = interface(IUnknown)',
  20808. ' function GetItems(Index: longint): longint;',
  20809. ' procedure SetItems(Index: longint; Value: longint);',
  20810. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20811. ' end;',
  20812. ' TObject = class',
  20813. ' end;',
  20814. ' TBird = class(TObject,IBird)',
  20815. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20816. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20817. ' end;',
  20818. 'var',
  20819. ' IntfVar: IBird = nil;',
  20820. ' IntfVar2: IBird;',
  20821. ' ObjVar: TBird;',
  20822. ' v: JSValue;',
  20823. 'begin',
  20824. ' IntfVar:=nil;',
  20825. ' IntfVar[3]:=IntfVar[4];',
  20826. ' if Assigned(IntfVar) then ;',
  20827. ' IntfVar:=IntfVar2;',
  20828. ' IntfVar:=ObjVar;',
  20829. ' if IntfVar=IntfVar2 then ;',
  20830. ' if IntfVar<>IntfVar2 then ;',
  20831. ' if IntfVar is IBird then ;',
  20832. ' if IntfVar is TBird then ;',
  20833. ' if ObjVar is IBird then ;',
  20834. ' IntfVar:=IntfVar2 as IBird;',
  20835. ' ObjVar:=IntfVar2 as TBird;',
  20836. ' IntfVar:=ObjVar as IBird;',
  20837. ' IntfVar:=IBird(IntfVar2);',
  20838. ' ObjVar:=TBird(IntfVar);',
  20839. ' IntfVar:=IBird(ObjVar);',
  20840. ' v:=IntfVar;',
  20841. ' IntfVar:=IBird(v);',
  20842. ' if v is IBird then ;',
  20843. ' v:=JSValue(IntfVar);',
  20844. ' v:=IBird;',
  20845. '']);
  20846. ConvertProgram;
  20847. CheckSource('TestClassInterface_Corba_Operators',
  20848. LinesToStr([ // statements
  20849. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20850. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20851. 'rtl.createClass(this, "TObject", null, function () {',
  20852. ' this.$init = function () {',
  20853. ' };',
  20854. ' this.$final = function () {',
  20855. ' };',
  20856. '});',
  20857. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20858. ' rtl.addIntf(this, $mod.IBird);',
  20859. '});',
  20860. 'this.IntfVar = null;',
  20861. 'this.IntfVar2 = null;',
  20862. 'this.ObjVar = null;',
  20863. 'this.v = undefined;',
  20864. '']),
  20865. LinesToStr([ // $mod.$main
  20866. '$mod.IntfVar = null;',
  20867. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20868. 'if ($mod.IntfVar != null) ;',
  20869. '$mod.IntfVar = $mod.IntfVar2;',
  20870. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20871. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20872. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20873. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20874. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20875. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20876. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20877. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20878. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20879. '$mod.IntfVar = $mod.IntfVar2;',
  20880. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20881. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20882. '$mod.v = $mod.IntfVar;',
  20883. '$mod.IntfVar = rtl.getObject($mod.v);',
  20884. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20885. '$mod.v = $mod.IntfVar;',
  20886. '$mod.v = $mod.IBird;',
  20887. '']));
  20888. end;
  20889. procedure TTestModule.TestClassInterface_Corba_Args;
  20890. begin
  20891. StartProgram(false);
  20892. Add([
  20893. '{$interfaces corba}',
  20894. 'type',
  20895. ' IUnknown = interface',
  20896. ' end;',
  20897. ' IBird = interface(IUnknown)',
  20898. ' end;',
  20899. ' TObject = class',
  20900. ' end;',
  20901. ' TBird = class(TObject,IBird)',
  20902. ' end;',
  20903. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  20904. 'begin',
  20905. ' DoIt(i,i,i);',
  20906. 'end;',
  20907. 'procedure Change(var i: IBird; out j: IBird);',
  20908. 'begin',
  20909. ' DoIt(i,i,i);',
  20910. ' Change(i,i);',
  20911. 'end;',
  20912. 'var',
  20913. ' i: IBird;',
  20914. ' o: TBird;',
  20915. 'begin',
  20916. ' DoIt(i,i,i);',
  20917. ' Change(i,i);',
  20918. ' DoIt(o,o,o);',
  20919. '']);
  20920. ConvertProgram;
  20921. CheckSource('TestClassInterface_Corba_Args',
  20922. LinesToStr([ // statements
  20923. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20924. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  20925. 'rtl.createClass(this, "TObject", null, function () {',
  20926. ' this.$init = function () {',
  20927. ' };',
  20928. ' this.$final = function () {',
  20929. ' };',
  20930. '});',
  20931. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20932. ' rtl.addIntf(this, $mod.IBird);',
  20933. '});',
  20934. 'this.DoIt = function (u, i, j) {',
  20935. ' $mod.DoIt({',
  20936. ' get: function () {',
  20937. ' return i;',
  20938. ' },',
  20939. ' set: function (v) {',
  20940. ' i = v;',
  20941. ' }',
  20942. ' }, i, i);',
  20943. '};',
  20944. 'this.Change = function (i, j) {',
  20945. ' $mod.DoIt(i, i.get(), i.get());',
  20946. ' $mod.Change(i, i);',
  20947. '};',
  20948. 'this.i = null;',
  20949. 'this.o = null;',
  20950. '']),
  20951. LinesToStr([ // $mod.$main
  20952. '$mod.DoIt({',
  20953. ' p: $mod,',
  20954. ' get: function () {',
  20955. ' return this.p.i;',
  20956. ' },',
  20957. ' set: function (v) {',
  20958. ' this.p.i = v;',
  20959. ' }',
  20960. '}, $mod.i, $mod.i);',
  20961. '$mod.Change({',
  20962. ' p: $mod,',
  20963. ' get: function () {',
  20964. ' return this.p.i;',
  20965. ' },',
  20966. ' set: function (v) {',
  20967. ' this.p.i = v;',
  20968. ' }',
  20969. '}, {',
  20970. ' p: $mod,',
  20971. ' get: function () {',
  20972. ' return this.p.i;',
  20973. ' },',
  20974. ' set: function (v) {',
  20975. ' this.p.i = v;',
  20976. ' }',
  20977. '});',
  20978. '$mod.DoIt({',
  20979. ' p: $mod,',
  20980. ' get: function () {',
  20981. ' return this.p.o;',
  20982. ' },',
  20983. ' set: function (v) {',
  20984. ' this.p.o = v;',
  20985. ' }',
  20986. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  20987. '']));
  20988. end;
  20989. procedure TTestModule.TestClassInterface_Corba_ForIn;
  20990. begin
  20991. StartProgram(false);
  20992. Add([
  20993. '{$interfaces corba}',
  20994. 'type',
  20995. ' IUnknown = interface end;',
  20996. ' TObject = class',
  20997. ' Id: longint;',
  20998. ' end;',
  20999. ' IEnumerator = interface(IUnknown)',
  21000. ' function GetCurrent: TObject;',
  21001. ' function MoveNext: Boolean;',
  21002. ' property Current: TObject read GetCurrent;',
  21003. ' end;',
  21004. ' IEnumerable = interface(IUnknown)',
  21005. ' function GetEnumerator: IEnumerator;',
  21006. ' end;',
  21007. 'var',
  21008. ' o: TObject;',
  21009. ' i: IEnumerable;',
  21010. 'begin',
  21011. ' for o in i do o.Id:=3;',
  21012. '']);
  21013. ConvertProgram;
  21014. CheckSource('TestClassInterface_Corba_ForIn',
  21015. LinesToStr([ // statements
  21016. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21017. 'rtl.createClass(this, "TObject", null, function () {',
  21018. ' this.$init = function () {',
  21019. ' this.Id = 0;',
  21020. ' };',
  21021. ' this.$final = function () {',
  21022. ' };',
  21023. '});',
  21024. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21025. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21026. 'this.o = null;',
  21027. 'this.i = null;',
  21028. '']),
  21029. LinesToStr([ // $mod.$main
  21030. 'var $in = $mod.i.GetEnumerator();',
  21031. 'while ($in.MoveNext()) {',
  21032. ' $mod.o = $in.GetCurrent();',
  21033. ' $mod.o.Id = 3;',
  21034. '};',
  21035. '']));
  21036. end;
  21037. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21038. begin
  21039. StartProgram(false);
  21040. Add([
  21041. '{$interfaces corba}',
  21042. 'type',
  21043. ' IUnknown = interface end;',
  21044. ' IBird = interface(IUnknown)',
  21045. ' function Fly(w: word): word;',
  21046. ' end;',
  21047. ' TBirdArray = array of IBird;',
  21048. 'var',
  21049. ' i: IBird;',
  21050. ' a: TBirdArray;',
  21051. 'begin',
  21052. ' SetLength(a,3);',
  21053. ' i:=a[1];',
  21054. ' a[2]:=i;',
  21055. ' for i in a do i.fly(3);',
  21056. '']);
  21057. ConvertProgram;
  21058. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21059. LinesToStr([ // statements
  21060. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21061. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21062. 'this.i = null;',
  21063. 'this.a = [];',
  21064. '']),
  21065. LinesToStr([ // $mod.$main
  21066. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21067. '$mod.i = $mod.a[1];',
  21068. '$mod.a[2] = $mod.i;',
  21069. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21070. ' $mod.i = $in[$l];',
  21071. ' $mod.i.Fly(3);',
  21072. '};',
  21073. '']));
  21074. end;
  21075. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21076. begin
  21077. StartProgram(false);
  21078. Add([
  21079. '{$interfaces com}',
  21080. 'type',
  21081. ' IUnknown = interface',
  21082. ' function _AddRef: longint;',
  21083. ' function _Release: longint;',
  21084. ' end;',
  21085. ' TObject = class(IUnknown)',
  21086. ' function _AddRef: longint; virtual; abstract;',
  21087. ' function _Release: longint; virtual; abstract;',
  21088. ' end;',
  21089. 'var',
  21090. ' i: IUnknown;',
  21091. 'procedure DoGlobal(o: TObject);',
  21092. 'begin',
  21093. ' i:=nil;',
  21094. ' i:=o;',
  21095. ' i:=i;',
  21096. 'end;',
  21097. 'procedure DoLocal(o: TObject);',
  21098. 'const k: IUnknown = nil;',
  21099. 'var j: IUnknown;',
  21100. 'begin',
  21101. ' k:=o;',
  21102. ' k:=i;',
  21103. ' j:=o;',
  21104. ' j:=i;',
  21105. 'end;',
  21106. 'var o: TObject;',
  21107. 'begin',
  21108. ' i:=nil;',
  21109. ' i:=o;',
  21110. '']);
  21111. ConvertProgram;
  21112. CheckSource('TestClassInterface_COM_AssignVar',
  21113. LinesToStr([ // statements
  21114. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21115. 'rtl.createClass(this, "TObject", null, function () {',
  21116. ' this.$init = function () {',
  21117. ' };',
  21118. ' this.$final = function () {',
  21119. ' };',
  21120. ' rtl.addIntf(this, $mod.IUnknown);',
  21121. '});',
  21122. 'this.i = null;',
  21123. 'this.DoGlobal = function (o) {',
  21124. ' rtl.setIntfP($mod, "i", null);',
  21125. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21126. ' rtl.setIntfP($mod, "i", $mod.i);',
  21127. '};',
  21128. 'var k = null;',
  21129. 'this.DoLocal = function (o) {',
  21130. ' var j = null;',
  21131. ' try{',
  21132. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21133. ' k = rtl.setIntfL(k, $mod.i);',
  21134. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21135. ' j = rtl.setIntfL(j, $mod.i);',
  21136. ' }finally{',
  21137. ' rtl._Release(j);',
  21138. ' };',
  21139. '};',
  21140. 'this.o = null;',
  21141. '']),
  21142. LinesToStr([ // $mod.$main
  21143. 'rtl.setIntfP($mod, "i", null);',
  21144. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21145. '']));
  21146. end;
  21147. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21148. begin
  21149. StartProgram(false);
  21150. Add([
  21151. '{$interfaces com}',
  21152. 'type',
  21153. ' IUnknown = interface',
  21154. ' function _AddRef: longint;',
  21155. ' function _Release: longint;',
  21156. ' end;',
  21157. ' TObject = class(IUnknown)',
  21158. ' function _AddRef: longint; virtual; abstract;',
  21159. ' function _Release: longint; virtual; abstract;',
  21160. ' end;',
  21161. 'procedure DoDefault(i, j: IUnknown);',
  21162. 'begin',
  21163. ' i:=nil;',
  21164. ' i:=j;',
  21165. 'end;',
  21166. 'begin',
  21167. '']);
  21168. ConvertProgram;
  21169. CheckSource('TestClassInterface_COM_AssignArg',
  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.DoDefault = function (i, j) {',
  21180. ' rtl._AddRef(i);',
  21181. ' try {',
  21182. ' i = rtl.setIntfL(i, null);',
  21183. ' i = rtl.setIntfL(i, j);',
  21184. ' } finally {',
  21185. ' rtl._Release(i);',
  21186. ' };',
  21187. '};',
  21188. '']),
  21189. LinesToStr([ // $mod.$main
  21190. '']));
  21191. end;
  21192. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21193. begin
  21194. StartProgram(false);
  21195. Add([
  21196. '{$interfaces com}',
  21197. 'type',
  21198. ' IUnknown = interface',
  21199. ' function _AddRef: longint;',
  21200. ' function _Release: longint;',
  21201. ' end;',
  21202. ' TObject = class(IUnknown)',
  21203. ' function _AddRef: longint; virtual; abstract;',
  21204. ' function _Release: longint; virtual; abstract;',
  21205. ' end;',
  21206. 'function DoDefault(i: IUnknown): IUnknown;',
  21207. 'begin',
  21208. ' Result:=i;',
  21209. ' if Result<>nil then exit;',
  21210. 'end;',
  21211. 'begin',
  21212. '']);
  21213. ConvertProgram;
  21214. CheckSource('TestClassInterface_COM_FunctionResult',
  21215. LinesToStr([ // statements
  21216. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21217. 'rtl.createClass(this, "TObject", null, function () {',
  21218. ' this.$init = function () {',
  21219. ' };',
  21220. ' this.$final = function () {',
  21221. ' };',
  21222. ' rtl.addIntf(this, $mod.IUnknown);',
  21223. '});',
  21224. 'this.DoDefault = function (i) {',
  21225. ' var Result = null;',
  21226. ' var $ok = false;',
  21227. ' try {',
  21228. ' Result = rtl.setIntfL(Result, i);',
  21229. ' if(Result !== null){',
  21230. ' $ok = true;',
  21231. ' return Result;',
  21232. ' };',
  21233. ' $ok = true;',
  21234. ' } finally {',
  21235. ' if(!$ok) rtl._Release(Result);',
  21236. ' };',
  21237. ' return Result;',
  21238. '};',
  21239. '']),
  21240. LinesToStr([ // $mod.$main
  21241. '']));
  21242. end;
  21243. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21244. begin
  21245. StartProgram(false);
  21246. Add([
  21247. '{$interfaces com}',
  21248. 'type',
  21249. ' IUnknown = interface',
  21250. ' function _AddRef: longint;',
  21251. ' function _Release: longint;',
  21252. ' end;',
  21253. ' TObject = class(IUnknown)',
  21254. ' function _AddRef: longint; virtual; abstract;',
  21255. ' function _Release: longint; virtual; abstract;',
  21256. ' function GetIntf: IUnknown; virtual;',
  21257. ' end;',
  21258. ' TMouse = class',
  21259. ' function GetIntf: IUnknown; override;',
  21260. ' end;',
  21261. 'function TObject.GetIntf: IUnknown; begin end;',
  21262. 'function TMouse.GetIntf: IUnknown;',
  21263. 'var i: IUnknown;',
  21264. 'begin',
  21265. ' inherited;',
  21266. ' inherited GetIntf;',
  21267. ' inherited GetIntf();',
  21268. ' Result:=inherited GetIntf;',
  21269. ' Result:=inherited GetIntf();',
  21270. ' i:=inherited GetIntf;',
  21271. ' i:=inherited GetIntf();',
  21272. 'end;',
  21273. 'begin',
  21274. '']);
  21275. ConvertProgram;
  21276. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21277. LinesToStr([ // statements
  21278. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21279. 'rtl.createClass(this, "TObject", null, function () {',
  21280. ' this.$init = function () {',
  21281. ' };',
  21282. ' this.$final = function () {',
  21283. ' };',
  21284. ' this.GetIntf = function () {',
  21285. ' var Result = null;',
  21286. ' return Result;',
  21287. ' };',
  21288. ' rtl.addIntf(this, $mod.IUnknown);',
  21289. '});',
  21290. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21291. ' this.GetIntf = function () {',
  21292. ' var Result = null;',
  21293. ' var i = null;',
  21294. ' var $ir = rtl.createIntfRefs();',
  21295. ' var $ok = false;',
  21296. ' try {',
  21297. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21298. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21299. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21300. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21301. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21302. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21303. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21304. ' $ok = true;',
  21305. ' } finally {',
  21306. ' $ir.free();',
  21307. ' rtl._Release(i);',
  21308. ' if (!$ok) rtl._Release(Result);',
  21309. ' };',
  21310. ' return Result;',
  21311. ' };',
  21312. ' rtl.addIntf(this, $mod.IUnknown);',
  21313. '});',
  21314. '']),
  21315. LinesToStr([ // $mod.$main
  21316. '']));
  21317. end;
  21318. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21319. begin
  21320. StartProgram(false);
  21321. Add([
  21322. '{$interfaces com}',
  21323. 'type',
  21324. ' IUnknown = interface',
  21325. ' function _AddRef: longint;',
  21326. ' function _Release: longint;',
  21327. ' end;',
  21328. ' TObject = class(IUnknown)',
  21329. ' function _AddRef: longint; virtual; abstract;',
  21330. ' function _Release: longint; virtual; abstract;',
  21331. ' end;',
  21332. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21333. 'begin',
  21334. ' if i is IUnknown then ;',
  21335. ' if o is IUnknown then ;',
  21336. ' if i is TObject then ;',
  21337. ' i:=j as IUnknown;',
  21338. ' i:=o as IUnknown;',
  21339. ' o:=j as TObject;',
  21340. ' i:=IUnknown(j);',
  21341. ' i:=IUnknown(o);',
  21342. ' o:=TObject(i);',
  21343. 'end;',
  21344. 'begin',
  21345. '']);
  21346. ConvertProgram;
  21347. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21348. LinesToStr([ // statements
  21349. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21350. 'rtl.createClass(this, "TObject", null, function () {',
  21351. ' this.$init = function () {',
  21352. ' };',
  21353. ' this.$final = function () {',
  21354. ' };',
  21355. ' rtl.addIntf(this, $mod.IUnknown);',
  21356. '});',
  21357. 'this.DoDefault = function (i, j, o) {',
  21358. ' rtl._AddRef(i);',
  21359. ' try {',
  21360. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21361. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21362. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21363. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21364. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21365. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21366. ' i = rtl.setIntfL(i, j);',
  21367. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21368. ' o = rtl.intfToClass(i, $mod.TObject);',
  21369. ' } finally {',
  21370. ' rtl._Release(i);',
  21371. ' };',
  21372. '};',
  21373. '']),
  21374. LinesToStr([ // $mod.$main
  21375. '']));
  21376. end;
  21377. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21378. begin
  21379. StartProgram(false);
  21380. Add([
  21381. '{$interfaces com}',
  21382. 'type',
  21383. ' IUnknown = interface',
  21384. ' function _AddRef: longint;',
  21385. ' function _Release: longint;',
  21386. ' end;',
  21387. ' TObject = class(IUnknown)',
  21388. ' function _AddRef: longint; virtual; abstract;',
  21389. ' function _Release: longint; virtual; abstract;',
  21390. ' end;',
  21391. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21392. 'var o: TObject;',
  21393. 'begin',
  21394. ' DoIt(v,v,v,v);',
  21395. ' DoIt(o,o,k,k);',
  21396. 'end;',
  21397. 'procedure DoSome;',
  21398. 'var v: IUnknown;',
  21399. 'begin',
  21400. ' DoIt(v,v,v,v);',
  21401. 'end;',
  21402. 'var i: IUnknown;',
  21403. 'begin',
  21404. ' DoIt(i,i,i,i);',
  21405. '']);
  21406. ConvertProgram;
  21407. CheckSource('TestClassInterface_COM_PassAsArg',
  21408. LinesToStr([ // statements
  21409. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21410. 'rtl.createClass(this, "TObject", null, function () {',
  21411. ' this.$init = function () {',
  21412. ' };',
  21413. ' this.$final = function () {',
  21414. ' };',
  21415. ' rtl.addIntf(this, $mod.IUnknown);',
  21416. '});',
  21417. 'this.DoIt = function (v, j, k, l) {',
  21418. ' var o = null;',
  21419. ' var $ir = rtl.createIntfRefs();',
  21420. ' rtl._AddRef(v);',
  21421. ' try {',
  21422. ' $mod.DoIt(v, v, {',
  21423. ' get: function () {',
  21424. ' return v;',
  21425. ' },',
  21426. ' set: function (w) {',
  21427. ' v = rtl.setIntfL(v, w);',
  21428. ' }',
  21429. ' }, {',
  21430. ' get: function () {',
  21431. ' return v;',
  21432. ' },',
  21433. ' set: function (w) {',
  21434. ' v = rtl.setIntfL(v, w);',
  21435. ' }',
  21436. ' });',
  21437. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21438. ' } finally {',
  21439. ' $ir.free();',
  21440. ' rtl._Release(v);',
  21441. ' };',
  21442. '};',
  21443. 'this.DoSome = function () {',
  21444. ' var v = null;',
  21445. ' try {',
  21446. ' $mod.DoIt(v, v, {',
  21447. ' get: function () {',
  21448. ' return v;',
  21449. ' },',
  21450. ' set: function (w) {',
  21451. ' v = rtl.setIntfL(v, w);',
  21452. ' }',
  21453. ' }, {',
  21454. ' get: function () {',
  21455. ' return v;',
  21456. ' },',
  21457. ' set: function (w) {',
  21458. ' v = rtl.setIntfL(v, w);',
  21459. ' }',
  21460. ' });',
  21461. ' } finally {',
  21462. ' rtl._Release(v);',
  21463. ' };',
  21464. '};',
  21465. 'this.i = null;',
  21466. '']),
  21467. LinesToStr([ // $mod.$main
  21468. '$mod.DoIt($mod.i, $mod.i, {',
  21469. ' p: $mod,',
  21470. ' get: function () {',
  21471. ' return this.p.i;',
  21472. ' },',
  21473. ' set: function (v) {',
  21474. ' rtl.setIntfP(this.p, "i", v);',
  21475. ' }',
  21476. '}, {',
  21477. ' p: $mod,',
  21478. ' get: function () {',
  21479. ' return this.p.i;',
  21480. ' },',
  21481. ' set: function (v) {',
  21482. ' rtl.setIntfP(this.p, "i", v);',
  21483. ' }',
  21484. '});',
  21485. '']));
  21486. end;
  21487. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21488. begin
  21489. StartProgram(false);
  21490. Add([
  21491. '{$interfaces com}',
  21492. 'type',
  21493. ' IUnknown = interface',
  21494. ' function _AddRef: longint;',
  21495. ' function _Release: longint;',
  21496. ' end;',
  21497. ' TObject = class(IUnknown)',
  21498. ' function _AddRef: longint; virtual; abstract;',
  21499. ' function _Release: longint; virtual; abstract;',
  21500. ' end;',
  21501. 'procedure DoIt(out i);',
  21502. 'begin end;',
  21503. 'procedure DoSome;',
  21504. 'var v: IUnknown;',
  21505. 'begin',
  21506. ' DoIt(v);',
  21507. 'end;',
  21508. 'function GetIt: IUnknown;',
  21509. 'begin',
  21510. ' DoIt(Result);',
  21511. 'end;',
  21512. 'var i: IUnknown;',
  21513. 'begin',
  21514. ' DoIt(i);',
  21515. '']);
  21516. ConvertProgram;
  21517. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21518. LinesToStr([ // statements
  21519. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21520. 'rtl.createClass(this, "TObject", null, function () {',
  21521. ' this.$init = function () {',
  21522. ' };',
  21523. ' this.$final = function () {',
  21524. ' };',
  21525. ' rtl.addIntf(this, $mod.IUnknown);',
  21526. '});',
  21527. 'this.DoIt = function (i) {',
  21528. '};',
  21529. 'this.DoSome = function () {',
  21530. ' var v = null;',
  21531. ' try {',
  21532. ' $mod.DoIt({',
  21533. ' get: function () {',
  21534. ' return v;',
  21535. ' },',
  21536. ' set: function (w) {',
  21537. ' v = w;',
  21538. ' }',
  21539. ' });',
  21540. ' } finally {',
  21541. ' rtl._Release(v);',
  21542. ' };',
  21543. '};',
  21544. 'this.GetIt = function () {',
  21545. ' var Result = null;',
  21546. ' var $ok = false;',
  21547. ' try {',
  21548. ' $mod.DoIt({',
  21549. ' get: function () {',
  21550. ' return Result;',
  21551. ' },',
  21552. ' set: function (v) {',
  21553. ' Result = v;',
  21554. ' }',
  21555. ' });',
  21556. ' $ok = true;',
  21557. ' } finally {',
  21558. ' if (!$ok) rtl._Release(Result);',
  21559. ' };',
  21560. ' return Result;',
  21561. '};',
  21562. 'this.i = null;',
  21563. '']),
  21564. LinesToStr([ // $mod.$main
  21565. 'try {',
  21566. ' $mod.DoIt({',
  21567. ' p: $mod,',
  21568. ' get: function () {',
  21569. ' return this.p.i;',
  21570. ' },',
  21571. ' set: function (v) {',
  21572. ' this.p.i = v;',
  21573. ' }',
  21574. ' });',
  21575. '} finally {',
  21576. ' rtl._Release($mod.i);',
  21577. '};',
  21578. '']));
  21579. end;
  21580. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21581. begin
  21582. StartProgram(false);
  21583. Add([
  21584. '{$interfaces com}',
  21585. 'type',
  21586. ' IUnknown = interface',
  21587. ' function _AddRef: longint;',
  21588. ' function _Release: longint;',
  21589. ' end;',
  21590. ' TObject = class(IUnknown)',
  21591. ' function _AddRef: longint; virtual; abstract;',
  21592. ' function _Release: longint; virtual; abstract;',
  21593. ' end;',
  21594. 'function GetIt: IUnknown;',
  21595. 'begin',
  21596. 'end;',
  21597. 'procedure DoSome;',
  21598. 'var v: IUnknown;',
  21599. ' i: longint;',
  21600. 'begin',
  21601. ' v:=GetIt;',
  21602. ' v:=GetIt();',
  21603. ' GetIt()._AddRef;',
  21604. ' i:=GetIt()._AddRef;',
  21605. 'end;',
  21606. 'var v: IUnknown;',
  21607. ' i: longint;',
  21608. 'begin',
  21609. ' v:=GetIt;',
  21610. ' v:=GetIt();',
  21611. ' GetIt()._AddRef;',
  21612. ' i:=GetIt()._AddRef;',
  21613. '']);
  21614. ConvertProgram;
  21615. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21616. LinesToStr([ // statements
  21617. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21618. 'rtl.createClass(this, "TObject", null, function () {',
  21619. ' this.$init = function () {',
  21620. ' };',
  21621. ' this.$final = function () {',
  21622. ' };',
  21623. ' rtl.addIntf(this, $mod.IUnknown);',
  21624. '});',
  21625. 'this.GetIt = function () {',
  21626. ' var Result = null;',
  21627. ' return Result;',
  21628. '};',
  21629. 'this.DoSome = function () {',
  21630. ' var v = null;',
  21631. ' var i = 0;',
  21632. ' var $ir = rtl.createIntfRefs();',
  21633. ' try {',
  21634. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21635. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21636. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21637. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21638. ' } finally {',
  21639. ' $ir.free();',
  21640. ' rtl._Release(v);',
  21641. ' };',
  21642. '};',
  21643. 'this.v = null;',
  21644. 'this.i = 0;',
  21645. '']),
  21646. LinesToStr([ // $mod.$main
  21647. 'var $ir = rtl.createIntfRefs();',
  21648. 'try {',
  21649. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21650. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21651. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21652. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21653. '} finally {',
  21654. ' $ir.free();',
  21655. '};',
  21656. '']));
  21657. end;
  21658. procedure TTestModule.TestClassInterface_COM_Property;
  21659. begin
  21660. StartProgram(false);
  21661. Add([
  21662. '{$interfaces com}',
  21663. 'type',
  21664. ' IUnknown = interface',
  21665. ' function _AddRef: longint;',
  21666. ' function _Release: longint;',
  21667. ' end;',
  21668. ' TObject = class(IUnknown)',
  21669. ' FAnt: IUnknown;',
  21670. ' function _AddRef: longint; virtual; abstract;',
  21671. ' function _Release: longint; virtual; abstract;',
  21672. ' function GetBird: IUnknown; virtual; abstract;',
  21673. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21674. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21675. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21676. ' property Ant: IUnknown read FAnt write FAnt;',
  21677. ' property Bird: IUnknown read GetBird write SetBird;',
  21678. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21679. ' end;',
  21680. 'procedure DoIt;',
  21681. 'var',
  21682. ' o: TObject;',
  21683. ' v: IUnknown;',
  21684. 'begin',
  21685. ' v:=o.Ant;',
  21686. ' o.Ant:=v;',
  21687. ' o.Ant:=o.Ant;',
  21688. ' v:=o.Bird;',
  21689. ' o.Bird:=v;',
  21690. ' o.Bird:=o.Bird;',
  21691. ' v:=o.Items[1];',
  21692. ' o.Items[2]:=v;',
  21693. ' o.Items[3]:=o.Items[4];',
  21694. ' v:=o[5];',
  21695. ' o[6]:=v;',
  21696. ' o[7]:=o[8];',
  21697. 'end;',
  21698. 'begin',
  21699. '']);
  21700. ConvertProgram;
  21701. CheckSource('TestClassInterface_COM_Property',
  21702. LinesToStr([ // statements
  21703. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21704. 'rtl.createClass(this, "TObject", null, function () {',
  21705. ' this.$init = function () {',
  21706. ' this.FAnt = null;',
  21707. ' };',
  21708. ' this.$final = function () {',
  21709. ' rtl.setIntfP(this, "FAnt", null);',
  21710. ' };',
  21711. ' rtl.addIntf(this, $mod.IUnknown);',
  21712. '});',
  21713. 'this.DoIt = function () {',
  21714. ' var o = null;',
  21715. ' var v = null;',
  21716. ' var $ir = rtl.createIntfRefs();',
  21717. ' try {',
  21718. ' v = rtl.setIntfL(v, o.FAnt);',
  21719. ' rtl.setIntfP(o, "FAnt", v);',
  21720. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21721. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21722. ' o.SetBird(v);',
  21723. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21724. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21725. ' o.SetItems(2, v);',
  21726. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21727. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21728. ' o.SetItems(6, v);',
  21729. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21730. ' } finally {',
  21731. ' $ir.free();',
  21732. ' rtl._Release(v);',
  21733. ' };',
  21734. '};',
  21735. '']),
  21736. LinesToStr([ // $mod.$main
  21737. '']));
  21738. end;
  21739. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21740. begin
  21741. StartProgram(false);
  21742. Add([
  21743. '{$interfaces com}',
  21744. 'type',
  21745. ' IUnknown = interface',
  21746. ' function _AddRef: longint;',
  21747. ' function _Release: longint;',
  21748. ' function GetBird: IUnknown;',
  21749. ' procedure SetBird(Value: IUnknown);',
  21750. ' function GetItems(Index: longint): IUnknown;',
  21751. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21752. ' property Bird: IUnknown read GetBird write SetBird;',
  21753. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21754. ' end;',
  21755. ' TObject = class(IUnknown)',
  21756. ' function _AddRef: longint; virtual; abstract;',
  21757. ' function _Release: longint; virtual; abstract;',
  21758. ' function GetBird: IUnknown; virtual; abstract;',
  21759. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21760. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21761. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21762. ' end;',
  21763. 'procedure DoIt;',
  21764. 'var',
  21765. ' o: TObject;',
  21766. ' v: IUnknown;',
  21767. 'begin',
  21768. ' v:=v.Items[1];',
  21769. ' v.Items[2]:=v;',
  21770. ' v.Items[3]:=v.Items[4];',
  21771. ' v:=v[5];',
  21772. ' v[6]:=v;',
  21773. ' v[7]:=v[8];',
  21774. ' v[9].Bird.Bird:=v;',
  21775. ' v:=v.Bird[10].Bird',
  21776. 'end;',
  21777. 'begin',
  21778. '']);
  21779. ConvertProgram;
  21780. CheckSource('TestClassInterface_COM_IntfProperty',
  21781. LinesToStr([ // statements
  21782. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21783. ' "_AddRef",',
  21784. ' "_Release",',
  21785. ' "GetBird",',
  21786. ' "SetBird",',
  21787. ' "GetItems",',
  21788. ' "SetItems"',
  21789. '], null);',
  21790. 'rtl.createClass(this, "TObject", null, function () {',
  21791. ' this.$init = function () {',
  21792. ' };',
  21793. ' this.$final = function () {',
  21794. ' };',
  21795. ' rtl.addIntf(this, $mod.IUnknown);',
  21796. '});',
  21797. 'this.DoIt = function () {',
  21798. ' var o = null;',
  21799. ' var v = null;',
  21800. ' var $ir = rtl.createIntfRefs();',
  21801. ' try {',
  21802. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21803. ' v.SetItems(2, v);',
  21804. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21805. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21806. ' v.SetItems(6, v);',
  21807. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21808. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21809. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21810. ' } finally {',
  21811. ' $ir.free();',
  21812. ' rtl._Release(v);',
  21813. ' };',
  21814. '};',
  21815. '']),
  21816. LinesToStr([ // $mod.$main
  21817. '']));
  21818. end;
  21819. procedure TTestModule.TestClassInterface_COM_Delegation;
  21820. begin
  21821. StartProgram(false);
  21822. Add([
  21823. '{$interfaces com}',
  21824. 'type',
  21825. ' IUnknown = interface',
  21826. ' function _AddRef: longint;',
  21827. ' function _Release: longint;',
  21828. ' end;',
  21829. ' IBird = interface(IUnknown)',
  21830. ' procedure Fly(s: string);',
  21831. ' end;',
  21832. ' IEagle = interface(IBird) end;',
  21833. ' IDove = interface(IBird) end;',
  21834. ' ISwallow = interface(IBird) end;',
  21835. ' TObject = class',
  21836. ' end;',
  21837. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21838. ' function _AddRef: longint; virtual; abstract;',
  21839. ' function _Release: longint; virtual; abstract;',
  21840. ' procedure Fly(s: string); virtual; abstract;',
  21841. ' end;',
  21842. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21843. ' function _AddRef: longint; virtual; abstract;',
  21844. ' function _Release: longint; virtual; abstract;',
  21845. ' FBirdIntf: IBird;',
  21846. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21847. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21848. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21849. ' FDoveObj: TBird;',
  21850. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21851. ' function GetSwallowObj: TBird; virtual; abstract;',
  21852. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21853. ' end;',
  21854. 'begin',
  21855. '']);
  21856. ConvertProgram;
  21857. CheckSource('TestClassInterface_COM_Delegation',
  21858. LinesToStr([ // statements
  21859. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21860. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21861. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21862. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21863. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21864. 'rtl.createClass(this, "TObject", null, function () {',
  21865. ' this.$init = function () {',
  21866. ' };',
  21867. ' this.$final = function () {',
  21868. ' };',
  21869. '});',
  21870. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21871. ' rtl.addIntf(this, $mod.IBird);',
  21872. ' rtl.addIntf(this, $mod.IEagle);',
  21873. ' rtl.addIntf(this, $mod.IDove);',
  21874. ' rtl.addIntf(this, $mod.ISwallow);',
  21875. '});',
  21876. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21877. ' this.$init = function () {',
  21878. ' $mod.TObject.$init.call(this);',
  21879. ' this.FBirdIntf = null;',
  21880. ' this.FDoveObj = null;',
  21881. ' };',
  21882. ' this.$final = function () {',
  21883. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21884. ' this.FDoveObj = undefined;',
  21885. ' $mod.TObject.$final.call(this);',
  21886. ' };',
  21887. ' this.$intfmaps = {',
  21888. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  21889. ' return rtl._AddRef(this.FBirdIntf);',
  21890. ' },',
  21891. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  21892. ' return this.GetEagleIntf();',
  21893. ' },',
  21894. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  21895. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  21896. ' },',
  21897. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  21898. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  21899. ' }',
  21900. ' };',
  21901. '});',
  21902. '']),
  21903. LinesToStr([ // $mod.$main
  21904. '']));
  21905. end;
  21906. procedure TTestModule.TestClassInterface_COM_With;
  21907. begin
  21908. StartProgram(false);
  21909. Add([
  21910. '{$interfaces com}',
  21911. 'type',
  21912. ' IUnknown = interface',
  21913. ' function _AddRef: longint;',
  21914. ' function _Release: longint;',
  21915. ' function GetAnt: IUnknown;',
  21916. ' property Ant: IUnknown read GetAnt;',
  21917. ' end;',
  21918. ' TObject = class(IUnknown)',
  21919. ' function _AddRef: longint; virtual; abstract;',
  21920. ' function _Release: longint; virtual; abstract;',
  21921. ' function GetAnt: IUnknown; virtual; abstract;',
  21922. ' property Ant: IUnknown read GetAnt;',
  21923. ' end;',
  21924. 'procedure DoIt;',
  21925. 'var',
  21926. ' i: IUnknown;',
  21927. 'begin',
  21928. ' with i do ',
  21929. ' GetAnt;',
  21930. ' with i.Ant, Ant do ',
  21931. ' GetAnt;',
  21932. 'end;',
  21933. 'begin',
  21934. '']);
  21935. ConvertProgram;
  21936. CheckSource('TestClassInterface_COM_With',
  21937. LinesToStr([ // statements
  21938. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  21939. 'rtl.createClass(this, "TObject", null, function () {',
  21940. ' this.$init = function () {',
  21941. ' };',
  21942. ' this.$final = function () {',
  21943. ' };',
  21944. ' rtl.addIntf(this, $mod.IUnknown);',
  21945. '});',
  21946. 'this.DoIt = function () {',
  21947. ' var i = null;',
  21948. ' var $ir = rtl.createIntfRefs();',
  21949. ' try {',
  21950. ' $ir.ref(1, i.GetAnt());',
  21951. ' var $with = $ir.ref(2, i.GetAnt());',
  21952. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  21953. ' $ir.ref(4, $with1.GetAnt());',
  21954. ' } finally {',
  21955. ' $ir.free();',
  21956. ' };',
  21957. '};',
  21958. '']),
  21959. LinesToStr([ // $mod.$main
  21960. '']));
  21961. end;
  21962. procedure TTestModule.TestClassInterface_COM_ForIn;
  21963. begin
  21964. StartProgram(false);
  21965. Add([
  21966. '{$interfaces com}',
  21967. 'type',
  21968. ' IUnknown = interface end;',
  21969. ' TObject = class',
  21970. ' Id: longint;',
  21971. ' end;',
  21972. ' IEnumerator = interface(IUnknown)',
  21973. ' function GetCurrent: TObject;',
  21974. ' function MoveNext: Boolean;',
  21975. ' property Current: TObject read GetCurrent;',
  21976. ' end;',
  21977. ' IEnumerable = interface(IUnknown)',
  21978. ' function GetEnumerator: IEnumerator;',
  21979. ' end;',
  21980. 'var',
  21981. ' o: TObject;',
  21982. ' i: IEnumerable;',
  21983. 'begin',
  21984. ' for o in i do o.Id:=3;',
  21985. '']);
  21986. ConvertProgram;
  21987. CheckSource('TestClassInterface_COM_ForIn',
  21988. LinesToStr([ // statements
  21989. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21990. 'rtl.createClass(this, "TObject", null, function () {',
  21991. ' this.$init = function () {',
  21992. ' this.Id = 0;',
  21993. ' };',
  21994. ' this.$final = function () {',
  21995. ' };',
  21996. '});',
  21997. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21998. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21999. 'this.o = null;',
  22000. 'this.i = null;',
  22001. '']),
  22002. LinesToStr([ // $mod.$main
  22003. 'var $in = $mod.i.GetEnumerator();',
  22004. 'try {',
  22005. ' while ($in.MoveNext()) {',
  22006. ' $mod.o = $in.GetCurrent();',
  22007. ' $mod.o.Id = 3;',
  22008. ' }',
  22009. '} finally {',
  22010. ' rtl._Release($in)',
  22011. '};',
  22012. '']));
  22013. end;
  22014. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf;
  22015. begin
  22016. {$IFNDEF EnableCOMArrayOfIntf}
  22017. exit;
  22018. {$ENDIF}
  22019. StartProgram(false);
  22020. Add([
  22021. '{$interfaces com}',
  22022. 'type',
  22023. ' IUnknown = interface end;',
  22024. ' IBird = interface(IUnknown)',
  22025. ' function Fly(w: word): word;',
  22026. ' end;',
  22027. ' TBirdArray = array of IBird;',
  22028. 'procedure Run;',
  22029. 'var',
  22030. ' i: IBird;',
  22031. ' a,b: TBirdArray;',
  22032. 'begin',
  22033. //' SetLength(a,3);',
  22034. ' a:=b;',
  22035. ' i:=a[1];',
  22036. ' a[2]:=i;',
  22037. //' for i in a do i.fly(3);',
  22038. // a:=copy(b,1,2);
  22039. // a:=concat(b,a);
  22040. // insert(i,b,1);
  22041. // a:=[i,i];
  22042. 'end;',
  22043. // ToDo: pass TBirdArray as arg
  22044. 'begin',
  22045. '']);
  22046. ConvertProgram;
  22047. CheckSource('TestClassInterface_COM_ArrayOfIntf',
  22048. LinesToStr([ // statements
  22049. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22050. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22051. 'this.Run = function () {',
  22052. ' var i = null;',
  22053. ' var a = [];',
  22054. ' var b = [];',
  22055. ' try {',
  22056. ' a = rtl.arrayRef(b);',
  22057. ' i = rtl.setIntfL(i, a[1]);',
  22058. ' rtl.setIntfP(a, 2, i);',
  22059. ' } finally {',
  22060. ' rtl._Release(i);',
  22061. ' rtl._ReleaseArray(a,1);',
  22062. ' };',
  22063. '};',
  22064. '']),
  22065. LinesToStr([ // $mod.$main
  22066. '']));
  22067. end;
  22068. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  22069. begin
  22070. StartProgram(false);
  22071. Add([
  22072. '{$interfaces com}',
  22073. 'type',
  22074. ' IUnknown = interface',
  22075. ' function _AddRef: longint;',
  22076. ' function _Release: longint;',
  22077. ' end;',
  22078. ' TObject = class',
  22079. ' end;',
  22080. ' TArrOfIntf = array of IUnknown;',
  22081. 'begin',
  22082. '']);
  22083. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  22084. ConvertProgram;
  22085. end;
  22086. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22087. begin
  22088. StartProgram(false);
  22089. Add([
  22090. '{$interfaces com}',
  22091. 'type',
  22092. ' IUnknown = interface',
  22093. ' function _AddRef: longint;',
  22094. ' function _Release: longint;',
  22095. ' end;',
  22096. ' TRec = record',
  22097. ' i: IUnknown;',
  22098. ' end;',
  22099. 'begin',
  22100. '']);
  22101. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22102. ConvertProgram;
  22103. end;
  22104. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22105. begin
  22106. StartUnit(false);
  22107. Add([
  22108. '{$interfaces com}',
  22109. 'interface',
  22110. 'implementation',
  22111. 'type',
  22112. ' IUnknown = interface',
  22113. ' function _AddRef: longint;',
  22114. ' end;',
  22115. ' TObject = class(IUnknown)',
  22116. ' function _AddRef: longint;',
  22117. ' end;',
  22118. 'function TObject._AddRef: longint; begin end;',
  22119. 'var i: IUnknown;',
  22120. ' o: TObject;',
  22121. 'initialization',
  22122. ' i:=nil;',
  22123. ' i:=i;',
  22124. ' i:=o;',
  22125. ' if (o as IUnknown)=nil then ;',
  22126. '']);
  22127. ConvertUnit;
  22128. CheckSource('TestClassInterface_COM_UnitInitialization',
  22129. LinesToStr([ // statements
  22130. 'var $impl = $mod.$impl;',
  22131. '']),
  22132. LinesToStr([ // this.$init
  22133. 'var $ir = rtl.createIntfRefs();',
  22134. 'try {',
  22135. ' rtl.setIntfP($impl, "i", null);',
  22136. ' rtl.setIntfP($impl, "i", $impl.i);',
  22137. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22138. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22139. '} finally {',
  22140. ' $ir.free();',
  22141. '};',
  22142. '']),
  22143. LinesToStr([ // implementation
  22144. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22145. 'rtl.createClass($impl, "TObject", null, function () {',
  22146. ' this.$init = function () {',
  22147. ' };',
  22148. ' this.$final = function () {',
  22149. ' };',
  22150. ' this._AddRef = function () {',
  22151. ' var Result = 0;',
  22152. ' return Result;',
  22153. ' };',
  22154. ' rtl.addIntf(this, $impl.IUnknown);',
  22155. '});',
  22156. '$impl.i = null;',
  22157. '$impl.o = null;',
  22158. ''])
  22159. );
  22160. end;
  22161. procedure TTestModule.TestClassInterface_Corba_GUID;
  22162. begin
  22163. StartProgram(false);
  22164. Add([
  22165. '{$interfaces corba}',
  22166. 'type',
  22167. ' IUnknown = interface',
  22168. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22169. ' end;',
  22170. ' TObject = class end;',
  22171. ' TGUID = record D1, D2, D3, D4: word; end;',
  22172. ' TAliasGUID = TGUID;',
  22173. ' TGUIDString = type string;',
  22174. ' TAliasGUIDString = TGUIDString;',
  22175. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  22176. 'begin end;',
  22177. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  22178. 'begin end;',
  22179. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  22180. 'begin end;',
  22181. 'var',
  22182. ' i: IUnknown;',
  22183. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  22184. ' s: TAliasGUIDString;',
  22185. 'begin',
  22186. ' DoConstGUIDIt(IUnknown);',
  22187. ' DoDefGUID(IUnknown);',
  22188. ' DoStr(IUnknown);',
  22189. ' DoConstGUIDIt(i);',
  22190. ' DoDefGUID(i);',
  22191. ' DoStr(i);',
  22192. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22193. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22194. ' DoStr(g);',
  22195. ' g:=i;',
  22196. ' g:=IUnknown;',
  22197. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22198. ' s:=i;',
  22199. ' s:=IUnknown;',
  22200. ' s:=g;',
  22201. ' if g=i then ;',
  22202. ' if i=g then ;',
  22203. ' if g=IUnknown then ;',
  22204. ' if IUnknown=g then ;',
  22205. ' if s=i then ;',
  22206. ' if i=s then ;',
  22207. ' if s=IUnknown then ;',
  22208. ' if IUnknown=s then ;',
  22209. ' if s=g then ;',
  22210. ' if g=s then ;',
  22211. '']);
  22212. ConvertProgram;
  22213. CheckSource('TestClassInterface_Corba_GUID',
  22214. LinesToStr([ // statements
  22215. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22216. 'rtl.createClass(this, "TObject", null, function () {',
  22217. ' this.$init = function () {',
  22218. ' };',
  22219. ' this.$final = function () {',
  22220. ' };',
  22221. '});',
  22222. 'rtl.recNewT(this, "TGUID", function () {',
  22223. ' this.D1 = 0;',
  22224. ' this.D2 = 0;',
  22225. ' this.D3 = 0;',
  22226. ' this.D4 = 0;',
  22227. ' this.$eq = function (b) {',
  22228. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22229. ' };',
  22230. ' this.$assign = function (s) {',
  22231. ' this.D1 = s.D1;',
  22232. ' this.D2 = s.D2;',
  22233. ' this.D3 = s.D3;',
  22234. ' this.D4 = s.D4;',
  22235. ' return this;',
  22236. ' };',
  22237. '});',
  22238. 'this.DoConstGUIDIt = function (g) {',
  22239. '};',
  22240. 'this.DoDefGUID = function (g) {',
  22241. '};',
  22242. 'this.DoStr = function (s) {',
  22243. '};',
  22244. 'this.i = null;',
  22245. 'this.g = this.TGUID.$clone({',
  22246. ' D1: 0xD91C9AF4,',
  22247. ' D2: 0x3C93,',
  22248. ' D3: 0x420F,',
  22249. ' D4: [',
  22250. ' 0xA3,',
  22251. ' 0x03,',
  22252. ' 0xBF,',
  22253. ' 0x5B,',
  22254. ' 0xA8,',
  22255. ' 0x2B,',
  22256. ' 0xFD,',
  22257. ' 0x23',
  22258. ' ]',
  22259. '});',
  22260. 'this.s = "";',
  22261. '']),
  22262. LinesToStr([ // $mod.$main
  22263. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  22264. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  22265. '$mod.DoStr($mod.IUnknown.$guid);',
  22266. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  22267. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  22268. '$mod.DoStr($mod.i.$guid);',
  22269. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22270. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22271. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  22272. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  22273. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  22274. '$mod.g.$assign({',
  22275. ' D1: 0xD91C9AF4,',
  22276. ' D2: 0x3C93,',
  22277. ' D3: 0x420F,',
  22278. ' D4: [',
  22279. ' 0xA3,',
  22280. ' 0x03,',
  22281. ' 0xBF,',
  22282. ' 0x5B,',
  22283. ' 0xA8,',
  22284. ' 0x2B,',
  22285. ' 0xFD,',
  22286. ' 0x23',
  22287. ' ]',
  22288. '});',
  22289. '$mod.s = $mod.i.$guid;',
  22290. '$mod.s = $mod.IUnknown.$guid;',
  22291. '$mod.s = rtl.guidrToStr($mod.g);',
  22292. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22293. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22294. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22295. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22296. 'if ($mod.s === $mod.i.$guid) ;',
  22297. 'if ($mod.i.$guid === $mod.s) ;',
  22298. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22299. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22300. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22301. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22302. '']));
  22303. end;
  22304. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  22305. begin
  22306. StartProgram(false);
  22307. Add([
  22308. '{$interfaces corba}',
  22309. 'type',
  22310. ' IUnknown = interface',
  22311. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22312. ' end;',
  22313. ' TGUID = record D1, D2, D3, D4: word; end;',
  22314. ' TAliasGUID = TGUID;',
  22315. ' TGUIDString = type string;',
  22316. ' TAliasGUIDString = TGUIDString;',
  22317. ' TObject = class',
  22318. ' function GetG: TAliasGUID; virtual; abstract;',
  22319. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22320. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22321. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22322. ' property g: TAliasGUID read GetG write SetG;',
  22323. ' property s: TAliasGUIDString read GetS write SetS;',
  22324. ' end;',
  22325. 'var o: TObject;',
  22326. 'begin',
  22327. ' o.g:=IUnknown;',
  22328. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22329. ' o.s:=IUnknown;',
  22330. ' o.s:=o.g;',
  22331. '']);
  22332. ConvertProgram;
  22333. CheckSource('TestClassInterface_GUIDProperty',
  22334. LinesToStr([ // statements
  22335. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22336. 'rtl.recNewT(this, "TGUID", function () {',
  22337. ' this.D1 = 0;',
  22338. ' this.D2 = 0;',
  22339. ' this.D3 = 0;',
  22340. ' this.D4 = 0;',
  22341. ' this.$eq = function (b) {',
  22342. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22343. ' };',
  22344. ' this.$assign = function (s) {',
  22345. ' this.D1 = s.D1;',
  22346. ' this.D2 = s.D2;',
  22347. ' this.D3 = s.D3;',
  22348. ' this.D4 = s.D4;',
  22349. ' return this;',
  22350. ' };',
  22351. '});',
  22352. 'rtl.createClass(this, "TObject", null, function () {',
  22353. ' this.$init = function () {',
  22354. ' };',
  22355. ' this.$final = function () {',
  22356. ' };',
  22357. '});',
  22358. 'this.o = null;',
  22359. '']),
  22360. LinesToStr([ // $mod.$main
  22361. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22362. '$mod.o.SetG({',
  22363. ' D1: 0xD91C9AF4,',
  22364. ' D2: 0x3C93,',
  22365. ' D3: 0x420F,',
  22366. ' D4: [',
  22367. ' 0xA3,',
  22368. ' 0x03,',
  22369. ' 0xBF,',
  22370. ' 0x5B,',
  22371. ' 0xA8,',
  22372. ' 0x2B,',
  22373. ' 0xFD,',
  22374. ' 0x23',
  22375. ' ]',
  22376. '});',
  22377. '$mod.o.SetS($mod.IUnknown.$guid);',
  22378. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22379. '']));
  22380. end;
  22381. procedure TTestModule.TestClassHelper_ClassVar;
  22382. begin
  22383. StartProgram(false);
  22384. Add([
  22385. 'type',
  22386. ' TObject = class',
  22387. ' end;',
  22388. ' THelper = class helper for TObject',
  22389. ' const',
  22390. ' One = 1;',
  22391. ' Two: word = 2;',
  22392. ' class var',
  22393. ' Glob: word;',
  22394. ' function Foo(w: word): word;',
  22395. ' class function Bar(w: word): word;',
  22396. ' end;',
  22397. 'function THelper.foo(w: word): word;',
  22398. 'begin',
  22399. ' Result:=w;',
  22400. ' Two:=One+w;',
  22401. ' Glob:=Glob;',
  22402. ' Result:=Self.Glob;',
  22403. ' Self.Glob:=Self.Glob;',
  22404. ' with Self do Glob:=Glob;',
  22405. 'end;',
  22406. 'class function THelper.bar(w: word): word;',
  22407. 'begin',
  22408. ' Result:=w;',
  22409. ' Two:=One;',
  22410. ' Glob:=Glob;',
  22411. ' Self.Glob:=Self.Glob;',
  22412. ' with Self do Glob:=Glob;',
  22413. 'end;',
  22414. 'var o: TObject;',
  22415. 'begin',
  22416. ' tobject.two:=tobject.one;',
  22417. ' tobject.Glob:=tobject.Glob;',
  22418. ' with tobject do begin',
  22419. ' two:=one;',
  22420. ' Glob:=Glob;',
  22421. ' end;',
  22422. ' o.two:=o.one;',
  22423. ' o.Glob:=o.Glob;',
  22424. ' with o do begin',
  22425. ' two:=one;',
  22426. ' Glob:=Glob;',
  22427. ' end;',
  22428. '']);
  22429. ConvertProgram;
  22430. CheckSource('TestClassHelper_ClassVar',
  22431. LinesToStr([ // statements
  22432. 'rtl.createClass(this, "TObject", null, function () {',
  22433. ' this.$init = function () {',
  22434. ' };',
  22435. ' this.$final = function () {',
  22436. ' };',
  22437. '});',
  22438. 'rtl.createHelper(this, "THelper", null, function () {',
  22439. ' this.One = 1;',
  22440. ' this.Two = 2;',
  22441. ' this.Glob = 0;',
  22442. ' this.Foo = function (w) {',
  22443. ' var Result = 0;',
  22444. ' Result = w;',
  22445. ' $mod.THelper.Two = 1 + w;',
  22446. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22447. ' Result = $mod.THelper.Glob;',
  22448. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22449. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22450. ' return Result;',
  22451. ' };',
  22452. ' this.Bar = function (w) {',
  22453. ' var Result = 0;',
  22454. ' Result = w;',
  22455. ' $mod.THelper.Two = 1;',
  22456. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22457. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22458. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22459. ' return Result;',
  22460. ' };',
  22461. '});',
  22462. 'this.o = null;',
  22463. '']),
  22464. LinesToStr([ // $mod.$main
  22465. '$mod.THelper.Two = 1;',
  22466. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22467. 'var $with = $mod.TObject;',
  22468. '$mod.THelper.Two = 1;',
  22469. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22470. '$mod.THelper.Two = 1;',
  22471. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22472. 'var $with1 = $mod.o;',
  22473. '$mod.THelper.Two = 1;',
  22474. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22475. '']));
  22476. end;
  22477. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22478. begin
  22479. StartProgram(false);
  22480. Add([
  22481. 'type',
  22482. ' TObject = class',
  22483. ' FSize: word;',
  22484. ' property Size: word read FSize write FSize;',
  22485. ' end;',
  22486. ' THelper = class helper for TObject',
  22487. ' function Foo(w: word = 1): word;',
  22488. ' end;',
  22489. 'function THelper.foo(w: word): word;',
  22490. 'begin',
  22491. ' Result:=Size;',
  22492. ' Size:=Size+2;',
  22493. ' Self.Size:=Self.Size+3;',
  22494. ' FSize:=FSize+4;',
  22495. ' Self.FSize:=Self.FSize+5;',
  22496. ' with Self do begin',
  22497. ' Size:=Size+6;',
  22498. ' FSize:=FSize+7;',
  22499. ' FSize:=FSize+8;',
  22500. ' end;',
  22501. 'end;',
  22502. 'begin',
  22503. '']);
  22504. ConvertProgram;
  22505. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22506. LinesToStr([ // statements
  22507. 'rtl.createClass(this, "TObject", null, function () {',
  22508. ' this.$init = function () {',
  22509. ' this.FSize = 0;',
  22510. ' };',
  22511. ' this.$final = function () {',
  22512. ' };',
  22513. '});',
  22514. 'rtl.createHelper(this, "THelper", null, function () {',
  22515. ' this.Foo = function (w) {',
  22516. ' var Result = 0;',
  22517. ' Result = this.FSize;',
  22518. ' this.FSize = this.FSize + 2;',
  22519. ' this.FSize = this.FSize + 3;',
  22520. ' this.FSize = this.FSize + 4;',
  22521. ' this.FSize = this.FSize + 5;',
  22522. ' this.FSize = this.FSize + 6;',
  22523. ' this.FSize = this.FSize + 7;',
  22524. ' this.FSize = this.FSize + 8;',
  22525. ' return Result;',
  22526. ' };',
  22527. '});',
  22528. '']),
  22529. LinesToStr([ // $mod.$main
  22530. '']));
  22531. end;
  22532. procedure TTestModule.TestClassHelper_Method_Call;
  22533. begin
  22534. StartProgram(false);
  22535. Add([
  22536. 'type',
  22537. ' TObject = class',
  22538. ' procedure Run(w: word = 10);',
  22539. ' end;',
  22540. ' THelper = class helper for TObject',
  22541. ' function Foo(w: word = 1): word;',
  22542. ' end;',
  22543. 'procedure TObject.Run(w: word);',
  22544. 'var o: TObject;',
  22545. 'begin',
  22546. ' Foo;',
  22547. ' Foo();',
  22548. ' Foo(2);',
  22549. ' Self.Foo;',
  22550. ' Self.Foo();',
  22551. ' Self.Foo(3);',
  22552. ' with Self do begin',
  22553. ' Foo;',
  22554. ' Foo();',
  22555. ' Foo(4);',
  22556. ' end;',
  22557. ' with o do Foo(5);',
  22558. 'end;',
  22559. 'function THelper.foo(w: word): word;',
  22560. 'begin',
  22561. ' Run;',
  22562. ' Run();',
  22563. ' Run(11);',
  22564. ' Foo;',
  22565. ' Foo();',
  22566. ' Foo(12);',
  22567. ' Self.Foo;',
  22568. ' Self.Foo();',
  22569. ' Self.Foo(13);',
  22570. ' with Self do begin',
  22571. ' Foo;',
  22572. ' Foo();',
  22573. ' Foo(14);',
  22574. ' end;',
  22575. 'end;',
  22576. 'var Obj: TObject;',
  22577. 'begin',
  22578. ' obj.Foo;',
  22579. ' obj.Foo();',
  22580. ' obj.Foo(21);',
  22581. ' with obj do begin',
  22582. ' Foo;',
  22583. ' Foo();',
  22584. ' Foo(22);',
  22585. ' end;',
  22586. '']);
  22587. ConvertProgram;
  22588. CheckSource('TestClassHelper_Method_Call',
  22589. LinesToStr([ // statements
  22590. 'rtl.createClass(this, "TObject", null, function () {',
  22591. ' this.$init = function () {',
  22592. ' };',
  22593. ' this.$final = function () {',
  22594. ' };',
  22595. ' this.Run = function (w) {',
  22596. ' var o = null;',
  22597. ' $mod.THelper.Foo.call(this, 1);',
  22598. ' $mod.THelper.Foo.call(this, 1);',
  22599. ' $mod.THelper.Foo.call(this, 2);',
  22600. ' $mod.THelper.Foo.call(this, 1);',
  22601. ' $mod.THelper.Foo.call(this, 1);',
  22602. ' $mod.THelper.Foo.call(this, 3);',
  22603. ' $mod.THelper.Foo.call(this, 1);',
  22604. ' $mod.THelper.Foo.call(this, 1);',
  22605. ' $mod.THelper.Foo.call(this, 4);',
  22606. ' $mod.THelper.Foo.call(o, 5);',
  22607. ' };',
  22608. '});',
  22609. 'rtl.createHelper(this, "THelper", null, function () {',
  22610. ' this.Foo = function (w) {',
  22611. ' var Result = 0;',
  22612. ' this.Run(10);',
  22613. ' this.Run(10);',
  22614. ' this.Run(11);',
  22615. ' $mod.THelper.Foo.call(this, 1);',
  22616. ' $mod.THelper.Foo.call(this, 1);',
  22617. ' $mod.THelper.Foo.call(this, 12);',
  22618. ' $mod.THelper.Foo.call(this, 1);',
  22619. ' $mod.THelper.Foo.call(this, 1);',
  22620. ' $mod.THelper.Foo.call(this, 13);',
  22621. ' $mod.THelper.Foo.call(this, 1);',
  22622. ' $mod.THelper.Foo.call(this, 1);',
  22623. ' $mod.THelper.Foo.call(this, 14);',
  22624. ' return Result;',
  22625. ' };',
  22626. '});',
  22627. 'this.Obj = null;',
  22628. '']),
  22629. LinesToStr([ // $mod.$main
  22630. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22631. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22632. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22633. 'var $with = $mod.Obj;',
  22634. '$mod.THelper.Foo.call($with, 1);',
  22635. '$mod.THelper.Foo.call($with, 1);',
  22636. '$mod.THelper.Foo.call($with, 22);',
  22637. '']));
  22638. end;
  22639. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22640. begin
  22641. StartProgram(false);
  22642. Add([
  22643. 'type',
  22644. ' TObject = class',
  22645. ' procedure Run(w: word = 10);',
  22646. ' end;',
  22647. ' THelper = class helper for TObject',
  22648. ' function Foo(w: word = 1): word;',
  22649. ' end;',
  22650. 'procedure TObject.Run(w: word);',
  22651. ' procedure Sub(Self: TObject);',
  22652. ' begin',
  22653. ' Foo;',
  22654. ' Foo();',
  22655. ' Self.Foo;',
  22656. ' Self.Foo();',
  22657. ' with Self do begin',
  22658. ' Foo;',
  22659. ' Foo();',
  22660. ' end;',
  22661. ' end;',
  22662. 'begin',
  22663. 'end;',
  22664. 'function THelper.foo(w: word): word;',
  22665. ' procedure Sub(Self: TObject);',
  22666. ' begin',
  22667. ' Run;',
  22668. ' Run();',
  22669. ' Foo;',
  22670. ' Foo();',
  22671. ' Self.Foo;',
  22672. ' Self.Foo();',
  22673. ' with Self do begin',
  22674. ' Foo;',
  22675. ' Foo();',
  22676. ' end;',
  22677. ' end;',
  22678. 'begin',
  22679. 'end;',
  22680. 'begin',
  22681. '']);
  22682. ConvertProgram;
  22683. CheckSource('TestClassHelper_Method_Nested_Call',
  22684. LinesToStr([ // statements
  22685. 'rtl.createClass(this, "TObject", null, function () {',
  22686. ' this.$init = function () {',
  22687. ' };',
  22688. ' this.$final = function () {',
  22689. ' };',
  22690. ' this.Run = function (w) {',
  22691. ' var $Self = this;',
  22692. ' function Sub(Self) {',
  22693. ' $mod.THelper.Foo.call($Self, 1);',
  22694. ' $mod.THelper.Foo.call($Self, 1);',
  22695. ' $mod.THelper.Foo.call(Self, 1);',
  22696. ' $mod.THelper.Foo.call(Self, 1);',
  22697. ' $mod.THelper.Foo.call(Self, 1);',
  22698. ' $mod.THelper.Foo.call(Self, 1);',
  22699. ' };',
  22700. ' };',
  22701. '});',
  22702. 'rtl.createHelper(this, "THelper", null, function () {',
  22703. ' this.Foo = function (w) {',
  22704. ' var $Self = this;',
  22705. ' var Result = 0;',
  22706. ' function Sub(Self) {',
  22707. ' $Self.Run(10);',
  22708. ' $Self.Run(10);',
  22709. ' $mod.THelper.Foo.call($Self, 1);',
  22710. ' $mod.THelper.Foo.call($Self, 1);',
  22711. ' $mod.THelper.Foo.call(Self, 1);',
  22712. ' $mod.THelper.Foo.call(Self, 1);',
  22713. ' $mod.THelper.Foo.call(Self, 1);',
  22714. ' $mod.THelper.Foo.call(Self, 1);',
  22715. ' };',
  22716. ' return Result;',
  22717. ' };',
  22718. '});',
  22719. '']),
  22720. LinesToStr([ // $mod.$main
  22721. '']));
  22722. end;
  22723. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22724. begin
  22725. StartProgram(false);
  22726. Add([
  22727. 'type',
  22728. ' TObject = class',
  22729. ' class procedure Run(w: word = 10);',
  22730. ' end;',
  22731. ' THelper = class helper for TObject',
  22732. ' class function Foo(w: word = 1): word;',
  22733. ' end;',
  22734. 'class procedure TObject.Run(w: word);',
  22735. 'begin',
  22736. ' Foo;',
  22737. ' Foo();',
  22738. ' Self.Foo;',
  22739. ' Self.Foo();',
  22740. ' with Self do begin',
  22741. ' Foo;',
  22742. ' Foo();',
  22743. ' end;',
  22744. 'end;',
  22745. 'class function THelper.foo(w: word): word;',
  22746. 'begin',
  22747. ' Run;',
  22748. ' Run();',
  22749. ' Foo;',
  22750. ' Foo();',
  22751. ' Self.Foo;',
  22752. ' Self.Foo();',
  22753. ' with Self do begin',
  22754. ' Foo;',
  22755. ' Foo();',
  22756. ' end;',
  22757. 'end;',
  22758. 'var',
  22759. ' Obj: TObject;',
  22760. 'begin',
  22761. ' obj.Foo;',
  22762. ' obj.Foo();',
  22763. ' with obj do begin',
  22764. ' Foo;',
  22765. ' Foo();',
  22766. ' end;',
  22767. ' tobject.Foo;',
  22768. ' tobject.Foo();',
  22769. ' with tobject do begin',
  22770. ' Foo;',
  22771. ' Foo();',
  22772. ' end;',
  22773. '']);
  22774. ConvertProgram;
  22775. CheckSource('TestClassHelper_ClassMethod_Call',
  22776. LinesToStr([ // statements
  22777. 'rtl.createClass(this, "TObject", null, function () {',
  22778. ' this.$init = function () {',
  22779. ' };',
  22780. ' this.$final = function () {',
  22781. ' };',
  22782. ' this.Run = function (w) {',
  22783. ' $mod.THelper.Foo.call(this, 1);',
  22784. ' $mod.THelper.Foo.call(this, 1);',
  22785. ' $mod.THelper.Foo.call(this, 1);',
  22786. ' $mod.THelper.Foo.call(this, 1);',
  22787. ' $mod.THelper.Foo.call(this, 1);',
  22788. ' $mod.THelper.Foo.call(this, 1);',
  22789. ' };',
  22790. '});',
  22791. 'rtl.createHelper(this, "THelper", null, function () {',
  22792. ' this.Foo = function (w) {',
  22793. ' var Result = 0;',
  22794. ' this.Run(10);',
  22795. ' this.Run(10);',
  22796. ' $mod.THelper.Foo.call(this, 1);',
  22797. ' $mod.THelper.Foo.call(this, 1);',
  22798. ' $mod.THelper.Foo.call(this, 1);',
  22799. ' $mod.THelper.Foo.call(this, 1);',
  22800. ' $mod.THelper.Foo.call(this, 1);',
  22801. ' $mod.THelper.Foo.call(this, 1);',
  22802. ' return Result;',
  22803. ' };',
  22804. '});',
  22805. 'this.Obj = null;',
  22806. '']),
  22807. LinesToStr([ // $mod.$main
  22808. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22809. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22810. 'var $with = $mod.Obj;',
  22811. '$mod.THelper.Foo.call($with.$class, 1);',
  22812. '$mod.THelper.Foo.call($with.$class, 1);',
  22813. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22814. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22815. 'var $with1 = $mod.TObject;',
  22816. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22817. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22818. '']));
  22819. end;
  22820. procedure TTestModule.TestClassHelper_ClassOf;
  22821. begin
  22822. StartProgram(false);
  22823. Add([
  22824. 'type',
  22825. ' TObject = class',
  22826. ' end;',
  22827. ' TClass = class of TObject;',
  22828. ' THelper = class helper for TObject',
  22829. ' class function Foo(w: word = 1): word;',
  22830. ' end;',
  22831. 'class function THelper.foo(w: word): word;',
  22832. 'begin',
  22833. 'end;',
  22834. 'var',
  22835. ' c: TClass;',
  22836. 'begin',
  22837. ' c.Foo;',
  22838. ' c.Foo();',
  22839. ' with c do begin',
  22840. ' Foo;',
  22841. ' Foo();',
  22842. ' end;',
  22843. '']);
  22844. ConvertProgram;
  22845. CheckSource('TestClassHelper_ClassOf',
  22846. LinesToStr([ // statements
  22847. 'rtl.createClass(this, "TObject", null, function () {',
  22848. ' this.$init = function () {',
  22849. ' };',
  22850. ' this.$final = function () {',
  22851. ' };',
  22852. '});',
  22853. 'rtl.createHelper(this, "THelper", null, function () {',
  22854. ' this.Foo = function (w) {',
  22855. ' var Result = 0;',
  22856. ' return Result;',
  22857. ' };',
  22858. '});',
  22859. 'this.c = null;',
  22860. '']),
  22861. LinesToStr([ // $mod.$main
  22862. '$mod.THelper.Foo.call($mod.c, 1);',
  22863. '$mod.THelper.Foo.call($mod.c, 1);',
  22864. 'var $with = $mod.c;',
  22865. '$mod.THelper.Foo.call($with, 1);',
  22866. '$mod.THelper.Foo.call($with, 1);',
  22867. '']));
  22868. end;
  22869. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22870. begin
  22871. StartProgram(false);
  22872. Add([
  22873. '{$mode objfpc}',
  22874. 'type',
  22875. ' TObject = class',
  22876. ' procedure DoIt;',
  22877. ' end;',
  22878. ' THelper = class helper for TObject',
  22879. ' procedure Fly(w: word = 1);',
  22880. ' class procedure Glide(w: word = 1);',
  22881. ' class procedure Run(w: word = 1); static;',
  22882. ' end;',
  22883. ' TFly = procedure(w: word) of object;',
  22884. ' TGlide = TFly;',
  22885. ' TRun = procedure(w: word);',
  22886. 'var',
  22887. ' f: TFly;',
  22888. ' g: TGlide;',
  22889. ' r: TRun;',
  22890. 'procedure TObject.DoIt;',
  22891. 'begin',
  22892. ' f:=@fly;',
  22893. ' g:=@glide;',
  22894. ' r:=@run;',
  22895. ' f:[email protected];',
  22896. ' g:[email protected];',
  22897. ' r:[email protected];',
  22898. ' with self do begin',
  22899. ' f:=@fly;',
  22900. ' g:=@glide;',
  22901. ' r:=@run;',
  22902. ' end;',
  22903. 'end;',
  22904. 'procedure THelper.fly(w: word);',
  22905. 'begin',
  22906. ' f:=@fly;',
  22907. ' g:=@glide;',
  22908. ' r:=@run;',
  22909. 'end;',
  22910. 'class procedure THelper.glide(w: word);',
  22911. 'begin',
  22912. ' g:=@glide;',
  22913. ' r:=@run;',
  22914. 'end;',
  22915. 'class procedure THelper.run(w: word);',
  22916. 'begin',
  22917. ' g:=@glide;',
  22918. ' r:=@run;',
  22919. 'end;',
  22920. 'var',
  22921. ' Obj: TObject;',
  22922. 'begin',
  22923. ' f:[email protected];',
  22924. ' g:[email protected];',
  22925. ' r:[email protected];',
  22926. ' with obj do begin',
  22927. ' f:=@fly;',
  22928. ' g:=@glide;',
  22929. ' r:=@run;',
  22930. ' end;',
  22931. ' g:[email protected];',
  22932. ' r:[email protected];',
  22933. ' with tobject do begin',
  22934. ' g:=@glide;',
  22935. ' r:=@run;',
  22936. ' end;',
  22937. '']);
  22938. ConvertProgram;
  22939. CheckSource('TestClassHelper_MethodRefObjFPC',
  22940. LinesToStr([ // statements
  22941. 'rtl.createClass(this, "TObject", null, function () {',
  22942. ' this.$init = function () {',
  22943. ' };',
  22944. ' this.$final = function () {',
  22945. ' };',
  22946. ' this.DoIt = function () {',
  22947. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22948. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22949. ' $mod.r = $mod.THelper.Run;',
  22950. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22951. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22952. ' $mod.r = $mod.THelper.Run;',
  22953. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22954. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22955. ' $mod.r = $mod.THelper.Run;',
  22956. ' };',
  22957. '});',
  22958. 'rtl.createHelper(this, "THelper", null, function () {',
  22959. ' this.Fly = function (w) {',
  22960. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22961. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22962. ' $mod.r = $mod.THelper.Run;',
  22963. ' };',
  22964. ' this.Glide = function (w) {',
  22965. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  22966. ' $mod.r = $mod.THelper.Run;',
  22967. ' };',
  22968. ' this.Run = function (w) {',
  22969. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  22970. ' $mod.r = $mod.THelper.Run;',
  22971. ' };',
  22972. '});',
  22973. 'this.f = null;',
  22974. 'this.g = null;',
  22975. 'this.r = null;',
  22976. 'this.Obj = null;',
  22977. '']),
  22978. LinesToStr([ // $mod.$main
  22979. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  22980. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  22981. '$mod.r = $mod.THelper.Run;',
  22982. 'var $with = $mod.Obj;',
  22983. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  22984. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  22985. '$mod.r = $mod.THelper.Run;',
  22986. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  22987. '$mod.r = $mod.THelper.Run;',
  22988. 'var $with1 = $mod.TObject;',
  22989. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  22990. '$mod.r = $mod.THelper.Run;',
  22991. '']));
  22992. end;
  22993. procedure TTestModule.TestClassHelper_Constructor;
  22994. begin
  22995. StartProgram(false);
  22996. Add([
  22997. 'type',
  22998. ' TObject = class',
  22999. ' constructor Create;',
  23000. ' end;',
  23001. ' TClass = class of TObject;',
  23002. ' THelper = class helper for TObject',
  23003. ' constructor NewHlp(w: word);',
  23004. ' end;',
  23005. 'var',
  23006. ' obj: TObject;',
  23007. ' c: TClass;',
  23008. 'constructor TObject.Create;',
  23009. 'begin',
  23010. ' NewHlp(2);', // normal call
  23011. ' tobject.NewHlp(3);', // new instance
  23012. ' c.newhlp(4);', // new instance
  23013. 'end;',
  23014. 'constructor THelper.NewHlp(w: word);',
  23015. 'begin',
  23016. ' create;', // normal call
  23017. ' tobject.create;', // new instance
  23018. ' NewHlp(2);', // normal call
  23019. ' tobject.NewHlp(3);', // new instance
  23020. ' c.newhlp(4);', // new instance
  23021. 'end;',
  23022. 'begin',
  23023. ' obj.newhlp(2);', // normal call
  23024. ' with Obj do newhlp(12);', // normal call
  23025. ' tobject.newhlp(3);', // new instance
  23026. ' with tobject do newhlp(13);', // new instance
  23027. ' c.newhlp(4);', // new instance
  23028. ' with c do newhlp(14);', // new instance
  23029. '']);
  23030. ConvertProgram;
  23031. CheckSource('TestClassHelper_Constructor',
  23032. LinesToStr([ // statements
  23033. 'rtl.createClass(this, "TObject", null, function () {',
  23034. ' this.$init = function () {',
  23035. ' };',
  23036. ' this.$final = function () {',
  23037. ' };',
  23038. ' this.Create = function () {',
  23039. ' $mod.THelper.NewHlp.call(this, 2);',
  23040. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23041. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23042. ' return this;',
  23043. ' };',
  23044. '});',
  23045. 'rtl.createHelper(this, "THelper", null, function () {',
  23046. ' this.NewHlp = function (w) {',
  23047. ' this.Create();',
  23048. ' $mod.TObject.$create("Create");',
  23049. ' $mod.THelper.NewHlp.call(this, 2);',
  23050. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23051. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23052. ' return this;',
  23053. ' };',
  23054. '});',
  23055. 'this.obj = null;',
  23056. 'this.c = null;',
  23057. '']),
  23058. LinesToStr([ // $mod.$main
  23059. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23060. 'var $with = $mod.obj;',
  23061. '$mod.THelper.NewHlp.call($with, 12);',
  23062. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23063. 'var $with1 = $mod.TObject;',
  23064. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23065. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23066. 'var $with2 = $mod.c;',
  23067. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23068. '']));
  23069. end;
  23070. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23071. begin
  23072. StartProgram(false);
  23073. Add([
  23074. 'type',
  23075. ' TObject = class',
  23076. ' procedure Fly;',
  23077. ' end;',
  23078. ' TObjHelper = class helper for TObject',
  23079. ' procedure Fly;',
  23080. ' end;',
  23081. ' TBird = class',
  23082. ' procedure Fly;',
  23083. ' end;',
  23084. ' TBirdHelper = class helper for TBird',
  23085. ' procedure Fly;',
  23086. ' procedure Walk(w: word);',
  23087. ' end;',
  23088. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23089. ' procedure Fly;',
  23090. ' procedure Walk(w: word);',
  23091. ' end;',
  23092. 'procedure Tobject.fly;',
  23093. 'begin',
  23094. ' inherited;', // ignore
  23095. 'end;',
  23096. 'procedure Tobjhelper.fly;',
  23097. 'begin',
  23098. ' {@TObject_Fly}inherited;',
  23099. ' inherited {@TObject_Fly}Fly;',
  23100. 'end;',
  23101. 'procedure Tbird.fly;',
  23102. 'begin',
  23103. ' {@TObjHelper_Fly}inherited;',
  23104. ' inherited {@TObjHelper_Fly}Fly;',
  23105. 'end;',
  23106. 'procedure Tbirdhelper.fly;',
  23107. 'begin',
  23108. ' {@TBird_Fly}inherited;',
  23109. ' inherited {@TBird_Fly}Fly;',
  23110. 'end;',
  23111. 'procedure Tbirdhelper.walk(w: word);',
  23112. 'begin',
  23113. 'end;',
  23114. 'procedure teagleHelper.fly;',
  23115. 'begin',
  23116. ' {@TBird_Fly}inherited;',
  23117. ' inherited {@TBird_Fly}Fly;',
  23118. 'end;',
  23119. 'procedure teagleHelper.walk(w: word);',
  23120. 'begin',
  23121. ' {@TBirdHelper_Walk}inherited;',
  23122. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23123. 'end;',
  23124. 'begin',
  23125. '']);
  23126. ConvertProgram;
  23127. CheckSource('TestClassHelper_InheritedObjFPC',
  23128. LinesToStr([ // statements
  23129. 'rtl.createClass(this, "TObject", null, function () {',
  23130. ' this.$init = function () {',
  23131. ' };',
  23132. ' this.$final = function () {',
  23133. ' };',
  23134. ' this.Fly = function () {',
  23135. ' };',
  23136. '});',
  23137. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23138. ' this.Fly = function () {',
  23139. ' $mod.TObject.Fly.call(this);',
  23140. ' $mod.TObject.Fly.call(this);',
  23141. ' };',
  23142. '});',
  23143. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23144. ' this.Fly$1 = function () {',
  23145. ' $mod.TObjHelper.Fly.call(this);',
  23146. ' $mod.TObjHelper.Fly.call(this);',
  23147. ' };',
  23148. '});',
  23149. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23150. ' this.Fly = function () {',
  23151. ' $mod.TBird.Fly$1.call(this);',
  23152. ' $mod.TBird.Fly$1.call(this);',
  23153. ' };',
  23154. ' this.Walk = function (w) {',
  23155. ' };',
  23156. '});',
  23157. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23158. ' this.Fly$1 = function () {',
  23159. ' $mod.TBird.Fly$1.call(this);',
  23160. ' $mod.TBird.Fly$1.call(this);',
  23161. ' };',
  23162. ' this.Walk$1 = function (w) {',
  23163. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  23164. ' $mod.TBirdHelper.Walk.call(this, 3);',
  23165. ' };',
  23166. '});',
  23167. '']),
  23168. LinesToStr([ // $mod.$main
  23169. '']));
  23170. end;
  23171. procedure TTestModule.TestClassHelper_Property;
  23172. begin
  23173. StartProgram(false);
  23174. Add([
  23175. 'type',
  23176. ' TObject = class',
  23177. ' FSize: word;',
  23178. ' function GetSpeed: word;',
  23179. ' procedure SetSpeed(Value: word);',
  23180. ' end;',
  23181. ' TObjHelper = class helper for TObject',
  23182. ' function GetLeft: word;',
  23183. ' procedure SetLeft(Value: word);',
  23184. ' property Size: word read FSize write FSize;',
  23185. ' property Speed: word read GetSpeed write SetSpeed;',
  23186. ' property Left: word read GetLeft write SetLeft;',
  23187. ' end;',
  23188. ' TBird = class',
  23189. ' property NotRight: word read GetLeft write SetLeft;',
  23190. ' procedure DoIt;',
  23191. ' end;',
  23192. 'var',
  23193. ' b: TBird;',
  23194. 'function Tobject.GetSpeed: word;',
  23195. 'begin',
  23196. ' Size:=Size+11;',
  23197. ' Speed:=Speed+12;',
  23198. ' Result:=Left+13;',
  23199. ' Left:=13;',
  23200. ' Left:=Left+13;',
  23201. ' Self.Size:=Self.Size+21;',
  23202. ' Self.Speed:=Self.Speed+22;',
  23203. ' Self.Left:=Self.Left+23;',
  23204. ' with Self do begin',
  23205. ' Size:=Size+31;',
  23206. ' Speed:=Speed+32;',
  23207. ' Left:=Left+33;',
  23208. ' end;',
  23209. 'end;',
  23210. 'procedure Tobject.SetSpeed(Value: word);',
  23211. 'begin',
  23212. 'end;',
  23213. 'function TObjHelper.GetLeft: word;',
  23214. 'begin',
  23215. ' Size:=Size+11;',
  23216. ' Speed:=Speed+12;',
  23217. ' Left:=Left+13;',
  23218. ' Self.Size:=Self.Size+21;',
  23219. ' Self.Speed:=Self.Speed+22;',
  23220. ' Self.Left:=Self.Left+23;',
  23221. ' with Self do begin',
  23222. ' Size:=Size+31;',
  23223. ' Speed:=Speed+32;',
  23224. ' Left:=Left+33;',
  23225. ' end;',
  23226. 'end;',
  23227. 'procedure TObjHelper.SetLeft(Value: word);',
  23228. 'begin',
  23229. 'end;',
  23230. 'procedure TBird.DoIt;',
  23231. 'begin',
  23232. ' NotRight:=NotRight+11;',
  23233. ' Self.NotRight:=Self.NotRight+21;',
  23234. ' with Self do begin',
  23235. ' NotRight:=NotRight+31;',
  23236. ' end;',
  23237. 'end;',
  23238. 'begin',
  23239. ' b.Size:=b.Size+11;',
  23240. ' b.Speed:=b.Speed+12;',
  23241. ' b.Left:=b.Left+13;',
  23242. ' b.NotRight:=b.NotRight+14;',
  23243. ' with b do begin',
  23244. ' Size:=Size+31;',
  23245. ' Speed:=Speed+32;',
  23246. ' Left:=Left+33;',
  23247. ' NotRight:=NotRight+34;',
  23248. ' end;',
  23249. '']);
  23250. ConvertProgram;
  23251. CheckSource('TestClassHelper_Property',
  23252. LinesToStr([ // statements
  23253. 'rtl.createClass(this, "TObject", null, function () {',
  23254. ' this.$init = function () {',
  23255. ' this.FSize = 0;',
  23256. ' };',
  23257. ' this.$final = function () {',
  23258. ' };',
  23259. ' this.GetSpeed = function () {',
  23260. ' var Result = 0;',
  23261. ' this.FSize = this.FSize + 11;',
  23262. ' this.SetSpeed(this.GetSpeed() + 12);',
  23263. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  23264. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  23265. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23266. ' this.FSize = this.FSize + 21;',
  23267. ' this.SetSpeed(this.GetSpeed() + 22);',
  23268. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23269. ' this.FSize = this.FSize + 31;',
  23270. ' this.SetSpeed(this.GetSpeed() + 32);',
  23271. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23272. ' return Result;',
  23273. ' };',
  23274. ' this.SetSpeed = function (Value) {',
  23275. ' };',
  23276. '});',
  23277. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23278. ' this.GetLeft = function () {',
  23279. ' var Result = 0;',
  23280. ' this.FSize = this.FSize + 11;',
  23281. ' this.SetSpeed(this.GetSpeed() + 12);',
  23282. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23283. ' this.FSize = this.FSize + 21;',
  23284. ' this.SetSpeed(this.GetSpeed() + 22);',
  23285. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23286. ' this.FSize = this.FSize + 31;',
  23287. ' this.SetSpeed(this.GetSpeed() + 32);',
  23288. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23289. ' return Result;',
  23290. ' };',
  23291. ' this.SetLeft = function (Value) {',
  23292. ' };',
  23293. '});',
  23294. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23295. ' this.DoIt = function () {',
  23296. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23297. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23298. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23299. ' };',
  23300. '});',
  23301. 'this.b = null;',
  23302. '']),
  23303. LinesToStr([ // $mod.$main
  23304. '$mod.b.FSize = $mod.b.FSize + 11;',
  23305. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23306. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23307. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23308. 'var $with = $mod.b;',
  23309. '$with.FSize = $with.FSize + 31;',
  23310. '$with.SetSpeed($with.GetSpeed() + 32);',
  23311. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23312. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23313. '']));
  23314. end;
  23315. procedure TTestModule.TestClassHelper_Property_Array;
  23316. begin
  23317. StartProgram(false);
  23318. Add([
  23319. 'type',
  23320. ' TObject = class',
  23321. ' function GetSpeed(Index: boolean): word;',
  23322. ' procedure SetSpeed(Index: boolean; Value: word);',
  23323. ' end;',
  23324. ' TObjHelper = class helper for TObject',
  23325. ' function GetSize(Index: boolean): word;',
  23326. ' procedure SetSize(Index: boolean; Value: word);',
  23327. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23328. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23329. ' end;',
  23330. ' TBird = class',
  23331. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23332. ' procedure DoIt;',
  23333. ' end;',
  23334. 'var',
  23335. ' b: TBird;',
  23336. 'function Tobject.GetSpeed(Index: boolean): word;',
  23337. 'begin',
  23338. ' Result:=Size[false];',
  23339. ' Size[true]:=Size[false]+11;',
  23340. ' Speed[true]:=Speed[false]+12;',
  23341. ' Self.Size[true]:=Self.Size[false]+21;',
  23342. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23343. ' with Self do begin',
  23344. ' Size[true]:=Size[false]+31;',
  23345. ' Speed[true]:=Speed[false]+32;',
  23346. ' end;',
  23347. 'end;',
  23348. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23349. 'begin',
  23350. 'end;',
  23351. 'function TObjHelper.GetSize(Index: boolean): word;',
  23352. 'begin',
  23353. ' Size[true]:=Size[false]+11;',
  23354. ' Speed[true]:=Speed[false]+12;',
  23355. ' Self.Size[true]:=Self.Size[false]+21;',
  23356. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23357. ' with Self do begin',
  23358. ' Size[true]:=Size[false]+31;',
  23359. ' Speed[true]:=Speed[false]+32;',
  23360. ' end;',
  23361. 'end;',
  23362. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23363. 'begin',
  23364. 'end;',
  23365. 'procedure TBird.DoIt;',
  23366. 'begin',
  23367. ' Items[true]:=Items[false]+11;',
  23368. ' Self.Items[true]:=Self.Items[false]+21;',
  23369. ' with Self do Items[true]:=Items[false]+31;',
  23370. 'end;',
  23371. 'begin',
  23372. ' b.Size[true]:=b.Size[false]+11;',
  23373. ' b.Speed[true]:=b.Speed[false]+12;',
  23374. ' b.Items[true]:=b.Items[false]+13;',
  23375. ' with b do begin',
  23376. ' Size[true]:=Size[false]+21;',
  23377. ' Speed[true]:=Speed[false]+22;',
  23378. ' Items[true]:=Items[false]+23;',
  23379. ' end;',
  23380. '']);
  23381. ConvertProgram;
  23382. CheckSource('TestClassHelper_Property_Array',
  23383. LinesToStr([ // statements
  23384. 'rtl.createClass(this, "TObject", null, function () {',
  23385. ' this.$init = function () {',
  23386. ' };',
  23387. ' this.$final = function () {',
  23388. ' };',
  23389. ' this.GetSpeed = function (Index) {',
  23390. ' var Result = 0;',
  23391. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23392. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23393. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23394. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23395. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23396. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23397. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23398. ' return Result;',
  23399. ' };',
  23400. ' this.SetSpeed = function (Index, Value) {',
  23401. ' };',
  23402. '});',
  23403. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23404. ' this.GetSize = function (Index) {',
  23405. ' var Result = 0;',
  23406. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23407. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23408. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23409. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23410. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23411. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23412. ' return Result;',
  23413. ' };',
  23414. ' this.SetSize = function (Index, Value) {',
  23415. ' };',
  23416. '});',
  23417. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23418. ' this.DoIt = function () {',
  23419. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23420. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23421. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23422. ' };',
  23423. '});',
  23424. 'this.b = null;',
  23425. '']),
  23426. LinesToStr([ // $mod.$main
  23427. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23428. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23429. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23430. 'var $with = $mod.b;',
  23431. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23432. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23433. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23434. '']));
  23435. end;
  23436. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23437. begin
  23438. StartProgram(false);
  23439. Add([
  23440. 'type',
  23441. ' TObject = class',
  23442. ' function GetSpeed(Index: boolean): word;',
  23443. ' procedure SetSpeed(Index: boolean; Value: word);',
  23444. ' end;',
  23445. ' TObjHelper = class helper for TObject',
  23446. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23447. ' end;',
  23448. ' TBird = class',
  23449. ' end;',
  23450. ' TBirdHelper = class helper for TBird',
  23451. ' function GetSize(Index: word): boolean;',
  23452. ' procedure SetSize(Index: word; Value: boolean);',
  23453. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23454. ' end;',
  23455. 'function Tobject.GetSpeed(Index: boolean): word;',
  23456. 'begin',
  23457. ' Self[true]:=Self[false]+1;',
  23458. 'end;',
  23459. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23460. 'begin',
  23461. 'end;',
  23462. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23463. 'begin',
  23464. ' Self[1]:=not Self[2];',
  23465. 'end;',
  23466. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23467. 'begin',
  23468. 'end;',
  23469. 'var',
  23470. ' o: TObject;',
  23471. ' b: TBird;',
  23472. 'begin',
  23473. ' o[true]:=o[false]+1;',
  23474. ' b[3]:=not b[4];',
  23475. '']);
  23476. ConvertProgram;
  23477. CheckSource('TestClassHelper_Property_Array_Default',
  23478. LinesToStr([ // statements
  23479. 'rtl.createClass(this, "TObject", null, function () {',
  23480. ' this.$init = function () {',
  23481. ' };',
  23482. ' this.$final = function () {',
  23483. ' };',
  23484. ' this.GetSpeed = function (Index) {',
  23485. ' var Result = 0;',
  23486. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23487. ' return Result;',
  23488. ' };',
  23489. ' this.SetSpeed = function (Index, Value) {',
  23490. ' };',
  23491. '});',
  23492. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23493. '});',
  23494. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23495. '});',
  23496. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23497. ' this.GetSize = function (Index) {',
  23498. ' var Result = false;',
  23499. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23500. ' return Result;',
  23501. ' };',
  23502. ' this.SetSize = function (Index, Value) {',
  23503. ' };',
  23504. '});',
  23505. 'this.o = null;',
  23506. 'this.b = null;',
  23507. '']),
  23508. LinesToStr([ // $mod.$main
  23509. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23510. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23511. '']));
  23512. end;
  23513. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23514. begin
  23515. StartProgram(false);
  23516. Add([
  23517. 'type',
  23518. ' TObject = class',
  23519. ' end;',
  23520. ' TObjHelper = class helper for TObject',
  23521. ' function GetItems(Index: word): TObject;',
  23522. ' procedure SetItems(Index: word; Value: TObject);',
  23523. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23524. ' end;',
  23525. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23526. 'begin',
  23527. ' Self[1][2]:=Self[3][4];',
  23528. 'end;',
  23529. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23530. 'begin',
  23531. 'end;',
  23532. 'var',
  23533. ' o: TObject;',
  23534. 'begin',
  23535. ' o[1][2]:=o[3][4];',
  23536. '']);
  23537. ConvertProgram;
  23538. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23539. LinesToStr([ // statements
  23540. 'rtl.createClass(this, "TObject", null, function () {',
  23541. ' this.$init = function () {',
  23542. ' };',
  23543. ' this.$final = function () {',
  23544. ' };',
  23545. '});',
  23546. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23547. ' this.GetItems = function (Index) {',
  23548. ' var Result = null;',
  23549. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23550. ' return Result;',
  23551. ' };',
  23552. ' this.SetItems = function (Index, Value) {',
  23553. ' };',
  23554. '});',
  23555. 'this.o = null;',
  23556. '']),
  23557. LinesToStr([ // $mod.$main
  23558. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23559. '']));
  23560. end;
  23561. procedure TTestModule.TestClassHelper_ClassProperty;
  23562. begin
  23563. StartProgram(false);
  23564. Add([
  23565. 'type',
  23566. ' TObject = class',
  23567. ' class var FSize: word;',
  23568. ' class function GetSpeed: word;',
  23569. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23570. ' end;',
  23571. ' TObjHelper = class helper for TObject',
  23572. ' class function GetLeft: word;',
  23573. ' class procedure SetLeft(Value: word);',
  23574. ' class property Size: word read FSize write FSize;',
  23575. ' class property Speed: word read GetSpeed write SetSpeed;',
  23576. ' class property Left: word read GetLeft write SetLeft;',
  23577. ' end;',
  23578. ' TBird = class',
  23579. ' class property NotRight: word read GetLeft write SetLeft;',
  23580. ' class procedure DoIt;',
  23581. ' end;',
  23582. ' TBirdClass = class of TBird;',
  23583. 'class function Tobject.GetSpeed: word;',
  23584. 'begin',
  23585. ' Size:=Size+11;',
  23586. ' Speed:=Speed+12;',
  23587. ' Left:=Left+13;',
  23588. ' Self.Size:=Self.Size+21;',
  23589. ' Self.Speed:=Self.Speed+22;',
  23590. ' Self.Left:=Self.Left+23;',
  23591. ' with Self do begin',
  23592. ' Size:=Size+31;',
  23593. ' Speed:=Speed+32;',
  23594. ' Left:=Left+33;',
  23595. ' end;',
  23596. 'end;',
  23597. 'class function TObjHelper.GetLeft: word;',
  23598. 'begin',
  23599. ' Size:=Size+11;',
  23600. ' Speed:=Speed+12;',
  23601. ' Left:=Left+13;',
  23602. ' Self.Size:=Self.Size+21;',
  23603. ' Self.Speed:=Self.Speed+22;',
  23604. ' Self.Left:=Self.Left+23;',
  23605. ' with Self do begin',
  23606. ' Size:=Size+31;',
  23607. ' Speed:=Speed+32;',
  23608. ' Left:=Left+33;',
  23609. ' end;',
  23610. 'end;',
  23611. 'class procedure TObjHelper.SetLeft(Value: word);',
  23612. 'begin',
  23613. 'end;',
  23614. 'class procedure TBird.DoIt;',
  23615. 'begin',
  23616. ' NotRight:=NotRight+11;',
  23617. ' Self.NotRight:=Self.NotRight+21;',
  23618. ' with Self do NotRight:=NotRight+31;',
  23619. 'end;',
  23620. 'var',
  23621. ' b: TBird;',
  23622. ' c: TBirdClass;',
  23623. 'begin',
  23624. ' b.Size:=b.Size+11;',
  23625. ' b.Speed:=b.Speed+12;',
  23626. ' b.Left:=b.Left+13;',
  23627. ' b.NotRight:=b.NotRight+14;',
  23628. ' with b do begin',
  23629. ' Size:=Size+31;',
  23630. ' Speed:=Speed+32;',
  23631. ' Left:=Left+33;',
  23632. ' NotRight:=NotRight+34;',
  23633. ' end;',
  23634. ' c.Size:=c.Size+11;',
  23635. ' c.Speed:=c.Speed+12;',
  23636. ' c.Left:=c.Left+13;',
  23637. ' c.NotRight:=c.NotRight+14;',
  23638. ' with c do begin',
  23639. ' Size:=Size+31;',
  23640. ' Speed:=Speed+32;',
  23641. ' Left:=Left+33;',
  23642. ' NotRight:=NotRight+34;',
  23643. ' end;',
  23644. ' tbird.Size:=tbird.Size+11;',
  23645. ' tbird.Speed:=tbird.Speed+12;',
  23646. ' tbird.Left:=tbird.Left+13;',
  23647. ' tbird.NotRight:=tbird.NotRight+14;',
  23648. ' with tbird do begin',
  23649. ' Size:=Size+31;',
  23650. ' Speed:=Speed+32;',
  23651. ' Left:=Left+33;',
  23652. ' NotRight:=NotRight+34;',
  23653. ' end;',
  23654. '']);
  23655. ConvertProgram;
  23656. CheckSource('TestClassHelper_ClassProperty',
  23657. LinesToStr([ // statements
  23658. 'rtl.createClass(this, "TObject", null, function () {',
  23659. ' this.FSize = 0;',
  23660. ' this.$init = function () {',
  23661. ' };',
  23662. ' this.$final = function () {',
  23663. ' };',
  23664. ' this.GetSpeed = function () {',
  23665. ' var Result = 0;',
  23666. ' $mod.TObject.FSize = this.FSize + 11;',
  23667. ' this.SetSpeed(this.GetSpeed() + 12);',
  23668. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23669. ' $mod.TObject.FSize = this.FSize + 21;',
  23670. ' this.SetSpeed(this.GetSpeed() + 22);',
  23671. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23672. ' $mod.TObject.FSize = this.FSize + 31;',
  23673. ' this.SetSpeed(this.GetSpeed() + 32);',
  23674. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23675. ' return Result;',
  23676. ' };',
  23677. '});',
  23678. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23679. ' this.GetLeft = function () {',
  23680. ' var Result = 0;',
  23681. ' $mod.TObject.FSize = this.FSize + 11;',
  23682. ' this.SetSpeed(this.GetSpeed() + 12);',
  23683. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23684. ' $mod.TObject.FSize = this.FSize + 21;',
  23685. ' this.SetSpeed(this.GetSpeed() + 22);',
  23686. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23687. ' $mod.TObject.FSize = this.FSize + 31;',
  23688. ' this.SetSpeed(this.GetSpeed() + 32);',
  23689. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23690. ' return Result;',
  23691. ' };',
  23692. ' this.SetLeft = function (Value) {',
  23693. ' };',
  23694. '});',
  23695. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23696. ' this.DoIt = function () {',
  23697. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23698. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23699. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23700. ' };',
  23701. '});',
  23702. 'this.b = null;',
  23703. 'this.c = null;',
  23704. '']),
  23705. LinesToStr([ // $mod.$main
  23706. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23707. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23708. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23709. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23710. 'var $with = $mod.b;',
  23711. '$mod.TObject.FSize = $with.FSize + 31;',
  23712. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23713. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23714. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23715. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23716. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23717. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23718. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23719. 'var $with1 = $mod.c;',
  23720. '$mod.TObject.FSize = $with1.FSize + 31;',
  23721. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23722. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23723. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23724. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23725. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23726. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23727. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23728. 'var $with2 = $mod.TBird;',
  23729. '$mod.TObject.FSize = $with2.FSize + 31;',
  23730. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23731. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23732. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23733. '']));
  23734. end;
  23735. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23736. begin
  23737. StartProgram(false);
  23738. Add([
  23739. 'type',
  23740. ' TObject = class',
  23741. ' class function GetSpeed: word; static;',
  23742. ' class procedure SetSpeed(Value: word); static;',
  23743. ' end;',
  23744. ' TObjHelper = class helper for TObject',
  23745. ' class function GetLeft: word; static;',
  23746. ' class procedure SetLeft(Value: word); static;',
  23747. ' class property Speed: word read GetSpeed write SetSpeed;',
  23748. ' class property Left: word read GetLeft write SetLeft;',
  23749. ' end;',
  23750. ' TBird = class',
  23751. ' class property NotRight: word read GetLeft write SetLeft;',
  23752. ' class procedure DoIt; static;',
  23753. ' class procedure DoSome;',
  23754. ' end;',
  23755. ' TBirdClass = class of TBird;',
  23756. 'class function Tobject.GetSpeed: word;',
  23757. 'begin',
  23758. ' Speed:=Speed+12;',
  23759. ' Left:=Left+13;',
  23760. 'end;',
  23761. 'class procedure TObject.SetSpeed(Value: word);',
  23762. 'begin',
  23763. 'end;',
  23764. 'class function TObjHelper.GetLeft: word;',
  23765. 'begin',
  23766. ' Speed:=Speed+12;',
  23767. ' Left:=Left+13;',
  23768. 'end;',
  23769. 'class procedure TObjHelper.SetLeft(Value: word);',
  23770. 'begin',
  23771. 'end;',
  23772. 'class procedure TBird.DoIt;',
  23773. 'begin',
  23774. ' NotRight:=NotRight+11;',
  23775. 'end;',
  23776. 'class procedure TBird.DoSome;',
  23777. 'begin',
  23778. ' Speed:=Speed+12;',
  23779. ' Left:=Left+13;',
  23780. ' Self.Speed:=Self.Speed+22;',
  23781. ' Self.Left:=Self.Left+23;',
  23782. ' with Self do begin',
  23783. ' Speed:=Speed+32;',
  23784. ' Left:=Left+33;',
  23785. ' end;',
  23786. ' NotRight:=NotRight+11;',
  23787. ' Self.NotRight:=Self.NotRight+21;',
  23788. ' with Self do NotRight:=NotRight+31;',
  23789. 'end;',
  23790. 'var',
  23791. ' b: TBird;',
  23792. ' c: TBirdClass;',
  23793. 'begin',
  23794. ' b.Speed:=b.Speed+12;',
  23795. ' b.Left:=b.Left+13;',
  23796. ' b.NotRight:=b.NotRight+14;',
  23797. ' with b do begin',
  23798. ' Speed:=Speed+32;',
  23799. ' Left:=Left+33;',
  23800. ' NotRight:=NotRight+34;',
  23801. ' end;',
  23802. ' c.Speed:=c.Speed+12;',
  23803. ' c.Left:=c.Left+13;',
  23804. ' c.NotRight:=c.NotRight+14;',
  23805. ' with c do begin',
  23806. ' Speed:=Speed+32;',
  23807. ' Left:=Left+33;',
  23808. ' NotRight:=NotRight+34;',
  23809. ' end;',
  23810. ' tbird.Speed:=tbird.Speed+12;',
  23811. ' tbird.Left:=tbird.Left+13;',
  23812. ' tbird.NotRight:=tbird.NotRight+14;',
  23813. ' with tbird do begin',
  23814. ' Speed:=Speed+32;',
  23815. ' Left:=Left+33;',
  23816. ' NotRight:=NotRight+34;',
  23817. ' end;',
  23818. '']);
  23819. ConvertProgram;
  23820. CheckSource('TestClassHelper_ClassPropertyStatic',
  23821. LinesToStr([ // statements
  23822. 'rtl.createClass(this, "TObject", null, function () {',
  23823. ' this.$init = function () {',
  23824. ' };',
  23825. ' this.$final = function () {',
  23826. ' };',
  23827. ' this.GetSpeed = function () {',
  23828. ' var Result = 0;',
  23829. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23830. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23831. ' return Result;',
  23832. ' };',
  23833. ' this.SetSpeed = function (Value) {',
  23834. ' };',
  23835. '});',
  23836. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23837. ' this.GetLeft = function () {',
  23838. ' var Result = 0;',
  23839. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23840. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23841. ' return Result;',
  23842. ' };',
  23843. ' this.SetLeft = function (Value) {',
  23844. ' };',
  23845. '});',
  23846. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23847. ' this.DoIt = function () {',
  23848. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23849. ' };',
  23850. ' this.DoSome = function () {',
  23851. ' this.SetSpeed(this.GetSpeed() + 12);',
  23852. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23853. ' this.SetSpeed(this.GetSpeed() + 22);',
  23854. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23855. ' this.SetSpeed(this.GetSpeed() + 32);',
  23856. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23857. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23858. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23859. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23860. ' };',
  23861. '});',
  23862. 'this.b = null;',
  23863. 'this.c = null;',
  23864. '']),
  23865. LinesToStr([ // $mod.$main
  23866. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23867. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23868. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23869. 'var $with = $mod.b;',
  23870. '$with.SetSpeed($with.GetSpeed() + 32);',
  23871. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23872. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23873. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23874. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23875. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23876. 'var $with1 = $mod.c;',
  23877. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23878. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23879. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23880. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23881. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23882. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23883. 'var $with2 = $mod.TBird;',
  23884. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23885. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23886. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23887. '']));
  23888. end;
  23889. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  23890. begin
  23891. StartProgram(false);
  23892. Add([
  23893. 'type',
  23894. ' TObject = class',
  23895. ' class function GetSpeed(Index: boolean): word;',
  23896. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  23897. ' end;',
  23898. ' TObjHelper = class helper for TObject',
  23899. ' class function GetSize(Index: boolean): word;',
  23900. ' class procedure SetSize(Index: boolean; Value: word);',
  23901. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  23902. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23903. ' end;',
  23904. ' TBird = class',
  23905. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  23906. ' class procedure DoIt;',
  23907. ' end;',
  23908. ' TBirdClass = class of TBird;',
  23909. 'class function Tobject.GetSpeed(Index: boolean): word;',
  23910. 'begin',
  23911. ' Size[true]:=Size[false]+11;',
  23912. ' Speed[true]:=Speed[false]+12;',
  23913. ' Self.Size[true]:=Self.Size[false]+21;',
  23914. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23915. ' with Self do begin',
  23916. ' Size[true]:=Size[false]+31;',
  23917. ' Speed[true]:=Speed[false]+32;',
  23918. ' end;',
  23919. 'end;',
  23920. 'class function TObjHelper.GetSize(Index: boolean): word;',
  23921. 'begin',
  23922. ' Size[true]:=Size[false]+11;',
  23923. ' Speed[true]:=Speed[false]+12;',
  23924. ' Self.Size[true]:=Self.Size[false]+21;',
  23925. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23926. ' with Self do begin',
  23927. ' Size[true]:=Size[false]+31;',
  23928. ' Speed[true]:=Speed[false]+32;',
  23929. ' end;',
  23930. 'end;',
  23931. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23932. 'begin',
  23933. 'end;',
  23934. 'class procedure TBird.DoIt;',
  23935. 'begin',
  23936. ' Items[true]:=Items[false]+11;',
  23937. ' Self.Items[true]:=Self.Items[false]+21;',
  23938. ' with Self do Items[true]:=Items[false]+31;',
  23939. 'end;',
  23940. 'var',
  23941. ' b: TBird;',
  23942. ' c: TBirdClass;',
  23943. 'begin',
  23944. ' b.Size[true]:=b.Size[false]+11;',
  23945. ' b.Speed[true]:=b.Speed[false]+12;',
  23946. ' b.Items[true]:=b.Items[false]+13;',
  23947. ' with b do begin',
  23948. ' Size[true]:=Size[false]+21;',
  23949. ' Speed[true]:=Speed[false]+22;',
  23950. ' Items[true]:=Items[false]+23;',
  23951. ' end;',
  23952. ' c.Size[true]:=c.Size[false]+11;',
  23953. ' c.Speed[true]:=c.Speed[false]+12;',
  23954. ' c.Items[true]:=c.Items[false]+13;',
  23955. ' with c do begin',
  23956. ' Size[true]:=Size[false]+21;',
  23957. ' Speed[true]:=Speed[false]+22;',
  23958. ' Items[true]:=Items[false]+23;',
  23959. ' end;',
  23960. ' TBird.Size[true]:=TBird.Size[false]+11;',
  23961. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  23962. ' TBird.Items[true]:=TBird.Items[false]+13;',
  23963. ' with TBird do begin',
  23964. ' Size[true]:=Size[false]+21;',
  23965. ' Speed[true]:=Speed[false]+22;',
  23966. ' Items[true]:=Items[false]+23;',
  23967. ' end;',
  23968. '']);
  23969. ConvertProgram;
  23970. CheckSource('TestClassHelper_ClassProperty_Array',
  23971. LinesToStr([ // statements
  23972. 'rtl.createClass(this, "TObject", null, function () {',
  23973. ' this.$init = function () {',
  23974. ' };',
  23975. ' this.$final = function () {',
  23976. ' };',
  23977. ' this.GetSpeed = function (Index) {',
  23978. ' var Result = 0;',
  23979. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23980. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23981. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23982. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23983. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23984. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23985. ' return Result;',
  23986. ' };',
  23987. '});',
  23988. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23989. ' this.GetSize = function (Index) {',
  23990. ' var Result = 0;',
  23991. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23992. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23993. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23994. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23995. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23996. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23997. ' return Result;',
  23998. ' };',
  23999. ' this.SetSize = function (Index, Value) {',
  24000. ' };',
  24001. '});',
  24002. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24003. ' this.DoIt = function () {',
  24004. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24005. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24006. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24007. ' };',
  24008. '});',
  24009. 'this.b = null;',
  24010. 'this.c = null;',
  24011. '']),
  24012. LinesToStr([ // $mod.$main
  24013. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24014. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24015. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24016. 'var $with = $mod.b;',
  24017. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24018. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24019. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24020. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24021. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24022. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24023. 'var $with1 = $mod.c;',
  24024. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24025. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24026. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24027. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24028. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24029. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24030. 'var $with2 = $mod.TBird;',
  24031. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24032. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24033. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24034. '']));
  24035. end;
  24036. procedure TTestModule.TestClassHelper_ForIn;
  24037. begin
  24038. StartProgram(false);
  24039. Add([
  24040. 'type',
  24041. ' TObject = class end;',
  24042. ' TItem = TObject;',
  24043. ' TEnumerator = class',
  24044. ' FCurrent: TItem;',
  24045. ' property Current: TItem read FCurrent;',
  24046. ' function MoveNext: boolean;',
  24047. ' end;',
  24048. ' TBird = class',
  24049. ' end;',
  24050. ' TBirdHelper = class helper for TBird',
  24051. ' function GetEnumerator: TEnumerator;',
  24052. ' end;',
  24053. 'function TEnumerator.MoveNext: boolean;',
  24054. 'begin',
  24055. 'end;',
  24056. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24057. 'begin',
  24058. 'end;',
  24059. 'var',
  24060. ' b: TBird;',
  24061. ' i, i2: TItem;',
  24062. 'begin',
  24063. ' for i in b do i2:=i;']);
  24064. ConvertProgram;
  24065. CheckSource('TestClassHelper_ForIn',
  24066. LinesToStr([ // statements
  24067. 'rtl.createClass(this, "TObject", null, function () {',
  24068. ' this.$init = function () {',
  24069. ' };',
  24070. ' this.$final = function () {',
  24071. ' };',
  24072. '});',
  24073. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24074. ' this.$init = function () {',
  24075. ' $mod.TObject.$init.call(this);',
  24076. ' this.FCurrent = null;',
  24077. ' };',
  24078. ' this.$final = function () {',
  24079. ' this.FCurrent = undefined;',
  24080. ' $mod.TObject.$final.call(this);',
  24081. ' };',
  24082. ' this.MoveNext = function () {',
  24083. ' var Result = false;',
  24084. ' return Result;',
  24085. ' };',
  24086. '});',
  24087. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24088. '});',
  24089. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24090. ' this.GetEnumerator = function () {',
  24091. ' var Result = null;',
  24092. ' return Result;',
  24093. ' };',
  24094. '});',
  24095. 'this.b = null;',
  24096. 'this.i = null;',
  24097. 'this.i2 = null;'
  24098. ]),
  24099. LinesToStr([ // $mod.$main
  24100. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24101. 'try {',
  24102. ' while ($in.MoveNext()){',
  24103. ' $mod.i = $in.FCurrent;',
  24104. ' $mod.i2 = $mod.i;',
  24105. ' }',
  24106. '} finally {',
  24107. ' $in = rtl.freeLoc($in)',
  24108. '};',
  24109. '']));
  24110. end;
  24111. procedure TTestModule.TestClassHelper_PassProperty;
  24112. begin
  24113. StartProgram(false);
  24114. Add([
  24115. 'type',
  24116. ' TObject = class',
  24117. ' FField: TObject;',
  24118. ' property Field: TObject read FField write FField;',
  24119. ' end;',
  24120. ' THelper = class helper for TObject',
  24121. ' procedure Fly;',
  24122. ' class procedure Run;',
  24123. ' class procedure Jump; static;',
  24124. ' end;',
  24125. 'procedure THelper.Fly;',
  24126. 'begin',
  24127. ' Field.Fly;',
  24128. ' Field.Run;',
  24129. ' Field.Jump;',
  24130. ' with Field do begin',
  24131. ' Fly;',
  24132. ' Run;',
  24133. ' Jump;',
  24134. ' end;',
  24135. 'end;',
  24136. 'class procedure THelper.Run;',
  24137. 'begin',
  24138. 'end;',
  24139. 'class procedure THelper.Jump;',
  24140. 'begin',
  24141. 'end;',
  24142. 'var',
  24143. ' b: TObject;',
  24144. 'begin',
  24145. ' b.Field.Fly;',
  24146. ' b.Field.Run;',
  24147. ' b.Field.Jump;',
  24148. ' with b do begin',
  24149. ' Field.Run;',
  24150. ' Field.Fly;',
  24151. ' Field.Jump;',
  24152. ' end;',
  24153. ' with b.Field do begin',
  24154. ' Run;',
  24155. ' Fly;',
  24156. ' Jump;',
  24157. ' end;',
  24158. '']);
  24159. ConvertProgram;
  24160. CheckSource('TestClassHelper_PassProperty',
  24161. LinesToStr([ // statements
  24162. 'rtl.createClass(this, "TObject", null, function () {',
  24163. ' this.$init = function () {',
  24164. ' this.FField = null;',
  24165. ' };',
  24166. ' this.$final = function () {',
  24167. ' this.FField = undefined;',
  24168. ' };',
  24169. '});',
  24170. 'rtl.createHelper(this, "THelper", null, function () {',
  24171. ' this.Fly = function () {',
  24172. ' $mod.THelper.Fly.call(this.FField);',
  24173. ' $mod.THelper.Run.call(this.FField.$class);',
  24174. ' $mod.THelper.Jump();',
  24175. ' var $with = this.FField;',
  24176. ' $mod.THelper.Fly.call($with);',
  24177. ' $mod.THelper.Run.call($with.$class);',
  24178. ' $mod.THelper.Jump();',
  24179. ' };',
  24180. ' this.Run = function () {',
  24181. ' };',
  24182. ' this.Jump = function () {',
  24183. ' };',
  24184. '});',
  24185. 'this.b = null;',
  24186. '']),
  24187. LinesToStr([ // $mod.$main
  24188. '$mod.THelper.Fly.call($mod.b.FField);',
  24189. '$mod.THelper.Run.call($mod.b.FField.$class);',
  24190. '$mod.THelper.Jump();',
  24191. 'var $with = $mod.b;',
  24192. '$mod.THelper.Run.call($with.FField.$class);',
  24193. '$mod.THelper.Fly.call($with.FField);',
  24194. '$mod.THelper.Jump();',
  24195. 'var $with1 = $mod.b.FField;',
  24196. '$mod.THelper.Run.call($with1.$class);',
  24197. '$mod.THelper.Fly.call($with1);',
  24198. '$mod.THelper.Jump();',
  24199. '']));
  24200. end;
  24201. procedure TTestModule.TestExtClassHelper_ClassVar;
  24202. begin
  24203. StartProgram(false);
  24204. Add([
  24205. '{$modeswitch externalclass}',
  24206. 'type',
  24207. ' TExtA = class external name ''ExtObj''',
  24208. ' end;',
  24209. ' THelper = class helper for TExtA',
  24210. ' const',
  24211. ' One = 1;',
  24212. ' Two: word = 2;',
  24213. ' class var',
  24214. ' Glob: word;',
  24215. ' function Foo(w: word): word;',
  24216. ' class function Bar(w: word): word; static;',
  24217. ' end;',
  24218. 'function THelper.foo(w: word): word;',
  24219. 'begin',
  24220. ' Result:=w;',
  24221. ' Two:=One+w;',
  24222. ' Glob:=Glob;',
  24223. ' Result:=Self.Glob;',
  24224. ' Self.Glob:=Self.Glob;',
  24225. ' with Self do Glob:=Glob;',
  24226. 'end;',
  24227. 'class function THelper.bar(w: word): word;',
  24228. 'begin',
  24229. ' Result:=w;',
  24230. ' Two:=One;',
  24231. ' Glob:=Glob;',
  24232. 'end;',
  24233. 'var o: TExtA;',
  24234. 'begin',
  24235. ' texta.two:=texta.one;',
  24236. ' texta.Glob:=texta.Glob;',
  24237. ' with texta do begin',
  24238. ' two:=one;',
  24239. ' Glob:=Glob;',
  24240. ' end;',
  24241. ' o.two:=o.one;',
  24242. ' o.Glob:=o.Glob;',
  24243. ' with o do begin',
  24244. ' two:=one;',
  24245. ' Glob:=Glob;',
  24246. ' end;',
  24247. '']);
  24248. ConvertProgram;
  24249. CheckSource('TestExtClassHelper_ClassVar',
  24250. LinesToStr([ // statements
  24251. 'rtl.createHelper(this, "THelper", null, function () {',
  24252. ' this.One = 1;',
  24253. ' this.Two = 2;',
  24254. ' this.Glob = 0;',
  24255. ' this.Foo = function (w) {',
  24256. ' var Result = 0;',
  24257. ' Result = w;',
  24258. ' $mod.THelper.Two = 1 + w;',
  24259. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24260. ' Result = $mod.THelper.Glob;',
  24261. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24262. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24263. ' return Result;',
  24264. ' };',
  24265. ' this.Bar = function (w) {',
  24266. ' var Result = 0;',
  24267. ' Result = w;',
  24268. ' $mod.THelper.Two = 1;',
  24269. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24270. ' return Result;',
  24271. ' };',
  24272. '});',
  24273. 'this.o = null;',
  24274. '']),
  24275. LinesToStr([ // $mod.$main
  24276. '$mod.THelper.Two = 1;',
  24277. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24278. '$mod.THelper.Two = 1;',
  24279. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24280. '$mod.THelper.Two = 1;',
  24281. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24282. 'var $with = $mod.o;',
  24283. '$mod.THelper.Two = 1;',
  24284. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24285. '']));
  24286. end;
  24287. procedure TTestModule.TestExtClassHelper_Method_Call;
  24288. begin
  24289. StartProgram(false);
  24290. Add([
  24291. '{$modeswitch externalclass}',
  24292. 'type',
  24293. ' TFly = function(w: word): word of object;',
  24294. ' TExtA = class external name ''ExtObj''',
  24295. ' procedure Run(w: word = 10);',
  24296. ' end;',
  24297. ' THelper = class helper for TExtA',
  24298. ' function Foo(w: word = 1): word;',
  24299. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24300. ' end;',
  24301. 'var p: TFly;',
  24302. 'function THelper.foo(w: word): word;',
  24303. 'begin',
  24304. ' Run;',
  24305. ' Run();',
  24306. ' Run(11);',
  24307. ' Foo;',
  24308. ' Foo();',
  24309. ' Foo(12);',
  24310. ' Self.Foo;',
  24311. ' Self.Foo();',
  24312. ' Self.Foo(13);',
  24313. ' Fly;',
  24314. ' Fly();',
  24315. ' with Self do begin',
  24316. ' Foo;',
  24317. ' Foo();',
  24318. ' Foo(14);',
  24319. ' Fly;',
  24320. ' Fly();',
  24321. ' end;',
  24322. ' p:=@Fly;',
  24323. 'end;',
  24324. 'var Obj: TExtA;',
  24325. 'begin',
  24326. ' obj.Foo;',
  24327. ' obj.Foo();',
  24328. ' obj.Foo(21);',
  24329. ' obj.Fly;',
  24330. ' obj.Fly();',
  24331. ' with obj do begin',
  24332. ' Foo;',
  24333. ' Foo();',
  24334. ' Foo(22);',
  24335. ' Fly;',
  24336. ' Fly();',
  24337. ' end;',
  24338. ' p:[email protected];',
  24339. '']);
  24340. ConvertProgram;
  24341. CheckSource('TestExtClassHelper_Method_Call',
  24342. LinesToStr([ // statements
  24343. 'rtl.createHelper(this, "THelper", null, function () {',
  24344. ' this.Foo = function (w) {',
  24345. ' var Result = 0;',
  24346. ' this.Run(10);',
  24347. ' this.Run(10);',
  24348. ' this.Run(11);',
  24349. ' $mod.THelper.Foo.call(this, 1);',
  24350. ' $mod.THelper.Foo.call(this, 1);',
  24351. ' $mod.THelper.Foo.call(this, 12);',
  24352. ' $mod.THelper.Foo.call(this, 1);',
  24353. ' $mod.THelper.Foo.call(this, 1);',
  24354. ' $mod.THelper.Foo.call(this, 13);',
  24355. ' this.Fly(2);',
  24356. ' this.Fly(2);',
  24357. ' $mod.THelper.Foo.call(this, 1);',
  24358. ' $mod.THelper.Foo.call(this, 1);',
  24359. ' $mod.THelper.Foo.call(this, 14);',
  24360. ' this.Fly(2);',
  24361. ' this.Fly(2);',
  24362. ' $mod.p = rtl.createCallback(this, "Fly");',
  24363. ' return Result;',
  24364. ' };',
  24365. '});',
  24366. 'this.p = null;',
  24367. 'this.Obj = null;',
  24368. '']),
  24369. LinesToStr([ // $mod.$main
  24370. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24371. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24372. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24373. '$mod.Obj.Fly(2);',
  24374. '$mod.Obj.Fly(2);',
  24375. 'var $with = $mod.Obj;',
  24376. '$mod.THelper.Foo.call($with, 1);',
  24377. '$mod.THelper.Foo.call($with, 1);',
  24378. '$mod.THelper.Foo.call($with, 22);',
  24379. '$with.Fly(2);',
  24380. '$with.Fly(2);',
  24381. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24382. '']));
  24383. end;
  24384. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24385. begin
  24386. StartProgram(false);
  24387. Add([
  24388. '{$modeswitch externalclass}',
  24389. 'type',
  24390. ' TExtA = class external name ''ExtObj''',
  24391. ' procedure Run(w: word = 10);',
  24392. ' end;',
  24393. ' THelper = class helper for TExtA',
  24394. ' class procedure Fly;',
  24395. ' end;',
  24396. 'class procedure THelper.Fly;',
  24397. 'begin end;',
  24398. 'begin',
  24399. '']);
  24400. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24401. nHelperClassMethodForExtClassMustBeStatic);
  24402. ConvertProgram;
  24403. end;
  24404. procedure TTestModule.TestRecordHelper_ClassVar;
  24405. begin
  24406. StartProgram(false);
  24407. Add([
  24408. 'type',
  24409. ' TRec = record',
  24410. ' end;',
  24411. ' THelper = record helper for TRec',
  24412. ' const',
  24413. ' One = 1;',
  24414. ' Two: word = 2;',
  24415. ' class var',
  24416. ' Glob: word;',
  24417. ' function Foo(w: word): word;',
  24418. ' class function Bar(w: word): word; static;',
  24419. ' end;',
  24420. 'function THelper.foo(w: word): word;',
  24421. 'begin',
  24422. ' Result:=w;',
  24423. ' Two:=One+w;',
  24424. ' Glob:=Glob;',
  24425. ' Result:=Self.Glob;',
  24426. ' Self.Glob:=Self.Glob;',
  24427. ' with Self do Glob:=Glob;',
  24428. ' Self:=Self;',
  24429. 'end;',
  24430. 'class function THelper.bar(w: word): word;',
  24431. 'begin',
  24432. ' Result:=w;',
  24433. ' Two:=One;',
  24434. ' Glob:=Glob;',
  24435. 'end;',
  24436. 'var r: TRec;',
  24437. 'begin',
  24438. ' trec.two:=trec.one;',
  24439. ' trec.Glob:=trec.Glob;',
  24440. ' with trec do begin',
  24441. ' two:=one;',
  24442. ' Glob:=Glob;',
  24443. ' end;',
  24444. ' r.two:=r.one;',
  24445. ' r.Glob:=r.Glob;',
  24446. ' with r do begin',
  24447. ' two:=one;',
  24448. ' Glob:=Glob;',
  24449. ' end;',
  24450. '']);
  24451. ConvertProgram;
  24452. CheckSource('TestRecordHelper_ClassVar',
  24453. LinesToStr([ // statements
  24454. 'rtl.recNewT(this, "TRec", function () {',
  24455. ' this.$eq = function (b) {',
  24456. ' return true;',
  24457. ' };',
  24458. ' this.$assign = function (s) {',
  24459. ' return this;',
  24460. ' };',
  24461. '});',
  24462. 'rtl.createHelper(this, "THelper", null, function () {',
  24463. ' this.One = 1;',
  24464. ' this.Two = 2;',
  24465. ' this.Glob = 0;',
  24466. ' this.Foo = function (w) {',
  24467. ' var Result = 0;',
  24468. ' Result = w;',
  24469. ' $mod.THelper.Two = 1 + w;',
  24470. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24471. ' Result = $mod.THelper.Glob;',
  24472. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24473. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24474. ' this.$assign(this);',
  24475. ' return Result;',
  24476. ' };',
  24477. ' this.Bar = function (w) {',
  24478. ' var Result = 0;',
  24479. ' Result = w;',
  24480. ' $mod.THelper.Two = 1;',
  24481. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24482. ' return Result;',
  24483. ' };',
  24484. '});',
  24485. 'this.r = this.TRec.$new();',
  24486. '']),
  24487. LinesToStr([ // $mod.$main
  24488. '$mod.THelper.Two = 1;',
  24489. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24490. 'var $with = $mod.TRec;',
  24491. '$mod.THelper.Two = 1;',
  24492. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24493. '$mod.THelper.Two = 1;',
  24494. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24495. 'var $with1 = $mod.r;',
  24496. '$mod.THelper.Two = 1;',
  24497. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24498. '']));
  24499. end;
  24500. procedure TTestModule.TestRecordHelper_Method_Call;
  24501. begin
  24502. StartProgram(false);
  24503. Add([
  24504. '{$modeswitch AdvancedRecords}',
  24505. 'type',
  24506. ' TRec = record',
  24507. ' procedure Run(w: word = 10);',
  24508. ' end;',
  24509. ' THelper = record helper for TRec',
  24510. ' function Foo(w: word = 1): word;',
  24511. ' end;',
  24512. 'procedure TRec.Run(w: word);',
  24513. 'begin',
  24514. ' Foo;',
  24515. ' Foo();',
  24516. ' Foo(2);',
  24517. ' Self.Foo;',
  24518. ' Self.Foo();',
  24519. ' Self.Foo(3);',
  24520. ' with Self do begin',
  24521. ' Foo;',
  24522. ' Foo();',
  24523. ' Foo(4);',
  24524. ' end;',
  24525. 'end;',
  24526. 'function THelper.foo(w: word): word;',
  24527. 'begin',
  24528. ' Run;',
  24529. ' Run();',
  24530. ' Run(11);',
  24531. ' Foo;',
  24532. ' Foo();',
  24533. ' Foo(12);',
  24534. ' Self.Foo;',
  24535. ' Self.Foo();',
  24536. ' Self.Foo(13);',
  24537. ' with Self do begin',
  24538. ' Foo;',
  24539. ' Foo();',
  24540. ' Foo(14);',
  24541. ' end;',
  24542. 'end;',
  24543. 'var Rec: TRec;',
  24544. 'begin',
  24545. ' Rec.Foo;',
  24546. ' Rec.Foo();',
  24547. ' Rec.Foo(21);',
  24548. ' with Rec do begin',
  24549. ' Foo;',
  24550. ' Foo();',
  24551. ' Foo(22);',
  24552. ' end;',
  24553. '']);
  24554. ConvertProgram;
  24555. CheckSource('TestRecordHelper_Method_Call',
  24556. LinesToStr([ // statements
  24557. 'rtl.recNewT(this, "TRec", function () {',
  24558. ' this.$eq = function (b) {',
  24559. ' return true;',
  24560. ' };',
  24561. ' this.$assign = function (s) {',
  24562. ' return this;',
  24563. ' };',
  24564. ' this.Run = function (w) {',
  24565. ' $mod.THelper.Foo.call(this, 1);',
  24566. ' $mod.THelper.Foo.call(this, 1);',
  24567. ' $mod.THelper.Foo.call(this, 2);',
  24568. ' $mod.THelper.Foo.call(this, 1);',
  24569. ' $mod.THelper.Foo.call(this, 1);',
  24570. ' $mod.THelper.Foo.call(this, 3);',
  24571. ' $mod.THelper.Foo.call(this, 1);',
  24572. ' $mod.THelper.Foo.call(this, 1);',
  24573. ' $mod.THelper.Foo.call(this, 4);',
  24574. ' };',
  24575. '});',
  24576. 'rtl.createHelper(this, "THelper", null, function () {',
  24577. ' this.Foo = function (w) {',
  24578. ' var Result = 0;',
  24579. ' this.Run(10);',
  24580. ' this.Run(10);',
  24581. ' this.Run(11);',
  24582. ' $mod.THelper.Foo.call(this, 1);',
  24583. ' $mod.THelper.Foo.call(this, 1);',
  24584. ' $mod.THelper.Foo.call(this, 12);',
  24585. ' $mod.THelper.Foo.call(this, 1);',
  24586. ' $mod.THelper.Foo.call(this, 1);',
  24587. ' $mod.THelper.Foo.call(this, 13);',
  24588. ' $mod.THelper.Foo.call(this, 1);',
  24589. ' $mod.THelper.Foo.call(this, 1);',
  24590. ' $mod.THelper.Foo.call(this, 14);',
  24591. ' return Result;',
  24592. ' };',
  24593. '});',
  24594. 'this.Rec = this.TRec.$new();',
  24595. '']),
  24596. LinesToStr([ // $mod.$main
  24597. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24598. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24599. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24600. 'var $with = $mod.Rec;',
  24601. '$mod.THelper.Foo.call($with, 1);',
  24602. '$mod.THelper.Foo.call($with, 1);',
  24603. '$mod.THelper.Foo.call($with, 22);',
  24604. '']));
  24605. end;
  24606. procedure TTestModule.TestRecordHelper_Constructor;
  24607. begin
  24608. StartProgram(false);
  24609. Add([
  24610. '{$modeswitch AdvancedRecords}',
  24611. 'type',
  24612. ' TRec = record',
  24613. ' constructor Create(w: word);',
  24614. ' end;',
  24615. ' THelper = record helper for TRec',
  24616. ' constructor NewHlp(w: word);',
  24617. ' end;',
  24618. 'var',
  24619. ' Rec: TRec;',
  24620. 'constructor TRec.Create(w: word);',
  24621. 'begin',
  24622. ' NewHlp(2);', // normal call
  24623. ' trec.NewHlp(3);', // new instance
  24624. 'end;',
  24625. 'constructor THelper.NewHlp(w: word);',
  24626. 'begin',
  24627. ' create(2);', // normal call
  24628. ' trec.create(3);', // new instance
  24629. ' NewHlp(4);', // normal call
  24630. ' trec.NewHlp(5);', // new instance
  24631. 'end;',
  24632. 'begin',
  24633. ' rec.newhlp(2);', // normal call
  24634. ' with rec do newhlp(12);', // normal call
  24635. ' trec.newhlp(3);', // new instance
  24636. ' with trec do newhlp(13);', // new instance
  24637. '']);
  24638. ConvertProgram;
  24639. CheckSource('TestRecordHelper_Constructor',
  24640. LinesToStr([ // statements
  24641. 'rtl.recNewT(this, "TRec", function () {',
  24642. ' this.$eq = function (b) {',
  24643. ' return true;',
  24644. ' };',
  24645. ' this.$assign = function (s) {',
  24646. ' return this;',
  24647. ' };',
  24648. ' this.Create = function (w) {',
  24649. ' $mod.THelper.NewHlp.call(this, 2);',
  24650. ' $mod.THelper.$new("NewHlp", [3]);',
  24651. ' return this;',
  24652. ' };',
  24653. '});',
  24654. 'rtl.createHelper(this, "THelper", null, function () {',
  24655. ' this.NewHlp = function (w) {',
  24656. ' this.Create(2);',
  24657. ' $mod.TRec.$new().Create(3);',
  24658. ' $mod.THelper.NewHlp.call(this, 4);',
  24659. ' $mod.THelper.$new("NewHlp", [5]);',
  24660. ' return this;',
  24661. ' };',
  24662. ' this.$new = function (fn, args) {',
  24663. ' return this[fn].apply($mod.TRec.$new(), args);',
  24664. ' };',
  24665. '});',
  24666. 'this.Rec = this.TRec.$new();',
  24667. '']),
  24668. LinesToStr([ // $mod.$main
  24669. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24670. 'var $with = $mod.Rec;',
  24671. '$mod.THelper.NewHlp.call($with, 12);',
  24672. '$mod.THelper.$new("NewHlp", [3]);',
  24673. 'var $with1 = $mod.TRec;',
  24674. '$mod.THelper.$new("NewHlp", [13]);',
  24675. '']));
  24676. end;
  24677. procedure TTestModule.TestTypeHelper_ClassVar;
  24678. begin
  24679. StartProgram(false);
  24680. Add([
  24681. '{$modeswitch typehelpers}',
  24682. 'type',
  24683. ' THelper = type helper for byte',
  24684. ' const',
  24685. ' One = 1;',
  24686. ' Two: word = 2;',
  24687. ' class var',
  24688. ' Glob: word;',
  24689. ' function Foo(w: word): word;',
  24690. ' class function Bar(w: word): word; static;',
  24691. ' end;',
  24692. 'function THelper.foo(w: word): word;',
  24693. 'begin',
  24694. ' Result:=w;',
  24695. ' Two:=One+w;',
  24696. ' Glob:=Glob;',
  24697. ' Result:=Self.Glob;',
  24698. ' Self.Glob:=Self.Glob;',
  24699. ' with Self do Glob:=Glob;',
  24700. 'end;',
  24701. 'class function THelper.bar(w: word): word;',
  24702. 'begin',
  24703. ' Result:=w;',
  24704. ' Two:=One;',
  24705. ' Glob:=Glob;',
  24706. 'end;',
  24707. 'var b: byte;',
  24708. 'begin',
  24709. ' byte.two:=byte.one;',
  24710. ' byte.Glob:=byte.Glob;',
  24711. ' with byte do begin',
  24712. ' two:=one;',
  24713. ' Glob:=Glob;',
  24714. ' end;',
  24715. ' b.two:=b.one;',
  24716. ' b.Glob:=b.Glob;',
  24717. ' with b do begin',
  24718. ' two:=one;',
  24719. ' Glob:=Glob;',
  24720. ' end;',
  24721. '']);
  24722. ConvertProgram;
  24723. CheckSource('TestTypeHelper_ClassVar',
  24724. LinesToStr([ // statements
  24725. 'rtl.createHelper(this, "THelper", null, function () {',
  24726. ' this.One = 1;',
  24727. ' this.Two = 2;',
  24728. ' this.Glob = 0;',
  24729. ' this.Foo = function (w) {',
  24730. ' var Result = 0;',
  24731. ' Result = w;',
  24732. ' $mod.THelper.Two = 1 + w;',
  24733. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24734. ' Result = $mod.THelper.Glob;',
  24735. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24736. ' var $with = this.get();',
  24737. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24738. ' return Result;',
  24739. ' };',
  24740. ' this.Bar = function (w) {',
  24741. ' var Result = 0;',
  24742. ' Result = w;',
  24743. ' $mod.THelper.Two = 1;',
  24744. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24745. ' return Result;',
  24746. ' };',
  24747. '});',
  24748. 'this.b = 0;',
  24749. '']),
  24750. LinesToStr([ // $mod.$main
  24751. '$mod.THelper.Two = 1;',
  24752. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24753. '$mod.THelper.Two = 1;',
  24754. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24755. '$mod.THelper.Two = 1;',
  24756. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24757. 'var $with = $mod.b;',
  24758. '$mod.THelper.Two = 1;',
  24759. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24760. '']));
  24761. end;
  24762. procedure TTestModule.TestTypeHelper_PassResultElement;
  24763. begin
  24764. StartProgram(false);
  24765. Add([
  24766. '{$modeswitch typehelpers}',
  24767. 'type',
  24768. ' THelper = type helper for word',
  24769. ' procedure DoIt(e: byte = 123);',
  24770. ' class procedure DoSome(e: byte = 456); static;',
  24771. ' end;',
  24772. 'procedure THelper.DoIt(e: byte);',
  24773. 'begin',
  24774. 'end;',
  24775. 'class procedure THelper.DoSome(e: byte);',
  24776. 'begin',
  24777. 'end;',
  24778. 'function Foo(w: word): word;',
  24779. 'begin',
  24780. ' Result.DoIt;',
  24781. ' Result.DoIt();',
  24782. ' Result.DoSome;',
  24783. ' Result.DoSome();',
  24784. ' with Result do begin',
  24785. ' DoIt;',
  24786. ' DoIt();',
  24787. ' DoSome;',
  24788. ' DoSome();',
  24789. ' end;',
  24790. 'end;',
  24791. 'begin',
  24792. '']);
  24793. ConvertProgram;
  24794. CheckSource('TestTypeHelper_PassResultElement',
  24795. LinesToStr([ // statements
  24796. 'rtl.createHelper(this, "THelper", null, function () {',
  24797. ' this.DoIt = function (e) {',
  24798. ' };',
  24799. ' this.DoSome = function (e) {',
  24800. ' };',
  24801. '});',
  24802. 'this.Foo = function (w) {',
  24803. ' var Result = 0;',
  24804. ' $mod.THelper.DoIt.call({',
  24805. ' get: function () {',
  24806. ' return Result;',
  24807. ' },',
  24808. ' set: function (v) {',
  24809. ' Result = v;',
  24810. ' }',
  24811. ' }, 123);',
  24812. ' $mod.THelper.DoIt.call({',
  24813. ' get: function () {',
  24814. ' return Result;',
  24815. ' },',
  24816. ' set: function (v) {',
  24817. ' Result = v;',
  24818. ' }',
  24819. ' }, 123);',
  24820. ' $mod.THelper.DoSome(456);',
  24821. ' $mod.THelper.DoSome(456);',
  24822. ' $mod.THelper.DoIt.call({',
  24823. ' get: function () {',
  24824. ' return Result;',
  24825. ' },',
  24826. ' set: function (v) {',
  24827. ' Result = v;',
  24828. ' }',
  24829. ' }, 123);',
  24830. ' $mod.THelper.DoIt.call({',
  24831. ' get: function () {',
  24832. ' return Result;',
  24833. ' },',
  24834. ' set: function (v) {',
  24835. ' Result = v;',
  24836. ' }',
  24837. ' }, 123);',
  24838. ' $mod.THelper.DoSome(456);',
  24839. ' $mod.THelper.DoSome(456);',
  24840. ' return Result;',
  24841. '};',
  24842. '']),
  24843. LinesToStr([ // $mod.$main
  24844. '']));
  24845. end;
  24846. procedure TTestModule.TestTypeHelper_PassArgs;
  24847. begin
  24848. StartProgram(false);
  24849. Add([
  24850. '{$modeswitch typehelpers}',
  24851. 'type',
  24852. ' THelper = type helper for word',
  24853. ' procedure DoIt(e: byte = 123);',
  24854. ' end;',
  24855. 'procedure THelper.DoIt(e: byte);',
  24856. 'begin',
  24857. 'end;',
  24858. 'procedure FooDefault(a: word);',
  24859. 'begin',
  24860. ' a.DoIt;',
  24861. ' with a do DoIt;',
  24862. 'end;',
  24863. 'procedure FooConst(const a: word);',
  24864. 'begin',
  24865. ' a.DoIt;',
  24866. ' with a do DoIt;',
  24867. 'end;',
  24868. 'procedure FooVar(var a: word);',
  24869. 'begin',
  24870. ' a.DoIt;',
  24871. ' with a do DoIt;',
  24872. 'end;',
  24873. 'begin',
  24874. '']);
  24875. ConvertProgram;
  24876. CheckSource('TestTypeHelper_PassArgs',
  24877. LinesToStr([ // statements
  24878. 'rtl.createHelper(this, "THelper", null, function () {',
  24879. ' this.DoIt = function (e) {',
  24880. ' };',
  24881. '});',
  24882. 'this.FooDefault = function (a) {',
  24883. ' $mod.THelper.DoIt.call({',
  24884. ' get: function () {',
  24885. ' return a;',
  24886. ' },',
  24887. ' set: function (v) {',
  24888. ' a = v;',
  24889. ' }',
  24890. ' }, 123);',
  24891. ' $mod.THelper.DoIt.call({',
  24892. ' get: function () {',
  24893. ' return a;',
  24894. ' },',
  24895. ' set: function (v) {',
  24896. ' a = v;',
  24897. ' }',
  24898. ' }, 123);',
  24899. '};',
  24900. 'this.FooConst = function (a) {',
  24901. ' $mod.THelper.DoIt.call({',
  24902. ' get: function () {',
  24903. ' return a;',
  24904. ' },',
  24905. ' set: function (v) {',
  24906. ' rtl.raiseE("EPropReadOnly");',
  24907. ' }',
  24908. ' }, 123);',
  24909. ' $mod.THelper.DoIt.call({',
  24910. ' get: function () {',
  24911. ' return a;',
  24912. ' },',
  24913. ' set: function () {',
  24914. ' rtl.raiseE("EPropReadOnly");',
  24915. ' }',
  24916. ' }, 123);',
  24917. '};',
  24918. 'this.FooVar = function (a) {',
  24919. ' $mod.THelper.DoIt.call(a, 123);',
  24920. ' var $with = a.get();',
  24921. ' $mod.THelper.DoIt.call(a, 123);',
  24922. '};',
  24923. '']),
  24924. LinesToStr([ // $mod.$main
  24925. '']));
  24926. end;
  24927. procedure TTestModule.TestTypeHelper_PassVarConst;
  24928. begin
  24929. StartProgram(false);
  24930. Add([
  24931. '{$modeswitch typehelpers}',
  24932. 'type',
  24933. ' THelper = type helper for word',
  24934. ' procedure DoIt(e: byte = 123);',
  24935. ' end;',
  24936. 'procedure THelper.DoIt(e: byte);',
  24937. 'begin',
  24938. 'end;',
  24939. 'var a: word;',
  24940. 'const c: word = 2;',
  24941. '{$writeableconst off}',
  24942. 'const r: word = 3;',
  24943. 'begin',
  24944. ' a.DoIt;',
  24945. ' with a do DoIt;',
  24946. ' c.DoIt;',
  24947. ' with c do DoIt;',
  24948. ' r.DoIt;',
  24949. ' with r do DoIt;',
  24950. '']);
  24951. ConvertProgram;
  24952. CheckSource('TestTypeHelper_PassVarConst',
  24953. LinesToStr([ // statements
  24954. 'rtl.createHelper(this, "THelper", null, function () {',
  24955. ' this.DoIt = function (e) {',
  24956. ' };',
  24957. '});',
  24958. 'this.a = 0;',
  24959. 'this.c = 2;',
  24960. 'this.r = 3;',
  24961. '']),
  24962. LinesToStr([ // $mod.$main
  24963. '$mod.THelper.DoIt.call({',
  24964. ' p: $mod,',
  24965. ' get: function () {',
  24966. ' return this.p.a;',
  24967. ' },',
  24968. ' set: function (v) {',
  24969. ' this.p.a = v;',
  24970. ' }',
  24971. '}, 123);',
  24972. 'var $with = $mod.a;',
  24973. '$mod.THelper.DoIt.call({',
  24974. ' get: function () {',
  24975. ' return $with;',
  24976. ' },',
  24977. ' set: function (v) {',
  24978. ' $with = v;',
  24979. ' }',
  24980. '}, 123);',
  24981. '$mod.THelper.DoIt.call({',
  24982. ' p: $mod,',
  24983. ' get: function () {',
  24984. ' return this.p.c;',
  24985. ' },',
  24986. ' set: function (v) {',
  24987. ' this.p.c = v;',
  24988. ' }',
  24989. '}, 123);',
  24990. 'var $with1 = $mod.c;',
  24991. '$mod.THelper.DoIt.call({',
  24992. ' get: function () {',
  24993. ' return $with1;',
  24994. ' },',
  24995. ' set: function (v) {',
  24996. ' $with1 = v;',
  24997. ' }',
  24998. '}, 123);',
  24999. '$mod.THelper.DoIt.call({',
  25000. ' get: function () {',
  25001. ' return 3;',
  25002. ' },',
  25003. ' set: function (v) {',
  25004. ' rtl.raiseE("EPropReadOnly");',
  25005. ' }',
  25006. '}, 123);',
  25007. 'var $with2 = 3;',
  25008. ' $mod.THelper.DoIt.call({',
  25009. ' get: function () {',
  25010. ' return $with2;',
  25011. ' },',
  25012. ' set: function () {',
  25013. ' rtl.raiseE("EPropReadOnly");',
  25014. ' }',
  25015. ' }, 123);',
  25016. '']));
  25017. end;
  25018. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25019. begin
  25020. StartProgram(false);
  25021. Add([
  25022. '{$modeswitch typehelpers}',
  25023. 'type',
  25024. ' THelper = type helper for word',
  25025. ' procedure DoIt(e: byte = 123);',
  25026. ' end;',
  25027. 'procedure THelper.DoIt(e: byte);',
  25028. 'begin',
  25029. 'end;',
  25030. 'function Foo(b: byte = 1): word;',
  25031. 'begin',
  25032. 'end;',
  25033. 'begin',
  25034. ' Foo.DoIt;',
  25035. ' Foo().DoIt;',
  25036. ' with Foo do DoIt;',
  25037. ' with Foo() do DoIt;',
  25038. '']);
  25039. ConvertProgram;
  25040. CheckSource('TestTypeHelper_PassFuncResult',
  25041. LinesToStr([ // statements
  25042. 'rtl.createHelper(this, "THelper", null, function () {',
  25043. ' this.DoIt = function (e) {',
  25044. ' };',
  25045. '});',
  25046. 'this.Foo = function (b) {',
  25047. ' var Result = 0;',
  25048. ' return Result;',
  25049. '};',
  25050. '']),
  25051. LinesToStr([ // $mod.$main
  25052. '$mod.THelper.DoIt.call({',
  25053. ' a: $mod.Foo(1),',
  25054. ' get: function () {',
  25055. ' return this.a;',
  25056. ' },',
  25057. ' set: function (v) {',
  25058. ' this.a = v;',
  25059. ' }',
  25060. '}, 123);',
  25061. '$mod.THelper.DoIt.call({',
  25062. ' a: $mod.Foo(1),',
  25063. ' get: function () {',
  25064. ' return this.a;',
  25065. ' },',
  25066. ' set: function (v) {',
  25067. ' this.a = v;',
  25068. ' }',
  25069. '}, 123);',
  25070. 'var $with = $mod.Foo(1);',
  25071. '$mod.THelper.DoIt.call({',
  25072. ' get: function () {',
  25073. ' return $with;',
  25074. ' },',
  25075. ' set: function (v) {',
  25076. ' $with = v;',
  25077. ' }',
  25078. '}, 123);',
  25079. 'var $with1 = $mod.Foo(1);',
  25080. '$mod.THelper.DoIt.call({',
  25081. ' get: function () {',
  25082. ' return $with1;',
  25083. ' },',
  25084. ' set: function (v) {',
  25085. ' $with1 = v;',
  25086. ' }',
  25087. '}, 123);',
  25088. '']));
  25089. end;
  25090. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25091. begin
  25092. StartProgram(false);
  25093. Add([
  25094. '{$modeswitch typehelpers}',
  25095. 'type',
  25096. ' TObject = class',
  25097. ' FField: word;',
  25098. ' procedure SetField(Value: word);',
  25099. ' property Field: word read FField write SetField;',
  25100. ' end;',
  25101. ' THelper = type helper for word',
  25102. ' procedure Fly;',
  25103. ' class procedure Run; static;',
  25104. ' end;',
  25105. 'procedure TObject.SetField(Value: word);',
  25106. 'begin',
  25107. ' Field.Fly;',
  25108. ' Field.Run;',
  25109. ' Self.Field.Fly;',
  25110. ' Self.Field.Run;',
  25111. ' with Self do begin',
  25112. ' Field.Fly;',
  25113. ' Field.Run;',
  25114. ' end;',
  25115. ' with Self.Field do begin',
  25116. ' Fly;',
  25117. ' Run;',
  25118. ' end;',
  25119. 'end;',
  25120. 'procedure THelper.Fly;',
  25121. 'begin',
  25122. 'end;',
  25123. 'class procedure THelper.Run;',
  25124. 'begin',
  25125. 'end;',
  25126. 'var',
  25127. ' o: TObject;',
  25128. 'begin',
  25129. ' o.Field.Fly;',
  25130. ' o.Field.Run;',
  25131. ' with o do begin',
  25132. ' Field.Fly;',
  25133. ' Field.Run;',
  25134. ' end;',
  25135. ' with o.Field do begin',
  25136. ' Fly;',
  25137. ' Run;',
  25138. ' end;',
  25139. '']);
  25140. ConvertProgram;
  25141. CheckSource('TestTypeHelper_PassPropertyField',
  25142. LinesToStr([ // statements
  25143. 'rtl.createClass(this, "TObject", null, function () {',
  25144. ' this.$init = function () {',
  25145. ' this.FField = 0;',
  25146. ' };',
  25147. ' this.$final = function () {',
  25148. ' };',
  25149. ' this.SetField = function (Value) {',
  25150. ' $mod.THelper.Fly.call({',
  25151. ' p: this,',
  25152. ' get: function () {',
  25153. ' return this.p.FField;',
  25154. ' },',
  25155. ' set: function (v) {',
  25156. ' this.p.FField = v;',
  25157. ' }',
  25158. ' });',
  25159. ' $mod.THelper.Run();',
  25160. ' $mod.THelper.Fly.call({',
  25161. ' p: this,',
  25162. ' get: function () {',
  25163. ' return this.p.FField;',
  25164. ' },',
  25165. ' set: function (v) {',
  25166. ' this.p.FField = v;',
  25167. ' }',
  25168. ' });',
  25169. ' $mod.THelper.Run();',
  25170. ' $mod.THelper.Fly.call({',
  25171. ' p: this,',
  25172. ' get: function () {',
  25173. ' return this.p.FField;',
  25174. ' },',
  25175. ' set: function (v) {',
  25176. ' this.p.FField = v;',
  25177. ' }',
  25178. ' });',
  25179. ' $mod.THelper.Run();',
  25180. ' var $with = this.FField;',
  25181. ' $mod.THelper.Fly.call({',
  25182. ' get: function () {',
  25183. ' return $with;',
  25184. ' },',
  25185. ' set: function (v) {',
  25186. ' $with = v;',
  25187. ' }',
  25188. ' });',
  25189. ' $mod.THelper.Run();',
  25190. ' };',
  25191. '});',
  25192. 'rtl.createHelper(this, "THelper", null, function () {',
  25193. ' this.Fly = function () {',
  25194. ' };',
  25195. ' this.Run = function () {',
  25196. ' };',
  25197. '});',
  25198. 'this.o = null;',
  25199. '']),
  25200. LinesToStr([ // $mod.$main
  25201. '$mod.THelper.Fly.call({',
  25202. ' p: $mod.o,',
  25203. ' get: function () {',
  25204. ' return this.p.FField;',
  25205. ' },',
  25206. ' set: function (v) {',
  25207. ' this.p.FField = v;',
  25208. ' }',
  25209. '});',
  25210. '$mod.THelper.Run();',
  25211. 'var $with = $mod.o;',
  25212. '$mod.THelper.Fly.call({',
  25213. ' p: $with,',
  25214. ' get: function () {',
  25215. ' return this.p.FField;',
  25216. ' },',
  25217. ' set: function (v) {',
  25218. ' this.p.FField = v;',
  25219. ' }',
  25220. '});',
  25221. '$mod.THelper.Run();',
  25222. 'var $with1 = $mod.o.FField;',
  25223. '$mod.THelper.Fly.call({',
  25224. ' get: function () {',
  25225. ' return $with1;',
  25226. ' },',
  25227. ' set: function (v) {',
  25228. ' $with1 = v;',
  25229. ' }',
  25230. '});',
  25231. '$mod.THelper.Run();',
  25232. '']));
  25233. end;
  25234. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  25235. begin
  25236. StartProgram(false);
  25237. Add([
  25238. '{$modeswitch typehelpers}',
  25239. 'type',
  25240. ' TObject = class',
  25241. ' FField: word;',
  25242. ' function GetField: word;',
  25243. ' property Field: word read GetField write FField;',
  25244. ' end;',
  25245. ' THelper = type helper for word',
  25246. ' procedure Fly;',
  25247. ' class procedure Run; static;',
  25248. ' end;',
  25249. 'function TObject.GetField: word;',
  25250. 'begin',
  25251. ' Field.Fly;',
  25252. ' Field.Run;',
  25253. ' Self.Field.Fly;',
  25254. ' Self.Field.Run;',
  25255. ' with Self do begin',
  25256. ' Field.Fly;',
  25257. ' Field.Run;',
  25258. ' end;',
  25259. ' with Self.Field do begin',
  25260. ' Fly;',
  25261. ' Run;',
  25262. ' end;',
  25263. 'end;',
  25264. 'procedure THelper.Fly;',
  25265. 'begin',
  25266. 'end;',
  25267. 'class procedure THelper.Run;',
  25268. 'begin',
  25269. 'end;',
  25270. 'var',
  25271. ' o: TObject;',
  25272. 'begin',
  25273. ' o.Field.Fly;',
  25274. ' o.Field.Run;',
  25275. ' with o do begin',
  25276. ' Field.Fly;',
  25277. ' Field.Run;',
  25278. ' end;',
  25279. ' with o.Field do begin',
  25280. ' Fly;',
  25281. ' Run;',
  25282. ' end;',
  25283. '']);
  25284. ConvertProgram;
  25285. CheckSource('TestTypeHelper_PassPropertyGetter',
  25286. LinesToStr([ // statements
  25287. 'rtl.createClass(this, "TObject", null, function () {',
  25288. ' this.$init = function () {',
  25289. ' this.FField = 0;',
  25290. ' };',
  25291. ' this.$final = function () {',
  25292. ' };',
  25293. ' this.GetField = function () {',
  25294. ' var Result = 0;',
  25295. ' $mod.THelper.Fly.call({',
  25296. ' p: this.GetField(),',
  25297. ' get: function () {',
  25298. ' return this.p;',
  25299. ' },',
  25300. ' set: function (v) {',
  25301. ' this.p = v;',
  25302. ' }',
  25303. ' });',
  25304. ' $mod.THelper.Run();',
  25305. ' $mod.THelper.Fly.call({',
  25306. ' p: this.GetField(),',
  25307. ' get: function () {',
  25308. ' return this.p;',
  25309. ' },',
  25310. ' set: function (v) {',
  25311. ' this.p = v;',
  25312. ' }',
  25313. ' });',
  25314. ' $mod.THelper.Run();',
  25315. ' $mod.THelper.Fly.call({',
  25316. ' p: this.GetField(),',
  25317. ' get: function () {',
  25318. ' return this.p;',
  25319. ' },',
  25320. ' set: function (v) {',
  25321. ' this.p = v;',
  25322. ' }',
  25323. ' });',
  25324. ' $mod.THelper.Run();',
  25325. ' var $with = this.GetField();',
  25326. ' $mod.THelper.Fly.call({',
  25327. ' get: function () {',
  25328. ' return $with;',
  25329. ' },',
  25330. ' set: function (v) {',
  25331. ' $with = v;',
  25332. ' }',
  25333. ' });',
  25334. ' $mod.THelper.Run();',
  25335. ' return Result;',
  25336. ' };',
  25337. '});',
  25338. 'rtl.createHelper(this, "THelper", null, function () {',
  25339. ' this.Fly = function () {',
  25340. ' };',
  25341. ' this.Run = function () {',
  25342. ' };',
  25343. '});',
  25344. 'this.o = null;',
  25345. '']),
  25346. LinesToStr([ // $mod.$main
  25347. '$mod.THelper.Fly.call({',
  25348. ' p: $mod.o.GetField(),',
  25349. ' get: function () {',
  25350. ' return this.p;',
  25351. ' },',
  25352. ' set: function (v) {',
  25353. ' this.p = v;',
  25354. ' }',
  25355. '});',
  25356. '$mod.THelper.Run();',
  25357. 'var $with = $mod.o;',
  25358. '$mod.THelper.Fly.call({',
  25359. ' p: $with.GetField(),',
  25360. ' get: function () {',
  25361. ' return this.p;',
  25362. ' },',
  25363. ' set: function (v) {',
  25364. ' this.p = v;',
  25365. ' }',
  25366. '});',
  25367. '$mod.THelper.Run();',
  25368. 'var $with1 = $mod.o.GetField();',
  25369. '$mod.THelper.Fly.call({',
  25370. ' get: function () {',
  25371. ' return $with1;',
  25372. ' },',
  25373. ' set: function (v) {',
  25374. ' $with1 = v;',
  25375. ' }',
  25376. '});',
  25377. '$mod.THelper.Run();',
  25378. '']));
  25379. end;
  25380. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25381. begin
  25382. StartProgram(false);
  25383. Add([
  25384. '{$modeswitch typehelpers}',
  25385. 'type',
  25386. ' TObject = class',
  25387. ' class var FField: word;',
  25388. ' class procedure SetField(Value: word);',
  25389. ' class property Field: word read FField write SetField;',
  25390. ' end;',
  25391. ' THelper = type helper for word',
  25392. ' procedure Fly(n: byte);',
  25393. ' end;',
  25394. 'class procedure TObject.SetField(Value: word);',
  25395. 'begin',
  25396. ' Field.Fly(1);',
  25397. ' Self.Field.Fly(2);',
  25398. ' with Self do Field.Fly(3);',
  25399. ' with Self.Field do Fly(4);',
  25400. ' TObject.Field.Fly(5);',
  25401. ' with TObject do Field.Fly(6);',
  25402. ' with TObject.Field do Fly(7);',
  25403. 'end;',
  25404. 'procedure THelper.Fly(n: byte);',
  25405. 'begin',
  25406. 'end;',
  25407. 'var',
  25408. ' o: TObject;',
  25409. 'begin',
  25410. ' o.Field.Fly(11);',
  25411. ' with o do Field.Fly(12);',
  25412. ' with o.Field do Fly(13);',
  25413. ' TObject.Field.Fly(14);',
  25414. ' with TObject do Field.Fly(15);',
  25415. ' with TObject.Field do Fly(16);',
  25416. '']);
  25417. ConvertProgram;
  25418. CheckSource('TestTypeHelper_PassClassPropertyField',
  25419. LinesToStr([ // statements
  25420. 'rtl.createClass(this, "TObject", null, function () {',
  25421. ' this.FField = 0;',
  25422. ' this.$init = function () {',
  25423. ' };',
  25424. ' this.$final = function () {',
  25425. ' };',
  25426. ' this.SetField = function (Value) {',
  25427. ' $mod.THelper.Fly.call({',
  25428. ' p: this,',
  25429. ' get: function () {',
  25430. ' return this.p.FField;',
  25431. ' },',
  25432. ' set: function (v) {',
  25433. ' $mod.TObject.FField = v;',
  25434. ' }',
  25435. ' }, 1);',
  25436. ' $mod.THelper.Fly.call({',
  25437. ' p: this,',
  25438. ' get: function () {',
  25439. ' return this.p.FField;',
  25440. ' },',
  25441. ' set: function (v) {',
  25442. ' $mod.TObject.FField = v;',
  25443. ' }',
  25444. ' }, 2);',
  25445. ' $mod.THelper.Fly.call({',
  25446. ' p: this,',
  25447. ' get: function () {',
  25448. ' return this.p.FField;',
  25449. ' },',
  25450. ' set: function (v) {',
  25451. ' $mod.TObject.FField = v;',
  25452. ' }',
  25453. ' }, 3);',
  25454. ' var $with = this.FField;',
  25455. ' $mod.THelper.Fly.call({',
  25456. ' get: function () {',
  25457. ' return $with;',
  25458. ' },',
  25459. ' set: function (v) {',
  25460. ' $with = v;',
  25461. ' }',
  25462. ' }, 4);',
  25463. ' $mod.THelper.Fly.call({',
  25464. ' p: $mod.TObject,',
  25465. ' get: function () {',
  25466. ' return this.p.FField;',
  25467. ' },',
  25468. ' set: function (v) {',
  25469. ' $mod.TObject.FField = v;',
  25470. ' }',
  25471. ' }, 5);',
  25472. ' var $with1 = $mod.TObject;',
  25473. ' $mod.THelper.Fly.call({',
  25474. ' p: $with1,',
  25475. ' get: function () {',
  25476. ' return this.p.FField;',
  25477. ' },',
  25478. ' set: function (v) {',
  25479. ' $mod.TObject.FField = v;',
  25480. ' }',
  25481. ' }, 6);',
  25482. ' var $with2 = $mod.TObject.FField;',
  25483. ' $mod.THelper.Fly.call({',
  25484. ' get: function () {',
  25485. ' return $with2;',
  25486. ' },',
  25487. ' set: function (v) {',
  25488. ' $with2 = v;',
  25489. ' }',
  25490. ' }, 7);',
  25491. ' };',
  25492. '});',
  25493. 'rtl.createHelper(this, "THelper", null, function () {',
  25494. ' this.Fly = function (n) {',
  25495. ' };',
  25496. '});',
  25497. 'this.o = null;',
  25498. '']),
  25499. LinesToStr([ // $mod.$main
  25500. '$mod.THelper.Fly.call({',
  25501. ' p: $mod.o,',
  25502. ' get: function () {',
  25503. ' return this.p.FField;',
  25504. ' },',
  25505. ' set: function (v) {',
  25506. ' $mod.TObject.FField = v;',
  25507. ' }',
  25508. '}, 11);',
  25509. 'var $with = $mod.o;',
  25510. '$mod.THelper.Fly.call({',
  25511. ' p: $with,',
  25512. ' get: function () {',
  25513. ' return this.p.FField;',
  25514. ' },',
  25515. ' set: function (v) {',
  25516. ' $mod.TObject.FField = v;',
  25517. ' }',
  25518. '}, 12);',
  25519. 'var $with1 = $mod.o.FField;',
  25520. '$mod.THelper.Fly.call({',
  25521. ' get: function () {',
  25522. ' return $with1;',
  25523. ' },',
  25524. ' set: function (v) {',
  25525. ' $with1 = v;',
  25526. ' }',
  25527. '}, 13);',
  25528. '$mod.THelper.Fly.call({',
  25529. ' p: $mod.TObject,',
  25530. ' get: function () {',
  25531. ' return this.p.FField;',
  25532. ' },',
  25533. ' set: function (v) {',
  25534. ' $mod.TObject.FField = v;',
  25535. ' }',
  25536. '}, 14);',
  25537. 'var $with2 = $mod.TObject;',
  25538. '$mod.THelper.Fly.call({',
  25539. ' p: $with2,',
  25540. ' get: function () {',
  25541. ' return this.p.FField;',
  25542. ' },',
  25543. ' set: function (v) {',
  25544. ' $mod.TObject.FField = v;',
  25545. ' }',
  25546. '}, 15);',
  25547. 'var $with3 = $mod.TObject.FField;',
  25548. '$mod.THelper.Fly.call({',
  25549. ' get: function () {',
  25550. ' return $with3;',
  25551. ' },',
  25552. ' set: function (v) {',
  25553. ' $with3 = v;',
  25554. ' }',
  25555. '}, 16);',
  25556. '']));
  25557. end;
  25558. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25559. begin
  25560. StartProgram(false);
  25561. Add([
  25562. '{$modeswitch typehelpers}',
  25563. 'type',
  25564. ' TObject = class',
  25565. ' class var FField: word;',
  25566. ' class function GetField: word; static;',
  25567. ' class property Field: word read GetField write FField;',
  25568. ' end;',
  25569. ' THelper = type helper for word',
  25570. ' procedure Fly(n: byte);',
  25571. ' end;',
  25572. 'class function TObject.GetField: word;',
  25573. 'begin',
  25574. ' Field.Fly(1);',
  25575. ' TObject.Field.Fly(5);',
  25576. ' with TObject do Field.Fly(6);',
  25577. ' with TObject.Field do Fly(7);',
  25578. 'end;',
  25579. 'procedure THelper.Fly(n: byte);',
  25580. 'begin',
  25581. 'end;',
  25582. 'var',
  25583. ' o: TObject;',
  25584. 'begin',
  25585. ' o.Field.Fly(11);',
  25586. ' with o do Field.Fly(12);',
  25587. ' with o.Field do Fly(13);',
  25588. '']);
  25589. ConvertProgram;
  25590. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25591. LinesToStr([ // statements
  25592. 'rtl.createClass(this, "TObject", null, function () {',
  25593. ' this.FField = 0;',
  25594. ' this.$init = function () {',
  25595. ' };',
  25596. ' this.$final = function () {',
  25597. ' };',
  25598. ' this.GetField = function () {',
  25599. ' var Result = 0;',
  25600. ' $mod.THelper.Fly.call({',
  25601. ' p: $mod.TObject.GetField(),',
  25602. ' get: function () {',
  25603. ' return this.p;',
  25604. ' },',
  25605. ' set: function (v) {',
  25606. ' this.p = v;',
  25607. ' }',
  25608. ' }, 1);',
  25609. ' $mod.THelper.Fly.call({',
  25610. ' p: $mod.TObject.GetField(),',
  25611. ' get: function () {',
  25612. ' return this.p;',
  25613. ' },',
  25614. ' set: function (v) {',
  25615. ' this.p = v;',
  25616. ' }',
  25617. ' }, 5);',
  25618. ' var $with = $mod.TObject;',
  25619. ' $mod.THelper.Fly.call({',
  25620. ' p: $with.GetField(),',
  25621. ' get: function () {',
  25622. ' return this.p;',
  25623. ' },',
  25624. ' set: function (v) {',
  25625. ' this.p = v;',
  25626. ' }',
  25627. ' }, 6);',
  25628. ' var $with1 = $mod.TObject.GetField();',
  25629. ' $mod.THelper.Fly.call({',
  25630. ' get: function () {',
  25631. ' return $with1;',
  25632. ' },',
  25633. ' set: function (v) {',
  25634. ' $with1 = v;',
  25635. ' }',
  25636. ' }, 7);',
  25637. ' return Result;',
  25638. ' };',
  25639. '});',
  25640. 'rtl.createHelper(this, "THelper", null, function () {',
  25641. ' this.Fly = function (n) {',
  25642. ' };',
  25643. '});',
  25644. 'this.o = null;',
  25645. '']),
  25646. LinesToStr([ // $mod.$main
  25647. '$mod.THelper.Fly.call({',
  25648. ' p: $mod.TObject.GetField(),',
  25649. ' get: function () {',
  25650. ' return this.p;',
  25651. ' },',
  25652. ' set: function (v) {',
  25653. ' this.p = v;',
  25654. ' }',
  25655. '}, 11);',
  25656. 'var $with = $mod.o;',
  25657. '$mod.THelper.Fly.call({',
  25658. ' p: $with.GetField(),',
  25659. ' get: function () {',
  25660. ' return this.p;',
  25661. ' },',
  25662. ' set: function (v) {',
  25663. ' this.p = v;',
  25664. ' }',
  25665. '}, 12);',
  25666. 'var $with1 = $mod.TObject.GetField();',
  25667. '$mod.THelper.Fly.call({',
  25668. ' get: function () {',
  25669. ' return $with1;',
  25670. ' },',
  25671. ' set: function (v) {',
  25672. ' $with1 = v;',
  25673. ' }',
  25674. '}, 13);',
  25675. '']));
  25676. end;
  25677. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25678. begin
  25679. StartProgram(false);
  25680. Add([
  25681. '{$modeswitch typehelpers}',
  25682. 'type',
  25683. ' TObject = class',
  25684. ' class var FField: word;',
  25685. ' class function GetField: word;',
  25686. ' class property Field: word read GetField write FField;',
  25687. ' end;',
  25688. ' TClass = class of TObject;',
  25689. ' THelper = type helper for word',
  25690. ' procedure Fly(n: byte);',
  25691. ' end;',
  25692. 'class function TObject.GetField: word;',
  25693. 'begin',
  25694. ' Field.Fly(1);',
  25695. ' Self.Field.Fly(5);',
  25696. ' with Self do Field.Fly(6);',
  25697. ' with Self.Field do Fly(7);',
  25698. 'end;',
  25699. 'procedure THelper.Fly(n: byte);',
  25700. 'begin',
  25701. 'end;',
  25702. 'var',
  25703. ' o: TObject;',
  25704. ' c: TClass;',
  25705. 'begin',
  25706. ' o.Field.Fly(11);',
  25707. ' with o do Field.Fly(12);',
  25708. ' with o.Field do Fly(13);',
  25709. ' c.Field.Fly(14);',
  25710. ' with c do Field.Fly(15);',
  25711. ' with c.Field do Fly(16);',
  25712. '']);
  25713. ConvertProgram;
  25714. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25715. LinesToStr([ // statements
  25716. 'rtl.createClass(this, "TObject", null, function () {',
  25717. ' this.FField = 0;',
  25718. ' this.$init = function () {',
  25719. ' };',
  25720. ' this.$final = function () {',
  25721. ' };',
  25722. ' this.GetField = function () {',
  25723. ' var Result = 0;',
  25724. ' $mod.THelper.Fly.call({',
  25725. ' p: this.GetField(),',
  25726. ' get: function () {',
  25727. ' return this.p;',
  25728. ' },',
  25729. ' set: function (v) {',
  25730. ' this.p = v;',
  25731. ' }',
  25732. ' }, 1);',
  25733. ' $mod.THelper.Fly.call({',
  25734. ' p: this.GetField(),',
  25735. ' get: function () {',
  25736. ' return this.p;',
  25737. ' },',
  25738. ' set: function (v) {',
  25739. ' this.p = v;',
  25740. ' }',
  25741. ' }, 5);',
  25742. ' $mod.THelper.Fly.call({',
  25743. ' p: this.GetField(),',
  25744. ' get: function () {',
  25745. ' return this.p;',
  25746. ' },',
  25747. ' set: function (v) {',
  25748. ' this.p = v;',
  25749. ' }',
  25750. ' }, 6);',
  25751. ' var $with = this.GetField();',
  25752. ' $mod.THelper.Fly.call({',
  25753. ' get: function () {',
  25754. ' return $with;',
  25755. ' },',
  25756. ' set: function (v) {',
  25757. ' $with = v;',
  25758. ' }',
  25759. ' }, 7);',
  25760. ' return Result;',
  25761. ' };',
  25762. '});',
  25763. 'rtl.createHelper(this, "THelper", null, function () {',
  25764. ' this.Fly = function (n) {',
  25765. ' };',
  25766. '});',
  25767. 'this.o = null;',
  25768. 'this.c = null;',
  25769. '']),
  25770. LinesToStr([ // $mod.$main
  25771. '$mod.THelper.Fly.call({',
  25772. ' p: $mod.o.$class.GetField(),',
  25773. ' get: function () {',
  25774. ' return this.p;',
  25775. ' },',
  25776. ' set: function (v) {',
  25777. ' this.p = v;',
  25778. ' }',
  25779. '}, 11);',
  25780. 'var $with = $mod.o;',
  25781. '$mod.THelper.Fly.call({',
  25782. ' p: $with.$class.GetField(),',
  25783. ' get: function () {',
  25784. ' return this.p;',
  25785. ' },',
  25786. ' set: function (v) {',
  25787. ' this.p = v;',
  25788. ' }',
  25789. '}, 12);',
  25790. 'var $with1 = $mod.o.$class.GetField();',
  25791. '$mod.THelper.Fly.call({',
  25792. ' get: function () {',
  25793. ' return $with1;',
  25794. ' },',
  25795. ' set: function (v) {',
  25796. ' $with1 = v;',
  25797. ' }',
  25798. '}, 13);',
  25799. '$mod.THelper.Fly.call({',
  25800. ' p: $mod.c.GetField(),',
  25801. ' get: function () {',
  25802. ' return this.p;',
  25803. ' },',
  25804. ' set: function (v) {',
  25805. ' this.p = v;',
  25806. ' }',
  25807. '}, 14);',
  25808. 'var $with2 = $mod.c;',
  25809. '$mod.THelper.Fly.call({',
  25810. ' p: $with2.GetField(),',
  25811. ' get: function () {',
  25812. ' return this.p;',
  25813. ' },',
  25814. ' set: function (v) {',
  25815. ' this.p = v;',
  25816. ' }',
  25817. '}, 15);',
  25818. 'var $with3 = $mod.c.GetField();',
  25819. '$mod.THelper.Fly.call({',
  25820. ' get: function () {',
  25821. ' return $with3;',
  25822. ' },',
  25823. ' set: function (v) {',
  25824. ' $with3 = v;',
  25825. ' }',
  25826. '}, 16);',
  25827. '']));
  25828. end;
  25829. procedure TTestModule.TestTypeHelper_Property;
  25830. begin
  25831. StartProgram(false);
  25832. Add([
  25833. '{$modeswitch typehelpers}',
  25834. 'type',
  25835. ' THelper = type helper for word',
  25836. ' function GetSize: longint;',
  25837. ' procedure SetSize(Value: longint);',
  25838. ' property Size: longint read GetSize write SetSize;',
  25839. ' end;',
  25840. 'function THelper.GetSize: longint;',
  25841. 'begin',
  25842. ' Result:=Size+1;',
  25843. ' Size:=2;',
  25844. ' Result:=Self.Size+3;',
  25845. ' Self.Size:=4;',
  25846. ' with Self do begin',
  25847. ' Result:=Size+5;',
  25848. ' Size:=6;',
  25849. ' end;',
  25850. 'end;',
  25851. 'procedure THelper.SetSize(Value: longint);',
  25852. 'begin',
  25853. 'end;',
  25854. 'var w: word;',
  25855. 'begin',
  25856. ' w:=w.Size+7;',
  25857. ' w.Size:=w+8;',
  25858. ' with w do begin',
  25859. ' w:=Size+9;',
  25860. ' Size:=w+10;',
  25861. ' end;',
  25862. '']);
  25863. ConvertProgram;
  25864. CheckSource('TestTypeHelper_Property',
  25865. LinesToStr([ // statements
  25866. 'rtl.createHelper(this, "THelper", null, function () {',
  25867. ' this.GetSize = function () {',
  25868. ' var Result = 0;',
  25869. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25870. ' $mod.THelper.SetSize.call(this, 2);',
  25871. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25872. ' $mod.THelper.SetSize.call(this, 4);',
  25873. ' var $with = this.get();',
  25874. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25875. ' $mod.THelper.SetSize.call(this, 6);',
  25876. ' return Result;',
  25877. ' };',
  25878. ' this.SetSize = function (Value) {',
  25879. ' };',
  25880. '});',
  25881. 'this.w = 0;',
  25882. '']),
  25883. LinesToStr([ // $mod.$main
  25884. '$mod.w = $mod.THelper.GetSize.call({',
  25885. ' p: $mod,',
  25886. ' get: function () {',
  25887. ' return this.p.w;',
  25888. ' },',
  25889. ' set: function (v) {',
  25890. ' this.p.w = v;',
  25891. ' }',
  25892. '}) + 7;',
  25893. '$mod.THelper.SetSize.call({',
  25894. ' p: $mod,',
  25895. ' get: function () {',
  25896. ' return this.p.w;',
  25897. ' },',
  25898. ' set: function (v) {',
  25899. ' this.p.w = v;',
  25900. ' }',
  25901. '}, $mod.w + 8);',
  25902. 'var $with = $mod.w;',
  25903. '$mod.w = $mod.THelper.GetSize.call({',
  25904. ' get: function () {',
  25905. ' return $with;',
  25906. ' },',
  25907. ' set: function (v) {',
  25908. ' $with = v;',
  25909. ' }',
  25910. '}) + 9;',
  25911. '$mod.THelper.SetSize.call({',
  25912. ' get: function () {',
  25913. ' return $with;',
  25914. ' },',
  25915. ' set: function (v) {',
  25916. ' $with = v;',
  25917. ' }',
  25918. '}, $mod.w + 10);',
  25919. '']));
  25920. end;
  25921. procedure TTestModule.TestTypeHelper_Property_Array;
  25922. begin
  25923. StartProgram(false);
  25924. Add([
  25925. '{$modeswitch typehelpers}',
  25926. 'type',
  25927. ' THelper = type helper for word',
  25928. ' function GetItems(Index: byte): boolean;',
  25929. ' procedure SetItems(Index: byte; Value: boolean);',
  25930. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  25931. ' end;',
  25932. 'function THelper.GetItems(Index: byte): boolean;',
  25933. 'begin',
  25934. ' Result:=Items[1];',
  25935. ' Items[2]:=false;',
  25936. ' Result:=Self.Items[3];',
  25937. ' Self.Items[4]:=true;',
  25938. ' with Self do begin',
  25939. ' Result:=Items[5];',
  25940. ' Items[6]:=false;',
  25941. ' end;',
  25942. 'end;',
  25943. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  25944. 'begin',
  25945. 'end;',
  25946. 'var',
  25947. ' w: word;',
  25948. ' b: boolean;',
  25949. 'begin',
  25950. ' b:=w.Items[1];',
  25951. ' w.Items[2]:=b;',
  25952. ' with w do begin',
  25953. ' b:=Items[3];',
  25954. ' Items[4]:=b;',
  25955. ' end;',
  25956. '']);
  25957. ConvertProgram;
  25958. CheckSource('TestTypeHelper_Property_Array',
  25959. LinesToStr([ // statements
  25960. 'rtl.createHelper(this, "THelper", null, function () {',
  25961. ' this.GetItems = function (Index) {',
  25962. ' var Result = false;',
  25963. ' Result = $mod.THelper.GetItems.call(this, 1);',
  25964. ' $mod.THelper.SetItems.call(this, 2, false);',
  25965. ' Result = $mod.THelper.GetItems.call(this, 3);',
  25966. ' $mod.THelper.SetItems.call(this, 4, true);',
  25967. ' var $with = this.get();',
  25968. ' Result = $mod.THelper.GetItems.call(this, 5);',
  25969. ' $mod.THelper.SetItems.call(this, 6, false);',
  25970. ' return Result;',
  25971. ' };',
  25972. ' this.SetItems = function (Index, Value) {',
  25973. ' };',
  25974. '});',
  25975. 'this.w = 0;',
  25976. 'this.b = false;',
  25977. '']),
  25978. LinesToStr([ // $mod.$main
  25979. '$mod.b = $mod.THelper.GetItems.call({',
  25980. ' p: $mod,',
  25981. ' get: function () {',
  25982. ' return this.p.w;',
  25983. ' },',
  25984. ' set: function (v) {',
  25985. ' this.p.w = v;',
  25986. ' }',
  25987. '}, 1);',
  25988. '$mod.THelper.SetItems.call({',
  25989. ' p: $mod,',
  25990. ' get: function () {',
  25991. ' return this.p.w;',
  25992. ' },',
  25993. ' set: function (v) {',
  25994. ' this.p.w = v;',
  25995. ' }',
  25996. '}, 2, $mod.b);',
  25997. 'var $with = $mod.w;',
  25998. '$mod.b = $mod.THelper.GetItems.call({',
  25999. ' get: function () {',
  26000. ' return $with;',
  26001. ' },',
  26002. ' set: function (v) {',
  26003. ' $with = v;',
  26004. ' }',
  26005. '}, 3);',
  26006. '$mod.THelper.SetItems.call({',
  26007. ' get: function () {',
  26008. ' return $with;',
  26009. ' },',
  26010. ' set: function (v) {',
  26011. ' $with = v;',
  26012. ' }',
  26013. '}, 4, $mod.b);',
  26014. '']));
  26015. end;
  26016. procedure TTestModule.TestTypeHelper_ClassProperty;
  26017. begin
  26018. StartProgram(false);
  26019. Add([
  26020. '{$modeswitch typehelpers}',
  26021. 'type',
  26022. ' THelper = type helper for word',
  26023. ' class function GetSize: longint; static;',
  26024. ' class procedure SetSize(Value: longint); static;',
  26025. ' class property Size: longint read GetSize write SetSize;',
  26026. ' end;',
  26027. 'class function THelper.GetSize: longint;',
  26028. 'begin',
  26029. ' Result:=Size+1;',
  26030. ' Size:=2;',
  26031. 'end;',
  26032. 'class procedure THelper.SetSize(Value: longint);',
  26033. 'begin',
  26034. 'end;',
  26035. 'begin',
  26036. '']);
  26037. ConvertProgram;
  26038. CheckSource('TestTypeHelper_ClassProperty',
  26039. LinesToStr([ // statements
  26040. 'rtl.createHelper(this, "THelper", null, function () {',
  26041. ' this.GetSize = function () {',
  26042. ' var Result = 0;',
  26043. ' Result = $mod.THelper.GetSize() + 1;',
  26044. ' $mod.THelper.SetSize(2);',
  26045. ' return Result;',
  26046. ' };',
  26047. ' this.SetSize = function (Value) {',
  26048. ' };',
  26049. '});',
  26050. '']),
  26051. LinesToStr([ // $mod.$main
  26052. '']));
  26053. end;
  26054. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26055. begin
  26056. StartProgram(false);
  26057. Add([
  26058. '{$modeswitch typehelpers}',
  26059. 'type',
  26060. ' THelper = type helper for word',
  26061. ' class function GetItems(Index: byte): boolean; static;',
  26062. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26063. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26064. ' end;',
  26065. 'class function THelper.GetItems(Index: byte): boolean;',
  26066. 'begin',
  26067. ' Result:=Items[1];',
  26068. ' Items[2]:=false;',
  26069. 'end;',
  26070. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26071. 'begin',
  26072. 'end;',
  26073. 'var',
  26074. ' w: word;',
  26075. ' b: boolean;',
  26076. 'begin',
  26077. ' b:=w.Items[1];',
  26078. ' w.Items[2]:=b;',
  26079. ' with w do begin',
  26080. ' b:=Items[3];',
  26081. ' Items[4]:=b;',
  26082. ' end;',
  26083. '']);
  26084. ConvertProgram;
  26085. CheckSource('TestTypeHelper_ClassProperty_Array',
  26086. LinesToStr([ // statements
  26087. 'rtl.createHelper(this, "THelper", null, function () {',
  26088. ' this.GetItems = function (Index) {',
  26089. ' var Result = false;',
  26090. ' Result = $mod.THelper.GetItems(1);',
  26091. ' $mod.THelper.SetItems(2, false);',
  26092. ' return Result;',
  26093. ' };',
  26094. ' this.SetItems = function (Index, Value) {',
  26095. ' };',
  26096. '});',
  26097. 'this.w = 0;',
  26098. 'this.b = false;',
  26099. '']),
  26100. LinesToStr([ // $mod.$main
  26101. '$mod.b = $mod.THelper.GetItems(1);',
  26102. '$mod.THelper.SetItems(2, $mod.b);',
  26103. 'var $with = $mod.w;',
  26104. '$mod.b = $mod.THelper.GetItems(3);',
  26105. '$mod.THelper.SetItems(4, $mod.b);',
  26106. '']));
  26107. end;
  26108. procedure TTestModule.TestTypeHelper_ClassMethod;
  26109. begin
  26110. StartProgram(false);
  26111. Add([
  26112. '{$modeswitch typehelpers}',
  26113. 'type',
  26114. ' THelper = type helper for word',
  26115. ' class procedure DoStatic; static;',
  26116. ' end;',
  26117. 'class procedure THelper.DoStatic;',
  26118. 'begin',
  26119. ' DoStatic;',
  26120. ' DoStatic();',
  26121. 'end;',
  26122. 'var w: word;',
  26123. 'begin',
  26124. ' w.DoStatic;',
  26125. ' w.DoStatic();',
  26126. '']);
  26127. ConvertProgram;
  26128. CheckSource('TestTypeHelper_ClassMethod',
  26129. LinesToStr([ // statements
  26130. 'rtl.createHelper(this, "THelper", null, function () {',
  26131. ' this.DoStatic = function () {',
  26132. ' $mod.THelper.DoStatic();',
  26133. ' $mod.THelper.DoStatic();',
  26134. ' };',
  26135. '});',
  26136. 'this.w = 0;',
  26137. '']),
  26138. LinesToStr([ // $mod.$main
  26139. '$mod.THelper.DoStatic();',
  26140. '$mod.THelper.DoStatic();',
  26141. '']));
  26142. end;
  26143. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26144. begin
  26145. StartProgram(false);
  26146. Add([
  26147. '{$modeswitch typehelpers}',
  26148. 'type',
  26149. ' THelper = type helper for word',
  26150. ' procedure Run; external name ''Run'';',
  26151. ' end;',
  26152. 'var w: word;',
  26153. 'begin',
  26154. ' w.Run;',
  26155. '']);
  26156. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26157. ConvertProgram;
  26158. end;
  26159. procedure TTestModule.TestTypeHelper_Constructor;
  26160. begin
  26161. StartProgram(false);
  26162. Add([
  26163. '{$modeswitch typehelpers}',
  26164. 'type',
  26165. ' THelper = type helper for word',
  26166. ' constructor Init(e: longint);',
  26167. ' end;',
  26168. 'constructor THelper.Init(e: longint);',
  26169. 'begin',
  26170. ' Self:=e;',
  26171. ' Init(e+1);',
  26172. 'end;',
  26173. 'var w: word;',
  26174. 'begin',
  26175. ' w:=word.Init(2);',
  26176. ' w:=w.Init(3);',
  26177. ' with word do w:=Init(4);',
  26178. ' with w do w:=Init(5);',
  26179. '']);
  26180. ConvertProgram;
  26181. CheckSource('TestTypeHelper_Constructor',
  26182. LinesToStr([ // statements
  26183. 'rtl.createHelper(this, "THelper", null, function () {',
  26184. ' this.Init = function (e) {',
  26185. ' this.set(e);',
  26186. ' $mod.THelper.Init.call(this, e + 1);',
  26187. ' return this.get();',
  26188. ' };',
  26189. ' this.$new = function (fn, args) {',
  26190. ' return this[fn].apply({',
  26191. ' p: 0,',
  26192. ' get: function () {',
  26193. ' return this.p;',
  26194. ' },',
  26195. ' set: function (v) {',
  26196. ' this.p = v;',
  26197. ' }',
  26198. ' }, args);',
  26199. ' };',
  26200. '});',
  26201. 'this.w = 0;',
  26202. '']),
  26203. LinesToStr([ // $mod.$main
  26204. '$mod.w = $mod.THelper.$new("Init", [2]);',
  26205. '$mod.w = $mod.THelper.Init.call({',
  26206. ' p: $mod,',
  26207. ' get: function () {',
  26208. ' return this.p.w;',
  26209. ' },',
  26210. ' set: function (v) {',
  26211. ' this.p.w = v;',
  26212. ' }',
  26213. '}, 3);',
  26214. '$mod.w = $mod.THelper.$new("Init", [4]);',
  26215. 'var $with = $mod.w;',
  26216. '$mod.w = $mod.THelper.Init.call({',
  26217. ' get: function () {',
  26218. ' return $with;',
  26219. ' },',
  26220. ' set: function (v) {',
  26221. ' $with = v;',
  26222. ' }',
  26223. '}, 5);',
  26224. '']));
  26225. end;
  26226. procedure TTestModule.TestTypeHelper_Word;
  26227. begin
  26228. StartProgram(false);
  26229. Add([
  26230. '{$modeswitch typehelpers}',
  26231. 'type',
  26232. ' THelper = type helper for word',
  26233. ' procedure DoIt(e: byte = 123);',
  26234. ' end;',
  26235. 'procedure THelper.DoIt(e: byte);',
  26236. 'begin',
  26237. ' Self:=e;',
  26238. ' Self:=Self+1;',
  26239. ' with Self do Doit;',
  26240. 'end;',
  26241. 'begin',
  26242. ' word(3).DoIt;',
  26243. '']);
  26244. ConvertProgram;
  26245. CheckSource('TestTypeHelper_Word',
  26246. LinesToStr([ // statements
  26247. 'rtl.createHelper(this, "THelper", null, function () {',
  26248. ' this.DoIt = function (e) {',
  26249. ' this.set(e);',
  26250. ' this.set(this.get() + 1);',
  26251. ' var $with = this.get();',
  26252. ' $mod.THelper.DoIt.call(this, 123);',
  26253. ' };',
  26254. '});',
  26255. '']),
  26256. LinesToStr([ // $mod.$main
  26257. '$mod.THelper.DoIt.call({',
  26258. ' get: function () {',
  26259. ' return 3;',
  26260. ' },',
  26261. ' set: function (v) {',
  26262. ' rtl.raiseE("EPropReadOnly");',
  26263. ' }',
  26264. '}, 123);',
  26265. '']));
  26266. end;
  26267. procedure TTestModule.TestTypeHelper_Boolean;
  26268. begin
  26269. StartProgram(false);
  26270. Add([
  26271. '{$modeswitch typehelpers}',
  26272. 'type',
  26273. ' Integer = longint;',
  26274. ' THelper = type helper for boolean',
  26275. ' procedure Run(e: wordbool = true);',
  26276. ' end;',
  26277. 'procedure THelper.Run(e: wordbool);',
  26278. 'begin',
  26279. ' Self:=e;',
  26280. ' Self:=not Self;',
  26281. ' with Self do Run;',
  26282. ' if Integer(Self)=0 then ;',
  26283. 'end;',
  26284. 'begin',
  26285. ' boolean(3).Run;',
  26286. '']);
  26287. ConvertProgram;
  26288. CheckSource('TestTypeHelper_Boolean',
  26289. LinesToStr([ // statements
  26290. 'rtl.createHelper(this, "THelper", null, function () {',
  26291. ' this.Run = function (e) {',
  26292. ' this.set(e);',
  26293. ' this.set(!this.get());',
  26294. ' var $with = this.get();',
  26295. ' $mod.THelper.Run.call(this, true);',
  26296. ' if ((this.get() ? 1 : 0) === 0) ;',
  26297. ' };',
  26298. '});',
  26299. '']),
  26300. LinesToStr([ // $mod.$main
  26301. '$mod.THelper.Run.call({',
  26302. ' a: 3 != 0,',
  26303. ' get: function () {',
  26304. ' return this.a;',
  26305. ' },',
  26306. ' set: function (v) {',
  26307. ' rtl.raiseE("EPropReadOnly");',
  26308. ' }',
  26309. '}, true);',
  26310. '']));
  26311. end;
  26312. procedure TTestModule.TestTypeHelper_WordBool;
  26313. begin
  26314. StartProgram(false);
  26315. Add([
  26316. '{$modeswitch typehelpers}',
  26317. 'type',
  26318. ' Integer = longint;',
  26319. ' THelper = type helper for WordBool',
  26320. ' procedure Run(e: wordbool = true);',
  26321. ' end;',
  26322. 'procedure THelper.Run(e: wordbool);',
  26323. 'var i: integer;',
  26324. 'begin',
  26325. ' i:=Integer(Self);',
  26326. 'end;',
  26327. 'var w: wordbool;',
  26328. 'begin',
  26329. ' w.Run;',
  26330. ' wordbool(3).Run;',
  26331. '']);
  26332. ConvertProgram;
  26333. CheckSource('TestTypeHelper_WordBool',
  26334. LinesToStr([ // statements
  26335. 'rtl.createHelper(this, "THelper", null, function () {',
  26336. ' this.Run = function (e) {',
  26337. ' var i = 0;',
  26338. ' i = (this.get() ? 1 : 0);',
  26339. ' };',
  26340. '});',
  26341. 'this.w = false;',
  26342. '']),
  26343. LinesToStr([ // $mod.$main
  26344. '$mod.THelper.Run.call({',
  26345. ' p: $mod,',
  26346. ' get: function () {',
  26347. ' return this.p.w;',
  26348. ' },',
  26349. ' set: function (v) {',
  26350. ' this.p.w = v;',
  26351. ' }',
  26352. '}, true);',
  26353. '$mod.THelper.Run.call({',
  26354. ' a: 3 != 0,',
  26355. ' get: function () {',
  26356. ' return this.a;',
  26357. ' },',
  26358. ' set: function (v) {',
  26359. ' rtl.raiseE("EPropReadOnly");',
  26360. ' }',
  26361. '}, true);',
  26362. '']));
  26363. end;
  26364. procedure TTestModule.TestTypeHelper_Double;
  26365. begin
  26366. StartProgram(false);
  26367. Add([
  26368. '{$modeswitch typehelpers}',
  26369. 'type',
  26370. ' Float = type double;',
  26371. ' THelper = type helper for Float',
  26372. ' const NPI = 3.141592;',
  26373. ' function ToStr: String;',
  26374. ' end;',
  26375. 'function THelper.ToStr: String;',
  26376. 'begin',
  26377. 'end;',
  26378. 'procedure DoIt(s: string);',
  26379. 'begin',
  26380. 'end;',
  26381. 'var f: Float;',
  26382. 'begin',
  26383. ' DoIt(f.toStr);',
  26384. ' DoIt(f.toStr());',
  26385. ' (f*f).toStr;',
  26386. ' DoIt((f*f).toStr);',
  26387. '']);
  26388. ConvertProgram;
  26389. CheckSource('TestTypeHelper_Double',
  26390. LinesToStr([ // statements
  26391. 'rtl.createHelper(this, "THelper", null, function () {',
  26392. ' this.NPI = 3.141592;',
  26393. ' this.ToStr = function () {',
  26394. ' var Result = "";',
  26395. ' return Result;',
  26396. ' };',
  26397. '});',
  26398. 'this.DoIt = function (s) {',
  26399. '};',
  26400. 'this.f = 0.0;',
  26401. '']),
  26402. LinesToStr([ // $mod.$main
  26403. '$mod.DoIt($mod.THelper.ToStr.call({',
  26404. ' p: $mod,',
  26405. ' get: function () {',
  26406. ' return this.p.f;',
  26407. ' },',
  26408. ' set: function (v) {',
  26409. ' this.p.f = v;',
  26410. ' }',
  26411. '}));',
  26412. '$mod.DoIt($mod.THelper.ToStr.call({',
  26413. ' p: $mod,',
  26414. ' get: function () {',
  26415. ' return this.p.f;',
  26416. ' },',
  26417. ' set: function (v) {',
  26418. ' this.p.f = v;',
  26419. ' }',
  26420. '}));',
  26421. '$mod.THelper.ToStr.call({',
  26422. ' a: $mod.f * $mod.f,',
  26423. ' get: function () {',
  26424. ' return this.a;',
  26425. ' },',
  26426. ' set: function (v) {',
  26427. ' rtl.raiseE("EPropReadOnly");',
  26428. ' }',
  26429. '});',
  26430. '$mod.DoIt($mod.THelper.ToStr.call({',
  26431. ' a: $mod.f * $mod.f,',
  26432. ' get: function () {',
  26433. ' return this.a;',
  26434. ' },',
  26435. ' set: function (v) {',
  26436. ' rtl.raiseE("EPropReadOnly");',
  26437. ' }',
  26438. '}));',
  26439. '']));
  26440. end;
  26441. procedure TTestModule.TestTypeHelper_NativeInt;
  26442. begin
  26443. StartProgram(false);
  26444. Add([
  26445. '{$modeswitch typehelpers}',
  26446. 'type',
  26447. ' MaxInt = type nativeint;',
  26448. ' THelperI = type helper for MaxInt',
  26449. ' function ToStr: String;',
  26450. ' end;',
  26451. ' MaxUInt = type nativeuint;',
  26452. ' THelperU = type helper for MaxUInt',
  26453. ' function ToStr: String;',
  26454. ' end;',
  26455. 'function THelperI.ToStr: String;',
  26456. 'begin',
  26457. ' Result:=str(Self);',
  26458. 'end;',
  26459. 'function THelperU.ToStr: String;',
  26460. 'begin',
  26461. ' Result:=str(Self);',
  26462. 'end;',
  26463. 'procedure DoIt(s: string);',
  26464. 'begin',
  26465. 'end;',
  26466. 'var i: MaxInt;',
  26467. 'begin',
  26468. ' DoIt(i.toStr);',
  26469. ' DoIt(i.toStr());',
  26470. ' (i*i).toStr;',
  26471. ' DoIt((i*i).toStr);',
  26472. '']);
  26473. ConvertProgram;
  26474. CheckSource('TestTypeHelper_NativeInt',
  26475. LinesToStr([ // statements
  26476. 'rtl.createHelper(this, "THelperI", null, function () {',
  26477. ' this.ToStr = function () {',
  26478. ' var Result = "";',
  26479. ' Result = "" + this.get();',
  26480. ' return Result;',
  26481. ' };',
  26482. '});',
  26483. 'rtl.createHelper(this, "THelperU", null, function () {',
  26484. ' this.ToStr = function () {',
  26485. ' var Result = "";',
  26486. ' Result = "" + this.get();',
  26487. ' return Result;',
  26488. ' };',
  26489. '});',
  26490. 'this.DoIt = function (s) {',
  26491. '};',
  26492. 'this.i = 0;',
  26493. '']),
  26494. LinesToStr([ // $mod.$main
  26495. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26496. ' p: $mod,',
  26497. ' get: function () {',
  26498. ' return this.p.i;',
  26499. ' },',
  26500. ' set: function (v) {',
  26501. ' this.p.i = v;',
  26502. ' }',
  26503. '}));',
  26504. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26505. ' p: $mod,',
  26506. ' get: function () {',
  26507. ' return this.p.i;',
  26508. ' },',
  26509. ' set: function (v) {',
  26510. ' this.p.i = v;',
  26511. ' }',
  26512. '}));',
  26513. '$mod.THelperI.ToStr.call({',
  26514. ' a: $mod.i * $mod.i,',
  26515. ' get: function () {',
  26516. ' return this.a;',
  26517. ' },',
  26518. ' set: function (v) {',
  26519. ' rtl.raiseE("EPropReadOnly");',
  26520. ' }',
  26521. '});',
  26522. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26523. ' a: $mod.i * $mod.i,',
  26524. ' get: function () {',
  26525. ' return this.a;',
  26526. ' },',
  26527. ' set: function (v) {',
  26528. ' rtl.raiseE("EPropReadOnly");',
  26529. ' }',
  26530. '}));',
  26531. '']));
  26532. end;
  26533. procedure TTestModule.TestTypeHelper_StringChar;
  26534. begin
  26535. StartProgram(false);
  26536. Add([
  26537. '{$modeswitch typehelpers}',
  26538. 'type',
  26539. ' TStringHelper = type helper for string',
  26540. ' procedure DoIt(e: byte = 123);',
  26541. ' end;',
  26542. ' TCharHelper = type helper for char',
  26543. ' procedure Fly;',
  26544. ' end;',
  26545. 'procedure TStringHelper.DoIt(e: byte);',
  26546. 'begin',
  26547. ' Self[1]:=''c'';',
  26548. ' Self[2]:=Self[3];',
  26549. 'end;',
  26550. 'procedure TCharHelper.Fly;',
  26551. 'begin',
  26552. ' Self:=''c'';',
  26553. 'end;',
  26554. 'begin',
  26555. ' ''abc''.DoIt;',
  26556. ' ''xyz''.DoIt();',
  26557. ' ''c''.Fly();',
  26558. '']);
  26559. ConvertProgram;
  26560. CheckSource('TestTypeHelper_StringChar',
  26561. LinesToStr([ // statements
  26562. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26563. ' this.DoIt = function (e) {',
  26564. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26565. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26566. ' };',
  26567. '});',
  26568. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26569. ' this.Fly = function () {',
  26570. ' this.set("c");',
  26571. ' };',
  26572. '});',
  26573. '']),
  26574. LinesToStr([ // $mod.$main
  26575. '$mod.TStringHelper.DoIt.call({',
  26576. ' get: function () {',
  26577. ' return "abc";',
  26578. ' },',
  26579. ' set: function (v) {',
  26580. ' rtl.raiseE("EPropReadOnly");',
  26581. ' }',
  26582. '}, 123);',
  26583. '$mod.TStringHelper.DoIt.call({',
  26584. ' get: function () {',
  26585. ' return "xyz";',
  26586. ' },',
  26587. ' set: function (v) {',
  26588. ' rtl.raiseE("EPropReadOnly");',
  26589. ' }',
  26590. '}, 123);',
  26591. '$mod.TCharHelper.Fly.call({',
  26592. ' get: function () {',
  26593. ' return "c";',
  26594. ' },',
  26595. ' set: function (v) {',
  26596. ' rtl.raiseE("EPropReadOnly");',
  26597. ' }',
  26598. '});',
  26599. '']));
  26600. end;
  26601. procedure TTestModule.TestTypeHelper_JSValue;
  26602. begin
  26603. StartProgram(false);
  26604. Add([
  26605. '{$modeswitch typehelpers}',
  26606. 'type',
  26607. ' TExtValue = type jsvalue;',
  26608. ' THelper = type helper for TExtValue',
  26609. ' function ToStr: String;',
  26610. ' end;',
  26611. 'function THelper.ToStr: String;',
  26612. 'begin',
  26613. 'end;',
  26614. 'var',
  26615. ' s: string;',
  26616. ' v: TExtValue;',
  26617. 'begin',
  26618. ' s:=v.toStr;',
  26619. ' s:=v.toStr();',
  26620. ' TExtValue(s).toStr;',
  26621. '']);
  26622. ConvertProgram;
  26623. CheckSource('TestTypeHelper_JSValue',
  26624. LinesToStr([ // statements
  26625. 'rtl.createHelper(this, "THelper", null, function () {',
  26626. ' this.ToStr = function () {',
  26627. ' var Result = "";',
  26628. ' return Result;',
  26629. ' };',
  26630. '});',
  26631. 'this.s = "";',
  26632. 'this.v = undefined;',
  26633. '']),
  26634. LinesToStr([ // $mod.$main
  26635. '$mod.s = $mod.THelper.ToStr.call({',
  26636. ' p: $mod,',
  26637. ' get: function () {',
  26638. ' return this.p.v;',
  26639. ' },',
  26640. ' set: function (v) {',
  26641. ' this.p.v = v;',
  26642. ' }',
  26643. '});',
  26644. '$mod.s = $mod.THelper.ToStr.call({',
  26645. ' p: $mod,',
  26646. ' get: function () {',
  26647. ' return this.p.v;',
  26648. ' },',
  26649. ' set: function (v) {',
  26650. ' this.p.v = v;',
  26651. ' }',
  26652. '});',
  26653. '$mod.THelper.ToStr.call({',
  26654. ' p: $mod,',
  26655. ' get: function () {',
  26656. ' return this.p.s;',
  26657. ' },',
  26658. ' set: function (v) {',
  26659. ' rtl.raiseE("EPropReadOnly");',
  26660. ' }',
  26661. '});',
  26662. '']));
  26663. end;
  26664. procedure TTestModule.TestTypeHelper_Array;
  26665. begin
  26666. StartProgram(false);
  26667. Add([
  26668. '{$modeswitch typehelpers}',
  26669. 'type',
  26670. ' TArrOfBool = array of boolean;',
  26671. ' TArrOfJS = array of jsvalue;',
  26672. ' THelper = type helper for TArrOfBool',
  26673. ' procedure DoIt(e: byte = 123);',
  26674. ' end;',
  26675. 'procedure THelper.DoIt(e: byte);',
  26676. 'begin',
  26677. ' Self[1]:=true;',
  26678. ' Self[2]:=not Self[3];',
  26679. ' SetLength(Self,4);',
  26680. ' Self:=Concat(Self,[true]);',
  26681. 'end;',
  26682. 'var',
  26683. ' b: TArrOfBool;',
  26684. ' j: TArrOfJS;',
  26685. 'begin',
  26686. ' b.DoIt;',
  26687. ' TArrOfBool(j).DoIt();',
  26688. '']);
  26689. ConvertProgram;
  26690. CheckSource('TestTypeHelper_Array',
  26691. LinesToStr([ // statements
  26692. 'rtl.createHelper(this, "THelper", null, function () {',
  26693. ' this.DoIt = function (e) {',
  26694. ' this.get()[1] = true;',
  26695. ' this.get()[2] = !this.get()[3];',
  26696. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26697. ' this.set(rtl.arrayPushN(this.get(), true));',
  26698. ' };',
  26699. '});',
  26700. 'this.b = [];',
  26701. 'this.j = [];',
  26702. '']),
  26703. LinesToStr([ // $mod.$main
  26704. '$mod.THelper.DoIt.call({',
  26705. ' p: $mod,',
  26706. ' get: function () {',
  26707. ' return this.p.b;',
  26708. ' },',
  26709. ' set: function (v) {',
  26710. ' this.p.b = v;',
  26711. ' }',
  26712. '}, 123);',
  26713. '$mod.THelper.DoIt.call({',
  26714. ' p: $mod,',
  26715. ' get: function () {',
  26716. ' return this.p.j;',
  26717. ' },',
  26718. ' set: function (v) {',
  26719. ' this.p.j = v;',
  26720. ' }',
  26721. '}, 123);',
  26722. '']));
  26723. end;
  26724. procedure TTestModule.TestTypeHelper_EnumType;
  26725. begin
  26726. StartProgram(false);
  26727. Add([
  26728. '{$modeswitch typehelpers}',
  26729. 'type',
  26730. ' TEnum = (red,blue);',
  26731. ' THelper = type helper for TEnum',
  26732. ' procedure DoIt(e: byte = 123);',
  26733. ' class procedure Swing(w: word); static;',
  26734. ' end;',
  26735. 'procedure THelper.DoIt(e: byte);',
  26736. 'begin',
  26737. ' Self:=red;',
  26738. ' Self:=succ(Self);',
  26739. ' with Self do Doit;',
  26740. 'end;',
  26741. 'class procedure THelper.Swing(w: word);',
  26742. 'begin',
  26743. 'end;',
  26744. 'var e: TEnum;',
  26745. 'begin',
  26746. ' e.DoIt;',
  26747. ' red.DoIt;',
  26748. ' TEnum.blue.DoIt;',
  26749. ' TEnum(1).DoIt;',
  26750. ' TEnum.Swing(3);',
  26751. '']);
  26752. ConvertProgram;
  26753. CheckSource('TestTypeHelper_EnumType',
  26754. LinesToStr([ // statements
  26755. 'this.TEnum = {',
  26756. ' "0": "red",',
  26757. ' red: 0,',
  26758. ' "1": "blue",',
  26759. ' blue: 1',
  26760. '};',
  26761. 'rtl.createHelper(this, "THelper", null, function () {',
  26762. ' this.DoIt = function (e) {',
  26763. ' this.set($mod.TEnum.red);',
  26764. ' this.set(this.get() + 1);',
  26765. ' var $with = this.get();',
  26766. ' $mod.THelper.DoIt.call(this, 123);',
  26767. ' };',
  26768. ' this.Swing = function (w) {',
  26769. ' };',
  26770. '});',
  26771. 'this.e = 0;',
  26772. '']),
  26773. LinesToStr([ // $mod.$main
  26774. '$mod.THelper.DoIt.call({',
  26775. ' p: $mod,',
  26776. ' get: function () {',
  26777. ' return this.p.e;',
  26778. ' },',
  26779. ' set: function (v) {',
  26780. ' this.p.e = v;',
  26781. ' }',
  26782. '}, 123);',
  26783. '$mod.THelper.DoIt.call({',
  26784. ' p: $mod.TEnum,',
  26785. ' get: function () {',
  26786. ' return this.p.red;',
  26787. ' },',
  26788. ' set: function (v) {',
  26789. ' rtl.raiseE("EPropReadOnly");',
  26790. ' }',
  26791. '}, 123);',
  26792. '$mod.THelper.DoIt.call({',
  26793. ' p: $mod.TEnum,',
  26794. ' get: function () {',
  26795. ' return this.p.blue;',
  26796. ' },',
  26797. ' set: function (v) {',
  26798. ' rtl.raiseE("EPropReadOnly");',
  26799. ' }',
  26800. '}, 123);',
  26801. '$mod.THelper.DoIt.call({',
  26802. ' get: function () {',
  26803. ' return 1;',
  26804. ' },',
  26805. ' set: function (v) {',
  26806. ' rtl.raiseE("EPropReadOnly");',
  26807. ' }',
  26808. '}, 123);',
  26809. '$mod.THelper.Swing(3);',
  26810. '']));
  26811. end;
  26812. procedure TTestModule.TestTypeHelper_SetType;
  26813. begin
  26814. StartProgram(false);
  26815. Add([
  26816. '{$modeswitch typehelpers}',
  26817. 'type',
  26818. ' TEnum = (red,blue);',
  26819. ' TSetOfEnum = set of TEnum;',
  26820. ' THelper = type helper for TSetOfEnum',
  26821. ' procedure DoIt(e: byte = 123);',
  26822. ' constructor Init(e: TEnum);',
  26823. ' constructor InitEmpty;',
  26824. ' end;',
  26825. 'procedure THelper.DoIt(e: byte);',
  26826. 'begin',
  26827. ' Self:=[];',
  26828. ' Self:=[red];',
  26829. ' Include(Self,blue);',
  26830. 'end;',
  26831. 'constructor THelper.Init(e: TEnum);',
  26832. 'begin',
  26833. ' Self:=[];',
  26834. ' Self:=[e];',
  26835. ' Include(Self,blue);',
  26836. 'end;',
  26837. 'constructor THelper.InitEmpty;',
  26838. 'begin',
  26839. 'end;',
  26840. 'var s: TSetOfEnum;',
  26841. 'begin',
  26842. ' s.DoIt;',
  26843. //' [red].DoIt;',
  26844. //' with s do DoIt;',
  26845. //' with [red,blue] do DoIt;',
  26846. ' s:=TSetOfEnum.Init(blue);',
  26847. ' s:=s.Init(blue);',
  26848. '']);
  26849. ConvertProgram;
  26850. CheckSource('TestTypeHelper_SetType',
  26851. LinesToStr([ // statements
  26852. 'this.TEnum = {',
  26853. ' "0": "red",',
  26854. ' red: 0,',
  26855. ' "1": "blue",',
  26856. ' blue: 1',
  26857. '};',
  26858. 'rtl.createHelper(this, "THelper", null, function () {',
  26859. ' this.DoIt = function (e) {',
  26860. ' this.set({});',
  26861. ' this.set(rtl.createSet($mod.TEnum.red));',
  26862. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26863. ' };',
  26864. ' this.Init = function (e) {',
  26865. ' this.set({});',
  26866. ' this.set(rtl.createSet(e));',
  26867. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26868. ' return this.get();',
  26869. ' };',
  26870. ' this.InitEmpty = function () {',
  26871. ' return this.get();',
  26872. ' };',
  26873. ' this.$new = function (fn, args) {',
  26874. ' return this[fn].apply({',
  26875. ' p: {},',
  26876. ' get: function () {',
  26877. ' return this.p;',
  26878. ' },',
  26879. ' set: function (v) {',
  26880. ' this.p = v;',
  26881. ' }',
  26882. ' }, args);',
  26883. ' };',
  26884. '});',
  26885. 'this.s = {};',
  26886. '']),
  26887. LinesToStr([ // $mod.$main
  26888. '$mod.THelper.DoIt.call({',
  26889. ' p: $mod,',
  26890. ' get: function () {',
  26891. ' return this.p.s;',
  26892. ' },',
  26893. ' set: function (v) {',
  26894. ' this.p.s = v;',
  26895. ' }',
  26896. '}, 123);',
  26897. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  26898. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  26899. ' p: $mod,',
  26900. ' get: function () {',
  26901. ' return this.p.s;',
  26902. ' },',
  26903. ' set: function (v) {',
  26904. ' this.p.s = v;',
  26905. ' }',
  26906. '}, $mod.TEnum.blue));',
  26907. '']));
  26908. end;
  26909. procedure TTestModule.TestTypeHelper_InterfaceType;
  26910. begin
  26911. StartProgram(false);
  26912. Add([
  26913. '{$interfaces com}',
  26914. '{$modeswitch typehelpers}',
  26915. 'type',
  26916. ' IUnknown = interface',
  26917. ' function _AddRef: longint;',
  26918. ' function _Release: longint;',
  26919. ' end;',
  26920. ' TObject = class(IUnknown)',
  26921. ' function _AddRef: longint; virtual; abstract;',
  26922. ' function _Release: longint; virtual; abstract;',
  26923. ' end;',
  26924. ' THelper = type helper for IUnknown',
  26925. ' procedure Fly(e: byte = 123);',
  26926. ' class procedure Run; static;',
  26927. ' end;',
  26928. 'var',
  26929. ' i: IUnknown;',
  26930. ' o: TObject;',
  26931. 'procedure THelper.Fly(e: byte);',
  26932. 'begin',
  26933. ' i:=Self;',
  26934. ' o:=Self as TObject;',
  26935. ' Self:=nil;',
  26936. ' Self:=i;',
  26937. ' Self:=o;',
  26938. ' with Self do begin',
  26939. ' Fly;',
  26940. ' Fly();',
  26941. ' end;',
  26942. 'end;',
  26943. 'class procedure THelper.Run;',
  26944. 'var l: IUnknown;',
  26945. 'begin',
  26946. ' l.Fly;',
  26947. ' l.Fly();',
  26948. 'end;',
  26949. 'begin',
  26950. ' i.Fly;',
  26951. ' i.Fly();',
  26952. ' i.Run;',
  26953. ' i.Run();',
  26954. ' IUnknown.Run;',
  26955. ' IUnknown.Run();',
  26956. '']);
  26957. ConvertProgram;
  26958. CheckSource('TestTypeHelper_InterfaceType',
  26959. LinesToStr([ // statements
  26960. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  26961. 'rtl.createClass(this, "TObject", null, function () {',
  26962. ' this.$init = function () {',
  26963. ' };',
  26964. ' this.$final = function () {',
  26965. ' };',
  26966. ' rtl.addIntf(this, $mod.IUnknown);',
  26967. '});',
  26968. 'rtl.createHelper(this, "THelper", null, function () {',
  26969. ' this.Fly = function (e) {',
  26970. ' var $ir = rtl.createIntfRefs();',
  26971. ' try {',
  26972. ' rtl.setIntfP($mod, "i", this.get());',
  26973. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  26974. ' this.set(null);',
  26975. ' this.set($mod.i);',
  26976. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  26977. ' var $with = this.get();',
  26978. ' $mod.THelper.Fly.call(this, 123);',
  26979. ' $mod.THelper.Fly.call(this, 123);',
  26980. ' } finally {',
  26981. ' $ir.free();',
  26982. ' };',
  26983. ' };',
  26984. ' this.Run = function () {',
  26985. ' var l = null;',
  26986. ' try {',
  26987. ' $mod.THelper.Fly.call({',
  26988. ' get: function () {',
  26989. ' return l;',
  26990. ' },',
  26991. ' set: function (v) {',
  26992. ' l = rtl.setIntfL(l, v);',
  26993. ' }',
  26994. ' }, 123);',
  26995. ' $mod.THelper.Fly.call({',
  26996. ' get: function () {',
  26997. ' return l;',
  26998. ' },',
  26999. ' set: function (v) {',
  27000. ' l = rtl.setIntfL(l, v);',
  27001. ' }',
  27002. ' }, 123);',
  27003. ' } finally {',
  27004. ' rtl._Release(l);',
  27005. ' };',
  27006. ' };',
  27007. '});',
  27008. 'this.i = null;',
  27009. 'this.o = null;',
  27010. '']),
  27011. LinesToStr([ // $mod.$main
  27012. '$mod.THelper.Fly.call({',
  27013. ' p: $mod,',
  27014. ' get: function () {',
  27015. ' return this.p.i;',
  27016. ' },',
  27017. ' set: function (v) {',
  27018. ' rtl.setIntfP(this.p, "i", v);',
  27019. ' }',
  27020. '}, 123);',
  27021. '$mod.THelper.Fly.call({',
  27022. ' p: $mod,',
  27023. ' get: function () {',
  27024. ' return this.p.i;',
  27025. ' },',
  27026. ' set: function (v) {',
  27027. ' rtl.setIntfP(this.p, "i", v);',
  27028. ' }',
  27029. '}, 123);',
  27030. '$mod.THelper.Run();',
  27031. '$mod.THelper.Run();',
  27032. '$mod.THelper.Run();',
  27033. '$mod.THelper.Run();',
  27034. '']));
  27035. end;
  27036. procedure TTestModule.TestTypeHelper_NestedSelf;
  27037. begin
  27038. StartProgram(false);
  27039. Add([
  27040. '{$modeswitch typehelpers}',
  27041. 'type',
  27042. ' THelper = type helper for string',
  27043. ' procedure Run(Value: string);',
  27044. ' end;',
  27045. 'procedure THelper.Run(Value: string);',
  27046. ' function Sub(i: nativeint): boolean;',
  27047. ' begin',
  27048. ' Result:=Self[i+1]=Value[i];',
  27049. ' end;',
  27050. 'begin',
  27051. ' if Self[3]=Value[4] then ;',
  27052. 'end;',
  27053. 'begin',
  27054. '']);
  27055. ConvertProgram;
  27056. CheckSource('TestTypeHelper_NestedSelf',
  27057. LinesToStr([ // statements
  27058. 'rtl.createHelper(this, "THelper", null, function () {',
  27059. ' this.Run = function (Value) {',
  27060. ' var $Self = this;',
  27061. ' function Sub(i) {',
  27062. ' var Result = false;',
  27063. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27064. ' return Result;',
  27065. ' };',
  27066. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27067. ' };',
  27068. '});',
  27069. '']),
  27070. LinesToStr([ // $mod.$main
  27071. '']));
  27072. end;
  27073. procedure TTestModule.TestProcType;
  27074. begin
  27075. StartProgram(false);
  27076. Add([
  27077. 'type',
  27078. ' TProcInt = procedure(vI: longint = 1);',
  27079. 'procedure DoIt(vJ: longint);',
  27080. 'begin end;',
  27081. 'var',
  27082. ' b: boolean;',
  27083. ' vP, vQ: tprocint;',
  27084. 'begin',
  27085. ' vp:=nil;',
  27086. ' vp:=vp;',
  27087. ' vp:=@doit;',
  27088. ' vp;',
  27089. ' vp();',
  27090. ' vp(2);',
  27091. ' b:=vp=nil;',
  27092. ' b:=nil=vp;',
  27093. ' b:=vp=vq;',
  27094. ' b:=vp=@doit;',
  27095. ' b:=@doit=vp;',
  27096. ' b:=vp<>nil;',
  27097. ' b:=nil<>vp;',
  27098. ' b:=vp<>vq;',
  27099. ' b:=vp<>@doit;',
  27100. ' b:=@doit<>vp;',
  27101. ' b:=Assigned(vp);',
  27102. ' if Assigned(vp) then ;']);
  27103. ConvertProgram;
  27104. CheckSource('TestProcType',
  27105. LinesToStr([ // statements
  27106. 'this.DoIt = function(vJ) {',
  27107. '};',
  27108. 'this.b = false;',
  27109. 'this.vP = null;',
  27110. 'this.vQ = null;'
  27111. ]),
  27112. LinesToStr([ // $mod.$main
  27113. '$mod.vP = null;',
  27114. '$mod.vP = $mod.vP;',
  27115. '$mod.vP = $mod.DoIt;',
  27116. '$mod.vP(1);',
  27117. '$mod.vP(1);',
  27118. '$mod.vP(2);',
  27119. '$mod.b = $mod.vP === null;',
  27120. '$mod.b = null === $mod.vP;',
  27121. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27122. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27123. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27124. '$mod.b = $mod.vP !== null;',
  27125. '$mod.b = null !== $mod.vP;',
  27126. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27127. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27128. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27129. '$mod.b = $mod.vP != null;',
  27130. 'if ($mod.vP != null) ;',
  27131. '']));
  27132. end;
  27133. procedure TTestModule.TestProcType_Arg;
  27134. begin
  27135. StartProgram(false);
  27136. Add([
  27137. 'type',
  27138. ' TProcInt = procedure(vI: longint = 1);',
  27139. 'procedure DoIt(vJ: longint); begin end;',
  27140. 'procedure DoSome(vP, vQ: TProcInt);',
  27141. 'var',
  27142. ' b: boolean;',
  27143. 'begin',
  27144. ' vp:=nil;',
  27145. ' vp:=vp;',
  27146. ' vp:=@doit;',
  27147. ' vp;',
  27148. ' vp();',
  27149. ' vp(2);',
  27150. ' b:=vp=nil;',
  27151. ' b:=nil=vp;',
  27152. ' b:=vp=vq;',
  27153. ' b:=vp=@doit;',
  27154. ' b:=@doit=vp;',
  27155. ' b:=vp<>nil;',
  27156. ' b:=nil<>vp;',
  27157. ' b:=vp<>vq;',
  27158. ' b:=vp<>@doit;',
  27159. ' b:=@doit<>vp;',
  27160. ' b:=Assigned(vp);',
  27161. ' if Assigned(vp) then ;',
  27162. 'end;',
  27163. 'begin',
  27164. ' DoSome(@DoIt,nil);']);
  27165. ConvertProgram;
  27166. CheckSource('TestProcType_Arg',
  27167. LinesToStr([ // statements
  27168. 'this.DoIt = function(vJ) {',
  27169. '};',
  27170. 'this.DoSome = function(vP, vQ) {',
  27171. ' var b = false;',
  27172. ' vP = null;',
  27173. ' vP = vP;',
  27174. ' vP = $mod.DoIt;',
  27175. ' vP(1);',
  27176. ' vP(1);',
  27177. ' vP(2);',
  27178. ' b = vP === null;',
  27179. ' b = null === vP;',
  27180. ' b = rtl.eqCallback(vP,vQ);',
  27181. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  27182. ' b = rtl.eqCallback($mod.DoIt, vP);',
  27183. ' b = vP !== null;',
  27184. ' b = null !== vP;',
  27185. ' b = !rtl.eqCallback(vP, vQ);',
  27186. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  27187. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  27188. ' b = vP != null;',
  27189. ' if (vP != null) ;',
  27190. '};',
  27191. '']),
  27192. LinesToStr([ // $mod.$main
  27193. '$mod.DoSome($mod.DoIt,null);',
  27194. '']));
  27195. end;
  27196. procedure TTestModule.TestProcType_FunctionFPC;
  27197. begin
  27198. StartProgram(false);
  27199. Add('type');
  27200. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27201. Add('function DoIt(vI: longint): longint;');
  27202. Add('begin end;');
  27203. Add('var');
  27204. Add(' b: boolean;');
  27205. Add(' vP, vQ: tfuncint;');
  27206. Add('begin');
  27207. Add(' vp:=nil;');
  27208. Add(' vp:=vp;');
  27209. Add(' vp:=@doit;'); // ok in fpc and delphi
  27210. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27211. Add(' vp;'); // ok in fpc and delphi
  27212. Add(' vp();');
  27213. Add(' vp(2);');
  27214. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27215. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27216. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27217. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27218. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27219. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27220. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27221. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27222. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27223. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27224. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27225. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27226. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27227. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27228. Add(' b:=Assigned(vp);');
  27229. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27230. Add(' doit(vp());'); // ok in fpc and delphi
  27231. Add(' doit(vp(2));'); // ok in fpc and delphi
  27232. ConvertProgram;
  27233. CheckSource('TestProcType_FunctionFPC',
  27234. LinesToStr([ // statements
  27235. 'this.DoIt = function(vI) {',
  27236. ' var Result = 0;',
  27237. ' return Result;',
  27238. '};',
  27239. 'this.b = false;',
  27240. 'this.vP = null;',
  27241. 'this.vQ = null;'
  27242. ]),
  27243. LinesToStr([ // $mod.$main
  27244. '$mod.vP = null;',
  27245. '$mod.vP = $mod.vP;',
  27246. '$mod.vP = $mod.DoIt;',
  27247. '$mod.vP(1);',
  27248. '$mod.vP(1);',
  27249. '$mod.vP(2);',
  27250. '$mod.b = $mod.vP === null;',
  27251. '$mod.b = null === $mod.vP;',
  27252. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27253. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27254. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27255. '$mod.b = 4 === $mod.vP(1);',
  27256. '$mod.b = $mod.vP !== null;',
  27257. '$mod.b = null !== $mod.vP;',
  27258. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27259. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27260. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27261. '$mod.b = 6 !== $mod.vP(1);',
  27262. '$mod.b = $mod.vP != null;',
  27263. '$mod.DoIt($mod.vP(1));',
  27264. '$mod.DoIt($mod.vP(2));',
  27265. '']));
  27266. end;
  27267. procedure TTestModule.TestProcType_FunctionDelphi;
  27268. begin
  27269. StartProgram(false);
  27270. Add('{$mode Delphi}');
  27271. Add('type');
  27272. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27273. Add('function DoIt(vI: longint): longint;');
  27274. Add('begin end;');
  27275. Add('var');
  27276. Add(' b: boolean;');
  27277. Add(' vP, vQ: tfuncint;');
  27278. Add('begin');
  27279. Add(' vp:=nil;');
  27280. Add(' vp:=vp;');
  27281. Add(' vp:=@doit;'); // ok in fpc and delphi
  27282. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27283. Add(' vp;'); // ok in fpc and delphi
  27284. Add(' vp();');
  27285. Add(' vp(2);');
  27286. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27287. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27288. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27289. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27290. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27291. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27292. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27293. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27294. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27295. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27296. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27297. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27298. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27299. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27300. Add(' b:=Assigned(vp);');
  27301. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27302. Add(' doit(vp());'); // ok in fpc and delphi
  27303. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27304. ConvertProgram;
  27305. CheckSource('TestProcType_FunctionDelphi',
  27306. LinesToStr([ // statements
  27307. 'this.DoIt = function(vI) {',
  27308. ' var Result = 0;',
  27309. ' return Result;',
  27310. '};',
  27311. 'this.b = false;',
  27312. 'this.vP = null;',
  27313. 'this.vQ = null;'
  27314. ]),
  27315. LinesToStr([ // $mod.$main
  27316. '$mod.vP = null;',
  27317. '$mod.vP = $mod.vP;',
  27318. '$mod.vP = $mod.DoIt;',
  27319. '$mod.vP = $mod.DoIt;',
  27320. '$mod.vP(1);',
  27321. '$mod.vP(1);',
  27322. '$mod.vP(2);',
  27323. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27324. '$mod.b = $mod.vP(1) === 3;',
  27325. '$mod.b = 4 === $mod.vP(1);',
  27326. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27327. '$mod.b = $mod.vP(1) !== 5;',
  27328. '$mod.b = 6 !== $mod.vP(1);',
  27329. '$mod.b = $mod.vP != null;',
  27330. '$mod.DoIt($mod.vP(1));',
  27331. '$mod.DoIt($mod.vP(1));',
  27332. '$mod.DoIt($mod.vP(2));',
  27333. '']));
  27334. end;
  27335. procedure TTestModule.TestProcType_ProcedureDelphi;
  27336. begin
  27337. StartProgram(false);
  27338. Add('{$mode Delphi}');
  27339. Add('type');
  27340. Add(' TProc = procedure;');
  27341. Add('procedure DoIt;');
  27342. Add('begin end;');
  27343. Add('var');
  27344. Add(' b: boolean;');
  27345. Add(' vP, vQ: tproc;');
  27346. Add('begin');
  27347. Add(' vp:=nil;');
  27348. Add(' vp:=vp;');
  27349. Add(' vp:=vq;');
  27350. 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
  27351. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27352. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27353. Add(' vp;'); // ok in fpc and delphi
  27354. Add(' vp();');
  27355. // equal
  27356. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27357. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27358. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27359. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27360. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27361. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27362. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27363. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27364. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27365. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27366. // unequal
  27367. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27368. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27369. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27370. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27371. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27372. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27373. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27374. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27375. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27376. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27377. Add(' b:=Assigned(vp);');
  27378. ConvertProgram;
  27379. CheckSource('TestProcType_ProcedureDelphi',
  27380. LinesToStr([ // statements
  27381. 'this.DoIt = function() {',
  27382. '};',
  27383. 'this.b = false;',
  27384. 'this.vP = null;',
  27385. 'this.vQ = null;'
  27386. ]),
  27387. LinesToStr([ // $mod.$main
  27388. '$mod.vP = null;',
  27389. '$mod.vP = $mod.vP;',
  27390. '$mod.vP = $mod.vQ;',
  27391. '$mod.vP = $mod.DoIt;',
  27392. '$mod.vP = $mod.DoIt;',
  27393. '$mod.vP();',
  27394. '$mod.vP();',
  27395. '$mod.b = $mod.vP === null;',
  27396. '$mod.b = null === $mod.vP;',
  27397. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27398. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27399. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27400. '$mod.b = $mod.vP !== null;',
  27401. '$mod.b = null !== $mod.vP;',
  27402. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27403. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27404. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27405. '$mod.b = $mod.vP != null;',
  27406. '']));
  27407. end;
  27408. procedure TTestModule.TestProcType_AsParam;
  27409. begin
  27410. StartProgram(false);
  27411. Add('type');
  27412. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27413. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27414. Add('var vJ: tfuncint;');
  27415. Add('begin');
  27416. Add(' vg:=vg;');
  27417. Add(' vj:=vh;');
  27418. Add(' vi:=vi;');
  27419. Add(' doit(vg,vg,vg);');
  27420. Add(' doit(vh,vh,vj);');
  27421. Add(' doit(vi,vi,vi);');
  27422. Add(' doit(vj,vj,vj);');
  27423. Add('end;');
  27424. Add('var i: tfuncint;');
  27425. Add('begin');
  27426. Add(' doit(i,i,i);');
  27427. ConvertProgram;
  27428. CheckSource('TestProcType_AsParam',
  27429. LinesToStr([ // statements
  27430. 'this.DoIt = function (vG,vH,vI) {',
  27431. ' var vJ = null;',
  27432. ' vG = vG;',
  27433. ' vJ = vH;',
  27434. ' vI.set(vI.get());',
  27435. ' $mod.DoIt(vG, vG, {',
  27436. ' get: function () {',
  27437. ' return vG;',
  27438. ' },',
  27439. ' set: function (v) {',
  27440. ' vG = v;',
  27441. ' }',
  27442. ' });',
  27443. ' $mod.DoIt(vH, vH, {',
  27444. ' get: function () {',
  27445. ' return vJ;',
  27446. ' },',
  27447. ' set: function (v) {',
  27448. ' vJ = v;',
  27449. ' }',
  27450. ' });',
  27451. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27452. ' $mod.DoIt(vJ, vJ, {',
  27453. ' get: function () {',
  27454. ' return vJ;',
  27455. ' },',
  27456. ' set: function (v) {',
  27457. ' vJ = v;',
  27458. ' }',
  27459. ' });',
  27460. '};',
  27461. 'this.i = null;'
  27462. ]),
  27463. LinesToStr([
  27464. '$mod.DoIt($mod.i,$mod.i,{',
  27465. ' p: $mod,',
  27466. ' get: function () {',
  27467. ' return this.p.i;',
  27468. ' },',
  27469. ' set: function (v) {',
  27470. ' this.p.i = v;',
  27471. ' }',
  27472. '});'
  27473. ]));
  27474. end;
  27475. procedure TTestModule.TestProcType_MethodFPC;
  27476. begin
  27477. StartProgram(false);
  27478. Add('type');
  27479. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27480. Add(' TObject = class');
  27481. Add(' function DoIt(vA: longint = 1): longint;');
  27482. Add(' end;');
  27483. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27484. Add('begin');
  27485. Add('end;');
  27486. Add('var');
  27487. Add(' Obj: TObject;');
  27488. Add(' vP: tfuncint;');
  27489. Add(' b: boolean;');
  27490. Add('begin');
  27491. Add(' vp:[email protected];'); // ok in fpc and delphi
  27492. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27493. Add(' vp;'); // ok in fpc and delphi
  27494. Add(' vp();');
  27495. Add(' vp(2);');
  27496. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27497. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27498. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27499. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27500. ConvertProgram;
  27501. CheckSource('TestProcType_MethodFPC',
  27502. LinesToStr([ // statements
  27503. 'rtl.createClass(this, "TObject", null, function () {',
  27504. ' this.$init = function () {',
  27505. ' };',
  27506. ' this.$final = function () {',
  27507. ' };',
  27508. ' this.DoIt = function (vA) {',
  27509. ' var Result = 0;',
  27510. ' return Result;',
  27511. ' };',
  27512. '});',
  27513. 'this.Obj = null;',
  27514. 'this.vP = null;',
  27515. 'this.b = false;'
  27516. ]),
  27517. LinesToStr([
  27518. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27519. '$mod.vP(1);',
  27520. '$mod.vP(1);',
  27521. '$mod.vP(2);',
  27522. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27523. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27524. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27525. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27526. '']));
  27527. end;
  27528. procedure TTestModule.TestProcType_MethodDelphi;
  27529. begin
  27530. StartProgram(false);
  27531. Add([
  27532. '{$mode delphi}',
  27533. 'type',
  27534. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27535. ' TObject = class',
  27536. ' function DoIt(vA: longint = 1): longint;',
  27537. ' end;',
  27538. 'function TObject.DoIt(vA: longint = 1): longint;',
  27539. 'begin',
  27540. 'end;',
  27541. 'var',
  27542. ' Obj: TObject;',
  27543. ' vP: tfuncint;',
  27544. ' b: boolean;',
  27545. 'begin',
  27546. ' vp:[email protected];', // ok in fpc and delphi
  27547. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27548. ' vp;', // ok in fpc and delphi
  27549. ' vp();',
  27550. ' vp(2);',
  27551. //' b:[email protected];', // ok in fpc, illegal in delphi
  27552. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27553. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27554. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27555. '']);
  27556. ConvertProgram;
  27557. CheckSource('TestProcType_MethodDelphi',
  27558. LinesToStr([ // statements
  27559. 'rtl.createClass(this, "TObject", null, function () {',
  27560. ' this.$init = function () {',
  27561. ' };',
  27562. ' this.$final = function () {',
  27563. ' };',
  27564. ' this.DoIt = function (vA) {',
  27565. ' var Result = 0;',
  27566. ' return Result;',
  27567. ' };',
  27568. '});',
  27569. 'this.Obj = null;',
  27570. 'this.vP = null;',
  27571. 'this.b = false;'
  27572. ]),
  27573. LinesToStr([
  27574. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27575. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27576. '$mod.vP(1);',
  27577. '$mod.vP(1);',
  27578. '$mod.vP(2);',
  27579. '']));
  27580. end;
  27581. procedure TTestModule.TestProcType_PropertyFPC;
  27582. begin
  27583. StartProgram(false);
  27584. Add('type');
  27585. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27586. Add(' TObject = class');
  27587. Add(' FOnFoo: TFuncInt;');
  27588. Add(' function DoIt(vA: longint = 1): longint;');
  27589. Add(' function GetFoo: TFuncInt;');
  27590. Add(' procedure SetFoo(const Value: TFuncInt);');
  27591. Add(' function GetEvents(Index: longint): TFuncInt;');
  27592. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27593. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27594. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27595. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27596. Add(' end;');
  27597. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27598. Add('function tobject.getfoo: tfuncint; begin end;');
  27599. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27600. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27601. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27602. Add('var');
  27603. Add(' Obj: TObject;');
  27604. Add(' vP: tfuncint;');
  27605. Add(' b: boolean;');
  27606. Add('begin');
  27607. Add(' obj.onfoo:=nil;');
  27608. Add(' obj.onbar:=nil;');
  27609. Add(' obj.events[1]:=nil;');
  27610. Add(' obj.onfoo:=obj.onfoo;');
  27611. Add(' obj.onbar:=obj.onbar;');
  27612. Add(' obj.events[2]:=obj.events[3];');
  27613. Add(' obj.onfoo:[email protected];');
  27614. Add(' obj.onbar:[email protected];');
  27615. Add(' obj.events[4]:[email protected];');
  27616. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27617. //Add(' obj.onbar:=obj.doit;'); // delphi
  27618. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27619. Add(' obj.onfoo;');
  27620. Add(' obj.onbar;');
  27621. //Add(' obj.events[5];'); ToDo in pasresolver
  27622. Add(' obj.onfoo();');
  27623. Add(' obj.onbar();');
  27624. Add(' obj.events[6]();');
  27625. Add(' b:=obj.onfoo=nil;');
  27626. Add(' b:=obj.onbar=nil;');
  27627. Add(' b:=obj.events[7]=nil;');
  27628. Add(' b:=obj.onfoo<>nil;');
  27629. Add(' b:=obj.onbar<>nil;');
  27630. Add(' b:=obj.events[8]<>nil;');
  27631. Add(' b:=obj.onfoo=vp;');
  27632. Add(' b:=obj.onbar=vp;');
  27633. Add(' b:=obj.events[9]=vp;');
  27634. Add(' b:=obj.onfoo=obj.onfoo;');
  27635. Add(' b:=obj.onbar=obj.onfoo;');
  27636. Add(' b:=obj.events[10]=obj.onfoo;');
  27637. Add(' b:=obj.onfoo<>obj.onfoo;');
  27638. Add(' b:=obj.onbar<>obj.onfoo;');
  27639. Add(' b:=obj.events[11]<>obj.onfoo;');
  27640. Add(' b:[email protected];');
  27641. Add(' b:[email protected];');
  27642. Add(' b:=obj.events[12][email protected];');
  27643. Add(' b:=obj.onfoo<>@obj.doit;');
  27644. Add(' b:=obj.onbar<>@obj.doit;');
  27645. Add(' b:=obj.events[12]<>@obj.doit;');
  27646. Add(' b:=Assigned(obj.onfoo);');
  27647. Add(' b:=Assigned(obj.onbar);');
  27648. Add(' b:=Assigned(obj.events[13]);');
  27649. ConvertProgram;
  27650. CheckSource('TestProcType_PropertyFPC',
  27651. LinesToStr([ // statements
  27652. 'rtl.createClass(this, "TObject", null, function () {',
  27653. ' this.$init = function () {',
  27654. ' this.FOnFoo = null;',
  27655. ' };',
  27656. ' this.$final = function () {',
  27657. ' this.FOnFoo = undefined;',
  27658. ' };',
  27659. ' this.DoIt = function (vA) {',
  27660. ' var Result = 0;',
  27661. ' return Result;',
  27662. ' };',
  27663. 'this.GetFoo = function () {',
  27664. ' var Result = null;',
  27665. ' return Result;',
  27666. '};',
  27667. 'this.SetFoo = function (Value) {',
  27668. '};',
  27669. 'this.GetEvents = function (Index) {',
  27670. ' var Result = null;',
  27671. ' return Result;',
  27672. '};',
  27673. 'this.SetEvents = function (Index, Value) {',
  27674. '};',
  27675. '});',
  27676. 'this.Obj = null;',
  27677. 'this.vP = null;',
  27678. 'this.b = false;'
  27679. ]),
  27680. LinesToStr([
  27681. '$mod.Obj.FOnFoo = null;',
  27682. '$mod.Obj.SetFoo(null);',
  27683. '$mod.Obj.SetEvents(1, null);',
  27684. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27685. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27686. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27687. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27688. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27689. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27690. '$mod.Obj.FOnFoo(1);',
  27691. '$mod.Obj.GetFoo();',
  27692. '$mod.Obj.FOnFoo(1);',
  27693. '$mod.Obj.GetFoo()(1);',
  27694. '$mod.Obj.GetEvents(6)(1);',
  27695. '$mod.b = $mod.Obj.FOnFoo === null;',
  27696. '$mod.b = $mod.Obj.GetFoo() === null;',
  27697. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27698. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27699. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27700. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27701. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27702. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27703. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27704. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27705. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27706. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27707. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27708. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27709. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27710. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27711. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27712. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27713. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27714. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27715. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27716. '$mod.b = $mod.Obj.FOnFoo != null;',
  27717. '$mod.b = $mod.Obj.GetFoo() != null;',
  27718. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27719. '']));
  27720. end;
  27721. procedure TTestModule.TestProcType_PropertyDelphi;
  27722. begin
  27723. StartProgram(false);
  27724. Add('{$mode delphi}');
  27725. Add('type');
  27726. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27727. Add(' TObject = class');
  27728. Add(' FOnFoo: TFuncInt;');
  27729. Add(' function DoIt(vA: longint = 1): longint;');
  27730. Add(' function GetFoo: TFuncInt;');
  27731. Add(' procedure SetFoo(const Value: TFuncInt);');
  27732. Add(' function GetEvents(Index: longint): TFuncInt;');
  27733. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27734. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27735. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27736. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27737. Add(' end;');
  27738. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27739. Add('function tobject.getfoo: tfuncint; begin end;');
  27740. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27741. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27742. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27743. Add('var');
  27744. Add(' Obj: TObject;');
  27745. Add(' vP: tfuncint;');
  27746. Add(' b: boolean;');
  27747. Add('begin');
  27748. Add(' obj.onfoo:=nil;');
  27749. Add(' obj.onbar:=nil;');
  27750. Add(' obj.events[1]:=nil;');
  27751. Add(' obj.onfoo:=obj.onfoo;');
  27752. Add(' obj.onbar:=obj.onbar;');
  27753. Add(' obj.events[2]:=obj.events[3];');
  27754. Add(' obj.onfoo:[email protected];');
  27755. Add(' obj.onbar:[email protected];');
  27756. Add(' obj.events[4]:[email protected];');
  27757. Add(' obj.onfoo:=obj.doit;'); // delphi
  27758. Add(' obj.onbar:=obj.doit;'); // delphi
  27759. Add(' obj.events[4]:=obj.doit;'); // delphi
  27760. Add(' obj.onfoo;');
  27761. Add(' obj.onbar;');
  27762. //Add(' obj.events[5];'); ToDo in pasresolver
  27763. Add(' obj.onfoo();');
  27764. Add(' obj.onbar();');
  27765. Add(' obj.events[6]();');
  27766. //Add(' b:=obj.onfoo=nil;'); // fpc
  27767. //Add(' b:=obj.onbar=nil;'); // fpc
  27768. //Add(' b:=obj.events[7]=nil;'); // fpc
  27769. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27770. //Add(' b:=obj.onbar<>nil;'); // fpc
  27771. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27772. Add(' b:=obj.onfoo=vp;');
  27773. Add(' b:=obj.onbar=vp;');
  27774. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27775. Add(' b:=obj.onfoo=obj.onfoo;');
  27776. Add(' b:=obj.onbar=obj.onfoo;');
  27777. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27778. Add(' b:=obj.onfoo<>obj.onfoo;');
  27779. Add(' b:=obj.onbar<>obj.onfoo;');
  27780. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27781. //Add(' b:[email protected];'); // fpc
  27782. //Add(' b:[email protected];'); // fpc
  27783. //Add(' b:=obj.events[12][email protected];'); // fpc
  27784. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27785. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27786. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27787. Add(' b:=Assigned(obj.onfoo);');
  27788. Add(' b:=Assigned(obj.onbar);');
  27789. Add(' b:=Assigned(obj.events[13]);');
  27790. ConvertProgram;
  27791. CheckSource('TestProcType_PropertyDelphi',
  27792. LinesToStr([ // statements
  27793. 'rtl.createClass(this, "TObject", null, function () {',
  27794. ' this.$init = function () {',
  27795. ' this.FOnFoo = null;',
  27796. ' };',
  27797. ' this.$final = function () {',
  27798. ' this.FOnFoo = undefined;',
  27799. ' };',
  27800. ' this.DoIt = function (vA) {',
  27801. ' var Result = 0;',
  27802. ' return Result;',
  27803. ' };',
  27804. 'this.GetFoo = function () {',
  27805. ' var Result = null;',
  27806. ' return Result;',
  27807. '};',
  27808. 'this.SetFoo = function (Value) {',
  27809. '};',
  27810. 'this.GetEvents = function (Index) {',
  27811. ' var Result = null;',
  27812. ' return Result;',
  27813. '};',
  27814. 'this.SetEvents = function (Index, Value) {',
  27815. '};',
  27816. '});',
  27817. 'this.Obj = null;',
  27818. 'this.vP = null;',
  27819. 'this.b = false;'
  27820. ]),
  27821. LinesToStr([
  27822. '$mod.Obj.FOnFoo = null;',
  27823. '$mod.Obj.SetFoo(null);',
  27824. '$mod.Obj.SetEvents(1, null);',
  27825. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27826. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27827. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27828. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27829. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27830. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27831. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27832. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27833. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27834. '$mod.Obj.FOnFoo(1);',
  27835. '$mod.Obj.GetFoo();',
  27836. '$mod.Obj.FOnFoo(1);',
  27837. '$mod.Obj.GetFoo()(1);',
  27838. '$mod.Obj.GetEvents(6)(1);',
  27839. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27840. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27841. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27842. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27843. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27844. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27845. '$mod.b = $mod.Obj.FOnFoo != null;',
  27846. '$mod.b = $mod.Obj.GetFoo() != null;',
  27847. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27848. '']));
  27849. end;
  27850. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27851. begin
  27852. StartProgram(false);
  27853. Add('type');
  27854. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27855. Add(' TObject = class');
  27856. Add(' FOnFoo: TFuncInt;');
  27857. Add(' function DoIt(vA: longint = 1): longint;');
  27858. Add(' function GetFoo: TFuncInt;');
  27859. Add(' procedure SetFoo(const Value: TFuncInt);');
  27860. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27861. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27862. Add(' end;');
  27863. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27864. Add('function tobject.getfoo: tfuncint; begin end;');
  27865. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27866. Add('var');
  27867. Add(' Obj: TObject;');
  27868. Add(' vP: tfuncint;');
  27869. Add(' b: boolean;');
  27870. Add('begin');
  27871. Add('with obj do begin');
  27872. Add(' fonfoo:=nil;');
  27873. Add(' onfoo:=nil;');
  27874. Add(' onbar:=nil;');
  27875. Add(' fonfoo:=fonfoo;');
  27876. Add(' onfoo:=onfoo;');
  27877. Add(' onbar:=onbar;');
  27878. Add(' fonfoo:=@doit;');
  27879. Add(' onfoo:=@doit;');
  27880. Add(' onbar:=@doit;');
  27881. //Add(' fonfoo:=doit;'); // delphi
  27882. //Add(' onfoo:=doit;'); // delphi
  27883. //Add(' onbar:=doit;'); // delphi
  27884. Add(' fonfoo;');
  27885. Add(' onfoo;');
  27886. Add(' onbar;');
  27887. Add(' fonfoo();');
  27888. Add(' onfoo();');
  27889. Add(' onbar();');
  27890. Add(' b:=fonfoo=nil;');
  27891. Add(' b:=onfoo=nil;');
  27892. Add(' b:=onbar=nil;');
  27893. Add(' b:=fonfoo<>nil;');
  27894. Add(' b:=onfoo<>nil;');
  27895. Add(' b:=onbar<>nil;');
  27896. Add(' b:=fonfoo=vp;');
  27897. Add(' b:=onfoo=vp;');
  27898. Add(' b:=onbar=vp;');
  27899. Add(' b:=fonfoo=fonfoo;');
  27900. Add(' b:=onfoo=onfoo;');
  27901. Add(' b:=onbar=onfoo;');
  27902. Add(' b:=fonfoo<>fonfoo;');
  27903. Add(' b:=onfoo<>onfoo;');
  27904. Add(' b:=onbar<>onfoo;');
  27905. Add(' b:=fonfoo=@doit;');
  27906. Add(' b:=onfoo=@doit;');
  27907. Add(' b:=onbar=@doit;');
  27908. Add(' b:=fonfoo<>@doit;');
  27909. Add(' b:=onfoo<>@doit;');
  27910. Add(' b:=onbar<>@doit;');
  27911. Add(' b:=Assigned(fonfoo);');
  27912. Add(' b:=Assigned(onfoo);');
  27913. Add(' b:=Assigned(onbar);');
  27914. Add('end;');
  27915. ConvertProgram;
  27916. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  27917. LinesToStr([ // statements
  27918. 'rtl.createClass(this, "TObject", null, function () {',
  27919. ' this.$init = function () {',
  27920. ' this.FOnFoo = null;',
  27921. ' };',
  27922. ' this.$final = function () {',
  27923. ' this.FOnFoo = undefined;',
  27924. ' };',
  27925. ' this.DoIt = function (vA) {',
  27926. ' var Result = 0;',
  27927. ' return Result;',
  27928. ' };',
  27929. ' this.GetFoo = function () {',
  27930. ' var Result = null;',
  27931. ' return Result;',
  27932. ' };',
  27933. ' this.SetFoo = function (Value) {',
  27934. ' };',
  27935. '});',
  27936. 'this.Obj = null;',
  27937. 'this.vP = null;',
  27938. 'this.b = false;'
  27939. ]),
  27940. LinesToStr([
  27941. 'var $with = $mod.Obj;',
  27942. '$with.FOnFoo = null;',
  27943. '$with.FOnFoo = null;',
  27944. '$with.SetFoo(null);',
  27945. '$with.FOnFoo = $with.FOnFoo;',
  27946. '$with.FOnFoo = $with.FOnFoo;',
  27947. '$with.SetFoo($with.GetFoo());',
  27948. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  27949. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  27950. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  27951. '$with.FOnFoo(1);',
  27952. '$with.FOnFoo(1);',
  27953. '$with.GetFoo();',
  27954. '$with.FOnFoo(1);',
  27955. '$with.FOnFoo(1);',
  27956. '$with.GetFoo()(1);',
  27957. '$mod.b = $with.FOnFoo === null;',
  27958. '$mod.b = $with.FOnFoo === null;',
  27959. '$mod.b = $with.GetFoo() === null;',
  27960. '$mod.b = $with.FOnFoo !== null;',
  27961. '$mod.b = $with.FOnFoo !== null;',
  27962. '$mod.b = $with.GetFoo() !== null;',
  27963. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  27964. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  27965. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  27966. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27967. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27968. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  27969. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27970. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27971. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  27972. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27973. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27974. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  27975. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27976. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27977. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  27978. '$mod.b = $with.FOnFoo != null;',
  27979. '$mod.b = $with.FOnFoo != null;',
  27980. '$mod.b = $with.GetFoo() != null;',
  27981. '']));
  27982. end;
  27983. procedure TTestModule.TestProcType_Nested;
  27984. begin
  27985. StartProgram(false);
  27986. Add([
  27987. 'type',
  27988. ' TProcInt = procedure(vI: longint = 1);',
  27989. 'procedure DoIt(vJ: longint);',
  27990. 'var aProc: TProcInt;',
  27991. ' b: boolean;',
  27992. ' procedure Sub(vK: longint);',
  27993. ' var aSub: TProcInt;',
  27994. ' procedure SubSub(vK: longint);',
  27995. ' var aSubSub: TProcInt;',
  27996. ' begin;',
  27997. ' aProc:=@DoIt;',
  27998. ' aSub:=@DoIt;',
  27999. ' aSubSub:=@DoIt;',
  28000. ' aProc:=@Sub;',
  28001. ' aSub:=@Sub;',
  28002. ' aSubSub:=@Sub;',
  28003. ' aProc:=@SubSub;',
  28004. ' aSub:=@SubSub;',
  28005. ' aSubSub:=@SubSub;',
  28006. ' end;',
  28007. ' begin;',
  28008. ' end;',
  28009. 'begin;',
  28010. ' aProc:=@Sub;',
  28011. ' b:=aProc=@Sub;',
  28012. ' b:=@Sub=aProc;',
  28013. 'end;',
  28014. 'begin',
  28015. '']);
  28016. ConvertProgram;
  28017. CheckSource('TestProcType_Nested',
  28018. LinesToStr([ // statements
  28019. 'this.DoIt = function (vJ) {',
  28020. ' var aProc = null;',
  28021. ' var b = false;',
  28022. ' function Sub(vK) {',
  28023. ' var aSub = null;',
  28024. ' function SubSub(vK) {',
  28025. ' var aSubSub = null;',
  28026. ' aProc = $mod.DoIt;',
  28027. ' aSub = $mod.DoIt;',
  28028. ' aSubSub = $mod.DoIt;',
  28029. ' aProc = Sub;',
  28030. ' aSub = Sub;',
  28031. ' aSubSub = Sub;',
  28032. ' aProc = SubSub;',
  28033. ' aSub = SubSub;',
  28034. ' aSubSub = SubSub;',
  28035. ' };',
  28036. ' };',
  28037. ' aProc = Sub;',
  28038. ' b = rtl.eqCallback(aProc, Sub);',
  28039. ' b = rtl.eqCallback(Sub, aProc);',
  28040. '};',
  28041. '']),
  28042. LinesToStr([ // $mod.$main
  28043. '']));
  28044. end;
  28045. procedure TTestModule.TestProcType_NestedOfObject;
  28046. begin
  28047. StartProgram(false);
  28048. Add([
  28049. 'type',
  28050. ' TProcInt = procedure(vI: longint = 1) of object;',
  28051. ' TObject = class',
  28052. ' procedure DoIt(vJ: longint);',
  28053. ' end;',
  28054. 'procedure TObject.DoIt(vJ: longint);',
  28055. 'var aProc: TProcInt;',
  28056. ' b: boolean;',
  28057. ' procedure Sub(vK: longint);',
  28058. ' var aSub: TProcInt;',
  28059. ' procedure SubSub(vK: longint);',
  28060. ' var aSubSub: TProcInt;',
  28061. ' begin;',
  28062. ' aProc:=@DoIt;',
  28063. ' aSub:=@DoIt;',
  28064. ' aSubSub:=@DoIt;',
  28065. ' aProc:=@Sub;',
  28066. ' aSub:=@Sub;',
  28067. ' aSubSub:=@Sub;',
  28068. ' aProc:=@SubSub;',
  28069. ' aSub:=@SubSub;',
  28070. ' aSubSub:=@SubSub;',
  28071. ' end;',
  28072. ' begin;',
  28073. ' end;',
  28074. 'begin;',
  28075. ' aProc:=@Sub;',
  28076. ' b:=aProc=@Sub;',
  28077. ' b:=@Sub=aProc;',
  28078. 'end;',
  28079. 'begin',
  28080. '']);
  28081. ConvertProgram;
  28082. CheckSource('TestProcType_Nested',
  28083. LinesToStr([ // statements
  28084. 'rtl.createClass(this, "TObject", null, function () {',
  28085. ' this.$init = function () {',
  28086. ' };',
  28087. ' this.$final = function () {',
  28088. ' };',
  28089. ' this.DoIt = function (vJ) {',
  28090. ' var $Self = this;',
  28091. ' var aProc = null;',
  28092. ' var b = false;',
  28093. ' function Sub(vK) {',
  28094. ' var aSub = null;',
  28095. ' function SubSub(vK) {',
  28096. ' var aSubSub = null;',
  28097. ' aProc = rtl.createCallback($Self, "DoIt");',
  28098. ' aSub = rtl.createCallback($Self, "DoIt");',
  28099. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28100. ' aProc = Sub;',
  28101. ' aSub = Sub;',
  28102. ' aSubSub = Sub;',
  28103. ' aProc = SubSub;',
  28104. ' aSub = SubSub;',
  28105. ' aSubSub = SubSub;',
  28106. ' };',
  28107. ' };',
  28108. ' aProc = Sub;',
  28109. ' b = rtl.eqCallback(aProc, Sub);',
  28110. ' b = rtl.eqCallback(Sub, aProc);',
  28111. ' };',
  28112. '});',
  28113. '']),
  28114. LinesToStr([ // $mod.$main
  28115. '']));
  28116. end;
  28117. procedure TTestModule.TestProcType_ReferenceToProc;
  28118. begin
  28119. StartProgram(false);
  28120. Add([
  28121. 'type',
  28122. ' TProcRef = reference to procedure(i: longint = 0);',
  28123. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28124. 'var',
  28125. ' p: TProcRef;',
  28126. ' f: TFuncRef;',
  28127. 'procedure DoIt(i: longint);',
  28128. 'begin',
  28129. 'end;',
  28130. 'function GetIt(i: longint): longint;',
  28131. 'begin',
  28132. ' p:=@DoIt;',
  28133. ' f:=@GetIt;',
  28134. ' f;',
  28135. ' f();',
  28136. ' f(1);',
  28137. 'end;',
  28138. 'begin',
  28139. ' p:=@DoIt;',
  28140. ' f:=@GetIt;',
  28141. ' f;',
  28142. ' f();',
  28143. ' f(1);',
  28144. ' p:=TProcRef(f);',
  28145. '']);
  28146. ConvertProgram;
  28147. CheckSource('TestProcType_ReferenceToProc',
  28148. LinesToStr([ // statements
  28149. 'this.p = null;',
  28150. 'this.f = null;',
  28151. 'this.DoIt = function (i) {',
  28152. '};',
  28153. 'this.GetIt = function (i) {',
  28154. ' var Result = 0;',
  28155. ' $mod.p = $mod.DoIt;',
  28156. ' $mod.f = $mod.GetIt;',
  28157. ' $mod.f(0);',
  28158. ' $mod.f(0);',
  28159. ' $mod.f(1);',
  28160. ' return Result;',
  28161. '};',
  28162. '']),
  28163. LinesToStr([ // $mod.$main
  28164. '$mod.p = $mod.DoIt;',
  28165. '$mod.f = $mod.GetIt;',
  28166. '$mod.f(0);',
  28167. '$mod.f(0);',
  28168. '$mod.f(1);',
  28169. '$mod.p = $mod.f;',
  28170. '']));
  28171. end;
  28172. procedure TTestModule.TestProcType_ReferenceToMethod;
  28173. begin
  28174. StartProgram(false);
  28175. Add([
  28176. 'type',
  28177. ' TFuncRef = reference to function(i: longint = 5): longint;',
  28178. ' TObject = class',
  28179. ' function Grow(s: longint): longint;',
  28180. ' end;',
  28181. 'var',
  28182. ' f: tfuncref;',
  28183. 'function tobject.grow(s: longint): longint;',
  28184. ' function GrowSub(i: longint): longint;',
  28185. ' begin',
  28186. ' f:=@grow;',
  28187. ' f:=@growsub;',
  28188. ' end;',
  28189. 'begin',
  28190. ' f:=@grow;',
  28191. ' f:=@growsub;',
  28192. 'end;',
  28193. 'begin',
  28194. '']);
  28195. ConvertProgram;
  28196. CheckSource('TestProcType_ReferenceToMethod',
  28197. LinesToStr([ // statements
  28198. 'rtl.createClass(this, "TObject", null, function () {',
  28199. ' this.$init = function () {',
  28200. ' };',
  28201. ' this.$final = function () {',
  28202. ' };',
  28203. ' this.Grow = function (s) {',
  28204. ' var $Self = this;',
  28205. ' var Result = 0;',
  28206. ' function GrowSub(i) {',
  28207. ' var Result = 0;',
  28208. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28209. ' $mod.f = GrowSub;',
  28210. ' return Result;',
  28211. ' };',
  28212. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28213. ' $mod.f = GrowSub;',
  28214. ' return Result;',
  28215. ' };',
  28216. '});',
  28217. 'this.f = null;',
  28218. '']),
  28219. LinesToStr([ // $mod.$main
  28220. '']));
  28221. end;
  28222. procedure TTestModule.TestProcType_Typecast;
  28223. begin
  28224. StartProgram(false);
  28225. Add([
  28226. 'type',
  28227. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  28228. ' TEvent = procedure of object;',
  28229. ' TGetter = function:longint of object;',
  28230. ' TProcA = procedure(i: longint);',
  28231. ' TFuncB = function(i, j: longint): longint;',
  28232. 'procedure DoIt(); varargs; begin end;',
  28233. 'var',
  28234. ' Notify: tnotifyevent;',
  28235. ' Event: tevent;',
  28236. ' Getter: tgetter;',
  28237. ' ProcA: tproca;',
  28238. ' FuncB: tfuncb;',
  28239. ' p: pointer;',
  28240. 'begin',
  28241. ' notify:=tnotifyevent(event);',
  28242. ' event:=tevent(event);',
  28243. ' event:=tevent(notify);',
  28244. ' event:=tevent(getter);',
  28245. ' event:=tevent(proca);',
  28246. ' proca:=tproca(funcb);',
  28247. ' funcb:=tfuncb(funcb);',
  28248. ' funcb:=tfuncb(proca);',
  28249. ' funcb:=tfuncb(getter);',
  28250. ' proca:=tproca(p);',
  28251. ' funcb:=tfuncb(p);',
  28252. ' getter:=tgetter(p);',
  28253. ' p:=pointer(notify);',
  28254. ' p:=notify;',
  28255. ' p:=pointer(proca);',
  28256. ' p:=proca;',
  28257. ' p:=pointer(funcb);',
  28258. ' p:=funcb;',
  28259. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  28260. '']);
  28261. ConvertProgram;
  28262. CheckSource('TestProcType_Typecast',
  28263. LinesToStr([ // statements
  28264. 'this.DoIt = function () {',
  28265. '};',
  28266. 'this.Notify = null;',
  28267. 'this.Event = null;',
  28268. 'this.Getter = null;',
  28269. 'this.ProcA = null;',
  28270. 'this.FuncB = null;',
  28271. 'this.p = null;',
  28272. '']),
  28273. LinesToStr([ // $mod.$main
  28274. '$mod.Notify = $mod.Event;',
  28275. '$mod.Event = $mod.Event;',
  28276. '$mod.Event = $mod.Notify;',
  28277. '$mod.Event = $mod.Getter;',
  28278. '$mod.Event = $mod.ProcA;',
  28279. '$mod.ProcA = $mod.FuncB;',
  28280. '$mod.FuncB = $mod.FuncB;',
  28281. '$mod.FuncB = $mod.ProcA;',
  28282. '$mod.FuncB = $mod.Getter;',
  28283. '$mod.ProcA = $mod.p;',
  28284. '$mod.FuncB = $mod.p;',
  28285. '$mod.Getter = $mod.p;',
  28286. '$mod.p = $mod.Notify;',
  28287. '$mod.p = $mod.Notify;',
  28288. '$mod.p = $mod.ProcA;',
  28289. '$mod.p = $mod.ProcA;',
  28290. '$mod.p = $mod.FuncB;',
  28291. '$mod.p = $mod.FuncB;',
  28292. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  28293. '']));
  28294. end;
  28295. procedure TTestModule.TestProcType_PassProcToUntyped;
  28296. begin
  28297. StartProgram(false);
  28298. Add([
  28299. 'type',
  28300. ' TEvent = procedure of object;',
  28301. ' TFunc = function: longint;',
  28302. 'procedure DoIt(); varargs; begin end;',
  28303. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28304. 'var',
  28305. ' Event: tevent;',
  28306. ' Func: TFunc;',
  28307. 'begin',
  28308. ' doit(event,func);',
  28309. ' dosome(event,event,event);',
  28310. ' dosome(func,func,func);',
  28311. '']);
  28312. ConvertProgram;
  28313. CheckSource('TestProcType_PassProcToUntyped',
  28314. LinesToStr([ // statements
  28315. 'this.DoIt = function () {',
  28316. '};',
  28317. 'this.DoSome = function (a, b, p) {',
  28318. '};',
  28319. 'this.Event = null;',
  28320. 'this.Func = null;',
  28321. '']),
  28322. LinesToStr([ // $mod.$main
  28323. '$mod.DoIt($mod.Event, $mod.Func);',
  28324. '$mod.DoSome($mod.Event, {',
  28325. ' p: $mod,',
  28326. ' get: function () {',
  28327. ' return this.p.Event;',
  28328. ' },',
  28329. ' set: function (v) {',
  28330. ' this.p.Event = v;',
  28331. ' }',
  28332. '}, $mod.Event);',
  28333. '$mod.DoSome($mod.Func, {',
  28334. ' p: $mod,',
  28335. ' get: function () {',
  28336. ' return this.p.Func;',
  28337. ' },',
  28338. ' set: function (v) {',
  28339. ' this.p.Func = v;',
  28340. ' }',
  28341. '}, $mod.Func);',
  28342. '']));
  28343. end;
  28344. procedure TTestModule.TestProcType_PassProcToArray;
  28345. begin
  28346. StartProgram(false);
  28347. Add([
  28348. 'type',
  28349. ' TFunc = function: longint;',
  28350. ' TArrFunc = array of TFunc;',
  28351. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28352. 'function GetIt: longint; begin end;',
  28353. 'var',
  28354. ' Func: tfunc;',
  28355. 'begin',
  28356. ' doit([]);',
  28357. ' doit([@GetIt]);',
  28358. ' doit([Func]);',
  28359. '']);
  28360. ConvertProgram;
  28361. CheckSource('TestProcType_PassProcToArray',
  28362. LinesToStr([ // statements
  28363. 'this.DoIt = function (Arr) {',
  28364. '};',
  28365. 'this.GetIt = function () {',
  28366. ' var Result = 0;',
  28367. ' return Result;',
  28368. '};',
  28369. 'this.Func = null;',
  28370. '']),
  28371. LinesToStr([ // $mod.$main
  28372. '$mod.DoIt([]);',
  28373. '$mod.DoIt([$mod.GetIt]);',
  28374. '$mod.DoIt([$mod.Func]);',
  28375. '']));
  28376. end;
  28377. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28378. begin
  28379. StartProgram(false);
  28380. Add([
  28381. '{$modeswitch externalclass}',
  28382. 'type',
  28383. ' TProc = reference to procedure(i: longint); safecall;',
  28384. ' TEvent = procedure(i: longint) of object; safecall;',
  28385. ' TExtA = class external name ''ExtObj''',
  28386. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28387. ' procedure DoSome(Id: longint = 1);',
  28388. ' procedure SetOnClick(const e: TEvent);',
  28389. ' property OnClick: TEvent write SetOnClick;',
  28390. ' class procedure Fly(Id: longint = 1); static;',
  28391. ' procedure SetOnShow(const p: TProc);',
  28392. ' property OnShow: TProc write SetOnShow;',
  28393. ' end;',
  28394. 'procedure Run(i: longint = 1);',
  28395. 'begin',
  28396. 'end;',
  28397. 'var',
  28398. ' Obj: texta;',
  28399. ' e: TEvent;',
  28400. ' p: TProc;',
  28401. 'begin',
  28402. ' e:=e;',
  28403. ' e:[email protected];',
  28404. ' e:[email protected];',
  28405. ' e:=TEvent(@obj.dosome);', // no safecall
  28406. ' obj.OnClick:[email protected];',
  28407. ' obj.OnClick:[email protected];',
  28408. ' obj.setonclick(@obj.doit);',
  28409. ' obj.setonclick(@obj.dosome);',
  28410. ' p:=@Run;',
  28411. ' p:[email protected];',
  28412. ' obj.OnShow:=@Run;',
  28413. ' obj.OnShow:[email protected];',
  28414. ' obj.setOnShow(@Run);',
  28415. ' obj.setOnShow(@TExtA.Fly);',
  28416. ' with obj do begin',
  28417. ' e:=@doit;',
  28418. ' e:=@dosome;',
  28419. ' OnClick:=@doit;',
  28420. ' OnClick:=@dosome;',
  28421. ' setonclick(@doit);',
  28422. ' setonclick(@dosome);',
  28423. ' OnShow:=@Run;',
  28424. ' setOnShow(@Run);',
  28425. ' end;']);
  28426. ConvertProgram;
  28427. CheckSource('TestProcType_SafeCallObjFPC',
  28428. LinesToStr([ // statements
  28429. 'this.Run = function (i) {',
  28430. '};',
  28431. 'this.Obj = null;',
  28432. 'this.e = null;',
  28433. 'this.p = null;',
  28434. '']),
  28435. LinesToStr([ // $mod.$main
  28436. '$mod.e = $mod.e;',
  28437. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28438. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28439. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28440. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28441. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28442. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28443. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28444. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28445. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28446. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28447. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28448. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28449. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28450. 'var $with = $mod.Obj;',
  28451. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28452. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28453. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28454. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28455. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28456. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28457. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28458. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28459. '']));
  28460. end;
  28461. procedure TTestModule.TestProcType_SafeCallDelphi;
  28462. begin
  28463. StartProgram(false);
  28464. Add([
  28465. '{$mode delphi}',
  28466. '{$modeswitch externalclass}',
  28467. 'type',
  28468. ' TProc = reference to procedure(i: longint); safecall;',
  28469. ' TEvent = procedure(i: longint) of object; safecall;',
  28470. ' TExtA = class external name ''ExtObj''',
  28471. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28472. ' procedure DoSome(Id: longint = 1);',
  28473. ' procedure SetOnClick(const e: TEvent);',
  28474. ' property OnClick: TEvent write SetOnClick;',
  28475. ' class procedure Fly(Id: longint = 1); static;',
  28476. ' procedure SetOnShow(const p: TProc);',
  28477. ' property OnShow: TProc write SetOnShow;',
  28478. ' end;',
  28479. 'procedure Run(i: longint = 1);',
  28480. 'begin',
  28481. 'end;',
  28482. 'var',
  28483. ' Obj: texta;',
  28484. ' e: TEvent;',
  28485. ' p: TProc;',
  28486. 'begin',
  28487. ' e:=e;',
  28488. ' e:=obj.doit;',
  28489. ' e:=obj.dosome;',
  28490. ' e:=TEvent(@obj.dosome);', // no safecall
  28491. ' obj.OnClick:=obj.doit;',
  28492. ' obj.OnClick:=obj.dosome;',
  28493. ' obj.setonclick(obj.doit);',
  28494. ' obj.setonclick(obj.dosome);',
  28495. ' p:=Run;',
  28496. ' p:=TExtA.Fly;',
  28497. ' obj.OnShow:=Run;',
  28498. ' obj.OnShow:=TExtA.Fly;',
  28499. ' obj.setOnShow(Run);',
  28500. ' obj.setOnShow(TExtA.Fly);',
  28501. ' with obj do begin',
  28502. ' e:=doit;',
  28503. ' e:=dosome;',
  28504. ' OnClick:=doit;',
  28505. ' OnClick:=dosome;',
  28506. ' setonclick(doit);',
  28507. ' setonclick(dosome);',
  28508. ' OnShow:=@Run;',
  28509. ' setOnShow(@Run);',
  28510. ' end;']);
  28511. ConvertProgram;
  28512. CheckSource('TestProcType_SafeCallDelphi',
  28513. LinesToStr([ // statements
  28514. 'this.Run = function (i) {',
  28515. '};',
  28516. 'this.Obj = null;',
  28517. 'this.e = null;',
  28518. 'this.p = null;',
  28519. '']),
  28520. LinesToStr([ // $mod.$main
  28521. '$mod.e = $mod.e;',
  28522. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28523. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28524. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28525. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28526. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28527. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28528. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28529. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28530. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28531. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28532. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28533. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28534. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28535. 'var $with = $mod.Obj;',
  28536. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28537. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28538. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28539. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28540. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28541. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28542. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28543. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28544. '']));
  28545. end;
  28546. procedure TTestModule.TestPointer;
  28547. begin
  28548. StartProgram(false);
  28549. Add(['type',
  28550. ' TObject = class end;',
  28551. ' TClass = class of TObject;',
  28552. ' TArrInt = array of longint;',
  28553. 'const',
  28554. ' n = nil;',
  28555. 'var',
  28556. ' v: jsvalue;',
  28557. ' Obj: tobject;',
  28558. ' C: tclass;',
  28559. ' a: tarrint;',
  28560. ' p: Pointer = nil;',
  28561. ' s: string;',
  28562. 'begin',
  28563. ' p:=p;',
  28564. ' p:=nil;',
  28565. ' if p=nil then;',
  28566. ' if nil=p then;',
  28567. ' if Assigned(p) then;',
  28568. ' p:=Pointer(v);',
  28569. ' p:=obj;',
  28570. ' p:=c;',
  28571. ' p:=a;',
  28572. ' p:=tobject;',
  28573. ' obj:=TObject(p);',
  28574. ' c:=TClass(p);',
  28575. ' a:=TArrInt(p);',
  28576. ' p:=n;',
  28577. ' p:=Pointer(a);',
  28578. ' p:=pointer(s);',
  28579. ' s:=string(p);',
  28580. '']);
  28581. ConvertProgram;
  28582. CheckSource('TestPointer',
  28583. LinesToStr([ // statements
  28584. 'rtl.createClass(this, "TObject", null, function () {',
  28585. ' this.$init = function () {',
  28586. ' };',
  28587. ' this.$final = function () {',
  28588. ' };',
  28589. '});',
  28590. 'this.n = null;',
  28591. 'this.v = undefined;',
  28592. 'this.Obj = null;',
  28593. 'this.C = null;',
  28594. 'this.a = [];',
  28595. 'this.p = null;',
  28596. 'this.s = "";',
  28597. '']),
  28598. LinesToStr([ // $mod.$main
  28599. '$mod.p = $mod.p;',
  28600. '$mod.p = null;',
  28601. 'if ($mod.p === null) ;',
  28602. 'if (null === $mod.p) ;',
  28603. 'if ($mod.p != null) ;',
  28604. '$mod.p = $mod.v;',
  28605. '$mod.p = $mod.Obj;',
  28606. '$mod.p = $mod.C;',
  28607. '$mod.p = $mod.a;',
  28608. '$mod.p = $mod.TObject;',
  28609. '$mod.Obj = $mod.p;',
  28610. '$mod.C = $mod.p;',
  28611. '$mod.a = $mod.p;',
  28612. '$mod.p = null;',
  28613. '$mod.p = $mod.a;',
  28614. '$mod.p = $mod.s;',
  28615. '$mod.s = $mod.p;',
  28616. '']));
  28617. end;
  28618. procedure TTestModule.TestPointer_Proc;
  28619. begin
  28620. StartProgram(false);
  28621. Add('type');
  28622. Add(' TObject = class');
  28623. Add(' procedure DoIt; virtual; abstract;');
  28624. Add(' end;');
  28625. Add('procedure DoSome; begin end;');
  28626. Add('var');
  28627. Add(' o: TObject;');
  28628. Add(' p: Pointer;');
  28629. Add('begin');
  28630. Add(' p:=@DoSome;');
  28631. Add(' p:[email protected];');
  28632. ConvertProgram;
  28633. CheckSource('TestPointer_Proc',
  28634. LinesToStr([ // statements
  28635. 'rtl.createClass(this, "TObject", null, function () {',
  28636. ' this.$init = function () {',
  28637. ' };',
  28638. ' this.$final = function () {',
  28639. ' };',
  28640. '});',
  28641. 'this.DoSome = function () {',
  28642. '};',
  28643. 'this.o = null;',
  28644. 'this.p = null;',
  28645. '']),
  28646. LinesToStr([ // $mod.$main
  28647. '$mod.p = $mod.DoSome;',
  28648. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28649. '']));
  28650. end;
  28651. procedure TTestModule.TestPointer_AssignRecordFail;
  28652. begin
  28653. StartProgram(false);
  28654. Add('type');
  28655. Add(' TRec = record end;');
  28656. Add('var');
  28657. Add(' p: Pointer;');
  28658. Add(' r: TRec;');
  28659. Add('begin');
  28660. Add(' p:=r;');
  28661. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28662. nIncompatibleTypesGotExpected);
  28663. ConvertProgram;
  28664. end;
  28665. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28666. begin
  28667. StartProgram(false);
  28668. Add('type');
  28669. Add(' TArr = array[boolean] of longint;');
  28670. Add('var');
  28671. Add(' p: Pointer;');
  28672. Add(' a: TArr;');
  28673. Add('begin');
  28674. Add(' p:=a;');
  28675. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28676. nIncompatibleTypesGotExpected);
  28677. ConvertProgram;
  28678. end;
  28679. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28680. begin
  28681. StartProgram(false);
  28682. Add([
  28683. 'procedure DoIt(args: array of jsvalue); begin end;',
  28684. 'procedure DoAll; varargs; begin end;',
  28685. 'var',
  28686. ' v: jsvalue;',
  28687. 'begin',
  28688. ' DoIt([pointer(v)]);',
  28689. ' DoAll(pointer(v));',
  28690. '']);
  28691. ConvertProgram;
  28692. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28693. LinesToStr([ // statements
  28694. 'this.DoIt = function (args) {',
  28695. '};',
  28696. 'this.DoAll = function () {',
  28697. '};',
  28698. 'this.v = undefined;',
  28699. '']),
  28700. LinesToStr([ // $mod.$main
  28701. '$mod.DoIt([$mod.v]);',
  28702. '$mod.DoAll($mod.v);',
  28703. '']));
  28704. end;
  28705. procedure TTestModule.TestPointer_NonRecordFail;
  28706. begin
  28707. StartProgram(false);
  28708. Add([
  28709. 'type',
  28710. ' p = ^longint;',
  28711. 'begin',
  28712. '']);
  28713. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28714. ConvertProgram;
  28715. end;
  28716. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28717. begin
  28718. StartProgram(false);
  28719. Add([
  28720. 'procedure DoIt(p: ^longint); begin end;',
  28721. 'begin',
  28722. '']);
  28723. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28724. ConvertProgram;
  28725. end;
  28726. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28727. begin
  28728. StartProgram(false);
  28729. Add([
  28730. 'var p: ^longint;',
  28731. 'begin',
  28732. '']);
  28733. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28734. ConvertProgram;
  28735. end;
  28736. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28737. begin
  28738. StartProgram(false);
  28739. Add([
  28740. 'function DoIt: ^longint; begin end;',
  28741. 'begin',
  28742. '']);
  28743. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28744. ConvertProgram;
  28745. end;
  28746. procedure TTestModule.TestPointer_AddrOperatorFail;
  28747. begin
  28748. StartProgram(false);
  28749. Add([
  28750. 'var i: longint;',
  28751. 'begin',
  28752. ' if @i=nil then ;',
  28753. '']);
  28754. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28755. ConvertProgram;
  28756. end;
  28757. procedure TTestModule.TestPointer_ArrayParamsFail;
  28758. begin
  28759. StartProgram(false);
  28760. Add([
  28761. 'var',
  28762. ' p: Pointer;',
  28763. 'begin',
  28764. ' p:=p[1];',
  28765. '']);
  28766. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28767. ConvertProgram;
  28768. end;
  28769. procedure TTestModule.TestPointer_PointerAddFail;
  28770. begin
  28771. StartProgram(false);
  28772. Add([
  28773. 'var',
  28774. ' p: Pointer;',
  28775. 'begin',
  28776. ' p:=p+1;',
  28777. '']);
  28778. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28779. ConvertProgram;
  28780. end;
  28781. procedure TTestModule.TestPointer_IncPointerFail;
  28782. begin
  28783. StartProgram(false);
  28784. Add([
  28785. 'var',
  28786. ' p: Pointer;',
  28787. 'begin',
  28788. ' inc(p,1);',
  28789. '']);
  28790. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28791. nIncompatibleTypeArgNo);
  28792. ConvertProgram;
  28793. end;
  28794. procedure TTestModule.TestPointer_Record;
  28795. begin
  28796. StartProgram(false);
  28797. Add([
  28798. 'type',
  28799. ' TRec = record x: longint; end;',
  28800. ' PRec = ^TRec;',
  28801. 'var',
  28802. ' r: TRec;',
  28803. ' p: PRec;',
  28804. ' q: ^TRec;',
  28805. ' Ptr: pointer;',
  28806. 'begin',
  28807. ' new(p);',
  28808. ' p:=@r;',
  28809. ' r:=p^;',
  28810. ' r.x:=p^.x;',
  28811. ' p^.x:=r.x;',
  28812. ' if p^.x=3 then ;',
  28813. ' if 4=p^.x then ;',
  28814. ' dispose(p);',
  28815. ' new(q);',
  28816. ' dispose(q);',
  28817. ' Ptr:=p;',
  28818. ' p:=PRec(ptr);',
  28819. '']);
  28820. ConvertProgram;
  28821. CheckSource('TestPointer_Record',
  28822. LinesToStr([ // statements
  28823. 'rtl.recNewT(this, "TRec", function () {',
  28824. ' this.x = 0;',
  28825. ' this.$eq = function (b) {',
  28826. ' return this.x === b.x;',
  28827. ' };',
  28828. ' this.$assign = function (s) {',
  28829. ' this.x = s.x;',
  28830. ' return this;',
  28831. ' };',
  28832. '});',
  28833. 'this.r = this.TRec.$new();',
  28834. 'this.p = null;',
  28835. 'this.q = null;',
  28836. 'this.Ptr = null;',
  28837. '']),
  28838. LinesToStr([ // $mod.$main
  28839. '$mod.p = $mod.TRec.$new();',
  28840. '$mod.p = $mod.r;',
  28841. '$mod.r.$assign($mod.p);',
  28842. '$mod.r.x = $mod.p.x;',
  28843. '$mod.p.x = $mod.r.x;',
  28844. 'if ($mod.p.x === 3) ;',
  28845. 'if (4 === $mod.p.x) ;',
  28846. '$mod.p = null;',
  28847. '$mod.q = $mod.TRec.$new();',
  28848. '$mod.q = null;',
  28849. '$mod.Ptr = $mod.p;',
  28850. '$mod.p = $mod.Ptr;',
  28851. '']));
  28852. end;
  28853. procedure TTestModule.TestPointer_RecordArg;
  28854. begin
  28855. StartProgram(false);
  28856. Add([
  28857. '{$modeswitch autoderef}',
  28858. 'type',
  28859. ' TRec = record x: longint; end;',
  28860. ' PRec = ^TRec;',
  28861. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  28862. 'begin',
  28863. ' a.x:=a.x;',
  28864. ' a^.x:=a^.x;',
  28865. ' with a^ do',
  28866. ' x:=x;',
  28867. 'end;',
  28868. 'function GetIt(p: PRec): PRec;',
  28869. 'begin',
  28870. ' p.x:=p.x;',
  28871. ' p^.x:=p^.x;',
  28872. ' with p^ do',
  28873. ' x:=x;',
  28874. 'end;',
  28875. 'var',
  28876. ' r: TRec;',
  28877. ' p: PRec;',
  28878. 'begin',
  28879. ' p:=GetIt(p);',
  28880. ' p^:=GetIt(@r)^;',
  28881. ' DoIt(p,p,p);',
  28882. ' DoIt(@r,p,p);',
  28883. '']);
  28884. ConvertProgram;
  28885. CheckSource('TestPointer_RecordArg',
  28886. LinesToStr([ // statements
  28887. 'rtl.recNewT(this, "TRec", function () {',
  28888. ' this.x = 0;',
  28889. ' this.$eq = function (b) {',
  28890. ' return this.x === b.x;',
  28891. ' };',
  28892. ' this.$assign = function (s) {',
  28893. ' this.x = s.x;',
  28894. ' return this;',
  28895. ' };',
  28896. '});',
  28897. 'this.DoIt = function (a, b, c) {',
  28898. ' var Result = $mod.TRec.$new();',
  28899. ' a.x = a.x;',
  28900. ' a.x = a.x;',
  28901. ' a.x = a.x;',
  28902. ' return Result;',
  28903. '};',
  28904. 'this.GetIt = function (p) {',
  28905. ' var Result = null;',
  28906. ' p.x = p.x;',
  28907. ' p.x = p.x;',
  28908. ' p.x = p.x;',
  28909. ' return Result;',
  28910. '};',
  28911. 'this.r = this.TRec.$new();',
  28912. 'this.p = null;',
  28913. '']),
  28914. LinesToStr([ // $mod.$main
  28915. '$mod.p = $mod.GetIt($mod.p);',
  28916. '$mod.p.$assign($mod.GetIt($mod.r));',
  28917. '$mod.DoIt($mod.p, {',
  28918. ' p: $mod,',
  28919. ' get: function () {',
  28920. ' return this.p.p;',
  28921. ' },',
  28922. ' set: function (v) {',
  28923. ' this.p.p = v;',
  28924. ' }',
  28925. '}, {',
  28926. ' p: $mod,',
  28927. ' get: function () {',
  28928. ' return this.p.p;',
  28929. ' },',
  28930. ' set: function (v) {',
  28931. ' this.p.p = v;',
  28932. ' }',
  28933. '});',
  28934. '$mod.DoIt($mod.r, {',
  28935. ' p: $mod,',
  28936. ' get: function () {',
  28937. ' return this.p.p;',
  28938. ' },',
  28939. ' set: function (v) {',
  28940. ' this.p.p = v;',
  28941. ' }',
  28942. '}, {',
  28943. ' p: $mod,',
  28944. ' get: function () {',
  28945. ' return this.p.p;',
  28946. ' },',
  28947. ' set: function (v) {',
  28948. ' this.p.p = v;',
  28949. ' }',
  28950. '});',
  28951. '']));
  28952. end;
  28953. procedure TTestModule.TestJSValue_AssignToJSValue;
  28954. begin
  28955. StartProgram(false);
  28956. Add('var');
  28957. Add(' v: jsvalue;');
  28958. Add(' i: longint;');
  28959. Add(' s: string;');
  28960. Add(' b: boolean;');
  28961. Add(' d: double;');
  28962. Add(' p: pointer;');
  28963. Add('begin');
  28964. Add(' v:=v;');
  28965. Add(' v:=1;');
  28966. Add(' v:=i;');
  28967. Add(' v:='''';');
  28968. Add(' v:=''c'';');
  28969. Add(' v:=''foo'';');
  28970. Add(' v:=s;');
  28971. Add(' v:=false;');
  28972. Add(' v:=true;');
  28973. Add(' v:=b;');
  28974. Add(' v:=0.1;');
  28975. Add(' v:=d;');
  28976. Add(' v:=nil;');
  28977. Add(' v:=p;');
  28978. ConvertProgram;
  28979. CheckSource('TestJSValue_AssignToJSValue',
  28980. LinesToStr([ // statements
  28981. 'this.v = undefined;',
  28982. 'this.i = 0;',
  28983. 'this.s = "";',
  28984. 'this.b = false;',
  28985. 'this.d = 0.0;',
  28986. 'this.p = null;',
  28987. '']),
  28988. LinesToStr([ // $mod.$main
  28989. '$mod.v = $mod.v;',
  28990. '$mod.v = 1;',
  28991. '$mod.v = $mod.i;',
  28992. '$mod.v = "";',
  28993. '$mod.v = "c";',
  28994. '$mod.v = "foo";',
  28995. '$mod.v = $mod.s;',
  28996. '$mod.v = false;',
  28997. '$mod.v = true;',
  28998. '$mod.v = $mod.b;',
  28999. '$mod.v = 0.1;',
  29000. '$mod.v = $mod.d;',
  29001. '$mod.v = null;',
  29002. '$mod.v = $mod.p;',
  29003. '']));
  29004. end;
  29005. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29006. begin
  29007. StartProgram(false);
  29008. Add('type');
  29009. Add(' integer = longint;');
  29010. Add(' TYesNo = boolean;');
  29011. Add(' TFloat = double;');
  29012. Add(' TCaption = string;');
  29013. Add(' TChar = char;');
  29014. Add('var');
  29015. Add(' v: jsvalue;');
  29016. Add(' i: integer;');
  29017. Add(' s: TCaption;');
  29018. Add(' b: TYesNo;');
  29019. Add(' d: TFloat;');
  29020. Add(' c: char;');
  29021. Add('begin');
  29022. Add(' i:=longint(v);');
  29023. Add(' i:=integer(v);');
  29024. Add(' s:=string(v);');
  29025. Add(' s:=TCaption(v);');
  29026. Add(' b:=boolean(v);');
  29027. Add(' b:=TYesNo(v);');
  29028. Add(' d:=double(v);');
  29029. Add(' d:=TFloat(v);');
  29030. Add(' c:=char(v);');
  29031. Add(' c:=TChar(v);');
  29032. ConvertProgram;
  29033. CheckSource('TestJSValue_TypeCastToBaseType',
  29034. LinesToStr([ // statements
  29035. 'this.v = undefined;',
  29036. 'this.i = 0;',
  29037. 'this.s = "";',
  29038. 'this.b = false;',
  29039. 'this.d = 0.0;',
  29040. 'this.c = "";',
  29041. '']),
  29042. LinesToStr([ // $mod.$main
  29043. '$mod.i = rtl.trunc($mod.v);',
  29044. '$mod.i = rtl.trunc($mod.v);',
  29045. '$mod.s = "" + $mod.v;',
  29046. '$mod.s = "" + $mod.v;',
  29047. '$mod.b = !($mod.v == false);',
  29048. '$mod.b = !($mod.v == false);',
  29049. '$mod.d = rtl.getNumber($mod.v);',
  29050. '$mod.d = rtl.getNumber($mod.v);',
  29051. '$mod.c = rtl.getChar($mod.v);',
  29052. '$mod.c = rtl.getChar($mod.v);',
  29053. '']));
  29054. end;
  29055. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29056. begin
  29057. StartProgram(false);
  29058. Add([
  29059. 'type',
  29060. ' TArr = array of word;',
  29061. ' TRec = record end;',
  29062. ' TSet = set of boolean;',
  29063. 'procedure Fly(v: jsvalue);',
  29064. 'begin',
  29065. 'end;',
  29066. 'var',
  29067. ' a: TArr;',
  29068. ' r: TRec;',
  29069. ' s: TSet;',
  29070. 'begin',
  29071. ' Fly(jsvalue(a));',
  29072. ' Fly(jsvalue(r));',
  29073. ' Fly(jsvalue(s));',
  29074. '']);
  29075. ConvertProgram;
  29076. CheckSource('TestJSValue_TypecastToJSValue',
  29077. LinesToStr([ // statements
  29078. 'rtl.recNewT(this, "TRec", function () {',
  29079. ' this.$eq = function (b) {',
  29080. ' return true;',
  29081. ' };',
  29082. ' this.$assign = function (s) {',
  29083. ' return this;',
  29084. ' };',
  29085. '});',
  29086. 'this.Fly = function (v) {',
  29087. '};',
  29088. 'this.a = [];',
  29089. 'this.r = this.TRec.$new();',
  29090. 'this.s = {};',
  29091. '']),
  29092. LinesToStr([ // $mod.$main
  29093. '$mod.Fly($mod.a);',
  29094. '$mod.Fly($mod.r);',
  29095. '$mod.Fly($mod.s);',
  29096. '']));
  29097. end;
  29098. procedure TTestModule.TestJSValue_Equal;
  29099. begin
  29100. StartProgram(false);
  29101. Add('type');
  29102. Add(' integer = longint;');
  29103. Add(' TYesNo = boolean;');
  29104. Add(' TFloat = double;');
  29105. Add(' TCaption = string;');
  29106. Add(' TChar = char;');
  29107. Add(' TMulti = JSValue;');
  29108. Add('var');
  29109. Add(' v: jsvalue;');
  29110. Add(' i: integer;');
  29111. Add(' s: TCaption;');
  29112. Add(' b: TYesNo;');
  29113. Add(' d: TFloat;');
  29114. Add(' c: char;');
  29115. Add(' m: TMulti;');
  29116. Add('begin');
  29117. Add(' b:=v=v;');
  29118. Add(' b:=v<>v;');
  29119. Add(' b:=v=1;');
  29120. Add(' b:=v<>1;');
  29121. Add(' b:=2=v;');
  29122. Add(' b:=2<>v;');
  29123. Add(' b:=v=i;');
  29124. Add(' b:=i=v;');
  29125. Add(' b:=v=nil;');
  29126. Add(' b:=nil=v;');
  29127. Add(' b:=v=false;');
  29128. Add(' b:=true=v;');
  29129. Add(' b:=v=b;');
  29130. Add(' b:=b=v;');
  29131. Add(' b:=v=s;');
  29132. Add(' b:=s=v;');
  29133. Add(' b:=v=''foo'';');
  29134. Add(' b:=''''=v;');
  29135. Add(' b:=v=d;');
  29136. Add(' b:=d=v;');
  29137. Add(' b:=v=3.4;');
  29138. Add(' b:=5.6=v;');
  29139. Add(' b:=v=c;');
  29140. Add(' b:=c=v;');
  29141. Add(' b:=m=m;');
  29142. Add(' b:=v=m;');
  29143. Add(' b:=m=v;');
  29144. ConvertProgram;
  29145. CheckSource('TestJSValue_Equal',
  29146. LinesToStr([ // statements
  29147. 'this.v = undefined;',
  29148. 'this.i = 0;',
  29149. 'this.s = "";',
  29150. 'this.b = false;',
  29151. 'this.d = 0.0;',
  29152. 'this.c = "";',
  29153. 'this.m = undefined;',
  29154. '']),
  29155. LinesToStr([ // $mod.$main
  29156. '$mod.b = $mod.v == $mod.v;',
  29157. '$mod.b = $mod.v != $mod.v;',
  29158. '$mod.b = $mod.v == 1;',
  29159. '$mod.b = $mod.v != 1;',
  29160. '$mod.b = 2 == $mod.v;',
  29161. '$mod.b = 2 != $mod.v;',
  29162. '$mod.b = $mod.v == $mod.i;',
  29163. '$mod.b = $mod.i == $mod.v;',
  29164. '$mod.b = $mod.v == null;',
  29165. '$mod.b = null == $mod.v;',
  29166. '$mod.b = $mod.v == false;',
  29167. '$mod.b = true == $mod.v;',
  29168. '$mod.b = $mod.v == $mod.b;',
  29169. '$mod.b = $mod.b == $mod.v;',
  29170. '$mod.b = $mod.v == $mod.s;',
  29171. '$mod.b = $mod.s == $mod.v;',
  29172. '$mod.b = $mod.v == "foo";',
  29173. '$mod.b = "" == $mod.v;',
  29174. '$mod.b = $mod.v == $mod.d;',
  29175. '$mod.b = $mod.d == $mod.v;',
  29176. '$mod.b = $mod.v == 3.4;',
  29177. '$mod.b = 5.6 == $mod.v;',
  29178. '$mod.b = $mod.v == $mod.c;',
  29179. '$mod.b = $mod.c == $mod.v;',
  29180. '$mod.b = $mod.m == $mod.m;',
  29181. '$mod.b = $mod.v == $mod.m;',
  29182. '$mod.b = $mod.m == $mod.v;',
  29183. '']));
  29184. end;
  29185. procedure TTestModule.TestJSValue_If;
  29186. begin
  29187. StartProgram(false);
  29188. Add([
  29189. 'procedure Fly(var u);',
  29190. 'begin',
  29191. ' if jsvalue(u) then ;',
  29192. 'end;',
  29193. 'var',
  29194. ' v: jsvalue;',
  29195. 'begin',
  29196. ' if v then ;',
  29197. ' while v do ;',
  29198. ' repeat until v;',
  29199. '']);
  29200. ConvertProgram;
  29201. CheckSource('TestJSValue_If',
  29202. LinesToStr([ // statements
  29203. 'this.Fly = function (u) {',
  29204. ' if (u.get()) ;',
  29205. '};',
  29206. 'this.v = undefined;',
  29207. '']),
  29208. LinesToStr([ // $mod.$main
  29209. 'if ($mod.v) ;',
  29210. 'while($mod.v){',
  29211. '};',
  29212. 'do{',
  29213. '} while(!$mod.v);',
  29214. '']));
  29215. end;
  29216. procedure TTestModule.TestJSValue_Not;
  29217. begin
  29218. StartProgram(false);
  29219. Add([
  29220. 'var',
  29221. ' v: jsvalue;',
  29222. ' b: boolean;',
  29223. 'begin',
  29224. ' b:=not v;',
  29225. ' if not v then ;',
  29226. ' while not v do ;',
  29227. ' repeat until not v;',
  29228. '']);
  29229. ConvertProgram;
  29230. CheckSource('TestJSValue_If',
  29231. LinesToStr([ // statements
  29232. 'this.v = undefined;',
  29233. 'this.b = false;',
  29234. '']),
  29235. LinesToStr([ // $mod.$main
  29236. '$mod.b=!$mod.v;',
  29237. 'if (!$mod.v) ;',
  29238. 'while(!$mod.v){',
  29239. '};',
  29240. 'do{',
  29241. '} while($mod.v);',
  29242. '']));
  29243. end;
  29244. procedure TTestModule.TestJSValue_Enum;
  29245. begin
  29246. StartProgram(false);
  29247. Add('type');
  29248. Add(' TColor = (red, blue);');
  29249. Add(' TRedBlue = TColor;');
  29250. Add('var');
  29251. Add(' v: jsvalue;');
  29252. Add(' e: TColor;');
  29253. Add('begin');
  29254. Add(' v:=e;');
  29255. Add(' v:=TColor(e);');
  29256. Add(' v:=TRedBlue(e);');
  29257. Add(' e:=TColor(v);');
  29258. Add(' e:=TRedBlue(v);');
  29259. ConvertProgram;
  29260. CheckSource('TestJSValue_Enum',
  29261. LinesToStr([ // statements
  29262. 'this.TColor = {',
  29263. ' "0": "red",',
  29264. ' red: 0,',
  29265. ' "1": "blue",',
  29266. ' blue: 1',
  29267. '};',
  29268. 'this.v = undefined;',
  29269. 'this.e = 0;',
  29270. '']),
  29271. LinesToStr([ // $mod.$main
  29272. '$mod.v = $mod.e;',
  29273. '$mod.v = $mod.e;',
  29274. '$mod.v = $mod.e;',
  29275. '$mod.e = $mod.v;',
  29276. '$mod.e = $mod.v;',
  29277. '']));
  29278. end;
  29279. procedure TTestModule.TestJSValue_ClassInstance;
  29280. begin
  29281. StartProgram(false);
  29282. Add([
  29283. 'type',
  29284. ' TObject = class',
  29285. ' end;',
  29286. ' TBirdObject = TObject;',
  29287. 'var',
  29288. ' v: jsvalue;',
  29289. ' o: TObject;',
  29290. 'begin',
  29291. ' v:=o;',
  29292. ' v:=TObject(o);',
  29293. ' v:=TBirdObject(o);',
  29294. ' o:=TObject(v);',
  29295. ' o:=TBirdObject(v);',
  29296. ' if v is TObject then ;',
  29297. '']);
  29298. ConvertProgram;
  29299. CheckSource('TestJSValue_ClassInstance',
  29300. LinesToStr([ // statements
  29301. 'rtl.createClass(this, "TObject", null, function () {',
  29302. ' this.$init = function () {',
  29303. ' };',
  29304. ' this.$final = function () {',
  29305. ' };',
  29306. '});',
  29307. 'this.v = undefined;',
  29308. 'this.o = null;',
  29309. '']),
  29310. LinesToStr([ // $mod.$main
  29311. '$mod.v = $mod.o;',
  29312. '$mod.v = $mod.o;',
  29313. '$mod.v = $mod.o;',
  29314. '$mod.o = rtl.getObject($mod.v);',
  29315. '$mod.o = rtl.getObject($mod.v);',
  29316. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29317. '']));
  29318. end;
  29319. procedure TTestModule.TestJSValue_ClassOf;
  29320. begin
  29321. StartProgram(false);
  29322. Add([
  29323. 'type',
  29324. ' TClass = class of TObject;',
  29325. ' TObject = class',
  29326. ' end;',
  29327. ' TBirds = class of TBird;',
  29328. ' TBird = class(TObject) end;',
  29329. 'var',
  29330. ' v: jsvalue;',
  29331. ' c: TClass;',
  29332. 'begin',
  29333. ' v:=c;',
  29334. ' v:=TObject;',
  29335. ' v:=TClass(c);',
  29336. ' v:=TBirds(c);',
  29337. ' c:=TClass(v);',
  29338. ' c:=TBirds(v);',
  29339. ' if v is TClass then ;',
  29340. '']);
  29341. ConvertProgram;
  29342. CheckSource('TestJSValue_ClassOf',
  29343. LinesToStr([ // statements
  29344. 'rtl.createClass(this, "TObject", null, function () {',
  29345. ' this.$init = function () {',
  29346. ' };',
  29347. ' this.$final = function () {',
  29348. ' };',
  29349. '});',
  29350. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29351. '});',
  29352. 'this.v = undefined;',
  29353. 'this.c = null;',
  29354. '']),
  29355. LinesToStr([ // $mod.$main
  29356. '$mod.v = $mod.c;',
  29357. '$mod.v = $mod.TObject;',
  29358. '$mod.v = $mod.c;',
  29359. '$mod.v = $mod.c;',
  29360. '$mod.c = rtl.getObject($mod.v);',
  29361. '$mod.c = rtl.getObject($mod.v);',
  29362. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29363. '']));
  29364. end;
  29365. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29366. begin
  29367. StartProgram(false);
  29368. Add([
  29369. 'type',
  29370. ' integer = longint;',
  29371. ' TArray = array of JSValue;',
  29372. ' TArrgh = tarray;',
  29373. ' TArrInt = array of integer;',
  29374. 'var',
  29375. ' v: jsvalue;',
  29376. ' TheArray: tarray = (1,''2'');',
  29377. ' Arr: tarrgh;',
  29378. ' i: integer;',
  29379. ' ArrInt: tarrint;',
  29380. 'begin',
  29381. ' arr:=thearray;',
  29382. ' thearray:=arr;',
  29383. ' setlength(arr,2);',
  29384. ' setlength(thearray,3);',
  29385. ' arr[4]:=v;',
  29386. ' arr[5]:=length(thearray);',
  29387. ' arr[6]:=nil;',
  29388. ' arr[7]:=thearray[8];',
  29389. ' arr[low(arr)]:=high(thearray);',
  29390. ' arr:=arrint;',
  29391. ' arrInt:=tarrint(arr);',
  29392. ' if TheArray = nil then ;',
  29393. ' if nil = TheArray then ;',
  29394. ' if TheArray <> nil then ;',
  29395. ' if nil <> TheArray then ;',
  29396. '']);
  29397. ConvertProgram;
  29398. CheckSource('TestJSValue_ArrayOfJSValue',
  29399. LinesToStr([ // statements
  29400. 'this.v = undefined;',
  29401. 'this.TheArray = [1, "2"];',
  29402. 'this.Arr = [];',
  29403. 'this.i = 0;',
  29404. 'this.ArrInt = [];',
  29405. '']),
  29406. LinesToStr([ // $mod.$main
  29407. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29408. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29409. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29410. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29411. '$mod.Arr[4] = $mod.v;',
  29412. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29413. '$mod.Arr[6] = null;',
  29414. '$mod.Arr[7] = $mod.TheArray[8];',
  29415. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29416. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29417. '$mod.ArrInt = $mod.Arr;',
  29418. 'if (rtl.length($mod.TheArray) === 0) ;',
  29419. 'if (rtl.length($mod.TheArray) === 0) ;',
  29420. 'if (rtl.length($mod.TheArray) > 0) ;',
  29421. 'if (rtl.length($mod.TheArray) > 0) ;',
  29422. '']));
  29423. end;
  29424. procedure TTestModule.TestJSValue_ArrayLit;
  29425. begin
  29426. StartProgram(false);
  29427. Add([
  29428. 'type',
  29429. ' TFlag = (big,small);',
  29430. ' TArray = array of JSValue;',
  29431. ' TObject = class end;',
  29432. ' TClass = class of TObject;',
  29433. 'var',
  29434. ' v: jsvalue;',
  29435. ' a: TArray;',
  29436. ' o: TObject;',
  29437. 'begin',
  29438. ' a:=[];',
  29439. ' a:=[1];',
  29440. ' a:=[1,2];',
  29441. ' a:=[big];',
  29442. ' a:=[1,big];',
  29443. ' a:=[o,nil];',
  29444. '']);
  29445. ConvertProgram;
  29446. CheckSource('TestJSValue_ArrayLit',
  29447. LinesToStr([ // statements
  29448. 'this.TFlag = {',
  29449. ' "0": "big",',
  29450. ' big: 0,',
  29451. ' "1": "small",',
  29452. ' small: 1',
  29453. '};',
  29454. 'rtl.createClass(this, "TObject", null, function () {',
  29455. ' this.$init = function () {',
  29456. ' };',
  29457. ' this.$final = function () {',
  29458. ' };',
  29459. '});',
  29460. 'this.v = undefined;',
  29461. 'this.a = [];',
  29462. 'this.o = null;',
  29463. '']),
  29464. LinesToStr([ // $mod.$main
  29465. '$mod.a = [];',
  29466. '$mod.a = [1];',
  29467. '$mod.a = [1, 2];',
  29468. '$mod.a = [$mod.TFlag.big];',
  29469. '$mod.a = [1, $mod.TFlag.big];',
  29470. '$mod.a = [$mod.o, null];',
  29471. '']));
  29472. end;
  29473. procedure TTestModule.TestJSValue_Params;
  29474. begin
  29475. StartProgram(false);
  29476. Add('type');
  29477. Add(' integer = longint;');
  29478. Add(' TYesNo = boolean;');
  29479. Add(' TFloat = double;');
  29480. Add(' TCaption = string;');
  29481. Add(' TChar = char;');
  29482. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29483. Add('var');
  29484. Add(' l: jsvalue;');
  29485. Add('begin');
  29486. Add(' a:=a;');
  29487. Add(' l:=b;');
  29488. Add(' c:=c;');
  29489. Add(' d:=d;');
  29490. Add(' Result:=l;');
  29491. Add('end;');
  29492. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29493. Add('var');
  29494. Add(' v: jsvalue;');
  29495. Add(' i: integer;');
  29496. Add(' b: TYesNo;');
  29497. Add(' d: TFloat;');
  29498. Add(' s: TCaption;');
  29499. Add(' c: TChar;');
  29500. Add('begin');
  29501. Add(' v:=doit(v,v,v,v);');
  29502. Add(' i:=integer(dosome(i,i));');
  29503. Add(' b:=TYesNo(dosome(b,b));');
  29504. Add(' d:=TFloat(dosome(d,d));');
  29505. Add(' s:=TCaption(dosome(s,s));');
  29506. Add(' c:=TChar(dosome(c,c));');
  29507. ConvertProgram;
  29508. CheckSource('TestJSValue_Params',
  29509. LinesToStr([ // statements
  29510. 'this.DoIt = function (a, b, c, d) {',
  29511. ' var Result = undefined;',
  29512. ' var l = undefined;',
  29513. ' a = a;',
  29514. ' l = b;',
  29515. ' c.set(c.get());',
  29516. ' d.set(d.get());',
  29517. ' Result = l;',
  29518. ' return Result;',
  29519. '};',
  29520. 'this.DoSome = function (a, b) {',
  29521. ' var Result = undefined;',
  29522. ' return Result;',
  29523. '};',
  29524. 'this.v = undefined;',
  29525. 'this.i = 0;',
  29526. 'this.b = false;',
  29527. 'this.d = 0.0;',
  29528. 'this.s = "";',
  29529. 'this.c = "";',
  29530. '']),
  29531. LinesToStr([ // $mod.$main
  29532. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29533. ' p: $mod,',
  29534. ' get: function () {',
  29535. ' return this.p.v;',
  29536. ' },',
  29537. ' set: function (v) {',
  29538. ' this.p.v = v;',
  29539. ' }',
  29540. '}, {',
  29541. ' p: $mod,',
  29542. ' get: function () {',
  29543. ' return this.p.v;',
  29544. ' },',
  29545. ' set: function (v) {',
  29546. ' this.p.v = v;',
  29547. ' }',
  29548. '});',
  29549. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29550. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29551. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29552. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29553. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29554. '']));
  29555. end;
  29556. procedure TTestModule.TestJSValue_UntypedParam;
  29557. begin
  29558. StartProgram(false);
  29559. Add('function DoIt(const a; var b; out c): jsvalue;');
  29560. Add('begin');
  29561. Add(' Result:=a;');
  29562. Add(' Result:=b;');
  29563. Add(' Result:=c;');
  29564. Add(' b:=Result;');
  29565. Add(' c:=Result;');
  29566. Add('end;');
  29567. Add('var i: longint;');
  29568. Add('begin');
  29569. Add(' doit(i,i,i);');
  29570. ConvertProgram;
  29571. CheckSource('TestJSValue_UntypedParam',
  29572. LinesToStr([ // statements
  29573. 'this.DoIt = function (a, b, c) {',
  29574. ' var Result = undefined;',
  29575. ' Result = a;',
  29576. ' Result = b.get();',
  29577. ' Result = c.get();',
  29578. ' b.set(Result);',
  29579. ' c.set(Result);',
  29580. ' return Result;',
  29581. '};',
  29582. 'this.i = 0;',
  29583. '']),
  29584. LinesToStr([ // $mod.$main
  29585. '$mod.DoIt($mod.i, {',
  29586. ' p: $mod,',
  29587. ' get: function () {',
  29588. ' return this.p.i;',
  29589. ' },',
  29590. ' set: function (v) {',
  29591. ' this.p.i = v;',
  29592. ' }',
  29593. '}, {',
  29594. ' p: $mod,',
  29595. ' get: function () {',
  29596. ' return this.p.i;',
  29597. ' },',
  29598. ' set: function (v) {',
  29599. ' this.p.i = v;',
  29600. ' }',
  29601. '});',
  29602. '']));
  29603. end;
  29604. procedure TTestModule.TestJSValue_FuncResultType;
  29605. begin
  29606. StartProgram(false);
  29607. Add('type');
  29608. Add(' integer = longint;');
  29609. Add(' TJSValueArray = array of JSValue;');
  29610. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29611. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29612. Add('begin');
  29613. Add(' while Compare(P,aList[0])>0 do ;');
  29614. Add('end;');
  29615. Add('var');
  29616. Add(' Compare: TListSortCompare;');
  29617. Add(' V: JSValue;');
  29618. Add(' i: integer;');
  29619. Add('begin');
  29620. Add(' if Compare(V,V)>0 then ;');
  29621. Add(' if Compare(i,i)>1 then ;');
  29622. Add(' if Compare(nil,false)>2 then ;');
  29623. Add(' if Compare(1,true)>3 then ;');
  29624. ConvertProgram;
  29625. CheckSource('TestJSValue_UntypedParam',
  29626. LinesToStr([ // statements
  29627. 'this.Sort = function (P, aList, Compare) {',
  29628. ' while (Compare(P, aList[0]) > 0) {',
  29629. ' };',
  29630. '};',
  29631. 'this.Compare = null;',
  29632. 'this.V = undefined;',
  29633. 'this.i = 0;',
  29634. '']),
  29635. LinesToStr([ // $mod.$main
  29636. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29637. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29638. 'if ($mod.Compare(null, false) > 2) ;',
  29639. 'if ($mod.Compare(1, true) > 3) ;',
  29640. '']));
  29641. end;
  29642. procedure TTestModule.TestJSValue_ProcType_Assign;
  29643. begin
  29644. StartProgram(false);
  29645. Add('type');
  29646. Add(' integer = longint;');
  29647. Add(' TObject = class');
  29648. Add(' class function GetGlob: integer;');
  29649. Add(' function Getter: integer;');
  29650. Add(' end;');
  29651. Add('class function TObject.GetGlob: integer;');
  29652. Add('var v1: jsvalue;');
  29653. Add('begin');
  29654. Add(' v1:=@GetGlob;');
  29655. Add(' v1:[email protected];');
  29656. Add('end;');
  29657. Add('function TObject.Getter: integer;');
  29658. Add('var v2: jsvalue;');
  29659. Add('begin');
  29660. Add(' v2:=@Getter;');
  29661. Add(' v2:[email protected];');
  29662. Add(' v2:=@GetGlob;');
  29663. Add(' v2:[email protected];');
  29664. Add('end;');
  29665. Add('function GetIt(i: integer): integer;');
  29666. Add('var v3: jsvalue;');
  29667. Add('begin');
  29668. Add(' v3:=@GetIt;');
  29669. Add('end;');
  29670. Add('var');
  29671. Add(' V: JSValue;');
  29672. Add(' o: TObject;');
  29673. Add('begin');
  29674. Add(' v:=@GetIt;');
  29675. Add(' v:[email protected];');
  29676. Add(' v:[email protected];');
  29677. ConvertProgram;
  29678. CheckSource('TestJSValue_ProcType_Assign',
  29679. LinesToStr([ // statements
  29680. 'rtl.createClass(this, "TObject", null, function () {',
  29681. ' this.$init = function () {',
  29682. ' };',
  29683. ' this.$final = function () {',
  29684. ' };',
  29685. ' this.GetGlob = function () {',
  29686. ' var Result = 0;',
  29687. ' var v1 = undefined;',
  29688. ' v1 = rtl.createCallback(this, "GetGlob");',
  29689. ' v1 = rtl.createCallback(this, "GetGlob");',
  29690. ' return Result;',
  29691. ' };',
  29692. ' this.Getter = function () {',
  29693. ' var Result = 0;',
  29694. ' var v2 = undefined;',
  29695. ' v2 = rtl.createCallback(this, "Getter");',
  29696. ' v2 = rtl.createCallback(this, "Getter");',
  29697. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29698. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29699. ' return Result;',
  29700. ' };',
  29701. '});',
  29702. 'this.GetIt = function (i) {',
  29703. ' var Result = 0;',
  29704. ' var v3 = undefined;',
  29705. ' v3 = $mod.GetIt;',
  29706. ' return Result;',
  29707. '};',
  29708. 'this.V = undefined;',
  29709. 'this.o = null;',
  29710. '']),
  29711. LinesToStr([ // $mod.$main
  29712. '$mod.V = $mod.GetIt;',
  29713. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29714. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29715. '']));
  29716. end;
  29717. procedure TTestModule.TestJSValue_ProcType_Equal;
  29718. begin
  29719. StartProgram(false);
  29720. Add('type');
  29721. Add(' integer = longint;');
  29722. Add(' TObject = class');
  29723. Add(' class function GetGlob: integer;');
  29724. Add(' function Getter: integer;');
  29725. Add(' end;');
  29726. Add('class function TObject.GetGlob: integer;');
  29727. Add('var v1: jsvalue;');
  29728. Add('begin');
  29729. Add(' if v1=@GetGlob then;');
  29730. Add(' if [email protected] then ;');
  29731. Add('end;');
  29732. Add('function TObject.Getter: integer;');
  29733. Add('var v2: jsvalue;');
  29734. Add('begin');
  29735. Add(' if v2=@Getter then;');
  29736. Add(' if [email protected] then ;');
  29737. Add(' if v2=@GetGlob then;');
  29738. Add(' if [email protected] then;');
  29739. Add('end;');
  29740. Add('function GetIt(i: integer): integer;');
  29741. Add('var v3: jsvalue;');
  29742. Add('begin');
  29743. Add(' if v3=@GetIt then;');
  29744. Add('end;');
  29745. Add('var');
  29746. Add(' V: JSValue;');
  29747. Add(' o: TObject;');
  29748. Add('begin');
  29749. Add(' if v=@GetIt then;');
  29750. Add(' if [email protected] then;');
  29751. Add(' if [email protected] then;');
  29752. Add(' if @GetIt=v then;');
  29753. Add(' if @o.Getter=v then;');
  29754. Add(' if @o.GetGlob=v then;');
  29755. ConvertProgram;
  29756. CheckSource('TestJSValue_ProcType_Equal',
  29757. LinesToStr([ // statements
  29758. 'rtl.createClass(this, "TObject", null, function () {',
  29759. ' this.$init = function () {',
  29760. ' };',
  29761. ' this.$final = function () {',
  29762. ' };',
  29763. ' this.GetGlob = function () {',
  29764. ' var Result = 0;',
  29765. ' var v1 = undefined;',
  29766. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29767. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29768. ' return Result;',
  29769. ' };',
  29770. ' this.Getter = function () {',
  29771. ' var Result = 0;',
  29772. ' var v2 = undefined;',
  29773. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29774. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29775. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29776. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29777. ' return Result;',
  29778. ' };',
  29779. '});',
  29780. 'this.GetIt = function (i) {',
  29781. ' var Result = 0;',
  29782. ' var v3 = undefined;',
  29783. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29784. ' return Result;',
  29785. '};',
  29786. 'this.V = undefined;',
  29787. 'this.o = null;',
  29788. '']),
  29789. LinesToStr([ // $mod.$main
  29790. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29791. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29792. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29793. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29794. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29795. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29796. '']));
  29797. end;
  29798. procedure TTestModule.TestJSValue_ProcType_Param;
  29799. begin
  29800. StartProgram(false);
  29801. Add([
  29802. 'type',
  29803. ' variant = jsvalue;',
  29804. ' TArrVariant = array of variant;',
  29805. ' TArrVar2 = TArrVariant;',
  29806. ' TFuncInt = function: longint;',
  29807. 'function GetIt: longint;',
  29808. 'begin',
  29809. 'end;',
  29810. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29811. 'var v: variant;',
  29812. 'begin',
  29813. ' v:=arr[1];',
  29814. 'end;',
  29815. 'var s: string;',
  29816. 'begin',
  29817. ' DoIt(GetIt,[]);',
  29818. ' DoIt(@GetIt,[]);',
  29819. ' DoIt(1,[s,GetIt]);',
  29820. ' DoIt(1,[s,@GetIt]);',
  29821. '']);
  29822. ConvertProgram;
  29823. CheckSource('TestJSValue_ProcType_Param',
  29824. LinesToStr([ // statements
  29825. 'this.GetIt = function () {',
  29826. ' var Result = 0;',
  29827. ' return Result;',
  29828. '};',
  29829. 'this.DoIt = function (p, Arr) {',
  29830. ' var v = undefined;',
  29831. ' v = Arr[1];',
  29832. '};',
  29833. 'this.s = "";',
  29834. '']),
  29835. LinesToStr([ // $mod.$main
  29836. '$mod.DoIt($mod.GetIt(), []);',
  29837. '$mod.DoIt($mod.GetIt, []);',
  29838. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29839. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29840. '']));
  29841. end;
  29842. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29843. begin
  29844. StartProgram(false);
  29845. Add([
  29846. 'var',
  29847. ' v: JSValue;',
  29848. ' p: Pointer;',
  29849. 'begin',
  29850. ' p:=v;',
  29851. '']);
  29852. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29853. nIncompatibleTypesGotExpected);
  29854. ConvertProgram;
  29855. end;
  29856. procedure TTestModule.TestJSValue_OverloadDouble;
  29857. begin
  29858. StartProgram(false);
  29859. Add([
  29860. 'type',
  29861. ' integer = longint;',
  29862. ' tdatetime = double;',
  29863. 'procedure DoIt(d: double); begin end;',
  29864. 'procedure DoIt(v: jsvalue); begin end;',
  29865. 'var',
  29866. ' d: double;',
  29867. ' dt: tdatetime;',
  29868. ' i: integer;',
  29869. ' b: byte;',
  29870. ' shi: shortint;',
  29871. ' w: word;',
  29872. ' smi: smallint;',
  29873. ' lw: longword;',
  29874. ' li: longint;',
  29875. ' ni: nativeint;',
  29876. ' nu: nativeuint;',
  29877. 'begin',
  29878. ' DoIt(d);',
  29879. ' DoIt(dt);',
  29880. ' DoIt(i);',
  29881. ' DoIt(b);',
  29882. ' DoIt(shi);',
  29883. ' DoIt(w);',
  29884. ' DoIt(smi);',
  29885. ' DoIt(lw);',
  29886. ' DoIt(li);',
  29887. ' DoIt(ni);',
  29888. ' DoIt(nu);',
  29889. '']);
  29890. ConvertProgram;
  29891. CheckSource('TestJSValue_OverloadDouble',
  29892. LinesToStr([ // statements
  29893. 'this.DoIt = function (d) {',
  29894. '};',
  29895. 'this.DoIt$1 = function (v) {',
  29896. '};',
  29897. 'this.d = 0.0;',
  29898. 'this.dt = 0.0;',
  29899. 'this.i = 0;',
  29900. 'this.b = 0;',
  29901. 'this.shi = 0;',
  29902. 'this.w = 0;',
  29903. 'this.smi = 0;',
  29904. 'this.lw = 0;',
  29905. 'this.li = 0;',
  29906. 'this.ni = 0;',
  29907. 'this.nu = 0;',
  29908. '']),
  29909. LinesToStr([ // $mod.$main
  29910. '$mod.DoIt($mod.d);',
  29911. '$mod.DoIt($mod.dt);',
  29912. '$mod.DoIt$1($mod.i);',
  29913. '$mod.DoIt$1($mod.b);',
  29914. '$mod.DoIt$1($mod.shi);',
  29915. '$mod.DoIt$1($mod.w);',
  29916. '$mod.DoIt$1($mod.smi);',
  29917. '$mod.DoIt$1($mod.lw);',
  29918. '$mod.DoIt$1($mod.li);',
  29919. '$mod.DoIt$1($mod.ni);',
  29920. '$mod.DoIt$1($mod.nu);',
  29921. '']));
  29922. end;
  29923. procedure TTestModule.TestJSValue_OverloadNativeInt;
  29924. begin
  29925. StartProgram(false);
  29926. Add([
  29927. 'type',
  29928. ' integer = longint;',
  29929. ' int53 = nativeint;',
  29930. ' tdatetime = double;',
  29931. 'procedure DoIt(n: nativeint); begin end;',
  29932. 'procedure DoIt(v: jsvalue); begin end;',
  29933. 'var',
  29934. ' d: double;',
  29935. ' dt: tdatetime;',
  29936. ' i: integer;',
  29937. ' b: byte;',
  29938. ' shi: shortint;',
  29939. ' w: word;',
  29940. ' smi: smallint;',
  29941. ' lw: longword;',
  29942. ' li: longint;',
  29943. ' ni: nativeint;',
  29944. ' nu: nativeuint;',
  29945. 'begin',
  29946. ' DoIt(d);',
  29947. ' DoIt(dt);',
  29948. ' DoIt(i);',
  29949. ' DoIt(b);',
  29950. ' DoIt(shi);',
  29951. ' DoIt(w);',
  29952. ' DoIt(smi);',
  29953. ' DoIt(lw);',
  29954. ' DoIt(li);',
  29955. ' DoIt(ni);',
  29956. ' DoIt(nu);',
  29957. '']);
  29958. ConvertProgram;
  29959. CheckSource('TestJSValue_OverloadNativeInt',
  29960. LinesToStr([ // statements
  29961. 'this.DoIt = function (n) {',
  29962. '};',
  29963. 'this.DoIt$1 = function (v) {',
  29964. '};',
  29965. 'this.d = 0.0;',
  29966. 'this.dt = 0.0;',
  29967. 'this.i = 0;',
  29968. 'this.b = 0;',
  29969. 'this.shi = 0;',
  29970. 'this.w = 0;',
  29971. 'this.smi = 0;',
  29972. 'this.lw = 0;',
  29973. 'this.li = 0;',
  29974. 'this.ni = 0;',
  29975. 'this.nu = 0;',
  29976. '']),
  29977. LinesToStr([ // $mod.$main
  29978. '$mod.DoIt$1($mod.d);',
  29979. '$mod.DoIt$1($mod.dt);',
  29980. '$mod.DoIt($mod.i);',
  29981. '$mod.DoIt($mod.b);',
  29982. '$mod.DoIt($mod.shi);',
  29983. '$mod.DoIt($mod.w);',
  29984. '$mod.DoIt($mod.smi);',
  29985. '$mod.DoIt($mod.lw);',
  29986. '$mod.DoIt($mod.li);',
  29987. '$mod.DoIt($mod.ni);',
  29988. '$mod.DoIt($mod.nu);',
  29989. '']));
  29990. end;
  29991. procedure TTestModule.TestJSValue_OverloadWord;
  29992. begin
  29993. StartProgram(false);
  29994. Add([
  29995. 'type',
  29996. ' integer = longint;',
  29997. ' int53 = nativeint;',
  29998. ' tdatetime = double;',
  29999. 'procedure DoIt(w: word); begin end;',
  30000. 'procedure DoIt(v: jsvalue); begin end;',
  30001. 'var',
  30002. ' d: double;',
  30003. ' dt: tdatetime;',
  30004. ' i: integer;',
  30005. ' b: byte;',
  30006. ' shi: shortint;',
  30007. ' w: word;',
  30008. ' smi: smallint;',
  30009. ' lw: longword;',
  30010. ' li: longint;',
  30011. ' ni: nativeint;',
  30012. ' nu: nativeuint;',
  30013. 'begin',
  30014. ' DoIt(d);',
  30015. ' DoIt(dt);',
  30016. ' DoIt(i);',
  30017. ' DoIt(b);',
  30018. ' DoIt(shi);',
  30019. ' DoIt(w);',
  30020. ' DoIt(smi);',
  30021. ' DoIt(lw);',
  30022. ' DoIt(li);',
  30023. ' DoIt(ni);',
  30024. ' DoIt(nu);',
  30025. '']);
  30026. ConvertProgram;
  30027. CheckSource('TestJSValue_OverloadWord',
  30028. LinesToStr([ // statements
  30029. 'this.DoIt = function (w) {',
  30030. '};',
  30031. 'this.DoIt$1 = function (v) {',
  30032. '};',
  30033. 'this.d = 0.0;',
  30034. 'this.dt = 0.0;',
  30035. 'this.i = 0;',
  30036. 'this.b = 0;',
  30037. 'this.shi = 0;',
  30038. 'this.w = 0;',
  30039. 'this.smi = 0;',
  30040. 'this.lw = 0;',
  30041. 'this.li = 0;',
  30042. 'this.ni = 0;',
  30043. 'this.nu = 0;',
  30044. '']),
  30045. LinesToStr([ // $mod.$main
  30046. '$mod.DoIt$1($mod.d);',
  30047. '$mod.DoIt$1($mod.dt);',
  30048. '$mod.DoIt$1($mod.i);',
  30049. '$mod.DoIt($mod.b);',
  30050. '$mod.DoIt($mod.shi);',
  30051. '$mod.DoIt($mod.w);',
  30052. '$mod.DoIt$1($mod.smi);',
  30053. '$mod.DoIt$1($mod.lw);',
  30054. '$mod.DoIt$1($mod.li);',
  30055. '$mod.DoIt$1($mod.ni);',
  30056. '$mod.DoIt$1($mod.nu);',
  30057. '']));
  30058. end;
  30059. procedure TTestModule.TestJSValue_OverloadString;
  30060. begin
  30061. StartProgram(false);
  30062. Add([
  30063. 'type',
  30064. ' uni = string;',
  30065. ' WChar = char;',
  30066. 'procedure DoIt(s: string); begin end;',
  30067. 'procedure DoIt(v: jsvalue); begin end;',
  30068. 'var',
  30069. ' s: string;',
  30070. ' c: char;',
  30071. ' u: uni;',
  30072. 'begin',
  30073. ' DoIt(s);',
  30074. ' DoIt(c);',
  30075. ' DoIt(u);',
  30076. '']);
  30077. ConvertProgram;
  30078. CheckSource('TestJSValue_OverloadString',
  30079. LinesToStr([ // statements
  30080. 'this.DoIt = function (s) {',
  30081. '};',
  30082. 'this.DoIt$1 = function (v) {',
  30083. '};',
  30084. 'this.s = "";',
  30085. 'this.c = "";',
  30086. 'this.u = "";',
  30087. '']),
  30088. LinesToStr([ // $mod.$main
  30089. '$mod.DoIt($mod.s);',
  30090. '$mod.DoIt($mod.c);',
  30091. '$mod.DoIt($mod.u);',
  30092. '']));
  30093. end;
  30094. procedure TTestModule.TestJSValue_OverloadChar;
  30095. begin
  30096. StartProgram(false);
  30097. Add([
  30098. 'type',
  30099. ' uni = string;',
  30100. ' WChar = char;',
  30101. 'procedure DoIt(c: char); begin end;',
  30102. 'procedure DoIt(v: jsvalue); begin end;',
  30103. 'var',
  30104. ' s: string;',
  30105. ' c: char;',
  30106. ' u: uni;',
  30107. 'begin',
  30108. ' DoIt(s);',
  30109. ' DoIt(c);',
  30110. ' DoIt(u);',
  30111. '']);
  30112. ConvertProgram;
  30113. CheckSource('TestJSValue_OverloadChar',
  30114. LinesToStr([ // statements
  30115. 'this.DoIt = function (c) {',
  30116. '};',
  30117. 'this.DoIt$1 = function (v) {',
  30118. '};',
  30119. 'this.s = "";',
  30120. 'this.c = "";',
  30121. 'this.u = "";',
  30122. '']),
  30123. LinesToStr([ // $mod.$main
  30124. '$mod.DoIt$1($mod.s);',
  30125. '$mod.DoIt($mod.c);',
  30126. '$mod.DoIt$1($mod.u);',
  30127. '']));
  30128. end;
  30129. procedure TTestModule.TestJSValue_OverloadPointer;
  30130. begin
  30131. StartProgram(false);
  30132. Add([
  30133. 'type',
  30134. ' TObject = class end;',
  30135. 'procedure DoIt(p: pointer); begin end;',
  30136. 'procedure DoIt(v: jsvalue); begin end;',
  30137. 'var',
  30138. ' o: TObject;',
  30139. 'begin',
  30140. ' DoIt(o);',
  30141. '']);
  30142. ConvertProgram;
  30143. CheckSource('TestJSValue_OverloadPointer',
  30144. LinesToStr([ // statements
  30145. 'rtl.createClass(this, "TObject", null, function () {',
  30146. ' this.$init = function () {',
  30147. ' };',
  30148. ' this.$final = function () {',
  30149. ' };',
  30150. '});',
  30151. 'this.DoIt = function (p) {',
  30152. '};',
  30153. 'this.DoIt$1 = function (v) {',
  30154. '};',
  30155. 'this.o = null;',
  30156. '']),
  30157. LinesToStr([ // $mod.$main
  30158. '$mod.DoIt($mod.o);',
  30159. '']));
  30160. end;
  30161. procedure TTestModule.TestJSValue_ForIn;
  30162. begin
  30163. StartProgram(false);
  30164. Add([
  30165. 'var',
  30166. ' v: JSValue;',
  30167. ' key: string;',
  30168. 'begin',
  30169. ' for key in v do begin',
  30170. ' if key=''abc'' then ;',
  30171. ' end;',
  30172. '']);
  30173. ConvertProgram;
  30174. CheckSource('TestJSValue_ForIn',
  30175. LinesToStr([ // statements
  30176. 'this.v = undefined;',
  30177. 'this.key = "";',
  30178. '']),
  30179. LinesToStr([ // $mod.$main
  30180. 'for ($mod.key in $mod.v) {',
  30181. ' if ($mod.key === "abc") ;',
  30182. '};',
  30183. '']));
  30184. end;
  30185. procedure TTestModule.TestRTTI_IntRange;
  30186. begin
  30187. WithTypeInfo:=true;
  30188. StartProgram(true,[supTypeInfo]);
  30189. Add([
  30190. '{$modeswitch externalclass}',
  30191. 'type',
  30192. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  30193. ' TColor = type TGraphicsColor;',
  30194. 'var',
  30195. ' p: TTypeInfo;',
  30196. ' k: TTypeKind;',
  30197. 'begin',
  30198. ' p:=typeinfo(TGraphicsColor);',
  30199. ' p:=typeinfo(TColor);',
  30200. ' k:=GetTypeKind(TGraphicsColor);',
  30201. ' k:=GetTypeKind(TColor);',
  30202. '']);
  30203. ConvertProgram;
  30204. CheckSource('TestRTTI_IntRange',
  30205. LinesToStr([ // statements
  30206. 'this.$rtti.$Int("TGraphicsColor", {',
  30207. ' minvalue: -2147483648,',
  30208. ' maxvalue: 2147483647,',
  30209. ' ordtype: 4',
  30210. '});',
  30211. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  30212. 'this.p = null;',
  30213. 'this.k = 0;',
  30214. '']),
  30215. LinesToStr([ // $mod.$main
  30216. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  30217. '$mod.p = $mod.$rtti["TColor"];',
  30218. '$mod.k = 1;',
  30219. '$mod.k = 1;',
  30220. '']));
  30221. end;
  30222. procedure TTestModule.TestRTTI_Double;
  30223. begin
  30224. WithTypeInfo:=true;
  30225. StartProgram(true,[supTypeInfo]);
  30226. Add([
  30227. '{$modeswitch externalclass}',
  30228. 'type',
  30229. ' TFloat = type double;',
  30230. 'var',
  30231. ' p: TTypeInfo;',
  30232. 'begin',
  30233. ' p:=typeinfo(double);',
  30234. ' p:=typeinfo(TFloat);',
  30235. '']);
  30236. ConvertProgram;
  30237. CheckSource('TestRTTI_Double',
  30238. LinesToStr([ // statements
  30239. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30240. 'this.p = null;',
  30241. '']),
  30242. LinesToStr([ // $mod.$main
  30243. '$mod.p = rtl.double;',
  30244. '$mod.p = $mod.$rtti["TFloat"];',
  30245. '']));
  30246. end;
  30247. procedure TTestModule.TestRTTI_ProcType;
  30248. begin
  30249. WithTypeInfo:=true;
  30250. StartProgram(false);
  30251. Add('type');
  30252. Add(' TProcA = procedure;');
  30253. Add(' TMethodB = procedure of object;');
  30254. Add(' TProcC = procedure; varargs;');
  30255. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  30256. Add(' TProcE = function: nativeint;');
  30257. Add(' TProcF = function(const p: TProcA): nativeuint;');
  30258. Add('var p: pointer;');
  30259. Add('begin');
  30260. Add(' p:=typeinfo(tproca);');
  30261. ConvertProgram;
  30262. CheckSource('TestRTTI_ProcType',
  30263. LinesToStr([ // statements
  30264. 'this.$rtti.$ProcVar("TProcA", {',
  30265. ' procsig: rtl.newTIProcSig([])',
  30266. '});',
  30267. 'this.$rtti.$MethodVar("TMethodB", {',
  30268. ' procsig: rtl.newTIProcSig([]),',
  30269. ' methodkind: 0',
  30270. '});',
  30271. 'this.$rtti.$ProcVar("TProcC", {',
  30272. ' procsig: rtl.newTIProcSig([], null, 2)',
  30273. '});',
  30274. 'this.$rtti.$ProcVar("TProcD", {',
  30275. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30276. '});',
  30277. 'this.$rtti.$ProcVar("TProcE", {',
  30278. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30279. '});',
  30280. 'this.$rtti.$ProcVar("TProcF", {',
  30281. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  30282. '});',
  30283. 'this.p = null;',
  30284. '']),
  30285. LinesToStr([ // $mod.$main
  30286. '$mod.p = $mod.$rtti["TProcA"];',
  30287. '']));
  30288. end;
  30289. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  30290. begin
  30291. WithTypeInfo:=true;
  30292. AddModuleWithIntfImplSrc('unit2.pas',
  30293. LinesToStr([
  30294. 'type',
  30295. ' TObject = class end;'
  30296. ]),
  30297. '');
  30298. StartUnit(true);
  30299. Add('interface');
  30300. Add('uses unit2;');
  30301. Add('type');
  30302. Add(' TProcA = function(o: tobject): tobject;');
  30303. Add('implementation');
  30304. Add('type');
  30305. Add(' TProcB = function(o: tobject): tobject;');
  30306. Add('var p: Pointer;');
  30307. Add('initialization');
  30308. Add(' p:=typeinfo(tproca);');
  30309. Add(' p:=typeinfo(tprocb);');
  30310. ConvertUnit;
  30311. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30312. LinesToStr([ // statements
  30313. 'var $impl = $mod.$impl;',
  30314. 'this.$rtti.$ProcVar("TProcA", {',
  30315. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30316. '});',
  30317. '']),
  30318. LinesToStr([ // this.$init
  30319. '$impl.p = $mod.$rtti["TProcA"];',
  30320. '$impl.p = $mod.$rtti["TProcB"];',
  30321. '']),
  30322. LinesToStr([ // implementation
  30323. '$mod.$rtti.$ProcVar("TProcB", {',
  30324. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30325. '});',
  30326. '$impl.p = null;',
  30327. '']) );
  30328. end;
  30329. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30330. begin
  30331. WithTypeInfo:=true;
  30332. StartProgram(false);
  30333. Add(['var',
  30334. ' ProcA: procedure;',
  30335. ' MethodB: procedure of object;',
  30336. ' ProcC: procedure; varargs;',
  30337. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  30338. ' ProcE: function: nativeint;',
  30339. ' p: pointer;',
  30340. 'begin',
  30341. ' p:=typeinfo(proca);']);
  30342. ConvertProgram;
  30343. CheckSource('TestRTTI_ProcTypeAnonymous',
  30344. LinesToStr([ // statements
  30345. 'this.$rtti.$ProcVar("ProcA$a", {',
  30346. ' procsig: rtl.newTIProcSig([])',
  30347. '});',
  30348. 'this.ProcA = null;',
  30349. 'this.$rtti.$MethodVar("MethodB$a", {',
  30350. ' procsig: rtl.newTIProcSig([]),',
  30351. ' methodkind: 0',
  30352. '});',
  30353. 'this.MethodB = null;',
  30354. 'this.$rtti.$ProcVar("ProcC$a", {',
  30355. ' procsig: rtl.newTIProcSig([], null, 2)',
  30356. '});',
  30357. 'this.ProcC = null;',
  30358. 'this.$rtti.$ProcVar("ProcD$a", {',
  30359. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30360. '});',
  30361. 'this.ProcD = null;',
  30362. 'this.$rtti.$ProcVar("ProcE$a", {',
  30363. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30364. '});',
  30365. 'this.ProcE = null;',
  30366. 'this.p = null;',
  30367. '']),
  30368. LinesToStr([ // $mod.$main
  30369. '$mod.p = $mod.$rtti["ProcA$a"];',
  30370. '']));
  30371. end;
  30372. procedure TTestModule.TestRTTI_EnumAndSetType;
  30373. begin
  30374. WithTypeInfo:=true;
  30375. StartProgram(false);
  30376. Add('type');
  30377. Add(' TFlag = (light,dark);');
  30378. Add(' TFlags = set of TFlag;');
  30379. Add(' TProc = function(f: TFlags): TFlag;');
  30380. Add('var p: pointer;');
  30381. Add('begin');
  30382. Add(' p:=typeinfo(tflag);');
  30383. Add(' p:=typeinfo(tflags);');
  30384. ConvertProgram;
  30385. CheckSource('TestRTTI_EnumAndType',
  30386. LinesToStr([ // statements
  30387. 'this.TFlag = {',
  30388. ' "0": "light",',
  30389. ' light: 0,',
  30390. ' "1": "dark",',
  30391. ' dark: 1',
  30392. '};',
  30393. 'this.$rtti.$Enum("TFlag", {',
  30394. ' minvalue: 0,',
  30395. ' maxvalue: 1,',
  30396. ' ordtype: 1,',
  30397. ' enumtype: this.TFlag',
  30398. '});',
  30399. 'this.$rtti.$Set("TFlags", {',
  30400. ' comptype: this.$rtti["TFlag"]',
  30401. '});',
  30402. 'this.$rtti.$ProcVar("TProc", {',
  30403. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30404. '});',
  30405. 'this.p = null;',
  30406. '']),
  30407. LinesToStr([ // $mod.$main
  30408. '$mod.p = $mod.$rtti["TFlag"];',
  30409. '$mod.p = $mod.$rtti["TFlags"];',
  30410. '']));
  30411. end;
  30412. procedure TTestModule.TestRTTI_EnumRange;
  30413. begin
  30414. WithTypeInfo:=true;
  30415. StartProgram(false);
  30416. Add([
  30417. 'type',
  30418. ' TCol = (red,green,blue);',
  30419. ' TColRg = green..blue;',
  30420. ' TSetOfColRg = set of TColRg;',
  30421. 'var p: pointer;',
  30422. 'begin',
  30423. ' p:=typeinfo(tcolrg);',
  30424. ' p:=typeinfo(tsetofcolrg);',
  30425. '']);
  30426. ConvertProgram;
  30427. end;
  30428. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30429. begin
  30430. WithTypeInfo:=true;
  30431. StartProgram(false);
  30432. Add('type');
  30433. Add(' TFlags = set of (red, green);');
  30434. Add('var');
  30435. Add(' f: TFlags;');
  30436. Add('begin');
  30437. Add(' Include(f,red);');
  30438. ConvertProgram;
  30439. CheckSource('TestRTTI_AnonymousEnumType',
  30440. LinesToStr([ // statements
  30441. 'this.TFlags$a = {',
  30442. ' "0": "red",',
  30443. ' red: 0,',
  30444. ' "1": "green",',
  30445. ' green: 1',
  30446. '};',
  30447. 'this.$rtti.$Enum("TFlags$a", {',
  30448. ' minvalue: 0,',
  30449. ' maxvalue: 1,',
  30450. ' ordtype: 1,',
  30451. ' enumtype: this.TFlags$a',
  30452. '});',
  30453. 'this.$rtti.$Set("TFlags", {',
  30454. ' comptype: this.$rtti["TFlags$a"]',
  30455. '});',
  30456. 'this.f = {};',
  30457. '']),
  30458. LinesToStr([
  30459. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30460. '']));
  30461. end;
  30462. procedure TTestModule.TestRTTI_StaticArray;
  30463. begin
  30464. WithTypeInfo:=true;
  30465. StartProgram(false);
  30466. Add('type');
  30467. Add(' TFlag = (light,dark);');
  30468. Add(' TFlagNames = array[TFlag] of string;');
  30469. Add(' TBoolNames = array[boolean] of string;');
  30470. Add(' TByteArray = array[1..32768] of byte;');
  30471. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30472. Add('var p: pointer;');
  30473. Add('begin');
  30474. Add(' p:=typeinfo(TFlagNames);');
  30475. Add(' p:=typeinfo(TBoolNames);');
  30476. ConvertProgram;
  30477. CheckSource('TestRTTI_StaticArray',
  30478. LinesToStr([ // statements
  30479. 'this.TFlag = {',
  30480. ' "0": "light",',
  30481. ' light: 0,',
  30482. ' "1": "dark",',
  30483. ' dark: 1',
  30484. '};',
  30485. 'this.$rtti.$Enum("TFlag", {',
  30486. ' minvalue: 0,',
  30487. ' maxvalue: 1,',
  30488. ' ordtype: 1,',
  30489. ' enumtype: this.TFlag',
  30490. '});',
  30491. 'this.$rtti.$StaticArray("TFlagNames", {',
  30492. ' dims: [2],',
  30493. ' eltype: rtl.string',
  30494. '});',
  30495. 'this.$rtti.$StaticArray("TBoolNames", {',
  30496. ' dims: [2],',
  30497. ' eltype: rtl.string',
  30498. '});',
  30499. 'this.$rtti.$StaticArray("TByteArray", {',
  30500. ' dims: [32768],',
  30501. ' eltype: rtl.byte',
  30502. '});',
  30503. 'this.$rtti.$ProcVar("TProc", {',
  30504. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30505. '});',
  30506. 'this.p = null;',
  30507. '']),
  30508. LinesToStr([ // $mod.$main
  30509. '$mod.p = $mod.$rtti["TFlagNames"];',
  30510. '$mod.p = $mod.$rtti["TBoolNames"];',
  30511. '']));
  30512. end;
  30513. procedure TTestModule.TestRTTI_DynArray;
  30514. begin
  30515. WithTypeInfo:=true;
  30516. StartProgram(false);
  30517. Add('type');
  30518. Add(' TArrStr = array of string;');
  30519. Add(' TArr2Dim = array of tarrstr;');
  30520. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30521. Add('var p: pointer;');
  30522. Add('begin');
  30523. Add(' p:=typeinfo(tarrstr);');
  30524. Add(' p:=typeinfo(tarr2dim);');
  30525. ConvertProgram;
  30526. CheckSource('TestRTTI_DynArray',
  30527. LinesToStr([ // statements
  30528. 'this.$rtti.$DynArray("TArrStr", {',
  30529. ' eltype: rtl.string',
  30530. '});',
  30531. 'this.$rtti.$DynArray("TArr2Dim", {',
  30532. ' eltype: this.$rtti["TArrStr"]',
  30533. '});',
  30534. 'this.$rtti.$ProcVar("TProc", {',
  30535. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30536. '});',
  30537. 'this.p = null;',
  30538. '']),
  30539. LinesToStr([ // $mod.$main
  30540. '$mod.p = $mod.$rtti["TArrStr"];',
  30541. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30542. '']));
  30543. end;
  30544. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30545. begin
  30546. WithTypeInfo:=true;
  30547. StartProgram(false);
  30548. Add('type');
  30549. Add(' TArr = array of array of longint;');
  30550. Add('var a: TArr;');
  30551. Add('begin');
  30552. ConvertProgram;
  30553. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30554. LinesToStr([ // statements
  30555. 'this.$rtti.$DynArray("TArr$a", {',
  30556. ' eltype: rtl.longint',
  30557. '});',
  30558. 'this.$rtti.$DynArray("TArr", {',
  30559. ' eltype: this.$rtti["TArr$a"]',
  30560. '});',
  30561. 'this.a = [];',
  30562. '']),
  30563. LinesToStr([ // $mod.$main
  30564. ]));
  30565. end;
  30566. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30567. begin
  30568. WithTypeInfo:=true;
  30569. StartProgram(false);
  30570. Add('type');
  30571. Add(' TObject = class');
  30572. Add(' published');
  30573. Add(' procedure Proc; virtual; abstract;');
  30574. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30575. Add(' end;');
  30576. Add('begin');
  30577. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30578. nDuplicatePublishedMethodXAtY);
  30579. ConvertProgram;
  30580. end;
  30581. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30582. begin
  30583. WithTypeInfo:=true;
  30584. StartUnit(false);
  30585. Add([
  30586. 'interface',
  30587. 'type',
  30588. ' TObject = class',
  30589. ' end;',
  30590. ' {$M+}',
  30591. ' TBird = class',
  30592. ' procedure Fly;',
  30593. ' end;',
  30594. ' {$M-}',
  30595. 'type',
  30596. ' TEagle = class(TBird)',
  30597. ' procedure Fly;',
  30598. ' end;',
  30599. 'implementation',
  30600. 'procedure TBird.Fly;',
  30601. 'begin',
  30602. 'end;',
  30603. 'procedure TEagle.Fly;',
  30604. 'begin',
  30605. 'end;',
  30606. '']);
  30607. ConvertUnit;
  30608. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30609. LinesToStr([ // statements
  30610. 'rtl.createClass(this, "TObject", null, function () {',
  30611. ' this.$init = function () {',
  30612. ' };',
  30613. ' this.$final = function () {',
  30614. ' };',
  30615. '});',
  30616. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30617. ' this.Fly = function () {',
  30618. ' };',
  30619. ' var $r = this.$rtti;',
  30620. ' $r.addMethod("Fly", 0, []);',
  30621. '});',
  30622. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30623. ' this.Fly = function () {',
  30624. ' };',
  30625. ' var $r = this.$rtti;',
  30626. ' $r.addMethod("Fly", 0, []);',
  30627. '});',
  30628. '']),
  30629. LinesToStr([ // $mod.$main
  30630. ]));
  30631. CheckResolverUnexpectedHints(true);
  30632. end;
  30633. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30634. begin
  30635. WithTypeInfo:=true;
  30636. StartProgram(false);
  30637. Add('type');
  30638. Add(' TObject = class');
  30639. Add(' published');
  30640. Add(' procedure Proc; external name ''foo'';');
  30641. Add(' end;');
  30642. Add('begin');
  30643. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30644. nPublishedNameMustMatchExternal);
  30645. ConvertProgram;
  30646. end;
  30647. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30648. begin
  30649. WithTypeInfo:=true;
  30650. StartProgram(false);
  30651. Add('type');
  30652. Add(' TObject = class');
  30653. Add(' class var FA: longint;');
  30654. Add(' published');
  30655. Add(' class property A: longint read FA;');
  30656. Add(' end;');
  30657. Add('begin');
  30658. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30659. nInvalidXModifierY);
  30660. ConvertProgram;
  30661. end;
  30662. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30663. begin
  30664. WithTypeInfo:=true;
  30665. StartProgram(false);
  30666. Add('type');
  30667. Add(' TObject = class');
  30668. Add(' published');
  30669. Add(' class var FA: longint;');
  30670. Add(' end;');
  30671. Add('begin');
  30672. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30673. nSymbolCannotBePublished);
  30674. ConvertProgram;
  30675. end;
  30676. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30677. begin
  30678. WithTypeInfo:=true;
  30679. StartProgram(false);
  30680. Add('{$modeswitch externalclass}');
  30681. Add('type');
  30682. Add(' TObject = class');
  30683. Add(' published');
  30684. Add(' V: longint; external name ''foo'';');
  30685. Add(' end;');
  30686. Add('begin');
  30687. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30688. nPublishedNameMustMatchExternal);
  30689. ConvertProgram;
  30690. end;
  30691. procedure TTestModule.TestRTTI_Class_Field;
  30692. begin
  30693. WithTypeInfo:=true;
  30694. StartProgram(false);
  30695. Add('{$modeswitch externalclass}');
  30696. Add('type');
  30697. Add(' TObject = class');
  30698. Add(' private');
  30699. Add(' FPropA: string;');
  30700. Add(' published');
  30701. Add(' VarLI: longint;');
  30702. Add(' VarC: char;');
  30703. Add(' VarS: string;');
  30704. Add(' VarD: double;');
  30705. Add(' VarB: boolean;');
  30706. Add(' VarLW: longword;');
  30707. Add(' VarSmI: smallint;');
  30708. Add(' VarW: word;');
  30709. Add(' VarShI: shortint;');
  30710. Add(' VarBy: byte;');
  30711. Add(' VarExt: longint external name ''VarExt'';');
  30712. Add(' ArrA, ArrB: array of byte;');
  30713. Add(' end;');
  30714. Add('var p: pointer;');
  30715. Add(' Obj: tobject;');
  30716. Add('begin');
  30717. Add(' p:=typeinfo(tobject);');
  30718. Add(' p:=typeinfo(p);');
  30719. Add(' p:=typeinfo(obj);');
  30720. ConvertProgram;
  30721. CheckSource('TestRTTI_Class_Field',
  30722. LinesToStr([ // statements
  30723. 'rtl.createClass(this, "TObject", null, function () {',
  30724. ' this.$init = function () {',
  30725. ' this.FPropA = "";',
  30726. ' this.VarLI = 0;',
  30727. ' this.VarC = "";',
  30728. ' this.VarS = "";',
  30729. ' this.VarD = 0.0;',
  30730. ' this.VarB = false;',
  30731. ' this.VarLW = 0;',
  30732. ' this.VarSmI = 0;',
  30733. ' this.VarW = 0;',
  30734. ' this.VarShI = 0;',
  30735. ' this.VarBy = 0;',
  30736. ' this.ArrA = [];',
  30737. ' this.ArrB = [];',
  30738. ' };',
  30739. ' this.$final = function () {',
  30740. ' this.ArrA = undefined;',
  30741. ' this.ArrB = undefined;',
  30742. ' };',
  30743. ' var $r = this.$rtti;',
  30744. ' $r.addField("VarLI", rtl.longint);',
  30745. ' $r.addField("VarC", rtl.char);',
  30746. ' $r.addField("VarS", rtl.string);',
  30747. ' $r.addField("VarD", rtl.double);',
  30748. ' $r.addField("VarB", rtl.boolean);',
  30749. ' $r.addField("VarLW", rtl.longword);',
  30750. ' $r.addField("VarSmI", rtl.smallint);',
  30751. ' $r.addField("VarW", rtl.word);',
  30752. ' $r.addField("VarShI", rtl.shortint);',
  30753. ' $r.addField("VarBy", rtl.byte);',
  30754. ' $r.addField("VarExt", rtl.longint);',
  30755. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30756. ' eltype: rtl.byte',
  30757. ' });',
  30758. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30759. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30760. '});',
  30761. 'this.p = null;',
  30762. 'this.Obj = null;',
  30763. '']),
  30764. LinesToStr([ // $mod.$main
  30765. '$mod.p = $mod.$rtti["TObject"];',
  30766. '$mod.p = rtl.pointer;',
  30767. '$mod.p = $mod.Obj.$rtti;',
  30768. '']));
  30769. end;
  30770. procedure TTestModule.TestRTTI_Class_Method;
  30771. begin
  30772. WithTypeInfo:=true;
  30773. StartProgram(false);
  30774. Add([
  30775. 'type',
  30776. ' TObject = class',
  30777. ' private',
  30778. ' procedure Internal; external name ''$intern'';',
  30779. ' published',
  30780. ' procedure Click; virtual; abstract;',
  30781. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30782. ' function GetNotify: boolean; external name ''GetNotify'';',
  30783. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30784. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30785. ' end;',
  30786. 'begin']);
  30787. ConvertProgram;
  30788. CheckSource('TestRTTI_Class_Method',
  30789. LinesToStr([ // statements
  30790. 'rtl.createClass(this, "TObject", null, function () {',
  30791. ' this.$init = function () {',
  30792. ' };',
  30793. ' this.$final = function () {',
  30794. ' };',
  30795. ' var $r = this.$rtti;',
  30796. ' $r.addMethod("Click", 0, []);',
  30797. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30798. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30799. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30800. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30801. '});',
  30802. '']),
  30803. LinesToStr([ // $mod.$main
  30804. '']));
  30805. end;
  30806. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30807. begin
  30808. WithTypeInfo:=true;
  30809. StartProgram(false);
  30810. Add('type');
  30811. Add(' TObject = class');
  30812. Add(' published');
  30813. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30814. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30815. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30816. Add(' end;');
  30817. Add('begin');
  30818. ConvertProgram;
  30819. CheckSource('TestRTTI_Class_MethodOpenArray',
  30820. LinesToStr([ // statements
  30821. 'rtl.createClass(this, "TObject", null, function () {',
  30822. ' this.$init = function () {',
  30823. ' };',
  30824. ' this.$final = function () {',
  30825. ' };',
  30826. ' var $r = this.$rtti;',
  30827. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30828. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30829. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30830. '});',
  30831. '']),
  30832. LinesToStr([ // $mod.$main
  30833. '']));
  30834. end;
  30835. procedure TTestModule.TestRTTI_Class_Property;
  30836. begin
  30837. WithTypeInfo:=true;
  30838. StartProgram(false);
  30839. Add('{$modeswitch externalclass}');
  30840. Add('type');
  30841. Add(' TObject = class');
  30842. Add(' private');
  30843. Add(' FColor: longint;');
  30844. Add(' FColorStored: boolean;');
  30845. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30846. Add(' function GetColor: longint; virtual; abstract;');
  30847. Add(' function GetColorStored: boolean; virtual; abstract;');
  30848. Add(' FExtSize: longint external name ''$extSize'';');
  30849. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30850. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30851. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30852. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30853. Add(' published');
  30854. Add(' property ColorA: longint read FColor;');
  30855. Add(' property ColorB: longint write FColor;');
  30856. Add(' property ColorC: longint read GetColor write SetColor;');
  30857. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  30858. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  30859. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  30860. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  30861. Add(' end;');
  30862. Add('begin');
  30863. ConvertProgram;
  30864. CheckSource('TestRTTI_Class_Property',
  30865. LinesToStr([ // statements
  30866. 'rtl.createClass(this, "TObject", null, function () {',
  30867. ' this.$init = function () {',
  30868. ' this.FColor = 0;',
  30869. ' this.FColorStored = false;',
  30870. ' };',
  30871. ' this.$final = function () {',
  30872. ' };',
  30873. ' var $r = this.$rtti;',
  30874. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  30875. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  30876. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  30877. ' $r.addProperty(',
  30878. ' "ColorD",',
  30879. ' 8,',
  30880. ' rtl.longint,',
  30881. ' "FColor",',
  30882. ' "FColor",',
  30883. ' {',
  30884. ' stored: "FColorStored"',
  30885. ' }',
  30886. ' );',
  30887. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  30888. ' $r.addProperty(',
  30889. ' "ExtSizeB",',
  30890. ' 11,',
  30891. ' rtl.longint,',
  30892. ' "$getSize",',
  30893. ' "$setSize",',
  30894. ' {',
  30895. ' stored: "$extSizeStored"',
  30896. ' }',
  30897. ' );',
  30898. ' $r.addProperty(',
  30899. ' "ExtSizeC",',
  30900. ' 12,',
  30901. ' rtl.longint,',
  30902. ' "$extSize",',
  30903. ' "$extSize",',
  30904. ' {',
  30905. ' stored: "$getExtSizeStored"',
  30906. ' }',
  30907. ' );',
  30908. '});',
  30909. '']),
  30910. LinesToStr([ // $mod.$main
  30911. '']));
  30912. end;
  30913. procedure TTestModule.TestRTTI_Class_PropertyParams;
  30914. begin
  30915. WithTypeInfo:=true;
  30916. StartProgram(false);
  30917. Add('{$modeswitch externalclass}');
  30918. Add('type');
  30919. Add(' integer = longint;');
  30920. Add(' TObject = class');
  30921. Add(' private');
  30922. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  30923. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  30924. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  30925. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  30926. Add(' published');
  30927. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  30928. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  30929. Add(' end;');
  30930. Add('begin');
  30931. ConvertProgram;
  30932. CheckSource('TestRTTI_Class_PropertyParams',
  30933. LinesToStr([ // statements
  30934. 'rtl.createClass(this, "TObject", null, function () {',
  30935. ' this.$init = function () {',
  30936. ' };',
  30937. ' this.$final = function () {',
  30938. ' };',
  30939. ' var $r = this.$rtti;',
  30940. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  30941. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  30942. '});',
  30943. '']),
  30944. LinesToStr([ // $mod.$main
  30945. '']));
  30946. end;
  30947. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  30948. begin
  30949. WithTypeInfo:=true;
  30950. AddModuleWithIntfImplSrc('unit1.pas',
  30951. 'type TColor = -5..5;',
  30952. '');
  30953. StartProgram(true);
  30954. Add([
  30955. 'uses unit1;',
  30956. 'type',
  30957. ' TColorAlias = TColor;',
  30958. ' TColorTypeAlias = type TColor;',
  30959. ' TObject = class',
  30960. ' private',
  30961. ' fColor: TColor;',
  30962. ' fAlias: TColorAlias;',
  30963. ' fTypeAlias: TColorTypeAlias;',
  30964. ' published',
  30965. ' property Color: TColor read fcolor;',
  30966. ' property Alias: TColorAlias read falias;',
  30967. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  30968. ' end;',
  30969. 'begin',
  30970. '']);
  30971. ConvertProgram;
  30972. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  30973. LinesToStr([ // statements
  30974. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  30975. 'rtl.createClass(this, "TObject", null, function () {',
  30976. ' this.$init = function () {',
  30977. ' this.fColor = 0;',
  30978. ' this.fAlias = 0;',
  30979. ' this.fTypeAlias = 0;',
  30980. ' };',
  30981. ' this.$final = function () {',
  30982. ' };',
  30983. ' var $r = this.$rtti;',
  30984. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  30985. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  30986. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  30987. '});',
  30988. '']),
  30989. LinesToStr([ // $mod.$main
  30990. '']));
  30991. end;
  30992. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  30993. begin
  30994. WithTypeInfo:=true;
  30995. StartProgram(false);
  30996. Add([
  30997. '{$modeswitch omitrtti}',
  30998. 'type',
  30999. ' TObject = class',
  31000. ' private',
  31001. ' FA: byte;',
  31002. ' published',
  31003. ' property A: byte read FA write FA;',
  31004. ' end;',
  31005. 'begin']);
  31006. ConvertProgram;
  31007. CheckSource('TestRTTI_Class_OmitRTTI',
  31008. LinesToStr([ // statements
  31009. 'rtl.createClass(this, "TObject", null, function () {',
  31010. ' this.$init = function () {',
  31011. ' this.FA = 0;',
  31012. ' };',
  31013. ' this.$final = function () {',
  31014. ' };',
  31015. '});',
  31016. '']),
  31017. LinesToStr([ // $mod.$main
  31018. '']));
  31019. end;
  31020. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  31021. begin
  31022. WithTypeInfo:=true;
  31023. StartUnit(true,[supTObject]);
  31024. Add([
  31025. 'interface',
  31026. 'type',
  31027. ' {$M+}',
  31028. ' TBird = class',
  31029. ' published',
  31030. ' Swarm: array of TBird;',
  31031. ' end;',
  31032. 'implementation',
  31033. '']);
  31034. ConvertUnit;
  31035. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  31036. LinesToStr([ // statements
  31037. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  31038. ' this.$init = function () {',
  31039. ' pas.system.TObject.$init.call(this);',
  31040. ' this.Swarm = [];',
  31041. ' };',
  31042. ' this.$final = function () {',
  31043. ' this.Swarm = undefined;',
  31044. ' pas.system.TObject.$final.call(this);',
  31045. ' };',
  31046. ' var $r = this.$rtti;',
  31047. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  31048. ' eltype: $r',
  31049. ' });',
  31050. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  31051. '});',
  31052. '']),
  31053. LinesToStr([ // $mod.$main
  31054. '']));
  31055. end;
  31056. procedure TTestModule.TestRTTI_IndexModifier;
  31057. begin
  31058. WithTypeInfo:=true;
  31059. StartProgram(false);
  31060. Add([
  31061. 'type',
  31062. ' TEnum = (red, blue);',
  31063. ' TObject = class',
  31064. ' FB: boolean;',
  31065. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  31066. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  31067. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  31068. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  31069. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  31070. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  31071. ' published',
  31072. ' property B1: boolean index 1 read FB write SetIntBool;',
  31073. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  31074. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  31075. ' end;',
  31076. 'begin']);
  31077. ConvertProgram;
  31078. CheckSource('TestRTTI_IndexModifier',
  31079. LinesToStr([ // statements
  31080. 'this.TEnum = {',
  31081. ' "0": "red",',
  31082. ' red: 0,',
  31083. ' "1": "blue",',
  31084. ' blue: 1',
  31085. '};',
  31086. 'this.$rtti.$Enum("TEnum", {',
  31087. ' minvalue: 0,',
  31088. ' maxvalue: 1,',
  31089. ' ordtype: 1,',
  31090. ' enumtype: this.TEnum',
  31091. '});',
  31092. 'rtl.createClass(this, "TObject", null, function () {',
  31093. ' this.$init = function () {',
  31094. ' this.FB = false;',
  31095. ' };',
  31096. ' this.$final = function () {',
  31097. ' };',
  31098. ' var $r = this.$rtti;',
  31099. ' $r.addProperty(',
  31100. ' "B1",',
  31101. ' 18,',
  31102. ' rtl.boolean,',
  31103. ' "FB",',
  31104. ' "SetIntBool",',
  31105. ' {',
  31106. ' index: 1',
  31107. ' }',
  31108. ' );',
  31109. ' $r.addProperty(',
  31110. ' "B2",',
  31111. ' 17,',
  31112. ' rtl.boolean,',
  31113. ' "GetEnumBool",',
  31114. ' "FB",',
  31115. ' {',
  31116. ' index: $mod.TEnum.blue',
  31117. ' }',
  31118. ' );',
  31119. ' $r.addProperty(',
  31120. ' "I1",',
  31121. ' 19,',
  31122. ' rtl.boolean,',
  31123. ' "GetStrIntBool",',
  31124. ' "SetStrIntBool",',
  31125. ' {',
  31126. ' index: 2',
  31127. ' }',
  31128. ' );',
  31129. '});',
  31130. '']),
  31131. LinesToStr([ // $mod.$main
  31132. '']));
  31133. end;
  31134. procedure TTestModule.TestRTTI_StoredModifier;
  31135. begin
  31136. WithTypeInfo:=true;
  31137. StartProgram(false);
  31138. Add([
  31139. 'const',
  31140. ' ConstB = true;',
  31141. 'type',
  31142. ' TObject = class',
  31143. ' private',
  31144. ' FB: boolean;',
  31145. ' function IsBStored: boolean; virtual; abstract;',
  31146. ' published',
  31147. ' property BoolA: boolean read FB stored true;',
  31148. ' property BoolB: boolean read FB stored false;',
  31149. ' property BoolC: boolean read FB stored FB;',
  31150. ' property BoolD: boolean read FB stored ConstB;',
  31151. ' property BoolE: boolean read FB stored IsBStored;',
  31152. ' end;',
  31153. 'begin']);
  31154. ConvertProgram;
  31155. CheckSource('TestRTTI_StoredModifier',
  31156. LinesToStr([ // statements
  31157. 'this.ConstB = true;',
  31158. 'rtl.createClass(this, "TObject", null, function () {',
  31159. ' this.$init = function () {',
  31160. ' this.FB = false;',
  31161. ' };',
  31162. ' this.$final = function () {',
  31163. ' };',
  31164. ' var $r = this.$rtti;',
  31165. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  31166. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  31167. ' $r.addProperty(',
  31168. ' "BoolC",',
  31169. ' 8,',
  31170. ' rtl.boolean,',
  31171. ' "FB",',
  31172. ' "",',
  31173. ' {',
  31174. ' stored: "FB"',
  31175. ' }',
  31176. ' );',
  31177. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  31178. ' $r.addProperty(',
  31179. ' "BoolE",',
  31180. ' 12,',
  31181. ' rtl.boolean,',
  31182. ' "FB",',
  31183. ' "",',
  31184. ' {',
  31185. ' stored: "IsBStored"',
  31186. ' }',
  31187. ' );',
  31188. '});',
  31189. '']),
  31190. LinesToStr([ // $mod.$main
  31191. '']));
  31192. end;
  31193. procedure TTestModule.TestRTTI_DefaultValue;
  31194. begin
  31195. WithTypeInfo:=true;
  31196. StartProgram(false);
  31197. Add([
  31198. 'type',
  31199. ' TEnum = (red, blue);',
  31200. 'const',
  31201. ' CB = true or false;',
  31202. ' CI = 1+2;',
  31203. 'type',
  31204. ' TObject = class',
  31205. ' FB: boolean;',
  31206. ' FI: longint;',
  31207. ' FE: TEnum;',
  31208. ' published',
  31209. ' property B1: boolean read FB default true;',
  31210. ' property B2: boolean read FB default CB;',
  31211. ' property B3: boolean read FB default test1.cb;',
  31212. ' property I1: longint read FI default 2;',
  31213. ' property I2: longint read FI default CI;',
  31214. ' property E1: TEnum read FE default red;',
  31215. ' property E2: TEnum read FE default TEnum.blue;',
  31216. ' end;',
  31217. 'begin']);
  31218. ConvertProgram;
  31219. CheckSource('TestRTTI_DefaultValue',
  31220. LinesToStr([ // statements
  31221. 'this.TEnum = {',
  31222. ' "0": "red",',
  31223. ' red: 0,',
  31224. ' "1": "blue",',
  31225. ' blue: 1',
  31226. '};',
  31227. 'this.$rtti.$Enum("TEnum", {',
  31228. ' minvalue: 0,',
  31229. ' maxvalue: 1,',
  31230. ' ordtype: 1,',
  31231. ' enumtype: this.TEnum',
  31232. '});',
  31233. 'this.CB = true || false;',
  31234. 'this.CI = 1 + 2;',
  31235. 'rtl.createClass(this, "TObject", null, function () {',
  31236. ' this.$init = function () {',
  31237. ' this.FB = false;',
  31238. ' this.FI = 0;',
  31239. ' this.FE = 0;',
  31240. ' };',
  31241. ' this.$final = function () {',
  31242. ' };',
  31243. ' var $r = this.$rtti;',
  31244. ' $r.addProperty(',
  31245. ' "B1",',
  31246. ' 0,',
  31247. ' rtl.boolean,',
  31248. ' "FB",',
  31249. ' "",',
  31250. ' {',
  31251. ' Default: true',
  31252. ' }',
  31253. ' );',
  31254. ' $r.addProperty(',
  31255. ' "B2",',
  31256. ' 0,',
  31257. ' rtl.boolean,',
  31258. ' "FB",',
  31259. ' "",',
  31260. ' {',
  31261. ' Default: true',
  31262. ' }',
  31263. ' );',
  31264. ' $r.addProperty(',
  31265. ' "B3",',
  31266. ' 0,',
  31267. ' rtl.boolean,',
  31268. ' "FB",',
  31269. ' "",',
  31270. ' {',
  31271. ' Default: true',
  31272. ' }',
  31273. ' );',
  31274. ' $r.addProperty(',
  31275. ' "I1",',
  31276. ' 0,',
  31277. ' rtl.longint,',
  31278. ' "FI",',
  31279. ' "",',
  31280. ' {',
  31281. ' Default: 2',
  31282. ' }',
  31283. ' );',
  31284. ' $r.addProperty(',
  31285. ' "I2",',
  31286. ' 0,',
  31287. ' rtl.longint,',
  31288. ' "FI",',
  31289. ' "",',
  31290. ' {',
  31291. ' Default: 3',
  31292. ' }',
  31293. ' );',
  31294. ' $r.addProperty(',
  31295. ' "E1",',
  31296. ' 0,',
  31297. ' $mod.$rtti["TEnum"],',
  31298. ' "FE",',
  31299. ' "",',
  31300. ' {',
  31301. ' Default: $mod.TEnum.red',
  31302. ' }',
  31303. ' );',
  31304. ' $r.addProperty(',
  31305. ' "E2",',
  31306. ' 0,',
  31307. ' $mod.$rtti["TEnum"],',
  31308. ' "FE",',
  31309. ' "",',
  31310. ' {',
  31311. ' Default: $mod.TEnum.blue',
  31312. ' }',
  31313. ' );',
  31314. '});',
  31315. '']),
  31316. LinesToStr([ // $mod.$main
  31317. '']));
  31318. end;
  31319. procedure TTestModule.TestRTTI_DefaultValueSet;
  31320. begin
  31321. WithTypeInfo:=true;
  31322. StartProgram(false);
  31323. Add([
  31324. 'type',
  31325. ' TEnum = (red, blue);',
  31326. ' TSet = set of TEnum;',
  31327. 'const',
  31328. ' CSet = [red,blue];',
  31329. 'type',
  31330. ' TObject = class',
  31331. ' FSet: TSet;',
  31332. ' published',
  31333. ' property Set1: TSet read FSet default [];',
  31334. ' property Set2: TSet read FSet default [red];',
  31335. ' property Set3: TSet read FSet default [red,blue];',
  31336. ' property Set4: TSet read FSet default CSet;',
  31337. ' end;',
  31338. 'begin']);
  31339. ConvertProgram;
  31340. CheckSource('TestRTTI_DefaultValueSet',
  31341. LinesToStr([ // statements
  31342. 'this.TEnum = {',
  31343. ' "0": "red",',
  31344. ' red: 0,',
  31345. ' "1": "blue",',
  31346. ' blue: 1',
  31347. '};',
  31348. 'this.$rtti.$Enum("TEnum", {',
  31349. ' minvalue: 0,',
  31350. ' maxvalue: 1,',
  31351. ' ordtype: 1,',
  31352. ' enumtype: this.TEnum',
  31353. '});',
  31354. 'this.$rtti.$Set("TSet", {',
  31355. ' comptype: this.$rtti["TEnum"]',
  31356. '});',
  31357. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31358. 'rtl.createClass(this, "TObject", null, function () {',
  31359. ' this.$init = function () {',
  31360. ' this.FSet = {};',
  31361. ' };',
  31362. ' this.$final = function () {',
  31363. ' this.FSet = undefined;',
  31364. ' };',
  31365. ' var $r = this.$rtti;',
  31366. ' $r.addProperty(',
  31367. ' "Set1",',
  31368. ' 0,',
  31369. ' $mod.$rtti["TSet"],',
  31370. ' "FSet",',
  31371. ' "",',
  31372. ' {',
  31373. ' Default: {}',
  31374. ' }',
  31375. ' );',
  31376. ' $r.addProperty(',
  31377. ' "Set2",',
  31378. ' 0,',
  31379. ' $mod.$rtti["TSet"],',
  31380. ' "FSet",',
  31381. ' "",',
  31382. ' {',
  31383. ' Default: rtl.createSet($mod.TEnum.red)',
  31384. ' }',
  31385. ' );',
  31386. ' $r.addProperty(',
  31387. ' "Set3",',
  31388. ' 0,',
  31389. ' $mod.$rtti["TSet"],',
  31390. ' "FSet",',
  31391. ' "",',
  31392. ' {',
  31393. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31394. ' }',
  31395. ' );',
  31396. ' $r.addProperty(',
  31397. ' "Set4",',
  31398. ' 0,',
  31399. ' $mod.$rtti["TSet"],',
  31400. ' "FSet",',
  31401. ' "",',
  31402. ' {',
  31403. ' Default: $mod.CSet',
  31404. ' }',
  31405. ' );',
  31406. '});',
  31407. '']),
  31408. LinesToStr([ // $mod.$main
  31409. '']));
  31410. end;
  31411. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31412. begin
  31413. WithTypeInfo:=true;
  31414. StartProgram(false);
  31415. Add([
  31416. 'type',
  31417. ' TRg = -1..1;',
  31418. 'const',
  31419. ' l = low(TRg);',
  31420. ' h = high(TRg);',
  31421. 'type',
  31422. ' TObject = class',
  31423. ' FV: TRg;',
  31424. ' published',
  31425. ' property V1: TRg read FV default -1;',
  31426. ' end;',
  31427. 'begin']);
  31428. ConvertProgram;
  31429. CheckSource('TestRTTI_DefaultValueRangeType',
  31430. LinesToStr([ // statements
  31431. 'this.$rtti.$Int("TRg", {',
  31432. ' minvalue: -1,',
  31433. ' maxvalue: 1,',
  31434. ' ordtype: 0',
  31435. '});',
  31436. 'this.l = -1;',
  31437. 'this.h = 1;',
  31438. 'rtl.createClass(this, "TObject", null, function () {',
  31439. ' this.$init = function () {',
  31440. ' this.FV = 0;',
  31441. ' };',
  31442. ' this.$final = function () {',
  31443. ' };',
  31444. ' var $r = this.$rtti;',
  31445. ' $r.addProperty(',
  31446. ' "V1",',
  31447. ' 0,',
  31448. ' $mod.$rtti["TRg"],',
  31449. ' "FV",',
  31450. ' "",',
  31451. ' {',
  31452. ' Default: -1',
  31453. ' }',
  31454. ' );',
  31455. '});',
  31456. '']),
  31457. LinesToStr([ // $mod.$main
  31458. '']));
  31459. end;
  31460. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31461. begin
  31462. WithTypeInfo:=true;
  31463. StartProgram(false);
  31464. Add([
  31465. 'type',
  31466. ' TObject = class',
  31467. ' FA, FB: byte;',
  31468. ' property A: byte read FA default 1;',
  31469. ' property B: byte read FB default 2;',
  31470. ' end;',
  31471. ' TBird = class',
  31472. ' published',
  31473. ' property A;',
  31474. ' property B nodefault;',
  31475. ' end;',
  31476. 'begin']);
  31477. ConvertProgram;
  31478. CheckSource('TestRTTI_DefaultValueInherit',
  31479. LinesToStr([ // statements
  31480. 'rtl.createClass(this, "TObject", null, function () {',
  31481. ' this.$init = function () {',
  31482. ' this.FA = 0;',
  31483. ' this.FB = 0;',
  31484. ' };',
  31485. ' this.$final = function () {',
  31486. ' };',
  31487. '});',
  31488. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31489. ' var $r = this.$rtti;',
  31490. ' $r.addProperty(',
  31491. ' "A",',
  31492. ' 0,',
  31493. ' rtl.byte,',
  31494. ' "FA",',
  31495. ' "",',
  31496. ' {',
  31497. ' Default: 1',
  31498. ' }',
  31499. ' );',
  31500. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31501. '});',
  31502. '']),
  31503. LinesToStr([ // $mod.$main
  31504. '']));
  31505. end;
  31506. procedure TTestModule.TestRTTI_OverrideMethod;
  31507. begin
  31508. WithTypeInfo:=true;
  31509. StartProgram(false);
  31510. Add('type');
  31511. Add(' TObject = class');
  31512. Add(' published');
  31513. Add(' procedure DoIt; virtual; abstract;');
  31514. Add(' end;');
  31515. Add(' TSky = class');
  31516. Add(' published');
  31517. Add(' procedure DoIt; override;');
  31518. Add(' end;');
  31519. Add('procedure TSky.DoIt; begin end;');
  31520. Add('begin');
  31521. ConvertProgram;
  31522. CheckSource('TestRTTI_OverrideMethod',
  31523. LinesToStr([ // statements
  31524. 'rtl.createClass(this, "TObject", null, function () {',
  31525. ' this.$init = function () {',
  31526. ' };',
  31527. ' this.$final = function () {',
  31528. ' };',
  31529. ' var $r = this.$rtti;',
  31530. ' $r.addMethod("DoIt", 0, []);',
  31531. '});',
  31532. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31533. ' this.DoIt = function () {',
  31534. ' };',
  31535. '});',
  31536. '']),
  31537. LinesToStr([ // $mod.$main
  31538. '']));
  31539. end;
  31540. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31541. begin
  31542. WithTypeInfo:=true;
  31543. StartProgram(false);
  31544. Add([
  31545. 'type',
  31546. ' TObject = class',
  31547. ' published',
  31548. ' procedure DoIt;',
  31549. ' end;',
  31550. ' TSky = class',
  31551. ' published',
  31552. ' procedure DoIt; reintroduce;',
  31553. ' end;',
  31554. 'procedure TObject.DoIt; begin end;',
  31555. 'procedure TSky.DoIt;',
  31556. 'begin',
  31557. ' inherited DoIt;',
  31558. 'end;',
  31559. 'begin']);
  31560. ConvertProgram;
  31561. CheckSource('TestRTTI_ReintroduceMethod',
  31562. LinesToStr([ // statements
  31563. 'rtl.createClass(this, "TObject", null, function () {',
  31564. ' this.$init = function () {',
  31565. ' };',
  31566. ' this.$final = function () {',
  31567. ' };',
  31568. ' this.DoIt = function () {',
  31569. ' };',
  31570. ' var $r = this.$rtti;',
  31571. ' $r.addMethod("DoIt", 0, []);',
  31572. '});',
  31573. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31574. ' this.DoIt = function () {',
  31575. ' $mod.TObject.DoIt.call(this);',
  31576. ' };',
  31577. ' var $r = this.$rtti;',
  31578. ' $r.addMethod("DoIt", 0, []);',
  31579. '});',
  31580. '']),
  31581. LinesToStr([ // $mod.$main
  31582. '']));
  31583. end;
  31584. procedure TTestModule.TestRTTI_OverloadProperty;
  31585. begin
  31586. WithTypeInfo:=true;
  31587. StartProgram(false);
  31588. Add('type');
  31589. Add(' TObject = class');
  31590. Add(' protected');
  31591. Add(' FFlag: longint;');
  31592. Add(' published');
  31593. Add(' property Flag: longint read fflag;');
  31594. Add(' end;');
  31595. Add(' TSky = class');
  31596. Add(' published');
  31597. Add(' property FLAG: longint write fflag;');
  31598. Add(' end;');
  31599. Add('begin');
  31600. ConvertProgram;
  31601. CheckSource('TestRTTI_OverrideMethod',
  31602. LinesToStr([ // statements
  31603. 'rtl.createClass(this, "TObject", null, function () {',
  31604. ' this.$init = function () {',
  31605. ' this.FFlag = 0;',
  31606. ' };',
  31607. ' this.$final = function () {',
  31608. ' };',
  31609. ' var $r = this.$rtti;',
  31610. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  31611. '});',
  31612. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31613. ' var $r = this.$rtti;',
  31614. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  31615. '});',
  31616. '']),
  31617. LinesToStr([ // $mod.$main
  31618. '']));
  31619. end;
  31620. procedure TTestModule.TestRTTI_ClassForward;
  31621. begin
  31622. WithTypeInfo:=true;
  31623. StartProgram(false);
  31624. Add('type');
  31625. Add(' TObject = class end;');
  31626. Add(' tbridge = class;');
  31627. Add(' TProc = function: tbridge;');
  31628. Add(' TOger = class');
  31629. Add(' published');
  31630. Add(' FBridge: tbridge;');
  31631. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  31632. Add(' property Bridge: tbridge read fbridge write setbridge;');
  31633. Add(' end;');
  31634. Add(' TBridge = class');
  31635. Add(' FOger: toger;');
  31636. Add(' end;');
  31637. Add('var p: Pointer;');
  31638. Add(' b: tbridge;');
  31639. Add('begin');
  31640. Add(' p:=typeinfo(tbridge);');
  31641. Add(' p:=typeinfo(b);');
  31642. ConvertProgram;
  31643. CheckSource('TestRTTI_ClassForward',
  31644. LinesToStr([ // statements
  31645. 'rtl.createClass(this, "TObject", null, function () {',
  31646. ' this.$init = function () {',
  31647. ' };',
  31648. ' this.$final = function () {',
  31649. ' };',
  31650. '});',
  31651. 'this.$rtti.$Class("TBridge");',
  31652. 'this.$rtti.$ProcVar("TProc", {',
  31653. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  31654. '});',
  31655. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  31656. ' this.$init = function () {',
  31657. ' $mod.TObject.$init.call(this);',
  31658. ' this.FBridge = null;',
  31659. ' };',
  31660. ' this.$final = function () {',
  31661. ' this.FBridge = undefined;',
  31662. ' $mod.TObject.$final.call(this);',
  31663. ' };',
  31664. ' var $r = this.$rtti;',
  31665. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  31666. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  31667. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  31668. '});',
  31669. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  31670. ' this.$init = function () {',
  31671. ' $mod.TObject.$init.call(this);',
  31672. ' this.FOger = null;',
  31673. ' };',
  31674. ' this.$final = function () {',
  31675. ' this.FOger = undefined;',
  31676. ' $mod.TObject.$final.call(this);',
  31677. ' };',
  31678. '});',
  31679. 'this.p = null;',
  31680. 'this.b = null;',
  31681. '']),
  31682. LinesToStr([ // $mod.$main
  31683. '$mod.p = $mod.$rtti["TBridge"];',
  31684. '$mod.p = $mod.b.$rtti;',
  31685. '']));
  31686. end;
  31687. procedure TTestModule.TestRTTI_ClassOf;
  31688. begin
  31689. WithTypeInfo:=true;
  31690. StartProgram(false);
  31691. Add('type');
  31692. Add(' TClass = class of tobject;');
  31693. Add(' TProcA = function: TClass;');
  31694. Add(' TObject = class');
  31695. Add(' published');
  31696. Add(' C: tclass;');
  31697. Add(' end;');
  31698. Add(' tfox = class;');
  31699. Add(' TBird = class end;');
  31700. Add(' TBirds = class of tbird;');
  31701. Add(' TFox = class end;');
  31702. Add(' TFoxes = class of tfox;');
  31703. Add(' TCows = class of TCow;');
  31704. Add(' TCow = class;');
  31705. Add(' TCow = class end;');
  31706. Add('begin');
  31707. ConvertProgram;
  31708. CheckSource('TestRTTI_ClassOf',
  31709. LinesToStr([ // statements
  31710. 'this.$rtti.$Class("TObject");',
  31711. 'this.$rtti.$ClassRef("TClass", {',
  31712. ' instancetype: this.$rtti["TObject"]',
  31713. '});',
  31714. 'this.$rtti.$ProcVar("TProcA", {',
  31715. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  31716. '});',
  31717. 'rtl.createClass(this, "TObject", null, function () {',
  31718. ' this.$init = function () {',
  31719. ' this.C = null;',
  31720. ' };',
  31721. ' this.$final = function () {',
  31722. ' this.C = undefined;',
  31723. ' };',
  31724. ' var $r = this.$rtti;',
  31725. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31726. '});',
  31727. 'this.$rtti.$Class("TFox");',
  31728. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31729. '});',
  31730. 'this.$rtti.$ClassRef("TBirds", {',
  31731. ' instancetype: this.$rtti["TBird"]',
  31732. '});',
  31733. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31734. '});',
  31735. 'this.$rtti.$ClassRef("TFoxes", {',
  31736. ' instancetype: this.$rtti["TFox"]',
  31737. '});',
  31738. 'this.$rtti.$Class("TCow");',
  31739. 'this.$rtti.$ClassRef("TCows", {',
  31740. ' instancetype: this.$rtti["TCow"]',
  31741. '});',
  31742. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31743. '});',
  31744. '']),
  31745. LinesToStr([ // $mod.$main
  31746. '']));
  31747. end;
  31748. procedure TTestModule.TestRTTI_Record;
  31749. begin
  31750. WithTypeInfo:=true;
  31751. StartProgram(false);
  31752. Add('type');
  31753. Add(' integer = longint;');
  31754. Add(' TPoint = record');
  31755. Add(' x,y: integer;');
  31756. Add(' end;');
  31757. Add('var p: pointer;');
  31758. Add(' r: tpoint;');
  31759. Add('begin');
  31760. Add(' p:=typeinfo(tpoint);');
  31761. Add(' p:=typeinfo(r);');
  31762. Add(' p:=typeinfo(r.x);');
  31763. ConvertProgram;
  31764. CheckSource('TestRTTI_Record',
  31765. LinesToStr([ // statements
  31766. 'rtl.recNewT(this, "TPoint", function () {',
  31767. ' this.x = 0;',
  31768. ' this.y = 0;',
  31769. ' this.$eq = function (b) {',
  31770. ' return (this.x === b.x) && (this.y === b.y);',
  31771. ' };',
  31772. ' this.$assign = function (s) {',
  31773. ' this.x = s.x;',
  31774. ' this.y = s.y;',
  31775. ' return this;',
  31776. ' };',
  31777. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31778. ' $r.addField("x", rtl.longint);',
  31779. ' $r.addField("y", rtl.longint);',
  31780. '});',
  31781. 'this.p = null;',
  31782. 'this.r = this.TPoint.$new();',
  31783. '']),
  31784. LinesToStr([ // $mod.$main
  31785. '$mod.p = $mod.$rtti["TPoint"];',
  31786. '$mod.p = $mod.$rtti["TPoint"];',
  31787. '$mod.p = rtl.longint;',
  31788. '']));
  31789. end;
  31790. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31791. begin
  31792. WithTypeInfo:=true;
  31793. StartProgram(false);
  31794. Add('type');
  31795. Add(' TFloatRec = record');
  31796. Add(' c,d: array of char;');
  31797. // Add(' i: array of array of longint;');
  31798. Add(' end;');
  31799. Add('var p: pointer;');
  31800. Add(' r: tfloatrec;');
  31801. Add('begin');
  31802. Add(' p:=typeinfo(tfloatrec);');
  31803. Add(' p:=typeinfo(r);');
  31804. Add(' p:=typeinfo(r.d);');
  31805. ConvertProgram;
  31806. CheckSource('TestRTTI_Record',
  31807. LinesToStr([ // statements
  31808. 'rtl.recNewT(this, "TFloatRec", function () {',
  31809. ' this.$new = function () {',
  31810. ' var r = Object.create(this);',
  31811. ' r.c = [];',
  31812. ' r.d = [];',
  31813. ' return r;',
  31814. ' };',
  31815. ' this.$eq = function (b) {',
  31816. ' return (this.c === b.c) && (this.d === b.d);',
  31817. ' };',
  31818. ' this.$assign = function (s) {',
  31819. ' this.c = rtl.arrayRef(s.c);',
  31820. ' this.d = rtl.arrayRef(s.d);',
  31821. ' return this;',
  31822. ' };',
  31823. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31824. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31825. ' eltype: rtl.char',
  31826. ' });',
  31827. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31828. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31829. '});',
  31830. 'this.p = null;',
  31831. 'this.r = this.TFloatRec.$new();',
  31832. '']),
  31833. LinesToStr([ // $mod.$main
  31834. '$mod.p = $mod.$rtti["TFloatRec"];',
  31835. '$mod.p = $mod.$rtti["TFloatRec"];',
  31836. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31837. '']));
  31838. end;
  31839. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31840. begin
  31841. WithTypeInfo:=true;
  31842. StartProgram(false);
  31843. Add([
  31844. '{$modeswitch AdvancedRecords}',
  31845. 'type',
  31846. ' TPoint = record',
  31847. ' type TProc = procedure(w: word);',
  31848. ' class var p: TProc;',
  31849. ' end;',
  31850. 'begin',
  31851. '']);
  31852. ConvertProgram;
  31853. CheckSource('TestRTTI_Record_ClassVarType',
  31854. LinesToStr([ // statements
  31855. 'rtl.recNewT(this, "TPoint", function () {',
  31856. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  31857. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  31858. ' });',
  31859. ' this.p = null;',
  31860. ' this.$eq = function (b) {',
  31861. ' return true;',
  31862. ' };',
  31863. ' this.$assign = function (s) {',
  31864. ' return this;',
  31865. ' };',
  31866. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31867. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  31868. '}, true);',
  31869. '']),
  31870. LinesToStr([ // $mod.$main
  31871. '']));
  31872. end;
  31873. procedure TTestModule.TestRTTI_LocalTypes;
  31874. begin
  31875. WithTypeInfo:=true;
  31876. StartProgram(false);
  31877. Add([
  31878. 'procedure DoIt;',
  31879. 'type',
  31880. ' integer = longint;',
  31881. ' TPoint = record',
  31882. ' x,y: integer;',
  31883. ' end;',
  31884. 'var p: TPoint;',
  31885. 'begin',
  31886. 'end;',
  31887. 'begin']);
  31888. ConvertProgram;
  31889. CheckSource('TestRTTI_LocalTypes',
  31890. LinesToStr([ // statements
  31891. 'var TPoint = rtl.recNewT(null, "", function () {',
  31892. ' this.x = 0;',
  31893. ' this.y = 0;',
  31894. ' this.$eq = function (b) {',
  31895. ' return (this.x === b.x) && (this.y === b.y);',
  31896. ' };',
  31897. ' this.$assign = function (s) {',
  31898. ' this.x = s.x;',
  31899. ' this.y = s.y;',
  31900. ' return this;',
  31901. ' };',
  31902. '});',
  31903. 'this.DoIt = function () {',
  31904. ' var p = TPoint.$new();',
  31905. '};',
  31906. '']),
  31907. LinesToStr([ // $mod.$main
  31908. '']));
  31909. end;
  31910. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  31911. begin
  31912. WithTypeInfo:=true;
  31913. StartProgram(false);
  31914. Add([
  31915. 'type',
  31916. ' TCaption = string;',
  31917. ' TYesNo = boolean;',
  31918. ' TLetter = char;',
  31919. ' TFloat = double;',
  31920. ' TPtr = pointer;',
  31921. ' TShortInt = shortint;',
  31922. ' TByte = byte;',
  31923. ' TSmallInt = smallint;',
  31924. ' TWord = word;',
  31925. ' TInt32 = longint;',
  31926. ' TDWord = longword;',
  31927. ' TValue = jsvalue;',
  31928. 'var p: TPtr;',
  31929. 'begin',
  31930. ' p:=typeinfo(string);',
  31931. ' p:=typeinfo(tcaption);',
  31932. ' p:=typeinfo(boolean);',
  31933. ' p:=typeinfo(tyesno);',
  31934. ' p:=typeinfo(char);',
  31935. ' p:=typeinfo(tletter);',
  31936. ' p:=typeinfo(double);',
  31937. ' p:=typeinfo(tfloat);',
  31938. ' p:=typeinfo(pointer);',
  31939. ' p:=typeinfo(tptr);',
  31940. ' p:=typeinfo(shortint);',
  31941. ' p:=typeinfo(tshortint);',
  31942. ' p:=typeinfo(byte);',
  31943. ' p:=typeinfo(tbyte);',
  31944. ' p:=typeinfo(smallint);',
  31945. ' p:=typeinfo(tsmallint);',
  31946. ' p:=typeinfo(word);',
  31947. ' p:=typeinfo(tword);',
  31948. ' p:=typeinfo(longword);',
  31949. ' p:=typeinfo(tdword);',
  31950. ' p:=typeinfo(jsvalue);',
  31951. ' p:=typeinfo(tvalue);',
  31952. '']);
  31953. ConvertProgram;
  31954. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  31955. LinesToStr([ // statements
  31956. 'this.p = null;',
  31957. '']),
  31958. LinesToStr([ // $mod.$main
  31959. '$mod.p = rtl.string;',
  31960. '$mod.p = rtl.string;',
  31961. '$mod.p = rtl.boolean;',
  31962. '$mod.p = rtl.boolean;',
  31963. '$mod.p = rtl.char;',
  31964. '$mod.p = rtl.char;',
  31965. '$mod.p = rtl.double;',
  31966. '$mod.p = rtl.double;',
  31967. '$mod.p = rtl.pointer;',
  31968. '$mod.p = rtl.pointer;',
  31969. '$mod.p = rtl.shortint;',
  31970. '$mod.p = rtl.shortint;',
  31971. '$mod.p = rtl.byte;',
  31972. '$mod.p = rtl.byte;',
  31973. '$mod.p = rtl.smallint;',
  31974. '$mod.p = rtl.smallint;',
  31975. '$mod.p = rtl.word;',
  31976. '$mod.p = rtl.word;',
  31977. '$mod.p = rtl.longword;',
  31978. '$mod.p = rtl.longword;',
  31979. '$mod.p = rtl.jsvalue;',
  31980. '$mod.p = rtl.jsvalue;',
  31981. '']));
  31982. end;
  31983. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  31984. begin
  31985. WithTypeInfo:=true;
  31986. StartProgram(false);
  31987. Add([
  31988. 'type',
  31989. ' TCaption = type string;',
  31990. ' TYesNo = type boolean;',
  31991. ' TLetter = type char;',
  31992. ' TFloat = type double;',
  31993. ' TPtr = type pointer;',
  31994. ' TShortInt = type shortint;',
  31995. ' TByte = type byte;',
  31996. ' TSmallInt = type smallint;',
  31997. ' TWord = type word;',
  31998. ' TInt32 = type longint;',
  31999. ' TDWord = type longword;',
  32000. ' TValue = type jsvalue;',
  32001. ' TAliasValue = type TValue;',
  32002. 'var',
  32003. ' p: TPtr;',
  32004. ' a: TAliasValue;',
  32005. 'begin',
  32006. ' p:=typeinfo(tcaption);',
  32007. ' p:=typeinfo(tyesno);',
  32008. ' p:=typeinfo(tletter);',
  32009. ' p:=typeinfo(tfloat);',
  32010. ' p:=typeinfo(tptr);',
  32011. ' p:=typeinfo(tshortint);',
  32012. ' p:=typeinfo(tbyte);',
  32013. ' p:=typeinfo(tsmallint);',
  32014. ' p:=typeinfo(tword);',
  32015. ' p:=typeinfo(tdword);',
  32016. ' p:=typeinfo(tvalue);',
  32017. ' p:=typeinfo(taliasvalue);',
  32018. ' p:=typeinfo(a);',
  32019. '']);
  32020. ConvertProgram;
  32021. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  32022. LinesToStr([ // statements
  32023. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  32024. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  32025. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  32026. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  32027. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  32028. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  32029. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  32030. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  32031. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  32032. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  32033. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  32034. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  32035. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  32036. 'this.p = null;',
  32037. 'this.a = undefined;',
  32038. '']),
  32039. LinesToStr([ // $mod.$main
  32040. '$mod.p = $mod.$rtti["TCaption"];',
  32041. '$mod.p = $mod.$rtti["TYesNo"];',
  32042. '$mod.p = $mod.$rtti["TLetter"];',
  32043. '$mod.p = $mod.$rtti["TFloat"];',
  32044. '$mod.p = $mod.$rtti["TPtr"];',
  32045. '$mod.p = $mod.$rtti["TShortInt"];',
  32046. '$mod.p = $mod.$rtti["TByte"];',
  32047. '$mod.p = $mod.$rtti["TSmallInt"];',
  32048. '$mod.p = $mod.$rtti["TWord"];',
  32049. '$mod.p = $mod.$rtti["TDWord"];',
  32050. '$mod.p = $mod.$rtti["TValue"];',
  32051. '$mod.p = $mod.$rtti["TAliasValue"];',
  32052. '$mod.p = $mod.$rtti["TAliasValue"];',
  32053. '']));
  32054. end;
  32055. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  32056. begin
  32057. WithTypeInfo:=true;
  32058. StartProgram(false);
  32059. Add('procedure DoIt;');
  32060. Add('type');
  32061. Add(' integer = longint;');
  32062. Add(' TPoint = record');
  32063. Add(' x,y: integer;');
  32064. Add(' end;');
  32065. Add('var p: pointer;');
  32066. Add('begin');
  32067. Add(' p:=typeinfo(tpoint);');
  32068. Add('end;');
  32069. Add('begin');
  32070. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  32071. ConvertProgram;
  32072. end;
  32073. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  32074. begin
  32075. WithTypeInfo:=true;
  32076. StartProgram(true,[supTypeInfo]);
  32077. Add([
  32078. '{$modeswitch externalclass}',
  32079. 'type',
  32080. ' TFlag = (up,down);',
  32081. ' TFlags = set of TFlag;',
  32082. 'var',
  32083. ' ti: TTypeInfo;',
  32084. ' tiInt: TTypeInfoInteger;',
  32085. ' tiEnum: TTypeInfoEnum;',
  32086. ' tiSet: TTypeInfoSet;',
  32087. 'begin',
  32088. ' ti:=typeinfo(string);',
  32089. ' ti:=typeinfo(boolean);',
  32090. ' ti:=typeinfo(char);',
  32091. ' ti:=typeinfo(double);',
  32092. ' tiInt:=typeinfo(shortint);',
  32093. ' tiInt:=typeinfo(byte);',
  32094. ' tiInt:=typeinfo(smallint);',
  32095. ' tiInt:=typeinfo(word);',
  32096. ' tiInt:=typeinfo(longint);',
  32097. ' tiInt:=typeinfo(longword);',
  32098. ' ti:=typeinfo(jsvalue);',
  32099. ' tiEnum:=typeinfo(tflag);',
  32100. ' tiSet:=typeinfo(tflags);']);
  32101. ConvertProgram;
  32102. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  32103. LinesToStr([ // statements
  32104. 'this.TFlag = {',
  32105. ' "0": "up",',
  32106. ' up: 0,',
  32107. ' "1": "down",',
  32108. ' down: 1',
  32109. '};',
  32110. 'this.$rtti.$Enum("TFlag", {',
  32111. ' minvalue: 0,',
  32112. ' maxvalue: 1,',
  32113. ' ordtype: 1,',
  32114. ' enumtype: this.TFlag',
  32115. '});',
  32116. 'this.$rtti.$Set("TFlags", {',
  32117. ' comptype: this.$rtti["TFlag"]',
  32118. '});',
  32119. 'this.ti = null;',
  32120. 'this.tiInt = null;',
  32121. 'this.tiEnum = null;',
  32122. 'this.tiSet = null;',
  32123. '']),
  32124. LinesToStr([ // $mod.$main
  32125. '$mod.ti = rtl.string;',
  32126. '$mod.ti = rtl.boolean;',
  32127. '$mod.ti = rtl.char;',
  32128. '$mod.ti = rtl.double;',
  32129. '$mod.tiInt = rtl.shortint;',
  32130. '$mod.tiInt = rtl.byte;',
  32131. '$mod.tiInt = rtl.smallint;',
  32132. '$mod.tiInt = rtl.word;',
  32133. '$mod.tiInt = rtl.longint;',
  32134. '$mod.tiInt = rtl.longword;',
  32135. '$mod.ti = rtl.jsvalue;',
  32136. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  32137. '$mod.tiSet = $mod.$rtti["TFlags"];',
  32138. '']));
  32139. end;
  32140. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  32141. begin
  32142. WithTypeInfo:=true;
  32143. StartProgram(true,[supTypeInfo]);
  32144. Add('{$modeswitch externalclass}');
  32145. Add('type');
  32146. Add(' TStaticArr = array[boolean] of string;');
  32147. Add(' TDynArr = array of string;');
  32148. Add(' TProc = procedure;');
  32149. Add(' TMethod = procedure of object;');
  32150. Add('var');
  32151. Add(' StaticArray: TStaticArr;');
  32152. Add(' tiStaticArray: TTypeInfoStaticArray;');
  32153. Add(' DynArray: TDynArr;');
  32154. Add(' tiDynArray: TTypeInfoDynArray;');
  32155. Add(' ProcVar: TProc;');
  32156. Add(' tiProcVar: TTypeInfoProcVar;');
  32157. Add(' MethodVar: TMethod;');
  32158. Add(' tiMethodVar: TTypeInfoMethodVar;');
  32159. Add('begin');
  32160. Add(' tiStaticArray:=typeinfo(StaticArray);');
  32161. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  32162. Add(' tiDynArray:=typeinfo(DynArray);');
  32163. Add(' tiDynArray:=typeinfo(TDynArr);');
  32164. Add(' tiProcVar:=typeinfo(ProcVar);');
  32165. Add(' tiProcVar:=typeinfo(TProc);');
  32166. Add(' tiMethodVar:=typeinfo(MethodVar);');
  32167. Add(' tiMethodVar:=typeinfo(TMethod);');
  32168. ConvertProgram;
  32169. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  32170. LinesToStr([ // statements
  32171. 'this.$rtti.$StaticArray("TStaticArr", {',
  32172. ' dims: [2],',
  32173. ' eltype: rtl.string',
  32174. '});',
  32175. 'this.$rtti.$DynArray("TDynArr", {',
  32176. ' eltype: rtl.string',
  32177. '});',
  32178. 'this.$rtti.$ProcVar("TProc", {',
  32179. ' procsig: rtl.newTIProcSig([])',
  32180. '});',
  32181. 'this.$rtti.$MethodVar("TMethod", {',
  32182. ' procsig: rtl.newTIProcSig([]),',
  32183. ' methodkind: 0',
  32184. '});',
  32185. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  32186. 'this.tiStaticArray = null;',
  32187. 'this.DynArray = [];',
  32188. 'this.tiDynArray = null;',
  32189. 'this.ProcVar = null;',
  32190. 'this.tiProcVar = null;',
  32191. 'this.MethodVar = null;',
  32192. 'this.tiMethodVar = null;',
  32193. '']),
  32194. LinesToStr([ // $mod.$main
  32195. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32196. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32197. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32198. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32199. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32200. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32201. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32202. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32203. '']));
  32204. end;
  32205. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  32206. begin
  32207. WithTypeInfo:=true;
  32208. StartProgram(true,[supTypeInfo]);
  32209. Add('{$modeswitch externalclass}');
  32210. Add('type');
  32211. Add(' TRec = record end;');
  32212. // ToDo: ^TRec
  32213. Add(' TObject = class end;');
  32214. Add(' TClass = class of tobject;');
  32215. Add('var');
  32216. Add(' Rec: trec;');
  32217. Add(' tiRecord: ttypeinforecord;');
  32218. Add(' Obj: tobject;');
  32219. Add(' tiClass: ttypeinfoclass;');
  32220. Add(' aClass: tclass;');
  32221. Add(' tiClassRef: ttypeinfoclassref;');
  32222. // ToDo: ^TRec
  32223. Add(' tiPointer: ttypeinfopointer;');
  32224. Add('begin');
  32225. Add(' tirecord:=typeinfo(trec);');
  32226. Add(' tirecord:=typeinfo(trec);');
  32227. Add(' ticlass:=typeinfo(obj);');
  32228. Add(' ticlass:=typeinfo(tobject);');
  32229. Add(' ticlass:=typeinfo(aclass);');
  32230. Add(' ticlassref:=typeinfo(tclass);');
  32231. ConvertProgram;
  32232. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  32233. LinesToStr([ // statements
  32234. 'rtl.recNewT(this, "TRec", function () {',
  32235. ' this.$eq = function (b) {',
  32236. ' return true;',
  32237. ' };',
  32238. ' this.$assign = function (s) {',
  32239. ' return this;',
  32240. ' };',
  32241. ' $mod.$rtti.$Record("TRec", {});',
  32242. '});',
  32243. 'rtl.createClass(this, "TObject", null, function () {',
  32244. ' this.$init = function () {',
  32245. ' };',
  32246. ' this.$final = function () {',
  32247. ' };',
  32248. '});',
  32249. 'this.$rtti.$ClassRef("TClass", {',
  32250. ' instancetype: this.$rtti["TObject"]',
  32251. '});',
  32252. 'this.Rec = this.TRec.$new();',
  32253. 'this.tiRecord = null;',
  32254. 'this.Obj = null;',
  32255. 'this.tiClass = null;',
  32256. 'this.aClass = null;',
  32257. 'this.tiClassRef = null;',
  32258. 'this.tiPointer = null;',
  32259. '']),
  32260. LinesToStr([ // $mod.$main
  32261. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32262. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32263. '$mod.tiClass = $mod.Obj.$rtti;',
  32264. '$mod.tiClass = $mod.$rtti["TObject"];',
  32265. '$mod.tiClass = $mod.aClass.$rtti;',
  32266. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  32267. '']));
  32268. end;
  32269. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  32270. begin
  32271. WithTypeInfo:=true;
  32272. StartProgram(true,[supTypeInfo]);
  32273. Add([
  32274. '{$modeswitch externalclass}',
  32275. 'type',
  32276. ' TClass = class of tobject;',
  32277. ' TObject = class',
  32278. ' function MyClass: TClass;',
  32279. ' class function ClassType: TClass;',
  32280. ' end;',
  32281. 'function TObject.MyClass: TClass;',
  32282. 'var t: TTypeInfoClass;',
  32283. 'begin',
  32284. ' t:=TypeInfo(Self);',
  32285. ' t:=TypeInfo(Result);',
  32286. ' t:=TypeInfo(TObject);',
  32287. 'end;',
  32288. 'class function TObject.ClassType: TClass;',
  32289. 'var t: TTypeInfoClass;',
  32290. 'begin',
  32291. ' t:=TypeInfo(Self);',
  32292. ' t:=TypeInfo(Result);',
  32293. 'end;',
  32294. 'var',
  32295. ' Obj: TObject;',
  32296. ' t: TTypeInfoClass;',
  32297. 'begin',
  32298. ' t:=TypeInfo(TObject.ClassType);',
  32299. ' t:=TypeInfo(Obj.ClassType);',
  32300. ' t:=TypeInfo(Obj.MyClass);',
  32301. '']);
  32302. ConvertProgram;
  32303. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32304. LinesToStr([ // statements
  32305. 'this.$rtti.$Class("TObject");',
  32306. 'this.$rtti.$ClassRef("TClass", {',
  32307. ' instancetype: this.$rtti["TObject"]',
  32308. '});',
  32309. 'rtl.createClass(this, "TObject", null, function () {',
  32310. ' this.$init = function () {',
  32311. ' };',
  32312. ' this.$final = function () {',
  32313. ' };',
  32314. ' this.MyClass = function () {',
  32315. ' var Result = null;',
  32316. ' var t = null;',
  32317. ' t = this.$rtti;',
  32318. ' t = Result.$rtti;',
  32319. ' t = $mod.$rtti["TObject"];',
  32320. ' return Result;',
  32321. ' };',
  32322. ' this.ClassType = function () {',
  32323. ' var Result = null;',
  32324. ' var t = null;',
  32325. ' t = this.$rtti;',
  32326. ' t = Result.$rtti;',
  32327. ' return Result;',
  32328. ' };',
  32329. '});',
  32330. 'this.Obj = null;',
  32331. 'this.t = null;',
  32332. '']),
  32333. LinesToStr([ // $mod.$main
  32334. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32335. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32336. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32337. '']));
  32338. end;
  32339. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32340. begin
  32341. WithTypeInfo:=true;
  32342. AddModuleWithIntfImplSrc('typinfo.pas',
  32343. LinesToStr([
  32344. '{$modeswitch externalclass}',
  32345. 'type',
  32346. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32347. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32348. '']),
  32349. '');
  32350. AddModuleWithIntfImplSrc('unit2.pas',
  32351. LinesToStr([
  32352. 'uses typinfo;',
  32353. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32354. 'procedure DoPtr(p: PTypeInfo);',
  32355. 'procedure DoInfo(t: TTypeInfo);',
  32356. 'procedure DoInt(t: TTypeInfoInteger);',
  32357. '']),
  32358. LinesToStr([
  32359. 'procedure DoPtr(p: PTypeInfo);',
  32360. 'begin end;',
  32361. 'procedure DoInfo(t: TTypeInfo);',
  32362. 'begin end;',
  32363. 'procedure DoInt(t: TTypeInfoInteger);',
  32364. 'begin end;',
  32365. '']));
  32366. StartUnit(true);
  32367. Add([
  32368. 'interface',
  32369. 'uses unit2;', // does not use unit typinfo
  32370. 'implementation',
  32371. 'var',
  32372. ' i: byte;',
  32373. ' p: pointer;',
  32374. ' t: PTypeInfo;',
  32375. 'initialization',
  32376. ' p:=typeinfo(i);',
  32377. ' t:=typeinfo(i);',
  32378. ' if p=t then ;',
  32379. ' if p=typeinfo(i) then ;',
  32380. ' if typeinfo(i)=p then ;',
  32381. ' if t=typeinfo(i) then ;',
  32382. ' if typeinfo(i)=t then ;',
  32383. ' DoPtr(p);',
  32384. ' DoPtr(t);',
  32385. ' DoPtr(typeinfo(i));',
  32386. ' DoInfo(p);',
  32387. ' DoInfo(t);',
  32388. ' DoInfo(typeinfo(i));',
  32389. ' DoInt(typeinfo(i));',
  32390. '']);
  32391. ConvertUnit;
  32392. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32393. LinesToStr([ // statements
  32394. 'var $impl = $mod.$impl;',
  32395. '']),
  32396. LinesToStr([ // this.$init
  32397. '$impl.p = rtl.byte;',
  32398. '$impl.t = rtl.byte;',
  32399. 'if ($impl.p === $impl.t) ;',
  32400. 'if ($impl.p === rtl.byte) ;',
  32401. 'if (rtl.byte === $impl.p) ;',
  32402. 'if ($impl.t === rtl.byte) ;',
  32403. 'if (rtl.byte === $impl.t) ;',
  32404. 'pas.unit2.DoPtr($impl.p);',
  32405. 'pas.unit2.DoPtr($impl.t);',
  32406. 'pas.unit2.DoPtr(rtl.byte);',
  32407. 'pas.unit2.DoInfo($impl.p);',
  32408. 'pas.unit2.DoInfo($impl.t);',
  32409. 'pas.unit2.DoInfo(rtl.byte);',
  32410. 'pas.unit2.DoInt(rtl.byte);',
  32411. '']),
  32412. LinesToStr([ // implementation
  32413. '$impl.i = 0;',
  32414. '$impl.p = null;',
  32415. '$impl.t = null;',
  32416. '']) );
  32417. end;
  32418. procedure TTestModule.TestRTTI_Interface_Corba;
  32419. begin
  32420. WithTypeInfo:=true;
  32421. StartProgram(true,[supTypeInfo]);
  32422. Add([
  32423. '{$interfaces corba}',
  32424. '{$modeswitch externalclass}',
  32425. 'type',
  32426. ' IUnknown = interface',
  32427. ' end;',
  32428. ' IBird = interface',
  32429. ' function GetItem: longint;',
  32430. ' procedure SetItem(Value: longint);',
  32431. ' property Item: longint read GetItem write SetItem;',
  32432. ' end;',
  32433. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32434. 'var',
  32435. ' i: IBird;',
  32436. ' t: TTypeInfoInterface;',
  32437. 'begin',
  32438. ' t:=TypeInfo(IBird);',
  32439. ' t:=TypeInfo(i);',
  32440. ' DoIt(t);',
  32441. ' DoIt(TypeInfo(IBird));',
  32442. '']);
  32443. ConvertProgram;
  32444. CheckSource('TestRTTI_Interface_Corba',
  32445. LinesToStr([ // statements
  32446. 'rtl.createInterface(',
  32447. ' this,',
  32448. ' "IUnknown",',
  32449. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32450. ' [],',
  32451. ' null,',
  32452. ' function () {',
  32453. ' }',
  32454. ');',
  32455. 'rtl.createInterface(',
  32456. ' this,',
  32457. ' "IBird",',
  32458. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32459. ' ["GetItem", "SetItem"],',
  32460. ' null,',
  32461. ' function () {',
  32462. ' var $r = this.$rtti;',
  32463. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32464. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32465. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32466. ' }',
  32467. ');',
  32468. 'this.DoIt = function (t) {',
  32469. '}; ',
  32470. 'this.i = null;',
  32471. 'this.t = null;',
  32472. '']),
  32473. LinesToStr([ // $mod.$main
  32474. '$mod.t = $mod.$rtti["IBird"];',
  32475. '$mod.t = $mod.i.$rtti;',
  32476. '$mod.DoIt($mod.t);',
  32477. '$mod.DoIt($mod.$rtti["IBird"]);',
  32478. '']));
  32479. end;
  32480. procedure TTestModule.TestRTTI_Interface_COM;
  32481. begin
  32482. WithTypeInfo:=true;
  32483. StartProgram(true,[supTypeInfo]);
  32484. Add([
  32485. '{$interfaces com}',
  32486. '{$modeswitch externalclass}',
  32487. 'type',
  32488. ' TGuid = record end;',
  32489. ' integer = longint;',
  32490. ' IUnknown = interface',
  32491. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32492. ' function _AddRef: Integer;',
  32493. ' function _Release: Integer;',
  32494. ' end;',
  32495. ' IBird = interface',
  32496. ' function GetItem: longint;',
  32497. ' procedure SetItem(Value: longint);',
  32498. ' property Item: longint read GetItem write SetItem;',
  32499. ' end;',
  32500. 'var',
  32501. ' i: IBird;',
  32502. ' t: TTypeInfoInterface;',
  32503. 'begin',
  32504. ' t:=TypeInfo(IBird);',
  32505. ' t:=TypeInfo(i);',
  32506. '']);
  32507. ConvertProgram;
  32508. CheckSource('TestRTTI_Interface_COM',
  32509. LinesToStr([ // statements
  32510. 'rtl.recNewT(this, "TGuid", function () {',
  32511. ' this.$eq = function (b) {',
  32512. ' return true;',
  32513. ' };',
  32514. ' this.$assign = function (s) {',
  32515. ' return this;',
  32516. ' };',
  32517. ' $mod.$rtti.$Record("TGuid", {});',
  32518. '});',
  32519. 'rtl.createInterface(',
  32520. ' this,',
  32521. ' "IUnknown",',
  32522. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32523. ' ["QueryInterface", "_AddRef", "_Release"],',
  32524. ' null,',
  32525. ' function () {',
  32526. ' this.$kind = "com";',
  32527. ' var $r = this.$rtti;',
  32528. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32529. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32530. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32531. ' }',
  32532. ');',
  32533. 'rtl.createInterface(',
  32534. ' this,',
  32535. ' "IBird",',
  32536. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32537. ' ["GetItem", "SetItem"],',
  32538. ' this.IUnknown,',
  32539. ' function () {',
  32540. ' var $r = this.$rtti;',
  32541. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32542. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32543. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32544. ' }',
  32545. ');',
  32546. 'this.i = null;',
  32547. 'this.t = null;',
  32548. '']),
  32549. LinesToStr([ // $mod.$main
  32550. '$mod.t = $mod.$rtti["IBird"];',
  32551. '$mod.t = $mod.i.$rtti;',
  32552. '']));
  32553. end;
  32554. procedure TTestModule.TestRTTI_ClassHelper;
  32555. begin
  32556. WithTypeInfo:=true;
  32557. StartProgram(true,[supTypeInfo]);
  32558. Add([
  32559. '{$interfaces com}',
  32560. '{$modeswitch externalclass}',
  32561. 'type',
  32562. ' TObject = class',
  32563. ' end;',
  32564. ' THelper = class helper for TObject',
  32565. ' published',
  32566. ' function GetItem: longint;',
  32567. ' property Item: longint read GetItem;',
  32568. ' end;',
  32569. 'function THelper.GetItem: longint;',
  32570. 'begin',
  32571. 'end;',
  32572. 'var',
  32573. ' t: TTypeInfoHelper;',
  32574. 'begin',
  32575. ' t:=TypeInfo(THelper);',
  32576. '']);
  32577. ConvertProgram;
  32578. CheckSource('TestRTTI_ClassHelper',
  32579. LinesToStr([ // statements
  32580. 'rtl.createClass(this, "TObject", null, function () {',
  32581. ' this.$init = function () {',
  32582. ' };',
  32583. ' this.$final = function () {',
  32584. ' };',
  32585. '});',
  32586. 'rtl.createHelper(this, "THelper", null, function () {',
  32587. ' this.GetItem = function () {',
  32588. ' var Result = 0;',
  32589. ' return Result;',
  32590. ' };',
  32591. ' var $r = this.$rtti;',
  32592. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32593. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32594. '});',
  32595. 'this.t = null;',
  32596. '']),
  32597. LinesToStr([ // $mod.$main
  32598. '$mod.t = $mod.$rtti["THelper"];',
  32599. '']));
  32600. end;
  32601. procedure TTestModule.TestRTTI_ExternalClass;
  32602. begin
  32603. WithTypeInfo:=true;
  32604. StartProgram(true,[supTypeInfo]);
  32605. Add([
  32606. '{$modeswitch externalclass}',
  32607. 'type',
  32608. ' TJSObject = class external name ''Object''',
  32609. ' end;',
  32610. ' TJSArray = class external name ''Array'' (TJSObject)',
  32611. ' end;',
  32612. 'var',
  32613. ' p: Pointer;',
  32614. ' tc: TTypeInfoExtClass;',
  32615. 'begin',
  32616. ' p:=typeinfo(TJSArray);']);
  32617. ConvertProgram;
  32618. CheckSource('TestRTTI_ExternalClass',
  32619. LinesToStr([ // statements
  32620. 'this.$rtti.$ExtClass("TJSObject", {',
  32621. ' jsclass: "Object"',
  32622. '});',
  32623. 'this.$rtti.$ExtClass("TJSArray", {',
  32624. ' ancestor: this.$rtti["TJSObject"],',
  32625. ' jsclass: "Array"',
  32626. '});',
  32627. 'this.p = null;',
  32628. 'this.tc = null;',
  32629. '']),
  32630. LinesToStr([ // $mod.$main
  32631. '$mod.p = $mod.$rtti["TJSArray"];',
  32632. '']));
  32633. end;
  32634. procedure TTestModule.TestRTTI_Unit;
  32635. begin
  32636. WithTypeInfo:=true;
  32637. AddModuleWithIntfImplSrc('unit2.pas',
  32638. LinesToStr([
  32639. '{$mode delphi}',
  32640. 'type',
  32641. ' TWordArray = array of word;',
  32642. ' TArray<T> = array of T;',
  32643. '']),
  32644. '');
  32645. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  32646. Add([
  32647. '{$mode delphi}',
  32648. 'interface',
  32649. 'uses unit2;',
  32650. 'type',
  32651. ' IBird = interface',
  32652. ' function Swoop: TWordArray;',
  32653. ' function Glide: TArray<word>;',
  32654. ' end;',
  32655. 'procedure Fly;',
  32656. 'implementation',
  32657. 'procedure Fly;',
  32658. 'var',
  32659. ' ta: tTypeInfoDynArray;',
  32660. ' ti: tTypeInfoInterface;',
  32661. 'begin',
  32662. ' ta:=typeinfo(TWordArray);',
  32663. ' ta:=typeinfo(TArray<word>);',
  32664. ' ti:=typeinfo(IBird);',
  32665. 'end;',
  32666. '']);
  32667. ConvertUnit;
  32668. CheckSource('TestRTTI_ExternalClass',
  32669. LinesToStr([ // statements
  32670. 'rtl.createInterface(',
  32671. ' this,',
  32672. ' "IBird",',
  32673. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  32674. ' ["Swoop", "Glide"],',
  32675. ' pas.system.IUnknown,',
  32676. ' function () {',
  32677. ' var $r = this.$rtti;',
  32678. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  32679. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  32680. ' }',
  32681. ');',
  32682. 'this.Fly = function () {',
  32683. ' var ta = null;',
  32684. ' var ti = null;',
  32685. ' ta = pas.unit2.$rtti["TWordArray"];',
  32686. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  32687. ' ti = $mod.$rtti["IBird"];',
  32688. '};',
  32689. '']),
  32690. LinesToStr([ // $mod.$main
  32691. '']));
  32692. end;
  32693. procedure TTestModule.TestResourcestringProgram;
  32694. begin
  32695. AddModuleWithIntfImplSrc('unit2.pas',
  32696. LinesToStr([
  32697. 'resourcestring Title = ''Nice'';',
  32698. '']),
  32699. '');
  32700. StartProgram(true);
  32701. Add([
  32702. 'uses unit2;',
  32703. 'const Bar = ''bar'';',
  32704. 'resourcestring',
  32705. ' Red = ''red'';',
  32706. ' Foobar = ''fOo''+bar;',
  32707. 'var s: string;',
  32708. ' c: char;',
  32709. 'begin',
  32710. ' s:=red;',
  32711. ' s:=test1.red;',
  32712. ' s:=Title;',
  32713. ' c:=red[1];',
  32714. ' c:=test1.red[2];',
  32715. ' if red=foobar then ;',
  32716. ' if red[3]=red[4] then ;']);
  32717. ConvertProgram;
  32718. CheckSource('TestResourcestringProgram',
  32719. LinesToStr([ // statements
  32720. 'this.Bar = "bar";',
  32721. 'this.s = "";',
  32722. 'this.c = "";',
  32723. '$mod.$resourcestrings = {',
  32724. ' Red: {',
  32725. ' org: "red"',
  32726. ' },',
  32727. ' Foobar: {',
  32728. ' org: "fOobar"',
  32729. ' }',
  32730. '};',
  32731. '']),
  32732. LinesToStr([ // $mod.$main
  32733. '$mod.s = rtl.getResStr($mod, "Red");',
  32734. '$mod.s = rtl.getResStr($mod, "Red");',
  32735. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32736. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32737. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32738. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32739. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32740. '']));
  32741. end;
  32742. procedure TTestModule.TestResourcestringUnit;
  32743. begin
  32744. AddModuleWithIntfImplSrc('unit2.pas',
  32745. LinesToStr([
  32746. 'resourcestring Title = ''Nice'';',
  32747. '']),
  32748. '');
  32749. StartUnit(true);
  32750. Add([
  32751. 'interface',
  32752. 'uses unit2;',
  32753. 'const Red = ''rEd'';',
  32754. 'resourcestring',
  32755. ' Blue = ''blue'';',
  32756. ' NotRed = ''not''+Red;',
  32757. 'var s: string;',
  32758. 'implementation',
  32759. 'resourcestring',
  32760. ' ImplGreen = ''green'';',
  32761. 'initialization',
  32762. ' s:=blue+ImplGreen;',
  32763. ' s:=test1.blue+test1.implgreen;',
  32764. ' s:=blue[1]+implgreen[2];',
  32765. ' s:=Title;',
  32766. '']);
  32767. ConvertUnit;
  32768. CheckSource('TestResourcestringUnit',
  32769. LinesToStr([ // statements
  32770. 'this.Red = "rEd";',
  32771. 'this.s = "";',
  32772. '$mod.$resourcestrings = {',
  32773. ' Blue: {',
  32774. ' org: "blue"',
  32775. ' },',
  32776. ' NotRed: {',
  32777. ' org: "notrEd"',
  32778. ' },',
  32779. ' ImplGreen: {',
  32780. ' org: "green"',
  32781. ' }',
  32782. '};',
  32783. '']),
  32784. LinesToStr([ // $mod.$main
  32785. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32786. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32787. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32788. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32789. '']));
  32790. end;
  32791. procedure TTestModule.TestResourcestringImplementation;
  32792. begin
  32793. StartUnit(false);
  32794. Add([
  32795. 'interface',
  32796. 'implementation',
  32797. 'resourcestring',
  32798. ' ImplRed = ''red'';']);
  32799. ConvertUnit;
  32800. CheckSource('TestResourcestringImplementation',
  32801. LinesToStr([ // intf statements
  32802. 'var $impl = $mod.$impl;']),
  32803. LinesToStr([ // $mod.$init
  32804. '']),
  32805. LinesToStr([ // impl statements
  32806. '$mod.$resourcestrings = {',
  32807. ' ImplRed: {',
  32808. ' org: "red"',
  32809. ' }',
  32810. '};',
  32811. '']));
  32812. end;
  32813. procedure TTestModule.TestAttributes_Members;
  32814. begin
  32815. WithTypeInfo:=true;
  32816. StartProgram(false);
  32817. Add([
  32818. '{$modeswitch PrefixedAttributes}',
  32819. 'type',
  32820. ' TObject = class',
  32821. ' constructor Create;',
  32822. ' end;',
  32823. ' TCustomAttribute = class',
  32824. ' constructor Create(Id: word);',
  32825. ' end;',
  32826. ' [Missing]',
  32827. ' TBird = class',
  32828. ' published',
  32829. ' [Tcustom]',
  32830. ' FField: word;',
  32831. ' [tcustom(14)]',
  32832. ' property Size: word read FField;',
  32833. ' [Tcustom(15)]',
  32834. ' procedure Fly; virtual; abstract;',
  32835. ' end;',
  32836. ' TRec = record',
  32837. ' [Tcustom,tcustom(14)]',
  32838. ' Size: word;',
  32839. ' end;',
  32840. 'constructor TObject.Create; begin end;',
  32841. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32842. 'begin',
  32843. '']);
  32844. ConvertProgram;
  32845. CheckSource('TestAttributes_Members',
  32846. LinesToStr([ // statements
  32847. 'rtl.createClass(this, "TObject", null, function () {',
  32848. ' this.$init = function () {',
  32849. ' };',
  32850. ' this.$final = function () {',
  32851. ' };',
  32852. ' this.Create = function () {',
  32853. ' return this;',
  32854. ' };',
  32855. '});',
  32856. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32857. ' this.Create$1 = function (Id) {',
  32858. ' return this;',
  32859. ' };',
  32860. '});',
  32861. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32862. ' this.$init = function () {',
  32863. ' $mod.TObject.$init.call(this);',
  32864. ' this.FField = 0;',
  32865. ' };',
  32866. ' var $r = this.$rtti;',
  32867. ' $r.addField("FField", rtl.word, {',
  32868. ' attr: [$mod.TCustomAttribute, "Create"]',
  32869. ' });',
  32870. ' $r.addProperty(',
  32871. ' "Size",',
  32872. ' 0,',
  32873. ' rtl.word,',
  32874. ' "FField",',
  32875. ' "",',
  32876. ' {',
  32877. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  32878. ' }',
  32879. ' );',
  32880. ' $r.addMethod("Fly", 0, [], null, 0, {',
  32881. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  32882. ' });',
  32883. '});',
  32884. 'rtl.recNewT(this, "TRec", function () {',
  32885. ' this.Size = 0;',
  32886. ' this.$eq = function (b) {',
  32887. ' return this.Size === b.Size;',
  32888. ' };',
  32889. ' this.$assign = function (s) {',
  32890. ' this.Size = s.Size;',
  32891. ' return this;',
  32892. ' };',
  32893. ' var $r = $mod.$rtti.$Record("TRec", {});',
  32894. ' $r.addField("Size", rtl.word, {',
  32895. ' attr: [',
  32896. ' $mod.TCustomAttribute,',
  32897. ' "Create",',
  32898. ' $mod.TCustomAttribute,',
  32899. ' "Create$1",',
  32900. ' [14]',
  32901. ' ]',
  32902. ' });',
  32903. '});',
  32904. '']),
  32905. LinesToStr([ // $mod.$main
  32906. '']));
  32907. end;
  32908. procedure TTestModule.TestAttributes_Types;
  32909. begin
  32910. WithTypeInfo:=true;
  32911. StartProgram(false);
  32912. Add([
  32913. '{$modeswitch PrefixedAttributes}',
  32914. 'type',
  32915. ' TObject = class',
  32916. ' constructor Create(Id: word);',
  32917. ' end;',
  32918. ' TCustomAttribute = class',
  32919. ' end;',
  32920. ' [TCustom(1)]',
  32921. ' TMyClass = class',
  32922. ' end;',
  32923. ' [TCustom(11)]',
  32924. ' TMyDescendant = class(TMyClass)',
  32925. ' end;',
  32926. ' [TCustom(2)]',
  32927. ' TRec = record',
  32928. ' end;',
  32929. ' [TCustom(3)]',
  32930. ' TInt = type word;',
  32931. 'constructor TObject.Create(Id: word);',
  32932. 'begin',
  32933. 'end;',
  32934. 'var p: pointer;',
  32935. 'begin',
  32936. ' p:=typeinfo(TMyClass);',
  32937. ' p:=typeinfo(TRec);',
  32938. ' p:=typeinfo(TInt);',
  32939. '']);
  32940. ConvertProgram;
  32941. CheckSource('TestAttributes_Types',
  32942. LinesToStr([ // statements
  32943. 'rtl.createClass(this, "TObject", null, function () {',
  32944. ' this.$init = function () {',
  32945. ' };',
  32946. ' this.$final = function () {',
  32947. ' };',
  32948. ' this.Create = function (Id) {',
  32949. ' return this;',
  32950. ' };',
  32951. '});',
  32952. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32953. '});',
  32954. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  32955. ' var $r = this.$rtti;',
  32956. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  32957. '});',
  32958. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  32959. ' var $r = this.$rtti;',
  32960. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  32961. '});',
  32962. 'rtl.recNewT(this, "TRec", function () {',
  32963. ' this.$eq = function (b) {',
  32964. ' return true;',
  32965. ' };',
  32966. ' this.$assign = function (s) {',
  32967. ' return this;',
  32968. ' };',
  32969. ' $mod.$rtti.$Record("TRec", {',
  32970. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  32971. ' });',
  32972. '});',
  32973. 'this.$rtti.$inherited("TInt", rtl.word, {',
  32974. ' attr: [this.TCustomAttribute, "Create", [3]]',
  32975. '});',
  32976. 'this.p = null;',
  32977. '']),
  32978. LinesToStr([ // $mod.$main
  32979. '$mod.p = $mod.$rtti["TMyClass"];',
  32980. '$mod.p = $mod.$rtti["TRec"];',
  32981. '$mod.p = $mod.$rtti["TInt"];',
  32982. '']));
  32983. end;
  32984. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  32985. begin
  32986. WithTypeInfo:=true;
  32987. StartProgram(false);
  32988. Add([
  32989. '{$modeswitch PrefixedAttributes}',
  32990. 'type',
  32991. ' TObject = class',
  32992. ' constructor Create;',
  32993. ' end;',
  32994. ' TCustomAttribute = class',
  32995. ' end;',
  32996. ' THelper = class helper for TCustomAttribute',
  32997. ' constructor Create(Id: word);',
  32998. ' end;',
  32999. ' [TCustom(3)]',
  33000. ' TMyInt = word;',
  33001. 'constructor TObject.Create; begin end;',
  33002. 'constructor THelper.Create(Id: word); begin end;',
  33003. 'begin',
  33004. ' if typeinfo(TMyInt)=nil then ;']);
  33005. ConvertProgram;
  33006. end;
  33007. procedure TTestModule.TestAttributes_InterfacesList;
  33008. begin
  33009. WithTypeInfo:=true;
  33010. StartProgram(false);
  33011. Add([
  33012. '{$mode Delphi}',
  33013. 'type',
  33014. ' TObject = class',
  33015. ' constructor Create;',
  33016. ' end;',
  33017. ' IInterface = interface end;',
  33018. ' TCustomAttribute = class',
  33019. ' end;',
  33020. ' Red = class(TCustomAttribute);',
  33021. ' Blue = class(TCustomAttribute);',
  33022. ' [Red]',
  33023. ' IBird<T> = interface',
  33024. ' procedure Fly;',
  33025. ' end;',
  33026. ' [Blue]',
  33027. ' IEagle = interface(IBird<Word>)',
  33028. ' procedure Dive;',
  33029. ' end;',
  33030. ' TAnt = class(TObject, IEagle)',
  33031. ' procedure Fly; virtual; abstract;',
  33032. ' procedure Dive; virtual; abstract;',
  33033. ' end;',
  33034. 'constructor TObject.Create;',
  33035. 'begin',
  33036. 'end;',
  33037. 'begin',
  33038. '']);
  33039. ConvertProgram;
  33040. CheckSource('TestAttributes_InterfacesList',
  33041. LinesToStr([ // statements
  33042. '$mod.$rtti.$Interface("IBird<System.Word>");',
  33043. 'rtl.createClass(this, "TObject", null, function () {',
  33044. ' this.$init = function () {',
  33045. ' };',
  33046. ' this.$final = function () {',
  33047. ' };',
  33048. ' this.Create = function () {',
  33049. ' return this;',
  33050. ' };',
  33051. '});',
  33052. 'rtl.createInterface(',
  33053. ' this,',
  33054. ' "IInterface",',
  33055. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  33056. ' [],',
  33057. ' null,',
  33058. ' function () {',
  33059. ' this.$kind = "com";',
  33060. ' }',
  33061. ');',
  33062. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33063. '});',
  33064. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  33065. '});',
  33066. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  33067. '});',
  33068. 'rtl.createInterface(',
  33069. ' this,',
  33070. ' "IBird$G1",',
  33071. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  33072. ' ["Fly"],',
  33073. ' this.IInterface,',
  33074. ' function () {',
  33075. ' var $r = this.$rtti;',
  33076. ' $r.addMethod("Fly", 0, []);',
  33077. ' $r.attr = [$mod.Red, "Create"];',
  33078. ' },',
  33079. ' "IBird<System.Word>"',
  33080. ');',
  33081. 'rtl.createInterface(',
  33082. ' this,',
  33083. ' "IEagle",',
  33084. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  33085. ' ["Dive"],',
  33086. ' this.IBird$G1,',
  33087. ' function () {',
  33088. ' var $r = this.$rtti;',
  33089. ' $r.addMethod("Dive", 0, []);',
  33090. ' $r.attr = [$mod.Blue, "Create"];',
  33091. ' }',
  33092. ');',
  33093. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  33094. ' rtl.addIntf(this, $mod.IEagle);',
  33095. '});',
  33096. '']),
  33097. LinesToStr([ // $mod.$main
  33098. '']));
  33099. end;
  33100. procedure TTestModule.TestAssert;
  33101. begin
  33102. StartProgram(false);
  33103. Add([
  33104. 'procedure DoIt;',
  33105. 'var',
  33106. ' b: boolean;',
  33107. ' s: string;',
  33108. 'begin',
  33109. ' {$Assertions on}',
  33110. ' Assert(b);',
  33111. 'end;',
  33112. 'begin',
  33113. ' DoIt;',
  33114. '']);
  33115. ConvertProgram;
  33116. CheckSource('TestAssert',
  33117. LinesToStr([ // statements
  33118. 'this.DoIt = function () {',
  33119. ' var b = false;',
  33120. ' var s = "";',
  33121. ' if (!b) throw "assert failed";',
  33122. '};',
  33123. '']),
  33124. LinesToStr([ // $mod.$main
  33125. '$mod.DoIt();',
  33126. '']));
  33127. end;
  33128. procedure TTestModule.TestAssert_SysUtils;
  33129. begin
  33130. AddModuleWithIntfImplSrc('SysUtils.pas',
  33131. LinesToStr([
  33132. 'type',
  33133. ' TObject = class',
  33134. ' constructor Create;',
  33135. ' end;',
  33136. ' EAssertionFailed = class',
  33137. ' constructor Create(s: string);',
  33138. ' end;',
  33139. '']),
  33140. LinesToStr([
  33141. 'constructor TObject.Create;',
  33142. 'begin end;',
  33143. 'constructor EAssertionFailed.Create(s: string);',
  33144. 'begin end;',
  33145. '']) );
  33146. StartProgram(true);
  33147. Add([
  33148. 'uses sysutils;',
  33149. 'procedure DoIt;',
  33150. 'var',
  33151. ' b: boolean;',
  33152. ' s: string;',
  33153. 'begin',
  33154. ' {$Assertions on}',
  33155. ' Assert(b);',
  33156. ' Assert(b,''msg'');',
  33157. 'end;',
  33158. 'begin',
  33159. ' DoIt;',
  33160. '']);
  33161. ConvertProgram;
  33162. CheckSource('TestAssert_SysUtils',
  33163. LinesToStr([ // statements
  33164. 'this.DoIt = function () {',
  33165. ' var b = false;',
  33166. ' var s = "";',
  33167. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  33168. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  33169. '};',
  33170. '']),
  33171. LinesToStr([ // $mod.$main
  33172. '$mod.DoIt();',
  33173. '']));
  33174. end;
  33175. procedure TTestModule.TestObjectChecks;
  33176. begin
  33177. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  33178. StartProgram(false);
  33179. Add([
  33180. 'type',
  33181. ' TObject = class',
  33182. ' procedure DoIt;',
  33183. ' end;',
  33184. ' TClass = class of tobject;',
  33185. ' TBird = class',
  33186. ' end;',
  33187. ' TBirdClass = class of TBird;',
  33188. 'var',
  33189. ' o : TObject;',
  33190. ' c: TClass;',
  33191. ' b: TBird;',
  33192. ' bc: TBirdClass;',
  33193. 'procedure TObject.DoIt;',
  33194. 'begin',
  33195. ' b:=TBird(o);',
  33196. 'end;',
  33197. 'begin',
  33198. ' o.DoIt;',
  33199. ' b:=TBird(o);',
  33200. ' bc:=TBirdClass(c);',
  33201. '']);
  33202. ConvertProgram;
  33203. CheckSource('TestCheckMethodCall',
  33204. LinesToStr([ // statements
  33205. 'rtl.createClass(this, "TObject", null, function () {',
  33206. ' this.$init = function () {',
  33207. ' };',
  33208. ' this.$final = function () {',
  33209. ' };',
  33210. ' this.DoIt = function () {',
  33211. ' rtl.checkMethodCall(this,$mod.TObject);',
  33212. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  33213. ' };',
  33214. '});',
  33215. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33216. '});',
  33217. 'this.o = null;',
  33218. 'this.c = null;',
  33219. 'this.b = null;',
  33220. 'this.bc = null;',
  33221. '']),
  33222. LinesToStr([ // $mod.$main
  33223. '$mod.o.DoIt();',
  33224. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  33225. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  33226. '']));
  33227. end;
  33228. procedure TTestModule.TestOverflowChecks_Int;
  33229. begin
  33230. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  33231. StartProgram(false);
  33232. Add([
  33233. 'procedure DoIt;',
  33234. 'var',
  33235. ' b: byte;',
  33236. ' n: nativeint;',
  33237. ' u: nativeuint;',
  33238. ' c: currency;',
  33239. 'begin',
  33240. ' n:=n+n;',
  33241. ' n:=n-n;',
  33242. ' n:=n+b;',
  33243. ' n:=b-n;',
  33244. ' n:=n*n;',
  33245. ' n:=n*u;',
  33246. ' c:=c+b;',
  33247. ' c:=b+c;',
  33248. ' c:=c*b;',
  33249. ' c:=b*c;',
  33250. 'end;',
  33251. 'begin',
  33252. '']);
  33253. ConvertProgram;
  33254. CheckSource('TestOverflowChecks_Int',
  33255. LinesToStr([ // statements
  33256. 'this.DoIt = function () {',
  33257. ' var b = 0;',
  33258. ' var n = 0;',
  33259. ' var u = 0;',
  33260. ' var c = 0;',
  33261. ' n = rtl.oc(n + n);',
  33262. ' n = rtl.oc(n - n);',
  33263. ' n = rtl.oc(n + b);',
  33264. ' n = rtl.oc(b - n);',
  33265. ' n = rtl.oc(n * n);',
  33266. ' n = rtl.oc(n * u);',
  33267. ' c = rtl.oc(c + (b * 10000));',
  33268. ' c = rtl.oc((b * 10000) + c);',
  33269. ' c = rtl.oc(c * b);',
  33270. ' c = rtl.oc(b * c);',
  33271. '};',
  33272. '']),
  33273. LinesToStr([ // $mod.$main
  33274. '']));
  33275. end;
  33276. procedure TTestModule.TestRangeChecks_AssignInt;
  33277. begin
  33278. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33279. StartProgram(false);
  33280. Add([
  33281. '{$R+}',
  33282. 'var',
  33283. ' b: byte = 2;',
  33284. ' w: word = 3;',
  33285. 'procedure DoIt(p: byte);',
  33286. 'begin',
  33287. ' b:=w;',
  33288. ' b+=w;',
  33289. ' b:=1;',
  33290. 'end;',
  33291. '{$R-}',
  33292. 'procedure DoSome;',
  33293. 'begin',
  33294. ' DoIt(w);',
  33295. ' b:=w;',
  33296. ' b:=2;',
  33297. 'end;',
  33298. 'begin',
  33299. '{$R+}',
  33300. '']);
  33301. ConvertProgram;
  33302. CheckSource('TestRangeChecks_AssignInt',
  33303. LinesToStr([ // statements
  33304. 'this.b = 2;',
  33305. 'this.w = 3;',
  33306. 'this.DoIt = function (p) {',
  33307. ' rtl.rc(p, 0, 255);',
  33308. ' $mod.b = rtl.rc($mod.w,0,255);',
  33309. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33310. ' $mod.b = 1;',
  33311. '};',
  33312. 'this.DoSome = function () {',
  33313. ' $mod.DoIt($mod.w);',
  33314. ' $mod.b = $mod.w;',
  33315. ' $mod.b = 2;',
  33316. '};',
  33317. '']),
  33318. LinesToStr([ // $mod.$main
  33319. '']));
  33320. end;
  33321. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33322. begin
  33323. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33324. StartProgram(false);
  33325. Add([
  33326. '{$R+}',
  33327. 'type Ten = 1..10;',
  33328. 'var',
  33329. ' b: Ten = 2;',
  33330. ' w: Ten = 3;',
  33331. 'procedure DoIt(p: Ten);',
  33332. 'begin',
  33333. ' b:=w;',
  33334. ' b+=w;',
  33335. ' b:=1;',
  33336. 'end;',
  33337. '{$R-}',
  33338. 'procedure DoSome;',
  33339. 'begin',
  33340. ' DoIt(w);',
  33341. ' b:=w;',
  33342. ' b:=2;',
  33343. 'end;',
  33344. 'begin',
  33345. '{$R+}',
  33346. '']);
  33347. ConvertProgram;
  33348. CheckSource('TestRangeChecks_AssignIntRange',
  33349. LinesToStr([ // statements
  33350. 'this.b = 2;',
  33351. 'this.w = 3;',
  33352. 'this.DoIt = function (p) {',
  33353. ' rtl.rc(p, 1, 10);',
  33354. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33355. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33356. ' $mod.b = 1;',
  33357. '};',
  33358. 'this.DoSome = function () {',
  33359. ' $mod.DoIt($mod.w);',
  33360. ' $mod.b = $mod.w;',
  33361. ' $mod.b = 2;',
  33362. '};',
  33363. '']),
  33364. LinesToStr([ // $mod.$main
  33365. '']));
  33366. end;
  33367. procedure TTestModule.TestRangeChecks_AssignEnum;
  33368. begin
  33369. StartProgram(false);
  33370. Add([
  33371. '{$R+}',
  33372. 'type TEnum = (red,green);',
  33373. 'var',
  33374. ' e: TEnum = red;',
  33375. 'procedure DoIt(p: TEnum);',
  33376. 'begin',
  33377. ' e:=p;',
  33378. ' p:=TEnum(0);',
  33379. ' p:=succ(e);',
  33380. 'end;',
  33381. '{$R-}',
  33382. 'procedure DoSome;',
  33383. 'begin',
  33384. ' DoIt(e);',
  33385. ' e:=TEnum(1);',
  33386. ' e:=pred(e);',
  33387. 'end;',
  33388. 'begin',
  33389. '{$R+}',
  33390. '']);
  33391. ConvertProgram;
  33392. CheckSource('TestRangeChecks_AssignEnum',
  33393. LinesToStr([ // statements
  33394. 'this.TEnum = {',
  33395. ' "0": "red",',
  33396. ' red: 0,',
  33397. ' "1": "green",',
  33398. ' green: 1',
  33399. '};',
  33400. 'this.e = this.TEnum.red;',
  33401. 'this.DoIt = function (p) {',
  33402. ' rtl.rc(p, 0, 1);',
  33403. ' $mod.e = rtl.rc(p, 0, 1);',
  33404. ' p = 0;',
  33405. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33406. '};',
  33407. 'this.DoSome = function () {',
  33408. ' $mod.DoIt($mod.e);',
  33409. ' $mod.e = 1;',
  33410. ' $mod.e = $mod.e - 1;',
  33411. '};',
  33412. '']),
  33413. LinesToStr([ // $mod.$main
  33414. '']));
  33415. end;
  33416. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33417. begin
  33418. StartProgram(false);
  33419. Add([
  33420. '{$R+}',
  33421. 'type',
  33422. ' TEnum = (red,green);',
  33423. ' TEnumRg = red..green;',
  33424. 'var',
  33425. ' e: TEnumRg = red;',
  33426. 'procedure DoIt(p: TEnumRg);',
  33427. 'begin',
  33428. ' e:=p;',
  33429. ' p:=TEnumRg(0);',
  33430. ' p:=succ(e);',
  33431. 'end;',
  33432. '{$R-}',
  33433. 'procedure DoSome;',
  33434. 'begin',
  33435. ' DoIt(e);',
  33436. ' e:=TEnum(1);',
  33437. ' e:=pred(e);',
  33438. 'end;',
  33439. 'begin',
  33440. '{$R+}',
  33441. '']);
  33442. ConvertProgram;
  33443. CheckSource('TestRangeChecks_AssignEnumRange',
  33444. LinesToStr([ // statements
  33445. 'this.TEnum = {',
  33446. ' "0": "red",',
  33447. ' red: 0,',
  33448. ' "1": "green",',
  33449. ' green: 1',
  33450. '};',
  33451. 'this.e = this.TEnum.red;',
  33452. 'this.DoIt = function (p) {',
  33453. ' rtl.rc(p, 0, 1);',
  33454. ' $mod.e = rtl.rc(p, 0, 1);',
  33455. ' p = 0;',
  33456. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33457. '};',
  33458. 'this.DoSome = function () {',
  33459. ' $mod.DoIt($mod.e);',
  33460. ' $mod.e = 1;',
  33461. ' $mod.e = $mod.e - 1;',
  33462. '};',
  33463. '']),
  33464. LinesToStr([ // $mod.$main
  33465. '']));
  33466. end;
  33467. procedure TTestModule.TestRangeChecks_AssignChar;
  33468. begin
  33469. StartProgram(false);
  33470. Add([
  33471. '{$R+}',
  33472. 'type',
  33473. ' TLetter = char;',
  33474. 'var',
  33475. ' b: TLetter = ''2'';',
  33476. ' w: TLetter = ''3'';',
  33477. 'procedure DoIt(p: TLetter);',
  33478. 'begin',
  33479. ' b:=w;',
  33480. ' b:=''1'';',
  33481. 'end;',
  33482. '{$R-}',
  33483. 'procedure DoSome;',
  33484. 'begin',
  33485. ' DoIt(w);',
  33486. ' b:=w;',
  33487. ' b:=''2'';',
  33488. 'end;',
  33489. 'begin',
  33490. '{$R+}',
  33491. '']);
  33492. ConvertProgram;
  33493. CheckSource('TestRangeChecks_AssignChar',
  33494. LinesToStr([ // statements
  33495. 'this.b = "2";',
  33496. 'this.w = "3";',
  33497. 'this.DoIt = function (p) {',
  33498. ' rtl.rcc(p, 0, 65535);',
  33499. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33500. ' $mod.b = "1";',
  33501. '};',
  33502. 'this.DoSome = function () {',
  33503. ' $mod.DoIt($mod.w);',
  33504. ' $mod.b = $mod.w;',
  33505. ' $mod.b = "2";',
  33506. '};',
  33507. '']),
  33508. LinesToStr([ // $mod.$main
  33509. '']));
  33510. end;
  33511. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33512. begin
  33513. StartProgram(false);
  33514. Add([
  33515. '{$R+}',
  33516. 'type TDigit = ''0''..''9'';',
  33517. 'var',
  33518. ' b: TDigit = ''2'';',
  33519. ' w: TDigit = ''3'';',
  33520. 'procedure DoIt(p: TDigit);',
  33521. 'begin',
  33522. ' b:=w;',
  33523. ' b:=''1'';',
  33524. 'end;',
  33525. '{$R-}',
  33526. 'procedure DoSome;',
  33527. 'begin',
  33528. ' DoIt(w);',
  33529. ' b:=w;',
  33530. ' b:=''2'';',
  33531. 'end;',
  33532. 'begin',
  33533. '{$R+}',
  33534. '']);
  33535. ConvertProgram;
  33536. CheckSource('TestRangeChecks_AssignCharRange',
  33537. LinesToStr([ // statements
  33538. 'this.b = "2";',
  33539. 'this.w = "3";',
  33540. 'this.DoIt = function (p) {',
  33541. ' rtl.rcc(p, 48, 57);',
  33542. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33543. ' $mod.b = "1";',
  33544. '};',
  33545. 'this.DoSome = function () {',
  33546. ' $mod.DoIt($mod.w);',
  33547. ' $mod.b = $mod.w;',
  33548. ' $mod.b = "2";',
  33549. '};',
  33550. '']),
  33551. LinesToStr([ // $mod.$main
  33552. '']));
  33553. end;
  33554. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33555. begin
  33556. StartProgram(false);
  33557. Add([
  33558. '{$R+}',
  33559. 'type',
  33560. ' Ten = 1..10;',
  33561. ' TArr = array of Ten;',
  33562. ' TArrArr = array of TArr;',
  33563. ' TArrByte = array[byte] of Ten;',
  33564. ' TArrChar = array[''0''..''9''] of Ten;',
  33565. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33566. ' TObject = class',
  33567. ' A: TArr;',
  33568. ' end;',
  33569. 'procedure DoIt;',
  33570. 'var',
  33571. ' Arr: TArr;',
  33572. ' ArrArr: TArrArr;',
  33573. ' ArrByte: TArrByte;',
  33574. ' ArrChar: TArrChar;',
  33575. ' ArrByteChar: TArrByteChar;',
  33576. ' i: Ten;',
  33577. ' c: char;',
  33578. ' o: tobject;',
  33579. 'begin',
  33580. ' i:=Arr[1];',
  33581. ' i:=ArrByteChar[1,''2''];',
  33582. ' Arr[1]:=Arr[1];',
  33583. ' Arr[i]:=Arr[i];',
  33584. ' ArrByte[3]:=ArrByte[3];',
  33585. ' ArrByte[i]:=ArrByte[i];',
  33586. ' ArrChar[''5'']:=ArrChar[''5''];',
  33587. ' ArrChar[c]:=ArrChar[c];',
  33588. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  33589. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  33590. ' o.a[i]:=o.a[i];',
  33591. 'end;',
  33592. 'begin',
  33593. '']);
  33594. ConvertProgram;
  33595. CheckSource('TestRangeChecks_ArrayIndex',
  33596. LinesToStr([ // statements
  33597. 'this.TArrByteChar$clone = function (a) {',
  33598. ' var b = [];',
  33599. ' b.length = 256;',
  33600. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  33601. ' return b;',
  33602. '};',
  33603. 'rtl.createClass(this, "TObject", null, function () {',
  33604. ' this.$init = function () {',
  33605. ' this.A = [];',
  33606. ' };',
  33607. ' this.$final = function () {',
  33608. ' this.A = undefined;',
  33609. ' };',
  33610. '});',
  33611. 'this.DoIt = function () {',
  33612. ' var Arr = [];',
  33613. ' var ArrArr = [];',
  33614. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  33615. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  33616. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  33617. ' var i = 0;',
  33618. ' var c = "";',
  33619. ' var o = null;',
  33620. ' i = rtl.rc(Arr[1], 1, 10);',
  33621. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  33622. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  33623. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  33624. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  33625. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  33626. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  33627. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  33628. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  33629. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  33630. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  33631. '};',
  33632. '']),
  33633. LinesToStr([ // $mod.$main
  33634. '']));
  33635. end;
  33636. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  33637. begin
  33638. StartProgram(false);
  33639. Add([
  33640. '{$R+}',
  33641. 'type',
  33642. ' Ten = 1..10;',
  33643. ' TRec = record x: Ten end;',
  33644. ' TArr = array of TRec;',
  33645. ' TArrArr = array of TArr;',
  33646. ' TObject = class',
  33647. ' A: TArr;',
  33648. ' end;',
  33649. 'procedure DoIt;',
  33650. 'var',
  33651. ' Arr: TArr;',
  33652. ' ArrArr: TArrArr;',
  33653. ' i: Ten;',
  33654. ' o: tobject;',
  33655. 'begin',
  33656. ' Arr[1]:=Arr[1];',
  33657. ' Arr[i]:=Arr[i+1];',
  33658. ' o.a[i]:=o.a[i+2];',
  33659. 'end;',
  33660. 'begin',
  33661. '']);
  33662. ConvertProgram;
  33663. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  33664. LinesToStr([ // statements
  33665. 'rtl.recNewT(this, "TRec", function () {',
  33666. ' this.x = 0;',
  33667. ' this.$eq = function (b) {',
  33668. ' return this.x === b.x;',
  33669. ' };',
  33670. ' this.$assign = function (s) {',
  33671. ' this.x = s.x;',
  33672. ' return this;',
  33673. ' };',
  33674. '});',
  33675. 'rtl.createClass(this, "TObject", null, function () {',
  33676. ' this.$init = function () {',
  33677. ' this.A = [];',
  33678. ' };',
  33679. ' this.$final = function () {',
  33680. ' this.A = undefined;',
  33681. ' };',
  33682. '});',
  33683. 'this.DoIt = function () {',
  33684. ' var Arr = [];',
  33685. ' var ArrArr = [];',
  33686. ' var i = 0;',
  33687. ' var o = null;',
  33688. ' Arr[1].$assign(Arr[1]);',
  33689. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  33690. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  33691. '};',
  33692. '']),
  33693. LinesToStr([ // $mod.$main
  33694. '']));
  33695. end;
  33696. procedure TTestModule.TestRangeChecks_StringIndex;
  33697. begin
  33698. StartProgram(false);
  33699. Add([
  33700. 'type',
  33701. ' TObject = class',
  33702. ' S: string;',
  33703. ' end;',
  33704. '{$R+}',
  33705. 'procedure DoIt(var h: string);',
  33706. 'var',
  33707. ' s: string;',
  33708. ' i: longint;',
  33709. ' c: char;',
  33710. ' o: tobject;',
  33711. 'begin',
  33712. ' c:=s[1];',
  33713. ' s[i]:=s[i];',
  33714. ' h[i]:=h[i];',
  33715. ' c:=o.s[i];',
  33716. ' o.s[i]:=c;',
  33717. 'end;',
  33718. 'begin',
  33719. '']);
  33720. ConvertProgram;
  33721. CheckSource('TestRangeChecks_StringIndex',
  33722. LinesToStr([ // statements
  33723. 'rtl.createClass(this, "TObject", null, function () {',
  33724. ' this.$init = function () {',
  33725. ' this.S = "";',
  33726. ' };',
  33727. ' this.$final = function () {',
  33728. ' };',
  33729. '});',
  33730. 'this.DoIt = function (h) {',
  33731. ' var s = "";',
  33732. ' var i = 0;',
  33733. ' var c = "";',
  33734. ' var o = null;',
  33735. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33736. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33737. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33738. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33739. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33740. '};',
  33741. '']),
  33742. LinesToStr([ // $mod.$main
  33743. '']));
  33744. end;
  33745. procedure TTestModule.TestRangeChecks_TypecastInt;
  33746. begin
  33747. StartProgram(false);
  33748. Add([
  33749. '{$R+}',
  33750. 'var',
  33751. ' i: nativeint;',
  33752. ' b: byte;',
  33753. ' sh: shortint;',
  33754. ' w: word;',
  33755. ' sm: smallint;',
  33756. ' lw: longword;',
  33757. ' li: longint;',
  33758. 'begin',
  33759. ' b:=12+byte(i);',
  33760. ' sh:=12+shortint(i);',
  33761. ' w:=12+word(i);',
  33762. ' sm:=12+smallint(i);',
  33763. ' lw:=12+longword(i);',
  33764. ' li:=12+longint(i);',
  33765. '']);
  33766. ConvertProgram;
  33767. CheckSource('TestRangeChecks_TypecastInt',
  33768. LinesToStr([
  33769. 'this.i = 0;',
  33770. 'this.b = 0;',
  33771. 'this.sh = 0;',
  33772. 'this.w = 0;',
  33773. 'this.sm = 0;',
  33774. 'this.lw = 0;',
  33775. 'this.li = 0;',
  33776. '']),
  33777. LinesToStr([
  33778. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33779. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33780. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33781. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33782. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33783. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33784. '']));
  33785. end;
  33786. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33787. begin
  33788. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33789. StartProgram(false);
  33790. Add([
  33791. '{$modeswitch typehelpers}',
  33792. '{$R+}',
  33793. 'type',
  33794. ' TObject = class',
  33795. ' FSize: byte;',
  33796. ' property Size: byte read FSize;',
  33797. ' end;',
  33798. ' THelper = type helper for byte',
  33799. ' procedure SetIt(w: word);',
  33800. ' end;',
  33801. 'procedure THelper.SetIt(w: word);',
  33802. 'begin',
  33803. ' Self:=w;',
  33804. 'end;',
  33805. 'function GetIt: byte;',
  33806. 'begin',
  33807. ' Result.SetIt(2);',
  33808. 'end;',
  33809. 'var',
  33810. ' b: byte = 3;',
  33811. ' o: TObject;',
  33812. 'begin',
  33813. ' b.SetIt(14);',
  33814. ' with b do SetIt(15);',
  33815. ' o.Size.SetIt(16);',
  33816. '']);
  33817. ConvertProgram;
  33818. CheckSource('TestRangeChecks_AssignInt',
  33819. LinesToStr([ // statements
  33820. 'rtl.createClass(this, "TObject", null, function () {',
  33821. ' this.$init = function () {',
  33822. ' this.FSize = 0;',
  33823. ' };',
  33824. ' this.$final = function () {',
  33825. ' };',
  33826. '});',
  33827. 'rtl.createHelper(this, "THelper", null, function () {',
  33828. ' this.SetIt = function (w) {',
  33829. ' rtl.rc(w, 0, 65535);',
  33830. ' this.set(w);',
  33831. ' };',
  33832. '});',
  33833. 'this.GetIt = function () {',
  33834. ' var Result = 0;',
  33835. ' $mod.THelper.SetIt.call({',
  33836. ' get: function () {',
  33837. ' return Result;',
  33838. ' },',
  33839. ' set: function (v) {',
  33840. ' rtl.rc(v, 0, 255);',
  33841. ' Result = v;',
  33842. ' }',
  33843. ' }, 2);',
  33844. ' return Result;',
  33845. '};',
  33846. 'this.b = 3;',
  33847. 'this.o = null;',
  33848. '']),
  33849. LinesToStr([ // $mod.$main
  33850. '$mod.THelper.SetIt.call({',
  33851. ' p: $mod,',
  33852. ' get: function () {',
  33853. ' return this.p.b;',
  33854. ' },',
  33855. ' set: function (v) {',
  33856. ' rtl.rc(v, 0, 255);',
  33857. ' this.p.b = v;',
  33858. ' }',
  33859. '}, 14);',
  33860. 'var $with = $mod.b;',
  33861. '$mod.THelper.SetIt.call({',
  33862. ' get: function () {',
  33863. ' return $with;',
  33864. ' },',
  33865. ' set: function (v) {',
  33866. ' rtl.rc(v, 0, 255);',
  33867. ' $with = v;',
  33868. ' }',
  33869. '}, 15);',
  33870. '$mod.THelper.SetIt.call({',
  33871. ' p: $mod.o,',
  33872. ' get: function () {',
  33873. ' return this.p.FSize;',
  33874. ' },',
  33875. ' set: function (v) {',
  33876. ' rtl.rc(v, 0, 255);',
  33877. ' this.p.FSize = v;',
  33878. ' }',
  33879. '}, 16);',
  33880. '']));
  33881. end;
  33882. procedure TTestModule.TestAsync_Proc;
  33883. begin
  33884. StartProgram(false);
  33885. Add([
  33886. 'procedure Fly(w: word = 1); async; forward;',
  33887. 'procedure Run(w: word = 2); async;',
  33888. 'begin',
  33889. ' Fly(w);',
  33890. ' Fly;',
  33891. ' await(Fly(w));',
  33892. ' await(Fly);',
  33893. 'end;',
  33894. 'procedure Fly(w: word); ',
  33895. 'begin',
  33896. 'end;',
  33897. 'begin',
  33898. ' Run;',
  33899. ' Run(3);',
  33900. '']);
  33901. CheckResolverUnexpectedHints();
  33902. ConvertProgram;
  33903. CheckSource('TestAsync_Proc',
  33904. LinesToStr([ // statements
  33905. 'this.Run = async function (w) {',
  33906. ' $mod.Fly(w);',
  33907. ' $mod.Fly(1);',
  33908. ' await $mod.Fly(w);',
  33909. ' await $mod.Fly(1);',
  33910. '};',
  33911. 'this.Fly = async function (w) {',
  33912. '};',
  33913. '']),
  33914. LinesToStr([
  33915. '$mod.Run(2);',
  33916. '$mod.Run(3);',
  33917. '']));
  33918. end;
  33919. procedure TTestModule.TestAsync_CallResultIsPromise;
  33920. begin
  33921. StartProgram(false);
  33922. Add([
  33923. '{$modeswitch externalclass}',
  33924. 'type',
  33925. ' TObject = class',
  33926. ' end;',
  33927. ' TJSPromise = class external name ''Promise''',
  33928. ' end;',
  33929. ' TBird = class',
  33930. ' function Fly: word; async; ',
  33931. ' end;',
  33932. 'function TBird.Fly: word; async; ',
  33933. 'begin',
  33934. ' Result:=3;',
  33935. ' Fly:=4+Result;',
  33936. ' if Result=5 then ;',
  33937. ' exit(6);',
  33938. 'end;',
  33939. 'function Run: word; async;',
  33940. 'begin',
  33941. ' Result:=11+Result;',
  33942. ' inc(Result);',
  33943. 'end;',
  33944. 'var',
  33945. ' p: TJSPromise;',
  33946. ' o: TBird;',
  33947. 'begin',
  33948. ' p:=Run;',
  33949. ' p:=Run();',
  33950. ' if Run=p then ;',
  33951. ' if p=Run then ;',
  33952. ' if Run()=p then ;',
  33953. ' if p=Run() then ;',
  33954. ' p:=o.Fly;',
  33955. ' p:=o.Fly();',
  33956. ' if o.Fly=p then ;',
  33957. ' if o.Fly()=p then ;',
  33958. ' with o do begin',
  33959. ' p:=Fly;',
  33960. ' p:=Fly();',
  33961. ' if Fly=p then ;',
  33962. ' if Fly()=p then ;',
  33963. ' end;',
  33964. '']);
  33965. CheckResolverUnexpectedHints();
  33966. ConvertProgram;
  33967. CheckSource('TestAsync_CallResultIsPromise',
  33968. LinesToStr([ // statements
  33969. 'rtl.createClass(this, "TObject", null, function () {',
  33970. ' this.$init = function () {',
  33971. ' };',
  33972. ' this.$final = function () {',
  33973. ' };',
  33974. '});',
  33975. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33976. ' this.Fly = async function () {',
  33977. ' var Result = 0;',
  33978. ' Result = 3;',
  33979. ' Result = 4 + Result;',
  33980. ' if (Result === 5) ;',
  33981. ' return 6;',
  33982. ' return Result;',
  33983. ' };',
  33984. '});',
  33985. 'this.Run = async function () {',
  33986. ' var Result = 0;',
  33987. ' Result = 11 + Result;',
  33988. ' Result += 1;',
  33989. ' return Result;',
  33990. '};',
  33991. 'this.p = null;',
  33992. 'this.o = null;',
  33993. '']),
  33994. LinesToStr([
  33995. '$mod.p = $mod.Run();',
  33996. '$mod.p = $mod.Run();',
  33997. 'if ($mod.Run() === $mod.p) ;',
  33998. 'if ($mod.p === $mod.Run()) ;',
  33999. 'if ($mod.Run() === $mod.p) ;',
  34000. 'if ($mod.p === $mod.Run()) ;',
  34001. '$mod.p = $mod.o.Fly();',
  34002. '$mod.p = $mod.o.Fly();',
  34003. 'if ($mod.o.Fly() === $mod.p) ;',
  34004. 'if ($mod.o.Fly() === $mod.p) ;',
  34005. 'var $with = $mod.o;',
  34006. '$mod.p = $with.Fly();',
  34007. '$mod.p = $with.Fly();',
  34008. 'if ($with.Fly() === $mod.p) ;',
  34009. 'if ($with.Fly() === $mod.p) ;',
  34010. '']));
  34011. end;
  34012. procedure TTestModule.TestAsync_ConstructorFail;
  34013. begin
  34014. StartProgram(false);
  34015. Add([
  34016. 'type',
  34017. ' TObject = class',
  34018. ' end;',
  34019. ' TBird = class',
  34020. ' constructor Create; async;',
  34021. ' end;',
  34022. 'constructor TBird.Create; async;',
  34023. 'begin',
  34024. 'end;',
  34025. 'begin',
  34026. '']);
  34027. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  34028. ConvertProgram;
  34029. end;
  34030. procedure TTestModule.TestAsync_PropertyGetterFail;
  34031. begin
  34032. StartProgram(false);
  34033. Add([
  34034. 'type',
  34035. ' TObject = class',
  34036. ' end;',
  34037. ' TBird = class',
  34038. ' function GetSize: word; async;',
  34039. ' property Size: word read GetSize;',
  34040. ' end;',
  34041. 'function TBird.GetSize: word; async;',
  34042. 'begin',
  34043. 'end;',
  34044. 'begin',
  34045. '']);
  34046. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  34047. ConvertProgram;
  34048. end;
  34049. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  34050. begin
  34051. StartProgram(false);
  34052. Add([
  34053. 'procedure Run; async;',
  34054. 'begin',
  34055. ' await(word,1);',
  34056. 'end;',
  34057. 'begin',
  34058. '']);
  34059. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  34060. ConvertProgram;
  34061. end;
  34062. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  34063. begin
  34064. StartProgram(false);
  34065. Add([
  34066. 'type',
  34067. ' TObject = class',
  34068. ' end;',
  34069. ' TBird = class',
  34070. ' end;',
  34071. 'function Fly: TObject; async;',
  34072. 'begin',
  34073. 'end;',
  34074. 'procedure Run; async;',
  34075. 'begin',
  34076. ' await(TBird,Fly);',
  34077. 'end;',
  34078. 'begin',
  34079. '']);
  34080. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  34081. ConvertProgram;
  34082. end;
  34083. procedure TTestModule.TestAWait_OutsideAsyncFail;
  34084. begin
  34085. StartProgram(false);
  34086. Add([
  34087. 'procedure Crawl(w: double); ',
  34088. 'begin',
  34089. 'end;',
  34090. 'procedure Run(w: double);',
  34091. 'begin',
  34092. ' await(Crawl(w));',
  34093. 'end;',
  34094. 'begin',
  34095. ' Run(1);']);
  34096. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  34097. ConvertProgram;
  34098. end;
  34099. procedure TTestModule.TestAWait_IntegerFail;
  34100. begin
  34101. StartProgram(false);
  34102. Add([
  34103. 'function Run: word;',
  34104. 'begin',
  34105. 'end;',
  34106. 'procedure Fly(w: word); async;',
  34107. 'begin',
  34108. ' await(Run());',
  34109. 'end;',
  34110. 'begin',
  34111. ' Fly(1);']);
  34112. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  34113. ConvertProgram;
  34114. end;
  34115. procedure TTestModule.TestAWait_ExternalClassPromise;
  34116. begin
  34117. StartProgram(false);
  34118. Add([
  34119. '{$modeswitch externalclass}',
  34120. 'type',
  34121. ' TJSPromise = class external name ''Promise''',
  34122. ' end;',
  34123. ' TJSThenable = class external name ''Thenable''',
  34124. ' end;',
  34125. 'function Fly(w: word): TJSPromise;',
  34126. 'begin',
  34127. 'end;',
  34128. 'function Jump(w: word): word; async;',
  34129. 'begin',
  34130. 'end;',
  34131. 'function Eat(w: word): TJSPromise; async;',
  34132. 'begin',
  34133. 'end;',
  34134. 'function Run(d: double): word; async;',
  34135. 'var',
  34136. ' p: TJSPromise;',
  34137. 'begin',
  34138. ' Result:=await(word,p);', // promise needs type
  34139. ' Result:=await(word,Fly(3));', // promise needs type
  34140. ' Result:=await(Jump(4));', // async non promise must omit the type
  34141. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  34142. ' Result:=await(word,Eat(6));', // promise needs type
  34143. 'end;',
  34144. 'begin',
  34145. '']);
  34146. ConvertProgram;
  34147. CheckSource('TestAWait_ExternalClassPromise',
  34148. LinesToStr([ // statements
  34149. 'this.Fly = function (w) {',
  34150. ' var Result = null;',
  34151. ' return Result;',
  34152. '};',
  34153. 'this.Jump = async function (w) {',
  34154. ' var Result = 0;',
  34155. ' return Result;',
  34156. '};',
  34157. 'this.Eat = async function (w) {',
  34158. ' var Result = null;',
  34159. ' return Result;',
  34160. '};',
  34161. 'this.Run = async function (d) {',
  34162. ' var Result = 0;',
  34163. ' var p = null;',
  34164. ' Result = await p;',
  34165. ' Result = await $mod.Fly(3);',
  34166. ' Result = await $mod.Jump(4);',
  34167. ' Result = await $mod.Jump(5);',
  34168. ' Result = await $mod.Eat(6);',
  34169. ' return Result;',
  34170. '};',
  34171. '']),
  34172. LinesToStr([
  34173. ]));
  34174. CheckResolverUnexpectedHints();
  34175. end;
  34176. procedure TTestModule.TestAWait_JSValue;
  34177. begin
  34178. StartProgram(false);
  34179. Add([
  34180. '{$modeswitch externalclass}',
  34181. 'type',
  34182. ' TJSPromise = class external name ''Promise''',
  34183. ' end;',
  34184. 'function Fly(w: word): jsvalue; async;',
  34185. 'begin',
  34186. 'end;',
  34187. 'function Run(d: jsvalue; var e): word; async;',
  34188. 'begin',
  34189. ' Result:=await(word,d);', // promise needs type
  34190. ' d:=await(Fly(4));', // async non promise must omit the type
  34191. ' Result:=await(word,e);', // promise needs type
  34192. 'end;',
  34193. 'begin',
  34194. '']);
  34195. ConvertProgram;
  34196. CheckSource('TestAWait_JSValue',
  34197. LinesToStr([ // statements
  34198. 'this.Fly = async function (w) {',
  34199. ' var Result = undefined;',
  34200. ' return Result;',
  34201. '};',
  34202. 'this.Run = async function (d, e) {',
  34203. ' var Result = 0;',
  34204. ' Result = await d;',
  34205. ' d = await $mod.Fly(4);',
  34206. ' Result = await e.get();',
  34207. ' return Result;',
  34208. '};',
  34209. '']),
  34210. LinesToStr([
  34211. ]));
  34212. CheckResolverUnexpectedHints();
  34213. end;
  34214. procedure TTestModule.TestAWait_Result;
  34215. begin
  34216. StartProgram(false);
  34217. Add([
  34218. '{$modeswitch externalclass}',
  34219. 'type',
  34220. ' TJSPromise = class external name ''Promise''',
  34221. ' end;',
  34222. 'function Crawl(d: double = 1.3): TJSPromise; ',
  34223. 'begin',
  34224. 'end;',
  34225. 'function Run(d: double = 1.6): word; async;',
  34226. 'begin',
  34227. ' Result:=await(word,Crawl);',
  34228. ' Result:=await(word,Crawl(4.5));',
  34229. ' Result:=await(Run);',
  34230. ' Result:=await(Run(6.7));',
  34231. 'end;',
  34232. 'begin',
  34233. ' Run(1);']);
  34234. ConvertProgram;
  34235. CheckSource('TestAWait_Result',
  34236. LinesToStr([ // statements
  34237. 'this.Crawl = function (d) {',
  34238. ' var Result = null;',
  34239. ' return Result;',
  34240. '};',
  34241. 'this.Run = async function (d) {',
  34242. ' var Result = 0;',
  34243. ' Result = await $mod.Crawl(1.3);',
  34244. ' Result = await $mod.Crawl(4.5);',
  34245. ' Result = await $mod.Run(1.6);',
  34246. ' Result = await $mod.Run(6.7);',
  34247. ' return Result;',
  34248. '};',
  34249. '']),
  34250. LinesToStr([
  34251. '$mod.Run(1);'
  34252. ]));
  34253. CheckResolverUnexpectedHints();
  34254. end;
  34255. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  34256. begin
  34257. StartProgram(false);
  34258. Add([
  34259. '{$mode objfpc}',
  34260. '{$modeswitch externalclass}',
  34261. 'type',
  34262. ' TJSPromise = class external name ''Promise''',
  34263. ' end;',
  34264. 'function Run: TJSPromise; async;',
  34265. 'begin',
  34266. 'end;',
  34267. 'procedure Fly(w: word); async;',
  34268. 'begin',
  34269. ' await(Run());',
  34270. 'end;',
  34271. 'begin',
  34272. ' Fly(1);']);
  34273. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  34274. nWrongNumberOfParametersForCallTo);
  34275. ConvertProgram;
  34276. end;
  34277. procedure TTestModule.TestAsync_AnonymousProc;
  34278. begin
  34279. StartProgram(false);
  34280. Add([
  34281. '{$mode objfpc}',
  34282. '{$modeswitch externalclass}',
  34283. 'type',
  34284. ' TJSPromise = class external name ''Promise''',
  34285. ' end;',
  34286. 'type',
  34287. ' TFunc = reference to function(x: double): word; async;',
  34288. 'function Crawl(d: double = 1.3): word; async;',
  34289. 'begin',
  34290. 'end;',
  34291. 'var Func: TFunc;',
  34292. 'begin',
  34293. ' Func:=function(c:double):word async begin',
  34294. ' Result:=await(Crawl(c));',
  34295. ' end;',
  34296. ' Func:=function(c:double):word async assembler asm',
  34297. ' end;',
  34298. '']);
  34299. ConvertProgram;
  34300. CheckSource('TestAsync_AnonymousProc',
  34301. LinesToStr([ // statements
  34302. 'this.Crawl = async function (d) {',
  34303. ' var Result = 0;',
  34304. ' return Result;',
  34305. '};',
  34306. 'this.Func = null;',
  34307. '']),
  34308. LinesToStr([
  34309. '$mod.Func = async function (c) {',
  34310. ' var Result = 0;',
  34311. ' Result = await $mod.Crawl(c);',
  34312. ' return Result;',
  34313. '};',
  34314. '$mod.Func = async function (c) {',
  34315. '};',
  34316. '']));
  34317. CheckResolverUnexpectedHints();
  34318. end;
  34319. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34320. begin
  34321. StartProgram(false);
  34322. Add([
  34323. '{$mode objfpc}',
  34324. '{$modeswitch externalclass}',
  34325. 'type',
  34326. ' TJSPromise = class external name ''Promise''',
  34327. ' end;',
  34328. ' TObject = class',
  34329. ' public',
  34330. ' procedure Fly(Prom: TJSPromise);',
  34331. ' end;',
  34332. ' TFunc = reference to procedure(Bird: TObject);',
  34333. 'procedure TObject.Fly(Prom: TJSPromise);',
  34334. 'begin',
  34335. 'end;',
  34336. 'function Crawl: jsvalue; async;',
  34337. 'begin',
  34338. 'end;',
  34339. 'procedure Add(Func: TFunc);',
  34340. 'begin',
  34341. 'end;',
  34342. 'begin',
  34343. ' Add(procedure(Bird: TObject)',
  34344. ' begin',
  34345. ' Bird.Fly(Crawl());',
  34346. ' end);',
  34347. '']);
  34348. ConvertProgram;
  34349. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34350. LinesToStr([ // statements
  34351. 'rtl.createClass(this, "TObject", null, function () {',
  34352. ' this.$init = function () {',
  34353. ' };',
  34354. ' this.$final = function () {',
  34355. ' };',
  34356. ' this.Fly = function (Prom) {',
  34357. ' };',
  34358. '});',
  34359. 'this.Crawl = async function () {',
  34360. ' var Result = undefined;',
  34361. ' return Result;',
  34362. '};',
  34363. 'this.Add = function (Func) {',
  34364. '};',
  34365. '']),
  34366. LinesToStr([
  34367. '$mod.Add(function (Bird) {',
  34368. ' Bird.Fly($mod.Crawl());',
  34369. '});',
  34370. '']));
  34371. end;
  34372. procedure TTestModule.TestAsync_ProcType;
  34373. begin
  34374. StartProgram(false);
  34375. Add([
  34376. '{$mode objfpc}',
  34377. 'type',
  34378. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34379. ' TFunc = function(x: double = 1.1): word; async;',
  34380. ' TProc = procedure(x: longint = 7); async;',
  34381. 'function Crawl(d: double): word; async;',
  34382. 'begin',
  34383. 'end;',
  34384. 'procedure Run(e:longint); async;',
  34385. 'begin',
  34386. 'end;',
  34387. 'procedure Fly(p: TProc); async;',
  34388. 'begin',
  34389. ' await(p);',
  34390. ' await(p());',
  34391. 'end;',
  34392. 'var',
  34393. ' RefFunc: TRefFunc;',
  34394. ' Func: TFunc;',
  34395. ' Proc, ProcB: TProc;',
  34396. 'begin',
  34397. ' Func:=@Crawl;',
  34398. ' RefFunc:=@Crawl;',
  34399. ' RefFunc:=function(c:double):word async begin',
  34400. ' Result:=await(RefFunc);',
  34401. ' Result:=await(RefFunc());',
  34402. ' Result:=await(Func);',
  34403. ' Result:=await(Func());',
  34404. ' await(Proc);',
  34405. ' await(Proc());',
  34406. ' await(Proc(13));',
  34407. ' end;',
  34408. ' Proc:=@Run;',
  34409. ' if Proc=ProcB then ;',
  34410. ' ']);
  34411. ConvertProgram;
  34412. CheckResolverUnexpectedHints();
  34413. CheckSource('TestAsync_ProcType',
  34414. LinesToStr([ // statements
  34415. 'this.Crawl = async function (d) {',
  34416. ' var Result = 0;',
  34417. ' return Result;',
  34418. '};',
  34419. 'this.Run = async function (e) {',
  34420. '};',
  34421. 'this.Fly = async function (p) {',
  34422. ' await p(7);',
  34423. ' await p(7);',
  34424. '};',
  34425. 'this.RefFunc = null;',
  34426. 'this.Func = null;',
  34427. 'this.Proc = null;',
  34428. 'this.ProcB = null;',
  34429. '']),
  34430. LinesToStr([
  34431. '$mod.Func = $mod.Crawl;',
  34432. '$mod.RefFunc = $mod.Crawl;',
  34433. '$mod.RefFunc = async function (c) {',
  34434. ' var Result = 0;',
  34435. ' Result = await $mod.RefFunc(1.3);',
  34436. ' Result = await $mod.RefFunc(1.3);',
  34437. ' Result = await $mod.Func(1.1);',
  34438. ' Result = await $mod.Func(1.1);',
  34439. ' await $mod.Proc(7);',
  34440. ' await $mod.Proc(7);',
  34441. ' await $mod.Proc(13);',
  34442. ' return Result;',
  34443. '};',
  34444. '$mod.Proc = $mod.Run;',
  34445. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34446. '']));
  34447. end;
  34448. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34449. begin
  34450. StartProgram(false);
  34451. Add([
  34452. '{$mode objfpc}',
  34453. 'type',
  34454. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34455. 'function Crawl(d: double): word; async;',
  34456. 'begin',
  34457. 'end;',
  34458. 'var',
  34459. ' RefFunc: TRefFunc;',
  34460. 'begin',
  34461. ' RefFunc:=@Crawl;',
  34462. ' ']);
  34463. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34464. ConvertProgram;
  34465. end;
  34466. procedure TTestModule.TestAsync_Inherited;
  34467. begin
  34468. StartProgram(false);
  34469. Add([
  34470. '{$mode objfpc}',
  34471. '{$modeswitch externalclass}',
  34472. 'type',
  34473. ' TJSPromise = class external name ''Promise''',
  34474. ' end;',
  34475. ' TObject = class',
  34476. ' function Run(w: word = 3): word; async; virtual;',
  34477. ' end;',
  34478. ' TBird = class',
  34479. ' function Run(w: word = 3): word; async; override;',
  34480. ' end;',
  34481. 'function TObject.Run(w: word = 3): word; async;',
  34482. 'begin',
  34483. 'end;',
  34484. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34485. 'var p: TJSPromise;',
  34486. 'begin',
  34487. ' p:=inherited;',
  34488. ' p:=inherited Run;',
  34489. ' p:=inherited Run();',
  34490. ' p:=inherited Run(4);',
  34491. ' exit(p);',
  34492. ' exit(inherited);',
  34493. ' exit(inherited Run);',
  34494. ' exit(inherited Run(5));',
  34495. ' exit(6);',
  34496. 'end;',
  34497. 'begin',
  34498. ' ']);
  34499. ConvertProgram;
  34500. CheckSource('TestAsync_Inherited',
  34501. LinesToStr([ // statements
  34502. 'rtl.createClass(this, "TObject", null, function () {',
  34503. ' this.$init = function () {',
  34504. ' };',
  34505. ' this.$final = function () {',
  34506. ' };',
  34507. ' this.Run = async function (w) {',
  34508. ' var Result = 0;',
  34509. ' return Result;',
  34510. ' };',
  34511. '});',
  34512. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34513. ' this.Run = async function (w) {',
  34514. ' var Result = 0;',
  34515. ' var p = null;',
  34516. ' p = $mod.TObject.Run.apply(this, arguments);',
  34517. ' p = $mod.TObject.Run.call(this, 3);',
  34518. ' p = $mod.TObject.Run.call(this, 3);',
  34519. ' p = $mod.TObject.Run.call(this, 4);',
  34520. ' return p;',
  34521. ' return $mod.TObject.Run.apply(this, arguments);',
  34522. ' return $mod.TObject.Run.call(this, 3);',
  34523. ' return $mod.TObject.Run.call(this, 5);',
  34524. ' return 6;',
  34525. ' return Result;',
  34526. ' };',
  34527. '});',
  34528. '']),
  34529. LinesToStr([
  34530. '']));
  34531. CheckResolverUnexpectedHints();
  34532. end;
  34533. procedure TTestModule.TestAsync_ClassInterface;
  34534. begin
  34535. StartProgram(false);
  34536. Add([
  34537. '{$mode objfpc}',
  34538. '{$modeswitch externalclass}',
  34539. 'type',
  34540. ' TJSPromise = class external name ''Promise''',
  34541. ' end;',
  34542. ' IUnknown = interface',
  34543. ' function _AddRef: longint;',
  34544. ' function _Release: longint;',
  34545. ' end;',
  34546. 'function Say(i: IUnknown): IUnknown; async;',
  34547. 'begin',
  34548. 'end;',
  34549. 'function Run: IUnknown; async;',
  34550. 'begin',
  34551. ' Result:=await(Run);',
  34552. ' Result:=await(Run());',
  34553. ' Result:=await(Run) as IUnknown;',
  34554. ' Result:=await(Say(nil));',
  34555. ' Result:=await(Say(await(Run())));',
  34556. ' Result:=await(Say(await(Run()) as IUnknown));',
  34557. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  34558. 'end;',
  34559. 'procedure Fly;',
  34560. 'var p: TJSPromise;',
  34561. 'begin',
  34562. ' Run;',
  34563. ' Run();',
  34564. ' p:=Run;',
  34565. ' p:=Run();',
  34566. 'end;',
  34567. 'begin',
  34568. ' ']);
  34569. ConvertProgram;
  34570. CheckSource('TestAsync_ClassInterface',
  34571. LinesToStr([ // statements
  34572. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  34573. 'this.Say = async function (i) {',
  34574. ' var Result = null;',
  34575. ' return Result;',
  34576. '};',
  34577. 'this.Run = async function () {',
  34578. ' var Result = null;',
  34579. ' var $ok = false;',
  34580. ' try {',
  34581. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34582. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34583. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  34584. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  34585. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  34586. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  34587. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  34588. ' $ok = true;',
  34589. ' } finally {',
  34590. ' if (!$ok) rtl._Release(Result);',
  34591. ' };',
  34592. ' return Result;',
  34593. '};',
  34594. 'this.Fly = function () {',
  34595. ' var p = null;',
  34596. ' $mod.Run();',
  34597. ' $mod.Run();',
  34598. ' p = $mod.Run();',
  34599. ' p = $mod.Run();',
  34600. '};',
  34601. '']),
  34602. LinesToStr([
  34603. '']));
  34604. CheckResolverUnexpectedHints();
  34605. end;
  34606. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  34607. begin
  34608. StartProgram(true,[supTInterfacedObject]);
  34609. Add([
  34610. '{$mode objfpc}',
  34611. '{$modeswitch externalclass}',
  34612. 'type',
  34613. ' TJSPromise = class external name ''Promise''',
  34614. ' end;',
  34615. ' IBird = interface',
  34616. ' procedure Run;',
  34617. ' end;',
  34618. ' TBird = class(TInterfacedObject,IBird)',
  34619. ' procedure Run; async;',
  34620. ' end;',
  34621. 'procedure TBird.Run;',
  34622. 'begin',
  34623. 'end;',
  34624. 'begin',
  34625. ' ']);
  34626. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34627. ConvertProgram;
  34628. end;
  34629. procedure TTestModule.TestAWait_ClassAs;
  34630. begin
  34631. StartProgram(false);
  34632. Add([
  34633. '{$mode objfpc}',
  34634. '{$modeswitch externalclass}',
  34635. 'type',
  34636. ' TJSPromise = class external name ''Promise''',
  34637. ' end;',
  34638. ' TObject = class',
  34639. ' function Run: TObject; async;',
  34640. ' end;',
  34641. ' TBird = class',
  34642. ' function Fly: TBird; async;',
  34643. ' end;',
  34644. 'function TObject.Run: TObject; async;',
  34645. 'begin',
  34646. 'end;',
  34647. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  34648. 'var o: TObject;',
  34649. 'begin',
  34650. ' o:=await(TObject,Run);',
  34651. ' o:=await(TObject,Fly);',
  34652. ' o:=await(TBird,Fly);',
  34653. ' o:=await(TObject,inherited Run);',
  34654. ' o:=await(TObject,inherited Run) as TBird;',
  34655. 'end;',
  34656. 'begin',
  34657. ' ']);
  34658. ConvertProgram;
  34659. CheckSource('TestAWait_ClassAs',
  34660. LinesToStr([ // statements
  34661. 'rtl.createClass(this, "TObject", null, function () {',
  34662. ' this.$init = function () {',
  34663. ' };',
  34664. ' this.$final = function () {',
  34665. ' };',
  34666. ' this.Run = async function () {',
  34667. ' var Result = null;',
  34668. ' return Result;',
  34669. ' };',
  34670. '});',
  34671. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34672. ' this.Fly = async function () {',
  34673. ' var Result = null;',
  34674. ' var o = null;',
  34675. ' o = await this.Run();',
  34676. ' o = await this.Fly();',
  34677. ' o = await this.Fly();',
  34678. ' o = await $mod.TObject.Run.call(this);',
  34679. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  34680. ' return Result;',
  34681. ' };',
  34682. '});',
  34683. '']),
  34684. LinesToStr([
  34685. '']));
  34686. CheckResolverUnexpectedHints();
  34687. end;
  34688. procedure TTestModule.TestLibrary_Empty;
  34689. begin
  34690. StartLibrary(false);
  34691. Add([
  34692. '']);
  34693. ConvertLibrary;
  34694. CheckFullSource('TestLibrary_Empty',
  34695. LinesToStr([ // statements
  34696. 'rtl.module("library", [], function () {',
  34697. ' var $mod = this;',
  34698. ' $mod.$main = function () {',
  34699. ' };',
  34700. '});',
  34701. 'rtl.run("library");',
  34702. '']));
  34703. CheckResolverUnexpectedHints();
  34704. end;
  34705. procedure TTestModule.TestLibrary_ExportFunc;
  34706. begin
  34707. StartLibrary(false);
  34708. Add([
  34709. 'procedure Run(w: word);',
  34710. 'begin',
  34711. 'end;',
  34712. 'exports',
  34713. ' Run;',
  34714. ' run name ''Foo'';',
  34715. ' test1.run name ''Test1Run'';',
  34716. '']);
  34717. ConvertLibrary;
  34718. CheckFullSource('TestLibrary_ExportFunc',
  34719. LinesToStr([ // statements
  34720. 'rtl.module("library", [], function () {',
  34721. ' var $mod = this;',
  34722. ' this.Run = function (w) {',
  34723. ' };',
  34724. ' $mod.$main = function () {',
  34725. ' };',
  34726. '});',
  34727. 'rtl.run("library");',
  34728. 'export const Run = pas.library.Run;',
  34729. 'export const Foo = pas.library.Run;',
  34730. 'export const Test1Run = pas.library.Run;',
  34731. '']));
  34732. CheckResolverUnexpectedHints();
  34733. end;
  34734. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  34735. begin
  34736. StartLibrary(false);
  34737. Add([
  34738. 'procedure Run(w: word); overload;',
  34739. 'begin',
  34740. 'end;',
  34741. 'procedure Run(s: string); overload;',
  34742. 'begin',
  34743. 'end;',
  34744. 'exports',
  34745. ' Run;',
  34746. '']);
  34747. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  34748. nCantDetermineWhichOverloadedFunctionToCall);
  34749. ConvertLibrary;
  34750. end;
  34751. procedure TTestModule.TestLibrary_Export_Index_Fail;
  34752. begin
  34753. StartLibrary(false);
  34754. Add([
  34755. 'procedure Run(w: word);',
  34756. 'begin',
  34757. 'end;',
  34758. 'exports',
  34759. ' Run index 3;',
  34760. '']);
  34761. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  34762. ConvertLibrary;
  34763. end;
  34764. procedure TTestModule.TestLibrary_ExportVar;
  34765. begin
  34766. StartLibrary(false);
  34767. Add([
  34768. 'var Wing: word;',
  34769. 'exports',
  34770. ' Wing, wing name ''BirdArm'';',
  34771. '']);
  34772. ConvertLibrary;
  34773. CheckFullSource('TestLibrary_ExportVar',
  34774. LinesToStr([ // statements
  34775. 'rtl.module("library", [], function () {',
  34776. ' var $mod = this;',
  34777. ' this.Wing = 0;',
  34778. ' $mod.$main = function () {',
  34779. ' };',
  34780. '});',
  34781. 'rtl.run("library");',
  34782. 'export const vars = {};',
  34783. 'Object.defineProperties(vars, {',
  34784. ' Wing: {',
  34785. ' enumerable: true,',
  34786. ' get: function () {',
  34787. ' return pas.library.Wing;',
  34788. ' },',
  34789. ' set: function (v) {',
  34790. ' pas.library.Wing = v;',
  34791. ' }',
  34792. ' },',
  34793. ' BirdArm: {',
  34794. ' enumerable: true,',
  34795. ' get: function () {',
  34796. ' return pas.library.Wing;',
  34797. ' },',
  34798. ' set: function (v) {',
  34799. ' pas.library.Wing = v;',
  34800. ' }',
  34801. ' }',
  34802. '});',
  34803. '']));
  34804. CheckResolverUnexpectedHints();
  34805. end;
  34806. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34807. begin
  34808. AddModuleWithIntfImplSrc('Unit1.pas',
  34809. LinesToStr([
  34810. 'type',
  34811. ' TAnt = class',
  34812. ' class function Crawl: word; static;',
  34813. ' end;',
  34814. 'function Fly: word;',
  34815. '']),
  34816. LinesToStr([
  34817. 'function Fly: word;',
  34818. 'begin',
  34819. 'end;',
  34820. 'class function TAnt.Crawl: word;',
  34821. 'begin',
  34822. 'end;',
  34823. '']));
  34824. StartLibrary(true,[supTObject]);
  34825. Add([
  34826. 'uses unit1;',
  34827. 'exports',
  34828. ' Fly;',
  34829. ' TAnt.Crawl;',
  34830. '']);
  34831. ConvertLibrary;
  34832. CheckFullSource('TestLibrary_ExportUnitFunc',
  34833. LinesToStr([ // statements
  34834. 'rtl.module("library", ["system", "Unit1"], function () {',
  34835. ' var $mod = this;',
  34836. ' $mod.$main = function () {',
  34837. ' };',
  34838. '});',
  34839. 'rtl.run("library");',
  34840. 'export const Fly = pas.Unit1.Fly;',
  34841. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  34842. '']));
  34843. CheckResolverUnexpectedHints();
  34844. end;
  34845. Initialization
  34846. RegisterTests([TTestModule]);
  34847. end.