tcmodules.pas 930 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval, FPPas2Js;
  22. const
  23. // default parser+scanner options
  24. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  25. co_tcmodules = [];
  26. type
  27. TSrcMarkerKind = (
  28. mkLabel,
  29. mkResolverReference,
  30. mkDirectReference
  31. );
  32. const
  33. SrcMarker: array[TSrcMarkerKind] of char = (
  34. '#', // mkLabel
  35. '@', // mkResolverReference
  36. '=' // mkDirectReference
  37. );
  38. type
  39. PSrcMarker = ^TSrcMarker;
  40. TSrcMarker = record
  41. Kind: TSrcMarkerKind;
  42. Filename: string;
  43. Row: integer;
  44. StartCol, EndCol: integer; // token start, end column
  45. Identifier: string;
  46. Next: PSrcMarker;
  47. end;
  48. TSystemUnitPart = (
  49. supTObject,
  50. supTVarRec,
  51. supTypeInfo,
  52. supTInterfacedObject,
  53. supWriteln
  54. );
  55. TSystemUnitParts = set of TSystemUnitPart;
  56. { TTestHintMessage }
  57. TTestHintMessage = class
  58. public
  59. Id: int64;
  60. MsgType: TMessageType;
  61. MsgNumber: integer;
  62. Msg: string;
  63. SourcePos: TPasSourcePos;
  64. end;
  65. TTestResolverReferenceData = record
  66. Filename: string;
  67. Row: integer;
  68. StartCol: integer;
  69. EndCol: integer;
  70. Found: TFPList; // list of TPasElement at this token
  71. end;
  72. PTestResolverReferenceData = ^TTestResolverReferenceData;
  73. { TTestPasParser }
  74. TTestPasParser = Class(TPasParser)
  75. end;
  76. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  77. { TTestEnginePasResolver }
  78. TTestEnginePasResolver = class(TPas2JsResolver)
  79. private
  80. FFilename: string;
  81. FModule: TPasModule;
  82. FOnFindUnit: TOnFindUnit;
  83. FParser: TTestPasParser;
  84. FStreamResolver: TStreamResolver;
  85. FScanner: TPas2jsPasScanner;
  86. FSource: string;
  87. procedure SetModule(const AValue: TPasModule);
  88. public
  89. destructor Destroy; override;
  90. function CreateElement(AClass: TPTreeElement; const AName: String;
  91. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  92. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  93. overload; override;
  94. function FindUnit(const AName, InFilename: String; NameExpr,
  95. InFileExpr: TPasExpr): TPasModule; override;
  96. procedure UsedInterfacesFinished(Section: TPasSection); override;
  97. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  98. property Filename: string read FFilename write FFilename;
  99. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  100. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  101. property Parser: TTestPasParser read FParser write FParser;
  102. property Source: string read FSource write FSource;
  103. property Module: TPasModule read FModule write SetModule;
  104. end;
  105. { TCustomTestModule }
  106. TCustomTestModule = Class(TTestCase)
  107. private
  108. FWithTypeInfo: boolean;
  109. FSource: TStringList;
  110. FSkipTests: boolean;
  111. FScanner: TPas2jsPasScanner;
  112. FResolvers: TObjectList;// list of TTestEnginePasResolver
  113. FPasProgram: TPasProgram;
  114. FPasLibrary: TPasLibrary;
  115. FParser: TTestPasParser;
  116. FModule: TPasModule;
  117. FJSSource: TStringList;
  118. FJSRegModuleCall: TJSCallExpression;
  119. FJSModuleSrc: TJSSourceElements;
  120. FJSModuleCallArgs: TJSArguments;
  121. FJSModule: TJSSourceElements;
  122. FJSInterfaceUses: TJSArrayLiteral;
  123. FJSInitBody: TJSFunctionBody;
  124. FJSImplentationUses: TJSArrayLiteral;
  125. FJSImplementationUses: TJSArrayLiteral;
  126. FHub: TPas2JSResolverHub;
  127. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  128. FHintMsgs: TObjectList; // list of TTestHintMessage
  129. FFirstPasStatement: TPasImplBlock;
  130. FFileResolver: TStreamResolver;
  131. FFilename: string;
  132. FExpectedErrorNumber: integer;
  133. FExpectedErrorMsg: string;
  134. FExpectedErrorClass: ExceptClass;
  135. FEngine: TTestEnginePasResolver;
  136. FConverter: TPasToJSConverter;
  137. {$IFDEF EnablePasTreeGlobalRefCount}
  138. FElementRefCountAtSetup: int64;
  139. {$ENDIF}
  140. procedure FreeSrcMarkers;
  141. function GetResolverCount: integer;
  142. function GetResolvers(Index: integer): TTestEnginePasResolver;
  143. function GetMsgCount: integer;
  144. function GetMsgs(Index: integer): TTestHintMessage;
  145. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  146. procedure OnParserLog(Sender: TObject; const Msg: String);
  147. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  148. procedure OnScannerLog(Sender: TObject; const Msg: String);
  149. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  150. procedure OnFindReference(El: TPasElement; FindData: pointer);
  151. procedure SetWithTypeInfo(const AValue: boolean);
  152. protected
  153. procedure SetUp; override;
  154. function CreateConverter: TPasToJSConverter; virtual;
  155. function LoadUnit(const aUnitName: String): TPasModule;
  156. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  157. procedure TearDown; override;
  158. Procedure Add(Line: string); virtual;
  159. Procedure Add(const Lines: array of string);
  160. Procedure StartParsing; virtual;
  161. procedure ParseModuleQueue; virtual;
  162. procedure ParseModule; virtual;
  163. procedure ParseProgram; virtual;
  164. procedure ParseLibrary; virtual;
  165. procedure ParseUnit; virtual;
  166. protected
  167. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  168. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  169. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  172. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  173. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  174. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  175. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure ConvertModule; virtual;
  178. procedure ConvertProgram; virtual;
  179. procedure ConvertLibrary; virtual;
  180. procedure ConvertUnit; virtual;
  181. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  182. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  183. function GetDottedIdentifier(El: TJSElement): string;
  184. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  185. ImplStatements: string = ''); virtual;
  186. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  187. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  188. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  189. procedure CheckReferenceDirectives; virtual;
  190. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  191. Msg: string; Marker: PSrcMarker = nil); virtual;
  192. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  193. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  194. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  197. function IsErrorExpected(E: Exception): boolean;
  198. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  199. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  200. procedure HandleScannerError(E: EScannerError);
  201. procedure HandleParserError(E: EParserError);
  202. procedure HandlePasResolveError(E: EPasResolve);
  203. procedure HandlePas2JSError(E: EPas2JS);
  204. procedure HandleException(E: Exception);
  205. procedure FailException(E: Exception);
  206. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  207. function IndexOfResolver(const Filename: string): integer;
  208. function GetResolver(const Filename: string): TTestEnginePasResolver;
  209. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  210. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  211. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  212. function FindSrcLabel(const Identifier: string): PSrcMarker;
  213. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  214. function GetDefaultNamespace: string;
  215. property PasProgram: TPasProgram Read FPasProgram;
  216. property PasLibrary: TPasLibrary Read FPasLibrary;
  217. property ResolverEngine: TTestEnginePasResolver read FEngine;
  218. property Filename: string read FFilename;
  219. Property Module: TPasModule Read FModule;
  220. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  221. property Converter: TPasToJSConverter read FConverter;
  222. property JSSource: TStringList read FJSSource;
  223. property JSModule: TJSSourceElements read FJSModule;
  224. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  225. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  226. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  227. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  228. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  229. property JSInitBody: TJSFunctionBody read FJSInitBody;
  230. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  231. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  232. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  233. property SkipTests: boolean read FSkipTests write FSkipTests;
  234. public
  235. constructor Create; override;
  236. destructor Destroy; override;
  237. property Hub: TPas2JSResolverHub read FHub;
  238. property Source: TStringList read FSource;
  239. property FileResolver: TStreamResolver read FFileResolver;
  240. property Scanner: TPas2jsPasScanner read FScanner;
  241. property Parser: TTestPasParser read FParser;
  242. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  243. property ResolverCount: integer read GetResolverCount;
  244. property MsgCount: integer read GetMsgCount;
  245. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  246. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  247. end;
  248. { TTestModule }
  249. TTestModule = class(TCustomTestModule)
  250. Published
  251. Procedure TestReservedWords;
  252. // program, units, includes
  253. Procedure TestEmptyProgram;
  254. Procedure TestEmptyProgramUseStrict;
  255. Procedure TestEmptyUnit;
  256. Procedure TestEmptyUnitUseStrict;
  257. Procedure TestDottedUnitNames;
  258. Procedure TestDottedUnitNameImpl;
  259. Procedure TestDottedUnitExpr;
  260. Procedure Test_ModeFPCFail;
  261. Procedure Test_ModeSwitchCBlocksFail;
  262. Procedure TestUnit_UseSystem;
  263. Procedure TestUnit_Intf1Impl2Intf1;
  264. Procedure TestIncludeVersion;
  265. // vars/const
  266. Procedure TestVarInt;
  267. Procedure TestVarBaseTypes;
  268. Procedure TestBaseTypeSingleFail;
  269. Procedure TestBaseTypeExtendedFail;
  270. Procedure TestConstBaseTypes;
  271. Procedure TestUnitImplVars;
  272. Procedure TestUnitImplConsts;
  273. Procedure TestUnitImplRecord;
  274. Procedure TestRenameJSNameConflict;
  275. Procedure TestLocalConst;
  276. Procedure TestVarExternal;
  277. Procedure TestVarExternalOtherUnit;
  278. Procedure TestVarAbsoluteFail;
  279. Procedure TestConstExternal;
  280. // numbers
  281. Procedure TestDouble;
  282. Procedure TestInteger;
  283. Procedure TestIntegerRange;
  284. Procedure TestIntegerTypecasts;
  285. Procedure TestInteger_BitwiseShrNativeInt;
  286. Procedure TestInteger_BitwiseShlNativeInt;
  287. Procedure TestInteger_SystemFunc;
  288. Procedure TestInteger_AssignOutsideConst;
  289. Procedure TestCurrency;
  290. Procedure TestForBoolDo;
  291. Procedure TestForIntDo;
  292. Procedure TestForIntInDo;
  293. // strings
  294. Procedure TestCharConst;
  295. Procedure TestChar_Compare;
  296. Procedure TestChar_BuiltInProcs;
  297. Procedure TestStringConst;
  298. Procedure TestStringConst_InvalidUTF16;
  299. Procedure TestStringConstSurrogate;
  300. Procedure TestStringConst_Multiline;
  301. Procedure TestString_Length;
  302. Procedure TestString_Compare;
  303. Procedure TestString_SetLength;
  304. Procedure TestString_CharAt;
  305. Procedure TestStringHMinusFail;
  306. Procedure TestStr;
  307. Procedure TestBaseType_AnsiStringFail;
  308. Procedure TestBaseType_WideStringFail;
  309. Procedure TestBaseType_ShortStringFail;
  310. Procedure TestBaseType_RawByteStringFail;
  311. Procedure TestTypeShortstring_Fail;
  312. Procedure TestCharSet_Custom;
  313. Procedure TestWideChar;
  314. Procedure TestForCharDo;
  315. Procedure TestForCharInDo;
  316. // alias types
  317. Procedure TestAliasTypeRef;
  318. Procedure TestTypeCast_BaseTypes;
  319. Procedure TestTypeCast_AliasBaseTypes;
  320. // functions
  321. Procedure TestEmptyProc;
  322. Procedure TestProcOneParam;
  323. Procedure TestFunctionWithoutParams;
  324. Procedure TestProcedureWithoutParams;
  325. Procedure TestPrgProcVar;
  326. Procedure TestProcTwoArgs;
  327. Procedure TestProc_DefaultValue;
  328. Procedure TestUnitProcVar;
  329. Procedure TestImplProc;
  330. Procedure TestFunctionResult;
  331. Procedure TestNestedProc;
  332. Procedure TestNestedProc_ResultString;
  333. Procedure TestForwardProc;
  334. Procedure TestNestedForwardProc;
  335. Procedure TestAssignFunctionResult;
  336. Procedure TestFunctionResultInCondition;
  337. Procedure TestFunctionResultInForLoop;
  338. Procedure TestFunctionResultInTypeCast;
  339. Procedure TestExit;
  340. Procedure TestExit_ResultInFinally;
  341. Procedure TestBreak;
  342. Procedure TestBreakAsVar;
  343. Procedure TestContinue;
  344. Procedure TestProc_External;
  345. Procedure TestProc_ExternalOtherUnit;
  346. Procedure TestProc_Asm;
  347. Procedure TestProc_AsmSubBlock;
  348. Procedure TestProc_Assembler;
  349. Procedure TestProc_VarParam;
  350. Procedure TestProc_VarParamString;
  351. Procedure TestProc_VarParamV;
  352. Procedure TestProc_Overload;
  353. Procedure TestProc_OverloadForward;
  354. Procedure TestProc_OverloadIntfImpl;
  355. Procedure TestProc_OverloadNested;
  356. Procedure TestProc_OverloadNestedForward;
  357. Procedure TestProc_OverloadUnitCycle;
  358. Procedure TestProc_Varargs;
  359. Procedure TestProc_ConstOrder;
  360. Procedure TestProc_DuplicateConst;
  361. Procedure TestProc_LocalVarAbsolute;
  362. Procedure TestProc_LocalVarInit;
  363. Procedure TestProc_ReservedWords;
  364. Procedure TestProc_ConstRefWord;
  365. // anonymous functions
  366. Procedure TestAnonymousProc_Assign_ObjFPC;
  367. Procedure TestAnonymousProc_Assign_Delphi;
  368. Procedure TestAnonymousProc_Arg;
  369. Procedure TestAnonymousProc_Typecast;
  370. Procedure TestAnonymousProc_With;
  371. Procedure TestAnonymousProc_ExceptOn;
  372. Procedure TestAnonymousProc_Nested;
  373. Procedure TestAnonymousProc_NestedAssignResult;
  374. Procedure TestAnonymousProc_Class;
  375. Procedure TestAnonymousProc_ForLoop;
  376. Procedure TestAnonymousProc_AsmDelphi;
  377. // enums, sets
  378. Procedure TestEnum_Name;
  379. Procedure TestEnum_Number;
  380. Procedure TestEnum_ConstFail;
  381. Procedure TestEnum_Functions;
  382. Procedure TestEnumRg_Functions;
  383. Procedure TestEnum_AsParams;
  384. Procedure TestEnumRange_Array;
  385. Procedure TestEnum_ForIn;
  386. Procedure TestEnum_ScopedNumber;
  387. Procedure TestEnum_InFunction;
  388. Procedure TestEnum_Name_Anonymous_Unit;
  389. Procedure TestSet_Enum;
  390. Procedure TestSet_Operators;
  391. Procedure TestSet_Operator_In;
  392. Procedure TestSet_Functions;
  393. Procedure TestSet_PassAsArgClone;
  394. Procedure TestSet_AsParams;
  395. Procedure TestSet_Property;
  396. Procedure TestSet_EnumConst;
  397. Procedure TestSet_IntConst;
  398. Procedure TestSet_IntRange;
  399. Procedure TestSet_AnonymousEnumType;
  400. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  401. Procedure TestSet_ConstEnum;
  402. Procedure TestSet_ConstChar;
  403. Procedure TestSet_ConstInt;
  404. Procedure TestSet_InFunction;
  405. Procedure TestSet_ForIn;
  406. // statements
  407. Procedure TestNestBegin;
  408. Procedure TestIncDec;
  409. Procedure TestLoHiFpcMode;
  410. Procedure TestLoHiDelphiMode;
  411. Procedure TestAssignments;
  412. Procedure TestArithmeticOperators1;
  413. Procedure TestMultiAdd;
  414. Procedure TestLogicalOperators;
  415. Procedure TestBitwiseOperators;
  416. Procedure TestBitwiseOperatorsLongword;
  417. Procedure TestFunctionInt;
  418. Procedure TestFunctionString;
  419. Procedure TestIfThen;
  420. Procedure TestForLoop;
  421. Procedure TestForLoopInsideFunction;
  422. Procedure TestForLoop_ReadVarAfter;
  423. Procedure TestForLoop_Nested;
  424. Procedure TestRepeatUntil;
  425. Procedure TestAsmBlock;
  426. Procedure TestAsmPas_Impl; // ToDo
  427. Procedure TestTryFinally;
  428. Procedure TestTryExcept;
  429. Procedure TestTryExcept_ReservedWords;
  430. Procedure TestIfThenRaiseElse;
  431. Procedure TestCaseOf;
  432. Procedure TestCaseOf_UseSwitch;
  433. Procedure TestCaseOfNoElse;
  434. Procedure TestCaseOfNoElse_UseSwitch;
  435. Procedure TestCaseOfRange;
  436. Procedure TestCaseOfString;
  437. Procedure TestCaseOfChar;
  438. Procedure TestCaseOfExternalClassConst;
  439. Procedure TestDebugger;
  440. // arrays
  441. Procedure TestArray_Dynamic;
  442. Procedure TestArray_Dynamic_Nil;
  443. Procedure TestArray_DynMultiDimensional;
  444. Procedure TestArray_DynamicAssign;
  445. Procedure TestArray_StaticInt;
  446. Procedure TestArray_StaticBool;
  447. Procedure TestArray_StaticChar;
  448. Procedure TestArray_StaticMultiDim;
  449. Procedure TestArray_StaticInFunction;
  450. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  451. Procedure TestArrayOfRecord;
  452. Procedure TestArray_StaticRecord;
  453. Procedure TestArrayOfSet;
  454. Procedure TestArray_DynAsParam;
  455. Procedure TestArray_StaticAsParam;
  456. Procedure TestArrayElement_AsParams;
  457. Procedure TestArrayElementFromFuncResult_AsParams;
  458. Procedure TestArrayEnumTypeRange;
  459. Procedure TestArray_SetLengthOutArg;
  460. Procedure TestArray_SetLengthProperty;
  461. Procedure TestArray_SetLengthMultiDim;
  462. Procedure TestArray_SetLengthDynOfStatic;
  463. Procedure TestArray_OpenArrayOfString;
  464. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  465. Procedure TestArray_ConstRef;
  466. Procedure TestArray_Concat;
  467. Procedure TestArray_Copy;
  468. Procedure TestArray_InsertDelete;
  469. Procedure TestArray_DynArrayConstObjFPC;
  470. Procedure TestArray_DynArrayConstDelphi;
  471. Procedure TestArray_ArrayLitAsParam;
  472. Procedure TestArray_ArrayLitMultiDimAsParam;
  473. Procedure TestArray_ArrayLitStaticAsParam;
  474. Procedure TestArray_ForInArrOfString;
  475. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  476. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  477. Procedure TestArrayOfConst_TVarRec;
  478. Procedure TestArrayOfConst_PassBaseTypes;
  479. Procedure TestArrayOfConst_PassObj;
  480. // record
  481. Procedure TestRecord_Empty;
  482. Procedure TestRecord_Var;
  483. Procedure TestRecord_VarExternal;
  484. Procedure TestRecord_WithDo;
  485. Procedure TestRecord_Assign;
  486. Procedure TestRecord_AsParams;
  487. Procedure TestRecord_ConstRef;
  488. Procedure TestRecordElement_AsParams;
  489. Procedure TestRecordElementFromFuncResult_AsParams;
  490. Procedure TestRecordElementFromWith_AsParams;
  491. Procedure TestRecord_Equal;
  492. Procedure TestRecord_JSValue;
  493. Procedure TestRecord_VariantFail;
  494. Procedure TestRecord_FieldArray;
  495. Procedure TestRecord_Const;
  496. Procedure TestRecord_TypecastFail;
  497. Procedure TestRecord_InFunction;
  498. // anonymous record
  499. Procedure TestRecordAnonym_Field;
  500. Procedure TestRecordAnonym_Assign;
  501. Procedure TestRecordAnonym_Nested;
  502. Procedure TestRecordAnonym_Const;
  503. Procedure TestRecordAnonym_InFunction;
  504. // advanced record
  505. Procedure TestAdvRecord_Function;
  506. Procedure TestAdvRecord_Property;
  507. Procedure TestAdvRecord_PropertyDefault;
  508. Procedure TestAdvRecord_Property_ClassMethod;
  509. Procedure TestAdvRecord_Const;
  510. Procedure TestAdvRecord_ExternalField;
  511. Procedure TestAdvRecord_SubRecord;
  512. Procedure TestAdvRecord_SubClass;
  513. Procedure TestAdvRecord_SubInterfaceFail;
  514. Procedure TestAdvRecord_Constructor;
  515. Procedure TestAdvRecord_ClassConstructor_Program;
  516. Procedure TestAdvRecord_ClassConstructor_Unit;
  517. // classes
  518. Procedure TestClass_TObjectDefaultConstructor;
  519. Procedure TestClass_TObjectConstructorWithParams;
  520. Procedure TestClass_TObjectConstructorWithDefaultParam;
  521. Procedure TestClass_Var;
  522. Procedure TestClass_Method;
  523. Procedure TestClass_Implementation;
  524. Procedure TestClass_Inheritance;
  525. Procedure TestClass_TypeAlias;
  526. Procedure TestClass_AbstractMethod;
  527. Procedure TestClass_CallInherited_ProcNoParams;
  528. Procedure TestClass_CallInherited_WithParams;
  529. Procedure TestClasS_CallInheritedConstructor;
  530. Procedure TestClass_ClassVar_Assign;
  531. Procedure TestClass_CallClassMethod;
  532. Procedure TestClass_CallClassMethodStatic;
  533. Procedure TestClass_Property;
  534. Procedure TestClass_Property_ClassMethod;
  535. Procedure TestClass_Property_ClassMethodStatic;
  536. Procedure TestClass_Property_Indexed;
  537. Procedure TestClass_Property_IndexSpec;
  538. Procedure TestClass_PropertyOfTypeArray;
  539. Procedure TestClass_PropertyDefault;
  540. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  541. //Procedure TestClass_PropertyDefault;
  542. Procedure TestClass_PropertyOverride;
  543. Procedure TestClass_PropertyIncVisibility;
  544. Procedure TestClass_Assigned;
  545. Procedure TestClass_WithClassDoCreate;
  546. Procedure TestClass_WithClassInstDoProperty;
  547. Procedure TestClass_WithClassInstDoPropertyWithParams;
  548. Procedure TestClass_WithClassInstDoFunc;
  549. Procedure TestClass_TypeCast;
  550. Procedure TestClass_TypeCastUntypedParam;
  551. Procedure TestClass_Overloads;
  552. Procedure TestClass_OverloadsAncestor;
  553. Procedure TestClass_OverloadConstructor;
  554. Procedure TestClass_OverloadDelphiOverride;
  555. Procedure TestClass_ReintroduceVarDelphi;
  556. Procedure TestClass_ReintroducedVar;
  557. Procedure TestClass_RaiseDescendant;
  558. Procedure TestClass_ExternalMethod;
  559. Procedure TestClass_ExternalVirtualNameMismatchFail;
  560. Procedure TestClass_ExternalOverrideFail;
  561. Procedure TestClass_ExternalVar;
  562. Procedure TestClass_Const;
  563. Procedure TestClass_ConstEnum;
  564. Procedure TestClass_LocalConstDuplicate_Prg;
  565. Procedure TestClass_LocalConstDuplicate_Unit;
  566. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  567. Procedure TestClass_LocalVarSelfFail;
  568. Procedure TestClass_ArgSelfFail;
  569. Procedure TestClass_NestedProcSelf;
  570. Procedure TestClass_NestedProcSelf2;
  571. Procedure TestClass_NestedProcClassSelf;
  572. Procedure TestClass_NestedProcCallInherited;
  573. Procedure TestClass_TObjectFree;
  574. Procedure TestClass_TObjectFree_VarArg;
  575. Procedure TestClass_TObjectFreeNewInstance;
  576. Procedure TestClass_TObjectFreeLowerCase;
  577. Procedure TestClass_TObjectFreeFunctionFail;
  578. Procedure TestClass_TObjectFreePropertyFail;
  579. Procedure TestClass_ForIn;
  580. Procedure TestClass_DispatchMessage;
  581. Procedure TestClass_Message_DuplicateIntFail;
  582. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  583. // class of
  584. Procedure TestClassOf_Create;
  585. Procedure TestClassOf_Call;
  586. Procedure TestClassOf_Assign;
  587. Procedure TestClassOf_Is;
  588. Procedure TestClassOf_Compare;
  589. Procedure TestClassOf_ClassVar;
  590. Procedure TestClassOf_ClassMethod;
  591. Procedure TestClassOf_ClassProperty;
  592. Procedure TestClassOf_ClassMethodSelf;
  593. Procedure TestClassOf_TypeCast;
  594. Procedure TestClassOf_ImplicitFunctionCall;
  595. Procedure TestClassOf_Const;
  596. // nested class
  597. Procedure TestNestedClass_Alias;
  598. Procedure TestNestedClass_Record;
  599. Procedure TestNestedClass_Class;
  600. // external class
  601. Procedure TestExternalClass_Var;
  602. Procedure TestExternalClass_Const;
  603. Procedure TestExternalClass_Dollar;
  604. Procedure TestExternalClass_DuplicateVarFail;
  605. Procedure TestExternalClass_Method;
  606. Procedure TestExternalClass_ClassMethod;
  607. Procedure TestExternalClass_ClassMethodStatic;
  608. Procedure TestExternalClass_FunctionResultInTypeCast;
  609. Procedure TestExternalClass_NonExternalOverride;
  610. Procedure TestExternalClass_OverloadHint;
  611. Procedure TestExternalClass_SameNamePublishedProperty;
  612. Procedure TestExternalClass_Property;
  613. Procedure TestExternalClass_PropertyDate;
  614. Procedure TestExternalClass_ClassProperty;
  615. Procedure TestExternalClass_ClassOf;
  616. Procedure TestExternalClass_ClassOtherUnit;
  617. Procedure TestExternalClass_Is;
  618. Procedure TestExternalClass_As;
  619. Procedure TestExternalClass_DestructorFail;
  620. Procedure TestExternalClass_New;
  621. Procedure TestExternalClass_ClassOf_New;
  622. Procedure TestExternalClass_FuncClassOf_New;
  623. Procedure TestExternalClass_New_PasClassFail;
  624. Procedure TestExternalClass_New_PasClassBracketsFail;
  625. Procedure TestExternalClass_NewExtName;
  626. Procedure TestExternalClass_Constructor;
  627. Procedure TestExternalClass_ConstructorBrackets;
  628. Procedure TestExternalClass_LocalConstSameName;
  629. Procedure TestExternalClass_ReintroduceOverload;
  630. Procedure TestExternalClass_Inherited;
  631. Procedure TestExternalClass_PascalAncestorFail;
  632. Procedure TestExternalClass_NewInstance;
  633. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  634. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  635. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  636. Procedure TestExternalClass_JSFunctionPasDescendant;
  637. Procedure TestExternalClass_PascalProperty;
  638. Procedure TestExternalClass_TypeCastToRootClass;
  639. Procedure TestExternalClass_TypeCastToJSObject;
  640. Procedure TestExternalClass_TypeCastStringToExternalString;
  641. Procedure TestExternalClass_TypeCastToJSFunction;
  642. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  643. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  644. Procedure TestExternalClass_BracketAccessor;
  645. Procedure TestExternalClass_BracketAccessor_Call;
  646. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  647. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  648. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  649. Procedure TestExternalClass_BracketAccessor_MultiType;
  650. Procedure TestExternalClass_BracketAccessor_Index;
  651. Procedure TestExternalClass_ForInJSObject;
  652. Procedure TestExternalClass_ForInJSArray;
  653. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  654. Procedure TestExternalClass_NestedConstructor;
  655. // class interfaces
  656. Procedure TestClassInterface_Corba;
  657. Procedure TestClassInterface_ProcExternalFail;
  658. Procedure TestClassInterface_Overloads;
  659. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  660. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  661. Procedure TestClassInterface_AncestorImpl;
  662. Procedure TestClassInterface_ImplReintroduce;
  663. Procedure TestClassInterface_MethodResolution;
  664. Procedure TestClassInterface_AncestorMoreInterfaces;
  665. Procedure TestClassInterface_MethodOverride;
  666. Procedure TestClassInterface_Corba_Delegation;
  667. Procedure TestClassInterface_Corba_DelegationStatic;
  668. Procedure TestClassInterface_Corba_Operators;
  669. Procedure TestClassInterface_Corba_Args;
  670. Procedure TestClassInterface_Corba_ForIn;
  671. Procedure TestClassInterface_COM_AssignVar;
  672. Procedure TestClassInterface_COM_AssignArg;
  673. Procedure TestClassInterface_COM_FunctionResult;
  674. Procedure TestClassInterface_COM_InheritedFuncResult;
  675. Procedure TestClassInterface_COM_IsAsTypeCasts;
  676. Procedure TestClassInterface_COM_PassAsArg;
  677. Procedure TestClassInterface_COM_PassToUntypedParam;
  678. Procedure TestClassInterface_COM_FunctionInExpr;
  679. Procedure TestClassInterface_COM_Property;
  680. Procedure TestClassInterface_COM_IntfProperty;
  681. Procedure TestClassInterface_COM_Delegation;
  682. Procedure TestClassInterface_COM_With;
  683. Procedure TestClassInterface_COM_ForIn;
  684. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  685. Procedure TestClassInterface_COM_RecordIntfFail;
  686. Procedure TestClassInterface_COM_UnitInitialization;
  687. Procedure TestClassInterface_GUID;
  688. Procedure TestClassInterface_GUIDProperty;
  689. // helpers
  690. Procedure TestClassHelper_ClassVar;
  691. Procedure TestClassHelper_Method_AccessInstanceFields;
  692. Procedure TestClassHelper_Method_Call;
  693. Procedure TestClassHelper_Method_Nested_Call;
  694. Procedure TestClassHelper_ClassMethod_Call;
  695. Procedure TestClassHelper_ClassOf;
  696. Procedure TestClassHelper_MethodRefObjFPC;
  697. Procedure TestClassHelper_Constructor;
  698. Procedure TestClassHelper_InheritedObjFPC;
  699. Procedure TestClassHelper_Property;
  700. Procedure TestClassHelper_Property_Array;
  701. Procedure TestClassHelper_Property_Array_Default;
  702. Procedure TestClassHelper_Property_Array_DefaultDefault;
  703. Procedure TestClassHelper_ClassProperty;
  704. Procedure TestClassHelper_ClassPropertyStatic;
  705. Procedure TestClassHelper_ClassProperty_Array;
  706. Procedure TestClassHelper_ForIn;
  707. Procedure TestClassHelper_PassProperty;
  708. Procedure TestExtClassHelper_ClassVar;
  709. Procedure TestExtClassHelper_Method_Call;
  710. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  711. Procedure TestRecordHelper_ClassVar;
  712. Procedure TestRecordHelper_Method_Call;
  713. Procedure TestRecordHelper_Constructor;
  714. Procedure TestTypeHelper_ClassVar;
  715. Procedure TestTypeHelper_PassResultElement;
  716. Procedure TestTypeHelper_PassArgs;
  717. Procedure TestTypeHelper_PassVarConst;
  718. Procedure TestTypeHelper_PassFuncResult;
  719. Procedure TestTypeHelper_PassPropertyField;
  720. Procedure TestTypeHelper_PassPropertyGetter;
  721. Procedure TestTypeHelper_PassClassPropertyField;
  722. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  723. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  724. Procedure TestTypeHelper_Property;
  725. Procedure TestTypeHelper_Property_Array;
  726. Procedure TestTypeHelper_ClassProperty;
  727. Procedure TestTypeHelper_ClassProperty_Array;
  728. Procedure TestTypeHelper_ClassMethod;
  729. Procedure TestTypeHelper_ExtClassMethodFail;
  730. Procedure TestTypeHelper_Constructor;
  731. Procedure TestTypeHelper_Word;
  732. Procedure TestTypeHelper_Boolean;
  733. Procedure TestTypeHelper_WordBool;
  734. Procedure TestTypeHelper_Double;
  735. Procedure TestTypeHelper_NativeInt;
  736. Procedure TestTypeHelper_StringChar;
  737. Procedure TestTypeHelper_JSValue;
  738. Procedure TestTypeHelper_Array;
  739. Procedure TestTypeHelper_EnumType;
  740. Procedure TestTypeHelper_SetType;
  741. Procedure TestTypeHelper_InterfaceType;
  742. Procedure TestTypeHelper_NestedSelf;
  743. // proc types
  744. Procedure TestProcType;
  745. Procedure TestProcType_Arg;
  746. Procedure TestProcType_FunctionFPC;
  747. Procedure TestProcType_FunctionDelphi;
  748. Procedure TestProcType_ProcedureDelphi;
  749. Procedure TestProcType_AsParam;
  750. Procedure TestProcType_MethodFPC;
  751. Procedure TestProcType_MethodDelphi;
  752. Procedure TestProcType_PropertyFPC;
  753. Procedure TestProcType_PropertyDelphi;
  754. Procedure TestProcType_WithClassInstDoPropertyFPC;
  755. Procedure TestProcType_Nested;
  756. Procedure TestProcType_NestedOfObject;
  757. Procedure TestProcType_ReferenceToProc;
  758. Procedure TestProcType_ReferenceToMethod;
  759. Procedure TestProcType_Typecast;
  760. Procedure TestProcType_PassProcToUntyped;
  761. Procedure TestProcType_PassProcToArray;
  762. Procedure TestProcType_SafeCallObjFPC;
  763. Procedure TestProcType_SafeCallDelphi;
  764. // pointer
  765. Procedure TestPointer;
  766. Procedure TestPointer_Proc;
  767. Procedure TestPointer_AssignRecordFail;
  768. Procedure TestPointer_AssignStaticArrayFail;
  769. Procedure TestPointer_TypeCastJSValueToPointer;
  770. Procedure TestPointer_NonRecordFail;
  771. Procedure TestPointer_AnonymousArgTypeFail;
  772. Procedure TestPointer_AnonymousVarTypeFail;
  773. Procedure TestPointer_AnonymousResultTypeFail;
  774. Procedure TestPointer_AddrOperatorFail;
  775. Procedure TestPointer_ArrayParamsFail;
  776. Procedure TestPointer_PointerAddFail;
  777. Procedure TestPointer_IncPointerFail;
  778. Procedure TestPointer_Record;
  779. Procedure TestPointer_RecordArg;
  780. // jsvalue
  781. Procedure TestJSValue_AssignToJSValue;
  782. Procedure TestJSValue_TypeCastToBaseType;
  783. Procedure TestJSValue_TypecastToJSValue;
  784. Procedure TestJSValue_Equal;
  785. Procedure TestJSValue_If;
  786. Procedure TestJSValue_Not;
  787. Procedure TestJSValue_Enum;
  788. Procedure TestJSValue_ClassInstance;
  789. Procedure TestJSValue_ClassOf;
  790. Procedure TestJSValue_ArrayOfJSValue;
  791. Procedure TestJSValue_ArrayLit;
  792. Procedure TestJSValue_Params;
  793. Procedure TestJSValue_UntypedParam;
  794. Procedure TestJSValue_FuncResultType;
  795. Procedure TestJSValue_ProcType_Assign;
  796. Procedure TestJSValue_ProcType_Equal;
  797. Procedure TestJSValue_ProcType_Param;
  798. Procedure TestJSValue_AssignToPointerFail;
  799. Procedure TestJSValue_OverloadDouble;
  800. Procedure TestJSValue_OverloadNativeInt;
  801. Procedure TestJSValue_OverloadWord;
  802. Procedure TestJSValue_OverloadString;
  803. Procedure TestJSValue_OverloadChar;
  804. Procedure TestJSValue_OverloadPointer;
  805. Procedure TestJSValue_ForIn;
  806. // RTTI
  807. Procedure TestRTTI_IntRange;
  808. Procedure TestRTTI_Double;
  809. Procedure TestRTTI_ProcType;
  810. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  811. Procedure TestRTTI_ProcTypeAnonymous;
  812. Procedure TestRTTI_EnumAndSetType;
  813. Procedure TestRTTI_EnumRange;
  814. Procedure TestRTTI_AnonymousEnumType;
  815. Procedure TestRTTI_StaticArray;
  816. Procedure TestRTTI_DynArray;
  817. Procedure TestRTTI_ArrayNestedAnonymous;
  818. Procedure TestRTTI_PublishedMethodOverloadFail;
  819. Procedure TestRTTI_PublishedMethodHideNoHint;
  820. Procedure TestRTTI_PublishedMethodExternalFail;
  821. Procedure TestRTTI_PublishedClassPropertyFail;
  822. Procedure TestRTTI_PublishedClassFieldFail;
  823. Procedure TestRTTI_PublishedFieldExternalFail;
  824. Procedure TestRTTI_Class_Field;
  825. Procedure TestRTTI_Class_Method;
  826. Procedure TestRTTI_Class_MethodArgFlags;
  827. Procedure TestRTTI_Class_Property;
  828. Procedure TestRTTI_Class_PropertyParams;
  829. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  830. Procedure TestRTTI_Class_OmitRTTI;
  831. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  832. Procedure TestRTTI_IndexModifier;
  833. Procedure TestRTTI_StoredModifier;
  834. Procedure TestRTTI_DefaultValue;
  835. Procedure TestRTTI_DefaultValueSet;
  836. Procedure TestRTTI_DefaultValueRangeType;
  837. Procedure TestRTTI_DefaultValueInherit;
  838. Procedure TestRTTI_OverrideMethod;
  839. Procedure TestRTTI_ReintroduceMethod;
  840. Procedure TestRTTI_OverloadProperty;
  841. // ToDo: array argument
  842. Procedure TestRTTI_ClassForward;
  843. Procedure TestRTTI_ClassOf;
  844. Procedure TestRTTI_Record;
  845. Procedure TestRTTI_RecordAnonymousArray;
  846. Procedure TestRTTI_Record_ClassVarType;
  847. Procedure TestRTTI_LocalTypes;
  848. Procedure TestRTTI_TypeInfo_BaseTypes;
  849. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  850. Procedure TestRTTI_TypeInfo_LocalFail;
  851. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  852. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  853. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  854. Procedure TestRTTI_TypeInfo_FunctionClassType;
  855. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  856. Procedure TestRTTI_Interface_Corba;
  857. Procedure TestRTTI_Interface_COM;
  858. Procedure TestRTTI_ClassHelper;
  859. Procedure TestRTTI_ExternalClass;
  860. Procedure TestRTTI_Unit;
  861. // Resourcestring
  862. Procedure TestResourcestringProgram;
  863. Procedure TestResourcestringUnit;
  864. Procedure TestResourcestringImplementation;
  865. // Attributes
  866. Procedure TestAttributes_Members;
  867. Procedure TestAttributes_Types;
  868. Procedure TestAttributes_HelperConstructor_Fail;
  869. Procedure TestAttributes_InterfacesList;
  870. // Assertions, checks
  871. procedure TestAssert;
  872. procedure TestAssert_SysUtils;
  873. procedure TestObjectChecks;
  874. procedure TestOverflowChecks_Int;
  875. procedure TestRangeChecks_AssignInt;
  876. procedure TestRangeChecks_AssignIntRange;
  877. procedure TestRangeChecks_AssignEnum;
  878. procedure TestRangeChecks_AssignEnumRange;
  879. procedure TestRangeChecks_AssignChar;
  880. procedure TestRangeChecks_AssignCharRange;
  881. procedure TestRangeChecks_ArrayIndex;
  882. procedure TestRangeChecks_ArrayOfRecIndex;
  883. procedure TestRangeChecks_StringIndex;
  884. procedure TestRangeChecks_TypecastInt;
  885. procedure TestRangeChecks_TypeHelperInt;
  886. // Async/AWait
  887. Procedure TestAsync_Proc;
  888. Procedure TestAsync_CallResultIsPromise;
  889. Procedure TestAsync_ConstructorFail;
  890. Procedure TestAsync_PropertyGetterFail;
  891. Procedure TestAwait_NonPromiseWithTypeFail;
  892. Procedure TestAwait_AsyncCallTypeMismatch;
  893. Procedure TestAWait_OutsideAsyncFail;
  894. Procedure TestAWait_IntegerFail;
  895. Procedure TestAWait_ExternalClassPromise;
  896. Procedure TestAWait_JSValue;
  897. Procedure TestAWait_Result;
  898. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  899. Procedure TestAsync_AnonymousProc;
  900. Procedure TestAsync_AnonymousProc_PassAsyncAsArg; // ToDo
  901. Procedure TestAsync_ProcType;
  902. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  903. Procedure TestAsync_Inherited;
  904. Procedure TestAsync_ClassInterface;
  905. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  906. Procedure TestAWait_ClassAs;
  907. // Library
  908. Procedure TestLibrary_Empty;
  909. Procedure TestLibrary_ExportFunc;
  910. Procedure TestLibrary_ExportFuncOverloadedFail;
  911. Procedure TestLibrary_Export_Index_Fail;
  912. Procedure TestLibrary_ExportVar;
  913. Procedure TestLibrary_ExportUnitFunc;
  914. // ToDo: test delayed specialization init
  915. // ToDo: shortrefoptimization
  916. end;
  917. function LinesToStr(Args: array of const): string;
  918. function ExtractFileUnitName(aFilename: string): string;
  919. function JSToStr(El: TJSElement): string;
  920. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  921. implementation
  922. function LinesToStr(Args: array of const): string;
  923. var
  924. s: String;
  925. i: Integer;
  926. begin
  927. s:='';
  928. for i:=Low(Args) to High(Args) do
  929. case Args[i].VType of
  930. vtChar: s += Args[i].VChar+LineEnding;
  931. vtString: s += Args[i].VString^+LineEnding;
  932. vtPChar: s += Args[i].VPChar+LineEnding;
  933. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  934. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  935. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  936. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  937. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  938. end;
  939. Result:=s;
  940. end;
  941. function ExtractFileUnitName(aFilename: string): string;
  942. var
  943. p: Integer;
  944. begin
  945. Result:=ExtractFileName(aFilename);
  946. if Result='' then exit;
  947. for p:=length(Result) downto 1 do
  948. case Result[p] of
  949. '/','\': exit;
  950. '.':
  951. begin
  952. Delete(Result,p,length(Result));
  953. exit;
  954. end;
  955. end;
  956. end;
  957. function JSToStr(El: TJSElement): string;
  958. var
  959. aWriter: TBufferWriter;
  960. aJSWriter: TJSWriter;
  961. begin
  962. aJSWriter:=nil;
  963. aWriter:=TBufferWriter.Create(1000);
  964. try
  965. aJSWriter:=TJSWriter.Create(aWriter);
  966. aJSWriter.IndentSize:=2;
  967. aJSWriter.WriteJS(El);
  968. Result:=aWriter.AsString;
  969. finally
  970. aJSWriter.Free;
  971. aWriter.Free;
  972. end;
  973. end;
  974. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  975. // search diff, ignore changes in spaces
  976. const
  977. SpaceChars = [#9,#10,#13,' '];
  978. var
  979. ExpectedP, ActualP: PChar;
  980. function FindLineEnd(p: PChar): PChar;
  981. begin
  982. Result:=p;
  983. while not (Result^ in [#0,#10,#13]) do inc(Result);
  984. end;
  985. function FindLineStart(p, MinP: PChar): PChar;
  986. begin
  987. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  988. Result:=p;
  989. end;
  990. procedure SkipLineEnd(var p: PChar);
  991. begin
  992. if p^ in [#10,#13] then
  993. begin
  994. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  995. inc(p,2)
  996. else
  997. inc(p);
  998. end;
  999. end;
  1000. function HasSpecialChar(s: string): boolean;
  1001. var
  1002. i: Integer;
  1003. begin
  1004. for i:=1 to length(s) do
  1005. if s[i] in [#0..#31,#127..#255] then
  1006. exit(true);
  1007. Result:=false;
  1008. end;
  1009. function HashSpecialChars(s: string): string;
  1010. var
  1011. i: Integer;
  1012. begin
  1013. Result:='';
  1014. for i:=1 to length(s) do
  1015. if s[i] in [#0..#31,#127..#255] then
  1016. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1017. else
  1018. Result:=Result+s[i];
  1019. end;
  1020. procedure DiffFound;
  1021. var
  1022. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1023. ExpLine, ActLine: String;
  1024. i, LineNo, DiffLineNo: Integer;
  1025. begin
  1026. writeln('Diff found "',Msg,'". Lines:');
  1027. // write correct lines
  1028. p:=PChar(Expected);
  1029. LineNo:=0;
  1030. DiffLineNo:=0;
  1031. repeat
  1032. StartPos:=p;
  1033. while not (p^ in [#0,#10,#13]) do inc(p);
  1034. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1035. SkipLineEnd(p);
  1036. inc(LineNo);
  1037. if (p<=ExpectedP) and (p^<>#0) then
  1038. begin
  1039. writeln('= ',ExpLine);
  1040. end else begin
  1041. // diff line
  1042. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1043. // write actual line
  1044. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1045. ActLineEndP:=FindLineEnd(ActualP);
  1046. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1047. writeln('- ',ActLine);
  1048. if HasSpecialChar(ActLine) then
  1049. writeln('- ',HashSpecialChars(ActLine));
  1050. // write expected line
  1051. writeln('+ ',ExpLine);
  1052. if HasSpecialChar(ExpLine) then
  1053. writeln('- ',HashSpecialChars(ExpLine));
  1054. // write empty line with pointer ^
  1055. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1056. writeln('^');
  1057. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1058. CheckSrcDiff:=false;
  1059. // write up to three following actual lines to get some context
  1060. for i:=1 to 3 do begin
  1061. ActLineStartP:=ActLineEndP;
  1062. SkipLineEnd(ActLineStartP);
  1063. if ActLineStartP^=#0 then break;
  1064. ActLineEndP:=FindLineEnd(ActLineStartP);
  1065. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1066. writeln('~ ',ActLine);
  1067. end;
  1068. exit;
  1069. end;
  1070. until p^=#0;
  1071. writeln('DiffFound Actual:-----------------------');
  1072. writeln(Actual);
  1073. writeln('DiffFound Expected:---------------------');
  1074. writeln(Expected);
  1075. writeln('DiffFound ------------------------------');
  1076. Msg:='diff found, but lines are the same, internal error';
  1077. CheckSrcDiff:=false;
  1078. end;
  1079. var
  1080. IsSpaceNeeded: Boolean;
  1081. LastChar, Quote: Char;
  1082. begin
  1083. Result:=true;
  1084. Msg:='';
  1085. if Expected='' then Expected:=' ';
  1086. if Actual='' then Actual:=' ';
  1087. ExpectedP:=PChar(Expected);
  1088. ActualP:=PChar(Actual);
  1089. repeat
  1090. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1091. case ExpectedP^ of
  1092. #0:
  1093. begin
  1094. // check that rest of Actual has only spaces
  1095. while ActualP^ in SpaceChars do inc(ActualP);
  1096. if ActualP^<>#0 then
  1097. begin
  1098. DiffFound;
  1099. exit;
  1100. end;
  1101. exit(true);
  1102. end;
  1103. ' ',#9,#10,#13:
  1104. begin
  1105. // skip space in Expected
  1106. IsSpaceNeeded:=false;
  1107. if ExpectedP>PChar(Expected) then
  1108. LastChar:=ExpectedP[-1]
  1109. else
  1110. LastChar:=#0;
  1111. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1112. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1113. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1114. IsSpaceNeeded:=true;
  1115. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1116. begin
  1117. DiffFound;
  1118. exit;
  1119. end;
  1120. while ActualP^ in SpaceChars do inc(ActualP);
  1121. end;
  1122. '''','"':
  1123. begin
  1124. while ActualP^ in SpaceChars do inc(ActualP);
  1125. if ExpectedP^<>ActualP^ then
  1126. begin
  1127. DiffFound;
  1128. exit;
  1129. end;
  1130. Quote:=ExpectedP^;
  1131. repeat
  1132. inc(ExpectedP);
  1133. inc(ActualP);
  1134. if ExpectedP^<>ActualP^ then
  1135. begin
  1136. DiffFound;
  1137. exit;
  1138. end;
  1139. if (ExpectedP^ in [#0,#10,#13]) then
  1140. break
  1141. else if (ExpectedP^=Quote) then
  1142. begin
  1143. inc(ExpectedP);
  1144. inc(ActualP);
  1145. break;
  1146. end;
  1147. until false;
  1148. end;
  1149. else
  1150. while ActualP^ in SpaceChars do inc(ActualP);
  1151. if ExpectedP^<>ActualP^ then
  1152. begin
  1153. DiffFound;
  1154. exit;
  1155. end;
  1156. inc(ExpectedP);
  1157. inc(ActualP);
  1158. end;
  1159. until false;
  1160. end;
  1161. { TTestEnginePasResolver }
  1162. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1163. begin
  1164. if FModule=AValue then Exit;
  1165. FModule:=AValue;
  1166. end;
  1167. destructor TTestEnginePasResolver.Destroy;
  1168. begin
  1169. FreeAndNil(FStreamResolver);
  1170. FreeAndNil(FParser);
  1171. FreeAndNil(FScanner);
  1172. FreeAndNil(FStreamResolver);
  1173. Module:=nil;
  1174. inherited Destroy;
  1175. end;
  1176. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1177. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1178. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1179. begin
  1180. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1181. TypeParams);
  1182. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1183. Module:=TPasModule(Result);
  1184. end;
  1185. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1186. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1187. begin
  1188. Result:=nil;
  1189. if InFilename<>'' then
  1190. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1191. if Assigned(OnFindUnit) then
  1192. Result:=OnFindUnit(AName);
  1193. if NameExpr=nil then ;
  1194. end;
  1195. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1196. begin
  1197. // do not parse recursively
  1198. // parse via the queue
  1199. if Section=nil then ;
  1200. end;
  1201. { TCustomTestModule }
  1202. procedure TCustomTestModule.FreeSrcMarkers;
  1203. var
  1204. aMarker, Last: PSrcMarker;
  1205. begin
  1206. aMarker:=FirstSrcMarker;
  1207. while aMarker<>nil do
  1208. begin
  1209. Last:=aMarker;
  1210. aMarker:=aMarker^.Next;
  1211. Dispose(Last);
  1212. end;
  1213. FirstSrcMarker:=nil;
  1214. LastSrcMarker:=nil;
  1215. end;
  1216. function TCustomTestModule.GetResolverCount: integer;
  1217. begin
  1218. Result:=FResolvers.Count;
  1219. end;
  1220. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1221. begin
  1222. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1223. end;
  1224. function TCustomTestModule.GetMsgCount: integer;
  1225. begin
  1226. Result:=FHintMsgs.Count;
  1227. end;
  1228. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1229. begin
  1230. Result:=TTestHintMessage(FHintMsgs[Index]);
  1231. end;
  1232. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1233. ): TPasModule;
  1234. var
  1235. DefNamespace: String;
  1236. begin
  1237. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1238. if (Pos('.',aUnitName)<1) then
  1239. begin
  1240. DefNamespace:=GetDefaultNamespace;
  1241. if DefNamespace<>'' then
  1242. begin
  1243. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1244. if Result<>nil then exit;
  1245. end;
  1246. end;
  1247. Result:=LoadUnit(aUnitName);
  1248. if Result<>nil then exit;
  1249. {$IFDEF VerbosePas2JS}
  1250. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1251. {$ENDIF}
  1252. Fail('can''t find unit "'+aUnitName+'"');
  1253. end;
  1254. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1255. var
  1256. aParser: TPasParser;
  1257. Item: TTestHintMessage;
  1258. begin
  1259. aParser:=Sender as TPasParser;
  1260. Item:=TTestHintMessage.Create;
  1261. Item.Id:=aParser.LastMsgNumber;
  1262. Item.MsgType:=aParser.LastMsgType;
  1263. Item.MsgNumber:=aParser.LastMsgNumber;
  1264. Item.Msg:=Msg;
  1265. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1266. {$IFDEF VerbosePas2JS}
  1267. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1268. {$ENDIF}
  1269. FHintMsgs.Add(Item);
  1270. end;
  1271. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1272. );
  1273. var
  1274. aResolver: TTestEnginePasResolver;
  1275. Item: TTestHintMessage;
  1276. begin
  1277. aResolver:=Sender as TTestEnginePasResolver;
  1278. Item:=TTestHintMessage.Create;
  1279. Item.Id:=aResolver.LastMsgId;
  1280. Item.MsgType:=aResolver.LastMsgType;
  1281. Item.MsgNumber:=aResolver.LastMsgNumber;
  1282. Item.Msg:=Msg;
  1283. Item.SourcePos:=aResolver.LastSourcePos;
  1284. {$IFDEF VerbosePas2JS}
  1285. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1286. {$ENDIF}
  1287. FHintMsgs.Add(Item);
  1288. end;
  1289. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1290. var
  1291. Item: TTestHintMessage;
  1292. aScanner: TPas2jsPasScanner;
  1293. begin
  1294. aScanner:=Sender as TPas2jsPasScanner;
  1295. Item:=TTestHintMessage.Create;
  1296. Item.Id:=aScanner.LastMsgNumber;
  1297. Item.MsgType:=aScanner.LastMsgType;
  1298. Item.MsgNumber:=aScanner.LastMsgNumber;
  1299. Item.Msg:=Msg;
  1300. Item.SourcePos:=aScanner.CurSourcePos;
  1301. {$IFDEF VerbosePas2JS}
  1302. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1303. {$ENDIF}
  1304. FHintMsgs.Add(Item);
  1305. end;
  1306. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1307. var
  1308. SubEl: TPasElement;
  1309. i: Integer;
  1310. procedure E(Msg: string);
  1311. var
  1312. s: String;
  1313. begin
  1314. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1315. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1316. writeln('ERROR: ',s);
  1317. Fail(s);
  1318. end;
  1319. begin
  1320. if arg=nil then ;
  1321. if El=nil then exit;
  1322. if El.Parent=El then
  1323. E('El.Parent=El='+GetObjName(El));
  1324. if El is TBinaryExpr then
  1325. begin
  1326. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1327. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1328. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1329. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1330. end
  1331. else if El is TParamsExpr then
  1332. begin
  1333. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1334. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1335. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1336. if TParamsExpr(El).Params[i].Parent<>El then
  1337. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1338. end
  1339. else if El is TProcedureExpr then
  1340. begin
  1341. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1342. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1343. end
  1344. else if El is TPasDeclarations then
  1345. begin
  1346. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1347. begin
  1348. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1349. if SubEl.Parent<>El then
  1350. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1351. end;
  1352. end
  1353. else if El is TPasImplBlock then
  1354. begin
  1355. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1356. begin
  1357. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1358. if SubEl.Parent<>El then
  1359. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1360. end;
  1361. end
  1362. else if El is TPasImplWithDo then
  1363. begin
  1364. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1365. begin
  1366. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1367. if SubEl.Parent<>El then
  1368. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1369. end;
  1370. end
  1371. else if El is TPasProcedure then
  1372. begin
  1373. if TPasProcedure(El).ProcType.Parent<>El then
  1374. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1375. end
  1376. else if El is TPasProcedureType then
  1377. begin
  1378. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1379. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1380. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1381. end;
  1382. end;
  1383. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1384. var
  1385. Data: PTestResolverReferenceData absolute FindData;
  1386. Line, Col: integer;
  1387. begin
  1388. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1389. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1390. if (Data^.Filename=El.SourceFilename)
  1391. and (Data^.Row=Line)
  1392. and (Data^.StartCol<=Col)
  1393. and (Data^.EndCol>=Col)
  1394. then
  1395. Data^.Found.Add(El);
  1396. end;
  1397. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1398. begin
  1399. if FWithTypeInfo=AValue then Exit;
  1400. FWithTypeInfo:=AValue;
  1401. if AValue then
  1402. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1403. else
  1404. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1405. end;
  1406. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1407. var
  1408. i: Integer;
  1409. CurEngine: TTestEnginePasResolver;
  1410. CurUnitName: String;
  1411. begin
  1412. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1413. Result:=nil;
  1414. if (Module.ClassType=TPasModule)
  1415. and (CompareText(Module.Name,aUnitName)=0) then
  1416. exit(Module);
  1417. for i:=0 to ResolverCount-1 do
  1418. begin
  1419. CurEngine:=Resolvers[i];
  1420. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1421. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1422. if CompareText(aUnitName,CurUnitName)=0 then
  1423. begin
  1424. Result:=CurEngine.Module;
  1425. if Result<>nil then exit;
  1426. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1427. FileResolver.FindSourceFile(aUnitName);
  1428. CurEngine.StreamResolver:=TStreamResolver.Create;
  1429. CurEngine.StreamResolver.OwnsStreams:=True;
  1430. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1431. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1432. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1433. InitScanner(CurEngine.Scanner);
  1434. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1435. CurEngine.Parser.Options:=po_tcmodules;
  1436. if CompareText(CurUnitName,'System')=0 then
  1437. CurEngine.Parser.ImplicitUses.Clear;
  1438. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1439. try
  1440. CurEngine.Parser.NextToken;
  1441. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1442. except
  1443. on E: Exception do
  1444. HandleException(E);
  1445. end;
  1446. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1447. Result:=CurEngine.Module;
  1448. exit;
  1449. end;
  1450. end;
  1451. end;
  1452. procedure TCustomTestModule.SetUp;
  1453. begin
  1454. {$IFDEF EnablePasTreeGlobalRefCount}
  1455. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1456. {$ENDIF}
  1457. if FResolvers<>nil then
  1458. begin
  1459. writeln('TCustomTestModule.SetUp FModules<>nil');
  1460. Halt;
  1461. end;
  1462. inherited SetUp;
  1463. FSkipTests:=false;
  1464. FWithTypeInfo:=false;
  1465. FSource:=TStringList.Create;
  1466. FHub:=TPas2JSResolverHub.Create(Self);
  1467. FResolvers:=TObjectList.Create(true);
  1468. FFilename:='test1.pp';
  1469. FFileResolver:=TStreamResolver.Create;
  1470. FFileResolver.OwnsStreams:=True;
  1471. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1472. InitScanner(FScanner);
  1473. FEngine:=AddModule(Filename);
  1474. FEngine.Scanner:=FScanner;
  1475. FScanner.Resolver:=FEngine;
  1476. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1477. FParser.OnLog:=@OnParserLog;
  1478. FEngine.Parser:=FParser;
  1479. Parser.Options:=po_tcmodules;
  1480. FModule:=Nil;
  1481. FConverter:=CreateConverter;
  1482. FExpectedErrorClass:=nil;
  1483. end;
  1484. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1485. var
  1486. Options: TPasToJsConverterOptions;
  1487. begin
  1488. Result:=TPasToJSConverter.Create;
  1489. Options:=co_tcmodules;
  1490. if WithTypeInfo then
  1491. Exclude(Options,coNoTypeInfo)
  1492. else
  1493. Include(Options,coNoTypeInfo);
  1494. Result.Options:=Options;
  1495. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1496. end;
  1497. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1498. begin
  1499. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1500. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1501. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1502. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1503. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1504. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1505. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1506. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1507. aScanner.OnLog:=@OnScannerLog;
  1508. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1509. end;
  1510. procedure TCustomTestModule.TearDown;
  1511. {$IFDEF CheckPasTreeRefCount}
  1512. var
  1513. El: TPasElement;
  1514. {$ENDIF}
  1515. var
  1516. i: Integer;
  1517. CurModule: TPasModule;
  1518. begin
  1519. FreeSrcMarkers;
  1520. FHintMsgs.Clear;
  1521. FHintMsgsGood.Clear;
  1522. FSkipTests:=false;
  1523. FWithTypeInfo:=false;
  1524. FJSRegModuleCall:=nil;
  1525. FJSModuleCallArgs:=nil;
  1526. FJSImplentationUses:=nil;
  1527. FJSInterfaceUses:=nil;
  1528. FJSModuleSrc:=nil;
  1529. FJSInitBody:=nil;
  1530. FreeAndNil(FJSSource);
  1531. FreeAndNil(FJSModule);
  1532. FreeAndNil(FConverter);
  1533. ResolverEngine.Clear;
  1534. FreeAndNil(FSource);
  1535. FreeAndNil(FFileResolver);
  1536. if FResolvers<>nil then
  1537. begin
  1538. for i:=0 to FResolvers.Count-1 do
  1539. begin
  1540. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1541. if CurModule=nil then continue;
  1542. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1543. CurModule.ReleaseUsedUnits;
  1544. end;
  1545. if FModule<>nil then
  1546. FModule.ReleaseUsedUnits;
  1547. for i:=0 to FResolvers.Count-1 do
  1548. begin
  1549. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1550. if CurModule=nil then continue;
  1551. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1552. end;
  1553. FreeAndNil(FResolvers);
  1554. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1555. FEngine:=nil;
  1556. end;
  1557. FreeAndNil(FHub);
  1558. inherited TearDown;
  1559. {$IFDEF EnablePasTreeGlobalRefCount}
  1560. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1561. begin
  1562. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1563. {$IFDEF CheckPasTreeRefCount}
  1564. El:=TPasElement.FirstRefEl;
  1565. while El<>nil do
  1566. begin
  1567. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1568. for i:=0 to El.RefIds.Count-1 do
  1569. writeln(' ',El.RefIds[i]);
  1570. El:=El.NextRefEl;
  1571. end;
  1572. {$ENDIF}
  1573. Halt;
  1574. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1575. end;
  1576. {$ENDIF}
  1577. end;
  1578. procedure TCustomTestModule.Add(Line: string);
  1579. begin
  1580. Source.Add(Line);
  1581. end;
  1582. procedure TCustomTestModule.Add(const Lines: array of string);
  1583. var
  1584. i: Integer;
  1585. begin
  1586. for i:=low(Lines) to high(Lines) do
  1587. Add(Lines[i]);
  1588. end;
  1589. procedure TCustomTestModule.StartParsing;
  1590. var
  1591. Src: String;
  1592. begin
  1593. Src:=Source.Text;
  1594. FEngine.Source:=Src;
  1595. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1596. Scanner.OpenFile(FileName);
  1597. Writeln('// Test : ',Self.TestName);
  1598. Writeln(Src);
  1599. end;
  1600. procedure TCustomTestModule.ParseModuleQueue;
  1601. var
  1602. i: Integer;
  1603. CurResolver: TTestEnginePasResolver;
  1604. Found: Boolean;
  1605. Section: TPasSection;
  1606. begin
  1607. // parse til exception or all Resolvers finished
  1608. while not SkipTests do
  1609. begin
  1610. Found:=false;
  1611. for i:=0 to ResolverCount-1 do
  1612. begin
  1613. CurResolver:=Resolvers[i];
  1614. if CurResolver.CurrentParser=nil then continue;
  1615. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1616. continue;
  1617. CurResolver.Parser.ParseContinue;
  1618. Found:=true;
  1619. break;
  1620. end;
  1621. if not Found then break;
  1622. end;
  1623. for i:=0 to ResolverCount-1 do
  1624. begin
  1625. CurResolver:=Resolvers[i];
  1626. if CurResolver.Parser=nil then
  1627. begin
  1628. if CurResolver.CurrentParser<>nil then
  1629. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1630. continue;
  1631. end;
  1632. if CurResolver.Parser.CurModule<>nil then
  1633. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1634. end;
  1635. end;
  1636. procedure TCustomTestModule.ParseModule;
  1637. begin
  1638. if SkipTests then exit;
  1639. FFirstPasStatement:=nil;
  1640. try
  1641. StartParsing;
  1642. Parser.ParseMain(FModule);
  1643. ParseModuleQueue;
  1644. except
  1645. on E: Exception do
  1646. HandleException(E);
  1647. end;
  1648. if SkipTests then exit;
  1649. AssertNotNull('Module resulted in Module',Module);
  1650. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1651. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1652. end;
  1653. procedure TCustomTestModule.ParseProgram;
  1654. begin
  1655. if SkipTests then exit;
  1656. ParseModule;
  1657. if SkipTests then exit;
  1658. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1659. FPasProgram:=TPasProgram(Module);
  1660. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1661. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1662. if (PasProgram.InitializationSection.Elements.Count>0) then
  1663. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1664. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1665. end;
  1666. procedure TCustomTestModule.ParseLibrary;
  1667. var
  1668. Init: TInitializationSection;
  1669. begin
  1670. if SkipTests then exit;
  1671. ParseModule;
  1672. if SkipTests then exit;
  1673. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1674. FPasLibrary:=TPasLibrary(Module);
  1675. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1676. Init:=PasLibrary.InitializationSection;
  1677. if (Init<>nil) and (Init.Elements.Count>0) then
  1678. if TObject(Init.Elements[0]) is TPasImplBlock then
  1679. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1680. end;
  1681. procedure TCustomTestModule.ParseUnit;
  1682. begin
  1683. if SkipTests then exit;
  1684. ParseModule;
  1685. if SkipTests then exit;
  1686. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1687. AssertNotNull('Has interface section',Module.InterfaceSection);
  1688. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1689. if (Module.InitializationSection<>nil)
  1690. and (Module.InitializationSection.Elements.Count>0)
  1691. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1692. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1693. end;
  1694. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1695. ): TTestEnginePasResolver;
  1696. var
  1697. i: Integer;
  1698. begin
  1699. for i:=0 to ResolverCount-1 do
  1700. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1701. exit(Resolvers[i]);
  1702. Result:=nil;
  1703. end;
  1704. function TCustomTestModule.AddModule(aFilename: string
  1705. ): TTestEnginePasResolver;
  1706. begin
  1707. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1708. if FindModuleWithFilename(aFilename)<>nil then
  1709. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1710. Result:=TTestEnginePasResolver.Create;
  1711. Result.Filename:=aFilename;
  1712. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1713. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1714. Result.OnLog:=@OnPasResolverLog;
  1715. Result.Hub:=Hub;
  1716. FResolvers.Add(Result);
  1717. end;
  1718. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1719. ): TTestEnginePasResolver;
  1720. begin
  1721. Result:=AddModule(aFilename);
  1722. Result.Source:=Src;
  1723. end;
  1724. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1725. ImplementationSrc: string): TTestEnginePasResolver;
  1726. var
  1727. Src: String;
  1728. begin
  1729. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1730. Src+=LineEnding;
  1731. Src+='interface'+LineEnding;
  1732. Src+=LineEnding;
  1733. Src+=InterfaceSrc;
  1734. Src+='implementation'+LineEnding;
  1735. Src+=LineEnding;
  1736. Src+=ImplementationSrc;
  1737. Src+='end.'+LineEnding;
  1738. Result:=AddModuleWithSrc(aFilename,Src);
  1739. end;
  1740. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1741. var
  1742. Intf, Impl: TStringList;
  1743. begin
  1744. Intf:=TStringList.Create;
  1745. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1746. // unit interface
  1747. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1748. Intf.Add('{$modeswitch externalclass}');
  1749. Intf.Add('type');
  1750. Intf.Add(' integer=longint;');
  1751. Intf.Add(' sizeint=nativeint;');
  1752. //'const',
  1753. //' LineEnding = #10;',
  1754. //' DirectorySeparator = ''/'';',
  1755. //' DriveSeparator = '''';',
  1756. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1757. //' AllowDriveSeparators : set of char = [];',
  1758. if supTObject in Parts then
  1759. Intf.AddStrings([
  1760. 'type',
  1761. ' TClass = class of TObject;',
  1762. ' TObject = class',
  1763. ' constructor Create;',
  1764. ' destructor Destroy; virtual;',
  1765. ' class function ClassType: TClass; assembler;',
  1766. ' class function ClassName: String; assembler;',
  1767. ' class function ClassNameIs(const Name: string): boolean;',
  1768. ' class function ClassParent: TClass; assembler;',
  1769. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1770. ' class function UnitName: String; assembler;',
  1771. ' procedure AfterConstruction; virtual;',
  1772. ' procedure BeforeDestruction;virtual;',
  1773. ' function Equals(Obj: TObject): boolean; virtual;',
  1774. ' function ToString: String; virtual;',
  1775. ' end;']);
  1776. if supTInterfacedObject in Parts then
  1777. Intf.AddStrings([
  1778. ' {$Interfaces COM}',
  1779. ' IUnknown = interface',
  1780. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1781. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1782. ' function _AddRef: Integer;',
  1783. ' function _Release: Integer;',
  1784. ' end;',
  1785. ' IInterface = IUnknown;',
  1786. ' TInterfacedObject = class(TObject,IUnknown)',
  1787. ' protected',
  1788. ' fRefCount: Integer;',
  1789. ' { implement methods of IUnknown }',
  1790. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1791. ' function _AddRef: Integer; virtual;',
  1792. ' function _Release: Integer; virtual;',
  1793. ' end;',
  1794. ' TInterfacedClass = class of TInterfacedObject;',
  1795. '',
  1796. '']);
  1797. if supTVarRec in Parts then
  1798. Intf.AddStrings([
  1799. 'const',
  1800. ' vtInteger = 0;',
  1801. ' vtBoolean = 1;',
  1802. ' vtJSValue = 19;',
  1803. 'type',
  1804. ' PVarRec = ^TVarRec;',
  1805. ' TVarRec = record',
  1806. ' VType : byte;',
  1807. ' VJSValue: JSValue;',
  1808. ' vInteger: longint external name ''VJSValue'';',
  1809. ' vBoolean: boolean external name ''VJSValue'';',
  1810. ' end;',
  1811. ' TVarRecArray = array of TVarRec;',
  1812. 'function VarRecs: TVarRecArray; varargs;',
  1813. '']);
  1814. if supTypeInfo in Parts then
  1815. begin
  1816. Intf.AddStrings([
  1817. 'type',
  1818. ' TTypeKind = (',
  1819. ' tkUnknown, // 0',
  1820. ' tkInteger, // 1',
  1821. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1822. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1823. ' tkEnumeration, // 4',
  1824. ' tkSet, // 5',
  1825. ' tkDouble, // 6',
  1826. ' tkBool, // 7',
  1827. ' tkProcVar, // 8 function or procedure',
  1828. ' tkMethod, // 9 proc var of object',
  1829. ' tkArray, // 10 static array',
  1830. ' tkDynArray, // 11',
  1831. ' tkRecord, // 12',
  1832. ' tkClass, // 13',
  1833. ' tkClassRef, // 14',
  1834. ' tkPointer, // 15',
  1835. ' tkJSValue, // 16',
  1836. ' tkRefToProcVar, // 17 variable of procedure type',
  1837. ' tkInterface, // 18',
  1838. ' //tkObject,',
  1839. ' //tkSString,tkLString,tkAString,tkWString,',
  1840. ' //tkVariant,',
  1841. ' //tkWChar,',
  1842. ' //tkInt64,',
  1843. ' //tkQWord,',
  1844. ' //tkInterfaceRaw,',
  1845. ' //tkUString,tkUChar,',
  1846. ' tkHelper, // 19',
  1847. ' //tkFile,',
  1848. ' tkExtClass // 20',
  1849. ' );',
  1850. ' TTypeKinds = set of TTypeKind;',
  1851. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1852. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1853. ' end;',
  1854. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1855. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1856. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1857. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1858. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1859. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1860. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1861. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1862. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1863. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1864. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1865. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1866. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1867. '']);
  1868. end;
  1869. if supWriteln in Parts then
  1870. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1871. Intf.Add('var');
  1872. Intf.Add(' ExitCode: Longint = 0;');
  1873. // unit implementation
  1874. Impl:=TStringList.Create;
  1875. if supTObject in Parts then
  1876. Impl.AddStrings([
  1877. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1878. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1879. 'asm',
  1880. 'end;',
  1881. 'constructor TObject.Create; begin end;',
  1882. 'destructor TObject.Destroy; begin end;',
  1883. 'class function TObject.ClassType: TClass; assembler;',
  1884. 'asm',
  1885. 'end;',
  1886. 'class function TObject.ClassName: String; assembler;',
  1887. 'asm',
  1888. 'end;',
  1889. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1890. 'begin',
  1891. ' Result:=SameText(Name,ClassName);',
  1892. 'end;',
  1893. 'class function TObject.ClassParent: TClass; assembler;',
  1894. 'asm',
  1895. 'end;',
  1896. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1897. 'asm',
  1898. 'end;',
  1899. 'class function TObject.UnitName: String; assembler;',
  1900. 'asm',
  1901. 'end;',
  1902. 'procedure TObject.AfterConstruction; begin end;',
  1903. 'procedure TObject.BeforeDestruction; begin end;',
  1904. 'function TObject.Equals(Obj: TObject): boolean;',
  1905. 'begin',
  1906. ' Result:=Obj=Self;',
  1907. 'end;',
  1908. 'function TObject.ToString: String;',
  1909. 'begin',
  1910. ' Result:=ClassName;',
  1911. 'end;'
  1912. ]);
  1913. if supTInterfacedObject in Parts then
  1914. Impl.AddStrings([
  1915. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1916. //'begin',
  1917. //'end;',
  1918. 'function TInterfacedObject._AddRef: Integer;',
  1919. 'begin',
  1920. 'end;',
  1921. 'function TInterfacedObject._Release: Integer;',
  1922. 'begin',
  1923. 'end;',
  1924. '']);
  1925. if supTVarRec in Parts then
  1926. Impl.AddStrings([
  1927. 'function VarRecs: TVarRecArray; varargs;',
  1928. 'var',
  1929. ' v: PVarRec;',
  1930. 'begin',
  1931. ' v^.VType:=1;',
  1932. ' v^.VJSValue:=2;',
  1933. 'end;',
  1934. '']);
  1935. try
  1936. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1937. finally
  1938. Intf.Free;
  1939. Impl.Free;
  1940. end;
  1941. end;
  1942. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1943. SystemUnitParts: TSystemUnitParts);
  1944. begin
  1945. if NeedSystemUnit then
  1946. AddSystemUnit(SystemUnitParts)
  1947. else
  1948. Parser.ImplicitUses.Clear;
  1949. Add('program '+ExtractFileUnitName(Filename)+';');
  1950. Add('');
  1951. end;
  1952. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1953. SystemUnitParts: TSystemUnitParts);
  1954. begin
  1955. if NeedSystemUnit then
  1956. AddSystemUnit(SystemUnitParts)
  1957. else
  1958. Parser.ImplicitUses.Clear;
  1959. Add('library '+ExtractFileUnitName(Filename)+';');
  1960. Add('');
  1961. end;
  1962. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1963. SystemUnitParts: TSystemUnitParts);
  1964. begin
  1965. if NeedSystemUnit then
  1966. AddSystemUnit(SystemUnitParts)
  1967. else
  1968. Parser.ImplicitUses.Clear;
  1969. Add('unit Test1;');
  1970. Add('');
  1971. end;
  1972. procedure TCustomTestModule.ConvertModule;
  1973. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1974. out UsesLit: TJSArrayLiteral);
  1975. var
  1976. i: Integer;
  1977. Item: TJSElement;
  1978. Lit: TJSLiteral;
  1979. begin
  1980. UsesLit:=nil;
  1981. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1982. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1983. exit; // null is ok
  1984. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1985. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1986. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1987. begin
  1988. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1989. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1990. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1991. Lit:=TJSLiteral(Item);
  1992. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1993. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1994. end;
  1995. end;
  1996. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1997. out Src: TJSSourceElements);
  1998. var
  1999. FunDecl: TJSFunctionDeclarationStatement;
  2000. FunDef: TJSFuncDef;
  2001. FunBody: TJSFunctionBody;
  2002. begin
  2003. Src:=nil;
  2004. AssertNotNull(ParamName,Arg.Expr);
  2005. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2006. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2007. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2008. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2009. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2010. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2011. AssertNotNull(ParamName+' body',FunDef.Body);
  2012. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2013. FunBody:=FunDef.Body as TJSFunctionBody;
  2014. AssertNotNull(ParamName+' body.A',FunBody.A);
  2015. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2016. Src:=FunBody.A as TJSSourceElements;
  2017. end;
  2018. var
  2019. ModuleNameExpr: TJSLiteral;
  2020. InitFunction: TJSFunctionDeclarationStatement;
  2021. InitAssign: TJSSimpleAssignStatement;
  2022. InitName: String;
  2023. LastNode, FirstNode: TJSElement;
  2024. Arg: TJSArrayLiteralElement;
  2025. IsProg, IsLib: Boolean;
  2026. begin
  2027. if SkipTests then exit;
  2028. IsProg:=false;
  2029. IsLib:=false;
  2030. if Module is TPasProgram then
  2031. IsProg:=true
  2032. else if Module is TPasLibrary then
  2033. IsLib:=true;
  2034. try
  2035. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2036. except
  2037. on E: Exception do
  2038. HandleException(E);
  2039. end;
  2040. if SkipTests then exit;
  2041. if ExpectedErrorClass<>nil then
  2042. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2043. FJSSource:=TStringList.Create;
  2044. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2045. {$IFDEF VerbosePas2JS}
  2046. writeln('TTestModule.ConvertModule JS:');
  2047. write(FJSSource.Text);
  2048. {$ENDIF}
  2049. // rtl.module(...
  2050. if JSModule.Statements.Count<1 then
  2051. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2052. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2053. AssertNotNull('register module call',FirstNode);
  2054. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2055. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2056. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2057. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2058. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2059. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2060. // parameter 'unitname'
  2061. if JSModuleCallArgs.Elements.Count<1 then
  2062. Fail('rtl.module first param unit missing');
  2063. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2064. AssertNotNull('module name param',Arg.Expr);
  2065. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2066. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2067. if IsProg then
  2068. begin
  2069. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2070. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2071. end
  2072. else if IsLib then
  2073. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2074. else
  2075. begin
  2076. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2077. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2078. end;
  2079. // main uses section
  2080. if JSModuleCallArgs.Elements.Count<2 then
  2081. Fail('rtl.module second param main uses missing');
  2082. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2083. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2084. // program/library/interface function()
  2085. if JSModuleCallArgs.Elements.Count<3 then
  2086. Fail('rtl.module third param intf-function missing');
  2087. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2088. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2089. // search for $mod.$init or $mod.$main - the last statement
  2090. if IsProg or IsLib then
  2091. begin
  2092. InitName:='$main';
  2093. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2094. end
  2095. else
  2096. InitName:='$init';
  2097. InitAssign:=nil;
  2098. InitFunction:=nil;
  2099. FJSInitBody:=nil;
  2100. if JSModuleSrc.Statements.Count>0 then
  2101. begin
  2102. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2103. if LastNode is TJSSimpleAssignStatement then
  2104. begin
  2105. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2106. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2107. begin
  2108. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2109. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2110. end
  2111. else if IsProg or IsLib then
  2112. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2113. end;
  2114. end;
  2115. // optional: implementation uses section
  2116. if JSModuleCallArgs.Elements.Count<4 then
  2117. exit;
  2118. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2119. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2120. end;
  2121. procedure TCustomTestModule.ConvertProgram;
  2122. begin
  2123. Add('end.');
  2124. ParseProgram;
  2125. ConvertModule;
  2126. end;
  2127. procedure TCustomTestModule.ConvertLibrary;
  2128. begin
  2129. Add('end.');
  2130. ParseLibrary;
  2131. ConvertModule;
  2132. end;
  2133. procedure TCustomTestModule.ConvertUnit;
  2134. begin
  2135. Add('end.');
  2136. ParseUnit;
  2137. ConvertModule;
  2138. end;
  2139. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2140. begin
  2141. Result:=tcmodules.JSToStr(El);
  2142. end;
  2143. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2144. DottedName: string);
  2145. begin
  2146. if DottedName='' then
  2147. begin
  2148. AssertNull(Msg,El);
  2149. end
  2150. else
  2151. begin
  2152. AssertNotNull(Msg,El);
  2153. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2154. end;
  2155. end;
  2156. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2157. begin
  2158. if El=nil then
  2159. Result:=''
  2160. else if El is TJSPrimaryExpressionIdent then
  2161. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2162. else if El is TJSDotMemberExpression then
  2163. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2164. else
  2165. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2166. end;
  2167. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2168. InitStatements: string; ImplStatements: string);
  2169. var
  2170. ActualSrc, ExpectedSrc, InitName: String;
  2171. IsProg, IsLib: Boolean;
  2172. begin
  2173. ActualSrc:=JSToStr(JSModuleSrc);
  2174. if coUseStrict in Converter.Options then
  2175. ExpectedSrc:='"use strict";'+LineEnding
  2176. else
  2177. ExpectedSrc:='';
  2178. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2179. ExpectedSrc:=ExpectedSrc+Statements;
  2180. // unit implementation
  2181. if (Trim(ImplStatements)<>'') then
  2182. ExpectedSrc:=ExpectedSrc+LineEnding
  2183. +'$mod.$implcode = function () {'+LineEnding
  2184. +ImplStatements
  2185. +'};'+LineEnding;
  2186. // program main or unit initialization
  2187. IsProg:=false;
  2188. IsLib:=false;
  2189. if Module is TPasProgram then
  2190. IsProg:=true
  2191. else if Module is TPasLibrary then
  2192. IsLib:=true;
  2193. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2194. begin
  2195. if IsProg or IsLib then
  2196. InitName:='$main'
  2197. else
  2198. InitName:='$init';
  2199. ExpectedSrc:=ExpectedSrc+LineEnding
  2200. +'$mod.'+InitName+' = function () {'+LineEnding
  2201. +InitStatements
  2202. +'};'+LineEnding;
  2203. end;
  2204. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2205. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2206. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2207. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2208. end;
  2209. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2210. var
  2211. ActualSrc: String;
  2212. begin
  2213. ActualSrc:=JSToStr(JSModule);
  2214. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2215. end;
  2216. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2217. // search diff, ignore changes in spaces
  2218. var
  2219. s: string;
  2220. begin
  2221. if CheckSrcDiff(Expected,Actual,s) then exit;
  2222. Fail(Msg+': '+s);
  2223. end;
  2224. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2225. var
  2226. aResolver: TTestEnginePasResolver;
  2227. aConverter: TPasToJSConverter;
  2228. aJSModule: TJSSourceElements;
  2229. ActualSrc: String;
  2230. begin
  2231. aResolver:=GetResolver(Filename);
  2232. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2233. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2234. {$IFDEF VerbosePas2JS}
  2235. writeln('CheckUnit '+Filename+' converting ...');
  2236. {$ENDIF}
  2237. aConverter:=CreateConverter;
  2238. aJSModule:=nil;
  2239. try
  2240. try
  2241. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2242. except
  2243. on E: Exception do
  2244. HandleException(E);
  2245. end;
  2246. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2247. {$IFDEF VerbosePas2JS}
  2248. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2249. write(aResolver.Source);
  2250. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2251. write(ActualSrc);
  2252. {$ENDIF}
  2253. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2254. finally
  2255. aJSModule.Free;
  2256. aConverter.Free;
  2257. end;
  2258. end;
  2259. procedure TCustomTestModule.CheckReferenceDirectives;
  2260. var
  2261. CurFilename: string;
  2262. LineNumber: Integer;
  2263. SrcLine: String;
  2264. CommentStartP, CommentEndP: PChar;
  2265. procedure RaiseError(Msg: string; p: PChar);
  2266. begin
  2267. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2268. end;
  2269. procedure AddMarker(Marker: PSrcMarker);
  2270. begin
  2271. if LastSrcMarker<>nil then
  2272. LastSrcMarker^.Next:=Marker
  2273. else
  2274. FirstSrcMarker:=Marker;
  2275. LastSrcMarker:=Marker;
  2276. end;
  2277. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2278. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2279. begin
  2280. New(Result);
  2281. Result^.Kind:=Kind;
  2282. Result^.Filename:=aFilename;
  2283. Result^.Row:=aLine;
  2284. Result^.StartCol:=aStartCol;
  2285. Result^.EndCol:=aEndCol;
  2286. Result^.Identifier:=Identifier;
  2287. Result^.Next:=nil;
  2288. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2289. AddMarker(Result);
  2290. end;
  2291. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2292. const Identifier: string): PSrcMarker;
  2293. var
  2294. TokenStart, p: PChar;
  2295. begin
  2296. p:=CommentEndP;
  2297. ReadNextPascalToken(p,TokenStart,false,false);
  2298. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2299. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2300. end;
  2301. function ReadIdentifier(var p: PChar): string;
  2302. var
  2303. StartP: PChar;
  2304. begin
  2305. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2306. RaiseError('identifier expected',p);
  2307. StartP:=p;
  2308. inc(p);
  2309. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2310. Result:='';
  2311. SetLength(Result,p-StartP);
  2312. Move(StartP^,Result[1],length(Result));
  2313. end;
  2314. procedure AddLabel;
  2315. var
  2316. Identifier: String;
  2317. p: PChar;
  2318. begin
  2319. p:=CommentStartP+2;
  2320. Identifier:=ReadIdentifier(p);
  2321. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2322. if FindSrcLabel(Identifier)<>nil then
  2323. RaiseError('duplicate label "'+Identifier+'"',p);
  2324. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2325. end;
  2326. procedure AddResolverReference;
  2327. var
  2328. Identifier: String;
  2329. p: PChar;
  2330. begin
  2331. p:=CommentStartP+2;
  2332. Identifier:=ReadIdentifier(p);
  2333. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2334. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2335. end;
  2336. procedure AddDirectReference;
  2337. var
  2338. Identifier: String;
  2339. p: PChar;
  2340. begin
  2341. p:=CommentStartP+2;
  2342. Identifier:=ReadIdentifier(p);
  2343. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2344. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2345. end;
  2346. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2347. var
  2348. p: PChar;
  2349. IsDirective: Boolean;
  2350. begin
  2351. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2352. CurFilename:=aFilename;
  2353. // parse code, find all labels
  2354. LineNumber:=0;
  2355. while LineNumber<SrcLines.Count do
  2356. begin
  2357. inc(LineNumber);
  2358. SrcLine:=SrcLines[LineNumber-1];
  2359. if SrcLine='' then continue;
  2360. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2361. p:=PChar(SrcLine);
  2362. repeat
  2363. case p^ of
  2364. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2365. '{':
  2366. begin
  2367. CommentStartP:=p;
  2368. inc(p);
  2369. IsDirective:=p^ in ['#','@','='];
  2370. // skip to end of comment
  2371. repeat
  2372. case p^ of
  2373. #0:
  2374. if (p-PChar(SrcLine)=length(SrcLine)) then
  2375. begin
  2376. // multi line comment
  2377. if IsDirective then
  2378. RaiseError('directive missing closing bracket',CommentStartP);
  2379. repeat
  2380. inc(LineNumber);
  2381. if LineNumber>SrcLines.Count then exit;
  2382. SrcLine:=SrcLines[LineNumber-1];
  2383. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2384. until SrcLine<>'';
  2385. p:=PChar(SrcLine);
  2386. continue;
  2387. end;
  2388. '}':
  2389. begin
  2390. inc(p);
  2391. break;
  2392. end;
  2393. end;
  2394. inc(p);
  2395. until false;
  2396. CommentEndP:=p;
  2397. case CommentStartP[1] of
  2398. '#': AddLabel;
  2399. '@': AddResolverReference;
  2400. '=': AddDirectReference;
  2401. end;
  2402. p:=CommentEndP;
  2403. continue;
  2404. end;
  2405. '/':
  2406. if p[1]='/' then
  2407. break; // rest of line is comment -> skip
  2408. end;
  2409. inc(p);
  2410. until false;
  2411. end;
  2412. end;
  2413. procedure CheckResolverReference(aMarker: PSrcMarker);
  2414. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2415. var
  2416. aLabel: PSrcMarker;
  2417. ReferenceElements, LabelElements: TFPList;
  2418. i, j, aLine, aCol: Integer;
  2419. El, Ref, LabelEl: TPasElement;
  2420. begin
  2421. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2422. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2423. if aLabel=nil then
  2424. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2425. LabelElements:=nil;
  2426. ReferenceElements:=nil;
  2427. try
  2428. LabelElements:=FindElementsAt(aLabel);
  2429. ReferenceElements:=FindElementsAt(aMarker);
  2430. for i:=0 to ReferenceElements.Count-1 do
  2431. begin
  2432. El:=TPasElement(ReferenceElements[i]);
  2433. Ref:=nil;
  2434. if El.CustomData is TResolvedReference then
  2435. Ref:=TResolvedReference(El.CustomData).Declaration
  2436. else if El.CustomData is TPasPropertyScope then
  2437. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2438. else if El.CustomData is TPasSpecializeTypeData then
  2439. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2440. if Ref<>nil then
  2441. for j:=0 to LabelElements.Count-1 do
  2442. begin
  2443. LabelEl:=TPasElement(LabelElements[j]);
  2444. if Ref=LabelEl then
  2445. exit; // success
  2446. end;
  2447. end;
  2448. // failure write candidates
  2449. for i:=0 to ReferenceElements.Count-1 do
  2450. begin
  2451. El:=TPasElement(ReferenceElements[i]);
  2452. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2453. write(' El=',GetObjName(El));
  2454. if EL is TPrimitiveExpr then
  2455. begin
  2456. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2457. end;
  2458. Ref:=nil;
  2459. if El.CustomData is TResolvedReference then
  2460. Ref:=TResolvedReference(El.CustomData).Declaration
  2461. else if El.CustomData is TPasPropertyScope then
  2462. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2463. else if El.CustomData is TPasSpecializeTypeData then
  2464. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2465. if Ref<>nil then
  2466. begin
  2467. write(' Decl=',GetObjName(Ref));
  2468. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2469. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2470. end
  2471. else
  2472. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2473. writeln;
  2474. end;
  2475. for i:=0 to LabelElements.Count-1 do
  2476. begin
  2477. El:=TPasElement(LabelElements[i]);
  2478. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2479. write(' El=',GetObjName(El));
  2480. writeln;
  2481. end;
  2482. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2483. finally
  2484. LabelElements.Free;
  2485. ReferenceElements.Free;
  2486. end;
  2487. end;
  2488. procedure CheckDirectReference(aMarker: PSrcMarker);
  2489. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2490. var
  2491. aLabel: PSrcMarker;
  2492. ReferenceElements, LabelElements: TFPList;
  2493. i, LabelLine, LabelCol, j: Integer;
  2494. El, LabelEl: TPasElement;
  2495. DeclEl, TypeEl: TPasType;
  2496. begin
  2497. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2498. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2499. if aLabel=nil then
  2500. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2501. LabelElements:=nil;
  2502. ReferenceElements:=nil;
  2503. try
  2504. //writeln('CheckDirectReference finding elements at label ...');
  2505. LabelElements:=FindElementsAt(aLabel);
  2506. //writeln('CheckDirectReference finding elements at reference ...');
  2507. ReferenceElements:=FindElementsAt(aMarker);
  2508. for i:=0 to ReferenceElements.Count-1 do
  2509. begin
  2510. El:=TPasElement(ReferenceElements[i]);
  2511. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2512. if El.ClassType=TPasVariable then
  2513. begin
  2514. if TPasVariable(El).VarType=nil then
  2515. begin
  2516. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2517. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2518. end;
  2519. TypeEl:=TPasVariable(El).VarType;
  2520. for j:=0 to LabelElements.Count-1 do
  2521. begin
  2522. LabelEl:=TPasElement(LabelElements[j]);
  2523. if TypeEl=LabelEl then
  2524. exit; // success
  2525. end;
  2526. end
  2527. else if El is TPasAliasType then
  2528. begin
  2529. DeclEl:=TPasAliasType(El).DestType;
  2530. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2531. if (aLabel^.Filename=DeclEl.SourceFilename)
  2532. and (integer(aLabel^.Row)=LabelLine)
  2533. and (aLabel^.StartCol<=LabelCol)
  2534. and (aLabel^.EndCol>=LabelCol) then
  2535. exit; // success
  2536. end
  2537. else if El.ClassType=TPasArgument then
  2538. begin
  2539. TypeEl:=TPasArgument(El).ArgType;
  2540. for j:=0 to LabelElements.Count-1 do
  2541. begin
  2542. LabelEl:=TPasElement(LabelElements[j]);
  2543. if TypeEl=LabelEl then
  2544. exit; // success
  2545. end;
  2546. end;
  2547. end;
  2548. // failed -> show candidates
  2549. writeln('CheckDirectReference failed: Labels:');
  2550. for j:=0 to LabelElements.Count-1 do
  2551. begin
  2552. LabelEl:=TPasElement(LabelElements[j]);
  2553. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2554. end;
  2555. writeln('CheckDirectReference failed: References:');
  2556. for i:=0 to ReferenceElements.Count-1 do
  2557. begin
  2558. El:=TPasElement(ReferenceElements[i]);
  2559. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2560. //if EL is TPasVariable then
  2561. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2562. end;
  2563. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2564. finally
  2565. LabelElements.Free;
  2566. ReferenceElements.Free;
  2567. end;
  2568. end;
  2569. var
  2570. aMarker: PSrcMarker;
  2571. i: Integer;
  2572. SrcLines: TStringList;
  2573. begin
  2574. Module.ForEachCall(@OnCheckElementParent,nil);
  2575. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2576. // find all markers
  2577. for i:=0 to FileResolver.Streams.Count-1 do
  2578. begin
  2579. GetSrc(i,SrcLines,CurFilename);
  2580. ParseCode(SrcLines,CurFilename);
  2581. SrcLines.Free;
  2582. end;
  2583. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2584. // check references
  2585. aMarker:=FirstSrcMarker;
  2586. while aMarker<>nil do
  2587. begin
  2588. case aMarker^.Kind of
  2589. mkResolverReference: CheckResolverReference(aMarker);
  2590. mkDirectReference: CheckDirectReference(aMarker);
  2591. end;
  2592. aMarker:=aMarker^.Next;
  2593. end;
  2594. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2595. end;
  2596. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2597. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2598. var
  2599. i: Integer;
  2600. Item: TTestHintMessage;
  2601. Expected,Actual: string;
  2602. begin
  2603. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2604. for i:=0 to MsgCount-1 do
  2605. begin
  2606. Item:=Msgs[i];
  2607. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2608. if (Marker<>nil) then
  2609. begin
  2610. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2611. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2612. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2613. end;
  2614. // found
  2615. FHintMsgsGood.Add(Item);
  2616. str(Item.MsgType,Actual);
  2617. str(MsgType,Expected);
  2618. AssertEquals('MsgType',Expected,Actual);
  2619. exit;
  2620. end;
  2621. // needed message missing -> show emitted messages
  2622. WriteSources('',0,0);
  2623. for i:=0 to MsgCount-1 do
  2624. begin
  2625. Item:=Msgs[i];
  2626. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2627. ' ('+IntToStr(Item.MsgNumber),')');
  2628. if Marker<>nil then
  2629. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2630. writeln(' {',Item.Msg,'}');
  2631. end;
  2632. str(MsgType,Expected);
  2633. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2634. if Marker<>nil then
  2635. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2636. Actual:=Actual+' '+Msg;
  2637. Fail(Actual);
  2638. end;
  2639. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2640. );
  2641. var
  2642. i: Integer;
  2643. s, Txt: String;
  2644. Msg: TTestHintMessage;
  2645. begin
  2646. for i:=0 to MsgCount-1 do
  2647. begin
  2648. Msg:=Msgs[i];
  2649. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2650. s:='';
  2651. str(Msg.MsgType,s);
  2652. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2653. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2654. if WithSourcePos then
  2655. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2656. Txt:=Txt+' {'+Msg.Msg+'}';
  2657. Fail(Txt);
  2658. end;
  2659. end;
  2660. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2661. MsgNumber: integer);
  2662. begin
  2663. ExpectedErrorClass:=EScannerError;
  2664. ExpectedErrorMsg:=Msg;
  2665. ExpectedErrorNumber:=MsgNumber;
  2666. end;
  2667. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2668. MsgNumber: integer);
  2669. begin
  2670. ExpectedErrorClass:=EParserError;
  2671. ExpectedErrorMsg:=Msg;
  2672. ExpectedErrorNumber:=MsgNumber;
  2673. end;
  2674. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2675. MsgNumber: integer);
  2676. begin
  2677. ExpectedErrorClass:=EPasResolve;
  2678. ExpectedErrorMsg:=Msg;
  2679. ExpectedErrorNumber:=MsgNumber;
  2680. end;
  2681. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2682. MsgNumber: integer);
  2683. begin
  2684. ExpectedErrorClass:=EPas2JS;
  2685. ExpectedErrorMsg:=Msg;
  2686. ExpectedErrorNumber:=MsgNumber;
  2687. end;
  2688. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2689. var
  2690. MsgNumber: Integer;
  2691. Msg: String;
  2692. begin
  2693. Result:=false;
  2694. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2695. Msg:=E.Message;
  2696. if E is EPas2JS then
  2697. MsgNumber:=EPas2JS(E).MsgNumber
  2698. else if E is EPasResolve then
  2699. MsgNumber:=EPasResolve(E).MsgNumber
  2700. else if E is EParserError then
  2701. MsgNumber:=Parser.LastMsgNumber
  2702. else if E is EScannerError then
  2703. begin
  2704. MsgNumber:=Scanner.LastMsgNumber;
  2705. Msg:=Scanner.LastMsg;
  2706. end
  2707. else
  2708. MsgNumber:=0;
  2709. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2710. if Result then
  2711. SkipTests:=true;
  2712. end;
  2713. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2714. const aFilename: string; aRow, aCol: integer);
  2715. var
  2716. s: String;
  2717. begin
  2718. WriteSources(aFilename,aRow,aCol);
  2719. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2720. writeln('ERROR: ',s);
  2721. Fail(s);
  2722. end;
  2723. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2724. aMarker: PSrcMarker);
  2725. begin
  2726. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2727. end;
  2728. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2729. begin
  2730. if IsErrorExpected(E) then exit;
  2731. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2732. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2733. +' '+Scanner.CurFilename
  2734. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2735. FailException(E);
  2736. end;
  2737. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2738. begin
  2739. if IsErrorExpected(E) then exit;
  2740. WriteSources(E.Filename,E.Row,E.Column);
  2741. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2742. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2743. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2744. );
  2745. FailException(E);
  2746. end;
  2747. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2748. var
  2749. P: TPasSourcePos;
  2750. begin
  2751. if IsErrorExpected(E) then exit;
  2752. P:=E.SourcePos;
  2753. WriteSources(P.FileName,P.Row,P.Column);
  2754. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2755. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2756. FailException(E);
  2757. end;
  2758. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2759. var
  2760. Row, Col: integer;
  2761. begin
  2762. if IsErrorExpected(E) then exit;
  2763. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2764. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2765. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2766. +' '+E.PasElement.SourceFilename
  2767. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2768. FailException(E);
  2769. end;
  2770. procedure TCustomTestModule.HandleException(E: Exception);
  2771. begin
  2772. if E is EScannerError then
  2773. HandleScannerError(EScannerError(E))
  2774. else if E is EParserError then
  2775. HandleParserError(EParserError(E))
  2776. else if E is EPasResolve then
  2777. HandlePasResolveError(EPasResolve(E))
  2778. else if E is EPas2JS then
  2779. HandlePas2JSError(EPas2JS(E))
  2780. else
  2781. begin
  2782. if IsErrorExpected(E) then exit;
  2783. if not (E is EAssertionFailedError) then
  2784. begin
  2785. WriteSources('',0,0);
  2786. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2787. end;
  2788. FailException(E);
  2789. end;
  2790. end;
  2791. procedure TCustomTestModule.FailException(E: Exception);
  2792. var
  2793. MsgNumber: Integer;
  2794. begin
  2795. if ExpectedErrorClass<>nil then
  2796. begin
  2797. if FExpectedErrorClass=E.ClassType then
  2798. begin
  2799. if E is EPas2JS then
  2800. MsgNumber:=EPas2JS(E).MsgNumber
  2801. else if E is EPasResolve then
  2802. MsgNumber:=EPasResolve(E).MsgNumber
  2803. else if E is EParserError then
  2804. MsgNumber:=Parser.LastMsgNumber
  2805. else if E is EScannerError then
  2806. MsgNumber:=Scanner.LastMsgNumber
  2807. else
  2808. MsgNumber:=0;
  2809. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2810. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2811. ExpectedErrorNumber,MsgNumber);
  2812. end else begin
  2813. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2814. end;
  2815. end;
  2816. Fail(E.Message);
  2817. end;
  2818. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2819. aCol: integer);
  2820. var
  2821. IsSrc: Boolean;
  2822. i, j: Integer;
  2823. SrcLines: TStringList;
  2824. Line: string;
  2825. aModule: TTestEnginePasResolver;
  2826. begin
  2827. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2828. for i:=0 to ResolverCount-1 do
  2829. begin
  2830. aModule:=Resolvers[i];
  2831. SrcLines:=TStringList.Create;
  2832. try
  2833. SrcLines.Text:=aModule.Source;
  2834. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2835. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2836. for j:=1 to SrcLines.Count do
  2837. begin
  2838. Line:=SrcLines[j-1];
  2839. if IsSrc and (j=aRow) then
  2840. begin
  2841. write('*');
  2842. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2843. end;
  2844. writeln(Format('%:4d: ',[j]),Line);
  2845. end;
  2846. finally
  2847. SrcLines.Free;
  2848. end;
  2849. end;
  2850. end;
  2851. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2852. var
  2853. i: Integer;
  2854. begin
  2855. for i:=0 to ResolverCount-1 do
  2856. if Filename=Resolvers[i].Filename then exit(i);
  2857. Result:=-1;
  2858. end;
  2859. function TCustomTestModule.GetResolver(const Filename: string
  2860. ): TTestEnginePasResolver;
  2861. var
  2862. i: Integer;
  2863. begin
  2864. i:=IndexOfResolver(Filename);
  2865. if i<0 then exit(nil);
  2866. Result:=Resolvers[i];
  2867. end;
  2868. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2869. out aFilename: string);
  2870. var
  2871. aStream: TStream;
  2872. begin
  2873. SrcLines:=TStringList.Create;
  2874. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2875. aStream.Position:=0;
  2876. SrcLines.LoadFromStream(aStream);
  2877. aFilename:=FileResolver.Streams[Index];
  2878. end;
  2879. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2880. aEndCol: integer): TFPList;
  2881. var
  2882. ok: Boolean;
  2883. FoundRefs: TTestResolverReferenceData;
  2884. i: Integer;
  2885. CurResolver: TTestEnginePasResolver;
  2886. begin
  2887. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2888. FoundRefs:=Default(TTestResolverReferenceData);
  2889. FoundRefs.Filename:=aFilename;
  2890. FoundRefs.Row:=aLine;
  2891. FoundRefs.StartCol:=aStartCol;
  2892. FoundRefs.EndCol:=aEndCol;
  2893. FoundRefs.Found:=TFPList.Create;
  2894. ok:=false;
  2895. try
  2896. // find all markers
  2897. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2898. for i:=0 to ResolverCount-1 do
  2899. begin
  2900. CurResolver:=Resolvers[i];
  2901. if CurResolver.Module=Module then continue;
  2902. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2903. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2904. end;
  2905. ok:=true;
  2906. finally
  2907. if not ok then
  2908. FreeAndNil(FoundRefs.Found);
  2909. end;
  2910. Result:=FoundRefs.Found;
  2911. FoundRefs.Found:=nil;
  2912. end;
  2913. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2914. ErrorOnNoElements: boolean): TFPList;
  2915. begin
  2916. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2917. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2918. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2919. end;
  2920. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2921. begin
  2922. Result:=FirstSrcMarker;
  2923. while Result<>nil do
  2924. begin
  2925. if (Result^.Kind=mkLabel)
  2926. and (CompareText(Result^.Identifier,Identifier)=0) then
  2927. exit;
  2928. Result:=Result^.Next;
  2929. end;
  2930. end;
  2931. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2932. ErrorOnNoElements: boolean): TFPList;
  2933. var
  2934. SrcLabel: PSrcMarker;
  2935. begin
  2936. SrcLabel:=FindSrcLabel(Identifier);
  2937. if SrcLabel=nil then
  2938. Fail('missing label "'+Identifier+'"');
  2939. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2940. end;
  2941. function TCustomTestModule.GetDefaultNamespace: string;
  2942. var
  2943. C: TClass;
  2944. begin
  2945. Result:='';
  2946. if FModule=nil then exit;
  2947. C:=FModule.ClassType;
  2948. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2949. Result:=ResolverEngine.DefaultNameSpace;
  2950. end;
  2951. constructor TCustomTestModule.Create;
  2952. begin
  2953. inherited Create;
  2954. FHintMsgs:=TObjectList.Create(true);
  2955. FHintMsgsGood:=TFPList.Create;
  2956. end;
  2957. destructor TCustomTestModule.Destroy;
  2958. begin
  2959. FreeAndNil(FHintMsgs);
  2960. FreeAndNil(FHintMsgsGood);
  2961. inherited Destroy;
  2962. end;
  2963. { TTestModule }
  2964. procedure TTestModule.TestReservedWords;
  2965. var
  2966. i: integer;
  2967. begin
  2968. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2969. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2970. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2971. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2972. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2973. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2974. end;
  2975. procedure TTestModule.TestEmptyProgram;
  2976. begin
  2977. StartProgram(false);
  2978. Add('begin');
  2979. ConvertProgram;
  2980. CheckSource('TestEmptyProgram','','');
  2981. end;
  2982. procedure TTestModule.TestEmptyProgramUseStrict;
  2983. begin
  2984. Converter.Options:=Converter.Options+[coUseStrict];
  2985. StartProgram(false);
  2986. Add('begin');
  2987. ConvertProgram;
  2988. CheckSource('TestEmptyProgramUseStrict','','');
  2989. end;
  2990. procedure TTestModule.TestEmptyUnit;
  2991. begin
  2992. StartUnit(false);
  2993. Add('interface');
  2994. Add('implementation');
  2995. ConvertUnit;
  2996. CheckSource('TestEmptyUnit',
  2997. LinesToStr([
  2998. ]),
  2999. '');
  3000. end;
  3001. procedure TTestModule.TestEmptyUnitUseStrict;
  3002. begin
  3003. Converter.Options:=Converter.Options+[coUseStrict];
  3004. StartUnit(false);
  3005. Add('interface');
  3006. Add('implementation');
  3007. ConvertUnit;
  3008. CheckSource('TestEmptyUnitUseStrict',
  3009. LinesToStr([
  3010. ''
  3011. ]),
  3012. '');
  3013. end;
  3014. procedure TTestModule.TestDottedUnitNames;
  3015. begin
  3016. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3017. LinesToStr([
  3018. 'var iV: longint;'
  3019. ]),
  3020. '');
  3021. FFilename:='ns1.test1.pp';
  3022. StartProgram(true);
  3023. Add('uses unIt2;');
  3024. Add('var');
  3025. Add(' i: longint;');
  3026. Add('begin');
  3027. Add(' i:=iv;');
  3028. Add(' i:=uNit2.iv;');
  3029. Add(' i:=Ns1.TEst1.i;');
  3030. ConvertProgram;
  3031. CheckSource('TestDottedUnitNames',
  3032. LinesToStr([
  3033. 'this.i = 0;',
  3034. '']),
  3035. LinesToStr([ // this.$init
  3036. '$mod.i = pas["NS1.Unit2"].iV;',
  3037. '$mod.i = pas["NS1.Unit2"].iV;',
  3038. '$mod.i = $mod.i;',
  3039. '']) );
  3040. end;
  3041. procedure TTestModule.TestDottedUnitNameImpl;
  3042. begin
  3043. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3044. LinesToStr([
  3045. 'type',
  3046. ' TObject = class end;',
  3047. ' TTestA = class',
  3048. ' end;'
  3049. ]),
  3050. LinesToStr(['uses TEST.UnitB;'])
  3051. );
  3052. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3053. LinesToStr([
  3054. 'uses TEST.UnitA;',
  3055. 'type TTestB = class(TTestA);'
  3056. ]),
  3057. ''
  3058. );
  3059. StartProgram(true);
  3060. Add('uses TEST.UnitA;');
  3061. Add('begin');
  3062. ConvertProgram;
  3063. CheckSource('TestDottedUnitNameImpl',
  3064. LinesToStr([
  3065. '']),
  3066. LinesToStr([ // this.$init
  3067. '']) );
  3068. CheckUnit('TEST.UnitA.pas',
  3069. LinesToStr([
  3070. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3071. ' var $mod = this;',
  3072. ' rtl.createClass(this, "TObject", null, function () {',
  3073. ' this.$init = function () {',
  3074. ' };',
  3075. ' this.$final = function () {',
  3076. ' };',
  3077. ' });',
  3078. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3079. ' });',
  3080. '}, ["TEST.UnitB"]);'
  3081. ]));
  3082. CheckUnit('TEST.UnitB.pas',
  3083. LinesToStr([
  3084. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3085. ' var $mod = this;',
  3086. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3087. ' });',
  3088. '});'
  3089. ]));
  3090. end;
  3091. procedure TTestModule.TestDottedUnitExpr;
  3092. begin
  3093. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3094. LinesToStr([
  3095. 'procedure DoIt;'
  3096. ]),
  3097. 'procedure DoIt; begin end;');
  3098. FFilename:='Ns1.SubNs1.Test1.pp';
  3099. StartProgram(true);
  3100. Add('uses Ns2.sUbnS2.unIt2;');
  3101. Add('var');
  3102. Add(' i: longint;');
  3103. Add('begin');
  3104. Add(' ns2.subns2.unit2.doit;');
  3105. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3106. ConvertProgram;
  3107. CheckSource('TestDottedUnitExpr',
  3108. LinesToStr([
  3109. 'this.i = 0;',
  3110. '']),
  3111. LinesToStr([ // this.$init
  3112. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3113. '$mod.i = $mod.i;',
  3114. '']) );
  3115. end;
  3116. procedure TTestModule.Test_ModeFPCFail;
  3117. begin
  3118. StartProgram(false);
  3119. Add('{$mode FPC}');
  3120. Add('begin');
  3121. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3122. ConvertProgram;
  3123. end;
  3124. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3125. begin
  3126. StartProgram(false);
  3127. Add('{$modeswitch cblocks-}');
  3128. Add('begin');
  3129. ConvertProgram;
  3130. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3131. CheckResolverUnexpectedHints();
  3132. end;
  3133. procedure TTestModule.TestUnit_UseSystem;
  3134. begin
  3135. StartUnit(true);
  3136. Add([
  3137. 'interface',
  3138. 'var i: integer;',
  3139. 'implementation']);
  3140. ConvertUnit;
  3141. CheckSource('TestUnit_UseSystem',
  3142. LinesToStr([
  3143. 'this.i = 0;',
  3144. '']),
  3145. LinesToStr([
  3146. '']) );
  3147. end;
  3148. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3149. begin
  3150. AddModuleWithIntfImplSrc('unit1.pp',
  3151. LinesToStr([
  3152. 'type number = longint;']),
  3153. LinesToStr([
  3154. 'uses test1;',
  3155. 'procedure DoIt;',
  3156. 'begin',
  3157. ' i:=3;',
  3158. 'end;']));
  3159. StartUnit(true);
  3160. Add([
  3161. 'interface',
  3162. 'uses unit1;',
  3163. 'var i: number;',
  3164. 'implementation']);
  3165. ConvertUnit;
  3166. CheckSource('TestUnit_Intf1Impl2Intf1',
  3167. LinesToStr([
  3168. 'this.i = 0;',
  3169. '']),
  3170. LinesToStr([
  3171. '']) );
  3172. end;
  3173. procedure TTestModule.TestIncludeVersion;
  3174. begin
  3175. StartProgram(false);
  3176. Add([
  3177. 'var',
  3178. ' s: string;',
  3179. ' i: word;',
  3180. 'begin',
  3181. ' s:={$I %line%};',
  3182. ' i:={$I %linenum%};',
  3183. ' s:={$I %currentroutine%};',
  3184. ' s:={$I %pas2jsversion%};',
  3185. ' s:={$I %pas2jstarget%};',
  3186. ' s:={$I %pas2jstargetos%};',
  3187. ' s:={$I %pas2jstargetcpu%};',
  3188. ' s:={$I %file%};',
  3189. '']);
  3190. ConvertProgram;
  3191. CheckSource('TestIncludeVersion',
  3192. LinesToStr([
  3193. 'this.s="";',
  3194. 'this.i = 0;']),
  3195. LinesToStr([
  3196. '$mod.s = "7";',
  3197. '$mod.i = 8;',
  3198. '$mod.s = "<anonymous>";',
  3199. '$mod.s = "Comp.Ver.tcmodules";',
  3200. '$mod.s = "Browser";',
  3201. '$mod.s = "Browser";',
  3202. '$mod.s = "ECMAScript5";',
  3203. '$mod.s = "test1.pp";',
  3204. '']));
  3205. end;
  3206. procedure TTestModule.TestVarInt;
  3207. begin
  3208. StartProgram(false);
  3209. Add('var MyI: longint;');
  3210. Add('begin');
  3211. ConvertProgram;
  3212. CheckSource('TestVarInt','this.MyI=0;','');
  3213. end;
  3214. procedure TTestModule.TestVarBaseTypes;
  3215. begin
  3216. StartProgram(false);
  3217. Add('var');
  3218. Add(' i: longint;');
  3219. Add(' s: string;');
  3220. Add(' c: char;');
  3221. Add(' b: boolean;');
  3222. Add(' d: double;');
  3223. Add(' i2: longint = 3;');
  3224. Add(' s2: string = ''foo'';');
  3225. Add(' c2: char = ''4'';');
  3226. Add(' b2: boolean = true;');
  3227. Add(' d2: double = 5.6;');
  3228. Add(' i3: longint = $707;');
  3229. Add(' i4: nativeint = 9007199254740991;');
  3230. Add(' i5: nativeint = -9007199254740991-1;');
  3231. Add(' i6: nativeint = $fffffffffffff;');
  3232. Add(' i7: nativeint = -$fffffffffffff-1;');
  3233. Add(' i8: byte = 00;');
  3234. Add(' u8: nativeuint = $fffffffffffff;');
  3235. Add(' u9: nativeuint = $0000000000000;');
  3236. Add(' u10: nativeuint = $00ff00;');
  3237. Add('begin');
  3238. ConvertProgram;
  3239. CheckSource('TestVarBaseTypes',
  3240. LinesToStr([
  3241. 'this.i = 0;',
  3242. 'this.s = "";',
  3243. 'this.c = "";',
  3244. 'this.b = false;',
  3245. 'this.d = 0.0;',
  3246. 'this.i2 = 3;',
  3247. 'this.s2 = "foo";',
  3248. 'this.c2 = "4";',
  3249. 'this.b2 = true;',
  3250. 'this.d2 = 5.6;',
  3251. 'this.i3 = 0x707;',
  3252. 'this.i4 = 9007199254740991;',
  3253. 'this.i5 = -9007199254740991-1;',
  3254. 'this.i6 = 0xfffffffffffff;',
  3255. 'this.i7 =-0xfffffffffffff-1;',
  3256. 'this.i8 = 0;',
  3257. 'this.u8 = 0xfffffffffffff;',
  3258. 'this.u9 = 0x0;',
  3259. 'this.u10 = 0xff00;'
  3260. ]),
  3261. '');
  3262. end;
  3263. procedure TTestModule.TestBaseTypeSingleFail;
  3264. begin
  3265. StartProgram(false);
  3266. Add('var s: single;');
  3267. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3268. ConvertProgram;
  3269. end;
  3270. procedure TTestModule.TestBaseTypeExtendedFail;
  3271. begin
  3272. StartProgram(false);
  3273. Add('var e: extended;');
  3274. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3275. ConvertProgram;
  3276. end;
  3277. procedure TTestModule.TestConstBaseTypes;
  3278. begin
  3279. StartProgram(false);
  3280. Add('const');
  3281. Add(' i: longint = 3;');
  3282. Add(' s: string = ''foo'';');
  3283. Add(' c: char = ''4'';');
  3284. Add(' b: boolean = true;');
  3285. Add(' d: double = 5.6;');
  3286. Add(' e = low(word);');
  3287. Add(' f = high(word);');
  3288. Add('begin');
  3289. ConvertProgram;
  3290. CheckSource('TestVarBaseTypes',
  3291. LinesToStr([
  3292. 'this.i=3;',
  3293. 'this.s="foo";',
  3294. 'this.c="4";',
  3295. 'this.b=true;',
  3296. 'this.d=5.6;',
  3297. 'this.e = 0;',
  3298. 'this.f = 65535;'
  3299. ]),
  3300. '');
  3301. end;
  3302. procedure TTestModule.TestAliasTypeRef;
  3303. begin
  3304. StartProgram(false);
  3305. Add('type');
  3306. Add(' a=longint;');
  3307. Add(' b=a;');
  3308. Add('var');
  3309. Add(' c: A;');
  3310. Add(' d: B;');
  3311. Add('begin');
  3312. ConvertProgram;
  3313. CheckSource('TestAliasTypeRef',
  3314. LinesToStr([ // statements
  3315. 'this.c = 0;',
  3316. 'this.d = 0;'
  3317. ]),
  3318. LinesToStr([ // this.$main
  3319. ''
  3320. ]));
  3321. end;
  3322. procedure TTestModule.TestTypeCast_BaseTypes;
  3323. begin
  3324. StartProgram(false);
  3325. Add([
  3326. 'var',
  3327. ' i: longint;',
  3328. ' b: boolean;',
  3329. ' d: double;',
  3330. ' s: string;',
  3331. ' c: char;',
  3332. 'begin',
  3333. ' i:=longint(i);',
  3334. ' i:=longint(b);',
  3335. ' b:=boolean(b);',
  3336. ' b:=boolean(i);',
  3337. ' d:=double(d);',
  3338. ' d:=double(i);',
  3339. ' s:=string(s);',
  3340. ' s:=string(c);',
  3341. ' c:=char(c);',
  3342. ' c:=char(i);',
  3343. ' c:=char(65);',
  3344. ' c:=char(#10);',
  3345. ' c:=char(#$E000);',
  3346. '']);
  3347. ConvertProgram;
  3348. CheckSource('TestAliasTypeRef',
  3349. LinesToStr([ // statements
  3350. 'this.i = 0;',
  3351. 'this.b = false;',
  3352. 'this.d = 0.0;',
  3353. 'this.s = "";',
  3354. 'this.c = "";',
  3355. '']),
  3356. LinesToStr([ // this.$main
  3357. '$mod.i = $mod.i;',
  3358. '$mod.i = ($mod.b ? 1 : 0);',
  3359. '$mod.b = $mod.b;',
  3360. '$mod.b = $mod.i != 0;',
  3361. '$mod.d = $mod.d;',
  3362. '$mod.d = $mod.i;',
  3363. '$mod.s = $mod.s;',
  3364. '$mod.s = $mod.c;',
  3365. '$mod.c = $mod.c;',
  3366. '$mod.c = String.fromCharCode($mod.i);',
  3367. '$mod.c = "A";',
  3368. '$mod.c = "\n";',
  3369. '$mod.c = "";',
  3370. '']));
  3371. end;
  3372. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3373. begin
  3374. StartProgram(false);
  3375. Add('type');
  3376. Add(' integer = longint;');
  3377. Add(' TYesNo = boolean;');
  3378. Add(' TFloat = double;');
  3379. Add(' TCaption = string;');
  3380. Add(' TChar = char;');
  3381. Add('var');
  3382. Add(' i: integer;');
  3383. Add(' b: TYesNo;');
  3384. Add(' d: TFloat;');
  3385. Add(' s: TCaption;');
  3386. Add(' c: TChar;');
  3387. Add('begin');
  3388. Add(' i:=integer(i);');
  3389. Add(' i:=integer(b);');
  3390. Add(' b:=TYesNo(b);');
  3391. Add(' b:=TYesNo(i);');
  3392. Add(' d:=TFloat(d);');
  3393. Add(' d:=TFloat(i);');
  3394. Add(' s:=TCaption(s);');
  3395. Add(' s:=TCaption(c);');
  3396. Add(' c:=TChar(c);');
  3397. ConvertProgram;
  3398. CheckSource('TestAliasTypeRef',
  3399. LinesToStr([ // statements
  3400. 'this.i = 0;',
  3401. 'this.b = false;',
  3402. 'this.d = 0.0;',
  3403. 'this.s = "";',
  3404. 'this.c = "";',
  3405. '']),
  3406. LinesToStr([ // this.$main
  3407. '$mod.i = $mod.i;',
  3408. '$mod.i = ($mod.b ? 1 : 0);',
  3409. '$mod.b = $mod.b;',
  3410. '$mod.b = $mod.i != 0;',
  3411. '$mod.d = $mod.d;',
  3412. '$mod.d = $mod.i;',
  3413. '$mod.s = $mod.s;',
  3414. '$mod.s = $mod.c;',
  3415. '$mod.c = $mod.c;',
  3416. '']));
  3417. end;
  3418. procedure TTestModule.TestEmptyProc;
  3419. begin
  3420. StartProgram(false);
  3421. Add('procedure Test;');
  3422. Add('begin');
  3423. Add('end;');
  3424. Add('begin');
  3425. ConvertProgram;
  3426. CheckSource('TestEmptyProc',
  3427. LinesToStr([ // statements
  3428. 'this.Test = function () {',
  3429. '};'
  3430. ]),
  3431. LinesToStr([ // this.$main
  3432. ''
  3433. ]));
  3434. end;
  3435. procedure TTestModule.TestProcOneParam;
  3436. begin
  3437. StartProgram(false);
  3438. Add('procedure ProcA(i: longint);');
  3439. Add('begin');
  3440. Add('end;');
  3441. Add('begin');
  3442. Add(' PROCA(3);');
  3443. ConvertProgram;
  3444. CheckSource('TestProcOneParam',
  3445. LinesToStr([ // statements
  3446. 'this.ProcA = function (i) {',
  3447. '};'
  3448. ]),
  3449. LinesToStr([ // this.$main
  3450. '$mod.ProcA(3);'
  3451. ]));
  3452. end;
  3453. procedure TTestModule.TestFunctionWithoutParams;
  3454. begin
  3455. StartProgram(false);
  3456. Add('function FuncA: longint;');
  3457. Add('begin');
  3458. Add('end;');
  3459. Add('var i: longint;');
  3460. Add('begin');
  3461. Add(' I:=FUNCA();');
  3462. Add(' I:=FUNCA;');
  3463. Add(' FUNCA();');
  3464. Add(' FUNCA;');
  3465. ConvertProgram;
  3466. CheckSource('TestProcWithoutParams',
  3467. LinesToStr([ // statements
  3468. 'this.FuncA = function () {',
  3469. ' var Result = 0;',
  3470. ' return Result;',
  3471. '};',
  3472. 'this.i=0;'
  3473. ]),
  3474. LinesToStr([ // this.$main
  3475. '$mod.i=$mod.FuncA();',
  3476. '$mod.i=$mod.FuncA();',
  3477. '$mod.FuncA();',
  3478. '$mod.FuncA();'
  3479. ]));
  3480. end;
  3481. procedure TTestModule.TestProcedureWithoutParams;
  3482. begin
  3483. StartProgram(false);
  3484. Add('procedure ProcA;');
  3485. Add('begin');
  3486. Add('end;');
  3487. Add('begin');
  3488. Add(' PROCA();');
  3489. Add(' PROCA;');
  3490. ConvertProgram;
  3491. CheckSource('TestProcWithoutParams',
  3492. LinesToStr([ // statements
  3493. 'this.ProcA = function () {',
  3494. '};'
  3495. ]),
  3496. LinesToStr([ // this.$main
  3497. '$mod.ProcA();',
  3498. '$mod.ProcA();'
  3499. ]));
  3500. end;
  3501. procedure TTestModule.TestIncDec;
  3502. begin
  3503. StartProgram(false);
  3504. Add([
  3505. 'procedure DoIt(var i: longint);',
  3506. 'begin',
  3507. ' inc(i);',
  3508. ' inc(i,2);',
  3509. 'end;',
  3510. 'var',
  3511. ' Bar: longint;',
  3512. 'begin',
  3513. ' inc(bar);',
  3514. ' inc(bar,2);',
  3515. ' dec(bar);',
  3516. ' dec(bar,3);',
  3517. '']);
  3518. ConvertProgram;
  3519. CheckSource('TestIncDec',
  3520. LinesToStr([ // statements
  3521. 'this.DoIt = function (i) {',
  3522. ' i.set(i.get()+1);',
  3523. ' i.set(i.get()+2);',
  3524. '};',
  3525. 'this.Bar = 0;'
  3526. ]),
  3527. LinesToStr([ // this.$main
  3528. '$mod.Bar+=1;',
  3529. '$mod.Bar+=2;',
  3530. '$mod.Bar-=1;',
  3531. '$mod.Bar-=3;'
  3532. ]));
  3533. end;
  3534. procedure TTestModule.TestLoHiFpcMode;
  3535. begin
  3536. StartProgram(false);
  3537. Add([
  3538. '{$mode objfpc}',
  3539. 'const',
  3540. ' LoByte1 = Lo(Word($1234));',
  3541. ' HiByte1 = Hi(Word($1234));',
  3542. ' LoByte2 = Lo(SmallInt($1234));',
  3543. ' HiByte2 = Hi(SmallInt($1234));',
  3544. ' LoWord1 = Lo($1234CDEF);',
  3545. ' HiWord1 = Hi($1234CDEF);',
  3546. ' LoWord2 = Lo(-$1234CDEF);',
  3547. ' HiWord2 = Hi(-$1234CDEF);',
  3548. ' lo4:byte=lo(byte($34));',
  3549. ' hi4:byte=hi(byte($34));',
  3550. ' lo5:byte=lo(shortint(-$34));',
  3551. ' hi5:byte=hi(shortint(-$34));',
  3552. ' lo6:longword=lo($123456789ABCD);',
  3553. ' hi6:longword=hi($123456789ABCD);',
  3554. ' lo7:longword=lo(-$123456789ABCD);',
  3555. ' hi7:longword=hi(-$123456789ABCD);',
  3556. 'var',
  3557. ' b: Byte;',
  3558. ' ss: shortint;',
  3559. ' w: Word;',
  3560. ' si: SmallInt;',
  3561. ' lw: LongWord;',
  3562. ' li: LongInt;',
  3563. ' b2: Byte;',
  3564. ' ni: nativeint;',
  3565. 'begin',
  3566. ' w := $1234;',
  3567. ' ss := -$12;',
  3568. ' b := lo(ss);',
  3569. ' b := HI(ss);',
  3570. ' b := lo(w);',
  3571. ' b := HI(w);',
  3572. ' b2 := lo(b);',
  3573. ' b2 := hi(b);',
  3574. ' lw := $1234CDEF;',
  3575. ' w := lo(lw);',
  3576. ' w := hi(lw);',
  3577. ' ni := $123456789ABCD;',
  3578. ' lw := lo(ni);',
  3579. ' lw := hi(ni);',
  3580. '']);
  3581. ConvertProgram;
  3582. CheckSource('TestLoHiFpcMode',
  3583. LinesToStr([ // statements
  3584. 'this.LoByte1 = 0x1234 & 0xFF;',
  3585. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3586. 'this.LoByte2 = 0x1234 & 0xFF;',
  3587. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3588. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3589. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3590. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3591. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3592. 'this.lo4 = 0x34 & 0xF;',
  3593. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3594. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3595. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3596. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3597. 'this.hi6 = 74565 >>> 0;',
  3598. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3599. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3600. 'this.b = 0;',
  3601. 'this.ss = 0;',
  3602. 'this.w = 0;',
  3603. 'this.si = 0;',
  3604. 'this.lw = 0;',
  3605. 'this.li = 0;',
  3606. 'this.b2 = 0;',
  3607. 'this.ni = 0;',
  3608. '']),
  3609. LinesToStr([ // this.$main
  3610. '$mod.w = 0x1234;',
  3611. '$mod.ss = -0x12;',
  3612. '$mod.b = $mod.ss & 0xFF;',
  3613. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3614. '$mod.b = $mod.w & 0xFF;',
  3615. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3616. '$mod.b2 = $mod.b & 0xF;',
  3617. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3618. '$mod.lw = 0x1234CDEF;',
  3619. '$mod.w = $mod.lw & 0xFFFF;',
  3620. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3621. '$mod.ni = 0x123456789ABCD;',
  3622. '$mod.lw = $mod.ni >>> 0;',
  3623. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3624. '']));
  3625. end;
  3626. procedure TTestModule.TestLoHiDelphiMode;
  3627. begin
  3628. StartProgram(false);
  3629. Add([
  3630. '{$mode delphi}',
  3631. 'const',
  3632. ' LoByte1 = Lo(Word($1234));',
  3633. ' HiByte1 = Hi(Word($1234));',
  3634. ' LoByte2 = Lo(SmallInt($1234));',
  3635. ' HiByte2 = Hi(SmallInt($1234));',
  3636. ' LoByte3 = Lo($1234CDEF);',
  3637. ' HiByte3 = Hi($1234CDEF);',
  3638. ' LoByte4 = Lo(-$1234CDEF);',
  3639. ' HiByte4 = Hi(-$1234CDEF);',
  3640. 'var',
  3641. ' b: Byte;',
  3642. ' w: Word;',
  3643. ' si: SmallInt;',
  3644. ' lw: LongWord;',
  3645. ' li: LongInt;',
  3646. 'begin',
  3647. ' w := $1234;',
  3648. ' b := lo(w);',
  3649. ' b := HI(w);',
  3650. ' lw := $1234CDEF;',
  3651. ' b := lo(lw);',
  3652. ' b := hi(lw);',
  3653. '']);
  3654. ConvertProgram;
  3655. CheckSource('TestLoHiDelphiMode',
  3656. LinesToStr([ // statements
  3657. 'this.LoByte1 = 0x1234 & 0xFF;',
  3658. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3659. 'this.LoByte2 = 0x1234 & 0xFF;',
  3660. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3661. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3662. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3663. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3664. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3665. 'this.b = 0;',
  3666. 'this.w = 0;',
  3667. 'this.si = 0;',
  3668. 'this.lw = 0;',
  3669. 'this.li = 0;'
  3670. ]),
  3671. LinesToStr([ // this.$main
  3672. '$mod.w = 0x1234;',
  3673. '$mod.b = $mod.w & 0xFF;',
  3674. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3675. '$mod.lw = 0x1234CDEF;',
  3676. '$mod.b = $mod.lw & 0xFF;',
  3677. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3678. ]));
  3679. end;
  3680. procedure TTestModule.TestAssignments;
  3681. begin
  3682. StartProgram(false);
  3683. Parser.Options:=Parser.Options+[po_cassignments];
  3684. Add('var');
  3685. Add(' Bar:longint;');
  3686. Add('begin');
  3687. Add(' bar:=3;');
  3688. Add(' bar+=4;');
  3689. Add(' bar-=5;');
  3690. Add(' bar*=6;');
  3691. ConvertProgram;
  3692. CheckSource('TestAssignments',
  3693. LinesToStr([ // statements
  3694. 'this.Bar = 0;'
  3695. ]),
  3696. LinesToStr([ // this.$main
  3697. '$mod.Bar=3;',
  3698. '$mod.Bar+=4;',
  3699. '$mod.Bar-=5;',
  3700. '$mod.Bar*=6;'
  3701. ]));
  3702. end;
  3703. procedure TTestModule.TestArithmeticOperators1;
  3704. begin
  3705. StartProgram(false);
  3706. Add('var');
  3707. Add(' vA,vB,vC:longint;');
  3708. Add('begin');
  3709. Add(' va:=1;');
  3710. Add(' vb:=va+va;');
  3711. Add(' vb:=va div vb;');
  3712. Add(' vb:=va mod vb;');
  3713. Add(' vb:=va+va*vb+va div vb;');
  3714. Add(' vc:=-va;');
  3715. Add(' va:=va-vb;');
  3716. Add(' vb:=va;');
  3717. Add(' if va<vb then vc:=va else vc:=vb;');
  3718. ConvertProgram;
  3719. CheckSource('TestArithmeticOperators1',
  3720. LinesToStr([ // statements
  3721. 'this.vA = 0;',
  3722. 'this.vB = 0;',
  3723. 'this.vC = 0;'
  3724. ]),
  3725. LinesToStr([ // this.$main
  3726. '$mod.vA = 1;',
  3727. '$mod.vB = $mod.vA + $mod.vA;',
  3728. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3729. '$mod.vB = $mod.vA % $mod.vB;',
  3730. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3731. '$mod.vC = -$mod.vA;',
  3732. '$mod.vA = $mod.vA - $mod.vB;',
  3733. '$mod.vB = $mod.vA;',
  3734. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3735. ]));
  3736. end;
  3737. procedure TTestModule.TestMultiAdd;
  3738. begin
  3739. StartProgram(false);
  3740. Add([
  3741. 'function Fly: string; external name ''fly'';',
  3742. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3743. 'var',
  3744. ' Date: double;',
  3745. 'begin',
  3746. ' Result:=(Year>0) and (Year<10000) and',
  3747. ' (Month >= 1) and (Month<=12) and',
  3748. ' (Day>0) and (Day<=31);',
  3749. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3750. 'end;',
  3751. 'var s: string;',
  3752. 'begin',
  3753. ' s:=''a''+''b''+''c''+''d'';',
  3754. ' s:=s+Fly+''e'';',
  3755. ' s:=Fly+Fly+Fly;',
  3756. '']);
  3757. ConvertProgram;
  3758. CheckSource('TestMultiAdd',
  3759. LinesToStr([ // statements
  3760. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3761. ' var Result = false;',
  3762. ' var date = 0.0;',
  3763. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3764. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3765. ' return Result;',
  3766. '};',
  3767. 'this.s = "";',
  3768. '']),
  3769. LinesToStr([ // this.$main
  3770. '$mod.s = "a" + "b" + "c" + "d";',
  3771. '$mod.s = $mod.s + fly() + "e";',
  3772. '$mod.s = fly() + fly() + fly();',
  3773. '']));
  3774. end;
  3775. procedure TTestModule.TestLogicalOperators;
  3776. begin
  3777. StartProgram(false);
  3778. Add('var');
  3779. Add(' vA,vB,vC:boolean;');
  3780. Add('begin');
  3781. Add(' va:=vb and vc;');
  3782. Add(' va:=vb or vc;');
  3783. Add(' va:=vb xor vc;');
  3784. Add(' va:=true and vc;');
  3785. Add(' va:=(vb and vc) or (va and vb);');
  3786. Add(' va:=not vb;');
  3787. ConvertProgram;
  3788. CheckSource('TestLogicalOperators',
  3789. LinesToStr([ // statements
  3790. 'this.vA = false;',
  3791. 'this.vB = false;',
  3792. 'this.vC = false;'
  3793. ]),
  3794. LinesToStr([ // this.$main
  3795. '$mod.vA = $mod.vB && $mod.vC;',
  3796. '$mod.vA = $mod.vB || $mod.vC;',
  3797. '$mod.vA = $mod.vB ^ $mod.vC;',
  3798. '$mod.vA = true && $mod.vC;',
  3799. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3800. '$mod.vA = !$mod.vB;'
  3801. ]));
  3802. end;
  3803. procedure TTestModule.TestBitwiseOperators;
  3804. begin
  3805. StartProgram(false);
  3806. Add([
  3807. 'var',
  3808. ' vA,vB,vC:longint;',
  3809. ' X,Y,Z: nativeint;',
  3810. 'begin',
  3811. ' va:=vb and vc;',
  3812. ' va:=vb or vc;',
  3813. ' va:=vb xor vc;',
  3814. ' va:=vb shl vc;',
  3815. ' va:=vb shr vc;',
  3816. ' va:=3 and vc;',
  3817. ' va:=(vb and vc) or (va and vb);',
  3818. ' va:=not vb;',
  3819. ' X:=Y and Z;',
  3820. ' X:=Y and va;',
  3821. ' X:=Y or Z;',
  3822. ' X:=Y or va;',
  3823. ' X:=Y xor Z;',
  3824. ' X:=Y xor va;',
  3825. '']);
  3826. ConvertProgram;
  3827. CheckSource('TestBitwiseOperators',
  3828. LinesToStr([ // statements
  3829. 'this.vA = 0;',
  3830. 'this.vB = 0;',
  3831. 'this.vC = 0;',
  3832. 'this.X = 0;',
  3833. 'this.Y = 0;',
  3834. 'this.Z = 0;',
  3835. '']),
  3836. LinesToStr([ // this.$main
  3837. '$mod.vA = $mod.vB & $mod.vC;',
  3838. '$mod.vA = $mod.vB | $mod.vC;',
  3839. '$mod.vA = $mod.vB ^ $mod.vC;',
  3840. '$mod.vA = $mod.vB << $mod.vC;',
  3841. '$mod.vA = $mod.vB >>> $mod.vC;',
  3842. '$mod.vA = 3 & $mod.vC;',
  3843. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3844. '$mod.vA = ~$mod.vB;',
  3845. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3846. '$mod.X = $mod.Y & $mod.vA;',
  3847. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3848. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3849. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3850. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3851. '']));
  3852. end;
  3853. procedure TTestModule.TestBitwiseOperatorsLongword;
  3854. begin
  3855. StartProgram(false);
  3856. Add([
  3857. 'var',
  3858. ' a,b,c:longword;',
  3859. ' i: longint;',
  3860. 'begin',
  3861. ' a:=$12345678;',
  3862. ' b:=$EDCBA987;',
  3863. ' c:=not a;',
  3864. ' c:=a and b;',
  3865. ' c:=a and $ffff0000;',
  3866. ' c:=a or b;',
  3867. ' c:=a or $ff00ff00;',
  3868. ' c:=a xor b;',
  3869. ' c:=a xor $f0f0f0f0;',
  3870. ' c:=a shl 1;',
  3871. ' c:=a shl 16;',
  3872. ' c:=a shl 24;',
  3873. ' c:=a shl b;',
  3874. ' c:=a shr 1;',
  3875. ' c:=a shr 16;',
  3876. ' c:=a shr 24;',
  3877. ' c:=a shr b;',
  3878. ' c:=(b and c) or (a and b);',
  3879. ' c:=i and a;',
  3880. ' c:=i or a;',
  3881. ' c:=i xor a;',
  3882. '']);
  3883. ConvertProgram;
  3884. CheckSource('TestBitwiseOperatorsLongword',
  3885. LinesToStr([ // statements
  3886. 'this.a = 0;',
  3887. 'this.b = 0;',
  3888. 'this.c = 0;',
  3889. 'this.i = 0;',
  3890. '']),
  3891. LinesToStr([ // this.$main
  3892. '$mod.a = 0x12345678;',
  3893. '$mod.b = 0xEDCBA987;',
  3894. '$mod.c = rtl.lw(~$mod.a);',
  3895. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3896. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3897. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3898. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3899. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3900. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3901. '$mod.c = rtl.lw($mod.a << 1);',
  3902. '$mod.c = rtl.lw($mod.a << 16);',
  3903. '$mod.c = rtl.lw($mod.a << 24);',
  3904. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3905. '$mod.c = rtl.lw($mod.a >>> 1);',
  3906. '$mod.c = rtl.lw($mod.a >>> 16);',
  3907. '$mod.c = rtl.lw($mod.a >>> 24);',
  3908. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3909. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3910. '$mod.c = $mod.i & $mod.a;',
  3911. '$mod.c = $mod.i | $mod.a;',
  3912. '$mod.c = $mod.i ^ $mod.a;',
  3913. '']));
  3914. end;
  3915. procedure TTestModule.TestPrgProcVar;
  3916. begin
  3917. StartProgram(false);
  3918. Add('procedure Proc1;');
  3919. Add('type');
  3920. Add(' t1=longint;');
  3921. Add('var');
  3922. Add(' vA:t1;');
  3923. Add('begin');
  3924. Add('end;');
  3925. Add('begin');
  3926. ConvertProgram;
  3927. CheckSource('TestPrgProcVar',
  3928. LinesToStr([ // statements
  3929. 'this.Proc1 = function () {',
  3930. ' var vA=0;',
  3931. '};'
  3932. ]),
  3933. LinesToStr([ // this.$main
  3934. ''
  3935. ]));
  3936. end;
  3937. procedure TTestModule.TestUnitProcVar;
  3938. begin
  3939. StartUnit(false);
  3940. Add('interface');
  3941. Add('');
  3942. Add('type tA=string; // unit scope');
  3943. Add('procedure Proc1;');
  3944. Add('');
  3945. Add('implementation');
  3946. Add('');
  3947. Add('procedure Proc1;');
  3948. Add('type tA=longint; // local proc scope');
  3949. Add('var v1:tA; // using local tA');
  3950. Add('begin');
  3951. Add('end;');
  3952. Add('var v2:tA; // using interface tA');
  3953. ConvertUnit;
  3954. CheckSource('TestUnitProcVar',
  3955. LinesToStr([ // statements
  3956. 'var $impl = $mod.$impl;',
  3957. 'this.Proc1 = function () {',
  3958. ' var v1 = 0;',
  3959. '};',
  3960. '']),
  3961. // this.$init
  3962. '',
  3963. // implementation
  3964. LinesToStr([
  3965. '$impl.v2 = "";',
  3966. '']));
  3967. end;
  3968. procedure TTestModule.TestImplProc;
  3969. begin
  3970. StartUnit(false);
  3971. Add('interface');
  3972. Add('');
  3973. Add('procedure Proc1;');
  3974. Add('');
  3975. Add('implementation');
  3976. Add('');
  3977. Add('procedure Proc1; begin end;');
  3978. Add('procedure Proc2; begin end;');
  3979. Add('initialization');
  3980. Add(' Proc1;');
  3981. Add(' Proc2;');
  3982. ConvertUnit;
  3983. CheckSource('TestImplProc',
  3984. LinesToStr([ // statements
  3985. 'var $impl = $mod.$impl;',
  3986. 'this.Proc1 = function () {',
  3987. '};',
  3988. '']),
  3989. LinesToStr([ // this.$init
  3990. '$mod.Proc1();',
  3991. '$impl.Proc2();',
  3992. '']),
  3993. LinesToStr([ // implementation
  3994. '$impl.Proc2 = function () {',
  3995. '};',
  3996. ''])
  3997. );
  3998. end;
  3999. procedure TTestModule.TestFunctionResult;
  4000. begin
  4001. StartProgram(false);
  4002. Add('function Func1: longint;');
  4003. Add('begin');
  4004. Add(' Result:=3;');
  4005. Add(' Func1:=4;');
  4006. Add('end;');
  4007. Add('begin');
  4008. ConvertProgram;
  4009. CheckSource('TestFunctionResult',
  4010. LinesToStr([ // statements
  4011. 'this.Func1 = function () {',
  4012. ' var Result = 0;',
  4013. ' Result = 3;',
  4014. ' Result = 4;',
  4015. ' return Result;',
  4016. '};'
  4017. ]),
  4018. '');
  4019. end;
  4020. procedure TTestModule.TestNestedProc;
  4021. begin
  4022. StartProgram(false);
  4023. Add([
  4024. 'var vInUnit: longint;',
  4025. 'function DoIt(pA,pD: longint): longint;',
  4026. 'var',
  4027. ' vB: longint;',
  4028. ' vC: longint;',
  4029. ' function Nesty(pA: longint): longint; ',
  4030. ' var vB: longint;',
  4031. ' begin',
  4032. ' Result:=pa+vb+vc+pd+vInUnit;',
  4033. ' nesty:=3;',
  4034. ' doit:=4;',
  4035. ' exit;',
  4036. ' end;',
  4037. 'begin',
  4038. ' Result:=pa+vb+vc;',
  4039. ' doit:=6;',
  4040. ' exit;',
  4041. 'end;',
  4042. 'begin']);
  4043. ConvertProgram;
  4044. CheckSource('TestNestedProc',
  4045. LinesToStr([ // statements
  4046. 'this.vInUnit = 0;',
  4047. 'this.DoIt = function (pA, pD) {',
  4048. ' var Result = 0;',
  4049. ' var vB = 0;',
  4050. ' var vC = 0;',
  4051. ' function Nesty(pA) {',
  4052. ' var Result$1 = 0;',
  4053. ' var vB = 0;',
  4054. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4055. ' Result$1 = 3;',
  4056. ' Result = 4;',
  4057. ' return Result$1;',
  4058. ' return Result$1;',
  4059. ' };',
  4060. ' Result = pA + vB + vC;',
  4061. ' Result = 6;',
  4062. ' return Result;',
  4063. ' return Result;',
  4064. '};'
  4065. ]),
  4066. '');
  4067. end;
  4068. procedure TTestModule.TestNestedProc_ResultString;
  4069. begin
  4070. StartProgram(false);
  4071. Add([
  4072. 'function DoIt: string;',
  4073. ' function Nesty: string; ',
  4074. ' begin',
  4075. ' nesty:=#65#66;',
  4076. ' nesty[1]:=#67;',
  4077. ' doit:=#68;',
  4078. ' doit[2]:=#69;',
  4079. ' end;',
  4080. 'begin',
  4081. ' doit:=#70;',
  4082. ' doit[3]:=#71;',
  4083. 'end;',
  4084. 'begin']);
  4085. ConvertProgram;
  4086. CheckSource('TestNestedProc_ResultString',
  4087. LinesToStr([ // statements
  4088. 'this.DoIt = function () {',
  4089. ' var Result = "";',
  4090. ' function Nesty() {',
  4091. ' var Result$1 = "";',
  4092. ' Result$1 = "AB";',
  4093. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4094. ' Result = "D";',
  4095. ' Result = rtl.setCharAt(Result, 1, "E");',
  4096. ' return Result$1;',
  4097. ' };',
  4098. ' Result = "F";',
  4099. ' Result = rtl.setCharAt(Result, 2, "G");',
  4100. ' return Result;',
  4101. '};'
  4102. ]),
  4103. '');
  4104. end;
  4105. procedure TTestModule.TestForwardProc;
  4106. begin
  4107. StartProgram(false);
  4108. Add('procedure FuncA(Bar: longint); forward;');
  4109. Add('procedure FuncB(Bar: longint);');
  4110. Add('begin');
  4111. Add(' funca(bar);');
  4112. Add('end;');
  4113. Add('procedure funca(bar: longint);');
  4114. Add('begin');
  4115. Add(' if bar=3 then ;');
  4116. Add('end;');
  4117. Add('begin');
  4118. Add(' funca(4);');
  4119. Add(' funcb(5);');
  4120. ConvertProgram;
  4121. CheckSource('TestForwardProc',
  4122. LinesToStr([ // statements'
  4123. 'this.FuncB = function (Bar) {',
  4124. ' $mod.FuncA(Bar);',
  4125. '};',
  4126. 'this.FuncA = function (Bar) {',
  4127. ' if (Bar === 3);',
  4128. '};'
  4129. ]),
  4130. LinesToStr([
  4131. '$mod.FuncA(4);',
  4132. '$mod.FuncB(5);'
  4133. ])
  4134. );
  4135. end;
  4136. procedure TTestModule.TestNestedForwardProc;
  4137. begin
  4138. StartProgram(false);
  4139. Add('procedure FuncA;');
  4140. Add(' procedure FuncB(i: longint); forward;');
  4141. Add(' procedure FuncC(i: longint);');
  4142. Add(' begin');
  4143. Add(' funcb(i);');
  4144. Add(' end;');
  4145. Add(' procedure FuncB(i: longint);');
  4146. Add(' begin');
  4147. Add(' if i=3 then ;');
  4148. Add(' end;');
  4149. Add('begin');
  4150. Add(' funcc(4)');
  4151. Add('end;');
  4152. Add('begin');
  4153. Add(' funca;');
  4154. ConvertProgram;
  4155. CheckSource('TestNestedForwardProc',
  4156. LinesToStr([ // statements'
  4157. 'this.FuncA = function () {',
  4158. ' function FuncC(i) {',
  4159. ' FuncB(i);',
  4160. ' };',
  4161. ' function FuncB(i) {',
  4162. ' if (i === 3);',
  4163. ' };',
  4164. ' FuncC(4);',
  4165. '};'
  4166. ]),
  4167. LinesToStr([
  4168. '$mod.FuncA();'
  4169. ])
  4170. );
  4171. end;
  4172. procedure TTestModule.TestAssignFunctionResult;
  4173. begin
  4174. StartProgram(false);
  4175. Add('function Func1: longint;');
  4176. Add('begin');
  4177. Add('end;');
  4178. Add('var i: longint;');
  4179. Add('begin');
  4180. Add(' i:=func1();');
  4181. Add(' i:=func1()+func1();');
  4182. ConvertProgram;
  4183. CheckSource('TestAssignFunctionResult',
  4184. LinesToStr([ // statements
  4185. 'this.Func1 = function () {',
  4186. ' var Result = 0;',
  4187. ' return Result;',
  4188. '};',
  4189. 'this.i = 0;'
  4190. ]),
  4191. LinesToStr([
  4192. '$mod.i = $mod.Func1();',
  4193. '$mod.i = $mod.Func1() + $mod.Func1();'
  4194. ]));
  4195. end;
  4196. procedure TTestModule.TestFunctionResultInCondition;
  4197. begin
  4198. StartProgram(false);
  4199. Add('function Func1: longint;');
  4200. Add('begin');
  4201. Add('end;');
  4202. Add('function Func2: boolean;');
  4203. Add('begin');
  4204. Add('end;');
  4205. Add('var i: longint;');
  4206. Add('begin');
  4207. Add(' if func2 then ;');
  4208. Add(' if i=func1() then ;');
  4209. Add(' if i=func1 then ;');
  4210. ConvertProgram;
  4211. CheckSource('TestFunctionResultInCondition',
  4212. LinesToStr([ // statements
  4213. 'this.Func1 = function () {',
  4214. ' var Result = 0;',
  4215. ' return Result;',
  4216. '};',
  4217. 'this.Func2 = function () {',
  4218. ' var Result = false;',
  4219. ' return Result;',
  4220. '};',
  4221. 'this.i = 0;'
  4222. ]),
  4223. LinesToStr([
  4224. 'if ($mod.Func2());',
  4225. 'if ($mod.i === $mod.Func1());',
  4226. 'if ($mod.i === $mod.Func1());'
  4227. ]));
  4228. end;
  4229. procedure TTestModule.TestFunctionResultInForLoop;
  4230. begin
  4231. StartProgram(false);
  4232. Add([
  4233. 'function Func1(a: array of longint): longint;',
  4234. 'begin',
  4235. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4236. ' for Result in a do if a[Result]=0 then exit;',
  4237. 'end;',
  4238. 'begin',
  4239. ' Func1([1,2,3])']);
  4240. ConvertProgram;
  4241. CheckSource('TestFunctionResultInForLoop',
  4242. LinesToStr([ // statements
  4243. 'this.Func1 = function (a) {',
  4244. ' var Result = 0;',
  4245. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4246. ' Result = $l;',
  4247. ' if (a[Result] === 0) return Result;',
  4248. ' };',
  4249. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4250. ' Result = $in[$l1];',
  4251. ' if (a[Result] === 0) return Result;',
  4252. ' };',
  4253. ' return Result;',
  4254. '};',
  4255. '']),
  4256. LinesToStr([
  4257. '$mod.Func1([1, 2, 3]);'
  4258. ]));
  4259. end;
  4260. procedure TTestModule.TestFunctionResultInTypeCast;
  4261. begin
  4262. StartProgram(false);
  4263. Add([
  4264. 'function GetInt: longint;',
  4265. 'begin',
  4266. 'end;',
  4267. 'begin',
  4268. ' if Byte(GetInt)=0 then ;',
  4269. '']);
  4270. ConvertProgram;
  4271. CheckSource('TestFunctionResultInTypeCast',
  4272. LinesToStr([ // statements
  4273. 'this.GetInt = function () {',
  4274. ' var Result = 0;',
  4275. ' return Result;',
  4276. '};',
  4277. '']),
  4278. LinesToStr([
  4279. 'if (($mod.GetInt() & 255) === 0) ;'
  4280. ]));
  4281. end;
  4282. procedure TTestModule.TestExit;
  4283. begin
  4284. StartProgram(false);
  4285. Add('procedure ProcA;');
  4286. Add('begin');
  4287. Add(' exit;');
  4288. Add('end;');
  4289. Add('function FuncB: longint;');
  4290. Add('begin');
  4291. Add(' exit;');
  4292. Add(' exit(3);');
  4293. Add('end;');
  4294. Add('function FuncC: string;');
  4295. Add('begin');
  4296. Add(' exit;');
  4297. Add(' exit(''a'');');
  4298. Add(' exit(''abc'');');
  4299. Add('end;');
  4300. Add('begin');
  4301. Add(' exit;');
  4302. Add(' exit(1);');
  4303. ConvertProgram;
  4304. CheckSource('TestExit',
  4305. LinesToStr([ // statements
  4306. 'this.ProcA = function () {',
  4307. ' return;',
  4308. '};',
  4309. 'this.FuncB = function () {',
  4310. ' var Result = 0;',
  4311. ' return Result;',
  4312. ' return 3;',
  4313. ' return Result;',
  4314. '};',
  4315. 'this.FuncC = function () {',
  4316. ' var Result = "";',
  4317. ' return Result;',
  4318. ' return "a";',
  4319. ' return "abc";',
  4320. ' return Result;',
  4321. '};'
  4322. ]),
  4323. LinesToStr([
  4324. 'return;',
  4325. 'return 1;',
  4326. '']));
  4327. end;
  4328. procedure TTestModule.TestExit_ResultInFinally;
  4329. begin
  4330. StartProgram(false);
  4331. Add([
  4332. 'function Run: word;',
  4333. 'begin',
  4334. ' try',
  4335. ' exit(3);', // no Result in finally -> use return 3
  4336. ' finally',
  4337. ' end;',
  4338. 'end;',
  4339. 'function Fly: word;',
  4340. 'begin',
  4341. ' try',
  4342. ' exit(3);',
  4343. ' finally',
  4344. ' if Result>0 then ;',
  4345. ' end;',
  4346. 'end;',
  4347. 'function Jump: word;',
  4348. 'begin',
  4349. ' try',
  4350. ' try',
  4351. ' exit(4);',
  4352. ' finally',
  4353. ' end;',
  4354. ' finally',
  4355. ' if Result>0 then ;',
  4356. ' end;',
  4357. 'end;',
  4358. 'begin',
  4359. '']);
  4360. ConvertProgram;
  4361. CheckSource('TestExit_ResultInFinally',
  4362. LinesToStr([ // statements
  4363. 'this.Run = function () {',
  4364. ' var Result = 0;',
  4365. ' try {',
  4366. ' return 3;',
  4367. ' } finally {',
  4368. ' };',
  4369. ' return Result;',
  4370. '};',
  4371. 'this.Fly = function () {',
  4372. ' var Result = 0;',
  4373. ' try {',
  4374. ' Result = 3;',
  4375. ' return Result;',
  4376. ' } finally {',
  4377. ' if (Result > 0) ;',
  4378. ' };',
  4379. ' return Result;',
  4380. '};',
  4381. 'this.Jump = function () {',
  4382. ' var Result = 0;',
  4383. ' try {',
  4384. ' try {',
  4385. ' Result = 4;',
  4386. ' return Result;',
  4387. ' } finally {',
  4388. ' };',
  4389. ' } finally {',
  4390. ' if (Result > 0) ;',
  4391. ' };',
  4392. ' return Result;',
  4393. '};',
  4394. '']),
  4395. LinesToStr([
  4396. '']));
  4397. end;
  4398. procedure TTestModule.TestBreak;
  4399. begin
  4400. StartProgram(false);
  4401. Add([
  4402. 'var',
  4403. ' i: longint;',
  4404. 'begin',
  4405. ' repeat',
  4406. ' break;',
  4407. ' until true;',
  4408. ' while true do',
  4409. ' break;',
  4410. ' for i:=1 to 2 do',
  4411. ' break;']);
  4412. ConvertProgram;
  4413. CheckSource('TestBreak',
  4414. LinesToStr([ // statements
  4415. 'this.i = 0;'
  4416. ]),
  4417. LinesToStr([
  4418. 'do {',
  4419. ' break;',
  4420. '} while (!true);',
  4421. 'while (true) break;',
  4422. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4423. '']));
  4424. end;
  4425. procedure TTestModule.TestBreakAsVar;
  4426. begin
  4427. StartProgram(false);
  4428. Add([
  4429. 'procedure DoIt(break: boolean);',
  4430. 'begin',
  4431. ' if break then ;',
  4432. 'end;',
  4433. 'var',
  4434. ' break: boolean;',
  4435. 'begin',
  4436. ' if break then ;']);
  4437. ConvertProgram;
  4438. CheckSource('TestBreakAsVar',
  4439. LinesToStr([ // statements
  4440. 'this.DoIt = function (Break) {',
  4441. ' if (Break) ;',
  4442. '};',
  4443. 'this.Break = false;',
  4444. '']),
  4445. LinesToStr([
  4446. 'if($mod.Break) ;',
  4447. '']));
  4448. end;
  4449. procedure TTestModule.TestContinue;
  4450. begin
  4451. StartProgram(false);
  4452. Add('var i: longint;');
  4453. Add('begin');
  4454. Add(' repeat');
  4455. Add(' continue;');
  4456. Add(' until true;');
  4457. Add(' while true do');
  4458. Add(' continue;');
  4459. Add(' for i:=1 to 2 do');
  4460. Add(' continue;');
  4461. ConvertProgram;
  4462. CheckSource('TestContinue',
  4463. LinesToStr([ // statements
  4464. 'this.i = 0;'
  4465. ]),
  4466. LinesToStr([
  4467. 'do {',
  4468. ' continue;',
  4469. '} while (!true);',
  4470. 'while (true) continue;',
  4471. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4472. '']));
  4473. end;
  4474. procedure TTestModule.TestProc_External;
  4475. begin
  4476. StartProgram(false);
  4477. Add('procedure Foo; external name ''console.log'';');
  4478. Add('function Bar: longint; external name ''get.item'';');
  4479. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4480. Add('var');
  4481. Add(' i: longint;');
  4482. Add('begin');
  4483. Add(' Foo;');
  4484. Add(' i:=Bar;');
  4485. Add(' i:=Bla(''abc'');');
  4486. ConvertProgram;
  4487. CheckSource('TestProc_External',
  4488. LinesToStr([ // statements
  4489. 'this.i = 0;'
  4490. ]),
  4491. LinesToStr([
  4492. 'console.log();',
  4493. '$mod.i = get.item();',
  4494. '$mod.i = apply.something("abc");'
  4495. ]));
  4496. end;
  4497. procedure TTestModule.TestProc_ExternalOtherUnit;
  4498. begin
  4499. AddModuleWithIntfImplSrc('unit2.pas',
  4500. LinesToStr([
  4501. 'procedure Now; external name ''Date.now'';',
  4502. 'procedure DoIt;'
  4503. ]),
  4504. 'procedure doit; begin end;');
  4505. StartUnit(true);
  4506. Add('interface');
  4507. Add('uses unit2;');
  4508. Add('implementation');
  4509. Add('begin');
  4510. Add(' now;');
  4511. Add(' now();');
  4512. Add(' uNit2.now;');
  4513. Add(' uNit2.now();');
  4514. Add(' doit;');
  4515. Add(' uNit2.doit;');
  4516. ConvertUnit;
  4517. CheckSource('TestProc_ExternalOtherUnit',
  4518. LinesToStr([
  4519. '']),
  4520. LinesToStr([
  4521. 'Date.now();',
  4522. 'Date.now();',
  4523. 'Date.now();',
  4524. 'Date.now();',
  4525. 'pas.unit2.DoIt();',
  4526. 'pas.unit2.DoIt();',
  4527. '']));
  4528. end;
  4529. procedure TTestModule.TestProc_Asm;
  4530. begin
  4531. StartProgram(false);
  4532. Add([
  4533. '{$mode delphi}',
  4534. 'function DoIt: longint;',
  4535. 'begin;',
  4536. ' asm',
  4537. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4538. ' end;',
  4539. ' asm console.log(); end;',
  4540. ' asm',
  4541. ' s = "'' ";',
  4542. ' s = ''" '';',
  4543. ' s = s + "world" + "''";',
  4544. ' // end',
  4545. ' s = ''end'';',
  4546. ' s = "end";',
  4547. ' s = "foo\"bar";',
  4548. ' s = ''a\''b'';',
  4549. ' s = `${expr}\`-"-''-`;',
  4550. ' s = `multi',
  4551. 'line`;',
  4552. ' end;',
  4553. 'end;',
  4554. 'procedure Fly;',
  4555. 'asm',
  4556. ' return;',
  4557. 'end;',
  4558. 'begin']);
  4559. ConvertProgram;
  4560. CheckSource('TestProc_Asm',
  4561. LinesToStr([ // statements
  4562. 'this.DoIt = function () {',
  4563. ' var Result = 0;',
  4564. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4565. ' console.log();',
  4566. ' s = "'' ";',
  4567. ' s = ''" '';',
  4568. ' s = s + "world" + "''";',
  4569. ' // end',
  4570. ' s = ''end'';',
  4571. ' s = "end";',
  4572. ' s = "foo\"bar";',
  4573. ' s = ''a\''b'';',
  4574. ' s = `${expr}\`-"-''-`;',
  4575. ' s = `multi',
  4576. 'line`;',
  4577. ' return Result;',
  4578. '};',
  4579. 'this.Fly = function () {',
  4580. ' return;',
  4581. '};',
  4582. '']),
  4583. LinesToStr([
  4584. ''
  4585. ]));
  4586. end;
  4587. procedure TTestModule.TestProc_AsmSubBlock;
  4588. begin
  4589. StartProgram(true,[supTObject]);
  4590. Add([
  4591. '{$mode delphi}',
  4592. 'type',
  4593. ' TBird = class end;',
  4594. 'procedure Run(w: word);',
  4595. 'begin;',
  4596. ' if true then asm console.log(); end;',
  4597. ' if w>3 then asm',
  4598. ' var a = w+1;',
  4599. ' w = a+3;',
  4600. ' end;',
  4601. ' while (w>7) do asm',
  4602. ' w+=3; w*=2;',
  4603. ' end;',
  4604. ' try',
  4605. ' except',
  4606. ' on E: TBird do',
  4607. ' asm console.log(E); end;',
  4608. ' on E: TObject do',
  4609. ' asm var i=3; i--; end;',
  4610. ' else asm Fly; High; end;',
  4611. ' end;',
  4612. 'end;',
  4613. 'begin']);
  4614. ConvertProgram;
  4615. CheckSource('TestProc_AsmSubBlock',
  4616. LinesToStr([ // statements
  4617. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4618. '});',
  4619. 'this.Run = function (w) {',
  4620. ' if (true) console.log();',
  4621. ' if (w > 3) {',
  4622. ' var a = w+1;',
  4623. ' w = a+3;',
  4624. ' };',
  4625. ' while (w > 7) {',
  4626. ' w+=3; w*=2;',
  4627. ' };',
  4628. ' try {} catch ($e) {',
  4629. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4630. ' var E = $e;',
  4631. ' console.log(E);',
  4632. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4633. ' var E = $e;',
  4634. ' var i=3; i--;',
  4635. ' } else {',
  4636. ' Fly; High;',
  4637. ' }',
  4638. ' };',
  4639. '};',
  4640. '']),
  4641. LinesToStr([
  4642. ''
  4643. ]));
  4644. end;
  4645. procedure TTestModule.TestProc_Assembler;
  4646. begin
  4647. StartProgram(false);
  4648. Add('function DoIt: longint; assembler;');
  4649. Add('asm');
  4650. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4651. Add('end;');
  4652. Add('begin');
  4653. ConvertProgram;
  4654. CheckSource('TestProc_Assembler',
  4655. LinesToStr([ // statements
  4656. 'this.DoIt = function () {',
  4657. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4658. '};'
  4659. ]),
  4660. LinesToStr([
  4661. ''
  4662. ]));
  4663. end;
  4664. procedure TTestModule.TestProc_VarParam;
  4665. begin
  4666. StartProgram(false);
  4667. Add('type integer = longint;');
  4668. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4669. Add('var vJ: integer;');
  4670. Add('begin');
  4671. Add(' vg:=vg+1;');
  4672. Add(' vj:=vh+2;');
  4673. Add(' vi:=vi+3;');
  4674. Add(' doit(vg,vg,vg);');
  4675. Add(' doit(vh,vh,vj);');
  4676. Add(' doit(vi,vi,vi);');
  4677. Add(' doit(vj,vj,vj);');
  4678. Add('end;');
  4679. Add('var i: integer;');
  4680. Add('begin');
  4681. Add(' doit(i,i,i);');
  4682. ConvertProgram;
  4683. CheckSource('TestProc_VarParam',
  4684. LinesToStr([ // statements
  4685. 'this.DoIt = function (vG,vH,vI) {',
  4686. ' var vJ = 0;',
  4687. ' vG = vG + 1;',
  4688. ' vJ = vH + 2;',
  4689. ' vI.set(vI.get()+3);',
  4690. ' $mod.DoIt(vG, vG, {',
  4691. ' get: function () {',
  4692. ' return vG;',
  4693. ' },',
  4694. ' set: function (v) {',
  4695. ' vG = v;',
  4696. ' }',
  4697. ' });',
  4698. ' $mod.DoIt(vH, vH, {',
  4699. ' get: function () {',
  4700. ' return vJ;',
  4701. ' },',
  4702. ' set: function (v) {',
  4703. ' vJ = v;',
  4704. ' }',
  4705. ' });',
  4706. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4707. ' $mod.DoIt(vJ, vJ, {',
  4708. ' get: function () {',
  4709. ' return vJ;',
  4710. ' },',
  4711. ' set: function (v) {',
  4712. ' vJ = v;',
  4713. ' }',
  4714. ' });',
  4715. '};',
  4716. 'this.i = 0;'
  4717. ]),
  4718. LinesToStr([
  4719. '$mod.DoIt($mod.i,$mod.i,{',
  4720. ' p: $mod,',
  4721. ' get: function () {',
  4722. ' return this.p.i;',
  4723. ' },',
  4724. ' set: function (v) {',
  4725. ' this.p.i = v;',
  4726. ' }',
  4727. '});'
  4728. ]));
  4729. end;
  4730. procedure TTestModule.TestProc_VarParamString;
  4731. begin
  4732. StartProgram(false);
  4733. Add(['type TCaption = string;',
  4734. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4735. 'var c: char;',
  4736. 'begin',
  4737. ' va[1]:=c;',
  4738. ' vb[2]:=c;',
  4739. ' vc[3]:=c;',
  4740. 'end;',
  4741. 'begin']);
  4742. ConvertProgram;
  4743. CheckSource('TestProc_VarParamString',
  4744. LinesToStr([ // statements
  4745. 'this.DoIt = function (vA,vB,vC) {',
  4746. ' var c = "";',
  4747. ' vA = rtl.setCharAt(vA, 0, c);',
  4748. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4749. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4750. '};',
  4751. '']),
  4752. LinesToStr([
  4753. ]));
  4754. end;
  4755. procedure TTestModule.TestProc_VarParamV;
  4756. begin
  4757. StartProgram(false);
  4758. Add([
  4759. 'procedure Inc2(var i: longint);',
  4760. 'begin',
  4761. ' i:=i+2;',
  4762. 'end;',
  4763. 'procedure DoIt(v: longint);',
  4764. 'var p: array of longint;',
  4765. 'begin',
  4766. ' Inc2(v);',
  4767. ' Inc2(p[v]);',
  4768. 'end;',
  4769. 'begin']);
  4770. ConvertProgram;
  4771. CheckSource('TestProc_VarParamV',
  4772. LinesToStr([ // statements
  4773. 'this.Inc2 = function (i) {',
  4774. ' i.set(i.get()+2);',
  4775. '};',
  4776. 'this.DoIt = function (v) {',
  4777. ' var p = [];',
  4778. ' $mod.Inc2({get: function () {',
  4779. ' return v;',
  4780. ' }, set: function (w) {',
  4781. ' v = w;',
  4782. ' }});',
  4783. ' $mod.Inc2({',
  4784. ' a: v,',
  4785. ' p: p,',
  4786. ' get: function () {',
  4787. ' return this.p[this.a];',
  4788. ' },',
  4789. ' set: function (v) {',
  4790. ' this.p[this.a] = v;',
  4791. ' }',
  4792. ' });',
  4793. '};',
  4794. '']),
  4795. LinesToStr([
  4796. '']));
  4797. end;
  4798. procedure TTestModule.TestProc_Overload;
  4799. begin
  4800. StartProgram(false);
  4801. Add('procedure DoIt(vI: longint); begin end;');
  4802. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4803. Add('procedure DoIt(vD: double); begin end;');
  4804. Add('begin');
  4805. Add(' DoIt(1);');
  4806. Add(' DoIt(2,3);');
  4807. Add(' DoIt(4.5);');
  4808. ConvertProgram;
  4809. CheckSource('TestProcedureOverload',
  4810. LinesToStr([ // statements
  4811. 'this.DoIt = function (vI) {',
  4812. '};',
  4813. 'this.DoIt$1 = function (vI, vJ) {',
  4814. '};',
  4815. 'this.DoIt$2 = function (vD) {',
  4816. '};',
  4817. '']),
  4818. LinesToStr([
  4819. '$mod.DoIt(1);',
  4820. '$mod.DoIt$1(2, 3);',
  4821. '$mod.DoIt$2(4.5);',
  4822. '']));
  4823. end;
  4824. procedure TTestModule.TestProc_OverloadForward;
  4825. begin
  4826. StartProgram(false);
  4827. Add('procedure DoIt(vI: longint); forward;');
  4828. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4829. Add('procedure doit(vi: longint); begin end;');
  4830. Add('begin');
  4831. Add(' doit(1);');
  4832. Add(' doit(2,3);');
  4833. ConvertProgram;
  4834. CheckSource('TestProcedureOverloadForward',
  4835. LinesToStr([ // statements
  4836. 'this.DoIt$1 = function (vI, vJ) {',
  4837. '};',
  4838. 'this.DoIt = function (vI) {',
  4839. '};',
  4840. '']),
  4841. LinesToStr([
  4842. '$mod.DoIt(1);',
  4843. '$mod.DoIt$1(2, 3);',
  4844. '']));
  4845. end;
  4846. procedure TTestModule.TestProc_OverloadIntfImpl;
  4847. begin
  4848. StartUnit(false);
  4849. Add('interface');
  4850. Add('procedure DoIt(vI: longint);');
  4851. Add('procedure DoIt(vI, vJ: longint);');
  4852. Add('implementation');
  4853. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4854. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4855. Add('procedure DoIt(vi: longint); begin end;');
  4856. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4857. Add('procedure DoIt(vi, vj: longint); begin end;');
  4858. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4859. Add('begin');
  4860. Add(' doit(1);');
  4861. Add(' doit(2,3);');
  4862. Add(' doit(4,5,6);');
  4863. Add(' doit(7,8,9,10);');
  4864. Add(' doit(11,12,13,14,15);');
  4865. ConvertUnit;
  4866. CheckSource('TestProcedureOverloadUnit',
  4867. LinesToStr([ // statements
  4868. 'var $impl = $mod.$impl;',
  4869. 'this.DoIt = function (vI) {',
  4870. '};',
  4871. 'this.DoIt$1 = function (vI, vJ) {',
  4872. '};',
  4873. '']),
  4874. LinesToStr([ // this.$init
  4875. '$mod.DoIt(1);',
  4876. '$mod.DoIt$1(2, 3);',
  4877. '$impl.DoIt$3(4,5,6);',
  4878. '$impl.DoIt$4(7,8,9,10);',
  4879. '$impl.DoIt$2(11,12,13,14,15);',
  4880. '']),
  4881. LinesToStr([ // implementation
  4882. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4883. '};',
  4884. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4885. '};',
  4886. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4887. '};',
  4888. '']));
  4889. end;
  4890. procedure TTestModule.TestProc_OverloadNested;
  4891. begin
  4892. StartProgram(false);
  4893. Add([
  4894. 'procedure doit(vA: longint);',
  4895. ' procedure DoIt(vA, vB: longint); overload;',
  4896. ' begin',
  4897. ' doit(1);',
  4898. ' doit(1,2);',
  4899. ' end;',
  4900. ' procedure doit(vA, vB, vC: longint);',
  4901. ' begin',
  4902. ' doit(1);',
  4903. ' doit(1,2);',
  4904. ' doit(1,2,3);',
  4905. ' end;',
  4906. 'begin',
  4907. ' doit(1);',
  4908. ' doit(1,2);',
  4909. ' doit(1,2,3);',
  4910. 'end;',
  4911. 'begin // main',
  4912. ' doit(1);']);
  4913. ConvertProgram;
  4914. CheckSource('TestProcedureOverloadNested',
  4915. LinesToStr([ // statements
  4916. 'this.doit = function (vA) {',
  4917. ' function DoIt$1(vA, vB) {',
  4918. ' $mod.doit(1);',
  4919. ' DoIt$1(1, 2);',
  4920. ' };',
  4921. ' function doit$2(vA, vB, vC) {',
  4922. ' $mod.doit(1);',
  4923. ' DoIt$1(1, 2);',
  4924. ' doit$2(1, 2, 3);',
  4925. ' };',
  4926. ' $mod.doit(1);',
  4927. ' DoIt$1(1, 2);',
  4928. ' doit$2(1, 2, 3);',
  4929. '};',
  4930. '']),
  4931. LinesToStr([
  4932. '$mod.doit(1);',
  4933. '']));
  4934. end;
  4935. procedure TTestModule.TestProc_OverloadNestedForward;
  4936. begin
  4937. StartProgram(false);
  4938. Add([
  4939. 'procedure DoIt(vA: longint); overload; forward;',
  4940. 'procedure DoIt(vB, vC: longint); overload;',
  4941. 'begin // 2 param overload',
  4942. ' doit(1);',
  4943. ' doit(1,2);',
  4944. 'end;',
  4945. 'procedure doit(vA: longint);',
  4946. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4947. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4948. ' begin // 4 param overload',
  4949. ' doit(1);',
  4950. ' doit(1,2);',
  4951. ' doit(1,2,3);',
  4952. ' doit(1,2,3,4);',
  4953. ' end;',
  4954. ' procedure doit(vA, vB, vC: longint);',
  4955. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4956. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4957. ' begin // 6 param overload',
  4958. ' doit(1);',
  4959. ' doit(1,2);',
  4960. ' doit(1,2,3);',
  4961. ' doit(1,2,3,4);',
  4962. ' doit(1,2,3,4,5);',
  4963. ' doit(1,2,3,4,5,6);',
  4964. ' end;',
  4965. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4966. ' begin // 5 param overload',
  4967. ' doit(1);',
  4968. ' doit(1,2);',
  4969. ' doit(1,2,3);',
  4970. ' doit(1,2,3,4);',
  4971. ' doit(1,2,3,4,5);',
  4972. ' doit(1,2,3,4,5,6);',
  4973. ' end;',
  4974. ' begin // 3 param overload',
  4975. ' doit(1);',
  4976. ' doit(1,2);',
  4977. ' doit(1,2,3);',
  4978. ' doit(1,2,3,4);',
  4979. ' doit(1,2,3,4,5);',
  4980. ' doit(1,2,3,4,5,6);',
  4981. ' end;',
  4982. 'begin // 1 param overload',
  4983. ' doit(1);',
  4984. ' doit(1,2);',
  4985. ' doit(1,2,3);',
  4986. ' doit(1,2,3,4);',
  4987. 'end;',
  4988. 'begin // main',
  4989. ' doit(1);',
  4990. ' doit(1,2);']);
  4991. ConvertProgram;
  4992. CheckSource('TestProc_OverloadNestedForward',
  4993. LinesToStr([ // statements
  4994. 'this.DoIt$1 = function (vB, vC) {',
  4995. ' $mod.DoIt(1);',
  4996. ' $mod.DoIt$1(1, 2);',
  4997. '};',
  4998. 'this.DoIt = function (vA) {',
  4999. ' function DoIt$3(vA, vB, vC, vD) {',
  5000. ' $mod.DoIt(1);',
  5001. ' $mod.DoIt$1(1, 2);',
  5002. ' DoIt$2(1, 2, 3);',
  5003. ' DoIt$3(1, 2, 3, 4);',
  5004. ' };',
  5005. ' function DoIt$2(vA, vB, vC) {',
  5006. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5007. ' $mod.DoIt(1);',
  5008. ' $mod.DoIt$1(1, 2);',
  5009. ' DoIt$2(1, 2, 3);',
  5010. ' DoIt$3(1, 2, 3, 4);',
  5011. ' DoIt$4(1, 2, 3, 4, 5);',
  5012. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5013. ' };',
  5014. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5015. ' $mod.DoIt(1);',
  5016. ' $mod.DoIt$1(1, 2);',
  5017. ' DoIt$2(1, 2, 3);',
  5018. ' DoIt$3(1, 2, 3, 4);',
  5019. ' DoIt$4(1, 2, 3, 4, 5);',
  5020. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5021. ' };',
  5022. ' $mod.DoIt(1);',
  5023. ' $mod.DoIt$1(1, 2);',
  5024. ' DoIt$2(1, 2, 3);',
  5025. ' DoIt$3(1, 2, 3, 4);',
  5026. ' DoIt$4(1, 2, 3, 4, 5);',
  5027. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5028. ' };',
  5029. ' $mod.DoIt(1);',
  5030. ' $mod.DoIt$1(1, 2);',
  5031. ' DoIt$2(1, 2, 3);',
  5032. ' DoIt$3(1, 2, 3, 4);',
  5033. '};',
  5034. '']),
  5035. LinesToStr([
  5036. '$mod.DoIt(1);',
  5037. '$mod.DoIt$1(1, 2);',
  5038. '']));
  5039. end;
  5040. procedure TTestModule.TestProc_OverloadUnitCycle;
  5041. begin
  5042. AddModuleWithIntfImplSrc('Unit2.pas',
  5043. LinesToStr([
  5044. 'type',
  5045. ' TObject = class',
  5046. ' procedure DoIt(b: boolean); virtual; abstract;',
  5047. ' procedure DoIt(i: longint); virtual; abstract;',
  5048. ' end;',
  5049. '']),
  5050. 'uses test1;');
  5051. StartUnit(true);
  5052. Add([
  5053. 'interface',
  5054. 'uses unit2;',
  5055. 'type',
  5056. ' TEagle = class(TObject)',
  5057. ' procedure DoIt(b: boolean); override;',
  5058. ' procedure DoIt(i: longint); override;',
  5059. ' end;',
  5060. 'implementation',
  5061. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5062. 'procedure TEagle.DoIt(i: longint); begin end;',
  5063. '']);
  5064. ConvertUnit;
  5065. CheckSource('TestProc_OverloadUnitCycle',
  5066. LinesToStr([ // statements
  5067. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5068. ' this.DoIt = function (b) {',
  5069. ' };',
  5070. ' this.DoIt$1 = function (i) {',
  5071. ' };',
  5072. '});',
  5073. '']),
  5074. '',
  5075. LinesToStr([
  5076. '']));
  5077. end;
  5078. procedure TTestModule.TestProc_Varargs;
  5079. begin
  5080. StartProgram(false);
  5081. Add([
  5082. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5083. 'procedure ProcB; varargs; external name ''ProcB'';',
  5084. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5085. 'function GetIt: longint; begin end;',
  5086. 'begin',
  5087. ' ProcA(1);',
  5088. ' ProcA(1,2);',
  5089. ' ProcA(1,2.0);',
  5090. ' ProcA(1,2,3);',
  5091. ' ProcA(1,''2'');',
  5092. ' ProcA(2,'''');',
  5093. ' ProcA(3,false);',
  5094. ' ProcB;',
  5095. ' ProcB();',
  5096. ' ProcB(4);',
  5097. ' ProcB(''foo'');',
  5098. ' ProcC;',
  5099. ' ProcC();',
  5100. ' ProcC(4);',
  5101. ' ProcC(5,''foo'');',
  5102. ' ProcB(GetIt);',
  5103. ' ProcB(GetIt());',
  5104. ' ProcB(GetIt,GetIt());']);
  5105. ConvertProgram;
  5106. CheckSource('TestProc_Varargs',
  5107. LinesToStr([ // statements
  5108. 'this.GetIt = function () {',
  5109. ' var Result = 0;',
  5110. ' return Result;',
  5111. '};',
  5112. '']),
  5113. LinesToStr([
  5114. 'ProcA(1);',
  5115. 'ProcA(1, 2);',
  5116. 'ProcA(1, 2.0);',
  5117. 'ProcA(1, 2, 3);',
  5118. 'ProcA(1, "2");',
  5119. 'ProcA(2, "");',
  5120. 'ProcA(3, false);',
  5121. 'ProcB();',
  5122. 'ProcB();',
  5123. 'ProcB(4);',
  5124. 'ProcB("foo");',
  5125. 'ProcC(17);',
  5126. 'ProcC(17);',
  5127. 'ProcC(4);',
  5128. 'ProcC(5, "foo");',
  5129. 'ProcB($mod.GetIt());',
  5130. 'ProcB($mod.GetIt());',
  5131. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5132. '']));
  5133. end;
  5134. procedure TTestModule.TestProc_ConstOrder;
  5135. begin
  5136. StartProgram(false);
  5137. Add([
  5138. 'const A = 3;',
  5139. 'const B = A+1;',
  5140. 'procedure DoIt;',
  5141. 'const C = A+1;',
  5142. 'const D = B+1;',
  5143. 'const E = D+C+B+A;',
  5144. 'begin',
  5145. 'end;',
  5146. 'begin'
  5147. ]);
  5148. ConvertProgram;
  5149. CheckSource('TestProc_ConstOrder',
  5150. LinesToStr([ // statements
  5151. 'this.A = 3;',
  5152. 'this.B = 3 + 1;',
  5153. 'var C = 3 + 1;',
  5154. 'var D = 4 + 1;',
  5155. 'var E = 5 + 4 + 4 + 3;',
  5156. 'this.DoIt = function () {',
  5157. '};',
  5158. '']),
  5159. LinesToStr([
  5160. ''
  5161. ]));
  5162. end;
  5163. procedure TTestModule.TestProc_DuplicateConst;
  5164. begin
  5165. StartProgram(false);
  5166. Add([
  5167. 'const A = 1;',
  5168. 'procedure DoIt;',
  5169. 'const A = 2;',
  5170. ' procedure SubIt;',
  5171. ' const A = 21;',
  5172. ' begin',
  5173. ' end;',
  5174. 'begin',
  5175. 'end;',
  5176. 'procedure DoSome;',
  5177. 'const A = 3;',
  5178. 'begin',
  5179. 'end;',
  5180. 'begin'
  5181. ]);
  5182. ConvertProgram;
  5183. CheckSource('TestProc_DuplicateConst',
  5184. LinesToStr([ // statements
  5185. 'this.A = 1;',
  5186. 'var A$1 = 2;',
  5187. 'var A$2 = 21;',
  5188. 'this.DoIt = function () {',
  5189. ' function SubIt() {',
  5190. ' };',
  5191. '};',
  5192. 'var A$3 = 3;',
  5193. 'this.DoSome = function () {',
  5194. '};',
  5195. '']),
  5196. LinesToStr([
  5197. ''
  5198. ]));
  5199. end;
  5200. procedure TTestModule.TestProc_LocalVarAbsolute;
  5201. begin
  5202. StartProgram(false);
  5203. Add([
  5204. 'type',
  5205. ' TObject = class',
  5206. ' Index: longint;',
  5207. ' procedure DoAbs(Item: pointer);',
  5208. ' end;',
  5209. 'procedure TObject.DoAbs(Item: pointer);',
  5210. 'var',
  5211. ' o: TObject absolute Item;',
  5212. 'begin',
  5213. ' if o.Index<o.Index then o.Index:=o.Index;',
  5214. 'end;',
  5215. 'procedure DoIt(i: longint; p: pointer);',
  5216. 'var',
  5217. ' d: double absolute i;',
  5218. ' s: string absolute d;',
  5219. ' oi: TObject absolute i;',
  5220. ' op: TObject absolute p;',
  5221. 'begin',
  5222. ' if d=d then d:=d;',
  5223. ' if s=s then s:=s;',
  5224. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5225. ' if op.Index=op.Index then op.Index:=op.Index;',
  5226. 'end;',
  5227. 'begin']);
  5228. ConvertProgram;
  5229. CheckSource('TestProc_LocalVarAbsolute',
  5230. LinesToStr([ // statements
  5231. 'rtl.createClass(this, "TObject", null, function () {',
  5232. ' this.$init = function () {',
  5233. ' this.Index = 0;',
  5234. ' };',
  5235. ' this.$final = function () {',
  5236. ' };',
  5237. ' this.DoAbs = function (Item) {',
  5238. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5239. ' };',
  5240. '});',
  5241. 'this.DoIt = function (i, p) {',
  5242. ' if (i === i) i = i;',
  5243. ' if (i === i) i = i;',
  5244. ' if (i.Index < i.Index) i.Index = i.Index;',
  5245. ' if (p.Index === p.Index) p.Index = p.Index;',
  5246. '};'
  5247. ]),
  5248. LinesToStr([
  5249. ]));
  5250. end;
  5251. procedure TTestModule.TestProc_LocalVarInit;
  5252. begin
  5253. StartProgram(false);
  5254. Add([
  5255. 'type TBytes = array of byte;',
  5256. 'procedure DoIt;',
  5257. 'const c = 4;',
  5258. 'var',
  5259. ' b: byte = 1;',
  5260. ' w: word = 2+c;',
  5261. ' p: pointer = nil;',
  5262. ' Buffer: TBytes = nil;',
  5263. 'begin',
  5264. 'end;',
  5265. 'begin']);
  5266. ConvertProgram;
  5267. CheckSource('TestProc_LocalVarInit',
  5268. LinesToStr([ // statements
  5269. 'var c = 4;',
  5270. 'this.DoIt = function () {',
  5271. ' var b = 1;',
  5272. ' var w = 2 + 4;',
  5273. ' var p = null;',
  5274. ' var Buffer = [];',
  5275. '};',
  5276. '']),
  5277. LinesToStr([
  5278. ]));
  5279. end;
  5280. procedure TTestModule.TestProc_ReservedWords;
  5281. begin
  5282. StartProgram(false);
  5283. Add([
  5284. 'procedure Date(ArrayBuffer: longint);',
  5285. 'const',
  5286. ' NaN: longint = 3;',
  5287. 'var',
  5288. ' &Boolean: longint;',
  5289. ' procedure Error(ArrayBuffer: longint);',
  5290. ' begin',
  5291. ' end;',
  5292. 'begin',
  5293. ' Nan:=&bOolean;',
  5294. 'end;',
  5295. 'begin',
  5296. ' Date(1);']);
  5297. ConvertProgram;
  5298. CheckSource('TestProc_ReservedWords',
  5299. LinesToStr([ // statements
  5300. 'var naN = 3;',
  5301. 'this.Date = function (arrayBuffer) {',
  5302. ' var boolean = 0;',
  5303. ' function error(arrayBuffer) {',
  5304. ' };',
  5305. ' naN = boolean;',
  5306. '};',
  5307. '']),
  5308. LinesToStr([
  5309. ' $mod.Date(1);'
  5310. ]));
  5311. end;
  5312. procedure TTestModule.TestProc_ConstRefWord;
  5313. begin
  5314. StartProgram(false);
  5315. Add([
  5316. 'procedure Run(constref w: word);',
  5317. 'var l: word;',
  5318. 'begin',
  5319. ' l:=w;',
  5320. ' Run(w);',
  5321. ' Run(l);',
  5322. 'end;',
  5323. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5324. 'begin',
  5325. ' Run(a);',
  5326. ' Run(b);',
  5327. ' Run(c);',
  5328. ' Run(d);',
  5329. ' Run(e);',
  5330. 'end;',
  5331. 'begin',
  5332. ' Run(1);']);
  5333. ConvertProgram;
  5334. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5335. CheckSource('TestProc_ConstRefWord',
  5336. LinesToStr([ // statements
  5337. 'this.Run = function (w) {',
  5338. ' var l = 0;',
  5339. ' l = w;',
  5340. ' $mod.Run(w);',
  5341. ' $mod.Run(l);',
  5342. '};',
  5343. 'this.Fly = function (a, b, c, d, e) {',
  5344. ' $mod.Run(a);',
  5345. ' $mod.Run(b.get());',
  5346. ' $mod.Run(c.get());',
  5347. ' $mod.Run(d);',
  5348. ' $mod.Run(e);',
  5349. '};',
  5350. '']),
  5351. LinesToStr([
  5352. '$mod.Run(1);'
  5353. ]));
  5354. end;
  5355. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5356. begin
  5357. StartProgram(false);
  5358. Add([
  5359. '{$mode objfpc}',
  5360. 'type',
  5361. ' TFunc = reference to function(x: word): word;',
  5362. 'var Func: TFunc;',
  5363. 'procedure DoIt(a: word);',
  5364. 'begin',
  5365. ' Func:=function(b:word): word',
  5366. ' begin',
  5367. ' Result:=a+b;',
  5368. ' exit(b);',
  5369. ' exit(Result);',
  5370. ' end;',// test semicolon
  5371. ' a:=3;',
  5372. 'end;',
  5373. 'begin',
  5374. ' Func:=function(c:word):word begin',
  5375. ' Result:=3+c;',
  5376. ' exit(c);',
  5377. ' exit(Result);',
  5378. ' end;']);
  5379. ConvertProgram;
  5380. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5381. LinesToStr([ // statements
  5382. 'this.Func = null;',
  5383. 'this.DoIt = function (a) {',
  5384. ' $mod.Func = function (b) {',
  5385. ' var Result = 0;',
  5386. ' Result = a + b;',
  5387. ' return b;',
  5388. ' return Result;',
  5389. ' return Result;',
  5390. ' };',
  5391. ' a = 3;',
  5392. '};',
  5393. '']),
  5394. LinesToStr([
  5395. '$mod.Func = function (c) {',
  5396. ' var Result = 0;',
  5397. ' Result = 3 + c;',
  5398. ' return c;',
  5399. ' return Result;',
  5400. ' return Result;',
  5401. '};',
  5402. '']));
  5403. end;
  5404. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5405. begin
  5406. StartProgram(false);
  5407. Add([
  5408. '{$mode delphi}',
  5409. 'type',
  5410. ' TProc = reference to procedure(x: word);',
  5411. 'procedure DoIt(a: word);',
  5412. 'var Proc: TProc;',
  5413. 'begin',
  5414. ' Proc:=procedure(b:word) begin end;',
  5415. 'end;',
  5416. 'var Proc: TProc;',
  5417. 'begin',
  5418. ' Proc:=procedure(c:word) begin end;',
  5419. '']);
  5420. ConvertProgram;
  5421. CheckSource('TestAnonymousProc_Assign_Delphi',
  5422. LinesToStr([ // statements
  5423. 'this.DoIt = function (a) {',
  5424. ' var Proc = null;',
  5425. ' Proc = function (b) {',
  5426. ' };',
  5427. '};',
  5428. 'this.Proc = null;',
  5429. '']),
  5430. LinesToStr([
  5431. '$mod.Proc = function (c) {',
  5432. '};',
  5433. '']));
  5434. end;
  5435. procedure TTestModule.TestAnonymousProc_Arg;
  5436. begin
  5437. StartProgram(false);
  5438. Add([
  5439. 'type',
  5440. ' TProc = reference to procedure;',
  5441. ' TFunc = reference to function(x: word): word;',
  5442. 'procedure DoMore(f,g: TProc);',
  5443. 'begin',
  5444. 'end;',
  5445. 'procedure DoOdd(v: jsvalue);',
  5446. 'begin',
  5447. 'end;',
  5448. 'procedure DoIt(f: TFunc);',
  5449. 'begin',
  5450. ' DoIt(function(b:word): word',
  5451. ' begin',
  5452. ' Result:=1+b;',
  5453. ' end);',
  5454. ' DoMore(procedure begin end, procedure begin end);',
  5455. ' DoOdd(procedure begin end);',
  5456. 'end;',
  5457. 'begin',
  5458. ' DoMore(procedure begin end,',
  5459. ' procedure assembler asm',
  5460. ' console.log("c");',
  5461. ' end);',
  5462. '']);
  5463. ConvertProgram;
  5464. CheckSource('TestAnonymousProc_Arg',
  5465. LinesToStr([ // statements
  5466. 'this.DoMore = function (f, g) {',
  5467. '};',
  5468. 'this.DoOdd = function (v) {',
  5469. '};',
  5470. 'this.DoIt = function (f) {',
  5471. ' $mod.DoIt(function (b) {',
  5472. ' var Result = 0;',
  5473. ' Result = 1 + b;',
  5474. ' return Result;',
  5475. ' });',
  5476. ' $mod.DoMore(function () {',
  5477. ' }, function () {',
  5478. ' });',
  5479. ' $mod.DoOdd(function () {',
  5480. ' });',
  5481. '};',
  5482. '']),
  5483. LinesToStr([
  5484. '$mod.DoMore(function () {',
  5485. '}, function () {',
  5486. ' console.log("c");',
  5487. '});',
  5488. '']));
  5489. end;
  5490. procedure TTestModule.TestAnonymousProc_Typecast;
  5491. begin
  5492. StartProgram(false);
  5493. Add([
  5494. 'type',
  5495. ' TProc = reference to procedure(w: word);',
  5496. ' TArr = array of word;',
  5497. ' TFuncArr = reference to function: TArr;',
  5498. 'procedure DoIt(p: TProc);',
  5499. 'var',
  5500. ' w: word;',
  5501. ' a: TArr;',
  5502. 'begin',
  5503. ' p:=TProc(procedure(b: smallint) begin end);',
  5504. ' a:=TFuncArr(function: TArr begin end)();',
  5505. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5506. 'end;',
  5507. 'begin']);
  5508. ConvertProgram;
  5509. CheckSource('TestAnonymousProc_Typecast',
  5510. LinesToStr([ // statements
  5511. 'this.DoIt = function (p) {',
  5512. ' var w = 0;',
  5513. ' var a = [];',
  5514. ' p = function (b) {',
  5515. ' };',
  5516. ' a = function () {',
  5517. ' var Result = [];',
  5518. ' return Result;',
  5519. ' }();',
  5520. ' w = function () {',
  5521. ' var Result = [];',
  5522. ' return Result;',
  5523. ' }()[3];',
  5524. '};',
  5525. '']),
  5526. LinesToStr([
  5527. '']));
  5528. end;
  5529. procedure TTestModule.TestAnonymousProc_With;
  5530. begin
  5531. StartProgram(false);
  5532. Add([
  5533. 'type',
  5534. ' TProc = reference to procedure(w: word);',
  5535. ' TObject = class',
  5536. ' b: boolean;',
  5537. ' end;',
  5538. 'var',
  5539. ' p: TProc;',
  5540. ' bird: TObject;',
  5541. 'begin',
  5542. ' with bird do',
  5543. ' p:=procedure(w: word)',
  5544. ' begin',
  5545. ' b:=w>2;',
  5546. ' end;',
  5547. '']);
  5548. ConvertProgram;
  5549. CheckSource('TestAnonymousProc_With',
  5550. LinesToStr([ // statements
  5551. 'rtl.createClass(this, "TObject", null, function () {',
  5552. ' this.$init = function () {',
  5553. ' this.b = false;',
  5554. ' };',
  5555. ' this.$final = function () {',
  5556. ' };',
  5557. '});',
  5558. 'this.p = null;',
  5559. 'this.bird = null;',
  5560. '']),
  5561. LinesToStr([
  5562. 'var $with = $mod.bird;',
  5563. '$mod.p = function (w) {',
  5564. ' $with.b = w > 2;',
  5565. '};',
  5566. '']));
  5567. end;
  5568. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5569. begin
  5570. StartProgram(false);
  5571. Add([
  5572. 'type',
  5573. ' TProc = reference to procedure;',
  5574. ' TObject = class',
  5575. ' b: boolean;',
  5576. ' end;',
  5577. 'procedure DoIt;',
  5578. 'var',
  5579. ' p: TProc;',
  5580. 'begin',
  5581. ' try',
  5582. ' except',
  5583. ' on E: TObject do',
  5584. ' p:=procedure',
  5585. ' begin',
  5586. ' E.b:=true;',
  5587. ' end;',
  5588. ' end;',
  5589. 'end;',
  5590. 'begin']);
  5591. ConvertProgram;
  5592. CheckSource('TestAnonymousProc_ExceptOn',
  5593. LinesToStr([ // statements
  5594. 'rtl.createClass(this, "TObject", null, function () {',
  5595. ' this.$init = function () {',
  5596. ' this.b = false;',
  5597. ' };',
  5598. ' this.$final = function () {',
  5599. ' };',
  5600. '});',
  5601. 'this.DoIt = function () {',
  5602. ' var p = null;',
  5603. ' try {} catch ($e) {',
  5604. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5605. ' var E = $e;',
  5606. ' p = function () {',
  5607. ' E.b = true;',
  5608. ' };',
  5609. ' } else throw $e',
  5610. ' };',
  5611. '};',
  5612. '']),
  5613. LinesToStr([
  5614. '']));
  5615. end;
  5616. procedure TTestModule.TestAnonymousProc_Nested;
  5617. begin
  5618. StartProgram(false);
  5619. Add([
  5620. 'type',
  5621. ' TProc = reference to procedure;',
  5622. ' TObject = class',
  5623. ' i: byte;',
  5624. ' procedure DoIt;',
  5625. ' end;',
  5626. 'procedure TObject.DoIt;',
  5627. 'var',
  5628. ' p: TProc;',
  5629. ' procedure Sub;',
  5630. ' begin',
  5631. ' p:=procedure',
  5632. ' begin',
  5633. ' i:=3;',
  5634. ' Self.i:=4;',
  5635. ' p:=procedure',
  5636. ' procedure SubSub;',
  5637. ' begin',
  5638. ' i:=13;',
  5639. ' Self.i:=14;',
  5640. ' end;',
  5641. ' begin',
  5642. ' i:=13;',
  5643. ' Self.i:=14;',
  5644. ' end;',
  5645. ' end;',
  5646. ' end;',
  5647. 'begin',
  5648. 'end;',
  5649. 'begin']);
  5650. ConvertProgram;
  5651. CheckSource('TestAnonymousProc_Nested',
  5652. LinesToStr([ // statements
  5653. 'rtl.createClass(this, "TObject", null, function () {',
  5654. ' this.$init = function () {',
  5655. ' this.i = 0;',
  5656. ' };',
  5657. ' this.$final = function () {',
  5658. ' };',
  5659. ' this.DoIt = function () {',
  5660. ' var $Self = this;',
  5661. ' var p = null;',
  5662. ' function Sub() {',
  5663. ' p = function () {',
  5664. ' $Self.i = 3;',
  5665. ' $Self.i = 4;',
  5666. ' p = function () {',
  5667. ' function SubSub() {',
  5668. ' $Self.i = 13;',
  5669. ' $Self.i = 14;',
  5670. ' };',
  5671. ' $Self.i = 13;',
  5672. ' $Self.i = 14;',
  5673. ' };',
  5674. ' };',
  5675. ' };',
  5676. ' };',
  5677. '});',
  5678. '']),
  5679. LinesToStr([
  5680. '']));
  5681. end;
  5682. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5683. begin
  5684. StartProgram(false);
  5685. Add([
  5686. 'type',
  5687. ' TProc = reference to procedure;',
  5688. 'function DoIt: TProc;',
  5689. ' function Sub: TProc;',
  5690. ' begin',
  5691. ' Result:=procedure',
  5692. ' begin',
  5693. ' Sub:=procedure',
  5694. ' procedure SubSub;',
  5695. ' begin',
  5696. ' Result:=nil;',
  5697. ' Sub:=nil;',
  5698. ' DoIt:=nil;',
  5699. ' end;',
  5700. ' begin',
  5701. ' Result:=nil;',
  5702. ' Sub:=nil;',
  5703. ' DoIt:=nil;',
  5704. ' end;',
  5705. ' end;',
  5706. ' end;',
  5707. 'begin',
  5708. 'end;',
  5709. 'begin']);
  5710. ConvertProgram;
  5711. CheckSource('TestAnonymousProc_NestedAssignResult',
  5712. LinesToStr([ // statements
  5713. 'this.DoIt = function () {',
  5714. ' var Result = null;',
  5715. ' function Sub() {',
  5716. ' var Result$1 = null;',
  5717. ' Result$1 = function () {',
  5718. ' Result$1 = function () {',
  5719. ' function SubSub() {',
  5720. ' Result$1 = null;',
  5721. ' Result$1 = null;',
  5722. ' Result = null;',
  5723. ' };',
  5724. ' Result$1 = null;',
  5725. ' Result$1 = null;',
  5726. ' Result = null;',
  5727. ' };',
  5728. ' };',
  5729. ' return Result$1;',
  5730. ' };',
  5731. ' return Result;',
  5732. '};',
  5733. '']),
  5734. LinesToStr([
  5735. '']));
  5736. end;
  5737. procedure TTestModule.TestAnonymousProc_Class;
  5738. begin
  5739. StartProgram(false);
  5740. Add([
  5741. 'type',
  5742. ' TProc = reference to procedure;',
  5743. ' TEvent = procedure of object;',
  5744. ' TObject = class',
  5745. ' Size: word;',
  5746. ' function GetIt: TProc;',
  5747. ' procedure DoIt; virtual; abstract;',
  5748. ' end;',
  5749. 'function TObject.GetIt: TProc;',
  5750. 'begin',
  5751. ' Result:=procedure',
  5752. ' var p: TEvent;',
  5753. ' begin',
  5754. ' Size:=Size;',
  5755. ' Size:=Self.Size;',
  5756. ' p:=@DoIt;',
  5757. ' p:[email protected];',
  5758. ' end;',
  5759. 'end;',
  5760. 'begin']);
  5761. ConvertProgram;
  5762. CheckSource('TestAnonymousProc_Class',
  5763. LinesToStr([ // statements
  5764. 'rtl.createClass(this, "TObject", null, function () {',
  5765. ' this.$init = function () {',
  5766. ' this.Size = 0;',
  5767. ' };',
  5768. ' this.$final = function () {',
  5769. ' };',
  5770. ' this.GetIt = function () {',
  5771. ' var $Self = this;',
  5772. ' var Result = null;',
  5773. ' Result = function () {',
  5774. ' var p = null;',
  5775. ' $Self.Size = $Self.Size;',
  5776. ' $Self.Size = $Self.Size;',
  5777. ' p = rtl.createCallback($Self, "DoIt");',
  5778. ' p = rtl.createCallback($Self, "DoIt");',
  5779. ' };',
  5780. ' return Result;',
  5781. ' };',
  5782. '});',
  5783. '']),
  5784. LinesToStr([
  5785. '']));
  5786. end;
  5787. procedure TTestModule.TestAnonymousProc_ForLoop;
  5788. begin
  5789. StartProgram(false);
  5790. Add([
  5791. 'type TProc = reference to procedure;',
  5792. 'procedure Foo(p: TProc);',
  5793. 'begin',
  5794. 'end;',
  5795. 'procedure DoIt;',
  5796. 'var i: word;',
  5797. ' a: word;',
  5798. 'begin',
  5799. ' for i:=1 to 10 do begin',
  5800. ' Foo(procedure begin a:=3; end);',
  5801. ' end;',
  5802. 'end;',
  5803. 'begin',
  5804. ' DoIt;']);
  5805. ConvertProgram;
  5806. CheckSource('TestAnonymousProc_ForLoop',
  5807. LinesToStr([ // statements
  5808. 'this.Foo = function (p) {',
  5809. '};',
  5810. 'this.DoIt = function () {',
  5811. ' var i = 0;',
  5812. ' var a = 0;',
  5813. ' for (i = 1; i <= 10; i++) {',
  5814. ' $mod.Foo(function () {',
  5815. ' a = 3;',
  5816. ' });',
  5817. ' };',
  5818. '};',
  5819. '']),
  5820. LinesToStr([
  5821. '$mod.DoIt();'
  5822. ]));
  5823. end;
  5824. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5825. begin
  5826. StartProgram(false);
  5827. Add([
  5828. '{$mode delphi}',
  5829. 'type',
  5830. ' TProc = reference to procedure;',
  5831. ' TFunc = reference to function(x: word): word;',
  5832. 'procedure Run;',
  5833. 'asm',
  5834. 'end;',
  5835. 'procedure Walk(p: TProc; f: TFunc);',
  5836. 'begin',
  5837. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5838. 'end;',
  5839. 'begin',
  5840. ' Walk(procedure',
  5841. ' asm',
  5842. ' console.log("a");',
  5843. ' end,',
  5844. ' function(x: word): word asm',
  5845. ' console.log("c");',
  5846. ' end);',
  5847. '']);
  5848. ConvertProgram;
  5849. CheckSource('TestAnonymousProc_AsmDelphi',
  5850. LinesToStr([ // statements
  5851. 'this.Run = function () {',
  5852. '};',
  5853. 'this.Walk = function (p, f) {',
  5854. ' $mod.Walk(function () {',
  5855. ' }, function (b) {',
  5856. ' return 1+b;',
  5857. ' });',
  5858. '};',
  5859. '']),
  5860. LinesToStr([
  5861. '$mod.Walk(function () {',
  5862. ' console.log("a");',
  5863. '}, function (x) {',
  5864. ' console.log("c");',
  5865. '});',
  5866. '']));
  5867. end;
  5868. procedure TTestModule.TestEnum_Name;
  5869. begin
  5870. StartProgram(false);
  5871. Add('type TMyEnum = (Red, Green, Blue);');
  5872. Add('var e: TMyEnum;');
  5873. Add('var f: TMyEnum = Blue;');
  5874. Add('begin');
  5875. Add(' e:=green;');
  5876. Add(' e:=default(TMyEnum);');
  5877. ConvertProgram;
  5878. CheckSource('TestEnum_Name',
  5879. LinesToStr([ // statements
  5880. 'this.TMyEnum = {',
  5881. ' "0":"Red",',
  5882. ' Red:0,',
  5883. ' "1":"Green",',
  5884. ' Green:1,',
  5885. ' "2":"Blue",',
  5886. ' Blue:2',
  5887. ' };',
  5888. 'this.e = 0;',
  5889. 'this.f = this.TMyEnum.Blue;'
  5890. ]),
  5891. LinesToStr([
  5892. '$mod.e=$mod.TMyEnum.Green;',
  5893. '$mod.e=$mod.TMyEnum.Red;'
  5894. ]));
  5895. end;
  5896. procedure TTestModule.TestEnum_Number;
  5897. begin
  5898. Converter.Options:=Converter.Options+[coEnumNumbers];
  5899. StartProgram(false);
  5900. Add('type TMyEnum = (Red, Green);');
  5901. Add('var');
  5902. Add(' e: TMyEnum;');
  5903. Add(' f: TMyEnum = Green;');
  5904. Add(' i: longint;');
  5905. Add('begin');
  5906. Add(' e:=green;');
  5907. Add(' i:=longint(e);');
  5908. ConvertProgram;
  5909. CheckSource('TestEnumNumber',
  5910. LinesToStr([ // statements
  5911. 'this.TMyEnum = {',
  5912. ' "0":"Red",',
  5913. ' Red:0,',
  5914. ' "1":"Green",',
  5915. ' Green:1',
  5916. ' };',
  5917. 'this.e = 0;',
  5918. 'this.f = 1;',
  5919. 'this.i = 0;'
  5920. ]),
  5921. LinesToStr([
  5922. '$mod.e=1;',
  5923. '$mod.i=$mod.e;'
  5924. ]));
  5925. end;
  5926. procedure TTestModule.TestEnum_ConstFail;
  5927. begin
  5928. StartProgram(false);
  5929. Add([
  5930. 'type TMyEnum = (Red = 100, Green = 101);',
  5931. 'var',
  5932. ' e: TMyEnum;',
  5933. ' f: TMyEnum = Green;',
  5934. 'begin',
  5935. ' e:=green;']);
  5936. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5937. ConvertProgram;
  5938. end;
  5939. procedure TTestModule.TestEnum_Functions;
  5940. begin
  5941. StartProgram(false);
  5942. Add([
  5943. 'type TMyEnum = (Red, Green);',
  5944. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5945. 'var',
  5946. ' v: longint;',
  5947. ' s: string;',
  5948. 'begin',
  5949. ' val(s,e,v);',
  5950. ' val(s,e,i);',
  5951. 'end;',
  5952. 'var',
  5953. ' e: TMyEnum;',
  5954. ' i: longint;',
  5955. ' s: string;',
  5956. ' b: boolean;',
  5957. 'begin',
  5958. ' i:=ord(red);',
  5959. ' i:=ord(green);',
  5960. ' i:=ord(e);',
  5961. ' i:=ord(b);',
  5962. ' e:=low(tmyenum);',
  5963. ' e:=low(e);',
  5964. ' b:=low(boolean);',
  5965. ' e:=high(tmyenum);',
  5966. ' e:=high(e);',
  5967. ' b:=high(boolean);',
  5968. ' e:=pred(green);',
  5969. ' e:=pred(e);',
  5970. ' b:=pred(b);',
  5971. ' e:=succ(red);',
  5972. ' e:=succ(e);',
  5973. ' b:=succ(b);',
  5974. ' e:=tmyenum(1);',
  5975. ' e:=tmyenum(i);',
  5976. ' s:=str(e);',
  5977. ' str(e,s);',
  5978. ' str(red,s);',
  5979. ' s:=str(e:3);',
  5980. ' writestr(s,e:3,red);',
  5981. ' val(s,e,i);',
  5982. ' i:=longint(e);']);
  5983. ConvertProgram;
  5984. CheckSource('TestEnum_Functions',
  5985. LinesToStr([ // statements
  5986. 'this.TMyEnum = {',
  5987. ' "0":"Red",',
  5988. ' Red:0,',
  5989. ' "1":"Green",',
  5990. ' Green:1',
  5991. ' };',
  5992. 'this.DoIt = function (e, i) {',
  5993. ' var v = 0;',
  5994. ' var s = "";',
  5995. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5996. ' v = w;',
  5997. ' }));',
  5998. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5999. '};',
  6000. 'this.e = 0;',
  6001. 'this.i = 0;',
  6002. 'this.s = "";',
  6003. 'this.b = false;',
  6004. '']),
  6005. LinesToStr([
  6006. '$mod.i=$mod.TMyEnum.Red;',
  6007. '$mod.i=$mod.TMyEnum.Green;',
  6008. '$mod.i=$mod.e;',
  6009. '$mod.i=$mod.b+0;',
  6010. '$mod.e=$mod.TMyEnum.Red;',
  6011. '$mod.e=$mod.TMyEnum.Red;',
  6012. '$mod.b=false;',
  6013. '$mod.e=$mod.TMyEnum.Green;',
  6014. '$mod.e=$mod.TMyEnum.Green;',
  6015. '$mod.b=true;',
  6016. '$mod.e=$mod.TMyEnum.Green-1;',
  6017. '$mod.e=$mod.e-1;',
  6018. '$mod.b=false;',
  6019. '$mod.e=$mod.TMyEnum.Red+1;',
  6020. '$mod.e=$mod.e+1;',
  6021. '$mod.b=true;',
  6022. '$mod.e=1;',
  6023. '$mod.e=$mod.i;',
  6024. '$mod.s = $mod.TMyEnum[$mod.e];',
  6025. '$mod.s = $mod.TMyEnum[$mod.e];',
  6026. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6027. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6028. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6029. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6030. ' $mod.i = v;',
  6031. '});',
  6032. '$mod.i=$mod.e;',
  6033. '']));
  6034. end;
  6035. procedure TTestModule.TestEnumRg_Functions;
  6036. begin
  6037. StartProgram(false);
  6038. Add([
  6039. 'type',
  6040. ' TEnum = (Red, Green, Blue);',
  6041. ' TEnumRg = Green..Blue;',
  6042. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6043. 'var',
  6044. ' v: longint;',
  6045. ' s: string;',
  6046. 'begin',
  6047. ' val(s,e,v);',
  6048. ' val(s,e,i);',
  6049. 'end;',
  6050. 'var',
  6051. ' e: TEnumRg;',
  6052. ' i: longint;',
  6053. ' s: string;',
  6054. 'begin',
  6055. ' i:=ord(green);',
  6056. ' i:=ord(e);',
  6057. ' e:=low(tenumrg);',
  6058. ' e:=low(e);',
  6059. ' e:=high(tenumrg);',
  6060. ' e:=high(e);',
  6061. ' e:=pred(blue);',
  6062. ' e:=pred(e);',
  6063. ' e:=succ(green);',
  6064. ' e:=succ(e);',
  6065. ' e:=tenumrg(1);',
  6066. ' e:=tenumrg(i);',
  6067. ' s:=str(e);',
  6068. ' str(e,s);',
  6069. ' str(red,s);',
  6070. ' s:=str(e:3);',
  6071. ' writestr(s,e:3,blue);',
  6072. ' val(s,e,i);',
  6073. ' i:=longint(e);']);
  6074. ConvertProgram;
  6075. CheckSource('TestEnumRg_Functions',
  6076. LinesToStr([ // statements
  6077. 'this.TEnum = {',
  6078. ' "0":"Red",',
  6079. ' Red:0,',
  6080. ' "1":"Green",',
  6081. ' Green:1,',
  6082. ' "2":"Blue",',
  6083. ' Blue:2',
  6084. ' };',
  6085. 'this.DoIt = function (e, i) {',
  6086. ' var v = 0;',
  6087. ' var s = "";',
  6088. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6089. ' v = w;',
  6090. ' }));',
  6091. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6092. '};',
  6093. 'this.e = this.TEnum.Green;',
  6094. 'this.i = 0;',
  6095. 'this.s = "";',
  6096. '']),
  6097. LinesToStr([
  6098. '$mod.i=$mod.TEnum.Green;',
  6099. '$mod.i=$mod.e;',
  6100. '$mod.e=$mod.TEnum.Green;',
  6101. '$mod.e=$mod.TEnum.Green;',
  6102. '$mod.e=$mod.TEnum.Blue;',
  6103. '$mod.e=$mod.TEnum.Blue;',
  6104. '$mod.e=$mod.TEnum.Blue-1;',
  6105. '$mod.e=$mod.e-1;',
  6106. '$mod.e=$mod.TEnum.Green+1;',
  6107. '$mod.e=$mod.e+1;',
  6108. '$mod.e=1;',
  6109. '$mod.e=$mod.i;',
  6110. '$mod.s = $mod.TEnum[$mod.e];',
  6111. '$mod.s = $mod.TEnum[$mod.e];',
  6112. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6113. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6114. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6115. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6116. ' $mod.i = v;',
  6117. '});',
  6118. '$mod.i=$mod.e;',
  6119. '']));
  6120. end;
  6121. procedure TTestModule.TestEnum_AsParams;
  6122. begin
  6123. StartProgram(false);
  6124. Add('type TEnum = (Red,Blue);');
  6125. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6126. Add('var vJ: TEnum;');
  6127. Add('begin');
  6128. Add(' vg:=vg;');
  6129. Add(' vj:=vh;');
  6130. Add(' vi:=vi;');
  6131. Add(' doit(vg,vg,vg);');
  6132. Add(' doit(vh,vh,vj);');
  6133. Add(' doit(vi,vi,vi);');
  6134. Add(' doit(vj,vj,vj);');
  6135. Add('end;');
  6136. Add('var i: TEnum;');
  6137. Add('begin');
  6138. Add(' doit(i,i,i);');
  6139. ConvertProgram;
  6140. CheckSource('TestEnum_AsParams',
  6141. LinesToStr([ // statements
  6142. 'this.TEnum = {',
  6143. ' "0": "Red",',
  6144. ' Red: 0,',
  6145. ' "1": "Blue",',
  6146. ' Blue: 1',
  6147. '};',
  6148. 'this.DoIt = function (vG,vH,vI) {',
  6149. ' var vJ = 0;',
  6150. ' vG = vG;',
  6151. ' vJ = vH;',
  6152. ' vI.set(vI.get());',
  6153. ' $mod.DoIt(vG, vG, {',
  6154. ' get: function () {',
  6155. ' return vG;',
  6156. ' },',
  6157. ' set: function (v) {',
  6158. ' vG = v;',
  6159. ' }',
  6160. ' });',
  6161. ' $mod.DoIt(vH, vH, {',
  6162. ' get: function () {',
  6163. ' return vJ;',
  6164. ' },',
  6165. ' set: function (v) {',
  6166. ' vJ = v;',
  6167. ' }',
  6168. ' });',
  6169. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6170. ' $mod.DoIt(vJ, vJ, {',
  6171. ' get: function () {',
  6172. ' return vJ;',
  6173. ' },',
  6174. ' set: function (v) {',
  6175. ' vJ = v;',
  6176. ' }',
  6177. ' });',
  6178. '};',
  6179. 'this.i = 0;'
  6180. ]),
  6181. LinesToStr([
  6182. '$mod.DoIt($mod.i,$mod.i,{',
  6183. ' p: $mod,',
  6184. ' get: function () {',
  6185. ' return this.p.i;',
  6186. ' },',
  6187. ' set: function (v) {',
  6188. ' this.p.i = v;',
  6189. ' }',
  6190. '});'
  6191. ]));
  6192. end;
  6193. procedure TTestModule.TestEnumRange_Array;
  6194. begin
  6195. StartProgram(false);
  6196. Add([
  6197. 'type',
  6198. ' TEnum = (Red, Green, Blue);',
  6199. ' TEnumRg = green..blue;',
  6200. ' TArr = array[TEnumRg] of byte;',
  6201. ' TArr2 = array[green..blue] of byte;',
  6202. 'var',
  6203. ' a: TArr;',
  6204. ' b: TArr = (3,4);',
  6205. ' c: TArr2 = (5,6);',
  6206. 'begin',
  6207. ' a[green] := b[blue];',
  6208. ' c[green] := c[blue];',
  6209. '']);
  6210. ConvertProgram;
  6211. CheckSource('TestEnumRange_Array',
  6212. LinesToStr([ // statements
  6213. 'this.TEnum = {',
  6214. ' "0": "Red",',
  6215. ' Red: 0,',
  6216. ' "1": "Green",',
  6217. ' Green: 1,',
  6218. ' "2": "Blue",',
  6219. ' Blue: 2',
  6220. '};',
  6221. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6222. 'this.b = [3, 4];',
  6223. 'this.c = [5, 6];',
  6224. '']),
  6225. LinesToStr([
  6226. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6227. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6228. '']));
  6229. end;
  6230. procedure TTestModule.TestEnum_ForIn;
  6231. begin
  6232. StartProgram(false);
  6233. Add([
  6234. 'type',
  6235. ' TEnum = (Red, Green, Blue);',
  6236. ' TEnumRg = green..blue;',
  6237. ' TArr = array[TEnum] of byte;',
  6238. ' TArrRg = array[TEnumRg] of byte;',
  6239. 'var',
  6240. ' e: TEnum;',
  6241. ' a1: TArr = (3,4,5);',
  6242. ' a2: TArrRg = (11,12);',
  6243. ' b: byte;',
  6244. 'begin',
  6245. ' for e in TEnum do ;',
  6246. ' for e in TEnumRg do ;',
  6247. ' for e in TArr do ;',
  6248. ' for e in TArrRg do ;',
  6249. ' for b in a1 do ;',
  6250. ' for b in a2 do ;',
  6251. '']);
  6252. ConvertProgram;
  6253. CheckSource('TestEnum_ForIn',
  6254. LinesToStr([ // statements
  6255. 'this.TEnum = {',
  6256. ' "0": "Red",',
  6257. ' Red: 0,',
  6258. ' "1": "Green",',
  6259. ' Green: 1,',
  6260. ' "2": "Blue",',
  6261. ' Blue: 2',
  6262. '};',
  6263. 'this.e = 0;',
  6264. 'this.a1 = [3, 4, 5];',
  6265. 'this.a2 = [11, 12];',
  6266. 'this.b = 0;',
  6267. '']),
  6268. LinesToStr([
  6269. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6270. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6271. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6272. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6273. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6274. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6275. '']));
  6276. end;
  6277. procedure TTestModule.TestEnum_ScopedNumber;
  6278. begin
  6279. Converter.Options:=Converter.Options+[coEnumNumbers];
  6280. StartProgram(false);
  6281. Add([
  6282. 'type',
  6283. ' TEnum = (Red, Green);',
  6284. 'var',
  6285. ' e: TEnum;',
  6286. 'begin',
  6287. ' e:=TEnum.Green;',
  6288. '']);
  6289. ConvertProgram;
  6290. CheckSource('TestEnum_ScopedNumber',
  6291. LinesToStr([ // statements
  6292. 'this.TEnum = {',
  6293. ' "0": "Red",',
  6294. ' Red: 0,',
  6295. ' "1": "Green",',
  6296. ' Green: 1',
  6297. '};',
  6298. 'this.e = 0;',
  6299. '']),
  6300. LinesToStr([
  6301. '$mod.e = 1;']));
  6302. end;
  6303. procedure TTestModule.TestEnum_InFunction;
  6304. begin
  6305. StartProgram(false);
  6306. Add([
  6307. 'const TEnum = 3;',
  6308. 'procedure DoIt;',
  6309. 'type',
  6310. ' TEnum = (Red, Green, Blue);',
  6311. ' procedure Sub;',
  6312. ' type',
  6313. ' TEnumSub = (Left, Right);',
  6314. ' var',
  6315. ' es: TEnumSub;',
  6316. ' begin',
  6317. ' es:=Left;',
  6318. ' end;',
  6319. 'var',
  6320. ' e, e2: TEnum;',
  6321. 'begin',
  6322. ' if e in [red,blue] then e2:=e;',
  6323. 'end;',
  6324. 'begin']);
  6325. ConvertProgram;
  6326. CheckSource('TestEnum_InFunction',
  6327. LinesToStr([ // statements
  6328. 'this.TEnum = 3;',
  6329. 'var TEnum$1 = {',
  6330. ' "0":"Red",',
  6331. ' Red:0,',
  6332. ' "1":"Green",',
  6333. ' Green:1,',
  6334. ' "2":"Blue",',
  6335. ' Blue:2',
  6336. ' };',
  6337. 'var TEnumSub = {',
  6338. ' "0": "Left",',
  6339. ' Left: 0,',
  6340. ' "1": "Right",',
  6341. ' Right: 1',
  6342. '};',
  6343. 'this.DoIt = function () {',
  6344. ' function Sub() {',
  6345. ' var es = 0;',
  6346. ' es = TEnumSub.Left;',
  6347. ' };',
  6348. ' var e = 0;',
  6349. ' var e2 = 0;',
  6350. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6351. '};',
  6352. '']),
  6353. LinesToStr([
  6354. '']));
  6355. end;
  6356. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6357. begin
  6358. StartUnit(true);
  6359. Add([
  6360. 'interface',
  6361. 'var color: (red, green);',
  6362. 'implementation',
  6363. 'initialization',
  6364. ' color:=green;',
  6365. '']);
  6366. ConvertUnit;
  6367. CheckSource('TestEnum_Name_Anonymous_Unit',
  6368. LinesToStr([
  6369. 'this.color$a = {',
  6370. ' "0": "red",',
  6371. ' red: 0,',
  6372. ' "1": "green",',
  6373. ' green: 1',
  6374. '};',
  6375. 'this.color = 0;',
  6376. '']),
  6377. LinesToStr([ // this.$init
  6378. '$mod.color = $mod.color$a.green;',
  6379. '']),
  6380. LinesToStr([ // implementation
  6381. '']) );
  6382. end;
  6383. procedure TTestModule.TestSet_Enum;
  6384. begin
  6385. StartProgram(false);
  6386. Add([
  6387. 'type',
  6388. ' TColor = (Red, Green, Blue);',
  6389. ' TColors = set of TColor;',
  6390. 'var',
  6391. ' c: TColor;',
  6392. ' s: TColors;',
  6393. ' t: TColors = [];',
  6394. ' u: TColors = [Red];',
  6395. 'begin',
  6396. ' s:=[];',
  6397. ' s:=[Green];',
  6398. ' s:=[Green,Blue];',
  6399. ' s:=[Red..Blue];',
  6400. ' s:=[Red,Green..Blue];',
  6401. ' s:=[Red,c];',
  6402. ' s:=t;',
  6403. ' s:=default(TColors);',
  6404. '']);
  6405. ConvertProgram;
  6406. CheckSource('TestSet',
  6407. LinesToStr([ // statements
  6408. 'this.TColor = {',
  6409. ' "0":"Red",',
  6410. ' Red:0,',
  6411. ' "1":"Green",',
  6412. ' Green:1,',
  6413. ' "2":"Blue",',
  6414. ' Blue:2',
  6415. ' };',
  6416. 'this.c = 0;',
  6417. 'this.s = {};',
  6418. 'this.t = {};',
  6419. 'this.u = rtl.createSet(this.TColor.Red);'
  6420. ]),
  6421. LinesToStr([
  6422. '$mod.s={};',
  6423. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6424. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6425. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6426. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6427. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6428. '$mod.s=rtl.refSet($mod.t);',
  6429. '$mod.s={};',
  6430. '']));
  6431. end;
  6432. procedure TTestModule.TestSet_Operators;
  6433. begin
  6434. StartProgram(false);
  6435. Add('type');
  6436. Add(' TColor = (Red, Green, Blue);');
  6437. Add(' TColors = set of tcolor;');
  6438. Add('var');
  6439. Add(' vC: TColor;');
  6440. Add(' vS: TColors;');
  6441. Add(' vT: TColors;');
  6442. Add(' vU: TColors;');
  6443. Add(' B: boolean;');
  6444. Add('begin');
  6445. Add(' include(vs,green);');
  6446. Add(' exclude(vs,vc);');
  6447. Add(' vs:=vt+vu;');
  6448. Add(' vs:=vt+[red];');
  6449. Add(' vs:=[red]+vt;');
  6450. Add(' vs:=[red]+[green];');
  6451. Add(' vs:=vt-vu;');
  6452. Add(' vs:=vt-[red];');
  6453. Add(' vs:=[red]-vt;');
  6454. Add(' vs:=[red]-[green];');
  6455. Add(' vs:=vt*vu;');
  6456. Add(' vs:=vt*[red];');
  6457. Add(' vs:=[red]*vt;');
  6458. Add(' vs:=[red]*[green];');
  6459. Add(' vs:=vt><vu;');
  6460. Add(' vs:=vt><[red];');
  6461. Add(' vs:=[red]><vt;');
  6462. Add(' vs:=[red]><[green];');
  6463. Add(' b:=vt=vu;');
  6464. Add(' b:=vt=[red];');
  6465. Add(' b:=[red]=vt;');
  6466. Add(' b:=[red]=[green];');
  6467. Add(' b:=vt<>vu;');
  6468. Add(' b:=vt<>[red];');
  6469. Add(' b:=[red]<>vt;');
  6470. Add(' b:=[red]<>[green];');
  6471. Add(' b:=vt<=vu;');
  6472. Add(' b:=vt<=[red];');
  6473. Add(' b:=[red]<=vt;');
  6474. Add(' b:=[red]<=[green];');
  6475. Add(' b:=vt>=vu;');
  6476. Add(' b:=vt>=[red];');
  6477. Add(' b:=[red]>=vt;');
  6478. Add(' b:=[red]>=[green];');
  6479. ConvertProgram;
  6480. CheckSource('TestSet_Operators',
  6481. LinesToStr([ // statements
  6482. 'this.TColor = {',
  6483. ' "0":"Red",',
  6484. ' Red:0,',
  6485. ' "1":"Green",',
  6486. ' Green:1,',
  6487. ' "2":"Blue",',
  6488. ' Blue:2',
  6489. ' };',
  6490. 'this.vC = 0;',
  6491. 'this.vS = {};',
  6492. 'this.vT = {};',
  6493. 'this.vU = {};',
  6494. 'this.B = false;'
  6495. ]),
  6496. LinesToStr([
  6497. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6498. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6499. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6500. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6501. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6502. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6503. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6504. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6505. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6506. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6507. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6508. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6509. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6510. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6511. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6512. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6513. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6514. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6515. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6516. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6517. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6518. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6519. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6520. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6521. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6522. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6523. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6524. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6525. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6526. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6527. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6528. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6529. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6530. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6531. '']));
  6532. end;
  6533. procedure TTestModule.TestSet_Operator_In;
  6534. begin
  6535. StartProgram(false);
  6536. Add([
  6537. 'type',
  6538. ' TColor = (Red, Green, Blue);',
  6539. ' TColors = set of tcolor;',
  6540. ' TColorRg = green..blue;',
  6541. 'var',
  6542. ' vC: tcolor;',
  6543. ' vT: tcolors;',
  6544. ' B: boolean;',
  6545. ' rg: TColorRg;',
  6546. 'begin',
  6547. ' b:=red in vt;',
  6548. ' b:=vc in vt;',
  6549. ' b:=green in [red..blue];',
  6550. ' b:=vc in [red..blue];',
  6551. ' ',
  6552. ' if red in vt then ;',
  6553. ' while vC in vt do ;',
  6554. ' repeat',
  6555. ' until vC in vt;',
  6556. ' if rg in [green..blue] then ;',
  6557. '']);
  6558. ConvertProgram;
  6559. CheckSource('TestSet_Operator_In',
  6560. LinesToStr([ // statements
  6561. 'this.TColor = {',
  6562. ' "0":"Red",',
  6563. ' Red:0,',
  6564. ' "1":"Green",',
  6565. ' Green:1,',
  6566. ' "2":"Blue",',
  6567. ' Blue:2',
  6568. ' };',
  6569. 'this.vC = 0;',
  6570. 'this.vT = {};',
  6571. 'this.B = false;',
  6572. 'this.rg = this.TColor.Green;',
  6573. '']),
  6574. LinesToStr([
  6575. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6576. '$mod.B = $mod.vC in $mod.vT;',
  6577. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6578. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6579. 'if ($mod.TColor.Red in $mod.vT) ;',
  6580. 'while ($mod.vC in $mod.vT) {',
  6581. '};',
  6582. 'do {',
  6583. '} while (!($mod.vC in $mod.vT));',
  6584. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6585. '']));
  6586. end;
  6587. procedure TTestModule.TestSet_Functions;
  6588. begin
  6589. StartProgram(false);
  6590. Add('type');
  6591. Add(' TMyEnum = (Red, Green);');
  6592. Add(' TMyEnums = set of TMyEnum;');
  6593. Add('var');
  6594. Add(' e: TMyEnum;');
  6595. Add(' s: TMyEnums;');
  6596. Add('begin');
  6597. Add(' e:=Low(TMyEnums);');
  6598. Add(' e:=Low(s);');
  6599. Add(' e:=High(TMyEnums);');
  6600. Add(' e:=High(s);');
  6601. ConvertProgram;
  6602. CheckSource('TestSetFunctions',
  6603. LinesToStr([ // statements
  6604. 'this.TMyEnum = {',
  6605. ' "0":"Red",',
  6606. ' Red:0,',
  6607. ' "1":"Green",',
  6608. ' Green:1',
  6609. ' };',
  6610. 'this.e = 0;',
  6611. 'this.s = {};'
  6612. ]),
  6613. LinesToStr([
  6614. '$mod.e=$mod.TMyEnum.Red;',
  6615. '$mod.e=$mod.TMyEnum.Red;',
  6616. '$mod.e=$mod.TMyEnum.Green;',
  6617. '$mod.e=$mod.TMyEnum.Green;',
  6618. '']));
  6619. end;
  6620. procedure TTestModule.TestSet_PassAsArgClone;
  6621. begin
  6622. StartProgram(false);
  6623. Add('type');
  6624. Add(' TMyEnum = (Red, Green);');
  6625. Add(' TMyEnums = set of TMyEnum;');
  6626. Add('procedure DoDefault(s: tmyenums); begin end;');
  6627. Add('procedure DoConst(const s: tmyenums); begin end;');
  6628. Add('var');
  6629. Add(' aSet: tmyenums;');
  6630. Add('begin');
  6631. Add(' dodefault(aset);');
  6632. Add(' doconst(aset);');
  6633. ConvertProgram;
  6634. CheckSource('TestSetFunctions',
  6635. LinesToStr([ // statements
  6636. 'this.TMyEnum = {',
  6637. ' "0":"Red",',
  6638. ' Red:0,',
  6639. ' "1":"Green",',
  6640. ' Green:1',
  6641. ' };',
  6642. 'this.DoDefault = function (s) {',
  6643. '};',
  6644. 'this.DoConst = function (s) {',
  6645. '};',
  6646. 'this.aSet = {};'
  6647. ]),
  6648. LinesToStr([
  6649. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6650. '$mod.DoConst($mod.aSet);',
  6651. '']));
  6652. end;
  6653. procedure TTestModule.TestSet_AsParams;
  6654. begin
  6655. StartProgram(false);
  6656. Add([
  6657. 'type TEnum = (Red,Blue);',
  6658. 'type TEnums = set of TEnum;',
  6659. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6660. 'var vJ: TEnums;',
  6661. 'begin',
  6662. ' Include(vg,red);',
  6663. ' Include(result,blue);',
  6664. ' vg:=vg;',
  6665. ' vj:=vh;',
  6666. ' vi:=vi;',
  6667. ' doit(vg,vg,vg);',
  6668. ' doit(vh,vh,vj);',
  6669. ' doit(vi,vi,vi);',
  6670. ' doit(vj,vj,vj);',
  6671. 'end;',
  6672. 'var i: TEnums;',
  6673. 'begin',
  6674. ' doit(i,i,i);']);
  6675. ConvertProgram;
  6676. CheckSource('TestSet_AsParams',
  6677. LinesToStr([ // statements
  6678. 'this.TEnum = {',
  6679. ' "0": "Red",',
  6680. ' Red: 0,',
  6681. ' "1": "Blue",',
  6682. ' Blue: 1',
  6683. '};',
  6684. 'this.DoIt = function (vG,vH,vI) {',
  6685. ' var Result = {};',
  6686. ' var vJ = {};',
  6687. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6688. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6689. ' vG = rtl.refSet(vG);',
  6690. ' vJ = rtl.refSet(vH);',
  6691. ' vI.set(rtl.refSet(vI.get()));',
  6692. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6693. ' get: function () {',
  6694. ' return vG;',
  6695. ' },',
  6696. ' set: function (v) {',
  6697. ' vG = v;',
  6698. ' }',
  6699. ' });',
  6700. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6701. ' get: function () {',
  6702. ' return vJ;',
  6703. ' },',
  6704. ' set: function (v) {',
  6705. ' vJ = v;',
  6706. ' }',
  6707. ' });',
  6708. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6709. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6710. ' get: function () {',
  6711. ' return vJ;',
  6712. ' },',
  6713. ' set: function (v) {',
  6714. ' vJ = v;',
  6715. ' }',
  6716. ' });',
  6717. ' return Result;',
  6718. '};',
  6719. 'this.i = {};'
  6720. ]),
  6721. LinesToStr([
  6722. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6723. ' p: $mod,',
  6724. ' get: function () {',
  6725. ' return this.p.i;',
  6726. ' },',
  6727. ' set: function (v) {',
  6728. ' this.p.i = v;',
  6729. ' }',
  6730. '});'
  6731. ]));
  6732. end;
  6733. procedure TTestModule.TestSet_Property;
  6734. begin
  6735. StartProgram(false);
  6736. Add('type');
  6737. Add(' TEnum = (Red,Blue);');
  6738. Add(' TEnums = set of TEnum;');
  6739. Add(' TObject = class');
  6740. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6741. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6742. Add(' property Colors: TEnums read GetColors write SetColors;');
  6743. Add(' end;');
  6744. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6745. Add('begin end;');
  6746. Add('var Obj: TObject;');
  6747. Add('begin');
  6748. Add(' Include(Obj.Colors,Red);');
  6749. Add(' Exclude(Obj.Colors,Red);');
  6750. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6751. ConvertProgram;
  6752. CheckSource('TestSet_Property',
  6753. LinesToStr([ // statements
  6754. 'this.TEnum = {',
  6755. ' "0": "Red",',
  6756. ' Red: 0,',
  6757. ' "1": "Blue",',
  6758. ' Blue: 1',
  6759. '};',
  6760. 'rtl.createClass(this, "TObject", null, function () {',
  6761. ' this.$init = function () {',
  6762. ' };',
  6763. ' this.$final = function () {',
  6764. ' };',
  6765. '});',
  6766. 'this.DoIt = function (i, j, k, l) {',
  6767. '};',
  6768. 'this.Obj = null;',
  6769. '']),
  6770. LinesToStr([
  6771. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6772. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6773. '']));
  6774. end;
  6775. procedure TTestModule.TestSet_EnumConst;
  6776. begin
  6777. StartProgram(false);
  6778. Add([
  6779. 'type',
  6780. ' TEnum = (Red,Blue);',
  6781. ' TEnums = set of TEnum;',
  6782. 'const',
  6783. ' Orange = red;',
  6784. 'var',
  6785. ' Enum: tenum;',
  6786. ' Enums: tenums;',
  6787. 'begin',
  6788. ' Include(enums,orange);',
  6789. ' Exclude(enums,orange);',
  6790. ' if orange in enums then;',
  6791. ' if orange in [orange,red] then;']);
  6792. ConvertProgram;
  6793. CheckSource('TestSet_EnumConst',
  6794. LinesToStr([ // statements
  6795. 'this.TEnum = {',
  6796. ' "0": "Red",',
  6797. ' Red: 0,',
  6798. ' "1": "Blue",',
  6799. ' Blue: 1',
  6800. '};',
  6801. 'this.Orange = this.TEnum.Red;',
  6802. 'this.Enum = 0;',
  6803. 'this.Enums = {};',
  6804. '']),
  6805. LinesToStr([
  6806. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6807. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6808. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6809. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6810. '']));
  6811. end;
  6812. procedure TTestModule.TestSet_IntConst;
  6813. begin
  6814. StartProgram(false);
  6815. Add([
  6816. 'type',
  6817. ' TEnums = set of Byte;',
  6818. 'const',
  6819. ' Orange = 0;',
  6820. 'var',
  6821. ' Enum: byte;',
  6822. ' Enums: tenums;',
  6823. 'begin',
  6824. ' Enums:=[];',
  6825. ' Enums:=[0];',
  6826. ' Enums:=[1..2];',
  6827. //' Include(enums,orange);',
  6828. //' Exclude(enums,orange);',
  6829. ' if orange in enums then;',
  6830. ' if orange in [orange,1] then;']);
  6831. ConvertProgram;
  6832. CheckSource('TestSet_IntConst',
  6833. LinesToStr([ // statements
  6834. 'this.Orange = 0;',
  6835. 'this.Enum = 0;',
  6836. 'this.Enums = {};',
  6837. '']),
  6838. LinesToStr([
  6839. '$mod.Enums = {};',
  6840. '$mod.Enums = rtl.createSet(0);',
  6841. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6842. 'if (0 in $mod.Enums) ;',
  6843. 'if (0 in rtl.createSet(0, 1)) ;',
  6844. '']));
  6845. end;
  6846. procedure TTestModule.TestSet_IntRange;
  6847. begin
  6848. StartProgram(false);
  6849. Add([
  6850. 'type',
  6851. ' TRange = 1..3;',
  6852. ' TEnums = set of TRange;',
  6853. 'const',
  6854. ' Orange = 2;',
  6855. 'var',
  6856. ' Enum: byte;',
  6857. ' Enums: TEnums;',
  6858. 'begin',
  6859. ' Enums:=[];',
  6860. ' Enums:=[1];',
  6861. ' Enums:=[2..3];',
  6862. ' Include(enums,orange);',
  6863. ' Exclude(enums,orange);',
  6864. ' if orange in enums then;',
  6865. ' if orange in [orange,1] then;']);
  6866. ConvertProgram;
  6867. CheckSource('TestSet_IntRange',
  6868. LinesToStr([ // statements
  6869. 'this.Orange = 2;',
  6870. 'this.Enum = 0;',
  6871. 'this.Enums = {};',
  6872. '']),
  6873. LinesToStr([
  6874. '$mod.Enums = {};',
  6875. '$mod.Enums = rtl.createSet(1);',
  6876. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6877. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6878. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6879. 'if (2 in $mod.Enums) ;',
  6880. 'if (2 in rtl.createSet(2, 1)) ;',
  6881. '']));
  6882. end;
  6883. procedure TTestModule.TestSet_AnonymousEnumType;
  6884. begin
  6885. StartProgram(false);
  6886. Add('type');
  6887. Add(' TFlags = set of (red, green);');
  6888. Add('const');
  6889. Add(' favorite = red;');
  6890. Add('var');
  6891. Add(' f: TFlags;');
  6892. Add(' i: longint;');
  6893. Add('begin');
  6894. Add(' Include(f,red);');
  6895. Add(' Include(f,favorite);');
  6896. Add(' i:=ord(red);');
  6897. Add(' i:=ord(favorite);');
  6898. Add(' i:=ord(low(TFlags));');
  6899. Add(' i:=ord(low(f));');
  6900. Add(' i:=ord(low(favorite));');
  6901. Add(' i:=ord(high(TFlags));');
  6902. Add(' i:=ord(high(f));');
  6903. Add(' i:=ord(high(favorite));');
  6904. Add(' f:=[green,favorite];');
  6905. ConvertProgram;
  6906. CheckSource('TestSet_AnonymousEnumType',
  6907. LinesToStr([ // statements
  6908. 'this.TFlags$a = {',
  6909. ' "0": "red",',
  6910. ' red: 0,',
  6911. ' "1": "green",',
  6912. ' green: 1',
  6913. '};',
  6914. 'this.favorite = this.TFlags$a.red;',
  6915. 'this.f = {};',
  6916. 'this.i = 0;',
  6917. '']),
  6918. LinesToStr([
  6919. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6920. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6921. '$mod.i = $mod.TFlags$a.red;',
  6922. '$mod.i = $mod.TFlags$a.red;',
  6923. '$mod.i = $mod.TFlags$a.red;',
  6924. '$mod.i = $mod.TFlags$a.red;',
  6925. '$mod.i = $mod.TFlags$a.red;',
  6926. '$mod.i = $mod.TFlags$a.green;',
  6927. '$mod.i = $mod.TFlags$a.green;',
  6928. '$mod.i = $mod.TFlags$a.green;',
  6929. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6930. '']));
  6931. end;
  6932. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6933. begin
  6934. exit;
  6935. StartProgram(false);
  6936. Add([
  6937. 'type',
  6938. ' TAtoZ = ''A''..''Z'';',
  6939. ' TSetOfAZ = set of TAtoZ;',
  6940. 'var',
  6941. ' c: char;',
  6942. ' a: TAtoZ;',
  6943. ' s: TSetOfAZ = [''P'',''A''];',
  6944. ' i: longint;',
  6945. 'begin',
  6946. ' Include(s,''S'');',
  6947. ' Include(s,c);',
  6948. ' Include(s,a);',
  6949. ' c:=low(TAtoZ);',
  6950. ' i:=ord(low(TAtoZ));',
  6951. ' a:=high(TAtoZ);',
  6952. ' a:=high(TSetOfAtoZ);',
  6953. ' s:=[a,c,''M''];',
  6954. '']);
  6955. ConvertProgram;
  6956. CheckSource('TestSet_AnonymousEnumTypeChar',
  6957. LinesToStr([ // statements
  6958. '']),
  6959. LinesToStr([
  6960. '']));
  6961. end;
  6962. procedure TTestModule.TestSet_ConstEnum;
  6963. begin
  6964. StartProgram(false);
  6965. Add([
  6966. 'type',
  6967. ' TEnum = (red,blue,green);',
  6968. ' TEnums = set of TEnum;',
  6969. 'const',
  6970. ' teAny = [low(TEnum)..high(TEnum)];',
  6971. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6972. 'var',
  6973. ' e: TEnum;',
  6974. ' s: TEnums;',
  6975. 'begin',
  6976. ' if blue in teAny then;',
  6977. ' if blue in teAny+[e] then;',
  6978. ' if blue in teAny+teRedBlue then;',
  6979. ' if e in [red,blue] then;',
  6980. ' s:=teAny;',
  6981. ' s:=teAny+[e];',
  6982. ' s:=[e]+teAny;',
  6983. ' s:=teAny+teRedBlue;',
  6984. ' s:=teAny+teRedBlue+[e];',
  6985. '']);
  6986. ConvertProgram;
  6987. CheckSource('TestSet_ConstEnum',
  6988. LinesToStr([ // statements
  6989. 'this.TEnum = {',
  6990. ' "0": "red",',
  6991. ' red: 0,',
  6992. ' "1": "blue",',
  6993. ' blue: 1,',
  6994. ' "2": "green",',
  6995. ' green: 2',
  6996. '};',
  6997. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6998. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6999. 'this.e = 0;',
  7000. 'this.s = {};',
  7001. '']),
  7002. LinesToStr([
  7003. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7004. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7005. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7006. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7007. '$mod.s = rtl.refSet($mod.teAny);',
  7008. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7009. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7010. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7011. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7012. '']));
  7013. end;
  7014. procedure TTestModule.TestSet_ConstChar;
  7015. begin
  7016. StartProgram(false);
  7017. Add([
  7018. 'const',
  7019. ' LowChars = [''a''..''z''];',
  7020. ' Chars = LowChars+[''A''..''Z''];',
  7021. ' sc = [''А'', ''Я''];',
  7022. 'var',
  7023. ' c: char;',
  7024. ' s: string;',
  7025. 'begin',
  7026. ' if c in lowchars then ;',
  7027. ' if ''a'' in lowchars then ;',
  7028. ' if s[1] in lowchars then ;',
  7029. ' if c in chars then ;',
  7030. ' if c in [''a''..''z'',''_''] then ;',
  7031. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7032. ' if ''Я'' in sc then ;',
  7033. ' if 3=ord('' '') then ;',
  7034. '']);
  7035. ConvertProgram;
  7036. CheckSource('TestSet_ConstChar',
  7037. LinesToStr([ // statements
  7038. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7039. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7040. 'this.sc = rtl.createSet(1040, 1071);',
  7041. 'this.c = "";',
  7042. 'this.s = "";',
  7043. '']),
  7044. LinesToStr([
  7045. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7046. 'if (97 in $mod.LowChars) ;',
  7047. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7048. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7049. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7050. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7051. 'if (1071 in $mod.sc) ;',
  7052. 'if (3 === 32) ;',
  7053. '']));
  7054. end;
  7055. procedure TTestModule.TestSet_ConstInt;
  7056. begin
  7057. StartProgram(false);
  7058. Add([
  7059. 'const',
  7060. ' Months = [1..12];',
  7061. ' Mirror = [-12..-1]+Months;',
  7062. 'var',
  7063. ' i: smallint;',
  7064. 'begin',
  7065. ' if 3 in Months then;',
  7066. ' if i in Months+[i] then;',
  7067. ' if i in Months+Mirror then;',
  7068. ' if i in [4..6,8] then;',
  7069. '']);
  7070. ConvertProgram;
  7071. CheckSource('TestSet_ConstInt',
  7072. LinesToStr([ // statements
  7073. 'this.Months = rtl.createSet(null, 1, 12);',
  7074. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7075. 'this.i = 0;',
  7076. '']),
  7077. LinesToStr([
  7078. 'if (3 in $mod.Months) ;',
  7079. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7080. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7081. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7082. '']));
  7083. end;
  7084. procedure TTestModule.TestSet_InFunction;
  7085. begin
  7086. StartProgram(false);
  7087. Add([
  7088. 'const',
  7089. ' TEnum = 3;',
  7090. ' TSetOfEnum = 4;',
  7091. ' TSetOfAno = 5;',
  7092. 'procedure DoIt;',
  7093. 'type',
  7094. ' TEnum = (red, blue);',
  7095. ' TSetOfEnum = set of TEnum;',
  7096. ' TSetOfAno = set of (up,down);',
  7097. 'var',
  7098. ' e: TEnum;',
  7099. ' se: TSetOfEnum;',
  7100. ' sa: TSetOfAno;',
  7101. 'begin',
  7102. ' se:=[e];',
  7103. ' sa:=[up];',
  7104. 'end;',
  7105. 'begin',
  7106. '']);
  7107. ConvertProgram;
  7108. CheckSource('TestSet_InFunction',
  7109. LinesToStr([ // statements
  7110. 'this.TEnum = 3;',
  7111. 'this.TSetOfEnum = 4;',
  7112. 'this.TSetOfAno = 5;',
  7113. 'var TEnum$1 = {',
  7114. ' "0": "red",',
  7115. ' red: 0,',
  7116. ' "1": "blue",',
  7117. ' blue: 1',
  7118. '};',
  7119. 'var TSetOfAno$a = {',
  7120. ' "0": "up",',
  7121. ' up: 0,',
  7122. ' "1": "down",',
  7123. ' down: 1',
  7124. '};',
  7125. 'this.DoIt = function () {',
  7126. ' var e = 0;',
  7127. ' var se = {};',
  7128. ' var sa = {};',
  7129. ' se = rtl.createSet(e);',
  7130. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7131. '};',
  7132. '']),
  7133. LinesToStr([
  7134. '']));
  7135. end;
  7136. procedure TTestModule.TestSet_ForIn;
  7137. begin
  7138. StartProgram(false);
  7139. Add([
  7140. 'type',
  7141. ' TEnum = (Red, Green, Blue);',
  7142. ' TEnumRg = green..blue;',
  7143. ' TSetOfEnum = set of TEnum;',
  7144. ' TSetOfEnumRg = set of TEnumRg;',
  7145. 'var',
  7146. ' e, e2: TEnum;',
  7147. ' er: TEnum;',
  7148. ' s: TSetOfEnum;',
  7149. 'begin',
  7150. ' for e in TSetOfEnum do ;',
  7151. ' for e in TSetOfEnumRg do ;',
  7152. ' for e in [] do e2:=e;',
  7153. ' for e in [red..green] do e2:=e;',
  7154. ' for e in [green,blue] do e2:=e;',
  7155. ' for e in [red,blue] do e2:=e;',
  7156. ' for e in s do e2:=e;',
  7157. ' for er in TSetOfEnumRg do ;',
  7158. '']);
  7159. ConvertProgram;
  7160. CheckSource('TestSet_ForIn',
  7161. LinesToStr([ // statements
  7162. 'this.TEnum = {',
  7163. ' "0":"Red",',
  7164. ' Red:0,',
  7165. ' "1":"Green",',
  7166. ' Green:1,',
  7167. ' "2":"Blue",',
  7168. ' Blue:2',
  7169. ' };',
  7170. 'this.e = 0;',
  7171. 'this.e2 = 0;',
  7172. 'this.er = 0;',
  7173. 'this.s = {};',
  7174. '']),
  7175. LinesToStr([
  7176. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7177. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7178. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7179. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7180. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7181. 'for (var $l in $mod.s){',
  7182. ' $mod.e = +$l;',
  7183. ' $mod.e2 = $mod.e;',
  7184. '};',
  7185. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7186. '']));
  7187. end;
  7188. procedure TTestModule.TestNestBegin;
  7189. begin
  7190. StartProgram(false);
  7191. Add('begin');
  7192. Add(' begin');
  7193. Add(' begin');
  7194. Add(' end;');
  7195. Add(' begin');
  7196. Add(' if true then ;');
  7197. Add(' end;');
  7198. Add(' end;');
  7199. ConvertProgram;
  7200. CheckSource('TestNestBegin',
  7201. '',
  7202. 'if (true) ;');
  7203. end;
  7204. procedure TTestModule.TestUnitImplVars;
  7205. begin
  7206. StartUnit(false);
  7207. Add('interface');
  7208. Add('implementation');
  7209. Add('var');
  7210. Add(' V1:longint;');
  7211. Add(' V2:longint = 3;');
  7212. Add(' V3:string = ''abc'';');
  7213. ConvertUnit;
  7214. CheckSource('TestUnitImplVars',
  7215. LinesToStr([ // statements
  7216. 'var $impl = $mod.$impl;',
  7217. '']),
  7218. '', // this.$init
  7219. LinesToStr([ // implementation
  7220. '$impl.V1 = 0;',
  7221. '$impl.V2 = 3;',
  7222. '$impl.V3 = "abc";',
  7223. '']) );
  7224. end;
  7225. procedure TTestModule.TestUnitImplConsts;
  7226. begin
  7227. StartUnit(false);
  7228. Add('interface');
  7229. Add('implementation');
  7230. Add('const');
  7231. Add(' v1 = 3;');
  7232. Add(' v2:longint = 4;');
  7233. Add(' v3:string = ''abc'';');
  7234. ConvertUnit;
  7235. CheckSource('TestUnitImplConsts',
  7236. LinesToStr([ // statements
  7237. 'var $impl = $mod.$impl;',
  7238. '']),
  7239. '', // this.$init
  7240. LinesToStr([ // implementation
  7241. '$impl.v1 = 3;',
  7242. '$impl.v2 = 4;',
  7243. '$impl.v3 = "abc";',
  7244. '']) );
  7245. end;
  7246. procedure TTestModule.TestUnitImplRecord;
  7247. begin
  7248. StartUnit(false);
  7249. Add('interface');
  7250. Add('implementation');
  7251. Add('type');
  7252. Add(' TMyRecord = record');
  7253. Add(' i: longint;');
  7254. Add(' end;');
  7255. Add('var aRec: TMyRecord;');
  7256. Add('initialization');
  7257. Add(' arec.i:=3;');
  7258. ConvertUnit;
  7259. CheckSource('TestUnitImplRecord',
  7260. LinesToStr([ // statements
  7261. 'var $impl = $mod.$impl;',
  7262. '']),
  7263. // this.$init
  7264. '$impl.aRec.i = 3;',
  7265. LinesToStr([ // implementation
  7266. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7267. ' this.i = 0;',
  7268. ' this.$eq = function (b) {',
  7269. ' return this.i === b.i;',
  7270. ' };',
  7271. ' this.$assign = function (s) {',
  7272. ' this.i = s.i;',
  7273. ' return this;',
  7274. ' };',
  7275. '});',
  7276. '$impl.aRec = $impl.TMyRecord.$new();',
  7277. '']) );
  7278. end;
  7279. procedure TTestModule.TestRenameJSNameConflict;
  7280. begin
  7281. StartProgram(false);
  7282. Add('var apply: longint;');
  7283. Add('var bind: longint;');
  7284. Add('var call: longint;');
  7285. Add('begin');
  7286. ConvertProgram;
  7287. CheckSource('TestRenameJSNameConflict',
  7288. LinesToStr([ // statements
  7289. 'this.Apply = 0;',
  7290. 'this.Bind = 0;',
  7291. 'this.Call = 0;'
  7292. ]),
  7293. LinesToStr([ // this.$main
  7294. ''
  7295. ]));
  7296. end;
  7297. procedure TTestModule.TestLocalConst;
  7298. begin
  7299. StartProgram(false);
  7300. Add('procedure DoIt;');
  7301. Add('const');
  7302. Add(' cA: longint = 1;');
  7303. Add(' cB = 2;');
  7304. Add(' procedure Sub;');
  7305. Add(' const');
  7306. Add(' csA = 3;');
  7307. Add(' cB: double = 4;');
  7308. Add(' begin');
  7309. Add(' cb:=cb+csa;');
  7310. Add(' ca:=ca+csa+5;');
  7311. Add(' end;');
  7312. Add('begin');
  7313. Add(' ca:=ca+cb+6;');
  7314. Add('end;');
  7315. Add('begin');
  7316. ConvertProgram;
  7317. CheckSource('TestLocalConst',
  7318. LinesToStr([
  7319. 'var cA = 1;',
  7320. 'var cB = 2;',
  7321. 'var csA = 3;',
  7322. 'var cB$1 = 4;',
  7323. 'this.DoIt = function () {',
  7324. ' function Sub() {',
  7325. ' cB$1 = cB$1 + 3;',
  7326. ' cA = cA + 3 + 5;',
  7327. ' };',
  7328. ' cA = cA + 2 + 6;',
  7329. '};'
  7330. ]),
  7331. LinesToStr([
  7332. ]));
  7333. end;
  7334. procedure TTestModule.TestVarExternal;
  7335. begin
  7336. StartProgram(false);
  7337. Add('var');
  7338. Add(' NaN: double; external name ''Global.NaN'';');
  7339. Add(' d: double;');
  7340. Add('begin');
  7341. Add(' d:=NaN;');
  7342. ConvertProgram;
  7343. CheckSource('TestVarExternal',
  7344. LinesToStr([
  7345. 'this.d = 0.0;'
  7346. ]),
  7347. LinesToStr([
  7348. '$mod.d = Global.NaN;'
  7349. ]));
  7350. end;
  7351. procedure TTestModule.TestVarExternalOtherUnit;
  7352. begin
  7353. AddModuleWithIntfImplSrc('unit2.pas',
  7354. LinesToStr([
  7355. 'var NaN: double; external name ''Global.NaN'';',
  7356. 'var iV: longint;'
  7357. ]),
  7358. '');
  7359. StartUnit(true);
  7360. Add('interface');
  7361. Add('uses unit2;');
  7362. Add('implementation');
  7363. Add('var');
  7364. Add(' d: double;');
  7365. Add(' i: longint; external name ''$i'';');
  7366. Add('begin');
  7367. Add(' d:=nan;');
  7368. Add(' d:=uNit2.nan;');
  7369. Add(' d:=test1.d;');
  7370. Add(' i:=iv;');
  7371. Add(' i:=uNit2.iv;');
  7372. Add(' i:=test1.i;');
  7373. ConvertUnit;
  7374. CheckSource('TestVarExternalOtherUnit',
  7375. LinesToStr([
  7376. 'var $impl = $mod.$impl;',
  7377. '']),
  7378. LinesToStr([ // this.$init
  7379. '$impl.d = Global.NaN;',
  7380. '$impl.d = Global.NaN;',
  7381. '$impl.d = $impl.d;',
  7382. '$i = pas.unit2.iV;',
  7383. '$i = pas.unit2.iV;',
  7384. '$i = $i;',
  7385. '']),
  7386. LinesToStr([ // implementation
  7387. '$impl.d = 0.0;',
  7388. '']) );
  7389. end;
  7390. procedure TTestModule.TestVarAbsoluteFail;
  7391. begin
  7392. StartProgram(false);
  7393. Add([
  7394. 'var',
  7395. ' a: longint;',
  7396. ' b: longword absolute a;',
  7397. 'begin']);
  7398. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7399. ConvertProgram;
  7400. end;
  7401. procedure TTestModule.TestConstExternal;
  7402. begin
  7403. StartProgram(false);
  7404. Add([
  7405. 'const',
  7406. ' PI: double; external name ''Global.PI'';',
  7407. ' Tau = 2*pi;',
  7408. 'var d: double;',
  7409. 'begin',
  7410. ' d:=pi;',
  7411. ' d:=tau+pi;']);
  7412. ConvertProgram;
  7413. CheckSource('TestConstExternal',
  7414. LinesToStr([
  7415. 'this.Tau = 2*Global.PI;',
  7416. 'this.d = 0.0;'
  7417. ]),
  7418. LinesToStr([
  7419. '$mod.d = Global.PI;',
  7420. '$mod.d = $mod.Tau + Global.PI;'
  7421. ]));
  7422. end;
  7423. procedure TTestModule.TestDouble;
  7424. begin
  7425. StartProgram(false);
  7426. Add([
  7427. 'type',
  7428. ' TDateTime = double;',
  7429. 'const',
  7430. ' a = TDateTime(2.7);',
  7431. ' b = a + TDateTime(1.7);',
  7432. ' c = 0.9 + 0.1;',
  7433. ' f0_1 = 0.1;',
  7434. ' f0_3 = 0.3;',
  7435. ' fn0_1 = -0.1;',
  7436. ' fn0_3 = -0.3;',
  7437. ' fn0_003 = -0.003;',
  7438. ' fn0_123456789 = -0.123456789;',
  7439. ' fn300_0 = -300.0;',
  7440. ' fn123456_0 = -123456.0;',
  7441. ' fn1234567_8 = -1234567.8;',
  7442. ' fn12345678_9 = -12345678.9;',
  7443. ' f1_0En12 = 1E-12;',
  7444. ' fn1_0En12 = -1E-12;',
  7445. ' maxdouble = 1.7e+308;',
  7446. ' mindouble = -1.7e+308;',
  7447. ' MinSafeIntDouble = -$1fffffffffffff;',
  7448. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7449. ' MaxSafeIntDouble = $1fffffffffffff;',
  7450. ' DZeroResolution = 1E-12;',
  7451. ' Minus1 = -1E-12;',
  7452. ' EPS = 1E-9;',
  7453. ' DELTA = 0.001;',
  7454. ' Big = 129.789E+100;',
  7455. ' Test0_15 = 0.15;',
  7456. ' Test999 = 2.9999999999999;',
  7457. ' Test111999 = 211199999999999000.0;',
  7458. ' TestMinus111999 = -211199999999999000.0;',
  7459. ' Inf = 1.0 / 0.0;',
  7460. ' NegInf = -1.0 / 0.0;',
  7461. 'procedure Run(d: double); external name ''Run'';',
  7462. 'var',
  7463. ' d: double = b;',
  7464. 'begin',
  7465. ' d:=1.0;',
  7466. ' d:=1.0/3.0;',
  7467. ' d:=1.0/(3-2-1);',
  7468. ' d:=1/3;',
  7469. ' d:=5.0E-324;',
  7470. ' d:=1.7E308;',
  7471. ' d:=001.00E00;',
  7472. ' d:=002.00E001;',
  7473. ' d:=003.000E000;',
  7474. ' d:=-004.00E-00;',
  7475. ' d:=-005.00E-001;',
  7476. ' d:=10**3;',
  7477. ' d:=10 mod 3;',
  7478. ' d:=10 div 3;',
  7479. ' d:=c;',
  7480. ' d:=f0_1;',
  7481. ' d:=f0_3;',
  7482. ' d:=fn0_1;',
  7483. ' d:=fn0_3;',
  7484. ' d:=fn0_003;',
  7485. ' d:=fn0_123456789;',
  7486. ' d:=fn300_0;',
  7487. ' d:=fn123456_0;',
  7488. ' d:=fn1234567_8;',
  7489. ' d:=fn12345678_9;',
  7490. ' d:=f1_0En12;',
  7491. ' d:=fn1_0En12;',
  7492. ' d:=maxdouble;',
  7493. ' d:=mindouble;',
  7494. ' d:=MinSafeIntDouble;',
  7495. ' d:=double(MinSafeIntDouble);',
  7496. ' d:=MinSafeIntDouble2;',
  7497. ' d:=double(MinSafeIntDouble2);',
  7498. ' d:=MaxSafeIntDouble;',
  7499. ' d:=default(double);',
  7500. ' Run(Inf);',
  7501. ' Run(NegInf);',
  7502. '']);
  7503. ConvertProgram;
  7504. CheckSource('TestDouble',
  7505. LinesToStr([
  7506. 'this.a = 2.7;',
  7507. 'this.b = 2.7 + 1.7;',
  7508. 'this.c = 0.9 + 0.1;',
  7509. 'this.f0_1 = 0.1;',
  7510. 'this.f0_3 = 0.3;',
  7511. 'this.fn0_1 = -0.1;',
  7512. 'this.fn0_3 = -0.3;',
  7513. 'this.fn0_003 = -0.003;',
  7514. 'this.fn0_123456789 = -0.123456789;',
  7515. 'this.fn300_0 = -300.0;',
  7516. 'this.fn123456_0 = -123456.0;',
  7517. 'this.fn1234567_8 = -1234567.8;',
  7518. 'this.fn12345678_9 = -12345678.9;',
  7519. 'this.f1_0En12 = 1E-12;',
  7520. 'this.fn1_0En12 = -1E-12;',
  7521. 'this.maxdouble = 1.7e+308;',
  7522. 'this.mindouble = -1.7e+308;',
  7523. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7524. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7525. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7526. 'this.DZeroResolution = 1E-12;',
  7527. 'this.Minus1 = -1E-12;',
  7528. 'this.EPS = 1E-9;',
  7529. 'this.DELTA = 0.001;',
  7530. 'this.Big = 129.789E+100;',
  7531. 'this.Test0_15 = 0.15;',
  7532. 'this.Test999 = 2.9999999999999;',
  7533. 'this.Test111999 = 211199999999999000.0;',
  7534. 'this.TestMinus111999 = -211199999999999000.0;',
  7535. 'this.Inf = 1.0 / 0.0;',
  7536. 'this.NegInf = -1.0 / 0.0;',
  7537. 'this.d = 4.4;',
  7538. '']),
  7539. LinesToStr([
  7540. '$mod.d = 1.0;',
  7541. '$mod.d = 1.0 / 3.0;',
  7542. '$mod.d = 1.0 / (3 - 2 - 1);',
  7543. '$mod.d = 1 / 3;',
  7544. '$mod.d = 5.0E-324;',
  7545. '$mod.d = 1.7E308;',
  7546. '$mod.d = 1.00E0;',
  7547. '$mod.d = 2.00E1;',
  7548. '$mod.d = 3.000E0;',
  7549. '$mod.d = -4.00E-0;',
  7550. '$mod.d = -5.00E-1;',
  7551. '$mod.d = Math.pow(10, 3);',
  7552. '$mod.d = 10 % 3;',
  7553. '$mod.d = rtl.trunc(10 / 3);',
  7554. '$mod.d = 1;',
  7555. '$mod.d = 0.1;',
  7556. '$mod.d = 0.3;',
  7557. '$mod.d = -0.1;',
  7558. '$mod.d = -0.3;',
  7559. '$mod.d = -0.003;',
  7560. '$mod.d = -0.123456789;',
  7561. '$mod.d = -300;',
  7562. '$mod.d = -123456;',
  7563. '$mod.d = -1234567.8;',
  7564. '$mod.d = -1.23456789E7;',
  7565. '$mod.d = 1E-12;',
  7566. '$mod.d = -1E-12;',
  7567. '$mod.d = 1.7E308;',
  7568. '$mod.d = -1.7E308;',
  7569. '$mod.d = -9007199254740991;',
  7570. '$mod.d = -9007199254740991;',
  7571. '$mod.d = -9.007199254740992E15;',
  7572. '$mod.d = -9.007199254740992E15;',
  7573. '$mod.d = 9007199254740991;',
  7574. '$mod.d = 0.0;',
  7575. 'Run(1 / 0);',
  7576. 'Run(-1 / 0);',
  7577. '']));
  7578. end;
  7579. procedure TTestModule.TestInteger;
  7580. begin
  7581. StartProgram(false);
  7582. Add([
  7583. 'const',
  7584. ' MinInt = low(NativeInt);',
  7585. ' MaxInt = high(NativeInt);',
  7586. 'type',
  7587. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7588. 'const',
  7589. ' a = low(TMyInt)+High(TMyInt);',
  7590. 'var',
  7591. ' i: TMyInt;',
  7592. 'begin',
  7593. ' i:=-MinInt;',
  7594. ' i:=default(TMyInt);',
  7595. ' i:=low(i)+high(i);',
  7596. '']);
  7597. ConvertProgram;
  7598. CheckSource('TestIntegerRange',
  7599. LinesToStr([
  7600. 'this.MinInt = -9007199254740991;',
  7601. 'this.MaxInt = 9007199254740991;',
  7602. 'this.a = -9007199254740991 + 9007199254740991;',
  7603. 'this.i = 0;',
  7604. '']),
  7605. LinesToStr([
  7606. '$mod.i = - -9007199254740991;',
  7607. '$mod.i = -9007199254740991;',
  7608. '$mod.i = -9007199254740991 + 9007199254740991;',
  7609. '']));
  7610. end;
  7611. procedure TTestModule.TestIntegerRange;
  7612. begin
  7613. StartProgram(false);
  7614. Add([
  7615. 'const',
  7616. ' MinInt = -1;',
  7617. ' MaxInt = +1;',
  7618. 'type',
  7619. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7620. ' TInt2 = 1..3;',
  7621. 'const',
  7622. ' a = low(TMyInt)+High(TMyInt);',
  7623. ' b = low(TInt2)+High(TInt2);',
  7624. ' s1 = [1];',
  7625. ' s2 = [1,2];',
  7626. ' s3 = [1..3];',
  7627. ' s4 = [low(shortint)..high(shortint)];',
  7628. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7629. ' s6 = 1 in s2;',
  7630. 'var',
  7631. ' i: TMyInt;',
  7632. ' i2: TInt2;',
  7633. 'begin',
  7634. ' i:=i2;',
  7635. ' i:=default(TMyInt);',
  7636. ' if i=i2 then ;',
  7637. ' i:=ord(i2);',
  7638. '']);
  7639. ConvertProgram;
  7640. CheckSource('TestIntegerRange',
  7641. LinesToStr([
  7642. 'this.MinInt = -1;',
  7643. 'this.MaxInt = +1;',
  7644. 'this.a = -1 + 1;',
  7645. 'this.b = 1 + 3;',
  7646. 'this.s1 = rtl.createSet(1);',
  7647. 'this.s2 = rtl.createSet(1, 2);',
  7648. 'this.s3 = rtl.createSet(null, 1, 3);',
  7649. 'this.s4 = rtl.createSet(null, -128, 127);',
  7650. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7651. 'this.s6 = 1 in this.s2;',
  7652. 'this.i = 0;',
  7653. 'this.i2 = 0;',
  7654. '']),
  7655. LinesToStr([
  7656. '$mod.i = $mod.i2;',
  7657. '$mod.i = -1;',
  7658. 'if ($mod.i === $mod.i2) ;',
  7659. '$mod.i = $mod.i2;',
  7660. '']));
  7661. end;
  7662. procedure TTestModule.TestIntegerTypecasts;
  7663. begin
  7664. StartProgram(false);
  7665. Add([
  7666. 'var',
  7667. ' i: nativeint;',
  7668. ' b: byte;',
  7669. ' sh: shortint;',
  7670. ' w: word;',
  7671. ' sm: smallint;',
  7672. ' lw: longword;',
  7673. ' li: longint;',
  7674. 'begin',
  7675. ' b:=byte(i);',
  7676. ' sh:=shortint(i);',
  7677. ' w:=word(i);',
  7678. ' sm:=smallint(i);',
  7679. ' lw:=longword(i);',
  7680. ' li:=longint(i);',
  7681. '']);
  7682. ConvertProgram;
  7683. CheckSource('TestIntegerTypecasts',
  7684. LinesToStr([
  7685. 'this.i = 0;',
  7686. 'this.b = 0;',
  7687. 'this.sh = 0;',
  7688. 'this.w = 0;',
  7689. 'this.sm = 0;',
  7690. 'this.lw = 0;',
  7691. 'this.li = 0;',
  7692. '']),
  7693. LinesToStr([
  7694. '$mod.b = $mod.i & 255;',
  7695. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7696. '$mod.w = $mod.i & 65535;',
  7697. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7698. '$mod.lw = $mod.i >>> 0;',
  7699. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7700. '']));
  7701. end;
  7702. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7703. begin
  7704. StartProgram(false);
  7705. Add([
  7706. 'var',
  7707. ' i,j: nativeint;',
  7708. 'begin',
  7709. ' i:=i shr 0;',
  7710. ' i:=i shr 1;',
  7711. ' i:=i shr 3;',
  7712. ' i:=i shr 54;',
  7713. ' i:=j shr i;',
  7714. '']);
  7715. ConvertProgram;
  7716. CheckResolverUnexpectedHints;
  7717. CheckSource('TestInteger_BitwiseShrNativeInt',
  7718. LinesToStr([
  7719. 'this.i = 0;',
  7720. 'this.j = 0;',
  7721. '']),
  7722. LinesToStr([
  7723. '$mod.i = $mod.i;',
  7724. '$mod.i = Math.floor($mod.i / 2);',
  7725. '$mod.i = Math.floor($mod.i / 8);',
  7726. '$mod.i = 0;',
  7727. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7728. '']));
  7729. end;
  7730. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7731. begin
  7732. StartProgram(false);
  7733. Add([
  7734. 'var',
  7735. ' i: nativeint;',
  7736. 'begin',
  7737. ' i:=i shl 0;',
  7738. ' i:=i shl 54;',
  7739. ' i:=123456789012 shl 1;',
  7740. ' i:=i shl 1;',
  7741. '']);
  7742. ConvertProgram;
  7743. CheckResolverUnexpectedHints;
  7744. CheckSource('TestInteger_BitwiseShrNativeInt',
  7745. LinesToStr([
  7746. 'this.i = 0;',
  7747. '']),
  7748. LinesToStr([
  7749. '$mod.i = $mod.i;',
  7750. '$mod.i = 0;',
  7751. '$mod.i = 246913578024;',
  7752. '$mod.i = rtl.shl($mod.i, 1);',
  7753. '']));
  7754. end;
  7755. procedure TTestModule.TestInteger_SystemFunc;
  7756. begin
  7757. StartProgram(true);
  7758. Add([
  7759. 'var',
  7760. ' i: byte;',
  7761. ' s: string;',
  7762. 'begin',
  7763. ' system.inc(i);',
  7764. ' system.str(i,s);',
  7765. ' s:=system.str(i);',
  7766. ' i:=system.low(i);',
  7767. ' i:=system.high(i);',
  7768. ' i:=system.pred(i);',
  7769. ' i:=system.succ(i);',
  7770. ' i:=system.ord(i);',
  7771. '']);
  7772. ConvertProgram;
  7773. CheckResolverUnexpectedHints;
  7774. CheckSource('TestInteger_SystemFunc',
  7775. LinesToStr([
  7776. 'this.i = 0;',
  7777. 'this.s = "";',
  7778. '']),
  7779. LinesToStr([
  7780. '$mod.i += 1;',
  7781. '$mod.s = "" + $mod.i;',
  7782. '$mod.s = "" + $mod.i;',
  7783. '$mod.i = 0;',
  7784. '$mod.i = 255;',
  7785. '$mod.i = $mod.i - 1;',
  7786. '$mod.i = $mod.i + 1;',
  7787. '$mod.i = $mod.i;',
  7788. '']));
  7789. end;
  7790. procedure TTestModule.TestInteger_AssignOutsideConst;
  7791. begin
  7792. StartProgram(false);
  7793. Add([
  7794. 'const',
  7795. ' MinInt = low(longint);',
  7796. ' MaxInt = high(longint);',
  7797. 'type',
  7798. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7799. 'var',
  7800. ' i: TMyInt;',
  7801. ' aByte: byte;',
  7802. ' aShortInt: shortint;',
  7803. ' aWord: word;',
  7804. ' aSmallInt: smallint;',
  7805. ' aLongWord: longword;',
  7806. ' aLongInt: longint;',
  7807. ' aNativeInt: nativeint;',
  7808. ' aNativeUInt: nativeuint;',
  7809. 'begin',
  7810. ' aByte:=$FF;',
  7811. ' aByte:=$100;',
  7812. ' aByte:=-1;',
  7813. ' aByte:=-127;',
  7814. ' aByte:=-128;',
  7815. ' aByte:=-254;',
  7816. ' aByte:=-255;',
  7817. ' aByte:=-256;',
  7818. ' aShortInt:=127;',
  7819. ' aShortInt:=128;',
  7820. ' aShortInt:=-128;',
  7821. ' aShortInt:=-129;',
  7822. ' aWord:=$ffff;',
  7823. ' aWord:=$10000;',
  7824. ' aWord:=-1;',
  7825. ' aWord:=-$ffff;',
  7826. ' aWord:=-$10000;',
  7827. ' aWord:=-$10001;',
  7828. ' aSmallInt:=$7fff;',
  7829. ' aSmallInt:=$8000;',
  7830. ' aSmallInt:=-$8000;',
  7831. ' aSmallInt:=-$8001;',
  7832. ' aLongWord:=$ffffffff;',
  7833. ' aLongWord:=$100000000;',
  7834. ' aLongWord:=-1;',
  7835. ' aLongWord:=-$ffffffff;',
  7836. ' aNativeInt:=$1fffffffffffff;',
  7837. ' aNativeInt:=-$1fffffffffffff;',
  7838. ' aNativeUInt:=$1fffffffffffff;',
  7839. ' aNativeUInt:=-$1fffffffffffff;',
  7840. '']);
  7841. ConvertProgram;
  7842. CheckSource('TestInteger_AssignOutsideConst',
  7843. LinesToStr([
  7844. 'this.MinInt = -2147483648;',
  7845. 'this.MaxInt = 2147483647;',
  7846. 'this.i = 0;',
  7847. 'this.aByte = 0;',
  7848. 'this.aShortInt = 0;',
  7849. 'this.aWord = 0;',
  7850. 'this.aSmallInt = 0;',
  7851. 'this.aLongWord = 0;',
  7852. 'this.aLongInt = 0;',
  7853. 'this.aNativeInt = 0;',
  7854. 'this.aNativeUInt = 0;',
  7855. '']),
  7856. LinesToStr([
  7857. '$mod.aByte = 0xFF;',
  7858. '$mod.aByte = 0;',
  7859. '$mod.aByte = 255;',
  7860. '$mod.aByte = 129;',
  7861. '$mod.aByte = 128;',
  7862. '$mod.aByte = 2;',
  7863. '$mod.aByte = 1;',
  7864. '$mod.aByte = 0;',
  7865. '$mod.aShortInt = 127;',
  7866. '$mod.aShortInt = -128;',
  7867. '$mod.aShortInt = -128;',
  7868. '$mod.aShortInt = 127;',
  7869. '$mod.aWord = 0xffff;',
  7870. '$mod.aWord = 0;',
  7871. '$mod.aWord = 65535;',
  7872. '$mod.aWord = 1;',
  7873. '$mod.aWord = 0;',
  7874. '$mod.aWord = 65535;',
  7875. '$mod.aSmallInt = 0x7fff;',
  7876. '$mod.aSmallInt = -32768;',
  7877. '$mod.aSmallInt = -0x8000;',
  7878. '$mod.aSmallInt = 32767;',
  7879. '$mod.aLongWord = 0xffffffff;',
  7880. '$mod.aLongWord = 0;',
  7881. '$mod.aLongWord = 4294967295;',
  7882. '$mod.aLongWord = 1;',
  7883. '$mod.aNativeInt = 0x1fffffffffffff;',
  7884. '$mod.aNativeInt = -0x1fffffffffffff;',
  7885. '$mod.aNativeUInt = 0x1fffffffffffff;',
  7886. '$mod.aNativeUInt = 1;',
  7887. '']));
  7888. end;
  7889. procedure TTestModule.TestCurrency;
  7890. begin
  7891. StartProgram(false);
  7892. Add([
  7893. 'type',
  7894. ' TCoin = currency;',
  7895. 'const',
  7896. ' a = TCoin(2.7);',
  7897. ' b = a + TCoin(1.7);',
  7898. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7899. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7900. 'var',
  7901. ' c: TCoin = b;',
  7902. ' i: nativeint;',
  7903. ' d: double;',
  7904. ' j: jsvalue;',
  7905. 'function DoIt(c: currency): currency; begin end;',
  7906. 'function GetIt(d: double): double; begin end;',
  7907. 'procedure Write(v: jsvalue); begin end;',
  7908. 'begin',
  7909. ' c:=1.0;',
  7910. ' c:=0.1;',
  7911. ' c:=1.0/3.0;',
  7912. ' c:=1/3;',
  7913. ' c:=a;',
  7914. ' d:=c;',
  7915. ' c:=d;',
  7916. ' c:=currency(c);',
  7917. ' c:=currency(d);',
  7918. ' d:=double(c);',
  7919. ' c:=i;',
  7920. ' c:=currency(i);',
  7921. //' i:=c;', not allowed
  7922. ' i:=nativeint(c);',
  7923. ' c:=c+a;',
  7924. ' c:=-c-a;',
  7925. ' c:=d+c;',
  7926. ' c:=c+d;',
  7927. ' c:=d-c;',
  7928. ' c:=c-d;',
  7929. ' c:=c*a;',
  7930. ' c:=a*c;',
  7931. ' c:=d*c;',
  7932. ' c:=c*d;',
  7933. ' c:=c/a;',
  7934. ' c:=a/c;',
  7935. ' c:=d/c;',
  7936. ' c:=c/d;',
  7937. ' c:=c**a;',
  7938. ' c:=a**c;',
  7939. ' c:=d**c;',
  7940. ' c:=c**d;',
  7941. ' if c=c then ;',
  7942. ' if c=a then ;',
  7943. ' if a=c then ;',
  7944. ' if d=c then ;',
  7945. ' if c=d then ;',
  7946. ' c:=DoIt(c);',
  7947. ' c:=DoIt(i);',
  7948. ' c:=DoIt(d);',
  7949. ' c:=GetIt(c);',
  7950. ' j:=c;',
  7951. ' Write(c);',
  7952. ' c:=default(currency);',
  7953. ' j:=str(c);',
  7954. ' j:=str(c:0:3);',
  7955. '']);
  7956. ConvertProgram;
  7957. CheckSource('TestCurrency',
  7958. LinesToStr([
  7959. 'this.a = 27000;',
  7960. 'this.b = this.a + 17000;',
  7961. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7962. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7963. 'this.c = this.b;',
  7964. 'this.i = 0;',
  7965. 'this.d = 0.0;',
  7966. 'this.j = undefined;',
  7967. 'this.DoIt = function (c) {',
  7968. ' var Result = 0;',
  7969. ' return Result;',
  7970. '};',
  7971. 'this.GetIt = function (d) {',
  7972. ' var Result = 0.0;',
  7973. ' return Result;',
  7974. '};',
  7975. 'this.Write = function (v) {',
  7976. '};',
  7977. '']),
  7978. LinesToStr([
  7979. '$mod.c = 10000;',
  7980. '$mod.c = 1000;',
  7981. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7982. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7983. '$mod.c = $mod.a;',
  7984. '$mod.d = $mod.c / 10000;',
  7985. '$mod.c = rtl.trunc($mod.d * 10000);',
  7986. '$mod.c = $mod.c;',
  7987. '$mod.c = $mod.d * 10000;',
  7988. '$mod.d = $mod.c / 10000;',
  7989. '$mod.c = $mod.i * 10000;',
  7990. '$mod.c = $mod.i * 10000;',
  7991. '$mod.i = rtl.trunc($mod.c / 10000);',
  7992. '$mod.c = $mod.c + $mod.a;',
  7993. '$mod.c = -$mod.c - $mod.a;',
  7994. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7995. '$mod.c = $mod.c + ($mod.d * 10000);',
  7996. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7997. '$mod.c = $mod.c - ($mod.d * 10000);',
  7998. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7999. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8000. '$mod.c = $mod.d * $mod.c;',
  8001. '$mod.c = $mod.c * $mod.d;',
  8002. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8003. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8004. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8005. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8006. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8007. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8008. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8009. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8010. 'if ($mod.c === $mod.c) ;',
  8011. 'if ($mod.c === $mod.a) ;',
  8012. 'if ($mod.a === $mod.c) ;',
  8013. 'if (($mod.d * 10000) === $mod.c) ;',
  8014. 'if ($mod.c === ($mod.d * 10000)) ;',
  8015. '$mod.c = $mod.DoIt($mod.c);',
  8016. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8017. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8018. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8019. '$mod.j = $mod.c / 10000;',
  8020. '$mod.Write($mod.c / 10000);',
  8021. '$mod.c = 0;',
  8022. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8023. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8024. '']));
  8025. end;
  8026. procedure TTestModule.TestForBoolDo;
  8027. begin
  8028. StartProgram(false);
  8029. Add([
  8030. 'var b: boolean;',
  8031. 'begin',
  8032. ' for b:=false to true do ;',
  8033. ' for b:=b downto false do ;',
  8034. ' for b in boolean do ;',
  8035. '']);
  8036. ConvertProgram;
  8037. CheckSource('TestForBoolDo',
  8038. LinesToStr([ // statements
  8039. 'this.b = false;']),
  8040. LinesToStr([ // this.$main
  8041. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8042. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8043. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8044. '']));
  8045. end;
  8046. procedure TTestModule.TestForIntDo;
  8047. begin
  8048. StartProgram(false);
  8049. Add([
  8050. 'var i: longint;',
  8051. 'begin',
  8052. ' for i:=3 to 5 do ;',
  8053. ' for i:=i downto 2 do ;',
  8054. ' for i in byte do ;',
  8055. '']);
  8056. ConvertProgram;
  8057. CheckSource('TestForIntDo',
  8058. LinesToStr([ // statements
  8059. 'this.i = 0;']),
  8060. LinesToStr([ // this.$main
  8061. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8062. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8063. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8064. '']));
  8065. end;
  8066. procedure TTestModule.TestForIntInDo;
  8067. begin
  8068. StartProgram(false);
  8069. Add([
  8070. 'type',
  8071. ' TSetOfInt = set of byte;',
  8072. ' TIntRg = 3..7;',
  8073. ' TSetOfIntRg = set of TIntRg;',
  8074. 'var',
  8075. ' i,i2: longint;',
  8076. ' a1: array of byte;',
  8077. ' a2: array[1..3] of byte;',
  8078. ' soi: TSetOfInt;',
  8079. ' soir: TSetOfIntRg;',
  8080. ' ir: TIntRg;',
  8081. 'begin',
  8082. ' for i in byte do ;',
  8083. ' for i in a1 do ;',
  8084. ' for i in a2 do ;',
  8085. ' for i in [11..13] do ;',
  8086. ' for i in TSetOfInt do ;',
  8087. ' for i in TIntRg do ;',
  8088. ' for i in soi do i2:=i;',
  8089. ' for i in TSetOfIntRg do ;',
  8090. ' for i in soir do ;',
  8091. ' for ir in TIntRg do ;',
  8092. ' for ir in TSetOfIntRg do ;',
  8093. ' for ir in soir do ;',
  8094. '']);
  8095. ConvertProgram;
  8096. CheckSource('TestForIntInDo',
  8097. LinesToStr([ // statements
  8098. 'this.i = 0;',
  8099. 'this.i2 = 0;',
  8100. 'this.a1 = [];',
  8101. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8102. 'this.soi = {};',
  8103. 'this.soir = {};',
  8104. 'this.ir = 0;',
  8105. '']),
  8106. LinesToStr([ // this.$main
  8107. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8108. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8109. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8110. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8111. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8112. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8113. 'for (var $l6 in $mod.soi) {',
  8114. ' $mod.i = +$l6;',
  8115. ' $mod.i2 = $mod.i;',
  8116. '};',
  8117. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8118. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8119. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8120. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8121. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8122. '']));
  8123. end;
  8124. procedure TTestModule.TestCharConst;
  8125. begin
  8126. StartProgram(false);
  8127. Add([
  8128. 'const',
  8129. ' a = #$00F3;',
  8130. ' c: char = ''1'';',
  8131. ' wc: widechar = ''ä'';',
  8132. 'begin',
  8133. ' c:=#0;',
  8134. ' c:=#1;',
  8135. ' c:=#9;',
  8136. ' c:=#10;',
  8137. ' c:=#13;',
  8138. ' c:=#31;',
  8139. ' c:=#32;',
  8140. ' c:=#$A;',
  8141. ' c:=#$0A;',
  8142. ' c:=#$b;',
  8143. ' c:=#$0b;',
  8144. ' c:=^A;',
  8145. ' c:=''"'';',
  8146. ' c:=default(char);',
  8147. ' c:=#$00E4;', // ä
  8148. ' c:=''ä'';',
  8149. ' c:=#$E4;', // ä
  8150. ' c:=#$D800;', // invalid UTF-16
  8151. ' c:=#$DFFF;', // invalid UTF-16
  8152. ' c:=#$FFFF;', // last UCS-2
  8153. ' c:=high(c);', // last UCS-2
  8154. ' c:=#269;',
  8155. '']);
  8156. ConvertProgram;
  8157. CheckSource('TestCharConst',
  8158. LinesToStr([
  8159. 'this.a="ó";',
  8160. 'this.c="1";',
  8161. 'this.wc="ä";'
  8162. ]),
  8163. LinesToStr([
  8164. '$mod.c="\x00";',
  8165. '$mod.c="\x01";',
  8166. '$mod.c="\t";',
  8167. '$mod.c="\n";',
  8168. '$mod.c="\r";',
  8169. '$mod.c="\x1F";',
  8170. '$mod.c=" ";',
  8171. '$mod.c="\n";',
  8172. '$mod.c="\n";',
  8173. '$mod.c="\x0B";',
  8174. '$mod.c="\x0B";',
  8175. '$mod.c="\x01";',
  8176. '$mod.c=''"'';',
  8177. '$mod.c="\x00";',
  8178. '$mod.c = "ä";',
  8179. '$mod.c = "ä";',
  8180. '$mod.c = "ä";',
  8181. '$mod.c="\uD800";',
  8182. '$mod.c="\uDFFF";',
  8183. '$mod.c="\uFFFF";',
  8184. '$mod.c="\uFFFF";',
  8185. '$mod.c = "č";',
  8186. '']));
  8187. end;
  8188. procedure TTestModule.TestChar_Compare;
  8189. begin
  8190. StartProgram(false);
  8191. Add('var');
  8192. Add(' c: char;');
  8193. Add(' b: boolean;');
  8194. Add('begin');
  8195. Add(' b:=c=''1'';');
  8196. Add(' b:=''2''=c;');
  8197. Add(' b:=''3''=''4'';');
  8198. Add(' b:=c<>''5'';');
  8199. Add(' b:=''6''<>c;');
  8200. Add(' b:=c>''7'';');
  8201. Add(' b:=''8''>c;');
  8202. Add(' b:=c>=''9'';');
  8203. Add(' b:=''A''>=c;');
  8204. Add(' b:=c<''B'';');
  8205. Add(' b:=''C''<c;');
  8206. Add(' b:=c<=''D'';');
  8207. Add(' b:=''E''<=c;');
  8208. ConvertProgram;
  8209. CheckSource('TestChar_Compare',
  8210. LinesToStr([
  8211. 'this.c="";',
  8212. 'this.b = false;'
  8213. ]),
  8214. LinesToStr([
  8215. '$mod.b = $mod.c === "1";',
  8216. '$mod.b = "2" === $mod.c;',
  8217. '$mod.b = "3" === "4";',
  8218. '$mod.b = $mod.c !== "5";',
  8219. '$mod.b = "6" !== $mod.c;',
  8220. '$mod.b = $mod.c > "7";',
  8221. '$mod.b = "8" > $mod.c;',
  8222. '$mod.b = $mod.c >= "9";',
  8223. '$mod.b = "A" >= $mod.c;',
  8224. '$mod.b = $mod.c < "B";',
  8225. '$mod.b = "C" < $mod.c;',
  8226. '$mod.b = $mod.c <= "D";',
  8227. '$mod.b = "E" <= $mod.c;',
  8228. '']));
  8229. end;
  8230. procedure TTestModule.TestChar_BuiltInProcs;
  8231. begin
  8232. StartProgram(false);
  8233. Add([
  8234. 'var',
  8235. ' c: char;',
  8236. ' i: longint;',
  8237. ' s: string;',
  8238. 'begin',
  8239. ' i:=ord(c);',
  8240. ' i:=ord(s[i]);',
  8241. ' c:=chr(i);',
  8242. ' c:=pred(c);',
  8243. ' c:=succ(c);',
  8244. ' c:=low(c);',
  8245. ' c:=high(c);',
  8246. ' i:=byte(c);',
  8247. ' i:=word(c);',
  8248. ' i:=longint(c);',
  8249. '']);
  8250. ConvertProgram;
  8251. CheckSource('TestChar_BuiltInProcs',
  8252. LinesToStr([
  8253. 'this.c = "";',
  8254. 'this.i = 0;',
  8255. 'this.s = "";'
  8256. ]),
  8257. LinesToStr([
  8258. '$mod.i = $mod.c.charCodeAt();',
  8259. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8260. '$mod.c = String.fromCharCode($mod.i);',
  8261. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8262. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8263. '$mod.c = "\x00";',
  8264. '$mod.c = "\uFFFF";',
  8265. '$mod.i = $mod.c.charCodeAt() & 255;',
  8266. '$mod.i = $mod.c.charCodeAt();',
  8267. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8268. '']));
  8269. end;
  8270. procedure TTestModule.TestStringConst;
  8271. begin
  8272. StartProgram(false);
  8273. Add([
  8274. '{$H+}',
  8275. 'const',
  8276. ' a = #$00F3#$017C;', // first <256, then >=256
  8277. ' b = string(''a'');',
  8278. ' c = string(''ä'');',
  8279. ' d = UnicodeString(''b'');',
  8280. ' e = UnicodeString(''ö'');',
  8281. ' f = low(a)+high(b);',
  8282. ' g: word = low(a);',
  8283. 'var',
  8284. ' s: string = ''abc'';',
  8285. ' i: longint;',
  8286. 'begin',
  8287. ' s:='''';',
  8288. ' s:=#13#10;',
  8289. ' s:=#9''foo'';',
  8290. ' s:=#$A9;',
  8291. ' s:=''foo''#13''bar'';',
  8292. ' s:=''"'';',
  8293. ' s:=''"''''"'';',
  8294. ' s:=#$20AC;', // euro
  8295. ' s:=#$10437;', // outside BMP
  8296. ' s:=''abc''#$20AC;', // ascii,#
  8297. ' s:=''ä''#$20AC;', // non ascii,#
  8298. ' s:=#$20AC''abc'';', // #, ascii
  8299. ' s:=#$20AC''ä'';', // #, non ascii
  8300. ' s:=default(string);',
  8301. ' s:=concat(s);',
  8302. ' s:=concat(s,''a'',s);',
  8303. ' s:=#250#269;',
  8304. ' i:=low(s)+high(a);',
  8305. //' s:=#$2F804;',
  8306. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8307. '']);
  8308. ConvertProgram;
  8309. CheckSource('TestStringConst',
  8310. LinesToStr([
  8311. 'this.a = "óż";',
  8312. 'this.b = "a";',
  8313. 'this.c = "ä";',
  8314. 'this.d = "b";',
  8315. 'this.e = "ö";',
  8316. 'this.f = 1 + this.b.length;',
  8317. 'this.g = 1;',
  8318. 'this.s="abc";',
  8319. 'this.i = 0;',
  8320. '']),
  8321. LinesToStr([
  8322. '$mod.s="";',
  8323. '$mod.s="\r\n";',
  8324. '$mod.s="\tfoo";',
  8325. '$mod.s="©";',
  8326. '$mod.s="foo\rbar";',
  8327. '$mod.s=''"'';',
  8328. '$mod.s=''"\''"'';',
  8329. '$mod.s="€";',
  8330. '$mod.s="'#$F0#$90#$90#$B7'";',
  8331. '$mod.s = "abc€";',
  8332. '$mod.s = "ä€";',
  8333. '$mod.s = "€abc";',
  8334. '$mod.s = "ۊ";',
  8335. '$mod.s="";',
  8336. '$mod.s = $mod.s;',
  8337. '$mod.s = $mod.s.concat("a", $mod.s);',
  8338. '$mod.s = "úč";',
  8339. '$mod.i = 1 + $mod.a.length;',
  8340. '']));
  8341. end;
  8342. procedure TTestModule.TestStringConst_InvalidUTF16;
  8343. begin
  8344. StartProgram(false);
  8345. Add([
  8346. 'const',
  8347. ' a: char = #$D87E;',
  8348. ' b: string = #$D87E;',
  8349. ' c: string = #$D87E#43;',
  8350. 'begin',
  8351. ' c:=''abc''#$D87E;',
  8352. ' c:=#0#1#2;',
  8353. ' c:=#127;',
  8354. ' c:=#128;',
  8355. ' c:=#255;',
  8356. ' c:=#256;',
  8357. '']);
  8358. ConvertProgram;
  8359. CheckSource('TestStringConst',
  8360. LinesToStr([
  8361. 'this.a = "\uD87E";',
  8362. 'this.b = "\uD87E";',
  8363. 'this.c = "\uD87E+";',
  8364. '']),
  8365. LinesToStr([
  8366. '$mod.c = "abc\uD87E";',
  8367. '$mod.c = "\x00\x01\x02";',
  8368. '$mod.c = "'#127'";',
  8369. '$mod.c = "'#$c2#$80'";',
  8370. '$mod.c = "'#$c3#$BF'";',
  8371. '$mod.c = "'#$c4#$80'";',
  8372. '']));
  8373. end;
  8374. procedure TTestModule.TestStringConstSurrogate;
  8375. begin
  8376. StartProgram(false);
  8377. Add([
  8378. 'var',
  8379. ' s: string;',
  8380. 'begin',
  8381. ' s:=''😊'';', // 1F60A
  8382. '']);
  8383. ConvertProgram;
  8384. CheckSource('TestStringConstSurrogate',
  8385. LinesToStr([
  8386. 'this.s="";'
  8387. ]),
  8388. LinesToStr([
  8389. '$mod.s="😊";'
  8390. ]));
  8391. end;
  8392. procedure TTestModule.TestStringConst_Multiline;
  8393. begin
  8394. StartProgram(false);
  8395. Add([
  8396. '{$modeswitch multilinestrings}',
  8397. 'const',
  8398. ' a = ``;',
  8399. ' b = `',
  8400. 'line`;',
  8401. ' c = `Single`;',
  8402. ' d = ````;',
  8403. ' e = `abc``xyz`;',
  8404. ' f = `first''line',
  8405. ' second''line`#10;',
  8406. 'begin',
  8407. '']);
  8408. ConvertProgram;
  8409. CheckSource('TestStringConst_Multiline',
  8410. LinesToStr([
  8411. 'this.a = "";',
  8412. 'this.b = "\nline";',
  8413. 'this.c = "Single";',
  8414. 'this.d = "`";',
  8415. 'this.e = "abc`xyz";',
  8416. 'this.f = "first''line\n second''line\n";',
  8417. '']),
  8418. LinesToStr([
  8419. ]));
  8420. end;
  8421. procedure TTestModule.TestString_Length;
  8422. begin
  8423. StartProgram(false);
  8424. Add('const c = ''foo'';');
  8425. Add('var');
  8426. Add(' s: string;');
  8427. Add(' i: longint;');
  8428. Add('begin');
  8429. Add(' i:=length(s);');
  8430. Add(' i:=length(s+s);');
  8431. Add(' i:=length(''abc'');');
  8432. Add(' i:=length(c);');
  8433. ConvertProgram;
  8434. CheckSource('TestString_Length',
  8435. LinesToStr([
  8436. 'this.c = "foo";',
  8437. 'this.s = "";',
  8438. 'this.i = 0;',
  8439. '']),
  8440. LinesToStr([
  8441. '$mod.i = $mod.s.length;',
  8442. '$mod.i = ($mod.s+$mod.s).length;',
  8443. '$mod.i = "abc".length;',
  8444. '$mod.i = $mod.c.length;',
  8445. '']));
  8446. end;
  8447. procedure TTestModule.TestString_Compare;
  8448. begin
  8449. StartProgram(false);
  8450. Add('var');
  8451. Add(' s, t: string;');
  8452. Add(' b: boolean;');
  8453. Add('begin');
  8454. Add(' b:=s=t;');
  8455. Add(' b:=s<>t;');
  8456. Add(' b:=s>t;');
  8457. Add(' b:=s>=t;');
  8458. Add(' b:=s<t;');
  8459. Add(' b:=s<=t;');
  8460. ConvertProgram;
  8461. CheckSource('TestString_Compare',
  8462. LinesToStr([ // statements
  8463. 'this.s = "";',
  8464. 'this.t = "";',
  8465. 'this.b =false;'
  8466. ]),
  8467. LinesToStr([ // this.$main
  8468. '$mod.b = $mod.s === $mod.t;',
  8469. '$mod.b = $mod.s !== $mod.t;',
  8470. '$mod.b = $mod.s > $mod.t;',
  8471. '$mod.b = $mod.s >= $mod.t;',
  8472. '$mod.b = $mod.s < $mod.t;',
  8473. '$mod.b = $mod.s <= $mod.t;',
  8474. '']));
  8475. end;
  8476. procedure TTestModule.TestString_SetLength;
  8477. begin
  8478. StartProgram(false);
  8479. Add([
  8480. 'procedure Fly(var s: string);',
  8481. 'begin',
  8482. ' SetLength(s,1);',
  8483. 'end;',
  8484. 'procedure Run(var s: unicodestring);',
  8485. 'begin',
  8486. ' SetLength(s,2);',
  8487. 'end;',
  8488. 'var s: string;',
  8489. ' u: unicodestring;',
  8490. 'begin',
  8491. ' SetLength(s,3);',
  8492. ' SetLength(u,4);',
  8493. '']);
  8494. ConvertProgram;
  8495. CheckSource('TestString_SetLength',
  8496. LinesToStr([ // statements
  8497. 'this.Fly = function (s) {',
  8498. ' s.set(rtl.strSetLength(s.get(), 1));',
  8499. '};',
  8500. 'this.Run = function (s) {',
  8501. ' s.set(rtl.strSetLength(s.get(), 2));',
  8502. '};',
  8503. 'this.s = "";',
  8504. 'this.u = "";',
  8505. '']),
  8506. LinesToStr([ // this.$main
  8507. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8508. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8509. ]));
  8510. end;
  8511. procedure TTestModule.TestString_CharAt;
  8512. begin
  8513. StartProgram(false);
  8514. Add([
  8515. 'var',
  8516. ' s: string;',
  8517. ' c: char;',
  8518. ' b: boolean;',
  8519. 'begin',
  8520. ' b:= s[1] = c;',
  8521. ' b:= c = s[1];',
  8522. ' b:= c <> s[1];',
  8523. ' b:= c > s[1];',
  8524. ' b:= c >= s[1];',
  8525. ' b:= c < s[2];',
  8526. ' b:= c <= s[1];',
  8527. ' s[1] := c;',
  8528. ' s[2+3] := c;']);
  8529. ConvertProgram;
  8530. CheckSource('TestString_CharAt',
  8531. LinesToStr([ // statements
  8532. 'this.s = "";',
  8533. 'this.c = "";',
  8534. 'this.b = false;'
  8535. ]),
  8536. LinesToStr([ // this.$main
  8537. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8538. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8539. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8540. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8541. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8542. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8543. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8544. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8545. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8546. '']));
  8547. end;
  8548. procedure TTestModule.TestStringHMinusFail;
  8549. begin
  8550. StartProgram(false);
  8551. Add([
  8552. '{$H-}',
  8553. 'var s: string;',
  8554. 'begin']);
  8555. ConvertProgram;
  8556. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8557. end;
  8558. procedure TTestModule.TestStr;
  8559. begin
  8560. StartProgram(false);
  8561. Add('var');
  8562. Add(' b: boolean;');
  8563. Add(' i: longint;');
  8564. Add(' d: double;');
  8565. Add(' s: string;');
  8566. Add('begin');
  8567. Add(' str(b,s);');
  8568. Add(' str(i,s);');
  8569. Add(' str(d,s);');
  8570. Add(' str(i:3,s);');
  8571. Add(' str(d:3:2,s);');
  8572. Add(' Str(12.456:12:1,s);');
  8573. Add(' Str(12.456:12,s);');
  8574. Add(' s:=str(b);');
  8575. Add(' s:=str(i);');
  8576. Add(' s:=str(d);');
  8577. Add(' s:=str(i,i);');
  8578. Add(' s:=str(i:3);');
  8579. Add(' s:=str(d:3:2);');
  8580. Add(' s:=str(i:4,i);');
  8581. Add(' s:=str(i,i:5);');
  8582. Add(' s:=str(i:4,i:5);');
  8583. Add(' s:=str(s,s);');
  8584. Add(' s:=str(s,''foo'');');
  8585. ConvertProgram;
  8586. CheckSource('TestStr',
  8587. LinesToStr([ // statements
  8588. 'this.b = false;',
  8589. 'this.i = 0;',
  8590. 'this.d = 0.0;',
  8591. 'this.s = "";',
  8592. '']),
  8593. LinesToStr([ // this.$main
  8594. '$mod.s = ""+$mod.b;',
  8595. '$mod.s = ""+$mod.i;',
  8596. '$mod.s = rtl.floatToStr($mod.d);',
  8597. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8598. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8599. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8600. '$mod.s = rtl.floatToStr(12.456,12);',
  8601. '$mod.s = ""+$mod.b;',
  8602. '$mod.s = ""+$mod.i;',
  8603. '$mod.s = rtl.floatToStr($mod.d);',
  8604. '$mod.s = ""+$mod.i+$mod.i;',
  8605. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8606. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8607. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8608. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8609. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8610. '$mod.s = $mod.s + $mod.s;',
  8611. '$mod.s = $mod.s + "foo";',
  8612. '']));
  8613. end;
  8614. procedure TTestModule.TestBaseType_AnsiStringFail;
  8615. begin
  8616. StartProgram(false);
  8617. Add('var s: AnsiString');
  8618. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8619. ConvertProgram;
  8620. end;
  8621. procedure TTestModule.TestBaseType_WideStringFail;
  8622. begin
  8623. StartProgram(false);
  8624. Add('var s: WideString');
  8625. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8626. ConvertProgram;
  8627. end;
  8628. procedure TTestModule.TestBaseType_ShortStringFail;
  8629. begin
  8630. StartProgram(false);
  8631. Add('var s: ShortString');
  8632. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8633. ConvertProgram;
  8634. end;
  8635. procedure TTestModule.TestBaseType_RawByteStringFail;
  8636. begin
  8637. StartProgram(false);
  8638. Add('var s: RawByteString');
  8639. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8640. ConvertProgram;
  8641. end;
  8642. procedure TTestModule.TestTypeShortstring_Fail;
  8643. begin
  8644. StartProgram(false);
  8645. Add('type t = string[12];');
  8646. Add('var s: t;');
  8647. Add('begin');
  8648. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8649. ConvertProgram;
  8650. end;
  8651. procedure TTestModule.TestCharSet_Custom;
  8652. begin
  8653. StartProgram(false);
  8654. Add([
  8655. 'type',
  8656. ' TCharRg = ''a''..''z'';',
  8657. ' TSetOfCharRg = set of TCharRg;',
  8658. ' TCharRg2 = ''m''..''p'';',
  8659. 'const',
  8660. ' crg: TCharRg = ''b'';',
  8661. 'var',
  8662. ' c: char;',
  8663. ' crg2: TCharRg2;',
  8664. ' s: TSetOfCharRg;',
  8665. 'begin',
  8666. ' c:=crg;',
  8667. ' crg:=c;',
  8668. ' crg2:=crg;',
  8669. ' if c=crg then ;',
  8670. ' if crg=c then ;',
  8671. ' if crg=crg2 then ;',
  8672. ' if c in s then ;',
  8673. ' if crg2 in s then ;',
  8674. ' c:=default(TCharRg);',
  8675. '']);
  8676. ConvertProgram;
  8677. CheckSource('TestCharSet_Custom',
  8678. LinesToStr([ // statements
  8679. 'this.crg = "b";',
  8680. 'this.c = "";',
  8681. 'this.crg2 = "m";',
  8682. 'this.s = {};',
  8683. '']),
  8684. LinesToStr([ // this.$main
  8685. '$mod.c = $mod.crg;',
  8686. '$mod.crg = $mod.c;',
  8687. '$mod.crg2 = $mod.crg;',
  8688. 'if ($mod.c === $mod.crg) ;',
  8689. 'if ($mod.crg === $mod.c) ;',
  8690. 'if ($mod.crg === $mod.crg2) ;',
  8691. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8692. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8693. '$mod.c = "a";',
  8694. '']));
  8695. end;
  8696. procedure TTestModule.TestWideChar;
  8697. begin
  8698. StartProgram(false);
  8699. Add([
  8700. 'procedure Fly(var c: char);',
  8701. 'begin',
  8702. 'end;',
  8703. 'procedure Run(var c: widechar);',
  8704. 'begin',
  8705. 'end;',
  8706. 'var',
  8707. ' c: char;',
  8708. ' wc: widechar;',
  8709. ' w: word;',
  8710. 'begin',
  8711. ' Fly(wc);',
  8712. ' Run(c);',
  8713. ' wc:=WideChar(w);',
  8714. ' w:=ord(wc);',
  8715. '']);
  8716. ConvertProgram;
  8717. CheckSource('TestWideChar_VarArg',
  8718. LinesToStr([ // statements
  8719. 'this.Fly = function (c) {',
  8720. '};',
  8721. 'this.Run = function (c) {',
  8722. '};',
  8723. 'this.c = "";',
  8724. 'this.wc = "";',
  8725. 'this.w = 0;',
  8726. '']),
  8727. LinesToStr([ // this.$main
  8728. '$mod.Fly({',
  8729. ' p: $mod,',
  8730. ' get: function () {',
  8731. ' return this.p.wc;',
  8732. ' },',
  8733. ' set: function (v) {',
  8734. ' this.p.wc = v;',
  8735. ' }',
  8736. '});',
  8737. '$mod.Run({',
  8738. ' p: $mod,',
  8739. ' get: function () {',
  8740. ' return this.p.c;',
  8741. ' },',
  8742. ' set: function (v) {',
  8743. ' this.p.c = v;',
  8744. ' }',
  8745. '});',
  8746. '$mod.wc = String.fromCharCode($mod.w);',
  8747. '$mod.w = $mod.wc.charCodeAt();',
  8748. '',
  8749. '']));
  8750. end;
  8751. procedure TTestModule.TestForCharDo;
  8752. begin
  8753. StartProgram(false);
  8754. Add([
  8755. 'var c: char;',
  8756. 'begin',
  8757. ' for c:=''a'' to ''c'' do ;',
  8758. ' for c:=c downto ''a'' do ;',
  8759. ' for c:=''Б'' to ''Я'' do ;',
  8760. '']);
  8761. ConvertProgram;
  8762. CheckSource('TestForCharDo',
  8763. LinesToStr([ // statements
  8764. 'this.c = "";']),
  8765. LinesToStr([ // this.$main
  8766. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8767. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8768. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8769. '']));
  8770. end;
  8771. procedure TTestModule.TestForCharInDo;
  8772. begin
  8773. StartProgram(false);
  8774. Add([
  8775. 'type',
  8776. ' TSetOfChar = set of char;',
  8777. ' TCharRg = ''a''..''z'';',
  8778. ' TSetOfCharRg = set of TCharRg;',
  8779. 'const Foo = ''foo'';',
  8780. 'var',
  8781. ' c,c2: char;',
  8782. ' s: string;',
  8783. ' a1: array of char;',
  8784. ' a2: array[1..3] of char;',
  8785. ' soc: TSetOfChar;',
  8786. ' socr: TSetOfCharRg;',
  8787. ' cr: TCharRg;',
  8788. 'begin',
  8789. ' for c in foo do ;',
  8790. ' for c in s do ;',
  8791. ' for c in char do ;',
  8792. ' for c in a1 do ;',
  8793. ' for c in a2 do ;',
  8794. ' for c in [''1''..''3''] do ;',
  8795. ' for c in TSetOfChar do ;',
  8796. ' for c in TCharRg do ;',
  8797. ' for c in soc do c2:=c;',
  8798. ' for c in TSetOfCharRg do ;',
  8799. ' for c in socr do ;',
  8800. ' for cr in TCharRg do ;',
  8801. ' for cr in TSetOfCharRg do ;',
  8802. ' for cr in socr do ;',
  8803. '']);
  8804. ConvertProgram;
  8805. CheckSource('TestForCharInDo',
  8806. LinesToStr([ // statements
  8807. 'this.Foo = "foo";',
  8808. 'this.c = "";',
  8809. 'this.c2 = "";',
  8810. 'this.s = "";',
  8811. 'this.a1 = [];',
  8812. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8813. 'this.soc = {};',
  8814. 'this.socr = {};',
  8815. 'this.cr = "a";',
  8816. '']),
  8817. LinesToStr([ // this.$main
  8818. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8819. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8820. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8821. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8822. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8823. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8824. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8825. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8826. 'for (var $l8 in $mod.soc) {',
  8827. ' $mod.c = String.fromCharCode($l8);',
  8828. ' $mod.c2 = $mod.c;',
  8829. '};',
  8830. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8831. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8832. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8833. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8834. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8835. '']));
  8836. end;
  8837. procedure TTestModule.TestProcTwoArgs;
  8838. begin
  8839. StartProgram(false);
  8840. Add('procedure Test(a,b: longint);');
  8841. Add('begin');
  8842. Add('end;');
  8843. Add('begin');
  8844. ConvertProgram;
  8845. CheckSource('TestProcTwoArgs',
  8846. LinesToStr([ // statements
  8847. 'this.Test = function (a,b) {',
  8848. '};'
  8849. ]),
  8850. LinesToStr([ // this.$main
  8851. ''
  8852. ]));
  8853. end;
  8854. procedure TTestModule.TestProc_DefaultValue;
  8855. begin
  8856. StartProgram(false);
  8857. Add('procedure p1(i: longint = 1);');
  8858. Add('begin');
  8859. Add('end;');
  8860. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8861. Add('begin');
  8862. Add('end;');
  8863. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8864. Add('begin');
  8865. Add('end;');
  8866. Add('begin');
  8867. Add(' p1;');
  8868. Add(' p1();');
  8869. Add(' p1(11);');
  8870. Add(' p2;');
  8871. Add(' p2();');
  8872. Add(' p2(12);');
  8873. Add(' p2(13,''b'');');
  8874. Add(' p3();');
  8875. ConvertProgram;
  8876. CheckSource('TestProc_DefaultValue',
  8877. LinesToStr([ // statements
  8878. 'this.p1 = function (i) {',
  8879. '};',
  8880. 'this.p2 = function (i,c) {',
  8881. '};',
  8882. 'this.p3 = function (d,b,s) {',
  8883. '};'
  8884. ]),
  8885. LinesToStr([ // this.$main
  8886. ' $mod.p1(1);',
  8887. ' $mod.p1(1);',
  8888. ' $mod.p1(11);',
  8889. ' $mod.p2(1,"a");',
  8890. ' $mod.p2(1,"a");',
  8891. ' $mod.p2(12,"a");',
  8892. ' $mod.p2(13,"b");',
  8893. ' $mod.p3(1.0,false,"abc");'
  8894. ]));
  8895. end;
  8896. procedure TTestModule.TestFunctionInt;
  8897. begin
  8898. StartProgram(false);
  8899. Add('function MyTest(Bar: longint): longint;');
  8900. Add('begin');
  8901. Add(' Result:=2*bar');
  8902. Add('end;');
  8903. Add('begin');
  8904. ConvertProgram;
  8905. CheckSource('TestFunctionInt',
  8906. LinesToStr([ // statements
  8907. 'this.MyTest = function (Bar) {',
  8908. ' var Result = 0;',
  8909. ' Result = 2*Bar;',
  8910. ' return Result;',
  8911. '};'
  8912. ]),
  8913. LinesToStr([ // this.$main
  8914. ''
  8915. ]));
  8916. end;
  8917. procedure TTestModule.TestFunctionString;
  8918. begin
  8919. StartProgram(false);
  8920. Add('function Test(Bar: string): string;');
  8921. Add('begin');
  8922. Add(' Result:=bar+BAR');
  8923. Add('end;');
  8924. Add('begin');
  8925. ConvertProgram;
  8926. CheckSource('TestFunctionString',
  8927. LinesToStr([ // statements
  8928. 'this.Test = function (Bar) {',
  8929. ' var Result = "";',
  8930. ' Result = Bar+Bar;',
  8931. ' return Result;',
  8932. '};'
  8933. ]),
  8934. LinesToStr([ // this.$main
  8935. ''
  8936. ]));
  8937. end;
  8938. procedure TTestModule.TestIfThen;
  8939. begin
  8940. StartProgram(false);
  8941. Add([
  8942. 'var b: boolean;',
  8943. 'begin',
  8944. ' if b then ;',
  8945. ' if b then else ;']);
  8946. ConvertProgram;
  8947. CheckSource('TestIfThen',
  8948. LinesToStr([ // statements
  8949. 'this.b = false;',
  8950. '']),
  8951. LinesToStr([ // this.$main
  8952. 'if ($mod.b) ;',
  8953. 'if ($mod.b) ;',
  8954. '']));
  8955. end;
  8956. procedure TTestModule.TestForLoop;
  8957. begin
  8958. StartProgram(false);
  8959. Add('var');
  8960. Add(' vI, vJ, vN: longint;');
  8961. Add('begin');
  8962. Add(' VJ:=0;');
  8963. Add(' VN:=3;');
  8964. Add(' for VI:=1 to VN do');
  8965. Add(' begin');
  8966. Add(' VJ:=VJ+VI;');
  8967. Add(' end;');
  8968. ConvertProgram;
  8969. CheckSource('TestForLoop',
  8970. LinesToStr([ // statements
  8971. 'this.vI = 0;',
  8972. 'this.vJ = 0;',
  8973. 'this.vN = 0;'
  8974. ]),
  8975. LinesToStr([ // this.$main
  8976. ' $mod.vJ = 0;',
  8977. ' $mod.vN = 3;',
  8978. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8979. ' $mod.vI = $l;',
  8980. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8981. ' };',
  8982. '']));
  8983. end;
  8984. procedure TTestModule.TestForLoopInsideFunction;
  8985. begin
  8986. StartProgram(false);
  8987. Add('function SumNumbers(Count: longint): longint;');
  8988. Add('var');
  8989. Add(' vI, vJ: longint;');
  8990. Add('begin');
  8991. Add(' vj:=0;');
  8992. Add(' for vi:=1 to count do');
  8993. Add(' begin');
  8994. Add(' vj:=vj+vi;');
  8995. Add(' end;');
  8996. Add('end;');
  8997. Add('begin');
  8998. Add(' sumnumbers(3);');
  8999. ConvertProgram;
  9000. CheckSource('TestForLoopInsideFunction',
  9001. LinesToStr([ // statements
  9002. 'this.SumNumbers = function (Count) {',
  9003. ' var Result = 0;',
  9004. ' var vI = 0;',
  9005. ' var vJ = 0;',
  9006. ' vJ = 0;',
  9007. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9008. ' vI = $l;',
  9009. ' vJ = vJ + vI;',
  9010. ' };',
  9011. ' return Result;',
  9012. '};'
  9013. ]),
  9014. LinesToStr([ // $mod.$main
  9015. ' $mod.SumNumbers(3);'
  9016. ]));
  9017. end;
  9018. procedure TTestModule.TestForLoop_ReadVarAfter;
  9019. begin
  9020. StartProgram(false);
  9021. Add('var');
  9022. Add(' vI: longint;');
  9023. Add('begin');
  9024. Add(' for vi:=1 to 2 do ;');
  9025. Add(' if vi=3 then ;');
  9026. ConvertProgram;
  9027. CheckSource('TestForLoop',
  9028. LinesToStr([ // statements
  9029. 'this.vI = 0;'
  9030. ]),
  9031. LinesToStr([ // this.$main
  9032. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9033. ' if ($mod.vI===3) ;'
  9034. ]));
  9035. end;
  9036. procedure TTestModule.TestForLoop_Nested;
  9037. begin
  9038. StartProgram(false);
  9039. Add('function SumNumbers(Count: longint): longint;');
  9040. Add('var');
  9041. Add(' vI, vJ, vK: longint;');
  9042. Add('begin');
  9043. Add(' VK:=0;');
  9044. Add(' for VI:=1 to count do');
  9045. Add(' begin');
  9046. Add(' for vj:=1 to vi do');
  9047. Add(' begin');
  9048. Add(' vk:=VK+VI;');
  9049. Add(' end;');
  9050. Add(' end;');
  9051. Add('end;');
  9052. Add('begin');
  9053. Add(' sumnumbers(3);');
  9054. ConvertProgram;
  9055. CheckSource('TestForLoopInFunction',
  9056. LinesToStr([ // statements
  9057. 'this.SumNumbers = function (Count) {',
  9058. ' var Result = 0;',
  9059. ' var vI = 0;',
  9060. ' var vJ = 0;',
  9061. ' var vK = 0;',
  9062. ' vK = 0;',
  9063. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9064. ' vI = $l;',
  9065. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9066. ' vJ = $l1;',
  9067. ' vK = vK + vI;',
  9068. ' };',
  9069. ' };',
  9070. ' return Result;',
  9071. '};'
  9072. ]),
  9073. LinesToStr([ // $mod.$main
  9074. ' $mod.SumNumbers(3);'
  9075. ]));
  9076. end;
  9077. procedure TTestModule.TestRepeatUntil;
  9078. begin
  9079. StartProgram(false);
  9080. Add('var');
  9081. Add(' vI, vJ, vN: longint;');
  9082. Add('begin');
  9083. Add(' vn:=3;');
  9084. Add(' vj:=0;');
  9085. Add(' VI:=0;');
  9086. Add(' repeat');
  9087. Add(' VI:=vi+1;');
  9088. Add(' vj:=VJ+vI;');
  9089. Add(' until vi>=vn');
  9090. ConvertProgram;
  9091. CheckSource('TestRepeatUntil',
  9092. LinesToStr([ // statements
  9093. 'this.vI = 0;',
  9094. 'this.vJ = 0;',
  9095. 'this.vN = 0;'
  9096. ]),
  9097. LinesToStr([ // $mod.$main
  9098. ' $mod.vN = 3;',
  9099. ' $mod.vJ = 0;',
  9100. ' $mod.vI = 0;',
  9101. ' do{',
  9102. ' $mod.vI = $mod.vI + 1;',
  9103. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9104. ' }while(!($mod.vI>=$mod.vN));'
  9105. ]));
  9106. end;
  9107. procedure TTestModule.TestAsmBlock;
  9108. begin
  9109. StartProgram(false);
  9110. Add([
  9111. 'var',
  9112. ' vI: longint;',
  9113. 'begin',
  9114. ' vi:=1;',
  9115. ' asm',
  9116. ' if (vI===1) {',
  9117. ' vI=2;',
  9118. //' console.log(''end;'');', ToDo
  9119. ' }',
  9120. ' if (vI===2){ vI=3; }',
  9121. ' end;',
  9122. ' VI:=4;']);
  9123. ConvertProgram;
  9124. CheckSource('TestAsmBlock',
  9125. LinesToStr([ // statements
  9126. 'this.vI = 0;'
  9127. ]),
  9128. LinesToStr([ // $mod.$main
  9129. '$mod.vI = 1;',
  9130. 'if (vI===1) {',
  9131. ' vI=2;',
  9132. '}',
  9133. 'if (vI===2){ vI=3; }',
  9134. ';',
  9135. '$mod.vI = 4;'
  9136. ]));
  9137. end;
  9138. procedure TTestModule.TestAsmPas_Impl;
  9139. begin
  9140. StartUnit(false);
  9141. Add('interface');
  9142. Add('const cIntf: longint = 1;');
  9143. Add('var vIntf: longint;');
  9144. Add('implementation');
  9145. Add('const cImpl: longint = 2;');
  9146. Add('var vImpl: longint;');
  9147. Add('procedure DoIt;');
  9148. Add('const cLoc: longint = 3;');
  9149. Add('var vLoc: longint;');
  9150. Add('begin;');
  9151. Add(' asm');
  9152. //Add(' pas(vIntf)=pas(cIntf);');
  9153. //Add(' pas(vImpl)=pas(cImpl);');
  9154. //Add(' pas(vLoc)=pas(cLoc);');
  9155. Add(' end;');
  9156. Add('end;');
  9157. ConvertUnit;
  9158. CheckSource('TestAsmPas_Impl',
  9159. LinesToStr([
  9160. 'var $impl = $mod.$impl;',
  9161. 'this.cIntf = 1;',
  9162. 'this.vIntf = 0;',
  9163. '']),
  9164. '', // this.$init
  9165. LinesToStr([ // implementation
  9166. '$impl.cImpl = 2;',
  9167. '$impl.vImpl = 0;',
  9168. 'var cLoc = 3;',
  9169. '$impl.DoIt = function () {',
  9170. ' var vLoc = 0;',
  9171. '};',
  9172. '']) );
  9173. end;
  9174. procedure TTestModule.TestTryFinally;
  9175. begin
  9176. StartProgram(false);
  9177. Add('var i: longint;');
  9178. Add('begin');
  9179. Add(' try');
  9180. Add(' i:=0; i:=2 div i;');
  9181. Add(' finally');
  9182. Add(' i:=3');
  9183. Add(' end;');
  9184. ConvertProgram;
  9185. CheckSource('TestTryFinally',
  9186. LinesToStr([ // statements
  9187. 'this.i = 0;'
  9188. ]),
  9189. LinesToStr([ // $mod.$main
  9190. 'try {',
  9191. ' $mod.i = 0;',
  9192. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9193. '} finally {',
  9194. ' $mod.i = 3;',
  9195. '};'
  9196. ]));
  9197. end;
  9198. procedure TTestModule.TestTryExcept;
  9199. begin
  9200. StartProgram(false);
  9201. Add([
  9202. 'type',
  9203. ' TObject = class end;',
  9204. ' Exception = class Msg: string; end;',
  9205. ' EInvalidCast = class(Exception) end;',
  9206. 'var vI: longint;',
  9207. 'begin',
  9208. ' try',
  9209. ' vi:=1;',
  9210. ' except',
  9211. ' vi:=2',
  9212. ' end;',
  9213. ' try',
  9214. ' vi:=3;',
  9215. ' except',
  9216. ' raise;',
  9217. ' end;',
  9218. ' try',
  9219. ' VI:=4;',
  9220. ' except',
  9221. ' on einvalidcast do',
  9222. ' raise;',
  9223. ' on E: exception do',
  9224. ' if e.msg='''' then',
  9225. ' raise e;',
  9226. ' else',
  9227. ' vi:=5',
  9228. ' end;',
  9229. ' try',
  9230. ' VI:=6;',
  9231. ' except',
  9232. ' on einvalidcast do ;',
  9233. ' end;',
  9234. '']);
  9235. ConvertProgram;
  9236. CheckSource('TestTryExcept',
  9237. LinesToStr([ // statements
  9238. 'rtl.createClass(this, "TObject", null, function () {',
  9239. ' this.$init = function () {',
  9240. ' };',
  9241. ' this.$final = function () {',
  9242. ' };',
  9243. '});',
  9244. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9245. ' this.$init = function () {',
  9246. ' $mod.TObject.$init.call(this);',
  9247. ' this.Msg = "";',
  9248. ' };',
  9249. '});',
  9250. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9251. '});',
  9252. 'this.vI = 0;'
  9253. ]),
  9254. LinesToStr([ // $mod.$main
  9255. 'try {',
  9256. ' $mod.vI = 1;',
  9257. '} catch ($e) {',
  9258. ' $mod.vI = 2;',
  9259. '};',
  9260. 'try {',
  9261. ' $mod.vI = 3;',
  9262. '} catch ($e) {',
  9263. ' throw $e;',
  9264. '};',
  9265. 'try {',
  9266. ' $mod.vI = 4;',
  9267. '} catch ($e) {',
  9268. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9269. ' throw $e',
  9270. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9271. ' var E = $e;',
  9272. ' if (E.Msg === "") throw E;',
  9273. ' } else {',
  9274. ' $mod.vI = 5;',
  9275. ' }',
  9276. '};',
  9277. 'try {',
  9278. ' $mod.vI = 6;',
  9279. '} catch ($e) {',
  9280. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9281. ' } else throw $e',
  9282. '};',
  9283. '']));
  9284. end;
  9285. procedure TTestModule.TestTryExcept_ReservedWords;
  9286. begin
  9287. StartProgram(false);
  9288. Add([
  9289. 'type',
  9290. ' TObject = class end;',
  9291. ' Exception = class',
  9292. ' Symbol: string;',
  9293. ' end;',
  9294. 'var &try: longint;',
  9295. 'begin',
  9296. ' try',
  9297. ' &try:=4;',
  9298. ' except',
  9299. ' on Error: exception do',
  9300. ' if errOR.symBol='''' then',
  9301. ' raise ERRor;',
  9302. ' end;',
  9303. '']);
  9304. ConvertProgram;
  9305. CheckSource('TestTryExcept_ReservedWords',
  9306. LinesToStr([ // statements
  9307. 'rtl.createClass(this, "TObject", null, function () {',
  9308. ' this.$init = function () {',
  9309. ' };',
  9310. ' this.$final = function () {',
  9311. ' };',
  9312. '});',
  9313. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9314. ' this.$init = function () {',
  9315. ' $mod.TObject.$init.call(this);',
  9316. ' this.Symbol = "";',
  9317. ' };',
  9318. '});',
  9319. 'this.Try = 0;',
  9320. '']),
  9321. LinesToStr([ // $mod.$main
  9322. 'try {',
  9323. ' $mod.Try = 4;',
  9324. '} catch ($e) {',
  9325. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9326. ' var error = $e;',
  9327. ' if (error.Symbol === "") throw error;',
  9328. ' } else throw $e',
  9329. '};',
  9330. '']));
  9331. end;
  9332. procedure TTestModule.TestIfThenRaiseElse;
  9333. begin
  9334. StartProgram(false);
  9335. Add([
  9336. 'type',
  9337. ' TObject = class',
  9338. ' constructor Create;',
  9339. ' end;',
  9340. 'constructor TObject.Create;',
  9341. 'begin',
  9342. 'end;',
  9343. 'var b: boolean;',
  9344. 'begin',
  9345. ' if b then',
  9346. ' raise TObject.Create',
  9347. ' else',
  9348. ' b:=false;',
  9349. '']);
  9350. ConvertProgram;
  9351. CheckSource('TestIfThenRaiseElse',
  9352. LinesToStr([ // statements
  9353. 'rtl.createClass(this, "TObject", null, function () {',
  9354. ' this.$init = function () {',
  9355. ' };',
  9356. ' this.$final = function () {',
  9357. ' };',
  9358. ' this.Create = function () {',
  9359. ' return this;',
  9360. ' };',
  9361. '});',
  9362. 'this.b = false;',
  9363. '']),
  9364. LinesToStr([ // $mod.$main
  9365. 'if ($mod.b) {',
  9366. ' throw $mod.TObject.$create("Create")}',
  9367. ' else $mod.b = false;',
  9368. '']));
  9369. end;
  9370. procedure TTestModule.TestCaseOf;
  9371. begin
  9372. StartProgram(false);
  9373. Add([
  9374. 'const e: longint; external name ''$e'';',
  9375. 'var vI: longint;',
  9376. 'begin',
  9377. ' case vi of',
  9378. ' 1: ;',
  9379. ' 2: vi:=3;',
  9380. ' e: ;',
  9381. ' else',
  9382. ' VI:=4',
  9383. ' end;']);
  9384. ConvertProgram;
  9385. CheckSource('TestCaseOf',
  9386. LinesToStr([ // statements
  9387. 'this.vI = 0;'
  9388. ]),
  9389. LinesToStr([ // $mod.$main
  9390. 'var $tmp = $mod.vI;',
  9391. 'if ($tmp === 1) {}',
  9392. 'else if ($tmp === 2) {',
  9393. ' $mod.vI = 3}',
  9394. ' else if ($tmp === $e) {}',
  9395. 'else {',
  9396. ' $mod.vI = 4;',
  9397. '};'
  9398. ]));
  9399. end;
  9400. procedure TTestModule.TestCaseOf_UseSwitch;
  9401. begin
  9402. StartProgram(false);
  9403. Converter.UseSwitchStatement:=true;
  9404. Add('var Vi: longint;');
  9405. Add('begin');
  9406. Add(' case vi of');
  9407. Add(' 1: ;');
  9408. Add(' 2: VI:=3;');
  9409. Add(' else');
  9410. Add(' vi:=4');
  9411. Add(' end;');
  9412. ConvertProgram;
  9413. CheckSource('TestCaseOf_UseSwitch',
  9414. LinesToStr([ // statements
  9415. 'this.Vi = 0;'
  9416. ]),
  9417. LinesToStr([ // $mod.$main
  9418. 'switch ($mod.Vi) {',
  9419. 'case 1:',
  9420. ' break;',
  9421. 'case 2:',
  9422. ' $mod.Vi = 3;',
  9423. ' break;',
  9424. 'default:',
  9425. ' $mod.Vi = 4;',
  9426. '};'
  9427. ]));
  9428. end;
  9429. procedure TTestModule.TestCaseOfNoElse;
  9430. begin
  9431. StartProgram(false);
  9432. Add('var Vi: longint;');
  9433. Add('begin');
  9434. Add(' case vi of');
  9435. Add(' 1: begin vi:=2; VI:=3; end;');
  9436. Add(' end;');
  9437. ConvertProgram;
  9438. CheckSource('TestCaseOfNoElse',
  9439. LinesToStr([ // statements
  9440. 'this.Vi = 0;'
  9441. ]),
  9442. LinesToStr([ // $mod.$main
  9443. 'var $tmp = $mod.Vi;',
  9444. 'if ($tmp === 1) {',
  9445. ' $mod.Vi = 2;',
  9446. ' $mod.Vi = 3;',
  9447. '};'
  9448. ]));
  9449. end;
  9450. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9451. begin
  9452. StartProgram(false);
  9453. Converter.UseSwitchStatement:=true;
  9454. Add('var vI: longint;');
  9455. Add('begin');
  9456. Add(' case vi of');
  9457. Add(' 1: begin VI:=2; vi:=3; end;');
  9458. Add(' end;');
  9459. ConvertProgram;
  9460. CheckSource('TestCaseOfNoElse_UseSwitch',
  9461. LinesToStr([ // statements
  9462. 'this.vI = 0;'
  9463. ]),
  9464. LinesToStr([ // $mod.$main
  9465. 'switch ($mod.vI) {',
  9466. 'case 1:',
  9467. ' $mod.vI = 2;',
  9468. ' $mod.vI = 3;',
  9469. ' break;',
  9470. '};'
  9471. ]));
  9472. end;
  9473. procedure TTestModule.TestCaseOfRange;
  9474. begin
  9475. StartProgram(false);
  9476. Add('var vI: longint;');
  9477. Add('begin');
  9478. Add(' case vi of');
  9479. Add(' 1..3: vi:=14;');
  9480. Add(' 4,5: vi:=16;');
  9481. Add(' 6..7,9..10: ;');
  9482. Add(' else ;');
  9483. Add(' end;');
  9484. ConvertProgram;
  9485. CheckSource('TestCaseOfRange',
  9486. LinesToStr([ // statements
  9487. 'this.vI = 0;'
  9488. ]),
  9489. LinesToStr([ // $mod.$main
  9490. 'var $tmp = $mod.vI;',
  9491. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9492. ' $mod.vI = 14',
  9493. '} else if (($tmp === 4) || ($tmp === 5)){',
  9494. ' $mod.vI = 16',
  9495. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9496. ]));
  9497. end;
  9498. procedure TTestModule.TestCaseOfString;
  9499. begin
  9500. StartProgram(false);
  9501. Add([
  9502. 'var s,h: string;',
  9503. 'begin',
  9504. ' case s of',
  9505. ' ''foo'': s:=h;',
  9506. ' ''a''..''z'': h:=s;',
  9507. ' ''ў'', ''ё'': ;',
  9508. ' ''Б''..''Я'': ;',
  9509. ' end;',
  9510. '']);
  9511. ConvertProgram;
  9512. CheckSource('TestCaseOfString',
  9513. LinesToStr([ // statements
  9514. 'this.s = "";',
  9515. 'this.h = "";',
  9516. '']),
  9517. LinesToStr([ // $mod.$main
  9518. 'var $tmp = $mod.s;',
  9519. 'if ($tmp === "foo") {',
  9520. ' $mod.s = $mod.h}',
  9521. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9522. ' $mod.h = $mod.s}',
  9523. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9524. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9525. '']));
  9526. end;
  9527. procedure TTestModule.TestCaseOfChar;
  9528. begin
  9529. StartProgram(false);
  9530. Add([
  9531. 'var s,h: char;',
  9532. 'begin',
  9533. ' case s of',
  9534. ' ''a''..''z'': h:=s;',
  9535. ' ''ä'': ;',
  9536. ' ''ў'', ''ё'': ;',
  9537. ' ''Б''..''Я'': ;',
  9538. ' end;',
  9539. '']);
  9540. ConvertProgram;
  9541. CheckSource('TestCaseOfString',
  9542. LinesToStr([ // statements
  9543. 'this.s = "";',
  9544. 'this.h = "";',
  9545. '']),
  9546. LinesToStr([ // $mod.$main
  9547. 'var $tmp = $mod.s;',
  9548. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9549. ' $mod.h = $mod.s}',
  9550. ' else if ($tmp === "ä") {}',
  9551. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9552. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9553. '']));
  9554. end;
  9555. procedure TTestModule.TestCaseOfExternalClassConst;
  9556. begin
  9557. StartProgram(false);
  9558. Add([
  9559. '{$modeswitch externalclass}',
  9560. 'type',
  9561. ' TBird = class external name ''Bird''',
  9562. ' const e: longint;',
  9563. ' end;',
  9564. 'var vI: longint;',
  9565. 'begin',
  9566. ' case vi of',
  9567. ' 1: vi:=3;',
  9568. ' TBird.e: ;',
  9569. ' end;']);
  9570. ConvertProgram;
  9571. CheckSource('TestCaseOfExternalClassConst',
  9572. LinesToStr([ // statements
  9573. 'this.vI = 0;'
  9574. ]),
  9575. LinesToStr([ // $mod.$main
  9576. 'var $tmp = $mod.vI;',
  9577. 'if ($tmp === 1) {',
  9578. ' $mod.vI = 3}',
  9579. ' else if ($tmp === Bird.e) ;'
  9580. ]));
  9581. end;
  9582. procedure TTestModule.TestDebugger;
  9583. begin
  9584. StartProgram(false);
  9585. Add([
  9586. 'procedure DoIt;',
  9587. 'begin',
  9588. ' deBugger;',
  9589. ' DeBugger();',
  9590. 'end;',
  9591. 'begin',
  9592. ' Debugger;']);
  9593. ConvertProgram;
  9594. CheckSource('TestDebugger',
  9595. LinesToStr([ // statements
  9596. 'this.DoIt = function () {',
  9597. ' debugger;',
  9598. ' debugger;',
  9599. '};',
  9600. '']),
  9601. LinesToStr([ // $mod.$main
  9602. 'debugger;',
  9603. '']));
  9604. end;
  9605. procedure TTestModule.TestArray_Dynamic;
  9606. begin
  9607. StartProgram(false);
  9608. Add([
  9609. 'type',
  9610. ' TArrayInt = array of longint;',
  9611. 'var',
  9612. ' Arr: TArrayInt;',
  9613. ' i: longint;',
  9614. ' b: boolean;',
  9615. 'begin',
  9616. ' SetLength(arr,3);',
  9617. ' arr[0]:=4;',
  9618. ' arr[1]:=length(arr)+arr[0];',
  9619. ' arr[i]:=5;',
  9620. ' arr[arr[i]]:=arr[6];',
  9621. ' i:=low(arr);',
  9622. ' i:=high(arr);',
  9623. ' b:=Assigned(arr);',
  9624. ' Arr:=default(TArrayInt);']);
  9625. ConvertProgram;
  9626. CheckSource('TestArray_Dynamic',
  9627. LinesToStr([ // statements
  9628. 'this.Arr = [];',
  9629. 'this.i = 0;',
  9630. 'this.b = false;'
  9631. ]),
  9632. LinesToStr([ // $mod.$main
  9633. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9634. '$mod.Arr[0] = 4;',
  9635. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9636. '$mod.Arr[$mod.i] = 5;',
  9637. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9638. '$mod.i = 0;',
  9639. '$mod.i = rtl.length($mod.Arr) - 1;',
  9640. '$mod.b = rtl.length($mod.Arr) > 0;',
  9641. '$mod.Arr = [];',
  9642. '']));
  9643. end;
  9644. procedure TTestModule.TestArray_Dynamic_Nil;
  9645. begin
  9646. StartProgram(false);
  9647. Add('type');
  9648. Add(' TArrayInt = array of longint;');
  9649. Add('var');
  9650. Add(' Arr: TArrayInt;');
  9651. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9652. Add('begin');
  9653. Add(' arr:=nil;');
  9654. Add(' if arr=nil then;');
  9655. Add(' if nil=arr then;');
  9656. Add(' if arr<>nil then;');
  9657. Add(' if nil<>arr then;');
  9658. Add(' DoIt(nil,nil);');
  9659. ConvertProgram;
  9660. CheckSource('TestArray_Dynamic',
  9661. LinesToStr([ // statements
  9662. 'this.Arr = [];',
  9663. 'this.DoIt = function(i,j){',
  9664. '};'
  9665. ]),
  9666. LinesToStr([ // $mod.$main
  9667. '$mod.Arr = [];',
  9668. 'if (rtl.length($mod.Arr) === 0) ;',
  9669. 'if (rtl.length($mod.Arr) === 0) ;',
  9670. 'if (rtl.length($mod.Arr) > 0) ;',
  9671. 'if (rtl.length($mod.Arr) > 0) ;',
  9672. '$mod.DoIt([],[]);',
  9673. '']));
  9674. end;
  9675. procedure TTestModule.TestArray_DynMultiDimensional;
  9676. begin
  9677. StartProgram(false);
  9678. Add([
  9679. 'type',
  9680. ' TArrayInt = array of longint;',
  9681. ' TArrayArrayInt = array of TArrayInt;',
  9682. 'var',
  9683. ' Arr: TArrayInt;',
  9684. ' Arr2: TArrayArrayInt;',
  9685. ' i: longint;',
  9686. 'begin',
  9687. ' arr2:=nil;',
  9688. ' if arr2=nil then;',
  9689. ' if nil=arr2 then;',
  9690. ' i:=low(arr2);',
  9691. ' i:=low(arr2[1]);',
  9692. ' i:=high(arr2);',
  9693. ' i:=high(arr2[2]);',
  9694. ' arr2[3]:=arr;',
  9695. ' arr2[4][5]:=i;',
  9696. ' i:=arr2[6][7];',
  9697. ' arr2[8,9]:=i;',
  9698. ' i:=arr2[10,11];',
  9699. ' SetLength(arr2,14);',
  9700. ' SetLength(arr2[15],16);']);
  9701. ConvertProgram;
  9702. CheckSource('TestArray_Dynamic',
  9703. LinesToStr([ // statements
  9704. 'this.Arr = [];',
  9705. 'this.Arr2 = [];',
  9706. 'this.i = 0;'
  9707. ]),
  9708. LinesToStr([ // $mod.$main
  9709. '$mod.Arr2 = [];',
  9710. 'if (rtl.length($mod.Arr2) === 0) ;',
  9711. 'if (rtl.length($mod.Arr2) === 0) ;',
  9712. '$mod.i = 0;',
  9713. '$mod.i = 0;',
  9714. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9715. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9716. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9717. '$mod.Arr2[4][5] = $mod.i;',
  9718. '$mod.i = $mod.Arr2[6][7];',
  9719. '$mod.Arr2[8][9] = $mod.i;',
  9720. '$mod.i = $mod.Arr2[10][11];',
  9721. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9722. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9723. '']));
  9724. end;
  9725. procedure TTestModule.TestArray_DynamicAssign;
  9726. begin
  9727. StartProgram(false);
  9728. Add([
  9729. 'type',
  9730. ' TArrayInt = array of longint;',
  9731. ' TArrayArrayInt = array of TArrayInt;',
  9732. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9733. 'begin',
  9734. 'end;',
  9735. 'procedure Fly(var a: TArrayInt);',
  9736. 'begin',
  9737. 'end;',
  9738. 'var',
  9739. ' Arr: TArrayInt;',
  9740. ' Arr2: TArrayArrayInt;',
  9741. 'begin',
  9742. ' arr:=nil;',
  9743. ' arr2:=nil;',
  9744. ' arr2[1]:=nil;',
  9745. ' arr2[2]:=arr;',
  9746. ' Run(arr,arr,arr);',
  9747. ' Fly(arr);',
  9748. ' Run(arr2[4],arr2[5],arr2[6]);',
  9749. ' Fly(arr2[7]);',
  9750. '']);
  9751. ConvertProgram;
  9752. CheckSource('TestArray_DynamicAssign',
  9753. LinesToStr([ // statements
  9754. 'this.Run = function (a, b, c) {',
  9755. '};',
  9756. 'this.Fly = function (a) {',
  9757. '};',
  9758. 'this.Arr = [];',
  9759. 'this.Arr2 = [];',
  9760. '']),
  9761. LinesToStr([ // $mod.$main
  9762. '$mod.Arr = [];',
  9763. '$mod.Arr2 = [];',
  9764. '$mod.Arr2[1] = [];',
  9765. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9766. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9767. '$mod.Fly({',
  9768. ' p: $mod,',
  9769. ' get: function () {',
  9770. ' return this.p.Arr;',
  9771. ' },',
  9772. ' set: function (v) {',
  9773. ' this.p.Arr = v;',
  9774. ' }',
  9775. '});',
  9776. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9777. '$mod.Fly({',
  9778. ' a: 7,',
  9779. ' p: $mod.Arr2,',
  9780. ' get: function () {',
  9781. ' return this.p[this.a];',
  9782. ' },',
  9783. ' set: function (v) {',
  9784. ' this.p[this.a] = v;',
  9785. ' }',
  9786. '});',
  9787. '']));
  9788. end;
  9789. procedure TTestModule.TestArray_StaticInt;
  9790. begin
  9791. StartProgram(false);
  9792. Add('type');
  9793. Add(' TArrayInt = array[2..4] of longint;');
  9794. Add('var');
  9795. Add(' Arr: TArrayInt;');
  9796. Add(' Arr2: TArrayInt = (5,6,7);');
  9797. Add(' i: longint;');
  9798. Add(' b: boolean;');
  9799. Add('begin');
  9800. Add(' arr[2]:=4;');
  9801. Add(' arr[3]:=arr[2]+arr[3];');
  9802. Add(' arr[i]:=5;');
  9803. Add(' arr[arr[i]]:=arr[high(arr)];');
  9804. Add(' i:=low(arr);');
  9805. Add(' i:=high(arr);');
  9806. Add(' b:=arr[2]=arr[3];');
  9807. Add(' arr:=default(TArrayInt);');
  9808. ConvertProgram;
  9809. CheckSource('TestArray_StaticInt',
  9810. LinesToStr([ // statements
  9811. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9812. 'this.Arr2 = [5, 6, 7];',
  9813. 'this.i = 0;',
  9814. 'this.b = false;'
  9815. ]),
  9816. LinesToStr([ // $mod.$main
  9817. '$mod.Arr[0] = 4;',
  9818. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9819. '$mod.Arr[$mod.i-2] = 5;',
  9820. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9821. '$mod.i = 2;',
  9822. '$mod.i = 4;',
  9823. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9824. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9825. '']));
  9826. end;
  9827. procedure TTestModule.TestArray_StaticBool;
  9828. begin
  9829. StartProgram(false);
  9830. Add('type');
  9831. Add(' TBools = array[boolean] of boolean;');
  9832. Add(' TBool2 = array[true..true] of boolean;');
  9833. Add('var');
  9834. Add(' Arr: TBools;');
  9835. Add(' Arr2: TBool2;');
  9836. Add(' Arr3: TBools = (true,false);');
  9837. Add(' b: boolean;');
  9838. Add('begin');
  9839. Add(' b:=low(arr);');
  9840. Add(' b:=high(arr);');
  9841. Add(' arr[true]:=false;');
  9842. Add(' arr[false]:=arr[b] or arr[true];');
  9843. Add(' arr[b]:=true;');
  9844. Add(' arr[arr[b]]:=arr[high(arr)];');
  9845. Add(' b:=arr[false]=arr[true];');
  9846. Add(' b:=low(arr2);');
  9847. Add(' b:=high(arr2);');
  9848. Add(' arr2[true]:=true;');
  9849. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9850. Add(' arr2[b]:=false;');
  9851. ConvertProgram;
  9852. CheckSource('TestArray_StaticBool',
  9853. LinesToStr([ // statements
  9854. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9855. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9856. 'this.Arr3 = [true, false];',
  9857. 'this.b = false;'
  9858. ]),
  9859. LinesToStr([ // $mod.$main
  9860. '$mod.b = false;',
  9861. '$mod.b = true;',
  9862. '$mod.Arr[1] = false;',
  9863. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9864. '$mod.Arr[+$mod.b] = true;',
  9865. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9866. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9867. '$mod.b = true;',
  9868. '$mod.b = true;',
  9869. '$mod.Arr2[0] = true;',
  9870. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9871. '$mod.Arr2[1-$mod.b] = false;',
  9872. '']));
  9873. end;
  9874. procedure TTestModule.TestArray_StaticChar;
  9875. begin
  9876. StartProgram(false);
  9877. Add([
  9878. 'type',
  9879. ' TChars = array[char] of char;',
  9880. ' TChars2 = array[''a''..''z''] of char;',
  9881. 'var',
  9882. ' Arr: TChars;',
  9883. ' Arr2: TChars2;',
  9884. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9885. ' Arr4: array[11..13] of char = ''pas'';',
  9886. ' Arr5: array[21..22] of char = ''äö'';',
  9887. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9888. ' c: char;',
  9889. ' b: boolean;',
  9890. 'begin',
  9891. ' c:=low(arr);',
  9892. ' c:=high(arr);',
  9893. ' arr[''B'']:=''a'';',
  9894. ' arr[''D'']:=arr[c];',
  9895. ' arr[c]:=arr[''d''];',
  9896. ' arr[arr[c]]:=arr[high(arr)];',
  9897. ' b:=arr[low(arr)]=arr[''e''];',
  9898. ' c:=low(arr2);',
  9899. ' c:=high(arr2);',
  9900. ' arr2[''b'']:=''f'';',
  9901. ' arr2[''a'']:=arr2[c];',
  9902. ' arr2[c]:=arr2[''g''];']);
  9903. ConvertProgram;
  9904. CheckSource('TestArray_StaticChar',
  9905. LinesToStr([ // statements
  9906. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9907. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9908. 'this.Arr3 = ["p", "a", "s"];',
  9909. 'this.Arr4 = ["p", "a", "s"];',
  9910. 'this.Arr5 = ["ä", "ö"];',
  9911. 'this.Arr6 = ["ä", "ö"];',
  9912. 'this.c = "";',
  9913. 'this.b = false;',
  9914. '']),
  9915. LinesToStr([ // $mod.$main
  9916. '$mod.c = "\x00";',
  9917. '$mod.c = "\uFFFF";',
  9918. '$mod.Arr[66] = "a";',
  9919. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9920. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9921. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9922. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9923. '$mod.c = "a";',
  9924. '$mod.c = "z";',
  9925. '$mod.Arr2[1] = "f";',
  9926. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9927. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9928. '']));
  9929. end;
  9930. procedure TTestModule.TestArray_StaticMultiDim;
  9931. begin
  9932. StartProgram(false);
  9933. Add([
  9934. 'type',
  9935. ' TArrayInt = array[1..3] of longint;',
  9936. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9937. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  9938. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  9939. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  9940. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  9941. 'var',
  9942. ' Arr: TArrayInt;',
  9943. ' Arr2: TArrayArrayInt;',
  9944. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9945. ' Arr4: TArrayArrayInt;',
  9946. ' ArrDim2: TArrayDim2Int;',
  9947. ' ArrDim3: TArrayDim3Int;',
  9948. ' ArrDim4: TArrayDim4Int;',
  9949. ' i: longint;',
  9950. 'begin',
  9951. ' i:=low(arr);',
  9952. ' i:=low(arr2);',
  9953. ' i:=low(arr2[5]);',
  9954. ' i:=high(arr);',
  9955. ' i:=high(arr2);',
  9956. ' i:=high(arr2[6]);',
  9957. ' arr2[5]:=arr;',
  9958. ' arr2[6][2]:=i;',
  9959. ' i:=arr2[6][3];',
  9960. ' arr2[6,3]:=i;',
  9961. ' i:=arr2[5,2];',
  9962. ' arr2:=arr2;',// clone multi dim static array
  9963. ' arr3:=arr3;',// clone anonymous multi dim static array
  9964. ' arr4:=arr4;',
  9965. ' Arr:=Arr;',
  9966. ' ArrDim2:=ArrDim2;',
  9967. ' ArrDim3:=ArrDim3;',
  9968. ' ArrDim4:=ArrDim4;',
  9969. '']);
  9970. ConvertProgram;
  9971. CheckSource('TestArray_StaticMultiDim',
  9972. LinesToStr([ // statements
  9973. 'this.TArrayArrayInt$clone = function (a) {',
  9974. ' var b = [];',
  9975. ' b.length = 2;',
  9976. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9977. ' return b;',
  9978. '};',
  9979. 'this.TArrayArrayArrayInt$clone = function (a) {',
  9980. ' var b = [];',
  9981. ' b.length = 2;',
  9982. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  9983. ' return b;',
  9984. '};',
  9985. 'this.TArrayDim2Int$clone = function (a) {',
  9986. ' var b = [];',
  9987. ' b.length = 2;',
  9988. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9989. ' return b;',
  9990. '};',
  9991. 'this.TArrayDim3Int$clone = function (a) {',
  9992. ' var b = [];',
  9993. ' b.length = 2;',
  9994. ' for (var c = 0; c < 2; c++) {',
  9995. ' var d = b[c] = [];',
  9996. ' d.length = 3;',
  9997. ' var e = a[c];',
  9998. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  9999. ' };',
  10000. ' return b;',
  10001. '};',
  10002. 'this.TArrayDim4Int$clone = function (a) {',
  10003. ' var b = [];',
  10004. ' b.length = 2;',
  10005. ' for (var c = 0; c < 2; c++) {',
  10006. ' var d = b[c] = [];',
  10007. ' d.length = 3;',
  10008. ' var e = a[c];',
  10009. ' for (var f = 0; f < 3; f++) {',
  10010. ' var g = d[f] = [];',
  10011. ' g.length = 4;',
  10012. ' var h = e[f];',
  10013. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10014. ' };',
  10015. ' };',
  10016. ' return b;',
  10017. '};',
  10018. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10019. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10020. 'this.Arr3$a$clone = function (a) {',
  10021. ' var b = [];',
  10022. ' b.length = 2;',
  10023. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10024. ' return b;',
  10025. '};',
  10026. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10027. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10028. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10029. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10030. 'this.ArrDim4 = rtl.arraySetLength(',
  10031. ' null,',
  10032. ' 0,',
  10033. ' 2,',
  10034. ' 3,',
  10035. ' 4,',
  10036. ' 5',
  10037. ');',
  10038. 'this.i = 0;'
  10039. ]),
  10040. LinesToStr([ // $mod.$main
  10041. '$mod.i = 1;',
  10042. '$mod.i = 5;',
  10043. '$mod.i = 1;',
  10044. '$mod.i = 3;',
  10045. '$mod.i = 6;',
  10046. '$mod.i = 3;',
  10047. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10048. '$mod.Arr2[1][1] = $mod.i;',
  10049. '$mod.i = $mod.Arr2[1][2];',
  10050. '$mod.Arr2[1][2] = $mod.i;',
  10051. '$mod.i = $mod.Arr2[0][1];',
  10052. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10053. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10054. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10055. '$mod.Arr = $mod.Arr.slice(0);',
  10056. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10057. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10058. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10059. '']));
  10060. end;
  10061. procedure TTestModule.TestArray_StaticInFunction;
  10062. begin
  10063. StartProgram(false);
  10064. Add([
  10065. 'const TArrayInt = 3;',
  10066. 'const TArrayArrayInt = 4;',
  10067. 'procedure DoIt;',
  10068. 'type',
  10069. ' TArrayInt = array[1..3] of longint;',
  10070. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10071. 'var',
  10072. ' Arr: TArrayInt;',
  10073. ' Arr2: TArrayArrayInt;',
  10074. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10075. ' i: longint;',
  10076. 'begin',
  10077. ' arr2[5]:=arr;',
  10078. ' arr2:=arr2;',// clone multi dim static array
  10079. ' arr3:=arr3;',// clone multi dim anonymous static array
  10080. 'end;',
  10081. 'begin',
  10082. '']);
  10083. ConvertProgram;
  10084. CheckSource('TestArray_StaticInFunction',
  10085. LinesToStr([ // statements
  10086. 'this.TArrayInt = 3;',
  10087. 'this.TArrayArrayInt = 4;',
  10088. 'var TArrayArrayInt$1$clone = function (a) {',
  10089. ' var b = [];',
  10090. ' b.length = 2;',
  10091. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10092. ' return b;',
  10093. '};',
  10094. 'var Arr3$a$clone = function (a) {',
  10095. ' var b = [];',
  10096. ' b.length = 2;',
  10097. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10098. ' return b;',
  10099. '};',
  10100. 'this.DoIt = function () {',
  10101. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10102. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10103. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10104. ' var i = 0;',
  10105. ' Arr2[0] = Arr.slice(0);',
  10106. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10107. ' Arr3 = Arr3$a$clone(Arr3);',
  10108. '};',
  10109. '']),
  10110. LinesToStr([ // $mod.$main
  10111. '']));
  10112. end;
  10113. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10114. begin
  10115. StartProgram(false);
  10116. Add([
  10117. 'type',
  10118. ' TArrayInt = array[1..3,1..2] of longint;',
  10119. 'var',
  10120. ' a,b: TArrayInt;',
  10121. 'begin',
  10122. ' if a=b then ;',
  10123. '']);
  10124. SetExpectedPasResolverError('compare static array is not supported',
  10125. nXIsNotSupported);
  10126. ConvertProgram;
  10127. end;
  10128. procedure TTestModule.TestArrayOfRecord;
  10129. begin
  10130. StartProgram(false);
  10131. Add([
  10132. 'type',
  10133. ' TRec = record',
  10134. ' Int: longint;',
  10135. ' end;',
  10136. ' TArrayRec = array of TRec;',
  10137. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10138. 'begin',
  10139. 'end;',
  10140. 'var',
  10141. ' Arr: TArrayRec;',
  10142. ' r: TRec;',
  10143. ' i: longint;',
  10144. 'begin',
  10145. ' SetLength(arr,3);',
  10146. ' arr[0].int:=4;',
  10147. ' arr[1].int:=length(arr)+arr[2].int;',
  10148. ' arr[arr[i].int].int:=arr[5].int;',
  10149. ' arr[7]:=r;',
  10150. ' r:=arr[8];',
  10151. ' i:=low(arr);',
  10152. ' i:=high(arr);',
  10153. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10154. ConvertProgram;
  10155. CheckSource('TestArrayOfRecord',
  10156. LinesToStr([ // statements
  10157. 'rtl.recNewT(this, "TRec", function () {',
  10158. ' this.Int = 0;',
  10159. ' this.$eq = function (b) {',
  10160. ' return this.Int === b.Int;',
  10161. ' };',
  10162. ' this.$assign = function (s) {',
  10163. ' this.Int = s.Int;',
  10164. ' return this;',
  10165. ' };',
  10166. '});',
  10167. 'this.DoIt = function (vd, vc, vv) {',
  10168. '};',
  10169. 'this.Arr = [];',
  10170. 'this.r = this.TRec.$new();',
  10171. 'this.i = 0;'
  10172. ]),
  10173. LinesToStr([ // $mod.$main
  10174. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10175. '$mod.Arr[0].Int = 4;',
  10176. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10177. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10178. '$mod.Arr[7].$assign($mod.r);',
  10179. '$mod.r.$assign($mod.Arr[8]);',
  10180. '$mod.i = 0;',
  10181. '$mod.i = rtl.length($mod.Arr)-1;',
  10182. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10183. '']));
  10184. end;
  10185. procedure TTestModule.TestArray_StaticRecord;
  10186. begin
  10187. StartProgram(false);
  10188. Add([
  10189. 'type',
  10190. ' TRec = record',
  10191. ' Int: longint;',
  10192. ' end;',
  10193. ' TArrayRec = array[1..2] of TRec;',
  10194. 'var',
  10195. ' Arr: TArrayRec;',
  10196. 'begin',
  10197. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10198. '']);
  10199. ConvertProgram;
  10200. CheckSource('TestArray_StaticRecord',
  10201. LinesToStr([ // statements
  10202. 'rtl.recNewT(this, "TRec", function () {',
  10203. ' this.Int = 0;',
  10204. ' this.$eq = function (b) {',
  10205. ' return this.Int === b.Int;',
  10206. ' };',
  10207. ' this.$assign = function (s) {',
  10208. ' this.Int = s.Int;',
  10209. ' return this;',
  10210. ' };',
  10211. '});',
  10212. 'this.TArrayRec$clone = function (a) {',
  10213. ' var b = [];',
  10214. ' b.length = 2;',
  10215. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10216. ' return b;',
  10217. '};',
  10218. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10219. '']),
  10220. LinesToStr([ // $mod.$main
  10221. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10222. end;
  10223. procedure TTestModule.TestArrayOfSet;
  10224. begin
  10225. StartProgram(false);
  10226. Add([
  10227. 'type',
  10228. ' TFlag = (big,small);',
  10229. ' TSetOfFlag = set of tflag;',
  10230. ' TArrayFlag = array of TSetOfFlag;',
  10231. 'procedure DoIt(const a: Tarrayflag);',
  10232. 'begin',
  10233. 'end;',
  10234. 'var',
  10235. ' f: TFlag;',
  10236. ' s: TSetOfFlag;',
  10237. ' Arr: TArrayFlag;',
  10238. ' i: longint;',
  10239. 'begin',
  10240. ' SetLength(arr,3);',
  10241. ' arr[0]:=s;',
  10242. ' arr[1]:=[big];',
  10243. ' arr[2]:=[big]+s;',
  10244. ' arr[3]:=s+[big];',
  10245. ' arr[4]:=arr[5];',
  10246. ' s:=arr[6];',
  10247. ' i:=low(arr);',
  10248. ' i:=high(arr);',
  10249. ' DoIt(arr);',
  10250. ' DoIt([s]);',
  10251. ' DoIt([[],s]);',
  10252. ' DoIt([s,[]]);',
  10253. '']);
  10254. ConvertProgram;
  10255. CheckSource('TestArrayOfSet',
  10256. LinesToStr([ // statements
  10257. 'this.TFlag = {',
  10258. ' "0": "big",',
  10259. ' big: 0,',
  10260. ' "1": "small",',
  10261. ' small: 1',
  10262. '};',
  10263. 'this.DoIt = function (a) {',
  10264. '};',
  10265. 'this.f = 0;',
  10266. 'this.s = {};',
  10267. 'this.Arr = [];',
  10268. 'this.i = 0;',
  10269. '']),
  10270. LinesToStr([ // $mod.$main
  10271. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10272. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10273. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10274. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10275. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10276. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10277. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10278. '$mod.i = 0;',
  10279. '$mod.i = rtl.length($mod.Arr) - 1;',
  10280. '$mod.DoIt($mod.Arr);',
  10281. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10282. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10283. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10284. '']));
  10285. end;
  10286. procedure TTestModule.TestArray_DynAsParam;
  10287. begin
  10288. StartProgram(false);
  10289. Add([
  10290. 'type integer = longint;',
  10291. 'type TArrInt = array of integer;',
  10292. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10293. 'var vJ: TArrInt;',
  10294. 'begin',
  10295. ' vg:=vg;',
  10296. ' vj:=vh;',
  10297. ' vi:=vi;',
  10298. ' doit(vg,vg,vg);',
  10299. ' doit(vh,vh,vj);',
  10300. ' doit(vi,vi,vi);',
  10301. ' doit(vj,vj,vj);',
  10302. 'end;',
  10303. 'var i: TArrInt;',
  10304. 'begin',
  10305. ' doit(i,i,i);']);
  10306. ConvertProgram;
  10307. CheckSource('TestArray_DynAsParams',
  10308. LinesToStr([ // statements
  10309. 'this.DoIt = function (vG,vH,vI) {',
  10310. ' var vJ = [];',
  10311. ' vG = rtl.arrayRef(vG);',
  10312. ' vJ = rtl.arrayRef(vH);',
  10313. ' vI.set(rtl.arrayRef(vI.get()));',
  10314. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10315. ' get: function () {',
  10316. ' return vG;',
  10317. ' },',
  10318. ' set: function (v) {',
  10319. ' vG = v;',
  10320. ' }',
  10321. ' });',
  10322. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10323. ' get: function () {',
  10324. ' return vJ;',
  10325. ' },',
  10326. ' set: function (v) {',
  10327. ' vJ = v;',
  10328. ' }',
  10329. ' });',
  10330. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10331. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10332. ' get: function () {',
  10333. ' return vJ;',
  10334. ' },',
  10335. ' set: function (v) {',
  10336. ' vJ = v;',
  10337. ' }',
  10338. ' });',
  10339. '};',
  10340. 'this.i = [];'
  10341. ]),
  10342. LinesToStr([
  10343. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10344. ' p: $mod,',
  10345. ' get: function () {',
  10346. ' return this.p.i;',
  10347. ' },',
  10348. ' set: function (v) {',
  10349. ' this.p.i = v;',
  10350. ' }',
  10351. '});'
  10352. ]));
  10353. end;
  10354. procedure TTestModule.TestArray_StaticAsParam;
  10355. begin
  10356. StartProgram(false);
  10357. Add([
  10358. 'type integer = longint;',
  10359. 'type TArrInt = array[1..2] of integer;',
  10360. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10361. 'var vJ: TArrInt;',
  10362. 'begin',
  10363. ' vg:=vg;',
  10364. ' vj:=vh;',
  10365. ' vi:=vi;',
  10366. ' doit(vg,vg,vg);',
  10367. ' doit(vh,vh,vj);',
  10368. ' doit(vi,vi,vi);',
  10369. ' doit(vj,vj,vj);',
  10370. 'end;',
  10371. 'var i: TArrInt;',
  10372. 'begin',
  10373. ' doit(i,i,i);']);
  10374. ConvertProgram;
  10375. CheckSource('TestArray_StaticAsParams',
  10376. LinesToStr([ // statements
  10377. 'this.DoIt = function (vG,vH,vI) {',
  10378. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10379. ' vG = vG.slice(0);',
  10380. ' vJ = vH.slice(0);',
  10381. ' vI.set(vI.get().slice(0));',
  10382. ' $mod.DoIt(vG.slice(0), vG, {',
  10383. ' get: function () {',
  10384. ' return vG;',
  10385. ' },',
  10386. ' set: function (v) {',
  10387. ' vG = v;',
  10388. ' }',
  10389. ' });',
  10390. ' $mod.DoIt(vH.slice(0), vH, {',
  10391. ' get: function () {',
  10392. ' return vJ;',
  10393. ' },',
  10394. ' set: function (v) {',
  10395. ' vJ = v;',
  10396. ' }',
  10397. ' });',
  10398. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10399. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10400. ' get: function () {',
  10401. ' return vJ;',
  10402. ' },',
  10403. ' set: function (v) {',
  10404. ' vJ = v;',
  10405. ' }',
  10406. ' });',
  10407. '};',
  10408. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10409. ]),
  10410. LinesToStr([
  10411. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10412. ' p: $mod,',
  10413. ' get: function () {',
  10414. ' return this.p.i;',
  10415. ' },',
  10416. ' set: function (v) {',
  10417. ' this.p.i = v;',
  10418. ' }',
  10419. '});'
  10420. ]));
  10421. end;
  10422. procedure TTestModule.TestArrayElement_AsParams;
  10423. begin
  10424. StartProgram(false);
  10425. Add('type integer = longint;');
  10426. Add('type TArrayInt = array of integer;');
  10427. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10428. Add('var vJ: tarrayint;');
  10429. Add('begin');
  10430. Add(' vi:=vi;');
  10431. Add(' doit(vi,vi,vi);');
  10432. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10433. Add('end;');
  10434. Add('var a: TArrayInt;');
  10435. Add('begin');
  10436. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10437. ConvertProgram;
  10438. CheckSource('TestArrayElement_AsParams',
  10439. LinesToStr([ // statements
  10440. 'this.DoIt = function (vG,vH,vI) {',
  10441. ' var vJ = [];',
  10442. ' vI.set(vI.get());',
  10443. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10444. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10445. ' a:1+3,',
  10446. ' p:vJ,',
  10447. ' get: function () {',
  10448. ' return this.p[this.a];',
  10449. ' },',
  10450. ' set: function (v) {',
  10451. ' this.p[this.a] = v;',
  10452. ' }',
  10453. ' });',
  10454. '};',
  10455. 'this.a = [];'
  10456. ]),
  10457. LinesToStr([
  10458. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10459. ' a: 1+6,',
  10460. ' p: $mod.a,',
  10461. ' get: function () {',
  10462. ' return this.p[this.a];',
  10463. ' },',
  10464. ' set: function (v) {',
  10465. ' this.p[this.a] = v;',
  10466. ' }',
  10467. '});'
  10468. ]));
  10469. end;
  10470. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10471. begin
  10472. StartProgram(false);
  10473. Add('type Integer = longint;');
  10474. Add('type TArrayInt = array of integer;');
  10475. Add('function GetArr(vB: integer = 0): tarrayint;');
  10476. Add('begin');
  10477. Add('end;');
  10478. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10479. Add('begin');
  10480. Add('end;');
  10481. Add('begin');
  10482. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10483. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10484. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10485. ConvertProgram;
  10486. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10487. LinesToStr([ // statements
  10488. 'this.GetArr = function (vB) {',
  10489. ' var Result = [];',
  10490. ' return Result;',
  10491. '};',
  10492. 'this.DoIt = function (vG,vH,vI) {',
  10493. '};'
  10494. ]),
  10495. LinesToStr([
  10496. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10497. ' a: 1+3,',
  10498. ' p: $mod.GetArr(0),',
  10499. ' get: function () {',
  10500. ' return this.p[this.a];',
  10501. ' },',
  10502. ' set: function (v) {',
  10503. ' this.p[this.a] = v;',
  10504. ' }',
  10505. '});',
  10506. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10507. ' a: 2+3,',
  10508. ' p: $mod.GetArr(0),',
  10509. ' get: function () {',
  10510. ' return this.p[this.a];',
  10511. ' },',
  10512. ' set: function (v) {',
  10513. ' this.p[this.a] = v;',
  10514. ' }',
  10515. '});',
  10516. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10517. ' a: 3+3,',
  10518. ' p: $mod.GetArr(9),',
  10519. ' get: function () {',
  10520. ' return this.p[this.a];',
  10521. ' },',
  10522. ' set: function (v) {',
  10523. ' this.p[this.a] = v;',
  10524. ' }',
  10525. '});',
  10526. '']));
  10527. end;
  10528. procedure TTestModule.TestArrayEnumTypeRange;
  10529. begin
  10530. StartProgram(false);
  10531. Add([
  10532. 'type',
  10533. ' TEnum = (red,blue);',
  10534. ' TEnumArray = array[TEnum] of longint;',
  10535. 'var',
  10536. ' e: TEnum;',
  10537. ' i: longint;',
  10538. ' a: TEnumArray;',
  10539. ' numbers: TEnumArray = (1,2);',
  10540. ' names: array[TEnum] of string = (''red'',''blue'');',
  10541. 'begin',
  10542. ' e:=low(a);',
  10543. ' e:=high(a);',
  10544. ' i:=a[red];',
  10545. ' a[e]:=a[e];']);
  10546. ConvertProgram;
  10547. CheckSource('TestArrayEnumTypeRange',
  10548. LinesToStr([ // statements
  10549. ' this.TEnum = {',
  10550. ' "0": "red",',
  10551. ' red: 0,',
  10552. ' "1": "blue",',
  10553. ' blue: 1',
  10554. '};',
  10555. 'this.e = 0;',
  10556. 'this.i = 0;',
  10557. 'this.a = rtl.arraySetLength(null,0,2);',
  10558. 'this.numbers = [1, 2];',
  10559. 'this.names = ["red", "blue"];',
  10560. '']),
  10561. LinesToStr([ // $mod.$main
  10562. '$mod.e = $mod.TEnum.red;',
  10563. '$mod.e = $mod.TEnum.blue;',
  10564. '$mod.i = $mod.a[$mod.TEnum.red];',
  10565. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10566. '']));
  10567. end;
  10568. procedure TTestModule.TestArray_SetLengthOutArg;
  10569. begin
  10570. StartProgram(false);
  10571. Add([
  10572. 'type TArrInt = array of longint;',
  10573. 'procedure DoIt(out a: TArrInt);',
  10574. 'begin',
  10575. ' SetLength(a,2);',
  10576. 'end;',
  10577. 'begin',
  10578. '']);
  10579. ConvertProgram;
  10580. CheckSource('TestArray_SetLengthOutArg',
  10581. LinesToStr([ // statements
  10582. 'this.DoIt = function (a) {',
  10583. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10584. '};',
  10585. '']),
  10586. LinesToStr([
  10587. '']));
  10588. end;
  10589. procedure TTestModule.TestArray_SetLengthProperty;
  10590. begin
  10591. StartProgram(false);
  10592. Add('type');
  10593. Add(' TArrInt = array of longint;');
  10594. Add(' TObject = class');
  10595. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10596. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10597. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10598. Add(' end;');
  10599. Add('var Obj: TObject;');
  10600. Add('begin');
  10601. Add(' SetLength(Obj.Colors,2);');
  10602. ConvertProgram;
  10603. CheckSource('TestArray_SetLengthProperty',
  10604. LinesToStr([ // statements
  10605. 'rtl.createClass(this, "TObject", null, function () {',
  10606. ' this.$init = function () {',
  10607. ' };',
  10608. ' this.$final = function () {',
  10609. ' };',
  10610. '});',
  10611. 'this.Obj = null;',
  10612. '']),
  10613. LinesToStr([
  10614. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10615. '']));
  10616. end;
  10617. procedure TTestModule.TestArray_SetLengthMultiDim;
  10618. begin
  10619. StartProgram(false);
  10620. Add([
  10621. 'type',
  10622. ' TArrArrInt = array of array of longint;',
  10623. ' TArrStaInt = array of array[1..2] of longint;',
  10624. 'var',
  10625. ' a: TArrArrInt;',
  10626. ' b: TArrStaInt;',
  10627. 'begin',
  10628. ' SetLength(a,2);',
  10629. ' SetLength(a,3,4);',
  10630. ' SetLength(b,5);',
  10631. '']);
  10632. ConvertProgram;
  10633. CheckSource('TestArray_SetLengthMultiDim',
  10634. LinesToStr([ // statements
  10635. 'this.a = [];',
  10636. 'this.b = [];',
  10637. '']),
  10638. LinesToStr([
  10639. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10640. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10641. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10642. '']));
  10643. end;
  10644. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10645. begin
  10646. StartProgram(false);
  10647. Add([
  10648. 'type',
  10649. ' TStaArr1 = array[1..3] of boolean;',
  10650. //' TStaArr2 = array[5..6] of TStaArr1;',
  10651. ' TDynArr1StaArr1 = array of TStaArr1;',
  10652. //' TDynArr1StaArr2 = array of TStaArr2;',
  10653. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10654. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10655. 'var',
  10656. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10657. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10658. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10659. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10660. 'begin',
  10661. ' SetLength(DynArr1StaArr1,11);',
  10662. ' SetLength(DynArr2StaArr1,12);',
  10663. ' SetLength(DynArr2StaArr1[13],14);',
  10664. ' SetLength(DynArr2StaArr1,15,16);',
  10665. //' SetLength(DynArr1StaArr2,21);',
  10666. //' SetLength(DynArr2StaArr2,22);',
  10667. //' SetLength(DynArr2StaArr2[23],24);',
  10668. //' SetLength(DynArr2StaArr2,25,26);',
  10669. '']);
  10670. ConvertProgram;
  10671. CheckSource('TestArray_DynOfStatic',
  10672. LinesToStr([ // statements
  10673. 'this.DynArr1StaArr1 = [];',
  10674. 'this.DynArr2StaArr1 = [];',
  10675. '']),
  10676. LinesToStr([ // $mod.$main
  10677. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10678. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10679. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10680. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10681. ' $mod.DynArr2StaArr1,',
  10682. ' false,',
  10683. ' 15,',
  10684. ' 16,',
  10685. ' "s",',
  10686. ' 3',
  10687. ');',
  10688. '']));
  10689. end;
  10690. procedure TTestModule.TestArray_OpenArrayOfString;
  10691. begin
  10692. StartProgram(false);
  10693. Add('procedure DoIt(const a: array of String);');
  10694. Add('var');
  10695. Add(' i: longint;');
  10696. Add(' s: string;');
  10697. Add('begin');
  10698. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10699. Add('end;');
  10700. Add('var s: string;');
  10701. Add('begin');
  10702. Add(' DoIt([]);');
  10703. Add(' DoIt([s,''foo'','''',s+s]);');
  10704. ConvertProgram;
  10705. CheckSource('TestArray_OpenArrayOfString',
  10706. LinesToStr([ // statements
  10707. 'this.DoIt = function (a) {',
  10708. ' var i = 0;',
  10709. ' var s = "";',
  10710. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10711. ' i = $l;',
  10712. ' s = a[rtl.length(a) - i - 1];',
  10713. ' };',
  10714. '};',
  10715. 'this.s = "";',
  10716. '']),
  10717. LinesToStr([
  10718. '$mod.DoIt([]);',
  10719. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10720. '']));
  10721. end;
  10722. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10723. begin
  10724. StartProgram(false);
  10725. Add([
  10726. 'type TArr = array of char;',
  10727. 'var',
  10728. ' c: char;',
  10729. ' s: string;',
  10730. ' a: TArr;',
  10731. 'procedure Run(const a: array of char);',
  10732. 'begin',
  10733. ' Run(c);',
  10734. ' Run(s);',
  10735. 'end;',
  10736. 'begin',
  10737. ' a:=c;',
  10738. ' a:=s;',
  10739. ' a:=#13;',
  10740. ' a:=''Foo'';',
  10741. ' Run(c);',
  10742. ' Run(s);',
  10743. '']);
  10744. ConvertProgram;
  10745. CheckSource('TestArray_ArrayOfCharAssignString',
  10746. LinesToStr([ // statements
  10747. 'this.c = "";',
  10748. 'this.s = "";',
  10749. 'this.a = [];',
  10750. 'this.Run = function (a) {',
  10751. ' $mod.Run($mod.c.split(""));',
  10752. ' $mod.Run($mod.s.split(""));',
  10753. '};',
  10754. '']),
  10755. LinesToStr([
  10756. '$mod.a = $mod.c.split("");',
  10757. '$mod.a = $mod.s.split("");',
  10758. '$mod.a = "\r".split("");',
  10759. '$mod.a = "Foo".split("");',
  10760. '$mod.Run($mod.c.split(""));',
  10761. '$mod.Run($mod.s.split(""));',
  10762. '']));
  10763. end;
  10764. procedure TTestModule.TestArray_ConstRef;
  10765. begin
  10766. StartProgram(false);
  10767. Add([
  10768. 'type TArr = array of word;',
  10769. 'procedure Run(constref a: TArr);',
  10770. 'begin',
  10771. 'end;',
  10772. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10773. 'var l: TArr;',
  10774. 'begin',
  10775. ' Run(l);',
  10776. ' Run(a);',
  10777. ' Run(b);',
  10778. ' Run(c);',
  10779. ' Run(d);',
  10780. ' Run(e);',
  10781. 'end;',
  10782. 'begin',
  10783. '']);
  10784. ConvertProgram;
  10785. CheckResolverUnexpectedHints();
  10786. CheckSource('TestArray_ConstRef',
  10787. LinesToStr([ // statements
  10788. 'this.Run = function (a) {',
  10789. '};',
  10790. 'this.Fly = function (a, b, c, d, e) {',
  10791. ' var l = [];',
  10792. ' $mod.Run(l);',
  10793. ' $mod.Run(a);',
  10794. ' $mod.Run(b.get());',
  10795. ' $mod.Run(c.get());',
  10796. ' $mod.Run(d);',
  10797. ' $mod.Run(e);',
  10798. '};',
  10799. '']),
  10800. LinesToStr([
  10801. '']));
  10802. end;
  10803. procedure TTestModule.TestArray_Concat;
  10804. begin
  10805. StartProgram(false);
  10806. Add([
  10807. 'type',
  10808. ' integer = longint;',
  10809. ' TFlag = (big,small);',
  10810. ' TFlags = set of TFlag;',
  10811. ' TRec = record',
  10812. ' i: integer;',
  10813. ' end;',
  10814. ' TArrInt = array of integer;',
  10815. ' TArrRec = array of TRec;',
  10816. ' TArrFlag = array of TFlag;',
  10817. ' TArrSet = array of TFlags;',
  10818. ' TArrJSValue = array of jsvalue;',
  10819. 'var',
  10820. ' ArrInt: tarrint;',
  10821. ' ArrRec: tarrrec;',
  10822. ' ArrFlag: tarrflag;',
  10823. ' ArrSet: tarrset;',
  10824. ' ArrJSValue: tarrjsvalue;',
  10825. 'begin',
  10826. ' arrint:=concat(arrint);',
  10827. ' arrint:=concat(arrint,arrint);',
  10828. ' arrint:=concat(arrint,arrint,arrint);',
  10829. ' arrrec:=concat(arrrec);',
  10830. ' arrrec:=concat(arrrec,arrrec);',
  10831. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  10832. ' arrset:=concat(arrset);',
  10833. ' arrset:=concat(arrset,arrset);',
  10834. ' arrset:=concat(arrset,arrset,arrset);',
  10835. ' arrjsvalue:=concat(arrjsvalue);',
  10836. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  10837. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  10838. ' arrint:=concat([1],arrint);',
  10839. ' arrflag:=concat([big]);',
  10840. ' arrflag:=concat([big],arrflag);',
  10841. ' arrflag:=concat(arrflag,[small]);',
  10842. '']);
  10843. ConvertProgram;
  10844. CheckSource('TestArray_Concat',
  10845. LinesToStr([ // statements
  10846. 'this.TFlag = {',
  10847. ' "0": "big",',
  10848. ' big: 0,',
  10849. ' "1": "small",',
  10850. ' small: 1',
  10851. '};',
  10852. 'rtl.recNewT(this, "TRec", function () {',
  10853. ' this.i = 0;',
  10854. ' this.$eq = function (b) {',
  10855. ' return this.i === b.i;',
  10856. ' };',
  10857. ' this.$assign = function (s) {',
  10858. ' this.i = s.i;',
  10859. ' return this;',
  10860. ' };',
  10861. '});',
  10862. 'this.ArrInt = [];',
  10863. 'this.ArrRec = [];',
  10864. 'this.ArrFlag = [];',
  10865. 'this.ArrSet = [];',
  10866. 'this.ArrJSValue = [];',
  10867. '']),
  10868. LinesToStr([ // $mod.$main
  10869. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  10870. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  10871. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  10872. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  10873. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  10874. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  10875. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  10876. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  10877. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  10878. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  10879. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  10880. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  10881. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  10882. '$mod.ArrFlag = [$mod.TFlag.big];',
  10883. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  10884. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  10885. '']));
  10886. end;
  10887. procedure TTestModule.TestArray_Copy;
  10888. begin
  10889. StartProgram(false);
  10890. Add([
  10891. 'type',
  10892. ' integer = longint;',
  10893. ' TFlag = (big,small);',
  10894. ' TFlags = set of TFlag;',
  10895. ' TRec = record',
  10896. ' i: integer;',
  10897. ' end;',
  10898. ' TArrInt = array of integer;',
  10899. ' TArrRec = array of TRec;',
  10900. ' TArrSet = array of TFlags;',
  10901. ' TArrJSValue = array of jsvalue;',
  10902. 'var',
  10903. ' ArrInt: tarrint;',
  10904. ' ArrRec: tarrrec;',
  10905. ' ArrSet: tarrset;',
  10906. ' ArrJSValue: tarrjsvalue;',
  10907. 'begin',
  10908. ' arrint:=copy(arrint);',
  10909. ' arrint:=copy(arrint,2);',
  10910. ' arrint:=copy(arrint,3,4);',
  10911. ' arrint:=copy([1,1],1,2);',
  10912. ' arrrec:=copy(arrrec);',
  10913. ' arrrec:=copy(arrrec,5);',
  10914. ' arrrec:=copy(arrrec,6,7);',
  10915. ' arrset:=copy(arrset);',
  10916. ' arrset:=copy(arrset,8);',
  10917. ' arrset:=copy(arrset,9,10);',
  10918. ' arrjsvalue:=copy(arrjsvalue);',
  10919. ' arrjsvalue:=copy(arrjsvalue,11);',
  10920. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10921. ' ']);
  10922. ConvertProgram;
  10923. CheckSource('TestArray_Copy',
  10924. LinesToStr([ // statements
  10925. 'this.TFlag = {',
  10926. ' "0": "big",',
  10927. ' big: 0,',
  10928. ' "1": "small",',
  10929. ' small: 1',
  10930. '};',
  10931. 'rtl.recNewT(this, "TRec", function () {',
  10932. ' this.i = 0;',
  10933. ' this.$eq = function (b) {',
  10934. ' return this.i === b.i;',
  10935. ' };',
  10936. ' this.$assign = function (s) {',
  10937. ' this.i = s.i;',
  10938. ' return this;',
  10939. ' };',
  10940. '});',
  10941. 'this.ArrInt = [];',
  10942. 'this.ArrRec = [];',
  10943. 'this.ArrSet = [];',
  10944. 'this.ArrJSValue = [];',
  10945. '']),
  10946. LinesToStr([ // $mod.$main
  10947. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10948. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10949. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10950. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10951. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10952. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10953. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10954. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10955. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10956. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10957. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10958. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10959. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10960. '']));
  10961. end;
  10962. procedure TTestModule.TestArray_InsertDelete;
  10963. begin
  10964. StartProgram(false);
  10965. Add([
  10966. 'type',
  10967. ' integer = longint;',
  10968. ' TFlag = (big,small);',
  10969. ' TFlags = set of TFlag;',
  10970. ' TRec = record',
  10971. ' i: integer;',
  10972. ' end;',
  10973. ' TArrInt = array of integer;',
  10974. ' TArrRec = array of TRec;',
  10975. ' TArrSet = array of TFlags;',
  10976. ' TArrJSValue = array of jsvalue;',
  10977. ' TArrArrInt = array of TArrInt;',
  10978. 'var',
  10979. ' ArrInt: tarrint;',
  10980. ' ArrRec: tarrrec;',
  10981. ' ArrSet: tarrset;',
  10982. ' ArrJSValue: tarrjsvalue;',
  10983. ' ArrArrInt: TArrArrInt;',
  10984. 'begin',
  10985. ' Insert(1,arrint,2);',
  10986. ' Insert(arrint[3],arrint,4);',
  10987. ' Insert(arrrec[5],arrrec,6);',
  10988. ' Insert(arrset[7],arrset,7);',
  10989. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10990. ' Insert(10,arrjsvalue,11);',
  10991. ' Insert([23],arrarrint,22);',
  10992. ' Delete(arrint,12,13);',
  10993. ' Delete(arrrec,14,15);',
  10994. ' Delete(arrset,17,18);',
  10995. ' Delete(arrjsvalue,19,10);']);
  10996. ConvertProgram;
  10997. CheckSource('TestArray_InsertDelete',
  10998. LinesToStr([ // statements
  10999. 'this.TFlag = {',
  11000. ' "0": "big",',
  11001. ' big: 0,',
  11002. ' "1": "small",',
  11003. ' small: 1',
  11004. '};',
  11005. 'rtl.recNewT(this, "TRec", function () {',
  11006. ' this.i = 0;',
  11007. ' this.$eq = function (b) {',
  11008. ' return this.i === b.i;',
  11009. ' };',
  11010. ' this.$assign = function (s) {',
  11011. ' this.i = s.i;',
  11012. ' return this;',
  11013. ' };',
  11014. '});',
  11015. 'this.ArrInt = [];',
  11016. 'this.ArrRec = [];',
  11017. 'this.ArrSet = [];',
  11018. 'this.ArrJSValue = [];',
  11019. 'this.ArrArrInt = [];',
  11020. '']),
  11021. LinesToStr([ // $mod.$main
  11022. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11023. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11024. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11025. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11026. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11027. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11028. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11029. '$mod.ArrInt.splice(12, 13);',
  11030. '$mod.ArrRec.splice(14, 15);',
  11031. '$mod.ArrSet.splice(17, 18);',
  11032. '$mod.ArrJSValue.splice(19, 10);',
  11033. '']));
  11034. end;
  11035. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11036. begin
  11037. Parser.Options:=Parser.Options+[po_cassignments];
  11038. StartProgram(false);
  11039. Add([
  11040. '{$modeswitch arrayoperators}',
  11041. 'type',
  11042. ' integer = longint;',
  11043. ' TArrInt = array of integer;',
  11044. ' TArrStr = array of string;',
  11045. 'const',
  11046. ' Ints: TArrInt = (1,2,3);',
  11047. ' Aliases: TarrStr = (''foo'',''b'');',
  11048. ' OneInt: TArrInt = (7);',
  11049. ' OneStr: array of integer = (7);',
  11050. ' Chars: array of char = ''aoc'';',
  11051. ' Names: array of string = (''a'',''foo'');',
  11052. ' NameCount = low(Names)+high(Names)+length(Names);',
  11053. 'var i: integer;',
  11054. 'begin',
  11055. ' Ints:=[];',
  11056. ' Ints:=[1,1];',
  11057. ' Ints:=[1]+[2];',
  11058. ' Ints:=[2];',
  11059. ' Ints:=[]+ints;',
  11060. ' Ints:=Ints+[];',
  11061. ' Ints:=Ints+OneInt;',
  11062. ' Ints:=Ints+[1,1];',
  11063. ' Ints:=[i,i]+Ints;',
  11064. ' Ints:=[1]+[i]+[3];',
  11065. '']);
  11066. ConvertProgram;
  11067. CheckSource('TestArray_DynArrayConstObjFPC',
  11068. LinesToStr([ // statements
  11069. 'this.Ints = [1, 2, 3];',
  11070. 'this.Aliases = ["foo", "b"];',
  11071. 'this.OneInt = [7];',
  11072. 'this.OneStr = [7];',
  11073. 'this.Chars = ["a", "o", "c"];',
  11074. 'this.Names = ["a", "foo"];',
  11075. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11076. 'this.i = 0;',
  11077. '']),
  11078. LinesToStr([ // $mod.$main
  11079. '$mod.Ints = [];',
  11080. '$mod.Ints = [1, 1];',
  11081. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11082. '$mod.Ints = [2];',
  11083. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11084. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  11085. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11086. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  11087. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11088. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11089. '']));
  11090. end;
  11091. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11092. begin
  11093. StartProgram(false);
  11094. // Note: const c = [1,1]; defines a set!
  11095. Add([
  11096. '{$mode delphi}',
  11097. 'type',
  11098. ' integer = longint;',
  11099. ' TArrInt = array of integer;',
  11100. ' TArrStr = array of string;',
  11101. 'const',
  11102. ' Ints: TArrInt = [1,1,2];',
  11103. ' Aliases: TarrStr = [''foo'',''b''];',
  11104. ' OneInt: TArrInt = [7];',
  11105. ' OneStr: array of integer = [7]+[8];',
  11106. ' Chars: array of char = ''aoc'';',
  11107. ' Names: array of string = [''a'',''a''];',
  11108. ' NameCount = low(Names)+high(Names)+length(Names);',
  11109. 'begin',
  11110. '']);
  11111. ConvertProgram;
  11112. CheckSource('TestArray_DynArrayConstDelphi',
  11113. LinesToStr([ // statements
  11114. 'this.Ints = [1, 1, 2];',
  11115. 'this.Aliases = ["foo", "b"];',
  11116. 'this.OneInt = [7];',
  11117. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11118. 'this.Chars = ["a", "o", "c"];',
  11119. 'this.Names = ["a", "a"];',
  11120. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11121. '']),
  11122. LinesToStr([ // $mod.$main
  11123. '']));
  11124. end;
  11125. procedure TTestModule.TestArray_ArrayLitAsParam;
  11126. begin
  11127. StartProgram(false);
  11128. Add([
  11129. '{$modeswitch arrayoperators}',
  11130. 'type',
  11131. ' integer = longint;',
  11132. ' TArrInt = array of integer;',
  11133. ' TArrSet = array of (red,green,blue);',
  11134. 'procedure DoOpenInt(const a: array of integer); forward;',
  11135. 'procedure DoInt(const a: TArrInt);',
  11136. 'begin',
  11137. ' DoInt(a+[1]);',
  11138. ' DoInt([1]+a);',
  11139. ' DoOpenInt(a);',
  11140. ' DoOpenInt(a+[1]);',
  11141. ' DoOpenInt([1]+a);',
  11142. 'end;',
  11143. 'procedure DoOpenInt(const a: array of integer);',
  11144. 'begin',
  11145. ' DoOpenInt(a+[1]);',
  11146. ' DoOpenInt([1]+a);',
  11147. ' DoInt(a);',
  11148. ' DoInt(a+[1]);',
  11149. ' DoInt([1]+a);',
  11150. 'end;',
  11151. 'procedure DoSet(const a: TArrSet);',
  11152. 'begin',
  11153. ' DoSet(a+[red]);',
  11154. ' DoSet([blue]+a);',
  11155. 'end;',
  11156. 'var',
  11157. ' i: TArrInt;',
  11158. ' s: TArrSet;',
  11159. 'begin',
  11160. ' DoInt([1]);',
  11161. ' DoInt([1]+[2]);',
  11162. ' DoInt(i+[1]);',
  11163. ' DoInt([1]+i);',
  11164. ' DoOpenInt([1]);',
  11165. ' DoOpenInt([1]+[2]);',
  11166. ' DoOpenInt(i+[1]);',
  11167. ' DoOpenInt([1]+i);',
  11168. ' DoSet([red]);',
  11169. ' DoSet([blue]+[green]);',
  11170. ' DoSet(s+[blue]);',
  11171. ' DoSet([red]+s);',
  11172. '']);
  11173. ConvertProgram;
  11174. CheckSource('TestArray_ArrayLitAsParam',
  11175. LinesToStr([ // statements
  11176. 'this.TArrSet$a = {',
  11177. ' "0": "red",',
  11178. ' red: 0,',
  11179. ' "1": "green",',
  11180. ' green: 1,',
  11181. ' "2": "blue",',
  11182. ' blue: 2',
  11183. '};',
  11184. 'this.DoInt = function (a) {',
  11185. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11186. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11187. ' $mod.DoOpenInt(a);',
  11188. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11189. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11190. '};',
  11191. 'this.DoOpenInt = function (a) {',
  11192. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11193. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11194. ' $mod.DoInt(a);',
  11195. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11196. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11197. '};',
  11198. 'this.DoSet = function (a) {',
  11199. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11200. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11201. '};',
  11202. 'this.i = [];',
  11203. 'this.s = [];',
  11204. '']),
  11205. LinesToStr([ // $mod.$main
  11206. '$mod.DoInt([1]);',
  11207. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11208. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11209. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11210. '$mod.DoOpenInt([1]);',
  11211. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11212. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11213. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11214. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11215. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11216. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11217. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11218. '']));
  11219. end;
  11220. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11221. begin
  11222. StartProgram(false);
  11223. Add([
  11224. '{$modeswitch arrayoperators}',
  11225. 'type',
  11226. ' integer = longint;',
  11227. ' TArrInt = array of integer;',
  11228. ' TArrArrInt = array of TArrInt;',
  11229. 'procedure DoInt(const a: TArrArrInt);',
  11230. 'begin',
  11231. ' DoInt(a+[[1]]);',
  11232. ' DoInt([[1]]+a);',
  11233. ' DoInt(a);',
  11234. 'end;',
  11235. 'var',
  11236. ' i: TArrInt;',
  11237. ' a: TArrArrInt;',
  11238. 'begin',
  11239. ' a:=[[1]];',
  11240. ' a:=[i];',
  11241. ' a:=a+[i];',
  11242. ' a:=[i]+a;',
  11243. ' a:=[[1]+i];',
  11244. ' a:=[[1]+[2]];',
  11245. ' a:=[i+[2]];',
  11246. ' DoInt([[1]]);',
  11247. ' DoInt([[1]+[2],[3,4],[5]]);',
  11248. ' DoInt([i+[1]]+a);',
  11249. ' DoInt([i]+a);',
  11250. '']);
  11251. ConvertProgram;
  11252. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11253. LinesToStr([ // statements
  11254. 'this.DoInt = function (a) {',
  11255. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11256. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11257. ' $mod.DoInt(a);',
  11258. '};',
  11259. 'this.i = [];',
  11260. 'this.a = [];',
  11261. '']),
  11262. LinesToStr([ // $mod.$main
  11263. '$mod.a = [[1]];',
  11264. '$mod.a = [$mod.i];',
  11265. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  11266. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11267. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11268. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11269. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11270. '$mod.DoInt([[1]]);',
  11271. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11272. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11273. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11274. '']));
  11275. end;
  11276. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11277. begin
  11278. StartProgram(false);
  11279. Add([
  11280. '{$modeswitch arrayoperators}',
  11281. 'type',
  11282. ' integer = longint;',
  11283. ' TArrInt = array[1..2] of integer;',
  11284. ' TArrArrInt = array of TArrInt;',
  11285. 'procedure DoInt(const a: TArrArrInt);',
  11286. 'begin',
  11287. ' DoInt(a+[[1,2]]);',
  11288. ' DoInt([[1,2]]+a);',
  11289. ' DoInt(a);',
  11290. 'end;',
  11291. 'var',
  11292. ' i: TArrInt;',
  11293. ' a: TArrArrInt;',
  11294. 'begin',
  11295. ' a:=[[1,1]];',
  11296. ' a:=[i];',
  11297. ' a:=a+[i];',
  11298. ' a:=[i]+a;',
  11299. ' DoInt([[1,1]]);',
  11300. ' DoInt([[1,2],[3,4]]);',
  11301. '']);
  11302. ConvertProgram;
  11303. CheckSource('TestArray_ArrayLitStaticAsParam',
  11304. LinesToStr([ // statements
  11305. 'this.DoInt = function (a) {',
  11306. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  11307. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  11308. ' $mod.DoInt(a);',
  11309. '};',
  11310. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11311. 'this.a = [];',
  11312. '']),
  11313. LinesToStr([ // $mod.$main
  11314. '$mod.a = [[1, 1]];',
  11315. '$mod.a = [$mod.i.slice(0)];',
  11316. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  11317. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  11318. '$mod.DoInt([[1, 1]]);',
  11319. '$mod.DoInt([[1, 2], [3, 4]]);',
  11320. '']));
  11321. end;
  11322. procedure TTestModule.TestArray_ForInArrOfString;
  11323. begin
  11324. StartProgram(false);
  11325. Add([
  11326. 'type',
  11327. 'type',
  11328. ' TMonthNameArray = array [1..12] of string;',
  11329. ' TMonthNames = TMonthNameArray;',
  11330. ' TObject = class',
  11331. ' private',
  11332. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11333. ' public',
  11334. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11335. ' end;',
  11336. 'var',
  11337. ' f: TObject;',
  11338. ' Month: string;',
  11339. ' Names: array of string = (''a'',''foo'',''bar'');',
  11340. ' i: longint;',
  11341. 'begin',
  11342. ' for Month in f.LongMonthNames do ;',
  11343. ' for Month in Names do ;',
  11344. ' for i:=low(Names) to high(Names) do ;',
  11345. '']);
  11346. ConvertProgram;
  11347. CheckSource('TestArray_ForInArrOfString',
  11348. LinesToStr([ // statements
  11349. 'rtl.createClass(this, "TObject", null, function () {',
  11350. ' this.$init = function () {',
  11351. ' };',
  11352. ' this.$final = function () {',
  11353. ' };',
  11354. '});',
  11355. 'this.f = null;',
  11356. 'this.Month = "";',
  11357. 'this.Names = ["a", "foo", "bar"];',
  11358. 'this.i = 0;',
  11359. '']),
  11360. LinesToStr([ // $mod.$main
  11361. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11362. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11363. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11364. '']));
  11365. end;
  11366. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11367. begin
  11368. StartProgram(false);
  11369. Add([
  11370. '{$modeswitch externalclass}',
  11371. 'type',
  11372. ' TJSObject = class external name ''Object''',
  11373. ' end;',
  11374. ' TJSArray = class external name ''Array''',
  11375. ' class function isArray(Value: JSValue) : boolean;',
  11376. ' function concat() : TJSArray; varargs;',
  11377. ' end;',
  11378. 'var',
  11379. ' aObj: TJSArray;',
  11380. ' a: array of longint;',
  11381. ' o: TJSObject;',
  11382. 'begin',
  11383. ' if TJSArray.isArray(65) then ;',
  11384. ' aObj:=TJSArray(a).concat(a);',
  11385. ' o:=TJSObject(a);']);
  11386. ConvertProgram;
  11387. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11388. LinesToStr([ // statements
  11389. 'this.aObj = null;',
  11390. 'this.a = [];',
  11391. 'this.o = null;',
  11392. '']),
  11393. LinesToStr([ // $mod.$main
  11394. 'if (Array.isArray(65)) ;',
  11395. '$mod.aObj = $mod.a.concat($mod.a);',
  11396. '$mod.o = $mod.a;',
  11397. '']));
  11398. end;
  11399. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11400. begin
  11401. StartProgram(false);
  11402. Add([
  11403. '{$modeswitch externalclass}',
  11404. 'type',
  11405. ' TArrStr = array of string;',
  11406. ' TJSArray = class external name ''Array''',
  11407. ' end;',
  11408. ' TJSObject = class external name ''Object''',
  11409. ' end;',
  11410. 'var',
  11411. ' aObj: TJSArray;',
  11412. ' a: TArrStr;',
  11413. ' jo: TJSObject;',
  11414. 'begin',
  11415. ' a:=TArrStr(aObj);',
  11416. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11417. ' a:=TarrStr(jo);',
  11418. '']);
  11419. ConvertProgram;
  11420. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11421. LinesToStr([ // statements
  11422. 'this.aObj = null;',
  11423. 'this.a = [];',
  11424. 'this.jo = null;',
  11425. '']),
  11426. LinesToStr([ // $mod.$main
  11427. '$mod.a = $mod.aObj;',
  11428. '$mod.aObj[1] = $mod.aObj[2];',
  11429. '$mod.a = $mod.jo;',
  11430. '']));
  11431. end;
  11432. procedure TTestModule.TestArrayOfConst_TVarRec;
  11433. begin
  11434. StartProgram(true,[supTVarRec]);
  11435. Add([
  11436. 'procedure Say(args: array of const);',
  11437. 'var',
  11438. ' i: longint;',
  11439. ' v: TVarRec;',
  11440. 'begin',
  11441. ' for i:=low(args) to high(args) do begin',
  11442. ' v:=args[i];',
  11443. ' case v.vtype of',
  11444. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11445. ' end;',
  11446. ' end;',
  11447. ' for v in args do ;',
  11448. ' args:=nil;',
  11449. ' SetLength(args,2);',
  11450. 'end;',
  11451. 'begin']);
  11452. ConvertProgram;
  11453. CheckSource('TestArrayOfConst_TVarRec',
  11454. LinesToStr([ // statements
  11455. 'this.Say = function (args) {',
  11456. ' var i = 0;',
  11457. ' var v = pas.system.TVarRec.$new();',
  11458. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11459. ' i = $l;',
  11460. ' v.$assign(args[i]);',
  11461. ' var $tmp = v.VType;',
  11462. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11463. ' };',
  11464. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11465. ' args = [];',
  11466. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11467. '};',
  11468. '']),
  11469. LinesToStr([ // $mod.$main
  11470. ]));
  11471. end;
  11472. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11473. begin
  11474. StartProgram(true,[supTVarRec]);
  11475. Add([
  11476. 'procedure Say(args: array of const);',
  11477. 'begin',
  11478. ' Say(args);',
  11479. 'end;',
  11480. 'var',
  11481. ' p: Pointer;',
  11482. ' j: jsvalue;',
  11483. ' c: currency;',
  11484. 'begin',
  11485. ' Say([]);',
  11486. ' Say([1]);',
  11487. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11488. '']);
  11489. ConvertProgram;
  11490. CheckSource('TestArrayOfConst_PassBaseTypes',
  11491. LinesToStr([ // statements
  11492. 'this.Say = function (args) {',
  11493. ' $mod.Say(args);',
  11494. '};',
  11495. 'this.p = null;',
  11496. 'this.j = undefined;',
  11497. 'this.c = 0;',
  11498. '']),
  11499. LinesToStr([ // $mod.$main
  11500. '$mod.Say([]);',
  11501. '$mod.Say(pas.system.VarRecs(0, 1));',
  11502. '$mod.Say(pas.system.VarRecs(',
  11503. ' 9,',
  11504. ' "c",',
  11505. ' 18,',
  11506. ' "foo",',
  11507. ' 5,',
  11508. ' null,',
  11509. ' 1,',
  11510. ' true,',
  11511. ' 3,',
  11512. ' 1.3,',
  11513. ' 5,',
  11514. ' $mod.p,',
  11515. ' 20,',
  11516. ' $mod.j,',
  11517. ' 12,',
  11518. ' $mod.c',
  11519. ' ));',
  11520. '']));
  11521. end;
  11522. procedure TTestModule.TestArrayOfConst_PassObj;
  11523. begin
  11524. StartProgram(true,[supTVarRec]);
  11525. Add([
  11526. '{$interfaces corba}',
  11527. 'type',
  11528. ' TObject = class',
  11529. ' end;',
  11530. ' TClass = class of TObject;',
  11531. ' IUnknown = interface',
  11532. ' end;',
  11533. 'procedure Say(args: array of const);',
  11534. 'begin',
  11535. 'end;',
  11536. 'var',
  11537. ' o: TObject;',
  11538. ' c: TClass;',
  11539. ' i: IUnknown;',
  11540. 'begin',
  11541. ' Say([o,c,TObject]);',
  11542. ' Say([nil,i]);',
  11543. '']);
  11544. ConvertProgram;
  11545. CheckSource('TestArrayOfConst_PassObj',
  11546. LinesToStr([ // statements
  11547. 'rtl.createClass(this, "TObject", null, function () {',
  11548. ' this.$init = function () {',
  11549. ' };',
  11550. ' this.$final = function () {',
  11551. ' };',
  11552. '});',
  11553. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11554. 'this.Say = function (args) {',
  11555. '};',
  11556. 'this.o = null;',
  11557. 'this.c = null;',
  11558. 'this.i = null;',
  11559. '']),
  11560. LinesToStr([ // $mod.$main
  11561. '$mod.Say(pas.system.VarRecs(',
  11562. ' 7,',
  11563. ' $mod.o,',
  11564. ' 8,',
  11565. ' $mod.c,',
  11566. ' 8,',
  11567. ' $mod.TObject',
  11568. '));',
  11569. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11570. '']));
  11571. end;
  11572. procedure TTestModule.TestRecord_Empty;
  11573. begin
  11574. StartProgram(false);
  11575. Add([
  11576. 'type',
  11577. ' TRecA = record',
  11578. ' end;',
  11579. 'var a,b: TRecA;',
  11580. 'begin',
  11581. ' if a=b then ;']);
  11582. ConvertProgram;
  11583. CheckSource('TestRecord_Empty',
  11584. LinesToStr([ // statements
  11585. 'rtl.recNewT(this, "TRecA", function () {',
  11586. ' this.$eq = function (b) {',
  11587. ' return true;',
  11588. ' };',
  11589. ' this.$assign = function (s) {',
  11590. ' return this;',
  11591. ' };',
  11592. '});',
  11593. 'this.a = this.TRecA.$new();',
  11594. 'this.b = this.TRecA.$new();',
  11595. '']),
  11596. LinesToStr([ // $mod.$main
  11597. 'if ($mod.a.$eq($mod.b)) ;'
  11598. ]));
  11599. end;
  11600. procedure TTestModule.TestRecord_Var;
  11601. begin
  11602. StartProgram(false);
  11603. Add('type');
  11604. Add(' TRecA = record');
  11605. Add(' Bold: longint;');
  11606. Add(' end;');
  11607. Add('var Rec: TRecA;');
  11608. Add('begin');
  11609. Add(' rec.bold:=123');
  11610. ConvertProgram;
  11611. CheckSource('TestRecord_Var',
  11612. LinesToStr([ // statements
  11613. 'rtl.recNewT(this, "TRecA", function () {',
  11614. ' this.Bold = 0;',
  11615. ' this.$eq = function (b) {',
  11616. ' return this.Bold === b.Bold;',
  11617. ' };',
  11618. ' this.$assign = function (s) {',
  11619. ' this.Bold = s.Bold;',
  11620. ' return this;',
  11621. ' };',
  11622. '});',
  11623. 'this.Rec = this.TRecA.$new();',
  11624. '']),
  11625. LinesToStr([ // $mod.$main
  11626. '$mod.Rec.Bold = 123;'
  11627. ]));
  11628. end;
  11629. procedure TTestModule.TestRecord_VarExternal;
  11630. begin
  11631. StartProgram(false);
  11632. Add([
  11633. '{$modeswitch externalclass}',
  11634. 'type',
  11635. ' TRecA = record',
  11636. ' i: byte;',
  11637. ' length_: longint external name ''length'';',
  11638. ' end;',
  11639. 'var Rec: TRecA;',
  11640. 'begin',
  11641. ' rec.length_ := rec.length_',
  11642. '']);
  11643. ConvertProgram;
  11644. CheckSource('TestRecord_VarExternal',
  11645. LinesToStr([ // statements
  11646. 'rtl.recNewT(this, "TRecA", function () {',
  11647. ' this.i = 0;',
  11648. ' this.$eq = function (b) {',
  11649. ' return (this.i === b.i) && (this.length === b.length);',
  11650. ' };',
  11651. ' this.$assign = function (s) {',
  11652. ' this.i = s.i;',
  11653. ' this.length = s.length;',
  11654. ' return this;',
  11655. ' };',
  11656. '});',
  11657. 'this.Rec = this.TRecA.$new();',
  11658. '']),
  11659. LinesToStr([ // $mod.$main
  11660. '$mod.Rec.length = $mod.Rec.length;'
  11661. ]));
  11662. end;
  11663. procedure TTestModule.TestRecord_WithDo;
  11664. begin
  11665. StartProgram(false);
  11666. Add('type');
  11667. Add(' TRec = record');
  11668. Add(' vI: longint;');
  11669. Add(' end;');
  11670. Add('var');
  11671. Add(' Int: longint;');
  11672. Add(' r: TRec;');
  11673. Add('begin');
  11674. Add(' with r do');
  11675. Add(' int:=vi;');
  11676. Add(' with r do begin');
  11677. Add(' int:=vi;');
  11678. Add(' vi:=int;');
  11679. Add(' end;');
  11680. ConvertProgram;
  11681. CheckSource('TestWithRecordDo',
  11682. LinesToStr([ // statements
  11683. 'rtl.recNewT(this, "TRec", function () {',
  11684. ' this.vI = 0;',
  11685. ' this.$eq = function (b) {',
  11686. ' return this.vI === b.vI;',
  11687. ' };',
  11688. ' this.$assign = function (s) {',
  11689. ' this.vI = s.vI;',
  11690. ' return this;',
  11691. ' };',
  11692. '});',
  11693. 'this.Int = 0;',
  11694. 'this.r = this.TRec.$new();',
  11695. '']),
  11696. LinesToStr([ // $mod.$main
  11697. 'var $with = $mod.r;',
  11698. '$mod.Int = $with.vI;',
  11699. 'var $with1 = $mod.r;',
  11700. '$mod.Int = $with1.vI;',
  11701. '$with1.vI = $mod.Int;'
  11702. ]));
  11703. end;
  11704. procedure TTestModule.TestRecord_Assign;
  11705. begin
  11706. StartProgram(false);
  11707. Add([
  11708. 'type',
  11709. ' TEnum = (red,green);',
  11710. ' TEnums = set of TEnum;',
  11711. ' TSmallRec = record',
  11712. ' N: longint;',
  11713. ' end;',
  11714. ' TBigRec = record',
  11715. ' Int: longint;',
  11716. ' D: double;',
  11717. ' Arr: array of longint;',
  11718. ' Arr2: array[1..2] of longint;',
  11719. ' Small: TSmallRec;',
  11720. ' Enums: TEnums;',
  11721. ' end;',
  11722. 'var',
  11723. ' r, s: TBigRec;',
  11724. 'begin',
  11725. ' r:=s;',
  11726. ' r:=default(TBigRec);',
  11727. ' r:=default(s);',
  11728. '']);
  11729. ConvertProgram;
  11730. CheckSource('TestRecord_Assign',
  11731. LinesToStr([ // statements
  11732. 'this.TEnum = {',
  11733. ' "0": "red",',
  11734. ' red: 0,',
  11735. ' "1": "green",',
  11736. ' green: 1',
  11737. '};',
  11738. 'rtl.recNewT(this, "TSmallRec", function () {',
  11739. ' this.N = 0;',
  11740. ' this.$eq = function (b) {',
  11741. ' return this.N === b.N;',
  11742. ' };',
  11743. ' this.$assign = function (s) {',
  11744. ' this.N = s.N;',
  11745. ' return this;',
  11746. ' };',
  11747. '});',
  11748. 'rtl.recNewT(this, "TBigRec", function () {',
  11749. ' this.Int = 0;',
  11750. ' this.D = 0.0;',
  11751. ' this.$new = function () {',
  11752. ' var r = Object.create(this);',
  11753. ' r.Arr = [];',
  11754. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  11755. ' r.Small = $mod.TSmallRec.$new();',
  11756. ' r.Enums = {};',
  11757. ' return r;',
  11758. ' };',
  11759. ' this.$eq = function (b) {',
  11760. ' 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);',
  11761. ' };',
  11762. ' this.$assign = function (s) {',
  11763. ' this.Int = s.Int;',
  11764. ' this.D = s.D;',
  11765. ' this.Arr = rtl.arrayRef(s.Arr);',
  11766. ' this.Arr2 = s.Arr2.slice(0);',
  11767. ' this.Small.$assign(s.Small);',
  11768. ' this.Enums = rtl.refSet(s.Enums);',
  11769. ' return this;',
  11770. ' };',
  11771. '});',
  11772. 'this.r = this.TBigRec.$new();',
  11773. 'this.s = this.TBigRec.$new();',
  11774. '']),
  11775. LinesToStr([ // $mod.$main
  11776. '$mod.r.$assign($mod.s);',
  11777. '$mod.r.$assign($mod.TBigRec.$new());',
  11778. '$mod.r.$assign($mod.TBigRec.$new());',
  11779. '']));
  11780. end;
  11781. procedure TTestModule.TestRecord_AsParams;
  11782. begin
  11783. StartProgram(false);
  11784. Add([
  11785. 'type',
  11786. ' integer = longint;',
  11787. ' TRecord = record',
  11788. ' i: integer;',
  11789. ' end;',
  11790. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  11791. 'var vL: TRecord;',
  11792. 'begin',
  11793. ' vd:=vd;',
  11794. ' vd.i:=vd.i;',
  11795. ' vl:=vc;',
  11796. ' vv:=vv;',
  11797. ' vv.i:=vv.i;',
  11798. ' U:=vl;',
  11799. ' U:=vd;',
  11800. ' U:=vc;',
  11801. ' U:=vv;',
  11802. ' vl:=TRecord(U);',
  11803. ' vd:=TRecord(U);',
  11804. ' vv:=TRecord(U);',
  11805. ' doit(vd,vd,vd,vd);',
  11806. ' doit(vc,vc,vl,vl);',
  11807. ' doit(vv,vv,vv,vv);',
  11808. ' doit(vl,vl,vl,vl);',
  11809. ' TRecord(U).i:=3;',
  11810. 'end;',
  11811. 'var i: TRecord;',
  11812. 'begin',
  11813. ' doit(i,i,i,i);',
  11814. '']);
  11815. ConvertProgram;
  11816. CheckSource('TestRecord_AsParams',
  11817. LinesToStr([ // statements
  11818. 'rtl.recNewT(this, "TRecord", function () {',
  11819. ' this.i = 0;',
  11820. ' this.$eq = function (b) {',
  11821. ' return this.i === b.i;',
  11822. ' };',
  11823. ' this.$assign = function (s) {',
  11824. ' this.i = s.i;',
  11825. ' return this;',
  11826. ' };',
  11827. '});',
  11828. 'this.DoIt = function (vD, vC, vV, U) {',
  11829. ' var vL = $mod.TRecord.$new();',
  11830. ' vD.$assign(vD);',
  11831. ' vD.i = vD.i;',
  11832. ' vL.$assign(vC);',
  11833. ' vV.$assign(vV);',
  11834. ' vV.i = vV.i;',
  11835. ' U.$assign(vL);',
  11836. ' U.$assign(vD);',
  11837. ' U.$assign(vC);',
  11838. ' U.$assign(vV);',
  11839. ' vL.$assign(U);',
  11840. ' vD.$assign(U);',
  11841. ' vV.$assign(U);',
  11842. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  11843. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  11844. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  11845. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  11846. ' U.i = 3;',
  11847. '};',
  11848. 'this.i = this.TRecord.$new();'
  11849. ]),
  11850. LinesToStr([
  11851. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  11852. '']));
  11853. end;
  11854. procedure TTestModule.TestRecord_ConstRef;
  11855. begin
  11856. StartProgram(false);
  11857. Add([
  11858. 'type TRec = record i: word; end;',
  11859. 'procedure Run(constref a: TRec);',
  11860. 'begin',
  11861. 'end;',
  11862. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  11863. 'var l: TRec;',
  11864. 'begin',
  11865. ' Run(l);',
  11866. ' Run(a);',
  11867. ' Run(b);',
  11868. ' Run(c);',
  11869. ' Run(d);',
  11870. ' Run(e);',
  11871. 'end;',
  11872. 'begin',
  11873. '']);
  11874. ConvertProgram;
  11875. CheckResolverUnexpectedHints();
  11876. CheckSource('TestRecord_ConstRef',
  11877. LinesToStr([ // statements
  11878. 'rtl.recNewT(this, "TRec", function () {',
  11879. ' this.i = 0;',
  11880. ' this.$eq = function (b) {',
  11881. ' return this.i === b.i;',
  11882. ' };',
  11883. ' this.$assign = function (s) {',
  11884. ' this.i = s.i;',
  11885. ' return this;',
  11886. ' };',
  11887. '});',
  11888. 'this.Run = function (a) {',
  11889. '};',
  11890. 'this.Fly = function (a, b, c, d, e) {',
  11891. ' var l = $mod.TRec.$new();',
  11892. ' $mod.Run(l);',
  11893. ' $mod.Run(a);',
  11894. ' $mod.Run(b);',
  11895. ' $mod.Run(c);',
  11896. ' $mod.Run(d);',
  11897. ' $mod.Run(e);',
  11898. '};',
  11899. '']),
  11900. LinesToStr([
  11901. '']));
  11902. end;
  11903. procedure TTestModule.TestRecordElement_AsParams;
  11904. begin
  11905. StartProgram(false);
  11906. Add('type');
  11907. Add(' integer = longint;');
  11908. Add(' TRecord = record');
  11909. Add(' i: integer;');
  11910. Add(' end;');
  11911. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11912. Add('var vJ: TRecord;');
  11913. Add('begin');
  11914. Add(' doit(vj.i,vj.i,vj.i);');
  11915. Add('end;');
  11916. Add('var r: TRecord;');
  11917. Add('begin');
  11918. Add(' doit(r.i,r.i,r.i);');
  11919. ConvertProgram;
  11920. CheckSource('TestRecordElement_AsParams',
  11921. LinesToStr([ // statements
  11922. 'rtl.recNewT(this, "TRecord", function () {',
  11923. ' this.i = 0;',
  11924. ' this.$eq = function (b) {',
  11925. ' return this.i === b.i;',
  11926. ' };',
  11927. ' this.$assign = function (s) {',
  11928. ' this.i = s.i;',
  11929. ' return this;',
  11930. ' };',
  11931. '});',
  11932. 'this.DoIt = function (vG,vH,vI) {',
  11933. ' var vJ = $mod.TRecord.$new();',
  11934. ' $mod.DoIt(vJ.i, vJ.i, {',
  11935. ' p: vJ,',
  11936. ' get: function () {',
  11937. ' return this.p.i;',
  11938. ' },',
  11939. ' set: function (v) {',
  11940. ' this.p.i = v;',
  11941. ' }',
  11942. ' });',
  11943. '};',
  11944. 'this.r = this.TRecord.$new();'
  11945. ]),
  11946. LinesToStr([
  11947. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11948. ' p: $mod.r,',
  11949. ' get: function () {',
  11950. ' return this.p.i;',
  11951. ' },',
  11952. ' set: function (v) {',
  11953. ' this.p.i = v;',
  11954. ' }',
  11955. '});'
  11956. ]));
  11957. end;
  11958. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11959. begin
  11960. StartProgram(false);
  11961. Add('type');
  11962. Add(' integer = longint;');
  11963. Add(' TRecord = record');
  11964. Add(' i: integer;');
  11965. Add(' end;');
  11966. Add('function GetRec(vB: integer = 0): TRecord;');
  11967. Add('begin');
  11968. Add('end;');
  11969. Add('procedure DoIt(vG: integer; const vH: integer);');
  11970. Add('begin');
  11971. Add('end;');
  11972. Add('begin');
  11973. Add(' doit(getrec.i,getrec.i);');
  11974. Add(' doit(getrec().i,getrec().i);');
  11975. Add(' doit(getrec(1).i,getrec(2).i);');
  11976. ConvertProgram;
  11977. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11978. LinesToStr([ // statements
  11979. 'rtl.recNewT(this, "TRecord", function () {',
  11980. ' this.i = 0;',
  11981. ' this.$eq = function (b) {',
  11982. ' return this.i === b.i;',
  11983. ' };',
  11984. ' this.$assign = function (s) {',
  11985. ' this.i = s.i;',
  11986. ' return this;',
  11987. ' };',
  11988. '});',
  11989. 'this.GetRec = function (vB) {',
  11990. ' var Result = $mod.TRecord.$new();',
  11991. ' return Result;',
  11992. '};',
  11993. 'this.DoIt = function (vG, vH) {',
  11994. '};',
  11995. '']),
  11996. LinesToStr([
  11997. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11998. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11999. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12000. '']));
  12001. end;
  12002. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12003. begin
  12004. StartProgram(false);
  12005. Add('type');
  12006. Add(' integer = longint;');
  12007. Add(' TRecord = record');
  12008. Add(' i: integer;');
  12009. Add(' end;');
  12010. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12011. Add('begin');
  12012. Add('end;');
  12013. Add('var r: trecord;');
  12014. Add('begin');
  12015. Add(' with r do ');
  12016. Add(' doit(i,i,i);');
  12017. ConvertProgram;
  12018. CheckSource('TestRecordElementFromWith_AsParams',
  12019. LinesToStr([ // statements
  12020. 'rtl.recNewT(this, "TRecord", function () {',
  12021. ' this.i = 0;',
  12022. ' this.$eq = function (b) {',
  12023. ' return this.i === b.i;',
  12024. ' };',
  12025. ' this.$assign = function (s) {',
  12026. ' this.i = s.i;',
  12027. ' return this;',
  12028. ' };',
  12029. '});',
  12030. 'this.DoIt = function (vG,vH,vI) {',
  12031. '};',
  12032. 'this.r = this.TRecord.$new();'
  12033. ]),
  12034. LinesToStr([
  12035. 'var $with = $mod.r;',
  12036. '$mod.DoIt($with.i,$with.i,{',
  12037. ' p: $with,',
  12038. ' get: function () {',
  12039. ' return this.p.i;',
  12040. ' },',
  12041. ' set: function (v) {',
  12042. ' this.p.i = v;',
  12043. ' }',
  12044. '});',
  12045. '']));
  12046. end;
  12047. procedure TTestModule.TestRecord_Equal;
  12048. begin
  12049. StartProgram(false);
  12050. Add('type');
  12051. Add(' integer = longint;');
  12052. Add(' TFlag = (red,blue);');
  12053. Add(' TFlags = set of TFlag;');
  12054. Add(' TProc = procedure;');
  12055. Add(' TRecord = record');
  12056. Add(' i: integer;');
  12057. Add(' Event: TProc;');
  12058. Add(' f: TFlags;');
  12059. Add(' end;');
  12060. Add(' TNested = record');
  12061. Add(' r: TRecord;');
  12062. Add(' end;');
  12063. Add('var');
  12064. Add(' b: boolean;');
  12065. Add(' r,s: trecord;');
  12066. Add('begin');
  12067. Add(' b:=r=s;');
  12068. Add(' b:=r<>s;');
  12069. ConvertProgram;
  12070. CheckSource('TestRecord_Equal',
  12071. LinesToStr([ // statements
  12072. 'this.TFlag = {',
  12073. ' "0": "red",',
  12074. ' red: 0,',
  12075. ' "1": "blue",',
  12076. ' blue: 1',
  12077. '};',
  12078. 'rtl.recNewT(this, "TRecord", function () {',
  12079. ' this.i = 0;',
  12080. ' this.Event = null;',
  12081. ' this.$new = function () {',
  12082. ' var r = Object.create(this);',
  12083. ' r.f = {};',
  12084. ' return r;',
  12085. ' };',
  12086. ' this.$eq = function (b) {',
  12087. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12088. ' };',
  12089. ' this.$assign = function (s) {',
  12090. ' this.i = s.i;',
  12091. ' this.Event = s.Event;',
  12092. ' this.f = rtl.refSet(s.f);',
  12093. ' return this;',
  12094. ' };',
  12095. '});',
  12096. 'rtl.recNewT(this, "TNested", function () {',
  12097. ' this.$new = function () {',
  12098. ' var r = Object.create(this);',
  12099. ' r.r = $mod.TRecord.$new();',
  12100. ' return r;',
  12101. ' };',
  12102. ' this.$eq = function (b) {',
  12103. ' return this.r.$eq(b.r);',
  12104. ' };',
  12105. ' this.$assign = function (s) {',
  12106. ' this.r.$assign(s.r);',
  12107. ' return this;',
  12108. ' };',
  12109. '});',
  12110. 'this.b = false;',
  12111. 'this.r = this.TRecord.$new();',
  12112. 'this.s = this.TRecord.$new();',
  12113. '']),
  12114. LinesToStr([
  12115. '$mod.b = $mod.r.$eq($mod.s);',
  12116. '$mod.b = !$mod.r.$eq($mod.s);',
  12117. '']));
  12118. end;
  12119. procedure TTestModule.TestRecord_JSValue;
  12120. begin
  12121. StartProgram(false);
  12122. Add([
  12123. 'type',
  12124. ' TRecord = record',
  12125. ' i: longint;',
  12126. ' end;',
  12127. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12128. 'begin',
  12129. 'end;',
  12130. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12131. 'begin',
  12132. ' if jsvalue(d) then ;',
  12133. ' if jsvalue(c) then ;',
  12134. ' if jsvalue(v) then ;',
  12135. 'end;',
  12136. 'var',
  12137. ' Jv: jsvalue;',
  12138. ' Rec: trecord;',
  12139. 'begin',
  12140. ' rec:=trecord(jv);',
  12141. ' jv:=rec;',
  12142. ' Fly(rec,rec);',
  12143. ' Fly(@rec,@rec);',
  12144. ' if jsvalue(Rec) then ;',
  12145. ' Run(trecord(jv),trecord(jv),rec);',
  12146. '']);
  12147. ConvertProgram;
  12148. CheckSource('TestRecord_JSValue',
  12149. LinesToStr([ // statements
  12150. 'rtl.recNewT(this, "TRecord", function () {',
  12151. ' this.i = 0;',
  12152. ' this.$eq = function (b) {',
  12153. ' return this.i === b.i;',
  12154. ' };',
  12155. ' this.$assign = function (s) {',
  12156. ' this.i = s.i;',
  12157. ' return this;',
  12158. ' };',
  12159. '});',
  12160. 'this.Fly = function (d, c) {',
  12161. '};',
  12162. 'this.Run = function (d, c, v) {',
  12163. ' if (d) ;',
  12164. ' if (c) ;',
  12165. ' if (v) ;',
  12166. '};',
  12167. 'this.Jv = undefined;',
  12168. 'this.Rec = this.TRecord.$new();',
  12169. '']),
  12170. LinesToStr([
  12171. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12172. '$mod.Jv = $mod.Rec;',
  12173. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12174. '$mod.Fly($mod.Rec, $mod.Rec);',
  12175. 'if ($mod.Rec) ;',
  12176. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12177. '']));
  12178. end;
  12179. procedure TTestModule.TestRecord_VariantFail;
  12180. begin
  12181. StartProgram(false);
  12182. Add([
  12183. 'type',
  12184. ' TRec = record',
  12185. ' case word of',
  12186. ' 0: (b0, b1: Byte);',
  12187. ' 1: (i: word);',
  12188. ' end;',
  12189. 'begin']);
  12190. SetExpectedPasResolverError('Not supported: variant record',
  12191. nNotSupportedX);
  12192. ConvertProgram;
  12193. end;
  12194. procedure TTestModule.TestRecord_FieldArray;
  12195. begin
  12196. StartProgram(false);
  12197. Add([
  12198. 'type',
  12199. ' TArrInt = array[3..4] of longint;',
  12200. ' TArrArrInt = array[3..4] of longint;',
  12201. ' TRec = record',
  12202. ' a: array of longint;',
  12203. ' s: array[1..2] of longint;',
  12204. ' m: array[1..2,3..4] of longint;',
  12205. ' o: TArrArrInt;',
  12206. ' end;',
  12207. 'begin']);
  12208. ConvertProgram;
  12209. CheckSource('TestRecord_FieldArray',
  12210. LinesToStr([ // statements
  12211. 'rtl.recNewT(this, "TRec", function () {',
  12212. ' this.m$a$clone = function (a) {',
  12213. ' var b = [];',
  12214. ' b.length = 2;',
  12215. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12216. ' return b;',
  12217. ' };',
  12218. ' this.$new = function () {',
  12219. ' var r = Object.create(this);',
  12220. ' r.a = [];',
  12221. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12222. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12223. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12224. ' return r;',
  12225. ' };',
  12226. ' this.$eq = function (b) {',
  12227. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12228. ' };',
  12229. ' this.$assign = function (s) {',
  12230. ' this.a = rtl.arrayRef(s.a);',
  12231. ' this.s = s.s.slice(0);',
  12232. ' this.m = this.m$a$clone(s.m);',
  12233. ' this.o = s.o.slice(0);',
  12234. ' return this;',
  12235. ' };',
  12236. '});',
  12237. '']),
  12238. LinesToStr([ // $mod.$main
  12239. '']));
  12240. end;
  12241. procedure TTestModule.TestRecord_Const;
  12242. begin
  12243. StartProgram(false);
  12244. Add([
  12245. 'type',
  12246. ' TArrInt = array[3..4] of longint;',
  12247. ' TPoint = record x,y: longint; end;',
  12248. ' TRec = record',
  12249. ' i: longint;',
  12250. ' a: array of longint;',
  12251. ' s: array[1..2] of longint;',
  12252. ' m: array[1..2,3..4] of longint;',
  12253. ' p: TPoint;',
  12254. ' end;',
  12255. ' TPoints = array of TPoint;',
  12256. 'const',
  12257. ' r: TRec = (',
  12258. ' i:1;',
  12259. ' a:(2,3);',
  12260. ' s:(4,5);',
  12261. ' m:( (11,12), (13,14) );',
  12262. ' p: (x:21; y:22)',
  12263. ' );',
  12264. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12265. 'begin']);
  12266. ConvertProgram;
  12267. CheckSource('TestRecord_Const',
  12268. LinesToStr([ // statements
  12269. 'rtl.recNewT(this, "TPoint", function () {',
  12270. ' this.x = 0;',
  12271. ' this.y = 0;',
  12272. ' this.$eq = function (b) {',
  12273. ' return (this.x === b.x) && (this.y === b.y);',
  12274. ' };',
  12275. ' this.$assign = function (s) {',
  12276. ' this.x = s.x;',
  12277. ' this.y = s.y;',
  12278. ' return this;',
  12279. ' };',
  12280. '});',
  12281. 'rtl.recNewT(this, "TRec", function () {',
  12282. ' this.i = 0;',
  12283. ' this.m$a$clone = function (a) {',
  12284. ' var b = [];',
  12285. ' b.length = 2;',
  12286. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12287. ' return b;',
  12288. ' };',
  12289. ' this.$new = function () {',
  12290. ' var r = Object.create(this);',
  12291. ' r.a = [];',
  12292. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12293. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12294. ' r.p = $mod.TPoint.$new();',
  12295. ' return r;',
  12296. ' };',
  12297. ' this.$eq = function (b) {',
  12298. ' 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);',
  12299. ' };',
  12300. ' this.$assign = function (s) {',
  12301. ' this.i = s.i;',
  12302. ' this.a = rtl.arrayRef(s.a);',
  12303. ' this.s = s.s.slice(0);',
  12304. ' this.m = this.m$a$clone(s.m);',
  12305. ' this.p.$assign(s.p);',
  12306. ' return this;',
  12307. ' };',
  12308. '});',
  12309. 'this.r = this.TRec.$clone({',
  12310. ' i: 1,',
  12311. ' a: [2, 3],',
  12312. ' s: [4, 5],',
  12313. ' m: [[11, 12], [13, 14]],',
  12314. ' p: this.TPoint.$clone({',
  12315. ' x: 21,',
  12316. ' y: 22',
  12317. ' })',
  12318. '});',
  12319. 'this.p = [this.TPoint.$clone({',
  12320. ' x: 1,',
  12321. ' y: 2',
  12322. '}), this.TPoint.$clone({',
  12323. ' x: 3,',
  12324. ' y: 4',
  12325. '})];',
  12326. '']),
  12327. LinesToStr([ // $mod.$main
  12328. '']));
  12329. end;
  12330. procedure TTestModule.TestRecord_TypecastFail;
  12331. begin
  12332. StartProgram(false);
  12333. Add([
  12334. 'type',
  12335. ' TPoint = record x,y: longint; end;',
  12336. ' TRec = record l: longint end;',
  12337. 'var p: TPoint;',
  12338. 'begin',
  12339. ' if TRec(p).l=2 then ;']);
  12340. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12341. nIllegalTypeConversionTo);
  12342. ConvertProgram;
  12343. end;
  12344. procedure TTestModule.TestRecord_InFunction;
  12345. begin
  12346. StartProgram(false);
  12347. Add([
  12348. 'var TPoint: longint = 3;',
  12349. 'procedure DoIt;',
  12350. 'type',
  12351. ' TPoint = record x,y: longint; end;',
  12352. ' TPoints = array of TPoint;',
  12353. 'var',
  12354. ' r: TPoint;',
  12355. ' p: TPoints;',
  12356. 'begin',
  12357. ' SetLength(p,2);',
  12358. 'end;',
  12359. 'begin']);
  12360. ConvertProgram;
  12361. CheckSource('TestRecord_InFunction',
  12362. LinesToStr([ // statements
  12363. 'this.TPoint = 3;',
  12364. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12365. ' this.x = 0;',
  12366. ' this.y = 0;',
  12367. ' this.$eq = function (b) {',
  12368. ' return (this.x === b.x) && (this.y === b.y);',
  12369. ' };',
  12370. ' this.$assign = function (s) {',
  12371. ' this.x = s.x;',
  12372. ' this.y = s.y;',
  12373. ' return this;',
  12374. ' };',
  12375. '});',
  12376. 'this.DoIt = function () {',
  12377. ' var r = TPoint$1.$new();',
  12378. ' var p = [];',
  12379. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12380. '};',
  12381. '']),
  12382. LinesToStr([ // $mod.$main
  12383. '']));
  12384. end;
  12385. procedure TTestModule.TestRecordAnonym_Field;
  12386. begin
  12387. StartProgram(false);
  12388. Add(['',
  12389. 'var Rec: record',
  12390. ' Bold: longint;',
  12391. ' end;',
  12392. 'begin',
  12393. ' rec.bold:=123;',
  12394. ' rec.bold:=rec.bold+7;',
  12395. '']);
  12396. ConvertProgram;
  12397. CheckSource('TestRecordAnonym_Field',
  12398. LinesToStr([ // statements
  12399. 'rtl.recNewT(this, "Rec$a", function () {',
  12400. ' this.Bold = 0;',
  12401. ' this.$eq = function (b) {',
  12402. ' return this.Bold === b.Bold;',
  12403. ' };',
  12404. ' this.$assign = function (s) {',
  12405. ' this.Bold = s.Bold;',
  12406. ' return this;',
  12407. ' };',
  12408. '});',
  12409. 'this.Rec = this.Rec$a.$new();',
  12410. '']),
  12411. LinesToStr([ // $mod.$main
  12412. '$mod.Rec.Bold = 123;',
  12413. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12414. '']));
  12415. end;
  12416. procedure TTestModule.TestRecordAnonym_Assign;
  12417. begin
  12418. StartProgram(false);
  12419. Add(['',
  12420. 'var S,T: record',
  12421. ' Bold: longint;',
  12422. ' end;',
  12423. ' b: boolean;',
  12424. 'begin',
  12425. ' S:=T;',
  12426. ' b:=s=t;',
  12427. '']);
  12428. ConvertProgram;
  12429. CheckSource('TestRecordAnonym_Assign',
  12430. LinesToStr([ // statements
  12431. 'rtl.recNewT(this, "T$a", function () {',
  12432. ' this.Bold = 0;',
  12433. ' this.$eq = function (b) {',
  12434. ' return this.Bold === b.Bold;',
  12435. ' };',
  12436. ' this.$assign = function (s) {',
  12437. ' this.Bold = s.Bold;',
  12438. ' return this;',
  12439. ' };',
  12440. '});',
  12441. 'this.S = this.T$a.$new();',
  12442. 'this.T = this.T$a.$new();',
  12443. 'this.b = false;',
  12444. '']),
  12445. LinesToStr([ // $mod.$main
  12446. '$mod.S.$assign($mod.T);',
  12447. '$mod.b = $mod.S.$eq($mod.T);',
  12448. '']));
  12449. end;
  12450. procedure TTestModule.TestRecordAnonym_Nested;
  12451. begin
  12452. StartProgram(false);
  12453. Add(['',
  12454. 'var S,T: record',
  12455. ' Bold: longint;',
  12456. ' Sub: record',
  12457. ' Color: word;',
  12458. ' end;',
  12459. ' end;',
  12460. ' b: boolean;',
  12461. 'begin',
  12462. ' S:=T;',
  12463. ' S.Sub:=T.Sub;',
  12464. ' S.Sub.Color:=T.Sub.Color+3;',
  12465. ' b:=s=t;',
  12466. ' b:=s.Sub=t.Sub;',
  12467. '']);
  12468. ConvertProgram;
  12469. CheckSource('TestRecordAnonym_Nested',
  12470. LinesToStr([ // statements
  12471. 'rtl.recNewT(this, "T$a", function () {',
  12472. ' this.Bold = 0;',
  12473. ' rtl.recNewT(this, "Sub$a", function () {',
  12474. ' this.Color = 0;',
  12475. ' this.$eq = function (b) {',
  12476. ' return this.Color === b.Color;',
  12477. ' };',
  12478. ' this.$assign = function (s) {',
  12479. ' this.Color = s.Color;',
  12480. ' return this;',
  12481. ' };',
  12482. ' });',
  12483. ' this.$new = function () {',
  12484. ' var r = Object.create(this);',
  12485. ' r.Sub = this.Sub$a.$new();',
  12486. ' return r;',
  12487. ' };',
  12488. ' this.$eq = function (b) {',
  12489. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12490. ' };',
  12491. ' this.$assign = function (s) {',
  12492. ' this.Bold = s.Bold;',
  12493. ' this.Sub.$assign(s.Sub);',
  12494. ' return this;',
  12495. ' };',
  12496. '}, true);',
  12497. 'this.S = this.T$a.$new();',
  12498. 'this.T = this.T$a.$new();',
  12499. 'this.b = false;',
  12500. '']),
  12501. LinesToStr([ // $mod.$main
  12502. '$mod.S.$assign($mod.T);',
  12503. '$mod.S.Sub.$assign($mod.T.Sub);',
  12504. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12505. '$mod.b = $mod.S.$eq($mod.T);',
  12506. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12507. '']));
  12508. end;
  12509. procedure TTestModule.TestRecordAnonym_Const;
  12510. begin
  12511. StartProgram(false);
  12512. Add(['',
  12513. 'var T: record',
  12514. ' Bold: longint;',
  12515. ' Sub: record',
  12516. ' Color: word;',
  12517. ' end;',
  12518. ' end = (Bold: 2; Sub: (Color: 3));',
  12519. 'begin',
  12520. '']);
  12521. ConvertProgram;
  12522. CheckSource('TestRecordAnonym_Const',
  12523. LinesToStr([ // statements
  12524. 'rtl.recNewT(this, "T$a", function () {',
  12525. ' this.Bold = 0;',
  12526. ' rtl.recNewT(this, "Sub$a", function () {',
  12527. ' this.Color = 0;',
  12528. ' this.$eq = function (b) {',
  12529. ' return this.Color === b.Color;',
  12530. ' };',
  12531. ' this.$assign = function (s) {',
  12532. ' this.Color = s.Color;',
  12533. ' return this;',
  12534. ' };',
  12535. ' });',
  12536. ' this.$new = function () {',
  12537. ' var r = Object.create(this);',
  12538. ' r.Sub = this.Sub$a.$new();',
  12539. ' return r;',
  12540. ' };',
  12541. ' this.$eq = function (b) {',
  12542. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12543. ' };',
  12544. ' this.$assign = function (s) {',
  12545. ' this.Bold = s.Bold;',
  12546. ' this.Sub.$assign(s.Sub);',
  12547. ' return this;',
  12548. ' };',
  12549. '}, true);',
  12550. 'this.T = this.T$a.$clone({',
  12551. ' Bold: 2,',
  12552. ' Sub: this.T$a.Sub$a.$clone({',
  12553. ' Color: 3',
  12554. ' })',
  12555. '});',
  12556. '']),
  12557. LinesToStr([ // $mod.$main
  12558. '']));
  12559. end;
  12560. procedure TTestModule.TestRecordAnonym_InFunction;
  12561. begin
  12562. StartProgram(false);
  12563. Add(['',
  12564. 'procedure Fly;',
  12565. 'var T: record',
  12566. ' Bold: longint;',
  12567. ' Sub: record',
  12568. ' Color: word;',
  12569. ' end;',
  12570. ' end = (Bold: 2; Sub: (Color: 3));',
  12571. 'begin',
  12572. 'end;',
  12573. 'begin',
  12574. '']);
  12575. ConvertProgram;
  12576. CheckSource('TestRecordAnonym_InFunction',
  12577. LinesToStr([ // statements
  12578. 'var T$a = rtl.recNewT(null, "", function () {',
  12579. ' this.Bold = 0;',
  12580. ' rtl.recNewT(this, "Sub$a", function () {',
  12581. ' this.Color = 0;',
  12582. ' this.$eq = function (b) {',
  12583. ' return this.Color === b.Color;',
  12584. ' };',
  12585. ' this.$assign = function (s) {',
  12586. ' this.Color = s.Color;',
  12587. ' return this;',
  12588. ' };',
  12589. ' });',
  12590. ' this.$new = function () {',
  12591. ' var r = Object.create(this);',
  12592. ' r.Sub = this.Sub$a.$new();',
  12593. ' return r;',
  12594. ' };',
  12595. ' this.$eq = function (b) {',
  12596. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12597. ' };',
  12598. ' this.$assign = function (s) {',
  12599. ' this.Bold = s.Bold;',
  12600. ' this.Sub.$assign(s.Sub);',
  12601. ' return this;',
  12602. ' };',
  12603. '}, true);',
  12604. 'this.Fly = function () {',
  12605. ' var T = T$a.$clone({',
  12606. ' Bold: 2,',
  12607. ' Sub: T$a.Sub$a.$clone({',
  12608. ' Color: 3',
  12609. ' })',
  12610. ' });',
  12611. '};',
  12612. '']),
  12613. LinesToStr([ // $mod.$main
  12614. '']));
  12615. end;
  12616. procedure TTestModule.TestAdvRecord_Function;
  12617. begin
  12618. StartProgram(false);
  12619. Parser.Options:=Parser.Options+[po_cassignments];
  12620. Add([
  12621. '{$modeswitch AdvancedRecords}',
  12622. 'type',
  12623. ' TPoint = record',
  12624. ' x,y: word;',
  12625. ' function Add(const apt: TPoint): TPoint;',
  12626. ' end;',
  12627. 'function TPoint.Add(const apt: TPoint): TPoint;',
  12628. 'begin',
  12629. ' Result:=Self;',
  12630. ' Result.x+=apt.x;',
  12631. ' Result.y:=Result.y+apt.y;',
  12632. ' Self:=apt;',
  12633. 'end;',
  12634. 'var p,q: TPoint;',
  12635. 'begin',
  12636. ' p.add(q);',
  12637. ' p:=default(TPoint);',
  12638. ' p:=q;',
  12639. '']);
  12640. ConvertProgram;
  12641. CheckSource('TestAdvRecord_Function',
  12642. LinesToStr([ // statements
  12643. 'rtl.recNewT(this, "TPoint", function () {',
  12644. ' this.x = 0;',
  12645. ' this.y = 0;',
  12646. ' this.$eq = function (b) {',
  12647. ' return (this.x === b.x) && (this.y === b.y);',
  12648. ' };',
  12649. ' this.$assign = function (s) {',
  12650. ' this.x = s.x;',
  12651. ' this.y = s.y;',
  12652. ' return this;',
  12653. ' };',
  12654. ' this.Add = function (apt) {',
  12655. ' var Result = $mod.TPoint.$new();',
  12656. ' Result.$assign(this);',
  12657. ' Result.x += apt.x;',
  12658. ' Result.y = Result.y + apt.y;',
  12659. ' this.$assign(apt);',
  12660. ' return Result;',
  12661. ' };',
  12662. '});',
  12663. 'this.p = this.TPoint.$new();',
  12664. 'this.q = this.TPoint.$new();',
  12665. '']),
  12666. LinesToStr([ // $mod.$main
  12667. '$mod.p.Add($mod.q);',
  12668. '$mod.p.$assign($mod.TPoint.$new());',
  12669. '$mod.p.$assign($mod.q);',
  12670. '']));
  12671. end;
  12672. procedure TTestModule.TestAdvRecord_Property;
  12673. begin
  12674. StartProgram(false);
  12675. Add([
  12676. '{$modeswitch AdvancedRecords}',
  12677. 'type',
  12678. ' TPoint = record',
  12679. ' x,y: word;',
  12680. ' strict private',
  12681. ' function GetSize: longword;',
  12682. ' procedure SetSize(Value: longword);',
  12683. ' public',
  12684. ' property Size: longword read GetSize write SetSize;',
  12685. ' property Left: word read x write y;',
  12686. ' end;',
  12687. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  12688. 'function TPoint.GetSize: longword;',
  12689. 'begin',
  12690. ' x:=y;',
  12691. ' Size:=Size;',
  12692. ' Left:=Left;',
  12693. 'end;',
  12694. 'procedure TPoint.SetSize(Value: longword);',
  12695. 'begin',
  12696. 'end;',
  12697. 'var p,q: TPoint;',
  12698. 'begin',
  12699. ' p.Size:=q.Size;',
  12700. ' p.Left:=q.Left;',
  12701. '']);
  12702. ConvertProgram;
  12703. CheckSource('TestAdvRecord_Property',
  12704. LinesToStr([ // statements
  12705. 'rtl.recNewT(this, "TPoint", function () {',
  12706. ' this.x = 0;',
  12707. ' this.y = 0;',
  12708. ' this.$eq = function (b) {',
  12709. ' return (this.x === b.x) && (this.y === b.y);',
  12710. ' };',
  12711. ' this.$assign = function (s) {',
  12712. ' this.x = s.x;',
  12713. ' this.y = s.y;',
  12714. ' return this;',
  12715. ' };',
  12716. ' this.GetSize = function () {',
  12717. ' var Result = 0;',
  12718. ' this.x = this.y;',
  12719. ' this.SetSize(this.GetSize());',
  12720. ' this.y = this.x;',
  12721. ' return Result;',
  12722. ' };',
  12723. ' this.SetSize = function (Value) {',
  12724. ' };',
  12725. '});',
  12726. 'this.SetSize = function (Value) {',
  12727. '};',
  12728. 'this.p = this.TPoint.$new();',
  12729. 'this.q = this.TPoint.$new();',
  12730. '']),
  12731. LinesToStr([ // $mod.$main
  12732. '$mod.p.SetSize($mod.q.GetSize());',
  12733. '$mod.p.y = $mod.q.x;',
  12734. '']));
  12735. end;
  12736. procedure TTestModule.TestAdvRecord_PropertyDefault;
  12737. begin
  12738. StartProgram(false);
  12739. Add([
  12740. '{$modeswitch AdvancedRecords}',
  12741. 'type',
  12742. ' TPoint = record',
  12743. ' strict private',
  12744. ' function GetItems(Index: word): word;',
  12745. ' procedure SetItems(Index: word; Value: word);',
  12746. ' public',
  12747. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  12748. ' end;',
  12749. 'function TPoint.GetItems(Index: word): word;',
  12750. 'begin',
  12751. ' Items[index]:=Items[index];',
  12752. ' self.Items[index]:=self.Items[index];',
  12753. 'end;',
  12754. 'procedure TPoint.SetItems(Index: word; Value: word);',
  12755. 'begin',
  12756. 'end;',
  12757. 'var p: TPoint;',
  12758. 'begin',
  12759. ' p[1]:=p[2];',
  12760. ' p.Items[3]:=p.Items[4];',
  12761. '']);
  12762. ConvertProgram;
  12763. CheckSource('TestAdvRecord_PropertyDefault',
  12764. LinesToStr([ // statements
  12765. 'rtl.recNewT(this, "TPoint", function () {',
  12766. ' this.$eq = function (b) {',
  12767. ' return true;',
  12768. ' };',
  12769. ' this.$assign = function (s) {',
  12770. ' return this;',
  12771. ' };',
  12772. ' this.GetItems = function (Index) {',
  12773. ' var Result = 0;',
  12774. ' this.SetItems(Index, this.GetItems(Index));',
  12775. ' this.SetItems(Index, this.GetItems(Index));',
  12776. ' return Result;',
  12777. ' };',
  12778. ' this.SetItems = function (Index, Value) {',
  12779. ' };',
  12780. '});',
  12781. 'this.p = this.TPoint.$new();',
  12782. '']),
  12783. LinesToStr([ // $mod.$main
  12784. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  12785. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  12786. '']));
  12787. end;
  12788. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  12789. begin
  12790. StartProgram(false);
  12791. Add([
  12792. '{$modeswitch AdvancedRecords}',
  12793. 'type',
  12794. ' TRec = record',
  12795. ' class var',
  12796. ' Fx: longint;',
  12797. ' Fy: longint;',
  12798. ' class function GetInt: longint; static;',
  12799. ' class procedure SetInt(Value: longint); static;',
  12800. ' class procedure DoIt; static;',
  12801. ' class property IntA: longint read Fx write Fy;',
  12802. ' class property IntB: longint read GetInt write SetInt;',
  12803. ' end;',
  12804. 'class function trec.getint: longint;',
  12805. 'begin',
  12806. ' result:=fx;',
  12807. 'end;',
  12808. 'class procedure trec.setint(value: longint);',
  12809. 'begin',
  12810. 'end;',
  12811. 'class procedure trec.doit;',
  12812. 'begin',
  12813. ' IntA:=IntA+1;',
  12814. ' IntB:=IntB+1;',
  12815. 'end;',
  12816. 'var r: trec;',
  12817. 'begin',
  12818. ' trec.inta:=trec.inta+1;',
  12819. ' if trec.intb=2 then;',
  12820. ' trec.intb:=trec.intb+2;',
  12821. ' trec.setint(trec.inta);',
  12822. ' r.inta:=r.inta+1;',
  12823. ' if r.intb=2 then;',
  12824. ' r.intb:=r.intb+2;',
  12825. ' r.setint(r.inta);']);
  12826. ConvertProgram;
  12827. CheckSource('TestAdvRecord_Property_ClassMethod',
  12828. LinesToStr([ // statements
  12829. 'rtl.recNewT(this, "TRec", function () {',
  12830. ' this.Fx = 0;',
  12831. ' this.Fy = 0;',
  12832. ' this.$eq = function (b) {',
  12833. ' return true;',
  12834. ' };',
  12835. ' this.$assign = function (s) {',
  12836. ' return this;',
  12837. ' };',
  12838. ' this.GetInt = function () {',
  12839. ' var Result = 0;',
  12840. ' Result = $mod.TRec.Fx;',
  12841. ' return Result;',
  12842. ' };',
  12843. ' this.SetInt = function (Value) {',
  12844. ' };',
  12845. ' this.DoIt = function () {',
  12846. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  12847. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  12848. ' };',
  12849. '}, true);',
  12850. 'this.r = this.TRec.$new();',
  12851. '']),
  12852. LinesToStr([ // $mod.$main
  12853. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  12854. 'if ($mod.TRec.GetInt() === 2) ;',
  12855. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12856. '$mod.TRec.SetInt($mod.TRec.Fx);',
  12857. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  12858. 'if ($mod.TRec.GetInt() === 2) ;',
  12859. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12860. '$mod.TRec.SetInt($mod.r.Fx);',
  12861. '']));
  12862. end;
  12863. procedure TTestModule.TestAdvRecord_Const;
  12864. begin
  12865. StartProgram(false);
  12866. Add([
  12867. '{$modeswitch AdvancedRecords}',
  12868. 'type',
  12869. ' TArrInt = array[3..4] of longint;',
  12870. ' TPoint = record',
  12871. ' x,y: longint;',
  12872. ' class var Count: nativeint;',
  12873. ' end;',
  12874. ' TRec = record',
  12875. ' i: longint;',
  12876. ' a: array of longint;',
  12877. ' s: array[1..2] of longint;',
  12878. ' m: array[1..2,3..4] of longint;',
  12879. ' p: TPoint;',
  12880. ' end;',
  12881. ' TPoints = array of TPoint;',
  12882. 'const',
  12883. ' r: TRec = (',
  12884. ' i:1;',
  12885. ' a:(2,3);',
  12886. ' s:(4,5);',
  12887. ' m:( (11,12), (13,14) );',
  12888. ' p: (x:21)',
  12889. ' );',
  12890. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12891. 'begin']);
  12892. ConvertProgram;
  12893. CheckSource('TestAdvRecord_Const',
  12894. LinesToStr([ // statements
  12895. 'rtl.recNewT(this, "TPoint", function () {',
  12896. ' this.x = 0;',
  12897. ' this.y = 0;',
  12898. ' this.Count = 0;',
  12899. ' this.$eq = function (b) {',
  12900. ' return (this.x === b.x) && (this.y === b.y);',
  12901. ' };',
  12902. ' this.$assign = function (s) {',
  12903. ' this.x = s.x;',
  12904. ' this.y = s.y;',
  12905. ' return this;',
  12906. ' };',
  12907. '}, true);',
  12908. 'rtl.recNewT(this, "TRec", function () {',
  12909. ' this.i = 0;',
  12910. ' this.m$a$clone = function (a) {',
  12911. ' var b = [];',
  12912. ' b.length = 2;',
  12913. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12914. ' return b;',
  12915. ' };',
  12916. ' this.$new = function () {',
  12917. ' var r = Object.create(this);',
  12918. ' r.a = [];',
  12919. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12920. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12921. ' r.p = $mod.TPoint.$new();',
  12922. ' return r;',
  12923. ' };',
  12924. ' this.$eq = function (b) {',
  12925. ' 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);',
  12926. ' };',
  12927. ' this.$assign = function (s) {',
  12928. ' this.i = s.i;',
  12929. ' this.a = rtl.arrayRef(s.a);',
  12930. ' this.s = s.s.slice(0);',
  12931. ' this.m = this.m$a$clone(s.m);',
  12932. ' this.p.$assign(s.p);',
  12933. ' return this;',
  12934. ' };',
  12935. '});',
  12936. 'this.r = this.TRec.$clone({',
  12937. ' i: 1,',
  12938. ' a: [2, 3],',
  12939. ' s: [4, 5],',
  12940. ' m: [[11, 12], [13, 14]],',
  12941. ' p: this.TPoint.$clone({',
  12942. ' x: 21,',
  12943. ' y: 0',
  12944. ' })',
  12945. '});',
  12946. 'this.p = [this.TPoint.$clone({',
  12947. ' x: 1,',
  12948. ' y: 2',
  12949. '}), this.TPoint.$clone({',
  12950. ' x: 3,',
  12951. ' y: 4',
  12952. '})];',
  12953. '']),
  12954. LinesToStr([ // $mod.$main
  12955. '']));
  12956. end;
  12957. procedure TTestModule.TestAdvRecord_ExternalField;
  12958. begin
  12959. StartProgram(false);
  12960. Add([
  12961. '{$modeswitch AdvancedRecords}',
  12962. '{$modeswitch externalclass}',
  12963. 'type',
  12964. ' TCar = record',
  12965. ' public',
  12966. ' Intern: longint external name ''$Intern'';',
  12967. ' Intern2: longint external name ''$Intern2'';',
  12968. ' Bracket: longint external name ''["A B"]'';',
  12969. ' procedure DoIt;',
  12970. ' end;',
  12971. 'procedure tcar.doit;',
  12972. 'begin',
  12973. ' Intern:=Intern+1;',
  12974. ' Intern2:=Intern2+2;',
  12975. ' Bracket:=Bracket+3;',
  12976. 'end;',
  12977. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  12978. 'begin',
  12979. ' Rec.intern:=Rec.intern+1;',
  12980. ' Rec.intern2:=Rec.intern2+2;',
  12981. ' Rec.Bracket:=Rec.Bracket+3;',
  12982. ' with Rec do begin',
  12983. ' intern:=intern+1;',
  12984. ' intern2:=intern2+2;',
  12985. ' Bracket:=Bracket+3;',
  12986. ' end;']);
  12987. ConvertProgram;
  12988. CheckSource('TestAdvRecord_ExternalField',
  12989. LinesToStr([ // statements
  12990. 'rtl.recNewT(this, "TCar", function () {',
  12991. ' this.$eq = function (b) {',
  12992. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  12993. ' };',
  12994. ' this.$assign = function (s) {',
  12995. ' this.$Intern = s.$Intern;',
  12996. ' this.$Intern2 = s.$Intern2;',
  12997. ' this["A B"] = s["A B"];',
  12998. ' return this;',
  12999. ' };',
  13000. ' this.DoIt = function () {',
  13001. ' this.$Intern = this.$Intern + 1;',
  13002. ' this.$Intern2 = this.$Intern2 + 2;',
  13003. ' this["A B"] = this["A B"] + 3;',
  13004. ' };',
  13005. '});',
  13006. 'this.Rec = this.TCar.$clone({',
  13007. ' $Intern: 11,',
  13008. ' $Intern2: 12,',
  13009. ' "A B": 13',
  13010. '});',
  13011. '']),
  13012. LinesToStr([ // $mod.$main
  13013. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13014. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13015. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13016. 'var $with = $mod.Rec;',
  13017. '$with.$Intern = $with.$Intern + 1;',
  13018. '$with.$Intern2 = $with.$Intern2 + 2;',
  13019. '$with["A B"] = $with["A B"] + 3;',
  13020. '']));
  13021. end;
  13022. procedure TTestModule.TestAdvRecord_SubRecord;
  13023. begin
  13024. StartProgram(false);
  13025. Add([
  13026. '{$modeswitch AdvancedRecords}',
  13027. 'type',
  13028. ' TRec = record',
  13029. ' type',
  13030. ' TPoint = record',
  13031. ' x,y: longint;',
  13032. ' class var Count: nativeint;',
  13033. ' procedure DoIt;',
  13034. ' class procedure DoThat; static;',
  13035. ' end;',
  13036. ' var',
  13037. ' i: longint;',
  13038. ' p: TPoint;',
  13039. ' procedure DoSome;',
  13040. ' end;',
  13041. 'const',
  13042. ' r: TRec = (',
  13043. ' i:1;',
  13044. ' p: (x:21;y:22)',
  13045. ' );',
  13046. 'procedure TRec.DoSome;',
  13047. 'begin',
  13048. ' p.x:=p.y+1;',
  13049. ' p.Count:=p.Count+2;',
  13050. 'end;',
  13051. 'procedure TRec.TPoint.DoIt;',
  13052. 'begin',
  13053. ' Count:=Count+3;',
  13054. 'end;',
  13055. 'class procedure TRec.TPoint.DoThat;',
  13056. 'begin',
  13057. ' Count:=Count+4;',
  13058. 'end;',
  13059. 'begin']);
  13060. ConvertProgram;
  13061. CheckSource('TestAdvRecord_SubRecord',
  13062. LinesToStr([ // statements
  13063. 'rtl.recNewT(this, "TRec", function () {',
  13064. ' rtl.recNewT(this, "TPoint", function () {',
  13065. ' this.x = 0;',
  13066. ' this.y = 0;',
  13067. ' this.Count = 0;',
  13068. ' this.$eq = function (b) {',
  13069. ' return (this.x === b.x) && (this.y === b.y);',
  13070. ' };',
  13071. ' this.$assign = function (s) {',
  13072. ' this.x = s.x;',
  13073. ' this.y = s.y;',
  13074. ' return this;',
  13075. ' };',
  13076. ' this.DoIt = function () {',
  13077. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13078. ' };',
  13079. ' this.DoThat = function () {',
  13080. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13081. ' };',
  13082. ' }, true);',
  13083. ' this.i = 0;',
  13084. ' this.$new = function () {',
  13085. ' var r = Object.create(this);',
  13086. ' r.p = this.TPoint.$new();',
  13087. ' return r;',
  13088. ' };',
  13089. ' this.$eq = function (b) {',
  13090. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13091. ' };',
  13092. ' this.$assign = function (s) {',
  13093. ' this.i = s.i;',
  13094. ' this.p.$assign(s.p);',
  13095. ' return this;',
  13096. ' };',
  13097. ' this.DoSome = function () {',
  13098. ' this.p.x = this.p.y + 1;',
  13099. ' this.TPoint.Count = this.p.Count + 2;',
  13100. ' };',
  13101. '}, true);',
  13102. 'this.r = this.TRec.$clone({',
  13103. ' i: 1,',
  13104. ' p: this.TRec.TPoint.$clone({',
  13105. ' x: 21,',
  13106. ' y: 22',
  13107. ' })',
  13108. '});',
  13109. '']),
  13110. LinesToStr([ // $mod.$main
  13111. '']));
  13112. end;
  13113. procedure TTestModule.TestAdvRecord_SubClass;
  13114. begin
  13115. StartProgram(false);
  13116. Add([
  13117. '{$modeswitch AdvancedRecords}',
  13118. 'type',
  13119. ' TObject = class end;',
  13120. ' TPoint = record',
  13121. ' type',
  13122. ' TBird = class',
  13123. ' procedure DoIt;',
  13124. ' class procedure Glob;',
  13125. ' end;',
  13126. ' procedure DoIt(b: TBird);',
  13127. ' end;',
  13128. 'procedure TPoint.TBird.DoIt;',
  13129. 'begin',
  13130. ' doit;',
  13131. ' self.doit;',
  13132. ' glob;',
  13133. ' self.glob;',
  13134. 'end;',
  13135. 'class procedure TPoint.TBird.Glob;',
  13136. 'begin',
  13137. ' glob;',
  13138. ' self.glob;',
  13139. 'end;',
  13140. 'procedure TPoint.DoIt(b: TBird);',
  13141. 'begin',
  13142. ' b.doit;',
  13143. ' b.glob;',
  13144. ' TBird.glob;',
  13145. 'end;',
  13146. 'begin',
  13147. '']);
  13148. ConvertProgram;
  13149. CheckSource('TestAdvRecord_SubClass',
  13150. LinesToStr([ // statements
  13151. 'rtl.createClass(this, "TObject", null, function () {',
  13152. ' this.$init = function () {',
  13153. ' };',
  13154. ' this.$final = function () {',
  13155. ' };',
  13156. '});',
  13157. 'rtl.recNewT(this, "TPoint", function () {',
  13158. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13159. ' this.DoIt = function () {',
  13160. ' this.DoIt();',
  13161. ' this.DoIt();',
  13162. ' this.$class.Glob();',
  13163. ' this.$class.Glob();',
  13164. ' };',
  13165. ' this.Glob = function () {',
  13166. ' this.Glob();',
  13167. ' this.Glob();',
  13168. ' };',
  13169. ' }, "TPoint.TBird");',
  13170. ' this.$eq = function (b) {',
  13171. ' return true;',
  13172. ' };',
  13173. ' this.$assign = function (s) {',
  13174. ' return this;',
  13175. ' };',
  13176. ' this.DoIt = function (b) {',
  13177. ' b.DoIt();',
  13178. ' b.$class.Glob();',
  13179. ' this.TBird.Glob();',
  13180. ' };',
  13181. '}, true);',
  13182. '']),
  13183. LinesToStr([ // $mod.$main
  13184. '']));
  13185. end;
  13186. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13187. begin
  13188. StartProgram(false);
  13189. Add([
  13190. '{$modeswitch AdvancedRecords}',
  13191. 'type',
  13192. ' IUnknown = interface end;',
  13193. ' TPoint = record',
  13194. ' type IBird = interface end;',
  13195. ' end;',
  13196. 'begin',
  13197. '']);
  13198. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13199. nNotYetImplemented);
  13200. ParseProgram;
  13201. end;
  13202. procedure TTestModule.TestAdvRecord_Constructor;
  13203. begin
  13204. StartProgram(false);
  13205. Add([
  13206. '{$modeswitch AdvancedRecords}',
  13207. 'type',
  13208. ' TPoint = record',
  13209. ' x,y: longint;',
  13210. ' class procedure Run(w: longint = 13); static;',
  13211. ' constructor Create(ax: longint; ay: longint = -1);',
  13212. ' end;',
  13213. 'class procedure tpoint.run(w: longint);',
  13214. 'begin',
  13215. ' run;',
  13216. ' run();',
  13217. 'end;',
  13218. 'constructor tpoint.create(ax,ay: longint);',
  13219. 'begin',
  13220. ' x:=ax;',
  13221. ' self.y:=ay;',
  13222. ' run;',
  13223. ' run(ax);',
  13224. 'end;',
  13225. 'var r: TPoint;',
  13226. 'begin',
  13227. ' r:=TPoint.Create(1,2);',
  13228. ' with TPoint do r:=Create(1,2);',
  13229. ' r.Create(3);',
  13230. ' r:=r.Create(4);',
  13231. '']);
  13232. ConvertProgram;
  13233. CheckSource('TestAdvRecord_Constructor',
  13234. LinesToStr([ // statements
  13235. 'rtl.recNewT(this, "TPoint", function () {',
  13236. ' this.x = 0;',
  13237. ' this.y = 0;',
  13238. ' this.$eq = function (b) {',
  13239. ' return (this.x === b.x) && (this.y === b.y);',
  13240. ' };',
  13241. ' this.$assign = function (s) {',
  13242. ' this.x = s.x;',
  13243. ' this.y = s.y;',
  13244. ' return this;',
  13245. ' };',
  13246. ' this.Run = function (w) {',
  13247. ' $mod.TPoint.Run(13);',
  13248. ' $mod.TPoint.Run(13);',
  13249. ' };',
  13250. ' this.Create = function (ax, ay) {',
  13251. ' this.x = ax;',
  13252. ' this.y = ay;',
  13253. ' this.Run(13);',
  13254. ' this.Run(ax);',
  13255. ' return this;',
  13256. ' };',
  13257. '});',
  13258. 'this.r = this.TPoint.$new();',
  13259. '']),
  13260. LinesToStr([ // $mod.$main
  13261. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13262. 'var $with = $mod.TPoint;',
  13263. '$mod.r.$assign($with.$new().Create(1, 2));',
  13264. '$mod.r.Create(3, -1);',
  13265. '$mod.r.$assign($mod.r.Create(4, -1));',
  13266. '']));
  13267. end;
  13268. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13269. begin
  13270. StartProgram(false);
  13271. Add([
  13272. '{$modeswitch AdvancedRecords}',
  13273. 'type',
  13274. ' TPoint = record',
  13275. ' class var x: longint;',
  13276. ' class procedure Fly; static;',
  13277. ' class constructor Init;',
  13278. ' end;',
  13279. 'var count: word;',
  13280. 'class procedure Tpoint.Fly;',
  13281. 'begin',
  13282. 'end;',
  13283. 'class constructor tpoint.init;',
  13284. 'begin',
  13285. ' count:=count+1;',
  13286. ' x:=x+3;',
  13287. ' tpoint.x:=tpoint.x+4;',
  13288. ' fly;',
  13289. ' tpoint.fly;',
  13290. 'end;',
  13291. 'var r: TPoint;',
  13292. 'begin',
  13293. ' r.x:=r.x+10;',
  13294. ' r.Fly;',
  13295. ' r.Fly();',
  13296. '']);
  13297. ConvertProgram;
  13298. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13299. LinesToStr([ // statements
  13300. 'rtl.recNewT(this, "TPoint", function () {',
  13301. ' this.x = 0;',
  13302. ' this.$eq = function (b) {',
  13303. ' return true;',
  13304. ' };',
  13305. ' this.$assign = function (s) {',
  13306. ' return this;',
  13307. ' };',
  13308. ' this.Fly = function () {',
  13309. ' };',
  13310. '}, true);',
  13311. 'this.count = 0;',
  13312. 'this.r = this.TPoint.$new();',
  13313. '']),
  13314. LinesToStr([ // $mod.$main
  13315. '(function () {',
  13316. ' $mod.count = $mod.count + 1;',
  13317. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13318. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13319. ' $mod.TPoint.Fly();',
  13320. ' $mod.TPoint.Fly();',
  13321. '})();',
  13322. '$mod.TPoint.x = $mod.r.x + 10;',
  13323. '$mod.TPoint.Fly();',
  13324. '$mod.TPoint.Fly();',
  13325. '']));
  13326. end;
  13327. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13328. begin
  13329. StartUnit(false);
  13330. Add([
  13331. 'interface',
  13332. '{$modeswitch AdvancedRecords}',
  13333. 'type',
  13334. ' TPoint = record',
  13335. ' class var x: longint;',
  13336. ' class procedure Fly; static;',
  13337. ' class constructor Init;',
  13338. ' end;',
  13339. 'implementation',
  13340. 'var count: word;',
  13341. 'class procedure Tpoint.Fly;',
  13342. 'begin',
  13343. 'end;',
  13344. 'class constructor tpoint.init;',
  13345. 'begin',
  13346. ' count:=count+1;',
  13347. ' x:=3;',
  13348. ' tpoint.x:=4;',
  13349. ' fly;',
  13350. ' tpoint.fly;',
  13351. 'end;',
  13352. '']);
  13353. ConvertUnit;
  13354. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13355. LinesToStr([ // statements
  13356. 'var $impl = $mod.$impl;',
  13357. 'rtl.recNewT(this, "TPoint", function () {',
  13358. ' this.x = 0;',
  13359. ' this.$eq = function (b) {',
  13360. ' return true;',
  13361. ' };',
  13362. ' this.$assign = function (s) {',
  13363. ' return this;',
  13364. ' };',
  13365. ' this.Fly = function () {',
  13366. ' };',
  13367. '}, true);',
  13368. '']),
  13369. LinesToStr([ // $mod.$init
  13370. '(function () {',
  13371. ' $impl.count = $impl.count + 1;',
  13372. ' $mod.TPoint.x = 3;',
  13373. ' $mod.TPoint.x = 4;',
  13374. ' $mod.TPoint.Fly();',
  13375. ' $mod.TPoint.Fly();',
  13376. '})();',
  13377. '']),
  13378. LinesToStr([ // $mod.$main
  13379. '$impl.count = 0;',
  13380. '']));
  13381. end;
  13382. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13383. begin
  13384. StartProgram(false);
  13385. Add(['type',
  13386. ' TObject = class',
  13387. ' public',
  13388. ' constructor Create;',
  13389. ' destructor Destroy;',
  13390. ' end;',
  13391. ' TBird = TObject;',
  13392. 'constructor tobject.create;',
  13393. 'begin end;',
  13394. 'destructor tobject.destroy;',
  13395. 'begin end;',
  13396. 'var Obj: tobject;',
  13397. 'begin',
  13398. ' obj:=tobject.create;',
  13399. ' obj:=tobject.create();',
  13400. ' obj:=tbird.create;',
  13401. ' obj:=tbird.create();',
  13402. ' obj:=obj.create();',
  13403. ' obj.destroy;',
  13404. '']);
  13405. ConvertProgram;
  13406. CheckSource('TestClass_TObjectDefaultConstructor',
  13407. LinesToStr([ // statements
  13408. 'rtl.createClass(this,"TObject",null,function(){',
  13409. ' this.$init = function () {',
  13410. ' };',
  13411. ' this.$final = function () {',
  13412. ' };',
  13413. ' this.Create = function(){',
  13414. ' return this;',
  13415. ' };',
  13416. ' this.Destroy = function(){',
  13417. ' };',
  13418. '});',
  13419. 'this.Obj = null;'
  13420. ]),
  13421. LinesToStr([ // $mod.$main
  13422. '$mod.Obj = $mod.TObject.$create("Create");',
  13423. '$mod.Obj = $mod.TObject.$create("Create");',
  13424. '$mod.Obj = $mod.TObject.$create("Create");',
  13425. '$mod.Obj = $mod.TObject.$create("Create");',
  13426. '$mod.Obj = $mod.Obj.Create();',
  13427. '$mod.Obj.$destroy("Destroy");',
  13428. '']));
  13429. end;
  13430. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13431. begin
  13432. StartProgram(false);
  13433. Add('type');
  13434. Add(' TObject = class');
  13435. Add(' public');
  13436. Add(' constructor Create(Par: longint);');
  13437. Add(' end;');
  13438. Add('constructor tobject.create(par: longint);');
  13439. Add('begin end;');
  13440. Add('var Obj: tobject;');
  13441. Add('begin');
  13442. Add(' obj:=tobject.create(3);');
  13443. ConvertProgram;
  13444. CheckSource('TestClass_TObjectConstructorWithParams',
  13445. LinesToStr([ // statements
  13446. 'rtl.createClass(this,"TObject",null,function(){',
  13447. ' this.$init = function () {',
  13448. ' };',
  13449. ' this.$final = function () {',
  13450. ' };',
  13451. ' this.Create = function(Par){',
  13452. ' return this;',
  13453. ' };',
  13454. '});',
  13455. 'this.Obj = null;'
  13456. ]),
  13457. LinesToStr([ // $mod.$main
  13458. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13459. ]));
  13460. end;
  13461. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13462. begin
  13463. StartProgram(false);
  13464. Add('type');
  13465. Add(' TObject = class');
  13466. Add(' public');
  13467. Add(' constructor Create;');
  13468. Add(' end;');
  13469. Add(' TTest = class(TObject)');
  13470. Add(' public');
  13471. Add(' constructor Create(const Par: longint = 1);');
  13472. Add(' end;');
  13473. Add('constructor tobject.create;');
  13474. Add('begin end;');
  13475. Add('constructor ttest.create(const par: longint);');
  13476. Add('begin end;');
  13477. Add('var t: ttest;');
  13478. Add('begin');
  13479. Add(' t:=ttest.create;');
  13480. Add(' t:=ttest.create(2);');
  13481. ConvertProgram;
  13482. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13483. LinesToStr([ // statements
  13484. 'rtl.createClass(this,"TObject",null,function(){',
  13485. ' this.$init = function () {',
  13486. ' };',
  13487. ' this.$final = function () {',
  13488. ' };',
  13489. ' this.Create = function(){',
  13490. ' return this;',
  13491. ' };',
  13492. '});',
  13493. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13494. ' this.Create$1 = function (Par) {',
  13495. ' return this;',
  13496. ' };',
  13497. '});',
  13498. 'this.t = null;'
  13499. ]),
  13500. LinesToStr([ // $mod.$main
  13501. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13502. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13503. ]));
  13504. end;
  13505. procedure TTestModule.TestClass_Var;
  13506. begin
  13507. StartProgram(false);
  13508. Add([
  13509. 'type',
  13510. ' TObject = class',
  13511. ' public',
  13512. ' vI: longint;',
  13513. ' constructor Create(Par: longint);',
  13514. ' end;',
  13515. 'constructor tobject.create(par: longint);',
  13516. 'begin',
  13517. ' vi:=par+3',
  13518. 'end;',
  13519. 'var Obj: tobject;',
  13520. 'begin',
  13521. ' obj:=tobject.create(4);',
  13522. ' obj.vi:=obj.VI+5;']);
  13523. ConvertProgram;
  13524. CheckSource('TestClass_Var',
  13525. LinesToStr([ // statements
  13526. 'rtl.createClass(this,"TObject",null,function(){',
  13527. ' this.$init = function () {',
  13528. ' this.vI = 0;',
  13529. ' };',
  13530. ' this.$final = function () {',
  13531. ' };',
  13532. ' this.Create = function(Par){',
  13533. ' this.vI = Par+3;',
  13534. ' return this;',
  13535. ' };',
  13536. '});',
  13537. 'this.Obj = null;'
  13538. ]),
  13539. LinesToStr([ // $mod.$main
  13540. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13541. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13542. ]));
  13543. end;
  13544. procedure TTestModule.TestClass_Method;
  13545. begin
  13546. StartProgram(false);
  13547. Add('type');
  13548. Add(' TObject = class');
  13549. Add(' public');
  13550. Add(' vI: longint;');
  13551. Add(' Sub: TObject;');
  13552. Add(' constructor Create;');
  13553. Add(' function GetIt(Par: longint): tobject;');
  13554. Add(' end;');
  13555. Add('constructor tobject.create; begin end;');
  13556. Add('function tobject.getit(par: longint): tobject;');
  13557. Add('begin');
  13558. Add(' Self.vi:=par+3;');
  13559. Add(' Result:=self.sub;');
  13560. Add('end;');
  13561. Add('var Obj: tobject;');
  13562. Add('begin');
  13563. Add(' obj:=tobject.create;');
  13564. Add(' obj.getit(4);');
  13565. Add(' obj.sub.sub:=nil;');
  13566. Add(' obj.sub.getit(5);');
  13567. Add(' obj.sub.getit(6).SUB:=nil;');
  13568. Add(' obj.sub.getit(7).GETIT(8);');
  13569. Add(' obj.sub.getit(9).SuB.getit(10);');
  13570. ConvertProgram;
  13571. CheckSource('TestClass_Method',
  13572. LinesToStr([ // statements
  13573. 'rtl.createClass(this,"TObject",null,function(){',
  13574. ' this.$init = function () {',
  13575. ' this.vI = 0;',
  13576. ' this.Sub = null;',
  13577. ' };',
  13578. ' this.$final = function () {',
  13579. ' this.Sub = undefined;',
  13580. ' };',
  13581. ' this.Create = function(){',
  13582. ' return this;',
  13583. ' };',
  13584. ' this.GetIt = function(Par){',
  13585. ' var Result = null;',
  13586. ' this.vI = Par + 3;',
  13587. ' Result = this.Sub;',
  13588. ' return Result;',
  13589. ' };',
  13590. '});',
  13591. 'this.Obj = null;'
  13592. ]),
  13593. LinesToStr([ // $mod.$main
  13594. '$mod.Obj = $mod.TObject.$create("Create");',
  13595. '$mod.Obj.GetIt(4);',
  13596. '$mod.Obj.Sub.Sub=null;',
  13597. '$mod.Obj.Sub.GetIt(5);',
  13598. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13599. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13600. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13601. ]));
  13602. end;
  13603. procedure TTestModule.TestClass_Implementation;
  13604. begin
  13605. StartUnit(false);
  13606. Add([
  13607. 'interface',
  13608. 'type',
  13609. ' TObject = class',
  13610. ' constructor Create;',
  13611. ' end;',
  13612. 'implementation',
  13613. 'type',
  13614. ' TIntClass = class',
  13615. ' constructor Create; reintroduce;',
  13616. ' class procedure DoGlob;',
  13617. ' end;',
  13618. 'constructor tintclass.create;',
  13619. 'begin',
  13620. ' inherited;',
  13621. ' inherited create;',
  13622. ' doglob;',
  13623. 'end;',
  13624. 'class procedure tintclass.doglob;',
  13625. 'begin',
  13626. 'end;',
  13627. 'constructor tobject.create;',
  13628. 'var',
  13629. ' iC: tintclass;',
  13630. 'begin',
  13631. ' ic:=tintclass.create;',
  13632. ' tintclass.doglob;',
  13633. ' ic.doglob;',
  13634. 'end;',
  13635. 'initialization',
  13636. ' tintclass.doglob;',
  13637. '']);
  13638. ConvertUnit;
  13639. CheckSource('TestClass_Implementation',
  13640. LinesToStr([ // statements
  13641. 'var $impl = $mod.$impl;',
  13642. 'rtl.createClass(this, "TObject", null, function () {',
  13643. ' this.$init = function () {',
  13644. ' };',
  13645. ' this.$final = function () {',
  13646. ' };',
  13647. ' this.Create = function () {',
  13648. ' var iC = null;',
  13649. ' iC = $impl.TIntClass.$create("Create$1");',
  13650. ' $impl.TIntClass.DoGlob();',
  13651. ' iC.$class.DoGlob();',
  13652. ' return this;',
  13653. ' };',
  13654. '});',
  13655. '']),
  13656. LinesToStr([ // $mod.$main
  13657. '$impl.TIntClass.DoGlob();',
  13658. '']),
  13659. LinesToStr([
  13660. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  13661. ' this.Create$1 = function () {',
  13662. ' $mod.TObject.Create.call(this);',
  13663. ' $mod.TObject.Create.call(this);',
  13664. ' this.$class.DoGlob();',
  13665. ' return this;',
  13666. ' };',
  13667. ' this.DoGlob = function () {',
  13668. ' };',
  13669. '});',
  13670. '']));
  13671. end;
  13672. procedure TTestModule.TestClass_Inheritance;
  13673. begin
  13674. StartProgram(false);
  13675. Add('type');
  13676. Add(' TObject = class');
  13677. Add(' public');
  13678. Add(' constructor Create;');
  13679. Add(' end;');
  13680. Add(' TClassA = class');
  13681. Add(' end;');
  13682. Add(' TClassB = class(TObject)');
  13683. Add(' procedure ProcB;');
  13684. Add(' end;');
  13685. Add('constructor tobject.create; begin end;');
  13686. Add('procedure tclassb.procb; begin end;');
  13687. Add('var');
  13688. Add(' oO: TObject;');
  13689. Add(' oA: TClassA;');
  13690. Add(' oB: TClassB;');
  13691. Add('begin');
  13692. Add(' oO:=tobject.Create;');
  13693. Add(' oA:=tclassa.Create;');
  13694. Add(' ob:=tclassb.Create;');
  13695. Add(' if oo is tclassa then ;');
  13696. Add(' ob:=oo as tclassb;');
  13697. Add(' (oo as tclassb).procb;');
  13698. ConvertProgram;
  13699. CheckSource('TestClass_Inheritance',
  13700. LinesToStr([ // statements
  13701. 'rtl.createClass(this,"TObject",null,function(){',
  13702. ' this.$init = function () {',
  13703. ' };',
  13704. ' this.$final = function () {',
  13705. ' };',
  13706. ' this.Create = function () {',
  13707. ' return this;',
  13708. ' };',
  13709. '});',
  13710. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  13711. '});',
  13712. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  13713. ' this.ProcB = function () {',
  13714. ' };',
  13715. '});',
  13716. 'this.oO = null;',
  13717. 'this.oA = null;',
  13718. 'this.oB = null;'
  13719. ]),
  13720. LinesToStr([ // $mod.$main
  13721. '$mod.oO = $mod.TObject.$create("Create");',
  13722. '$mod.oA = $mod.TClassA.$create("Create");',
  13723. '$mod.oB = $mod.TClassB.$create("Create");',
  13724. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  13725. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  13726. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  13727. ]));
  13728. end;
  13729. procedure TTestModule.TestClass_TypeAlias;
  13730. begin
  13731. StartProgram(false);
  13732. Add([
  13733. '{$interfaces corba}',
  13734. 'type',
  13735. ' IObject = interface',
  13736. ' end;',
  13737. ' IBird = type IObject;',
  13738. ' TObject = class',
  13739. ' end;',
  13740. ' TBird = type TObject;',
  13741. 'var',
  13742. ' oObj: TObject;',
  13743. ' oBird: TBird;',
  13744. ' IntfObj: IObject;',
  13745. ' IntfBird: IBird;',
  13746. 'begin',
  13747. ' oObj:=oBird;',
  13748. '']);
  13749. ConvertProgram;
  13750. CheckSource('TestClass_TypeAlias',
  13751. LinesToStr([ // statements
  13752. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  13753. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  13754. 'rtl.createClass(this, "TObject", null, function () {',
  13755. ' this.$init = function () {',
  13756. ' };',
  13757. ' this.$final = function () {',
  13758. ' };',
  13759. '});',
  13760. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13761. '});',
  13762. 'this.oObj = null;',
  13763. 'this.oBird = null;',
  13764. 'this.IntfObj = null;',
  13765. 'this.IntfBird = null;',
  13766. '']),
  13767. LinesToStr([ // $mod.$main
  13768. '$mod.oObj = $mod.oBird;',
  13769. '']));
  13770. end;
  13771. procedure TTestModule.TestClass_AbstractMethod;
  13772. begin
  13773. StartProgram(false);
  13774. Add('type');
  13775. Add(' TObject = class');
  13776. Add(' public');
  13777. Add(' procedure DoIt; virtual; abstract;');
  13778. Add(' end;');
  13779. Add('begin');
  13780. ConvertProgram;
  13781. CheckSource('TestClass_AbstractMethod',
  13782. LinesToStr([ // statements
  13783. 'rtl.createClass(this,"TObject",null,function(){',
  13784. ' this.$init = function () {',
  13785. ' };',
  13786. ' this.$final = function () {',
  13787. ' };',
  13788. '});'
  13789. ]),
  13790. LinesToStr([ // this.$main
  13791. ''
  13792. ]));
  13793. end;
  13794. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  13795. begin
  13796. StartProgram(false);
  13797. Add([
  13798. 'type',
  13799. ' TObject = class',
  13800. ' procedure DoAbstract; virtual; abstract;',
  13801. ' procedure DoVirtual; virtual;',
  13802. ' procedure DoIt;',
  13803. ' end;',
  13804. ' TA = class',
  13805. ' procedure doabstract; override;',
  13806. ' procedure dovirtual; override;',
  13807. ' procedure DoSome;',
  13808. ' end;',
  13809. 'procedure tobject.dovirtual;',
  13810. 'begin',
  13811. ' inherited; // call non existing ancestor -> ignore silently',
  13812. 'end;',
  13813. 'procedure tobject.doit;',
  13814. 'begin',
  13815. 'end;',
  13816. 'procedure ta.doabstract;',
  13817. 'begin',
  13818. ' inherited dovirtual; // call TObject.DoVirtual',
  13819. 'end;',
  13820. 'procedure ta.dovirtual;',
  13821. 'begin',
  13822. ' inherited; // call TObject.DoVirtual',
  13823. ' inherited dovirtual; // call TObject.DoVirtual',
  13824. ' inherited dovirtual(); // call TObject.DoVirtual',
  13825. ' doit;',
  13826. ' doit();',
  13827. 'end;',
  13828. 'procedure ta.dosome;',
  13829. 'begin',
  13830. ' inherited; // call non existing ancestor method -> silently ignore',
  13831. 'end;',
  13832. 'begin']);
  13833. ConvertProgram;
  13834. CheckSource('TestClass_CallInherited_ProcNoParams',
  13835. LinesToStr([ // statements
  13836. 'rtl.createClass(this,"TObject",null,function(){',
  13837. ' this.$init = function () {',
  13838. ' };',
  13839. ' this.$final = function () {',
  13840. ' };',
  13841. ' this.DoVirtual = function () {',
  13842. ' };',
  13843. ' this.DoIt = function () {',
  13844. ' };',
  13845. '});',
  13846. 'rtl.createClass(this, "TA", this.TObject, function () {',
  13847. ' this.DoAbstract = function () {',
  13848. ' $mod.TObject.DoVirtual.call(this);',
  13849. ' };',
  13850. ' this.DoVirtual = function () {',
  13851. ' $mod.TObject.DoVirtual.call(this);',
  13852. ' $mod.TObject.DoVirtual.call(this);',
  13853. ' $mod.TObject.DoVirtual.call(this);',
  13854. ' this.DoIt();',
  13855. ' this.DoIt();',
  13856. ' };',
  13857. ' this.DoSome = function () {',
  13858. ' };',
  13859. '});'
  13860. ]),
  13861. LinesToStr([ // this.$main
  13862. ''
  13863. ]));
  13864. end;
  13865. procedure TTestModule.TestClass_CallInherited_WithParams;
  13866. begin
  13867. StartProgram(false);
  13868. Add([
  13869. 'type',
  13870. ' TObject = class',
  13871. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  13872. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  13873. ' procedure DoIt(pA: longint; pB: longint = 0);',
  13874. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  13875. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13876. ' end;',
  13877. ' TClassA = class',
  13878. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  13879. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  13880. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13881. ' end;',
  13882. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  13883. 'begin',
  13884. 'end;',
  13885. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  13886. 'begin',
  13887. 'end;',
  13888. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  13889. 'begin',
  13890. 'end;',
  13891. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  13892. 'begin',
  13893. 'end;',
  13894. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  13895. 'begin',
  13896. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13897. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13898. 'end;',
  13899. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  13900. 'begin',
  13901. ' inherited; // call TObject.DoVirtual(pA,pB)',
  13902. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13903. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13904. ' doit(pa,pb);',
  13905. ' doit(pa);',
  13906. ' doit2(pa);',
  13907. ' doit2;',
  13908. 'end;',
  13909. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  13910. 'begin',
  13911. ' pa:=inherited;',
  13912. 'end;',
  13913. 'begin']);
  13914. ConvertProgram;
  13915. CheckSource('TestClass_CallInherited_WithParams',
  13916. LinesToStr([ // statements
  13917. 'rtl.createClass(this,"TObject",null,function(){',
  13918. ' this.$init = function () {',
  13919. ' };',
  13920. ' this.$final = function () {',
  13921. ' };',
  13922. ' this.DoVirtual = function (pA,pB) {',
  13923. ' };',
  13924. ' this.DoIt = function (pA,pB) {',
  13925. ' };',
  13926. ' this.DoIt2 = function (pA,pB) {',
  13927. ' };',
  13928. ' this.GetIt = function (pA, pB) {',
  13929. ' var Result = 0;',
  13930. ' return Result;',
  13931. ' };',
  13932. '});',
  13933. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  13934. ' this.DoAbstract = function (pA,pB) {',
  13935. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  13936. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  13937. ' };',
  13938. ' this.DoVirtual = function (pA,pB) {',
  13939. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  13940. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  13941. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  13942. ' this.DoIt(pA,pB);',
  13943. ' this.DoIt(pA,0);',
  13944. ' this.DoIt2(pA,2);',
  13945. ' this.DoIt2(1,2);',
  13946. ' };',
  13947. ' this.GetIt$1 = function (pA, pB) {',
  13948. ' var Result = 0;',
  13949. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  13950. ' return Result;',
  13951. ' };',
  13952. '});'
  13953. ]),
  13954. LinesToStr([ // this.$main
  13955. ''
  13956. ]));
  13957. end;
  13958. procedure TTestModule.TestClasS_CallInheritedConstructor;
  13959. begin
  13960. StartProgram(false);
  13961. Add('type');
  13962. Add(' TObject = class');
  13963. Add(' constructor Create; virtual;');
  13964. Add(' constructor CreateWithB(b: boolean);');
  13965. Add(' end;');
  13966. Add(' TA = class');
  13967. Add(' constructor Create; override;');
  13968. Add(' constructor CreateWithC(c: char);');
  13969. Add(' procedure DoIt;');
  13970. Add(' class function DoSome: TObject;');
  13971. Add(' end;');
  13972. Add('constructor tobject.create;');
  13973. Add('begin');
  13974. Add(' inherited; // call non existing ancestor -> ignore silently');
  13975. Add('end;');
  13976. Add('constructor tobject.createwithb(b: boolean);');
  13977. Add('begin');
  13978. Add(' inherited; // call non existing ancestor -> ignore silently');
  13979. Add(' create; // normal call');
  13980. Add('end;');
  13981. Add('constructor ta.create;');
  13982. Add('begin');
  13983. Add(' inherited; // normal call TObject.Create');
  13984. Add(' inherited create; // normal call TObject.Create');
  13985. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  13986. Add('end;');
  13987. Add('constructor ta.createwithc(c: char);');
  13988. Add('begin');
  13989. Add(' inherited create; // call TObject.Create');
  13990. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  13991. Add(' doit;');
  13992. Add(' doit();');
  13993. Add(' dosome;');
  13994. Add('end;');
  13995. Add('procedure ta.doit;');
  13996. Add('begin');
  13997. Add(' create; // normal call');
  13998. Add(' createwithb(false); // normal call');
  13999. Add(' createwithc(''c''); // normal call');
  14000. Add('end;');
  14001. Add('class function ta.dosome: TObject;');
  14002. Add('begin');
  14003. Add(' Result:=create; // constructor');
  14004. Add(' Result:=createwithb(true); // constructor');
  14005. Add(' Result:=createwithc(''c''); // constructor');
  14006. Add('end;');
  14007. Add('begin');
  14008. ConvertProgram;
  14009. CheckSource('TestClass_CallInheritedConstructor',
  14010. LinesToStr([ // statements
  14011. 'rtl.createClass(this,"TObject",null,function(){',
  14012. ' this.$init = function () {',
  14013. ' };',
  14014. ' this.$final = function () {',
  14015. ' };',
  14016. ' this.Create = function () {',
  14017. ' return this;',
  14018. ' };',
  14019. ' this.CreateWithB = function (b) {',
  14020. ' this.Create();',
  14021. ' return this;',
  14022. ' };',
  14023. '});',
  14024. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14025. ' this.Create = function () {',
  14026. ' $mod.TObject.Create.call(this);',
  14027. ' $mod.TObject.Create.call(this);',
  14028. ' $mod.TObject.CreateWithB.call(this, false);',
  14029. ' return this;',
  14030. ' };',
  14031. ' this.CreateWithC = function (c) {',
  14032. ' $mod.TObject.Create.call(this);',
  14033. ' $mod.TObject.CreateWithB.call(this, true);',
  14034. ' this.DoIt();',
  14035. ' this.DoIt();',
  14036. ' this.$class.DoSome();',
  14037. ' return this;',
  14038. ' };',
  14039. ' this.DoIt = function () {',
  14040. ' this.Create();',
  14041. ' this.CreateWithB(false);',
  14042. ' this.CreateWithC("c");',
  14043. ' };',
  14044. ' this.DoSome = function () {',
  14045. ' var Result = null;',
  14046. ' Result = this.$create("Create");',
  14047. ' Result = this.$create("CreateWithB", [true]);',
  14048. ' Result = this.$create("CreateWithC", ["c"]);',
  14049. ' return Result;',
  14050. ' };',
  14051. '});'
  14052. ]),
  14053. LinesToStr([ // this.$main
  14054. ''
  14055. ]));
  14056. end;
  14057. procedure TTestModule.TestClass_ClassVar_Assign;
  14058. begin
  14059. StartProgram(false);
  14060. Add([
  14061. 'type',
  14062. ' TObject = class',
  14063. ' public',
  14064. ' class var vI: longint;',
  14065. ' class var Sub: TObject;',
  14066. ' constructor Create;',
  14067. ' class function GetIt(var Par: longint): tobject;',
  14068. ' end;',
  14069. 'constructor tobject.create;',
  14070. 'begin',
  14071. ' vi:=vi+1;',
  14072. ' Self.vi:=Self.vi+1;',
  14073. ' inc(vi);',
  14074. 'end;',
  14075. 'class function tobject.getit(var par: longint): tobject;',
  14076. 'begin',
  14077. ' vi:=vi+3;',
  14078. ' Self.vi:=Self.vi+4;',
  14079. ' inc(vi);',
  14080. ' Result:=self.sub;',
  14081. ' GetIt(vi);',
  14082. 'end;',
  14083. 'var Obj: tobject;',
  14084. 'begin',
  14085. ' obj:=tobject.create;',
  14086. ' tobject.vi:=3;',
  14087. ' if tobject.vi=4 then ;',
  14088. ' tobject.sub:=nil;',
  14089. ' obj.sub:=nil;',
  14090. ' obj.sub.sub:=nil;']);
  14091. ConvertProgram;
  14092. CheckSource('TestClass_ClassVar_Assign',
  14093. LinesToStr([ // statements
  14094. 'rtl.createClass(this,"TObject",null,function(){',
  14095. ' this.vI = 0;',
  14096. ' this.Sub = null;',
  14097. ' this.$init = function () {',
  14098. ' };',
  14099. ' this.$final = function () {',
  14100. ' };',
  14101. ' this.Create = function(){',
  14102. ' $mod.TObject.vI = this.vI+1;',
  14103. ' $mod.TObject.vI = this.vI+1;',
  14104. ' $mod.TObject.vI += 1;',
  14105. ' return this;',
  14106. ' };',
  14107. ' this.GetIt = function(Par){',
  14108. ' var Result = null;',
  14109. ' $mod.TObject.vI = this.vI + 3;',
  14110. ' $mod.TObject.vI = this.vI + 4;',
  14111. ' $mod.TObject.vI += 1;',
  14112. ' Result = this.Sub;',
  14113. ' this.GetIt({',
  14114. ' p: $mod.TObject,',
  14115. ' get: function () {',
  14116. ' return this.p.vI;',
  14117. ' },',
  14118. ' set: function (v) {',
  14119. ' this.p.vI = v;',
  14120. ' }',
  14121. ' });',
  14122. ' return Result;',
  14123. ' };',
  14124. '});',
  14125. 'this.Obj = null;'
  14126. ]),
  14127. LinesToStr([ // $mod.$main
  14128. '$mod.Obj = $mod.TObject.$create("Create");',
  14129. '$mod.TObject.vI = 3;',
  14130. 'if ($mod.TObject.vI === 4);',
  14131. '$mod.TObject.Sub=null;',
  14132. '$mod.TObject.Sub=null;',
  14133. '$mod.TObject.Sub=null;',
  14134. '']));
  14135. end;
  14136. procedure TTestModule.TestClass_CallClassMethod;
  14137. begin
  14138. StartProgram(false);
  14139. Add('type');
  14140. Add(' TObject = class');
  14141. Add(' public');
  14142. Add(' class var vI: longint;');
  14143. Add(' class var Sub: TObject;');
  14144. Add(' constructor Create;');
  14145. Add(' function GetMore(Par: longint): longint;');
  14146. Add(' class function GetIt(Par: longint): tobject;');
  14147. Add(' end;');
  14148. Add('constructor tobject.create;');
  14149. Add('begin');
  14150. Add(' sub:=getit(3);');
  14151. Add(' vi:=getmore(4);');
  14152. Add(' sub:=Self.getit(5);');
  14153. Add(' vi:=Self.getmore(6);');
  14154. Add('end;');
  14155. Add('function tobject.getmore(par: longint): longint;');
  14156. Add('begin');
  14157. Add(' sub:=getit(11);');
  14158. Add(' vi:=getmore(12);');
  14159. Add(' sub:=self.getit(13);');
  14160. Add(' vi:=self.getmore(14);');
  14161. Add('end;');
  14162. Add('class function tobject.getit(par: longint): tobject;');
  14163. Add('begin');
  14164. Add(' sub:=getit(21);');
  14165. Add(' vi:=sub.getmore(22);');
  14166. Add(' sub:=self.getit(23);');
  14167. Add(' vi:=self.sub.getmore(24);');
  14168. Add('end;');
  14169. Add('var Obj: tobject;');
  14170. Add('begin');
  14171. Add(' obj:=tobject.create;');
  14172. Add(' tobject.getit(5);');
  14173. Add(' obj.getit(6);');
  14174. Add(' obj.sub.getit(7);');
  14175. Add(' obj.sub.getit(8).SUB:=nil;');
  14176. Add(' obj.sub.getit(9).GETIT(10);');
  14177. Add(' obj.sub.getit(11).SuB.getit(12);');
  14178. ConvertProgram;
  14179. CheckSource('TestClass_CallClassMethod',
  14180. LinesToStr([ // statements
  14181. 'rtl.createClass(this,"TObject",null,function(){',
  14182. ' this.vI = 0;',
  14183. ' this.Sub = null;',
  14184. ' this.$init = function () {',
  14185. ' };',
  14186. ' this.$final = function () {',
  14187. ' };',
  14188. ' this.Create = function(){',
  14189. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14190. ' $mod.TObject.vI = this.GetMore(4);',
  14191. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14192. ' $mod.TObject.vI = this.GetMore(6);',
  14193. ' return this;',
  14194. ' };',
  14195. ' this.GetMore = function(Par){',
  14196. ' var Result = 0;',
  14197. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14198. ' $mod.TObject.vI = this.GetMore(12);',
  14199. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14200. ' $mod.TObject.vI = this.GetMore(14);',
  14201. ' return Result;',
  14202. ' };',
  14203. ' this.GetIt = function(Par){',
  14204. ' var Result = null;',
  14205. ' $mod.TObject.Sub = this.GetIt(21);',
  14206. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14207. ' $mod.TObject.Sub = this.GetIt(23);',
  14208. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14209. ' return Result;',
  14210. ' };',
  14211. '});',
  14212. 'this.Obj = null;'
  14213. ]),
  14214. LinesToStr([ // $mod.$main
  14215. '$mod.Obj = $mod.TObject.$create("Create");',
  14216. '$mod.TObject.GetIt(5);',
  14217. '$mod.Obj.$class.GetIt(6);',
  14218. '$mod.Obj.Sub.$class.GetIt(7);',
  14219. '$mod.TObject.Sub=null;',
  14220. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14221. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14222. '']));
  14223. end;
  14224. procedure TTestModule.TestClass_CallClassMethodStatic;
  14225. begin
  14226. StartProgram(false);
  14227. Add([
  14228. 'type',
  14229. ' TObject = class',
  14230. ' public',
  14231. ' class function Fly: tobject; static;',
  14232. ' end;',
  14233. 'class function tobject.Fly: tobject;',
  14234. 'begin',
  14235. ' Result.Fly;',
  14236. ' Result.Fly();',
  14237. ' Fly;',
  14238. ' Fly();',
  14239. ' Fly.Fly;',
  14240. ' Fly.Fly();',
  14241. 'end;',
  14242. 'var Obj: tobject;',
  14243. 'begin',
  14244. ' obj.Fly;',
  14245. ' obj.Fly();',
  14246. ' with obj do begin',
  14247. ' Fly;',
  14248. ' Fly();',
  14249. ' end;',
  14250. '']);
  14251. ConvertProgram;
  14252. CheckSource('TestClass_CallClassMethodStatic',
  14253. LinesToStr([ // statements
  14254. 'rtl.createClass(this, "TObject", null, function () {',
  14255. ' this.$init = function () {',
  14256. ' };',
  14257. ' this.$final = function () {',
  14258. ' };',
  14259. ' this.Fly = function () {',
  14260. ' var Result = null;',
  14261. ' $mod.TObject.Fly();',
  14262. ' $mod.TObject.Fly();',
  14263. ' $mod.TObject.Fly();',
  14264. ' $mod.TObject.Fly();',
  14265. ' $mod.TObject.Fly();',
  14266. ' $mod.TObject.Fly();',
  14267. ' return Result;',
  14268. ' };',
  14269. '});',
  14270. 'this.Obj = null;'
  14271. ]),
  14272. LinesToStr([ // $mod.$main
  14273. '$mod.TObject.Fly();',
  14274. '$mod.TObject.Fly();',
  14275. 'var $with = $mod.Obj;',
  14276. '$with.Fly();',
  14277. '$with.Fly();',
  14278. '']));
  14279. end;
  14280. procedure TTestModule.TestClass_Property;
  14281. begin
  14282. StartProgram(false);
  14283. Add('type');
  14284. Add(' TObject = class');
  14285. Add(' Fx: longint;');
  14286. Add(' Fy: longint;');
  14287. Add(' function GetInt: longint;');
  14288. Add(' procedure SetInt(Value: longint);');
  14289. Add(' procedure DoIt;');
  14290. Add(' property IntA: longint read Fx write Fy;');
  14291. Add(' property IntB: longint read GetInt write SetInt;');
  14292. Add(' end;');
  14293. Add('function tobject.getint: longint;');
  14294. Add('begin');
  14295. Add(' result:=fx;');
  14296. Add('end;');
  14297. Add('procedure tobject.setint(value: longint);');
  14298. Add('begin');
  14299. Add(' if value=fy then exit;');
  14300. Add(' fy:=value;');
  14301. Add('end;');
  14302. Add('procedure tobject.doit;');
  14303. Add('begin');
  14304. Add(' IntA:=IntA+1;');
  14305. Add(' Self.IntA:=Self.IntA+1;');
  14306. Add(' IntB:=IntB+1;');
  14307. Add(' Self.IntB:=Self.IntB+1;');
  14308. Add('end;');
  14309. Add('var Obj: tobject;');
  14310. Add('begin');
  14311. Add(' obj.inta:=obj.inta+1;');
  14312. Add(' if obj.intb=2 then;');
  14313. Add(' obj.intb:=obj.intb+2;');
  14314. Add(' obj.setint(obj.inta);');
  14315. ConvertProgram;
  14316. CheckSource('TestClass_Property',
  14317. LinesToStr([ // statements
  14318. 'rtl.createClass(this, "TObject", null, function () {',
  14319. ' this.$init = function () {',
  14320. ' this.Fx = 0;',
  14321. ' this.Fy = 0;',
  14322. ' };',
  14323. ' this.$final = function () {',
  14324. ' };',
  14325. ' this.GetInt = function () {',
  14326. ' var Result = 0;',
  14327. ' Result = this.Fx;',
  14328. ' return Result;',
  14329. ' };',
  14330. ' this.SetInt = function (Value) {',
  14331. ' if (Value === this.Fy) return;',
  14332. ' this.Fy = Value;',
  14333. ' };',
  14334. ' this.DoIt = function () {',
  14335. ' this.Fy = this.Fx + 1;',
  14336. ' this.Fy = this.Fx + 1;',
  14337. ' this.SetInt(this.GetInt() + 1);',
  14338. ' this.SetInt(this.GetInt() + 1);',
  14339. ' };',
  14340. '});',
  14341. 'this.Obj = null;'
  14342. ]),
  14343. LinesToStr([ // $mod.$main
  14344. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14345. 'if ($mod.Obj.GetInt() === 2);',
  14346. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14347. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14348. ]));
  14349. end;
  14350. procedure TTestModule.TestClass_Property_ClassMethod;
  14351. begin
  14352. StartProgram(false);
  14353. Add([
  14354. 'type',
  14355. ' TObject = class',
  14356. ' class var Fx: longint;',
  14357. ' class var Fy: longint;',
  14358. ' class function GetInt: longint;',
  14359. ' class procedure SetInt(Value: longint);',
  14360. ' end;',
  14361. ' TBird = class',
  14362. ' class procedure DoIt;',
  14363. ' class property IntA: longint read Fx write Fy;',
  14364. ' class property IntB: longint read GetInt write SetInt;',
  14365. ' end;',
  14366. 'class function tobject.getint: longint;',
  14367. 'begin',
  14368. ' result:=fx;',
  14369. 'end;',
  14370. 'class procedure tobject.setint(value: longint);',
  14371. 'begin',
  14372. 'end;',
  14373. 'class procedure tbird.doit;',
  14374. 'begin',
  14375. ' FX:=3;',
  14376. ' IntA:=IntA+1;',
  14377. ' Self.IntA:=Self.IntA+1;',
  14378. ' IntB:=IntB+1;',
  14379. ' Self.IntB:=Self.IntB+1;',
  14380. ' with Self do begin',
  14381. ' FX:=11;',
  14382. ' IntA:=IntA+12;',
  14383. ' IntB:=IntB+13;',
  14384. ' end;',
  14385. 'end;',
  14386. 'var Obj: tbird;',
  14387. 'begin',
  14388. ' tbird.fx:=tbird.fx+1;',
  14389. ' tbird.inta:=tbird.inta+1;',
  14390. ' if tbird.intb=2 then;',
  14391. ' tbird.intb:=tbird.intb+2;',
  14392. ' tbird.setint(tbird.inta);',
  14393. ' obj.inta:=obj.inta+1;',
  14394. ' if obj.intb=2 then;',
  14395. ' obj.intb:=obj.intb+2;',
  14396. ' obj.setint(obj.inta);',
  14397. ' with Tbird do begin',
  14398. ' FX:=FY+1;',
  14399. ' inta:=inta+2;',
  14400. ' intb:=intb+3;',
  14401. ' end;',
  14402. ' with Obj do begin',
  14403. ' FX:=FY+1;',
  14404. ' inta:=inta+2;',
  14405. ' intb:=intb+3;',
  14406. ' end;',
  14407. '']);
  14408. ConvertProgram;
  14409. CheckSource('TestClass_Property_ClassMethod',
  14410. LinesToStr([ // statements
  14411. 'rtl.createClass(this, "TObject", null, function () {',
  14412. ' this.Fx = 0;',
  14413. ' this.Fy = 0;',
  14414. ' this.$init = function () {',
  14415. ' };',
  14416. ' this.$final = function () {',
  14417. ' };',
  14418. ' this.GetInt = function () {',
  14419. ' var Result = 0;',
  14420. ' Result = this.Fx;',
  14421. ' return Result;',
  14422. ' };',
  14423. ' this.SetInt = function (Value) {',
  14424. ' };',
  14425. '});',
  14426. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14427. ' this.DoIt = function () {',
  14428. ' $mod.TObject.Fx = 3;',
  14429. ' $mod.TObject.Fy = this.Fx + 1;',
  14430. ' $mod.TObject.Fy = this.Fx + 1;',
  14431. ' this.SetInt(this.GetInt() + 1);',
  14432. ' this.SetInt(this.GetInt() + 1);',
  14433. ' $mod.TObject.Fx = 11;',
  14434. ' $mod.TObject.Fy = this.Fx + 12;',
  14435. ' this.SetInt(this.GetInt() + 13);',
  14436. ' };',
  14437. '});',
  14438. 'this.Obj = null;'
  14439. ]),
  14440. LinesToStr([ // $mod.$main
  14441. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14442. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14443. 'if ($mod.TBird.GetInt() === 2);',
  14444. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14445. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14446. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14447. 'if ($mod.Obj.$class.GetInt() === 2);',
  14448. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14449. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14450. 'var $with = $mod.TBird;',
  14451. '$mod.TObject.Fx = $with.Fy + 1;',
  14452. '$mod.TObject.Fy = $with.Fx + 2;',
  14453. '$with.SetInt($with.GetInt() + 3);',
  14454. 'var $with1 = $mod.Obj;',
  14455. '$mod.TObject.Fx = $with1.Fy + 1;',
  14456. '$mod.TObject.Fy = $with1.Fx + 2;',
  14457. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14458. '']));
  14459. end;
  14460. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14461. begin
  14462. StartProgram(false);
  14463. Add([
  14464. 'type',
  14465. ' TObject = class',
  14466. ' class function GetInt: longint; static;',
  14467. ' class procedure SetInt(Value: longint); static;',
  14468. ' class function GetItems(Index: word): longint; static;',
  14469. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14470. ' end;',
  14471. ' TBird = class',
  14472. ' class procedure Fly;',
  14473. ' class property IntA: longint read GetInt write SetInt;',
  14474. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14475. ' end;',
  14476. 'class function tobject.getint: longint;',
  14477. 'begin',
  14478. 'end;',
  14479. 'class procedure tobject.setint(value: longint);',
  14480. 'begin',
  14481. 'end;',
  14482. 'class function tobject.GetItems(Index: word): longint;',
  14483. 'begin',
  14484. 'end;',
  14485. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14486. 'begin',
  14487. 'end;',
  14488. 'class procedure tbird.fly;',
  14489. 'var w: longint;',
  14490. 'begin',
  14491. ' inta:=inta+51;',
  14492. ' w:=items[52];',
  14493. ' items[53]:=54;',
  14494. 'end;',
  14495. 'var Obj: tbird;',
  14496. ' i: longint;',
  14497. 'begin',
  14498. ' tbird.inta:=tbird.inta+1;',
  14499. ' i:=tbird.items[2];',
  14500. ' tbird.items[3]:=4;',
  14501. ' obj.inta:=obj.inta+11;',
  14502. ' i:=obj.items[12];',
  14503. ' obj.items[13]:=14;',
  14504. ' with Tbird do begin',
  14505. ' inta:=inta+21;',
  14506. ' i:=items[22];',
  14507. ' items[23]:=24;',
  14508. ' end;',
  14509. ' with Obj do begin',
  14510. ' inta:=inta+31;',
  14511. ' i:=items[32];',
  14512. ' items[33]:=34;',
  14513. ' end;',
  14514. '']);
  14515. ConvertProgram;
  14516. CheckSource('TestClass_Property_ClassMethod',
  14517. LinesToStr([ // statements
  14518. 'rtl.createClass(this, "TObject", null, function () {',
  14519. ' this.$init = function () {',
  14520. ' };',
  14521. ' this.$final = function () {',
  14522. ' };',
  14523. ' this.GetInt = function () {',
  14524. ' var Result = 0;',
  14525. ' return Result;',
  14526. ' };',
  14527. ' this.SetInt = function (Value) {',
  14528. ' };',
  14529. ' this.GetItems = function (Index) {',
  14530. ' var Result = 0;',
  14531. ' return Result;',
  14532. ' };',
  14533. ' this.SetItems = function (Index, Value) {',
  14534. ' };',
  14535. '});',
  14536. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14537. ' this.Fly = function () {',
  14538. ' var w = 0;',
  14539. ' this.SetInt(this.GetInt() + 51);',
  14540. ' w = this.GetItems(52);',
  14541. ' this.SetItems(53, 54);',
  14542. ' };',
  14543. '});',
  14544. 'this.Obj = null;',
  14545. 'this.i = 0;',
  14546. '']),
  14547. LinesToStr([ // $mod.$main
  14548. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14549. '$mod.i = $mod.TObject.GetItems(2);',
  14550. '$mod.TObject.SetItems(3, 4);',
  14551. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14552. '$mod.i = $mod.TObject.GetItems(12);',
  14553. '$mod.TObject.SetItems(13, 14);',
  14554. 'var $with = $mod.TBird;',
  14555. '$with.SetInt($with.GetInt() + 21);',
  14556. '$mod.i = $with.GetItems(22);',
  14557. '$with.SetItems(23, 24);',
  14558. 'var $with1 = $mod.Obj;',
  14559. '$with1.SetInt($with1.GetInt() + 31);',
  14560. '$mod.i = $with1.GetItems(32);',
  14561. '$with1.SetItems(33, 34);',
  14562. '']));
  14563. end;
  14564. procedure TTestModule.TestClass_Property_Indexed;
  14565. begin
  14566. StartProgram(false);
  14567. Add([
  14568. 'type',
  14569. ' TObject = class',
  14570. ' FItems: array of longint;',
  14571. ' function GetItems(Index: longint): longint;',
  14572. ' procedure SetItems(Index: longint; Value: longint);',
  14573. ' procedure DoIt;',
  14574. ' property Items[Index: longint]: longint read getitems write setitems;',
  14575. ' end;',
  14576. 'function tobject.getitems(index: longint): longint;',
  14577. 'begin',
  14578. ' Result:=fitems[index];',
  14579. 'end;',
  14580. 'procedure tobject.setitems(index: longint; value: longint);',
  14581. 'begin',
  14582. ' fitems[index]:=value;',
  14583. 'end;',
  14584. 'procedure tobject.doit;',
  14585. 'begin',
  14586. ' items[1]:=2;',
  14587. ' items[3]:=items[4];',
  14588. ' self.items[5]:=self.items[6];',
  14589. ' items[items[7]]:=items[items[8]];',
  14590. 'end;',
  14591. 'var Obj: tobject;',
  14592. 'begin',
  14593. ' obj.Items[11]:=obj.Items[12];',
  14594. '']);
  14595. ConvertProgram;
  14596. CheckSource('TestClass_Property_Indexed',
  14597. LinesToStr([ // statements
  14598. 'rtl.createClass(this, "TObject", null, function () {',
  14599. ' this.$init = function () {',
  14600. ' this.FItems = [];',
  14601. ' };',
  14602. ' this.$final = function () {',
  14603. ' this.FItems = undefined;',
  14604. ' };',
  14605. ' this.GetItems = function (Index) {',
  14606. ' var Result = 0;',
  14607. ' Result = this.FItems[Index];',
  14608. ' return Result;',
  14609. ' };',
  14610. ' this.SetItems = function (Index, Value) {',
  14611. ' this.FItems[Index] = Value;',
  14612. ' };',
  14613. ' this.DoIt = function () {',
  14614. ' this.SetItems(1, 2);',
  14615. ' this.SetItems(3,this.GetItems(4));',
  14616. ' this.SetItems(5,this.GetItems(6));',
  14617. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14618. ' };',
  14619. '});',
  14620. 'this.Obj = null;'
  14621. ]),
  14622. LinesToStr([ // $mod.$main
  14623. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  14624. ]));
  14625. end;
  14626. procedure TTestModule.TestClass_Property_IndexSpec;
  14627. begin
  14628. StartProgram(false);
  14629. Add([
  14630. 'type',
  14631. ' TEnum = (red, blue);',
  14632. ' TObject = class',
  14633. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  14634. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  14635. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  14636. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  14637. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  14638. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  14639. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  14640. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  14641. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  14642. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  14643. ' end;',
  14644. 'procedure DoIt(b: boolean); begin end;',
  14645. 'var',
  14646. ' o: TObject;',
  14647. 'begin',
  14648. ' o.B1:=o.B1;',
  14649. ' o.B2:=o.B2;',
  14650. ' o.B3:=o.B3;',
  14651. ' o.I1[''a'']:=o.I1[''b''];',
  14652. ' doit(o.b1);',
  14653. ' doit(o.b2);',
  14654. ' doit(o.i1[''c'']);',
  14655. '']);
  14656. ConvertProgram;
  14657. CheckSource('TestClass_Property_IndexSpec',
  14658. LinesToStr([ // statements
  14659. 'this.TEnum = {',
  14660. ' "0": "red",',
  14661. ' red: 0,',
  14662. ' "1": "blue",',
  14663. ' blue: 1',
  14664. '};',
  14665. 'rtl.createClass(this, "TObject", null, function () {',
  14666. ' this.$init = function () {',
  14667. ' };',
  14668. ' this.$final = function () {',
  14669. ' };',
  14670. '});',
  14671. 'this.DoIt = function (b) {',
  14672. '};',
  14673. 'this.o = null;',
  14674. '']),
  14675. LinesToStr([ // $mod.$main
  14676. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  14677. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  14678. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  14679. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  14680. '$mod.DoIt($mod.o.GetIntBool(1));',
  14681. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  14682. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  14683. '']));
  14684. end;
  14685. procedure TTestModule.TestClass_PropertyOfTypeArray;
  14686. begin
  14687. StartProgram(false);
  14688. Add('type');
  14689. Add(' TArray = array of longint;');
  14690. Add(' TObject = class');
  14691. Add(' FItems: TArray;');
  14692. Add(' function GetItems: tarray;');
  14693. Add(' procedure SetItems(Value: tarray);');
  14694. Add(' property Items: tarray read getitems write setitems;');
  14695. Add(' procedure SetNumbers(const Value: tarray);');
  14696. Add(' property Numbers: tarray write setnumbers;');
  14697. Add(' end;');
  14698. Add('function tobject.getitems: tarray;');
  14699. Add('begin');
  14700. Add(' Result:=fitems;');
  14701. Add('end;');
  14702. Add('procedure tobject.setitems(value: tarray);');
  14703. Add('begin');
  14704. Add(' fitems:=value;');
  14705. Add(' fitems:=nil;');
  14706. Add(' Items:=nil;');
  14707. Add(' Items:=Items;');
  14708. Add(' Items[1]:=2;');
  14709. Add(' fitems[3]:=Items[4];');
  14710. Add(' Items[5]:=Items[6];');
  14711. Add(' Self.Items[7]:=8;');
  14712. Add(' Self.Items[9]:=Self.Items[10];');
  14713. Add(' Items[Items[11]]:=Items[Items[12]];');
  14714. Add('end;');
  14715. Add('procedure tobject.SetNumbers(const Value: tarray);');
  14716. Add('begin;');
  14717. Add(' Numbers:=nil;');
  14718. Add(' Numbers:=Value;');
  14719. Add(' Self.Numbers:=Value;');
  14720. Add('end;');
  14721. Add('var Obj: tobject;');
  14722. Add('begin');
  14723. Add(' obj.items:=nil;');
  14724. Add(' obj.items:=obj.items;');
  14725. Add(' obj.items[11]:=obj.items[12];');
  14726. ConvertProgram;
  14727. CheckSource('TestClass_PropertyOfTypeArray',
  14728. LinesToStr([ // statements
  14729. 'rtl.createClass(this, "TObject", null, function () {',
  14730. ' this.$init = function () {',
  14731. ' this.FItems = [];',
  14732. ' };',
  14733. ' this.$final = function () {',
  14734. ' this.FItems = undefined;',
  14735. ' };',
  14736. ' this.GetItems = function () {',
  14737. ' var Result = [];',
  14738. ' Result = rtl.arrayRef(this.FItems);',
  14739. ' return Result;',
  14740. ' };',
  14741. ' this.SetItems = function (Value) {',
  14742. ' this.FItems = rtl.arrayRef(Value);',
  14743. ' this.FItems = [];',
  14744. ' this.SetItems([]);',
  14745. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  14746. ' this.GetItems()[1] = 2;',
  14747. ' this.FItems[3] = this.GetItems()[4];',
  14748. ' this.GetItems()[5] = this.GetItems()[6];',
  14749. ' this.GetItems()[7] = 8;',
  14750. ' this.GetItems()[9] = this.GetItems()[10];',
  14751. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  14752. ' };',
  14753. ' this.SetNumbers = function (Value) {',
  14754. ' this.SetNumbers([]);',
  14755. ' this.SetNumbers(Value);',
  14756. ' this.SetNumbers(Value);',
  14757. ' };',
  14758. '});',
  14759. 'this.Obj = null;'
  14760. ]),
  14761. LinesToStr([ // $mod.$main
  14762. '$mod.Obj.SetItems([]);',
  14763. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  14764. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  14765. ]));
  14766. end;
  14767. procedure TTestModule.TestClass_PropertyDefault;
  14768. begin
  14769. StartProgram(false);
  14770. Add([
  14771. 'type',
  14772. ' TArray = array of longint;',
  14773. ' TObject = class',
  14774. ' end;',
  14775. ' TBird = class',
  14776. ' FItems: TArray;',
  14777. ' function GetItems(Index: longint): longint;',
  14778. ' procedure SetItems(Index, Value: longint);',
  14779. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  14780. ' end;',
  14781. 'function TBird.getitems(index: longint): longint;',
  14782. 'begin',
  14783. 'end;',
  14784. 'procedure TBird.setitems(index, value: longint);',
  14785. 'begin',
  14786. ' Self[1]:=2;',
  14787. ' Self[3]:=Self[index];',
  14788. ' Self[index]:=Self[Self[value]];',
  14789. ' Self[Self[4]]:=value;',
  14790. 'end;',
  14791. 'var',
  14792. ' Bird: TBird;',
  14793. ' Obj: TObject;',
  14794. 'begin',
  14795. ' bird[11]:=12;',
  14796. ' bird[13]:=bird[14];',
  14797. ' bird[Bird[15]]:=bird[Bird[15]];',
  14798. ' TBird(obj)[16]:=TBird(obj)[17];',
  14799. ' (obj as tbird)[18]:=19;',
  14800. '']);
  14801. ConvertProgram;
  14802. CheckSource('TestClass_PropertyDefault',
  14803. LinesToStr([ // statements
  14804. 'rtl.createClass(this, "TObject", null, function () {',
  14805. ' this.$init = function () {',
  14806. ' };',
  14807. ' this.$final = function () {',
  14808. ' };',
  14809. '});',
  14810. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14811. ' this.$init = function () {',
  14812. ' $mod.TObject.$init.call(this);',
  14813. ' this.FItems = [];',
  14814. ' };',
  14815. ' this.$final = function () {',
  14816. ' this.FItems = undefined;',
  14817. ' $mod.TObject.$final.call(this);',
  14818. ' };',
  14819. ' this.GetItems = function (Index) {',
  14820. ' var Result = 0;',
  14821. ' return Result;',
  14822. ' };',
  14823. ' this.SetItems = function (Index, Value) {',
  14824. ' this.SetItems(1, 2);',
  14825. ' this.SetItems(3, this.GetItems(Index));',
  14826. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  14827. ' this.SetItems(this.GetItems(4), Value);',
  14828. ' };',
  14829. '});',
  14830. 'this.Bird = null;',
  14831. 'this.Obj = null;',
  14832. '']),
  14833. LinesToStr([ // $mod.$main
  14834. '$mod.Bird.SetItems(11, 12);',
  14835. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  14836. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  14837. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  14838. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  14839. '']));
  14840. end;
  14841. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  14842. begin
  14843. StartProgram(false);
  14844. Add([
  14845. 'type',
  14846. ' TObject = class end;',
  14847. ' TAlphaList = class',
  14848. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  14849. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  14850. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  14851. ' end;',
  14852. ' TBetaList = class',
  14853. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  14854. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  14855. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  14856. ' end;',
  14857. ' TBird = class',
  14858. ' procedure DoIt;',
  14859. ' end;',
  14860. 'procedure TBird.DoIt;',
  14861. 'var',
  14862. ' List: TAlphaList;',
  14863. 'begin',
  14864. ' if TBetaList(List[true])[3]=nil then ;',
  14865. ' TBetaList(List[false])[5]:=nil;',
  14866. 'end;',
  14867. 'var',
  14868. ' List: TAlphaList;',
  14869. 'begin',
  14870. ' if TBetaList(List[true])[3]=nil then ;',
  14871. ' TBetaList(List[false])[5]:=nil;',
  14872. '']);
  14873. ConvertProgram;
  14874. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  14875. LinesToStr([ // statements
  14876. 'rtl.createClass(this, "TObject", null, function () {',
  14877. ' this.$init = function () {',
  14878. ' };',
  14879. ' this.$final = function () {',
  14880. ' };',
  14881. '});',
  14882. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  14883. '});',
  14884. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  14885. '});',
  14886. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14887. ' this.DoIt = function () {',
  14888. ' var List = null;',
  14889. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  14890. ' List.GetAlphas(false).SetBetas(5, null);',
  14891. ' };',
  14892. '});',
  14893. 'this.List = null;',
  14894. '']),
  14895. LinesToStr([ // $mod.$main
  14896. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  14897. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  14898. '']));
  14899. end;
  14900. procedure TTestModule.TestClass_PropertyOverride;
  14901. begin
  14902. StartProgram(false);
  14903. Add('type');
  14904. Add(' integer = longint;');
  14905. Add(' TObject = class');
  14906. Add(' FItem: integer;');
  14907. Add(' function GetItem: integer; external name ''GetItem'';');
  14908. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  14909. Add(' property Item: integer read getitem write setitem;');
  14910. Add(' end;');
  14911. Add(' TCar = class');
  14912. Add(' FBag: integer;');
  14913. Add(' function GetBag: integer; external name ''GetBag'';');
  14914. Add(' property Item read getbag;');
  14915. Add(' end;');
  14916. Add('var');
  14917. Add(' Obj: tobject;');
  14918. Add(' Car: tcar;');
  14919. Add('begin');
  14920. Add(' Obj.Item:=Obj.Item;');
  14921. Add(' Car.Item:=Car.Item;');
  14922. ConvertProgram;
  14923. CheckSource('TestClass_PropertyOverride',
  14924. LinesToStr([ // statements
  14925. 'rtl.createClass(this, "TObject", null, function () {',
  14926. ' this.$init = function () {',
  14927. ' this.FItem = 0;',
  14928. ' };',
  14929. ' this.$final = function () {',
  14930. ' };',
  14931. '});',
  14932. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14933. ' this.$init = function () {',
  14934. ' $mod.TObject.$init.call(this);',
  14935. ' this.FBag = 0;',
  14936. ' };',
  14937. '});',
  14938. 'this.Obj = null;',
  14939. 'this.Car = null;',
  14940. '']),
  14941. LinesToStr([ // $mod.$main
  14942. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  14943. '$mod.Car.SetItem($mod.Car.GetBag());',
  14944. '']));
  14945. end;
  14946. procedure TTestModule.TestClass_PropertyIncVisibility;
  14947. begin
  14948. AddModuleWithIntfImplSrc('unit1.pp',
  14949. LinesToStr([
  14950. 'type',
  14951. ' TNumber = longint;',
  14952. ' TInteger = longint;',
  14953. ' TObject = class',
  14954. ' private',
  14955. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  14956. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  14957. ' protected',
  14958. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  14959. ' end;']),
  14960. LinesToStr([
  14961. '']));
  14962. StartProgram(true);
  14963. Add([
  14964. 'uses unit1;',
  14965. 'type',
  14966. ' TBird = class',
  14967. ' public',
  14968. ' property Items;',
  14969. ' end;',
  14970. 'procedure DoIt(i: TInteger);',
  14971. 'begin',
  14972. 'end;',
  14973. 'var b: TBird;',
  14974. 'begin',
  14975. ' b.Items[1]:=2;',
  14976. ' b.Items[3]:=b.Items[4];',
  14977. ' DoIt(b.Items[5]);',
  14978. '']);
  14979. ConvertProgram;
  14980. CheckSource('TestClass_PropertyIncVisibility',
  14981. LinesToStr([ // statements
  14982. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  14983. '});',
  14984. 'this.DoIt = function (i) {',
  14985. '};',
  14986. 'this.b = null;'
  14987. ]),
  14988. LinesToStr([ // $mod.$main
  14989. '$mod.b.SetItems(1, 2);',
  14990. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  14991. '$mod.DoIt($mod.b.GetItems(5));'
  14992. ]));
  14993. end;
  14994. procedure TTestModule.TestClass_Assigned;
  14995. begin
  14996. StartProgram(false);
  14997. Add('type');
  14998. Add(' TObject = class');
  14999. Add(' end;');
  15000. Add('var');
  15001. Add(' Obj: tobject;');
  15002. Add(' b: boolean;');
  15003. Add('begin');
  15004. Add(' if Assigned(obj) then ;');
  15005. Add(' b:=Assigned(obj) or false;');
  15006. ConvertProgram;
  15007. CheckSource('TestClass_Assigned',
  15008. LinesToStr([ // statements
  15009. 'rtl.createClass(this, "TObject", null, function () {',
  15010. ' this.$init = function () {',
  15011. ' };',
  15012. ' this.$final = function () {',
  15013. ' };',
  15014. '});',
  15015. 'this.Obj = null;',
  15016. 'this.b = false;'
  15017. ]),
  15018. LinesToStr([ // $mod.$main
  15019. 'if ($mod.Obj != null);',
  15020. '$mod.b = ($mod.Obj != null) || false;'
  15021. ]));
  15022. end;
  15023. procedure TTestModule.TestClass_WithClassDoCreate;
  15024. begin
  15025. StartProgram(false);
  15026. Add('type');
  15027. Add(' TObject = class');
  15028. Add(' aBool: boolean;');
  15029. Add(' Arr: array of boolean;');
  15030. Add(' constructor Create;');
  15031. Add(' end;');
  15032. Add('constructor TObject.Create; begin end;');
  15033. Add('var');
  15034. Add(' Obj: tobject;');
  15035. Add(' b: boolean;');
  15036. Add('begin');
  15037. Add(' with tobject.create do begin');
  15038. Add(' b:=abool;');
  15039. Add(' abool:=b;');
  15040. Add(' b:=arr[1];');
  15041. Add(' arr[2]:=b;');
  15042. Add(' end;');
  15043. Add(' with tobject do');
  15044. Add(' obj:=create;');
  15045. Add(' with obj do begin');
  15046. Add(' create;');
  15047. Add(' b:=abool;');
  15048. Add(' abool:=b;');
  15049. Add(' b:=arr[3];');
  15050. Add(' arr[4]:=b;');
  15051. Add(' end;');
  15052. ConvertProgram;
  15053. CheckSource('TestClass_WithClassDoCreate',
  15054. LinesToStr([ // statements
  15055. 'rtl.createClass(this, "TObject", null, function () {',
  15056. ' this.$init = function () {',
  15057. ' this.aBool = false;',
  15058. ' this.Arr = [];',
  15059. ' };',
  15060. ' this.$final = function () {',
  15061. ' this.Arr = undefined;',
  15062. ' };',
  15063. ' this.Create = function () {',
  15064. ' return this;',
  15065. ' };',
  15066. '});',
  15067. 'this.Obj = null;',
  15068. 'this.b = false;'
  15069. ]),
  15070. LinesToStr([ // $mod.$main
  15071. 'var $with = $mod.TObject.$create("Create");',
  15072. '$mod.b = $with.aBool;',
  15073. '$with.aBool = $mod.b;',
  15074. '$mod.b = $with.Arr[1];',
  15075. '$with.Arr[2] = $mod.b;',
  15076. 'var $with1 = $mod.TObject;',
  15077. '$mod.Obj = $with1.$create("Create");',
  15078. 'var $with2 = $mod.Obj;',
  15079. '$with2.Create();',
  15080. '$mod.b = $with2.aBool;',
  15081. '$with2.aBool = $mod.b;',
  15082. '$mod.b = $with2.Arr[3];',
  15083. '$with2.Arr[4] = $mod.b;',
  15084. '']));
  15085. end;
  15086. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15087. begin
  15088. StartProgram(false);
  15089. Add('type');
  15090. Add(' TObject = class');
  15091. Add(' FInt: longint;');
  15092. Add(' constructor Create;');
  15093. Add(' function GetSize: longint;');
  15094. Add(' procedure SetSize(Value: longint);');
  15095. Add(' property Int: longint read FInt write FInt;');
  15096. Add(' property Size: longint read GetSize write SetSize;');
  15097. Add(' end;');
  15098. Add('constructor TObject.Create; begin end;');
  15099. Add('function TObject.GetSize: longint; begin; end;');
  15100. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15101. Add('var');
  15102. Add(' Obj: tobject;');
  15103. Add(' i: longint;');
  15104. Add('begin');
  15105. Add(' with TObject.Create do begin');
  15106. Add(' i:=int;');
  15107. Add(' int:=i;');
  15108. Add(' i:=size;');
  15109. Add(' size:=i;');
  15110. Add(' end;');
  15111. Add(' with obj do begin');
  15112. Add(' i:=int;');
  15113. Add(' int:=i;');
  15114. Add(' i:=size;');
  15115. Add(' size:=i;');
  15116. Add(' end;');
  15117. ConvertProgram;
  15118. CheckSource('TestClass_WithClassInstDoProperty',
  15119. LinesToStr([ // statements
  15120. 'rtl.createClass(this, "TObject", null, function () {',
  15121. ' this.$init = function () {',
  15122. ' this.FInt = 0;',
  15123. ' };',
  15124. ' this.$final = function () {',
  15125. ' };',
  15126. ' this.Create = function () {',
  15127. ' return this;',
  15128. ' };',
  15129. ' this.GetSize = function () {',
  15130. ' var Result = 0;',
  15131. ' return Result;',
  15132. ' };',
  15133. ' this.SetSize = function (Value) {',
  15134. ' };',
  15135. '});',
  15136. 'this.Obj = null;',
  15137. 'this.i = 0;'
  15138. ]),
  15139. LinesToStr([ // $mod.$main
  15140. 'var $with = $mod.TObject.$create("Create");',
  15141. '$mod.i = $with.FInt;',
  15142. '$with.FInt = $mod.i;',
  15143. '$mod.i = $with.GetSize();',
  15144. '$with.SetSize($mod.i);',
  15145. 'var $with1 = $mod.Obj;',
  15146. '$mod.i = $with1.FInt;',
  15147. '$with1.FInt = $mod.i;',
  15148. '$mod.i = $with1.GetSize();',
  15149. '$with1.SetSize($mod.i);',
  15150. '']));
  15151. end;
  15152. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15153. begin
  15154. StartProgram(false);
  15155. Add('type');
  15156. Add(' TObject = class');
  15157. Add(' constructor Create;');
  15158. Add(' function GetItems(Index: longint): longint;');
  15159. Add(' procedure SetItems(Index, Value: longint);');
  15160. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15161. Add(' end;');
  15162. Add('constructor TObject.Create; begin end;');
  15163. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15164. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15165. Add('var');
  15166. Add(' Obj: tobject;');
  15167. Add(' i: longint;');
  15168. Add('begin');
  15169. Add(' with TObject.Create do begin');
  15170. Add(' i:=Items[1];');
  15171. Add(' Items[2]:=i;');
  15172. Add(' end;');
  15173. Add(' with obj do begin');
  15174. Add(' i:=Items[3];');
  15175. Add(' Items[4]:=i;');
  15176. Add(' end;');
  15177. ConvertProgram;
  15178. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15179. LinesToStr([ // statements
  15180. 'rtl.createClass(this, "TObject", null, function () {',
  15181. ' this.$init = function () {',
  15182. ' };',
  15183. ' this.$final = function () {',
  15184. ' };',
  15185. ' this.Create = function () {',
  15186. ' return this;',
  15187. ' };',
  15188. ' this.GetItems = function (Index) {',
  15189. ' var Result = 0;',
  15190. ' return Result;',
  15191. ' };',
  15192. ' this.SetItems = function (Index, Value) {',
  15193. ' };',
  15194. '});',
  15195. 'this.Obj = null;',
  15196. 'this.i = 0;'
  15197. ]),
  15198. LinesToStr([ // $mod.$main
  15199. 'var $with = $mod.TObject.$create("Create");',
  15200. '$mod.i = $with.GetItems(1);',
  15201. '$with.SetItems(2, $mod.i);',
  15202. 'var $with1 = $mod.Obj;',
  15203. '$mod.i = $with1.GetItems(3);',
  15204. '$with1.SetItems(4, $mod.i);',
  15205. '']));
  15206. end;
  15207. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15208. begin
  15209. StartProgram(false);
  15210. Add('type');
  15211. Add(' TObject = class');
  15212. Add(' constructor Create;');
  15213. Add(' function GetSize: longint;');
  15214. Add(' procedure SetSize(Value: longint);');
  15215. Add(' end;');
  15216. Add('constructor TObject.Create; begin end;');
  15217. Add('function TObject.GetSize: longint; begin; end;');
  15218. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15219. Add('var');
  15220. Add(' Obj: tobject;');
  15221. Add(' i: longint;');
  15222. Add('begin');
  15223. Add(' with TObject.Create do begin');
  15224. Add(' i:=GetSize;');
  15225. Add(' i:=GetSize();');
  15226. Add(' SetSize(i);');
  15227. Add(' end;');
  15228. Add(' with obj do begin');
  15229. Add(' i:=GetSize;');
  15230. Add(' i:=GetSize();');
  15231. Add(' SetSize(i);');
  15232. Add(' end;');
  15233. ConvertProgram;
  15234. CheckSource('TestClass_WithClassInstDoFunc',
  15235. LinesToStr([ // statements
  15236. 'rtl.createClass(this, "TObject", null, function () {',
  15237. ' this.$init = function () {',
  15238. ' };',
  15239. ' this.$final = function () {',
  15240. ' };',
  15241. ' this.Create = function () {',
  15242. ' return this;',
  15243. ' };',
  15244. ' this.GetSize = function () {',
  15245. ' var Result = 0;',
  15246. ' return Result;',
  15247. ' };',
  15248. ' this.SetSize = function (Value) {',
  15249. ' };',
  15250. '});',
  15251. 'this.Obj = null;',
  15252. 'this.i = 0;'
  15253. ]),
  15254. LinesToStr([ // $mod.$main
  15255. 'var $with = $mod.TObject.$create("Create");',
  15256. '$mod.i = $with.GetSize();',
  15257. '$mod.i = $with.GetSize();',
  15258. '$with.SetSize($mod.i);',
  15259. 'var $with1 = $mod.Obj;',
  15260. '$mod.i = $with1.GetSize();',
  15261. '$mod.i = $with1.GetSize();',
  15262. '$with1.SetSize($mod.i);',
  15263. '']));
  15264. end;
  15265. procedure TTestModule.TestClass_TypeCast;
  15266. begin
  15267. StartProgram(false);
  15268. Add('type');
  15269. Add(' TObject = class');
  15270. Add(' Next: TObject;');
  15271. Add(' constructor Create;');
  15272. Add(' end;');
  15273. Add(' TControl = class(TObject)');
  15274. Add(' Arr: array of TObject;');
  15275. Add(' function GetIt(vI: longint = 0): TObject;');
  15276. Add(' end;');
  15277. Add('constructor tobject.create; begin end;');
  15278. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15279. Add('var');
  15280. Add(' Obj: tobject;');
  15281. Add('begin');
  15282. Add(' obj:=tcontrol(obj).next;');
  15283. Add(' tcontrol(obj):=nil;');
  15284. Add(' obj:=tcontrol(obj);');
  15285. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15286. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15287. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15288. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15289. Add(' obj:=tcontrol(nil);');
  15290. ConvertProgram;
  15291. CheckSource('TestClass_TypeCast',
  15292. LinesToStr([ // statements
  15293. 'rtl.createClass(this, "TObject", null, function () {',
  15294. ' this.$init = function () {',
  15295. ' this.Next = null;',
  15296. ' };',
  15297. ' this.$final = function () {',
  15298. ' this.Next = undefined;',
  15299. ' };',
  15300. ' this.Create = function () {',
  15301. ' return this;',
  15302. ' };',
  15303. '});',
  15304. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15305. ' this.$init = function () {',
  15306. ' $mod.TObject.$init.call(this);',
  15307. ' this.Arr = [];',
  15308. ' };',
  15309. ' this.$final = function () {',
  15310. ' this.Arr = undefined;',
  15311. ' $mod.TObject.$final.call(this);',
  15312. ' };',
  15313. ' this.GetIt = function (vI) {',
  15314. ' var Result = null;',
  15315. ' return Result;',
  15316. ' };',
  15317. '});',
  15318. 'this.Obj = null;'
  15319. ]),
  15320. LinesToStr([ // $mod.$main
  15321. '$mod.Obj = $mod.Obj.Next;',
  15322. '$mod.Obj = null;',
  15323. '$mod.Obj = $mod.Obj;',
  15324. '$mod.Obj = $mod.Obj.GetIt(0);',
  15325. '$mod.Obj = $mod.Obj.GetIt(0);',
  15326. '$mod.Obj = $mod.Obj.GetIt(1);',
  15327. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15328. '$mod.Obj = null;',
  15329. '']));
  15330. end;
  15331. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15332. begin
  15333. StartProgram(false);
  15334. Add('type');
  15335. Add(' TObject = class end;');
  15336. Add('procedure ProcA(var A);');
  15337. Add('begin');
  15338. Add(' TObject(A):=nil;');
  15339. Add(' TObject(A):=TObject(A);');
  15340. Add(' if TObject(A)=nil then ;');
  15341. Add(' if nil=TObject(A) then ;');
  15342. Add('end;');
  15343. Add('procedure ProcB(out A);');
  15344. Add('begin');
  15345. Add(' TObject(A):=nil;');
  15346. Add(' TObject(A):=TObject(A);');
  15347. Add(' if TObject(A)=nil then ;');
  15348. Add(' if nil=TObject(A) then ;');
  15349. Add('end;');
  15350. Add('procedure ProcC(const A);');
  15351. Add('begin');
  15352. Add(' if TObject(A)=nil then ;');
  15353. Add(' if nil=TObject(A) then ;');
  15354. Add('end;');
  15355. Add('var o: TObject;');
  15356. Add('begin');
  15357. Add(' ProcA(o);');
  15358. Add(' ProcB(o);');
  15359. Add(' ProcC(o);');
  15360. ConvertProgram;
  15361. CheckSource('TestClass_TypeCastUntypedParam',
  15362. LinesToStr([ // statements
  15363. 'rtl.createClass(this, "TObject", null, function () {',
  15364. ' this.$init = function () {',
  15365. ' };',
  15366. ' this.$final = function () {',
  15367. ' };',
  15368. '});',
  15369. 'this.ProcA = function (A) {',
  15370. ' A.set(null);',
  15371. ' A.set(A.get());',
  15372. ' if (A.get() === null);',
  15373. ' if (null === A.get());',
  15374. '};',
  15375. 'this.ProcB = function (A) {',
  15376. ' A.set(null);',
  15377. ' A.set(A.get());',
  15378. ' if (A.get() === null);',
  15379. ' if (null === A.get());',
  15380. '};',
  15381. 'this.ProcC = function (A) {',
  15382. ' if (A === null);',
  15383. ' if (null === A);',
  15384. '};',
  15385. 'this.o = null;',
  15386. '']),
  15387. LinesToStr([ // $mod.$main
  15388. '$mod.ProcA({',
  15389. ' p: $mod,',
  15390. ' get: function () {',
  15391. ' return this.p.o;',
  15392. ' },',
  15393. ' set: function (v) {',
  15394. ' this.p.o = v;',
  15395. ' }',
  15396. '});',
  15397. '$mod.ProcB({',
  15398. ' p: $mod,',
  15399. ' get: function () {',
  15400. ' return this.p.o;',
  15401. ' },',
  15402. ' set: function (v) {',
  15403. ' this.p.o = v;',
  15404. ' }',
  15405. '});',
  15406. '$mod.ProcC($mod.o);',
  15407. '']));
  15408. end;
  15409. procedure TTestModule.TestClass_Overloads;
  15410. begin
  15411. StartProgram(false);
  15412. Add('type');
  15413. Add(' TObject = class');
  15414. Add(' procedure DoIt;');
  15415. Add(' procedure DoIt(vI: longint);');
  15416. Add(' end;');
  15417. Add('procedure TObject.DoIt;');
  15418. Add('begin');
  15419. Add(' DoIt;');
  15420. Add(' DoIt(1);');
  15421. Add('end;');
  15422. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15423. Add('begin');
  15424. ConvertProgram;
  15425. CheckSource('TestClass_Overloads',
  15426. LinesToStr([ // statements
  15427. 'rtl.createClass(this, "TObject", null, function () {',
  15428. ' this.$init = function () {',
  15429. ' };',
  15430. ' this.$final = function () {',
  15431. ' };',
  15432. ' this.DoIt = function () {',
  15433. ' this.DoIt();',
  15434. ' this.DoIt$1(1);',
  15435. ' };',
  15436. ' this.DoIt$1 = function (vI) {',
  15437. ' };',
  15438. '});',
  15439. '']),
  15440. LinesToStr([ // $mod.$main
  15441. '']));
  15442. end;
  15443. procedure TTestModule.TestClass_OverloadsAncestor;
  15444. begin
  15445. StartProgram(false);
  15446. Add('type');
  15447. Add(' TObject = class;');
  15448. Add(' TObject = class');
  15449. Add(' procedure DoIt(vA: longint);');
  15450. Add(' procedure DoIt(vA, vB: longint);');
  15451. Add(' end;');
  15452. Add(' TCar = class;');
  15453. Add(' TCar = class');
  15454. Add(' procedure DoIt(vA: longint);');
  15455. Add(' procedure DoIt(vA, vB: longint);');
  15456. Add(' end;');
  15457. Add('procedure tobject.doit(va: longint);');
  15458. Add('begin');
  15459. Add(' doit(1);');
  15460. Add(' doit(1,2);');
  15461. Add('end;');
  15462. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15463. Add('procedure tcar.doit(va: longint);');
  15464. Add('begin');
  15465. Add(' doit(1);');
  15466. Add(' doit(1,2);');
  15467. Add(' inherited doit(1);');
  15468. Add(' inherited doit(1,2);');
  15469. Add('end;');
  15470. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15471. Add('begin');
  15472. ConvertProgram;
  15473. CheckSource('TestClass_OverloadsAncestor',
  15474. LinesToStr([ // statements
  15475. 'rtl.createClass(this, "TObject", null, function () {',
  15476. ' this.$init = function () {',
  15477. ' };',
  15478. ' this.$final = function () {',
  15479. ' };',
  15480. ' this.DoIt = function (vA) {',
  15481. ' this.DoIt(1);',
  15482. ' this.DoIt$1(1,2);',
  15483. ' };',
  15484. ' this.DoIt$1 = function (vA, vB) {',
  15485. ' };',
  15486. '});',
  15487. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15488. ' this.DoIt$2 = function (vA) {',
  15489. ' this.DoIt$2(1);',
  15490. ' this.DoIt$3(1, 2);',
  15491. ' $mod.TObject.DoIt.call(this, 1);',
  15492. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15493. ' };',
  15494. ' this.DoIt$3 = function (vA, vB) {',
  15495. ' };',
  15496. '});',
  15497. '']),
  15498. LinesToStr([ // $mod.$main
  15499. '']));
  15500. end;
  15501. procedure TTestModule.TestClass_OverloadConstructor;
  15502. begin
  15503. StartProgram(false);
  15504. Add('type');
  15505. Add(' TObject = class');
  15506. Add(' constructor Create(vA: longint);');
  15507. Add(' constructor Create(vA, vB: longint);');
  15508. Add(' end;');
  15509. Add(' TCar = class');
  15510. Add(' constructor Create(vA: longint);');
  15511. Add(' constructor Create(vA, vB: longint);');
  15512. Add(' end;');
  15513. Add('constructor tobject.create(va: longint);');
  15514. Add('begin');
  15515. Add(' create(1);');
  15516. Add(' create(1,2);');
  15517. Add('end;');
  15518. Add('constructor tobject.create(va, vb: longint); begin end;');
  15519. Add('constructor tcar.create(va: longint);');
  15520. Add('begin');
  15521. Add(' create(1);');
  15522. Add(' create(1,2);');
  15523. Add(' inherited create(1);');
  15524. Add(' inherited create(1,2);');
  15525. Add('end;');
  15526. Add('constructor tcar.create(va, vb: longint); begin end;');
  15527. Add('begin');
  15528. Add(' tobject.create(1);');
  15529. Add(' tobject.create(1,2);');
  15530. Add(' tcar.create(1);');
  15531. Add(' tcar.create(1,2);');
  15532. ConvertProgram;
  15533. CheckSource('TestClass_OverloadConstructor',
  15534. LinesToStr([ // statements
  15535. 'rtl.createClass(this, "TObject", null, function () {',
  15536. ' this.$init = function () {',
  15537. ' };',
  15538. ' this.$final = function () {',
  15539. ' };',
  15540. ' this.Create = function (vA) {',
  15541. ' this.Create(1);',
  15542. ' this.Create$1(1,2);',
  15543. ' return this;',
  15544. ' };',
  15545. ' this.Create$1 = function (vA, vB) {',
  15546. ' return this;',
  15547. ' };',
  15548. '});',
  15549. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15550. ' this.Create$2 = function (vA) {',
  15551. ' this.Create$2(1);',
  15552. ' this.Create$3(1, 2);',
  15553. ' $mod.TObject.Create.call(this, 1);',
  15554. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15555. ' return this;',
  15556. ' };',
  15557. ' this.Create$3 = function (vA, vB) {',
  15558. ' return this;',
  15559. ' };',
  15560. '});',
  15561. '']),
  15562. LinesToStr([ // $mod.$main
  15563. '$mod.TObject.$create("Create", [1]);',
  15564. '$mod.TObject.$create("Create$1", [1, 2]);',
  15565. '$mod.TCar.$create("Create$2", [1]);',
  15566. '$mod.TCar.$create("Create$3", [1, 2]);',
  15567. '']));
  15568. end;
  15569. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15570. begin
  15571. StartProgram(false);
  15572. Add([
  15573. '{$mode delphi}',
  15574. 'type',
  15575. ' TObject = class end;',
  15576. ' TBird = class',
  15577. ' function {#a}GetValue: longint; overload; virtual;',
  15578. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15579. ' end;',
  15580. ' TEagle = class(TBird)',
  15581. ' function {#c}GetValue: longint; overload; override;',
  15582. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15583. ' end;',
  15584. 'function TBird.GetValue: longint;',
  15585. 'begin',
  15586. ' if 3={@a}GetValue then ;',
  15587. ' if 4={@b}GetValue(5) then ;',
  15588. 'end;',
  15589. 'function TBird.GetValue(AValue: longint): longint;',
  15590. 'begin',
  15591. 'end;',
  15592. 'function TEagle.GetValue: longint;',
  15593. 'begin',
  15594. ' if 13={@c}GetValue then ;',
  15595. ' if 14={@d}GetValue(15) then ;',
  15596. ' if 15=inherited {@a}GetValue then ;',
  15597. ' if 16=inherited {@b}GetValue(17) then ;',
  15598. 'end;',
  15599. 'function TEagle.GetValue(AValue: longint): longint;',
  15600. 'begin',
  15601. 'end;',
  15602. 'var',
  15603. ' e: TEagle;',
  15604. 'begin',
  15605. ' if 23=e.{@c}GetValue then ;',
  15606. ' if 24=e.{@d}GetValue(25) then ;']);
  15607. ConvertProgram;
  15608. CheckSource('TestClass_OverloadDelphiOverride',
  15609. LinesToStr([ // statements
  15610. 'rtl.createClass(this, "TObject", null, function () {',
  15611. ' this.$init = function () {',
  15612. ' };',
  15613. ' this.$final = function () {',
  15614. ' };',
  15615. '});',
  15616. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15617. ' this.GetValue = function () {',
  15618. ' var Result = 0;',
  15619. ' if (3 === this.GetValue()) ;',
  15620. ' if (4 === this.GetValue$1(5)) ;',
  15621. ' return Result;',
  15622. ' };',
  15623. ' this.GetValue$1 = function (AValue) {',
  15624. ' var Result = 0;',
  15625. ' return Result;',
  15626. ' };',
  15627. '});',
  15628. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15629. ' this.GetValue = function () {',
  15630. ' var Result = 0;',
  15631. ' if (13 === this.GetValue()) ;',
  15632. ' if (14 === this.GetValue$1(15)) ;',
  15633. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  15634. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  15635. ' return Result;',
  15636. ' };',
  15637. ' this.GetValue$1 = function (AValue) {',
  15638. ' var Result = 0;',
  15639. ' return Result;',
  15640. ' };',
  15641. '});',
  15642. 'this.e = null;',
  15643. '']),
  15644. LinesToStr([ // $mod.$main
  15645. 'if (23 === $mod.e.GetValue()) ;',
  15646. 'if (24 === $mod.e.GetValue$1(25)) ;',
  15647. '']));
  15648. end;
  15649. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  15650. begin
  15651. StartProgram(false);
  15652. Add([
  15653. '{$mode delphi}',
  15654. 'type',
  15655. ' TObject = class end;',
  15656. ' TAnimal = class',
  15657. ' public',
  15658. ' {#animal_a}A: longint;',
  15659. ' function {#animal_b}B: longint;',
  15660. ' end;',
  15661. ' TBird = class(TAnimal)',
  15662. ' public',
  15663. ' {#bird_a}A: double;',
  15664. ' {#bird_b}B: boolean;',
  15665. ' end;',
  15666. ' TEagle = class(TBird)',
  15667. ' public',
  15668. ' function {#eagle_a}A: boolean;',
  15669. ' {#eagle_b}B: double;',
  15670. ' end;',
  15671. 'function TAnimal.B: longint;',
  15672. 'begin',
  15673. 'end;',
  15674. 'function TEagle.A: boolean;',
  15675. 'begin',
  15676. ' {@eagle_b}B:=3.3;',
  15677. ' {@eagle_a}A();',
  15678. ' TBird(Self).{@bird_b}B:=true;',
  15679. ' TAnimal(Self).{@animal_a}A:=17;',
  15680. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  15681. 'end;',
  15682. 'var',
  15683. ' e: TEagle;',
  15684. 'begin',
  15685. ' e.{@eagle_b}B:=5.3;',
  15686. ' if e.{@eagle_a}A then ;',
  15687. '']);
  15688. ConvertProgram;
  15689. CheckSource('TestClass_ReintroduceVarDelphi',
  15690. LinesToStr([ // statements
  15691. 'rtl.createClass(this, "TObject", null, function () {',
  15692. ' this.$init = function () {',
  15693. ' };',
  15694. ' this.$final = function () {',
  15695. ' };',
  15696. '});',
  15697. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  15698. ' this.$init = function () {',
  15699. ' $mod.TObject.$init.call(this);',
  15700. ' this.A = 0;',
  15701. ' };',
  15702. ' this.B = function () {',
  15703. ' var Result = 0;',
  15704. ' return Result;',
  15705. ' };',
  15706. '});',
  15707. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  15708. ' this.$init = function () {',
  15709. ' $mod.TAnimal.$init.call(this);',
  15710. ' this.A$1 = 0.0;',
  15711. ' this.B$1 = false;',
  15712. ' };',
  15713. '});',
  15714. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15715. ' this.$init = function () {',
  15716. ' $mod.TBird.$init.call(this);',
  15717. ' this.B$2 = 0.0;',
  15718. ' };',
  15719. ' this.A$2 = function () {',
  15720. ' var Result = false;',
  15721. ' this.B$2 = 3.3;',
  15722. ' this.A$2();',
  15723. ' this.B$1 = true;',
  15724. ' this.A = 17;',
  15725. ' this.B$1 = this.A$1 > 1;',
  15726. ' return Result;',
  15727. ' };',
  15728. '});',
  15729. 'this.e = null;',
  15730. '']),
  15731. LinesToStr([ // $mod.$main
  15732. '$mod.e.B$2 = 5.3;',
  15733. 'if ($mod.e.A$2()) ;',
  15734. '']));
  15735. end;
  15736. procedure TTestModule.TestClass_ReintroducedVar;
  15737. begin
  15738. StartProgram(false);
  15739. Add('type');
  15740. Add(' TObject = class');
  15741. Add(' strict private');
  15742. Add(' Some: longint;');
  15743. Add(' end;');
  15744. Add(' TMobile = class');
  15745. Add(' strict private');
  15746. Add(' Some: string;');
  15747. Add(' end;');
  15748. Add(' TCar = class(tmobile)');
  15749. Add(' procedure Some;');
  15750. Add(' procedure Some(vA: longint);');
  15751. Add(' end;');
  15752. Add('procedure tcar.some;');
  15753. Add('begin');
  15754. Add(' Some;');
  15755. Add(' Some(1);');
  15756. Add('end;');
  15757. Add('procedure tcar.some(va: longint); begin end;');
  15758. Add('begin');
  15759. ConvertProgram;
  15760. CheckSource('TestClass_ReintroducedVar',
  15761. LinesToStr([ // statements
  15762. 'rtl.createClass(this, "TObject", null, function () {',
  15763. ' this.$init = function () {',
  15764. ' this.Some = 0;',
  15765. ' };',
  15766. ' this.$final = function () {',
  15767. ' };',
  15768. '});',
  15769. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  15770. ' this.$init = function () {',
  15771. ' $mod.TObject.$init.call(this);',
  15772. ' this.Some$1 = "";',
  15773. ' };',
  15774. '});',
  15775. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  15776. ' this.Some$2 = function () {',
  15777. ' this.Some$2();',
  15778. ' this.Some$3(1);',
  15779. ' };',
  15780. ' this.Some$3 = function (vA) {',
  15781. ' };',
  15782. '});',
  15783. '']),
  15784. LinesToStr([ // $mod.$main
  15785. '']));
  15786. end;
  15787. procedure TTestModule.TestClass_RaiseDescendant;
  15788. begin
  15789. StartProgram(false);
  15790. Add([
  15791. 'type',
  15792. ' TObject = class',
  15793. ' constructor Create(Msg: string);',
  15794. ' end;',
  15795. ' Exception = class',
  15796. ' end;',
  15797. ' EConvertError = class(Exception)',
  15798. ' end;',
  15799. 'constructor TObject.Create(Msg: string); begin end;',
  15800. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  15801. 'begin',
  15802. ' raise Exception.Create(''Bar1'');',
  15803. ' raise EConvertError.Create(''Bar2'');',
  15804. ' raise AssertConv(''Bar2'');',
  15805. ' raise AssertConv;',
  15806. '']);
  15807. ConvertProgram;
  15808. CheckSource('TestClass_RaiseDescendant',
  15809. LinesToStr([ // statements
  15810. 'rtl.createClass(this, "TObject", null, function () {',
  15811. ' this.$init = function () {',
  15812. ' };',
  15813. ' this.$final = function () {',
  15814. ' };',
  15815. ' this.Create = function (Msg) {',
  15816. ' return this;',
  15817. ' };',
  15818. '});',
  15819. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  15820. '});',
  15821. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  15822. '});',
  15823. 'this.AssertConv = function (Msg) {',
  15824. ' var Result = null;',
  15825. ' return Result;',
  15826. '};',
  15827. '']),
  15828. LinesToStr([ // $mod.$main
  15829. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  15830. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  15831. 'throw $mod.AssertConv("Bar2");',
  15832. 'throw $mod.AssertConv("def");',
  15833. '']));
  15834. end;
  15835. procedure TTestModule.TestClass_ExternalMethod;
  15836. begin
  15837. AddModuleWithIntfImplSrc('unit2.pas',
  15838. LinesToStr([
  15839. 'type',
  15840. ' TObject = class',
  15841. ' public',
  15842. ' procedure Intern; external name ''$DoIntern'';',
  15843. ' end;',
  15844. '']),
  15845. LinesToStr([
  15846. '']));
  15847. StartUnit(true);
  15848. Add('interface');
  15849. Add('uses unit2;');
  15850. Add('type');
  15851. Add(' TCar = class(TObject)');
  15852. Add(' public');
  15853. Add(' procedure Intern2; external name ''$DoIntern2'';');
  15854. Add(' procedure DoIt;');
  15855. Add(' end;');
  15856. Add('implementation');
  15857. Add('procedure tcar.doit;');
  15858. Add('begin');
  15859. Add(' Intern;');
  15860. Add(' Intern();');
  15861. Add(' Intern2;');
  15862. Add(' Intern2();');
  15863. Add('end;');
  15864. Add('var Obj: TCar;');
  15865. Add('begin');
  15866. Add(' obj.intern;');
  15867. Add(' obj.intern();');
  15868. Add(' obj.intern2;');
  15869. Add(' obj.intern2();');
  15870. Add(' obj.doit;');
  15871. Add(' obj.doit();');
  15872. Add(' with obj do begin');
  15873. Add(' Intern;');
  15874. Add(' Intern();');
  15875. Add(' Intern2;');
  15876. Add(' Intern2();');
  15877. Add(' end;');
  15878. ConvertUnit;
  15879. CheckSource('TestClass_ExternalMethod',
  15880. LinesToStr([
  15881. 'var $impl = $mod.$impl;',
  15882. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15883. ' this.DoIt = function () {',
  15884. ' this.$DoIntern();',
  15885. ' this.$DoIntern();',
  15886. ' this.$DoIntern2();',
  15887. ' this.$DoIntern2();',
  15888. ' };',
  15889. ' });',
  15890. '']),
  15891. LinesToStr([ // this.$init
  15892. '$impl.Obj.$DoIntern();',
  15893. '$impl.Obj.$DoIntern();',
  15894. '$impl.Obj.$DoIntern2();',
  15895. '$impl.Obj.$DoIntern2();',
  15896. '$impl.Obj.DoIt();',
  15897. '$impl.Obj.DoIt();',
  15898. 'var $with = $impl.Obj;',
  15899. '$with.$DoIntern();',
  15900. '$with.$DoIntern();',
  15901. '$with.$DoIntern2();',
  15902. '$with.$DoIntern2();',
  15903. '']),
  15904. LinesToStr([ // implementation
  15905. '$impl.Obj = null;',
  15906. '']) );
  15907. end;
  15908. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  15909. begin
  15910. StartProgram(false);
  15911. Add('type');
  15912. Add(' TObject = class');
  15913. Add(' procedure DoIt; virtual; external name ''Foo'';');
  15914. Add(' end;');
  15915. Add('begin');
  15916. SetExpectedPasResolverError('Virtual method name must match external',
  15917. nVirtualMethodNameMustMatchExternal);
  15918. ConvertProgram;
  15919. end;
  15920. procedure TTestModule.TestClass_ExternalOverrideFail;
  15921. begin
  15922. StartProgram(false);
  15923. Add('type');
  15924. Add(' TObject = class');
  15925. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  15926. Add(' end;');
  15927. Add(' TCar = class');
  15928. Add(' procedure DoIt; override; external name ''DoIt'';');
  15929. Add(' end;');
  15930. Add('begin');
  15931. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  15932. nInvalidXModifierY);
  15933. ConvertProgram;
  15934. end;
  15935. procedure TTestModule.TestClass_ExternalVar;
  15936. begin
  15937. AddModuleWithIntfImplSrc('unit2.pas',
  15938. LinesToStr([
  15939. '{$modeswitch externalclass}',
  15940. 'type',
  15941. ' TObject = class',
  15942. ' public',
  15943. ' Intern: longint external name ''$Intern'';',
  15944. ' Bracket: longint external name ''["A B"]'';',
  15945. ' end;',
  15946. '']),
  15947. LinesToStr([
  15948. '']));
  15949. StartUnit(true);
  15950. Add([
  15951. 'interface',
  15952. 'uses unit2;',
  15953. '{$modeswitch externalclass}',
  15954. 'type',
  15955. ' TCar = class(tobject)',
  15956. ' public',
  15957. ' Intern2: longint external name ''$Intern2'';',
  15958. ' procedure DoIt;',
  15959. ' end;',
  15960. 'implementation',
  15961. 'procedure tcar.doit;',
  15962. 'begin',
  15963. ' Intern:=Intern+1;',
  15964. ' Intern2:=Intern2+2;',
  15965. ' Bracket:=Bracket+3;',
  15966. 'end;',
  15967. 'var Obj: TCar;',
  15968. 'begin',
  15969. ' obj.intern:=obj.intern+1;',
  15970. ' obj.intern2:=obj.intern2+2;',
  15971. ' obj.Bracket:=obj.Bracket+3;',
  15972. ' with obj do begin',
  15973. ' intern:=intern+1;',
  15974. ' intern2:=intern2+2;',
  15975. ' Bracket:=Bracket+3;',
  15976. ' end;']);
  15977. ConvertUnit;
  15978. CheckSource('TestClass_ExternalVar',
  15979. LinesToStr([
  15980. 'var $impl = $mod.$impl;',
  15981. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15982. ' this.DoIt = function () {',
  15983. ' this.$Intern = this.$Intern + 1;',
  15984. ' this.$Intern2 = this.$Intern2 + 2;',
  15985. ' this["A B"] = this["A B"] + 3;',
  15986. ' };',
  15987. ' });',
  15988. '']),
  15989. LinesToStr([
  15990. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  15991. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  15992. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  15993. 'var $with = $impl.Obj;',
  15994. '$with.$Intern = $with.$Intern + 1;',
  15995. '$with.$Intern2 = $with.$Intern2 + 2;',
  15996. '$with["A B"] = $with["A B"] + 3;',
  15997. '']),
  15998. LinesToStr([ // implementation
  15999. '$impl.Obj = null;',
  16000. '']));
  16001. end;
  16002. procedure TTestModule.TestClass_Const;
  16003. begin
  16004. StartProgram(false);
  16005. Add([
  16006. 'type',
  16007. ' integer = longint;',
  16008. ' TClass = class of TObject;',
  16009. ' TObject = class',
  16010. ' public',
  16011. ' const cI: integer = 3;',
  16012. ' procedure DoIt;',
  16013. ' class procedure DoMore;',
  16014. ' end;',
  16015. 'procedure tobject.doit;',
  16016. 'begin',
  16017. ' if cI=4 then;',
  16018. ' if 5=cI then;',
  16019. ' if Self.cI=6 then;',
  16020. ' if 7=Self.cI then;',
  16021. ' with Self do begin',
  16022. ' if cI=11 then;',
  16023. ' if 12=cI then;',
  16024. ' end;',
  16025. 'end;',
  16026. 'class procedure tobject.domore;',
  16027. 'begin',
  16028. ' if cI=8 then;',
  16029. ' if Self.cI=9 then;',
  16030. ' if 10=cI then;',
  16031. ' if 11=Self.cI then;',
  16032. ' with Self do begin',
  16033. ' if cI=13 then;',
  16034. ' if 14=cI then;',
  16035. ' end;',
  16036. 'end;',
  16037. 'var',
  16038. ' Obj: TObject;',
  16039. ' Cla: TClass;',
  16040. 'begin',
  16041. ' if TObject.cI=21 then ;',
  16042. ' if Obj.cI=22 then ;',
  16043. ' if Cla.cI=23 then ;',
  16044. ' with obj do if ci=24 then;',
  16045. ' with TObject do if ci=25 then;',
  16046. ' with Cla do if ci=26 then;']);
  16047. ConvertProgram;
  16048. CheckSource('TestClass_Const',
  16049. LinesToStr([
  16050. 'rtl.createClass(this, "TObject", null, function () {',
  16051. ' this.cI = 3;',
  16052. ' this.$init = function () {',
  16053. ' };',
  16054. ' this.$final = function () {',
  16055. ' };',
  16056. ' this.DoIt = function () {',
  16057. ' if (this.cI === 4) ;',
  16058. ' if (5 === this.cI) ;',
  16059. ' if (this.cI === 6) ;',
  16060. ' if (7 === this.cI) ;',
  16061. ' if (this.cI === 11) ;',
  16062. ' if (12 === this.cI) ;',
  16063. ' };',
  16064. ' this.DoMore = function () {',
  16065. ' if (this.cI === 8) ;',
  16066. ' if (this.cI === 9) ;',
  16067. ' if (10 === this.cI) ;',
  16068. ' if (11 === this.cI) ;',
  16069. ' if (this.cI === 13) ;',
  16070. ' if (14 === this.cI) ;',
  16071. ' };',
  16072. '});',
  16073. 'this.Obj = null;',
  16074. 'this.Cla = null;',
  16075. '']),
  16076. LinesToStr([
  16077. 'if ($mod.TObject.cI === 21) ;',
  16078. 'if ($mod.Obj.cI === 22) ;',
  16079. 'if ($mod.Cla.cI === 23) ;',
  16080. 'var $with = $mod.Obj;',
  16081. 'if ($with.cI === 24) ;',
  16082. 'var $with1 = $mod.TObject;',
  16083. 'if ($with1.cI === 25) ;',
  16084. 'var $with2 = $mod.Cla;',
  16085. 'if ($with2.cI === 26) ;',
  16086. '']));
  16087. end;
  16088. procedure TTestModule.TestClass_ConstEnum;
  16089. begin
  16090. StartProgram(false);
  16091. Add([
  16092. 'type',
  16093. ' TEnum = (red,blue);',
  16094. ' TObject = class',
  16095. ' end;',
  16096. ' TAnimal = class',
  16097. ' public',
  16098. ' type TSubEnum = (light,dark);',
  16099. ' const a = high(TEnum);',
  16100. ' const b = high(TSubEnum);',
  16101. ' end;',
  16102. ' TBird = class(TAnimal)',
  16103. ' public',
  16104. ' const c = high(TEnum);',
  16105. ' const d = high(TSubEnum);',
  16106. ' end;',
  16107. ' TAnt = class',
  16108. ' public',
  16109. ' const e = high(TEnum);',
  16110. ' const f = high(TBird.TSubEnum);',
  16111. ' end;',
  16112. 'begin',
  16113. '']);
  16114. ConvertProgram;
  16115. CheckSource('TestClass_ConstEnum',
  16116. LinesToStr([
  16117. 'this.TEnum = {',
  16118. ' "0": "red",',
  16119. ' red: 0,',
  16120. ' "1": "blue",',
  16121. ' blue: 1',
  16122. '};',
  16123. 'rtl.createClass(this, "TObject", null, function () {',
  16124. ' this.$init = function () {',
  16125. ' };',
  16126. ' this.$final = function () {',
  16127. ' };',
  16128. '});',
  16129. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16130. ' this.TSubEnum = {',
  16131. ' "0": "light",',
  16132. ' light: 0,',
  16133. ' "1": "dark",',
  16134. ' dark: 1',
  16135. ' };',
  16136. ' this.a = $mod.TEnum.blue;',
  16137. ' this.b = this.TSubEnum.dark;',
  16138. '});',
  16139. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16140. ' this.c = $mod.TEnum.blue;',
  16141. ' this.d = this.TSubEnum.dark;',
  16142. '});',
  16143. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16144. ' this.e = $mod.TEnum.blue;',
  16145. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16146. '});',
  16147. '']),
  16148. LinesToStr([
  16149. '']));
  16150. end;
  16151. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16152. begin
  16153. StartProgram(false);
  16154. Add([
  16155. 'type',
  16156. ' TObject = class',
  16157. ' const cI: longint = 3;',
  16158. ' procedure Fly;',
  16159. ' procedure Run;',
  16160. ' end;',
  16161. ' TBird = class',
  16162. ' procedure Go;',
  16163. ' end;',
  16164. 'procedure tobject.fly;',
  16165. 'const cI: word = 4;',
  16166. 'begin',
  16167. ' if cI=Self.cI then ;',
  16168. 'end;',
  16169. 'procedure tobject.run;',
  16170. 'const cI: word = 5;',
  16171. 'begin',
  16172. ' if cI=Self.cI then ;',
  16173. 'end;',
  16174. 'procedure tbird.go;',
  16175. 'const cI: word = 6;',
  16176. 'begin',
  16177. ' if cI=Self.cI then ;',
  16178. 'end;',
  16179. 'begin',
  16180. '']);
  16181. ConvertProgram;
  16182. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16183. LinesToStr([
  16184. 'rtl.createClass(this, "TObject", null, function () {',
  16185. ' this.cI = 3;',
  16186. ' this.$init = function () {',
  16187. ' };',
  16188. ' this.$final = function () {',
  16189. ' };',
  16190. ' var cI$1 = 4;',
  16191. ' this.Fly = function () {',
  16192. ' if (cI$1 === this.cI) ;',
  16193. ' };',
  16194. ' var cI$2 = 5;',
  16195. ' this.Run = function () {',
  16196. ' if (cI$2 === this.cI) ;',
  16197. ' };',
  16198. '});',
  16199. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16200. ' var cI$3 = 6;',
  16201. ' this.Go = function () {',
  16202. ' if (cI$3 === this.cI) ;',
  16203. ' };',
  16204. '});',
  16205. '']),
  16206. LinesToStr([
  16207. '']));
  16208. end;
  16209. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16210. begin
  16211. StartUnit(false);
  16212. Add([
  16213. 'interface',
  16214. 'type',
  16215. ' TObject = class',
  16216. ' const cI: longint = 3;',
  16217. ' procedure Fly;',
  16218. ' procedure Run;',
  16219. ' end;',
  16220. ' TBird = class',
  16221. ' procedure Go;',
  16222. ' end;',
  16223. 'implementation',
  16224. 'procedure tobject.fly;',
  16225. 'const cI: word = 4;',
  16226. 'begin',
  16227. ' if cI=Self.cI then ;',
  16228. 'end;',
  16229. 'procedure tobject.run;',
  16230. 'const cI: word = 5;',
  16231. 'begin',
  16232. ' if cI=Self.cI then ;',
  16233. 'end;',
  16234. 'procedure tbird.go;',
  16235. 'const cI: word = 6;',
  16236. 'begin',
  16237. ' if cI=Self.cI then ;',
  16238. 'end;',
  16239. '']);
  16240. ConvertUnit;
  16241. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16242. LinesToStr([
  16243. 'rtl.createClass(this, "TObject", null, function () {',
  16244. ' this.cI = 3;',
  16245. ' this.$init = function () {',
  16246. ' };',
  16247. ' this.$final = function () {',
  16248. ' };',
  16249. ' var cI$1 = 4;',
  16250. ' this.Fly = function () {',
  16251. ' if (cI$1 === this.cI) ;',
  16252. ' };',
  16253. ' var cI$2 = 5;',
  16254. ' this.Run = function () {',
  16255. ' if (cI$2 === this.cI) ;',
  16256. ' };',
  16257. '});',
  16258. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16259. ' var cI$3 = 6;',
  16260. ' this.Go = function () {',
  16261. ' if (cI$3 === this.cI) ;',
  16262. ' };',
  16263. '});',
  16264. '']),
  16265. '',
  16266. '');
  16267. end;
  16268. procedure TTestModule.TestClass_LocalVarSelfFail;
  16269. begin
  16270. StartProgram(false);
  16271. Add([
  16272. 'type',
  16273. ' TObject = class',
  16274. ' constructor Create;',
  16275. ' end;',
  16276. 'constructor tobject.create;',
  16277. 'var self: longint;',
  16278. 'begin',
  16279. 'end',
  16280. 'begin',
  16281. '']);
  16282. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16283. ConvertProgram;
  16284. end;
  16285. procedure TTestModule.TestClass_ArgSelfFail;
  16286. begin
  16287. StartProgram(false);
  16288. Add([
  16289. 'type',
  16290. ' TObject = class',
  16291. ' procedure DoIt(Self: longint);',
  16292. ' end;',
  16293. 'procedure tobject.doit(self: longint);',
  16294. 'begin',
  16295. 'end',
  16296. 'begin',
  16297. '']);
  16298. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16299. ConvertProgram;
  16300. end;
  16301. procedure TTestModule.TestClass_NestedProcSelf;
  16302. begin
  16303. StartProgram(false);
  16304. Add([
  16305. 'type',
  16306. ' TObject = class',
  16307. ' Key: longint;',
  16308. ' class var State: longint;',
  16309. ' procedure DoIt;',
  16310. ' function GetSize: longint; virtual; abstract;',
  16311. ' procedure SetSize(Value: longint); virtual; abstract;',
  16312. ' property Size: longint read GetSize write SetSize;',
  16313. ' end;',
  16314. 'procedure tobject.doit;',
  16315. ' procedure Sub;',
  16316. ' begin',
  16317. ' key:=key+2;',
  16318. ' self.key:=self.key+3;',
  16319. ' state:=state+4;',
  16320. ' self.state:=self.state+5;',
  16321. ' tobject.state:=tobject.state+6;',
  16322. ' size:=size+7;',
  16323. ' self.size:=self.size+8;',
  16324. ' end;',
  16325. 'begin',
  16326. ' sub;',
  16327. ' key:=key+12;',
  16328. ' self.key:=self.key+13;',
  16329. ' state:=state+14;',
  16330. ' self.state:=self.state+15;',
  16331. ' tobject.state:=tobject.state+16;',
  16332. ' size:=size+17;',
  16333. ' self.size:=self.size+18;',
  16334. 'end;',
  16335. 'begin',
  16336. '']);
  16337. ConvertProgram;
  16338. CheckSource('TestClass_NestedProcSelf',
  16339. LinesToStr([ // statements
  16340. 'rtl.createClass(this, "TObject", null, function () {',
  16341. ' this.State = 0;',
  16342. ' this.$init = function () {',
  16343. ' this.Key = 0;',
  16344. ' };',
  16345. ' this.$final = function () {',
  16346. ' };',
  16347. ' this.DoIt = function () {',
  16348. ' var $Self = this;',
  16349. ' function Sub() {',
  16350. ' $Self.Key = $Self.Key + 2;',
  16351. ' $Self.Key = $Self.Key + 3;',
  16352. ' $mod.TObject.State = $Self.State + 4;',
  16353. ' $mod.TObject.State = $Self.State + 5;',
  16354. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16355. ' $Self.SetSize($Self.GetSize() + 7);',
  16356. ' $Self.SetSize($Self.GetSize() + 8);',
  16357. ' };',
  16358. ' Sub();',
  16359. ' this.Key = this.Key + 12;',
  16360. ' $Self.Key = $Self.Key + 13;',
  16361. ' $mod.TObject.State = this.State + 14;',
  16362. ' $mod.TObject.State = $Self.State + 15;',
  16363. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16364. ' this.SetSize(this.GetSize() + 17);',
  16365. ' $Self.SetSize($Self.GetSize() + 18);',
  16366. ' };',
  16367. '});',
  16368. '']),
  16369. LinesToStr([ // $mod.$main
  16370. '']));
  16371. end;
  16372. procedure TTestModule.TestClass_NestedProcSelf2;
  16373. begin
  16374. StartProgram(false);
  16375. Add([
  16376. 'type',
  16377. ' TObject = class',
  16378. ' Key: longint;',
  16379. ' class var State: longint;',
  16380. ' function GetSize: longint; virtual; abstract;',
  16381. ' procedure SetSize(Value: longint); virtual; abstract;',
  16382. ' property Size: longint read GetSize write SetSize;',
  16383. ' end;',
  16384. ' TBird = class',
  16385. ' procedure DoIt;',
  16386. ' end;',
  16387. 'procedure tbird.doit;',
  16388. ' procedure Sub;',
  16389. ' begin',
  16390. ' key:=key+2;',
  16391. ' self.key:=self.key+3;',
  16392. ' state:=state+4;',
  16393. ' self.state:=self.state+5;',
  16394. ' tobject.state:=tobject.state+6;',
  16395. ' size:=size+7;',
  16396. ' self.size:=self.size+8;',
  16397. ' end;',
  16398. 'begin',
  16399. ' sub;',
  16400. ' key:=key+12;',
  16401. ' self.key:=self.key+13;',
  16402. ' state:=state+14;',
  16403. ' self.state:=self.state+15;',
  16404. ' tobject.state:=tobject.state+16;',
  16405. ' size:=size+17;',
  16406. ' self.size:=self.size+18;',
  16407. 'end;',
  16408. 'begin',
  16409. '']);
  16410. ConvertProgram;
  16411. CheckSource('TestClass_NestedProcSelf2',
  16412. LinesToStr([ // statements
  16413. 'rtl.createClass(this, "TObject", null, function () {',
  16414. ' this.State = 0;',
  16415. ' this.$init = function () {',
  16416. ' this.Key = 0;',
  16417. ' };',
  16418. ' this.$final = function () {',
  16419. ' };',
  16420. '});',
  16421. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16422. ' this.DoIt = function () {',
  16423. ' var $Self = this;',
  16424. ' function Sub() {',
  16425. ' $Self.Key = $Self.Key + 2;',
  16426. ' $Self.Key = $Self.Key + 3;',
  16427. ' $mod.TObject.State = $Self.State + 4;',
  16428. ' $mod.TObject.State = $Self.State + 5;',
  16429. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16430. ' $Self.SetSize($Self.GetSize() + 7);',
  16431. ' $Self.SetSize($Self.GetSize() + 8);',
  16432. ' };',
  16433. ' Sub();',
  16434. ' this.Key = this.Key + 12;',
  16435. ' $Self.Key = $Self.Key + 13;',
  16436. ' $mod.TObject.State = this.State + 14;',
  16437. ' $mod.TObject.State = $Self.State + 15;',
  16438. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16439. ' this.SetSize(this.GetSize() + 17);',
  16440. ' $Self.SetSize($Self.GetSize() + 18);',
  16441. ' };',
  16442. '});',
  16443. '']),
  16444. LinesToStr([ // $mod.$main
  16445. '']));
  16446. end;
  16447. procedure TTestModule.TestClass_NestedProcClassSelf;
  16448. begin
  16449. StartProgram(false);
  16450. Add([
  16451. 'type',
  16452. ' TObject = class',
  16453. ' class var State: longint;',
  16454. ' class procedure DoIt;',
  16455. ' class function GetSize: longint; virtual; abstract;',
  16456. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16457. ' class property Size: longint read GetSize write SetSize;',
  16458. ' end;',
  16459. 'class procedure tobject.doit;',
  16460. ' procedure Sub;',
  16461. ' begin',
  16462. ' state:=state+2;',
  16463. ' self.state:=self.state+3;',
  16464. ' tobject.state:=tobject.state+4;',
  16465. ' size:=size+5;',
  16466. ' self.size:=self.size+6;',
  16467. ' tobject.size:=tobject.size+7;',
  16468. ' end;',
  16469. 'begin',
  16470. ' sub;',
  16471. ' state:=state+12;',
  16472. ' self.state:=self.state+13;',
  16473. ' tobject.state:=tobject.state+14;',
  16474. ' size:=size+15;',
  16475. ' self.size:=self.size+16;',
  16476. ' tobject.size:=tobject.size+17;',
  16477. 'end;',
  16478. 'begin',
  16479. '']);
  16480. ConvertProgram;
  16481. CheckSource('TestClass_NestedProcClassSelf',
  16482. LinesToStr([ // statements
  16483. 'rtl.createClass(this, "TObject", null, function () {',
  16484. ' this.State = 0;',
  16485. ' this.$init = function () {',
  16486. ' };',
  16487. ' this.$final = function () {',
  16488. ' };',
  16489. ' this.DoIt = function () {',
  16490. ' var $Self = this;',
  16491. ' function Sub() {',
  16492. ' $mod.TObject.State = $Self.State + 2;',
  16493. ' $mod.TObject.State = $Self.State + 3;',
  16494. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16495. ' $Self.SetSize($Self.GetSize() + 5);',
  16496. ' $Self.SetSize($Self.GetSize() + 6);',
  16497. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16498. ' };',
  16499. ' Sub();',
  16500. ' $mod.TObject.State = this.State + 12;',
  16501. ' $mod.TObject.State = $Self.State + 13;',
  16502. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16503. ' this.SetSize(this.GetSize() + 15);',
  16504. ' $Self.SetSize($Self.GetSize() + 16);',
  16505. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16506. ' };',
  16507. '});',
  16508. '']),
  16509. LinesToStr([ // $mod.$main
  16510. '']));
  16511. end;
  16512. procedure TTestModule.TestClass_NestedProcCallInherited;
  16513. begin
  16514. StartProgram(false);
  16515. Add([
  16516. 'type',
  16517. ' TObject = class',
  16518. ' function DoIt(k: boolean): longint; virtual;',
  16519. ' end;',
  16520. ' TBird = class',
  16521. ' function DoIt(k: boolean): longint; override;',
  16522. ' end;',
  16523. 'function tobject.doit(k: boolean): longint;',
  16524. 'begin',
  16525. 'end;',
  16526. 'function tbird.doit(k: boolean): longint;',
  16527. ' procedure Sub;',
  16528. ' begin',
  16529. ' inherited DoIt(true);',
  16530. //' if inherited DoIt(false)=4 then ;',
  16531. ' end;',
  16532. 'begin',
  16533. ' Sub;',
  16534. ' inherited;',
  16535. ' inherited DoIt(true);',
  16536. //' if inherited DoIt(false)=14 then ;',
  16537. 'end;',
  16538. 'begin',
  16539. '']);
  16540. ConvertProgram;
  16541. CheckSource('TestClass_NestedProcCallInherited',
  16542. LinesToStr([ // statements
  16543. 'rtl.createClass(this, "TObject", null, function () {',
  16544. ' this.$init = function () {',
  16545. ' };',
  16546. ' this.$final = function () {',
  16547. ' };',
  16548. ' this.DoIt = function (k) {',
  16549. ' var Result = 0;',
  16550. ' return Result;',
  16551. ' };',
  16552. '});',
  16553. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16554. ' this.DoIt = function (k) {',
  16555. ' var $Self = this;',
  16556. ' var Result = 0;',
  16557. ' function Sub() {',
  16558. ' $mod.TObject.DoIt.call($Self, true);',
  16559. ' };',
  16560. ' Sub();',
  16561. ' $mod.TObject.DoIt.apply(this, arguments);',
  16562. ' $mod.TObject.DoIt.call(this, true);',
  16563. ' return Result;',
  16564. ' };',
  16565. '});',
  16566. '']),
  16567. LinesToStr([ // $mod.$main
  16568. '']));
  16569. end;
  16570. procedure TTestModule.TestClass_TObjectFree;
  16571. begin
  16572. StartProgram(false);
  16573. Add([
  16574. 'type',
  16575. ' TObject = class',
  16576. ' Obj: tobject;',
  16577. ' procedure Free;',
  16578. ' procedure Release;',
  16579. ' end;',
  16580. 'procedure tobject.free;',
  16581. 'begin',
  16582. 'end;',
  16583. 'procedure tobject.release;',
  16584. 'begin',
  16585. ' free;',
  16586. ' if true then free;',
  16587. 'end;',
  16588. 'function DoIt(o: tobject): tobject;',
  16589. 'var l: tobject;',
  16590. 'begin',
  16591. ' o.free;',
  16592. ' o.free();',
  16593. ' l.free;',
  16594. ' l.free();',
  16595. ' o.obj.free;',
  16596. ' o.obj.free();',
  16597. ' with o do obj.free;',
  16598. ' with o do obj.free();',
  16599. ' result.Free;',
  16600. ' result.Free();',
  16601. 'end;',
  16602. 'var o: tobject;',
  16603. ' a: array of tobject;',
  16604. 'begin',
  16605. ' o.free;',
  16606. ' o.obj.free;',
  16607. ' a[1+2].free;',
  16608. '']);
  16609. ConvertProgram;
  16610. CheckSource('TestClass_TObjectFree',
  16611. LinesToStr([ // statements
  16612. 'rtl.createClass(this, "TObject", null, function () {',
  16613. ' this.$init = function () {',
  16614. ' this.Obj = null;',
  16615. ' };',
  16616. ' this.$final = function () {',
  16617. ' this.Obj = undefined;',
  16618. ' };',
  16619. ' this.Free = function () {',
  16620. ' };',
  16621. ' this.Release = function () {',
  16622. ' this.Free();',
  16623. ' if (true) this.Free();',
  16624. ' };',
  16625. '});',
  16626. 'this.DoIt = function (o) {',
  16627. ' var Result = null;',
  16628. ' var l = null;',
  16629. ' o = rtl.freeLoc(o);',
  16630. ' o = rtl.freeLoc(o);',
  16631. ' l = rtl.freeLoc(l);',
  16632. ' l = rtl.freeLoc(l);',
  16633. ' rtl.free(o, "Obj");',
  16634. ' rtl.free(o, "Obj");',
  16635. ' rtl.free(o, "Obj");',
  16636. ' rtl.free(o, "Obj");',
  16637. ' Result = rtl.freeLoc(Result);',
  16638. ' Result = rtl.freeLoc(Result);',
  16639. ' return Result;',
  16640. '};',
  16641. 'this.o = null;',
  16642. 'this.a = [];',
  16643. '']),
  16644. LinesToStr([ // $mod.$main
  16645. 'rtl.free($mod, "o");',
  16646. 'rtl.free($mod.o, "Obj");',
  16647. 'rtl.free($mod.a, 1 + 2);',
  16648. '']));
  16649. end;
  16650. procedure TTestModule.TestClass_TObjectFree_VarArg;
  16651. begin
  16652. StartProgram(false);
  16653. Add([
  16654. 'type',
  16655. ' TObject = class',
  16656. ' Obj: tobject;',
  16657. ' procedure Free;',
  16658. ' end;',
  16659. 'procedure tobject.free;',
  16660. 'begin',
  16661. 'end;',
  16662. 'procedure DoIt(var o: tobject);',
  16663. 'begin',
  16664. ' o.free;',
  16665. ' o.free();',
  16666. 'end;',
  16667. 'begin',
  16668. '']);
  16669. ConvertProgram;
  16670. CheckSource('TestClass_TObjectFree_VarArg',
  16671. LinesToStr([ // statements
  16672. 'rtl.createClass(this, "TObject", null, function () {',
  16673. ' this.$init = function () {',
  16674. ' this.Obj = null;',
  16675. ' };',
  16676. ' this.$final = function () {',
  16677. ' this.Obj = undefined;',
  16678. ' };',
  16679. ' this.Free = function () {',
  16680. ' };',
  16681. '});',
  16682. 'this.DoIt = function (o) {',
  16683. ' o.set(rtl.freeLoc(o.get()));',
  16684. ' o.set(rtl.freeLoc(o.get()));',
  16685. '};',
  16686. '']),
  16687. LinesToStr([ // $mod.$main
  16688. '']));
  16689. end;
  16690. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  16691. begin
  16692. StartProgram(false);
  16693. Add([
  16694. 'type',
  16695. ' TObject = class',
  16696. ' constructor Create;',
  16697. ' procedure Free;',
  16698. ' end;',
  16699. 'constructor TObject.Create; begin end;',
  16700. 'procedure tobject.free; begin end;',
  16701. 'begin',
  16702. ' with tobject.create do free;',
  16703. '']);
  16704. ConvertProgram;
  16705. CheckSource('TestClass_TObjectFreeNewInstance',
  16706. LinesToStr([ // statements
  16707. 'rtl.createClass(this, "TObject", null, function () {',
  16708. ' this.$init = function () {',
  16709. ' };',
  16710. ' this.$final = function () {',
  16711. ' };',
  16712. ' this.Create = function () {',
  16713. ' return this;',
  16714. ' };',
  16715. ' this.Free = function () {',
  16716. ' };',
  16717. '});',
  16718. '']),
  16719. LinesToStr([ // $mod.$main
  16720. 'var $with = $mod.TObject.$create("Create");',
  16721. '$with=rtl.freeLoc($with);',
  16722. '']));
  16723. end;
  16724. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  16725. begin
  16726. StartProgram(false);
  16727. Add([
  16728. 'type',
  16729. ' TObject = class',
  16730. ' destructor Destroy;',
  16731. ' procedure Free;',
  16732. ' end;',
  16733. 'destructor TObject.Destroy; begin end;',
  16734. 'procedure tobject.free; begin end;',
  16735. 'var o: tobject;',
  16736. 'begin',
  16737. ' o.free;',
  16738. '']);
  16739. Converter.UseLowerCase:=true;
  16740. ConvertProgram;
  16741. CheckSource('TestClass_TObjectFreeLowerCase',
  16742. LinesToStr([ // statements
  16743. 'rtl.createClass(this, "tobject", null, function () {',
  16744. ' this.$init = function () {',
  16745. ' };',
  16746. ' this.$final = function () {',
  16747. ' };',
  16748. ' rtl.tObjectDestroy = "destroy";',
  16749. ' this.destroy = function () {',
  16750. ' };',
  16751. ' this.free = function () {',
  16752. ' };',
  16753. '});',
  16754. 'this.o = null;',
  16755. '']),
  16756. LinesToStr([ // $mod.$main
  16757. 'rtl.free($mod, "o");',
  16758. '']));
  16759. end;
  16760. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  16761. begin
  16762. StartProgram(false);
  16763. Add([
  16764. 'type',
  16765. ' TObject = class',
  16766. ' procedure Free;',
  16767. ' function GetObj: tobject; virtual; abstract;',
  16768. ' end;',
  16769. 'procedure tobject.free;',
  16770. 'begin',
  16771. 'end;',
  16772. 'var o: tobject;',
  16773. 'begin',
  16774. ' o.getobj.free;',
  16775. '']);
  16776. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  16777. ConvertProgram;
  16778. end;
  16779. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  16780. begin
  16781. StartProgram(false);
  16782. Add([
  16783. 'type',
  16784. ' TObject = class',
  16785. ' procedure Free;',
  16786. ' FObj: TObject;',
  16787. ' property Obj: tobject read FObj write FObj;',
  16788. ' end;',
  16789. 'procedure tobject.free;',
  16790. 'begin',
  16791. 'end;',
  16792. 'var o: tobject;',
  16793. 'begin',
  16794. ' o.obj.free;',
  16795. '']);
  16796. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  16797. ConvertProgram;
  16798. end;
  16799. procedure TTestModule.TestClass_ForIn;
  16800. begin
  16801. StartProgram(false);
  16802. Add([
  16803. 'type',
  16804. ' TObject = class end;',
  16805. ' TItem = TObject;',
  16806. ' TEnumerator = class',
  16807. ' FCurrent: TItem;',
  16808. ' property Current: TItem read FCurrent;',
  16809. ' function MoveNext: boolean;',
  16810. ' end;',
  16811. ' TBird = class',
  16812. ' function GetEnumerator: TEnumerator;',
  16813. ' end;',
  16814. 'function TEnumerator.MoveNext: boolean;',
  16815. 'begin',
  16816. 'end;',
  16817. 'function TBird.GetEnumerator: TEnumerator;',
  16818. 'begin',
  16819. 'end;',
  16820. 'var',
  16821. ' b: TBird;',
  16822. ' i, i2: TItem;',
  16823. 'begin',
  16824. ' for i in b do i2:=i;']);
  16825. ConvertProgram;
  16826. CheckSource('TestClass_ForIn',
  16827. LinesToStr([ // statements
  16828. 'rtl.createClass(this, "TObject", null, function () {',
  16829. ' this.$init = function () {',
  16830. ' };',
  16831. ' this.$final = function () {',
  16832. ' };',
  16833. '});',
  16834. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  16835. ' this.$init = function () {',
  16836. ' $mod.TObject.$init.call(this);',
  16837. ' this.FCurrent = null;',
  16838. ' };',
  16839. ' this.$final = function () {',
  16840. ' this.FCurrent = undefined;',
  16841. ' $mod.TObject.$final.call(this);',
  16842. ' };',
  16843. ' this.MoveNext = function () {',
  16844. ' var Result = false;',
  16845. ' return Result;',
  16846. ' };',
  16847. '});',
  16848. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16849. ' this.GetEnumerator = function () {',
  16850. ' var Result = null;',
  16851. ' return Result;',
  16852. ' };',
  16853. '});',
  16854. 'this.b = null;',
  16855. 'this.i = null;',
  16856. 'this.i2 = null;'
  16857. ]),
  16858. LinesToStr([ // $mod.$main
  16859. 'var $in = $mod.b.GetEnumerator();',
  16860. 'try {',
  16861. ' while ($in.MoveNext()){',
  16862. ' $mod.i = $in.FCurrent;',
  16863. ' $mod.i2 = $mod.i;',
  16864. ' }',
  16865. '} finally {',
  16866. ' $in = rtl.freeLoc($in)',
  16867. '};',
  16868. '']));
  16869. end;
  16870. procedure TTestModule.TestClass_DispatchMessage;
  16871. begin
  16872. StartProgram(false);
  16873. Add([
  16874. 'type',
  16875. ' TObject = class',
  16876. ' {$DispatchField DispInt}',
  16877. ' procedure Dispatch(var Msg); virtual; abstract;',
  16878. ' {$DispatchStrField DispStr}',
  16879. ' procedure DispatchStr(var Msg); virtual; abstract;',
  16880. ' end;',
  16881. ' THopMsg = record',
  16882. ' DispInt: longint;',
  16883. ' end;',
  16884. ' TPutMsg = record',
  16885. ' DispStr: string;',
  16886. ' end;',
  16887. ' TBird = class',
  16888. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  16889. ' procedure Run; overload; virtual; abstract;',
  16890. ' procedure Run(var Msg); overload; message ''Fast'';',
  16891. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  16892. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  16893. ' end;',
  16894. 'procedure TBird.Run(var Msg);',
  16895. 'begin',
  16896. 'end;',
  16897. 'begin',
  16898. '']);
  16899. ConvertProgram;
  16900. CheckSource('TestClass_Message',
  16901. LinesToStr([ // statements
  16902. 'rtl.createClass(this, "TObject", null, function () {',
  16903. ' this.$init = function () {',
  16904. ' };',
  16905. ' this.$final = function () {',
  16906. ' };',
  16907. '});',
  16908. 'rtl.recNewT(this, "THopMsg", function () {',
  16909. ' this.DispInt = 0;',
  16910. ' this.$eq = function (b) {',
  16911. ' return this.DispInt === b.DispInt;',
  16912. ' };',
  16913. ' this.$assign = function (s) {',
  16914. ' this.DispInt = s.DispInt;',
  16915. ' return this;',
  16916. ' };',
  16917. '});',
  16918. 'rtl.recNewT(this, "TPutMsg", function () {',
  16919. ' this.DispStr = "";',
  16920. ' this.$eq = function (b) {',
  16921. ' return this.DispStr === b.DispStr;',
  16922. ' };',
  16923. ' this.$assign = function (s) {',
  16924. ' this.DispStr = s.DispStr;',
  16925. ' return this;',
  16926. ' };',
  16927. '});',
  16928. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16929. ' this.Run$1 = function (Msg) {',
  16930. ' };',
  16931. ' this.$msgint = {',
  16932. ' "2": "Fly",',
  16933. ' "3": "Hop"',
  16934. ' };',
  16935. ' this.$msgstr = {',
  16936. ' Fast: "Run$1",',
  16937. ' foo: "Put"',
  16938. ' };',
  16939. '});',
  16940. '']),
  16941. LinesToStr([ // $mod.$main
  16942. '']));
  16943. end;
  16944. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  16945. begin
  16946. StartProgram(false);
  16947. Add([
  16948. 'type',
  16949. ' TObject = class',
  16950. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  16951. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  16952. ' end;',
  16953. 'begin',
  16954. '']);
  16955. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  16956. ConvertProgram;
  16957. end;
  16958. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  16959. begin
  16960. StartProgram(false);
  16961. Add([
  16962. 'type',
  16963. ' TObject = class',
  16964. ' {$dispatchfield Msg}',
  16965. ' procedure Dispatch(var Msg); virtual; abstract;',
  16966. ' end;',
  16967. ' TFlyMsg = record',
  16968. ' FlyId: longint;',
  16969. ' end;',
  16970. ' TBird = class',
  16971. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  16972. ' end;',
  16973. 'begin',
  16974. '']);
  16975. ConvertProgram;
  16976. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  16977. end;
  16978. procedure TTestModule.TestClassOf_Create;
  16979. begin
  16980. StartProgram(false);
  16981. Add('type');
  16982. Add(' TObject = class');
  16983. Add(' constructor Create;');
  16984. Add(' end;');
  16985. Add(' TClass = class of TObject;');
  16986. Add('constructor tobject.create; begin end;');
  16987. Add('var');
  16988. Add(' Obj: tobject;');
  16989. Add(' C: tclass;');
  16990. Add('begin');
  16991. Add(' obj:=C.create;');
  16992. Add(' with c do obj:=create;');
  16993. ConvertProgram;
  16994. CheckSource('TestClassOf_Create',
  16995. LinesToStr([ // statements
  16996. 'rtl.createClass(this, "TObject", null, function () {',
  16997. ' this.$init = function () {',
  16998. ' };',
  16999. ' this.$final = function () {',
  17000. ' };',
  17001. ' this.Create = function () {',
  17002. ' return this;',
  17003. ' };',
  17004. '});',
  17005. 'this.Obj = null;',
  17006. 'this.C = null;'
  17007. ]),
  17008. LinesToStr([ // $mod.$main
  17009. '$mod.Obj = $mod.C.$create("Create");',
  17010. 'var $with = $mod.C;',
  17011. '$mod.Obj = $with.$create("Create");',
  17012. '']));
  17013. end;
  17014. procedure TTestModule.TestClassOf_Call;
  17015. begin
  17016. StartProgram(false);
  17017. Add('type');
  17018. Add(' TObject = class');
  17019. Add(' class procedure DoIt;');
  17020. Add(' end;');
  17021. Add(' TClass = class of TObject;');
  17022. Add('class procedure tobject.doit; begin end;');
  17023. Add('var');
  17024. Add(' C: tclass;');
  17025. Add('begin');
  17026. Add(' c.doit;');
  17027. Add(' with c do doit;');
  17028. ConvertProgram;
  17029. CheckSource('TestClassOf_Call',
  17030. LinesToStr([ // statements
  17031. 'rtl.createClass(this, "TObject", null, function () {',
  17032. ' this.$init = function () {',
  17033. ' };',
  17034. ' this.$final = function () {',
  17035. ' };',
  17036. ' this.DoIt = function () {',
  17037. ' };',
  17038. '});',
  17039. 'this.C = null;'
  17040. ]),
  17041. LinesToStr([ // $mod.$main
  17042. '$mod.C.DoIt();',
  17043. 'var $with = $mod.C;',
  17044. '$with.DoIt();',
  17045. '']));
  17046. end;
  17047. procedure TTestModule.TestClassOf_Assign;
  17048. begin
  17049. StartProgram(false);
  17050. Add('type');
  17051. Add(' TClass = class of TObject;');
  17052. Add(' TObject = class');
  17053. Add(' ClassType: TClass; ');
  17054. Add(' end;');
  17055. Add('var');
  17056. Add(' Obj: tobject;');
  17057. Add(' C: tclass;');
  17058. Add('begin');
  17059. Add(' c:=nil;');
  17060. Add(' c:=obj.classtype;');
  17061. ConvertProgram;
  17062. CheckSource('TestClassOf_Assign',
  17063. LinesToStr([ // statements
  17064. 'rtl.createClass(this, "TObject", null, function () {',
  17065. ' this.$init = function () {',
  17066. ' this.ClassType = null;',
  17067. ' };',
  17068. ' this.$final = function () {',
  17069. ' this.ClassType = undefined;',
  17070. ' };',
  17071. '});',
  17072. 'this.Obj = null;',
  17073. 'this.C = null;'
  17074. ]),
  17075. LinesToStr([ // $mod.$main
  17076. '$mod.C = null;',
  17077. '$mod.C = $mod.Obj.ClassType;',
  17078. '']));
  17079. end;
  17080. procedure TTestModule.TestClassOf_Is;
  17081. begin
  17082. StartProgram(false);
  17083. Add('type');
  17084. Add(' TClass = class of TObject;');
  17085. Add(' TObject = class');
  17086. Add(' end;');
  17087. Add(' TCar = class');
  17088. Add(' end;');
  17089. Add(' TCars = class of TCar;');
  17090. Add('var');
  17091. Add(' Obj: tobject;');
  17092. Add(' C: tclass;');
  17093. Add(' Cars: tcars;');
  17094. Add('begin');
  17095. Add(' if c is tcar then ;');
  17096. Add(' if c is tcars then ;');
  17097. ConvertProgram;
  17098. CheckSource('TestClassOf_Is',
  17099. LinesToStr([ // statements
  17100. 'rtl.createClass(this, "TObject", null, function () {',
  17101. ' this.$init = function () {',
  17102. ' };',
  17103. ' this.$final = function () {',
  17104. ' };',
  17105. '});',
  17106. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17107. '});',
  17108. 'this.Obj = null;',
  17109. 'this.C = null;',
  17110. 'this.Cars = null;'
  17111. ]),
  17112. LinesToStr([ // $mod.$main
  17113. 'if(rtl.is($mod.C,$mod.TCar));',
  17114. 'if(rtl.is($mod.C,$mod.TCar));',
  17115. '']));
  17116. end;
  17117. procedure TTestModule.TestClassOf_Compare;
  17118. begin
  17119. StartProgram(false);
  17120. Add('type');
  17121. Add(' TClass = class of TObject;');
  17122. Add(' TObject = class');
  17123. Add(' ClassType: TClass; ');
  17124. Add(' end;');
  17125. Add('var');
  17126. Add(' b: boolean;');
  17127. Add(' Obj: tobject;');
  17128. Add(' C: tclass;');
  17129. Add('begin');
  17130. Add(' b:=c=nil;');
  17131. Add(' b:=nil=c;');
  17132. Add(' b:=c=obj.classtype;');
  17133. Add(' b:=obj.classtype=c;');
  17134. Add(' b:=c=TObject;');
  17135. Add(' b:=TObject=c;');
  17136. Add(' b:=c<>nil;');
  17137. Add(' b:=nil<>c;');
  17138. Add(' b:=c<>obj.classtype;');
  17139. Add(' b:=obj.classtype<>c;');
  17140. Add(' b:=c<>TObject;');
  17141. Add(' b:=TObject<>c;');
  17142. ConvertProgram;
  17143. CheckSource('TestClassOf_Compare',
  17144. LinesToStr([ // statements
  17145. 'rtl.createClass(this, "TObject", null, function () {',
  17146. ' this.$init = function () {',
  17147. ' this.ClassType = null;',
  17148. ' };',
  17149. ' this.$final = function () {',
  17150. ' this.ClassType = undefined;',
  17151. ' };',
  17152. '});',
  17153. 'this.b = false;',
  17154. 'this.Obj = null;',
  17155. 'this.C = null;'
  17156. ]),
  17157. LinesToStr([ // $mod.$main
  17158. '$mod.b = $mod.C === null;',
  17159. '$mod.b = null === $mod.C;',
  17160. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17161. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17162. '$mod.b = $mod.C === $mod.TObject;',
  17163. '$mod.b = $mod.TObject === $mod.C;',
  17164. '$mod.b = $mod.C !== null;',
  17165. '$mod.b = null !== $mod.C;',
  17166. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17167. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17168. '$mod.b = $mod.C !== $mod.TObject;',
  17169. '$mod.b = $mod.TObject !== $mod.C;',
  17170. '']));
  17171. end;
  17172. procedure TTestModule.TestClassOf_ClassVar;
  17173. begin
  17174. StartProgram(false);
  17175. Add('type');
  17176. Add(' TObject = class');
  17177. Add(' class var id: longint;');
  17178. Add(' end;');
  17179. Add(' TClass = class of TObject;');
  17180. Add('var');
  17181. Add(' C: tclass;');
  17182. Add('begin');
  17183. Add(' C.id:=C.id;');
  17184. ConvertProgram;
  17185. CheckSource('TestClassOf_ClassVar',
  17186. LinesToStr([ // statements
  17187. 'rtl.createClass(this, "TObject", null, function () {',
  17188. ' this.id = 0;',
  17189. ' this.$init = function () {',
  17190. ' };',
  17191. ' this.$final = function () {',
  17192. ' };',
  17193. '});',
  17194. 'this.C = null;'
  17195. ]),
  17196. LinesToStr([ // $mod.$main
  17197. '$mod.TObject.id = $mod.C.id;',
  17198. '']));
  17199. end;
  17200. procedure TTestModule.TestClassOf_ClassMethod;
  17201. begin
  17202. StartProgram(false);
  17203. Add('type');
  17204. Add(' TObject = class');
  17205. Add(' class function DoIt(i: longint = 0): longint;');
  17206. Add(' end;');
  17207. Add(' TClass = class of TObject;');
  17208. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17209. Add('var');
  17210. Add(' i: longint;');
  17211. Add(' C: tclass;');
  17212. Add('begin');
  17213. Add(' C.DoIt;');
  17214. Add(' C.DoIt();');
  17215. Add(' i:=C.DoIt;');
  17216. Add(' i:=C.DoIt();');
  17217. ConvertProgram;
  17218. CheckSource('TestClassOf_ClassMethod',
  17219. LinesToStr([ // statements
  17220. 'rtl.createClass(this, "TObject", null, function () {',
  17221. ' this.$init = function () {',
  17222. ' };',
  17223. ' this.$final = function () {',
  17224. ' };',
  17225. ' this.DoIt = function (i) {',
  17226. ' var Result = 0;',
  17227. ' return Result;',
  17228. ' };',
  17229. '});',
  17230. 'this.i = 0;',
  17231. 'this.C = null;'
  17232. ]),
  17233. LinesToStr([ // $mod.$main
  17234. '$mod.C.DoIt(0);',
  17235. '$mod.C.DoIt(0);',
  17236. '$mod.i = $mod.C.DoIt(0);',
  17237. '$mod.i = $mod.C.DoIt(0);',
  17238. '']));
  17239. end;
  17240. procedure TTestModule.TestClassOf_ClassProperty;
  17241. begin
  17242. StartProgram(false);
  17243. Add([
  17244. 'type',
  17245. ' TObject = class',
  17246. ' class var FA: longint;',
  17247. ' class function GetA: longint;',
  17248. ' class procedure SetA(Value: longint);',
  17249. ' class property pA: longint read fa write fa;',
  17250. ' class property pB: longint read geta write seta;',
  17251. ' end;',
  17252. ' TObjectClass = class of tobject;',
  17253. 'class function tobject.geta: longint; begin end;',
  17254. 'class procedure tobject.seta(value: longint); begin end;',
  17255. 'var',
  17256. ' b: boolean;',
  17257. ' Obj: tobject;',
  17258. ' Cla: tobjectclass;',
  17259. 'begin',
  17260. ' obj.pa:=obj.pa;',
  17261. ' obj.pb:=obj.pb;',
  17262. ' b:=obj.pa=4;',
  17263. ' b:=obj.pb=obj.pb;',
  17264. ' b:=5=obj.pa;',
  17265. ' cla.pa:=6;',
  17266. ' cla.pa:=cla.pa;',
  17267. ' cla.pb:=cla.pb;',
  17268. ' b:=cla.pa=7;',
  17269. ' b:=cla.pb=cla.pb;',
  17270. ' b:=8=cla.pa;',
  17271. ' tobject.pa:=9;',
  17272. ' tobject.pb:=tobject.pb;',
  17273. ' b:=tobject.pa=10;',
  17274. ' b:=11=tobject.pa;',
  17275. '']);
  17276. ConvertProgram;
  17277. CheckSource('TestClassOf_ClassProperty',
  17278. LinesToStr([ // statements
  17279. 'rtl.createClass(this, "TObject", null, function () {',
  17280. ' this.FA = 0;',
  17281. ' this.$init = function () {',
  17282. ' };',
  17283. ' this.$final = function () {',
  17284. ' };',
  17285. ' this.GetA = function () {',
  17286. ' var Result = 0;',
  17287. ' return Result;',
  17288. ' };',
  17289. ' this.SetA = function (Value) {',
  17290. ' };',
  17291. '});',
  17292. 'this.b = false;',
  17293. 'this.Obj = null;',
  17294. 'this.Cla = null;'
  17295. ]),
  17296. LinesToStr([ // $mod.$main
  17297. '$mod.TObject.FA = $mod.Obj.FA;',
  17298. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17299. '$mod.b = $mod.Obj.FA === 4;',
  17300. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17301. '$mod.b = 5 === $mod.Obj.FA;',
  17302. '$mod.TObject.FA = 6;',
  17303. '$mod.TObject.FA = $mod.Cla.FA;',
  17304. '$mod.Cla.SetA($mod.Cla.GetA());',
  17305. '$mod.b = $mod.Cla.FA === 7;',
  17306. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17307. '$mod.b = 8 === $mod.Cla.FA;',
  17308. '$mod.TObject.FA = 9;',
  17309. '$mod.TObject.SetA($mod.TObject.GetA());',
  17310. '$mod.b = $mod.TObject.FA === 10;',
  17311. '$mod.b = 11 === $mod.TObject.FA;',
  17312. '']));
  17313. end;
  17314. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17315. begin
  17316. StartProgram(false);
  17317. Add('type');
  17318. Add(' TObject = class');
  17319. Add(' class var GlobalId: longint;');
  17320. Add(' class procedure ProcA;');
  17321. Add(' end;');
  17322. Add('class procedure tobject.proca;');
  17323. Add('var b: boolean;');
  17324. Add('begin');
  17325. Add(' b:=self=nil;');
  17326. Add(' b:=self.globalid=3;');
  17327. Add(' b:=4=self.globalid;');
  17328. Add(' self.globalid:=5;');
  17329. Add(' self.proca;');
  17330. Add('end;');
  17331. Add('begin');
  17332. ConvertProgram;
  17333. CheckSource('TestClassOf_ClassMethodSelf',
  17334. LinesToStr([ // statements
  17335. 'rtl.createClass(this, "TObject", null, function () {',
  17336. ' this.GlobalId = 0;',
  17337. ' this.$init = function () {',
  17338. ' };',
  17339. ' this.$final = function () {',
  17340. ' };',
  17341. ' this.ProcA = function () {',
  17342. ' var b = false;',
  17343. ' b = this === null;',
  17344. ' b = this.GlobalId === 3;',
  17345. ' b = 4 === this.GlobalId;',
  17346. ' $mod.TObject.GlobalId = 5;',
  17347. ' this.ProcA();',
  17348. ' };',
  17349. '});'
  17350. ]),
  17351. LinesToStr([ // $mod.$main
  17352. '']));
  17353. end;
  17354. procedure TTestModule.TestClassOf_TypeCast;
  17355. begin
  17356. StartProgram(false);
  17357. Add('type');
  17358. Add(' TObject = class');
  17359. Add(' class procedure {#TObject_DoIt}DoIt;');
  17360. Add(' end;');
  17361. Add(' TClass = class of TObject;');
  17362. Add(' TMobile = class');
  17363. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17364. Add(' end;');
  17365. Add(' TMobileClass = class of TMobile;');
  17366. Add(' TCar = class(TMobile)');
  17367. Add(' class procedure {#TCar_DoIt}DoIt;');
  17368. Add(' end;');
  17369. Add(' TCarClass = class of TCar;');
  17370. Add('class procedure TObject.DoIt;');
  17371. Add('begin');
  17372. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17373. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17374. Add('end;');
  17375. Add('class procedure TMobile.DoIt;');
  17376. Add('begin');
  17377. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17378. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17379. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17380. Add('end;');
  17381. Add('class procedure TCar.DoIt; begin end;');
  17382. Add('var');
  17383. Add(' ObjC: TClass;');
  17384. Add(' MobileC: TMobileClass;');
  17385. Add(' CarC: TCarClass;');
  17386. Add('begin');
  17387. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17388. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17389. Add(' CarC.{@TCar_DoIt}DoIt;');
  17390. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17391. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17392. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17393. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17394. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17395. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17396. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17397. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17398. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17399. ConvertProgram;
  17400. CheckSource('TestClassOf_TypeCast',
  17401. LinesToStr([ // statements
  17402. 'rtl.createClass(this, "TObject", null, function () {',
  17403. ' this.$init = function () {',
  17404. ' };',
  17405. ' this.$final = function () {',
  17406. ' };',
  17407. ' this.DoIt = function () {',
  17408. ' this.DoIt();',
  17409. ' this.DoIt$1();',
  17410. ' };',
  17411. '});',
  17412. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17413. ' this.DoIt$1 = function () {',
  17414. ' this.DoIt();',
  17415. ' this.DoIt$1();',
  17416. ' this.DoIt$2();',
  17417. ' };',
  17418. '});',
  17419. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17420. ' this.DoIt$2 = function () {',
  17421. ' };',
  17422. '});',
  17423. 'this.ObjC = null;',
  17424. 'this.MobileC = null;',
  17425. 'this.CarC = null;',
  17426. '']),
  17427. LinesToStr([ // $mod.$main
  17428. '$mod.ObjC.DoIt();',
  17429. '$mod.MobileC.DoIt$1();',
  17430. '$mod.CarC.DoIt$2();',
  17431. '$mod.ObjC.DoIt();',
  17432. '$mod.ObjC.DoIt$1();',
  17433. '$mod.ObjC.DoIt$2();',
  17434. '$mod.MobileC.DoIt();',
  17435. '$mod.MobileC.DoIt$1();',
  17436. '$mod.MobileC.DoIt$2();',
  17437. '$mod.CarC.DoIt();',
  17438. '$mod.CarC.DoIt$1();',
  17439. '$mod.CarC.DoIt$2();',
  17440. '']));
  17441. end;
  17442. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17443. begin
  17444. StartProgram(false);
  17445. Add('type');
  17446. Add(' TObject = class');
  17447. Add(' function CurNow: longint; ');
  17448. Add(' class function Now: longint; ');
  17449. Add(' end;');
  17450. Add('function TObject.CurNow: longint; begin end;');
  17451. Add('class function TObject.Now: longint; begin end;');
  17452. Add('var');
  17453. Add(' Obj: tobject;');
  17454. Add(' vI: longint;');
  17455. Add('begin');
  17456. Add(' obj.curnow;');
  17457. Add(' vi:=obj.curnow;');
  17458. Add(' tobject.now;');
  17459. Add(' vi:=tobject.now;');
  17460. ConvertProgram;
  17461. CheckSource('TestClassOf_ImplicitFunctionCall',
  17462. LinesToStr([ // statements
  17463. 'rtl.createClass(this, "TObject", null, function () {',
  17464. ' this.$init = function () {',
  17465. ' };',
  17466. ' this.$final = function () {',
  17467. ' };',
  17468. ' this.CurNow = function () {',
  17469. ' var Result = 0;',
  17470. ' return Result;',
  17471. ' };',
  17472. ' this.Now = function () {',
  17473. ' var Result = 0;',
  17474. ' return Result;',
  17475. ' };',
  17476. '});',
  17477. 'this.Obj = null;',
  17478. 'this.vI = 0;',
  17479. '']),
  17480. LinesToStr([ // $mod.$main
  17481. '$mod.Obj.CurNow();',
  17482. '$mod.vI = $mod.Obj.CurNow();',
  17483. '$mod.TObject.Now();',
  17484. '$mod.vI = $mod.TObject.Now();',
  17485. '']));
  17486. end;
  17487. procedure TTestModule.TestClassOf_Const;
  17488. begin
  17489. StartProgram(false);
  17490. Add([
  17491. 'type',
  17492. ' TObject = class',
  17493. ' end;',
  17494. ' TBird = TObject;',
  17495. ' TBirds = class of TBird;',
  17496. ' TEagles = TBirds;',
  17497. ' THawk = class(TBird);',
  17498. 'const',
  17499. ' Hawk: TEagles = THawk;',
  17500. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17501. ' TBird,',
  17502. ' THawk',
  17503. ' );',
  17504. 'begin']);
  17505. ConvertProgram;
  17506. CheckSource('TestClassOf_Const',
  17507. LinesToStr([ // statements
  17508. 'rtl.createClass(this, "TObject", null, function () {',
  17509. ' this.$init = function () {',
  17510. ' };',
  17511. ' this.$final = function () {',
  17512. ' };',
  17513. '});',
  17514. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17515. '});',
  17516. 'this.Hawk = this.THawk;',
  17517. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17518. '']),
  17519. LinesToStr([ // $mod.$main
  17520. '']));
  17521. end;
  17522. procedure TTestModule.TestNestedClass_Alias;
  17523. begin
  17524. WithTypeInfo:=true;
  17525. StartProgram(false);
  17526. Add([
  17527. 'type',
  17528. ' TObject = class',
  17529. ' type TNested = type longint;',
  17530. ' end;',
  17531. 'type TAlias = type tobject.tnested;',
  17532. 'var i: tobject.tnested = 3;',
  17533. 'var j: TAlias = 4;',
  17534. 'begin',
  17535. ' if typeinfo(TAlias)=nil then ;',
  17536. ' if typeinfo(tobject.tnested)=nil then ;',
  17537. '']);
  17538. ConvertProgram;
  17539. CheckSource('TestNestedClass_Alias',
  17540. LinesToStr([ // statements
  17541. 'rtl.createClass(this, "TObject", null, function () {',
  17542. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17543. ' this.$init = function () {',
  17544. ' };',
  17545. ' this.$final = function () {',
  17546. ' };',
  17547. '});',
  17548. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17549. 'this.i = 3;',
  17550. 'this.j = 4;',
  17551. '']),
  17552. LinesToStr([ // $mod.$main
  17553. 'if ($mod.$rtti["TAlias"] === null) ;',
  17554. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17555. '']));
  17556. end;
  17557. procedure TTestModule.TestNestedClass_Record;
  17558. begin
  17559. WithTypeInfo:=true;
  17560. StartProgram(false);
  17561. Add([
  17562. 'type',
  17563. ' TObject = class',
  17564. ' type TPoint = record',
  17565. ' x,y: byte;',
  17566. ' end;',
  17567. ' procedure DoIt(t: TPoint);',
  17568. ' end;',
  17569. 'procedure tobject.DoIt(t: TPoint);',
  17570. 'var p: TPoint;',
  17571. 'begin',
  17572. ' t.x:=t.y;',
  17573. ' p:=t;',
  17574. 'end;',
  17575. 'var',
  17576. ' p: tobject.tpoint = (x:2; y:4);',
  17577. ' o: TObject;',
  17578. 'begin',
  17579. ' p:=p;',
  17580. ' o.doit(p);',
  17581. '']);
  17582. ConvertProgram;
  17583. CheckSource('TestNestedClass_Record',
  17584. LinesToStr([ // statements
  17585. 'rtl.createClass(this, "TObject", null, function () {',
  17586. ' rtl.recNewT(this, "TPoint", function () {',
  17587. ' this.x = 0;',
  17588. ' this.y = 0;',
  17589. ' this.$eq = function (b) {',
  17590. ' return (this.x === b.x) && (this.y === b.y);',
  17591. ' };',
  17592. ' this.$assign = function (s) {',
  17593. ' this.x = s.x;',
  17594. ' this.y = s.y;',
  17595. ' return this;',
  17596. ' };',
  17597. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17598. ' $r.addField("x", rtl.byte);',
  17599. ' $r.addField("y", rtl.byte);',
  17600. ' });',
  17601. ' this.$init = function () {',
  17602. ' };',
  17603. ' this.$final = function () {',
  17604. ' };',
  17605. ' this.DoIt = function (t) {',
  17606. ' var p = this.TPoint.$new();',
  17607. ' t.x = t.y;',
  17608. ' p.$assign(t);',
  17609. ' };',
  17610. '});',
  17611. 'this.p = this.TObject.TPoint.$clone({',
  17612. ' x: 2,',
  17613. ' y: 4',
  17614. '});',
  17615. 'this.o = null;',
  17616. '']),
  17617. LinesToStr([ // $mod.$main
  17618. '$mod.p.$assign($mod.p);',
  17619. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17620. '']));
  17621. end;
  17622. procedure TTestModule.TestNestedClass_Class;
  17623. begin
  17624. WithTypeInfo:=true;
  17625. StartProgram(false);
  17626. Add([
  17627. 'type',
  17628. ' TObject = class end;',
  17629. ' TBird = class',
  17630. ' type TLeg = class',
  17631. ' FId: longint;',
  17632. ' constructor Create;',
  17633. ' function Create(i: longint): TLeg;',
  17634. ' end;',
  17635. ' function DoIt(b: TBird): Tleg;',
  17636. ' end;',
  17637. 'constructor tbird.tleg.create;',
  17638. 'begin',
  17639. ' FId:=3;',
  17640. 'end;',
  17641. 'function tbird.tleg.Create(i: longint): TLeg;',
  17642. 'begin',
  17643. ' Create;',
  17644. ' Result:=TLeg.Create;',
  17645. ' Result:=TBird.TLeg.Create;',
  17646. ' Result:=Create(3);',
  17647. ' FId:=i;',
  17648. 'end;',
  17649. 'function tbird.DoIt(b: tbird): tleg;',
  17650. 'begin',
  17651. ' Result.Create;',
  17652. ' Result:=TLeg.Create;',
  17653. ' Result:=TBird.TLeg.Create;',
  17654. ' Result:=Result.Create(3);',
  17655. 'end;',
  17656. 'var',
  17657. ' b: Tbird.tleg;',
  17658. 'begin',
  17659. ' b.Create;',
  17660. ' b:=TBird.TLeg.Create;',
  17661. ' b:=b.Create(3);',
  17662. '']);
  17663. ConvertProgram;
  17664. CheckSource('TestNestedClass_Class',
  17665. LinesToStr([ // statements
  17666. 'rtl.createClass(this, "TObject", null, function () {',
  17667. ' this.$init = function () {',
  17668. ' };',
  17669. ' this.$final = function () {',
  17670. ' };',
  17671. '});',
  17672. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17673. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  17674. ' this.$init = function () {',
  17675. ' $mod.TObject.$init.call(this);',
  17676. ' this.FId = 0;',
  17677. ' };',
  17678. ' this.Create = function () {',
  17679. ' this.FId = 3;',
  17680. ' return this;',
  17681. ' };',
  17682. ' this.Create$1 = function (i) {',
  17683. ' var Result = null;',
  17684. ' this.Create();',
  17685. ' Result = $mod.TBird.TLeg.$create("Create");',
  17686. ' Result = $mod.TBird.TLeg.$create("Create");',
  17687. ' Result = this.Create$1(3);',
  17688. ' this.FId = i;',
  17689. ' return Result;',
  17690. ' };',
  17691. ' }, "TBird.TLeg");',
  17692. ' this.DoIt = function (b) {',
  17693. ' var Result = null;',
  17694. ' Result.Create();',
  17695. ' Result = this.TLeg.$create("Create");',
  17696. ' Result = $mod.TBird.TLeg.$create("Create");',
  17697. ' Result = Result.Create$1(3);',
  17698. ' return Result;',
  17699. ' };',
  17700. '});',
  17701. 'this.b = null;',
  17702. '']),
  17703. LinesToStr([ // $mod.$main
  17704. '$mod.b.Create();',
  17705. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  17706. '$mod.b = $mod.b.Create$1(3);',
  17707. '']));
  17708. end;
  17709. procedure TTestModule.TestExternalClass_Var;
  17710. begin
  17711. StartProgram(false);
  17712. Add([
  17713. '{$modeswitch externalclass}',
  17714. 'type',
  17715. ' TExtA = class external name ''ExtObj''',
  17716. ' Id: longint external name ''$Id'';',
  17717. ' B: longint;',
  17718. ' end;',
  17719. 'var Obj: TExtA;',
  17720. 'begin',
  17721. ' obj.id:=obj.id+1;',
  17722. ' obj.B:=obj.B+1;']);
  17723. ConvertProgram;
  17724. CheckSource('TestExternalClass_Var',
  17725. LinesToStr([ // statements
  17726. 'this.Obj = null;',
  17727. '']),
  17728. LinesToStr([ // $mod.$main
  17729. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  17730. '$mod.Obj.B = $mod.Obj.B + 1;',
  17731. '']));
  17732. end;
  17733. procedure TTestModule.TestExternalClass_Const;
  17734. begin
  17735. StartProgram(false);
  17736. Add([
  17737. '{$modeswitch externalclass}',
  17738. 'type',
  17739. ' TExtA = class external name ''ExtObj''',
  17740. ' const Two: longint = 2;',
  17741. ' const Three = 3;',
  17742. ' const Id: longint;',
  17743. ' end;',
  17744. ' TExtB = class external name ''ExtB''',
  17745. ' A: TExtA;',
  17746. ' end;',
  17747. 'var',
  17748. ' A: texta;',
  17749. ' B: textb;',
  17750. ' i: longint;',
  17751. 'begin',
  17752. ' i:=a.two;',
  17753. ' i:=texta.two;',
  17754. ' i:=a.three;',
  17755. ' i:=texta.three;',
  17756. ' i:=a.id;',
  17757. ' i:=texta.id;',
  17758. '']);
  17759. ConvertProgram;
  17760. CheckSource('TestExternalClass_Const',
  17761. LinesToStr([ // statements
  17762. 'this.A = null;',
  17763. 'this.B = null;',
  17764. 'this.i = 0;',
  17765. '']),
  17766. LinesToStr([ // $mod.$main
  17767. '$mod.i = 2;',
  17768. '$mod.i = 2;',
  17769. '$mod.i = 3;',
  17770. '$mod.i = 3;',
  17771. '$mod.i = $mod.A.Id;',
  17772. '$mod.i = ExtObj.Id;',
  17773. '']));
  17774. end;
  17775. procedure TTestModule.TestExternalClass_Dollar;
  17776. begin
  17777. StartProgram(false);
  17778. Add([
  17779. '{$modeswitch externalclass}',
  17780. 'type',
  17781. ' TExtA = class external name ''$''',
  17782. ' Id: longint external name ''$'';',
  17783. ' function Bla(i: longint): longint; external name ''$'';',
  17784. ' end;',
  17785. 'function dollar(k: longint): longint; external name ''$'';',
  17786. 'var Obj: TExtA;',
  17787. 'begin',
  17788. ' dollar(1);',
  17789. ' obj.id:=obj.id+2;',
  17790. ' obj.Bla(3);',
  17791. '']);
  17792. ConvertProgram;
  17793. CheckSource('TestExternalClass_Dollar',
  17794. LinesToStr([ // statements
  17795. 'this.Obj = null;',
  17796. '']),
  17797. LinesToStr([ // $mod.$main
  17798. '$(1);',
  17799. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  17800. '$mod.Obj.$(3);',
  17801. '']));
  17802. end;
  17803. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  17804. begin
  17805. StartProgram(false);
  17806. Add('{$modeswitch externalclass}');
  17807. Add('type');
  17808. Add(' TExtA = class external name ''ExtA''');
  17809. Add(' Id: longint external name ''$Id'';');
  17810. Add(' end;');
  17811. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  17812. Add(' Id: longint;');
  17813. Add(' end;');
  17814. Add('begin');
  17815. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  17816. ConvertProgram;
  17817. end;
  17818. procedure TTestModule.TestExternalClass_Method;
  17819. begin
  17820. StartProgram(false);
  17821. Add(['{$modeswitch externalclass}',
  17822. 'type',
  17823. ' TExtA = class external name ''ExtObj''',
  17824. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  17825. ' procedure DoSome(Id: longint = 1);',
  17826. ' end;',
  17827. 'var Obj: texta;',
  17828. 'begin',
  17829. ' obj.doit;',
  17830. ' obj.doit();',
  17831. ' obj.doit(2);',
  17832. ' with obj do begin',
  17833. ' doit;',
  17834. ' doit();',
  17835. ' doit(3);',
  17836. ' end;']);
  17837. ConvertProgram;
  17838. CheckSource('TestExternalClass_Method',
  17839. LinesToStr([ // statements
  17840. 'this.Obj = null;',
  17841. '']),
  17842. LinesToStr([ // $mod.$main
  17843. '$mod.Obj.$Execute(1);',
  17844. '$mod.Obj.$Execute(1);',
  17845. '$mod.Obj.$Execute(2);',
  17846. 'var $with = $mod.Obj;',
  17847. '$with.$Execute(1);',
  17848. '$with.$Execute(1);',
  17849. '$with.$Execute(3);',
  17850. '']));
  17851. end;
  17852. procedure TTestModule.TestExternalClass_ClassMethod;
  17853. begin
  17854. StartProgram(false);
  17855. Add([
  17856. '{$modeswitch externalclass}',
  17857. 'type',
  17858. ' TExtA = class external name ''ExtObj''',
  17859. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  17860. ' end;',
  17861. ' TExtB = TExtA;',
  17862. 'var p: Pointer;',
  17863. 'begin',
  17864. ' texta.doit;',
  17865. ' texta.doit();',
  17866. ' texta.doit(2);',
  17867. ' p:[email protected];',
  17868. ' with texta do begin',
  17869. ' doit;',
  17870. ' doit();',
  17871. ' doit(3);',
  17872. ' p:=@DoIt;',
  17873. ' end;',
  17874. ' textb.doit;',
  17875. ' textb.doit();',
  17876. ' textb.doit(4);',
  17877. ' with textb do begin',
  17878. ' doit;',
  17879. ' doit();',
  17880. ' doit(5);',
  17881. ' end;',
  17882. '']);
  17883. ConvertProgram;
  17884. CheckSource('TestExternalClass_ClassMethod',
  17885. LinesToStr([ // statements
  17886. 'this.p = null;',
  17887. '']),
  17888. LinesToStr([ // $mod.$main
  17889. 'ExtObj.$Execute(1);',
  17890. 'ExtObj.$Execute(1);',
  17891. 'ExtObj.$Execute(2);',
  17892. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  17893. 'ExtObj.$Execute(1);',
  17894. 'ExtObj.$Execute(1);',
  17895. 'ExtObj.$Execute(3);',
  17896. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  17897. 'ExtObj.$Execute(1);',
  17898. 'ExtObj.$Execute(1);',
  17899. 'ExtObj.$Execute(4);',
  17900. 'ExtObj.$Execute(1);',
  17901. 'ExtObj.$Execute(1);',
  17902. 'ExtObj.$Execute(5);',
  17903. '']));
  17904. end;
  17905. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  17906. begin
  17907. StartProgram(false);
  17908. Add([
  17909. '{$modeswitch externalclass}',
  17910. 'type',
  17911. ' TExtA = class external name ''ExtObj''',
  17912. ' class procedure DoIt(Id: longint = 1); static;',
  17913. ' end;',
  17914. 'var p: Pointer;',
  17915. 'begin',
  17916. ' texta.doit;',
  17917. ' texta.doit();',
  17918. ' texta.doit(2);',
  17919. ' p:[email protected];',
  17920. ' with texta do begin',
  17921. ' doit;',
  17922. ' doit();',
  17923. ' doit(3);',
  17924. ' p:=@DoIt;',
  17925. ' end;',
  17926. '']);
  17927. ConvertProgram;
  17928. CheckSource('TestExternalClass_ClassMethodStatic',
  17929. LinesToStr([ // statements
  17930. 'this.p = null;',
  17931. '']),
  17932. LinesToStr([ // $mod.$main
  17933. 'ExtObj.DoIt(1);',
  17934. 'ExtObj.DoIt(1);',
  17935. 'ExtObj.DoIt(2);',
  17936. '$mod.p = ExtObj.DoIt;',
  17937. 'ExtObj.DoIt(1);',
  17938. 'ExtObj.DoIt(1);',
  17939. 'ExtObj.DoIt(3);',
  17940. '$mod.p = ExtObj.DoIt;',
  17941. '']));
  17942. end;
  17943. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  17944. begin
  17945. StartProgram(false);
  17946. Add([
  17947. '{$modeswitch externalclass}',
  17948. 'type',
  17949. ' TBird = class external name ''Array''',
  17950. ' end;',
  17951. 'function GetPtr: Pointer;',
  17952. 'begin',
  17953. 'end;',
  17954. 'procedure Write(const p);',
  17955. 'begin',
  17956. 'end;',
  17957. 'procedure WriteLn; varargs;',
  17958. 'begin',
  17959. 'end;',
  17960. 'begin',
  17961. ' if TBird(GetPtr)=nil then ;',
  17962. ' Write(GetPtr);',
  17963. ' WriteLn(GetPtr);',
  17964. ' Write(TBird(GetPtr));',
  17965. ' WriteLn(TBird(GetPtr));',
  17966. '']);
  17967. ConvertProgram;
  17968. CheckSource('TestFunctionResultInTypeCast',
  17969. LinesToStr([ // statements
  17970. 'this.GetPtr = function () {',
  17971. ' var Result = null;',
  17972. ' return Result;',
  17973. '};',
  17974. 'this.Write = function (p) {',
  17975. '};',
  17976. 'this.WriteLn = function () {',
  17977. '};',
  17978. '']),
  17979. LinesToStr([
  17980. 'if ($mod.GetPtr() === null) ;',
  17981. '$mod.Write($mod.GetPtr());',
  17982. '$mod.WriteLn($mod.GetPtr());',
  17983. '$mod.Write($mod.GetPtr());',
  17984. '$mod.WriteLn($mod.GetPtr());',
  17985. '']));
  17986. end;
  17987. procedure TTestModule.TestExternalClass_NonExternalOverride;
  17988. begin
  17989. StartProgram(false);
  17990. Add([
  17991. '{$modeswitch externalclass}',
  17992. 'type',
  17993. ' TExtA = class external name ''ExtObjA''',
  17994. ' procedure ProcA; virtual;',
  17995. ' procedure ProcB; virtual;',
  17996. ' end;',
  17997. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  17998. ' end;',
  17999. ' TExtC = class (TExtB)',
  18000. ' procedure ProcA; override;',
  18001. ' end;',
  18002. 'procedure TExtC.ProcA;',
  18003. 'begin',
  18004. ' ProcA;',
  18005. ' Self.ProcA;',
  18006. ' ProcB;',
  18007. ' Self.ProcB;',
  18008. 'end;',
  18009. 'var',
  18010. ' A: texta;',
  18011. ' B: textb;',
  18012. ' C: textc;',
  18013. 'begin',
  18014. ' a.proca;',
  18015. ' b.proca;',
  18016. ' c.proca;']);
  18017. ConvertProgram;
  18018. CheckSource('TestExternalClass_NonExternalOverride',
  18019. LinesToStr([ // statements
  18020. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18021. ' this.$init = function () {',
  18022. ' };',
  18023. ' this.$final = function () {',
  18024. ' };',
  18025. ' this.ProcA = function () {',
  18026. ' this.ProcA();',
  18027. ' this.ProcA();',
  18028. ' this.ProcB();',
  18029. ' this.ProcB();',
  18030. ' };',
  18031. '});',
  18032. 'this.A = null;',
  18033. 'this.B = null;',
  18034. 'this.C = null;',
  18035. '']),
  18036. LinesToStr([ // $mod.$main
  18037. '$mod.A.ProcA();',
  18038. '$mod.B.ProcA();',
  18039. '$mod.C.ProcA();',
  18040. '']));
  18041. end;
  18042. procedure TTestModule.TestExternalClass_OverloadHint;
  18043. begin
  18044. StartProgram(false);
  18045. Add([
  18046. '{$modeswitch externalclass}',
  18047. 'type',
  18048. ' TExtA = class external name ''ExtObjA''',
  18049. ' procedure DoIt;',
  18050. ' procedure DoIt(i: longint);',
  18051. ' end;',
  18052. 'begin',
  18053. '']);
  18054. ConvertProgram;
  18055. CheckResolverUnexpectedHints(true);
  18056. CheckSource('TestExternalClass_OverloadHint',
  18057. LinesToStr([ // statements
  18058. '']),
  18059. LinesToStr([ // $mod.$main
  18060. '']));
  18061. end;
  18062. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18063. begin
  18064. WithTypeInfo:=true;
  18065. StartProgram(false);
  18066. Add([
  18067. '{$modeswitch externalclass}',
  18068. 'type',
  18069. ' JSwiper = class external name ''Swiper''',
  18070. ' constructor New;',
  18071. ' end;',
  18072. ' TObject = class',
  18073. ' private',
  18074. ' FSwiper: JSwiper;',
  18075. ' published',
  18076. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18077. ' end;',
  18078. 'begin',
  18079. ' JSwiper.new;',
  18080. '']);
  18081. ConvertProgram;
  18082. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18083. LinesToStr([ // statements
  18084. 'this.$rtti.$ExtClass("JSwiper", {',
  18085. ' jsclass: "Swiper"',
  18086. '});',
  18087. 'rtl.createClass(this, "TObject", null, function () {',
  18088. ' this.$init = function () {',
  18089. ' this.FSwiper = null;',
  18090. ' };',
  18091. ' this.$final = function () {',
  18092. ' this.FSwiper = undefined;',
  18093. ' };',
  18094. ' var $r = this.$rtti;',
  18095. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18096. '});',
  18097. '']),
  18098. LinesToStr([ // $mod.$main
  18099. 'new Swiper();',
  18100. '']));
  18101. end;
  18102. procedure TTestModule.TestExternalClass_Property;
  18103. begin
  18104. StartProgram(false);
  18105. Add([
  18106. '{$modeswitch externalclass}',
  18107. 'type',
  18108. ' TExtA = class external name ''ExtA''',
  18109. ' function getYear: longint;',
  18110. ' procedure setYear(Value: longint);',
  18111. ' property Year: longint read getyear write setyear;',
  18112. ' end;',
  18113. ' TExtB = class (TExtA)',
  18114. ' procedure OtherSetYear(Value: longint);',
  18115. ' property year write othersetyear;',
  18116. ' end;',
  18117. 'procedure textb.othersetyear(value: longint);',
  18118. 'begin',
  18119. ' setYear(Value+4);',
  18120. 'end;',
  18121. 'var',
  18122. ' A: texta;',
  18123. ' B: textb;',
  18124. 'begin',
  18125. ' a.year:=a.year+1;',
  18126. ' b.year:=b.year+2;']);
  18127. ConvertProgram;
  18128. CheckSource('TestExternalClass_NonExternalOverride',
  18129. LinesToStr([ // statements
  18130. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18131. ' this.$init = function () {',
  18132. ' };',
  18133. ' this.$final = function () {',
  18134. ' };',
  18135. ' this.OtherSetYear = function (Value) {',
  18136. ' this.setYear(Value+4);',
  18137. ' };',
  18138. '});',
  18139. 'this.A = null;',
  18140. 'this.B = null;',
  18141. '']),
  18142. LinesToStr([ // $mod.$main
  18143. '$mod.A.setYear($mod.A.getYear()+1);',
  18144. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18145. '']));
  18146. end;
  18147. procedure TTestModule.TestExternalClass_PropertyDate;
  18148. begin
  18149. StartProgram(false);
  18150. Add([
  18151. '{$modeswitch externalclass}',
  18152. 'type',
  18153. ' TExtA = class external name ''ExtA''',
  18154. ' end;',
  18155. ' TExtB = class (TExtA)',
  18156. ' FDate: string;',
  18157. ' property Date: string read FDate write FDate;',
  18158. ' property ExtA: string read FDate write FDate;',
  18159. ' end;',
  18160. ' {$M+}',
  18161. ' TObject = class',
  18162. ' FDate: string;',
  18163. ' published',
  18164. ' property Date: string read FDate write FDate;',
  18165. ' property ExtA: string read FDate write FDate;',
  18166. ' end;',
  18167. 'var',
  18168. ' B: textb;',
  18169. ' o: TObject;',
  18170. 'begin',
  18171. ' b.date:=b.exta;',
  18172. ' o.date:=o.exta;']);
  18173. ConvertProgram;
  18174. CheckSource('TestExternalClass_PropertyDate',
  18175. LinesToStr([ // statements
  18176. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18177. ' this.$init = function () {',
  18178. ' this.FDate = "";',
  18179. ' };',
  18180. ' this.$final = function () {',
  18181. ' };',
  18182. '});',
  18183. 'rtl.createClass(this, "TObject", null, function () {',
  18184. ' this.$init = function () {',
  18185. ' this.FDate = "";',
  18186. ' };',
  18187. ' this.$final = function () {',
  18188. ' };',
  18189. ' var $r = this.$rtti;',
  18190. ' $r.addField("FDate", rtl.string);',
  18191. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18192. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18193. '});',
  18194. 'this.B = null;',
  18195. 'this.o = null;',
  18196. '']),
  18197. LinesToStr([ // $mod.$main
  18198. '$mod.B.FDate = $mod.B.FDate;',
  18199. '$mod.o.FDate = $mod.o.FDate;',
  18200. '']));
  18201. end;
  18202. procedure TTestModule.TestExternalClass_ClassProperty;
  18203. begin
  18204. StartProgram(false);
  18205. Add('{$modeswitch externalclass}');
  18206. Add('type');
  18207. Add(' TExtA = class external name ''ExtA''');
  18208. Add(' class function getYear: longint;');
  18209. Add(' class procedure setYear(Value: longint);');
  18210. Add(' class property Year: longint read getyear write setyear;');
  18211. Add(' end;');
  18212. Add(' TExtB = class (TExtA)');
  18213. Add(' class function GetCentury: longint;');
  18214. Add(' class procedure SetCentury(Value: longint);');
  18215. Add(' class property Century: longint read getcentury write setcentury;');
  18216. Add(' end;');
  18217. Add('class function textb.getcentury: longint;');
  18218. Add('begin');
  18219. Add('end;');
  18220. Add('class procedure textb.setcentury(value: longint);');
  18221. Add('begin');
  18222. Add(' setyear(value+11);');
  18223. Add(' texta.year:=texta.year+12;');
  18224. Add(' year:=year+13;');
  18225. Add(' textb.century:=textb.century+14;');
  18226. Add(' century:=century+15;');
  18227. Add('end;');
  18228. Add('var');
  18229. Add(' A: texta;');
  18230. Add(' B: textb;');
  18231. Add('begin');
  18232. Add(' texta.year:=texta.year+1;');
  18233. Add(' textb.year:=textb.year+2;');
  18234. Add(' TextA.year:=TextA.year+3;');
  18235. Add(' b.year:=b.year+4;');
  18236. Add(' textb.century:=textb.century+5;');
  18237. Add(' b.century:=b.century+6;');
  18238. ConvertProgram;
  18239. CheckSource('TestExternalClass_ClassProperty',
  18240. LinesToStr([ // statements
  18241. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18242. ' this.$init = function () {',
  18243. ' };',
  18244. ' this.$final = function () {',
  18245. ' };',
  18246. ' this.GetCentury = function () {',
  18247. ' var Result = 0;',
  18248. ' return Result;',
  18249. ' };',
  18250. ' this.SetCentury = function (Value) {',
  18251. ' this.setYear(Value + 11);',
  18252. ' ExtA.setYear(ExtA.getYear() + 12);',
  18253. ' this.setYear(this.getYear() + 13);',
  18254. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18255. ' this.SetCentury(this.GetCentury() + 15);',
  18256. ' };',
  18257. '});',
  18258. 'this.A = null;',
  18259. 'this.B = null;',
  18260. '']),
  18261. LinesToStr([ // $mod.$main
  18262. 'ExtA.setYear(ExtA.getYear() + 1);',
  18263. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18264. 'ExtA.setYear(ExtA.getYear() + 3);',
  18265. '$mod.B.setYear($mod.B.getYear() + 4);',
  18266. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18267. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18268. '']));
  18269. end;
  18270. procedure TTestModule.TestExternalClass_ClassOf;
  18271. begin
  18272. StartProgram(false);
  18273. Add('{$modeswitch externalclass}');
  18274. Add('type');
  18275. Add(' TExtA = class external name ''ExtA''');
  18276. Add(' procedure ProcA; virtual;');
  18277. Add(' procedure ProcB; virtual;');
  18278. Add(' end;');
  18279. Add(' TExtAClass = class of TExtA;');
  18280. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18281. Add(' end;');
  18282. Add(' TExtBClass = class of TExtB;');
  18283. Add(' TExtC = class (TExtB)');
  18284. Add(' procedure ProcA; override;');
  18285. Add(' end;');
  18286. Add(' TExtCClass = class of TExtC;');
  18287. Add('procedure TExtC.ProcA; begin end;');
  18288. Add('var');
  18289. Add(' A: texta; ClA: TExtAClass;');
  18290. Add(' B: textb; ClB: TExtBClass;');
  18291. Add(' C: textc; ClC: TExtCClass;');
  18292. Add('begin');
  18293. Add(' ClA:=texta;');
  18294. Add(' ClA:=textb;');
  18295. Add(' ClA:=textc;');
  18296. Add(' ClB:=textb;');
  18297. Add(' ClB:=textc;');
  18298. Add(' ClC:=textc;');
  18299. ConvertProgram;
  18300. CheckSource('TestExternalClass_ClassOf',
  18301. LinesToStr([ // statements
  18302. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18303. ' this.$init = function () {',
  18304. ' };',
  18305. ' this.$final = function () {',
  18306. ' };',
  18307. ' this.ProcA = function () {',
  18308. ' };',
  18309. '});',
  18310. 'this.A = null;',
  18311. 'this.ClA = null;',
  18312. 'this.B = null;',
  18313. 'this.ClB = null;',
  18314. 'this.C = null;',
  18315. 'this.ClC = null;',
  18316. '']),
  18317. LinesToStr([ // $mod.$main
  18318. '$mod.ClA = ExtA;',
  18319. '$mod.ClA = ExtB;',
  18320. '$mod.ClA = $mod.TExtC;',
  18321. '$mod.ClB = ExtB;',
  18322. '$mod.ClB = $mod.TExtC;',
  18323. '$mod.ClC = $mod.TExtC;',
  18324. '']));
  18325. end;
  18326. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18327. begin
  18328. AddModuleWithIntfImplSrc('unit2.pas',
  18329. LinesToStr([
  18330. '{$modeswitch externalclass}',
  18331. 'type',
  18332. ' TExtA = class external name ''ExtA''',
  18333. ' class var Id: longint;',
  18334. ' end;',
  18335. '']),
  18336. '');
  18337. StartUnit(true);
  18338. Add('interface');
  18339. Add('uses unit2;');
  18340. Add('implementation');
  18341. Add('begin');
  18342. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18343. ConvertUnit;
  18344. CheckSource('TestExternalClass_ClassOtherUnit',
  18345. LinesToStr([
  18346. '']),
  18347. LinesToStr([
  18348. 'ExtA.Id = ExtA.Id + 1;',
  18349. '']));
  18350. end;
  18351. procedure TTestModule.TestExternalClass_Is;
  18352. begin
  18353. StartProgram(false);
  18354. Add([
  18355. '{$modeswitch externalclass}',
  18356. 'type',
  18357. ' TExtA = class external name ''ExtA''',
  18358. ' end;',
  18359. ' TExtAClass = class of TExtA;',
  18360. ' TExtB = class external name ''ExtB'' (TExtA)',
  18361. ' end;',
  18362. ' TExtBClass = class of TExtB;',
  18363. ' TExtC = class (TExtB)',
  18364. ' end;',
  18365. ' TExtCClass = class of TExtC;',
  18366. 'var',
  18367. ' A: texta; ClA: TExtAClass;',
  18368. ' B: textb; ClB: TExtBClass;',
  18369. ' C: textc; ClC: TExtCClass;',
  18370. 'begin',
  18371. ' if a is textb then ;',
  18372. ' if a is textc then ;',
  18373. ' if b is textc then ;',
  18374. ' if cla is textb then ;',
  18375. ' if cla is textc then ;',
  18376. ' if clb is textc then ;',
  18377. ' try',
  18378. ' except',
  18379. ' on TExtA do ;',
  18380. ' on e: TExtB do ;',
  18381. ' end;',
  18382. '']);
  18383. ConvertProgram;
  18384. CheckSource('TestExternalClass_Is',
  18385. LinesToStr([ // statements
  18386. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18387. ' this.$init = function () {',
  18388. ' };',
  18389. ' this.$final = function () {',
  18390. ' };',
  18391. '});',
  18392. 'this.A = null;',
  18393. 'this.ClA = null;',
  18394. 'this.B = null;',
  18395. 'this.ClB = null;',
  18396. 'this.C = null;',
  18397. 'this.ClC = null;',
  18398. '']),
  18399. LinesToStr([ // $mod.$main
  18400. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18401. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18402. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18403. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18404. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18405. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18406. 'try {} catch ($e) {',
  18407. ' if (rtl.isExt($e,ExtA)) {}',
  18408. ' else if (rtl.isExt($e,ExtB)) {',
  18409. ' var e = $e;',
  18410. ' } else throw $e',
  18411. '};',
  18412. '']));
  18413. end;
  18414. procedure TTestModule.TestExternalClass_As;
  18415. begin
  18416. StartProgram(false);
  18417. Add('{$modeswitch externalclass}');
  18418. Add('type');
  18419. Add(' TExtA = class external name ''ExtA''');
  18420. Add(' end;');
  18421. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18422. Add(' end;');
  18423. Add(' TExtC = class (TExtB)');
  18424. Add(' end;');
  18425. Add('var');
  18426. Add(' A: texta;');
  18427. Add(' B: textb;');
  18428. Add(' C: textc;');
  18429. Add('begin');
  18430. Add(' b:=a as textb;');
  18431. Add(' c:=a as textc;');
  18432. Add(' c:=b as textc;');
  18433. ConvertProgram;
  18434. CheckSource('TestExternalClass_Is',
  18435. LinesToStr([ // statements
  18436. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18437. ' this.$init = function () {',
  18438. ' };',
  18439. ' this.$final = function () {',
  18440. ' };',
  18441. '});',
  18442. 'this.A = null;',
  18443. 'this.B = null;',
  18444. 'this.C = null;',
  18445. '']),
  18446. LinesToStr([ // $mod.$main
  18447. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18448. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18449. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18450. '']));
  18451. end;
  18452. procedure TTestModule.TestExternalClass_DestructorFail;
  18453. begin
  18454. StartProgram(false);
  18455. Add('{$modeswitch externalclass}');
  18456. Add('type');
  18457. Add(' TExtA = class external name ''ExtA''');
  18458. Add(' destructor Free;');
  18459. Add(' end;');
  18460. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18461. nPasElementNotSupported);
  18462. ConvertProgram;
  18463. end;
  18464. procedure TTestModule.TestExternalClass_New;
  18465. begin
  18466. StartProgram(false);
  18467. Add([
  18468. '{$modeswitch externalclass}',
  18469. 'type',
  18470. ' TExtA = class external name ''ExtA''',
  18471. ' constructor New;',
  18472. ' constructor New(i: longint; j: longint = 2);',
  18473. ' end;',
  18474. 'var',
  18475. ' A: texta;',
  18476. 'begin',
  18477. ' a:=texta.new;',
  18478. ' a:=texta(texta.new);',
  18479. ' a:=texta.new();',
  18480. ' a:=texta.new(1);',
  18481. ' with texta do begin',
  18482. ' a:=new;',
  18483. ' a:=new();',
  18484. ' a:=new(2);',
  18485. ' end;',
  18486. ' a:=test1.texta.new;',
  18487. ' a:=test1.texta.new();',
  18488. ' a:=test1.texta.new(3);',
  18489. '']);
  18490. ConvertProgram;
  18491. CheckSource('TestExternalClass_New',
  18492. LinesToStr([ // statements
  18493. 'this.A = null;',
  18494. '']),
  18495. LinesToStr([ // $mod.$main
  18496. '$mod.A = new ExtA();',
  18497. '$mod.A = new ExtA();',
  18498. '$mod.A = new ExtA();',
  18499. '$mod.A = new ExtA(1,2);',
  18500. '$mod.A = new ExtA();',
  18501. '$mod.A = new ExtA();',
  18502. '$mod.A = new ExtA(2,2);',
  18503. '$mod.A = new ExtA();',
  18504. '$mod.A = new ExtA();',
  18505. '$mod.A = new ExtA(3,2);',
  18506. '']));
  18507. end;
  18508. procedure TTestModule.TestExternalClass_ClassOf_New;
  18509. begin
  18510. StartProgram(false);
  18511. Add('{$modeswitch externalclass}');
  18512. Add('type');
  18513. Add(' TExtAClass = class of TExtA;');
  18514. Add(' TExtA = class external name ''ExtA''');
  18515. Add(' C: TExtAClass;');
  18516. Add(' constructor New;');
  18517. Add(' end;');
  18518. Add('var');
  18519. Add(' A: texta;');
  18520. Add(' C: textaclass;');
  18521. Add('begin');
  18522. Add(' a:=c.new;');
  18523. Add(' a:=c.new();');
  18524. Add(' with C do begin');
  18525. Add(' a:=new;');
  18526. Add(' a:=new();');
  18527. Add(' end;');
  18528. Add(' a:=test1.c.new;');
  18529. Add(' a:=test1.c.new();');
  18530. Add(' a:=A.c.new();');
  18531. ConvertProgram;
  18532. CheckSource('TestExternalClass_ClassOf_New',
  18533. LinesToStr([ // statements
  18534. 'this.A = null;',
  18535. 'this.C = null;',
  18536. '']),
  18537. LinesToStr([ // $mod.$main
  18538. '$mod.A = new $mod.C();',
  18539. '$mod.A = new $mod.C();',
  18540. 'var $with = $mod.C;',
  18541. '$mod.A = new $with();',
  18542. '$mod.A = new $with();',
  18543. '$mod.A = new $mod.C();',
  18544. '$mod.A = new $mod.C();',
  18545. '$mod.A = new $mod.A.C();',
  18546. '']));
  18547. end;
  18548. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18549. begin
  18550. StartProgram(false);
  18551. Add([
  18552. '{$modeswitch externalclass}',
  18553. 'type',
  18554. ' TExtAClass = class of TExtA;',
  18555. ' TExtA = class external name ''ExtA''',
  18556. ' constructor New;',
  18557. ' end;',
  18558. 'function GetCreator: TExtAClass;',
  18559. 'begin',
  18560. ' Result:=TExtA;',
  18561. 'end;',
  18562. 'var',
  18563. ' A: texta;',
  18564. 'begin',
  18565. ' a:=getcreator.new;',
  18566. ' a:=getcreator().new;',
  18567. ' a:=getcreator().new();',
  18568. ' a:=getcreator.new();',
  18569. ' with getcreator do begin',
  18570. ' a:=new;',
  18571. ' a:=new();',
  18572. ' end;']);
  18573. ConvertProgram;
  18574. CheckSource('TestExternalClass_FuncClassOf_New',
  18575. LinesToStr([ // statements
  18576. 'this.GetCreator = function () {',
  18577. ' var Result = null;',
  18578. ' Result = ExtA;',
  18579. ' return Result;',
  18580. '};',
  18581. 'this.A = null;',
  18582. '']),
  18583. LinesToStr([ // $mod.$main
  18584. '$mod.A = new ($mod.GetCreator())();',
  18585. '$mod.A = new ($mod.GetCreator())();',
  18586. '$mod.A = new ($mod.GetCreator())();',
  18587. '$mod.A = new ($mod.GetCreator())();',
  18588. 'var $with = $mod.GetCreator();',
  18589. '$mod.A = new $with();',
  18590. '$mod.A = new $with();',
  18591. '']));
  18592. end;
  18593. procedure TTestModule.TestExternalClass_New_PasClassFail;
  18594. begin
  18595. StartProgram(false);
  18596. Add([
  18597. '{$modeswitch externalclass}',
  18598. 'type',
  18599. ' TExtA = class external name ''ExtA''',
  18600. ' constructor New;',
  18601. ' end;',
  18602. ' TBird = class(TExtA)',
  18603. ' end;',
  18604. 'begin',
  18605. ' TBird.new;',
  18606. '']);
  18607. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18608. ConvertProgram;
  18609. end;
  18610. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  18611. begin
  18612. StartProgram(false);
  18613. Add([
  18614. '{$modeswitch externalclass}',
  18615. 'type',
  18616. ' TExtA = class external name ''ExtA''',
  18617. ' constructor New;',
  18618. ' end;',
  18619. ' TBird = class(TExtA)',
  18620. ' end;',
  18621. 'begin',
  18622. ' TBird.new();',
  18623. '']);
  18624. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18625. ConvertProgram;
  18626. end;
  18627. procedure TTestModule.TestExternalClass_NewExtName;
  18628. begin
  18629. StartProgram(false);
  18630. Add([
  18631. '{$modeswitch externalclass}',
  18632. 'type',
  18633. ' TExtA = class external name ''ExtA''',
  18634. ' constructor New; external name ''Other'';',
  18635. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  18636. ' end;',
  18637. 'var',
  18638. ' A: texta;',
  18639. 'begin',
  18640. ' a:=texta.new;',
  18641. ' a:=texta(texta.new);',
  18642. ' a:=texta.new();',
  18643. ' a:=texta.new(1);',
  18644. ' with texta do begin',
  18645. ' a:=new;',
  18646. ' a:=new();',
  18647. ' a:=new(2);',
  18648. ' end;',
  18649. ' a:=test1.texta.new;',
  18650. ' a:=test1.texta.new();',
  18651. ' a:=test1.texta.new(3);',
  18652. '']);
  18653. ConvertProgram;
  18654. CheckSource('TestExternalClass_NewExtName',
  18655. LinesToStr([ // statements
  18656. 'this.A = null;',
  18657. '']),
  18658. LinesToStr([ // $mod.$main
  18659. '$mod.A = new Other();',
  18660. '$mod.A = new Other();',
  18661. '$mod.A = new Other();',
  18662. '$mod.A = new A.B(1,2);',
  18663. '$mod.A = new Other();',
  18664. '$mod.A = new Other();',
  18665. '$mod.A = new A.B(2,2);',
  18666. '$mod.A = new Other();',
  18667. '$mod.A = new Other();',
  18668. '$mod.A = new A.B(3,2);',
  18669. '']));
  18670. end;
  18671. procedure TTestModule.TestExternalClass_Constructor;
  18672. begin
  18673. StartProgram(false);
  18674. Add([
  18675. '{$modeswitch externalclass}',
  18676. 'type',
  18677. ' TExtA = class external name ''ExtA''',
  18678. ' public type',
  18679. ' TExtB = class external name ''ExtB''',
  18680. ' public type',
  18681. ' TExtC = class external name ''ExtC''',
  18682. ' constructor New;',
  18683. ' constructor New(i: word);',
  18684. ' end;',
  18685. ' end;',
  18686. ' constructor Create;',
  18687. ' constructor Create(i: longint; j: longint = 2);',
  18688. ' end;',
  18689. 'var',
  18690. ' A: texta;',
  18691. ' C: texta.textb.textc;',
  18692. 'begin',
  18693. ' a:=texta.create;',
  18694. ' a:=texta(texta.create);',
  18695. ' a:=texta.create();',
  18696. ' a:=texta.create(1);',
  18697. ' with texta do begin',
  18698. ' a:=create;',
  18699. ' a:=create();',
  18700. ' a:=create(2);',
  18701. ' end;',
  18702. ' a:=test1.texta.create;',
  18703. ' a:=test1.texta.create();',
  18704. ' a:=test1.texta.create(3);',
  18705. ' c:=texta.textb.textc.new;',
  18706. ' c:=texta.textb.textc.new();',
  18707. ' c:=texta.textb.textc.new(4);',
  18708. '']);
  18709. ConvertProgram;
  18710. CheckSource('TestExternalClass_Constructor',
  18711. LinesToStr([ // statements
  18712. 'this.A = null;',
  18713. 'this.C = null;',
  18714. '']),
  18715. LinesToStr([ // $mod.$main
  18716. '$mod.A = new ExtA.Create();',
  18717. '$mod.A = new ExtA.Create();',
  18718. '$mod.A = new ExtA.Create();',
  18719. '$mod.A = new ExtA.Create(1,2);',
  18720. '$mod.A = new ExtA.Create();',
  18721. '$mod.A = new ExtA.Create();',
  18722. '$mod.A = new ExtA.Create(2,2);',
  18723. '$mod.A = new ExtA.Create();',
  18724. '$mod.A = new ExtA.Create();',
  18725. '$mod.A = new ExtA.Create(3,2);',
  18726. '$mod.C = new ExtA.ExtB.ExtC();',
  18727. '$mod.C = new ExtA.ExtB.ExtC();',
  18728. '$mod.C = new ExtA.ExtB.ExtC(4);',
  18729. '']));
  18730. end;
  18731. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  18732. begin
  18733. StartProgram(false);
  18734. Add([
  18735. '{$modeswitch externalclass}',
  18736. 'type',
  18737. ' TExtA = class external name ''ExtA''',
  18738. ' constructor Create; external name ''{}'';',
  18739. ' end;',
  18740. 'var',
  18741. ' A: texta;',
  18742. 'begin',
  18743. ' a:=texta.create;',
  18744. ' a:=texta(texta.create);',
  18745. ' a:=texta.create();',
  18746. ' with texta do begin',
  18747. ' a:=create;',
  18748. ' a:=create();',
  18749. ' end;',
  18750. ' a:=test1.texta.create;',
  18751. ' a:=test1.texta.create();',
  18752. '']);
  18753. ConvertProgram;
  18754. CheckSource('TestExternalClass_ConstructorBrackets',
  18755. LinesToStr([ // statements
  18756. 'this.A = null;',
  18757. '']),
  18758. LinesToStr([ // $mod.$main
  18759. '$mod.A = {};',
  18760. '$mod.A = {};',
  18761. '$mod.A = {};',
  18762. '$mod.A = {};',
  18763. '$mod.A = {};',
  18764. '$mod.A = {};',
  18765. '$mod.A = {};',
  18766. '']));
  18767. end;
  18768. procedure TTestModule.TestExternalClass_LocalConstSameName;
  18769. begin
  18770. StartProgram(false);
  18771. Add('{$modeswitch externalclass}');
  18772. Add('type');
  18773. Add(' TExtA = class external name ''ExtA''');
  18774. Add(' constructor New;');
  18775. Add(' end;');
  18776. Add('function DoIt: longint;');
  18777. Add('const ExtA: longint = 3;');
  18778. Add('begin');
  18779. Add(' Result:=ExtA;');
  18780. Add('end;');
  18781. Add('var');
  18782. Add(' A: texta;');
  18783. Add('begin');
  18784. Add(' a:=texta.new;');
  18785. ConvertProgram;
  18786. CheckSource('TestExternalClass_LocalConstSameName',
  18787. LinesToStr([ // statements
  18788. 'var ExtA$1 = 3;',
  18789. 'this.DoIt = function () {',
  18790. ' var Result = 0;',
  18791. ' Result = ExtA$1;',
  18792. ' return Result;',
  18793. '};',
  18794. 'this.A = null;',
  18795. '']),
  18796. LinesToStr([ // $mod.$main
  18797. '$mod.A = new ExtA();',
  18798. '']));
  18799. end;
  18800. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  18801. begin
  18802. StartProgram(false);
  18803. Add('{$modeswitch externalclass}');
  18804. Add('type');
  18805. Add(' TExtA = class external name ''ExtA''');
  18806. Add(' procedure DoIt;');
  18807. Add(' end;');
  18808. Add(' TMyA = class(TExtA)');
  18809. Add(' procedure DoIt;');
  18810. Add(' end;');
  18811. Add('procedure TMyA.DoIt; begin end;');
  18812. Add('begin');
  18813. ConvertProgram;
  18814. CheckSource('TestExternalClass_ReintroduceOverload',
  18815. LinesToStr([ // statements
  18816. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  18817. ' this.$init = function () {',
  18818. ' };',
  18819. ' this.$final = function () {',
  18820. ' };',
  18821. ' this.DoIt$1 = function () {',
  18822. ' };',
  18823. '});',
  18824. '']),
  18825. LinesToStr([ // $mod.$main
  18826. '']));
  18827. end;
  18828. procedure TTestModule.TestExternalClass_Inherited;
  18829. begin
  18830. StartProgram(false);
  18831. Add('{$modeswitch externalclass}');
  18832. Add('type');
  18833. Add(' TExtA = class external name ''ExtA''');
  18834. Add(' procedure DoIt(i: longint = 1); virtual;');
  18835. Add(' procedure DoSome(j: longint = 2);');
  18836. Add(' end;');
  18837. Add(' TExtB = class external name ''ExtB''(TExtA)');
  18838. Add(' end;');
  18839. Add(' TMyC = class(TExtB)');
  18840. Add(' procedure DoIt(i: longint = 1); override;');
  18841. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  18842. Add(' end;');
  18843. Add('procedure TMyC.DoIt(i: longint);');
  18844. Add('begin');
  18845. Add(' inherited;');
  18846. Add(' inherited DoIt;');
  18847. Add(' inherited DoIt();');
  18848. Add(' inherited DoIt(3);');
  18849. Add(' inherited DoSome;');
  18850. Add(' inherited DoSome();');
  18851. Add(' inherited DoSome(4);');
  18852. Add('end;');
  18853. Add('procedure TMyC.DoSome(j: longint);');
  18854. Add('begin');
  18855. Add(' inherited;');
  18856. Add('end;');
  18857. Add('begin');
  18858. ConvertProgram;
  18859. CheckSource('TestExternalClass_ReintroduceOverload',
  18860. LinesToStr([ // statements
  18861. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  18862. ' this.$init = function () {',
  18863. ' };',
  18864. ' this.$final = function () {',
  18865. ' };',
  18866. ' this.DoIt = function (i) {',
  18867. ' ExtB.DoIt.apply(this, arguments);',
  18868. ' ExtB.DoIt.call(this, 1);',
  18869. ' ExtB.DoIt.call(this, 1);',
  18870. ' ExtB.DoIt.call(this, 3);',
  18871. ' ExtB.DoSome.call(this, 2);',
  18872. ' ExtB.DoSome.call(this, 2);',
  18873. ' ExtB.DoSome.call(this, 4);',
  18874. ' };',
  18875. ' this.DoSome$1 = function (j) {',
  18876. ' ExtB.DoSome.apply(this, arguments);',
  18877. ' };',
  18878. '});',
  18879. '']),
  18880. LinesToStr([ // $mod.$main
  18881. '']));
  18882. end;
  18883. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  18884. begin
  18885. StartProgram(false);
  18886. Add('{$modeswitch externalclass}');
  18887. Add('type');
  18888. Add(' TObject = class');
  18889. Add(' end;');
  18890. Add(' TExtA = class external name ''ExtA''(TObject)');
  18891. Add(' end;');
  18892. Add('begin');
  18893. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  18894. ConvertProgram;
  18895. end;
  18896. procedure TTestModule.TestExternalClass_NewInstance;
  18897. begin
  18898. StartProgram(false);
  18899. Add('{$modeswitch externalclass}');
  18900. Add('type');
  18901. Add(' TExtA = class external name ''ExtA''');
  18902. Add(' end;');
  18903. Add(' TMyB = class(TExtA)');
  18904. Add(' protected');
  18905. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  18906. Add(' end;');
  18907. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  18908. Add('begin end;');
  18909. Add('begin');
  18910. ConvertProgram;
  18911. CheckSource('TestExternalClass_NewInstance',
  18912. LinesToStr([ // statements
  18913. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  18914. ' this.$init = function () {',
  18915. ' };',
  18916. ' this.$final = function () {',
  18917. ' };',
  18918. ' this.NewInstance = function (fnname, paramarray) {',
  18919. ' var Result = null;',
  18920. ' return Result;',
  18921. ' };',
  18922. '});',
  18923. '']),
  18924. LinesToStr([ // $mod.$main
  18925. '']));
  18926. end;
  18927. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  18928. begin
  18929. StartProgram(false);
  18930. Add('{$modeswitch externalclass}');
  18931. Add('type');
  18932. Add(' TExtA = class external name ''ExtA''');
  18933. Add(' end;');
  18934. Add(' TMyB = class(TExtA)');
  18935. Add(' protected');
  18936. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  18937. Add(' end;');
  18938. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  18939. Add('begin end;');
  18940. Add('begin');
  18941. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  18942. ConvertProgram;
  18943. end;
  18944. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  18945. begin
  18946. StartProgram(false);
  18947. Add('{$modeswitch externalclass}');
  18948. Add('type');
  18949. Add(' TExtA = class external name ''ExtA''');
  18950. Add(' end;');
  18951. Add(' TMyB = class(TExtA)');
  18952. Add(' protected');
  18953. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  18954. Add(' end;');
  18955. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  18956. Add('begin end;');
  18957. Add('begin');
  18958. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  18959. nIncompatibleTypeArgNo);
  18960. ConvertProgram;
  18961. end;
  18962. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  18963. begin
  18964. StartProgram(false);
  18965. Add('{$modeswitch externalclass}');
  18966. Add('type');
  18967. Add(' TExtA = class external name ''ExtA''');
  18968. Add(' end;');
  18969. Add(' TMyB = class(TExtA)');
  18970. Add(' protected');
  18971. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  18972. Add(' end;');
  18973. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  18974. Add('begin end;');
  18975. Add('begin');
  18976. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  18977. nIncompatibleTypeArgNo);
  18978. ConvertProgram;
  18979. end;
  18980. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  18981. begin
  18982. StartProgram(false);
  18983. Add([
  18984. '{$modeswitch externalclass}',
  18985. 'type',
  18986. ' TJSFunction = class external name ''Function''',
  18987. ' end;',
  18988. ' TExtA = class external name ''ExtA''(TJSFunction)',
  18989. ' constructor New(w: word);',
  18990. ' end;',
  18991. ' TBird = class (TExtA)',
  18992. ' public',
  18993. ' Size: word;',
  18994. ' class var Legs: word;',
  18995. ' constructor Create(a: word);',
  18996. ' end;',
  18997. ' TEagle = class (TBird)',
  18998. ' public',
  18999. ' constructor Create(b: word); reintroduce;',
  19000. ' end;',
  19001. 'constructor TBird.Create(a: word);',
  19002. 'begin',
  19003. ' inherited;', // silently ignored
  19004. ' inherited New(a);', // this.$func(a)
  19005. 'end;',
  19006. 'constructor TEagle.Create(b: word);',
  19007. 'begin',
  19008. ' inherited Create(b);',
  19009. 'end;',
  19010. 'var',
  19011. ' Bird: TBird;',
  19012. ' Eagle: TEagle;',
  19013. 'begin',
  19014. ' Bird:=TBird.Create(3);',
  19015. ' Eagle:=TEagle.Create(4);',
  19016. ' Bird.Size:=Bird.Size+5;',
  19017. ' Bird.Legs:=Bird.Legs+6;',
  19018. ' Eagle.Size:=Eagle.Size+5;',
  19019. ' Eagle.Legs:=Eagle.Legs+6;',
  19020. '']);
  19021. ConvertProgram;
  19022. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19023. LinesToStr([ // statements
  19024. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19025. ' this.Legs = 0;',
  19026. ' this.$init = function () {',
  19027. ' this.Size = 0;',
  19028. ' };',
  19029. ' this.$final = function () {',
  19030. ' };',
  19031. ' this.Create = function (a) {',
  19032. ' this.$ancestorfunc(a);',
  19033. ' return this;',
  19034. ' };',
  19035. '});',
  19036. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19037. ' this.Create$1 = function (b) {',
  19038. ' $mod.TBird.Create.call(this, b);',
  19039. ' return this;',
  19040. ' };',
  19041. '});',
  19042. 'this.Bird = null;',
  19043. 'this.Eagle = null;',
  19044. '']),
  19045. LinesToStr([ // $mod.$main
  19046. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19047. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19048. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19049. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19050. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19051. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19052. '']));
  19053. end;
  19054. procedure TTestModule.TestExternalClass_PascalProperty;
  19055. begin
  19056. StartProgram(false);
  19057. Add('{$modeswitch externalclass}');
  19058. Add('type');
  19059. Add(' TJSElement = class;');
  19060. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19061. Add(' TJSElement = class external name ''ExtA''');
  19062. Add(' end;');
  19063. Add(' TControl = class(TJSElement)');
  19064. Add(' private');
  19065. Add(' FOnClick: TJSNotifyEvent;');
  19066. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19067. Add(' procedure Click(Sender: TJSElement);');
  19068. Add(' end;');
  19069. Add('procedure TControl.Click(Sender: TJSElement);');
  19070. Add('begin');
  19071. Add(' OnClick(Self);');
  19072. Add('end;');
  19073. Add('var');
  19074. Add(' Ctrl: TControl;');
  19075. Add('begin');
  19076. Add(' Ctrl.OnClick:[email protected];');
  19077. Add(' Ctrl.OnClick(Ctrl);');
  19078. ConvertProgram;
  19079. CheckSource('TestExternalClass_PascalProperty',
  19080. LinesToStr([ // statements
  19081. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19082. ' this.$init = function () {',
  19083. ' this.FOnClick = null;',
  19084. ' };',
  19085. ' this.$final = function () {',
  19086. ' this.FOnClick = undefined;',
  19087. ' };',
  19088. ' this.Click = function (Sender) {',
  19089. ' this.FOnClick(this);',
  19090. ' };',
  19091. '});',
  19092. 'this.Ctrl = null;',
  19093. '']),
  19094. LinesToStr([ // $mod.$main
  19095. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19096. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19097. '']));
  19098. end;
  19099. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19100. begin
  19101. StartProgram(false);
  19102. Add([
  19103. '{$modeswitch externalclass}',
  19104. 'type',
  19105. ' IUnknown = interface end;',
  19106. ' TObject = class',
  19107. ' end;',
  19108. ' TChild = class',
  19109. ' end;',
  19110. ' TExtRootA = class external name ''ExtRootA''',
  19111. ' end;',
  19112. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19113. ' end;',
  19114. ' TExtRootB = class external name ''ExtRootB''',
  19115. ' end;',
  19116. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19117. ' end;',
  19118. ' TExtString = class external name ''String''',
  19119. ' function charAt(aIndex : NativeInt) : string;',
  19120. ' end;',
  19121. 'var',
  19122. ' Obj: TObject;',
  19123. ' Child: TChild;',
  19124. ' RootA: TExtRootA;',
  19125. ' ChildA: TExtChildA;',
  19126. ' RootB: TExtRootB;',
  19127. ' ChildB: TExtChildB;',
  19128. ' i: IUnknown;',
  19129. ' s: string;',
  19130. ' v: jsvalue;',
  19131. 'begin',
  19132. ' obj:=tobject(roota);',
  19133. ' obj:=tobject(childa);',
  19134. ' child:=tchild(tobject(roota));',
  19135. ' roota:=textroota(obj);',
  19136. ' roota:=textroota(child);',
  19137. ' roota:=textroota(rootb);',
  19138. ' roota:=textroota(childb);',
  19139. ' childa:=textchilda(textroota(obj));',
  19140. ' roota:=TExtRootA(i);',
  19141. ' s:=TExtString(s).charAt(7);',
  19142. ' s:=TExtString(v).charAt(8);',
  19143. '']);
  19144. ConvertProgram;
  19145. CheckSource('TestExternalClass_TypeCastToRootClass',
  19146. LinesToStr([ // statements
  19147. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19148. 'rtl.createClass(this, "TObject", null, function () {',
  19149. ' this.$init = function () {',
  19150. ' };',
  19151. ' this.$final = function () {',
  19152. ' };',
  19153. '});',
  19154. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19155. '});',
  19156. 'this.Obj = null;',
  19157. 'this.Child = null;',
  19158. 'this.RootA = null;',
  19159. 'this.ChildA = null;',
  19160. 'this.RootB = null;',
  19161. 'this.ChildB = null;',
  19162. 'this.i = null;',
  19163. 'this.s = "";',
  19164. 'this.v = undefined;',
  19165. '']),
  19166. LinesToStr([ // $mod.$main
  19167. '$mod.Obj = $mod.RootA;',
  19168. '$mod.Obj = $mod.ChildA;',
  19169. '$mod.Child = $mod.RootA;',
  19170. '$mod.RootA = $mod.Obj;',
  19171. '$mod.RootA = $mod.Child;',
  19172. '$mod.RootA = $mod.RootB;',
  19173. '$mod.RootA = $mod.ChildB;',
  19174. '$mod.ChildA = $mod.Obj;',
  19175. '$mod.RootA = $mod.i;',
  19176. '$mod.s = $mod.s.charAt(7);',
  19177. '$mod.s = $mod.v.charAt(8);',
  19178. '']));
  19179. end;
  19180. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19181. begin
  19182. StartProgram(false);
  19183. Add([
  19184. '{$modeswitch externalclass}',
  19185. 'type',
  19186. ' IUnknown = interface end;',
  19187. ' IBird = interface(IUnknown) end;',
  19188. ' TClass = class of TObject;',
  19189. ' TObject = class',
  19190. ' end;',
  19191. ' TChild = class',
  19192. ' end;',
  19193. ' TJSObject = class external name ''Object''',
  19194. ' end;',
  19195. ' TRec = record end;',
  19196. 'var',
  19197. ' Obj: TObject;',
  19198. ' Child: TChild;',
  19199. ' i: IUnknown;',
  19200. ' Bird: IBird;',
  19201. ' j: TJSObject;',
  19202. ' r: TRec;',
  19203. ' c: TClass;',
  19204. 'begin',
  19205. ' j:=tjsobject(IUnknown);',
  19206. ' j:=tjsobject(IBird);',
  19207. ' j:=tjsobject(TObject);',
  19208. ' j:=tjsobject(TChild);',
  19209. ' j:=tjsobject(TRec);',
  19210. ' j:=tjsobject(Obj);',
  19211. ' j:=tjsobject(Child);',
  19212. ' j:=tjsobject(i);',
  19213. ' j:=tjsobject(Bird);',
  19214. ' j:=tjsobject(r);',
  19215. ' j:=tjsobject(c);',
  19216. '']);
  19217. ConvertProgram;
  19218. CheckSource('TestExternalClass_TypeCastToJSObject',
  19219. LinesToStr([ // statements
  19220. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19221. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19222. 'rtl.createClass(this, "TObject", null, function () {',
  19223. ' this.$init = function () {',
  19224. ' };',
  19225. ' this.$final = function () {',
  19226. ' };',
  19227. '});',
  19228. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19229. '});',
  19230. 'rtl.recNewT(this, "TRec", function () {',
  19231. ' this.$eq = function (b) {',
  19232. ' return true;',
  19233. ' };',
  19234. ' this.$assign = function (s) {',
  19235. ' return this;',
  19236. ' };',
  19237. '});',
  19238. 'this.Obj = null;',
  19239. 'this.Child = null;',
  19240. 'this.i = null;',
  19241. 'this.Bird = null;',
  19242. 'this.j = null;',
  19243. 'this.r = this.TRec.$new();',
  19244. 'this.c = null;',
  19245. '']),
  19246. LinesToStr([ // $mod.$main
  19247. '$mod.j = $mod.IUnknown;',
  19248. '$mod.j = $mod.IBird;',
  19249. '$mod.j = $mod.TObject;',
  19250. '$mod.j = $mod.TChild;',
  19251. '$mod.j = $mod.TRec;',
  19252. '$mod.j = $mod.Obj;',
  19253. '$mod.j = $mod.Child;',
  19254. '$mod.j = $mod.i;',
  19255. '$mod.j = $mod.Bird;',
  19256. '$mod.j = $mod.r;',
  19257. '$mod.j = $mod.c;',
  19258. '']));
  19259. end;
  19260. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19261. begin
  19262. StartProgram(false);
  19263. Add('{$modeswitch externalclass}');
  19264. Add('type');
  19265. Add(' TJSString = class external name ''String''');
  19266. Add(' class function fromCharCode() : string; varargs;');
  19267. Add(' function anchor(const aName : string) : string;');
  19268. Add(' end;');
  19269. Add('var');
  19270. Add(' s: string;');
  19271. Add('begin');
  19272. Add(' s:=TJSString.fromCharCode(65,66);');
  19273. Add(' s:=TJSString(s).anchor(s);');
  19274. Add(' s:=TJSString(''foo'').anchor(s);');
  19275. ConvertProgram;
  19276. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19277. LinesToStr([ // statements
  19278. 'this.s = "";',
  19279. '']),
  19280. LinesToStr([ // $mod.$main
  19281. '$mod.s = String.fromCharCode(65, 66);',
  19282. '$mod.s = $mod.s.anchor($mod.s);',
  19283. '$mod.s = "foo".anchor($mod.s);',
  19284. '']));
  19285. end;
  19286. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19287. begin
  19288. StartProgram(false);
  19289. Add([
  19290. '{$modeswitch externalclass}',
  19291. 'type',
  19292. ' TJSObject = class external name ''Object'' end;',
  19293. ' TJSFunction = class external name ''Function''',
  19294. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19295. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19296. ' end;',
  19297. ' TObject = class',
  19298. ' procedure DoIt(i: longint);',
  19299. ' end;',
  19300. ' TFuncInt = function(o: TObject): longint;',
  19301. 'function GetIt(o: TObject): longint;',
  19302. ' procedure Sub; begin end;',
  19303. 'var',
  19304. ' f: TJSFunction;',
  19305. ' fi: TFuncInt;',
  19306. 'begin',
  19307. ' fi:=TFuncInt(f);',
  19308. ' f:=TJSFunction(fi);',
  19309. ' f:=TJSFunction(@GetIt);',
  19310. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19311. ' f:=TJSFunction(@Sub);',
  19312. ' f:=TJSFunction(@o.doit);',
  19313. ' f:=TJSFunction(fi).bind(nil,4)',
  19314. 'end;',
  19315. 'procedure TObject.DoIt(i: longint);',
  19316. ' procedure Sub; begin end;',
  19317. 'var f: TJSFunction;',
  19318. 'begin',
  19319. ' f:=TJSFunction(@DoIt);',
  19320. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19321. ' f:=TJSFunction(@Sub);',
  19322. ' f:=TJSFunction(@GetIt);',
  19323. 'end;',
  19324. 'begin']);
  19325. ConvertProgram;
  19326. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19327. LinesToStr([ // statements
  19328. 'rtl.createClass(this, "TObject", null, function () {',
  19329. ' this.$init = function () {',
  19330. ' };',
  19331. ' this.$final = function () {',
  19332. ' };',
  19333. ' this.DoIt = function (i) {',
  19334. ' var $Self = this;',
  19335. ' function Sub() {',
  19336. ' };',
  19337. ' var f = null;',
  19338. ' f = this.DoIt;',
  19339. ' f = this.DoIt.bind(null, 13);',
  19340. ' f = Sub;',
  19341. ' f = $mod.GetIt;',
  19342. ' };',
  19343. '});',
  19344. 'this.GetIt = function (o) {',
  19345. ' var Result = 0;',
  19346. ' function Sub() {',
  19347. ' };',
  19348. ' var f = null;',
  19349. ' var fi = null;',
  19350. ' fi = f;',
  19351. ' f = fi;',
  19352. ' f = $mod.GetIt;',
  19353. ' f = $mod.GetIt.bind(null, 3);',
  19354. ' f = Sub;',
  19355. ' f = $mod.TObject.DoIt;',
  19356. ' f = fi.bind(null, 4);',
  19357. ' return Result;',
  19358. '};',
  19359. '']),
  19360. LinesToStr([ // $mod.$main
  19361. '']));
  19362. end;
  19363. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19364. begin
  19365. StartProgram(false);
  19366. Add([
  19367. '{$mode delphi}',
  19368. '{$modeswitch externalclass}',
  19369. 'type',
  19370. ' TJSObject = class external name ''Object'' end;',
  19371. ' TJSWindow = class external name ''Window''(TJSObject)',
  19372. ' procedure Open;',
  19373. ' end;',
  19374. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19375. ' procedure Execute;',
  19376. ' end;',
  19377. 'procedure Fly;',
  19378. 'var',
  19379. ' w: TJSWindow;',
  19380. ' e: TJSEventTarget;',
  19381. 'begin',
  19382. ' w:=TJSWindow(e);',
  19383. ' e:=TJSEventTarget(w);',
  19384. 'end;',
  19385. 'begin']);
  19386. ConvertProgram;
  19387. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19388. LinesToStr([ // statements
  19389. 'this.Fly = function () {',
  19390. ' var w = null;',
  19391. ' var e = null;',
  19392. ' w = e;',
  19393. ' e = w;',
  19394. '};',
  19395. '']),
  19396. LinesToStr([ // $mod.$main
  19397. '']));
  19398. end;
  19399. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19400. begin
  19401. StartProgram(false);
  19402. Add('{$modeswitch externalclass}');
  19403. Add('type');
  19404. Add(' TJSString = class external name ''String''');
  19405. Add(' class function fromCharCode() : string; varargs;');
  19406. Add(' end;');
  19407. Add('var');
  19408. Add(' s: string;');
  19409. Add(' sObj: TJSString;');
  19410. Add('begin');
  19411. Add(' s:=sObj.fromCharCode(65,66);');
  19412. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19413. nExternalClassInstanceCannotAccessStaticX);
  19414. ConvertProgram;
  19415. end;
  19416. procedure TTestModule.TestExternalClass_BracketAccessor;
  19417. begin
  19418. StartProgram(false);
  19419. Add([
  19420. '{$modeswitch externalclass}',
  19421. 'type',
  19422. ' TJSArray = class external name ''Array2''',
  19423. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19424. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19425. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19426. ' end;',
  19427. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19428. 'begin end;',
  19429. 'var',
  19430. ' Arr: tjsarray;',
  19431. ' s: string;',
  19432. ' i: longint;',
  19433. ' v: jsvalue;',
  19434. 'begin',
  19435. ' v:=arr[0];',
  19436. ' v:=arr.items[1];',
  19437. ' arr[2]:=s;',
  19438. ' arr.items[3]:=s;',
  19439. ' arr[4]:=i;',
  19440. ' arr[5]:=arr[6];',
  19441. ' arr.items[7]:=arr.items[8];',
  19442. ' with arr do items[9]:=items[10];',
  19443. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19444. ' with arr do begin',
  19445. ' v:=GetItems(14);',
  19446. ' setitems(15,16);',
  19447. ' end;',
  19448. ' v:=test1.arr.items[17];',
  19449. ' test1.arr.items[18]:=v;',
  19450. '']);
  19451. ConvertProgram;
  19452. CheckSource('TestExternalClass_BracketAccessor',
  19453. LinesToStr([ // statements
  19454. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19455. '};',
  19456. 'this.Arr = null;',
  19457. 'this.s = "";',
  19458. 'this.i = 0;',
  19459. 'this.v = undefined;',
  19460. '']),
  19461. LinesToStr([ // $mod.$main
  19462. '$mod.v = $mod.Arr[0];',
  19463. '$mod.v = $mod.Arr[1];',
  19464. '$mod.Arr[2] = $mod.s;',
  19465. '$mod.Arr[3] = $mod.s;',
  19466. '$mod.Arr[4] = $mod.i;',
  19467. '$mod.Arr[5] = $mod.Arr[6];',
  19468. '$mod.Arr[7] = $mod.Arr[8];',
  19469. 'var $with = $mod.Arr;',
  19470. '$with[9] = $with[10];',
  19471. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19472. ' a: 9,',
  19473. ' p: $mod.Arr,',
  19474. ' get: function () {',
  19475. ' return this.p[this.a];',
  19476. ' },',
  19477. ' set: function (v) {',
  19478. ' this.p[this.a] = v;',
  19479. ' }',
  19480. '}, {',
  19481. ' a: 10,',
  19482. ' p: $mod.Arr,',
  19483. ' get: function () {',
  19484. ' return this.p[this.a];',
  19485. ' },',
  19486. ' set: function (v) {',
  19487. ' this.p[this.a] = v;',
  19488. ' }',
  19489. '});',
  19490. 'var $with1 = $mod.Arr;',
  19491. '$mod.v = $with1[14];',
  19492. '$with1[15] = 16;',
  19493. '$mod.v = $mod.Arr[17];',
  19494. '$mod.Arr[18] = $mod.v;',
  19495. '']));
  19496. end;
  19497. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19498. begin
  19499. StartProgram(false);
  19500. Add([
  19501. '{$modeswitch externalclass}',
  19502. 'type',
  19503. ' TJSArray = class external name ''Array2''',
  19504. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19505. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19506. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19507. ' end;',
  19508. ' TMyArr = class(TJSArray)',
  19509. ' procedure DoIt;',
  19510. ' end;',
  19511. 'procedure tmyarr.DoIt;',
  19512. 'begin',
  19513. ' Items[1]:=Items[2];',
  19514. ' SetItems(3,getItems(4));',
  19515. 'end;',
  19516. 'var',
  19517. ' Arr: tmyarr;',
  19518. ' s: string;',
  19519. ' i: longint;',
  19520. ' v: jsvalue;',
  19521. 'begin',
  19522. ' v:=arr[0];',
  19523. ' v:=arr.items[1];',
  19524. ' arr[2]:=s;',
  19525. ' arr.items[3]:=s;',
  19526. ' arr[4]:=i;',
  19527. ' arr[5]:=arr[6];',
  19528. ' arr.items[7]:=arr.items[8];',
  19529. ' with arr do items[9]:=items[10];',
  19530. ' with arr do begin',
  19531. ' v:=GetItems(14);',
  19532. ' setitems(15,16);',
  19533. ' end;',
  19534. '']);
  19535. ConvertProgram;
  19536. CheckSource('TestExternalClass_BracketAccessor_Call',
  19537. LinesToStr([ // statements
  19538. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19539. ' this.$init = function () {',
  19540. ' };',
  19541. ' this.$final = function () {',
  19542. ' };',
  19543. ' this.DoIt = function () {',
  19544. ' this[1] = this[2];',
  19545. ' this[3] = this[4];',
  19546. ' };',
  19547. '});',
  19548. 'this.Arr = null;',
  19549. 'this.s = "";',
  19550. 'this.i = 0;',
  19551. 'this.v = undefined;',
  19552. '']),
  19553. LinesToStr([ // $mod.$main
  19554. '$mod.v = $mod.Arr[0];',
  19555. '$mod.v = $mod.Arr[1];',
  19556. '$mod.Arr[2] = $mod.s;',
  19557. '$mod.Arr[3] = $mod.s;',
  19558. '$mod.Arr[4] = $mod.i;',
  19559. '$mod.Arr[5] = $mod.Arr[6];',
  19560. '$mod.Arr[7] = $mod.Arr[8];',
  19561. 'var $with = $mod.Arr;',
  19562. '$with[9] = $with[10];',
  19563. 'var $with1 = $mod.Arr;',
  19564. '$mod.v = $with1[14];',
  19565. '$with1[15] = 16;',
  19566. '']));
  19567. end;
  19568. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  19569. begin
  19570. StartProgram(false);
  19571. Add('{$modeswitch externalclass}');
  19572. Add('type');
  19573. Add(' TJSArray = class external name ''Array2''');
  19574. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  19575. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  19576. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  19577. Add(' end;');
  19578. Add('begin');
  19579. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  19580. nBracketAccessorOfExternalClassMustHaveOneParameter);
  19581. ConvertProgram;
  19582. end;
  19583. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  19584. begin
  19585. StartProgram(false);
  19586. Add('{$modeswitch externalclass}');
  19587. Add('type');
  19588. Add(' TJSArray = class external name ''Array2''');
  19589. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19590. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  19591. Add(' end;');
  19592. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  19593. Add('begin end;');
  19594. Add('var');
  19595. Add(' Arr: tjsarray;');
  19596. Add(' v: jsvalue;');
  19597. Add('begin');
  19598. Add(' v:=arr[0];');
  19599. Add(' v:=arr.items[1];');
  19600. Add(' with arr do v:=items[2];');
  19601. Add(' doit(arr[3],arr[4]);');
  19602. ConvertProgram;
  19603. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  19604. LinesToStr([ // statements
  19605. 'this.DoIt = function (vI, vJ) {',
  19606. '};',
  19607. 'this.Arr = null;',
  19608. 'this.v = undefined;',
  19609. '']),
  19610. LinesToStr([ // $mod.$main
  19611. '$mod.v = $mod.Arr[0];',
  19612. '$mod.v = $mod.Arr[1];',
  19613. 'var $with = $mod.Arr;',
  19614. '$mod.v = $with[2];',
  19615. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  19616. '']));
  19617. end;
  19618. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  19619. begin
  19620. StartProgram(false);
  19621. Add('{$modeswitch externalclass}');
  19622. Add('type');
  19623. Add(' TJSArray = class external name ''Array2''');
  19624. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19625. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  19626. Add(' end;');
  19627. Add('var');
  19628. Add(' Arr: tjsarray;');
  19629. Add(' s: string;');
  19630. Add(' i: longint;');
  19631. Add(' v: jsvalue;');
  19632. Add('begin');
  19633. Add(' arr[2]:=s;');
  19634. Add(' arr.items[3]:=s;');
  19635. Add(' arr[4]:=i;');
  19636. Add(' with arr do items[5]:=i;');
  19637. ConvertProgram;
  19638. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  19639. LinesToStr([ // statements
  19640. 'this.Arr = null;',
  19641. 'this.s = "";',
  19642. 'this.i = 0;',
  19643. 'this.v = undefined;',
  19644. '']),
  19645. LinesToStr([ // $mod.$main
  19646. '$mod.Arr[2] = $mod.s;',
  19647. '$mod.Arr[3] = $mod.s;',
  19648. '$mod.Arr[4] = $mod.i;',
  19649. 'var $with = $mod.Arr;',
  19650. '$with[5] = $mod.i;',
  19651. '']));
  19652. end;
  19653. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  19654. begin
  19655. StartProgram(false);
  19656. Add('{$modeswitch externalclass}');
  19657. Add('type');
  19658. Add(' TJSArray = class external name ''Array2''');
  19659. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19660. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  19661. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  19662. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  19663. Add(' end;');
  19664. Add('var');
  19665. Add(' Arr: tjsarray;');
  19666. Add(' s: string;');
  19667. Add(' i: longint;');
  19668. Add(' v: jsvalue;');
  19669. Add('begin');
  19670. Add(' arr[2]:=s;');
  19671. Add(' arr.items[3]:=s;');
  19672. Add(' arr.numbers[4]:=i;');
  19673. Add(' with arr do items[5]:=i;');
  19674. Add(' with arr do numbers[6]:=i;');
  19675. ConvertProgram;
  19676. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  19677. LinesToStr([ // statements
  19678. 'this.Arr = null;',
  19679. 'this.s = "";',
  19680. 'this.i = 0;',
  19681. 'this.v = undefined;',
  19682. '']),
  19683. LinesToStr([ // $mod.$main
  19684. '$mod.Arr[2] = $mod.s;',
  19685. '$mod.Arr[3] = $mod.s;',
  19686. '$mod.Arr[4] = $mod.i;',
  19687. 'var $with = $mod.Arr;',
  19688. '$with[5] = $mod.i;',
  19689. 'var $with1 = $mod.Arr;',
  19690. '$with1[6] = $mod.i;',
  19691. '']));
  19692. end;
  19693. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  19694. begin
  19695. StartProgram(false);
  19696. Add('{$modeswitch externalclass}');
  19697. Add('type');
  19698. Add(' TJSArray = class external name ''Array2''');
  19699. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19700. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19701. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  19702. Add(' end;');
  19703. Add('var');
  19704. Add(' Arr: tjsarray;');
  19705. Add(' i: longint;');
  19706. Add(' IntArr: array of longint;');
  19707. Add(' v: jsvalue;');
  19708. Add('begin');
  19709. Add(' v:=arr.items[i];');
  19710. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  19711. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  19712. ConvertProgram;
  19713. CheckSource('TestExternalClass_BracketAccessor_Index',
  19714. LinesToStr([ // statements
  19715. 'this.Arr = null;',
  19716. 'this.i = 0;',
  19717. 'this.IntArr = [];',
  19718. 'this.v = undefined;',
  19719. '']),
  19720. LinesToStr([ // $mod.$main
  19721. '$mod.v = $mod.Arr[$mod.i];',
  19722. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  19723. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  19724. '']));
  19725. end;
  19726. procedure TTestModule.TestExternalClass_ForInJSObject;
  19727. begin
  19728. StartProgram(false);
  19729. Add([
  19730. '{$modeswitch externalclass}',
  19731. 'type',
  19732. ' TJSObject = class external name ''Object''',
  19733. ' end;',
  19734. 'var',
  19735. ' o: TJSObject;',
  19736. ' key: string;',
  19737. 'begin',
  19738. ' for key in o do',
  19739. ' if key=''abc'' then ;',
  19740. '']);
  19741. ConvertProgram;
  19742. CheckSource('TestExternalClass_ForInJSObject',
  19743. LinesToStr([ // statements
  19744. 'this.o = null;',
  19745. 'this.key = "";',
  19746. '']),
  19747. LinesToStr([ // $mod.$main
  19748. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  19749. '']));
  19750. end;
  19751. procedure TTestModule.TestExternalClass_ForInJSArray;
  19752. begin
  19753. StartProgram(false);
  19754. Add([
  19755. '{$modeswitch externalclass}',
  19756. 'type',
  19757. ' TJSInt8Array = class external name ''Int8Array''',
  19758. ' private',
  19759. ' flength: NativeInt external name ''length'';',
  19760. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  19761. ' public',
  19762. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  19763. ' property Length: NativeInt read flength;',
  19764. ' end;',
  19765. 'var',
  19766. ' a: TJSInt8Array;',
  19767. ' value: shortint;',
  19768. 'begin',
  19769. ' for value in a do',
  19770. ' if value=3 then ;',
  19771. '']);
  19772. ConvertProgram;
  19773. CheckSource('TestExternalClass_ForInJSArray',
  19774. LinesToStr([ // statements
  19775. 'this.a = null;',
  19776. 'this.value = 0;',
  19777. '']),
  19778. LinesToStr([ // $mod.$main
  19779. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  19780. ' $mod.value = $in[$l];',
  19781. ' if ($mod.value === 3) ;',
  19782. '};',
  19783. '']));
  19784. end;
  19785. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  19786. begin
  19787. AddModuleWithIntfImplSrc('unit2.pas',
  19788. LinesToStr([
  19789. '{$modeswitch externalclass}',
  19790. 'type',
  19791. ' TJSBufferSource = class external name ''BufferSource''',
  19792. ' end;',
  19793. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  19794. '']),
  19795. '');
  19796. AddModuleWithIntfImplSrc('unit3.pas',
  19797. LinesToStr([
  19798. '{$modeswitch externalclass}',
  19799. 'type',
  19800. ' TJSBufferSource = class external name ''BufferSource''',
  19801. ' end;',
  19802. '']),
  19803. '');
  19804. StartUnit(true);
  19805. Add([
  19806. 'interface',
  19807. 'uses unit2, unit3;',
  19808. 'procedure DoSome(s: TJSBufferSource);',
  19809. 'implementation',
  19810. 'procedure DoSome(s: TJSBufferSource);',
  19811. 'begin',
  19812. ' DoIt(s);',
  19813. 'end;',
  19814. '']);
  19815. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  19816. nIncompatibleTypeArgNo);
  19817. ConvertUnit;
  19818. end;
  19819. procedure TTestModule.TestExternalClass_NestedConstructor;
  19820. begin
  19821. StartProgram(false);
  19822. Add([
  19823. '{$modeswitch externalclass}',
  19824. 'type',
  19825. ' TJSObject = class external name ''Object''',
  19826. ' type TBird = class external name ''Bird''',
  19827. ' type TWing = class external name ''Wing''',
  19828. ' constructor New;',
  19829. ' constructor Create(w: word = 3);',
  19830. ' end;',
  19831. ' end;',
  19832. ' end;',
  19833. 'var',
  19834. ' w: TJSObject.TBird.TWing;',
  19835. 'begin',
  19836. ' w:=tjsobject.tbird.twing.new;',
  19837. ' w:=tjsobject.tbird.twing.new();',
  19838. ' w:=tjsobject.tbird.twing.create;',
  19839. ' w:=tjsobject.tbird.twing.create(4);',
  19840. ' with tjsobject do begin',
  19841. ' w:=tbird.twing.new;',
  19842. ' w:=tbird.twing.new();',
  19843. ' w:=tbird.twing.create;',
  19844. ' w:=tbird.twing.create(11);',
  19845. ' end;',
  19846. ' with tjsobject.tbird do begin',
  19847. ' w:=twing.new;',
  19848. ' w:=twing.new();',
  19849. ' w:=twing.create;',
  19850. ' w:=twing.create(21);',
  19851. ' end;',
  19852. ' with tjsobject.tbird.twing do begin',
  19853. ' w:=new;',
  19854. ' w:=new();',
  19855. ' w:=create;',
  19856. ' w:=create(31);',
  19857. ' end;',
  19858. '']);
  19859. ConvertProgram;
  19860. CheckSource('TestExternalClass_NestedConstructor',
  19861. LinesToStr([ // statements
  19862. 'this.w = null;',
  19863. '']),
  19864. LinesToStr([ // $mod.$main
  19865. '$mod.w = new Object.Bird.Wing();',
  19866. '$mod.w = new Object.Bird.Wing();',
  19867. '$mod.w = new Object.Bird.Wing.Create();',
  19868. '$mod.w = new Object.Bird.Wing.Create(4);',
  19869. '$mod.w = new Object.Bird.Wing();',
  19870. '$mod.w = new Object.Bird.Wing();',
  19871. '$mod.w = new Object.Bird.Wing.Create();',
  19872. '$mod.w = new Object.Bird.Wing.Create(11);',
  19873. 'var $with = Object.Bird;',
  19874. '$mod.w = new Object.Bird.Wing();',
  19875. '$mod.w = new Object.Bird.Wing();',
  19876. '$mod.w = new Object.Bird.Wing.Create();',
  19877. '$mod.w = new Object.Bird.Wing.Create(21);',
  19878. 'var $with1 = Object.Bird.Wing;',
  19879. '$mod.w = new $with1();',
  19880. '$mod.w = new $with1();',
  19881. '$mod.w = new Object.Bird.Wing.Create();',
  19882. '$mod.w = new Object.Bird.Wing.Create(31);',
  19883. '']));
  19884. end;
  19885. procedure TTestModule.TestClassInterface_Corba;
  19886. begin
  19887. StartProgram(false);
  19888. Add([
  19889. '{$interfaces corba}',
  19890. 'type',
  19891. ' IUnknown = interface;',
  19892. ' IUnknown = interface',
  19893. ' [''{00000000-0000-0000-C000-000000000046}'']',
  19894. ' end;',
  19895. ' IInterface = IUnknown;',
  19896. ' IBird = interface(IInterface)',
  19897. ' function GetSize: longint;',
  19898. ' procedure SetSize(i: longint);',
  19899. ' property Size: longint read GetSize write SetSize;',
  19900. ' procedure DoIt(i: longint);',
  19901. ' end;',
  19902. ' TObject = class',
  19903. ' end;',
  19904. ' TBird = class(TObject,IBird)',
  19905. ' function GetSize: longint; virtual; abstract;',
  19906. ' procedure SetSize(i: longint); virtual; abstract;',
  19907. ' procedure DoIt(i: longint); virtual; abstract;',
  19908. ' end;',
  19909. 'var',
  19910. ' BirdIntf: IBird;',
  19911. 'begin',
  19912. ' BirdIntf.Size:=BirdIntf.Size;',
  19913. '']);
  19914. ConvertProgram;
  19915. CheckSource('TestClassInterface_Corba',
  19916. LinesToStr([ // statements
  19917. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  19918. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  19919. 'rtl.createClass(this, "TObject", null, function () {',
  19920. ' this.$init = function () {',
  19921. ' };',
  19922. ' this.$final = function () {',
  19923. ' };',
  19924. '});',
  19925. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19926. ' rtl.addIntf(this, $mod.IBird);',
  19927. '});',
  19928. 'this.BirdIntf = null;',
  19929. '']),
  19930. LinesToStr([ // $mod.$main
  19931. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  19932. '']));
  19933. end;
  19934. procedure TTestModule.TestClassInterface_ProcExternalFail;
  19935. begin
  19936. StartProgram(false);
  19937. Add([
  19938. '{$interfaces corba}',
  19939. 'type',
  19940. ' IUnknown = interface',
  19941. ' procedure DoIt; external name ''foo'';',
  19942. ' end;',
  19943. 'begin']);
  19944. SetExpectedParserError(
  19945. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  19946. nParserNoFieldsAllowed);
  19947. ConvertProgram;
  19948. end;
  19949. procedure TTestModule.TestClassInterface_Overloads;
  19950. begin
  19951. StartProgram(false);
  19952. Add([
  19953. '{$interfaces corba}',
  19954. 'type',
  19955. ' integer = longint;',
  19956. ' IUnknown = interface',
  19957. ' procedure DoIt(i: integer);',
  19958. ' procedure DoIt(s: string);',
  19959. ' end;',
  19960. ' IBird = interface(IUnknown)',
  19961. ' procedure DoIt(b: boolean); overload;',
  19962. ' end;',
  19963. ' TObject = class',
  19964. ' end;',
  19965. ' TBird = class(TObject,IBird)',
  19966. ' procedure DoIt(o: TObject);',
  19967. ' procedure DoIt(s: string);',
  19968. ' procedure DoIt(i: integer);',
  19969. ' procedure DoIt(b: boolean);',
  19970. ' end;',
  19971. 'procedure TBird.DoIt(o: TObject); begin end;',
  19972. 'procedure TBird.DoIt(s: string); begin end;',
  19973. 'procedure TBird.DoIt(i: integer); begin end;',
  19974. 'procedure TBird.DoIt(b: boolean); begin end;',
  19975. 'var',
  19976. ' BirdIntf: IBird;',
  19977. 'begin',
  19978. ' BirdIntf.DoIt(3);',
  19979. ' BirdIntf.DoIt(''abc'');',
  19980. ' BirdIntf.DoIt(true);',
  19981. '']);
  19982. ConvertProgram;
  19983. CheckSource('TestClassInterface_Overloads',
  19984. LinesToStr([ // statements
  19985. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  19986. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  19987. 'rtl.createClass(this, "TObject", null, function () {',
  19988. ' this.$init = function () {',
  19989. ' };',
  19990. ' this.$final = function () {',
  19991. ' };',
  19992. '});',
  19993. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19994. ' this.DoIt = function (o) {',
  19995. ' };',
  19996. ' this.DoIt$1 = function (s) {',
  19997. ' };',
  19998. ' this.DoIt$2 = function (i) {',
  19999. ' };',
  20000. ' this.DoIt$3 = function (b) {',
  20001. ' };',
  20002. ' rtl.addIntf(this, $mod.IBird, {',
  20003. ' DoIt$2: "DoIt$3",',
  20004. ' DoIt: "DoIt$2"',
  20005. ' });',
  20006. '});',
  20007. 'this.BirdIntf = null;',
  20008. '']),
  20009. LinesToStr([ // $mod.$main
  20010. '$mod.BirdIntf.DoIt(3);',
  20011. '$mod.BirdIntf.DoIt$1("abc");',
  20012. '$mod.BirdIntf.DoIt$2(true);',
  20013. '']));
  20014. end;
  20015. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  20016. begin
  20017. StartProgram(false);
  20018. Add([
  20019. '{$interfaces corba}',
  20020. 'type',
  20021. ' IBird = interface',
  20022. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20023. ' end;',
  20024. ' IDog = interface',
  20025. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20026. ' end;',
  20027. ' TObject = class(IBird,IDog)',
  20028. ' end;',
  20029. 'begin']);
  20030. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20031. nDuplicateGUIDXInYZ);
  20032. ConvertProgram;
  20033. end;
  20034. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  20035. begin
  20036. StartProgram(false);
  20037. Add([
  20038. '{$interfaces corba}',
  20039. 'type',
  20040. ' IAnimal = interface',
  20041. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20042. ' end;',
  20043. ' IBird = interface(IAnimal)',
  20044. ' end;',
  20045. ' IHawk = interface(IBird)',
  20046. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20047. ' end;',
  20048. 'begin']);
  20049. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20050. nDuplicateGUIDXInYZ);
  20051. ConvertProgram;
  20052. end;
  20053. procedure TTestModule.TestClassInterface_AncestorImpl;
  20054. begin
  20055. StartProgram(false);
  20056. Add([
  20057. '{$interfaces corba}',
  20058. 'type',
  20059. ' integer = longint;',
  20060. ' IUnknown = interface',
  20061. ' procedure DoIt(i: integer);',
  20062. ' end;',
  20063. ' IBird = interface',
  20064. ' procedure Fly(i: integer);',
  20065. ' end;',
  20066. ' TObject = class(IUnknown)',
  20067. ' procedure DoIt(i: integer);',
  20068. ' end;',
  20069. ' TBird = class(IBird)',
  20070. ' procedure Fly(i: integer);',
  20071. ' end;',
  20072. 'procedure TObject.DoIt(i: integer); begin end;',
  20073. 'procedure TBird.Fly(i: integer); begin end;',
  20074. 'begin',
  20075. '']);
  20076. ConvertProgram;
  20077. CheckSource('TestClassInterface_AncestorIntf',
  20078. LinesToStr([ // statements
  20079. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20080. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20081. 'rtl.createClass(this, "TObject", null, function () {',
  20082. ' this.$init = function () {',
  20083. ' };',
  20084. ' this.$final = function () {',
  20085. ' };',
  20086. ' this.DoIt = function (i) {',
  20087. ' };',
  20088. ' rtl.addIntf(this, $mod.IUnknown);',
  20089. '});',
  20090. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20091. ' this.Fly = function (i) {',
  20092. ' };',
  20093. ' rtl.addIntf(this, $mod.IBird);',
  20094. ' rtl.addIntf(this, $mod.IUnknown);',
  20095. '});',
  20096. '']),
  20097. LinesToStr([ // $mod.$main
  20098. '']));
  20099. end;
  20100. procedure TTestModule.TestClassInterface_ImplReintroduce;
  20101. begin
  20102. StartProgram(false);
  20103. Add([
  20104. '{$interfaces corba}',
  20105. 'type',
  20106. ' integer = longint;',
  20107. ' IBird = interface',
  20108. ' procedure DoIt(i: integer);',
  20109. ' end;',
  20110. ' TObject = class',
  20111. ' procedure DoIt(i: integer);',
  20112. ' end;',
  20113. ' TBird = class(IBird)',
  20114. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20115. ' end;',
  20116. 'procedure TObject.DoIt(i: integer); begin end;',
  20117. 'procedure TBird.DoIt(i: integer); begin end;',
  20118. 'begin',
  20119. '']);
  20120. ConvertProgram;
  20121. CheckSource('TestClassInterface_ImplReintroduce',
  20122. LinesToStr([ // statements
  20123. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20124. 'rtl.createClass(this, "TObject", null, function () {',
  20125. ' this.$init = function () {',
  20126. ' };',
  20127. ' this.$final = function () {',
  20128. ' };',
  20129. ' this.DoIt = function (i) {',
  20130. ' };',
  20131. '});',
  20132. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20133. ' this.DoIt$1 = function (i) {',
  20134. ' };',
  20135. ' rtl.addIntf(this, $mod.IBird, {',
  20136. ' DoIt: "DoIt$1"',
  20137. ' });',
  20138. '});',
  20139. '']),
  20140. LinesToStr([ // $mod.$main
  20141. '']));
  20142. end;
  20143. procedure TTestModule.TestClassInterface_MethodResolution;
  20144. begin
  20145. StartProgram(false);
  20146. Add([
  20147. '{$interfaces corba}',
  20148. 'type',
  20149. ' IUnknown = interface',
  20150. ' procedure Walk(i: longint);',
  20151. ' end;',
  20152. ' IBird = interface(IUnknown)',
  20153. ' procedure Walk(b: boolean); overload;',
  20154. ' procedure Fly(s: string);',
  20155. ' end;',
  20156. ' TObject = class',
  20157. ' end;',
  20158. ' TBird = class(TObject,IBird)',
  20159. ' procedure IBird.Fly = Move;',
  20160. ' procedure IBird.Walk = Hop;',
  20161. ' procedure Hop(i: longint);',
  20162. ' procedure Move(s: string);',
  20163. ' procedure Hop(b: boolean);',
  20164. ' end;',
  20165. 'procedure TBird.Move(s: string); begin end;',
  20166. 'procedure TBird.Hop(i: longint); begin end;',
  20167. 'procedure TBird.Hop(b: boolean); begin end;',
  20168. 'var',
  20169. ' BirdIntf: IBird;',
  20170. 'begin',
  20171. ' BirdIntf.Walk(3);',
  20172. ' BirdIntf.Walk(true);',
  20173. ' BirdIntf.Fly(''abc'');',
  20174. '']);
  20175. ConvertProgram;
  20176. CheckSource('TestClassInterface_MethodResolution',
  20177. LinesToStr([ // statements
  20178. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20179. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20180. 'rtl.createClass(this, "TObject", null, function () {',
  20181. ' this.$init = function () {',
  20182. ' };',
  20183. ' this.$final = function () {',
  20184. ' };',
  20185. '});',
  20186. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20187. ' this.Hop = function (i) {',
  20188. ' };',
  20189. ' this.Move = function (s) {',
  20190. ' };',
  20191. ' this.Hop$1 = function (b) {',
  20192. ' };',
  20193. ' rtl.addIntf(this, $mod.IBird, {',
  20194. ' Walk$1: "Hop$1",',
  20195. ' Fly: "Move",',
  20196. ' Walk: "Hop"',
  20197. ' });',
  20198. '});',
  20199. 'this.BirdIntf = null;',
  20200. '']),
  20201. LinesToStr([ // $mod.$main
  20202. '$mod.BirdIntf.Walk(3);',
  20203. '$mod.BirdIntf.Walk$1(true);',
  20204. '$mod.BirdIntf.Fly("abc");',
  20205. '']));
  20206. end;
  20207. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  20208. begin
  20209. StartProgram(false);
  20210. Add([
  20211. '{$interfaces com}',
  20212. 'type',
  20213. ' IUnknown = interface',
  20214. ' function _AddRef: longint;',
  20215. ' procedure Walk;',
  20216. ' end;',
  20217. ' IBird = interface end;',
  20218. ' IDog = interface end;',
  20219. ' TObject = class(IBird,IDog)',
  20220. ' function _AddRef: longint; virtual; abstract;',
  20221. ' procedure Walk; virtual; abstract;',
  20222. ' end;',
  20223. ' TBird = class(IUnknown)',
  20224. ' end;',
  20225. 'begin',
  20226. '']);
  20227. ConvertProgram;
  20228. CheckSource('TestClassInterface_COM_AncestorLess',
  20229. LinesToStr([ // statements
  20230. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20231. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20232. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20233. 'rtl.createClass(this, "TObject", null, function () {',
  20234. ' this.$init = function () {',
  20235. ' };',
  20236. ' this.$final = function () {',
  20237. ' };',
  20238. ' rtl.addIntf(this, $mod.IBird);',
  20239. ' rtl.addIntf(this, $mod.IDog);',
  20240. '});',
  20241. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20242. ' rtl.addIntf(this, $mod.IUnknown);',
  20243. ' rtl.addIntf(this, $mod.IBird);',
  20244. ' rtl.addIntf(this, $mod.IDog);',
  20245. '});',
  20246. '']),
  20247. LinesToStr([ // $mod.$main
  20248. '']));
  20249. end;
  20250. procedure TTestModule.TestClassInterface_MethodOverride;
  20251. begin
  20252. StartProgram(false);
  20253. Add([
  20254. '{$interfaces corba}',
  20255. 'type',
  20256. ' IUnknown = interface',
  20257. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20258. ' procedure Go;',
  20259. ' end;',
  20260. ' TObject = class(IUnknown)',
  20261. ' procedure Go; virtual; abstract;',
  20262. ' end;',
  20263. ' TBird = class',
  20264. ' procedure Go; override;',
  20265. ' end;',
  20266. ' TCat = class(TObject)',
  20267. ' procedure Go; override;',
  20268. ' end;',
  20269. ' TDog = class(TObject, IUnknown)',
  20270. ' procedure Go; override;',
  20271. ' end;',
  20272. 'procedure TBird.Go; begin end;',
  20273. 'procedure TCat.Go; begin end;',
  20274. 'procedure TDog.Go; begin end;',
  20275. 'begin',
  20276. '']);
  20277. ConvertProgram;
  20278. CheckSource('TestClassInterface_MethodOverride',
  20279. LinesToStr([ // statements
  20280. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20281. 'rtl.createClass(this, "TObject", null, function () {',
  20282. ' this.$init = function () {',
  20283. ' };',
  20284. ' this.$final = function () {',
  20285. ' };',
  20286. ' rtl.addIntf(this, $mod.IUnknown);',
  20287. '});',
  20288. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20289. ' this.Go = function () {',
  20290. ' };',
  20291. ' rtl.addIntf(this, $mod.IUnknown);',
  20292. '});',
  20293. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20294. ' this.Go = function () {',
  20295. ' };',
  20296. ' rtl.addIntf(this, $mod.IUnknown);',
  20297. '});',
  20298. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20299. ' this.Go = function () {',
  20300. ' };',
  20301. ' rtl.addIntf(this, $mod.IUnknown);',
  20302. '});',
  20303. '']),
  20304. LinesToStr([ // $mod.$main
  20305. '']));
  20306. end;
  20307. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20308. begin
  20309. StartProgram(false);
  20310. Add([
  20311. '{$interfaces corba}',
  20312. 'type',
  20313. ' IUnknown = interface',
  20314. ' end;',
  20315. ' IBird = interface(IUnknown)',
  20316. ' procedure Fly(s: string);',
  20317. ' end;',
  20318. ' IEagle = interface(IBird)',
  20319. ' end;',
  20320. ' IDove = interface(IBird)',
  20321. ' end;',
  20322. ' ISwallow = interface(IBird)',
  20323. ' end;',
  20324. ' TObject = class',
  20325. ' end;',
  20326. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20327. ' procedure Fly(s: string); virtual; abstract;',
  20328. ' end;',
  20329. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20330. ' FBirdIntf: IBird;',
  20331. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20332. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20333. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20334. ' FDoveObj: TBird;',
  20335. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20336. ' function GetSwallowObj: TBird; virtual; abstract;',
  20337. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20338. ' end;',
  20339. 'begin',
  20340. '']);
  20341. ConvertProgram;
  20342. CheckSource('TestClassInterface_Corba_Delegation',
  20343. LinesToStr([ // statements
  20344. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20345. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20346. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20347. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20348. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20349. 'rtl.createClass(this, "TObject", null, function () {',
  20350. ' this.$init = function () {',
  20351. ' };',
  20352. ' this.$final = function () {',
  20353. ' };',
  20354. '});',
  20355. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20356. ' rtl.addIntf(this, $mod.IBird);',
  20357. ' rtl.addIntf(this, $mod.IEagle);',
  20358. ' rtl.addIntf(this, $mod.IDove);',
  20359. ' rtl.addIntf(this, $mod.ISwallow);',
  20360. '});',
  20361. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20362. ' this.$init = function () {',
  20363. ' $mod.TObject.$init.call(this);',
  20364. ' this.FBirdIntf = null;',
  20365. ' this.FDoveObj = null;',
  20366. ' };',
  20367. ' this.$final = function () {',
  20368. ' this.FBirdIntf = undefined;',
  20369. ' this.FDoveObj = undefined;',
  20370. ' $mod.TObject.$final.call(this);',
  20371. ' };',
  20372. ' this.$intfmaps = {',
  20373. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20374. ' return this.FBirdIntf;',
  20375. ' },',
  20376. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20377. ' return this.GetEagleIntf();',
  20378. ' },',
  20379. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20380. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20381. ' },',
  20382. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20383. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20384. ' }',
  20385. ' };',
  20386. '});',
  20387. '']),
  20388. LinesToStr([ // $mod.$main
  20389. '']));
  20390. end;
  20391. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20392. begin
  20393. StartProgram(false);
  20394. Add([
  20395. '{$interfaces corba}',
  20396. 'type',
  20397. ' IUnknown = interface',
  20398. ' end;',
  20399. ' IBird = interface(IUnknown)',
  20400. ' procedure Fly(s: string);',
  20401. ' end;',
  20402. ' IEagle = interface(IBird)',
  20403. ' end;',
  20404. ' IDove = interface(IBird)',
  20405. ' end;',
  20406. ' ISwallow = interface(IBird)',
  20407. ' end;',
  20408. ' TObject = class',
  20409. ' end;',
  20410. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20411. ' procedure Fly(s: string); virtual; abstract;',
  20412. ' end;',
  20413. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20414. ' private',
  20415. ' class var FBirdIntf: IBird;',
  20416. ' class var FDoveObj: TBird;',
  20417. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20418. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20419. ' protected',
  20420. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20421. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20422. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20423. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20424. ' end;',
  20425. 'begin',
  20426. '']);
  20427. ConvertProgram;
  20428. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20429. LinesToStr([ // statements
  20430. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20431. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20432. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20433. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20434. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20435. 'rtl.createClass(this, "TObject", null, function () {',
  20436. ' this.$init = function () {',
  20437. ' };',
  20438. ' this.$final = function () {',
  20439. ' };',
  20440. '});',
  20441. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20442. ' rtl.addIntf(this, $mod.IBird);',
  20443. ' rtl.addIntf(this, $mod.IEagle);',
  20444. ' rtl.addIntf(this, $mod.IDove);',
  20445. ' rtl.addIntf(this, $mod.ISwallow);',
  20446. '});',
  20447. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20448. ' this.FBirdIntf = null;',
  20449. ' this.FDoveObj = null;',
  20450. ' this.$intfmaps = {',
  20451. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20452. ' return this.FBirdIntf;',
  20453. ' },',
  20454. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20455. ' return this.GetEagleIntf();',
  20456. ' },',
  20457. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20458. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20459. ' },',
  20460. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20461. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20462. ' }',
  20463. ' };',
  20464. '});',
  20465. '']),
  20466. LinesToStr([ // $mod.$main
  20467. '']));
  20468. end;
  20469. procedure TTestModule.TestClassInterface_Corba_Operators;
  20470. begin
  20471. StartProgram(false);
  20472. Add([
  20473. '{$interfaces corba}',
  20474. 'type',
  20475. ' IUnknown = interface',
  20476. ' end;',
  20477. ' IBird = interface(IUnknown)',
  20478. ' function GetItems(Index: longint): longint;',
  20479. ' procedure SetItems(Index: longint; Value: longint);',
  20480. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20481. ' end;',
  20482. ' TObject = class',
  20483. ' end;',
  20484. ' TBird = class(TObject,IBird)',
  20485. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20486. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20487. ' end;',
  20488. 'var',
  20489. ' IntfVar: IBird = nil;',
  20490. ' IntfVar2: IBird;',
  20491. ' ObjVar: TBird;',
  20492. ' v: JSValue;',
  20493. 'begin',
  20494. ' IntfVar:=nil;',
  20495. ' IntfVar[3]:=IntfVar[4];',
  20496. ' if Assigned(IntfVar) then ;',
  20497. ' IntfVar:=IntfVar2;',
  20498. ' IntfVar:=ObjVar;',
  20499. ' if IntfVar=IntfVar2 then ;',
  20500. ' if IntfVar<>IntfVar2 then ;',
  20501. ' if IntfVar is IBird then ;',
  20502. ' if IntfVar is TBird then ;',
  20503. ' if ObjVar is IBird then ;',
  20504. ' IntfVar:=IntfVar2 as IBird;',
  20505. ' ObjVar:=IntfVar2 as TBird;',
  20506. ' IntfVar:=ObjVar as IBird;',
  20507. ' IntfVar:=IBird(IntfVar2);',
  20508. ' ObjVar:=TBird(IntfVar);',
  20509. ' IntfVar:=IBird(ObjVar);',
  20510. ' v:=IntfVar;',
  20511. ' IntfVar:=IBird(v);',
  20512. ' if v is IBird then ;',
  20513. ' v:=JSValue(IntfVar);',
  20514. ' v:=IBird;',
  20515. '']);
  20516. ConvertProgram;
  20517. CheckSource('TestClassInterface_Corba_Operators',
  20518. LinesToStr([ // statements
  20519. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20520. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20521. 'rtl.createClass(this, "TObject", null, function () {',
  20522. ' this.$init = function () {',
  20523. ' };',
  20524. ' this.$final = function () {',
  20525. ' };',
  20526. '});',
  20527. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20528. ' rtl.addIntf(this, $mod.IBird);',
  20529. '});',
  20530. 'this.IntfVar = null;',
  20531. 'this.IntfVar2 = null;',
  20532. 'this.ObjVar = null;',
  20533. 'this.v = undefined;',
  20534. '']),
  20535. LinesToStr([ // $mod.$main
  20536. '$mod.IntfVar = null;',
  20537. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20538. 'if ($mod.IntfVar != null) ;',
  20539. '$mod.IntfVar = $mod.IntfVar2;',
  20540. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20541. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20542. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20543. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20544. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20545. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20546. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20547. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20548. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20549. '$mod.IntfVar = $mod.IntfVar2;',
  20550. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20551. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20552. '$mod.v = $mod.IntfVar;',
  20553. '$mod.IntfVar = rtl.getObject($mod.v);',
  20554. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20555. '$mod.v = $mod.IntfVar;',
  20556. '$mod.v = $mod.IBird;',
  20557. '']));
  20558. end;
  20559. procedure TTestModule.TestClassInterface_Corba_Args;
  20560. begin
  20561. StartProgram(false);
  20562. Add([
  20563. '{$interfaces corba}',
  20564. 'type',
  20565. ' IUnknown = interface',
  20566. ' end;',
  20567. ' IBird = interface(IUnknown)',
  20568. ' end;',
  20569. ' TObject = class',
  20570. ' end;',
  20571. ' TBird = class(TObject,IBird)',
  20572. ' end;',
  20573. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  20574. 'begin',
  20575. ' DoIt(i,i,i);',
  20576. 'end;',
  20577. 'procedure Change(var i: IBird; out j: IBird);',
  20578. 'begin',
  20579. ' DoIt(i,i,i);',
  20580. ' Change(i,i);',
  20581. 'end;',
  20582. 'var',
  20583. ' i: IBird;',
  20584. ' o: TBird;',
  20585. 'begin',
  20586. ' DoIt(i,i,i);',
  20587. ' Change(i,i);',
  20588. ' DoIt(o,o,o);',
  20589. '']);
  20590. ConvertProgram;
  20591. CheckSource('TestClassInterface_Corba_Args',
  20592. LinesToStr([ // statements
  20593. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20594. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  20595. 'rtl.createClass(this, "TObject", null, function () {',
  20596. ' this.$init = function () {',
  20597. ' };',
  20598. ' this.$final = function () {',
  20599. ' };',
  20600. '});',
  20601. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20602. ' rtl.addIntf(this, $mod.IBird);',
  20603. '});',
  20604. 'this.DoIt = function (u, i, j) {',
  20605. ' $mod.DoIt({',
  20606. ' get: function () {',
  20607. ' return i;',
  20608. ' },',
  20609. ' set: function (v) {',
  20610. ' i = v;',
  20611. ' }',
  20612. ' }, i, i);',
  20613. '};',
  20614. 'this.Change = function (i, j) {',
  20615. ' $mod.DoIt(i, i.get(), i.get());',
  20616. ' $mod.Change(i, i);',
  20617. '};',
  20618. 'this.i = null;',
  20619. 'this.o = null;',
  20620. '']),
  20621. LinesToStr([ // $mod.$main
  20622. '$mod.DoIt({',
  20623. ' p: $mod,',
  20624. ' get: function () {',
  20625. ' return this.p.i;',
  20626. ' },',
  20627. ' set: function (v) {',
  20628. ' this.p.i = v;',
  20629. ' }',
  20630. '}, $mod.i, $mod.i);',
  20631. '$mod.Change({',
  20632. ' p: $mod,',
  20633. ' get: function () {',
  20634. ' return this.p.i;',
  20635. ' },',
  20636. ' set: function (v) {',
  20637. ' this.p.i = v;',
  20638. ' }',
  20639. '}, {',
  20640. ' p: $mod,',
  20641. ' get: function () {',
  20642. ' return this.p.i;',
  20643. ' },',
  20644. ' set: function (v) {',
  20645. ' this.p.i = v;',
  20646. ' }',
  20647. '});',
  20648. '$mod.DoIt({',
  20649. ' p: $mod,',
  20650. ' get: function () {',
  20651. ' return this.p.o;',
  20652. ' },',
  20653. ' set: function (v) {',
  20654. ' this.p.o = v;',
  20655. ' }',
  20656. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  20657. '']));
  20658. end;
  20659. procedure TTestModule.TestClassInterface_Corba_ForIn;
  20660. begin
  20661. StartProgram(false);
  20662. Add([
  20663. '{$interfaces corba}',
  20664. 'type',
  20665. ' IUnknown = interface end;',
  20666. ' TObject = class',
  20667. ' Id: longint;',
  20668. ' end;',
  20669. ' IEnumerator = interface(IUnknown)',
  20670. ' function GetCurrent: TObject;',
  20671. ' function MoveNext: Boolean;',
  20672. ' property Current: TObject read GetCurrent;',
  20673. ' end;',
  20674. ' IEnumerable = interface(IUnknown)',
  20675. ' function GetEnumerator: IEnumerator;',
  20676. ' end;',
  20677. 'var',
  20678. ' o: TObject;',
  20679. ' i: IEnumerable;',
  20680. 'begin',
  20681. ' for o in i do o.Id:=3;',
  20682. '']);
  20683. ConvertProgram;
  20684. CheckSource('TestClassInterface_Corba_ForIn',
  20685. LinesToStr([ // statements
  20686. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20687. 'rtl.createClass(this, "TObject", null, function () {',
  20688. ' this.$init = function () {',
  20689. ' this.Id = 0;',
  20690. ' };',
  20691. ' this.$final = function () {',
  20692. ' };',
  20693. '});',
  20694. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20695. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20696. 'this.o = null;',
  20697. 'this.i = null;',
  20698. '']),
  20699. LinesToStr([ // $mod.$main
  20700. 'var $in = $mod.i.GetEnumerator();',
  20701. 'while ($in.MoveNext()) {',
  20702. ' $mod.o = $in.GetCurrent();',
  20703. ' $mod.o.Id = 3;',
  20704. '};',
  20705. '']));
  20706. end;
  20707. procedure TTestModule.TestClassInterface_COM_AssignVar;
  20708. begin
  20709. StartProgram(false);
  20710. Add([
  20711. '{$interfaces com}',
  20712. 'type',
  20713. ' IUnknown = interface',
  20714. ' function _AddRef: longint;',
  20715. ' function _Release: longint;',
  20716. ' end;',
  20717. ' TObject = class(IUnknown)',
  20718. ' function _AddRef: longint; virtual; abstract;',
  20719. ' function _Release: longint; virtual; abstract;',
  20720. ' end;',
  20721. 'var',
  20722. ' i: IUnknown;',
  20723. 'procedure DoGlobal(o: TObject);',
  20724. 'begin',
  20725. ' i:=nil;',
  20726. ' i:=o;',
  20727. ' i:=i;',
  20728. 'end;',
  20729. 'procedure DoLocal(o: TObject);',
  20730. 'const k: IUnknown = nil;',
  20731. 'var j: IUnknown;',
  20732. 'begin',
  20733. ' k:=o;',
  20734. ' k:=i;',
  20735. ' j:=o;',
  20736. ' j:=i;',
  20737. 'end;',
  20738. 'var o: TObject;',
  20739. 'begin',
  20740. ' i:=nil;',
  20741. ' i:=o;',
  20742. '']);
  20743. ConvertProgram;
  20744. CheckSource('TestClassInterface_COM_AssignVar',
  20745. LinesToStr([ // statements
  20746. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20747. 'rtl.createClass(this, "TObject", null, function () {',
  20748. ' this.$init = function () {',
  20749. ' };',
  20750. ' this.$final = function () {',
  20751. ' };',
  20752. ' rtl.addIntf(this, $mod.IUnknown);',
  20753. '});',
  20754. 'this.i = null;',
  20755. 'this.DoGlobal = function (o) {',
  20756. ' rtl.setIntfP($mod, "i", null);',
  20757. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  20758. ' rtl.setIntfP($mod, "i", $mod.i);',
  20759. '};',
  20760. 'var k = null;',
  20761. 'this.DoLocal = function (o) {',
  20762. ' var j = null;',
  20763. ' try{',
  20764. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20765. ' k = rtl.setIntfL(k, $mod.i);',
  20766. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20767. ' j = rtl.setIntfL(j, $mod.i);',
  20768. ' }finally{',
  20769. ' rtl._Release(j);',
  20770. ' };',
  20771. '};',
  20772. 'this.o = null;',
  20773. '']),
  20774. LinesToStr([ // $mod.$main
  20775. 'rtl.setIntfP($mod, "i", null);',
  20776. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  20777. '']));
  20778. end;
  20779. procedure TTestModule.TestClassInterface_COM_AssignArg;
  20780. begin
  20781. StartProgram(false);
  20782. Add([
  20783. '{$interfaces com}',
  20784. 'type',
  20785. ' IUnknown = interface',
  20786. ' function _AddRef: longint;',
  20787. ' function _Release: longint;',
  20788. ' end;',
  20789. ' TObject = class(IUnknown)',
  20790. ' function _AddRef: longint; virtual; abstract;',
  20791. ' function _Release: longint; virtual; abstract;',
  20792. ' end;',
  20793. 'procedure DoDefault(i, j: IUnknown);',
  20794. 'begin',
  20795. ' i:=nil;',
  20796. ' i:=j;',
  20797. 'end;',
  20798. 'begin',
  20799. '']);
  20800. ConvertProgram;
  20801. CheckSource('TestClassInterface_COM_AssignArg',
  20802. LinesToStr([ // statements
  20803. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20804. 'rtl.createClass(this, "TObject", null, function () {',
  20805. ' this.$init = function () {',
  20806. ' };',
  20807. ' this.$final = function () {',
  20808. ' };',
  20809. ' rtl.addIntf(this, $mod.IUnknown);',
  20810. '});',
  20811. 'this.DoDefault = function (i, j) {',
  20812. ' rtl._AddRef(i);',
  20813. ' try {',
  20814. ' i = rtl.setIntfL(i, null);',
  20815. ' i = rtl.setIntfL(i, j);',
  20816. ' } finally {',
  20817. ' rtl._Release(i);',
  20818. ' };',
  20819. '};',
  20820. '']),
  20821. LinesToStr([ // $mod.$main
  20822. '']));
  20823. end;
  20824. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  20825. begin
  20826. StartProgram(false);
  20827. Add([
  20828. '{$interfaces com}',
  20829. 'type',
  20830. ' IUnknown = interface',
  20831. ' function _AddRef: longint;',
  20832. ' function _Release: longint;',
  20833. ' end;',
  20834. ' TObject = class(IUnknown)',
  20835. ' function _AddRef: longint; virtual; abstract;',
  20836. ' function _Release: longint; virtual; abstract;',
  20837. ' end;',
  20838. 'function DoDefault(i: IUnknown): IUnknown;',
  20839. 'begin',
  20840. ' Result:=i;',
  20841. ' if Result<>nil then exit;',
  20842. 'end;',
  20843. 'begin',
  20844. '']);
  20845. ConvertProgram;
  20846. CheckSource('TestClassInterface_COM_FunctionResult',
  20847. LinesToStr([ // statements
  20848. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20849. 'rtl.createClass(this, "TObject", null, function () {',
  20850. ' this.$init = function () {',
  20851. ' };',
  20852. ' this.$final = function () {',
  20853. ' };',
  20854. ' rtl.addIntf(this, $mod.IUnknown);',
  20855. '});',
  20856. 'this.DoDefault = function (i) {',
  20857. ' var Result = null;',
  20858. ' var $ok = false;',
  20859. ' try {',
  20860. ' Result = rtl.setIntfL(Result, i);',
  20861. ' if(Result !== null){',
  20862. ' $ok = true;',
  20863. ' return Result;',
  20864. ' };',
  20865. ' $ok = true;',
  20866. ' } finally {',
  20867. ' if(!$ok) rtl._Release(Result);',
  20868. ' };',
  20869. ' return Result;',
  20870. '};',
  20871. '']),
  20872. LinesToStr([ // $mod.$main
  20873. '']));
  20874. end;
  20875. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  20876. begin
  20877. StartProgram(false);
  20878. Add([
  20879. '{$interfaces com}',
  20880. 'type',
  20881. ' IUnknown = interface',
  20882. ' function _AddRef: longint;',
  20883. ' function _Release: longint;',
  20884. ' end;',
  20885. ' TObject = class(IUnknown)',
  20886. ' function _AddRef: longint; virtual; abstract;',
  20887. ' function _Release: longint; virtual; abstract;',
  20888. ' function GetIntf: IUnknown; virtual;',
  20889. ' end;',
  20890. ' TMouse = class',
  20891. ' function GetIntf: IUnknown; override;',
  20892. ' end;',
  20893. 'function TObject.GetIntf: IUnknown; begin end;',
  20894. 'function TMouse.GetIntf: IUnknown;',
  20895. 'var i: IUnknown;',
  20896. 'begin',
  20897. ' inherited;',
  20898. ' inherited GetIntf;',
  20899. ' inherited GetIntf();',
  20900. ' Result:=inherited GetIntf;',
  20901. ' Result:=inherited GetIntf();',
  20902. ' i:=inherited GetIntf;',
  20903. ' i:=inherited GetIntf();',
  20904. 'end;',
  20905. 'begin',
  20906. '']);
  20907. ConvertProgram;
  20908. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  20909. LinesToStr([ // statements
  20910. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20911. 'rtl.createClass(this, "TObject", null, function () {',
  20912. ' this.$init = function () {',
  20913. ' };',
  20914. ' this.$final = function () {',
  20915. ' };',
  20916. ' this.GetIntf = function () {',
  20917. ' var Result = null;',
  20918. ' return Result;',
  20919. ' };',
  20920. ' rtl.addIntf(this, $mod.IUnknown);',
  20921. '});',
  20922. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  20923. ' this.GetIntf = function () {',
  20924. ' var Result = null;',
  20925. ' var i = null;',
  20926. ' var $ir = rtl.createIntfRefs();',
  20927. ' var $ok = false;',
  20928. ' try {',
  20929. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  20930. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  20931. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  20932. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  20933. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  20934. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  20935. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  20936. ' $ok = true;',
  20937. ' } finally {',
  20938. ' $ir.free();',
  20939. ' rtl._Release(i);',
  20940. ' if (!$ok) rtl._Release(Result);',
  20941. ' };',
  20942. ' return Result;',
  20943. ' };',
  20944. ' rtl.addIntf(this, $mod.IUnknown);',
  20945. '});',
  20946. '']),
  20947. LinesToStr([ // $mod.$main
  20948. '']));
  20949. end;
  20950. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  20951. begin
  20952. StartProgram(false);
  20953. Add([
  20954. '{$interfaces com}',
  20955. 'type',
  20956. ' IUnknown = interface',
  20957. ' function _AddRef: longint;',
  20958. ' function _Release: longint;',
  20959. ' end;',
  20960. ' TObject = class(IUnknown)',
  20961. ' function _AddRef: longint; virtual; abstract;',
  20962. ' function _Release: longint; virtual; abstract;',
  20963. ' end;',
  20964. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  20965. 'begin',
  20966. ' if i is IUnknown then ;',
  20967. ' if o is IUnknown then ;',
  20968. ' if i is TObject then ;',
  20969. ' i:=j as IUnknown;',
  20970. ' i:=o as IUnknown;',
  20971. ' o:=j as TObject;',
  20972. ' i:=IUnknown(j);',
  20973. ' i:=IUnknown(o);',
  20974. ' o:=TObject(i);',
  20975. 'end;',
  20976. 'begin',
  20977. '']);
  20978. ConvertProgram;
  20979. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  20980. LinesToStr([ // statements
  20981. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20982. 'rtl.createClass(this, "TObject", null, function () {',
  20983. ' this.$init = function () {',
  20984. ' };',
  20985. ' this.$final = function () {',
  20986. ' };',
  20987. ' rtl.addIntf(this, $mod.IUnknown);',
  20988. '});',
  20989. 'this.DoDefault = function (i, j, o) {',
  20990. ' rtl._AddRef(i);',
  20991. ' try {',
  20992. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  20993. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  20994. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  20995. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  20996. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20997. ' o = rtl.intfAsClass(j, $mod.TObject);',
  20998. ' i = rtl.setIntfL(i, j);',
  20999. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21000. ' o = rtl.intfToClass(i, $mod.TObject);',
  21001. ' } finally {',
  21002. ' rtl._Release(i);',
  21003. ' };',
  21004. '};',
  21005. '']),
  21006. LinesToStr([ // $mod.$main
  21007. '']));
  21008. end;
  21009. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21010. begin
  21011. StartProgram(false);
  21012. Add([
  21013. '{$interfaces com}',
  21014. 'type',
  21015. ' IUnknown = interface',
  21016. ' function _AddRef: longint;',
  21017. ' function _Release: longint;',
  21018. ' end;',
  21019. ' TObject = class(IUnknown)',
  21020. ' function _AddRef: longint; virtual; abstract;',
  21021. ' function _Release: longint; virtual; abstract;',
  21022. ' end;',
  21023. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21024. 'var o: TObject;',
  21025. 'begin',
  21026. ' DoIt(v,v,v,v);',
  21027. ' DoIt(o,o,k,k);',
  21028. 'end;',
  21029. 'procedure DoSome;',
  21030. 'var v: IUnknown;',
  21031. 'begin',
  21032. ' DoIt(v,v,v,v);',
  21033. 'end;',
  21034. 'var i: IUnknown;',
  21035. 'begin',
  21036. ' DoIt(i,i,i,i);',
  21037. '']);
  21038. ConvertProgram;
  21039. CheckSource('TestClassInterface_COM_PassAsArg',
  21040. LinesToStr([ // statements
  21041. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21042. 'rtl.createClass(this, "TObject", null, function () {',
  21043. ' this.$init = function () {',
  21044. ' };',
  21045. ' this.$final = function () {',
  21046. ' };',
  21047. ' rtl.addIntf(this, $mod.IUnknown);',
  21048. '});',
  21049. 'this.DoIt = function (v, j, k, l) {',
  21050. ' var o = null;',
  21051. ' var $ir = rtl.createIntfRefs();',
  21052. ' rtl._AddRef(v);',
  21053. ' try {',
  21054. ' $mod.DoIt(v, v, {',
  21055. ' get: function () {',
  21056. ' return v;',
  21057. ' },',
  21058. ' set: function (w) {',
  21059. ' v = rtl.setIntfL(v, w);',
  21060. ' }',
  21061. ' }, {',
  21062. ' get: function () {',
  21063. ' return v;',
  21064. ' },',
  21065. ' set: function (w) {',
  21066. ' v = rtl.setIntfL(v, w);',
  21067. ' }',
  21068. ' });',
  21069. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21070. ' } finally {',
  21071. ' $ir.free();',
  21072. ' rtl._Release(v);',
  21073. ' };',
  21074. '};',
  21075. 'this.DoSome = function () {',
  21076. ' var v = null;',
  21077. ' try {',
  21078. ' $mod.DoIt(v, v, {',
  21079. ' get: function () {',
  21080. ' return v;',
  21081. ' },',
  21082. ' set: function (w) {',
  21083. ' v = rtl.setIntfL(v, w);',
  21084. ' }',
  21085. ' }, {',
  21086. ' get: function () {',
  21087. ' return v;',
  21088. ' },',
  21089. ' set: function (w) {',
  21090. ' v = rtl.setIntfL(v, w);',
  21091. ' }',
  21092. ' });',
  21093. ' } finally {',
  21094. ' rtl._Release(v);',
  21095. ' };',
  21096. '};',
  21097. 'this.i = null;',
  21098. '']),
  21099. LinesToStr([ // $mod.$main
  21100. '$mod.DoIt($mod.i, $mod.i, {',
  21101. ' p: $mod,',
  21102. ' get: function () {',
  21103. ' return this.p.i;',
  21104. ' },',
  21105. ' set: function (v) {',
  21106. ' rtl.setIntfP(this.p, "i", v);',
  21107. ' }',
  21108. '}, {',
  21109. ' p: $mod,',
  21110. ' get: function () {',
  21111. ' return this.p.i;',
  21112. ' },',
  21113. ' set: function (v) {',
  21114. ' rtl.setIntfP(this.p, "i", v);',
  21115. ' }',
  21116. '});',
  21117. '']));
  21118. end;
  21119. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21120. begin
  21121. StartProgram(false);
  21122. Add([
  21123. '{$interfaces com}',
  21124. 'type',
  21125. ' IUnknown = interface',
  21126. ' function _AddRef: longint;',
  21127. ' function _Release: longint;',
  21128. ' end;',
  21129. ' TObject = class(IUnknown)',
  21130. ' function _AddRef: longint; virtual; abstract;',
  21131. ' function _Release: longint; virtual; abstract;',
  21132. ' end;',
  21133. 'procedure DoIt(out i);',
  21134. 'begin end;',
  21135. 'procedure DoSome;',
  21136. 'var v: IUnknown;',
  21137. 'begin',
  21138. ' DoIt(v);',
  21139. 'end;',
  21140. 'function GetIt: IUnknown;',
  21141. 'begin',
  21142. ' DoIt(Result);',
  21143. 'end;',
  21144. 'var i: IUnknown;',
  21145. 'begin',
  21146. ' DoIt(i);',
  21147. '']);
  21148. ConvertProgram;
  21149. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21150. LinesToStr([ // statements
  21151. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21152. 'rtl.createClass(this, "TObject", null, function () {',
  21153. ' this.$init = function () {',
  21154. ' };',
  21155. ' this.$final = function () {',
  21156. ' };',
  21157. ' rtl.addIntf(this, $mod.IUnknown);',
  21158. '});',
  21159. 'this.DoIt = function (i) {',
  21160. '};',
  21161. 'this.DoSome = function () {',
  21162. ' var v = null;',
  21163. ' try {',
  21164. ' $mod.DoIt({',
  21165. ' get: function () {',
  21166. ' return v;',
  21167. ' },',
  21168. ' set: function (w) {',
  21169. ' v = w;',
  21170. ' }',
  21171. ' });',
  21172. ' } finally {',
  21173. ' rtl._Release(v);',
  21174. ' };',
  21175. '};',
  21176. 'this.GetIt = function () {',
  21177. ' var Result = null;',
  21178. ' var $ok = false;',
  21179. ' try {',
  21180. ' $mod.DoIt({',
  21181. ' get: function () {',
  21182. ' return Result;',
  21183. ' },',
  21184. ' set: function (v) {',
  21185. ' Result = v;',
  21186. ' }',
  21187. ' });',
  21188. ' $ok = true;',
  21189. ' } finally {',
  21190. ' if (!$ok) rtl._Release(Result);',
  21191. ' };',
  21192. ' return Result;',
  21193. '};',
  21194. 'this.i = null;',
  21195. '']),
  21196. LinesToStr([ // $mod.$main
  21197. 'try {',
  21198. ' $mod.DoIt({',
  21199. ' p: $mod,',
  21200. ' get: function () {',
  21201. ' return this.p.i;',
  21202. ' },',
  21203. ' set: function (v) {',
  21204. ' this.p.i = v;',
  21205. ' }',
  21206. ' });',
  21207. '} finally {',
  21208. ' rtl._Release($mod.i);',
  21209. '};',
  21210. '']));
  21211. end;
  21212. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21213. begin
  21214. StartProgram(false);
  21215. Add([
  21216. '{$interfaces com}',
  21217. 'type',
  21218. ' IUnknown = interface',
  21219. ' function _AddRef: longint;',
  21220. ' function _Release: longint;',
  21221. ' end;',
  21222. ' TObject = class(IUnknown)',
  21223. ' function _AddRef: longint; virtual; abstract;',
  21224. ' function _Release: longint; virtual; abstract;',
  21225. ' end;',
  21226. 'function GetIt: IUnknown;',
  21227. 'begin',
  21228. 'end;',
  21229. 'procedure DoSome;',
  21230. 'var v: IUnknown;',
  21231. ' i: longint;',
  21232. 'begin',
  21233. ' v:=GetIt;',
  21234. ' v:=GetIt();',
  21235. ' GetIt()._AddRef;',
  21236. ' i:=GetIt()._AddRef;',
  21237. 'end;',
  21238. 'var v: IUnknown;',
  21239. ' i: longint;',
  21240. 'begin',
  21241. ' v:=GetIt;',
  21242. ' v:=GetIt();',
  21243. ' GetIt()._AddRef;',
  21244. ' i:=GetIt()._AddRef;',
  21245. '']);
  21246. ConvertProgram;
  21247. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21248. LinesToStr([ // statements
  21249. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21250. 'rtl.createClass(this, "TObject", null, function () {',
  21251. ' this.$init = function () {',
  21252. ' };',
  21253. ' this.$final = function () {',
  21254. ' };',
  21255. ' rtl.addIntf(this, $mod.IUnknown);',
  21256. '});',
  21257. 'this.GetIt = function () {',
  21258. ' var Result = null;',
  21259. ' return Result;',
  21260. '};',
  21261. 'this.DoSome = function () {',
  21262. ' var v = null;',
  21263. ' var i = 0;',
  21264. ' var $ir = rtl.createIntfRefs();',
  21265. ' try {',
  21266. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21267. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21268. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21269. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21270. ' } finally {',
  21271. ' $ir.free();',
  21272. ' rtl._Release(v);',
  21273. ' };',
  21274. '};',
  21275. 'this.v = null;',
  21276. 'this.i = 0;',
  21277. '']),
  21278. LinesToStr([ // $mod.$main
  21279. 'var $ir = rtl.createIntfRefs();',
  21280. 'try {',
  21281. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21282. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21283. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21284. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21285. '} finally {',
  21286. ' $ir.free();',
  21287. '};',
  21288. '']));
  21289. end;
  21290. procedure TTestModule.TestClassInterface_COM_Property;
  21291. begin
  21292. StartProgram(false);
  21293. Add([
  21294. '{$interfaces com}',
  21295. 'type',
  21296. ' IUnknown = interface',
  21297. ' function _AddRef: longint;',
  21298. ' function _Release: longint;',
  21299. ' end;',
  21300. ' TObject = class(IUnknown)',
  21301. ' FAnt: IUnknown;',
  21302. ' function _AddRef: longint; virtual; abstract;',
  21303. ' function _Release: longint; virtual; abstract;',
  21304. ' function GetBird: IUnknown; virtual; abstract;',
  21305. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21306. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21307. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21308. ' property Ant: IUnknown read FAnt write FAnt;',
  21309. ' property Bird: IUnknown read GetBird write SetBird;',
  21310. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21311. ' end;',
  21312. 'procedure DoIt;',
  21313. 'var',
  21314. ' o: TObject;',
  21315. ' v: IUnknown;',
  21316. 'begin',
  21317. ' v:=o.Ant;',
  21318. ' o.Ant:=v;',
  21319. ' o.Ant:=o.Ant;',
  21320. ' v:=o.Bird;',
  21321. ' o.Bird:=v;',
  21322. ' o.Bird:=o.Bird;',
  21323. ' v:=o.Items[1];',
  21324. ' o.Items[2]:=v;',
  21325. ' o.Items[3]:=o.Items[4];',
  21326. ' v:=o[5];',
  21327. ' o[6]:=v;',
  21328. ' o[7]:=o[8];',
  21329. 'end;',
  21330. 'begin',
  21331. '']);
  21332. ConvertProgram;
  21333. CheckSource('TestClassInterface_COM_Property',
  21334. LinesToStr([ // statements
  21335. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21336. 'rtl.createClass(this, "TObject", null, function () {',
  21337. ' this.$init = function () {',
  21338. ' this.FAnt = null;',
  21339. ' };',
  21340. ' this.$final = function () {',
  21341. ' rtl.setIntfP(this, "FAnt", null);',
  21342. ' };',
  21343. ' rtl.addIntf(this, $mod.IUnknown);',
  21344. '});',
  21345. 'this.DoIt = function () {',
  21346. ' var o = null;',
  21347. ' var v = null;',
  21348. ' var $ir = rtl.createIntfRefs();',
  21349. ' try {',
  21350. ' v = rtl.setIntfL(v, o.FAnt);',
  21351. ' rtl.setIntfP(o, "FAnt", v);',
  21352. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21353. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21354. ' o.SetBird(v);',
  21355. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21356. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21357. ' o.SetItems(2, v);',
  21358. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21359. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21360. ' o.SetItems(6, v);',
  21361. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21362. ' } finally {',
  21363. ' $ir.free();',
  21364. ' rtl._Release(v);',
  21365. ' };',
  21366. '};',
  21367. '']),
  21368. LinesToStr([ // $mod.$main
  21369. '']));
  21370. end;
  21371. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21372. begin
  21373. StartProgram(false);
  21374. Add([
  21375. '{$interfaces com}',
  21376. 'type',
  21377. ' IUnknown = interface',
  21378. ' function _AddRef: longint;',
  21379. ' function _Release: longint;',
  21380. ' function GetBird: IUnknown;',
  21381. ' procedure SetBird(Value: IUnknown);',
  21382. ' function GetItems(Index: longint): IUnknown;',
  21383. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21384. ' property Bird: IUnknown read GetBird write SetBird;',
  21385. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21386. ' end;',
  21387. ' TObject = class(IUnknown)',
  21388. ' function _AddRef: longint; virtual; abstract;',
  21389. ' function _Release: longint; virtual; abstract;',
  21390. ' function GetBird: IUnknown; virtual; abstract;',
  21391. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21392. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21393. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21394. ' end;',
  21395. 'procedure DoIt;',
  21396. 'var',
  21397. ' o: TObject;',
  21398. ' v: IUnknown;',
  21399. 'begin',
  21400. ' v:=v.Items[1];',
  21401. ' v.Items[2]:=v;',
  21402. ' v.Items[3]:=v.Items[4];',
  21403. ' v:=v[5];',
  21404. ' v[6]:=v;',
  21405. ' v[7]:=v[8];',
  21406. ' v[9].Bird.Bird:=v;',
  21407. ' v:=v.Bird[10].Bird',
  21408. 'end;',
  21409. 'begin',
  21410. '']);
  21411. ConvertProgram;
  21412. CheckSource('TestClassInterface_COM_IntfProperty',
  21413. LinesToStr([ // statements
  21414. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21415. ' "_AddRef",',
  21416. ' "_Release",',
  21417. ' "GetBird",',
  21418. ' "SetBird",',
  21419. ' "GetItems",',
  21420. ' "SetItems"',
  21421. '], null);',
  21422. 'rtl.createClass(this, "TObject", null, function () {',
  21423. ' this.$init = function () {',
  21424. ' };',
  21425. ' this.$final = function () {',
  21426. ' };',
  21427. ' rtl.addIntf(this, $mod.IUnknown);',
  21428. '});',
  21429. 'this.DoIt = function () {',
  21430. ' var o = null;',
  21431. ' var v = null;',
  21432. ' var $ir = rtl.createIntfRefs();',
  21433. ' try {',
  21434. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21435. ' v.SetItems(2, v);',
  21436. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21437. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21438. ' v.SetItems(6, v);',
  21439. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21440. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21441. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21442. ' } finally {',
  21443. ' $ir.free();',
  21444. ' rtl._Release(v);',
  21445. ' };',
  21446. '};',
  21447. '']),
  21448. LinesToStr([ // $mod.$main
  21449. '']));
  21450. end;
  21451. procedure TTestModule.TestClassInterface_COM_Delegation;
  21452. begin
  21453. StartProgram(false);
  21454. Add([
  21455. '{$interfaces com}',
  21456. 'type',
  21457. ' IUnknown = interface',
  21458. ' function _AddRef: longint;',
  21459. ' function _Release: longint;',
  21460. ' end;',
  21461. ' IBird = interface(IUnknown)',
  21462. ' procedure Fly(s: string);',
  21463. ' end;',
  21464. ' IEagle = interface(IBird) end;',
  21465. ' IDove = interface(IBird) end;',
  21466. ' ISwallow = interface(IBird) end;',
  21467. ' TObject = class',
  21468. ' end;',
  21469. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21470. ' function _AddRef: longint; virtual; abstract;',
  21471. ' function _Release: longint; virtual; abstract;',
  21472. ' procedure Fly(s: string); virtual; abstract;',
  21473. ' end;',
  21474. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21475. ' function _AddRef: longint; virtual; abstract;',
  21476. ' function _Release: longint; virtual; abstract;',
  21477. ' FBirdIntf: IBird;',
  21478. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21479. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21480. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21481. ' FDoveObj: TBird;',
  21482. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21483. ' function GetSwallowObj: TBird; virtual; abstract;',
  21484. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21485. ' end;',
  21486. 'begin',
  21487. '']);
  21488. ConvertProgram;
  21489. CheckSource('TestClassInterface_COM_Delegation',
  21490. LinesToStr([ // statements
  21491. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21492. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21493. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21494. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21495. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21496. 'rtl.createClass(this, "TObject", null, function () {',
  21497. ' this.$init = function () {',
  21498. ' };',
  21499. ' this.$final = function () {',
  21500. ' };',
  21501. '});',
  21502. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21503. ' rtl.addIntf(this, $mod.IBird);',
  21504. ' rtl.addIntf(this, $mod.IEagle);',
  21505. ' rtl.addIntf(this, $mod.IDove);',
  21506. ' rtl.addIntf(this, $mod.ISwallow);',
  21507. '});',
  21508. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21509. ' this.$init = function () {',
  21510. ' $mod.TObject.$init.call(this);',
  21511. ' this.FBirdIntf = null;',
  21512. ' this.FDoveObj = null;',
  21513. ' };',
  21514. ' this.$final = function () {',
  21515. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21516. ' this.FDoveObj = undefined;',
  21517. ' $mod.TObject.$final.call(this);',
  21518. ' };',
  21519. ' this.$intfmaps = {',
  21520. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  21521. ' return rtl._AddRef(this.FBirdIntf);',
  21522. ' },',
  21523. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  21524. ' return this.GetEagleIntf();',
  21525. ' },',
  21526. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  21527. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  21528. ' },',
  21529. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  21530. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  21531. ' }',
  21532. ' };',
  21533. '});',
  21534. '']),
  21535. LinesToStr([ // $mod.$main
  21536. '']));
  21537. end;
  21538. procedure TTestModule.TestClassInterface_COM_With;
  21539. begin
  21540. StartProgram(false);
  21541. Add([
  21542. '{$interfaces com}',
  21543. 'type',
  21544. ' IUnknown = interface',
  21545. ' function _AddRef: longint;',
  21546. ' function _Release: longint;',
  21547. ' function GetAnt: IUnknown;',
  21548. ' property Ant: IUnknown read GetAnt;',
  21549. ' end;',
  21550. ' TObject = class(IUnknown)',
  21551. ' function _AddRef: longint; virtual; abstract;',
  21552. ' function _Release: longint; virtual; abstract;',
  21553. ' function GetAnt: IUnknown; virtual; abstract;',
  21554. ' property Ant: IUnknown read GetAnt;',
  21555. ' end;',
  21556. 'procedure DoIt;',
  21557. 'var',
  21558. ' i: IUnknown;',
  21559. 'begin',
  21560. ' with i do ',
  21561. ' GetAnt;',
  21562. ' with i.Ant, Ant do ',
  21563. ' GetAnt;',
  21564. 'end;',
  21565. 'begin',
  21566. '']);
  21567. ConvertProgram;
  21568. CheckSource('TestClassInterface_COM_With',
  21569. LinesToStr([ // statements
  21570. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  21571. 'rtl.createClass(this, "TObject", null, function () {',
  21572. ' this.$init = function () {',
  21573. ' };',
  21574. ' this.$final = function () {',
  21575. ' };',
  21576. ' rtl.addIntf(this, $mod.IUnknown);',
  21577. '});',
  21578. 'this.DoIt = function () {',
  21579. ' var i = null;',
  21580. ' var $ir = rtl.createIntfRefs();',
  21581. ' try {',
  21582. ' $ir.ref(1, i.GetAnt());',
  21583. ' var $with = $ir.ref(2, i.GetAnt());',
  21584. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  21585. ' $ir.ref(4, $with1.GetAnt());',
  21586. ' } finally {',
  21587. ' $ir.free();',
  21588. ' };',
  21589. '};',
  21590. '']),
  21591. LinesToStr([ // $mod.$main
  21592. '']));
  21593. end;
  21594. procedure TTestModule.TestClassInterface_COM_ForIn;
  21595. begin
  21596. StartProgram(false);
  21597. Add([
  21598. '{$interfaces com}',
  21599. 'type',
  21600. ' IUnknown = interface end;',
  21601. ' TObject = class',
  21602. ' Id: longint;',
  21603. ' end;',
  21604. ' IEnumerator = interface(IUnknown)',
  21605. ' function GetCurrent: TObject;',
  21606. ' function MoveNext: Boolean;',
  21607. ' property Current: TObject read GetCurrent;',
  21608. ' end;',
  21609. ' IEnumerable = interface(IUnknown)',
  21610. ' function GetEnumerator: IEnumerator;',
  21611. ' end;',
  21612. 'var',
  21613. ' o: TObject;',
  21614. ' i: IEnumerable;',
  21615. 'begin',
  21616. ' for o in i do o.Id:=3;',
  21617. '']);
  21618. ConvertProgram;
  21619. CheckSource('TestClassInterface_COM_ForIn',
  21620. LinesToStr([ // statements
  21621. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21622. 'rtl.createClass(this, "TObject", null, function () {',
  21623. ' this.$init = function () {',
  21624. ' this.Id = 0;',
  21625. ' };',
  21626. ' this.$final = function () {',
  21627. ' };',
  21628. '});',
  21629. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21630. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21631. 'this.o = null;',
  21632. 'this.i = null;',
  21633. '']),
  21634. LinesToStr([ // $mod.$main
  21635. 'var $in = $mod.i.GetEnumerator();',
  21636. 'try {',
  21637. ' while ($in.MoveNext()) {',
  21638. ' $mod.o = $in.GetCurrent();',
  21639. ' $mod.o.Id = 3;',
  21640. ' }',
  21641. '} finally {',
  21642. ' rtl._Release($in)',
  21643. '};',
  21644. '']));
  21645. end;
  21646. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  21647. begin
  21648. StartProgram(false);
  21649. Add([
  21650. '{$interfaces com}',
  21651. 'type',
  21652. ' IUnknown = interface',
  21653. ' function _AddRef: longint;',
  21654. ' function _Release: longint;',
  21655. ' end;',
  21656. ' TObject = class',
  21657. ' end;',
  21658. ' TArrOfIntf = array of IUnknown;',
  21659. 'begin',
  21660. '']);
  21661. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  21662. ConvertProgram;
  21663. end;
  21664. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  21665. begin
  21666. StartProgram(false);
  21667. Add([
  21668. '{$interfaces com}',
  21669. 'type',
  21670. ' IUnknown = interface',
  21671. ' function _AddRef: longint;',
  21672. ' function _Release: longint;',
  21673. ' end;',
  21674. ' TRec = record',
  21675. ' i: IUnknown;',
  21676. ' end;',
  21677. 'begin',
  21678. '']);
  21679. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  21680. ConvertProgram;
  21681. end;
  21682. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  21683. begin
  21684. StartUnit(false);
  21685. Add([
  21686. '{$interfaces com}',
  21687. 'interface',
  21688. 'implementation',
  21689. 'type',
  21690. ' IUnknown = interface',
  21691. ' function _AddRef: longint;',
  21692. ' end;',
  21693. ' TObject = class(IUnknown)',
  21694. ' function _AddRef: longint;',
  21695. ' end;',
  21696. 'function TObject._AddRef: longint; begin end;',
  21697. 'var i: IUnknown;',
  21698. ' o: TObject;',
  21699. 'initialization',
  21700. ' i:=nil;',
  21701. ' i:=i;',
  21702. ' i:=o;',
  21703. ' if (o as IUnknown)=nil then ;',
  21704. '']);
  21705. ConvertUnit;
  21706. CheckSource('TestClassInterface_COM_UnitInitialization',
  21707. LinesToStr([ // statements
  21708. 'var $impl = $mod.$impl;',
  21709. '']),
  21710. LinesToStr([ // this.$init
  21711. 'var $ir = rtl.createIntfRefs();',
  21712. 'try {',
  21713. ' rtl.setIntfP($impl, "i", null);',
  21714. ' rtl.setIntfP($impl, "i", $impl.i);',
  21715. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  21716. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  21717. '} finally {',
  21718. ' $ir.free();',
  21719. '};',
  21720. '']),
  21721. LinesToStr([ // implementation
  21722. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  21723. 'rtl.createClass($impl, "TObject", null, function () {',
  21724. ' this.$init = function () {',
  21725. ' };',
  21726. ' this.$final = function () {',
  21727. ' };',
  21728. ' this._AddRef = function () {',
  21729. ' var Result = 0;',
  21730. ' return Result;',
  21731. ' };',
  21732. ' rtl.addIntf(this, $impl.IUnknown);',
  21733. '});',
  21734. '$impl.i = null;',
  21735. '$impl.o = null;',
  21736. ''])
  21737. );
  21738. end;
  21739. procedure TTestModule.TestClassInterface_GUID;
  21740. begin
  21741. StartProgram(false);
  21742. Add([
  21743. '{$interfaces corba}',
  21744. 'type',
  21745. ' IUnknown = interface',
  21746. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  21747. ' end;',
  21748. ' TObject = class end;',
  21749. ' TGUID = record D1, D2, D3, D4: word; end;',
  21750. ' TAliasGUID = TGUID;',
  21751. ' TGUIDString = type string;',
  21752. ' TAliasGUIDString = TGUIDString;',
  21753. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  21754. 'begin end;',
  21755. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  21756. 'begin end;',
  21757. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  21758. 'begin end;',
  21759. 'var',
  21760. ' i: IUnknown;',
  21761. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  21762. ' s: TAliasGUIDString;',
  21763. 'begin',
  21764. ' DoConstGUIDIt(IUnknown);',
  21765. ' DoDefGUID(IUnknown);',
  21766. ' DoStr(IUnknown);',
  21767. ' DoConstGUIDIt(i);',
  21768. ' DoDefGUID(i);',
  21769. ' DoStr(i);',
  21770. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  21771. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  21772. ' DoStr(g);',
  21773. ' g:=i;',
  21774. ' g:=IUnknown;',
  21775. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  21776. ' s:=i;',
  21777. ' s:=IUnknown;',
  21778. ' s:=g;',
  21779. ' if g=i then ;',
  21780. ' if i=g then ;',
  21781. ' if g=IUnknown then ;',
  21782. ' if IUnknown=g then ;',
  21783. ' if s=i then ;',
  21784. ' if i=s then ;',
  21785. ' if s=IUnknown then ;',
  21786. ' if IUnknown=s then ;',
  21787. ' if s=g then ;',
  21788. ' if g=s then ;',
  21789. '']);
  21790. ConvertProgram;
  21791. CheckSource('TestClassInterface_GUID',
  21792. LinesToStr([ // statements
  21793. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  21794. 'rtl.createClass(this, "TObject", null, function () {',
  21795. ' this.$init = function () {',
  21796. ' };',
  21797. ' this.$final = function () {',
  21798. ' };',
  21799. '});',
  21800. 'rtl.recNewT(this, "TGUID", function () {',
  21801. ' this.D1 = 0;',
  21802. ' this.D2 = 0;',
  21803. ' this.D3 = 0;',
  21804. ' this.D4 = 0;',
  21805. ' this.$eq = function (b) {',
  21806. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  21807. ' };',
  21808. ' this.$assign = function (s) {',
  21809. ' this.D1 = s.D1;',
  21810. ' this.D2 = s.D2;',
  21811. ' this.D3 = s.D3;',
  21812. ' this.D4 = s.D4;',
  21813. ' return this;',
  21814. ' };',
  21815. '});',
  21816. 'this.DoConstGUIDIt = function (g) {',
  21817. '};',
  21818. 'this.DoDefGUID = function (g) {',
  21819. '};',
  21820. 'this.DoStr = function (s) {',
  21821. '};',
  21822. 'this.i = null;',
  21823. 'this.g = this.TGUID.$clone({',
  21824. ' D1: 0xD91C9AF4,',
  21825. ' D2: 0x3C93,',
  21826. ' D3: 0x420F,',
  21827. ' D4: [',
  21828. ' 0xA3,',
  21829. ' 0x03,',
  21830. ' 0xBF,',
  21831. ' 0x5B,',
  21832. ' 0xA8,',
  21833. ' 0x2B,',
  21834. ' 0xFD,',
  21835. ' 0x23',
  21836. ' ]',
  21837. '});',
  21838. 'this.s = "";',
  21839. '']),
  21840. LinesToStr([ // $mod.$main
  21841. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  21842. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  21843. '$mod.DoStr($mod.IUnknown.$guid);',
  21844. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  21845. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  21846. '$mod.DoStr($mod.i.$guid);',
  21847. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  21848. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  21849. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  21850. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  21851. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  21852. '$mod.g.$assign({',
  21853. ' D1: 0xD91C9AF4,',
  21854. ' D2: 0x3C93,',
  21855. ' D3: 0x420F,',
  21856. ' D4: [',
  21857. ' 0xA3,',
  21858. ' 0x03,',
  21859. ' 0xBF,',
  21860. ' 0x5B,',
  21861. ' 0xA8,',
  21862. ' 0x2B,',
  21863. ' 0xFD,',
  21864. ' 0x23',
  21865. ' ]',
  21866. '});',
  21867. '$mod.s = $mod.i.$guid;',
  21868. '$mod.s = $mod.IUnknown.$guid;',
  21869. '$mod.s = rtl.guidrToStr($mod.g);',
  21870. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  21871. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  21872. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  21873. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  21874. 'if ($mod.s === $mod.i.$guid) ;',
  21875. 'if ($mod.i.$guid === $mod.s) ;',
  21876. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  21877. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  21878. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  21879. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  21880. '']));
  21881. end;
  21882. procedure TTestModule.TestClassInterface_GUIDProperty;
  21883. begin
  21884. StartProgram(false);
  21885. Add([
  21886. '{$interfaces corba}',
  21887. 'type',
  21888. ' IUnknown = interface',
  21889. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  21890. ' end;',
  21891. ' TGUID = record D1, D2, D3, D4: word; end;',
  21892. ' TAliasGUID = TGUID;',
  21893. ' TGUIDString = type string;',
  21894. ' TAliasGUIDString = TGUIDString;',
  21895. ' TObject = class',
  21896. ' function GetG: TAliasGUID; virtual; abstract;',
  21897. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  21898. ' function GetS: TAliasGUIDString; virtual; abstract;',
  21899. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  21900. ' property g: TAliasGUID read GetG write SetG;',
  21901. ' property s: TAliasGUIDString read GetS write SetS;',
  21902. ' end;',
  21903. 'var o: TObject;',
  21904. 'begin',
  21905. ' o.g:=IUnknown;',
  21906. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  21907. ' o.s:=IUnknown;',
  21908. ' o.s:=o.g;',
  21909. '']);
  21910. ConvertProgram;
  21911. CheckSource('TestClassInterface_GUIDProperty',
  21912. LinesToStr([ // statements
  21913. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  21914. 'rtl.recNewT(this, "TGUID", function () {',
  21915. ' this.D1 = 0;',
  21916. ' this.D2 = 0;',
  21917. ' this.D3 = 0;',
  21918. ' this.D4 = 0;',
  21919. ' this.$eq = function (b) {',
  21920. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  21921. ' };',
  21922. ' this.$assign = function (s) {',
  21923. ' this.D1 = s.D1;',
  21924. ' this.D2 = s.D2;',
  21925. ' this.D3 = s.D3;',
  21926. ' this.D4 = s.D4;',
  21927. ' return this;',
  21928. ' };',
  21929. '});',
  21930. 'rtl.createClass(this, "TObject", null, function () {',
  21931. ' this.$init = function () {',
  21932. ' };',
  21933. ' this.$final = function () {',
  21934. ' };',
  21935. '});',
  21936. 'this.o = null;',
  21937. '']),
  21938. LinesToStr([ // $mod.$main
  21939. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  21940. '$mod.o.SetG({',
  21941. ' D1: 0xD91C9AF4,',
  21942. ' D2: 0x3C93,',
  21943. ' D3: 0x420F,',
  21944. ' D4: [',
  21945. ' 0xA3,',
  21946. ' 0x03,',
  21947. ' 0xBF,',
  21948. ' 0x5B,',
  21949. ' 0xA8,',
  21950. ' 0x2B,',
  21951. ' 0xFD,',
  21952. ' 0x23',
  21953. ' ]',
  21954. '});',
  21955. '$mod.o.SetS($mod.IUnknown.$guid);',
  21956. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  21957. '']));
  21958. end;
  21959. procedure TTestModule.TestClassHelper_ClassVar;
  21960. begin
  21961. StartProgram(false);
  21962. Add([
  21963. 'type',
  21964. ' TObject = class',
  21965. ' end;',
  21966. ' THelper = class helper for TObject',
  21967. ' const',
  21968. ' One = 1;',
  21969. ' Two: word = 2;',
  21970. ' class var',
  21971. ' Glob: word;',
  21972. ' function Foo(w: word): word;',
  21973. ' class function Bar(w: word): word;',
  21974. ' end;',
  21975. 'function THelper.foo(w: word): word;',
  21976. 'begin',
  21977. ' Result:=w;',
  21978. ' Two:=One+w;',
  21979. ' Glob:=Glob;',
  21980. ' Result:=Self.Glob;',
  21981. ' Self.Glob:=Self.Glob;',
  21982. ' with Self do Glob:=Glob;',
  21983. 'end;',
  21984. 'class function THelper.bar(w: word): word;',
  21985. 'begin',
  21986. ' Result:=w;',
  21987. ' Two:=One;',
  21988. ' Glob:=Glob;',
  21989. ' Self.Glob:=Self.Glob;',
  21990. ' with Self do Glob:=Glob;',
  21991. 'end;',
  21992. 'var o: TObject;',
  21993. 'begin',
  21994. ' tobject.two:=tobject.one;',
  21995. ' tobject.Glob:=tobject.Glob;',
  21996. ' with tobject do begin',
  21997. ' two:=one;',
  21998. ' Glob:=Glob;',
  21999. ' end;',
  22000. ' o.two:=o.one;',
  22001. ' o.Glob:=o.Glob;',
  22002. ' with o do begin',
  22003. ' two:=one;',
  22004. ' Glob:=Glob;',
  22005. ' end;',
  22006. '']);
  22007. ConvertProgram;
  22008. CheckSource('TestClassHelper_ClassVar',
  22009. LinesToStr([ // statements
  22010. 'rtl.createClass(this, "TObject", null, function () {',
  22011. ' this.$init = function () {',
  22012. ' };',
  22013. ' this.$final = function () {',
  22014. ' };',
  22015. '});',
  22016. 'rtl.createHelper(this, "THelper", null, function () {',
  22017. ' this.One = 1;',
  22018. ' this.Two = 2;',
  22019. ' this.Glob = 0;',
  22020. ' this.Foo = function (w) {',
  22021. ' var Result = 0;',
  22022. ' Result = w;',
  22023. ' $mod.THelper.Two = 1 + w;',
  22024. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22025. ' Result = $mod.THelper.Glob;',
  22026. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22027. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22028. ' return Result;',
  22029. ' };',
  22030. ' this.Bar = function (w) {',
  22031. ' var Result = 0;',
  22032. ' Result = w;',
  22033. ' $mod.THelper.Two = 1;',
  22034. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22035. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22036. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22037. ' return Result;',
  22038. ' };',
  22039. '});',
  22040. 'this.o = null;',
  22041. '']),
  22042. LinesToStr([ // $mod.$main
  22043. '$mod.THelper.Two = 1;',
  22044. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22045. 'var $with = $mod.TObject;',
  22046. '$mod.THelper.Two = 1;',
  22047. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22048. '$mod.THelper.Two = 1;',
  22049. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22050. 'var $with1 = $mod.o;',
  22051. '$mod.THelper.Two = 1;',
  22052. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22053. '']));
  22054. end;
  22055. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22056. begin
  22057. StartProgram(false);
  22058. Add([
  22059. 'type',
  22060. ' TObject = class',
  22061. ' FSize: word;',
  22062. ' property Size: word read FSize write FSize;',
  22063. ' end;',
  22064. ' THelper = class helper for TObject',
  22065. ' function Foo(w: word = 1): word;',
  22066. ' end;',
  22067. 'function THelper.foo(w: word): word;',
  22068. 'begin',
  22069. ' Result:=Size;',
  22070. ' Size:=Size+2;',
  22071. ' Self.Size:=Self.Size+3;',
  22072. ' FSize:=FSize+4;',
  22073. ' Self.FSize:=Self.FSize+5;',
  22074. ' with Self do begin',
  22075. ' Size:=Size+6;',
  22076. ' FSize:=FSize+7;',
  22077. ' FSize:=FSize+8;',
  22078. ' end;',
  22079. 'end;',
  22080. 'begin',
  22081. '']);
  22082. ConvertProgram;
  22083. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22084. LinesToStr([ // statements
  22085. 'rtl.createClass(this, "TObject", null, function () {',
  22086. ' this.$init = function () {',
  22087. ' this.FSize = 0;',
  22088. ' };',
  22089. ' this.$final = function () {',
  22090. ' };',
  22091. '});',
  22092. 'rtl.createHelper(this, "THelper", null, function () {',
  22093. ' this.Foo = function (w) {',
  22094. ' var Result = 0;',
  22095. ' Result = this.FSize;',
  22096. ' this.FSize = this.FSize + 2;',
  22097. ' this.FSize = this.FSize + 3;',
  22098. ' this.FSize = this.FSize + 4;',
  22099. ' this.FSize = this.FSize + 5;',
  22100. ' this.FSize = this.FSize + 6;',
  22101. ' this.FSize = this.FSize + 7;',
  22102. ' this.FSize = this.FSize + 8;',
  22103. ' return Result;',
  22104. ' };',
  22105. '});',
  22106. '']),
  22107. LinesToStr([ // $mod.$main
  22108. '']));
  22109. end;
  22110. procedure TTestModule.TestClassHelper_Method_Call;
  22111. begin
  22112. StartProgram(false);
  22113. Add([
  22114. 'type',
  22115. ' TObject = class',
  22116. ' procedure Run(w: word = 10);',
  22117. ' end;',
  22118. ' THelper = class helper for TObject',
  22119. ' function Foo(w: word = 1): word;',
  22120. ' end;',
  22121. 'procedure TObject.Run(w: word);',
  22122. 'var o: TObject;',
  22123. 'begin',
  22124. ' Foo;',
  22125. ' Foo();',
  22126. ' Foo(2);',
  22127. ' Self.Foo;',
  22128. ' Self.Foo();',
  22129. ' Self.Foo(3);',
  22130. ' with Self do begin',
  22131. ' Foo;',
  22132. ' Foo();',
  22133. ' Foo(4);',
  22134. ' end;',
  22135. ' with o do Foo(5);',
  22136. 'end;',
  22137. 'function THelper.foo(w: word): word;',
  22138. 'begin',
  22139. ' Run;',
  22140. ' Run();',
  22141. ' Run(11);',
  22142. ' Foo;',
  22143. ' Foo();',
  22144. ' Foo(12);',
  22145. ' Self.Foo;',
  22146. ' Self.Foo();',
  22147. ' Self.Foo(13);',
  22148. ' with Self do begin',
  22149. ' Foo;',
  22150. ' Foo();',
  22151. ' Foo(14);',
  22152. ' end;',
  22153. 'end;',
  22154. 'var Obj: TObject;',
  22155. 'begin',
  22156. ' obj.Foo;',
  22157. ' obj.Foo();',
  22158. ' obj.Foo(21);',
  22159. ' with obj do begin',
  22160. ' Foo;',
  22161. ' Foo();',
  22162. ' Foo(22);',
  22163. ' end;',
  22164. '']);
  22165. ConvertProgram;
  22166. CheckSource('TestClassHelper_Method_Call',
  22167. LinesToStr([ // statements
  22168. 'rtl.createClass(this, "TObject", null, function () {',
  22169. ' this.$init = function () {',
  22170. ' };',
  22171. ' this.$final = function () {',
  22172. ' };',
  22173. ' this.Run = function (w) {',
  22174. ' var o = null;',
  22175. ' $mod.THelper.Foo.call(this, 1);',
  22176. ' $mod.THelper.Foo.call(this, 1);',
  22177. ' $mod.THelper.Foo.call(this, 2);',
  22178. ' $mod.THelper.Foo.call(this, 1);',
  22179. ' $mod.THelper.Foo.call(this, 1);',
  22180. ' $mod.THelper.Foo.call(this, 3);',
  22181. ' $mod.THelper.Foo.call(this, 1);',
  22182. ' $mod.THelper.Foo.call(this, 1);',
  22183. ' $mod.THelper.Foo.call(this, 4);',
  22184. ' $mod.THelper.Foo.call(o, 5);',
  22185. ' };',
  22186. '});',
  22187. 'rtl.createHelper(this, "THelper", null, function () {',
  22188. ' this.Foo = function (w) {',
  22189. ' var Result = 0;',
  22190. ' this.Run(10);',
  22191. ' this.Run(10);',
  22192. ' this.Run(11);',
  22193. ' $mod.THelper.Foo.call(this, 1);',
  22194. ' $mod.THelper.Foo.call(this, 1);',
  22195. ' $mod.THelper.Foo.call(this, 12);',
  22196. ' $mod.THelper.Foo.call(this, 1);',
  22197. ' $mod.THelper.Foo.call(this, 1);',
  22198. ' $mod.THelper.Foo.call(this, 13);',
  22199. ' $mod.THelper.Foo.call(this, 1);',
  22200. ' $mod.THelper.Foo.call(this, 1);',
  22201. ' $mod.THelper.Foo.call(this, 14);',
  22202. ' return Result;',
  22203. ' };',
  22204. '});',
  22205. 'this.Obj = null;',
  22206. '']),
  22207. LinesToStr([ // $mod.$main
  22208. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22209. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22210. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22211. 'var $with = $mod.Obj;',
  22212. '$mod.THelper.Foo.call($with, 1);',
  22213. '$mod.THelper.Foo.call($with, 1);',
  22214. '$mod.THelper.Foo.call($with, 22);',
  22215. '']));
  22216. end;
  22217. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22218. begin
  22219. StartProgram(false);
  22220. Add([
  22221. 'type',
  22222. ' TObject = class',
  22223. ' procedure Run(w: word = 10);',
  22224. ' end;',
  22225. ' THelper = class helper for TObject',
  22226. ' function Foo(w: word = 1): word;',
  22227. ' end;',
  22228. 'procedure TObject.Run(w: word);',
  22229. ' procedure Sub(Self: TObject);',
  22230. ' begin',
  22231. ' Foo;',
  22232. ' Foo();',
  22233. ' Self.Foo;',
  22234. ' Self.Foo();',
  22235. ' with Self do begin',
  22236. ' Foo;',
  22237. ' Foo();',
  22238. ' end;',
  22239. ' end;',
  22240. 'begin',
  22241. 'end;',
  22242. 'function THelper.foo(w: word): word;',
  22243. ' procedure Sub(Self: TObject);',
  22244. ' begin',
  22245. ' Run;',
  22246. ' Run();',
  22247. ' Foo;',
  22248. ' Foo();',
  22249. ' Self.Foo;',
  22250. ' Self.Foo();',
  22251. ' with Self do begin',
  22252. ' Foo;',
  22253. ' Foo();',
  22254. ' end;',
  22255. ' end;',
  22256. 'begin',
  22257. 'end;',
  22258. 'begin',
  22259. '']);
  22260. ConvertProgram;
  22261. CheckSource('TestClassHelper_Method_Nested_Call',
  22262. LinesToStr([ // statements
  22263. 'rtl.createClass(this, "TObject", null, function () {',
  22264. ' this.$init = function () {',
  22265. ' };',
  22266. ' this.$final = function () {',
  22267. ' };',
  22268. ' this.Run = function (w) {',
  22269. ' var $Self = this;',
  22270. ' function Sub(Self) {',
  22271. ' $mod.THelper.Foo.call($Self, 1);',
  22272. ' $mod.THelper.Foo.call($Self, 1);',
  22273. ' $mod.THelper.Foo.call(Self, 1);',
  22274. ' $mod.THelper.Foo.call(Self, 1);',
  22275. ' $mod.THelper.Foo.call(Self, 1);',
  22276. ' $mod.THelper.Foo.call(Self, 1);',
  22277. ' };',
  22278. ' };',
  22279. '});',
  22280. 'rtl.createHelper(this, "THelper", null, function () {',
  22281. ' this.Foo = function (w) {',
  22282. ' var $Self = this;',
  22283. ' var Result = 0;',
  22284. ' function Sub(Self) {',
  22285. ' $Self.Run(10);',
  22286. ' $Self.Run(10);',
  22287. ' $mod.THelper.Foo.call($Self, 1);',
  22288. ' $mod.THelper.Foo.call($Self, 1);',
  22289. ' $mod.THelper.Foo.call(Self, 1);',
  22290. ' $mod.THelper.Foo.call(Self, 1);',
  22291. ' $mod.THelper.Foo.call(Self, 1);',
  22292. ' $mod.THelper.Foo.call(Self, 1);',
  22293. ' };',
  22294. ' return Result;',
  22295. ' };',
  22296. '});',
  22297. '']),
  22298. LinesToStr([ // $mod.$main
  22299. '']));
  22300. end;
  22301. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22302. begin
  22303. StartProgram(false);
  22304. Add([
  22305. 'type',
  22306. ' TObject = class',
  22307. ' class procedure Run(w: word = 10);',
  22308. ' end;',
  22309. ' THelper = class helper for TObject',
  22310. ' class function Foo(w: word = 1): word;',
  22311. ' end;',
  22312. 'class procedure TObject.Run(w: word);',
  22313. 'begin',
  22314. ' Foo;',
  22315. ' Foo();',
  22316. ' Self.Foo;',
  22317. ' Self.Foo();',
  22318. ' with Self do begin',
  22319. ' Foo;',
  22320. ' Foo();',
  22321. ' end;',
  22322. 'end;',
  22323. 'class function THelper.foo(w: word): word;',
  22324. 'begin',
  22325. ' Run;',
  22326. ' Run();',
  22327. ' Foo;',
  22328. ' Foo();',
  22329. ' Self.Foo;',
  22330. ' Self.Foo();',
  22331. ' with Self do begin',
  22332. ' Foo;',
  22333. ' Foo();',
  22334. ' end;',
  22335. 'end;',
  22336. 'var',
  22337. ' Obj: TObject;',
  22338. 'begin',
  22339. ' obj.Foo;',
  22340. ' obj.Foo();',
  22341. ' with obj do begin',
  22342. ' Foo;',
  22343. ' Foo();',
  22344. ' end;',
  22345. ' tobject.Foo;',
  22346. ' tobject.Foo();',
  22347. ' with tobject do begin',
  22348. ' Foo;',
  22349. ' Foo();',
  22350. ' end;',
  22351. '']);
  22352. ConvertProgram;
  22353. CheckSource('TestClassHelper_ClassMethod_Call',
  22354. LinesToStr([ // statements
  22355. 'rtl.createClass(this, "TObject", null, function () {',
  22356. ' this.$init = function () {',
  22357. ' };',
  22358. ' this.$final = function () {',
  22359. ' };',
  22360. ' this.Run = function (w) {',
  22361. ' $mod.THelper.Foo.call(this, 1);',
  22362. ' $mod.THelper.Foo.call(this, 1);',
  22363. ' $mod.THelper.Foo.call(this, 1);',
  22364. ' $mod.THelper.Foo.call(this, 1);',
  22365. ' $mod.THelper.Foo.call(this, 1);',
  22366. ' $mod.THelper.Foo.call(this, 1);',
  22367. ' };',
  22368. '});',
  22369. 'rtl.createHelper(this, "THelper", null, function () {',
  22370. ' this.Foo = function (w) {',
  22371. ' var Result = 0;',
  22372. ' this.Run(10);',
  22373. ' this.Run(10);',
  22374. ' $mod.THelper.Foo.call(this, 1);',
  22375. ' $mod.THelper.Foo.call(this, 1);',
  22376. ' $mod.THelper.Foo.call(this, 1);',
  22377. ' $mod.THelper.Foo.call(this, 1);',
  22378. ' $mod.THelper.Foo.call(this, 1);',
  22379. ' $mod.THelper.Foo.call(this, 1);',
  22380. ' return Result;',
  22381. ' };',
  22382. '});',
  22383. 'this.Obj = null;',
  22384. '']),
  22385. LinesToStr([ // $mod.$main
  22386. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22387. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22388. 'var $with = $mod.Obj;',
  22389. '$mod.THelper.Foo.call($with.$class, 1);',
  22390. '$mod.THelper.Foo.call($with.$class, 1);',
  22391. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22392. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22393. 'var $with1 = $mod.TObject;',
  22394. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22395. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22396. '']));
  22397. end;
  22398. procedure TTestModule.TestClassHelper_ClassOf;
  22399. begin
  22400. StartProgram(false);
  22401. Add([
  22402. 'type',
  22403. ' TObject = class',
  22404. ' end;',
  22405. ' TClass = class of TObject;',
  22406. ' THelper = class helper for TObject',
  22407. ' class function Foo(w: word = 1): word;',
  22408. ' end;',
  22409. 'class function THelper.foo(w: word): word;',
  22410. 'begin',
  22411. 'end;',
  22412. 'var',
  22413. ' c: TClass;',
  22414. 'begin',
  22415. ' c.Foo;',
  22416. ' c.Foo();',
  22417. ' with c do begin',
  22418. ' Foo;',
  22419. ' Foo();',
  22420. ' end;',
  22421. '']);
  22422. ConvertProgram;
  22423. CheckSource('TestClassHelper_ClassOf',
  22424. LinesToStr([ // statements
  22425. 'rtl.createClass(this, "TObject", null, function () {',
  22426. ' this.$init = function () {',
  22427. ' };',
  22428. ' this.$final = function () {',
  22429. ' };',
  22430. '});',
  22431. 'rtl.createHelper(this, "THelper", null, function () {',
  22432. ' this.Foo = function (w) {',
  22433. ' var Result = 0;',
  22434. ' return Result;',
  22435. ' };',
  22436. '});',
  22437. 'this.c = null;',
  22438. '']),
  22439. LinesToStr([ // $mod.$main
  22440. '$mod.THelper.Foo.call($mod.c, 1);',
  22441. '$mod.THelper.Foo.call($mod.c, 1);',
  22442. 'var $with = $mod.c;',
  22443. '$mod.THelper.Foo.call($with, 1);',
  22444. '$mod.THelper.Foo.call($with, 1);',
  22445. '']));
  22446. end;
  22447. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22448. begin
  22449. StartProgram(false);
  22450. Add([
  22451. '{$mode objfpc}',
  22452. 'type',
  22453. ' TObject = class',
  22454. ' procedure DoIt;',
  22455. ' end;',
  22456. ' THelper = class helper for TObject',
  22457. ' procedure Fly(w: word = 1);',
  22458. ' class procedure Glide(w: word = 1);',
  22459. ' class procedure Run(w: word = 1); static;',
  22460. ' end;',
  22461. ' TFly = procedure(w: word) of object;',
  22462. ' TGlide = TFly;',
  22463. ' TRun = procedure(w: word);',
  22464. 'var',
  22465. ' f: TFly;',
  22466. ' g: TGlide;',
  22467. ' r: TRun;',
  22468. 'procedure TObject.DoIt;',
  22469. 'begin',
  22470. ' f:=@fly;',
  22471. ' g:=@glide;',
  22472. ' r:=@run;',
  22473. ' f:[email protected];',
  22474. ' g:[email protected];',
  22475. ' r:[email protected];',
  22476. ' with self do begin',
  22477. ' f:=@fly;',
  22478. ' g:=@glide;',
  22479. ' r:=@run;',
  22480. ' end;',
  22481. 'end;',
  22482. 'procedure THelper.fly(w: word);',
  22483. 'begin',
  22484. ' f:=@fly;',
  22485. ' g:=@glide;',
  22486. ' r:=@run;',
  22487. 'end;',
  22488. 'class procedure THelper.glide(w: word);',
  22489. 'begin',
  22490. ' g:=@glide;',
  22491. ' r:=@run;',
  22492. 'end;',
  22493. 'class procedure THelper.run(w: word);',
  22494. 'begin',
  22495. ' g:=@glide;',
  22496. ' r:=@run;',
  22497. 'end;',
  22498. 'var',
  22499. ' Obj: TObject;',
  22500. 'begin',
  22501. ' f:[email protected];',
  22502. ' g:[email protected];',
  22503. ' r:[email protected];',
  22504. ' with obj do begin',
  22505. ' f:=@fly;',
  22506. ' g:=@glide;',
  22507. ' r:=@run;',
  22508. ' end;',
  22509. ' g:[email protected];',
  22510. ' r:[email protected];',
  22511. ' with tobject do begin',
  22512. ' g:=@glide;',
  22513. ' r:=@run;',
  22514. ' end;',
  22515. '']);
  22516. ConvertProgram;
  22517. CheckSource('TestClassHelper_MethodRefObjFPC',
  22518. LinesToStr([ // statements
  22519. 'rtl.createClass(this, "TObject", null, function () {',
  22520. ' this.$init = function () {',
  22521. ' };',
  22522. ' this.$final = function () {',
  22523. ' };',
  22524. ' this.DoIt = function () {',
  22525. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22526. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22527. ' $mod.r = $mod.THelper.Run;',
  22528. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22529. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22530. ' $mod.r = $mod.THelper.Run;',
  22531. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22532. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22533. ' $mod.r = $mod.THelper.Run;',
  22534. ' };',
  22535. '});',
  22536. 'rtl.createHelper(this, "THelper", null, function () {',
  22537. ' this.Fly = function (w) {',
  22538. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22539. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22540. ' $mod.r = $mod.THelper.Run;',
  22541. ' };',
  22542. ' this.Glide = function (w) {',
  22543. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  22544. ' $mod.r = $mod.THelper.Run;',
  22545. ' };',
  22546. ' this.Run = function (w) {',
  22547. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  22548. ' $mod.r = $mod.THelper.Run;',
  22549. ' };',
  22550. '});',
  22551. 'this.f = null;',
  22552. 'this.g = null;',
  22553. 'this.r = null;',
  22554. 'this.Obj = null;',
  22555. '']),
  22556. LinesToStr([ // $mod.$main
  22557. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  22558. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  22559. '$mod.r = $mod.THelper.Run;',
  22560. 'var $with = $mod.Obj;',
  22561. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  22562. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  22563. '$mod.r = $mod.THelper.Run;',
  22564. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  22565. '$mod.r = $mod.THelper.Run;',
  22566. 'var $with1 = $mod.TObject;',
  22567. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  22568. '$mod.r = $mod.THelper.Run;',
  22569. '']));
  22570. end;
  22571. procedure TTestModule.TestClassHelper_Constructor;
  22572. begin
  22573. StartProgram(false);
  22574. Add([
  22575. 'type',
  22576. ' TObject = class',
  22577. ' constructor Create;',
  22578. ' end;',
  22579. ' TClass = class of TObject;',
  22580. ' THelper = class helper for TObject',
  22581. ' constructor NewHlp(w: word);',
  22582. ' end;',
  22583. 'var',
  22584. ' obj: TObject;',
  22585. ' c: TClass;',
  22586. 'constructor TObject.Create;',
  22587. 'begin',
  22588. ' NewHlp(2);', // normal call
  22589. ' tobject.NewHlp(3);', // new instance
  22590. ' c.newhlp(4);', // new instance
  22591. 'end;',
  22592. 'constructor THelper.NewHlp(w: word);',
  22593. 'begin',
  22594. ' create;', // normal call
  22595. ' tobject.create;', // new instance
  22596. ' NewHlp(2);', // normal call
  22597. ' tobject.NewHlp(3);', // new instance
  22598. ' c.newhlp(4);', // new instance
  22599. 'end;',
  22600. 'begin',
  22601. ' obj.newhlp(2);', // normal call
  22602. ' with Obj do newhlp(12);', // normal call
  22603. ' tobject.newhlp(3);', // new instance
  22604. ' with tobject do newhlp(13);', // new instance
  22605. ' c.newhlp(4);', // new instance
  22606. ' with c do newhlp(14);', // new instance
  22607. '']);
  22608. ConvertProgram;
  22609. CheckSource('TestClassHelper_Constructor',
  22610. LinesToStr([ // statements
  22611. 'rtl.createClass(this, "TObject", null, function () {',
  22612. ' this.$init = function () {',
  22613. ' };',
  22614. ' this.$final = function () {',
  22615. ' };',
  22616. ' this.Create = function () {',
  22617. ' $mod.THelper.NewHlp.call(this, 2);',
  22618. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  22619. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  22620. ' return this;',
  22621. ' };',
  22622. '});',
  22623. 'rtl.createHelper(this, "THelper", null, function () {',
  22624. ' this.NewHlp = function (w) {',
  22625. ' this.Create();',
  22626. ' $mod.TObject.$create("Create");',
  22627. ' $mod.THelper.NewHlp.call(this, 2);',
  22628. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  22629. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  22630. ' return this;',
  22631. ' };',
  22632. '});',
  22633. 'this.obj = null;',
  22634. 'this.c = null;',
  22635. '']),
  22636. LinesToStr([ // $mod.$main
  22637. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  22638. 'var $with = $mod.obj;',
  22639. '$mod.THelper.NewHlp.call($with, 12);',
  22640. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  22641. 'var $with1 = $mod.TObject;',
  22642. '$with1.$create($mod.THelper.NewHlp, [13]);',
  22643. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  22644. 'var $with2 = $mod.c;',
  22645. '$with2.$create($mod.THelper.NewHlp, [14]);',
  22646. '']));
  22647. end;
  22648. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  22649. begin
  22650. StartProgram(false);
  22651. Add([
  22652. 'type',
  22653. ' TObject = class',
  22654. ' procedure Fly;',
  22655. ' end;',
  22656. ' TObjHelper = class helper for TObject',
  22657. ' procedure Fly;',
  22658. ' end;',
  22659. ' TBird = class',
  22660. ' procedure Fly;',
  22661. ' end;',
  22662. ' TBirdHelper = class helper for TBird',
  22663. ' procedure Fly;',
  22664. ' procedure Walk(w: word);',
  22665. ' end;',
  22666. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  22667. ' procedure Fly;',
  22668. ' procedure Walk(w: word);',
  22669. ' end;',
  22670. 'procedure Tobject.fly;',
  22671. 'begin',
  22672. ' inherited;', // ignore
  22673. 'end;',
  22674. 'procedure Tobjhelper.fly;',
  22675. 'begin',
  22676. ' {@TObject_Fly}inherited;',
  22677. ' inherited {@TObject_Fly}Fly;',
  22678. 'end;',
  22679. 'procedure Tbird.fly;',
  22680. 'begin',
  22681. ' {@TObjHelper_Fly}inherited;',
  22682. ' inherited {@TObjHelper_Fly}Fly;',
  22683. 'end;',
  22684. 'procedure Tbirdhelper.fly;',
  22685. 'begin',
  22686. ' {@TBird_Fly}inherited;',
  22687. ' inherited {@TBird_Fly}Fly;',
  22688. 'end;',
  22689. 'procedure Tbirdhelper.walk(w: word);',
  22690. 'begin',
  22691. 'end;',
  22692. 'procedure teagleHelper.fly;',
  22693. 'begin',
  22694. ' {@TBird_Fly}inherited;',
  22695. ' inherited {@TBird_Fly}Fly;',
  22696. 'end;',
  22697. 'procedure teagleHelper.walk(w: word);',
  22698. 'begin',
  22699. ' {@TBirdHelper_Walk}inherited;',
  22700. ' inherited {@TBirdHelper_Walk}Walk(3);',
  22701. 'end;',
  22702. 'begin',
  22703. '']);
  22704. ConvertProgram;
  22705. CheckSource('TestClassHelper_InheritedObjFPC',
  22706. LinesToStr([ // statements
  22707. 'rtl.createClass(this, "TObject", null, function () {',
  22708. ' this.$init = function () {',
  22709. ' };',
  22710. ' this.$final = function () {',
  22711. ' };',
  22712. ' this.Fly = function () {',
  22713. ' };',
  22714. '});',
  22715. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22716. ' this.Fly = function () {',
  22717. ' $mod.TObject.Fly.call(this);',
  22718. ' $mod.TObject.Fly.call(this);',
  22719. ' };',
  22720. '});',
  22721. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22722. ' this.Fly$1 = function () {',
  22723. ' $mod.TObjHelper.Fly.call(this);',
  22724. ' $mod.TObjHelper.Fly.call(this);',
  22725. ' };',
  22726. '});',
  22727. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22728. ' this.Fly = function () {',
  22729. ' $mod.TBird.Fly$1.call(this);',
  22730. ' $mod.TBird.Fly$1.call(this);',
  22731. ' };',
  22732. ' this.Walk = function (w) {',
  22733. ' };',
  22734. '});',
  22735. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  22736. ' this.Fly$1 = function () {',
  22737. ' $mod.TBird.Fly$1.call(this);',
  22738. ' $mod.TBird.Fly$1.call(this);',
  22739. ' };',
  22740. ' this.Walk$1 = function (w) {',
  22741. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  22742. ' $mod.TBirdHelper.Walk.call(this, 3);',
  22743. ' };',
  22744. '});',
  22745. '']),
  22746. LinesToStr([ // $mod.$main
  22747. '']));
  22748. end;
  22749. procedure TTestModule.TestClassHelper_Property;
  22750. begin
  22751. StartProgram(false);
  22752. Add([
  22753. 'type',
  22754. ' TObject = class',
  22755. ' FSize: word;',
  22756. ' function GetSpeed: word;',
  22757. ' procedure SetSpeed(Value: word);',
  22758. ' end;',
  22759. ' TObjHelper = class helper for TObject',
  22760. ' function GetLeft: word;',
  22761. ' procedure SetLeft(Value: word);',
  22762. ' property Size: word read FSize write FSize;',
  22763. ' property Speed: word read GetSpeed write SetSpeed;',
  22764. ' property Left: word read GetLeft write SetLeft;',
  22765. ' end;',
  22766. ' TBird = class',
  22767. ' property NotRight: word read GetLeft write SetLeft;',
  22768. ' procedure DoIt;',
  22769. ' end;',
  22770. 'var',
  22771. ' b: TBird;',
  22772. 'function Tobject.GetSpeed: word;',
  22773. 'begin',
  22774. ' Size:=Size+11;',
  22775. ' Speed:=Speed+12;',
  22776. ' Result:=Left+13;',
  22777. ' Left:=13;',
  22778. ' Left:=Left+13;',
  22779. ' Self.Size:=Self.Size+21;',
  22780. ' Self.Speed:=Self.Speed+22;',
  22781. ' Self.Left:=Self.Left+23;',
  22782. ' with Self do begin',
  22783. ' Size:=Size+31;',
  22784. ' Speed:=Speed+32;',
  22785. ' Left:=Left+33;',
  22786. ' end;',
  22787. 'end;',
  22788. 'procedure Tobject.SetSpeed(Value: word);',
  22789. 'begin',
  22790. 'end;',
  22791. 'function TObjHelper.GetLeft: word;',
  22792. 'begin',
  22793. ' Size:=Size+11;',
  22794. ' Speed:=Speed+12;',
  22795. ' Left:=Left+13;',
  22796. ' Self.Size:=Self.Size+21;',
  22797. ' Self.Speed:=Self.Speed+22;',
  22798. ' Self.Left:=Self.Left+23;',
  22799. ' with Self do begin',
  22800. ' Size:=Size+31;',
  22801. ' Speed:=Speed+32;',
  22802. ' Left:=Left+33;',
  22803. ' end;',
  22804. 'end;',
  22805. 'procedure TObjHelper.SetLeft(Value: word);',
  22806. 'begin',
  22807. 'end;',
  22808. 'procedure TBird.DoIt;',
  22809. 'begin',
  22810. ' NotRight:=NotRight+11;',
  22811. ' Self.NotRight:=Self.NotRight+21;',
  22812. ' with Self do begin',
  22813. ' NotRight:=NotRight+31;',
  22814. ' end;',
  22815. 'end;',
  22816. 'begin',
  22817. ' b.Size:=b.Size+11;',
  22818. ' b.Speed:=b.Speed+12;',
  22819. ' b.Left:=b.Left+13;',
  22820. ' b.NotRight:=b.NotRight+14;',
  22821. ' with b do begin',
  22822. ' Size:=Size+31;',
  22823. ' Speed:=Speed+32;',
  22824. ' Left:=Left+33;',
  22825. ' NotRight:=NotRight+34;',
  22826. ' end;',
  22827. '']);
  22828. ConvertProgram;
  22829. CheckSource('TestClassHelper_Property',
  22830. LinesToStr([ // statements
  22831. 'rtl.createClass(this, "TObject", null, function () {',
  22832. ' this.$init = function () {',
  22833. ' this.FSize = 0;',
  22834. ' };',
  22835. ' this.$final = function () {',
  22836. ' };',
  22837. ' this.GetSpeed = function () {',
  22838. ' var Result = 0;',
  22839. ' this.FSize = this.FSize + 11;',
  22840. ' this.SetSpeed(this.GetSpeed() + 12);',
  22841. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  22842. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  22843. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22844. ' this.FSize = this.FSize + 21;',
  22845. ' this.SetSpeed(this.GetSpeed() + 22);',
  22846. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22847. ' this.FSize = this.FSize + 31;',
  22848. ' this.SetSpeed(this.GetSpeed() + 32);',
  22849. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22850. ' return Result;',
  22851. ' };',
  22852. ' this.SetSpeed = function (Value) {',
  22853. ' };',
  22854. '});',
  22855. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22856. ' this.GetLeft = function () {',
  22857. ' var Result = 0;',
  22858. ' this.FSize = this.FSize + 11;',
  22859. ' this.SetSpeed(this.GetSpeed() + 12);',
  22860. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22861. ' this.FSize = this.FSize + 21;',
  22862. ' this.SetSpeed(this.GetSpeed() + 22);',
  22863. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22864. ' this.FSize = this.FSize + 31;',
  22865. ' this.SetSpeed(this.GetSpeed() + 32);',
  22866. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22867. ' return Result;',
  22868. ' };',
  22869. ' this.SetLeft = function (Value) {',
  22870. ' };',
  22871. '});',
  22872. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22873. ' this.DoIt = function () {',
  22874. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  22875. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  22876. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  22877. ' };',
  22878. '});',
  22879. 'this.b = null;',
  22880. '']),
  22881. LinesToStr([ // $mod.$main
  22882. '$mod.b.FSize = $mod.b.FSize + 11;',
  22883. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  22884. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  22885. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  22886. 'var $with = $mod.b;',
  22887. '$with.FSize = $with.FSize + 31;',
  22888. '$with.SetSpeed($with.GetSpeed() + 32);',
  22889. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  22890. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  22891. '']));
  22892. end;
  22893. procedure TTestModule.TestClassHelper_Property_Array;
  22894. begin
  22895. StartProgram(false);
  22896. Add([
  22897. 'type',
  22898. ' TObject = class',
  22899. ' function GetSpeed(Index: boolean): word;',
  22900. ' procedure SetSpeed(Index: boolean; Value: word);',
  22901. ' end;',
  22902. ' TObjHelper = class helper for TObject',
  22903. ' function GetSize(Index: boolean): word;',
  22904. ' procedure SetSize(Index: boolean; Value: word);',
  22905. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  22906. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22907. ' end;',
  22908. ' TBird = class',
  22909. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  22910. ' procedure DoIt;',
  22911. ' end;',
  22912. 'var',
  22913. ' b: TBird;',
  22914. 'function Tobject.GetSpeed(Index: boolean): word;',
  22915. 'begin',
  22916. ' Result:=Size[false];',
  22917. ' Size[true]:=Size[false]+11;',
  22918. ' Speed[true]:=Speed[false]+12;',
  22919. ' Self.Size[true]:=Self.Size[false]+21;',
  22920. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22921. ' with Self do begin',
  22922. ' Size[true]:=Size[false]+31;',
  22923. ' Speed[true]:=Speed[false]+32;',
  22924. ' end;',
  22925. 'end;',
  22926. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  22927. 'begin',
  22928. 'end;',
  22929. 'function TObjHelper.GetSize(Index: boolean): word;',
  22930. 'begin',
  22931. ' Size[true]:=Size[false]+11;',
  22932. ' Speed[true]:=Speed[false]+12;',
  22933. ' Self.Size[true]:=Self.Size[false]+21;',
  22934. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22935. ' with Self do begin',
  22936. ' Size[true]:=Size[false]+31;',
  22937. ' Speed[true]:=Speed[false]+32;',
  22938. ' end;',
  22939. 'end;',
  22940. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22941. 'begin',
  22942. 'end;',
  22943. 'procedure TBird.DoIt;',
  22944. 'begin',
  22945. ' Items[true]:=Items[false]+11;',
  22946. ' Self.Items[true]:=Self.Items[false]+21;',
  22947. ' with Self do Items[true]:=Items[false]+31;',
  22948. 'end;',
  22949. 'begin',
  22950. ' b.Size[true]:=b.Size[false]+11;',
  22951. ' b.Speed[true]:=b.Speed[false]+12;',
  22952. ' b.Items[true]:=b.Items[false]+13;',
  22953. ' with b do begin',
  22954. ' Size[true]:=Size[false]+21;',
  22955. ' Speed[true]:=Speed[false]+22;',
  22956. ' Items[true]:=Items[false]+23;',
  22957. ' end;',
  22958. '']);
  22959. ConvertProgram;
  22960. CheckSource('TestClassHelper_Property_Array',
  22961. LinesToStr([ // statements
  22962. 'rtl.createClass(this, "TObject", null, function () {',
  22963. ' this.$init = function () {',
  22964. ' };',
  22965. ' this.$final = function () {',
  22966. ' };',
  22967. ' this.GetSpeed = function (Index) {',
  22968. ' var Result = 0;',
  22969. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  22970. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22971. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22972. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22973. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22974. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22975. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22976. ' return Result;',
  22977. ' };',
  22978. ' this.SetSpeed = function (Index, Value) {',
  22979. ' };',
  22980. '});',
  22981. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22982. ' this.GetSize = function (Index) {',
  22983. ' var Result = 0;',
  22984. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22985. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22986. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22987. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22988. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22989. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22990. ' return Result;',
  22991. ' };',
  22992. ' this.SetSize = function (Index, Value) {',
  22993. ' };',
  22994. '});',
  22995. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22996. ' this.DoIt = function () {',
  22997. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22998. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22999. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23000. ' };',
  23001. '});',
  23002. 'this.b = null;',
  23003. '']),
  23004. LinesToStr([ // $mod.$main
  23005. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23006. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23007. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23008. 'var $with = $mod.b;',
  23009. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23010. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23011. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23012. '']));
  23013. end;
  23014. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23015. begin
  23016. StartProgram(false);
  23017. Add([
  23018. 'type',
  23019. ' TObject = class',
  23020. ' function GetSpeed(Index: boolean): word;',
  23021. ' procedure SetSpeed(Index: boolean; Value: word);',
  23022. ' end;',
  23023. ' TObjHelper = class helper for TObject',
  23024. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23025. ' end;',
  23026. ' TBird = class',
  23027. ' end;',
  23028. ' TBirdHelper = class helper for TBird',
  23029. ' function GetSize(Index: word): boolean;',
  23030. ' procedure SetSize(Index: word; Value: boolean);',
  23031. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23032. ' end;',
  23033. 'function Tobject.GetSpeed(Index: boolean): word;',
  23034. 'begin',
  23035. ' Self[true]:=Self[false]+1;',
  23036. 'end;',
  23037. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23038. 'begin',
  23039. 'end;',
  23040. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23041. 'begin',
  23042. ' Self[1]:=not Self[2];',
  23043. 'end;',
  23044. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23045. 'begin',
  23046. 'end;',
  23047. 'var',
  23048. ' o: TObject;',
  23049. ' b: TBird;',
  23050. 'begin',
  23051. ' o[true]:=o[false]+1;',
  23052. ' b[3]:=not b[4];',
  23053. '']);
  23054. ConvertProgram;
  23055. CheckSource('TestClassHelper_Property_Array_Default',
  23056. LinesToStr([ // statements
  23057. 'rtl.createClass(this, "TObject", null, function () {',
  23058. ' this.$init = function () {',
  23059. ' };',
  23060. ' this.$final = function () {',
  23061. ' };',
  23062. ' this.GetSpeed = function (Index) {',
  23063. ' var Result = 0;',
  23064. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23065. ' return Result;',
  23066. ' };',
  23067. ' this.SetSpeed = function (Index, Value) {',
  23068. ' };',
  23069. '});',
  23070. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23071. '});',
  23072. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23073. '});',
  23074. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23075. ' this.GetSize = function (Index) {',
  23076. ' var Result = false;',
  23077. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23078. ' return Result;',
  23079. ' };',
  23080. ' this.SetSize = function (Index, Value) {',
  23081. ' };',
  23082. '});',
  23083. 'this.o = null;',
  23084. 'this.b = null;',
  23085. '']),
  23086. LinesToStr([ // $mod.$main
  23087. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23088. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23089. '']));
  23090. end;
  23091. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23092. begin
  23093. StartProgram(false);
  23094. Add([
  23095. 'type',
  23096. ' TObject = class',
  23097. ' end;',
  23098. ' TObjHelper = class helper for TObject',
  23099. ' function GetItems(Index: word): TObject;',
  23100. ' procedure SetItems(Index: word; Value: TObject);',
  23101. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23102. ' end;',
  23103. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23104. 'begin',
  23105. ' Self[1][2]:=Self[3][4];',
  23106. 'end;',
  23107. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23108. 'begin',
  23109. 'end;',
  23110. 'var',
  23111. ' o: TObject;',
  23112. 'begin',
  23113. ' o[1][2]:=o[3][4];',
  23114. '']);
  23115. ConvertProgram;
  23116. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23117. LinesToStr([ // statements
  23118. 'rtl.createClass(this, "TObject", null, function () {',
  23119. ' this.$init = function () {',
  23120. ' };',
  23121. ' this.$final = function () {',
  23122. ' };',
  23123. '});',
  23124. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23125. ' this.GetItems = function (Index) {',
  23126. ' var Result = null;',
  23127. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23128. ' return Result;',
  23129. ' };',
  23130. ' this.SetItems = function (Index, Value) {',
  23131. ' };',
  23132. '});',
  23133. 'this.o = null;',
  23134. '']),
  23135. LinesToStr([ // $mod.$main
  23136. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23137. '']));
  23138. end;
  23139. procedure TTestModule.TestClassHelper_ClassProperty;
  23140. begin
  23141. StartProgram(false);
  23142. Add([
  23143. 'type',
  23144. ' TObject = class',
  23145. ' class var FSize: word;',
  23146. ' class function GetSpeed: word;',
  23147. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23148. ' end;',
  23149. ' TObjHelper = class helper for TObject',
  23150. ' class function GetLeft: word;',
  23151. ' class procedure SetLeft(Value: word);',
  23152. ' class property Size: word read FSize write FSize;',
  23153. ' class property Speed: word read GetSpeed write SetSpeed;',
  23154. ' class property Left: word read GetLeft write SetLeft;',
  23155. ' end;',
  23156. ' TBird = class',
  23157. ' class property NotRight: word read GetLeft write SetLeft;',
  23158. ' class procedure DoIt;',
  23159. ' end;',
  23160. ' TBirdClass = class of TBird;',
  23161. 'class function Tobject.GetSpeed: word;',
  23162. 'begin',
  23163. ' Size:=Size+11;',
  23164. ' Speed:=Speed+12;',
  23165. ' Left:=Left+13;',
  23166. ' Self.Size:=Self.Size+21;',
  23167. ' Self.Speed:=Self.Speed+22;',
  23168. ' Self.Left:=Self.Left+23;',
  23169. ' with Self do begin',
  23170. ' Size:=Size+31;',
  23171. ' Speed:=Speed+32;',
  23172. ' Left:=Left+33;',
  23173. ' end;',
  23174. 'end;',
  23175. 'class function TObjHelper.GetLeft: word;',
  23176. 'begin',
  23177. ' Size:=Size+11;',
  23178. ' Speed:=Speed+12;',
  23179. ' Left:=Left+13;',
  23180. ' Self.Size:=Self.Size+21;',
  23181. ' Self.Speed:=Self.Speed+22;',
  23182. ' Self.Left:=Self.Left+23;',
  23183. ' with Self do begin',
  23184. ' Size:=Size+31;',
  23185. ' Speed:=Speed+32;',
  23186. ' Left:=Left+33;',
  23187. ' end;',
  23188. 'end;',
  23189. 'class procedure TObjHelper.SetLeft(Value: word);',
  23190. 'begin',
  23191. 'end;',
  23192. 'class procedure TBird.DoIt;',
  23193. 'begin',
  23194. ' NotRight:=NotRight+11;',
  23195. ' Self.NotRight:=Self.NotRight+21;',
  23196. ' with Self do NotRight:=NotRight+31;',
  23197. 'end;',
  23198. 'var',
  23199. ' b: TBird;',
  23200. ' c: TBirdClass;',
  23201. 'begin',
  23202. ' b.Size:=b.Size+11;',
  23203. ' b.Speed:=b.Speed+12;',
  23204. ' b.Left:=b.Left+13;',
  23205. ' b.NotRight:=b.NotRight+14;',
  23206. ' with b do begin',
  23207. ' Size:=Size+31;',
  23208. ' Speed:=Speed+32;',
  23209. ' Left:=Left+33;',
  23210. ' NotRight:=NotRight+34;',
  23211. ' end;',
  23212. ' c.Size:=c.Size+11;',
  23213. ' c.Speed:=c.Speed+12;',
  23214. ' c.Left:=c.Left+13;',
  23215. ' c.NotRight:=c.NotRight+14;',
  23216. ' with c do begin',
  23217. ' Size:=Size+31;',
  23218. ' Speed:=Speed+32;',
  23219. ' Left:=Left+33;',
  23220. ' NotRight:=NotRight+34;',
  23221. ' end;',
  23222. ' tbird.Size:=tbird.Size+11;',
  23223. ' tbird.Speed:=tbird.Speed+12;',
  23224. ' tbird.Left:=tbird.Left+13;',
  23225. ' tbird.NotRight:=tbird.NotRight+14;',
  23226. ' with tbird do begin',
  23227. ' Size:=Size+31;',
  23228. ' Speed:=Speed+32;',
  23229. ' Left:=Left+33;',
  23230. ' NotRight:=NotRight+34;',
  23231. ' end;',
  23232. '']);
  23233. ConvertProgram;
  23234. CheckSource('TestClassHelper_ClassProperty',
  23235. LinesToStr([ // statements
  23236. 'rtl.createClass(this, "TObject", null, function () {',
  23237. ' this.FSize = 0;',
  23238. ' this.$init = function () {',
  23239. ' };',
  23240. ' this.$final = function () {',
  23241. ' };',
  23242. ' this.GetSpeed = function () {',
  23243. ' var Result = 0;',
  23244. ' $mod.TObject.FSize = this.FSize + 11;',
  23245. ' this.SetSpeed(this.GetSpeed() + 12);',
  23246. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23247. ' $mod.TObject.FSize = this.FSize + 21;',
  23248. ' this.SetSpeed(this.GetSpeed() + 22);',
  23249. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23250. ' $mod.TObject.FSize = this.FSize + 31;',
  23251. ' this.SetSpeed(this.GetSpeed() + 32);',
  23252. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23253. ' return Result;',
  23254. ' };',
  23255. '});',
  23256. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23257. ' this.GetLeft = function () {',
  23258. ' var Result = 0;',
  23259. ' $mod.TObject.FSize = this.FSize + 11;',
  23260. ' this.SetSpeed(this.GetSpeed() + 12);',
  23261. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23262. ' $mod.TObject.FSize = this.FSize + 21;',
  23263. ' this.SetSpeed(this.GetSpeed() + 22);',
  23264. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23265. ' $mod.TObject.FSize = this.FSize + 31;',
  23266. ' this.SetSpeed(this.GetSpeed() + 32);',
  23267. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23268. ' return Result;',
  23269. ' };',
  23270. ' this.SetLeft = function (Value) {',
  23271. ' };',
  23272. '});',
  23273. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23274. ' this.DoIt = function () {',
  23275. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23276. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23277. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23278. ' };',
  23279. '});',
  23280. 'this.b = null;',
  23281. 'this.c = null;',
  23282. '']),
  23283. LinesToStr([ // $mod.$main
  23284. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23285. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23286. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23287. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23288. 'var $with = $mod.b;',
  23289. '$mod.TObject.FSize = $with.FSize + 31;',
  23290. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23291. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23292. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23293. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23294. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23295. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23296. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23297. 'var $with1 = $mod.c;',
  23298. '$mod.TObject.FSize = $with1.FSize + 31;',
  23299. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23300. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23301. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23302. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23303. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23304. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23305. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23306. 'var $with2 = $mod.TBird;',
  23307. '$mod.TObject.FSize = $with2.FSize + 31;',
  23308. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23309. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23310. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23311. '']));
  23312. end;
  23313. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23314. begin
  23315. StartProgram(false);
  23316. Add([
  23317. 'type',
  23318. ' TObject = class',
  23319. ' class function GetSpeed: word; static;',
  23320. ' class procedure SetSpeed(Value: word); static;',
  23321. ' end;',
  23322. ' TObjHelper = class helper for TObject',
  23323. ' class function GetLeft: word; static;',
  23324. ' class procedure SetLeft(Value: word); static;',
  23325. ' class property Speed: word read GetSpeed write SetSpeed;',
  23326. ' class property Left: word read GetLeft write SetLeft;',
  23327. ' end;',
  23328. ' TBird = class',
  23329. ' class property NotRight: word read GetLeft write SetLeft;',
  23330. ' class procedure DoIt; static;',
  23331. ' class procedure DoSome;',
  23332. ' end;',
  23333. ' TBirdClass = class of TBird;',
  23334. 'class function Tobject.GetSpeed: word;',
  23335. 'begin',
  23336. ' Speed:=Speed+12;',
  23337. ' Left:=Left+13;',
  23338. 'end;',
  23339. 'class procedure TObject.SetSpeed(Value: word);',
  23340. 'begin',
  23341. 'end;',
  23342. 'class function TObjHelper.GetLeft: word;',
  23343. 'begin',
  23344. ' Speed:=Speed+12;',
  23345. ' Left:=Left+13;',
  23346. 'end;',
  23347. 'class procedure TObjHelper.SetLeft(Value: word);',
  23348. 'begin',
  23349. 'end;',
  23350. 'class procedure TBird.DoIt;',
  23351. 'begin',
  23352. ' NotRight:=NotRight+11;',
  23353. 'end;',
  23354. 'class procedure TBird.DoSome;',
  23355. 'begin',
  23356. ' Speed:=Speed+12;',
  23357. ' Left:=Left+13;',
  23358. ' Self.Speed:=Self.Speed+22;',
  23359. ' Self.Left:=Self.Left+23;',
  23360. ' with Self do begin',
  23361. ' Speed:=Speed+32;',
  23362. ' Left:=Left+33;',
  23363. ' end;',
  23364. ' NotRight:=NotRight+11;',
  23365. ' Self.NotRight:=Self.NotRight+21;',
  23366. ' with Self do NotRight:=NotRight+31;',
  23367. 'end;',
  23368. 'var',
  23369. ' b: TBird;',
  23370. ' c: TBirdClass;',
  23371. 'begin',
  23372. ' b.Speed:=b.Speed+12;',
  23373. ' b.Left:=b.Left+13;',
  23374. ' b.NotRight:=b.NotRight+14;',
  23375. ' with b do begin',
  23376. ' Speed:=Speed+32;',
  23377. ' Left:=Left+33;',
  23378. ' NotRight:=NotRight+34;',
  23379. ' end;',
  23380. ' c.Speed:=c.Speed+12;',
  23381. ' c.Left:=c.Left+13;',
  23382. ' c.NotRight:=c.NotRight+14;',
  23383. ' with c do begin',
  23384. ' Speed:=Speed+32;',
  23385. ' Left:=Left+33;',
  23386. ' NotRight:=NotRight+34;',
  23387. ' end;',
  23388. ' tbird.Speed:=tbird.Speed+12;',
  23389. ' tbird.Left:=tbird.Left+13;',
  23390. ' tbird.NotRight:=tbird.NotRight+14;',
  23391. ' with tbird do begin',
  23392. ' Speed:=Speed+32;',
  23393. ' Left:=Left+33;',
  23394. ' NotRight:=NotRight+34;',
  23395. ' end;',
  23396. '']);
  23397. ConvertProgram;
  23398. CheckSource('TestClassHelper_ClassPropertyStatic',
  23399. LinesToStr([ // statements
  23400. 'rtl.createClass(this, "TObject", null, function () {',
  23401. ' this.$init = function () {',
  23402. ' };',
  23403. ' this.$final = function () {',
  23404. ' };',
  23405. ' this.GetSpeed = function () {',
  23406. ' var Result = 0;',
  23407. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23408. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23409. ' return Result;',
  23410. ' };',
  23411. ' this.SetSpeed = function (Value) {',
  23412. ' };',
  23413. '});',
  23414. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23415. ' this.GetLeft = function () {',
  23416. ' var Result = 0;',
  23417. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23418. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23419. ' return Result;',
  23420. ' };',
  23421. ' this.SetLeft = function (Value) {',
  23422. ' };',
  23423. '});',
  23424. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23425. ' this.DoIt = function () {',
  23426. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23427. ' };',
  23428. ' this.DoSome = function () {',
  23429. ' this.SetSpeed(this.GetSpeed() + 12);',
  23430. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23431. ' this.SetSpeed(this.GetSpeed() + 22);',
  23432. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23433. ' this.SetSpeed(this.GetSpeed() + 32);',
  23434. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23435. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23436. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23437. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23438. ' };',
  23439. '});',
  23440. 'this.b = null;',
  23441. 'this.c = null;',
  23442. '']),
  23443. LinesToStr([ // $mod.$main
  23444. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23445. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23446. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23447. 'var $with = $mod.b;',
  23448. '$with.SetSpeed($with.GetSpeed() + 32);',
  23449. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23450. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23451. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23452. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23453. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23454. 'var $with1 = $mod.c;',
  23455. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23456. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23457. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23458. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23459. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23460. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23461. 'var $with2 = $mod.TBird;',
  23462. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23463. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23464. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23465. '']));
  23466. end;
  23467. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  23468. begin
  23469. StartProgram(false);
  23470. Add([
  23471. 'type',
  23472. ' TObject = class',
  23473. ' class function GetSpeed(Index: boolean): word;',
  23474. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  23475. ' end;',
  23476. ' TObjHelper = class helper for TObject',
  23477. ' class function GetSize(Index: boolean): word;',
  23478. ' class procedure SetSize(Index: boolean; Value: word);',
  23479. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  23480. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23481. ' end;',
  23482. ' TBird = class',
  23483. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  23484. ' class procedure DoIt;',
  23485. ' end;',
  23486. ' TBirdClass = class of TBird;',
  23487. 'class function Tobject.GetSpeed(Index: boolean): word;',
  23488. 'begin',
  23489. ' Size[true]:=Size[false]+11;',
  23490. ' Speed[true]:=Speed[false]+12;',
  23491. ' Self.Size[true]:=Self.Size[false]+21;',
  23492. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23493. ' with Self do begin',
  23494. ' Size[true]:=Size[false]+31;',
  23495. ' Speed[true]:=Speed[false]+32;',
  23496. ' end;',
  23497. 'end;',
  23498. 'class function TObjHelper.GetSize(Index: boolean): word;',
  23499. 'begin',
  23500. ' Size[true]:=Size[false]+11;',
  23501. ' Speed[true]:=Speed[false]+12;',
  23502. ' Self.Size[true]:=Self.Size[false]+21;',
  23503. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23504. ' with Self do begin',
  23505. ' Size[true]:=Size[false]+31;',
  23506. ' Speed[true]:=Speed[false]+32;',
  23507. ' end;',
  23508. 'end;',
  23509. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23510. 'begin',
  23511. 'end;',
  23512. 'class procedure TBird.DoIt;',
  23513. 'begin',
  23514. ' Items[true]:=Items[false]+11;',
  23515. ' Self.Items[true]:=Self.Items[false]+21;',
  23516. ' with Self do Items[true]:=Items[false]+31;',
  23517. 'end;',
  23518. 'var',
  23519. ' b: TBird;',
  23520. ' c: TBirdClass;',
  23521. 'begin',
  23522. ' b.Size[true]:=b.Size[false]+11;',
  23523. ' b.Speed[true]:=b.Speed[false]+12;',
  23524. ' b.Items[true]:=b.Items[false]+13;',
  23525. ' with b do begin',
  23526. ' Size[true]:=Size[false]+21;',
  23527. ' Speed[true]:=Speed[false]+22;',
  23528. ' Items[true]:=Items[false]+23;',
  23529. ' end;',
  23530. ' c.Size[true]:=c.Size[false]+11;',
  23531. ' c.Speed[true]:=c.Speed[false]+12;',
  23532. ' c.Items[true]:=c.Items[false]+13;',
  23533. ' with c do begin',
  23534. ' Size[true]:=Size[false]+21;',
  23535. ' Speed[true]:=Speed[false]+22;',
  23536. ' Items[true]:=Items[false]+23;',
  23537. ' end;',
  23538. ' TBird.Size[true]:=TBird.Size[false]+11;',
  23539. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  23540. ' TBird.Items[true]:=TBird.Items[false]+13;',
  23541. ' with TBird do begin',
  23542. ' Size[true]:=Size[false]+21;',
  23543. ' Speed[true]:=Speed[false]+22;',
  23544. ' Items[true]:=Items[false]+23;',
  23545. ' end;',
  23546. '']);
  23547. ConvertProgram;
  23548. CheckSource('TestClassHelper_ClassProperty_Array',
  23549. LinesToStr([ // statements
  23550. 'rtl.createClass(this, "TObject", null, function () {',
  23551. ' this.$init = function () {',
  23552. ' };',
  23553. ' this.$final = function () {',
  23554. ' };',
  23555. ' this.GetSpeed = function (Index) {',
  23556. ' var Result = 0;',
  23557. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23558. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23559. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23560. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23561. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23562. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23563. ' return Result;',
  23564. ' };',
  23565. '});',
  23566. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23567. ' this.GetSize = function (Index) {',
  23568. ' var Result = 0;',
  23569. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23570. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23571. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23572. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23573. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23574. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23575. ' return Result;',
  23576. ' };',
  23577. ' this.SetSize = function (Index, Value) {',
  23578. ' };',
  23579. '});',
  23580. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23581. ' this.DoIt = function () {',
  23582. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23583. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23584. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23585. ' };',
  23586. '});',
  23587. 'this.b = null;',
  23588. 'this.c = null;',
  23589. '']),
  23590. LinesToStr([ // $mod.$main
  23591. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  23592. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  23593. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  23594. 'var $with = $mod.b;',
  23595. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  23596. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  23597. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  23598. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  23599. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  23600. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  23601. 'var $with1 = $mod.c;',
  23602. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  23603. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  23604. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  23605. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  23606. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  23607. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  23608. 'var $with2 = $mod.TBird;',
  23609. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  23610. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  23611. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  23612. '']));
  23613. end;
  23614. procedure TTestModule.TestClassHelper_ForIn;
  23615. begin
  23616. StartProgram(false);
  23617. Add([
  23618. 'type',
  23619. ' TObject = class end;',
  23620. ' TItem = TObject;',
  23621. ' TEnumerator = class',
  23622. ' FCurrent: TItem;',
  23623. ' property Current: TItem read FCurrent;',
  23624. ' function MoveNext: boolean;',
  23625. ' end;',
  23626. ' TBird = class',
  23627. ' end;',
  23628. ' TBirdHelper = class helper for TBird',
  23629. ' function GetEnumerator: TEnumerator;',
  23630. ' end;',
  23631. 'function TEnumerator.MoveNext: boolean;',
  23632. 'begin',
  23633. 'end;',
  23634. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  23635. 'begin',
  23636. 'end;',
  23637. 'var',
  23638. ' b: TBird;',
  23639. ' i, i2: TItem;',
  23640. 'begin',
  23641. ' for i in b do i2:=i;']);
  23642. ConvertProgram;
  23643. CheckSource('TestClassHelper_ForIn',
  23644. LinesToStr([ // statements
  23645. 'rtl.createClass(this, "TObject", null, function () {',
  23646. ' this.$init = function () {',
  23647. ' };',
  23648. ' this.$final = function () {',
  23649. ' };',
  23650. '});',
  23651. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  23652. ' this.$init = function () {',
  23653. ' $mod.TObject.$init.call(this);',
  23654. ' this.FCurrent = null;',
  23655. ' };',
  23656. ' this.$final = function () {',
  23657. ' this.FCurrent = undefined;',
  23658. ' $mod.TObject.$final.call(this);',
  23659. ' };',
  23660. ' this.MoveNext = function () {',
  23661. ' var Result = false;',
  23662. ' return Result;',
  23663. ' };',
  23664. '});',
  23665. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23666. '});',
  23667. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23668. ' this.GetEnumerator = function () {',
  23669. ' var Result = null;',
  23670. ' return Result;',
  23671. ' };',
  23672. '});',
  23673. 'this.b = null;',
  23674. 'this.i = null;',
  23675. 'this.i2 = null;'
  23676. ]),
  23677. LinesToStr([ // $mod.$main
  23678. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  23679. 'try {',
  23680. ' while ($in.MoveNext()){',
  23681. ' $mod.i = $in.FCurrent;',
  23682. ' $mod.i2 = $mod.i;',
  23683. ' }',
  23684. '} finally {',
  23685. ' $in = rtl.freeLoc($in)',
  23686. '};',
  23687. '']));
  23688. end;
  23689. procedure TTestModule.TestClassHelper_PassProperty;
  23690. begin
  23691. StartProgram(false);
  23692. Add([
  23693. 'type',
  23694. ' TObject = class',
  23695. ' FField: TObject;',
  23696. ' property Field: TObject read FField write FField;',
  23697. ' end;',
  23698. ' THelper = class helper for TObject',
  23699. ' procedure Fly;',
  23700. ' class procedure Run;',
  23701. ' class procedure Jump; static;',
  23702. ' end;',
  23703. 'procedure THelper.Fly;',
  23704. 'begin',
  23705. ' Field.Fly;',
  23706. ' Field.Run;',
  23707. ' Field.Jump;',
  23708. ' with Field do begin',
  23709. ' Fly;',
  23710. ' Run;',
  23711. ' Jump;',
  23712. ' end;',
  23713. 'end;',
  23714. 'class procedure THelper.Run;',
  23715. 'begin',
  23716. 'end;',
  23717. 'class procedure THelper.Jump;',
  23718. 'begin',
  23719. 'end;',
  23720. 'var',
  23721. ' b: TObject;',
  23722. 'begin',
  23723. ' b.Field.Fly;',
  23724. ' b.Field.Run;',
  23725. ' b.Field.Jump;',
  23726. ' with b do begin',
  23727. ' Field.Run;',
  23728. ' Field.Fly;',
  23729. ' Field.Jump;',
  23730. ' end;',
  23731. ' with b.Field do begin',
  23732. ' Run;',
  23733. ' Fly;',
  23734. ' Jump;',
  23735. ' end;',
  23736. '']);
  23737. ConvertProgram;
  23738. CheckSource('TestClassHelper_PassProperty',
  23739. LinesToStr([ // statements
  23740. 'rtl.createClass(this, "TObject", null, function () {',
  23741. ' this.$init = function () {',
  23742. ' this.FField = null;',
  23743. ' };',
  23744. ' this.$final = function () {',
  23745. ' this.FField = undefined;',
  23746. ' };',
  23747. '});',
  23748. 'rtl.createHelper(this, "THelper", null, function () {',
  23749. ' this.Fly = function () {',
  23750. ' $mod.THelper.Fly.call(this.FField);',
  23751. ' $mod.THelper.Run.call(this.FField.$class);',
  23752. ' $mod.THelper.Jump();',
  23753. ' var $with = this.FField;',
  23754. ' $mod.THelper.Fly.call($with);',
  23755. ' $mod.THelper.Run.call($with.$class);',
  23756. ' $mod.THelper.Jump();',
  23757. ' };',
  23758. ' this.Run = function () {',
  23759. ' };',
  23760. ' this.Jump = function () {',
  23761. ' };',
  23762. '});',
  23763. 'this.b = null;',
  23764. '']),
  23765. LinesToStr([ // $mod.$main
  23766. '$mod.THelper.Fly.call($mod.b.FField);',
  23767. '$mod.THelper.Run.call($mod.b.FField.$class);',
  23768. '$mod.THelper.Jump();',
  23769. 'var $with = $mod.b;',
  23770. '$mod.THelper.Run.call($with.FField.$class);',
  23771. '$mod.THelper.Fly.call($with.FField);',
  23772. '$mod.THelper.Jump();',
  23773. 'var $with1 = $mod.b.FField;',
  23774. '$mod.THelper.Run.call($with1.$class);',
  23775. '$mod.THelper.Fly.call($with1);',
  23776. '$mod.THelper.Jump();',
  23777. '']));
  23778. end;
  23779. procedure TTestModule.TestExtClassHelper_ClassVar;
  23780. begin
  23781. StartProgram(false);
  23782. Add([
  23783. '{$modeswitch externalclass}',
  23784. 'type',
  23785. ' TExtA = class external name ''ExtObj''',
  23786. ' end;',
  23787. ' THelper = class helper for TExtA',
  23788. ' const',
  23789. ' One = 1;',
  23790. ' Two: word = 2;',
  23791. ' class var',
  23792. ' Glob: word;',
  23793. ' function Foo(w: word): word;',
  23794. ' class function Bar(w: word): word; static;',
  23795. ' end;',
  23796. 'function THelper.foo(w: word): word;',
  23797. 'begin',
  23798. ' Result:=w;',
  23799. ' Two:=One+w;',
  23800. ' Glob:=Glob;',
  23801. ' Result:=Self.Glob;',
  23802. ' Self.Glob:=Self.Glob;',
  23803. ' with Self do Glob:=Glob;',
  23804. 'end;',
  23805. 'class function THelper.bar(w: word): word;',
  23806. 'begin',
  23807. ' Result:=w;',
  23808. ' Two:=One;',
  23809. ' Glob:=Glob;',
  23810. 'end;',
  23811. 'var o: TExtA;',
  23812. 'begin',
  23813. ' texta.two:=texta.one;',
  23814. ' texta.Glob:=texta.Glob;',
  23815. ' with texta do begin',
  23816. ' two:=one;',
  23817. ' Glob:=Glob;',
  23818. ' end;',
  23819. ' o.two:=o.one;',
  23820. ' o.Glob:=o.Glob;',
  23821. ' with o do begin',
  23822. ' two:=one;',
  23823. ' Glob:=Glob;',
  23824. ' end;',
  23825. '']);
  23826. ConvertProgram;
  23827. CheckSource('TestExtClassHelper_ClassVar',
  23828. LinesToStr([ // statements
  23829. 'rtl.createHelper(this, "THelper", null, function () {',
  23830. ' this.One = 1;',
  23831. ' this.Two = 2;',
  23832. ' this.Glob = 0;',
  23833. ' this.Foo = function (w) {',
  23834. ' var Result = 0;',
  23835. ' Result = w;',
  23836. ' $mod.THelper.Two = 1 + w;',
  23837. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23838. ' Result = $mod.THelper.Glob;',
  23839. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23840. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23841. ' return Result;',
  23842. ' };',
  23843. ' this.Bar = function (w) {',
  23844. ' var Result = 0;',
  23845. ' Result = w;',
  23846. ' $mod.THelper.Two = 1;',
  23847. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23848. ' return Result;',
  23849. ' };',
  23850. '});',
  23851. 'this.o = null;',
  23852. '']),
  23853. LinesToStr([ // $mod.$main
  23854. '$mod.THelper.Two = 1;',
  23855. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23856. '$mod.THelper.Two = 1;',
  23857. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23858. '$mod.THelper.Two = 1;',
  23859. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23860. 'var $with = $mod.o;',
  23861. '$mod.THelper.Two = 1;',
  23862. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23863. '']));
  23864. end;
  23865. procedure TTestModule.TestExtClassHelper_Method_Call;
  23866. begin
  23867. StartProgram(false);
  23868. Add([
  23869. '{$modeswitch externalclass}',
  23870. 'type',
  23871. ' TFly = function(w: word): word of object;',
  23872. ' TExtA = class external name ''ExtObj''',
  23873. ' procedure Run(w: word = 10);',
  23874. ' end;',
  23875. ' THelper = class helper for TExtA',
  23876. ' function Foo(w: word = 1): word;',
  23877. ' function Fly(w: word = 2): word; external name ''Fly'';',
  23878. ' end;',
  23879. 'var p: TFly;',
  23880. 'function THelper.foo(w: word): word;',
  23881. 'begin',
  23882. ' Run;',
  23883. ' Run();',
  23884. ' Run(11);',
  23885. ' Foo;',
  23886. ' Foo();',
  23887. ' Foo(12);',
  23888. ' Self.Foo;',
  23889. ' Self.Foo();',
  23890. ' Self.Foo(13);',
  23891. ' Fly;',
  23892. ' Fly();',
  23893. ' with Self do begin',
  23894. ' Foo;',
  23895. ' Foo();',
  23896. ' Foo(14);',
  23897. ' Fly;',
  23898. ' Fly();',
  23899. ' end;',
  23900. ' p:=@Fly;',
  23901. 'end;',
  23902. 'var Obj: TExtA;',
  23903. 'begin',
  23904. ' obj.Foo;',
  23905. ' obj.Foo();',
  23906. ' obj.Foo(21);',
  23907. ' obj.Fly;',
  23908. ' obj.Fly();',
  23909. ' with obj do begin',
  23910. ' Foo;',
  23911. ' Foo();',
  23912. ' Foo(22);',
  23913. ' Fly;',
  23914. ' Fly();',
  23915. ' end;',
  23916. ' p:[email protected];',
  23917. '']);
  23918. ConvertProgram;
  23919. CheckSource('TestExtClassHelper_Method_Call',
  23920. LinesToStr([ // statements
  23921. 'rtl.createHelper(this, "THelper", null, function () {',
  23922. ' this.Foo = function (w) {',
  23923. ' var Result = 0;',
  23924. ' this.Run(10);',
  23925. ' this.Run(10);',
  23926. ' this.Run(11);',
  23927. ' $mod.THelper.Foo.call(this, 1);',
  23928. ' $mod.THelper.Foo.call(this, 1);',
  23929. ' $mod.THelper.Foo.call(this, 12);',
  23930. ' $mod.THelper.Foo.call(this, 1);',
  23931. ' $mod.THelper.Foo.call(this, 1);',
  23932. ' $mod.THelper.Foo.call(this, 13);',
  23933. ' this.Fly(2);',
  23934. ' this.Fly(2);',
  23935. ' $mod.THelper.Foo.call(this, 1);',
  23936. ' $mod.THelper.Foo.call(this, 1);',
  23937. ' $mod.THelper.Foo.call(this, 14);',
  23938. ' this.Fly(2);',
  23939. ' this.Fly(2);',
  23940. ' $mod.p = rtl.createCallback(this, "Fly");',
  23941. ' return Result;',
  23942. ' };',
  23943. '});',
  23944. 'this.p = null;',
  23945. 'this.Obj = null;',
  23946. '']),
  23947. LinesToStr([ // $mod.$main
  23948. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23949. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23950. '$mod.THelper.Foo.call($mod.Obj, 21);',
  23951. '$mod.Obj.Fly(2);',
  23952. '$mod.Obj.Fly(2);',
  23953. 'var $with = $mod.Obj;',
  23954. '$mod.THelper.Foo.call($with, 1);',
  23955. '$mod.THelper.Foo.call($with, 1);',
  23956. '$mod.THelper.Foo.call($with, 22);',
  23957. '$with.Fly(2);',
  23958. '$with.Fly(2);',
  23959. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  23960. '']));
  23961. end;
  23962. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  23963. begin
  23964. StartProgram(false);
  23965. Add([
  23966. '{$modeswitch externalclass}',
  23967. 'type',
  23968. ' TExtA = class external name ''ExtObj''',
  23969. ' procedure Run(w: word = 10);',
  23970. ' end;',
  23971. ' THelper = class helper for TExtA',
  23972. ' class procedure Fly;',
  23973. ' end;',
  23974. 'class procedure THelper.Fly;',
  23975. 'begin end;',
  23976. 'begin',
  23977. '']);
  23978. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  23979. nHelperClassMethodForExtClassMustBeStatic);
  23980. ConvertProgram;
  23981. end;
  23982. procedure TTestModule.TestRecordHelper_ClassVar;
  23983. begin
  23984. StartProgram(false);
  23985. Add([
  23986. 'type',
  23987. ' TRec = record',
  23988. ' end;',
  23989. ' THelper = record helper for TRec',
  23990. ' const',
  23991. ' One = 1;',
  23992. ' Two: word = 2;',
  23993. ' class var',
  23994. ' Glob: word;',
  23995. ' function Foo(w: word): word;',
  23996. ' class function Bar(w: word): word; static;',
  23997. ' end;',
  23998. 'function THelper.foo(w: word): word;',
  23999. 'begin',
  24000. ' Result:=w;',
  24001. ' Two:=One+w;',
  24002. ' Glob:=Glob;',
  24003. ' Result:=Self.Glob;',
  24004. ' Self.Glob:=Self.Glob;',
  24005. ' with Self do Glob:=Glob;',
  24006. ' Self:=Self;',
  24007. 'end;',
  24008. 'class function THelper.bar(w: word): word;',
  24009. 'begin',
  24010. ' Result:=w;',
  24011. ' Two:=One;',
  24012. ' Glob:=Glob;',
  24013. 'end;',
  24014. 'var r: TRec;',
  24015. 'begin',
  24016. ' trec.two:=trec.one;',
  24017. ' trec.Glob:=trec.Glob;',
  24018. ' with trec do begin',
  24019. ' two:=one;',
  24020. ' Glob:=Glob;',
  24021. ' end;',
  24022. ' r.two:=r.one;',
  24023. ' r.Glob:=r.Glob;',
  24024. ' with r do begin',
  24025. ' two:=one;',
  24026. ' Glob:=Glob;',
  24027. ' end;',
  24028. '']);
  24029. ConvertProgram;
  24030. CheckSource('TestRecordHelper_ClassVar',
  24031. LinesToStr([ // statements
  24032. 'rtl.recNewT(this, "TRec", function () {',
  24033. ' this.$eq = function (b) {',
  24034. ' return true;',
  24035. ' };',
  24036. ' this.$assign = function (s) {',
  24037. ' return this;',
  24038. ' };',
  24039. '});',
  24040. 'rtl.createHelper(this, "THelper", null, function () {',
  24041. ' this.One = 1;',
  24042. ' this.Two = 2;',
  24043. ' this.Glob = 0;',
  24044. ' this.Foo = function (w) {',
  24045. ' var Result = 0;',
  24046. ' Result = w;',
  24047. ' $mod.THelper.Two = 1 + w;',
  24048. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24049. ' Result = $mod.THelper.Glob;',
  24050. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24051. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24052. ' this.$assign(this);',
  24053. ' return Result;',
  24054. ' };',
  24055. ' this.Bar = function (w) {',
  24056. ' var Result = 0;',
  24057. ' Result = w;',
  24058. ' $mod.THelper.Two = 1;',
  24059. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24060. ' return Result;',
  24061. ' };',
  24062. '});',
  24063. 'this.r = this.TRec.$new();',
  24064. '']),
  24065. LinesToStr([ // $mod.$main
  24066. '$mod.THelper.Two = 1;',
  24067. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24068. 'var $with = $mod.TRec;',
  24069. '$mod.THelper.Two = 1;',
  24070. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24071. '$mod.THelper.Two = 1;',
  24072. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24073. 'var $with1 = $mod.r;',
  24074. '$mod.THelper.Two = 1;',
  24075. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24076. '']));
  24077. end;
  24078. procedure TTestModule.TestRecordHelper_Method_Call;
  24079. begin
  24080. StartProgram(false);
  24081. Add([
  24082. '{$modeswitch AdvancedRecords}',
  24083. 'type',
  24084. ' TRec = record',
  24085. ' procedure Run(w: word = 10);',
  24086. ' end;',
  24087. ' THelper = record helper for TRec',
  24088. ' function Foo(w: word = 1): word;',
  24089. ' end;',
  24090. 'procedure TRec.Run(w: word);',
  24091. 'begin',
  24092. ' Foo;',
  24093. ' Foo();',
  24094. ' Foo(2);',
  24095. ' Self.Foo;',
  24096. ' Self.Foo();',
  24097. ' Self.Foo(3);',
  24098. ' with Self do begin',
  24099. ' Foo;',
  24100. ' Foo();',
  24101. ' Foo(4);',
  24102. ' end;',
  24103. 'end;',
  24104. 'function THelper.foo(w: word): word;',
  24105. 'begin',
  24106. ' Run;',
  24107. ' Run();',
  24108. ' Run(11);',
  24109. ' Foo;',
  24110. ' Foo();',
  24111. ' Foo(12);',
  24112. ' Self.Foo;',
  24113. ' Self.Foo();',
  24114. ' Self.Foo(13);',
  24115. ' with Self do begin',
  24116. ' Foo;',
  24117. ' Foo();',
  24118. ' Foo(14);',
  24119. ' end;',
  24120. 'end;',
  24121. 'var Rec: TRec;',
  24122. 'begin',
  24123. ' Rec.Foo;',
  24124. ' Rec.Foo();',
  24125. ' Rec.Foo(21);',
  24126. ' with Rec do begin',
  24127. ' Foo;',
  24128. ' Foo();',
  24129. ' Foo(22);',
  24130. ' end;',
  24131. '']);
  24132. ConvertProgram;
  24133. CheckSource('TestRecordHelper_Method_Call',
  24134. LinesToStr([ // statements
  24135. 'rtl.recNewT(this, "TRec", function () {',
  24136. ' this.$eq = function (b) {',
  24137. ' return true;',
  24138. ' };',
  24139. ' this.$assign = function (s) {',
  24140. ' return this;',
  24141. ' };',
  24142. ' this.Run = function (w) {',
  24143. ' $mod.THelper.Foo.call(this, 1);',
  24144. ' $mod.THelper.Foo.call(this, 1);',
  24145. ' $mod.THelper.Foo.call(this, 2);',
  24146. ' $mod.THelper.Foo.call(this, 1);',
  24147. ' $mod.THelper.Foo.call(this, 1);',
  24148. ' $mod.THelper.Foo.call(this, 3);',
  24149. ' $mod.THelper.Foo.call(this, 1);',
  24150. ' $mod.THelper.Foo.call(this, 1);',
  24151. ' $mod.THelper.Foo.call(this, 4);',
  24152. ' };',
  24153. '});',
  24154. 'rtl.createHelper(this, "THelper", null, function () {',
  24155. ' this.Foo = function (w) {',
  24156. ' var Result = 0;',
  24157. ' this.Run(10);',
  24158. ' this.Run(10);',
  24159. ' this.Run(11);',
  24160. ' $mod.THelper.Foo.call(this, 1);',
  24161. ' $mod.THelper.Foo.call(this, 1);',
  24162. ' $mod.THelper.Foo.call(this, 12);',
  24163. ' $mod.THelper.Foo.call(this, 1);',
  24164. ' $mod.THelper.Foo.call(this, 1);',
  24165. ' $mod.THelper.Foo.call(this, 13);',
  24166. ' $mod.THelper.Foo.call(this, 1);',
  24167. ' $mod.THelper.Foo.call(this, 1);',
  24168. ' $mod.THelper.Foo.call(this, 14);',
  24169. ' return Result;',
  24170. ' };',
  24171. '});',
  24172. 'this.Rec = this.TRec.$new();',
  24173. '']),
  24174. LinesToStr([ // $mod.$main
  24175. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24176. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24177. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24178. 'var $with = $mod.Rec;',
  24179. '$mod.THelper.Foo.call($with, 1);',
  24180. '$mod.THelper.Foo.call($with, 1);',
  24181. '$mod.THelper.Foo.call($with, 22);',
  24182. '']));
  24183. end;
  24184. procedure TTestModule.TestRecordHelper_Constructor;
  24185. begin
  24186. StartProgram(false);
  24187. Add([
  24188. '{$modeswitch AdvancedRecords}',
  24189. 'type',
  24190. ' TRec = record',
  24191. ' constructor Create(w: word);',
  24192. ' end;',
  24193. ' THelper = record helper for TRec',
  24194. ' constructor NewHlp(w: word);',
  24195. ' end;',
  24196. 'var',
  24197. ' Rec: TRec;',
  24198. 'constructor TRec.Create(w: word);',
  24199. 'begin',
  24200. ' NewHlp(2);', // normal call
  24201. ' trec.NewHlp(3);', // new instance
  24202. 'end;',
  24203. 'constructor THelper.NewHlp(w: word);',
  24204. 'begin',
  24205. ' create(2);', // normal call
  24206. ' trec.create(3);', // new instance
  24207. ' NewHlp(4);', // normal call
  24208. ' trec.NewHlp(5);', // new instance
  24209. 'end;',
  24210. 'begin',
  24211. ' rec.newhlp(2);', // normal call
  24212. ' with rec do newhlp(12);', // normal call
  24213. ' trec.newhlp(3);', // new instance
  24214. ' with trec do newhlp(13);', // new instance
  24215. '']);
  24216. ConvertProgram;
  24217. CheckSource('TestRecordHelper_Constructor',
  24218. LinesToStr([ // statements
  24219. 'rtl.recNewT(this, "TRec", function () {',
  24220. ' this.$eq = function (b) {',
  24221. ' return true;',
  24222. ' };',
  24223. ' this.$assign = function (s) {',
  24224. ' return this;',
  24225. ' };',
  24226. ' this.Create = function (w) {',
  24227. ' $mod.THelper.NewHlp.call(this, 2);',
  24228. ' $mod.THelper.$new("NewHlp", [3]);',
  24229. ' return this;',
  24230. ' };',
  24231. '});',
  24232. 'rtl.createHelper(this, "THelper", null, function () {',
  24233. ' this.NewHlp = function (w) {',
  24234. ' this.Create(2);',
  24235. ' $mod.TRec.$new().Create(3);',
  24236. ' $mod.THelper.NewHlp.call(this, 4);',
  24237. ' $mod.THelper.$new("NewHlp", [5]);',
  24238. ' return this;',
  24239. ' };',
  24240. ' this.$new = function (fn, args) {',
  24241. ' return this[fn].apply($mod.TRec.$new(), args);',
  24242. ' };',
  24243. '});',
  24244. 'this.Rec = this.TRec.$new();',
  24245. '']),
  24246. LinesToStr([ // $mod.$main
  24247. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24248. 'var $with = $mod.Rec;',
  24249. '$mod.THelper.NewHlp.call($with, 12);',
  24250. '$mod.THelper.$new("NewHlp", [3]);',
  24251. 'var $with1 = $mod.TRec;',
  24252. '$mod.THelper.$new("NewHlp", [13]);',
  24253. '']));
  24254. end;
  24255. procedure TTestModule.TestTypeHelper_ClassVar;
  24256. begin
  24257. StartProgram(false);
  24258. Add([
  24259. '{$modeswitch typehelpers}',
  24260. 'type',
  24261. ' THelper = type helper for byte',
  24262. ' const',
  24263. ' One = 1;',
  24264. ' Two: word = 2;',
  24265. ' class var',
  24266. ' Glob: word;',
  24267. ' function Foo(w: word): word;',
  24268. ' class function Bar(w: word): word; static;',
  24269. ' end;',
  24270. 'function THelper.foo(w: word): word;',
  24271. 'begin',
  24272. ' Result:=w;',
  24273. ' Two:=One+w;',
  24274. ' Glob:=Glob;',
  24275. ' Result:=Self.Glob;',
  24276. ' Self.Glob:=Self.Glob;',
  24277. ' with Self do Glob:=Glob;',
  24278. 'end;',
  24279. 'class function THelper.bar(w: word): word;',
  24280. 'begin',
  24281. ' Result:=w;',
  24282. ' Two:=One;',
  24283. ' Glob:=Glob;',
  24284. 'end;',
  24285. 'var b: byte;',
  24286. 'begin',
  24287. ' byte.two:=byte.one;',
  24288. ' byte.Glob:=byte.Glob;',
  24289. ' with byte do begin',
  24290. ' two:=one;',
  24291. ' Glob:=Glob;',
  24292. ' end;',
  24293. ' b.two:=b.one;',
  24294. ' b.Glob:=b.Glob;',
  24295. ' with b do begin',
  24296. ' two:=one;',
  24297. ' Glob:=Glob;',
  24298. ' end;',
  24299. '']);
  24300. ConvertProgram;
  24301. CheckSource('TestTypeHelper_ClassVar',
  24302. LinesToStr([ // statements
  24303. 'rtl.createHelper(this, "THelper", null, function () {',
  24304. ' this.One = 1;',
  24305. ' this.Two = 2;',
  24306. ' this.Glob = 0;',
  24307. ' this.Foo = function (w) {',
  24308. ' var Result = 0;',
  24309. ' Result = w;',
  24310. ' $mod.THelper.Two = 1 + w;',
  24311. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24312. ' Result = $mod.THelper.Glob;',
  24313. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24314. ' var $with = this.get();',
  24315. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24316. ' return Result;',
  24317. ' };',
  24318. ' this.Bar = function (w) {',
  24319. ' var Result = 0;',
  24320. ' Result = w;',
  24321. ' $mod.THelper.Two = 1;',
  24322. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24323. ' return Result;',
  24324. ' };',
  24325. '});',
  24326. 'this.b = 0;',
  24327. '']),
  24328. LinesToStr([ // $mod.$main
  24329. '$mod.THelper.Two = 1;',
  24330. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24331. '$mod.THelper.Two = 1;',
  24332. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24333. '$mod.THelper.Two = 1;',
  24334. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24335. 'var $with = $mod.b;',
  24336. '$mod.THelper.Two = 1;',
  24337. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24338. '']));
  24339. end;
  24340. procedure TTestModule.TestTypeHelper_PassResultElement;
  24341. begin
  24342. StartProgram(false);
  24343. Add([
  24344. '{$modeswitch typehelpers}',
  24345. 'type',
  24346. ' THelper = type helper for word',
  24347. ' procedure DoIt(e: byte = 123);',
  24348. ' class procedure DoSome(e: byte = 456); static;',
  24349. ' end;',
  24350. 'procedure THelper.DoIt(e: byte);',
  24351. 'begin',
  24352. 'end;',
  24353. 'class procedure THelper.DoSome(e: byte);',
  24354. 'begin',
  24355. 'end;',
  24356. 'function Foo(w: word): word;',
  24357. 'begin',
  24358. ' Result.DoIt;',
  24359. ' Result.DoIt();',
  24360. ' Result.DoSome;',
  24361. ' Result.DoSome();',
  24362. ' with Result do begin',
  24363. ' DoIt;',
  24364. ' DoIt();',
  24365. ' DoSome;',
  24366. ' DoSome();',
  24367. ' end;',
  24368. 'end;',
  24369. 'begin',
  24370. '']);
  24371. ConvertProgram;
  24372. CheckSource('TestTypeHelper_PassResultElement',
  24373. LinesToStr([ // statements
  24374. 'rtl.createHelper(this, "THelper", null, function () {',
  24375. ' this.DoIt = function (e) {',
  24376. ' };',
  24377. ' this.DoSome = function (e) {',
  24378. ' };',
  24379. '});',
  24380. 'this.Foo = function (w) {',
  24381. ' var Result = 0;',
  24382. ' $mod.THelper.DoIt.call({',
  24383. ' get: function () {',
  24384. ' return Result;',
  24385. ' },',
  24386. ' set: function (v) {',
  24387. ' Result = v;',
  24388. ' }',
  24389. ' }, 123);',
  24390. ' $mod.THelper.DoIt.call({',
  24391. ' get: function () {',
  24392. ' return Result;',
  24393. ' },',
  24394. ' set: function (v) {',
  24395. ' Result = v;',
  24396. ' }',
  24397. ' }, 123);',
  24398. ' $mod.THelper.DoSome(456);',
  24399. ' $mod.THelper.DoSome(456);',
  24400. ' $mod.THelper.DoIt.call({',
  24401. ' get: function () {',
  24402. ' return Result;',
  24403. ' },',
  24404. ' set: function (v) {',
  24405. ' Result = v;',
  24406. ' }',
  24407. ' }, 123);',
  24408. ' $mod.THelper.DoIt.call({',
  24409. ' get: function () {',
  24410. ' return Result;',
  24411. ' },',
  24412. ' set: function (v) {',
  24413. ' Result = v;',
  24414. ' }',
  24415. ' }, 123);',
  24416. ' $mod.THelper.DoSome(456);',
  24417. ' $mod.THelper.DoSome(456);',
  24418. ' return Result;',
  24419. '};',
  24420. '']),
  24421. LinesToStr([ // $mod.$main
  24422. '']));
  24423. end;
  24424. procedure TTestModule.TestTypeHelper_PassArgs;
  24425. begin
  24426. StartProgram(false);
  24427. Add([
  24428. '{$modeswitch typehelpers}',
  24429. 'type',
  24430. ' THelper = type helper for word',
  24431. ' procedure DoIt(e: byte = 123);',
  24432. ' end;',
  24433. 'procedure THelper.DoIt(e: byte);',
  24434. 'begin',
  24435. 'end;',
  24436. 'procedure FooDefault(a: word);',
  24437. 'begin',
  24438. ' a.DoIt;',
  24439. ' with a do DoIt;',
  24440. 'end;',
  24441. 'procedure FooConst(const a: word);',
  24442. 'begin',
  24443. ' a.DoIt;',
  24444. ' with a do DoIt;',
  24445. 'end;',
  24446. 'procedure FooVar(var a: word);',
  24447. 'begin',
  24448. ' a.DoIt;',
  24449. ' with a do DoIt;',
  24450. 'end;',
  24451. 'begin',
  24452. '']);
  24453. ConvertProgram;
  24454. CheckSource('TestTypeHelper_PassArgs',
  24455. LinesToStr([ // statements
  24456. 'rtl.createHelper(this, "THelper", null, function () {',
  24457. ' this.DoIt = function (e) {',
  24458. ' };',
  24459. '});',
  24460. 'this.FooDefault = function (a) {',
  24461. ' $mod.THelper.DoIt.call({',
  24462. ' get: function () {',
  24463. ' return a;',
  24464. ' },',
  24465. ' set: function (v) {',
  24466. ' a = v;',
  24467. ' }',
  24468. ' }, 123);',
  24469. ' $mod.THelper.DoIt.call({',
  24470. ' get: function () {',
  24471. ' return a;',
  24472. ' },',
  24473. ' set: function (v) {',
  24474. ' a = v;',
  24475. ' }',
  24476. ' }, 123);',
  24477. '};',
  24478. 'this.FooConst = function (a) {',
  24479. ' $mod.THelper.DoIt.call({',
  24480. ' get: function () {',
  24481. ' return a;',
  24482. ' },',
  24483. ' set: function (v) {',
  24484. ' rtl.raiseE("EPropReadOnly");',
  24485. ' }',
  24486. ' }, 123);',
  24487. ' $mod.THelper.DoIt.call({',
  24488. ' get: function () {',
  24489. ' return a;',
  24490. ' },',
  24491. ' set: function () {',
  24492. ' rtl.raiseE("EPropReadOnly");',
  24493. ' }',
  24494. ' }, 123);',
  24495. '};',
  24496. 'this.FooVar = function (a) {',
  24497. ' $mod.THelper.DoIt.call(a, 123);',
  24498. ' var $with = a.get();',
  24499. ' $mod.THelper.DoIt.call(a, 123);',
  24500. '};',
  24501. '']),
  24502. LinesToStr([ // $mod.$main
  24503. '']));
  24504. end;
  24505. procedure TTestModule.TestTypeHelper_PassVarConst;
  24506. begin
  24507. StartProgram(false);
  24508. Add([
  24509. '{$modeswitch typehelpers}',
  24510. 'type',
  24511. ' THelper = type helper for word',
  24512. ' procedure DoIt(e: byte = 123);',
  24513. ' end;',
  24514. 'procedure THelper.DoIt(e: byte);',
  24515. 'begin',
  24516. 'end;',
  24517. 'var a: word;',
  24518. 'const c: word = 2;',
  24519. '{$writeableconst off}',
  24520. 'const r: word = 3;',
  24521. 'begin',
  24522. ' a.DoIt;',
  24523. ' with a do DoIt;',
  24524. ' c.DoIt;',
  24525. ' with c do DoIt;',
  24526. ' r.DoIt;',
  24527. ' with r do DoIt;',
  24528. '']);
  24529. ConvertProgram;
  24530. CheckSource('TestTypeHelper_PassVarConst',
  24531. LinesToStr([ // statements
  24532. 'rtl.createHelper(this, "THelper", null, function () {',
  24533. ' this.DoIt = function (e) {',
  24534. ' };',
  24535. '});',
  24536. 'this.a = 0;',
  24537. 'this.c = 2;',
  24538. 'this.r = 3;',
  24539. '']),
  24540. LinesToStr([ // $mod.$main
  24541. '$mod.THelper.DoIt.call({',
  24542. ' p: $mod,',
  24543. ' get: function () {',
  24544. ' return this.p.a;',
  24545. ' },',
  24546. ' set: function (v) {',
  24547. ' this.p.a = v;',
  24548. ' }',
  24549. '}, 123);',
  24550. 'var $with = $mod.a;',
  24551. '$mod.THelper.DoIt.call({',
  24552. ' get: function () {',
  24553. ' return $with;',
  24554. ' },',
  24555. ' set: function (v) {',
  24556. ' $with = v;',
  24557. ' }',
  24558. '}, 123);',
  24559. '$mod.THelper.DoIt.call({',
  24560. ' p: $mod,',
  24561. ' get: function () {',
  24562. ' return this.p.c;',
  24563. ' },',
  24564. ' set: function (v) {',
  24565. ' this.p.c = v;',
  24566. ' }',
  24567. '}, 123);',
  24568. 'var $with1 = $mod.c;',
  24569. '$mod.THelper.DoIt.call({',
  24570. ' get: function () {',
  24571. ' return $with1;',
  24572. ' },',
  24573. ' set: function (v) {',
  24574. ' $with1 = v;',
  24575. ' }',
  24576. '}, 123);',
  24577. '$mod.THelper.DoIt.call({',
  24578. ' get: function () {',
  24579. ' return 3;',
  24580. ' },',
  24581. ' set: function (v) {',
  24582. ' rtl.raiseE("EPropReadOnly");',
  24583. ' }',
  24584. '}, 123);',
  24585. 'var $with2 = 3;',
  24586. ' $mod.THelper.DoIt.call({',
  24587. ' get: function () {',
  24588. ' return $with2;',
  24589. ' },',
  24590. ' set: function () {',
  24591. ' rtl.raiseE("EPropReadOnly");',
  24592. ' }',
  24593. ' }, 123);',
  24594. '']));
  24595. end;
  24596. procedure TTestModule.TestTypeHelper_PassFuncResult;
  24597. begin
  24598. StartProgram(false);
  24599. Add([
  24600. '{$modeswitch typehelpers}',
  24601. 'type',
  24602. ' THelper = type helper for word',
  24603. ' procedure DoIt(e: byte = 123);',
  24604. ' end;',
  24605. 'procedure THelper.DoIt(e: byte);',
  24606. 'begin',
  24607. 'end;',
  24608. 'function Foo(b: byte = 1): word;',
  24609. 'begin',
  24610. 'end;',
  24611. 'begin',
  24612. ' Foo.DoIt;',
  24613. ' Foo().DoIt;',
  24614. ' with Foo do DoIt;',
  24615. ' with Foo() do DoIt;',
  24616. '']);
  24617. ConvertProgram;
  24618. CheckSource('TestTypeHelper_PassFuncResult',
  24619. LinesToStr([ // statements
  24620. 'rtl.createHelper(this, "THelper", null, function () {',
  24621. ' this.DoIt = function (e) {',
  24622. ' };',
  24623. '});',
  24624. 'this.Foo = function (b) {',
  24625. ' var Result = 0;',
  24626. ' return Result;',
  24627. '};',
  24628. '']),
  24629. LinesToStr([ // $mod.$main
  24630. '$mod.THelper.DoIt.call({',
  24631. ' a: $mod.Foo(1),',
  24632. ' get: function () {',
  24633. ' return this.a;',
  24634. ' },',
  24635. ' set: function (v) {',
  24636. ' this.a = v;',
  24637. ' }',
  24638. '}, 123);',
  24639. '$mod.THelper.DoIt.call({',
  24640. ' a: $mod.Foo(1),',
  24641. ' get: function () {',
  24642. ' return this.a;',
  24643. ' },',
  24644. ' set: function (v) {',
  24645. ' this.a = v;',
  24646. ' }',
  24647. '}, 123);',
  24648. 'var $with = $mod.Foo(1);',
  24649. '$mod.THelper.DoIt.call({',
  24650. ' get: function () {',
  24651. ' return $with;',
  24652. ' },',
  24653. ' set: function (v) {',
  24654. ' $with = v;',
  24655. ' }',
  24656. '}, 123);',
  24657. 'var $with1 = $mod.Foo(1);',
  24658. '$mod.THelper.DoIt.call({',
  24659. ' get: function () {',
  24660. ' return $with1;',
  24661. ' },',
  24662. ' set: function (v) {',
  24663. ' $with1 = v;',
  24664. ' }',
  24665. '}, 123);',
  24666. '']));
  24667. end;
  24668. procedure TTestModule.TestTypeHelper_PassPropertyField;
  24669. begin
  24670. StartProgram(false);
  24671. Add([
  24672. '{$modeswitch typehelpers}',
  24673. 'type',
  24674. ' TObject = class',
  24675. ' FField: word;',
  24676. ' procedure SetField(Value: word);',
  24677. ' property Field: word read FField write SetField;',
  24678. ' end;',
  24679. ' THelper = type helper for word',
  24680. ' procedure Fly;',
  24681. ' class procedure Run; static;',
  24682. ' end;',
  24683. 'procedure TObject.SetField(Value: word);',
  24684. 'begin',
  24685. ' Field.Fly;',
  24686. ' Field.Run;',
  24687. ' Self.Field.Fly;',
  24688. ' Self.Field.Run;',
  24689. ' with Self do begin',
  24690. ' Field.Fly;',
  24691. ' Field.Run;',
  24692. ' end;',
  24693. ' with Self.Field do begin',
  24694. ' Fly;',
  24695. ' Run;',
  24696. ' end;',
  24697. 'end;',
  24698. 'procedure THelper.Fly;',
  24699. 'begin',
  24700. 'end;',
  24701. 'class procedure THelper.Run;',
  24702. 'begin',
  24703. 'end;',
  24704. 'var',
  24705. ' o: TObject;',
  24706. 'begin',
  24707. ' o.Field.Fly;',
  24708. ' o.Field.Run;',
  24709. ' with o do begin',
  24710. ' Field.Fly;',
  24711. ' Field.Run;',
  24712. ' end;',
  24713. ' with o.Field do begin',
  24714. ' Fly;',
  24715. ' Run;',
  24716. ' end;',
  24717. '']);
  24718. ConvertProgram;
  24719. CheckSource('TestTypeHelper_PassPropertyField',
  24720. LinesToStr([ // statements
  24721. 'rtl.createClass(this, "TObject", null, function () {',
  24722. ' this.$init = function () {',
  24723. ' this.FField = 0;',
  24724. ' };',
  24725. ' this.$final = function () {',
  24726. ' };',
  24727. ' this.SetField = function (Value) {',
  24728. ' $mod.THelper.Fly.call({',
  24729. ' p: this,',
  24730. ' get: function () {',
  24731. ' return this.p.FField;',
  24732. ' },',
  24733. ' set: function (v) {',
  24734. ' this.p.FField = v;',
  24735. ' }',
  24736. ' });',
  24737. ' $mod.THelper.Run();',
  24738. ' $mod.THelper.Fly.call({',
  24739. ' p: this,',
  24740. ' get: function () {',
  24741. ' return this.p.FField;',
  24742. ' },',
  24743. ' set: function (v) {',
  24744. ' this.p.FField = v;',
  24745. ' }',
  24746. ' });',
  24747. ' $mod.THelper.Run();',
  24748. ' $mod.THelper.Fly.call({',
  24749. ' p: this,',
  24750. ' get: function () {',
  24751. ' return this.p.FField;',
  24752. ' },',
  24753. ' set: function (v) {',
  24754. ' this.p.FField = v;',
  24755. ' }',
  24756. ' });',
  24757. ' $mod.THelper.Run();',
  24758. ' var $with = this.FField;',
  24759. ' $mod.THelper.Fly.call({',
  24760. ' get: function () {',
  24761. ' return $with;',
  24762. ' },',
  24763. ' set: function (v) {',
  24764. ' $with = v;',
  24765. ' }',
  24766. ' });',
  24767. ' $mod.THelper.Run();',
  24768. ' };',
  24769. '});',
  24770. 'rtl.createHelper(this, "THelper", null, function () {',
  24771. ' this.Fly = function () {',
  24772. ' };',
  24773. ' this.Run = function () {',
  24774. ' };',
  24775. '});',
  24776. 'this.o = null;',
  24777. '']),
  24778. LinesToStr([ // $mod.$main
  24779. '$mod.THelper.Fly.call({',
  24780. ' p: $mod.o,',
  24781. ' get: function () {',
  24782. ' return this.p.FField;',
  24783. ' },',
  24784. ' set: function (v) {',
  24785. ' this.p.FField = v;',
  24786. ' }',
  24787. '});',
  24788. '$mod.THelper.Run();',
  24789. 'var $with = $mod.o;',
  24790. '$mod.THelper.Fly.call({',
  24791. ' p: $with,',
  24792. ' get: function () {',
  24793. ' return this.p.FField;',
  24794. ' },',
  24795. ' set: function (v) {',
  24796. ' this.p.FField = v;',
  24797. ' }',
  24798. '});',
  24799. '$mod.THelper.Run();',
  24800. 'var $with1 = $mod.o.FField;',
  24801. '$mod.THelper.Fly.call({',
  24802. ' get: function () {',
  24803. ' return $with1;',
  24804. ' },',
  24805. ' set: function (v) {',
  24806. ' $with1 = v;',
  24807. ' }',
  24808. '});',
  24809. '$mod.THelper.Run();',
  24810. '']));
  24811. end;
  24812. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  24813. begin
  24814. StartProgram(false);
  24815. Add([
  24816. '{$modeswitch typehelpers}',
  24817. 'type',
  24818. ' TObject = class',
  24819. ' FField: word;',
  24820. ' function GetField: word;',
  24821. ' property Field: word read GetField write FField;',
  24822. ' end;',
  24823. ' THelper = type helper for word',
  24824. ' procedure Fly;',
  24825. ' class procedure Run; static;',
  24826. ' end;',
  24827. 'function TObject.GetField: word;',
  24828. 'begin',
  24829. ' Field.Fly;',
  24830. ' Field.Run;',
  24831. ' Self.Field.Fly;',
  24832. ' Self.Field.Run;',
  24833. ' with Self do begin',
  24834. ' Field.Fly;',
  24835. ' Field.Run;',
  24836. ' end;',
  24837. ' with Self.Field do begin',
  24838. ' Fly;',
  24839. ' Run;',
  24840. ' end;',
  24841. 'end;',
  24842. 'procedure THelper.Fly;',
  24843. 'begin',
  24844. 'end;',
  24845. 'class procedure THelper.Run;',
  24846. 'begin',
  24847. 'end;',
  24848. 'var',
  24849. ' o: TObject;',
  24850. 'begin',
  24851. ' o.Field.Fly;',
  24852. ' o.Field.Run;',
  24853. ' with o do begin',
  24854. ' Field.Fly;',
  24855. ' Field.Run;',
  24856. ' end;',
  24857. ' with o.Field do begin',
  24858. ' Fly;',
  24859. ' Run;',
  24860. ' end;',
  24861. '']);
  24862. ConvertProgram;
  24863. CheckSource('TestTypeHelper_PassPropertyGetter',
  24864. LinesToStr([ // statements
  24865. 'rtl.createClass(this, "TObject", null, function () {',
  24866. ' this.$init = function () {',
  24867. ' this.FField = 0;',
  24868. ' };',
  24869. ' this.$final = function () {',
  24870. ' };',
  24871. ' this.GetField = function () {',
  24872. ' var Result = 0;',
  24873. ' $mod.THelper.Fly.call({',
  24874. ' p: this.GetField(),',
  24875. ' get: function () {',
  24876. ' return this.p;',
  24877. ' },',
  24878. ' set: function (v) {',
  24879. ' this.p = v;',
  24880. ' }',
  24881. ' });',
  24882. ' $mod.THelper.Run();',
  24883. ' $mod.THelper.Fly.call({',
  24884. ' p: this.GetField(),',
  24885. ' get: function () {',
  24886. ' return this.p;',
  24887. ' },',
  24888. ' set: function (v) {',
  24889. ' this.p = v;',
  24890. ' }',
  24891. ' });',
  24892. ' $mod.THelper.Run();',
  24893. ' $mod.THelper.Fly.call({',
  24894. ' p: this.GetField(),',
  24895. ' get: function () {',
  24896. ' return this.p;',
  24897. ' },',
  24898. ' set: function (v) {',
  24899. ' this.p = v;',
  24900. ' }',
  24901. ' });',
  24902. ' $mod.THelper.Run();',
  24903. ' var $with = this.GetField();',
  24904. ' $mod.THelper.Fly.call({',
  24905. ' get: function () {',
  24906. ' return $with;',
  24907. ' },',
  24908. ' set: function (v) {',
  24909. ' $with = v;',
  24910. ' }',
  24911. ' });',
  24912. ' $mod.THelper.Run();',
  24913. ' return Result;',
  24914. ' };',
  24915. '});',
  24916. 'rtl.createHelper(this, "THelper", null, function () {',
  24917. ' this.Fly = function () {',
  24918. ' };',
  24919. ' this.Run = function () {',
  24920. ' };',
  24921. '});',
  24922. 'this.o = null;',
  24923. '']),
  24924. LinesToStr([ // $mod.$main
  24925. '$mod.THelper.Fly.call({',
  24926. ' p: $mod.o.GetField(),',
  24927. ' get: function () {',
  24928. ' return this.p;',
  24929. ' },',
  24930. ' set: function (v) {',
  24931. ' this.p = v;',
  24932. ' }',
  24933. '});',
  24934. '$mod.THelper.Run();',
  24935. 'var $with = $mod.o;',
  24936. '$mod.THelper.Fly.call({',
  24937. ' p: $with.GetField(),',
  24938. ' get: function () {',
  24939. ' return this.p;',
  24940. ' },',
  24941. ' set: function (v) {',
  24942. ' this.p = v;',
  24943. ' }',
  24944. '});',
  24945. '$mod.THelper.Run();',
  24946. 'var $with1 = $mod.o.GetField();',
  24947. '$mod.THelper.Fly.call({',
  24948. ' get: function () {',
  24949. ' return $with1;',
  24950. ' },',
  24951. ' set: function (v) {',
  24952. ' $with1 = v;',
  24953. ' }',
  24954. '});',
  24955. '$mod.THelper.Run();',
  24956. '']));
  24957. end;
  24958. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  24959. begin
  24960. StartProgram(false);
  24961. Add([
  24962. '{$modeswitch typehelpers}',
  24963. 'type',
  24964. ' TObject = class',
  24965. ' class var FField: word;',
  24966. ' class procedure SetField(Value: word);',
  24967. ' class property Field: word read FField write SetField;',
  24968. ' end;',
  24969. ' THelper = type helper for word',
  24970. ' procedure Fly(n: byte);',
  24971. ' end;',
  24972. 'class procedure TObject.SetField(Value: word);',
  24973. 'begin',
  24974. ' Field.Fly(1);',
  24975. ' Self.Field.Fly(2);',
  24976. ' with Self do Field.Fly(3);',
  24977. ' with Self.Field do Fly(4);',
  24978. ' TObject.Field.Fly(5);',
  24979. ' with TObject do Field.Fly(6);',
  24980. ' with TObject.Field do Fly(7);',
  24981. 'end;',
  24982. 'procedure THelper.Fly(n: byte);',
  24983. 'begin',
  24984. 'end;',
  24985. 'var',
  24986. ' o: TObject;',
  24987. 'begin',
  24988. ' o.Field.Fly(11);',
  24989. ' with o do Field.Fly(12);',
  24990. ' with o.Field do Fly(13);',
  24991. ' TObject.Field.Fly(14);',
  24992. ' with TObject do Field.Fly(15);',
  24993. ' with TObject.Field do Fly(16);',
  24994. '']);
  24995. ConvertProgram;
  24996. CheckSource('TestTypeHelper_PassClassPropertyField',
  24997. LinesToStr([ // statements
  24998. 'rtl.createClass(this, "TObject", null, function () {',
  24999. ' this.FField = 0;',
  25000. ' this.$init = function () {',
  25001. ' };',
  25002. ' this.$final = function () {',
  25003. ' };',
  25004. ' this.SetField = function (Value) {',
  25005. ' $mod.THelper.Fly.call({',
  25006. ' p: this,',
  25007. ' get: function () {',
  25008. ' return this.p.FField;',
  25009. ' },',
  25010. ' set: function (v) {',
  25011. ' $mod.TObject.FField = v;',
  25012. ' }',
  25013. ' }, 1);',
  25014. ' $mod.THelper.Fly.call({',
  25015. ' p: this,',
  25016. ' get: function () {',
  25017. ' return this.p.FField;',
  25018. ' },',
  25019. ' set: function (v) {',
  25020. ' $mod.TObject.FField = v;',
  25021. ' }',
  25022. ' }, 2);',
  25023. ' $mod.THelper.Fly.call({',
  25024. ' p: this,',
  25025. ' get: function () {',
  25026. ' return this.p.FField;',
  25027. ' },',
  25028. ' set: function (v) {',
  25029. ' $mod.TObject.FField = v;',
  25030. ' }',
  25031. ' }, 3);',
  25032. ' var $with = this.FField;',
  25033. ' $mod.THelper.Fly.call({',
  25034. ' get: function () {',
  25035. ' return $with;',
  25036. ' },',
  25037. ' set: function (v) {',
  25038. ' $with = v;',
  25039. ' }',
  25040. ' }, 4);',
  25041. ' $mod.THelper.Fly.call({',
  25042. ' p: $mod.TObject,',
  25043. ' get: function () {',
  25044. ' return this.p.FField;',
  25045. ' },',
  25046. ' set: function (v) {',
  25047. ' $mod.TObject.FField = v;',
  25048. ' }',
  25049. ' }, 5);',
  25050. ' var $with1 = $mod.TObject;',
  25051. ' $mod.THelper.Fly.call({',
  25052. ' p: $with1,',
  25053. ' get: function () {',
  25054. ' return this.p.FField;',
  25055. ' },',
  25056. ' set: function (v) {',
  25057. ' $mod.TObject.FField = v;',
  25058. ' }',
  25059. ' }, 6);',
  25060. ' var $with2 = $mod.TObject.FField;',
  25061. ' $mod.THelper.Fly.call({',
  25062. ' get: function () {',
  25063. ' return $with2;',
  25064. ' },',
  25065. ' set: function (v) {',
  25066. ' $with2 = v;',
  25067. ' }',
  25068. ' }, 7);',
  25069. ' };',
  25070. '});',
  25071. 'rtl.createHelper(this, "THelper", null, function () {',
  25072. ' this.Fly = function (n) {',
  25073. ' };',
  25074. '});',
  25075. 'this.o = null;',
  25076. '']),
  25077. LinesToStr([ // $mod.$main
  25078. '$mod.THelper.Fly.call({',
  25079. ' p: $mod.o,',
  25080. ' get: function () {',
  25081. ' return this.p.FField;',
  25082. ' },',
  25083. ' set: function (v) {',
  25084. ' $mod.TObject.FField = v;',
  25085. ' }',
  25086. '}, 11);',
  25087. 'var $with = $mod.o;',
  25088. '$mod.THelper.Fly.call({',
  25089. ' p: $with,',
  25090. ' get: function () {',
  25091. ' return this.p.FField;',
  25092. ' },',
  25093. ' set: function (v) {',
  25094. ' $mod.TObject.FField = v;',
  25095. ' }',
  25096. '}, 12);',
  25097. 'var $with1 = $mod.o.FField;',
  25098. '$mod.THelper.Fly.call({',
  25099. ' get: function () {',
  25100. ' return $with1;',
  25101. ' },',
  25102. ' set: function (v) {',
  25103. ' $with1 = v;',
  25104. ' }',
  25105. '}, 13);',
  25106. '$mod.THelper.Fly.call({',
  25107. ' p: $mod.TObject,',
  25108. ' get: function () {',
  25109. ' return this.p.FField;',
  25110. ' },',
  25111. ' set: function (v) {',
  25112. ' $mod.TObject.FField = v;',
  25113. ' }',
  25114. '}, 14);',
  25115. 'var $with2 = $mod.TObject;',
  25116. '$mod.THelper.Fly.call({',
  25117. ' p: $with2,',
  25118. ' get: function () {',
  25119. ' return this.p.FField;',
  25120. ' },',
  25121. ' set: function (v) {',
  25122. ' $mod.TObject.FField = v;',
  25123. ' }',
  25124. '}, 15);',
  25125. 'var $with3 = $mod.TObject.FField;',
  25126. '$mod.THelper.Fly.call({',
  25127. ' get: function () {',
  25128. ' return $with3;',
  25129. ' },',
  25130. ' set: function (v) {',
  25131. ' $with3 = v;',
  25132. ' }',
  25133. '}, 16);',
  25134. '']));
  25135. end;
  25136. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25137. begin
  25138. StartProgram(false);
  25139. Add([
  25140. '{$modeswitch typehelpers}',
  25141. 'type',
  25142. ' TObject = class',
  25143. ' class var FField: word;',
  25144. ' class function GetField: word; static;',
  25145. ' class property Field: word read GetField write FField;',
  25146. ' end;',
  25147. ' THelper = type helper for word',
  25148. ' procedure Fly(n: byte);',
  25149. ' end;',
  25150. 'class function TObject.GetField: word;',
  25151. 'begin',
  25152. ' Field.Fly(1);',
  25153. ' TObject.Field.Fly(5);',
  25154. ' with TObject do Field.Fly(6);',
  25155. ' with TObject.Field do Fly(7);',
  25156. 'end;',
  25157. 'procedure THelper.Fly(n: byte);',
  25158. 'begin',
  25159. 'end;',
  25160. 'var',
  25161. ' o: TObject;',
  25162. 'begin',
  25163. ' o.Field.Fly(11);',
  25164. ' with o do Field.Fly(12);',
  25165. ' with o.Field do Fly(13);',
  25166. '']);
  25167. ConvertProgram;
  25168. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25169. LinesToStr([ // statements
  25170. 'rtl.createClass(this, "TObject", null, function () {',
  25171. ' this.FField = 0;',
  25172. ' this.$init = function () {',
  25173. ' };',
  25174. ' this.$final = function () {',
  25175. ' };',
  25176. ' this.GetField = function () {',
  25177. ' var Result = 0;',
  25178. ' $mod.THelper.Fly.call({',
  25179. ' p: $mod.TObject.GetField(),',
  25180. ' get: function () {',
  25181. ' return this.p;',
  25182. ' },',
  25183. ' set: function (v) {',
  25184. ' this.p = v;',
  25185. ' }',
  25186. ' }, 1);',
  25187. ' $mod.THelper.Fly.call({',
  25188. ' p: $mod.TObject.GetField(),',
  25189. ' get: function () {',
  25190. ' return this.p;',
  25191. ' },',
  25192. ' set: function (v) {',
  25193. ' this.p = v;',
  25194. ' }',
  25195. ' }, 5);',
  25196. ' var $with = $mod.TObject;',
  25197. ' $mod.THelper.Fly.call({',
  25198. ' p: $with.GetField(),',
  25199. ' get: function () {',
  25200. ' return this.p;',
  25201. ' },',
  25202. ' set: function (v) {',
  25203. ' this.p = v;',
  25204. ' }',
  25205. ' }, 6);',
  25206. ' var $with1 = $mod.TObject.GetField();',
  25207. ' $mod.THelper.Fly.call({',
  25208. ' get: function () {',
  25209. ' return $with1;',
  25210. ' },',
  25211. ' set: function (v) {',
  25212. ' $with1 = v;',
  25213. ' }',
  25214. ' }, 7);',
  25215. ' return Result;',
  25216. ' };',
  25217. '});',
  25218. 'rtl.createHelper(this, "THelper", null, function () {',
  25219. ' this.Fly = function (n) {',
  25220. ' };',
  25221. '});',
  25222. 'this.o = null;',
  25223. '']),
  25224. LinesToStr([ // $mod.$main
  25225. '$mod.THelper.Fly.call({',
  25226. ' p: $mod.TObject.GetField(),',
  25227. ' get: function () {',
  25228. ' return this.p;',
  25229. ' },',
  25230. ' set: function (v) {',
  25231. ' this.p = v;',
  25232. ' }',
  25233. '}, 11);',
  25234. 'var $with = $mod.o;',
  25235. '$mod.THelper.Fly.call({',
  25236. ' p: $with.GetField(),',
  25237. ' get: function () {',
  25238. ' return this.p;',
  25239. ' },',
  25240. ' set: function (v) {',
  25241. ' this.p = v;',
  25242. ' }',
  25243. '}, 12);',
  25244. 'var $with1 = $mod.TObject.GetField();',
  25245. '$mod.THelper.Fly.call({',
  25246. ' get: function () {',
  25247. ' return $with1;',
  25248. ' },',
  25249. ' set: function (v) {',
  25250. ' $with1 = v;',
  25251. ' }',
  25252. '}, 13);',
  25253. '']));
  25254. end;
  25255. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25256. begin
  25257. StartProgram(false);
  25258. Add([
  25259. '{$modeswitch typehelpers}',
  25260. 'type',
  25261. ' TObject = class',
  25262. ' class var FField: word;',
  25263. ' class function GetField: word;',
  25264. ' class property Field: word read GetField write FField;',
  25265. ' end;',
  25266. ' TClass = class of TObject;',
  25267. ' THelper = type helper for word',
  25268. ' procedure Fly(n: byte);',
  25269. ' end;',
  25270. 'class function TObject.GetField: word;',
  25271. 'begin',
  25272. ' Field.Fly(1);',
  25273. ' Self.Field.Fly(5);',
  25274. ' with Self do Field.Fly(6);',
  25275. ' with Self.Field do Fly(7);',
  25276. 'end;',
  25277. 'procedure THelper.Fly(n: byte);',
  25278. 'begin',
  25279. 'end;',
  25280. 'var',
  25281. ' o: TObject;',
  25282. ' c: TClass;',
  25283. 'begin',
  25284. ' o.Field.Fly(11);',
  25285. ' with o do Field.Fly(12);',
  25286. ' with o.Field do Fly(13);',
  25287. ' c.Field.Fly(14);',
  25288. ' with c do Field.Fly(15);',
  25289. ' with c.Field do Fly(16);',
  25290. '']);
  25291. ConvertProgram;
  25292. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25293. LinesToStr([ // statements
  25294. 'rtl.createClass(this, "TObject", null, function () {',
  25295. ' this.FField = 0;',
  25296. ' this.$init = function () {',
  25297. ' };',
  25298. ' this.$final = function () {',
  25299. ' };',
  25300. ' this.GetField = function () {',
  25301. ' var Result = 0;',
  25302. ' $mod.THelper.Fly.call({',
  25303. ' p: this.GetField(),',
  25304. ' get: function () {',
  25305. ' return this.p;',
  25306. ' },',
  25307. ' set: function (v) {',
  25308. ' this.p = v;',
  25309. ' }',
  25310. ' }, 1);',
  25311. ' $mod.THelper.Fly.call({',
  25312. ' p: this.GetField(),',
  25313. ' get: function () {',
  25314. ' return this.p;',
  25315. ' },',
  25316. ' set: function (v) {',
  25317. ' this.p = v;',
  25318. ' }',
  25319. ' }, 5);',
  25320. ' $mod.THelper.Fly.call({',
  25321. ' p: this.GetField(),',
  25322. ' get: function () {',
  25323. ' return this.p;',
  25324. ' },',
  25325. ' set: function (v) {',
  25326. ' this.p = v;',
  25327. ' }',
  25328. ' }, 6);',
  25329. ' var $with = this.GetField();',
  25330. ' $mod.THelper.Fly.call({',
  25331. ' get: function () {',
  25332. ' return $with;',
  25333. ' },',
  25334. ' set: function (v) {',
  25335. ' $with = v;',
  25336. ' }',
  25337. ' }, 7);',
  25338. ' return Result;',
  25339. ' };',
  25340. '});',
  25341. 'rtl.createHelper(this, "THelper", null, function () {',
  25342. ' this.Fly = function (n) {',
  25343. ' };',
  25344. '});',
  25345. 'this.o = null;',
  25346. 'this.c = null;',
  25347. '']),
  25348. LinesToStr([ // $mod.$main
  25349. '$mod.THelper.Fly.call({',
  25350. ' p: $mod.o.$class.GetField(),',
  25351. ' get: function () {',
  25352. ' return this.p;',
  25353. ' },',
  25354. ' set: function (v) {',
  25355. ' this.p = v;',
  25356. ' }',
  25357. '}, 11);',
  25358. 'var $with = $mod.o;',
  25359. '$mod.THelper.Fly.call({',
  25360. ' p: $with.$class.GetField(),',
  25361. ' get: function () {',
  25362. ' return this.p;',
  25363. ' },',
  25364. ' set: function (v) {',
  25365. ' this.p = v;',
  25366. ' }',
  25367. '}, 12);',
  25368. 'var $with1 = $mod.o.$class.GetField();',
  25369. '$mod.THelper.Fly.call({',
  25370. ' get: function () {',
  25371. ' return $with1;',
  25372. ' },',
  25373. ' set: function (v) {',
  25374. ' $with1 = v;',
  25375. ' }',
  25376. '}, 13);',
  25377. '$mod.THelper.Fly.call({',
  25378. ' p: $mod.c.GetField(),',
  25379. ' get: function () {',
  25380. ' return this.p;',
  25381. ' },',
  25382. ' set: function (v) {',
  25383. ' this.p = v;',
  25384. ' }',
  25385. '}, 14);',
  25386. 'var $with2 = $mod.c;',
  25387. '$mod.THelper.Fly.call({',
  25388. ' p: $with2.GetField(),',
  25389. ' get: function () {',
  25390. ' return this.p;',
  25391. ' },',
  25392. ' set: function (v) {',
  25393. ' this.p = v;',
  25394. ' }',
  25395. '}, 15);',
  25396. 'var $with3 = $mod.c.GetField();',
  25397. '$mod.THelper.Fly.call({',
  25398. ' get: function () {',
  25399. ' return $with3;',
  25400. ' },',
  25401. ' set: function (v) {',
  25402. ' $with3 = v;',
  25403. ' }',
  25404. '}, 16);',
  25405. '']));
  25406. end;
  25407. procedure TTestModule.TestTypeHelper_Property;
  25408. begin
  25409. StartProgram(false);
  25410. Add([
  25411. '{$modeswitch typehelpers}',
  25412. 'type',
  25413. ' THelper = type helper for word',
  25414. ' function GetSize: longint;',
  25415. ' procedure SetSize(Value: longint);',
  25416. ' property Size: longint read GetSize write SetSize;',
  25417. ' end;',
  25418. 'function THelper.GetSize: longint;',
  25419. 'begin',
  25420. ' Result:=Size+1;',
  25421. ' Size:=2;',
  25422. ' Result:=Self.Size+3;',
  25423. ' Self.Size:=4;',
  25424. ' with Self do begin',
  25425. ' Result:=Size+5;',
  25426. ' Size:=6;',
  25427. ' end;',
  25428. 'end;',
  25429. 'procedure THelper.SetSize(Value: longint);',
  25430. 'begin',
  25431. 'end;',
  25432. 'var w: word;',
  25433. 'begin',
  25434. ' w:=w.Size+7;',
  25435. ' w.Size:=w+8;',
  25436. ' with w do begin',
  25437. ' w:=Size+9;',
  25438. ' Size:=w+10;',
  25439. ' end;',
  25440. '']);
  25441. ConvertProgram;
  25442. CheckSource('TestTypeHelper_Property',
  25443. LinesToStr([ // statements
  25444. 'rtl.createHelper(this, "THelper", null, function () {',
  25445. ' this.GetSize = function () {',
  25446. ' var Result = 0;',
  25447. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25448. ' $mod.THelper.SetSize.call(this, 2);',
  25449. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25450. ' $mod.THelper.SetSize.call(this, 4);',
  25451. ' var $with = this.get();',
  25452. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25453. ' $mod.THelper.SetSize.call(this, 6);',
  25454. ' return Result;',
  25455. ' };',
  25456. ' this.SetSize = function (Value) {',
  25457. ' };',
  25458. '});',
  25459. 'this.w = 0;',
  25460. '']),
  25461. LinesToStr([ // $mod.$main
  25462. '$mod.w = $mod.THelper.GetSize.call({',
  25463. ' p: $mod,',
  25464. ' get: function () {',
  25465. ' return this.p.w;',
  25466. ' },',
  25467. ' set: function (v) {',
  25468. ' this.p.w = v;',
  25469. ' }',
  25470. '}) + 7;',
  25471. '$mod.THelper.SetSize.call({',
  25472. ' p: $mod,',
  25473. ' get: function () {',
  25474. ' return this.p.w;',
  25475. ' },',
  25476. ' set: function (v) {',
  25477. ' this.p.w = v;',
  25478. ' }',
  25479. '}, $mod.w + 8);',
  25480. 'var $with = $mod.w;',
  25481. '$mod.w = $mod.THelper.GetSize.call({',
  25482. ' get: function () {',
  25483. ' return $with;',
  25484. ' },',
  25485. ' set: function (v) {',
  25486. ' $with = v;',
  25487. ' }',
  25488. '}) + 9;',
  25489. '$mod.THelper.SetSize.call({',
  25490. ' get: function () {',
  25491. ' return $with;',
  25492. ' },',
  25493. ' set: function (v) {',
  25494. ' $with = v;',
  25495. ' }',
  25496. '}, $mod.w + 10);',
  25497. '']));
  25498. end;
  25499. procedure TTestModule.TestTypeHelper_Property_Array;
  25500. begin
  25501. StartProgram(false);
  25502. Add([
  25503. '{$modeswitch typehelpers}',
  25504. 'type',
  25505. ' THelper = type helper for word',
  25506. ' function GetItems(Index: byte): boolean;',
  25507. ' procedure SetItems(Index: byte; Value: boolean);',
  25508. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  25509. ' end;',
  25510. 'function THelper.GetItems(Index: byte): boolean;',
  25511. 'begin',
  25512. ' Result:=Items[1];',
  25513. ' Items[2]:=false;',
  25514. ' Result:=Self.Items[3];',
  25515. ' Self.Items[4]:=true;',
  25516. ' with Self do begin',
  25517. ' Result:=Items[5];',
  25518. ' Items[6]:=false;',
  25519. ' end;',
  25520. 'end;',
  25521. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  25522. 'begin',
  25523. 'end;',
  25524. 'var',
  25525. ' w: word;',
  25526. ' b: boolean;',
  25527. 'begin',
  25528. ' b:=w.Items[1];',
  25529. ' w.Items[2]:=b;',
  25530. ' with w do begin',
  25531. ' b:=Items[3];',
  25532. ' Items[4]:=b;',
  25533. ' end;',
  25534. '']);
  25535. ConvertProgram;
  25536. CheckSource('TestTypeHelper_Property_Array',
  25537. LinesToStr([ // statements
  25538. 'rtl.createHelper(this, "THelper", null, function () {',
  25539. ' this.GetItems = function (Index) {',
  25540. ' var Result = false;',
  25541. ' Result = $mod.THelper.GetItems.call(this, 1);',
  25542. ' $mod.THelper.SetItems.call(this, 2, false);',
  25543. ' Result = $mod.THelper.GetItems.call(this, 3);',
  25544. ' $mod.THelper.SetItems.call(this, 4, true);',
  25545. ' var $with = this.get();',
  25546. ' Result = $mod.THelper.GetItems.call(this, 5);',
  25547. ' $mod.THelper.SetItems.call(this, 6, false);',
  25548. ' return Result;',
  25549. ' };',
  25550. ' this.SetItems = function (Index, Value) {',
  25551. ' };',
  25552. '});',
  25553. 'this.w = 0;',
  25554. 'this.b = false;',
  25555. '']),
  25556. LinesToStr([ // $mod.$main
  25557. '$mod.b = $mod.THelper.GetItems.call({',
  25558. ' p: $mod,',
  25559. ' get: function () {',
  25560. ' return this.p.w;',
  25561. ' },',
  25562. ' set: function (v) {',
  25563. ' this.p.w = v;',
  25564. ' }',
  25565. '}, 1);',
  25566. '$mod.THelper.SetItems.call({',
  25567. ' p: $mod,',
  25568. ' get: function () {',
  25569. ' return this.p.w;',
  25570. ' },',
  25571. ' set: function (v) {',
  25572. ' this.p.w = v;',
  25573. ' }',
  25574. '}, 2, $mod.b);',
  25575. 'var $with = $mod.w;',
  25576. '$mod.b = $mod.THelper.GetItems.call({',
  25577. ' get: function () {',
  25578. ' return $with;',
  25579. ' },',
  25580. ' set: function (v) {',
  25581. ' $with = v;',
  25582. ' }',
  25583. '}, 3);',
  25584. '$mod.THelper.SetItems.call({',
  25585. ' get: function () {',
  25586. ' return $with;',
  25587. ' },',
  25588. ' set: function (v) {',
  25589. ' $with = v;',
  25590. ' }',
  25591. '}, 4, $mod.b);',
  25592. '']));
  25593. end;
  25594. procedure TTestModule.TestTypeHelper_ClassProperty;
  25595. begin
  25596. StartProgram(false);
  25597. Add([
  25598. '{$modeswitch typehelpers}',
  25599. 'type',
  25600. ' THelper = type helper for word',
  25601. ' class function GetSize: longint; static;',
  25602. ' class procedure SetSize(Value: longint); static;',
  25603. ' class property Size: longint read GetSize write SetSize;',
  25604. ' end;',
  25605. 'class function THelper.GetSize: longint;',
  25606. 'begin',
  25607. ' Result:=Size+1;',
  25608. ' Size:=2;',
  25609. 'end;',
  25610. 'class procedure THelper.SetSize(Value: longint);',
  25611. 'begin',
  25612. 'end;',
  25613. 'begin',
  25614. '']);
  25615. ConvertProgram;
  25616. CheckSource('TestTypeHelper_ClassProperty',
  25617. LinesToStr([ // statements
  25618. 'rtl.createHelper(this, "THelper", null, function () {',
  25619. ' this.GetSize = function () {',
  25620. ' var Result = 0;',
  25621. ' Result = $mod.THelper.GetSize() + 1;',
  25622. ' $mod.THelper.SetSize(2);',
  25623. ' return Result;',
  25624. ' };',
  25625. ' this.SetSize = function (Value) {',
  25626. ' };',
  25627. '});',
  25628. '']),
  25629. LinesToStr([ // $mod.$main
  25630. '']));
  25631. end;
  25632. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  25633. begin
  25634. StartProgram(false);
  25635. Add([
  25636. '{$modeswitch typehelpers}',
  25637. 'type',
  25638. ' THelper = type helper for word',
  25639. ' class function GetItems(Index: byte): boolean; static;',
  25640. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  25641. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  25642. ' end;',
  25643. 'class function THelper.GetItems(Index: byte): boolean;',
  25644. 'begin',
  25645. ' Result:=Items[1];',
  25646. ' Items[2]:=false;',
  25647. 'end;',
  25648. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  25649. 'begin',
  25650. 'end;',
  25651. 'var',
  25652. ' w: word;',
  25653. ' b: boolean;',
  25654. 'begin',
  25655. ' b:=w.Items[1];',
  25656. ' w.Items[2]:=b;',
  25657. ' with w do begin',
  25658. ' b:=Items[3];',
  25659. ' Items[4]:=b;',
  25660. ' end;',
  25661. '']);
  25662. ConvertProgram;
  25663. CheckSource('TestTypeHelper_ClassProperty_Array',
  25664. LinesToStr([ // statements
  25665. 'rtl.createHelper(this, "THelper", null, function () {',
  25666. ' this.GetItems = function (Index) {',
  25667. ' var Result = false;',
  25668. ' Result = $mod.THelper.GetItems(1);',
  25669. ' $mod.THelper.SetItems(2, false);',
  25670. ' return Result;',
  25671. ' };',
  25672. ' this.SetItems = function (Index, Value) {',
  25673. ' };',
  25674. '});',
  25675. 'this.w = 0;',
  25676. 'this.b = false;',
  25677. '']),
  25678. LinesToStr([ // $mod.$main
  25679. '$mod.b = $mod.THelper.GetItems(1);',
  25680. '$mod.THelper.SetItems(2, $mod.b);',
  25681. 'var $with = $mod.w;',
  25682. '$mod.b = $mod.THelper.GetItems(3);',
  25683. '$mod.THelper.SetItems(4, $mod.b);',
  25684. '']));
  25685. end;
  25686. procedure TTestModule.TestTypeHelper_ClassMethod;
  25687. begin
  25688. StartProgram(false);
  25689. Add([
  25690. '{$modeswitch typehelpers}',
  25691. 'type',
  25692. ' THelper = type helper for word',
  25693. ' class procedure DoStatic; static;',
  25694. ' end;',
  25695. 'class procedure THelper.DoStatic;',
  25696. 'begin',
  25697. ' DoStatic;',
  25698. ' DoStatic();',
  25699. 'end;',
  25700. 'var w: word;',
  25701. 'begin',
  25702. ' w.DoStatic;',
  25703. ' w.DoStatic();',
  25704. '']);
  25705. ConvertProgram;
  25706. CheckSource('TestTypeHelper_ClassMethod',
  25707. LinesToStr([ // statements
  25708. 'rtl.createHelper(this, "THelper", null, function () {',
  25709. ' this.DoStatic = function () {',
  25710. ' $mod.THelper.DoStatic();',
  25711. ' $mod.THelper.DoStatic();',
  25712. ' };',
  25713. '});',
  25714. 'this.w = 0;',
  25715. '']),
  25716. LinesToStr([ // $mod.$main
  25717. '$mod.THelper.DoStatic();',
  25718. '$mod.THelper.DoStatic();',
  25719. '']));
  25720. end;
  25721. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  25722. begin
  25723. StartProgram(false);
  25724. Add([
  25725. '{$modeswitch typehelpers}',
  25726. 'type',
  25727. ' THelper = type helper for word',
  25728. ' procedure Run; external name ''Run'';',
  25729. ' end;',
  25730. 'var w: word;',
  25731. 'begin',
  25732. ' w.Run;',
  25733. '']);
  25734. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  25735. ConvertProgram;
  25736. end;
  25737. procedure TTestModule.TestTypeHelper_Constructor;
  25738. begin
  25739. StartProgram(false);
  25740. Add([
  25741. '{$modeswitch typehelpers}',
  25742. 'type',
  25743. ' THelper = type helper for word',
  25744. ' constructor Init(e: longint);',
  25745. ' end;',
  25746. 'constructor THelper.Init(e: longint);',
  25747. 'begin',
  25748. ' Self:=e;',
  25749. ' Init(e+1);',
  25750. 'end;',
  25751. 'var w: word;',
  25752. 'begin',
  25753. ' w:=word.Init(2);',
  25754. ' w:=w.Init(3);',
  25755. ' with word do w:=Init(4);',
  25756. ' with w do w:=Init(5);',
  25757. '']);
  25758. ConvertProgram;
  25759. CheckSource('TestTypeHelper_Constructor',
  25760. LinesToStr([ // statements
  25761. 'rtl.createHelper(this, "THelper", null, function () {',
  25762. ' this.Init = function (e) {',
  25763. ' this.set(e);',
  25764. ' $mod.THelper.Init.call(this, e + 1);',
  25765. ' return this.get();',
  25766. ' };',
  25767. ' this.$new = function (fn, args) {',
  25768. ' return this[fn].apply({',
  25769. ' p: 0,',
  25770. ' get: function () {',
  25771. ' return this.p;',
  25772. ' },',
  25773. ' set: function (v) {',
  25774. ' this.p = v;',
  25775. ' }',
  25776. ' }, args);',
  25777. ' };',
  25778. '});',
  25779. 'this.w = 0;',
  25780. '']),
  25781. LinesToStr([ // $mod.$main
  25782. '$mod.w = $mod.THelper.$new("Init", [2]);',
  25783. '$mod.w = $mod.THelper.Init.call({',
  25784. ' p: $mod,',
  25785. ' get: function () {',
  25786. ' return this.p.w;',
  25787. ' },',
  25788. ' set: function (v) {',
  25789. ' this.p.w = v;',
  25790. ' }',
  25791. '}, 3);',
  25792. '$mod.w = $mod.THelper.$new("Init", [4]);',
  25793. 'var $with = $mod.w;',
  25794. '$mod.w = $mod.THelper.Init.call({',
  25795. ' get: function () {',
  25796. ' return $with;',
  25797. ' },',
  25798. ' set: function (v) {',
  25799. ' $with = v;',
  25800. ' }',
  25801. '}, 5);',
  25802. '']));
  25803. end;
  25804. procedure TTestModule.TestTypeHelper_Word;
  25805. begin
  25806. StartProgram(false);
  25807. Add([
  25808. '{$modeswitch typehelpers}',
  25809. 'type',
  25810. ' THelper = type helper for word',
  25811. ' procedure DoIt(e: byte = 123);',
  25812. ' end;',
  25813. 'procedure THelper.DoIt(e: byte);',
  25814. 'begin',
  25815. ' Self:=e;',
  25816. ' Self:=Self+1;',
  25817. ' with Self do Doit;',
  25818. 'end;',
  25819. 'begin',
  25820. ' word(3).DoIt;',
  25821. '']);
  25822. ConvertProgram;
  25823. CheckSource('TestTypeHelper_Word',
  25824. LinesToStr([ // statements
  25825. 'rtl.createHelper(this, "THelper", null, function () {',
  25826. ' this.DoIt = function (e) {',
  25827. ' this.set(e);',
  25828. ' this.set(this.get() + 1);',
  25829. ' var $with = this.get();',
  25830. ' $mod.THelper.DoIt.call(this, 123);',
  25831. ' };',
  25832. '});',
  25833. '']),
  25834. LinesToStr([ // $mod.$main
  25835. '$mod.THelper.DoIt.call({',
  25836. ' get: function () {',
  25837. ' return 3;',
  25838. ' },',
  25839. ' set: function (v) {',
  25840. ' rtl.raiseE("EPropReadOnly");',
  25841. ' }',
  25842. '}, 123);',
  25843. '']));
  25844. end;
  25845. procedure TTestModule.TestTypeHelper_Boolean;
  25846. begin
  25847. StartProgram(false);
  25848. Add([
  25849. '{$modeswitch typehelpers}',
  25850. 'type',
  25851. ' Integer = longint;',
  25852. ' THelper = type helper for boolean',
  25853. ' procedure Run(e: wordbool = true);',
  25854. ' end;',
  25855. 'procedure THelper.Run(e: wordbool);',
  25856. 'begin',
  25857. ' Self:=e;',
  25858. ' Self:=not Self;',
  25859. ' with Self do Run;',
  25860. ' if Integer(Self)=0 then ;',
  25861. 'end;',
  25862. 'begin',
  25863. ' boolean(3).Run;',
  25864. '']);
  25865. ConvertProgram;
  25866. CheckSource('TestTypeHelper_Boolean',
  25867. LinesToStr([ // statements
  25868. 'rtl.createHelper(this, "THelper", null, function () {',
  25869. ' this.Run = function (e) {',
  25870. ' this.set(e);',
  25871. ' this.set(!this.get());',
  25872. ' var $with = this.get();',
  25873. ' $mod.THelper.Run.call(this, true);',
  25874. ' if ((this.get() ? 1 : 0) === 0) ;',
  25875. ' };',
  25876. '});',
  25877. '']),
  25878. LinesToStr([ // $mod.$main
  25879. '$mod.THelper.Run.call({',
  25880. ' a: 3 != 0,',
  25881. ' get: function () {',
  25882. ' return this.a;',
  25883. ' },',
  25884. ' set: function (v) {',
  25885. ' rtl.raiseE("EPropReadOnly");',
  25886. ' }',
  25887. '}, true);',
  25888. '']));
  25889. end;
  25890. procedure TTestModule.TestTypeHelper_WordBool;
  25891. begin
  25892. StartProgram(false);
  25893. Add([
  25894. '{$modeswitch typehelpers}',
  25895. 'type',
  25896. ' Integer = longint;',
  25897. ' THelper = type helper for WordBool',
  25898. ' procedure Run(e: wordbool = true);',
  25899. ' end;',
  25900. 'procedure THelper.Run(e: wordbool);',
  25901. 'var i: integer;',
  25902. 'begin',
  25903. ' i:=Integer(Self);',
  25904. 'end;',
  25905. 'var w: wordbool;',
  25906. 'begin',
  25907. ' w.Run;',
  25908. ' wordbool(3).Run;',
  25909. '']);
  25910. ConvertProgram;
  25911. CheckSource('TestTypeHelper_WordBool',
  25912. LinesToStr([ // statements
  25913. 'rtl.createHelper(this, "THelper", null, function () {',
  25914. ' this.Run = function (e) {',
  25915. ' var i = 0;',
  25916. ' i = (this.get() ? 1 : 0);',
  25917. ' };',
  25918. '});',
  25919. 'this.w = false;',
  25920. '']),
  25921. LinesToStr([ // $mod.$main
  25922. '$mod.THelper.Run.call({',
  25923. ' p: $mod,',
  25924. ' get: function () {',
  25925. ' return this.p.w;',
  25926. ' },',
  25927. ' set: function (v) {',
  25928. ' this.p.w = v;',
  25929. ' }',
  25930. '}, true);',
  25931. '$mod.THelper.Run.call({',
  25932. ' a: 3 != 0,',
  25933. ' get: function () {',
  25934. ' return this.a;',
  25935. ' },',
  25936. ' set: function (v) {',
  25937. ' rtl.raiseE("EPropReadOnly");',
  25938. ' }',
  25939. '}, true);',
  25940. '']));
  25941. end;
  25942. procedure TTestModule.TestTypeHelper_Double;
  25943. begin
  25944. StartProgram(false);
  25945. Add([
  25946. '{$modeswitch typehelpers}',
  25947. 'type',
  25948. ' Float = type double;',
  25949. ' THelper = type helper for Float',
  25950. ' const NPI = 3.141592;',
  25951. ' function ToStr: String;',
  25952. ' end;',
  25953. 'function THelper.ToStr: String;',
  25954. 'begin',
  25955. 'end;',
  25956. 'procedure DoIt(s: string);',
  25957. 'begin',
  25958. 'end;',
  25959. 'var f: Float;',
  25960. 'begin',
  25961. ' DoIt(f.toStr);',
  25962. ' DoIt(f.toStr());',
  25963. ' (f*f).toStr;',
  25964. ' DoIt((f*f).toStr);',
  25965. '']);
  25966. ConvertProgram;
  25967. CheckSource('TestTypeHelper_Double',
  25968. LinesToStr([ // statements
  25969. 'rtl.createHelper(this, "THelper", null, function () {',
  25970. ' this.NPI = 3.141592;',
  25971. ' this.ToStr = function () {',
  25972. ' var Result = "";',
  25973. ' return Result;',
  25974. ' };',
  25975. '});',
  25976. 'this.DoIt = function (s) {',
  25977. '};',
  25978. 'this.f = 0.0;',
  25979. '']),
  25980. LinesToStr([ // $mod.$main
  25981. '$mod.DoIt($mod.THelper.ToStr.call({',
  25982. ' p: $mod,',
  25983. ' get: function () {',
  25984. ' return this.p.f;',
  25985. ' },',
  25986. ' set: function (v) {',
  25987. ' this.p.f = v;',
  25988. ' }',
  25989. '}));',
  25990. '$mod.DoIt($mod.THelper.ToStr.call({',
  25991. ' p: $mod,',
  25992. ' get: function () {',
  25993. ' return this.p.f;',
  25994. ' },',
  25995. ' set: function (v) {',
  25996. ' this.p.f = v;',
  25997. ' }',
  25998. '}));',
  25999. '$mod.THelper.ToStr.call({',
  26000. ' a: $mod.f * $mod.f,',
  26001. ' get: function () {',
  26002. ' return this.a;',
  26003. ' },',
  26004. ' set: function (v) {',
  26005. ' rtl.raiseE("EPropReadOnly");',
  26006. ' }',
  26007. '});',
  26008. '$mod.DoIt($mod.THelper.ToStr.call({',
  26009. ' a: $mod.f * $mod.f,',
  26010. ' get: function () {',
  26011. ' return this.a;',
  26012. ' },',
  26013. ' set: function (v) {',
  26014. ' rtl.raiseE("EPropReadOnly");',
  26015. ' }',
  26016. '}));',
  26017. '']));
  26018. end;
  26019. procedure TTestModule.TestTypeHelper_NativeInt;
  26020. begin
  26021. StartProgram(false);
  26022. Add([
  26023. '{$modeswitch typehelpers}',
  26024. 'type',
  26025. ' MaxInt = type nativeint;',
  26026. ' THelperI = type helper for MaxInt',
  26027. ' function ToStr: String;',
  26028. ' end;',
  26029. ' MaxUInt = type nativeuint;',
  26030. ' THelperU = type helper for MaxUInt',
  26031. ' function ToStr: String;',
  26032. ' end;',
  26033. 'function THelperI.ToStr: String;',
  26034. 'begin',
  26035. ' Result:=str(Self);',
  26036. 'end;',
  26037. 'function THelperU.ToStr: String;',
  26038. 'begin',
  26039. ' Result:=str(Self);',
  26040. 'end;',
  26041. 'procedure DoIt(s: string);',
  26042. 'begin',
  26043. 'end;',
  26044. 'var i: MaxInt;',
  26045. 'begin',
  26046. ' DoIt(i.toStr);',
  26047. ' DoIt(i.toStr());',
  26048. ' (i*i).toStr;',
  26049. ' DoIt((i*i).toStr);',
  26050. '']);
  26051. ConvertProgram;
  26052. CheckSource('TestTypeHelper_NativeInt',
  26053. LinesToStr([ // statements
  26054. 'rtl.createHelper(this, "THelperI", null, function () {',
  26055. ' this.ToStr = function () {',
  26056. ' var Result = "";',
  26057. ' Result = "" + this.get();',
  26058. ' return Result;',
  26059. ' };',
  26060. '});',
  26061. 'rtl.createHelper(this, "THelperU", null, function () {',
  26062. ' this.ToStr = function () {',
  26063. ' var Result = "";',
  26064. ' Result = "" + this.get();',
  26065. ' return Result;',
  26066. ' };',
  26067. '});',
  26068. 'this.DoIt = function (s) {',
  26069. '};',
  26070. 'this.i = 0;',
  26071. '']),
  26072. LinesToStr([ // $mod.$main
  26073. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26074. ' p: $mod,',
  26075. ' get: function () {',
  26076. ' return this.p.i;',
  26077. ' },',
  26078. ' set: function (v) {',
  26079. ' this.p.i = v;',
  26080. ' }',
  26081. '}));',
  26082. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26083. ' p: $mod,',
  26084. ' get: function () {',
  26085. ' return this.p.i;',
  26086. ' },',
  26087. ' set: function (v) {',
  26088. ' this.p.i = v;',
  26089. ' }',
  26090. '}));',
  26091. '$mod.THelperI.ToStr.call({',
  26092. ' a: $mod.i * $mod.i,',
  26093. ' get: function () {',
  26094. ' return this.a;',
  26095. ' },',
  26096. ' set: function (v) {',
  26097. ' rtl.raiseE("EPropReadOnly");',
  26098. ' }',
  26099. '});',
  26100. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26101. ' a: $mod.i * $mod.i,',
  26102. ' get: function () {',
  26103. ' return this.a;',
  26104. ' },',
  26105. ' set: function (v) {',
  26106. ' rtl.raiseE("EPropReadOnly");',
  26107. ' }',
  26108. '}));',
  26109. '']));
  26110. end;
  26111. procedure TTestModule.TestTypeHelper_StringChar;
  26112. begin
  26113. StartProgram(false);
  26114. Add([
  26115. '{$modeswitch typehelpers}',
  26116. 'type',
  26117. ' TStringHelper = type helper for string',
  26118. ' procedure DoIt(e: byte = 123);',
  26119. ' end;',
  26120. ' TCharHelper = type helper for char',
  26121. ' procedure Fly;',
  26122. ' end;',
  26123. 'procedure TStringHelper.DoIt(e: byte);',
  26124. 'begin',
  26125. ' Self[1]:=''c'';',
  26126. ' Self[2]:=Self[3];',
  26127. 'end;',
  26128. 'procedure TCharHelper.Fly;',
  26129. 'begin',
  26130. ' Self:=''c'';',
  26131. 'end;',
  26132. 'begin',
  26133. ' ''abc''.DoIt;',
  26134. ' ''xyz''.DoIt();',
  26135. ' ''c''.Fly();',
  26136. '']);
  26137. ConvertProgram;
  26138. CheckSource('TestTypeHelper_StringChar',
  26139. LinesToStr([ // statements
  26140. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26141. ' this.DoIt = function (e) {',
  26142. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26143. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26144. ' };',
  26145. '});',
  26146. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26147. ' this.Fly = function () {',
  26148. ' this.set("c");',
  26149. ' };',
  26150. '});',
  26151. '']),
  26152. LinesToStr([ // $mod.$main
  26153. '$mod.TStringHelper.DoIt.call({',
  26154. ' get: function () {',
  26155. ' return "abc";',
  26156. ' },',
  26157. ' set: function (v) {',
  26158. ' rtl.raiseE("EPropReadOnly");',
  26159. ' }',
  26160. '}, 123);',
  26161. '$mod.TStringHelper.DoIt.call({',
  26162. ' get: function () {',
  26163. ' return "xyz";',
  26164. ' },',
  26165. ' set: function (v) {',
  26166. ' rtl.raiseE("EPropReadOnly");',
  26167. ' }',
  26168. '}, 123);',
  26169. '$mod.TCharHelper.Fly.call({',
  26170. ' get: function () {',
  26171. ' return "c";',
  26172. ' },',
  26173. ' set: function (v) {',
  26174. ' rtl.raiseE("EPropReadOnly");',
  26175. ' }',
  26176. '});',
  26177. '']));
  26178. end;
  26179. procedure TTestModule.TestTypeHelper_JSValue;
  26180. begin
  26181. StartProgram(false);
  26182. Add([
  26183. '{$modeswitch typehelpers}',
  26184. 'type',
  26185. ' TExtValue = type jsvalue;',
  26186. ' THelper = type helper for TExtValue',
  26187. ' function ToStr: String;',
  26188. ' end;',
  26189. 'function THelper.ToStr: String;',
  26190. 'begin',
  26191. 'end;',
  26192. 'var',
  26193. ' s: string;',
  26194. ' v: TExtValue;',
  26195. 'begin',
  26196. ' s:=v.toStr;',
  26197. ' s:=v.toStr();',
  26198. ' TExtValue(s).toStr;',
  26199. '']);
  26200. ConvertProgram;
  26201. CheckSource('TestTypeHelper_JSValue',
  26202. LinesToStr([ // statements
  26203. 'rtl.createHelper(this, "THelper", null, function () {',
  26204. ' this.ToStr = function () {',
  26205. ' var Result = "";',
  26206. ' return Result;',
  26207. ' };',
  26208. '});',
  26209. 'this.s = "";',
  26210. 'this.v = undefined;',
  26211. '']),
  26212. LinesToStr([ // $mod.$main
  26213. '$mod.s = $mod.THelper.ToStr.call({',
  26214. ' p: $mod,',
  26215. ' get: function () {',
  26216. ' return this.p.v;',
  26217. ' },',
  26218. ' set: function (v) {',
  26219. ' this.p.v = v;',
  26220. ' }',
  26221. '});',
  26222. '$mod.s = $mod.THelper.ToStr.call({',
  26223. ' p: $mod,',
  26224. ' get: function () {',
  26225. ' return this.p.v;',
  26226. ' },',
  26227. ' set: function (v) {',
  26228. ' this.p.v = v;',
  26229. ' }',
  26230. '});',
  26231. '$mod.THelper.ToStr.call({',
  26232. ' p: $mod,',
  26233. ' get: function () {',
  26234. ' return this.p.s;',
  26235. ' },',
  26236. ' set: function (v) {',
  26237. ' rtl.raiseE("EPropReadOnly");',
  26238. ' }',
  26239. '});',
  26240. '']));
  26241. end;
  26242. procedure TTestModule.TestTypeHelper_Array;
  26243. begin
  26244. StartProgram(false);
  26245. Add([
  26246. '{$modeswitch typehelpers}',
  26247. 'type',
  26248. ' TArrOfBool = array of boolean;',
  26249. ' TArrOfJS = array of jsvalue;',
  26250. ' THelper = type helper for TArrOfBool',
  26251. ' procedure DoIt(e: byte = 123);',
  26252. ' end;',
  26253. 'procedure THelper.DoIt(e: byte);',
  26254. 'begin',
  26255. ' Self[1]:=true;',
  26256. ' Self[2]:=not Self[3];',
  26257. ' SetLength(Self,4);',
  26258. 'end;',
  26259. 'var',
  26260. ' b: TArrOfBool;',
  26261. ' j: TArrOfJS;',
  26262. 'begin',
  26263. ' b.DoIt;',
  26264. ' TArrOfBool(j).DoIt();',
  26265. '']);
  26266. ConvertProgram;
  26267. CheckSource('TestTypeHelper_Array',
  26268. LinesToStr([ // statements
  26269. 'rtl.createHelper(this, "THelper", null, function () {',
  26270. ' this.DoIt = function (e) {',
  26271. ' this.get()[1] = true;',
  26272. ' this.get()[2] = !this.get()[3];',
  26273. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26274. ' };',
  26275. '});',
  26276. 'this.b = [];',
  26277. 'this.j = [];',
  26278. '']),
  26279. LinesToStr([ // $mod.$main
  26280. '$mod.THelper.DoIt.call({',
  26281. ' p: $mod,',
  26282. ' get: function () {',
  26283. ' return this.p.b;',
  26284. ' },',
  26285. ' set: function (v) {',
  26286. ' this.p.b = v;',
  26287. ' }',
  26288. '}, 123);',
  26289. '$mod.THelper.DoIt.call({',
  26290. ' p: $mod,',
  26291. ' get: function () {',
  26292. ' return this.p.j;',
  26293. ' },',
  26294. ' set: function (v) {',
  26295. ' this.p.j = v;',
  26296. ' }',
  26297. '}, 123);',
  26298. '']));
  26299. end;
  26300. procedure TTestModule.TestTypeHelper_EnumType;
  26301. begin
  26302. StartProgram(false);
  26303. Add([
  26304. '{$modeswitch typehelpers}',
  26305. 'type',
  26306. ' TEnum = (red,blue);',
  26307. ' THelper = type helper for TEnum',
  26308. ' procedure DoIt(e: byte = 123);',
  26309. ' class procedure Swing(w: word); static;',
  26310. ' end;',
  26311. 'procedure THelper.DoIt(e: byte);',
  26312. 'begin',
  26313. ' Self:=red;',
  26314. ' Self:=succ(Self);',
  26315. ' with Self do Doit;',
  26316. 'end;',
  26317. 'class procedure THelper.Swing(w: word);',
  26318. 'begin',
  26319. 'end;',
  26320. 'var e: TEnum;',
  26321. 'begin',
  26322. ' e.DoIt;',
  26323. ' red.DoIt;',
  26324. ' TEnum.blue.DoIt;',
  26325. ' TEnum(1).DoIt;',
  26326. ' TEnum.Swing(3);',
  26327. '']);
  26328. ConvertProgram;
  26329. CheckSource('TestTypeHelper_EnumType',
  26330. LinesToStr([ // statements
  26331. 'this.TEnum = {',
  26332. ' "0": "red",',
  26333. ' red: 0,',
  26334. ' "1": "blue",',
  26335. ' blue: 1',
  26336. '};',
  26337. 'rtl.createHelper(this, "THelper", null, function () {',
  26338. ' this.DoIt = function (e) {',
  26339. ' this.set($mod.TEnum.red);',
  26340. ' this.set(this.get() + 1);',
  26341. ' var $with = this.get();',
  26342. ' $mod.THelper.DoIt.call(this, 123);',
  26343. ' };',
  26344. ' this.Swing = function (w) {',
  26345. ' };',
  26346. '});',
  26347. 'this.e = 0;',
  26348. '']),
  26349. LinesToStr([ // $mod.$main
  26350. '$mod.THelper.DoIt.call({',
  26351. ' p: $mod,',
  26352. ' get: function () {',
  26353. ' return this.p.e;',
  26354. ' },',
  26355. ' set: function (v) {',
  26356. ' this.p.e = v;',
  26357. ' }',
  26358. '}, 123);',
  26359. '$mod.THelper.DoIt.call({',
  26360. ' p: $mod.TEnum,',
  26361. ' get: function () {',
  26362. ' return this.p.red;',
  26363. ' },',
  26364. ' set: function (v) {',
  26365. ' rtl.raiseE("EPropReadOnly");',
  26366. ' }',
  26367. '}, 123);',
  26368. '$mod.THelper.DoIt.call({',
  26369. ' p: $mod.TEnum,',
  26370. ' get: function () {',
  26371. ' return this.p.blue;',
  26372. ' },',
  26373. ' set: function (v) {',
  26374. ' rtl.raiseE("EPropReadOnly");',
  26375. ' }',
  26376. '}, 123);',
  26377. '$mod.THelper.DoIt.call({',
  26378. ' get: function () {',
  26379. ' return 1;',
  26380. ' },',
  26381. ' set: function (v) {',
  26382. ' rtl.raiseE("EPropReadOnly");',
  26383. ' }',
  26384. '}, 123);',
  26385. '$mod.THelper.Swing(3);',
  26386. '']));
  26387. end;
  26388. procedure TTestModule.TestTypeHelper_SetType;
  26389. begin
  26390. StartProgram(false);
  26391. Add([
  26392. '{$modeswitch typehelpers}',
  26393. 'type',
  26394. ' TEnum = (red,blue);',
  26395. ' TSetOfEnum = set of TEnum;',
  26396. ' THelper = type helper for TSetOfEnum',
  26397. ' procedure DoIt(e: byte = 123);',
  26398. ' constructor Init(e: TEnum);',
  26399. ' constructor InitEmpty;',
  26400. ' end;',
  26401. 'procedure THelper.DoIt(e: byte);',
  26402. 'begin',
  26403. ' Self:=[];',
  26404. ' Self:=[red];',
  26405. ' Include(Self,blue);',
  26406. 'end;',
  26407. 'constructor THelper.Init(e: TEnum);',
  26408. 'begin',
  26409. ' Self:=[];',
  26410. ' Self:=[e];',
  26411. ' Include(Self,blue);',
  26412. 'end;',
  26413. 'constructor THelper.InitEmpty;',
  26414. 'begin',
  26415. 'end;',
  26416. 'var s: TSetOfEnum;',
  26417. 'begin',
  26418. ' s.DoIt;',
  26419. //' [red].DoIt;',
  26420. //' with s do DoIt;',
  26421. //' with [red,blue] do DoIt;',
  26422. ' s:=TSetOfEnum.Init(blue);',
  26423. ' s:=s.Init(blue);',
  26424. '']);
  26425. ConvertProgram;
  26426. CheckSource('TestTypeHelper_SetType',
  26427. LinesToStr([ // statements
  26428. 'this.TEnum = {',
  26429. ' "0": "red",',
  26430. ' red: 0,',
  26431. ' "1": "blue",',
  26432. ' blue: 1',
  26433. '};',
  26434. 'rtl.createHelper(this, "THelper", null, function () {',
  26435. ' this.DoIt = function (e) {',
  26436. ' this.set({});',
  26437. ' this.set(rtl.createSet($mod.TEnum.red));',
  26438. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26439. ' };',
  26440. ' this.Init = function (e) {',
  26441. ' this.set({});',
  26442. ' this.set(rtl.createSet(e));',
  26443. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26444. ' return this.get();',
  26445. ' };',
  26446. ' this.InitEmpty = function () {',
  26447. ' return this.get();',
  26448. ' };',
  26449. ' this.$new = function (fn, args) {',
  26450. ' return this[fn].apply({',
  26451. ' p: {},',
  26452. ' get: function () {',
  26453. ' return this.p;',
  26454. ' },',
  26455. ' set: function (v) {',
  26456. ' this.p = v;',
  26457. ' }',
  26458. ' }, args);',
  26459. ' };',
  26460. '});',
  26461. 'this.s = {};',
  26462. '']),
  26463. LinesToStr([ // $mod.$main
  26464. '$mod.THelper.DoIt.call({',
  26465. ' p: $mod,',
  26466. ' get: function () {',
  26467. ' return this.p.s;',
  26468. ' },',
  26469. ' set: function (v) {',
  26470. ' this.p.s = v;',
  26471. ' }',
  26472. '}, 123);',
  26473. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  26474. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  26475. ' p: $mod,',
  26476. ' get: function () {',
  26477. ' return this.p.s;',
  26478. ' },',
  26479. ' set: function (v) {',
  26480. ' this.p.s = v;',
  26481. ' }',
  26482. '}, $mod.TEnum.blue));',
  26483. '']));
  26484. end;
  26485. procedure TTestModule.TestTypeHelper_InterfaceType;
  26486. begin
  26487. StartProgram(false);
  26488. Add([
  26489. '{$interfaces com}',
  26490. '{$modeswitch typehelpers}',
  26491. 'type',
  26492. ' IUnknown = interface',
  26493. ' function _AddRef: longint;',
  26494. ' function _Release: longint;',
  26495. ' end;',
  26496. ' TObject = class(IUnknown)',
  26497. ' function _AddRef: longint; virtual; abstract;',
  26498. ' function _Release: longint; virtual; abstract;',
  26499. ' end;',
  26500. ' THelper = type helper for IUnknown',
  26501. ' procedure Fly(e: byte = 123);',
  26502. ' class procedure Run; static;',
  26503. ' end;',
  26504. 'var',
  26505. ' i: IUnknown;',
  26506. ' o: TObject;',
  26507. 'procedure THelper.Fly(e: byte);',
  26508. 'begin',
  26509. ' i:=Self;',
  26510. ' o:=Self as TObject;',
  26511. ' Self:=nil;',
  26512. ' Self:=i;',
  26513. ' Self:=o;',
  26514. ' with Self do begin',
  26515. ' Fly;',
  26516. ' Fly();',
  26517. ' end;',
  26518. 'end;',
  26519. 'class procedure THelper.Run;',
  26520. 'var l: IUnknown;',
  26521. 'begin',
  26522. ' l.Fly;',
  26523. ' l.Fly();',
  26524. 'end;',
  26525. 'begin',
  26526. ' i.Fly;',
  26527. ' i.Fly();',
  26528. ' i.Run;',
  26529. ' i.Run();',
  26530. ' IUnknown.Run;',
  26531. ' IUnknown.Run();',
  26532. '']);
  26533. ConvertProgram;
  26534. CheckSource('TestTypeHelper_InterfaceType',
  26535. LinesToStr([ // statements
  26536. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  26537. 'rtl.createClass(this, "TObject", null, function () {',
  26538. ' this.$init = function () {',
  26539. ' };',
  26540. ' this.$final = function () {',
  26541. ' };',
  26542. ' rtl.addIntf(this, $mod.IUnknown);',
  26543. '});',
  26544. 'rtl.createHelper(this, "THelper", null, function () {',
  26545. ' this.Fly = function (e) {',
  26546. ' var $ir = rtl.createIntfRefs();',
  26547. ' try {',
  26548. ' rtl.setIntfP($mod, "i", this.get());',
  26549. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  26550. ' this.set(null);',
  26551. ' this.set($mod.i);',
  26552. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  26553. ' var $with = this.get();',
  26554. ' $mod.THelper.Fly.call(this, 123);',
  26555. ' $mod.THelper.Fly.call(this, 123);',
  26556. ' } finally {',
  26557. ' $ir.free();',
  26558. ' };',
  26559. ' };',
  26560. ' this.Run = function () {',
  26561. ' var l = null;',
  26562. ' try {',
  26563. ' $mod.THelper.Fly.call({',
  26564. ' get: function () {',
  26565. ' return l;',
  26566. ' },',
  26567. ' set: function (v) {',
  26568. ' l = rtl.setIntfL(l, v);',
  26569. ' }',
  26570. ' }, 123);',
  26571. ' $mod.THelper.Fly.call({',
  26572. ' get: function () {',
  26573. ' return l;',
  26574. ' },',
  26575. ' set: function (v) {',
  26576. ' l = rtl.setIntfL(l, v);',
  26577. ' }',
  26578. ' }, 123);',
  26579. ' } finally {',
  26580. ' rtl._Release(l);',
  26581. ' };',
  26582. ' };',
  26583. '});',
  26584. 'this.i = null;',
  26585. 'this.o = null;',
  26586. '']),
  26587. LinesToStr([ // $mod.$main
  26588. '$mod.THelper.Fly.call({',
  26589. ' p: $mod,',
  26590. ' get: function () {',
  26591. ' return this.p.i;',
  26592. ' },',
  26593. ' set: function (v) {',
  26594. ' rtl.setIntfP(this.p, "i", v);',
  26595. ' }',
  26596. '}, 123);',
  26597. '$mod.THelper.Fly.call({',
  26598. ' p: $mod,',
  26599. ' get: function () {',
  26600. ' return this.p.i;',
  26601. ' },',
  26602. ' set: function (v) {',
  26603. ' rtl.setIntfP(this.p, "i", v);',
  26604. ' }',
  26605. '}, 123);',
  26606. '$mod.THelper.Run();',
  26607. '$mod.THelper.Run();',
  26608. '$mod.THelper.Run();',
  26609. '$mod.THelper.Run();',
  26610. '']));
  26611. end;
  26612. procedure TTestModule.TestTypeHelper_NestedSelf;
  26613. begin
  26614. StartProgram(false);
  26615. Add([
  26616. '{$modeswitch typehelpers}',
  26617. 'type',
  26618. ' THelper = type helper for string',
  26619. ' procedure Run(Value: string);',
  26620. ' end;',
  26621. 'procedure THelper.Run(Value: string);',
  26622. ' function Sub(i: nativeint): boolean;',
  26623. ' begin',
  26624. ' Result:=Self[i+1]=Value[i];',
  26625. ' end;',
  26626. 'begin',
  26627. ' if Self[3]=Value[4] then ;',
  26628. 'end;',
  26629. 'begin',
  26630. '']);
  26631. ConvertProgram;
  26632. CheckSource('TestTypeHelper_NestedSelf',
  26633. LinesToStr([ // statements
  26634. 'rtl.createHelper(this, "THelper", null, function () {',
  26635. ' this.Run = function (Value) {',
  26636. ' var $Self = this;',
  26637. ' function Sub(i) {',
  26638. ' var Result = false;',
  26639. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  26640. ' return Result;',
  26641. ' };',
  26642. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  26643. ' };',
  26644. '});',
  26645. '']),
  26646. LinesToStr([ // $mod.$main
  26647. '']));
  26648. end;
  26649. procedure TTestModule.TestProcType;
  26650. begin
  26651. StartProgram(false);
  26652. Add([
  26653. 'type',
  26654. ' TProcInt = procedure(vI: longint = 1);',
  26655. 'procedure DoIt(vJ: longint);',
  26656. 'begin end;',
  26657. 'var',
  26658. ' b: boolean;',
  26659. ' vP, vQ: tprocint;',
  26660. 'begin',
  26661. ' vp:=nil;',
  26662. ' vp:=vp;',
  26663. ' vp:=@doit;',
  26664. ' vp;',
  26665. ' vp();',
  26666. ' vp(2);',
  26667. ' b:=vp=nil;',
  26668. ' b:=nil=vp;',
  26669. ' b:=vp=vq;',
  26670. ' b:=vp=@doit;',
  26671. ' b:=@doit=vp;',
  26672. ' b:=vp<>nil;',
  26673. ' b:=nil<>vp;',
  26674. ' b:=vp<>vq;',
  26675. ' b:=vp<>@doit;',
  26676. ' b:=@doit<>vp;',
  26677. ' b:=Assigned(vp);',
  26678. ' if Assigned(vp) then ;']);
  26679. ConvertProgram;
  26680. CheckSource('TestProcType',
  26681. LinesToStr([ // statements
  26682. 'this.DoIt = function(vJ) {',
  26683. '};',
  26684. 'this.b = false;',
  26685. 'this.vP = null;',
  26686. 'this.vQ = null;'
  26687. ]),
  26688. LinesToStr([ // $mod.$main
  26689. '$mod.vP = null;',
  26690. '$mod.vP = $mod.vP;',
  26691. '$mod.vP = $mod.DoIt;',
  26692. '$mod.vP(1);',
  26693. '$mod.vP(1);',
  26694. '$mod.vP(2);',
  26695. '$mod.b = $mod.vP === null;',
  26696. '$mod.b = null === $mod.vP;',
  26697. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  26698. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26699. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26700. '$mod.b = $mod.vP !== null;',
  26701. '$mod.b = null !== $mod.vP;',
  26702. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  26703. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26704. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26705. '$mod.b = $mod.vP != null;',
  26706. 'if ($mod.vP != null) ;',
  26707. '']));
  26708. end;
  26709. procedure TTestModule.TestProcType_Arg;
  26710. begin
  26711. StartProgram(false);
  26712. Add([
  26713. 'type',
  26714. ' TProcInt = procedure(vI: longint = 1);',
  26715. 'procedure DoIt(vJ: longint); begin end;',
  26716. 'procedure DoSome(vP, vQ: TProcInt);',
  26717. 'var',
  26718. ' b: boolean;',
  26719. 'begin',
  26720. ' vp:=nil;',
  26721. ' vp:=vp;',
  26722. ' vp:=@doit;',
  26723. ' vp;',
  26724. ' vp();',
  26725. ' vp(2);',
  26726. ' b:=vp=nil;',
  26727. ' b:=nil=vp;',
  26728. ' b:=vp=vq;',
  26729. ' b:=vp=@doit;',
  26730. ' b:=@doit=vp;',
  26731. ' b:=vp<>nil;',
  26732. ' b:=nil<>vp;',
  26733. ' b:=vp<>vq;',
  26734. ' b:=vp<>@doit;',
  26735. ' b:=@doit<>vp;',
  26736. ' b:=Assigned(vp);',
  26737. ' if Assigned(vp) then ;',
  26738. 'end;',
  26739. 'begin',
  26740. ' DoSome(@DoIt,nil);']);
  26741. ConvertProgram;
  26742. CheckSource('TestProcType_Arg',
  26743. LinesToStr([ // statements
  26744. 'this.DoIt = function(vJ) {',
  26745. '};',
  26746. 'this.DoSome = function(vP, vQ) {',
  26747. ' var b = false;',
  26748. ' vP = null;',
  26749. ' vP = vP;',
  26750. ' vP = $mod.DoIt;',
  26751. ' vP(1);',
  26752. ' vP(1);',
  26753. ' vP(2);',
  26754. ' b = vP === null;',
  26755. ' b = null === vP;',
  26756. ' b = rtl.eqCallback(vP,vQ);',
  26757. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  26758. ' b = rtl.eqCallback($mod.DoIt, vP);',
  26759. ' b = vP !== null;',
  26760. ' b = null !== vP;',
  26761. ' b = !rtl.eqCallback(vP, vQ);',
  26762. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  26763. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  26764. ' b = vP != null;',
  26765. ' if (vP != null) ;',
  26766. '};',
  26767. '']),
  26768. LinesToStr([ // $mod.$main
  26769. '$mod.DoSome($mod.DoIt,null);',
  26770. '']));
  26771. end;
  26772. procedure TTestModule.TestProcType_FunctionFPC;
  26773. begin
  26774. StartProgram(false);
  26775. Add('type');
  26776. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26777. Add('function DoIt(vI: longint): longint;');
  26778. Add('begin end;');
  26779. Add('var');
  26780. Add(' b: boolean;');
  26781. Add(' vP, vQ: tfuncint;');
  26782. Add('begin');
  26783. Add(' vp:=nil;');
  26784. Add(' vp:=vp;');
  26785. Add(' vp:=@doit;'); // ok in fpc and delphi
  26786. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26787. Add(' vp;'); // ok in fpc and delphi
  26788. Add(' vp();');
  26789. Add(' vp(2);');
  26790. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26791. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26792. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26793. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26794. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26795. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  26796. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  26797. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26798. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26799. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26800. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26801. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26802. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  26803. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  26804. Add(' b:=Assigned(vp);');
  26805. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  26806. Add(' doit(vp());'); // ok in fpc and delphi
  26807. Add(' doit(vp(2));'); // ok in fpc and delphi
  26808. ConvertProgram;
  26809. CheckSource('TestProcType_FunctionFPC',
  26810. LinesToStr([ // statements
  26811. 'this.DoIt = function(vI) {',
  26812. ' var Result = 0;',
  26813. ' return Result;',
  26814. '};',
  26815. 'this.b = false;',
  26816. 'this.vP = null;',
  26817. 'this.vQ = null;'
  26818. ]),
  26819. LinesToStr([ // $mod.$main
  26820. '$mod.vP = null;',
  26821. '$mod.vP = $mod.vP;',
  26822. '$mod.vP = $mod.DoIt;',
  26823. '$mod.vP(1);',
  26824. '$mod.vP(1);',
  26825. '$mod.vP(2);',
  26826. '$mod.b = $mod.vP === null;',
  26827. '$mod.b = null === $mod.vP;',
  26828. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  26829. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26830. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26831. '$mod.b = 4 === $mod.vP(1);',
  26832. '$mod.b = $mod.vP !== null;',
  26833. '$mod.b = null !== $mod.vP;',
  26834. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  26835. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26836. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26837. '$mod.b = 6 !== $mod.vP(1);',
  26838. '$mod.b = $mod.vP != null;',
  26839. '$mod.DoIt($mod.vP(1));',
  26840. '$mod.DoIt($mod.vP(2));',
  26841. '']));
  26842. end;
  26843. procedure TTestModule.TestProcType_FunctionDelphi;
  26844. begin
  26845. StartProgram(false);
  26846. Add('{$mode Delphi}');
  26847. Add('type');
  26848. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26849. Add('function DoIt(vI: longint): longint;');
  26850. Add('begin end;');
  26851. Add('var');
  26852. Add(' b: boolean;');
  26853. Add(' vP, vQ: tfuncint;');
  26854. Add('begin');
  26855. Add(' vp:=nil;');
  26856. Add(' vp:=vp;');
  26857. Add(' vp:=@doit;'); // ok in fpc and delphi
  26858. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26859. Add(' vp;'); // ok in fpc and delphi
  26860. Add(' vp();');
  26861. Add(' vp(2);');
  26862. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26863. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26864. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26865. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26866. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26867. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  26868. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  26869. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26870. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26871. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26872. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26873. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26874. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  26875. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  26876. Add(' b:=Assigned(vp);');
  26877. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  26878. Add(' doit(vp());'); // ok in fpc and delphi
  26879. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  26880. ConvertProgram;
  26881. CheckSource('TestProcType_FunctionDelphi',
  26882. LinesToStr([ // statements
  26883. 'this.DoIt = function(vI) {',
  26884. ' var Result = 0;',
  26885. ' return Result;',
  26886. '};',
  26887. 'this.b = false;',
  26888. 'this.vP = null;',
  26889. 'this.vQ = null;'
  26890. ]),
  26891. LinesToStr([ // $mod.$main
  26892. '$mod.vP = null;',
  26893. '$mod.vP = $mod.vP;',
  26894. '$mod.vP = $mod.DoIt;',
  26895. '$mod.vP = $mod.DoIt;',
  26896. '$mod.vP(1);',
  26897. '$mod.vP(1);',
  26898. '$mod.vP(2);',
  26899. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  26900. '$mod.b = $mod.vP(1) === 3;',
  26901. '$mod.b = 4 === $mod.vP(1);',
  26902. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  26903. '$mod.b = $mod.vP(1) !== 5;',
  26904. '$mod.b = 6 !== $mod.vP(1);',
  26905. '$mod.b = $mod.vP != null;',
  26906. '$mod.DoIt($mod.vP(1));',
  26907. '$mod.DoIt($mod.vP(1));',
  26908. '$mod.DoIt($mod.vP(2));',
  26909. '']));
  26910. end;
  26911. procedure TTestModule.TestProcType_ProcedureDelphi;
  26912. begin
  26913. StartProgram(false);
  26914. Add('{$mode Delphi}');
  26915. Add('type');
  26916. Add(' TProc = procedure;');
  26917. Add('procedure DoIt;');
  26918. Add('begin end;');
  26919. Add('var');
  26920. Add(' b: boolean;');
  26921. Add(' vP, vQ: tproc;');
  26922. Add('begin');
  26923. Add(' vp:=nil;');
  26924. Add(' vp:=vp;');
  26925. Add(' vp:=vq;');
  26926. 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
  26927. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26928. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  26929. Add(' vp;'); // ok in fpc and delphi
  26930. Add(' vp();');
  26931. // equal
  26932. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26933. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  26934. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26935. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  26936. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  26937. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26938. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26939. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  26940. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26941. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  26942. // unequal
  26943. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26944. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  26945. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26946. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  26947. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26948. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  26949. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26950. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  26951. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26952. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  26953. Add(' b:=Assigned(vp);');
  26954. ConvertProgram;
  26955. CheckSource('TestProcType_ProcedureDelphi',
  26956. LinesToStr([ // statements
  26957. 'this.DoIt = function() {',
  26958. '};',
  26959. 'this.b = false;',
  26960. 'this.vP = null;',
  26961. 'this.vQ = null;'
  26962. ]),
  26963. LinesToStr([ // $mod.$main
  26964. '$mod.vP = null;',
  26965. '$mod.vP = $mod.vP;',
  26966. '$mod.vP = $mod.vQ;',
  26967. '$mod.vP = $mod.DoIt;',
  26968. '$mod.vP = $mod.DoIt;',
  26969. '$mod.vP();',
  26970. '$mod.vP();',
  26971. '$mod.b = $mod.vP === null;',
  26972. '$mod.b = null === $mod.vP;',
  26973. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  26974. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26975. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26976. '$mod.b = $mod.vP !== null;',
  26977. '$mod.b = null !== $mod.vP;',
  26978. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  26979. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26980. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26981. '$mod.b = $mod.vP != null;',
  26982. '']));
  26983. end;
  26984. procedure TTestModule.TestProcType_AsParam;
  26985. begin
  26986. StartProgram(false);
  26987. Add('type');
  26988. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26989. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  26990. Add('var vJ: tfuncint;');
  26991. Add('begin');
  26992. Add(' vg:=vg;');
  26993. Add(' vj:=vh;');
  26994. Add(' vi:=vi;');
  26995. Add(' doit(vg,vg,vg);');
  26996. Add(' doit(vh,vh,vj);');
  26997. Add(' doit(vi,vi,vi);');
  26998. Add(' doit(vj,vj,vj);');
  26999. Add('end;');
  27000. Add('var i: tfuncint;');
  27001. Add('begin');
  27002. Add(' doit(i,i,i);');
  27003. ConvertProgram;
  27004. CheckSource('TestProcType_AsParam',
  27005. LinesToStr([ // statements
  27006. 'this.DoIt = function (vG,vH,vI) {',
  27007. ' var vJ = null;',
  27008. ' vG = vG;',
  27009. ' vJ = vH;',
  27010. ' vI.set(vI.get());',
  27011. ' $mod.DoIt(vG, vG, {',
  27012. ' get: function () {',
  27013. ' return vG;',
  27014. ' },',
  27015. ' set: function (v) {',
  27016. ' vG = v;',
  27017. ' }',
  27018. ' });',
  27019. ' $mod.DoIt(vH, vH, {',
  27020. ' get: function () {',
  27021. ' return vJ;',
  27022. ' },',
  27023. ' set: function (v) {',
  27024. ' vJ = v;',
  27025. ' }',
  27026. ' });',
  27027. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27028. ' $mod.DoIt(vJ, vJ, {',
  27029. ' get: function () {',
  27030. ' return vJ;',
  27031. ' },',
  27032. ' set: function (v) {',
  27033. ' vJ = v;',
  27034. ' }',
  27035. ' });',
  27036. '};',
  27037. 'this.i = null;'
  27038. ]),
  27039. LinesToStr([
  27040. '$mod.DoIt($mod.i,$mod.i,{',
  27041. ' p: $mod,',
  27042. ' get: function () {',
  27043. ' return this.p.i;',
  27044. ' },',
  27045. ' set: function (v) {',
  27046. ' this.p.i = v;',
  27047. ' }',
  27048. '});'
  27049. ]));
  27050. end;
  27051. procedure TTestModule.TestProcType_MethodFPC;
  27052. begin
  27053. StartProgram(false);
  27054. Add('type');
  27055. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27056. Add(' TObject = class');
  27057. Add(' function DoIt(vA: longint = 1): longint;');
  27058. Add(' end;');
  27059. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27060. Add('begin');
  27061. Add('end;');
  27062. Add('var');
  27063. Add(' Obj: TObject;');
  27064. Add(' vP: tfuncint;');
  27065. Add(' b: boolean;');
  27066. Add('begin');
  27067. Add(' vp:[email protected];'); // ok in fpc and delphi
  27068. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27069. Add(' vp;'); // ok in fpc and delphi
  27070. Add(' vp();');
  27071. Add(' vp(2);');
  27072. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27073. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27074. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27075. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27076. ConvertProgram;
  27077. CheckSource('TestProcType_MethodFPC',
  27078. LinesToStr([ // statements
  27079. 'rtl.createClass(this, "TObject", null, function () {',
  27080. ' this.$init = function () {',
  27081. ' };',
  27082. ' this.$final = function () {',
  27083. ' };',
  27084. ' this.DoIt = function (vA) {',
  27085. ' var Result = 0;',
  27086. ' return Result;',
  27087. ' };',
  27088. '});',
  27089. 'this.Obj = null;',
  27090. 'this.vP = null;',
  27091. 'this.b = false;'
  27092. ]),
  27093. LinesToStr([
  27094. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27095. '$mod.vP(1);',
  27096. '$mod.vP(1);',
  27097. '$mod.vP(2);',
  27098. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27099. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27100. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27101. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27102. '']));
  27103. end;
  27104. procedure TTestModule.TestProcType_MethodDelphi;
  27105. begin
  27106. StartProgram(false);
  27107. Add([
  27108. '{$mode delphi}',
  27109. 'type',
  27110. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27111. ' TObject = class',
  27112. ' function DoIt(vA: longint = 1): longint;',
  27113. ' end;',
  27114. 'function TObject.DoIt(vA: longint = 1): longint;',
  27115. 'begin',
  27116. 'end;',
  27117. 'var',
  27118. ' Obj: TObject;',
  27119. ' vP: tfuncint;',
  27120. ' b: boolean;',
  27121. 'begin',
  27122. ' vp:[email protected];', // ok in fpc and delphi
  27123. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27124. ' vp;', // ok in fpc and delphi
  27125. ' vp();',
  27126. ' vp(2);',
  27127. //' b:[email protected];', // ok in fpc, illegal in delphi
  27128. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27129. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27130. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27131. '']);
  27132. ConvertProgram;
  27133. CheckSource('TestProcType_MethodDelphi',
  27134. LinesToStr([ // statements
  27135. 'rtl.createClass(this, "TObject", null, function () {',
  27136. ' this.$init = function () {',
  27137. ' };',
  27138. ' this.$final = function () {',
  27139. ' };',
  27140. ' this.DoIt = function (vA) {',
  27141. ' var Result = 0;',
  27142. ' return Result;',
  27143. ' };',
  27144. '});',
  27145. 'this.Obj = null;',
  27146. 'this.vP = null;',
  27147. 'this.b = false;'
  27148. ]),
  27149. LinesToStr([
  27150. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27151. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27152. '$mod.vP(1);',
  27153. '$mod.vP(1);',
  27154. '$mod.vP(2);',
  27155. '']));
  27156. end;
  27157. procedure TTestModule.TestProcType_PropertyFPC;
  27158. begin
  27159. StartProgram(false);
  27160. Add('type');
  27161. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27162. Add(' TObject = class');
  27163. Add(' FOnFoo: TFuncInt;');
  27164. Add(' function DoIt(vA: longint = 1): longint;');
  27165. Add(' function GetFoo: TFuncInt;');
  27166. Add(' procedure SetFoo(const Value: TFuncInt);');
  27167. Add(' function GetEvents(Index: longint): TFuncInt;');
  27168. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27169. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27170. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27171. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27172. Add(' end;');
  27173. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27174. Add('function tobject.getfoo: tfuncint; begin end;');
  27175. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27176. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27177. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27178. Add('var');
  27179. Add(' Obj: TObject;');
  27180. Add(' vP: tfuncint;');
  27181. Add(' b: boolean;');
  27182. Add('begin');
  27183. Add(' obj.onfoo:=nil;');
  27184. Add(' obj.onbar:=nil;');
  27185. Add(' obj.events[1]:=nil;');
  27186. Add(' obj.onfoo:=obj.onfoo;');
  27187. Add(' obj.onbar:=obj.onbar;');
  27188. Add(' obj.events[2]:=obj.events[3];');
  27189. Add(' obj.onfoo:[email protected];');
  27190. Add(' obj.onbar:[email protected];');
  27191. Add(' obj.events[4]:[email protected];');
  27192. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27193. //Add(' obj.onbar:=obj.doit;'); // delphi
  27194. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27195. Add(' obj.onfoo;');
  27196. Add(' obj.onbar;');
  27197. //Add(' obj.events[5];'); ToDo in pasresolver
  27198. Add(' obj.onfoo();');
  27199. Add(' obj.onbar();');
  27200. Add(' obj.events[6]();');
  27201. Add(' b:=obj.onfoo=nil;');
  27202. Add(' b:=obj.onbar=nil;');
  27203. Add(' b:=obj.events[7]=nil;');
  27204. Add(' b:=obj.onfoo<>nil;');
  27205. Add(' b:=obj.onbar<>nil;');
  27206. Add(' b:=obj.events[8]<>nil;');
  27207. Add(' b:=obj.onfoo=vp;');
  27208. Add(' b:=obj.onbar=vp;');
  27209. Add(' b:=obj.events[9]=vp;');
  27210. Add(' b:=obj.onfoo=obj.onfoo;');
  27211. Add(' b:=obj.onbar=obj.onfoo;');
  27212. Add(' b:=obj.events[10]=obj.onfoo;');
  27213. Add(' b:=obj.onfoo<>obj.onfoo;');
  27214. Add(' b:=obj.onbar<>obj.onfoo;');
  27215. Add(' b:=obj.events[11]<>obj.onfoo;');
  27216. Add(' b:[email protected];');
  27217. Add(' b:[email protected];');
  27218. Add(' b:=obj.events[12][email protected];');
  27219. Add(' b:=obj.onfoo<>@obj.doit;');
  27220. Add(' b:=obj.onbar<>@obj.doit;');
  27221. Add(' b:=obj.events[12]<>@obj.doit;');
  27222. Add(' b:=Assigned(obj.onfoo);');
  27223. Add(' b:=Assigned(obj.onbar);');
  27224. Add(' b:=Assigned(obj.events[13]);');
  27225. ConvertProgram;
  27226. CheckSource('TestProcType_PropertyFPC',
  27227. LinesToStr([ // statements
  27228. 'rtl.createClass(this, "TObject", null, function () {',
  27229. ' this.$init = function () {',
  27230. ' this.FOnFoo = null;',
  27231. ' };',
  27232. ' this.$final = function () {',
  27233. ' this.FOnFoo = undefined;',
  27234. ' };',
  27235. ' this.DoIt = function (vA) {',
  27236. ' var Result = 0;',
  27237. ' return Result;',
  27238. ' };',
  27239. 'this.GetFoo = function () {',
  27240. ' var Result = null;',
  27241. ' return Result;',
  27242. '};',
  27243. 'this.SetFoo = function (Value) {',
  27244. '};',
  27245. 'this.GetEvents = function (Index) {',
  27246. ' var Result = null;',
  27247. ' return Result;',
  27248. '};',
  27249. 'this.SetEvents = function (Index, Value) {',
  27250. '};',
  27251. '});',
  27252. 'this.Obj = null;',
  27253. 'this.vP = null;',
  27254. 'this.b = false;'
  27255. ]),
  27256. LinesToStr([
  27257. '$mod.Obj.FOnFoo = null;',
  27258. '$mod.Obj.SetFoo(null);',
  27259. '$mod.Obj.SetEvents(1, null);',
  27260. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27261. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27262. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27263. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27264. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27265. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27266. '$mod.Obj.FOnFoo(1);',
  27267. '$mod.Obj.GetFoo();',
  27268. '$mod.Obj.FOnFoo(1);',
  27269. '$mod.Obj.GetFoo()(1);',
  27270. '$mod.Obj.GetEvents(6)(1);',
  27271. '$mod.b = $mod.Obj.FOnFoo === null;',
  27272. '$mod.b = $mod.Obj.GetFoo() === null;',
  27273. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27274. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27275. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27276. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27277. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27278. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27279. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27280. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27281. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27282. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27283. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27284. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27285. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27286. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27287. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27288. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27289. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27290. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27291. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27292. '$mod.b = $mod.Obj.FOnFoo != null;',
  27293. '$mod.b = $mod.Obj.GetFoo() != null;',
  27294. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27295. '']));
  27296. end;
  27297. procedure TTestModule.TestProcType_PropertyDelphi;
  27298. begin
  27299. StartProgram(false);
  27300. Add('{$mode delphi}');
  27301. Add('type');
  27302. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27303. Add(' TObject = class');
  27304. Add(' FOnFoo: TFuncInt;');
  27305. Add(' function DoIt(vA: longint = 1): longint;');
  27306. Add(' function GetFoo: TFuncInt;');
  27307. Add(' procedure SetFoo(const Value: TFuncInt);');
  27308. Add(' function GetEvents(Index: longint): TFuncInt;');
  27309. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27310. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27311. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27312. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27313. Add(' end;');
  27314. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27315. Add('function tobject.getfoo: tfuncint; begin end;');
  27316. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27317. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27318. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27319. Add('var');
  27320. Add(' Obj: TObject;');
  27321. Add(' vP: tfuncint;');
  27322. Add(' b: boolean;');
  27323. Add('begin');
  27324. Add(' obj.onfoo:=nil;');
  27325. Add(' obj.onbar:=nil;');
  27326. Add(' obj.events[1]:=nil;');
  27327. Add(' obj.onfoo:=obj.onfoo;');
  27328. Add(' obj.onbar:=obj.onbar;');
  27329. Add(' obj.events[2]:=obj.events[3];');
  27330. Add(' obj.onfoo:[email protected];');
  27331. Add(' obj.onbar:[email protected];');
  27332. Add(' obj.events[4]:[email protected];');
  27333. Add(' obj.onfoo:=obj.doit;'); // delphi
  27334. Add(' obj.onbar:=obj.doit;'); // delphi
  27335. Add(' obj.events[4]:=obj.doit;'); // delphi
  27336. Add(' obj.onfoo;');
  27337. Add(' obj.onbar;');
  27338. //Add(' obj.events[5];'); ToDo in pasresolver
  27339. Add(' obj.onfoo();');
  27340. Add(' obj.onbar();');
  27341. Add(' obj.events[6]();');
  27342. //Add(' b:=obj.onfoo=nil;'); // fpc
  27343. //Add(' b:=obj.onbar=nil;'); // fpc
  27344. //Add(' b:=obj.events[7]=nil;'); // fpc
  27345. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27346. //Add(' b:=obj.onbar<>nil;'); // fpc
  27347. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27348. Add(' b:=obj.onfoo=vp;');
  27349. Add(' b:=obj.onbar=vp;');
  27350. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27351. Add(' b:=obj.onfoo=obj.onfoo;');
  27352. Add(' b:=obj.onbar=obj.onfoo;');
  27353. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27354. Add(' b:=obj.onfoo<>obj.onfoo;');
  27355. Add(' b:=obj.onbar<>obj.onfoo;');
  27356. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27357. //Add(' b:[email protected];'); // fpc
  27358. //Add(' b:[email protected];'); // fpc
  27359. //Add(' b:=obj.events[12][email protected];'); // fpc
  27360. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27361. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27362. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27363. Add(' b:=Assigned(obj.onfoo);');
  27364. Add(' b:=Assigned(obj.onbar);');
  27365. Add(' b:=Assigned(obj.events[13]);');
  27366. ConvertProgram;
  27367. CheckSource('TestProcType_PropertyDelphi',
  27368. LinesToStr([ // statements
  27369. 'rtl.createClass(this, "TObject", null, function () {',
  27370. ' this.$init = function () {',
  27371. ' this.FOnFoo = null;',
  27372. ' };',
  27373. ' this.$final = function () {',
  27374. ' this.FOnFoo = undefined;',
  27375. ' };',
  27376. ' this.DoIt = function (vA) {',
  27377. ' var Result = 0;',
  27378. ' return Result;',
  27379. ' };',
  27380. 'this.GetFoo = function () {',
  27381. ' var Result = null;',
  27382. ' return Result;',
  27383. '};',
  27384. 'this.SetFoo = function (Value) {',
  27385. '};',
  27386. 'this.GetEvents = function (Index) {',
  27387. ' var Result = null;',
  27388. ' return Result;',
  27389. '};',
  27390. 'this.SetEvents = function (Index, Value) {',
  27391. '};',
  27392. '});',
  27393. 'this.Obj = null;',
  27394. 'this.vP = null;',
  27395. 'this.b = false;'
  27396. ]),
  27397. LinesToStr([
  27398. '$mod.Obj.FOnFoo = null;',
  27399. '$mod.Obj.SetFoo(null);',
  27400. '$mod.Obj.SetEvents(1, null);',
  27401. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27402. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27403. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27404. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27405. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27406. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27407. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27408. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27409. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27410. '$mod.Obj.FOnFoo(1);',
  27411. '$mod.Obj.GetFoo();',
  27412. '$mod.Obj.FOnFoo(1);',
  27413. '$mod.Obj.GetFoo()(1);',
  27414. '$mod.Obj.GetEvents(6)(1);',
  27415. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27416. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27417. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27418. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27419. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27420. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27421. '$mod.b = $mod.Obj.FOnFoo != null;',
  27422. '$mod.b = $mod.Obj.GetFoo() != null;',
  27423. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27424. '']));
  27425. end;
  27426. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27427. begin
  27428. StartProgram(false);
  27429. Add('type');
  27430. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27431. Add(' TObject = class');
  27432. Add(' FOnFoo: TFuncInt;');
  27433. Add(' function DoIt(vA: longint = 1): longint;');
  27434. Add(' function GetFoo: TFuncInt;');
  27435. Add(' procedure SetFoo(const Value: TFuncInt);');
  27436. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27437. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27438. Add(' end;');
  27439. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27440. Add('function tobject.getfoo: tfuncint; begin end;');
  27441. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27442. Add('var');
  27443. Add(' Obj: TObject;');
  27444. Add(' vP: tfuncint;');
  27445. Add(' b: boolean;');
  27446. Add('begin');
  27447. Add('with obj do begin');
  27448. Add(' fonfoo:=nil;');
  27449. Add(' onfoo:=nil;');
  27450. Add(' onbar:=nil;');
  27451. Add(' fonfoo:=fonfoo;');
  27452. Add(' onfoo:=onfoo;');
  27453. Add(' onbar:=onbar;');
  27454. Add(' fonfoo:=@doit;');
  27455. Add(' onfoo:=@doit;');
  27456. Add(' onbar:=@doit;');
  27457. //Add(' fonfoo:=doit;'); // delphi
  27458. //Add(' onfoo:=doit;'); // delphi
  27459. //Add(' onbar:=doit;'); // delphi
  27460. Add(' fonfoo;');
  27461. Add(' onfoo;');
  27462. Add(' onbar;');
  27463. Add(' fonfoo();');
  27464. Add(' onfoo();');
  27465. Add(' onbar();');
  27466. Add(' b:=fonfoo=nil;');
  27467. Add(' b:=onfoo=nil;');
  27468. Add(' b:=onbar=nil;');
  27469. Add(' b:=fonfoo<>nil;');
  27470. Add(' b:=onfoo<>nil;');
  27471. Add(' b:=onbar<>nil;');
  27472. Add(' b:=fonfoo=vp;');
  27473. Add(' b:=onfoo=vp;');
  27474. Add(' b:=onbar=vp;');
  27475. Add(' b:=fonfoo=fonfoo;');
  27476. Add(' b:=onfoo=onfoo;');
  27477. Add(' b:=onbar=onfoo;');
  27478. Add(' b:=fonfoo<>fonfoo;');
  27479. Add(' b:=onfoo<>onfoo;');
  27480. Add(' b:=onbar<>onfoo;');
  27481. Add(' b:=fonfoo=@doit;');
  27482. Add(' b:=onfoo=@doit;');
  27483. Add(' b:=onbar=@doit;');
  27484. Add(' b:=fonfoo<>@doit;');
  27485. Add(' b:=onfoo<>@doit;');
  27486. Add(' b:=onbar<>@doit;');
  27487. Add(' b:=Assigned(fonfoo);');
  27488. Add(' b:=Assigned(onfoo);');
  27489. Add(' b:=Assigned(onbar);');
  27490. Add('end;');
  27491. ConvertProgram;
  27492. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  27493. LinesToStr([ // statements
  27494. 'rtl.createClass(this, "TObject", null, function () {',
  27495. ' this.$init = function () {',
  27496. ' this.FOnFoo = null;',
  27497. ' };',
  27498. ' this.$final = function () {',
  27499. ' this.FOnFoo = undefined;',
  27500. ' };',
  27501. ' this.DoIt = function (vA) {',
  27502. ' var Result = 0;',
  27503. ' return Result;',
  27504. ' };',
  27505. ' this.GetFoo = function () {',
  27506. ' var Result = null;',
  27507. ' return Result;',
  27508. ' };',
  27509. ' this.SetFoo = function (Value) {',
  27510. ' };',
  27511. '});',
  27512. 'this.Obj = null;',
  27513. 'this.vP = null;',
  27514. 'this.b = false;'
  27515. ]),
  27516. LinesToStr([
  27517. 'var $with = $mod.Obj;',
  27518. '$with.FOnFoo = null;',
  27519. '$with.FOnFoo = null;',
  27520. '$with.SetFoo(null);',
  27521. '$with.FOnFoo = $with.FOnFoo;',
  27522. '$with.FOnFoo = $with.FOnFoo;',
  27523. '$with.SetFoo($with.GetFoo());',
  27524. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  27525. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  27526. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  27527. '$with.FOnFoo(1);',
  27528. '$with.FOnFoo(1);',
  27529. '$with.GetFoo();',
  27530. '$with.FOnFoo(1);',
  27531. '$with.FOnFoo(1);',
  27532. '$with.GetFoo()(1);',
  27533. '$mod.b = $with.FOnFoo === null;',
  27534. '$mod.b = $with.FOnFoo === null;',
  27535. '$mod.b = $with.GetFoo() === null;',
  27536. '$mod.b = $with.FOnFoo !== null;',
  27537. '$mod.b = $with.FOnFoo !== null;',
  27538. '$mod.b = $with.GetFoo() !== null;',
  27539. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  27540. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  27541. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  27542. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27543. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27544. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  27545. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27546. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27547. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  27548. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27549. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27550. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  27551. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27552. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27553. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  27554. '$mod.b = $with.FOnFoo != null;',
  27555. '$mod.b = $with.FOnFoo != null;',
  27556. '$mod.b = $with.GetFoo() != null;',
  27557. '']));
  27558. end;
  27559. procedure TTestModule.TestProcType_Nested;
  27560. begin
  27561. StartProgram(false);
  27562. Add([
  27563. 'type',
  27564. ' TProcInt = procedure(vI: longint = 1);',
  27565. 'procedure DoIt(vJ: longint);',
  27566. 'var aProc: TProcInt;',
  27567. ' b: boolean;',
  27568. ' procedure Sub(vK: longint);',
  27569. ' var aSub: TProcInt;',
  27570. ' procedure SubSub(vK: longint);',
  27571. ' var aSubSub: TProcInt;',
  27572. ' begin;',
  27573. ' aProc:=@DoIt;',
  27574. ' aSub:=@DoIt;',
  27575. ' aSubSub:=@DoIt;',
  27576. ' aProc:=@Sub;',
  27577. ' aSub:=@Sub;',
  27578. ' aSubSub:=@Sub;',
  27579. ' aProc:=@SubSub;',
  27580. ' aSub:=@SubSub;',
  27581. ' aSubSub:=@SubSub;',
  27582. ' end;',
  27583. ' begin;',
  27584. ' end;',
  27585. 'begin;',
  27586. ' aProc:=@Sub;',
  27587. ' b:=aProc=@Sub;',
  27588. ' b:=@Sub=aProc;',
  27589. 'end;',
  27590. 'begin',
  27591. '']);
  27592. ConvertProgram;
  27593. CheckSource('TestProcType_Nested',
  27594. LinesToStr([ // statements
  27595. 'this.DoIt = function (vJ) {',
  27596. ' var aProc = null;',
  27597. ' var b = false;',
  27598. ' function Sub(vK) {',
  27599. ' var aSub = null;',
  27600. ' function SubSub(vK) {',
  27601. ' var aSubSub = null;',
  27602. ' aProc = $mod.DoIt;',
  27603. ' aSub = $mod.DoIt;',
  27604. ' aSubSub = $mod.DoIt;',
  27605. ' aProc = Sub;',
  27606. ' aSub = Sub;',
  27607. ' aSubSub = Sub;',
  27608. ' aProc = SubSub;',
  27609. ' aSub = SubSub;',
  27610. ' aSubSub = SubSub;',
  27611. ' };',
  27612. ' };',
  27613. ' aProc = Sub;',
  27614. ' b = rtl.eqCallback(aProc, Sub);',
  27615. ' b = rtl.eqCallback(Sub, aProc);',
  27616. '};',
  27617. '']),
  27618. LinesToStr([ // $mod.$main
  27619. '']));
  27620. end;
  27621. procedure TTestModule.TestProcType_NestedOfObject;
  27622. begin
  27623. StartProgram(false);
  27624. Add([
  27625. 'type',
  27626. ' TProcInt = procedure(vI: longint = 1) of object;',
  27627. ' TObject = class',
  27628. ' procedure DoIt(vJ: longint);',
  27629. ' end;',
  27630. 'procedure TObject.DoIt(vJ: longint);',
  27631. 'var aProc: TProcInt;',
  27632. ' b: boolean;',
  27633. ' procedure Sub(vK: longint);',
  27634. ' var aSub: TProcInt;',
  27635. ' procedure SubSub(vK: longint);',
  27636. ' var aSubSub: TProcInt;',
  27637. ' begin;',
  27638. ' aProc:=@DoIt;',
  27639. ' aSub:=@DoIt;',
  27640. ' aSubSub:=@DoIt;',
  27641. ' aProc:=@Sub;',
  27642. ' aSub:=@Sub;',
  27643. ' aSubSub:=@Sub;',
  27644. ' aProc:=@SubSub;',
  27645. ' aSub:=@SubSub;',
  27646. ' aSubSub:=@SubSub;',
  27647. ' end;',
  27648. ' begin;',
  27649. ' end;',
  27650. 'begin;',
  27651. ' aProc:=@Sub;',
  27652. ' b:=aProc=@Sub;',
  27653. ' b:=@Sub=aProc;',
  27654. 'end;',
  27655. 'begin',
  27656. '']);
  27657. ConvertProgram;
  27658. CheckSource('TestProcType_Nested',
  27659. LinesToStr([ // statements
  27660. 'rtl.createClass(this, "TObject", null, function () {',
  27661. ' this.$init = function () {',
  27662. ' };',
  27663. ' this.$final = function () {',
  27664. ' };',
  27665. ' this.DoIt = function (vJ) {',
  27666. ' var $Self = this;',
  27667. ' var aProc = null;',
  27668. ' var b = false;',
  27669. ' function Sub(vK) {',
  27670. ' var aSub = null;',
  27671. ' function SubSub(vK) {',
  27672. ' var aSubSub = null;',
  27673. ' aProc = rtl.createCallback($Self, "DoIt");',
  27674. ' aSub = rtl.createCallback($Self, "DoIt");',
  27675. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  27676. ' aProc = Sub;',
  27677. ' aSub = Sub;',
  27678. ' aSubSub = Sub;',
  27679. ' aProc = SubSub;',
  27680. ' aSub = SubSub;',
  27681. ' aSubSub = SubSub;',
  27682. ' };',
  27683. ' };',
  27684. ' aProc = Sub;',
  27685. ' b = rtl.eqCallback(aProc, Sub);',
  27686. ' b = rtl.eqCallback(Sub, aProc);',
  27687. ' };',
  27688. '});',
  27689. '']),
  27690. LinesToStr([ // $mod.$main
  27691. '']));
  27692. end;
  27693. procedure TTestModule.TestProcType_ReferenceToProc;
  27694. begin
  27695. StartProgram(false);
  27696. Add([
  27697. 'type',
  27698. ' TProcRef = reference to procedure(i: longint = 0);',
  27699. ' TFuncRef = reference to function(i: longint = 0): longint;',
  27700. 'var',
  27701. ' p: TProcRef;',
  27702. ' f: TFuncRef;',
  27703. 'procedure DoIt(i: longint);',
  27704. 'begin',
  27705. 'end;',
  27706. 'function GetIt(i: longint): longint;',
  27707. 'begin',
  27708. ' p:=@DoIt;',
  27709. ' f:=@GetIt;',
  27710. ' f;',
  27711. ' f();',
  27712. ' f(1);',
  27713. 'end;',
  27714. 'begin',
  27715. ' p:=@DoIt;',
  27716. ' f:=@GetIt;',
  27717. ' f;',
  27718. ' f();',
  27719. ' f(1);',
  27720. ' p:=TProcRef(f);',
  27721. '']);
  27722. ConvertProgram;
  27723. CheckSource('TestProcType_ReferenceToProc',
  27724. LinesToStr([ // statements
  27725. 'this.p = null;',
  27726. 'this.f = null;',
  27727. 'this.DoIt = function (i) {',
  27728. '};',
  27729. 'this.GetIt = function (i) {',
  27730. ' var Result = 0;',
  27731. ' $mod.p = $mod.DoIt;',
  27732. ' $mod.f = $mod.GetIt;',
  27733. ' $mod.f(0);',
  27734. ' $mod.f(0);',
  27735. ' $mod.f(1);',
  27736. ' return Result;',
  27737. '};',
  27738. '']),
  27739. LinesToStr([ // $mod.$main
  27740. '$mod.p = $mod.DoIt;',
  27741. '$mod.f = $mod.GetIt;',
  27742. '$mod.f(0);',
  27743. '$mod.f(0);',
  27744. '$mod.f(1);',
  27745. '$mod.p = $mod.f;',
  27746. '']));
  27747. end;
  27748. procedure TTestModule.TestProcType_ReferenceToMethod;
  27749. begin
  27750. StartProgram(false);
  27751. Add([
  27752. 'type',
  27753. ' TFuncRef = reference to function(i: longint = 5): longint;',
  27754. ' TObject = class',
  27755. ' function Grow(s: longint): longint;',
  27756. ' end;',
  27757. 'var',
  27758. ' f: tfuncref;',
  27759. 'function tobject.grow(s: longint): longint;',
  27760. ' function GrowSub(i: longint): longint;',
  27761. ' begin',
  27762. ' f:=@grow;',
  27763. ' f:=@growsub;',
  27764. ' end;',
  27765. 'begin',
  27766. ' f:=@grow;',
  27767. ' f:=@growsub;',
  27768. 'end;',
  27769. 'begin',
  27770. '']);
  27771. ConvertProgram;
  27772. CheckSource('TestProcType_ReferenceToMethod',
  27773. LinesToStr([ // statements
  27774. 'rtl.createClass(this, "TObject", null, function () {',
  27775. ' this.$init = function () {',
  27776. ' };',
  27777. ' this.$final = function () {',
  27778. ' };',
  27779. ' this.Grow = function (s) {',
  27780. ' var $Self = this;',
  27781. ' var Result = 0;',
  27782. ' function GrowSub(i) {',
  27783. ' var Result = 0;',
  27784. ' $mod.f = rtl.createCallback($Self, "Grow");',
  27785. ' $mod.f = GrowSub;',
  27786. ' return Result;',
  27787. ' };',
  27788. ' $mod.f = rtl.createCallback($Self, "Grow");',
  27789. ' $mod.f = GrowSub;',
  27790. ' return Result;',
  27791. ' };',
  27792. '});',
  27793. 'this.f = null;',
  27794. '']),
  27795. LinesToStr([ // $mod.$main
  27796. '']));
  27797. end;
  27798. procedure TTestModule.TestProcType_Typecast;
  27799. begin
  27800. StartProgram(false);
  27801. Add([
  27802. 'type',
  27803. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  27804. ' TEvent = procedure of object;',
  27805. ' TGetter = function:longint of object;',
  27806. ' TProcA = procedure(i: longint);',
  27807. ' TFuncB = function(i, j: longint): longint;',
  27808. 'procedure DoIt(); varargs; begin end;',
  27809. 'var',
  27810. ' Notify: tnotifyevent;',
  27811. ' Event: tevent;',
  27812. ' Getter: tgetter;',
  27813. ' ProcA: tproca;',
  27814. ' FuncB: tfuncb;',
  27815. ' p: pointer;',
  27816. 'begin',
  27817. ' notify:=tnotifyevent(event);',
  27818. ' event:=tevent(event);',
  27819. ' event:=tevent(notify);',
  27820. ' event:=tevent(getter);',
  27821. ' event:=tevent(proca);',
  27822. ' proca:=tproca(funcb);',
  27823. ' funcb:=tfuncb(funcb);',
  27824. ' funcb:=tfuncb(proca);',
  27825. ' funcb:=tfuncb(getter);',
  27826. ' proca:=tproca(p);',
  27827. ' funcb:=tfuncb(p);',
  27828. ' getter:=tgetter(p);',
  27829. ' p:=pointer(notify);',
  27830. ' p:=notify;',
  27831. ' p:=pointer(proca);',
  27832. ' p:=proca;',
  27833. ' p:=pointer(funcb);',
  27834. ' p:=funcb;',
  27835. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  27836. '']);
  27837. ConvertProgram;
  27838. CheckSource('TestProcType_Typecast',
  27839. LinesToStr([ // statements
  27840. 'this.DoIt = function () {',
  27841. '};',
  27842. 'this.Notify = null;',
  27843. 'this.Event = null;',
  27844. 'this.Getter = null;',
  27845. 'this.ProcA = null;',
  27846. 'this.FuncB = null;',
  27847. 'this.p = null;',
  27848. '']),
  27849. LinesToStr([ // $mod.$main
  27850. '$mod.Notify = $mod.Event;',
  27851. '$mod.Event = $mod.Event;',
  27852. '$mod.Event = $mod.Notify;',
  27853. '$mod.Event = $mod.Getter;',
  27854. '$mod.Event = $mod.ProcA;',
  27855. '$mod.ProcA = $mod.FuncB;',
  27856. '$mod.FuncB = $mod.FuncB;',
  27857. '$mod.FuncB = $mod.ProcA;',
  27858. '$mod.FuncB = $mod.Getter;',
  27859. '$mod.ProcA = $mod.p;',
  27860. '$mod.FuncB = $mod.p;',
  27861. '$mod.Getter = $mod.p;',
  27862. '$mod.p = $mod.Notify;',
  27863. '$mod.p = $mod.Notify;',
  27864. '$mod.p = $mod.ProcA;',
  27865. '$mod.p = $mod.ProcA;',
  27866. '$mod.p = $mod.FuncB;',
  27867. '$mod.p = $mod.FuncB;',
  27868. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  27869. '']));
  27870. end;
  27871. procedure TTestModule.TestProcType_PassProcToUntyped;
  27872. begin
  27873. StartProgram(false);
  27874. Add([
  27875. 'type',
  27876. ' TEvent = procedure of object;',
  27877. ' TFunc = function: longint;',
  27878. 'procedure DoIt(); varargs; begin end;',
  27879. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  27880. 'var',
  27881. ' Event: tevent;',
  27882. ' Func: TFunc;',
  27883. 'begin',
  27884. ' doit(event,func);',
  27885. ' dosome(event,event,event);',
  27886. ' dosome(func,func,func);',
  27887. '']);
  27888. ConvertProgram;
  27889. CheckSource('TestProcType_PassProcToUntyped',
  27890. LinesToStr([ // statements
  27891. 'this.DoIt = function () {',
  27892. '};',
  27893. 'this.DoSome = function (a, b, p) {',
  27894. '};',
  27895. 'this.Event = null;',
  27896. 'this.Func = null;',
  27897. '']),
  27898. LinesToStr([ // $mod.$main
  27899. '$mod.DoIt($mod.Event, $mod.Func);',
  27900. '$mod.DoSome($mod.Event, {',
  27901. ' p: $mod,',
  27902. ' get: function () {',
  27903. ' return this.p.Event;',
  27904. ' },',
  27905. ' set: function (v) {',
  27906. ' this.p.Event = v;',
  27907. ' }',
  27908. '}, $mod.Event);',
  27909. '$mod.DoSome($mod.Func, {',
  27910. ' p: $mod,',
  27911. ' get: function () {',
  27912. ' return this.p.Func;',
  27913. ' },',
  27914. ' set: function (v) {',
  27915. ' this.p.Func = v;',
  27916. ' }',
  27917. '}, $mod.Func);',
  27918. '']));
  27919. end;
  27920. procedure TTestModule.TestProcType_PassProcToArray;
  27921. begin
  27922. StartProgram(false);
  27923. Add([
  27924. 'type',
  27925. ' TFunc = function: longint;',
  27926. ' TArrFunc = array of TFunc;',
  27927. 'procedure DoIt(Arr: TArrFunc); begin end;',
  27928. 'function GetIt: longint; begin end;',
  27929. 'var',
  27930. ' Func: tfunc;',
  27931. 'begin',
  27932. ' doit([]);',
  27933. ' doit([@GetIt]);',
  27934. ' doit([Func]);',
  27935. '']);
  27936. ConvertProgram;
  27937. CheckSource('TestProcType_PassProcToArray',
  27938. LinesToStr([ // statements
  27939. 'this.DoIt = function (Arr) {',
  27940. '};',
  27941. 'this.GetIt = function () {',
  27942. ' var Result = 0;',
  27943. ' return Result;',
  27944. '};',
  27945. 'this.Func = null;',
  27946. '']),
  27947. LinesToStr([ // $mod.$main
  27948. '$mod.DoIt([]);',
  27949. '$mod.DoIt([$mod.GetIt]);',
  27950. '$mod.DoIt([$mod.Func]);',
  27951. '']));
  27952. end;
  27953. procedure TTestModule.TestProcType_SafeCallObjFPC;
  27954. begin
  27955. StartProgram(false);
  27956. Add([
  27957. '{$modeswitch externalclass}',
  27958. 'type',
  27959. ' TProc = reference to procedure(i: longint); safecall;',
  27960. ' TEvent = procedure(i: longint) of object; safecall;',
  27961. ' TExtA = class external name ''ExtObj''',
  27962. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  27963. ' procedure DoSome(Id: longint = 1);',
  27964. ' procedure SetOnClick(const e: TEvent);',
  27965. ' property OnClick: TEvent write SetOnClick;',
  27966. ' class procedure Fly(Id: longint = 1); static;',
  27967. ' procedure SetOnShow(const p: TProc);',
  27968. ' property OnShow: TProc write SetOnShow;',
  27969. ' end;',
  27970. 'procedure Run(i: longint = 1);',
  27971. 'begin',
  27972. 'end;',
  27973. 'var',
  27974. ' Obj: texta;',
  27975. ' e: TEvent;',
  27976. ' p: TProc;',
  27977. 'begin',
  27978. ' e:=e;',
  27979. ' e:[email protected];',
  27980. ' e:[email protected];',
  27981. ' e:=TEvent(@obj.dosome);', // no safecall
  27982. ' obj.OnClick:[email protected];',
  27983. ' obj.OnClick:[email protected];',
  27984. ' obj.setonclick(@obj.doit);',
  27985. ' obj.setonclick(@obj.dosome);',
  27986. ' p:=@Run;',
  27987. ' p:[email protected];',
  27988. ' obj.OnShow:=@Run;',
  27989. ' obj.OnShow:[email protected];',
  27990. ' obj.setOnShow(@Run);',
  27991. ' obj.setOnShow(@TExtA.Fly);',
  27992. ' with obj do begin',
  27993. ' e:=@doit;',
  27994. ' e:=@dosome;',
  27995. ' OnClick:=@doit;',
  27996. ' OnClick:=@dosome;',
  27997. ' setonclick(@doit);',
  27998. ' setonclick(@dosome);',
  27999. ' OnShow:=@Run;',
  28000. ' setOnShow(@Run);',
  28001. ' end;']);
  28002. ConvertProgram;
  28003. CheckSource('TestProcType_SafeCallObjFPC',
  28004. LinesToStr([ // statements
  28005. 'this.Run = function (i) {',
  28006. '};',
  28007. 'this.Obj = null;',
  28008. 'this.e = null;',
  28009. 'this.p = null;',
  28010. '']),
  28011. LinesToStr([ // $mod.$main
  28012. '$mod.e = $mod.e;',
  28013. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28014. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28015. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28016. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28017. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28018. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28019. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28020. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28021. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28022. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28023. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28024. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28025. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28026. 'var $with = $mod.Obj;',
  28027. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28028. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28029. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28030. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28031. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28032. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28033. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28034. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28035. '']));
  28036. end;
  28037. procedure TTestModule.TestProcType_SafeCallDelphi;
  28038. begin
  28039. StartProgram(false);
  28040. Add([
  28041. '{$mode delphi}',
  28042. '{$modeswitch externalclass}',
  28043. 'type',
  28044. ' TProc = reference to procedure(i: longint); safecall;',
  28045. ' TEvent = procedure(i: longint) of object; safecall;',
  28046. ' TExtA = class external name ''ExtObj''',
  28047. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28048. ' procedure DoSome(Id: longint = 1);',
  28049. ' procedure SetOnClick(const e: TEvent);',
  28050. ' property OnClick: TEvent write SetOnClick;',
  28051. ' class procedure Fly(Id: longint = 1); static;',
  28052. ' procedure SetOnShow(const p: TProc);',
  28053. ' property OnShow: TProc write SetOnShow;',
  28054. ' end;',
  28055. 'procedure Run(i: longint = 1);',
  28056. 'begin',
  28057. 'end;',
  28058. 'var',
  28059. ' Obj: texta;',
  28060. ' e: TEvent;',
  28061. ' p: TProc;',
  28062. 'begin',
  28063. ' e:=e;',
  28064. ' e:=obj.doit;',
  28065. ' e:=obj.dosome;',
  28066. ' e:=TEvent(@obj.dosome);', // no safecall
  28067. ' obj.OnClick:=obj.doit;',
  28068. ' obj.OnClick:=obj.dosome;',
  28069. ' obj.setonclick(obj.doit);',
  28070. ' obj.setonclick(obj.dosome);',
  28071. ' p:=Run;',
  28072. ' p:=TExtA.Fly;',
  28073. ' obj.OnShow:=Run;',
  28074. ' obj.OnShow:=TExtA.Fly;',
  28075. ' obj.setOnShow(Run);',
  28076. ' obj.setOnShow(TExtA.Fly);',
  28077. ' with obj do begin',
  28078. ' e:=doit;',
  28079. ' e:=dosome;',
  28080. ' OnClick:=doit;',
  28081. ' OnClick:=dosome;',
  28082. ' setonclick(doit);',
  28083. ' setonclick(dosome);',
  28084. ' OnShow:=@Run;',
  28085. ' setOnShow(@Run);',
  28086. ' end;']);
  28087. ConvertProgram;
  28088. CheckSource('TestProcType_SafeCallDelphi',
  28089. LinesToStr([ // statements
  28090. 'this.Run = function (i) {',
  28091. '};',
  28092. 'this.Obj = null;',
  28093. 'this.e = null;',
  28094. 'this.p = null;',
  28095. '']),
  28096. LinesToStr([ // $mod.$main
  28097. '$mod.e = $mod.e;',
  28098. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28099. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28100. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28101. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28102. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28103. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28104. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28105. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28106. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28107. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28108. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28109. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28110. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28111. 'var $with = $mod.Obj;',
  28112. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28113. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28114. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28115. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28116. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28117. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28118. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28119. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28120. '']));
  28121. end;
  28122. procedure TTestModule.TestPointer;
  28123. begin
  28124. StartProgram(false);
  28125. Add(['type',
  28126. ' TObject = class end;',
  28127. ' TClass = class of TObject;',
  28128. ' TArrInt = array of longint;',
  28129. 'const',
  28130. ' n = nil;',
  28131. 'var',
  28132. ' v: jsvalue;',
  28133. ' Obj: tobject;',
  28134. ' C: tclass;',
  28135. ' a: tarrint;',
  28136. ' p: Pointer = nil;',
  28137. ' s: string;',
  28138. 'begin',
  28139. ' p:=p;',
  28140. ' p:=nil;',
  28141. ' if p=nil then;',
  28142. ' if nil=p then;',
  28143. ' if Assigned(p) then;',
  28144. ' p:=Pointer(v);',
  28145. ' p:=obj;',
  28146. ' p:=c;',
  28147. ' p:=a;',
  28148. ' p:=tobject;',
  28149. ' obj:=TObject(p);',
  28150. ' c:=TClass(p);',
  28151. ' a:=TArrInt(p);',
  28152. ' p:=n;',
  28153. ' p:=Pointer(a);',
  28154. ' p:=pointer(s);',
  28155. ' s:=string(p);',
  28156. '']);
  28157. ConvertProgram;
  28158. CheckSource('TestPointer',
  28159. LinesToStr([ // statements
  28160. 'rtl.createClass(this, "TObject", null, function () {',
  28161. ' this.$init = function () {',
  28162. ' };',
  28163. ' this.$final = function () {',
  28164. ' };',
  28165. '});',
  28166. 'this.n = null;',
  28167. 'this.v = undefined;',
  28168. 'this.Obj = null;',
  28169. 'this.C = null;',
  28170. 'this.a = [];',
  28171. 'this.p = null;',
  28172. 'this.s = "";',
  28173. '']),
  28174. LinesToStr([ // $mod.$main
  28175. '$mod.p = $mod.p;',
  28176. '$mod.p = null;',
  28177. 'if ($mod.p === null) ;',
  28178. 'if (null === $mod.p) ;',
  28179. 'if ($mod.p != null) ;',
  28180. '$mod.p = $mod.v;',
  28181. '$mod.p = $mod.Obj;',
  28182. '$mod.p = $mod.C;',
  28183. '$mod.p = $mod.a;',
  28184. '$mod.p = $mod.TObject;',
  28185. '$mod.Obj = $mod.p;',
  28186. '$mod.C = $mod.p;',
  28187. '$mod.a = $mod.p;',
  28188. '$mod.p = null;',
  28189. '$mod.p = $mod.a;',
  28190. '$mod.p = $mod.s;',
  28191. '$mod.s = $mod.p;',
  28192. '']));
  28193. end;
  28194. procedure TTestModule.TestPointer_Proc;
  28195. begin
  28196. StartProgram(false);
  28197. Add('type');
  28198. Add(' TObject = class');
  28199. Add(' procedure DoIt; virtual; abstract;');
  28200. Add(' end;');
  28201. Add('procedure DoSome; begin end;');
  28202. Add('var');
  28203. Add(' o: TObject;');
  28204. Add(' p: Pointer;');
  28205. Add('begin');
  28206. Add(' p:=@DoSome;');
  28207. Add(' p:[email protected];');
  28208. ConvertProgram;
  28209. CheckSource('TestPointer_Proc',
  28210. LinesToStr([ // statements
  28211. 'rtl.createClass(this, "TObject", null, function () {',
  28212. ' this.$init = function () {',
  28213. ' };',
  28214. ' this.$final = function () {',
  28215. ' };',
  28216. '});',
  28217. 'this.DoSome = function () {',
  28218. '};',
  28219. 'this.o = null;',
  28220. 'this.p = null;',
  28221. '']),
  28222. LinesToStr([ // $mod.$main
  28223. '$mod.p = $mod.DoSome;',
  28224. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28225. '']));
  28226. end;
  28227. procedure TTestModule.TestPointer_AssignRecordFail;
  28228. begin
  28229. StartProgram(false);
  28230. Add('type');
  28231. Add(' TRec = record end;');
  28232. Add('var');
  28233. Add(' p: Pointer;');
  28234. Add(' r: TRec;');
  28235. Add('begin');
  28236. Add(' p:=r;');
  28237. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28238. nIncompatibleTypesGotExpected);
  28239. ConvertProgram;
  28240. end;
  28241. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28242. begin
  28243. StartProgram(false);
  28244. Add('type');
  28245. Add(' TArr = array[boolean] of longint;');
  28246. Add('var');
  28247. Add(' p: Pointer;');
  28248. Add(' a: TArr;');
  28249. Add('begin');
  28250. Add(' p:=a;');
  28251. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28252. nIncompatibleTypesGotExpected);
  28253. ConvertProgram;
  28254. end;
  28255. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28256. begin
  28257. StartProgram(false);
  28258. Add([
  28259. 'procedure DoIt(args: array of jsvalue); begin end;',
  28260. 'procedure DoAll; varargs; begin end;',
  28261. 'var',
  28262. ' v: jsvalue;',
  28263. 'begin',
  28264. ' DoIt([pointer(v)]);',
  28265. ' DoAll(pointer(v));',
  28266. '']);
  28267. ConvertProgram;
  28268. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28269. LinesToStr([ // statements
  28270. 'this.DoIt = function (args) {',
  28271. '};',
  28272. 'this.DoAll = function () {',
  28273. '};',
  28274. 'this.v = undefined;',
  28275. '']),
  28276. LinesToStr([ // $mod.$main
  28277. '$mod.DoIt([$mod.v]);',
  28278. '$mod.DoAll($mod.v);',
  28279. '']));
  28280. end;
  28281. procedure TTestModule.TestPointer_NonRecordFail;
  28282. begin
  28283. StartProgram(false);
  28284. Add([
  28285. 'type',
  28286. ' p = ^longint;',
  28287. 'begin',
  28288. '']);
  28289. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28290. ConvertProgram;
  28291. end;
  28292. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28293. begin
  28294. StartProgram(false);
  28295. Add([
  28296. 'procedure DoIt(p: ^longint); begin end;',
  28297. 'begin',
  28298. '']);
  28299. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28300. ConvertProgram;
  28301. end;
  28302. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28303. begin
  28304. StartProgram(false);
  28305. Add([
  28306. 'var p: ^longint;',
  28307. 'begin',
  28308. '']);
  28309. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28310. ConvertProgram;
  28311. end;
  28312. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28313. begin
  28314. StartProgram(false);
  28315. Add([
  28316. 'function DoIt: ^longint; begin end;',
  28317. 'begin',
  28318. '']);
  28319. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28320. ConvertProgram;
  28321. end;
  28322. procedure TTestModule.TestPointer_AddrOperatorFail;
  28323. begin
  28324. StartProgram(false);
  28325. Add([
  28326. 'var i: longint;',
  28327. 'begin',
  28328. ' if @i=nil then ;',
  28329. '']);
  28330. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28331. ConvertProgram;
  28332. end;
  28333. procedure TTestModule.TestPointer_ArrayParamsFail;
  28334. begin
  28335. StartProgram(false);
  28336. Add([
  28337. 'var',
  28338. ' p: Pointer;',
  28339. 'begin',
  28340. ' p:=p[1];',
  28341. '']);
  28342. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28343. ConvertProgram;
  28344. end;
  28345. procedure TTestModule.TestPointer_PointerAddFail;
  28346. begin
  28347. StartProgram(false);
  28348. Add([
  28349. 'var',
  28350. ' p: Pointer;',
  28351. 'begin',
  28352. ' p:=p+1;',
  28353. '']);
  28354. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28355. ConvertProgram;
  28356. end;
  28357. procedure TTestModule.TestPointer_IncPointerFail;
  28358. begin
  28359. StartProgram(false);
  28360. Add([
  28361. 'var',
  28362. ' p: Pointer;',
  28363. 'begin',
  28364. ' inc(p,1);',
  28365. '']);
  28366. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28367. nIncompatibleTypeArgNo);
  28368. ConvertProgram;
  28369. end;
  28370. procedure TTestModule.TestPointer_Record;
  28371. begin
  28372. StartProgram(false);
  28373. Add([
  28374. 'type',
  28375. ' TRec = record x: longint; end;',
  28376. ' PRec = ^TRec;',
  28377. 'var',
  28378. ' r: TRec;',
  28379. ' p: PRec;',
  28380. ' q: ^TRec;',
  28381. ' Ptr: pointer;',
  28382. 'begin',
  28383. ' new(p);',
  28384. ' p:=@r;',
  28385. ' r:=p^;',
  28386. ' r.x:=p^.x;',
  28387. ' p^.x:=r.x;',
  28388. ' if p^.x=3 then ;',
  28389. ' if 4=p^.x then ;',
  28390. ' dispose(p);',
  28391. ' new(q);',
  28392. ' dispose(q);',
  28393. ' Ptr:=p;',
  28394. ' p:=PRec(ptr);',
  28395. '']);
  28396. ConvertProgram;
  28397. CheckSource('TestPointer_Record',
  28398. LinesToStr([ // statements
  28399. 'rtl.recNewT(this, "TRec", function () {',
  28400. ' this.x = 0;',
  28401. ' this.$eq = function (b) {',
  28402. ' return this.x === b.x;',
  28403. ' };',
  28404. ' this.$assign = function (s) {',
  28405. ' this.x = s.x;',
  28406. ' return this;',
  28407. ' };',
  28408. '});',
  28409. 'this.r = this.TRec.$new();',
  28410. 'this.p = null;',
  28411. 'this.q = null;',
  28412. 'this.Ptr = null;',
  28413. '']),
  28414. LinesToStr([ // $mod.$main
  28415. '$mod.p = $mod.TRec.$new();',
  28416. '$mod.p = $mod.r;',
  28417. '$mod.r.$assign($mod.p);',
  28418. '$mod.r.x = $mod.p.x;',
  28419. '$mod.p.x = $mod.r.x;',
  28420. 'if ($mod.p.x === 3) ;',
  28421. 'if (4 === $mod.p.x) ;',
  28422. '$mod.p = null;',
  28423. '$mod.q = $mod.TRec.$new();',
  28424. '$mod.q = null;',
  28425. '$mod.Ptr = $mod.p;',
  28426. '$mod.p = $mod.Ptr;',
  28427. '']));
  28428. end;
  28429. procedure TTestModule.TestPointer_RecordArg;
  28430. begin
  28431. StartProgram(false);
  28432. Add([
  28433. '{$modeswitch autoderef}',
  28434. 'type',
  28435. ' TRec = record x: longint; end;',
  28436. ' PRec = ^TRec;',
  28437. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  28438. 'begin',
  28439. ' a.x:=a.x;',
  28440. ' a^.x:=a^.x;',
  28441. ' with a^ do',
  28442. ' x:=x;',
  28443. 'end;',
  28444. 'function GetIt(p: PRec): PRec;',
  28445. 'begin',
  28446. ' p.x:=p.x;',
  28447. ' p^.x:=p^.x;',
  28448. ' with p^ do',
  28449. ' x:=x;',
  28450. 'end;',
  28451. 'var',
  28452. ' r: TRec;',
  28453. ' p: PRec;',
  28454. 'begin',
  28455. ' p:=GetIt(p);',
  28456. ' p^:=GetIt(@r)^;',
  28457. ' DoIt(p,p,p);',
  28458. ' DoIt(@r,p,p);',
  28459. '']);
  28460. ConvertProgram;
  28461. CheckSource('TestPointer_RecordArg',
  28462. LinesToStr([ // statements
  28463. 'rtl.recNewT(this, "TRec", function () {',
  28464. ' this.x = 0;',
  28465. ' this.$eq = function (b) {',
  28466. ' return this.x === b.x;',
  28467. ' };',
  28468. ' this.$assign = function (s) {',
  28469. ' this.x = s.x;',
  28470. ' return this;',
  28471. ' };',
  28472. '});',
  28473. 'this.DoIt = function (a, b, c) {',
  28474. ' var Result = $mod.TRec.$new();',
  28475. ' a.x = a.x;',
  28476. ' a.x = a.x;',
  28477. ' a.x = a.x;',
  28478. ' return Result;',
  28479. '};',
  28480. 'this.GetIt = function (p) {',
  28481. ' var Result = null;',
  28482. ' p.x = p.x;',
  28483. ' p.x = p.x;',
  28484. ' p.x = p.x;',
  28485. ' return Result;',
  28486. '};',
  28487. 'this.r = this.TRec.$new();',
  28488. 'this.p = null;',
  28489. '']),
  28490. LinesToStr([ // $mod.$main
  28491. '$mod.p = $mod.GetIt($mod.p);',
  28492. '$mod.p.$assign($mod.GetIt($mod.r));',
  28493. '$mod.DoIt($mod.p, {',
  28494. ' p: $mod,',
  28495. ' get: function () {',
  28496. ' return this.p.p;',
  28497. ' },',
  28498. ' set: function (v) {',
  28499. ' this.p.p = v;',
  28500. ' }',
  28501. '}, {',
  28502. ' p: $mod,',
  28503. ' get: function () {',
  28504. ' return this.p.p;',
  28505. ' },',
  28506. ' set: function (v) {',
  28507. ' this.p.p = v;',
  28508. ' }',
  28509. '});',
  28510. '$mod.DoIt($mod.r, {',
  28511. ' p: $mod,',
  28512. ' get: function () {',
  28513. ' return this.p.p;',
  28514. ' },',
  28515. ' set: function (v) {',
  28516. ' this.p.p = v;',
  28517. ' }',
  28518. '}, {',
  28519. ' p: $mod,',
  28520. ' get: function () {',
  28521. ' return this.p.p;',
  28522. ' },',
  28523. ' set: function (v) {',
  28524. ' this.p.p = v;',
  28525. ' }',
  28526. '});',
  28527. '']));
  28528. end;
  28529. procedure TTestModule.TestJSValue_AssignToJSValue;
  28530. begin
  28531. StartProgram(false);
  28532. Add('var');
  28533. Add(' v: jsvalue;');
  28534. Add(' i: longint;');
  28535. Add(' s: string;');
  28536. Add(' b: boolean;');
  28537. Add(' d: double;');
  28538. Add(' p: pointer;');
  28539. Add('begin');
  28540. Add(' v:=v;');
  28541. Add(' v:=1;');
  28542. Add(' v:=i;');
  28543. Add(' v:='''';');
  28544. Add(' v:=''c'';');
  28545. Add(' v:=''foo'';');
  28546. Add(' v:=s;');
  28547. Add(' v:=false;');
  28548. Add(' v:=true;');
  28549. Add(' v:=b;');
  28550. Add(' v:=0.1;');
  28551. Add(' v:=d;');
  28552. Add(' v:=nil;');
  28553. Add(' v:=p;');
  28554. ConvertProgram;
  28555. CheckSource('TestJSValue_AssignToJSValue',
  28556. LinesToStr([ // statements
  28557. 'this.v = undefined;',
  28558. 'this.i = 0;',
  28559. 'this.s = "";',
  28560. 'this.b = false;',
  28561. 'this.d = 0.0;',
  28562. 'this.p = null;',
  28563. '']),
  28564. LinesToStr([ // $mod.$main
  28565. '$mod.v = $mod.v;',
  28566. '$mod.v = 1;',
  28567. '$mod.v = $mod.i;',
  28568. '$mod.v = "";',
  28569. '$mod.v = "c";',
  28570. '$mod.v = "foo";',
  28571. '$mod.v = $mod.s;',
  28572. '$mod.v = false;',
  28573. '$mod.v = true;',
  28574. '$mod.v = $mod.b;',
  28575. '$mod.v = 0.1;',
  28576. '$mod.v = $mod.d;',
  28577. '$mod.v = null;',
  28578. '$mod.v = $mod.p;',
  28579. '']));
  28580. end;
  28581. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  28582. begin
  28583. StartProgram(false);
  28584. Add('type');
  28585. Add(' integer = longint;');
  28586. Add(' TYesNo = boolean;');
  28587. Add(' TFloat = double;');
  28588. Add(' TCaption = string;');
  28589. Add(' TChar = char;');
  28590. Add('var');
  28591. Add(' v: jsvalue;');
  28592. Add(' i: integer;');
  28593. Add(' s: TCaption;');
  28594. Add(' b: TYesNo;');
  28595. Add(' d: TFloat;');
  28596. Add(' c: char;');
  28597. Add('begin');
  28598. Add(' i:=longint(v);');
  28599. Add(' i:=integer(v);');
  28600. Add(' s:=string(v);');
  28601. Add(' s:=TCaption(v);');
  28602. Add(' b:=boolean(v);');
  28603. Add(' b:=TYesNo(v);');
  28604. Add(' d:=double(v);');
  28605. Add(' d:=TFloat(v);');
  28606. Add(' c:=char(v);');
  28607. Add(' c:=TChar(v);');
  28608. ConvertProgram;
  28609. CheckSource('TestJSValue_TypeCastToBaseType',
  28610. LinesToStr([ // statements
  28611. 'this.v = undefined;',
  28612. 'this.i = 0;',
  28613. 'this.s = "";',
  28614. 'this.b = false;',
  28615. 'this.d = 0.0;',
  28616. 'this.c = "";',
  28617. '']),
  28618. LinesToStr([ // $mod.$main
  28619. '$mod.i = rtl.trunc($mod.v);',
  28620. '$mod.i = rtl.trunc($mod.v);',
  28621. '$mod.s = "" + $mod.v;',
  28622. '$mod.s = "" + $mod.v;',
  28623. '$mod.b = !($mod.v == false);',
  28624. '$mod.b = !($mod.v == false);',
  28625. '$mod.d = rtl.getNumber($mod.v);',
  28626. '$mod.d = rtl.getNumber($mod.v);',
  28627. '$mod.c = rtl.getChar($mod.v);',
  28628. '$mod.c = rtl.getChar($mod.v);',
  28629. '']));
  28630. end;
  28631. procedure TTestModule.TestJSValue_TypecastToJSValue;
  28632. begin
  28633. StartProgram(false);
  28634. Add([
  28635. 'type',
  28636. ' TArr = array of word;',
  28637. ' TRec = record end;',
  28638. ' TSet = set of boolean;',
  28639. 'procedure Fly(v: jsvalue);',
  28640. 'begin',
  28641. 'end;',
  28642. 'var',
  28643. ' a: TArr;',
  28644. ' r: TRec;',
  28645. ' s: TSet;',
  28646. 'begin',
  28647. ' Fly(jsvalue(a));',
  28648. ' Fly(jsvalue(r));',
  28649. ' Fly(jsvalue(s));',
  28650. '']);
  28651. ConvertProgram;
  28652. CheckSource('TestJSValue_TypecastToJSValue',
  28653. LinesToStr([ // statements
  28654. 'rtl.recNewT(this, "TRec", function () {',
  28655. ' this.$eq = function (b) {',
  28656. ' return true;',
  28657. ' };',
  28658. ' this.$assign = function (s) {',
  28659. ' return this;',
  28660. ' };',
  28661. '});',
  28662. 'this.Fly = function (v) {',
  28663. '};',
  28664. 'this.a = [];',
  28665. 'this.r = this.TRec.$new();',
  28666. 'this.s = {};',
  28667. '']),
  28668. LinesToStr([ // $mod.$main
  28669. '$mod.Fly($mod.a);',
  28670. '$mod.Fly($mod.r);',
  28671. '$mod.Fly($mod.s);',
  28672. '']));
  28673. end;
  28674. procedure TTestModule.TestJSValue_Equal;
  28675. begin
  28676. StartProgram(false);
  28677. Add('type');
  28678. Add(' integer = longint;');
  28679. Add(' TYesNo = boolean;');
  28680. Add(' TFloat = double;');
  28681. Add(' TCaption = string;');
  28682. Add(' TChar = char;');
  28683. Add(' TMulti = JSValue;');
  28684. Add('var');
  28685. Add(' v: jsvalue;');
  28686. Add(' i: integer;');
  28687. Add(' s: TCaption;');
  28688. Add(' b: TYesNo;');
  28689. Add(' d: TFloat;');
  28690. Add(' c: char;');
  28691. Add(' m: TMulti;');
  28692. Add('begin');
  28693. Add(' b:=v=v;');
  28694. Add(' b:=v<>v;');
  28695. Add(' b:=v=1;');
  28696. Add(' b:=v<>1;');
  28697. Add(' b:=2=v;');
  28698. Add(' b:=2<>v;');
  28699. Add(' b:=v=i;');
  28700. Add(' b:=i=v;');
  28701. Add(' b:=v=nil;');
  28702. Add(' b:=nil=v;');
  28703. Add(' b:=v=false;');
  28704. Add(' b:=true=v;');
  28705. Add(' b:=v=b;');
  28706. Add(' b:=b=v;');
  28707. Add(' b:=v=s;');
  28708. Add(' b:=s=v;');
  28709. Add(' b:=v=''foo'';');
  28710. Add(' b:=''''=v;');
  28711. Add(' b:=v=d;');
  28712. Add(' b:=d=v;');
  28713. Add(' b:=v=3.4;');
  28714. Add(' b:=5.6=v;');
  28715. Add(' b:=v=c;');
  28716. Add(' b:=c=v;');
  28717. Add(' b:=m=m;');
  28718. Add(' b:=v=m;');
  28719. Add(' b:=m=v;');
  28720. ConvertProgram;
  28721. CheckSource('TestJSValue_Equal',
  28722. LinesToStr([ // statements
  28723. 'this.v = undefined;',
  28724. 'this.i = 0;',
  28725. 'this.s = "";',
  28726. 'this.b = false;',
  28727. 'this.d = 0.0;',
  28728. 'this.c = "";',
  28729. 'this.m = undefined;',
  28730. '']),
  28731. LinesToStr([ // $mod.$main
  28732. '$mod.b = $mod.v == $mod.v;',
  28733. '$mod.b = $mod.v != $mod.v;',
  28734. '$mod.b = $mod.v == 1;',
  28735. '$mod.b = $mod.v != 1;',
  28736. '$mod.b = 2 == $mod.v;',
  28737. '$mod.b = 2 != $mod.v;',
  28738. '$mod.b = $mod.v == $mod.i;',
  28739. '$mod.b = $mod.i == $mod.v;',
  28740. '$mod.b = $mod.v == null;',
  28741. '$mod.b = null == $mod.v;',
  28742. '$mod.b = $mod.v == false;',
  28743. '$mod.b = true == $mod.v;',
  28744. '$mod.b = $mod.v == $mod.b;',
  28745. '$mod.b = $mod.b == $mod.v;',
  28746. '$mod.b = $mod.v == $mod.s;',
  28747. '$mod.b = $mod.s == $mod.v;',
  28748. '$mod.b = $mod.v == "foo";',
  28749. '$mod.b = "" == $mod.v;',
  28750. '$mod.b = $mod.v == $mod.d;',
  28751. '$mod.b = $mod.d == $mod.v;',
  28752. '$mod.b = $mod.v == 3.4;',
  28753. '$mod.b = 5.6 == $mod.v;',
  28754. '$mod.b = $mod.v == $mod.c;',
  28755. '$mod.b = $mod.c == $mod.v;',
  28756. '$mod.b = $mod.m == $mod.m;',
  28757. '$mod.b = $mod.v == $mod.m;',
  28758. '$mod.b = $mod.m == $mod.v;',
  28759. '']));
  28760. end;
  28761. procedure TTestModule.TestJSValue_If;
  28762. begin
  28763. StartProgram(false);
  28764. Add([
  28765. 'procedure Fly(var u);',
  28766. 'begin',
  28767. ' if jsvalue(u) then ;',
  28768. 'end;',
  28769. 'var',
  28770. ' v: jsvalue;',
  28771. 'begin',
  28772. ' if v then ;',
  28773. ' while v do ;',
  28774. ' repeat until v;',
  28775. '']);
  28776. ConvertProgram;
  28777. CheckSource('TestJSValue_If',
  28778. LinesToStr([ // statements
  28779. 'this.Fly = function (u) {',
  28780. ' if (u.get()) ;',
  28781. '};',
  28782. 'this.v = undefined;',
  28783. '']),
  28784. LinesToStr([ // $mod.$main
  28785. 'if ($mod.v) ;',
  28786. 'while($mod.v){',
  28787. '};',
  28788. 'do{',
  28789. '} while(!$mod.v);',
  28790. '']));
  28791. end;
  28792. procedure TTestModule.TestJSValue_Not;
  28793. begin
  28794. StartProgram(false);
  28795. Add([
  28796. 'var',
  28797. ' v: jsvalue;',
  28798. ' b: boolean;',
  28799. 'begin',
  28800. ' b:=not v;',
  28801. ' if not v then ;',
  28802. ' while not v do ;',
  28803. ' repeat until not v;',
  28804. '']);
  28805. ConvertProgram;
  28806. CheckSource('TestJSValue_If',
  28807. LinesToStr([ // statements
  28808. 'this.v = undefined;',
  28809. 'this.b = false;',
  28810. '']),
  28811. LinesToStr([ // $mod.$main
  28812. '$mod.b=!$mod.v;',
  28813. 'if (!$mod.v) ;',
  28814. 'while(!$mod.v){',
  28815. '};',
  28816. 'do{',
  28817. '} while($mod.v);',
  28818. '']));
  28819. end;
  28820. procedure TTestModule.TestJSValue_Enum;
  28821. begin
  28822. StartProgram(false);
  28823. Add('type');
  28824. Add(' TColor = (red, blue);');
  28825. Add(' TRedBlue = TColor;');
  28826. Add('var');
  28827. Add(' v: jsvalue;');
  28828. Add(' e: TColor;');
  28829. Add('begin');
  28830. Add(' v:=e;');
  28831. Add(' v:=TColor(e);');
  28832. Add(' v:=TRedBlue(e);');
  28833. Add(' e:=TColor(v);');
  28834. Add(' e:=TRedBlue(v);');
  28835. ConvertProgram;
  28836. CheckSource('TestJSValue_Enum',
  28837. LinesToStr([ // statements
  28838. 'this.TColor = {',
  28839. ' "0": "red",',
  28840. ' red: 0,',
  28841. ' "1": "blue",',
  28842. ' blue: 1',
  28843. '};',
  28844. 'this.v = undefined;',
  28845. 'this.e = 0;',
  28846. '']),
  28847. LinesToStr([ // $mod.$main
  28848. '$mod.v = $mod.e;',
  28849. '$mod.v = $mod.e;',
  28850. '$mod.v = $mod.e;',
  28851. '$mod.e = $mod.v;',
  28852. '$mod.e = $mod.v;',
  28853. '']));
  28854. end;
  28855. procedure TTestModule.TestJSValue_ClassInstance;
  28856. begin
  28857. StartProgram(false);
  28858. Add([
  28859. 'type',
  28860. ' TObject = class',
  28861. ' end;',
  28862. ' TBirdObject = TObject;',
  28863. 'var',
  28864. ' v: jsvalue;',
  28865. ' o: TObject;',
  28866. 'begin',
  28867. ' v:=o;',
  28868. ' v:=TObject(o);',
  28869. ' v:=TBirdObject(o);',
  28870. ' o:=TObject(v);',
  28871. ' o:=TBirdObject(v);',
  28872. ' if v is TObject then ;',
  28873. '']);
  28874. ConvertProgram;
  28875. CheckSource('TestJSValue_ClassInstance',
  28876. LinesToStr([ // statements
  28877. 'rtl.createClass(this, "TObject", null, function () {',
  28878. ' this.$init = function () {',
  28879. ' };',
  28880. ' this.$final = function () {',
  28881. ' };',
  28882. '});',
  28883. 'this.v = undefined;',
  28884. 'this.o = null;',
  28885. '']),
  28886. LinesToStr([ // $mod.$main
  28887. '$mod.v = $mod.o;',
  28888. '$mod.v = $mod.o;',
  28889. '$mod.v = $mod.o;',
  28890. '$mod.o = rtl.getObject($mod.v);',
  28891. '$mod.o = rtl.getObject($mod.v);',
  28892. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  28893. '']));
  28894. end;
  28895. procedure TTestModule.TestJSValue_ClassOf;
  28896. begin
  28897. StartProgram(false);
  28898. Add([
  28899. 'type',
  28900. ' TClass = class of TObject;',
  28901. ' TObject = class',
  28902. ' end;',
  28903. ' TBirds = class of TBird;',
  28904. ' TBird = class(TObject) end;',
  28905. 'var',
  28906. ' v: jsvalue;',
  28907. ' c: TClass;',
  28908. 'begin',
  28909. ' v:=c;',
  28910. ' v:=TObject;',
  28911. ' v:=TClass(c);',
  28912. ' v:=TBirds(c);',
  28913. ' c:=TClass(v);',
  28914. ' c:=TBirds(v);',
  28915. ' if v is TClass then ;',
  28916. '']);
  28917. ConvertProgram;
  28918. CheckSource('TestJSValue_ClassOf',
  28919. LinesToStr([ // statements
  28920. 'rtl.createClass(this, "TObject", null, function () {',
  28921. ' this.$init = function () {',
  28922. ' };',
  28923. ' this.$final = function () {',
  28924. ' };',
  28925. '});',
  28926. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28927. '});',
  28928. 'this.v = undefined;',
  28929. 'this.c = null;',
  28930. '']),
  28931. LinesToStr([ // $mod.$main
  28932. '$mod.v = $mod.c;',
  28933. '$mod.v = $mod.TObject;',
  28934. '$mod.v = $mod.c;',
  28935. '$mod.v = $mod.c;',
  28936. '$mod.c = rtl.getObject($mod.v);',
  28937. '$mod.c = rtl.getObject($mod.v);',
  28938. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  28939. '']));
  28940. end;
  28941. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  28942. begin
  28943. StartProgram(false);
  28944. Add([
  28945. 'type',
  28946. ' integer = longint;',
  28947. ' TArray = array of JSValue;',
  28948. ' TArrgh = tarray;',
  28949. ' TArrInt = array of integer;',
  28950. 'var',
  28951. ' v: jsvalue;',
  28952. ' TheArray: tarray = (1,''2'');',
  28953. ' Arr: tarrgh;',
  28954. ' i: integer;',
  28955. ' ArrInt: tarrint;',
  28956. 'begin',
  28957. ' arr:=thearray;',
  28958. ' thearray:=arr;',
  28959. ' setlength(arr,2);',
  28960. ' setlength(thearray,3);',
  28961. ' arr[4]:=v;',
  28962. ' arr[5]:=length(thearray);',
  28963. ' arr[6]:=nil;',
  28964. ' arr[7]:=thearray[8];',
  28965. ' arr[low(arr)]:=high(thearray);',
  28966. ' arr:=arrint;',
  28967. ' arrInt:=tarrint(arr);',
  28968. ' if TheArray = nil then ;',
  28969. ' if nil = TheArray then ;',
  28970. ' if TheArray <> nil then ;',
  28971. ' if nil <> TheArray then ;',
  28972. '']);
  28973. ConvertProgram;
  28974. CheckSource('TestJSValue_ArrayOfJSValue',
  28975. LinesToStr([ // statements
  28976. 'this.v = undefined;',
  28977. 'this.TheArray = [1, "2"];',
  28978. 'this.Arr = [];',
  28979. 'this.i = 0;',
  28980. 'this.ArrInt = [];',
  28981. '']),
  28982. LinesToStr([ // $mod.$main
  28983. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  28984. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  28985. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  28986. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  28987. '$mod.Arr[4] = $mod.v;',
  28988. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  28989. '$mod.Arr[6] = null;',
  28990. '$mod.Arr[7] = $mod.TheArray[8];',
  28991. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  28992. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  28993. '$mod.ArrInt = $mod.Arr;',
  28994. 'if (rtl.length($mod.TheArray) === 0) ;',
  28995. 'if (rtl.length($mod.TheArray) === 0) ;',
  28996. 'if (rtl.length($mod.TheArray) > 0) ;',
  28997. 'if (rtl.length($mod.TheArray) > 0) ;',
  28998. '']));
  28999. end;
  29000. procedure TTestModule.TestJSValue_ArrayLit;
  29001. begin
  29002. StartProgram(false);
  29003. Add([
  29004. 'type',
  29005. ' TFlag = (big,small);',
  29006. ' TArray = array of JSValue;',
  29007. ' TObject = class end;',
  29008. ' TClass = class of TObject;',
  29009. 'var',
  29010. ' v: jsvalue;',
  29011. ' a: TArray;',
  29012. ' o: TObject;',
  29013. 'begin',
  29014. ' a:=[];',
  29015. ' a:=[1];',
  29016. ' a:=[1,2];',
  29017. ' a:=[big];',
  29018. ' a:=[1,big];',
  29019. ' a:=[o,nil];',
  29020. '']);
  29021. ConvertProgram;
  29022. CheckSource('TestJSValue_ArrayLit',
  29023. LinesToStr([ // statements
  29024. 'this.TFlag = {',
  29025. ' "0": "big",',
  29026. ' big: 0,',
  29027. ' "1": "small",',
  29028. ' small: 1',
  29029. '};',
  29030. 'rtl.createClass(this, "TObject", null, function () {',
  29031. ' this.$init = function () {',
  29032. ' };',
  29033. ' this.$final = function () {',
  29034. ' };',
  29035. '});',
  29036. 'this.v = undefined;',
  29037. 'this.a = [];',
  29038. 'this.o = null;',
  29039. '']),
  29040. LinesToStr([ // $mod.$main
  29041. '$mod.a = [];',
  29042. '$mod.a = [1];',
  29043. '$mod.a = [1, 2];',
  29044. '$mod.a = [$mod.TFlag.big];',
  29045. '$mod.a = [1, $mod.TFlag.big];',
  29046. '$mod.a = [$mod.o, null];',
  29047. '']));
  29048. end;
  29049. procedure TTestModule.TestJSValue_Params;
  29050. begin
  29051. StartProgram(false);
  29052. Add('type');
  29053. Add(' integer = longint;');
  29054. Add(' TYesNo = boolean;');
  29055. Add(' TFloat = double;');
  29056. Add(' TCaption = string;');
  29057. Add(' TChar = char;');
  29058. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29059. Add('var');
  29060. Add(' l: jsvalue;');
  29061. Add('begin');
  29062. Add(' a:=a;');
  29063. Add(' l:=b;');
  29064. Add(' c:=c;');
  29065. Add(' d:=d;');
  29066. Add(' Result:=l;');
  29067. Add('end;');
  29068. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29069. Add('var');
  29070. Add(' v: jsvalue;');
  29071. Add(' i: integer;');
  29072. Add(' b: TYesNo;');
  29073. Add(' d: TFloat;');
  29074. Add(' s: TCaption;');
  29075. Add(' c: TChar;');
  29076. Add('begin');
  29077. Add(' v:=doit(v,v,v,v);');
  29078. Add(' i:=integer(dosome(i,i));');
  29079. Add(' b:=TYesNo(dosome(b,b));');
  29080. Add(' d:=TFloat(dosome(d,d));');
  29081. Add(' s:=TCaption(dosome(s,s));');
  29082. Add(' c:=TChar(dosome(c,c));');
  29083. ConvertProgram;
  29084. CheckSource('TestJSValue_Params',
  29085. LinesToStr([ // statements
  29086. 'this.DoIt = function (a, b, c, d) {',
  29087. ' var Result = undefined;',
  29088. ' var l = undefined;',
  29089. ' a = a;',
  29090. ' l = b;',
  29091. ' c.set(c.get());',
  29092. ' d.set(d.get());',
  29093. ' Result = l;',
  29094. ' return Result;',
  29095. '};',
  29096. 'this.DoSome = function (a, b) {',
  29097. ' var Result = undefined;',
  29098. ' return Result;',
  29099. '};',
  29100. 'this.v = undefined;',
  29101. 'this.i = 0;',
  29102. 'this.b = false;',
  29103. 'this.d = 0.0;',
  29104. 'this.s = "";',
  29105. 'this.c = "";',
  29106. '']),
  29107. LinesToStr([ // $mod.$main
  29108. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29109. ' p: $mod,',
  29110. ' get: function () {',
  29111. ' return this.p.v;',
  29112. ' },',
  29113. ' set: function (v) {',
  29114. ' this.p.v = v;',
  29115. ' }',
  29116. '}, {',
  29117. ' p: $mod,',
  29118. ' get: function () {',
  29119. ' return this.p.v;',
  29120. ' },',
  29121. ' set: function (v) {',
  29122. ' this.p.v = v;',
  29123. ' }',
  29124. '});',
  29125. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29126. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29127. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29128. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29129. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29130. '']));
  29131. end;
  29132. procedure TTestModule.TestJSValue_UntypedParam;
  29133. begin
  29134. StartProgram(false);
  29135. Add('function DoIt(const a; var b; out c): jsvalue;');
  29136. Add('begin');
  29137. Add(' Result:=a;');
  29138. Add(' Result:=b;');
  29139. Add(' Result:=c;');
  29140. Add(' b:=Result;');
  29141. Add(' c:=Result;');
  29142. Add('end;');
  29143. Add('var i: longint;');
  29144. Add('begin');
  29145. Add(' doit(i,i,i);');
  29146. ConvertProgram;
  29147. CheckSource('TestJSValue_UntypedParam',
  29148. LinesToStr([ // statements
  29149. 'this.DoIt = function (a, b, c) {',
  29150. ' var Result = undefined;',
  29151. ' Result = a;',
  29152. ' Result = b.get();',
  29153. ' Result = c.get();',
  29154. ' b.set(Result);',
  29155. ' c.set(Result);',
  29156. ' return Result;',
  29157. '};',
  29158. 'this.i = 0;',
  29159. '']),
  29160. LinesToStr([ // $mod.$main
  29161. '$mod.DoIt($mod.i, {',
  29162. ' p: $mod,',
  29163. ' get: function () {',
  29164. ' return this.p.i;',
  29165. ' },',
  29166. ' set: function (v) {',
  29167. ' this.p.i = v;',
  29168. ' }',
  29169. '}, {',
  29170. ' p: $mod,',
  29171. ' get: function () {',
  29172. ' return this.p.i;',
  29173. ' },',
  29174. ' set: function (v) {',
  29175. ' this.p.i = v;',
  29176. ' }',
  29177. '});',
  29178. '']));
  29179. end;
  29180. procedure TTestModule.TestJSValue_FuncResultType;
  29181. begin
  29182. StartProgram(false);
  29183. Add('type');
  29184. Add(' integer = longint;');
  29185. Add(' TJSValueArray = array of JSValue;');
  29186. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29187. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29188. Add('begin');
  29189. Add(' while Compare(P,aList[0])>0 do ;');
  29190. Add('end;');
  29191. Add('var');
  29192. Add(' Compare: TListSortCompare;');
  29193. Add(' V: JSValue;');
  29194. Add(' i: integer;');
  29195. Add('begin');
  29196. Add(' if Compare(V,V)>0 then ;');
  29197. Add(' if Compare(i,i)>1 then ;');
  29198. Add(' if Compare(nil,false)>2 then ;');
  29199. Add(' if Compare(1,true)>3 then ;');
  29200. ConvertProgram;
  29201. CheckSource('TestJSValue_UntypedParam',
  29202. LinesToStr([ // statements
  29203. 'this.Sort = function (P, aList, Compare) {',
  29204. ' while (Compare(P, aList[0]) > 0) {',
  29205. ' };',
  29206. '};',
  29207. 'this.Compare = null;',
  29208. 'this.V = undefined;',
  29209. 'this.i = 0;',
  29210. '']),
  29211. LinesToStr([ // $mod.$main
  29212. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29213. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29214. 'if ($mod.Compare(null, false) > 2) ;',
  29215. 'if ($mod.Compare(1, true) > 3) ;',
  29216. '']));
  29217. end;
  29218. procedure TTestModule.TestJSValue_ProcType_Assign;
  29219. begin
  29220. StartProgram(false);
  29221. Add('type');
  29222. Add(' integer = longint;');
  29223. Add(' TObject = class');
  29224. Add(' class function GetGlob: integer;');
  29225. Add(' function Getter: integer;');
  29226. Add(' end;');
  29227. Add('class function TObject.GetGlob: integer;');
  29228. Add('var v1: jsvalue;');
  29229. Add('begin');
  29230. Add(' v1:=@GetGlob;');
  29231. Add(' v1:[email protected];');
  29232. Add('end;');
  29233. Add('function TObject.Getter: integer;');
  29234. Add('var v2: jsvalue;');
  29235. Add('begin');
  29236. Add(' v2:=@Getter;');
  29237. Add(' v2:[email protected];');
  29238. Add(' v2:=@GetGlob;');
  29239. Add(' v2:[email protected];');
  29240. Add('end;');
  29241. Add('function GetIt(i: integer): integer;');
  29242. Add('var v3: jsvalue;');
  29243. Add('begin');
  29244. Add(' v3:=@GetIt;');
  29245. Add('end;');
  29246. Add('var');
  29247. Add(' V: JSValue;');
  29248. Add(' o: TObject;');
  29249. Add('begin');
  29250. Add(' v:=@GetIt;');
  29251. Add(' v:[email protected];');
  29252. Add(' v:[email protected];');
  29253. ConvertProgram;
  29254. CheckSource('TestJSValue_ProcType_Assign',
  29255. LinesToStr([ // statements
  29256. 'rtl.createClass(this, "TObject", null, function () {',
  29257. ' this.$init = function () {',
  29258. ' };',
  29259. ' this.$final = function () {',
  29260. ' };',
  29261. ' this.GetGlob = function () {',
  29262. ' var Result = 0;',
  29263. ' var v1 = undefined;',
  29264. ' v1 = rtl.createCallback(this, "GetGlob");',
  29265. ' v1 = rtl.createCallback(this, "GetGlob");',
  29266. ' return Result;',
  29267. ' };',
  29268. ' this.Getter = function () {',
  29269. ' var Result = 0;',
  29270. ' var v2 = undefined;',
  29271. ' v2 = rtl.createCallback(this, "Getter");',
  29272. ' v2 = rtl.createCallback(this, "Getter");',
  29273. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29274. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29275. ' return Result;',
  29276. ' };',
  29277. '});',
  29278. 'this.GetIt = function (i) {',
  29279. ' var Result = 0;',
  29280. ' var v3 = undefined;',
  29281. ' v3 = $mod.GetIt;',
  29282. ' return Result;',
  29283. '};',
  29284. 'this.V = undefined;',
  29285. 'this.o = null;',
  29286. '']),
  29287. LinesToStr([ // $mod.$main
  29288. '$mod.V = $mod.GetIt;',
  29289. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29290. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29291. '']));
  29292. end;
  29293. procedure TTestModule.TestJSValue_ProcType_Equal;
  29294. begin
  29295. StartProgram(false);
  29296. Add('type');
  29297. Add(' integer = longint;');
  29298. Add(' TObject = class');
  29299. Add(' class function GetGlob: integer;');
  29300. Add(' function Getter: integer;');
  29301. Add(' end;');
  29302. Add('class function TObject.GetGlob: integer;');
  29303. Add('var v1: jsvalue;');
  29304. Add('begin');
  29305. Add(' if v1=@GetGlob then;');
  29306. Add(' if [email protected] then ;');
  29307. Add('end;');
  29308. Add('function TObject.Getter: integer;');
  29309. Add('var v2: jsvalue;');
  29310. Add('begin');
  29311. Add(' if v2=@Getter then;');
  29312. Add(' if [email protected] then ;');
  29313. Add(' if v2=@GetGlob then;');
  29314. Add(' if [email protected] then;');
  29315. Add('end;');
  29316. Add('function GetIt(i: integer): integer;');
  29317. Add('var v3: jsvalue;');
  29318. Add('begin');
  29319. Add(' if v3=@GetIt then;');
  29320. Add('end;');
  29321. Add('var');
  29322. Add(' V: JSValue;');
  29323. Add(' o: TObject;');
  29324. Add('begin');
  29325. Add(' if v=@GetIt then;');
  29326. Add(' if [email protected] then;');
  29327. Add(' if [email protected] then;');
  29328. Add(' if @GetIt=v then;');
  29329. Add(' if @o.Getter=v then;');
  29330. Add(' if @o.GetGlob=v then;');
  29331. ConvertProgram;
  29332. CheckSource('TestJSValue_ProcType_Equal',
  29333. LinesToStr([ // statements
  29334. 'rtl.createClass(this, "TObject", null, function () {',
  29335. ' this.$init = function () {',
  29336. ' };',
  29337. ' this.$final = function () {',
  29338. ' };',
  29339. ' this.GetGlob = function () {',
  29340. ' var Result = 0;',
  29341. ' var v1 = undefined;',
  29342. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29343. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29344. ' return Result;',
  29345. ' };',
  29346. ' this.Getter = function () {',
  29347. ' var Result = 0;',
  29348. ' var v2 = undefined;',
  29349. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29350. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29351. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29352. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29353. ' return Result;',
  29354. ' };',
  29355. '});',
  29356. 'this.GetIt = function (i) {',
  29357. ' var Result = 0;',
  29358. ' var v3 = undefined;',
  29359. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29360. ' return Result;',
  29361. '};',
  29362. 'this.V = undefined;',
  29363. 'this.o = null;',
  29364. '']),
  29365. LinesToStr([ // $mod.$main
  29366. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29367. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29368. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29369. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29370. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29371. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29372. '']));
  29373. end;
  29374. procedure TTestModule.TestJSValue_ProcType_Param;
  29375. begin
  29376. StartProgram(false);
  29377. Add([
  29378. 'type',
  29379. ' variant = jsvalue;',
  29380. ' TArrVariant = array of variant;',
  29381. ' TArrVar2 = TArrVariant;',
  29382. ' TFuncInt = function: longint;',
  29383. 'function GetIt: longint;',
  29384. 'begin',
  29385. 'end;',
  29386. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29387. 'var v: variant;',
  29388. 'begin',
  29389. ' v:=arr[1];',
  29390. 'end;',
  29391. 'var s: string;',
  29392. 'begin',
  29393. ' DoIt(GetIt,[]);',
  29394. ' DoIt(@GetIt,[]);',
  29395. ' DoIt(1,[s,GetIt]);',
  29396. ' DoIt(1,[s,@GetIt]);',
  29397. '']);
  29398. ConvertProgram;
  29399. CheckSource('TestJSValue_ProcType_Param',
  29400. LinesToStr([ // statements
  29401. 'this.GetIt = function () {',
  29402. ' var Result = 0;',
  29403. ' return Result;',
  29404. '};',
  29405. 'this.DoIt = function (p, Arr) {',
  29406. ' var v = undefined;',
  29407. ' v = Arr[1];',
  29408. '};',
  29409. 'this.s = "";',
  29410. '']),
  29411. LinesToStr([ // $mod.$main
  29412. '$mod.DoIt($mod.GetIt(), []);',
  29413. '$mod.DoIt($mod.GetIt, []);',
  29414. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29415. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29416. '']));
  29417. end;
  29418. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29419. begin
  29420. StartProgram(false);
  29421. Add([
  29422. 'var',
  29423. ' v: JSValue;',
  29424. ' p: Pointer;',
  29425. 'begin',
  29426. ' p:=v;',
  29427. '']);
  29428. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29429. nIncompatibleTypesGotExpected);
  29430. ConvertProgram;
  29431. end;
  29432. procedure TTestModule.TestJSValue_OverloadDouble;
  29433. begin
  29434. StartProgram(false);
  29435. Add([
  29436. 'type',
  29437. ' integer = longint;',
  29438. ' tdatetime = double;',
  29439. 'procedure DoIt(d: double); begin end;',
  29440. 'procedure DoIt(v: jsvalue); begin end;',
  29441. 'var',
  29442. ' d: double;',
  29443. ' dt: tdatetime;',
  29444. ' i: integer;',
  29445. ' b: byte;',
  29446. ' shi: shortint;',
  29447. ' w: word;',
  29448. ' smi: smallint;',
  29449. ' lw: longword;',
  29450. ' li: longint;',
  29451. ' ni: nativeint;',
  29452. ' nu: nativeuint;',
  29453. 'begin',
  29454. ' DoIt(d);',
  29455. ' DoIt(dt);',
  29456. ' DoIt(i);',
  29457. ' DoIt(b);',
  29458. ' DoIt(shi);',
  29459. ' DoIt(w);',
  29460. ' DoIt(smi);',
  29461. ' DoIt(lw);',
  29462. ' DoIt(li);',
  29463. ' DoIt(ni);',
  29464. ' DoIt(nu);',
  29465. '']);
  29466. ConvertProgram;
  29467. CheckSource('TestJSValue_OverloadDouble',
  29468. LinesToStr([ // statements
  29469. 'this.DoIt = function (d) {',
  29470. '};',
  29471. 'this.DoIt$1 = function (v) {',
  29472. '};',
  29473. 'this.d = 0.0;',
  29474. 'this.dt = 0.0;',
  29475. 'this.i = 0;',
  29476. 'this.b = 0;',
  29477. 'this.shi = 0;',
  29478. 'this.w = 0;',
  29479. 'this.smi = 0;',
  29480. 'this.lw = 0;',
  29481. 'this.li = 0;',
  29482. 'this.ni = 0;',
  29483. 'this.nu = 0;',
  29484. '']),
  29485. LinesToStr([ // $mod.$main
  29486. '$mod.DoIt($mod.d);',
  29487. '$mod.DoIt($mod.dt);',
  29488. '$mod.DoIt$1($mod.i);',
  29489. '$mod.DoIt$1($mod.b);',
  29490. '$mod.DoIt$1($mod.shi);',
  29491. '$mod.DoIt$1($mod.w);',
  29492. '$mod.DoIt$1($mod.smi);',
  29493. '$mod.DoIt$1($mod.lw);',
  29494. '$mod.DoIt$1($mod.li);',
  29495. '$mod.DoIt$1($mod.ni);',
  29496. '$mod.DoIt$1($mod.nu);',
  29497. '']));
  29498. end;
  29499. procedure TTestModule.TestJSValue_OverloadNativeInt;
  29500. begin
  29501. StartProgram(false);
  29502. Add([
  29503. 'type',
  29504. ' integer = longint;',
  29505. ' int53 = nativeint;',
  29506. ' tdatetime = double;',
  29507. 'procedure DoIt(n: nativeint); begin end;',
  29508. 'procedure DoIt(v: jsvalue); begin end;',
  29509. 'var',
  29510. ' d: double;',
  29511. ' dt: tdatetime;',
  29512. ' i: integer;',
  29513. ' b: byte;',
  29514. ' shi: shortint;',
  29515. ' w: word;',
  29516. ' smi: smallint;',
  29517. ' lw: longword;',
  29518. ' li: longint;',
  29519. ' ni: nativeint;',
  29520. ' nu: nativeuint;',
  29521. 'begin',
  29522. ' DoIt(d);',
  29523. ' DoIt(dt);',
  29524. ' DoIt(i);',
  29525. ' DoIt(b);',
  29526. ' DoIt(shi);',
  29527. ' DoIt(w);',
  29528. ' DoIt(smi);',
  29529. ' DoIt(lw);',
  29530. ' DoIt(li);',
  29531. ' DoIt(ni);',
  29532. ' DoIt(nu);',
  29533. '']);
  29534. ConvertProgram;
  29535. CheckSource('TestJSValue_OverloadNativeInt',
  29536. LinesToStr([ // statements
  29537. 'this.DoIt = function (n) {',
  29538. '};',
  29539. 'this.DoIt$1 = function (v) {',
  29540. '};',
  29541. 'this.d = 0.0;',
  29542. 'this.dt = 0.0;',
  29543. 'this.i = 0;',
  29544. 'this.b = 0;',
  29545. 'this.shi = 0;',
  29546. 'this.w = 0;',
  29547. 'this.smi = 0;',
  29548. 'this.lw = 0;',
  29549. 'this.li = 0;',
  29550. 'this.ni = 0;',
  29551. 'this.nu = 0;',
  29552. '']),
  29553. LinesToStr([ // $mod.$main
  29554. '$mod.DoIt$1($mod.d);',
  29555. '$mod.DoIt$1($mod.dt);',
  29556. '$mod.DoIt($mod.i);',
  29557. '$mod.DoIt($mod.b);',
  29558. '$mod.DoIt($mod.shi);',
  29559. '$mod.DoIt($mod.w);',
  29560. '$mod.DoIt($mod.smi);',
  29561. '$mod.DoIt($mod.lw);',
  29562. '$mod.DoIt($mod.li);',
  29563. '$mod.DoIt($mod.ni);',
  29564. '$mod.DoIt($mod.nu);',
  29565. '']));
  29566. end;
  29567. procedure TTestModule.TestJSValue_OverloadWord;
  29568. begin
  29569. StartProgram(false);
  29570. Add([
  29571. 'type',
  29572. ' integer = longint;',
  29573. ' int53 = nativeint;',
  29574. ' tdatetime = double;',
  29575. 'procedure DoIt(w: word); begin end;',
  29576. 'procedure DoIt(v: jsvalue); begin end;',
  29577. 'var',
  29578. ' d: double;',
  29579. ' dt: tdatetime;',
  29580. ' i: integer;',
  29581. ' b: byte;',
  29582. ' shi: shortint;',
  29583. ' w: word;',
  29584. ' smi: smallint;',
  29585. ' lw: longword;',
  29586. ' li: longint;',
  29587. ' ni: nativeint;',
  29588. ' nu: nativeuint;',
  29589. 'begin',
  29590. ' DoIt(d);',
  29591. ' DoIt(dt);',
  29592. ' DoIt(i);',
  29593. ' DoIt(b);',
  29594. ' DoIt(shi);',
  29595. ' DoIt(w);',
  29596. ' DoIt(smi);',
  29597. ' DoIt(lw);',
  29598. ' DoIt(li);',
  29599. ' DoIt(ni);',
  29600. ' DoIt(nu);',
  29601. '']);
  29602. ConvertProgram;
  29603. CheckSource('TestJSValue_OverloadWord',
  29604. LinesToStr([ // statements
  29605. 'this.DoIt = function (w) {',
  29606. '};',
  29607. 'this.DoIt$1 = function (v) {',
  29608. '};',
  29609. 'this.d = 0.0;',
  29610. 'this.dt = 0.0;',
  29611. 'this.i = 0;',
  29612. 'this.b = 0;',
  29613. 'this.shi = 0;',
  29614. 'this.w = 0;',
  29615. 'this.smi = 0;',
  29616. 'this.lw = 0;',
  29617. 'this.li = 0;',
  29618. 'this.ni = 0;',
  29619. 'this.nu = 0;',
  29620. '']),
  29621. LinesToStr([ // $mod.$main
  29622. '$mod.DoIt$1($mod.d);',
  29623. '$mod.DoIt$1($mod.dt);',
  29624. '$mod.DoIt$1($mod.i);',
  29625. '$mod.DoIt($mod.b);',
  29626. '$mod.DoIt($mod.shi);',
  29627. '$mod.DoIt($mod.w);',
  29628. '$mod.DoIt$1($mod.smi);',
  29629. '$mod.DoIt$1($mod.lw);',
  29630. '$mod.DoIt$1($mod.li);',
  29631. '$mod.DoIt$1($mod.ni);',
  29632. '$mod.DoIt$1($mod.nu);',
  29633. '']));
  29634. end;
  29635. procedure TTestModule.TestJSValue_OverloadString;
  29636. begin
  29637. StartProgram(false);
  29638. Add([
  29639. 'type',
  29640. ' uni = string;',
  29641. ' WChar = char;',
  29642. 'procedure DoIt(s: string); begin end;',
  29643. 'procedure DoIt(v: jsvalue); begin end;',
  29644. 'var',
  29645. ' s: string;',
  29646. ' c: char;',
  29647. ' u: uni;',
  29648. 'begin',
  29649. ' DoIt(s);',
  29650. ' DoIt(c);',
  29651. ' DoIt(u);',
  29652. '']);
  29653. ConvertProgram;
  29654. CheckSource('TestJSValue_OverloadString',
  29655. LinesToStr([ // statements
  29656. 'this.DoIt = function (s) {',
  29657. '};',
  29658. 'this.DoIt$1 = function (v) {',
  29659. '};',
  29660. 'this.s = "";',
  29661. 'this.c = "";',
  29662. 'this.u = "";',
  29663. '']),
  29664. LinesToStr([ // $mod.$main
  29665. '$mod.DoIt($mod.s);',
  29666. '$mod.DoIt($mod.c);',
  29667. '$mod.DoIt($mod.u);',
  29668. '']));
  29669. end;
  29670. procedure TTestModule.TestJSValue_OverloadChar;
  29671. begin
  29672. StartProgram(false);
  29673. Add([
  29674. 'type',
  29675. ' uni = string;',
  29676. ' WChar = char;',
  29677. 'procedure DoIt(c: char); begin end;',
  29678. 'procedure DoIt(v: jsvalue); begin end;',
  29679. 'var',
  29680. ' s: string;',
  29681. ' c: char;',
  29682. ' u: uni;',
  29683. 'begin',
  29684. ' DoIt(s);',
  29685. ' DoIt(c);',
  29686. ' DoIt(u);',
  29687. '']);
  29688. ConvertProgram;
  29689. CheckSource('TestJSValue_OverloadChar',
  29690. LinesToStr([ // statements
  29691. 'this.DoIt = function (c) {',
  29692. '};',
  29693. 'this.DoIt$1 = function (v) {',
  29694. '};',
  29695. 'this.s = "";',
  29696. 'this.c = "";',
  29697. 'this.u = "";',
  29698. '']),
  29699. LinesToStr([ // $mod.$main
  29700. '$mod.DoIt$1($mod.s);',
  29701. '$mod.DoIt($mod.c);',
  29702. '$mod.DoIt$1($mod.u);',
  29703. '']));
  29704. end;
  29705. procedure TTestModule.TestJSValue_OverloadPointer;
  29706. begin
  29707. StartProgram(false);
  29708. Add([
  29709. 'type',
  29710. ' TObject = class end;',
  29711. 'procedure DoIt(p: pointer); begin end;',
  29712. 'procedure DoIt(v: jsvalue); begin end;',
  29713. 'var',
  29714. ' o: TObject;',
  29715. 'begin',
  29716. ' DoIt(o);',
  29717. '']);
  29718. ConvertProgram;
  29719. CheckSource('TestJSValue_OverloadPointer',
  29720. LinesToStr([ // statements
  29721. 'rtl.createClass(this, "TObject", null, function () {',
  29722. ' this.$init = function () {',
  29723. ' };',
  29724. ' this.$final = function () {',
  29725. ' };',
  29726. '});',
  29727. 'this.DoIt = function (p) {',
  29728. '};',
  29729. 'this.DoIt$1 = function (v) {',
  29730. '};',
  29731. 'this.o = null;',
  29732. '']),
  29733. LinesToStr([ // $mod.$main
  29734. '$mod.DoIt($mod.o);',
  29735. '']));
  29736. end;
  29737. procedure TTestModule.TestJSValue_ForIn;
  29738. begin
  29739. StartProgram(false);
  29740. Add([
  29741. 'var',
  29742. ' v: JSValue;',
  29743. ' key: string;',
  29744. 'begin',
  29745. ' for key in v do begin',
  29746. ' if key=''abc'' then ;',
  29747. ' end;',
  29748. '']);
  29749. ConvertProgram;
  29750. CheckSource('TestJSValue_ForIn',
  29751. LinesToStr([ // statements
  29752. 'this.v = undefined;',
  29753. 'this.key = "";',
  29754. '']),
  29755. LinesToStr([ // $mod.$main
  29756. 'for ($mod.key in $mod.v) {',
  29757. ' if ($mod.key === "abc") ;',
  29758. '};',
  29759. '']));
  29760. end;
  29761. procedure TTestModule.TestRTTI_IntRange;
  29762. begin
  29763. WithTypeInfo:=true;
  29764. StartProgram(true,[supTypeInfo]);
  29765. Add([
  29766. '{$modeswitch externalclass}',
  29767. 'type',
  29768. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  29769. ' TColor = type TGraphicsColor;',
  29770. 'var',
  29771. ' p: TTypeInfo;',
  29772. ' k: TTypeKind;',
  29773. 'begin',
  29774. ' p:=typeinfo(TGraphicsColor);',
  29775. ' p:=typeinfo(TColor);',
  29776. ' k:=GetTypeKind(TGraphicsColor);',
  29777. ' k:=GetTypeKind(TColor);',
  29778. '']);
  29779. ConvertProgram;
  29780. CheckSource('TestRTTI_IntRange',
  29781. LinesToStr([ // statements
  29782. 'this.$rtti.$Int("TGraphicsColor", {',
  29783. ' minvalue: -2147483648,',
  29784. ' maxvalue: 2147483647,',
  29785. ' ordtype: 4',
  29786. '});',
  29787. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  29788. 'this.p = null;',
  29789. 'this.k = 0;',
  29790. '']),
  29791. LinesToStr([ // $mod.$main
  29792. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  29793. '$mod.p = $mod.$rtti["TColor"];',
  29794. '$mod.k = 1;',
  29795. '$mod.k = 1;',
  29796. '']));
  29797. end;
  29798. procedure TTestModule.TestRTTI_Double;
  29799. begin
  29800. WithTypeInfo:=true;
  29801. StartProgram(true,[supTypeInfo]);
  29802. Add([
  29803. '{$modeswitch externalclass}',
  29804. 'type',
  29805. ' TFloat = type double;',
  29806. 'var',
  29807. ' p: TTypeInfo;',
  29808. 'begin',
  29809. ' p:=typeinfo(double);',
  29810. ' p:=typeinfo(TFloat);',
  29811. '']);
  29812. ConvertProgram;
  29813. CheckSource('TestRTTI_Double',
  29814. LinesToStr([ // statements
  29815. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  29816. 'this.p = null;',
  29817. '']),
  29818. LinesToStr([ // $mod.$main
  29819. '$mod.p = rtl.double;',
  29820. '$mod.p = $mod.$rtti["TFloat"];',
  29821. '']));
  29822. end;
  29823. procedure TTestModule.TestRTTI_ProcType;
  29824. begin
  29825. WithTypeInfo:=true;
  29826. StartProgram(false);
  29827. Add('type');
  29828. Add(' TProcA = procedure;');
  29829. Add(' TMethodB = procedure of object;');
  29830. Add(' TProcC = procedure; varargs;');
  29831. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  29832. Add(' TProcE = function: nativeint;');
  29833. Add(' TProcF = function(const p: TProcA): nativeuint;');
  29834. Add('var p: pointer;');
  29835. Add('begin');
  29836. Add(' p:=typeinfo(tproca);');
  29837. ConvertProgram;
  29838. CheckSource('TestRTTI_ProcType',
  29839. LinesToStr([ // statements
  29840. 'this.$rtti.$ProcVar("TProcA", {',
  29841. ' procsig: rtl.newTIProcSig([])',
  29842. '});',
  29843. 'this.$rtti.$MethodVar("TMethodB", {',
  29844. ' procsig: rtl.newTIProcSig([]),',
  29845. ' methodkind: 0',
  29846. '});',
  29847. 'this.$rtti.$ProcVar("TProcC", {',
  29848. ' procsig: rtl.newTIProcSig([], null, 2)',
  29849. '});',
  29850. 'this.$rtti.$ProcVar("TProcD", {',
  29851. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  29852. '});',
  29853. 'this.$rtti.$ProcVar("TProcE", {',
  29854. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  29855. '});',
  29856. 'this.$rtti.$ProcVar("TProcF", {',
  29857. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  29858. '});',
  29859. 'this.p = null;',
  29860. '']),
  29861. LinesToStr([ // $mod.$main
  29862. '$mod.p = $mod.$rtti["TProcA"];',
  29863. '']));
  29864. end;
  29865. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  29866. begin
  29867. WithTypeInfo:=true;
  29868. AddModuleWithIntfImplSrc('unit2.pas',
  29869. LinesToStr([
  29870. 'type',
  29871. ' TObject = class end;'
  29872. ]),
  29873. '');
  29874. StartUnit(true);
  29875. Add('interface');
  29876. Add('uses unit2;');
  29877. Add('type');
  29878. Add(' TProcA = function(o: tobject): tobject;');
  29879. Add('implementation');
  29880. Add('type');
  29881. Add(' TProcB = function(o: tobject): tobject;');
  29882. Add('var p: Pointer;');
  29883. Add('initialization');
  29884. Add(' p:=typeinfo(tproca);');
  29885. Add(' p:=typeinfo(tprocb);');
  29886. ConvertUnit;
  29887. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  29888. LinesToStr([ // statements
  29889. 'var $impl = $mod.$impl;',
  29890. 'this.$rtti.$ProcVar("TProcA", {',
  29891. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  29892. '});',
  29893. '']),
  29894. LinesToStr([ // this.$init
  29895. '$impl.p = $mod.$rtti["TProcA"];',
  29896. '$impl.p = $mod.$rtti["TProcB"];',
  29897. '']),
  29898. LinesToStr([ // implementation
  29899. '$mod.$rtti.$ProcVar("TProcB", {',
  29900. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  29901. '});',
  29902. '$impl.p = null;',
  29903. '']) );
  29904. end;
  29905. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  29906. begin
  29907. WithTypeInfo:=true;
  29908. StartProgram(false);
  29909. Add(['var',
  29910. ' ProcA: procedure;',
  29911. ' MethodB: procedure of object;',
  29912. ' ProcC: procedure; varargs;',
  29913. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  29914. ' ProcE: function: nativeint;',
  29915. ' p: pointer;',
  29916. 'begin',
  29917. ' p:=typeinfo(proca);']);
  29918. ConvertProgram;
  29919. CheckSource('TestRTTI_ProcTypeAnonymous',
  29920. LinesToStr([ // statements
  29921. 'this.$rtti.$ProcVar("ProcA$a", {',
  29922. ' procsig: rtl.newTIProcSig([])',
  29923. '});',
  29924. 'this.ProcA = null;',
  29925. 'this.$rtti.$MethodVar("MethodB$a", {',
  29926. ' procsig: rtl.newTIProcSig([]),',
  29927. ' methodkind: 0',
  29928. '});',
  29929. 'this.MethodB = null;',
  29930. 'this.$rtti.$ProcVar("ProcC$a", {',
  29931. ' procsig: rtl.newTIProcSig([], null, 2)',
  29932. '});',
  29933. 'this.ProcC = null;',
  29934. 'this.$rtti.$ProcVar("ProcD$a", {',
  29935. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  29936. '});',
  29937. 'this.ProcD = null;',
  29938. 'this.$rtti.$ProcVar("ProcE$a", {',
  29939. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  29940. '});',
  29941. 'this.ProcE = null;',
  29942. 'this.p = null;',
  29943. '']),
  29944. LinesToStr([ // $mod.$main
  29945. '$mod.p = $mod.$rtti["ProcA$a"];',
  29946. '']));
  29947. end;
  29948. procedure TTestModule.TestRTTI_EnumAndSetType;
  29949. begin
  29950. WithTypeInfo:=true;
  29951. StartProgram(false);
  29952. Add('type');
  29953. Add(' TFlag = (light,dark);');
  29954. Add(' TFlags = set of TFlag;');
  29955. Add(' TProc = function(f: TFlags): TFlag;');
  29956. Add('var p: pointer;');
  29957. Add('begin');
  29958. Add(' p:=typeinfo(tflag);');
  29959. Add(' p:=typeinfo(tflags);');
  29960. ConvertProgram;
  29961. CheckSource('TestRTTI_EnumAndType',
  29962. LinesToStr([ // statements
  29963. 'this.TFlag = {',
  29964. ' "0": "light",',
  29965. ' light: 0,',
  29966. ' "1": "dark",',
  29967. ' dark: 1',
  29968. '};',
  29969. 'this.$rtti.$Enum("TFlag", {',
  29970. ' minvalue: 0,',
  29971. ' maxvalue: 1,',
  29972. ' ordtype: 1,',
  29973. ' enumtype: this.TFlag',
  29974. '});',
  29975. 'this.$rtti.$Set("TFlags", {',
  29976. ' comptype: this.$rtti["TFlag"]',
  29977. '});',
  29978. 'this.$rtti.$ProcVar("TProc", {',
  29979. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  29980. '});',
  29981. 'this.p = null;',
  29982. '']),
  29983. LinesToStr([ // $mod.$main
  29984. '$mod.p = $mod.$rtti["TFlag"];',
  29985. '$mod.p = $mod.$rtti["TFlags"];',
  29986. '']));
  29987. end;
  29988. procedure TTestModule.TestRTTI_EnumRange;
  29989. begin
  29990. WithTypeInfo:=true;
  29991. StartProgram(false);
  29992. Add([
  29993. 'type',
  29994. ' TCol = (red,green,blue);',
  29995. ' TColRg = green..blue;',
  29996. ' TSetOfColRg = set of TColRg;',
  29997. 'var p: pointer;',
  29998. 'begin',
  29999. ' p:=typeinfo(tcolrg);',
  30000. ' p:=typeinfo(tsetofcolrg);',
  30001. '']);
  30002. ConvertProgram;
  30003. end;
  30004. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30005. begin
  30006. WithTypeInfo:=true;
  30007. StartProgram(false);
  30008. Add('type');
  30009. Add(' TFlags = set of (red, green);');
  30010. Add('var');
  30011. Add(' f: TFlags;');
  30012. Add('begin');
  30013. Add(' Include(f,red);');
  30014. ConvertProgram;
  30015. CheckSource('TestRTTI_AnonymousEnumType',
  30016. LinesToStr([ // statements
  30017. 'this.TFlags$a = {',
  30018. ' "0": "red",',
  30019. ' red: 0,',
  30020. ' "1": "green",',
  30021. ' green: 1',
  30022. '};',
  30023. 'this.$rtti.$Enum("TFlags$a", {',
  30024. ' minvalue: 0,',
  30025. ' maxvalue: 1,',
  30026. ' ordtype: 1,',
  30027. ' enumtype: this.TFlags$a',
  30028. '});',
  30029. 'this.$rtti.$Set("TFlags", {',
  30030. ' comptype: this.$rtti["TFlags$a"]',
  30031. '});',
  30032. 'this.f = {};',
  30033. '']),
  30034. LinesToStr([
  30035. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30036. '']));
  30037. end;
  30038. procedure TTestModule.TestRTTI_StaticArray;
  30039. begin
  30040. WithTypeInfo:=true;
  30041. StartProgram(false);
  30042. Add('type');
  30043. Add(' TFlag = (light,dark);');
  30044. Add(' TFlagNames = array[TFlag] of string;');
  30045. Add(' TBoolNames = array[boolean] of string;');
  30046. Add(' TByteArray = array[1..32768] of byte;');
  30047. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30048. Add('var p: pointer;');
  30049. Add('begin');
  30050. Add(' p:=typeinfo(TFlagNames);');
  30051. Add(' p:=typeinfo(TBoolNames);');
  30052. ConvertProgram;
  30053. CheckSource('TestRTTI_StaticArray',
  30054. LinesToStr([ // statements
  30055. 'this.TFlag = {',
  30056. ' "0": "light",',
  30057. ' light: 0,',
  30058. ' "1": "dark",',
  30059. ' dark: 1',
  30060. '};',
  30061. 'this.$rtti.$Enum("TFlag", {',
  30062. ' minvalue: 0,',
  30063. ' maxvalue: 1,',
  30064. ' ordtype: 1,',
  30065. ' enumtype: this.TFlag',
  30066. '});',
  30067. 'this.$rtti.$StaticArray("TFlagNames", {',
  30068. ' dims: [2],',
  30069. ' eltype: rtl.string',
  30070. '});',
  30071. 'this.$rtti.$StaticArray("TBoolNames", {',
  30072. ' dims: [2],',
  30073. ' eltype: rtl.string',
  30074. '});',
  30075. 'this.$rtti.$StaticArray("TByteArray", {',
  30076. ' dims: [32768],',
  30077. ' eltype: rtl.byte',
  30078. '});',
  30079. 'this.$rtti.$ProcVar("TProc", {',
  30080. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30081. '});',
  30082. 'this.p = null;',
  30083. '']),
  30084. LinesToStr([ // $mod.$main
  30085. '$mod.p = $mod.$rtti["TFlagNames"];',
  30086. '$mod.p = $mod.$rtti["TBoolNames"];',
  30087. '']));
  30088. end;
  30089. procedure TTestModule.TestRTTI_DynArray;
  30090. begin
  30091. WithTypeInfo:=true;
  30092. StartProgram(false);
  30093. Add('type');
  30094. Add(' TArrStr = array of string;');
  30095. Add(' TArr2Dim = array of tarrstr;');
  30096. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30097. Add('var p: pointer;');
  30098. Add('begin');
  30099. Add(' p:=typeinfo(tarrstr);');
  30100. Add(' p:=typeinfo(tarr2dim);');
  30101. ConvertProgram;
  30102. CheckSource('TestRTTI_DynArray',
  30103. LinesToStr([ // statements
  30104. 'this.$rtti.$DynArray("TArrStr", {',
  30105. ' eltype: rtl.string',
  30106. '});',
  30107. 'this.$rtti.$DynArray("TArr2Dim", {',
  30108. ' eltype: this.$rtti["TArrStr"]',
  30109. '});',
  30110. 'this.$rtti.$ProcVar("TProc", {',
  30111. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30112. '});',
  30113. 'this.p = null;',
  30114. '']),
  30115. LinesToStr([ // $mod.$main
  30116. '$mod.p = $mod.$rtti["TArrStr"];',
  30117. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30118. '']));
  30119. end;
  30120. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30121. begin
  30122. WithTypeInfo:=true;
  30123. StartProgram(false);
  30124. Add('type');
  30125. Add(' TArr = array of array of longint;');
  30126. Add('var a: TArr;');
  30127. Add('begin');
  30128. ConvertProgram;
  30129. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30130. LinesToStr([ // statements
  30131. 'this.$rtti.$DynArray("TArr$a", {',
  30132. ' eltype: rtl.longint',
  30133. '});',
  30134. 'this.$rtti.$DynArray("TArr", {',
  30135. ' eltype: this.$rtti["TArr$a"]',
  30136. '});',
  30137. 'this.a = [];',
  30138. '']),
  30139. LinesToStr([ // $mod.$main
  30140. ]));
  30141. end;
  30142. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30143. begin
  30144. WithTypeInfo:=true;
  30145. StartProgram(false);
  30146. Add('type');
  30147. Add(' TObject = class');
  30148. Add(' published');
  30149. Add(' procedure Proc; virtual; abstract;');
  30150. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30151. Add(' end;');
  30152. Add('begin');
  30153. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30154. nDuplicatePublishedMethodXAtY);
  30155. ConvertProgram;
  30156. end;
  30157. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30158. begin
  30159. WithTypeInfo:=true;
  30160. StartUnit(false);
  30161. Add([
  30162. 'interface',
  30163. 'type',
  30164. ' TObject = class',
  30165. ' end;',
  30166. ' {$M+}',
  30167. ' TBird = class',
  30168. ' procedure Fly;',
  30169. ' end;',
  30170. ' {$M-}',
  30171. 'type',
  30172. ' TEagle = class(TBird)',
  30173. ' procedure Fly;',
  30174. ' end;',
  30175. 'implementation',
  30176. 'procedure TBird.Fly;',
  30177. 'begin',
  30178. 'end;',
  30179. 'procedure TEagle.Fly;',
  30180. 'begin',
  30181. 'end;',
  30182. '']);
  30183. ConvertUnit;
  30184. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30185. LinesToStr([ // statements
  30186. 'rtl.createClass(this, "TObject", null, function () {',
  30187. ' this.$init = function () {',
  30188. ' };',
  30189. ' this.$final = function () {',
  30190. ' };',
  30191. '});',
  30192. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30193. ' this.Fly = function () {',
  30194. ' };',
  30195. ' var $r = this.$rtti;',
  30196. ' $r.addMethod("Fly", 0, []);',
  30197. '});',
  30198. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30199. ' this.Fly = function () {',
  30200. ' };',
  30201. ' var $r = this.$rtti;',
  30202. ' $r.addMethod("Fly", 0, []);',
  30203. '});',
  30204. '']),
  30205. LinesToStr([ // $mod.$main
  30206. ]));
  30207. CheckResolverUnexpectedHints(true);
  30208. end;
  30209. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30210. begin
  30211. WithTypeInfo:=true;
  30212. StartProgram(false);
  30213. Add('type');
  30214. Add(' TObject = class');
  30215. Add(' published');
  30216. Add(' procedure Proc; external name ''foo'';');
  30217. Add(' end;');
  30218. Add('begin');
  30219. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30220. nPublishedNameMustMatchExternal);
  30221. ConvertProgram;
  30222. end;
  30223. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30224. begin
  30225. WithTypeInfo:=true;
  30226. StartProgram(false);
  30227. Add('type');
  30228. Add(' TObject = class');
  30229. Add(' class var FA: longint;');
  30230. Add(' published');
  30231. Add(' class property A: longint read FA;');
  30232. Add(' end;');
  30233. Add('begin');
  30234. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30235. nInvalidXModifierY);
  30236. ConvertProgram;
  30237. end;
  30238. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30239. begin
  30240. WithTypeInfo:=true;
  30241. StartProgram(false);
  30242. Add('type');
  30243. Add(' TObject = class');
  30244. Add(' published');
  30245. Add(' class var FA: longint;');
  30246. Add(' end;');
  30247. Add('begin');
  30248. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30249. nSymbolCannotBePublished);
  30250. ConvertProgram;
  30251. end;
  30252. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30253. begin
  30254. WithTypeInfo:=true;
  30255. StartProgram(false);
  30256. Add('{$modeswitch externalclass}');
  30257. Add('type');
  30258. Add(' TObject = class');
  30259. Add(' published');
  30260. Add(' V: longint; external name ''foo'';');
  30261. Add(' end;');
  30262. Add('begin');
  30263. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30264. nPublishedNameMustMatchExternal);
  30265. ConvertProgram;
  30266. end;
  30267. procedure TTestModule.TestRTTI_Class_Field;
  30268. begin
  30269. WithTypeInfo:=true;
  30270. StartProgram(false);
  30271. Add('{$modeswitch externalclass}');
  30272. Add('type');
  30273. Add(' TObject = class');
  30274. Add(' private');
  30275. Add(' FPropA: string;');
  30276. Add(' published');
  30277. Add(' VarLI: longint;');
  30278. Add(' VarC: char;');
  30279. Add(' VarS: string;');
  30280. Add(' VarD: double;');
  30281. Add(' VarB: boolean;');
  30282. Add(' VarLW: longword;');
  30283. Add(' VarSmI: smallint;');
  30284. Add(' VarW: word;');
  30285. Add(' VarShI: shortint;');
  30286. Add(' VarBy: byte;');
  30287. Add(' VarExt: longint external name ''VarExt'';');
  30288. Add(' ArrA, ArrB: array of byte;');
  30289. Add(' end;');
  30290. Add('var p: pointer;');
  30291. Add(' Obj: tobject;');
  30292. Add('begin');
  30293. Add(' p:=typeinfo(tobject);');
  30294. Add(' p:=typeinfo(p);');
  30295. Add(' p:=typeinfo(obj);');
  30296. ConvertProgram;
  30297. CheckSource('TestRTTI_Class_Field',
  30298. LinesToStr([ // statements
  30299. 'rtl.createClass(this, "TObject", null, function () {',
  30300. ' this.$init = function () {',
  30301. ' this.FPropA = "";',
  30302. ' this.VarLI = 0;',
  30303. ' this.VarC = "";',
  30304. ' this.VarS = "";',
  30305. ' this.VarD = 0.0;',
  30306. ' this.VarB = false;',
  30307. ' this.VarLW = 0;',
  30308. ' this.VarSmI = 0;',
  30309. ' this.VarW = 0;',
  30310. ' this.VarShI = 0;',
  30311. ' this.VarBy = 0;',
  30312. ' this.ArrA = [];',
  30313. ' this.ArrB = [];',
  30314. ' };',
  30315. ' this.$final = function () {',
  30316. ' this.ArrA = undefined;',
  30317. ' this.ArrB = undefined;',
  30318. ' };',
  30319. ' var $r = this.$rtti;',
  30320. ' $r.addField("VarLI", rtl.longint);',
  30321. ' $r.addField("VarC", rtl.char);',
  30322. ' $r.addField("VarS", rtl.string);',
  30323. ' $r.addField("VarD", rtl.double);',
  30324. ' $r.addField("VarB", rtl.boolean);',
  30325. ' $r.addField("VarLW", rtl.longword);',
  30326. ' $r.addField("VarSmI", rtl.smallint);',
  30327. ' $r.addField("VarW", rtl.word);',
  30328. ' $r.addField("VarShI", rtl.shortint);',
  30329. ' $r.addField("VarBy", rtl.byte);',
  30330. ' $r.addField("VarExt", rtl.longint);',
  30331. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30332. ' eltype: rtl.byte',
  30333. ' });',
  30334. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30335. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30336. '});',
  30337. 'this.p = null;',
  30338. 'this.Obj = null;',
  30339. '']),
  30340. LinesToStr([ // $mod.$main
  30341. '$mod.p = $mod.$rtti["TObject"];',
  30342. '$mod.p = rtl.pointer;',
  30343. '$mod.p = $mod.Obj.$rtti;',
  30344. '']));
  30345. end;
  30346. procedure TTestModule.TestRTTI_Class_Method;
  30347. begin
  30348. WithTypeInfo:=true;
  30349. StartProgram(false);
  30350. Add([
  30351. 'type',
  30352. ' TObject = class',
  30353. ' private',
  30354. ' procedure Internal; external name ''$intern'';',
  30355. ' published',
  30356. ' procedure Click; virtual; abstract;',
  30357. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30358. ' function GetNotify: boolean; external name ''GetNotify'';',
  30359. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30360. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30361. ' end;',
  30362. 'begin']);
  30363. ConvertProgram;
  30364. CheckSource('TestRTTI_Class_Method',
  30365. LinesToStr([ // statements
  30366. 'rtl.createClass(this, "TObject", null, function () {',
  30367. ' this.$init = function () {',
  30368. ' };',
  30369. ' this.$final = function () {',
  30370. ' };',
  30371. ' var $r = this.$rtti;',
  30372. ' $r.addMethod("Click", 0, []);',
  30373. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30374. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30375. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30376. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30377. '});',
  30378. '']),
  30379. LinesToStr([ // $mod.$main
  30380. '']));
  30381. end;
  30382. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30383. begin
  30384. WithTypeInfo:=true;
  30385. StartProgram(false);
  30386. Add('type');
  30387. Add(' TObject = class');
  30388. Add(' published');
  30389. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30390. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30391. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30392. Add(' end;');
  30393. Add('begin');
  30394. ConvertProgram;
  30395. CheckSource('TestRTTI_Class_MethodOpenArray',
  30396. LinesToStr([ // statements
  30397. 'rtl.createClass(this, "TObject", null, function () {',
  30398. ' this.$init = function () {',
  30399. ' };',
  30400. ' this.$final = function () {',
  30401. ' };',
  30402. ' var $r = this.$rtti;',
  30403. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30404. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30405. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30406. '});',
  30407. '']),
  30408. LinesToStr([ // $mod.$main
  30409. '']));
  30410. end;
  30411. procedure TTestModule.TestRTTI_Class_Property;
  30412. begin
  30413. WithTypeInfo:=true;
  30414. StartProgram(false);
  30415. Add('{$modeswitch externalclass}');
  30416. Add('type');
  30417. Add(' TObject = class');
  30418. Add(' private');
  30419. Add(' FColor: longint;');
  30420. Add(' FColorStored: boolean;');
  30421. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30422. Add(' function GetColor: longint; virtual; abstract;');
  30423. Add(' function GetColorStored: boolean; virtual; abstract;');
  30424. Add(' FExtSize: longint external name ''$extSize'';');
  30425. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30426. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30427. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30428. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30429. Add(' published');
  30430. Add(' property ColorA: longint read FColor;');
  30431. Add(' property ColorB: longint write FColor;');
  30432. Add(' property ColorC: longint read GetColor write SetColor;');
  30433. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  30434. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  30435. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  30436. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  30437. Add(' end;');
  30438. Add('begin');
  30439. ConvertProgram;
  30440. CheckSource('TestRTTI_Class_Property',
  30441. LinesToStr([ // statements
  30442. 'rtl.createClass(this, "TObject", null, function () {',
  30443. ' this.$init = function () {',
  30444. ' this.FColor = 0;',
  30445. ' this.FColorStored = false;',
  30446. ' };',
  30447. ' this.$final = function () {',
  30448. ' };',
  30449. ' var $r = this.$rtti;',
  30450. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  30451. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  30452. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  30453. ' $r.addProperty(',
  30454. ' "ColorD",',
  30455. ' 8,',
  30456. ' rtl.longint,',
  30457. ' "FColor",',
  30458. ' "FColor",',
  30459. ' {',
  30460. ' stored: "FColorStored"',
  30461. ' }',
  30462. ' );',
  30463. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  30464. ' $r.addProperty(',
  30465. ' "ExtSizeB",',
  30466. ' 11,',
  30467. ' rtl.longint,',
  30468. ' "$getSize",',
  30469. ' "$setSize",',
  30470. ' {',
  30471. ' stored: "$extSizeStored"',
  30472. ' }',
  30473. ' );',
  30474. ' $r.addProperty(',
  30475. ' "ExtSizeC",',
  30476. ' 12,',
  30477. ' rtl.longint,',
  30478. ' "$extSize",',
  30479. ' "$extSize",',
  30480. ' {',
  30481. ' stored: "$getExtSizeStored"',
  30482. ' }',
  30483. ' );',
  30484. '});',
  30485. '']),
  30486. LinesToStr([ // $mod.$main
  30487. '']));
  30488. end;
  30489. procedure TTestModule.TestRTTI_Class_PropertyParams;
  30490. begin
  30491. WithTypeInfo:=true;
  30492. StartProgram(false);
  30493. Add('{$modeswitch externalclass}');
  30494. Add('type');
  30495. Add(' integer = longint;');
  30496. Add(' TObject = class');
  30497. Add(' private');
  30498. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  30499. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  30500. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  30501. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  30502. Add(' published');
  30503. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  30504. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  30505. Add(' end;');
  30506. Add('begin');
  30507. ConvertProgram;
  30508. CheckSource('TestRTTI_Class_PropertyParams',
  30509. LinesToStr([ // statements
  30510. 'rtl.createClass(this, "TObject", null, function () {',
  30511. ' this.$init = function () {',
  30512. ' };',
  30513. ' this.$final = function () {',
  30514. ' };',
  30515. ' var $r = this.$rtti;',
  30516. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  30517. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  30518. '});',
  30519. '']),
  30520. LinesToStr([ // $mod.$main
  30521. '']));
  30522. end;
  30523. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  30524. begin
  30525. WithTypeInfo:=true;
  30526. AddModuleWithIntfImplSrc('unit1.pas',
  30527. 'type TColor = -5..5;',
  30528. '');
  30529. StartProgram(true);
  30530. Add([
  30531. 'uses unit1;',
  30532. 'type',
  30533. ' TColorAlias = TColor;',
  30534. ' TColorTypeAlias = type TColor;',
  30535. ' TObject = class',
  30536. ' private',
  30537. ' fColor: TColor;',
  30538. ' fAlias: TColorAlias;',
  30539. ' fTypeAlias: TColorTypeAlias;',
  30540. ' published',
  30541. ' property Color: TColor read fcolor;',
  30542. ' property Alias: TColorAlias read falias;',
  30543. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  30544. ' end;',
  30545. 'begin',
  30546. '']);
  30547. ConvertProgram;
  30548. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  30549. LinesToStr([ // statements
  30550. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  30551. 'rtl.createClass(this, "TObject", null, function () {',
  30552. ' this.$init = function () {',
  30553. ' this.fColor = 0;',
  30554. ' this.fAlias = 0;',
  30555. ' this.fTypeAlias = 0;',
  30556. ' };',
  30557. ' this.$final = function () {',
  30558. ' };',
  30559. ' var $r = this.$rtti;',
  30560. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  30561. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  30562. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  30563. '});',
  30564. '']),
  30565. LinesToStr([ // $mod.$main
  30566. '']));
  30567. end;
  30568. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  30569. begin
  30570. WithTypeInfo:=true;
  30571. StartProgram(false);
  30572. Add([
  30573. '{$modeswitch omitrtti}',
  30574. 'type',
  30575. ' TObject = class',
  30576. ' private',
  30577. ' FA: byte;',
  30578. ' published',
  30579. ' property A: byte read FA write FA;',
  30580. ' end;',
  30581. 'begin']);
  30582. ConvertProgram;
  30583. CheckSource('TestRTTI_Class_OmitRTTI',
  30584. LinesToStr([ // statements
  30585. 'rtl.createClass(this, "TObject", null, function () {',
  30586. ' this.$init = function () {',
  30587. ' this.FA = 0;',
  30588. ' };',
  30589. ' this.$final = function () {',
  30590. ' };',
  30591. '});',
  30592. '']),
  30593. LinesToStr([ // $mod.$main
  30594. '']));
  30595. end;
  30596. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  30597. begin
  30598. WithTypeInfo:=true;
  30599. StartUnit(true,[supTObject]);
  30600. Add([
  30601. 'interface',
  30602. 'type',
  30603. ' {$M+}',
  30604. ' TBird = class',
  30605. ' published',
  30606. ' Swarm: array of TBird;',
  30607. ' end;',
  30608. 'implementation',
  30609. '']);
  30610. ConvertUnit;
  30611. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  30612. LinesToStr([ // statements
  30613. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  30614. ' this.$init = function () {',
  30615. ' pas.system.TObject.$init.call(this);',
  30616. ' this.Swarm = [];',
  30617. ' };',
  30618. ' this.$final = function () {',
  30619. ' this.Swarm = undefined;',
  30620. ' pas.system.TObject.$final.call(this);',
  30621. ' };',
  30622. ' var $r = this.$rtti;',
  30623. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  30624. ' eltype: $r',
  30625. ' });',
  30626. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  30627. '});',
  30628. '']),
  30629. LinesToStr([ // $mod.$main
  30630. '']));
  30631. end;
  30632. procedure TTestModule.TestRTTI_IndexModifier;
  30633. begin
  30634. WithTypeInfo:=true;
  30635. StartProgram(false);
  30636. Add([
  30637. 'type',
  30638. ' TEnum = (red, blue);',
  30639. ' TObject = class',
  30640. ' FB: boolean;',
  30641. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  30642. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  30643. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  30644. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  30645. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  30646. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  30647. ' published',
  30648. ' property B1: boolean index 1 read FB write SetIntBool;',
  30649. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  30650. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  30651. ' end;',
  30652. 'begin']);
  30653. ConvertProgram;
  30654. CheckSource('TestRTTI_IndexModifier',
  30655. LinesToStr([ // statements
  30656. 'this.TEnum = {',
  30657. ' "0": "red",',
  30658. ' red: 0,',
  30659. ' "1": "blue",',
  30660. ' blue: 1',
  30661. '};',
  30662. 'this.$rtti.$Enum("TEnum", {',
  30663. ' minvalue: 0,',
  30664. ' maxvalue: 1,',
  30665. ' ordtype: 1,',
  30666. ' enumtype: this.TEnum',
  30667. '});',
  30668. 'rtl.createClass(this, "TObject", null, function () {',
  30669. ' this.$init = function () {',
  30670. ' this.FB = false;',
  30671. ' };',
  30672. ' this.$final = function () {',
  30673. ' };',
  30674. ' var $r = this.$rtti;',
  30675. ' $r.addProperty(',
  30676. ' "B1",',
  30677. ' 18,',
  30678. ' rtl.boolean,',
  30679. ' "FB",',
  30680. ' "SetIntBool",',
  30681. ' {',
  30682. ' index: 1',
  30683. ' }',
  30684. ' );',
  30685. ' $r.addProperty(',
  30686. ' "B2",',
  30687. ' 17,',
  30688. ' rtl.boolean,',
  30689. ' "GetEnumBool",',
  30690. ' "FB",',
  30691. ' {',
  30692. ' index: $mod.TEnum.blue',
  30693. ' }',
  30694. ' );',
  30695. ' $r.addProperty(',
  30696. ' "I1",',
  30697. ' 19,',
  30698. ' rtl.boolean,',
  30699. ' "GetStrIntBool",',
  30700. ' "SetStrIntBool",',
  30701. ' {',
  30702. ' index: 2',
  30703. ' }',
  30704. ' );',
  30705. '});',
  30706. '']),
  30707. LinesToStr([ // $mod.$main
  30708. '']));
  30709. end;
  30710. procedure TTestModule.TestRTTI_StoredModifier;
  30711. begin
  30712. WithTypeInfo:=true;
  30713. StartProgram(false);
  30714. Add([
  30715. 'const',
  30716. ' ConstB = true;',
  30717. 'type',
  30718. ' TObject = class',
  30719. ' private',
  30720. ' FB: boolean;',
  30721. ' function IsBStored: boolean; virtual; abstract;',
  30722. ' published',
  30723. ' property BoolA: boolean read FB stored true;',
  30724. ' property BoolB: boolean read FB stored false;',
  30725. ' property BoolC: boolean read FB stored FB;',
  30726. ' property BoolD: boolean read FB stored ConstB;',
  30727. ' property BoolE: boolean read FB stored IsBStored;',
  30728. ' end;',
  30729. 'begin']);
  30730. ConvertProgram;
  30731. CheckSource('TestRTTI_StoredModifier',
  30732. LinesToStr([ // statements
  30733. 'this.ConstB = true;',
  30734. 'rtl.createClass(this, "TObject", null, function () {',
  30735. ' this.$init = function () {',
  30736. ' this.FB = false;',
  30737. ' };',
  30738. ' this.$final = function () {',
  30739. ' };',
  30740. ' var $r = this.$rtti;',
  30741. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  30742. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  30743. ' $r.addProperty(',
  30744. ' "BoolC",',
  30745. ' 8,',
  30746. ' rtl.boolean,',
  30747. ' "FB",',
  30748. ' "",',
  30749. ' {',
  30750. ' stored: "FB"',
  30751. ' }',
  30752. ' );',
  30753. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  30754. ' $r.addProperty(',
  30755. ' "BoolE",',
  30756. ' 12,',
  30757. ' rtl.boolean,',
  30758. ' "FB",',
  30759. ' "",',
  30760. ' {',
  30761. ' stored: "IsBStored"',
  30762. ' }',
  30763. ' );',
  30764. '});',
  30765. '']),
  30766. LinesToStr([ // $mod.$main
  30767. '']));
  30768. end;
  30769. procedure TTestModule.TestRTTI_DefaultValue;
  30770. begin
  30771. WithTypeInfo:=true;
  30772. StartProgram(false);
  30773. Add([
  30774. 'type',
  30775. ' TEnum = (red, blue);',
  30776. 'const',
  30777. ' CB = true or false;',
  30778. ' CI = 1+2;',
  30779. 'type',
  30780. ' TObject = class',
  30781. ' FB: boolean;',
  30782. ' FI: longint;',
  30783. ' FE: TEnum;',
  30784. ' published',
  30785. ' property B1: boolean read FB default true;',
  30786. ' property B2: boolean read FB default CB;',
  30787. ' property B3: boolean read FB default test1.cb;',
  30788. ' property I1: longint read FI default 2;',
  30789. ' property I2: longint read FI default CI;',
  30790. ' property E1: TEnum read FE default red;',
  30791. ' property E2: TEnum read FE default TEnum.blue;',
  30792. ' end;',
  30793. 'begin']);
  30794. ConvertProgram;
  30795. CheckSource('TestRTTI_DefaultValue',
  30796. LinesToStr([ // statements
  30797. 'this.TEnum = {',
  30798. ' "0": "red",',
  30799. ' red: 0,',
  30800. ' "1": "blue",',
  30801. ' blue: 1',
  30802. '};',
  30803. 'this.$rtti.$Enum("TEnum", {',
  30804. ' minvalue: 0,',
  30805. ' maxvalue: 1,',
  30806. ' ordtype: 1,',
  30807. ' enumtype: this.TEnum',
  30808. '});',
  30809. 'this.CB = true || false;',
  30810. 'this.CI = 1 + 2;',
  30811. 'rtl.createClass(this, "TObject", null, function () {',
  30812. ' this.$init = function () {',
  30813. ' this.FB = false;',
  30814. ' this.FI = 0;',
  30815. ' this.FE = 0;',
  30816. ' };',
  30817. ' this.$final = function () {',
  30818. ' };',
  30819. ' var $r = this.$rtti;',
  30820. ' $r.addProperty(',
  30821. ' "B1",',
  30822. ' 0,',
  30823. ' rtl.boolean,',
  30824. ' "FB",',
  30825. ' "",',
  30826. ' {',
  30827. ' Default: true',
  30828. ' }',
  30829. ' );',
  30830. ' $r.addProperty(',
  30831. ' "B2",',
  30832. ' 0,',
  30833. ' rtl.boolean,',
  30834. ' "FB",',
  30835. ' "",',
  30836. ' {',
  30837. ' Default: true',
  30838. ' }',
  30839. ' );',
  30840. ' $r.addProperty(',
  30841. ' "B3",',
  30842. ' 0,',
  30843. ' rtl.boolean,',
  30844. ' "FB",',
  30845. ' "",',
  30846. ' {',
  30847. ' Default: true',
  30848. ' }',
  30849. ' );',
  30850. ' $r.addProperty(',
  30851. ' "I1",',
  30852. ' 0,',
  30853. ' rtl.longint,',
  30854. ' "FI",',
  30855. ' "",',
  30856. ' {',
  30857. ' Default: 2',
  30858. ' }',
  30859. ' );',
  30860. ' $r.addProperty(',
  30861. ' "I2",',
  30862. ' 0,',
  30863. ' rtl.longint,',
  30864. ' "FI",',
  30865. ' "",',
  30866. ' {',
  30867. ' Default: 3',
  30868. ' }',
  30869. ' );',
  30870. ' $r.addProperty(',
  30871. ' "E1",',
  30872. ' 0,',
  30873. ' $mod.$rtti["TEnum"],',
  30874. ' "FE",',
  30875. ' "",',
  30876. ' {',
  30877. ' Default: $mod.TEnum.red',
  30878. ' }',
  30879. ' );',
  30880. ' $r.addProperty(',
  30881. ' "E2",',
  30882. ' 0,',
  30883. ' $mod.$rtti["TEnum"],',
  30884. ' "FE",',
  30885. ' "",',
  30886. ' {',
  30887. ' Default: $mod.TEnum.blue',
  30888. ' }',
  30889. ' );',
  30890. '});',
  30891. '']),
  30892. LinesToStr([ // $mod.$main
  30893. '']));
  30894. end;
  30895. procedure TTestModule.TestRTTI_DefaultValueSet;
  30896. begin
  30897. WithTypeInfo:=true;
  30898. StartProgram(false);
  30899. Add([
  30900. 'type',
  30901. ' TEnum = (red, blue);',
  30902. ' TSet = set of TEnum;',
  30903. 'const',
  30904. ' CSet = [red,blue];',
  30905. 'type',
  30906. ' TObject = class',
  30907. ' FSet: TSet;',
  30908. ' published',
  30909. ' property Set1: TSet read FSet default [];',
  30910. ' property Set2: TSet read FSet default [red];',
  30911. ' property Set3: TSet read FSet default [red,blue];',
  30912. ' property Set4: TSet read FSet default CSet;',
  30913. ' end;',
  30914. 'begin']);
  30915. ConvertProgram;
  30916. CheckSource('TestRTTI_DefaultValueSet',
  30917. LinesToStr([ // statements
  30918. 'this.TEnum = {',
  30919. ' "0": "red",',
  30920. ' red: 0,',
  30921. ' "1": "blue",',
  30922. ' blue: 1',
  30923. '};',
  30924. 'this.$rtti.$Enum("TEnum", {',
  30925. ' minvalue: 0,',
  30926. ' maxvalue: 1,',
  30927. ' ordtype: 1,',
  30928. ' enumtype: this.TEnum',
  30929. '});',
  30930. 'this.$rtti.$Set("TSet", {',
  30931. ' comptype: this.$rtti["TEnum"]',
  30932. '});',
  30933. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  30934. 'rtl.createClass(this, "TObject", null, function () {',
  30935. ' this.$init = function () {',
  30936. ' this.FSet = {};',
  30937. ' };',
  30938. ' this.$final = function () {',
  30939. ' this.FSet = undefined;',
  30940. ' };',
  30941. ' var $r = this.$rtti;',
  30942. ' $r.addProperty(',
  30943. ' "Set1",',
  30944. ' 0,',
  30945. ' $mod.$rtti["TSet"],',
  30946. ' "FSet",',
  30947. ' "",',
  30948. ' {',
  30949. ' Default: {}',
  30950. ' }',
  30951. ' );',
  30952. ' $r.addProperty(',
  30953. ' "Set2",',
  30954. ' 0,',
  30955. ' $mod.$rtti["TSet"],',
  30956. ' "FSet",',
  30957. ' "",',
  30958. ' {',
  30959. ' Default: rtl.createSet($mod.TEnum.red)',
  30960. ' }',
  30961. ' );',
  30962. ' $r.addProperty(',
  30963. ' "Set3",',
  30964. ' 0,',
  30965. ' $mod.$rtti["TSet"],',
  30966. ' "FSet",',
  30967. ' "",',
  30968. ' {',
  30969. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  30970. ' }',
  30971. ' );',
  30972. ' $r.addProperty(',
  30973. ' "Set4",',
  30974. ' 0,',
  30975. ' $mod.$rtti["TSet"],',
  30976. ' "FSet",',
  30977. ' "",',
  30978. ' {',
  30979. ' Default: $mod.CSet',
  30980. ' }',
  30981. ' );',
  30982. '});',
  30983. '']),
  30984. LinesToStr([ // $mod.$main
  30985. '']));
  30986. end;
  30987. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  30988. begin
  30989. WithTypeInfo:=true;
  30990. StartProgram(false);
  30991. Add([
  30992. 'type',
  30993. ' TRg = -1..1;',
  30994. 'const',
  30995. ' l = low(TRg);',
  30996. ' h = high(TRg);',
  30997. 'type',
  30998. ' TObject = class',
  30999. ' FV: TRg;',
  31000. ' published',
  31001. ' property V1: TRg read FV default -1;',
  31002. ' end;',
  31003. 'begin']);
  31004. ConvertProgram;
  31005. CheckSource('TestRTTI_DefaultValueRangeType',
  31006. LinesToStr([ // statements
  31007. 'this.$rtti.$Int("TRg", {',
  31008. ' minvalue: -1,',
  31009. ' maxvalue: 1,',
  31010. ' ordtype: 0',
  31011. '});',
  31012. 'this.l = -1;',
  31013. 'this.h = 1;',
  31014. 'rtl.createClass(this, "TObject", null, function () {',
  31015. ' this.$init = function () {',
  31016. ' this.FV = 0;',
  31017. ' };',
  31018. ' this.$final = function () {',
  31019. ' };',
  31020. ' var $r = this.$rtti;',
  31021. ' $r.addProperty(',
  31022. ' "V1",',
  31023. ' 0,',
  31024. ' $mod.$rtti["TRg"],',
  31025. ' "FV",',
  31026. ' "",',
  31027. ' {',
  31028. ' Default: -1',
  31029. ' }',
  31030. ' );',
  31031. '});',
  31032. '']),
  31033. LinesToStr([ // $mod.$main
  31034. '']));
  31035. end;
  31036. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31037. begin
  31038. WithTypeInfo:=true;
  31039. StartProgram(false);
  31040. Add([
  31041. 'type',
  31042. ' TObject = class',
  31043. ' FA, FB: byte;',
  31044. ' property A: byte read FA default 1;',
  31045. ' property B: byte read FB default 2;',
  31046. ' end;',
  31047. ' TBird = class',
  31048. ' published',
  31049. ' property A;',
  31050. ' property B nodefault;',
  31051. ' end;',
  31052. 'begin']);
  31053. ConvertProgram;
  31054. CheckSource('TestRTTI_DefaultValueInherit',
  31055. LinesToStr([ // statements
  31056. 'rtl.createClass(this, "TObject", null, function () {',
  31057. ' this.$init = function () {',
  31058. ' this.FA = 0;',
  31059. ' this.FB = 0;',
  31060. ' };',
  31061. ' this.$final = function () {',
  31062. ' };',
  31063. '});',
  31064. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31065. ' var $r = this.$rtti;',
  31066. ' $r.addProperty(',
  31067. ' "A",',
  31068. ' 0,',
  31069. ' rtl.byte,',
  31070. ' "FA",',
  31071. ' "",',
  31072. ' {',
  31073. ' Default: 1',
  31074. ' }',
  31075. ' );',
  31076. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31077. '});',
  31078. '']),
  31079. LinesToStr([ // $mod.$main
  31080. '']));
  31081. end;
  31082. procedure TTestModule.TestRTTI_OverrideMethod;
  31083. begin
  31084. WithTypeInfo:=true;
  31085. StartProgram(false);
  31086. Add('type');
  31087. Add(' TObject = class');
  31088. Add(' published');
  31089. Add(' procedure DoIt; virtual; abstract;');
  31090. Add(' end;');
  31091. Add(' TSky = class');
  31092. Add(' published');
  31093. Add(' procedure DoIt; override;');
  31094. Add(' end;');
  31095. Add('procedure TSky.DoIt; begin end;');
  31096. Add('begin');
  31097. ConvertProgram;
  31098. CheckSource('TestRTTI_OverrideMethod',
  31099. LinesToStr([ // statements
  31100. 'rtl.createClass(this, "TObject", null, function () {',
  31101. ' this.$init = function () {',
  31102. ' };',
  31103. ' this.$final = function () {',
  31104. ' };',
  31105. ' var $r = this.$rtti;',
  31106. ' $r.addMethod("DoIt", 0, []);',
  31107. '});',
  31108. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31109. ' this.DoIt = function () {',
  31110. ' };',
  31111. '});',
  31112. '']),
  31113. LinesToStr([ // $mod.$main
  31114. '']));
  31115. end;
  31116. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31117. begin
  31118. WithTypeInfo:=true;
  31119. StartProgram(false);
  31120. Add([
  31121. 'type',
  31122. ' TObject = class',
  31123. ' published',
  31124. ' procedure DoIt;',
  31125. ' end;',
  31126. ' TSky = class',
  31127. ' published',
  31128. ' procedure DoIt; reintroduce;',
  31129. ' end;',
  31130. 'procedure TObject.DoIt; begin end;',
  31131. 'procedure TSky.DoIt;',
  31132. 'begin',
  31133. ' inherited DoIt;',
  31134. 'end;',
  31135. 'begin']);
  31136. ConvertProgram;
  31137. CheckSource('TestRTTI_ReintroduceMethod',
  31138. LinesToStr([ // statements
  31139. 'rtl.createClass(this, "TObject", null, function () {',
  31140. ' this.$init = function () {',
  31141. ' };',
  31142. ' this.$final = function () {',
  31143. ' };',
  31144. ' this.DoIt = function () {',
  31145. ' };',
  31146. ' var $r = this.$rtti;',
  31147. ' $r.addMethod("DoIt", 0, []);',
  31148. '});',
  31149. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31150. ' this.DoIt = function () {',
  31151. ' $mod.TObject.DoIt.call(this);',
  31152. ' };',
  31153. ' var $r = this.$rtti;',
  31154. ' $r.addMethod("DoIt", 0, []);',
  31155. '});',
  31156. '']),
  31157. LinesToStr([ // $mod.$main
  31158. '']));
  31159. end;
  31160. procedure TTestModule.TestRTTI_OverloadProperty;
  31161. begin
  31162. WithTypeInfo:=true;
  31163. StartProgram(false);
  31164. Add('type');
  31165. Add(' TObject = class');
  31166. Add(' protected');
  31167. Add(' FFlag: longint;');
  31168. Add(' published');
  31169. Add(' property Flag: longint read fflag;');
  31170. Add(' end;');
  31171. Add(' TSky = class');
  31172. Add(' published');
  31173. Add(' property FLAG: longint write fflag;');
  31174. Add(' end;');
  31175. Add('begin');
  31176. ConvertProgram;
  31177. CheckSource('TestRTTI_OverrideMethod',
  31178. LinesToStr([ // statements
  31179. 'rtl.createClass(this, "TObject", null, function () {',
  31180. ' this.$init = function () {',
  31181. ' this.FFlag = 0;',
  31182. ' };',
  31183. ' this.$final = function () {',
  31184. ' };',
  31185. ' var $r = this.$rtti;',
  31186. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  31187. '});',
  31188. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31189. ' var $r = this.$rtti;',
  31190. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  31191. '});',
  31192. '']),
  31193. LinesToStr([ // $mod.$main
  31194. '']));
  31195. end;
  31196. procedure TTestModule.TestRTTI_ClassForward;
  31197. begin
  31198. WithTypeInfo:=true;
  31199. StartProgram(false);
  31200. Add('type');
  31201. Add(' TObject = class end;');
  31202. Add(' tbridge = class;');
  31203. Add(' TProc = function: tbridge;');
  31204. Add(' TOger = class');
  31205. Add(' published');
  31206. Add(' FBridge: tbridge;');
  31207. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  31208. Add(' property Bridge: tbridge read fbridge write setbridge;');
  31209. Add(' end;');
  31210. Add(' TBridge = class');
  31211. Add(' FOger: toger;');
  31212. Add(' end;');
  31213. Add('var p: Pointer;');
  31214. Add(' b: tbridge;');
  31215. Add('begin');
  31216. Add(' p:=typeinfo(tbridge);');
  31217. Add(' p:=typeinfo(b);');
  31218. ConvertProgram;
  31219. CheckSource('TestRTTI_ClassForward',
  31220. LinesToStr([ // statements
  31221. 'rtl.createClass(this, "TObject", null, function () {',
  31222. ' this.$init = function () {',
  31223. ' };',
  31224. ' this.$final = function () {',
  31225. ' };',
  31226. '});',
  31227. 'this.$rtti.$Class("TBridge");',
  31228. 'this.$rtti.$ProcVar("TProc", {',
  31229. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  31230. '});',
  31231. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  31232. ' this.$init = function () {',
  31233. ' $mod.TObject.$init.call(this);',
  31234. ' this.FBridge = null;',
  31235. ' };',
  31236. ' this.$final = function () {',
  31237. ' this.FBridge = undefined;',
  31238. ' $mod.TObject.$final.call(this);',
  31239. ' };',
  31240. ' var $r = this.$rtti;',
  31241. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  31242. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  31243. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  31244. '});',
  31245. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  31246. ' this.$init = function () {',
  31247. ' $mod.TObject.$init.call(this);',
  31248. ' this.FOger = null;',
  31249. ' };',
  31250. ' this.$final = function () {',
  31251. ' this.FOger = undefined;',
  31252. ' $mod.TObject.$final.call(this);',
  31253. ' };',
  31254. '});',
  31255. 'this.p = null;',
  31256. 'this.b = null;',
  31257. '']),
  31258. LinesToStr([ // $mod.$main
  31259. '$mod.p = $mod.$rtti["TBridge"];',
  31260. '$mod.p = $mod.b.$rtti;',
  31261. '']));
  31262. end;
  31263. procedure TTestModule.TestRTTI_ClassOf;
  31264. begin
  31265. WithTypeInfo:=true;
  31266. StartProgram(false);
  31267. Add('type');
  31268. Add(' TClass = class of tobject;');
  31269. Add(' TProcA = function: TClass;');
  31270. Add(' TObject = class');
  31271. Add(' published');
  31272. Add(' C: tclass;');
  31273. Add(' end;');
  31274. Add(' tfox = class;');
  31275. Add(' TBird = class end;');
  31276. Add(' TBirds = class of tbird;');
  31277. Add(' TFox = class end;');
  31278. Add(' TFoxes = class of tfox;');
  31279. Add(' TCows = class of TCow;');
  31280. Add(' TCow = class;');
  31281. Add(' TCow = class end;');
  31282. Add('begin');
  31283. ConvertProgram;
  31284. CheckSource('TestRTTI_ClassOf',
  31285. LinesToStr([ // statements
  31286. 'this.$rtti.$Class("TObject");',
  31287. 'this.$rtti.$ClassRef("TClass", {',
  31288. ' instancetype: this.$rtti["TObject"]',
  31289. '});',
  31290. 'this.$rtti.$ProcVar("TProcA", {',
  31291. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  31292. '});',
  31293. 'rtl.createClass(this, "TObject", null, function () {',
  31294. ' this.$init = function () {',
  31295. ' this.C = null;',
  31296. ' };',
  31297. ' this.$final = function () {',
  31298. ' this.C = undefined;',
  31299. ' };',
  31300. ' var $r = this.$rtti;',
  31301. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31302. '});',
  31303. 'this.$rtti.$Class("TFox");',
  31304. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31305. '});',
  31306. 'this.$rtti.$ClassRef("TBirds", {',
  31307. ' instancetype: this.$rtti["TBird"]',
  31308. '});',
  31309. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31310. '});',
  31311. 'this.$rtti.$ClassRef("TFoxes", {',
  31312. ' instancetype: this.$rtti["TFox"]',
  31313. '});',
  31314. 'this.$rtti.$Class("TCow");',
  31315. 'this.$rtti.$ClassRef("TCows", {',
  31316. ' instancetype: this.$rtti["TCow"]',
  31317. '});',
  31318. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31319. '});',
  31320. '']),
  31321. LinesToStr([ // $mod.$main
  31322. '']));
  31323. end;
  31324. procedure TTestModule.TestRTTI_Record;
  31325. begin
  31326. WithTypeInfo:=true;
  31327. StartProgram(false);
  31328. Add('type');
  31329. Add(' integer = longint;');
  31330. Add(' TPoint = record');
  31331. Add(' x,y: integer;');
  31332. Add(' end;');
  31333. Add('var p: pointer;');
  31334. Add(' r: tpoint;');
  31335. Add('begin');
  31336. Add(' p:=typeinfo(tpoint);');
  31337. Add(' p:=typeinfo(r);');
  31338. Add(' p:=typeinfo(r.x);');
  31339. ConvertProgram;
  31340. CheckSource('TestRTTI_Record',
  31341. LinesToStr([ // statements
  31342. 'rtl.recNewT(this, "TPoint", function () {',
  31343. ' this.x = 0;',
  31344. ' this.y = 0;',
  31345. ' this.$eq = function (b) {',
  31346. ' return (this.x === b.x) && (this.y === b.y);',
  31347. ' };',
  31348. ' this.$assign = function (s) {',
  31349. ' this.x = s.x;',
  31350. ' this.y = s.y;',
  31351. ' return this;',
  31352. ' };',
  31353. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31354. ' $r.addField("x", rtl.longint);',
  31355. ' $r.addField("y", rtl.longint);',
  31356. '});',
  31357. 'this.p = null;',
  31358. 'this.r = this.TPoint.$new();',
  31359. '']),
  31360. LinesToStr([ // $mod.$main
  31361. '$mod.p = $mod.$rtti["TPoint"];',
  31362. '$mod.p = $mod.$rtti["TPoint"];',
  31363. '$mod.p = rtl.longint;',
  31364. '']));
  31365. end;
  31366. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31367. begin
  31368. WithTypeInfo:=true;
  31369. StartProgram(false);
  31370. Add('type');
  31371. Add(' TFloatRec = record');
  31372. Add(' c,d: array of char;');
  31373. // Add(' i: array of array of longint;');
  31374. Add(' end;');
  31375. Add('var p: pointer;');
  31376. Add(' r: tfloatrec;');
  31377. Add('begin');
  31378. Add(' p:=typeinfo(tfloatrec);');
  31379. Add(' p:=typeinfo(r);');
  31380. Add(' p:=typeinfo(r.d);');
  31381. ConvertProgram;
  31382. CheckSource('TestRTTI_Record',
  31383. LinesToStr([ // statements
  31384. 'rtl.recNewT(this, "TFloatRec", function () {',
  31385. ' this.$new = function () {',
  31386. ' var r = Object.create(this);',
  31387. ' r.c = [];',
  31388. ' r.d = [];',
  31389. ' return r;',
  31390. ' };',
  31391. ' this.$eq = function (b) {',
  31392. ' return (this.c === b.c) && (this.d === b.d);',
  31393. ' };',
  31394. ' this.$assign = function (s) {',
  31395. ' this.c = rtl.arrayRef(s.c);',
  31396. ' this.d = rtl.arrayRef(s.d);',
  31397. ' return this;',
  31398. ' };',
  31399. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31400. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31401. ' eltype: rtl.char',
  31402. ' });',
  31403. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31404. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31405. '});',
  31406. 'this.p = null;',
  31407. 'this.r = this.TFloatRec.$new();',
  31408. '']),
  31409. LinesToStr([ // $mod.$main
  31410. '$mod.p = $mod.$rtti["TFloatRec"];',
  31411. '$mod.p = $mod.$rtti["TFloatRec"];',
  31412. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31413. '']));
  31414. end;
  31415. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31416. begin
  31417. WithTypeInfo:=true;
  31418. StartProgram(false);
  31419. Add([
  31420. '{$modeswitch AdvancedRecords}',
  31421. 'type',
  31422. ' TPoint = record',
  31423. ' type TProc = procedure(w: word);',
  31424. ' class var p: TProc;',
  31425. ' end;',
  31426. 'begin',
  31427. '']);
  31428. ConvertProgram;
  31429. CheckSource('TestRTTI_Record_ClassVarType',
  31430. LinesToStr([ // statements
  31431. 'rtl.recNewT(this, "TPoint", function () {',
  31432. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  31433. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  31434. ' });',
  31435. ' this.p = null;',
  31436. ' this.$eq = function (b) {',
  31437. ' return true;',
  31438. ' };',
  31439. ' this.$assign = function (s) {',
  31440. ' return this;',
  31441. ' };',
  31442. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31443. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  31444. '}, true);',
  31445. '']),
  31446. LinesToStr([ // $mod.$main
  31447. '']));
  31448. end;
  31449. procedure TTestModule.TestRTTI_LocalTypes;
  31450. begin
  31451. WithTypeInfo:=true;
  31452. StartProgram(false);
  31453. Add([
  31454. 'procedure DoIt;',
  31455. 'type',
  31456. ' integer = longint;',
  31457. ' TPoint = record',
  31458. ' x,y: integer;',
  31459. ' end;',
  31460. 'var p: TPoint;',
  31461. 'begin',
  31462. 'end;',
  31463. 'begin']);
  31464. ConvertProgram;
  31465. CheckSource('TestRTTI_LocalTypes',
  31466. LinesToStr([ // statements
  31467. 'var TPoint = rtl.recNewT(null, "", function () {',
  31468. ' this.x = 0;',
  31469. ' this.y = 0;',
  31470. ' this.$eq = function (b) {',
  31471. ' return (this.x === b.x) && (this.y === b.y);',
  31472. ' };',
  31473. ' this.$assign = function (s) {',
  31474. ' this.x = s.x;',
  31475. ' this.y = s.y;',
  31476. ' return this;',
  31477. ' };',
  31478. '});',
  31479. 'this.DoIt = function () {',
  31480. ' var p = TPoint.$new();',
  31481. '};',
  31482. '']),
  31483. LinesToStr([ // $mod.$main
  31484. '']));
  31485. end;
  31486. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  31487. begin
  31488. WithTypeInfo:=true;
  31489. StartProgram(false);
  31490. Add([
  31491. 'type',
  31492. ' TCaption = string;',
  31493. ' TYesNo = boolean;',
  31494. ' TLetter = char;',
  31495. ' TFloat = double;',
  31496. ' TPtr = pointer;',
  31497. ' TShortInt = shortint;',
  31498. ' TByte = byte;',
  31499. ' TSmallInt = smallint;',
  31500. ' TWord = word;',
  31501. ' TInt32 = longint;',
  31502. ' TDWord = longword;',
  31503. ' TValue = jsvalue;',
  31504. 'var p: TPtr;',
  31505. 'begin',
  31506. ' p:=typeinfo(string);',
  31507. ' p:=typeinfo(tcaption);',
  31508. ' p:=typeinfo(boolean);',
  31509. ' p:=typeinfo(tyesno);',
  31510. ' p:=typeinfo(char);',
  31511. ' p:=typeinfo(tletter);',
  31512. ' p:=typeinfo(double);',
  31513. ' p:=typeinfo(tfloat);',
  31514. ' p:=typeinfo(pointer);',
  31515. ' p:=typeinfo(tptr);',
  31516. ' p:=typeinfo(shortint);',
  31517. ' p:=typeinfo(tshortint);',
  31518. ' p:=typeinfo(byte);',
  31519. ' p:=typeinfo(tbyte);',
  31520. ' p:=typeinfo(smallint);',
  31521. ' p:=typeinfo(tsmallint);',
  31522. ' p:=typeinfo(word);',
  31523. ' p:=typeinfo(tword);',
  31524. ' p:=typeinfo(longword);',
  31525. ' p:=typeinfo(tdword);',
  31526. ' p:=typeinfo(jsvalue);',
  31527. ' p:=typeinfo(tvalue);',
  31528. '']);
  31529. ConvertProgram;
  31530. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  31531. LinesToStr([ // statements
  31532. 'this.p = null;',
  31533. '']),
  31534. LinesToStr([ // $mod.$main
  31535. '$mod.p = rtl.string;',
  31536. '$mod.p = rtl.string;',
  31537. '$mod.p = rtl.boolean;',
  31538. '$mod.p = rtl.boolean;',
  31539. '$mod.p = rtl.char;',
  31540. '$mod.p = rtl.char;',
  31541. '$mod.p = rtl.double;',
  31542. '$mod.p = rtl.double;',
  31543. '$mod.p = rtl.pointer;',
  31544. '$mod.p = rtl.pointer;',
  31545. '$mod.p = rtl.shortint;',
  31546. '$mod.p = rtl.shortint;',
  31547. '$mod.p = rtl.byte;',
  31548. '$mod.p = rtl.byte;',
  31549. '$mod.p = rtl.smallint;',
  31550. '$mod.p = rtl.smallint;',
  31551. '$mod.p = rtl.word;',
  31552. '$mod.p = rtl.word;',
  31553. '$mod.p = rtl.longword;',
  31554. '$mod.p = rtl.longword;',
  31555. '$mod.p = rtl.jsvalue;',
  31556. '$mod.p = rtl.jsvalue;',
  31557. '']));
  31558. end;
  31559. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  31560. begin
  31561. WithTypeInfo:=true;
  31562. StartProgram(false);
  31563. Add([
  31564. 'type',
  31565. ' TCaption = type string;',
  31566. ' TYesNo = type boolean;',
  31567. ' TLetter = type char;',
  31568. ' TFloat = type double;',
  31569. ' TPtr = type pointer;',
  31570. ' TShortInt = type shortint;',
  31571. ' TByte = type byte;',
  31572. ' TSmallInt = type smallint;',
  31573. ' TWord = type word;',
  31574. ' TInt32 = type longint;',
  31575. ' TDWord = type longword;',
  31576. ' TValue = type jsvalue;',
  31577. ' TAliasValue = type TValue;',
  31578. 'var',
  31579. ' p: TPtr;',
  31580. ' a: TAliasValue;',
  31581. 'begin',
  31582. ' p:=typeinfo(tcaption);',
  31583. ' p:=typeinfo(tyesno);',
  31584. ' p:=typeinfo(tletter);',
  31585. ' p:=typeinfo(tfloat);',
  31586. ' p:=typeinfo(tptr);',
  31587. ' p:=typeinfo(tshortint);',
  31588. ' p:=typeinfo(tbyte);',
  31589. ' p:=typeinfo(tsmallint);',
  31590. ' p:=typeinfo(tword);',
  31591. ' p:=typeinfo(tdword);',
  31592. ' p:=typeinfo(tvalue);',
  31593. ' p:=typeinfo(taliasvalue);',
  31594. ' p:=typeinfo(a);',
  31595. '']);
  31596. ConvertProgram;
  31597. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  31598. LinesToStr([ // statements
  31599. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  31600. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  31601. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  31602. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  31603. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  31604. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  31605. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  31606. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  31607. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  31608. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  31609. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  31610. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  31611. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  31612. 'this.p = null;',
  31613. 'this.a = undefined;',
  31614. '']),
  31615. LinesToStr([ // $mod.$main
  31616. '$mod.p = $mod.$rtti["TCaption"];',
  31617. '$mod.p = $mod.$rtti["TYesNo"];',
  31618. '$mod.p = $mod.$rtti["TLetter"];',
  31619. '$mod.p = $mod.$rtti["TFloat"];',
  31620. '$mod.p = $mod.$rtti["TPtr"];',
  31621. '$mod.p = $mod.$rtti["TShortInt"];',
  31622. '$mod.p = $mod.$rtti["TByte"];',
  31623. '$mod.p = $mod.$rtti["TSmallInt"];',
  31624. '$mod.p = $mod.$rtti["TWord"];',
  31625. '$mod.p = $mod.$rtti["TDWord"];',
  31626. '$mod.p = $mod.$rtti["TValue"];',
  31627. '$mod.p = $mod.$rtti["TAliasValue"];',
  31628. '$mod.p = $mod.$rtti["TAliasValue"];',
  31629. '']));
  31630. end;
  31631. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  31632. begin
  31633. WithTypeInfo:=true;
  31634. StartProgram(false);
  31635. Add('procedure DoIt;');
  31636. Add('type');
  31637. Add(' integer = longint;');
  31638. Add(' TPoint = record');
  31639. Add(' x,y: integer;');
  31640. Add(' end;');
  31641. Add('var p: pointer;');
  31642. Add('begin');
  31643. Add(' p:=typeinfo(tpoint);');
  31644. Add('end;');
  31645. Add('begin');
  31646. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  31647. ConvertProgram;
  31648. end;
  31649. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  31650. begin
  31651. WithTypeInfo:=true;
  31652. StartProgram(true,[supTypeInfo]);
  31653. Add([
  31654. '{$modeswitch externalclass}',
  31655. 'type',
  31656. ' TFlag = (up,down);',
  31657. ' TFlags = set of TFlag;',
  31658. 'var',
  31659. ' ti: TTypeInfo;',
  31660. ' tiInt: TTypeInfoInteger;',
  31661. ' tiEnum: TTypeInfoEnum;',
  31662. ' tiSet: TTypeInfoSet;',
  31663. 'begin',
  31664. ' ti:=typeinfo(string);',
  31665. ' ti:=typeinfo(boolean);',
  31666. ' ti:=typeinfo(char);',
  31667. ' ti:=typeinfo(double);',
  31668. ' tiInt:=typeinfo(shortint);',
  31669. ' tiInt:=typeinfo(byte);',
  31670. ' tiInt:=typeinfo(smallint);',
  31671. ' tiInt:=typeinfo(word);',
  31672. ' tiInt:=typeinfo(longint);',
  31673. ' tiInt:=typeinfo(longword);',
  31674. ' ti:=typeinfo(jsvalue);',
  31675. ' tiEnum:=typeinfo(tflag);',
  31676. ' tiSet:=typeinfo(tflags);']);
  31677. ConvertProgram;
  31678. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  31679. LinesToStr([ // statements
  31680. 'this.TFlag = {',
  31681. ' "0": "up",',
  31682. ' up: 0,',
  31683. ' "1": "down",',
  31684. ' down: 1',
  31685. '};',
  31686. 'this.$rtti.$Enum("TFlag", {',
  31687. ' minvalue: 0,',
  31688. ' maxvalue: 1,',
  31689. ' ordtype: 1,',
  31690. ' enumtype: this.TFlag',
  31691. '});',
  31692. 'this.$rtti.$Set("TFlags", {',
  31693. ' comptype: this.$rtti["TFlag"]',
  31694. '});',
  31695. 'this.ti = null;',
  31696. 'this.tiInt = null;',
  31697. 'this.tiEnum = null;',
  31698. 'this.tiSet = null;',
  31699. '']),
  31700. LinesToStr([ // $mod.$main
  31701. '$mod.ti = rtl.string;',
  31702. '$mod.ti = rtl.boolean;',
  31703. '$mod.ti = rtl.char;',
  31704. '$mod.ti = rtl.double;',
  31705. '$mod.tiInt = rtl.shortint;',
  31706. '$mod.tiInt = rtl.byte;',
  31707. '$mod.tiInt = rtl.smallint;',
  31708. '$mod.tiInt = rtl.word;',
  31709. '$mod.tiInt = rtl.longint;',
  31710. '$mod.tiInt = rtl.longword;',
  31711. '$mod.ti = rtl.jsvalue;',
  31712. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  31713. '$mod.tiSet = $mod.$rtti["TFlags"];',
  31714. '']));
  31715. end;
  31716. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  31717. begin
  31718. WithTypeInfo:=true;
  31719. StartProgram(true,[supTypeInfo]);
  31720. Add('{$modeswitch externalclass}');
  31721. Add('type');
  31722. Add(' TStaticArr = array[boolean] of string;');
  31723. Add(' TDynArr = array of string;');
  31724. Add(' TProc = procedure;');
  31725. Add(' TMethod = procedure of object;');
  31726. Add('var');
  31727. Add(' StaticArray: TStaticArr;');
  31728. Add(' tiStaticArray: TTypeInfoStaticArray;');
  31729. Add(' DynArray: TDynArr;');
  31730. Add(' tiDynArray: TTypeInfoDynArray;');
  31731. Add(' ProcVar: TProc;');
  31732. Add(' tiProcVar: TTypeInfoProcVar;');
  31733. Add(' MethodVar: TMethod;');
  31734. Add(' tiMethodVar: TTypeInfoMethodVar;');
  31735. Add('begin');
  31736. Add(' tiStaticArray:=typeinfo(StaticArray);');
  31737. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  31738. Add(' tiDynArray:=typeinfo(DynArray);');
  31739. Add(' tiDynArray:=typeinfo(TDynArr);');
  31740. Add(' tiProcVar:=typeinfo(ProcVar);');
  31741. Add(' tiProcVar:=typeinfo(TProc);');
  31742. Add(' tiMethodVar:=typeinfo(MethodVar);');
  31743. Add(' tiMethodVar:=typeinfo(TMethod);');
  31744. ConvertProgram;
  31745. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  31746. LinesToStr([ // statements
  31747. 'this.$rtti.$StaticArray("TStaticArr", {',
  31748. ' dims: [2],',
  31749. ' eltype: rtl.string',
  31750. '});',
  31751. 'this.$rtti.$DynArray("TDynArr", {',
  31752. ' eltype: rtl.string',
  31753. '});',
  31754. 'this.$rtti.$ProcVar("TProc", {',
  31755. ' procsig: rtl.newTIProcSig([])',
  31756. '});',
  31757. 'this.$rtti.$MethodVar("TMethod", {',
  31758. ' procsig: rtl.newTIProcSig([]),',
  31759. ' methodkind: 0',
  31760. '});',
  31761. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  31762. 'this.tiStaticArray = null;',
  31763. 'this.DynArray = [];',
  31764. 'this.tiDynArray = null;',
  31765. 'this.ProcVar = null;',
  31766. 'this.tiProcVar = null;',
  31767. 'this.MethodVar = null;',
  31768. 'this.tiMethodVar = null;',
  31769. '']),
  31770. LinesToStr([ // $mod.$main
  31771. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  31772. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  31773. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  31774. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  31775. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  31776. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  31777. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  31778. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  31779. '']));
  31780. end;
  31781. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  31782. begin
  31783. WithTypeInfo:=true;
  31784. StartProgram(true,[supTypeInfo]);
  31785. Add('{$modeswitch externalclass}');
  31786. Add('type');
  31787. Add(' TRec = record end;');
  31788. // ToDo: ^TRec
  31789. Add(' TObject = class end;');
  31790. Add(' TClass = class of tobject;');
  31791. Add('var');
  31792. Add(' Rec: trec;');
  31793. Add(' tiRecord: ttypeinforecord;');
  31794. Add(' Obj: tobject;');
  31795. Add(' tiClass: ttypeinfoclass;');
  31796. Add(' aClass: tclass;');
  31797. Add(' tiClassRef: ttypeinfoclassref;');
  31798. // ToDo: ^TRec
  31799. Add(' tiPointer: ttypeinfopointer;');
  31800. Add('begin');
  31801. Add(' tirecord:=typeinfo(trec);');
  31802. Add(' tirecord:=typeinfo(trec);');
  31803. Add(' ticlass:=typeinfo(obj);');
  31804. Add(' ticlass:=typeinfo(tobject);');
  31805. Add(' ticlass:=typeinfo(aclass);');
  31806. Add(' ticlassref:=typeinfo(tclass);');
  31807. ConvertProgram;
  31808. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  31809. LinesToStr([ // statements
  31810. 'rtl.recNewT(this, "TRec", function () {',
  31811. ' this.$eq = function (b) {',
  31812. ' return true;',
  31813. ' };',
  31814. ' this.$assign = function (s) {',
  31815. ' return this;',
  31816. ' };',
  31817. ' $mod.$rtti.$Record("TRec", {});',
  31818. '});',
  31819. 'rtl.createClass(this, "TObject", null, function () {',
  31820. ' this.$init = function () {',
  31821. ' };',
  31822. ' this.$final = function () {',
  31823. ' };',
  31824. '});',
  31825. 'this.$rtti.$ClassRef("TClass", {',
  31826. ' instancetype: this.$rtti["TObject"]',
  31827. '});',
  31828. 'this.Rec = this.TRec.$new();',
  31829. 'this.tiRecord = null;',
  31830. 'this.Obj = null;',
  31831. 'this.tiClass = null;',
  31832. 'this.aClass = null;',
  31833. 'this.tiClassRef = null;',
  31834. 'this.tiPointer = null;',
  31835. '']),
  31836. LinesToStr([ // $mod.$main
  31837. '$mod.tiRecord = $mod.$rtti["TRec"];',
  31838. '$mod.tiRecord = $mod.$rtti["TRec"];',
  31839. '$mod.tiClass = $mod.Obj.$rtti;',
  31840. '$mod.tiClass = $mod.$rtti["TObject"];',
  31841. '$mod.tiClass = $mod.aClass.$rtti;',
  31842. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  31843. '']));
  31844. end;
  31845. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  31846. begin
  31847. WithTypeInfo:=true;
  31848. StartProgram(true,[supTypeInfo]);
  31849. Add([
  31850. '{$modeswitch externalclass}',
  31851. 'type',
  31852. ' TClass = class of tobject;',
  31853. ' TObject = class',
  31854. ' function MyClass: TClass;',
  31855. ' class function ClassType: TClass;',
  31856. ' end;',
  31857. 'function TObject.MyClass: TClass;',
  31858. 'var t: TTypeInfoClass;',
  31859. 'begin',
  31860. ' t:=TypeInfo(Self);',
  31861. ' t:=TypeInfo(Result);',
  31862. ' t:=TypeInfo(TObject);',
  31863. 'end;',
  31864. 'class function TObject.ClassType: TClass;',
  31865. 'var t: TTypeInfoClass;',
  31866. 'begin',
  31867. ' t:=TypeInfo(Self);',
  31868. ' t:=TypeInfo(Result);',
  31869. 'end;',
  31870. 'var',
  31871. ' Obj: TObject;',
  31872. ' t: TTypeInfoClass;',
  31873. 'begin',
  31874. ' t:=TypeInfo(TObject.ClassType);',
  31875. ' t:=TypeInfo(Obj.ClassType);',
  31876. ' t:=TypeInfo(Obj.MyClass);',
  31877. '']);
  31878. ConvertProgram;
  31879. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  31880. LinesToStr([ // statements
  31881. 'this.$rtti.$Class("TObject");',
  31882. 'this.$rtti.$ClassRef("TClass", {',
  31883. ' instancetype: this.$rtti["TObject"]',
  31884. '});',
  31885. 'rtl.createClass(this, "TObject", null, function () {',
  31886. ' this.$init = function () {',
  31887. ' };',
  31888. ' this.$final = function () {',
  31889. ' };',
  31890. ' this.MyClass = function () {',
  31891. ' var Result = null;',
  31892. ' var t = null;',
  31893. ' t = this.$rtti;',
  31894. ' t = Result.$rtti;',
  31895. ' t = $mod.$rtti["TObject"];',
  31896. ' return Result;',
  31897. ' };',
  31898. ' this.ClassType = function () {',
  31899. ' var Result = null;',
  31900. ' var t = null;',
  31901. ' t = this.$rtti;',
  31902. ' t = Result.$rtti;',
  31903. ' return Result;',
  31904. ' };',
  31905. '});',
  31906. 'this.Obj = null;',
  31907. 'this.t = null;',
  31908. '']),
  31909. LinesToStr([ // $mod.$main
  31910. '$mod.t = $mod.TObject.ClassType().$rtti;',
  31911. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  31912. '$mod.t = $mod.Obj.MyClass().$rtti;',
  31913. '']));
  31914. end;
  31915. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  31916. begin
  31917. WithTypeInfo:=true;
  31918. AddModuleWithIntfImplSrc('typinfo.pas',
  31919. LinesToStr([
  31920. '{$modeswitch externalclass}',
  31921. 'type',
  31922. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  31923. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  31924. '']),
  31925. '');
  31926. AddModuleWithIntfImplSrc('unit2.pas',
  31927. LinesToStr([
  31928. 'uses typinfo;',
  31929. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  31930. 'procedure DoPtr(p: PTypeInfo);',
  31931. 'procedure DoInfo(t: TTypeInfo);',
  31932. 'procedure DoInt(t: TTypeInfoInteger);',
  31933. '']),
  31934. LinesToStr([
  31935. 'procedure DoPtr(p: PTypeInfo);',
  31936. 'begin end;',
  31937. 'procedure DoInfo(t: TTypeInfo);',
  31938. 'begin end;',
  31939. 'procedure DoInt(t: TTypeInfoInteger);',
  31940. 'begin end;',
  31941. '']));
  31942. StartUnit(true);
  31943. Add([
  31944. 'interface',
  31945. 'uses unit2;', // does not use unit typinfo
  31946. 'implementation',
  31947. 'var',
  31948. ' i: byte;',
  31949. ' p: pointer;',
  31950. ' t: PTypeInfo;',
  31951. 'initialization',
  31952. ' p:=typeinfo(i);',
  31953. ' t:=typeinfo(i);',
  31954. ' if p=t then ;',
  31955. ' if p=typeinfo(i) then ;',
  31956. ' if typeinfo(i)=p then ;',
  31957. ' if t=typeinfo(i) then ;',
  31958. ' if typeinfo(i)=t then ;',
  31959. ' DoPtr(p);',
  31960. ' DoPtr(t);',
  31961. ' DoPtr(typeinfo(i));',
  31962. ' DoInfo(p);',
  31963. ' DoInfo(t);',
  31964. ' DoInfo(typeinfo(i));',
  31965. ' DoInt(typeinfo(i));',
  31966. '']);
  31967. ConvertUnit;
  31968. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  31969. LinesToStr([ // statements
  31970. 'var $impl = $mod.$impl;',
  31971. '']),
  31972. LinesToStr([ // this.$init
  31973. '$impl.p = rtl.byte;',
  31974. '$impl.t = rtl.byte;',
  31975. 'if ($impl.p === $impl.t) ;',
  31976. 'if ($impl.p === rtl.byte) ;',
  31977. 'if (rtl.byte === $impl.p) ;',
  31978. 'if ($impl.t === rtl.byte) ;',
  31979. 'if (rtl.byte === $impl.t) ;',
  31980. 'pas.unit2.DoPtr($impl.p);',
  31981. 'pas.unit2.DoPtr($impl.t);',
  31982. 'pas.unit2.DoPtr(rtl.byte);',
  31983. 'pas.unit2.DoInfo($impl.p);',
  31984. 'pas.unit2.DoInfo($impl.t);',
  31985. 'pas.unit2.DoInfo(rtl.byte);',
  31986. 'pas.unit2.DoInt(rtl.byte);',
  31987. '']),
  31988. LinesToStr([ // implementation
  31989. '$impl.i = 0;',
  31990. '$impl.p = null;',
  31991. '$impl.t = null;',
  31992. '']) );
  31993. end;
  31994. procedure TTestModule.TestRTTI_Interface_Corba;
  31995. begin
  31996. WithTypeInfo:=true;
  31997. StartProgram(true,[supTypeInfo]);
  31998. Add([
  31999. '{$interfaces corba}',
  32000. '{$modeswitch externalclass}',
  32001. 'type',
  32002. ' IUnknown = interface',
  32003. ' end;',
  32004. ' IBird = interface',
  32005. ' function GetItem: longint;',
  32006. ' procedure SetItem(Value: longint);',
  32007. ' property Item: longint read GetItem write SetItem;',
  32008. ' end;',
  32009. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32010. 'var',
  32011. ' i: IBird;',
  32012. ' t: TTypeInfoInterface;',
  32013. 'begin',
  32014. ' t:=TypeInfo(IBird);',
  32015. ' t:=TypeInfo(i);',
  32016. ' DoIt(t);',
  32017. ' DoIt(TypeInfo(IBird));',
  32018. '']);
  32019. ConvertProgram;
  32020. CheckSource('TestRTTI_Interface_Corba',
  32021. LinesToStr([ // statements
  32022. 'rtl.createInterface(',
  32023. ' this,',
  32024. ' "IUnknown",',
  32025. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32026. ' [],',
  32027. ' null,',
  32028. ' function () {',
  32029. ' }',
  32030. ');',
  32031. 'rtl.createInterface(',
  32032. ' this,',
  32033. ' "IBird",',
  32034. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32035. ' ["GetItem", "SetItem"],',
  32036. ' null,',
  32037. ' function () {',
  32038. ' var $r = this.$rtti;',
  32039. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32040. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32041. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32042. ' }',
  32043. ');',
  32044. 'this.DoIt = function (t) {',
  32045. '}; ',
  32046. 'this.i = null;',
  32047. 'this.t = null;',
  32048. '']),
  32049. LinesToStr([ // $mod.$main
  32050. '$mod.t = $mod.$rtti["IBird"];',
  32051. '$mod.t = $mod.i.$rtti;',
  32052. '$mod.DoIt($mod.t);',
  32053. '$mod.DoIt($mod.$rtti["IBird"]);',
  32054. '']));
  32055. end;
  32056. procedure TTestModule.TestRTTI_Interface_COM;
  32057. begin
  32058. WithTypeInfo:=true;
  32059. StartProgram(true,[supTypeInfo]);
  32060. Add([
  32061. '{$interfaces com}',
  32062. '{$modeswitch externalclass}',
  32063. 'type',
  32064. ' TGuid = record end;',
  32065. ' integer = longint;',
  32066. ' IUnknown = interface',
  32067. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32068. ' function _AddRef: Integer;',
  32069. ' function _Release: Integer;',
  32070. ' end;',
  32071. ' IBird = interface',
  32072. ' function GetItem: longint;',
  32073. ' procedure SetItem(Value: longint);',
  32074. ' property Item: longint read GetItem write SetItem;',
  32075. ' end;',
  32076. 'var',
  32077. ' i: IBird;',
  32078. ' t: TTypeInfoInterface;',
  32079. 'begin',
  32080. ' t:=TypeInfo(IBird);',
  32081. ' t:=TypeInfo(i);',
  32082. '']);
  32083. ConvertProgram;
  32084. CheckSource('TestRTTI_Interface_COM',
  32085. LinesToStr([ // statements
  32086. 'rtl.recNewT(this, "TGuid", function () {',
  32087. ' this.$eq = function (b) {',
  32088. ' return true;',
  32089. ' };',
  32090. ' this.$assign = function (s) {',
  32091. ' return this;',
  32092. ' };',
  32093. ' $mod.$rtti.$Record("TGuid", {});',
  32094. '});',
  32095. 'rtl.createInterface(',
  32096. ' this,',
  32097. ' "IUnknown",',
  32098. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32099. ' ["QueryInterface", "_AddRef", "_Release"],',
  32100. ' null,',
  32101. ' function () {',
  32102. ' this.$kind = "com";',
  32103. ' var $r = this.$rtti;',
  32104. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32105. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32106. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32107. ' }',
  32108. ');',
  32109. 'rtl.createInterface(',
  32110. ' this,',
  32111. ' "IBird",',
  32112. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32113. ' ["GetItem", "SetItem"],',
  32114. ' this.IUnknown,',
  32115. ' function () {',
  32116. ' var $r = this.$rtti;',
  32117. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32118. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32119. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32120. ' }',
  32121. ');',
  32122. 'this.i = null;',
  32123. 'this.t = null;',
  32124. '']),
  32125. LinesToStr([ // $mod.$main
  32126. '$mod.t = $mod.$rtti["IBird"];',
  32127. '$mod.t = $mod.i.$rtti;',
  32128. '']));
  32129. end;
  32130. procedure TTestModule.TestRTTI_ClassHelper;
  32131. begin
  32132. WithTypeInfo:=true;
  32133. StartProgram(true,[supTypeInfo]);
  32134. Add([
  32135. '{$interfaces com}',
  32136. '{$modeswitch externalclass}',
  32137. 'type',
  32138. ' TObject = class',
  32139. ' end;',
  32140. ' THelper = class helper for TObject',
  32141. ' published',
  32142. ' function GetItem: longint;',
  32143. ' property Item: longint read GetItem;',
  32144. ' end;',
  32145. 'function THelper.GetItem: longint;',
  32146. 'begin',
  32147. 'end;',
  32148. 'var',
  32149. ' t: TTypeInfoHelper;',
  32150. 'begin',
  32151. ' t:=TypeInfo(THelper);',
  32152. '']);
  32153. ConvertProgram;
  32154. CheckSource('TestRTTI_ClassHelper',
  32155. LinesToStr([ // statements
  32156. 'rtl.createClass(this, "TObject", null, function () {',
  32157. ' this.$init = function () {',
  32158. ' };',
  32159. ' this.$final = function () {',
  32160. ' };',
  32161. '});',
  32162. 'rtl.createHelper(this, "THelper", null, function () {',
  32163. ' this.GetItem = function () {',
  32164. ' var Result = 0;',
  32165. ' return Result;',
  32166. ' };',
  32167. ' var $r = this.$rtti;',
  32168. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32169. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32170. '});',
  32171. 'this.t = null;',
  32172. '']),
  32173. LinesToStr([ // $mod.$main
  32174. '$mod.t = $mod.$rtti["THelper"];',
  32175. '']));
  32176. end;
  32177. procedure TTestModule.TestRTTI_ExternalClass;
  32178. begin
  32179. WithTypeInfo:=true;
  32180. StartProgram(true,[supTypeInfo]);
  32181. Add([
  32182. '{$modeswitch externalclass}',
  32183. 'type',
  32184. ' TJSObject = class external name ''Object''',
  32185. ' end;',
  32186. ' TJSArray = class external name ''Array'' (TJSObject)',
  32187. ' end;',
  32188. 'var',
  32189. ' p: Pointer;',
  32190. ' tc: TTypeInfoExtClass;',
  32191. 'begin',
  32192. ' p:=typeinfo(TJSArray);']);
  32193. ConvertProgram;
  32194. CheckSource('TestRTTI_ExternalClass',
  32195. LinesToStr([ // statements
  32196. 'this.$rtti.$ExtClass("TJSObject", {',
  32197. ' jsclass: "Object"',
  32198. '});',
  32199. 'this.$rtti.$ExtClass("TJSArray", {',
  32200. ' ancestor: this.$rtti["TJSObject"],',
  32201. ' jsclass: "Array"',
  32202. '});',
  32203. 'this.p = null;',
  32204. 'this.tc = null;',
  32205. '']),
  32206. LinesToStr([ // $mod.$main
  32207. '$mod.p = $mod.$rtti["TJSArray"];',
  32208. '']));
  32209. end;
  32210. procedure TTestModule.TestRTTI_Unit;
  32211. begin
  32212. WithTypeInfo:=true;
  32213. AddModuleWithIntfImplSrc('unit2.pas',
  32214. LinesToStr([
  32215. '{$mode delphi}',
  32216. 'type',
  32217. ' TWordArray = array of word;',
  32218. ' TArray<T> = array of T;',
  32219. '']),
  32220. '');
  32221. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  32222. Add([
  32223. '{$mode delphi}',
  32224. 'interface',
  32225. 'uses unit2;',
  32226. 'type',
  32227. ' IBird = interface',
  32228. ' function Swoop: TWordArray;',
  32229. ' function Glide: TArray<word>;',
  32230. ' end;',
  32231. 'procedure Fly;',
  32232. 'implementation',
  32233. 'procedure Fly;',
  32234. 'var',
  32235. ' ta: tTypeInfoDynArray;',
  32236. ' ti: tTypeInfoInterface;',
  32237. 'begin',
  32238. ' ta:=typeinfo(TWordArray);',
  32239. ' ta:=typeinfo(TArray<word>);',
  32240. ' ti:=typeinfo(IBird);',
  32241. 'end;',
  32242. '']);
  32243. ConvertUnit;
  32244. CheckSource('TestRTTI_ExternalClass',
  32245. LinesToStr([ // statements
  32246. 'rtl.createInterface(',
  32247. ' this,',
  32248. ' "IBird",',
  32249. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  32250. ' ["Swoop", "Glide"],',
  32251. ' pas.system.IUnknown,',
  32252. ' function () {',
  32253. ' var $r = this.$rtti;',
  32254. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  32255. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  32256. ' }',
  32257. ');',
  32258. 'this.Fly = function () {',
  32259. ' var ta = null;',
  32260. ' var ti = null;',
  32261. ' ta = pas.unit2.$rtti["TWordArray"];',
  32262. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  32263. ' ti = $mod.$rtti["IBird"];',
  32264. '};',
  32265. '']),
  32266. LinesToStr([ // $mod.$main
  32267. '']));
  32268. end;
  32269. procedure TTestModule.TestResourcestringProgram;
  32270. begin
  32271. AddModuleWithIntfImplSrc('unit2.pas',
  32272. LinesToStr([
  32273. 'resourcestring Title = ''Nice'';',
  32274. '']),
  32275. '');
  32276. StartProgram(true);
  32277. Add([
  32278. 'uses unit2;',
  32279. 'const Bar = ''bar'';',
  32280. 'resourcestring',
  32281. ' Red = ''red'';',
  32282. ' Foobar = ''fOo''+bar;',
  32283. 'var s: string;',
  32284. ' c: char;',
  32285. 'begin',
  32286. ' s:=red;',
  32287. ' s:=test1.red;',
  32288. ' s:=Title;',
  32289. ' c:=red[1];',
  32290. ' c:=test1.red[2];',
  32291. ' if red=foobar then ;',
  32292. ' if red[3]=red[4] then ;']);
  32293. ConvertProgram;
  32294. CheckSource('TestResourcestringProgram',
  32295. LinesToStr([ // statements
  32296. 'this.Bar = "bar";',
  32297. 'this.s = "";',
  32298. 'this.c = "";',
  32299. '$mod.$resourcestrings = {',
  32300. ' Red: {',
  32301. ' org: "red"',
  32302. ' },',
  32303. ' Foobar: {',
  32304. ' org: "fOobar"',
  32305. ' }',
  32306. '};',
  32307. '']),
  32308. LinesToStr([ // $mod.$main
  32309. '$mod.s = rtl.getResStr($mod, "Red");',
  32310. '$mod.s = rtl.getResStr($mod, "Red");',
  32311. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32312. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32313. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32314. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32315. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32316. '']));
  32317. end;
  32318. procedure TTestModule.TestResourcestringUnit;
  32319. begin
  32320. AddModuleWithIntfImplSrc('unit2.pas',
  32321. LinesToStr([
  32322. 'resourcestring Title = ''Nice'';',
  32323. '']),
  32324. '');
  32325. StartUnit(true);
  32326. Add([
  32327. 'interface',
  32328. 'uses unit2;',
  32329. 'const Red = ''rEd'';',
  32330. 'resourcestring',
  32331. ' Blue = ''blue'';',
  32332. ' NotRed = ''not''+Red;',
  32333. 'var s: string;',
  32334. 'implementation',
  32335. 'resourcestring',
  32336. ' ImplGreen = ''green'';',
  32337. 'initialization',
  32338. ' s:=blue+ImplGreen;',
  32339. ' s:=test1.blue+test1.implgreen;',
  32340. ' s:=blue[1]+implgreen[2];',
  32341. ' s:=Title;',
  32342. '']);
  32343. ConvertUnit;
  32344. CheckSource('TestResourcestringUnit',
  32345. LinesToStr([ // statements
  32346. 'this.Red = "rEd";',
  32347. 'this.s = "";',
  32348. '$mod.$resourcestrings = {',
  32349. ' Blue: {',
  32350. ' org: "blue"',
  32351. ' },',
  32352. ' NotRed: {',
  32353. ' org: "notrEd"',
  32354. ' },',
  32355. ' ImplGreen: {',
  32356. ' org: "green"',
  32357. ' }',
  32358. '};',
  32359. '']),
  32360. LinesToStr([ // $mod.$main
  32361. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32362. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32363. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32364. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32365. '']));
  32366. end;
  32367. procedure TTestModule.TestResourcestringImplementation;
  32368. begin
  32369. StartUnit(false);
  32370. Add([
  32371. 'interface',
  32372. 'implementation',
  32373. 'resourcestring',
  32374. ' ImplRed = ''red'';']);
  32375. ConvertUnit;
  32376. CheckSource('TestResourcestringImplementation',
  32377. LinesToStr([ // intf statements
  32378. 'var $impl = $mod.$impl;']),
  32379. LinesToStr([ // $mod.$init
  32380. '']),
  32381. LinesToStr([ // impl statements
  32382. '$mod.$resourcestrings = {',
  32383. ' ImplRed: {',
  32384. ' org: "red"',
  32385. ' }',
  32386. '};',
  32387. '']));
  32388. end;
  32389. procedure TTestModule.TestAttributes_Members;
  32390. begin
  32391. WithTypeInfo:=true;
  32392. StartProgram(false);
  32393. Add([
  32394. '{$modeswitch PrefixedAttributes}',
  32395. 'type',
  32396. ' TObject = class',
  32397. ' constructor Create;',
  32398. ' end;',
  32399. ' TCustomAttribute = class',
  32400. ' constructor Create(Id: word);',
  32401. ' end;',
  32402. ' [Missing]',
  32403. ' TBird = class',
  32404. ' published',
  32405. ' [Tcustom]',
  32406. ' FField: word;',
  32407. ' [tcustom(14)]',
  32408. ' property Size: word read FField;',
  32409. ' [Tcustom(15)]',
  32410. ' procedure Fly; virtual; abstract;',
  32411. ' end;',
  32412. ' TRec = record',
  32413. ' [Tcustom,tcustom(14)]',
  32414. ' Size: word;',
  32415. ' end;',
  32416. 'constructor TObject.Create; begin end;',
  32417. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32418. 'begin',
  32419. '']);
  32420. ConvertProgram;
  32421. CheckSource('TestAttributes_Members',
  32422. LinesToStr([ // statements
  32423. 'rtl.createClass(this, "TObject", null, function () {',
  32424. ' this.$init = function () {',
  32425. ' };',
  32426. ' this.$final = function () {',
  32427. ' };',
  32428. ' this.Create = function () {',
  32429. ' return this;',
  32430. ' };',
  32431. '});',
  32432. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32433. ' this.Create$1 = function (Id) {',
  32434. ' return this;',
  32435. ' };',
  32436. '});',
  32437. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32438. ' this.$init = function () {',
  32439. ' $mod.TObject.$init.call(this);',
  32440. ' this.FField = 0;',
  32441. ' };',
  32442. ' var $r = this.$rtti;',
  32443. ' $r.addField("FField", rtl.word, {',
  32444. ' attr: [$mod.TCustomAttribute, "Create"]',
  32445. ' });',
  32446. ' $r.addProperty(',
  32447. ' "Size",',
  32448. ' 0,',
  32449. ' rtl.word,',
  32450. ' "FField",',
  32451. ' "",',
  32452. ' {',
  32453. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  32454. ' }',
  32455. ' );',
  32456. ' $r.addMethod("Fly", 0, [], null, 0, {',
  32457. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  32458. ' });',
  32459. '});',
  32460. 'rtl.recNewT(this, "TRec", function () {',
  32461. ' this.Size = 0;',
  32462. ' this.$eq = function (b) {',
  32463. ' return this.Size === b.Size;',
  32464. ' };',
  32465. ' this.$assign = function (s) {',
  32466. ' this.Size = s.Size;',
  32467. ' return this;',
  32468. ' };',
  32469. ' var $r = $mod.$rtti.$Record("TRec", {});',
  32470. ' $r.addField("Size", rtl.word, {',
  32471. ' attr: [',
  32472. ' $mod.TCustomAttribute,',
  32473. ' "Create",',
  32474. ' $mod.TCustomAttribute,',
  32475. ' "Create$1",',
  32476. ' [14]',
  32477. ' ]',
  32478. ' });',
  32479. '});',
  32480. '']),
  32481. LinesToStr([ // $mod.$main
  32482. '']));
  32483. end;
  32484. procedure TTestModule.TestAttributes_Types;
  32485. begin
  32486. WithTypeInfo:=true;
  32487. StartProgram(false);
  32488. Add([
  32489. '{$modeswitch PrefixedAttributes}',
  32490. 'type',
  32491. ' TObject = class',
  32492. ' constructor Create(Id: word);',
  32493. ' end;',
  32494. ' TCustomAttribute = class',
  32495. ' end;',
  32496. ' [TCustom(1)]',
  32497. ' TMyClass = class',
  32498. ' end;',
  32499. ' [TCustom(11)]',
  32500. ' TMyDescendant = class(TMyClass)',
  32501. ' end;',
  32502. ' [TCustom(2)]',
  32503. ' TRec = record',
  32504. ' end;',
  32505. ' [TCustom(3)]',
  32506. ' TInt = type word;',
  32507. 'constructor TObject.Create(Id: word);',
  32508. 'begin',
  32509. 'end;',
  32510. 'var p: pointer;',
  32511. 'begin',
  32512. ' p:=typeinfo(TMyClass);',
  32513. ' p:=typeinfo(TRec);',
  32514. ' p:=typeinfo(TInt);',
  32515. '']);
  32516. ConvertProgram;
  32517. CheckSource('TestAttributes_Types',
  32518. LinesToStr([ // statements
  32519. 'rtl.createClass(this, "TObject", null, function () {',
  32520. ' this.$init = function () {',
  32521. ' };',
  32522. ' this.$final = function () {',
  32523. ' };',
  32524. ' this.Create = function (Id) {',
  32525. ' return this;',
  32526. ' };',
  32527. '});',
  32528. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32529. '});',
  32530. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  32531. ' var $r = this.$rtti;',
  32532. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  32533. '});',
  32534. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  32535. ' var $r = this.$rtti;',
  32536. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  32537. '});',
  32538. 'rtl.recNewT(this, "TRec", function () {',
  32539. ' this.$eq = function (b) {',
  32540. ' return true;',
  32541. ' };',
  32542. ' this.$assign = function (s) {',
  32543. ' return this;',
  32544. ' };',
  32545. ' $mod.$rtti.$Record("TRec", {',
  32546. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  32547. ' });',
  32548. '});',
  32549. 'this.$rtti.$inherited("TInt", rtl.word, {',
  32550. ' attr: [this.TCustomAttribute, "Create", [3]]',
  32551. '});',
  32552. 'this.p = null;',
  32553. '']),
  32554. LinesToStr([ // $mod.$main
  32555. '$mod.p = $mod.$rtti["TMyClass"];',
  32556. '$mod.p = $mod.$rtti["TRec"];',
  32557. '$mod.p = $mod.$rtti["TInt"];',
  32558. '']));
  32559. end;
  32560. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  32561. begin
  32562. WithTypeInfo:=true;
  32563. StartProgram(false);
  32564. Add([
  32565. '{$modeswitch PrefixedAttributes}',
  32566. 'type',
  32567. ' TObject = class',
  32568. ' constructor Create;',
  32569. ' end;',
  32570. ' TCustomAttribute = class',
  32571. ' end;',
  32572. ' THelper = class helper for TCustomAttribute',
  32573. ' constructor Create(Id: word);',
  32574. ' end;',
  32575. ' [TCustom(3)]',
  32576. ' TMyInt = word;',
  32577. 'constructor TObject.Create; begin end;',
  32578. 'constructor THelper.Create(Id: word); begin end;',
  32579. 'begin',
  32580. ' if typeinfo(TMyInt)=nil then ;']);
  32581. ConvertProgram;
  32582. end;
  32583. procedure TTestModule.TestAttributes_InterfacesList;
  32584. begin
  32585. WithTypeInfo:=true;
  32586. StartProgram(false);
  32587. Add([
  32588. '{$mode Delphi}',
  32589. 'type',
  32590. ' TObject = class',
  32591. ' constructor Create;',
  32592. ' end;',
  32593. ' IInterface = interface end;',
  32594. ' TCustomAttribute = class',
  32595. ' end;',
  32596. ' Red = class(TCustomAttribute);',
  32597. ' Blue = class(TCustomAttribute);',
  32598. ' [Red]',
  32599. ' IBird<T> = interface',
  32600. ' procedure Fly;',
  32601. ' end;',
  32602. ' [Blue]',
  32603. ' IEagle = interface(IBird<Word>)',
  32604. ' procedure Dive;',
  32605. ' end;',
  32606. ' TAnt = class(TObject, IEagle)',
  32607. ' procedure Fly; virtual; abstract;',
  32608. ' procedure Dive; virtual; abstract;',
  32609. ' end;',
  32610. 'constructor TObject.Create;',
  32611. 'begin',
  32612. 'end;',
  32613. 'begin',
  32614. '']);
  32615. ConvertProgram;
  32616. CheckSource('TestAttributes_InterfacesList',
  32617. LinesToStr([ // statements
  32618. '$mod.$rtti.$Interface("IBird<System.Word>");',
  32619. 'rtl.createClass(this, "TObject", null, function () {',
  32620. ' this.$init = function () {',
  32621. ' };',
  32622. ' this.$final = function () {',
  32623. ' };',
  32624. ' this.Create = function () {',
  32625. ' return this;',
  32626. ' };',
  32627. '});',
  32628. 'rtl.createInterface(',
  32629. ' this,',
  32630. ' "IInterface",',
  32631. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  32632. ' [],',
  32633. ' null,',
  32634. ' function () {',
  32635. ' this.$kind = "com";',
  32636. ' }',
  32637. ');',
  32638. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32639. '});',
  32640. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  32641. '});',
  32642. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  32643. '});',
  32644. 'rtl.createInterface(',
  32645. ' this,',
  32646. ' "IBird$G1",',
  32647. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  32648. ' ["Fly"],',
  32649. ' this.IInterface,',
  32650. ' function () {',
  32651. ' var $r = this.$rtti;',
  32652. ' $r.addMethod("Fly", 0, []);',
  32653. ' $r.attr = [$mod.Red, "Create"];',
  32654. ' },',
  32655. ' "IBird<System.Word>"',
  32656. ');',
  32657. 'rtl.createInterface(',
  32658. ' this,',
  32659. ' "IEagle",',
  32660. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  32661. ' ["Dive"],',
  32662. ' this.IBird$G1,',
  32663. ' function () {',
  32664. ' var $r = this.$rtti;',
  32665. ' $r.addMethod("Dive", 0, []);',
  32666. ' $r.attr = [$mod.Blue, "Create"];',
  32667. ' }',
  32668. ');',
  32669. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  32670. ' rtl.addIntf(this, $mod.IEagle);',
  32671. '});',
  32672. '']),
  32673. LinesToStr([ // $mod.$main
  32674. '']));
  32675. end;
  32676. procedure TTestModule.TestAssert;
  32677. begin
  32678. StartProgram(false);
  32679. Add([
  32680. 'procedure DoIt;',
  32681. 'var',
  32682. ' b: boolean;',
  32683. ' s: string;',
  32684. 'begin',
  32685. ' {$Assertions on}',
  32686. ' Assert(b);',
  32687. 'end;',
  32688. 'begin',
  32689. ' DoIt;',
  32690. '']);
  32691. ConvertProgram;
  32692. CheckSource('TestAssert',
  32693. LinesToStr([ // statements
  32694. 'this.DoIt = function () {',
  32695. ' var b = false;',
  32696. ' var s = "";',
  32697. ' if (!b) throw "assert failed";',
  32698. '};',
  32699. '']),
  32700. LinesToStr([ // $mod.$main
  32701. '$mod.DoIt();',
  32702. '']));
  32703. end;
  32704. procedure TTestModule.TestAssert_SysUtils;
  32705. begin
  32706. AddModuleWithIntfImplSrc('SysUtils.pas',
  32707. LinesToStr([
  32708. 'type',
  32709. ' TObject = class',
  32710. ' constructor Create;',
  32711. ' end;',
  32712. ' EAssertionFailed = class',
  32713. ' constructor Create(s: string);',
  32714. ' end;',
  32715. '']),
  32716. LinesToStr([
  32717. 'constructor TObject.Create;',
  32718. 'begin end;',
  32719. 'constructor EAssertionFailed.Create(s: string);',
  32720. 'begin end;',
  32721. '']) );
  32722. StartProgram(true);
  32723. Add([
  32724. 'uses sysutils;',
  32725. 'procedure DoIt;',
  32726. 'var',
  32727. ' b: boolean;',
  32728. ' s: string;',
  32729. 'begin',
  32730. ' {$Assertions on}',
  32731. ' Assert(b);',
  32732. ' Assert(b,''msg'');',
  32733. 'end;',
  32734. 'begin',
  32735. ' DoIt;',
  32736. '']);
  32737. ConvertProgram;
  32738. CheckSource('TestAssert_SysUtils',
  32739. LinesToStr([ // statements
  32740. 'this.DoIt = function () {',
  32741. ' var b = false;',
  32742. ' var s = "";',
  32743. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  32744. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  32745. '};',
  32746. '']),
  32747. LinesToStr([ // $mod.$main
  32748. '$mod.DoIt();',
  32749. '']));
  32750. end;
  32751. procedure TTestModule.TestObjectChecks;
  32752. begin
  32753. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  32754. StartProgram(false);
  32755. Add([
  32756. 'type',
  32757. ' TObject = class',
  32758. ' procedure DoIt;',
  32759. ' end;',
  32760. ' TClass = class of tobject;',
  32761. ' TBird = class',
  32762. ' end;',
  32763. ' TBirdClass = class of TBird;',
  32764. 'var',
  32765. ' o : TObject;',
  32766. ' c: TClass;',
  32767. ' b: TBird;',
  32768. ' bc: TBirdClass;',
  32769. 'procedure TObject.DoIt;',
  32770. 'begin',
  32771. ' b:=TBird(o);',
  32772. 'end;',
  32773. 'begin',
  32774. ' o.DoIt;',
  32775. ' b:=TBird(o);',
  32776. ' bc:=TBirdClass(c);',
  32777. '']);
  32778. ConvertProgram;
  32779. CheckSource('TestCheckMethodCall',
  32780. LinesToStr([ // statements
  32781. 'rtl.createClass(this, "TObject", null, function () {',
  32782. ' this.$init = function () {',
  32783. ' };',
  32784. ' this.$final = function () {',
  32785. ' };',
  32786. ' this.DoIt = function () {',
  32787. ' rtl.checkMethodCall(this,$mod.TObject);',
  32788. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  32789. ' };',
  32790. '});',
  32791. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32792. '});',
  32793. 'this.o = null;',
  32794. 'this.c = null;',
  32795. 'this.b = null;',
  32796. 'this.bc = null;',
  32797. '']),
  32798. LinesToStr([ // $mod.$main
  32799. '$mod.o.DoIt();',
  32800. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  32801. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  32802. '']));
  32803. end;
  32804. procedure TTestModule.TestOverflowChecks_Int;
  32805. begin
  32806. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  32807. StartProgram(false);
  32808. Add([
  32809. 'procedure DoIt;',
  32810. 'var',
  32811. ' b: byte;',
  32812. ' n: nativeint;',
  32813. ' u: nativeuint;',
  32814. ' c: currency;',
  32815. 'begin',
  32816. ' n:=n+n;',
  32817. ' n:=n-n;',
  32818. ' n:=n+b;',
  32819. ' n:=b-n;',
  32820. ' n:=n*n;',
  32821. ' n:=n*u;',
  32822. ' c:=c+b;',
  32823. ' c:=b+c;',
  32824. ' c:=c*b;',
  32825. ' c:=b*c;',
  32826. 'end;',
  32827. 'begin',
  32828. '']);
  32829. ConvertProgram;
  32830. CheckSource('TestOverflowChecks_Int',
  32831. LinesToStr([ // statements
  32832. 'this.DoIt = function () {',
  32833. ' var b = 0;',
  32834. ' var n = 0;',
  32835. ' var u = 0;',
  32836. ' var c = 0;',
  32837. ' n = rtl.oc(n + n);',
  32838. ' n = rtl.oc(n - n);',
  32839. ' n = rtl.oc(n + b);',
  32840. ' n = rtl.oc(b - n);',
  32841. ' n = rtl.oc(n * n);',
  32842. ' n = rtl.oc(n * u);',
  32843. ' c = rtl.oc(c + (b * 10000));',
  32844. ' c = rtl.oc((b * 10000) + c);',
  32845. ' c = rtl.oc(c * b);',
  32846. ' c = rtl.oc(b * c);',
  32847. '};',
  32848. '']),
  32849. LinesToStr([ // $mod.$main
  32850. '']));
  32851. end;
  32852. procedure TTestModule.TestRangeChecks_AssignInt;
  32853. begin
  32854. Scanner.Options:=Scanner.Options+[po_CAssignments];
  32855. StartProgram(false);
  32856. Add([
  32857. '{$R+}',
  32858. 'var',
  32859. ' b: byte = 2;',
  32860. ' w: word = 3;',
  32861. 'procedure DoIt(p: byte);',
  32862. 'begin',
  32863. ' b:=w;',
  32864. ' b+=w;',
  32865. ' b:=1;',
  32866. 'end;',
  32867. '{$R-}',
  32868. 'procedure DoSome;',
  32869. 'begin',
  32870. ' DoIt(w);',
  32871. ' b:=w;',
  32872. ' b:=2;',
  32873. 'end;',
  32874. 'begin',
  32875. '{$R+}',
  32876. '']);
  32877. ConvertProgram;
  32878. CheckSource('TestRangeChecks_AssignInt',
  32879. LinesToStr([ // statements
  32880. 'this.b = 2;',
  32881. 'this.w = 3;',
  32882. 'this.DoIt = function (p) {',
  32883. ' rtl.rc(p, 0, 255);',
  32884. ' $mod.b = rtl.rc($mod.w,0,255);',
  32885. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  32886. ' $mod.b = 1;',
  32887. '};',
  32888. 'this.DoSome = function () {',
  32889. ' $mod.DoIt($mod.w);',
  32890. ' $mod.b = $mod.w;',
  32891. ' $mod.b = 2;',
  32892. '};',
  32893. '']),
  32894. LinesToStr([ // $mod.$main
  32895. '']));
  32896. end;
  32897. procedure TTestModule.TestRangeChecks_AssignIntRange;
  32898. begin
  32899. Scanner.Options:=Scanner.Options+[po_CAssignments];
  32900. StartProgram(false);
  32901. Add([
  32902. '{$R+}',
  32903. 'type Ten = 1..10;',
  32904. 'var',
  32905. ' b: Ten = 2;',
  32906. ' w: Ten = 3;',
  32907. 'procedure DoIt(p: Ten);',
  32908. 'begin',
  32909. ' b:=w;',
  32910. ' b+=w;',
  32911. ' b:=1;',
  32912. 'end;',
  32913. '{$R-}',
  32914. 'procedure DoSome;',
  32915. 'begin',
  32916. ' DoIt(w);',
  32917. ' b:=w;',
  32918. ' b:=2;',
  32919. 'end;',
  32920. 'begin',
  32921. '{$R+}',
  32922. '']);
  32923. ConvertProgram;
  32924. CheckSource('TestRangeChecks_AssignIntRange',
  32925. LinesToStr([ // statements
  32926. 'this.b = 2;',
  32927. 'this.w = 3;',
  32928. 'this.DoIt = function (p) {',
  32929. ' rtl.rc(p, 1, 10);',
  32930. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  32931. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  32932. ' $mod.b = 1;',
  32933. '};',
  32934. 'this.DoSome = function () {',
  32935. ' $mod.DoIt($mod.w);',
  32936. ' $mod.b = $mod.w;',
  32937. ' $mod.b = 2;',
  32938. '};',
  32939. '']),
  32940. LinesToStr([ // $mod.$main
  32941. '']));
  32942. end;
  32943. procedure TTestModule.TestRangeChecks_AssignEnum;
  32944. begin
  32945. StartProgram(false);
  32946. Add([
  32947. '{$R+}',
  32948. 'type TEnum = (red,green);',
  32949. 'var',
  32950. ' e: TEnum = red;',
  32951. 'procedure DoIt(p: TEnum);',
  32952. 'begin',
  32953. ' e:=p;',
  32954. ' p:=TEnum(0);',
  32955. ' p:=succ(e);',
  32956. 'end;',
  32957. '{$R-}',
  32958. 'procedure DoSome;',
  32959. 'begin',
  32960. ' DoIt(e);',
  32961. ' e:=TEnum(1);',
  32962. ' e:=pred(e);',
  32963. 'end;',
  32964. 'begin',
  32965. '{$R+}',
  32966. '']);
  32967. ConvertProgram;
  32968. CheckSource('TestRangeChecks_AssignEnum',
  32969. LinesToStr([ // statements
  32970. 'this.TEnum = {',
  32971. ' "0": "red",',
  32972. ' red: 0,',
  32973. ' "1": "green",',
  32974. ' green: 1',
  32975. '};',
  32976. 'this.e = this.TEnum.red;',
  32977. 'this.DoIt = function (p) {',
  32978. ' rtl.rc(p, 0, 1);',
  32979. ' $mod.e = rtl.rc(p, 0, 1);',
  32980. ' p = 0;',
  32981. ' p = rtl.rc($mod.e + 1, 0, 1);',
  32982. '};',
  32983. 'this.DoSome = function () {',
  32984. ' $mod.DoIt($mod.e);',
  32985. ' $mod.e = 1;',
  32986. ' $mod.e = $mod.e - 1;',
  32987. '};',
  32988. '']),
  32989. LinesToStr([ // $mod.$main
  32990. '']));
  32991. end;
  32992. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  32993. begin
  32994. StartProgram(false);
  32995. Add([
  32996. '{$R+}',
  32997. 'type',
  32998. ' TEnum = (red,green);',
  32999. ' TEnumRg = red..green;',
  33000. 'var',
  33001. ' e: TEnumRg = red;',
  33002. 'procedure DoIt(p: TEnumRg);',
  33003. 'begin',
  33004. ' e:=p;',
  33005. ' p:=TEnumRg(0);',
  33006. ' p:=succ(e);',
  33007. 'end;',
  33008. '{$R-}',
  33009. 'procedure DoSome;',
  33010. 'begin',
  33011. ' DoIt(e);',
  33012. ' e:=TEnum(1);',
  33013. ' e:=pred(e);',
  33014. 'end;',
  33015. 'begin',
  33016. '{$R+}',
  33017. '']);
  33018. ConvertProgram;
  33019. CheckSource('TestRangeChecks_AssignEnumRange',
  33020. LinesToStr([ // statements
  33021. 'this.TEnum = {',
  33022. ' "0": "red",',
  33023. ' red: 0,',
  33024. ' "1": "green",',
  33025. ' green: 1',
  33026. '};',
  33027. 'this.e = this.TEnum.red;',
  33028. 'this.DoIt = function (p) {',
  33029. ' rtl.rc(p, 0, 1);',
  33030. ' $mod.e = rtl.rc(p, 0, 1);',
  33031. ' p = 0;',
  33032. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33033. '};',
  33034. 'this.DoSome = function () {',
  33035. ' $mod.DoIt($mod.e);',
  33036. ' $mod.e = 1;',
  33037. ' $mod.e = $mod.e - 1;',
  33038. '};',
  33039. '']),
  33040. LinesToStr([ // $mod.$main
  33041. '']));
  33042. end;
  33043. procedure TTestModule.TestRangeChecks_AssignChar;
  33044. begin
  33045. StartProgram(false);
  33046. Add([
  33047. '{$R+}',
  33048. 'type',
  33049. ' TLetter = char;',
  33050. 'var',
  33051. ' b: TLetter = ''2'';',
  33052. ' w: TLetter = ''3'';',
  33053. 'procedure DoIt(p: TLetter);',
  33054. 'begin',
  33055. ' b:=w;',
  33056. ' b:=''1'';',
  33057. 'end;',
  33058. '{$R-}',
  33059. 'procedure DoSome;',
  33060. 'begin',
  33061. ' DoIt(w);',
  33062. ' b:=w;',
  33063. ' b:=''2'';',
  33064. 'end;',
  33065. 'begin',
  33066. '{$R+}',
  33067. '']);
  33068. ConvertProgram;
  33069. CheckSource('TestRangeChecks_AssignChar',
  33070. LinesToStr([ // statements
  33071. 'this.b = "2";',
  33072. 'this.w = "3";',
  33073. 'this.DoIt = function (p) {',
  33074. ' rtl.rcc(p, 0, 65535);',
  33075. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33076. ' $mod.b = "1";',
  33077. '};',
  33078. 'this.DoSome = function () {',
  33079. ' $mod.DoIt($mod.w);',
  33080. ' $mod.b = $mod.w;',
  33081. ' $mod.b = "2";',
  33082. '};',
  33083. '']),
  33084. LinesToStr([ // $mod.$main
  33085. '']));
  33086. end;
  33087. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33088. begin
  33089. StartProgram(false);
  33090. Add([
  33091. '{$R+}',
  33092. 'type TDigit = ''0''..''9'';',
  33093. 'var',
  33094. ' b: TDigit = ''2'';',
  33095. ' w: TDigit = ''3'';',
  33096. 'procedure DoIt(p: TDigit);',
  33097. 'begin',
  33098. ' b:=w;',
  33099. ' b:=''1'';',
  33100. 'end;',
  33101. '{$R-}',
  33102. 'procedure DoSome;',
  33103. 'begin',
  33104. ' DoIt(w);',
  33105. ' b:=w;',
  33106. ' b:=''2'';',
  33107. 'end;',
  33108. 'begin',
  33109. '{$R+}',
  33110. '']);
  33111. ConvertProgram;
  33112. CheckSource('TestRangeChecks_AssignCharRange',
  33113. LinesToStr([ // statements
  33114. 'this.b = "2";',
  33115. 'this.w = "3";',
  33116. 'this.DoIt = function (p) {',
  33117. ' rtl.rcc(p, 48, 57);',
  33118. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33119. ' $mod.b = "1";',
  33120. '};',
  33121. 'this.DoSome = function () {',
  33122. ' $mod.DoIt($mod.w);',
  33123. ' $mod.b = $mod.w;',
  33124. ' $mod.b = "2";',
  33125. '};',
  33126. '']),
  33127. LinesToStr([ // $mod.$main
  33128. '']));
  33129. end;
  33130. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33131. begin
  33132. StartProgram(false);
  33133. Add([
  33134. '{$R+}',
  33135. 'type',
  33136. ' Ten = 1..10;',
  33137. ' TArr = array of Ten;',
  33138. ' TArrArr = array of TArr;',
  33139. ' TArrByte = array[byte] of Ten;',
  33140. ' TArrChar = array[''0''..''9''] of Ten;',
  33141. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33142. ' TObject = class',
  33143. ' A: TArr;',
  33144. ' end;',
  33145. 'procedure DoIt;',
  33146. 'var',
  33147. ' Arr: TArr;',
  33148. ' ArrArr: TArrArr;',
  33149. ' ArrByte: TArrByte;',
  33150. ' ArrChar: TArrChar;',
  33151. ' ArrByteChar: TArrByteChar;',
  33152. ' i: Ten;',
  33153. ' c: char;',
  33154. ' o: tobject;',
  33155. 'begin',
  33156. ' i:=Arr[1];',
  33157. ' i:=ArrByteChar[1,''2''];',
  33158. ' Arr[1]:=Arr[1];',
  33159. ' Arr[i]:=Arr[i];',
  33160. ' ArrByte[3]:=ArrByte[3];',
  33161. ' ArrByte[i]:=ArrByte[i];',
  33162. ' ArrChar[''5'']:=ArrChar[''5''];',
  33163. ' ArrChar[c]:=ArrChar[c];',
  33164. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  33165. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  33166. ' o.a[i]:=o.a[i];',
  33167. 'end;',
  33168. 'begin',
  33169. '']);
  33170. ConvertProgram;
  33171. CheckSource('TestRangeChecks_ArrayIndex',
  33172. LinesToStr([ // statements
  33173. 'this.TArrByteChar$clone = function (a) {',
  33174. ' var b = [];',
  33175. ' b.length = 256;',
  33176. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  33177. ' return b;',
  33178. '};',
  33179. 'rtl.createClass(this, "TObject", null, function () {',
  33180. ' this.$init = function () {',
  33181. ' this.A = [];',
  33182. ' };',
  33183. ' this.$final = function () {',
  33184. ' this.A = undefined;',
  33185. ' };',
  33186. '});',
  33187. 'this.DoIt = function () {',
  33188. ' var Arr = [];',
  33189. ' var ArrArr = [];',
  33190. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  33191. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  33192. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  33193. ' var i = 0;',
  33194. ' var c = "";',
  33195. ' var o = null;',
  33196. ' i = rtl.rc(Arr[1], 1, 10);',
  33197. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  33198. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  33199. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  33200. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  33201. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  33202. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  33203. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  33204. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  33205. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  33206. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  33207. '};',
  33208. '']),
  33209. LinesToStr([ // $mod.$main
  33210. '']));
  33211. end;
  33212. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  33213. begin
  33214. StartProgram(false);
  33215. Add([
  33216. '{$R+}',
  33217. 'type',
  33218. ' Ten = 1..10;',
  33219. ' TRec = record x: Ten end;',
  33220. ' TArr = array of TRec;',
  33221. ' TArrArr = array of TArr;',
  33222. ' TObject = class',
  33223. ' A: TArr;',
  33224. ' end;',
  33225. 'procedure DoIt;',
  33226. 'var',
  33227. ' Arr: TArr;',
  33228. ' ArrArr: TArrArr;',
  33229. ' i: Ten;',
  33230. ' o: tobject;',
  33231. 'begin',
  33232. ' Arr[1]:=Arr[1];',
  33233. ' Arr[i]:=Arr[i+1];',
  33234. ' o.a[i]:=o.a[i+2];',
  33235. 'end;',
  33236. 'begin',
  33237. '']);
  33238. ConvertProgram;
  33239. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  33240. LinesToStr([ // statements
  33241. 'rtl.recNewT(this, "TRec", function () {',
  33242. ' this.x = 0;',
  33243. ' this.$eq = function (b) {',
  33244. ' return this.x === b.x;',
  33245. ' };',
  33246. ' this.$assign = function (s) {',
  33247. ' this.x = s.x;',
  33248. ' return this;',
  33249. ' };',
  33250. '});',
  33251. 'rtl.createClass(this, "TObject", null, function () {',
  33252. ' this.$init = function () {',
  33253. ' this.A = [];',
  33254. ' };',
  33255. ' this.$final = function () {',
  33256. ' this.A = undefined;',
  33257. ' };',
  33258. '});',
  33259. 'this.DoIt = function () {',
  33260. ' var Arr = [];',
  33261. ' var ArrArr = [];',
  33262. ' var i = 0;',
  33263. ' var o = null;',
  33264. ' Arr[1].$assign(Arr[1]);',
  33265. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  33266. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  33267. '};',
  33268. '']),
  33269. LinesToStr([ // $mod.$main
  33270. '']));
  33271. end;
  33272. procedure TTestModule.TestRangeChecks_StringIndex;
  33273. begin
  33274. StartProgram(false);
  33275. Add([
  33276. 'type',
  33277. ' TObject = class',
  33278. ' S: string;',
  33279. ' end;',
  33280. '{$R+}',
  33281. 'procedure DoIt(var h: string);',
  33282. 'var',
  33283. ' s: string;',
  33284. ' i: longint;',
  33285. ' c: char;',
  33286. ' o: tobject;',
  33287. 'begin',
  33288. ' c:=s[1];',
  33289. ' s[i]:=s[i];',
  33290. ' h[i]:=h[i];',
  33291. ' c:=o.s[i];',
  33292. ' o.s[i]:=c;',
  33293. 'end;',
  33294. 'begin',
  33295. '']);
  33296. ConvertProgram;
  33297. CheckSource('TestRangeChecks_StringIndex',
  33298. LinesToStr([ // statements
  33299. 'rtl.createClass(this, "TObject", null, function () {',
  33300. ' this.$init = function () {',
  33301. ' this.S = "";',
  33302. ' };',
  33303. ' this.$final = function () {',
  33304. ' };',
  33305. '});',
  33306. 'this.DoIt = function (h) {',
  33307. ' var s = "";',
  33308. ' var i = 0;',
  33309. ' var c = "";',
  33310. ' var o = null;',
  33311. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33312. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33313. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33314. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33315. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33316. '};',
  33317. '']),
  33318. LinesToStr([ // $mod.$main
  33319. '']));
  33320. end;
  33321. procedure TTestModule.TestRangeChecks_TypecastInt;
  33322. begin
  33323. StartProgram(false);
  33324. Add([
  33325. '{$R+}',
  33326. 'var',
  33327. ' i: nativeint;',
  33328. ' b: byte;',
  33329. ' sh: shortint;',
  33330. ' w: word;',
  33331. ' sm: smallint;',
  33332. ' lw: longword;',
  33333. ' li: longint;',
  33334. 'begin',
  33335. ' b:=12+byte(i);',
  33336. ' sh:=12+shortint(i);',
  33337. ' w:=12+word(i);',
  33338. ' sm:=12+smallint(i);',
  33339. ' lw:=12+longword(i);',
  33340. ' li:=12+longint(i);',
  33341. '']);
  33342. ConvertProgram;
  33343. CheckSource('TestRangeChecks_TypecastInt',
  33344. LinesToStr([
  33345. 'this.i = 0;',
  33346. 'this.b = 0;',
  33347. 'this.sh = 0;',
  33348. 'this.w = 0;',
  33349. 'this.sm = 0;',
  33350. 'this.lw = 0;',
  33351. 'this.li = 0;',
  33352. '']),
  33353. LinesToStr([
  33354. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33355. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33356. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33357. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33358. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33359. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33360. '']));
  33361. end;
  33362. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33363. begin
  33364. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33365. StartProgram(false);
  33366. Add([
  33367. '{$modeswitch typehelpers}',
  33368. '{$R+}',
  33369. 'type',
  33370. ' TObject = class',
  33371. ' FSize: byte;',
  33372. ' property Size: byte read FSize;',
  33373. ' end;',
  33374. ' THelper = type helper for byte',
  33375. ' procedure SetIt(w: word);',
  33376. ' end;',
  33377. 'procedure THelper.SetIt(w: word);',
  33378. 'begin',
  33379. ' Self:=w;',
  33380. 'end;',
  33381. 'function GetIt: byte;',
  33382. 'begin',
  33383. ' Result.SetIt(2);',
  33384. 'end;',
  33385. 'var',
  33386. ' b: byte = 3;',
  33387. ' o: TObject;',
  33388. 'begin',
  33389. ' b.SetIt(14);',
  33390. ' with b do SetIt(15);',
  33391. ' o.Size.SetIt(16);',
  33392. '']);
  33393. ConvertProgram;
  33394. CheckSource('TestRangeChecks_AssignInt',
  33395. LinesToStr([ // statements
  33396. 'rtl.createClass(this, "TObject", null, function () {',
  33397. ' this.$init = function () {',
  33398. ' this.FSize = 0;',
  33399. ' };',
  33400. ' this.$final = function () {',
  33401. ' };',
  33402. '});',
  33403. 'rtl.createHelper(this, "THelper", null, function () {',
  33404. ' this.SetIt = function (w) {',
  33405. ' rtl.rc(w, 0, 65535);',
  33406. ' this.set(w);',
  33407. ' };',
  33408. '});',
  33409. 'this.GetIt = function () {',
  33410. ' var Result = 0;',
  33411. ' $mod.THelper.SetIt.call({',
  33412. ' get: function () {',
  33413. ' return Result;',
  33414. ' },',
  33415. ' set: function (v) {',
  33416. ' rtl.rc(v, 0, 255);',
  33417. ' Result = v;',
  33418. ' }',
  33419. ' }, 2);',
  33420. ' return Result;',
  33421. '};',
  33422. 'this.b = 3;',
  33423. 'this.o = null;',
  33424. '']),
  33425. LinesToStr([ // $mod.$main
  33426. '$mod.THelper.SetIt.call({',
  33427. ' p: $mod,',
  33428. ' get: function () {',
  33429. ' return this.p.b;',
  33430. ' },',
  33431. ' set: function (v) {',
  33432. ' rtl.rc(v, 0, 255);',
  33433. ' this.p.b = v;',
  33434. ' }',
  33435. '}, 14);',
  33436. 'var $with = $mod.b;',
  33437. '$mod.THelper.SetIt.call({',
  33438. ' get: function () {',
  33439. ' return $with;',
  33440. ' },',
  33441. ' set: function (v) {',
  33442. ' rtl.rc(v, 0, 255);',
  33443. ' $with = v;',
  33444. ' }',
  33445. '}, 15);',
  33446. '$mod.THelper.SetIt.call({',
  33447. ' p: $mod.o,',
  33448. ' get: function () {',
  33449. ' return this.p.FSize;',
  33450. ' },',
  33451. ' set: function (v) {',
  33452. ' rtl.rc(v, 0, 255);',
  33453. ' this.p.FSize = v;',
  33454. ' }',
  33455. '}, 16);',
  33456. '']));
  33457. end;
  33458. procedure TTestModule.TestAsync_Proc;
  33459. begin
  33460. StartProgram(false);
  33461. Add([
  33462. 'procedure Fly(w: word = 1); async; forward;',
  33463. 'procedure Run(w: word = 2); async;',
  33464. 'begin',
  33465. ' Fly(w);',
  33466. ' Fly;',
  33467. ' await(Fly(w));',
  33468. ' await(Fly);',
  33469. 'end;',
  33470. 'procedure Fly(w: word); ',
  33471. 'begin',
  33472. 'end;',
  33473. 'begin',
  33474. ' Run;',
  33475. ' Run(3);',
  33476. '']);
  33477. CheckResolverUnexpectedHints();
  33478. ConvertProgram;
  33479. CheckSource('TestAsync_Proc',
  33480. LinesToStr([ // statements
  33481. 'this.Run = async function (w) {',
  33482. ' $mod.Fly(w);',
  33483. ' $mod.Fly(1);',
  33484. ' await $mod.Fly(w);',
  33485. ' await $mod.Fly(1);',
  33486. '};',
  33487. 'this.Fly = async function (w) {',
  33488. '};',
  33489. '']),
  33490. LinesToStr([
  33491. '$mod.Run(2);',
  33492. '$mod.Run(3);',
  33493. '']));
  33494. end;
  33495. procedure TTestModule.TestAsync_CallResultIsPromise;
  33496. begin
  33497. StartProgram(false);
  33498. Add([
  33499. '{$modeswitch externalclass}',
  33500. 'type',
  33501. ' TObject = class',
  33502. ' end;',
  33503. ' TJSPromise = class external name ''Promise''',
  33504. ' end;',
  33505. ' TBird = class',
  33506. ' function Fly: word; async; ',
  33507. ' end;',
  33508. 'function TBird.Fly: word; async; ',
  33509. 'begin',
  33510. ' Result:=3;',
  33511. ' Fly:=4+Result;',
  33512. ' if Result=5 then ;',
  33513. ' exit(6);',
  33514. 'end;',
  33515. 'function Run: word; async;',
  33516. 'begin',
  33517. ' Result:=11+Result;',
  33518. ' inc(Result);',
  33519. 'end;',
  33520. 'var',
  33521. ' p: TJSPromise;',
  33522. ' o: TBird;',
  33523. 'begin',
  33524. ' p:=Run;',
  33525. ' p:=Run();',
  33526. ' if Run=p then ;',
  33527. ' if p=Run then ;',
  33528. ' if Run()=p then ;',
  33529. ' if p=Run() then ;',
  33530. ' p:=o.Fly;',
  33531. ' p:=o.Fly();',
  33532. ' if o.Fly=p then ;',
  33533. ' if o.Fly()=p then ;',
  33534. ' with o do begin',
  33535. ' p:=Fly;',
  33536. ' p:=Fly();',
  33537. ' if Fly=p then ;',
  33538. ' if Fly()=p then ;',
  33539. ' end;',
  33540. '']);
  33541. CheckResolverUnexpectedHints();
  33542. ConvertProgram;
  33543. CheckSource('TestAsync_CallResultIsPromise',
  33544. LinesToStr([ // statements
  33545. 'rtl.createClass(this, "TObject", null, function () {',
  33546. ' this.$init = function () {',
  33547. ' };',
  33548. ' this.$final = function () {',
  33549. ' };',
  33550. '});',
  33551. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33552. ' this.Fly = async function () {',
  33553. ' var Result = 0;',
  33554. ' Result = 3;',
  33555. ' Result = 4 + Result;',
  33556. ' if (Result === 5) ;',
  33557. ' return 6;',
  33558. ' return Result;',
  33559. ' };',
  33560. '});',
  33561. 'this.Run = async function () {',
  33562. ' var Result = 0;',
  33563. ' Result = 11 + Result;',
  33564. ' Result += 1;',
  33565. ' return Result;',
  33566. '};',
  33567. 'this.p = null;',
  33568. 'this.o = null;',
  33569. '']),
  33570. LinesToStr([
  33571. '$mod.p = $mod.Run();',
  33572. '$mod.p = $mod.Run();',
  33573. 'if ($mod.Run() === $mod.p) ;',
  33574. 'if ($mod.p === $mod.Run()) ;',
  33575. 'if ($mod.Run() === $mod.p) ;',
  33576. 'if ($mod.p === $mod.Run()) ;',
  33577. '$mod.p = $mod.o.Fly();',
  33578. '$mod.p = $mod.o.Fly();',
  33579. 'if ($mod.o.Fly() === $mod.p) ;',
  33580. 'if ($mod.o.Fly() === $mod.p) ;',
  33581. 'var $with = $mod.o;',
  33582. '$mod.p = $with.Fly();',
  33583. '$mod.p = $with.Fly();',
  33584. 'if ($with.Fly() === $mod.p) ;',
  33585. 'if ($with.Fly() === $mod.p) ;',
  33586. '']));
  33587. end;
  33588. procedure TTestModule.TestAsync_ConstructorFail;
  33589. begin
  33590. StartProgram(false);
  33591. Add([
  33592. 'type',
  33593. ' TObject = class',
  33594. ' end;',
  33595. ' TBird = class',
  33596. ' constructor Create; async;',
  33597. ' end;',
  33598. 'constructor TBird.Create; async;',
  33599. 'begin',
  33600. 'end;',
  33601. 'begin',
  33602. '']);
  33603. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  33604. ConvertProgram;
  33605. end;
  33606. procedure TTestModule.TestAsync_PropertyGetterFail;
  33607. begin
  33608. StartProgram(false);
  33609. Add([
  33610. 'type',
  33611. ' TObject = class',
  33612. ' end;',
  33613. ' TBird = class',
  33614. ' function GetSize: word; async;',
  33615. ' property Size: word read GetSize;',
  33616. ' end;',
  33617. 'function TBird.GetSize: word; async;',
  33618. 'begin',
  33619. 'end;',
  33620. 'begin',
  33621. '']);
  33622. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  33623. ConvertProgram;
  33624. end;
  33625. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  33626. begin
  33627. StartProgram(false);
  33628. Add([
  33629. 'procedure Run; async;',
  33630. 'begin',
  33631. ' await(word,1);',
  33632. 'end;',
  33633. 'begin',
  33634. '']);
  33635. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  33636. ConvertProgram;
  33637. end;
  33638. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  33639. begin
  33640. StartProgram(false);
  33641. Add([
  33642. 'type',
  33643. ' TObject = class',
  33644. ' end;',
  33645. ' TBird = class',
  33646. ' end;',
  33647. 'function Fly: TObject; async;',
  33648. 'begin',
  33649. 'end;',
  33650. 'procedure Run; async;',
  33651. 'begin',
  33652. ' await(TBird,Fly);',
  33653. 'end;',
  33654. 'begin',
  33655. '']);
  33656. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  33657. ConvertProgram;
  33658. end;
  33659. procedure TTestModule.TestAWait_OutsideAsyncFail;
  33660. begin
  33661. StartProgram(false);
  33662. Add([
  33663. 'procedure Crawl(w: double); ',
  33664. 'begin',
  33665. 'end;',
  33666. 'procedure Run(w: double);',
  33667. 'begin',
  33668. ' await(Crawl(w));',
  33669. 'end;',
  33670. 'begin',
  33671. ' Run(1);']);
  33672. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  33673. ConvertProgram;
  33674. end;
  33675. procedure TTestModule.TestAWait_IntegerFail;
  33676. begin
  33677. StartProgram(false);
  33678. Add([
  33679. 'function Run: word;',
  33680. 'begin',
  33681. 'end;',
  33682. 'procedure Fly(w: word); async;',
  33683. 'begin',
  33684. ' await(Run());',
  33685. 'end;',
  33686. 'begin',
  33687. ' Fly(1);']);
  33688. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  33689. ConvertProgram;
  33690. end;
  33691. procedure TTestModule.TestAWait_ExternalClassPromise;
  33692. begin
  33693. StartProgram(false);
  33694. Add([
  33695. '{$modeswitch externalclass}',
  33696. 'type',
  33697. ' TJSPromise = class external name ''Promise''',
  33698. ' end;',
  33699. ' TJSThenable = class external name ''Thenable''',
  33700. ' end;',
  33701. 'function Fly(w: word): TJSPromise;',
  33702. 'begin',
  33703. 'end;',
  33704. 'function Jump(w: word): word; async;',
  33705. 'begin',
  33706. 'end;',
  33707. 'function Eat(w: word): TJSPromise; async;',
  33708. 'begin',
  33709. 'end;',
  33710. 'function Run(d: double): word; async;',
  33711. 'var',
  33712. ' p: TJSPromise;',
  33713. 'begin',
  33714. ' Result:=await(word,p);', // promise needs type
  33715. ' Result:=await(word,Fly(3));', // promise needs type
  33716. ' Result:=await(Jump(4));', // async non promise must omit the type
  33717. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  33718. ' Result:=await(word,Eat(6));', // promise needs type
  33719. 'end;',
  33720. 'begin',
  33721. '']);
  33722. ConvertProgram;
  33723. CheckSource('TestAWait_ExternalClassPromise',
  33724. LinesToStr([ // statements
  33725. 'this.Fly = function (w) {',
  33726. ' var Result = null;',
  33727. ' return Result;',
  33728. '};',
  33729. 'this.Jump = async function (w) {',
  33730. ' var Result = 0;',
  33731. ' return Result;',
  33732. '};',
  33733. 'this.Eat = async function (w) {',
  33734. ' var Result = null;',
  33735. ' return Result;',
  33736. '};',
  33737. 'this.Run = async function (d) {',
  33738. ' var Result = 0;',
  33739. ' var p = null;',
  33740. ' Result = await p;',
  33741. ' Result = await $mod.Fly(3);',
  33742. ' Result = await $mod.Jump(4);',
  33743. ' Result = await $mod.Jump(5);',
  33744. ' Result = await $mod.Eat(6);',
  33745. ' return Result;',
  33746. '};',
  33747. '']),
  33748. LinesToStr([
  33749. ]));
  33750. CheckResolverUnexpectedHints();
  33751. end;
  33752. procedure TTestModule.TestAWait_JSValue;
  33753. begin
  33754. StartProgram(false);
  33755. Add([
  33756. '{$modeswitch externalclass}',
  33757. 'type',
  33758. ' TJSPromise = class external name ''Promise''',
  33759. ' end;',
  33760. 'function Fly(w: word): jsvalue; async;',
  33761. 'begin',
  33762. 'end;',
  33763. 'function Run(d: jsvalue; var e): word; async;',
  33764. 'begin',
  33765. ' Result:=await(word,d);', // promise needs type
  33766. ' d:=await(Fly(4));', // async non promise must omit the type
  33767. ' Result:=await(word,e);', // promise needs type
  33768. 'end;',
  33769. 'begin',
  33770. '']);
  33771. ConvertProgram;
  33772. CheckSource('TestAWait_JSValue',
  33773. LinesToStr([ // statements
  33774. 'this.Fly = async function (w) {',
  33775. ' var Result = undefined;',
  33776. ' return Result;',
  33777. '};',
  33778. 'this.Run = async function (d, e) {',
  33779. ' var Result = 0;',
  33780. ' Result = await d;',
  33781. ' d = await $mod.Fly(4);',
  33782. ' Result = await e.get();',
  33783. ' return Result;',
  33784. '};',
  33785. '']),
  33786. LinesToStr([
  33787. ]));
  33788. CheckResolverUnexpectedHints();
  33789. end;
  33790. procedure TTestModule.TestAWait_Result;
  33791. begin
  33792. StartProgram(false);
  33793. Add([
  33794. '{$modeswitch externalclass}',
  33795. 'type',
  33796. ' TJSPromise = class external name ''Promise''',
  33797. ' end;',
  33798. 'function Crawl(d: double = 1.3): TJSPromise; ',
  33799. 'begin',
  33800. 'end;',
  33801. 'function Run(d: double = 1.6): word; async;',
  33802. 'begin',
  33803. ' Result:=await(word,Crawl);',
  33804. ' Result:=await(word,Crawl(4.5));',
  33805. ' Result:=await(Run);',
  33806. ' Result:=await(Run(6.7));',
  33807. 'end;',
  33808. 'begin',
  33809. ' Run(1);']);
  33810. ConvertProgram;
  33811. CheckSource('TestAWait_Result',
  33812. LinesToStr([ // statements
  33813. 'this.Crawl = function (d) {',
  33814. ' var Result = null;',
  33815. ' return Result;',
  33816. '};',
  33817. 'this.Run = async function (d) {',
  33818. ' var Result = 0;',
  33819. ' Result = await $mod.Crawl(1.3);',
  33820. ' Result = await $mod.Crawl(4.5);',
  33821. ' Result = await $mod.Run(1.6);',
  33822. ' Result = await $mod.Run(6.7);',
  33823. ' return Result;',
  33824. '};',
  33825. '']),
  33826. LinesToStr([
  33827. '$mod.Run(1);'
  33828. ]));
  33829. CheckResolverUnexpectedHints();
  33830. end;
  33831. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  33832. begin
  33833. StartProgram(false);
  33834. Add([
  33835. '{$mode objfpc}',
  33836. '{$modeswitch externalclass}',
  33837. 'type',
  33838. ' TJSPromise = class external name ''Promise''',
  33839. ' end;',
  33840. 'function Run: TJSPromise; async;',
  33841. 'begin',
  33842. 'end;',
  33843. 'procedure Fly(w: word); async;',
  33844. 'begin',
  33845. ' await(Run());',
  33846. 'end;',
  33847. 'begin',
  33848. ' Fly(1);']);
  33849. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  33850. nWrongNumberOfParametersForCallTo);
  33851. ConvertProgram;
  33852. end;
  33853. procedure TTestModule.TestAsync_AnonymousProc;
  33854. begin
  33855. StartProgram(false);
  33856. Add([
  33857. '{$mode objfpc}',
  33858. '{$modeswitch externalclass}',
  33859. 'type',
  33860. ' TJSPromise = class external name ''Promise''',
  33861. ' end;',
  33862. 'type',
  33863. ' TFunc = reference to function(x: double): word; async;',
  33864. 'function Crawl(d: double = 1.3): word; async;',
  33865. 'begin',
  33866. 'end;',
  33867. 'var Func: TFunc;',
  33868. 'begin',
  33869. ' Func:=function(c:double):word async begin',
  33870. ' Result:=await(Crawl(c));',
  33871. ' end;',
  33872. ' Func:=function(c:double):word async assembler asm',
  33873. ' end;',
  33874. ' ']);
  33875. ConvertProgram;
  33876. CheckSource('TestAsync_AnonymousProc',
  33877. LinesToStr([ // statements
  33878. 'this.Crawl = async function (d) {',
  33879. ' var Result = 0;',
  33880. ' return Result;',
  33881. '};',
  33882. 'this.Func = null;',
  33883. '']),
  33884. LinesToStr([
  33885. '$mod.Func = async function (c) {',
  33886. ' var Result = 0;',
  33887. ' Result = await $mod.Crawl(c);',
  33888. ' return Result;',
  33889. '};',
  33890. '$mod.Func = async function (c) {',
  33891. '};',
  33892. '']));
  33893. CheckResolverUnexpectedHints();
  33894. end;
  33895. procedure TTestModule.TestAsync_AnonymousProc_PassAsyncAsArg;
  33896. begin
  33897. exit;
  33898. StartProgram(false);
  33899. Add([
  33900. '{$mode objfpc}',
  33901. '{$modeswitch externalclass}',
  33902. 'type',
  33903. ' TJSPromise = class external name ''Promise''',
  33904. ' end;',
  33905. 'type',
  33906. ' TFunc = reference to function(x: double): word; async;',
  33907. 'function Crawl: jsvalue; async;',
  33908. 'begin',
  33909. 'end;',
  33910. 'begin',
  33911. ' function(c:double):word async begin',
  33912. ' Result:=await(Crawl(c));',
  33913. ' end;',
  33914. ' Func:=function(c:double):word async assembler asm',
  33915. ' end;',
  33916. ' ']);
  33917. ConvertProgram;
  33918. CheckSource('TestAsync_AnonymousProc',
  33919. LinesToStr([ // statements
  33920. 'this.Crawl = async function (d) {',
  33921. ' var Result = 0;',
  33922. ' return Result;',
  33923. '};',
  33924. 'this.Func = null;',
  33925. '']),
  33926. LinesToStr([
  33927. '$mod.Func = async function (c) {',
  33928. ' var Result = 0;',
  33929. ' Result = await $mod.Crawl(c);',
  33930. ' return Result;',
  33931. '};',
  33932. '$mod.Func = async function (c) {',
  33933. '};',
  33934. '']));
  33935. CheckResolverUnexpectedHints();
  33936. end;
  33937. procedure TTestModule.TestAsync_ProcType;
  33938. begin
  33939. StartProgram(false);
  33940. Add([
  33941. '{$mode objfpc}',
  33942. 'type',
  33943. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  33944. ' TFunc = function(x: double = 1.1): word; async;',
  33945. ' TProc = procedure(x: longint = 7); async;',
  33946. 'function Crawl(d: double): word; async;',
  33947. 'begin',
  33948. 'end;',
  33949. 'procedure Run(e:longint); async;',
  33950. 'begin',
  33951. 'end;',
  33952. 'procedure Fly(p: TProc); async;',
  33953. 'begin',
  33954. ' await(p);',
  33955. ' await(p());',
  33956. 'end;',
  33957. 'var',
  33958. ' RefFunc: TRefFunc;',
  33959. ' Func: TFunc;',
  33960. ' Proc, ProcB: TProc;',
  33961. 'begin',
  33962. ' Func:=@Crawl;',
  33963. ' RefFunc:=@Crawl;',
  33964. ' RefFunc:=function(c:double):word async begin',
  33965. ' Result:=await(RefFunc);',
  33966. ' Result:=await(RefFunc());',
  33967. ' Result:=await(Func);',
  33968. ' Result:=await(Func());',
  33969. ' await(Proc);',
  33970. ' await(Proc());',
  33971. ' await(Proc(13));',
  33972. ' end;',
  33973. ' Proc:=@Run;',
  33974. ' if Proc=ProcB then ;',
  33975. ' ']);
  33976. ConvertProgram;
  33977. CheckResolverUnexpectedHints();
  33978. CheckSource('TestAsync_ProcType',
  33979. LinesToStr([ // statements
  33980. 'this.Crawl = async function (d) {',
  33981. ' var Result = 0;',
  33982. ' return Result;',
  33983. '};',
  33984. 'this.Run = async function (e) {',
  33985. '};',
  33986. 'this.Fly = async function (p) {',
  33987. ' await p(7);',
  33988. ' await p(7);',
  33989. '};',
  33990. 'this.RefFunc = null;',
  33991. 'this.Func = null;',
  33992. 'this.Proc = null;',
  33993. 'this.ProcB = null;',
  33994. '']),
  33995. LinesToStr([
  33996. '$mod.Func = $mod.Crawl;',
  33997. '$mod.RefFunc = $mod.Crawl;',
  33998. '$mod.RefFunc = async function (c) {',
  33999. ' var Result = 0;',
  34000. ' Result = await $mod.RefFunc(1.3);',
  34001. ' Result = await $mod.RefFunc(1.3);',
  34002. ' Result = await $mod.Func(1.1);',
  34003. ' Result = await $mod.Func(1.1);',
  34004. ' await $mod.Proc(7);',
  34005. ' await $mod.Proc(7);',
  34006. ' await $mod.Proc(13);',
  34007. ' return Result;',
  34008. '};',
  34009. '$mod.Proc = $mod.Run;',
  34010. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34011. '']));
  34012. end;
  34013. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34014. begin
  34015. StartProgram(false);
  34016. Add([
  34017. '{$mode objfpc}',
  34018. 'type',
  34019. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34020. 'function Crawl(d: double): word; async;',
  34021. 'begin',
  34022. 'end;',
  34023. 'var',
  34024. ' RefFunc: TRefFunc;',
  34025. 'begin',
  34026. ' RefFunc:=@Crawl;',
  34027. ' ']);
  34028. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34029. ConvertProgram;
  34030. end;
  34031. procedure TTestModule.TestAsync_Inherited;
  34032. begin
  34033. StartProgram(false);
  34034. Add([
  34035. '{$mode objfpc}',
  34036. '{$modeswitch externalclass}',
  34037. 'type',
  34038. ' TJSPromise = class external name ''Promise''',
  34039. ' end;',
  34040. ' TObject = class',
  34041. ' function Run(w: word = 3): word; async; virtual;',
  34042. ' end;',
  34043. ' TBird = class',
  34044. ' function Run(w: word = 3): word; async; override;',
  34045. ' end;',
  34046. 'function TObject.Run(w: word = 3): word; async;',
  34047. 'begin',
  34048. 'end;',
  34049. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34050. 'var p: TJSPromise;',
  34051. 'begin',
  34052. ' p:=inherited;',
  34053. ' p:=inherited Run;',
  34054. ' p:=inherited Run();',
  34055. ' p:=inherited Run(4);',
  34056. ' exit(p);',
  34057. ' exit(inherited);',
  34058. ' exit(inherited Run);',
  34059. ' exit(inherited Run(5));',
  34060. ' exit(6);',
  34061. 'end;',
  34062. 'begin',
  34063. ' ']);
  34064. ConvertProgram;
  34065. CheckSource('TestAsync_Inherited',
  34066. LinesToStr([ // statements
  34067. 'rtl.createClass(this, "TObject", null, function () {',
  34068. ' this.$init = function () {',
  34069. ' };',
  34070. ' this.$final = function () {',
  34071. ' };',
  34072. ' this.Run = async function (w) {',
  34073. ' var Result = 0;',
  34074. ' return Result;',
  34075. ' };',
  34076. '});',
  34077. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34078. ' this.Run = async function (w) {',
  34079. ' var Result = 0;',
  34080. ' var p = null;',
  34081. ' p = $mod.TObject.Run.apply(this, arguments);',
  34082. ' p = $mod.TObject.Run.call(this, 3);',
  34083. ' p = $mod.TObject.Run.call(this, 3);',
  34084. ' p = $mod.TObject.Run.call(this, 4);',
  34085. ' return p;',
  34086. ' return $mod.TObject.Run.apply(this, arguments);',
  34087. ' return $mod.TObject.Run.call(this, 3);',
  34088. ' return $mod.TObject.Run.call(this, 5);',
  34089. ' return 6;',
  34090. ' return Result;',
  34091. ' };',
  34092. '});',
  34093. '']),
  34094. LinesToStr([
  34095. '']));
  34096. CheckResolverUnexpectedHints();
  34097. end;
  34098. procedure TTestModule.TestAsync_ClassInterface;
  34099. begin
  34100. StartProgram(false);
  34101. Add([
  34102. '{$mode objfpc}',
  34103. '{$modeswitch externalclass}',
  34104. 'type',
  34105. ' TJSPromise = class external name ''Promise''',
  34106. ' end;',
  34107. ' IUnknown = interface',
  34108. ' function _AddRef: longint;',
  34109. ' function _Release: longint;',
  34110. ' end;',
  34111. 'function Say(i: IUnknown): IUnknown; async;',
  34112. 'begin',
  34113. 'end;',
  34114. 'function Run: IUnknown; async;',
  34115. 'begin',
  34116. ' Result:=await(Run);',
  34117. ' Result:=await(Run());',
  34118. ' Result:=await(Run) as IUnknown;',
  34119. ' Result:=await(Say(nil));',
  34120. ' Result:=await(Say(await(Run())));',
  34121. ' Result:=await(Say(await(Run()) as IUnknown));',
  34122. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  34123. 'end;',
  34124. 'procedure Fly;',
  34125. 'var p: TJSPromise;',
  34126. 'begin',
  34127. ' Run;',
  34128. ' Run();',
  34129. ' p:=Run;',
  34130. ' p:=Run();',
  34131. 'end;',
  34132. 'begin',
  34133. ' ']);
  34134. ConvertProgram;
  34135. CheckSource('TestAsync_ClassInterface',
  34136. LinesToStr([ // statements
  34137. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  34138. 'this.Say = async function (i) {',
  34139. ' var Result = null;',
  34140. ' return Result;',
  34141. '};',
  34142. 'this.Run = async function () {',
  34143. ' var Result = null;',
  34144. ' var $ok = false;',
  34145. ' try {',
  34146. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34147. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34148. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  34149. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  34150. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  34151. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  34152. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  34153. ' $ok = true;',
  34154. ' } finally {',
  34155. ' if (!$ok) rtl._Release(Result);',
  34156. ' };',
  34157. ' return Result;',
  34158. '};',
  34159. 'this.Fly = function () {',
  34160. ' var p = null;',
  34161. ' $mod.Run();',
  34162. ' $mod.Run();',
  34163. ' p = $mod.Run();',
  34164. ' p = $mod.Run();',
  34165. '};',
  34166. '']),
  34167. LinesToStr([
  34168. '']));
  34169. CheckResolverUnexpectedHints();
  34170. end;
  34171. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  34172. begin
  34173. StartProgram(true,[supTInterfacedObject]);
  34174. Add([
  34175. '{$mode objfpc}',
  34176. '{$modeswitch externalclass}',
  34177. 'type',
  34178. ' TJSPromise = class external name ''Promise''',
  34179. ' end;',
  34180. ' IBird = interface',
  34181. ' procedure Run;',
  34182. ' end;',
  34183. ' TBird = class(TInterfacedObject,IBird)',
  34184. ' procedure Run; async;',
  34185. ' end;',
  34186. 'procedure TBird.Run;',
  34187. 'begin',
  34188. 'end;',
  34189. 'begin',
  34190. ' ']);
  34191. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34192. ConvertProgram;
  34193. end;
  34194. procedure TTestModule.TestAWait_ClassAs;
  34195. begin
  34196. StartProgram(false);
  34197. Add([
  34198. '{$mode objfpc}',
  34199. '{$modeswitch externalclass}',
  34200. 'type',
  34201. ' TJSPromise = class external name ''Promise''',
  34202. ' end;',
  34203. ' TObject = class',
  34204. ' function Run: TObject; async;',
  34205. ' end;',
  34206. ' TBird = class',
  34207. ' function Fly: TBird; async;',
  34208. ' end;',
  34209. 'function TObject.Run: TObject; async;',
  34210. 'begin',
  34211. 'end;',
  34212. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  34213. 'var o: TObject;',
  34214. 'begin',
  34215. ' o:=await(TObject,Run);',
  34216. ' o:=await(TObject,Fly);',
  34217. ' o:=await(TBird,Fly);',
  34218. ' o:=await(TObject,inherited Run);',
  34219. ' o:=await(TObject,inherited Run) as TBird;',
  34220. 'end;',
  34221. 'begin',
  34222. ' ']);
  34223. ConvertProgram;
  34224. CheckSource('TestAWait_ClassAs',
  34225. LinesToStr([ // statements
  34226. 'rtl.createClass(this, "TObject", null, function () {',
  34227. ' this.$init = function () {',
  34228. ' };',
  34229. ' this.$final = function () {',
  34230. ' };',
  34231. ' this.Run = async function () {',
  34232. ' var Result = null;',
  34233. ' return Result;',
  34234. ' };',
  34235. '});',
  34236. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34237. ' this.Fly = async function () {',
  34238. ' var Result = null;',
  34239. ' var o = null;',
  34240. ' o = await this.Run();',
  34241. ' o = await this.Fly();',
  34242. ' o = await this.Fly();',
  34243. ' o = await $mod.TObject.Run.call(this);',
  34244. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  34245. ' return Result;',
  34246. ' };',
  34247. '});',
  34248. '']),
  34249. LinesToStr([
  34250. '']));
  34251. CheckResolverUnexpectedHints();
  34252. end;
  34253. procedure TTestModule.TestLibrary_Empty;
  34254. begin
  34255. StartLibrary(false);
  34256. Add([
  34257. '']);
  34258. ConvertLibrary;
  34259. CheckFullSource('TestLibrary_Empty',
  34260. LinesToStr([ // statements
  34261. 'rtl.module("library", [], function () {',
  34262. ' var $mod = this;',
  34263. ' $mod.$main = function () {',
  34264. ' };',
  34265. '});',
  34266. 'rtl.run("library");',
  34267. '']));
  34268. CheckResolverUnexpectedHints();
  34269. end;
  34270. procedure TTestModule.TestLibrary_ExportFunc;
  34271. begin
  34272. StartLibrary(false);
  34273. Add([
  34274. 'procedure Run(w: word);',
  34275. 'begin',
  34276. 'end;',
  34277. 'exports',
  34278. ' Run;',
  34279. ' run name ''Foo'';',
  34280. ' test1.run name ''Test1Run'';',
  34281. '']);
  34282. ConvertLibrary;
  34283. CheckFullSource('TestLibrary_ExportFunc',
  34284. LinesToStr([ // statements
  34285. 'rtl.module("library", [], function () {',
  34286. ' var $mod = this;',
  34287. ' this.Run = function (w) {',
  34288. ' };',
  34289. ' $mod.$main = function () {',
  34290. ' };',
  34291. '});',
  34292. 'rtl.run("library");',
  34293. 'export const Run = pas.library.Run;',
  34294. 'export const Foo = pas.library.Run;',
  34295. 'export const Test1Run = pas.library.Run;',
  34296. '']));
  34297. CheckResolverUnexpectedHints();
  34298. end;
  34299. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  34300. begin
  34301. StartLibrary(false);
  34302. Add([
  34303. 'procedure Run(w: word); overload;',
  34304. 'begin',
  34305. 'end;',
  34306. 'procedure Run(s: string); overload;',
  34307. 'begin',
  34308. 'end;',
  34309. 'exports',
  34310. ' Run;',
  34311. '']);
  34312. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  34313. nCantDetermineWhichOverloadedFunctionToCall);
  34314. ConvertLibrary;
  34315. end;
  34316. procedure TTestModule.TestLibrary_Export_Index_Fail;
  34317. begin
  34318. StartLibrary(false);
  34319. Add([
  34320. 'procedure Run(w: word);',
  34321. 'begin',
  34322. 'end;',
  34323. 'exports',
  34324. ' Run index 3;',
  34325. '']);
  34326. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  34327. ConvertLibrary;
  34328. end;
  34329. procedure TTestModule.TestLibrary_ExportVar;
  34330. begin
  34331. StartLibrary(false);
  34332. Add([
  34333. 'var Wing: word;',
  34334. 'exports',
  34335. ' Wing, wing name ''BirdArm'';',
  34336. '']);
  34337. ConvertLibrary;
  34338. CheckFullSource('TestLibrary_ExportVar',
  34339. LinesToStr([ // statements
  34340. 'rtl.module("library", [], function () {',
  34341. ' var $mod = this;',
  34342. ' this.Wing = 0;',
  34343. ' $mod.$main = function () {',
  34344. ' };',
  34345. '});',
  34346. 'rtl.run("library");',
  34347. 'export const vars = {};',
  34348. 'Object.defineProperties(vars, {',
  34349. ' Wing: {',
  34350. ' enumerable: true,',
  34351. ' get: function () {',
  34352. ' return pas.library.Wing;',
  34353. ' },',
  34354. ' set: function (v) {',
  34355. ' pas.library.Wing = v;',
  34356. ' }',
  34357. ' },',
  34358. ' BirdArm: {',
  34359. ' enumerable: true,',
  34360. ' get: function () {',
  34361. ' return pas.library.Wing;',
  34362. ' },',
  34363. ' set: function (v) {',
  34364. ' pas.library.Wing = v;',
  34365. ' }',
  34366. ' }',
  34367. '});',
  34368. '']));
  34369. CheckResolverUnexpectedHints();
  34370. end;
  34371. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34372. begin
  34373. AddModuleWithIntfImplSrc('Unit1.pas',
  34374. LinesToStr([
  34375. 'type',
  34376. ' TAnt = class',
  34377. ' class function Crawl: word; static;',
  34378. ' end;',
  34379. 'function Fly: word;',
  34380. '']),
  34381. LinesToStr([
  34382. 'function Fly: word;',
  34383. 'begin',
  34384. 'end;',
  34385. 'class function TAnt.Crawl: word;',
  34386. 'begin',
  34387. 'end;',
  34388. '']));
  34389. StartLibrary(true,[supTObject]);
  34390. Add([
  34391. 'uses unit1;',
  34392. 'exports',
  34393. ' Fly;',
  34394. ' TAnt.Crawl;',
  34395. '']);
  34396. ConvertLibrary;
  34397. CheckFullSource('TestLibrary_ExportUnitFunc',
  34398. LinesToStr([ // statements
  34399. 'rtl.module("library", ["system", "Unit1"], function () {',
  34400. ' var $mod = this;',
  34401. ' $mod.$main = function () {',
  34402. ' };',
  34403. '});',
  34404. 'rtl.run("library");',
  34405. 'export const Fly = pas.Unit1.Fly;',
  34406. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  34407. '']));
  34408. CheckResolverUnexpectedHints();
  34409. end;
  34410. Initialization
  34411. RegisterTests([TTestModule]);
  34412. end.