tcmodules.pas 943 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval, FPPas2Js;
  22. const
  23. // default parser+scanner options
  24. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  25. co_tcmodules = [];
  26. type
  27. TSrcMarkerKind = (
  28. mkLabel,
  29. mkResolverReference,
  30. mkDirectReference
  31. );
  32. const
  33. SrcMarker: array[TSrcMarkerKind] of char = (
  34. '#', // mkLabel
  35. '@', // mkResolverReference
  36. '=' // mkDirectReference
  37. );
  38. type
  39. PSrcMarker = ^TSrcMarker;
  40. TSrcMarker = record
  41. Kind: TSrcMarkerKind;
  42. Filename: string;
  43. Row: integer;
  44. StartCol, EndCol: integer; // token start, end column
  45. Identifier: string;
  46. Next: PSrcMarker;
  47. end;
  48. TSystemUnitPart = (
  49. supTObject,
  50. supTVarRec,
  51. supTypeInfo,
  52. supTInterfacedObject,
  53. supWriteln
  54. );
  55. TSystemUnitParts = set of TSystemUnitPart;
  56. { TTestHintMessage }
  57. TTestHintMessage = class
  58. public
  59. Id: int64;
  60. MsgType: TMessageType;
  61. MsgNumber: integer;
  62. Msg: string;
  63. SourcePos: TPasSourcePos;
  64. end;
  65. TTestResolverReferenceData = record
  66. Filename: string;
  67. Row: integer;
  68. StartCol: integer;
  69. EndCol: integer;
  70. Found: TFPList; // list of TPasElement at this token
  71. end;
  72. PTestResolverReferenceData = ^TTestResolverReferenceData;
  73. { TTestPasParser }
  74. TTestPasParser = Class(TPasParser)
  75. end;
  76. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  77. { TTestEnginePasResolver }
  78. TTestEnginePasResolver = class(TPas2JsResolver)
  79. private
  80. FFilename: string;
  81. FModule: TPasModule;
  82. FOnFindUnit: TOnFindUnit;
  83. FParser: TTestPasParser;
  84. FStreamResolver: TStreamResolver;
  85. FScanner: TPas2jsPasScanner;
  86. FSource: string;
  87. procedure SetModule(const AValue: TPasModule);
  88. public
  89. destructor Destroy; override;
  90. function CreateElement(AClass: TPTreeElement; const AName: String;
  91. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  92. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  93. overload; override;
  94. function FindUnit(const AName, InFilename: String; NameExpr,
  95. InFileExpr: TPasExpr): TPasModule; override;
  96. procedure UsedInterfacesFinished(Section: TPasSection); override;
  97. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  98. property Filename: string read FFilename write FFilename;
  99. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  100. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  101. property Parser: TTestPasParser read FParser write FParser;
  102. property Source: string read FSource write FSource;
  103. property Module: TPasModule read FModule write SetModule;
  104. end;
  105. { TCustomTestModule }
  106. TCustomTestModule = Class(TTestCase)
  107. private
  108. FWithTypeInfo: boolean;
  109. FSource: TStringList;
  110. FSkipTests: boolean;
  111. FScanner: TPas2jsPasScanner;
  112. FResolvers: TObjectList;// list of TTestEnginePasResolver
  113. FPasProgram: TPasProgram;
  114. FPasLibrary: TPasLibrary;
  115. FParser: TTestPasParser;
  116. FModule: TPasModule;
  117. FJSSource: TStringList;
  118. FJSRegModuleCall: TJSCallExpression;
  119. FJSModuleSrc: TJSSourceElements;
  120. FJSModuleCallArgs: TJSArguments;
  121. FJSModule: TJSSourceElements;
  122. FJSInterfaceUses: TJSArrayLiteral;
  123. FJSInitBody: TJSFunctionBody;
  124. FJSImplentationUses: TJSArrayLiteral;
  125. FJSImplementationUses: TJSArrayLiteral;
  126. FHub: TPas2JSResolverHub;
  127. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  128. FHintMsgs: TObjectList; // list of TTestHintMessage
  129. FFirstPasStatement: TPasImplBlock;
  130. FFileResolver: TStreamResolver;
  131. FFilename: string;
  132. FExpectedErrorNumber: integer;
  133. FExpectedErrorMsg: string;
  134. FExpectedErrorClass: ExceptClass;
  135. FEngine: TTestEnginePasResolver;
  136. FConverter: TPasToJSConverter;
  137. {$IFDEF EnablePasTreeGlobalRefCount}
  138. FElementRefCountAtSetup: int64;
  139. {$ENDIF}
  140. procedure FreeSrcMarkers;
  141. function GetResolverCount: integer;
  142. function GetResolvers(Index: integer): TTestEnginePasResolver;
  143. function GetMsgCount: integer;
  144. function GetMsgs(Index: integer): TTestHintMessage;
  145. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  146. procedure OnParserLog(Sender: TObject; const Msg: String);
  147. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  148. procedure OnScannerLog(Sender: TObject; const Msg: String);
  149. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  150. procedure OnFindReference(El: TPasElement; FindData: pointer);
  151. procedure SetWithTypeInfo(const AValue: boolean);
  152. protected
  153. procedure SetUp; override;
  154. function CreateConverter: TPasToJSConverter; virtual;
  155. function LoadUnit(const aUnitName: String): TPasModule;
  156. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  157. procedure TearDown; override;
  158. Procedure Add(Line: string); virtual;
  159. Procedure Add(const Lines: array of string);
  160. Procedure StartParsing; virtual;
  161. procedure ParseModuleQueue; virtual;
  162. procedure ParseModule; virtual;
  163. procedure ParseProgram; virtual;
  164. procedure ParseLibrary; virtual;
  165. procedure ParseUnit; virtual;
  166. protected
  167. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  168. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  169. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  172. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  173. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  174. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  175. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure ConvertModule; virtual;
  178. procedure ConvertProgram; virtual;
  179. procedure ConvertLibrary; virtual;
  180. procedure ConvertUnit; virtual;
  181. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  182. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  183. function GetDottedIdentifier(El: TJSElement): string;
  184. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  185. ImplStatements: string = ''); virtual;
  186. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  187. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  188. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  189. procedure CheckReferenceDirectives; virtual;
  190. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  191. Msg: string; Marker: PSrcMarker = nil); virtual;
  192. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  193. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  194. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  197. function IsErrorExpected(E: Exception): boolean;
  198. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  199. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  200. procedure HandleScannerError(E: EScannerError);
  201. procedure HandleParserError(E: EParserError);
  202. procedure HandlePasResolveError(E: EPasResolve);
  203. procedure HandlePas2JSError(E: EPas2JS);
  204. procedure HandleException(E: Exception);
  205. procedure FailException(E: Exception);
  206. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  207. function IndexOfResolver(const Filename: string): integer;
  208. function GetResolver(const Filename: string): TTestEnginePasResolver;
  209. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  210. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  211. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  212. function FindSrcLabel(const Identifier: string): PSrcMarker;
  213. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  214. function GetDefaultNamespace: string;
  215. property PasProgram: TPasProgram Read FPasProgram;
  216. property PasLibrary: TPasLibrary Read FPasLibrary;
  217. property ResolverEngine: TTestEnginePasResolver read FEngine;
  218. property Filename: string read FFilename;
  219. Property Module: TPasModule Read FModule;
  220. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  221. property Converter: TPasToJSConverter read FConverter;
  222. property JSSource: TStringList read FJSSource;
  223. property JSModule: TJSSourceElements read FJSModule;
  224. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  225. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  226. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  227. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  228. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  229. property JSInitBody: TJSFunctionBody read FJSInitBody;
  230. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  231. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  232. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  233. property SkipTests: boolean read FSkipTests write FSkipTests;
  234. public
  235. constructor Create; override;
  236. destructor Destroy; override;
  237. property Hub: TPas2JSResolverHub read FHub;
  238. property Source: TStringList read FSource;
  239. property FileResolver: TStreamResolver read FFileResolver;
  240. property Scanner: TPas2jsPasScanner read FScanner;
  241. property Parser: TTestPasParser read FParser;
  242. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  243. property ResolverCount: integer read GetResolverCount;
  244. property MsgCount: integer read GetMsgCount;
  245. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  246. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  247. end;
  248. { TTestModule }
  249. TTestModule = class(TCustomTestModule)
  250. Published
  251. Procedure TestReservedWords;
  252. // program, units, includes
  253. Procedure TestEmptyProgram;
  254. Procedure TestEmptyProgramUseStrict;
  255. Procedure TestEmptyUnit;
  256. Procedure TestEmptyUnitUseStrict;
  257. Procedure TestDottedUnitNames;
  258. Procedure TestDottedUnitNameImpl;
  259. Procedure TestDottedUnitExpr;
  260. Procedure Test_ModeFPCFail;
  261. Procedure Test_ModeSwitchCBlocksFail;
  262. Procedure TestUnit_UseSystem;
  263. Procedure TestUnit_Intf1Impl2Intf1;
  264. Procedure TestIncludeVersion;
  265. // vars/const
  266. Procedure TestVarInt;
  267. Procedure TestVarBaseTypes;
  268. Procedure TestBaseTypeSingleFail;
  269. Procedure TestBaseTypeExtendedFail;
  270. Procedure TestConstBaseTypes;
  271. Procedure TestUnitImplVars;
  272. Procedure TestUnitImplConsts;
  273. Procedure TestUnitImplRecord;
  274. Procedure TestRenameJSNameConflict;
  275. Procedure TestLocalConst;
  276. Procedure TestVarExternal;
  277. Procedure TestVarExternalOtherUnit;
  278. Procedure TestVarAbsoluteFail;
  279. Procedure TestConstExternal;
  280. // numbers
  281. Procedure TestDouble;
  282. Procedure TestDoubleSmall;
  283. Procedure TestInteger;
  284. Procedure TestIntegerRange;
  285. Procedure TestIntegerTypecasts;
  286. Procedure TestInteger_BitwiseShrNativeInt;
  287. Procedure TestInteger_BitwiseShlNativeInt;
  288. Procedure TestInteger_SystemFunc;
  289. Procedure TestInteger_AssignOutsideConst;
  290. Procedure TestCurrency;
  291. Procedure TestForBoolDo;
  292. Procedure TestForIntDo;
  293. Procedure TestForIntInDo;
  294. // strings
  295. Procedure TestCharConst;
  296. Procedure TestChar_Compare;
  297. Procedure TestChar_BuiltInProcs;
  298. Procedure TestStringConst;
  299. Procedure TestStringConst_InvalidUTF16;
  300. Procedure TestStringConstSurrogate;
  301. Procedure TestStringConst_Multiline;
  302. Procedure TestString_Length;
  303. Procedure TestString_Compare;
  304. Procedure TestString_SetLength;
  305. Procedure TestString_CharAt;
  306. Procedure TestStringHMinusFail;
  307. Procedure TestStr;
  308. Procedure TestBaseType_AnsiStringFail;
  309. Procedure TestBaseType_WideStringFail;
  310. Procedure TestBaseType_ShortStringFail;
  311. Procedure TestBaseType_RawByteStringFail;
  312. Procedure TestTypeShortstring_Fail;
  313. Procedure TestCharSet_Custom;
  314. Procedure TestWideChar;
  315. Procedure TestForCharDo;
  316. Procedure TestForCharInDo;
  317. // alias types
  318. Procedure TestAliasTypeRef;
  319. Procedure TestTypeCast_BaseTypes;
  320. Procedure TestTypeCast_AliasBaseTypes;
  321. // functions
  322. Procedure TestEmptyProc;
  323. Procedure TestProcOneParam;
  324. Procedure TestFunctionWithoutParams;
  325. Procedure TestProcedureWithoutParams;
  326. Procedure TestPrgProcVar;
  327. Procedure TestProcTwoArgs;
  328. Procedure TestProc_DefaultValue;
  329. Procedure TestUnitProcVar;
  330. Procedure TestImplProc;
  331. Procedure TestFunctionResult;
  332. Procedure TestNestedProc;
  333. Procedure TestNestedProc_ResultString;
  334. Procedure TestForwardProc;
  335. Procedure TestNestedForwardProc;
  336. Procedure TestAssignFunctionResult;
  337. Procedure TestFunctionResultInCondition;
  338. Procedure TestFunctionResultInForLoop;
  339. Procedure TestFunctionResultInTypeCast;
  340. Procedure TestExit;
  341. Procedure TestExit_ResultInFinally;
  342. Procedure TestBreak;
  343. Procedure TestBreakAsVar;
  344. Procedure TestContinue;
  345. Procedure TestProc_External;
  346. Procedure TestProc_ExternalOtherUnit;
  347. Procedure TestProc_Asm;
  348. Procedure TestProc_AsmSubBlock;
  349. Procedure TestProc_Assembler;
  350. Procedure TestProc_VarParam;
  351. Procedure TestProc_VarParamString;
  352. Procedure TestProc_VarParamV;
  353. Procedure TestProc_Overload;
  354. Procedure TestProc_OverloadForward;
  355. Procedure TestProc_OverloadIntfImpl;
  356. Procedure TestProc_OverloadNested;
  357. Procedure TestProc_OverloadNestedForward;
  358. Procedure TestProc_OverloadUnitCycle;
  359. Procedure TestProc_Varargs;
  360. Procedure TestProc_ConstOrder;
  361. Procedure TestProc_DuplicateConst;
  362. Procedure TestProc_LocalVarAbsolute;
  363. Procedure TestProc_ResultAbsolute;
  364. Procedure TestProc_LocalVarInit;
  365. Procedure TestProc_ReservedWords;
  366. Procedure TestProc_ConstRefWord;
  367. // anonymous functions
  368. Procedure TestAnonymousProc_Assign_ObjFPC;
  369. Procedure TestAnonymousProc_Assign_Delphi;
  370. Procedure TestAnonymousProc_Arg;
  371. Procedure TestAnonymousProc_Typecast;
  372. Procedure TestAnonymousProc_With;
  373. Procedure TestAnonymousProc_ExceptOn;
  374. Procedure TestAnonymousProc_Nested;
  375. Procedure TestAnonymousProc_NestedAssignResult;
  376. Procedure TestAnonymousProc_Class;
  377. Procedure TestAnonymousProc_ForLoop;
  378. Procedure TestAnonymousProc_AsmDelphi;
  379. // enums, sets
  380. Procedure TestEnum_Name;
  381. Procedure TestEnum_Number;
  382. Procedure TestEnum_ConstFail;
  383. Procedure TestEnum_Functions;
  384. Procedure TestEnumRg_Functions;
  385. Procedure TestEnum_AsParams;
  386. Procedure TestEnumRange_Array;
  387. Procedure TestEnum_ForIn;
  388. Procedure TestEnum_ScopedNumber;
  389. Procedure TestEnum_InFunction;
  390. Procedure TestEnum_Name_Anonymous_Unit;
  391. Procedure TestSet_Enum;
  392. Procedure TestSet_Operators;
  393. Procedure TestSet_Operator_In;
  394. Procedure TestSet_Functions;
  395. Procedure TestSet_PassAsArgClone;
  396. Procedure TestSet_AsParams;
  397. Procedure TestSet_Property;
  398. Procedure TestSet_EnumConst;
  399. Procedure TestSet_IntConst;
  400. Procedure TestSet_IntRange;
  401. Procedure TestSet_AnonymousEnumType;
  402. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  403. Procedure TestSet_ConstEnum;
  404. Procedure TestSet_ConstChar;
  405. Procedure TestSet_ConstInt;
  406. Procedure TestSet_InFunction;
  407. Procedure TestSet_ForIn;
  408. // statements
  409. Procedure TestNestBegin;
  410. Procedure TestIncDec;
  411. Procedure TestLoHiFpcMode;
  412. Procedure TestLoHiDelphiMode;
  413. Procedure TestAssignments;
  414. Procedure TestArithmeticOperators1;
  415. Procedure TestMultiAdd;
  416. Procedure TestLogicalOperators;
  417. Procedure TestBitwiseOperators;
  418. Procedure TestBitwiseOperatorsLongword;
  419. Procedure TestFunctionInt;
  420. Procedure TestFunctionString;
  421. Procedure TestIfThen;
  422. Procedure TestForLoop;
  423. Procedure TestForLoopInsideFunction;
  424. Procedure TestForLoop_ReadVarAfter;
  425. Procedure TestForLoop_Nested;
  426. Procedure TestRepeatUntil;
  427. Procedure TestAsmBlock;
  428. Procedure TestAsmPas_Impl; // ToDo
  429. Procedure TestTryFinally;
  430. Procedure TestTryExcept;
  431. Procedure TestTryExcept_ReservedWords;
  432. Procedure TestIfThenRaiseElse;
  433. Procedure TestCaseOf;
  434. Procedure TestCaseOf_UseSwitch;
  435. Procedure TestCaseOfNoElse;
  436. Procedure TestCaseOfNoElse_UseSwitch;
  437. Procedure TestCaseOfRange;
  438. Procedure TestCaseOfString;
  439. Procedure TestCaseOfChar;
  440. Procedure TestCaseOfExternalClassConst;
  441. Procedure TestDebugger;
  442. // arrays
  443. Procedure TestArray_Dynamic;
  444. Procedure TestArray_Dynamic_Nil;
  445. Procedure TestArray_DynMultiDimensional;
  446. Procedure TestArray_DynamicAssign;
  447. Procedure TestArray_StaticInt;
  448. Procedure TestArray_StaticBool;
  449. Procedure TestArray_StaticChar;
  450. Procedure TestArray_StaticMultiDim;
  451. Procedure TestArray_StaticInFunction;
  452. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  453. Procedure TestArrayOfRecord;
  454. Procedure TestArray_StaticRecord;
  455. Procedure TestArrayOfSet;
  456. Procedure TestArray_DynAsParam;
  457. Procedure TestArray_StaticAsParam;
  458. Procedure TestArrayElement_AsParams;
  459. Procedure TestArrayElementFromFuncResult_AsParams;
  460. Procedure TestArrayEnumTypeRange;
  461. Procedure TestArray_SetLengthOutArg;
  462. Procedure TestArray_SetLengthProperty;
  463. Procedure TestArray_SetLengthMultiDim;
  464. Procedure TestArray_SetLengthDynOfStatic;
  465. Procedure TestArray_OpenArrayOfString;
  466. Procedure TestArray_ArrayOfCharAssignString;
  467. Procedure TestArray_ConstRef;
  468. Procedure TestArray_Concat;
  469. Procedure TestArray_Concat_Append;
  470. Procedure TestArray_Concat_Append_Var;
  471. Procedure TestArray_Copy;
  472. Procedure TestArray_InsertDelete;
  473. Procedure TestArray_Add_Append;
  474. Procedure TestArray_DynArrayConstObjFPC;
  475. Procedure TestArray_DynArrayConstDelphi;
  476. Procedure TestArray_ArrayLitAsParam;
  477. Procedure TestArray_ArrayLitMultiDimAsParam;
  478. Procedure TestArray_ArrayLitStaticAsParam;
  479. Procedure TestArray_ForInArrOfString;
  480. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  481. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  482. Procedure TestArrayOfConst_TVarRec;
  483. Procedure TestArrayOfConst_PassBaseTypes;
  484. Procedure TestArrayOfConst_PassObj;
  485. // record
  486. Procedure TestRecord_Empty;
  487. Procedure TestRecord_Var;
  488. Procedure TestRecord_VarExternal;
  489. Procedure TestRecord_WithDo;
  490. Procedure TestRecord_Assign;
  491. Procedure TestRecord_AsParams;
  492. Procedure TestRecord_ConstRef;
  493. Procedure TestRecordElement_AsParams;
  494. Procedure TestRecordElementFromFuncResult_AsParams;
  495. Procedure TestRecordElementFromWith_AsParams;
  496. Procedure TestRecord_Equal;
  497. Procedure TestRecord_JSValue;
  498. Procedure TestRecord_VariantFail;
  499. Procedure TestRecord_FieldArray;
  500. Procedure TestRecord_Const;
  501. Procedure TestRecord_TypecastFail;
  502. Procedure TestRecord_InFunction;
  503. // anonymous record
  504. Procedure TestRecordAnonym_Field;
  505. Procedure TestRecordAnonym_Assign;
  506. Procedure TestRecordAnonym_Nested;
  507. Procedure TestRecordAnonym_Const;
  508. Procedure TestRecordAnonym_InFunction;
  509. // advanced record
  510. Procedure TestAdvRecord_Function;
  511. Procedure TestAdvRecord_Property;
  512. Procedure TestAdvRecord_PropertyDefault;
  513. Procedure TestAdvRecord_Property_ClassMethod;
  514. Procedure TestAdvRecord_Const;
  515. Procedure TestAdvRecord_ExternalField;
  516. Procedure TestAdvRecord_SubRecord;
  517. Procedure TestAdvRecord_SubClass;
  518. Procedure TestAdvRecord_SubInterfaceFail;
  519. Procedure TestAdvRecord_Constructor;
  520. Procedure TestAdvRecord_ClassConstructor_Program;
  521. Procedure TestAdvRecord_ClassConstructor_Unit;
  522. // classes
  523. Procedure TestClass_TObjectDefaultConstructor;
  524. Procedure TestClass_TObjectConstructorWithParams;
  525. Procedure TestClass_TObjectConstructorWithDefaultParam;
  526. Procedure TestClass_Var;
  527. Procedure TestClass_Method;
  528. Procedure TestClass_Implementation;
  529. Procedure TestClass_Inheritance;
  530. Procedure TestClass_TypeAlias;
  531. Procedure TestClass_AbstractMethod;
  532. Procedure TestClass_CallInherited_ProcNoParams;
  533. Procedure TestClass_CallInherited_WithParams;
  534. Procedure TestClasS_CallInheritedConstructor;
  535. Procedure TestClass_ClassVar_Assign;
  536. Procedure TestClass_CallClassMethod;
  537. Procedure TestClass_CallClassMethodStatic;
  538. Procedure TestClass_Property;
  539. Procedure TestClass_Property_ClassMethod;
  540. Procedure TestClass_Property_ClassMethodStatic;
  541. Procedure TestClass_Property_Indexed;
  542. Procedure TestClass_Property_IndexSpec;
  543. Procedure TestClass_PropertyOfTypeArray;
  544. Procedure TestClass_PropertyDefault;
  545. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  546. //Procedure TestClass_PropertyDefault;
  547. Procedure TestClass_PropertyOverride;
  548. Procedure TestClass_PropertyIncVisibility;
  549. Procedure TestClass_Assigned;
  550. Procedure TestClass_WithClassDoCreate;
  551. Procedure TestClass_WithClassInstDoProperty;
  552. Procedure TestClass_WithClassInstDoPropertyWithParams;
  553. Procedure TestClass_WithClassInstDoFunc;
  554. Procedure TestClass_TypeCast;
  555. Procedure TestClass_TypeCastUntypedParam;
  556. Procedure TestClass_Overloads;
  557. Procedure TestClass_OverloadsAncestor;
  558. Procedure TestClass_OverloadConstructor;
  559. Procedure TestClass_OverloadDelphiOverride;
  560. Procedure TestClass_ReintroduceVarDelphi;
  561. Procedure TestClass_ReintroducedVar;
  562. Procedure TestClass_RaiseDescendant;
  563. Procedure TestClass_ExternalMethod;
  564. Procedure TestClass_ExternalVirtualNameMismatchFail;
  565. Procedure TestClass_ExternalOverrideFail;
  566. Procedure TestClass_ExternalVar;
  567. Procedure TestClass_Const;
  568. Procedure TestClass_ConstEnum;
  569. Procedure TestClass_LocalConstDuplicate_Prg;
  570. Procedure TestClass_LocalConstDuplicate_Unit;
  571. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  572. Procedure TestClass_LocalVarSelfFail;
  573. Procedure TestClass_ArgSelfFail;
  574. Procedure TestClass_NestedProcSelf;
  575. Procedure TestClass_NestedProcSelf2;
  576. Procedure TestClass_NestedProcClassSelf;
  577. Procedure TestClass_NestedProcCallInherited;
  578. Procedure TestClass_TObjectFree;
  579. Procedure TestClass_TObjectFree_VarArg;
  580. Procedure TestClass_TObjectFreeNewInstance;
  581. Procedure TestClass_TObjectFreeLowerCase;
  582. Procedure TestClass_TObjectFreeFunctionFail;
  583. Procedure TestClass_TObjectFreePropertyFail;
  584. Procedure TestClass_ForIn;
  585. Procedure TestClass_DispatchMessage;
  586. Procedure TestClass_Message_DuplicateIntFail;
  587. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  588. // class of
  589. Procedure TestClassOf_Create;
  590. Procedure TestClassOf_Call;
  591. Procedure TestClassOf_Assign;
  592. Procedure TestClassOf_Is;
  593. Procedure TestClassOf_Compare;
  594. Procedure TestClassOf_ClassVar;
  595. Procedure TestClassOf_ClassMethod;
  596. Procedure TestClassOf_ClassProperty;
  597. Procedure TestClassOf_ClassMethodSelf;
  598. Procedure TestClassOf_TypeCast;
  599. Procedure TestClassOf_ImplicitFunctionCall;
  600. Procedure TestClassOf_Const;
  601. // nested class
  602. Procedure TestNestedClass_Alias;
  603. Procedure TestNestedClass_Record;
  604. Procedure TestNestedClass_Class;
  605. Procedure TestNestedClass_CallInherited;
  606. // external class
  607. Procedure TestExternalClass_Var;
  608. Procedure TestExternalClass_Const;
  609. Procedure TestExternalClass_Dollar;
  610. Procedure TestExternalClass_DuplicateVarFail;
  611. Procedure TestExternalClass_Method;
  612. Procedure TestExternalClass_ClassMethod;
  613. Procedure TestExternalClass_ClassMethodStatic;
  614. Procedure TestExternalClass_FunctionResultInTypeCast;
  615. Procedure TestExternalClass_NonExternalOverride;
  616. Procedure TestExternalClass_OverloadHint;
  617. Procedure TestExternalClass_SameNamePublishedProperty;
  618. Procedure TestExternalClass_Property;
  619. Procedure TestExternalClass_PropertyDate;
  620. Procedure TestExternalClass_ClassProperty;
  621. Procedure TestExternalClass_ClassOf;
  622. Procedure TestExternalClass_ClassOtherUnit;
  623. Procedure TestExternalClass_Is;
  624. Procedure TestExternalClass_As;
  625. Procedure TestExternalClass_DestructorFail;
  626. Procedure TestExternalClass_New;
  627. Procedure TestExternalClass_ClassOf_New;
  628. Procedure TestExternalClass_FuncClassOf_New;
  629. Procedure TestExternalClass_New_PasClassFail;
  630. Procedure TestExternalClass_New_PasClassBracketsFail;
  631. Procedure TestExternalClass_NewExtName;
  632. Procedure TestExternalClass_Constructor;
  633. Procedure TestExternalClass_ConstructorBrackets;
  634. Procedure TestExternalClass_LocalConstSameName;
  635. Procedure TestExternalClass_ReintroduceOverload;
  636. Procedure TestExternalClass_Inherited;
  637. Procedure TestExternalClass_PascalAncestorFail;
  638. Procedure TestExternalClass_NewInstance;
  639. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  640. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  641. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  642. Procedure TestExternalClass_JSFunctionPasDescendant;
  643. Procedure TestExternalClass_PascalProperty;
  644. Procedure TestExternalClass_TypeCastToRootClass;
  645. Procedure TestExternalClass_TypeCastToJSObject;
  646. Procedure TestExternalClass_TypeCastStringToExternalString;
  647. Procedure TestExternalClass_TypeCastToJSFunction;
  648. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  649. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  650. Procedure TestExternalClass_BracketAccessor;
  651. Procedure TestExternalClass_BracketAccessor_Call;
  652. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  653. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  654. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  655. Procedure TestExternalClass_BracketAccessor_MultiType;
  656. Procedure TestExternalClass_BracketAccessor_Index;
  657. Procedure TestExternalClass_ForInJSObject;
  658. Procedure TestExternalClass_ForInJSArray;
  659. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  660. Procedure TestExternalClass_NestedConstructor;
  661. // class interfaces
  662. Procedure TestClassInterface_Corba;
  663. Procedure TestClassInterface_Corba_ProcExternalFail;
  664. Procedure TestClassInterface_Corba_Overloads;
  665. Procedure TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  666. Procedure TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  667. Procedure TestClassInterface_Corba_AncestorImpl;
  668. Procedure TestClassInterface_Corba_ImplReintroduce;
  669. Procedure TestClassInterface_Corba_MethodResolution;
  670. Procedure TestClassInterface_COM_AncestorMoreInterfaces;
  671. Procedure TestClassInterface_Corba_MethodOverride;
  672. Procedure TestClassInterface_Corba_Delegation;
  673. Procedure TestClassInterface_Corba_DelegationStatic;
  674. Procedure TestClassInterface_Corba_Operators;
  675. Procedure TestClassInterface_Corba_Args;
  676. Procedure TestClassInterface_Corba_ForIn;
  677. Procedure TestClassInterface_Corba_ArrayOfIntf;
  678. Procedure TestClassInterface_COM_AssignVar;
  679. Procedure TestClassInterface_COM_AssignArg;
  680. Procedure TestClassInterface_COM_FunctionResult;
  681. Procedure TestClassInterface_COM_InheritedFuncResult;
  682. Procedure TestClassInterface_COM_IsAsTypeCasts;
  683. Procedure TestClassInterface_COM_PassAsArg;
  684. Procedure TestClassInterface_COM_PassToUntypedParam;
  685. Procedure TestClassInterface_COM_FunctionInExpr;
  686. Procedure TestClassInterface_COM_Property;
  687. Procedure TestClassInterface_COM_IntfProperty;
  688. Procedure TestClassInterface_COM_Delegation;
  689. Procedure TestClassInterface_COM_With;
  690. Procedure TestClassInterface_COM_ForIn;
  691. Procedure TestClassInterface_COM_ArrayOfIntf;
  692. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  693. Procedure TestClassInterface_COM_RecordIntfFail;
  694. Procedure TestClassInterface_COM_UnitInitialization;
  695. Procedure TestClassInterface_Corba_GUID;
  696. Procedure TestClassInterface_Corba_GUIDProperty;
  697. // helpers
  698. Procedure TestClassHelper_ClassVar;
  699. Procedure TestClassHelper_Method_AccessInstanceFields;
  700. Procedure TestClassHelper_Method_Call;
  701. Procedure TestClassHelper_Method_Nested_Call;
  702. Procedure TestClassHelper_ClassMethod_Call;
  703. Procedure TestClassHelper_ClassOf;
  704. Procedure TestClassHelper_MethodRefObjFPC;
  705. Procedure TestClassHelper_Constructor;
  706. Procedure TestClassHelper_InheritedObjFPC;
  707. Procedure TestClassHelper_Property;
  708. Procedure TestClassHelper_Property_Array;
  709. Procedure TestClassHelper_Property_Array_Default;
  710. Procedure TestClassHelper_Property_Array_DefaultDefault;
  711. Procedure TestClassHelper_ClassProperty;
  712. Procedure TestClassHelper_ClassPropertyStatic;
  713. Procedure TestClassHelper_ClassProperty_Array;
  714. Procedure TestClassHelper_ForIn;
  715. Procedure TestClassHelper_PassProperty;
  716. Procedure TestExtClassHelper_ClassVar;
  717. Procedure TestExtClassHelper_Method_Call;
  718. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  719. Procedure TestRecordHelper_ClassVar;
  720. Procedure TestRecordHelper_Method_Call;
  721. Procedure TestRecordHelper_Constructor;
  722. Procedure TestTypeHelper_ClassVar;
  723. Procedure TestTypeHelper_PassResultElement;
  724. Procedure TestTypeHelper_PassArgs;
  725. Procedure TestTypeHelper_PassVarConst;
  726. Procedure TestTypeHelper_PassFuncResult;
  727. Procedure TestTypeHelper_PassPropertyField;
  728. Procedure TestTypeHelper_PassPropertyGetter;
  729. Procedure TestTypeHelper_PassClassPropertyField;
  730. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  731. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  732. Procedure TestTypeHelper_Property;
  733. Procedure TestTypeHelper_Property_Array;
  734. Procedure TestTypeHelper_ClassProperty;
  735. Procedure TestTypeHelper_ClassProperty_Array;
  736. Procedure TestTypeHelper_ClassMethod;
  737. Procedure TestTypeHelper_ExtClassMethodFail;
  738. Procedure TestTypeHelper_Constructor;
  739. Procedure TestTypeHelper_Word;
  740. Procedure TestTypeHelper_Boolean;
  741. Procedure TestTypeHelper_WordBool;
  742. Procedure TestTypeHelper_Double;
  743. Procedure TestTypeHelper_NativeInt;
  744. Procedure TestTypeHelper_StringChar;
  745. Procedure TestTypeHelper_JSValue;
  746. Procedure TestTypeHelper_Array;
  747. Procedure TestTypeHelper_EnumType;
  748. Procedure TestTypeHelper_SetType;
  749. Procedure TestTypeHelper_InterfaceType;
  750. Procedure TestTypeHelper_NestedSelf;
  751. // proc types
  752. Procedure TestProcType;
  753. Procedure TestProcType_Arg;
  754. Procedure TestProcType_FunctionFPC;
  755. Procedure TestProcType_FunctionDelphi;
  756. Procedure TestProcType_ProcedureDelphi;
  757. Procedure TestProcType_AsParam;
  758. Procedure TestProcType_MethodFPC;
  759. Procedure TestProcType_MethodDelphi;
  760. Procedure TestProcType_PropertyFPC;
  761. Procedure TestProcType_PropertyDelphi;
  762. Procedure TestProcType_WithClassInstDoPropertyFPC;
  763. Procedure TestProcType_Nested;
  764. Procedure TestProcType_NestedOfObject;
  765. Procedure TestProcType_ReferenceToProc;
  766. Procedure TestProcType_ReferenceToMethod;
  767. Procedure TestProcType_Typecast;
  768. Procedure TestProcType_PassProcToUntyped;
  769. Procedure TestProcType_PassProcToArray;
  770. Procedure TestProcType_SafeCallObjFPC;
  771. Procedure TestProcType_SafeCallDelphi;
  772. // pointer
  773. Procedure TestPointer;
  774. Procedure TestPointer_Proc;
  775. Procedure TestPointer_AssignRecordFail;
  776. Procedure TestPointer_AssignStaticArrayFail;
  777. Procedure TestPointer_TypeCastJSValueToPointer;
  778. Procedure TestPointer_NonRecordFail;
  779. Procedure TestPointer_AnonymousArgTypeFail;
  780. Procedure TestPointer_AnonymousVarTypeFail;
  781. Procedure TestPointer_AnonymousResultTypeFail;
  782. Procedure TestPointer_AddrOperatorFail;
  783. Procedure TestPointer_ArrayParamsFail;
  784. Procedure TestPointer_PointerAddFail;
  785. Procedure TestPointer_IncPointerFail;
  786. Procedure TestPointer_Record;
  787. Procedure TestPointer_RecordArg;
  788. // jsvalue
  789. Procedure TestJSValue_AssignToJSValue;
  790. Procedure TestJSValue_TypeCastToBaseType;
  791. Procedure TestJSValue_TypecastToJSValue;
  792. Procedure TestJSValue_Equal;
  793. Procedure TestJSValue_If;
  794. Procedure TestJSValue_Not;
  795. Procedure TestJSValue_Enum;
  796. Procedure TestJSValue_ClassInstance;
  797. Procedure TestJSValue_ClassOf;
  798. Procedure TestJSValue_ArrayOfJSValue;
  799. Procedure TestJSValue_ArrayLit;
  800. Procedure TestJSValue_Params;
  801. Procedure TestJSValue_UntypedParam;
  802. Procedure TestJSValue_FuncResultType;
  803. Procedure TestJSValue_ProcType_Assign;
  804. Procedure TestJSValue_ProcType_Equal;
  805. Procedure TestJSValue_ProcType_Param;
  806. Procedure TestJSValue_AssignToPointerFail;
  807. Procedure TestJSValue_OverloadDouble;
  808. Procedure TestJSValue_OverloadNativeInt;
  809. Procedure TestJSValue_OverloadWord;
  810. Procedure TestJSValue_OverloadString;
  811. Procedure TestJSValue_OverloadChar;
  812. Procedure TestJSValue_OverloadPointer;
  813. Procedure TestJSValue_ForIn;
  814. // RTTI
  815. Procedure TestRTTI_IntRange;
  816. Procedure TestRTTI_Double;
  817. Procedure TestRTTI_ProcType;
  818. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  819. Procedure TestRTTI_ProcTypeAnonymous;
  820. Procedure TestRTTI_EnumAndSetType;
  821. Procedure TestRTTI_EnumRange;
  822. Procedure TestRTTI_AnonymousEnumType;
  823. Procedure TestRTTI_StaticArray;
  824. Procedure TestRTTI_DynArray;
  825. Procedure TestRTTI_ArrayNestedAnonymous;
  826. Procedure TestRTTI_PublishedMethodOverloadFail;
  827. Procedure TestRTTI_PublishedMethodHideNoHint;
  828. Procedure TestRTTI_PublishedMethodExternalFail;
  829. Procedure TestRTTI_PublishedClassPropertyFail;
  830. Procedure TestRTTI_PublishedClassFieldFail;
  831. Procedure TestRTTI_PublishedFieldExternalFail;
  832. Procedure TestRTTI_Class_Field;
  833. Procedure TestRTTI_Class_Method;
  834. Procedure TestRTTI_Class_MethodArgFlags;
  835. Procedure TestRTTI_Class_Property;
  836. Procedure TestRTTI_Class_PropertyParams;
  837. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  838. Procedure TestRTTI_Class_OmitRTTI;
  839. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  840. Procedure TestRTTI_IndexModifier;
  841. Procedure TestRTTI_StoredModifier;
  842. Procedure TestRTTI_DefaultValue;
  843. Procedure TestRTTI_DefaultValueSet;
  844. Procedure TestRTTI_DefaultValueRangeType;
  845. Procedure TestRTTI_DefaultValueInherit;
  846. Procedure TestRTTI_OverrideMethod;
  847. Procedure TestRTTI_ReintroduceMethod;
  848. Procedure TestRTTI_OverloadProperty;
  849. // ToDo: array argument
  850. Procedure TestRTTI_ClassForward;
  851. Procedure TestRTTI_ClassOf;
  852. Procedure TestRTTI_Record;
  853. Procedure TestRTTI_RecordAnonymousArray;
  854. Procedure TestRTTI_Record_ClassVarType;
  855. Procedure TestRTTI_LocalTypes;
  856. Procedure TestRTTI_TypeInfo_BaseTypes;
  857. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  858. Procedure TestRTTI_TypeInfo_LocalFail;
  859. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  860. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  861. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  862. Procedure TestRTTI_TypeInfo_FunctionClassType;
  863. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  864. Procedure TestRTTI_Interface_Corba;
  865. Procedure TestRTTI_Interface_COM;
  866. Procedure TestRTTI_ClassHelper;
  867. Procedure TestRTTI_ExternalClass;
  868. Procedure TestRTTI_Unit;
  869. // Resourcestring
  870. Procedure TestResourcestringProgram;
  871. Procedure TestResourcestringUnit;
  872. Procedure TestResourcestringImplementation;
  873. // Attributes
  874. Procedure TestAttributes_Members;
  875. Procedure TestAttributes_Types;
  876. Procedure TestAttributes_HelperConstructor_Fail;
  877. Procedure TestAttributes_InterfacesList;
  878. // Assertions, checks
  879. procedure TestAssert;
  880. procedure TestAssert_SysUtils;
  881. procedure TestObjectChecks;
  882. procedure TestOverflowChecks_Int;
  883. procedure TestRangeChecks_AssignInt;
  884. procedure TestRangeChecks_AssignIntRange;
  885. procedure TestRangeChecks_AssignEnum;
  886. procedure TestRangeChecks_AssignEnumRange;
  887. procedure TestRangeChecks_AssignChar;
  888. procedure TestRangeChecks_AssignCharRange;
  889. procedure TestRangeChecks_ArrayIndex;
  890. procedure TestRangeChecks_ArrayOfRecIndex;
  891. procedure TestRangeChecks_StringIndex;
  892. procedure TestRangeChecks_TypecastInt;
  893. procedure TestRangeChecks_TypeHelperInt;
  894. // Async/AWait
  895. Procedure TestAsync_Proc;
  896. Procedure TestAsync_CallResultIsPromise;
  897. Procedure TestAsync_ConstructorFail;
  898. Procedure TestAsync_PropertyGetterFail;
  899. Procedure TestAwait_NonPromiseWithTypeFail;
  900. Procedure TestAwait_AsyncCallTypeMismatch;
  901. Procedure TestAWait_OutsideAsyncFail;
  902. Procedure TestAWait_IntegerFail;
  903. Procedure TestAWait_ExternalClassPromise;
  904. Procedure TestAWait_JSValue;
  905. Procedure TestAWait_Result;
  906. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  907. Procedure TestAsync_AnonymousProc;
  908. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  909. Procedure TestAsync_ProcType;
  910. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  911. Procedure TestAsync_Inherited;
  912. Procedure TestAsync_ClassInterface;
  913. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  914. Procedure TestAWait_ClassAs;
  915. // Library
  916. Procedure TestLibrary_Empty;
  917. Procedure TestLibrary_ExportFunc;
  918. Procedure TestLibrary_ExportFuncOverloadedFail;
  919. Procedure TestLibrary_Export_Index_Fail;
  920. Procedure TestLibrary_ExportVar;
  921. Procedure TestLibrary_ExportUnitFunc;
  922. end;
  923. function LinesToStr(Args: array of const): string;
  924. function ExtractFileUnitName(aFilename: string): string;
  925. function JSToStr(El: TJSElement): string;
  926. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  927. implementation
  928. function LinesToStr(Args: array of const): string;
  929. var
  930. s: String;
  931. i: Integer;
  932. begin
  933. s:='';
  934. for i:=Low(Args) to High(Args) do
  935. case Args[i].VType of
  936. vtChar: s += Args[i].VChar+LineEnding;
  937. vtString: s += Args[i].VString^+LineEnding;
  938. vtPChar: s += Args[i].VPChar+LineEnding;
  939. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  940. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  941. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  942. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  943. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  944. end;
  945. Result:=s;
  946. end;
  947. function ExtractFileUnitName(aFilename: string): string;
  948. var
  949. p: Integer;
  950. begin
  951. Result:=ExtractFileName(aFilename);
  952. if Result='' then exit;
  953. for p:=length(Result) downto 1 do
  954. case Result[p] of
  955. '/','\': exit;
  956. '.':
  957. begin
  958. Delete(Result,p,length(Result));
  959. exit;
  960. end;
  961. end;
  962. end;
  963. function JSToStr(El: TJSElement): string;
  964. var
  965. aWriter: TBufferWriter;
  966. aJSWriter: TJSWriter;
  967. begin
  968. aJSWriter:=nil;
  969. aWriter:=TBufferWriter.Create(1000);
  970. try
  971. aJSWriter:=TJSWriter.Create(aWriter);
  972. aJSWriter.IndentSize:=2;
  973. aJSWriter.WriteJS(El);
  974. Result:=aWriter.AsString;
  975. finally
  976. aJSWriter.Free;
  977. aWriter.Free;
  978. end;
  979. end;
  980. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  981. // search diff, ignore changes in spaces
  982. const
  983. SpaceChars = [#9,#10,#13,' '];
  984. var
  985. ExpectedP, ActualP: PChar;
  986. function FindLineEnd(p: PChar): PChar;
  987. begin
  988. Result:=p;
  989. while not (Result^ in [#0,#10,#13]) do inc(Result);
  990. end;
  991. function FindLineStart(p, MinP: PChar): PChar;
  992. begin
  993. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  994. Result:=p;
  995. end;
  996. procedure SkipLineEnd(var p: PChar);
  997. begin
  998. if p^ in [#10,#13] then
  999. begin
  1000. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1001. inc(p,2)
  1002. else
  1003. inc(p);
  1004. end;
  1005. end;
  1006. function HasSpecialChar(s: string): boolean;
  1007. var
  1008. i: Integer;
  1009. begin
  1010. for i:=1 to length(s) do
  1011. if s[i] in [#0..#31,#127..#255] then
  1012. exit(true);
  1013. Result:=false;
  1014. end;
  1015. function HashSpecialChars(s: string): string;
  1016. var
  1017. i: Integer;
  1018. begin
  1019. Result:='';
  1020. for i:=1 to length(s) do
  1021. if s[i] in [#0..#31,#127..#255] then
  1022. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1023. else
  1024. Result:=Result+s[i];
  1025. end;
  1026. procedure DiffFound;
  1027. var
  1028. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1029. ExpLine, ActLine: String;
  1030. i, LineNo, DiffLineNo: Integer;
  1031. begin
  1032. writeln('Diff found "',Msg,'". Lines:');
  1033. // write correct lines
  1034. p:=PChar(Expected);
  1035. LineNo:=0;
  1036. DiffLineNo:=0;
  1037. repeat
  1038. StartPos:=p;
  1039. while not (p^ in [#0,#10,#13]) do inc(p);
  1040. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1041. SkipLineEnd(p);
  1042. inc(LineNo);
  1043. if (p<=ExpectedP) and (p^<>#0) then
  1044. begin
  1045. writeln('= ',ExpLine);
  1046. end else begin
  1047. // diff line
  1048. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1049. // write actual line
  1050. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1051. ActLineEndP:=FindLineEnd(ActualP);
  1052. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1053. writeln('- ',ActLine);
  1054. if HasSpecialChar(ActLine) then
  1055. writeln('- ',HashSpecialChars(ActLine));
  1056. // write expected line
  1057. writeln('+ ',ExpLine);
  1058. if HasSpecialChar(ExpLine) then
  1059. writeln('- ',HashSpecialChars(ExpLine));
  1060. // write empty line with pointer ^
  1061. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1062. writeln('^');
  1063. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1064. CheckSrcDiff:=false;
  1065. // write up to three following actual lines to get some context
  1066. for i:=1 to 3 do begin
  1067. ActLineStartP:=ActLineEndP;
  1068. SkipLineEnd(ActLineStartP);
  1069. if ActLineStartP^=#0 then break;
  1070. ActLineEndP:=FindLineEnd(ActLineStartP);
  1071. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1072. writeln('~ ',ActLine);
  1073. end;
  1074. exit;
  1075. end;
  1076. until p^=#0;
  1077. writeln('DiffFound Actual:-----------------------');
  1078. writeln(Actual);
  1079. writeln('DiffFound Expected:---------------------');
  1080. writeln(Expected);
  1081. writeln('DiffFound ------------------------------');
  1082. Msg:='diff found, but lines are the same, internal error';
  1083. CheckSrcDiff:=false;
  1084. end;
  1085. var
  1086. IsSpaceNeeded: Boolean;
  1087. LastChar, Quote: Char;
  1088. begin
  1089. Result:=true;
  1090. Msg:='';
  1091. if Expected='' then Expected:=' ';
  1092. if Actual='' then Actual:=' ';
  1093. ExpectedP:=PChar(Expected);
  1094. ActualP:=PChar(Actual);
  1095. repeat
  1096. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1097. case ExpectedP^ of
  1098. #0:
  1099. begin
  1100. // check that rest of Actual has only spaces
  1101. while ActualP^ in SpaceChars do inc(ActualP);
  1102. if ActualP^<>#0 then
  1103. begin
  1104. DiffFound;
  1105. exit;
  1106. end;
  1107. exit(true);
  1108. end;
  1109. ' ',#9,#10,#13:
  1110. begin
  1111. // skip space in Expected
  1112. IsSpaceNeeded:=false;
  1113. if ExpectedP>PChar(Expected) then
  1114. LastChar:=ExpectedP[-1]
  1115. else
  1116. LastChar:=#0;
  1117. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1118. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1119. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1120. IsSpaceNeeded:=true;
  1121. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1122. begin
  1123. DiffFound;
  1124. exit;
  1125. end;
  1126. while ActualP^ in SpaceChars do inc(ActualP);
  1127. end;
  1128. '''','"':
  1129. begin
  1130. while ActualP^ in SpaceChars do inc(ActualP);
  1131. if ExpectedP^<>ActualP^ then
  1132. begin
  1133. DiffFound;
  1134. exit;
  1135. end;
  1136. Quote:=ExpectedP^;
  1137. repeat
  1138. inc(ExpectedP);
  1139. inc(ActualP);
  1140. if ExpectedP^<>ActualP^ then
  1141. begin
  1142. DiffFound;
  1143. exit;
  1144. end;
  1145. if (ExpectedP^ in [#0,#10,#13]) then
  1146. break
  1147. else if (ExpectedP^=Quote) then
  1148. begin
  1149. inc(ExpectedP);
  1150. inc(ActualP);
  1151. break;
  1152. end;
  1153. until false;
  1154. end;
  1155. else
  1156. while ActualP^ in SpaceChars do inc(ActualP);
  1157. if ExpectedP^<>ActualP^ then
  1158. begin
  1159. DiffFound;
  1160. exit;
  1161. end;
  1162. inc(ExpectedP);
  1163. inc(ActualP);
  1164. end;
  1165. until false;
  1166. end;
  1167. { TTestEnginePasResolver }
  1168. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1169. begin
  1170. if FModule=AValue then Exit;
  1171. FModule:=AValue;
  1172. end;
  1173. destructor TTestEnginePasResolver.Destroy;
  1174. begin
  1175. FreeAndNil(FStreamResolver);
  1176. FreeAndNil(FParser);
  1177. FreeAndNil(FScanner);
  1178. FreeAndNil(FStreamResolver);
  1179. Module:=nil;
  1180. inherited Destroy;
  1181. end;
  1182. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1183. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1184. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1185. begin
  1186. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1187. TypeParams);
  1188. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1189. Module:=TPasModule(Result);
  1190. end;
  1191. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1192. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1193. begin
  1194. Result:=nil;
  1195. if InFilename<>'' then
  1196. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1197. if Assigned(OnFindUnit) then
  1198. Result:=OnFindUnit(AName);
  1199. if NameExpr=nil then ;
  1200. end;
  1201. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1202. begin
  1203. // do not parse recursively
  1204. // parse via the queue
  1205. if Section=nil then ;
  1206. end;
  1207. { TCustomTestModule }
  1208. procedure TCustomTestModule.FreeSrcMarkers;
  1209. var
  1210. aMarker, Last: PSrcMarker;
  1211. begin
  1212. aMarker:=FirstSrcMarker;
  1213. while aMarker<>nil do
  1214. begin
  1215. Last:=aMarker;
  1216. aMarker:=aMarker^.Next;
  1217. Dispose(Last);
  1218. end;
  1219. FirstSrcMarker:=nil;
  1220. LastSrcMarker:=nil;
  1221. end;
  1222. function TCustomTestModule.GetResolverCount: integer;
  1223. begin
  1224. Result:=FResolvers.Count;
  1225. end;
  1226. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1227. begin
  1228. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1229. end;
  1230. function TCustomTestModule.GetMsgCount: integer;
  1231. begin
  1232. Result:=FHintMsgs.Count;
  1233. end;
  1234. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1235. begin
  1236. Result:=TTestHintMessage(FHintMsgs[Index]);
  1237. end;
  1238. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1239. ): TPasModule;
  1240. var
  1241. DefNamespace: String;
  1242. begin
  1243. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1244. if (Pos('.',aUnitName)<1) then
  1245. begin
  1246. DefNamespace:=GetDefaultNamespace;
  1247. if DefNamespace<>'' then
  1248. begin
  1249. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1250. if Result<>nil then exit;
  1251. end;
  1252. end;
  1253. Result:=LoadUnit(aUnitName);
  1254. if Result<>nil then exit;
  1255. {$IFDEF VerbosePas2JS}
  1256. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1257. {$ENDIF}
  1258. Fail('can''t find unit "'+aUnitName+'"');
  1259. end;
  1260. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1261. var
  1262. aParser: TPasParser;
  1263. Item: TTestHintMessage;
  1264. begin
  1265. aParser:=Sender as TPasParser;
  1266. Item:=TTestHintMessage.Create;
  1267. Item.Id:=aParser.LastMsgNumber;
  1268. Item.MsgType:=aParser.LastMsgType;
  1269. Item.MsgNumber:=aParser.LastMsgNumber;
  1270. Item.Msg:=Msg;
  1271. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1272. {$IFDEF VerbosePas2JS}
  1273. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1274. {$ENDIF}
  1275. FHintMsgs.Add(Item);
  1276. end;
  1277. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1278. );
  1279. var
  1280. aResolver: TTestEnginePasResolver;
  1281. Item: TTestHintMessage;
  1282. begin
  1283. aResolver:=Sender as TTestEnginePasResolver;
  1284. Item:=TTestHintMessage.Create;
  1285. Item.Id:=aResolver.LastMsgId;
  1286. Item.MsgType:=aResolver.LastMsgType;
  1287. Item.MsgNumber:=aResolver.LastMsgNumber;
  1288. Item.Msg:=Msg;
  1289. Item.SourcePos:=aResolver.LastSourcePos;
  1290. {$IFDEF VerbosePas2JS}
  1291. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1292. {$ENDIF}
  1293. FHintMsgs.Add(Item);
  1294. end;
  1295. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1296. var
  1297. Item: TTestHintMessage;
  1298. aScanner: TPas2jsPasScanner;
  1299. begin
  1300. aScanner:=Sender as TPas2jsPasScanner;
  1301. Item:=TTestHintMessage.Create;
  1302. Item.Id:=aScanner.LastMsgNumber;
  1303. Item.MsgType:=aScanner.LastMsgType;
  1304. Item.MsgNumber:=aScanner.LastMsgNumber;
  1305. Item.Msg:=Msg;
  1306. Item.SourcePos:=aScanner.CurSourcePos;
  1307. {$IFDEF VerbosePas2JS}
  1308. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1309. {$ENDIF}
  1310. FHintMsgs.Add(Item);
  1311. end;
  1312. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1313. var
  1314. SubEl: TPasElement;
  1315. i: Integer;
  1316. procedure E(Msg: string);
  1317. var
  1318. s: String;
  1319. begin
  1320. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1321. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1322. writeln('ERROR: ',s);
  1323. Fail(s);
  1324. end;
  1325. begin
  1326. if arg=nil then ;
  1327. if El=nil then exit;
  1328. if El.Parent=El then
  1329. E('El.Parent=El='+GetObjName(El));
  1330. if El is TBinaryExpr then
  1331. begin
  1332. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1333. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1334. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1335. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1336. end
  1337. else if El is TParamsExpr then
  1338. begin
  1339. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1340. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1341. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1342. if TParamsExpr(El).Params[i].Parent<>El then
  1343. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1344. end
  1345. else if El is TProcedureExpr then
  1346. begin
  1347. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1348. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1349. end
  1350. else if El is TPasDeclarations then
  1351. begin
  1352. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1353. begin
  1354. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1355. if SubEl.Parent<>El then
  1356. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1357. end;
  1358. end
  1359. else if El is TPasImplBlock then
  1360. begin
  1361. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1362. begin
  1363. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1364. if SubEl.Parent<>El then
  1365. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1366. end;
  1367. end
  1368. else if El is TPasImplWithDo then
  1369. begin
  1370. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1371. begin
  1372. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1373. if SubEl.Parent<>El then
  1374. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1375. end;
  1376. end
  1377. else if El is TPasProcedure then
  1378. begin
  1379. if TPasProcedure(El).ProcType.Parent<>El then
  1380. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1381. end
  1382. else if El is TPasProcedureType then
  1383. begin
  1384. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1385. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1386. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1387. end;
  1388. end;
  1389. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1390. var
  1391. Data: PTestResolverReferenceData absolute FindData;
  1392. Line, Col: integer;
  1393. begin
  1394. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1395. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1396. if (Data^.Filename=El.SourceFilename)
  1397. and (Data^.Row=Line)
  1398. and (Data^.StartCol<=Col)
  1399. and (Data^.EndCol>=Col)
  1400. then
  1401. Data^.Found.Add(El);
  1402. end;
  1403. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1404. begin
  1405. if FWithTypeInfo=AValue then Exit;
  1406. FWithTypeInfo:=AValue;
  1407. if AValue then
  1408. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1409. else
  1410. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1411. end;
  1412. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1413. var
  1414. i: Integer;
  1415. CurEngine: TTestEnginePasResolver;
  1416. CurUnitName: String;
  1417. begin
  1418. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1419. Result:=nil;
  1420. if (Module.ClassType=TPasModule)
  1421. and (CompareText(Module.Name,aUnitName)=0) then
  1422. exit(Module);
  1423. for i:=0 to ResolverCount-1 do
  1424. begin
  1425. CurEngine:=Resolvers[i];
  1426. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1427. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1428. if CompareText(aUnitName,CurUnitName)=0 then
  1429. begin
  1430. Result:=CurEngine.Module;
  1431. if Result<>nil then exit;
  1432. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1433. FileResolver.FindSourceFile(aUnitName);
  1434. CurEngine.StreamResolver:=TStreamResolver.Create;
  1435. CurEngine.StreamResolver.OwnsStreams:=True;
  1436. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1437. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1438. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1439. InitScanner(CurEngine.Scanner);
  1440. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1441. CurEngine.Parser.Options:=po_tcmodules;
  1442. if CompareText(CurUnitName,'System')=0 then
  1443. CurEngine.Parser.ImplicitUses.Clear;
  1444. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1445. try
  1446. CurEngine.Parser.NextToken;
  1447. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1448. except
  1449. on E: Exception do
  1450. HandleException(E);
  1451. end;
  1452. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1453. Result:=CurEngine.Module;
  1454. exit;
  1455. end;
  1456. end;
  1457. end;
  1458. procedure TCustomTestModule.SetUp;
  1459. begin
  1460. {$IFDEF EnablePasTreeGlobalRefCount}
  1461. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1462. {$ENDIF}
  1463. if FResolvers<>nil then
  1464. begin
  1465. writeln('TCustomTestModule.SetUp FModules<>nil');
  1466. Halt;
  1467. end;
  1468. inherited SetUp;
  1469. FSkipTests:=false;
  1470. FWithTypeInfo:=false;
  1471. FSource:=TStringList.Create;
  1472. FHub:=TPas2JSResolverHub.Create(Self);
  1473. FResolvers:=TObjectList.Create(true);
  1474. FFilename:='test1.pp';
  1475. FFileResolver:=TStreamResolver.Create;
  1476. FFileResolver.OwnsStreams:=True;
  1477. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1478. InitScanner(FScanner);
  1479. FEngine:=AddModule(Filename);
  1480. FEngine.Scanner:=FScanner;
  1481. FScanner.Resolver:=FEngine;
  1482. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1483. FParser.OnLog:=@OnParserLog;
  1484. FEngine.Parser:=FParser;
  1485. Parser.Options:=po_tcmodules;
  1486. FModule:=Nil;
  1487. FConverter:=CreateConverter;
  1488. FExpectedErrorClass:=nil;
  1489. end;
  1490. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1491. var
  1492. Options: TPasToJsConverterOptions;
  1493. begin
  1494. Result:=TPasToJSConverter.Create;
  1495. Options:=co_tcmodules;
  1496. if WithTypeInfo then
  1497. Exclude(Options,coNoTypeInfo)
  1498. else
  1499. Include(Options,coNoTypeInfo);
  1500. Result.Options:=Options;
  1501. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1502. end;
  1503. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1504. begin
  1505. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1506. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1507. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1508. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1509. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1510. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1511. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1512. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1513. aScanner.OnLog:=@OnScannerLog;
  1514. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1515. end;
  1516. procedure TCustomTestModule.TearDown;
  1517. {$IFDEF CheckPasTreeRefCount}
  1518. var
  1519. El: TPasElement;
  1520. {$ENDIF}
  1521. var
  1522. i: Integer;
  1523. CurModule: TPasModule;
  1524. begin
  1525. FreeSrcMarkers;
  1526. FHintMsgs.Clear;
  1527. FHintMsgsGood.Clear;
  1528. FSkipTests:=false;
  1529. FWithTypeInfo:=false;
  1530. FJSRegModuleCall:=nil;
  1531. FJSModuleCallArgs:=nil;
  1532. FJSImplentationUses:=nil;
  1533. FJSInterfaceUses:=nil;
  1534. FJSModuleSrc:=nil;
  1535. FJSInitBody:=nil;
  1536. FreeAndNil(FJSSource);
  1537. FreeAndNil(FJSModule);
  1538. FreeAndNil(FConverter);
  1539. ResolverEngine.Clear;
  1540. FreeAndNil(FSource);
  1541. FreeAndNil(FFileResolver);
  1542. if FResolvers<>nil then
  1543. begin
  1544. for i:=0 to FResolvers.Count-1 do
  1545. begin
  1546. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1547. if CurModule=nil then continue;
  1548. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1549. end;
  1550. for i:=0 to FResolvers.Count-1 do
  1551. begin
  1552. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1553. if CurModule=nil then continue;
  1554. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1555. end;
  1556. FreeAndNil(FResolvers);
  1557. FModule:=nil;
  1558. FEngine:=nil;
  1559. end;
  1560. FreeAndNil(FHub);
  1561. inherited TearDown;
  1562. {$IFDEF EnablePasTreeGlobalRefCount}
  1563. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1564. begin
  1565. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1566. {$IFDEF CheckPasTreeRefCount}
  1567. El:=TPasElement.FirstRefEl;
  1568. while El<>nil do
  1569. begin
  1570. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1571. for i:=0 to El.RefIds.Count-1 do
  1572. writeln(' ',El.RefIds[i]);
  1573. El:=El.NextRefEl;
  1574. end;
  1575. {$ENDIF}
  1576. Halt;
  1577. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1578. end;
  1579. {$ENDIF}
  1580. end;
  1581. procedure TCustomTestModule.Add(Line: string);
  1582. begin
  1583. Source.Add(Line);
  1584. end;
  1585. procedure TCustomTestModule.Add(const Lines: array of string);
  1586. var
  1587. i: Integer;
  1588. begin
  1589. for i:=low(Lines) to high(Lines) do
  1590. Add(Lines[i]);
  1591. end;
  1592. procedure TCustomTestModule.StartParsing;
  1593. var
  1594. Src: String;
  1595. begin
  1596. Src:=Source.Text;
  1597. FEngine.Source:=Src;
  1598. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1599. Scanner.OpenFile(FileName);
  1600. Writeln('// Test : ',Self.TestName);
  1601. Writeln(Src);
  1602. end;
  1603. procedure TCustomTestModule.ParseModuleQueue;
  1604. var
  1605. i: Integer;
  1606. CurResolver: TTestEnginePasResolver;
  1607. Found: Boolean;
  1608. Section: TPasSection;
  1609. begin
  1610. // parse til exception or all Resolvers finished
  1611. while not SkipTests do
  1612. begin
  1613. Found:=false;
  1614. for i:=0 to ResolverCount-1 do
  1615. begin
  1616. CurResolver:=Resolvers[i];
  1617. if CurResolver.CurrentParser=nil then continue;
  1618. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1619. continue;
  1620. CurResolver.Parser.ParseContinue;
  1621. Found:=true;
  1622. break;
  1623. end;
  1624. if not Found then break;
  1625. end;
  1626. for i:=0 to ResolverCount-1 do
  1627. begin
  1628. CurResolver:=Resolvers[i];
  1629. if CurResolver.Parser=nil then
  1630. begin
  1631. if CurResolver.CurrentParser<>nil then
  1632. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1633. continue;
  1634. end;
  1635. if CurResolver.Parser.CurModule<>nil then
  1636. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1637. end;
  1638. end;
  1639. procedure TCustomTestModule.ParseModule;
  1640. begin
  1641. if SkipTests then exit;
  1642. FFirstPasStatement:=nil;
  1643. try
  1644. StartParsing;
  1645. Parser.ParseMain(FModule);
  1646. ParseModuleQueue;
  1647. except
  1648. on E: Exception do
  1649. HandleException(E);
  1650. end;
  1651. if SkipTests then exit;
  1652. AssertNotNull('Module resulted in Module',Module);
  1653. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1654. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1655. end;
  1656. procedure TCustomTestModule.ParseProgram;
  1657. begin
  1658. if SkipTests then exit;
  1659. ParseModule;
  1660. if SkipTests then exit;
  1661. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1662. FPasProgram:=TPasProgram(Module);
  1663. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1664. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1665. if (PasProgram.InitializationSection.Elements.Count>0) then
  1666. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1667. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1668. end;
  1669. procedure TCustomTestModule.ParseLibrary;
  1670. var
  1671. Init: TInitializationSection;
  1672. begin
  1673. if SkipTests then exit;
  1674. ParseModule;
  1675. if SkipTests then exit;
  1676. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1677. FPasLibrary:=TPasLibrary(Module);
  1678. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1679. Init:=PasLibrary.InitializationSection;
  1680. if (Init<>nil) and (Init.Elements.Count>0) then
  1681. if TObject(Init.Elements[0]) is TPasImplBlock then
  1682. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1683. end;
  1684. procedure TCustomTestModule.ParseUnit;
  1685. begin
  1686. if SkipTests then exit;
  1687. ParseModule;
  1688. if SkipTests then exit;
  1689. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1690. AssertNotNull('Has interface section',Module.InterfaceSection);
  1691. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1692. if (Module.InitializationSection<>nil)
  1693. and (Module.InitializationSection.Elements.Count>0)
  1694. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1695. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1696. end;
  1697. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1698. ): TTestEnginePasResolver;
  1699. var
  1700. i: Integer;
  1701. begin
  1702. for i:=0 to ResolverCount-1 do
  1703. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1704. exit(Resolvers[i]);
  1705. Result:=nil;
  1706. end;
  1707. function TCustomTestModule.AddModule(aFilename: string
  1708. ): TTestEnginePasResolver;
  1709. begin
  1710. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1711. if FindModuleWithFilename(aFilename)<>nil then
  1712. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1713. Result:=TTestEnginePasResolver.Create;
  1714. Result.Filename:=aFilename;
  1715. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1716. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1717. Result.OnLog:=@OnPasResolverLog;
  1718. Result.Hub:=Hub;
  1719. FResolvers.Add(Result);
  1720. end;
  1721. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1722. ): TTestEnginePasResolver;
  1723. begin
  1724. Result:=AddModule(aFilename);
  1725. Result.Source:=Src;
  1726. end;
  1727. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1728. ImplementationSrc: string): TTestEnginePasResolver;
  1729. var
  1730. Src: String;
  1731. begin
  1732. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1733. Src+=LineEnding;
  1734. Src+='interface'+LineEnding;
  1735. Src+=LineEnding;
  1736. Src+=InterfaceSrc;
  1737. Src+='implementation'+LineEnding;
  1738. Src+=LineEnding;
  1739. Src+=ImplementationSrc;
  1740. Src+='end.'+LineEnding;
  1741. Result:=AddModuleWithSrc(aFilename,Src);
  1742. end;
  1743. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1744. var
  1745. Intf, Impl: TStringList;
  1746. begin
  1747. Intf:=TStringList.Create;
  1748. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1749. // unit interface
  1750. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1751. Intf.Add('{$modeswitch externalclass}');
  1752. Intf.Add('type');
  1753. Intf.Add(' integer=longint;');
  1754. Intf.Add(' sizeint=nativeint;');
  1755. //'const',
  1756. //' LineEnding = #10;',
  1757. //' DirectorySeparator = ''/'';',
  1758. //' DriveSeparator = '''';',
  1759. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1760. //' AllowDriveSeparators : set of char = [];',
  1761. if supTObject in Parts then
  1762. Intf.AddStrings([
  1763. 'type',
  1764. ' TClass = class of TObject;',
  1765. ' TObject = class',
  1766. ' constructor Create;',
  1767. ' destructor Destroy; virtual;',
  1768. ' class function ClassType: TClass; assembler;',
  1769. ' class function ClassName: String; assembler;',
  1770. ' class function ClassNameIs(const Name: string): boolean;',
  1771. ' class function ClassParent: TClass; assembler;',
  1772. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1773. ' class function UnitName: String; assembler;',
  1774. ' procedure AfterConstruction; virtual;',
  1775. ' procedure BeforeDestruction;virtual;',
  1776. ' function Equals(Obj: TObject): boolean; virtual;',
  1777. ' function ToString: String; virtual;',
  1778. ' end;']);
  1779. if supTInterfacedObject in Parts then
  1780. Intf.AddStrings([
  1781. ' {$Interfaces COM}',
  1782. ' IUnknown = interface',
  1783. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1784. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1785. ' function _AddRef: Integer;',
  1786. ' function _Release: Integer;',
  1787. ' end;',
  1788. ' IInterface = IUnknown;',
  1789. ' TInterfacedObject = class(TObject,IUnknown)',
  1790. ' protected',
  1791. ' fRefCount: Integer;',
  1792. ' { implement methods of IUnknown }',
  1793. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1794. ' function _AddRef: Integer; virtual;',
  1795. ' function _Release: Integer; virtual;',
  1796. ' end;',
  1797. ' TInterfacedClass = class of TInterfacedObject;',
  1798. '',
  1799. '']);
  1800. if supTVarRec in Parts then
  1801. Intf.AddStrings([
  1802. 'const',
  1803. ' vtInteger = 0;',
  1804. ' vtBoolean = 1;',
  1805. ' vtJSValue = 19;',
  1806. 'type',
  1807. ' PVarRec = ^TVarRec;',
  1808. ' TVarRec = record',
  1809. ' VType : byte;',
  1810. ' VJSValue: JSValue;',
  1811. ' vInteger: longint external name ''VJSValue'';',
  1812. ' vBoolean: boolean external name ''VJSValue'';',
  1813. ' end;',
  1814. ' TVarRecArray = array of TVarRec;',
  1815. 'function VarRecs: TVarRecArray; varargs;',
  1816. '']);
  1817. if supTypeInfo in Parts then
  1818. begin
  1819. Intf.AddStrings([
  1820. 'type',
  1821. ' TTypeKind = (',
  1822. ' tkUnknown, // 0',
  1823. ' tkInteger, // 1',
  1824. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1825. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1826. ' tkEnumeration, // 4',
  1827. ' tkSet, // 5',
  1828. ' tkDouble, // 6',
  1829. ' tkBool, // 7',
  1830. ' tkProcVar, // 8 function or procedure',
  1831. ' tkMethod, // 9 proc var of object',
  1832. ' tkArray, // 10 static array',
  1833. ' tkDynArray, // 11',
  1834. ' tkRecord, // 12',
  1835. ' tkClass, // 13',
  1836. ' tkClassRef, // 14',
  1837. ' tkPointer, // 15',
  1838. ' tkJSValue, // 16',
  1839. ' tkRefToProcVar, // 17 variable of procedure type',
  1840. ' tkInterface, // 18',
  1841. ' //tkObject,',
  1842. ' //tkSString,tkLString,tkAString,tkWString,',
  1843. ' //tkVariant,',
  1844. ' //tkWChar,',
  1845. ' //tkInt64,',
  1846. ' //tkQWord,',
  1847. ' //tkInterfaceRaw,',
  1848. ' //tkUString,tkUChar,',
  1849. ' tkHelper, // 19',
  1850. ' //tkFile,',
  1851. ' tkExtClass // 20',
  1852. ' );',
  1853. ' TTypeKinds = set of TTypeKind;',
  1854. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1855. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1856. ' end;',
  1857. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1858. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1859. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1860. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1861. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1862. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1863. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1864. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1865. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1866. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1867. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1868. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1869. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1870. '']);
  1871. end;
  1872. if supWriteln in Parts then
  1873. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1874. Intf.Add('var');
  1875. Intf.Add(' ExitCode: Longint = 0;');
  1876. // unit implementation
  1877. Impl:=TStringList.Create;
  1878. if supTObject in Parts then
  1879. Impl.AddStrings([
  1880. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1881. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1882. 'asm',
  1883. 'end;',
  1884. 'constructor TObject.Create; begin end;',
  1885. 'destructor TObject.Destroy; begin end;',
  1886. 'class function TObject.ClassType: TClass; assembler;',
  1887. 'asm',
  1888. 'end;',
  1889. 'class function TObject.ClassName: String; assembler;',
  1890. 'asm',
  1891. 'end;',
  1892. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1893. 'begin',
  1894. ' Result:=SameText(Name,ClassName);',
  1895. 'end;',
  1896. 'class function TObject.ClassParent: TClass; assembler;',
  1897. 'asm',
  1898. 'end;',
  1899. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1900. 'asm',
  1901. 'end;',
  1902. 'class function TObject.UnitName: String; assembler;',
  1903. 'asm',
  1904. 'end;',
  1905. 'procedure TObject.AfterConstruction; begin end;',
  1906. 'procedure TObject.BeforeDestruction; begin end;',
  1907. 'function TObject.Equals(Obj: TObject): boolean;',
  1908. 'begin',
  1909. ' Result:=Obj=Self;',
  1910. 'end;',
  1911. 'function TObject.ToString: String;',
  1912. 'begin',
  1913. ' Result:=ClassName;',
  1914. 'end;'
  1915. ]);
  1916. if supTInterfacedObject in Parts then
  1917. Impl.AddStrings([
  1918. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1919. //'begin',
  1920. //'end;',
  1921. 'function TInterfacedObject._AddRef: Integer;',
  1922. 'begin',
  1923. 'end;',
  1924. 'function TInterfacedObject._Release: Integer;',
  1925. 'begin',
  1926. 'end;',
  1927. '']);
  1928. if supTVarRec in Parts then
  1929. Impl.AddStrings([
  1930. 'function VarRecs: TVarRecArray; varargs;',
  1931. 'var',
  1932. ' v: PVarRec;',
  1933. 'begin',
  1934. ' v^.VType:=1;',
  1935. ' v^.VJSValue:=2;',
  1936. 'end;',
  1937. '']);
  1938. try
  1939. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1940. finally
  1941. Intf.Free;
  1942. Impl.Free;
  1943. end;
  1944. end;
  1945. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1946. SystemUnitParts: TSystemUnitParts);
  1947. begin
  1948. if NeedSystemUnit then
  1949. AddSystemUnit(SystemUnitParts)
  1950. else
  1951. Parser.ImplicitUses.Clear;
  1952. Add('program '+ExtractFileUnitName(Filename)+';');
  1953. Add('');
  1954. end;
  1955. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1956. SystemUnitParts: TSystemUnitParts);
  1957. begin
  1958. if NeedSystemUnit then
  1959. AddSystemUnit(SystemUnitParts)
  1960. else
  1961. Parser.ImplicitUses.Clear;
  1962. Add('library '+ExtractFileUnitName(Filename)+';');
  1963. Add('');
  1964. end;
  1965. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1966. SystemUnitParts: TSystemUnitParts);
  1967. begin
  1968. if NeedSystemUnit then
  1969. AddSystemUnit(SystemUnitParts)
  1970. else
  1971. Parser.ImplicitUses.Clear;
  1972. Add('unit Test1;');
  1973. Add('');
  1974. end;
  1975. procedure TCustomTestModule.ConvertModule;
  1976. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1977. out UsesLit: TJSArrayLiteral);
  1978. var
  1979. i: Integer;
  1980. Item: TJSElement;
  1981. Lit: TJSLiteral;
  1982. begin
  1983. UsesLit:=nil;
  1984. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1985. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1986. exit; // null is ok
  1987. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1988. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1989. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1990. begin
  1991. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1992. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1993. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1994. Lit:=TJSLiteral(Item);
  1995. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1996. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1997. end;
  1998. end;
  1999. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2000. out Src: TJSSourceElements);
  2001. var
  2002. FunDecl: TJSFunctionDeclarationStatement;
  2003. FunDef: TJSFuncDef;
  2004. FunBody: TJSFunctionBody;
  2005. begin
  2006. Src:=nil;
  2007. AssertNotNull(ParamName,Arg.Expr);
  2008. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2009. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2010. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2011. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2012. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2013. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2014. AssertNotNull(ParamName+' body',FunDef.Body);
  2015. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2016. FunBody:=FunDef.Body as TJSFunctionBody;
  2017. AssertNotNull(ParamName+' body.A',FunBody.A);
  2018. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2019. Src:=FunBody.A as TJSSourceElements;
  2020. end;
  2021. var
  2022. ModuleNameExpr: TJSLiteral;
  2023. InitFunction: TJSFunctionDeclarationStatement;
  2024. InitAssign: TJSSimpleAssignStatement;
  2025. InitName: String;
  2026. LastNode, FirstNode: TJSElement;
  2027. Arg: TJSArrayLiteralElement;
  2028. IsProg, IsLib: Boolean;
  2029. begin
  2030. if SkipTests then exit;
  2031. IsProg:=false;
  2032. IsLib:=false;
  2033. if Module is TPasProgram then
  2034. IsProg:=true
  2035. else if Module is TPasLibrary then
  2036. IsLib:=true;
  2037. try
  2038. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2039. except
  2040. on E: Exception do
  2041. HandleException(E);
  2042. end;
  2043. if SkipTests then exit;
  2044. if ExpectedErrorClass<>nil then
  2045. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2046. FJSSource:=TStringList.Create;
  2047. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2048. {$IFDEF VerbosePas2JS}
  2049. writeln('TTestModule.ConvertModule JS:');
  2050. write(FJSSource.Text);
  2051. {$ENDIF}
  2052. // rtl.module(...
  2053. if JSModule.Statements.Count<1 then
  2054. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2055. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2056. AssertNotNull('register module call',FirstNode);
  2057. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2058. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2059. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2060. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2061. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2062. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2063. // parameter 'unitname'
  2064. if JSModuleCallArgs.Elements.Count<1 then
  2065. Fail('rtl.module first param unit missing');
  2066. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2067. AssertNotNull('module name param',Arg.Expr);
  2068. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2069. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2070. if IsProg then
  2071. begin
  2072. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2073. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2074. end
  2075. else if IsLib then
  2076. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2077. else
  2078. begin
  2079. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2080. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2081. end;
  2082. // main uses section
  2083. if JSModuleCallArgs.Elements.Count<2 then
  2084. Fail('rtl.module second param main uses missing');
  2085. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2086. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2087. // program/library/interface function()
  2088. if JSModuleCallArgs.Elements.Count<3 then
  2089. Fail('rtl.module third param intf-function missing');
  2090. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2091. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2092. // search for $mod.$init or $mod.$main - the last statement
  2093. if IsProg or IsLib then
  2094. begin
  2095. InitName:='$main';
  2096. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2097. end
  2098. else
  2099. InitName:='$init';
  2100. InitAssign:=nil;
  2101. InitFunction:=nil;
  2102. FJSInitBody:=nil;
  2103. if JSModuleSrc.Statements.Count>0 then
  2104. begin
  2105. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2106. if LastNode is TJSSimpleAssignStatement then
  2107. begin
  2108. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2109. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2110. begin
  2111. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2112. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2113. end
  2114. else if IsProg or IsLib then
  2115. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2116. end;
  2117. end;
  2118. // optional: implementation uses section
  2119. if JSModuleCallArgs.Elements.Count<4 then
  2120. exit;
  2121. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2122. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2123. end;
  2124. procedure TCustomTestModule.ConvertProgram;
  2125. begin
  2126. Add('end.');
  2127. ParseProgram;
  2128. ConvertModule;
  2129. end;
  2130. procedure TCustomTestModule.ConvertLibrary;
  2131. begin
  2132. Add('end.');
  2133. ParseLibrary;
  2134. ConvertModule;
  2135. end;
  2136. procedure TCustomTestModule.ConvertUnit;
  2137. begin
  2138. Add('end.');
  2139. ParseUnit;
  2140. ConvertModule;
  2141. end;
  2142. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2143. begin
  2144. Result:=tcmodules.JSToStr(El);
  2145. end;
  2146. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2147. DottedName: string);
  2148. begin
  2149. if DottedName='' then
  2150. begin
  2151. AssertNull(Msg,El);
  2152. end
  2153. else
  2154. begin
  2155. AssertNotNull(Msg,El);
  2156. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2157. end;
  2158. end;
  2159. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2160. begin
  2161. if El=nil then
  2162. Result:=''
  2163. else if El is TJSPrimaryExpressionIdent then
  2164. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2165. else if El is TJSDotMemberExpression then
  2166. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2167. else
  2168. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2169. end;
  2170. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2171. InitStatements: string; ImplStatements: string);
  2172. var
  2173. ActualSrc, ExpectedSrc, InitName: String;
  2174. IsProg, IsLib: Boolean;
  2175. begin
  2176. ActualSrc:=JSToStr(JSModuleSrc);
  2177. if coUseStrict in Converter.Options then
  2178. ExpectedSrc:='"use strict";'+LineEnding
  2179. else
  2180. ExpectedSrc:='';
  2181. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2182. ExpectedSrc:=ExpectedSrc+Statements;
  2183. // unit implementation
  2184. if (Trim(ImplStatements)<>'') then
  2185. ExpectedSrc:=ExpectedSrc+LineEnding
  2186. +'$mod.$implcode = function () {'+LineEnding
  2187. +ImplStatements
  2188. +'};'+LineEnding;
  2189. // program main or unit initialization
  2190. IsProg:=false;
  2191. IsLib:=false;
  2192. if Module is TPasProgram then
  2193. IsProg:=true
  2194. else if Module is TPasLibrary then
  2195. IsLib:=true;
  2196. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2197. begin
  2198. if IsProg or IsLib then
  2199. InitName:='$main'
  2200. else
  2201. InitName:='$init';
  2202. ExpectedSrc:=ExpectedSrc+LineEnding
  2203. +'$mod.'+InitName+' = function () {'+LineEnding
  2204. +InitStatements
  2205. +'};'+LineEnding;
  2206. end;
  2207. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2208. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2209. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2210. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2211. end;
  2212. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2213. var
  2214. ActualSrc: String;
  2215. begin
  2216. ActualSrc:=JSToStr(JSModule);
  2217. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2218. end;
  2219. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2220. // search diff, ignore changes in spaces
  2221. var
  2222. s: string;
  2223. begin
  2224. if CheckSrcDiff(Expected,Actual,s) then exit;
  2225. Fail(Msg+': '+s);
  2226. end;
  2227. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2228. var
  2229. aResolver: TTestEnginePasResolver;
  2230. aConverter: TPasToJSConverter;
  2231. aJSModule: TJSSourceElements;
  2232. ActualSrc: String;
  2233. begin
  2234. aResolver:=GetResolver(Filename);
  2235. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2236. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2237. {$IFDEF VerbosePas2JS}
  2238. writeln('CheckUnit '+Filename+' converting ...');
  2239. {$ENDIF}
  2240. aConverter:=CreateConverter;
  2241. aJSModule:=nil;
  2242. try
  2243. try
  2244. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2245. except
  2246. on E: Exception do
  2247. HandleException(E);
  2248. end;
  2249. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2250. {$IFDEF VerbosePas2JS}
  2251. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2252. write(aResolver.Source);
  2253. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2254. write(ActualSrc);
  2255. {$ENDIF}
  2256. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2257. finally
  2258. aJSModule.Free;
  2259. aConverter.Free;
  2260. end;
  2261. end;
  2262. procedure TCustomTestModule.CheckReferenceDirectives;
  2263. var
  2264. CurFilename: string;
  2265. LineNumber: Integer;
  2266. SrcLine: String;
  2267. CommentStartP, CommentEndP: PChar;
  2268. procedure RaiseError(Msg: string; p: PChar);
  2269. begin
  2270. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2271. end;
  2272. procedure AddMarker(Marker: PSrcMarker);
  2273. begin
  2274. if LastSrcMarker<>nil then
  2275. LastSrcMarker^.Next:=Marker
  2276. else
  2277. FirstSrcMarker:=Marker;
  2278. LastSrcMarker:=Marker;
  2279. end;
  2280. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2281. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2282. begin
  2283. New(Result);
  2284. Result^.Kind:=Kind;
  2285. Result^.Filename:=aFilename;
  2286. Result^.Row:=aLine;
  2287. Result^.StartCol:=aStartCol;
  2288. Result^.EndCol:=aEndCol;
  2289. Result^.Identifier:=Identifier;
  2290. Result^.Next:=nil;
  2291. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2292. AddMarker(Result);
  2293. end;
  2294. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2295. const Identifier: string): PSrcMarker;
  2296. var
  2297. TokenStart, p: PChar;
  2298. begin
  2299. p:=CommentEndP;
  2300. ReadNextPascalToken(p,TokenStart,false,false);
  2301. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2302. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2303. end;
  2304. function ReadIdentifier(var p: PChar): string;
  2305. var
  2306. StartP: PChar;
  2307. begin
  2308. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2309. RaiseError('identifier expected',p);
  2310. StartP:=p;
  2311. inc(p);
  2312. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2313. Result:='';
  2314. SetLength(Result,p-StartP);
  2315. Move(StartP^,Result[1],length(Result));
  2316. end;
  2317. procedure AddLabel;
  2318. var
  2319. Identifier: String;
  2320. p: PChar;
  2321. begin
  2322. p:=CommentStartP+2;
  2323. Identifier:=ReadIdentifier(p);
  2324. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2325. if FindSrcLabel(Identifier)<>nil then
  2326. RaiseError('duplicate label "'+Identifier+'"',p);
  2327. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2328. end;
  2329. procedure AddResolverReference;
  2330. var
  2331. Identifier: String;
  2332. p: PChar;
  2333. begin
  2334. p:=CommentStartP+2;
  2335. Identifier:=ReadIdentifier(p);
  2336. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2337. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2338. end;
  2339. procedure AddDirectReference;
  2340. var
  2341. Identifier: String;
  2342. p: PChar;
  2343. begin
  2344. p:=CommentStartP+2;
  2345. Identifier:=ReadIdentifier(p);
  2346. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2347. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2348. end;
  2349. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2350. var
  2351. p: PChar;
  2352. IsDirective: Boolean;
  2353. begin
  2354. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2355. CurFilename:=aFilename;
  2356. // parse code, find all labels
  2357. LineNumber:=0;
  2358. while LineNumber<SrcLines.Count do
  2359. begin
  2360. inc(LineNumber);
  2361. SrcLine:=SrcLines[LineNumber-1];
  2362. if SrcLine='' then continue;
  2363. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2364. p:=PChar(SrcLine);
  2365. repeat
  2366. case p^ of
  2367. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2368. '{':
  2369. begin
  2370. CommentStartP:=p;
  2371. inc(p);
  2372. IsDirective:=p^ in ['#','@','='];
  2373. // skip to end of comment
  2374. repeat
  2375. case p^ of
  2376. #0:
  2377. if (p-PChar(SrcLine)=length(SrcLine)) then
  2378. begin
  2379. // multi line comment
  2380. if IsDirective then
  2381. RaiseError('directive missing closing bracket',CommentStartP);
  2382. repeat
  2383. inc(LineNumber);
  2384. if LineNumber>SrcLines.Count then exit;
  2385. SrcLine:=SrcLines[LineNumber-1];
  2386. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2387. until SrcLine<>'';
  2388. p:=PChar(SrcLine);
  2389. continue;
  2390. end;
  2391. '}':
  2392. begin
  2393. inc(p);
  2394. break;
  2395. end;
  2396. end;
  2397. inc(p);
  2398. until false;
  2399. CommentEndP:=p;
  2400. case CommentStartP[1] of
  2401. '#': AddLabel;
  2402. '@': AddResolverReference;
  2403. '=': AddDirectReference;
  2404. end;
  2405. p:=CommentEndP;
  2406. continue;
  2407. end;
  2408. '/':
  2409. if p[1]='/' then
  2410. break; // rest of line is comment -> skip
  2411. end;
  2412. inc(p);
  2413. until false;
  2414. end;
  2415. end;
  2416. procedure CheckResolverReference(aMarker: PSrcMarker);
  2417. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2418. var
  2419. aLabel: PSrcMarker;
  2420. ReferenceElements, LabelElements: TFPList;
  2421. i, j, aLine, aCol: Integer;
  2422. El, Ref, LabelEl: TPasElement;
  2423. begin
  2424. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2425. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2426. if aLabel=nil then
  2427. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2428. LabelElements:=nil;
  2429. ReferenceElements:=nil;
  2430. try
  2431. LabelElements:=FindElementsAt(aLabel);
  2432. ReferenceElements:=FindElementsAt(aMarker);
  2433. for i:=0 to ReferenceElements.Count-1 do
  2434. begin
  2435. El:=TPasElement(ReferenceElements[i]);
  2436. Ref:=nil;
  2437. if El.CustomData is TResolvedReference then
  2438. Ref:=TResolvedReference(El.CustomData).Declaration
  2439. else if El.CustomData is TPasPropertyScope then
  2440. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2441. else if El.CustomData is TPasSpecializeTypeData then
  2442. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2443. if Ref<>nil then
  2444. for j:=0 to LabelElements.Count-1 do
  2445. begin
  2446. LabelEl:=TPasElement(LabelElements[j]);
  2447. if Ref=LabelEl then
  2448. exit; // success
  2449. end;
  2450. end;
  2451. // failure write candidates
  2452. for i:=0 to ReferenceElements.Count-1 do
  2453. begin
  2454. El:=TPasElement(ReferenceElements[i]);
  2455. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2456. write(' El=',GetObjName(El));
  2457. if EL is TPrimitiveExpr then
  2458. begin
  2459. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2460. end;
  2461. Ref:=nil;
  2462. if El.CustomData is TResolvedReference then
  2463. Ref:=TResolvedReference(El.CustomData).Declaration
  2464. else if El.CustomData is TPasPropertyScope then
  2465. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2466. else if El.CustomData is TPasSpecializeTypeData then
  2467. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2468. if Ref<>nil then
  2469. begin
  2470. write(' Decl=',GetObjName(Ref));
  2471. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2472. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2473. end
  2474. else
  2475. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2476. writeln;
  2477. end;
  2478. for i:=0 to LabelElements.Count-1 do
  2479. begin
  2480. El:=TPasElement(LabelElements[i]);
  2481. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2482. write(' El=',GetObjName(El));
  2483. writeln;
  2484. end;
  2485. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2486. finally
  2487. LabelElements.Free;
  2488. ReferenceElements.Free;
  2489. end;
  2490. end;
  2491. procedure CheckDirectReference(aMarker: PSrcMarker);
  2492. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2493. var
  2494. aLabel: PSrcMarker;
  2495. ReferenceElements, LabelElements: TFPList;
  2496. i, LabelLine, LabelCol, j: Integer;
  2497. El, LabelEl: TPasElement;
  2498. DeclEl, TypeEl: TPasType;
  2499. begin
  2500. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2501. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2502. if aLabel=nil then
  2503. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2504. LabelElements:=nil;
  2505. ReferenceElements:=nil;
  2506. try
  2507. //writeln('CheckDirectReference finding elements at label ...');
  2508. LabelElements:=FindElementsAt(aLabel);
  2509. //writeln('CheckDirectReference finding elements at reference ...');
  2510. ReferenceElements:=FindElementsAt(aMarker);
  2511. for i:=0 to ReferenceElements.Count-1 do
  2512. begin
  2513. El:=TPasElement(ReferenceElements[i]);
  2514. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2515. if El.ClassType=TPasVariable then
  2516. begin
  2517. if TPasVariable(El).VarType=nil then
  2518. begin
  2519. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2520. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2521. end;
  2522. TypeEl:=TPasVariable(El).VarType;
  2523. for j:=0 to LabelElements.Count-1 do
  2524. begin
  2525. LabelEl:=TPasElement(LabelElements[j]);
  2526. if TypeEl=LabelEl then
  2527. exit; // success
  2528. end;
  2529. end
  2530. else if El is TPasAliasType then
  2531. begin
  2532. DeclEl:=TPasAliasType(El).DestType;
  2533. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2534. if (aLabel^.Filename=DeclEl.SourceFilename)
  2535. and (integer(aLabel^.Row)=LabelLine)
  2536. and (aLabel^.StartCol<=LabelCol)
  2537. and (aLabel^.EndCol>=LabelCol) then
  2538. exit; // success
  2539. end
  2540. else if El.ClassType=TPasArgument then
  2541. begin
  2542. TypeEl:=TPasArgument(El).ArgType;
  2543. for j:=0 to LabelElements.Count-1 do
  2544. begin
  2545. LabelEl:=TPasElement(LabelElements[j]);
  2546. if TypeEl=LabelEl then
  2547. exit; // success
  2548. end;
  2549. end;
  2550. end;
  2551. // failed -> show candidates
  2552. writeln('CheckDirectReference failed: Labels:');
  2553. for j:=0 to LabelElements.Count-1 do
  2554. begin
  2555. LabelEl:=TPasElement(LabelElements[j]);
  2556. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2557. end;
  2558. writeln('CheckDirectReference failed: References:');
  2559. for i:=0 to ReferenceElements.Count-1 do
  2560. begin
  2561. El:=TPasElement(ReferenceElements[i]);
  2562. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2563. //if EL is TPasVariable then
  2564. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2565. end;
  2566. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2567. finally
  2568. LabelElements.Free;
  2569. ReferenceElements.Free;
  2570. end;
  2571. end;
  2572. var
  2573. aMarker: PSrcMarker;
  2574. i: Integer;
  2575. SrcLines: TStringList;
  2576. begin
  2577. Module.ForEachCall(@OnCheckElementParent,nil);
  2578. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2579. // find all markers
  2580. for i:=0 to FileResolver.Streams.Count-1 do
  2581. begin
  2582. GetSrc(i,SrcLines,CurFilename);
  2583. ParseCode(SrcLines,CurFilename);
  2584. SrcLines.Free;
  2585. end;
  2586. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2587. // check references
  2588. aMarker:=FirstSrcMarker;
  2589. while aMarker<>nil do
  2590. begin
  2591. case aMarker^.Kind of
  2592. mkResolverReference: CheckResolverReference(aMarker);
  2593. mkDirectReference: CheckDirectReference(aMarker);
  2594. end;
  2595. aMarker:=aMarker^.Next;
  2596. end;
  2597. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2598. end;
  2599. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2600. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2601. var
  2602. i: Integer;
  2603. Item: TTestHintMessage;
  2604. Expected,Actual: string;
  2605. begin
  2606. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2607. for i:=0 to MsgCount-1 do
  2608. begin
  2609. Item:=Msgs[i];
  2610. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2611. if (Marker<>nil) then
  2612. begin
  2613. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2614. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2615. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2616. end;
  2617. // found
  2618. FHintMsgsGood.Add(Item);
  2619. str(Item.MsgType,Actual);
  2620. str(MsgType,Expected);
  2621. AssertEquals('MsgType',Expected,Actual);
  2622. exit;
  2623. end;
  2624. // needed message missing -> show emitted messages
  2625. WriteSources('',0,0);
  2626. for i:=0 to MsgCount-1 do
  2627. begin
  2628. Item:=Msgs[i];
  2629. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2630. ' ('+IntToStr(Item.MsgNumber),')');
  2631. if Marker<>nil then
  2632. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2633. writeln(' {',Item.Msg,'}');
  2634. end;
  2635. str(MsgType,Expected);
  2636. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2637. if Marker<>nil then
  2638. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2639. Actual:=Actual+' '+Msg;
  2640. Fail(Actual);
  2641. end;
  2642. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2643. );
  2644. var
  2645. i: Integer;
  2646. s, Txt: String;
  2647. Msg: TTestHintMessage;
  2648. begin
  2649. for i:=0 to MsgCount-1 do
  2650. begin
  2651. Msg:=Msgs[i];
  2652. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2653. s:='';
  2654. str(Msg.MsgType,s);
  2655. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2656. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2657. if WithSourcePos then
  2658. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2659. Txt:=Txt+' {'+Msg.Msg+'}';
  2660. Fail(Txt);
  2661. end;
  2662. end;
  2663. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2664. MsgNumber: integer);
  2665. begin
  2666. ExpectedErrorClass:=EScannerError;
  2667. ExpectedErrorMsg:=Msg;
  2668. ExpectedErrorNumber:=MsgNumber;
  2669. end;
  2670. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2671. MsgNumber: integer);
  2672. begin
  2673. ExpectedErrorClass:=EParserError;
  2674. ExpectedErrorMsg:=Msg;
  2675. ExpectedErrorNumber:=MsgNumber;
  2676. end;
  2677. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2678. MsgNumber: integer);
  2679. begin
  2680. ExpectedErrorClass:=EPasResolve;
  2681. ExpectedErrorMsg:=Msg;
  2682. ExpectedErrorNumber:=MsgNumber;
  2683. end;
  2684. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2685. MsgNumber: integer);
  2686. begin
  2687. ExpectedErrorClass:=EPas2JS;
  2688. ExpectedErrorMsg:=Msg;
  2689. ExpectedErrorNumber:=MsgNumber;
  2690. end;
  2691. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2692. var
  2693. MsgNumber: Integer;
  2694. Msg: String;
  2695. begin
  2696. Result:=false;
  2697. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2698. Msg:=E.Message;
  2699. if E is EPas2JS then
  2700. MsgNumber:=EPas2JS(E).MsgNumber
  2701. else if E is EPasResolve then
  2702. MsgNumber:=EPasResolve(E).MsgNumber
  2703. else if E is EParserError then
  2704. MsgNumber:=Parser.LastMsgNumber
  2705. else if E is EScannerError then
  2706. begin
  2707. MsgNumber:=Scanner.LastMsgNumber;
  2708. Msg:=Scanner.LastMsg;
  2709. end
  2710. else
  2711. MsgNumber:=0;
  2712. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2713. if Result then
  2714. SkipTests:=true;
  2715. end;
  2716. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2717. const aFilename: string; aRow, aCol: integer);
  2718. var
  2719. s: String;
  2720. begin
  2721. WriteSources(aFilename,aRow,aCol);
  2722. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2723. writeln('ERROR: ',s);
  2724. Fail(s);
  2725. end;
  2726. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2727. aMarker: PSrcMarker);
  2728. begin
  2729. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2730. end;
  2731. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2732. begin
  2733. if IsErrorExpected(E) then exit;
  2734. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2735. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2736. +' '+Scanner.CurFilename
  2737. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2738. FailException(E);
  2739. end;
  2740. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2741. begin
  2742. if IsErrorExpected(E) then exit;
  2743. WriteSources(E.Filename,E.Row,E.Column);
  2744. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2745. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2746. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2747. );
  2748. FailException(E);
  2749. end;
  2750. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2751. var
  2752. P: TPasSourcePos;
  2753. begin
  2754. if IsErrorExpected(E) then exit;
  2755. P:=E.SourcePos;
  2756. WriteSources(P.FileName,P.Row,P.Column);
  2757. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2758. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2759. FailException(E);
  2760. end;
  2761. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2762. var
  2763. Row, Col: integer;
  2764. begin
  2765. if IsErrorExpected(E) then exit;
  2766. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2767. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2768. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2769. +' '+E.PasElement.SourceFilename
  2770. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2771. FailException(E);
  2772. end;
  2773. procedure TCustomTestModule.HandleException(E: Exception);
  2774. begin
  2775. if E is EScannerError then
  2776. HandleScannerError(EScannerError(E))
  2777. else if E is EParserError then
  2778. HandleParserError(EParserError(E))
  2779. else if E is EPasResolve then
  2780. HandlePasResolveError(EPasResolve(E))
  2781. else if E is EPas2JS then
  2782. HandlePas2JSError(EPas2JS(E))
  2783. else
  2784. begin
  2785. if IsErrorExpected(E) then exit;
  2786. if not (E is EAssertionFailedError) then
  2787. begin
  2788. WriteSources('',0,0);
  2789. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2790. end;
  2791. FailException(E);
  2792. end;
  2793. end;
  2794. procedure TCustomTestModule.FailException(E: Exception);
  2795. var
  2796. MsgNumber: Integer;
  2797. begin
  2798. if ExpectedErrorClass<>nil then
  2799. begin
  2800. if FExpectedErrorClass=E.ClassType then
  2801. begin
  2802. if E is EPas2JS then
  2803. MsgNumber:=EPas2JS(E).MsgNumber
  2804. else if E is EPasResolve then
  2805. MsgNumber:=EPasResolve(E).MsgNumber
  2806. else if E is EParserError then
  2807. MsgNumber:=Parser.LastMsgNumber
  2808. else if E is EScannerError then
  2809. MsgNumber:=Scanner.LastMsgNumber
  2810. else
  2811. MsgNumber:=0;
  2812. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2813. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2814. ExpectedErrorNumber,MsgNumber);
  2815. end else begin
  2816. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2817. end;
  2818. end;
  2819. Fail(E.Message);
  2820. end;
  2821. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2822. aCol: integer);
  2823. var
  2824. IsSrc: Boolean;
  2825. i, j: Integer;
  2826. SrcLines: TStringList;
  2827. Line: string;
  2828. aModule: TTestEnginePasResolver;
  2829. begin
  2830. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2831. for i:=0 to ResolverCount-1 do
  2832. begin
  2833. aModule:=Resolvers[i];
  2834. SrcLines:=TStringList.Create;
  2835. try
  2836. SrcLines.Text:=aModule.Source;
  2837. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2838. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2839. for j:=1 to SrcLines.Count do
  2840. begin
  2841. Line:=SrcLines[j-1];
  2842. if IsSrc and (j=aRow) then
  2843. begin
  2844. write('*');
  2845. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2846. end;
  2847. writeln(Format('%:4d: ',[j]),Line);
  2848. end;
  2849. finally
  2850. SrcLines.Free;
  2851. end;
  2852. end;
  2853. end;
  2854. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2855. var
  2856. i: Integer;
  2857. begin
  2858. for i:=0 to ResolverCount-1 do
  2859. if Filename=Resolvers[i].Filename then exit(i);
  2860. Result:=-1;
  2861. end;
  2862. function TCustomTestModule.GetResolver(const Filename: string
  2863. ): TTestEnginePasResolver;
  2864. var
  2865. i: Integer;
  2866. begin
  2867. i:=IndexOfResolver(Filename);
  2868. if i<0 then exit(nil);
  2869. Result:=Resolvers[i];
  2870. end;
  2871. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2872. out aFilename: string);
  2873. var
  2874. aStream: TStream;
  2875. begin
  2876. SrcLines:=TStringList.Create;
  2877. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2878. aStream.Position:=0;
  2879. SrcLines.LoadFromStream(aStream);
  2880. aFilename:=FileResolver.Streams[Index];
  2881. end;
  2882. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2883. aEndCol: integer): TFPList;
  2884. var
  2885. ok: Boolean;
  2886. FoundRefs: TTestResolverReferenceData;
  2887. i: Integer;
  2888. CurResolver: TTestEnginePasResolver;
  2889. begin
  2890. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2891. FoundRefs:=Default(TTestResolverReferenceData);
  2892. FoundRefs.Filename:=aFilename;
  2893. FoundRefs.Row:=aLine;
  2894. FoundRefs.StartCol:=aStartCol;
  2895. FoundRefs.EndCol:=aEndCol;
  2896. FoundRefs.Found:=TFPList.Create;
  2897. ok:=false;
  2898. try
  2899. // find all markers
  2900. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2901. for i:=0 to ResolverCount-1 do
  2902. begin
  2903. CurResolver:=Resolvers[i];
  2904. if CurResolver.Module=Module then continue;
  2905. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2906. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2907. end;
  2908. ok:=true;
  2909. finally
  2910. if not ok then
  2911. FreeAndNil(FoundRefs.Found);
  2912. end;
  2913. Result:=FoundRefs.Found;
  2914. FoundRefs.Found:=nil;
  2915. end;
  2916. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2917. ErrorOnNoElements: boolean): TFPList;
  2918. begin
  2919. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2920. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2921. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2922. end;
  2923. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2924. begin
  2925. Result:=FirstSrcMarker;
  2926. while Result<>nil do
  2927. begin
  2928. if (Result^.Kind=mkLabel)
  2929. and (CompareText(Result^.Identifier,Identifier)=0) then
  2930. exit;
  2931. Result:=Result^.Next;
  2932. end;
  2933. end;
  2934. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2935. ErrorOnNoElements: boolean): TFPList;
  2936. var
  2937. SrcLabel: PSrcMarker;
  2938. begin
  2939. SrcLabel:=FindSrcLabel(Identifier);
  2940. if SrcLabel=nil then
  2941. Fail('missing label "'+Identifier+'"');
  2942. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2943. end;
  2944. function TCustomTestModule.GetDefaultNamespace: string;
  2945. var
  2946. C: TClass;
  2947. begin
  2948. Result:='';
  2949. if FModule=nil then exit;
  2950. C:=FModule.ClassType;
  2951. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2952. Result:=ResolverEngine.DefaultNameSpace;
  2953. end;
  2954. constructor TCustomTestModule.Create;
  2955. begin
  2956. inherited Create;
  2957. FHintMsgs:=TObjectList.Create(true);
  2958. FHintMsgsGood:=TFPList.Create;
  2959. end;
  2960. destructor TCustomTestModule.Destroy;
  2961. begin
  2962. FreeAndNil(FHintMsgs);
  2963. FreeAndNil(FHintMsgsGood);
  2964. inherited Destroy;
  2965. end;
  2966. { TTestModule }
  2967. procedure TTestModule.TestReservedWords;
  2968. var
  2969. i: integer;
  2970. begin
  2971. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2972. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2973. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2974. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2975. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2976. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2977. end;
  2978. procedure TTestModule.TestEmptyProgram;
  2979. begin
  2980. StartProgram(false);
  2981. Add('begin');
  2982. ConvertProgram;
  2983. CheckSource('TestEmptyProgram','','');
  2984. end;
  2985. procedure TTestModule.TestEmptyProgramUseStrict;
  2986. begin
  2987. Converter.Options:=Converter.Options+[coUseStrict];
  2988. StartProgram(false);
  2989. Add('begin');
  2990. ConvertProgram;
  2991. CheckSource('TestEmptyProgramUseStrict','','');
  2992. end;
  2993. procedure TTestModule.TestEmptyUnit;
  2994. begin
  2995. StartUnit(false);
  2996. Add('interface');
  2997. Add('implementation');
  2998. ConvertUnit;
  2999. CheckSource('TestEmptyUnit',
  3000. LinesToStr([
  3001. ]),
  3002. '');
  3003. end;
  3004. procedure TTestModule.TestEmptyUnitUseStrict;
  3005. begin
  3006. Converter.Options:=Converter.Options+[coUseStrict];
  3007. StartUnit(false);
  3008. Add('interface');
  3009. Add('implementation');
  3010. ConvertUnit;
  3011. CheckSource('TestEmptyUnitUseStrict',
  3012. LinesToStr([
  3013. ''
  3014. ]),
  3015. '');
  3016. end;
  3017. procedure TTestModule.TestDottedUnitNames;
  3018. begin
  3019. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3020. LinesToStr([
  3021. 'var iV: longint;'
  3022. ]),
  3023. '');
  3024. FFilename:='ns1.test1.pp';
  3025. StartProgram(true);
  3026. Add('uses unIt2;');
  3027. Add('var');
  3028. Add(' i: longint;');
  3029. Add('begin');
  3030. Add(' i:=iv;');
  3031. Add(' i:=uNit2.iv;');
  3032. Add(' i:=Ns1.TEst1.i;');
  3033. ConvertProgram;
  3034. CheckSource('TestDottedUnitNames',
  3035. LinesToStr([
  3036. 'this.i = 0;',
  3037. '']),
  3038. LinesToStr([ // this.$init
  3039. '$mod.i = pas["NS1.Unit2"].iV;',
  3040. '$mod.i = pas["NS1.Unit2"].iV;',
  3041. '$mod.i = $mod.i;',
  3042. '']) );
  3043. end;
  3044. procedure TTestModule.TestDottedUnitNameImpl;
  3045. begin
  3046. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3047. LinesToStr([
  3048. 'type',
  3049. ' TObject = class end;',
  3050. ' TTestA = class',
  3051. ' end;'
  3052. ]),
  3053. LinesToStr(['uses TEST.UnitB;'])
  3054. );
  3055. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3056. LinesToStr([
  3057. 'uses TEST.UnitA;',
  3058. 'type TTestB = class(TTestA);'
  3059. ]),
  3060. ''
  3061. );
  3062. StartProgram(true);
  3063. Add('uses TEST.UnitA;');
  3064. Add('begin');
  3065. ConvertProgram;
  3066. CheckSource('TestDottedUnitNameImpl',
  3067. LinesToStr([
  3068. '']),
  3069. LinesToStr([ // this.$init
  3070. '']) );
  3071. CheckUnit('TEST.UnitA.pas',
  3072. LinesToStr([
  3073. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3074. ' var $mod = this;',
  3075. ' rtl.createClass(this, "TObject", null, function () {',
  3076. ' this.$init = function () {',
  3077. ' };',
  3078. ' this.$final = function () {',
  3079. ' };',
  3080. ' });',
  3081. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3082. ' });',
  3083. '}, ["TEST.UnitB"]);'
  3084. ]));
  3085. CheckUnit('TEST.UnitB.pas',
  3086. LinesToStr([
  3087. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3088. ' var $mod = this;',
  3089. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3090. ' });',
  3091. '});'
  3092. ]));
  3093. end;
  3094. procedure TTestModule.TestDottedUnitExpr;
  3095. begin
  3096. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3097. LinesToStr([
  3098. 'procedure DoIt;'
  3099. ]),
  3100. 'procedure DoIt; begin end;');
  3101. FFilename:='Ns1.SubNs1.Test1.pp';
  3102. StartProgram(true);
  3103. Add('uses Ns2.sUbnS2.unIt2;');
  3104. Add('var');
  3105. Add(' i: longint;');
  3106. Add('begin');
  3107. Add(' ns2.subns2.unit2.doit;');
  3108. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3109. ConvertProgram;
  3110. CheckSource('TestDottedUnitExpr',
  3111. LinesToStr([
  3112. 'this.i = 0;',
  3113. '']),
  3114. LinesToStr([ // this.$init
  3115. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3116. '$mod.i = $mod.i;',
  3117. '']) );
  3118. end;
  3119. procedure TTestModule.Test_ModeFPCFail;
  3120. begin
  3121. StartProgram(false);
  3122. Add('{$mode FPC}');
  3123. Add('begin');
  3124. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3125. ConvertProgram;
  3126. end;
  3127. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3128. begin
  3129. StartProgram(false);
  3130. Add('{$modeswitch cblocks-}');
  3131. Add('begin');
  3132. ConvertProgram;
  3133. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3134. CheckResolverUnexpectedHints();
  3135. end;
  3136. procedure TTestModule.TestUnit_UseSystem;
  3137. begin
  3138. StartUnit(true);
  3139. Add([
  3140. 'interface',
  3141. 'var i: integer;',
  3142. 'implementation']);
  3143. ConvertUnit;
  3144. CheckSource('TestUnit_UseSystem',
  3145. LinesToStr([
  3146. 'this.i = 0;',
  3147. '']),
  3148. LinesToStr([
  3149. '']) );
  3150. end;
  3151. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3152. begin
  3153. AddModuleWithIntfImplSrc('unit1.pp',
  3154. LinesToStr([
  3155. 'type number = longint;']),
  3156. LinesToStr([
  3157. 'uses test1;',
  3158. 'procedure DoIt;',
  3159. 'begin',
  3160. ' i:=3;',
  3161. 'end;']));
  3162. StartUnit(true);
  3163. Add([
  3164. 'interface',
  3165. 'uses unit1;',
  3166. 'var i: number;',
  3167. 'implementation']);
  3168. ConvertUnit;
  3169. CheckSource('TestUnit_Intf1Impl2Intf1',
  3170. LinesToStr([
  3171. 'this.i = 0;',
  3172. '']),
  3173. LinesToStr([
  3174. '']) );
  3175. end;
  3176. procedure TTestModule.TestIncludeVersion;
  3177. begin
  3178. StartProgram(false);
  3179. Add([
  3180. 'var',
  3181. ' s: string;',
  3182. ' i: word;',
  3183. 'begin',
  3184. ' s:={$I %line%};',
  3185. ' i:={$I %linenum%};',
  3186. ' s:={$I %currentroutine%};',
  3187. ' s:={$I %pas2jsversion%};',
  3188. ' s:={$I %pas2jstarget%};',
  3189. ' s:={$I %pas2jstargetos%};',
  3190. ' s:={$I %pas2jstargetcpu%};',
  3191. ' s:={$I %file%};',
  3192. '']);
  3193. ConvertProgram;
  3194. CheckSource('TestIncludeVersion',
  3195. LinesToStr([
  3196. 'this.s="";',
  3197. 'this.i = 0;']),
  3198. LinesToStr([
  3199. '$mod.s = "7";',
  3200. '$mod.i = 8;',
  3201. '$mod.s = "<anonymous>";',
  3202. '$mod.s = "Comp.Ver.tcmodules";',
  3203. '$mod.s = "Browser";',
  3204. '$mod.s = "Browser";',
  3205. '$mod.s = "ECMAScript5";',
  3206. '$mod.s = "test1.pp";',
  3207. '']));
  3208. end;
  3209. procedure TTestModule.TestVarInt;
  3210. begin
  3211. StartProgram(false);
  3212. Add('var MyI: longint;');
  3213. Add('begin');
  3214. ConvertProgram;
  3215. CheckSource('TestVarInt','this.MyI=0;','');
  3216. end;
  3217. procedure TTestModule.TestVarBaseTypes;
  3218. begin
  3219. StartProgram(false);
  3220. Add('var');
  3221. Add(' i: longint;');
  3222. Add(' s: string;');
  3223. Add(' c: char;');
  3224. Add(' b: boolean;');
  3225. Add(' d: double;');
  3226. Add(' i2: longint = 3;');
  3227. Add(' s2: string = ''foo'';');
  3228. Add(' c2: char = ''4'';');
  3229. Add(' b2: boolean = true;');
  3230. Add(' d2: double = 5.6;');
  3231. Add(' i3: longint = $707;');
  3232. Add(' i4: nativeint = 9007199254740991;');
  3233. Add(' i5: nativeint = -9007199254740991-1;');
  3234. Add(' i6: nativeint = $fffffffffffff;');
  3235. Add(' i7: nativeint = -$fffffffffffff-1;');
  3236. Add(' i8: byte = 00;');
  3237. Add(' u8: nativeuint = $fffffffffffff;');
  3238. Add(' u9: nativeuint = $0000000000000;');
  3239. Add(' u10: nativeuint = $00ff00;');
  3240. Add('begin');
  3241. ConvertProgram;
  3242. CheckSource('TestVarBaseTypes',
  3243. LinesToStr([
  3244. 'this.i = 0;',
  3245. 'this.s = "";',
  3246. 'this.c = "";',
  3247. 'this.b = false;',
  3248. 'this.d = 0.0;',
  3249. 'this.i2 = 3;',
  3250. 'this.s2 = "foo";',
  3251. 'this.c2 = "4";',
  3252. 'this.b2 = true;',
  3253. 'this.d2 = 5.6;',
  3254. 'this.i3 = 0x707;',
  3255. 'this.i4 = 9007199254740991;',
  3256. 'this.i5 = -9007199254740991-1;',
  3257. 'this.i6 = 0xfffffffffffff;',
  3258. 'this.i7 =-0xfffffffffffff-1;',
  3259. 'this.i8 = 0;',
  3260. 'this.u8 = 0xfffffffffffff;',
  3261. 'this.u9 = 0x0;',
  3262. 'this.u10 = 0xff00;'
  3263. ]),
  3264. '');
  3265. end;
  3266. procedure TTestModule.TestBaseTypeSingleFail;
  3267. begin
  3268. StartProgram(false);
  3269. Add('var s: single;');
  3270. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3271. ConvertProgram;
  3272. end;
  3273. procedure TTestModule.TestBaseTypeExtendedFail;
  3274. begin
  3275. StartProgram(false);
  3276. Add('var e: extended;');
  3277. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3278. ConvertProgram;
  3279. end;
  3280. procedure TTestModule.TestConstBaseTypes;
  3281. begin
  3282. StartProgram(false);
  3283. Add('const');
  3284. Add(' i: longint = 3;');
  3285. Add(' s: string = ''foo'';');
  3286. Add(' c: char = ''4'';');
  3287. Add(' b: boolean = true;');
  3288. Add(' d: double = 5.6;');
  3289. Add(' e = low(word);');
  3290. Add(' f = high(word);');
  3291. Add('begin');
  3292. ConvertProgram;
  3293. CheckSource('TestVarBaseTypes',
  3294. LinesToStr([
  3295. 'this.i=3;',
  3296. 'this.s="foo";',
  3297. 'this.c="4";',
  3298. 'this.b=true;',
  3299. 'this.d=5.6;',
  3300. 'this.e = 0;',
  3301. 'this.f = 65535;'
  3302. ]),
  3303. '');
  3304. end;
  3305. procedure TTestModule.TestAliasTypeRef;
  3306. begin
  3307. StartProgram(false);
  3308. Add('type');
  3309. Add(' a=longint;');
  3310. Add(' b=a;');
  3311. Add('var');
  3312. Add(' c: A;');
  3313. Add(' d: B;');
  3314. Add('begin');
  3315. ConvertProgram;
  3316. CheckSource('TestAliasTypeRef',
  3317. LinesToStr([ // statements
  3318. 'this.c = 0;',
  3319. 'this.d = 0;'
  3320. ]),
  3321. LinesToStr([ // this.$main
  3322. ''
  3323. ]));
  3324. end;
  3325. procedure TTestModule.TestTypeCast_BaseTypes;
  3326. begin
  3327. StartProgram(false);
  3328. Add([
  3329. 'var',
  3330. ' i: longint;',
  3331. ' b: boolean;',
  3332. ' d: double;',
  3333. ' s: string;',
  3334. ' c: char;',
  3335. 'begin',
  3336. ' i:=longint(i);',
  3337. ' i:=longint(b);',
  3338. ' b:=boolean(b);',
  3339. ' b:=boolean(i);',
  3340. ' d:=double(d);',
  3341. ' d:=double(i);',
  3342. ' s:=string(s);',
  3343. ' s:=string(c);',
  3344. ' c:=char(c);',
  3345. ' c:=char(i);',
  3346. ' c:=char(65);',
  3347. ' c:=char(#10);',
  3348. ' c:=char(#$E000);',
  3349. '']);
  3350. ConvertProgram;
  3351. CheckSource('TestAliasTypeRef',
  3352. LinesToStr([ // statements
  3353. 'this.i = 0;',
  3354. 'this.b = false;',
  3355. 'this.d = 0.0;',
  3356. 'this.s = "";',
  3357. 'this.c = "";',
  3358. '']),
  3359. LinesToStr([ // this.$main
  3360. '$mod.i = $mod.i;',
  3361. '$mod.i = ($mod.b ? 1 : 0);',
  3362. '$mod.b = $mod.b;',
  3363. '$mod.b = $mod.i != 0;',
  3364. '$mod.d = $mod.d;',
  3365. '$mod.d = $mod.i;',
  3366. '$mod.s = $mod.s;',
  3367. '$mod.s = $mod.c;',
  3368. '$mod.c = $mod.c;',
  3369. '$mod.c = String.fromCharCode($mod.i);',
  3370. '$mod.c = "A";',
  3371. '$mod.c = "\n";',
  3372. '$mod.c = "";',
  3373. '']));
  3374. end;
  3375. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3376. begin
  3377. StartProgram(false);
  3378. Add('type');
  3379. Add(' integer = longint;');
  3380. Add(' TYesNo = boolean;');
  3381. Add(' TFloat = double;');
  3382. Add(' TCaption = string;');
  3383. Add(' TChar = char;');
  3384. Add('var');
  3385. Add(' i: integer;');
  3386. Add(' b: TYesNo;');
  3387. Add(' d: TFloat;');
  3388. Add(' s: TCaption;');
  3389. Add(' c: TChar;');
  3390. Add('begin');
  3391. Add(' i:=integer(i);');
  3392. Add(' i:=integer(b);');
  3393. Add(' b:=TYesNo(b);');
  3394. Add(' b:=TYesNo(i);');
  3395. Add(' d:=TFloat(d);');
  3396. Add(' d:=TFloat(i);');
  3397. Add(' s:=TCaption(s);');
  3398. Add(' s:=TCaption(c);');
  3399. Add(' c:=TChar(c);');
  3400. ConvertProgram;
  3401. CheckSource('TestAliasTypeRef',
  3402. LinesToStr([ // statements
  3403. 'this.i = 0;',
  3404. 'this.b = false;',
  3405. 'this.d = 0.0;',
  3406. 'this.s = "";',
  3407. 'this.c = "";',
  3408. '']),
  3409. LinesToStr([ // this.$main
  3410. '$mod.i = $mod.i;',
  3411. '$mod.i = ($mod.b ? 1 : 0);',
  3412. '$mod.b = $mod.b;',
  3413. '$mod.b = $mod.i != 0;',
  3414. '$mod.d = $mod.d;',
  3415. '$mod.d = $mod.i;',
  3416. '$mod.s = $mod.s;',
  3417. '$mod.s = $mod.c;',
  3418. '$mod.c = $mod.c;',
  3419. '']));
  3420. end;
  3421. procedure TTestModule.TestEmptyProc;
  3422. begin
  3423. StartProgram(false);
  3424. Add('procedure Test;');
  3425. Add('begin');
  3426. Add('end;');
  3427. Add('begin');
  3428. ConvertProgram;
  3429. CheckSource('TestEmptyProc',
  3430. LinesToStr([ // statements
  3431. 'this.Test = function () {',
  3432. '};'
  3433. ]),
  3434. LinesToStr([ // this.$main
  3435. ''
  3436. ]));
  3437. end;
  3438. procedure TTestModule.TestProcOneParam;
  3439. begin
  3440. StartProgram(false);
  3441. Add('procedure ProcA(i: longint);');
  3442. Add('begin');
  3443. Add('end;');
  3444. Add('begin');
  3445. Add(' PROCA(3);');
  3446. ConvertProgram;
  3447. CheckSource('TestProcOneParam',
  3448. LinesToStr([ // statements
  3449. 'this.ProcA = function (i) {',
  3450. '};'
  3451. ]),
  3452. LinesToStr([ // this.$main
  3453. '$mod.ProcA(3);'
  3454. ]));
  3455. end;
  3456. procedure TTestModule.TestFunctionWithoutParams;
  3457. begin
  3458. StartProgram(false);
  3459. Add('function FuncA: longint;');
  3460. Add('begin');
  3461. Add('end;');
  3462. Add('var i: longint;');
  3463. Add('begin');
  3464. Add(' I:=FUNCA();');
  3465. Add(' I:=FUNCA;');
  3466. Add(' FUNCA();');
  3467. Add(' FUNCA;');
  3468. ConvertProgram;
  3469. CheckSource('TestProcWithoutParams',
  3470. LinesToStr([ // statements
  3471. 'this.FuncA = function () {',
  3472. ' var Result = 0;',
  3473. ' return Result;',
  3474. '};',
  3475. 'this.i=0;'
  3476. ]),
  3477. LinesToStr([ // this.$main
  3478. '$mod.i=$mod.FuncA();',
  3479. '$mod.i=$mod.FuncA();',
  3480. '$mod.FuncA();',
  3481. '$mod.FuncA();'
  3482. ]));
  3483. end;
  3484. procedure TTestModule.TestProcedureWithoutParams;
  3485. begin
  3486. StartProgram(false);
  3487. Add('procedure ProcA;');
  3488. Add('begin');
  3489. Add('end;');
  3490. Add('begin');
  3491. Add(' PROCA();');
  3492. Add(' PROCA;');
  3493. ConvertProgram;
  3494. CheckSource('TestProcWithoutParams',
  3495. LinesToStr([ // statements
  3496. 'this.ProcA = function () {',
  3497. '};'
  3498. ]),
  3499. LinesToStr([ // this.$main
  3500. '$mod.ProcA();',
  3501. '$mod.ProcA();'
  3502. ]));
  3503. end;
  3504. procedure TTestModule.TestIncDec;
  3505. begin
  3506. StartProgram(false);
  3507. Add([
  3508. 'procedure DoIt(var i: longint);',
  3509. 'begin',
  3510. ' inc(i);',
  3511. ' inc(i,2);',
  3512. 'end;',
  3513. 'var',
  3514. ' Bar: longint;',
  3515. 'begin',
  3516. ' inc(bar);',
  3517. ' inc(bar,2);',
  3518. ' dec(bar);',
  3519. ' dec(bar,3);',
  3520. '']);
  3521. ConvertProgram;
  3522. CheckSource('TestIncDec',
  3523. LinesToStr([ // statements
  3524. 'this.DoIt = function (i) {',
  3525. ' i.set(i.get()+1);',
  3526. ' i.set(i.get()+2);',
  3527. '};',
  3528. 'this.Bar = 0;'
  3529. ]),
  3530. LinesToStr([ // this.$main
  3531. '$mod.Bar+=1;',
  3532. '$mod.Bar+=2;',
  3533. '$mod.Bar-=1;',
  3534. '$mod.Bar-=3;'
  3535. ]));
  3536. end;
  3537. procedure TTestModule.TestLoHiFpcMode;
  3538. begin
  3539. StartProgram(false);
  3540. Add([
  3541. '{$mode objfpc}',
  3542. 'const',
  3543. ' LoByte1 = Lo(Word($1234));',
  3544. ' HiByte1 = Hi(Word($1234));',
  3545. ' LoByte2 = Lo(SmallInt($1234));',
  3546. ' HiByte2 = Hi(SmallInt($1234));',
  3547. ' LoWord1 = Lo($1234CDEF);',
  3548. ' HiWord1 = Hi($1234CDEF);',
  3549. ' LoWord2 = Lo(-$1234CDEF);',
  3550. ' HiWord2 = Hi(-$1234CDEF);',
  3551. ' lo4:byte=lo(byte($34));',
  3552. ' hi4:byte=hi(byte($34));',
  3553. ' lo5:byte=lo(shortint(-$34));',
  3554. ' hi5:byte=hi(shortint(-$34));',
  3555. ' lo6:longword=lo($123456789ABCD);',
  3556. ' hi6:longword=hi($123456789ABCD);',
  3557. ' lo7:longword=lo(-$123456789ABCD);',
  3558. ' hi7:longword=hi(-$123456789ABCD);',
  3559. 'var',
  3560. ' b: Byte;',
  3561. ' ss: shortint;',
  3562. ' w: Word;',
  3563. ' si: SmallInt;',
  3564. ' lw: LongWord;',
  3565. ' li: LongInt;',
  3566. ' b2: Byte;',
  3567. ' ni: nativeint;',
  3568. 'begin',
  3569. ' w := $1234;',
  3570. ' ss := -$12;',
  3571. ' b := lo(ss);',
  3572. ' b := HI(ss);',
  3573. ' b := lo(w);',
  3574. ' b := HI(w);',
  3575. ' b2 := lo(b);',
  3576. ' b2 := hi(b);',
  3577. ' lw := $1234CDEF;',
  3578. ' w := lo(lw);',
  3579. ' w := hi(lw);',
  3580. ' ni := $123456789ABCD;',
  3581. ' lw := lo(ni);',
  3582. ' lw := hi(ni);',
  3583. '']);
  3584. ConvertProgram;
  3585. CheckSource('TestLoHiFpcMode',
  3586. LinesToStr([ // statements
  3587. 'this.LoByte1 = 0x1234 & 0xFF;',
  3588. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3589. 'this.LoByte2 = 0x1234 & 0xFF;',
  3590. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3591. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3592. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3593. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3594. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3595. 'this.lo4 = 0x34 & 0xF;',
  3596. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3597. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3598. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3599. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3600. 'this.hi6 = 74565 >>> 0;',
  3601. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3602. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3603. 'this.b = 0;',
  3604. 'this.ss = 0;',
  3605. 'this.w = 0;',
  3606. 'this.si = 0;',
  3607. 'this.lw = 0;',
  3608. 'this.li = 0;',
  3609. 'this.b2 = 0;',
  3610. 'this.ni = 0;',
  3611. '']),
  3612. LinesToStr([ // this.$main
  3613. '$mod.w = 0x1234;',
  3614. '$mod.ss = -0x12;',
  3615. '$mod.b = $mod.ss & 0xFF;',
  3616. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3617. '$mod.b = $mod.w & 0xFF;',
  3618. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3619. '$mod.b2 = $mod.b & 0xF;',
  3620. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3621. '$mod.lw = 0x1234CDEF;',
  3622. '$mod.w = $mod.lw & 0xFFFF;',
  3623. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3624. '$mod.ni = 0x123456789ABCD;',
  3625. '$mod.lw = $mod.ni >>> 0;',
  3626. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3627. '']));
  3628. end;
  3629. procedure TTestModule.TestLoHiDelphiMode;
  3630. begin
  3631. StartProgram(false);
  3632. Add([
  3633. '{$mode delphi}',
  3634. 'const',
  3635. ' LoByte1 = Lo(Word($1234));',
  3636. ' HiByte1 = Hi(Word($1234));',
  3637. ' LoByte2 = Lo(SmallInt($1234));',
  3638. ' HiByte2 = Hi(SmallInt($1234));',
  3639. ' LoByte3 = Lo($1234CDEF);',
  3640. ' HiByte3 = Hi($1234CDEF);',
  3641. ' LoByte4 = Lo(-$1234CDEF);',
  3642. ' HiByte4 = Hi(-$1234CDEF);',
  3643. 'var',
  3644. ' b: Byte;',
  3645. ' w: Word;',
  3646. ' si: SmallInt;',
  3647. ' lw: LongWord;',
  3648. ' li: LongInt;',
  3649. 'begin',
  3650. ' w := $1234;',
  3651. ' b := lo(w);',
  3652. ' b := HI(w);',
  3653. ' lw := $1234CDEF;',
  3654. ' b := lo(lw);',
  3655. ' b := hi(lw);',
  3656. '']);
  3657. ConvertProgram;
  3658. CheckSource('TestLoHiDelphiMode',
  3659. LinesToStr([ // statements
  3660. 'this.LoByte1 = 0x1234 & 0xFF;',
  3661. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3662. 'this.LoByte2 = 0x1234 & 0xFF;',
  3663. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3664. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3665. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3666. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3667. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3668. 'this.b = 0;',
  3669. 'this.w = 0;',
  3670. 'this.si = 0;',
  3671. 'this.lw = 0;',
  3672. 'this.li = 0;'
  3673. ]),
  3674. LinesToStr([ // this.$main
  3675. '$mod.w = 0x1234;',
  3676. '$mod.b = $mod.w & 0xFF;',
  3677. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3678. '$mod.lw = 0x1234CDEF;',
  3679. '$mod.b = $mod.lw & 0xFF;',
  3680. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3681. ]));
  3682. end;
  3683. procedure TTestModule.TestAssignments;
  3684. begin
  3685. StartProgram(false);
  3686. Parser.Options:=Parser.Options+[po_cassignments];
  3687. Add('var');
  3688. Add(' Bar:longint;');
  3689. Add('begin');
  3690. Add(' bar:=3;');
  3691. Add(' bar+=4;');
  3692. Add(' bar-=5;');
  3693. Add(' bar*=6;');
  3694. ConvertProgram;
  3695. CheckSource('TestAssignments',
  3696. LinesToStr([ // statements
  3697. 'this.Bar = 0;'
  3698. ]),
  3699. LinesToStr([ // this.$main
  3700. '$mod.Bar=3;',
  3701. '$mod.Bar+=4;',
  3702. '$mod.Bar-=5;',
  3703. '$mod.Bar*=6;'
  3704. ]));
  3705. end;
  3706. procedure TTestModule.TestArithmeticOperators1;
  3707. begin
  3708. StartProgram(false);
  3709. Add('var');
  3710. Add(' vA,vB,vC:longint;');
  3711. Add('begin');
  3712. Add(' va:=1;');
  3713. Add(' vb:=va+va;');
  3714. Add(' vb:=va div vb;');
  3715. Add(' vb:=va mod vb;');
  3716. Add(' vb:=va+va*vb+va div vb;');
  3717. Add(' vc:=-va;');
  3718. Add(' va:=va-vb;');
  3719. Add(' vb:=va;');
  3720. Add(' if va<vb then vc:=va else vc:=vb;');
  3721. ConvertProgram;
  3722. CheckSource('TestArithmeticOperators1',
  3723. LinesToStr([ // statements
  3724. 'this.vA = 0;',
  3725. 'this.vB = 0;',
  3726. 'this.vC = 0;'
  3727. ]),
  3728. LinesToStr([ // this.$main
  3729. '$mod.vA = 1;',
  3730. '$mod.vB = $mod.vA + $mod.vA;',
  3731. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3732. '$mod.vB = $mod.vA % $mod.vB;',
  3733. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3734. '$mod.vC = -$mod.vA;',
  3735. '$mod.vA = $mod.vA - $mod.vB;',
  3736. '$mod.vB = $mod.vA;',
  3737. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3738. ]));
  3739. end;
  3740. procedure TTestModule.TestMultiAdd;
  3741. begin
  3742. StartProgram(false);
  3743. Add([
  3744. 'function Fly: string; external name ''fly'';',
  3745. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3746. 'var',
  3747. ' Date: double;',
  3748. 'begin',
  3749. ' Result:=(Year>0) and (Year<10000) and',
  3750. ' (Month >= 1) and (Month<=12) and',
  3751. ' (Day>0) and (Day<=31);',
  3752. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3753. 'end;',
  3754. 'var s: string;',
  3755. 'begin',
  3756. ' s:=''a''+''b''+''c''+''d'';',
  3757. ' s:=s+Fly+''e'';',
  3758. ' s:=Fly+Fly+Fly;',
  3759. '']);
  3760. ConvertProgram;
  3761. CheckSource('TestMultiAdd',
  3762. LinesToStr([ // statements
  3763. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3764. ' var Result = false;',
  3765. ' var date = 0.0;',
  3766. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3767. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3768. ' return Result;',
  3769. '};',
  3770. 'this.s = "";',
  3771. '']),
  3772. LinesToStr([ // this.$main
  3773. '$mod.s = "a" + "b" + "c" + "d";',
  3774. '$mod.s = $mod.s + fly() + "e";',
  3775. '$mod.s = fly() + fly() + fly();',
  3776. '']));
  3777. end;
  3778. procedure TTestModule.TestLogicalOperators;
  3779. begin
  3780. StartProgram(false);
  3781. Add('var');
  3782. Add(' vA,vB,vC:boolean;');
  3783. Add('begin');
  3784. Add(' va:=vb and vc;');
  3785. Add(' va:=vb or vc;');
  3786. Add(' va:=vb xor vc;');
  3787. Add(' va:=true and vc;');
  3788. Add(' va:=(vb and vc) or (va and vb);');
  3789. Add(' va:=not vb;');
  3790. ConvertProgram;
  3791. CheckSource('TestLogicalOperators',
  3792. LinesToStr([ // statements
  3793. 'this.vA = false;',
  3794. 'this.vB = false;',
  3795. 'this.vC = false;'
  3796. ]),
  3797. LinesToStr([ // this.$main
  3798. '$mod.vA = $mod.vB && $mod.vC;',
  3799. '$mod.vA = $mod.vB || $mod.vC;',
  3800. '$mod.vA = $mod.vB ^ $mod.vC;',
  3801. '$mod.vA = true && $mod.vC;',
  3802. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3803. '$mod.vA = !$mod.vB;'
  3804. ]));
  3805. end;
  3806. procedure TTestModule.TestBitwiseOperators;
  3807. begin
  3808. StartProgram(false);
  3809. Add([
  3810. 'var',
  3811. ' vA,vB,vC:longint;',
  3812. ' X,Y,Z: nativeint;',
  3813. 'begin',
  3814. ' va:=vb and vc;',
  3815. ' va:=vb or vc;',
  3816. ' va:=vb xor vc;',
  3817. ' va:=vb shl vc;',
  3818. ' va:=vb shr vc;',
  3819. ' va:=3 and vc;',
  3820. ' va:=(vb and vc) or (va and vb);',
  3821. ' va:=not vb;',
  3822. ' X:=Y and Z;',
  3823. ' X:=Y and va;',
  3824. ' X:=Y or Z;',
  3825. ' X:=Y or va;',
  3826. ' X:=Y xor Z;',
  3827. ' X:=Y xor va;',
  3828. '']);
  3829. ConvertProgram;
  3830. CheckSource('TestBitwiseOperators',
  3831. LinesToStr([ // statements
  3832. 'this.vA = 0;',
  3833. 'this.vB = 0;',
  3834. 'this.vC = 0;',
  3835. 'this.X = 0;',
  3836. 'this.Y = 0;',
  3837. 'this.Z = 0;',
  3838. '']),
  3839. LinesToStr([ // this.$main
  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 = $mod.vB >>> $mod.vC;',
  3845. '$mod.vA = 3 & $mod.vC;',
  3846. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3847. '$mod.vA = ~$mod.vB;',
  3848. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3849. '$mod.X = $mod.Y & $mod.vA;',
  3850. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3851. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3852. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3853. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3854. '']));
  3855. end;
  3856. procedure TTestModule.TestBitwiseOperatorsLongword;
  3857. begin
  3858. StartProgram(false);
  3859. Add([
  3860. 'var',
  3861. ' a,b,c:longword;',
  3862. ' i: longint;',
  3863. 'begin',
  3864. ' a:=$12345678;',
  3865. ' b:=$EDCBA987;',
  3866. ' c:=not a;',
  3867. ' c:=a and b;',
  3868. ' c:=a and $ffff0000;',
  3869. ' c:=a or b;',
  3870. ' c:=a or $ff00ff00;',
  3871. ' c:=a xor b;',
  3872. ' c:=a xor $f0f0f0f0;',
  3873. ' c:=a shl 1;',
  3874. ' c:=a shl 16;',
  3875. ' c:=a shl 24;',
  3876. ' c:=a shl b;',
  3877. ' c:=a shr 1;',
  3878. ' c:=a shr 16;',
  3879. ' c:=a shr 24;',
  3880. ' c:=a shr b;',
  3881. ' c:=(b and c) or (a and b);',
  3882. ' c:=i and a;',
  3883. ' c:=i or a;',
  3884. ' c:=i xor a;',
  3885. '']);
  3886. ConvertProgram;
  3887. CheckSource('TestBitwiseOperatorsLongword',
  3888. LinesToStr([ // statements
  3889. 'this.a = 0;',
  3890. 'this.b = 0;',
  3891. 'this.c = 0;',
  3892. 'this.i = 0;',
  3893. '']),
  3894. LinesToStr([ // this.$main
  3895. '$mod.a = 0x12345678;',
  3896. '$mod.b = 0xEDCBA987;',
  3897. '$mod.c = rtl.lw(~$mod.a);',
  3898. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3899. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3900. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3901. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3902. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3903. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3904. '$mod.c = rtl.lw($mod.a << 1);',
  3905. '$mod.c = rtl.lw($mod.a << 16);',
  3906. '$mod.c = rtl.lw($mod.a << 24);',
  3907. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3908. '$mod.c = rtl.lw($mod.a >>> 1);',
  3909. '$mod.c = rtl.lw($mod.a >>> 16);',
  3910. '$mod.c = rtl.lw($mod.a >>> 24);',
  3911. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3912. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3913. '$mod.c = $mod.i & $mod.a;',
  3914. '$mod.c = $mod.i | $mod.a;',
  3915. '$mod.c = $mod.i ^ $mod.a;',
  3916. '']));
  3917. end;
  3918. procedure TTestModule.TestPrgProcVar;
  3919. begin
  3920. StartProgram(false);
  3921. Add('procedure Proc1;');
  3922. Add('type');
  3923. Add(' t1=longint;');
  3924. Add('var');
  3925. Add(' vA:t1;');
  3926. Add('begin');
  3927. Add('end;');
  3928. Add('begin');
  3929. ConvertProgram;
  3930. CheckSource('TestPrgProcVar',
  3931. LinesToStr([ // statements
  3932. 'this.Proc1 = function () {',
  3933. ' var vA=0;',
  3934. '};'
  3935. ]),
  3936. LinesToStr([ // this.$main
  3937. ''
  3938. ]));
  3939. end;
  3940. procedure TTestModule.TestUnitProcVar;
  3941. begin
  3942. StartUnit(false);
  3943. Add('interface');
  3944. Add('');
  3945. Add('type tA=string; // unit scope');
  3946. Add('procedure Proc1;');
  3947. Add('');
  3948. Add('implementation');
  3949. Add('');
  3950. Add('procedure Proc1;');
  3951. Add('type tA=longint; // local proc scope');
  3952. Add('var v1:tA; // using local tA');
  3953. Add('begin');
  3954. Add('end;');
  3955. Add('var v2:tA; // using interface tA');
  3956. ConvertUnit;
  3957. CheckSource('TestUnitProcVar',
  3958. LinesToStr([ // statements
  3959. 'var $impl = $mod.$impl;',
  3960. 'this.Proc1 = function () {',
  3961. ' var v1 = 0;',
  3962. '};',
  3963. '']),
  3964. // this.$init
  3965. '',
  3966. // implementation
  3967. LinesToStr([
  3968. '$impl.v2 = "";',
  3969. '']));
  3970. end;
  3971. procedure TTestModule.TestImplProc;
  3972. begin
  3973. StartUnit(false);
  3974. Add('interface');
  3975. Add('');
  3976. Add('procedure Proc1;');
  3977. Add('');
  3978. Add('implementation');
  3979. Add('');
  3980. Add('procedure Proc1; begin end;');
  3981. Add('procedure Proc2; begin end;');
  3982. Add('initialization');
  3983. Add(' Proc1;');
  3984. Add(' Proc2;');
  3985. ConvertUnit;
  3986. CheckSource('TestImplProc',
  3987. LinesToStr([ // statements
  3988. 'var $impl = $mod.$impl;',
  3989. 'this.Proc1 = function () {',
  3990. '};',
  3991. '']),
  3992. LinesToStr([ // this.$init
  3993. '$mod.Proc1();',
  3994. '$impl.Proc2();',
  3995. '']),
  3996. LinesToStr([ // implementation
  3997. '$impl.Proc2 = function () {',
  3998. '};',
  3999. ''])
  4000. );
  4001. end;
  4002. procedure TTestModule.TestFunctionResult;
  4003. begin
  4004. StartProgram(false);
  4005. Add('function Func1: longint;');
  4006. Add('begin');
  4007. Add(' Result:=3;');
  4008. Add(' Func1:=4;');
  4009. Add('end;');
  4010. Add('begin');
  4011. ConvertProgram;
  4012. CheckSource('TestFunctionResult',
  4013. LinesToStr([ // statements
  4014. 'this.Func1 = function () {',
  4015. ' var Result = 0;',
  4016. ' Result = 3;',
  4017. ' Result = 4;',
  4018. ' return Result;',
  4019. '};'
  4020. ]),
  4021. '');
  4022. end;
  4023. procedure TTestModule.TestNestedProc;
  4024. begin
  4025. StartProgram(false);
  4026. Add([
  4027. 'var vInUnit: longint;',
  4028. 'function DoIt(pA,pD: longint): longint;',
  4029. 'var',
  4030. ' vB: longint;',
  4031. ' vC: longint;',
  4032. ' function Nesty(pA: longint): longint; ',
  4033. ' var vB: longint;',
  4034. ' begin',
  4035. ' Result:=pa+vb+vc+pd+vInUnit;',
  4036. ' nesty:=3;',
  4037. ' doit:=4;',
  4038. ' exit;',
  4039. ' end;',
  4040. 'begin',
  4041. ' Result:=pa+vb+vc;',
  4042. ' doit:=6;',
  4043. ' exit;',
  4044. 'end;',
  4045. 'begin']);
  4046. ConvertProgram;
  4047. CheckSource('TestNestedProc',
  4048. LinesToStr([ // statements
  4049. 'this.vInUnit = 0;',
  4050. 'this.DoIt = function (pA, pD) {',
  4051. ' var Result = 0;',
  4052. ' var vB = 0;',
  4053. ' var vC = 0;',
  4054. ' function Nesty(pA) {',
  4055. ' var Result$1 = 0;',
  4056. ' var vB = 0;',
  4057. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4058. ' Result$1 = 3;',
  4059. ' Result = 4;',
  4060. ' return Result$1;',
  4061. ' return Result$1;',
  4062. ' };',
  4063. ' Result = pA + vB + vC;',
  4064. ' Result = 6;',
  4065. ' return Result;',
  4066. ' return Result;',
  4067. '};'
  4068. ]),
  4069. '');
  4070. end;
  4071. procedure TTestModule.TestNestedProc_ResultString;
  4072. begin
  4073. StartProgram(false);
  4074. Add([
  4075. 'function DoIt: string;',
  4076. ' function Nesty: string; ',
  4077. ' begin',
  4078. ' nesty:=#65#66;',
  4079. ' nesty[1]:=#67;',
  4080. ' doit:=#68;',
  4081. ' doit[2]:=#69;',
  4082. ' end;',
  4083. 'begin',
  4084. ' doit:=#70;',
  4085. ' doit[3]:=#71;',
  4086. 'end;',
  4087. 'begin']);
  4088. ConvertProgram;
  4089. CheckSource('TestNestedProc_ResultString',
  4090. LinesToStr([ // statements
  4091. 'this.DoIt = function () {',
  4092. ' var Result = "";',
  4093. ' function Nesty() {',
  4094. ' var Result$1 = "";',
  4095. ' Result$1 = "AB";',
  4096. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4097. ' Result = "D";',
  4098. ' Result = rtl.setCharAt(Result, 1, "E");',
  4099. ' return Result$1;',
  4100. ' };',
  4101. ' Result = "F";',
  4102. ' Result = rtl.setCharAt(Result, 2, "G");',
  4103. ' return Result;',
  4104. '};'
  4105. ]),
  4106. '');
  4107. end;
  4108. procedure TTestModule.TestForwardProc;
  4109. begin
  4110. StartProgram(false);
  4111. Add('procedure FuncA(Bar: longint); forward;');
  4112. Add('procedure FuncB(Bar: longint);');
  4113. Add('begin');
  4114. Add(' funca(bar);');
  4115. Add('end;');
  4116. Add('procedure funca(bar: longint);');
  4117. Add('begin');
  4118. Add(' if bar=3 then ;');
  4119. Add('end;');
  4120. Add('begin');
  4121. Add(' funca(4);');
  4122. Add(' funcb(5);');
  4123. ConvertProgram;
  4124. CheckSource('TestForwardProc',
  4125. LinesToStr([ // statements'
  4126. 'this.FuncB = function (Bar) {',
  4127. ' $mod.FuncA(Bar);',
  4128. '};',
  4129. 'this.FuncA = function (Bar) {',
  4130. ' if (Bar === 3);',
  4131. '};'
  4132. ]),
  4133. LinesToStr([
  4134. '$mod.FuncA(4);',
  4135. '$mod.FuncB(5);'
  4136. ])
  4137. );
  4138. end;
  4139. procedure TTestModule.TestNestedForwardProc;
  4140. begin
  4141. StartProgram(false);
  4142. Add('procedure FuncA;');
  4143. Add(' procedure FuncB(i: longint); forward;');
  4144. Add(' procedure FuncC(i: longint);');
  4145. Add(' begin');
  4146. Add(' funcb(i);');
  4147. Add(' end;');
  4148. Add(' procedure FuncB(i: longint);');
  4149. Add(' begin');
  4150. Add(' if i=3 then ;');
  4151. Add(' end;');
  4152. Add('begin');
  4153. Add(' funcc(4)');
  4154. Add('end;');
  4155. Add('begin');
  4156. Add(' funca;');
  4157. ConvertProgram;
  4158. CheckSource('TestNestedForwardProc',
  4159. LinesToStr([ // statements'
  4160. 'this.FuncA = function () {',
  4161. ' function FuncC(i) {',
  4162. ' FuncB(i);',
  4163. ' };',
  4164. ' function FuncB(i) {',
  4165. ' if (i === 3);',
  4166. ' };',
  4167. ' FuncC(4);',
  4168. '};'
  4169. ]),
  4170. LinesToStr([
  4171. '$mod.FuncA();'
  4172. ])
  4173. );
  4174. end;
  4175. procedure TTestModule.TestAssignFunctionResult;
  4176. begin
  4177. StartProgram(false);
  4178. Add('function Func1: longint;');
  4179. Add('begin');
  4180. Add('end;');
  4181. Add('var i: longint;');
  4182. Add('begin');
  4183. Add(' i:=func1();');
  4184. Add(' i:=func1()+func1();');
  4185. ConvertProgram;
  4186. CheckSource('TestAssignFunctionResult',
  4187. LinesToStr([ // statements
  4188. 'this.Func1 = function () {',
  4189. ' var Result = 0;',
  4190. ' return Result;',
  4191. '};',
  4192. 'this.i = 0;'
  4193. ]),
  4194. LinesToStr([
  4195. '$mod.i = $mod.Func1();',
  4196. '$mod.i = $mod.Func1() + $mod.Func1();'
  4197. ]));
  4198. end;
  4199. procedure TTestModule.TestFunctionResultInCondition;
  4200. begin
  4201. StartProgram(false);
  4202. Add('function Func1: longint;');
  4203. Add('begin');
  4204. Add('end;');
  4205. Add('function Func2: boolean;');
  4206. Add('begin');
  4207. Add('end;');
  4208. Add('var i: longint;');
  4209. Add('begin');
  4210. Add(' if func2 then ;');
  4211. Add(' if i=func1() then ;');
  4212. Add(' if i=func1 then ;');
  4213. ConvertProgram;
  4214. CheckSource('TestFunctionResultInCondition',
  4215. LinesToStr([ // statements
  4216. 'this.Func1 = function () {',
  4217. ' var Result = 0;',
  4218. ' return Result;',
  4219. '};',
  4220. 'this.Func2 = function () {',
  4221. ' var Result = false;',
  4222. ' return Result;',
  4223. '};',
  4224. 'this.i = 0;'
  4225. ]),
  4226. LinesToStr([
  4227. 'if ($mod.Func2());',
  4228. 'if ($mod.i === $mod.Func1());',
  4229. 'if ($mod.i === $mod.Func1());'
  4230. ]));
  4231. end;
  4232. procedure TTestModule.TestFunctionResultInForLoop;
  4233. begin
  4234. StartProgram(false);
  4235. Add([
  4236. 'function Func1(a: array of longint): longint;',
  4237. 'begin',
  4238. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4239. ' for Result in a do if a[Result]=0 then exit;',
  4240. 'end;',
  4241. 'begin',
  4242. ' Func1([1,2,3])']);
  4243. ConvertProgram;
  4244. CheckSource('TestFunctionResultInForLoop',
  4245. LinesToStr([ // statements
  4246. 'this.Func1 = function (a) {',
  4247. ' var Result = 0;',
  4248. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4249. ' Result = $l;',
  4250. ' if (a[Result] === 0) return Result;',
  4251. ' };',
  4252. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4253. ' Result = $in[$l1];',
  4254. ' if (a[Result] === 0) return Result;',
  4255. ' };',
  4256. ' return Result;',
  4257. '};',
  4258. '']),
  4259. LinesToStr([
  4260. '$mod.Func1([1, 2, 3]);'
  4261. ]));
  4262. end;
  4263. procedure TTestModule.TestFunctionResultInTypeCast;
  4264. begin
  4265. StartProgram(false);
  4266. Add([
  4267. 'function GetInt: longint;',
  4268. 'begin',
  4269. 'end;',
  4270. 'begin',
  4271. ' if Byte(GetInt)=0 then ;',
  4272. '']);
  4273. ConvertProgram;
  4274. CheckSource('TestFunctionResultInTypeCast',
  4275. LinesToStr([ // statements
  4276. 'this.GetInt = function () {',
  4277. ' var Result = 0;',
  4278. ' return Result;',
  4279. '};',
  4280. '']),
  4281. LinesToStr([
  4282. 'if (($mod.GetInt() & 255) === 0) ;'
  4283. ]));
  4284. end;
  4285. procedure TTestModule.TestExit;
  4286. begin
  4287. StartProgram(false);
  4288. Add('procedure ProcA;');
  4289. Add('begin');
  4290. Add(' exit;');
  4291. Add('end;');
  4292. Add('function FuncB: longint;');
  4293. Add('begin');
  4294. Add(' exit;');
  4295. Add(' exit(3);');
  4296. Add('end;');
  4297. Add('function FuncC: string;');
  4298. Add('begin');
  4299. Add(' exit;');
  4300. Add(' exit(''a'');');
  4301. Add(' exit(''abc'');');
  4302. Add('end;');
  4303. Add('begin');
  4304. Add(' exit;');
  4305. Add(' exit(1);');
  4306. ConvertProgram;
  4307. CheckSource('TestExit',
  4308. LinesToStr([ // statements
  4309. 'this.ProcA = function () {',
  4310. ' return;',
  4311. '};',
  4312. 'this.FuncB = function () {',
  4313. ' var Result = 0;',
  4314. ' return Result;',
  4315. ' return 3;',
  4316. ' return Result;',
  4317. '};',
  4318. 'this.FuncC = function () {',
  4319. ' var Result = "";',
  4320. ' return Result;',
  4321. ' return "a";',
  4322. ' return "abc";',
  4323. ' return Result;',
  4324. '};'
  4325. ]),
  4326. LinesToStr([
  4327. 'return;',
  4328. 'return 1;',
  4329. '']));
  4330. end;
  4331. procedure TTestModule.TestExit_ResultInFinally;
  4332. begin
  4333. StartProgram(false);
  4334. Add([
  4335. 'function Run: word;',
  4336. 'begin',
  4337. ' try',
  4338. ' exit(3);', // no Result in finally -> use return 3
  4339. ' finally',
  4340. ' end;',
  4341. 'end;',
  4342. 'function Fly: word;',
  4343. 'begin',
  4344. ' try',
  4345. ' exit(3);',
  4346. ' finally',
  4347. ' if Result>0 then ;',
  4348. ' end;',
  4349. 'end;',
  4350. 'function Jump: word;',
  4351. 'begin',
  4352. ' try',
  4353. ' try',
  4354. ' exit(4);',
  4355. ' finally',
  4356. ' end;',
  4357. ' finally',
  4358. ' if Result>0 then ;',
  4359. ' end;',
  4360. 'end;',
  4361. 'begin',
  4362. '']);
  4363. ConvertProgram;
  4364. CheckSource('TestExit_ResultInFinally',
  4365. LinesToStr([ // statements
  4366. 'this.Run = function () {',
  4367. ' var Result = 0;',
  4368. ' try {',
  4369. ' return 3;',
  4370. ' } finally {',
  4371. ' };',
  4372. ' return Result;',
  4373. '};',
  4374. 'this.Fly = function () {',
  4375. ' var Result = 0;',
  4376. ' try {',
  4377. ' Result = 3;',
  4378. ' return Result;',
  4379. ' } finally {',
  4380. ' if (Result > 0) ;',
  4381. ' };',
  4382. ' return Result;',
  4383. '};',
  4384. 'this.Jump = function () {',
  4385. ' var Result = 0;',
  4386. ' try {',
  4387. ' try {',
  4388. ' Result = 4;',
  4389. ' return Result;',
  4390. ' } finally {',
  4391. ' };',
  4392. ' } finally {',
  4393. ' if (Result > 0) ;',
  4394. ' };',
  4395. ' return Result;',
  4396. '};',
  4397. '']),
  4398. LinesToStr([
  4399. '']));
  4400. end;
  4401. procedure TTestModule.TestBreak;
  4402. begin
  4403. StartProgram(false);
  4404. Add([
  4405. 'var',
  4406. ' i: longint;',
  4407. 'begin',
  4408. ' repeat',
  4409. ' break;',
  4410. ' until true;',
  4411. ' while true do',
  4412. ' break;',
  4413. ' for i:=1 to 2 do',
  4414. ' break;']);
  4415. ConvertProgram;
  4416. CheckSource('TestBreak',
  4417. LinesToStr([ // statements
  4418. 'this.i = 0;'
  4419. ]),
  4420. LinesToStr([
  4421. 'do {',
  4422. ' break;',
  4423. '} while (!true);',
  4424. 'while (true) break;',
  4425. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4426. '']));
  4427. end;
  4428. procedure TTestModule.TestBreakAsVar;
  4429. begin
  4430. StartProgram(false);
  4431. Add([
  4432. 'procedure DoIt(break: boolean);',
  4433. 'begin',
  4434. ' if break then ;',
  4435. 'end;',
  4436. 'var',
  4437. ' break: boolean;',
  4438. 'begin',
  4439. ' if break then ;']);
  4440. ConvertProgram;
  4441. CheckSource('TestBreakAsVar',
  4442. LinesToStr([ // statements
  4443. 'this.DoIt = function (Break) {',
  4444. ' if (Break) ;',
  4445. '};',
  4446. 'this.Break = false;',
  4447. '']),
  4448. LinesToStr([
  4449. 'if($mod.Break) ;',
  4450. '']));
  4451. end;
  4452. procedure TTestModule.TestContinue;
  4453. begin
  4454. StartProgram(false);
  4455. Add('var i: longint;');
  4456. Add('begin');
  4457. Add(' repeat');
  4458. Add(' continue;');
  4459. Add(' until true;');
  4460. Add(' while true do');
  4461. Add(' continue;');
  4462. Add(' for i:=1 to 2 do');
  4463. Add(' continue;');
  4464. ConvertProgram;
  4465. CheckSource('TestContinue',
  4466. LinesToStr([ // statements
  4467. 'this.i = 0;'
  4468. ]),
  4469. LinesToStr([
  4470. 'do {',
  4471. ' continue;',
  4472. '} while (!true);',
  4473. 'while (true) continue;',
  4474. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4475. '']));
  4476. end;
  4477. procedure TTestModule.TestProc_External;
  4478. begin
  4479. StartProgram(false);
  4480. Add('procedure Foo; external name ''console.log'';');
  4481. Add('function Bar: longint; external name ''get.item'';');
  4482. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4483. Add('var');
  4484. Add(' i: longint;');
  4485. Add('begin');
  4486. Add(' Foo;');
  4487. Add(' i:=Bar;');
  4488. Add(' i:=Bla(''abc'');');
  4489. ConvertProgram;
  4490. CheckSource('TestProc_External',
  4491. LinesToStr([ // statements
  4492. 'this.i = 0;'
  4493. ]),
  4494. LinesToStr([
  4495. 'console.log();',
  4496. '$mod.i = get.item();',
  4497. '$mod.i = apply.something("abc");'
  4498. ]));
  4499. end;
  4500. procedure TTestModule.TestProc_ExternalOtherUnit;
  4501. begin
  4502. AddModuleWithIntfImplSrc('unit2.pas',
  4503. LinesToStr([
  4504. 'procedure Now; external name ''Date.now'';',
  4505. 'procedure DoIt;'
  4506. ]),
  4507. 'procedure doit; begin end;');
  4508. StartUnit(true);
  4509. Add('interface');
  4510. Add('uses unit2;');
  4511. Add('implementation');
  4512. Add('begin');
  4513. Add(' now;');
  4514. Add(' now();');
  4515. Add(' uNit2.now;');
  4516. Add(' uNit2.now();');
  4517. Add(' doit;');
  4518. Add(' uNit2.doit;');
  4519. ConvertUnit;
  4520. CheckSource('TestProc_ExternalOtherUnit',
  4521. LinesToStr([
  4522. '']),
  4523. LinesToStr([
  4524. 'Date.now();',
  4525. 'Date.now();',
  4526. 'Date.now();',
  4527. 'Date.now();',
  4528. 'pas.unit2.DoIt();',
  4529. 'pas.unit2.DoIt();',
  4530. '']));
  4531. end;
  4532. procedure TTestModule.TestProc_Asm;
  4533. begin
  4534. StartProgram(false);
  4535. Add([
  4536. '{$mode delphi}',
  4537. 'function DoIt: longint;',
  4538. 'begin;',
  4539. ' asm',
  4540. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4541. ' end;',
  4542. ' asm console.log(); end;',
  4543. ' asm',
  4544. ' s = "'' ";',
  4545. ' s = ''" '';',
  4546. ' s = s + "world" + "''";',
  4547. ' // end',
  4548. ' s = ''end'';',
  4549. ' s = "end";',
  4550. ' s = "foo\"bar";',
  4551. ' s = ''a\''b'';',
  4552. ' s = `${expr}\`-"-''-`;',
  4553. ' s = `multi',
  4554. 'line`;',
  4555. ' end;',
  4556. 'end;',
  4557. 'procedure Fly;',
  4558. 'asm',
  4559. ' return;',
  4560. 'end;',
  4561. 'begin']);
  4562. ConvertProgram;
  4563. CheckSource('TestProc_Asm',
  4564. LinesToStr([ // statements
  4565. 'this.DoIt = function () {',
  4566. ' var Result = 0;',
  4567. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4568. ' console.log();',
  4569. ' s = "'' ";',
  4570. ' s = ''" '';',
  4571. ' s = s + "world" + "''";',
  4572. ' // end',
  4573. ' s = ''end'';',
  4574. ' s = "end";',
  4575. ' s = "foo\"bar";',
  4576. ' s = ''a\''b'';',
  4577. ' s = `${expr}\`-"-''-`;',
  4578. ' s = `multi',
  4579. 'line`;',
  4580. ' return Result;',
  4581. '};',
  4582. 'this.Fly = function () {',
  4583. ' return;',
  4584. '};',
  4585. '']),
  4586. LinesToStr([
  4587. ''
  4588. ]));
  4589. end;
  4590. procedure TTestModule.TestProc_AsmSubBlock;
  4591. begin
  4592. StartProgram(true,[supTObject]);
  4593. Add([
  4594. '{$mode delphi}',
  4595. 'type',
  4596. ' TBird = class end;',
  4597. 'procedure Run(w: word);',
  4598. 'begin;',
  4599. ' if true then asm console.log(); end;',
  4600. ' if w>3 then asm',
  4601. ' var a = w+1;',
  4602. ' w = a+3;',
  4603. ' end;',
  4604. ' while (w>7) do asm',
  4605. ' w+=3; w*=2;',
  4606. ' end;',
  4607. ' try',
  4608. ' except',
  4609. ' on E: TBird do',
  4610. ' asm console.log(E); end;',
  4611. ' on E: TObject do',
  4612. ' asm var i=3; i--; end;',
  4613. ' else asm Fly; High; end;',
  4614. ' end;',
  4615. 'end;',
  4616. 'begin']);
  4617. ConvertProgram;
  4618. CheckSource('TestProc_AsmSubBlock',
  4619. LinesToStr([ // statements
  4620. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4621. '});',
  4622. 'this.Run = function (w) {',
  4623. ' if (true) console.log();',
  4624. ' if (w > 3) {',
  4625. ' var a = w+1;',
  4626. ' w = a+3;',
  4627. ' };',
  4628. ' while (w > 7) {',
  4629. ' w+=3; w*=2;',
  4630. ' };',
  4631. ' try {} catch ($e) {',
  4632. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4633. ' var E = $e;',
  4634. ' console.log(E);',
  4635. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4636. ' var E = $e;',
  4637. ' var i=3; i--;',
  4638. ' } else {',
  4639. ' Fly; High;',
  4640. ' }',
  4641. ' };',
  4642. '};',
  4643. '']),
  4644. LinesToStr([
  4645. ''
  4646. ]));
  4647. end;
  4648. procedure TTestModule.TestProc_Assembler;
  4649. begin
  4650. StartProgram(false);
  4651. Add('function DoIt: longint; assembler;');
  4652. Add('asm');
  4653. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4654. Add('end;');
  4655. Add('begin');
  4656. ConvertProgram;
  4657. CheckSource('TestProc_Assembler',
  4658. LinesToStr([ // statements
  4659. 'this.DoIt = function () {',
  4660. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4661. '};'
  4662. ]),
  4663. LinesToStr([
  4664. ''
  4665. ]));
  4666. end;
  4667. procedure TTestModule.TestProc_VarParam;
  4668. begin
  4669. StartProgram(false);
  4670. Add('type integer = longint;');
  4671. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4672. Add('var vJ: integer;');
  4673. Add('begin');
  4674. Add(' vg:=vg+1;');
  4675. Add(' vj:=vh+2;');
  4676. Add(' vi:=vi+3;');
  4677. Add(' doit(vg,vg,vg);');
  4678. Add(' doit(vh,vh,vj);');
  4679. Add(' doit(vi,vi,vi);');
  4680. Add(' doit(vj,vj,vj);');
  4681. Add('end;');
  4682. Add('var i: integer;');
  4683. Add('begin');
  4684. Add(' doit(i,i,i);');
  4685. ConvertProgram;
  4686. CheckSource('TestProc_VarParam',
  4687. LinesToStr([ // statements
  4688. 'this.DoIt = function (vG,vH,vI) {',
  4689. ' var vJ = 0;',
  4690. ' vG = vG + 1;',
  4691. ' vJ = vH + 2;',
  4692. ' vI.set(vI.get()+3);',
  4693. ' $mod.DoIt(vG, vG, {',
  4694. ' get: function () {',
  4695. ' return vG;',
  4696. ' },',
  4697. ' set: function (v) {',
  4698. ' vG = v;',
  4699. ' }',
  4700. ' });',
  4701. ' $mod.DoIt(vH, vH, {',
  4702. ' get: function () {',
  4703. ' return vJ;',
  4704. ' },',
  4705. ' set: function (v) {',
  4706. ' vJ = v;',
  4707. ' }',
  4708. ' });',
  4709. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4710. ' $mod.DoIt(vJ, vJ, {',
  4711. ' get: function () {',
  4712. ' return vJ;',
  4713. ' },',
  4714. ' set: function (v) {',
  4715. ' vJ = v;',
  4716. ' }',
  4717. ' });',
  4718. '};',
  4719. 'this.i = 0;'
  4720. ]),
  4721. LinesToStr([
  4722. '$mod.DoIt($mod.i,$mod.i,{',
  4723. ' p: $mod,',
  4724. ' get: function () {',
  4725. ' return this.p.i;',
  4726. ' },',
  4727. ' set: function (v) {',
  4728. ' this.p.i = v;',
  4729. ' }',
  4730. '});'
  4731. ]));
  4732. end;
  4733. procedure TTestModule.TestProc_VarParamString;
  4734. begin
  4735. StartProgram(false);
  4736. Add(['type TCaption = string;',
  4737. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4738. 'var c: char;',
  4739. 'begin',
  4740. ' va[1]:=c;',
  4741. ' vb[2]:=c;',
  4742. ' vc[3]:=c;',
  4743. 'end;',
  4744. 'begin']);
  4745. ConvertProgram;
  4746. CheckSource('TestProc_VarParamString',
  4747. LinesToStr([ // statements
  4748. 'this.DoIt = function (vA,vB,vC) {',
  4749. ' var c = "";',
  4750. ' vA = rtl.setCharAt(vA, 0, c);',
  4751. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4752. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4753. '};',
  4754. '']),
  4755. LinesToStr([
  4756. ]));
  4757. end;
  4758. procedure TTestModule.TestProc_VarParamV;
  4759. begin
  4760. StartProgram(false);
  4761. Add([
  4762. 'procedure Inc2(var i: longint);',
  4763. 'begin',
  4764. ' i:=i+2;',
  4765. 'end;',
  4766. 'procedure DoIt(v: longint);',
  4767. 'var p: array of longint;',
  4768. 'begin',
  4769. ' Inc2(v);',
  4770. ' Inc2(p[v]);',
  4771. 'end;',
  4772. 'begin']);
  4773. ConvertProgram;
  4774. CheckSource('TestProc_VarParamV',
  4775. LinesToStr([ // statements
  4776. 'this.Inc2 = function (i) {',
  4777. ' i.set(i.get()+2);',
  4778. '};',
  4779. 'this.DoIt = function (v) {',
  4780. ' var p = [];',
  4781. ' $mod.Inc2({get: function () {',
  4782. ' return v;',
  4783. ' }, set: function (w) {',
  4784. ' v = w;',
  4785. ' }});',
  4786. ' $mod.Inc2({',
  4787. ' a: v,',
  4788. ' p: p,',
  4789. ' get: function () {',
  4790. ' return this.p[this.a];',
  4791. ' },',
  4792. ' set: function (v) {',
  4793. ' this.p[this.a] = v;',
  4794. ' }',
  4795. ' });',
  4796. '};',
  4797. '']),
  4798. LinesToStr([
  4799. '']));
  4800. end;
  4801. procedure TTestModule.TestProc_Overload;
  4802. begin
  4803. StartProgram(false);
  4804. Add('procedure DoIt(vI: longint); begin end;');
  4805. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4806. Add('procedure DoIt(vD: double); begin end;');
  4807. Add('begin');
  4808. Add(' DoIt(1);');
  4809. Add(' DoIt(2,3);');
  4810. Add(' DoIt(4.5);');
  4811. ConvertProgram;
  4812. CheckSource('TestProcedureOverload',
  4813. LinesToStr([ // statements
  4814. 'this.DoIt = function (vI) {',
  4815. '};',
  4816. 'this.DoIt$1 = function (vI, vJ) {',
  4817. '};',
  4818. 'this.DoIt$2 = function (vD) {',
  4819. '};',
  4820. '']),
  4821. LinesToStr([
  4822. '$mod.DoIt(1);',
  4823. '$mod.DoIt$1(2, 3);',
  4824. '$mod.DoIt$2(4.5);',
  4825. '']));
  4826. end;
  4827. procedure TTestModule.TestProc_OverloadForward;
  4828. begin
  4829. StartProgram(false);
  4830. Add('procedure DoIt(vI: longint); forward;');
  4831. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4832. Add('procedure doit(vi: longint); begin end;');
  4833. Add('begin');
  4834. Add(' doit(1);');
  4835. Add(' doit(2,3);');
  4836. ConvertProgram;
  4837. CheckSource('TestProcedureOverloadForward',
  4838. LinesToStr([ // statements
  4839. 'this.DoIt$1 = function (vI, vJ) {',
  4840. '};',
  4841. 'this.DoIt = function (vI) {',
  4842. '};',
  4843. '']),
  4844. LinesToStr([
  4845. '$mod.DoIt(1);',
  4846. '$mod.DoIt$1(2, 3);',
  4847. '']));
  4848. end;
  4849. procedure TTestModule.TestProc_OverloadIntfImpl;
  4850. begin
  4851. StartUnit(false);
  4852. Add('interface');
  4853. Add('procedure DoIt(vI: longint);');
  4854. Add('procedure DoIt(vI, vJ: longint);');
  4855. Add('implementation');
  4856. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4857. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4858. Add('procedure DoIt(vi: longint); begin end;');
  4859. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4860. Add('procedure DoIt(vi, vj: longint); begin end;');
  4861. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4862. Add('begin');
  4863. Add(' doit(1);');
  4864. Add(' doit(2,3);');
  4865. Add(' doit(4,5,6);');
  4866. Add(' doit(7,8,9,10);');
  4867. Add(' doit(11,12,13,14,15);');
  4868. ConvertUnit;
  4869. CheckSource('TestProcedureOverloadUnit',
  4870. LinesToStr([ // statements
  4871. 'var $impl = $mod.$impl;',
  4872. 'this.DoIt = function (vI) {',
  4873. '};',
  4874. 'this.DoIt$1 = function (vI, vJ) {',
  4875. '};',
  4876. '']),
  4877. LinesToStr([ // this.$init
  4878. '$mod.DoIt(1);',
  4879. '$mod.DoIt$1(2, 3);',
  4880. '$impl.DoIt$3(4,5,6);',
  4881. '$impl.DoIt$4(7,8,9,10);',
  4882. '$impl.DoIt$2(11,12,13,14,15);',
  4883. '']),
  4884. LinesToStr([ // implementation
  4885. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4886. '};',
  4887. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4888. '};',
  4889. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4890. '};',
  4891. '']));
  4892. end;
  4893. procedure TTestModule.TestProc_OverloadNested;
  4894. begin
  4895. StartProgram(false);
  4896. Add([
  4897. 'procedure doit(vA: longint);',
  4898. ' procedure DoIt(vA, vB: longint); overload;',
  4899. ' begin',
  4900. ' doit(1);',
  4901. ' doit(1,2);',
  4902. ' end;',
  4903. ' procedure doit(vA, vB, vC: longint);',
  4904. ' begin',
  4905. ' doit(1);',
  4906. ' doit(1,2);',
  4907. ' doit(1,2,3);',
  4908. ' end;',
  4909. 'begin',
  4910. ' doit(1);',
  4911. ' doit(1,2);',
  4912. ' doit(1,2,3);',
  4913. 'end;',
  4914. 'begin // main',
  4915. ' doit(1);']);
  4916. ConvertProgram;
  4917. CheckSource('TestProcedureOverloadNested',
  4918. LinesToStr([ // statements
  4919. 'this.doit = function (vA) {',
  4920. ' function DoIt$1(vA, vB) {',
  4921. ' $mod.doit(1);',
  4922. ' DoIt$1(1, 2);',
  4923. ' };',
  4924. ' function doit$2(vA, vB, vC) {',
  4925. ' $mod.doit(1);',
  4926. ' DoIt$1(1, 2);',
  4927. ' doit$2(1, 2, 3);',
  4928. ' };',
  4929. ' $mod.doit(1);',
  4930. ' DoIt$1(1, 2);',
  4931. ' doit$2(1, 2, 3);',
  4932. '};',
  4933. '']),
  4934. LinesToStr([
  4935. '$mod.doit(1);',
  4936. '']));
  4937. end;
  4938. procedure TTestModule.TestProc_OverloadNestedForward;
  4939. begin
  4940. StartProgram(false);
  4941. Add([
  4942. 'procedure DoIt(vA: longint); overload; forward;',
  4943. 'procedure DoIt(vB, vC: longint); overload;',
  4944. 'begin // 2 param overload',
  4945. ' doit(1);',
  4946. ' doit(1,2);',
  4947. 'end;',
  4948. 'procedure doit(vA: longint);',
  4949. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4950. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4951. ' begin // 4 param overload',
  4952. ' doit(1);',
  4953. ' doit(1,2);',
  4954. ' doit(1,2,3);',
  4955. ' doit(1,2,3,4);',
  4956. ' end;',
  4957. ' procedure doit(vA, vB, vC: longint);',
  4958. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4959. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4960. ' begin // 6 param overload',
  4961. ' doit(1);',
  4962. ' doit(1,2);',
  4963. ' doit(1,2,3);',
  4964. ' doit(1,2,3,4);',
  4965. ' doit(1,2,3,4,5);',
  4966. ' doit(1,2,3,4,5,6);',
  4967. ' end;',
  4968. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4969. ' begin // 5 param overload',
  4970. ' doit(1);',
  4971. ' doit(1,2);',
  4972. ' doit(1,2,3);',
  4973. ' doit(1,2,3,4);',
  4974. ' doit(1,2,3,4,5);',
  4975. ' doit(1,2,3,4,5,6);',
  4976. ' end;',
  4977. ' begin // 3 param overload',
  4978. ' doit(1);',
  4979. ' doit(1,2);',
  4980. ' doit(1,2,3);',
  4981. ' doit(1,2,3,4);',
  4982. ' doit(1,2,3,4,5);',
  4983. ' doit(1,2,3,4,5,6);',
  4984. ' end;',
  4985. 'begin // 1 param overload',
  4986. ' doit(1);',
  4987. ' doit(1,2);',
  4988. ' doit(1,2,3);',
  4989. ' doit(1,2,3,4);',
  4990. 'end;',
  4991. 'begin // main',
  4992. ' doit(1);',
  4993. ' doit(1,2);']);
  4994. ConvertProgram;
  4995. CheckSource('TestProc_OverloadNestedForward',
  4996. LinesToStr([ // statements
  4997. 'this.DoIt$1 = function (vB, vC) {',
  4998. ' $mod.DoIt(1);',
  4999. ' $mod.DoIt$1(1, 2);',
  5000. '};',
  5001. 'this.DoIt = function (vA) {',
  5002. ' function DoIt$3(vA, vB, vC, vD) {',
  5003. ' $mod.DoIt(1);',
  5004. ' $mod.DoIt$1(1, 2);',
  5005. ' DoIt$2(1, 2, 3);',
  5006. ' DoIt$3(1, 2, 3, 4);',
  5007. ' };',
  5008. ' function DoIt$2(vA, vB, vC) {',
  5009. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5010. ' $mod.DoIt(1);',
  5011. ' $mod.DoIt$1(1, 2);',
  5012. ' DoIt$2(1, 2, 3);',
  5013. ' DoIt$3(1, 2, 3, 4);',
  5014. ' DoIt$4(1, 2, 3, 4, 5);',
  5015. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5016. ' };',
  5017. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5018. ' $mod.DoIt(1);',
  5019. ' $mod.DoIt$1(1, 2);',
  5020. ' DoIt$2(1, 2, 3);',
  5021. ' DoIt$3(1, 2, 3, 4);',
  5022. ' DoIt$4(1, 2, 3, 4, 5);',
  5023. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5024. ' };',
  5025. ' $mod.DoIt(1);',
  5026. ' $mod.DoIt$1(1, 2);',
  5027. ' DoIt$2(1, 2, 3);',
  5028. ' DoIt$3(1, 2, 3, 4);',
  5029. ' DoIt$4(1, 2, 3, 4, 5);',
  5030. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5031. ' };',
  5032. ' $mod.DoIt(1);',
  5033. ' $mod.DoIt$1(1, 2);',
  5034. ' DoIt$2(1, 2, 3);',
  5035. ' DoIt$3(1, 2, 3, 4);',
  5036. '};',
  5037. '']),
  5038. LinesToStr([
  5039. '$mod.DoIt(1);',
  5040. '$mod.DoIt$1(1, 2);',
  5041. '']));
  5042. end;
  5043. procedure TTestModule.TestProc_OverloadUnitCycle;
  5044. begin
  5045. AddModuleWithIntfImplSrc('Unit2.pas',
  5046. LinesToStr([
  5047. 'type',
  5048. ' TObject = class',
  5049. ' procedure DoIt(b: boolean); virtual; abstract;',
  5050. ' procedure DoIt(i: longint); virtual; abstract;',
  5051. ' end;',
  5052. '']),
  5053. 'uses test1;');
  5054. StartUnit(true);
  5055. Add([
  5056. 'interface',
  5057. 'uses unit2;',
  5058. 'type',
  5059. ' TEagle = class(TObject)',
  5060. ' procedure DoIt(b: boolean); override;',
  5061. ' procedure DoIt(i: longint); override;',
  5062. ' end;',
  5063. 'implementation',
  5064. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5065. 'procedure TEagle.DoIt(i: longint); begin end;',
  5066. '']);
  5067. ConvertUnit;
  5068. CheckSource('TestProc_OverloadUnitCycle',
  5069. LinesToStr([ // statements
  5070. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5071. ' this.DoIt = function (b) {',
  5072. ' };',
  5073. ' this.DoIt$1 = function (i) {',
  5074. ' };',
  5075. '});',
  5076. '']),
  5077. '',
  5078. LinesToStr([
  5079. '']));
  5080. end;
  5081. procedure TTestModule.TestProc_Varargs;
  5082. begin
  5083. StartProgram(false);
  5084. Add([
  5085. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5086. 'procedure ProcB; varargs; external name ''ProcB'';',
  5087. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5088. 'function GetIt: longint; begin end;',
  5089. 'begin',
  5090. ' ProcA(1);',
  5091. ' ProcA(1,2);',
  5092. ' ProcA(1,2.0);',
  5093. ' ProcA(1,2,3);',
  5094. ' ProcA(1,''2'');',
  5095. ' ProcA(2,'''');',
  5096. ' ProcA(3,false);',
  5097. ' ProcB;',
  5098. ' ProcB();',
  5099. ' ProcB(4);',
  5100. ' ProcB(''foo'');',
  5101. ' ProcC;',
  5102. ' ProcC();',
  5103. ' ProcC(4);',
  5104. ' ProcC(5,''foo'');',
  5105. ' ProcB(GetIt);',
  5106. ' ProcB(GetIt());',
  5107. ' ProcB(GetIt,GetIt());']);
  5108. ConvertProgram;
  5109. CheckSource('TestProc_Varargs',
  5110. LinesToStr([ // statements
  5111. 'this.GetIt = function () {',
  5112. ' var Result = 0;',
  5113. ' return Result;',
  5114. '};',
  5115. '']),
  5116. LinesToStr([
  5117. 'ProcA(1);',
  5118. 'ProcA(1, 2);',
  5119. 'ProcA(1, 2.0);',
  5120. 'ProcA(1, 2, 3);',
  5121. 'ProcA(1, "2");',
  5122. 'ProcA(2, "");',
  5123. 'ProcA(3, false);',
  5124. 'ProcB();',
  5125. 'ProcB();',
  5126. 'ProcB(4);',
  5127. 'ProcB("foo");',
  5128. 'ProcC(17);',
  5129. 'ProcC(17);',
  5130. 'ProcC(4);',
  5131. 'ProcC(5, "foo");',
  5132. 'ProcB($mod.GetIt());',
  5133. 'ProcB($mod.GetIt());',
  5134. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5135. '']));
  5136. end;
  5137. procedure TTestModule.TestProc_ConstOrder;
  5138. begin
  5139. StartProgram(false);
  5140. Add([
  5141. 'const A = 3;',
  5142. 'const B = A+1;',
  5143. 'procedure DoIt;',
  5144. 'const C = A+1;',
  5145. 'const D = B+1;',
  5146. 'const E = D+C+B+A;',
  5147. 'begin',
  5148. 'end;',
  5149. 'begin'
  5150. ]);
  5151. ConvertProgram;
  5152. CheckSource('TestProc_ConstOrder',
  5153. LinesToStr([ // statements
  5154. 'this.A = 3;',
  5155. 'this.B = 3 + 1;',
  5156. 'var C = 3 + 1;',
  5157. 'var D = 4 + 1;',
  5158. 'var E = 5 + 4 + 4 + 3;',
  5159. 'this.DoIt = function () {',
  5160. '};',
  5161. '']),
  5162. LinesToStr([
  5163. ''
  5164. ]));
  5165. end;
  5166. procedure TTestModule.TestProc_DuplicateConst;
  5167. begin
  5168. StartProgram(false);
  5169. Add([
  5170. 'const A = 1;',
  5171. 'procedure DoIt;',
  5172. 'const A = 2;',
  5173. ' procedure SubIt;',
  5174. ' const A = 21;',
  5175. ' begin',
  5176. ' end;',
  5177. 'begin',
  5178. 'end;',
  5179. 'procedure DoSome;',
  5180. 'const A = 3;',
  5181. 'begin',
  5182. 'end;',
  5183. 'begin'
  5184. ]);
  5185. ConvertProgram;
  5186. CheckSource('TestProc_DuplicateConst',
  5187. LinesToStr([ // statements
  5188. 'this.A = 1;',
  5189. 'var A$1 = 2;',
  5190. 'var A$2 = 21;',
  5191. 'this.DoIt = function () {',
  5192. ' function SubIt() {',
  5193. ' };',
  5194. '};',
  5195. 'var A$3 = 3;',
  5196. 'this.DoSome = function () {',
  5197. '};',
  5198. '']),
  5199. LinesToStr([
  5200. ''
  5201. ]));
  5202. end;
  5203. procedure TTestModule.TestProc_LocalVarAbsolute;
  5204. begin
  5205. StartProgram(false);
  5206. Add([
  5207. 'type',
  5208. ' TObject = class',
  5209. ' Index: longint;',
  5210. ' procedure DoAbs(Item: pointer);',
  5211. ' end;',
  5212. 'procedure TObject.DoAbs(Item: pointer);',
  5213. 'var',
  5214. ' o: TObject absolute Item;',
  5215. 'begin',
  5216. ' if o.Index<o.Index then o.Index:=o.Index;',
  5217. 'end;',
  5218. 'procedure DoIt(i: longint; p: pointer);',
  5219. 'var',
  5220. ' d: double absolute i;',
  5221. ' s: string absolute d;',
  5222. ' oi: TObject absolute i;',
  5223. ' op: TObject absolute p;',
  5224. 'begin',
  5225. ' if d=d then d:=d;',
  5226. ' if s=s then s:=s;',
  5227. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5228. ' if op.Index=op.Index then op.Index:=op.Index;',
  5229. 'end;',
  5230. 'begin']);
  5231. ConvertProgram;
  5232. CheckSource('TestProc_LocalVarAbsolute',
  5233. LinesToStr([ // statements
  5234. 'rtl.createClass(this, "TObject", null, function () {',
  5235. ' this.$init = function () {',
  5236. ' this.Index = 0;',
  5237. ' };',
  5238. ' this.$final = function () {',
  5239. ' };',
  5240. ' this.DoAbs = function (Item) {',
  5241. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5242. ' };',
  5243. '});',
  5244. 'this.DoIt = function (i, p) {',
  5245. ' if (i === i) i = i;',
  5246. ' if (i === i) i = i;',
  5247. ' if (i.Index < i.Index) i.Index = i.Index;',
  5248. ' if (p.Index === p.Index) p.Index = p.Index;',
  5249. '};'
  5250. ]),
  5251. LinesToStr([
  5252. ]));
  5253. end;
  5254. procedure TTestModule.TestProc_ResultAbsolute;
  5255. begin
  5256. StartProgram(false);
  5257. Add([
  5258. 'type',
  5259. ' TObject = class',
  5260. ' Index: longint;',
  5261. ' function DoAbs: pointer;',
  5262. ' end;',
  5263. 'function TObject.DoAbs: pointer;',
  5264. 'var',
  5265. ' o: TObject absolute Result;',
  5266. 'begin',
  5267. ' if o.Index<o.Index then o.Index:=o.Index;',
  5268. 'end;',
  5269. 'function DoIt: jsvalue;',
  5270. 'var',
  5271. ' d: double absolute Result;',
  5272. ' s: string absolute Result;',
  5273. ' o: TObject absolute Result;',
  5274. 'begin',
  5275. ' if d=d then d:=d;',
  5276. ' if s=s then s:=s;',
  5277. ' if o.Index<o.Index then o.Index:=o.Index;',
  5278. 'end;',
  5279. 'begin']);
  5280. ConvertProgram;
  5281. CheckSource('TestProc_ResultAbsolute',
  5282. LinesToStr([ // statements
  5283. 'rtl.createClass(this, "TObject", null, function () {',
  5284. ' this.$init = function () {',
  5285. ' this.Index = 0;',
  5286. ' };',
  5287. ' this.$final = function () {',
  5288. ' };',
  5289. ' this.DoAbs = function () {',
  5290. ' var Result = null;',
  5291. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5292. ' return Result;',
  5293. ' };',
  5294. '});',
  5295. 'this.DoIt = function () {',
  5296. ' var Result = undefined;',
  5297. ' if (Result === Result) Result = Result;',
  5298. ' if (Result === Result) Result = Result;',
  5299. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5300. ' return Result;',
  5301. '};',
  5302. '']),
  5303. LinesToStr([
  5304. ]));
  5305. end;
  5306. procedure TTestModule.TestProc_LocalVarInit;
  5307. begin
  5308. StartProgram(false);
  5309. Add([
  5310. 'type TBytes = array of byte;',
  5311. 'procedure DoIt;',
  5312. 'const c = 4;',
  5313. 'var',
  5314. ' b: byte = 1;',
  5315. ' w: word = 2+c;',
  5316. ' p: pointer = nil;',
  5317. ' Buffer: TBytes = nil;',
  5318. 'begin',
  5319. 'end;',
  5320. 'begin']);
  5321. ConvertProgram;
  5322. CheckSource('TestProc_LocalVarInit',
  5323. LinesToStr([ // statements
  5324. 'var c = 4;',
  5325. 'this.DoIt = function () {',
  5326. ' var b = 1;',
  5327. ' var w = 2 + 4;',
  5328. ' var p = null;',
  5329. ' var Buffer = [];',
  5330. '};',
  5331. '']),
  5332. LinesToStr([
  5333. ]));
  5334. end;
  5335. procedure TTestModule.TestProc_ReservedWords;
  5336. begin
  5337. StartProgram(false);
  5338. Add([
  5339. 'procedure Date(ArrayBuffer: longint);',
  5340. 'const',
  5341. ' NaN: longint = 3;',
  5342. 'var',
  5343. ' &Boolean: longint;',
  5344. ' procedure Error(ArrayBuffer: longint);',
  5345. ' begin',
  5346. ' end;',
  5347. 'begin',
  5348. ' Nan:=&bOolean;',
  5349. 'end;',
  5350. 'begin',
  5351. ' Date(1);']);
  5352. ConvertProgram;
  5353. CheckSource('TestProc_ReservedWords',
  5354. LinesToStr([ // statements
  5355. 'var naN = 3;',
  5356. 'this.Date = function (arrayBuffer) {',
  5357. ' var boolean = 0;',
  5358. ' function error(arrayBuffer) {',
  5359. ' };',
  5360. ' naN = boolean;',
  5361. '};',
  5362. '']),
  5363. LinesToStr([
  5364. ' $mod.Date(1);'
  5365. ]));
  5366. end;
  5367. procedure TTestModule.TestProc_ConstRefWord;
  5368. begin
  5369. StartProgram(false);
  5370. Add([
  5371. 'procedure Run(constref w: word);',
  5372. 'var l: word;',
  5373. 'begin',
  5374. ' l:=w;',
  5375. ' Run(w);',
  5376. ' Run(l);',
  5377. 'end;',
  5378. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5379. 'begin',
  5380. ' Run(a);',
  5381. ' Run(b);',
  5382. ' Run(c);',
  5383. ' Run(d);',
  5384. ' Run(e);',
  5385. 'end;',
  5386. 'begin',
  5387. ' Run(1);']);
  5388. ConvertProgram;
  5389. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5390. CheckSource('TestProc_ConstRefWord',
  5391. LinesToStr([ // statements
  5392. 'this.Run = function (w) {',
  5393. ' var l = 0;',
  5394. ' l = w;',
  5395. ' $mod.Run(w);',
  5396. ' $mod.Run(l);',
  5397. '};',
  5398. 'this.Fly = function (a, b, c, d, e) {',
  5399. ' $mod.Run(a);',
  5400. ' $mod.Run(b.get());',
  5401. ' $mod.Run(c.get());',
  5402. ' $mod.Run(d);',
  5403. ' $mod.Run(e);',
  5404. '};',
  5405. '']),
  5406. LinesToStr([
  5407. '$mod.Run(1);'
  5408. ]));
  5409. end;
  5410. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5411. begin
  5412. StartProgram(false);
  5413. Add([
  5414. '{$mode objfpc}',
  5415. 'type',
  5416. ' TFunc = reference to function(x: word): word;',
  5417. 'var Func: TFunc;',
  5418. 'procedure DoIt(a: word);',
  5419. 'begin',
  5420. ' Func:=function(b:word): word',
  5421. ' begin',
  5422. ' Result:=a+b;',
  5423. ' exit(b);',
  5424. ' exit(Result);',
  5425. ' end;',// test semicolon
  5426. ' a:=3;',
  5427. 'end;',
  5428. 'begin',
  5429. ' Func:=function(c:word):word begin',
  5430. ' Result:=3+c;',
  5431. ' exit(c);',
  5432. ' exit(Result);',
  5433. ' end;']);
  5434. ConvertProgram;
  5435. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5436. LinesToStr([ // statements
  5437. 'this.Func = null;',
  5438. 'this.DoIt = function (a) {',
  5439. ' $mod.Func = function (b) {',
  5440. ' var Result = 0;',
  5441. ' Result = a + b;',
  5442. ' return b;',
  5443. ' return Result;',
  5444. ' return Result;',
  5445. ' };',
  5446. ' a = 3;',
  5447. '};',
  5448. '']),
  5449. LinesToStr([
  5450. '$mod.Func = function (c) {',
  5451. ' var Result = 0;',
  5452. ' Result = 3 + c;',
  5453. ' return c;',
  5454. ' return Result;',
  5455. ' return Result;',
  5456. '};',
  5457. '']));
  5458. end;
  5459. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5460. begin
  5461. StartProgram(false);
  5462. Add([
  5463. '{$mode delphi}',
  5464. 'type',
  5465. ' TProc = reference to procedure(x: word);',
  5466. 'procedure DoIt(a: word);',
  5467. 'var Proc: TProc;',
  5468. 'begin',
  5469. ' Proc:=procedure(b:word) begin end;',
  5470. 'end;',
  5471. 'var Proc: TProc;',
  5472. 'begin',
  5473. ' Proc:=procedure(c:word) begin end;',
  5474. '']);
  5475. ConvertProgram;
  5476. CheckSource('TestAnonymousProc_Assign_Delphi',
  5477. LinesToStr([ // statements
  5478. 'this.DoIt = function (a) {',
  5479. ' var Proc = null;',
  5480. ' Proc = function (b) {',
  5481. ' };',
  5482. '};',
  5483. 'this.Proc = null;',
  5484. '']),
  5485. LinesToStr([
  5486. '$mod.Proc = function (c) {',
  5487. '};',
  5488. '']));
  5489. end;
  5490. procedure TTestModule.TestAnonymousProc_Arg;
  5491. begin
  5492. StartProgram(false);
  5493. Add([
  5494. 'type',
  5495. ' TProc = reference to procedure;',
  5496. ' TFunc = reference to function(x: word): word;',
  5497. 'procedure DoMore(f,g: TProc);',
  5498. 'begin',
  5499. 'end;',
  5500. 'procedure DoOdd(v: jsvalue);',
  5501. 'begin',
  5502. 'end;',
  5503. 'procedure DoIt(f: TFunc);',
  5504. 'begin',
  5505. ' DoIt(function(b:word): word',
  5506. ' begin',
  5507. ' Result:=1+b;',
  5508. ' end);',
  5509. ' DoMore(procedure begin end, procedure begin end);',
  5510. ' DoOdd(procedure begin end);',
  5511. 'end;',
  5512. 'begin',
  5513. ' DoMore(procedure begin end,',
  5514. ' procedure assembler asm',
  5515. ' console.log("c");',
  5516. ' end);',
  5517. '']);
  5518. ConvertProgram;
  5519. CheckSource('TestAnonymousProc_Arg',
  5520. LinesToStr([ // statements
  5521. 'this.DoMore = function (f, g) {',
  5522. '};',
  5523. 'this.DoOdd = function (v) {',
  5524. '};',
  5525. 'this.DoIt = function (f) {',
  5526. ' $mod.DoIt(function (b) {',
  5527. ' var Result = 0;',
  5528. ' Result = 1 + b;',
  5529. ' return Result;',
  5530. ' });',
  5531. ' $mod.DoMore(function () {',
  5532. ' }, function () {',
  5533. ' });',
  5534. ' $mod.DoOdd(function () {',
  5535. ' });',
  5536. '};',
  5537. '']),
  5538. LinesToStr([
  5539. '$mod.DoMore(function () {',
  5540. '}, function () {',
  5541. ' console.log("c");',
  5542. '});',
  5543. '']));
  5544. end;
  5545. procedure TTestModule.TestAnonymousProc_Typecast;
  5546. begin
  5547. StartProgram(false);
  5548. Add([
  5549. 'type',
  5550. ' TProc = reference to procedure(w: word);',
  5551. ' TArr = array of word;',
  5552. ' TFuncArr = reference to function: TArr;',
  5553. 'procedure DoIt(p: TProc);',
  5554. 'var',
  5555. ' w: word;',
  5556. ' a: TArr;',
  5557. 'begin',
  5558. ' p:=TProc(procedure(b: smallint) begin end);',
  5559. ' a:=TFuncArr(function: TArr begin end)();',
  5560. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5561. 'end;',
  5562. 'begin']);
  5563. ConvertProgram;
  5564. CheckSource('TestAnonymousProc_Typecast',
  5565. LinesToStr([ // statements
  5566. 'this.DoIt = function (p) {',
  5567. ' var w = 0;',
  5568. ' var a = [];',
  5569. ' p = function (b) {',
  5570. ' };',
  5571. ' a = function () {',
  5572. ' var Result = [];',
  5573. ' return Result;',
  5574. ' }();',
  5575. ' w = function () {',
  5576. ' var Result = [];',
  5577. ' return Result;',
  5578. ' }()[3];',
  5579. '};',
  5580. '']),
  5581. LinesToStr([
  5582. '']));
  5583. end;
  5584. procedure TTestModule.TestAnonymousProc_With;
  5585. begin
  5586. StartProgram(false);
  5587. Add([
  5588. 'type',
  5589. ' TProc = reference to procedure(w: word);',
  5590. ' TObject = class',
  5591. ' b: boolean;',
  5592. ' end;',
  5593. 'var',
  5594. ' p: TProc;',
  5595. ' bird: TObject;',
  5596. 'begin',
  5597. ' with bird do',
  5598. ' p:=procedure(w: word)',
  5599. ' begin',
  5600. ' b:=w>2;',
  5601. ' end;',
  5602. '']);
  5603. ConvertProgram;
  5604. CheckSource('TestAnonymousProc_With',
  5605. LinesToStr([ // statements
  5606. 'rtl.createClass(this, "TObject", null, function () {',
  5607. ' this.$init = function () {',
  5608. ' this.b = false;',
  5609. ' };',
  5610. ' this.$final = function () {',
  5611. ' };',
  5612. '});',
  5613. 'this.p = null;',
  5614. 'this.bird = null;',
  5615. '']),
  5616. LinesToStr([
  5617. 'var $with = $mod.bird;',
  5618. '$mod.p = function (w) {',
  5619. ' $with.b = w > 2;',
  5620. '};',
  5621. '']));
  5622. end;
  5623. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5624. begin
  5625. StartProgram(false);
  5626. Add([
  5627. 'type',
  5628. ' TProc = reference to procedure;',
  5629. ' TObject = class',
  5630. ' b: boolean;',
  5631. ' end;',
  5632. 'procedure DoIt;',
  5633. 'var',
  5634. ' p: TProc;',
  5635. 'begin',
  5636. ' try',
  5637. ' except',
  5638. ' on E: TObject do',
  5639. ' p:=procedure',
  5640. ' begin',
  5641. ' E.b:=true;',
  5642. ' end;',
  5643. ' end;',
  5644. 'end;',
  5645. 'begin']);
  5646. ConvertProgram;
  5647. CheckSource('TestAnonymousProc_ExceptOn',
  5648. LinesToStr([ // statements
  5649. 'rtl.createClass(this, "TObject", null, function () {',
  5650. ' this.$init = function () {',
  5651. ' this.b = false;',
  5652. ' };',
  5653. ' this.$final = function () {',
  5654. ' };',
  5655. '});',
  5656. 'this.DoIt = function () {',
  5657. ' var p = null;',
  5658. ' try {} catch ($e) {',
  5659. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5660. ' var E = $e;',
  5661. ' p = function () {',
  5662. ' E.b = true;',
  5663. ' };',
  5664. ' } else throw $e',
  5665. ' };',
  5666. '};',
  5667. '']),
  5668. LinesToStr([
  5669. '']));
  5670. end;
  5671. procedure TTestModule.TestAnonymousProc_Nested;
  5672. begin
  5673. StartProgram(false);
  5674. Add([
  5675. 'type',
  5676. ' TProc = reference to procedure;',
  5677. ' TObject = class',
  5678. ' i: byte;',
  5679. ' procedure DoIt;',
  5680. ' end;',
  5681. 'procedure TObject.DoIt;',
  5682. 'var',
  5683. ' p: TProc;',
  5684. ' procedure Sub;',
  5685. ' begin',
  5686. ' p:=procedure',
  5687. ' begin',
  5688. ' i:=3;',
  5689. ' Self.i:=4;',
  5690. ' p:=procedure',
  5691. ' procedure SubSub;',
  5692. ' begin',
  5693. ' i:=13;',
  5694. ' Self.i:=14;',
  5695. ' end;',
  5696. ' begin',
  5697. ' i:=13;',
  5698. ' Self.i:=14;',
  5699. ' end;',
  5700. ' end;',
  5701. ' end;',
  5702. 'begin',
  5703. 'end;',
  5704. 'begin']);
  5705. ConvertProgram;
  5706. CheckSource('TestAnonymousProc_Nested',
  5707. LinesToStr([ // statements
  5708. 'rtl.createClass(this, "TObject", null, function () {',
  5709. ' this.$init = function () {',
  5710. ' this.i = 0;',
  5711. ' };',
  5712. ' this.$final = function () {',
  5713. ' };',
  5714. ' this.DoIt = function () {',
  5715. ' var $Self = this;',
  5716. ' var p = null;',
  5717. ' function Sub() {',
  5718. ' p = function () {',
  5719. ' $Self.i = 3;',
  5720. ' $Self.i = 4;',
  5721. ' p = function () {',
  5722. ' function SubSub() {',
  5723. ' $Self.i = 13;',
  5724. ' $Self.i = 14;',
  5725. ' };',
  5726. ' $Self.i = 13;',
  5727. ' $Self.i = 14;',
  5728. ' };',
  5729. ' };',
  5730. ' };',
  5731. ' };',
  5732. '});',
  5733. '']),
  5734. LinesToStr([
  5735. '']));
  5736. end;
  5737. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5738. begin
  5739. StartProgram(false);
  5740. Add([
  5741. 'type',
  5742. ' TProc = reference to procedure;',
  5743. 'function DoIt: TProc;',
  5744. ' function Sub: TProc;',
  5745. ' begin',
  5746. ' Result:=procedure',
  5747. ' begin',
  5748. ' Sub:=procedure',
  5749. ' procedure SubSub;',
  5750. ' begin',
  5751. ' Result:=nil;',
  5752. ' Sub:=nil;',
  5753. ' DoIt:=nil;',
  5754. ' end;',
  5755. ' begin',
  5756. ' Result:=nil;',
  5757. ' Sub:=nil;',
  5758. ' DoIt:=nil;',
  5759. ' end;',
  5760. ' end;',
  5761. ' end;',
  5762. 'begin',
  5763. 'end;',
  5764. 'begin']);
  5765. ConvertProgram;
  5766. CheckSource('TestAnonymousProc_NestedAssignResult',
  5767. LinesToStr([ // statements
  5768. 'this.DoIt = function () {',
  5769. ' var Result = null;',
  5770. ' function Sub() {',
  5771. ' var Result$1 = null;',
  5772. ' Result$1 = function () {',
  5773. ' Result$1 = function () {',
  5774. ' function SubSub() {',
  5775. ' Result$1 = null;',
  5776. ' Result$1 = null;',
  5777. ' Result = null;',
  5778. ' };',
  5779. ' Result$1 = null;',
  5780. ' Result$1 = null;',
  5781. ' Result = null;',
  5782. ' };',
  5783. ' };',
  5784. ' return Result$1;',
  5785. ' };',
  5786. ' return Result;',
  5787. '};',
  5788. '']),
  5789. LinesToStr([
  5790. '']));
  5791. end;
  5792. procedure TTestModule.TestAnonymousProc_Class;
  5793. begin
  5794. StartProgram(false);
  5795. Add([
  5796. 'type',
  5797. ' TProc = reference to procedure;',
  5798. ' TEvent = procedure of object;',
  5799. ' TObject = class',
  5800. ' Size: word;',
  5801. ' function GetIt: TProc;',
  5802. ' procedure DoIt; virtual; abstract;',
  5803. ' end;',
  5804. 'function TObject.GetIt: TProc;',
  5805. 'begin',
  5806. ' Result:=procedure',
  5807. ' var p: TEvent;',
  5808. ' begin',
  5809. ' Size:=Size;',
  5810. ' Size:=Self.Size;',
  5811. ' p:=@DoIt;',
  5812. ' p:[email protected];',
  5813. ' end;',
  5814. 'end;',
  5815. 'begin']);
  5816. ConvertProgram;
  5817. CheckSource('TestAnonymousProc_Class',
  5818. LinesToStr([ // statements
  5819. 'rtl.createClass(this, "TObject", null, function () {',
  5820. ' this.$init = function () {',
  5821. ' this.Size = 0;',
  5822. ' };',
  5823. ' this.$final = function () {',
  5824. ' };',
  5825. ' this.GetIt = function () {',
  5826. ' var $Self = this;',
  5827. ' var Result = null;',
  5828. ' Result = function () {',
  5829. ' var p = null;',
  5830. ' $Self.Size = $Self.Size;',
  5831. ' $Self.Size = $Self.Size;',
  5832. ' p = rtl.createCallback($Self, "DoIt");',
  5833. ' p = rtl.createCallback($Self, "DoIt");',
  5834. ' };',
  5835. ' return Result;',
  5836. ' };',
  5837. '});',
  5838. '']),
  5839. LinesToStr([
  5840. '']));
  5841. end;
  5842. procedure TTestModule.TestAnonymousProc_ForLoop;
  5843. begin
  5844. StartProgram(false);
  5845. Add([
  5846. 'type TProc = reference to procedure;',
  5847. 'procedure Foo(p: TProc);',
  5848. 'begin',
  5849. 'end;',
  5850. 'procedure DoIt;',
  5851. 'var i: word;',
  5852. ' a: word;',
  5853. 'begin',
  5854. ' for i:=1 to 10 do begin',
  5855. ' Foo(procedure begin a:=3; end);',
  5856. ' end;',
  5857. 'end;',
  5858. 'begin',
  5859. ' DoIt;']);
  5860. ConvertProgram;
  5861. CheckSource('TestAnonymousProc_ForLoop',
  5862. LinesToStr([ // statements
  5863. 'this.Foo = function (p) {',
  5864. '};',
  5865. 'this.DoIt = function () {',
  5866. ' var i = 0;',
  5867. ' var a = 0;',
  5868. ' for (i = 1; i <= 10; i++) {',
  5869. ' $mod.Foo(function () {',
  5870. ' a = 3;',
  5871. ' });',
  5872. ' };',
  5873. '};',
  5874. '']),
  5875. LinesToStr([
  5876. '$mod.DoIt();'
  5877. ]));
  5878. end;
  5879. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5880. begin
  5881. StartProgram(false);
  5882. Add([
  5883. '{$mode delphi}',
  5884. 'type',
  5885. ' TProc = reference to procedure;',
  5886. ' TFunc = reference to function(x: word): word;',
  5887. 'procedure Run;',
  5888. 'asm',
  5889. 'end;',
  5890. 'procedure Walk(p: TProc; f: TFunc);',
  5891. 'begin',
  5892. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5893. 'end;',
  5894. 'begin',
  5895. ' Walk(procedure',
  5896. ' asm',
  5897. ' console.log("a");',
  5898. ' end,',
  5899. ' function(x: word): word asm',
  5900. ' console.log("c");',
  5901. ' end);',
  5902. '']);
  5903. ConvertProgram;
  5904. CheckSource('TestAnonymousProc_AsmDelphi',
  5905. LinesToStr([ // statements
  5906. 'this.Run = function () {',
  5907. '};',
  5908. 'this.Walk = function (p, f) {',
  5909. ' $mod.Walk(function () {',
  5910. ' }, function (b) {',
  5911. ' return 1+b;',
  5912. ' });',
  5913. '};',
  5914. '']),
  5915. LinesToStr([
  5916. '$mod.Walk(function () {',
  5917. ' console.log("a");',
  5918. '}, function (x) {',
  5919. ' console.log("c");',
  5920. '});',
  5921. '']));
  5922. end;
  5923. procedure TTestModule.TestEnum_Name;
  5924. begin
  5925. StartProgram(false);
  5926. Add('type TMyEnum = (Red, Green, Blue);');
  5927. Add('var e: TMyEnum;');
  5928. Add('var f: TMyEnum = Blue;');
  5929. Add('begin');
  5930. Add(' e:=green;');
  5931. Add(' e:=default(TMyEnum);');
  5932. ConvertProgram;
  5933. CheckSource('TestEnum_Name',
  5934. LinesToStr([ // statements
  5935. 'this.TMyEnum = {',
  5936. ' "0":"Red",',
  5937. ' Red:0,',
  5938. ' "1":"Green",',
  5939. ' Green:1,',
  5940. ' "2":"Blue",',
  5941. ' Blue:2',
  5942. ' };',
  5943. 'this.e = 0;',
  5944. 'this.f = this.TMyEnum.Blue;'
  5945. ]),
  5946. LinesToStr([
  5947. '$mod.e=$mod.TMyEnum.Green;',
  5948. '$mod.e=$mod.TMyEnum.Red;'
  5949. ]));
  5950. end;
  5951. procedure TTestModule.TestEnum_Number;
  5952. begin
  5953. Converter.Options:=Converter.Options+[coEnumNumbers];
  5954. StartProgram(false);
  5955. Add('type TMyEnum = (Red, Green);');
  5956. Add('var');
  5957. Add(' e: TMyEnum;');
  5958. Add(' f: TMyEnum = Green;');
  5959. Add(' i: longint;');
  5960. Add('begin');
  5961. Add(' e:=green;');
  5962. Add(' i:=longint(e);');
  5963. ConvertProgram;
  5964. CheckSource('TestEnumNumber',
  5965. LinesToStr([ // statements
  5966. 'this.TMyEnum = {',
  5967. ' "0":"Red",',
  5968. ' Red:0,',
  5969. ' "1":"Green",',
  5970. ' Green:1',
  5971. ' };',
  5972. 'this.e = 0;',
  5973. 'this.f = 1;',
  5974. 'this.i = 0;'
  5975. ]),
  5976. LinesToStr([
  5977. '$mod.e=1;',
  5978. '$mod.i=$mod.e;'
  5979. ]));
  5980. end;
  5981. procedure TTestModule.TestEnum_ConstFail;
  5982. begin
  5983. StartProgram(false);
  5984. Add([
  5985. 'type TMyEnum = (Red = 100, Green = 101);',
  5986. 'var',
  5987. ' e: TMyEnum;',
  5988. ' f: TMyEnum = Green;',
  5989. 'begin',
  5990. ' e:=green;']);
  5991. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5992. ConvertProgram;
  5993. end;
  5994. procedure TTestModule.TestEnum_Functions;
  5995. begin
  5996. StartProgram(false);
  5997. Add([
  5998. 'type TMyEnum = (Red, Green);',
  5999. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6000. 'var',
  6001. ' v: longint;',
  6002. ' s: string;',
  6003. 'begin',
  6004. ' val(s,e,v);',
  6005. ' val(s,e,i);',
  6006. 'end;',
  6007. 'var',
  6008. ' e: TMyEnum;',
  6009. ' i: longint;',
  6010. ' s: string;',
  6011. ' b: boolean;',
  6012. 'begin',
  6013. ' i:=ord(red);',
  6014. ' i:=ord(green);',
  6015. ' i:=ord(e);',
  6016. ' i:=ord(b);',
  6017. ' e:=low(tmyenum);',
  6018. ' e:=low(e);',
  6019. ' b:=low(boolean);',
  6020. ' e:=high(tmyenum);',
  6021. ' e:=high(e);',
  6022. ' b:=high(boolean);',
  6023. ' e:=pred(green);',
  6024. ' e:=pred(e);',
  6025. ' b:=pred(b);',
  6026. ' e:=succ(red);',
  6027. ' e:=succ(e);',
  6028. ' b:=succ(b);',
  6029. ' e:=tmyenum(1);',
  6030. ' e:=tmyenum(i);',
  6031. ' s:=str(e);',
  6032. ' str(e,s);',
  6033. ' str(red,s);',
  6034. ' s:=str(e:3);',
  6035. ' writestr(s,e:3,red);',
  6036. ' val(s,e,i);',
  6037. ' i:=longint(e);']);
  6038. ConvertProgram;
  6039. CheckSource('TestEnum_Functions',
  6040. LinesToStr([ // statements
  6041. 'this.TMyEnum = {',
  6042. ' "0":"Red",',
  6043. ' Red:0,',
  6044. ' "1":"Green",',
  6045. ' Green:1',
  6046. ' };',
  6047. 'this.DoIt = function (e, i) {',
  6048. ' var v = 0;',
  6049. ' var s = "";',
  6050. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6051. ' v = w;',
  6052. ' }));',
  6053. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6054. '};',
  6055. 'this.e = 0;',
  6056. 'this.i = 0;',
  6057. 'this.s = "";',
  6058. 'this.b = false;',
  6059. '']),
  6060. LinesToStr([
  6061. '$mod.i=$mod.TMyEnum.Red;',
  6062. '$mod.i=$mod.TMyEnum.Green;',
  6063. '$mod.i=$mod.e;',
  6064. '$mod.i=$mod.b+0;',
  6065. '$mod.e=$mod.TMyEnum.Red;',
  6066. '$mod.e=$mod.TMyEnum.Red;',
  6067. '$mod.b=false;',
  6068. '$mod.e=$mod.TMyEnum.Green;',
  6069. '$mod.e=$mod.TMyEnum.Green;',
  6070. '$mod.b=true;',
  6071. '$mod.e=$mod.TMyEnum.Green-1;',
  6072. '$mod.e=$mod.e-1;',
  6073. '$mod.b=false;',
  6074. '$mod.e=$mod.TMyEnum.Red+1;',
  6075. '$mod.e=$mod.e+1;',
  6076. '$mod.b=true;',
  6077. '$mod.e=1;',
  6078. '$mod.e=$mod.i;',
  6079. '$mod.s = $mod.TMyEnum[$mod.e];',
  6080. '$mod.s = $mod.TMyEnum[$mod.e];',
  6081. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6082. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6083. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6084. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6085. ' $mod.i = v;',
  6086. '});',
  6087. '$mod.i=$mod.e;',
  6088. '']));
  6089. end;
  6090. procedure TTestModule.TestEnumRg_Functions;
  6091. begin
  6092. StartProgram(false);
  6093. Add([
  6094. 'type',
  6095. ' TEnum = (Red, Green, Blue);',
  6096. ' TEnumRg = Green..Blue;',
  6097. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6098. 'var',
  6099. ' v: longint;',
  6100. ' s: string;',
  6101. 'begin',
  6102. ' val(s,e,v);',
  6103. ' val(s,e,i);',
  6104. 'end;',
  6105. 'var',
  6106. ' e: TEnumRg;',
  6107. ' i: longint;',
  6108. ' s: string;',
  6109. 'begin',
  6110. ' i:=ord(green);',
  6111. ' i:=ord(e);',
  6112. ' e:=low(tenumrg);',
  6113. ' e:=low(e);',
  6114. ' e:=high(tenumrg);',
  6115. ' e:=high(e);',
  6116. ' e:=pred(blue);',
  6117. ' e:=pred(e);',
  6118. ' e:=succ(green);',
  6119. ' e:=succ(e);',
  6120. ' e:=tenumrg(1);',
  6121. ' e:=tenumrg(i);',
  6122. ' s:=str(e);',
  6123. ' str(e,s);',
  6124. ' str(red,s);',
  6125. ' s:=str(e:3);',
  6126. ' writestr(s,e:3,blue);',
  6127. ' val(s,e,i);',
  6128. ' i:=longint(e);']);
  6129. ConvertProgram;
  6130. CheckSource('TestEnumRg_Functions',
  6131. LinesToStr([ // statements
  6132. 'this.TEnum = {',
  6133. ' "0":"Red",',
  6134. ' Red:0,',
  6135. ' "1":"Green",',
  6136. ' Green:1,',
  6137. ' "2":"Blue",',
  6138. ' Blue:2',
  6139. ' };',
  6140. 'this.DoIt = function (e, i) {',
  6141. ' var v = 0;',
  6142. ' var s = "";',
  6143. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6144. ' v = w;',
  6145. ' }));',
  6146. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6147. '};',
  6148. 'this.e = this.TEnum.Green;',
  6149. 'this.i = 0;',
  6150. 'this.s = "";',
  6151. '']),
  6152. LinesToStr([
  6153. '$mod.i=$mod.TEnum.Green;',
  6154. '$mod.i=$mod.e;',
  6155. '$mod.e=$mod.TEnum.Green;',
  6156. '$mod.e=$mod.TEnum.Green;',
  6157. '$mod.e=$mod.TEnum.Blue;',
  6158. '$mod.e=$mod.TEnum.Blue;',
  6159. '$mod.e=$mod.TEnum.Blue-1;',
  6160. '$mod.e=$mod.e-1;',
  6161. '$mod.e=$mod.TEnum.Green+1;',
  6162. '$mod.e=$mod.e+1;',
  6163. '$mod.e=1;',
  6164. '$mod.e=$mod.i;',
  6165. '$mod.s = $mod.TEnum[$mod.e];',
  6166. '$mod.s = $mod.TEnum[$mod.e];',
  6167. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6168. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6169. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6170. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6171. ' $mod.i = v;',
  6172. '});',
  6173. '$mod.i=$mod.e;',
  6174. '']));
  6175. end;
  6176. procedure TTestModule.TestEnum_AsParams;
  6177. begin
  6178. StartProgram(false);
  6179. Add('type TEnum = (Red,Blue);');
  6180. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6181. Add('var vJ: TEnum;');
  6182. Add('begin');
  6183. Add(' vg:=vg;');
  6184. Add(' vj:=vh;');
  6185. Add(' vi:=vi;');
  6186. Add(' doit(vg,vg,vg);');
  6187. Add(' doit(vh,vh,vj);');
  6188. Add(' doit(vi,vi,vi);');
  6189. Add(' doit(vj,vj,vj);');
  6190. Add('end;');
  6191. Add('var i: TEnum;');
  6192. Add('begin');
  6193. Add(' doit(i,i,i);');
  6194. ConvertProgram;
  6195. CheckSource('TestEnum_AsParams',
  6196. LinesToStr([ // statements
  6197. 'this.TEnum = {',
  6198. ' "0": "Red",',
  6199. ' Red: 0,',
  6200. ' "1": "Blue",',
  6201. ' Blue: 1',
  6202. '};',
  6203. 'this.DoIt = function (vG,vH,vI) {',
  6204. ' var vJ = 0;',
  6205. ' vG = vG;',
  6206. ' vJ = vH;',
  6207. ' vI.set(vI.get());',
  6208. ' $mod.DoIt(vG, vG, {',
  6209. ' get: function () {',
  6210. ' return vG;',
  6211. ' },',
  6212. ' set: function (v) {',
  6213. ' vG = v;',
  6214. ' }',
  6215. ' });',
  6216. ' $mod.DoIt(vH, vH, {',
  6217. ' get: function () {',
  6218. ' return vJ;',
  6219. ' },',
  6220. ' set: function (v) {',
  6221. ' vJ = v;',
  6222. ' }',
  6223. ' });',
  6224. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6225. ' $mod.DoIt(vJ, vJ, {',
  6226. ' get: function () {',
  6227. ' return vJ;',
  6228. ' },',
  6229. ' set: function (v) {',
  6230. ' vJ = v;',
  6231. ' }',
  6232. ' });',
  6233. '};',
  6234. 'this.i = 0;'
  6235. ]),
  6236. LinesToStr([
  6237. '$mod.DoIt($mod.i,$mod.i,{',
  6238. ' p: $mod,',
  6239. ' get: function () {',
  6240. ' return this.p.i;',
  6241. ' },',
  6242. ' set: function (v) {',
  6243. ' this.p.i = v;',
  6244. ' }',
  6245. '});'
  6246. ]));
  6247. end;
  6248. procedure TTestModule.TestEnumRange_Array;
  6249. begin
  6250. StartProgram(false);
  6251. Add([
  6252. 'type',
  6253. ' TEnum = (Red, Green, Blue);',
  6254. ' TEnumRg = green..blue;',
  6255. ' TArr = array[TEnumRg] of byte;',
  6256. ' TArr2 = array[green..blue] of byte;',
  6257. 'var',
  6258. ' a: TArr;',
  6259. ' b: TArr = (3,4);',
  6260. ' c: TArr2 = (5,6);',
  6261. 'begin',
  6262. ' a[green] := b[blue];',
  6263. ' c[green] := c[blue];',
  6264. '']);
  6265. ConvertProgram;
  6266. CheckSource('TestEnumRange_Array',
  6267. LinesToStr([ // statements
  6268. 'this.TEnum = {',
  6269. ' "0": "Red",',
  6270. ' Red: 0,',
  6271. ' "1": "Green",',
  6272. ' Green: 1,',
  6273. ' "2": "Blue",',
  6274. ' Blue: 2',
  6275. '};',
  6276. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6277. 'this.b = [3, 4];',
  6278. 'this.c = [5, 6];',
  6279. '']),
  6280. LinesToStr([
  6281. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6282. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6283. '']));
  6284. end;
  6285. procedure TTestModule.TestEnum_ForIn;
  6286. begin
  6287. StartProgram(false);
  6288. Add([
  6289. 'type',
  6290. ' TEnum = (Red, Green, Blue);',
  6291. ' TEnumRg = green..blue;',
  6292. ' TArr = array[TEnum] of byte;',
  6293. ' TArrRg = array[TEnumRg] of byte;',
  6294. 'var',
  6295. ' e: TEnum;',
  6296. ' a1: TArr = (3,4,5);',
  6297. ' a2: TArrRg = (11,12);',
  6298. ' b: byte;',
  6299. 'begin',
  6300. ' for e in TEnum do ;',
  6301. ' for e in TEnumRg do ;',
  6302. ' for e in TArr do ;',
  6303. ' for e in TArrRg do ;',
  6304. ' for b in a1 do ;',
  6305. ' for b in a2 do ;',
  6306. '']);
  6307. ConvertProgram;
  6308. CheckSource('TestEnum_ForIn',
  6309. LinesToStr([ // statements
  6310. 'this.TEnum = {',
  6311. ' "0": "Red",',
  6312. ' Red: 0,',
  6313. ' "1": "Green",',
  6314. ' Green: 1,',
  6315. ' "2": "Blue",',
  6316. ' Blue: 2',
  6317. '};',
  6318. 'this.e = 0;',
  6319. 'this.a1 = [3, 4, 5];',
  6320. 'this.a2 = [11, 12];',
  6321. 'this.b = 0;',
  6322. '']),
  6323. LinesToStr([
  6324. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6325. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6326. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6327. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6328. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6329. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6330. '']));
  6331. end;
  6332. procedure TTestModule.TestEnum_ScopedNumber;
  6333. begin
  6334. Converter.Options:=Converter.Options+[coEnumNumbers];
  6335. StartProgram(false);
  6336. Add([
  6337. 'type',
  6338. ' TEnum = (Red, Green);',
  6339. 'var',
  6340. ' e: TEnum;',
  6341. 'begin',
  6342. ' e:=TEnum.Green;',
  6343. '']);
  6344. ConvertProgram;
  6345. CheckSource('TestEnum_ScopedNumber',
  6346. LinesToStr([ // statements
  6347. 'this.TEnum = {',
  6348. ' "0": "Red",',
  6349. ' Red: 0,',
  6350. ' "1": "Green",',
  6351. ' Green: 1',
  6352. '};',
  6353. 'this.e = 0;',
  6354. '']),
  6355. LinesToStr([
  6356. '$mod.e = 1;']));
  6357. end;
  6358. procedure TTestModule.TestEnum_InFunction;
  6359. begin
  6360. StartProgram(false);
  6361. Add([
  6362. 'const TEnum = 3;',
  6363. 'procedure DoIt;',
  6364. 'type',
  6365. ' TEnum = (Red, Green, Blue);',
  6366. ' procedure Sub;',
  6367. ' type',
  6368. ' TEnumSub = (Left, Right);',
  6369. ' var',
  6370. ' es: TEnumSub;',
  6371. ' begin',
  6372. ' es:=Left;',
  6373. ' end;',
  6374. 'var',
  6375. ' e, e2: TEnum;',
  6376. 'begin',
  6377. ' if e in [red,blue] then e2:=e;',
  6378. 'end;',
  6379. 'begin']);
  6380. ConvertProgram;
  6381. CheckSource('TestEnum_InFunction',
  6382. LinesToStr([ // statements
  6383. 'this.TEnum = 3;',
  6384. 'var TEnum$1 = {',
  6385. ' "0":"Red",',
  6386. ' Red:0,',
  6387. ' "1":"Green",',
  6388. ' Green:1,',
  6389. ' "2":"Blue",',
  6390. ' Blue:2',
  6391. ' };',
  6392. 'var TEnumSub = {',
  6393. ' "0": "Left",',
  6394. ' Left: 0,',
  6395. ' "1": "Right",',
  6396. ' Right: 1',
  6397. '};',
  6398. 'this.DoIt = function () {',
  6399. ' function Sub() {',
  6400. ' var es = 0;',
  6401. ' es = TEnumSub.Left;',
  6402. ' };',
  6403. ' var e = 0;',
  6404. ' var e2 = 0;',
  6405. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6406. '};',
  6407. '']),
  6408. LinesToStr([
  6409. '']));
  6410. end;
  6411. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6412. begin
  6413. StartUnit(true);
  6414. Add([
  6415. 'interface',
  6416. 'var color: (red, green);',
  6417. 'implementation',
  6418. 'initialization',
  6419. ' color:=green;',
  6420. '']);
  6421. ConvertUnit;
  6422. CheckSource('TestEnum_Name_Anonymous_Unit',
  6423. LinesToStr([
  6424. 'this.color$a = {',
  6425. ' "0": "red",',
  6426. ' red: 0,',
  6427. ' "1": "green",',
  6428. ' green: 1',
  6429. '};',
  6430. 'this.color = 0;',
  6431. '']),
  6432. LinesToStr([ // this.$init
  6433. '$mod.color = $mod.color$a.green;',
  6434. '']),
  6435. LinesToStr([ // implementation
  6436. '']) );
  6437. end;
  6438. procedure TTestModule.TestSet_Enum;
  6439. begin
  6440. StartProgram(false);
  6441. Add([
  6442. 'type',
  6443. ' TColor = (Red, Green, Blue);',
  6444. ' TColors = set of TColor;',
  6445. 'var',
  6446. ' c: TColor;',
  6447. ' s: TColors;',
  6448. ' t: TColors = [];',
  6449. ' u: TColors = [Red];',
  6450. 'begin',
  6451. ' s:=[];',
  6452. ' s:=[Green];',
  6453. ' s:=[Green,Blue];',
  6454. ' s:=[Red..Blue];',
  6455. ' s:=[Red,Green..Blue];',
  6456. ' s:=[Red,c];',
  6457. ' s:=t;',
  6458. ' s:=default(TColors);',
  6459. '']);
  6460. ConvertProgram;
  6461. CheckSource('TestSet',
  6462. LinesToStr([ // statements
  6463. 'this.TColor = {',
  6464. ' "0":"Red",',
  6465. ' Red:0,',
  6466. ' "1":"Green",',
  6467. ' Green:1,',
  6468. ' "2":"Blue",',
  6469. ' Blue:2',
  6470. ' };',
  6471. 'this.c = 0;',
  6472. 'this.s = {};',
  6473. 'this.t = {};',
  6474. 'this.u = rtl.createSet(this.TColor.Red);'
  6475. ]),
  6476. LinesToStr([
  6477. '$mod.s={};',
  6478. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6479. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6480. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6481. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6482. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6483. '$mod.s=rtl.refSet($mod.t);',
  6484. '$mod.s={};',
  6485. '']));
  6486. end;
  6487. procedure TTestModule.TestSet_Operators;
  6488. begin
  6489. StartProgram(false);
  6490. Add('type');
  6491. Add(' TColor = (Red, Green, Blue);');
  6492. Add(' TColors = set of tcolor;');
  6493. Add('var');
  6494. Add(' vC: TColor;');
  6495. Add(' vS: TColors;');
  6496. Add(' vT: TColors;');
  6497. Add(' vU: TColors;');
  6498. Add(' B: boolean;');
  6499. Add('begin');
  6500. Add(' include(vs,green);');
  6501. Add(' exclude(vs,vc);');
  6502. Add(' vs:=vt+vu;');
  6503. Add(' vs:=vt+[red];');
  6504. Add(' vs:=[red]+vt;');
  6505. Add(' vs:=[red]+[green];');
  6506. Add(' vs:=vt-vu;');
  6507. Add(' vs:=vt-[red];');
  6508. Add(' vs:=[red]-vt;');
  6509. Add(' vs:=[red]-[green];');
  6510. Add(' vs:=vt*vu;');
  6511. Add(' vs:=vt*[red];');
  6512. Add(' vs:=[red]*vt;');
  6513. Add(' vs:=[red]*[green];');
  6514. Add(' vs:=vt><vu;');
  6515. Add(' vs:=vt><[red];');
  6516. Add(' vs:=[red]><vt;');
  6517. Add(' vs:=[red]><[green];');
  6518. Add(' b:=vt=vu;');
  6519. Add(' b:=vt=[red];');
  6520. Add(' b:=[red]=vt;');
  6521. Add(' b:=[red]=[green];');
  6522. Add(' b:=vt<>vu;');
  6523. Add(' b:=vt<>[red];');
  6524. Add(' b:=[red]<>vt;');
  6525. Add(' b:=[red]<>[green];');
  6526. Add(' b:=vt<=vu;');
  6527. Add(' b:=vt<=[red];');
  6528. Add(' b:=[red]<=vt;');
  6529. Add(' b:=[red]<=[green];');
  6530. Add(' b:=vt>=vu;');
  6531. Add(' b:=vt>=[red];');
  6532. Add(' b:=[red]>=vt;');
  6533. Add(' b:=[red]>=[green];');
  6534. ConvertProgram;
  6535. CheckSource('TestSet_Operators',
  6536. LinesToStr([ // statements
  6537. 'this.TColor = {',
  6538. ' "0":"Red",',
  6539. ' Red:0,',
  6540. ' "1":"Green",',
  6541. ' Green:1,',
  6542. ' "2":"Blue",',
  6543. ' Blue:2',
  6544. ' };',
  6545. 'this.vC = 0;',
  6546. 'this.vS = {};',
  6547. 'this.vT = {};',
  6548. 'this.vU = {};',
  6549. 'this.B = false;'
  6550. ]),
  6551. LinesToStr([
  6552. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6553. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6554. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6555. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6556. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6557. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6558. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6559. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6560. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6561. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6562. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6563. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6564. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6565. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6566. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6567. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6568. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6569. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6570. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6571. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6572. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6573. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6574. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6575. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6576. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6577. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6578. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6579. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6580. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6581. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6582. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6583. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6584. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6585. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6586. '']));
  6587. end;
  6588. procedure TTestModule.TestSet_Operator_In;
  6589. begin
  6590. StartProgram(false);
  6591. Add([
  6592. 'type',
  6593. ' TColor = (Red, Green, Blue);',
  6594. ' TColors = set of tcolor;',
  6595. ' TColorRg = green..blue;',
  6596. 'var',
  6597. ' vC: tcolor;',
  6598. ' vT: tcolors;',
  6599. ' B: boolean;',
  6600. ' rg: TColorRg;',
  6601. 'begin',
  6602. ' b:=red in vt;',
  6603. ' b:=vc in vt;',
  6604. ' b:=green in [red..blue];',
  6605. ' b:=vc in [red..blue];',
  6606. ' ',
  6607. ' if red in vt then ;',
  6608. ' while vC in vt do ;',
  6609. ' repeat',
  6610. ' until vC in vt;',
  6611. ' if rg in [green..blue] then ;',
  6612. '']);
  6613. ConvertProgram;
  6614. CheckSource('TestSet_Operator_In',
  6615. LinesToStr([ // statements
  6616. 'this.TColor = {',
  6617. ' "0":"Red",',
  6618. ' Red:0,',
  6619. ' "1":"Green",',
  6620. ' Green:1,',
  6621. ' "2":"Blue",',
  6622. ' Blue:2',
  6623. ' };',
  6624. 'this.vC = 0;',
  6625. 'this.vT = {};',
  6626. 'this.B = false;',
  6627. 'this.rg = this.TColor.Green;',
  6628. '']),
  6629. LinesToStr([
  6630. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6631. '$mod.B = $mod.vC in $mod.vT;',
  6632. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6633. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6634. 'if ($mod.TColor.Red in $mod.vT) ;',
  6635. 'while ($mod.vC in $mod.vT) {',
  6636. '};',
  6637. 'do {',
  6638. '} while (!($mod.vC in $mod.vT));',
  6639. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6640. '']));
  6641. end;
  6642. procedure TTestModule.TestSet_Functions;
  6643. begin
  6644. StartProgram(false);
  6645. Add('type');
  6646. Add(' TMyEnum = (Red, Green);');
  6647. Add(' TMyEnums = set of TMyEnum;');
  6648. Add('var');
  6649. Add(' e: TMyEnum;');
  6650. Add(' s: TMyEnums;');
  6651. Add('begin');
  6652. Add(' e:=Low(TMyEnums);');
  6653. Add(' e:=Low(s);');
  6654. Add(' e:=High(TMyEnums);');
  6655. Add(' e:=High(s);');
  6656. ConvertProgram;
  6657. CheckSource('TestSetFunctions',
  6658. LinesToStr([ // statements
  6659. 'this.TMyEnum = {',
  6660. ' "0":"Red",',
  6661. ' Red:0,',
  6662. ' "1":"Green",',
  6663. ' Green:1',
  6664. ' };',
  6665. 'this.e = 0;',
  6666. 'this.s = {};'
  6667. ]),
  6668. LinesToStr([
  6669. '$mod.e=$mod.TMyEnum.Red;',
  6670. '$mod.e=$mod.TMyEnum.Red;',
  6671. '$mod.e=$mod.TMyEnum.Green;',
  6672. '$mod.e=$mod.TMyEnum.Green;',
  6673. '']));
  6674. end;
  6675. procedure TTestModule.TestSet_PassAsArgClone;
  6676. begin
  6677. StartProgram(false);
  6678. Add('type');
  6679. Add(' TMyEnum = (Red, Green);');
  6680. Add(' TMyEnums = set of TMyEnum;');
  6681. Add('procedure DoDefault(s: tmyenums); begin end;');
  6682. Add('procedure DoConst(const s: tmyenums); begin end;');
  6683. Add('var');
  6684. Add(' aSet: tmyenums;');
  6685. Add('begin');
  6686. Add(' dodefault(aset);');
  6687. Add(' doconst(aset);');
  6688. ConvertProgram;
  6689. CheckSource('TestSetFunctions',
  6690. LinesToStr([ // statements
  6691. 'this.TMyEnum = {',
  6692. ' "0":"Red",',
  6693. ' Red:0,',
  6694. ' "1":"Green",',
  6695. ' Green:1',
  6696. ' };',
  6697. 'this.DoDefault = function (s) {',
  6698. '};',
  6699. 'this.DoConst = function (s) {',
  6700. '};',
  6701. 'this.aSet = {};'
  6702. ]),
  6703. LinesToStr([
  6704. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6705. '$mod.DoConst($mod.aSet);',
  6706. '']));
  6707. end;
  6708. procedure TTestModule.TestSet_AsParams;
  6709. begin
  6710. StartProgram(false);
  6711. Add([
  6712. 'type TEnum = (Red,Blue);',
  6713. 'type TEnums = set of TEnum;',
  6714. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6715. 'var vJ: TEnums;',
  6716. 'begin',
  6717. ' Include(vg,red);',
  6718. ' Include(result,blue);',
  6719. ' vg:=vg;',
  6720. ' vj:=vh;',
  6721. ' vi:=vi;',
  6722. ' doit(vg,vg,vg);',
  6723. ' doit(vh,vh,vj);',
  6724. ' doit(vi,vi,vi);',
  6725. ' doit(vj,vj,vj);',
  6726. 'end;',
  6727. 'var i: TEnums;',
  6728. 'begin',
  6729. ' doit(i,i,i);']);
  6730. ConvertProgram;
  6731. CheckSource('TestSet_AsParams',
  6732. LinesToStr([ // statements
  6733. 'this.TEnum = {',
  6734. ' "0": "Red",',
  6735. ' Red: 0,',
  6736. ' "1": "Blue",',
  6737. ' Blue: 1',
  6738. '};',
  6739. 'this.DoIt = function (vG,vH,vI) {',
  6740. ' var Result = {};',
  6741. ' var vJ = {};',
  6742. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6743. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6744. ' vG = rtl.refSet(vG);',
  6745. ' vJ = rtl.refSet(vH);',
  6746. ' vI.set(rtl.refSet(vI.get()));',
  6747. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6748. ' get: function () {',
  6749. ' return vG;',
  6750. ' },',
  6751. ' set: function (v) {',
  6752. ' vG = v;',
  6753. ' }',
  6754. ' });',
  6755. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6756. ' get: function () {',
  6757. ' return vJ;',
  6758. ' },',
  6759. ' set: function (v) {',
  6760. ' vJ = v;',
  6761. ' }',
  6762. ' });',
  6763. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6764. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6765. ' get: function () {',
  6766. ' return vJ;',
  6767. ' },',
  6768. ' set: function (v) {',
  6769. ' vJ = v;',
  6770. ' }',
  6771. ' });',
  6772. ' return Result;',
  6773. '};',
  6774. 'this.i = {};'
  6775. ]),
  6776. LinesToStr([
  6777. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6778. ' p: $mod,',
  6779. ' get: function () {',
  6780. ' return this.p.i;',
  6781. ' },',
  6782. ' set: function (v) {',
  6783. ' this.p.i = v;',
  6784. ' }',
  6785. '});'
  6786. ]));
  6787. end;
  6788. procedure TTestModule.TestSet_Property;
  6789. begin
  6790. StartProgram(false);
  6791. Add('type');
  6792. Add(' TEnum = (Red,Blue);');
  6793. Add(' TEnums = set of TEnum;');
  6794. Add(' TObject = class');
  6795. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6796. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6797. Add(' property Colors: TEnums read GetColors write SetColors;');
  6798. Add(' end;');
  6799. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6800. Add('begin end;');
  6801. Add('var Obj: TObject;');
  6802. Add('begin');
  6803. Add(' Include(Obj.Colors,Red);');
  6804. Add(' Exclude(Obj.Colors,Red);');
  6805. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6806. ConvertProgram;
  6807. CheckSource('TestSet_Property',
  6808. LinesToStr([ // statements
  6809. 'this.TEnum = {',
  6810. ' "0": "Red",',
  6811. ' Red: 0,',
  6812. ' "1": "Blue",',
  6813. ' Blue: 1',
  6814. '};',
  6815. 'rtl.createClass(this, "TObject", null, function () {',
  6816. ' this.$init = function () {',
  6817. ' };',
  6818. ' this.$final = function () {',
  6819. ' };',
  6820. '});',
  6821. 'this.DoIt = function (i, j, k, l) {',
  6822. '};',
  6823. 'this.Obj = null;',
  6824. '']),
  6825. LinesToStr([
  6826. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6827. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6828. '']));
  6829. end;
  6830. procedure TTestModule.TestSet_EnumConst;
  6831. begin
  6832. StartProgram(false);
  6833. Add([
  6834. 'type',
  6835. ' TEnum = (Red,Blue);',
  6836. ' TEnums = set of TEnum;',
  6837. 'const',
  6838. ' Orange = red;',
  6839. 'var',
  6840. ' Enum: tenum;',
  6841. ' Enums: tenums;',
  6842. 'begin',
  6843. ' Include(enums,orange);',
  6844. ' Exclude(enums,orange);',
  6845. ' if orange in enums then;',
  6846. ' if orange in [orange,red] then;']);
  6847. ConvertProgram;
  6848. CheckSource('TestSet_EnumConst',
  6849. LinesToStr([ // statements
  6850. 'this.TEnum = {',
  6851. ' "0": "Red",',
  6852. ' Red: 0,',
  6853. ' "1": "Blue",',
  6854. ' Blue: 1',
  6855. '};',
  6856. 'this.Orange = this.TEnum.Red;',
  6857. 'this.Enum = 0;',
  6858. 'this.Enums = {};',
  6859. '']),
  6860. LinesToStr([
  6861. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6862. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6863. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6864. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6865. '']));
  6866. end;
  6867. procedure TTestModule.TestSet_IntConst;
  6868. begin
  6869. StartProgram(false);
  6870. Add([
  6871. 'type',
  6872. ' TEnums = set of Byte;',
  6873. 'const',
  6874. ' Orange = 0;',
  6875. 'var',
  6876. ' Enum: byte;',
  6877. ' Enums: tenums;',
  6878. 'begin',
  6879. ' Enums:=[];',
  6880. ' Enums:=[0];',
  6881. ' Enums:=[1..2];',
  6882. //' Include(enums,orange);',
  6883. //' Exclude(enums,orange);',
  6884. ' if orange in enums then;',
  6885. ' if orange in [orange,1] then;']);
  6886. ConvertProgram;
  6887. CheckSource('TestSet_IntConst',
  6888. LinesToStr([ // statements
  6889. 'this.Orange = 0;',
  6890. 'this.Enum = 0;',
  6891. 'this.Enums = {};',
  6892. '']),
  6893. LinesToStr([
  6894. '$mod.Enums = {};',
  6895. '$mod.Enums = rtl.createSet(0);',
  6896. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6897. 'if (0 in $mod.Enums) ;',
  6898. 'if (0 in rtl.createSet(0, 1)) ;',
  6899. '']));
  6900. end;
  6901. procedure TTestModule.TestSet_IntRange;
  6902. begin
  6903. StartProgram(false);
  6904. Add([
  6905. 'type',
  6906. ' TRange = 1..3;',
  6907. ' TEnums = set of TRange;',
  6908. 'const',
  6909. ' Orange = 2;',
  6910. 'var',
  6911. ' Enum: byte;',
  6912. ' Enums: TEnums;',
  6913. 'begin',
  6914. ' Enums:=[];',
  6915. ' Enums:=[1];',
  6916. ' Enums:=[2..3];',
  6917. ' Include(enums,orange);',
  6918. ' Exclude(enums,orange);',
  6919. ' if orange in enums then;',
  6920. ' if orange in [orange,1] then;']);
  6921. ConvertProgram;
  6922. CheckSource('TestSet_IntRange',
  6923. LinesToStr([ // statements
  6924. 'this.Orange = 2;',
  6925. 'this.Enum = 0;',
  6926. 'this.Enums = {};',
  6927. '']),
  6928. LinesToStr([
  6929. '$mod.Enums = {};',
  6930. '$mod.Enums = rtl.createSet(1);',
  6931. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6932. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6933. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6934. 'if (2 in $mod.Enums) ;',
  6935. 'if (2 in rtl.createSet(2, 1)) ;',
  6936. '']));
  6937. end;
  6938. procedure TTestModule.TestSet_AnonymousEnumType;
  6939. begin
  6940. StartProgram(false);
  6941. Add('type');
  6942. Add(' TFlags = set of (red, green);');
  6943. Add('const');
  6944. Add(' favorite = red;');
  6945. Add('var');
  6946. Add(' f: TFlags;');
  6947. Add(' i: longint;');
  6948. Add('begin');
  6949. Add(' Include(f,red);');
  6950. Add(' Include(f,favorite);');
  6951. Add(' i:=ord(red);');
  6952. Add(' i:=ord(favorite);');
  6953. Add(' i:=ord(low(TFlags));');
  6954. Add(' i:=ord(low(f));');
  6955. Add(' i:=ord(low(favorite));');
  6956. Add(' i:=ord(high(TFlags));');
  6957. Add(' i:=ord(high(f));');
  6958. Add(' i:=ord(high(favorite));');
  6959. Add(' f:=[green,favorite];');
  6960. ConvertProgram;
  6961. CheckSource('TestSet_AnonymousEnumType',
  6962. LinesToStr([ // statements
  6963. 'this.TFlags$a = {',
  6964. ' "0": "red",',
  6965. ' red: 0,',
  6966. ' "1": "green",',
  6967. ' green: 1',
  6968. '};',
  6969. 'this.favorite = this.TFlags$a.red;',
  6970. 'this.f = {};',
  6971. 'this.i = 0;',
  6972. '']),
  6973. LinesToStr([
  6974. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6975. '$mod.f = rtl.includeSet($mod.f, $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.red;',
  6981. '$mod.i = $mod.TFlags$a.green;',
  6982. '$mod.i = $mod.TFlags$a.green;',
  6983. '$mod.i = $mod.TFlags$a.green;',
  6984. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6985. '']));
  6986. end;
  6987. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6988. begin
  6989. exit;
  6990. StartProgram(false);
  6991. Add([
  6992. 'type',
  6993. ' TAtoZ = ''A''..''Z'';',
  6994. ' TSetOfAZ = set of TAtoZ;',
  6995. 'var',
  6996. ' c: char;',
  6997. ' a: TAtoZ;',
  6998. ' s: TSetOfAZ = [''P'',''A''];',
  6999. ' i: longint;',
  7000. 'begin',
  7001. ' Include(s,''S'');',
  7002. ' Include(s,c);',
  7003. ' Include(s,a);',
  7004. ' c:=low(TAtoZ);',
  7005. ' i:=ord(low(TAtoZ));',
  7006. ' a:=high(TAtoZ);',
  7007. ' a:=high(TSetOfAtoZ);',
  7008. ' s:=[a,c,''M''];',
  7009. '']);
  7010. ConvertProgram;
  7011. CheckSource('TestSet_AnonymousEnumTypeChar',
  7012. LinesToStr([ // statements
  7013. '']),
  7014. LinesToStr([
  7015. '']));
  7016. end;
  7017. procedure TTestModule.TestSet_ConstEnum;
  7018. begin
  7019. StartProgram(false);
  7020. Add([
  7021. 'type',
  7022. ' TEnum = (red,blue,green);',
  7023. ' TEnums = set of TEnum;',
  7024. 'const',
  7025. ' teAny = [low(TEnum)..high(TEnum)];',
  7026. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7027. 'var',
  7028. ' e: TEnum;',
  7029. ' s: TEnums;',
  7030. 'begin',
  7031. ' if blue in teAny then;',
  7032. ' if blue in teAny+[e] then;',
  7033. ' if blue in teAny+teRedBlue then;',
  7034. ' if e in [red,blue] then;',
  7035. ' s:=teAny;',
  7036. ' s:=teAny+[e];',
  7037. ' s:=[e]+teAny;',
  7038. ' s:=teAny+teRedBlue;',
  7039. ' s:=teAny+teRedBlue+[e];',
  7040. '']);
  7041. ConvertProgram;
  7042. CheckSource('TestSet_ConstEnum',
  7043. LinesToStr([ // statements
  7044. 'this.TEnum = {',
  7045. ' "0": "red",',
  7046. ' red: 0,',
  7047. ' "1": "blue",',
  7048. ' blue: 1,',
  7049. ' "2": "green",',
  7050. ' green: 2',
  7051. '};',
  7052. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7053. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7054. 'this.e = 0;',
  7055. 'this.s = {};',
  7056. '']),
  7057. LinesToStr([
  7058. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7059. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7060. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7061. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7062. '$mod.s = rtl.refSet($mod.teAny);',
  7063. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7064. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7065. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7066. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7067. '']));
  7068. end;
  7069. procedure TTestModule.TestSet_ConstChar;
  7070. begin
  7071. StartProgram(false);
  7072. Add([
  7073. 'const',
  7074. ' LowChars = [''a''..''z''];',
  7075. ' Chars = LowChars+[''A''..''Z''];',
  7076. ' sc = [''А'', ''Я''];',
  7077. 'var',
  7078. ' c: char;',
  7079. ' s: string;',
  7080. 'begin',
  7081. ' if c in lowchars then ;',
  7082. ' if ''a'' in lowchars then ;',
  7083. ' if s[1] in lowchars then ;',
  7084. ' if c in chars then ;',
  7085. ' if c in [''a''..''z'',''_''] then ;',
  7086. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7087. ' if ''Я'' in sc then ;',
  7088. ' if 3=ord('' '') then ;',
  7089. '']);
  7090. ConvertProgram;
  7091. CheckSource('TestSet_ConstChar',
  7092. LinesToStr([ // statements
  7093. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7094. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7095. 'this.sc = rtl.createSet(1040, 1071);',
  7096. 'this.c = "";',
  7097. 'this.s = "";',
  7098. '']),
  7099. LinesToStr([
  7100. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7101. 'if (97 in $mod.LowChars) ;',
  7102. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7103. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7104. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7105. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7106. 'if (1071 in $mod.sc) ;',
  7107. 'if (3 === 32) ;',
  7108. '']));
  7109. end;
  7110. procedure TTestModule.TestSet_ConstInt;
  7111. begin
  7112. StartProgram(false);
  7113. Add([
  7114. 'const',
  7115. ' Months = [1..12];',
  7116. ' Mirror = [-12..-1]+Months;',
  7117. 'var',
  7118. ' i: smallint;',
  7119. 'begin',
  7120. ' if 3 in Months then;',
  7121. ' if i in Months+[i] then;',
  7122. ' if i in Months+Mirror then;',
  7123. ' if i in [4..6,8] then;',
  7124. '']);
  7125. ConvertProgram;
  7126. CheckSource('TestSet_ConstInt',
  7127. LinesToStr([ // statements
  7128. 'this.Months = rtl.createSet(null, 1, 12);',
  7129. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7130. 'this.i = 0;',
  7131. '']),
  7132. LinesToStr([
  7133. 'if (3 in $mod.Months) ;',
  7134. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7135. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7136. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7137. '']));
  7138. end;
  7139. procedure TTestModule.TestSet_InFunction;
  7140. begin
  7141. StartProgram(false);
  7142. Add([
  7143. 'const',
  7144. ' TEnum = 3;',
  7145. ' TSetOfEnum = 4;',
  7146. ' TSetOfAno = 5;',
  7147. 'procedure DoIt;',
  7148. 'type',
  7149. ' TEnum = (red, blue);',
  7150. ' TSetOfEnum = set of TEnum;',
  7151. ' TSetOfAno = set of (up,down);',
  7152. 'var',
  7153. ' e: TEnum;',
  7154. ' se: TSetOfEnum;',
  7155. ' sa: TSetOfAno;',
  7156. 'begin',
  7157. ' se:=[e];',
  7158. ' sa:=[up];',
  7159. 'end;',
  7160. 'begin',
  7161. '']);
  7162. ConvertProgram;
  7163. CheckSource('TestSet_InFunction',
  7164. LinesToStr([ // statements
  7165. 'this.TEnum = 3;',
  7166. 'this.TSetOfEnum = 4;',
  7167. 'this.TSetOfAno = 5;',
  7168. 'var TEnum$1 = {',
  7169. ' "0": "red",',
  7170. ' red: 0,',
  7171. ' "1": "blue",',
  7172. ' blue: 1',
  7173. '};',
  7174. 'var TSetOfAno$a = {',
  7175. ' "0": "up",',
  7176. ' up: 0,',
  7177. ' "1": "down",',
  7178. ' down: 1',
  7179. '};',
  7180. 'this.DoIt = function () {',
  7181. ' var e = 0;',
  7182. ' var se = {};',
  7183. ' var sa = {};',
  7184. ' se = rtl.createSet(e);',
  7185. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7186. '};',
  7187. '']),
  7188. LinesToStr([
  7189. '']));
  7190. end;
  7191. procedure TTestModule.TestSet_ForIn;
  7192. begin
  7193. StartProgram(false);
  7194. Add([
  7195. 'type',
  7196. ' TEnum = (Red, Green, Blue);',
  7197. ' TEnumRg = green..blue;',
  7198. ' TSetOfEnum = set of TEnum;',
  7199. ' TSetOfEnumRg = set of TEnumRg;',
  7200. 'var',
  7201. ' e, e2: TEnum;',
  7202. ' er: TEnum;',
  7203. ' s: TSetOfEnum;',
  7204. 'begin',
  7205. ' for e in TSetOfEnum do ;',
  7206. ' for e in TSetOfEnumRg do ;',
  7207. ' for e in [] do e2:=e;',
  7208. ' for e in [red..green] do e2:=e;',
  7209. ' for e in [green,blue] do e2:=e;',
  7210. ' for e in [red,blue] do e2:=e;',
  7211. ' for e in s do e2:=e;',
  7212. ' for er in TSetOfEnumRg do ;',
  7213. '']);
  7214. ConvertProgram;
  7215. CheckSource('TestSet_ForIn',
  7216. LinesToStr([ // statements
  7217. 'this.TEnum = {',
  7218. ' "0":"Red",',
  7219. ' Red:0,',
  7220. ' "1":"Green",',
  7221. ' Green:1,',
  7222. ' "2":"Blue",',
  7223. ' Blue:2',
  7224. ' };',
  7225. 'this.e = 0;',
  7226. 'this.e2 = 0;',
  7227. 'this.er = 0;',
  7228. 'this.s = {};',
  7229. '']),
  7230. LinesToStr([
  7231. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7232. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7233. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7234. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7235. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7236. 'for (var $l in $mod.s){',
  7237. ' $mod.e = +$l;',
  7238. ' $mod.e2 = $mod.e;',
  7239. '};',
  7240. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7241. '']));
  7242. end;
  7243. procedure TTestModule.TestNestBegin;
  7244. begin
  7245. StartProgram(false);
  7246. Add('begin');
  7247. Add(' begin');
  7248. Add(' begin');
  7249. Add(' end;');
  7250. Add(' begin');
  7251. Add(' if true then ;');
  7252. Add(' end;');
  7253. Add(' end;');
  7254. ConvertProgram;
  7255. CheckSource('TestNestBegin',
  7256. '',
  7257. 'if (true) ;');
  7258. end;
  7259. procedure TTestModule.TestUnitImplVars;
  7260. begin
  7261. StartUnit(false);
  7262. Add('interface');
  7263. Add('implementation');
  7264. Add('var');
  7265. Add(' V1:longint;');
  7266. Add(' V2:longint = 3;');
  7267. Add(' V3:string = ''abc'';');
  7268. ConvertUnit;
  7269. CheckSource('TestUnitImplVars',
  7270. LinesToStr([ // statements
  7271. 'var $impl = $mod.$impl;',
  7272. '']),
  7273. '', // this.$init
  7274. LinesToStr([ // implementation
  7275. '$impl.V1 = 0;',
  7276. '$impl.V2 = 3;',
  7277. '$impl.V3 = "abc";',
  7278. '']) );
  7279. end;
  7280. procedure TTestModule.TestUnitImplConsts;
  7281. begin
  7282. StartUnit(false);
  7283. Add('interface');
  7284. Add('implementation');
  7285. Add('const');
  7286. Add(' v1 = 3;');
  7287. Add(' v2:longint = 4;');
  7288. Add(' v3:string = ''abc'';');
  7289. ConvertUnit;
  7290. CheckSource('TestUnitImplConsts',
  7291. LinesToStr([ // statements
  7292. 'var $impl = $mod.$impl;',
  7293. '']),
  7294. '', // this.$init
  7295. LinesToStr([ // implementation
  7296. '$impl.v1 = 3;',
  7297. '$impl.v2 = 4;',
  7298. '$impl.v3 = "abc";',
  7299. '']) );
  7300. end;
  7301. procedure TTestModule.TestUnitImplRecord;
  7302. begin
  7303. StartUnit(false);
  7304. Add('interface');
  7305. Add('implementation');
  7306. Add('type');
  7307. Add(' TMyRecord = record');
  7308. Add(' i: longint;');
  7309. Add(' end;');
  7310. Add('var aRec: TMyRecord;');
  7311. Add('initialization');
  7312. Add(' arec.i:=3;');
  7313. ConvertUnit;
  7314. CheckSource('TestUnitImplRecord',
  7315. LinesToStr([ // statements
  7316. 'var $impl = $mod.$impl;',
  7317. '']),
  7318. // this.$init
  7319. '$impl.aRec.i = 3;',
  7320. LinesToStr([ // implementation
  7321. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7322. ' this.i = 0;',
  7323. ' this.$eq = function (b) {',
  7324. ' return this.i === b.i;',
  7325. ' };',
  7326. ' this.$assign = function (s) {',
  7327. ' this.i = s.i;',
  7328. ' return this;',
  7329. ' };',
  7330. '});',
  7331. '$impl.aRec = $impl.TMyRecord.$new();',
  7332. '']) );
  7333. end;
  7334. procedure TTestModule.TestRenameJSNameConflict;
  7335. begin
  7336. StartProgram(false);
  7337. Add('var apply: longint;');
  7338. Add('var bind: longint;');
  7339. Add('var call: longint;');
  7340. Add('begin');
  7341. ConvertProgram;
  7342. CheckSource('TestRenameJSNameConflict',
  7343. LinesToStr([ // statements
  7344. 'this.Apply = 0;',
  7345. 'this.Bind = 0;',
  7346. 'this.Call = 0;'
  7347. ]),
  7348. LinesToStr([ // this.$main
  7349. ''
  7350. ]));
  7351. end;
  7352. procedure TTestModule.TestLocalConst;
  7353. begin
  7354. StartProgram(false);
  7355. Add('procedure DoIt;');
  7356. Add('const');
  7357. Add(' cA: longint = 1;');
  7358. Add(' cB = 2;');
  7359. Add(' procedure Sub;');
  7360. Add(' const');
  7361. Add(' csA = 3;');
  7362. Add(' cB: double = 4;');
  7363. Add(' begin');
  7364. Add(' cb:=cb+csa;');
  7365. Add(' ca:=ca+csa+5;');
  7366. Add(' end;');
  7367. Add('begin');
  7368. Add(' ca:=ca+cb+6;');
  7369. Add('end;');
  7370. Add('begin');
  7371. ConvertProgram;
  7372. CheckSource('TestLocalConst',
  7373. LinesToStr([
  7374. 'var cA = 1;',
  7375. 'var cB = 2;',
  7376. 'var csA = 3;',
  7377. 'var cB$1 = 4;',
  7378. 'this.DoIt = function () {',
  7379. ' function Sub() {',
  7380. ' cB$1 = cB$1 + 3;',
  7381. ' cA = cA + 3 + 5;',
  7382. ' };',
  7383. ' cA = cA + 2 + 6;',
  7384. '};'
  7385. ]),
  7386. LinesToStr([
  7387. ]));
  7388. end;
  7389. procedure TTestModule.TestVarExternal;
  7390. begin
  7391. StartProgram(false);
  7392. Add('var');
  7393. Add(' NaN: double; external name ''Global.NaN'';');
  7394. Add(' d: double;');
  7395. Add('begin');
  7396. Add(' d:=NaN;');
  7397. ConvertProgram;
  7398. CheckSource('TestVarExternal',
  7399. LinesToStr([
  7400. 'this.d = 0.0;'
  7401. ]),
  7402. LinesToStr([
  7403. '$mod.d = Global.NaN;'
  7404. ]));
  7405. end;
  7406. procedure TTestModule.TestVarExternalOtherUnit;
  7407. begin
  7408. AddModuleWithIntfImplSrc('unit2.pas',
  7409. LinesToStr([
  7410. 'var NaN: double; external name ''Global.NaN'';',
  7411. 'var iV: longint;'
  7412. ]),
  7413. '');
  7414. StartUnit(true);
  7415. Add('interface');
  7416. Add('uses unit2;');
  7417. Add('implementation');
  7418. Add('var');
  7419. Add(' d: double;');
  7420. Add(' i: longint; external name ''$i'';');
  7421. Add('begin');
  7422. Add(' d:=nan;');
  7423. Add(' d:=uNit2.nan;');
  7424. Add(' d:=test1.d;');
  7425. Add(' i:=iv;');
  7426. Add(' i:=uNit2.iv;');
  7427. Add(' i:=test1.i;');
  7428. ConvertUnit;
  7429. CheckSource('TestVarExternalOtherUnit',
  7430. LinesToStr([
  7431. 'var $impl = $mod.$impl;',
  7432. '']),
  7433. LinesToStr([ // this.$init
  7434. '$impl.d = Global.NaN;',
  7435. '$impl.d = Global.NaN;',
  7436. '$impl.d = $impl.d;',
  7437. '$i = pas.unit2.iV;',
  7438. '$i = pas.unit2.iV;',
  7439. '$i = $i;',
  7440. '']),
  7441. LinesToStr([ // implementation
  7442. '$impl.d = 0.0;',
  7443. '']) );
  7444. end;
  7445. procedure TTestModule.TestVarAbsoluteFail;
  7446. begin
  7447. StartProgram(false);
  7448. Add([
  7449. 'var',
  7450. ' a: longint;',
  7451. ' b: longword absolute a;',
  7452. 'begin']);
  7453. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7454. ConvertProgram;
  7455. end;
  7456. procedure TTestModule.TestConstExternal;
  7457. begin
  7458. StartProgram(false);
  7459. Add([
  7460. 'const',
  7461. ' PI: double; external name ''Global.PI'';',
  7462. ' Tau = 2*pi;',
  7463. 'var d: double;',
  7464. 'begin',
  7465. ' d:=pi;',
  7466. ' d:=tau+pi;']);
  7467. ConvertProgram;
  7468. CheckSource('TestConstExternal',
  7469. LinesToStr([
  7470. 'this.Tau = 2*Global.PI;',
  7471. 'this.d = 0.0;'
  7472. ]),
  7473. LinesToStr([
  7474. '$mod.d = Global.PI;',
  7475. '$mod.d = $mod.Tau + Global.PI;'
  7476. ]));
  7477. end;
  7478. procedure TTestModule.TestDouble;
  7479. begin
  7480. StartProgram(false);
  7481. Add([
  7482. 'type',
  7483. ' TDateTime = double;',
  7484. 'const',
  7485. ' a = TDateTime(2.7);',
  7486. ' b = a + TDateTime(1.7);',
  7487. ' c = 0.9 + 0.1;',
  7488. ' f0_1 = 0.1;',
  7489. ' f0_3 = 0.3;',
  7490. ' fn0_1 = -0.1;',
  7491. ' fn0_3 = -0.3;',
  7492. ' fn0_003 = -0.003;',
  7493. ' fn0_123456789 = -0.123456789;',
  7494. ' fn300_0 = -300.0;',
  7495. ' fn123456_0 = -123456.0;',
  7496. ' fn1234567_8 = -1234567.8;',
  7497. ' fn12345678_9 = -12345678.9;',
  7498. ' f1_0En12 = 1E-12;',
  7499. ' fn1_0En12 = -1E-12;',
  7500. ' maxdouble = 1.7e+308;',
  7501. ' mindouble = -1.7e+308;',
  7502. ' MinSafeIntDouble = -$1fffffffffffff;',
  7503. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7504. ' MaxSafeIntDouble = $1fffffffffffff;',
  7505. ' DZeroResolution = 1E-12;',
  7506. ' Minus1 = -1E-12;',
  7507. ' EPS = 1E-9;',
  7508. ' DELTA = 0.001;',
  7509. ' Big = 129.789E+100;',
  7510. ' Test0_15 = 0.15;',
  7511. ' Test999 = 2.9999999999999;',
  7512. ' Test111999 = 211199999999999000.0;',
  7513. ' TestMinus111999 = -211199999999999000.0;',
  7514. ' Inf = 1.0 / 0.0;',
  7515. ' NegInf = -1.0 / 0.0;',
  7516. 'procedure Run(d: double); external name ''Run'';',
  7517. 'var',
  7518. ' d: double = b;',
  7519. 'begin',
  7520. ' d:=1.0;',
  7521. ' d:=1.0/3.0;',
  7522. ' d:=1.0/(3-2-1);',
  7523. ' d:=1/3;',
  7524. ' d:=5.0E-324;',
  7525. ' d:=1.7E308;',
  7526. ' d:=001.00E00;',
  7527. ' d:=002.00E001;',
  7528. ' d:=003.000E000;',
  7529. ' d:=-004.00E-00;',
  7530. ' d:=-005.00E-001;',
  7531. ' d:=10**3;',
  7532. ' d:=10 mod 3;',
  7533. ' d:=10 div 3;',
  7534. ' d:=c;',
  7535. ' d:=f0_1;',
  7536. ' d:=f0_3;',
  7537. ' d:=fn0_1;',
  7538. ' d:=fn0_3;',
  7539. ' d:=fn0_003;',
  7540. ' d:=fn0_123456789;',
  7541. ' d:=fn300_0;',
  7542. ' d:=fn123456_0;',
  7543. ' d:=fn1234567_8;',
  7544. ' d:=fn12345678_9;',
  7545. ' d:=f1_0En12;',
  7546. ' d:=fn1_0En12;',
  7547. ' d:=maxdouble;',
  7548. ' d:=mindouble;',
  7549. ' d:=MinSafeIntDouble;',
  7550. ' d:=double(MinSafeIntDouble);',
  7551. ' d:=MinSafeIntDouble2;',
  7552. ' d:=double(MinSafeIntDouble2);',
  7553. ' d:=MaxSafeIntDouble;',
  7554. ' d:=default(double);',
  7555. ' Run(Inf);',
  7556. ' Run(NegInf);',
  7557. '']);
  7558. ConvertProgram;
  7559. CheckSource('TestDouble',
  7560. LinesToStr([
  7561. 'this.a = 2.7;',
  7562. 'this.b = 2.7 + 1.7;',
  7563. 'this.c = 0.9 + 0.1;',
  7564. 'this.f0_1 = 0.1;',
  7565. 'this.f0_3 = 0.3;',
  7566. 'this.fn0_1 = -0.1;',
  7567. 'this.fn0_3 = -0.3;',
  7568. 'this.fn0_003 = -0.003;',
  7569. 'this.fn0_123456789 = -0.123456789;',
  7570. 'this.fn300_0 = -300.0;',
  7571. 'this.fn123456_0 = -123456.0;',
  7572. 'this.fn1234567_8 = -1234567.8;',
  7573. 'this.fn12345678_9 = -12345678.9;',
  7574. 'this.f1_0En12 = 1E-12;',
  7575. 'this.fn1_0En12 = -1E-12;',
  7576. 'this.maxdouble = 1.7e+308;',
  7577. 'this.mindouble = -1.7e+308;',
  7578. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7579. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7580. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7581. 'this.DZeroResolution = 1E-12;',
  7582. 'this.Minus1 = -1E-12;',
  7583. 'this.EPS = 1E-9;',
  7584. 'this.DELTA = 0.001;',
  7585. 'this.Big = 129.789E+100;',
  7586. 'this.Test0_15 = 0.15;',
  7587. 'this.Test999 = 2.9999999999999;',
  7588. 'this.Test111999 = 211199999999999000.0;',
  7589. 'this.TestMinus111999 = -211199999999999000.0;',
  7590. 'this.Inf = 1.0 / 0.0;',
  7591. 'this.NegInf = -1.0 / 0.0;',
  7592. 'this.d = 4.4;',
  7593. '']),
  7594. LinesToStr([
  7595. '$mod.d = 1.0;',
  7596. '$mod.d = 1.0 / 3.0;',
  7597. '$mod.d = 1.0 / (3 - 2 - 1);',
  7598. '$mod.d = 1 / 3;',
  7599. '$mod.d = 5.0E-324;',
  7600. '$mod.d = 1.7E308;',
  7601. '$mod.d = 1.00E0;',
  7602. '$mod.d = 2.00E1;',
  7603. '$mod.d = 3.000E0;',
  7604. '$mod.d = -4.00E-0;',
  7605. '$mod.d = -5.00E-1;',
  7606. '$mod.d = Math.pow(10, 3);',
  7607. '$mod.d = 10 % 3;',
  7608. '$mod.d = rtl.trunc(10 / 3);',
  7609. '$mod.d = 1;',
  7610. '$mod.d = 0.1;',
  7611. '$mod.d = 0.3;',
  7612. '$mod.d = -0.1;',
  7613. '$mod.d = -0.3;',
  7614. '$mod.d = -3E-3;',
  7615. '$mod.d = -0.123456789;',
  7616. '$mod.d = -300;',
  7617. '$mod.d = -123456;',
  7618. '$mod.d = -1234567.8;',
  7619. '$mod.d = -1.23456789E7;',
  7620. '$mod.d = 1E-12;',
  7621. '$mod.d = -1E-12;',
  7622. '$mod.d = 1.7E308;',
  7623. '$mod.d = -1.7E308;',
  7624. '$mod.d = -9007199254740991;',
  7625. '$mod.d = -9007199254740991;',
  7626. '$mod.d = -9.007199254740992E15;',
  7627. '$mod.d = -9.007199254740992E15;',
  7628. '$mod.d = 9007199254740991;',
  7629. '$mod.d = 0.0;',
  7630. 'Run(1 / 0);',
  7631. 'Run(-1 / 0);',
  7632. '']));
  7633. end;
  7634. procedure TTestModule.TestDoubleSmall;
  7635. begin
  7636. StartProgram(false);
  7637. Add([
  7638. 'const',
  7639. ' a = 1e-1;',
  7640. ' b = 1e-2;',
  7641. ' c = 1e-3;',
  7642. ' d = 1e-4;',
  7643. ' e = 1e-5;',
  7644. ' f = 1e-6;',
  7645. ' g = 1e-7;',
  7646. ' h = -1e-1;',
  7647. ' i = -1e-2;',
  7648. 'procedure Fly(d: double);',
  7649. 'begin',
  7650. 'end;',
  7651. 'begin',
  7652. ' Fly(a);',
  7653. ' Fly(b);',
  7654. ' Fly(c);',
  7655. ' Fly(d);',
  7656. ' Fly(e);',
  7657. ' Fly(f);',
  7658. ' Fly(g);',
  7659. ' Fly(h);',
  7660. ' Fly(i);',
  7661. '']);
  7662. ConvertProgram;
  7663. CheckSource('TestDoubleSmall',
  7664. LinesToStr([
  7665. 'this.a = 1e-1;',
  7666. 'this.b = 1e-2;',
  7667. 'this.c = 1e-3;',
  7668. 'this.d = 1e-4;',
  7669. 'this.e = 1e-5;',
  7670. 'this.f = 1e-6;',
  7671. 'this.g = 1e-7;',
  7672. 'this.h = -1e-1;',
  7673. 'this.i = -1e-2;',
  7674. 'this.Fly = function (d) {',
  7675. '};',
  7676. '']),
  7677. LinesToStr([
  7678. '$mod.Fly(0.1);',
  7679. '$mod.Fly(0.01);',
  7680. '$mod.Fly(1E-3);',
  7681. '$mod.Fly(1E-4);',
  7682. '$mod.Fly(1E-5);',
  7683. '$mod.Fly(1E-6);',
  7684. '$mod.Fly(1E-7);',
  7685. '$mod.Fly(-0.1);',
  7686. '$mod.Fly(-0.01);',
  7687. '']));
  7688. end;
  7689. procedure TTestModule.TestInteger;
  7690. begin
  7691. StartProgram(false);
  7692. Add([
  7693. 'const',
  7694. ' MinInt = low(NativeInt);',
  7695. ' MaxInt = high(NativeInt);',
  7696. 'type',
  7697. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7698. 'const',
  7699. ' a = low(TMyInt)+High(TMyInt);',
  7700. 'var',
  7701. ' i: TMyInt;',
  7702. 'begin',
  7703. ' i:=-MinInt;',
  7704. ' i:=default(TMyInt);',
  7705. ' i:=low(i)+high(i);',
  7706. '']);
  7707. ConvertProgram;
  7708. CheckSource('TestIntegerRange',
  7709. LinesToStr([
  7710. 'this.MinInt = -9007199254740991;',
  7711. 'this.MaxInt = 9007199254740991;',
  7712. 'this.a = -9007199254740991 + 9007199254740991;',
  7713. 'this.i = 0;',
  7714. '']),
  7715. LinesToStr([
  7716. '$mod.i = - -9007199254740991;',
  7717. '$mod.i = -9007199254740991;',
  7718. '$mod.i = -9007199254740991 + 9007199254740991;',
  7719. '']));
  7720. end;
  7721. procedure TTestModule.TestIntegerRange;
  7722. begin
  7723. StartProgram(false);
  7724. Add([
  7725. 'const',
  7726. ' MinInt = -1;',
  7727. ' MaxInt = +1;',
  7728. 'type',
  7729. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7730. ' TInt2 = 1..3;',
  7731. 'const',
  7732. ' a = low(TMyInt)+High(TMyInt);',
  7733. ' b = low(TInt2)+High(TInt2);',
  7734. ' s1 = [1];',
  7735. ' s2 = [1,2];',
  7736. ' s3 = [1..3];',
  7737. ' s4 = [low(shortint)..high(shortint)];',
  7738. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7739. ' s6 = 1 in s2;',
  7740. 'var',
  7741. ' i: TMyInt;',
  7742. ' i2: TInt2;',
  7743. 'begin',
  7744. ' i:=i2;',
  7745. ' i:=default(TMyInt);',
  7746. ' if i=i2 then ;',
  7747. ' i:=ord(i2);',
  7748. '']);
  7749. ConvertProgram;
  7750. CheckSource('TestIntegerRange',
  7751. LinesToStr([
  7752. 'this.MinInt = -1;',
  7753. 'this.MaxInt = +1;',
  7754. 'this.a = -1 + 1;',
  7755. 'this.b = 1 + 3;',
  7756. 'this.s1 = rtl.createSet(1);',
  7757. 'this.s2 = rtl.createSet(1, 2);',
  7758. 'this.s3 = rtl.createSet(null, 1, 3);',
  7759. 'this.s4 = rtl.createSet(null, -128, 127);',
  7760. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7761. 'this.s6 = 1 in this.s2;',
  7762. 'this.i = 0;',
  7763. 'this.i2 = 0;',
  7764. '']),
  7765. LinesToStr([
  7766. '$mod.i = $mod.i2;',
  7767. '$mod.i = -1;',
  7768. 'if ($mod.i === $mod.i2) ;',
  7769. '$mod.i = $mod.i2;',
  7770. '']));
  7771. end;
  7772. procedure TTestModule.TestIntegerTypecasts;
  7773. begin
  7774. StartProgram(false);
  7775. Add([
  7776. 'var',
  7777. ' i: nativeint;',
  7778. ' b: byte;',
  7779. ' sh: shortint;',
  7780. ' w: word;',
  7781. ' sm: smallint;',
  7782. ' lw: longword;',
  7783. ' li: longint;',
  7784. 'begin',
  7785. ' b:=byte(i);',
  7786. ' sh:=shortint(i);',
  7787. ' w:=word(i);',
  7788. ' sm:=smallint(i);',
  7789. ' lw:=longword(i);',
  7790. ' li:=longint(i);',
  7791. '']);
  7792. ConvertProgram;
  7793. CheckSource('TestIntegerTypecasts',
  7794. LinesToStr([
  7795. 'this.i = 0;',
  7796. 'this.b = 0;',
  7797. 'this.sh = 0;',
  7798. 'this.w = 0;',
  7799. 'this.sm = 0;',
  7800. 'this.lw = 0;',
  7801. 'this.li = 0;',
  7802. '']),
  7803. LinesToStr([
  7804. '$mod.b = $mod.i & 255;',
  7805. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7806. '$mod.w = $mod.i & 65535;',
  7807. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7808. '$mod.lw = $mod.i >>> 0;',
  7809. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7810. '']));
  7811. end;
  7812. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7813. begin
  7814. StartProgram(false);
  7815. Add([
  7816. 'var',
  7817. ' i,j: nativeint;',
  7818. 'begin',
  7819. ' i:=i shr 0;',
  7820. ' i:=i shr 1;',
  7821. ' i:=i shr 3;',
  7822. ' i:=i shr 54;',
  7823. ' i:=j shr i;',
  7824. '']);
  7825. ConvertProgram;
  7826. CheckResolverUnexpectedHints;
  7827. CheckSource('TestInteger_BitwiseShrNativeInt',
  7828. LinesToStr([
  7829. 'this.i = 0;',
  7830. 'this.j = 0;',
  7831. '']),
  7832. LinesToStr([
  7833. '$mod.i = $mod.i;',
  7834. '$mod.i = Math.floor($mod.i / 2);',
  7835. '$mod.i = Math.floor($mod.i / 8);',
  7836. '$mod.i = 0;',
  7837. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7838. '']));
  7839. end;
  7840. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7841. begin
  7842. StartProgram(false);
  7843. Add([
  7844. 'var',
  7845. ' i: nativeint;',
  7846. 'begin',
  7847. ' i:=i shl 0;',
  7848. ' i:=i shl 54;',
  7849. ' i:=123456789012 shl 1;',
  7850. ' i:=i shl 1;',
  7851. '']);
  7852. ConvertProgram;
  7853. CheckResolverUnexpectedHints;
  7854. CheckSource('TestInteger_BitwiseShrNativeInt',
  7855. LinesToStr([
  7856. 'this.i = 0;',
  7857. '']),
  7858. LinesToStr([
  7859. '$mod.i = $mod.i;',
  7860. '$mod.i = 0;',
  7861. '$mod.i = 246913578024;',
  7862. '$mod.i = rtl.shl($mod.i, 1);',
  7863. '']));
  7864. end;
  7865. procedure TTestModule.TestInteger_SystemFunc;
  7866. begin
  7867. StartProgram(true);
  7868. Add([
  7869. 'var',
  7870. ' i: byte;',
  7871. ' s: string;',
  7872. 'begin',
  7873. ' system.inc(i);',
  7874. ' system.str(i,s);',
  7875. ' s:=system.str(i);',
  7876. ' i:=system.low(i);',
  7877. ' i:=system.high(i);',
  7878. ' i:=system.pred(i);',
  7879. ' i:=system.succ(i);',
  7880. ' i:=system.ord(i);',
  7881. '']);
  7882. ConvertProgram;
  7883. CheckResolverUnexpectedHints;
  7884. CheckSource('TestInteger_SystemFunc',
  7885. LinesToStr([
  7886. 'this.i = 0;',
  7887. 'this.s = "";',
  7888. '']),
  7889. LinesToStr([
  7890. '$mod.i += 1;',
  7891. '$mod.s = "" + $mod.i;',
  7892. '$mod.s = "" + $mod.i;',
  7893. '$mod.i = 0;',
  7894. '$mod.i = 255;',
  7895. '$mod.i = $mod.i - 1;',
  7896. '$mod.i = $mod.i + 1;',
  7897. '$mod.i = $mod.i;',
  7898. '']));
  7899. end;
  7900. procedure TTestModule.TestInteger_AssignOutsideConst;
  7901. begin
  7902. StartProgram(false);
  7903. Add([
  7904. 'const',
  7905. ' MinInt = low(longint);',
  7906. ' MaxInt = high(longint);',
  7907. 'type',
  7908. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7909. 'var',
  7910. ' i: TMyInt;',
  7911. ' aByte: byte;',
  7912. ' aShortInt: shortint;',
  7913. ' aWord: word;',
  7914. ' aSmallInt: smallint;',
  7915. ' aLongWord: longword;',
  7916. ' aLongInt: longint;',
  7917. ' aNativeInt: nativeint;',
  7918. ' aNativeUInt: nativeuint;',
  7919. 'begin',
  7920. ' aByte:=$FF;',
  7921. ' aByte:=$100;',
  7922. ' aByte:=-1;',
  7923. ' aByte:=-127;',
  7924. ' aByte:=-128;',
  7925. ' aByte:=-254;',
  7926. ' aByte:=-255;',
  7927. ' aByte:=-256;',
  7928. ' aShortInt:=127;',
  7929. ' aShortInt:=128;',
  7930. ' aShortInt:=-128;',
  7931. ' aShortInt:=-129;',
  7932. ' aWord:=$ffff;',
  7933. ' aWord:=$10000;',
  7934. ' aWord:=-1;',
  7935. ' aWord:=-$ffff;',
  7936. ' aWord:=-$10000;',
  7937. ' aWord:=-$10001;',
  7938. ' aSmallInt:=$7fff;',
  7939. ' aSmallInt:=$8000;',
  7940. ' aSmallInt:=-$8000;',
  7941. ' aSmallInt:=-$8001;',
  7942. ' aLongWord:=$ffffffff;',
  7943. ' aLongWord:=$100000000;',
  7944. ' aLongWord:=-1;',
  7945. ' aLongWord:=-$ffffffff;',
  7946. ' aNativeInt:=$1fffffffffffff;',
  7947. ' aNativeInt:=-$1fffffffffffff;',
  7948. ' aNativeUInt:=$1fffffffffffff;',
  7949. ' aNativeUInt:=-$1fffffffffffff;',
  7950. '']);
  7951. ConvertProgram;
  7952. CheckSource('TestInteger_AssignOutsideConst',
  7953. LinesToStr([
  7954. 'this.MinInt = -2147483648;',
  7955. 'this.MaxInt = 2147483647;',
  7956. 'this.i = 0;',
  7957. 'this.aByte = 0;',
  7958. 'this.aShortInt = 0;',
  7959. 'this.aWord = 0;',
  7960. 'this.aSmallInt = 0;',
  7961. 'this.aLongWord = 0;',
  7962. 'this.aLongInt = 0;',
  7963. 'this.aNativeInt = 0;',
  7964. 'this.aNativeUInt = 0;',
  7965. '']),
  7966. LinesToStr([
  7967. '$mod.aByte = 0xFF;',
  7968. '$mod.aByte = 0;',
  7969. '$mod.aByte = 255;',
  7970. '$mod.aByte = 129;',
  7971. '$mod.aByte = 128;',
  7972. '$mod.aByte = 2;',
  7973. '$mod.aByte = 1;',
  7974. '$mod.aByte = 0;',
  7975. '$mod.aShortInt = 127;',
  7976. '$mod.aShortInt = -128;',
  7977. '$mod.aShortInt = -128;',
  7978. '$mod.aShortInt = 127;',
  7979. '$mod.aWord = 0xffff;',
  7980. '$mod.aWord = 0;',
  7981. '$mod.aWord = 65535;',
  7982. '$mod.aWord = 1;',
  7983. '$mod.aWord = 0;',
  7984. '$mod.aWord = 65535;',
  7985. '$mod.aSmallInt = 0x7fff;',
  7986. '$mod.aSmallInt = -32768;',
  7987. '$mod.aSmallInt = -0x8000;',
  7988. '$mod.aSmallInt = 32767;',
  7989. '$mod.aLongWord = 0xffffffff;',
  7990. '$mod.aLongWord = 0;',
  7991. '$mod.aLongWord = 4294967295;',
  7992. '$mod.aLongWord = 1;',
  7993. '$mod.aNativeInt = 0x1fffffffffffff;',
  7994. '$mod.aNativeInt = -0x1fffffffffffff;',
  7995. '$mod.aNativeUInt = 0x1fffffffffffff;',
  7996. '$mod.aNativeUInt = 1;',
  7997. '']));
  7998. end;
  7999. procedure TTestModule.TestCurrency;
  8000. begin
  8001. StartProgram(false);
  8002. Add([
  8003. 'type',
  8004. ' TCoin = currency;',
  8005. 'const',
  8006. ' a = TCoin(2.7);',
  8007. ' b = a + TCoin(1.7);',
  8008. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  8009. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  8010. 'var',
  8011. ' c: TCoin = b;',
  8012. ' i: nativeint;',
  8013. ' d: double;',
  8014. ' j: jsvalue;',
  8015. 'function DoIt(c: currency): currency; begin end;',
  8016. 'function GetIt(d: double): double; begin end;',
  8017. 'procedure Write(v: jsvalue); begin end;',
  8018. 'begin',
  8019. ' c:=1.0;',
  8020. ' c:=0.1;',
  8021. ' c:=1.0/3.0;',
  8022. ' c:=1/3;',
  8023. ' c:=a;',
  8024. ' d:=c;',
  8025. ' c:=d;',
  8026. ' c:=currency(c);',
  8027. ' c:=currency(d);',
  8028. ' d:=double(c);',
  8029. ' c:=i;',
  8030. ' c:=currency(i);',
  8031. //' i:=c;', not allowed
  8032. ' i:=nativeint(c);',
  8033. ' c:=c+a;',
  8034. ' c:=-c-a;',
  8035. ' c:=d+c;',
  8036. ' c:=c+d;',
  8037. ' c:=d-c;',
  8038. ' c:=c-d;',
  8039. ' c:=c*a;',
  8040. ' c:=a*c;',
  8041. ' c:=d*c;',
  8042. ' c:=c*d;',
  8043. ' c:=c/a;',
  8044. ' c:=a/c;',
  8045. ' c:=d/c;',
  8046. ' c:=c/d;',
  8047. ' c:=c**a;',
  8048. ' c:=a**c;',
  8049. ' c:=d**c;',
  8050. ' c:=c**d;',
  8051. ' if c=c then ;',
  8052. ' if c=a then ;',
  8053. ' if a=c then ;',
  8054. ' if d=c then ;',
  8055. ' if c=d then ;',
  8056. ' c:=DoIt(c);',
  8057. ' c:=DoIt(i);',
  8058. ' c:=DoIt(d);',
  8059. ' c:=GetIt(c);',
  8060. ' j:=c;',
  8061. ' Write(c);',
  8062. ' c:=default(currency);',
  8063. ' j:=str(c);',
  8064. ' j:=str(c:0:3);',
  8065. '']);
  8066. ConvertProgram;
  8067. CheckSource('TestCurrency',
  8068. LinesToStr([
  8069. 'this.a = 27000;',
  8070. 'this.b = this.a + 17000;',
  8071. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8072. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8073. 'this.c = this.b;',
  8074. 'this.i = 0;',
  8075. 'this.d = 0.0;',
  8076. 'this.j = undefined;',
  8077. 'this.DoIt = function (c) {',
  8078. ' var Result = 0;',
  8079. ' return Result;',
  8080. '};',
  8081. 'this.GetIt = function (d) {',
  8082. ' var Result = 0.0;',
  8083. ' return Result;',
  8084. '};',
  8085. 'this.Write = function (v) {',
  8086. '};',
  8087. '']),
  8088. LinesToStr([
  8089. '$mod.c = 10000;',
  8090. '$mod.c = 1000;',
  8091. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8092. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8093. '$mod.c = $mod.a;',
  8094. '$mod.d = $mod.c / 10000;',
  8095. '$mod.c = rtl.trunc($mod.d * 10000);',
  8096. '$mod.c = $mod.c;',
  8097. '$mod.c = $mod.d * 10000;',
  8098. '$mod.d = $mod.c / 10000;',
  8099. '$mod.c = $mod.i * 10000;',
  8100. '$mod.c = $mod.i * 10000;',
  8101. '$mod.i = rtl.trunc($mod.c / 10000);',
  8102. '$mod.c = $mod.c + $mod.a;',
  8103. '$mod.c = -$mod.c - $mod.a;',
  8104. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8105. '$mod.c = $mod.c + ($mod.d * 10000);',
  8106. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8107. '$mod.c = $mod.c - ($mod.d * 10000);',
  8108. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8109. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8110. '$mod.c = $mod.d * $mod.c;',
  8111. '$mod.c = $mod.c * $mod.d;',
  8112. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8113. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8114. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8115. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8116. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8117. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8118. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8119. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8120. 'if ($mod.c === $mod.c) ;',
  8121. 'if ($mod.c === $mod.a) ;',
  8122. 'if ($mod.a === $mod.c) ;',
  8123. 'if (($mod.d * 10000) === $mod.c) ;',
  8124. 'if ($mod.c === ($mod.d * 10000)) ;',
  8125. '$mod.c = $mod.DoIt($mod.c);',
  8126. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8127. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8128. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8129. '$mod.j = $mod.c / 10000;',
  8130. '$mod.Write($mod.c / 10000);',
  8131. '$mod.c = 0;',
  8132. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8133. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8134. '']));
  8135. end;
  8136. procedure TTestModule.TestForBoolDo;
  8137. begin
  8138. StartProgram(false);
  8139. Add([
  8140. 'var b: boolean;',
  8141. 'begin',
  8142. ' for b:=false to true do ;',
  8143. ' for b:=b downto false do ;',
  8144. ' for b in boolean do ;',
  8145. '']);
  8146. ConvertProgram;
  8147. CheckSource('TestForBoolDo',
  8148. LinesToStr([ // statements
  8149. 'this.b = false;']),
  8150. LinesToStr([ // this.$main
  8151. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8152. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8153. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8154. '']));
  8155. end;
  8156. procedure TTestModule.TestForIntDo;
  8157. begin
  8158. StartProgram(false);
  8159. Add([
  8160. 'var i: longint;',
  8161. 'begin',
  8162. ' for i:=3 to 5 do ;',
  8163. ' for i:=i downto 2 do ;',
  8164. ' for i in byte do ;',
  8165. '']);
  8166. ConvertProgram;
  8167. CheckSource('TestForIntDo',
  8168. LinesToStr([ // statements
  8169. 'this.i = 0;']),
  8170. LinesToStr([ // this.$main
  8171. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8172. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8173. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8174. '']));
  8175. end;
  8176. procedure TTestModule.TestForIntInDo;
  8177. begin
  8178. StartProgram(false);
  8179. Add([
  8180. 'type',
  8181. ' TSetOfInt = set of byte;',
  8182. ' TIntRg = 3..7;',
  8183. ' TSetOfIntRg = set of TIntRg;',
  8184. 'var',
  8185. ' i,i2: longint;',
  8186. ' a1: array of byte;',
  8187. ' a2: array[1..3] of byte;',
  8188. ' soi: TSetOfInt;',
  8189. ' soir: TSetOfIntRg;',
  8190. ' ir: TIntRg;',
  8191. 'begin',
  8192. ' for i in byte do ;',
  8193. ' for i in a1 do ;',
  8194. ' for i in a2 do ;',
  8195. ' for i in [11..13] do ;',
  8196. ' for i in TSetOfInt do ;',
  8197. ' for i in TIntRg do ;',
  8198. ' for i in soi do i2:=i;',
  8199. ' for i in TSetOfIntRg do ;',
  8200. ' for i in soir do ;',
  8201. ' for ir in TIntRg do ;',
  8202. ' for ir in TSetOfIntRg do ;',
  8203. ' for ir in soir do ;',
  8204. '']);
  8205. ConvertProgram;
  8206. CheckSource('TestForIntInDo',
  8207. LinesToStr([ // statements
  8208. 'this.i = 0;',
  8209. 'this.i2 = 0;',
  8210. 'this.a1 = [];',
  8211. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8212. 'this.soi = {};',
  8213. 'this.soir = {};',
  8214. 'this.ir = 0;',
  8215. '']),
  8216. LinesToStr([ // this.$main
  8217. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8218. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8219. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8220. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8221. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8222. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8223. 'for (var $l6 in $mod.soi) {',
  8224. ' $mod.i = +$l6;',
  8225. ' $mod.i2 = $mod.i;',
  8226. '};',
  8227. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8228. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8229. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8230. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8231. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8232. '']));
  8233. end;
  8234. procedure TTestModule.TestCharConst;
  8235. begin
  8236. StartProgram(false);
  8237. Add([
  8238. 'const',
  8239. ' a = #$00F3;',
  8240. ' c: char = ''1'';',
  8241. ' wc: widechar = ''ä'';',
  8242. 'begin',
  8243. ' c:=#0;',
  8244. ' c:=#1;',
  8245. ' c:=#9;',
  8246. ' c:=#10;',
  8247. ' c:=#13;',
  8248. ' c:=#31;',
  8249. ' c:=#32;',
  8250. ' c:=#$A;',
  8251. ' c:=#$0A;',
  8252. ' c:=#$b;',
  8253. ' c:=#$0b;',
  8254. ' c:=^A;',
  8255. ' c:=''"'';',
  8256. ' c:=default(char);',
  8257. ' c:=#$00E4;', // ä
  8258. ' c:=''ä'';',
  8259. ' c:=#$E4;', // ä
  8260. ' c:=#$D800;', // invalid UTF-16
  8261. ' c:=#$DFFF;', // invalid UTF-16
  8262. ' c:=#$FFFF;', // last UCS-2
  8263. ' c:=high(c);', // last UCS-2
  8264. ' c:=#269;',
  8265. '']);
  8266. ConvertProgram;
  8267. CheckSource('TestCharConst',
  8268. LinesToStr([
  8269. 'this.a="ó";',
  8270. 'this.c="1";',
  8271. 'this.wc="ä";'
  8272. ]),
  8273. LinesToStr([
  8274. '$mod.c="\x00";',
  8275. '$mod.c="\x01";',
  8276. '$mod.c="\t";',
  8277. '$mod.c="\n";',
  8278. '$mod.c="\r";',
  8279. '$mod.c="\x1F";',
  8280. '$mod.c=" ";',
  8281. '$mod.c="\n";',
  8282. '$mod.c="\n";',
  8283. '$mod.c="\x0B";',
  8284. '$mod.c="\x0B";',
  8285. '$mod.c="\x01";',
  8286. '$mod.c=''"'';',
  8287. '$mod.c="\x00";',
  8288. '$mod.c = "ä";',
  8289. '$mod.c = "ä";',
  8290. '$mod.c = "ä";',
  8291. '$mod.c="\uD800";',
  8292. '$mod.c="\uDFFF";',
  8293. '$mod.c="\uFFFF";',
  8294. '$mod.c="\uFFFF";',
  8295. '$mod.c = "č";',
  8296. '']));
  8297. end;
  8298. procedure TTestModule.TestChar_Compare;
  8299. begin
  8300. StartProgram(false);
  8301. Add('var');
  8302. Add(' c: char;');
  8303. Add(' b: boolean;');
  8304. Add('begin');
  8305. Add(' b:=c=''1'';');
  8306. Add(' b:=''2''=c;');
  8307. Add(' b:=''3''=''4'';');
  8308. Add(' b:=c<>''5'';');
  8309. Add(' b:=''6''<>c;');
  8310. Add(' b:=c>''7'';');
  8311. Add(' b:=''8''>c;');
  8312. Add(' b:=c>=''9'';');
  8313. Add(' b:=''A''>=c;');
  8314. Add(' b:=c<''B'';');
  8315. Add(' b:=''C''<c;');
  8316. Add(' b:=c<=''D'';');
  8317. Add(' b:=''E''<=c;');
  8318. ConvertProgram;
  8319. CheckSource('TestChar_Compare',
  8320. LinesToStr([
  8321. 'this.c="";',
  8322. 'this.b = false;'
  8323. ]),
  8324. LinesToStr([
  8325. '$mod.b = $mod.c === "1";',
  8326. '$mod.b = "2" === $mod.c;',
  8327. '$mod.b = "3" === "4";',
  8328. '$mod.b = $mod.c !== "5";',
  8329. '$mod.b = "6" !== $mod.c;',
  8330. '$mod.b = $mod.c > "7";',
  8331. '$mod.b = "8" > $mod.c;',
  8332. '$mod.b = $mod.c >= "9";',
  8333. '$mod.b = "A" >= $mod.c;',
  8334. '$mod.b = $mod.c < "B";',
  8335. '$mod.b = "C" < $mod.c;',
  8336. '$mod.b = $mod.c <= "D";',
  8337. '$mod.b = "E" <= $mod.c;',
  8338. '']));
  8339. end;
  8340. procedure TTestModule.TestChar_BuiltInProcs;
  8341. begin
  8342. StartProgram(false);
  8343. Add([
  8344. 'var',
  8345. ' c: char;',
  8346. ' i: longint;',
  8347. ' s: string;',
  8348. 'begin',
  8349. ' i:=ord(c);',
  8350. ' i:=ord(s[i]);',
  8351. ' c:=chr(i);',
  8352. ' c:=pred(c);',
  8353. ' c:=succ(c);',
  8354. ' c:=low(c);',
  8355. ' c:=high(c);',
  8356. ' i:=byte(c);',
  8357. ' i:=word(c);',
  8358. ' i:=longint(c);',
  8359. '']);
  8360. ConvertProgram;
  8361. CheckSource('TestChar_BuiltInProcs',
  8362. LinesToStr([
  8363. 'this.c = "";',
  8364. 'this.i = 0;',
  8365. 'this.s = "";'
  8366. ]),
  8367. LinesToStr([
  8368. '$mod.i = $mod.c.charCodeAt();',
  8369. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8370. '$mod.c = String.fromCharCode($mod.i);',
  8371. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8372. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8373. '$mod.c = "\x00";',
  8374. '$mod.c = "\uFFFF";',
  8375. '$mod.i = $mod.c.charCodeAt() & 255;',
  8376. '$mod.i = $mod.c.charCodeAt();',
  8377. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8378. '']));
  8379. end;
  8380. procedure TTestModule.TestStringConst;
  8381. begin
  8382. StartProgram(false);
  8383. Add([
  8384. '{$H+}',
  8385. 'const',
  8386. ' a = #$00F3#$017C;', // first <256, then >=256
  8387. ' b = string(''a'');',
  8388. ' c = string(''ä'');',
  8389. ' d = UnicodeString(''b'');',
  8390. ' e = UnicodeString(''ö'');',
  8391. ' f = low(a)+high(b);',
  8392. ' g: word = low(a);',
  8393. 'var',
  8394. ' s: string = ''abc'';',
  8395. ' i: longint;',
  8396. 'begin',
  8397. ' s:='''';',
  8398. ' s:=#13#10;',
  8399. ' s:=#9''foo'';',
  8400. ' s:=#$A9;',
  8401. ' s:=''foo''#13''bar'';',
  8402. ' s:=''"'';',
  8403. ' s:=''"''''"'';',
  8404. ' s:=#$20AC;', // euro
  8405. ' s:=#$10437;', // outside BMP
  8406. ' s:=''abc''#$20AC;', // ascii,#
  8407. ' s:=''ä''#$20AC;', // non ascii,#
  8408. ' s:=#$20AC''abc'';', // #, ascii
  8409. ' s:=#$20AC''ä'';', // #, non ascii
  8410. ' s:=default(string);',
  8411. ' s:=concat(s);',
  8412. ' s:=concat(s,''a'',s);',
  8413. ' s:=#250#269;',
  8414. ' i:=low(s)+high(a);',
  8415. ' s:=''a/b'';',
  8416. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8417. '']);
  8418. ConvertProgram;
  8419. CheckSource('TestStringConst',
  8420. LinesToStr([
  8421. 'this.a = "óż";',
  8422. 'this.b = "a";',
  8423. 'this.c = "ä";',
  8424. 'this.d = "b";',
  8425. 'this.e = "ö";',
  8426. 'this.f = 1 + this.b.length;',
  8427. 'this.g = 1;',
  8428. 'this.s="abc";',
  8429. 'this.i = 0;',
  8430. '']),
  8431. LinesToStr([
  8432. '$mod.s="";',
  8433. '$mod.s="\r\n";',
  8434. '$mod.s="\tfoo";',
  8435. '$mod.s="©";',
  8436. '$mod.s="foo\rbar";',
  8437. '$mod.s=''"'';',
  8438. '$mod.s=''"\''"'';',
  8439. '$mod.s="€";',
  8440. '$mod.s="'#$F0#$90#$90#$B7'";',
  8441. '$mod.s = "abc€";',
  8442. '$mod.s = "ä€";',
  8443. '$mod.s = "€abc";',
  8444. '$mod.s = "ۊ";',
  8445. '$mod.s="";',
  8446. '$mod.s = $mod.s;',
  8447. '$mod.s = $mod.s.concat("a", $mod.s);',
  8448. '$mod.s = "úč";',
  8449. '$mod.i = 1 + $mod.a.length;',
  8450. '$mod.s = "a/b";',
  8451. '']));
  8452. end;
  8453. procedure TTestModule.TestStringConst_InvalidUTF16;
  8454. begin
  8455. StartProgram(false);
  8456. Add([
  8457. 'const',
  8458. ' a: char = #$D87E;',
  8459. ' b: string = #$D87E;',
  8460. ' c: string = #$D87E#43;',
  8461. 'begin',
  8462. ' c:=''abc''#$D87E;',
  8463. ' c:=#0#1#2;',
  8464. ' c:=#127;',
  8465. ' c:=#128;',
  8466. ' c:=#255;',
  8467. ' c:=#256;',
  8468. '']);
  8469. ConvertProgram;
  8470. CheckSource('TestStringConst',
  8471. LinesToStr([
  8472. 'this.a = "\uD87E";',
  8473. 'this.b = "\uD87E";',
  8474. 'this.c = "\uD87E+";',
  8475. '']),
  8476. LinesToStr([
  8477. '$mod.c = "abc\uD87E";',
  8478. '$mod.c = "\x00\x01\x02";',
  8479. '$mod.c = "'#127'";',
  8480. '$mod.c = "'#$c2#$80'";',
  8481. '$mod.c = "'#$c3#$BF'";',
  8482. '$mod.c = "'#$c4#$80'";',
  8483. '']));
  8484. end;
  8485. procedure TTestModule.TestStringConstSurrogate;
  8486. begin
  8487. StartProgram(false);
  8488. Add([
  8489. 'var',
  8490. ' s: string;',
  8491. 'begin',
  8492. ' s:=''😊'';', // 1F60A
  8493. '']);
  8494. ConvertProgram;
  8495. CheckSource('TestStringConstSurrogate',
  8496. LinesToStr([
  8497. 'this.s="";'
  8498. ]),
  8499. LinesToStr([
  8500. '$mod.s="😊";'
  8501. ]));
  8502. end;
  8503. procedure TTestModule.TestStringConst_Multiline;
  8504. begin
  8505. StartProgram(false);
  8506. Add([
  8507. '{$modeswitch multilinestrings}',
  8508. 'const',
  8509. ' a = ``;',
  8510. ' b = `',
  8511. 'line`;',
  8512. ' c = `Single`;',
  8513. ' d = ````;',
  8514. ' e = `abc``xyz`;',
  8515. ' f = `first''line',
  8516. ' second''line`#10;',
  8517. 'begin',
  8518. '']);
  8519. ConvertProgram;
  8520. CheckSource('TestStringConst_Multiline',
  8521. LinesToStr([
  8522. 'this.a = "";',
  8523. 'this.b = "\nline";',
  8524. 'this.c = "Single";',
  8525. 'this.d = "`";',
  8526. 'this.e = "abc`xyz";',
  8527. 'this.f = "first''line\n second''line\n";',
  8528. '']),
  8529. LinesToStr([
  8530. ]));
  8531. end;
  8532. procedure TTestModule.TestString_Length;
  8533. begin
  8534. StartProgram(false);
  8535. Add('const c = ''foo'';');
  8536. Add('var');
  8537. Add(' s: string;');
  8538. Add(' i: longint;');
  8539. Add('begin');
  8540. Add(' i:=length(s);');
  8541. Add(' i:=length(s+s);');
  8542. Add(' i:=length(''abc'');');
  8543. Add(' i:=length(c);');
  8544. ConvertProgram;
  8545. CheckSource('TestString_Length',
  8546. LinesToStr([
  8547. 'this.c = "foo";',
  8548. 'this.s = "";',
  8549. 'this.i = 0;',
  8550. '']),
  8551. LinesToStr([
  8552. '$mod.i = $mod.s.length;',
  8553. '$mod.i = ($mod.s+$mod.s).length;',
  8554. '$mod.i = "abc".length;',
  8555. '$mod.i = $mod.c.length;',
  8556. '']));
  8557. end;
  8558. procedure TTestModule.TestString_Compare;
  8559. begin
  8560. StartProgram(false);
  8561. Add('var');
  8562. Add(' s, t: string;');
  8563. Add(' b: boolean;');
  8564. Add('begin');
  8565. Add(' b:=s=t;');
  8566. Add(' b:=s<>t;');
  8567. Add(' b:=s>t;');
  8568. Add(' b:=s>=t;');
  8569. Add(' b:=s<t;');
  8570. Add(' b:=s<=t;');
  8571. ConvertProgram;
  8572. CheckSource('TestString_Compare',
  8573. LinesToStr([ // statements
  8574. 'this.s = "";',
  8575. 'this.t = "";',
  8576. 'this.b =false;'
  8577. ]),
  8578. LinesToStr([ // this.$main
  8579. '$mod.b = $mod.s === $mod.t;',
  8580. '$mod.b = $mod.s !== $mod.t;',
  8581. '$mod.b = $mod.s > $mod.t;',
  8582. '$mod.b = $mod.s >= $mod.t;',
  8583. '$mod.b = $mod.s < $mod.t;',
  8584. '$mod.b = $mod.s <= $mod.t;',
  8585. '']));
  8586. end;
  8587. procedure TTestModule.TestString_SetLength;
  8588. begin
  8589. StartProgram(false);
  8590. Add([
  8591. 'procedure Fly(var s: string);',
  8592. 'begin',
  8593. ' SetLength(s,1);',
  8594. 'end;',
  8595. 'procedure Run(var s: unicodestring);',
  8596. 'begin',
  8597. ' SetLength(s,2);',
  8598. 'end;',
  8599. 'var s: string;',
  8600. ' u: unicodestring;',
  8601. 'begin',
  8602. ' SetLength(s,3);',
  8603. ' SetLength(u,4);',
  8604. '']);
  8605. ConvertProgram;
  8606. CheckSource('TestString_SetLength',
  8607. LinesToStr([ // statements
  8608. 'this.Fly = function (s) {',
  8609. ' s.set(rtl.strSetLength(s.get(), 1));',
  8610. '};',
  8611. 'this.Run = function (s) {',
  8612. ' s.set(rtl.strSetLength(s.get(), 2));',
  8613. '};',
  8614. 'this.s = "";',
  8615. 'this.u = "";',
  8616. '']),
  8617. LinesToStr([ // this.$main
  8618. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8619. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8620. ]));
  8621. end;
  8622. procedure TTestModule.TestString_CharAt;
  8623. begin
  8624. StartProgram(false);
  8625. Add([
  8626. 'var',
  8627. ' s: string;',
  8628. ' c: char;',
  8629. ' b: boolean;',
  8630. 'begin',
  8631. ' b:= s[1] = c;',
  8632. ' b:= c = s[1];',
  8633. ' b:= c <> s[1];',
  8634. ' b:= c > s[1];',
  8635. ' b:= c >= s[1];',
  8636. ' b:= c < s[2];',
  8637. ' b:= c <= s[1];',
  8638. ' s[1] := c;',
  8639. ' s[2+3] := c;']);
  8640. ConvertProgram;
  8641. CheckSource('TestString_CharAt',
  8642. LinesToStr([ // statements
  8643. 'this.s = "";',
  8644. 'this.c = "";',
  8645. 'this.b = false;'
  8646. ]),
  8647. LinesToStr([ // this.$main
  8648. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8649. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8650. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8651. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8652. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8653. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8654. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8655. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8656. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8657. '']));
  8658. end;
  8659. procedure TTestModule.TestStringHMinusFail;
  8660. begin
  8661. StartProgram(false);
  8662. Add([
  8663. '{$H-}',
  8664. 'var s: string;',
  8665. 'begin']);
  8666. ConvertProgram;
  8667. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8668. end;
  8669. procedure TTestModule.TestStr;
  8670. begin
  8671. StartProgram(false);
  8672. Add('var');
  8673. Add(' b: boolean;');
  8674. Add(' i: longint;');
  8675. Add(' d: double;');
  8676. Add(' s: string;');
  8677. Add('begin');
  8678. Add(' str(b,s);');
  8679. Add(' str(i,s);');
  8680. Add(' str(d,s);');
  8681. Add(' str(i:3,s);');
  8682. Add(' str(d:3:2,s);');
  8683. Add(' Str(12.456:12:1,s);');
  8684. Add(' Str(12.456:12,s);');
  8685. Add(' s:=str(b);');
  8686. Add(' s:=str(i);');
  8687. Add(' s:=str(d);');
  8688. Add(' s:=str(i,i);');
  8689. Add(' s:=str(i:3);');
  8690. Add(' s:=str(d:3:2);');
  8691. Add(' s:=str(i:4,i);');
  8692. Add(' s:=str(i,i:5);');
  8693. Add(' s:=str(i:4,i:5);');
  8694. Add(' s:=str(s,s);');
  8695. Add(' s:=str(s,''foo'');');
  8696. ConvertProgram;
  8697. CheckSource('TestStr',
  8698. LinesToStr([ // statements
  8699. 'this.b = false;',
  8700. 'this.i = 0;',
  8701. 'this.d = 0.0;',
  8702. 'this.s = "";',
  8703. '']),
  8704. LinesToStr([ // this.$main
  8705. '$mod.s = ""+$mod.b;',
  8706. '$mod.s = ""+$mod.i;',
  8707. '$mod.s = rtl.floatToStr($mod.d);',
  8708. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8709. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8710. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8711. '$mod.s = rtl.floatToStr(12.456,12);',
  8712. '$mod.s = ""+$mod.b;',
  8713. '$mod.s = ""+$mod.i;',
  8714. '$mod.s = rtl.floatToStr($mod.d);',
  8715. '$mod.s = ""+$mod.i+$mod.i;',
  8716. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8717. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8718. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8719. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8720. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8721. '$mod.s = $mod.s + $mod.s;',
  8722. '$mod.s = $mod.s + "foo";',
  8723. '']));
  8724. end;
  8725. procedure TTestModule.TestBaseType_AnsiStringFail;
  8726. begin
  8727. StartProgram(false);
  8728. Add('var s: AnsiString');
  8729. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8730. ConvertProgram;
  8731. end;
  8732. procedure TTestModule.TestBaseType_WideStringFail;
  8733. begin
  8734. StartProgram(false);
  8735. Add('var s: WideString');
  8736. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8737. ConvertProgram;
  8738. end;
  8739. procedure TTestModule.TestBaseType_ShortStringFail;
  8740. begin
  8741. StartProgram(false);
  8742. Add('var s: ShortString');
  8743. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8744. ConvertProgram;
  8745. end;
  8746. procedure TTestModule.TestBaseType_RawByteStringFail;
  8747. begin
  8748. StartProgram(false);
  8749. Add('var s: RawByteString');
  8750. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8751. ConvertProgram;
  8752. end;
  8753. procedure TTestModule.TestTypeShortstring_Fail;
  8754. begin
  8755. StartProgram(false);
  8756. Add('type t = string[12];');
  8757. Add('var s: t;');
  8758. Add('begin');
  8759. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8760. ConvertProgram;
  8761. end;
  8762. procedure TTestModule.TestCharSet_Custom;
  8763. begin
  8764. StartProgram(false);
  8765. Add([
  8766. 'type',
  8767. ' TCharRg = ''a''..''z'';',
  8768. ' TSetOfCharRg = set of TCharRg;',
  8769. ' TCharRg2 = ''m''..''p'';',
  8770. 'const',
  8771. ' crg: TCharRg = ''b'';',
  8772. 'var',
  8773. ' c: char;',
  8774. ' crg2: TCharRg2;',
  8775. ' s: TSetOfCharRg;',
  8776. 'begin',
  8777. ' c:=crg;',
  8778. ' crg:=c;',
  8779. ' crg2:=crg;',
  8780. ' if c=crg then ;',
  8781. ' if crg=c then ;',
  8782. ' if crg=crg2 then ;',
  8783. ' if c in s then ;',
  8784. ' if crg2 in s then ;',
  8785. ' c:=default(TCharRg);',
  8786. '']);
  8787. ConvertProgram;
  8788. CheckSource('TestCharSet_Custom',
  8789. LinesToStr([ // statements
  8790. 'this.crg = "b";',
  8791. 'this.c = "";',
  8792. 'this.crg2 = "m";',
  8793. 'this.s = {};',
  8794. '']),
  8795. LinesToStr([ // this.$main
  8796. '$mod.c = $mod.crg;',
  8797. '$mod.crg = $mod.c;',
  8798. '$mod.crg2 = $mod.crg;',
  8799. 'if ($mod.c === $mod.crg) ;',
  8800. 'if ($mod.crg === $mod.c) ;',
  8801. 'if ($mod.crg === $mod.crg2) ;',
  8802. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8803. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8804. '$mod.c = "a";',
  8805. '']));
  8806. end;
  8807. procedure TTestModule.TestWideChar;
  8808. begin
  8809. StartProgram(false);
  8810. Add([
  8811. 'procedure Fly(var c: char);',
  8812. 'begin',
  8813. 'end;',
  8814. 'procedure Run(var c: widechar);',
  8815. 'begin',
  8816. 'end;',
  8817. 'var',
  8818. ' c: char;',
  8819. ' wc: widechar;',
  8820. ' w: word;',
  8821. 'begin',
  8822. ' Fly(wc);',
  8823. ' Run(c);',
  8824. ' wc:=WideChar(w);',
  8825. ' w:=ord(wc);',
  8826. '']);
  8827. ConvertProgram;
  8828. CheckSource('TestWideChar_VarArg',
  8829. LinesToStr([ // statements
  8830. 'this.Fly = function (c) {',
  8831. '};',
  8832. 'this.Run = function (c) {',
  8833. '};',
  8834. 'this.c = "";',
  8835. 'this.wc = "";',
  8836. 'this.w = 0;',
  8837. '']),
  8838. LinesToStr([ // this.$main
  8839. '$mod.Fly({',
  8840. ' p: $mod,',
  8841. ' get: function () {',
  8842. ' return this.p.wc;',
  8843. ' },',
  8844. ' set: function (v) {',
  8845. ' this.p.wc = v;',
  8846. ' }',
  8847. '});',
  8848. '$mod.Run({',
  8849. ' p: $mod,',
  8850. ' get: function () {',
  8851. ' return this.p.c;',
  8852. ' },',
  8853. ' set: function (v) {',
  8854. ' this.p.c = v;',
  8855. ' }',
  8856. '});',
  8857. '$mod.wc = String.fromCharCode($mod.w);',
  8858. '$mod.w = $mod.wc.charCodeAt();',
  8859. '',
  8860. '']));
  8861. end;
  8862. procedure TTestModule.TestForCharDo;
  8863. begin
  8864. StartProgram(false);
  8865. Add([
  8866. 'var c: char;',
  8867. 'begin',
  8868. ' for c:=''a'' to ''c'' do ;',
  8869. ' for c:=c downto ''a'' do ;',
  8870. ' for c:=''Б'' to ''Я'' do ;',
  8871. '']);
  8872. ConvertProgram;
  8873. CheckSource('TestForCharDo',
  8874. LinesToStr([ // statements
  8875. 'this.c = "";']),
  8876. LinesToStr([ // this.$main
  8877. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8878. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8879. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8880. '']));
  8881. end;
  8882. procedure TTestModule.TestForCharInDo;
  8883. begin
  8884. StartProgram(false);
  8885. Add([
  8886. 'type',
  8887. ' TSetOfChar = set of char;',
  8888. ' TCharRg = ''a''..''z'';',
  8889. ' TSetOfCharRg = set of TCharRg;',
  8890. 'const Foo = ''foo'';',
  8891. 'var',
  8892. ' c,c2: char;',
  8893. ' s: string;',
  8894. ' a1: array of char;',
  8895. ' a2: array[1..3] of char;',
  8896. ' soc: TSetOfChar;',
  8897. ' socr: TSetOfCharRg;',
  8898. ' cr: TCharRg;',
  8899. 'begin',
  8900. ' for c in foo do ;',
  8901. ' for c in s do ;',
  8902. ' for c in char do ;',
  8903. ' for c in a1 do ;',
  8904. ' for c in a2 do ;',
  8905. ' for c in [''1''..''3''] do ;',
  8906. ' for c in TSetOfChar do ;',
  8907. ' for c in TCharRg do ;',
  8908. ' for c in soc do c2:=c;',
  8909. ' for c in TSetOfCharRg do ;',
  8910. ' for c in socr do ;',
  8911. ' for cr in TCharRg do ;',
  8912. ' for cr in TSetOfCharRg do ;',
  8913. ' for cr in socr do ;',
  8914. '']);
  8915. ConvertProgram;
  8916. CheckSource('TestForCharInDo',
  8917. LinesToStr([ // statements
  8918. 'this.Foo = "foo";',
  8919. 'this.c = "";',
  8920. 'this.c2 = "";',
  8921. 'this.s = "";',
  8922. 'this.a1 = [];',
  8923. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8924. 'this.soc = {};',
  8925. 'this.socr = {};',
  8926. 'this.cr = "a";',
  8927. '']),
  8928. LinesToStr([ // this.$main
  8929. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8930. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8931. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8932. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8933. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8934. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8935. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8936. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8937. 'for (var $l8 in $mod.soc) {',
  8938. ' $mod.c = String.fromCharCode($l8);',
  8939. ' $mod.c2 = $mod.c;',
  8940. '};',
  8941. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8942. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8943. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8944. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8945. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8946. '']));
  8947. end;
  8948. procedure TTestModule.TestProcTwoArgs;
  8949. begin
  8950. StartProgram(false);
  8951. Add('procedure Test(a,b: longint);');
  8952. Add('begin');
  8953. Add('end;');
  8954. Add('begin');
  8955. ConvertProgram;
  8956. CheckSource('TestProcTwoArgs',
  8957. LinesToStr([ // statements
  8958. 'this.Test = function (a,b) {',
  8959. '};'
  8960. ]),
  8961. LinesToStr([ // this.$main
  8962. ''
  8963. ]));
  8964. end;
  8965. procedure TTestModule.TestProc_DefaultValue;
  8966. begin
  8967. StartProgram(false);
  8968. Add('procedure p1(i: longint = 1);');
  8969. Add('begin');
  8970. Add('end;');
  8971. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8972. Add('begin');
  8973. Add('end;');
  8974. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8975. Add('begin');
  8976. Add('end;');
  8977. Add('begin');
  8978. Add(' p1;');
  8979. Add(' p1();');
  8980. Add(' p1(11);');
  8981. Add(' p2;');
  8982. Add(' p2();');
  8983. Add(' p2(12);');
  8984. Add(' p2(13,''b'');');
  8985. Add(' p3();');
  8986. ConvertProgram;
  8987. CheckSource('TestProc_DefaultValue',
  8988. LinesToStr([ // statements
  8989. 'this.p1 = function (i) {',
  8990. '};',
  8991. 'this.p2 = function (i,c) {',
  8992. '};',
  8993. 'this.p3 = function (d,b,s) {',
  8994. '};'
  8995. ]),
  8996. LinesToStr([ // this.$main
  8997. ' $mod.p1(1);',
  8998. ' $mod.p1(1);',
  8999. ' $mod.p1(11);',
  9000. ' $mod.p2(1,"a");',
  9001. ' $mod.p2(1,"a");',
  9002. ' $mod.p2(12,"a");',
  9003. ' $mod.p2(13,"b");',
  9004. ' $mod.p3(1.0,false,"abc");'
  9005. ]));
  9006. end;
  9007. procedure TTestModule.TestFunctionInt;
  9008. begin
  9009. StartProgram(false);
  9010. Add('function MyTest(Bar: longint): longint;');
  9011. Add('begin');
  9012. Add(' Result:=2*bar');
  9013. Add('end;');
  9014. Add('begin');
  9015. ConvertProgram;
  9016. CheckSource('TestFunctionInt',
  9017. LinesToStr([ // statements
  9018. 'this.MyTest = function (Bar) {',
  9019. ' var Result = 0;',
  9020. ' Result = 2*Bar;',
  9021. ' return Result;',
  9022. '};'
  9023. ]),
  9024. LinesToStr([ // this.$main
  9025. ''
  9026. ]));
  9027. end;
  9028. procedure TTestModule.TestFunctionString;
  9029. begin
  9030. StartProgram(false);
  9031. Add('function Test(Bar: string): string;');
  9032. Add('begin');
  9033. Add(' Result:=bar+BAR');
  9034. Add('end;');
  9035. Add('begin');
  9036. ConvertProgram;
  9037. CheckSource('TestFunctionString',
  9038. LinesToStr([ // statements
  9039. 'this.Test = function (Bar) {',
  9040. ' var Result = "";',
  9041. ' Result = Bar+Bar;',
  9042. ' return Result;',
  9043. '};'
  9044. ]),
  9045. LinesToStr([ // this.$main
  9046. ''
  9047. ]));
  9048. end;
  9049. procedure TTestModule.TestIfThen;
  9050. begin
  9051. StartProgram(false);
  9052. Add([
  9053. 'var b: boolean;',
  9054. 'begin',
  9055. ' if b then ;',
  9056. ' if b then else ;']);
  9057. ConvertProgram;
  9058. CheckSource('TestIfThen',
  9059. LinesToStr([ // statements
  9060. 'this.b = false;',
  9061. '']),
  9062. LinesToStr([ // this.$main
  9063. 'if ($mod.b) ;',
  9064. 'if ($mod.b) ;',
  9065. '']));
  9066. end;
  9067. procedure TTestModule.TestForLoop;
  9068. begin
  9069. StartProgram(false);
  9070. Add('var');
  9071. Add(' vI, vJ, vN: longint;');
  9072. Add('begin');
  9073. Add(' VJ:=0;');
  9074. Add(' VN:=3;');
  9075. Add(' for VI:=1 to VN do');
  9076. Add(' begin');
  9077. Add(' VJ:=VJ+VI;');
  9078. Add(' end;');
  9079. ConvertProgram;
  9080. CheckSource('TestForLoop',
  9081. LinesToStr([ // statements
  9082. 'this.vI = 0;',
  9083. 'this.vJ = 0;',
  9084. 'this.vN = 0;'
  9085. ]),
  9086. LinesToStr([ // this.$main
  9087. ' $mod.vJ = 0;',
  9088. ' $mod.vN = 3;',
  9089. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9090. ' $mod.vI = $l;',
  9091. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9092. ' };',
  9093. '']));
  9094. end;
  9095. procedure TTestModule.TestForLoopInsideFunction;
  9096. begin
  9097. StartProgram(false);
  9098. Add('function SumNumbers(Count: longint): longint;');
  9099. Add('var');
  9100. Add(' vI, vJ: longint;');
  9101. Add('begin');
  9102. Add(' vj:=0;');
  9103. Add(' for vi:=1 to count do');
  9104. Add(' begin');
  9105. Add(' vj:=vj+vi;');
  9106. Add(' end;');
  9107. Add('end;');
  9108. Add('begin');
  9109. Add(' sumnumbers(3);');
  9110. ConvertProgram;
  9111. CheckSource('TestForLoopInsideFunction',
  9112. LinesToStr([ // statements
  9113. 'this.SumNumbers = function (Count) {',
  9114. ' var Result = 0;',
  9115. ' var vI = 0;',
  9116. ' var vJ = 0;',
  9117. ' vJ = 0;',
  9118. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9119. ' vI = $l;',
  9120. ' vJ = vJ + vI;',
  9121. ' };',
  9122. ' return Result;',
  9123. '};'
  9124. ]),
  9125. LinesToStr([ // $mod.$main
  9126. ' $mod.SumNumbers(3);'
  9127. ]));
  9128. end;
  9129. procedure TTestModule.TestForLoop_ReadVarAfter;
  9130. begin
  9131. StartProgram(false);
  9132. Add('var');
  9133. Add(' vI: longint;');
  9134. Add('begin');
  9135. Add(' for vi:=1 to 2 do ;');
  9136. Add(' if vi=3 then ;');
  9137. ConvertProgram;
  9138. CheckSource('TestForLoop',
  9139. LinesToStr([ // statements
  9140. 'this.vI = 0;'
  9141. ]),
  9142. LinesToStr([ // this.$main
  9143. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9144. ' if ($mod.vI===3) ;'
  9145. ]));
  9146. end;
  9147. procedure TTestModule.TestForLoop_Nested;
  9148. begin
  9149. StartProgram(false);
  9150. Add('function SumNumbers(Count: longint): longint;');
  9151. Add('var');
  9152. Add(' vI, vJ, vK: longint;');
  9153. Add('begin');
  9154. Add(' VK:=0;');
  9155. Add(' for VI:=1 to count do');
  9156. Add(' begin');
  9157. Add(' for vj:=1 to vi do');
  9158. Add(' begin');
  9159. Add(' vk:=VK+VI;');
  9160. Add(' end;');
  9161. Add(' end;');
  9162. Add('end;');
  9163. Add('begin');
  9164. Add(' sumnumbers(3);');
  9165. ConvertProgram;
  9166. CheckSource('TestForLoopInFunction',
  9167. LinesToStr([ // statements
  9168. 'this.SumNumbers = function (Count) {',
  9169. ' var Result = 0;',
  9170. ' var vI = 0;',
  9171. ' var vJ = 0;',
  9172. ' var vK = 0;',
  9173. ' vK = 0;',
  9174. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9175. ' vI = $l;',
  9176. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9177. ' vJ = $l1;',
  9178. ' vK = vK + vI;',
  9179. ' };',
  9180. ' };',
  9181. ' return Result;',
  9182. '};'
  9183. ]),
  9184. LinesToStr([ // $mod.$main
  9185. ' $mod.SumNumbers(3);'
  9186. ]));
  9187. end;
  9188. procedure TTestModule.TestRepeatUntil;
  9189. begin
  9190. StartProgram(false);
  9191. Add('var');
  9192. Add(' vI, vJ, vN: longint;');
  9193. Add('begin');
  9194. Add(' vn:=3;');
  9195. Add(' vj:=0;');
  9196. Add(' VI:=0;');
  9197. Add(' repeat');
  9198. Add(' VI:=vi+1;');
  9199. Add(' vj:=VJ+vI;');
  9200. Add(' until vi>=vn');
  9201. ConvertProgram;
  9202. CheckSource('TestRepeatUntil',
  9203. LinesToStr([ // statements
  9204. 'this.vI = 0;',
  9205. 'this.vJ = 0;',
  9206. 'this.vN = 0;'
  9207. ]),
  9208. LinesToStr([ // $mod.$main
  9209. ' $mod.vN = 3;',
  9210. ' $mod.vJ = 0;',
  9211. ' $mod.vI = 0;',
  9212. ' do{',
  9213. ' $mod.vI = $mod.vI + 1;',
  9214. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9215. ' }while(!($mod.vI>=$mod.vN));'
  9216. ]));
  9217. end;
  9218. procedure TTestModule.TestAsmBlock;
  9219. begin
  9220. StartProgram(false);
  9221. Add([
  9222. 'var',
  9223. ' vI: longint;',
  9224. 'begin',
  9225. ' vi:=1;',
  9226. ' asm',
  9227. ' if (vI===1) {',
  9228. ' vI=2;',
  9229. //' console.log(''end;'');', ToDo
  9230. ' }',
  9231. ' if (vI===2){ vI=3; }',
  9232. ' end;',
  9233. ' VI:=4;']);
  9234. ConvertProgram;
  9235. CheckSource('TestAsmBlock',
  9236. LinesToStr([ // statements
  9237. 'this.vI = 0;'
  9238. ]),
  9239. LinesToStr([ // $mod.$main
  9240. '$mod.vI = 1;',
  9241. 'if (vI===1) {',
  9242. ' vI=2;',
  9243. '}',
  9244. 'if (vI===2){ vI=3; }',
  9245. ';',
  9246. '$mod.vI = 4;'
  9247. ]));
  9248. end;
  9249. procedure TTestModule.TestAsmPas_Impl;
  9250. begin
  9251. StartUnit(false);
  9252. Add('interface');
  9253. Add('const cIntf: longint = 1;');
  9254. Add('var vIntf: longint;');
  9255. Add('implementation');
  9256. Add('const cImpl: longint = 2;');
  9257. Add('var vImpl: longint;');
  9258. Add('procedure DoIt;');
  9259. Add('const cLoc: longint = 3;');
  9260. Add('var vLoc: longint;');
  9261. Add('begin;');
  9262. Add(' asm');
  9263. //Add(' pas(vIntf)=pas(cIntf);');
  9264. //Add(' pas(vImpl)=pas(cImpl);');
  9265. //Add(' pas(vLoc)=pas(cLoc);');
  9266. Add(' end;');
  9267. Add('end;');
  9268. ConvertUnit;
  9269. CheckSource('TestAsmPas_Impl',
  9270. LinesToStr([
  9271. 'var $impl = $mod.$impl;',
  9272. 'this.cIntf = 1;',
  9273. 'this.vIntf = 0;',
  9274. '']),
  9275. '', // this.$init
  9276. LinesToStr([ // implementation
  9277. '$impl.cImpl = 2;',
  9278. '$impl.vImpl = 0;',
  9279. 'var cLoc = 3;',
  9280. '$impl.DoIt = function () {',
  9281. ' var vLoc = 0;',
  9282. '};',
  9283. '']) );
  9284. end;
  9285. procedure TTestModule.TestTryFinally;
  9286. begin
  9287. StartProgram(false);
  9288. Add('var i: longint;');
  9289. Add('begin');
  9290. Add(' try');
  9291. Add(' i:=0; i:=2 div i;');
  9292. Add(' finally');
  9293. Add(' i:=3');
  9294. Add(' end;');
  9295. ConvertProgram;
  9296. CheckSource('TestTryFinally',
  9297. LinesToStr([ // statements
  9298. 'this.i = 0;'
  9299. ]),
  9300. LinesToStr([ // $mod.$main
  9301. 'try {',
  9302. ' $mod.i = 0;',
  9303. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9304. '} finally {',
  9305. ' $mod.i = 3;',
  9306. '};'
  9307. ]));
  9308. end;
  9309. procedure TTestModule.TestTryExcept;
  9310. begin
  9311. StartProgram(false);
  9312. Add([
  9313. 'type',
  9314. ' TObject = class end;',
  9315. ' Exception = class Msg: string; end;',
  9316. ' EInvalidCast = class(Exception) end;',
  9317. 'var vI: longint;',
  9318. 'begin',
  9319. ' try',
  9320. ' vi:=1;',
  9321. ' except',
  9322. ' vi:=2',
  9323. ' end;',
  9324. ' try',
  9325. ' vi:=3;',
  9326. ' except',
  9327. ' raise;',
  9328. ' end;',
  9329. ' try',
  9330. ' VI:=4;',
  9331. ' except',
  9332. ' on einvalidcast do',
  9333. ' raise;',
  9334. ' on E: exception do',
  9335. ' if e.msg='''' then',
  9336. ' raise e;',
  9337. ' else',
  9338. ' vi:=5',
  9339. ' end;',
  9340. ' try',
  9341. ' VI:=6;',
  9342. ' except',
  9343. ' on einvalidcast do ;',
  9344. ' end;',
  9345. '']);
  9346. ConvertProgram;
  9347. CheckSource('TestTryExcept',
  9348. LinesToStr([ // statements
  9349. 'rtl.createClass(this, "TObject", null, function () {',
  9350. ' this.$init = function () {',
  9351. ' };',
  9352. ' this.$final = function () {',
  9353. ' };',
  9354. '});',
  9355. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9356. ' this.$init = function () {',
  9357. ' $mod.TObject.$init.call(this);',
  9358. ' this.Msg = "";',
  9359. ' };',
  9360. '});',
  9361. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9362. '});',
  9363. 'this.vI = 0;'
  9364. ]),
  9365. LinesToStr([ // $mod.$main
  9366. 'try {',
  9367. ' $mod.vI = 1;',
  9368. '} catch ($e) {',
  9369. ' $mod.vI = 2;',
  9370. '};',
  9371. 'try {',
  9372. ' $mod.vI = 3;',
  9373. '} catch ($e) {',
  9374. ' throw $e;',
  9375. '};',
  9376. 'try {',
  9377. ' $mod.vI = 4;',
  9378. '} catch ($e) {',
  9379. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9380. ' throw $e',
  9381. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9382. ' var E = $e;',
  9383. ' if (E.Msg === "") throw E;',
  9384. ' } else {',
  9385. ' $mod.vI = 5;',
  9386. ' }',
  9387. '};',
  9388. 'try {',
  9389. ' $mod.vI = 6;',
  9390. '} catch ($e) {',
  9391. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9392. ' } else throw $e',
  9393. '};',
  9394. '']));
  9395. end;
  9396. procedure TTestModule.TestTryExcept_ReservedWords;
  9397. begin
  9398. StartProgram(false);
  9399. Add([
  9400. 'type',
  9401. ' TObject = class end;',
  9402. ' Exception = class',
  9403. ' Symbol: string;',
  9404. ' end;',
  9405. 'var &try: longint;',
  9406. 'begin',
  9407. ' try',
  9408. ' &try:=4;',
  9409. ' except',
  9410. ' on Error: exception do',
  9411. ' if errOR.symBol='''' then',
  9412. ' raise ERRor;',
  9413. ' end;',
  9414. '']);
  9415. ConvertProgram;
  9416. CheckSource('TestTryExcept_ReservedWords',
  9417. LinesToStr([ // statements
  9418. 'rtl.createClass(this, "TObject", null, function () {',
  9419. ' this.$init = function () {',
  9420. ' };',
  9421. ' this.$final = function () {',
  9422. ' };',
  9423. '});',
  9424. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9425. ' this.$init = function () {',
  9426. ' $mod.TObject.$init.call(this);',
  9427. ' this.Symbol = "";',
  9428. ' };',
  9429. '});',
  9430. 'this.Try = 0;',
  9431. '']),
  9432. LinesToStr([ // $mod.$main
  9433. 'try {',
  9434. ' $mod.Try = 4;',
  9435. '} catch ($e) {',
  9436. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9437. ' var error = $e;',
  9438. ' if (error.Symbol === "") throw error;',
  9439. ' } else throw $e',
  9440. '};',
  9441. '']));
  9442. end;
  9443. procedure TTestModule.TestIfThenRaiseElse;
  9444. begin
  9445. StartProgram(false);
  9446. Add([
  9447. 'type',
  9448. ' TObject = class',
  9449. ' constructor Create;',
  9450. ' end;',
  9451. 'constructor TObject.Create;',
  9452. 'begin',
  9453. 'end;',
  9454. 'var b: boolean;',
  9455. 'begin',
  9456. ' if b then',
  9457. ' raise TObject.Create',
  9458. ' else',
  9459. ' b:=false;',
  9460. '']);
  9461. ConvertProgram;
  9462. CheckSource('TestIfThenRaiseElse',
  9463. LinesToStr([ // statements
  9464. 'rtl.createClass(this, "TObject", null, function () {',
  9465. ' this.$init = function () {',
  9466. ' };',
  9467. ' this.$final = function () {',
  9468. ' };',
  9469. ' this.Create = function () {',
  9470. ' return this;',
  9471. ' };',
  9472. '});',
  9473. 'this.b = false;',
  9474. '']),
  9475. LinesToStr([ // $mod.$main
  9476. 'if ($mod.b) {',
  9477. ' throw $mod.TObject.$create("Create")}',
  9478. ' else $mod.b = false;',
  9479. '']));
  9480. end;
  9481. procedure TTestModule.TestCaseOf;
  9482. begin
  9483. StartProgram(false);
  9484. Add([
  9485. 'const e: longint; external name ''$e'';',
  9486. 'var vI: longint;',
  9487. 'begin',
  9488. ' case vi of',
  9489. ' 1: ;',
  9490. ' 2: vi:=3;',
  9491. ' e: ;',
  9492. ' else',
  9493. ' VI:=4',
  9494. ' end;']);
  9495. ConvertProgram;
  9496. CheckSource('TestCaseOf',
  9497. LinesToStr([ // statements
  9498. 'this.vI = 0;'
  9499. ]),
  9500. LinesToStr([ // $mod.$main
  9501. 'var $tmp = $mod.vI;',
  9502. 'if ($tmp === 1) {}',
  9503. 'else if ($tmp === 2) {',
  9504. ' $mod.vI = 3}',
  9505. ' else if ($tmp === $e) {}',
  9506. 'else {',
  9507. ' $mod.vI = 4;',
  9508. '};'
  9509. ]));
  9510. end;
  9511. procedure TTestModule.TestCaseOf_UseSwitch;
  9512. begin
  9513. StartProgram(false);
  9514. Converter.UseSwitchStatement:=true;
  9515. Add('var Vi: longint;');
  9516. Add('begin');
  9517. Add(' case vi of');
  9518. Add(' 1: ;');
  9519. Add(' 2: VI:=3;');
  9520. Add(' else');
  9521. Add(' vi:=4');
  9522. Add(' end;');
  9523. ConvertProgram;
  9524. CheckSource('TestCaseOf_UseSwitch',
  9525. LinesToStr([ // statements
  9526. 'this.Vi = 0;'
  9527. ]),
  9528. LinesToStr([ // $mod.$main
  9529. 'switch ($mod.Vi) {',
  9530. 'case 1:',
  9531. ' break;',
  9532. 'case 2:',
  9533. ' $mod.Vi = 3;',
  9534. ' break;',
  9535. 'default:',
  9536. ' $mod.Vi = 4;',
  9537. '};'
  9538. ]));
  9539. end;
  9540. procedure TTestModule.TestCaseOfNoElse;
  9541. begin
  9542. StartProgram(false);
  9543. Add('var Vi: longint;');
  9544. Add('begin');
  9545. Add(' case vi of');
  9546. Add(' 1: begin vi:=2; VI:=3; end;');
  9547. Add(' end;');
  9548. ConvertProgram;
  9549. CheckSource('TestCaseOfNoElse',
  9550. LinesToStr([ // statements
  9551. 'this.Vi = 0;'
  9552. ]),
  9553. LinesToStr([ // $mod.$main
  9554. 'var $tmp = $mod.Vi;',
  9555. 'if ($tmp === 1) {',
  9556. ' $mod.Vi = 2;',
  9557. ' $mod.Vi = 3;',
  9558. '};'
  9559. ]));
  9560. end;
  9561. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9562. begin
  9563. StartProgram(false);
  9564. Converter.UseSwitchStatement:=true;
  9565. Add('var vI: longint;');
  9566. Add('begin');
  9567. Add(' case vi of');
  9568. Add(' 1: begin VI:=2; vi:=3; end;');
  9569. Add(' end;');
  9570. ConvertProgram;
  9571. CheckSource('TestCaseOfNoElse_UseSwitch',
  9572. LinesToStr([ // statements
  9573. 'this.vI = 0;'
  9574. ]),
  9575. LinesToStr([ // $mod.$main
  9576. 'switch ($mod.vI) {',
  9577. 'case 1:',
  9578. ' $mod.vI = 2;',
  9579. ' $mod.vI = 3;',
  9580. ' break;',
  9581. '};'
  9582. ]));
  9583. end;
  9584. procedure TTestModule.TestCaseOfRange;
  9585. begin
  9586. StartProgram(false);
  9587. Add('var vI: longint;');
  9588. Add('begin');
  9589. Add(' case vi of');
  9590. Add(' 1..3: vi:=14;');
  9591. Add(' 4,5: vi:=16;');
  9592. Add(' 6..7,9..10: ;');
  9593. Add(' else ;');
  9594. Add(' end;');
  9595. ConvertProgram;
  9596. CheckSource('TestCaseOfRange',
  9597. LinesToStr([ // statements
  9598. 'this.vI = 0;'
  9599. ]),
  9600. LinesToStr([ // $mod.$main
  9601. 'var $tmp = $mod.vI;',
  9602. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9603. ' $mod.vI = 14',
  9604. '} else if (($tmp === 4) || ($tmp === 5)){',
  9605. ' $mod.vI = 16',
  9606. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9607. ]));
  9608. end;
  9609. procedure TTestModule.TestCaseOfString;
  9610. begin
  9611. StartProgram(false);
  9612. Add([
  9613. 'var s,h: string;',
  9614. 'begin',
  9615. ' case s of',
  9616. ' ''foo'': s:=h;',
  9617. ' ''a''..''z'': h:=s;',
  9618. ' ''ў'', ''ё'': ;',
  9619. ' ''Б''..''Я'': ;',
  9620. ' end;',
  9621. '']);
  9622. ConvertProgram;
  9623. CheckSource('TestCaseOfString',
  9624. LinesToStr([ // statements
  9625. 'this.s = "";',
  9626. 'this.h = "";',
  9627. '']),
  9628. LinesToStr([ // $mod.$main
  9629. 'var $tmp = $mod.s;',
  9630. 'if ($tmp === "foo") {',
  9631. ' $mod.s = $mod.h}',
  9632. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9633. ' $mod.h = $mod.s}',
  9634. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9635. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9636. '']));
  9637. end;
  9638. procedure TTestModule.TestCaseOfChar;
  9639. begin
  9640. StartProgram(false);
  9641. Add([
  9642. 'var s,h: char;',
  9643. 'begin',
  9644. ' case s of',
  9645. ' ''a''..''z'': h:=s;',
  9646. ' ''ä'': ;',
  9647. ' ''ў'', ''ё'': ;',
  9648. ' ''Б''..''Я'': ;',
  9649. ' end;',
  9650. '']);
  9651. ConvertProgram;
  9652. CheckSource('TestCaseOfString',
  9653. LinesToStr([ // statements
  9654. 'this.s = "";',
  9655. 'this.h = "";',
  9656. '']),
  9657. LinesToStr([ // $mod.$main
  9658. 'var $tmp = $mod.s;',
  9659. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9660. ' $mod.h = $mod.s}',
  9661. ' else if ($tmp === "ä") {}',
  9662. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9663. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9664. '']));
  9665. end;
  9666. procedure TTestModule.TestCaseOfExternalClassConst;
  9667. begin
  9668. StartProgram(false);
  9669. Add([
  9670. '{$modeswitch externalclass}',
  9671. 'type',
  9672. ' TBird = class external name ''Bird''',
  9673. ' const e: longint;',
  9674. ' end;',
  9675. 'var vI: longint;',
  9676. 'begin',
  9677. ' case vi of',
  9678. ' 1: vi:=3;',
  9679. ' TBird.e: ;',
  9680. ' end;']);
  9681. ConvertProgram;
  9682. CheckSource('TestCaseOfExternalClassConst',
  9683. LinesToStr([ // statements
  9684. 'this.vI = 0;'
  9685. ]),
  9686. LinesToStr([ // $mod.$main
  9687. 'var $tmp = $mod.vI;',
  9688. 'if ($tmp === 1) {',
  9689. ' $mod.vI = 3}',
  9690. ' else if ($tmp === Bird.e) ;'
  9691. ]));
  9692. end;
  9693. procedure TTestModule.TestDebugger;
  9694. begin
  9695. StartProgram(false);
  9696. Add([
  9697. 'procedure DoIt;',
  9698. 'begin',
  9699. ' deBugger;',
  9700. ' DeBugger();',
  9701. 'end;',
  9702. 'begin',
  9703. ' Debugger;']);
  9704. ConvertProgram;
  9705. CheckSource('TestDebugger',
  9706. LinesToStr([ // statements
  9707. 'this.DoIt = function () {',
  9708. ' debugger;',
  9709. ' debugger;',
  9710. '};',
  9711. '']),
  9712. LinesToStr([ // $mod.$main
  9713. 'debugger;',
  9714. '']));
  9715. end;
  9716. procedure TTestModule.TestArray_Dynamic;
  9717. begin
  9718. StartProgram(false);
  9719. Add([
  9720. 'type',
  9721. ' TArrayInt = array of longint;',
  9722. 'var',
  9723. ' Arr: TArrayInt;',
  9724. ' i: longint;',
  9725. ' b: boolean;',
  9726. 'begin',
  9727. ' SetLength(arr,3);',
  9728. ' arr[0]:=4;',
  9729. ' arr[1]:=length(arr)+arr[0];',
  9730. ' arr[i]:=5;',
  9731. ' arr[arr[i]]:=arr[6];',
  9732. ' i:=low(arr);',
  9733. ' i:=high(arr);',
  9734. ' b:=Assigned(arr);',
  9735. ' Arr:=default(TArrayInt);']);
  9736. ConvertProgram;
  9737. CheckSource('TestArray_Dynamic',
  9738. LinesToStr([ // statements
  9739. 'this.Arr = [];',
  9740. 'this.i = 0;',
  9741. 'this.b = false;'
  9742. ]),
  9743. LinesToStr([ // $mod.$main
  9744. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9745. '$mod.Arr[0] = 4;',
  9746. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9747. '$mod.Arr[$mod.i] = 5;',
  9748. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9749. '$mod.i = 0;',
  9750. '$mod.i = rtl.length($mod.Arr) - 1;',
  9751. '$mod.b = rtl.length($mod.Arr) > 0;',
  9752. '$mod.Arr = [];',
  9753. '']));
  9754. end;
  9755. procedure TTestModule.TestArray_Dynamic_Nil;
  9756. begin
  9757. StartProgram(false);
  9758. Add('type');
  9759. Add(' TArrayInt = array of longint;');
  9760. Add('var');
  9761. Add(' Arr: TArrayInt;');
  9762. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9763. Add('begin');
  9764. Add(' arr:=nil;');
  9765. Add(' if arr=nil then;');
  9766. Add(' if nil=arr then;');
  9767. Add(' if arr<>nil then;');
  9768. Add(' if nil<>arr then;');
  9769. Add(' DoIt(nil,nil);');
  9770. ConvertProgram;
  9771. CheckSource('TestArray_Dynamic',
  9772. LinesToStr([ // statements
  9773. 'this.Arr = [];',
  9774. 'this.DoIt = function(i,j){',
  9775. '};'
  9776. ]),
  9777. LinesToStr([ // $mod.$main
  9778. '$mod.Arr = [];',
  9779. 'if (rtl.length($mod.Arr) === 0) ;',
  9780. 'if (rtl.length($mod.Arr) === 0) ;',
  9781. 'if (rtl.length($mod.Arr) > 0) ;',
  9782. 'if (rtl.length($mod.Arr) > 0) ;',
  9783. '$mod.DoIt([],[]);',
  9784. '']));
  9785. end;
  9786. procedure TTestModule.TestArray_DynMultiDimensional;
  9787. begin
  9788. StartProgram(false);
  9789. Add([
  9790. 'type',
  9791. ' TArrayInt = array of longint;',
  9792. ' TArrayArrayInt = array of TArrayInt;',
  9793. 'var',
  9794. ' Arr: TArrayInt;',
  9795. ' Arr2: TArrayArrayInt;',
  9796. ' i: longint;',
  9797. 'begin',
  9798. ' arr2:=nil;',
  9799. ' if arr2=nil then;',
  9800. ' if nil=arr2 then;',
  9801. ' i:=low(arr2);',
  9802. ' i:=low(arr2[1]);',
  9803. ' i:=high(arr2);',
  9804. ' i:=high(arr2[2]);',
  9805. ' arr2[3]:=arr;',
  9806. ' arr2[4][5]:=i;',
  9807. ' i:=arr2[6][7];',
  9808. ' arr2[8,9]:=i;',
  9809. ' i:=arr2[10,11];',
  9810. ' SetLength(arr2,14);',
  9811. ' SetLength(arr2[15],16);']);
  9812. ConvertProgram;
  9813. CheckSource('TestArray_Dynamic',
  9814. LinesToStr([ // statements
  9815. 'this.Arr = [];',
  9816. 'this.Arr2 = [];',
  9817. 'this.i = 0;'
  9818. ]),
  9819. LinesToStr([ // $mod.$main
  9820. '$mod.Arr2 = [];',
  9821. 'if (rtl.length($mod.Arr2) === 0) ;',
  9822. 'if (rtl.length($mod.Arr2) === 0) ;',
  9823. '$mod.i = 0;',
  9824. '$mod.i = 0;',
  9825. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9826. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9827. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9828. '$mod.Arr2[4][5] = $mod.i;',
  9829. '$mod.i = $mod.Arr2[6][7];',
  9830. '$mod.Arr2[8][9] = $mod.i;',
  9831. '$mod.i = $mod.Arr2[10][11];',
  9832. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9833. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9834. '']));
  9835. end;
  9836. procedure TTestModule.TestArray_DynamicAssign;
  9837. begin
  9838. StartProgram(false);
  9839. Add([
  9840. 'type',
  9841. ' TArrayInt = array of longint;',
  9842. ' TArrayArrayInt = array of TArrayInt;',
  9843. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9844. 'begin',
  9845. 'end;',
  9846. 'procedure Fly(var a: TArrayInt);',
  9847. 'begin',
  9848. 'end;',
  9849. 'var',
  9850. ' Arr: TArrayInt;',
  9851. ' Arr2: TArrayArrayInt;',
  9852. 'begin',
  9853. ' arr:=nil;',
  9854. ' arr2:=nil;',
  9855. ' arr2[1]:=nil;',
  9856. ' arr2[2]:=arr;',
  9857. ' Run(arr,arr,arr);',
  9858. ' Fly(arr);',
  9859. ' Run(arr2[4],arr2[5],arr2[6]);',
  9860. ' Fly(arr2[7]);',
  9861. '']);
  9862. ConvertProgram;
  9863. CheckSource('TestArray_DynamicAssign',
  9864. LinesToStr([ // statements
  9865. 'this.Run = function (a, b, c) {',
  9866. '};',
  9867. 'this.Fly = function (a) {',
  9868. '};',
  9869. 'this.Arr = [];',
  9870. 'this.Arr2 = [];',
  9871. '']),
  9872. LinesToStr([ // $mod.$main
  9873. '$mod.Arr = [];',
  9874. '$mod.Arr2 = [];',
  9875. '$mod.Arr2[1] = [];',
  9876. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9877. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9878. '$mod.Fly({',
  9879. ' p: $mod,',
  9880. ' get: function () {',
  9881. ' return this.p.Arr;',
  9882. ' },',
  9883. ' set: function (v) {',
  9884. ' this.p.Arr = v;',
  9885. ' }',
  9886. '});',
  9887. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9888. '$mod.Fly({',
  9889. ' a: 7,',
  9890. ' p: $mod.Arr2,',
  9891. ' get: function () {',
  9892. ' return this.p[this.a];',
  9893. ' },',
  9894. ' set: function (v) {',
  9895. ' this.p[this.a] = v;',
  9896. ' }',
  9897. '});',
  9898. '']));
  9899. end;
  9900. procedure TTestModule.TestArray_StaticInt;
  9901. begin
  9902. StartProgram(false);
  9903. Add('type');
  9904. Add(' TArrayInt = array[2..4] of longint;');
  9905. Add('var');
  9906. Add(' Arr: TArrayInt;');
  9907. Add(' Arr2: TArrayInt = (5,6,7);');
  9908. Add(' i: longint;');
  9909. Add(' b: boolean;');
  9910. Add('begin');
  9911. Add(' arr[2]:=4;');
  9912. Add(' arr[3]:=arr[2]+arr[3];');
  9913. Add(' arr[i]:=5;');
  9914. Add(' arr[arr[i]]:=arr[high(arr)];');
  9915. Add(' i:=low(arr);');
  9916. Add(' i:=high(arr);');
  9917. Add(' b:=arr[2]=arr[3];');
  9918. Add(' arr:=default(TArrayInt);');
  9919. ConvertProgram;
  9920. CheckSource('TestArray_StaticInt',
  9921. LinesToStr([ // statements
  9922. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9923. 'this.Arr2 = [5, 6, 7];',
  9924. 'this.i = 0;',
  9925. 'this.b = false;'
  9926. ]),
  9927. LinesToStr([ // $mod.$main
  9928. '$mod.Arr[0] = 4;',
  9929. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9930. '$mod.Arr[$mod.i-2] = 5;',
  9931. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9932. '$mod.i = 2;',
  9933. '$mod.i = 4;',
  9934. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9935. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9936. '']));
  9937. end;
  9938. procedure TTestModule.TestArray_StaticBool;
  9939. begin
  9940. StartProgram(false);
  9941. Add('type');
  9942. Add(' TBools = array[boolean] of boolean;');
  9943. Add(' TBool2 = array[true..true] of boolean;');
  9944. Add('var');
  9945. Add(' Arr: TBools;');
  9946. Add(' Arr2: TBool2;');
  9947. Add(' Arr3: TBools = (true,false);');
  9948. Add(' b: boolean;');
  9949. Add('begin');
  9950. Add(' b:=low(arr);');
  9951. Add(' b:=high(arr);');
  9952. Add(' arr[true]:=false;');
  9953. Add(' arr[false]:=arr[b] or arr[true];');
  9954. Add(' arr[b]:=true;');
  9955. Add(' arr[arr[b]]:=arr[high(arr)];');
  9956. Add(' b:=arr[false]=arr[true];');
  9957. Add(' b:=low(arr2);');
  9958. Add(' b:=high(arr2);');
  9959. Add(' arr2[true]:=true;');
  9960. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9961. Add(' arr2[b]:=false;');
  9962. ConvertProgram;
  9963. CheckSource('TestArray_StaticBool',
  9964. LinesToStr([ // statements
  9965. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9966. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9967. 'this.Arr3 = [true, false];',
  9968. 'this.b = false;'
  9969. ]),
  9970. LinesToStr([ // $mod.$main
  9971. '$mod.b = false;',
  9972. '$mod.b = true;',
  9973. '$mod.Arr[1] = false;',
  9974. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9975. '$mod.Arr[+$mod.b] = true;',
  9976. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9977. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9978. '$mod.b = true;',
  9979. '$mod.b = true;',
  9980. '$mod.Arr2[0] = true;',
  9981. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9982. '$mod.Arr2[1-$mod.b] = false;',
  9983. '']));
  9984. end;
  9985. procedure TTestModule.TestArray_StaticChar;
  9986. begin
  9987. StartProgram(false);
  9988. Add([
  9989. 'type',
  9990. ' TChars = array[char] of char;',
  9991. ' TChars2 = array[''a''..''z''] of char;',
  9992. 'var',
  9993. ' Arr: TChars;',
  9994. ' Arr2: TChars2;',
  9995. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9996. ' Arr4: array[11..13] of char = ''pas'';',
  9997. ' Arr5: array[21..22] of char = ''äö'';',
  9998. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9999. ' c: char;',
  10000. ' b: boolean;',
  10001. 'begin',
  10002. ' c:=low(arr);',
  10003. ' c:=high(arr);',
  10004. ' arr[''B'']:=''a'';',
  10005. ' arr[''D'']:=arr[c];',
  10006. ' arr[c]:=arr[''d''];',
  10007. ' arr[arr[c]]:=arr[high(arr)];',
  10008. ' b:=arr[low(arr)]=arr[''e''];',
  10009. ' c:=low(arr2);',
  10010. ' c:=high(arr2);',
  10011. ' arr2[''b'']:=''f'';',
  10012. ' arr2[''a'']:=arr2[c];',
  10013. ' arr2[c]:=arr2[''g''];']);
  10014. ConvertProgram;
  10015. CheckSource('TestArray_StaticChar',
  10016. LinesToStr([ // statements
  10017. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  10018. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  10019. 'this.Arr3 = ["p", "a", "s"];',
  10020. 'this.Arr4 = ["p", "a", "s"];',
  10021. 'this.Arr5 = ["ä", "ö"];',
  10022. 'this.Arr6 = ["ä", "ö"];',
  10023. 'this.c = "";',
  10024. 'this.b = false;',
  10025. '']),
  10026. LinesToStr([ // $mod.$main
  10027. '$mod.c = "\x00";',
  10028. '$mod.c = "\uFFFF";',
  10029. '$mod.Arr[66] = "a";',
  10030. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10031. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10032. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10033. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10034. '$mod.c = "a";',
  10035. '$mod.c = "z";',
  10036. '$mod.Arr2[1] = "f";',
  10037. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10038. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10039. '']));
  10040. end;
  10041. procedure TTestModule.TestArray_StaticMultiDim;
  10042. begin
  10043. StartProgram(false);
  10044. Add([
  10045. 'type',
  10046. ' TArrayInt = array[1..3] of longint;',
  10047. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10048. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10049. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10050. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10051. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10052. 'var',
  10053. ' Arr: TArrayInt;',
  10054. ' Arr2: TArrayArrayInt;',
  10055. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10056. ' Arr4: TArrayArrayInt;',
  10057. ' ArrDim2: TArrayDim2Int;',
  10058. ' ArrDim3: TArrayDim3Int;',
  10059. ' ArrDim4: TArrayDim4Int;',
  10060. ' i: longint;',
  10061. 'begin',
  10062. ' i:=low(arr);',
  10063. ' i:=low(arr2);',
  10064. ' i:=low(arr2[5]);',
  10065. ' i:=high(arr);',
  10066. ' i:=high(arr2);',
  10067. ' i:=high(arr2[6]);',
  10068. ' arr2[5]:=arr;',
  10069. ' arr2[6][2]:=i;',
  10070. ' i:=arr2[6][3];',
  10071. ' arr2[6,3]:=i;',
  10072. ' i:=arr2[5,2];',
  10073. ' arr2:=arr2;',// clone multi dim static array
  10074. ' arr3:=arr3;',// clone anonymous multi dim static array
  10075. ' arr4:=arr4;',
  10076. ' Arr:=Arr;',
  10077. ' ArrDim2:=ArrDim2;',
  10078. ' ArrDim3:=ArrDim3;',
  10079. ' ArrDim4:=ArrDim4;',
  10080. '']);
  10081. ConvertProgram;
  10082. CheckSource('TestArray_StaticMultiDim',
  10083. LinesToStr([ // statements
  10084. 'this.TArrayArrayInt$clone = function (a) {',
  10085. ' var b = [];',
  10086. ' b.length = 2;',
  10087. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10088. ' return b;',
  10089. '};',
  10090. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10091. ' var b = [];',
  10092. ' b.length = 2;',
  10093. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10094. ' return b;',
  10095. '};',
  10096. 'this.TArrayDim2Int$clone = function (a) {',
  10097. ' var b = [];',
  10098. ' b.length = 2;',
  10099. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10100. ' return b;',
  10101. '};',
  10102. 'this.TArrayDim3Int$clone = function (a) {',
  10103. ' var b = [];',
  10104. ' b.length = 2;',
  10105. ' for (var c = 0; c < 2; c++) {',
  10106. ' var d = b[c] = [];',
  10107. ' d.length = 3;',
  10108. ' var e = a[c];',
  10109. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10110. ' };',
  10111. ' return b;',
  10112. '};',
  10113. 'this.TArrayDim4Int$clone = function (a) {',
  10114. ' var b = [];',
  10115. ' b.length = 2;',
  10116. ' for (var c = 0; c < 2; c++) {',
  10117. ' var d = b[c] = [];',
  10118. ' d.length = 3;',
  10119. ' var e = a[c];',
  10120. ' for (var f = 0; f < 3; f++) {',
  10121. ' var g = d[f] = [];',
  10122. ' g.length = 4;',
  10123. ' var h = e[f];',
  10124. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10125. ' };',
  10126. ' };',
  10127. ' return b;',
  10128. '};',
  10129. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10130. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10131. 'this.Arr3$a$clone = function (a) {',
  10132. ' var b = [];',
  10133. ' b.length = 2;',
  10134. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10135. ' return b;',
  10136. '};',
  10137. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10138. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10139. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10140. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10141. 'this.ArrDim4 = rtl.arraySetLength(',
  10142. ' null,',
  10143. ' 0,',
  10144. ' 2,',
  10145. ' 3,',
  10146. ' 4,',
  10147. ' 5',
  10148. ');',
  10149. 'this.i = 0;'
  10150. ]),
  10151. LinesToStr([ // $mod.$main
  10152. '$mod.i = 1;',
  10153. '$mod.i = 5;',
  10154. '$mod.i = 1;',
  10155. '$mod.i = 3;',
  10156. '$mod.i = 6;',
  10157. '$mod.i = 3;',
  10158. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10159. '$mod.Arr2[1][1] = $mod.i;',
  10160. '$mod.i = $mod.Arr2[1][2];',
  10161. '$mod.Arr2[1][2] = $mod.i;',
  10162. '$mod.i = $mod.Arr2[0][1];',
  10163. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10164. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10165. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10166. '$mod.Arr = $mod.Arr.slice(0);',
  10167. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10168. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10169. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10170. '']));
  10171. end;
  10172. procedure TTestModule.TestArray_StaticInFunction;
  10173. begin
  10174. StartProgram(false);
  10175. Add([
  10176. 'const TArrayInt = 3;',
  10177. 'const TArrayArrayInt = 4;',
  10178. 'procedure DoIt;',
  10179. 'type',
  10180. ' TArrayInt = array[1..3] of longint;',
  10181. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10182. 'var',
  10183. ' Arr: TArrayInt;',
  10184. ' Arr2: TArrayArrayInt;',
  10185. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10186. ' i: longint;',
  10187. 'begin',
  10188. ' arr2[5]:=arr;',
  10189. ' arr2:=arr2;',// clone multi dim static array
  10190. ' arr3:=arr3;',// clone multi dim anonymous static array
  10191. 'end;',
  10192. 'begin',
  10193. '']);
  10194. ConvertProgram;
  10195. CheckSource('TestArray_StaticInFunction',
  10196. LinesToStr([ // statements
  10197. 'this.TArrayInt = 3;',
  10198. 'this.TArrayArrayInt = 4;',
  10199. 'var TArrayArrayInt$1$clone = function (a) {',
  10200. ' var b = [];',
  10201. ' b.length = 2;',
  10202. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10203. ' return b;',
  10204. '};',
  10205. 'var Arr3$a$clone = function (a) {',
  10206. ' var b = [];',
  10207. ' b.length = 2;',
  10208. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10209. ' return b;',
  10210. '};',
  10211. 'this.DoIt = function () {',
  10212. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10213. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10214. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10215. ' var i = 0;',
  10216. ' Arr2[0] = Arr.slice(0);',
  10217. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10218. ' Arr3 = Arr3$a$clone(Arr3);',
  10219. '};',
  10220. '']),
  10221. LinesToStr([ // $mod.$main
  10222. '']));
  10223. end;
  10224. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10225. begin
  10226. StartProgram(false);
  10227. Add([
  10228. 'type',
  10229. ' TArrayInt = array[1..3,1..2] of longint;',
  10230. 'var',
  10231. ' a,b: TArrayInt;',
  10232. 'begin',
  10233. ' if a=b then ;',
  10234. '']);
  10235. SetExpectedPasResolverError('compare static array is not supported',
  10236. nXIsNotSupported);
  10237. ConvertProgram;
  10238. end;
  10239. procedure TTestModule.TestArrayOfRecord;
  10240. begin
  10241. StartProgram(false);
  10242. Add([
  10243. 'type',
  10244. ' TRec = record',
  10245. ' Int: longint;',
  10246. ' end;',
  10247. ' TArrayRec = array of TRec;',
  10248. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10249. 'begin',
  10250. 'end;',
  10251. 'var',
  10252. ' Arr: TArrayRec;',
  10253. ' r: TRec;',
  10254. ' i: longint;',
  10255. 'begin',
  10256. ' SetLength(arr,3);',
  10257. ' arr[0].int:=4;',
  10258. ' arr[1].int:=length(arr)+arr[2].int;',
  10259. ' arr[arr[i].int].int:=arr[5].int;',
  10260. ' arr[7]:=r;',
  10261. ' r:=arr[8];',
  10262. ' i:=low(arr);',
  10263. ' i:=high(arr);',
  10264. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10265. ConvertProgram;
  10266. CheckSource('TestArrayOfRecord',
  10267. LinesToStr([ // statements
  10268. 'rtl.recNewT(this, "TRec", function () {',
  10269. ' this.Int = 0;',
  10270. ' this.$eq = function (b) {',
  10271. ' return this.Int === b.Int;',
  10272. ' };',
  10273. ' this.$assign = function (s) {',
  10274. ' this.Int = s.Int;',
  10275. ' return this;',
  10276. ' };',
  10277. '});',
  10278. 'this.DoIt = function (vd, vc, vv) {',
  10279. '};',
  10280. 'this.Arr = [];',
  10281. 'this.r = this.TRec.$new();',
  10282. 'this.i = 0;'
  10283. ]),
  10284. LinesToStr([ // $mod.$main
  10285. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10286. '$mod.Arr[0].Int = 4;',
  10287. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10288. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10289. '$mod.Arr[7].$assign($mod.r);',
  10290. '$mod.r.$assign($mod.Arr[8]);',
  10291. '$mod.i = 0;',
  10292. '$mod.i = rtl.length($mod.Arr)-1;',
  10293. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10294. '']));
  10295. end;
  10296. procedure TTestModule.TestArray_StaticRecord;
  10297. begin
  10298. StartProgram(false);
  10299. Add([
  10300. 'type',
  10301. ' TRec = record',
  10302. ' Int: longint;',
  10303. ' end;',
  10304. ' TArrayRec = array[1..2] of TRec;',
  10305. 'var',
  10306. ' Arr: TArrayRec;',
  10307. 'begin',
  10308. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10309. '']);
  10310. ConvertProgram;
  10311. CheckSource('TestArray_StaticRecord',
  10312. LinesToStr([ // statements
  10313. 'rtl.recNewT(this, "TRec", function () {',
  10314. ' this.Int = 0;',
  10315. ' this.$eq = function (b) {',
  10316. ' return this.Int === b.Int;',
  10317. ' };',
  10318. ' this.$assign = function (s) {',
  10319. ' this.Int = s.Int;',
  10320. ' return this;',
  10321. ' };',
  10322. '});',
  10323. 'this.TArrayRec$clone = function (a) {',
  10324. ' var b = [];',
  10325. ' b.length = 2;',
  10326. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10327. ' return b;',
  10328. '};',
  10329. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10330. '']),
  10331. LinesToStr([ // $mod.$main
  10332. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10333. end;
  10334. procedure TTestModule.TestArrayOfSet;
  10335. begin
  10336. StartProgram(false);
  10337. Add([
  10338. 'type',
  10339. ' TFlag = (big,small);',
  10340. ' TSetOfFlag = set of tflag;',
  10341. ' TArrayFlag = array of TSetOfFlag;',
  10342. 'procedure DoIt(const a: Tarrayflag);',
  10343. 'begin',
  10344. 'end;',
  10345. 'var',
  10346. ' f: TFlag;',
  10347. ' s: TSetOfFlag;',
  10348. ' Arr: TArrayFlag;',
  10349. ' i: longint;',
  10350. 'begin',
  10351. ' SetLength(arr,3);',
  10352. ' arr[0]:=s;',
  10353. ' arr[1]:=[big];',
  10354. ' arr[2]:=[big]+s;',
  10355. ' arr[3]:=s+[big];',
  10356. ' arr[4]:=arr[5];',
  10357. ' s:=arr[6];',
  10358. ' i:=low(arr);',
  10359. ' i:=high(arr);',
  10360. ' DoIt(arr);',
  10361. ' DoIt([s]);',
  10362. ' DoIt([[],s]);',
  10363. ' DoIt([s,[]]);',
  10364. '']);
  10365. ConvertProgram;
  10366. CheckSource('TestArrayOfSet',
  10367. LinesToStr([ // statements
  10368. 'this.TFlag = {',
  10369. ' "0": "big",',
  10370. ' big: 0,',
  10371. ' "1": "small",',
  10372. ' small: 1',
  10373. '};',
  10374. 'this.DoIt = function (a) {',
  10375. '};',
  10376. 'this.f = 0;',
  10377. 'this.s = {};',
  10378. 'this.Arr = [];',
  10379. 'this.i = 0;',
  10380. '']),
  10381. LinesToStr([ // $mod.$main
  10382. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10383. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10384. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10385. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10386. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10387. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10388. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10389. '$mod.i = 0;',
  10390. '$mod.i = rtl.length($mod.Arr) - 1;',
  10391. '$mod.DoIt($mod.Arr);',
  10392. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10393. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10394. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10395. '']));
  10396. end;
  10397. procedure TTestModule.TestArray_DynAsParam;
  10398. begin
  10399. StartProgram(false);
  10400. Add([
  10401. 'type integer = longint;',
  10402. 'type TArrInt = array of integer;',
  10403. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10404. 'var vJ: TArrInt;',
  10405. 'begin',
  10406. ' vg:=vg;',
  10407. ' vj:=vh;',
  10408. ' vi:=vi;',
  10409. ' doit(vg,vg,vg);',
  10410. ' doit(vh,vh,vj);',
  10411. ' doit(vi,vi,vi);',
  10412. ' doit(vj,vj,vj);',
  10413. 'end;',
  10414. 'var i: TArrInt;',
  10415. 'begin',
  10416. ' doit(i,i,i);']);
  10417. ConvertProgram;
  10418. CheckSource('TestArray_DynAsParams',
  10419. LinesToStr([ // statements
  10420. 'this.DoIt = function (vG,vH,vI) {',
  10421. ' var vJ = [];',
  10422. ' vG = rtl.arrayRef(vG);',
  10423. ' vJ = rtl.arrayRef(vH);',
  10424. ' vI.set(rtl.arrayRef(vI.get()));',
  10425. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10426. ' get: function () {',
  10427. ' return vG;',
  10428. ' },',
  10429. ' set: function (v) {',
  10430. ' vG = v;',
  10431. ' }',
  10432. ' });',
  10433. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10434. ' get: function () {',
  10435. ' return vJ;',
  10436. ' },',
  10437. ' set: function (v) {',
  10438. ' vJ = v;',
  10439. ' }',
  10440. ' });',
  10441. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10442. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10443. ' get: function () {',
  10444. ' return vJ;',
  10445. ' },',
  10446. ' set: function (v) {',
  10447. ' vJ = v;',
  10448. ' }',
  10449. ' });',
  10450. '};',
  10451. 'this.i = [];'
  10452. ]),
  10453. LinesToStr([
  10454. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10455. ' p: $mod,',
  10456. ' get: function () {',
  10457. ' return this.p.i;',
  10458. ' },',
  10459. ' set: function (v) {',
  10460. ' this.p.i = v;',
  10461. ' }',
  10462. '});'
  10463. ]));
  10464. end;
  10465. procedure TTestModule.TestArray_StaticAsParam;
  10466. begin
  10467. StartProgram(false);
  10468. Add([
  10469. 'type integer = longint;',
  10470. 'type TArrInt = array[1..2] of integer;',
  10471. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10472. 'var vJ: TArrInt;',
  10473. 'begin',
  10474. ' vg:=vg;',
  10475. ' vj:=vh;',
  10476. ' vi:=vi;',
  10477. ' doit(vg,vg,vg);',
  10478. ' doit(vh,vh,vj);',
  10479. ' doit(vi,vi,vi);',
  10480. ' doit(vj,vj,vj);',
  10481. 'end;',
  10482. 'var i: TArrInt;',
  10483. 'begin',
  10484. ' doit(i,i,i);']);
  10485. ConvertProgram;
  10486. CheckSource('TestArray_StaticAsParams',
  10487. LinesToStr([ // statements
  10488. 'this.DoIt = function (vG,vH,vI) {',
  10489. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10490. ' vG = vG.slice(0);',
  10491. ' vJ = vH.slice(0);',
  10492. ' vI.set(vI.get().slice(0));',
  10493. ' $mod.DoIt(vG.slice(0), vG, {',
  10494. ' get: function () {',
  10495. ' return vG;',
  10496. ' },',
  10497. ' set: function (v) {',
  10498. ' vG = v;',
  10499. ' }',
  10500. ' });',
  10501. ' $mod.DoIt(vH.slice(0), vH, {',
  10502. ' get: function () {',
  10503. ' return vJ;',
  10504. ' },',
  10505. ' set: function (v) {',
  10506. ' vJ = v;',
  10507. ' }',
  10508. ' });',
  10509. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10510. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10511. ' get: function () {',
  10512. ' return vJ;',
  10513. ' },',
  10514. ' set: function (v) {',
  10515. ' vJ = v;',
  10516. ' }',
  10517. ' });',
  10518. '};',
  10519. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10520. ]),
  10521. LinesToStr([
  10522. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10523. ' p: $mod,',
  10524. ' get: function () {',
  10525. ' return this.p.i;',
  10526. ' },',
  10527. ' set: function (v) {',
  10528. ' this.p.i = v;',
  10529. ' }',
  10530. '});'
  10531. ]));
  10532. end;
  10533. procedure TTestModule.TestArrayElement_AsParams;
  10534. begin
  10535. StartProgram(false);
  10536. Add('type integer = longint;');
  10537. Add('type TArrayInt = array of integer;');
  10538. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10539. Add('var vJ: tarrayint;');
  10540. Add('begin');
  10541. Add(' vi:=vi;');
  10542. Add(' doit(vi,vi,vi);');
  10543. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10544. Add('end;');
  10545. Add('var a: TArrayInt;');
  10546. Add('begin');
  10547. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10548. ConvertProgram;
  10549. CheckSource('TestArrayElement_AsParams',
  10550. LinesToStr([ // statements
  10551. 'this.DoIt = function (vG,vH,vI) {',
  10552. ' var vJ = [];',
  10553. ' vI.set(vI.get());',
  10554. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10555. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10556. ' a:1+3,',
  10557. ' p:vJ,',
  10558. ' get: function () {',
  10559. ' return this.p[this.a];',
  10560. ' },',
  10561. ' set: function (v) {',
  10562. ' this.p[this.a] = v;',
  10563. ' }',
  10564. ' });',
  10565. '};',
  10566. 'this.a = [];'
  10567. ]),
  10568. LinesToStr([
  10569. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10570. ' a: 1+6,',
  10571. ' p: $mod.a,',
  10572. ' get: function () {',
  10573. ' return this.p[this.a];',
  10574. ' },',
  10575. ' set: function (v) {',
  10576. ' this.p[this.a] = v;',
  10577. ' }',
  10578. '});'
  10579. ]));
  10580. end;
  10581. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10582. begin
  10583. StartProgram(false);
  10584. Add('type Integer = longint;');
  10585. Add('type TArrayInt = array of integer;');
  10586. Add('function GetArr(vB: integer = 0): tarrayint;');
  10587. Add('begin');
  10588. Add('end;');
  10589. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10590. Add('begin');
  10591. Add('end;');
  10592. Add('begin');
  10593. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10594. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10595. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10596. ConvertProgram;
  10597. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10598. LinesToStr([ // statements
  10599. 'this.GetArr = function (vB) {',
  10600. ' var Result = [];',
  10601. ' return Result;',
  10602. '};',
  10603. 'this.DoIt = function (vG,vH,vI) {',
  10604. '};'
  10605. ]),
  10606. LinesToStr([
  10607. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10608. ' a: 1+3,',
  10609. ' p: $mod.GetArr(0),',
  10610. ' get: function () {',
  10611. ' return this.p[this.a];',
  10612. ' },',
  10613. ' set: function (v) {',
  10614. ' this.p[this.a] = v;',
  10615. ' }',
  10616. '});',
  10617. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10618. ' a: 2+3,',
  10619. ' p: $mod.GetArr(0),',
  10620. ' get: function () {',
  10621. ' return this.p[this.a];',
  10622. ' },',
  10623. ' set: function (v) {',
  10624. ' this.p[this.a] = v;',
  10625. ' }',
  10626. '});',
  10627. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10628. ' a: 3+3,',
  10629. ' p: $mod.GetArr(9),',
  10630. ' get: function () {',
  10631. ' return this.p[this.a];',
  10632. ' },',
  10633. ' set: function (v) {',
  10634. ' this.p[this.a] = v;',
  10635. ' }',
  10636. '});',
  10637. '']));
  10638. end;
  10639. procedure TTestModule.TestArrayEnumTypeRange;
  10640. begin
  10641. StartProgram(false);
  10642. Add([
  10643. 'type',
  10644. ' TEnum = (red,blue);',
  10645. ' TEnumArray = array[TEnum] of longint;',
  10646. 'var',
  10647. ' e: TEnum;',
  10648. ' i: longint;',
  10649. ' a: TEnumArray;',
  10650. ' numbers: TEnumArray = (1,2);',
  10651. ' names: array[TEnum] of string = (''red'',''blue'');',
  10652. 'begin',
  10653. ' e:=low(a);',
  10654. ' e:=high(a);',
  10655. ' i:=a[red];',
  10656. ' a[e]:=a[e];']);
  10657. ConvertProgram;
  10658. CheckSource('TestArrayEnumTypeRange',
  10659. LinesToStr([ // statements
  10660. ' this.TEnum = {',
  10661. ' "0": "red",',
  10662. ' red: 0,',
  10663. ' "1": "blue",',
  10664. ' blue: 1',
  10665. '};',
  10666. 'this.e = 0;',
  10667. 'this.i = 0;',
  10668. 'this.a = rtl.arraySetLength(null,0,2);',
  10669. 'this.numbers = [1, 2];',
  10670. 'this.names = ["red", "blue"];',
  10671. '']),
  10672. LinesToStr([ // $mod.$main
  10673. '$mod.e = $mod.TEnum.red;',
  10674. '$mod.e = $mod.TEnum.blue;',
  10675. '$mod.i = $mod.a[$mod.TEnum.red];',
  10676. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10677. '']));
  10678. end;
  10679. procedure TTestModule.TestArray_SetLengthOutArg;
  10680. begin
  10681. StartProgram(false);
  10682. Add([
  10683. 'type TArrInt = array of longint;',
  10684. 'procedure DoIt(out a: TArrInt);',
  10685. 'begin',
  10686. ' SetLength(a,2);',
  10687. 'end;',
  10688. 'begin',
  10689. '']);
  10690. ConvertProgram;
  10691. CheckSource('TestArray_SetLengthOutArg',
  10692. LinesToStr([ // statements
  10693. 'this.DoIt = function (a) {',
  10694. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10695. '};',
  10696. '']),
  10697. LinesToStr([
  10698. '']));
  10699. end;
  10700. procedure TTestModule.TestArray_SetLengthProperty;
  10701. begin
  10702. StartProgram(false);
  10703. Add('type');
  10704. Add(' TArrInt = array of longint;');
  10705. Add(' TObject = class');
  10706. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10707. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10708. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10709. Add(' end;');
  10710. Add('var Obj: TObject;');
  10711. Add('begin');
  10712. Add(' SetLength(Obj.Colors,2);');
  10713. ConvertProgram;
  10714. CheckSource('TestArray_SetLengthProperty',
  10715. LinesToStr([ // statements
  10716. 'rtl.createClass(this, "TObject", null, function () {',
  10717. ' this.$init = function () {',
  10718. ' };',
  10719. ' this.$final = function () {',
  10720. ' };',
  10721. '});',
  10722. 'this.Obj = null;',
  10723. '']),
  10724. LinesToStr([
  10725. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10726. '']));
  10727. end;
  10728. procedure TTestModule.TestArray_SetLengthMultiDim;
  10729. begin
  10730. StartProgram(false);
  10731. Add([
  10732. 'type',
  10733. ' TArrArrInt = array of array of longint;',
  10734. ' TArrStaInt = array of array[1..2] of longint;',
  10735. 'var',
  10736. ' a: TArrArrInt;',
  10737. ' b: TArrStaInt;',
  10738. 'begin',
  10739. ' SetLength(a,2);',
  10740. ' SetLength(a,3,4);',
  10741. ' SetLength(b,5);',
  10742. '']);
  10743. ConvertProgram;
  10744. CheckSource('TestArray_SetLengthMultiDim',
  10745. LinesToStr([ // statements
  10746. 'this.a = [];',
  10747. 'this.b = [];',
  10748. '']),
  10749. LinesToStr([
  10750. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10751. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10752. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10753. '']));
  10754. end;
  10755. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10756. begin
  10757. StartProgram(false);
  10758. Add([
  10759. 'type',
  10760. ' TStaArr1 = array[1..3] of boolean;',
  10761. //' TStaArr2 = array[5..6] of TStaArr1;',
  10762. ' TDynArr1StaArr1 = array of TStaArr1;',
  10763. //' TDynArr1StaArr2 = array of TStaArr2;',
  10764. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10765. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10766. 'var',
  10767. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10768. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10769. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10770. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10771. 'begin',
  10772. ' SetLength(DynArr1StaArr1,11);',
  10773. ' SetLength(DynArr2StaArr1,12);',
  10774. ' SetLength(DynArr2StaArr1[13],14);',
  10775. ' SetLength(DynArr2StaArr1,15,16);',
  10776. //' SetLength(DynArr1StaArr2,21);',
  10777. //' SetLength(DynArr2StaArr2,22);',
  10778. //' SetLength(DynArr2StaArr2[23],24);',
  10779. //' SetLength(DynArr2StaArr2,25,26);',
  10780. '']);
  10781. ConvertProgram;
  10782. CheckSource('TestArray_DynOfStatic',
  10783. LinesToStr([ // statements
  10784. 'this.DynArr1StaArr1 = [];',
  10785. 'this.DynArr2StaArr1 = [];',
  10786. '']),
  10787. LinesToStr([ // $mod.$main
  10788. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10789. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10790. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10791. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10792. ' $mod.DynArr2StaArr1,',
  10793. ' false,',
  10794. ' 15,',
  10795. ' 16,',
  10796. ' "s",',
  10797. ' 3',
  10798. ');',
  10799. '']));
  10800. end;
  10801. procedure TTestModule.TestArray_OpenArrayOfString;
  10802. begin
  10803. StartProgram(false);
  10804. Add('procedure DoIt(const a: array of String);');
  10805. Add('var');
  10806. Add(' i: longint;');
  10807. Add(' s: string;');
  10808. Add('begin');
  10809. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10810. Add('end;');
  10811. Add('var s: string;');
  10812. Add('begin');
  10813. Add(' DoIt([]);');
  10814. Add(' DoIt([s,''foo'','''',s+s]);');
  10815. ConvertProgram;
  10816. CheckSource('TestArray_OpenArrayOfString',
  10817. LinesToStr([ // statements
  10818. 'this.DoIt = function (a) {',
  10819. ' var i = 0;',
  10820. ' var s = "";',
  10821. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10822. ' i = $l;',
  10823. ' s = a[rtl.length(a) - i - 1];',
  10824. ' };',
  10825. '};',
  10826. 'this.s = "";',
  10827. '']),
  10828. LinesToStr([
  10829. '$mod.DoIt([]);',
  10830. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10831. '']));
  10832. end;
  10833. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10834. begin
  10835. StartProgram(false);
  10836. Add([
  10837. 'type TArr = array of char;',
  10838. 'var',
  10839. ' c: char;',
  10840. ' s: string;',
  10841. ' a: TArr;',
  10842. 'procedure Run(const a: array of char);',
  10843. 'begin',
  10844. ' Run(c);',
  10845. ' Run(s);',
  10846. 'end;',
  10847. 'begin',
  10848. ' a:=c;',
  10849. ' a:=s;',
  10850. ' a:=#13;',
  10851. ' a:=''Foo'';',
  10852. ' Run(c);',
  10853. ' Run(s);',
  10854. '']);
  10855. ConvertProgram;
  10856. CheckSource('TestArray_ArrayOfCharAssignString',
  10857. LinesToStr([ // statements
  10858. 'this.c = "";',
  10859. 'this.s = "";',
  10860. 'this.a = [];',
  10861. 'this.Run = function (a) {',
  10862. ' $mod.Run($mod.c.split(""));',
  10863. ' $mod.Run($mod.s.split(""));',
  10864. '};',
  10865. '']),
  10866. LinesToStr([
  10867. '$mod.a = $mod.c.split("");',
  10868. '$mod.a = $mod.s.split("");',
  10869. '$mod.a = "\r".split("");',
  10870. '$mod.a = "Foo".split("");',
  10871. '$mod.Run($mod.c.split(""));',
  10872. '$mod.Run($mod.s.split(""));',
  10873. '']));
  10874. end;
  10875. procedure TTestModule.TestArray_ConstRef;
  10876. begin
  10877. StartProgram(false);
  10878. Add([
  10879. 'type TArr = array of word;',
  10880. 'procedure Run(constref a: TArr);',
  10881. 'begin',
  10882. 'end;',
  10883. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10884. 'var l: TArr;',
  10885. 'begin',
  10886. ' Run(l);',
  10887. ' Run(a);',
  10888. ' Run(b);',
  10889. ' Run(c);',
  10890. ' Run(d);',
  10891. ' Run(e);',
  10892. 'end;',
  10893. 'begin',
  10894. '']);
  10895. ConvertProgram;
  10896. CheckResolverUnexpectedHints();
  10897. CheckSource('TestArray_ConstRef',
  10898. LinesToStr([ // statements
  10899. 'this.Run = function (a) {',
  10900. '};',
  10901. 'this.Fly = function (a, b, c, d, e) {',
  10902. ' var l = [];',
  10903. ' $mod.Run(l);',
  10904. ' $mod.Run(a);',
  10905. ' $mod.Run(b.get());',
  10906. ' $mod.Run(c.get());',
  10907. ' $mod.Run(d);',
  10908. ' $mod.Run(e);',
  10909. '};',
  10910. '']),
  10911. LinesToStr([
  10912. '']));
  10913. end;
  10914. procedure TTestModule.TestArray_Concat;
  10915. begin
  10916. StartProgram(false);
  10917. Add([
  10918. 'type',
  10919. ' integer = longint;',
  10920. ' TFlag = (big,small);',
  10921. ' TFlags = set of TFlag;',
  10922. ' TRec = record',
  10923. ' i: integer;',
  10924. ' end;',
  10925. ' TArrInt = array of integer;',
  10926. ' TArrRec = array of TRec;',
  10927. ' TArrFlag = array of TFlag;',
  10928. ' TArrSet = array of TFlags;',
  10929. ' TArrJSValue = array of jsvalue;',
  10930. 'var',
  10931. ' ArrInt1, ArrInt2: tarrint;',
  10932. ' ArrRec1, ArrRec2: tarrrec;',
  10933. ' ArrFlag1, ArrFlag2: tarrflag;',
  10934. ' ArrSet1, ArrSet2: tarrset;',
  10935. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10936. 'begin',
  10937. ' arrint1:=concat(arrint2);',
  10938. ' arrint1:=concat(arrint2,arrint2);',
  10939. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10940. ' arrrec1:=concat(arrrec2);',
  10941. ' arrrec1:=concat(arrrec2,arrrec2);',
  10942. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10943. ' arrset1:=concat(arrset2);',
  10944. ' arrset1:=concat(arrset2,arrset2);',
  10945. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10946. ' arrjsvalue1:=concat(arrjsvalue2);',
  10947. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10948. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10949. ' arrint1:=concat([1],arrint2);',
  10950. ' arrflag1:=concat([big]);',
  10951. ' arrflag1:=concat([big],arrflag2);',
  10952. ' arrflag1:=concat(arrflag2,[small]);',
  10953. '']);
  10954. ConvertProgram;
  10955. CheckSource('TestArray_Concat',
  10956. LinesToStr([ // statements
  10957. 'this.TFlag = {',
  10958. ' "0": "big",',
  10959. ' big: 0,',
  10960. ' "1": "small",',
  10961. ' small: 1',
  10962. '};',
  10963. 'rtl.recNewT(this, "TRec", function () {',
  10964. ' this.i = 0;',
  10965. ' this.$eq = function (b) {',
  10966. ' return this.i === b.i;',
  10967. ' };',
  10968. ' this.$assign = function (s) {',
  10969. ' this.i = s.i;',
  10970. ' return this;',
  10971. ' };',
  10972. '});',
  10973. 'this.ArrInt1 = [];',
  10974. 'this.ArrInt2 = [];',
  10975. 'this.ArrRec1 = [];',
  10976. 'this.ArrRec2 = [];',
  10977. 'this.ArrFlag1 = [];',
  10978. 'this.ArrFlag2 = [];',
  10979. 'this.ArrSet1 = [];',
  10980. 'this.ArrSet2 = [];',
  10981. 'this.ArrJSValue1 = [];',
  10982. 'this.ArrJSValue2 = [];',
  10983. '']),
  10984. LinesToStr([ // $mod.$main
  10985. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  10986. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  10987. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  10988. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  10989. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  10990. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  10991. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  10992. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  10993. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  10994. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  10995. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  10996. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  10997. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  10998. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  10999. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11000. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11001. '']));
  11002. end;
  11003. procedure TTestModule.TestArray_Concat_Append;
  11004. begin
  11005. StartProgram(false);
  11006. Add([
  11007. 'type',
  11008. ' integer = longint;',
  11009. ' TFlag = (big,small);',
  11010. ' TFlags = set of TFlag;',
  11011. ' TRec = record',
  11012. ' i: integer;',
  11013. ' end;',
  11014. ' TArrInt = array of integer;',
  11015. ' TArrRec = array of TRec;',
  11016. ' TArrFlag = array of TFlag;',
  11017. ' TArrSet = array of TFlags;',
  11018. ' TArrJSValue = array of jsvalue;',
  11019. 'var',
  11020. ' ArrInt: tarrint;',
  11021. ' ArrRec: tarrrec;',
  11022. ' ArrFlag: tarrflag;',
  11023. ' ArrSet: tarrset;',
  11024. ' ArrJSValue: tarrjsvalue;',
  11025. ' r: TRec;',
  11026. ' f: TFlags;',
  11027. 'begin',
  11028. ' // append',
  11029. ' arrint:=concat(arrint);',
  11030. ' arrint:=concat(arrint,[2]);',
  11031. ' arrint:=concat(arrint,[3,4]);',
  11032. ' arrrec:=concat(arrrec);',
  11033. ' arrrec:=concat(arrrec,[r]);',
  11034. ' arrrec:=concat(arrrec,[r,r]);',
  11035. ' arrset:=concat(arrset);',
  11036. ' arrset:=concat(arrset,[f]);',
  11037. ' arrset:=concat(arrset,[f,f]);',
  11038. ' arrjsvalue:=concat(arrjsvalue);',
  11039. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11040. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11041. ' arrflag:=concat(arrflag);',
  11042. ' arrflag:=concat(arrflag,[small]);',
  11043. ' arrflag:=concat(arrflag,[small,big]);',
  11044. '']);
  11045. ConvertProgram;
  11046. CheckSource('TestArray_Concat_Append',
  11047. LinesToStr([ // statements
  11048. 'this.TFlag = {',
  11049. ' "0": "big",',
  11050. ' big: 0,',
  11051. ' "1": "small",',
  11052. ' small: 1',
  11053. '};',
  11054. 'rtl.recNewT(this, "TRec", function () {',
  11055. ' this.i = 0;',
  11056. ' this.$eq = function (b) {',
  11057. ' return this.i === b.i;',
  11058. ' };',
  11059. ' this.$assign = function (s) {',
  11060. ' this.i = s.i;',
  11061. ' return this;',
  11062. ' };',
  11063. '});',
  11064. 'this.ArrInt = [];',
  11065. 'this.ArrRec = [];',
  11066. 'this.ArrFlag = [];',
  11067. 'this.ArrSet = [];',
  11068. 'this.ArrJSValue = [];',
  11069. 'this.r = this.TRec.$new();',
  11070. 'this.f = {};',
  11071. '']),
  11072. LinesToStr([ // $mod.$main
  11073. '$mod.ArrInt = $mod.ArrInt;',
  11074. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11075. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11076. '$mod.ArrRec = $mod.ArrRec;',
  11077. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11078. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11079. '$mod.ArrSet = $mod.ArrSet;',
  11080. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11081. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11082. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11083. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11084. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11085. '$mod.ArrFlag = $mod.ArrFlag;',
  11086. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11087. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11088. '']));
  11089. end;
  11090. procedure TTestModule.TestArray_Concat_Append_Var;
  11091. begin
  11092. StartProgram(false);
  11093. Add([
  11094. 'type',
  11095. ' TArrInt = array of word;',
  11096. '',
  11097. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11098. 'begin',
  11099. ' a:=concat(a,[2]);',
  11100. ' b:=concat(b,[2]);',
  11101. 'end;',
  11102. 'var',
  11103. ' ArrInt: tarrint;',
  11104. 'begin',
  11105. ' Fly(ArrInt,ArrInt);',
  11106. '']);
  11107. ConvertProgram;
  11108. CheckSource('TestArray_Concat_Append_Var',
  11109. LinesToStr([ // statements
  11110. 'this.Fly = function (a, b) {',
  11111. ' a = rtl.arrayPushN(a, 2);',
  11112. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11113. '};',
  11114. 'this.ArrInt = [];',
  11115. '']),
  11116. LinesToStr([ // $mod.$main
  11117. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11118. ' p: $mod,',
  11119. ' get: function () {',
  11120. ' return this.p.ArrInt;',
  11121. ' },',
  11122. ' set: function (v) {',
  11123. ' this.p.ArrInt = v;',
  11124. ' }',
  11125. '});',
  11126. '']));
  11127. end;
  11128. procedure TTestModule.TestArray_Copy;
  11129. begin
  11130. StartProgram(false);
  11131. Add([
  11132. 'type',
  11133. ' integer = longint;',
  11134. ' TFlag = (big,small);',
  11135. ' TFlags = set of TFlag;',
  11136. ' TRec = record',
  11137. ' i: integer;',
  11138. ' end;',
  11139. ' TArrInt = array of integer;',
  11140. ' TArrRec = array of TRec;',
  11141. ' TArrSet = array of TFlags;',
  11142. ' TArrJSValue = array of jsvalue;',
  11143. 'var',
  11144. ' ArrInt: tarrint;',
  11145. ' ArrRec: tarrrec;',
  11146. ' ArrSet: tarrset;',
  11147. ' ArrJSValue: tarrjsvalue;',
  11148. 'begin',
  11149. ' arrint:=copy(arrint);',
  11150. ' arrint:=copy(arrint,2);',
  11151. ' arrint:=copy(arrint,3,4);',
  11152. ' arrint:=copy([1,1],1,2);',
  11153. ' arrrec:=copy(arrrec);',
  11154. ' arrrec:=copy(arrrec,5);',
  11155. ' arrrec:=copy(arrrec,6,7);',
  11156. ' arrset:=copy(arrset);',
  11157. ' arrset:=copy(arrset,8);',
  11158. ' arrset:=copy(arrset,9,10);',
  11159. ' arrjsvalue:=copy(arrjsvalue);',
  11160. ' arrjsvalue:=copy(arrjsvalue,11);',
  11161. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11162. ' ']);
  11163. ConvertProgram;
  11164. CheckSource('TestArray_Copy',
  11165. LinesToStr([ // statements
  11166. 'this.TFlag = {',
  11167. ' "0": "big",',
  11168. ' big: 0,',
  11169. ' "1": "small",',
  11170. ' small: 1',
  11171. '};',
  11172. 'rtl.recNewT(this, "TRec", function () {',
  11173. ' this.i = 0;',
  11174. ' this.$eq = function (b) {',
  11175. ' return this.i === b.i;',
  11176. ' };',
  11177. ' this.$assign = function (s) {',
  11178. ' this.i = s.i;',
  11179. ' return this;',
  11180. ' };',
  11181. '});',
  11182. 'this.ArrInt = [];',
  11183. 'this.ArrRec = [];',
  11184. 'this.ArrSet = [];',
  11185. 'this.ArrJSValue = [];',
  11186. '']),
  11187. LinesToStr([ // $mod.$main
  11188. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11189. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11190. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11191. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11192. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11193. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11194. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11195. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11196. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11197. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11198. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11199. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11200. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11201. '']));
  11202. end;
  11203. procedure TTestModule.TestArray_InsertDelete;
  11204. begin
  11205. StartProgram(false);
  11206. Add([
  11207. 'type',
  11208. ' integer = longint;',
  11209. ' TFlag = (big,small);',
  11210. ' TFlags = set of TFlag;',
  11211. ' TRec = record',
  11212. ' i: integer;',
  11213. ' end;',
  11214. ' TArrInt = array of integer;',
  11215. ' TArrRec = array of TRec;',
  11216. ' TArrSet = array of TFlags;',
  11217. ' TArrJSValue = array of jsvalue;',
  11218. ' TArrArrInt = array of TArrInt;',
  11219. 'var',
  11220. ' ArrInt: tarrint;',
  11221. ' ArrRec: tarrrec;',
  11222. ' ArrSet: tarrset;',
  11223. ' ArrJSValue: tarrjsvalue;',
  11224. ' ArrArrInt: TArrArrInt;',
  11225. 'begin',
  11226. ' Insert(1,arrint,2);',
  11227. ' Insert(arrint[3],arrint,4);',
  11228. ' Insert(arrrec[5],arrrec,6);',
  11229. ' Insert(arrset[7],arrset,7);',
  11230. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11231. ' Insert(10,arrjsvalue,11);',
  11232. ' Insert([23],arrarrint,22);',
  11233. ' Delete(arrint,12,13);',
  11234. ' Delete(arrrec,14,15);',
  11235. ' Delete(arrset,17,18);',
  11236. ' Delete(arrjsvalue,19,10);']);
  11237. ConvertProgram;
  11238. CheckSource('TestArray_InsertDelete',
  11239. LinesToStr([ // statements
  11240. 'this.TFlag = {',
  11241. ' "0": "big",',
  11242. ' big: 0,',
  11243. ' "1": "small",',
  11244. ' small: 1',
  11245. '};',
  11246. 'rtl.recNewT(this, "TRec", function () {',
  11247. ' this.i = 0;',
  11248. ' this.$eq = function (b) {',
  11249. ' return this.i === b.i;',
  11250. ' };',
  11251. ' this.$assign = function (s) {',
  11252. ' this.i = s.i;',
  11253. ' return this;',
  11254. ' };',
  11255. '});',
  11256. 'this.ArrInt = [];',
  11257. 'this.ArrRec = [];',
  11258. 'this.ArrSet = [];',
  11259. 'this.ArrJSValue = [];',
  11260. 'this.ArrArrInt = [];',
  11261. '']),
  11262. LinesToStr([ // $mod.$main
  11263. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11264. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11265. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11266. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11267. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11268. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11269. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11270. '$mod.ArrInt.splice(12, 13);',
  11271. '$mod.ArrRec.splice(14, 15);',
  11272. '$mod.ArrSet.splice(17, 18);',
  11273. '$mod.ArrJSValue.splice(19, 10);',
  11274. '']));
  11275. end;
  11276. procedure TTestModule.TestArray_Add_Append;
  11277. begin
  11278. StartProgram(false);
  11279. Add([
  11280. '{$modeswitch arrayoperators}',
  11281. 'type',
  11282. ' integer = longint;',
  11283. ' TFlag = (big,small);',
  11284. ' TFlags = set of TFlag;',
  11285. ' TRec = record',
  11286. ' i: integer;',
  11287. ' end;',
  11288. ' TArrInt = array of integer;',
  11289. ' TArrRec = array of TRec;',
  11290. ' TArrFlag = array of TFlag;',
  11291. ' TArrSet = array of TFlags;',
  11292. ' TArrJSValue = array of jsvalue;',
  11293. 'var',
  11294. ' ArrInt: tarrint;',
  11295. ' ArrRec: tarrrec;',
  11296. ' ArrFlag: tarrflag;',
  11297. ' ArrSet: tarrset;',
  11298. ' ArrJSValue: tarrjsvalue;',
  11299. ' r: TRec;',
  11300. ' f: TFlags;',
  11301. 'begin',
  11302. ' // append',
  11303. ' arrint:=arrint+[2];',
  11304. ' arrint:=arrint+[3,4];',
  11305. ' arrrec:=arrrec+[r];',
  11306. ' arrrec:=arrrec+[r,r];',
  11307. ' arrset:=arrset+[f];',
  11308. ' arrset:=arrset+[f,f];',
  11309. ' arrjsvalue:=arrjsvalue+[11];',
  11310. ' arrjsvalue:=arrjsvalue+[12,13];',
  11311. ' arrflag:=arrflag+[small];',
  11312. ' arrflag:=arrflag+[small,big];',
  11313. '']);
  11314. ConvertProgram;
  11315. CheckSource('TestArray_Add_Append',
  11316. LinesToStr([ // statements
  11317. 'this.TFlag = {',
  11318. ' "0": "big",',
  11319. ' big: 0,',
  11320. ' "1": "small",',
  11321. ' small: 1',
  11322. '};',
  11323. 'rtl.recNewT(this, "TRec", function () {',
  11324. ' this.i = 0;',
  11325. ' this.$eq = function (b) {',
  11326. ' return this.i === b.i;',
  11327. ' };',
  11328. ' this.$assign = function (s) {',
  11329. ' this.i = s.i;',
  11330. ' return this;',
  11331. ' };',
  11332. '});',
  11333. 'this.ArrInt = [];',
  11334. 'this.ArrRec = [];',
  11335. 'this.ArrFlag = [];',
  11336. 'this.ArrSet = [];',
  11337. 'this.ArrJSValue = [];',
  11338. 'this.r = this.TRec.$new();',
  11339. 'this.f = {};',
  11340. '']),
  11341. LinesToStr([ // $mod.$main
  11342. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11343. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11344. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11345. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11346. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11347. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11348. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11349. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11350. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11351. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11352. '']));
  11353. end;
  11354. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11355. begin
  11356. Parser.Options:=Parser.Options+[po_cassignments];
  11357. StartProgram(false);
  11358. Add([
  11359. '{$modeswitch arrayoperators}',
  11360. 'type',
  11361. ' integer = longint;',
  11362. ' TArrInt = array of integer;',
  11363. ' TArrStr = array of string;',
  11364. 'const',
  11365. ' Ints: TArrInt = (1,2,3);',
  11366. ' Aliases: TarrStr = (''foo'',''b'');',
  11367. ' OneInt: TArrInt = (7);',
  11368. ' OneStr: array of integer = (7);',
  11369. ' Chars: array of char = ''aoc'';',
  11370. ' Names: array of string = (''a'',''foo'');',
  11371. ' NameCount = low(Names)+high(Names)+length(Names);',
  11372. 'var i: integer;',
  11373. 'begin',
  11374. ' Ints:=[];',
  11375. ' Ints:=[1,1];',
  11376. ' Ints:=[1]+[2];',
  11377. ' Ints:=[2];',
  11378. ' Ints:=[]+ints;',
  11379. ' Ints:=Ints+[];',
  11380. ' Ints:=Ints+OneInt;',
  11381. ' Ints:=Ints+[1,1];',
  11382. ' Ints:=[i,i]+Ints;',
  11383. ' Ints:=[1]+[i]+[3];',
  11384. '']);
  11385. ConvertProgram;
  11386. CheckSource('TestArray_DynArrayConstObjFPC',
  11387. LinesToStr([ // statements
  11388. 'this.Ints = [1, 2, 3];',
  11389. 'this.Aliases = ["foo", "b"];',
  11390. 'this.OneInt = [7];',
  11391. 'this.OneStr = [7];',
  11392. 'this.Chars = ["a", "o", "c"];',
  11393. 'this.Names = ["a", "foo"];',
  11394. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11395. 'this.i = 0;',
  11396. '']),
  11397. LinesToStr([ // $mod.$main
  11398. '$mod.Ints = [];',
  11399. '$mod.Ints = [1, 1];',
  11400. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11401. '$mod.Ints = [2];',
  11402. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11403. '$mod.Ints = $mod.Ints;',
  11404. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11405. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11406. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11407. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11408. '']));
  11409. end;
  11410. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11411. begin
  11412. StartProgram(false);
  11413. // Note: const c = [1,1]; defines a set!
  11414. Add([
  11415. '{$mode delphi}',
  11416. 'type',
  11417. ' integer = longint;',
  11418. ' TArrInt = array of integer;',
  11419. ' TArrStr = array of string;',
  11420. 'const',
  11421. ' Ints: TArrInt = [1,1,2];',
  11422. ' Aliases: TarrStr = [''foo'',''b''];',
  11423. ' OneInt: TArrInt = [7];',
  11424. ' OneStr: array of integer = [7]+[8];',
  11425. ' Chars: array of char = ''aoc'';',
  11426. ' Names: array of string = [''a'',''a''];',
  11427. ' NameCount = low(Names)+high(Names)+length(Names);',
  11428. 'begin',
  11429. '']);
  11430. ConvertProgram;
  11431. CheckSource('TestArray_DynArrayConstDelphi',
  11432. LinesToStr([ // statements
  11433. 'this.Ints = [1, 1, 2];',
  11434. 'this.Aliases = ["foo", "b"];',
  11435. 'this.OneInt = [7];',
  11436. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11437. 'this.Chars = ["a", "o", "c"];',
  11438. 'this.Names = ["a", "a"];',
  11439. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11440. '']),
  11441. LinesToStr([ // $mod.$main
  11442. '']));
  11443. end;
  11444. procedure TTestModule.TestArray_ArrayLitAsParam;
  11445. begin
  11446. StartProgram(false);
  11447. Add([
  11448. '{$modeswitch arrayoperators}',
  11449. 'type',
  11450. ' integer = longint;',
  11451. ' TArrInt = array of integer;',
  11452. ' TArrSet = array of (red,green,blue);',
  11453. 'procedure DoOpenInt(const a: array of integer); forward;',
  11454. 'procedure DoInt(const a: TArrInt);',
  11455. 'begin',
  11456. ' DoInt(a+[1]);',
  11457. ' DoInt([1]+a);',
  11458. ' DoOpenInt(a);',
  11459. ' DoOpenInt(a+[1]);',
  11460. ' DoOpenInt([1]+a);',
  11461. 'end;',
  11462. 'procedure DoOpenInt(const a: array of integer);',
  11463. 'begin',
  11464. ' DoOpenInt(a+[1]);',
  11465. ' DoOpenInt([1]+a);',
  11466. ' DoInt(a);',
  11467. ' DoInt(a+[1]);',
  11468. ' DoInt([1]+a);',
  11469. 'end;',
  11470. 'procedure DoSet(const a: TArrSet);',
  11471. 'begin',
  11472. ' DoSet(a+[red]);',
  11473. ' DoSet([blue]+a);',
  11474. 'end;',
  11475. 'var',
  11476. ' i: TArrInt;',
  11477. ' s: TArrSet;',
  11478. 'begin',
  11479. ' DoInt([1]);',
  11480. ' DoInt([1]+[2]);',
  11481. ' DoInt(i+[1]);',
  11482. ' DoInt([1]+i);',
  11483. ' DoOpenInt([1]);',
  11484. ' DoOpenInt([1]+[2]);',
  11485. ' DoOpenInt(i+[1]);',
  11486. ' DoOpenInt([1]+i);',
  11487. ' DoSet([red]);',
  11488. ' DoSet([blue]+[green]);',
  11489. ' DoSet(s+[blue]);',
  11490. ' DoSet([red]+s);',
  11491. '']);
  11492. ConvertProgram;
  11493. CheckSource('TestArray_ArrayLitAsParam',
  11494. LinesToStr([ // statements
  11495. 'this.TArrSet$a = {',
  11496. ' "0": "red",',
  11497. ' red: 0,',
  11498. ' "1": "green",',
  11499. ' green: 1,',
  11500. ' "2": "blue",',
  11501. ' blue: 2',
  11502. '};',
  11503. 'this.DoInt = function (a) {',
  11504. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11505. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11506. ' $mod.DoOpenInt(a);',
  11507. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11508. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11509. '};',
  11510. 'this.DoOpenInt = function (a) {',
  11511. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11512. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11513. ' $mod.DoInt(a);',
  11514. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11515. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11516. '};',
  11517. 'this.DoSet = function (a) {',
  11518. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11519. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11520. '};',
  11521. 'this.i = [];',
  11522. 'this.s = [];',
  11523. '']),
  11524. LinesToStr([ // $mod.$main
  11525. '$mod.DoInt([1]);',
  11526. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11527. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11528. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11529. '$mod.DoOpenInt([1]);',
  11530. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11531. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11532. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11533. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11534. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11535. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11536. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11537. '']));
  11538. end;
  11539. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11540. begin
  11541. StartProgram(false);
  11542. Add([
  11543. '{$modeswitch arrayoperators}',
  11544. 'type',
  11545. ' integer = longint;',
  11546. ' TArrInt = array of integer;',
  11547. ' TArrArrInt = array of TArrInt;',
  11548. 'procedure DoInt(const a: TArrArrInt);',
  11549. 'begin',
  11550. ' DoInt(a+[[1]]);',
  11551. ' DoInt([[1]]+a);',
  11552. ' DoInt(a);',
  11553. 'end;',
  11554. 'var',
  11555. ' i: TArrInt;',
  11556. ' a: TArrArrInt;',
  11557. 'begin',
  11558. ' a:=[[1]];',
  11559. ' a:=[i];',
  11560. ' a:=a+[i];',
  11561. ' a:=[i]+a;',
  11562. ' a:=[[1]+i];',
  11563. ' a:=[[1]+[2]];',
  11564. ' a:=[i+[2]];',
  11565. ' DoInt([[1]]);',
  11566. ' DoInt([[1]+[2],[3,4],[5]]);',
  11567. ' DoInt([i+[1]]+a);',
  11568. ' DoInt([i]+a);',
  11569. '']);
  11570. ConvertProgram;
  11571. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11572. LinesToStr([ // statements
  11573. 'this.DoInt = function (a) {',
  11574. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11575. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11576. ' $mod.DoInt(a);',
  11577. '};',
  11578. 'this.i = [];',
  11579. 'this.a = [];',
  11580. '']),
  11581. LinesToStr([ // $mod.$main
  11582. '$mod.a = [[1]];',
  11583. '$mod.a = [$mod.i];',
  11584. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11585. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11586. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11587. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11588. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11589. '$mod.DoInt([[1]]);',
  11590. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11591. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11592. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11593. '']));
  11594. end;
  11595. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11596. begin
  11597. StartProgram(false);
  11598. Add([
  11599. '{$modeswitch arrayoperators}',
  11600. 'type',
  11601. ' integer = longint;',
  11602. ' TArrInt = array[1..2] of integer;',
  11603. ' TArrArrInt = array of TArrInt;',
  11604. 'procedure DoInt(const a: TArrArrInt);',
  11605. 'begin',
  11606. ' DoInt(a+[[1,2]]);',
  11607. ' DoInt([[1,2]]+a);',
  11608. ' DoInt(a);',
  11609. 'end;',
  11610. 'var',
  11611. ' i: TArrInt;',
  11612. ' a: TArrArrInt;',
  11613. 'begin',
  11614. ' a:=[[1,1]];',
  11615. ' a:=[i];',
  11616. ' a:=a+[i];',
  11617. ' a:=[i]+a;',
  11618. ' DoInt([[1,1]]);',
  11619. ' DoInt([[1,2],[3,4]]);',
  11620. '']);
  11621. ConvertProgram;
  11622. CheckSource('TestArray_ArrayLitStaticAsParam',
  11623. LinesToStr([ // statements
  11624. 'this.DoInt = function (a) {',
  11625. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11626. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11627. ' $mod.DoInt(a);',
  11628. '};',
  11629. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11630. 'this.a = [];',
  11631. '']),
  11632. LinesToStr([ // $mod.$main
  11633. '$mod.a = [[1, 1]];',
  11634. '$mod.a = [$mod.i.slice(0)];',
  11635. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11636. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11637. '$mod.DoInt([[1, 1]]);',
  11638. '$mod.DoInt([[1, 2], [3, 4]]);',
  11639. '']));
  11640. end;
  11641. procedure TTestModule.TestArray_ForInArrOfString;
  11642. begin
  11643. StartProgram(false);
  11644. Add([
  11645. 'type',
  11646. 'type',
  11647. ' TMonthNameArray = array [1..12] of string;',
  11648. ' TMonthNames = TMonthNameArray;',
  11649. ' TObject = class',
  11650. ' private',
  11651. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11652. ' public',
  11653. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11654. ' end;',
  11655. 'var',
  11656. ' f: TObject;',
  11657. ' Month: string;',
  11658. ' Names: array of string = (''a'',''foo'',''bar'');',
  11659. ' i: longint;',
  11660. 'begin',
  11661. ' for Month in f.LongMonthNames do ;',
  11662. ' for Month in Names do ;',
  11663. ' for i:=low(Names) to high(Names) do ;',
  11664. '']);
  11665. ConvertProgram;
  11666. CheckSource('TestArray_ForInArrOfString',
  11667. LinesToStr([ // statements
  11668. 'rtl.createClass(this, "TObject", null, function () {',
  11669. ' this.$init = function () {',
  11670. ' };',
  11671. ' this.$final = function () {',
  11672. ' };',
  11673. '});',
  11674. 'this.f = null;',
  11675. 'this.Month = "";',
  11676. 'this.Names = ["a", "foo", "bar"];',
  11677. 'this.i = 0;',
  11678. '']),
  11679. LinesToStr([ // $mod.$main
  11680. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11681. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11682. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11683. '']));
  11684. end;
  11685. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11686. begin
  11687. StartProgram(false);
  11688. Add([
  11689. '{$modeswitch externalclass}',
  11690. 'type',
  11691. ' TJSObject = class external name ''Object''',
  11692. ' end;',
  11693. ' TJSArray = class external name ''Array''',
  11694. ' class function isArray(Value: JSValue) : boolean;',
  11695. ' function concat() : TJSArray; varargs;',
  11696. ' end;',
  11697. 'var',
  11698. ' aObj: TJSArray;',
  11699. ' a: array of longint;',
  11700. ' o: TJSObject;',
  11701. 'begin',
  11702. ' if TJSArray.isArray(65) then ;',
  11703. ' aObj:=TJSArray(a).concat(a);',
  11704. ' o:=TJSObject(a);',
  11705. ' aObj:=TJSArray([''bird'',''ant'']);',
  11706. '']);
  11707. ConvertProgram;
  11708. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11709. LinesToStr([ // statements
  11710. 'this.aObj = null;',
  11711. 'this.a = [];',
  11712. 'this.o = null;',
  11713. '']),
  11714. LinesToStr([ // $mod.$main
  11715. 'if (Array.isArray(65)) ;',
  11716. '$mod.aObj = $mod.a.concat($mod.a);',
  11717. '$mod.o = $mod.a;',
  11718. '$mod.aObj = ["bird", "ant"];',
  11719. '']));
  11720. end;
  11721. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11722. begin
  11723. StartProgram(false);
  11724. Add([
  11725. '{$modeswitch externalclass}',
  11726. 'type',
  11727. ' TArrStr = array of string;',
  11728. ' TJSArray = class external name ''Array''',
  11729. ' end;',
  11730. ' TJSObject = class external name ''Object''',
  11731. ' end;',
  11732. 'var',
  11733. ' aObj: TJSArray;',
  11734. ' a: TArrStr;',
  11735. ' jo: TJSObject;',
  11736. 'begin',
  11737. ' a:=TArrStr(aObj);',
  11738. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11739. ' a:=TarrStr(jo);',
  11740. '']);
  11741. ConvertProgram;
  11742. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11743. LinesToStr([ // statements
  11744. 'this.aObj = null;',
  11745. 'this.a = [];',
  11746. 'this.jo = null;',
  11747. '']),
  11748. LinesToStr([ // $mod.$main
  11749. '$mod.a = $mod.aObj;',
  11750. '$mod.aObj[1] = $mod.aObj[2];',
  11751. '$mod.a = $mod.jo;',
  11752. '']));
  11753. end;
  11754. procedure TTestModule.TestArrayOfConst_TVarRec;
  11755. begin
  11756. StartProgram(true,[supTVarRec]);
  11757. Add([
  11758. 'procedure Say(args: array of const);',
  11759. 'var',
  11760. ' i: longint;',
  11761. ' v: TVarRec;',
  11762. 'begin',
  11763. ' for i:=low(args) to high(args) do begin',
  11764. ' v:=args[i];',
  11765. ' case v.vtype of',
  11766. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11767. ' end;',
  11768. ' end;',
  11769. ' for v in args do ;',
  11770. ' args:=nil;',
  11771. ' SetLength(args,2);',
  11772. 'end;',
  11773. 'begin']);
  11774. ConvertProgram;
  11775. CheckSource('TestArrayOfConst_TVarRec',
  11776. LinesToStr([ // statements
  11777. 'this.Say = function (args) {',
  11778. ' var i = 0;',
  11779. ' var v = pas.system.TVarRec.$new();',
  11780. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11781. ' i = $l;',
  11782. ' v.$assign(args[i]);',
  11783. ' var $tmp = v.VType;',
  11784. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11785. ' };',
  11786. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11787. ' args = [];',
  11788. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11789. '};',
  11790. '']),
  11791. LinesToStr([ // $mod.$main
  11792. ]));
  11793. end;
  11794. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11795. begin
  11796. StartProgram(true,[supTVarRec]);
  11797. Add([
  11798. 'procedure Say(args: array of const);',
  11799. 'begin',
  11800. ' Say(args);',
  11801. 'end;',
  11802. 'var',
  11803. ' p: Pointer;',
  11804. ' j: jsvalue;',
  11805. ' c: currency;',
  11806. 'begin',
  11807. ' Say([]);',
  11808. ' Say([1]);',
  11809. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11810. '']);
  11811. ConvertProgram;
  11812. CheckSource('TestArrayOfConst_PassBaseTypes',
  11813. LinesToStr([ // statements
  11814. 'this.Say = function (args) {',
  11815. ' $mod.Say(args);',
  11816. '};',
  11817. 'this.p = null;',
  11818. 'this.j = undefined;',
  11819. 'this.c = 0;',
  11820. '']),
  11821. LinesToStr([ // $mod.$main
  11822. '$mod.Say([]);',
  11823. '$mod.Say(pas.system.VarRecs(0, 1));',
  11824. '$mod.Say(pas.system.VarRecs(',
  11825. ' 9,',
  11826. ' "c",',
  11827. ' 18,',
  11828. ' "foo",',
  11829. ' 5,',
  11830. ' null,',
  11831. ' 1,',
  11832. ' true,',
  11833. ' 3,',
  11834. ' 1.3,',
  11835. ' 5,',
  11836. ' $mod.p,',
  11837. ' 20,',
  11838. ' $mod.j,',
  11839. ' 12,',
  11840. ' $mod.c',
  11841. ' ));',
  11842. '']));
  11843. end;
  11844. procedure TTestModule.TestArrayOfConst_PassObj;
  11845. begin
  11846. StartProgram(true,[supTVarRec]);
  11847. Add([
  11848. '{$interfaces corba}',
  11849. 'type',
  11850. ' TObject = class',
  11851. ' end;',
  11852. ' TClass = class of TObject;',
  11853. ' IUnknown = interface',
  11854. ' end;',
  11855. 'procedure Say(args: array of const);',
  11856. 'begin',
  11857. 'end;',
  11858. 'var',
  11859. ' o: TObject;',
  11860. ' c: TClass;',
  11861. ' i: IUnknown;',
  11862. 'begin',
  11863. ' Say([o,c,TObject]);',
  11864. ' Say([nil,i]);',
  11865. '']);
  11866. ConvertProgram;
  11867. CheckSource('TestArrayOfConst_PassObj',
  11868. LinesToStr([ // statements
  11869. 'rtl.createClass(this, "TObject", null, function () {',
  11870. ' this.$init = function () {',
  11871. ' };',
  11872. ' this.$final = function () {',
  11873. ' };',
  11874. '});',
  11875. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11876. 'this.Say = function (args) {',
  11877. '};',
  11878. 'this.o = null;',
  11879. 'this.c = null;',
  11880. 'this.i = null;',
  11881. '']),
  11882. LinesToStr([ // $mod.$main
  11883. '$mod.Say(pas.system.VarRecs(',
  11884. ' 7,',
  11885. ' $mod.o,',
  11886. ' 8,',
  11887. ' $mod.c,',
  11888. ' 8,',
  11889. ' $mod.TObject',
  11890. '));',
  11891. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11892. '']));
  11893. end;
  11894. procedure TTestModule.TestRecord_Empty;
  11895. begin
  11896. StartProgram(false);
  11897. Add([
  11898. 'type',
  11899. ' TRecA = record',
  11900. ' end;',
  11901. 'var a,b: TRecA;',
  11902. 'begin',
  11903. ' if a=b then ;']);
  11904. ConvertProgram;
  11905. CheckSource('TestRecord_Empty',
  11906. LinesToStr([ // statements
  11907. 'rtl.recNewT(this, "TRecA", function () {',
  11908. ' this.$eq = function (b) {',
  11909. ' return true;',
  11910. ' };',
  11911. ' this.$assign = function (s) {',
  11912. ' return this;',
  11913. ' };',
  11914. '});',
  11915. 'this.a = this.TRecA.$new();',
  11916. 'this.b = this.TRecA.$new();',
  11917. '']),
  11918. LinesToStr([ // $mod.$main
  11919. 'if ($mod.a.$eq($mod.b)) ;'
  11920. ]));
  11921. end;
  11922. procedure TTestModule.TestRecord_Var;
  11923. begin
  11924. StartProgram(false);
  11925. Add('type');
  11926. Add(' TRecA = record');
  11927. Add(' Bold: longint;');
  11928. Add(' end;');
  11929. Add('var Rec: TRecA;');
  11930. Add('begin');
  11931. Add(' rec.bold:=123');
  11932. ConvertProgram;
  11933. CheckSource('TestRecord_Var',
  11934. LinesToStr([ // statements
  11935. 'rtl.recNewT(this, "TRecA", function () {',
  11936. ' this.Bold = 0;',
  11937. ' this.$eq = function (b) {',
  11938. ' return this.Bold === b.Bold;',
  11939. ' };',
  11940. ' this.$assign = function (s) {',
  11941. ' this.Bold = s.Bold;',
  11942. ' return this;',
  11943. ' };',
  11944. '});',
  11945. 'this.Rec = this.TRecA.$new();',
  11946. '']),
  11947. LinesToStr([ // $mod.$main
  11948. '$mod.Rec.Bold = 123;'
  11949. ]));
  11950. end;
  11951. procedure TTestModule.TestRecord_VarExternal;
  11952. begin
  11953. StartProgram(false);
  11954. Add([
  11955. '{$modeswitch externalclass}',
  11956. 'type',
  11957. ' TRecA = record',
  11958. ' i: byte;',
  11959. ' length_: longint external name ''length'';',
  11960. ' end;',
  11961. 'var Rec: TRecA;',
  11962. 'begin',
  11963. ' rec.length_ := rec.length_',
  11964. '']);
  11965. ConvertProgram;
  11966. CheckSource('TestRecord_VarExternal',
  11967. LinesToStr([ // statements
  11968. 'rtl.recNewT(this, "TRecA", function () {',
  11969. ' this.i = 0;',
  11970. ' this.$eq = function (b) {',
  11971. ' return (this.i === b.i) && (this.length === b.length);',
  11972. ' };',
  11973. ' this.$assign = function (s) {',
  11974. ' this.i = s.i;',
  11975. ' this.length = s.length;',
  11976. ' return this;',
  11977. ' };',
  11978. '});',
  11979. 'this.Rec = this.TRecA.$new();',
  11980. '']),
  11981. LinesToStr([ // $mod.$main
  11982. '$mod.Rec.length = $mod.Rec.length;'
  11983. ]));
  11984. end;
  11985. procedure TTestModule.TestRecord_WithDo;
  11986. begin
  11987. StartProgram(false);
  11988. Add('type');
  11989. Add(' TRec = record');
  11990. Add(' vI: longint;');
  11991. Add(' end;');
  11992. Add('var');
  11993. Add(' Int: longint;');
  11994. Add(' r: TRec;');
  11995. Add('begin');
  11996. Add(' with r do');
  11997. Add(' int:=vi;');
  11998. Add(' with r do begin');
  11999. Add(' int:=vi;');
  12000. Add(' vi:=int;');
  12001. Add(' end;');
  12002. ConvertProgram;
  12003. CheckSource('TestWithRecordDo',
  12004. LinesToStr([ // statements
  12005. 'rtl.recNewT(this, "TRec", function () {',
  12006. ' this.vI = 0;',
  12007. ' this.$eq = function (b) {',
  12008. ' return this.vI === b.vI;',
  12009. ' };',
  12010. ' this.$assign = function (s) {',
  12011. ' this.vI = s.vI;',
  12012. ' return this;',
  12013. ' };',
  12014. '});',
  12015. 'this.Int = 0;',
  12016. 'this.r = this.TRec.$new();',
  12017. '']),
  12018. LinesToStr([ // $mod.$main
  12019. 'var $with = $mod.r;',
  12020. '$mod.Int = $with.vI;',
  12021. 'var $with1 = $mod.r;',
  12022. '$mod.Int = $with1.vI;',
  12023. '$with1.vI = $mod.Int;'
  12024. ]));
  12025. end;
  12026. procedure TTestModule.TestRecord_Assign;
  12027. begin
  12028. StartProgram(false);
  12029. Add([
  12030. 'type',
  12031. ' TEnum = (red,green);',
  12032. ' TEnums = set of TEnum;',
  12033. ' TSmallRec = record',
  12034. ' N: longint;',
  12035. ' end;',
  12036. ' TBigRec = record',
  12037. ' Int: longint;',
  12038. ' D: double;',
  12039. ' Arr: array of longint;',
  12040. ' Arr2: array[1..2] of longint;',
  12041. ' Small: TSmallRec;',
  12042. ' Enums: TEnums;',
  12043. ' end;',
  12044. 'var',
  12045. ' r, s: TBigRec;',
  12046. 'begin',
  12047. ' r:=s;',
  12048. ' r:=default(TBigRec);',
  12049. ' r:=default(s);',
  12050. '']);
  12051. ConvertProgram;
  12052. CheckSource('TestRecord_Assign',
  12053. LinesToStr([ // statements
  12054. 'this.TEnum = {',
  12055. ' "0": "red",',
  12056. ' red: 0,',
  12057. ' "1": "green",',
  12058. ' green: 1',
  12059. '};',
  12060. 'rtl.recNewT(this, "TSmallRec", function () {',
  12061. ' this.N = 0;',
  12062. ' this.$eq = function (b) {',
  12063. ' return this.N === b.N;',
  12064. ' };',
  12065. ' this.$assign = function (s) {',
  12066. ' this.N = s.N;',
  12067. ' return this;',
  12068. ' };',
  12069. '});',
  12070. 'rtl.recNewT(this, "TBigRec", function () {',
  12071. ' this.Int = 0;',
  12072. ' this.D = 0.0;',
  12073. ' this.$new = function () {',
  12074. ' var r = Object.create(this);',
  12075. ' r.Arr = [];',
  12076. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12077. ' r.Small = $mod.TSmallRec.$new();',
  12078. ' r.Enums = {};',
  12079. ' return r;',
  12080. ' };',
  12081. ' this.$eq = function (b) {',
  12082. ' 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);',
  12083. ' };',
  12084. ' this.$assign = function (s) {',
  12085. ' this.Int = s.Int;',
  12086. ' this.D = s.D;',
  12087. ' this.Arr = rtl.arrayRef(s.Arr);',
  12088. ' this.Arr2 = s.Arr2.slice(0);',
  12089. ' this.Small.$assign(s.Small);',
  12090. ' this.Enums = rtl.refSet(s.Enums);',
  12091. ' return this;',
  12092. ' };',
  12093. '});',
  12094. 'this.r = this.TBigRec.$new();',
  12095. 'this.s = this.TBigRec.$new();',
  12096. '']),
  12097. LinesToStr([ // $mod.$main
  12098. '$mod.r.$assign($mod.s);',
  12099. '$mod.r.$assign($mod.TBigRec.$new());',
  12100. '$mod.r.$assign($mod.TBigRec.$new());',
  12101. '']));
  12102. end;
  12103. procedure TTestModule.TestRecord_AsParams;
  12104. begin
  12105. StartProgram(false);
  12106. Add([
  12107. 'type',
  12108. ' integer = longint;',
  12109. ' TRecord = record',
  12110. ' i: integer;',
  12111. ' end;',
  12112. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12113. 'var vL: TRecord;',
  12114. 'begin',
  12115. ' vd:=vd;',
  12116. ' vd.i:=vd.i;',
  12117. ' vl:=vc;',
  12118. ' vv:=vv;',
  12119. ' vv.i:=vv.i;',
  12120. ' U:=vl;',
  12121. ' U:=vd;',
  12122. ' U:=vc;',
  12123. ' U:=vv;',
  12124. ' vl:=TRecord(U);',
  12125. ' vd:=TRecord(U);',
  12126. ' vv:=TRecord(U);',
  12127. ' doit(vd,vd,vd,vd);',
  12128. ' doit(vc,vc,vl,vl);',
  12129. ' doit(vv,vv,vv,vv);',
  12130. ' doit(vl,vl,vl,vl);',
  12131. ' TRecord(U).i:=3;',
  12132. 'end;',
  12133. 'var i: TRecord;',
  12134. 'begin',
  12135. ' doit(i,i,i,i);',
  12136. '']);
  12137. ConvertProgram;
  12138. CheckSource('TestRecord_AsParams',
  12139. LinesToStr([ // statements
  12140. 'rtl.recNewT(this, "TRecord", function () {',
  12141. ' this.i = 0;',
  12142. ' this.$eq = function (b) {',
  12143. ' return this.i === b.i;',
  12144. ' };',
  12145. ' this.$assign = function (s) {',
  12146. ' this.i = s.i;',
  12147. ' return this;',
  12148. ' };',
  12149. '});',
  12150. 'this.DoIt = function (vD, vC, vV, U) {',
  12151. ' var vL = $mod.TRecord.$new();',
  12152. ' vD.$assign(vD);',
  12153. ' vD.i = vD.i;',
  12154. ' vL.$assign(vC);',
  12155. ' vV.$assign(vV);',
  12156. ' vV.i = vV.i;',
  12157. ' U.$assign(vL);',
  12158. ' U.$assign(vD);',
  12159. ' U.$assign(vC);',
  12160. ' U.$assign(vV);',
  12161. ' vL.$assign(U);',
  12162. ' vD.$assign(U);',
  12163. ' vV.$assign(U);',
  12164. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12165. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12166. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12167. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12168. ' U.i = 3;',
  12169. '};',
  12170. 'this.i = this.TRecord.$new();'
  12171. ]),
  12172. LinesToStr([
  12173. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12174. '']));
  12175. end;
  12176. procedure TTestModule.TestRecord_ConstRef;
  12177. begin
  12178. StartProgram(false);
  12179. Add([
  12180. 'type TRec = record i: word; end;',
  12181. 'procedure Run(constref a: TRec);',
  12182. 'begin',
  12183. 'end;',
  12184. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12185. 'var l: TRec;',
  12186. 'begin',
  12187. ' Run(l);',
  12188. ' Run(a);',
  12189. ' Run(b);',
  12190. ' Run(c);',
  12191. ' Run(d);',
  12192. ' Run(e);',
  12193. 'end;',
  12194. 'begin',
  12195. '']);
  12196. ConvertProgram;
  12197. CheckResolverUnexpectedHints();
  12198. CheckSource('TestRecord_ConstRef',
  12199. LinesToStr([ // statements
  12200. 'rtl.recNewT(this, "TRec", function () {',
  12201. ' this.i = 0;',
  12202. ' this.$eq = function (b) {',
  12203. ' return this.i === b.i;',
  12204. ' };',
  12205. ' this.$assign = function (s) {',
  12206. ' this.i = s.i;',
  12207. ' return this;',
  12208. ' };',
  12209. '});',
  12210. 'this.Run = function (a) {',
  12211. '};',
  12212. 'this.Fly = function (a, b, c, d, e) {',
  12213. ' var l = $mod.TRec.$new();',
  12214. ' $mod.Run(l);',
  12215. ' $mod.Run(a);',
  12216. ' $mod.Run(b);',
  12217. ' $mod.Run(c);',
  12218. ' $mod.Run(d);',
  12219. ' $mod.Run(e);',
  12220. '};',
  12221. '']),
  12222. LinesToStr([
  12223. '']));
  12224. end;
  12225. procedure TTestModule.TestRecordElement_AsParams;
  12226. begin
  12227. StartProgram(false);
  12228. Add('type');
  12229. Add(' integer = longint;');
  12230. Add(' TRecord = record');
  12231. Add(' i: integer;');
  12232. Add(' end;');
  12233. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12234. Add('var vJ: TRecord;');
  12235. Add('begin');
  12236. Add(' doit(vj.i,vj.i,vj.i);');
  12237. Add('end;');
  12238. Add('var r: TRecord;');
  12239. Add('begin');
  12240. Add(' doit(r.i,r.i,r.i);');
  12241. ConvertProgram;
  12242. CheckSource('TestRecordElement_AsParams',
  12243. LinesToStr([ // statements
  12244. 'rtl.recNewT(this, "TRecord", function () {',
  12245. ' this.i = 0;',
  12246. ' this.$eq = function (b) {',
  12247. ' return this.i === b.i;',
  12248. ' };',
  12249. ' this.$assign = function (s) {',
  12250. ' this.i = s.i;',
  12251. ' return this;',
  12252. ' };',
  12253. '});',
  12254. 'this.DoIt = function (vG,vH,vI) {',
  12255. ' var vJ = $mod.TRecord.$new();',
  12256. ' $mod.DoIt(vJ.i, vJ.i, {',
  12257. ' p: vJ,',
  12258. ' get: function () {',
  12259. ' return this.p.i;',
  12260. ' },',
  12261. ' set: function (v) {',
  12262. ' this.p.i = v;',
  12263. ' }',
  12264. ' });',
  12265. '};',
  12266. 'this.r = this.TRecord.$new();'
  12267. ]),
  12268. LinesToStr([
  12269. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12270. ' p: $mod.r,',
  12271. ' get: function () {',
  12272. ' return this.p.i;',
  12273. ' },',
  12274. ' set: function (v) {',
  12275. ' this.p.i = v;',
  12276. ' }',
  12277. '});'
  12278. ]));
  12279. end;
  12280. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12281. begin
  12282. StartProgram(false);
  12283. Add('type');
  12284. Add(' integer = longint;');
  12285. Add(' TRecord = record');
  12286. Add(' i: integer;');
  12287. Add(' end;');
  12288. Add('function GetRec(vB: integer = 0): TRecord;');
  12289. Add('begin');
  12290. Add('end;');
  12291. Add('procedure DoIt(vG: integer; const vH: integer);');
  12292. Add('begin');
  12293. Add('end;');
  12294. Add('begin');
  12295. Add(' doit(getrec.i,getrec.i);');
  12296. Add(' doit(getrec().i,getrec().i);');
  12297. Add(' doit(getrec(1).i,getrec(2).i);');
  12298. ConvertProgram;
  12299. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12300. LinesToStr([ // statements
  12301. 'rtl.recNewT(this, "TRecord", function () {',
  12302. ' this.i = 0;',
  12303. ' this.$eq = function (b) {',
  12304. ' return this.i === b.i;',
  12305. ' };',
  12306. ' this.$assign = function (s) {',
  12307. ' this.i = s.i;',
  12308. ' return this;',
  12309. ' };',
  12310. '});',
  12311. 'this.GetRec = function (vB) {',
  12312. ' var Result = $mod.TRecord.$new();',
  12313. ' return Result;',
  12314. '};',
  12315. 'this.DoIt = function (vG, vH) {',
  12316. '};',
  12317. '']),
  12318. LinesToStr([
  12319. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12320. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12321. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12322. '']));
  12323. end;
  12324. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12325. begin
  12326. StartProgram(false);
  12327. Add('type');
  12328. Add(' integer = longint;');
  12329. Add(' TRecord = record');
  12330. Add(' i: integer;');
  12331. Add(' end;');
  12332. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12333. Add('begin');
  12334. Add('end;');
  12335. Add('var r: trecord;');
  12336. Add('begin');
  12337. Add(' with r do ');
  12338. Add(' doit(i,i,i);');
  12339. ConvertProgram;
  12340. CheckSource('TestRecordElementFromWith_AsParams',
  12341. LinesToStr([ // statements
  12342. 'rtl.recNewT(this, "TRecord", function () {',
  12343. ' this.i = 0;',
  12344. ' this.$eq = function (b) {',
  12345. ' return this.i === b.i;',
  12346. ' };',
  12347. ' this.$assign = function (s) {',
  12348. ' this.i = s.i;',
  12349. ' return this;',
  12350. ' };',
  12351. '});',
  12352. 'this.DoIt = function (vG,vH,vI) {',
  12353. '};',
  12354. 'this.r = this.TRecord.$new();'
  12355. ]),
  12356. LinesToStr([
  12357. 'var $with = $mod.r;',
  12358. '$mod.DoIt($with.i,$with.i,{',
  12359. ' p: $with,',
  12360. ' get: function () {',
  12361. ' return this.p.i;',
  12362. ' },',
  12363. ' set: function (v) {',
  12364. ' this.p.i = v;',
  12365. ' }',
  12366. '});',
  12367. '']));
  12368. end;
  12369. procedure TTestModule.TestRecord_Equal;
  12370. begin
  12371. StartProgram(false);
  12372. Add('type');
  12373. Add(' integer = longint;');
  12374. Add(' TFlag = (red,blue);');
  12375. Add(' TFlags = set of TFlag;');
  12376. Add(' TProc = procedure;');
  12377. Add(' TRecord = record');
  12378. Add(' i: integer;');
  12379. Add(' Event: TProc;');
  12380. Add(' f: TFlags;');
  12381. Add(' end;');
  12382. Add(' TNested = record');
  12383. Add(' r: TRecord;');
  12384. Add(' end;');
  12385. Add('var');
  12386. Add(' b: boolean;');
  12387. Add(' r,s: trecord;');
  12388. Add('begin');
  12389. Add(' b:=r=s;');
  12390. Add(' b:=r<>s;');
  12391. ConvertProgram;
  12392. CheckSource('TestRecord_Equal',
  12393. LinesToStr([ // statements
  12394. 'this.TFlag = {',
  12395. ' "0": "red",',
  12396. ' red: 0,',
  12397. ' "1": "blue",',
  12398. ' blue: 1',
  12399. '};',
  12400. 'rtl.recNewT(this, "TRecord", function () {',
  12401. ' this.i = 0;',
  12402. ' this.Event = null;',
  12403. ' this.$new = function () {',
  12404. ' var r = Object.create(this);',
  12405. ' r.f = {};',
  12406. ' return r;',
  12407. ' };',
  12408. ' this.$eq = function (b) {',
  12409. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12410. ' };',
  12411. ' this.$assign = function (s) {',
  12412. ' this.i = s.i;',
  12413. ' this.Event = s.Event;',
  12414. ' this.f = rtl.refSet(s.f);',
  12415. ' return this;',
  12416. ' };',
  12417. '});',
  12418. 'rtl.recNewT(this, "TNested", function () {',
  12419. ' this.$new = function () {',
  12420. ' var r = Object.create(this);',
  12421. ' r.r = $mod.TRecord.$new();',
  12422. ' return r;',
  12423. ' };',
  12424. ' this.$eq = function (b) {',
  12425. ' return this.r.$eq(b.r);',
  12426. ' };',
  12427. ' this.$assign = function (s) {',
  12428. ' this.r.$assign(s.r);',
  12429. ' return this;',
  12430. ' };',
  12431. '});',
  12432. 'this.b = false;',
  12433. 'this.r = this.TRecord.$new();',
  12434. 'this.s = this.TRecord.$new();',
  12435. '']),
  12436. LinesToStr([
  12437. '$mod.b = $mod.r.$eq($mod.s);',
  12438. '$mod.b = !$mod.r.$eq($mod.s);',
  12439. '']));
  12440. end;
  12441. procedure TTestModule.TestRecord_JSValue;
  12442. begin
  12443. StartProgram(false);
  12444. Add([
  12445. 'type',
  12446. ' TRecord = record',
  12447. ' i: longint;',
  12448. ' end;',
  12449. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12450. 'begin',
  12451. 'end;',
  12452. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12453. 'begin',
  12454. ' if jsvalue(d) then ;',
  12455. ' if jsvalue(c) then ;',
  12456. ' if jsvalue(v) then ;',
  12457. 'end;',
  12458. 'var',
  12459. ' Jv: jsvalue;',
  12460. ' Rec: trecord;',
  12461. 'begin',
  12462. ' rec:=trecord(jv);',
  12463. ' jv:=rec;',
  12464. ' Fly(rec,rec);',
  12465. ' Fly(@rec,@rec);',
  12466. ' if jsvalue(Rec) then ;',
  12467. ' Run(trecord(jv),trecord(jv),rec);',
  12468. '']);
  12469. ConvertProgram;
  12470. CheckSource('TestRecord_JSValue',
  12471. LinesToStr([ // statements
  12472. 'rtl.recNewT(this, "TRecord", function () {',
  12473. ' this.i = 0;',
  12474. ' this.$eq = function (b) {',
  12475. ' return this.i === b.i;',
  12476. ' };',
  12477. ' this.$assign = function (s) {',
  12478. ' this.i = s.i;',
  12479. ' return this;',
  12480. ' };',
  12481. '});',
  12482. 'this.Fly = function (d, c) {',
  12483. '};',
  12484. 'this.Run = function (d, c, v) {',
  12485. ' if (d) ;',
  12486. ' if (c) ;',
  12487. ' if (v) ;',
  12488. '};',
  12489. 'this.Jv = undefined;',
  12490. 'this.Rec = this.TRecord.$new();',
  12491. '']),
  12492. LinesToStr([
  12493. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12494. '$mod.Jv = $mod.Rec;',
  12495. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12496. '$mod.Fly($mod.Rec, $mod.Rec);',
  12497. 'if ($mod.Rec) ;',
  12498. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12499. '']));
  12500. end;
  12501. procedure TTestModule.TestRecord_VariantFail;
  12502. begin
  12503. StartProgram(false);
  12504. Add([
  12505. 'type',
  12506. ' TRec = record',
  12507. ' case word of',
  12508. ' 0: (b0, b1: Byte);',
  12509. ' 1: (i: word);',
  12510. ' end;',
  12511. 'begin']);
  12512. SetExpectedPasResolverError('Not supported: variant record',
  12513. nNotSupportedX);
  12514. ConvertProgram;
  12515. end;
  12516. procedure TTestModule.TestRecord_FieldArray;
  12517. begin
  12518. StartProgram(false);
  12519. Add([
  12520. 'type',
  12521. ' TArrInt = array[3..4] of longint;',
  12522. ' TArrArrInt = array[3..4] of longint;',
  12523. ' TRec = record',
  12524. ' a: array of longint;',
  12525. ' s: array[1..2] of longint;',
  12526. ' m: array[1..2,3..4] of longint;',
  12527. ' o: TArrArrInt;',
  12528. ' end;',
  12529. 'begin']);
  12530. ConvertProgram;
  12531. CheckSource('TestRecord_FieldArray',
  12532. LinesToStr([ // statements
  12533. 'rtl.recNewT(this, "TRec", function () {',
  12534. ' this.m$a$clone = function (a) {',
  12535. ' var b = [];',
  12536. ' b.length = 2;',
  12537. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12538. ' return b;',
  12539. ' };',
  12540. ' this.$new = function () {',
  12541. ' var r = Object.create(this);',
  12542. ' r.a = [];',
  12543. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12544. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12545. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12546. ' return r;',
  12547. ' };',
  12548. ' this.$eq = function (b) {',
  12549. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12550. ' };',
  12551. ' this.$assign = function (s) {',
  12552. ' this.a = rtl.arrayRef(s.a);',
  12553. ' this.s = s.s.slice(0);',
  12554. ' this.m = this.m$a$clone(s.m);',
  12555. ' this.o = s.o.slice(0);',
  12556. ' return this;',
  12557. ' };',
  12558. '});',
  12559. '']),
  12560. LinesToStr([ // $mod.$main
  12561. '']));
  12562. end;
  12563. procedure TTestModule.TestRecord_Const;
  12564. begin
  12565. StartProgram(false);
  12566. Add([
  12567. 'type',
  12568. ' TArrInt = array[3..4] of longint;',
  12569. ' TPoint = record x,y: longint; end;',
  12570. ' TRec = record',
  12571. ' i: longint;',
  12572. ' a: array of longint;',
  12573. ' s: array[1..2] of longint;',
  12574. ' m: array[1..2,3..4] of longint;',
  12575. ' p: TPoint;',
  12576. ' end;',
  12577. ' TPoints = array of TPoint;',
  12578. 'const',
  12579. ' r: TRec = (',
  12580. ' i:1;',
  12581. ' a:(2,3);',
  12582. ' s:(4,5);',
  12583. ' m:( (11,12), (13,14) );',
  12584. ' p: (x:21; y:22)',
  12585. ' );',
  12586. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12587. 'begin']);
  12588. ConvertProgram;
  12589. CheckSource('TestRecord_Const',
  12590. LinesToStr([ // statements
  12591. 'rtl.recNewT(this, "TPoint", function () {',
  12592. ' this.x = 0;',
  12593. ' this.y = 0;',
  12594. ' this.$eq = function (b) {',
  12595. ' return (this.x === b.x) && (this.y === b.y);',
  12596. ' };',
  12597. ' this.$assign = function (s) {',
  12598. ' this.x = s.x;',
  12599. ' this.y = s.y;',
  12600. ' return this;',
  12601. ' };',
  12602. '});',
  12603. 'rtl.recNewT(this, "TRec", function () {',
  12604. ' this.i = 0;',
  12605. ' this.m$a$clone = function (a) {',
  12606. ' var b = [];',
  12607. ' b.length = 2;',
  12608. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12609. ' return b;',
  12610. ' };',
  12611. ' this.$new = function () {',
  12612. ' var r = Object.create(this);',
  12613. ' r.a = [];',
  12614. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12615. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12616. ' r.p = $mod.TPoint.$new();',
  12617. ' return r;',
  12618. ' };',
  12619. ' this.$eq = function (b) {',
  12620. ' 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);',
  12621. ' };',
  12622. ' this.$assign = function (s) {',
  12623. ' this.i = s.i;',
  12624. ' this.a = rtl.arrayRef(s.a);',
  12625. ' this.s = s.s.slice(0);',
  12626. ' this.m = this.m$a$clone(s.m);',
  12627. ' this.p.$assign(s.p);',
  12628. ' return this;',
  12629. ' };',
  12630. '});',
  12631. 'this.r = this.TRec.$clone({',
  12632. ' i: 1,',
  12633. ' a: [2, 3],',
  12634. ' s: [4, 5],',
  12635. ' m: [[11, 12], [13, 14]],',
  12636. ' p: this.TPoint.$clone({',
  12637. ' x: 21,',
  12638. ' y: 22',
  12639. ' })',
  12640. '});',
  12641. 'this.p = [this.TPoint.$clone({',
  12642. ' x: 1,',
  12643. ' y: 2',
  12644. '}), this.TPoint.$clone({',
  12645. ' x: 3,',
  12646. ' y: 4',
  12647. '})];',
  12648. '']),
  12649. LinesToStr([ // $mod.$main
  12650. '']));
  12651. end;
  12652. procedure TTestModule.TestRecord_TypecastFail;
  12653. begin
  12654. StartProgram(false);
  12655. Add([
  12656. 'type',
  12657. ' TPoint = record x,y: longint; end;',
  12658. ' TRec = record l: longint end;',
  12659. 'var p: TPoint;',
  12660. 'begin',
  12661. ' if TRec(p).l=2 then ;']);
  12662. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12663. nIllegalTypeConversionTo);
  12664. ConvertProgram;
  12665. end;
  12666. procedure TTestModule.TestRecord_InFunction;
  12667. begin
  12668. StartProgram(false);
  12669. Add([
  12670. 'var TPoint: longint = 3;',
  12671. 'procedure DoIt;',
  12672. 'type',
  12673. ' TPoint = record x,y: longint; end;',
  12674. ' TPoints = array of TPoint;',
  12675. 'var',
  12676. ' r: TPoint;',
  12677. ' p: TPoints;',
  12678. 'begin',
  12679. ' SetLength(p,2);',
  12680. 'end;',
  12681. 'begin']);
  12682. ConvertProgram;
  12683. CheckSource('TestRecord_InFunction',
  12684. LinesToStr([ // statements
  12685. 'this.TPoint = 3;',
  12686. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12687. ' this.x = 0;',
  12688. ' this.y = 0;',
  12689. ' this.$eq = function (b) {',
  12690. ' return (this.x === b.x) && (this.y === b.y);',
  12691. ' };',
  12692. ' this.$assign = function (s) {',
  12693. ' this.x = s.x;',
  12694. ' this.y = s.y;',
  12695. ' return this;',
  12696. ' };',
  12697. '});',
  12698. 'this.DoIt = function () {',
  12699. ' var r = TPoint$1.$new();',
  12700. ' var p = [];',
  12701. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12702. '};',
  12703. '']),
  12704. LinesToStr([ // $mod.$main
  12705. '']));
  12706. end;
  12707. procedure TTestModule.TestRecordAnonym_Field;
  12708. begin
  12709. StartProgram(false);
  12710. Add(['',
  12711. 'var Rec: record',
  12712. ' Bold: longint;',
  12713. ' end;',
  12714. 'begin',
  12715. ' rec.bold:=123;',
  12716. ' rec.bold:=rec.bold+7;',
  12717. '']);
  12718. ConvertProgram;
  12719. CheckSource('TestRecordAnonym_Field',
  12720. LinesToStr([ // statements
  12721. 'rtl.recNewT(this, "Rec$a", function () {',
  12722. ' this.Bold = 0;',
  12723. ' this.$eq = function (b) {',
  12724. ' return this.Bold === b.Bold;',
  12725. ' };',
  12726. ' this.$assign = function (s) {',
  12727. ' this.Bold = s.Bold;',
  12728. ' return this;',
  12729. ' };',
  12730. '});',
  12731. 'this.Rec = this.Rec$a.$new();',
  12732. '']),
  12733. LinesToStr([ // $mod.$main
  12734. '$mod.Rec.Bold = 123;',
  12735. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12736. '']));
  12737. end;
  12738. procedure TTestModule.TestRecordAnonym_Assign;
  12739. begin
  12740. StartProgram(false);
  12741. Add(['',
  12742. 'var S,T: record',
  12743. ' Bold: longint;',
  12744. ' end;',
  12745. ' b: boolean;',
  12746. 'begin',
  12747. ' S:=T;',
  12748. ' b:=s=t;',
  12749. '']);
  12750. ConvertProgram;
  12751. CheckSource('TestRecordAnonym_Assign',
  12752. LinesToStr([ // statements
  12753. 'rtl.recNewT(this, "T$a", function () {',
  12754. ' this.Bold = 0;',
  12755. ' this.$eq = function (b) {',
  12756. ' return this.Bold === b.Bold;',
  12757. ' };',
  12758. ' this.$assign = function (s) {',
  12759. ' this.Bold = s.Bold;',
  12760. ' return this;',
  12761. ' };',
  12762. '});',
  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.b = $mod.S.$eq($mod.T);',
  12770. '']));
  12771. end;
  12772. procedure TTestModule.TestRecordAnonym_Nested;
  12773. begin
  12774. StartProgram(false);
  12775. Add(['',
  12776. 'var S,T: record',
  12777. ' Bold: longint;',
  12778. ' Sub: record',
  12779. ' Color: word;',
  12780. ' end;',
  12781. ' end;',
  12782. ' b: boolean;',
  12783. 'begin',
  12784. ' S:=T;',
  12785. ' S.Sub:=T.Sub;',
  12786. ' S.Sub.Color:=T.Sub.Color+3;',
  12787. ' b:=s=t;',
  12788. ' b:=s.Sub=t.Sub;',
  12789. '']);
  12790. ConvertProgram;
  12791. CheckSource('TestRecordAnonym_Nested',
  12792. LinesToStr([ // statements
  12793. 'rtl.recNewT(this, "T$a", function () {',
  12794. ' this.Bold = 0;',
  12795. ' rtl.recNewT(this, "Sub$a", function () {',
  12796. ' this.Color = 0;',
  12797. ' this.$eq = function (b) {',
  12798. ' return this.Color === b.Color;',
  12799. ' };',
  12800. ' this.$assign = function (s) {',
  12801. ' this.Color = s.Color;',
  12802. ' return this;',
  12803. ' };',
  12804. ' });',
  12805. ' this.$new = function () {',
  12806. ' var r = Object.create(this);',
  12807. ' r.Sub = this.Sub$a.$new();',
  12808. ' return r;',
  12809. ' };',
  12810. ' this.$eq = function (b) {',
  12811. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12812. ' };',
  12813. ' this.$assign = function (s) {',
  12814. ' this.Bold = s.Bold;',
  12815. ' this.Sub.$assign(s.Sub);',
  12816. ' return this;',
  12817. ' };',
  12818. '}, true);',
  12819. 'this.S = this.T$a.$new();',
  12820. 'this.T = this.T$a.$new();',
  12821. 'this.b = false;',
  12822. '']),
  12823. LinesToStr([ // $mod.$main
  12824. '$mod.S.$assign($mod.T);',
  12825. '$mod.S.Sub.$assign($mod.T.Sub);',
  12826. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12827. '$mod.b = $mod.S.$eq($mod.T);',
  12828. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12829. '']));
  12830. end;
  12831. procedure TTestModule.TestRecordAnonym_Const;
  12832. begin
  12833. StartProgram(false);
  12834. Add(['',
  12835. 'var T: record',
  12836. ' Bold: longint;',
  12837. ' Sub: record',
  12838. ' Color: word;',
  12839. ' end;',
  12840. ' end = (Bold: 2; Sub: (Color: 3));',
  12841. 'begin',
  12842. '']);
  12843. ConvertProgram;
  12844. CheckSource('TestRecordAnonym_Const',
  12845. LinesToStr([ // statements
  12846. 'rtl.recNewT(this, "T$a", function () {',
  12847. ' this.Bold = 0;',
  12848. ' rtl.recNewT(this, "Sub$a", function () {',
  12849. ' this.Color = 0;',
  12850. ' this.$eq = function (b) {',
  12851. ' return this.Color === b.Color;',
  12852. ' };',
  12853. ' this.$assign = function (s) {',
  12854. ' this.Color = s.Color;',
  12855. ' return this;',
  12856. ' };',
  12857. ' });',
  12858. ' this.$new = function () {',
  12859. ' var r = Object.create(this);',
  12860. ' r.Sub = this.Sub$a.$new();',
  12861. ' return r;',
  12862. ' };',
  12863. ' this.$eq = function (b) {',
  12864. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12865. ' };',
  12866. ' this.$assign = function (s) {',
  12867. ' this.Bold = s.Bold;',
  12868. ' this.Sub.$assign(s.Sub);',
  12869. ' return this;',
  12870. ' };',
  12871. '}, true);',
  12872. 'this.T = this.T$a.$clone({',
  12873. ' Bold: 2,',
  12874. ' Sub: this.T$a.Sub$a.$clone({',
  12875. ' Color: 3',
  12876. ' })',
  12877. '});',
  12878. '']),
  12879. LinesToStr([ // $mod.$main
  12880. '']));
  12881. end;
  12882. procedure TTestModule.TestRecordAnonym_InFunction;
  12883. begin
  12884. StartProgram(false);
  12885. Add(['',
  12886. 'procedure Fly;',
  12887. 'var T: record',
  12888. ' Bold: longint;',
  12889. ' Sub: record',
  12890. ' Color: word;',
  12891. ' end;',
  12892. ' end = (Bold: 2; Sub: (Color: 3));',
  12893. 'begin',
  12894. 'end;',
  12895. 'begin',
  12896. '']);
  12897. ConvertProgram;
  12898. CheckSource('TestRecordAnonym_InFunction',
  12899. LinesToStr([ // statements
  12900. 'var T$a = rtl.recNewT(null, "", function () {',
  12901. ' this.Bold = 0;',
  12902. ' rtl.recNewT(this, "Sub$a", function () {',
  12903. ' this.Color = 0;',
  12904. ' this.$eq = function (b) {',
  12905. ' return this.Color === b.Color;',
  12906. ' };',
  12907. ' this.$assign = function (s) {',
  12908. ' this.Color = s.Color;',
  12909. ' return this;',
  12910. ' };',
  12911. ' });',
  12912. ' this.$new = function () {',
  12913. ' var r = Object.create(this);',
  12914. ' r.Sub = this.Sub$a.$new();',
  12915. ' return r;',
  12916. ' };',
  12917. ' this.$eq = function (b) {',
  12918. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12919. ' };',
  12920. ' this.$assign = function (s) {',
  12921. ' this.Bold = s.Bold;',
  12922. ' this.Sub.$assign(s.Sub);',
  12923. ' return this;',
  12924. ' };',
  12925. '}, true);',
  12926. 'this.Fly = function () {',
  12927. ' var T = T$a.$clone({',
  12928. ' Bold: 2,',
  12929. ' Sub: T$a.Sub$a.$clone({',
  12930. ' Color: 3',
  12931. ' })',
  12932. ' });',
  12933. '};',
  12934. '']),
  12935. LinesToStr([ // $mod.$main
  12936. '']));
  12937. end;
  12938. procedure TTestModule.TestAdvRecord_Function;
  12939. begin
  12940. StartProgram(false);
  12941. Parser.Options:=Parser.Options+[po_cassignments];
  12942. Add([
  12943. '{$modeswitch AdvancedRecords}',
  12944. 'type',
  12945. ' TPoint = record',
  12946. ' x,y: word;',
  12947. ' function Add(const apt: TPoint): TPoint;',
  12948. ' end;',
  12949. 'function TPoint.Add(const apt: TPoint): TPoint;',
  12950. 'begin',
  12951. ' Result:=Self;',
  12952. ' Result.x+=apt.x;',
  12953. ' Result.y:=Result.y+apt.y;',
  12954. ' Self:=apt;',
  12955. 'end;',
  12956. 'var p,q: TPoint;',
  12957. 'begin',
  12958. ' p.add(q);',
  12959. ' p:=default(TPoint);',
  12960. ' p:=q;',
  12961. '']);
  12962. ConvertProgram;
  12963. CheckSource('TestAdvRecord_Function',
  12964. LinesToStr([ // statements
  12965. 'rtl.recNewT(this, "TPoint", function () {',
  12966. ' this.x = 0;',
  12967. ' this.y = 0;',
  12968. ' this.$eq = function (b) {',
  12969. ' return (this.x === b.x) && (this.y === b.y);',
  12970. ' };',
  12971. ' this.$assign = function (s) {',
  12972. ' this.x = s.x;',
  12973. ' this.y = s.y;',
  12974. ' return this;',
  12975. ' };',
  12976. ' this.Add = function (apt) {',
  12977. ' var Result = $mod.TPoint.$new();',
  12978. ' Result.$assign(this);',
  12979. ' Result.x += apt.x;',
  12980. ' Result.y = Result.y + apt.y;',
  12981. ' this.$assign(apt);',
  12982. ' return Result;',
  12983. ' };',
  12984. '});',
  12985. 'this.p = this.TPoint.$new();',
  12986. 'this.q = this.TPoint.$new();',
  12987. '']),
  12988. LinesToStr([ // $mod.$main
  12989. '$mod.p.Add($mod.q);',
  12990. '$mod.p.$assign($mod.TPoint.$new());',
  12991. '$mod.p.$assign($mod.q);',
  12992. '']));
  12993. end;
  12994. procedure TTestModule.TestAdvRecord_Property;
  12995. begin
  12996. StartProgram(false);
  12997. Add([
  12998. '{$modeswitch AdvancedRecords}',
  12999. 'type',
  13000. ' TPoint = record',
  13001. ' x,y: word;',
  13002. ' strict private',
  13003. ' function GetSize: longword;',
  13004. ' procedure SetSize(Value: longword);',
  13005. ' public',
  13006. ' property Size: longword read GetSize write SetSize;',
  13007. ' property Left: word read x write y;',
  13008. ' end;',
  13009. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13010. 'function TPoint.GetSize: longword;',
  13011. 'begin',
  13012. ' x:=y;',
  13013. ' Size:=Size;',
  13014. ' Left:=Left;',
  13015. 'end;',
  13016. 'procedure TPoint.SetSize(Value: longword);',
  13017. 'begin',
  13018. 'end;',
  13019. 'var p,q: TPoint;',
  13020. 'begin',
  13021. ' p.Size:=q.Size;',
  13022. ' p.Left:=q.Left;',
  13023. '']);
  13024. ConvertProgram;
  13025. CheckSource('TestAdvRecord_Property',
  13026. LinesToStr([ // statements
  13027. 'rtl.recNewT(this, "TPoint", function () {',
  13028. ' this.x = 0;',
  13029. ' this.y = 0;',
  13030. ' this.$eq = function (b) {',
  13031. ' return (this.x === b.x) && (this.y === b.y);',
  13032. ' };',
  13033. ' this.$assign = function (s) {',
  13034. ' this.x = s.x;',
  13035. ' this.y = s.y;',
  13036. ' return this;',
  13037. ' };',
  13038. ' this.GetSize = function () {',
  13039. ' var Result = 0;',
  13040. ' this.x = this.y;',
  13041. ' this.SetSize(this.GetSize());',
  13042. ' this.y = this.x;',
  13043. ' return Result;',
  13044. ' };',
  13045. ' this.SetSize = function (Value) {',
  13046. ' };',
  13047. '});',
  13048. 'this.SetSize = function (Value) {',
  13049. '};',
  13050. 'this.p = this.TPoint.$new();',
  13051. 'this.q = this.TPoint.$new();',
  13052. '']),
  13053. LinesToStr([ // $mod.$main
  13054. '$mod.p.SetSize($mod.q.GetSize());',
  13055. '$mod.p.y = $mod.q.x;',
  13056. '']));
  13057. end;
  13058. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13059. begin
  13060. StartProgram(false);
  13061. Add([
  13062. '{$modeswitch AdvancedRecords}',
  13063. 'type',
  13064. ' TPoint = record',
  13065. ' strict private',
  13066. ' function GetItems(Index: word): word;',
  13067. ' procedure SetItems(Index: word; Value: word);',
  13068. ' public',
  13069. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13070. ' end;',
  13071. 'function TPoint.GetItems(Index: word): word;',
  13072. 'begin',
  13073. ' Items[index]:=Items[index];',
  13074. ' self.Items[index]:=self.Items[index];',
  13075. 'end;',
  13076. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13077. 'begin',
  13078. 'end;',
  13079. 'var p: TPoint;',
  13080. 'begin',
  13081. ' p[1]:=p[2];',
  13082. ' p.Items[3]:=p.Items[4];',
  13083. '']);
  13084. ConvertProgram;
  13085. CheckSource('TestAdvRecord_PropertyDefault',
  13086. LinesToStr([ // statements
  13087. 'rtl.recNewT(this, "TPoint", function () {',
  13088. ' this.$eq = function (b) {',
  13089. ' return true;',
  13090. ' };',
  13091. ' this.$assign = function (s) {',
  13092. ' return this;',
  13093. ' };',
  13094. ' this.GetItems = function (Index) {',
  13095. ' var Result = 0;',
  13096. ' this.SetItems(Index, this.GetItems(Index));',
  13097. ' this.SetItems(Index, this.GetItems(Index));',
  13098. ' return Result;',
  13099. ' };',
  13100. ' this.SetItems = function (Index, Value) {',
  13101. ' };',
  13102. '});',
  13103. 'this.p = this.TPoint.$new();',
  13104. '']),
  13105. LinesToStr([ // $mod.$main
  13106. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13107. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13108. '']));
  13109. end;
  13110. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13111. begin
  13112. StartProgram(false);
  13113. Add([
  13114. '{$modeswitch AdvancedRecords}',
  13115. 'type',
  13116. ' TRec = record',
  13117. ' class var',
  13118. ' Fx: longint;',
  13119. ' Fy: longint;',
  13120. ' class function GetInt: longint; static;',
  13121. ' class procedure SetInt(Value: longint); static;',
  13122. ' class procedure DoIt; static;',
  13123. ' class property IntA: longint read Fx write Fy;',
  13124. ' class property IntB: longint read GetInt write SetInt;',
  13125. ' end;',
  13126. 'class function trec.getint: longint;',
  13127. 'begin',
  13128. ' result:=fx;',
  13129. 'end;',
  13130. 'class procedure trec.setint(value: longint);',
  13131. 'begin',
  13132. 'end;',
  13133. 'class procedure trec.doit;',
  13134. 'begin',
  13135. ' IntA:=IntA+1;',
  13136. ' IntB:=IntB+1;',
  13137. 'end;',
  13138. 'var r: trec;',
  13139. 'begin',
  13140. ' trec.inta:=trec.inta+1;',
  13141. ' if trec.intb=2 then;',
  13142. ' trec.intb:=trec.intb+2;',
  13143. ' trec.setint(trec.inta);',
  13144. ' r.inta:=r.inta+1;',
  13145. ' if r.intb=2 then;',
  13146. ' r.intb:=r.intb+2;',
  13147. ' r.setint(r.inta);']);
  13148. ConvertProgram;
  13149. CheckSource('TestAdvRecord_Property_ClassMethod',
  13150. LinesToStr([ // statements
  13151. 'rtl.recNewT(this, "TRec", function () {',
  13152. ' this.Fx = 0;',
  13153. ' this.Fy = 0;',
  13154. ' this.$eq = function (b) {',
  13155. ' return true;',
  13156. ' };',
  13157. ' this.$assign = function (s) {',
  13158. ' return this;',
  13159. ' };',
  13160. ' this.GetInt = function () {',
  13161. ' var Result = 0;',
  13162. ' Result = $mod.TRec.Fx;',
  13163. ' return Result;',
  13164. ' };',
  13165. ' this.SetInt = function (Value) {',
  13166. ' };',
  13167. ' this.DoIt = function () {',
  13168. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13169. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13170. ' };',
  13171. '}, true);',
  13172. 'this.r = this.TRec.$new();',
  13173. '']),
  13174. LinesToStr([ // $mod.$main
  13175. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13176. 'if ($mod.TRec.GetInt() === 2) ;',
  13177. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13178. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13179. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13180. 'if ($mod.TRec.GetInt() === 2) ;',
  13181. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13182. '$mod.TRec.SetInt($mod.r.Fx);',
  13183. '']));
  13184. end;
  13185. procedure TTestModule.TestAdvRecord_Const;
  13186. begin
  13187. StartProgram(false);
  13188. Add([
  13189. '{$modeswitch AdvancedRecords}',
  13190. 'type',
  13191. ' TArrInt = array[3..4] of longint;',
  13192. ' TPoint = record',
  13193. ' x,y: longint;',
  13194. ' class var Count: nativeint;',
  13195. ' end;',
  13196. ' TRec = record',
  13197. ' i: longint;',
  13198. ' a: array of longint;',
  13199. ' s: array[1..2] of longint;',
  13200. ' m: array[1..2,3..4] of longint;',
  13201. ' p: TPoint;',
  13202. ' end;',
  13203. ' TPoints = array of TPoint;',
  13204. 'const',
  13205. ' r: TRec = (',
  13206. ' i:1;',
  13207. ' a:(2,3);',
  13208. ' s:(4,5);',
  13209. ' m:( (11,12), (13,14) );',
  13210. ' p: (x:21)',
  13211. ' );',
  13212. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13213. 'begin']);
  13214. ConvertProgram;
  13215. CheckSource('TestAdvRecord_Const',
  13216. LinesToStr([ // statements
  13217. 'rtl.recNewT(this, "TPoint", function () {',
  13218. ' this.x = 0;',
  13219. ' this.y = 0;',
  13220. ' this.Count = 0;',
  13221. ' this.$eq = function (b) {',
  13222. ' return (this.x === b.x) && (this.y === b.y);',
  13223. ' };',
  13224. ' this.$assign = function (s) {',
  13225. ' this.x = s.x;',
  13226. ' this.y = s.y;',
  13227. ' return this;',
  13228. ' };',
  13229. '}, true);',
  13230. 'rtl.recNewT(this, "TRec", function () {',
  13231. ' this.i = 0;',
  13232. ' this.m$a$clone = function (a) {',
  13233. ' var b = [];',
  13234. ' b.length = 2;',
  13235. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13236. ' return b;',
  13237. ' };',
  13238. ' this.$new = function () {',
  13239. ' var r = Object.create(this);',
  13240. ' r.a = [];',
  13241. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13242. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13243. ' r.p = $mod.TPoint.$new();',
  13244. ' return r;',
  13245. ' };',
  13246. ' this.$eq = function (b) {',
  13247. ' 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);',
  13248. ' };',
  13249. ' this.$assign = function (s) {',
  13250. ' this.i = s.i;',
  13251. ' this.a = rtl.arrayRef(s.a);',
  13252. ' this.s = s.s.slice(0);',
  13253. ' this.m = this.m$a$clone(s.m);',
  13254. ' this.p.$assign(s.p);',
  13255. ' return this;',
  13256. ' };',
  13257. '});',
  13258. 'this.r = this.TRec.$clone({',
  13259. ' i: 1,',
  13260. ' a: [2, 3],',
  13261. ' s: [4, 5],',
  13262. ' m: [[11, 12], [13, 14]],',
  13263. ' p: this.TPoint.$clone({',
  13264. ' x: 21,',
  13265. ' y: 0',
  13266. ' })',
  13267. '});',
  13268. 'this.p = [this.TPoint.$clone({',
  13269. ' x: 1,',
  13270. ' y: 2',
  13271. '}), this.TPoint.$clone({',
  13272. ' x: 3,',
  13273. ' y: 4',
  13274. '})];',
  13275. '']),
  13276. LinesToStr([ // $mod.$main
  13277. '']));
  13278. end;
  13279. procedure TTestModule.TestAdvRecord_ExternalField;
  13280. begin
  13281. StartProgram(false);
  13282. Add([
  13283. '{$modeswitch AdvancedRecords}',
  13284. '{$modeswitch externalclass}',
  13285. 'type',
  13286. ' TCar = record',
  13287. ' public',
  13288. ' Intern: longint external name ''$Intern'';',
  13289. ' Intern2: longint external name ''$Intern2'';',
  13290. ' Bracket: longint external name ''["A B"]'';',
  13291. ' procedure DoIt;',
  13292. ' end;',
  13293. 'procedure tcar.doit;',
  13294. 'begin',
  13295. ' Intern:=Intern+1;',
  13296. ' Intern2:=Intern2+2;',
  13297. ' Bracket:=Bracket+3;',
  13298. 'end;',
  13299. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13300. 'begin',
  13301. ' Rec.intern:=Rec.intern+1;',
  13302. ' Rec.intern2:=Rec.intern2+2;',
  13303. ' Rec.Bracket:=Rec.Bracket+3;',
  13304. ' with Rec do begin',
  13305. ' intern:=intern+1;',
  13306. ' intern2:=intern2+2;',
  13307. ' Bracket:=Bracket+3;',
  13308. ' end;']);
  13309. ConvertProgram;
  13310. CheckSource('TestAdvRecord_ExternalField',
  13311. LinesToStr([ // statements
  13312. 'rtl.recNewT(this, "TCar", function () {',
  13313. ' this.$eq = function (b) {',
  13314. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13315. ' };',
  13316. ' this.$assign = function (s) {',
  13317. ' this.$Intern = s.$Intern;',
  13318. ' this.$Intern2 = s.$Intern2;',
  13319. ' this["A B"] = s["A B"];',
  13320. ' return this;',
  13321. ' };',
  13322. ' this.DoIt = function () {',
  13323. ' this.$Intern = this.$Intern + 1;',
  13324. ' this.$Intern2 = this.$Intern2 + 2;',
  13325. ' this["A B"] = this["A B"] + 3;',
  13326. ' };',
  13327. '});',
  13328. 'this.Rec = this.TCar.$clone({',
  13329. ' $Intern: 11,',
  13330. ' $Intern2: 12,',
  13331. ' "A B": 13',
  13332. '});',
  13333. '']),
  13334. LinesToStr([ // $mod.$main
  13335. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13336. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13337. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13338. 'var $with = $mod.Rec;',
  13339. '$with.$Intern = $with.$Intern + 1;',
  13340. '$with.$Intern2 = $with.$Intern2 + 2;',
  13341. '$with["A B"] = $with["A B"] + 3;',
  13342. '']));
  13343. end;
  13344. procedure TTestModule.TestAdvRecord_SubRecord;
  13345. begin
  13346. StartProgram(false);
  13347. Add([
  13348. '{$modeswitch AdvancedRecords}',
  13349. 'type',
  13350. ' TRec = record',
  13351. ' type',
  13352. ' TPoint = record',
  13353. ' x,y: longint;',
  13354. ' class var Count: nativeint;',
  13355. ' procedure DoIt;',
  13356. ' class procedure DoThat; static;',
  13357. ' end;',
  13358. ' var',
  13359. ' i: longint;',
  13360. ' p: TPoint;',
  13361. ' procedure DoSome;',
  13362. ' end;',
  13363. 'const',
  13364. ' r: TRec = (',
  13365. ' i:1;',
  13366. ' p: (x:21;y:22)',
  13367. ' );',
  13368. 'procedure TRec.DoSome;',
  13369. 'begin',
  13370. ' p.x:=p.y+1;',
  13371. ' p.Count:=p.Count+2;',
  13372. 'end;',
  13373. 'procedure TRec.TPoint.DoIt;',
  13374. 'begin',
  13375. ' Count:=Count+3;',
  13376. 'end;',
  13377. 'class procedure TRec.TPoint.DoThat;',
  13378. 'begin',
  13379. ' Count:=Count+4;',
  13380. 'end;',
  13381. 'begin']);
  13382. ConvertProgram;
  13383. CheckSource('TestAdvRecord_SubRecord',
  13384. LinesToStr([ // statements
  13385. 'rtl.recNewT(this, "TRec", function () {',
  13386. ' rtl.recNewT(this, "TPoint", function () {',
  13387. ' this.x = 0;',
  13388. ' this.y = 0;',
  13389. ' this.Count = 0;',
  13390. ' this.$eq = function (b) {',
  13391. ' return (this.x === b.x) && (this.y === b.y);',
  13392. ' };',
  13393. ' this.$assign = function (s) {',
  13394. ' this.x = s.x;',
  13395. ' this.y = s.y;',
  13396. ' return this;',
  13397. ' };',
  13398. ' this.DoIt = function () {',
  13399. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13400. ' };',
  13401. ' this.DoThat = function () {',
  13402. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13403. ' };',
  13404. ' }, true);',
  13405. ' this.i = 0;',
  13406. ' this.$new = function () {',
  13407. ' var r = Object.create(this);',
  13408. ' r.p = this.TPoint.$new();',
  13409. ' return r;',
  13410. ' };',
  13411. ' this.$eq = function (b) {',
  13412. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13413. ' };',
  13414. ' this.$assign = function (s) {',
  13415. ' this.i = s.i;',
  13416. ' this.p.$assign(s.p);',
  13417. ' return this;',
  13418. ' };',
  13419. ' this.DoSome = function () {',
  13420. ' this.p.x = this.p.y + 1;',
  13421. ' this.TPoint.Count = this.p.Count + 2;',
  13422. ' };',
  13423. '}, true);',
  13424. 'this.r = this.TRec.$clone({',
  13425. ' i: 1,',
  13426. ' p: this.TRec.TPoint.$clone({',
  13427. ' x: 21,',
  13428. ' y: 22',
  13429. ' })',
  13430. '});',
  13431. '']),
  13432. LinesToStr([ // $mod.$main
  13433. '']));
  13434. end;
  13435. procedure TTestModule.TestAdvRecord_SubClass;
  13436. begin
  13437. StartProgram(false);
  13438. Add([
  13439. '{$modeswitch AdvancedRecords}',
  13440. 'type',
  13441. ' TObject = class end;',
  13442. ' TPoint = record',
  13443. ' type',
  13444. ' TBird = class',
  13445. ' procedure DoIt;',
  13446. ' class procedure Glob;',
  13447. ' end;',
  13448. ' procedure DoIt(b: TBird);',
  13449. ' end;',
  13450. 'procedure TPoint.TBird.DoIt;',
  13451. 'begin',
  13452. ' doit;',
  13453. ' self.doit;',
  13454. ' glob;',
  13455. ' self.glob;',
  13456. 'end;',
  13457. 'class procedure TPoint.TBird.Glob;',
  13458. 'begin',
  13459. ' glob;',
  13460. ' self.glob;',
  13461. 'end;',
  13462. 'procedure TPoint.DoIt(b: TBird);',
  13463. 'begin',
  13464. ' b.doit;',
  13465. ' b.glob;',
  13466. ' TBird.glob;',
  13467. 'end;',
  13468. 'begin',
  13469. '']);
  13470. ConvertProgram;
  13471. CheckSource('TestAdvRecord_SubClass',
  13472. LinesToStr([ // statements
  13473. 'rtl.createClass(this, "TObject", null, function () {',
  13474. ' this.$init = function () {',
  13475. ' };',
  13476. ' this.$final = function () {',
  13477. ' };',
  13478. '});',
  13479. 'rtl.recNewT(this, "TPoint", function () {',
  13480. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13481. ' this.DoIt = function () {',
  13482. ' this.DoIt();',
  13483. ' this.DoIt();',
  13484. ' this.$class.Glob();',
  13485. ' this.$class.Glob();',
  13486. ' };',
  13487. ' this.Glob = function () {',
  13488. ' this.Glob();',
  13489. ' this.Glob();',
  13490. ' };',
  13491. ' }, "TPoint.TBird");',
  13492. ' this.$eq = function (b) {',
  13493. ' return true;',
  13494. ' };',
  13495. ' this.$assign = function (s) {',
  13496. ' return this;',
  13497. ' };',
  13498. ' this.DoIt = function (b) {',
  13499. ' b.DoIt();',
  13500. ' b.$class.Glob();',
  13501. ' this.TBird.Glob();',
  13502. ' };',
  13503. '}, true);',
  13504. '']),
  13505. LinesToStr([ // $mod.$main
  13506. '']));
  13507. end;
  13508. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13509. begin
  13510. StartProgram(false);
  13511. Add([
  13512. '{$modeswitch AdvancedRecords}',
  13513. 'type',
  13514. ' IUnknown = interface end;',
  13515. ' TPoint = record',
  13516. ' type IBird = interface end;',
  13517. ' end;',
  13518. 'begin',
  13519. '']);
  13520. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13521. nNotYetImplemented);
  13522. ParseProgram;
  13523. end;
  13524. procedure TTestModule.TestAdvRecord_Constructor;
  13525. begin
  13526. StartProgram(false);
  13527. Add([
  13528. '{$modeswitch AdvancedRecords}',
  13529. 'type',
  13530. ' TPoint = record',
  13531. ' x,y: longint;',
  13532. ' class procedure Run(w: longint = 13); static;',
  13533. ' constructor Create(ax: longint; ay: longint = -1);',
  13534. ' end;',
  13535. 'class procedure tpoint.run(w: longint);',
  13536. 'begin',
  13537. ' run;',
  13538. ' run();',
  13539. 'end;',
  13540. 'constructor tpoint.create(ax,ay: longint);',
  13541. 'begin',
  13542. ' x:=ax;',
  13543. ' self.y:=ay;',
  13544. ' run;',
  13545. ' run(ax);',
  13546. 'end;',
  13547. 'var r: TPoint;',
  13548. 'begin',
  13549. ' r:=TPoint.Create(1,2);',
  13550. ' with TPoint do r:=Create(1,2);',
  13551. ' r.Create(3);',
  13552. ' r:=r.Create(4);',
  13553. '']);
  13554. ConvertProgram;
  13555. CheckSource('TestAdvRecord_Constructor',
  13556. LinesToStr([ // statements
  13557. 'rtl.recNewT(this, "TPoint", function () {',
  13558. ' this.x = 0;',
  13559. ' this.y = 0;',
  13560. ' this.$eq = function (b) {',
  13561. ' return (this.x === b.x) && (this.y === b.y);',
  13562. ' };',
  13563. ' this.$assign = function (s) {',
  13564. ' this.x = s.x;',
  13565. ' this.y = s.y;',
  13566. ' return this;',
  13567. ' };',
  13568. ' this.Run = function (w) {',
  13569. ' $mod.TPoint.Run(13);',
  13570. ' $mod.TPoint.Run(13);',
  13571. ' };',
  13572. ' this.Create = function (ax, ay) {',
  13573. ' this.x = ax;',
  13574. ' this.y = ay;',
  13575. ' this.Run(13);',
  13576. ' this.Run(ax);',
  13577. ' return this;',
  13578. ' };',
  13579. '});',
  13580. 'this.r = this.TPoint.$new();',
  13581. '']),
  13582. LinesToStr([ // $mod.$main
  13583. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13584. 'var $with = $mod.TPoint;',
  13585. '$mod.r.$assign($with.$new().Create(1, 2));',
  13586. '$mod.r.Create(3, -1);',
  13587. '$mod.r.$assign($mod.r.Create(4, -1));',
  13588. '']));
  13589. end;
  13590. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13591. begin
  13592. StartProgram(false);
  13593. Add([
  13594. '{$modeswitch AdvancedRecords}',
  13595. 'type',
  13596. ' TPoint = record',
  13597. ' class var x: longint;',
  13598. ' class procedure Fly; static;',
  13599. ' class constructor Init;',
  13600. ' end;',
  13601. 'var count: word;',
  13602. 'class procedure Tpoint.Fly;',
  13603. 'begin',
  13604. 'end;',
  13605. 'class constructor tpoint.init;',
  13606. 'begin',
  13607. ' count:=count+1;',
  13608. ' x:=x+3;',
  13609. ' tpoint.x:=tpoint.x+4;',
  13610. ' fly;',
  13611. ' tpoint.fly;',
  13612. 'end;',
  13613. 'var r: TPoint;',
  13614. 'begin',
  13615. ' r.x:=r.x+10;',
  13616. ' r.Fly;',
  13617. ' r.Fly();',
  13618. '']);
  13619. ConvertProgram;
  13620. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13621. LinesToStr([ // statements
  13622. 'rtl.recNewT(this, "TPoint", function () {',
  13623. ' this.x = 0;',
  13624. ' this.$eq = function (b) {',
  13625. ' return true;',
  13626. ' };',
  13627. ' this.$assign = function (s) {',
  13628. ' return this;',
  13629. ' };',
  13630. ' this.Fly = function () {',
  13631. ' };',
  13632. '}, true);',
  13633. 'this.count = 0;',
  13634. 'this.r = this.TPoint.$new();',
  13635. '']),
  13636. LinesToStr([ // $mod.$main
  13637. '(function () {',
  13638. ' $mod.count = $mod.count + 1;',
  13639. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13640. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13641. ' $mod.TPoint.Fly();',
  13642. ' $mod.TPoint.Fly();',
  13643. '})();',
  13644. '$mod.TPoint.x = $mod.r.x + 10;',
  13645. '$mod.TPoint.Fly();',
  13646. '$mod.TPoint.Fly();',
  13647. '']));
  13648. end;
  13649. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13650. begin
  13651. StartUnit(false);
  13652. Add([
  13653. 'interface',
  13654. '{$modeswitch AdvancedRecords}',
  13655. 'type',
  13656. ' TPoint = record',
  13657. ' class var x: longint;',
  13658. ' class procedure Fly; static;',
  13659. ' class constructor Init;',
  13660. ' end;',
  13661. 'implementation',
  13662. 'var count: word;',
  13663. 'class procedure Tpoint.Fly;',
  13664. 'begin',
  13665. 'end;',
  13666. 'class constructor tpoint.init;',
  13667. 'begin',
  13668. ' count:=count+1;',
  13669. ' x:=3;',
  13670. ' tpoint.x:=4;',
  13671. ' fly;',
  13672. ' tpoint.fly;',
  13673. 'end;',
  13674. '']);
  13675. ConvertUnit;
  13676. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13677. LinesToStr([ // statements
  13678. 'var $impl = $mod.$impl;',
  13679. 'rtl.recNewT(this, "TPoint", function () {',
  13680. ' this.x = 0;',
  13681. ' this.$eq = function (b) {',
  13682. ' return true;',
  13683. ' };',
  13684. ' this.$assign = function (s) {',
  13685. ' return this;',
  13686. ' };',
  13687. ' this.Fly = function () {',
  13688. ' };',
  13689. '}, true);',
  13690. '']),
  13691. LinesToStr([ // $mod.$init
  13692. '(function () {',
  13693. ' $impl.count = $impl.count + 1;',
  13694. ' $mod.TPoint.x = 3;',
  13695. ' $mod.TPoint.x = 4;',
  13696. ' $mod.TPoint.Fly();',
  13697. ' $mod.TPoint.Fly();',
  13698. '})();',
  13699. '']),
  13700. LinesToStr([ // $mod.$main
  13701. '$impl.count = 0;',
  13702. '']));
  13703. end;
  13704. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13705. begin
  13706. StartProgram(false);
  13707. Add(['type',
  13708. ' TObject = class',
  13709. ' public',
  13710. ' constructor Create;',
  13711. ' destructor Destroy;',
  13712. ' end;',
  13713. ' TBird = TObject;',
  13714. 'constructor tobject.create;',
  13715. 'begin end;',
  13716. 'destructor tobject.destroy;',
  13717. 'begin end;',
  13718. 'var Obj: tobject;',
  13719. 'begin',
  13720. ' obj:=tobject.create;',
  13721. ' obj:=tobject.create();',
  13722. ' obj:=tbird.create;',
  13723. ' obj:=tbird.create();',
  13724. ' obj:=obj.create();',
  13725. ' obj.destroy;',
  13726. '']);
  13727. ConvertProgram;
  13728. CheckSource('TestClass_TObjectDefaultConstructor',
  13729. LinesToStr([ // statements
  13730. 'rtl.createClass(this,"TObject",null,function(){',
  13731. ' this.$init = function () {',
  13732. ' };',
  13733. ' this.$final = function () {',
  13734. ' };',
  13735. ' this.Create = function(){',
  13736. ' return this;',
  13737. ' };',
  13738. ' this.Destroy = function(){',
  13739. ' };',
  13740. '});',
  13741. 'this.Obj = null;'
  13742. ]),
  13743. LinesToStr([ // $mod.$main
  13744. '$mod.Obj = $mod.TObject.$create("Create");',
  13745. '$mod.Obj = $mod.TObject.$create("Create");',
  13746. '$mod.Obj = $mod.TObject.$create("Create");',
  13747. '$mod.Obj = $mod.TObject.$create("Create");',
  13748. '$mod.Obj = $mod.Obj.Create();',
  13749. '$mod.Obj.$destroy("Destroy");',
  13750. '']));
  13751. end;
  13752. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13753. begin
  13754. StartProgram(false);
  13755. Add('type');
  13756. Add(' TObject = class');
  13757. Add(' public');
  13758. Add(' constructor Create(Par: longint);');
  13759. Add(' end;');
  13760. Add('constructor tobject.create(par: longint);');
  13761. Add('begin end;');
  13762. Add('var Obj: tobject;');
  13763. Add('begin');
  13764. Add(' obj:=tobject.create(3);');
  13765. ConvertProgram;
  13766. CheckSource('TestClass_TObjectConstructorWithParams',
  13767. LinesToStr([ // statements
  13768. 'rtl.createClass(this,"TObject",null,function(){',
  13769. ' this.$init = function () {',
  13770. ' };',
  13771. ' this.$final = function () {',
  13772. ' };',
  13773. ' this.Create = function(Par){',
  13774. ' return this;',
  13775. ' };',
  13776. '});',
  13777. 'this.Obj = null;'
  13778. ]),
  13779. LinesToStr([ // $mod.$main
  13780. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13781. ]));
  13782. end;
  13783. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13784. begin
  13785. StartProgram(false);
  13786. Add('type');
  13787. Add(' TObject = class');
  13788. Add(' public');
  13789. Add(' constructor Create;');
  13790. Add(' end;');
  13791. Add(' TTest = class(TObject)');
  13792. Add(' public');
  13793. Add(' constructor Create(const Par: longint = 1);');
  13794. Add(' end;');
  13795. Add('constructor tobject.create;');
  13796. Add('begin end;');
  13797. Add('constructor ttest.create(const par: longint);');
  13798. Add('begin end;');
  13799. Add('var t: ttest;');
  13800. Add('begin');
  13801. Add(' t:=ttest.create;');
  13802. Add(' t:=ttest.create(2);');
  13803. ConvertProgram;
  13804. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13805. LinesToStr([ // statements
  13806. 'rtl.createClass(this,"TObject",null,function(){',
  13807. ' this.$init = function () {',
  13808. ' };',
  13809. ' this.$final = function () {',
  13810. ' };',
  13811. ' this.Create = function(){',
  13812. ' return this;',
  13813. ' };',
  13814. '});',
  13815. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13816. ' this.Create$1 = function (Par) {',
  13817. ' return this;',
  13818. ' };',
  13819. '});',
  13820. 'this.t = null;'
  13821. ]),
  13822. LinesToStr([ // $mod.$main
  13823. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13824. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13825. ]));
  13826. end;
  13827. procedure TTestModule.TestClass_Var;
  13828. begin
  13829. StartProgram(false);
  13830. Add([
  13831. 'type',
  13832. ' TObject = class',
  13833. ' public',
  13834. ' vI: longint;',
  13835. ' constructor Create(Par: longint);',
  13836. ' end;',
  13837. 'constructor tobject.create(par: longint);',
  13838. 'begin',
  13839. ' vi:=par+3',
  13840. 'end;',
  13841. 'var Obj: tobject;',
  13842. 'begin',
  13843. ' obj:=tobject.create(4);',
  13844. ' obj.vi:=obj.VI+5;']);
  13845. ConvertProgram;
  13846. CheckSource('TestClass_Var',
  13847. LinesToStr([ // statements
  13848. 'rtl.createClass(this,"TObject",null,function(){',
  13849. ' this.$init = function () {',
  13850. ' this.vI = 0;',
  13851. ' };',
  13852. ' this.$final = function () {',
  13853. ' };',
  13854. ' this.Create = function(Par){',
  13855. ' this.vI = Par+3;',
  13856. ' return this;',
  13857. ' };',
  13858. '});',
  13859. 'this.Obj = null;'
  13860. ]),
  13861. LinesToStr([ // $mod.$main
  13862. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13863. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13864. ]));
  13865. end;
  13866. procedure TTestModule.TestClass_Method;
  13867. begin
  13868. StartProgram(false);
  13869. Add('type');
  13870. Add(' TObject = class');
  13871. Add(' public');
  13872. Add(' vI: longint;');
  13873. Add(' Sub: TObject;');
  13874. Add(' constructor Create;');
  13875. Add(' function GetIt(Par: longint): tobject;');
  13876. Add(' end;');
  13877. Add('constructor tobject.create; begin end;');
  13878. Add('function tobject.getit(par: longint): tobject;');
  13879. Add('begin');
  13880. Add(' Self.vi:=par+3;');
  13881. Add(' Result:=self.sub;');
  13882. Add('end;');
  13883. Add('var Obj: tobject;');
  13884. Add('begin');
  13885. Add(' obj:=tobject.create;');
  13886. Add(' obj.getit(4);');
  13887. Add(' obj.sub.sub:=nil;');
  13888. Add(' obj.sub.getit(5);');
  13889. Add(' obj.sub.getit(6).SUB:=nil;');
  13890. Add(' obj.sub.getit(7).GETIT(8);');
  13891. Add(' obj.sub.getit(9).SuB.getit(10);');
  13892. ConvertProgram;
  13893. CheckSource('TestClass_Method',
  13894. LinesToStr([ // statements
  13895. 'rtl.createClass(this,"TObject",null,function(){',
  13896. ' this.$init = function () {',
  13897. ' this.vI = 0;',
  13898. ' this.Sub = null;',
  13899. ' };',
  13900. ' this.$final = function () {',
  13901. ' this.Sub = undefined;',
  13902. ' };',
  13903. ' this.Create = function(){',
  13904. ' return this;',
  13905. ' };',
  13906. ' this.GetIt = function(Par){',
  13907. ' var Result = null;',
  13908. ' this.vI = Par + 3;',
  13909. ' Result = this.Sub;',
  13910. ' return Result;',
  13911. ' };',
  13912. '});',
  13913. 'this.Obj = null;'
  13914. ]),
  13915. LinesToStr([ // $mod.$main
  13916. '$mod.Obj = $mod.TObject.$create("Create");',
  13917. '$mod.Obj.GetIt(4);',
  13918. '$mod.Obj.Sub.Sub=null;',
  13919. '$mod.Obj.Sub.GetIt(5);',
  13920. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13921. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13922. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13923. ]));
  13924. end;
  13925. procedure TTestModule.TestClass_Implementation;
  13926. begin
  13927. StartUnit(false);
  13928. Add([
  13929. 'interface',
  13930. 'type',
  13931. ' TObject = class',
  13932. ' constructor Create;',
  13933. ' end;',
  13934. 'implementation',
  13935. 'type',
  13936. ' TIntClass = class',
  13937. ' constructor Create; reintroduce;',
  13938. ' class procedure DoGlob;',
  13939. ' end;',
  13940. 'constructor tintclass.create;',
  13941. 'begin',
  13942. ' inherited;',
  13943. ' inherited create;',
  13944. ' doglob;',
  13945. 'end;',
  13946. 'class procedure tintclass.doglob;',
  13947. 'begin',
  13948. 'end;',
  13949. 'constructor tobject.create;',
  13950. 'var',
  13951. ' iC: tintclass;',
  13952. 'begin',
  13953. ' ic:=tintclass.create;',
  13954. ' tintclass.doglob;',
  13955. ' ic.doglob;',
  13956. 'end;',
  13957. 'initialization',
  13958. ' tintclass.doglob;',
  13959. '']);
  13960. ConvertUnit;
  13961. CheckSource('TestClass_Implementation',
  13962. LinesToStr([ // statements
  13963. 'var $impl = $mod.$impl;',
  13964. 'rtl.createClass(this, "TObject", null, function () {',
  13965. ' this.$init = function () {',
  13966. ' };',
  13967. ' this.$final = function () {',
  13968. ' };',
  13969. ' this.Create = function () {',
  13970. ' var iC = null;',
  13971. ' iC = $impl.TIntClass.$create("Create$1");',
  13972. ' $impl.TIntClass.DoGlob();',
  13973. ' iC.$class.DoGlob();',
  13974. ' return this;',
  13975. ' };',
  13976. '});',
  13977. '']),
  13978. LinesToStr([ // $mod.$main
  13979. '$impl.TIntClass.DoGlob();',
  13980. '']),
  13981. LinesToStr([
  13982. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  13983. ' this.Create$1 = function () {',
  13984. ' $mod.TObject.Create.call(this);',
  13985. ' $mod.TObject.Create.call(this);',
  13986. ' this.$class.DoGlob();',
  13987. ' return this;',
  13988. ' };',
  13989. ' this.DoGlob = function () {',
  13990. ' };',
  13991. '});',
  13992. '']));
  13993. end;
  13994. procedure TTestModule.TestClass_Inheritance;
  13995. begin
  13996. StartProgram(false);
  13997. Add('type');
  13998. Add(' TObject = class');
  13999. Add(' public');
  14000. Add(' constructor Create;');
  14001. Add(' end;');
  14002. Add(' TClassA = class');
  14003. Add(' end;');
  14004. Add(' TClassB = class(TObject)');
  14005. Add(' procedure ProcB;');
  14006. Add(' end;');
  14007. Add('constructor tobject.create; begin end;');
  14008. Add('procedure tclassb.procb; begin end;');
  14009. Add('var');
  14010. Add(' oO: TObject;');
  14011. Add(' oA: TClassA;');
  14012. Add(' oB: TClassB;');
  14013. Add('begin');
  14014. Add(' oO:=tobject.Create;');
  14015. Add(' oA:=tclassa.Create;');
  14016. Add(' ob:=tclassb.Create;');
  14017. Add(' if oo is tclassa then ;');
  14018. Add(' ob:=oo as tclassb;');
  14019. Add(' (oo as tclassb).procb;');
  14020. ConvertProgram;
  14021. CheckSource('TestClass_Inheritance',
  14022. LinesToStr([ // statements
  14023. 'rtl.createClass(this,"TObject",null,function(){',
  14024. ' this.$init = function () {',
  14025. ' };',
  14026. ' this.$final = function () {',
  14027. ' };',
  14028. ' this.Create = function () {',
  14029. ' return this;',
  14030. ' };',
  14031. '});',
  14032. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14033. '});',
  14034. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14035. ' this.ProcB = function () {',
  14036. ' };',
  14037. '});',
  14038. 'this.oO = null;',
  14039. 'this.oA = null;',
  14040. 'this.oB = null;'
  14041. ]),
  14042. LinesToStr([ // $mod.$main
  14043. '$mod.oO = $mod.TObject.$create("Create");',
  14044. '$mod.oA = $mod.TClassA.$create("Create");',
  14045. '$mod.oB = $mod.TClassB.$create("Create");',
  14046. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14047. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14048. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14049. ]));
  14050. end;
  14051. procedure TTestModule.TestClass_TypeAlias;
  14052. begin
  14053. StartProgram(false);
  14054. Add([
  14055. '{$interfaces corba}',
  14056. 'type',
  14057. ' IObject = interface',
  14058. ' end;',
  14059. ' IBird = type IObject;',
  14060. ' TObject = class',
  14061. ' end;',
  14062. ' TBird = type TObject;',
  14063. 'var',
  14064. ' oObj: TObject;',
  14065. ' oBird: TBird;',
  14066. ' IntfObj: IObject;',
  14067. ' IntfBird: IBird;',
  14068. 'begin',
  14069. ' oObj:=oBird;',
  14070. '']);
  14071. ConvertProgram;
  14072. CheckSource('TestClass_TypeAlias',
  14073. LinesToStr([ // statements
  14074. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14075. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14076. 'rtl.createClass(this, "TObject", null, function () {',
  14077. ' this.$init = function () {',
  14078. ' };',
  14079. ' this.$final = function () {',
  14080. ' };',
  14081. '});',
  14082. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14083. '});',
  14084. 'this.oObj = null;',
  14085. 'this.oBird = null;',
  14086. 'this.IntfObj = null;',
  14087. 'this.IntfBird = null;',
  14088. '']),
  14089. LinesToStr([ // $mod.$main
  14090. '$mod.oObj = $mod.oBird;',
  14091. '']));
  14092. end;
  14093. procedure TTestModule.TestClass_AbstractMethod;
  14094. begin
  14095. StartProgram(false);
  14096. Add('type');
  14097. Add(' TObject = class');
  14098. Add(' public');
  14099. Add(' procedure DoIt; virtual; abstract;');
  14100. Add(' end;');
  14101. Add('begin');
  14102. ConvertProgram;
  14103. CheckSource('TestClass_AbstractMethod',
  14104. LinesToStr([ // statements
  14105. 'rtl.createClass(this,"TObject",null,function(){',
  14106. ' this.$init = function () {',
  14107. ' };',
  14108. ' this.$final = function () {',
  14109. ' };',
  14110. '});'
  14111. ]),
  14112. LinesToStr([ // this.$main
  14113. ''
  14114. ]));
  14115. end;
  14116. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14117. begin
  14118. StartProgram(false);
  14119. Add([
  14120. 'type',
  14121. ' TObject = class',
  14122. ' procedure DoAbstract; virtual; abstract;',
  14123. ' procedure DoVirtual; virtual;',
  14124. ' procedure DoIt;',
  14125. ' end;',
  14126. ' TA = class',
  14127. ' procedure doabstract; override;',
  14128. ' procedure dovirtual; override;',
  14129. ' procedure DoSome;',
  14130. ' end;',
  14131. 'procedure tobject.dovirtual;',
  14132. 'begin',
  14133. ' inherited; // call non existing ancestor -> ignore silently',
  14134. 'end;',
  14135. 'procedure tobject.doit;',
  14136. 'begin',
  14137. 'end;',
  14138. 'procedure ta.doabstract;',
  14139. 'begin',
  14140. ' inherited dovirtual; // call TObject.DoVirtual',
  14141. 'end;',
  14142. 'procedure ta.dovirtual;',
  14143. 'begin',
  14144. ' inherited; // call TObject.DoVirtual',
  14145. ' inherited dovirtual; // call TObject.DoVirtual',
  14146. ' inherited dovirtual(); // call TObject.DoVirtual',
  14147. ' doit;',
  14148. ' doit();',
  14149. 'end;',
  14150. 'procedure ta.dosome;',
  14151. 'begin',
  14152. ' inherited; // call non existing ancestor method -> silently ignore',
  14153. 'end;',
  14154. 'begin']);
  14155. ConvertProgram;
  14156. CheckSource('TestClass_CallInherited_ProcNoParams',
  14157. LinesToStr([ // statements
  14158. 'rtl.createClass(this,"TObject",null,function(){',
  14159. ' this.$init = function () {',
  14160. ' };',
  14161. ' this.$final = function () {',
  14162. ' };',
  14163. ' this.DoVirtual = function () {',
  14164. ' };',
  14165. ' this.DoIt = function () {',
  14166. ' };',
  14167. '});',
  14168. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14169. ' this.DoAbstract = function () {',
  14170. ' $mod.TObject.DoVirtual.call(this);',
  14171. ' };',
  14172. ' this.DoVirtual = function () {',
  14173. ' $mod.TObject.DoVirtual.call(this);',
  14174. ' $mod.TObject.DoVirtual.call(this);',
  14175. ' $mod.TObject.DoVirtual.call(this);',
  14176. ' this.DoIt();',
  14177. ' this.DoIt();',
  14178. ' };',
  14179. ' this.DoSome = function () {',
  14180. ' };',
  14181. '});'
  14182. ]),
  14183. LinesToStr([ // this.$main
  14184. ''
  14185. ]));
  14186. end;
  14187. procedure TTestModule.TestClass_CallInherited_WithParams;
  14188. begin
  14189. StartProgram(false);
  14190. Add([
  14191. 'type',
  14192. ' TObject = class',
  14193. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14194. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14195. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14196. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14197. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14198. ' end;',
  14199. ' TClassA = class',
  14200. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14201. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14202. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14203. ' end;',
  14204. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14205. 'begin',
  14206. 'end;',
  14207. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14208. 'begin',
  14209. 'end;',
  14210. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14211. 'begin',
  14212. 'end;',
  14213. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14214. 'begin',
  14215. 'end;',
  14216. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14217. 'begin',
  14218. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14219. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14220. 'end;',
  14221. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14222. 'begin',
  14223. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14224. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14225. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14226. ' doit(pa,pb);',
  14227. ' doit(pa);',
  14228. ' doit2(pa);',
  14229. ' doit2;',
  14230. 'end;',
  14231. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14232. 'begin',
  14233. ' pa:=inherited;',
  14234. 'end;',
  14235. 'begin']);
  14236. ConvertProgram;
  14237. CheckSource('TestClass_CallInherited_WithParams',
  14238. LinesToStr([ // statements
  14239. 'rtl.createClass(this,"TObject",null,function(){',
  14240. ' this.$init = function () {',
  14241. ' };',
  14242. ' this.$final = function () {',
  14243. ' };',
  14244. ' this.DoVirtual = function (pA,pB) {',
  14245. ' };',
  14246. ' this.DoIt = function (pA,pB) {',
  14247. ' };',
  14248. ' this.DoIt2 = function (pA,pB) {',
  14249. ' };',
  14250. ' this.GetIt = function (pA, pB) {',
  14251. ' var Result = 0;',
  14252. ' return Result;',
  14253. ' };',
  14254. '});',
  14255. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14256. ' this.DoAbstract = function (pA,pB) {',
  14257. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14258. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14259. ' };',
  14260. ' this.DoVirtual = function (pA,pB) {',
  14261. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14262. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14263. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14264. ' this.DoIt(pA,pB);',
  14265. ' this.DoIt(pA,0);',
  14266. ' this.DoIt2(pA,2);',
  14267. ' this.DoIt2(1,2);',
  14268. ' };',
  14269. ' this.GetIt$1 = function (pA, pB) {',
  14270. ' var Result = 0;',
  14271. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14272. ' return Result;',
  14273. ' };',
  14274. '});'
  14275. ]),
  14276. LinesToStr([ // this.$main
  14277. ''
  14278. ]));
  14279. end;
  14280. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14281. begin
  14282. StartProgram(false);
  14283. Add('type');
  14284. Add(' TObject = class');
  14285. Add(' constructor Create; virtual;');
  14286. Add(' constructor CreateWithB(b: boolean);');
  14287. Add(' end;');
  14288. Add(' TA = class');
  14289. Add(' constructor Create; override;');
  14290. Add(' constructor CreateWithC(c: char);');
  14291. Add(' procedure DoIt;');
  14292. Add(' class function DoSome: TObject;');
  14293. Add(' end;');
  14294. Add('constructor tobject.create;');
  14295. Add('begin');
  14296. Add(' inherited; // call non existing ancestor -> ignore silently');
  14297. Add('end;');
  14298. Add('constructor tobject.createwithb(b: boolean);');
  14299. Add('begin');
  14300. Add(' inherited; // call non existing ancestor -> ignore silently');
  14301. Add(' create; // normal call');
  14302. Add('end;');
  14303. Add('constructor ta.create;');
  14304. Add('begin');
  14305. Add(' inherited; // normal call TObject.Create');
  14306. Add(' inherited create; // normal call TObject.Create');
  14307. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14308. Add('end;');
  14309. Add('constructor ta.createwithc(c: char);');
  14310. Add('begin');
  14311. Add(' inherited create; // call TObject.Create');
  14312. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14313. Add(' doit;');
  14314. Add(' doit();');
  14315. Add(' dosome;');
  14316. Add('end;');
  14317. Add('procedure ta.doit;');
  14318. Add('begin');
  14319. Add(' create; // normal call');
  14320. Add(' createwithb(false); // normal call');
  14321. Add(' createwithc(''c''); // normal call');
  14322. Add('end;');
  14323. Add('class function ta.dosome: TObject;');
  14324. Add('begin');
  14325. Add(' Result:=create; // constructor');
  14326. Add(' Result:=createwithb(true); // constructor');
  14327. Add(' Result:=createwithc(''c''); // constructor');
  14328. Add('end;');
  14329. Add('begin');
  14330. ConvertProgram;
  14331. CheckSource('TestClass_CallInheritedConstructor',
  14332. LinesToStr([ // statements
  14333. 'rtl.createClass(this,"TObject",null,function(){',
  14334. ' this.$init = function () {',
  14335. ' };',
  14336. ' this.$final = function () {',
  14337. ' };',
  14338. ' this.Create = function () {',
  14339. ' return this;',
  14340. ' };',
  14341. ' this.CreateWithB = function (b) {',
  14342. ' this.Create();',
  14343. ' return this;',
  14344. ' };',
  14345. '});',
  14346. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14347. ' this.Create = function () {',
  14348. ' $mod.TObject.Create.call(this);',
  14349. ' $mod.TObject.Create.call(this);',
  14350. ' $mod.TObject.CreateWithB.call(this, false);',
  14351. ' return this;',
  14352. ' };',
  14353. ' this.CreateWithC = function (c) {',
  14354. ' $mod.TObject.Create.call(this);',
  14355. ' $mod.TObject.CreateWithB.call(this, true);',
  14356. ' this.DoIt();',
  14357. ' this.DoIt();',
  14358. ' this.$class.DoSome();',
  14359. ' return this;',
  14360. ' };',
  14361. ' this.DoIt = function () {',
  14362. ' this.Create();',
  14363. ' this.CreateWithB(false);',
  14364. ' this.CreateWithC("c");',
  14365. ' };',
  14366. ' this.DoSome = function () {',
  14367. ' var Result = null;',
  14368. ' Result = this.$create("Create");',
  14369. ' Result = this.$create("CreateWithB", [true]);',
  14370. ' Result = this.$create("CreateWithC", ["c"]);',
  14371. ' return Result;',
  14372. ' };',
  14373. '});'
  14374. ]),
  14375. LinesToStr([ // this.$main
  14376. ''
  14377. ]));
  14378. end;
  14379. procedure TTestModule.TestClass_ClassVar_Assign;
  14380. begin
  14381. StartProgram(false);
  14382. Add([
  14383. 'type',
  14384. ' TObject = class',
  14385. ' public',
  14386. ' class var vI: longint;',
  14387. ' class var Sub: TObject;',
  14388. ' constructor Create;',
  14389. ' class function GetIt(var Par: longint): tobject;',
  14390. ' end;',
  14391. 'constructor tobject.create;',
  14392. 'begin',
  14393. ' vi:=vi+1;',
  14394. ' Self.vi:=Self.vi+1;',
  14395. ' inc(vi);',
  14396. 'end;',
  14397. 'class function tobject.getit(var par: longint): tobject;',
  14398. 'begin',
  14399. ' vi:=vi+3;',
  14400. ' Self.vi:=Self.vi+4;',
  14401. ' inc(vi);',
  14402. ' Result:=self.sub;',
  14403. ' GetIt(vi);',
  14404. 'end;',
  14405. 'var Obj: tobject;',
  14406. 'begin',
  14407. ' obj:=tobject.create;',
  14408. ' tobject.vi:=3;',
  14409. ' if tobject.vi=4 then ;',
  14410. ' tobject.sub:=nil;',
  14411. ' obj.sub:=nil;',
  14412. ' obj.sub.sub:=nil;']);
  14413. ConvertProgram;
  14414. CheckSource('TestClass_ClassVar_Assign',
  14415. LinesToStr([ // statements
  14416. 'rtl.createClass(this,"TObject",null,function(){',
  14417. ' this.vI = 0;',
  14418. ' this.Sub = null;',
  14419. ' this.$init = function () {',
  14420. ' };',
  14421. ' this.$final = function () {',
  14422. ' };',
  14423. ' this.Create = function(){',
  14424. ' $mod.TObject.vI = this.vI+1;',
  14425. ' $mod.TObject.vI = this.vI+1;',
  14426. ' $mod.TObject.vI += 1;',
  14427. ' return this;',
  14428. ' };',
  14429. ' this.GetIt = function(Par){',
  14430. ' var Result = null;',
  14431. ' $mod.TObject.vI = this.vI + 3;',
  14432. ' $mod.TObject.vI = this.vI + 4;',
  14433. ' $mod.TObject.vI += 1;',
  14434. ' Result = this.Sub;',
  14435. ' this.GetIt({',
  14436. ' p: $mod.TObject,',
  14437. ' get: function () {',
  14438. ' return this.p.vI;',
  14439. ' },',
  14440. ' set: function (v) {',
  14441. ' this.p.vI = v;',
  14442. ' }',
  14443. ' });',
  14444. ' return Result;',
  14445. ' };',
  14446. '});',
  14447. 'this.Obj = null;'
  14448. ]),
  14449. LinesToStr([ // $mod.$main
  14450. '$mod.Obj = $mod.TObject.$create("Create");',
  14451. '$mod.TObject.vI = 3;',
  14452. 'if ($mod.TObject.vI === 4);',
  14453. '$mod.TObject.Sub=null;',
  14454. '$mod.TObject.Sub=null;',
  14455. '$mod.TObject.Sub=null;',
  14456. '']));
  14457. end;
  14458. procedure TTestModule.TestClass_CallClassMethod;
  14459. begin
  14460. StartProgram(false);
  14461. Add('type');
  14462. Add(' TObject = class');
  14463. Add(' public');
  14464. Add(' class var vI: longint;');
  14465. Add(' class var Sub: TObject;');
  14466. Add(' constructor Create;');
  14467. Add(' function GetMore(Par: longint): longint;');
  14468. Add(' class function GetIt(Par: longint): tobject;');
  14469. Add(' end;');
  14470. Add('constructor tobject.create;');
  14471. Add('begin');
  14472. Add(' sub:=getit(3);');
  14473. Add(' vi:=getmore(4);');
  14474. Add(' sub:=Self.getit(5);');
  14475. Add(' vi:=Self.getmore(6);');
  14476. Add('end;');
  14477. Add('function tobject.getmore(par: longint): longint;');
  14478. Add('begin');
  14479. Add(' sub:=getit(11);');
  14480. Add(' vi:=getmore(12);');
  14481. Add(' sub:=self.getit(13);');
  14482. Add(' vi:=self.getmore(14);');
  14483. Add('end;');
  14484. Add('class function tobject.getit(par: longint): tobject;');
  14485. Add('begin');
  14486. Add(' sub:=getit(21);');
  14487. Add(' vi:=sub.getmore(22);');
  14488. Add(' sub:=self.getit(23);');
  14489. Add(' vi:=self.sub.getmore(24);');
  14490. Add('end;');
  14491. Add('var Obj: tobject;');
  14492. Add('begin');
  14493. Add(' obj:=tobject.create;');
  14494. Add(' tobject.getit(5);');
  14495. Add(' obj.getit(6);');
  14496. Add(' obj.sub.getit(7);');
  14497. Add(' obj.sub.getit(8).SUB:=nil;');
  14498. Add(' obj.sub.getit(9).GETIT(10);');
  14499. Add(' obj.sub.getit(11).SuB.getit(12);');
  14500. ConvertProgram;
  14501. CheckSource('TestClass_CallClassMethod',
  14502. LinesToStr([ // statements
  14503. 'rtl.createClass(this,"TObject",null,function(){',
  14504. ' this.vI = 0;',
  14505. ' this.Sub = null;',
  14506. ' this.$init = function () {',
  14507. ' };',
  14508. ' this.$final = function () {',
  14509. ' };',
  14510. ' this.Create = function(){',
  14511. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14512. ' $mod.TObject.vI = this.GetMore(4);',
  14513. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14514. ' $mod.TObject.vI = this.GetMore(6);',
  14515. ' return this;',
  14516. ' };',
  14517. ' this.GetMore = function(Par){',
  14518. ' var Result = 0;',
  14519. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14520. ' $mod.TObject.vI = this.GetMore(12);',
  14521. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14522. ' $mod.TObject.vI = this.GetMore(14);',
  14523. ' return Result;',
  14524. ' };',
  14525. ' this.GetIt = function(Par){',
  14526. ' var Result = null;',
  14527. ' $mod.TObject.Sub = this.GetIt(21);',
  14528. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14529. ' $mod.TObject.Sub = this.GetIt(23);',
  14530. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14531. ' return Result;',
  14532. ' };',
  14533. '});',
  14534. 'this.Obj = null;'
  14535. ]),
  14536. LinesToStr([ // $mod.$main
  14537. '$mod.Obj = $mod.TObject.$create("Create");',
  14538. '$mod.TObject.GetIt(5);',
  14539. '$mod.Obj.$class.GetIt(6);',
  14540. '$mod.Obj.Sub.$class.GetIt(7);',
  14541. '$mod.TObject.Sub=null;',
  14542. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14543. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14544. '']));
  14545. end;
  14546. procedure TTestModule.TestClass_CallClassMethodStatic;
  14547. begin
  14548. StartProgram(false);
  14549. Add([
  14550. 'type',
  14551. ' TObject = class',
  14552. ' public',
  14553. ' class function Fly: tobject; static;',
  14554. ' end;',
  14555. 'class function tobject.Fly: tobject;',
  14556. 'begin',
  14557. ' Result.Fly;',
  14558. ' Result.Fly();',
  14559. ' Fly;',
  14560. ' Fly();',
  14561. ' Fly.Fly;',
  14562. ' Fly.Fly();',
  14563. 'end;',
  14564. 'var Obj: tobject;',
  14565. 'begin',
  14566. ' obj.Fly;',
  14567. ' obj.Fly();',
  14568. ' with obj do begin',
  14569. ' Fly;',
  14570. ' Fly();',
  14571. ' end;',
  14572. '']);
  14573. ConvertProgram;
  14574. CheckSource('TestClass_CallClassMethodStatic',
  14575. LinesToStr([ // statements
  14576. 'rtl.createClass(this, "TObject", null, function () {',
  14577. ' this.$init = function () {',
  14578. ' };',
  14579. ' this.$final = function () {',
  14580. ' };',
  14581. ' this.Fly = function () {',
  14582. ' var Result = null;',
  14583. ' $mod.TObject.Fly();',
  14584. ' $mod.TObject.Fly();',
  14585. ' $mod.TObject.Fly();',
  14586. ' $mod.TObject.Fly();',
  14587. ' $mod.TObject.Fly();',
  14588. ' $mod.TObject.Fly();',
  14589. ' return Result;',
  14590. ' };',
  14591. '});',
  14592. 'this.Obj = null;'
  14593. ]),
  14594. LinesToStr([ // $mod.$main
  14595. '$mod.TObject.Fly();',
  14596. '$mod.TObject.Fly();',
  14597. 'var $with = $mod.Obj;',
  14598. '$with.Fly();',
  14599. '$with.Fly();',
  14600. '']));
  14601. end;
  14602. procedure TTestModule.TestClass_Property;
  14603. begin
  14604. StartProgram(false);
  14605. Add('type');
  14606. Add(' TObject = class');
  14607. Add(' Fx: longint;');
  14608. Add(' Fy: longint;');
  14609. Add(' function GetInt: longint;');
  14610. Add(' procedure SetInt(Value: longint);');
  14611. Add(' procedure DoIt;');
  14612. Add(' property IntA: longint read Fx write Fy;');
  14613. Add(' property IntB: longint read GetInt write SetInt;');
  14614. Add(' end;');
  14615. Add('function tobject.getint: longint;');
  14616. Add('begin');
  14617. Add(' result:=fx;');
  14618. Add('end;');
  14619. Add('procedure tobject.setint(value: longint);');
  14620. Add('begin');
  14621. Add(' if value=fy then exit;');
  14622. Add(' fy:=value;');
  14623. Add('end;');
  14624. Add('procedure tobject.doit;');
  14625. Add('begin');
  14626. Add(' IntA:=IntA+1;');
  14627. Add(' Self.IntA:=Self.IntA+1;');
  14628. Add(' IntB:=IntB+1;');
  14629. Add(' Self.IntB:=Self.IntB+1;');
  14630. Add('end;');
  14631. Add('var Obj: tobject;');
  14632. Add('begin');
  14633. Add(' obj.inta:=obj.inta+1;');
  14634. Add(' if obj.intb=2 then;');
  14635. Add(' obj.intb:=obj.intb+2;');
  14636. Add(' obj.setint(obj.inta);');
  14637. ConvertProgram;
  14638. CheckSource('TestClass_Property',
  14639. LinesToStr([ // statements
  14640. 'rtl.createClass(this, "TObject", null, function () {',
  14641. ' this.$init = function () {',
  14642. ' this.Fx = 0;',
  14643. ' this.Fy = 0;',
  14644. ' };',
  14645. ' this.$final = function () {',
  14646. ' };',
  14647. ' this.GetInt = function () {',
  14648. ' var Result = 0;',
  14649. ' Result = this.Fx;',
  14650. ' return Result;',
  14651. ' };',
  14652. ' this.SetInt = function (Value) {',
  14653. ' if (Value === this.Fy) return;',
  14654. ' this.Fy = Value;',
  14655. ' };',
  14656. ' this.DoIt = function () {',
  14657. ' this.Fy = this.Fx + 1;',
  14658. ' this.Fy = this.Fx + 1;',
  14659. ' this.SetInt(this.GetInt() + 1);',
  14660. ' this.SetInt(this.GetInt() + 1);',
  14661. ' };',
  14662. '});',
  14663. 'this.Obj = null;'
  14664. ]),
  14665. LinesToStr([ // $mod.$main
  14666. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14667. 'if ($mod.Obj.GetInt() === 2);',
  14668. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14669. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14670. ]));
  14671. end;
  14672. procedure TTestModule.TestClass_Property_ClassMethod;
  14673. begin
  14674. StartProgram(false);
  14675. Add([
  14676. 'type',
  14677. ' TObject = class',
  14678. ' class var Fx: longint;',
  14679. ' class var Fy: longint;',
  14680. ' class function GetInt: longint;',
  14681. ' class procedure SetInt(Value: longint);',
  14682. ' end;',
  14683. ' TBird = class',
  14684. ' class procedure DoIt;',
  14685. ' class property IntA: longint read Fx write Fy;',
  14686. ' class property IntB: longint read GetInt write SetInt;',
  14687. ' end;',
  14688. 'class function tobject.getint: longint;',
  14689. 'begin',
  14690. ' result:=fx;',
  14691. 'end;',
  14692. 'class procedure tobject.setint(value: longint);',
  14693. 'begin',
  14694. 'end;',
  14695. 'class procedure tbird.doit;',
  14696. 'begin',
  14697. ' FX:=3;',
  14698. ' IntA:=IntA+1;',
  14699. ' Self.IntA:=Self.IntA+1;',
  14700. ' IntB:=IntB+1;',
  14701. ' Self.IntB:=Self.IntB+1;',
  14702. ' with Self do begin',
  14703. ' FX:=11;',
  14704. ' IntA:=IntA+12;',
  14705. ' IntB:=IntB+13;',
  14706. ' end;',
  14707. 'end;',
  14708. 'var Obj: tbird;',
  14709. 'begin',
  14710. ' tbird.fx:=tbird.fx+1;',
  14711. ' tbird.inta:=tbird.inta+1;',
  14712. ' if tbird.intb=2 then;',
  14713. ' tbird.intb:=tbird.intb+2;',
  14714. ' tbird.setint(tbird.inta);',
  14715. ' obj.inta:=obj.inta+1;',
  14716. ' if obj.intb=2 then;',
  14717. ' obj.intb:=obj.intb+2;',
  14718. ' obj.setint(obj.inta);',
  14719. ' with Tbird do begin',
  14720. ' FX:=FY+1;',
  14721. ' inta:=inta+2;',
  14722. ' intb:=intb+3;',
  14723. ' end;',
  14724. ' with Obj do begin',
  14725. ' FX:=FY+1;',
  14726. ' inta:=inta+2;',
  14727. ' intb:=intb+3;',
  14728. ' end;',
  14729. '']);
  14730. ConvertProgram;
  14731. CheckSource('TestClass_Property_ClassMethod',
  14732. LinesToStr([ // statements
  14733. 'rtl.createClass(this, "TObject", null, function () {',
  14734. ' this.Fx = 0;',
  14735. ' this.Fy = 0;',
  14736. ' this.$init = function () {',
  14737. ' };',
  14738. ' this.$final = function () {',
  14739. ' };',
  14740. ' this.GetInt = function () {',
  14741. ' var Result = 0;',
  14742. ' Result = this.Fx;',
  14743. ' return Result;',
  14744. ' };',
  14745. ' this.SetInt = function (Value) {',
  14746. ' };',
  14747. '});',
  14748. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14749. ' this.DoIt = function () {',
  14750. ' $mod.TObject.Fx = 3;',
  14751. ' $mod.TObject.Fy = this.Fx + 1;',
  14752. ' $mod.TObject.Fy = this.Fx + 1;',
  14753. ' this.SetInt(this.GetInt() + 1);',
  14754. ' this.SetInt(this.GetInt() + 1);',
  14755. ' $mod.TObject.Fx = 11;',
  14756. ' $mod.TObject.Fy = this.Fx + 12;',
  14757. ' this.SetInt(this.GetInt() + 13);',
  14758. ' };',
  14759. '});',
  14760. 'this.Obj = null;'
  14761. ]),
  14762. LinesToStr([ // $mod.$main
  14763. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14764. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14765. 'if ($mod.TBird.GetInt() === 2);',
  14766. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14767. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14768. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14769. 'if ($mod.Obj.$class.GetInt() === 2);',
  14770. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14771. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14772. 'var $with = $mod.TBird;',
  14773. '$mod.TObject.Fx = $with.Fy + 1;',
  14774. '$mod.TObject.Fy = $with.Fx + 2;',
  14775. '$with.SetInt($with.GetInt() + 3);',
  14776. 'var $with1 = $mod.Obj;',
  14777. '$mod.TObject.Fx = $with1.Fy + 1;',
  14778. '$mod.TObject.Fy = $with1.Fx + 2;',
  14779. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14780. '']));
  14781. end;
  14782. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14783. begin
  14784. StartProgram(false);
  14785. Add([
  14786. 'type',
  14787. ' TObject = class',
  14788. ' class function GetInt: longint; static;',
  14789. ' class procedure SetInt(Value: longint); static;',
  14790. ' class function GetItems(Index: word): longint; static;',
  14791. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14792. ' end;',
  14793. ' TBird = class',
  14794. ' class procedure Fly;',
  14795. ' class property IntA: longint read GetInt write SetInt;',
  14796. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14797. ' end;',
  14798. 'class function tobject.getint: longint;',
  14799. 'begin',
  14800. 'end;',
  14801. 'class procedure tobject.setint(value: longint);',
  14802. 'begin',
  14803. 'end;',
  14804. 'class function tobject.GetItems(Index: word): longint;',
  14805. 'begin',
  14806. 'end;',
  14807. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14808. 'begin',
  14809. 'end;',
  14810. 'class procedure tbird.fly;',
  14811. 'var w: longint;',
  14812. 'begin',
  14813. ' inta:=inta+51;',
  14814. ' w:=items[52];',
  14815. ' items[53]:=54;',
  14816. 'end;',
  14817. 'var Obj: tbird;',
  14818. ' i: longint;',
  14819. 'begin',
  14820. ' tbird.inta:=tbird.inta+1;',
  14821. ' i:=tbird.items[2];',
  14822. ' tbird.items[3]:=4;',
  14823. ' obj.inta:=obj.inta+11;',
  14824. ' i:=obj.items[12];',
  14825. ' obj.items[13]:=14;',
  14826. ' with Tbird do begin',
  14827. ' inta:=inta+21;',
  14828. ' i:=items[22];',
  14829. ' items[23]:=24;',
  14830. ' end;',
  14831. ' with Obj do begin',
  14832. ' inta:=inta+31;',
  14833. ' i:=items[32];',
  14834. ' items[33]:=34;',
  14835. ' end;',
  14836. '']);
  14837. ConvertProgram;
  14838. CheckSource('TestClass_Property_ClassMethod',
  14839. LinesToStr([ // statements
  14840. 'rtl.createClass(this, "TObject", null, function () {',
  14841. ' this.$init = function () {',
  14842. ' };',
  14843. ' this.$final = function () {',
  14844. ' };',
  14845. ' this.GetInt = function () {',
  14846. ' var Result = 0;',
  14847. ' return Result;',
  14848. ' };',
  14849. ' this.SetInt = function (Value) {',
  14850. ' };',
  14851. ' this.GetItems = function (Index) {',
  14852. ' var Result = 0;',
  14853. ' return Result;',
  14854. ' };',
  14855. ' this.SetItems = function (Index, Value) {',
  14856. ' };',
  14857. '});',
  14858. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14859. ' this.Fly = function () {',
  14860. ' var w = 0;',
  14861. ' this.SetInt(this.GetInt() + 51);',
  14862. ' w = this.GetItems(52);',
  14863. ' this.SetItems(53, 54);',
  14864. ' };',
  14865. '});',
  14866. 'this.Obj = null;',
  14867. 'this.i = 0;',
  14868. '']),
  14869. LinesToStr([ // $mod.$main
  14870. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14871. '$mod.i = $mod.TObject.GetItems(2);',
  14872. '$mod.TObject.SetItems(3, 4);',
  14873. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14874. '$mod.i = $mod.TObject.GetItems(12);',
  14875. '$mod.TObject.SetItems(13, 14);',
  14876. 'var $with = $mod.TBird;',
  14877. '$with.SetInt($with.GetInt() + 21);',
  14878. '$mod.i = $with.GetItems(22);',
  14879. '$with.SetItems(23, 24);',
  14880. 'var $with1 = $mod.Obj;',
  14881. '$with1.SetInt($with1.GetInt() + 31);',
  14882. '$mod.i = $with1.GetItems(32);',
  14883. '$with1.SetItems(33, 34);',
  14884. '']));
  14885. end;
  14886. procedure TTestModule.TestClass_Property_Indexed;
  14887. begin
  14888. StartProgram(false);
  14889. Add([
  14890. 'type',
  14891. ' TObject = class',
  14892. ' FItems: array of longint;',
  14893. ' function GetItems(Index: longint): longint;',
  14894. ' procedure SetItems(Index: longint; Value: longint);',
  14895. ' procedure DoIt;',
  14896. ' property Items[Index: longint]: longint read getitems write setitems;',
  14897. ' end;',
  14898. 'function tobject.getitems(index: longint): longint;',
  14899. 'begin',
  14900. ' Result:=fitems[index];',
  14901. 'end;',
  14902. 'procedure tobject.setitems(index: longint; value: longint);',
  14903. 'begin',
  14904. ' fitems[index]:=value;',
  14905. 'end;',
  14906. 'procedure tobject.doit;',
  14907. 'begin',
  14908. ' items[1]:=2;',
  14909. ' items[3]:=items[4];',
  14910. ' self.items[5]:=self.items[6];',
  14911. ' items[items[7]]:=items[items[8]];',
  14912. 'end;',
  14913. 'var Obj: tobject;',
  14914. 'begin',
  14915. ' obj.Items[11]:=obj.Items[12];',
  14916. '']);
  14917. ConvertProgram;
  14918. CheckSource('TestClass_Property_Indexed',
  14919. LinesToStr([ // statements
  14920. 'rtl.createClass(this, "TObject", null, function () {',
  14921. ' this.$init = function () {',
  14922. ' this.FItems = [];',
  14923. ' };',
  14924. ' this.$final = function () {',
  14925. ' this.FItems = undefined;',
  14926. ' };',
  14927. ' this.GetItems = function (Index) {',
  14928. ' var Result = 0;',
  14929. ' Result = this.FItems[Index];',
  14930. ' return Result;',
  14931. ' };',
  14932. ' this.SetItems = function (Index, Value) {',
  14933. ' this.FItems[Index] = Value;',
  14934. ' };',
  14935. ' this.DoIt = function () {',
  14936. ' this.SetItems(1, 2);',
  14937. ' this.SetItems(3,this.GetItems(4));',
  14938. ' this.SetItems(5,this.GetItems(6));',
  14939. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14940. ' };',
  14941. '});',
  14942. 'this.Obj = null;'
  14943. ]),
  14944. LinesToStr([ // $mod.$main
  14945. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  14946. ]));
  14947. end;
  14948. procedure TTestModule.TestClass_Property_IndexSpec;
  14949. begin
  14950. StartProgram(false);
  14951. Add([
  14952. 'type',
  14953. ' TEnum = (red, blue);',
  14954. ' TObject = class',
  14955. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  14956. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  14957. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  14958. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  14959. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  14960. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  14961. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  14962. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  14963. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  14964. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  14965. ' end;',
  14966. 'procedure DoIt(b: boolean); begin end;',
  14967. 'var',
  14968. ' o: TObject;',
  14969. 'begin',
  14970. ' o.B1:=o.B1;',
  14971. ' o.B2:=o.B2;',
  14972. ' o.B3:=o.B3;',
  14973. ' o.I1[''a'']:=o.I1[''b''];',
  14974. ' doit(o.b1);',
  14975. ' doit(o.b2);',
  14976. ' doit(o.i1[''c'']);',
  14977. '']);
  14978. ConvertProgram;
  14979. CheckSource('TestClass_Property_IndexSpec',
  14980. LinesToStr([ // statements
  14981. 'this.TEnum = {',
  14982. ' "0": "red",',
  14983. ' red: 0,',
  14984. ' "1": "blue",',
  14985. ' blue: 1',
  14986. '};',
  14987. 'rtl.createClass(this, "TObject", null, function () {',
  14988. ' this.$init = function () {',
  14989. ' };',
  14990. ' this.$final = function () {',
  14991. ' };',
  14992. '});',
  14993. 'this.DoIt = function (b) {',
  14994. '};',
  14995. 'this.o = null;',
  14996. '']),
  14997. LinesToStr([ // $mod.$main
  14998. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  14999. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15000. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15001. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15002. '$mod.DoIt($mod.o.GetIntBool(1));',
  15003. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15004. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15005. '']));
  15006. end;
  15007. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15008. begin
  15009. StartProgram(false);
  15010. Add('type');
  15011. Add(' TArray = array of longint;');
  15012. Add(' TObject = class');
  15013. Add(' FItems: TArray;');
  15014. Add(' function GetItems: tarray;');
  15015. Add(' procedure SetItems(Value: tarray);');
  15016. Add(' property Items: tarray read getitems write setitems;');
  15017. Add(' procedure SetNumbers(const Value: tarray);');
  15018. Add(' property Numbers: tarray write setnumbers;');
  15019. Add(' end;');
  15020. Add('function tobject.getitems: tarray;');
  15021. Add('begin');
  15022. Add(' Result:=fitems;');
  15023. Add('end;');
  15024. Add('procedure tobject.setitems(value: tarray);');
  15025. Add('begin');
  15026. Add(' fitems:=value;');
  15027. Add(' fitems:=nil;');
  15028. Add(' Items:=nil;');
  15029. Add(' Items:=Items;');
  15030. Add(' Items[1]:=2;');
  15031. Add(' fitems[3]:=Items[4];');
  15032. Add(' Items[5]:=Items[6];');
  15033. Add(' Self.Items[7]:=8;');
  15034. Add(' Self.Items[9]:=Self.Items[10];');
  15035. Add(' Items[Items[11]]:=Items[Items[12]];');
  15036. Add('end;');
  15037. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15038. Add('begin;');
  15039. Add(' Numbers:=nil;');
  15040. Add(' Numbers:=Value;');
  15041. Add(' Self.Numbers:=Value;');
  15042. Add('end;');
  15043. Add('var Obj: tobject;');
  15044. Add('begin');
  15045. Add(' obj.items:=nil;');
  15046. Add(' obj.items:=obj.items;');
  15047. Add(' obj.items[11]:=obj.items[12];');
  15048. ConvertProgram;
  15049. CheckSource('TestClass_PropertyOfTypeArray',
  15050. LinesToStr([ // statements
  15051. 'rtl.createClass(this, "TObject", null, function () {',
  15052. ' this.$init = function () {',
  15053. ' this.FItems = [];',
  15054. ' };',
  15055. ' this.$final = function () {',
  15056. ' this.FItems = undefined;',
  15057. ' };',
  15058. ' this.GetItems = function () {',
  15059. ' var Result = [];',
  15060. ' Result = rtl.arrayRef(this.FItems);',
  15061. ' return Result;',
  15062. ' };',
  15063. ' this.SetItems = function (Value) {',
  15064. ' this.FItems = rtl.arrayRef(Value);',
  15065. ' this.FItems = [];',
  15066. ' this.SetItems([]);',
  15067. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15068. ' this.GetItems()[1] = 2;',
  15069. ' this.FItems[3] = this.GetItems()[4];',
  15070. ' this.GetItems()[5] = this.GetItems()[6];',
  15071. ' this.GetItems()[7] = 8;',
  15072. ' this.GetItems()[9] = this.GetItems()[10];',
  15073. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15074. ' };',
  15075. ' this.SetNumbers = function (Value) {',
  15076. ' this.SetNumbers([]);',
  15077. ' this.SetNumbers(Value);',
  15078. ' this.SetNumbers(Value);',
  15079. ' };',
  15080. '});',
  15081. 'this.Obj = null;'
  15082. ]),
  15083. LinesToStr([ // $mod.$main
  15084. '$mod.Obj.SetItems([]);',
  15085. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15086. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15087. ]));
  15088. end;
  15089. procedure TTestModule.TestClass_PropertyDefault;
  15090. begin
  15091. StartProgram(false);
  15092. Add([
  15093. 'type',
  15094. ' TArray = array of longint;',
  15095. ' TObject = class',
  15096. ' end;',
  15097. ' TBird = class',
  15098. ' FItems: TArray;',
  15099. ' function GetItems(Index: longint): longint;',
  15100. ' procedure SetItems(Index, Value: longint);',
  15101. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15102. ' end;',
  15103. 'function TBird.getitems(index: longint): longint;',
  15104. 'begin',
  15105. 'end;',
  15106. 'procedure TBird.setitems(index, value: longint);',
  15107. 'begin',
  15108. ' Self[1]:=2;',
  15109. ' Self[3]:=Self[index];',
  15110. ' Self[index]:=Self[Self[value]];',
  15111. ' Self[Self[4]]:=value;',
  15112. 'end;',
  15113. 'var',
  15114. ' Bird: TBird;',
  15115. ' Obj: TObject;',
  15116. 'begin',
  15117. ' bird[11]:=12;',
  15118. ' bird[13]:=bird[14];',
  15119. ' bird[Bird[15]]:=bird[Bird[15]];',
  15120. ' TBird(obj)[16]:=TBird(obj)[17];',
  15121. ' (obj as tbird)[18]:=19;',
  15122. '']);
  15123. ConvertProgram;
  15124. CheckSource('TestClass_PropertyDefault',
  15125. LinesToStr([ // statements
  15126. 'rtl.createClass(this, "TObject", null, function () {',
  15127. ' this.$init = function () {',
  15128. ' };',
  15129. ' this.$final = function () {',
  15130. ' };',
  15131. '});',
  15132. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15133. ' this.$init = function () {',
  15134. ' $mod.TObject.$init.call(this);',
  15135. ' this.FItems = [];',
  15136. ' };',
  15137. ' this.$final = function () {',
  15138. ' this.FItems = undefined;',
  15139. ' $mod.TObject.$final.call(this);',
  15140. ' };',
  15141. ' this.GetItems = function (Index) {',
  15142. ' var Result = 0;',
  15143. ' return Result;',
  15144. ' };',
  15145. ' this.SetItems = function (Index, Value) {',
  15146. ' this.SetItems(1, 2);',
  15147. ' this.SetItems(3, this.GetItems(Index));',
  15148. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15149. ' this.SetItems(this.GetItems(4), Value);',
  15150. ' };',
  15151. '});',
  15152. 'this.Bird = null;',
  15153. 'this.Obj = null;',
  15154. '']),
  15155. LinesToStr([ // $mod.$main
  15156. '$mod.Bird.SetItems(11, 12);',
  15157. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15158. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15159. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15160. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15161. '']));
  15162. end;
  15163. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15164. begin
  15165. StartProgram(false);
  15166. Add([
  15167. 'type',
  15168. ' TObject = class end;',
  15169. ' TAlphaList = class',
  15170. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15171. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15172. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15173. ' end;',
  15174. ' TBetaList = class',
  15175. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15176. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15177. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15178. ' end;',
  15179. ' TBird = class',
  15180. ' procedure DoIt;',
  15181. ' end;',
  15182. 'procedure TBird.DoIt;',
  15183. 'var',
  15184. ' List: TAlphaList;',
  15185. 'begin',
  15186. ' if TBetaList(List[true])[3]=nil then ;',
  15187. ' TBetaList(List[false])[5]:=nil;',
  15188. 'end;',
  15189. 'var',
  15190. ' List: TAlphaList;',
  15191. 'begin',
  15192. ' if TBetaList(List[true])[3]=nil then ;',
  15193. ' TBetaList(List[false])[5]:=nil;',
  15194. '']);
  15195. ConvertProgram;
  15196. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15197. LinesToStr([ // statements
  15198. 'rtl.createClass(this, "TObject", null, function () {',
  15199. ' this.$init = function () {',
  15200. ' };',
  15201. ' this.$final = function () {',
  15202. ' };',
  15203. '});',
  15204. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15205. '});',
  15206. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15207. '});',
  15208. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15209. ' this.DoIt = function () {',
  15210. ' var List = null;',
  15211. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15212. ' List.GetAlphas(false).SetBetas(5, null);',
  15213. ' };',
  15214. '});',
  15215. 'this.List = null;',
  15216. '']),
  15217. LinesToStr([ // $mod.$main
  15218. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15219. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15220. '']));
  15221. end;
  15222. procedure TTestModule.TestClass_PropertyOverride;
  15223. begin
  15224. StartProgram(false);
  15225. Add('type');
  15226. Add(' integer = longint;');
  15227. Add(' TObject = class');
  15228. Add(' FItem: integer;');
  15229. Add(' function GetItem: integer; external name ''GetItem'';');
  15230. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15231. Add(' property Item: integer read getitem write setitem;');
  15232. Add(' end;');
  15233. Add(' TCar = class');
  15234. Add(' FBag: integer;');
  15235. Add(' function GetBag: integer; external name ''GetBag'';');
  15236. Add(' property Item read getbag;');
  15237. Add(' end;');
  15238. Add('var');
  15239. Add(' Obj: tobject;');
  15240. Add(' Car: tcar;');
  15241. Add('begin');
  15242. Add(' Obj.Item:=Obj.Item;');
  15243. Add(' Car.Item:=Car.Item;');
  15244. ConvertProgram;
  15245. CheckSource('TestClass_PropertyOverride',
  15246. LinesToStr([ // statements
  15247. 'rtl.createClass(this, "TObject", null, function () {',
  15248. ' this.$init = function () {',
  15249. ' this.FItem = 0;',
  15250. ' };',
  15251. ' this.$final = function () {',
  15252. ' };',
  15253. '});',
  15254. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15255. ' this.$init = function () {',
  15256. ' $mod.TObject.$init.call(this);',
  15257. ' this.FBag = 0;',
  15258. ' };',
  15259. '});',
  15260. 'this.Obj = null;',
  15261. 'this.Car = null;',
  15262. '']),
  15263. LinesToStr([ // $mod.$main
  15264. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15265. '$mod.Car.SetItem($mod.Car.GetBag());',
  15266. '']));
  15267. end;
  15268. procedure TTestModule.TestClass_PropertyIncVisibility;
  15269. begin
  15270. AddModuleWithIntfImplSrc('unit1.pp',
  15271. LinesToStr([
  15272. 'type',
  15273. ' TNumber = longint;',
  15274. ' TInteger = longint;',
  15275. ' TObject = class',
  15276. ' private',
  15277. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15278. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15279. ' protected',
  15280. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15281. ' end;']),
  15282. LinesToStr([
  15283. '']));
  15284. StartProgram(true);
  15285. Add([
  15286. 'uses unit1;',
  15287. 'type',
  15288. ' TBird = class',
  15289. ' public',
  15290. ' property Items;',
  15291. ' end;',
  15292. 'procedure DoIt(i: TInteger);',
  15293. 'begin',
  15294. 'end;',
  15295. 'var b: TBird;',
  15296. 'begin',
  15297. ' b.Items[1]:=2;',
  15298. ' b.Items[3]:=b.Items[4];',
  15299. ' DoIt(b.Items[5]);',
  15300. '']);
  15301. ConvertProgram;
  15302. CheckSource('TestClass_PropertyIncVisibility',
  15303. LinesToStr([ // statements
  15304. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15305. '});',
  15306. 'this.DoIt = function (i) {',
  15307. '};',
  15308. 'this.b = null;'
  15309. ]),
  15310. LinesToStr([ // $mod.$main
  15311. '$mod.b.SetItems(1, 2);',
  15312. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15313. '$mod.DoIt($mod.b.GetItems(5));'
  15314. ]));
  15315. end;
  15316. procedure TTestModule.TestClass_Assigned;
  15317. begin
  15318. StartProgram(false);
  15319. Add('type');
  15320. Add(' TObject = class');
  15321. Add(' end;');
  15322. Add('var');
  15323. Add(' Obj: tobject;');
  15324. Add(' b: boolean;');
  15325. Add('begin');
  15326. Add(' if Assigned(obj) then ;');
  15327. Add(' b:=Assigned(obj) or false;');
  15328. ConvertProgram;
  15329. CheckSource('TestClass_Assigned',
  15330. LinesToStr([ // statements
  15331. 'rtl.createClass(this, "TObject", null, function () {',
  15332. ' this.$init = function () {',
  15333. ' };',
  15334. ' this.$final = function () {',
  15335. ' };',
  15336. '});',
  15337. 'this.Obj = null;',
  15338. 'this.b = false;'
  15339. ]),
  15340. LinesToStr([ // $mod.$main
  15341. 'if ($mod.Obj != null);',
  15342. '$mod.b = ($mod.Obj != null) || false;'
  15343. ]));
  15344. end;
  15345. procedure TTestModule.TestClass_WithClassDoCreate;
  15346. begin
  15347. StartProgram(false);
  15348. Add('type');
  15349. Add(' TObject = class');
  15350. Add(' aBool: boolean;');
  15351. Add(' Arr: array of boolean;');
  15352. Add(' constructor Create;');
  15353. Add(' end;');
  15354. Add('constructor TObject.Create; begin end;');
  15355. Add('var');
  15356. Add(' Obj: tobject;');
  15357. Add(' b: boolean;');
  15358. Add('begin');
  15359. Add(' with tobject.create do begin');
  15360. Add(' b:=abool;');
  15361. Add(' abool:=b;');
  15362. Add(' b:=arr[1];');
  15363. Add(' arr[2]:=b;');
  15364. Add(' end;');
  15365. Add(' with tobject do');
  15366. Add(' obj:=create;');
  15367. Add(' with obj do begin');
  15368. Add(' create;');
  15369. Add(' b:=abool;');
  15370. Add(' abool:=b;');
  15371. Add(' b:=arr[3];');
  15372. Add(' arr[4]:=b;');
  15373. Add(' end;');
  15374. ConvertProgram;
  15375. CheckSource('TestClass_WithClassDoCreate',
  15376. LinesToStr([ // statements
  15377. 'rtl.createClass(this, "TObject", null, function () {',
  15378. ' this.$init = function () {',
  15379. ' this.aBool = false;',
  15380. ' this.Arr = [];',
  15381. ' };',
  15382. ' this.$final = function () {',
  15383. ' this.Arr = undefined;',
  15384. ' };',
  15385. ' this.Create = function () {',
  15386. ' return this;',
  15387. ' };',
  15388. '});',
  15389. 'this.Obj = null;',
  15390. 'this.b = false;'
  15391. ]),
  15392. LinesToStr([ // $mod.$main
  15393. 'var $with = $mod.TObject.$create("Create");',
  15394. '$mod.b = $with.aBool;',
  15395. '$with.aBool = $mod.b;',
  15396. '$mod.b = $with.Arr[1];',
  15397. '$with.Arr[2] = $mod.b;',
  15398. 'var $with1 = $mod.TObject;',
  15399. '$mod.Obj = $with1.$create("Create");',
  15400. 'var $with2 = $mod.Obj;',
  15401. '$with2.Create();',
  15402. '$mod.b = $with2.aBool;',
  15403. '$with2.aBool = $mod.b;',
  15404. '$mod.b = $with2.Arr[3];',
  15405. '$with2.Arr[4] = $mod.b;',
  15406. '']));
  15407. end;
  15408. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15409. begin
  15410. StartProgram(false);
  15411. Add('type');
  15412. Add(' TObject = class');
  15413. Add(' FInt: longint;');
  15414. Add(' constructor Create;');
  15415. Add(' function GetSize: longint;');
  15416. Add(' procedure SetSize(Value: longint);');
  15417. Add(' property Int: longint read FInt write FInt;');
  15418. Add(' property Size: longint read GetSize write SetSize;');
  15419. Add(' end;');
  15420. Add('constructor TObject.Create; begin end;');
  15421. Add('function TObject.GetSize: longint; begin; end;');
  15422. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15423. Add('var');
  15424. Add(' Obj: tobject;');
  15425. Add(' i: longint;');
  15426. Add('begin');
  15427. Add(' with TObject.Create do begin');
  15428. Add(' i:=int;');
  15429. Add(' int:=i;');
  15430. Add(' i:=size;');
  15431. Add(' size:=i;');
  15432. Add(' end;');
  15433. Add(' with obj do begin');
  15434. Add(' i:=int;');
  15435. Add(' int:=i;');
  15436. Add(' i:=size;');
  15437. Add(' size:=i;');
  15438. Add(' end;');
  15439. ConvertProgram;
  15440. CheckSource('TestClass_WithClassInstDoProperty',
  15441. LinesToStr([ // statements
  15442. 'rtl.createClass(this, "TObject", null, function () {',
  15443. ' this.$init = function () {',
  15444. ' this.FInt = 0;',
  15445. ' };',
  15446. ' this.$final = function () {',
  15447. ' };',
  15448. ' this.Create = function () {',
  15449. ' return this;',
  15450. ' };',
  15451. ' this.GetSize = function () {',
  15452. ' var Result = 0;',
  15453. ' return Result;',
  15454. ' };',
  15455. ' this.SetSize = function (Value) {',
  15456. ' };',
  15457. '});',
  15458. 'this.Obj = null;',
  15459. 'this.i = 0;'
  15460. ]),
  15461. LinesToStr([ // $mod.$main
  15462. 'var $with = $mod.TObject.$create("Create");',
  15463. '$mod.i = $with.FInt;',
  15464. '$with.FInt = $mod.i;',
  15465. '$mod.i = $with.GetSize();',
  15466. '$with.SetSize($mod.i);',
  15467. 'var $with1 = $mod.Obj;',
  15468. '$mod.i = $with1.FInt;',
  15469. '$with1.FInt = $mod.i;',
  15470. '$mod.i = $with1.GetSize();',
  15471. '$with1.SetSize($mod.i);',
  15472. '']));
  15473. end;
  15474. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15475. begin
  15476. StartProgram(false);
  15477. Add('type');
  15478. Add(' TObject = class');
  15479. Add(' constructor Create;');
  15480. Add(' function GetItems(Index: longint): longint;');
  15481. Add(' procedure SetItems(Index, Value: longint);');
  15482. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15483. Add(' end;');
  15484. Add('constructor TObject.Create; begin end;');
  15485. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15486. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15487. Add('var');
  15488. Add(' Obj: tobject;');
  15489. Add(' i: longint;');
  15490. Add('begin');
  15491. Add(' with TObject.Create do begin');
  15492. Add(' i:=Items[1];');
  15493. Add(' Items[2]:=i;');
  15494. Add(' end;');
  15495. Add(' with obj do begin');
  15496. Add(' i:=Items[3];');
  15497. Add(' Items[4]:=i;');
  15498. Add(' end;');
  15499. ConvertProgram;
  15500. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  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.GetItems = function (Index) {',
  15511. ' var Result = 0;',
  15512. ' return Result;',
  15513. ' };',
  15514. ' this.SetItems = function (Index, 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.GetItems(1);',
  15523. '$with.SetItems(2, $mod.i);',
  15524. 'var $with1 = $mod.Obj;',
  15525. '$mod.i = $with1.GetItems(3);',
  15526. '$with1.SetItems(4, $mod.i);',
  15527. '']));
  15528. end;
  15529. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15530. begin
  15531. StartProgram(false);
  15532. Add('type');
  15533. Add(' TObject = class');
  15534. Add(' constructor Create;');
  15535. Add(' function GetSize: longint;');
  15536. Add(' procedure SetSize(Value: longint);');
  15537. Add(' end;');
  15538. Add('constructor TObject.Create; begin end;');
  15539. Add('function TObject.GetSize: longint; begin; end;');
  15540. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15541. Add('var');
  15542. Add(' Obj: tobject;');
  15543. Add(' i: longint;');
  15544. Add('begin');
  15545. Add(' with TObject.Create do begin');
  15546. Add(' i:=GetSize;');
  15547. Add(' i:=GetSize();');
  15548. Add(' SetSize(i);');
  15549. Add(' end;');
  15550. Add(' with obj do begin');
  15551. Add(' i:=GetSize;');
  15552. Add(' i:=GetSize();');
  15553. Add(' SetSize(i);');
  15554. Add(' end;');
  15555. ConvertProgram;
  15556. CheckSource('TestClass_WithClassInstDoFunc',
  15557. LinesToStr([ // statements
  15558. 'rtl.createClass(this, "TObject", null, function () {',
  15559. ' this.$init = function () {',
  15560. ' };',
  15561. ' this.$final = function () {',
  15562. ' };',
  15563. ' this.Create = function () {',
  15564. ' return this;',
  15565. ' };',
  15566. ' this.GetSize = function () {',
  15567. ' var Result = 0;',
  15568. ' return Result;',
  15569. ' };',
  15570. ' this.SetSize = function (Value) {',
  15571. ' };',
  15572. '});',
  15573. 'this.Obj = null;',
  15574. 'this.i = 0;'
  15575. ]),
  15576. LinesToStr([ // $mod.$main
  15577. 'var $with = $mod.TObject.$create("Create");',
  15578. '$mod.i = $with.GetSize();',
  15579. '$mod.i = $with.GetSize();',
  15580. '$with.SetSize($mod.i);',
  15581. 'var $with1 = $mod.Obj;',
  15582. '$mod.i = $with1.GetSize();',
  15583. '$mod.i = $with1.GetSize();',
  15584. '$with1.SetSize($mod.i);',
  15585. '']));
  15586. end;
  15587. procedure TTestModule.TestClass_TypeCast;
  15588. begin
  15589. StartProgram(false);
  15590. Add('type');
  15591. Add(' TObject = class');
  15592. Add(' Next: TObject;');
  15593. Add(' constructor Create;');
  15594. Add(' end;');
  15595. Add(' TControl = class(TObject)');
  15596. Add(' Arr: array of TObject;');
  15597. Add(' function GetIt(vI: longint = 0): TObject;');
  15598. Add(' end;');
  15599. Add('constructor tobject.create; begin end;');
  15600. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15601. Add('var');
  15602. Add(' Obj: tobject;');
  15603. Add('begin');
  15604. Add(' obj:=tcontrol(obj).next;');
  15605. Add(' tcontrol(obj):=nil;');
  15606. Add(' obj:=tcontrol(obj);');
  15607. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15608. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15609. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15610. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15611. Add(' obj:=tcontrol(nil);');
  15612. ConvertProgram;
  15613. CheckSource('TestClass_TypeCast',
  15614. LinesToStr([ // statements
  15615. 'rtl.createClass(this, "TObject", null, function () {',
  15616. ' this.$init = function () {',
  15617. ' this.Next = null;',
  15618. ' };',
  15619. ' this.$final = function () {',
  15620. ' this.Next = undefined;',
  15621. ' };',
  15622. ' this.Create = function () {',
  15623. ' return this;',
  15624. ' };',
  15625. '});',
  15626. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15627. ' this.$init = function () {',
  15628. ' $mod.TObject.$init.call(this);',
  15629. ' this.Arr = [];',
  15630. ' };',
  15631. ' this.$final = function () {',
  15632. ' this.Arr = undefined;',
  15633. ' $mod.TObject.$final.call(this);',
  15634. ' };',
  15635. ' this.GetIt = function (vI) {',
  15636. ' var Result = null;',
  15637. ' return Result;',
  15638. ' };',
  15639. '});',
  15640. 'this.Obj = null;'
  15641. ]),
  15642. LinesToStr([ // $mod.$main
  15643. '$mod.Obj = $mod.Obj.Next;',
  15644. '$mod.Obj = null;',
  15645. '$mod.Obj = $mod.Obj;',
  15646. '$mod.Obj = $mod.Obj.GetIt(0);',
  15647. '$mod.Obj = $mod.Obj.GetIt(0);',
  15648. '$mod.Obj = $mod.Obj.GetIt(1);',
  15649. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15650. '$mod.Obj = null;',
  15651. '']));
  15652. end;
  15653. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15654. begin
  15655. StartProgram(false);
  15656. Add('type');
  15657. Add(' TObject = class end;');
  15658. Add('procedure ProcA(var A);');
  15659. Add('begin');
  15660. Add(' TObject(A):=nil;');
  15661. Add(' TObject(A):=TObject(A);');
  15662. Add(' if TObject(A)=nil then ;');
  15663. Add(' if nil=TObject(A) then ;');
  15664. Add('end;');
  15665. Add('procedure ProcB(out A);');
  15666. Add('begin');
  15667. Add(' TObject(A):=nil;');
  15668. Add(' TObject(A):=TObject(A);');
  15669. Add(' if TObject(A)=nil then ;');
  15670. Add(' if nil=TObject(A) then ;');
  15671. Add('end;');
  15672. Add('procedure ProcC(const A);');
  15673. Add('begin');
  15674. Add(' if TObject(A)=nil then ;');
  15675. Add(' if nil=TObject(A) then ;');
  15676. Add('end;');
  15677. Add('var o: TObject;');
  15678. Add('begin');
  15679. Add(' ProcA(o);');
  15680. Add(' ProcB(o);');
  15681. Add(' ProcC(o);');
  15682. ConvertProgram;
  15683. CheckSource('TestClass_TypeCastUntypedParam',
  15684. LinesToStr([ // statements
  15685. 'rtl.createClass(this, "TObject", null, function () {',
  15686. ' this.$init = function () {',
  15687. ' };',
  15688. ' this.$final = function () {',
  15689. ' };',
  15690. '});',
  15691. 'this.ProcA = function (A) {',
  15692. ' A.set(null);',
  15693. ' A.set(A.get());',
  15694. ' if (A.get() === null);',
  15695. ' if (null === A.get());',
  15696. '};',
  15697. 'this.ProcB = function (A) {',
  15698. ' A.set(null);',
  15699. ' A.set(A.get());',
  15700. ' if (A.get() === null);',
  15701. ' if (null === A.get());',
  15702. '};',
  15703. 'this.ProcC = function (A) {',
  15704. ' if (A === null);',
  15705. ' if (null === A);',
  15706. '};',
  15707. 'this.o = null;',
  15708. '']),
  15709. LinesToStr([ // $mod.$main
  15710. '$mod.ProcA({',
  15711. ' p: $mod,',
  15712. ' get: function () {',
  15713. ' return this.p.o;',
  15714. ' },',
  15715. ' set: function (v) {',
  15716. ' this.p.o = v;',
  15717. ' }',
  15718. '});',
  15719. '$mod.ProcB({',
  15720. ' p: $mod,',
  15721. ' get: function () {',
  15722. ' return this.p.o;',
  15723. ' },',
  15724. ' set: function (v) {',
  15725. ' this.p.o = v;',
  15726. ' }',
  15727. '});',
  15728. '$mod.ProcC($mod.o);',
  15729. '']));
  15730. end;
  15731. procedure TTestModule.TestClass_Overloads;
  15732. begin
  15733. StartProgram(false);
  15734. Add('type');
  15735. Add(' TObject = class');
  15736. Add(' procedure DoIt;');
  15737. Add(' procedure DoIt(vI: longint);');
  15738. Add(' end;');
  15739. Add('procedure TObject.DoIt;');
  15740. Add('begin');
  15741. Add(' DoIt;');
  15742. Add(' DoIt(1);');
  15743. Add('end;');
  15744. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15745. Add('begin');
  15746. ConvertProgram;
  15747. CheckSource('TestClass_Overloads',
  15748. LinesToStr([ // statements
  15749. 'rtl.createClass(this, "TObject", null, function () {',
  15750. ' this.$init = function () {',
  15751. ' };',
  15752. ' this.$final = function () {',
  15753. ' };',
  15754. ' this.DoIt = function () {',
  15755. ' this.DoIt();',
  15756. ' this.DoIt$1(1);',
  15757. ' };',
  15758. ' this.DoIt$1 = function (vI) {',
  15759. ' };',
  15760. '});',
  15761. '']),
  15762. LinesToStr([ // $mod.$main
  15763. '']));
  15764. end;
  15765. procedure TTestModule.TestClass_OverloadsAncestor;
  15766. begin
  15767. StartProgram(false);
  15768. Add('type');
  15769. Add(' TObject = class;');
  15770. Add(' TObject = class');
  15771. Add(' procedure DoIt(vA: longint);');
  15772. Add(' procedure DoIt(vA, vB: longint);');
  15773. Add(' end;');
  15774. Add(' TCar = class;');
  15775. Add(' TCar = class');
  15776. Add(' procedure DoIt(vA: longint);');
  15777. Add(' procedure DoIt(vA, vB: longint);');
  15778. Add(' end;');
  15779. Add('procedure tobject.doit(va: longint);');
  15780. Add('begin');
  15781. Add(' doit(1);');
  15782. Add(' doit(1,2);');
  15783. Add('end;');
  15784. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15785. Add('procedure tcar.doit(va: longint);');
  15786. Add('begin');
  15787. Add(' doit(1);');
  15788. Add(' doit(1,2);');
  15789. Add(' inherited doit(1);');
  15790. Add(' inherited doit(1,2);');
  15791. Add('end;');
  15792. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15793. Add('begin');
  15794. ConvertProgram;
  15795. CheckSource('TestClass_OverloadsAncestor',
  15796. LinesToStr([ // statements
  15797. 'rtl.createClass(this, "TObject", null, function () {',
  15798. ' this.$init = function () {',
  15799. ' };',
  15800. ' this.$final = function () {',
  15801. ' };',
  15802. ' this.DoIt = function (vA) {',
  15803. ' this.DoIt(1);',
  15804. ' this.DoIt$1(1,2);',
  15805. ' };',
  15806. ' this.DoIt$1 = function (vA, vB) {',
  15807. ' };',
  15808. '});',
  15809. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15810. ' this.DoIt$2 = function (vA) {',
  15811. ' this.DoIt$2(1);',
  15812. ' this.DoIt$3(1, 2);',
  15813. ' $mod.TObject.DoIt.call(this, 1);',
  15814. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15815. ' };',
  15816. ' this.DoIt$3 = function (vA, vB) {',
  15817. ' };',
  15818. '});',
  15819. '']),
  15820. LinesToStr([ // $mod.$main
  15821. '']));
  15822. end;
  15823. procedure TTestModule.TestClass_OverloadConstructor;
  15824. begin
  15825. StartProgram(false);
  15826. Add('type');
  15827. Add(' TObject = class');
  15828. Add(' constructor Create(vA: longint);');
  15829. Add(' constructor Create(vA, vB: longint);');
  15830. Add(' end;');
  15831. Add(' TCar = class');
  15832. Add(' constructor Create(vA: longint);');
  15833. Add(' constructor Create(vA, vB: longint);');
  15834. Add(' end;');
  15835. Add('constructor tobject.create(va: longint);');
  15836. Add('begin');
  15837. Add(' create(1);');
  15838. Add(' create(1,2);');
  15839. Add('end;');
  15840. Add('constructor tobject.create(va, vb: longint); begin end;');
  15841. Add('constructor tcar.create(va: longint);');
  15842. Add('begin');
  15843. Add(' create(1);');
  15844. Add(' create(1,2);');
  15845. Add(' inherited create(1);');
  15846. Add(' inherited create(1,2);');
  15847. Add('end;');
  15848. Add('constructor tcar.create(va, vb: longint); begin end;');
  15849. Add('begin');
  15850. Add(' tobject.create(1);');
  15851. Add(' tobject.create(1,2);');
  15852. Add(' tcar.create(1);');
  15853. Add(' tcar.create(1,2);');
  15854. ConvertProgram;
  15855. CheckSource('TestClass_OverloadConstructor',
  15856. LinesToStr([ // statements
  15857. 'rtl.createClass(this, "TObject", null, function () {',
  15858. ' this.$init = function () {',
  15859. ' };',
  15860. ' this.$final = function () {',
  15861. ' };',
  15862. ' this.Create = function (vA) {',
  15863. ' this.Create(1);',
  15864. ' this.Create$1(1,2);',
  15865. ' return this;',
  15866. ' };',
  15867. ' this.Create$1 = function (vA, vB) {',
  15868. ' return this;',
  15869. ' };',
  15870. '});',
  15871. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15872. ' this.Create$2 = function (vA) {',
  15873. ' this.Create$2(1);',
  15874. ' this.Create$3(1, 2);',
  15875. ' $mod.TObject.Create.call(this, 1);',
  15876. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15877. ' return this;',
  15878. ' };',
  15879. ' this.Create$3 = function (vA, vB) {',
  15880. ' return this;',
  15881. ' };',
  15882. '});',
  15883. '']),
  15884. LinesToStr([ // $mod.$main
  15885. '$mod.TObject.$create("Create", [1]);',
  15886. '$mod.TObject.$create("Create$1", [1, 2]);',
  15887. '$mod.TCar.$create("Create$2", [1]);',
  15888. '$mod.TCar.$create("Create$3", [1, 2]);',
  15889. '']));
  15890. end;
  15891. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15892. begin
  15893. StartProgram(false);
  15894. Add([
  15895. '{$mode delphi}',
  15896. 'type',
  15897. ' TObject = class end;',
  15898. ' TBird = class',
  15899. ' function {#a}GetValue: longint; overload; virtual;',
  15900. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15901. ' end;',
  15902. ' TEagle = class(TBird)',
  15903. ' function {#c}GetValue: longint; overload; override;',
  15904. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15905. ' end;',
  15906. 'function TBird.GetValue: longint;',
  15907. 'begin',
  15908. ' if 3={@a}GetValue then ;',
  15909. ' if 4={@b}GetValue(5) then ;',
  15910. 'end;',
  15911. 'function TBird.GetValue(AValue: longint): longint;',
  15912. 'begin',
  15913. 'end;',
  15914. 'function TEagle.GetValue: longint;',
  15915. 'begin',
  15916. ' if 13={@c}GetValue then ;',
  15917. ' if 14={@d}GetValue(15) then ;',
  15918. ' if 15=inherited {@a}GetValue then ;',
  15919. ' if 16=inherited {@b}GetValue(17) then ;',
  15920. 'end;',
  15921. 'function TEagle.GetValue(AValue: longint): longint;',
  15922. 'begin',
  15923. 'end;',
  15924. 'var',
  15925. ' e: TEagle;',
  15926. 'begin',
  15927. ' if 23=e.{@c}GetValue then ;',
  15928. ' if 24=e.{@d}GetValue(25) then ;']);
  15929. ConvertProgram;
  15930. CheckSource('TestClass_OverloadDelphiOverride',
  15931. LinesToStr([ // statements
  15932. 'rtl.createClass(this, "TObject", null, function () {',
  15933. ' this.$init = function () {',
  15934. ' };',
  15935. ' this.$final = function () {',
  15936. ' };',
  15937. '});',
  15938. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15939. ' this.GetValue = function () {',
  15940. ' var Result = 0;',
  15941. ' if (3 === this.GetValue()) ;',
  15942. ' if (4 === this.GetValue$1(5)) ;',
  15943. ' return Result;',
  15944. ' };',
  15945. ' this.GetValue$1 = function (AValue) {',
  15946. ' var Result = 0;',
  15947. ' return Result;',
  15948. ' };',
  15949. '});',
  15950. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15951. ' this.GetValue = function () {',
  15952. ' var Result = 0;',
  15953. ' if (13 === this.GetValue()) ;',
  15954. ' if (14 === this.GetValue$1(15)) ;',
  15955. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  15956. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  15957. ' return Result;',
  15958. ' };',
  15959. ' this.GetValue$1 = function (AValue) {',
  15960. ' var Result = 0;',
  15961. ' return Result;',
  15962. ' };',
  15963. '});',
  15964. 'this.e = null;',
  15965. '']),
  15966. LinesToStr([ // $mod.$main
  15967. 'if (23 === $mod.e.GetValue()) ;',
  15968. 'if (24 === $mod.e.GetValue$1(25)) ;',
  15969. '']));
  15970. end;
  15971. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  15972. begin
  15973. StartProgram(false);
  15974. Add([
  15975. '{$mode delphi}',
  15976. 'type',
  15977. ' TObject = class end;',
  15978. ' TAnimal = class',
  15979. ' public',
  15980. ' {#animal_a}A: longint;',
  15981. ' function {#animal_b}B: longint;',
  15982. ' end;',
  15983. ' TBird = class(TAnimal)',
  15984. ' public',
  15985. ' {#bird_a}A: double;',
  15986. ' {#bird_b}B: boolean;',
  15987. ' end;',
  15988. ' TEagle = class(TBird)',
  15989. ' public',
  15990. ' function {#eagle_a}A: boolean;',
  15991. ' {#eagle_b}B: double;',
  15992. ' end;',
  15993. 'function TAnimal.B: longint;',
  15994. 'begin',
  15995. 'end;',
  15996. 'function TEagle.A: boolean;',
  15997. 'begin',
  15998. ' {@eagle_b}B:=3.3;',
  15999. ' {@eagle_a}A();',
  16000. ' TBird(Self).{@bird_b}B:=true;',
  16001. ' TAnimal(Self).{@animal_a}A:=17;',
  16002. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16003. 'end;',
  16004. 'var',
  16005. ' e: TEagle;',
  16006. 'begin',
  16007. ' e.{@eagle_b}B:=5.3;',
  16008. ' if e.{@eagle_a}A then ;',
  16009. '']);
  16010. ConvertProgram;
  16011. CheckSource('TestClass_ReintroduceVarDelphi',
  16012. LinesToStr([ // statements
  16013. 'rtl.createClass(this, "TObject", null, function () {',
  16014. ' this.$init = function () {',
  16015. ' };',
  16016. ' this.$final = function () {',
  16017. ' };',
  16018. '});',
  16019. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16020. ' this.$init = function () {',
  16021. ' $mod.TObject.$init.call(this);',
  16022. ' this.A = 0;',
  16023. ' };',
  16024. ' this.B = function () {',
  16025. ' var Result = 0;',
  16026. ' return Result;',
  16027. ' };',
  16028. '});',
  16029. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16030. ' this.$init = function () {',
  16031. ' $mod.TAnimal.$init.call(this);',
  16032. ' this.A$1 = 0.0;',
  16033. ' this.B$1 = false;',
  16034. ' };',
  16035. '});',
  16036. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16037. ' this.$init = function () {',
  16038. ' $mod.TBird.$init.call(this);',
  16039. ' this.B$2 = 0.0;',
  16040. ' };',
  16041. ' this.A$2 = function () {',
  16042. ' var Result = false;',
  16043. ' this.B$2 = 3.3;',
  16044. ' this.A$2();',
  16045. ' this.B$1 = true;',
  16046. ' this.A = 17;',
  16047. ' this.B$1 = this.A$1 > 1;',
  16048. ' return Result;',
  16049. ' };',
  16050. '});',
  16051. 'this.e = null;',
  16052. '']),
  16053. LinesToStr([ // $mod.$main
  16054. '$mod.e.B$2 = 5.3;',
  16055. 'if ($mod.e.A$2()) ;',
  16056. '']));
  16057. end;
  16058. procedure TTestModule.TestClass_ReintroducedVar;
  16059. begin
  16060. StartProgram(false);
  16061. Add('type');
  16062. Add(' TObject = class');
  16063. Add(' strict private');
  16064. Add(' Some: longint;');
  16065. Add(' end;');
  16066. Add(' TMobile = class');
  16067. Add(' strict private');
  16068. Add(' Some: string;');
  16069. Add(' end;');
  16070. Add(' TCar = class(tmobile)');
  16071. Add(' procedure Some;');
  16072. Add(' procedure Some(vA: longint);');
  16073. Add(' end;');
  16074. Add('procedure tcar.some;');
  16075. Add('begin');
  16076. Add(' Some;');
  16077. Add(' Some(1);');
  16078. Add('end;');
  16079. Add('procedure tcar.some(va: longint); begin end;');
  16080. Add('begin');
  16081. ConvertProgram;
  16082. CheckSource('TestClass_ReintroducedVar',
  16083. LinesToStr([ // statements
  16084. 'rtl.createClass(this, "TObject", null, function () {',
  16085. ' this.$init = function () {',
  16086. ' this.Some = 0;',
  16087. ' };',
  16088. ' this.$final = function () {',
  16089. ' };',
  16090. '});',
  16091. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16092. ' this.$init = function () {',
  16093. ' $mod.TObject.$init.call(this);',
  16094. ' this.Some$1 = "";',
  16095. ' };',
  16096. '});',
  16097. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16098. ' this.Some$2 = function () {',
  16099. ' this.Some$2();',
  16100. ' this.Some$3(1);',
  16101. ' };',
  16102. ' this.Some$3 = function (vA) {',
  16103. ' };',
  16104. '});',
  16105. '']),
  16106. LinesToStr([ // $mod.$main
  16107. '']));
  16108. end;
  16109. procedure TTestModule.TestClass_RaiseDescendant;
  16110. begin
  16111. StartProgram(false);
  16112. Add([
  16113. 'type',
  16114. ' TObject = class',
  16115. ' constructor Create(Msg: string);',
  16116. ' end;',
  16117. ' Exception = class',
  16118. ' end;',
  16119. ' EConvertError = class(Exception)',
  16120. ' end;',
  16121. 'constructor TObject.Create(Msg: string); begin end;',
  16122. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16123. 'begin',
  16124. ' raise Exception.Create(''Bar1'');',
  16125. ' raise EConvertError.Create(''Bar2'');',
  16126. ' raise AssertConv(''Bar2'');',
  16127. ' raise AssertConv;',
  16128. '']);
  16129. ConvertProgram;
  16130. CheckSource('TestClass_RaiseDescendant',
  16131. LinesToStr([ // statements
  16132. 'rtl.createClass(this, "TObject", null, function () {',
  16133. ' this.$init = function () {',
  16134. ' };',
  16135. ' this.$final = function () {',
  16136. ' };',
  16137. ' this.Create = function (Msg) {',
  16138. ' return this;',
  16139. ' };',
  16140. '});',
  16141. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16142. '});',
  16143. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16144. '});',
  16145. 'this.AssertConv = function (Msg) {',
  16146. ' var Result = null;',
  16147. ' return Result;',
  16148. '};',
  16149. '']),
  16150. LinesToStr([ // $mod.$main
  16151. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16152. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16153. 'throw $mod.AssertConv("Bar2");',
  16154. 'throw $mod.AssertConv("def");',
  16155. '']));
  16156. end;
  16157. procedure TTestModule.TestClass_ExternalMethod;
  16158. begin
  16159. AddModuleWithIntfImplSrc('unit2.pas',
  16160. LinesToStr([
  16161. 'type',
  16162. ' TObject = class',
  16163. ' public',
  16164. ' procedure Intern; external name ''$DoIntern'';',
  16165. ' end;',
  16166. '']),
  16167. LinesToStr([
  16168. '']));
  16169. StartUnit(true);
  16170. Add('interface');
  16171. Add('uses unit2;');
  16172. Add('type');
  16173. Add(' TCar = class(TObject)');
  16174. Add(' public');
  16175. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16176. Add(' procedure DoIt;');
  16177. Add(' end;');
  16178. Add('implementation');
  16179. Add('procedure tcar.doit;');
  16180. Add('begin');
  16181. Add(' Intern;');
  16182. Add(' Intern();');
  16183. Add(' Intern2;');
  16184. Add(' Intern2();');
  16185. Add('end;');
  16186. Add('var Obj: TCar;');
  16187. Add('begin');
  16188. Add(' obj.intern;');
  16189. Add(' obj.intern();');
  16190. Add(' obj.intern2;');
  16191. Add(' obj.intern2();');
  16192. Add(' obj.doit;');
  16193. Add(' obj.doit();');
  16194. Add(' with obj do begin');
  16195. Add(' Intern;');
  16196. Add(' Intern();');
  16197. Add(' Intern2;');
  16198. Add(' Intern2();');
  16199. Add(' end;');
  16200. ConvertUnit;
  16201. CheckSource('TestClass_ExternalMethod',
  16202. LinesToStr([
  16203. 'var $impl = $mod.$impl;',
  16204. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16205. ' this.DoIt = function () {',
  16206. ' this.$DoIntern();',
  16207. ' this.$DoIntern();',
  16208. ' this.$DoIntern2();',
  16209. ' this.$DoIntern2();',
  16210. ' };',
  16211. ' });',
  16212. '']),
  16213. LinesToStr([ // this.$init
  16214. '$impl.Obj.$DoIntern();',
  16215. '$impl.Obj.$DoIntern();',
  16216. '$impl.Obj.$DoIntern2();',
  16217. '$impl.Obj.$DoIntern2();',
  16218. '$impl.Obj.DoIt();',
  16219. '$impl.Obj.DoIt();',
  16220. 'var $with = $impl.Obj;',
  16221. '$with.$DoIntern();',
  16222. '$with.$DoIntern();',
  16223. '$with.$DoIntern2();',
  16224. '$with.$DoIntern2();',
  16225. '']),
  16226. LinesToStr([ // implementation
  16227. '$impl.Obj = null;',
  16228. '']) );
  16229. end;
  16230. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16231. begin
  16232. StartProgram(false);
  16233. Add('type');
  16234. Add(' TObject = class');
  16235. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16236. Add(' end;');
  16237. Add('begin');
  16238. SetExpectedPasResolverError('Virtual method name must match external',
  16239. nVirtualMethodNameMustMatchExternal);
  16240. ConvertProgram;
  16241. end;
  16242. procedure TTestModule.TestClass_ExternalOverrideFail;
  16243. begin
  16244. StartProgram(false);
  16245. Add('type');
  16246. Add(' TObject = class');
  16247. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16248. Add(' end;');
  16249. Add(' TCar = class');
  16250. Add(' procedure DoIt; override; external name ''DoIt'';');
  16251. Add(' end;');
  16252. Add('begin');
  16253. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16254. nInvalidXModifierY);
  16255. ConvertProgram;
  16256. end;
  16257. procedure TTestModule.TestClass_ExternalVar;
  16258. begin
  16259. AddModuleWithIntfImplSrc('unit2.pas',
  16260. LinesToStr([
  16261. '{$modeswitch externalclass}',
  16262. 'type',
  16263. ' TObject = class',
  16264. ' public',
  16265. ' Intern: longint external name ''$Intern'';',
  16266. ' Bracket: longint external name ''["A B"]'';',
  16267. ' end;',
  16268. '']),
  16269. LinesToStr([
  16270. '']));
  16271. StartUnit(true);
  16272. Add([
  16273. 'interface',
  16274. 'uses unit2;',
  16275. '{$modeswitch externalclass}',
  16276. 'type',
  16277. ' TCar = class(tobject)',
  16278. ' public',
  16279. ' Intern2: longint external name ''$Intern2'';',
  16280. ' procedure DoIt;',
  16281. ' end;',
  16282. 'implementation',
  16283. 'procedure tcar.doit;',
  16284. 'begin',
  16285. ' Intern:=Intern+1;',
  16286. ' Intern2:=Intern2+2;',
  16287. ' Bracket:=Bracket+3;',
  16288. 'end;',
  16289. 'var Obj: TCar;',
  16290. 'begin',
  16291. ' obj.intern:=obj.intern+1;',
  16292. ' obj.intern2:=obj.intern2+2;',
  16293. ' obj.Bracket:=obj.Bracket+3;',
  16294. ' with obj do begin',
  16295. ' intern:=intern+1;',
  16296. ' intern2:=intern2+2;',
  16297. ' Bracket:=Bracket+3;',
  16298. ' end;']);
  16299. ConvertUnit;
  16300. CheckSource('TestClass_ExternalVar',
  16301. LinesToStr([
  16302. 'var $impl = $mod.$impl;',
  16303. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16304. ' this.DoIt = function () {',
  16305. ' this.$Intern = this.$Intern + 1;',
  16306. ' this.$Intern2 = this.$Intern2 + 2;',
  16307. ' this["A B"] = this["A B"] + 3;',
  16308. ' };',
  16309. ' });',
  16310. '']),
  16311. LinesToStr([
  16312. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16313. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16314. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16315. 'var $with = $impl.Obj;',
  16316. '$with.$Intern = $with.$Intern + 1;',
  16317. '$with.$Intern2 = $with.$Intern2 + 2;',
  16318. '$with["A B"] = $with["A B"] + 3;',
  16319. '']),
  16320. LinesToStr([ // implementation
  16321. '$impl.Obj = null;',
  16322. '']));
  16323. end;
  16324. procedure TTestModule.TestClass_Const;
  16325. begin
  16326. StartProgram(false);
  16327. Add([
  16328. 'type',
  16329. ' integer = longint;',
  16330. ' TClass = class of TObject;',
  16331. ' TObject = class',
  16332. ' public',
  16333. ' const cI: integer = 3;',
  16334. ' procedure DoIt;',
  16335. ' class procedure DoMore;',
  16336. ' end;',
  16337. 'procedure tobject.doit;',
  16338. 'begin',
  16339. ' if cI=4 then;',
  16340. ' if 5=cI then;',
  16341. ' if Self.cI=6 then;',
  16342. ' if 7=Self.cI then;',
  16343. ' with Self do begin',
  16344. ' if cI=11 then;',
  16345. ' if 12=cI then;',
  16346. ' end;',
  16347. 'end;',
  16348. 'class procedure tobject.domore;',
  16349. 'begin',
  16350. ' if cI=8 then;',
  16351. ' if Self.cI=9 then;',
  16352. ' if 10=cI then;',
  16353. ' if 11=Self.cI then;',
  16354. ' with Self do begin',
  16355. ' if cI=13 then;',
  16356. ' if 14=cI then;',
  16357. ' end;',
  16358. 'end;',
  16359. 'var',
  16360. ' Obj: TObject;',
  16361. ' Cla: TClass;',
  16362. 'begin',
  16363. ' if TObject.cI=21 then ;',
  16364. ' if Obj.cI=22 then ;',
  16365. ' if Cla.cI=23 then ;',
  16366. ' with obj do if ci=24 then;',
  16367. ' with TObject do if ci=25 then;',
  16368. ' with Cla do if ci=26 then;']);
  16369. ConvertProgram;
  16370. CheckSource('TestClass_Const',
  16371. LinesToStr([
  16372. 'rtl.createClass(this, "TObject", null, function () {',
  16373. ' this.cI = 3;',
  16374. ' this.$init = function () {',
  16375. ' };',
  16376. ' this.$final = function () {',
  16377. ' };',
  16378. ' this.DoIt = function () {',
  16379. ' if (this.cI === 4) ;',
  16380. ' if (5 === this.cI) ;',
  16381. ' if (this.cI === 6) ;',
  16382. ' if (7 === this.cI) ;',
  16383. ' if (this.cI === 11) ;',
  16384. ' if (12 === this.cI) ;',
  16385. ' };',
  16386. ' this.DoMore = function () {',
  16387. ' if (this.cI === 8) ;',
  16388. ' if (this.cI === 9) ;',
  16389. ' if (10 === this.cI) ;',
  16390. ' if (11 === this.cI) ;',
  16391. ' if (this.cI === 13) ;',
  16392. ' if (14 === this.cI) ;',
  16393. ' };',
  16394. '});',
  16395. 'this.Obj = null;',
  16396. 'this.Cla = null;',
  16397. '']),
  16398. LinesToStr([
  16399. 'if ($mod.TObject.cI === 21) ;',
  16400. 'if ($mod.Obj.cI === 22) ;',
  16401. 'if ($mod.Cla.cI === 23) ;',
  16402. 'var $with = $mod.Obj;',
  16403. 'if ($with.cI === 24) ;',
  16404. 'var $with1 = $mod.TObject;',
  16405. 'if ($with1.cI === 25) ;',
  16406. 'var $with2 = $mod.Cla;',
  16407. 'if ($with2.cI === 26) ;',
  16408. '']));
  16409. end;
  16410. procedure TTestModule.TestClass_ConstEnum;
  16411. begin
  16412. StartProgram(false);
  16413. Add([
  16414. 'type',
  16415. ' TEnum = (red,blue);',
  16416. ' TObject = class',
  16417. ' end;',
  16418. ' TAnimal = class',
  16419. ' public',
  16420. ' type TSubEnum = (light,dark);',
  16421. ' const a = high(TEnum);',
  16422. ' const b = high(TSubEnum);',
  16423. ' end;',
  16424. ' TBird = class(TAnimal)',
  16425. ' public',
  16426. ' const c = high(TEnum);',
  16427. ' const d = high(TSubEnum);',
  16428. ' end;',
  16429. ' TAnt = class',
  16430. ' public',
  16431. ' const e = high(TEnum);',
  16432. ' const f = high(TBird.TSubEnum);',
  16433. ' end;',
  16434. 'begin',
  16435. '']);
  16436. ConvertProgram;
  16437. CheckSource('TestClass_ConstEnum',
  16438. LinesToStr([
  16439. 'this.TEnum = {',
  16440. ' "0": "red",',
  16441. ' red: 0,',
  16442. ' "1": "blue",',
  16443. ' blue: 1',
  16444. '};',
  16445. 'rtl.createClass(this, "TObject", null, function () {',
  16446. ' this.$init = function () {',
  16447. ' };',
  16448. ' this.$final = function () {',
  16449. ' };',
  16450. '});',
  16451. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16452. ' this.TSubEnum = {',
  16453. ' "0": "light",',
  16454. ' light: 0,',
  16455. ' "1": "dark",',
  16456. ' dark: 1',
  16457. ' };',
  16458. ' this.a = $mod.TEnum.blue;',
  16459. ' this.b = this.TSubEnum.dark;',
  16460. '});',
  16461. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16462. ' this.c = $mod.TEnum.blue;',
  16463. ' this.d = this.TSubEnum.dark;',
  16464. '});',
  16465. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16466. ' this.e = $mod.TEnum.blue;',
  16467. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16468. '});',
  16469. '']),
  16470. LinesToStr([
  16471. '']));
  16472. end;
  16473. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16474. begin
  16475. StartProgram(false);
  16476. Add([
  16477. 'type',
  16478. ' TObject = class',
  16479. ' const cI: longint = 3;',
  16480. ' procedure Fly;',
  16481. ' procedure Run;',
  16482. ' end;',
  16483. ' TBird = class',
  16484. ' procedure Go;',
  16485. ' end;',
  16486. 'procedure tobject.fly;',
  16487. 'const cI: word = 4;',
  16488. 'begin',
  16489. ' if cI=Self.cI then ;',
  16490. 'end;',
  16491. 'procedure tobject.run;',
  16492. 'const cI: word = 5;',
  16493. 'begin',
  16494. ' if cI=Self.cI then ;',
  16495. 'end;',
  16496. 'procedure tbird.go;',
  16497. 'const cI: word = 6;',
  16498. 'begin',
  16499. ' if cI=Self.cI then ;',
  16500. 'end;',
  16501. 'begin',
  16502. '']);
  16503. ConvertProgram;
  16504. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16505. LinesToStr([
  16506. 'rtl.createClass(this, "TObject", null, function () {',
  16507. ' this.cI = 3;',
  16508. ' this.$init = function () {',
  16509. ' };',
  16510. ' this.$final = function () {',
  16511. ' };',
  16512. ' var cI$1 = 4;',
  16513. ' this.Fly = function () {',
  16514. ' if (cI$1 === this.cI) ;',
  16515. ' };',
  16516. ' var cI$2 = 5;',
  16517. ' this.Run = function () {',
  16518. ' if (cI$2 === this.cI) ;',
  16519. ' };',
  16520. '});',
  16521. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16522. ' var cI$3 = 6;',
  16523. ' this.Go = function () {',
  16524. ' if (cI$3 === this.cI) ;',
  16525. ' };',
  16526. '});',
  16527. '']),
  16528. LinesToStr([
  16529. '']));
  16530. end;
  16531. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16532. begin
  16533. StartUnit(false);
  16534. Add([
  16535. 'interface',
  16536. 'type',
  16537. ' TObject = class',
  16538. ' const cI: longint = 3;',
  16539. ' procedure Fly;',
  16540. ' procedure Run;',
  16541. ' end;',
  16542. ' TBird = class',
  16543. ' procedure Go;',
  16544. ' end;',
  16545. 'implementation',
  16546. 'procedure tobject.fly;',
  16547. 'const cI: word = 4;',
  16548. 'begin',
  16549. ' if cI=Self.cI then ;',
  16550. 'end;',
  16551. 'procedure tobject.run;',
  16552. 'const cI: word = 5;',
  16553. 'begin',
  16554. ' if cI=Self.cI then ;',
  16555. 'end;',
  16556. 'procedure tbird.go;',
  16557. 'const cI: word = 6;',
  16558. 'begin',
  16559. ' if cI=Self.cI then ;',
  16560. 'end;',
  16561. '']);
  16562. ConvertUnit;
  16563. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16564. LinesToStr([
  16565. 'rtl.createClass(this, "TObject", null, function () {',
  16566. ' this.cI = 3;',
  16567. ' this.$init = function () {',
  16568. ' };',
  16569. ' this.$final = function () {',
  16570. ' };',
  16571. ' var cI$1 = 4;',
  16572. ' this.Fly = function () {',
  16573. ' if (cI$1 === this.cI) ;',
  16574. ' };',
  16575. ' var cI$2 = 5;',
  16576. ' this.Run = function () {',
  16577. ' if (cI$2 === this.cI) ;',
  16578. ' };',
  16579. '});',
  16580. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16581. ' var cI$3 = 6;',
  16582. ' this.Go = function () {',
  16583. ' if (cI$3 === this.cI) ;',
  16584. ' };',
  16585. '});',
  16586. '']),
  16587. '',
  16588. '');
  16589. end;
  16590. procedure TTestModule.TestClass_LocalVarSelfFail;
  16591. begin
  16592. StartProgram(false);
  16593. Add([
  16594. 'type',
  16595. ' TObject = class',
  16596. ' constructor Create;',
  16597. ' end;',
  16598. 'constructor tobject.create;',
  16599. 'var self: longint;',
  16600. 'begin',
  16601. 'end',
  16602. 'begin',
  16603. '']);
  16604. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16605. ConvertProgram;
  16606. end;
  16607. procedure TTestModule.TestClass_ArgSelfFail;
  16608. begin
  16609. StartProgram(false);
  16610. Add([
  16611. 'type',
  16612. ' TObject = class',
  16613. ' procedure DoIt(Self: longint);',
  16614. ' end;',
  16615. 'procedure tobject.doit(self: longint);',
  16616. 'begin',
  16617. 'end',
  16618. 'begin',
  16619. '']);
  16620. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16621. ConvertProgram;
  16622. end;
  16623. procedure TTestModule.TestClass_NestedProcSelf;
  16624. begin
  16625. StartProgram(false);
  16626. Add([
  16627. 'type',
  16628. ' TObject = class',
  16629. ' Key: longint;',
  16630. ' class var State: longint;',
  16631. ' procedure DoIt;',
  16632. ' function GetSize: longint; virtual; abstract;',
  16633. ' procedure SetSize(Value: longint); virtual; abstract;',
  16634. ' property Size: longint read GetSize write SetSize;',
  16635. ' end;',
  16636. 'procedure tobject.doit;',
  16637. ' procedure Sub;',
  16638. ' begin',
  16639. ' key:=key+2;',
  16640. ' self.key:=self.key+3;',
  16641. ' state:=state+4;',
  16642. ' self.state:=self.state+5;',
  16643. ' tobject.state:=tobject.state+6;',
  16644. ' size:=size+7;',
  16645. ' self.size:=self.size+8;',
  16646. ' end;',
  16647. 'begin',
  16648. ' sub;',
  16649. ' key:=key+12;',
  16650. ' self.key:=self.key+13;',
  16651. ' state:=state+14;',
  16652. ' self.state:=self.state+15;',
  16653. ' tobject.state:=tobject.state+16;',
  16654. ' size:=size+17;',
  16655. ' self.size:=self.size+18;',
  16656. 'end;',
  16657. 'begin',
  16658. '']);
  16659. ConvertProgram;
  16660. CheckSource('TestClass_NestedProcSelf',
  16661. LinesToStr([ // statements
  16662. 'rtl.createClass(this, "TObject", null, function () {',
  16663. ' this.State = 0;',
  16664. ' this.$init = function () {',
  16665. ' this.Key = 0;',
  16666. ' };',
  16667. ' this.$final = function () {',
  16668. ' };',
  16669. ' this.DoIt = function () {',
  16670. ' var $Self = this;',
  16671. ' function Sub() {',
  16672. ' $Self.Key = $Self.Key + 2;',
  16673. ' $Self.Key = $Self.Key + 3;',
  16674. ' $mod.TObject.State = $Self.State + 4;',
  16675. ' $mod.TObject.State = $Self.State + 5;',
  16676. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16677. ' $Self.SetSize($Self.GetSize() + 7);',
  16678. ' $Self.SetSize($Self.GetSize() + 8);',
  16679. ' };',
  16680. ' Sub();',
  16681. ' this.Key = this.Key + 12;',
  16682. ' $Self.Key = $Self.Key + 13;',
  16683. ' $mod.TObject.State = this.State + 14;',
  16684. ' $mod.TObject.State = $Self.State + 15;',
  16685. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16686. ' this.SetSize(this.GetSize() + 17);',
  16687. ' $Self.SetSize($Self.GetSize() + 18);',
  16688. ' };',
  16689. '});',
  16690. '']),
  16691. LinesToStr([ // $mod.$main
  16692. '']));
  16693. end;
  16694. procedure TTestModule.TestClass_NestedProcSelf2;
  16695. begin
  16696. StartProgram(false);
  16697. Add([
  16698. 'type',
  16699. ' TObject = class',
  16700. ' Key: longint;',
  16701. ' class var State: longint;',
  16702. ' function GetSize: longint; virtual; abstract;',
  16703. ' procedure SetSize(Value: longint); virtual; abstract;',
  16704. ' property Size: longint read GetSize write SetSize;',
  16705. ' end;',
  16706. ' TBird = class',
  16707. ' procedure DoIt;',
  16708. ' end;',
  16709. 'procedure tbird.doit;',
  16710. ' procedure Sub;',
  16711. ' begin',
  16712. ' key:=key+2;',
  16713. ' self.key:=self.key+3;',
  16714. ' state:=state+4;',
  16715. ' self.state:=self.state+5;',
  16716. ' tobject.state:=tobject.state+6;',
  16717. ' size:=size+7;',
  16718. ' self.size:=self.size+8;',
  16719. ' end;',
  16720. 'begin',
  16721. ' sub;',
  16722. ' key:=key+12;',
  16723. ' self.key:=self.key+13;',
  16724. ' state:=state+14;',
  16725. ' self.state:=self.state+15;',
  16726. ' tobject.state:=tobject.state+16;',
  16727. ' size:=size+17;',
  16728. ' self.size:=self.size+18;',
  16729. 'end;',
  16730. 'begin',
  16731. '']);
  16732. ConvertProgram;
  16733. CheckSource('TestClass_NestedProcSelf2',
  16734. LinesToStr([ // statements
  16735. 'rtl.createClass(this, "TObject", null, function () {',
  16736. ' this.State = 0;',
  16737. ' this.$init = function () {',
  16738. ' this.Key = 0;',
  16739. ' };',
  16740. ' this.$final = function () {',
  16741. ' };',
  16742. '});',
  16743. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16744. ' this.DoIt = function () {',
  16745. ' var $Self = this;',
  16746. ' function Sub() {',
  16747. ' $Self.Key = $Self.Key + 2;',
  16748. ' $Self.Key = $Self.Key + 3;',
  16749. ' $mod.TObject.State = $Self.State + 4;',
  16750. ' $mod.TObject.State = $Self.State + 5;',
  16751. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16752. ' $Self.SetSize($Self.GetSize() + 7);',
  16753. ' $Self.SetSize($Self.GetSize() + 8);',
  16754. ' };',
  16755. ' Sub();',
  16756. ' this.Key = this.Key + 12;',
  16757. ' $Self.Key = $Self.Key + 13;',
  16758. ' $mod.TObject.State = this.State + 14;',
  16759. ' $mod.TObject.State = $Self.State + 15;',
  16760. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16761. ' this.SetSize(this.GetSize() + 17);',
  16762. ' $Self.SetSize($Self.GetSize() + 18);',
  16763. ' };',
  16764. '});',
  16765. '']),
  16766. LinesToStr([ // $mod.$main
  16767. '']));
  16768. end;
  16769. procedure TTestModule.TestClass_NestedProcClassSelf;
  16770. begin
  16771. StartProgram(false);
  16772. Add([
  16773. 'type',
  16774. ' TObject = class',
  16775. ' class var State: longint;',
  16776. ' class procedure DoIt;',
  16777. ' class function GetSize: longint; virtual; abstract;',
  16778. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16779. ' class property Size: longint read GetSize write SetSize;',
  16780. ' end;',
  16781. 'class procedure tobject.doit;',
  16782. ' procedure Sub;',
  16783. ' begin',
  16784. ' state:=state+2;',
  16785. ' self.state:=self.state+3;',
  16786. ' tobject.state:=tobject.state+4;',
  16787. ' size:=size+5;',
  16788. ' self.size:=self.size+6;',
  16789. ' tobject.size:=tobject.size+7;',
  16790. ' end;',
  16791. 'begin',
  16792. ' sub;',
  16793. ' state:=state+12;',
  16794. ' self.state:=self.state+13;',
  16795. ' tobject.state:=tobject.state+14;',
  16796. ' size:=size+15;',
  16797. ' self.size:=self.size+16;',
  16798. ' tobject.size:=tobject.size+17;',
  16799. 'end;',
  16800. 'begin',
  16801. '']);
  16802. ConvertProgram;
  16803. CheckSource('TestClass_NestedProcClassSelf',
  16804. LinesToStr([ // statements
  16805. 'rtl.createClass(this, "TObject", null, function () {',
  16806. ' this.State = 0;',
  16807. ' this.$init = function () {',
  16808. ' };',
  16809. ' this.$final = function () {',
  16810. ' };',
  16811. ' this.DoIt = function () {',
  16812. ' var $Self = this;',
  16813. ' function Sub() {',
  16814. ' $mod.TObject.State = $Self.State + 2;',
  16815. ' $mod.TObject.State = $Self.State + 3;',
  16816. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16817. ' $Self.SetSize($Self.GetSize() + 5);',
  16818. ' $Self.SetSize($Self.GetSize() + 6);',
  16819. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16820. ' };',
  16821. ' Sub();',
  16822. ' $mod.TObject.State = this.State + 12;',
  16823. ' $mod.TObject.State = $Self.State + 13;',
  16824. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16825. ' this.SetSize(this.GetSize() + 15);',
  16826. ' $Self.SetSize($Self.GetSize() + 16);',
  16827. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16828. ' };',
  16829. '});',
  16830. '']),
  16831. LinesToStr([ // $mod.$main
  16832. '']));
  16833. end;
  16834. procedure TTestModule.TestClass_NestedProcCallInherited;
  16835. begin
  16836. StartProgram(false);
  16837. Add([
  16838. 'type',
  16839. ' TObject = class',
  16840. ' function DoIt(k: boolean): longint; virtual;',
  16841. ' end;',
  16842. ' TBird = class',
  16843. ' function DoIt(k: boolean): longint; override;',
  16844. ' end;',
  16845. 'function tobject.doit(k: boolean): longint;',
  16846. 'begin',
  16847. 'end;',
  16848. 'function tbird.doit(k: boolean): longint;',
  16849. ' procedure Sub;',
  16850. ' begin',
  16851. ' inherited DoIt(true);',
  16852. //' if inherited DoIt(false)=4 then ;',
  16853. ' end;',
  16854. 'begin',
  16855. ' Sub;',
  16856. ' inherited;',
  16857. ' inherited DoIt(true);',
  16858. //' if inherited DoIt(false)=14 then ;',
  16859. 'end;',
  16860. 'begin',
  16861. '']);
  16862. ConvertProgram;
  16863. CheckSource('TestClass_NestedProcCallInherited',
  16864. LinesToStr([ // statements
  16865. 'rtl.createClass(this, "TObject", null, function () {',
  16866. ' this.$init = function () {',
  16867. ' };',
  16868. ' this.$final = function () {',
  16869. ' };',
  16870. ' this.DoIt = function (k) {',
  16871. ' var Result = 0;',
  16872. ' return Result;',
  16873. ' };',
  16874. '});',
  16875. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16876. ' this.DoIt = function (k) {',
  16877. ' var $Self = this;',
  16878. ' var Result = 0;',
  16879. ' function Sub() {',
  16880. ' $mod.TObject.DoIt.call($Self, true);',
  16881. ' };',
  16882. ' Sub();',
  16883. ' $mod.TObject.DoIt.apply(this, arguments);',
  16884. ' $mod.TObject.DoIt.call(this, true);',
  16885. ' return Result;',
  16886. ' };',
  16887. '});',
  16888. '']),
  16889. LinesToStr([ // $mod.$main
  16890. '']));
  16891. end;
  16892. procedure TTestModule.TestClass_TObjectFree;
  16893. begin
  16894. StartProgram(false);
  16895. Add([
  16896. 'type',
  16897. ' TObject = class',
  16898. ' Obj: tobject;',
  16899. ' procedure Free;',
  16900. ' procedure Release;',
  16901. ' end;',
  16902. 'procedure tobject.free;',
  16903. 'begin',
  16904. 'end;',
  16905. 'procedure tobject.release;',
  16906. 'begin',
  16907. ' free;',
  16908. ' if true then free;',
  16909. 'end;',
  16910. 'function DoIt(o: tobject): tobject;',
  16911. 'var l: tobject;',
  16912. 'begin',
  16913. ' o.free;',
  16914. ' o.free();',
  16915. ' l.free;',
  16916. ' l.free();',
  16917. ' o.obj.free;',
  16918. ' o.obj.free();',
  16919. ' with o do obj.free;',
  16920. ' with o do obj.free();',
  16921. ' result.Free;',
  16922. ' result.Free();',
  16923. 'end;',
  16924. 'var o: tobject;',
  16925. ' a: array of tobject;',
  16926. 'begin',
  16927. ' o.free;',
  16928. ' o.obj.free;',
  16929. ' a[1+2].free;',
  16930. '']);
  16931. ConvertProgram;
  16932. CheckSource('TestClass_TObjectFree',
  16933. LinesToStr([ // statements
  16934. 'rtl.createClass(this, "TObject", null, function () {',
  16935. ' this.$init = function () {',
  16936. ' this.Obj = null;',
  16937. ' };',
  16938. ' this.$final = function () {',
  16939. ' this.Obj = undefined;',
  16940. ' };',
  16941. ' this.Free = function () {',
  16942. ' };',
  16943. ' this.Release = function () {',
  16944. ' this.Free();',
  16945. ' if (true) this.Free();',
  16946. ' };',
  16947. '});',
  16948. 'this.DoIt = function (o) {',
  16949. ' var Result = null;',
  16950. ' var l = null;',
  16951. ' o = rtl.freeLoc(o);',
  16952. ' o = rtl.freeLoc(o);',
  16953. ' l = rtl.freeLoc(l);',
  16954. ' l = rtl.freeLoc(l);',
  16955. ' rtl.free(o, "Obj");',
  16956. ' rtl.free(o, "Obj");',
  16957. ' rtl.free(o, "Obj");',
  16958. ' rtl.free(o, "Obj");',
  16959. ' Result = rtl.freeLoc(Result);',
  16960. ' Result = rtl.freeLoc(Result);',
  16961. ' return Result;',
  16962. '};',
  16963. 'this.o = null;',
  16964. 'this.a = [];',
  16965. '']),
  16966. LinesToStr([ // $mod.$main
  16967. 'rtl.free($mod, "o");',
  16968. 'rtl.free($mod.o, "Obj");',
  16969. 'rtl.free($mod.a, 1 + 2);',
  16970. '']));
  16971. end;
  16972. procedure TTestModule.TestClass_TObjectFree_VarArg;
  16973. begin
  16974. StartProgram(false);
  16975. Add([
  16976. 'type',
  16977. ' TObject = class',
  16978. ' Obj: tobject;',
  16979. ' procedure Free;',
  16980. ' end;',
  16981. 'procedure tobject.free;',
  16982. 'begin',
  16983. 'end;',
  16984. 'procedure DoIt(var o: tobject);',
  16985. 'begin',
  16986. ' o.free;',
  16987. ' o.free();',
  16988. 'end;',
  16989. 'begin',
  16990. '']);
  16991. ConvertProgram;
  16992. CheckSource('TestClass_TObjectFree_VarArg',
  16993. LinesToStr([ // statements
  16994. 'rtl.createClass(this, "TObject", null, function () {',
  16995. ' this.$init = function () {',
  16996. ' this.Obj = null;',
  16997. ' };',
  16998. ' this.$final = function () {',
  16999. ' this.Obj = undefined;',
  17000. ' };',
  17001. ' this.Free = function () {',
  17002. ' };',
  17003. '});',
  17004. 'this.DoIt = function (o) {',
  17005. ' o.set(rtl.freeLoc(o.get()));',
  17006. ' o.set(rtl.freeLoc(o.get()));',
  17007. '};',
  17008. '']),
  17009. LinesToStr([ // $mod.$main
  17010. '']));
  17011. end;
  17012. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17013. begin
  17014. StartProgram(false);
  17015. Add([
  17016. 'type',
  17017. ' TObject = class',
  17018. ' constructor Create;',
  17019. ' procedure Free;',
  17020. ' end;',
  17021. 'constructor TObject.Create; begin end;',
  17022. 'procedure tobject.free; begin end;',
  17023. 'begin',
  17024. ' with tobject.create do free;',
  17025. '']);
  17026. ConvertProgram;
  17027. CheckSource('TestClass_TObjectFreeNewInstance',
  17028. LinesToStr([ // statements
  17029. 'rtl.createClass(this, "TObject", null, function () {',
  17030. ' this.$init = function () {',
  17031. ' };',
  17032. ' this.$final = function () {',
  17033. ' };',
  17034. ' this.Create = function () {',
  17035. ' return this;',
  17036. ' };',
  17037. ' this.Free = function () {',
  17038. ' };',
  17039. '});',
  17040. '']),
  17041. LinesToStr([ // $mod.$main
  17042. 'var $with = $mod.TObject.$create("Create");',
  17043. '$with=rtl.freeLoc($with);',
  17044. '']));
  17045. end;
  17046. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17047. begin
  17048. StartProgram(false);
  17049. Add([
  17050. 'type',
  17051. ' TObject = class',
  17052. ' destructor Destroy;',
  17053. ' procedure Free;',
  17054. ' end;',
  17055. 'destructor TObject.Destroy; begin end;',
  17056. 'procedure tobject.free; begin end;',
  17057. 'var o: tobject;',
  17058. 'begin',
  17059. ' o.free;',
  17060. '']);
  17061. Converter.UseLowerCase:=true;
  17062. ConvertProgram;
  17063. CheckSource('TestClass_TObjectFreeLowerCase',
  17064. LinesToStr([ // statements
  17065. 'rtl.createClass(this, "tobject", null, function () {',
  17066. ' this.$init = function () {',
  17067. ' };',
  17068. ' this.$final = function () {',
  17069. ' };',
  17070. ' rtl.tObjectDestroy = "destroy";',
  17071. ' this.destroy = function () {',
  17072. ' };',
  17073. ' this.free = function () {',
  17074. ' };',
  17075. '});',
  17076. 'this.o = null;',
  17077. '']),
  17078. LinesToStr([ // $mod.$main
  17079. 'rtl.free($mod, "o");',
  17080. '']));
  17081. end;
  17082. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17083. begin
  17084. StartProgram(false);
  17085. Add([
  17086. 'type',
  17087. ' TObject = class',
  17088. ' procedure Free;',
  17089. ' function GetObj: tobject; virtual; abstract;',
  17090. ' end;',
  17091. 'procedure tobject.free;',
  17092. 'begin',
  17093. 'end;',
  17094. 'var o: tobject;',
  17095. 'begin',
  17096. ' o.getobj.free;',
  17097. '']);
  17098. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17099. ConvertProgram;
  17100. end;
  17101. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17102. begin
  17103. StartProgram(false);
  17104. Add([
  17105. 'type',
  17106. ' TObject = class',
  17107. ' procedure Free;',
  17108. ' FObj: TObject;',
  17109. ' property Obj: tobject read FObj write FObj;',
  17110. ' end;',
  17111. 'procedure tobject.free;',
  17112. 'begin',
  17113. 'end;',
  17114. 'var o: tobject;',
  17115. 'begin',
  17116. ' o.obj.free;',
  17117. '']);
  17118. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17119. ConvertProgram;
  17120. end;
  17121. procedure TTestModule.TestClass_ForIn;
  17122. begin
  17123. StartProgram(false);
  17124. Add([
  17125. 'type',
  17126. ' TObject = class end;',
  17127. ' TItem = TObject;',
  17128. ' TEnumerator = class',
  17129. ' FCurrent: TItem;',
  17130. ' property Current: TItem read FCurrent;',
  17131. ' function MoveNext: boolean;',
  17132. ' end;',
  17133. ' TBird = class',
  17134. ' function GetEnumerator: TEnumerator;',
  17135. ' end;',
  17136. 'function TEnumerator.MoveNext: boolean;',
  17137. 'begin',
  17138. 'end;',
  17139. 'function TBird.GetEnumerator: TEnumerator;',
  17140. 'begin',
  17141. 'end;',
  17142. 'var',
  17143. ' b: TBird;',
  17144. ' i, i2: TItem;',
  17145. 'begin',
  17146. ' for i in b do i2:=i;']);
  17147. ConvertProgram;
  17148. CheckSource('TestClass_ForIn',
  17149. LinesToStr([ // statements
  17150. 'rtl.createClass(this, "TObject", null, function () {',
  17151. ' this.$init = function () {',
  17152. ' };',
  17153. ' this.$final = function () {',
  17154. ' };',
  17155. '});',
  17156. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17157. ' this.$init = function () {',
  17158. ' $mod.TObject.$init.call(this);',
  17159. ' this.FCurrent = null;',
  17160. ' };',
  17161. ' this.$final = function () {',
  17162. ' this.FCurrent = undefined;',
  17163. ' $mod.TObject.$final.call(this);',
  17164. ' };',
  17165. ' this.MoveNext = function () {',
  17166. ' var Result = false;',
  17167. ' return Result;',
  17168. ' };',
  17169. '});',
  17170. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17171. ' this.GetEnumerator = function () {',
  17172. ' var Result = null;',
  17173. ' return Result;',
  17174. ' };',
  17175. '});',
  17176. 'this.b = null;',
  17177. 'this.i = null;',
  17178. 'this.i2 = null;'
  17179. ]),
  17180. LinesToStr([ // $mod.$main
  17181. 'var $in = $mod.b.GetEnumerator();',
  17182. 'try {',
  17183. ' while ($in.MoveNext()){',
  17184. ' $mod.i = $in.FCurrent;',
  17185. ' $mod.i2 = $mod.i;',
  17186. ' }',
  17187. '} finally {',
  17188. ' $in = rtl.freeLoc($in)',
  17189. '};',
  17190. '']));
  17191. end;
  17192. procedure TTestModule.TestClass_DispatchMessage;
  17193. begin
  17194. StartProgram(false);
  17195. Add([
  17196. 'type',
  17197. ' {$DispatchField DispInt}',
  17198. ' {$DispatchStrField DispStr}',
  17199. ' TObject = class',
  17200. ' procedure Dispatch(var Msg); virtual; abstract;',
  17201. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17202. ' end;',
  17203. ' THopMsg = record',
  17204. ' DispInt: longint;',
  17205. ' end;',
  17206. ' TPutMsg = record',
  17207. ' DispStr: string;',
  17208. ' end;',
  17209. ' TBird = class',
  17210. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17211. ' procedure Run; overload; virtual; abstract;',
  17212. ' procedure Run(var Msg); overload; message ''Fast'';',
  17213. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17214. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17215. ' end;',
  17216. 'procedure TBird.Run(var Msg);',
  17217. 'begin',
  17218. 'end;',
  17219. 'begin',
  17220. '']);
  17221. ConvertProgram;
  17222. CheckResolverUnexpectedHints(true);
  17223. CheckSource('TestClass_Message',
  17224. LinesToStr([ // statements
  17225. 'rtl.createClass(this, "TObject", null, function () {',
  17226. ' this.$init = function () {',
  17227. ' };',
  17228. ' this.$final = function () {',
  17229. ' };',
  17230. '});',
  17231. 'rtl.recNewT(this, "THopMsg", function () {',
  17232. ' this.DispInt = 0;',
  17233. ' this.$eq = function (b) {',
  17234. ' return this.DispInt === b.DispInt;',
  17235. ' };',
  17236. ' this.$assign = function (s) {',
  17237. ' this.DispInt = s.DispInt;',
  17238. ' return this;',
  17239. ' };',
  17240. '});',
  17241. 'rtl.recNewT(this, "TPutMsg", function () {',
  17242. ' this.DispStr = "";',
  17243. ' this.$eq = function (b) {',
  17244. ' return this.DispStr === b.DispStr;',
  17245. ' };',
  17246. ' this.$assign = function (s) {',
  17247. ' this.DispStr = s.DispStr;',
  17248. ' return this;',
  17249. ' };',
  17250. '});',
  17251. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17252. ' this.Run$1 = function (Msg) {',
  17253. ' };',
  17254. ' this.$msgint = {',
  17255. ' "2": "Fly",',
  17256. ' "3": "Hop"',
  17257. ' };',
  17258. ' this.$msgstr = {',
  17259. ' Fast: "Run$1",',
  17260. ' foo: "Put"',
  17261. ' };',
  17262. '});',
  17263. '']),
  17264. LinesToStr([ // $mod.$main
  17265. '']));
  17266. end;
  17267. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17268. begin
  17269. StartProgram(false);
  17270. Add([
  17271. 'type',
  17272. ' TObject = class',
  17273. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17274. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17275. ' end;',
  17276. 'begin',
  17277. '']);
  17278. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17279. ConvertProgram;
  17280. end;
  17281. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17282. begin
  17283. StartProgram(false);
  17284. Add([
  17285. 'type',
  17286. ' TObject = class',
  17287. ' procedure Dispatch(var Msg); virtual; abstract;',
  17288. ' end;',
  17289. ' TFlyMsg = record',
  17290. ' FlyId: longint;',
  17291. ' end;',
  17292. ' TBird = class',
  17293. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17294. ' end;',
  17295. 'begin',
  17296. '']);
  17297. ConvertProgram;
  17298. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17299. end;
  17300. procedure TTestModule.TestClassOf_Create;
  17301. begin
  17302. StartProgram(false);
  17303. Add('type');
  17304. Add(' TObject = class');
  17305. Add(' constructor Create;');
  17306. Add(' end;');
  17307. Add(' TClass = class of TObject;');
  17308. Add('constructor tobject.create; begin end;');
  17309. Add('var');
  17310. Add(' Obj: tobject;');
  17311. Add(' C: tclass;');
  17312. Add('begin');
  17313. Add(' obj:=C.create;');
  17314. Add(' with c do obj:=create;');
  17315. ConvertProgram;
  17316. CheckSource('TestClassOf_Create',
  17317. LinesToStr([ // statements
  17318. 'rtl.createClass(this, "TObject", null, function () {',
  17319. ' this.$init = function () {',
  17320. ' };',
  17321. ' this.$final = function () {',
  17322. ' };',
  17323. ' this.Create = function () {',
  17324. ' return this;',
  17325. ' };',
  17326. '});',
  17327. 'this.Obj = null;',
  17328. 'this.C = null;'
  17329. ]),
  17330. LinesToStr([ // $mod.$main
  17331. '$mod.Obj = $mod.C.$create("Create");',
  17332. 'var $with = $mod.C;',
  17333. '$mod.Obj = $with.$create("Create");',
  17334. '']));
  17335. end;
  17336. procedure TTestModule.TestClassOf_Call;
  17337. begin
  17338. StartProgram(false);
  17339. Add('type');
  17340. Add(' TObject = class');
  17341. Add(' class procedure DoIt;');
  17342. Add(' end;');
  17343. Add(' TClass = class of TObject;');
  17344. Add('class procedure tobject.doit; begin end;');
  17345. Add('var');
  17346. Add(' C: tclass;');
  17347. Add('begin');
  17348. Add(' c.doit;');
  17349. Add(' with c do doit;');
  17350. ConvertProgram;
  17351. CheckSource('TestClassOf_Call',
  17352. LinesToStr([ // statements
  17353. 'rtl.createClass(this, "TObject", null, function () {',
  17354. ' this.$init = function () {',
  17355. ' };',
  17356. ' this.$final = function () {',
  17357. ' };',
  17358. ' this.DoIt = function () {',
  17359. ' };',
  17360. '});',
  17361. 'this.C = null;'
  17362. ]),
  17363. LinesToStr([ // $mod.$main
  17364. '$mod.C.DoIt();',
  17365. 'var $with = $mod.C;',
  17366. '$with.DoIt();',
  17367. '']));
  17368. end;
  17369. procedure TTestModule.TestClassOf_Assign;
  17370. begin
  17371. StartProgram(false);
  17372. Add('type');
  17373. Add(' TClass = class of TObject;');
  17374. Add(' TObject = class');
  17375. Add(' ClassType: TClass; ');
  17376. Add(' end;');
  17377. Add('var');
  17378. Add(' Obj: tobject;');
  17379. Add(' C: tclass;');
  17380. Add('begin');
  17381. Add(' c:=nil;');
  17382. Add(' c:=obj.classtype;');
  17383. ConvertProgram;
  17384. CheckSource('TestClassOf_Assign',
  17385. LinesToStr([ // statements
  17386. 'rtl.createClass(this, "TObject", null, function () {',
  17387. ' this.$init = function () {',
  17388. ' this.ClassType = null;',
  17389. ' };',
  17390. ' this.$final = function () {',
  17391. ' this.ClassType = undefined;',
  17392. ' };',
  17393. '});',
  17394. 'this.Obj = null;',
  17395. 'this.C = null;'
  17396. ]),
  17397. LinesToStr([ // $mod.$main
  17398. '$mod.C = null;',
  17399. '$mod.C = $mod.Obj.ClassType;',
  17400. '']));
  17401. end;
  17402. procedure TTestModule.TestClassOf_Is;
  17403. begin
  17404. StartProgram(false);
  17405. Add('type');
  17406. Add(' TClass = class of TObject;');
  17407. Add(' TObject = class');
  17408. Add(' end;');
  17409. Add(' TCar = class');
  17410. Add(' end;');
  17411. Add(' TCars = class of TCar;');
  17412. Add('var');
  17413. Add(' Obj: tobject;');
  17414. Add(' C: tclass;');
  17415. Add(' Cars: tcars;');
  17416. Add('begin');
  17417. Add(' if c is tcar then ;');
  17418. Add(' if c is tcars then ;');
  17419. ConvertProgram;
  17420. CheckSource('TestClassOf_Is',
  17421. LinesToStr([ // statements
  17422. 'rtl.createClass(this, "TObject", null, function () {',
  17423. ' this.$init = function () {',
  17424. ' };',
  17425. ' this.$final = function () {',
  17426. ' };',
  17427. '});',
  17428. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17429. '});',
  17430. 'this.Obj = null;',
  17431. 'this.C = null;',
  17432. 'this.Cars = null;'
  17433. ]),
  17434. LinesToStr([ // $mod.$main
  17435. 'if(rtl.is($mod.C,$mod.TCar));',
  17436. 'if(rtl.is($mod.C,$mod.TCar));',
  17437. '']));
  17438. end;
  17439. procedure TTestModule.TestClassOf_Compare;
  17440. begin
  17441. StartProgram(false);
  17442. Add('type');
  17443. Add(' TClass = class of TObject;');
  17444. Add(' TObject = class');
  17445. Add(' ClassType: TClass; ');
  17446. Add(' end;');
  17447. Add('var');
  17448. Add(' b: boolean;');
  17449. Add(' Obj: tobject;');
  17450. Add(' C: tclass;');
  17451. Add('begin');
  17452. Add(' b:=c=nil;');
  17453. Add(' b:=nil=c;');
  17454. Add(' b:=c=obj.classtype;');
  17455. Add(' b:=obj.classtype=c;');
  17456. Add(' b:=c=TObject;');
  17457. Add(' b:=TObject=c;');
  17458. Add(' b:=c<>nil;');
  17459. Add(' b:=nil<>c;');
  17460. Add(' b:=c<>obj.classtype;');
  17461. Add(' b:=obj.classtype<>c;');
  17462. Add(' b:=c<>TObject;');
  17463. Add(' b:=TObject<>c;');
  17464. ConvertProgram;
  17465. CheckSource('TestClassOf_Compare',
  17466. LinesToStr([ // statements
  17467. 'rtl.createClass(this, "TObject", null, function () {',
  17468. ' this.$init = function () {',
  17469. ' this.ClassType = null;',
  17470. ' };',
  17471. ' this.$final = function () {',
  17472. ' this.ClassType = undefined;',
  17473. ' };',
  17474. '});',
  17475. 'this.b = false;',
  17476. 'this.Obj = null;',
  17477. 'this.C = null;'
  17478. ]),
  17479. LinesToStr([ // $mod.$main
  17480. '$mod.b = $mod.C === null;',
  17481. '$mod.b = null === $mod.C;',
  17482. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17483. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17484. '$mod.b = $mod.C === $mod.TObject;',
  17485. '$mod.b = $mod.TObject === $mod.C;',
  17486. '$mod.b = $mod.C !== null;',
  17487. '$mod.b = null !== $mod.C;',
  17488. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17489. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17490. '$mod.b = $mod.C !== $mod.TObject;',
  17491. '$mod.b = $mod.TObject !== $mod.C;',
  17492. '']));
  17493. end;
  17494. procedure TTestModule.TestClassOf_ClassVar;
  17495. begin
  17496. StartProgram(false);
  17497. Add('type');
  17498. Add(' TObject = class');
  17499. Add(' class var id: longint;');
  17500. Add(' end;');
  17501. Add(' TClass = class of TObject;');
  17502. Add('var');
  17503. Add(' C: tclass;');
  17504. Add('begin');
  17505. Add(' C.id:=C.id;');
  17506. ConvertProgram;
  17507. CheckSource('TestClassOf_ClassVar',
  17508. LinesToStr([ // statements
  17509. 'rtl.createClass(this, "TObject", null, function () {',
  17510. ' this.id = 0;',
  17511. ' this.$init = function () {',
  17512. ' };',
  17513. ' this.$final = function () {',
  17514. ' };',
  17515. '});',
  17516. 'this.C = null;'
  17517. ]),
  17518. LinesToStr([ // $mod.$main
  17519. '$mod.TObject.id = $mod.C.id;',
  17520. '']));
  17521. end;
  17522. procedure TTestModule.TestClassOf_ClassMethod;
  17523. begin
  17524. StartProgram(false);
  17525. Add('type');
  17526. Add(' TObject = class');
  17527. Add(' class function DoIt(i: longint = 0): longint;');
  17528. Add(' end;');
  17529. Add(' TClass = class of TObject;');
  17530. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17531. Add('var');
  17532. Add(' i: longint;');
  17533. Add(' C: tclass;');
  17534. Add('begin');
  17535. Add(' C.DoIt;');
  17536. Add(' C.DoIt();');
  17537. Add(' i:=C.DoIt;');
  17538. Add(' i:=C.DoIt();');
  17539. ConvertProgram;
  17540. CheckSource('TestClassOf_ClassMethod',
  17541. LinesToStr([ // statements
  17542. 'rtl.createClass(this, "TObject", null, function () {',
  17543. ' this.$init = function () {',
  17544. ' };',
  17545. ' this.$final = function () {',
  17546. ' };',
  17547. ' this.DoIt = function (i) {',
  17548. ' var Result = 0;',
  17549. ' return Result;',
  17550. ' };',
  17551. '});',
  17552. 'this.i = 0;',
  17553. 'this.C = null;'
  17554. ]),
  17555. LinesToStr([ // $mod.$main
  17556. '$mod.C.DoIt(0);',
  17557. '$mod.C.DoIt(0);',
  17558. '$mod.i = $mod.C.DoIt(0);',
  17559. '$mod.i = $mod.C.DoIt(0);',
  17560. '']));
  17561. end;
  17562. procedure TTestModule.TestClassOf_ClassProperty;
  17563. begin
  17564. StartProgram(false);
  17565. Add([
  17566. 'type',
  17567. ' TObject = class',
  17568. ' class var FA: longint;',
  17569. ' class function GetA: longint;',
  17570. ' class procedure SetA(Value: longint);',
  17571. ' class property pA: longint read fa write fa;',
  17572. ' class property pB: longint read geta write seta;',
  17573. ' end;',
  17574. ' TObjectClass = class of tobject;',
  17575. 'class function tobject.geta: longint; begin end;',
  17576. 'class procedure tobject.seta(value: longint); begin end;',
  17577. 'var',
  17578. ' b: boolean;',
  17579. ' Obj: tobject;',
  17580. ' Cla: tobjectclass;',
  17581. 'begin',
  17582. ' obj.pa:=obj.pa;',
  17583. ' obj.pb:=obj.pb;',
  17584. ' b:=obj.pa=4;',
  17585. ' b:=obj.pb=obj.pb;',
  17586. ' b:=5=obj.pa;',
  17587. ' cla.pa:=6;',
  17588. ' cla.pa:=cla.pa;',
  17589. ' cla.pb:=cla.pb;',
  17590. ' b:=cla.pa=7;',
  17591. ' b:=cla.pb=cla.pb;',
  17592. ' b:=8=cla.pa;',
  17593. ' tobject.pa:=9;',
  17594. ' tobject.pb:=tobject.pb;',
  17595. ' b:=tobject.pa=10;',
  17596. ' b:=11=tobject.pa;',
  17597. '']);
  17598. ConvertProgram;
  17599. CheckSource('TestClassOf_ClassProperty',
  17600. LinesToStr([ // statements
  17601. 'rtl.createClass(this, "TObject", null, function () {',
  17602. ' this.FA = 0;',
  17603. ' this.$init = function () {',
  17604. ' };',
  17605. ' this.$final = function () {',
  17606. ' };',
  17607. ' this.GetA = function () {',
  17608. ' var Result = 0;',
  17609. ' return Result;',
  17610. ' };',
  17611. ' this.SetA = function (Value) {',
  17612. ' };',
  17613. '});',
  17614. 'this.b = false;',
  17615. 'this.Obj = null;',
  17616. 'this.Cla = null;'
  17617. ]),
  17618. LinesToStr([ // $mod.$main
  17619. '$mod.TObject.FA = $mod.Obj.FA;',
  17620. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17621. '$mod.b = $mod.Obj.FA === 4;',
  17622. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17623. '$mod.b = 5 === $mod.Obj.FA;',
  17624. '$mod.TObject.FA = 6;',
  17625. '$mod.TObject.FA = $mod.Cla.FA;',
  17626. '$mod.Cla.SetA($mod.Cla.GetA());',
  17627. '$mod.b = $mod.Cla.FA === 7;',
  17628. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17629. '$mod.b = 8 === $mod.Cla.FA;',
  17630. '$mod.TObject.FA = 9;',
  17631. '$mod.TObject.SetA($mod.TObject.GetA());',
  17632. '$mod.b = $mod.TObject.FA === 10;',
  17633. '$mod.b = 11 === $mod.TObject.FA;',
  17634. '']));
  17635. end;
  17636. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17637. begin
  17638. StartProgram(false);
  17639. Add('type');
  17640. Add(' TObject = class');
  17641. Add(' class var GlobalId: longint;');
  17642. Add(' class procedure ProcA;');
  17643. Add(' end;');
  17644. Add('class procedure tobject.proca;');
  17645. Add('var b: boolean;');
  17646. Add('begin');
  17647. Add(' b:=self=nil;');
  17648. Add(' b:=self.globalid=3;');
  17649. Add(' b:=4=self.globalid;');
  17650. Add(' self.globalid:=5;');
  17651. Add(' self.proca;');
  17652. Add('end;');
  17653. Add('begin');
  17654. ConvertProgram;
  17655. CheckSource('TestClassOf_ClassMethodSelf',
  17656. LinesToStr([ // statements
  17657. 'rtl.createClass(this, "TObject", null, function () {',
  17658. ' this.GlobalId = 0;',
  17659. ' this.$init = function () {',
  17660. ' };',
  17661. ' this.$final = function () {',
  17662. ' };',
  17663. ' this.ProcA = function () {',
  17664. ' var b = false;',
  17665. ' b = this === null;',
  17666. ' b = this.GlobalId === 3;',
  17667. ' b = 4 === this.GlobalId;',
  17668. ' $mod.TObject.GlobalId = 5;',
  17669. ' this.ProcA();',
  17670. ' };',
  17671. '});'
  17672. ]),
  17673. LinesToStr([ // $mod.$main
  17674. '']));
  17675. end;
  17676. procedure TTestModule.TestClassOf_TypeCast;
  17677. begin
  17678. StartProgram(false);
  17679. Add('type');
  17680. Add(' TObject = class');
  17681. Add(' class procedure {#TObject_DoIt}DoIt;');
  17682. Add(' end;');
  17683. Add(' TClass = class of TObject;');
  17684. Add(' TMobile = class');
  17685. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17686. Add(' end;');
  17687. Add(' TMobileClass = class of TMobile;');
  17688. Add(' TCar = class(TMobile)');
  17689. Add(' class procedure {#TCar_DoIt}DoIt;');
  17690. Add(' end;');
  17691. Add(' TCarClass = class of TCar;');
  17692. Add('class procedure TObject.DoIt;');
  17693. Add('begin');
  17694. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17695. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17696. Add('end;');
  17697. Add('class procedure TMobile.DoIt;');
  17698. Add('begin');
  17699. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17700. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17701. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17702. Add('end;');
  17703. Add('class procedure TCar.DoIt; begin end;');
  17704. Add('var');
  17705. Add(' ObjC: TClass;');
  17706. Add(' MobileC: TMobileClass;');
  17707. Add(' CarC: TCarClass;');
  17708. Add('begin');
  17709. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17710. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17711. Add(' CarC.{@TCar_DoIt}DoIt;');
  17712. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17713. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17714. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17715. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17716. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17717. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17718. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17719. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17720. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17721. ConvertProgram;
  17722. CheckSource('TestClassOf_TypeCast',
  17723. LinesToStr([ // statements
  17724. 'rtl.createClass(this, "TObject", null, function () {',
  17725. ' this.$init = function () {',
  17726. ' };',
  17727. ' this.$final = function () {',
  17728. ' };',
  17729. ' this.DoIt = function () {',
  17730. ' this.DoIt();',
  17731. ' this.DoIt$1();',
  17732. ' };',
  17733. '});',
  17734. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17735. ' this.DoIt$1 = function () {',
  17736. ' this.DoIt();',
  17737. ' this.DoIt$1();',
  17738. ' this.DoIt$2();',
  17739. ' };',
  17740. '});',
  17741. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17742. ' this.DoIt$2 = function () {',
  17743. ' };',
  17744. '});',
  17745. 'this.ObjC = null;',
  17746. 'this.MobileC = null;',
  17747. 'this.CarC = null;',
  17748. '']),
  17749. LinesToStr([ // $mod.$main
  17750. '$mod.ObjC.DoIt();',
  17751. '$mod.MobileC.DoIt$1();',
  17752. '$mod.CarC.DoIt$2();',
  17753. '$mod.ObjC.DoIt();',
  17754. '$mod.ObjC.DoIt$1();',
  17755. '$mod.ObjC.DoIt$2();',
  17756. '$mod.MobileC.DoIt();',
  17757. '$mod.MobileC.DoIt$1();',
  17758. '$mod.MobileC.DoIt$2();',
  17759. '$mod.CarC.DoIt();',
  17760. '$mod.CarC.DoIt$1();',
  17761. '$mod.CarC.DoIt$2();',
  17762. '']));
  17763. end;
  17764. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17765. begin
  17766. StartProgram(false);
  17767. Add('type');
  17768. Add(' TObject = class');
  17769. Add(' function CurNow: longint; ');
  17770. Add(' class function Now: longint; ');
  17771. Add(' end;');
  17772. Add('function TObject.CurNow: longint; begin end;');
  17773. Add('class function TObject.Now: longint; begin end;');
  17774. Add('var');
  17775. Add(' Obj: tobject;');
  17776. Add(' vI: longint;');
  17777. Add('begin');
  17778. Add(' obj.curnow;');
  17779. Add(' vi:=obj.curnow;');
  17780. Add(' tobject.now;');
  17781. Add(' vi:=tobject.now;');
  17782. ConvertProgram;
  17783. CheckSource('TestClassOf_ImplicitFunctionCall',
  17784. LinesToStr([ // statements
  17785. 'rtl.createClass(this, "TObject", null, function () {',
  17786. ' this.$init = function () {',
  17787. ' };',
  17788. ' this.$final = function () {',
  17789. ' };',
  17790. ' this.CurNow = function () {',
  17791. ' var Result = 0;',
  17792. ' return Result;',
  17793. ' };',
  17794. ' this.Now = function () {',
  17795. ' var Result = 0;',
  17796. ' return Result;',
  17797. ' };',
  17798. '});',
  17799. 'this.Obj = null;',
  17800. 'this.vI = 0;',
  17801. '']),
  17802. LinesToStr([ // $mod.$main
  17803. '$mod.Obj.CurNow();',
  17804. '$mod.vI = $mod.Obj.CurNow();',
  17805. '$mod.TObject.Now();',
  17806. '$mod.vI = $mod.TObject.Now();',
  17807. '']));
  17808. end;
  17809. procedure TTestModule.TestClassOf_Const;
  17810. begin
  17811. StartProgram(false);
  17812. Add([
  17813. 'type',
  17814. ' TObject = class',
  17815. ' end;',
  17816. ' TBird = TObject;',
  17817. ' TBirds = class of TBird;',
  17818. ' TEagles = TBirds;',
  17819. ' THawk = class(TBird);',
  17820. 'const',
  17821. ' Hawk: TEagles = THawk;',
  17822. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17823. ' TBird,',
  17824. ' THawk',
  17825. ' );',
  17826. 'begin']);
  17827. ConvertProgram;
  17828. CheckSource('TestClassOf_Const',
  17829. LinesToStr([ // statements
  17830. 'rtl.createClass(this, "TObject", null, function () {',
  17831. ' this.$init = function () {',
  17832. ' };',
  17833. ' this.$final = function () {',
  17834. ' };',
  17835. '});',
  17836. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17837. '});',
  17838. 'this.Hawk = this.THawk;',
  17839. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17840. '']),
  17841. LinesToStr([ // $mod.$main
  17842. '']));
  17843. end;
  17844. procedure TTestModule.TestNestedClass_Alias;
  17845. begin
  17846. WithTypeInfo:=true;
  17847. StartProgram(false);
  17848. Add([
  17849. 'type',
  17850. ' TObject = class',
  17851. ' type TNested = type longint;',
  17852. ' end;',
  17853. 'type TAlias = type tobject.tnested;',
  17854. 'var i: tobject.tnested = 3;',
  17855. 'var j: TAlias = 4;',
  17856. 'begin',
  17857. ' if typeinfo(TAlias)=nil then ;',
  17858. ' if typeinfo(tobject.tnested)=nil then ;',
  17859. '']);
  17860. ConvertProgram;
  17861. CheckSource('TestNestedClass_Alias',
  17862. LinesToStr([ // statements
  17863. 'rtl.createClass(this, "TObject", null, function () {',
  17864. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17865. ' this.$init = function () {',
  17866. ' };',
  17867. ' this.$final = function () {',
  17868. ' };',
  17869. '});',
  17870. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17871. 'this.i = 3;',
  17872. 'this.j = 4;',
  17873. '']),
  17874. LinesToStr([ // $mod.$main
  17875. 'if ($mod.$rtti["TAlias"] === null) ;',
  17876. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17877. '']));
  17878. end;
  17879. procedure TTestModule.TestNestedClass_Record;
  17880. begin
  17881. WithTypeInfo:=true;
  17882. StartProgram(false);
  17883. Add([
  17884. 'type',
  17885. ' TObject = class',
  17886. ' type TPoint = record',
  17887. ' x,y: byte;',
  17888. ' end;',
  17889. ' procedure DoIt(t: TPoint);',
  17890. ' end;',
  17891. 'procedure tobject.DoIt(t: TPoint);',
  17892. 'var p: TPoint;',
  17893. 'begin',
  17894. ' t.x:=t.y;',
  17895. ' p:=t;',
  17896. 'end;',
  17897. 'var',
  17898. ' p: tobject.tpoint = (x:2; y:4);',
  17899. ' o: TObject;',
  17900. 'begin',
  17901. ' p:=p;',
  17902. ' o.doit(p);',
  17903. '']);
  17904. ConvertProgram;
  17905. CheckSource('TestNestedClass_Record',
  17906. LinesToStr([ // statements
  17907. 'rtl.createClass(this, "TObject", null, function () {',
  17908. ' rtl.recNewT(this, "TPoint", function () {',
  17909. ' this.x = 0;',
  17910. ' this.y = 0;',
  17911. ' this.$eq = function (b) {',
  17912. ' return (this.x === b.x) && (this.y === b.y);',
  17913. ' };',
  17914. ' this.$assign = function (s) {',
  17915. ' this.x = s.x;',
  17916. ' this.y = s.y;',
  17917. ' return this;',
  17918. ' };',
  17919. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17920. ' $r.addField("x", rtl.byte);',
  17921. ' $r.addField("y", rtl.byte);',
  17922. ' });',
  17923. ' this.$init = function () {',
  17924. ' };',
  17925. ' this.$final = function () {',
  17926. ' };',
  17927. ' this.DoIt = function (t) {',
  17928. ' var p = this.TPoint.$new();',
  17929. ' t.x = t.y;',
  17930. ' p.$assign(t);',
  17931. ' };',
  17932. '});',
  17933. 'this.p = this.TObject.TPoint.$clone({',
  17934. ' x: 2,',
  17935. ' y: 4',
  17936. '});',
  17937. 'this.o = null;',
  17938. '']),
  17939. LinesToStr([ // $mod.$main
  17940. '$mod.p.$assign($mod.p);',
  17941. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17942. '']));
  17943. end;
  17944. procedure TTestModule.TestNestedClass_Class;
  17945. begin
  17946. StartProgram(false);
  17947. Add([
  17948. 'type',
  17949. ' TObject = class end;',
  17950. ' TBird = class',
  17951. ' type TLeg = class',
  17952. ' FId: longint;',
  17953. ' constructor Create;',
  17954. ' function Create(i: longint): TLeg;',
  17955. ' end;',
  17956. ' function DoIt(b: TBird): Tleg;',
  17957. ' end;',
  17958. 'constructor tbird.tleg.create;',
  17959. 'begin',
  17960. ' FId:=3;',
  17961. 'end;',
  17962. 'function tbird.tleg.Create(i: longint): TLeg;',
  17963. 'begin',
  17964. ' Create;',
  17965. ' Result:=TLeg.Create;',
  17966. ' Result:=TBird.TLeg.Create;',
  17967. ' Result:=Create(3);',
  17968. ' FId:=i;',
  17969. 'end;',
  17970. 'function tbird.DoIt(b: tbird): tleg;',
  17971. 'begin',
  17972. ' Result.Create;',
  17973. ' Result:=TLeg.Create;',
  17974. ' Result:=TBird.TLeg.Create;',
  17975. ' Result:=Result.Create(3);',
  17976. 'end;',
  17977. 'var',
  17978. ' b: Tbird.tleg;',
  17979. 'begin',
  17980. ' b.Create;',
  17981. ' b:=TBird.TLeg.Create;',
  17982. ' b:=b.Create(3);',
  17983. '']);
  17984. ConvertProgram;
  17985. CheckSource('TestNestedClass_Class',
  17986. LinesToStr([ // statements
  17987. 'rtl.createClass(this, "TObject", null, function () {',
  17988. ' this.$init = function () {',
  17989. ' };',
  17990. ' this.$final = function () {',
  17991. ' };',
  17992. '});',
  17993. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17994. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  17995. ' this.$init = function () {',
  17996. ' $mod.TObject.$init.call(this);',
  17997. ' this.FId = 0;',
  17998. ' };',
  17999. ' this.Create = function () {',
  18000. ' this.FId = 3;',
  18001. ' return this;',
  18002. ' };',
  18003. ' this.Create$1 = function (i) {',
  18004. ' var Result = null;',
  18005. ' this.Create();',
  18006. ' Result = $mod.TBird.TLeg.$create("Create");',
  18007. ' Result = $mod.TBird.TLeg.$create("Create");',
  18008. ' Result = this.Create$1(3);',
  18009. ' this.FId = i;',
  18010. ' return Result;',
  18011. ' };',
  18012. ' }, "TBird.TLeg");',
  18013. ' this.DoIt = function (b) {',
  18014. ' var Result = null;',
  18015. ' Result.Create();',
  18016. ' Result = this.TLeg.$create("Create");',
  18017. ' Result = $mod.TBird.TLeg.$create("Create");',
  18018. ' Result = Result.Create$1(3);',
  18019. ' return Result;',
  18020. ' };',
  18021. '});',
  18022. 'this.b = null;',
  18023. '']),
  18024. LinesToStr([ // $mod.$main
  18025. '$mod.b.Create();',
  18026. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18027. '$mod.b = $mod.b.Create$1(3);',
  18028. '']));
  18029. end;
  18030. procedure TTestModule.TestNestedClass_CallInherited;
  18031. begin
  18032. StartProgram(false);
  18033. Add([
  18034. 'type',
  18035. ' TObject = class end;',
  18036. ' TBird = class',
  18037. ' type',
  18038. ' TWing = class',
  18039. ' function Fly(w: word = 17): word; virtual;',
  18040. ' end;',
  18041. ' end;',
  18042. ' TEagle = class(TBird)',
  18043. ' type',
  18044. ' TEagleWing = class(TWing)',
  18045. ' function Fly(w: word): word; override;',
  18046. ' end;',
  18047. ' end;',
  18048. 'function TBird.TWing.Fly(w: word): word;',
  18049. 'begin',
  18050. 'end;',
  18051. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18052. 'begin',
  18053. ' inherited;',
  18054. ' inherited Fly;',
  18055. ' inherited Fly(3);',
  18056. ' Result:=inherited Fly;',
  18057. ' Result:=inherited Fly(4);',
  18058. 'end;',
  18059. 'begin',
  18060. '']);
  18061. ConvertProgram;
  18062. CheckSource('TestNestedClass_CallInherited',
  18063. LinesToStr([ // statements
  18064. 'rtl.createClass(this, "TObject", null, function () {',
  18065. ' this.$init = function () {',
  18066. ' };',
  18067. ' this.$final = function () {',
  18068. ' };',
  18069. '});',
  18070. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18071. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18072. ' this.Fly = function (w) {',
  18073. ' var Result = 0;',
  18074. ' return Result;',
  18075. ' };',
  18076. ' }, "TBird.TWing");',
  18077. '});',
  18078. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18079. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18080. ' this.Fly = function (w) {',
  18081. ' var Result = 0;',
  18082. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18083. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18084. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18085. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18086. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18087. ' return Result;',
  18088. ' };',
  18089. ' }, "TEagle.TEagleWing");',
  18090. '});',
  18091. '']),
  18092. LinesToStr([ // $mod.$main
  18093. '']));
  18094. end;
  18095. procedure TTestModule.TestExternalClass_Var;
  18096. begin
  18097. StartProgram(false);
  18098. Add([
  18099. '{$modeswitch externalclass}',
  18100. 'type',
  18101. ' TExtA = class external name ''ExtObj''',
  18102. ' Id: longint external name ''$Id'';',
  18103. ' B: longint;',
  18104. ' end;',
  18105. 'var Obj: TExtA;',
  18106. 'begin',
  18107. ' obj.id:=obj.id+1;',
  18108. ' obj.B:=obj.B+1;']);
  18109. ConvertProgram;
  18110. CheckSource('TestExternalClass_Var',
  18111. LinesToStr([ // statements
  18112. 'this.Obj = null;',
  18113. '']),
  18114. LinesToStr([ // $mod.$main
  18115. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18116. '$mod.Obj.B = $mod.Obj.B + 1;',
  18117. '']));
  18118. end;
  18119. procedure TTestModule.TestExternalClass_Const;
  18120. begin
  18121. StartProgram(false);
  18122. Add([
  18123. '{$modeswitch externalclass}',
  18124. 'type',
  18125. ' TExtA = class external name ''ExtObj''',
  18126. ' const Two: longint = 2;',
  18127. ' const Three = 3;',
  18128. ' const Id: longint;',
  18129. ' end;',
  18130. ' TExtB = class external name ''ExtB''',
  18131. ' A: TExtA;',
  18132. ' end;',
  18133. 'var',
  18134. ' A: texta;',
  18135. ' B: textb;',
  18136. ' i: longint;',
  18137. 'begin',
  18138. ' i:=a.two;',
  18139. ' i:=texta.two;',
  18140. ' i:=a.three;',
  18141. ' i:=texta.three;',
  18142. ' i:=a.id;',
  18143. ' i:=texta.id;',
  18144. '']);
  18145. ConvertProgram;
  18146. CheckSource('TestExternalClass_Const',
  18147. LinesToStr([ // statements
  18148. 'this.A = null;',
  18149. 'this.B = null;',
  18150. 'this.i = 0;',
  18151. '']),
  18152. LinesToStr([ // $mod.$main
  18153. '$mod.i = 2;',
  18154. '$mod.i = 2;',
  18155. '$mod.i = 3;',
  18156. '$mod.i = 3;',
  18157. '$mod.i = $mod.A.Id;',
  18158. '$mod.i = ExtObj.Id;',
  18159. '']));
  18160. end;
  18161. procedure TTestModule.TestExternalClass_Dollar;
  18162. begin
  18163. StartProgram(false);
  18164. Add([
  18165. '{$modeswitch externalclass}',
  18166. 'type',
  18167. ' TExtA = class external name ''$''',
  18168. ' Id: longint external name ''$'';',
  18169. ' function Bla(i: longint): longint; external name ''$'';',
  18170. ' end;',
  18171. 'function dollar(k: longint): longint; external name ''$'';',
  18172. 'var Obj: TExtA;',
  18173. 'begin',
  18174. ' dollar(1);',
  18175. ' obj.id:=obj.id+2;',
  18176. ' obj.Bla(3);',
  18177. '']);
  18178. ConvertProgram;
  18179. CheckSource('TestExternalClass_Dollar',
  18180. LinesToStr([ // statements
  18181. 'this.Obj = null;',
  18182. '']),
  18183. LinesToStr([ // $mod.$main
  18184. '$(1);',
  18185. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18186. '$mod.Obj.$(3);',
  18187. '']));
  18188. end;
  18189. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18190. begin
  18191. StartProgram(false);
  18192. Add('{$modeswitch externalclass}');
  18193. Add('type');
  18194. Add(' TExtA = class external name ''ExtA''');
  18195. Add(' Id: longint external name ''$Id'';');
  18196. Add(' end;');
  18197. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18198. Add(' Id: longint;');
  18199. Add(' end;');
  18200. Add('begin');
  18201. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18202. ConvertProgram;
  18203. end;
  18204. procedure TTestModule.TestExternalClass_Method;
  18205. begin
  18206. StartProgram(false);
  18207. Add(['{$modeswitch externalclass}',
  18208. 'type',
  18209. ' TExtA = class external name ''ExtObj''',
  18210. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18211. ' procedure DoSome(Id: longint = 1);',
  18212. ' end;',
  18213. 'var Obj: texta;',
  18214. 'begin',
  18215. ' obj.doit;',
  18216. ' obj.doit();',
  18217. ' obj.doit(2);',
  18218. ' with obj do begin',
  18219. ' doit;',
  18220. ' doit();',
  18221. ' doit(3);',
  18222. ' end;']);
  18223. ConvertProgram;
  18224. CheckSource('TestExternalClass_Method',
  18225. LinesToStr([ // statements
  18226. 'this.Obj = null;',
  18227. '']),
  18228. LinesToStr([ // $mod.$main
  18229. '$mod.Obj.$Execute(1);',
  18230. '$mod.Obj.$Execute(1);',
  18231. '$mod.Obj.$Execute(2);',
  18232. 'var $with = $mod.Obj;',
  18233. '$with.$Execute(1);',
  18234. '$with.$Execute(1);',
  18235. '$with.$Execute(3);',
  18236. '']));
  18237. end;
  18238. procedure TTestModule.TestExternalClass_ClassMethod;
  18239. begin
  18240. StartProgram(false);
  18241. Add([
  18242. '{$modeswitch externalclass}',
  18243. 'type',
  18244. ' TExtA = class external name ''ExtObj''',
  18245. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18246. ' end;',
  18247. ' TExtB = TExtA;',
  18248. 'var p: Pointer;',
  18249. 'begin',
  18250. ' texta.doit;',
  18251. ' texta.doit();',
  18252. ' texta.doit(2);',
  18253. ' p:[email protected];',
  18254. ' with texta do begin',
  18255. ' doit;',
  18256. ' doit();',
  18257. ' doit(3);',
  18258. ' p:=@DoIt;',
  18259. ' end;',
  18260. ' textb.doit;',
  18261. ' textb.doit();',
  18262. ' textb.doit(4);',
  18263. ' with textb do begin',
  18264. ' doit;',
  18265. ' doit();',
  18266. ' doit(5);',
  18267. ' end;',
  18268. '']);
  18269. ConvertProgram;
  18270. CheckSource('TestExternalClass_ClassMethod',
  18271. LinesToStr([ // statements
  18272. 'this.p = null;',
  18273. '']),
  18274. LinesToStr([ // $mod.$main
  18275. 'ExtObj.$Execute(1);',
  18276. 'ExtObj.$Execute(1);',
  18277. 'ExtObj.$Execute(2);',
  18278. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18279. 'ExtObj.$Execute(1);',
  18280. 'ExtObj.$Execute(1);',
  18281. 'ExtObj.$Execute(3);',
  18282. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18283. 'ExtObj.$Execute(1);',
  18284. 'ExtObj.$Execute(1);',
  18285. 'ExtObj.$Execute(4);',
  18286. 'ExtObj.$Execute(1);',
  18287. 'ExtObj.$Execute(1);',
  18288. 'ExtObj.$Execute(5);',
  18289. '']));
  18290. end;
  18291. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18292. begin
  18293. StartProgram(false);
  18294. Add([
  18295. '{$modeswitch externalclass}',
  18296. 'type',
  18297. ' TExtA = class external name ''ExtObj''',
  18298. ' class procedure DoIt(Id: longint = 1); static;',
  18299. ' end;',
  18300. 'var p: Pointer;',
  18301. 'begin',
  18302. ' texta.doit;',
  18303. ' texta.doit();',
  18304. ' texta.doit(2);',
  18305. ' p:[email protected];',
  18306. ' with texta do begin',
  18307. ' doit;',
  18308. ' doit();',
  18309. ' doit(3);',
  18310. ' p:=@DoIt;',
  18311. ' end;',
  18312. '']);
  18313. ConvertProgram;
  18314. CheckSource('TestExternalClass_ClassMethodStatic',
  18315. LinesToStr([ // statements
  18316. 'this.p = null;',
  18317. '']),
  18318. LinesToStr([ // $mod.$main
  18319. 'ExtObj.DoIt(1);',
  18320. 'ExtObj.DoIt(1);',
  18321. 'ExtObj.DoIt(2);',
  18322. '$mod.p = ExtObj.DoIt;',
  18323. 'ExtObj.DoIt(1);',
  18324. 'ExtObj.DoIt(1);',
  18325. 'ExtObj.DoIt(3);',
  18326. '$mod.p = ExtObj.DoIt;',
  18327. '']));
  18328. end;
  18329. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18330. begin
  18331. StartProgram(false);
  18332. Add([
  18333. '{$modeswitch externalclass}',
  18334. 'type',
  18335. ' TBird = class external name ''Array''',
  18336. ' end;',
  18337. 'function GetPtr: Pointer;',
  18338. 'begin',
  18339. 'end;',
  18340. 'procedure Write(const p);',
  18341. 'begin',
  18342. 'end;',
  18343. 'procedure WriteLn; varargs;',
  18344. 'begin',
  18345. 'end;',
  18346. 'begin',
  18347. ' if TBird(GetPtr)=nil then ;',
  18348. ' Write(GetPtr);',
  18349. ' WriteLn(GetPtr);',
  18350. ' Write(TBird(GetPtr));',
  18351. ' WriteLn(TBird(GetPtr));',
  18352. '']);
  18353. ConvertProgram;
  18354. CheckSource('TestFunctionResultInTypeCast',
  18355. LinesToStr([ // statements
  18356. 'this.GetPtr = function () {',
  18357. ' var Result = null;',
  18358. ' return Result;',
  18359. '};',
  18360. 'this.Write = function (p) {',
  18361. '};',
  18362. 'this.WriteLn = function () {',
  18363. '};',
  18364. '']),
  18365. LinesToStr([
  18366. 'if ($mod.GetPtr() === null) ;',
  18367. '$mod.Write($mod.GetPtr());',
  18368. '$mod.WriteLn($mod.GetPtr());',
  18369. '$mod.Write($mod.GetPtr());',
  18370. '$mod.WriteLn($mod.GetPtr());',
  18371. '']));
  18372. end;
  18373. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18374. begin
  18375. StartProgram(false);
  18376. Add([
  18377. '{$modeswitch externalclass}',
  18378. 'type',
  18379. ' TExtA = class external name ''ExtObjA''',
  18380. ' procedure ProcA; virtual;',
  18381. ' procedure ProcB; virtual;',
  18382. ' end;',
  18383. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18384. ' end;',
  18385. ' TExtC = class (TExtB)',
  18386. ' procedure ProcA; override;',
  18387. ' end;',
  18388. 'procedure TExtC.ProcA;',
  18389. 'begin',
  18390. ' ProcA;',
  18391. ' Self.ProcA;',
  18392. ' ProcB;',
  18393. ' Self.ProcB;',
  18394. 'end;',
  18395. 'var',
  18396. ' A: texta;',
  18397. ' B: textb;',
  18398. ' C: textc;',
  18399. 'begin',
  18400. ' a.proca;',
  18401. ' b.proca;',
  18402. ' c.proca;']);
  18403. ConvertProgram;
  18404. CheckSource('TestExternalClass_NonExternalOverride',
  18405. LinesToStr([ // statements
  18406. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18407. ' this.$init = function () {',
  18408. ' };',
  18409. ' this.$final = function () {',
  18410. ' };',
  18411. ' this.ProcA = function () {',
  18412. ' this.ProcA();',
  18413. ' this.ProcA();',
  18414. ' this.ProcB();',
  18415. ' this.ProcB();',
  18416. ' };',
  18417. '});',
  18418. 'this.A = null;',
  18419. 'this.B = null;',
  18420. 'this.C = null;',
  18421. '']),
  18422. LinesToStr([ // $mod.$main
  18423. '$mod.A.ProcA();',
  18424. '$mod.B.ProcA();',
  18425. '$mod.C.ProcA();',
  18426. '']));
  18427. end;
  18428. procedure TTestModule.TestExternalClass_OverloadHint;
  18429. begin
  18430. StartProgram(false);
  18431. Add([
  18432. '{$modeswitch externalclass}',
  18433. 'type',
  18434. ' TExtA = class external name ''ExtObjA''',
  18435. ' procedure DoIt;',
  18436. ' procedure DoIt(i: longint);',
  18437. ' end;',
  18438. 'begin',
  18439. '']);
  18440. ConvertProgram;
  18441. CheckResolverUnexpectedHints(true);
  18442. CheckSource('TestExternalClass_OverloadHint',
  18443. LinesToStr([ // statements
  18444. '']),
  18445. LinesToStr([ // $mod.$main
  18446. '']));
  18447. end;
  18448. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18449. begin
  18450. WithTypeInfo:=true;
  18451. StartProgram(false);
  18452. Add([
  18453. '{$modeswitch externalclass}',
  18454. 'type',
  18455. ' JSwiper = class external name ''Swiper''',
  18456. ' constructor New;',
  18457. ' end;',
  18458. ' TObject = class',
  18459. ' private',
  18460. ' FSwiper: JSwiper;',
  18461. ' published',
  18462. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18463. ' end;',
  18464. 'begin',
  18465. ' JSwiper.new;',
  18466. '']);
  18467. ConvertProgram;
  18468. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18469. LinesToStr([ // statements
  18470. 'this.$rtti.$ExtClass("JSwiper", {',
  18471. ' jsclass: "Swiper"',
  18472. '});',
  18473. 'rtl.createClass(this, "TObject", null, function () {',
  18474. ' this.$init = function () {',
  18475. ' this.FSwiper = null;',
  18476. ' };',
  18477. ' this.$final = function () {',
  18478. ' this.FSwiper = undefined;',
  18479. ' };',
  18480. ' var $r = this.$rtti;',
  18481. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18482. '});',
  18483. '']),
  18484. LinesToStr([ // $mod.$main
  18485. 'new Swiper();',
  18486. '']));
  18487. end;
  18488. procedure TTestModule.TestExternalClass_Property;
  18489. begin
  18490. StartProgram(false);
  18491. Add([
  18492. '{$modeswitch externalclass}',
  18493. 'type',
  18494. ' TExtA = class external name ''ExtA''',
  18495. ' function getYear: longint;',
  18496. ' procedure setYear(Value: longint);',
  18497. ' property Year: longint read getyear write setyear;',
  18498. ' end;',
  18499. ' TExtB = class (TExtA)',
  18500. ' procedure OtherSetYear(Value: longint);',
  18501. ' property year write othersetyear;',
  18502. ' end;',
  18503. 'procedure textb.othersetyear(value: longint);',
  18504. 'begin',
  18505. ' setYear(Value+4);',
  18506. 'end;',
  18507. 'var',
  18508. ' A: texta;',
  18509. ' B: textb;',
  18510. 'begin',
  18511. ' a.year:=a.year+1;',
  18512. ' b.year:=b.year+2;']);
  18513. ConvertProgram;
  18514. CheckSource('TestExternalClass_NonExternalOverride',
  18515. LinesToStr([ // statements
  18516. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18517. ' this.$init = function () {',
  18518. ' };',
  18519. ' this.$final = function () {',
  18520. ' };',
  18521. ' this.OtherSetYear = function (Value) {',
  18522. ' this.setYear(Value+4);',
  18523. ' };',
  18524. '});',
  18525. 'this.A = null;',
  18526. 'this.B = null;',
  18527. '']),
  18528. LinesToStr([ // $mod.$main
  18529. '$mod.A.setYear($mod.A.getYear()+1);',
  18530. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18531. '']));
  18532. end;
  18533. procedure TTestModule.TestExternalClass_PropertyDate;
  18534. begin
  18535. StartProgram(false);
  18536. Add([
  18537. '{$modeswitch externalclass}',
  18538. 'type',
  18539. ' TExtA = class external name ''ExtA''',
  18540. ' end;',
  18541. ' TExtB = class (TExtA)',
  18542. ' FDate: string;',
  18543. ' property Date: string read FDate write FDate;',
  18544. ' property ExtA: string read FDate write FDate;',
  18545. ' end;',
  18546. ' {$M+}',
  18547. ' TObject = class',
  18548. ' FDate: string;',
  18549. ' published',
  18550. ' property Date: string read FDate write FDate;',
  18551. ' property ExtA: string read FDate write FDate;',
  18552. ' end;',
  18553. 'var',
  18554. ' B: textb;',
  18555. ' o: TObject;',
  18556. 'begin',
  18557. ' b.date:=b.exta;',
  18558. ' o.date:=o.exta;']);
  18559. ConvertProgram;
  18560. CheckSource('TestExternalClass_PropertyDate',
  18561. LinesToStr([ // statements
  18562. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18563. ' this.$init = function () {',
  18564. ' this.FDate = "";',
  18565. ' };',
  18566. ' this.$final = function () {',
  18567. ' };',
  18568. '});',
  18569. 'rtl.createClass(this, "TObject", null, function () {',
  18570. ' this.$init = function () {',
  18571. ' this.FDate = "";',
  18572. ' };',
  18573. ' this.$final = function () {',
  18574. ' };',
  18575. ' var $r = this.$rtti;',
  18576. ' $r.addField("FDate", rtl.string);',
  18577. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18578. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18579. '});',
  18580. 'this.B = null;',
  18581. 'this.o = null;',
  18582. '']),
  18583. LinesToStr([ // $mod.$main
  18584. '$mod.B.FDate = $mod.B.FDate;',
  18585. '$mod.o.FDate = $mod.o.FDate;',
  18586. '']));
  18587. end;
  18588. procedure TTestModule.TestExternalClass_ClassProperty;
  18589. begin
  18590. StartProgram(false);
  18591. Add('{$modeswitch externalclass}');
  18592. Add('type');
  18593. Add(' TExtA = class external name ''ExtA''');
  18594. Add(' class function getYear: longint;');
  18595. Add(' class procedure setYear(Value: longint);');
  18596. Add(' class property Year: longint read getyear write setyear;');
  18597. Add(' end;');
  18598. Add(' TExtB = class (TExtA)');
  18599. Add(' class function GetCentury: longint;');
  18600. Add(' class procedure SetCentury(Value: longint);');
  18601. Add(' class property Century: longint read getcentury write setcentury;');
  18602. Add(' end;');
  18603. Add('class function textb.getcentury: longint;');
  18604. Add('begin');
  18605. Add('end;');
  18606. Add('class procedure textb.setcentury(value: longint);');
  18607. Add('begin');
  18608. Add(' setyear(value+11);');
  18609. Add(' texta.year:=texta.year+12;');
  18610. Add(' year:=year+13;');
  18611. Add(' textb.century:=textb.century+14;');
  18612. Add(' century:=century+15;');
  18613. Add('end;');
  18614. Add('var');
  18615. Add(' A: texta;');
  18616. Add(' B: textb;');
  18617. Add('begin');
  18618. Add(' texta.year:=texta.year+1;');
  18619. Add(' textb.year:=textb.year+2;');
  18620. Add(' TextA.year:=TextA.year+3;');
  18621. Add(' b.year:=b.year+4;');
  18622. Add(' textb.century:=textb.century+5;');
  18623. Add(' b.century:=b.century+6;');
  18624. ConvertProgram;
  18625. CheckSource('TestExternalClass_ClassProperty',
  18626. LinesToStr([ // statements
  18627. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18628. ' this.$init = function () {',
  18629. ' };',
  18630. ' this.$final = function () {',
  18631. ' };',
  18632. ' this.GetCentury = function () {',
  18633. ' var Result = 0;',
  18634. ' return Result;',
  18635. ' };',
  18636. ' this.SetCentury = function (Value) {',
  18637. ' this.setYear(Value + 11);',
  18638. ' ExtA.setYear(ExtA.getYear() + 12);',
  18639. ' this.setYear(this.getYear() + 13);',
  18640. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18641. ' this.SetCentury(this.GetCentury() + 15);',
  18642. ' };',
  18643. '});',
  18644. 'this.A = null;',
  18645. 'this.B = null;',
  18646. '']),
  18647. LinesToStr([ // $mod.$main
  18648. 'ExtA.setYear(ExtA.getYear() + 1);',
  18649. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18650. 'ExtA.setYear(ExtA.getYear() + 3);',
  18651. '$mod.B.setYear($mod.B.getYear() + 4);',
  18652. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18653. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18654. '']));
  18655. end;
  18656. procedure TTestModule.TestExternalClass_ClassOf;
  18657. begin
  18658. StartProgram(false);
  18659. Add('{$modeswitch externalclass}');
  18660. Add('type');
  18661. Add(' TExtA = class external name ''ExtA''');
  18662. Add(' procedure ProcA; virtual;');
  18663. Add(' procedure ProcB; virtual;');
  18664. Add(' end;');
  18665. Add(' TExtAClass = class of TExtA;');
  18666. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18667. Add(' end;');
  18668. Add(' TExtBClass = class of TExtB;');
  18669. Add(' TExtC = class (TExtB)');
  18670. Add(' procedure ProcA; override;');
  18671. Add(' end;');
  18672. Add(' TExtCClass = class of TExtC;');
  18673. Add('procedure TExtC.ProcA; begin end;');
  18674. Add('var');
  18675. Add(' A: texta; ClA: TExtAClass;');
  18676. Add(' B: textb; ClB: TExtBClass;');
  18677. Add(' C: textc; ClC: TExtCClass;');
  18678. Add('begin');
  18679. Add(' ClA:=texta;');
  18680. Add(' ClA:=textb;');
  18681. Add(' ClA:=textc;');
  18682. Add(' ClB:=textb;');
  18683. Add(' ClB:=textc;');
  18684. Add(' ClC:=textc;');
  18685. ConvertProgram;
  18686. CheckSource('TestExternalClass_ClassOf',
  18687. LinesToStr([ // statements
  18688. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18689. ' this.$init = function () {',
  18690. ' };',
  18691. ' this.$final = function () {',
  18692. ' };',
  18693. ' this.ProcA = function () {',
  18694. ' };',
  18695. '});',
  18696. 'this.A = null;',
  18697. 'this.ClA = null;',
  18698. 'this.B = null;',
  18699. 'this.ClB = null;',
  18700. 'this.C = null;',
  18701. 'this.ClC = null;',
  18702. '']),
  18703. LinesToStr([ // $mod.$main
  18704. '$mod.ClA = ExtA;',
  18705. '$mod.ClA = ExtB;',
  18706. '$mod.ClA = $mod.TExtC;',
  18707. '$mod.ClB = ExtB;',
  18708. '$mod.ClB = $mod.TExtC;',
  18709. '$mod.ClC = $mod.TExtC;',
  18710. '']));
  18711. end;
  18712. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18713. begin
  18714. AddModuleWithIntfImplSrc('unit2.pas',
  18715. LinesToStr([
  18716. '{$modeswitch externalclass}',
  18717. 'type',
  18718. ' TExtA = class external name ''ExtA''',
  18719. ' class var Id: longint;',
  18720. ' end;',
  18721. '']),
  18722. '');
  18723. StartUnit(true);
  18724. Add('interface');
  18725. Add('uses unit2;');
  18726. Add('implementation');
  18727. Add('begin');
  18728. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18729. ConvertUnit;
  18730. CheckSource('TestExternalClass_ClassOtherUnit',
  18731. LinesToStr([
  18732. '']),
  18733. LinesToStr([
  18734. 'ExtA.Id = ExtA.Id + 1;',
  18735. '']));
  18736. end;
  18737. procedure TTestModule.TestExternalClass_Is;
  18738. begin
  18739. StartProgram(false);
  18740. Add([
  18741. '{$modeswitch externalclass}',
  18742. 'type',
  18743. ' TExtA = class external name ''ExtA''',
  18744. ' end;',
  18745. ' TExtAClass = class of TExtA;',
  18746. ' TExtB = class external name ''ExtB'' (TExtA)',
  18747. ' end;',
  18748. ' TExtBClass = class of TExtB;',
  18749. ' TExtC = class (TExtB)',
  18750. ' end;',
  18751. ' TExtCClass = class of TExtC;',
  18752. 'var',
  18753. ' A: texta; ClA: TExtAClass;',
  18754. ' B: textb; ClB: TExtBClass;',
  18755. ' C: textc; ClC: TExtCClass;',
  18756. 'begin',
  18757. ' if a is textb then ;',
  18758. ' if a is textc then ;',
  18759. ' if b is textc then ;',
  18760. ' if cla is textb then ;',
  18761. ' if cla is textc then ;',
  18762. ' if clb is textc then ;',
  18763. ' try',
  18764. ' except',
  18765. ' on TExtA do ;',
  18766. ' on e: TExtB do ;',
  18767. ' end;',
  18768. '']);
  18769. ConvertProgram;
  18770. CheckSource('TestExternalClass_Is',
  18771. LinesToStr([ // statements
  18772. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18773. ' this.$init = function () {',
  18774. ' };',
  18775. ' this.$final = function () {',
  18776. ' };',
  18777. '});',
  18778. 'this.A = null;',
  18779. 'this.ClA = null;',
  18780. 'this.B = null;',
  18781. 'this.ClB = null;',
  18782. 'this.C = null;',
  18783. 'this.ClC = null;',
  18784. '']),
  18785. LinesToStr([ // $mod.$main
  18786. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18787. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18788. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18789. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18790. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18791. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18792. 'try {} catch ($e) {',
  18793. ' if (rtl.isExt($e,ExtA)) {}',
  18794. ' else if (rtl.isExt($e,ExtB)) {',
  18795. ' var e = $e;',
  18796. ' } else throw $e',
  18797. '};',
  18798. '']));
  18799. end;
  18800. procedure TTestModule.TestExternalClass_As;
  18801. begin
  18802. StartProgram(false);
  18803. Add('{$modeswitch externalclass}');
  18804. Add('type');
  18805. Add(' TExtA = class external name ''ExtA''');
  18806. Add(' end;');
  18807. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18808. Add(' end;');
  18809. Add(' TExtC = class (TExtB)');
  18810. Add(' end;');
  18811. Add('var');
  18812. Add(' A: texta;');
  18813. Add(' B: textb;');
  18814. Add(' C: textc;');
  18815. Add('begin');
  18816. Add(' b:=a as textb;');
  18817. Add(' c:=a as textc;');
  18818. Add(' c:=b as textc;');
  18819. ConvertProgram;
  18820. CheckSource('TestExternalClass_Is',
  18821. LinesToStr([ // statements
  18822. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18823. ' this.$init = function () {',
  18824. ' };',
  18825. ' this.$final = function () {',
  18826. ' };',
  18827. '});',
  18828. 'this.A = null;',
  18829. 'this.B = null;',
  18830. 'this.C = null;',
  18831. '']),
  18832. LinesToStr([ // $mod.$main
  18833. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18834. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18835. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18836. '']));
  18837. end;
  18838. procedure TTestModule.TestExternalClass_DestructorFail;
  18839. begin
  18840. StartProgram(false);
  18841. Add('{$modeswitch externalclass}');
  18842. Add('type');
  18843. Add(' TExtA = class external name ''ExtA''');
  18844. Add(' destructor Free;');
  18845. Add(' end;');
  18846. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18847. nPasElementNotSupported);
  18848. ConvertProgram;
  18849. end;
  18850. procedure TTestModule.TestExternalClass_New;
  18851. begin
  18852. StartProgram(false);
  18853. Add([
  18854. '{$modeswitch externalclass}',
  18855. 'type',
  18856. ' TExtA = class external name ''ExtA''',
  18857. ' constructor New;',
  18858. ' constructor New(i: longint; j: longint = 2);',
  18859. ' end;',
  18860. 'var',
  18861. ' A: texta;',
  18862. 'begin',
  18863. ' a:=texta.new;',
  18864. ' a:=texta(texta.new);',
  18865. ' a:=texta.new();',
  18866. ' a:=texta.new(1);',
  18867. ' with texta do begin',
  18868. ' a:=new;',
  18869. ' a:=new();',
  18870. ' a:=new(2);',
  18871. ' end;',
  18872. ' a:=test1.texta.new;',
  18873. ' a:=test1.texta.new();',
  18874. ' a:=test1.texta.new(3);',
  18875. '']);
  18876. ConvertProgram;
  18877. CheckSource('TestExternalClass_New',
  18878. LinesToStr([ // statements
  18879. 'this.A = null;',
  18880. '']),
  18881. LinesToStr([ // $mod.$main
  18882. '$mod.A = new ExtA();',
  18883. '$mod.A = new ExtA();',
  18884. '$mod.A = new ExtA();',
  18885. '$mod.A = new ExtA(1,2);',
  18886. '$mod.A = new ExtA();',
  18887. '$mod.A = new ExtA();',
  18888. '$mod.A = new ExtA(2,2);',
  18889. '$mod.A = new ExtA();',
  18890. '$mod.A = new ExtA();',
  18891. '$mod.A = new ExtA(3,2);',
  18892. '']));
  18893. end;
  18894. procedure TTestModule.TestExternalClass_ClassOf_New;
  18895. begin
  18896. StartProgram(false);
  18897. Add('{$modeswitch externalclass}');
  18898. Add('type');
  18899. Add(' TExtAClass = class of TExtA;');
  18900. Add(' TExtA = class external name ''ExtA''');
  18901. Add(' C: TExtAClass;');
  18902. Add(' constructor New;');
  18903. Add(' end;');
  18904. Add('var');
  18905. Add(' A: texta;');
  18906. Add(' C: textaclass;');
  18907. Add('begin');
  18908. Add(' a:=c.new;');
  18909. Add(' a:=c.new();');
  18910. Add(' with C do begin');
  18911. Add(' a:=new;');
  18912. Add(' a:=new();');
  18913. Add(' end;');
  18914. Add(' a:=test1.c.new;');
  18915. Add(' a:=test1.c.new();');
  18916. Add(' a:=A.c.new();');
  18917. ConvertProgram;
  18918. CheckSource('TestExternalClass_ClassOf_New',
  18919. LinesToStr([ // statements
  18920. 'this.A = null;',
  18921. 'this.C = null;',
  18922. '']),
  18923. LinesToStr([ // $mod.$main
  18924. '$mod.A = new $mod.C();',
  18925. '$mod.A = new $mod.C();',
  18926. 'var $with = $mod.C;',
  18927. '$mod.A = new $with();',
  18928. '$mod.A = new $with();',
  18929. '$mod.A = new $mod.C();',
  18930. '$mod.A = new $mod.C();',
  18931. '$mod.A = new $mod.A.C();',
  18932. '']));
  18933. end;
  18934. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18935. begin
  18936. StartProgram(false);
  18937. Add([
  18938. '{$modeswitch externalclass}',
  18939. 'type',
  18940. ' TExtAClass = class of TExtA;',
  18941. ' TExtA = class external name ''ExtA''',
  18942. ' constructor New;',
  18943. ' end;',
  18944. 'function GetCreator: TExtAClass;',
  18945. 'begin',
  18946. ' Result:=TExtA;',
  18947. 'end;',
  18948. 'var',
  18949. ' A: texta;',
  18950. 'begin',
  18951. ' a:=getcreator.new;',
  18952. ' a:=getcreator().new;',
  18953. ' a:=getcreator().new();',
  18954. ' a:=getcreator.new();',
  18955. ' with getcreator do begin',
  18956. ' a:=new;',
  18957. ' a:=new();',
  18958. ' end;']);
  18959. ConvertProgram;
  18960. CheckSource('TestExternalClass_FuncClassOf_New',
  18961. LinesToStr([ // statements
  18962. 'this.GetCreator = function () {',
  18963. ' var Result = null;',
  18964. ' Result = ExtA;',
  18965. ' return Result;',
  18966. '};',
  18967. 'this.A = null;',
  18968. '']),
  18969. LinesToStr([ // $mod.$main
  18970. '$mod.A = new ($mod.GetCreator())();',
  18971. '$mod.A = new ($mod.GetCreator())();',
  18972. '$mod.A = new ($mod.GetCreator())();',
  18973. '$mod.A = new ($mod.GetCreator())();',
  18974. 'var $with = $mod.GetCreator();',
  18975. '$mod.A = new $with();',
  18976. '$mod.A = new $with();',
  18977. '']));
  18978. end;
  18979. procedure TTestModule.TestExternalClass_New_PasClassFail;
  18980. begin
  18981. StartProgram(false);
  18982. Add([
  18983. '{$modeswitch externalclass}',
  18984. 'type',
  18985. ' TExtA = class external name ''ExtA''',
  18986. ' constructor New;',
  18987. ' end;',
  18988. ' TBird = class(TExtA)',
  18989. ' end;',
  18990. 'begin',
  18991. ' TBird.new;',
  18992. '']);
  18993. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18994. ConvertProgram;
  18995. end;
  18996. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  18997. begin
  18998. StartProgram(false);
  18999. Add([
  19000. '{$modeswitch externalclass}',
  19001. 'type',
  19002. ' TExtA = class external name ''ExtA''',
  19003. ' constructor New;',
  19004. ' end;',
  19005. ' TBird = class(TExtA)',
  19006. ' end;',
  19007. 'begin',
  19008. ' TBird.new();',
  19009. '']);
  19010. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19011. ConvertProgram;
  19012. end;
  19013. procedure TTestModule.TestExternalClass_NewExtName;
  19014. begin
  19015. StartProgram(false);
  19016. Add([
  19017. '{$modeswitch externalclass}',
  19018. 'type',
  19019. ' TExtA = class external name ''ExtA''',
  19020. ' constructor New; external name ''Other'';',
  19021. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19022. ' end;',
  19023. 'var',
  19024. ' A: texta;',
  19025. 'begin',
  19026. ' a:=texta.new;',
  19027. ' a:=texta(texta.new);',
  19028. ' a:=texta.new();',
  19029. ' a:=texta.new(1);',
  19030. ' with texta do begin',
  19031. ' a:=new;',
  19032. ' a:=new();',
  19033. ' a:=new(2);',
  19034. ' end;',
  19035. ' a:=test1.texta.new;',
  19036. ' a:=test1.texta.new();',
  19037. ' a:=test1.texta.new(3);',
  19038. '']);
  19039. ConvertProgram;
  19040. CheckSource('TestExternalClass_NewExtName',
  19041. LinesToStr([ // statements
  19042. 'this.A = null;',
  19043. '']),
  19044. LinesToStr([ // $mod.$main
  19045. '$mod.A = new Other();',
  19046. '$mod.A = new Other();',
  19047. '$mod.A = new Other();',
  19048. '$mod.A = new A.B(1,2);',
  19049. '$mod.A = new Other();',
  19050. '$mod.A = new Other();',
  19051. '$mod.A = new A.B(2,2);',
  19052. '$mod.A = new Other();',
  19053. '$mod.A = new Other();',
  19054. '$mod.A = new A.B(3,2);',
  19055. '']));
  19056. end;
  19057. procedure TTestModule.TestExternalClass_Constructor;
  19058. begin
  19059. StartProgram(false);
  19060. Add([
  19061. '{$modeswitch externalclass}',
  19062. 'type',
  19063. ' TExtA = class external name ''ExtA''',
  19064. ' public type',
  19065. ' TExtB = class external name ''ExtB''',
  19066. ' public type',
  19067. ' TExtC = class external name ''ExtC''',
  19068. ' constructor New;',
  19069. ' constructor New(i: word);',
  19070. ' end;',
  19071. ' end;',
  19072. ' constructor Create;',
  19073. ' constructor Create(i: longint; j: longint = 2);',
  19074. ' end;',
  19075. 'var',
  19076. ' A: texta;',
  19077. ' C: texta.textb.textc;',
  19078. 'begin',
  19079. ' a:=texta.create;',
  19080. ' a:=texta(texta.create);',
  19081. ' a:=texta.create();',
  19082. ' a:=texta.create(1);',
  19083. ' with texta do begin',
  19084. ' a:=create;',
  19085. ' a:=create();',
  19086. ' a:=create(2);',
  19087. ' end;',
  19088. ' a:=test1.texta.create;',
  19089. ' a:=test1.texta.create();',
  19090. ' a:=test1.texta.create(3);',
  19091. ' c:=texta.textb.textc.new;',
  19092. ' c:=texta.textb.textc.new();',
  19093. ' c:=texta.textb.textc.new(4);',
  19094. '']);
  19095. ConvertProgram;
  19096. CheckSource('TestExternalClass_Constructor',
  19097. LinesToStr([ // statements
  19098. 'this.A = null;',
  19099. 'this.C = null;',
  19100. '']),
  19101. LinesToStr([ // $mod.$main
  19102. '$mod.A = new ExtA.Create();',
  19103. '$mod.A = new ExtA.Create();',
  19104. '$mod.A = new ExtA.Create();',
  19105. '$mod.A = new ExtA.Create(1,2);',
  19106. '$mod.A = new ExtA.Create();',
  19107. '$mod.A = new ExtA.Create();',
  19108. '$mod.A = new ExtA.Create(2,2);',
  19109. '$mod.A = new ExtA.Create();',
  19110. '$mod.A = new ExtA.Create();',
  19111. '$mod.A = new ExtA.Create(3,2);',
  19112. '$mod.C = new ExtA.ExtB.ExtC();',
  19113. '$mod.C = new ExtA.ExtB.ExtC();',
  19114. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19115. '']));
  19116. end;
  19117. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19118. begin
  19119. StartProgram(false);
  19120. Add([
  19121. '{$modeswitch externalclass}',
  19122. 'type',
  19123. ' TExtA = class external name ''ExtA''',
  19124. ' constructor Create; external name ''{}'';',
  19125. ' end;',
  19126. 'var',
  19127. ' A: texta;',
  19128. 'begin',
  19129. ' a:=texta.create;',
  19130. ' a:=texta(texta.create);',
  19131. ' a:=texta.create();',
  19132. ' with texta do begin',
  19133. ' a:=create;',
  19134. ' a:=create();',
  19135. ' end;',
  19136. ' a:=test1.texta.create;',
  19137. ' a:=test1.texta.create();',
  19138. '']);
  19139. ConvertProgram;
  19140. CheckSource('TestExternalClass_ConstructorBrackets',
  19141. LinesToStr([ // statements
  19142. 'this.A = null;',
  19143. '']),
  19144. LinesToStr([ // $mod.$main
  19145. '$mod.A = {};',
  19146. '$mod.A = {};',
  19147. '$mod.A = {};',
  19148. '$mod.A = {};',
  19149. '$mod.A = {};',
  19150. '$mod.A = {};',
  19151. '$mod.A = {};',
  19152. '']));
  19153. end;
  19154. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19155. begin
  19156. StartProgram(false);
  19157. Add('{$modeswitch externalclass}');
  19158. Add('type');
  19159. Add(' TExtA = class external name ''ExtA''');
  19160. Add(' constructor New;');
  19161. Add(' end;');
  19162. Add('function DoIt: longint;');
  19163. Add('const ExtA: longint = 3;');
  19164. Add('begin');
  19165. Add(' Result:=ExtA;');
  19166. Add('end;');
  19167. Add('var');
  19168. Add(' A: texta;');
  19169. Add('begin');
  19170. Add(' a:=texta.new;');
  19171. ConvertProgram;
  19172. CheckSource('TestExternalClass_LocalConstSameName',
  19173. LinesToStr([ // statements
  19174. 'var ExtA$1 = 3;',
  19175. 'this.DoIt = function () {',
  19176. ' var Result = 0;',
  19177. ' Result = ExtA$1;',
  19178. ' return Result;',
  19179. '};',
  19180. 'this.A = null;',
  19181. '']),
  19182. LinesToStr([ // $mod.$main
  19183. '$mod.A = new ExtA();',
  19184. '']));
  19185. end;
  19186. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19187. begin
  19188. StartProgram(false);
  19189. Add('{$modeswitch externalclass}');
  19190. Add('type');
  19191. Add(' TExtA = class external name ''ExtA''');
  19192. Add(' procedure DoIt;');
  19193. Add(' end;');
  19194. Add(' TMyA = class(TExtA)');
  19195. Add(' procedure DoIt;');
  19196. Add(' end;');
  19197. Add('procedure TMyA.DoIt; begin end;');
  19198. Add('begin');
  19199. ConvertProgram;
  19200. CheckSource('TestExternalClass_ReintroduceOverload',
  19201. LinesToStr([ // statements
  19202. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19203. ' this.$init = function () {',
  19204. ' };',
  19205. ' this.$final = function () {',
  19206. ' };',
  19207. ' this.DoIt$1 = function () {',
  19208. ' };',
  19209. '});',
  19210. '']),
  19211. LinesToStr([ // $mod.$main
  19212. '']));
  19213. end;
  19214. procedure TTestModule.TestExternalClass_Inherited;
  19215. begin
  19216. StartProgram(false);
  19217. Add('{$modeswitch externalclass}');
  19218. Add('type');
  19219. Add(' TExtA = class external name ''ExtA''');
  19220. Add(' procedure DoIt(i: longint = 1); virtual;');
  19221. Add(' procedure DoSome(j: longint = 2);');
  19222. Add(' end;');
  19223. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19224. Add(' end;');
  19225. Add(' TMyC = class(TExtB)');
  19226. Add(' procedure DoIt(i: longint = 1); override;');
  19227. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19228. Add(' end;');
  19229. Add('procedure TMyC.DoIt(i: longint);');
  19230. Add('begin');
  19231. Add(' inherited;');
  19232. Add(' inherited DoIt;');
  19233. Add(' inherited DoIt();');
  19234. Add(' inherited DoIt(3);');
  19235. Add(' inherited DoSome;');
  19236. Add(' inherited DoSome();');
  19237. Add(' inherited DoSome(4);');
  19238. Add('end;');
  19239. Add('procedure TMyC.DoSome(j: longint);');
  19240. Add('begin');
  19241. Add(' inherited;');
  19242. Add('end;');
  19243. Add('begin');
  19244. ConvertProgram;
  19245. CheckSource('TestExternalClass_ReintroduceOverload',
  19246. LinesToStr([ // statements
  19247. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19248. ' this.$init = function () {',
  19249. ' };',
  19250. ' this.$final = function () {',
  19251. ' };',
  19252. ' this.DoIt = function (i) {',
  19253. ' ExtB.DoIt.apply(this, arguments);',
  19254. ' ExtB.DoIt.call(this, 1);',
  19255. ' ExtB.DoIt.call(this, 1);',
  19256. ' ExtB.DoIt.call(this, 3);',
  19257. ' ExtB.DoSome.call(this, 2);',
  19258. ' ExtB.DoSome.call(this, 2);',
  19259. ' ExtB.DoSome.call(this, 4);',
  19260. ' };',
  19261. ' this.DoSome$1 = function (j) {',
  19262. ' ExtB.DoSome.apply(this, arguments);',
  19263. ' };',
  19264. '});',
  19265. '']),
  19266. LinesToStr([ // $mod.$main
  19267. '']));
  19268. end;
  19269. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19270. begin
  19271. StartProgram(false);
  19272. Add('{$modeswitch externalclass}');
  19273. Add('type');
  19274. Add(' TObject = class');
  19275. Add(' end;');
  19276. Add(' TExtA = class external name ''ExtA''(TObject)');
  19277. Add(' end;');
  19278. Add('begin');
  19279. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19280. ConvertProgram;
  19281. end;
  19282. procedure TTestModule.TestExternalClass_NewInstance;
  19283. begin
  19284. StartProgram(false);
  19285. Add('{$modeswitch externalclass}');
  19286. Add('type');
  19287. Add(' TExtA = class external name ''ExtA''');
  19288. Add(' end;');
  19289. Add(' TMyB = class(TExtA)');
  19290. Add(' protected');
  19291. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19292. Add(' end;');
  19293. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19294. Add('begin end;');
  19295. Add('begin');
  19296. ConvertProgram;
  19297. CheckSource('TestExternalClass_NewInstance',
  19298. LinesToStr([ // statements
  19299. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19300. ' this.$init = function () {',
  19301. ' };',
  19302. ' this.$final = function () {',
  19303. ' };',
  19304. ' this.NewInstance = function (fnname, paramarray) {',
  19305. ' var Result = null;',
  19306. ' return Result;',
  19307. ' };',
  19308. '});',
  19309. '']),
  19310. LinesToStr([ // $mod.$main
  19311. '']));
  19312. end;
  19313. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19314. begin
  19315. StartProgram(false);
  19316. Add('{$modeswitch externalclass}');
  19317. Add('type');
  19318. Add(' TExtA = class external name ''ExtA''');
  19319. Add(' end;');
  19320. Add(' TMyB = class(TExtA)');
  19321. Add(' protected');
  19322. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19323. Add(' end;');
  19324. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19325. Add('begin end;');
  19326. Add('begin');
  19327. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19328. ConvertProgram;
  19329. end;
  19330. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19331. begin
  19332. StartProgram(false);
  19333. Add('{$modeswitch externalclass}');
  19334. Add('type');
  19335. Add(' TExtA = class external name ''ExtA''');
  19336. Add(' end;');
  19337. Add(' TMyB = class(TExtA)');
  19338. Add(' protected');
  19339. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19340. Add(' end;');
  19341. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19342. Add('begin end;');
  19343. Add('begin');
  19344. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19345. nIncompatibleTypeArgNo);
  19346. ConvertProgram;
  19347. end;
  19348. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19349. begin
  19350. StartProgram(false);
  19351. Add('{$modeswitch externalclass}');
  19352. Add('type');
  19353. Add(' TExtA = class external name ''ExtA''');
  19354. Add(' end;');
  19355. Add(' TMyB = class(TExtA)');
  19356. Add(' protected');
  19357. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19358. Add(' end;');
  19359. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19360. Add('begin end;');
  19361. Add('begin');
  19362. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19363. nIncompatibleTypeArgNo);
  19364. ConvertProgram;
  19365. end;
  19366. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19367. begin
  19368. StartProgram(false);
  19369. Add([
  19370. '{$modeswitch externalclass}',
  19371. 'type',
  19372. ' TJSFunction = class external name ''Function''',
  19373. ' end;',
  19374. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19375. ' constructor New(w: word);',
  19376. ' end;',
  19377. ' TBird = class (TExtA)',
  19378. ' public',
  19379. ' Size: word;',
  19380. ' class var Legs: word;',
  19381. ' constructor Create(a: word);',
  19382. ' end;',
  19383. ' TEagle = class (TBird)',
  19384. ' public',
  19385. ' constructor Create(b: word); reintroduce;',
  19386. ' end;',
  19387. 'constructor TBird.Create(a: word);',
  19388. 'begin',
  19389. ' inherited;', // silently ignored
  19390. ' inherited New(a);', // this.$func(a)
  19391. 'end;',
  19392. 'constructor TEagle.Create(b: word);',
  19393. 'begin',
  19394. ' inherited Create(b);',
  19395. 'end;',
  19396. 'var',
  19397. ' Bird: TBird;',
  19398. ' Eagle: TEagle;',
  19399. 'begin',
  19400. ' Bird:=TBird.Create(3);',
  19401. ' Eagle:=TEagle.Create(4);',
  19402. ' Bird.Size:=Bird.Size+5;',
  19403. ' Bird.Legs:=Bird.Legs+6;',
  19404. ' Eagle.Size:=Eagle.Size+5;',
  19405. ' Eagle.Legs:=Eagle.Legs+6;',
  19406. '']);
  19407. ConvertProgram;
  19408. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19409. LinesToStr([ // statements
  19410. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19411. ' this.Legs = 0;',
  19412. ' this.$init = function () {',
  19413. ' this.Size = 0;',
  19414. ' };',
  19415. ' this.$final = function () {',
  19416. ' };',
  19417. ' this.Create = function (a) {',
  19418. ' this.$ancestorfunc(a);',
  19419. ' return this;',
  19420. ' };',
  19421. '});',
  19422. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19423. ' this.Create$1 = function (b) {',
  19424. ' $mod.TBird.Create.call(this, b);',
  19425. ' return this;',
  19426. ' };',
  19427. '});',
  19428. 'this.Bird = null;',
  19429. 'this.Eagle = null;',
  19430. '']),
  19431. LinesToStr([ // $mod.$main
  19432. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19433. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19434. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19435. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19436. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19437. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19438. '']));
  19439. end;
  19440. procedure TTestModule.TestExternalClass_PascalProperty;
  19441. begin
  19442. StartProgram(false);
  19443. Add('{$modeswitch externalclass}');
  19444. Add('type');
  19445. Add(' TJSElement = class;');
  19446. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19447. Add(' TJSElement = class external name ''ExtA''');
  19448. Add(' end;');
  19449. Add(' TControl = class(TJSElement)');
  19450. Add(' private');
  19451. Add(' FOnClick: TJSNotifyEvent;');
  19452. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19453. Add(' procedure Click(Sender: TJSElement);');
  19454. Add(' end;');
  19455. Add('procedure TControl.Click(Sender: TJSElement);');
  19456. Add('begin');
  19457. Add(' OnClick(Self);');
  19458. Add('end;');
  19459. Add('var');
  19460. Add(' Ctrl: TControl;');
  19461. Add('begin');
  19462. Add(' Ctrl.OnClick:[email protected];');
  19463. Add(' Ctrl.OnClick(Ctrl);');
  19464. ConvertProgram;
  19465. CheckSource('TestExternalClass_PascalProperty',
  19466. LinesToStr([ // statements
  19467. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19468. ' this.$init = function () {',
  19469. ' this.FOnClick = null;',
  19470. ' };',
  19471. ' this.$final = function () {',
  19472. ' this.FOnClick = undefined;',
  19473. ' };',
  19474. ' this.Click = function (Sender) {',
  19475. ' this.FOnClick(this);',
  19476. ' };',
  19477. '});',
  19478. 'this.Ctrl = null;',
  19479. '']),
  19480. LinesToStr([ // $mod.$main
  19481. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19482. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19483. '']));
  19484. end;
  19485. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19486. begin
  19487. StartProgram(false);
  19488. Add([
  19489. '{$modeswitch externalclass}',
  19490. 'type',
  19491. ' IUnknown = interface end;',
  19492. ' TObject = class',
  19493. ' end;',
  19494. ' TChild = class',
  19495. ' end;',
  19496. ' TExtRootA = class external name ''ExtRootA''',
  19497. ' end;',
  19498. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19499. ' end;',
  19500. ' TExtRootB = class external name ''ExtRootB''',
  19501. ' end;',
  19502. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19503. ' end;',
  19504. ' TExtString = class external name ''String''',
  19505. ' function charAt(aIndex : NativeInt) : string;',
  19506. ' end;',
  19507. 'var',
  19508. ' Obj: TObject;',
  19509. ' Child: TChild;',
  19510. ' RootA: TExtRootA;',
  19511. ' ChildA: TExtChildA;',
  19512. ' RootB: TExtRootB;',
  19513. ' ChildB: TExtChildB;',
  19514. ' i: IUnknown;',
  19515. ' s: string;',
  19516. ' v: jsvalue;',
  19517. 'begin',
  19518. ' obj:=tobject(roota);',
  19519. ' obj:=tobject(childa);',
  19520. ' child:=tchild(tobject(roota));',
  19521. ' roota:=textroota(obj);',
  19522. ' roota:=textroota(child);',
  19523. ' roota:=textroota(rootb);',
  19524. ' roota:=textroota(childb);',
  19525. ' childa:=textchilda(textroota(obj));',
  19526. ' roota:=TExtRootA(i);',
  19527. ' s:=TExtString(s).charAt(7);',
  19528. ' s:=TExtString(v).charAt(8);',
  19529. '']);
  19530. ConvertProgram;
  19531. CheckSource('TestExternalClass_TypeCastToRootClass',
  19532. LinesToStr([ // statements
  19533. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19534. 'rtl.createClass(this, "TObject", null, function () {',
  19535. ' this.$init = function () {',
  19536. ' };',
  19537. ' this.$final = function () {',
  19538. ' };',
  19539. '});',
  19540. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19541. '});',
  19542. 'this.Obj = null;',
  19543. 'this.Child = null;',
  19544. 'this.RootA = null;',
  19545. 'this.ChildA = null;',
  19546. 'this.RootB = null;',
  19547. 'this.ChildB = null;',
  19548. 'this.i = null;',
  19549. 'this.s = "";',
  19550. 'this.v = undefined;',
  19551. '']),
  19552. LinesToStr([ // $mod.$main
  19553. '$mod.Obj = $mod.RootA;',
  19554. '$mod.Obj = $mod.ChildA;',
  19555. '$mod.Child = $mod.RootA;',
  19556. '$mod.RootA = $mod.Obj;',
  19557. '$mod.RootA = $mod.Child;',
  19558. '$mod.RootA = $mod.RootB;',
  19559. '$mod.RootA = $mod.ChildB;',
  19560. '$mod.ChildA = $mod.Obj;',
  19561. '$mod.RootA = $mod.i;',
  19562. '$mod.s = $mod.s.charAt(7);',
  19563. '$mod.s = $mod.v.charAt(8);',
  19564. '']));
  19565. end;
  19566. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19567. begin
  19568. StartProgram(false);
  19569. Add([
  19570. '{$modeswitch externalclass}',
  19571. 'type',
  19572. ' IUnknown = interface end;',
  19573. ' IBird = interface(IUnknown) end;',
  19574. ' TClass = class of TObject;',
  19575. ' TObject = class',
  19576. ' end;',
  19577. ' TChild = class',
  19578. ' end;',
  19579. ' TJSObject = class external name ''Object''',
  19580. ' end;',
  19581. ' TRec = record end;',
  19582. 'var',
  19583. ' Obj: TObject;',
  19584. ' Child: TChild;',
  19585. ' i: IUnknown;',
  19586. ' Bird: IBird;',
  19587. ' j: TJSObject;',
  19588. ' r: TRec;',
  19589. ' c: TClass;',
  19590. 'begin',
  19591. ' j:=tjsobject(IUnknown);',
  19592. ' j:=tjsobject(IBird);',
  19593. ' j:=tjsobject(TObject);',
  19594. ' j:=tjsobject(TChild);',
  19595. ' j:=tjsobject(TRec);',
  19596. ' j:=tjsobject(Obj);',
  19597. ' j:=tjsobject(Child);',
  19598. ' j:=tjsobject(i);',
  19599. ' j:=tjsobject(Bird);',
  19600. ' j:=tjsobject(r);',
  19601. ' j:=tjsobject(c);',
  19602. '']);
  19603. ConvertProgram;
  19604. CheckSource('TestExternalClass_TypeCastToJSObject',
  19605. LinesToStr([ // statements
  19606. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19607. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19608. 'rtl.createClass(this, "TObject", null, function () {',
  19609. ' this.$init = function () {',
  19610. ' };',
  19611. ' this.$final = function () {',
  19612. ' };',
  19613. '});',
  19614. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19615. '});',
  19616. 'rtl.recNewT(this, "TRec", function () {',
  19617. ' this.$eq = function (b) {',
  19618. ' return true;',
  19619. ' };',
  19620. ' this.$assign = function (s) {',
  19621. ' return this;',
  19622. ' };',
  19623. '});',
  19624. 'this.Obj = null;',
  19625. 'this.Child = null;',
  19626. 'this.i = null;',
  19627. 'this.Bird = null;',
  19628. 'this.j = null;',
  19629. 'this.r = this.TRec.$new();',
  19630. 'this.c = null;',
  19631. '']),
  19632. LinesToStr([ // $mod.$main
  19633. '$mod.j = $mod.IUnknown;',
  19634. '$mod.j = $mod.IBird;',
  19635. '$mod.j = $mod.TObject;',
  19636. '$mod.j = $mod.TChild;',
  19637. '$mod.j = $mod.TRec;',
  19638. '$mod.j = $mod.Obj;',
  19639. '$mod.j = $mod.Child;',
  19640. '$mod.j = $mod.i;',
  19641. '$mod.j = $mod.Bird;',
  19642. '$mod.j = $mod.r;',
  19643. '$mod.j = $mod.c;',
  19644. '']));
  19645. end;
  19646. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19647. begin
  19648. StartProgram(false);
  19649. Add('{$modeswitch externalclass}');
  19650. Add('type');
  19651. Add(' TJSString = class external name ''String''');
  19652. Add(' class function fromCharCode() : string; varargs;');
  19653. Add(' function anchor(const aName : string) : string;');
  19654. Add(' end;');
  19655. Add('var');
  19656. Add(' s: string;');
  19657. Add('begin');
  19658. Add(' s:=TJSString.fromCharCode(65,66);');
  19659. Add(' s:=TJSString(s).anchor(s);');
  19660. Add(' s:=TJSString(''foo'').anchor(s);');
  19661. ConvertProgram;
  19662. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19663. LinesToStr([ // statements
  19664. 'this.s = "";',
  19665. '']),
  19666. LinesToStr([ // $mod.$main
  19667. '$mod.s = String.fromCharCode(65, 66);',
  19668. '$mod.s = $mod.s.anchor($mod.s);',
  19669. '$mod.s = "foo".anchor($mod.s);',
  19670. '']));
  19671. end;
  19672. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19673. begin
  19674. StartProgram(false);
  19675. Add([
  19676. '{$modeswitch externalclass}',
  19677. 'type',
  19678. ' TJSObject = class external name ''Object'' end;',
  19679. ' TJSFunction = class external name ''Function''',
  19680. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19681. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19682. ' end;',
  19683. ' TObject = class',
  19684. ' procedure DoIt(i: longint);',
  19685. ' end;',
  19686. ' TFuncInt = function(o: TObject): longint;',
  19687. 'function GetIt(o: TObject): longint;',
  19688. ' procedure Sub; begin end;',
  19689. 'var',
  19690. ' f: TJSFunction;',
  19691. ' fi: TFuncInt;',
  19692. 'begin',
  19693. ' fi:=TFuncInt(f);',
  19694. ' f:=TJSFunction(fi);',
  19695. ' f:=TJSFunction(@GetIt);',
  19696. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19697. ' f:=TJSFunction(@Sub);',
  19698. ' f:=TJSFunction(@o.doit);',
  19699. ' f:=TJSFunction(fi).bind(nil,4)',
  19700. 'end;',
  19701. 'procedure TObject.DoIt(i: longint);',
  19702. ' procedure Sub; begin end;',
  19703. 'var f: TJSFunction;',
  19704. 'begin',
  19705. ' f:=TJSFunction(@DoIt);',
  19706. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19707. ' f:=TJSFunction(@Sub);',
  19708. ' f:=TJSFunction(@GetIt);',
  19709. 'end;',
  19710. 'begin']);
  19711. ConvertProgram;
  19712. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19713. LinesToStr([ // statements
  19714. 'rtl.createClass(this, "TObject", null, function () {',
  19715. ' this.$init = function () {',
  19716. ' };',
  19717. ' this.$final = function () {',
  19718. ' };',
  19719. ' this.DoIt = function (i) {',
  19720. ' var $Self = this;',
  19721. ' function Sub() {',
  19722. ' };',
  19723. ' var f = null;',
  19724. ' f = this.DoIt;',
  19725. ' f = this.DoIt.bind(null, 13);',
  19726. ' f = Sub;',
  19727. ' f = $mod.GetIt;',
  19728. ' };',
  19729. '});',
  19730. 'this.GetIt = function (o) {',
  19731. ' var Result = 0;',
  19732. ' function Sub() {',
  19733. ' };',
  19734. ' var f = null;',
  19735. ' var fi = null;',
  19736. ' fi = f;',
  19737. ' f = fi;',
  19738. ' f = $mod.GetIt;',
  19739. ' f = $mod.GetIt.bind(null, 3);',
  19740. ' f = Sub;',
  19741. ' f = $mod.TObject.DoIt;',
  19742. ' f = fi.bind(null, 4);',
  19743. ' return Result;',
  19744. '};',
  19745. '']),
  19746. LinesToStr([ // $mod.$main
  19747. '']));
  19748. end;
  19749. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19750. begin
  19751. StartProgram(false);
  19752. Add([
  19753. '{$mode delphi}',
  19754. '{$modeswitch externalclass}',
  19755. 'type',
  19756. ' TJSObject = class external name ''Object'' end;',
  19757. ' TJSWindow = class external name ''Window''(TJSObject)',
  19758. ' procedure Open;',
  19759. ' end;',
  19760. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19761. ' procedure Execute;',
  19762. ' end;',
  19763. 'procedure Fly;',
  19764. 'var',
  19765. ' w: TJSWindow;',
  19766. ' e: TJSEventTarget;',
  19767. 'begin',
  19768. ' w:=TJSWindow(e);',
  19769. ' e:=TJSEventTarget(w);',
  19770. 'end;',
  19771. 'begin']);
  19772. ConvertProgram;
  19773. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19774. LinesToStr([ // statements
  19775. 'this.Fly = function () {',
  19776. ' var w = null;',
  19777. ' var e = null;',
  19778. ' w = e;',
  19779. ' e = w;',
  19780. '};',
  19781. '']),
  19782. LinesToStr([ // $mod.$main
  19783. '']));
  19784. end;
  19785. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19786. begin
  19787. StartProgram(false);
  19788. Add('{$modeswitch externalclass}');
  19789. Add('type');
  19790. Add(' TJSString = class external name ''String''');
  19791. Add(' class function fromCharCode() : string; varargs;');
  19792. Add(' end;');
  19793. Add('var');
  19794. Add(' s: string;');
  19795. Add(' sObj: TJSString;');
  19796. Add('begin');
  19797. Add(' s:=sObj.fromCharCode(65,66);');
  19798. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19799. nExternalClassInstanceCannotAccessStaticX);
  19800. ConvertProgram;
  19801. end;
  19802. procedure TTestModule.TestExternalClass_BracketAccessor;
  19803. begin
  19804. StartProgram(false);
  19805. Add([
  19806. '{$modeswitch externalclass}',
  19807. 'type',
  19808. ' TJSArray = class external name ''Array2''',
  19809. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19810. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19811. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19812. ' end;',
  19813. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19814. 'begin end;',
  19815. 'var',
  19816. ' Arr: tjsarray;',
  19817. ' s: string;',
  19818. ' i: longint;',
  19819. ' v: jsvalue;',
  19820. 'begin',
  19821. ' v:=arr[0];',
  19822. ' v:=arr.items[1];',
  19823. ' arr[2]:=s;',
  19824. ' arr.items[3]:=s;',
  19825. ' arr[4]:=i;',
  19826. ' arr[5]:=arr[6];',
  19827. ' arr.items[7]:=arr.items[8];',
  19828. ' with arr do items[9]:=items[10];',
  19829. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19830. ' with arr do begin',
  19831. ' v:=GetItems(14);',
  19832. ' setitems(15,16);',
  19833. ' end;',
  19834. ' v:=test1.arr.items[17];',
  19835. ' test1.arr.items[18]:=v;',
  19836. '']);
  19837. ConvertProgram;
  19838. CheckSource('TestExternalClass_BracketAccessor',
  19839. LinesToStr([ // statements
  19840. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19841. '};',
  19842. 'this.Arr = null;',
  19843. 'this.s = "";',
  19844. 'this.i = 0;',
  19845. 'this.v = undefined;',
  19846. '']),
  19847. LinesToStr([ // $mod.$main
  19848. '$mod.v = $mod.Arr[0];',
  19849. '$mod.v = $mod.Arr[1];',
  19850. '$mod.Arr[2] = $mod.s;',
  19851. '$mod.Arr[3] = $mod.s;',
  19852. '$mod.Arr[4] = $mod.i;',
  19853. '$mod.Arr[5] = $mod.Arr[6];',
  19854. '$mod.Arr[7] = $mod.Arr[8];',
  19855. 'var $with = $mod.Arr;',
  19856. '$with[9] = $with[10];',
  19857. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19858. ' a: 9,',
  19859. ' p: $mod.Arr,',
  19860. ' get: function () {',
  19861. ' return this.p[this.a];',
  19862. ' },',
  19863. ' set: function (v) {',
  19864. ' this.p[this.a] = v;',
  19865. ' }',
  19866. '}, {',
  19867. ' a: 10,',
  19868. ' p: $mod.Arr,',
  19869. ' get: function () {',
  19870. ' return this.p[this.a];',
  19871. ' },',
  19872. ' set: function (v) {',
  19873. ' this.p[this.a] = v;',
  19874. ' }',
  19875. '});',
  19876. 'var $with1 = $mod.Arr;',
  19877. '$mod.v = $with1[14];',
  19878. '$with1[15] = 16;',
  19879. '$mod.v = $mod.Arr[17];',
  19880. '$mod.Arr[18] = $mod.v;',
  19881. '']));
  19882. end;
  19883. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19884. begin
  19885. StartProgram(false);
  19886. Add([
  19887. '{$modeswitch externalclass}',
  19888. 'type',
  19889. ' TJSArray = class external name ''Array2''',
  19890. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19891. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19892. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19893. ' end;',
  19894. ' TMyArr = class(TJSArray)',
  19895. ' procedure DoIt;',
  19896. ' end;',
  19897. 'procedure tmyarr.DoIt;',
  19898. 'begin',
  19899. ' Items[1]:=Items[2];',
  19900. ' SetItems(3,getItems(4));',
  19901. 'end;',
  19902. 'var',
  19903. ' Arr: tmyarr;',
  19904. ' s: string;',
  19905. ' i: longint;',
  19906. ' v: jsvalue;',
  19907. 'begin',
  19908. ' v:=arr[0];',
  19909. ' v:=arr.items[1];',
  19910. ' arr[2]:=s;',
  19911. ' arr.items[3]:=s;',
  19912. ' arr[4]:=i;',
  19913. ' arr[5]:=arr[6];',
  19914. ' arr.items[7]:=arr.items[8];',
  19915. ' with arr do items[9]:=items[10];',
  19916. ' with arr do begin',
  19917. ' v:=GetItems(14);',
  19918. ' setitems(15,16);',
  19919. ' end;',
  19920. '']);
  19921. ConvertProgram;
  19922. CheckSource('TestExternalClass_BracketAccessor_Call',
  19923. LinesToStr([ // statements
  19924. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19925. ' this.$init = function () {',
  19926. ' };',
  19927. ' this.$final = function () {',
  19928. ' };',
  19929. ' this.DoIt = function () {',
  19930. ' this[1] = this[2];',
  19931. ' this[3] = this[4];',
  19932. ' };',
  19933. '});',
  19934. 'this.Arr = null;',
  19935. 'this.s = "";',
  19936. 'this.i = 0;',
  19937. 'this.v = undefined;',
  19938. '']),
  19939. LinesToStr([ // $mod.$main
  19940. '$mod.v = $mod.Arr[0];',
  19941. '$mod.v = $mod.Arr[1];',
  19942. '$mod.Arr[2] = $mod.s;',
  19943. '$mod.Arr[3] = $mod.s;',
  19944. '$mod.Arr[4] = $mod.i;',
  19945. '$mod.Arr[5] = $mod.Arr[6];',
  19946. '$mod.Arr[7] = $mod.Arr[8];',
  19947. 'var $with = $mod.Arr;',
  19948. '$with[9] = $with[10];',
  19949. 'var $with1 = $mod.Arr;',
  19950. '$mod.v = $with1[14];',
  19951. '$with1[15] = 16;',
  19952. '']));
  19953. end;
  19954. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  19955. begin
  19956. StartProgram(false);
  19957. Add('{$modeswitch externalclass}');
  19958. Add('type');
  19959. Add(' TJSArray = class external name ''Array2''');
  19960. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  19961. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  19962. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  19963. Add(' end;');
  19964. Add('begin');
  19965. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  19966. nBracketAccessorOfExternalClassMustHaveOneParameter);
  19967. ConvertProgram;
  19968. end;
  19969. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  19970. begin
  19971. StartProgram(false);
  19972. Add('{$modeswitch externalclass}');
  19973. Add('type');
  19974. Add(' TJSArray = class external name ''Array2''');
  19975. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19976. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  19977. Add(' end;');
  19978. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  19979. Add('begin end;');
  19980. Add('var');
  19981. Add(' Arr: tjsarray;');
  19982. Add(' v: jsvalue;');
  19983. Add('begin');
  19984. Add(' v:=arr[0];');
  19985. Add(' v:=arr.items[1];');
  19986. Add(' with arr do v:=items[2];');
  19987. Add(' doit(arr[3],arr[4]);');
  19988. ConvertProgram;
  19989. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  19990. LinesToStr([ // statements
  19991. 'this.DoIt = function (vI, vJ) {',
  19992. '};',
  19993. 'this.Arr = null;',
  19994. 'this.v = undefined;',
  19995. '']),
  19996. LinesToStr([ // $mod.$main
  19997. '$mod.v = $mod.Arr[0];',
  19998. '$mod.v = $mod.Arr[1];',
  19999. 'var $with = $mod.Arr;',
  20000. '$mod.v = $with[2];',
  20001. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20002. '']));
  20003. end;
  20004. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20005. begin
  20006. StartProgram(false);
  20007. Add('{$modeswitch externalclass}');
  20008. Add('type');
  20009. Add(' TJSArray = class external name ''Array2''');
  20010. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20011. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20012. Add(' end;');
  20013. Add('var');
  20014. Add(' Arr: tjsarray;');
  20015. Add(' s: string;');
  20016. Add(' i: longint;');
  20017. Add(' v: jsvalue;');
  20018. Add('begin');
  20019. Add(' arr[2]:=s;');
  20020. Add(' arr.items[3]:=s;');
  20021. Add(' arr[4]:=i;');
  20022. Add(' with arr do items[5]:=i;');
  20023. ConvertProgram;
  20024. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20025. LinesToStr([ // statements
  20026. 'this.Arr = null;',
  20027. 'this.s = "";',
  20028. 'this.i = 0;',
  20029. 'this.v = undefined;',
  20030. '']),
  20031. LinesToStr([ // $mod.$main
  20032. '$mod.Arr[2] = $mod.s;',
  20033. '$mod.Arr[3] = $mod.s;',
  20034. '$mod.Arr[4] = $mod.i;',
  20035. 'var $with = $mod.Arr;',
  20036. '$with[5] = $mod.i;',
  20037. '']));
  20038. end;
  20039. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20040. begin
  20041. StartProgram(false);
  20042. Add('{$modeswitch externalclass}');
  20043. Add('type');
  20044. Add(' TJSArray = class external name ''Array2''');
  20045. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20046. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20047. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20048. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20049. Add(' end;');
  20050. Add('var');
  20051. Add(' Arr: tjsarray;');
  20052. Add(' s: string;');
  20053. Add(' i: longint;');
  20054. Add(' v: jsvalue;');
  20055. Add('begin');
  20056. Add(' arr[2]:=s;');
  20057. Add(' arr.items[3]:=s;');
  20058. Add(' arr.numbers[4]:=i;');
  20059. Add(' with arr do items[5]:=i;');
  20060. Add(' with arr do numbers[6]:=i;');
  20061. ConvertProgram;
  20062. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20063. LinesToStr([ // statements
  20064. 'this.Arr = null;',
  20065. 'this.s = "";',
  20066. 'this.i = 0;',
  20067. 'this.v = undefined;',
  20068. '']),
  20069. LinesToStr([ // $mod.$main
  20070. '$mod.Arr[2] = $mod.s;',
  20071. '$mod.Arr[3] = $mod.s;',
  20072. '$mod.Arr[4] = $mod.i;',
  20073. 'var $with = $mod.Arr;',
  20074. '$with[5] = $mod.i;',
  20075. 'var $with1 = $mod.Arr;',
  20076. '$with1[6] = $mod.i;',
  20077. '']));
  20078. end;
  20079. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20080. begin
  20081. StartProgram(false);
  20082. Add('{$modeswitch externalclass}');
  20083. Add('type');
  20084. Add(' TJSArray = class external name ''Array2''');
  20085. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20086. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20087. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20088. Add(' end;');
  20089. Add('var');
  20090. Add(' Arr: tjsarray;');
  20091. Add(' i: longint;');
  20092. Add(' IntArr: array of longint;');
  20093. Add(' v: jsvalue;');
  20094. Add('begin');
  20095. Add(' v:=arr.items[i];');
  20096. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20097. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20098. ConvertProgram;
  20099. CheckSource('TestExternalClass_BracketAccessor_Index',
  20100. LinesToStr([ // statements
  20101. 'this.Arr = null;',
  20102. 'this.i = 0;',
  20103. 'this.IntArr = [];',
  20104. 'this.v = undefined;',
  20105. '']),
  20106. LinesToStr([ // $mod.$main
  20107. '$mod.v = $mod.Arr[$mod.i];',
  20108. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20109. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20110. '']));
  20111. end;
  20112. procedure TTestModule.TestExternalClass_ForInJSObject;
  20113. begin
  20114. StartProgram(false);
  20115. Add([
  20116. '{$modeswitch externalclass}',
  20117. 'type',
  20118. ' TJSObject = class external name ''Object''',
  20119. ' end;',
  20120. 'var',
  20121. ' o: TJSObject;',
  20122. ' key: string;',
  20123. 'begin',
  20124. ' for key in o do',
  20125. ' if key=''abc'' then ;',
  20126. '']);
  20127. ConvertProgram;
  20128. CheckSource('TestExternalClass_ForInJSObject',
  20129. LinesToStr([ // statements
  20130. 'this.o = null;',
  20131. 'this.key = "";',
  20132. '']),
  20133. LinesToStr([ // $mod.$main
  20134. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20135. '']));
  20136. end;
  20137. procedure TTestModule.TestExternalClass_ForInJSArray;
  20138. begin
  20139. StartProgram(false);
  20140. Add([
  20141. '{$modeswitch externalclass}',
  20142. 'type',
  20143. ' TJSInt8Array = class external name ''Int8Array''',
  20144. ' private',
  20145. ' flength: NativeInt external name ''length'';',
  20146. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20147. ' public',
  20148. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20149. ' property Length: NativeInt read flength;',
  20150. ' end;',
  20151. 'var',
  20152. ' a: TJSInt8Array;',
  20153. ' value: shortint;',
  20154. 'begin',
  20155. ' for value in a do',
  20156. ' if value=3 then ;',
  20157. '']);
  20158. ConvertProgram;
  20159. CheckSource('TestExternalClass_ForInJSArray',
  20160. LinesToStr([ // statements
  20161. 'this.a = null;',
  20162. 'this.value = 0;',
  20163. '']),
  20164. LinesToStr([ // $mod.$main
  20165. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20166. ' $mod.value = $in[$l];',
  20167. ' if ($mod.value === 3) ;',
  20168. '};',
  20169. '']));
  20170. end;
  20171. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20172. begin
  20173. AddModuleWithIntfImplSrc('unit2.pas',
  20174. LinesToStr([
  20175. '{$modeswitch externalclass}',
  20176. 'type',
  20177. ' TJSBufferSource = class external name ''BufferSource''',
  20178. ' end;',
  20179. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20180. '']),
  20181. '');
  20182. AddModuleWithIntfImplSrc('unit3.pas',
  20183. LinesToStr([
  20184. '{$modeswitch externalclass}',
  20185. 'type',
  20186. ' TJSBufferSource = class external name ''BufferSource''',
  20187. ' end;',
  20188. '']),
  20189. '');
  20190. StartUnit(true);
  20191. Add([
  20192. 'interface',
  20193. 'uses unit2, unit3;',
  20194. 'procedure DoSome(s: TJSBufferSource);',
  20195. 'implementation',
  20196. 'procedure DoSome(s: TJSBufferSource);',
  20197. 'begin',
  20198. ' DoIt(s);',
  20199. 'end;',
  20200. '']);
  20201. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20202. nIncompatibleTypeArgNo);
  20203. ConvertUnit;
  20204. end;
  20205. procedure TTestModule.TestExternalClass_NestedConstructor;
  20206. begin
  20207. StartProgram(false);
  20208. Add([
  20209. '{$modeswitch externalclass}',
  20210. 'type',
  20211. ' TJSObject = class external name ''Object''',
  20212. ' type TBird = class external name ''Bird''',
  20213. ' type TWing = class external name ''Wing''',
  20214. ' constructor New;',
  20215. ' constructor Create(w: word = 3);',
  20216. ' end;',
  20217. ' end;',
  20218. ' end;',
  20219. 'var',
  20220. ' w: TJSObject.TBird.TWing;',
  20221. 'begin',
  20222. ' w:=tjsobject.tbird.twing.new;',
  20223. ' w:=tjsobject.tbird.twing.new();',
  20224. ' w:=tjsobject.tbird.twing.create;',
  20225. ' w:=tjsobject.tbird.twing.create(4);',
  20226. ' with tjsobject do begin',
  20227. ' w:=tbird.twing.new;',
  20228. ' w:=tbird.twing.new();',
  20229. ' w:=tbird.twing.create;',
  20230. ' w:=tbird.twing.create(11);',
  20231. ' end;',
  20232. ' with tjsobject.tbird do begin',
  20233. ' w:=twing.new;',
  20234. ' w:=twing.new();',
  20235. ' w:=twing.create;',
  20236. ' w:=twing.create(21);',
  20237. ' end;',
  20238. ' with tjsobject.tbird.twing do begin',
  20239. ' w:=new;',
  20240. ' w:=new();',
  20241. ' w:=create;',
  20242. ' w:=create(31);',
  20243. ' end;',
  20244. '']);
  20245. ConvertProgram;
  20246. CheckSource('TestExternalClass_NestedConstructor',
  20247. LinesToStr([ // statements
  20248. 'this.w = null;',
  20249. '']),
  20250. LinesToStr([ // $mod.$main
  20251. '$mod.w = new Object.Bird.Wing();',
  20252. '$mod.w = new Object.Bird.Wing();',
  20253. '$mod.w = new Object.Bird.Wing.Create();',
  20254. '$mod.w = new Object.Bird.Wing.Create(4);',
  20255. '$mod.w = new Object.Bird.Wing();',
  20256. '$mod.w = new Object.Bird.Wing();',
  20257. '$mod.w = new Object.Bird.Wing.Create();',
  20258. '$mod.w = new Object.Bird.Wing.Create(11);',
  20259. 'var $with = Object.Bird;',
  20260. '$mod.w = new Object.Bird.Wing();',
  20261. '$mod.w = new Object.Bird.Wing();',
  20262. '$mod.w = new Object.Bird.Wing.Create();',
  20263. '$mod.w = new Object.Bird.Wing.Create(21);',
  20264. 'var $with1 = Object.Bird.Wing;',
  20265. '$mod.w = new $with1();',
  20266. '$mod.w = new $with1();',
  20267. '$mod.w = new Object.Bird.Wing.Create();',
  20268. '$mod.w = new Object.Bird.Wing.Create(31);',
  20269. '']));
  20270. end;
  20271. procedure TTestModule.TestClassInterface_Corba;
  20272. begin
  20273. StartProgram(false);
  20274. Add([
  20275. '{$interfaces corba}',
  20276. 'type',
  20277. ' IUnknown = interface;',
  20278. ' IUnknown = interface',
  20279. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20280. ' end;',
  20281. ' IInterface = IUnknown;',
  20282. ' IBird = interface(IInterface)',
  20283. ' function GetSize: longint;',
  20284. ' procedure SetSize(i: longint);',
  20285. ' property Size: longint read GetSize write SetSize;',
  20286. ' procedure DoIt(i: longint);',
  20287. ' end;',
  20288. ' TObject = class',
  20289. ' end;',
  20290. ' TBird = class(TObject,IBird)',
  20291. ' function GetSize: longint; virtual; abstract;',
  20292. ' procedure SetSize(i: longint); virtual; abstract;',
  20293. ' procedure DoIt(i: longint); virtual; abstract;',
  20294. ' end;',
  20295. 'var',
  20296. ' BirdIntf: IBird;',
  20297. 'begin',
  20298. ' BirdIntf.Size:=BirdIntf.Size;',
  20299. '']);
  20300. ConvertProgram;
  20301. CheckSource('TestClassInterface_Corba',
  20302. LinesToStr([ // statements
  20303. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20304. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20305. 'rtl.createClass(this, "TObject", null, function () {',
  20306. ' this.$init = function () {',
  20307. ' };',
  20308. ' this.$final = function () {',
  20309. ' };',
  20310. '});',
  20311. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20312. ' rtl.addIntf(this, $mod.IBird);',
  20313. '});',
  20314. 'this.BirdIntf = null;',
  20315. '']),
  20316. LinesToStr([ // $mod.$main
  20317. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20318. '']));
  20319. end;
  20320. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20321. begin
  20322. StartProgram(false);
  20323. Add([
  20324. '{$interfaces corba}',
  20325. 'type',
  20326. ' IUnknown = interface',
  20327. ' procedure DoIt; external name ''foo'';',
  20328. ' end;',
  20329. 'begin']);
  20330. SetExpectedParserError(
  20331. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20332. nParserNoFieldsAllowed);
  20333. ConvertProgram;
  20334. end;
  20335. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20336. begin
  20337. StartProgram(false);
  20338. Add([
  20339. '{$interfaces corba}',
  20340. 'type',
  20341. ' integer = longint;',
  20342. ' IUnknown = interface',
  20343. ' procedure DoIt(i: integer);',
  20344. ' procedure DoIt(s: string);',
  20345. ' end;',
  20346. ' IBird = interface(IUnknown)',
  20347. ' procedure DoIt(b: boolean); overload;',
  20348. ' end;',
  20349. ' TObject = class',
  20350. ' end;',
  20351. ' TBird = class(TObject,IBird)',
  20352. ' procedure DoIt(o: TObject);',
  20353. ' procedure DoIt(s: string);',
  20354. ' procedure DoIt(i: integer);',
  20355. ' procedure DoIt(b: boolean);',
  20356. ' end;',
  20357. 'procedure TBird.DoIt(o: TObject); begin end;',
  20358. 'procedure TBird.DoIt(s: string); begin end;',
  20359. 'procedure TBird.DoIt(i: integer); begin end;',
  20360. 'procedure TBird.DoIt(b: boolean); begin end;',
  20361. 'var',
  20362. ' BirdIntf: IBird;',
  20363. 'begin',
  20364. ' BirdIntf.DoIt(3);',
  20365. ' BirdIntf.DoIt(''abc'');',
  20366. ' BirdIntf.DoIt(true);',
  20367. '']);
  20368. ConvertProgram;
  20369. CheckSource('TestClassInterface_Overloads',
  20370. LinesToStr([ // statements
  20371. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20372. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20373. 'rtl.createClass(this, "TObject", null, function () {',
  20374. ' this.$init = function () {',
  20375. ' };',
  20376. ' this.$final = function () {',
  20377. ' };',
  20378. '});',
  20379. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20380. ' this.DoIt = function (o) {',
  20381. ' };',
  20382. ' this.DoIt$1 = function (s) {',
  20383. ' };',
  20384. ' this.DoIt$2 = function (i) {',
  20385. ' };',
  20386. ' this.DoIt$3 = function (b) {',
  20387. ' };',
  20388. ' rtl.addIntf(this, $mod.IBird, {',
  20389. ' DoIt$2: "DoIt$3",',
  20390. ' DoIt: "DoIt$2"',
  20391. ' });',
  20392. '});',
  20393. 'this.BirdIntf = null;',
  20394. '']),
  20395. LinesToStr([ // $mod.$main
  20396. '$mod.BirdIntf.DoIt(3);',
  20397. '$mod.BirdIntf.DoIt$1("abc");',
  20398. '$mod.BirdIntf.DoIt$2(true);',
  20399. '']));
  20400. end;
  20401. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20402. begin
  20403. StartProgram(false);
  20404. Add([
  20405. '{$interfaces corba}',
  20406. 'type',
  20407. ' IBird = interface',
  20408. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20409. ' end;',
  20410. ' IDog = interface',
  20411. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20412. ' end;',
  20413. ' TObject = class(IBird,IDog)',
  20414. ' end;',
  20415. 'begin']);
  20416. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20417. nDuplicateGUIDXInYZ);
  20418. ConvertProgram;
  20419. end;
  20420. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20421. begin
  20422. StartProgram(false);
  20423. Add([
  20424. '{$interfaces corba}',
  20425. 'type',
  20426. ' IAnimal = interface',
  20427. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20428. ' end;',
  20429. ' IBird = interface(IAnimal)',
  20430. ' end;',
  20431. ' IHawk = interface(IBird)',
  20432. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20433. ' end;',
  20434. 'begin']);
  20435. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20436. nDuplicateGUIDXInYZ);
  20437. ConvertProgram;
  20438. end;
  20439. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20440. begin
  20441. StartProgram(false);
  20442. Add([
  20443. '{$interfaces corba}',
  20444. 'type',
  20445. ' integer = longint;',
  20446. ' IUnknown = interface',
  20447. ' procedure DoIt(i: integer);',
  20448. ' end;',
  20449. ' IBird = interface',
  20450. ' procedure Fly(i: integer);',
  20451. ' end;',
  20452. ' TObject = class(IUnknown)',
  20453. ' procedure DoIt(i: integer);',
  20454. ' end;',
  20455. ' TBird = class(IBird)',
  20456. ' procedure Fly(i: integer);',
  20457. ' end;',
  20458. 'procedure TObject.DoIt(i: integer); begin end;',
  20459. 'procedure TBird.Fly(i: integer); begin end;',
  20460. 'begin',
  20461. '']);
  20462. ConvertProgram;
  20463. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20464. LinesToStr([ // statements
  20465. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20466. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20467. 'rtl.createClass(this, "TObject", null, function () {',
  20468. ' this.$init = function () {',
  20469. ' };',
  20470. ' this.$final = function () {',
  20471. ' };',
  20472. ' this.DoIt = function (i) {',
  20473. ' };',
  20474. ' rtl.addIntf(this, $mod.IUnknown);',
  20475. '});',
  20476. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20477. ' this.Fly = function (i) {',
  20478. ' };',
  20479. ' rtl.addIntf(this, $mod.IBird);',
  20480. ' rtl.addIntf(this, $mod.IUnknown);',
  20481. '});',
  20482. '']),
  20483. LinesToStr([ // $mod.$main
  20484. '']));
  20485. end;
  20486. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20487. begin
  20488. StartProgram(false);
  20489. Add([
  20490. '{$interfaces corba}',
  20491. 'type',
  20492. ' integer = longint;',
  20493. ' IBird = interface',
  20494. ' procedure DoIt(i: integer);',
  20495. ' end;',
  20496. ' TObject = class',
  20497. ' procedure DoIt(i: integer);',
  20498. ' end;',
  20499. ' TBird = class(IBird)',
  20500. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20501. ' end;',
  20502. 'procedure TObject.DoIt(i: integer); begin end;',
  20503. 'procedure TBird.DoIt(i: integer); begin end;',
  20504. 'begin',
  20505. '']);
  20506. ConvertProgram;
  20507. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20508. LinesToStr([ // statements
  20509. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20510. 'rtl.createClass(this, "TObject", null, function () {',
  20511. ' this.$init = function () {',
  20512. ' };',
  20513. ' this.$final = function () {',
  20514. ' };',
  20515. ' this.DoIt = function (i) {',
  20516. ' };',
  20517. '});',
  20518. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20519. ' this.DoIt$1 = function (i) {',
  20520. ' };',
  20521. ' rtl.addIntf(this, $mod.IBird, {',
  20522. ' DoIt: "DoIt$1"',
  20523. ' });',
  20524. '});',
  20525. '']),
  20526. LinesToStr([ // $mod.$main
  20527. '']));
  20528. end;
  20529. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20530. begin
  20531. StartProgram(false);
  20532. Add([
  20533. '{$interfaces corba}',
  20534. 'type',
  20535. ' IUnknown = interface',
  20536. ' procedure Walk(i: longint);',
  20537. ' end;',
  20538. ' IBird = interface(IUnknown)',
  20539. ' procedure Walk(b: boolean); overload;',
  20540. ' procedure Fly(s: string);',
  20541. ' end;',
  20542. ' TObject = class',
  20543. ' end;',
  20544. ' TBird = class(TObject,IBird)',
  20545. ' procedure IBird.Fly = Move;',
  20546. ' procedure IBird.Walk = Hop;',
  20547. ' procedure Hop(i: longint);',
  20548. ' procedure Move(s: string);',
  20549. ' procedure Hop(b: boolean);',
  20550. ' end;',
  20551. 'procedure TBird.Move(s: string); begin end;',
  20552. 'procedure TBird.Hop(i: longint); begin end;',
  20553. 'procedure TBird.Hop(b: boolean); begin end;',
  20554. 'var',
  20555. ' BirdIntf: IBird;',
  20556. 'begin',
  20557. ' BirdIntf.Walk(3);',
  20558. ' BirdIntf.Walk(true);',
  20559. ' BirdIntf.Fly(''abc'');',
  20560. '']);
  20561. ConvertProgram;
  20562. CheckSource('TestClassInterface_Corba_MethodResolution',
  20563. LinesToStr([ // statements
  20564. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20565. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20566. 'rtl.createClass(this, "TObject", null, function () {',
  20567. ' this.$init = function () {',
  20568. ' };',
  20569. ' this.$final = function () {',
  20570. ' };',
  20571. '});',
  20572. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20573. ' this.Hop = function (i) {',
  20574. ' };',
  20575. ' this.Move = function (s) {',
  20576. ' };',
  20577. ' this.Hop$1 = function (b) {',
  20578. ' };',
  20579. ' rtl.addIntf(this, $mod.IBird, {',
  20580. ' Walk$1: "Hop$1",',
  20581. ' Fly: "Move",',
  20582. ' Walk: "Hop"',
  20583. ' });',
  20584. '});',
  20585. 'this.BirdIntf = null;',
  20586. '']),
  20587. LinesToStr([ // $mod.$main
  20588. '$mod.BirdIntf.Walk(3);',
  20589. '$mod.BirdIntf.Walk$1(true);',
  20590. '$mod.BirdIntf.Fly("abc");',
  20591. '']));
  20592. end;
  20593. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20594. begin
  20595. StartProgram(false);
  20596. Add([
  20597. '{$interfaces com}',
  20598. 'type',
  20599. ' IUnknown = interface',
  20600. ' function _AddRef: longint;',
  20601. ' procedure Walk;',
  20602. ' end;',
  20603. ' IBird = interface end;',
  20604. ' IDog = interface end;',
  20605. ' TObject = class(IBird,IDog)',
  20606. ' function _AddRef: longint; virtual; abstract;',
  20607. ' procedure Walk; virtual; abstract;',
  20608. ' end;',
  20609. ' TBird = class(IUnknown)',
  20610. ' end;',
  20611. 'begin',
  20612. '']);
  20613. ConvertProgram;
  20614. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20615. LinesToStr([ // statements
  20616. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20617. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20618. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20619. 'rtl.createClass(this, "TObject", null, function () {',
  20620. ' this.$init = function () {',
  20621. ' };',
  20622. ' this.$final = function () {',
  20623. ' };',
  20624. ' rtl.addIntf(this, $mod.IBird);',
  20625. ' rtl.addIntf(this, $mod.IDog);',
  20626. '});',
  20627. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20628. ' rtl.addIntf(this, $mod.IUnknown);',
  20629. ' rtl.addIntf(this, $mod.IBird);',
  20630. ' rtl.addIntf(this, $mod.IDog);',
  20631. '});',
  20632. '']),
  20633. LinesToStr([ // $mod.$main
  20634. '']));
  20635. end;
  20636. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20637. begin
  20638. StartProgram(false);
  20639. Add([
  20640. '{$interfaces corba}',
  20641. 'type',
  20642. ' IUnknown = interface',
  20643. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20644. ' procedure Go;',
  20645. ' end;',
  20646. ' TObject = class(IUnknown)',
  20647. ' procedure Go; virtual; abstract;',
  20648. ' end;',
  20649. ' TBird = class',
  20650. ' procedure Go; override;',
  20651. ' end;',
  20652. ' TCat = class(TObject)',
  20653. ' procedure Go; override;',
  20654. ' end;',
  20655. ' TDog = class(TObject, IUnknown)',
  20656. ' procedure Go; override;',
  20657. ' end;',
  20658. 'procedure TBird.Go; begin end;',
  20659. 'procedure TCat.Go; begin end;',
  20660. 'procedure TDog.Go; begin end;',
  20661. 'begin',
  20662. '']);
  20663. ConvertProgram;
  20664. CheckSource('TestClassInterface_Corba_MethodOverride',
  20665. LinesToStr([ // statements
  20666. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20667. 'rtl.createClass(this, "TObject", null, function () {',
  20668. ' this.$init = function () {',
  20669. ' };',
  20670. ' this.$final = function () {',
  20671. ' };',
  20672. ' rtl.addIntf(this, $mod.IUnknown);',
  20673. '});',
  20674. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20675. ' this.Go = function () {',
  20676. ' };',
  20677. ' rtl.addIntf(this, $mod.IUnknown);',
  20678. '});',
  20679. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20680. ' this.Go = function () {',
  20681. ' };',
  20682. ' rtl.addIntf(this, $mod.IUnknown);',
  20683. '});',
  20684. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20685. ' this.Go = function () {',
  20686. ' };',
  20687. ' rtl.addIntf(this, $mod.IUnknown);',
  20688. '});',
  20689. '']),
  20690. LinesToStr([ // $mod.$main
  20691. '']));
  20692. end;
  20693. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20694. begin
  20695. StartProgram(false);
  20696. Add([
  20697. '{$interfaces corba}',
  20698. 'type',
  20699. ' IUnknown = interface',
  20700. ' end;',
  20701. ' IBird = interface(IUnknown)',
  20702. ' procedure Fly(s: string);',
  20703. ' end;',
  20704. ' IEagle = interface(IBird)',
  20705. ' end;',
  20706. ' IDove = interface(IBird)',
  20707. ' end;',
  20708. ' ISwallow = interface(IBird)',
  20709. ' end;',
  20710. ' TObject = class',
  20711. ' end;',
  20712. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20713. ' procedure Fly(s: string); virtual; abstract;',
  20714. ' end;',
  20715. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20716. ' FBirdIntf: IBird;',
  20717. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20718. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20719. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20720. ' FDoveObj: TBird;',
  20721. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20722. ' function GetSwallowObj: TBird; virtual; abstract;',
  20723. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20724. ' end;',
  20725. 'begin',
  20726. '']);
  20727. ConvertProgram;
  20728. CheckSource('TestClassInterface_Corba_Delegation',
  20729. LinesToStr([ // statements
  20730. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20731. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20732. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20733. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20734. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20735. 'rtl.createClass(this, "TObject", null, function () {',
  20736. ' this.$init = function () {',
  20737. ' };',
  20738. ' this.$final = function () {',
  20739. ' };',
  20740. '});',
  20741. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20742. ' rtl.addIntf(this, $mod.IBird);',
  20743. ' rtl.addIntf(this, $mod.IEagle);',
  20744. ' rtl.addIntf(this, $mod.IDove);',
  20745. ' rtl.addIntf(this, $mod.ISwallow);',
  20746. '});',
  20747. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20748. ' this.$init = function () {',
  20749. ' $mod.TObject.$init.call(this);',
  20750. ' this.FBirdIntf = null;',
  20751. ' this.FDoveObj = null;',
  20752. ' };',
  20753. ' this.$final = function () {',
  20754. ' this.FBirdIntf = undefined;',
  20755. ' this.FDoveObj = undefined;',
  20756. ' $mod.TObject.$final.call(this);',
  20757. ' };',
  20758. ' this.$intfmaps = {',
  20759. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20760. ' return this.FBirdIntf;',
  20761. ' },',
  20762. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20763. ' return this.GetEagleIntf();',
  20764. ' },',
  20765. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20766. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20767. ' },',
  20768. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20769. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20770. ' }',
  20771. ' };',
  20772. '});',
  20773. '']),
  20774. LinesToStr([ // $mod.$main
  20775. '']));
  20776. end;
  20777. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20778. begin
  20779. StartProgram(false);
  20780. Add([
  20781. '{$interfaces corba}',
  20782. 'type',
  20783. ' IUnknown = interface',
  20784. ' end;',
  20785. ' IBird = interface(IUnknown)',
  20786. ' procedure Fly(s: string);',
  20787. ' end;',
  20788. ' IEagle = interface(IBird)',
  20789. ' end;',
  20790. ' IDove = interface(IBird)',
  20791. ' end;',
  20792. ' ISwallow = interface(IBird)',
  20793. ' end;',
  20794. ' TObject = class',
  20795. ' end;',
  20796. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20797. ' procedure Fly(s: string); virtual; abstract;',
  20798. ' end;',
  20799. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20800. ' private',
  20801. ' class var FBirdIntf: IBird;',
  20802. ' class var FDoveObj: TBird;',
  20803. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20804. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20805. ' protected',
  20806. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20807. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20808. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20809. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20810. ' end;',
  20811. 'begin',
  20812. '']);
  20813. ConvertProgram;
  20814. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20815. LinesToStr([ // statements
  20816. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20817. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20818. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20819. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20820. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20821. 'rtl.createClass(this, "TObject", null, function () {',
  20822. ' this.$init = function () {',
  20823. ' };',
  20824. ' this.$final = function () {',
  20825. ' };',
  20826. '});',
  20827. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20828. ' rtl.addIntf(this, $mod.IBird);',
  20829. ' rtl.addIntf(this, $mod.IEagle);',
  20830. ' rtl.addIntf(this, $mod.IDove);',
  20831. ' rtl.addIntf(this, $mod.ISwallow);',
  20832. '});',
  20833. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20834. ' this.FBirdIntf = null;',
  20835. ' this.FDoveObj = null;',
  20836. ' this.$intfmaps = {',
  20837. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20838. ' return this.FBirdIntf;',
  20839. ' },',
  20840. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20841. ' return this.GetEagleIntf();',
  20842. ' },',
  20843. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20844. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20845. ' },',
  20846. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20847. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20848. ' }',
  20849. ' };',
  20850. '});',
  20851. '']),
  20852. LinesToStr([ // $mod.$main
  20853. '']));
  20854. end;
  20855. procedure TTestModule.TestClassInterface_Corba_Operators;
  20856. begin
  20857. StartProgram(false);
  20858. Add([
  20859. '{$interfaces corba}',
  20860. 'type',
  20861. ' IUnknown = interface',
  20862. ' end;',
  20863. ' IBird = interface(IUnknown)',
  20864. ' function GetItems(Index: longint): longint;',
  20865. ' procedure SetItems(Index: longint; Value: longint);',
  20866. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20867. ' end;',
  20868. ' TObject = class',
  20869. ' end;',
  20870. ' TBird = class(TObject,IBird)',
  20871. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20872. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20873. ' end;',
  20874. 'var',
  20875. ' IntfVar: IBird = nil;',
  20876. ' IntfVar2: IBird;',
  20877. ' ObjVar: TBird;',
  20878. ' v: JSValue;',
  20879. 'begin',
  20880. ' IntfVar:=nil;',
  20881. ' IntfVar[3]:=IntfVar[4];',
  20882. ' if Assigned(IntfVar) then ;',
  20883. ' IntfVar:=IntfVar2;',
  20884. ' IntfVar:=ObjVar;',
  20885. ' if IntfVar=IntfVar2 then ;',
  20886. ' if IntfVar<>IntfVar2 then ;',
  20887. ' if IntfVar is IBird then ;',
  20888. ' if IntfVar is TBird then ;',
  20889. ' if ObjVar is IBird then ;',
  20890. ' IntfVar:=IntfVar2 as IBird;',
  20891. ' ObjVar:=IntfVar2 as TBird;',
  20892. ' IntfVar:=ObjVar as IBird;',
  20893. ' IntfVar:=IBird(IntfVar2);',
  20894. ' ObjVar:=TBird(IntfVar);',
  20895. ' IntfVar:=IBird(ObjVar);',
  20896. ' v:=IntfVar;',
  20897. ' IntfVar:=IBird(v);',
  20898. ' if v is IBird then ;',
  20899. ' v:=JSValue(IntfVar);',
  20900. ' v:=IBird;',
  20901. '']);
  20902. ConvertProgram;
  20903. CheckSource('TestClassInterface_Corba_Operators',
  20904. LinesToStr([ // statements
  20905. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20906. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20907. 'rtl.createClass(this, "TObject", null, function () {',
  20908. ' this.$init = function () {',
  20909. ' };',
  20910. ' this.$final = function () {',
  20911. ' };',
  20912. '});',
  20913. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20914. ' rtl.addIntf(this, $mod.IBird);',
  20915. '});',
  20916. 'this.IntfVar = null;',
  20917. 'this.IntfVar2 = null;',
  20918. 'this.ObjVar = null;',
  20919. 'this.v = undefined;',
  20920. '']),
  20921. LinesToStr([ // $mod.$main
  20922. '$mod.IntfVar = null;',
  20923. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20924. 'if ($mod.IntfVar != null) ;',
  20925. '$mod.IntfVar = $mod.IntfVar2;',
  20926. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20927. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20928. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20929. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20930. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20931. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20932. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20933. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20934. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20935. '$mod.IntfVar = $mod.IntfVar2;',
  20936. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20937. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20938. '$mod.v = $mod.IntfVar;',
  20939. '$mod.IntfVar = rtl.getObject($mod.v);',
  20940. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20941. '$mod.v = $mod.IntfVar;',
  20942. '$mod.v = $mod.IBird;',
  20943. '']));
  20944. end;
  20945. procedure TTestModule.TestClassInterface_Corba_Args;
  20946. begin
  20947. StartProgram(false);
  20948. Add([
  20949. '{$interfaces corba}',
  20950. 'type',
  20951. ' IUnknown = interface',
  20952. ' end;',
  20953. ' IBird = interface(IUnknown)',
  20954. ' end;',
  20955. ' TObject = class',
  20956. ' end;',
  20957. ' TBird = class(TObject,IBird)',
  20958. ' end;',
  20959. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  20960. 'begin',
  20961. ' DoIt(i,i,i);',
  20962. 'end;',
  20963. 'procedure Change(var i: IBird; out j: IBird);',
  20964. 'begin',
  20965. ' DoIt(i,i,i);',
  20966. ' Change(i,i);',
  20967. 'end;',
  20968. 'var',
  20969. ' i: IBird;',
  20970. ' o: TBird;',
  20971. 'begin',
  20972. ' DoIt(i,i,i);',
  20973. ' Change(i,i);',
  20974. ' DoIt(o,o,o);',
  20975. '']);
  20976. ConvertProgram;
  20977. CheckSource('TestClassInterface_Corba_Args',
  20978. LinesToStr([ // statements
  20979. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20980. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  20981. 'rtl.createClass(this, "TObject", null, function () {',
  20982. ' this.$init = function () {',
  20983. ' };',
  20984. ' this.$final = function () {',
  20985. ' };',
  20986. '});',
  20987. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20988. ' rtl.addIntf(this, $mod.IBird);',
  20989. '});',
  20990. 'this.DoIt = function (u, i, j) {',
  20991. ' $mod.DoIt({',
  20992. ' get: function () {',
  20993. ' return i;',
  20994. ' },',
  20995. ' set: function (v) {',
  20996. ' i = v;',
  20997. ' }',
  20998. ' }, i, i);',
  20999. '};',
  21000. 'this.Change = function (i, j) {',
  21001. ' $mod.DoIt(i, i.get(), i.get());',
  21002. ' $mod.Change(i, i);',
  21003. '};',
  21004. 'this.i = null;',
  21005. 'this.o = null;',
  21006. '']),
  21007. LinesToStr([ // $mod.$main
  21008. '$mod.DoIt({',
  21009. ' p: $mod,',
  21010. ' get: function () {',
  21011. ' return this.p.i;',
  21012. ' },',
  21013. ' set: function (v) {',
  21014. ' this.p.i = v;',
  21015. ' }',
  21016. '}, $mod.i, $mod.i);',
  21017. '$mod.Change({',
  21018. ' p: $mod,',
  21019. ' get: function () {',
  21020. ' return this.p.i;',
  21021. ' },',
  21022. ' set: function (v) {',
  21023. ' this.p.i = v;',
  21024. ' }',
  21025. '}, {',
  21026. ' p: $mod,',
  21027. ' get: function () {',
  21028. ' return this.p.i;',
  21029. ' },',
  21030. ' set: function (v) {',
  21031. ' this.p.i = v;',
  21032. ' }',
  21033. '});',
  21034. '$mod.DoIt({',
  21035. ' p: $mod,',
  21036. ' get: function () {',
  21037. ' return this.p.o;',
  21038. ' },',
  21039. ' set: function (v) {',
  21040. ' this.p.o = v;',
  21041. ' }',
  21042. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21043. '']));
  21044. end;
  21045. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21046. begin
  21047. StartProgram(false);
  21048. Add([
  21049. '{$interfaces corba}',
  21050. 'type',
  21051. ' IUnknown = interface end;',
  21052. ' TObject = class',
  21053. ' Id: longint;',
  21054. ' end;',
  21055. ' IEnumerator = interface(IUnknown)',
  21056. ' function GetCurrent: TObject;',
  21057. ' function MoveNext: Boolean;',
  21058. ' property Current: TObject read GetCurrent;',
  21059. ' end;',
  21060. ' IEnumerable = interface(IUnknown)',
  21061. ' function GetEnumerator: IEnumerator;',
  21062. ' end;',
  21063. 'var',
  21064. ' o: TObject;',
  21065. ' i: IEnumerable;',
  21066. 'begin',
  21067. ' for o in i do o.Id:=3;',
  21068. '']);
  21069. ConvertProgram;
  21070. CheckSource('TestClassInterface_Corba_ForIn',
  21071. LinesToStr([ // statements
  21072. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21073. 'rtl.createClass(this, "TObject", null, function () {',
  21074. ' this.$init = function () {',
  21075. ' this.Id = 0;',
  21076. ' };',
  21077. ' this.$final = function () {',
  21078. ' };',
  21079. '});',
  21080. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21081. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21082. 'this.o = null;',
  21083. 'this.i = null;',
  21084. '']),
  21085. LinesToStr([ // $mod.$main
  21086. 'var $in = $mod.i.GetEnumerator();',
  21087. 'while ($in.MoveNext()) {',
  21088. ' $mod.o = $in.GetCurrent();',
  21089. ' $mod.o.Id = 3;',
  21090. '};',
  21091. '']));
  21092. end;
  21093. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21094. begin
  21095. StartProgram(false);
  21096. Add([
  21097. '{$interfaces corba}',
  21098. 'type',
  21099. ' IUnknown = interface end;',
  21100. ' IBird = interface(IUnknown)',
  21101. ' function Fly(w: word): word;',
  21102. ' end;',
  21103. ' TBirdArray = array of IBird;',
  21104. 'var',
  21105. ' i: IBird;',
  21106. ' a: TBirdArray;',
  21107. 'begin',
  21108. ' SetLength(a,3);',
  21109. ' i:=a[1];',
  21110. ' a[2]:=i;',
  21111. ' for i in a do i.fly(3);',
  21112. '']);
  21113. ConvertProgram;
  21114. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21115. LinesToStr([ // statements
  21116. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21117. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21118. 'this.i = null;',
  21119. 'this.a = [];',
  21120. '']),
  21121. LinesToStr([ // $mod.$main
  21122. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21123. '$mod.i = $mod.a[1];',
  21124. '$mod.a[2] = $mod.i;',
  21125. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21126. ' $mod.i = $in[$l];',
  21127. ' $mod.i.Fly(3);',
  21128. '};',
  21129. '']));
  21130. end;
  21131. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21132. begin
  21133. StartProgram(false);
  21134. Add([
  21135. '{$interfaces com}',
  21136. 'type',
  21137. ' IUnknown = interface',
  21138. ' function _AddRef: longint;',
  21139. ' function _Release: longint;',
  21140. ' end;',
  21141. ' TObject = class(IUnknown)',
  21142. ' function _AddRef: longint; virtual; abstract;',
  21143. ' function _Release: longint; virtual; abstract;',
  21144. ' end;',
  21145. 'var',
  21146. ' i: IUnknown;',
  21147. 'procedure DoGlobal(o: TObject);',
  21148. 'begin',
  21149. ' i:=nil;',
  21150. ' i:=o;',
  21151. ' i:=i;',
  21152. 'end;',
  21153. 'procedure DoLocal(o: TObject);',
  21154. 'const k: IUnknown = nil;',
  21155. 'var j: IUnknown;',
  21156. 'begin',
  21157. ' k:=o;',
  21158. ' k:=i;',
  21159. ' j:=o;',
  21160. ' j:=i;',
  21161. 'end;',
  21162. 'var o: TObject;',
  21163. 'begin',
  21164. ' i:=nil;',
  21165. ' i:=o;',
  21166. '']);
  21167. ConvertProgram;
  21168. CheckSource('TestClassInterface_COM_AssignVar',
  21169. LinesToStr([ // statements
  21170. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21171. 'rtl.createClass(this, "TObject", null, function () {',
  21172. ' this.$init = function () {',
  21173. ' };',
  21174. ' this.$final = function () {',
  21175. ' };',
  21176. ' rtl.addIntf(this, $mod.IUnknown);',
  21177. '});',
  21178. 'this.i = null;',
  21179. 'this.DoGlobal = function (o) {',
  21180. ' rtl.setIntfP($mod, "i", null);',
  21181. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21182. ' rtl.setIntfP($mod, "i", $mod.i);',
  21183. '};',
  21184. 'var k = null;',
  21185. 'this.DoLocal = function (o) {',
  21186. ' var j = null;',
  21187. ' try{',
  21188. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21189. ' k = rtl.setIntfL(k, $mod.i);',
  21190. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21191. ' j = rtl.setIntfL(j, $mod.i);',
  21192. ' }finally{',
  21193. ' rtl._Release(j);',
  21194. ' };',
  21195. '};',
  21196. 'this.o = null;',
  21197. '']),
  21198. LinesToStr([ // $mod.$main
  21199. 'rtl.setIntfP($mod, "i", null);',
  21200. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21201. '']));
  21202. end;
  21203. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21204. begin
  21205. StartProgram(false);
  21206. Add([
  21207. '{$interfaces com}',
  21208. 'type',
  21209. ' IUnknown = interface',
  21210. ' function _AddRef: longint;',
  21211. ' function _Release: longint;',
  21212. ' end;',
  21213. ' TObject = class(IUnknown)',
  21214. ' function _AddRef: longint; virtual; abstract;',
  21215. ' function _Release: longint; virtual; abstract;',
  21216. ' end;',
  21217. 'procedure DoDefault(i, j: IUnknown);',
  21218. 'begin',
  21219. ' i:=nil;',
  21220. ' i:=j;',
  21221. 'end;',
  21222. 'begin',
  21223. '']);
  21224. ConvertProgram;
  21225. CheckSource('TestClassInterface_COM_AssignArg',
  21226. LinesToStr([ // statements
  21227. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21228. 'rtl.createClass(this, "TObject", null, function () {',
  21229. ' this.$init = function () {',
  21230. ' };',
  21231. ' this.$final = function () {',
  21232. ' };',
  21233. ' rtl.addIntf(this, $mod.IUnknown);',
  21234. '});',
  21235. 'this.DoDefault = function (i, j) {',
  21236. ' rtl._AddRef(i);',
  21237. ' try {',
  21238. ' i = rtl.setIntfL(i, null);',
  21239. ' i = rtl.setIntfL(i, j);',
  21240. ' } finally {',
  21241. ' rtl._Release(i);',
  21242. ' };',
  21243. '};',
  21244. '']),
  21245. LinesToStr([ // $mod.$main
  21246. '']));
  21247. end;
  21248. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21249. begin
  21250. StartProgram(false);
  21251. Add([
  21252. '{$interfaces com}',
  21253. 'type',
  21254. ' IUnknown = interface',
  21255. ' function _AddRef: longint;',
  21256. ' function _Release: longint;',
  21257. ' end;',
  21258. ' TObject = class(IUnknown)',
  21259. ' function _AddRef: longint; virtual; abstract;',
  21260. ' function _Release: longint; virtual; abstract;',
  21261. ' end;',
  21262. 'function DoDefault(i: IUnknown): IUnknown;',
  21263. 'begin',
  21264. ' Result:=i;',
  21265. ' if Result<>nil then exit;',
  21266. 'end;',
  21267. 'begin',
  21268. '']);
  21269. ConvertProgram;
  21270. CheckSource('TestClassInterface_COM_FunctionResult',
  21271. LinesToStr([ // statements
  21272. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21273. 'rtl.createClass(this, "TObject", null, function () {',
  21274. ' this.$init = function () {',
  21275. ' };',
  21276. ' this.$final = function () {',
  21277. ' };',
  21278. ' rtl.addIntf(this, $mod.IUnknown);',
  21279. '});',
  21280. 'this.DoDefault = function (i) {',
  21281. ' var Result = null;',
  21282. ' var $ok = false;',
  21283. ' try {',
  21284. ' Result = rtl.setIntfL(Result, i);',
  21285. ' if(Result !== null){',
  21286. ' $ok = true;',
  21287. ' return Result;',
  21288. ' };',
  21289. ' $ok = true;',
  21290. ' } finally {',
  21291. ' if(!$ok) rtl._Release(Result);',
  21292. ' };',
  21293. ' return Result;',
  21294. '};',
  21295. '']),
  21296. LinesToStr([ // $mod.$main
  21297. '']));
  21298. end;
  21299. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21300. begin
  21301. StartProgram(false);
  21302. Add([
  21303. '{$interfaces com}',
  21304. 'type',
  21305. ' IUnknown = interface',
  21306. ' function _AddRef: longint;',
  21307. ' function _Release: longint;',
  21308. ' end;',
  21309. ' TObject = class(IUnknown)',
  21310. ' function _AddRef: longint; virtual; abstract;',
  21311. ' function _Release: longint; virtual; abstract;',
  21312. ' function GetIntf: IUnknown; virtual;',
  21313. ' end;',
  21314. ' TMouse = class',
  21315. ' function GetIntf: IUnknown; override;',
  21316. ' end;',
  21317. 'function TObject.GetIntf: IUnknown; begin end;',
  21318. 'function TMouse.GetIntf: IUnknown;',
  21319. 'var i: IUnknown;',
  21320. 'begin',
  21321. ' inherited;',
  21322. ' inherited GetIntf;',
  21323. ' inherited GetIntf();',
  21324. ' Result:=inherited GetIntf;',
  21325. ' Result:=inherited GetIntf();',
  21326. ' i:=inherited GetIntf;',
  21327. ' i:=inherited GetIntf();',
  21328. 'end;',
  21329. 'begin',
  21330. '']);
  21331. ConvertProgram;
  21332. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21333. LinesToStr([ // statements
  21334. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21335. 'rtl.createClass(this, "TObject", null, function () {',
  21336. ' this.$init = function () {',
  21337. ' };',
  21338. ' this.$final = function () {',
  21339. ' };',
  21340. ' this.GetIntf = function () {',
  21341. ' var Result = null;',
  21342. ' return Result;',
  21343. ' };',
  21344. ' rtl.addIntf(this, $mod.IUnknown);',
  21345. '});',
  21346. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21347. ' this.GetIntf = function () {',
  21348. ' var Result = null;',
  21349. ' var i = null;',
  21350. ' var $ir = rtl.createIntfRefs();',
  21351. ' var $ok = false;',
  21352. ' try {',
  21353. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21354. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21355. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21356. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21357. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21358. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21359. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21360. ' $ok = true;',
  21361. ' } finally {',
  21362. ' $ir.free();',
  21363. ' rtl._Release(i);',
  21364. ' if (!$ok) rtl._Release(Result);',
  21365. ' };',
  21366. ' return Result;',
  21367. ' };',
  21368. ' rtl.addIntf(this, $mod.IUnknown);',
  21369. '});',
  21370. '']),
  21371. LinesToStr([ // $mod.$main
  21372. '']));
  21373. end;
  21374. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21375. begin
  21376. StartProgram(false);
  21377. Add([
  21378. '{$interfaces com}',
  21379. 'type',
  21380. ' IUnknown = interface',
  21381. ' function _AddRef: longint;',
  21382. ' function _Release: longint;',
  21383. ' end;',
  21384. ' TObject = class(IUnknown)',
  21385. ' function _AddRef: longint; virtual; abstract;',
  21386. ' function _Release: longint; virtual; abstract;',
  21387. ' end;',
  21388. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21389. 'begin',
  21390. ' if i is IUnknown then ;',
  21391. ' if o is IUnknown then ;',
  21392. ' if i is TObject then ;',
  21393. ' i:=j as IUnknown;',
  21394. ' i:=o as IUnknown;',
  21395. ' o:=j as TObject;',
  21396. ' i:=IUnknown(j);',
  21397. ' i:=IUnknown(o);',
  21398. ' o:=TObject(i);',
  21399. 'end;',
  21400. 'begin',
  21401. '']);
  21402. ConvertProgram;
  21403. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21404. LinesToStr([ // statements
  21405. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21406. 'rtl.createClass(this, "TObject", null, function () {',
  21407. ' this.$init = function () {',
  21408. ' };',
  21409. ' this.$final = function () {',
  21410. ' };',
  21411. ' rtl.addIntf(this, $mod.IUnknown);',
  21412. '});',
  21413. 'this.DoDefault = function (i, j, o) {',
  21414. ' rtl._AddRef(i);',
  21415. ' try {',
  21416. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21417. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21418. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21419. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21420. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21421. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21422. ' i = rtl.setIntfL(i, j);',
  21423. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21424. ' o = rtl.intfToClass(i, $mod.TObject);',
  21425. ' } finally {',
  21426. ' rtl._Release(i);',
  21427. ' };',
  21428. '};',
  21429. '']),
  21430. LinesToStr([ // $mod.$main
  21431. '']));
  21432. end;
  21433. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21434. begin
  21435. StartProgram(false);
  21436. Add([
  21437. '{$interfaces com}',
  21438. 'type',
  21439. ' IUnknown = interface',
  21440. ' function _AddRef: longint;',
  21441. ' function _Release: longint;',
  21442. ' end;',
  21443. ' TObject = class(IUnknown)',
  21444. ' function _AddRef: longint; virtual; abstract;',
  21445. ' function _Release: longint; virtual; abstract;',
  21446. ' end;',
  21447. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21448. 'var o: TObject;',
  21449. 'begin',
  21450. ' DoIt(v,v,v,v);',
  21451. ' DoIt(o,o,k,k);',
  21452. 'end;',
  21453. 'procedure DoSome;',
  21454. 'var v: IUnknown;',
  21455. 'begin',
  21456. ' DoIt(v,v,v,v);',
  21457. 'end;',
  21458. 'var i: IUnknown;',
  21459. 'begin',
  21460. ' DoIt(i,i,i,i);',
  21461. '']);
  21462. ConvertProgram;
  21463. CheckSource('TestClassInterface_COM_PassAsArg',
  21464. LinesToStr([ // statements
  21465. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21466. 'rtl.createClass(this, "TObject", null, function () {',
  21467. ' this.$init = function () {',
  21468. ' };',
  21469. ' this.$final = function () {',
  21470. ' };',
  21471. ' rtl.addIntf(this, $mod.IUnknown);',
  21472. '});',
  21473. 'this.DoIt = function (v, j, k, l) {',
  21474. ' var o = null;',
  21475. ' var $ir = rtl.createIntfRefs();',
  21476. ' rtl._AddRef(v);',
  21477. ' try {',
  21478. ' $mod.DoIt(v, v, {',
  21479. ' get: function () {',
  21480. ' return v;',
  21481. ' },',
  21482. ' set: function (w) {',
  21483. ' v = rtl.setIntfL(v, w);',
  21484. ' }',
  21485. ' }, {',
  21486. ' get: function () {',
  21487. ' return v;',
  21488. ' },',
  21489. ' set: function (w) {',
  21490. ' v = rtl.setIntfL(v, w);',
  21491. ' }',
  21492. ' });',
  21493. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21494. ' } finally {',
  21495. ' $ir.free();',
  21496. ' rtl._Release(v);',
  21497. ' };',
  21498. '};',
  21499. 'this.DoSome = function () {',
  21500. ' var v = null;',
  21501. ' try {',
  21502. ' $mod.DoIt(v, v, {',
  21503. ' get: function () {',
  21504. ' return v;',
  21505. ' },',
  21506. ' set: function (w) {',
  21507. ' v = rtl.setIntfL(v, w);',
  21508. ' }',
  21509. ' }, {',
  21510. ' get: function () {',
  21511. ' return v;',
  21512. ' },',
  21513. ' set: function (w) {',
  21514. ' v = rtl.setIntfL(v, w);',
  21515. ' }',
  21516. ' });',
  21517. ' } finally {',
  21518. ' rtl._Release(v);',
  21519. ' };',
  21520. '};',
  21521. 'this.i = null;',
  21522. '']),
  21523. LinesToStr([ // $mod.$main
  21524. '$mod.DoIt($mod.i, $mod.i, {',
  21525. ' p: $mod,',
  21526. ' get: function () {',
  21527. ' return this.p.i;',
  21528. ' },',
  21529. ' set: function (v) {',
  21530. ' rtl.setIntfP(this.p, "i", v);',
  21531. ' }',
  21532. '}, {',
  21533. ' p: $mod,',
  21534. ' get: function () {',
  21535. ' return this.p.i;',
  21536. ' },',
  21537. ' set: function (v) {',
  21538. ' rtl.setIntfP(this.p, "i", v);',
  21539. ' }',
  21540. '});',
  21541. '']));
  21542. end;
  21543. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21544. begin
  21545. StartProgram(false);
  21546. Add([
  21547. '{$interfaces com}',
  21548. 'type',
  21549. ' IUnknown = interface',
  21550. ' function _AddRef: longint;',
  21551. ' function _Release: longint;',
  21552. ' end;',
  21553. ' TObject = class(IUnknown)',
  21554. ' function _AddRef: longint; virtual; abstract;',
  21555. ' function _Release: longint; virtual; abstract;',
  21556. ' end;',
  21557. 'procedure DoIt(out i);',
  21558. 'begin end;',
  21559. 'procedure DoSome;',
  21560. 'var v: IUnknown;',
  21561. 'begin',
  21562. ' DoIt(v);',
  21563. 'end;',
  21564. 'function GetIt: IUnknown;',
  21565. 'begin',
  21566. ' DoIt(Result);',
  21567. 'end;',
  21568. 'var i: IUnknown;',
  21569. 'begin',
  21570. ' DoIt(i);',
  21571. '']);
  21572. ConvertProgram;
  21573. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21574. LinesToStr([ // statements
  21575. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21576. 'rtl.createClass(this, "TObject", null, function () {',
  21577. ' this.$init = function () {',
  21578. ' };',
  21579. ' this.$final = function () {',
  21580. ' };',
  21581. ' rtl.addIntf(this, $mod.IUnknown);',
  21582. '});',
  21583. 'this.DoIt = function (i) {',
  21584. '};',
  21585. 'this.DoSome = function () {',
  21586. ' var v = null;',
  21587. ' try {',
  21588. ' $mod.DoIt({',
  21589. ' get: function () {',
  21590. ' return v;',
  21591. ' },',
  21592. ' set: function (w) {',
  21593. ' v = w;',
  21594. ' }',
  21595. ' });',
  21596. ' } finally {',
  21597. ' rtl._Release(v);',
  21598. ' };',
  21599. '};',
  21600. 'this.GetIt = function () {',
  21601. ' var Result = null;',
  21602. ' var $ok = false;',
  21603. ' try {',
  21604. ' $mod.DoIt({',
  21605. ' get: function () {',
  21606. ' return Result;',
  21607. ' },',
  21608. ' set: function (v) {',
  21609. ' Result = v;',
  21610. ' }',
  21611. ' });',
  21612. ' $ok = true;',
  21613. ' } finally {',
  21614. ' if (!$ok) rtl._Release(Result);',
  21615. ' };',
  21616. ' return Result;',
  21617. '};',
  21618. 'this.i = null;',
  21619. '']),
  21620. LinesToStr([ // $mod.$main
  21621. 'try {',
  21622. ' $mod.DoIt({',
  21623. ' p: $mod,',
  21624. ' get: function () {',
  21625. ' return this.p.i;',
  21626. ' },',
  21627. ' set: function (v) {',
  21628. ' this.p.i = v;',
  21629. ' }',
  21630. ' });',
  21631. '} finally {',
  21632. ' rtl._Release($mod.i);',
  21633. '};',
  21634. '']));
  21635. end;
  21636. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21637. begin
  21638. StartProgram(false);
  21639. Add([
  21640. '{$interfaces com}',
  21641. 'type',
  21642. ' IUnknown = interface',
  21643. ' function _AddRef: longint;',
  21644. ' function _Release: longint;',
  21645. ' end;',
  21646. ' TObject = class(IUnknown)',
  21647. ' function _AddRef: longint; virtual; abstract;',
  21648. ' function _Release: longint; virtual; abstract;',
  21649. ' end;',
  21650. 'function GetIt: IUnknown;',
  21651. 'begin',
  21652. 'end;',
  21653. 'procedure DoSome;',
  21654. 'var v: IUnknown;',
  21655. ' i: longint;',
  21656. 'begin',
  21657. ' v:=GetIt;',
  21658. ' v:=GetIt();',
  21659. ' GetIt()._AddRef;',
  21660. ' i:=GetIt()._AddRef;',
  21661. 'end;',
  21662. 'var v: IUnknown;',
  21663. ' i: longint;',
  21664. 'begin',
  21665. ' v:=GetIt;',
  21666. ' v:=GetIt();',
  21667. ' GetIt()._AddRef;',
  21668. ' i:=GetIt()._AddRef;',
  21669. '']);
  21670. ConvertProgram;
  21671. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21672. LinesToStr([ // statements
  21673. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21674. 'rtl.createClass(this, "TObject", null, function () {',
  21675. ' this.$init = function () {',
  21676. ' };',
  21677. ' this.$final = function () {',
  21678. ' };',
  21679. ' rtl.addIntf(this, $mod.IUnknown);',
  21680. '});',
  21681. 'this.GetIt = function () {',
  21682. ' var Result = null;',
  21683. ' return Result;',
  21684. '};',
  21685. 'this.DoSome = function () {',
  21686. ' var v = null;',
  21687. ' var i = 0;',
  21688. ' var $ir = rtl.createIntfRefs();',
  21689. ' try {',
  21690. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21691. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21692. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21693. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21694. ' } finally {',
  21695. ' $ir.free();',
  21696. ' rtl._Release(v);',
  21697. ' };',
  21698. '};',
  21699. 'this.v = null;',
  21700. 'this.i = 0;',
  21701. '']),
  21702. LinesToStr([ // $mod.$main
  21703. 'var $ir = rtl.createIntfRefs();',
  21704. 'try {',
  21705. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21706. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21707. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21708. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21709. '} finally {',
  21710. ' $ir.free();',
  21711. '};',
  21712. '']));
  21713. end;
  21714. procedure TTestModule.TestClassInterface_COM_Property;
  21715. begin
  21716. StartProgram(false);
  21717. Add([
  21718. '{$interfaces com}',
  21719. 'type',
  21720. ' IUnknown = interface',
  21721. ' function _AddRef: longint;',
  21722. ' function _Release: longint;',
  21723. ' end;',
  21724. ' TObject = class(IUnknown)',
  21725. ' FAnt: IUnknown;',
  21726. ' function _AddRef: longint; virtual; abstract;',
  21727. ' function _Release: longint; virtual; abstract;',
  21728. ' function GetBird: IUnknown; virtual; abstract;',
  21729. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21730. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21731. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21732. ' property Ant: IUnknown read FAnt write FAnt;',
  21733. ' property Bird: IUnknown read GetBird write SetBird;',
  21734. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21735. ' end;',
  21736. 'procedure DoIt;',
  21737. 'var',
  21738. ' o: TObject;',
  21739. ' v: IUnknown;',
  21740. 'begin',
  21741. ' v:=o.Ant;',
  21742. ' o.Ant:=v;',
  21743. ' o.Ant:=o.Ant;',
  21744. ' v:=o.Bird;',
  21745. ' o.Bird:=v;',
  21746. ' o.Bird:=o.Bird;',
  21747. ' v:=o.Items[1];',
  21748. ' o.Items[2]:=v;',
  21749. ' o.Items[3]:=o.Items[4];',
  21750. ' v:=o[5];',
  21751. ' o[6]:=v;',
  21752. ' o[7]:=o[8];',
  21753. 'end;',
  21754. 'begin',
  21755. '']);
  21756. ConvertProgram;
  21757. CheckSource('TestClassInterface_COM_Property',
  21758. LinesToStr([ // statements
  21759. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21760. 'rtl.createClass(this, "TObject", null, function () {',
  21761. ' this.$init = function () {',
  21762. ' this.FAnt = null;',
  21763. ' };',
  21764. ' this.$final = function () {',
  21765. ' rtl.setIntfP(this, "FAnt", null);',
  21766. ' };',
  21767. ' rtl.addIntf(this, $mod.IUnknown);',
  21768. '});',
  21769. 'this.DoIt = function () {',
  21770. ' var o = null;',
  21771. ' var v = null;',
  21772. ' var $ir = rtl.createIntfRefs();',
  21773. ' try {',
  21774. ' v = rtl.setIntfL(v, o.FAnt);',
  21775. ' rtl.setIntfP(o, "FAnt", v);',
  21776. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21777. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21778. ' o.SetBird(v);',
  21779. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21780. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21781. ' o.SetItems(2, v);',
  21782. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21783. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21784. ' o.SetItems(6, v);',
  21785. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21786. ' } finally {',
  21787. ' $ir.free();',
  21788. ' rtl._Release(v);',
  21789. ' };',
  21790. '};',
  21791. '']),
  21792. LinesToStr([ // $mod.$main
  21793. '']));
  21794. end;
  21795. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21796. begin
  21797. StartProgram(false);
  21798. Add([
  21799. '{$interfaces com}',
  21800. 'type',
  21801. ' IUnknown = interface',
  21802. ' function _AddRef: longint;',
  21803. ' function _Release: longint;',
  21804. ' function GetBird: IUnknown;',
  21805. ' procedure SetBird(Value: IUnknown);',
  21806. ' function GetItems(Index: longint): IUnknown;',
  21807. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21808. ' property Bird: IUnknown read GetBird write SetBird;',
  21809. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21810. ' end;',
  21811. ' TObject = class(IUnknown)',
  21812. ' function _AddRef: longint; virtual; abstract;',
  21813. ' function _Release: longint; virtual; abstract;',
  21814. ' function GetBird: IUnknown; virtual; abstract;',
  21815. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21816. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21817. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21818. ' end;',
  21819. 'procedure DoIt;',
  21820. 'var',
  21821. ' o: TObject;',
  21822. ' v: IUnknown;',
  21823. 'begin',
  21824. ' v:=v.Items[1];',
  21825. ' v.Items[2]:=v;',
  21826. ' v.Items[3]:=v.Items[4];',
  21827. ' v:=v[5];',
  21828. ' v[6]:=v;',
  21829. ' v[7]:=v[8];',
  21830. ' v[9].Bird.Bird:=v;',
  21831. ' v:=v.Bird[10].Bird',
  21832. 'end;',
  21833. 'begin',
  21834. '']);
  21835. ConvertProgram;
  21836. CheckSource('TestClassInterface_COM_IntfProperty',
  21837. LinesToStr([ // statements
  21838. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21839. ' "_AddRef",',
  21840. ' "_Release",',
  21841. ' "GetBird",',
  21842. ' "SetBird",',
  21843. ' "GetItems",',
  21844. ' "SetItems"',
  21845. '], null);',
  21846. 'rtl.createClass(this, "TObject", null, function () {',
  21847. ' this.$init = function () {',
  21848. ' };',
  21849. ' this.$final = function () {',
  21850. ' };',
  21851. ' rtl.addIntf(this, $mod.IUnknown);',
  21852. '});',
  21853. 'this.DoIt = function () {',
  21854. ' var o = null;',
  21855. ' var v = null;',
  21856. ' var $ir = rtl.createIntfRefs();',
  21857. ' try {',
  21858. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21859. ' v.SetItems(2, v);',
  21860. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21861. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21862. ' v.SetItems(6, v);',
  21863. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21864. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21865. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21866. ' } finally {',
  21867. ' $ir.free();',
  21868. ' rtl._Release(v);',
  21869. ' };',
  21870. '};',
  21871. '']),
  21872. LinesToStr([ // $mod.$main
  21873. '']));
  21874. end;
  21875. procedure TTestModule.TestClassInterface_COM_Delegation;
  21876. begin
  21877. StartProgram(false);
  21878. Add([
  21879. '{$interfaces com}',
  21880. 'type',
  21881. ' IUnknown = interface',
  21882. ' function _AddRef: longint;',
  21883. ' function _Release: longint;',
  21884. ' end;',
  21885. ' IBird = interface(IUnknown)',
  21886. ' procedure Fly(s: string);',
  21887. ' end;',
  21888. ' IEagle = interface(IBird) end;',
  21889. ' IDove = interface(IBird) end;',
  21890. ' ISwallow = interface(IBird) end;',
  21891. ' TObject = class',
  21892. ' end;',
  21893. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21894. ' function _AddRef: longint; virtual; abstract;',
  21895. ' function _Release: longint; virtual; abstract;',
  21896. ' procedure Fly(s: string); virtual; abstract;',
  21897. ' end;',
  21898. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21899. ' function _AddRef: longint; virtual; abstract;',
  21900. ' function _Release: longint; virtual; abstract;',
  21901. ' FBirdIntf: IBird;',
  21902. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21903. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21904. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21905. ' FDoveObj: TBird;',
  21906. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21907. ' function GetSwallowObj: TBird; virtual; abstract;',
  21908. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21909. ' end;',
  21910. 'begin',
  21911. '']);
  21912. ConvertProgram;
  21913. CheckSource('TestClassInterface_COM_Delegation',
  21914. LinesToStr([ // statements
  21915. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21916. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21917. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21918. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21919. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21920. 'rtl.createClass(this, "TObject", null, function () {',
  21921. ' this.$init = function () {',
  21922. ' };',
  21923. ' this.$final = function () {',
  21924. ' };',
  21925. '});',
  21926. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21927. ' rtl.addIntf(this, $mod.IBird);',
  21928. ' rtl.addIntf(this, $mod.IEagle);',
  21929. ' rtl.addIntf(this, $mod.IDove);',
  21930. ' rtl.addIntf(this, $mod.ISwallow);',
  21931. '});',
  21932. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21933. ' this.$init = function () {',
  21934. ' $mod.TObject.$init.call(this);',
  21935. ' this.FBirdIntf = null;',
  21936. ' this.FDoveObj = null;',
  21937. ' };',
  21938. ' this.$final = function () {',
  21939. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21940. ' this.FDoveObj = undefined;',
  21941. ' $mod.TObject.$final.call(this);',
  21942. ' };',
  21943. ' this.$intfmaps = {',
  21944. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  21945. ' return rtl._AddRef(this.FBirdIntf);',
  21946. ' },',
  21947. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  21948. ' return this.GetEagleIntf();',
  21949. ' },',
  21950. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  21951. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  21952. ' },',
  21953. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  21954. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  21955. ' }',
  21956. ' };',
  21957. '});',
  21958. '']),
  21959. LinesToStr([ // $mod.$main
  21960. '']));
  21961. end;
  21962. procedure TTestModule.TestClassInterface_COM_With;
  21963. begin
  21964. StartProgram(false);
  21965. Add([
  21966. '{$interfaces com}',
  21967. 'type',
  21968. ' IUnknown = interface',
  21969. ' function _AddRef: longint;',
  21970. ' function _Release: longint;',
  21971. ' function GetAnt: IUnknown;',
  21972. ' property Ant: IUnknown read GetAnt;',
  21973. ' end;',
  21974. ' TObject = class(IUnknown)',
  21975. ' function _AddRef: longint; virtual; abstract;',
  21976. ' function _Release: longint; virtual; abstract;',
  21977. ' function GetAnt: IUnknown; virtual; abstract;',
  21978. ' property Ant: IUnknown read GetAnt;',
  21979. ' end;',
  21980. 'procedure DoIt;',
  21981. 'var',
  21982. ' i: IUnknown;',
  21983. 'begin',
  21984. ' with i do ',
  21985. ' GetAnt;',
  21986. ' with i.Ant, Ant do ',
  21987. ' GetAnt;',
  21988. 'end;',
  21989. 'begin',
  21990. '']);
  21991. ConvertProgram;
  21992. CheckSource('TestClassInterface_COM_With',
  21993. LinesToStr([ // statements
  21994. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  21995. 'rtl.createClass(this, "TObject", null, function () {',
  21996. ' this.$init = function () {',
  21997. ' };',
  21998. ' this.$final = function () {',
  21999. ' };',
  22000. ' rtl.addIntf(this, $mod.IUnknown);',
  22001. '});',
  22002. 'this.DoIt = function () {',
  22003. ' var i = null;',
  22004. ' var $ir = rtl.createIntfRefs();',
  22005. ' try {',
  22006. ' $ir.ref(1, i.GetAnt());',
  22007. ' var $with = $ir.ref(2, i.GetAnt());',
  22008. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22009. ' $ir.ref(4, $with1.GetAnt());',
  22010. ' } finally {',
  22011. ' $ir.free();',
  22012. ' };',
  22013. '};',
  22014. '']),
  22015. LinesToStr([ // $mod.$main
  22016. '']));
  22017. end;
  22018. procedure TTestModule.TestClassInterface_COM_ForIn;
  22019. begin
  22020. StartProgram(false);
  22021. Add([
  22022. '{$interfaces com}',
  22023. 'type',
  22024. ' IUnknown = interface end;',
  22025. ' TObject = class',
  22026. ' Id: longint;',
  22027. ' end;',
  22028. ' IEnumerator = interface(IUnknown)',
  22029. ' function GetCurrent: TObject;',
  22030. ' function MoveNext: Boolean;',
  22031. ' property Current: TObject read GetCurrent;',
  22032. ' end;',
  22033. ' IEnumerable = interface(IUnknown)',
  22034. ' function GetEnumerator: IEnumerator;',
  22035. ' end;',
  22036. 'var',
  22037. ' o: TObject;',
  22038. ' i: IEnumerable;',
  22039. 'begin',
  22040. ' for o in i do o.Id:=3;',
  22041. '']);
  22042. ConvertProgram;
  22043. CheckSource('TestClassInterface_COM_ForIn',
  22044. LinesToStr([ // statements
  22045. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22046. 'rtl.createClass(this, "TObject", null, function () {',
  22047. ' this.$init = function () {',
  22048. ' this.Id = 0;',
  22049. ' };',
  22050. ' this.$final = function () {',
  22051. ' };',
  22052. '});',
  22053. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22054. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22055. 'this.o = null;',
  22056. 'this.i = null;',
  22057. '']),
  22058. LinesToStr([ // $mod.$main
  22059. 'var $in = $mod.i.GetEnumerator();',
  22060. 'try {',
  22061. ' while ($in.MoveNext()) {',
  22062. ' $mod.o = $in.GetCurrent();',
  22063. ' $mod.o.Id = 3;',
  22064. ' }',
  22065. '} finally {',
  22066. ' rtl._Release($in)',
  22067. '};',
  22068. '']));
  22069. end;
  22070. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf;
  22071. begin
  22072. {$IFNDEF EnableCOMArrayOfIntf}
  22073. exit;
  22074. {$ENDIF}
  22075. StartProgram(false);
  22076. Add([
  22077. '{$interfaces com}',
  22078. 'type',
  22079. ' IUnknown = interface end;',
  22080. ' IBird = interface(IUnknown)',
  22081. ' function Fly(w: word): word;',
  22082. ' end;',
  22083. ' TBirdArray = array of IBird;',
  22084. 'procedure Run;',
  22085. 'var',
  22086. ' i: IBird;',
  22087. ' a,b: TBirdArray;',
  22088. 'begin',
  22089. //' SetLength(a,3);',
  22090. ' a:=b;',
  22091. ' i:=a[1];',
  22092. ' a[2]:=i;',
  22093. //' for i in a do i.fly(3);',
  22094. // a:=copy(b,1,2);
  22095. // a:=concat(b,a);
  22096. // insert(i,b,1);
  22097. // a:=[i,i];
  22098. 'end;',
  22099. // ToDo: pass TBirdArray as arg
  22100. 'begin',
  22101. '']);
  22102. ConvertProgram;
  22103. CheckSource('TestClassInterface_COM_ArrayOfIntf',
  22104. LinesToStr([ // statements
  22105. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22106. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22107. 'this.Run = function () {',
  22108. ' var i = null;',
  22109. ' var a = [];',
  22110. ' var b = [];',
  22111. ' try {',
  22112. ' a = rtl.arrayRef(b);',
  22113. ' i = rtl.setIntfL(i, a[1]);',
  22114. ' rtl.setIntfP(a, 2, i);',
  22115. ' } finally {',
  22116. ' rtl._Release(i);',
  22117. ' rtl._ReleaseArray(a,1);',
  22118. ' };',
  22119. '};',
  22120. '']),
  22121. LinesToStr([ // $mod.$main
  22122. '']));
  22123. end;
  22124. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  22125. begin
  22126. StartProgram(false);
  22127. Add([
  22128. '{$interfaces com}',
  22129. 'type',
  22130. ' IUnknown = interface',
  22131. ' function _AddRef: longint;',
  22132. ' function _Release: longint;',
  22133. ' end;',
  22134. ' TObject = class',
  22135. ' end;',
  22136. ' TArrOfIntf = array of IUnknown;',
  22137. 'begin',
  22138. '']);
  22139. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  22140. ConvertProgram;
  22141. end;
  22142. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22143. begin
  22144. StartProgram(false);
  22145. Add([
  22146. '{$interfaces com}',
  22147. 'type',
  22148. ' IUnknown = interface',
  22149. ' function _AddRef: longint;',
  22150. ' function _Release: longint;',
  22151. ' end;',
  22152. ' TRec = record',
  22153. ' i: IUnknown;',
  22154. ' end;',
  22155. 'begin',
  22156. '']);
  22157. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22158. ConvertProgram;
  22159. end;
  22160. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22161. begin
  22162. StartUnit(false);
  22163. Add([
  22164. '{$interfaces com}',
  22165. 'interface',
  22166. 'implementation',
  22167. 'type',
  22168. ' IUnknown = interface',
  22169. ' function _AddRef: longint;',
  22170. ' end;',
  22171. ' TObject = class(IUnknown)',
  22172. ' function _AddRef: longint;',
  22173. ' end;',
  22174. 'function TObject._AddRef: longint; begin end;',
  22175. 'var i: IUnknown;',
  22176. ' o: TObject;',
  22177. 'initialization',
  22178. ' i:=nil;',
  22179. ' i:=i;',
  22180. ' i:=o;',
  22181. ' if (o as IUnknown)=nil then ;',
  22182. '']);
  22183. ConvertUnit;
  22184. CheckSource('TestClassInterface_COM_UnitInitialization',
  22185. LinesToStr([ // statements
  22186. 'var $impl = $mod.$impl;',
  22187. '']),
  22188. LinesToStr([ // this.$init
  22189. 'var $ir = rtl.createIntfRefs();',
  22190. 'try {',
  22191. ' rtl.setIntfP($impl, "i", null);',
  22192. ' rtl.setIntfP($impl, "i", $impl.i);',
  22193. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22194. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22195. '} finally {',
  22196. ' $ir.free();',
  22197. '};',
  22198. '']),
  22199. LinesToStr([ // implementation
  22200. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22201. 'rtl.createClass($impl, "TObject", null, function () {',
  22202. ' this.$init = function () {',
  22203. ' };',
  22204. ' this.$final = function () {',
  22205. ' };',
  22206. ' this._AddRef = function () {',
  22207. ' var Result = 0;',
  22208. ' return Result;',
  22209. ' };',
  22210. ' rtl.addIntf(this, $impl.IUnknown);',
  22211. '});',
  22212. '$impl.i = null;',
  22213. '$impl.o = null;',
  22214. ''])
  22215. );
  22216. end;
  22217. procedure TTestModule.TestClassInterface_Corba_GUID;
  22218. begin
  22219. StartProgram(false);
  22220. Add([
  22221. '{$interfaces corba}',
  22222. 'type',
  22223. ' IUnknown = interface',
  22224. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22225. ' end;',
  22226. ' TObject = class end;',
  22227. ' TGUID = record D1, D2, D3, D4: word; end;',
  22228. ' TAliasGUID = TGUID;',
  22229. ' TGUIDString = type string;',
  22230. ' TAliasGUIDString = TGUIDString;',
  22231. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  22232. 'begin end;',
  22233. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  22234. 'begin end;',
  22235. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  22236. 'begin end;',
  22237. 'var',
  22238. ' i: IUnknown;',
  22239. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  22240. ' s: TAliasGUIDString;',
  22241. 'begin',
  22242. ' DoConstGUIDIt(IUnknown);',
  22243. ' DoDefGUID(IUnknown);',
  22244. ' DoStr(IUnknown);',
  22245. ' DoConstGUIDIt(i);',
  22246. ' DoDefGUID(i);',
  22247. ' DoStr(i);',
  22248. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22249. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22250. ' DoStr(g);',
  22251. ' g:=i;',
  22252. ' g:=IUnknown;',
  22253. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22254. ' s:=i;',
  22255. ' s:=IUnknown;',
  22256. ' s:=g;',
  22257. ' if g=i then ;',
  22258. ' if i=g then ;',
  22259. ' if g=IUnknown then ;',
  22260. ' if IUnknown=g then ;',
  22261. ' if s=i then ;',
  22262. ' if i=s then ;',
  22263. ' if s=IUnknown then ;',
  22264. ' if IUnknown=s then ;',
  22265. ' if s=g then ;',
  22266. ' if g=s then ;',
  22267. '']);
  22268. ConvertProgram;
  22269. CheckSource('TestClassInterface_Corba_GUID',
  22270. LinesToStr([ // statements
  22271. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22272. 'rtl.createClass(this, "TObject", null, function () {',
  22273. ' this.$init = function () {',
  22274. ' };',
  22275. ' this.$final = function () {',
  22276. ' };',
  22277. '});',
  22278. 'rtl.recNewT(this, "TGUID", function () {',
  22279. ' this.D1 = 0;',
  22280. ' this.D2 = 0;',
  22281. ' this.D3 = 0;',
  22282. ' this.D4 = 0;',
  22283. ' this.$eq = function (b) {',
  22284. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22285. ' };',
  22286. ' this.$assign = function (s) {',
  22287. ' this.D1 = s.D1;',
  22288. ' this.D2 = s.D2;',
  22289. ' this.D3 = s.D3;',
  22290. ' this.D4 = s.D4;',
  22291. ' return this;',
  22292. ' };',
  22293. '});',
  22294. 'this.DoConstGUIDIt = function (g) {',
  22295. '};',
  22296. 'this.DoDefGUID = function (g) {',
  22297. '};',
  22298. 'this.DoStr = function (s) {',
  22299. '};',
  22300. 'this.i = null;',
  22301. 'this.g = this.TGUID.$clone({',
  22302. ' D1: 0xD91C9AF4,',
  22303. ' D2: 0x3C93,',
  22304. ' D3: 0x420F,',
  22305. ' D4: [',
  22306. ' 0xA3,',
  22307. ' 0x03,',
  22308. ' 0xBF,',
  22309. ' 0x5B,',
  22310. ' 0xA8,',
  22311. ' 0x2B,',
  22312. ' 0xFD,',
  22313. ' 0x23',
  22314. ' ]',
  22315. '});',
  22316. 'this.s = "";',
  22317. '']),
  22318. LinesToStr([ // $mod.$main
  22319. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  22320. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  22321. '$mod.DoStr($mod.IUnknown.$guid);',
  22322. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  22323. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  22324. '$mod.DoStr($mod.i.$guid);',
  22325. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22326. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22327. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  22328. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  22329. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  22330. '$mod.g.$assign({',
  22331. ' D1: 0xD91C9AF4,',
  22332. ' D2: 0x3C93,',
  22333. ' D3: 0x420F,',
  22334. ' D4: [',
  22335. ' 0xA3,',
  22336. ' 0x03,',
  22337. ' 0xBF,',
  22338. ' 0x5B,',
  22339. ' 0xA8,',
  22340. ' 0x2B,',
  22341. ' 0xFD,',
  22342. ' 0x23',
  22343. ' ]',
  22344. '});',
  22345. '$mod.s = $mod.i.$guid;',
  22346. '$mod.s = $mod.IUnknown.$guid;',
  22347. '$mod.s = rtl.guidrToStr($mod.g);',
  22348. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22349. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22350. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22351. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22352. 'if ($mod.s === $mod.i.$guid) ;',
  22353. 'if ($mod.i.$guid === $mod.s) ;',
  22354. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22355. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22356. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22357. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22358. '']));
  22359. end;
  22360. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  22361. begin
  22362. StartProgram(false);
  22363. Add([
  22364. '{$interfaces corba}',
  22365. 'type',
  22366. ' IUnknown = interface',
  22367. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22368. ' end;',
  22369. ' TGUID = record D1, D2, D3, D4: word; end;',
  22370. ' TAliasGUID = TGUID;',
  22371. ' TGUIDString = type string;',
  22372. ' TAliasGUIDString = TGUIDString;',
  22373. ' TObject = class',
  22374. ' function GetG: TAliasGUID; virtual; abstract;',
  22375. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22376. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22377. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22378. ' property g: TAliasGUID read GetG write SetG;',
  22379. ' property s: TAliasGUIDString read GetS write SetS;',
  22380. ' end;',
  22381. 'var o: TObject;',
  22382. 'begin',
  22383. ' o.g:=IUnknown;',
  22384. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22385. ' o.s:=IUnknown;',
  22386. ' o.s:=o.g;',
  22387. '']);
  22388. ConvertProgram;
  22389. CheckSource('TestClassInterface_GUIDProperty',
  22390. LinesToStr([ // statements
  22391. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22392. 'rtl.recNewT(this, "TGUID", function () {',
  22393. ' this.D1 = 0;',
  22394. ' this.D2 = 0;',
  22395. ' this.D3 = 0;',
  22396. ' this.D4 = 0;',
  22397. ' this.$eq = function (b) {',
  22398. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22399. ' };',
  22400. ' this.$assign = function (s) {',
  22401. ' this.D1 = s.D1;',
  22402. ' this.D2 = s.D2;',
  22403. ' this.D3 = s.D3;',
  22404. ' this.D4 = s.D4;',
  22405. ' return this;',
  22406. ' };',
  22407. '});',
  22408. 'rtl.createClass(this, "TObject", null, function () {',
  22409. ' this.$init = function () {',
  22410. ' };',
  22411. ' this.$final = function () {',
  22412. ' };',
  22413. '});',
  22414. 'this.o = null;',
  22415. '']),
  22416. LinesToStr([ // $mod.$main
  22417. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22418. '$mod.o.SetG({',
  22419. ' D1: 0xD91C9AF4,',
  22420. ' D2: 0x3C93,',
  22421. ' D3: 0x420F,',
  22422. ' D4: [',
  22423. ' 0xA3,',
  22424. ' 0x03,',
  22425. ' 0xBF,',
  22426. ' 0x5B,',
  22427. ' 0xA8,',
  22428. ' 0x2B,',
  22429. ' 0xFD,',
  22430. ' 0x23',
  22431. ' ]',
  22432. '});',
  22433. '$mod.o.SetS($mod.IUnknown.$guid);',
  22434. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22435. '']));
  22436. end;
  22437. procedure TTestModule.TestClassHelper_ClassVar;
  22438. begin
  22439. StartProgram(false);
  22440. Add([
  22441. 'type',
  22442. ' TObject = class',
  22443. ' end;',
  22444. ' THelper = class helper for TObject',
  22445. ' const',
  22446. ' One = 1;',
  22447. ' Two: word = 2;',
  22448. ' class var',
  22449. ' Glob: word;',
  22450. ' function Foo(w: word): word;',
  22451. ' class function Bar(w: word): word;',
  22452. ' end;',
  22453. 'function THelper.foo(w: word): word;',
  22454. 'begin',
  22455. ' Result:=w;',
  22456. ' Two:=One+w;',
  22457. ' Glob:=Glob;',
  22458. ' Result:=Self.Glob;',
  22459. ' Self.Glob:=Self.Glob;',
  22460. ' with Self do Glob:=Glob;',
  22461. 'end;',
  22462. 'class function THelper.bar(w: word): word;',
  22463. 'begin',
  22464. ' Result:=w;',
  22465. ' Two:=One;',
  22466. ' Glob:=Glob;',
  22467. ' Self.Glob:=Self.Glob;',
  22468. ' with Self do Glob:=Glob;',
  22469. 'end;',
  22470. 'var o: TObject;',
  22471. 'begin',
  22472. ' tobject.two:=tobject.one;',
  22473. ' tobject.Glob:=tobject.Glob;',
  22474. ' with tobject do begin',
  22475. ' two:=one;',
  22476. ' Glob:=Glob;',
  22477. ' end;',
  22478. ' o.two:=o.one;',
  22479. ' o.Glob:=o.Glob;',
  22480. ' with o do begin',
  22481. ' two:=one;',
  22482. ' Glob:=Glob;',
  22483. ' end;',
  22484. '']);
  22485. ConvertProgram;
  22486. CheckSource('TestClassHelper_ClassVar',
  22487. LinesToStr([ // statements
  22488. 'rtl.createClass(this, "TObject", null, function () {',
  22489. ' this.$init = function () {',
  22490. ' };',
  22491. ' this.$final = function () {',
  22492. ' };',
  22493. '});',
  22494. 'rtl.createHelper(this, "THelper", null, function () {',
  22495. ' this.One = 1;',
  22496. ' this.Two = 2;',
  22497. ' this.Glob = 0;',
  22498. ' this.Foo = function (w) {',
  22499. ' var Result = 0;',
  22500. ' Result = w;',
  22501. ' $mod.THelper.Two = 1 + w;',
  22502. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22503. ' Result = $mod.THelper.Glob;',
  22504. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22505. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22506. ' return Result;',
  22507. ' };',
  22508. ' this.Bar = function (w) {',
  22509. ' var Result = 0;',
  22510. ' Result = w;',
  22511. ' $mod.THelper.Two = 1;',
  22512. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22513. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22514. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22515. ' return Result;',
  22516. ' };',
  22517. '});',
  22518. 'this.o = null;',
  22519. '']),
  22520. LinesToStr([ // $mod.$main
  22521. '$mod.THelper.Two = 1;',
  22522. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22523. 'var $with = $mod.TObject;',
  22524. '$mod.THelper.Two = 1;',
  22525. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22526. '$mod.THelper.Two = 1;',
  22527. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22528. 'var $with1 = $mod.o;',
  22529. '$mod.THelper.Two = 1;',
  22530. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22531. '']));
  22532. end;
  22533. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22534. begin
  22535. StartProgram(false);
  22536. Add([
  22537. 'type',
  22538. ' TObject = class',
  22539. ' FSize: word;',
  22540. ' property Size: word read FSize write FSize;',
  22541. ' end;',
  22542. ' THelper = class helper for TObject',
  22543. ' function Foo(w: word = 1): word;',
  22544. ' end;',
  22545. 'function THelper.foo(w: word): word;',
  22546. 'begin',
  22547. ' Result:=Size;',
  22548. ' Size:=Size+2;',
  22549. ' Self.Size:=Self.Size+3;',
  22550. ' FSize:=FSize+4;',
  22551. ' Self.FSize:=Self.FSize+5;',
  22552. ' with Self do begin',
  22553. ' Size:=Size+6;',
  22554. ' FSize:=FSize+7;',
  22555. ' FSize:=FSize+8;',
  22556. ' end;',
  22557. 'end;',
  22558. 'begin',
  22559. '']);
  22560. ConvertProgram;
  22561. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22562. LinesToStr([ // statements
  22563. 'rtl.createClass(this, "TObject", null, function () {',
  22564. ' this.$init = function () {',
  22565. ' this.FSize = 0;',
  22566. ' };',
  22567. ' this.$final = function () {',
  22568. ' };',
  22569. '});',
  22570. 'rtl.createHelper(this, "THelper", null, function () {',
  22571. ' this.Foo = function (w) {',
  22572. ' var Result = 0;',
  22573. ' Result = this.FSize;',
  22574. ' this.FSize = this.FSize + 2;',
  22575. ' this.FSize = this.FSize + 3;',
  22576. ' this.FSize = this.FSize + 4;',
  22577. ' this.FSize = this.FSize + 5;',
  22578. ' this.FSize = this.FSize + 6;',
  22579. ' this.FSize = this.FSize + 7;',
  22580. ' this.FSize = this.FSize + 8;',
  22581. ' return Result;',
  22582. ' };',
  22583. '});',
  22584. '']),
  22585. LinesToStr([ // $mod.$main
  22586. '']));
  22587. end;
  22588. procedure TTestModule.TestClassHelper_Method_Call;
  22589. begin
  22590. StartProgram(false);
  22591. Add([
  22592. 'type',
  22593. ' TObject = class',
  22594. ' procedure Run(w: word = 10);',
  22595. ' end;',
  22596. ' THelper = class helper for TObject',
  22597. ' function Foo(w: word = 1): word;',
  22598. ' end;',
  22599. 'procedure TObject.Run(w: word);',
  22600. 'var o: TObject;',
  22601. 'begin',
  22602. ' Foo;',
  22603. ' Foo();',
  22604. ' Foo(2);',
  22605. ' Self.Foo;',
  22606. ' Self.Foo();',
  22607. ' Self.Foo(3);',
  22608. ' with Self do begin',
  22609. ' Foo;',
  22610. ' Foo();',
  22611. ' Foo(4);',
  22612. ' end;',
  22613. ' with o do Foo(5);',
  22614. 'end;',
  22615. 'function THelper.foo(w: word): word;',
  22616. 'begin',
  22617. ' Run;',
  22618. ' Run();',
  22619. ' Run(11);',
  22620. ' Foo;',
  22621. ' Foo();',
  22622. ' Foo(12);',
  22623. ' Self.Foo;',
  22624. ' Self.Foo();',
  22625. ' Self.Foo(13);',
  22626. ' with Self do begin',
  22627. ' Foo;',
  22628. ' Foo();',
  22629. ' Foo(14);',
  22630. ' end;',
  22631. 'end;',
  22632. 'var Obj: TObject;',
  22633. 'begin',
  22634. ' obj.Foo;',
  22635. ' obj.Foo();',
  22636. ' obj.Foo(21);',
  22637. ' with obj do begin',
  22638. ' Foo;',
  22639. ' Foo();',
  22640. ' Foo(22);',
  22641. ' end;',
  22642. '']);
  22643. ConvertProgram;
  22644. CheckSource('TestClassHelper_Method_Call',
  22645. LinesToStr([ // statements
  22646. 'rtl.createClass(this, "TObject", null, function () {',
  22647. ' this.$init = function () {',
  22648. ' };',
  22649. ' this.$final = function () {',
  22650. ' };',
  22651. ' this.Run = function (w) {',
  22652. ' var o = null;',
  22653. ' $mod.THelper.Foo.call(this, 1);',
  22654. ' $mod.THelper.Foo.call(this, 1);',
  22655. ' $mod.THelper.Foo.call(this, 2);',
  22656. ' $mod.THelper.Foo.call(this, 1);',
  22657. ' $mod.THelper.Foo.call(this, 1);',
  22658. ' $mod.THelper.Foo.call(this, 3);',
  22659. ' $mod.THelper.Foo.call(this, 1);',
  22660. ' $mod.THelper.Foo.call(this, 1);',
  22661. ' $mod.THelper.Foo.call(this, 4);',
  22662. ' $mod.THelper.Foo.call(o, 5);',
  22663. ' };',
  22664. '});',
  22665. 'rtl.createHelper(this, "THelper", null, function () {',
  22666. ' this.Foo = function (w) {',
  22667. ' var Result = 0;',
  22668. ' this.Run(10);',
  22669. ' this.Run(10);',
  22670. ' this.Run(11);',
  22671. ' $mod.THelper.Foo.call(this, 1);',
  22672. ' $mod.THelper.Foo.call(this, 1);',
  22673. ' $mod.THelper.Foo.call(this, 12);',
  22674. ' $mod.THelper.Foo.call(this, 1);',
  22675. ' $mod.THelper.Foo.call(this, 1);',
  22676. ' $mod.THelper.Foo.call(this, 13);',
  22677. ' $mod.THelper.Foo.call(this, 1);',
  22678. ' $mod.THelper.Foo.call(this, 1);',
  22679. ' $mod.THelper.Foo.call(this, 14);',
  22680. ' return Result;',
  22681. ' };',
  22682. '});',
  22683. 'this.Obj = null;',
  22684. '']),
  22685. LinesToStr([ // $mod.$main
  22686. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22687. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22688. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22689. 'var $with = $mod.Obj;',
  22690. '$mod.THelper.Foo.call($with, 1);',
  22691. '$mod.THelper.Foo.call($with, 1);',
  22692. '$mod.THelper.Foo.call($with, 22);',
  22693. '']));
  22694. end;
  22695. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22696. begin
  22697. StartProgram(false);
  22698. Add([
  22699. 'type',
  22700. ' TObject = class',
  22701. ' procedure Run(w: word = 10);',
  22702. ' end;',
  22703. ' THelper = class helper for TObject',
  22704. ' function Foo(w: word = 1): word;',
  22705. ' end;',
  22706. 'procedure TObject.Run(w: word);',
  22707. ' procedure Sub(Self: TObject);',
  22708. ' begin',
  22709. ' Foo;',
  22710. ' Foo();',
  22711. ' Self.Foo;',
  22712. ' Self.Foo();',
  22713. ' with Self do begin',
  22714. ' Foo;',
  22715. ' Foo();',
  22716. ' end;',
  22717. ' end;',
  22718. 'begin',
  22719. 'end;',
  22720. 'function THelper.foo(w: word): word;',
  22721. ' procedure Sub(Self: TObject);',
  22722. ' begin',
  22723. ' Run;',
  22724. ' Run();',
  22725. ' Foo;',
  22726. ' Foo();',
  22727. ' Self.Foo;',
  22728. ' Self.Foo();',
  22729. ' with Self do begin',
  22730. ' Foo;',
  22731. ' Foo();',
  22732. ' end;',
  22733. ' end;',
  22734. 'begin',
  22735. 'end;',
  22736. 'begin',
  22737. '']);
  22738. ConvertProgram;
  22739. CheckSource('TestClassHelper_Method_Nested_Call',
  22740. LinesToStr([ // statements
  22741. 'rtl.createClass(this, "TObject", null, function () {',
  22742. ' this.$init = function () {',
  22743. ' };',
  22744. ' this.$final = function () {',
  22745. ' };',
  22746. ' this.Run = function (w) {',
  22747. ' var $Self = this;',
  22748. ' function Sub(Self) {',
  22749. ' $mod.THelper.Foo.call($Self, 1);',
  22750. ' $mod.THelper.Foo.call($Self, 1);',
  22751. ' $mod.THelper.Foo.call(Self, 1);',
  22752. ' $mod.THelper.Foo.call(Self, 1);',
  22753. ' $mod.THelper.Foo.call(Self, 1);',
  22754. ' $mod.THelper.Foo.call(Self, 1);',
  22755. ' };',
  22756. ' };',
  22757. '});',
  22758. 'rtl.createHelper(this, "THelper", null, function () {',
  22759. ' this.Foo = function (w) {',
  22760. ' var $Self = this;',
  22761. ' var Result = 0;',
  22762. ' function Sub(Self) {',
  22763. ' $Self.Run(10);',
  22764. ' $Self.Run(10);',
  22765. ' $mod.THelper.Foo.call($Self, 1);',
  22766. ' $mod.THelper.Foo.call($Self, 1);',
  22767. ' $mod.THelper.Foo.call(Self, 1);',
  22768. ' $mod.THelper.Foo.call(Self, 1);',
  22769. ' $mod.THelper.Foo.call(Self, 1);',
  22770. ' $mod.THelper.Foo.call(Self, 1);',
  22771. ' };',
  22772. ' return Result;',
  22773. ' };',
  22774. '});',
  22775. '']),
  22776. LinesToStr([ // $mod.$main
  22777. '']));
  22778. end;
  22779. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22780. begin
  22781. StartProgram(false);
  22782. Add([
  22783. 'type',
  22784. ' TObject = class',
  22785. ' class procedure Run(w: word = 10);',
  22786. ' end;',
  22787. ' THelper = class helper for TObject',
  22788. ' class function Foo(w: word = 1): word;',
  22789. ' end;',
  22790. 'class procedure TObject.Run(w: word);',
  22791. 'begin',
  22792. ' Foo;',
  22793. ' Foo();',
  22794. ' Self.Foo;',
  22795. ' Self.Foo();',
  22796. ' with Self do begin',
  22797. ' Foo;',
  22798. ' Foo();',
  22799. ' end;',
  22800. 'end;',
  22801. 'class function THelper.foo(w: word): word;',
  22802. 'begin',
  22803. ' Run;',
  22804. ' Run();',
  22805. ' Foo;',
  22806. ' Foo();',
  22807. ' Self.Foo;',
  22808. ' Self.Foo();',
  22809. ' with Self do begin',
  22810. ' Foo;',
  22811. ' Foo();',
  22812. ' end;',
  22813. 'end;',
  22814. 'var',
  22815. ' Obj: TObject;',
  22816. 'begin',
  22817. ' obj.Foo;',
  22818. ' obj.Foo();',
  22819. ' with obj do begin',
  22820. ' Foo;',
  22821. ' Foo();',
  22822. ' end;',
  22823. ' tobject.Foo;',
  22824. ' tobject.Foo();',
  22825. ' with tobject do begin',
  22826. ' Foo;',
  22827. ' Foo();',
  22828. ' end;',
  22829. '']);
  22830. ConvertProgram;
  22831. CheckSource('TestClassHelper_ClassMethod_Call',
  22832. LinesToStr([ // statements
  22833. 'rtl.createClass(this, "TObject", null, function () {',
  22834. ' this.$init = function () {',
  22835. ' };',
  22836. ' this.$final = function () {',
  22837. ' };',
  22838. ' this.Run = function (w) {',
  22839. ' $mod.THelper.Foo.call(this, 1);',
  22840. ' $mod.THelper.Foo.call(this, 1);',
  22841. ' $mod.THelper.Foo.call(this, 1);',
  22842. ' $mod.THelper.Foo.call(this, 1);',
  22843. ' $mod.THelper.Foo.call(this, 1);',
  22844. ' $mod.THelper.Foo.call(this, 1);',
  22845. ' };',
  22846. '});',
  22847. 'rtl.createHelper(this, "THelper", null, function () {',
  22848. ' this.Foo = function (w) {',
  22849. ' var Result = 0;',
  22850. ' this.Run(10);',
  22851. ' this.Run(10);',
  22852. ' $mod.THelper.Foo.call(this, 1);',
  22853. ' $mod.THelper.Foo.call(this, 1);',
  22854. ' $mod.THelper.Foo.call(this, 1);',
  22855. ' $mod.THelper.Foo.call(this, 1);',
  22856. ' $mod.THelper.Foo.call(this, 1);',
  22857. ' $mod.THelper.Foo.call(this, 1);',
  22858. ' return Result;',
  22859. ' };',
  22860. '});',
  22861. 'this.Obj = null;',
  22862. '']),
  22863. LinesToStr([ // $mod.$main
  22864. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22865. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22866. 'var $with = $mod.Obj;',
  22867. '$mod.THelper.Foo.call($with.$class, 1);',
  22868. '$mod.THelper.Foo.call($with.$class, 1);',
  22869. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22870. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22871. 'var $with1 = $mod.TObject;',
  22872. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22873. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22874. '']));
  22875. end;
  22876. procedure TTestModule.TestClassHelper_ClassOf;
  22877. begin
  22878. StartProgram(false);
  22879. Add([
  22880. 'type',
  22881. ' TObject = class',
  22882. ' end;',
  22883. ' TClass = class of TObject;',
  22884. ' THelper = class helper for TObject',
  22885. ' class function Foo(w: word = 1): word;',
  22886. ' end;',
  22887. 'class function THelper.foo(w: word): word;',
  22888. 'begin',
  22889. 'end;',
  22890. 'var',
  22891. ' c: TClass;',
  22892. 'begin',
  22893. ' c.Foo;',
  22894. ' c.Foo();',
  22895. ' with c do begin',
  22896. ' Foo;',
  22897. ' Foo();',
  22898. ' end;',
  22899. '']);
  22900. ConvertProgram;
  22901. CheckSource('TestClassHelper_ClassOf',
  22902. LinesToStr([ // statements
  22903. 'rtl.createClass(this, "TObject", null, function () {',
  22904. ' this.$init = function () {',
  22905. ' };',
  22906. ' this.$final = function () {',
  22907. ' };',
  22908. '});',
  22909. 'rtl.createHelper(this, "THelper", null, function () {',
  22910. ' this.Foo = function (w) {',
  22911. ' var Result = 0;',
  22912. ' return Result;',
  22913. ' };',
  22914. '});',
  22915. 'this.c = null;',
  22916. '']),
  22917. LinesToStr([ // $mod.$main
  22918. '$mod.THelper.Foo.call($mod.c, 1);',
  22919. '$mod.THelper.Foo.call($mod.c, 1);',
  22920. 'var $with = $mod.c;',
  22921. '$mod.THelper.Foo.call($with, 1);',
  22922. '$mod.THelper.Foo.call($with, 1);',
  22923. '']));
  22924. end;
  22925. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22926. begin
  22927. StartProgram(false);
  22928. Add([
  22929. '{$mode objfpc}',
  22930. 'type',
  22931. ' TObject = class',
  22932. ' procedure DoIt;',
  22933. ' end;',
  22934. ' THelper = class helper for TObject',
  22935. ' procedure Fly(w: word = 1);',
  22936. ' class procedure Glide(w: word = 1);',
  22937. ' class procedure Run(w: word = 1); static;',
  22938. ' end;',
  22939. ' TFly = procedure(w: word) of object;',
  22940. ' TGlide = TFly;',
  22941. ' TRun = procedure(w: word);',
  22942. 'var',
  22943. ' f: TFly;',
  22944. ' g: TGlide;',
  22945. ' r: TRun;',
  22946. 'procedure TObject.DoIt;',
  22947. 'begin',
  22948. ' f:=@fly;',
  22949. ' g:=@glide;',
  22950. ' r:=@run;',
  22951. ' f:[email protected];',
  22952. ' g:[email protected];',
  22953. ' r:[email protected];',
  22954. ' with self do begin',
  22955. ' f:=@fly;',
  22956. ' g:=@glide;',
  22957. ' r:=@run;',
  22958. ' end;',
  22959. 'end;',
  22960. 'procedure THelper.fly(w: word);',
  22961. 'begin',
  22962. ' f:=@fly;',
  22963. ' g:=@glide;',
  22964. ' r:=@run;',
  22965. 'end;',
  22966. 'class procedure THelper.glide(w: word);',
  22967. 'begin',
  22968. ' g:=@glide;',
  22969. ' r:=@run;',
  22970. 'end;',
  22971. 'class procedure THelper.run(w: word);',
  22972. 'begin',
  22973. ' g:=@glide;',
  22974. ' r:=@run;',
  22975. 'end;',
  22976. 'var',
  22977. ' Obj: TObject;',
  22978. 'begin',
  22979. ' f:[email protected];',
  22980. ' g:[email protected];',
  22981. ' r:[email protected];',
  22982. ' with obj do begin',
  22983. ' f:=@fly;',
  22984. ' g:=@glide;',
  22985. ' r:=@run;',
  22986. ' end;',
  22987. ' g:[email protected];',
  22988. ' r:[email protected];',
  22989. ' with tobject do begin',
  22990. ' g:=@glide;',
  22991. ' r:=@run;',
  22992. ' end;',
  22993. '']);
  22994. ConvertProgram;
  22995. CheckSource('TestClassHelper_MethodRefObjFPC',
  22996. LinesToStr([ // statements
  22997. 'rtl.createClass(this, "TObject", null, function () {',
  22998. ' this.$init = function () {',
  22999. ' };',
  23000. ' this.$final = function () {',
  23001. ' };',
  23002. ' this.DoIt = function () {',
  23003. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23004. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23005. ' $mod.r = $mod.THelper.Run;',
  23006. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23007. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23008. ' $mod.r = $mod.THelper.Run;',
  23009. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23010. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23011. ' $mod.r = $mod.THelper.Run;',
  23012. ' };',
  23013. '});',
  23014. 'rtl.createHelper(this, "THelper", null, function () {',
  23015. ' this.Fly = function (w) {',
  23016. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23017. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23018. ' $mod.r = $mod.THelper.Run;',
  23019. ' };',
  23020. ' this.Glide = function (w) {',
  23021. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23022. ' $mod.r = $mod.THelper.Run;',
  23023. ' };',
  23024. ' this.Run = function (w) {',
  23025. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23026. ' $mod.r = $mod.THelper.Run;',
  23027. ' };',
  23028. '});',
  23029. 'this.f = null;',
  23030. 'this.g = null;',
  23031. 'this.r = null;',
  23032. 'this.Obj = null;',
  23033. '']),
  23034. LinesToStr([ // $mod.$main
  23035. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23036. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23037. '$mod.r = $mod.THelper.Run;',
  23038. 'var $with = $mod.Obj;',
  23039. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23040. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23041. '$mod.r = $mod.THelper.Run;',
  23042. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23043. '$mod.r = $mod.THelper.Run;',
  23044. 'var $with1 = $mod.TObject;',
  23045. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23046. '$mod.r = $mod.THelper.Run;',
  23047. '']));
  23048. end;
  23049. procedure TTestModule.TestClassHelper_Constructor;
  23050. begin
  23051. StartProgram(false);
  23052. Add([
  23053. 'type',
  23054. ' TObject = class',
  23055. ' constructor Create;',
  23056. ' end;',
  23057. ' TClass = class of TObject;',
  23058. ' THelper = class helper for TObject',
  23059. ' constructor NewHlp(w: word);',
  23060. ' end;',
  23061. 'var',
  23062. ' obj: TObject;',
  23063. ' c: TClass;',
  23064. 'constructor TObject.Create;',
  23065. 'begin',
  23066. ' NewHlp(2);', // normal call
  23067. ' tobject.NewHlp(3);', // new instance
  23068. ' c.newhlp(4);', // new instance
  23069. 'end;',
  23070. 'constructor THelper.NewHlp(w: word);',
  23071. 'begin',
  23072. ' create;', // normal call
  23073. ' tobject.create;', // new instance
  23074. ' NewHlp(2);', // normal call
  23075. ' tobject.NewHlp(3);', // new instance
  23076. ' c.newhlp(4);', // new instance
  23077. 'end;',
  23078. 'begin',
  23079. ' obj.newhlp(2);', // normal call
  23080. ' with Obj do newhlp(12);', // normal call
  23081. ' tobject.newhlp(3);', // new instance
  23082. ' with tobject do newhlp(13);', // new instance
  23083. ' c.newhlp(4);', // new instance
  23084. ' with c do newhlp(14);', // new instance
  23085. '']);
  23086. ConvertProgram;
  23087. CheckSource('TestClassHelper_Constructor',
  23088. LinesToStr([ // statements
  23089. 'rtl.createClass(this, "TObject", null, function () {',
  23090. ' this.$init = function () {',
  23091. ' };',
  23092. ' this.$final = function () {',
  23093. ' };',
  23094. ' this.Create = function () {',
  23095. ' $mod.THelper.NewHlp.call(this, 2);',
  23096. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23097. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23098. ' return this;',
  23099. ' };',
  23100. '});',
  23101. 'rtl.createHelper(this, "THelper", null, function () {',
  23102. ' this.NewHlp = function (w) {',
  23103. ' this.Create();',
  23104. ' $mod.TObject.$create("Create");',
  23105. ' $mod.THelper.NewHlp.call(this, 2);',
  23106. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23107. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23108. ' return this;',
  23109. ' };',
  23110. '});',
  23111. 'this.obj = null;',
  23112. 'this.c = null;',
  23113. '']),
  23114. LinesToStr([ // $mod.$main
  23115. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23116. 'var $with = $mod.obj;',
  23117. '$mod.THelper.NewHlp.call($with, 12);',
  23118. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23119. 'var $with1 = $mod.TObject;',
  23120. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23121. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23122. 'var $with2 = $mod.c;',
  23123. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23124. '']));
  23125. end;
  23126. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23127. begin
  23128. StartProgram(false);
  23129. Add([
  23130. 'type',
  23131. ' TObject = class',
  23132. ' procedure Fly;',
  23133. ' end;',
  23134. ' TObjHelper = class helper for TObject',
  23135. ' procedure Fly;',
  23136. ' end;',
  23137. ' TBird = class',
  23138. ' procedure Fly;',
  23139. ' end;',
  23140. ' TBirdHelper = class helper for TBird',
  23141. ' procedure Fly;',
  23142. ' procedure Walk(w: word);',
  23143. ' end;',
  23144. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23145. ' procedure Fly;',
  23146. ' procedure Walk(w: word);',
  23147. ' end;',
  23148. 'procedure Tobject.fly;',
  23149. 'begin',
  23150. ' inherited;', // ignore
  23151. 'end;',
  23152. 'procedure Tobjhelper.fly;',
  23153. 'begin',
  23154. ' {@TObject_Fly}inherited;',
  23155. ' inherited {@TObject_Fly}Fly;',
  23156. 'end;',
  23157. 'procedure Tbird.fly;',
  23158. 'begin',
  23159. ' {@TObjHelper_Fly}inherited;',
  23160. ' inherited {@TObjHelper_Fly}Fly;',
  23161. 'end;',
  23162. 'procedure Tbirdhelper.fly;',
  23163. 'begin',
  23164. ' {@TBird_Fly}inherited;',
  23165. ' inherited {@TBird_Fly}Fly;',
  23166. 'end;',
  23167. 'procedure Tbirdhelper.walk(w: word);',
  23168. 'begin',
  23169. 'end;',
  23170. 'procedure teagleHelper.fly;',
  23171. 'begin',
  23172. ' {@TBird_Fly}inherited;',
  23173. ' inherited {@TBird_Fly}Fly;',
  23174. 'end;',
  23175. 'procedure teagleHelper.walk(w: word);',
  23176. 'begin',
  23177. ' {@TBirdHelper_Walk}inherited;',
  23178. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23179. 'end;',
  23180. 'begin',
  23181. '']);
  23182. ConvertProgram;
  23183. CheckSource('TestClassHelper_InheritedObjFPC',
  23184. LinesToStr([ // statements
  23185. 'rtl.createClass(this, "TObject", null, function () {',
  23186. ' this.$init = function () {',
  23187. ' };',
  23188. ' this.$final = function () {',
  23189. ' };',
  23190. ' this.Fly = function () {',
  23191. ' };',
  23192. '});',
  23193. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23194. ' this.Fly = function () {',
  23195. ' $mod.TObject.Fly.call(this);',
  23196. ' $mod.TObject.Fly.call(this);',
  23197. ' };',
  23198. '});',
  23199. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23200. ' this.Fly$1 = function () {',
  23201. ' $mod.TObjHelper.Fly.call(this);',
  23202. ' $mod.TObjHelper.Fly.call(this);',
  23203. ' };',
  23204. '});',
  23205. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23206. ' this.Fly = function () {',
  23207. ' $mod.TBird.Fly$1.call(this);',
  23208. ' $mod.TBird.Fly$1.call(this);',
  23209. ' };',
  23210. ' this.Walk = function (w) {',
  23211. ' };',
  23212. '});',
  23213. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23214. ' this.Fly$1 = function () {',
  23215. ' $mod.TBird.Fly$1.call(this);',
  23216. ' $mod.TBird.Fly$1.call(this);',
  23217. ' };',
  23218. ' this.Walk$1 = function (w) {',
  23219. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  23220. ' $mod.TBirdHelper.Walk.call(this, 3);',
  23221. ' };',
  23222. '});',
  23223. '']),
  23224. LinesToStr([ // $mod.$main
  23225. '']));
  23226. end;
  23227. procedure TTestModule.TestClassHelper_Property;
  23228. begin
  23229. StartProgram(false);
  23230. Add([
  23231. 'type',
  23232. ' TObject = class',
  23233. ' FSize: word;',
  23234. ' function GetSpeed: word;',
  23235. ' procedure SetSpeed(Value: word);',
  23236. ' end;',
  23237. ' TObjHelper = class helper for TObject',
  23238. ' function GetLeft: word;',
  23239. ' procedure SetLeft(Value: word);',
  23240. ' property Size: word read FSize write FSize;',
  23241. ' property Speed: word read GetSpeed write SetSpeed;',
  23242. ' property Left: word read GetLeft write SetLeft;',
  23243. ' end;',
  23244. ' TBird = class',
  23245. ' property NotRight: word read GetLeft write SetLeft;',
  23246. ' procedure DoIt;',
  23247. ' end;',
  23248. 'var',
  23249. ' b: TBird;',
  23250. 'function Tobject.GetSpeed: word;',
  23251. 'begin',
  23252. ' Size:=Size+11;',
  23253. ' Speed:=Speed+12;',
  23254. ' Result:=Left+13;',
  23255. ' Left:=13;',
  23256. ' Left:=Left+13;',
  23257. ' Self.Size:=Self.Size+21;',
  23258. ' Self.Speed:=Self.Speed+22;',
  23259. ' Self.Left:=Self.Left+23;',
  23260. ' with Self do begin',
  23261. ' Size:=Size+31;',
  23262. ' Speed:=Speed+32;',
  23263. ' Left:=Left+33;',
  23264. ' end;',
  23265. 'end;',
  23266. 'procedure Tobject.SetSpeed(Value: word);',
  23267. 'begin',
  23268. 'end;',
  23269. 'function TObjHelper.GetLeft: word;',
  23270. 'begin',
  23271. ' Size:=Size+11;',
  23272. ' Speed:=Speed+12;',
  23273. ' Left:=Left+13;',
  23274. ' Self.Size:=Self.Size+21;',
  23275. ' Self.Speed:=Self.Speed+22;',
  23276. ' Self.Left:=Self.Left+23;',
  23277. ' with Self do begin',
  23278. ' Size:=Size+31;',
  23279. ' Speed:=Speed+32;',
  23280. ' Left:=Left+33;',
  23281. ' end;',
  23282. 'end;',
  23283. 'procedure TObjHelper.SetLeft(Value: word);',
  23284. 'begin',
  23285. 'end;',
  23286. 'procedure TBird.DoIt;',
  23287. 'begin',
  23288. ' NotRight:=NotRight+11;',
  23289. ' Self.NotRight:=Self.NotRight+21;',
  23290. ' with Self do begin',
  23291. ' NotRight:=NotRight+31;',
  23292. ' end;',
  23293. 'end;',
  23294. 'begin',
  23295. ' b.Size:=b.Size+11;',
  23296. ' b.Speed:=b.Speed+12;',
  23297. ' b.Left:=b.Left+13;',
  23298. ' b.NotRight:=b.NotRight+14;',
  23299. ' with b do begin',
  23300. ' Size:=Size+31;',
  23301. ' Speed:=Speed+32;',
  23302. ' Left:=Left+33;',
  23303. ' NotRight:=NotRight+34;',
  23304. ' end;',
  23305. '']);
  23306. ConvertProgram;
  23307. CheckSource('TestClassHelper_Property',
  23308. LinesToStr([ // statements
  23309. 'rtl.createClass(this, "TObject", null, function () {',
  23310. ' this.$init = function () {',
  23311. ' this.FSize = 0;',
  23312. ' };',
  23313. ' this.$final = function () {',
  23314. ' };',
  23315. ' this.GetSpeed = function () {',
  23316. ' var Result = 0;',
  23317. ' this.FSize = this.FSize + 11;',
  23318. ' this.SetSpeed(this.GetSpeed() + 12);',
  23319. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  23320. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  23321. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23322. ' this.FSize = this.FSize + 21;',
  23323. ' this.SetSpeed(this.GetSpeed() + 22);',
  23324. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23325. ' this.FSize = this.FSize + 31;',
  23326. ' this.SetSpeed(this.GetSpeed() + 32);',
  23327. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23328. ' return Result;',
  23329. ' };',
  23330. ' this.SetSpeed = function (Value) {',
  23331. ' };',
  23332. '});',
  23333. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23334. ' this.GetLeft = function () {',
  23335. ' var Result = 0;',
  23336. ' this.FSize = this.FSize + 11;',
  23337. ' this.SetSpeed(this.GetSpeed() + 12);',
  23338. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23339. ' this.FSize = this.FSize + 21;',
  23340. ' this.SetSpeed(this.GetSpeed() + 22);',
  23341. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23342. ' this.FSize = this.FSize + 31;',
  23343. ' this.SetSpeed(this.GetSpeed() + 32);',
  23344. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23345. ' return Result;',
  23346. ' };',
  23347. ' this.SetLeft = function (Value) {',
  23348. ' };',
  23349. '});',
  23350. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23351. ' this.DoIt = function () {',
  23352. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23353. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23354. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23355. ' };',
  23356. '});',
  23357. 'this.b = null;',
  23358. '']),
  23359. LinesToStr([ // $mod.$main
  23360. '$mod.b.FSize = $mod.b.FSize + 11;',
  23361. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23362. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23363. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23364. 'var $with = $mod.b;',
  23365. '$with.FSize = $with.FSize + 31;',
  23366. '$with.SetSpeed($with.GetSpeed() + 32);',
  23367. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23368. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23369. '']));
  23370. end;
  23371. procedure TTestModule.TestClassHelper_Property_Array;
  23372. begin
  23373. StartProgram(false);
  23374. Add([
  23375. 'type',
  23376. ' TObject = class',
  23377. ' function GetSpeed(Index: boolean): word;',
  23378. ' procedure SetSpeed(Index: boolean; Value: word);',
  23379. ' end;',
  23380. ' TObjHelper = class helper for TObject',
  23381. ' function GetSize(Index: boolean): word;',
  23382. ' procedure SetSize(Index: boolean; Value: word);',
  23383. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23384. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23385. ' end;',
  23386. ' TBird = class',
  23387. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23388. ' procedure DoIt;',
  23389. ' end;',
  23390. 'var',
  23391. ' b: TBird;',
  23392. 'function Tobject.GetSpeed(Index: boolean): word;',
  23393. 'begin',
  23394. ' Result:=Size[false];',
  23395. ' Size[true]:=Size[false]+11;',
  23396. ' Speed[true]:=Speed[false]+12;',
  23397. ' Self.Size[true]:=Self.Size[false]+21;',
  23398. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23399. ' with Self do begin',
  23400. ' Size[true]:=Size[false]+31;',
  23401. ' Speed[true]:=Speed[false]+32;',
  23402. ' end;',
  23403. 'end;',
  23404. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23405. 'begin',
  23406. 'end;',
  23407. 'function TObjHelper.GetSize(Index: boolean): word;',
  23408. 'begin',
  23409. ' Size[true]:=Size[false]+11;',
  23410. ' Speed[true]:=Speed[false]+12;',
  23411. ' Self.Size[true]:=Self.Size[false]+21;',
  23412. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23413. ' with Self do begin',
  23414. ' Size[true]:=Size[false]+31;',
  23415. ' Speed[true]:=Speed[false]+32;',
  23416. ' end;',
  23417. 'end;',
  23418. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23419. 'begin',
  23420. 'end;',
  23421. 'procedure TBird.DoIt;',
  23422. 'begin',
  23423. ' Items[true]:=Items[false]+11;',
  23424. ' Self.Items[true]:=Self.Items[false]+21;',
  23425. ' with Self do Items[true]:=Items[false]+31;',
  23426. 'end;',
  23427. 'begin',
  23428. ' b.Size[true]:=b.Size[false]+11;',
  23429. ' b.Speed[true]:=b.Speed[false]+12;',
  23430. ' b.Items[true]:=b.Items[false]+13;',
  23431. ' with b do begin',
  23432. ' Size[true]:=Size[false]+21;',
  23433. ' Speed[true]:=Speed[false]+22;',
  23434. ' Items[true]:=Items[false]+23;',
  23435. ' end;',
  23436. '']);
  23437. ConvertProgram;
  23438. CheckSource('TestClassHelper_Property_Array',
  23439. LinesToStr([ // statements
  23440. 'rtl.createClass(this, "TObject", null, function () {',
  23441. ' this.$init = function () {',
  23442. ' };',
  23443. ' this.$final = function () {',
  23444. ' };',
  23445. ' this.GetSpeed = function (Index) {',
  23446. ' var Result = 0;',
  23447. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23448. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23449. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23450. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23451. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23452. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23453. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23454. ' return Result;',
  23455. ' };',
  23456. ' this.SetSpeed = function (Index, Value) {',
  23457. ' };',
  23458. '});',
  23459. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23460. ' this.GetSize = function (Index) {',
  23461. ' var Result = 0;',
  23462. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23463. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23464. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23465. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23466. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23467. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23468. ' return Result;',
  23469. ' };',
  23470. ' this.SetSize = function (Index, Value) {',
  23471. ' };',
  23472. '});',
  23473. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23474. ' this.DoIt = function () {',
  23475. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23476. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23477. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23478. ' };',
  23479. '});',
  23480. 'this.b = null;',
  23481. '']),
  23482. LinesToStr([ // $mod.$main
  23483. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23484. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23485. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23486. 'var $with = $mod.b;',
  23487. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23488. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23489. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23490. '']));
  23491. end;
  23492. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23493. begin
  23494. StartProgram(false);
  23495. Add([
  23496. 'type',
  23497. ' TObject = class',
  23498. ' function GetSpeed(Index: boolean): word;',
  23499. ' procedure SetSpeed(Index: boolean; Value: word);',
  23500. ' end;',
  23501. ' TObjHelper = class helper for TObject',
  23502. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23503. ' end;',
  23504. ' TBird = class',
  23505. ' end;',
  23506. ' TBirdHelper = class helper for TBird',
  23507. ' function GetSize(Index: word): boolean;',
  23508. ' procedure SetSize(Index: word; Value: boolean);',
  23509. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23510. ' end;',
  23511. 'function Tobject.GetSpeed(Index: boolean): word;',
  23512. 'begin',
  23513. ' Self[true]:=Self[false]+1;',
  23514. 'end;',
  23515. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23516. 'begin',
  23517. 'end;',
  23518. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23519. 'begin',
  23520. ' Self[1]:=not Self[2];',
  23521. 'end;',
  23522. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23523. 'begin',
  23524. 'end;',
  23525. 'var',
  23526. ' o: TObject;',
  23527. ' b: TBird;',
  23528. 'begin',
  23529. ' o[true]:=o[false]+1;',
  23530. ' b[3]:=not b[4];',
  23531. '']);
  23532. ConvertProgram;
  23533. CheckSource('TestClassHelper_Property_Array_Default',
  23534. LinesToStr([ // statements
  23535. 'rtl.createClass(this, "TObject", null, function () {',
  23536. ' this.$init = function () {',
  23537. ' };',
  23538. ' this.$final = function () {',
  23539. ' };',
  23540. ' this.GetSpeed = function (Index) {',
  23541. ' var Result = 0;',
  23542. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23543. ' return Result;',
  23544. ' };',
  23545. ' this.SetSpeed = function (Index, Value) {',
  23546. ' };',
  23547. '});',
  23548. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23549. '});',
  23550. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23551. '});',
  23552. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23553. ' this.GetSize = function (Index) {',
  23554. ' var Result = false;',
  23555. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23556. ' return Result;',
  23557. ' };',
  23558. ' this.SetSize = function (Index, Value) {',
  23559. ' };',
  23560. '});',
  23561. 'this.o = null;',
  23562. 'this.b = null;',
  23563. '']),
  23564. LinesToStr([ // $mod.$main
  23565. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23566. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23567. '']));
  23568. end;
  23569. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23570. begin
  23571. StartProgram(false);
  23572. Add([
  23573. 'type',
  23574. ' TObject = class',
  23575. ' end;',
  23576. ' TObjHelper = class helper for TObject',
  23577. ' function GetItems(Index: word): TObject;',
  23578. ' procedure SetItems(Index: word; Value: TObject);',
  23579. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23580. ' end;',
  23581. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23582. 'begin',
  23583. ' Self[1][2]:=Self[3][4];',
  23584. 'end;',
  23585. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23586. 'begin',
  23587. 'end;',
  23588. 'var',
  23589. ' o: TObject;',
  23590. 'begin',
  23591. ' o[1][2]:=o[3][4];',
  23592. '']);
  23593. ConvertProgram;
  23594. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23595. LinesToStr([ // statements
  23596. 'rtl.createClass(this, "TObject", null, function () {',
  23597. ' this.$init = function () {',
  23598. ' };',
  23599. ' this.$final = function () {',
  23600. ' };',
  23601. '});',
  23602. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23603. ' this.GetItems = function (Index) {',
  23604. ' var Result = null;',
  23605. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23606. ' return Result;',
  23607. ' };',
  23608. ' this.SetItems = function (Index, Value) {',
  23609. ' };',
  23610. '});',
  23611. 'this.o = null;',
  23612. '']),
  23613. LinesToStr([ // $mod.$main
  23614. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23615. '']));
  23616. end;
  23617. procedure TTestModule.TestClassHelper_ClassProperty;
  23618. begin
  23619. StartProgram(false);
  23620. Add([
  23621. 'type',
  23622. ' TObject = class',
  23623. ' class var FSize: word;',
  23624. ' class function GetSpeed: word;',
  23625. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23626. ' end;',
  23627. ' TObjHelper = class helper for TObject',
  23628. ' class function GetLeft: word;',
  23629. ' class procedure SetLeft(Value: word);',
  23630. ' class property Size: word read FSize write FSize;',
  23631. ' class property Speed: word read GetSpeed write SetSpeed;',
  23632. ' class property Left: word read GetLeft write SetLeft;',
  23633. ' end;',
  23634. ' TBird = class',
  23635. ' class property NotRight: word read GetLeft write SetLeft;',
  23636. ' class procedure DoIt;',
  23637. ' end;',
  23638. ' TBirdClass = class of TBird;',
  23639. 'class function Tobject.GetSpeed: word;',
  23640. 'begin',
  23641. ' Size:=Size+11;',
  23642. ' Speed:=Speed+12;',
  23643. ' Left:=Left+13;',
  23644. ' Self.Size:=Self.Size+21;',
  23645. ' Self.Speed:=Self.Speed+22;',
  23646. ' Self.Left:=Self.Left+23;',
  23647. ' with Self do begin',
  23648. ' Size:=Size+31;',
  23649. ' Speed:=Speed+32;',
  23650. ' Left:=Left+33;',
  23651. ' end;',
  23652. 'end;',
  23653. 'class function TObjHelper.GetLeft: word;',
  23654. 'begin',
  23655. ' Size:=Size+11;',
  23656. ' Speed:=Speed+12;',
  23657. ' Left:=Left+13;',
  23658. ' Self.Size:=Self.Size+21;',
  23659. ' Self.Speed:=Self.Speed+22;',
  23660. ' Self.Left:=Self.Left+23;',
  23661. ' with Self do begin',
  23662. ' Size:=Size+31;',
  23663. ' Speed:=Speed+32;',
  23664. ' Left:=Left+33;',
  23665. ' end;',
  23666. 'end;',
  23667. 'class procedure TObjHelper.SetLeft(Value: word);',
  23668. 'begin',
  23669. 'end;',
  23670. 'class procedure TBird.DoIt;',
  23671. 'begin',
  23672. ' NotRight:=NotRight+11;',
  23673. ' Self.NotRight:=Self.NotRight+21;',
  23674. ' with Self do NotRight:=NotRight+31;',
  23675. 'end;',
  23676. 'var',
  23677. ' b: TBird;',
  23678. ' c: TBirdClass;',
  23679. 'begin',
  23680. ' b.Size:=b.Size+11;',
  23681. ' b.Speed:=b.Speed+12;',
  23682. ' b.Left:=b.Left+13;',
  23683. ' b.NotRight:=b.NotRight+14;',
  23684. ' with b do begin',
  23685. ' Size:=Size+31;',
  23686. ' Speed:=Speed+32;',
  23687. ' Left:=Left+33;',
  23688. ' NotRight:=NotRight+34;',
  23689. ' end;',
  23690. ' c.Size:=c.Size+11;',
  23691. ' c.Speed:=c.Speed+12;',
  23692. ' c.Left:=c.Left+13;',
  23693. ' c.NotRight:=c.NotRight+14;',
  23694. ' with c do begin',
  23695. ' Size:=Size+31;',
  23696. ' Speed:=Speed+32;',
  23697. ' Left:=Left+33;',
  23698. ' NotRight:=NotRight+34;',
  23699. ' end;',
  23700. ' tbird.Size:=tbird.Size+11;',
  23701. ' tbird.Speed:=tbird.Speed+12;',
  23702. ' tbird.Left:=tbird.Left+13;',
  23703. ' tbird.NotRight:=tbird.NotRight+14;',
  23704. ' with tbird do begin',
  23705. ' Size:=Size+31;',
  23706. ' Speed:=Speed+32;',
  23707. ' Left:=Left+33;',
  23708. ' NotRight:=NotRight+34;',
  23709. ' end;',
  23710. '']);
  23711. ConvertProgram;
  23712. CheckSource('TestClassHelper_ClassProperty',
  23713. LinesToStr([ // statements
  23714. 'rtl.createClass(this, "TObject", null, function () {',
  23715. ' this.FSize = 0;',
  23716. ' this.$init = function () {',
  23717. ' };',
  23718. ' this.$final = function () {',
  23719. ' };',
  23720. ' this.GetSpeed = function () {',
  23721. ' var Result = 0;',
  23722. ' $mod.TObject.FSize = this.FSize + 11;',
  23723. ' this.SetSpeed(this.GetSpeed() + 12);',
  23724. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23725. ' $mod.TObject.FSize = this.FSize + 21;',
  23726. ' this.SetSpeed(this.GetSpeed() + 22);',
  23727. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23728. ' $mod.TObject.FSize = this.FSize + 31;',
  23729. ' this.SetSpeed(this.GetSpeed() + 32);',
  23730. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23731. ' return Result;',
  23732. ' };',
  23733. '});',
  23734. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23735. ' this.GetLeft = function () {',
  23736. ' var Result = 0;',
  23737. ' $mod.TObject.FSize = this.FSize + 11;',
  23738. ' this.SetSpeed(this.GetSpeed() + 12);',
  23739. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23740. ' $mod.TObject.FSize = this.FSize + 21;',
  23741. ' this.SetSpeed(this.GetSpeed() + 22);',
  23742. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23743. ' $mod.TObject.FSize = this.FSize + 31;',
  23744. ' this.SetSpeed(this.GetSpeed() + 32);',
  23745. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23746. ' return Result;',
  23747. ' };',
  23748. ' this.SetLeft = function (Value) {',
  23749. ' };',
  23750. '});',
  23751. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23752. ' this.DoIt = function () {',
  23753. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23754. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23755. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23756. ' };',
  23757. '});',
  23758. 'this.b = null;',
  23759. 'this.c = null;',
  23760. '']),
  23761. LinesToStr([ // $mod.$main
  23762. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23763. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23764. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23765. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23766. 'var $with = $mod.b;',
  23767. '$mod.TObject.FSize = $with.FSize + 31;',
  23768. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23769. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23770. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23771. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23772. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23773. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23774. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23775. 'var $with1 = $mod.c;',
  23776. '$mod.TObject.FSize = $with1.FSize + 31;',
  23777. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23778. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23779. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23780. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23781. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23782. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23783. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23784. 'var $with2 = $mod.TBird;',
  23785. '$mod.TObject.FSize = $with2.FSize + 31;',
  23786. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23787. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23788. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23789. '']));
  23790. end;
  23791. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23792. begin
  23793. StartProgram(false);
  23794. Add([
  23795. 'type',
  23796. ' TObject = class',
  23797. ' class function GetSpeed: word; static;',
  23798. ' class procedure SetSpeed(Value: word); static;',
  23799. ' end;',
  23800. ' TObjHelper = class helper for TObject',
  23801. ' class function GetLeft: word; static;',
  23802. ' class procedure SetLeft(Value: word); static;',
  23803. ' class property Speed: word read GetSpeed write SetSpeed;',
  23804. ' class property Left: word read GetLeft write SetLeft;',
  23805. ' end;',
  23806. ' TBird = class',
  23807. ' class property NotRight: word read GetLeft write SetLeft;',
  23808. ' class procedure DoIt; static;',
  23809. ' class procedure DoSome;',
  23810. ' end;',
  23811. ' TBirdClass = class of TBird;',
  23812. 'class function Tobject.GetSpeed: word;',
  23813. 'begin',
  23814. ' Speed:=Speed+12;',
  23815. ' Left:=Left+13;',
  23816. 'end;',
  23817. 'class procedure TObject.SetSpeed(Value: word);',
  23818. 'begin',
  23819. 'end;',
  23820. 'class function TObjHelper.GetLeft: word;',
  23821. 'begin',
  23822. ' Speed:=Speed+12;',
  23823. ' Left:=Left+13;',
  23824. 'end;',
  23825. 'class procedure TObjHelper.SetLeft(Value: word);',
  23826. 'begin',
  23827. 'end;',
  23828. 'class procedure TBird.DoIt;',
  23829. 'begin',
  23830. ' NotRight:=NotRight+11;',
  23831. 'end;',
  23832. 'class procedure TBird.DoSome;',
  23833. 'begin',
  23834. ' Speed:=Speed+12;',
  23835. ' Left:=Left+13;',
  23836. ' Self.Speed:=Self.Speed+22;',
  23837. ' Self.Left:=Self.Left+23;',
  23838. ' with Self do begin',
  23839. ' Speed:=Speed+32;',
  23840. ' Left:=Left+33;',
  23841. ' end;',
  23842. ' NotRight:=NotRight+11;',
  23843. ' Self.NotRight:=Self.NotRight+21;',
  23844. ' with Self do NotRight:=NotRight+31;',
  23845. 'end;',
  23846. 'var',
  23847. ' b: TBird;',
  23848. ' c: TBirdClass;',
  23849. 'begin',
  23850. ' b.Speed:=b.Speed+12;',
  23851. ' b.Left:=b.Left+13;',
  23852. ' b.NotRight:=b.NotRight+14;',
  23853. ' with b do begin',
  23854. ' Speed:=Speed+32;',
  23855. ' Left:=Left+33;',
  23856. ' NotRight:=NotRight+34;',
  23857. ' end;',
  23858. ' c.Speed:=c.Speed+12;',
  23859. ' c.Left:=c.Left+13;',
  23860. ' c.NotRight:=c.NotRight+14;',
  23861. ' with c do begin',
  23862. ' Speed:=Speed+32;',
  23863. ' Left:=Left+33;',
  23864. ' NotRight:=NotRight+34;',
  23865. ' end;',
  23866. ' tbird.Speed:=tbird.Speed+12;',
  23867. ' tbird.Left:=tbird.Left+13;',
  23868. ' tbird.NotRight:=tbird.NotRight+14;',
  23869. ' with tbird do begin',
  23870. ' Speed:=Speed+32;',
  23871. ' Left:=Left+33;',
  23872. ' NotRight:=NotRight+34;',
  23873. ' end;',
  23874. '']);
  23875. ConvertProgram;
  23876. CheckSource('TestClassHelper_ClassPropertyStatic',
  23877. LinesToStr([ // statements
  23878. 'rtl.createClass(this, "TObject", null, function () {',
  23879. ' this.$init = function () {',
  23880. ' };',
  23881. ' this.$final = function () {',
  23882. ' };',
  23883. ' this.GetSpeed = function () {',
  23884. ' var Result = 0;',
  23885. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23886. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23887. ' return Result;',
  23888. ' };',
  23889. ' this.SetSpeed = function (Value) {',
  23890. ' };',
  23891. '});',
  23892. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23893. ' this.GetLeft = function () {',
  23894. ' var Result = 0;',
  23895. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23896. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23897. ' return Result;',
  23898. ' };',
  23899. ' this.SetLeft = function (Value) {',
  23900. ' };',
  23901. '});',
  23902. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23903. ' this.DoIt = function () {',
  23904. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23905. ' };',
  23906. ' this.DoSome = function () {',
  23907. ' this.SetSpeed(this.GetSpeed() + 12);',
  23908. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23909. ' this.SetSpeed(this.GetSpeed() + 22);',
  23910. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23911. ' this.SetSpeed(this.GetSpeed() + 32);',
  23912. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23913. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23914. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23915. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23916. ' };',
  23917. '});',
  23918. 'this.b = null;',
  23919. 'this.c = null;',
  23920. '']),
  23921. LinesToStr([ // $mod.$main
  23922. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23923. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23924. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23925. 'var $with = $mod.b;',
  23926. '$with.SetSpeed($with.GetSpeed() + 32);',
  23927. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23928. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23929. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23930. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23931. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23932. 'var $with1 = $mod.c;',
  23933. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23934. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23935. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23936. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23937. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23938. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23939. 'var $with2 = $mod.TBird;',
  23940. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23941. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23942. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23943. '']));
  23944. end;
  23945. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  23946. begin
  23947. StartProgram(false);
  23948. Add([
  23949. 'type',
  23950. ' TObject = class',
  23951. ' class function GetSpeed(Index: boolean): word;',
  23952. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  23953. ' end;',
  23954. ' TObjHelper = class helper for TObject',
  23955. ' class function GetSize(Index: boolean): word;',
  23956. ' class procedure SetSize(Index: boolean; Value: word);',
  23957. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  23958. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23959. ' end;',
  23960. ' TBird = class',
  23961. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  23962. ' class procedure DoIt;',
  23963. ' end;',
  23964. ' TBirdClass = class of TBird;',
  23965. 'class function Tobject.GetSpeed(Index: boolean): word;',
  23966. 'begin',
  23967. ' Size[true]:=Size[false]+11;',
  23968. ' Speed[true]:=Speed[false]+12;',
  23969. ' Self.Size[true]:=Self.Size[false]+21;',
  23970. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23971. ' with Self do begin',
  23972. ' Size[true]:=Size[false]+31;',
  23973. ' Speed[true]:=Speed[false]+32;',
  23974. ' end;',
  23975. 'end;',
  23976. 'class function TObjHelper.GetSize(Index: boolean): word;',
  23977. 'begin',
  23978. ' Size[true]:=Size[false]+11;',
  23979. ' Speed[true]:=Speed[false]+12;',
  23980. ' Self.Size[true]:=Self.Size[false]+21;',
  23981. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23982. ' with Self do begin',
  23983. ' Size[true]:=Size[false]+31;',
  23984. ' Speed[true]:=Speed[false]+32;',
  23985. ' end;',
  23986. 'end;',
  23987. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23988. 'begin',
  23989. 'end;',
  23990. 'class procedure TBird.DoIt;',
  23991. 'begin',
  23992. ' Items[true]:=Items[false]+11;',
  23993. ' Self.Items[true]:=Self.Items[false]+21;',
  23994. ' with Self do Items[true]:=Items[false]+31;',
  23995. 'end;',
  23996. 'var',
  23997. ' b: TBird;',
  23998. ' c: TBirdClass;',
  23999. 'begin',
  24000. ' b.Size[true]:=b.Size[false]+11;',
  24001. ' b.Speed[true]:=b.Speed[false]+12;',
  24002. ' b.Items[true]:=b.Items[false]+13;',
  24003. ' with b do begin',
  24004. ' Size[true]:=Size[false]+21;',
  24005. ' Speed[true]:=Speed[false]+22;',
  24006. ' Items[true]:=Items[false]+23;',
  24007. ' end;',
  24008. ' c.Size[true]:=c.Size[false]+11;',
  24009. ' c.Speed[true]:=c.Speed[false]+12;',
  24010. ' c.Items[true]:=c.Items[false]+13;',
  24011. ' with c do begin',
  24012. ' Size[true]:=Size[false]+21;',
  24013. ' Speed[true]:=Speed[false]+22;',
  24014. ' Items[true]:=Items[false]+23;',
  24015. ' end;',
  24016. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24017. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24018. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24019. ' with TBird do begin',
  24020. ' Size[true]:=Size[false]+21;',
  24021. ' Speed[true]:=Speed[false]+22;',
  24022. ' Items[true]:=Items[false]+23;',
  24023. ' end;',
  24024. '']);
  24025. ConvertProgram;
  24026. CheckSource('TestClassHelper_ClassProperty_Array',
  24027. LinesToStr([ // statements
  24028. 'rtl.createClass(this, "TObject", null, function () {',
  24029. ' this.$init = function () {',
  24030. ' };',
  24031. ' this.$final = function () {',
  24032. ' };',
  24033. ' this.GetSpeed = function (Index) {',
  24034. ' var Result = 0;',
  24035. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24036. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24037. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24038. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24039. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24040. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24041. ' return Result;',
  24042. ' };',
  24043. '});',
  24044. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24045. ' this.GetSize = function (Index) {',
  24046. ' var Result = 0;',
  24047. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24048. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24049. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24050. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24051. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24052. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24053. ' return Result;',
  24054. ' };',
  24055. ' this.SetSize = function (Index, Value) {',
  24056. ' };',
  24057. '});',
  24058. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24059. ' this.DoIt = function () {',
  24060. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24061. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24062. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24063. ' };',
  24064. '});',
  24065. 'this.b = null;',
  24066. 'this.c = null;',
  24067. '']),
  24068. LinesToStr([ // $mod.$main
  24069. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24070. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24071. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24072. 'var $with = $mod.b;',
  24073. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24074. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24075. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24076. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24077. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24078. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24079. 'var $with1 = $mod.c;',
  24080. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24081. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24082. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24083. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24084. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24085. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24086. 'var $with2 = $mod.TBird;',
  24087. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24088. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24089. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24090. '']));
  24091. end;
  24092. procedure TTestModule.TestClassHelper_ForIn;
  24093. begin
  24094. StartProgram(false);
  24095. Add([
  24096. 'type',
  24097. ' TObject = class end;',
  24098. ' TItem = TObject;',
  24099. ' TEnumerator = class',
  24100. ' FCurrent: TItem;',
  24101. ' property Current: TItem read FCurrent;',
  24102. ' function MoveNext: boolean;',
  24103. ' end;',
  24104. ' TBird = class',
  24105. ' end;',
  24106. ' TBirdHelper = class helper for TBird',
  24107. ' function GetEnumerator: TEnumerator;',
  24108. ' end;',
  24109. 'function TEnumerator.MoveNext: boolean;',
  24110. 'begin',
  24111. 'end;',
  24112. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24113. 'begin',
  24114. 'end;',
  24115. 'var',
  24116. ' b: TBird;',
  24117. ' i, i2: TItem;',
  24118. 'begin',
  24119. ' for i in b do i2:=i;']);
  24120. ConvertProgram;
  24121. CheckSource('TestClassHelper_ForIn',
  24122. LinesToStr([ // statements
  24123. 'rtl.createClass(this, "TObject", null, function () {',
  24124. ' this.$init = function () {',
  24125. ' };',
  24126. ' this.$final = function () {',
  24127. ' };',
  24128. '});',
  24129. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24130. ' this.$init = function () {',
  24131. ' $mod.TObject.$init.call(this);',
  24132. ' this.FCurrent = null;',
  24133. ' };',
  24134. ' this.$final = function () {',
  24135. ' this.FCurrent = undefined;',
  24136. ' $mod.TObject.$final.call(this);',
  24137. ' };',
  24138. ' this.MoveNext = function () {',
  24139. ' var Result = false;',
  24140. ' return Result;',
  24141. ' };',
  24142. '});',
  24143. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24144. '});',
  24145. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24146. ' this.GetEnumerator = function () {',
  24147. ' var Result = null;',
  24148. ' return Result;',
  24149. ' };',
  24150. '});',
  24151. 'this.b = null;',
  24152. 'this.i = null;',
  24153. 'this.i2 = null;'
  24154. ]),
  24155. LinesToStr([ // $mod.$main
  24156. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24157. 'try {',
  24158. ' while ($in.MoveNext()){',
  24159. ' $mod.i = $in.FCurrent;',
  24160. ' $mod.i2 = $mod.i;',
  24161. ' }',
  24162. '} finally {',
  24163. ' $in = rtl.freeLoc($in)',
  24164. '};',
  24165. '']));
  24166. end;
  24167. procedure TTestModule.TestClassHelper_PassProperty;
  24168. begin
  24169. StartProgram(false);
  24170. Add([
  24171. 'type',
  24172. ' TObject = class',
  24173. ' FField: TObject;',
  24174. ' property Field: TObject read FField write FField;',
  24175. ' end;',
  24176. ' THelper = class helper for TObject',
  24177. ' procedure Fly;',
  24178. ' class procedure Run;',
  24179. ' class procedure Jump; static;',
  24180. ' end;',
  24181. 'procedure THelper.Fly;',
  24182. 'begin',
  24183. ' Field.Fly;',
  24184. ' Field.Run;',
  24185. ' Field.Jump;',
  24186. ' with Field do begin',
  24187. ' Fly;',
  24188. ' Run;',
  24189. ' Jump;',
  24190. ' end;',
  24191. 'end;',
  24192. 'class procedure THelper.Run;',
  24193. 'begin',
  24194. 'end;',
  24195. 'class procedure THelper.Jump;',
  24196. 'begin',
  24197. 'end;',
  24198. 'var',
  24199. ' b: TObject;',
  24200. 'begin',
  24201. ' b.Field.Fly;',
  24202. ' b.Field.Run;',
  24203. ' b.Field.Jump;',
  24204. ' with b do begin',
  24205. ' Field.Run;',
  24206. ' Field.Fly;',
  24207. ' Field.Jump;',
  24208. ' end;',
  24209. ' with b.Field do begin',
  24210. ' Run;',
  24211. ' Fly;',
  24212. ' Jump;',
  24213. ' end;',
  24214. '']);
  24215. ConvertProgram;
  24216. CheckSource('TestClassHelper_PassProperty',
  24217. LinesToStr([ // statements
  24218. 'rtl.createClass(this, "TObject", null, function () {',
  24219. ' this.$init = function () {',
  24220. ' this.FField = null;',
  24221. ' };',
  24222. ' this.$final = function () {',
  24223. ' this.FField = undefined;',
  24224. ' };',
  24225. '});',
  24226. 'rtl.createHelper(this, "THelper", null, function () {',
  24227. ' this.Fly = function () {',
  24228. ' $mod.THelper.Fly.call(this.FField);',
  24229. ' $mod.THelper.Run.call(this.FField.$class);',
  24230. ' $mod.THelper.Jump();',
  24231. ' var $with = this.FField;',
  24232. ' $mod.THelper.Fly.call($with);',
  24233. ' $mod.THelper.Run.call($with.$class);',
  24234. ' $mod.THelper.Jump();',
  24235. ' };',
  24236. ' this.Run = function () {',
  24237. ' };',
  24238. ' this.Jump = function () {',
  24239. ' };',
  24240. '});',
  24241. 'this.b = null;',
  24242. '']),
  24243. LinesToStr([ // $mod.$main
  24244. '$mod.THelper.Fly.call($mod.b.FField);',
  24245. '$mod.THelper.Run.call($mod.b.FField.$class);',
  24246. '$mod.THelper.Jump();',
  24247. 'var $with = $mod.b;',
  24248. '$mod.THelper.Run.call($with.FField.$class);',
  24249. '$mod.THelper.Fly.call($with.FField);',
  24250. '$mod.THelper.Jump();',
  24251. 'var $with1 = $mod.b.FField;',
  24252. '$mod.THelper.Run.call($with1.$class);',
  24253. '$mod.THelper.Fly.call($with1);',
  24254. '$mod.THelper.Jump();',
  24255. '']));
  24256. end;
  24257. procedure TTestModule.TestExtClassHelper_ClassVar;
  24258. begin
  24259. StartProgram(false);
  24260. Add([
  24261. '{$modeswitch externalclass}',
  24262. 'type',
  24263. ' TExtA = class external name ''ExtObj''',
  24264. ' end;',
  24265. ' THelper = class helper for TExtA',
  24266. ' const',
  24267. ' One = 1;',
  24268. ' Two: word = 2;',
  24269. ' class var',
  24270. ' Glob: word;',
  24271. ' function Foo(w: word): word;',
  24272. ' class function Bar(w: word): word; static;',
  24273. ' end;',
  24274. 'function THelper.foo(w: word): word;',
  24275. 'begin',
  24276. ' Result:=w;',
  24277. ' Two:=One+w;',
  24278. ' Glob:=Glob;',
  24279. ' Result:=Self.Glob;',
  24280. ' Self.Glob:=Self.Glob;',
  24281. ' with Self do Glob:=Glob;',
  24282. 'end;',
  24283. 'class function THelper.bar(w: word): word;',
  24284. 'begin',
  24285. ' Result:=w;',
  24286. ' Two:=One;',
  24287. ' Glob:=Glob;',
  24288. 'end;',
  24289. 'var o: TExtA;',
  24290. 'begin',
  24291. ' texta.two:=texta.one;',
  24292. ' texta.Glob:=texta.Glob;',
  24293. ' with texta do begin',
  24294. ' two:=one;',
  24295. ' Glob:=Glob;',
  24296. ' end;',
  24297. ' o.two:=o.one;',
  24298. ' o.Glob:=o.Glob;',
  24299. ' with o do begin',
  24300. ' two:=one;',
  24301. ' Glob:=Glob;',
  24302. ' end;',
  24303. '']);
  24304. ConvertProgram;
  24305. CheckSource('TestExtClassHelper_ClassVar',
  24306. LinesToStr([ // statements
  24307. 'rtl.createHelper(this, "THelper", null, function () {',
  24308. ' this.One = 1;',
  24309. ' this.Two = 2;',
  24310. ' this.Glob = 0;',
  24311. ' this.Foo = function (w) {',
  24312. ' var Result = 0;',
  24313. ' Result = w;',
  24314. ' $mod.THelper.Two = 1 + w;',
  24315. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24316. ' Result = $mod.THelper.Glob;',
  24317. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24318. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24319. ' return Result;',
  24320. ' };',
  24321. ' this.Bar = function (w) {',
  24322. ' var Result = 0;',
  24323. ' Result = w;',
  24324. ' $mod.THelper.Two = 1;',
  24325. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24326. ' return Result;',
  24327. ' };',
  24328. '});',
  24329. 'this.o = null;',
  24330. '']),
  24331. LinesToStr([ // $mod.$main
  24332. '$mod.THelper.Two = 1;',
  24333. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24334. '$mod.THelper.Two = 1;',
  24335. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24336. '$mod.THelper.Two = 1;',
  24337. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24338. 'var $with = $mod.o;',
  24339. '$mod.THelper.Two = 1;',
  24340. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24341. '']));
  24342. end;
  24343. procedure TTestModule.TestExtClassHelper_Method_Call;
  24344. begin
  24345. StartProgram(false);
  24346. Add([
  24347. '{$modeswitch externalclass}',
  24348. 'type',
  24349. ' TFly = function(w: word): word of object;',
  24350. ' TExtA = class external name ''ExtObj''',
  24351. ' procedure Run(w: word = 10);',
  24352. ' end;',
  24353. ' THelper = class helper for TExtA',
  24354. ' function Foo(w: word = 1): word;',
  24355. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24356. ' end;',
  24357. 'var p: TFly;',
  24358. 'function THelper.foo(w: word): word;',
  24359. 'begin',
  24360. ' Run;',
  24361. ' Run();',
  24362. ' Run(11);',
  24363. ' Foo;',
  24364. ' Foo();',
  24365. ' Foo(12);',
  24366. ' Self.Foo;',
  24367. ' Self.Foo();',
  24368. ' Self.Foo(13);',
  24369. ' Fly;',
  24370. ' Fly();',
  24371. ' with Self do begin',
  24372. ' Foo;',
  24373. ' Foo();',
  24374. ' Foo(14);',
  24375. ' Fly;',
  24376. ' Fly();',
  24377. ' end;',
  24378. ' p:=@Fly;',
  24379. 'end;',
  24380. 'var Obj: TExtA;',
  24381. 'begin',
  24382. ' obj.Foo;',
  24383. ' obj.Foo();',
  24384. ' obj.Foo(21);',
  24385. ' obj.Fly;',
  24386. ' obj.Fly();',
  24387. ' with obj do begin',
  24388. ' Foo;',
  24389. ' Foo();',
  24390. ' Foo(22);',
  24391. ' Fly;',
  24392. ' Fly();',
  24393. ' end;',
  24394. ' p:[email protected];',
  24395. '']);
  24396. ConvertProgram;
  24397. CheckSource('TestExtClassHelper_Method_Call',
  24398. LinesToStr([ // statements
  24399. 'rtl.createHelper(this, "THelper", null, function () {',
  24400. ' this.Foo = function (w) {',
  24401. ' var Result = 0;',
  24402. ' this.Run(10);',
  24403. ' this.Run(10);',
  24404. ' this.Run(11);',
  24405. ' $mod.THelper.Foo.call(this, 1);',
  24406. ' $mod.THelper.Foo.call(this, 1);',
  24407. ' $mod.THelper.Foo.call(this, 12);',
  24408. ' $mod.THelper.Foo.call(this, 1);',
  24409. ' $mod.THelper.Foo.call(this, 1);',
  24410. ' $mod.THelper.Foo.call(this, 13);',
  24411. ' this.Fly(2);',
  24412. ' this.Fly(2);',
  24413. ' $mod.THelper.Foo.call(this, 1);',
  24414. ' $mod.THelper.Foo.call(this, 1);',
  24415. ' $mod.THelper.Foo.call(this, 14);',
  24416. ' this.Fly(2);',
  24417. ' this.Fly(2);',
  24418. ' $mod.p = rtl.createCallback(this, "Fly");',
  24419. ' return Result;',
  24420. ' };',
  24421. '});',
  24422. 'this.p = null;',
  24423. 'this.Obj = null;',
  24424. '']),
  24425. LinesToStr([ // $mod.$main
  24426. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24427. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24428. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24429. '$mod.Obj.Fly(2);',
  24430. '$mod.Obj.Fly(2);',
  24431. 'var $with = $mod.Obj;',
  24432. '$mod.THelper.Foo.call($with, 1);',
  24433. '$mod.THelper.Foo.call($with, 1);',
  24434. '$mod.THelper.Foo.call($with, 22);',
  24435. '$with.Fly(2);',
  24436. '$with.Fly(2);',
  24437. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24438. '']));
  24439. end;
  24440. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24441. begin
  24442. StartProgram(false);
  24443. Add([
  24444. '{$modeswitch externalclass}',
  24445. 'type',
  24446. ' TExtA = class external name ''ExtObj''',
  24447. ' procedure Run(w: word = 10);',
  24448. ' end;',
  24449. ' THelper = class helper for TExtA',
  24450. ' class procedure Fly;',
  24451. ' end;',
  24452. 'class procedure THelper.Fly;',
  24453. 'begin end;',
  24454. 'begin',
  24455. '']);
  24456. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24457. nHelperClassMethodForExtClassMustBeStatic);
  24458. ConvertProgram;
  24459. end;
  24460. procedure TTestModule.TestRecordHelper_ClassVar;
  24461. begin
  24462. StartProgram(false);
  24463. Add([
  24464. 'type',
  24465. ' TRec = record',
  24466. ' end;',
  24467. ' THelper = record helper for TRec',
  24468. ' const',
  24469. ' One = 1;',
  24470. ' Two: word = 2;',
  24471. ' class var',
  24472. ' Glob: word;',
  24473. ' function Foo(w: word): word;',
  24474. ' class function Bar(w: word): word; static;',
  24475. ' end;',
  24476. 'function THelper.foo(w: word): word;',
  24477. 'begin',
  24478. ' Result:=w;',
  24479. ' Two:=One+w;',
  24480. ' Glob:=Glob;',
  24481. ' Result:=Self.Glob;',
  24482. ' Self.Glob:=Self.Glob;',
  24483. ' with Self do Glob:=Glob;',
  24484. ' Self:=Self;',
  24485. 'end;',
  24486. 'class function THelper.bar(w: word): word;',
  24487. 'begin',
  24488. ' Result:=w;',
  24489. ' Two:=One;',
  24490. ' Glob:=Glob;',
  24491. 'end;',
  24492. 'var r: TRec;',
  24493. 'begin',
  24494. ' trec.two:=trec.one;',
  24495. ' trec.Glob:=trec.Glob;',
  24496. ' with trec do begin',
  24497. ' two:=one;',
  24498. ' Glob:=Glob;',
  24499. ' end;',
  24500. ' r.two:=r.one;',
  24501. ' r.Glob:=r.Glob;',
  24502. ' with r do begin',
  24503. ' two:=one;',
  24504. ' Glob:=Glob;',
  24505. ' end;',
  24506. '']);
  24507. ConvertProgram;
  24508. CheckSource('TestRecordHelper_ClassVar',
  24509. LinesToStr([ // statements
  24510. 'rtl.recNewT(this, "TRec", function () {',
  24511. ' this.$eq = function (b) {',
  24512. ' return true;',
  24513. ' };',
  24514. ' this.$assign = function (s) {',
  24515. ' return this;',
  24516. ' };',
  24517. '});',
  24518. 'rtl.createHelper(this, "THelper", null, function () {',
  24519. ' this.One = 1;',
  24520. ' this.Two = 2;',
  24521. ' this.Glob = 0;',
  24522. ' this.Foo = function (w) {',
  24523. ' var Result = 0;',
  24524. ' Result = w;',
  24525. ' $mod.THelper.Two = 1 + w;',
  24526. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24527. ' Result = $mod.THelper.Glob;',
  24528. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24529. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24530. ' this.$assign(this);',
  24531. ' return Result;',
  24532. ' };',
  24533. ' this.Bar = function (w) {',
  24534. ' var Result = 0;',
  24535. ' Result = w;',
  24536. ' $mod.THelper.Two = 1;',
  24537. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24538. ' return Result;',
  24539. ' };',
  24540. '});',
  24541. 'this.r = this.TRec.$new();',
  24542. '']),
  24543. LinesToStr([ // $mod.$main
  24544. '$mod.THelper.Two = 1;',
  24545. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24546. 'var $with = $mod.TRec;',
  24547. '$mod.THelper.Two = 1;',
  24548. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24549. '$mod.THelper.Two = 1;',
  24550. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24551. 'var $with1 = $mod.r;',
  24552. '$mod.THelper.Two = 1;',
  24553. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24554. '']));
  24555. end;
  24556. procedure TTestModule.TestRecordHelper_Method_Call;
  24557. begin
  24558. StartProgram(false);
  24559. Add([
  24560. '{$modeswitch AdvancedRecords}',
  24561. 'type',
  24562. ' TRec = record',
  24563. ' procedure Run(w: word = 10);',
  24564. ' end;',
  24565. ' THelper = record helper for TRec',
  24566. ' function Foo(w: word = 1): word;',
  24567. ' end;',
  24568. 'procedure TRec.Run(w: word);',
  24569. 'begin',
  24570. ' Foo;',
  24571. ' Foo();',
  24572. ' Foo(2);',
  24573. ' Self.Foo;',
  24574. ' Self.Foo();',
  24575. ' Self.Foo(3);',
  24576. ' with Self do begin',
  24577. ' Foo;',
  24578. ' Foo();',
  24579. ' Foo(4);',
  24580. ' end;',
  24581. 'end;',
  24582. 'function THelper.foo(w: word): word;',
  24583. 'begin',
  24584. ' Run;',
  24585. ' Run();',
  24586. ' Run(11);',
  24587. ' Foo;',
  24588. ' Foo();',
  24589. ' Foo(12);',
  24590. ' Self.Foo;',
  24591. ' Self.Foo();',
  24592. ' Self.Foo(13);',
  24593. ' with Self do begin',
  24594. ' Foo;',
  24595. ' Foo();',
  24596. ' Foo(14);',
  24597. ' end;',
  24598. 'end;',
  24599. 'var Rec: TRec;',
  24600. 'begin',
  24601. ' Rec.Foo;',
  24602. ' Rec.Foo();',
  24603. ' Rec.Foo(21);',
  24604. ' with Rec do begin',
  24605. ' Foo;',
  24606. ' Foo();',
  24607. ' Foo(22);',
  24608. ' end;',
  24609. '']);
  24610. ConvertProgram;
  24611. CheckSource('TestRecordHelper_Method_Call',
  24612. LinesToStr([ // statements
  24613. 'rtl.recNewT(this, "TRec", function () {',
  24614. ' this.$eq = function (b) {',
  24615. ' return true;',
  24616. ' };',
  24617. ' this.$assign = function (s) {',
  24618. ' return this;',
  24619. ' };',
  24620. ' this.Run = function (w) {',
  24621. ' $mod.THelper.Foo.call(this, 1);',
  24622. ' $mod.THelper.Foo.call(this, 1);',
  24623. ' $mod.THelper.Foo.call(this, 2);',
  24624. ' $mod.THelper.Foo.call(this, 1);',
  24625. ' $mod.THelper.Foo.call(this, 1);',
  24626. ' $mod.THelper.Foo.call(this, 3);',
  24627. ' $mod.THelper.Foo.call(this, 1);',
  24628. ' $mod.THelper.Foo.call(this, 1);',
  24629. ' $mod.THelper.Foo.call(this, 4);',
  24630. ' };',
  24631. '});',
  24632. 'rtl.createHelper(this, "THelper", null, function () {',
  24633. ' this.Foo = function (w) {',
  24634. ' var Result = 0;',
  24635. ' this.Run(10);',
  24636. ' this.Run(10);',
  24637. ' this.Run(11);',
  24638. ' $mod.THelper.Foo.call(this, 1);',
  24639. ' $mod.THelper.Foo.call(this, 1);',
  24640. ' $mod.THelper.Foo.call(this, 12);',
  24641. ' $mod.THelper.Foo.call(this, 1);',
  24642. ' $mod.THelper.Foo.call(this, 1);',
  24643. ' $mod.THelper.Foo.call(this, 13);',
  24644. ' $mod.THelper.Foo.call(this, 1);',
  24645. ' $mod.THelper.Foo.call(this, 1);',
  24646. ' $mod.THelper.Foo.call(this, 14);',
  24647. ' return Result;',
  24648. ' };',
  24649. '});',
  24650. 'this.Rec = this.TRec.$new();',
  24651. '']),
  24652. LinesToStr([ // $mod.$main
  24653. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24654. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24655. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24656. 'var $with = $mod.Rec;',
  24657. '$mod.THelper.Foo.call($with, 1);',
  24658. '$mod.THelper.Foo.call($with, 1);',
  24659. '$mod.THelper.Foo.call($with, 22);',
  24660. '']));
  24661. end;
  24662. procedure TTestModule.TestRecordHelper_Constructor;
  24663. begin
  24664. StartProgram(false);
  24665. Add([
  24666. '{$modeswitch AdvancedRecords}',
  24667. 'type',
  24668. ' TRec = record',
  24669. ' constructor Create(w: word);',
  24670. ' end;',
  24671. ' THelper = record helper for TRec',
  24672. ' constructor NewHlp(w: word);',
  24673. ' end;',
  24674. 'var',
  24675. ' Rec: TRec;',
  24676. 'constructor TRec.Create(w: word);',
  24677. 'begin',
  24678. ' NewHlp(2);', // normal call
  24679. ' trec.NewHlp(3);', // new instance
  24680. 'end;',
  24681. 'constructor THelper.NewHlp(w: word);',
  24682. 'begin',
  24683. ' create(2);', // normal call
  24684. ' trec.create(3);', // new instance
  24685. ' NewHlp(4);', // normal call
  24686. ' trec.NewHlp(5);', // new instance
  24687. 'end;',
  24688. 'begin',
  24689. ' rec.newhlp(2);', // normal call
  24690. ' with rec do newhlp(12);', // normal call
  24691. ' trec.newhlp(3);', // new instance
  24692. ' with trec do newhlp(13);', // new instance
  24693. '']);
  24694. ConvertProgram;
  24695. CheckSource('TestRecordHelper_Constructor',
  24696. LinesToStr([ // statements
  24697. 'rtl.recNewT(this, "TRec", function () {',
  24698. ' this.$eq = function (b) {',
  24699. ' return true;',
  24700. ' };',
  24701. ' this.$assign = function (s) {',
  24702. ' return this;',
  24703. ' };',
  24704. ' this.Create = function (w) {',
  24705. ' $mod.THelper.NewHlp.call(this, 2);',
  24706. ' $mod.THelper.$new("NewHlp", [3]);',
  24707. ' return this;',
  24708. ' };',
  24709. '});',
  24710. 'rtl.createHelper(this, "THelper", null, function () {',
  24711. ' this.NewHlp = function (w) {',
  24712. ' this.Create(2);',
  24713. ' $mod.TRec.$new().Create(3);',
  24714. ' $mod.THelper.NewHlp.call(this, 4);',
  24715. ' $mod.THelper.$new("NewHlp", [5]);',
  24716. ' return this;',
  24717. ' };',
  24718. ' this.$new = function (fn, args) {',
  24719. ' return this[fn].apply($mod.TRec.$new(), args);',
  24720. ' };',
  24721. '});',
  24722. 'this.Rec = this.TRec.$new();',
  24723. '']),
  24724. LinesToStr([ // $mod.$main
  24725. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24726. 'var $with = $mod.Rec;',
  24727. '$mod.THelper.NewHlp.call($with, 12);',
  24728. '$mod.THelper.$new("NewHlp", [3]);',
  24729. 'var $with1 = $mod.TRec;',
  24730. '$mod.THelper.$new("NewHlp", [13]);',
  24731. '']));
  24732. end;
  24733. procedure TTestModule.TestTypeHelper_ClassVar;
  24734. begin
  24735. StartProgram(false);
  24736. Add([
  24737. '{$modeswitch typehelpers}',
  24738. 'type',
  24739. ' THelper = type helper for byte',
  24740. ' const',
  24741. ' One = 1;',
  24742. ' Two: word = 2;',
  24743. ' class var',
  24744. ' Glob: word;',
  24745. ' function Foo(w: word): word;',
  24746. ' class function Bar(w: word): word; static;',
  24747. ' end;',
  24748. 'function THelper.foo(w: word): word;',
  24749. 'begin',
  24750. ' Result:=w;',
  24751. ' Two:=One+w;',
  24752. ' Glob:=Glob;',
  24753. ' Result:=Self.Glob;',
  24754. ' Self.Glob:=Self.Glob;',
  24755. ' with Self do Glob:=Glob;',
  24756. 'end;',
  24757. 'class function THelper.bar(w: word): word;',
  24758. 'begin',
  24759. ' Result:=w;',
  24760. ' Two:=One;',
  24761. ' Glob:=Glob;',
  24762. 'end;',
  24763. 'var b: byte;',
  24764. 'begin',
  24765. ' byte.two:=byte.one;',
  24766. ' byte.Glob:=byte.Glob;',
  24767. ' with byte do begin',
  24768. ' two:=one;',
  24769. ' Glob:=Glob;',
  24770. ' end;',
  24771. ' b.two:=b.one;',
  24772. ' b.Glob:=b.Glob;',
  24773. ' with b do begin',
  24774. ' two:=one;',
  24775. ' Glob:=Glob;',
  24776. ' end;',
  24777. '']);
  24778. ConvertProgram;
  24779. CheckSource('TestTypeHelper_ClassVar',
  24780. LinesToStr([ // statements
  24781. 'rtl.createHelper(this, "THelper", null, function () {',
  24782. ' this.One = 1;',
  24783. ' this.Two = 2;',
  24784. ' this.Glob = 0;',
  24785. ' this.Foo = function (w) {',
  24786. ' var Result = 0;',
  24787. ' Result = w;',
  24788. ' $mod.THelper.Two = 1 + w;',
  24789. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24790. ' Result = $mod.THelper.Glob;',
  24791. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24792. ' var $with = this.get();',
  24793. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24794. ' return Result;',
  24795. ' };',
  24796. ' this.Bar = function (w) {',
  24797. ' var Result = 0;',
  24798. ' Result = w;',
  24799. ' $mod.THelper.Two = 1;',
  24800. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24801. ' return Result;',
  24802. ' };',
  24803. '});',
  24804. 'this.b = 0;',
  24805. '']),
  24806. LinesToStr([ // $mod.$main
  24807. '$mod.THelper.Two = 1;',
  24808. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24809. '$mod.THelper.Two = 1;',
  24810. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24811. '$mod.THelper.Two = 1;',
  24812. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24813. 'var $with = $mod.b;',
  24814. '$mod.THelper.Two = 1;',
  24815. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24816. '']));
  24817. end;
  24818. procedure TTestModule.TestTypeHelper_PassResultElement;
  24819. begin
  24820. StartProgram(false);
  24821. Add([
  24822. '{$modeswitch typehelpers}',
  24823. 'type',
  24824. ' THelper = type helper for word',
  24825. ' procedure DoIt(e: byte = 123);',
  24826. ' class procedure DoSome(e: byte = 456); static;',
  24827. ' end;',
  24828. 'procedure THelper.DoIt(e: byte);',
  24829. 'begin',
  24830. 'end;',
  24831. 'class procedure THelper.DoSome(e: byte);',
  24832. 'begin',
  24833. 'end;',
  24834. 'function Foo(w: word): word;',
  24835. 'begin',
  24836. ' Result.DoIt;',
  24837. ' Result.DoIt();',
  24838. ' Result.DoSome;',
  24839. ' Result.DoSome();',
  24840. ' with Result do begin',
  24841. ' DoIt;',
  24842. ' DoIt();',
  24843. ' DoSome;',
  24844. ' DoSome();',
  24845. ' end;',
  24846. 'end;',
  24847. 'begin',
  24848. '']);
  24849. ConvertProgram;
  24850. CheckSource('TestTypeHelper_PassResultElement',
  24851. LinesToStr([ // statements
  24852. 'rtl.createHelper(this, "THelper", null, function () {',
  24853. ' this.DoIt = function (e) {',
  24854. ' };',
  24855. ' this.DoSome = function (e) {',
  24856. ' };',
  24857. '});',
  24858. 'this.Foo = function (w) {',
  24859. ' var Result = 0;',
  24860. ' $mod.THelper.DoIt.call({',
  24861. ' get: function () {',
  24862. ' return Result;',
  24863. ' },',
  24864. ' set: function (v) {',
  24865. ' Result = v;',
  24866. ' }',
  24867. ' }, 123);',
  24868. ' $mod.THelper.DoIt.call({',
  24869. ' get: function () {',
  24870. ' return Result;',
  24871. ' },',
  24872. ' set: function (v) {',
  24873. ' Result = v;',
  24874. ' }',
  24875. ' }, 123);',
  24876. ' $mod.THelper.DoSome(456);',
  24877. ' $mod.THelper.DoSome(456);',
  24878. ' $mod.THelper.DoIt.call({',
  24879. ' get: function () {',
  24880. ' return Result;',
  24881. ' },',
  24882. ' set: function (v) {',
  24883. ' Result = v;',
  24884. ' }',
  24885. ' }, 123);',
  24886. ' $mod.THelper.DoIt.call({',
  24887. ' get: function () {',
  24888. ' return Result;',
  24889. ' },',
  24890. ' set: function (v) {',
  24891. ' Result = v;',
  24892. ' }',
  24893. ' }, 123);',
  24894. ' $mod.THelper.DoSome(456);',
  24895. ' $mod.THelper.DoSome(456);',
  24896. ' return Result;',
  24897. '};',
  24898. '']),
  24899. LinesToStr([ // $mod.$main
  24900. '']));
  24901. end;
  24902. procedure TTestModule.TestTypeHelper_PassArgs;
  24903. begin
  24904. StartProgram(false);
  24905. Add([
  24906. '{$modeswitch typehelpers}',
  24907. 'type',
  24908. ' THelper = type helper for word',
  24909. ' procedure DoIt(e: byte = 123);',
  24910. ' end;',
  24911. 'procedure THelper.DoIt(e: byte);',
  24912. 'begin',
  24913. 'end;',
  24914. 'procedure FooDefault(a: word);',
  24915. 'begin',
  24916. ' a.DoIt;',
  24917. ' with a do DoIt;',
  24918. 'end;',
  24919. 'procedure FooConst(const a: word);',
  24920. 'begin',
  24921. ' a.DoIt;',
  24922. ' with a do DoIt;',
  24923. 'end;',
  24924. 'procedure FooVar(var a: word);',
  24925. 'begin',
  24926. ' a.DoIt;',
  24927. ' with a do DoIt;',
  24928. 'end;',
  24929. 'begin',
  24930. '']);
  24931. ConvertProgram;
  24932. CheckSource('TestTypeHelper_PassArgs',
  24933. LinesToStr([ // statements
  24934. 'rtl.createHelper(this, "THelper", null, function () {',
  24935. ' this.DoIt = function (e) {',
  24936. ' };',
  24937. '});',
  24938. 'this.FooDefault = function (a) {',
  24939. ' $mod.THelper.DoIt.call({',
  24940. ' get: function () {',
  24941. ' return a;',
  24942. ' },',
  24943. ' set: function (v) {',
  24944. ' a = v;',
  24945. ' }',
  24946. ' }, 123);',
  24947. ' $mod.THelper.DoIt.call({',
  24948. ' get: function () {',
  24949. ' return a;',
  24950. ' },',
  24951. ' set: function (v) {',
  24952. ' a = v;',
  24953. ' }',
  24954. ' }, 123);',
  24955. '};',
  24956. 'this.FooConst = function (a) {',
  24957. ' $mod.THelper.DoIt.call({',
  24958. ' get: function () {',
  24959. ' return a;',
  24960. ' },',
  24961. ' set: function (v) {',
  24962. ' rtl.raiseE("EPropReadOnly");',
  24963. ' }',
  24964. ' }, 123);',
  24965. ' $mod.THelper.DoIt.call({',
  24966. ' get: function () {',
  24967. ' return a;',
  24968. ' },',
  24969. ' set: function () {',
  24970. ' rtl.raiseE("EPropReadOnly");',
  24971. ' }',
  24972. ' }, 123);',
  24973. '};',
  24974. 'this.FooVar = function (a) {',
  24975. ' $mod.THelper.DoIt.call(a, 123);',
  24976. ' var $with = a.get();',
  24977. ' $mod.THelper.DoIt.call(a, 123);',
  24978. '};',
  24979. '']),
  24980. LinesToStr([ // $mod.$main
  24981. '']));
  24982. end;
  24983. procedure TTestModule.TestTypeHelper_PassVarConst;
  24984. begin
  24985. StartProgram(false);
  24986. Add([
  24987. '{$modeswitch typehelpers}',
  24988. 'type',
  24989. ' THelper = type helper for word',
  24990. ' procedure DoIt(e: byte = 123);',
  24991. ' end;',
  24992. 'procedure THelper.DoIt(e: byte);',
  24993. 'begin',
  24994. 'end;',
  24995. 'var a: word;',
  24996. 'const c: word = 2;',
  24997. '{$writeableconst off}',
  24998. 'const r: word = 3;',
  24999. 'begin',
  25000. ' a.DoIt;',
  25001. ' with a do DoIt;',
  25002. ' c.DoIt;',
  25003. ' with c do DoIt;',
  25004. ' r.DoIt;',
  25005. ' with r do DoIt;',
  25006. '']);
  25007. ConvertProgram;
  25008. CheckSource('TestTypeHelper_PassVarConst',
  25009. LinesToStr([ // statements
  25010. 'rtl.createHelper(this, "THelper", null, function () {',
  25011. ' this.DoIt = function (e) {',
  25012. ' };',
  25013. '});',
  25014. 'this.a = 0;',
  25015. 'this.c = 2;',
  25016. 'this.r = 3;',
  25017. '']),
  25018. LinesToStr([ // $mod.$main
  25019. '$mod.THelper.DoIt.call({',
  25020. ' p: $mod,',
  25021. ' get: function () {',
  25022. ' return this.p.a;',
  25023. ' },',
  25024. ' set: function (v) {',
  25025. ' this.p.a = v;',
  25026. ' }',
  25027. '}, 123);',
  25028. 'var $with = $mod.a;',
  25029. '$mod.THelper.DoIt.call({',
  25030. ' get: function () {',
  25031. ' return $with;',
  25032. ' },',
  25033. ' set: function (v) {',
  25034. ' $with = v;',
  25035. ' }',
  25036. '}, 123);',
  25037. '$mod.THelper.DoIt.call({',
  25038. ' p: $mod,',
  25039. ' get: function () {',
  25040. ' return this.p.c;',
  25041. ' },',
  25042. ' set: function (v) {',
  25043. ' this.p.c = v;',
  25044. ' }',
  25045. '}, 123);',
  25046. 'var $with1 = $mod.c;',
  25047. '$mod.THelper.DoIt.call({',
  25048. ' get: function () {',
  25049. ' return $with1;',
  25050. ' },',
  25051. ' set: function (v) {',
  25052. ' $with1 = v;',
  25053. ' }',
  25054. '}, 123);',
  25055. '$mod.THelper.DoIt.call({',
  25056. ' get: function () {',
  25057. ' return 3;',
  25058. ' },',
  25059. ' set: function (v) {',
  25060. ' rtl.raiseE("EPropReadOnly");',
  25061. ' }',
  25062. '}, 123);',
  25063. 'var $with2 = 3;',
  25064. ' $mod.THelper.DoIt.call({',
  25065. ' get: function () {',
  25066. ' return $with2;',
  25067. ' },',
  25068. ' set: function () {',
  25069. ' rtl.raiseE("EPropReadOnly");',
  25070. ' }',
  25071. ' }, 123);',
  25072. '']));
  25073. end;
  25074. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25075. begin
  25076. StartProgram(false);
  25077. Add([
  25078. '{$modeswitch typehelpers}',
  25079. 'type',
  25080. ' THelper = type helper for word',
  25081. ' procedure DoIt(e: byte = 123);',
  25082. ' end;',
  25083. 'procedure THelper.DoIt(e: byte);',
  25084. 'begin',
  25085. 'end;',
  25086. 'function Foo(b: byte = 1): word;',
  25087. 'begin',
  25088. 'end;',
  25089. 'begin',
  25090. ' Foo.DoIt;',
  25091. ' Foo().DoIt;',
  25092. ' with Foo do DoIt;',
  25093. ' with Foo() do DoIt;',
  25094. '']);
  25095. ConvertProgram;
  25096. CheckSource('TestTypeHelper_PassFuncResult',
  25097. LinesToStr([ // statements
  25098. 'rtl.createHelper(this, "THelper", null, function () {',
  25099. ' this.DoIt = function (e) {',
  25100. ' };',
  25101. '});',
  25102. 'this.Foo = function (b) {',
  25103. ' var Result = 0;',
  25104. ' return Result;',
  25105. '};',
  25106. '']),
  25107. LinesToStr([ // $mod.$main
  25108. '$mod.THelper.DoIt.call({',
  25109. ' a: $mod.Foo(1),',
  25110. ' get: function () {',
  25111. ' return this.a;',
  25112. ' },',
  25113. ' set: function (v) {',
  25114. ' this.a = v;',
  25115. ' }',
  25116. '}, 123);',
  25117. '$mod.THelper.DoIt.call({',
  25118. ' a: $mod.Foo(1),',
  25119. ' get: function () {',
  25120. ' return this.a;',
  25121. ' },',
  25122. ' set: function (v) {',
  25123. ' this.a = v;',
  25124. ' }',
  25125. '}, 123);',
  25126. 'var $with = $mod.Foo(1);',
  25127. '$mod.THelper.DoIt.call({',
  25128. ' get: function () {',
  25129. ' return $with;',
  25130. ' },',
  25131. ' set: function (v) {',
  25132. ' $with = v;',
  25133. ' }',
  25134. '}, 123);',
  25135. 'var $with1 = $mod.Foo(1);',
  25136. '$mod.THelper.DoIt.call({',
  25137. ' get: function () {',
  25138. ' return $with1;',
  25139. ' },',
  25140. ' set: function (v) {',
  25141. ' $with1 = v;',
  25142. ' }',
  25143. '}, 123);',
  25144. '']));
  25145. end;
  25146. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25147. begin
  25148. StartProgram(false);
  25149. Add([
  25150. '{$modeswitch typehelpers}',
  25151. 'type',
  25152. ' TObject = class',
  25153. ' FField: word;',
  25154. ' procedure SetField(Value: word);',
  25155. ' property Field: word read FField write SetField;',
  25156. ' end;',
  25157. ' THelper = type helper for word',
  25158. ' procedure Fly;',
  25159. ' class procedure Run; static;',
  25160. ' end;',
  25161. 'procedure TObject.SetField(Value: word);',
  25162. 'begin',
  25163. ' Field.Fly;',
  25164. ' Field.Run;',
  25165. ' Self.Field.Fly;',
  25166. ' Self.Field.Run;',
  25167. ' with Self do begin',
  25168. ' Field.Fly;',
  25169. ' Field.Run;',
  25170. ' end;',
  25171. ' with Self.Field do begin',
  25172. ' Fly;',
  25173. ' Run;',
  25174. ' end;',
  25175. 'end;',
  25176. 'procedure THelper.Fly;',
  25177. 'begin',
  25178. 'end;',
  25179. 'class procedure THelper.Run;',
  25180. 'begin',
  25181. 'end;',
  25182. 'var',
  25183. ' o: TObject;',
  25184. 'begin',
  25185. ' o.Field.Fly;',
  25186. ' o.Field.Run;',
  25187. ' with o do begin',
  25188. ' Field.Fly;',
  25189. ' Field.Run;',
  25190. ' end;',
  25191. ' with o.Field do begin',
  25192. ' Fly;',
  25193. ' Run;',
  25194. ' end;',
  25195. '']);
  25196. ConvertProgram;
  25197. CheckSource('TestTypeHelper_PassPropertyField',
  25198. LinesToStr([ // statements
  25199. 'rtl.createClass(this, "TObject", null, function () {',
  25200. ' this.$init = function () {',
  25201. ' this.FField = 0;',
  25202. ' };',
  25203. ' this.$final = function () {',
  25204. ' };',
  25205. ' this.SetField = function (Value) {',
  25206. ' $mod.THelper.Fly.call({',
  25207. ' p: this,',
  25208. ' get: function () {',
  25209. ' return this.p.FField;',
  25210. ' },',
  25211. ' set: function (v) {',
  25212. ' this.p.FField = v;',
  25213. ' }',
  25214. ' });',
  25215. ' $mod.THelper.Run();',
  25216. ' $mod.THelper.Fly.call({',
  25217. ' p: this,',
  25218. ' get: function () {',
  25219. ' return this.p.FField;',
  25220. ' },',
  25221. ' set: function (v) {',
  25222. ' this.p.FField = v;',
  25223. ' }',
  25224. ' });',
  25225. ' $mod.THelper.Run();',
  25226. ' $mod.THelper.Fly.call({',
  25227. ' p: this,',
  25228. ' get: function () {',
  25229. ' return this.p.FField;',
  25230. ' },',
  25231. ' set: function (v) {',
  25232. ' this.p.FField = v;',
  25233. ' }',
  25234. ' });',
  25235. ' $mod.THelper.Run();',
  25236. ' var $with = this.FField;',
  25237. ' $mod.THelper.Fly.call({',
  25238. ' get: function () {',
  25239. ' return $with;',
  25240. ' },',
  25241. ' set: function (v) {',
  25242. ' $with = v;',
  25243. ' }',
  25244. ' });',
  25245. ' $mod.THelper.Run();',
  25246. ' };',
  25247. '});',
  25248. 'rtl.createHelper(this, "THelper", null, function () {',
  25249. ' this.Fly = function () {',
  25250. ' };',
  25251. ' this.Run = function () {',
  25252. ' };',
  25253. '});',
  25254. 'this.o = null;',
  25255. '']),
  25256. LinesToStr([ // $mod.$main
  25257. '$mod.THelper.Fly.call({',
  25258. ' p: $mod.o,',
  25259. ' get: function () {',
  25260. ' return this.p.FField;',
  25261. ' },',
  25262. ' set: function (v) {',
  25263. ' this.p.FField = v;',
  25264. ' }',
  25265. '});',
  25266. '$mod.THelper.Run();',
  25267. 'var $with = $mod.o;',
  25268. '$mod.THelper.Fly.call({',
  25269. ' p: $with,',
  25270. ' get: function () {',
  25271. ' return this.p.FField;',
  25272. ' },',
  25273. ' set: function (v) {',
  25274. ' this.p.FField = v;',
  25275. ' }',
  25276. '});',
  25277. '$mod.THelper.Run();',
  25278. 'var $with1 = $mod.o.FField;',
  25279. '$mod.THelper.Fly.call({',
  25280. ' get: function () {',
  25281. ' return $with1;',
  25282. ' },',
  25283. ' set: function (v) {',
  25284. ' $with1 = v;',
  25285. ' }',
  25286. '});',
  25287. '$mod.THelper.Run();',
  25288. '']));
  25289. end;
  25290. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  25291. begin
  25292. StartProgram(false);
  25293. Add([
  25294. '{$modeswitch typehelpers}',
  25295. 'type',
  25296. ' TObject = class',
  25297. ' FField: word;',
  25298. ' function GetField: word;',
  25299. ' property Field: word read GetField write FField;',
  25300. ' end;',
  25301. ' THelper = type helper for word',
  25302. ' procedure Fly;',
  25303. ' class procedure Run; static;',
  25304. ' end;',
  25305. 'function TObject.GetField: word;',
  25306. 'begin',
  25307. ' Field.Fly;',
  25308. ' Field.Run;',
  25309. ' Self.Field.Fly;',
  25310. ' Self.Field.Run;',
  25311. ' with Self do begin',
  25312. ' Field.Fly;',
  25313. ' Field.Run;',
  25314. ' end;',
  25315. ' with Self.Field do begin',
  25316. ' Fly;',
  25317. ' Run;',
  25318. ' end;',
  25319. 'end;',
  25320. 'procedure THelper.Fly;',
  25321. 'begin',
  25322. 'end;',
  25323. 'class procedure THelper.Run;',
  25324. 'begin',
  25325. 'end;',
  25326. 'var',
  25327. ' o: TObject;',
  25328. 'begin',
  25329. ' o.Field.Fly;',
  25330. ' o.Field.Run;',
  25331. ' with o do begin',
  25332. ' Field.Fly;',
  25333. ' Field.Run;',
  25334. ' end;',
  25335. ' with o.Field do begin',
  25336. ' Fly;',
  25337. ' Run;',
  25338. ' end;',
  25339. '']);
  25340. ConvertProgram;
  25341. CheckSource('TestTypeHelper_PassPropertyGetter',
  25342. LinesToStr([ // statements
  25343. 'rtl.createClass(this, "TObject", null, function () {',
  25344. ' this.$init = function () {',
  25345. ' this.FField = 0;',
  25346. ' };',
  25347. ' this.$final = function () {',
  25348. ' };',
  25349. ' this.GetField = function () {',
  25350. ' var Result = 0;',
  25351. ' $mod.THelper.Fly.call({',
  25352. ' p: this.GetField(),',
  25353. ' get: function () {',
  25354. ' return this.p;',
  25355. ' },',
  25356. ' set: function (v) {',
  25357. ' this.p = v;',
  25358. ' }',
  25359. ' });',
  25360. ' $mod.THelper.Run();',
  25361. ' $mod.THelper.Fly.call({',
  25362. ' p: this.GetField(),',
  25363. ' get: function () {',
  25364. ' return this.p;',
  25365. ' },',
  25366. ' set: function (v) {',
  25367. ' this.p = v;',
  25368. ' }',
  25369. ' });',
  25370. ' $mod.THelper.Run();',
  25371. ' $mod.THelper.Fly.call({',
  25372. ' p: this.GetField(),',
  25373. ' get: function () {',
  25374. ' return this.p;',
  25375. ' },',
  25376. ' set: function (v) {',
  25377. ' this.p = v;',
  25378. ' }',
  25379. ' });',
  25380. ' $mod.THelper.Run();',
  25381. ' var $with = this.GetField();',
  25382. ' $mod.THelper.Fly.call({',
  25383. ' get: function () {',
  25384. ' return $with;',
  25385. ' },',
  25386. ' set: function (v) {',
  25387. ' $with = v;',
  25388. ' }',
  25389. ' });',
  25390. ' $mod.THelper.Run();',
  25391. ' return Result;',
  25392. ' };',
  25393. '});',
  25394. 'rtl.createHelper(this, "THelper", null, function () {',
  25395. ' this.Fly = function () {',
  25396. ' };',
  25397. ' this.Run = function () {',
  25398. ' };',
  25399. '});',
  25400. 'this.o = null;',
  25401. '']),
  25402. LinesToStr([ // $mod.$main
  25403. '$mod.THelper.Fly.call({',
  25404. ' p: $mod.o.GetField(),',
  25405. ' get: function () {',
  25406. ' return this.p;',
  25407. ' },',
  25408. ' set: function (v) {',
  25409. ' this.p = v;',
  25410. ' }',
  25411. '});',
  25412. '$mod.THelper.Run();',
  25413. 'var $with = $mod.o;',
  25414. '$mod.THelper.Fly.call({',
  25415. ' p: $with.GetField(),',
  25416. ' get: function () {',
  25417. ' return this.p;',
  25418. ' },',
  25419. ' set: function (v) {',
  25420. ' this.p = v;',
  25421. ' }',
  25422. '});',
  25423. '$mod.THelper.Run();',
  25424. 'var $with1 = $mod.o.GetField();',
  25425. '$mod.THelper.Fly.call({',
  25426. ' get: function () {',
  25427. ' return $with1;',
  25428. ' },',
  25429. ' set: function (v) {',
  25430. ' $with1 = v;',
  25431. ' }',
  25432. '});',
  25433. '$mod.THelper.Run();',
  25434. '']));
  25435. end;
  25436. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25437. begin
  25438. StartProgram(false);
  25439. Add([
  25440. '{$modeswitch typehelpers}',
  25441. 'type',
  25442. ' TObject = class',
  25443. ' class var FField: word;',
  25444. ' class procedure SetField(Value: word);',
  25445. ' class property Field: word read FField write SetField;',
  25446. ' end;',
  25447. ' THelper = type helper for word',
  25448. ' procedure Fly(n: byte);',
  25449. ' end;',
  25450. 'class procedure TObject.SetField(Value: word);',
  25451. 'begin',
  25452. ' Field.Fly(1);',
  25453. ' Self.Field.Fly(2);',
  25454. ' with Self do Field.Fly(3);',
  25455. ' with Self.Field do Fly(4);',
  25456. ' TObject.Field.Fly(5);',
  25457. ' with TObject do Field.Fly(6);',
  25458. ' with TObject.Field do Fly(7);',
  25459. 'end;',
  25460. 'procedure THelper.Fly(n: byte);',
  25461. 'begin',
  25462. 'end;',
  25463. 'var',
  25464. ' o: TObject;',
  25465. 'begin',
  25466. ' o.Field.Fly(11);',
  25467. ' with o do Field.Fly(12);',
  25468. ' with o.Field do Fly(13);',
  25469. ' TObject.Field.Fly(14);',
  25470. ' with TObject do Field.Fly(15);',
  25471. ' with TObject.Field do Fly(16);',
  25472. '']);
  25473. ConvertProgram;
  25474. CheckSource('TestTypeHelper_PassClassPropertyField',
  25475. LinesToStr([ // statements
  25476. 'rtl.createClass(this, "TObject", null, function () {',
  25477. ' this.FField = 0;',
  25478. ' this.$init = function () {',
  25479. ' };',
  25480. ' this.$final = function () {',
  25481. ' };',
  25482. ' this.SetField = function (Value) {',
  25483. ' $mod.THelper.Fly.call({',
  25484. ' p: this,',
  25485. ' get: function () {',
  25486. ' return this.p.FField;',
  25487. ' },',
  25488. ' set: function (v) {',
  25489. ' $mod.TObject.FField = v;',
  25490. ' }',
  25491. ' }, 1);',
  25492. ' $mod.THelper.Fly.call({',
  25493. ' p: this,',
  25494. ' get: function () {',
  25495. ' return this.p.FField;',
  25496. ' },',
  25497. ' set: function (v) {',
  25498. ' $mod.TObject.FField = v;',
  25499. ' }',
  25500. ' }, 2);',
  25501. ' $mod.THelper.Fly.call({',
  25502. ' p: this,',
  25503. ' get: function () {',
  25504. ' return this.p.FField;',
  25505. ' },',
  25506. ' set: function (v) {',
  25507. ' $mod.TObject.FField = v;',
  25508. ' }',
  25509. ' }, 3);',
  25510. ' var $with = this.FField;',
  25511. ' $mod.THelper.Fly.call({',
  25512. ' get: function () {',
  25513. ' return $with;',
  25514. ' },',
  25515. ' set: function (v) {',
  25516. ' $with = v;',
  25517. ' }',
  25518. ' }, 4);',
  25519. ' $mod.THelper.Fly.call({',
  25520. ' p: $mod.TObject,',
  25521. ' get: function () {',
  25522. ' return this.p.FField;',
  25523. ' },',
  25524. ' set: function (v) {',
  25525. ' $mod.TObject.FField = v;',
  25526. ' }',
  25527. ' }, 5);',
  25528. ' var $with1 = $mod.TObject;',
  25529. ' $mod.THelper.Fly.call({',
  25530. ' p: $with1,',
  25531. ' get: function () {',
  25532. ' return this.p.FField;',
  25533. ' },',
  25534. ' set: function (v) {',
  25535. ' $mod.TObject.FField = v;',
  25536. ' }',
  25537. ' }, 6);',
  25538. ' var $with2 = $mod.TObject.FField;',
  25539. ' $mod.THelper.Fly.call({',
  25540. ' get: function () {',
  25541. ' return $with2;',
  25542. ' },',
  25543. ' set: function (v) {',
  25544. ' $with2 = v;',
  25545. ' }',
  25546. ' }, 7);',
  25547. ' };',
  25548. '});',
  25549. 'rtl.createHelper(this, "THelper", null, function () {',
  25550. ' this.Fly = function (n) {',
  25551. ' };',
  25552. '});',
  25553. 'this.o = null;',
  25554. '']),
  25555. LinesToStr([ // $mod.$main
  25556. '$mod.THelper.Fly.call({',
  25557. ' p: $mod.o,',
  25558. ' get: function () {',
  25559. ' return this.p.FField;',
  25560. ' },',
  25561. ' set: function (v) {',
  25562. ' $mod.TObject.FField = v;',
  25563. ' }',
  25564. '}, 11);',
  25565. 'var $with = $mod.o;',
  25566. '$mod.THelper.Fly.call({',
  25567. ' p: $with,',
  25568. ' get: function () {',
  25569. ' return this.p.FField;',
  25570. ' },',
  25571. ' set: function (v) {',
  25572. ' $mod.TObject.FField = v;',
  25573. ' }',
  25574. '}, 12);',
  25575. 'var $with1 = $mod.o.FField;',
  25576. '$mod.THelper.Fly.call({',
  25577. ' get: function () {',
  25578. ' return $with1;',
  25579. ' },',
  25580. ' set: function (v) {',
  25581. ' $with1 = v;',
  25582. ' }',
  25583. '}, 13);',
  25584. '$mod.THelper.Fly.call({',
  25585. ' p: $mod.TObject,',
  25586. ' get: function () {',
  25587. ' return this.p.FField;',
  25588. ' },',
  25589. ' set: function (v) {',
  25590. ' $mod.TObject.FField = v;',
  25591. ' }',
  25592. '}, 14);',
  25593. 'var $with2 = $mod.TObject;',
  25594. '$mod.THelper.Fly.call({',
  25595. ' p: $with2,',
  25596. ' get: function () {',
  25597. ' return this.p.FField;',
  25598. ' },',
  25599. ' set: function (v) {',
  25600. ' $mod.TObject.FField = v;',
  25601. ' }',
  25602. '}, 15);',
  25603. 'var $with3 = $mod.TObject.FField;',
  25604. '$mod.THelper.Fly.call({',
  25605. ' get: function () {',
  25606. ' return $with3;',
  25607. ' },',
  25608. ' set: function (v) {',
  25609. ' $with3 = v;',
  25610. ' }',
  25611. '}, 16);',
  25612. '']));
  25613. end;
  25614. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25615. begin
  25616. StartProgram(false);
  25617. Add([
  25618. '{$modeswitch typehelpers}',
  25619. 'type',
  25620. ' TObject = class',
  25621. ' class var FField: word;',
  25622. ' class function GetField: word; static;',
  25623. ' class property Field: word read GetField write FField;',
  25624. ' end;',
  25625. ' THelper = type helper for word',
  25626. ' procedure Fly(n: byte);',
  25627. ' end;',
  25628. 'class function TObject.GetField: word;',
  25629. 'begin',
  25630. ' Field.Fly(1);',
  25631. ' TObject.Field.Fly(5);',
  25632. ' with TObject do Field.Fly(6);',
  25633. ' with TObject.Field do Fly(7);',
  25634. 'end;',
  25635. 'procedure THelper.Fly(n: byte);',
  25636. 'begin',
  25637. 'end;',
  25638. 'var',
  25639. ' o: TObject;',
  25640. 'begin',
  25641. ' o.Field.Fly(11);',
  25642. ' with o do Field.Fly(12);',
  25643. ' with o.Field do Fly(13);',
  25644. '']);
  25645. ConvertProgram;
  25646. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25647. LinesToStr([ // statements
  25648. 'rtl.createClass(this, "TObject", null, function () {',
  25649. ' this.FField = 0;',
  25650. ' this.$init = function () {',
  25651. ' };',
  25652. ' this.$final = function () {',
  25653. ' };',
  25654. ' this.GetField = function () {',
  25655. ' var Result = 0;',
  25656. ' $mod.THelper.Fly.call({',
  25657. ' p: $mod.TObject.GetField(),',
  25658. ' get: function () {',
  25659. ' return this.p;',
  25660. ' },',
  25661. ' set: function (v) {',
  25662. ' this.p = v;',
  25663. ' }',
  25664. ' }, 1);',
  25665. ' $mod.THelper.Fly.call({',
  25666. ' p: $mod.TObject.GetField(),',
  25667. ' get: function () {',
  25668. ' return this.p;',
  25669. ' },',
  25670. ' set: function (v) {',
  25671. ' this.p = v;',
  25672. ' }',
  25673. ' }, 5);',
  25674. ' var $with = $mod.TObject;',
  25675. ' $mod.THelper.Fly.call({',
  25676. ' p: $with.GetField(),',
  25677. ' get: function () {',
  25678. ' return this.p;',
  25679. ' },',
  25680. ' set: function (v) {',
  25681. ' this.p = v;',
  25682. ' }',
  25683. ' }, 6);',
  25684. ' var $with1 = $mod.TObject.GetField();',
  25685. ' $mod.THelper.Fly.call({',
  25686. ' get: function () {',
  25687. ' return $with1;',
  25688. ' },',
  25689. ' set: function (v) {',
  25690. ' $with1 = v;',
  25691. ' }',
  25692. ' }, 7);',
  25693. ' return Result;',
  25694. ' };',
  25695. '});',
  25696. 'rtl.createHelper(this, "THelper", null, function () {',
  25697. ' this.Fly = function (n) {',
  25698. ' };',
  25699. '});',
  25700. 'this.o = null;',
  25701. '']),
  25702. LinesToStr([ // $mod.$main
  25703. '$mod.THelper.Fly.call({',
  25704. ' p: $mod.TObject.GetField(),',
  25705. ' get: function () {',
  25706. ' return this.p;',
  25707. ' },',
  25708. ' set: function (v) {',
  25709. ' this.p = v;',
  25710. ' }',
  25711. '}, 11);',
  25712. 'var $with = $mod.o;',
  25713. '$mod.THelper.Fly.call({',
  25714. ' p: $with.GetField(),',
  25715. ' get: function () {',
  25716. ' return this.p;',
  25717. ' },',
  25718. ' set: function (v) {',
  25719. ' this.p = v;',
  25720. ' }',
  25721. '}, 12);',
  25722. 'var $with1 = $mod.TObject.GetField();',
  25723. '$mod.THelper.Fly.call({',
  25724. ' get: function () {',
  25725. ' return $with1;',
  25726. ' },',
  25727. ' set: function (v) {',
  25728. ' $with1 = v;',
  25729. ' }',
  25730. '}, 13);',
  25731. '']));
  25732. end;
  25733. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25734. begin
  25735. StartProgram(false);
  25736. Add([
  25737. '{$modeswitch typehelpers}',
  25738. 'type',
  25739. ' TObject = class',
  25740. ' class var FField: word;',
  25741. ' class function GetField: word;',
  25742. ' class property Field: word read GetField write FField;',
  25743. ' end;',
  25744. ' TClass = class of TObject;',
  25745. ' THelper = type helper for word',
  25746. ' procedure Fly(n: byte);',
  25747. ' end;',
  25748. 'class function TObject.GetField: word;',
  25749. 'begin',
  25750. ' Field.Fly(1);',
  25751. ' Self.Field.Fly(5);',
  25752. ' with Self do Field.Fly(6);',
  25753. ' with Self.Field do Fly(7);',
  25754. 'end;',
  25755. 'procedure THelper.Fly(n: byte);',
  25756. 'begin',
  25757. 'end;',
  25758. 'var',
  25759. ' o: TObject;',
  25760. ' c: TClass;',
  25761. 'begin',
  25762. ' o.Field.Fly(11);',
  25763. ' with o do Field.Fly(12);',
  25764. ' with o.Field do Fly(13);',
  25765. ' c.Field.Fly(14);',
  25766. ' with c do Field.Fly(15);',
  25767. ' with c.Field do Fly(16);',
  25768. '']);
  25769. ConvertProgram;
  25770. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25771. LinesToStr([ // statements
  25772. 'rtl.createClass(this, "TObject", null, function () {',
  25773. ' this.FField = 0;',
  25774. ' this.$init = function () {',
  25775. ' };',
  25776. ' this.$final = function () {',
  25777. ' };',
  25778. ' this.GetField = function () {',
  25779. ' var Result = 0;',
  25780. ' $mod.THelper.Fly.call({',
  25781. ' p: this.GetField(),',
  25782. ' get: function () {',
  25783. ' return this.p;',
  25784. ' },',
  25785. ' set: function (v) {',
  25786. ' this.p = v;',
  25787. ' }',
  25788. ' }, 1);',
  25789. ' $mod.THelper.Fly.call({',
  25790. ' p: this.GetField(),',
  25791. ' get: function () {',
  25792. ' return this.p;',
  25793. ' },',
  25794. ' set: function (v) {',
  25795. ' this.p = v;',
  25796. ' }',
  25797. ' }, 5);',
  25798. ' $mod.THelper.Fly.call({',
  25799. ' p: this.GetField(),',
  25800. ' get: function () {',
  25801. ' return this.p;',
  25802. ' },',
  25803. ' set: function (v) {',
  25804. ' this.p = v;',
  25805. ' }',
  25806. ' }, 6);',
  25807. ' var $with = this.GetField();',
  25808. ' $mod.THelper.Fly.call({',
  25809. ' get: function () {',
  25810. ' return $with;',
  25811. ' },',
  25812. ' set: function (v) {',
  25813. ' $with = v;',
  25814. ' }',
  25815. ' }, 7);',
  25816. ' return Result;',
  25817. ' };',
  25818. '});',
  25819. 'rtl.createHelper(this, "THelper", null, function () {',
  25820. ' this.Fly = function (n) {',
  25821. ' };',
  25822. '});',
  25823. 'this.o = null;',
  25824. 'this.c = null;',
  25825. '']),
  25826. LinesToStr([ // $mod.$main
  25827. '$mod.THelper.Fly.call({',
  25828. ' p: $mod.o.$class.GetField(),',
  25829. ' get: function () {',
  25830. ' return this.p;',
  25831. ' },',
  25832. ' set: function (v) {',
  25833. ' this.p = v;',
  25834. ' }',
  25835. '}, 11);',
  25836. 'var $with = $mod.o;',
  25837. '$mod.THelper.Fly.call({',
  25838. ' p: $with.$class.GetField(),',
  25839. ' get: function () {',
  25840. ' return this.p;',
  25841. ' },',
  25842. ' set: function (v) {',
  25843. ' this.p = v;',
  25844. ' }',
  25845. '}, 12);',
  25846. 'var $with1 = $mod.o.$class.GetField();',
  25847. '$mod.THelper.Fly.call({',
  25848. ' get: function () {',
  25849. ' return $with1;',
  25850. ' },',
  25851. ' set: function (v) {',
  25852. ' $with1 = v;',
  25853. ' }',
  25854. '}, 13);',
  25855. '$mod.THelper.Fly.call({',
  25856. ' p: $mod.c.GetField(),',
  25857. ' get: function () {',
  25858. ' return this.p;',
  25859. ' },',
  25860. ' set: function (v) {',
  25861. ' this.p = v;',
  25862. ' }',
  25863. '}, 14);',
  25864. 'var $with2 = $mod.c;',
  25865. '$mod.THelper.Fly.call({',
  25866. ' p: $with2.GetField(),',
  25867. ' get: function () {',
  25868. ' return this.p;',
  25869. ' },',
  25870. ' set: function (v) {',
  25871. ' this.p = v;',
  25872. ' }',
  25873. '}, 15);',
  25874. 'var $with3 = $mod.c.GetField();',
  25875. '$mod.THelper.Fly.call({',
  25876. ' get: function () {',
  25877. ' return $with3;',
  25878. ' },',
  25879. ' set: function (v) {',
  25880. ' $with3 = v;',
  25881. ' }',
  25882. '}, 16);',
  25883. '']));
  25884. end;
  25885. procedure TTestModule.TestTypeHelper_Property;
  25886. begin
  25887. StartProgram(false);
  25888. Add([
  25889. '{$modeswitch typehelpers}',
  25890. 'type',
  25891. ' THelper = type helper for word',
  25892. ' function GetSize: longint;',
  25893. ' procedure SetSize(Value: longint);',
  25894. ' property Size: longint read GetSize write SetSize;',
  25895. ' end;',
  25896. 'function THelper.GetSize: longint;',
  25897. 'begin',
  25898. ' Result:=Size+1;',
  25899. ' Size:=2;',
  25900. ' Result:=Self.Size+3;',
  25901. ' Self.Size:=4;',
  25902. ' with Self do begin',
  25903. ' Result:=Size+5;',
  25904. ' Size:=6;',
  25905. ' end;',
  25906. 'end;',
  25907. 'procedure THelper.SetSize(Value: longint);',
  25908. 'begin',
  25909. 'end;',
  25910. 'var w: word;',
  25911. 'begin',
  25912. ' w:=w.Size+7;',
  25913. ' w.Size:=w+8;',
  25914. ' with w do begin',
  25915. ' w:=Size+9;',
  25916. ' Size:=w+10;',
  25917. ' end;',
  25918. '']);
  25919. ConvertProgram;
  25920. CheckSource('TestTypeHelper_Property',
  25921. LinesToStr([ // statements
  25922. 'rtl.createHelper(this, "THelper", null, function () {',
  25923. ' this.GetSize = function () {',
  25924. ' var Result = 0;',
  25925. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25926. ' $mod.THelper.SetSize.call(this, 2);',
  25927. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25928. ' $mod.THelper.SetSize.call(this, 4);',
  25929. ' var $with = this.get();',
  25930. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25931. ' $mod.THelper.SetSize.call(this, 6);',
  25932. ' return Result;',
  25933. ' };',
  25934. ' this.SetSize = function (Value) {',
  25935. ' };',
  25936. '});',
  25937. 'this.w = 0;',
  25938. '']),
  25939. LinesToStr([ // $mod.$main
  25940. '$mod.w = $mod.THelper.GetSize.call({',
  25941. ' p: $mod,',
  25942. ' get: function () {',
  25943. ' return this.p.w;',
  25944. ' },',
  25945. ' set: function (v) {',
  25946. ' this.p.w = v;',
  25947. ' }',
  25948. '}) + 7;',
  25949. '$mod.THelper.SetSize.call({',
  25950. ' p: $mod,',
  25951. ' get: function () {',
  25952. ' return this.p.w;',
  25953. ' },',
  25954. ' set: function (v) {',
  25955. ' this.p.w = v;',
  25956. ' }',
  25957. '}, $mod.w + 8);',
  25958. 'var $with = $mod.w;',
  25959. '$mod.w = $mod.THelper.GetSize.call({',
  25960. ' get: function () {',
  25961. ' return $with;',
  25962. ' },',
  25963. ' set: function (v) {',
  25964. ' $with = v;',
  25965. ' }',
  25966. '}) + 9;',
  25967. '$mod.THelper.SetSize.call({',
  25968. ' get: function () {',
  25969. ' return $with;',
  25970. ' },',
  25971. ' set: function (v) {',
  25972. ' $with = v;',
  25973. ' }',
  25974. '}, $mod.w + 10);',
  25975. '']));
  25976. end;
  25977. procedure TTestModule.TestTypeHelper_Property_Array;
  25978. begin
  25979. StartProgram(false);
  25980. Add([
  25981. '{$modeswitch typehelpers}',
  25982. 'type',
  25983. ' THelper = type helper for word',
  25984. ' function GetItems(Index: byte): boolean;',
  25985. ' procedure SetItems(Index: byte; Value: boolean);',
  25986. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  25987. ' end;',
  25988. 'function THelper.GetItems(Index: byte): boolean;',
  25989. 'begin',
  25990. ' Result:=Items[1];',
  25991. ' Items[2]:=false;',
  25992. ' Result:=Self.Items[3];',
  25993. ' Self.Items[4]:=true;',
  25994. ' with Self do begin',
  25995. ' Result:=Items[5];',
  25996. ' Items[6]:=false;',
  25997. ' end;',
  25998. 'end;',
  25999. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26000. 'begin',
  26001. 'end;',
  26002. 'var',
  26003. ' w: word;',
  26004. ' b: boolean;',
  26005. 'begin',
  26006. ' b:=w.Items[1];',
  26007. ' w.Items[2]:=b;',
  26008. ' with w do begin',
  26009. ' b:=Items[3];',
  26010. ' Items[4]:=b;',
  26011. ' end;',
  26012. '']);
  26013. ConvertProgram;
  26014. CheckSource('TestTypeHelper_Property_Array',
  26015. LinesToStr([ // statements
  26016. 'rtl.createHelper(this, "THelper", null, function () {',
  26017. ' this.GetItems = function (Index) {',
  26018. ' var Result = false;',
  26019. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26020. ' $mod.THelper.SetItems.call(this, 2, false);',
  26021. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26022. ' $mod.THelper.SetItems.call(this, 4, true);',
  26023. ' var $with = this.get();',
  26024. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26025. ' $mod.THelper.SetItems.call(this, 6, false);',
  26026. ' return Result;',
  26027. ' };',
  26028. ' this.SetItems = function (Index, Value) {',
  26029. ' };',
  26030. '});',
  26031. 'this.w = 0;',
  26032. 'this.b = false;',
  26033. '']),
  26034. LinesToStr([ // $mod.$main
  26035. '$mod.b = $mod.THelper.GetItems.call({',
  26036. ' p: $mod,',
  26037. ' get: function () {',
  26038. ' return this.p.w;',
  26039. ' },',
  26040. ' set: function (v) {',
  26041. ' this.p.w = v;',
  26042. ' }',
  26043. '}, 1);',
  26044. '$mod.THelper.SetItems.call({',
  26045. ' p: $mod,',
  26046. ' get: function () {',
  26047. ' return this.p.w;',
  26048. ' },',
  26049. ' set: function (v) {',
  26050. ' this.p.w = v;',
  26051. ' }',
  26052. '}, 2, $mod.b);',
  26053. 'var $with = $mod.w;',
  26054. '$mod.b = $mod.THelper.GetItems.call({',
  26055. ' get: function () {',
  26056. ' return $with;',
  26057. ' },',
  26058. ' set: function (v) {',
  26059. ' $with = v;',
  26060. ' }',
  26061. '}, 3);',
  26062. '$mod.THelper.SetItems.call({',
  26063. ' get: function () {',
  26064. ' return $with;',
  26065. ' },',
  26066. ' set: function (v) {',
  26067. ' $with = v;',
  26068. ' }',
  26069. '}, 4, $mod.b);',
  26070. '']));
  26071. end;
  26072. procedure TTestModule.TestTypeHelper_ClassProperty;
  26073. begin
  26074. StartProgram(false);
  26075. Add([
  26076. '{$modeswitch typehelpers}',
  26077. 'type',
  26078. ' THelper = type helper for word',
  26079. ' class function GetSize: longint; static;',
  26080. ' class procedure SetSize(Value: longint); static;',
  26081. ' class property Size: longint read GetSize write SetSize;',
  26082. ' end;',
  26083. 'class function THelper.GetSize: longint;',
  26084. 'begin',
  26085. ' Result:=Size+1;',
  26086. ' Size:=2;',
  26087. 'end;',
  26088. 'class procedure THelper.SetSize(Value: longint);',
  26089. 'begin',
  26090. 'end;',
  26091. 'begin',
  26092. '']);
  26093. ConvertProgram;
  26094. CheckSource('TestTypeHelper_ClassProperty',
  26095. LinesToStr([ // statements
  26096. 'rtl.createHelper(this, "THelper", null, function () {',
  26097. ' this.GetSize = function () {',
  26098. ' var Result = 0;',
  26099. ' Result = $mod.THelper.GetSize() + 1;',
  26100. ' $mod.THelper.SetSize(2);',
  26101. ' return Result;',
  26102. ' };',
  26103. ' this.SetSize = function (Value) {',
  26104. ' };',
  26105. '});',
  26106. '']),
  26107. LinesToStr([ // $mod.$main
  26108. '']));
  26109. end;
  26110. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26111. begin
  26112. StartProgram(false);
  26113. Add([
  26114. '{$modeswitch typehelpers}',
  26115. 'type',
  26116. ' THelper = type helper for word',
  26117. ' class function GetItems(Index: byte): boolean; static;',
  26118. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26119. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26120. ' end;',
  26121. 'class function THelper.GetItems(Index: byte): boolean;',
  26122. 'begin',
  26123. ' Result:=Items[1];',
  26124. ' Items[2]:=false;',
  26125. 'end;',
  26126. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26127. 'begin',
  26128. 'end;',
  26129. 'var',
  26130. ' w: word;',
  26131. ' b: boolean;',
  26132. 'begin',
  26133. ' b:=w.Items[1];',
  26134. ' w.Items[2]:=b;',
  26135. ' with w do begin',
  26136. ' b:=Items[3];',
  26137. ' Items[4]:=b;',
  26138. ' end;',
  26139. '']);
  26140. ConvertProgram;
  26141. CheckSource('TestTypeHelper_ClassProperty_Array',
  26142. LinesToStr([ // statements
  26143. 'rtl.createHelper(this, "THelper", null, function () {',
  26144. ' this.GetItems = function (Index) {',
  26145. ' var Result = false;',
  26146. ' Result = $mod.THelper.GetItems(1);',
  26147. ' $mod.THelper.SetItems(2, false);',
  26148. ' return Result;',
  26149. ' };',
  26150. ' this.SetItems = function (Index, Value) {',
  26151. ' };',
  26152. '});',
  26153. 'this.w = 0;',
  26154. 'this.b = false;',
  26155. '']),
  26156. LinesToStr([ // $mod.$main
  26157. '$mod.b = $mod.THelper.GetItems(1);',
  26158. '$mod.THelper.SetItems(2, $mod.b);',
  26159. 'var $with = $mod.w;',
  26160. '$mod.b = $mod.THelper.GetItems(3);',
  26161. '$mod.THelper.SetItems(4, $mod.b);',
  26162. '']));
  26163. end;
  26164. procedure TTestModule.TestTypeHelper_ClassMethod;
  26165. begin
  26166. StartProgram(false);
  26167. Add([
  26168. '{$modeswitch typehelpers}',
  26169. 'type',
  26170. ' THelper = type helper for word',
  26171. ' class procedure DoStatic; static;',
  26172. ' end;',
  26173. 'class procedure THelper.DoStatic;',
  26174. 'begin',
  26175. ' DoStatic;',
  26176. ' DoStatic();',
  26177. 'end;',
  26178. 'var w: word;',
  26179. 'begin',
  26180. ' w.DoStatic;',
  26181. ' w.DoStatic();',
  26182. '']);
  26183. ConvertProgram;
  26184. CheckSource('TestTypeHelper_ClassMethod',
  26185. LinesToStr([ // statements
  26186. 'rtl.createHelper(this, "THelper", null, function () {',
  26187. ' this.DoStatic = function () {',
  26188. ' $mod.THelper.DoStatic();',
  26189. ' $mod.THelper.DoStatic();',
  26190. ' };',
  26191. '});',
  26192. 'this.w = 0;',
  26193. '']),
  26194. LinesToStr([ // $mod.$main
  26195. '$mod.THelper.DoStatic();',
  26196. '$mod.THelper.DoStatic();',
  26197. '']));
  26198. end;
  26199. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26200. begin
  26201. StartProgram(false);
  26202. Add([
  26203. '{$modeswitch typehelpers}',
  26204. 'type',
  26205. ' THelper = type helper for word',
  26206. ' procedure Run; external name ''Run'';',
  26207. ' end;',
  26208. 'var w: word;',
  26209. 'begin',
  26210. ' w.Run;',
  26211. '']);
  26212. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26213. ConvertProgram;
  26214. end;
  26215. procedure TTestModule.TestTypeHelper_Constructor;
  26216. begin
  26217. StartProgram(false);
  26218. Add([
  26219. '{$modeswitch typehelpers}',
  26220. 'type',
  26221. ' THelper = type helper for word',
  26222. ' constructor Init(e: longint);',
  26223. ' end;',
  26224. 'constructor THelper.Init(e: longint);',
  26225. 'begin',
  26226. ' Self:=e;',
  26227. ' Init(e+1);',
  26228. 'end;',
  26229. 'var w: word;',
  26230. 'begin',
  26231. ' w:=word.Init(2);',
  26232. ' w:=w.Init(3);',
  26233. ' with word do w:=Init(4);',
  26234. ' with w do w:=Init(5);',
  26235. '']);
  26236. ConvertProgram;
  26237. CheckSource('TestTypeHelper_Constructor',
  26238. LinesToStr([ // statements
  26239. 'rtl.createHelper(this, "THelper", null, function () {',
  26240. ' this.Init = function (e) {',
  26241. ' this.set(e);',
  26242. ' $mod.THelper.Init.call(this, e + 1);',
  26243. ' return this.get();',
  26244. ' };',
  26245. ' this.$new = function (fn, args) {',
  26246. ' return this[fn].apply({',
  26247. ' p: 0,',
  26248. ' get: function () {',
  26249. ' return this.p;',
  26250. ' },',
  26251. ' set: function (v) {',
  26252. ' this.p = v;',
  26253. ' }',
  26254. ' }, args);',
  26255. ' };',
  26256. '});',
  26257. 'this.w = 0;',
  26258. '']),
  26259. LinesToStr([ // $mod.$main
  26260. '$mod.w = $mod.THelper.$new("Init", [2]);',
  26261. '$mod.w = $mod.THelper.Init.call({',
  26262. ' p: $mod,',
  26263. ' get: function () {',
  26264. ' return this.p.w;',
  26265. ' },',
  26266. ' set: function (v) {',
  26267. ' this.p.w = v;',
  26268. ' }',
  26269. '}, 3);',
  26270. '$mod.w = $mod.THelper.$new("Init", [4]);',
  26271. 'var $with = $mod.w;',
  26272. '$mod.w = $mod.THelper.Init.call({',
  26273. ' get: function () {',
  26274. ' return $with;',
  26275. ' },',
  26276. ' set: function (v) {',
  26277. ' $with = v;',
  26278. ' }',
  26279. '}, 5);',
  26280. '']));
  26281. end;
  26282. procedure TTestModule.TestTypeHelper_Word;
  26283. begin
  26284. StartProgram(false);
  26285. Add([
  26286. '{$modeswitch typehelpers}',
  26287. 'type',
  26288. ' THelper = type helper for word',
  26289. ' procedure DoIt(e: byte = 123);',
  26290. ' end;',
  26291. 'procedure THelper.DoIt(e: byte);',
  26292. 'begin',
  26293. ' Self:=e;',
  26294. ' Self:=Self+1;',
  26295. ' with Self do Doit;',
  26296. 'end;',
  26297. 'begin',
  26298. ' word(3).DoIt;',
  26299. '']);
  26300. ConvertProgram;
  26301. CheckSource('TestTypeHelper_Word',
  26302. LinesToStr([ // statements
  26303. 'rtl.createHelper(this, "THelper", null, function () {',
  26304. ' this.DoIt = function (e) {',
  26305. ' this.set(e);',
  26306. ' this.set(this.get() + 1);',
  26307. ' var $with = this.get();',
  26308. ' $mod.THelper.DoIt.call(this, 123);',
  26309. ' };',
  26310. '});',
  26311. '']),
  26312. LinesToStr([ // $mod.$main
  26313. '$mod.THelper.DoIt.call({',
  26314. ' get: function () {',
  26315. ' return 3;',
  26316. ' },',
  26317. ' set: function (v) {',
  26318. ' rtl.raiseE("EPropReadOnly");',
  26319. ' }',
  26320. '}, 123);',
  26321. '']));
  26322. end;
  26323. procedure TTestModule.TestTypeHelper_Boolean;
  26324. begin
  26325. StartProgram(false);
  26326. Add([
  26327. '{$modeswitch typehelpers}',
  26328. 'type',
  26329. ' Integer = longint;',
  26330. ' THelper = type helper for boolean',
  26331. ' procedure Run(e: wordbool = true);',
  26332. ' end;',
  26333. 'procedure THelper.Run(e: wordbool);',
  26334. 'begin',
  26335. ' Self:=e;',
  26336. ' Self:=not Self;',
  26337. ' with Self do Run;',
  26338. ' if Integer(Self)=0 then ;',
  26339. 'end;',
  26340. 'begin',
  26341. ' boolean(3).Run;',
  26342. '']);
  26343. ConvertProgram;
  26344. CheckSource('TestTypeHelper_Boolean',
  26345. LinesToStr([ // statements
  26346. 'rtl.createHelper(this, "THelper", null, function () {',
  26347. ' this.Run = function (e) {',
  26348. ' this.set(e);',
  26349. ' this.set(!this.get());',
  26350. ' var $with = this.get();',
  26351. ' $mod.THelper.Run.call(this, true);',
  26352. ' if ((this.get() ? 1 : 0) === 0) ;',
  26353. ' };',
  26354. '});',
  26355. '']),
  26356. LinesToStr([ // $mod.$main
  26357. '$mod.THelper.Run.call({',
  26358. ' a: 3 != 0,',
  26359. ' get: function () {',
  26360. ' return this.a;',
  26361. ' },',
  26362. ' set: function (v) {',
  26363. ' rtl.raiseE("EPropReadOnly");',
  26364. ' }',
  26365. '}, true);',
  26366. '']));
  26367. end;
  26368. procedure TTestModule.TestTypeHelper_WordBool;
  26369. begin
  26370. StartProgram(false);
  26371. Add([
  26372. '{$modeswitch typehelpers}',
  26373. 'type',
  26374. ' Integer = longint;',
  26375. ' THelper = type helper for WordBool',
  26376. ' procedure Run(e: wordbool = true);',
  26377. ' end;',
  26378. 'procedure THelper.Run(e: wordbool);',
  26379. 'var i: integer;',
  26380. 'begin',
  26381. ' i:=Integer(Self);',
  26382. 'end;',
  26383. 'var w: wordbool;',
  26384. 'begin',
  26385. ' w.Run;',
  26386. ' wordbool(3).Run;',
  26387. '']);
  26388. ConvertProgram;
  26389. CheckSource('TestTypeHelper_WordBool',
  26390. LinesToStr([ // statements
  26391. 'rtl.createHelper(this, "THelper", null, function () {',
  26392. ' this.Run = function (e) {',
  26393. ' var i = 0;',
  26394. ' i = (this.get() ? 1 : 0);',
  26395. ' };',
  26396. '});',
  26397. 'this.w = false;',
  26398. '']),
  26399. LinesToStr([ // $mod.$main
  26400. '$mod.THelper.Run.call({',
  26401. ' p: $mod,',
  26402. ' get: function () {',
  26403. ' return this.p.w;',
  26404. ' },',
  26405. ' set: function (v) {',
  26406. ' this.p.w = v;',
  26407. ' }',
  26408. '}, true);',
  26409. '$mod.THelper.Run.call({',
  26410. ' a: 3 != 0,',
  26411. ' get: function () {',
  26412. ' return this.a;',
  26413. ' },',
  26414. ' set: function (v) {',
  26415. ' rtl.raiseE("EPropReadOnly");',
  26416. ' }',
  26417. '}, true);',
  26418. '']));
  26419. end;
  26420. procedure TTestModule.TestTypeHelper_Double;
  26421. begin
  26422. StartProgram(false);
  26423. Add([
  26424. '{$modeswitch typehelpers}',
  26425. 'type',
  26426. ' Float = type double;',
  26427. ' THelper = type helper for Float',
  26428. ' const NPI = 3.141592;',
  26429. ' function ToStr: String;',
  26430. ' end;',
  26431. 'function THelper.ToStr: String;',
  26432. 'begin',
  26433. 'end;',
  26434. 'procedure DoIt(s: string);',
  26435. 'begin',
  26436. 'end;',
  26437. 'var f: Float;',
  26438. 'begin',
  26439. ' DoIt(f.toStr);',
  26440. ' DoIt(f.toStr());',
  26441. ' (f*f).toStr;',
  26442. ' DoIt((f*f).toStr);',
  26443. '']);
  26444. ConvertProgram;
  26445. CheckSource('TestTypeHelper_Double',
  26446. LinesToStr([ // statements
  26447. 'rtl.createHelper(this, "THelper", null, function () {',
  26448. ' this.NPI = 3.141592;',
  26449. ' this.ToStr = function () {',
  26450. ' var Result = "";',
  26451. ' return Result;',
  26452. ' };',
  26453. '});',
  26454. 'this.DoIt = function (s) {',
  26455. '};',
  26456. 'this.f = 0.0;',
  26457. '']),
  26458. LinesToStr([ // $mod.$main
  26459. '$mod.DoIt($mod.THelper.ToStr.call({',
  26460. ' p: $mod,',
  26461. ' get: function () {',
  26462. ' return this.p.f;',
  26463. ' },',
  26464. ' set: function (v) {',
  26465. ' this.p.f = v;',
  26466. ' }',
  26467. '}));',
  26468. '$mod.DoIt($mod.THelper.ToStr.call({',
  26469. ' p: $mod,',
  26470. ' get: function () {',
  26471. ' return this.p.f;',
  26472. ' },',
  26473. ' set: function (v) {',
  26474. ' this.p.f = v;',
  26475. ' }',
  26476. '}));',
  26477. '$mod.THelper.ToStr.call({',
  26478. ' a: $mod.f * $mod.f,',
  26479. ' get: function () {',
  26480. ' return this.a;',
  26481. ' },',
  26482. ' set: function (v) {',
  26483. ' rtl.raiseE("EPropReadOnly");',
  26484. ' }',
  26485. '});',
  26486. '$mod.DoIt($mod.THelper.ToStr.call({',
  26487. ' a: $mod.f * $mod.f,',
  26488. ' get: function () {',
  26489. ' return this.a;',
  26490. ' },',
  26491. ' set: function (v) {',
  26492. ' rtl.raiseE("EPropReadOnly");',
  26493. ' }',
  26494. '}));',
  26495. '']));
  26496. end;
  26497. procedure TTestModule.TestTypeHelper_NativeInt;
  26498. begin
  26499. StartProgram(false);
  26500. Add([
  26501. '{$modeswitch typehelpers}',
  26502. 'type',
  26503. ' MaxInt = type nativeint;',
  26504. ' THelperI = type helper for MaxInt',
  26505. ' function ToStr: String;',
  26506. ' end;',
  26507. ' MaxUInt = type nativeuint;',
  26508. ' THelperU = type helper for MaxUInt',
  26509. ' function ToStr: String;',
  26510. ' end;',
  26511. 'function THelperI.ToStr: String;',
  26512. 'begin',
  26513. ' Result:=str(Self);',
  26514. 'end;',
  26515. 'function THelperU.ToStr: String;',
  26516. 'begin',
  26517. ' Result:=str(Self);',
  26518. 'end;',
  26519. 'procedure DoIt(s: string);',
  26520. 'begin',
  26521. 'end;',
  26522. 'var i: MaxInt;',
  26523. 'begin',
  26524. ' DoIt(i.toStr);',
  26525. ' DoIt(i.toStr());',
  26526. ' (i*i).toStr;',
  26527. ' DoIt((i*i).toStr);',
  26528. '']);
  26529. ConvertProgram;
  26530. CheckSource('TestTypeHelper_NativeInt',
  26531. LinesToStr([ // statements
  26532. 'rtl.createHelper(this, "THelperI", null, function () {',
  26533. ' this.ToStr = function () {',
  26534. ' var Result = "";',
  26535. ' Result = "" + this.get();',
  26536. ' return Result;',
  26537. ' };',
  26538. '});',
  26539. 'rtl.createHelper(this, "THelperU", null, function () {',
  26540. ' this.ToStr = function () {',
  26541. ' var Result = "";',
  26542. ' Result = "" + this.get();',
  26543. ' return Result;',
  26544. ' };',
  26545. '});',
  26546. 'this.DoIt = function (s) {',
  26547. '};',
  26548. 'this.i = 0;',
  26549. '']),
  26550. LinesToStr([ // $mod.$main
  26551. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26552. ' p: $mod,',
  26553. ' get: function () {',
  26554. ' return this.p.i;',
  26555. ' },',
  26556. ' set: function (v) {',
  26557. ' this.p.i = v;',
  26558. ' }',
  26559. '}));',
  26560. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26561. ' p: $mod,',
  26562. ' get: function () {',
  26563. ' return this.p.i;',
  26564. ' },',
  26565. ' set: function (v) {',
  26566. ' this.p.i = v;',
  26567. ' }',
  26568. '}));',
  26569. '$mod.THelperI.ToStr.call({',
  26570. ' a: $mod.i * $mod.i,',
  26571. ' get: function () {',
  26572. ' return this.a;',
  26573. ' },',
  26574. ' set: function (v) {',
  26575. ' rtl.raiseE("EPropReadOnly");',
  26576. ' }',
  26577. '});',
  26578. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26579. ' a: $mod.i * $mod.i,',
  26580. ' get: function () {',
  26581. ' return this.a;',
  26582. ' },',
  26583. ' set: function (v) {',
  26584. ' rtl.raiseE("EPropReadOnly");',
  26585. ' }',
  26586. '}));',
  26587. '']));
  26588. end;
  26589. procedure TTestModule.TestTypeHelper_StringChar;
  26590. begin
  26591. StartProgram(false);
  26592. Add([
  26593. '{$modeswitch typehelpers}',
  26594. 'type',
  26595. ' TStringHelper = type helper for string',
  26596. ' procedure DoIt(e: byte = 123);',
  26597. ' end;',
  26598. ' TCharHelper = type helper for char',
  26599. ' procedure Fly;',
  26600. ' end;',
  26601. 'procedure TStringHelper.DoIt(e: byte);',
  26602. 'begin',
  26603. ' Self[1]:=''c'';',
  26604. ' Self[2]:=Self[3];',
  26605. 'end;',
  26606. 'procedure TCharHelper.Fly;',
  26607. 'begin',
  26608. ' Self:=''c'';',
  26609. 'end;',
  26610. 'begin',
  26611. ' ''abc''.DoIt;',
  26612. ' ''xyz''.DoIt();',
  26613. ' ''c''.Fly();',
  26614. '']);
  26615. ConvertProgram;
  26616. CheckSource('TestTypeHelper_StringChar',
  26617. LinesToStr([ // statements
  26618. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26619. ' this.DoIt = function (e) {',
  26620. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26621. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26622. ' };',
  26623. '});',
  26624. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26625. ' this.Fly = function () {',
  26626. ' this.set("c");',
  26627. ' };',
  26628. '});',
  26629. '']),
  26630. LinesToStr([ // $mod.$main
  26631. '$mod.TStringHelper.DoIt.call({',
  26632. ' get: function () {',
  26633. ' return "abc";',
  26634. ' },',
  26635. ' set: function (v) {',
  26636. ' rtl.raiseE("EPropReadOnly");',
  26637. ' }',
  26638. '}, 123);',
  26639. '$mod.TStringHelper.DoIt.call({',
  26640. ' get: function () {',
  26641. ' return "xyz";',
  26642. ' },',
  26643. ' set: function (v) {',
  26644. ' rtl.raiseE("EPropReadOnly");',
  26645. ' }',
  26646. '}, 123);',
  26647. '$mod.TCharHelper.Fly.call({',
  26648. ' get: function () {',
  26649. ' return "c";',
  26650. ' },',
  26651. ' set: function (v) {',
  26652. ' rtl.raiseE("EPropReadOnly");',
  26653. ' }',
  26654. '});',
  26655. '']));
  26656. end;
  26657. procedure TTestModule.TestTypeHelper_JSValue;
  26658. begin
  26659. StartProgram(false);
  26660. Add([
  26661. '{$modeswitch typehelpers}',
  26662. 'type',
  26663. ' TExtValue = type jsvalue;',
  26664. ' THelper = type helper for TExtValue',
  26665. ' function ToStr: String;',
  26666. ' end;',
  26667. 'function THelper.ToStr: String;',
  26668. 'begin',
  26669. 'end;',
  26670. 'var',
  26671. ' s: string;',
  26672. ' v: TExtValue;',
  26673. 'begin',
  26674. ' s:=v.toStr;',
  26675. ' s:=v.toStr();',
  26676. ' TExtValue(s).toStr;',
  26677. '']);
  26678. ConvertProgram;
  26679. CheckSource('TestTypeHelper_JSValue',
  26680. LinesToStr([ // statements
  26681. 'rtl.createHelper(this, "THelper", null, function () {',
  26682. ' this.ToStr = function () {',
  26683. ' var Result = "";',
  26684. ' return Result;',
  26685. ' };',
  26686. '});',
  26687. 'this.s = "";',
  26688. 'this.v = undefined;',
  26689. '']),
  26690. LinesToStr([ // $mod.$main
  26691. '$mod.s = $mod.THelper.ToStr.call({',
  26692. ' p: $mod,',
  26693. ' get: function () {',
  26694. ' return this.p.v;',
  26695. ' },',
  26696. ' set: function (v) {',
  26697. ' this.p.v = v;',
  26698. ' }',
  26699. '});',
  26700. '$mod.s = $mod.THelper.ToStr.call({',
  26701. ' p: $mod,',
  26702. ' get: function () {',
  26703. ' return this.p.v;',
  26704. ' },',
  26705. ' set: function (v) {',
  26706. ' this.p.v = v;',
  26707. ' }',
  26708. '});',
  26709. '$mod.THelper.ToStr.call({',
  26710. ' p: $mod,',
  26711. ' get: function () {',
  26712. ' return this.p.s;',
  26713. ' },',
  26714. ' set: function (v) {',
  26715. ' rtl.raiseE("EPropReadOnly");',
  26716. ' }',
  26717. '});',
  26718. '']));
  26719. end;
  26720. procedure TTestModule.TestTypeHelper_Array;
  26721. begin
  26722. StartProgram(false);
  26723. Add([
  26724. '{$modeswitch typehelpers}',
  26725. 'type',
  26726. ' TArrOfBool = array of boolean;',
  26727. ' TArrOfJS = array of jsvalue;',
  26728. ' THelper = type helper for TArrOfBool',
  26729. ' procedure DoIt(e: byte = 123);',
  26730. ' end;',
  26731. 'procedure THelper.DoIt(e: byte);',
  26732. 'begin',
  26733. ' Self[1]:=true;',
  26734. ' Self[2]:=not Self[3];',
  26735. ' SetLength(Self,4);',
  26736. ' Self:=Concat(Self,[true]);',
  26737. 'end;',
  26738. 'var',
  26739. ' b: TArrOfBool;',
  26740. ' j: TArrOfJS;',
  26741. 'begin',
  26742. ' b.DoIt;',
  26743. ' TArrOfBool(j).DoIt();',
  26744. '']);
  26745. ConvertProgram;
  26746. CheckSource('TestTypeHelper_Array',
  26747. LinesToStr([ // statements
  26748. 'rtl.createHelper(this, "THelper", null, function () {',
  26749. ' this.DoIt = function (e) {',
  26750. ' this.get()[1] = true;',
  26751. ' this.get()[2] = !this.get()[3];',
  26752. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26753. ' this.set(rtl.arrayPushN(this.get(), true));',
  26754. ' };',
  26755. '});',
  26756. 'this.b = [];',
  26757. 'this.j = [];',
  26758. '']),
  26759. LinesToStr([ // $mod.$main
  26760. '$mod.THelper.DoIt.call({',
  26761. ' p: $mod,',
  26762. ' get: function () {',
  26763. ' return this.p.b;',
  26764. ' },',
  26765. ' set: function (v) {',
  26766. ' this.p.b = v;',
  26767. ' }',
  26768. '}, 123);',
  26769. '$mod.THelper.DoIt.call({',
  26770. ' p: $mod,',
  26771. ' get: function () {',
  26772. ' return this.p.j;',
  26773. ' },',
  26774. ' set: function (v) {',
  26775. ' this.p.j = v;',
  26776. ' }',
  26777. '}, 123);',
  26778. '']));
  26779. end;
  26780. procedure TTestModule.TestTypeHelper_EnumType;
  26781. begin
  26782. StartProgram(false);
  26783. Add([
  26784. '{$modeswitch typehelpers}',
  26785. 'type',
  26786. ' TEnum = (red,blue);',
  26787. ' THelper = type helper for TEnum',
  26788. ' procedure DoIt(e: byte = 123);',
  26789. ' class procedure Swing(w: word); static;',
  26790. ' end;',
  26791. 'procedure THelper.DoIt(e: byte);',
  26792. 'begin',
  26793. ' Self:=red;',
  26794. ' Self:=succ(Self);',
  26795. ' with Self do Doit;',
  26796. 'end;',
  26797. 'class procedure THelper.Swing(w: word);',
  26798. 'begin',
  26799. 'end;',
  26800. 'var e: TEnum;',
  26801. 'begin',
  26802. ' e.DoIt;',
  26803. ' red.DoIt;',
  26804. ' TEnum.blue.DoIt;',
  26805. ' TEnum(1).DoIt;',
  26806. ' TEnum.Swing(3);',
  26807. '']);
  26808. ConvertProgram;
  26809. CheckSource('TestTypeHelper_EnumType',
  26810. LinesToStr([ // statements
  26811. 'this.TEnum = {',
  26812. ' "0": "red",',
  26813. ' red: 0,',
  26814. ' "1": "blue",',
  26815. ' blue: 1',
  26816. '};',
  26817. 'rtl.createHelper(this, "THelper", null, function () {',
  26818. ' this.DoIt = function (e) {',
  26819. ' this.set($mod.TEnum.red);',
  26820. ' this.set(this.get() + 1);',
  26821. ' var $with = this.get();',
  26822. ' $mod.THelper.DoIt.call(this, 123);',
  26823. ' };',
  26824. ' this.Swing = function (w) {',
  26825. ' };',
  26826. '});',
  26827. 'this.e = 0;',
  26828. '']),
  26829. LinesToStr([ // $mod.$main
  26830. '$mod.THelper.DoIt.call({',
  26831. ' p: $mod,',
  26832. ' get: function () {',
  26833. ' return this.p.e;',
  26834. ' },',
  26835. ' set: function (v) {',
  26836. ' this.p.e = v;',
  26837. ' }',
  26838. '}, 123);',
  26839. '$mod.THelper.DoIt.call({',
  26840. ' p: $mod.TEnum,',
  26841. ' get: function () {',
  26842. ' return this.p.red;',
  26843. ' },',
  26844. ' set: function (v) {',
  26845. ' rtl.raiseE("EPropReadOnly");',
  26846. ' }',
  26847. '}, 123);',
  26848. '$mod.THelper.DoIt.call({',
  26849. ' p: $mod.TEnum,',
  26850. ' get: function () {',
  26851. ' return this.p.blue;',
  26852. ' },',
  26853. ' set: function (v) {',
  26854. ' rtl.raiseE("EPropReadOnly");',
  26855. ' }',
  26856. '}, 123);',
  26857. '$mod.THelper.DoIt.call({',
  26858. ' get: function () {',
  26859. ' return 1;',
  26860. ' },',
  26861. ' set: function (v) {',
  26862. ' rtl.raiseE("EPropReadOnly");',
  26863. ' }',
  26864. '}, 123);',
  26865. '$mod.THelper.Swing(3);',
  26866. '']));
  26867. end;
  26868. procedure TTestModule.TestTypeHelper_SetType;
  26869. begin
  26870. StartProgram(false);
  26871. Add([
  26872. '{$modeswitch typehelpers}',
  26873. 'type',
  26874. ' TEnum = (red,blue);',
  26875. ' TSetOfEnum = set of TEnum;',
  26876. ' THelper = type helper for TSetOfEnum',
  26877. ' procedure DoIt(e: byte = 123);',
  26878. ' constructor Init(e: TEnum);',
  26879. ' constructor InitEmpty;',
  26880. ' end;',
  26881. 'procedure THelper.DoIt(e: byte);',
  26882. 'begin',
  26883. ' Self:=[];',
  26884. ' Self:=[red];',
  26885. ' Include(Self,blue);',
  26886. 'end;',
  26887. 'constructor THelper.Init(e: TEnum);',
  26888. 'begin',
  26889. ' Self:=[];',
  26890. ' Self:=[e];',
  26891. ' Include(Self,blue);',
  26892. 'end;',
  26893. 'constructor THelper.InitEmpty;',
  26894. 'begin',
  26895. 'end;',
  26896. 'var s: TSetOfEnum;',
  26897. 'begin',
  26898. ' s.DoIt;',
  26899. //' [red].DoIt;',
  26900. //' with s do DoIt;',
  26901. //' with [red,blue] do DoIt;',
  26902. ' s:=TSetOfEnum.Init(blue);',
  26903. ' s:=s.Init(blue);',
  26904. '']);
  26905. ConvertProgram;
  26906. CheckSource('TestTypeHelper_SetType',
  26907. LinesToStr([ // statements
  26908. 'this.TEnum = {',
  26909. ' "0": "red",',
  26910. ' red: 0,',
  26911. ' "1": "blue",',
  26912. ' blue: 1',
  26913. '};',
  26914. 'rtl.createHelper(this, "THelper", null, function () {',
  26915. ' this.DoIt = function (e) {',
  26916. ' this.set({});',
  26917. ' this.set(rtl.createSet($mod.TEnum.red));',
  26918. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26919. ' };',
  26920. ' this.Init = function (e) {',
  26921. ' this.set({});',
  26922. ' this.set(rtl.createSet(e));',
  26923. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26924. ' return this.get();',
  26925. ' };',
  26926. ' this.InitEmpty = function () {',
  26927. ' return this.get();',
  26928. ' };',
  26929. ' this.$new = function (fn, args) {',
  26930. ' return this[fn].apply({',
  26931. ' p: {},',
  26932. ' get: function () {',
  26933. ' return this.p;',
  26934. ' },',
  26935. ' set: function (v) {',
  26936. ' this.p = v;',
  26937. ' }',
  26938. ' }, args);',
  26939. ' };',
  26940. '});',
  26941. 'this.s = {};',
  26942. '']),
  26943. LinesToStr([ // $mod.$main
  26944. '$mod.THelper.DoIt.call({',
  26945. ' p: $mod,',
  26946. ' get: function () {',
  26947. ' return this.p.s;',
  26948. ' },',
  26949. ' set: function (v) {',
  26950. ' this.p.s = v;',
  26951. ' }',
  26952. '}, 123);',
  26953. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  26954. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  26955. ' p: $mod,',
  26956. ' get: function () {',
  26957. ' return this.p.s;',
  26958. ' },',
  26959. ' set: function (v) {',
  26960. ' this.p.s = v;',
  26961. ' }',
  26962. '}, $mod.TEnum.blue));',
  26963. '']));
  26964. end;
  26965. procedure TTestModule.TestTypeHelper_InterfaceType;
  26966. begin
  26967. StartProgram(false);
  26968. Add([
  26969. '{$interfaces com}',
  26970. '{$modeswitch typehelpers}',
  26971. 'type',
  26972. ' IUnknown = interface',
  26973. ' function _AddRef: longint;',
  26974. ' function _Release: longint;',
  26975. ' end;',
  26976. ' TObject = class(IUnknown)',
  26977. ' function _AddRef: longint; virtual; abstract;',
  26978. ' function _Release: longint; virtual; abstract;',
  26979. ' end;',
  26980. ' THelper = type helper for IUnknown',
  26981. ' procedure Fly(e: byte = 123);',
  26982. ' class procedure Run; static;',
  26983. ' end;',
  26984. 'var',
  26985. ' i: IUnknown;',
  26986. ' o: TObject;',
  26987. 'procedure THelper.Fly(e: byte);',
  26988. 'begin',
  26989. ' i:=Self;',
  26990. ' o:=Self as TObject;',
  26991. ' Self:=nil;',
  26992. ' Self:=i;',
  26993. ' Self:=o;',
  26994. ' with Self do begin',
  26995. ' Fly;',
  26996. ' Fly();',
  26997. ' end;',
  26998. 'end;',
  26999. 'class procedure THelper.Run;',
  27000. 'var l: IUnknown;',
  27001. 'begin',
  27002. ' l.Fly;',
  27003. ' l.Fly();',
  27004. 'end;',
  27005. 'begin',
  27006. ' i.Fly;',
  27007. ' i.Fly();',
  27008. ' i.Run;',
  27009. ' i.Run();',
  27010. ' IUnknown.Run;',
  27011. ' IUnknown.Run();',
  27012. '']);
  27013. ConvertProgram;
  27014. CheckSource('TestTypeHelper_InterfaceType',
  27015. LinesToStr([ // statements
  27016. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27017. 'rtl.createClass(this, "TObject", null, function () {',
  27018. ' this.$init = function () {',
  27019. ' };',
  27020. ' this.$final = function () {',
  27021. ' };',
  27022. ' rtl.addIntf(this, $mod.IUnknown);',
  27023. '});',
  27024. 'rtl.createHelper(this, "THelper", null, function () {',
  27025. ' this.Fly = function (e) {',
  27026. ' var $ir = rtl.createIntfRefs();',
  27027. ' try {',
  27028. ' rtl.setIntfP($mod, "i", this.get());',
  27029. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27030. ' this.set(null);',
  27031. ' this.set($mod.i);',
  27032. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27033. ' var $with = this.get();',
  27034. ' $mod.THelper.Fly.call(this, 123);',
  27035. ' $mod.THelper.Fly.call(this, 123);',
  27036. ' } finally {',
  27037. ' $ir.free();',
  27038. ' };',
  27039. ' };',
  27040. ' this.Run = function () {',
  27041. ' var l = null;',
  27042. ' try {',
  27043. ' $mod.THelper.Fly.call({',
  27044. ' get: function () {',
  27045. ' return l;',
  27046. ' },',
  27047. ' set: function (v) {',
  27048. ' l = rtl.setIntfL(l, v);',
  27049. ' }',
  27050. ' }, 123);',
  27051. ' $mod.THelper.Fly.call({',
  27052. ' get: function () {',
  27053. ' return l;',
  27054. ' },',
  27055. ' set: function (v) {',
  27056. ' l = rtl.setIntfL(l, v);',
  27057. ' }',
  27058. ' }, 123);',
  27059. ' } finally {',
  27060. ' rtl._Release(l);',
  27061. ' };',
  27062. ' };',
  27063. '});',
  27064. 'this.i = null;',
  27065. 'this.o = null;',
  27066. '']),
  27067. LinesToStr([ // $mod.$main
  27068. '$mod.THelper.Fly.call({',
  27069. ' p: $mod,',
  27070. ' get: function () {',
  27071. ' return this.p.i;',
  27072. ' },',
  27073. ' set: function (v) {',
  27074. ' rtl.setIntfP(this.p, "i", v);',
  27075. ' }',
  27076. '}, 123);',
  27077. '$mod.THelper.Fly.call({',
  27078. ' p: $mod,',
  27079. ' get: function () {',
  27080. ' return this.p.i;',
  27081. ' },',
  27082. ' set: function (v) {',
  27083. ' rtl.setIntfP(this.p, "i", v);',
  27084. ' }',
  27085. '}, 123);',
  27086. '$mod.THelper.Run();',
  27087. '$mod.THelper.Run();',
  27088. '$mod.THelper.Run();',
  27089. '$mod.THelper.Run();',
  27090. '']));
  27091. end;
  27092. procedure TTestModule.TestTypeHelper_NestedSelf;
  27093. begin
  27094. StartProgram(false);
  27095. Add([
  27096. '{$modeswitch typehelpers}',
  27097. 'type',
  27098. ' THelper = type helper for string',
  27099. ' procedure Run(Value: string);',
  27100. ' end;',
  27101. 'procedure THelper.Run(Value: string);',
  27102. ' function Sub(i: nativeint): boolean;',
  27103. ' begin',
  27104. ' Result:=Self[i+1]=Value[i];',
  27105. ' end;',
  27106. 'begin',
  27107. ' if Self[3]=Value[4] then ;',
  27108. 'end;',
  27109. 'begin',
  27110. '']);
  27111. ConvertProgram;
  27112. CheckSource('TestTypeHelper_NestedSelf',
  27113. LinesToStr([ // statements
  27114. 'rtl.createHelper(this, "THelper", null, function () {',
  27115. ' this.Run = function (Value) {',
  27116. ' var $Self = this;',
  27117. ' function Sub(i) {',
  27118. ' var Result = false;',
  27119. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27120. ' return Result;',
  27121. ' };',
  27122. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27123. ' };',
  27124. '});',
  27125. '']),
  27126. LinesToStr([ // $mod.$main
  27127. '']));
  27128. end;
  27129. procedure TTestModule.TestProcType;
  27130. begin
  27131. StartProgram(false);
  27132. Add([
  27133. 'type',
  27134. ' TProcInt = procedure(vI: longint = 1);',
  27135. 'procedure DoIt(vJ: longint);',
  27136. 'begin end;',
  27137. 'var',
  27138. ' b: boolean;',
  27139. ' vP, vQ: tprocint;',
  27140. 'begin',
  27141. ' vp:=nil;',
  27142. ' vp:=vp;',
  27143. ' vp:=@doit;',
  27144. ' vp;',
  27145. ' vp();',
  27146. ' vp(2);',
  27147. ' b:=vp=nil;',
  27148. ' b:=nil=vp;',
  27149. ' b:=vp=vq;',
  27150. ' b:=vp=@doit;',
  27151. ' b:=@doit=vp;',
  27152. ' b:=vp<>nil;',
  27153. ' b:=nil<>vp;',
  27154. ' b:=vp<>vq;',
  27155. ' b:=vp<>@doit;',
  27156. ' b:=@doit<>vp;',
  27157. ' b:=Assigned(vp);',
  27158. ' if Assigned(vp) then ;']);
  27159. ConvertProgram;
  27160. CheckSource('TestProcType',
  27161. LinesToStr([ // statements
  27162. 'this.DoIt = function(vJ) {',
  27163. '};',
  27164. 'this.b = false;',
  27165. 'this.vP = null;',
  27166. 'this.vQ = null;'
  27167. ]),
  27168. LinesToStr([ // $mod.$main
  27169. '$mod.vP = null;',
  27170. '$mod.vP = $mod.vP;',
  27171. '$mod.vP = $mod.DoIt;',
  27172. '$mod.vP(1);',
  27173. '$mod.vP(1);',
  27174. '$mod.vP(2);',
  27175. '$mod.b = $mod.vP === null;',
  27176. '$mod.b = null === $mod.vP;',
  27177. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27178. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27179. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27180. '$mod.b = $mod.vP !== null;',
  27181. '$mod.b = null !== $mod.vP;',
  27182. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27183. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27184. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27185. '$mod.b = $mod.vP != null;',
  27186. 'if ($mod.vP != null) ;',
  27187. '']));
  27188. end;
  27189. procedure TTestModule.TestProcType_Arg;
  27190. begin
  27191. StartProgram(false);
  27192. Add([
  27193. 'type',
  27194. ' TProcInt = procedure(vI: longint = 1);',
  27195. 'procedure DoIt(vJ: longint); begin end;',
  27196. 'procedure DoSome(vP, vQ: TProcInt);',
  27197. 'var',
  27198. ' b: boolean;',
  27199. 'begin',
  27200. ' vp:=nil;',
  27201. ' vp:=vp;',
  27202. ' vp:=@doit;',
  27203. ' vp;',
  27204. ' vp();',
  27205. ' vp(2);',
  27206. ' b:=vp=nil;',
  27207. ' b:=nil=vp;',
  27208. ' b:=vp=vq;',
  27209. ' b:=vp=@doit;',
  27210. ' b:=@doit=vp;',
  27211. ' b:=vp<>nil;',
  27212. ' b:=nil<>vp;',
  27213. ' b:=vp<>vq;',
  27214. ' b:=vp<>@doit;',
  27215. ' b:=@doit<>vp;',
  27216. ' b:=Assigned(vp);',
  27217. ' if Assigned(vp) then ;',
  27218. 'end;',
  27219. 'begin',
  27220. ' DoSome(@DoIt,nil);']);
  27221. ConvertProgram;
  27222. CheckSource('TestProcType_Arg',
  27223. LinesToStr([ // statements
  27224. 'this.DoIt = function(vJ) {',
  27225. '};',
  27226. 'this.DoSome = function(vP, vQ) {',
  27227. ' var b = false;',
  27228. ' vP = null;',
  27229. ' vP = vP;',
  27230. ' vP = $mod.DoIt;',
  27231. ' vP(1);',
  27232. ' vP(1);',
  27233. ' vP(2);',
  27234. ' b = vP === null;',
  27235. ' b = null === vP;',
  27236. ' b = rtl.eqCallback(vP,vQ);',
  27237. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  27238. ' b = rtl.eqCallback($mod.DoIt, vP);',
  27239. ' b = vP !== null;',
  27240. ' b = null !== vP;',
  27241. ' b = !rtl.eqCallback(vP, vQ);',
  27242. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  27243. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  27244. ' b = vP != null;',
  27245. ' if (vP != null) ;',
  27246. '};',
  27247. '']),
  27248. LinesToStr([ // $mod.$main
  27249. '$mod.DoSome($mod.DoIt,null);',
  27250. '']));
  27251. end;
  27252. procedure TTestModule.TestProcType_FunctionFPC;
  27253. begin
  27254. StartProgram(false);
  27255. Add('type');
  27256. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27257. Add('function DoIt(vI: longint): longint;');
  27258. Add('begin end;');
  27259. Add('var');
  27260. Add(' b: boolean;');
  27261. Add(' vP, vQ: tfuncint;');
  27262. Add('begin');
  27263. Add(' vp:=nil;');
  27264. Add(' vp:=vp;');
  27265. Add(' vp:=@doit;'); // ok in fpc and delphi
  27266. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27267. Add(' vp;'); // ok in fpc and delphi
  27268. Add(' vp();');
  27269. Add(' vp(2);');
  27270. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27271. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27272. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27273. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27274. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27275. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27276. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27277. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27278. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27279. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27280. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27281. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27282. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27283. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27284. Add(' b:=Assigned(vp);');
  27285. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27286. Add(' doit(vp());'); // ok in fpc and delphi
  27287. Add(' doit(vp(2));'); // ok in fpc and delphi
  27288. ConvertProgram;
  27289. CheckSource('TestProcType_FunctionFPC',
  27290. LinesToStr([ // statements
  27291. 'this.DoIt = function(vI) {',
  27292. ' var Result = 0;',
  27293. ' return Result;',
  27294. '};',
  27295. 'this.b = false;',
  27296. 'this.vP = null;',
  27297. 'this.vQ = null;'
  27298. ]),
  27299. LinesToStr([ // $mod.$main
  27300. '$mod.vP = null;',
  27301. '$mod.vP = $mod.vP;',
  27302. '$mod.vP = $mod.DoIt;',
  27303. '$mod.vP(1);',
  27304. '$mod.vP(1);',
  27305. '$mod.vP(2);',
  27306. '$mod.b = $mod.vP === null;',
  27307. '$mod.b = null === $mod.vP;',
  27308. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27309. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27310. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27311. '$mod.b = 4 === $mod.vP(1);',
  27312. '$mod.b = $mod.vP !== null;',
  27313. '$mod.b = null !== $mod.vP;',
  27314. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27315. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27316. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27317. '$mod.b = 6 !== $mod.vP(1);',
  27318. '$mod.b = $mod.vP != null;',
  27319. '$mod.DoIt($mod.vP(1));',
  27320. '$mod.DoIt($mod.vP(2));',
  27321. '']));
  27322. end;
  27323. procedure TTestModule.TestProcType_FunctionDelphi;
  27324. begin
  27325. StartProgram(false);
  27326. Add('{$mode Delphi}');
  27327. Add('type');
  27328. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27329. Add('function DoIt(vI: longint): longint;');
  27330. Add('begin end;');
  27331. Add('var');
  27332. Add(' b: boolean;');
  27333. Add(' vP, vQ: tfuncint;');
  27334. Add('begin');
  27335. Add(' vp:=nil;');
  27336. Add(' vp:=vp;');
  27337. Add(' vp:=@doit;'); // ok in fpc and delphi
  27338. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27339. Add(' vp;'); // ok in fpc and delphi
  27340. Add(' vp();');
  27341. Add(' vp(2);');
  27342. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27343. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27344. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27345. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27346. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27347. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27348. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27349. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27350. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27351. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27352. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27353. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27354. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27355. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27356. Add(' b:=Assigned(vp);');
  27357. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27358. Add(' doit(vp());'); // ok in fpc and delphi
  27359. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27360. ConvertProgram;
  27361. CheckSource('TestProcType_FunctionDelphi',
  27362. LinesToStr([ // statements
  27363. 'this.DoIt = function(vI) {',
  27364. ' var Result = 0;',
  27365. ' return Result;',
  27366. '};',
  27367. 'this.b = false;',
  27368. 'this.vP = null;',
  27369. 'this.vQ = null;'
  27370. ]),
  27371. LinesToStr([ // $mod.$main
  27372. '$mod.vP = null;',
  27373. '$mod.vP = $mod.vP;',
  27374. '$mod.vP = $mod.DoIt;',
  27375. '$mod.vP = $mod.DoIt;',
  27376. '$mod.vP(1);',
  27377. '$mod.vP(1);',
  27378. '$mod.vP(2);',
  27379. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27380. '$mod.b = $mod.vP(1) === 3;',
  27381. '$mod.b = 4 === $mod.vP(1);',
  27382. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27383. '$mod.b = $mod.vP(1) !== 5;',
  27384. '$mod.b = 6 !== $mod.vP(1);',
  27385. '$mod.b = $mod.vP != null;',
  27386. '$mod.DoIt($mod.vP(1));',
  27387. '$mod.DoIt($mod.vP(1));',
  27388. '$mod.DoIt($mod.vP(2));',
  27389. '']));
  27390. end;
  27391. procedure TTestModule.TestProcType_ProcedureDelphi;
  27392. begin
  27393. StartProgram(false);
  27394. Add('{$mode Delphi}');
  27395. Add('type');
  27396. Add(' TProc = procedure;');
  27397. Add('procedure DoIt;');
  27398. Add('begin end;');
  27399. Add('var');
  27400. Add(' b: boolean;');
  27401. Add(' vP, vQ: tproc;');
  27402. Add('begin');
  27403. Add(' vp:=nil;');
  27404. Add(' vp:=vp;');
  27405. Add(' vp:=vq;');
  27406. 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
  27407. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27408. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27409. Add(' vp;'); // ok in fpc and delphi
  27410. Add(' vp();');
  27411. // equal
  27412. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27413. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27414. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27415. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27416. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27417. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27418. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27419. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27420. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27421. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27422. // unequal
  27423. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27424. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27425. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27426. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27427. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27428. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27429. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27430. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27431. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27432. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27433. Add(' b:=Assigned(vp);');
  27434. ConvertProgram;
  27435. CheckSource('TestProcType_ProcedureDelphi',
  27436. LinesToStr([ // statements
  27437. 'this.DoIt = function() {',
  27438. '};',
  27439. 'this.b = false;',
  27440. 'this.vP = null;',
  27441. 'this.vQ = null;'
  27442. ]),
  27443. LinesToStr([ // $mod.$main
  27444. '$mod.vP = null;',
  27445. '$mod.vP = $mod.vP;',
  27446. '$mod.vP = $mod.vQ;',
  27447. '$mod.vP = $mod.DoIt;',
  27448. '$mod.vP = $mod.DoIt;',
  27449. '$mod.vP();',
  27450. '$mod.vP();',
  27451. '$mod.b = $mod.vP === null;',
  27452. '$mod.b = null === $mod.vP;',
  27453. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27454. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27455. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27456. '$mod.b = $mod.vP !== null;',
  27457. '$mod.b = null !== $mod.vP;',
  27458. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27459. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27460. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27461. '$mod.b = $mod.vP != null;',
  27462. '']));
  27463. end;
  27464. procedure TTestModule.TestProcType_AsParam;
  27465. begin
  27466. StartProgram(false);
  27467. Add('type');
  27468. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27469. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27470. Add('var vJ: tfuncint;');
  27471. Add('begin');
  27472. Add(' vg:=vg;');
  27473. Add(' vj:=vh;');
  27474. Add(' vi:=vi;');
  27475. Add(' doit(vg,vg,vg);');
  27476. Add(' doit(vh,vh,vj);');
  27477. Add(' doit(vi,vi,vi);');
  27478. Add(' doit(vj,vj,vj);');
  27479. Add('end;');
  27480. Add('var i: tfuncint;');
  27481. Add('begin');
  27482. Add(' doit(i,i,i);');
  27483. ConvertProgram;
  27484. CheckSource('TestProcType_AsParam',
  27485. LinesToStr([ // statements
  27486. 'this.DoIt = function (vG,vH,vI) {',
  27487. ' var vJ = null;',
  27488. ' vG = vG;',
  27489. ' vJ = vH;',
  27490. ' vI.set(vI.get());',
  27491. ' $mod.DoIt(vG, vG, {',
  27492. ' get: function () {',
  27493. ' return vG;',
  27494. ' },',
  27495. ' set: function (v) {',
  27496. ' vG = v;',
  27497. ' }',
  27498. ' });',
  27499. ' $mod.DoIt(vH, vH, {',
  27500. ' get: function () {',
  27501. ' return vJ;',
  27502. ' },',
  27503. ' set: function (v) {',
  27504. ' vJ = v;',
  27505. ' }',
  27506. ' });',
  27507. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27508. ' $mod.DoIt(vJ, vJ, {',
  27509. ' get: function () {',
  27510. ' return vJ;',
  27511. ' },',
  27512. ' set: function (v) {',
  27513. ' vJ = v;',
  27514. ' }',
  27515. ' });',
  27516. '};',
  27517. 'this.i = null;'
  27518. ]),
  27519. LinesToStr([
  27520. '$mod.DoIt($mod.i,$mod.i,{',
  27521. ' p: $mod,',
  27522. ' get: function () {',
  27523. ' return this.p.i;',
  27524. ' },',
  27525. ' set: function (v) {',
  27526. ' this.p.i = v;',
  27527. ' }',
  27528. '});'
  27529. ]));
  27530. end;
  27531. procedure TTestModule.TestProcType_MethodFPC;
  27532. begin
  27533. StartProgram(false);
  27534. Add('type');
  27535. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27536. Add(' TObject = class');
  27537. Add(' function DoIt(vA: longint = 1): longint;');
  27538. Add(' end;');
  27539. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27540. Add('begin');
  27541. Add('end;');
  27542. Add('var');
  27543. Add(' Obj: TObject;');
  27544. Add(' vP: tfuncint;');
  27545. Add(' b: boolean;');
  27546. Add('begin');
  27547. Add(' vp:[email protected];'); // ok in fpc and delphi
  27548. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27549. Add(' vp;'); // ok in fpc and delphi
  27550. Add(' vp();');
  27551. Add(' vp(2);');
  27552. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27553. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27554. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27555. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27556. ConvertProgram;
  27557. CheckSource('TestProcType_MethodFPC',
  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(1);',
  27576. '$mod.vP(1);',
  27577. '$mod.vP(2);',
  27578. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27579. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27580. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27581. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27582. '']));
  27583. end;
  27584. procedure TTestModule.TestProcType_MethodDelphi;
  27585. begin
  27586. StartProgram(false);
  27587. Add([
  27588. '{$mode delphi}',
  27589. 'type',
  27590. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27591. ' TObject = class',
  27592. ' function DoIt(vA: longint = 1): longint;',
  27593. ' end;',
  27594. 'function TObject.DoIt(vA: longint = 1): longint;',
  27595. 'begin',
  27596. 'end;',
  27597. 'var',
  27598. ' Obj: TObject;',
  27599. ' vP: tfuncint;',
  27600. ' b: boolean;',
  27601. 'begin',
  27602. ' vp:[email protected];', // ok in fpc and delphi
  27603. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27604. ' vp;', // ok in fpc and delphi
  27605. ' vp();',
  27606. ' vp(2);',
  27607. //' b:[email protected];', // ok in fpc, illegal in delphi
  27608. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27609. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27610. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27611. '']);
  27612. ConvertProgram;
  27613. CheckSource('TestProcType_MethodDelphi',
  27614. LinesToStr([ // statements
  27615. 'rtl.createClass(this, "TObject", null, function () {',
  27616. ' this.$init = function () {',
  27617. ' };',
  27618. ' this.$final = function () {',
  27619. ' };',
  27620. ' this.DoIt = function (vA) {',
  27621. ' var Result = 0;',
  27622. ' return Result;',
  27623. ' };',
  27624. '});',
  27625. 'this.Obj = null;',
  27626. 'this.vP = null;',
  27627. 'this.b = false;'
  27628. ]),
  27629. LinesToStr([
  27630. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27631. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27632. '$mod.vP(1);',
  27633. '$mod.vP(1);',
  27634. '$mod.vP(2);',
  27635. '']));
  27636. end;
  27637. procedure TTestModule.TestProcType_PropertyFPC;
  27638. begin
  27639. StartProgram(false);
  27640. Add('type');
  27641. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27642. Add(' TObject = class');
  27643. Add(' FOnFoo: TFuncInt;');
  27644. Add(' function DoIt(vA: longint = 1): longint;');
  27645. Add(' function GetFoo: TFuncInt;');
  27646. Add(' procedure SetFoo(const Value: TFuncInt);');
  27647. Add(' function GetEvents(Index: longint): TFuncInt;');
  27648. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27649. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27650. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27651. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27652. Add(' end;');
  27653. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27654. Add('function tobject.getfoo: tfuncint; begin end;');
  27655. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27656. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27657. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27658. Add('var');
  27659. Add(' Obj: TObject;');
  27660. Add(' vP: tfuncint;');
  27661. Add(' b: boolean;');
  27662. Add('begin');
  27663. Add(' obj.onfoo:=nil;');
  27664. Add(' obj.onbar:=nil;');
  27665. Add(' obj.events[1]:=nil;');
  27666. Add(' obj.onfoo:=obj.onfoo;');
  27667. Add(' obj.onbar:=obj.onbar;');
  27668. Add(' obj.events[2]:=obj.events[3];');
  27669. Add(' obj.onfoo:[email protected];');
  27670. Add(' obj.onbar:[email protected];');
  27671. Add(' obj.events[4]:[email protected];');
  27672. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27673. //Add(' obj.onbar:=obj.doit;'); // delphi
  27674. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27675. Add(' obj.onfoo;');
  27676. Add(' obj.onbar;');
  27677. //Add(' obj.events[5];'); ToDo in pasresolver
  27678. Add(' obj.onfoo();');
  27679. Add(' obj.onbar();');
  27680. Add(' obj.events[6]();');
  27681. Add(' b:=obj.onfoo=nil;');
  27682. Add(' b:=obj.onbar=nil;');
  27683. Add(' b:=obj.events[7]=nil;');
  27684. Add(' b:=obj.onfoo<>nil;');
  27685. Add(' b:=obj.onbar<>nil;');
  27686. Add(' b:=obj.events[8]<>nil;');
  27687. Add(' b:=obj.onfoo=vp;');
  27688. Add(' b:=obj.onbar=vp;');
  27689. Add(' b:=obj.events[9]=vp;');
  27690. Add(' b:=obj.onfoo=obj.onfoo;');
  27691. Add(' b:=obj.onbar=obj.onfoo;');
  27692. Add(' b:=obj.events[10]=obj.onfoo;');
  27693. Add(' b:=obj.onfoo<>obj.onfoo;');
  27694. Add(' b:=obj.onbar<>obj.onfoo;');
  27695. Add(' b:=obj.events[11]<>obj.onfoo;');
  27696. Add(' b:[email protected];');
  27697. Add(' b:[email protected];');
  27698. Add(' b:=obj.events[12][email protected];');
  27699. Add(' b:=obj.onfoo<>@obj.doit;');
  27700. Add(' b:=obj.onbar<>@obj.doit;');
  27701. Add(' b:=obj.events[12]<>@obj.doit;');
  27702. Add(' b:=Assigned(obj.onfoo);');
  27703. Add(' b:=Assigned(obj.onbar);');
  27704. Add(' b:=Assigned(obj.events[13]);');
  27705. ConvertProgram;
  27706. CheckSource('TestProcType_PropertyFPC',
  27707. LinesToStr([ // statements
  27708. 'rtl.createClass(this, "TObject", null, function () {',
  27709. ' this.$init = function () {',
  27710. ' this.FOnFoo = null;',
  27711. ' };',
  27712. ' this.$final = function () {',
  27713. ' this.FOnFoo = undefined;',
  27714. ' };',
  27715. ' this.DoIt = function (vA) {',
  27716. ' var Result = 0;',
  27717. ' return Result;',
  27718. ' };',
  27719. 'this.GetFoo = function () {',
  27720. ' var Result = null;',
  27721. ' return Result;',
  27722. '};',
  27723. 'this.SetFoo = function (Value) {',
  27724. '};',
  27725. 'this.GetEvents = function (Index) {',
  27726. ' var Result = null;',
  27727. ' return Result;',
  27728. '};',
  27729. 'this.SetEvents = function (Index, Value) {',
  27730. '};',
  27731. '});',
  27732. 'this.Obj = null;',
  27733. 'this.vP = null;',
  27734. 'this.b = false;'
  27735. ]),
  27736. LinesToStr([
  27737. '$mod.Obj.FOnFoo = null;',
  27738. '$mod.Obj.SetFoo(null);',
  27739. '$mod.Obj.SetEvents(1, null);',
  27740. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27741. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27742. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27743. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27744. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27745. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27746. '$mod.Obj.FOnFoo(1);',
  27747. '$mod.Obj.GetFoo();',
  27748. '$mod.Obj.FOnFoo(1);',
  27749. '$mod.Obj.GetFoo()(1);',
  27750. '$mod.Obj.GetEvents(6)(1);',
  27751. '$mod.b = $mod.Obj.FOnFoo === null;',
  27752. '$mod.b = $mod.Obj.GetFoo() === null;',
  27753. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27754. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27755. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27756. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27757. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27758. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27759. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27760. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27761. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27762. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27763. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27764. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27765. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27766. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27767. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27768. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27769. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27770. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27771. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27772. '$mod.b = $mod.Obj.FOnFoo != null;',
  27773. '$mod.b = $mod.Obj.GetFoo() != null;',
  27774. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27775. '']));
  27776. end;
  27777. procedure TTestModule.TestProcType_PropertyDelphi;
  27778. begin
  27779. StartProgram(false);
  27780. Add('{$mode delphi}');
  27781. Add('type');
  27782. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27783. Add(' TObject = class');
  27784. Add(' FOnFoo: TFuncInt;');
  27785. Add(' function DoIt(vA: longint = 1): longint;');
  27786. Add(' function GetFoo: TFuncInt;');
  27787. Add(' procedure SetFoo(const Value: TFuncInt);');
  27788. Add(' function GetEvents(Index: longint): TFuncInt;');
  27789. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27790. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27791. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27792. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27793. Add(' end;');
  27794. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27795. Add('function tobject.getfoo: tfuncint; begin end;');
  27796. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27797. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27798. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27799. Add('var');
  27800. Add(' Obj: TObject;');
  27801. Add(' vP: tfuncint;');
  27802. Add(' b: boolean;');
  27803. Add('begin');
  27804. Add(' obj.onfoo:=nil;');
  27805. Add(' obj.onbar:=nil;');
  27806. Add(' obj.events[1]:=nil;');
  27807. Add(' obj.onfoo:=obj.onfoo;');
  27808. Add(' obj.onbar:=obj.onbar;');
  27809. Add(' obj.events[2]:=obj.events[3];');
  27810. Add(' obj.onfoo:[email protected];');
  27811. Add(' obj.onbar:[email protected];');
  27812. Add(' obj.events[4]:[email protected];');
  27813. Add(' obj.onfoo:=obj.doit;'); // delphi
  27814. Add(' obj.onbar:=obj.doit;'); // delphi
  27815. Add(' obj.events[4]:=obj.doit;'); // delphi
  27816. Add(' obj.onfoo;');
  27817. Add(' obj.onbar;');
  27818. //Add(' obj.events[5];'); ToDo in pasresolver
  27819. Add(' obj.onfoo();');
  27820. Add(' obj.onbar();');
  27821. Add(' obj.events[6]();');
  27822. //Add(' b:=obj.onfoo=nil;'); // fpc
  27823. //Add(' b:=obj.onbar=nil;'); // fpc
  27824. //Add(' b:=obj.events[7]=nil;'); // fpc
  27825. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27826. //Add(' b:=obj.onbar<>nil;'); // fpc
  27827. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27828. Add(' b:=obj.onfoo=vp;');
  27829. Add(' b:=obj.onbar=vp;');
  27830. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27831. Add(' b:=obj.onfoo=obj.onfoo;');
  27832. Add(' b:=obj.onbar=obj.onfoo;');
  27833. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27834. Add(' b:=obj.onfoo<>obj.onfoo;');
  27835. Add(' b:=obj.onbar<>obj.onfoo;');
  27836. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27837. //Add(' b:[email protected];'); // fpc
  27838. //Add(' b:[email protected];'); // fpc
  27839. //Add(' b:=obj.events[12][email protected];'); // fpc
  27840. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27841. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27842. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27843. Add(' b:=Assigned(obj.onfoo);');
  27844. Add(' b:=Assigned(obj.onbar);');
  27845. Add(' b:=Assigned(obj.events[13]);');
  27846. ConvertProgram;
  27847. CheckSource('TestProcType_PropertyDelphi',
  27848. LinesToStr([ // statements
  27849. 'rtl.createClass(this, "TObject", null, function () {',
  27850. ' this.$init = function () {',
  27851. ' this.FOnFoo = null;',
  27852. ' };',
  27853. ' this.$final = function () {',
  27854. ' this.FOnFoo = undefined;',
  27855. ' };',
  27856. ' this.DoIt = function (vA) {',
  27857. ' var Result = 0;',
  27858. ' return Result;',
  27859. ' };',
  27860. 'this.GetFoo = function () {',
  27861. ' var Result = null;',
  27862. ' return Result;',
  27863. '};',
  27864. 'this.SetFoo = function (Value) {',
  27865. '};',
  27866. 'this.GetEvents = function (Index) {',
  27867. ' var Result = null;',
  27868. ' return Result;',
  27869. '};',
  27870. 'this.SetEvents = function (Index, Value) {',
  27871. '};',
  27872. '});',
  27873. 'this.Obj = null;',
  27874. 'this.vP = null;',
  27875. 'this.b = false;'
  27876. ]),
  27877. LinesToStr([
  27878. '$mod.Obj.FOnFoo = null;',
  27879. '$mod.Obj.SetFoo(null);',
  27880. '$mod.Obj.SetEvents(1, null);',
  27881. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27882. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27883. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27884. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27885. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27886. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27887. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27888. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27889. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27890. '$mod.Obj.FOnFoo(1);',
  27891. '$mod.Obj.GetFoo();',
  27892. '$mod.Obj.FOnFoo(1);',
  27893. '$mod.Obj.GetFoo()(1);',
  27894. '$mod.Obj.GetEvents(6)(1);',
  27895. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27896. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27897. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27898. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27899. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27900. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27901. '$mod.b = $mod.Obj.FOnFoo != null;',
  27902. '$mod.b = $mod.Obj.GetFoo() != null;',
  27903. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27904. '']));
  27905. end;
  27906. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27907. begin
  27908. StartProgram(false);
  27909. Add('type');
  27910. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27911. Add(' TObject = class');
  27912. Add(' FOnFoo: TFuncInt;');
  27913. Add(' function DoIt(vA: longint = 1): longint;');
  27914. Add(' function GetFoo: TFuncInt;');
  27915. Add(' procedure SetFoo(const Value: TFuncInt);');
  27916. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27917. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27918. Add(' end;');
  27919. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27920. Add('function tobject.getfoo: tfuncint; begin end;');
  27921. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27922. Add('var');
  27923. Add(' Obj: TObject;');
  27924. Add(' vP: tfuncint;');
  27925. Add(' b: boolean;');
  27926. Add('begin');
  27927. Add('with obj do begin');
  27928. Add(' fonfoo:=nil;');
  27929. Add(' onfoo:=nil;');
  27930. Add(' onbar:=nil;');
  27931. Add(' fonfoo:=fonfoo;');
  27932. Add(' onfoo:=onfoo;');
  27933. Add(' onbar:=onbar;');
  27934. Add(' fonfoo:=@doit;');
  27935. Add(' onfoo:=@doit;');
  27936. Add(' onbar:=@doit;');
  27937. //Add(' fonfoo:=doit;'); // delphi
  27938. //Add(' onfoo:=doit;'); // delphi
  27939. //Add(' onbar:=doit;'); // delphi
  27940. Add(' fonfoo;');
  27941. Add(' onfoo;');
  27942. Add(' onbar;');
  27943. Add(' fonfoo();');
  27944. Add(' onfoo();');
  27945. Add(' onbar();');
  27946. Add(' b:=fonfoo=nil;');
  27947. Add(' b:=onfoo=nil;');
  27948. Add(' b:=onbar=nil;');
  27949. Add(' b:=fonfoo<>nil;');
  27950. Add(' b:=onfoo<>nil;');
  27951. Add(' b:=onbar<>nil;');
  27952. Add(' b:=fonfoo=vp;');
  27953. Add(' b:=onfoo=vp;');
  27954. Add(' b:=onbar=vp;');
  27955. Add(' b:=fonfoo=fonfoo;');
  27956. Add(' b:=onfoo=onfoo;');
  27957. Add(' b:=onbar=onfoo;');
  27958. Add(' b:=fonfoo<>fonfoo;');
  27959. Add(' b:=onfoo<>onfoo;');
  27960. Add(' b:=onbar<>onfoo;');
  27961. Add(' b:=fonfoo=@doit;');
  27962. Add(' b:=onfoo=@doit;');
  27963. Add(' b:=onbar=@doit;');
  27964. Add(' b:=fonfoo<>@doit;');
  27965. Add(' b:=onfoo<>@doit;');
  27966. Add(' b:=onbar<>@doit;');
  27967. Add(' b:=Assigned(fonfoo);');
  27968. Add(' b:=Assigned(onfoo);');
  27969. Add(' b:=Assigned(onbar);');
  27970. Add('end;');
  27971. ConvertProgram;
  27972. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  27973. LinesToStr([ // statements
  27974. 'rtl.createClass(this, "TObject", null, function () {',
  27975. ' this.$init = function () {',
  27976. ' this.FOnFoo = null;',
  27977. ' };',
  27978. ' this.$final = function () {',
  27979. ' this.FOnFoo = undefined;',
  27980. ' };',
  27981. ' this.DoIt = function (vA) {',
  27982. ' var Result = 0;',
  27983. ' return Result;',
  27984. ' };',
  27985. ' this.GetFoo = function () {',
  27986. ' var Result = null;',
  27987. ' return Result;',
  27988. ' };',
  27989. ' this.SetFoo = function (Value) {',
  27990. ' };',
  27991. '});',
  27992. 'this.Obj = null;',
  27993. 'this.vP = null;',
  27994. 'this.b = false;'
  27995. ]),
  27996. LinesToStr([
  27997. 'var $with = $mod.Obj;',
  27998. '$with.FOnFoo = null;',
  27999. '$with.FOnFoo = null;',
  28000. '$with.SetFoo(null);',
  28001. '$with.FOnFoo = $with.FOnFoo;',
  28002. '$with.FOnFoo = $with.FOnFoo;',
  28003. '$with.SetFoo($with.GetFoo());',
  28004. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28005. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28006. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28007. '$with.FOnFoo(1);',
  28008. '$with.FOnFoo(1);',
  28009. '$with.GetFoo();',
  28010. '$with.FOnFoo(1);',
  28011. '$with.FOnFoo(1);',
  28012. '$with.GetFoo()(1);',
  28013. '$mod.b = $with.FOnFoo === null;',
  28014. '$mod.b = $with.FOnFoo === null;',
  28015. '$mod.b = $with.GetFoo() === null;',
  28016. '$mod.b = $with.FOnFoo !== null;',
  28017. '$mod.b = $with.FOnFoo !== null;',
  28018. '$mod.b = $with.GetFoo() !== null;',
  28019. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28020. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28021. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28022. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28023. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28024. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28025. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28026. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28027. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28028. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28029. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28030. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28031. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28032. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28033. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28034. '$mod.b = $with.FOnFoo != null;',
  28035. '$mod.b = $with.FOnFoo != null;',
  28036. '$mod.b = $with.GetFoo() != null;',
  28037. '']));
  28038. end;
  28039. procedure TTestModule.TestProcType_Nested;
  28040. begin
  28041. StartProgram(false);
  28042. Add([
  28043. 'type',
  28044. ' TProcInt = procedure(vI: longint = 1);',
  28045. 'procedure DoIt(vJ: longint);',
  28046. 'var aProc: TProcInt;',
  28047. ' b: boolean;',
  28048. ' procedure Sub(vK: longint);',
  28049. ' var aSub: TProcInt;',
  28050. ' procedure SubSub(vK: longint);',
  28051. ' var aSubSub: TProcInt;',
  28052. ' begin;',
  28053. ' aProc:=@DoIt;',
  28054. ' aSub:=@DoIt;',
  28055. ' aSubSub:=@DoIt;',
  28056. ' aProc:=@Sub;',
  28057. ' aSub:=@Sub;',
  28058. ' aSubSub:=@Sub;',
  28059. ' aProc:=@SubSub;',
  28060. ' aSub:=@SubSub;',
  28061. ' aSubSub:=@SubSub;',
  28062. ' end;',
  28063. ' begin;',
  28064. ' end;',
  28065. 'begin;',
  28066. ' aProc:=@Sub;',
  28067. ' b:=aProc=@Sub;',
  28068. ' b:=@Sub=aProc;',
  28069. 'end;',
  28070. 'begin',
  28071. '']);
  28072. ConvertProgram;
  28073. CheckSource('TestProcType_Nested',
  28074. LinesToStr([ // statements
  28075. 'this.DoIt = function (vJ) {',
  28076. ' var aProc = null;',
  28077. ' var b = false;',
  28078. ' function Sub(vK) {',
  28079. ' var aSub = null;',
  28080. ' function SubSub(vK) {',
  28081. ' var aSubSub = null;',
  28082. ' aProc = $mod.DoIt;',
  28083. ' aSub = $mod.DoIt;',
  28084. ' aSubSub = $mod.DoIt;',
  28085. ' aProc = Sub;',
  28086. ' aSub = Sub;',
  28087. ' aSubSub = Sub;',
  28088. ' aProc = SubSub;',
  28089. ' aSub = SubSub;',
  28090. ' aSubSub = SubSub;',
  28091. ' };',
  28092. ' };',
  28093. ' aProc = Sub;',
  28094. ' b = rtl.eqCallback(aProc, Sub);',
  28095. ' b = rtl.eqCallback(Sub, aProc);',
  28096. '};',
  28097. '']),
  28098. LinesToStr([ // $mod.$main
  28099. '']));
  28100. end;
  28101. procedure TTestModule.TestProcType_NestedOfObject;
  28102. begin
  28103. StartProgram(false);
  28104. Add([
  28105. 'type',
  28106. ' TProcInt = procedure(vI: longint = 1) of object;',
  28107. ' TObject = class',
  28108. ' procedure DoIt(vJ: longint);',
  28109. ' end;',
  28110. 'procedure TObject.DoIt(vJ: longint);',
  28111. 'var aProc: TProcInt;',
  28112. ' b: boolean;',
  28113. ' procedure Sub(vK: longint);',
  28114. ' var aSub: TProcInt;',
  28115. ' procedure SubSub(vK: longint);',
  28116. ' var aSubSub: TProcInt;',
  28117. ' begin;',
  28118. ' aProc:=@DoIt;',
  28119. ' aSub:=@DoIt;',
  28120. ' aSubSub:=@DoIt;',
  28121. ' aProc:=@Sub;',
  28122. ' aSub:=@Sub;',
  28123. ' aSubSub:=@Sub;',
  28124. ' aProc:=@SubSub;',
  28125. ' aSub:=@SubSub;',
  28126. ' aSubSub:=@SubSub;',
  28127. ' end;',
  28128. ' begin;',
  28129. ' end;',
  28130. 'begin;',
  28131. ' aProc:=@Sub;',
  28132. ' b:=aProc=@Sub;',
  28133. ' b:=@Sub=aProc;',
  28134. 'end;',
  28135. 'begin',
  28136. '']);
  28137. ConvertProgram;
  28138. CheckSource('TestProcType_Nested',
  28139. LinesToStr([ // statements
  28140. 'rtl.createClass(this, "TObject", null, function () {',
  28141. ' this.$init = function () {',
  28142. ' };',
  28143. ' this.$final = function () {',
  28144. ' };',
  28145. ' this.DoIt = function (vJ) {',
  28146. ' var $Self = this;',
  28147. ' var aProc = null;',
  28148. ' var b = false;',
  28149. ' function Sub(vK) {',
  28150. ' var aSub = null;',
  28151. ' function SubSub(vK) {',
  28152. ' var aSubSub = null;',
  28153. ' aProc = rtl.createCallback($Self, "DoIt");',
  28154. ' aSub = rtl.createCallback($Self, "DoIt");',
  28155. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28156. ' aProc = Sub;',
  28157. ' aSub = Sub;',
  28158. ' aSubSub = Sub;',
  28159. ' aProc = SubSub;',
  28160. ' aSub = SubSub;',
  28161. ' aSubSub = SubSub;',
  28162. ' };',
  28163. ' };',
  28164. ' aProc = Sub;',
  28165. ' b = rtl.eqCallback(aProc, Sub);',
  28166. ' b = rtl.eqCallback(Sub, aProc);',
  28167. ' };',
  28168. '});',
  28169. '']),
  28170. LinesToStr([ // $mod.$main
  28171. '']));
  28172. end;
  28173. procedure TTestModule.TestProcType_ReferenceToProc;
  28174. begin
  28175. StartProgram(false);
  28176. Add([
  28177. 'type',
  28178. ' TProcRef = reference to procedure(i: longint = 0);',
  28179. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28180. 'var',
  28181. ' p: TProcRef;',
  28182. ' f: TFuncRef;',
  28183. 'procedure DoIt(i: longint);',
  28184. 'begin',
  28185. 'end;',
  28186. 'function GetIt(i: longint): longint;',
  28187. 'begin',
  28188. ' p:=@DoIt;',
  28189. ' f:=@GetIt;',
  28190. ' f;',
  28191. ' f();',
  28192. ' f(1);',
  28193. 'end;',
  28194. 'begin',
  28195. ' p:=@DoIt;',
  28196. ' f:=@GetIt;',
  28197. ' f;',
  28198. ' f();',
  28199. ' f(1);',
  28200. ' p:=TProcRef(f);',
  28201. '']);
  28202. ConvertProgram;
  28203. CheckSource('TestProcType_ReferenceToProc',
  28204. LinesToStr([ // statements
  28205. 'this.p = null;',
  28206. 'this.f = null;',
  28207. 'this.DoIt = function (i) {',
  28208. '};',
  28209. 'this.GetIt = function (i) {',
  28210. ' var Result = 0;',
  28211. ' $mod.p = $mod.DoIt;',
  28212. ' $mod.f = $mod.GetIt;',
  28213. ' $mod.f(0);',
  28214. ' $mod.f(0);',
  28215. ' $mod.f(1);',
  28216. ' return Result;',
  28217. '};',
  28218. '']),
  28219. LinesToStr([ // $mod.$main
  28220. '$mod.p = $mod.DoIt;',
  28221. '$mod.f = $mod.GetIt;',
  28222. '$mod.f(0);',
  28223. '$mod.f(0);',
  28224. '$mod.f(1);',
  28225. '$mod.p = $mod.f;',
  28226. '']));
  28227. end;
  28228. procedure TTestModule.TestProcType_ReferenceToMethod;
  28229. begin
  28230. StartProgram(false);
  28231. Add([
  28232. 'type',
  28233. ' TFuncRef = reference to function(i: longint = 5): longint;',
  28234. ' TObject = class',
  28235. ' function Grow(s: longint): longint;',
  28236. ' end;',
  28237. 'var',
  28238. ' f: tfuncref;',
  28239. 'function tobject.grow(s: longint): longint;',
  28240. ' function GrowSub(i: longint): longint;',
  28241. ' begin',
  28242. ' f:=@grow;',
  28243. ' f:=@growsub;',
  28244. ' end;',
  28245. 'begin',
  28246. ' f:=@grow;',
  28247. ' f:=@growsub;',
  28248. 'end;',
  28249. 'begin',
  28250. '']);
  28251. ConvertProgram;
  28252. CheckSource('TestProcType_ReferenceToMethod',
  28253. LinesToStr([ // statements
  28254. 'rtl.createClass(this, "TObject", null, function () {',
  28255. ' this.$init = function () {',
  28256. ' };',
  28257. ' this.$final = function () {',
  28258. ' };',
  28259. ' this.Grow = function (s) {',
  28260. ' var $Self = this;',
  28261. ' var Result = 0;',
  28262. ' function GrowSub(i) {',
  28263. ' var Result = 0;',
  28264. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28265. ' $mod.f = GrowSub;',
  28266. ' return Result;',
  28267. ' };',
  28268. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28269. ' $mod.f = GrowSub;',
  28270. ' return Result;',
  28271. ' };',
  28272. '});',
  28273. 'this.f = null;',
  28274. '']),
  28275. LinesToStr([ // $mod.$main
  28276. '']));
  28277. end;
  28278. procedure TTestModule.TestProcType_Typecast;
  28279. begin
  28280. StartProgram(false);
  28281. Add([
  28282. 'type',
  28283. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  28284. ' TEvent = procedure of object;',
  28285. ' TGetter = function:longint of object;',
  28286. ' TProcA = procedure(i: longint);',
  28287. ' TFuncB = function(i, j: longint): longint;',
  28288. 'procedure DoIt(); varargs; begin end;',
  28289. 'var',
  28290. ' Notify: tnotifyevent;',
  28291. ' Event: tevent;',
  28292. ' Getter: tgetter;',
  28293. ' ProcA: tproca;',
  28294. ' FuncB: tfuncb;',
  28295. ' p: pointer;',
  28296. 'begin',
  28297. ' notify:=tnotifyevent(event);',
  28298. ' event:=tevent(event);',
  28299. ' event:=tevent(notify);',
  28300. ' event:=tevent(getter);',
  28301. ' event:=tevent(proca);',
  28302. ' proca:=tproca(funcb);',
  28303. ' funcb:=tfuncb(funcb);',
  28304. ' funcb:=tfuncb(proca);',
  28305. ' funcb:=tfuncb(getter);',
  28306. ' proca:=tproca(p);',
  28307. ' funcb:=tfuncb(p);',
  28308. ' getter:=tgetter(p);',
  28309. ' p:=pointer(notify);',
  28310. ' p:=notify;',
  28311. ' p:=pointer(proca);',
  28312. ' p:=proca;',
  28313. ' p:=pointer(funcb);',
  28314. ' p:=funcb;',
  28315. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  28316. '']);
  28317. ConvertProgram;
  28318. CheckSource('TestProcType_Typecast',
  28319. LinesToStr([ // statements
  28320. 'this.DoIt = function () {',
  28321. '};',
  28322. 'this.Notify = null;',
  28323. 'this.Event = null;',
  28324. 'this.Getter = null;',
  28325. 'this.ProcA = null;',
  28326. 'this.FuncB = null;',
  28327. 'this.p = null;',
  28328. '']),
  28329. LinesToStr([ // $mod.$main
  28330. '$mod.Notify = $mod.Event;',
  28331. '$mod.Event = $mod.Event;',
  28332. '$mod.Event = $mod.Notify;',
  28333. '$mod.Event = $mod.Getter;',
  28334. '$mod.Event = $mod.ProcA;',
  28335. '$mod.ProcA = $mod.FuncB;',
  28336. '$mod.FuncB = $mod.FuncB;',
  28337. '$mod.FuncB = $mod.ProcA;',
  28338. '$mod.FuncB = $mod.Getter;',
  28339. '$mod.ProcA = $mod.p;',
  28340. '$mod.FuncB = $mod.p;',
  28341. '$mod.Getter = $mod.p;',
  28342. '$mod.p = $mod.Notify;',
  28343. '$mod.p = $mod.Notify;',
  28344. '$mod.p = $mod.ProcA;',
  28345. '$mod.p = $mod.ProcA;',
  28346. '$mod.p = $mod.FuncB;',
  28347. '$mod.p = $mod.FuncB;',
  28348. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  28349. '']));
  28350. end;
  28351. procedure TTestModule.TestProcType_PassProcToUntyped;
  28352. begin
  28353. StartProgram(false);
  28354. Add([
  28355. 'type',
  28356. ' TEvent = procedure of object;',
  28357. ' TFunc = function: longint;',
  28358. 'procedure DoIt(); varargs; begin end;',
  28359. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28360. 'var',
  28361. ' Event: tevent;',
  28362. ' Func: TFunc;',
  28363. 'begin',
  28364. ' doit(event,func);',
  28365. ' dosome(event,event,event);',
  28366. ' dosome(func,func,func);',
  28367. '']);
  28368. ConvertProgram;
  28369. CheckSource('TestProcType_PassProcToUntyped',
  28370. LinesToStr([ // statements
  28371. 'this.DoIt = function () {',
  28372. '};',
  28373. 'this.DoSome = function (a, b, p) {',
  28374. '};',
  28375. 'this.Event = null;',
  28376. 'this.Func = null;',
  28377. '']),
  28378. LinesToStr([ // $mod.$main
  28379. '$mod.DoIt($mod.Event, $mod.Func);',
  28380. '$mod.DoSome($mod.Event, {',
  28381. ' p: $mod,',
  28382. ' get: function () {',
  28383. ' return this.p.Event;',
  28384. ' },',
  28385. ' set: function (v) {',
  28386. ' this.p.Event = v;',
  28387. ' }',
  28388. '}, $mod.Event);',
  28389. '$mod.DoSome($mod.Func, {',
  28390. ' p: $mod,',
  28391. ' get: function () {',
  28392. ' return this.p.Func;',
  28393. ' },',
  28394. ' set: function (v) {',
  28395. ' this.p.Func = v;',
  28396. ' }',
  28397. '}, $mod.Func);',
  28398. '']));
  28399. end;
  28400. procedure TTestModule.TestProcType_PassProcToArray;
  28401. begin
  28402. StartProgram(false);
  28403. Add([
  28404. 'type',
  28405. ' TFunc = function: longint;',
  28406. ' TArrFunc = array of TFunc;',
  28407. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28408. 'function GetIt: longint; begin end;',
  28409. 'var',
  28410. ' Func: tfunc;',
  28411. 'begin',
  28412. ' doit([]);',
  28413. ' doit([@GetIt]);',
  28414. ' doit([Func]);',
  28415. '']);
  28416. ConvertProgram;
  28417. CheckSource('TestProcType_PassProcToArray',
  28418. LinesToStr([ // statements
  28419. 'this.DoIt = function (Arr) {',
  28420. '};',
  28421. 'this.GetIt = function () {',
  28422. ' var Result = 0;',
  28423. ' return Result;',
  28424. '};',
  28425. 'this.Func = null;',
  28426. '']),
  28427. LinesToStr([ // $mod.$main
  28428. '$mod.DoIt([]);',
  28429. '$mod.DoIt([$mod.GetIt]);',
  28430. '$mod.DoIt([$mod.Func]);',
  28431. '']));
  28432. end;
  28433. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28434. begin
  28435. StartProgram(false);
  28436. Add([
  28437. '{$modeswitch externalclass}',
  28438. 'type',
  28439. ' TProc = reference to procedure(i: longint); safecall;',
  28440. ' TEvent = procedure(i: longint) of object; safecall;',
  28441. ' TExtA = class external name ''ExtObj''',
  28442. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28443. ' procedure DoSome(Id: longint = 1);',
  28444. ' procedure SetOnClick(const e: TEvent);',
  28445. ' property OnClick: TEvent write SetOnClick;',
  28446. ' class procedure Fly(Id: longint = 1); static;',
  28447. ' procedure SetOnShow(const p: TProc);',
  28448. ' property OnShow: TProc write SetOnShow;',
  28449. ' end;',
  28450. 'procedure Run(i: longint = 1);',
  28451. 'begin',
  28452. 'end;',
  28453. 'var',
  28454. ' Obj: texta;',
  28455. ' e: TEvent;',
  28456. ' p: TProc;',
  28457. 'begin',
  28458. ' e:=e;',
  28459. ' e:[email protected];',
  28460. ' e:[email protected];',
  28461. ' e:=TEvent(@obj.dosome);', // no safecall
  28462. ' obj.OnClick:[email protected];',
  28463. ' obj.OnClick:[email protected];',
  28464. ' obj.setonclick(@obj.doit);',
  28465. ' obj.setonclick(@obj.dosome);',
  28466. ' p:=@Run;',
  28467. ' p:[email protected];',
  28468. ' obj.OnShow:=@Run;',
  28469. ' obj.OnShow:[email protected];',
  28470. ' obj.setOnShow(@Run);',
  28471. ' obj.setOnShow(@TExtA.Fly);',
  28472. ' with obj do begin',
  28473. ' e:=@doit;',
  28474. ' e:=@dosome;',
  28475. ' OnClick:=@doit;',
  28476. ' OnClick:=@dosome;',
  28477. ' setonclick(@doit);',
  28478. ' setonclick(@dosome);',
  28479. ' OnShow:=@Run;',
  28480. ' setOnShow(@Run);',
  28481. ' end;']);
  28482. ConvertProgram;
  28483. CheckSource('TestProcType_SafeCallObjFPC',
  28484. LinesToStr([ // statements
  28485. 'this.Run = function (i) {',
  28486. '};',
  28487. 'this.Obj = null;',
  28488. 'this.e = null;',
  28489. 'this.p = null;',
  28490. '']),
  28491. LinesToStr([ // $mod.$main
  28492. '$mod.e = $mod.e;',
  28493. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28494. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28495. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28496. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28497. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28498. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28499. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28500. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28501. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28502. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28503. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28504. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28505. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28506. 'var $with = $mod.Obj;',
  28507. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28508. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28509. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28510. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28511. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28512. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28513. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28514. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28515. '']));
  28516. end;
  28517. procedure TTestModule.TestProcType_SafeCallDelphi;
  28518. begin
  28519. StartProgram(false);
  28520. Add([
  28521. '{$mode delphi}',
  28522. '{$modeswitch externalclass}',
  28523. 'type',
  28524. ' TProc = reference to procedure(i: longint); safecall;',
  28525. ' TEvent = procedure(i: longint) of object; safecall;',
  28526. ' TExtA = class external name ''ExtObj''',
  28527. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28528. ' procedure DoSome(Id: longint = 1);',
  28529. ' procedure SetOnClick(const e: TEvent);',
  28530. ' property OnClick: TEvent write SetOnClick;',
  28531. ' class procedure Fly(Id: longint = 1); static;',
  28532. ' procedure SetOnShow(const p: TProc);',
  28533. ' property OnShow: TProc write SetOnShow;',
  28534. ' end;',
  28535. 'procedure Run(i: longint = 1);',
  28536. 'begin',
  28537. 'end;',
  28538. 'var',
  28539. ' Obj: texta;',
  28540. ' e: TEvent;',
  28541. ' p: TProc;',
  28542. 'begin',
  28543. ' e:=e;',
  28544. ' e:=obj.doit;',
  28545. ' e:=obj.dosome;',
  28546. ' e:=TEvent(@obj.dosome);', // no safecall
  28547. ' obj.OnClick:=obj.doit;',
  28548. ' obj.OnClick:=obj.dosome;',
  28549. ' obj.setonclick(obj.doit);',
  28550. ' obj.setonclick(obj.dosome);',
  28551. ' p:=Run;',
  28552. ' p:=TExtA.Fly;',
  28553. ' obj.OnShow:=Run;',
  28554. ' obj.OnShow:=TExtA.Fly;',
  28555. ' obj.setOnShow(Run);',
  28556. ' obj.setOnShow(TExtA.Fly);',
  28557. ' with obj do begin',
  28558. ' e:=doit;',
  28559. ' e:=dosome;',
  28560. ' OnClick:=doit;',
  28561. ' OnClick:=dosome;',
  28562. ' setonclick(doit);',
  28563. ' setonclick(dosome);',
  28564. ' OnShow:=@Run;',
  28565. ' setOnShow(@Run);',
  28566. ' end;']);
  28567. ConvertProgram;
  28568. CheckSource('TestProcType_SafeCallDelphi',
  28569. LinesToStr([ // statements
  28570. 'this.Run = function (i) {',
  28571. '};',
  28572. 'this.Obj = null;',
  28573. 'this.e = null;',
  28574. 'this.p = null;',
  28575. '']),
  28576. LinesToStr([ // $mod.$main
  28577. '$mod.e = $mod.e;',
  28578. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28579. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28580. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28581. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28582. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28583. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28584. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28585. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28586. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28587. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28588. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28589. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28590. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28591. 'var $with = $mod.Obj;',
  28592. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28593. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28594. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28595. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28596. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28597. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28598. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28599. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28600. '']));
  28601. end;
  28602. procedure TTestModule.TestPointer;
  28603. begin
  28604. StartProgram(false);
  28605. Add(['type',
  28606. ' TObject = class end;',
  28607. ' TClass = class of TObject;',
  28608. ' TArrInt = array of longint;',
  28609. 'const',
  28610. ' n = nil;',
  28611. 'var',
  28612. ' v: jsvalue;',
  28613. ' Obj: tobject;',
  28614. ' C: tclass;',
  28615. ' a: tarrint;',
  28616. ' p: Pointer = nil;',
  28617. ' s: string;',
  28618. 'begin',
  28619. ' p:=p;',
  28620. ' p:=nil;',
  28621. ' if p=nil then;',
  28622. ' if nil=p then;',
  28623. ' if Assigned(p) then;',
  28624. ' p:=Pointer(v);',
  28625. ' p:=obj;',
  28626. ' p:=c;',
  28627. ' p:=a;',
  28628. ' p:=tobject;',
  28629. ' obj:=TObject(p);',
  28630. ' c:=TClass(p);',
  28631. ' a:=TArrInt(p);',
  28632. ' p:=n;',
  28633. ' p:=Pointer(a);',
  28634. ' p:=pointer(s);',
  28635. ' s:=string(p);',
  28636. '']);
  28637. ConvertProgram;
  28638. CheckSource('TestPointer',
  28639. LinesToStr([ // statements
  28640. 'rtl.createClass(this, "TObject", null, function () {',
  28641. ' this.$init = function () {',
  28642. ' };',
  28643. ' this.$final = function () {',
  28644. ' };',
  28645. '});',
  28646. 'this.n = null;',
  28647. 'this.v = undefined;',
  28648. 'this.Obj = null;',
  28649. 'this.C = null;',
  28650. 'this.a = [];',
  28651. 'this.p = null;',
  28652. 'this.s = "";',
  28653. '']),
  28654. LinesToStr([ // $mod.$main
  28655. '$mod.p = $mod.p;',
  28656. '$mod.p = null;',
  28657. 'if ($mod.p === null) ;',
  28658. 'if (null === $mod.p) ;',
  28659. 'if ($mod.p != null) ;',
  28660. '$mod.p = $mod.v;',
  28661. '$mod.p = $mod.Obj;',
  28662. '$mod.p = $mod.C;',
  28663. '$mod.p = $mod.a;',
  28664. '$mod.p = $mod.TObject;',
  28665. '$mod.Obj = $mod.p;',
  28666. '$mod.C = $mod.p;',
  28667. '$mod.a = $mod.p;',
  28668. '$mod.p = null;',
  28669. '$mod.p = $mod.a;',
  28670. '$mod.p = $mod.s;',
  28671. '$mod.s = $mod.p;',
  28672. '']));
  28673. end;
  28674. procedure TTestModule.TestPointer_Proc;
  28675. begin
  28676. StartProgram(false);
  28677. Add('type');
  28678. Add(' TObject = class');
  28679. Add(' procedure DoIt; virtual; abstract;');
  28680. Add(' end;');
  28681. Add('procedure DoSome; begin end;');
  28682. Add('var');
  28683. Add(' o: TObject;');
  28684. Add(' p: Pointer;');
  28685. Add('begin');
  28686. Add(' p:=@DoSome;');
  28687. Add(' p:[email protected];');
  28688. ConvertProgram;
  28689. CheckSource('TestPointer_Proc',
  28690. LinesToStr([ // statements
  28691. 'rtl.createClass(this, "TObject", null, function () {',
  28692. ' this.$init = function () {',
  28693. ' };',
  28694. ' this.$final = function () {',
  28695. ' };',
  28696. '});',
  28697. 'this.DoSome = function () {',
  28698. '};',
  28699. 'this.o = null;',
  28700. 'this.p = null;',
  28701. '']),
  28702. LinesToStr([ // $mod.$main
  28703. '$mod.p = $mod.DoSome;',
  28704. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28705. '']));
  28706. end;
  28707. procedure TTestModule.TestPointer_AssignRecordFail;
  28708. begin
  28709. StartProgram(false);
  28710. Add('type');
  28711. Add(' TRec = record end;');
  28712. Add('var');
  28713. Add(' p: Pointer;');
  28714. Add(' r: TRec;');
  28715. Add('begin');
  28716. Add(' p:=r;');
  28717. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28718. nIncompatibleTypesGotExpected);
  28719. ConvertProgram;
  28720. end;
  28721. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28722. begin
  28723. StartProgram(false);
  28724. Add('type');
  28725. Add(' TArr = array[boolean] of longint;');
  28726. Add('var');
  28727. Add(' p: Pointer;');
  28728. Add(' a: TArr;');
  28729. Add('begin');
  28730. Add(' p:=a;');
  28731. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28732. nIncompatibleTypesGotExpected);
  28733. ConvertProgram;
  28734. end;
  28735. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28736. begin
  28737. StartProgram(false);
  28738. Add([
  28739. 'procedure DoIt(args: array of jsvalue); begin end;',
  28740. 'procedure DoAll; varargs; begin end;',
  28741. 'var',
  28742. ' v: jsvalue;',
  28743. 'begin',
  28744. ' DoIt([pointer(v)]);',
  28745. ' DoAll(pointer(v));',
  28746. '']);
  28747. ConvertProgram;
  28748. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28749. LinesToStr([ // statements
  28750. 'this.DoIt = function (args) {',
  28751. '};',
  28752. 'this.DoAll = function () {',
  28753. '};',
  28754. 'this.v = undefined;',
  28755. '']),
  28756. LinesToStr([ // $mod.$main
  28757. '$mod.DoIt([$mod.v]);',
  28758. '$mod.DoAll($mod.v);',
  28759. '']));
  28760. end;
  28761. procedure TTestModule.TestPointer_NonRecordFail;
  28762. begin
  28763. StartProgram(false);
  28764. Add([
  28765. 'type',
  28766. ' p = ^longint;',
  28767. 'begin',
  28768. '']);
  28769. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28770. ConvertProgram;
  28771. end;
  28772. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28773. begin
  28774. StartProgram(false);
  28775. Add([
  28776. 'procedure DoIt(p: ^longint); begin end;',
  28777. 'begin',
  28778. '']);
  28779. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28780. ConvertProgram;
  28781. end;
  28782. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28783. begin
  28784. StartProgram(false);
  28785. Add([
  28786. 'var p: ^longint;',
  28787. 'begin',
  28788. '']);
  28789. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28790. ConvertProgram;
  28791. end;
  28792. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28793. begin
  28794. StartProgram(false);
  28795. Add([
  28796. 'function DoIt: ^longint; begin end;',
  28797. 'begin',
  28798. '']);
  28799. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28800. ConvertProgram;
  28801. end;
  28802. procedure TTestModule.TestPointer_AddrOperatorFail;
  28803. begin
  28804. StartProgram(false);
  28805. Add([
  28806. 'var i: longint;',
  28807. 'begin',
  28808. ' if @i=nil then ;',
  28809. '']);
  28810. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28811. ConvertProgram;
  28812. end;
  28813. procedure TTestModule.TestPointer_ArrayParamsFail;
  28814. begin
  28815. StartProgram(false);
  28816. Add([
  28817. 'var',
  28818. ' p: Pointer;',
  28819. 'begin',
  28820. ' p:=p[1];',
  28821. '']);
  28822. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28823. ConvertProgram;
  28824. end;
  28825. procedure TTestModule.TestPointer_PointerAddFail;
  28826. begin
  28827. StartProgram(false);
  28828. Add([
  28829. 'var',
  28830. ' p: Pointer;',
  28831. 'begin',
  28832. ' p:=p+1;',
  28833. '']);
  28834. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28835. ConvertProgram;
  28836. end;
  28837. procedure TTestModule.TestPointer_IncPointerFail;
  28838. begin
  28839. StartProgram(false);
  28840. Add([
  28841. 'var',
  28842. ' p: Pointer;',
  28843. 'begin',
  28844. ' inc(p,1);',
  28845. '']);
  28846. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28847. nIncompatibleTypeArgNo);
  28848. ConvertProgram;
  28849. end;
  28850. procedure TTestModule.TestPointer_Record;
  28851. begin
  28852. StartProgram(false);
  28853. Add([
  28854. 'type',
  28855. ' TRec = record x: longint; end;',
  28856. ' PRec = ^TRec;',
  28857. 'var',
  28858. ' r: TRec;',
  28859. ' p: PRec;',
  28860. ' q: ^TRec;',
  28861. ' Ptr: pointer;',
  28862. 'begin',
  28863. ' new(p);',
  28864. ' p:=@r;',
  28865. ' r:=p^;',
  28866. ' r.x:=p^.x;',
  28867. ' p^.x:=r.x;',
  28868. ' if p^.x=3 then ;',
  28869. ' if 4=p^.x then ;',
  28870. ' dispose(p);',
  28871. ' new(q);',
  28872. ' dispose(q);',
  28873. ' Ptr:=p;',
  28874. ' p:=PRec(ptr);',
  28875. '']);
  28876. ConvertProgram;
  28877. CheckSource('TestPointer_Record',
  28878. LinesToStr([ // statements
  28879. 'rtl.recNewT(this, "TRec", function () {',
  28880. ' this.x = 0;',
  28881. ' this.$eq = function (b) {',
  28882. ' return this.x === b.x;',
  28883. ' };',
  28884. ' this.$assign = function (s) {',
  28885. ' this.x = s.x;',
  28886. ' return this;',
  28887. ' };',
  28888. '});',
  28889. 'this.r = this.TRec.$new();',
  28890. 'this.p = null;',
  28891. 'this.q = null;',
  28892. 'this.Ptr = null;',
  28893. '']),
  28894. LinesToStr([ // $mod.$main
  28895. '$mod.p = $mod.TRec.$new();',
  28896. '$mod.p = $mod.r;',
  28897. '$mod.r.$assign($mod.p);',
  28898. '$mod.r.x = $mod.p.x;',
  28899. '$mod.p.x = $mod.r.x;',
  28900. 'if ($mod.p.x === 3) ;',
  28901. 'if (4 === $mod.p.x) ;',
  28902. '$mod.p = null;',
  28903. '$mod.q = $mod.TRec.$new();',
  28904. '$mod.q = null;',
  28905. '$mod.Ptr = $mod.p;',
  28906. '$mod.p = $mod.Ptr;',
  28907. '']));
  28908. end;
  28909. procedure TTestModule.TestPointer_RecordArg;
  28910. begin
  28911. StartProgram(false);
  28912. Add([
  28913. '{$modeswitch autoderef}',
  28914. 'type',
  28915. ' TRec = record x: longint; end;',
  28916. ' PRec = ^TRec;',
  28917. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  28918. 'begin',
  28919. ' a.x:=a.x;',
  28920. ' a^.x:=a^.x;',
  28921. ' with a^ do',
  28922. ' x:=x;',
  28923. 'end;',
  28924. 'function GetIt(p: PRec): PRec;',
  28925. 'begin',
  28926. ' p.x:=p.x;',
  28927. ' p^.x:=p^.x;',
  28928. ' with p^ do',
  28929. ' x:=x;',
  28930. 'end;',
  28931. 'var',
  28932. ' r: TRec;',
  28933. ' p: PRec;',
  28934. 'begin',
  28935. ' p:=GetIt(p);',
  28936. ' p^:=GetIt(@r)^;',
  28937. ' DoIt(p,p,p);',
  28938. ' DoIt(@r,p,p);',
  28939. '']);
  28940. ConvertProgram;
  28941. CheckSource('TestPointer_RecordArg',
  28942. LinesToStr([ // statements
  28943. 'rtl.recNewT(this, "TRec", function () {',
  28944. ' this.x = 0;',
  28945. ' this.$eq = function (b) {',
  28946. ' return this.x === b.x;',
  28947. ' };',
  28948. ' this.$assign = function (s) {',
  28949. ' this.x = s.x;',
  28950. ' return this;',
  28951. ' };',
  28952. '});',
  28953. 'this.DoIt = function (a, b, c) {',
  28954. ' var Result = $mod.TRec.$new();',
  28955. ' a.x = a.x;',
  28956. ' a.x = a.x;',
  28957. ' a.x = a.x;',
  28958. ' return Result;',
  28959. '};',
  28960. 'this.GetIt = function (p) {',
  28961. ' var Result = null;',
  28962. ' p.x = p.x;',
  28963. ' p.x = p.x;',
  28964. ' p.x = p.x;',
  28965. ' return Result;',
  28966. '};',
  28967. 'this.r = this.TRec.$new();',
  28968. 'this.p = null;',
  28969. '']),
  28970. LinesToStr([ // $mod.$main
  28971. '$mod.p = $mod.GetIt($mod.p);',
  28972. '$mod.p.$assign($mod.GetIt($mod.r));',
  28973. '$mod.DoIt($mod.p, {',
  28974. ' p: $mod,',
  28975. ' get: function () {',
  28976. ' return this.p.p;',
  28977. ' },',
  28978. ' set: function (v) {',
  28979. ' this.p.p = v;',
  28980. ' }',
  28981. '}, {',
  28982. ' p: $mod,',
  28983. ' get: function () {',
  28984. ' return this.p.p;',
  28985. ' },',
  28986. ' set: function (v) {',
  28987. ' this.p.p = v;',
  28988. ' }',
  28989. '});',
  28990. '$mod.DoIt($mod.r, {',
  28991. ' p: $mod,',
  28992. ' get: function () {',
  28993. ' return this.p.p;',
  28994. ' },',
  28995. ' set: function (v) {',
  28996. ' this.p.p = v;',
  28997. ' }',
  28998. '}, {',
  28999. ' p: $mod,',
  29000. ' get: function () {',
  29001. ' return this.p.p;',
  29002. ' },',
  29003. ' set: function (v) {',
  29004. ' this.p.p = v;',
  29005. ' }',
  29006. '});',
  29007. '']));
  29008. end;
  29009. procedure TTestModule.TestJSValue_AssignToJSValue;
  29010. begin
  29011. StartProgram(false);
  29012. Add('var');
  29013. Add(' v: jsvalue;');
  29014. Add(' i: longint;');
  29015. Add(' s: string;');
  29016. Add(' b: boolean;');
  29017. Add(' d: double;');
  29018. Add(' p: pointer;');
  29019. Add('begin');
  29020. Add(' v:=v;');
  29021. Add(' v:=1;');
  29022. Add(' v:=i;');
  29023. Add(' v:='''';');
  29024. Add(' v:=''c'';');
  29025. Add(' v:=''foo'';');
  29026. Add(' v:=s;');
  29027. Add(' v:=false;');
  29028. Add(' v:=true;');
  29029. Add(' v:=b;');
  29030. Add(' v:=0.1;');
  29031. Add(' v:=d;');
  29032. Add(' v:=nil;');
  29033. Add(' v:=p;');
  29034. ConvertProgram;
  29035. CheckSource('TestJSValue_AssignToJSValue',
  29036. LinesToStr([ // statements
  29037. 'this.v = undefined;',
  29038. 'this.i = 0;',
  29039. 'this.s = "";',
  29040. 'this.b = false;',
  29041. 'this.d = 0.0;',
  29042. 'this.p = null;',
  29043. '']),
  29044. LinesToStr([ // $mod.$main
  29045. '$mod.v = $mod.v;',
  29046. '$mod.v = 1;',
  29047. '$mod.v = $mod.i;',
  29048. '$mod.v = "";',
  29049. '$mod.v = "c";',
  29050. '$mod.v = "foo";',
  29051. '$mod.v = $mod.s;',
  29052. '$mod.v = false;',
  29053. '$mod.v = true;',
  29054. '$mod.v = $mod.b;',
  29055. '$mod.v = 0.1;',
  29056. '$mod.v = $mod.d;',
  29057. '$mod.v = null;',
  29058. '$mod.v = $mod.p;',
  29059. '']));
  29060. end;
  29061. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29062. begin
  29063. StartProgram(false);
  29064. Add('type');
  29065. Add(' integer = longint;');
  29066. Add(' TYesNo = boolean;');
  29067. Add(' TFloat = double;');
  29068. Add(' TCaption = string;');
  29069. Add(' TChar = char;');
  29070. Add('var');
  29071. Add(' v: jsvalue;');
  29072. Add(' i: integer;');
  29073. Add(' s: TCaption;');
  29074. Add(' b: TYesNo;');
  29075. Add(' d: TFloat;');
  29076. Add(' c: char;');
  29077. Add('begin');
  29078. Add(' i:=longint(v);');
  29079. Add(' i:=integer(v);');
  29080. Add(' s:=string(v);');
  29081. Add(' s:=TCaption(v);');
  29082. Add(' b:=boolean(v);');
  29083. Add(' b:=TYesNo(v);');
  29084. Add(' d:=double(v);');
  29085. Add(' d:=TFloat(v);');
  29086. Add(' c:=char(v);');
  29087. Add(' c:=TChar(v);');
  29088. ConvertProgram;
  29089. CheckSource('TestJSValue_TypeCastToBaseType',
  29090. LinesToStr([ // statements
  29091. 'this.v = undefined;',
  29092. 'this.i = 0;',
  29093. 'this.s = "";',
  29094. 'this.b = false;',
  29095. 'this.d = 0.0;',
  29096. 'this.c = "";',
  29097. '']),
  29098. LinesToStr([ // $mod.$main
  29099. '$mod.i = rtl.trunc($mod.v);',
  29100. '$mod.i = rtl.trunc($mod.v);',
  29101. '$mod.s = "" + $mod.v;',
  29102. '$mod.s = "" + $mod.v;',
  29103. '$mod.b = !($mod.v == false);',
  29104. '$mod.b = !($mod.v == false);',
  29105. '$mod.d = rtl.getNumber($mod.v);',
  29106. '$mod.d = rtl.getNumber($mod.v);',
  29107. '$mod.c = rtl.getChar($mod.v);',
  29108. '$mod.c = rtl.getChar($mod.v);',
  29109. '']));
  29110. end;
  29111. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29112. begin
  29113. StartProgram(false);
  29114. Add([
  29115. 'type',
  29116. ' TArr = array of word;',
  29117. ' TRec = record end;',
  29118. ' TSet = set of boolean;',
  29119. 'procedure Fly(v: jsvalue);',
  29120. 'begin',
  29121. 'end;',
  29122. 'var',
  29123. ' a: TArr;',
  29124. ' r: TRec;',
  29125. ' s: TSet;',
  29126. 'begin',
  29127. ' Fly(jsvalue(a));',
  29128. ' Fly(jsvalue(r));',
  29129. ' Fly(jsvalue(s));',
  29130. '']);
  29131. ConvertProgram;
  29132. CheckSource('TestJSValue_TypecastToJSValue',
  29133. LinesToStr([ // statements
  29134. 'rtl.recNewT(this, "TRec", function () {',
  29135. ' this.$eq = function (b) {',
  29136. ' return true;',
  29137. ' };',
  29138. ' this.$assign = function (s) {',
  29139. ' return this;',
  29140. ' };',
  29141. '});',
  29142. 'this.Fly = function (v) {',
  29143. '};',
  29144. 'this.a = [];',
  29145. 'this.r = this.TRec.$new();',
  29146. 'this.s = {};',
  29147. '']),
  29148. LinesToStr([ // $mod.$main
  29149. '$mod.Fly($mod.a);',
  29150. '$mod.Fly($mod.r);',
  29151. '$mod.Fly($mod.s);',
  29152. '']));
  29153. end;
  29154. procedure TTestModule.TestJSValue_Equal;
  29155. begin
  29156. StartProgram(false);
  29157. Add('type');
  29158. Add(' integer = longint;');
  29159. Add(' TYesNo = boolean;');
  29160. Add(' TFloat = double;');
  29161. Add(' TCaption = string;');
  29162. Add(' TChar = char;');
  29163. Add(' TMulti = JSValue;');
  29164. Add('var');
  29165. Add(' v: jsvalue;');
  29166. Add(' i: integer;');
  29167. Add(' s: TCaption;');
  29168. Add(' b: TYesNo;');
  29169. Add(' d: TFloat;');
  29170. Add(' c: char;');
  29171. Add(' m: TMulti;');
  29172. Add('begin');
  29173. Add(' b:=v=v;');
  29174. Add(' b:=v<>v;');
  29175. Add(' b:=v=1;');
  29176. Add(' b:=v<>1;');
  29177. Add(' b:=2=v;');
  29178. Add(' b:=2<>v;');
  29179. Add(' b:=v=i;');
  29180. Add(' b:=i=v;');
  29181. Add(' b:=v=nil;');
  29182. Add(' b:=nil=v;');
  29183. Add(' b:=v=false;');
  29184. Add(' b:=true=v;');
  29185. Add(' b:=v=b;');
  29186. Add(' b:=b=v;');
  29187. Add(' b:=v=s;');
  29188. Add(' b:=s=v;');
  29189. Add(' b:=v=''foo'';');
  29190. Add(' b:=''''=v;');
  29191. Add(' b:=v=d;');
  29192. Add(' b:=d=v;');
  29193. Add(' b:=v=3.4;');
  29194. Add(' b:=5.6=v;');
  29195. Add(' b:=v=c;');
  29196. Add(' b:=c=v;');
  29197. Add(' b:=m=m;');
  29198. Add(' b:=v=m;');
  29199. Add(' b:=m=v;');
  29200. ConvertProgram;
  29201. CheckSource('TestJSValue_Equal',
  29202. LinesToStr([ // statements
  29203. 'this.v = undefined;',
  29204. 'this.i = 0;',
  29205. 'this.s = "";',
  29206. 'this.b = false;',
  29207. 'this.d = 0.0;',
  29208. 'this.c = "";',
  29209. 'this.m = undefined;',
  29210. '']),
  29211. LinesToStr([ // $mod.$main
  29212. '$mod.b = $mod.v == $mod.v;',
  29213. '$mod.b = $mod.v != $mod.v;',
  29214. '$mod.b = $mod.v == 1;',
  29215. '$mod.b = $mod.v != 1;',
  29216. '$mod.b = 2 == $mod.v;',
  29217. '$mod.b = 2 != $mod.v;',
  29218. '$mod.b = $mod.v == $mod.i;',
  29219. '$mod.b = $mod.i == $mod.v;',
  29220. '$mod.b = $mod.v == null;',
  29221. '$mod.b = null == $mod.v;',
  29222. '$mod.b = $mod.v == false;',
  29223. '$mod.b = true == $mod.v;',
  29224. '$mod.b = $mod.v == $mod.b;',
  29225. '$mod.b = $mod.b == $mod.v;',
  29226. '$mod.b = $mod.v == $mod.s;',
  29227. '$mod.b = $mod.s == $mod.v;',
  29228. '$mod.b = $mod.v == "foo";',
  29229. '$mod.b = "" == $mod.v;',
  29230. '$mod.b = $mod.v == $mod.d;',
  29231. '$mod.b = $mod.d == $mod.v;',
  29232. '$mod.b = $mod.v == 3.4;',
  29233. '$mod.b = 5.6 == $mod.v;',
  29234. '$mod.b = $mod.v == $mod.c;',
  29235. '$mod.b = $mod.c == $mod.v;',
  29236. '$mod.b = $mod.m == $mod.m;',
  29237. '$mod.b = $mod.v == $mod.m;',
  29238. '$mod.b = $mod.m == $mod.v;',
  29239. '']));
  29240. end;
  29241. procedure TTestModule.TestJSValue_If;
  29242. begin
  29243. StartProgram(false);
  29244. Add([
  29245. 'procedure Fly(var u);',
  29246. 'begin',
  29247. ' if jsvalue(u) then ;',
  29248. 'end;',
  29249. 'var',
  29250. ' v: jsvalue;',
  29251. 'begin',
  29252. ' if v then ;',
  29253. ' while v do ;',
  29254. ' repeat until v;',
  29255. '']);
  29256. ConvertProgram;
  29257. CheckSource('TestJSValue_If',
  29258. LinesToStr([ // statements
  29259. 'this.Fly = function (u) {',
  29260. ' if (u.get()) ;',
  29261. '};',
  29262. 'this.v = undefined;',
  29263. '']),
  29264. LinesToStr([ // $mod.$main
  29265. 'if ($mod.v) ;',
  29266. 'while($mod.v){',
  29267. '};',
  29268. 'do{',
  29269. '} while(!$mod.v);',
  29270. '']));
  29271. end;
  29272. procedure TTestModule.TestJSValue_Not;
  29273. begin
  29274. StartProgram(false);
  29275. Add([
  29276. 'var',
  29277. ' v: jsvalue;',
  29278. ' b: boolean;',
  29279. 'begin',
  29280. ' b:=not v;',
  29281. ' if not v then ;',
  29282. ' while not v do ;',
  29283. ' repeat until not v;',
  29284. '']);
  29285. ConvertProgram;
  29286. CheckSource('TestJSValue_If',
  29287. LinesToStr([ // statements
  29288. 'this.v = undefined;',
  29289. 'this.b = false;',
  29290. '']),
  29291. LinesToStr([ // $mod.$main
  29292. '$mod.b=!$mod.v;',
  29293. 'if (!$mod.v) ;',
  29294. 'while(!$mod.v){',
  29295. '};',
  29296. 'do{',
  29297. '} while($mod.v);',
  29298. '']));
  29299. end;
  29300. procedure TTestModule.TestJSValue_Enum;
  29301. begin
  29302. StartProgram(false);
  29303. Add('type');
  29304. Add(' TColor = (red, blue);');
  29305. Add(' TRedBlue = TColor;');
  29306. Add('var');
  29307. Add(' v: jsvalue;');
  29308. Add(' e: TColor;');
  29309. Add('begin');
  29310. Add(' v:=e;');
  29311. Add(' v:=TColor(e);');
  29312. Add(' v:=TRedBlue(e);');
  29313. Add(' e:=TColor(v);');
  29314. Add(' e:=TRedBlue(v);');
  29315. ConvertProgram;
  29316. CheckSource('TestJSValue_Enum',
  29317. LinesToStr([ // statements
  29318. 'this.TColor = {',
  29319. ' "0": "red",',
  29320. ' red: 0,',
  29321. ' "1": "blue",',
  29322. ' blue: 1',
  29323. '};',
  29324. 'this.v = undefined;',
  29325. 'this.e = 0;',
  29326. '']),
  29327. LinesToStr([ // $mod.$main
  29328. '$mod.v = $mod.e;',
  29329. '$mod.v = $mod.e;',
  29330. '$mod.v = $mod.e;',
  29331. '$mod.e = $mod.v;',
  29332. '$mod.e = $mod.v;',
  29333. '']));
  29334. end;
  29335. procedure TTestModule.TestJSValue_ClassInstance;
  29336. begin
  29337. StartProgram(false);
  29338. Add([
  29339. 'type',
  29340. ' TObject = class',
  29341. ' end;',
  29342. ' TBirdObject = TObject;',
  29343. 'var',
  29344. ' v: jsvalue;',
  29345. ' o: TObject;',
  29346. 'begin',
  29347. ' v:=o;',
  29348. ' v:=TObject(o);',
  29349. ' v:=TBirdObject(o);',
  29350. ' o:=TObject(v);',
  29351. ' o:=TBirdObject(v);',
  29352. ' if v is TObject then ;',
  29353. '']);
  29354. ConvertProgram;
  29355. CheckSource('TestJSValue_ClassInstance',
  29356. LinesToStr([ // statements
  29357. 'rtl.createClass(this, "TObject", null, function () {',
  29358. ' this.$init = function () {',
  29359. ' };',
  29360. ' this.$final = function () {',
  29361. ' };',
  29362. '});',
  29363. 'this.v = undefined;',
  29364. 'this.o = null;',
  29365. '']),
  29366. LinesToStr([ // $mod.$main
  29367. '$mod.v = $mod.o;',
  29368. '$mod.v = $mod.o;',
  29369. '$mod.v = $mod.o;',
  29370. '$mod.o = rtl.getObject($mod.v);',
  29371. '$mod.o = rtl.getObject($mod.v);',
  29372. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29373. '']));
  29374. end;
  29375. procedure TTestModule.TestJSValue_ClassOf;
  29376. begin
  29377. StartProgram(false);
  29378. Add([
  29379. 'type',
  29380. ' TClass = class of TObject;',
  29381. ' TObject = class',
  29382. ' end;',
  29383. ' TBirds = class of TBird;',
  29384. ' TBird = class(TObject) end;',
  29385. 'var',
  29386. ' v: jsvalue;',
  29387. ' c: TClass;',
  29388. 'begin',
  29389. ' v:=c;',
  29390. ' v:=TObject;',
  29391. ' v:=TClass(c);',
  29392. ' v:=TBirds(c);',
  29393. ' c:=TClass(v);',
  29394. ' c:=TBirds(v);',
  29395. ' if v is TClass then ;',
  29396. '']);
  29397. ConvertProgram;
  29398. CheckSource('TestJSValue_ClassOf',
  29399. LinesToStr([ // statements
  29400. 'rtl.createClass(this, "TObject", null, function () {',
  29401. ' this.$init = function () {',
  29402. ' };',
  29403. ' this.$final = function () {',
  29404. ' };',
  29405. '});',
  29406. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29407. '});',
  29408. 'this.v = undefined;',
  29409. 'this.c = null;',
  29410. '']),
  29411. LinesToStr([ // $mod.$main
  29412. '$mod.v = $mod.c;',
  29413. '$mod.v = $mod.TObject;',
  29414. '$mod.v = $mod.c;',
  29415. '$mod.v = $mod.c;',
  29416. '$mod.c = rtl.getObject($mod.v);',
  29417. '$mod.c = rtl.getObject($mod.v);',
  29418. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29419. '']));
  29420. end;
  29421. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29422. begin
  29423. StartProgram(false);
  29424. Add([
  29425. 'type',
  29426. ' integer = longint;',
  29427. ' TArray = array of JSValue;',
  29428. ' TArrgh = tarray;',
  29429. ' TArrInt = array of integer;',
  29430. 'var',
  29431. ' v: jsvalue;',
  29432. ' TheArray: tarray = (1,''2'');',
  29433. ' Arr: tarrgh;',
  29434. ' i: integer;',
  29435. ' ArrInt: tarrint;',
  29436. 'begin',
  29437. ' arr:=thearray;',
  29438. ' thearray:=arr;',
  29439. ' setlength(arr,2);',
  29440. ' setlength(thearray,3);',
  29441. ' arr[4]:=v;',
  29442. ' arr[5]:=length(thearray);',
  29443. ' arr[6]:=nil;',
  29444. ' arr[7]:=thearray[8];',
  29445. ' arr[low(arr)]:=high(thearray);',
  29446. ' arr:=arrint;',
  29447. ' arrInt:=tarrint(arr);',
  29448. ' if TheArray = nil then ;',
  29449. ' if nil = TheArray then ;',
  29450. ' if TheArray <> nil then ;',
  29451. ' if nil <> TheArray then ;',
  29452. '']);
  29453. ConvertProgram;
  29454. CheckSource('TestJSValue_ArrayOfJSValue',
  29455. LinesToStr([ // statements
  29456. 'this.v = undefined;',
  29457. 'this.TheArray = [1, "2"];',
  29458. 'this.Arr = [];',
  29459. 'this.i = 0;',
  29460. 'this.ArrInt = [];',
  29461. '']),
  29462. LinesToStr([ // $mod.$main
  29463. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29464. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29465. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29466. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29467. '$mod.Arr[4] = $mod.v;',
  29468. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29469. '$mod.Arr[6] = null;',
  29470. '$mod.Arr[7] = $mod.TheArray[8];',
  29471. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29472. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29473. '$mod.ArrInt = $mod.Arr;',
  29474. 'if (rtl.length($mod.TheArray) === 0) ;',
  29475. 'if (rtl.length($mod.TheArray) === 0) ;',
  29476. 'if (rtl.length($mod.TheArray) > 0) ;',
  29477. 'if (rtl.length($mod.TheArray) > 0) ;',
  29478. '']));
  29479. end;
  29480. procedure TTestModule.TestJSValue_ArrayLit;
  29481. begin
  29482. StartProgram(false);
  29483. Add([
  29484. 'type',
  29485. ' TFlag = (big,small);',
  29486. ' TArray = array of JSValue;',
  29487. ' TObject = class end;',
  29488. ' TClass = class of TObject;',
  29489. 'var',
  29490. ' v: jsvalue;',
  29491. ' a: TArray;',
  29492. ' o: TObject;',
  29493. 'begin',
  29494. ' a:=[];',
  29495. ' a:=[1];',
  29496. ' a:=[1,2];',
  29497. ' a:=[big];',
  29498. ' a:=[1,big];',
  29499. ' a:=[o,nil];',
  29500. '']);
  29501. ConvertProgram;
  29502. CheckSource('TestJSValue_ArrayLit',
  29503. LinesToStr([ // statements
  29504. 'this.TFlag = {',
  29505. ' "0": "big",',
  29506. ' big: 0,',
  29507. ' "1": "small",',
  29508. ' small: 1',
  29509. '};',
  29510. 'rtl.createClass(this, "TObject", null, function () {',
  29511. ' this.$init = function () {',
  29512. ' };',
  29513. ' this.$final = function () {',
  29514. ' };',
  29515. '});',
  29516. 'this.v = undefined;',
  29517. 'this.a = [];',
  29518. 'this.o = null;',
  29519. '']),
  29520. LinesToStr([ // $mod.$main
  29521. '$mod.a = [];',
  29522. '$mod.a = [1];',
  29523. '$mod.a = [1, 2];',
  29524. '$mod.a = [$mod.TFlag.big];',
  29525. '$mod.a = [1, $mod.TFlag.big];',
  29526. '$mod.a = [$mod.o, null];',
  29527. '']));
  29528. end;
  29529. procedure TTestModule.TestJSValue_Params;
  29530. begin
  29531. StartProgram(false);
  29532. Add('type');
  29533. Add(' integer = longint;');
  29534. Add(' TYesNo = boolean;');
  29535. Add(' TFloat = double;');
  29536. Add(' TCaption = string;');
  29537. Add(' TChar = char;');
  29538. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29539. Add('var');
  29540. Add(' l: jsvalue;');
  29541. Add('begin');
  29542. Add(' a:=a;');
  29543. Add(' l:=b;');
  29544. Add(' c:=c;');
  29545. Add(' d:=d;');
  29546. Add(' Result:=l;');
  29547. Add('end;');
  29548. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29549. Add('var');
  29550. Add(' v: jsvalue;');
  29551. Add(' i: integer;');
  29552. Add(' b: TYesNo;');
  29553. Add(' d: TFloat;');
  29554. Add(' s: TCaption;');
  29555. Add(' c: TChar;');
  29556. Add('begin');
  29557. Add(' v:=doit(v,v,v,v);');
  29558. Add(' i:=integer(dosome(i,i));');
  29559. Add(' b:=TYesNo(dosome(b,b));');
  29560. Add(' d:=TFloat(dosome(d,d));');
  29561. Add(' s:=TCaption(dosome(s,s));');
  29562. Add(' c:=TChar(dosome(c,c));');
  29563. ConvertProgram;
  29564. CheckSource('TestJSValue_Params',
  29565. LinesToStr([ // statements
  29566. 'this.DoIt = function (a, b, c, d) {',
  29567. ' var Result = undefined;',
  29568. ' var l = undefined;',
  29569. ' a = a;',
  29570. ' l = b;',
  29571. ' c.set(c.get());',
  29572. ' d.set(d.get());',
  29573. ' Result = l;',
  29574. ' return Result;',
  29575. '};',
  29576. 'this.DoSome = function (a, b) {',
  29577. ' var Result = undefined;',
  29578. ' return Result;',
  29579. '};',
  29580. 'this.v = undefined;',
  29581. 'this.i = 0;',
  29582. 'this.b = false;',
  29583. 'this.d = 0.0;',
  29584. 'this.s = "";',
  29585. 'this.c = "";',
  29586. '']),
  29587. LinesToStr([ // $mod.$main
  29588. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29589. ' p: $mod,',
  29590. ' get: function () {',
  29591. ' return this.p.v;',
  29592. ' },',
  29593. ' set: function (v) {',
  29594. ' this.p.v = v;',
  29595. ' }',
  29596. '}, {',
  29597. ' p: $mod,',
  29598. ' get: function () {',
  29599. ' return this.p.v;',
  29600. ' },',
  29601. ' set: function (v) {',
  29602. ' this.p.v = v;',
  29603. ' }',
  29604. '});',
  29605. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29606. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29607. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29608. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29609. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29610. '']));
  29611. end;
  29612. procedure TTestModule.TestJSValue_UntypedParam;
  29613. begin
  29614. StartProgram(false);
  29615. Add('function DoIt(const a; var b; out c): jsvalue;');
  29616. Add('begin');
  29617. Add(' Result:=a;');
  29618. Add(' Result:=b;');
  29619. Add(' Result:=c;');
  29620. Add(' b:=Result;');
  29621. Add(' c:=Result;');
  29622. Add('end;');
  29623. Add('var i: longint;');
  29624. Add('begin');
  29625. Add(' doit(i,i,i);');
  29626. ConvertProgram;
  29627. CheckSource('TestJSValue_UntypedParam',
  29628. LinesToStr([ // statements
  29629. 'this.DoIt = function (a, b, c) {',
  29630. ' var Result = undefined;',
  29631. ' Result = a;',
  29632. ' Result = b.get();',
  29633. ' Result = c.get();',
  29634. ' b.set(Result);',
  29635. ' c.set(Result);',
  29636. ' return Result;',
  29637. '};',
  29638. 'this.i = 0;',
  29639. '']),
  29640. LinesToStr([ // $mod.$main
  29641. '$mod.DoIt($mod.i, {',
  29642. ' p: $mod,',
  29643. ' get: function () {',
  29644. ' return this.p.i;',
  29645. ' },',
  29646. ' set: function (v) {',
  29647. ' this.p.i = v;',
  29648. ' }',
  29649. '}, {',
  29650. ' p: $mod,',
  29651. ' get: function () {',
  29652. ' return this.p.i;',
  29653. ' },',
  29654. ' set: function (v) {',
  29655. ' this.p.i = v;',
  29656. ' }',
  29657. '});',
  29658. '']));
  29659. end;
  29660. procedure TTestModule.TestJSValue_FuncResultType;
  29661. begin
  29662. StartProgram(false);
  29663. Add('type');
  29664. Add(' integer = longint;');
  29665. Add(' TJSValueArray = array of JSValue;');
  29666. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29667. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29668. Add('begin');
  29669. Add(' while Compare(P,aList[0])>0 do ;');
  29670. Add('end;');
  29671. Add('var');
  29672. Add(' Compare: TListSortCompare;');
  29673. Add(' V: JSValue;');
  29674. Add(' i: integer;');
  29675. Add('begin');
  29676. Add(' if Compare(V,V)>0 then ;');
  29677. Add(' if Compare(i,i)>1 then ;');
  29678. Add(' if Compare(nil,false)>2 then ;');
  29679. Add(' if Compare(1,true)>3 then ;');
  29680. ConvertProgram;
  29681. CheckSource('TestJSValue_UntypedParam',
  29682. LinesToStr([ // statements
  29683. 'this.Sort = function (P, aList, Compare) {',
  29684. ' while (Compare(P, aList[0]) > 0) {',
  29685. ' };',
  29686. '};',
  29687. 'this.Compare = null;',
  29688. 'this.V = undefined;',
  29689. 'this.i = 0;',
  29690. '']),
  29691. LinesToStr([ // $mod.$main
  29692. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29693. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29694. 'if ($mod.Compare(null, false) > 2) ;',
  29695. 'if ($mod.Compare(1, true) > 3) ;',
  29696. '']));
  29697. end;
  29698. procedure TTestModule.TestJSValue_ProcType_Assign;
  29699. begin
  29700. StartProgram(false);
  29701. Add('type');
  29702. Add(' integer = longint;');
  29703. Add(' TObject = class');
  29704. Add(' class function GetGlob: integer;');
  29705. Add(' function Getter: integer;');
  29706. Add(' end;');
  29707. Add('class function TObject.GetGlob: integer;');
  29708. Add('var v1: jsvalue;');
  29709. Add('begin');
  29710. Add(' v1:=@GetGlob;');
  29711. Add(' v1:[email protected];');
  29712. Add('end;');
  29713. Add('function TObject.Getter: integer;');
  29714. Add('var v2: jsvalue;');
  29715. Add('begin');
  29716. Add(' v2:=@Getter;');
  29717. Add(' v2:[email protected];');
  29718. Add(' v2:=@GetGlob;');
  29719. Add(' v2:[email protected];');
  29720. Add('end;');
  29721. Add('function GetIt(i: integer): integer;');
  29722. Add('var v3: jsvalue;');
  29723. Add('begin');
  29724. Add(' v3:=@GetIt;');
  29725. Add('end;');
  29726. Add('var');
  29727. Add(' V: JSValue;');
  29728. Add(' o: TObject;');
  29729. Add('begin');
  29730. Add(' v:=@GetIt;');
  29731. Add(' v:[email protected];');
  29732. Add(' v:[email protected];');
  29733. ConvertProgram;
  29734. CheckSource('TestJSValue_ProcType_Assign',
  29735. LinesToStr([ // statements
  29736. 'rtl.createClass(this, "TObject", null, function () {',
  29737. ' this.$init = function () {',
  29738. ' };',
  29739. ' this.$final = function () {',
  29740. ' };',
  29741. ' this.GetGlob = function () {',
  29742. ' var Result = 0;',
  29743. ' var v1 = undefined;',
  29744. ' v1 = rtl.createCallback(this, "GetGlob");',
  29745. ' v1 = rtl.createCallback(this, "GetGlob");',
  29746. ' return Result;',
  29747. ' };',
  29748. ' this.Getter = function () {',
  29749. ' var Result = 0;',
  29750. ' var v2 = undefined;',
  29751. ' v2 = rtl.createCallback(this, "Getter");',
  29752. ' v2 = rtl.createCallback(this, "Getter");',
  29753. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29754. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29755. ' return Result;',
  29756. ' };',
  29757. '});',
  29758. 'this.GetIt = function (i) {',
  29759. ' var Result = 0;',
  29760. ' var v3 = undefined;',
  29761. ' v3 = $mod.GetIt;',
  29762. ' return Result;',
  29763. '};',
  29764. 'this.V = undefined;',
  29765. 'this.o = null;',
  29766. '']),
  29767. LinesToStr([ // $mod.$main
  29768. '$mod.V = $mod.GetIt;',
  29769. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29770. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29771. '']));
  29772. end;
  29773. procedure TTestModule.TestJSValue_ProcType_Equal;
  29774. begin
  29775. StartProgram(false);
  29776. Add('type');
  29777. Add(' integer = longint;');
  29778. Add(' TObject = class');
  29779. Add(' class function GetGlob: integer;');
  29780. Add(' function Getter: integer;');
  29781. Add(' end;');
  29782. Add('class function TObject.GetGlob: integer;');
  29783. Add('var v1: jsvalue;');
  29784. Add('begin');
  29785. Add(' if v1=@GetGlob then;');
  29786. Add(' if [email protected] then ;');
  29787. Add('end;');
  29788. Add('function TObject.Getter: integer;');
  29789. Add('var v2: jsvalue;');
  29790. Add('begin');
  29791. Add(' if v2=@Getter then;');
  29792. Add(' if [email protected] then ;');
  29793. Add(' if v2=@GetGlob then;');
  29794. Add(' if [email protected] then;');
  29795. Add('end;');
  29796. Add('function GetIt(i: integer): integer;');
  29797. Add('var v3: jsvalue;');
  29798. Add('begin');
  29799. Add(' if v3=@GetIt then;');
  29800. Add('end;');
  29801. Add('var');
  29802. Add(' V: JSValue;');
  29803. Add(' o: TObject;');
  29804. Add('begin');
  29805. Add(' if v=@GetIt then;');
  29806. Add(' if [email protected] then;');
  29807. Add(' if [email protected] then;');
  29808. Add(' if @GetIt=v then;');
  29809. Add(' if @o.Getter=v then;');
  29810. Add(' if @o.GetGlob=v then;');
  29811. ConvertProgram;
  29812. CheckSource('TestJSValue_ProcType_Equal',
  29813. LinesToStr([ // statements
  29814. 'rtl.createClass(this, "TObject", null, function () {',
  29815. ' this.$init = function () {',
  29816. ' };',
  29817. ' this.$final = function () {',
  29818. ' };',
  29819. ' this.GetGlob = function () {',
  29820. ' var Result = 0;',
  29821. ' var v1 = undefined;',
  29822. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29823. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29824. ' return Result;',
  29825. ' };',
  29826. ' this.Getter = function () {',
  29827. ' var Result = 0;',
  29828. ' var v2 = undefined;',
  29829. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29830. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29831. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29832. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29833. ' return Result;',
  29834. ' };',
  29835. '});',
  29836. 'this.GetIt = function (i) {',
  29837. ' var Result = 0;',
  29838. ' var v3 = undefined;',
  29839. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29840. ' return Result;',
  29841. '};',
  29842. 'this.V = undefined;',
  29843. 'this.o = null;',
  29844. '']),
  29845. LinesToStr([ // $mod.$main
  29846. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29847. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29848. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29849. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29850. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29851. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29852. '']));
  29853. end;
  29854. procedure TTestModule.TestJSValue_ProcType_Param;
  29855. begin
  29856. StartProgram(false);
  29857. Add([
  29858. 'type',
  29859. ' variant = jsvalue;',
  29860. ' TArrVariant = array of variant;',
  29861. ' TArrVar2 = TArrVariant;',
  29862. ' TFuncInt = function: longint;',
  29863. 'function GetIt: longint;',
  29864. 'begin',
  29865. 'end;',
  29866. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29867. 'var v: variant;',
  29868. 'begin',
  29869. ' v:=arr[1];',
  29870. 'end;',
  29871. 'var s: string;',
  29872. 'begin',
  29873. ' DoIt(GetIt,[]);',
  29874. ' DoIt(@GetIt,[]);',
  29875. ' DoIt(1,[s,GetIt]);',
  29876. ' DoIt(1,[s,@GetIt]);',
  29877. '']);
  29878. ConvertProgram;
  29879. CheckSource('TestJSValue_ProcType_Param',
  29880. LinesToStr([ // statements
  29881. 'this.GetIt = function () {',
  29882. ' var Result = 0;',
  29883. ' return Result;',
  29884. '};',
  29885. 'this.DoIt = function (p, Arr) {',
  29886. ' var v = undefined;',
  29887. ' v = Arr[1];',
  29888. '};',
  29889. 'this.s = "";',
  29890. '']),
  29891. LinesToStr([ // $mod.$main
  29892. '$mod.DoIt($mod.GetIt(), []);',
  29893. '$mod.DoIt($mod.GetIt, []);',
  29894. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29895. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29896. '']));
  29897. end;
  29898. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29899. begin
  29900. StartProgram(false);
  29901. Add([
  29902. 'var',
  29903. ' v: JSValue;',
  29904. ' p: Pointer;',
  29905. 'begin',
  29906. ' p:=v;',
  29907. '']);
  29908. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29909. nIncompatibleTypesGotExpected);
  29910. ConvertProgram;
  29911. end;
  29912. procedure TTestModule.TestJSValue_OverloadDouble;
  29913. begin
  29914. StartProgram(false);
  29915. Add([
  29916. 'type',
  29917. ' integer = longint;',
  29918. ' tdatetime = double;',
  29919. 'procedure DoIt(d: double); begin end;',
  29920. 'procedure DoIt(v: jsvalue); begin end;',
  29921. 'var',
  29922. ' d: double;',
  29923. ' dt: tdatetime;',
  29924. ' i: integer;',
  29925. ' b: byte;',
  29926. ' shi: shortint;',
  29927. ' w: word;',
  29928. ' smi: smallint;',
  29929. ' lw: longword;',
  29930. ' li: longint;',
  29931. ' ni: nativeint;',
  29932. ' nu: nativeuint;',
  29933. 'begin',
  29934. ' DoIt(d);',
  29935. ' DoIt(dt);',
  29936. ' DoIt(i);',
  29937. ' DoIt(b);',
  29938. ' DoIt(shi);',
  29939. ' DoIt(w);',
  29940. ' DoIt(smi);',
  29941. ' DoIt(lw);',
  29942. ' DoIt(li);',
  29943. ' DoIt(ni);',
  29944. ' DoIt(nu);',
  29945. '']);
  29946. ConvertProgram;
  29947. CheckSource('TestJSValue_OverloadDouble',
  29948. LinesToStr([ // statements
  29949. 'this.DoIt = function (d) {',
  29950. '};',
  29951. 'this.DoIt$1 = function (v) {',
  29952. '};',
  29953. 'this.d = 0.0;',
  29954. 'this.dt = 0.0;',
  29955. 'this.i = 0;',
  29956. 'this.b = 0;',
  29957. 'this.shi = 0;',
  29958. 'this.w = 0;',
  29959. 'this.smi = 0;',
  29960. 'this.lw = 0;',
  29961. 'this.li = 0;',
  29962. 'this.ni = 0;',
  29963. 'this.nu = 0;',
  29964. '']),
  29965. LinesToStr([ // $mod.$main
  29966. '$mod.DoIt($mod.d);',
  29967. '$mod.DoIt($mod.dt);',
  29968. '$mod.DoIt$1($mod.i);',
  29969. '$mod.DoIt$1($mod.b);',
  29970. '$mod.DoIt$1($mod.shi);',
  29971. '$mod.DoIt$1($mod.w);',
  29972. '$mod.DoIt$1($mod.smi);',
  29973. '$mod.DoIt$1($mod.lw);',
  29974. '$mod.DoIt$1($mod.li);',
  29975. '$mod.DoIt$1($mod.ni);',
  29976. '$mod.DoIt$1($mod.nu);',
  29977. '']));
  29978. end;
  29979. procedure TTestModule.TestJSValue_OverloadNativeInt;
  29980. begin
  29981. StartProgram(false);
  29982. Add([
  29983. 'type',
  29984. ' integer = longint;',
  29985. ' int53 = nativeint;',
  29986. ' tdatetime = double;',
  29987. 'procedure DoIt(n: nativeint); begin end;',
  29988. 'procedure DoIt(v: jsvalue); begin end;',
  29989. 'var',
  29990. ' d: double;',
  29991. ' dt: tdatetime;',
  29992. ' i: integer;',
  29993. ' b: byte;',
  29994. ' shi: shortint;',
  29995. ' w: word;',
  29996. ' smi: smallint;',
  29997. ' lw: longword;',
  29998. ' li: longint;',
  29999. ' ni: nativeint;',
  30000. ' nu: nativeuint;',
  30001. 'begin',
  30002. ' DoIt(d);',
  30003. ' DoIt(dt);',
  30004. ' DoIt(i);',
  30005. ' DoIt(b);',
  30006. ' DoIt(shi);',
  30007. ' DoIt(w);',
  30008. ' DoIt(smi);',
  30009. ' DoIt(lw);',
  30010. ' DoIt(li);',
  30011. ' DoIt(ni);',
  30012. ' DoIt(nu);',
  30013. '']);
  30014. ConvertProgram;
  30015. CheckSource('TestJSValue_OverloadNativeInt',
  30016. LinesToStr([ // statements
  30017. 'this.DoIt = function (n) {',
  30018. '};',
  30019. 'this.DoIt$1 = function (v) {',
  30020. '};',
  30021. 'this.d = 0.0;',
  30022. 'this.dt = 0.0;',
  30023. 'this.i = 0;',
  30024. 'this.b = 0;',
  30025. 'this.shi = 0;',
  30026. 'this.w = 0;',
  30027. 'this.smi = 0;',
  30028. 'this.lw = 0;',
  30029. 'this.li = 0;',
  30030. 'this.ni = 0;',
  30031. 'this.nu = 0;',
  30032. '']),
  30033. LinesToStr([ // $mod.$main
  30034. '$mod.DoIt$1($mod.d);',
  30035. '$mod.DoIt$1($mod.dt);',
  30036. '$mod.DoIt($mod.i);',
  30037. '$mod.DoIt($mod.b);',
  30038. '$mod.DoIt($mod.shi);',
  30039. '$mod.DoIt($mod.w);',
  30040. '$mod.DoIt($mod.smi);',
  30041. '$mod.DoIt($mod.lw);',
  30042. '$mod.DoIt($mod.li);',
  30043. '$mod.DoIt($mod.ni);',
  30044. '$mod.DoIt($mod.nu);',
  30045. '']));
  30046. end;
  30047. procedure TTestModule.TestJSValue_OverloadWord;
  30048. begin
  30049. StartProgram(false);
  30050. Add([
  30051. 'type',
  30052. ' integer = longint;',
  30053. ' int53 = nativeint;',
  30054. ' tdatetime = double;',
  30055. 'procedure DoIt(w: word); begin end;',
  30056. 'procedure DoIt(v: jsvalue); begin end;',
  30057. 'var',
  30058. ' d: double;',
  30059. ' dt: tdatetime;',
  30060. ' i: integer;',
  30061. ' b: byte;',
  30062. ' shi: shortint;',
  30063. ' w: word;',
  30064. ' smi: smallint;',
  30065. ' lw: longword;',
  30066. ' li: longint;',
  30067. ' ni: nativeint;',
  30068. ' nu: nativeuint;',
  30069. 'begin',
  30070. ' DoIt(d);',
  30071. ' DoIt(dt);',
  30072. ' DoIt(i);',
  30073. ' DoIt(b);',
  30074. ' DoIt(shi);',
  30075. ' DoIt(w);',
  30076. ' DoIt(smi);',
  30077. ' DoIt(lw);',
  30078. ' DoIt(li);',
  30079. ' DoIt(ni);',
  30080. ' DoIt(nu);',
  30081. '']);
  30082. ConvertProgram;
  30083. CheckSource('TestJSValue_OverloadWord',
  30084. LinesToStr([ // statements
  30085. 'this.DoIt = function (w) {',
  30086. '};',
  30087. 'this.DoIt$1 = function (v) {',
  30088. '};',
  30089. 'this.d = 0.0;',
  30090. 'this.dt = 0.0;',
  30091. 'this.i = 0;',
  30092. 'this.b = 0;',
  30093. 'this.shi = 0;',
  30094. 'this.w = 0;',
  30095. 'this.smi = 0;',
  30096. 'this.lw = 0;',
  30097. 'this.li = 0;',
  30098. 'this.ni = 0;',
  30099. 'this.nu = 0;',
  30100. '']),
  30101. LinesToStr([ // $mod.$main
  30102. '$mod.DoIt$1($mod.d);',
  30103. '$mod.DoIt$1($mod.dt);',
  30104. '$mod.DoIt$1($mod.i);',
  30105. '$mod.DoIt($mod.b);',
  30106. '$mod.DoIt($mod.shi);',
  30107. '$mod.DoIt($mod.w);',
  30108. '$mod.DoIt$1($mod.smi);',
  30109. '$mod.DoIt$1($mod.lw);',
  30110. '$mod.DoIt$1($mod.li);',
  30111. '$mod.DoIt$1($mod.ni);',
  30112. '$mod.DoIt$1($mod.nu);',
  30113. '']));
  30114. end;
  30115. procedure TTestModule.TestJSValue_OverloadString;
  30116. begin
  30117. StartProgram(false);
  30118. Add([
  30119. 'type',
  30120. ' uni = string;',
  30121. ' WChar = char;',
  30122. 'procedure DoIt(s: string); begin end;',
  30123. 'procedure DoIt(v: jsvalue); begin end;',
  30124. 'var',
  30125. ' s: string;',
  30126. ' c: char;',
  30127. ' u: uni;',
  30128. 'begin',
  30129. ' DoIt(s);',
  30130. ' DoIt(c);',
  30131. ' DoIt(u);',
  30132. '']);
  30133. ConvertProgram;
  30134. CheckSource('TestJSValue_OverloadString',
  30135. LinesToStr([ // statements
  30136. 'this.DoIt = function (s) {',
  30137. '};',
  30138. 'this.DoIt$1 = function (v) {',
  30139. '};',
  30140. 'this.s = "";',
  30141. 'this.c = "";',
  30142. 'this.u = "";',
  30143. '']),
  30144. LinesToStr([ // $mod.$main
  30145. '$mod.DoIt($mod.s);',
  30146. '$mod.DoIt($mod.c);',
  30147. '$mod.DoIt($mod.u);',
  30148. '']));
  30149. end;
  30150. procedure TTestModule.TestJSValue_OverloadChar;
  30151. begin
  30152. StartProgram(false);
  30153. Add([
  30154. 'type',
  30155. ' uni = string;',
  30156. ' WChar = char;',
  30157. 'procedure DoIt(c: char); begin end;',
  30158. 'procedure DoIt(v: jsvalue); begin end;',
  30159. 'var',
  30160. ' s: string;',
  30161. ' c: char;',
  30162. ' u: uni;',
  30163. 'begin',
  30164. ' DoIt(s);',
  30165. ' DoIt(c);',
  30166. ' DoIt(u);',
  30167. '']);
  30168. ConvertProgram;
  30169. CheckSource('TestJSValue_OverloadChar',
  30170. LinesToStr([ // statements
  30171. 'this.DoIt = function (c) {',
  30172. '};',
  30173. 'this.DoIt$1 = function (v) {',
  30174. '};',
  30175. 'this.s = "";',
  30176. 'this.c = "";',
  30177. 'this.u = "";',
  30178. '']),
  30179. LinesToStr([ // $mod.$main
  30180. '$mod.DoIt$1($mod.s);',
  30181. '$mod.DoIt($mod.c);',
  30182. '$mod.DoIt$1($mod.u);',
  30183. '']));
  30184. end;
  30185. procedure TTestModule.TestJSValue_OverloadPointer;
  30186. begin
  30187. StartProgram(false);
  30188. Add([
  30189. 'type',
  30190. ' TObject = class end;',
  30191. 'procedure DoIt(p: pointer); begin end;',
  30192. 'procedure DoIt(v: jsvalue); begin end;',
  30193. 'var',
  30194. ' o: TObject;',
  30195. 'begin',
  30196. ' DoIt(o);',
  30197. '']);
  30198. ConvertProgram;
  30199. CheckSource('TestJSValue_OverloadPointer',
  30200. LinesToStr([ // statements
  30201. 'rtl.createClass(this, "TObject", null, function () {',
  30202. ' this.$init = function () {',
  30203. ' };',
  30204. ' this.$final = function () {',
  30205. ' };',
  30206. '});',
  30207. 'this.DoIt = function (p) {',
  30208. '};',
  30209. 'this.DoIt$1 = function (v) {',
  30210. '};',
  30211. 'this.o = null;',
  30212. '']),
  30213. LinesToStr([ // $mod.$main
  30214. '$mod.DoIt($mod.o);',
  30215. '']));
  30216. end;
  30217. procedure TTestModule.TestJSValue_ForIn;
  30218. begin
  30219. StartProgram(false);
  30220. Add([
  30221. 'var',
  30222. ' v: JSValue;',
  30223. ' key: string;',
  30224. 'begin',
  30225. ' for key in v do begin',
  30226. ' if key=''abc'' then ;',
  30227. ' end;',
  30228. '']);
  30229. ConvertProgram;
  30230. CheckSource('TestJSValue_ForIn',
  30231. LinesToStr([ // statements
  30232. 'this.v = undefined;',
  30233. 'this.key = "";',
  30234. '']),
  30235. LinesToStr([ // $mod.$main
  30236. 'for ($mod.key in $mod.v) {',
  30237. ' if ($mod.key === "abc") ;',
  30238. '};',
  30239. '']));
  30240. end;
  30241. procedure TTestModule.TestRTTI_IntRange;
  30242. begin
  30243. WithTypeInfo:=true;
  30244. StartProgram(true,[supTypeInfo]);
  30245. Add([
  30246. '{$modeswitch externalclass}',
  30247. 'type',
  30248. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  30249. ' TColor = type TGraphicsColor;',
  30250. 'var',
  30251. ' p: TTypeInfo;',
  30252. ' k: TTypeKind;',
  30253. 'begin',
  30254. ' p:=typeinfo(TGraphicsColor);',
  30255. ' p:=typeinfo(TColor);',
  30256. ' k:=GetTypeKind(TGraphicsColor);',
  30257. ' k:=GetTypeKind(TColor);',
  30258. '']);
  30259. ConvertProgram;
  30260. CheckSource('TestRTTI_IntRange',
  30261. LinesToStr([ // statements
  30262. 'this.$rtti.$Int("TGraphicsColor", {',
  30263. ' minvalue: -2147483648,',
  30264. ' maxvalue: 2147483647,',
  30265. ' ordtype: 4',
  30266. '});',
  30267. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  30268. 'this.p = null;',
  30269. 'this.k = 0;',
  30270. '']),
  30271. LinesToStr([ // $mod.$main
  30272. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  30273. '$mod.p = $mod.$rtti["TColor"];',
  30274. '$mod.k = 1;',
  30275. '$mod.k = 1;',
  30276. '']));
  30277. end;
  30278. procedure TTestModule.TestRTTI_Double;
  30279. begin
  30280. WithTypeInfo:=true;
  30281. StartProgram(true,[supTypeInfo]);
  30282. Add([
  30283. '{$modeswitch externalclass}',
  30284. 'type',
  30285. ' TFloat = type double;',
  30286. 'var',
  30287. ' p: TTypeInfo;',
  30288. 'begin',
  30289. ' p:=typeinfo(double);',
  30290. ' p:=typeinfo(TFloat);',
  30291. '']);
  30292. ConvertProgram;
  30293. CheckSource('TestRTTI_Double',
  30294. LinesToStr([ // statements
  30295. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30296. 'this.p = null;',
  30297. '']),
  30298. LinesToStr([ // $mod.$main
  30299. '$mod.p = rtl.double;',
  30300. '$mod.p = $mod.$rtti["TFloat"];',
  30301. '']));
  30302. end;
  30303. procedure TTestModule.TestRTTI_ProcType;
  30304. begin
  30305. WithTypeInfo:=true;
  30306. StartProgram(false);
  30307. Add('type');
  30308. Add(' TProcA = procedure;');
  30309. Add(' TMethodB = procedure of object;');
  30310. Add(' TProcC = procedure; varargs;');
  30311. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  30312. Add(' TProcE = function: nativeint;');
  30313. Add(' TProcF = function(const p: TProcA): nativeuint;');
  30314. Add('var p: pointer;');
  30315. Add('begin');
  30316. Add(' p:=typeinfo(tproca);');
  30317. ConvertProgram;
  30318. CheckSource('TestRTTI_ProcType',
  30319. LinesToStr([ // statements
  30320. 'this.$rtti.$ProcVar("TProcA", {',
  30321. ' procsig: rtl.newTIProcSig([])',
  30322. '});',
  30323. 'this.$rtti.$MethodVar("TMethodB", {',
  30324. ' procsig: rtl.newTIProcSig([]),',
  30325. ' methodkind: 0',
  30326. '});',
  30327. 'this.$rtti.$ProcVar("TProcC", {',
  30328. ' procsig: rtl.newTIProcSig([], null, 2)',
  30329. '});',
  30330. 'this.$rtti.$ProcVar("TProcD", {',
  30331. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30332. '});',
  30333. 'this.$rtti.$ProcVar("TProcE", {',
  30334. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30335. '});',
  30336. 'this.$rtti.$ProcVar("TProcF", {',
  30337. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  30338. '});',
  30339. 'this.p = null;',
  30340. '']),
  30341. LinesToStr([ // $mod.$main
  30342. '$mod.p = $mod.$rtti["TProcA"];',
  30343. '']));
  30344. end;
  30345. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  30346. begin
  30347. WithTypeInfo:=true;
  30348. AddModuleWithIntfImplSrc('unit2.pas',
  30349. LinesToStr([
  30350. 'type',
  30351. ' TObject = class end;'
  30352. ]),
  30353. '');
  30354. StartUnit(true);
  30355. Add('interface');
  30356. Add('uses unit2;');
  30357. Add('type');
  30358. Add(' TProcA = function(o: tobject): tobject;');
  30359. Add('implementation');
  30360. Add('type');
  30361. Add(' TProcB = function(o: tobject): tobject;');
  30362. Add('var p: Pointer;');
  30363. Add('initialization');
  30364. Add(' p:=typeinfo(tproca);');
  30365. Add(' p:=typeinfo(tprocb);');
  30366. ConvertUnit;
  30367. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30368. LinesToStr([ // statements
  30369. 'var $impl = $mod.$impl;',
  30370. 'this.$rtti.$ProcVar("TProcA", {',
  30371. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30372. '});',
  30373. '']),
  30374. LinesToStr([ // this.$init
  30375. '$impl.p = $mod.$rtti["TProcA"];',
  30376. '$impl.p = $mod.$rtti["TProcB"];',
  30377. '']),
  30378. LinesToStr([ // implementation
  30379. '$mod.$rtti.$ProcVar("TProcB", {',
  30380. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30381. '});',
  30382. '$impl.p = null;',
  30383. '']) );
  30384. end;
  30385. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30386. begin
  30387. WithTypeInfo:=true;
  30388. StartProgram(false);
  30389. Add(['var',
  30390. ' ProcA: procedure;',
  30391. ' MethodB: procedure of object;',
  30392. ' ProcC: procedure; varargs;',
  30393. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  30394. ' ProcE: function: nativeint;',
  30395. ' p: pointer;',
  30396. 'begin',
  30397. ' p:=typeinfo(proca);']);
  30398. ConvertProgram;
  30399. CheckSource('TestRTTI_ProcTypeAnonymous',
  30400. LinesToStr([ // statements
  30401. 'this.$rtti.$ProcVar("ProcA$a", {',
  30402. ' procsig: rtl.newTIProcSig([])',
  30403. '});',
  30404. 'this.ProcA = null;',
  30405. 'this.$rtti.$MethodVar("MethodB$a", {',
  30406. ' procsig: rtl.newTIProcSig([]),',
  30407. ' methodkind: 0',
  30408. '});',
  30409. 'this.MethodB = null;',
  30410. 'this.$rtti.$ProcVar("ProcC$a", {',
  30411. ' procsig: rtl.newTIProcSig([], null, 2)',
  30412. '});',
  30413. 'this.ProcC = null;',
  30414. 'this.$rtti.$ProcVar("ProcD$a", {',
  30415. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30416. '});',
  30417. 'this.ProcD = null;',
  30418. 'this.$rtti.$ProcVar("ProcE$a", {',
  30419. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30420. '});',
  30421. 'this.ProcE = null;',
  30422. 'this.p = null;',
  30423. '']),
  30424. LinesToStr([ // $mod.$main
  30425. '$mod.p = $mod.$rtti["ProcA$a"];',
  30426. '']));
  30427. end;
  30428. procedure TTestModule.TestRTTI_EnumAndSetType;
  30429. begin
  30430. WithTypeInfo:=true;
  30431. StartProgram(false);
  30432. Add('type');
  30433. Add(' TFlag = (light,dark);');
  30434. Add(' TFlags = set of TFlag;');
  30435. Add(' TProc = function(f: TFlags): TFlag;');
  30436. Add('var p: pointer;');
  30437. Add('begin');
  30438. Add(' p:=typeinfo(tflag);');
  30439. Add(' p:=typeinfo(tflags);');
  30440. ConvertProgram;
  30441. CheckSource('TestRTTI_EnumAndType',
  30442. LinesToStr([ // statements
  30443. 'this.TFlag = {',
  30444. ' "0": "light",',
  30445. ' light: 0,',
  30446. ' "1": "dark",',
  30447. ' dark: 1',
  30448. '};',
  30449. 'this.$rtti.$Enum("TFlag", {',
  30450. ' minvalue: 0,',
  30451. ' maxvalue: 1,',
  30452. ' ordtype: 1,',
  30453. ' enumtype: this.TFlag',
  30454. '});',
  30455. 'this.$rtti.$Set("TFlags", {',
  30456. ' comptype: this.$rtti["TFlag"]',
  30457. '});',
  30458. 'this.$rtti.$ProcVar("TProc", {',
  30459. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30460. '});',
  30461. 'this.p = null;',
  30462. '']),
  30463. LinesToStr([ // $mod.$main
  30464. '$mod.p = $mod.$rtti["TFlag"];',
  30465. '$mod.p = $mod.$rtti["TFlags"];',
  30466. '']));
  30467. end;
  30468. procedure TTestModule.TestRTTI_EnumRange;
  30469. begin
  30470. WithTypeInfo:=true;
  30471. StartProgram(false);
  30472. Add([
  30473. 'type',
  30474. ' TCol = (red,green,blue);',
  30475. ' TColRg = green..blue;',
  30476. ' TSetOfColRg = set of TColRg;',
  30477. 'var p: pointer;',
  30478. 'begin',
  30479. ' p:=typeinfo(tcolrg);',
  30480. ' p:=typeinfo(tsetofcolrg);',
  30481. '']);
  30482. ConvertProgram;
  30483. end;
  30484. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30485. begin
  30486. WithTypeInfo:=true;
  30487. StartProgram(false);
  30488. Add('type');
  30489. Add(' TFlags = set of (red, green);');
  30490. Add('var');
  30491. Add(' f: TFlags;');
  30492. Add('begin');
  30493. Add(' Include(f,red);');
  30494. ConvertProgram;
  30495. CheckSource('TestRTTI_AnonymousEnumType',
  30496. LinesToStr([ // statements
  30497. 'this.TFlags$a = {',
  30498. ' "0": "red",',
  30499. ' red: 0,',
  30500. ' "1": "green",',
  30501. ' green: 1',
  30502. '};',
  30503. 'this.$rtti.$Enum("TFlags$a", {',
  30504. ' minvalue: 0,',
  30505. ' maxvalue: 1,',
  30506. ' ordtype: 1,',
  30507. ' enumtype: this.TFlags$a',
  30508. '});',
  30509. 'this.$rtti.$Set("TFlags", {',
  30510. ' comptype: this.$rtti["TFlags$a"]',
  30511. '});',
  30512. 'this.f = {};',
  30513. '']),
  30514. LinesToStr([
  30515. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30516. '']));
  30517. end;
  30518. procedure TTestModule.TestRTTI_StaticArray;
  30519. begin
  30520. WithTypeInfo:=true;
  30521. StartProgram(false);
  30522. Add('type');
  30523. Add(' TFlag = (light,dark);');
  30524. Add(' TFlagNames = array[TFlag] of string;');
  30525. Add(' TBoolNames = array[boolean] of string;');
  30526. Add(' TByteArray = array[1..32768] of byte;');
  30527. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30528. Add('var p: pointer;');
  30529. Add('begin');
  30530. Add(' p:=typeinfo(TFlagNames);');
  30531. Add(' p:=typeinfo(TBoolNames);');
  30532. ConvertProgram;
  30533. CheckSource('TestRTTI_StaticArray',
  30534. LinesToStr([ // statements
  30535. 'this.TFlag = {',
  30536. ' "0": "light",',
  30537. ' light: 0,',
  30538. ' "1": "dark",',
  30539. ' dark: 1',
  30540. '};',
  30541. 'this.$rtti.$Enum("TFlag", {',
  30542. ' minvalue: 0,',
  30543. ' maxvalue: 1,',
  30544. ' ordtype: 1,',
  30545. ' enumtype: this.TFlag',
  30546. '});',
  30547. 'this.$rtti.$StaticArray("TFlagNames", {',
  30548. ' dims: [2],',
  30549. ' eltype: rtl.string',
  30550. '});',
  30551. 'this.$rtti.$StaticArray("TBoolNames", {',
  30552. ' dims: [2],',
  30553. ' eltype: rtl.string',
  30554. '});',
  30555. 'this.$rtti.$StaticArray("TByteArray", {',
  30556. ' dims: [32768],',
  30557. ' eltype: rtl.byte',
  30558. '});',
  30559. 'this.$rtti.$ProcVar("TProc", {',
  30560. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30561. '});',
  30562. 'this.p = null;',
  30563. '']),
  30564. LinesToStr([ // $mod.$main
  30565. '$mod.p = $mod.$rtti["TFlagNames"];',
  30566. '$mod.p = $mod.$rtti["TBoolNames"];',
  30567. '']));
  30568. end;
  30569. procedure TTestModule.TestRTTI_DynArray;
  30570. begin
  30571. WithTypeInfo:=true;
  30572. StartProgram(false);
  30573. Add('type');
  30574. Add(' TArrStr = array of string;');
  30575. Add(' TArr2Dim = array of tarrstr;');
  30576. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30577. Add('var p: pointer;');
  30578. Add('begin');
  30579. Add(' p:=typeinfo(tarrstr);');
  30580. Add(' p:=typeinfo(tarr2dim);');
  30581. ConvertProgram;
  30582. CheckSource('TestRTTI_DynArray',
  30583. LinesToStr([ // statements
  30584. 'this.$rtti.$DynArray("TArrStr", {',
  30585. ' eltype: rtl.string',
  30586. '});',
  30587. 'this.$rtti.$DynArray("TArr2Dim", {',
  30588. ' eltype: this.$rtti["TArrStr"]',
  30589. '});',
  30590. 'this.$rtti.$ProcVar("TProc", {',
  30591. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30592. '});',
  30593. 'this.p = null;',
  30594. '']),
  30595. LinesToStr([ // $mod.$main
  30596. '$mod.p = $mod.$rtti["TArrStr"];',
  30597. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30598. '']));
  30599. end;
  30600. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30601. begin
  30602. WithTypeInfo:=true;
  30603. StartProgram(false);
  30604. Add('type');
  30605. Add(' TArr = array of array of longint;');
  30606. Add('var a: TArr;');
  30607. Add('begin');
  30608. ConvertProgram;
  30609. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30610. LinesToStr([ // statements
  30611. 'this.$rtti.$DynArray("TArr$a", {',
  30612. ' eltype: rtl.longint',
  30613. '});',
  30614. 'this.$rtti.$DynArray("TArr", {',
  30615. ' eltype: this.$rtti["TArr$a"]',
  30616. '});',
  30617. 'this.a = [];',
  30618. '']),
  30619. LinesToStr([ // $mod.$main
  30620. ]));
  30621. end;
  30622. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30623. begin
  30624. WithTypeInfo:=true;
  30625. StartProgram(false);
  30626. Add('type');
  30627. Add(' TObject = class');
  30628. Add(' published');
  30629. Add(' procedure Proc; virtual; abstract;');
  30630. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30631. Add(' end;');
  30632. Add('begin');
  30633. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30634. nDuplicatePublishedMethodXAtY);
  30635. ConvertProgram;
  30636. end;
  30637. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30638. begin
  30639. WithTypeInfo:=true;
  30640. StartUnit(false);
  30641. Add([
  30642. 'interface',
  30643. 'type',
  30644. ' TObject = class',
  30645. ' end;',
  30646. ' {$M+}',
  30647. ' TBird = class',
  30648. ' procedure Fly;',
  30649. ' end;',
  30650. ' {$M-}',
  30651. 'type',
  30652. ' TEagle = class(TBird)',
  30653. ' procedure Fly;',
  30654. ' end;',
  30655. 'implementation',
  30656. 'procedure TBird.Fly;',
  30657. 'begin',
  30658. 'end;',
  30659. 'procedure TEagle.Fly;',
  30660. 'begin',
  30661. 'end;',
  30662. '']);
  30663. ConvertUnit;
  30664. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30665. LinesToStr([ // statements
  30666. 'rtl.createClass(this, "TObject", null, function () {',
  30667. ' this.$init = function () {',
  30668. ' };',
  30669. ' this.$final = function () {',
  30670. ' };',
  30671. '});',
  30672. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30673. ' this.Fly = function () {',
  30674. ' };',
  30675. ' var $r = this.$rtti;',
  30676. ' $r.addMethod("Fly", 0, []);',
  30677. '});',
  30678. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30679. ' this.Fly = function () {',
  30680. ' };',
  30681. ' var $r = this.$rtti;',
  30682. ' $r.addMethod("Fly", 0, []);',
  30683. '});',
  30684. '']),
  30685. LinesToStr([ // $mod.$main
  30686. ]));
  30687. CheckResolverUnexpectedHints(true);
  30688. end;
  30689. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30690. begin
  30691. WithTypeInfo:=true;
  30692. StartProgram(false);
  30693. Add('type');
  30694. Add(' TObject = class');
  30695. Add(' published');
  30696. Add(' procedure Proc; external name ''foo'';');
  30697. Add(' end;');
  30698. Add('begin');
  30699. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30700. nPublishedNameMustMatchExternal);
  30701. ConvertProgram;
  30702. end;
  30703. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30704. begin
  30705. WithTypeInfo:=true;
  30706. StartProgram(false);
  30707. Add('type');
  30708. Add(' TObject = class');
  30709. Add(' class var FA: longint;');
  30710. Add(' published');
  30711. Add(' class property A: longint read FA;');
  30712. Add(' end;');
  30713. Add('begin');
  30714. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30715. nInvalidXModifierY);
  30716. ConvertProgram;
  30717. end;
  30718. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30719. begin
  30720. WithTypeInfo:=true;
  30721. StartProgram(false);
  30722. Add('type');
  30723. Add(' TObject = class');
  30724. Add(' published');
  30725. Add(' class var FA: longint;');
  30726. Add(' end;');
  30727. Add('begin');
  30728. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30729. nSymbolCannotBePublished);
  30730. ConvertProgram;
  30731. end;
  30732. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30733. begin
  30734. WithTypeInfo:=true;
  30735. StartProgram(false);
  30736. Add('{$modeswitch externalclass}');
  30737. Add('type');
  30738. Add(' TObject = class');
  30739. Add(' published');
  30740. Add(' V: longint; external name ''foo'';');
  30741. Add(' end;');
  30742. Add('begin');
  30743. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30744. nPublishedNameMustMatchExternal);
  30745. ConvertProgram;
  30746. end;
  30747. procedure TTestModule.TestRTTI_Class_Field;
  30748. begin
  30749. WithTypeInfo:=true;
  30750. StartProgram(false);
  30751. Add('{$modeswitch externalclass}');
  30752. Add('type');
  30753. Add(' TObject = class');
  30754. Add(' private');
  30755. Add(' FPropA: string;');
  30756. Add(' published');
  30757. Add(' VarLI: longint;');
  30758. Add(' VarC: char;');
  30759. Add(' VarS: string;');
  30760. Add(' VarD: double;');
  30761. Add(' VarB: boolean;');
  30762. Add(' VarLW: longword;');
  30763. Add(' VarSmI: smallint;');
  30764. Add(' VarW: word;');
  30765. Add(' VarShI: shortint;');
  30766. Add(' VarBy: byte;');
  30767. Add(' VarExt: longint external name ''VarExt'';');
  30768. Add(' ArrA, ArrB: array of byte;');
  30769. Add(' end;');
  30770. Add('var p: pointer;');
  30771. Add(' Obj: tobject;');
  30772. Add('begin');
  30773. Add(' p:=typeinfo(tobject);');
  30774. Add(' p:=typeinfo(p);');
  30775. Add(' p:=typeinfo(obj);');
  30776. ConvertProgram;
  30777. CheckSource('TestRTTI_Class_Field',
  30778. LinesToStr([ // statements
  30779. 'rtl.createClass(this, "TObject", null, function () {',
  30780. ' this.$init = function () {',
  30781. ' this.FPropA = "";',
  30782. ' this.VarLI = 0;',
  30783. ' this.VarC = "";',
  30784. ' this.VarS = "";',
  30785. ' this.VarD = 0.0;',
  30786. ' this.VarB = false;',
  30787. ' this.VarLW = 0;',
  30788. ' this.VarSmI = 0;',
  30789. ' this.VarW = 0;',
  30790. ' this.VarShI = 0;',
  30791. ' this.VarBy = 0;',
  30792. ' this.ArrA = [];',
  30793. ' this.ArrB = [];',
  30794. ' };',
  30795. ' this.$final = function () {',
  30796. ' this.ArrA = undefined;',
  30797. ' this.ArrB = undefined;',
  30798. ' };',
  30799. ' var $r = this.$rtti;',
  30800. ' $r.addField("VarLI", rtl.longint);',
  30801. ' $r.addField("VarC", rtl.char);',
  30802. ' $r.addField("VarS", rtl.string);',
  30803. ' $r.addField("VarD", rtl.double);',
  30804. ' $r.addField("VarB", rtl.boolean);',
  30805. ' $r.addField("VarLW", rtl.longword);',
  30806. ' $r.addField("VarSmI", rtl.smallint);',
  30807. ' $r.addField("VarW", rtl.word);',
  30808. ' $r.addField("VarShI", rtl.shortint);',
  30809. ' $r.addField("VarBy", rtl.byte);',
  30810. ' $r.addField("VarExt", rtl.longint);',
  30811. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30812. ' eltype: rtl.byte',
  30813. ' });',
  30814. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30815. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30816. '});',
  30817. 'this.p = null;',
  30818. 'this.Obj = null;',
  30819. '']),
  30820. LinesToStr([ // $mod.$main
  30821. '$mod.p = $mod.$rtti["TObject"];',
  30822. '$mod.p = rtl.pointer;',
  30823. '$mod.p = $mod.Obj.$rtti;',
  30824. '']));
  30825. end;
  30826. procedure TTestModule.TestRTTI_Class_Method;
  30827. begin
  30828. WithTypeInfo:=true;
  30829. StartProgram(false);
  30830. Add([
  30831. 'type',
  30832. ' TObject = class',
  30833. ' private',
  30834. ' procedure Internal; external name ''$intern'';',
  30835. ' published',
  30836. ' procedure Click; virtual; abstract;',
  30837. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30838. ' function GetNotify: boolean; external name ''GetNotify'';',
  30839. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30840. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30841. ' end;',
  30842. 'begin']);
  30843. ConvertProgram;
  30844. CheckSource('TestRTTI_Class_Method',
  30845. LinesToStr([ // statements
  30846. 'rtl.createClass(this, "TObject", null, function () {',
  30847. ' this.$init = function () {',
  30848. ' };',
  30849. ' this.$final = function () {',
  30850. ' };',
  30851. ' var $r = this.$rtti;',
  30852. ' $r.addMethod("Click", 0, []);',
  30853. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30854. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30855. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30856. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30857. '});',
  30858. '']),
  30859. LinesToStr([ // $mod.$main
  30860. '']));
  30861. end;
  30862. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30863. begin
  30864. WithTypeInfo:=true;
  30865. StartProgram(false);
  30866. Add('type');
  30867. Add(' TObject = class');
  30868. Add(' published');
  30869. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30870. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30871. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30872. Add(' end;');
  30873. Add('begin');
  30874. ConvertProgram;
  30875. CheckSource('TestRTTI_Class_MethodOpenArray',
  30876. LinesToStr([ // statements
  30877. 'rtl.createClass(this, "TObject", null, function () {',
  30878. ' this.$init = function () {',
  30879. ' };',
  30880. ' this.$final = function () {',
  30881. ' };',
  30882. ' var $r = this.$rtti;',
  30883. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30884. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30885. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30886. '});',
  30887. '']),
  30888. LinesToStr([ // $mod.$main
  30889. '']));
  30890. end;
  30891. procedure TTestModule.TestRTTI_Class_Property;
  30892. begin
  30893. WithTypeInfo:=true;
  30894. StartProgram(false);
  30895. Add('{$modeswitch externalclass}');
  30896. Add('type');
  30897. Add(' TObject = class');
  30898. Add(' private');
  30899. Add(' FColor: longint;');
  30900. Add(' FColorStored: boolean;');
  30901. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30902. Add(' function GetColor: longint; virtual; abstract;');
  30903. Add(' function GetColorStored: boolean; virtual; abstract;');
  30904. Add(' FExtSize: longint external name ''$extSize'';');
  30905. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30906. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30907. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30908. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30909. Add(' published');
  30910. Add(' property ColorA: longint read FColor;');
  30911. Add(' property ColorB: longint write FColor;');
  30912. Add(' property ColorC: longint read GetColor write SetColor;');
  30913. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  30914. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  30915. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  30916. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  30917. Add(' end;');
  30918. Add('begin');
  30919. ConvertProgram;
  30920. CheckSource('TestRTTI_Class_Property',
  30921. LinesToStr([ // statements
  30922. 'rtl.createClass(this, "TObject", null, function () {',
  30923. ' this.$init = function () {',
  30924. ' this.FColor = 0;',
  30925. ' this.FColorStored = false;',
  30926. ' };',
  30927. ' this.$final = function () {',
  30928. ' };',
  30929. ' var $r = this.$rtti;',
  30930. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  30931. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  30932. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  30933. ' $r.addProperty(',
  30934. ' "ColorD",',
  30935. ' 8,',
  30936. ' rtl.longint,',
  30937. ' "FColor",',
  30938. ' "FColor",',
  30939. ' {',
  30940. ' stored: "FColorStored"',
  30941. ' }',
  30942. ' );',
  30943. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  30944. ' $r.addProperty(',
  30945. ' "ExtSizeB",',
  30946. ' 11,',
  30947. ' rtl.longint,',
  30948. ' "$getSize",',
  30949. ' "$setSize",',
  30950. ' {',
  30951. ' stored: "$extSizeStored"',
  30952. ' }',
  30953. ' );',
  30954. ' $r.addProperty(',
  30955. ' "ExtSizeC",',
  30956. ' 12,',
  30957. ' rtl.longint,',
  30958. ' "$extSize",',
  30959. ' "$extSize",',
  30960. ' {',
  30961. ' stored: "$getExtSizeStored"',
  30962. ' }',
  30963. ' );',
  30964. '});',
  30965. '']),
  30966. LinesToStr([ // $mod.$main
  30967. '']));
  30968. end;
  30969. procedure TTestModule.TestRTTI_Class_PropertyParams;
  30970. begin
  30971. WithTypeInfo:=true;
  30972. StartProgram(false);
  30973. Add('{$modeswitch externalclass}');
  30974. Add('type');
  30975. Add(' integer = longint;');
  30976. Add(' TObject = class');
  30977. Add(' private');
  30978. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  30979. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  30980. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  30981. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  30982. Add(' published');
  30983. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  30984. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  30985. Add(' end;');
  30986. Add('begin');
  30987. ConvertProgram;
  30988. CheckSource('TestRTTI_Class_PropertyParams',
  30989. LinesToStr([ // statements
  30990. 'rtl.createClass(this, "TObject", null, function () {',
  30991. ' this.$init = function () {',
  30992. ' };',
  30993. ' this.$final = function () {',
  30994. ' };',
  30995. ' var $r = this.$rtti;',
  30996. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  30997. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  30998. '});',
  30999. '']),
  31000. LinesToStr([ // $mod.$main
  31001. '']));
  31002. end;
  31003. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  31004. begin
  31005. WithTypeInfo:=true;
  31006. AddModuleWithIntfImplSrc('unit1.pas',
  31007. 'type TColor = -5..5;',
  31008. '');
  31009. StartProgram(true);
  31010. Add([
  31011. 'uses unit1;',
  31012. 'type',
  31013. ' TColorAlias = TColor;',
  31014. ' TColorTypeAlias = type TColor;',
  31015. ' TObject = class',
  31016. ' private',
  31017. ' fColor: TColor;',
  31018. ' fAlias: TColorAlias;',
  31019. ' fTypeAlias: TColorTypeAlias;',
  31020. ' published',
  31021. ' property Color: TColor read fcolor;',
  31022. ' property Alias: TColorAlias read falias;',
  31023. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  31024. ' end;',
  31025. 'begin',
  31026. '']);
  31027. ConvertProgram;
  31028. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  31029. LinesToStr([ // statements
  31030. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  31031. 'rtl.createClass(this, "TObject", null, function () {',
  31032. ' this.$init = function () {',
  31033. ' this.fColor = 0;',
  31034. ' this.fAlias = 0;',
  31035. ' this.fTypeAlias = 0;',
  31036. ' };',
  31037. ' this.$final = function () {',
  31038. ' };',
  31039. ' var $r = this.$rtti;',
  31040. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  31041. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  31042. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  31043. '});',
  31044. '']),
  31045. LinesToStr([ // $mod.$main
  31046. '']));
  31047. end;
  31048. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  31049. begin
  31050. WithTypeInfo:=true;
  31051. StartProgram(false);
  31052. Add([
  31053. '{$modeswitch omitrtti}',
  31054. 'type',
  31055. ' TObject = class',
  31056. ' private',
  31057. ' FA: byte;',
  31058. ' published',
  31059. ' property A: byte read FA write FA;',
  31060. ' end;',
  31061. 'begin']);
  31062. ConvertProgram;
  31063. CheckSource('TestRTTI_Class_OmitRTTI',
  31064. LinesToStr([ // statements
  31065. 'rtl.createClass(this, "TObject", null, function () {',
  31066. ' this.$init = function () {',
  31067. ' this.FA = 0;',
  31068. ' };',
  31069. ' this.$final = function () {',
  31070. ' };',
  31071. '});',
  31072. '']),
  31073. LinesToStr([ // $mod.$main
  31074. '']));
  31075. end;
  31076. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  31077. begin
  31078. WithTypeInfo:=true;
  31079. StartUnit(true,[supTObject]);
  31080. Add([
  31081. 'interface',
  31082. 'type',
  31083. ' {$M+}',
  31084. ' TBird = class',
  31085. ' published',
  31086. ' Swarm: array of TBird;',
  31087. ' end;',
  31088. 'implementation',
  31089. '']);
  31090. ConvertUnit;
  31091. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  31092. LinesToStr([ // statements
  31093. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  31094. ' this.$init = function () {',
  31095. ' pas.system.TObject.$init.call(this);',
  31096. ' this.Swarm = [];',
  31097. ' };',
  31098. ' this.$final = function () {',
  31099. ' this.Swarm = undefined;',
  31100. ' pas.system.TObject.$final.call(this);',
  31101. ' };',
  31102. ' var $r = this.$rtti;',
  31103. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  31104. ' eltype: $r',
  31105. ' });',
  31106. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  31107. '});',
  31108. '']),
  31109. LinesToStr([ // $mod.$main
  31110. '']));
  31111. end;
  31112. procedure TTestModule.TestRTTI_IndexModifier;
  31113. begin
  31114. WithTypeInfo:=true;
  31115. StartProgram(false);
  31116. Add([
  31117. 'type',
  31118. ' TEnum = (red, blue);',
  31119. ' TObject = class',
  31120. ' FB: boolean;',
  31121. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  31122. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  31123. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  31124. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  31125. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  31126. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  31127. ' published',
  31128. ' property B1: boolean index 1 read FB write SetIntBool;',
  31129. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  31130. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  31131. ' end;',
  31132. 'begin']);
  31133. ConvertProgram;
  31134. CheckSource('TestRTTI_IndexModifier',
  31135. LinesToStr([ // statements
  31136. 'this.TEnum = {',
  31137. ' "0": "red",',
  31138. ' red: 0,',
  31139. ' "1": "blue",',
  31140. ' blue: 1',
  31141. '};',
  31142. 'this.$rtti.$Enum("TEnum", {',
  31143. ' minvalue: 0,',
  31144. ' maxvalue: 1,',
  31145. ' ordtype: 1,',
  31146. ' enumtype: this.TEnum',
  31147. '});',
  31148. 'rtl.createClass(this, "TObject", null, function () {',
  31149. ' this.$init = function () {',
  31150. ' this.FB = false;',
  31151. ' };',
  31152. ' this.$final = function () {',
  31153. ' };',
  31154. ' var $r = this.$rtti;',
  31155. ' $r.addProperty(',
  31156. ' "B1",',
  31157. ' 18,',
  31158. ' rtl.boolean,',
  31159. ' "FB",',
  31160. ' "SetIntBool",',
  31161. ' {',
  31162. ' index: 1',
  31163. ' }',
  31164. ' );',
  31165. ' $r.addProperty(',
  31166. ' "B2",',
  31167. ' 17,',
  31168. ' rtl.boolean,',
  31169. ' "GetEnumBool",',
  31170. ' "FB",',
  31171. ' {',
  31172. ' index: $mod.TEnum.blue',
  31173. ' }',
  31174. ' );',
  31175. ' $r.addProperty(',
  31176. ' "I1",',
  31177. ' 19,',
  31178. ' rtl.boolean,',
  31179. ' "GetStrIntBool",',
  31180. ' "SetStrIntBool",',
  31181. ' {',
  31182. ' index: 2',
  31183. ' }',
  31184. ' );',
  31185. '});',
  31186. '']),
  31187. LinesToStr([ // $mod.$main
  31188. '']));
  31189. end;
  31190. procedure TTestModule.TestRTTI_StoredModifier;
  31191. begin
  31192. WithTypeInfo:=true;
  31193. StartProgram(false);
  31194. Add([
  31195. 'const',
  31196. ' ConstB = true;',
  31197. 'type',
  31198. ' TObject = class',
  31199. ' private',
  31200. ' FB: boolean;',
  31201. ' function IsBStored: boolean; virtual; abstract;',
  31202. ' published',
  31203. ' property BoolA: boolean read FB stored true;',
  31204. ' property BoolB: boolean read FB stored false;',
  31205. ' property BoolC: boolean read FB stored FB;',
  31206. ' property BoolD: boolean read FB stored ConstB;',
  31207. ' property BoolE: boolean read FB stored IsBStored;',
  31208. ' end;',
  31209. 'begin']);
  31210. ConvertProgram;
  31211. CheckSource('TestRTTI_StoredModifier',
  31212. LinesToStr([ // statements
  31213. 'this.ConstB = true;',
  31214. 'rtl.createClass(this, "TObject", null, function () {',
  31215. ' this.$init = function () {',
  31216. ' this.FB = false;',
  31217. ' };',
  31218. ' this.$final = function () {',
  31219. ' };',
  31220. ' var $r = this.$rtti;',
  31221. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  31222. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  31223. ' $r.addProperty(',
  31224. ' "BoolC",',
  31225. ' 8,',
  31226. ' rtl.boolean,',
  31227. ' "FB",',
  31228. ' "",',
  31229. ' {',
  31230. ' stored: "FB"',
  31231. ' }',
  31232. ' );',
  31233. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  31234. ' $r.addProperty(',
  31235. ' "BoolE",',
  31236. ' 12,',
  31237. ' rtl.boolean,',
  31238. ' "FB",',
  31239. ' "",',
  31240. ' {',
  31241. ' stored: "IsBStored"',
  31242. ' }',
  31243. ' );',
  31244. '});',
  31245. '']),
  31246. LinesToStr([ // $mod.$main
  31247. '']));
  31248. end;
  31249. procedure TTestModule.TestRTTI_DefaultValue;
  31250. begin
  31251. WithTypeInfo:=true;
  31252. StartProgram(false);
  31253. Add([
  31254. 'type',
  31255. ' TEnum = (red, blue);',
  31256. 'const',
  31257. ' CB = true or false;',
  31258. ' CI = 1+2;',
  31259. 'type',
  31260. ' TObject = class',
  31261. ' FB: boolean;',
  31262. ' FI: longint;',
  31263. ' FE: TEnum;',
  31264. ' published',
  31265. ' property B1: boolean read FB default true;',
  31266. ' property B2: boolean read FB default CB;',
  31267. ' property B3: boolean read FB default test1.cb;',
  31268. ' property I1: longint read FI default 2;',
  31269. ' property I2: longint read FI default CI;',
  31270. ' property E1: TEnum read FE default red;',
  31271. ' property E2: TEnum read FE default TEnum.blue;',
  31272. ' end;',
  31273. 'begin']);
  31274. ConvertProgram;
  31275. CheckSource('TestRTTI_DefaultValue',
  31276. LinesToStr([ // statements
  31277. 'this.TEnum = {',
  31278. ' "0": "red",',
  31279. ' red: 0,',
  31280. ' "1": "blue",',
  31281. ' blue: 1',
  31282. '};',
  31283. 'this.$rtti.$Enum("TEnum", {',
  31284. ' minvalue: 0,',
  31285. ' maxvalue: 1,',
  31286. ' ordtype: 1,',
  31287. ' enumtype: this.TEnum',
  31288. '});',
  31289. 'this.CB = true || false;',
  31290. 'this.CI = 1 + 2;',
  31291. 'rtl.createClass(this, "TObject", null, function () {',
  31292. ' this.$init = function () {',
  31293. ' this.FB = false;',
  31294. ' this.FI = 0;',
  31295. ' this.FE = 0;',
  31296. ' };',
  31297. ' this.$final = function () {',
  31298. ' };',
  31299. ' var $r = this.$rtti;',
  31300. ' $r.addProperty(',
  31301. ' "B1",',
  31302. ' 0,',
  31303. ' rtl.boolean,',
  31304. ' "FB",',
  31305. ' "",',
  31306. ' {',
  31307. ' Default: true',
  31308. ' }',
  31309. ' );',
  31310. ' $r.addProperty(',
  31311. ' "B2",',
  31312. ' 0,',
  31313. ' rtl.boolean,',
  31314. ' "FB",',
  31315. ' "",',
  31316. ' {',
  31317. ' Default: true',
  31318. ' }',
  31319. ' );',
  31320. ' $r.addProperty(',
  31321. ' "B3",',
  31322. ' 0,',
  31323. ' rtl.boolean,',
  31324. ' "FB",',
  31325. ' "",',
  31326. ' {',
  31327. ' Default: true',
  31328. ' }',
  31329. ' );',
  31330. ' $r.addProperty(',
  31331. ' "I1",',
  31332. ' 0,',
  31333. ' rtl.longint,',
  31334. ' "FI",',
  31335. ' "",',
  31336. ' {',
  31337. ' Default: 2',
  31338. ' }',
  31339. ' );',
  31340. ' $r.addProperty(',
  31341. ' "I2",',
  31342. ' 0,',
  31343. ' rtl.longint,',
  31344. ' "FI",',
  31345. ' "",',
  31346. ' {',
  31347. ' Default: 3',
  31348. ' }',
  31349. ' );',
  31350. ' $r.addProperty(',
  31351. ' "E1",',
  31352. ' 0,',
  31353. ' $mod.$rtti["TEnum"],',
  31354. ' "FE",',
  31355. ' "",',
  31356. ' {',
  31357. ' Default: $mod.TEnum.red',
  31358. ' }',
  31359. ' );',
  31360. ' $r.addProperty(',
  31361. ' "E2",',
  31362. ' 0,',
  31363. ' $mod.$rtti["TEnum"],',
  31364. ' "FE",',
  31365. ' "",',
  31366. ' {',
  31367. ' Default: $mod.TEnum.blue',
  31368. ' }',
  31369. ' );',
  31370. '});',
  31371. '']),
  31372. LinesToStr([ // $mod.$main
  31373. '']));
  31374. end;
  31375. procedure TTestModule.TestRTTI_DefaultValueSet;
  31376. begin
  31377. WithTypeInfo:=true;
  31378. StartProgram(false);
  31379. Add([
  31380. 'type',
  31381. ' TEnum = (red, blue);',
  31382. ' TSet = set of TEnum;',
  31383. 'const',
  31384. ' CSet = [red,blue];',
  31385. 'type',
  31386. ' TObject = class',
  31387. ' FSet: TSet;',
  31388. ' published',
  31389. ' property Set1: TSet read FSet default [];',
  31390. ' property Set2: TSet read FSet default [red];',
  31391. ' property Set3: TSet read FSet default [red,blue];',
  31392. ' property Set4: TSet read FSet default CSet;',
  31393. ' end;',
  31394. 'begin']);
  31395. ConvertProgram;
  31396. CheckSource('TestRTTI_DefaultValueSet',
  31397. LinesToStr([ // statements
  31398. 'this.TEnum = {',
  31399. ' "0": "red",',
  31400. ' red: 0,',
  31401. ' "1": "blue",',
  31402. ' blue: 1',
  31403. '};',
  31404. 'this.$rtti.$Enum("TEnum", {',
  31405. ' minvalue: 0,',
  31406. ' maxvalue: 1,',
  31407. ' ordtype: 1,',
  31408. ' enumtype: this.TEnum',
  31409. '});',
  31410. 'this.$rtti.$Set("TSet", {',
  31411. ' comptype: this.$rtti["TEnum"]',
  31412. '});',
  31413. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31414. 'rtl.createClass(this, "TObject", null, function () {',
  31415. ' this.$init = function () {',
  31416. ' this.FSet = {};',
  31417. ' };',
  31418. ' this.$final = function () {',
  31419. ' this.FSet = undefined;',
  31420. ' };',
  31421. ' var $r = this.$rtti;',
  31422. ' $r.addProperty(',
  31423. ' "Set1",',
  31424. ' 0,',
  31425. ' $mod.$rtti["TSet"],',
  31426. ' "FSet",',
  31427. ' "",',
  31428. ' {',
  31429. ' Default: {}',
  31430. ' }',
  31431. ' );',
  31432. ' $r.addProperty(',
  31433. ' "Set2",',
  31434. ' 0,',
  31435. ' $mod.$rtti["TSet"],',
  31436. ' "FSet",',
  31437. ' "",',
  31438. ' {',
  31439. ' Default: rtl.createSet($mod.TEnum.red)',
  31440. ' }',
  31441. ' );',
  31442. ' $r.addProperty(',
  31443. ' "Set3",',
  31444. ' 0,',
  31445. ' $mod.$rtti["TSet"],',
  31446. ' "FSet",',
  31447. ' "",',
  31448. ' {',
  31449. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31450. ' }',
  31451. ' );',
  31452. ' $r.addProperty(',
  31453. ' "Set4",',
  31454. ' 0,',
  31455. ' $mod.$rtti["TSet"],',
  31456. ' "FSet",',
  31457. ' "",',
  31458. ' {',
  31459. ' Default: $mod.CSet',
  31460. ' }',
  31461. ' );',
  31462. '});',
  31463. '']),
  31464. LinesToStr([ // $mod.$main
  31465. '']));
  31466. end;
  31467. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31468. begin
  31469. WithTypeInfo:=true;
  31470. StartProgram(false);
  31471. Add([
  31472. 'type',
  31473. ' TRg = -1..1;',
  31474. 'const',
  31475. ' l = low(TRg);',
  31476. ' h = high(TRg);',
  31477. 'type',
  31478. ' TObject = class',
  31479. ' FV: TRg;',
  31480. ' published',
  31481. ' property V1: TRg read FV default -1;',
  31482. ' end;',
  31483. 'begin']);
  31484. ConvertProgram;
  31485. CheckSource('TestRTTI_DefaultValueRangeType',
  31486. LinesToStr([ // statements
  31487. 'this.$rtti.$Int("TRg", {',
  31488. ' minvalue: -1,',
  31489. ' maxvalue: 1,',
  31490. ' ordtype: 0',
  31491. '});',
  31492. 'this.l = -1;',
  31493. 'this.h = 1;',
  31494. 'rtl.createClass(this, "TObject", null, function () {',
  31495. ' this.$init = function () {',
  31496. ' this.FV = 0;',
  31497. ' };',
  31498. ' this.$final = function () {',
  31499. ' };',
  31500. ' var $r = this.$rtti;',
  31501. ' $r.addProperty(',
  31502. ' "V1",',
  31503. ' 0,',
  31504. ' $mod.$rtti["TRg"],',
  31505. ' "FV",',
  31506. ' "",',
  31507. ' {',
  31508. ' Default: -1',
  31509. ' }',
  31510. ' );',
  31511. '});',
  31512. '']),
  31513. LinesToStr([ // $mod.$main
  31514. '']));
  31515. end;
  31516. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31517. begin
  31518. WithTypeInfo:=true;
  31519. StartProgram(false);
  31520. Add([
  31521. 'type',
  31522. ' TObject = class',
  31523. ' FA, FB: byte;',
  31524. ' property A: byte read FA default 1;',
  31525. ' property B: byte read FB default 2;',
  31526. ' end;',
  31527. ' TBird = class',
  31528. ' published',
  31529. ' property A;',
  31530. ' property B nodefault;',
  31531. ' end;',
  31532. 'begin']);
  31533. ConvertProgram;
  31534. CheckSource('TestRTTI_DefaultValueInherit',
  31535. LinesToStr([ // statements
  31536. 'rtl.createClass(this, "TObject", null, function () {',
  31537. ' this.$init = function () {',
  31538. ' this.FA = 0;',
  31539. ' this.FB = 0;',
  31540. ' };',
  31541. ' this.$final = function () {',
  31542. ' };',
  31543. '});',
  31544. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31545. ' var $r = this.$rtti;',
  31546. ' $r.addProperty(',
  31547. ' "A",',
  31548. ' 0,',
  31549. ' rtl.byte,',
  31550. ' "FA",',
  31551. ' "",',
  31552. ' {',
  31553. ' Default: 1',
  31554. ' }',
  31555. ' );',
  31556. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31557. '});',
  31558. '']),
  31559. LinesToStr([ // $mod.$main
  31560. '']));
  31561. end;
  31562. procedure TTestModule.TestRTTI_OverrideMethod;
  31563. begin
  31564. WithTypeInfo:=true;
  31565. StartProgram(false);
  31566. Add('type');
  31567. Add(' TObject = class');
  31568. Add(' published');
  31569. Add(' procedure DoIt; virtual; abstract;');
  31570. Add(' end;');
  31571. Add(' TSky = class');
  31572. Add(' published');
  31573. Add(' procedure DoIt; override;');
  31574. Add(' end;');
  31575. Add('procedure TSky.DoIt; begin end;');
  31576. Add('begin');
  31577. ConvertProgram;
  31578. CheckSource('TestRTTI_OverrideMethod',
  31579. LinesToStr([ // statements
  31580. 'rtl.createClass(this, "TObject", null, function () {',
  31581. ' this.$init = function () {',
  31582. ' };',
  31583. ' this.$final = function () {',
  31584. ' };',
  31585. ' var $r = this.$rtti;',
  31586. ' $r.addMethod("DoIt", 0, []);',
  31587. '});',
  31588. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31589. ' this.DoIt = function () {',
  31590. ' };',
  31591. '});',
  31592. '']),
  31593. LinesToStr([ // $mod.$main
  31594. '']));
  31595. end;
  31596. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31597. begin
  31598. WithTypeInfo:=true;
  31599. StartProgram(false);
  31600. Add([
  31601. 'type',
  31602. ' TObject = class',
  31603. ' published',
  31604. ' procedure DoIt;',
  31605. ' end;',
  31606. ' TSky = class',
  31607. ' published',
  31608. ' procedure DoIt; reintroduce;',
  31609. ' end;',
  31610. 'procedure TObject.DoIt; begin end;',
  31611. 'procedure TSky.DoIt;',
  31612. 'begin',
  31613. ' inherited DoIt;',
  31614. 'end;',
  31615. 'begin']);
  31616. ConvertProgram;
  31617. CheckSource('TestRTTI_ReintroduceMethod',
  31618. LinesToStr([ // statements
  31619. 'rtl.createClass(this, "TObject", null, function () {',
  31620. ' this.$init = function () {',
  31621. ' };',
  31622. ' this.$final = function () {',
  31623. ' };',
  31624. ' this.DoIt = function () {',
  31625. ' };',
  31626. ' var $r = this.$rtti;',
  31627. ' $r.addMethod("DoIt", 0, []);',
  31628. '});',
  31629. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31630. ' this.DoIt = function () {',
  31631. ' $mod.TObject.DoIt.call(this);',
  31632. ' };',
  31633. ' var $r = this.$rtti;',
  31634. ' $r.addMethod("DoIt", 0, []);',
  31635. '});',
  31636. '']),
  31637. LinesToStr([ // $mod.$main
  31638. '']));
  31639. end;
  31640. procedure TTestModule.TestRTTI_OverloadProperty;
  31641. begin
  31642. WithTypeInfo:=true;
  31643. StartProgram(false);
  31644. Add('type');
  31645. Add(' TObject = class');
  31646. Add(' protected');
  31647. Add(' FFlag: longint;');
  31648. Add(' published');
  31649. Add(' property Flag: longint read fflag;');
  31650. Add(' end;');
  31651. Add(' TSky = class');
  31652. Add(' published');
  31653. Add(' property FLAG: longint write fflag;');
  31654. Add(' end;');
  31655. Add('begin');
  31656. ConvertProgram;
  31657. CheckSource('TestRTTI_OverrideMethod',
  31658. LinesToStr([ // statements
  31659. 'rtl.createClass(this, "TObject", null, function () {',
  31660. ' this.$init = function () {',
  31661. ' this.FFlag = 0;',
  31662. ' };',
  31663. ' this.$final = function () {',
  31664. ' };',
  31665. ' var $r = this.$rtti;',
  31666. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  31667. '});',
  31668. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31669. ' var $r = this.$rtti;',
  31670. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  31671. '});',
  31672. '']),
  31673. LinesToStr([ // $mod.$main
  31674. '']));
  31675. end;
  31676. procedure TTestModule.TestRTTI_ClassForward;
  31677. begin
  31678. WithTypeInfo:=true;
  31679. StartProgram(false);
  31680. Add('type');
  31681. Add(' TObject = class end;');
  31682. Add(' tbridge = class;');
  31683. Add(' TProc = function: tbridge;');
  31684. Add(' TOger = class');
  31685. Add(' published');
  31686. Add(' FBridge: tbridge;');
  31687. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  31688. Add(' property Bridge: tbridge read fbridge write setbridge;');
  31689. Add(' end;');
  31690. Add(' TBridge = class');
  31691. Add(' FOger: toger;');
  31692. Add(' end;');
  31693. Add('var p: Pointer;');
  31694. Add(' b: tbridge;');
  31695. Add('begin');
  31696. Add(' p:=typeinfo(tbridge);');
  31697. Add(' p:=typeinfo(b);');
  31698. ConvertProgram;
  31699. CheckSource('TestRTTI_ClassForward',
  31700. LinesToStr([ // statements
  31701. 'rtl.createClass(this, "TObject", null, function () {',
  31702. ' this.$init = function () {',
  31703. ' };',
  31704. ' this.$final = function () {',
  31705. ' };',
  31706. '});',
  31707. 'this.$rtti.$Class("TBridge");',
  31708. 'this.$rtti.$ProcVar("TProc", {',
  31709. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  31710. '});',
  31711. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  31712. ' this.$init = function () {',
  31713. ' $mod.TObject.$init.call(this);',
  31714. ' this.FBridge = null;',
  31715. ' };',
  31716. ' this.$final = function () {',
  31717. ' this.FBridge = undefined;',
  31718. ' $mod.TObject.$final.call(this);',
  31719. ' };',
  31720. ' var $r = this.$rtti;',
  31721. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  31722. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  31723. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  31724. '});',
  31725. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  31726. ' this.$init = function () {',
  31727. ' $mod.TObject.$init.call(this);',
  31728. ' this.FOger = null;',
  31729. ' };',
  31730. ' this.$final = function () {',
  31731. ' this.FOger = undefined;',
  31732. ' $mod.TObject.$final.call(this);',
  31733. ' };',
  31734. '});',
  31735. 'this.p = null;',
  31736. 'this.b = null;',
  31737. '']),
  31738. LinesToStr([ // $mod.$main
  31739. '$mod.p = $mod.$rtti["TBridge"];',
  31740. '$mod.p = $mod.b.$rtti;',
  31741. '']));
  31742. end;
  31743. procedure TTestModule.TestRTTI_ClassOf;
  31744. begin
  31745. WithTypeInfo:=true;
  31746. StartProgram(false);
  31747. Add('type');
  31748. Add(' TClass = class of tobject;');
  31749. Add(' TProcA = function: TClass;');
  31750. Add(' TObject = class');
  31751. Add(' published');
  31752. Add(' C: tclass;');
  31753. Add(' end;');
  31754. Add(' tfox = class;');
  31755. Add(' TBird = class end;');
  31756. Add(' TBirds = class of tbird;');
  31757. Add(' TFox = class end;');
  31758. Add(' TFoxes = class of tfox;');
  31759. Add(' TCows = class of TCow;');
  31760. Add(' TCow = class;');
  31761. Add(' TCow = class end;');
  31762. Add('begin');
  31763. ConvertProgram;
  31764. CheckSource('TestRTTI_ClassOf',
  31765. LinesToStr([ // statements
  31766. 'this.$rtti.$Class("TObject");',
  31767. 'this.$rtti.$ClassRef("TClass", {',
  31768. ' instancetype: this.$rtti["TObject"]',
  31769. '});',
  31770. 'this.$rtti.$ProcVar("TProcA", {',
  31771. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  31772. '});',
  31773. 'rtl.createClass(this, "TObject", null, function () {',
  31774. ' this.$init = function () {',
  31775. ' this.C = null;',
  31776. ' };',
  31777. ' this.$final = function () {',
  31778. ' this.C = undefined;',
  31779. ' };',
  31780. ' var $r = this.$rtti;',
  31781. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31782. '});',
  31783. 'this.$rtti.$Class("TFox");',
  31784. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31785. '});',
  31786. 'this.$rtti.$ClassRef("TBirds", {',
  31787. ' instancetype: this.$rtti["TBird"]',
  31788. '});',
  31789. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31790. '});',
  31791. 'this.$rtti.$ClassRef("TFoxes", {',
  31792. ' instancetype: this.$rtti["TFox"]',
  31793. '});',
  31794. 'this.$rtti.$Class("TCow");',
  31795. 'this.$rtti.$ClassRef("TCows", {',
  31796. ' instancetype: this.$rtti["TCow"]',
  31797. '});',
  31798. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31799. '});',
  31800. '']),
  31801. LinesToStr([ // $mod.$main
  31802. '']));
  31803. end;
  31804. procedure TTestModule.TestRTTI_Record;
  31805. begin
  31806. WithTypeInfo:=true;
  31807. StartProgram(false);
  31808. Add('type');
  31809. Add(' integer = longint;');
  31810. Add(' TPoint = record');
  31811. Add(' x,y: integer;');
  31812. Add(' end;');
  31813. Add('var p: pointer;');
  31814. Add(' r: tpoint;');
  31815. Add('begin');
  31816. Add(' p:=typeinfo(tpoint);');
  31817. Add(' p:=typeinfo(r);');
  31818. Add(' p:=typeinfo(r.x);');
  31819. ConvertProgram;
  31820. CheckSource('TestRTTI_Record',
  31821. LinesToStr([ // statements
  31822. 'rtl.recNewT(this, "TPoint", function () {',
  31823. ' this.x = 0;',
  31824. ' this.y = 0;',
  31825. ' this.$eq = function (b) {',
  31826. ' return (this.x === b.x) && (this.y === b.y);',
  31827. ' };',
  31828. ' this.$assign = function (s) {',
  31829. ' this.x = s.x;',
  31830. ' this.y = s.y;',
  31831. ' return this;',
  31832. ' };',
  31833. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31834. ' $r.addField("x", rtl.longint);',
  31835. ' $r.addField("y", rtl.longint);',
  31836. '});',
  31837. 'this.p = null;',
  31838. 'this.r = this.TPoint.$new();',
  31839. '']),
  31840. LinesToStr([ // $mod.$main
  31841. '$mod.p = $mod.$rtti["TPoint"];',
  31842. '$mod.p = $mod.$rtti["TPoint"];',
  31843. '$mod.p = rtl.longint;',
  31844. '']));
  31845. end;
  31846. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31847. begin
  31848. WithTypeInfo:=true;
  31849. StartProgram(false);
  31850. Add('type');
  31851. Add(' TFloatRec = record');
  31852. Add(' c,d: array of char;');
  31853. // Add(' i: array of array of longint;');
  31854. Add(' end;');
  31855. Add('var p: pointer;');
  31856. Add(' r: tfloatrec;');
  31857. Add('begin');
  31858. Add(' p:=typeinfo(tfloatrec);');
  31859. Add(' p:=typeinfo(r);');
  31860. Add(' p:=typeinfo(r.d);');
  31861. ConvertProgram;
  31862. CheckSource('TestRTTI_Record',
  31863. LinesToStr([ // statements
  31864. 'rtl.recNewT(this, "TFloatRec", function () {',
  31865. ' this.$new = function () {',
  31866. ' var r = Object.create(this);',
  31867. ' r.c = [];',
  31868. ' r.d = [];',
  31869. ' return r;',
  31870. ' };',
  31871. ' this.$eq = function (b) {',
  31872. ' return (this.c === b.c) && (this.d === b.d);',
  31873. ' };',
  31874. ' this.$assign = function (s) {',
  31875. ' this.c = rtl.arrayRef(s.c);',
  31876. ' this.d = rtl.arrayRef(s.d);',
  31877. ' return this;',
  31878. ' };',
  31879. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31880. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31881. ' eltype: rtl.char',
  31882. ' });',
  31883. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31884. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31885. '});',
  31886. 'this.p = null;',
  31887. 'this.r = this.TFloatRec.$new();',
  31888. '']),
  31889. LinesToStr([ // $mod.$main
  31890. '$mod.p = $mod.$rtti["TFloatRec"];',
  31891. '$mod.p = $mod.$rtti["TFloatRec"];',
  31892. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31893. '']));
  31894. end;
  31895. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31896. begin
  31897. WithTypeInfo:=true;
  31898. StartProgram(false);
  31899. Add([
  31900. '{$modeswitch AdvancedRecords}',
  31901. 'type',
  31902. ' TPoint = record',
  31903. ' type TProc = procedure(w: word);',
  31904. ' class var p: TProc;',
  31905. ' end;',
  31906. 'begin',
  31907. '']);
  31908. ConvertProgram;
  31909. CheckSource('TestRTTI_Record_ClassVarType',
  31910. LinesToStr([ // statements
  31911. 'rtl.recNewT(this, "TPoint", function () {',
  31912. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  31913. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  31914. ' });',
  31915. ' this.p = null;',
  31916. ' this.$eq = function (b) {',
  31917. ' return true;',
  31918. ' };',
  31919. ' this.$assign = function (s) {',
  31920. ' return this;',
  31921. ' };',
  31922. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31923. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  31924. '}, true);',
  31925. '']),
  31926. LinesToStr([ // $mod.$main
  31927. '']));
  31928. end;
  31929. procedure TTestModule.TestRTTI_LocalTypes;
  31930. begin
  31931. WithTypeInfo:=true;
  31932. StartProgram(false);
  31933. Add([
  31934. 'procedure DoIt;',
  31935. 'type',
  31936. ' integer = longint;',
  31937. ' TPoint = record',
  31938. ' x,y: integer;',
  31939. ' end;',
  31940. 'var p: TPoint;',
  31941. 'begin',
  31942. 'end;',
  31943. 'begin']);
  31944. ConvertProgram;
  31945. CheckSource('TestRTTI_LocalTypes',
  31946. LinesToStr([ // statements
  31947. 'var TPoint = rtl.recNewT(null, "", function () {',
  31948. ' this.x = 0;',
  31949. ' this.y = 0;',
  31950. ' this.$eq = function (b) {',
  31951. ' return (this.x === b.x) && (this.y === b.y);',
  31952. ' };',
  31953. ' this.$assign = function (s) {',
  31954. ' this.x = s.x;',
  31955. ' this.y = s.y;',
  31956. ' return this;',
  31957. ' };',
  31958. '});',
  31959. 'this.DoIt = function () {',
  31960. ' var p = TPoint.$new();',
  31961. '};',
  31962. '']),
  31963. LinesToStr([ // $mod.$main
  31964. '']));
  31965. end;
  31966. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  31967. begin
  31968. WithTypeInfo:=true;
  31969. StartProgram(false);
  31970. Add([
  31971. 'type',
  31972. ' TCaption = string;',
  31973. ' TYesNo = boolean;',
  31974. ' TLetter = char;',
  31975. ' TFloat = double;',
  31976. ' TPtr = pointer;',
  31977. ' TShortInt = shortint;',
  31978. ' TByte = byte;',
  31979. ' TSmallInt = smallint;',
  31980. ' TWord = word;',
  31981. ' TInt32 = longint;',
  31982. ' TDWord = longword;',
  31983. ' TValue = jsvalue;',
  31984. 'var p: TPtr;',
  31985. 'begin',
  31986. ' p:=typeinfo(string);',
  31987. ' p:=typeinfo(tcaption);',
  31988. ' p:=typeinfo(boolean);',
  31989. ' p:=typeinfo(tyesno);',
  31990. ' p:=typeinfo(char);',
  31991. ' p:=typeinfo(tletter);',
  31992. ' p:=typeinfo(double);',
  31993. ' p:=typeinfo(tfloat);',
  31994. ' p:=typeinfo(pointer);',
  31995. ' p:=typeinfo(tptr);',
  31996. ' p:=typeinfo(shortint);',
  31997. ' p:=typeinfo(tshortint);',
  31998. ' p:=typeinfo(byte);',
  31999. ' p:=typeinfo(tbyte);',
  32000. ' p:=typeinfo(smallint);',
  32001. ' p:=typeinfo(tsmallint);',
  32002. ' p:=typeinfo(word);',
  32003. ' p:=typeinfo(tword);',
  32004. ' p:=typeinfo(longword);',
  32005. ' p:=typeinfo(tdword);',
  32006. ' p:=typeinfo(jsvalue);',
  32007. ' p:=typeinfo(tvalue);',
  32008. '']);
  32009. ConvertProgram;
  32010. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  32011. LinesToStr([ // statements
  32012. 'this.p = null;',
  32013. '']),
  32014. LinesToStr([ // $mod.$main
  32015. '$mod.p = rtl.string;',
  32016. '$mod.p = rtl.string;',
  32017. '$mod.p = rtl.boolean;',
  32018. '$mod.p = rtl.boolean;',
  32019. '$mod.p = rtl.char;',
  32020. '$mod.p = rtl.char;',
  32021. '$mod.p = rtl.double;',
  32022. '$mod.p = rtl.double;',
  32023. '$mod.p = rtl.pointer;',
  32024. '$mod.p = rtl.pointer;',
  32025. '$mod.p = rtl.shortint;',
  32026. '$mod.p = rtl.shortint;',
  32027. '$mod.p = rtl.byte;',
  32028. '$mod.p = rtl.byte;',
  32029. '$mod.p = rtl.smallint;',
  32030. '$mod.p = rtl.smallint;',
  32031. '$mod.p = rtl.word;',
  32032. '$mod.p = rtl.word;',
  32033. '$mod.p = rtl.longword;',
  32034. '$mod.p = rtl.longword;',
  32035. '$mod.p = rtl.jsvalue;',
  32036. '$mod.p = rtl.jsvalue;',
  32037. '']));
  32038. end;
  32039. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  32040. begin
  32041. WithTypeInfo:=true;
  32042. StartProgram(false);
  32043. Add([
  32044. 'type',
  32045. ' TCaption = type string;',
  32046. ' TYesNo = type boolean;',
  32047. ' TLetter = type char;',
  32048. ' TFloat = type double;',
  32049. ' TPtr = type pointer;',
  32050. ' TShortInt = type shortint;',
  32051. ' TByte = type byte;',
  32052. ' TSmallInt = type smallint;',
  32053. ' TWord = type word;',
  32054. ' TInt32 = type longint;',
  32055. ' TDWord = type longword;',
  32056. ' TValue = type jsvalue;',
  32057. ' TAliasValue = type TValue;',
  32058. 'var',
  32059. ' p: TPtr;',
  32060. ' a: TAliasValue;',
  32061. 'begin',
  32062. ' p:=typeinfo(tcaption);',
  32063. ' p:=typeinfo(tyesno);',
  32064. ' p:=typeinfo(tletter);',
  32065. ' p:=typeinfo(tfloat);',
  32066. ' p:=typeinfo(tptr);',
  32067. ' p:=typeinfo(tshortint);',
  32068. ' p:=typeinfo(tbyte);',
  32069. ' p:=typeinfo(tsmallint);',
  32070. ' p:=typeinfo(tword);',
  32071. ' p:=typeinfo(tdword);',
  32072. ' p:=typeinfo(tvalue);',
  32073. ' p:=typeinfo(taliasvalue);',
  32074. ' p:=typeinfo(a);',
  32075. '']);
  32076. ConvertProgram;
  32077. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  32078. LinesToStr([ // statements
  32079. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  32080. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  32081. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  32082. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  32083. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  32084. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  32085. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  32086. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  32087. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  32088. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  32089. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  32090. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  32091. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  32092. 'this.p = null;',
  32093. 'this.a = undefined;',
  32094. '']),
  32095. LinesToStr([ // $mod.$main
  32096. '$mod.p = $mod.$rtti["TCaption"];',
  32097. '$mod.p = $mod.$rtti["TYesNo"];',
  32098. '$mod.p = $mod.$rtti["TLetter"];',
  32099. '$mod.p = $mod.$rtti["TFloat"];',
  32100. '$mod.p = $mod.$rtti["TPtr"];',
  32101. '$mod.p = $mod.$rtti["TShortInt"];',
  32102. '$mod.p = $mod.$rtti["TByte"];',
  32103. '$mod.p = $mod.$rtti["TSmallInt"];',
  32104. '$mod.p = $mod.$rtti["TWord"];',
  32105. '$mod.p = $mod.$rtti["TDWord"];',
  32106. '$mod.p = $mod.$rtti["TValue"];',
  32107. '$mod.p = $mod.$rtti["TAliasValue"];',
  32108. '$mod.p = $mod.$rtti["TAliasValue"];',
  32109. '']));
  32110. end;
  32111. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  32112. begin
  32113. WithTypeInfo:=true;
  32114. StartProgram(false);
  32115. Add('procedure DoIt;');
  32116. Add('type');
  32117. Add(' integer = longint;');
  32118. Add(' TPoint = record');
  32119. Add(' x,y: integer;');
  32120. Add(' end;');
  32121. Add('var p: pointer;');
  32122. Add('begin');
  32123. Add(' p:=typeinfo(tpoint);');
  32124. Add('end;');
  32125. Add('begin');
  32126. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  32127. ConvertProgram;
  32128. end;
  32129. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  32130. begin
  32131. WithTypeInfo:=true;
  32132. StartProgram(true,[supTypeInfo]);
  32133. Add([
  32134. '{$modeswitch externalclass}',
  32135. 'type',
  32136. ' TFlag = (up,down);',
  32137. ' TFlags = set of TFlag;',
  32138. 'var',
  32139. ' ti: TTypeInfo;',
  32140. ' tiInt: TTypeInfoInteger;',
  32141. ' tiEnum: TTypeInfoEnum;',
  32142. ' tiSet: TTypeInfoSet;',
  32143. 'begin',
  32144. ' ti:=typeinfo(string);',
  32145. ' ti:=typeinfo(boolean);',
  32146. ' ti:=typeinfo(char);',
  32147. ' ti:=typeinfo(double);',
  32148. ' tiInt:=typeinfo(shortint);',
  32149. ' tiInt:=typeinfo(byte);',
  32150. ' tiInt:=typeinfo(smallint);',
  32151. ' tiInt:=typeinfo(word);',
  32152. ' tiInt:=typeinfo(longint);',
  32153. ' tiInt:=typeinfo(longword);',
  32154. ' ti:=typeinfo(jsvalue);',
  32155. ' tiEnum:=typeinfo(tflag);',
  32156. ' tiSet:=typeinfo(tflags);']);
  32157. ConvertProgram;
  32158. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  32159. LinesToStr([ // statements
  32160. 'this.TFlag = {',
  32161. ' "0": "up",',
  32162. ' up: 0,',
  32163. ' "1": "down",',
  32164. ' down: 1',
  32165. '};',
  32166. 'this.$rtti.$Enum("TFlag", {',
  32167. ' minvalue: 0,',
  32168. ' maxvalue: 1,',
  32169. ' ordtype: 1,',
  32170. ' enumtype: this.TFlag',
  32171. '});',
  32172. 'this.$rtti.$Set("TFlags", {',
  32173. ' comptype: this.$rtti["TFlag"]',
  32174. '});',
  32175. 'this.ti = null;',
  32176. 'this.tiInt = null;',
  32177. 'this.tiEnum = null;',
  32178. 'this.tiSet = null;',
  32179. '']),
  32180. LinesToStr([ // $mod.$main
  32181. '$mod.ti = rtl.string;',
  32182. '$mod.ti = rtl.boolean;',
  32183. '$mod.ti = rtl.char;',
  32184. '$mod.ti = rtl.double;',
  32185. '$mod.tiInt = rtl.shortint;',
  32186. '$mod.tiInt = rtl.byte;',
  32187. '$mod.tiInt = rtl.smallint;',
  32188. '$mod.tiInt = rtl.word;',
  32189. '$mod.tiInt = rtl.longint;',
  32190. '$mod.tiInt = rtl.longword;',
  32191. '$mod.ti = rtl.jsvalue;',
  32192. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  32193. '$mod.tiSet = $mod.$rtti["TFlags"];',
  32194. '']));
  32195. end;
  32196. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  32197. begin
  32198. WithTypeInfo:=true;
  32199. StartProgram(true,[supTypeInfo]);
  32200. Add('{$modeswitch externalclass}');
  32201. Add('type');
  32202. Add(' TStaticArr = array[boolean] of string;');
  32203. Add(' TDynArr = array of string;');
  32204. Add(' TProc = procedure;');
  32205. Add(' TMethod = procedure of object;');
  32206. Add('var');
  32207. Add(' StaticArray: TStaticArr;');
  32208. Add(' tiStaticArray: TTypeInfoStaticArray;');
  32209. Add(' DynArray: TDynArr;');
  32210. Add(' tiDynArray: TTypeInfoDynArray;');
  32211. Add(' ProcVar: TProc;');
  32212. Add(' tiProcVar: TTypeInfoProcVar;');
  32213. Add(' MethodVar: TMethod;');
  32214. Add(' tiMethodVar: TTypeInfoMethodVar;');
  32215. Add('begin');
  32216. Add(' tiStaticArray:=typeinfo(StaticArray);');
  32217. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  32218. Add(' tiDynArray:=typeinfo(DynArray);');
  32219. Add(' tiDynArray:=typeinfo(TDynArr);');
  32220. Add(' tiProcVar:=typeinfo(ProcVar);');
  32221. Add(' tiProcVar:=typeinfo(TProc);');
  32222. Add(' tiMethodVar:=typeinfo(MethodVar);');
  32223. Add(' tiMethodVar:=typeinfo(TMethod);');
  32224. ConvertProgram;
  32225. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  32226. LinesToStr([ // statements
  32227. 'this.$rtti.$StaticArray("TStaticArr", {',
  32228. ' dims: [2],',
  32229. ' eltype: rtl.string',
  32230. '});',
  32231. 'this.$rtti.$DynArray("TDynArr", {',
  32232. ' eltype: rtl.string',
  32233. '});',
  32234. 'this.$rtti.$ProcVar("TProc", {',
  32235. ' procsig: rtl.newTIProcSig([])',
  32236. '});',
  32237. 'this.$rtti.$MethodVar("TMethod", {',
  32238. ' procsig: rtl.newTIProcSig([]),',
  32239. ' methodkind: 0',
  32240. '});',
  32241. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  32242. 'this.tiStaticArray = null;',
  32243. 'this.DynArray = [];',
  32244. 'this.tiDynArray = null;',
  32245. 'this.ProcVar = null;',
  32246. 'this.tiProcVar = null;',
  32247. 'this.MethodVar = null;',
  32248. 'this.tiMethodVar = null;',
  32249. '']),
  32250. LinesToStr([ // $mod.$main
  32251. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32252. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32253. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32254. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32255. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32256. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32257. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32258. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32259. '']));
  32260. end;
  32261. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  32262. begin
  32263. WithTypeInfo:=true;
  32264. StartProgram(true,[supTypeInfo]);
  32265. Add('{$modeswitch externalclass}');
  32266. Add('type');
  32267. Add(' TRec = record end;');
  32268. // ToDo: ^TRec
  32269. Add(' TObject = class end;');
  32270. Add(' TClass = class of tobject;');
  32271. Add('var');
  32272. Add(' Rec: trec;');
  32273. Add(' tiRecord: ttypeinforecord;');
  32274. Add(' Obj: tobject;');
  32275. Add(' tiClass: ttypeinfoclass;');
  32276. Add(' aClass: tclass;');
  32277. Add(' tiClassRef: ttypeinfoclassref;');
  32278. // ToDo: ^TRec
  32279. Add(' tiPointer: ttypeinfopointer;');
  32280. Add('begin');
  32281. Add(' tirecord:=typeinfo(trec);');
  32282. Add(' tirecord:=typeinfo(trec);');
  32283. Add(' ticlass:=typeinfo(obj);');
  32284. Add(' ticlass:=typeinfo(tobject);');
  32285. Add(' ticlass:=typeinfo(aclass);');
  32286. Add(' ticlassref:=typeinfo(tclass);');
  32287. ConvertProgram;
  32288. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  32289. LinesToStr([ // statements
  32290. 'rtl.recNewT(this, "TRec", function () {',
  32291. ' this.$eq = function (b) {',
  32292. ' return true;',
  32293. ' };',
  32294. ' this.$assign = function (s) {',
  32295. ' return this;',
  32296. ' };',
  32297. ' $mod.$rtti.$Record("TRec", {});',
  32298. '});',
  32299. 'rtl.createClass(this, "TObject", null, function () {',
  32300. ' this.$init = function () {',
  32301. ' };',
  32302. ' this.$final = function () {',
  32303. ' };',
  32304. '});',
  32305. 'this.$rtti.$ClassRef("TClass", {',
  32306. ' instancetype: this.$rtti["TObject"]',
  32307. '});',
  32308. 'this.Rec = this.TRec.$new();',
  32309. 'this.tiRecord = null;',
  32310. 'this.Obj = null;',
  32311. 'this.tiClass = null;',
  32312. 'this.aClass = null;',
  32313. 'this.tiClassRef = null;',
  32314. 'this.tiPointer = null;',
  32315. '']),
  32316. LinesToStr([ // $mod.$main
  32317. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32318. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32319. '$mod.tiClass = $mod.Obj.$rtti;',
  32320. '$mod.tiClass = $mod.$rtti["TObject"];',
  32321. '$mod.tiClass = $mod.aClass.$rtti;',
  32322. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  32323. '']));
  32324. end;
  32325. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  32326. begin
  32327. WithTypeInfo:=true;
  32328. StartProgram(true,[supTypeInfo]);
  32329. Add([
  32330. '{$modeswitch externalclass}',
  32331. 'type',
  32332. ' TClass = class of tobject;',
  32333. ' TObject = class',
  32334. ' function MyClass: TClass;',
  32335. ' class function ClassType: TClass;',
  32336. ' end;',
  32337. 'function TObject.MyClass: TClass;',
  32338. 'var t: TTypeInfoClass;',
  32339. 'begin',
  32340. ' t:=TypeInfo(Self);',
  32341. ' t:=TypeInfo(Result);',
  32342. ' t:=TypeInfo(TObject);',
  32343. 'end;',
  32344. 'class function TObject.ClassType: TClass;',
  32345. 'var t: TTypeInfoClass;',
  32346. 'begin',
  32347. ' t:=TypeInfo(Self);',
  32348. ' t:=TypeInfo(Result);',
  32349. 'end;',
  32350. 'var',
  32351. ' Obj: TObject;',
  32352. ' t: TTypeInfoClass;',
  32353. 'begin',
  32354. ' t:=TypeInfo(TObject.ClassType);',
  32355. ' t:=TypeInfo(Obj.ClassType);',
  32356. ' t:=TypeInfo(Obj.MyClass);',
  32357. '']);
  32358. ConvertProgram;
  32359. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32360. LinesToStr([ // statements
  32361. 'this.$rtti.$Class("TObject");',
  32362. 'this.$rtti.$ClassRef("TClass", {',
  32363. ' instancetype: this.$rtti["TObject"]',
  32364. '});',
  32365. 'rtl.createClass(this, "TObject", null, function () {',
  32366. ' this.$init = function () {',
  32367. ' };',
  32368. ' this.$final = function () {',
  32369. ' };',
  32370. ' this.MyClass = function () {',
  32371. ' var Result = null;',
  32372. ' var t = null;',
  32373. ' t = this.$rtti;',
  32374. ' t = Result.$rtti;',
  32375. ' t = $mod.$rtti["TObject"];',
  32376. ' return Result;',
  32377. ' };',
  32378. ' this.ClassType = function () {',
  32379. ' var Result = null;',
  32380. ' var t = null;',
  32381. ' t = this.$rtti;',
  32382. ' t = Result.$rtti;',
  32383. ' return Result;',
  32384. ' };',
  32385. '});',
  32386. 'this.Obj = null;',
  32387. 'this.t = null;',
  32388. '']),
  32389. LinesToStr([ // $mod.$main
  32390. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32391. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32392. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32393. '']));
  32394. end;
  32395. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32396. begin
  32397. WithTypeInfo:=true;
  32398. AddModuleWithIntfImplSrc('typinfo.pas',
  32399. LinesToStr([
  32400. '{$modeswitch externalclass}',
  32401. 'type',
  32402. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32403. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32404. '']),
  32405. '');
  32406. AddModuleWithIntfImplSrc('unit2.pas',
  32407. LinesToStr([
  32408. 'uses typinfo;',
  32409. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32410. 'procedure DoPtr(p: PTypeInfo);',
  32411. 'procedure DoInfo(t: TTypeInfo);',
  32412. 'procedure DoInt(t: TTypeInfoInteger);',
  32413. '']),
  32414. LinesToStr([
  32415. 'procedure DoPtr(p: PTypeInfo);',
  32416. 'begin end;',
  32417. 'procedure DoInfo(t: TTypeInfo);',
  32418. 'begin end;',
  32419. 'procedure DoInt(t: TTypeInfoInteger);',
  32420. 'begin end;',
  32421. '']));
  32422. StartUnit(true);
  32423. Add([
  32424. 'interface',
  32425. 'uses unit2;', // does not use unit typinfo
  32426. 'implementation',
  32427. 'var',
  32428. ' i: byte;',
  32429. ' p: pointer;',
  32430. ' t: PTypeInfo;',
  32431. 'initialization',
  32432. ' p:=typeinfo(i);',
  32433. ' t:=typeinfo(i);',
  32434. ' if p=t then ;',
  32435. ' if p=typeinfo(i) then ;',
  32436. ' if typeinfo(i)=p then ;',
  32437. ' if t=typeinfo(i) then ;',
  32438. ' if typeinfo(i)=t then ;',
  32439. ' DoPtr(p);',
  32440. ' DoPtr(t);',
  32441. ' DoPtr(typeinfo(i));',
  32442. ' DoInfo(p);',
  32443. ' DoInfo(t);',
  32444. ' DoInfo(typeinfo(i));',
  32445. ' DoInt(typeinfo(i));',
  32446. '']);
  32447. ConvertUnit;
  32448. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32449. LinesToStr([ // statements
  32450. 'var $impl = $mod.$impl;',
  32451. '']),
  32452. LinesToStr([ // this.$init
  32453. '$impl.p = rtl.byte;',
  32454. '$impl.t = rtl.byte;',
  32455. 'if ($impl.p === $impl.t) ;',
  32456. 'if ($impl.p === rtl.byte) ;',
  32457. 'if (rtl.byte === $impl.p) ;',
  32458. 'if ($impl.t === rtl.byte) ;',
  32459. 'if (rtl.byte === $impl.t) ;',
  32460. 'pas.unit2.DoPtr($impl.p);',
  32461. 'pas.unit2.DoPtr($impl.t);',
  32462. 'pas.unit2.DoPtr(rtl.byte);',
  32463. 'pas.unit2.DoInfo($impl.p);',
  32464. 'pas.unit2.DoInfo($impl.t);',
  32465. 'pas.unit2.DoInfo(rtl.byte);',
  32466. 'pas.unit2.DoInt(rtl.byte);',
  32467. '']),
  32468. LinesToStr([ // implementation
  32469. '$impl.i = 0;',
  32470. '$impl.p = null;',
  32471. '$impl.t = null;',
  32472. '']) );
  32473. end;
  32474. procedure TTestModule.TestRTTI_Interface_Corba;
  32475. begin
  32476. WithTypeInfo:=true;
  32477. StartProgram(true,[supTypeInfo]);
  32478. Add([
  32479. '{$interfaces corba}',
  32480. '{$modeswitch externalclass}',
  32481. 'type',
  32482. ' IUnknown = interface',
  32483. ' end;',
  32484. ' IBird = interface',
  32485. ' function GetItem: longint;',
  32486. ' procedure SetItem(Value: longint);',
  32487. ' property Item: longint read GetItem write SetItem;',
  32488. ' end;',
  32489. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32490. 'var',
  32491. ' i: IBird;',
  32492. ' t: TTypeInfoInterface;',
  32493. 'begin',
  32494. ' t:=TypeInfo(IBird);',
  32495. ' t:=TypeInfo(i);',
  32496. ' DoIt(t);',
  32497. ' DoIt(TypeInfo(IBird));',
  32498. '']);
  32499. ConvertProgram;
  32500. CheckSource('TestRTTI_Interface_Corba',
  32501. LinesToStr([ // statements
  32502. 'rtl.createInterface(',
  32503. ' this,',
  32504. ' "IUnknown",',
  32505. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32506. ' [],',
  32507. ' null,',
  32508. ' function () {',
  32509. ' }',
  32510. ');',
  32511. 'rtl.createInterface(',
  32512. ' this,',
  32513. ' "IBird",',
  32514. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32515. ' ["GetItem", "SetItem"],',
  32516. ' null,',
  32517. ' function () {',
  32518. ' var $r = this.$rtti;',
  32519. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32520. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32521. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32522. ' }',
  32523. ');',
  32524. 'this.DoIt = function (t) {',
  32525. '}; ',
  32526. 'this.i = null;',
  32527. 'this.t = null;',
  32528. '']),
  32529. LinesToStr([ // $mod.$main
  32530. '$mod.t = $mod.$rtti["IBird"];',
  32531. '$mod.t = $mod.i.$rtti;',
  32532. '$mod.DoIt($mod.t);',
  32533. '$mod.DoIt($mod.$rtti["IBird"]);',
  32534. '']));
  32535. end;
  32536. procedure TTestModule.TestRTTI_Interface_COM;
  32537. begin
  32538. WithTypeInfo:=true;
  32539. StartProgram(true,[supTypeInfo]);
  32540. Add([
  32541. '{$interfaces com}',
  32542. '{$modeswitch externalclass}',
  32543. 'type',
  32544. ' TGuid = record end;',
  32545. ' integer = longint;',
  32546. ' IUnknown = interface',
  32547. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32548. ' function _AddRef: Integer;',
  32549. ' function _Release: Integer;',
  32550. ' end;',
  32551. ' IBird = interface',
  32552. ' function GetItem: longint;',
  32553. ' procedure SetItem(Value: longint);',
  32554. ' property Item: longint read GetItem write SetItem;',
  32555. ' end;',
  32556. 'var',
  32557. ' i: IBird;',
  32558. ' t: TTypeInfoInterface;',
  32559. 'begin',
  32560. ' t:=TypeInfo(IBird);',
  32561. ' t:=TypeInfo(i);',
  32562. '']);
  32563. ConvertProgram;
  32564. CheckSource('TestRTTI_Interface_COM',
  32565. LinesToStr([ // statements
  32566. 'rtl.recNewT(this, "TGuid", function () {',
  32567. ' this.$eq = function (b) {',
  32568. ' return true;',
  32569. ' };',
  32570. ' this.$assign = function (s) {',
  32571. ' return this;',
  32572. ' };',
  32573. ' $mod.$rtti.$Record("TGuid", {});',
  32574. '});',
  32575. 'rtl.createInterface(',
  32576. ' this,',
  32577. ' "IUnknown",',
  32578. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32579. ' ["QueryInterface", "_AddRef", "_Release"],',
  32580. ' null,',
  32581. ' function () {',
  32582. ' this.$kind = "com";',
  32583. ' var $r = this.$rtti;',
  32584. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32585. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32586. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32587. ' }',
  32588. ');',
  32589. 'rtl.createInterface(',
  32590. ' this,',
  32591. ' "IBird",',
  32592. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32593. ' ["GetItem", "SetItem"],',
  32594. ' this.IUnknown,',
  32595. ' function () {',
  32596. ' var $r = this.$rtti;',
  32597. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32598. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32599. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32600. ' }',
  32601. ');',
  32602. 'this.i = null;',
  32603. 'this.t = null;',
  32604. '']),
  32605. LinesToStr([ // $mod.$main
  32606. '$mod.t = $mod.$rtti["IBird"];',
  32607. '$mod.t = $mod.i.$rtti;',
  32608. '']));
  32609. end;
  32610. procedure TTestModule.TestRTTI_ClassHelper;
  32611. begin
  32612. WithTypeInfo:=true;
  32613. StartProgram(true,[supTypeInfo]);
  32614. Add([
  32615. '{$interfaces com}',
  32616. '{$modeswitch externalclass}',
  32617. 'type',
  32618. ' TObject = class',
  32619. ' end;',
  32620. ' THelper = class helper for TObject',
  32621. ' published',
  32622. ' function GetItem: longint;',
  32623. ' property Item: longint read GetItem;',
  32624. ' end;',
  32625. 'function THelper.GetItem: longint;',
  32626. 'begin',
  32627. 'end;',
  32628. 'var',
  32629. ' t: TTypeInfoHelper;',
  32630. 'begin',
  32631. ' t:=TypeInfo(THelper);',
  32632. '']);
  32633. ConvertProgram;
  32634. CheckSource('TestRTTI_ClassHelper',
  32635. LinesToStr([ // statements
  32636. 'rtl.createClass(this, "TObject", null, function () {',
  32637. ' this.$init = function () {',
  32638. ' };',
  32639. ' this.$final = function () {',
  32640. ' };',
  32641. '});',
  32642. 'rtl.createHelper(this, "THelper", null, function () {',
  32643. ' this.GetItem = function () {',
  32644. ' var Result = 0;',
  32645. ' return Result;',
  32646. ' };',
  32647. ' var $r = this.$rtti;',
  32648. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32649. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32650. '});',
  32651. 'this.t = null;',
  32652. '']),
  32653. LinesToStr([ // $mod.$main
  32654. '$mod.t = $mod.$rtti["THelper"];',
  32655. '']));
  32656. end;
  32657. procedure TTestModule.TestRTTI_ExternalClass;
  32658. begin
  32659. WithTypeInfo:=true;
  32660. StartProgram(true,[supTypeInfo]);
  32661. Add([
  32662. '{$modeswitch externalclass}',
  32663. 'type',
  32664. ' TJSObject = class external name ''Object''',
  32665. ' end;',
  32666. ' TJSArray = class external name ''Array'' (TJSObject)',
  32667. ' end;',
  32668. 'var',
  32669. ' p: Pointer;',
  32670. ' tc: TTypeInfoExtClass;',
  32671. 'begin',
  32672. ' p:=typeinfo(TJSArray);']);
  32673. ConvertProgram;
  32674. CheckSource('TestRTTI_ExternalClass',
  32675. LinesToStr([ // statements
  32676. 'this.$rtti.$ExtClass("TJSObject", {',
  32677. ' jsclass: "Object"',
  32678. '});',
  32679. 'this.$rtti.$ExtClass("TJSArray", {',
  32680. ' ancestor: this.$rtti["TJSObject"],',
  32681. ' jsclass: "Array"',
  32682. '});',
  32683. 'this.p = null;',
  32684. 'this.tc = null;',
  32685. '']),
  32686. LinesToStr([ // $mod.$main
  32687. '$mod.p = $mod.$rtti["TJSArray"];',
  32688. '']));
  32689. end;
  32690. procedure TTestModule.TestRTTI_Unit;
  32691. begin
  32692. WithTypeInfo:=true;
  32693. AddModuleWithIntfImplSrc('unit2.pas',
  32694. LinesToStr([
  32695. '{$mode delphi}',
  32696. 'type',
  32697. ' TWordArray = array of word;',
  32698. ' TArray<T> = array of T;',
  32699. '']),
  32700. '');
  32701. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  32702. Add([
  32703. '{$mode delphi}',
  32704. 'interface',
  32705. 'uses unit2;',
  32706. 'type',
  32707. ' IBird = interface',
  32708. ' function Swoop: TWordArray;',
  32709. ' function Glide: TArray<word>;',
  32710. ' end;',
  32711. 'procedure Fly;',
  32712. 'implementation',
  32713. 'procedure Fly;',
  32714. 'var',
  32715. ' ta: tTypeInfoDynArray;',
  32716. ' ti: tTypeInfoInterface;',
  32717. 'begin',
  32718. ' ta:=typeinfo(TWordArray);',
  32719. ' ta:=typeinfo(TArray<word>);',
  32720. ' ti:=typeinfo(IBird);',
  32721. 'end;',
  32722. '']);
  32723. ConvertUnit;
  32724. CheckSource('TestRTTI_ExternalClass',
  32725. LinesToStr([ // statements
  32726. 'rtl.createInterface(',
  32727. ' this,',
  32728. ' "IBird",',
  32729. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  32730. ' ["Swoop", "Glide"],',
  32731. ' pas.system.IUnknown,',
  32732. ' function () {',
  32733. ' var $r = this.$rtti;',
  32734. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  32735. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  32736. ' }',
  32737. ');',
  32738. 'this.Fly = function () {',
  32739. ' var ta = null;',
  32740. ' var ti = null;',
  32741. ' ta = pas.unit2.$rtti["TWordArray"];',
  32742. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  32743. ' ti = $mod.$rtti["IBird"];',
  32744. '};',
  32745. '']),
  32746. LinesToStr([ // $mod.$main
  32747. '']));
  32748. end;
  32749. procedure TTestModule.TestResourcestringProgram;
  32750. begin
  32751. AddModuleWithIntfImplSrc('unit2.pas',
  32752. LinesToStr([
  32753. 'resourcestring Title = ''Nice'';',
  32754. '']),
  32755. '');
  32756. StartProgram(true);
  32757. Add([
  32758. 'uses unit2;',
  32759. 'const Bar = ''bar'';',
  32760. 'resourcestring',
  32761. ' Red = ''red'';',
  32762. ' Foobar = ''fOo''+bar;',
  32763. 'var s: string;',
  32764. ' c: char;',
  32765. 'begin',
  32766. ' s:=red;',
  32767. ' s:=test1.red;',
  32768. ' s:=Title;',
  32769. ' c:=red[1];',
  32770. ' c:=test1.red[2];',
  32771. ' if red=foobar then ;',
  32772. ' if red[3]=red[4] then ;']);
  32773. ConvertProgram;
  32774. CheckSource('TestResourcestringProgram',
  32775. LinesToStr([ // statements
  32776. 'this.Bar = "bar";',
  32777. 'this.s = "";',
  32778. 'this.c = "";',
  32779. '$mod.$resourcestrings = {',
  32780. ' Red: {',
  32781. ' org: "red"',
  32782. ' },',
  32783. ' Foobar: {',
  32784. ' org: "fOobar"',
  32785. ' }',
  32786. '};',
  32787. '']),
  32788. LinesToStr([ // $mod.$main
  32789. '$mod.s = rtl.getResStr($mod, "Red");',
  32790. '$mod.s = rtl.getResStr($mod, "Red");',
  32791. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32792. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32793. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32794. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32795. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32796. '']));
  32797. end;
  32798. procedure TTestModule.TestResourcestringUnit;
  32799. begin
  32800. AddModuleWithIntfImplSrc('unit2.pas',
  32801. LinesToStr([
  32802. 'resourcestring Title = ''Nice'';',
  32803. '']),
  32804. '');
  32805. StartUnit(true);
  32806. Add([
  32807. 'interface',
  32808. 'uses unit2;',
  32809. 'const Red = ''rEd'';',
  32810. 'resourcestring',
  32811. ' Blue = ''blue'';',
  32812. ' NotRed = ''not''+Red;',
  32813. 'var s: string;',
  32814. 'implementation',
  32815. 'resourcestring',
  32816. ' ImplGreen = ''green'';',
  32817. 'initialization',
  32818. ' s:=blue+ImplGreen;',
  32819. ' s:=test1.blue+test1.implgreen;',
  32820. ' s:=blue[1]+implgreen[2];',
  32821. ' s:=Title;',
  32822. '']);
  32823. ConvertUnit;
  32824. CheckSource('TestResourcestringUnit',
  32825. LinesToStr([ // statements
  32826. 'this.Red = "rEd";',
  32827. 'this.s = "";',
  32828. '$mod.$resourcestrings = {',
  32829. ' Blue: {',
  32830. ' org: "blue"',
  32831. ' },',
  32832. ' NotRed: {',
  32833. ' org: "notrEd"',
  32834. ' },',
  32835. ' ImplGreen: {',
  32836. ' org: "green"',
  32837. ' }',
  32838. '};',
  32839. '']),
  32840. LinesToStr([ // $mod.$main
  32841. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32842. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32843. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32844. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32845. '']));
  32846. end;
  32847. procedure TTestModule.TestResourcestringImplementation;
  32848. begin
  32849. StartUnit(false);
  32850. Add([
  32851. 'interface',
  32852. 'implementation',
  32853. 'resourcestring',
  32854. ' ImplRed = ''red'';']);
  32855. ConvertUnit;
  32856. CheckSource('TestResourcestringImplementation',
  32857. LinesToStr([ // intf statements
  32858. 'var $impl = $mod.$impl;']),
  32859. LinesToStr([ // $mod.$init
  32860. '']),
  32861. LinesToStr([ // impl statements
  32862. '$mod.$resourcestrings = {',
  32863. ' ImplRed: {',
  32864. ' org: "red"',
  32865. ' }',
  32866. '};',
  32867. '']));
  32868. end;
  32869. procedure TTestModule.TestAttributes_Members;
  32870. begin
  32871. WithTypeInfo:=true;
  32872. StartProgram(false);
  32873. Add([
  32874. '{$modeswitch PrefixedAttributes}',
  32875. 'type',
  32876. ' TObject = class',
  32877. ' constructor Create;',
  32878. ' end;',
  32879. ' TCustomAttribute = class',
  32880. ' constructor Create(Id: word);',
  32881. ' end;',
  32882. ' [Missing]',
  32883. ' TBird = class',
  32884. ' published',
  32885. ' [Tcustom]',
  32886. ' FField: word;',
  32887. ' [tcustom(14)]',
  32888. ' property Size: word read FField;',
  32889. ' [Tcustom(15)]',
  32890. ' procedure Fly; virtual; abstract;',
  32891. ' end;',
  32892. ' TRec = record',
  32893. ' [Tcustom,tcustom(14)]',
  32894. ' Size: word;',
  32895. ' end;',
  32896. 'constructor TObject.Create; begin end;',
  32897. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32898. 'begin',
  32899. '']);
  32900. ConvertProgram;
  32901. CheckSource('TestAttributes_Members',
  32902. LinesToStr([ // statements
  32903. 'rtl.createClass(this, "TObject", null, function () {',
  32904. ' this.$init = function () {',
  32905. ' };',
  32906. ' this.$final = function () {',
  32907. ' };',
  32908. ' this.Create = function () {',
  32909. ' return this;',
  32910. ' };',
  32911. '});',
  32912. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32913. ' this.Create$1 = function (Id) {',
  32914. ' return this;',
  32915. ' };',
  32916. '});',
  32917. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32918. ' this.$init = function () {',
  32919. ' $mod.TObject.$init.call(this);',
  32920. ' this.FField = 0;',
  32921. ' };',
  32922. ' var $r = this.$rtti;',
  32923. ' $r.addField("FField", rtl.word, {',
  32924. ' attr: [$mod.TCustomAttribute, "Create"]',
  32925. ' });',
  32926. ' $r.addProperty(',
  32927. ' "Size",',
  32928. ' 0,',
  32929. ' rtl.word,',
  32930. ' "FField",',
  32931. ' "",',
  32932. ' {',
  32933. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  32934. ' }',
  32935. ' );',
  32936. ' $r.addMethod("Fly", 0, [], null, 0, {',
  32937. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  32938. ' });',
  32939. '});',
  32940. 'rtl.recNewT(this, "TRec", function () {',
  32941. ' this.Size = 0;',
  32942. ' this.$eq = function (b) {',
  32943. ' return this.Size === b.Size;',
  32944. ' };',
  32945. ' this.$assign = function (s) {',
  32946. ' this.Size = s.Size;',
  32947. ' return this;',
  32948. ' };',
  32949. ' var $r = $mod.$rtti.$Record("TRec", {});',
  32950. ' $r.addField("Size", rtl.word, {',
  32951. ' attr: [',
  32952. ' $mod.TCustomAttribute,',
  32953. ' "Create",',
  32954. ' $mod.TCustomAttribute,',
  32955. ' "Create$1",',
  32956. ' [14]',
  32957. ' ]',
  32958. ' });',
  32959. '});',
  32960. '']),
  32961. LinesToStr([ // $mod.$main
  32962. '']));
  32963. end;
  32964. procedure TTestModule.TestAttributes_Types;
  32965. begin
  32966. WithTypeInfo:=true;
  32967. StartProgram(false);
  32968. Add([
  32969. '{$modeswitch PrefixedAttributes}',
  32970. 'type',
  32971. ' TObject = class',
  32972. ' constructor Create(Id: word);',
  32973. ' end;',
  32974. ' TCustomAttribute = class',
  32975. ' end;',
  32976. ' [TCustom(1)]',
  32977. ' TMyClass = class',
  32978. ' end;',
  32979. ' [TCustom(11)]',
  32980. ' TMyDescendant = class(TMyClass)',
  32981. ' end;',
  32982. ' [TCustom(2)]',
  32983. ' TRec = record',
  32984. ' end;',
  32985. ' [TCustom(3)]',
  32986. ' TInt = type word;',
  32987. 'constructor TObject.Create(Id: word);',
  32988. 'begin',
  32989. 'end;',
  32990. 'var p: pointer;',
  32991. 'begin',
  32992. ' p:=typeinfo(TMyClass);',
  32993. ' p:=typeinfo(TRec);',
  32994. ' p:=typeinfo(TInt);',
  32995. '']);
  32996. ConvertProgram;
  32997. CheckSource('TestAttributes_Types',
  32998. LinesToStr([ // statements
  32999. 'rtl.createClass(this, "TObject", null, function () {',
  33000. ' this.$init = function () {',
  33001. ' };',
  33002. ' this.$final = function () {',
  33003. ' };',
  33004. ' this.Create = function (Id) {',
  33005. ' return this;',
  33006. ' };',
  33007. '});',
  33008. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33009. '});',
  33010. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  33011. ' var $r = this.$rtti;',
  33012. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  33013. '});',
  33014. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  33015. ' var $r = this.$rtti;',
  33016. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  33017. '});',
  33018. 'rtl.recNewT(this, "TRec", function () {',
  33019. ' this.$eq = function (b) {',
  33020. ' return true;',
  33021. ' };',
  33022. ' this.$assign = function (s) {',
  33023. ' return this;',
  33024. ' };',
  33025. ' $mod.$rtti.$Record("TRec", {',
  33026. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  33027. ' });',
  33028. '});',
  33029. 'this.$rtti.$inherited("TInt", rtl.word, {',
  33030. ' attr: [this.TCustomAttribute, "Create", [3]]',
  33031. '});',
  33032. 'this.p = null;',
  33033. '']),
  33034. LinesToStr([ // $mod.$main
  33035. '$mod.p = $mod.$rtti["TMyClass"];',
  33036. '$mod.p = $mod.$rtti["TRec"];',
  33037. '$mod.p = $mod.$rtti["TInt"];',
  33038. '']));
  33039. end;
  33040. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  33041. begin
  33042. WithTypeInfo:=true;
  33043. StartProgram(false);
  33044. Add([
  33045. '{$modeswitch PrefixedAttributes}',
  33046. 'type',
  33047. ' TObject = class',
  33048. ' constructor Create;',
  33049. ' end;',
  33050. ' TCustomAttribute = class',
  33051. ' end;',
  33052. ' THelper = class helper for TCustomAttribute',
  33053. ' constructor Create(Id: word);',
  33054. ' end;',
  33055. ' [TCustom(3)]',
  33056. ' TMyInt = word;',
  33057. 'constructor TObject.Create; begin end;',
  33058. 'constructor THelper.Create(Id: word); begin end;',
  33059. 'begin',
  33060. ' if typeinfo(TMyInt)=nil then ;']);
  33061. ConvertProgram;
  33062. end;
  33063. procedure TTestModule.TestAttributes_InterfacesList;
  33064. begin
  33065. WithTypeInfo:=true;
  33066. StartProgram(false);
  33067. Add([
  33068. '{$mode Delphi}',
  33069. 'type',
  33070. ' TObject = class',
  33071. ' constructor Create;',
  33072. ' end;',
  33073. ' IInterface = interface end;',
  33074. ' TCustomAttribute = class',
  33075. ' end;',
  33076. ' Red = class(TCustomAttribute);',
  33077. ' Blue = class(TCustomAttribute);',
  33078. ' [Red]',
  33079. ' IBird<T> = interface',
  33080. ' procedure Fly;',
  33081. ' end;',
  33082. ' [Blue]',
  33083. ' IEagle = interface(IBird<Word>)',
  33084. ' procedure Dive;',
  33085. ' end;',
  33086. ' TAnt = class(TObject, IEagle)',
  33087. ' procedure Fly; virtual; abstract;',
  33088. ' procedure Dive; virtual; abstract;',
  33089. ' end;',
  33090. 'constructor TObject.Create;',
  33091. 'begin',
  33092. 'end;',
  33093. 'begin',
  33094. '']);
  33095. ConvertProgram;
  33096. CheckSource('TestAttributes_InterfacesList',
  33097. LinesToStr([ // statements
  33098. '$mod.$rtti.$Interface("IBird<System.Word>");',
  33099. 'rtl.createClass(this, "TObject", null, function () {',
  33100. ' this.$init = function () {',
  33101. ' };',
  33102. ' this.$final = function () {',
  33103. ' };',
  33104. ' this.Create = function () {',
  33105. ' return this;',
  33106. ' };',
  33107. '});',
  33108. 'rtl.createInterface(',
  33109. ' this,',
  33110. ' "IInterface",',
  33111. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  33112. ' [],',
  33113. ' null,',
  33114. ' function () {',
  33115. ' this.$kind = "com";',
  33116. ' }',
  33117. ');',
  33118. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33119. '});',
  33120. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  33121. '});',
  33122. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  33123. '});',
  33124. 'rtl.createInterface(',
  33125. ' this,',
  33126. ' "IBird$G1",',
  33127. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  33128. ' ["Fly"],',
  33129. ' this.IInterface,',
  33130. ' function () {',
  33131. ' var $r = this.$rtti;',
  33132. ' $r.addMethod("Fly", 0, []);',
  33133. ' $r.attr = [$mod.Red, "Create"];',
  33134. ' },',
  33135. ' "IBird<System.Word>"',
  33136. ');',
  33137. 'rtl.createInterface(',
  33138. ' this,',
  33139. ' "IEagle",',
  33140. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  33141. ' ["Dive"],',
  33142. ' this.IBird$G1,',
  33143. ' function () {',
  33144. ' var $r = this.$rtti;',
  33145. ' $r.addMethod("Dive", 0, []);',
  33146. ' $r.attr = [$mod.Blue, "Create"];',
  33147. ' }',
  33148. ');',
  33149. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  33150. ' rtl.addIntf(this, $mod.IEagle);',
  33151. '});',
  33152. '']),
  33153. LinesToStr([ // $mod.$main
  33154. '']));
  33155. end;
  33156. procedure TTestModule.TestAssert;
  33157. begin
  33158. StartProgram(false);
  33159. Add([
  33160. 'procedure DoIt;',
  33161. 'var',
  33162. ' b: boolean;',
  33163. ' s: string;',
  33164. 'begin',
  33165. ' {$Assertions on}',
  33166. ' Assert(b);',
  33167. 'end;',
  33168. 'begin',
  33169. ' DoIt;',
  33170. '']);
  33171. ConvertProgram;
  33172. CheckSource('TestAssert',
  33173. LinesToStr([ // statements
  33174. 'this.DoIt = function () {',
  33175. ' var b = false;',
  33176. ' var s = "";',
  33177. ' if (!b) throw "assert failed";',
  33178. '};',
  33179. '']),
  33180. LinesToStr([ // $mod.$main
  33181. '$mod.DoIt();',
  33182. '']));
  33183. end;
  33184. procedure TTestModule.TestAssert_SysUtils;
  33185. begin
  33186. AddModuleWithIntfImplSrc('SysUtils.pas',
  33187. LinesToStr([
  33188. 'type',
  33189. ' TObject = class',
  33190. ' constructor Create;',
  33191. ' end;',
  33192. ' EAssertionFailed = class',
  33193. ' constructor Create(s: string);',
  33194. ' end;',
  33195. '']),
  33196. LinesToStr([
  33197. 'constructor TObject.Create;',
  33198. 'begin end;',
  33199. 'constructor EAssertionFailed.Create(s: string);',
  33200. 'begin end;',
  33201. '']) );
  33202. StartProgram(true);
  33203. Add([
  33204. 'uses sysutils;',
  33205. 'procedure DoIt;',
  33206. 'var',
  33207. ' b: boolean;',
  33208. ' s: string;',
  33209. 'begin',
  33210. ' {$Assertions on}',
  33211. ' Assert(b);',
  33212. ' Assert(b,''msg'');',
  33213. 'end;',
  33214. 'begin',
  33215. ' DoIt;',
  33216. '']);
  33217. ConvertProgram;
  33218. CheckSource('TestAssert_SysUtils',
  33219. LinesToStr([ // statements
  33220. 'this.DoIt = function () {',
  33221. ' var b = false;',
  33222. ' var s = "";',
  33223. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  33224. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  33225. '};',
  33226. '']),
  33227. LinesToStr([ // $mod.$main
  33228. '$mod.DoIt();',
  33229. '']));
  33230. end;
  33231. procedure TTestModule.TestObjectChecks;
  33232. begin
  33233. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  33234. StartProgram(false);
  33235. Add([
  33236. 'type',
  33237. ' TObject = class',
  33238. ' procedure DoIt;',
  33239. ' end;',
  33240. ' TClass = class of tobject;',
  33241. ' TBird = class',
  33242. ' end;',
  33243. ' TBirdClass = class of TBird;',
  33244. 'var',
  33245. ' o : TObject;',
  33246. ' c: TClass;',
  33247. ' b: TBird;',
  33248. ' bc: TBirdClass;',
  33249. 'procedure TObject.DoIt;',
  33250. 'begin',
  33251. ' b:=TBird(o);',
  33252. 'end;',
  33253. 'begin',
  33254. ' o.DoIt;',
  33255. ' b:=TBird(o);',
  33256. ' bc:=TBirdClass(c);',
  33257. '']);
  33258. ConvertProgram;
  33259. CheckSource('TestCheckMethodCall',
  33260. LinesToStr([ // statements
  33261. 'rtl.createClass(this, "TObject", null, function () {',
  33262. ' this.$init = function () {',
  33263. ' };',
  33264. ' this.$final = function () {',
  33265. ' };',
  33266. ' this.DoIt = function () {',
  33267. ' rtl.checkMethodCall(this,$mod.TObject);',
  33268. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  33269. ' };',
  33270. '});',
  33271. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33272. '});',
  33273. 'this.o = null;',
  33274. 'this.c = null;',
  33275. 'this.b = null;',
  33276. 'this.bc = null;',
  33277. '']),
  33278. LinesToStr([ // $mod.$main
  33279. '$mod.o.DoIt();',
  33280. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  33281. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  33282. '']));
  33283. end;
  33284. procedure TTestModule.TestOverflowChecks_Int;
  33285. begin
  33286. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  33287. StartProgram(false);
  33288. Add([
  33289. 'procedure DoIt;',
  33290. 'var',
  33291. ' b: byte;',
  33292. ' n: nativeint;',
  33293. ' u: nativeuint;',
  33294. ' c: currency;',
  33295. 'begin',
  33296. ' n:=n+n;',
  33297. ' n:=n-n;',
  33298. ' n:=n+b;',
  33299. ' n:=b-n;',
  33300. ' n:=n*n;',
  33301. ' n:=n*u;',
  33302. ' c:=c+b;',
  33303. ' c:=b+c;',
  33304. ' c:=c*b;',
  33305. ' c:=b*c;',
  33306. 'end;',
  33307. 'begin',
  33308. '']);
  33309. ConvertProgram;
  33310. CheckSource('TestOverflowChecks_Int',
  33311. LinesToStr([ // statements
  33312. 'this.DoIt = function () {',
  33313. ' var b = 0;',
  33314. ' var n = 0;',
  33315. ' var u = 0;',
  33316. ' var c = 0;',
  33317. ' n = rtl.oc(n + n);',
  33318. ' n = rtl.oc(n - n);',
  33319. ' n = rtl.oc(n + b);',
  33320. ' n = rtl.oc(b - n);',
  33321. ' n = rtl.oc(n * n);',
  33322. ' n = rtl.oc(n * u);',
  33323. ' c = rtl.oc(c + (b * 10000));',
  33324. ' c = rtl.oc((b * 10000) + c);',
  33325. ' c = rtl.oc(c * b);',
  33326. ' c = rtl.oc(b * c);',
  33327. '};',
  33328. '']),
  33329. LinesToStr([ // $mod.$main
  33330. '']));
  33331. end;
  33332. procedure TTestModule.TestRangeChecks_AssignInt;
  33333. begin
  33334. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33335. StartProgram(false);
  33336. Add([
  33337. '{$R+}',
  33338. 'var',
  33339. ' b: byte = 2;',
  33340. ' w: word = 3;',
  33341. 'procedure DoIt(p: byte);',
  33342. 'begin',
  33343. ' b:=w;',
  33344. ' b+=w;',
  33345. ' b:=1;',
  33346. 'end;',
  33347. '{$R-}',
  33348. 'procedure DoSome;',
  33349. 'begin',
  33350. ' DoIt(w);',
  33351. ' b:=w;',
  33352. ' b:=2;',
  33353. 'end;',
  33354. 'begin',
  33355. '{$R+}',
  33356. '']);
  33357. ConvertProgram;
  33358. CheckSource('TestRangeChecks_AssignInt',
  33359. LinesToStr([ // statements
  33360. 'this.b = 2;',
  33361. 'this.w = 3;',
  33362. 'this.DoIt = function (p) {',
  33363. ' rtl.rc(p, 0, 255);',
  33364. ' $mod.b = rtl.rc($mod.w,0,255);',
  33365. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33366. ' $mod.b = 1;',
  33367. '};',
  33368. 'this.DoSome = function () {',
  33369. ' $mod.DoIt($mod.w);',
  33370. ' $mod.b = $mod.w;',
  33371. ' $mod.b = 2;',
  33372. '};',
  33373. '']),
  33374. LinesToStr([ // $mod.$main
  33375. '']));
  33376. end;
  33377. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33378. begin
  33379. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33380. StartProgram(false);
  33381. Add([
  33382. '{$R+}',
  33383. 'type Ten = 1..10;',
  33384. 'var',
  33385. ' b: Ten = 2;',
  33386. ' w: Ten = 3;',
  33387. 'procedure DoIt(p: Ten);',
  33388. 'begin',
  33389. ' b:=w;',
  33390. ' b+=w;',
  33391. ' b:=1;',
  33392. 'end;',
  33393. '{$R-}',
  33394. 'procedure DoSome;',
  33395. 'begin',
  33396. ' DoIt(w);',
  33397. ' b:=w;',
  33398. ' b:=2;',
  33399. 'end;',
  33400. 'begin',
  33401. '{$R+}',
  33402. '']);
  33403. ConvertProgram;
  33404. CheckSource('TestRangeChecks_AssignIntRange',
  33405. LinesToStr([ // statements
  33406. 'this.b = 2;',
  33407. 'this.w = 3;',
  33408. 'this.DoIt = function (p) {',
  33409. ' rtl.rc(p, 1, 10);',
  33410. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33411. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33412. ' $mod.b = 1;',
  33413. '};',
  33414. 'this.DoSome = function () {',
  33415. ' $mod.DoIt($mod.w);',
  33416. ' $mod.b = $mod.w;',
  33417. ' $mod.b = 2;',
  33418. '};',
  33419. '']),
  33420. LinesToStr([ // $mod.$main
  33421. '']));
  33422. end;
  33423. procedure TTestModule.TestRangeChecks_AssignEnum;
  33424. begin
  33425. StartProgram(false);
  33426. Add([
  33427. '{$R+}',
  33428. 'type TEnum = (red,green);',
  33429. 'var',
  33430. ' e: TEnum = red;',
  33431. 'procedure DoIt(p: TEnum);',
  33432. 'begin',
  33433. ' e:=p;',
  33434. ' p:=TEnum(0);',
  33435. ' p:=succ(e);',
  33436. 'end;',
  33437. '{$R-}',
  33438. 'procedure DoSome;',
  33439. 'begin',
  33440. ' DoIt(e);',
  33441. ' e:=TEnum(1);',
  33442. ' e:=pred(e);',
  33443. 'end;',
  33444. 'begin',
  33445. '{$R+}',
  33446. '']);
  33447. ConvertProgram;
  33448. CheckSource('TestRangeChecks_AssignEnum',
  33449. LinesToStr([ // statements
  33450. 'this.TEnum = {',
  33451. ' "0": "red",',
  33452. ' red: 0,',
  33453. ' "1": "green",',
  33454. ' green: 1',
  33455. '};',
  33456. 'this.e = this.TEnum.red;',
  33457. 'this.DoIt = function (p) {',
  33458. ' rtl.rc(p, 0, 1);',
  33459. ' $mod.e = rtl.rc(p, 0, 1);',
  33460. ' p = 0;',
  33461. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33462. '};',
  33463. 'this.DoSome = function () {',
  33464. ' $mod.DoIt($mod.e);',
  33465. ' $mod.e = 1;',
  33466. ' $mod.e = $mod.e - 1;',
  33467. '};',
  33468. '']),
  33469. LinesToStr([ // $mod.$main
  33470. '']));
  33471. end;
  33472. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33473. begin
  33474. StartProgram(false);
  33475. Add([
  33476. '{$R+}',
  33477. 'type',
  33478. ' TEnum = (red,green);',
  33479. ' TEnumRg = red..green;',
  33480. 'var',
  33481. ' e: TEnumRg = red;',
  33482. 'procedure DoIt(p: TEnumRg);',
  33483. 'begin',
  33484. ' e:=p;',
  33485. ' p:=TEnumRg(0);',
  33486. ' p:=succ(e);',
  33487. 'end;',
  33488. '{$R-}',
  33489. 'procedure DoSome;',
  33490. 'begin',
  33491. ' DoIt(e);',
  33492. ' e:=TEnum(1);',
  33493. ' e:=pred(e);',
  33494. 'end;',
  33495. 'begin',
  33496. '{$R+}',
  33497. '']);
  33498. ConvertProgram;
  33499. CheckSource('TestRangeChecks_AssignEnumRange',
  33500. LinesToStr([ // statements
  33501. 'this.TEnum = {',
  33502. ' "0": "red",',
  33503. ' red: 0,',
  33504. ' "1": "green",',
  33505. ' green: 1',
  33506. '};',
  33507. 'this.e = this.TEnum.red;',
  33508. 'this.DoIt = function (p) {',
  33509. ' rtl.rc(p, 0, 1);',
  33510. ' $mod.e = rtl.rc(p, 0, 1);',
  33511. ' p = 0;',
  33512. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33513. '};',
  33514. 'this.DoSome = function () {',
  33515. ' $mod.DoIt($mod.e);',
  33516. ' $mod.e = 1;',
  33517. ' $mod.e = $mod.e - 1;',
  33518. '};',
  33519. '']),
  33520. LinesToStr([ // $mod.$main
  33521. '']));
  33522. end;
  33523. procedure TTestModule.TestRangeChecks_AssignChar;
  33524. begin
  33525. StartProgram(false);
  33526. Add([
  33527. '{$R+}',
  33528. 'type',
  33529. ' TLetter = char;',
  33530. 'var',
  33531. ' b: TLetter = ''2'';',
  33532. ' w: TLetter = ''3'';',
  33533. 'procedure DoIt(p: TLetter);',
  33534. 'begin',
  33535. ' b:=w;',
  33536. ' b:=''1'';',
  33537. 'end;',
  33538. '{$R-}',
  33539. 'procedure DoSome;',
  33540. 'begin',
  33541. ' DoIt(w);',
  33542. ' b:=w;',
  33543. ' b:=''2'';',
  33544. 'end;',
  33545. 'begin',
  33546. '{$R+}',
  33547. '']);
  33548. ConvertProgram;
  33549. CheckSource('TestRangeChecks_AssignChar',
  33550. LinesToStr([ // statements
  33551. 'this.b = "2";',
  33552. 'this.w = "3";',
  33553. 'this.DoIt = function (p) {',
  33554. ' rtl.rcc(p, 0, 65535);',
  33555. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33556. ' $mod.b = "1";',
  33557. '};',
  33558. 'this.DoSome = function () {',
  33559. ' $mod.DoIt($mod.w);',
  33560. ' $mod.b = $mod.w;',
  33561. ' $mod.b = "2";',
  33562. '};',
  33563. '']),
  33564. LinesToStr([ // $mod.$main
  33565. '']));
  33566. end;
  33567. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33568. begin
  33569. StartProgram(false);
  33570. Add([
  33571. '{$R+}',
  33572. 'type TDigit = ''0''..''9'';',
  33573. 'var',
  33574. ' b: TDigit = ''2'';',
  33575. ' w: TDigit = ''3'';',
  33576. 'procedure DoIt(p: TDigit);',
  33577. 'begin',
  33578. ' b:=w;',
  33579. ' b:=''1'';',
  33580. 'end;',
  33581. '{$R-}',
  33582. 'procedure DoSome;',
  33583. 'begin',
  33584. ' DoIt(w);',
  33585. ' b:=w;',
  33586. ' b:=''2'';',
  33587. 'end;',
  33588. 'begin',
  33589. '{$R+}',
  33590. '']);
  33591. ConvertProgram;
  33592. CheckSource('TestRangeChecks_AssignCharRange',
  33593. LinesToStr([ // statements
  33594. 'this.b = "2";',
  33595. 'this.w = "3";',
  33596. 'this.DoIt = function (p) {',
  33597. ' rtl.rcc(p, 48, 57);',
  33598. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33599. ' $mod.b = "1";',
  33600. '};',
  33601. 'this.DoSome = function () {',
  33602. ' $mod.DoIt($mod.w);',
  33603. ' $mod.b = $mod.w;',
  33604. ' $mod.b = "2";',
  33605. '};',
  33606. '']),
  33607. LinesToStr([ // $mod.$main
  33608. '']));
  33609. end;
  33610. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33611. begin
  33612. StartProgram(false);
  33613. Add([
  33614. '{$R+}',
  33615. 'type',
  33616. ' Ten = 1..10;',
  33617. ' TArr = array of Ten;',
  33618. ' TArrArr = array of TArr;',
  33619. ' TArrByte = array[byte] of Ten;',
  33620. ' TArrChar = array[''0''..''9''] of Ten;',
  33621. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33622. ' TObject = class',
  33623. ' A: TArr;',
  33624. ' end;',
  33625. 'procedure DoIt;',
  33626. 'var',
  33627. ' Arr: TArr;',
  33628. ' ArrArr: TArrArr;',
  33629. ' ArrByte: TArrByte;',
  33630. ' ArrChar: TArrChar;',
  33631. ' ArrByteChar: TArrByteChar;',
  33632. ' i: Ten;',
  33633. ' c: char;',
  33634. ' o: tobject;',
  33635. 'begin',
  33636. ' i:=Arr[1];',
  33637. ' i:=ArrByteChar[1,''2''];',
  33638. ' Arr[1]:=Arr[1];',
  33639. ' Arr[i]:=Arr[i];',
  33640. ' ArrByte[3]:=ArrByte[3];',
  33641. ' ArrByte[i]:=ArrByte[i];',
  33642. ' ArrChar[''5'']:=ArrChar[''5''];',
  33643. ' ArrChar[c]:=ArrChar[c];',
  33644. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  33645. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  33646. ' o.a[i]:=o.a[i];',
  33647. 'end;',
  33648. 'begin',
  33649. '']);
  33650. ConvertProgram;
  33651. CheckSource('TestRangeChecks_ArrayIndex',
  33652. LinesToStr([ // statements
  33653. 'this.TArrByteChar$clone = function (a) {',
  33654. ' var b = [];',
  33655. ' b.length = 256;',
  33656. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  33657. ' return b;',
  33658. '};',
  33659. 'rtl.createClass(this, "TObject", null, function () {',
  33660. ' this.$init = function () {',
  33661. ' this.A = [];',
  33662. ' };',
  33663. ' this.$final = function () {',
  33664. ' this.A = undefined;',
  33665. ' };',
  33666. '});',
  33667. 'this.DoIt = function () {',
  33668. ' var Arr = [];',
  33669. ' var ArrArr = [];',
  33670. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  33671. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  33672. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  33673. ' var i = 0;',
  33674. ' var c = "";',
  33675. ' var o = null;',
  33676. ' i = rtl.rc(Arr[1], 1, 10);',
  33677. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  33678. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  33679. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  33680. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  33681. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  33682. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  33683. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  33684. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  33685. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  33686. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  33687. '};',
  33688. '']),
  33689. LinesToStr([ // $mod.$main
  33690. '']));
  33691. end;
  33692. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  33693. begin
  33694. StartProgram(false);
  33695. Add([
  33696. '{$R+}',
  33697. 'type',
  33698. ' Ten = 1..10;',
  33699. ' TRec = record x: Ten end;',
  33700. ' TArr = array of TRec;',
  33701. ' TArrArr = array of TArr;',
  33702. ' TObject = class',
  33703. ' A: TArr;',
  33704. ' end;',
  33705. 'procedure DoIt;',
  33706. 'var',
  33707. ' Arr: TArr;',
  33708. ' ArrArr: TArrArr;',
  33709. ' i: Ten;',
  33710. ' o: tobject;',
  33711. 'begin',
  33712. ' Arr[1]:=Arr[1];',
  33713. ' Arr[i]:=Arr[i+1];',
  33714. ' o.a[i]:=o.a[i+2];',
  33715. 'end;',
  33716. 'begin',
  33717. '']);
  33718. ConvertProgram;
  33719. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  33720. LinesToStr([ // statements
  33721. 'rtl.recNewT(this, "TRec", function () {',
  33722. ' this.x = 0;',
  33723. ' this.$eq = function (b) {',
  33724. ' return this.x === b.x;',
  33725. ' };',
  33726. ' this.$assign = function (s) {',
  33727. ' this.x = s.x;',
  33728. ' return this;',
  33729. ' };',
  33730. '});',
  33731. 'rtl.createClass(this, "TObject", null, function () {',
  33732. ' this.$init = function () {',
  33733. ' this.A = [];',
  33734. ' };',
  33735. ' this.$final = function () {',
  33736. ' this.A = undefined;',
  33737. ' };',
  33738. '});',
  33739. 'this.DoIt = function () {',
  33740. ' var Arr = [];',
  33741. ' var ArrArr = [];',
  33742. ' var i = 0;',
  33743. ' var o = null;',
  33744. ' Arr[1].$assign(Arr[1]);',
  33745. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  33746. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  33747. '};',
  33748. '']),
  33749. LinesToStr([ // $mod.$main
  33750. '']));
  33751. end;
  33752. procedure TTestModule.TestRangeChecks_StringIndex;
  33753. begin
  33754. StartProgram(false);
  33755. Add([
  33756. 'type',
  33757. ' TObject = class',
  33758. ' S: string;',
  33759. ' end;',
  33760. '{$R+}',
  33761. 'procedure DoIt(var h: string);',
  33762. 'var',
  33763. ' s: string;',
  33764. ' i: longint;',
  33765. ' c: char;',
  33766. ' o: tobject;',
  33767. 'begin',
  33768. ' c:=s[1];',
  33769. ' s[i]:=s[i];',
  33770. ' h[i]:=h[i];',
  33771. ' c:=o.s[i];',
  33772. ' o.s[i]:=c;',
  33773. 'end;',
  33774. 'begin',
  33775. '']);
  33776. ConvertProgram;
  33777. CheckSource('TestRangeChecks_StringIndex',
  33778. LinesToStr([ // statements
  33779. 'rtl.createClass(this, "TObject", null, function () {',
  33780. ' this.$init = function () {',
  33781. ' this.S = "";',
  33782. ' };',
  33783. ' this.$final = function () {',
  33784. ' };',
  33785. '});',
  33786. 'this.DoIt = function (h) {',
  33787. ' var s = "";',
  33788. ' var i = 0;',
  33789. ' var c = "";',
  33790. ' var o = null;',
  33791. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33792. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33793. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33794. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33795. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33796. '};',
  33797. '']),
  33798. LinesToStr([ // $mod.$main
  33799. '']));
  33800. end;
  33801. procedure TTestModule.TestRangeChecks_TypecastInt;
  33802. begin
  33803. StartProgram(false);
  33804. Add([
  33805. '{$R+}',
  33806. 'var',
  33807. ' i: nativeint;',
  33808. ' b: byte;',
  33809. ' sh: shortint;',
  33810. ' w: word;',
  33811. ' sm: smallint;',
  33812. ' lw: longword;',
  33813. ' li: longint;',
  33814. 'begin',
  33815. ' b:=12+byte(i);',
  33816. ' sh:=12+shortint(i);',
  33817. ' w:=12+word(i);',
  33818. ' sm:=12+smallint(i);',
  33819. ' lw:=12+longword(i);',
  33820. ' li:=12+longint(i);',
  33821. '']);
  33822. ConvertProgram;
  33823. CheckSource('TestRangeChecks_TypecastInt',
  33824. LinesToStr([
  33825. 'this.i = 0;',
  33826. 'this.b = 0;',
  33827. 'this.sh = 0;',
  33828. 'this.w = 0;',
  33829. 'this.sm = 0;',
  33830. 'this.lw = 0;',
  33831. 'this.li = 0;',
  33832. '']),
  33833. LinesToStr([
  33834. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33835. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33836. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33837. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33838. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33839. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33840. '']));
  33841. end;
  33842. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33843. begin
  33844. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33845. StartProgram(false);
  33846. Add([
  33847. '{$modeswitch typehelpers}',
  33848. '{$R+}',
  33849. 'type',
  33850. ' TObject = class',
  33851. ' FSize: byte;',
  33852. ' property Size: byte read FSize;',
  33853. ' end;',
  33854. ' THelper = type helper for byte',
  33855. ' procedure SetIt(w: word);',
  33856. ' end;',
  33857. 'procedure THelper.SetIt(w: word);',
  33858. 'begin',
  33859. ' Self:=w;',
  33860. 'end;',
  33861. 'function GetIt: byte;',
  33862. 'begin',
  33863. ' Result.SetIt(2);',
  33864. 'end;',
  33865. 'var',
  33866. ' b: byte = 3;',
  33867. ' o: TObject;',
  33868. 'begin',
  33869. ' b.SetIt(14);',
  33870. ' with b do SetIt(15);',
  33871. ' o.Size.SetIt(16);',
  33872. '']);
  33873. ConvertProgram;
  33874. CheckSource('TestRangeChecks_AssignInt',
  33875. LinesToStr([ // statements
  33876. 'rtl.createClass(this, "TObject", null, function () {',
  33877. ' this.$init = function () {',
  33878. ' this.FSize = 0;',
  33879. ' };',
  33880. ' this.$final = function () {',
  33881. ' };',
  33882. '});',
  33883. 'rtl.createHelper(this, "THelper", null, function () {',
  33884. ' this.SetIt = function (w) {',
  33885. ' rtl.rc(w, 0, 65535);',
  33886. ' this.set(w);',
  33887. ' };',
  33888. '});',
  33889. 'this.GetIt = function () {',
  33890. ' var Result = 0;',
  33891. ' $mod.THelper.SetIt.call({',
  33892. ' get: function () {',
  33893. ' return Result;',
  33894. ' },',
  33895. ' set: function (v) {',
  33896. ' rtl.rc(v, 0, 255);',
  33897. ' Result = v;',
  33898. ' }',
  33899. ' }, 2);',
  33900. ' return Result;',
  33901. '};',
  33902. 'this.b = 3;',
  33903. 'this.o = null;',
  33904. '']),
  33905. LinesToStr([ // $mod.$main
  33906. '$mod.THelper.SetIt.call({',
  33907. ' p: $mod,',
  33908. ' get: function () {',
  33909. ' return this.p.b;',
  33910. ' },',
  33911. ' set: function (v) {',
  33912. ' rtl.rc(v, 0, 255);',
  33913. ' this.p.b = v;',
  33914. ' }',
  33915. '}, 14);',
  33916. 'var $with = $mod.b;',
  33917. '$mod.THelper.SetIt.call({',
  33918. ' get: function () {',
  33919. ' return $with;',
  33920. ' },',
  33921. ' set: function (v) {',
  33922. ' rtl.rc(v, 0, 255);',
  33923. ' $with = v;',
  33924. ' }',
  33925. '}, 15);',
  33926. '$mod.THelper.SetIt.call({',
  33927. ' p: $mod.o,',
  33928. ' get: function () {',
  33929. ' return this.p.FSize;',
  33930. ' },',
  33931. ' set: function (v) {',
  33932. ' rtl.rc(v, 0, 255);',
  33933. ' this.p.FSize = v;',
  33934. ' }',
  33935. '}, 16);',
  33936. '']));
  33937. end;
  33938. procedure TTestModule.TestAsync_Proc;
  33939. begin
  33940. StartProgram(false);
  33941. Add([
  33942. 'procedure Fly(w: word = 1); async; forward;',
  33943. 'procedure Run(w: word = 2); async;',
  33944. 'begin',
  33945. ' Fly(w);',
  33946. ' Fly;',
  33947. ' await(Fly(w));',
  33948. ' await(Fly);',
  33949. 'end;',
  33950. 'procedure Fly(w: word); ',
  33951. 'begin',
  33952. 'end;',
  33953. 'begin',
  33954. ' Run;',
  33955. ' Run(3);',
  33956. '']);
  33957. CheckResolverUnexpectedHints();
  33958. ConvertProgram;
  33959. CheckSource('TestAsync_Proc',
  33960. LinesToStr([ // statements
  33961. 'this.Run = async function (w) {',
  33962. ' $mod.Fly(w);',
  33963. ' $mod.Fly(1);',
  33964. ' await $mod.Fly(w);',
  33965. ' await $mod.Fly(1);',
  33966. '};',
  33967. 'this.Fly = async function (w) {',
  33968. '};',
  33969. '']),
  33970. LinesToStr([
  33971. '$mod.Run(2);',
  33972. '$mod.Run(3);',
  33973. '']));
  33974. end;
  33975. procedure TTestModule.TestAsync_CallResultIsPromise;
  33976. begin
  33977. StartProgram(false);
  33978. Add([
  33979. '{$modeswitch externalclass}',
  33980. 'type',
  33981. ' TObject = class',
  33982. ' end;',
  33983. ' TJSPromise = class external name ''Promise''',
  33984. ' end;',
  33985. ' TBird = class',
  33986. ' function Fly: word; async; ',
  33987. ' end;',
  33988. 'function TBird.Fly: word; async; ',
  33989. 'begin',
  33990. ' Result:=3;',
  33991. ' Fly:=4+Result;',
  33992. ' if Result=5 then ;',
  33993. ' exit(6);',
  33994. 'end;',
  33995. 'function Run: word; async;',
  33996. 'begin',
  33997. ' Result:=11+Result;',
  33998. ' inc(Result);',
  33999. 'end;',
  34000. 'var',
  34001. ' p: TJSPromise;',
  34002. ' o: TBird;',
  34003. 'begin',
  34004. ' p:=Run;',
  34005. ' p:=Run();',
  34006. ' if Run=p then ;',
  34007. ' if p=Run then ;',
  34008. ' if Run()=p then ;',
  34009. ' if p=Run() then ;',
  34010. ' p:=o.Fly;',
  34011. ' p:=o.Fly();',
  34012. ' if o.Fly=p then ;',
  34013. ' if o.Fly()=p then ;',
  34014. ' with o do begin',
  34015. ' p:=Fly;',
  34016. ' p:=Fly();',
  34017. ' if Fly=p then ;',
  34018. ' if Fly()=p then ;',
  34019. ' end;',
  34020. '']);
  34021. CheckResolverUnexpectedHints();
  34022. ConvertProgram;
  34023. CheckSource('TestAsync_CallResultIsPromise',
  34024. LinesToStr([ // statements
  34025. 'rtl.createClass(this, "TObject", null, function () {',
  34026. ' this.$init = function () {',
  34027. ' };',
  34028. ' this.$final = function () {',
  34029. ' };',
  34030. '});',
  34031. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34032. ' this.Fly = async function () {',
  34033. ' var Result = 0;',
  34034. ' Result = 3;',
  34035. ' Result = 4 + Result;',
  34036. ' if (Result === 5) ;',
  34037. ' return 6;',
  34038. ' return Result;',
  34039. ' };',
  34040. '});',
  34041. 'this.Run = async function () {',
  34042. ' var Result = 0;',
  34043. ' Result = 11 + Result;',
  34044. ' Result += 1;',
  34045. ' return Result;',
  34046. '};',
  34047. 'this.p = null;',
  34048. 'this.o = null;',
  34049. '']),
  34050. LinesToStr([
  34051. '$mod.p = $mod.Run();',
  34052. '$mod.p = $mod.Run();',
  34053. 'if ($mod.Run() === $mod.p) ;',
  34054. 'if ($mod.p === $mod.Run()) ;',
  34055. 'if ($mod.Run() === $mod.p) ;',
  34056. 'if ($mod.p === $mod.Run()) ;',
  34057. '$mod.p = $mod.o.Fly();',
  34058. '$mod.p = $mod.o.Fly();',
  34059. 'if ($mod.o.Fly() === $mod.p) ;',
  34060. 'if ($mod.o.Fly() === $mod.p) ;',
  34061. 'var $with = $mod.o;',
  34062. '$mod.p = $with.Fly();',
  34063. '$mod.p = $with.Fly();',
  34064. 'if ($with.Fly() === $mod.p) ;',
  34065. 'if ($with.Fly() === $mod.p) ;',
  34066. '']));
  34067. end;
  34068. procedure TTestModule.TestAsync_ConstructorFail;
  34069. begin
  34070. StartProgram(false);
  34071. Add([
  34072. 'type',
  34073. ' TObject = class',
  34074. ' end;',
  34075. ' TBird = class',
  34076. ' constructor Create; async;',
  34077. ' end;',
  34078. 'constructor TBird.Create; async;',
  34079. 'begin',
  34080. 'end;',
  34081. 'begin',
  34082. '']);
  34083. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  34084. ConvertProgram;
  34085. end;
  34086. procedure TTestModule.TestAsync_PropertyGetterFail;
  34087. begin
  34088. StartProgram(false);
  34089. Add([
  34090. 'type',
  34091. ' TObject = class',
  34092. ' end;',
  34093. ' TBird = class',
  34094. ' function GetSize: word; async;',
  34095. ' property Size: word read GetSize;',
  34096. ' end;',
  34097. 'function TBird.GetSize: word; async;',
  34098. 'begin',
  34099. 'end;',
  34100. 'begin',
  34101. '']);
  34102. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  34103. ConvertProgram;
  34104. end;
  34105. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  34106. begin
  34107. StartProgram(false);
  34108. Add([
  34109. 'procedure Run; async;',
  34110. 'begin',
  34111. ' await(word,1);',
  34112. 'end;',
  34113. 'begin',
  34114. '']);
  34115. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  34116. ConvertProgram;
  34117. end;
  34118. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  34119. begin
  34120. StartProgram(false);
  34121. Add([
  34122. 'type',
  34123. ' TObject = class',
  34124. ' end;',
  34125. ' TBird = class',
  34126. ' end;',
  34127. 'function Fly: TObject; async;',
  34128. 'begin',
  34129. 'end;',
  34130. 'procedure Run; async;',
  34131. 'begin',
  34132. ' await(TBird,Fly);',
  34133. 'end;',
  34134. 'begin',
  34135. '']);
  34136. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  34137. ConvertProgram;
  34138. end;
  34139. procedure TTestModule.TestAWait_OutsideAsyncFail;
  34140. begin
  34141. StartProgram(false);
  34142. Add([
  34143. 'procedure Crawl(w: double); ',
  34144. 'begin',
  34145. 'end;',
  34146. 'procedure Run(w: double);',
  34147. 'begin',
  34148. ' await(Crawl(w));',
  34149. 'end;',
  34150. 'begin',
  34151. ' Run(1);']);
  34152. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  34153. ConvertProgram;
  34154. end;
  34155. procedure TTestModule.TestAWait_IntegerFail;
  34156. begin
  34157. StartProgram(false);
  34158. Add([
  34159. 'function Run: word;',
  34160. 'begin',
  34161. 'end;',
  34162. 'procedure Fly(w: word); async;',
  34163. 'begin',
  34164. ' await(Run());',
  34165. 'end;',
  34166. 'begin',
  34167. ' Fly(1);']);
  34168. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  34169. ConvertProgram;
  34170. end;
  34171. procedure TTestModule.TestAWait_ExternalClassPromise;
  34172. begin
  34173. StartProgram(false);
  34174. Add([
  34175. '{$modeswitch externalclass}',
  34176. 'type',
  34177. ' TJSPromise = class external name ''Promise''',
  34178. ' end;',
  34179. ' TJSThenable = class external name ''Thenable''',
  34180. ' end;',
  34181. 'function Fly(w: word): TJSPromise;',
  34182. 'begin',
  34183. 'end;',
  34184. 'function Jump(w: word): word; async;',
  34185. 'begin',
  34186. 'end;',
  34187. 'function Eat(w: word): TJSPromise; async;',
  34188. 'begin',
  34189. 'end;',
  34190. 'function Run(d: double): word; async;',
  34191. 'var',
  34192. ' p: TJSPromise;',
  34193. 'begin',
  34194. ' Result:=await(word,p);', // promise needs type
  34195. ' Result:=await(word,Fly(3));', // promise needs type
  34196. ' Result:=await(Jump(4));', // async non promise must omit the type
  34197. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  34198. ' Result:=await(word,Eat(6));', // promise needs type
  34199. 'end;',
  34200. 'begin',
  34201. '']);
  34202. ConvertProgram;
  34203. CheckSource('TestAWait_ExternalClassPromise',
  34204. LinesToStr([ // statements
  34205. 'this.Fly = function (w) {',
  34206. ' var Result = null;',
  34207. ' return Result;',
  34208. '};',
  34209. 'this.Jump = async function (w) {',
  34210. ' var Result = 0;',
  34211. ' return Result;',
  34212. '};',
  34213. 'this.Eat = async function (w) {',
  34214. ' var Result = null;',
  34215. ' return Result;',
  34216. '};',
  34217. 'this.Run = async function (d) {',
  34218. ' var Result = 0;',
  34219. ' var p = null;',
  34220. ' Result = await p;',
  34221. ' Result = await $mod.Fly(3);',
  34222. ' Result = await $mod.Jump(4);',
  34223. ' Result = await $mod.Jump(5);',
  34224. ' Result = await $mod.Eat(6);',
  34225. ' return Result;',
  34226. '};',
  34227. '']),
  34228. LinesToStr([
  34229. ]));
  34230. CheckResolverUnexpectedHints();
  34231. end;
  34232. procedure TTestModule.TestAWait_JSValue;
  34233. begin
  34234. StartProgram(false);
  34235. Add([
  34236. '{$modeswitch externalclass}',
  34237. 'type',
  34238. ' TJSPromise = class external name ''Promise''',
  34239. ' end;',
  34240. 'function Fly(w: word): jsvalue; async;',
  34241. 'begin',
  34242. 'end;',
  34243. 'function Run(d: jsvalue; var e): word; async;',
  34244. 'begin',
  34245. ' Result:=await(word,d);', // promise needs type
  34246. ' d:=await(Fly(4));', // async non promise must omit the type
  34247. ' Result:=await(word,e);', // promise needs type
  34248. 'end;',
  34249. 'begin',
  34250. '']);
  34251. ConvertProgram;
  34252. CheckSource('TestAWait_JSValue',
  34253. LinesToStr([ // statements
  34254. 'this.Fly = async function (w) {',
  34255. ' var Result = undefined;',
  34256. ' return Result;',
  34257. '};',
  34258. 'this.Run = async function (d, e) {',
  34259. ' var Result = 0;',
  34260. ' Result = await d;',
  34261. ' d = await $mod.Fly(4);',
  34262. ' Result = await e.get();',
  34263. ' return Result;',
  34264. '};',
  34265. '']),
  34266. LinesToStr([
  34267. ]));
  34268. CheckResolverUnexpectedHints();
  34269. end;
  34270. procedure TTestModule.TestAWait_Result;
  34271. begin
  34272. StartProgram(false);
  34273. Add([
  34274. '{$modeswitch externalclass}',
  34275. 'type',
  34276. ' TJSPromise = class external name ''Promise''',
  34277. ' end;',
  34278. 'function Crawl(d: double = 1.3): TJSPromise; ',
  34279. 'begin',
  34280. 'end;',
  34281. 'function Run(d: double = 1.6): word; async;',
  34282. 'begin',
  34283. ' Result:=await(word,Crawl);',
  34284. ' Result:=await(word,Crawl(4.5));',
  34285. ' Result:=await(Run);',
  34286. ' Result:=await(Run(6.7));',
  34287. 'end;',
  34288. 'begin',
  34289. ' Run(1);']);
  34290. ConvertProgram;
  34291. CheckSource('TestAWait_Result',
  34292. LinesToStr([ // statements
  34293. 'this.Crawl = function (d) {',
  34294. ' var Result = null;',
  34295. ' return Result;',
  34296. '};',
  34297. 'this.Run = async function (d) {',
  34298. ' var Result = 0;',
  34299. ' Result = await $mod.Crawl(1.3);',
  34300. ' Result = await $mod.Crawl(4.5);',
  34301. ' Result = await $mod.Run(1.6);',
  34302. ' Result = await $mod.Run(6.7);',
  34303. ' return Result;',
  34304. '};',
  34305. '']),
  34306. LinesToStr([
  34307. '$mod.Run(1);'
  34308. ]));
  34309. CheckResolverUnexpectedHints();
  34310. end;
  34311. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  34312. begin
  34313. StartProgram(false);
  34314. Add([
  34315. '{$mode objfpc}',
  34316. '{$modeswitch externalclass}',
  34317. 'type',
  34318. ' TJSPromise = class external name ''Promise''',
  34319. ' end;',
  34320. 'function Run: TJSPromise; async;',
  34321. 'begin',
  34322. 'end;',
  34323. 'procedure Fly(w: word); async;',
  34324. 'begin',
  34325. ' await(Run());',
  34326. 'end;',
  34327. 'begin',
  34328. ' Fly(1);']);
  34329. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  34330. nWrongNumberOfParametersForCallTo);
  34331. ConvertProgram;
  34332. end;
  34333. procedure TTestModule.TestAsync_AnonymousProc;
  34334. begin
  34335. StartProgram(false);
  34336. Add([
  34337. '{$mode objfpc}',
  34338. '{$modeswitch externalclass}',
  34339. 'type',
  34340. ' TJSPromise = class external name ''Promise''',
  34341. ' end;',
  34342. 'type',
  34343. ' TFunc = reference to function(x: double): word; async;',
  34344. 'function Crawl(d: double = 1.3): word; async;',
  34345. 'begin',
  34346. 'end;',
  34347. 'var Func: TFunc;',
  34348. 'begin',
  34349. ' Func:=function(c:double):word async begin',
  34350. ' Result:=await(Crawl(c));',
  34351. ' end;',
  34352. ' Func:=function(c:double):word async assembler asm',
  34353. ' end;',
  34354. '']);
  34355. ConvertProgram;
  34356. CheckSource('TestAsync_AnonymousProc',
  34357. LinesToStr([ // statements
  34358. 'this.Crawl = async function (d) {',
  34359. ' var Result = 0;',
  34360. ' return Result;',
  34361. '};',
  34362. 'this.Func = null;',
  34363. '']),
  34364. LinesToStr([
  34365. '$mod.Func = async function (c) {',
  34366. ' var Result = 0;',
  34367. ' Result = await $mod.Crawl(c);',
  34368. ' return Result;',
  34369. '};',
  34370. '$mod.Func = async function (c) {',
  34371. '};',
  34372. '']));
  34373. CheckResolverUnexpectedHints();
  34374. end;
  34375. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34376. begin
  34377. StartProgram(false);
  34378. Add([
  34379. '{$mode objfpc}',
  34380. '{$modeswitch externalclass}',
  34381. 'type',
  34382. ' TJSPromise = class external name ''Promise''',
  34383. ' end;',
  34384. ' TObject = class',
  34385. ' public',
  34386. ' procedure Fly(Prom: TJSPromise);',
  34387. ' end;',
  34388. ' TFunc = reference to procedure(Bird: TObject);',
  34389. 'procedure TObject.Fly(Prom: TJSPromise);',
  34390. 'begin',
  34391. 'end;',
  34392. 'function Crawl: jsvalue; async;',
  34393. 'begin',
  34394. 'end;',
  34395. 'procedure Add(Func: TFunc);',
  34396. 'begin',
  34397. 'end;',
  34398. 'begin',
  34399. ' Add(procedure(Bird: TObject)',
  34400. ' begin',
  34401. ' Bird.Fly(Crawl());',
  34402. ' end);',
  34403. '']);
  34404. ConvertProgram;
  34405. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34406. LinesToStr([ // statements
  34407. 'rtl.createClass(this, "TObject", null, function () {',
  34408. ' this.$init = function () {',
  34409. ' };',
  34410. ' this.$final = function () {',
  34411. ' };',
  34412. ' this.Fly = function (Prom) {',
  34413. ' };',
  34414. '});',
  34415. 'this.Crawl = async function () {',
  34416. ' var Result = undefined;',
  34417. ' return Result;',
  34418. '};',
  34419. 'this.Add = function (Func) {',
  34420. '};',
  34421. '']),
  34422. LinesToStr([
  34423. '$mod.Add(function (Bird) {',
  34424. ' Bird.Fly($mod.Crawl());',
  34425. '});',
  34426. '']));
  34427. end;
  34428. procedure TTestModule.TestAsync_ProcType;
  34429. begin
  34430. StartProgram(false);
  34431. Add([
  34432. '{$mode objfpc}',
  34433. 'type',
  34434. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34435. ' TFunc = function(x: double = 1.1): word; async;',
  34436. ' TProc = procedure(x: longint = 7); async;',
  34437. 'function Crawl(d: double): word; async;',
  34438. 'begin',
  34439. 'end;',
  34440. 'procedure Run(e:longint); async;',
  34441. 'begin',
  34442. 'end;',
  34443. 'procedure Fly(p: TProc); async;',
  34444. 'begin',
  34445. ' await(p);',
  34446. ' await(p());',
  34447. 'end;',
  34448. 'var',
  34449. ' RefFunc: TRefFunc;',
  34450. ' Func: TFunc;',
  34451. ' Proc, ProcB: TProc;',
  34452. 'begin',
  34453. ' Func:=@Crawl;',
  34454. ' RefFunc:=@Crawl;',
  34455. ' RefFunc:=function(c:double):word async begin',
  34456. ' Result:=await(RefFunc);',
  34457. ' Result:=await(RefFunc());',
  34458. ' Result:=await(Func);',
  34459. ' Result:=await(Func());',
  34460. ' await(Proc);',
  34461. ' await(Proc());',
  34462. ' await(Proc(13));',
  34463. ' end;',
  34464. ' Proc:=@Run;',
  34465. ' if Proc=ProcB then ;',
  34466. ' ']);
  34467. ConvertProgram;
  34468. CheckResolverUnexpectedHints();
  34469. CheckSource('TestAsync_ProcType',
  34470. LinesToStr([ // statements
  34471. 'this.Crawl = async function (d) {',
  34472. ' var Result = 0;',
  34473. ' return Result;',
  34474. '};',
  34475. 'this.Run = async function (e) {',
  34476. '};',
  34477. 'this.Fly = async function (p) {',
  34478. ' await p(7);',
  34479. ' await p(7);',
  34480. '};',
  34481. 'this.RefFunc = null;',
  34482. 'this.Func = null;',
  34483. 'this.Proc = null;',
  34484. 'this.ProcB = null;',
  34485. '']),
  34486. LinesToStr([
  34487. '$mod.Func = $mod.Crawl;',
  34488. '$mod.RefFunc = $mod.Crawl;',
  34489. '$mod.RefFunc = async function (c) {',
  34490. ' var Result = 0;',
  34491. ' Result = await $mod.RefFunc(1.3);',
  34492. ' Result = await $mod.RefFunc(1.3);',
  34493. ' Result = await $mod.Func(1.1);',
  34494. ' Result = await $mod.Func(1.1);',
  34495. ' await $mod.Proc(7);',
  34496. ' await $mod.Proc(7);',
  34497. ' await $mod.Proc(13);',
  34498. ' return Result;',
  34499. '};',
  34500. '$mod.Proc = $mod.Run;',
  34501. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34502. '']));
  34503. end;
  34504. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34505. begin
  34506. StartProgram(false);
  34507. Add([
  34508. '{$mode objfpc}',
  34509. 'type',
  34510. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34511. 'function Crawl(d: double): word; async;',
  34512. 'begin',
  34513. 'end;',
  34514. 'var',
  34515. ' RefFunc: TRefFunc;',
  34516. 'begin',
  34517. ' RefFunc:=@Crawl;',
  34518. ' ']);
  34519. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34520. ConvertProgram;
  34521. end;
  34522. procedure TTestModule.TestAsync_Inherited;
  34523. begin
  34524. StartProgram(false);
  34525. Add([
  34526. '{$mode objfpc}',
  34527. '{$modeswitch externalclass}',
  34528. 'type',
  34529. ' TJSPromise = class external name ''Promise''',
  34530. ' end;',
  34531. ' TObject = class',
  34532. ' function Run(w: word = 3): word; async; virtual;',
  34533. ' end;',
  34534. ' TBird = class',
  34535. ' function Run(w: word = 3): word; async; override;',
  34536. ' end;',
  34537. 'function TObject.Run(w: word = 3): word; async;',
  34538. 'begin',
  34539. 'end;',
  34540. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34541. 'var p: TJSPromise;',
  34542. 'begin',
  34543. ' p:=inherited;',
  34544. ' p:=inherited Run;',
  34545. ' p:=inherited Run();',
  34546. ' p:=inherited Run(4);',
  34547. ' exit(p);',
  34548. ' exit(inherited);',
  34549. ' exit(inherited Run);',
  34550. ' exit(inherited Run(5));',
  34551. ' exit(6);',
  34552. 'end;',
  34553. 'begin',
  34554. ' ']);
  34555. ConvertProgram;
  34556. CheckSource('TestAsync_Inherited',
  34557. LinesToStr([ // statements
  34558. 'rtl.createClass(this, "TObject", null, function () {',
  34559. ' this.$init = function () {',
  34560. ' };',
  34561. ' this.$final = function () {',
  34562. ' };',
  34563. ' this.Run = async function (w) {',
  34564. ' var Result = 0;',
  34565. ' return Result;',
  34566. ' };',
  34567. '});',
  34568. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34569. ' this.Run = async function (w) {',
  34570. ' var Result = 0;',
  34571. ' var p = null;',
  34572. ' p = $mod.TObject.Run.apply(this, arguments);',
  34573. ' p = $mod.TObject.Run.call(this, 3);',
  34574. ' p = $mod.TObject.Run.call(this, 3);',
  34575. ' p = $mod.TObject.Run.call(this, 4);',
  34576. ' return p;',
  34577. ' return $mod.TObject.Run.apply(this, arguments);',
  34578. ' return $mod.TObject.Run.call(this, 3);',
  34579. ' return $mod.TObject.Run.call(this, 5);',
  34580. ' return 6;',
  34581. ' return Result;',
  34582. ' };',
  34583. '});',
  34584. '']),
  34585. LinesToStr([
  34586. '']));
  34587. CheckResolverUnexpectedHints();
  34588. end;
  34589. procedure TTestModule.TestAsync_ClassInterface;
  34590. begin
  34591. StartProgram(false);
  34592. Add([
  34593. '{$mode objfpc}',
  34594. '{$modeswitch externalclass}',
  34595. 'type',
  34596. ' TJSPromise = class external name ''Promise''',
  34597. ' end;',
  34598. ' IUnknown = interface',
  34599. ' function _AddRef: longint;',
  34600. ' function _Release: longint;',
  34601. ' end;',
  34602. 'function Say(i: IUnknown): IUnknown; async;',
  34603. 'begin',
  34604. 'end;',
  34605. 'function Run: IUnknown; async;',
  34606. 'begin',
  34607. ' Result:=await(Run);',
  34608. ' Result:=await(Run());',
  34609. ' Result:=await(Run) as IUnknown;',
  34610. ' Result:=await(Say(nil));',
  34611. ' Result:=await(Say(await(Run())));',
  34612. ' Result:=await(Say(await(Run()) as IUnknown));',
  34613. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  34614. 'end;',
  34615. 'procedure Fly;',
  34616. 'var p: TJSPromise;',
  34617. 'begin',
  34618. ' Run;',
  34619. ' Run();',
  34620. ' p:=Run;',
  34621. ' p:=Run();',
  34622. 'end;',
  34623. 'begin',
  34624. ' ']);
  34625. ConvertProgram;
  34626. CheckSource('TestAsync_ClassInterface',
  34627. LinesToStr([ // statements
  34628. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  34629. 'this.Say = async function (i) {',
  34630. ' var Result = null;',
  34631. ' return Result;',
  34632. '};',
  34633. 'this.Run = async function () {',
  34634. ' var Result = null;',
  34635. ' var $ok = false;',
  34636. ' try {',
  34637. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34638. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34639. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  34640. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  34641. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  34642. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  34643. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  34644. ' $ok = true;',
  34645. ' } finally {',
  34646. ' if (!$ok) rtl._Release(Result);',
  34647. ' };',
  34648. ' return Result;',
  34649. '};',
  34650. 'this.Fly = function () {',
  34651. ' var p = null;',
  34652. ' $mod.Run();',
  34653. ' $mod.Run();',
  34654. ' p = $mod.Run();',
  34655. ' p = $mod.Run();',
  34656. '};',
  34657. '']),
  34658. LinesToStr([
  34659. '']));
  34660. CheckResolverUnexpectedHints();
  34661. end;
  34662. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  34663. begin
  34664. StartProgram(true,[supTInterfacedObject]);
  34665. Add([
  34666. '{$mode objfpc}',
  34667. '{$modeswitch externalclass}',
  34668. 'type',
  34669. ' TJSPromise = class external name ''Promise''',
  34670. ' end;',
  34671. ' IBird = interface',
  34672. ' procedure Run;',
  34673. ' end;',
  34674. ' TBird = class(TInterfacedObject,IBird)',
  34675. ' procedure Run; async;',
  34676. ' end;',
  34677. 'procedure TBird.Run;',
  34678. 'begin',
  34679. 'end;',
  34680. 'begin',
  34681. ' ']);
  34682. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34683. ConvertProgram;
  34684. end;
  34685. procedure TTestModule.TestAWait_ClassAs;
  34686. begin
  34687. StartProgram(false);
  34688. Add([
  34689. '{$mode objfpc}',
  34690. '{$modeswitch externalclass}',
  34691. 'type',
  34692. ' TJSPromise = class external name ''Promise''',
  34693. ' end;',
  34694. ' TObject = class',
  34695. ' function Run: TObject; async;',
  34696. ' end;',
  34697. ' TBird = class',
  34698. ' function Fly: TBird; async;',
  34699. ' end;',
  34700. 'function TObject.Run: TObject; async;',
  34701. 'begin',
  34702. 'end;',
  34703. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  34704. 'var o: TObject;',
  34705. 'begin',
  34706. ' o:=await(TObject,Run);',
  34707. ' o:=await(TObject,Fly);',
  34708. ' o:=await(TBird,Fly);',
  34709. ' o:=await(TObject,inherited Run);',
  34710. ' o:=await(TObject,inherited Run) as TBird;',
  34711. 'end;',
  34712. 'begin',
  34713. ' ']);
  34714. ConvertProgram;
  34715. CheckSource('TestAWait_ClassAs',
  34716. LinesToStr([ // statements
  34717. 'rtl.createClass(this, "TObject", null, function () {',
  34718. ' this.$init = function () {',
  34719. ' };',
  34720. ' this.$final = function () {',
  34721. ' };',
  34722. ' this.Run = async function () {',
  34723. ' var Result = null;',
  34724. ' return Result;',
  34725. ' };',
  34726. '});',
  34727. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34728. ' this.Fly = async function () {',
  34729. ' var Result = null;',
  34730. ' var o = null;',
  34731. ' o = await this.Run();',
  34732. ' o = await this.Fly();',
  34733. ' o = await this.Fly();',
  34734. ' o = await $mod.TObject.Run.call(this);',
  34735. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  34736. ' return Result;',
  34737. ' };',
  34738. '});',
  34739. '']),
  34740. LinesToStr([
  34741. '']));
  34742. CheckResolverUnexpectedHints();
  34743. end;
  34744. procedure TTestModule.TestLibrary_Empty;
  34745. begin
  34746. StartLibrary(false);
  34747. Add([
  34748. '']);
  34749. ConvertLibrary;
  34750. CheckFullSource('TestLibrary_Empty',
  34751. LinesToStr([ // statements
  34752. 'rtl.module("library", [], function () {',
  34753. ' var $mod = this;',
  34754. ' $mod.$main = function () {',
  34755. ' };',
  34756. '});',
  34757. 'rtl.run("library");',
  34758. '']));
  34759. CheckResolverUnexpectedHints();
  34760. end;
  34761. procedure TTestModule.TestLibrary_ExportFunc;
  34762. begin
  34763. StartLibrary(false);
  34764. Add([
  34765. 'procedure Run(w: word);',
  34766. 'begin',
  34767. 'end;',
  34768. 'exports',
  34769. ' Run;',
  34770. ' run name ''Foo'';',
  34771. ' test1.run name ''Test1Run'';',
  34772. '']);
  34773. ConvertLibrary;
  34774. CheckFullSource('TestLibrary_ExportFunc',
  34775. LinesToStr([ // statements
  34776. 'rtl.module("library", [], function () {',
  34777. ' var $mod = this;',
  34778. ' this.Run = function (w) {',
  34779. ' };',
  34780. ' $mod.$main = function () {',
  34781. ' };',
  34782. '});',
  34783. 'rtl.run("library");',
  34784. 'export const Run = pas.library.Run;',
  34785. 'export const Foo = pas.library.Run;',
  34786. 'export const Test1Run = pas.library.Run;',
  34787. '']));
  34788. CheckResolverUnexpectedHints();
  34789. end;
  34790. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  34791. begin
  34792. StartLibrary(false);
  34793. Add([
  34794. 'procedure Run(w: word); overload;',
  34795. 'begin',
  34796. 'end;',
  34797. 'procedure Run(s: string); overload;',
  34798. 'begin',
  34799. 'end;',
  34800. 'exports',
  34801. ' Run;',
  34802. '']);
  34803. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  34804. nCantDetermineWhichOverloadedFunctionToCall);
  34805. ConvertLibrary;
  34806. end;
  34807. procedure TTestModule.TestLibrary_Export_Index_Fail;
  34808. begin
  34809. StartLibrary(false);
  34810. Add([
  34811. 'procedure Run(w: word);',
  34812. 'begin',
  34813. 'end;',
  34814. 'exports',
  34815. ' Run index 3;',
  34816. '']);
  34817. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  34818. ConvertLibrary;
  34819. end;
  34820. procedure TTestModule.TestLibrary_ExportVar;
  34821. begin
  34822. StartLibrary(false);
  34823. Add([
  34824. 'var Wing: word;',
  34825. 'exports',
  34826. ' Wing, wing name ''BirdArm'';',
  34827. '']);
  34828. ConvertLibrary;
  34829. CheckFullSource('TestLibrary_ExportVar',
  34830. LinesToStr([ // statements
  34831. 'rtl.module("library", [], function () {',
  34832. ' var $mod = this;',
  34833. ' this.Wing = 0;',
  34834. ' $mod.$main = function () {',
  34835. ' };',
  34836. '});',
  34837. 'rtl.run("library");',
  34838. 'export const vars = {};',
  34839. 'Object.defineProperties(vars, {',
  34840. ' Wing: {',
  34841. ' enumerable: true,',
  34842. ' get: function () {',
  34843. ' return pas.library.Wing;',
  34844. ' },',
  34845. ' set: function (v) {',
  34846. ' pas.library.Wing = v;',
  34847. ' }',
  34848. ' },',
  34849. ' BirdArm: {',
  34850. ' enumerable: true,',
  34851. ' get: function () {',
  34852. ' return pas.library.Wing;',
  34853. ' },',
  34854. ' set: function (v) {',
  34855. ' pas.library.Wing = v;',
  34856. ' }',
  34857. ' }',
  34858. '});',
  34859. '']));
  34860. CheckResolverUnexpectedHints();
  34861. end;
  34862. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34863. begin
  34864. AddModuleWithIntfImplSrc('Unit1.pas',
  34865. LinesToStr([
  34866. 'type',
  34867. ' TAnt = class',
  34868. ' class function Crawl: word; static;',
  34869. ' end;',
  34870. 'function Fly: word;',
  34871. '']),
  34872. LinesToStr([
  34873. 'function Fly: word;',
  34874. 'begin',
  34875. 'end;',
  34876. 'class function TAnt.Crawl: word;',
  34877. 'begin',
  34878. 'end;',
  34879. '']));
  34880. StartLibrary(true,[supTObject]);
  34881. Add([
  34882. 'uses unit1;',
  34883. 'exports',
  34884. ' Fly;',
  34885. ' TAnt.Crawl;',
  34886. '']);
  34887. ConvertLibrary;
  34888. CheckFullSource('TestLibrary_ExportUnitFunc',
  34889. LinesToStr([ // statements
  34890. 'rtl.module("library", ["system", "Unit1"], function () {',
  34891. ' var $mod = this;',
  34892. ' $mod.$main = function () {',
  34893. ' };',
  34894. '});',
  34895. 'rtl.run("library");',
  34896. 'export const Fly = pas.Unit1.Fly;',
  34897. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  34898. '']));
  34899. CheckResolverUnexpectedHints();
  34900. end;
  34901. Initialization
  34902. RegisterTests([TTestModule]);
  34903. end.