tcmodules.pas 944 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812
  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 AnsiChar = (
  34. '#', // mkLabel
  35. '@', // mkResolverReference
  36. '=' // mkDirectReference
  37. );
  38. type
  39. PSrcMarker = ^TSrcMarker;
  40. TSrcMarker = record
  41. Kind: TSrcMarkerKind;
  42. Filename: string;
  43. Row: integer;
  44. StartCol, EndCol: integer; // token start, end column
  45. Identifier: string;
  46. Next: PSrcMarker;
  47. end;
  48. TSystemUnitPart = (
  49. supTObject,
  50. supTVarRec,
  51. supTypeInfo,
  52. supTInterfacedObject,
  53. supWriteln
  54. );
  55. TSystemUnitParts = set of TSystemUnitPart;
  56. { TTestHintMessage }
  57. TTestHintMessage = class
  58. public
  59. Id: int64;
  60. MsgType: TMessageType;
  61. MsgNumber: integer;
  62. Msg: string;
  63. SourcePos: TPasSourcePos;
  64. end;
  65. TTestResolverReferenceData = record
  66. Filename: string;
  67. Row: integer;
  68. StartCol: integer;
  69. EndCol: integer;
  70. Found: TFPList; // list of TPasElement at this token
  71. end;
  72. PTestResolverReferenceData = ^TTestResolverReferenceData;
  73. { TTestPasParser }
  74. TTestPasParser = Class(TPasParser)
  75. end;
  76. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  77. { TTestEnginePasResolver }
  78. TTestEnginePasResolver = class(TPas2JsResolver)
  79. private
  80. FFilename: string;
  81. FModule: TPasModule;
  82. FOnFindUnit: TOnFindUnit;
  83. FParser: TTestPasParser;
  84. FStreamResolver: TStreamResolver;
  85. FScanner: TPas2jsPasScanner;
  86. FSource: string;
  87. procedure SetModule(const AValue: TPasModule);
  88. public
  89. destructor Destroy; override;
  90. function CreateElement(AClass: TPTreeElement; const AName: String;
  91. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  92. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  93. overload; override;
  94. function FindUnit(const AName, InFilename: String; NameExpr,
  95. InFileExpr: TPasExpr): TPasModule; override;
  96. procedure UsedInterfacesFinished(Section: TPasSection); override;
  97. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  98. property Filename: string read FFilename write FFilename;
  99. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  100. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  101. property Parser: TTestPasParser read FParser write FParser;
  102. property Source: string read FSource write FSource;
  103. property Module: TPasModule read FModule write SetModule;
  104. end;
  105. { TCustomTestModule }
  106. TCustomTestModule = Class(TTestCase)
  107. private
  108. FWithTypeInfo: boolean;
  109. FSource: TStringList;
  110. FSkipTests: boolean;
  111. FScanner: TPas2jsPasScanner;
  112. FResolvers: TObjectList;// list of TTestEnginePasResolver
  113. FPasProgram: TPasProgram;
  114. FPasLibrary: TPasLibrary;
  115. FParser: TTestPasParser;
  116. FModule: TPasModule;
  117. FJSSource: TStringList;
  118. FJSRegModuleCall: TJSCallExpression;
  119. FJSModuleSrc: TJSSourceElements;
  120. FJSModuleCallArgs: TJSArguments;
  121. FJSModule: TJSSourceElements;
  122. FJSInterfaceUses: TJSArrayLiteral;
  123. FJSInitBody: TJSFunctionBody;
  124. FJSImplentationUses: TJSArrayLiteral;
  125. FJSImplementationUses: TJSArrayLiteral;
  126. FHub: TPas2JSResolverHub;
  127. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  128. FHintMsgs: TObjectList; // list of TTestHintMessage
  129. FFirstPasStatement: TPasImplBlock;
  130. FFileResolver: TStreamResolver;
  131. FFilename: string;
  132. FExpectedErrorNumber: integer;
  133. FExpectedErrorMsg: string;
  134. FExpectedErrorClass: ExceptClass;
  135. FEngine: TTestEnginePasResolver;
  136. FConverter: TPasToJSConverter;
  137. {$IFDEF EnablePasTreeGlobalRefCount}
  138. FElementRefCountAtSetup: int64;
  139. {$ENDIF}
  140. procedure FreeSrcMarkers;
  141. function GetResolverCount: integer;
  142. function GetResolvers(Index: integer): TTestEnginePasResolver;
  143. function GetMsgCount: integer;
  144. function GetMsgs(Index: integer): TTestHintMessage;
  145. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  146. procedure OnParserLog(Sender: TObject; const Msg: String);
  147. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  148. procedure OnScannerLog(Sender: TObject; const Msg: String);
  149. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  150. procedure OnFindReference(El: TPasElement; FindData: pointer);
  151. procedure SetWithTypeInfo(const AValue: boolean);
  152. protected
  153. procedure SetUp; override;
  154. function CreateConverter: TPasToJSConverter; virtual;
  155. function LoadUnit(const aUnitName: String): TPasModule;
  156. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  157. procedure TearDown; override;
  158. Procedure Add(Line: string); virtual;
  159. Procedure Add(const Lines: array of string);
  160. Procedure StartParsing; virtual;
  161. procedure ParseModuleQueue; virtual;
  162. procedure ParseModule; virtual;
  163. procedure ParseProgram; virtual;
  164. procedure ParseLibrary; virtual;
  165. procedure ParseUnit; virtual;
  166. protected
  167. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  168. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  169. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  172. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  173. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  174. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  175. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure ConvertModule; virtual;
  178. procedure ConvertProgram; virtual;
  179. procedure ConvertLibrary; virtual;
  180. procedure ConvertUnit; virtual;
  181. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  182. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  183. function GetDottedIdentifier(El: TJSElement): string;
  184. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  185. ImplStatements: string = ''); virtual;
  186. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  187. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  188. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  189. procedure CheckReferenceDirectives; virtual;
  190. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  191. Msg: string; Marker: PSrcMarker = nil); virtual;
  192. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  193. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  194. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  197. function IsErrorExpected(E: Exception): boolean;
  198. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  199. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  200. procedure HandleScannerError(E: EScannerError);
  201. procedure HandleParserError(E: EParserError);
  202. procedure HandlePasResolveError(E: EPasResolve);
  203. procedure HandlePas2JSError(E: EPas2JS);
  204. procedure HandleException(E: Exception);
  205. procedure FailException(E: Exception);
  206. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  207. function IndexOfResolver(const Filename: string): integer;
  208. function GetResolver(const Filename: string): TTestEnginePasResolver;
  209. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  210. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  211. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  212. function FindSrcLabel(const Identifier: string): PSrcMarker;
  213. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  214. function GetDefaultNamespace: string;
  215. property PasProgram: TPasProgram Read FPasProgram;
  216. property PasLibrary: TPasLibrary Read FPasLibrary;
  217. property ResolverEngine: TTestEnginePasResolver read FEngine;
  218. property Filename: string read FFilename;
  219. Property Module: TPasModule Read FModule;
  220. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  221. property Converter: TPasToJSConverter read FConverter;
  222. property JSSource: TStringList read FJSSource;
  223. property JSModule: TJSSourceElements read FJSModule;
  224. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  225. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  226. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  227. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  228. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  229. property JSInitBody: TJSFunctionBody read FJSInitBody;
  230. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  231. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  232. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  233. property SkipTests: boolean read FSkipTests write FSkipTests;
  234. public
  235. constructor Create; override;
  236. destructor Destroy; override;
  237. property Hub: TPas2JSResolverHub read FHub;
  238. property Source: TStringList read FSource;
  239. property FileResolver: TStreamResolver read FFileResolver;
  240. property Scanner: TPas2jsPasScanner read FScanner;
  241. property Parser: TTestPasParser read FParser;
  242. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  243. property ResolverCount: integer read GetResolverCount;
  244. property MsgCount: integer read GetMsgCount;
  245. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  246. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  247. end;
  248. { TTestModule }
  249. TTestModule = class(TCustomTestModule)
  250. Published
  251. Procedure TestReservedWords;
  252. // program, units, includes
  253. Procedure TestEmptyProgram;
  254. Procedure TestEmptyProgramUseStrict;
  255. Procedure TestEmptyUnit;
  256. Procedure TestEmptyUnitUseStrict;
  257. Procedure TestDottedUnitNames;
  258. Procedure TestDottedUnitNameImpl;
  259. Procedure TestDottedUnitExpr;
  260. Procedure Test_ModeFPCFail;
  261. Procedure Test_ModeSwitchCBlocksFail;
  262. Procedure TestUnit_UseSystem;
  263. Procedure TestUnit_Intf1Impl2Intf1;
  264. Procedure TestIncludeVersion;
  265. // vars/const
  266. Procedure TestVarInt;
  267. Procedure TestVarBaseTypes;
  268. Procedure TestBaseTypeSingleFail;
  269. Procedure TestBaseTypeExtendedFail;
  270. Procedure TestConstBaseTypes;
  271. Procedure TestUnitImplVars;
  272. Procedure TestUnitImplConsts;
  273. Procedure TestUnitImplRecord;
  274. Procedure TestRenameJSNameConflict;
  275. Procedure TestLocalConst;
  276. Procedure TestVarExternal;
  277. Procedure TestVarExternalOtherUnit;
  278. Procedure TestVarAbsoluteFail;
  279. Procedure TestConstExternal;
  280. // numbers
  281. Procedure TestDouble;
  282. Procedure TestDoubleSmall;
  283. Procedure TestInteger;
  284. Procedure TestIntegerRange;
  285. Procedure TestIntegerTypecasts;
  286. Procedure TestInteger_BitwiseShrNativeInt;
  287. Procedure TestInteger_BitwiseShlNativeInt;
  288. Procedure TestInteger_SystemFunc;
  289. Procedure TestInteger_AssignOutsideConst;
  290. Procedure TestCurrency;
  291. Procedure TestForBoolDo;
  292. Procedure TestForIntDo;
  293. Procedure TestForIntInDo;
  294. // strings
  295. Procedure TestCharConst;
  296. Procedure TestChar_Compare;
  297. Procedure TestChar_BuiltInProcs;
  298. Procedure TestStringConst;
  299. Procedure TestStringConst_InvalidUTF16;
  300. Procedure TestStringConstSurrogate;
  301. Procedure TestStringConst_Multiline;
  302. Procedure TestString_Length;
  303. Procedure TestString_Compare;
  304. Procedure TestString_SetLength;
  305. Procedure TestString_CharAt;
  306. Procedure TestStringHMinusFail;
  307. Procedure TestStr;
  308. Procedure TestBaseType_AnsiStringFail;
  309. Procedure TestBaseType_WideStringFail;
  310. Procedure TestBaseType_ShortStringFail;
  311. Procedure TestBaseType_RawByteStringFail;
  312. Procedure TestTypeShortstring_Fail;
  313. Procedure TestCharSet_Custom;
  314. Procedure TestWideChar;
  315. Procedure TestForCharDo;
  316. Procedure TestForCharInDo;
  317. // alias types
  318. Procedure TestAliasTypeRef;
  319. Procedure TestTypeCast_BaseTypes;
  320. Procedure TestTypeCast_AliasBaseTypes;
  321. // functions
  322. Procedure TestEmptyProc;
  323. Procedure TestProcOneParam;
  324. Procedure TestFunctionWithoutParams;
  325. Procedure TestProcedureWithoutParams;
  326. Procedure TestPrgProcVar;
  327. Procedure TestProcTwoArgs;
  328. Procedure TestProc_DefaultValue;
  329. Procedure TestUnitProcVar;
  330. Procedure TestImplProc;
  331. Procedure TestFunctionResult;
  332. Procedure TestNestedProc;
  333. Procedure TestNestedProc_ResultString;
  334. Procedure TestForwardProc;
  335. Procedure TestNestedForwardProc;
  336. Procedure TestAssignFunctionResult;
  337. Procedure TestFunctionResultInCondition;
  338. Procedure TestFunctionResultInForLoop;
  339. Procedure TestFunctionResultInTypeCast;
  340. Procedure TestExit;
  341. Procedure TestExit_ResultInFinally;
  342. Procedure TestBreak;
  343. Procedure TestBreakAsVar;
  344. Procedure TestContinue;
  345. Procedure TestProc_External;
  346. Procedure TestProc_ExternalOtherUnit;
  347. Procedure TestProc_Asm;
  348. Procedure TestProc_AsmSubBlock;
  349. Procedure TestProc_Assembler;
  350. Procedure TestProc_VarParam;
  351. Procedure TestProc_VarParamString;
  352. Procedure TestProc_VarParamV;
  353. Procedure TestProc_Overload;
  354. Procedure TestProc_OverloadForward;
  355. Procedure TestProc_OverloadIntfImpl;
  356. Procedure TestProc_OverloadNested;
  357. Procedure TestProc_OverloadNestedForward;
  358. Procedure TestProc_OverloadUnitCycle;
  359. Procedure TestProc_Varargs;
  360. Procedure TestProc_ConstOrder;
  361. Procedure TestProc_DuplicateConst;
  362. Procedure TestProc_LocalVarAbsolute;
  363. Procedure TestProc_ResultAbsolute;
  364. Procedure TestProc_LocalVarInit;
  365. Procedure TestProc_ReservedWords;
  366. Procedure TestProc_ConstRefWord;
  367. // anonymous functions
  368. Procedure TestAnonymousProc_Assign_ObjFPC;
  369. Procedure TestAnonymousProc_Assign_Delphi;
  370. Procedure TestAnonymousProc_Arg;
  371. Procedure TestAnonymousProc_Typecast;
  372. Procedure TestAnonymousProc_With;
  373. Procedure TestAnonymousProc_ExceptOn;
  374. Procedure TestAnonymousProc_Nested;
  375. Procedure TestAnonymousProc_NestedAssignResult;
  376. Procedure TestAnonymousProc_Class;
  377. Procedure TestAnonymousProc_ForLoop;
  378. Procedure TestAnonymousProc_AsmDelphi;
  379. // enums, sets
  380. Procedure TestEnum_Name;
  381. Procedure TestEnum_Number;
  382. Procedure TestEnum_ConstFail;
  383. Procedure TestEnum_Functions;
  384. Procedure TestEnumRg_Functions;
  385. Procedure TestEnum_AsParams;
  386. Procedure TestEnumRange_Array;
  387. Procedure TestEnum_ForIn;
  388. Procedure TestEnum_ScopedNumber;
  389. Procedure TestEnum_InFunction;
  390. Procedure TestEnum_Name_Anonymous_Unit;
  391. Procedure TestSet_Enum;
  392. Procedure TestSet_Operators;
  393. Procedure TestSet_Operator_In;
  394. Procedure TestSet_Functions;
  395. Procedure TestSet_PassAsArgClone;
  396. Procedure TestSet_AsParams;
  397. Procedure TestSet_Property;
  398. Procedure TestSet_EnumConst;
  399. Procedure TestSet_IntConst;
  400. Procedure TestSet_IntRange;
  401. Procedure TestSet_AnonymousEnumType;
  402. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  403. Procedure TestSet_ConstEnum;
  404. Procedure TestSet_ConstChar;
  405. Procedure TestSet_ConstInt;
  406. Procedure TestSet_InFunction;
  407. Procedure TestSet_ForIn;
  408. // statements
  409. Procedure TestNestBegin;
  410. Procedure TestIncDec;
  411. Procedure TestLoHiFpcMode;
  412. Procedure TestLoHiDelphiMode;
  413. Procedure TestAssignments;
  414. Procedure TestArithmeticOperators1;
  415. Procedure TestMultiAdd;
  416. Procedure TestLogicalOperators;
  417. Procedure TestBitwiseOperators;
  418. Procedure TestBitwiseOperatorsLongword;
  419. Procedure TestFunctionInt;
  420. Procedure TestFunctionString;
  421. Procedure TestIfThen;
  422. Procedure TestForLoop;
  423. Procedure TestForLoopInsideFunction;
  424. Procedure TestForLoop_ReadVarAfter;
  425. Procedure TestForLoop_Nested;
  426. Procedure TestRepeatUntil;
  427. Procedure TestAsmBlock;
  428. Procedure TestAsmPas_Impl; // ToDo
  429. Procedure TestTryFinally;
  430. Procedure TestTryExcept;
  431. Procedure TestTryExcept_ReservedWords;
  432. Procedure TestIfThenRaiseElse;
  433. Procedure TestCaseOf;
  434. Procedure TestCaseOf_UseSwitch;
  435. Procedure TestCaseOfNoElse;
  436. Procedure TestCaseOfNoElse_UseSwitch;
  437. Procedure TestCaseOfRange;
  438. Procedure TestCaseOfString;
  439. Procedure TestCaseOfChar;
  440. Procedure TestCaseOfExternalClassConst;
  441. Procedure TestDebugger;
  442. // arrays
  443. Procedure TestArray_Dynamic;
  444. Procedure TestArray_Dynamic_Nil;
  445. Procedure TestArray_DynMultiDimensional;
  446. Procedure TestArray_DynamicAssign;
  447. Procedure TestArray_StaticInt;
  448. Procedure TestArray_StaticBool;
  449. Procedure TestArray_StaticChar;
  450. Procedure TestArray_StaticMultiDim;
  451. Procedure TestArray_StaticInFunction;
  452. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  453. Procedure TestArrayOfRecord;
  454. Procedure TestArray_StaticRecord;
  455. Procedure TestArrayOfSet;
  456. Procedure TestArray_DynAsParam;
  457. Procedure TestArray_StaticAsParam;
  458. Procedure TestArrayElement_AsParams;
  459. Procedure TestArrayElementFromFuncResult_AsParams;
  460. Procedure TestArrayEnumTypeRange;
  461. Procedure TestArray_SetLengthOutArg;
  462. Procedure TestArray_SetLengthProperty;
  463. Procedure TestArray_SetLengthMultiDim;
  464. Procedure TestArray_SetLengthDynOfStatic;
  465. Procedure TestArray_OpenArrayOfString;
  466. Procedure TestArray_ArrayOfCharAssignString;
  467. Procedure TestArray_ConstRef;
  468. Procedure TestArray_Concat;
  469. Procedure TestArray_Concat_Append;
  470. Procedure TestArray_Concat_Append_Var;
  471. Procedure TestArray_Copy;
  472. Procedure TestArray_InsertDelete;
  473. Procedure TestArray_Add_Append;
  474. Procedure TestArray_DynArrayConstObjFPC;
  475. Procedure TestArray_DynArrayConstDelphi;
  476. Procedure TestArray_ArrayLitAsParam;
  477. Procedure TestArray_ArrayLitMultiDimAsParam;
  478. Procedure TestArray_ArrayLitStaticAsParam;
  479. Procedure TestArray_ForInArrOfString;
  480. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  481. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  482. Procedure TestArrayOfConst_TVarRec;
  483. Procedure TestArrayOfConst_PassBaseTypes;
  484. Procedure TestArrayOfConst_PassObj;
  485. // record
  486. Procedure TestRecord_Empty;
  487. Procedure TestRecord_Var;
  488. Procedure TestRecord_VarExternal;
  489. Procedure TestRecord_WithDo;
  490. Procedure TestRecord_Assign;
  491. Procedure TestRecord_AsParams;
  492. Procedure TestRecord_ConstRef;
  493. Procedure TestRecordElement_AsParams;
  494. Procedure TestRecordElementFromFuncResult_AsParams;
  495. Procedure TestRecordElementFromWith_AsParams;
  496. Procedure TestRecord_Equal;
  497. Procedure TestRecord_JSValue;
  498. Procedure TestRecord_VariantFail;
  499. Procedure TestRecord_FieldArray;
  500. Procedure TestRecord_Const;
  501. Procedure TestRecord_TypecastFail;
  502. Procedure TestRecord_InFunction;
  503. // anonymous record
  504. Procedure TestRecordAnonym_Field;
  505. Procedure TestRecordAnonym_Assign;
  506. Procedure TestRecordAnonym_Nested;
  507. Procedure TestRecordAnonym_Const;
  508. Procedure TestRecordAnonym_InFunction;
  509. // advanced record
  510. Procedure TestAdvRecord_Function;
  511. Procedure TestAdvRecord_Property;
  512. Procedure TestAdvRecord_PropertyDefault;
  513. Procedure TestAdvRecord_Property_ClassMethod;
  514. Procedure TestAdvRecord_Const;
  515. Procedure TestAdvRecord_ExternalField;
  516. Procedure TestAdvRecord_SubRecord;
  517. Procedure TestAdvRecord_SubClass;
  518. Procedure TestAdvRecord_SubInterfaceFail;
  519. Procedure TestAdvRecord_Constructor;
  520. Procedure TestAdvRecord_ClassConstructor_Program;
  521. Procedure TestAdvRecord_ClassConstructor_Unit;
  522. // classes
  523. Procedure TestClass_TObjectDefaultConstructor;
  524. Procedure TestClass_TObjectConstructorWithParams;
  525. Procedure TestClass_TObjectConstructorWithDefaultParam;
  526. Procedure TestClass_Var;
  527. Procedure TestClass_Method;
  528. Procedure TestClass_Implementation;
  529. Procedure TestClass_Inheritance;
  530. Procedure TestClass_TypeAlias;
  531. Procedure TestClass_AbstractMethod;
  532. Procedure TestClass_CallInherited_ProcNoParams;
  533. Procedure TestClass_CallInherited_WithParams;
  534. Procedure TestClasS_CallInheritedConstructor;
  535. Procedure TestClass_ClassVar_Assign;
  536. Procedure TestClass_CallClassMethod;
  537. Procedure TestClass_CallClassMethodStatic;
  538. Procedure TestClass_Property;
  539. Procedure TestClass_Property_ClassMethod;
  540. Procedure TestClass_Property_ClassMethodStatic;
  541. Procedure TestClass_Property_Indexed;
  542. Procedure TestClass_Property_IndexSpec;
  543. Procedure TestClass_PropertyOfTypeArray;
  544. Procedure TestClass_PropertyDefault;
  545. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  546. //Procedure TestClass_PropertyDefault;
  547. Procedure TestClass_PropertyOverride;
  548. Procedure TestClass_PropertyIncVisibility;
  549. Procedure TestClass_Assigned;
  550. Procedure TestClass_WithClassDoCreate;
  551. Procedure TestClass_WithClassInstDoProperty;
  552. Procedure TestClass_WithClassInstDoPropertyWithParams;
  553. Procedure TestClass_WithClassInstDoFunc;
  554. Procedure TestClass_TypeCast;
  555. Procedure TestClass_TypeCastUntypedParam;
  556. Procedure TestClass_Overloads;
  557. Procedure TestClass_OverloadsAncestor;
  558. Procedure TestClass_OverloadConstructor;
  559. Procedure TestClass_OverloadDelphiOverride;
  560. Procedure TestClass_ReintroduceVarDelphi;
  561. Procedure TestClass_ReintroducedVar;
  562. Procedure TestClass_RaiseDescendant;
  563. Procedure TestClass_ExternalMethod;
  564. Procedure TestClass_ExternalVirtualNameMismatchFail;
  565. Procedure TestClass_ExternalOverrideFail;
  566. Procedure TestClass_ExternalVar;
  567. Procedure TestClass_Const;
  568. Procedure TestClass_ConstEnum;
  569. Procedure TestClass_LocalConstDuplicate_Prg;
  570. Procedure TestClass_LocalConstDuplicate_Unit;
  571. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  572. Procedure TestClass_LocalVarSelfFail;
  573. Procedure TestClass_ArgSelfFail;
  574. Procedure TestClass_NestedProcSelf;
  575. Procedure TestClass_NestedProcSelf2;
  576. Procedure TestClass_NestedProcClassSelf;
  577. Procedure TestClass_NestedProcCallInherited;
  578. Procedure TestClass_TObjectFree;
  579. Procedure TestClass_TObjectFree_VarArg;
  580. Procedure TestClass_TObjectFreeNewInstance;
  581. Procedure TestClass_TObjectFreeLowerCase;
  582. Procedure TestClass_TObjectFreeFunctionFail;
  583. Procedure TestClass_TObjectFreePropertyFail;
  584. Procedure TestClass_ForIn;
  585. Procedure TestClass_DispatchMessage;
  586. Procedure TestClass_Message_DuplicateIntFail;
  587. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  588. // class of
  589. Procedure TestClassOf_Create;
  590. Procedure TestClassOf_Call;
  591. Procedure TestClassOf_Assign;
  592. Procedure TestClassOf_Is;
  593. Procedure TestClassOf_Compare;
  594. Procedure TestClassOf_ClassVar;
  595. Procedure TestClassOf_ClassMethod;
  596. Procedure TestClassOf_ClassProperty;
  597. Procedure TestClassOf_ClassMethodSelf;
  598. Procedure TestClassOf_TypeCast;
  599. Procedure TestClassOf_ImplicitFunctionCall;
  600. Procedure TestClassOf_Const;
  601. // nested class
  602. Procedure TestNestedClass_Alias;
  603. Procedure TestNestedClass_Record;
  604. Procedure TestNestedClass_Class;
  605. Procedure TestNestedClass_CallInherited;
  606. // external class
  607. Procedure TestExternalClass_Var;
  608. Procedure TestExternalClass_Const;
  609. Procedure TestExternalClass_Dollar;
  610. Procedure TestExternalClass_DuplicateVarFail;
  611. Procedure TestExternalClass_Method;
  612. Procedure TestExternalClass_ClassMethod;
  613. Procedure TestExternalClass_ClassMethodStatic;
  614. Procedure TestExternalClass_FunctionResultInTypeCast;
  615. Procedure TestExternalClass_NonExternalOverride;
  616. Procedure TestExternalClass_OverloadHint;
  617. Procedure TestExternalClass_SameNamePublishedProperty;
  618. Procedure TestExternalClass_Property;
  619. Procedure TestExternalClass_PropertyDate;
  620. Procedure TestExternalClass_ClassProperty;
  621. Procedure TestExternalClass_ClassOf;
  622. Procedure TestExternalClass_ClassOtherUnit;
  623. Procedure TestExternalClass_Is;
  624. Procedure TestExternalClass_As;
  625. Procedure TestExternalClass_DestructorFail;
  626. Procedure TestExternalClass_New;
  627. Procedure TestExternalClass_ClassOf_New;
  628. Procedure TestExternalClass_FuncClassOf_New;
  629. Procedure TestExternalClass_New_PasClassFail;
  630. Procedure TestExternalClass_New_PasClassBracketsFail;
  631. Procedure TestExternalClass_NewExtName;
  632. Procedure TestExternalClass_Constructor;
  633. Procedure TestExternalClass_ConstructorBrackets;
  634. Procedure TestExternalClass_LocalConstSameName;
  635. Procedure TestExternalClass_ReintroduceOverload;
  636. Procedure TestExternalClass_Inherited;
  637. Procedure TestExternalClass_PascalAncestorFail;
  638. Procedure TestExternalClass_NewInstance;
  639. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  640. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  641. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  642. Procedure TestExternalClass_JSFunctionPasDescendant;
  643. Procedure TestExternalClass_PascalProperty;
  644. Procedure TestExternalClass_TypeCastToRootClass;
  645. Procedure TestExternalClass_TypeCastToJSObject;
  646. Procedure TestExternalClass_TypeCastStringToExternalString;
  647. Procedure TestExternalClass_TypeCastToJSFunction;
  648. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  649. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  650. Procedure TestExternalClass_BracketAccessor;
  651. Procedure TestExternalClass_BracketAccessor_Call;
  652. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  653. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  654. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  655. Procedure TestExternalClass_BracketAccessor_MultiType;
  656. Procedure TestExternalClass_BracketAccessor_Index;
  657. Procedure TestExternalClass_ForInJSObject;
  658. Procedure TestExternalClass_ForInJSArray;
  659. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  660. Procedure TestExternalClass_NestedConstructor;
  661. // class interfaces
  662. Procedure TestClassInterface_Corba;
  663. Procedure TestClassInterface_Corba_ProcExternalFail;
  664. Procedure TestClassInterface_Corba_Overloads;
  665. Procedure TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  666. Procedure TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  667. Procedure TestClassInterface_Corba_AncestorImpl;
  668. Procedure TestClassInterface_Corba_ImplReintroduce;
  669. Procedure TestClassInterface_Corba_MethodResolution;
  670. Procedure TestClassInterface_COM_AncestorMoreInterfaces;
  671. Procedure TestClassInterface_Corba_MethodOverride;
  672. Procedure TestClassInterface_Corba_Delegation;
  673. Procedure TestClassInterface_Corba_DelegationStatic;
  674. Procedure TestClassInterface_Corba_Operators;
  675. Procedure TestClassInterface_Corba_Args;
  676. Procedure TestClassInterface_Corba_ForIn;
  677. Procedure TestClassInterface_Corba_ArrayOfIntf;
  678. Procedure TestClassInterface_COM_AssignVar;
  679. Procedure TestClassInterface_COM_AssignArg;
  680. Procedure TestClassInterface_COM_FunctionResult;
  681. Procedure TestClassInterface_COM_InheritedFuncResult;
  682. Procedure TestClassInterface_COM_IsAsTypeCasts;
  683. Procedure TestClassInterface_COM_PassAsArg;
  684. Procedure TestClassInterface_COM_PassToUntypedParam;
  685. Procedure TestClassInterface_COM_FunctionInExpr;
  686. Procedure TestClassInterface_COM_Property;
  687. Procedure TestClassInterface_COM_IntfProperty;
  688. Procedure TestClassInterface_COM_Delegation;
  689. Procedure TestClassInterface_COM_With;
  690. Procedure TestClassInterface_COM_ForIn;
  691. Procedure TestClassInterface_COM_ArrayOfIntf;
  692. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  693. Procedure TestClassInterface_COM_RecordIntfFail;
  694. Procedure TestClassInterface_COM_UnitInitialization;
  695. Procedure TestClassInterface_Corba_GUID;
  696. Procedure TestClassInterface_Corba_GUIDProperty;
  697. // helpers
  698. Procedure TestClassHelper_ClassVar;
  699. Procedure TestClassHelper_Method_AccessInstanceFields;
  700. Procedure TestClassHelper_Method_Call;
  701. Procedure TestClassHelper_Method_Nested_Call;
  702. Procedure TestClassHelper_ClassMethod_Call;
  703. Procedure TestClassHelper_ClassOf;
  704. Procedure TestClassHelper_MethodRefObjFPC;
  705. Procedure TestClassHelper_Constructor;
  706. Procedure TestClassHelper_InheritedObjFPC;
  707. Procedure TestClassHelper_Property;
  708. Procedure TestClassHelper_Property_Array;
  709. Procedure TestClassHelper_Property_Array_Default;
  710. Procedure TestClassHelper_Property_Array_DefaultDefault;
  711. Procedure TestClassHelper_ClassProperty;
  712. Procedure TestClassHelper_ClassPropertyStatic;
  713. Procedure TestClassHelper_ClassProperty_Array;
  714. Procedure TestClassHelper_ForIn;
  715. Procedure TestClassHelper_PassProperty;
  716. Procedure TestExtClassHelper_ClassVar;
  717. Procedure TestExtClassHelper_Method_Call;
  718. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  719. Procedure TestRecordHelper_ClassVar;
  720. Procedure TestRecordHelper_Method_Call;
  721. Procedure TestRecordHelper_Constructor;
  722. Procedure TestTypeHelper_ClassVar;
  723. Procedure TestTypeHelper_PassResultElement;
  724. Procedure TestTypeHelper_PassArgs;
  725. Procedure TestTypeHelper_PassVarConst;
  726. Procedure TestTypeHelper_PassFuncResult;
  727. Procedure TestTypeHelper_PassPropertyField;
  728. Procedure TestTypeHelper_PassPropertyGetter;
  729. Procedure TestTypeHelper_PassClassPropertyField;
  730. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  731. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  732. Procedure TestTypeHelper_Property;
  733. Procedure TestTypeHelper_Property_Array;
  734. Procedure TestTypeHelper_ClassProperty;
  735. Procedure TestTypeHelper_ClassProperty_Array;
  736. Procedure TestTypeHelper_ClassMethod;
  737. Procedure TestTypeHelper_ExtClassMethodFail;
  738. Procedure TestTypeHelper_Constructor;
  739. Procedure TestTypeHelper_Word;
  740. Procedure TestTypeHelper_Boolean;
  741. Procedure TestTypeHelper_WordBool;
  742. Procedure TestTypeHelper_Double;
  743. Procedure TestTypeHelper_NativeInt;
  744. Procedure TestTypeHelper_StringChar;
  745. Procedure TestTypeHelper_JSValue;
  746. Procedure TestTypeHelper_Array;
  747. Procedure TestTypeHelper_EnumType;
  748. Procedure TestTypeHelper_SetType;
  749. Procedure TestTypeHelper_InterfaceType;
  750. Procedure TestTypeHelper_NestedSelf;
  751. // proc types
  752. Procedure TestProcType;
  753. Procedure TestProcType_Arg;
  754. Procedure TestProcType_FunctionFPC;
  755. Procedure TestProcType_FunctionDelphi;
  756. Procedure TestProcType_ProcedureDelphi;
  757. Procedure TestProcType_AsParam;
  758. Procedure TestProcType_MethodFPC;
  759. Procedure TestProcType_MethodDelphi;
  760. Procedure TestProcType_PropertyFPC;
  761. Procedure TestProcType_PropertyDelphi;
  762. Procedure TestProcType_WithClassInstDoPropertyFPC;
  763. Procedure TestProcType_Nested;
  764. Procedure TestProcType_NestedOfObject;
  765. Procedure TestProcType_ReferenceToProc;
  766. Procedure TestProcType_ReferenceToMethod;
  767. Procedure TestProcType_Typecast;
  768. Procedure TestProcType_PassProcToUntyped;
  769. Procedure TestProcType_PassProcToArray;
  770. Procedure TestProcType_SafeCallObjFPC;
  771. Procedure TestProcType_SafeCallDelphi;
  772. Procedure TestProcType_SafeCall_Arg;
  773. // pointer
  774. Procedure TestPointer;
  775. Procedure TestPointer_Proc;
  776. Procedure TestPointer_AssignRecordFail;
  777. Procedure TestPointer_AssignStaticArrayFail;
  778. Procedure TestPointer_TypeCastJSValueToPointer;
  779. Procedure TestPointer_NonRecordFail;
  780. Procedure TestPointer_AnonymousArgTypeFail;
  781. Procedure TestPointer_AnonymousVarTypeFail;
  782. Procedure TestPointer_AnonymousResultTypeFail;
  783. Procedure TestPointer_AddrOperatorFail;
  784. Procedure TestPointer_ArrayParamsFail;
  785. Procedure TestPointer_PointerAddFail;
  786. Procedure TestPointer_IncPointerFail;
  787. Procedure TestPointer_Record;
  788. Procedure TestPointer_RecordArg;
  789. // jsvalue
  790. Procedure TestJSValue_AssignToJSValue;
  791. Procedure TestJSValue_TypeCastToBaseType;
  792. Procedure TestJSValue_TypecastToJSValue;
  793. Procedure TestJSValue_Equal;
  794. Procedure TestJSValue_If;
  795. Procedure TestJSValue_Not;
  796. Procedure TestJSValue_Enum;
  797. Procedure TestJSValue_ClassInstance;
  798. Procedure TestJSValue_ClassOf;
  799. Procedure TestJSValue_ArrayOfJSValue;
  800. Procedure TestJSValue_ArrayLit;
  801. Procedure TestJSValue_Params;
  802. Procedure TestJSValue_UntypedParam;
  803. Procedure TestJSValue_FuncResultType;
  804. Procedure TestJSValue_ProcType_Assign;
  805. Procedure TestJSValue_ProcType_Equal;
  806. Procedure TestJSValue_ProcType_Param;
  807. Procedure TestJSValue_AssignToPointerFail;
  808. Procedure TestJSValue_OverloadDouble;
  809. Procedure TestJSValue_OverloadNativeInt;
  810. Procedure TestJSValue_OverloadWord;
  811. Procedure TestJSValue_OverloadString;
  812. Procedure TestJSValue_OverloadChar;
  813. Procedure TestJSValue_OverloadPointer;
  814. Procedure TestJSValue_ForIn;
  815. // RTTI
  816. Procedure TestRTTI_IntRange;
  817. Procedure TestRTTI_Double;
  818. Procedure TestRTTI_ProcType;
  819. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  820. Procedure TestRTTI_ProcTypeAnonymous;
  821. Procedure TestRTTI_EnumAndSetType;
  822. Procedure TestRTTI_EnumRange;
  823. Procedure TestRTTI_AnonymousEnumType;
  824. Procedure TestRTTI_StaticArray;
  825. Procedure TestRTTI_DynArray;
  826. Procedure TestRTTI_ArrayNestedAnonymous;
  827. Procedure TestRTTI_PublishedMethodOverloadFail;
  828. Procedure TestRTTI_PublishedMethodHideNoHint;
  829. Procedure TestRTTI_PublishedMethodExternalFail;
  830. Procedure TestRTTI_PublishedClassPropertyFail;
  831. Procedure TestRTTI_PublishedClassFieldFail;
  832. Procedure TestRTTI_PublishedFieldExternalFail;
  833. Procedure TestRTTI_Class_Field;
  834. Procedure TestRTTI_Class_Method;
  835. Procedure TestRTTI_Class_MethodArgFlags;
  836. Procedure TestRTTI_Class_Property;
  837. Procedure TestRTTI_Class_PropertyParams;
  838. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  839. Procedure TestRTTI_Class_OmitRTTI;
  840. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  841. Procedure TestRTTI_IndexModifier;
  842. Procedure TestRTTI_StoredModifier;
  843. Procedure TestRTTI_DefaultValue;
  844. Procedure TestRTTI_DefaultValueSet;
  845. Procedure TestRTTI_DefaultValueRangeType;
  846. Procedure TestRTTI_DefaultValueInherit;
  847. Procedure TestRTTI_OverrideMethod;
  848. Procedure TestRTTI_ReintroduceMethod;
  849. Procedure TestRTTI_OverloadProperty;
  850. // ToDo: array argument
  851. Procedure TestRTTI_ClassForward;
  852. Procedure TestRTTI_ClassOf;
  853. Procedure TestRTTI_Record;
  854. Procedure TestRTTI_RecordAnonymousArray;
  855. Procedure TestRTTI_Record_ClassVarType;
  856. Procedure TestRTTI_LocalTypes;
  857. Procedure TestRTTI_TypeInfo_BaseTypes;
  858. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  859. Procedure TestRTTI_TypeInfo_LocalFail;
  860. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  861. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  862. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  863. Procedure TestRTTI_TypeInfo_FunctionClassType;
  864. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  865. Procedure TestRTTI_Interface_Corba;
  866. Procedure TestRTTI_Interface_COM;
  867. Procedure TestRTTI_ClassHelper;
  868. Procedure TestRTTI_ExternalClass;
  869. Procedure TestRTTI_Unit;
  870. // Resourcestring
  871. Procedure TestResourcestringProgram;
  872. Procedure TestResourcestringUnit;
  873. Procedure TestResourcestringImplementation;
  874. // Attributes
  875. Procedure TestAttributes_Members;
  876. Procedure TestAttributes_Types;
  877. Procedure TestAttributes_HelperConstructor_Fail;
  878. Procedure TestAttributes_InterfacesList;
  879. // Assertions, checks
  880. procedure TestAssert;
  881. procedure TestAssert_SysUtils;
  882. procedure TestObjectChecks;
  883. procedure TestOverflowChecks_Int;
  884. procedure TestRangeChecks_AssignInt;
  885. procedure TestRangeChecks_AssignIntRange;
  886. procedure TestRangeChecks_AssignEnum;
  887. procedure TestRangeChecks_AssignEnumRange;
  888. procedure TestRangeChecks_AssignChar;
  889. procedure TestRangeChecks_AssignCharRange;
  890. procedure TestRangeChecks_ArrayIndex;
  891. procedure TestRangeChecks_ArrayOfRecIndex;
  892. procedure TestRangeChecks_StringIndex;
  893. procedure TestRangeChecks_TypecastInt;
  894. procedure TestRangeChecks_TypeHelperInt;
  895. // Async/AWait
  896. Procedure TestAsync_Proc;
  897. Procedure TestAsync_CallResultIsPromise;
  898. Procedure TestAsync_ConstructorFail;
  899. Procedure TestAsync_PropertyGetterFail;
  900. Procedure TestAwait_NonPromiseWithTypeFail;
  901. Procedure TestAwait_AsyncCallTypeMismatch;
  902. Procedure TestAWait_OutsideAsyncFail;
  903. Procedure TestAWait_IntegerFail;
  904. Procedure TestAWait_ExternalClassPromise;
  905. Procedure TestAWait_JSValue;
  906. Procedure TestAWait_Result;
  907. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  908. Procedure TestAsync_AnonymousProc;
  909. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  910. Procedure TestAsync_ProcType;
  911. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  912. Procedure TestAsync_Inherited;
  913. Procedure TestAsync_ClassInterface;
  914. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  915. Procedure TestAWait_ClassAs;
  916. // Library
  917. Procedure TestLibrary_Empty;
  918. Procedure TestLibrary_ExportFunc;
  919. Procedure TestLibrary_ExportFuncOverloadedFail;
  920. Procedure TestLibrary_Export_Index_Fail;
  921. Procedure TestLibrary_ExportVar;
  922. Procedure TestLibrary_ExportUnitFunc;
  923. end;
  924. function LinesToStr(Args: array of const): string;
  925. function ExtractFileUnitName(aFilename: string): string;
  926. function JSToStr(El: TJSElement): string;
  927. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  928. implementation
  929. function LinesToStr(Args: array of const): string;
  930. var
  931. s: String;
  932. i: Integer;
  933. begin
  934. s:='';
  935. for i:=Low(Args) to High(Args) do
  936. case Args[i].VType of
  937. vtChar: s += Args[i].VChar+LineEnding;
  938. vtString: s += Args[i].VString^+LineEnding;
  939. vtPChar: s += Args[i].VPChar+LineEnding;
  940. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  941. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  942. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  943. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  944. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  945. end;
  946. Result:=s;
  947. end;
  948. function ExtractFileUnitName(aFilename: string): string;
  949. var
  950. p: Integer;
  951. begin
  952. Result:=ExtractFileName(aFilename);
  953. if Result='' then exit;
  954. for p:=length(Result) downto 1 do
  955. case Result[p] of
  956. '/','\': exit;
  957. '.':
  958. begin
  959. Delete(Result,p,length(Result));
  960. exit;
  961. end;
  962. end;
  963. end;
  964. function JSToStr(El: TJSElement): string;
  965. var
  966. aWriter: TBufferWriter;
  967. aJSWriter: TJSWriter;
  968. begin
  969. aJSWriter:=nil;
  970. aWriter:=TBufferWriter.Create(1000);
  971. try
  972. aJSWriter:=TJSWriter.Create(aWriter);
  973. aJSWriter.IndentSize:=2;
  974. aJSWriter.WriteJS(El);
  975. Result:=aWriter.AsString;
  976. finally
  977. aJSWriter.Free;
  978. aWriter.Free;
  979. end;
  980. end;
  981. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  982. // search diff, ignore changes in spaces
  983. const
  984. SpaceChars = [#9,#10,#13,' '];
  985. var
  986. ExpectedP, ActualP: PAnsiChar;
  987. function FindLineEnd(p: PAnsiChar): PAnsiChar;
  988. begin
  989. Result:=p;
  990. while not (Result^ in [#0,#10,#13]) do inc(Result);
  991. end;
  992. function FindLineStart(p, MinP: PAnsiChar): PAnsiChar;
  993. begin
  994. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  995. Result:=p;
  996. end;
  997. procedure SkipLineEnd(var p: PAnsiChar);
  998. begin
  999. if p^ in [#10,#13] then
  1000. begin
  1001. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1002. inc(p,2)
  1003. else
  1004. inc(p);
  1005. end;
  1006. end;
  1007. function HasSpecialChar(s: string): boolean;
  1008. var
  1009. i: Integer;
  1010. begin
  1011. for i:=1 to length(s) do
  1012. if s[i] in [#0..#31,#127..#255] then
  1013. exit(true);
  1014. Result:=false;
  1015. end;
  1016. function HashSpecialChars(s: string): string;
  1017. var
  1018. i: Integer;
  1019. begin
  1020. Result:='';
  1021. for i:=1 to length(s) do
  1022. if s[i] in [#0..#31,#127..#255] then
  1023. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1024. else
  1025. Result:=Result+s[i];
  1026. end;
  1027. procedure DiffFound;
  1028. var
  1029. ActLineStartP, ActLineEndP, p, StartPos: PAnsiChar;
  1030. ExpLine, ActLine: String;
  1031. i, LineNo, DiffLineNo: Integer;
  1032. begin
  1033. writeln('Diff found "',Msg,'". Lines:');
  1034. // write correct lines
  1035. p:=PAnsiChar(Expected);
  1036. LineNo:=0;
  1037. DiffLineNo:=0;
  1038. repeat
  1039. StartPos:=p;
  1040. while not (p^ in [#0,#10,#13]) do inc(p);
  1041. ExpLine:=copy(Expected,StartPos-PAnsiChar(Expected)+1,p-StartPos);
  1042. SkipLineEnd(p);
  1043. inc(LineNo);
  1044. if (p<=ExpectedP) and (p^<>#0) then
  1045. begin
  1046. writeln('= ',ExpLine);
  1047. end else begin
  1048. // diff line
  1049. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1050. // write actual line
  1051. ActLineStartP:=FindLineStart(ActualP,PAnsiChar(Actual));
  1052. ActLineEndP:=FindLineEnd(ActualP);
  1053. ActLine:=copy(Actual,ActLineStartP-PAnsiChar(Actual)+1,ActLineEndP-ActLineStartP);
  1054. writeln('- ',ActLine);
  1055. if HasSpecialChar(ActLine) then
  1056. writeln('- ',HashSpecialChars(ActLine));
  1057. // write expected line
  1058. writeln('+ ',ExpLine);
  1059. if HasSpecialChar(ExpLine) then
  1060. writeln('- ',HashSpecialChars(ExpLine));
  1061. // write empty line with pointer ^
  1062. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1063. writeln('^');
  1064. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1065. CheckSrcDiff:=false;
  1066. // write up to three following actual lines to get some context
  1067. for i:=1 to 3 do begin
  1068. ActLineStartP:=ActLineEndP;
  1069. SkipLineEnd(ActLineStartP);
  1070. if ActLineStartP^=#0 then break;
  1071. ActLineEndP:=FindLineEnd(ActLineStartP);
  1072. ActLine:=copy(Actual,ActLineStartP-PAnsiChar(Actual)+1,ActLineEndP-ActLineStartP);
  1073. writeln('~ ',ActLine);
  1074. end;
  1075. exit;
  1076. end;
  1077. until p^=#0;
  1078. writeln('DiffFound Actual:-----------------------');
  1079. writeln(Actual);
  1080. writeln('DiffFound Expected:---------------------');
  1081. writeln(Expected);
  1082. writeln('DiffFound ------------------------------');
  1083. Msg:='diff found, but lines are the same, internal error';
  1084. CheckSrcDiff:=false;
  1085. end;
  1086. var
  1087. IsSpaceNeeded: Boolean;
  1088. LastChar, Quote: AnsiChar;
  1089. begin
  1090. Result:=true;
  1091. Msg:='';
  1092. if Expected='' then Expected:=' ';
  1093. if Actual='' then Actual:=' ';
  1094. ExpectedP:=PAnsiChar(Expected);
  1095. ActualP:=PAnsiChar(Actual);
  1096. repeat
  1097. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1098. case ExpectedP^ of
  1099. #0:
  1100. begin
  1101. // check that rest of Actual has only spaces
  1102. while ActualP^ in SpaceChars do inc(ActualP);
  1103. if ActualP^<>#0 then
  1104. begin
  1105. DiffFound;
  1106. exit;
  1107. end;
  1108. exit(true);
  1109. end;
  1110. ' ',#9,#10,#13:
  1111. begin
  1112. // skip space in Expected
  1113. IsSpaceNeeded:=false;
  1114. if ExpectedP>PAnsiChar(Expected) then
  1115. LastChar:=ExpectedP[-1]
  1116. else
  1117. LastChar:=#0;
  1118. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1119. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1120. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1121. IsSpaceNeeded:=true;
  1122. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1123. begin
  1124. DiffFound;
  1125. exit;
  1126. end;
  1127. while ActualP^ in SpaceChars do inc(ActualP);
  1128. end;
  1129. '''','"':
  1130. begin
  1131. while ActualP^ in SpaceChars do inc(ActualP);
  1132. if ExpectedP^<>ActualP^ then
  1133. begin
  1134. DiffFound;
  1135. exit;
  1136. end;
  1137. Quote:=ExpectedP^;
  1138. repeat
  1139. inc(ExpectedP);
  1140. inc(ActualP);
  1141. if ExpectedP^<>ActualP^ then
  1142. begin
  1143. DiffFound;
  1144. exit;
  1145. end;
  1146. if (ExpectedP^ in [#0,#10,#13]) then
  1147. break
  1148. else if (ExpectedP^=Quote) then
  1149. begin
  1150. inc(ExpectedP);
  1151. inc(ActualP);
  1152. break;
  1153. end;
  1154. until false;
  1155. end;
  1156. else
  1157. while ActualP^ in SpaceChars do inc(ActualP);
  1158. if ExpectedP^<>ActualP^ then
  1159. begin
  1160. DiffFound;
  1161. exit;
  1162. end;
  1163. inc(ExpectedP);
  1164. inc(ActualP);
  1165. end;
  1166. until false;
  1167. end;
  1168. { TTestEnginePasResolver }
  1169. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1170. begin
  1171. if FModule=AValue then Exit;
  1172. FModule:=AValue;
  1173. end;
  1174. destructor TTestEnginePasResolver.Destroy;
  1175. begin
  1176. FreeAndNil(FStreamResolver);
  1177. FreeAndNil(FParser);
  1178. FreeAndNil(FScanner);
  1179. FreeAndNil(FStreamResolver);
  1180. Module:=nil;
  1181. inherited Destroy;
  1182. end;
  1183. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1184. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1185. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1186. begin
  1187. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1188. TypeParams);
  1189. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1190. Module:=TPasModule(Result);
  1191. end;
  1192. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1193. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1194. begin
  1195. Result:=nil;
  1196. if InFilename<>'' then
  1197. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1198. if Assigned(OnFindUnit) then
  1199. Result:=OnFindUnit(AName);
  1200. if NameExpr=nil then ;
  1201. end;
  1202. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1203. begin
  1204. // do not parse recursively
  1205. // parse via the queue
  1206. if Section=nil then ;
  1207. end;
  1208. { TCustomTestModule }
  1209. procedure TCustomTestModule.FreeSrcMarkers;
  1210. var
  1211. aMarker, Last: PSrcMarker;
  1212. begin
  1213. aMarker:=FirstSrcMarker;
  1214. while aMarker<>nil do
  1215. begin
  1216. Last:=aMarker;
  1217. aMarker:=aMarker^.Next;
  1218. Dispose(Last);
  1219. end;
  1220. FirstSrcMarker:=nil;
  1221. LastSrcMarker:=nil;
  1222. end;
  1223. function TCustomTestModule.GetResolverCount: integer;
  1224. begin
  1225. Result:=FResolvers.Count;
  1226. end;
  1227. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1228. begin
  1229. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1230. end;
  1231. function TCustomTestModule.GetMsgCount: integer;
  1232. begin
  1233. Result:=FHintMsgs.Count;
  1234. end;
  1235. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1236. begin
  1237. Result:=TTestHintMessage(FHintMsgs[Index]);
  1238. end;
  1239. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1240. ): TPasModule;
  1241. var
  1242. DefNamespace: String;
  1243. begin
  1244. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1245. if (Pos('.',aUnitName)<1) then
  1246. begin
  1247. DefNamespace:=GetDefaultNamespace;
  1248. if DefNamespace<>'' then
  1249. begin
  1250. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1251. if Result<>nil then exit;
  1252. end;
  1253. end;
  1254. Result:=LoadUnit(aUnitName);
  1255. if Result<>nil then exit;
  1256. {$IFDEF VerbosePas2JS}
  1257. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1258. {$ENDIF}
  1259. Fail('can''t find unit "'+aUnitName+'"');
  1260. end;
  1261. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1262. var
  1263. aParser: TPasParser;
  1264. Item: TTestHintMessage;
  1265. begin
  1266. aParser:=Sender as TPasParser;
  1267. Item:=TTestHintMessage.Create;
  1268. Item.Id:=aParser.LastMsgNumber;
  1269. Item.MsgType:=aParser.LastMsgType;
  1270. Item.MsgNumber:=aParser.LastMsgNumber;
  1271. Item.Msg:=Msg;
  1272. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1273. {$IFDEF VerbosePas2JS}
  1274. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1275. {$ENDIF}
  1276. FHintMsgs.Add(Item);
  1277. end;
  1278. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1279. );
  1280. var
  1281. aResolver: TTestEnginePasResolver;
  1282. Item: TTestHintMessage;
  1283. begin
  1284. aResolver:=Sender as TTestEnginePasResolver;
  1285. Item:=TTestHintMessage.Create;
  1286. Item.Id:=aResolver.LastMsgId;
  1287. Item.MsgType:=aResolver.LastMsgType;
  1288. Item.MsgNumber:=aResolver.LastMsgNumber;
  1289. Item.Msg:=Msg;
  1290. Item.SourcePos:=aResolver.LastSourcePos;
  1291. {$IFDEF VerbosePas2JS}
  1292. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1293. {$ENDIF}
  1294. FHintMsgs.Add(Item);
  1295. end;
  1296. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1297. var
  1298. Item: TTestHintMessage;
  1299. aScanner: TPas2jsPasScanner;
  1300. begin
  1301. aScanner:=Sender as TPas2jsPasScanner;
  1302. Item:=TTestHintMessage.Create;
  1303. Item.Id:=aScanner.LastMsgNumber;
  1304. Item.MsgType:=aScanner.LastMsgType;
  1305. Item.MsgNumber:=aScanner.LastMsgNumber;
  1306. Item.Msg:=Msg;
  1307. Item.SourcePos:=aScanner.CurSourcePos;
  1308. {$IFDEF VerbosePas2JS}
  1309. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1310. {$ENDIF}
  1311. FHintMsgs.Add(Item);
  1312. end;
  1313. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1314. var
  1315. SubEl: TPasElement;
  1316. i: Integer;
  1317. procedure E(Msg: string);
  1318. var
  1319. s: String;
  1320. begin
  1321. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1322. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1323. writeln('ERROR: ',s);
  1324. Fail(s);
  1325. end;
  1326. begin
  1327. if arg=nil then ;
  1328. if El=nil then exit;
  1329. if El.Parent=El then
  1330. E('El.Parent=El='+GetObjName(El));
  1331. if El is TBinaryExpr then
  1332. begin
  1333. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1334. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1335. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1336. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1337. end
  1338. else if El is TParamsExpr then
  1339. begin
  1340. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1341. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1342. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1343. if TParamsExpr(El).Params[i].Parent<>El then
  1344. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1345. end
  1346. else if El is TProcedureExpr then
  1347. begin
  1348. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1349. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1350. end
  1351. else if El is TPasDeclarations then
  1352. begin
  1353. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1354. begin
  1355. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1356. if SubEl.Parent<>El then
  1357. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1358. end;
  1359. end
  1360. else if El is TPasImplBlock then
  1361. begin
  1362. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1363. begin
  1364. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1365. if SubEl.Parent<>El then
  1366. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1367. end;
  1368. end
  1369. else if El is TPasImplWithDo then
  1370. begin
  1371. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1372. begin
  1373. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1374. if SubEl.Parent<>El then
  1375. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1376. end;
  1377. end
  1378. else if El is TPasProcedure then
  1379. begin
  1380. if TPasProcedure(El).ProcType.Parent<>El then
  1381. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1382. end
  1383. else if El is TPasProcedureType then
  1384. begin
  1385. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1386. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1387. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1388. end;
  1389. end;
  1390. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1391. var
  1392. Data: PTestResolverReferenceData absolute FindData;
  1393. Line, Col: integer;
  1394. begin
  1395. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1396. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1397. if (Data^.Filename=El.SourceFilename)
  1398. and (Data^.Row=Line)
  1399. and (Data^.StartCol<=Col)
  1400. and (Data^.EndCol>=Col)
  1401. then
  1402. Data^.Found.Add(El);
  1403. end;
  1404. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1405. begin
  1406. if FWithTypeInfo=AValue then Exit;
  1407. FWithTypeInfo:=AValue;
  1408. if AValue then
  1409. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1410. else
  1411. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1412. end;
  1413. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1414. var
  1415. i: Integer;
  1416. CurEngine: TTestEnginePasResolver;
  1417. CurUnitName: String;
  1418. begin
  1419. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1420. Result:=nil;
  1421. if (Module.ClassType=TPasModule)
  1422. and (CompareText(Module.Name,aUnitName)=0) then
  1423. exit(Module);
  1424. for i:=0 to ResolverCount-1 do
  1425. begin
  1426. CurEngine:=Resolvers[i];
  1427. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1428. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1429. if CompareText(aUnitName,CurUnitName)=0 then
  1430. begin
  1431. Result:=CurEngine.Module;
  1432. if Result<>nil then exit;
  1433. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1434. FileResolver.FindSourceFile(aUnitName);
  1435. CurEngine.StreamResolver:=TStreamResolver.Create;
  1436. CurEngine.StreamResolver.OwnsStreams:=True;
  1437. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1438. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1439. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1440. InitScanner(CurEngine.Scanner);
  1441. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1442. CurEngine.Parser.Options:=po_tcmodules;
  1443. if CompareText(CurUnitName,'System')=0 then
  1444. CurEngine.Parser.ImplicitUses.Clear;
  1445. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1446. try
  1447. CurEngine.Parser.NextToken;
  1448. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1449. except
  1450. on E: Exception do
  1451. HandleException(E);
  1452. end;
  1453. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1454. Result:=CurEngine.Module;
  1455. exit;
  1456. end;
  1457. end;
  1458. end;
  1459. procedure TCustomTestModule.SetUp;
  1460. begin
  1461. {$IFDEF EnablePasTreeGlobalRefCount}
  1462. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1463. {$ENDIF}
  1464. if FResolvers<>nil then
  1465. begin
  1466. writeln('TCustomTestModule.SetUp FModules<>nil');
  1467. Halt;
  1468. end;
  1469. inherited SetUp;
  1470. FSkipTests:=false;
  1471. FWithTypeInfo:=false;
  1472. FSource:=TStringList.Create;
  1473. FHub:=TPas2JSResolverHub.Create(Self);
  1474. FResolvers:=TObjectList.Create(true);
  1475. FFilename:='test1.pp';
  1476. FFileResolver:=TStreamResolver.Create;
  1477. FFileResolver.OwnsStreams:=True;
  1478. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1479. InitScanner(FScanner);
  1480. FEngine:=AddModule(Filename);
  1481. FEngine.Scanner:=FScanner;
  1482. FScanner.Resolver:=FEngine;
  1483. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1484. FParser.OnLog:=@OnParserLog;
  1485. FEngine.Parser:=FParser;
  1486. Parser.Options:=po_tcmodules;
  1487. FModule:=Nil;
  1488. FConverter:=CreateConverter;
  1489. FExpectedErrorClass:=nil;
  1490. end;
  1491. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1492. var
  1493. Options: TPasToJsConverterOptions;
  1494. begin
  1495. Result:=TPasToJSConverter.Create;
  1496. Options:=co_tcmodules;
  1497. if WithTypeInfo then
  1498. Exclude(Options,coNoTypeInfo)
  1499. else
  1500. Include(Options,coNoTypeInfo);
  1501. Result.Options:=Options;
  1502. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1503. end;
  1504. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1505. begin
  1506. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1507. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1508. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1509. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1510. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1511. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1512. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1513. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1514. aScanner.OnLog:=@OnScannerLog;
  1515. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1516. end;
  1517. procedure TCustomTestModule.TearDown;
  1518. {$IFDEF CheckPasTreeRefCount}
  1519. var
  1520. El: TPasElement;
  1521. {$ENDIF}
  1522. var
  1523. i: Integer;
  1524. CurModule: TPasModule;
  1525. begin
  1526. FreeSrcMarkers;
  1527. FHintMsgs.Clear;
  1528. FHintMsgsGood.Clear;
  1529. FSkipTests:=false;
  1530. FWithTypeInfo:=false;
  1531. FJSRegModuleCall:=nil;
  1532. FJSModuleCallArgs:=nil;
  1533. FJSImplentationUses:=nil;
  1534. FJSInterfaceUses:=nil;
  1535. FJSModuleSrc:=nil;
  1536. FJSInitBody:=nil;
  1537. FreeAndNil(FJSSource);
  1538. FreeAndNil(FJSModule);
  1539. FreeAndNil(FConverter);
  1540. ResolverEngine.Clear;
  1541. FreeAndNil(FSource);
  1542. FreeAndNil(FFileResolver);
  1543. if FResolvers<>nil then
  1544. begin
  1545. for i:=0 to FResolvers.Count-1 do
  1546. begin
  1547. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1548. if CurModule=nil then continue;
  1549. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1550. end;
  1551. for i:=0 to FResolvers.Count-1 do
  1552. begin
  1553. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1554. if CurModule=nil then continue;
  1555. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1556. end;
  1557. FreeAndNil(FResolvers);
  1558. FModule:=nil;
  1559. FEngine:=nil;
  1560. end;
  1561. FreeAndNil(FHub);
  1562. inherited TearDown;
  1563. {$IFDEF EnablePasTreeGlobalRefCount}
  1564. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1565. begin
  1566. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1567. {$IFDEF CheckPasTreeRefCount}
  1568. El:=TPasElement.FirstRefEl;
  1569. while El<>nil do
  1570. begin
  1571. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1572. for i:=0 to El.RefIds.Count-1 do
  1573. writeln(' ',El.RefIds[i]);
  1574. El:=El.NextRefEl;
  1575. end;
  1576. {$ENDIF}
  1577. Halt;
  1578. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1579. end;
  1580. {$ENDIF}
  1581. end;
  1582. procedure TCustomTestModule.Add(Line: string);
  1583. begin
  1584. Source.Add(Line);
  1585. end;
  1586. procedure TCustomTestModule.Add(const Lines: array of string);
  1587. var
  1588. i: Integer;
  1589. begin
  1590. for i:=low(Lines) to high(Lines) do
  1591. Add(Lines[i]);
  1592. end;
  1593. procedure TCustomTestModule.StartParsing;
  1594. var
  1595. Src: String;
  1596. begin
  1597. Src:=Source.Text;
  1598. FEngine.Source:=Src;
  1599. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1600. Scanner.OpenFile(FileName);
  1601. Writeln('// Test : ',Self.TestName);
  1602. Writeln(Src);
  1603. end;
  1604. procedure TCustomTestModule.ParseModuleQueue;
  1605. var
  1606. i: Integer;
  1607. CurResolver: TTestEnginePasResolver;
  1608. Found: Boolean;
  1609. Section: TPasSection;
  1610. begin
  1611. // parse til exception or all Resolvers finished
  1612. while not SkipTests do
  1613. begin
  1614. Found:=false;
  1615. for i:=0 to ResolverCount-1 do
  1616. begin
  1617. CurResolver:=Resolvers[i];
  1618. if CurResolver.CurrentParser=nil then continue;
  1619. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1620. continue;
  1621. CurResolver.Parser.ParseContinue;
  1622. Found:=true;
  1623. break;
  1624. end;
  1625. if not Found then break;
  1626. end;
  1627. for i:=0 to ResolverCount-1 do
  1628. begin
  1629. CurResolver:=Resolvers[i];
  1630. if CurResolver.Parser=nil then
  1631. begin
  1632. if CurResolver.CurrentParser<>nil then
  1633. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1634. continue;
  1635. end;
  1636. if CurResolver.Parser.CurModule<>nil then
  1637. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1638. end;
  1639. end;
  1640. procedure TCustomTestModule.ParseModule;
  1641. begin
  1642. if SkipTests then exit;
  1643. FFirstPasStatement:=nil;
  1644. try
  1645. StartParsing;
  1646. Parser.ParseMain(FModule);
  1647. ParseModuleQueue;
  1648. except
  1649. on E: Exception do
  1650. HandleException(E);
  1651. end;
  1652. if SkipTests then exit;
  1653. AssertNotNull('Module resulted in Module',Module);
  1654. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1655. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1656. end;
  1657. procedure TCustomTestModule.ParseProgram;
  1658. begin
  1659. if SkipTests then exit;
  1660. ParseModule;
  1661. if SkipTests then exit;
  1662. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1663. FPasProgram:=TPasProgram(Module);
  1664. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1665. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1666. if (PasProgram.InitializationSection.Elements.Count>0) then
  1667. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1668. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1669. end;
  1670. procedure TCustomTestModule.ParseLibrary;
  1671. var
  1672. Init: TInitializationSection;
  1673. begin
  1674. if SkipTests then exit;
  1675. ParseModule;
  1676. if SkipTests then exit;
  1677. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1678. FPasLibrary:=TPasLibrary(Module);
  1679. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1680. Init:=PasLibrary.InitializationSection;
  1681. if (Init<>nil) and (Init.Elements.Count>0) then
  1682. if TObject(Init.Elements[0]) is TPasImplBlock then
  1683. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1684. end;
  1685. procedure TCustomTestModule.ParseUnit;
  1686. begin
  1687. if SkipTests then exit;
  1688. ParseModule;
  1689. if SkipTests then exit;
  1690. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1691. AssertNotNull('Has interface section',Module.InterfaceSection);
  1692. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1693. if (Module.InitializationSection<>nil)
  1694. and (Module.InitializationSection.Elements.Count>0)
  1695. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1696. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1697. end;
  1698. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1699. ): TTestEnginePasResolver;
  1700. var
  1701. i: Integer;
  1702. begin
  1703. for i:=0 to ResolverCount-1 do
  1704. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1705. exit(Resolvers[i]);
  1706. Result:=nil;
  1707. end;
  1708. function TCustomTestModule.AddModule(aFilename: string
  1709. ): TTestEnginePasResolver;
  1710. begin
  1711. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1712. if FindModuleWithFilename(aFilename)<>nil then
  1713. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1714. Result:=TTestEnginePasResolver.Create;
  1715. Result.Filename:=aFilename;
  1716. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1717. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1718. Result.OnLog:=@OnPasResolverLog;
  1719. Result.Hub:=Hub;
  1720. FResolvers.Add(Result);
  1721. end;
  1722. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1723. ): TTestEnginePasResolver;
  1724. begin
  1725. Result:=AddModule(aFilename);
  1726. Result.Source:=Src;
  1727. end;
  1728. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1729. ImplementationSrc: string): TTestEnginePasResolver;
  1730. var
  1731. Src: String;
  1732. begin
  1733. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1734. Src+=LineEnding;
  1735. Src+='interface'+LineEnding;
  1736. Src+=LineEnding;
  1737. Src+=InterfaceSrc;
  1738. Src+='implementation'+LineEnding;
  1739. Src+=LineEnding;
  1740. Src+=ImplementationSrc;
  1741. Src+='end.'+LineEnding;
  1742. Result:=AddModuleWithSrc(aFilename,Src);
  1743. end;
  1744. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1745. var
  1746. Intf, Impl: TStringList;
  1747. begin
  1748. Intf:=TStringList.Create;
  1749. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1750. // unit interface
  1751. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1752. Intf.Add('{$modeswitch externalclass}');
  1753. Intf.Add('type');
  1754. Intf.Add(' integer=longint;');
  1755. Intf.Add(' sizeint=nativeint;');
  1756. //'const',
  1757. //' LineEnding = #10;',
  1758. //' DirectorySeparator = ''/'';',
  1759. //' DriveSeparator = '''';',
  1760. //' AllowDirectorySeparators : set of AnsiChar = [''\'',''/''];',
  1761. //' AllowDriveSeparators : set of AnsiChar = [];',
  1762. if supTObject in Parts then
  1763. Intf.AddStrings([
  1764. 'type',
  1765. ' TClass = class of TObject;',
  1766. ' TObject = class',
  1767. ' constructor Create;',
  1768. ' destructor Destroy; virtual;',
  1769. ' class function ClassType: TClass; assembler;',
  1770. ' class function ClassName: String; assembler;',
  1771. ' class function ClassNameIs(const Name: string): boolean;',
  1772. ' class function ClassParent: TClass; assembler;',
  1773. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1774. ' class function UnitName: String; assembler;',
  1775. ' procedure AfterConstruction; virtual;',
  1776. ' procedure BeforeDestruction;virtual;',
  1777. ' function Equals(Obj: TObject): boolean; virtual;',
  1778. ' function ToString: String; virtual;',
  1779. ' end;']);
  1780. if supTInterfacedObject in Parts then
  1781. Intf.AddStrings([
  1782. ' {$Interfaces COM}',
  1783. ' IUnknown = interface',
  1784. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1785. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1786. ' function _AddRef: Integer;',
  1787. ' function _Release: Integer;',
  1788. ' end;',
  1789. ' IInterface = IUnknown;',
  1790. ' TInterfacedObject = class(TObject,IUnknown)',
  1791. ' protected',
  1792. ' fRefCount: Integer;',
  1793. ' { implement methods of IUnknown }',
  1794. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1795. ' function _AddRef: Integer; virtual;',
  1796. ' function _Release: Integer; virtual;',
  1797. ' end;',
  1798. ' TInterfacedClass = class of TInterfacedObject;',
  1799. '',
  1800. '']);
  1801. if supTVarRec in Parts then
  1802. Intf.AddStrings([
  1803. 'const',
  1804. ' vtInteger = 0;',
  1805. ' vtBoolean = 1;',
  1806. ' vtJSValue = 19;',
  1807. 'type',
  1808. ' PVarRec = ^TVarRec;',
  1809. ' TVarRec = record',
  1810. ' VType : byte;',
  1811. ' VJSValue: JSValue;',
  1812. ' vInteger: longint external name ''VJSValue'';',
  1813. ' vBoolean: boolean external name ''VJSValue'';',
  1814. ' end;',
  1815. ' TVarRecArray = array of TVarRec;',
  1816. 'function VarRecs: TVarRecArray; varargs;',
  1817. '']);
  1818. if supTypeInfo in Parts then
  1819. begin
  1820. Intf.AddStrings([
  1821. 'type',
  1822. ' TTypeKind = (',
  1823. ' tkUnknown, // 0',
  1824. ' tkInteger, // 1',
  1825. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1826. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1827. ' tkEnumeration, // 4',
  1828. ' tkSet, // 5',
  1829. ' tkDouble, // 6',
  1830. ' tkBool, // 7',
  1831. ' tkProcVar, // 8 function or procedure',
  1832. ' tkMethod, // 9 proc var of object',
  1833. ' tkArray, // 10 static array',
  1834. ' tkDynArray, // 11',
  1835. ' tkRecord, // 12',
  1836. ' tkClass, // 13',
  1837. ' tkClassRef, // 14',
  1838. ' tkPointer, // 15',
  1839. ' tkJSValue, // 16',
  1840. ' tkRefToProcVar, // 17 variable of procedure type',
  1841. ' tkInterface, // 18',
  1842. ' //tkObject,',
  1843. ' //tkSString,tkLString,tkAString,tkWString,',
  1844. ' //tkVariant,',
  1845. ' //tkWChar,',
  1846. ' //tkInt64,',
  1847. ' //tkQWord,',
  1848. ' //tkInterfaceRaw,',
  1849. ' //tkUString,tkUChar,',
  1850. ' tkHelper, // 19',
  1851. ' //tkFile,',
  1852. ' tkExtClass // 20',
  1853. ' );',
  1854. ' TTypeKinds = set of TTypeKind;',
  1855. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1856. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1857. ' end;',
  1858. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1859. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1860. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1861. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1862. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1863. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1864. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1865. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1866. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1867. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1868. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1869. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1870. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1871. '']);
  1872. end;
  1873. if supWriteln in Parts then
  1874. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1875. Intf.Add('var');
  1876. Intf.Add(' ExitCode: Longint = 0;');
  1877. // unit implementation
  1878. Impl:=TStringList.Create;
  1879. if supTObject in Parts then
  1880. Impl.AddStrings([
  1881. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1882. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1883. 'asm',
  1884. 'end;',
  1885. 'constructor TObject.Create; begin end;',
  1886. 'destructor TObject.Destroy; begin end;',
  1887. 'class function TObject.ClassType: TClass; assembler;',
  1888. 'asm',
  1889. 'end;',
  1890. 'class function TObject.ClassName: String; assembler;',
  1891. 'asm',
  1892. 'end;',
  1893. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1894. 'begin',
  1895. ' Result:=SameText(Name,ClassName);',
  1896. 'end;',
  1897. 'class function TObject.ClassParent: TClass; assembler;',
  1898. 'asm',
  1899. 'end;',
  1900. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1901. 'asm',
  1902. 'end;',
  1903. 'class function TObject.UnitName: String; assembler;',
  1904. 'asm',
  1905. 'end;',
  1906. 'procedure TObject.AfterConstruction; begin end;',
  1907. 'procedure TObject.BeforeDestruction; begin end;',
  1908. 'function TObject.Equals(Obj: TObject): boolean;',
  1909. 'begin',
  1910. ' Result:=Obj=Self;',
  1911. 'end;',
  1912. 'function TObject.ToString: String;',
  1913. 'begin',
  1914. ' Result:=ClassName;',
  1915. 'end;'
  1916. ]);
  1917. if supTInterfacedObject in Parts then
  1918. Impl.AddStrings([
  1919. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1920. //'begin',
  1921. //'end;',
  1922. 'function TInterfacedObject._AddRef: Integer;',
  1923. 'begin',
  1924. 'end;',
  1925. 'function TInterfacedObject._Release: Integer;',
  1926. 'begin',
  1927. 'end;',
  1928. '']);
  1929. if supTVarRec in Parts then
  1930. Impl.AddStrings([
  1931. 'function VarRecs: TVarRecArray; varargs;',
  1932. 'var',
  1933. ' v: PVarRec;',
  1934. 'begin',
  1935. ' v^.VType:=1;',
  1936. ' v^.VJSValue:=2;',
  1937. 'end;',
  1938. '']);
  1939. try
  1940. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1941. finally
  1942. Intf.Free;
  1943. Impl.Free;
  1944. end;
  1945. end;
  1946. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1947. SystemUnitParts: TSystemUnitParts);
  1948. begin
  1949. if NeedSystemUnit then
  1950. AddSystemUnit(SystemUnitParts)
  1951. else
  1952. Parser.ImplicitUses.Clear;
  1953. Add('program '+ExtractFileUnitName(Filename)+';');
  1954. Add('');
  1955. end;
  1956. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1957. SystemUnitParts: TSystemUnitParts);
  1958. begin
  1959. if NeedSystemUnit then
  1960. AddSystemUnit(SystemUnitParts)
  1961. else
  1962. Parser.ImplicitUses.Clear;
  1963. Add('library '+ExtractFileUnitName(Filename)+';');
  1964. Add('');
  1965. end;
  1966. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1967. SystemUnitParts: TSystemUnitParts);
  1968. begin
  1969. if NeedSystemUnit then
  1970. AddSystemUnit(SystemUnitParts)
  1971. else
  1972. Parser.ImplicitUses.Clear;
  1973. Add('unit Test1;');
  1974. Add('');
  1975. end;
  1976. procedure TCustomTestModule.ConvertModule;
  1977. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1978. out UsesLit: TJSArrayLiteral);
  1979. var
  1980. i: Integer;
  1981. Item: TJSElement;
  1982. Lit: TJSLiteral;
  1983. begin
  1984. UsesLit:=nil;
  1985. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1986. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1987. exit; // null is ok
  1988. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1989. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1990. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1991. begin
  1992. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1993. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1994. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1995. Lit:=TJSLiteral(Item);
  1996. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1997. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1998. end;
  1999. end;
  2000. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2001. out Src: TJSSourceElements);
  2002. var
  2003. FunDecl: TJSFunctionDeclarationStatement;
  2004. FunDef: TJSFuncDef;
  2005. FunBody: TJSFunctionBody;
  2006. begin
  2007. Src:=nil;
  2008. AssertNotNull(ParamName,Arg.Expr);
  2009. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2010. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2011. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2012. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2013. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2014. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2015. AssertNotNull(ParamName+' body',FunDef.Body);
  2016. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2017. FunBody:=FunDef.Body as TJSFunctionBody;
  2018. AssertNotNull(ParamName+' body.A',FunBody.A);
  2019. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2020. Src:=FunBody.A as TJSSourceElements;
  2021. end;
  2022. var
  2023. ModuleNameExpr: TJSLiteral;
  2024. InitFunction: TJSFunctionDeclarationStatement;
  2025. InitAssign: TJSSimpleAssignStatement;
  2026. InitName: String;
  2027. LastNode, FirstNode: TJSElement;
  2028. Arg: TJSArrayLiteralElement;
  2029. IsProg, IsLib: Boolean;
  2030. begin
  2031. if SkipTests then exit;
  2032. IsProg:=false;
  2033. IsLib:=false;
  2034. if Module is TPasProgram then
  2035. IsProg:=true
  2036. else if Module is TPasLibrary then
  2037. IsLib:=true;
  2038. try
  2039. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2040. except
  2041. on E: Exception do
  2042. HandleException(E);
  2043. end;
  2044. if SkipTests then exit;
  2045. if ExpectedErrorClass<>nil then
  2046. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2047. FJSSource:=TStringList.Create;
  2048. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2049. {$IFDEF VerbosePas2JS}
  2050. writeln('TTestModule.ConvertModule JS:');
  2051. write(FJSSource.Text);
  2052. {$ENDIF}
  2053. // rtl.module(...
  2054. if JSModule.Statements.Count<1 then
  2055. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2056. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2057. AssertNotNull('register module call',FirstNode);
  2058. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2059. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2060. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2061. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2062. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2063. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2064. // parameter 'unitname'
  2065. if JSModuleCallArgs.Elements.Count<1 then
  2066. Fail('rtl.module first param unit missing');
  2067. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2068. AssertNotNull('module name param',Arg.Expr);
  2069. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2070. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2071. if IsProg then
  2072. begin
  2073. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2074. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2075. end
  2076. else if IsLib then
  2077. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2078. else
  2079. begin
  2080. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2081. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2082. end;
  2083. // main uses section
  2084. if JSModuleCallArgs.Elements.Count<2 then
  2085. Fail('rtl.module second param main uses missing');
  2086. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2087. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2088. // program/library/interface function()
  2089. if JSModuleCallArgs.Elements.Count<3 then
  2090. Fail('rtl.module third param intf-function missing');
  2091. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2092. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2093. // search for $mod.$init or $mod.$main - the last statement
  2094. if IsProg or IsLib then
  2095. begin
  2096. InitName:='$main';
  2097. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2098. end
  2099. else
  2100. InitName:='$init';
  2101. InitAssign:=nil;
  2102. InitFunction:=nil;
  2103. FJSInitBody:=nil;
  2104. if JSModuleSrc.Statements.Count>0 then
  2105. begin
  2106. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2107. if LastNode is TJSSimpleAssignStatement then
  2108. begin
  2109. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2110. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2111. begin
  2112. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2113. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2114. end
  2115. else if IsProg or IsLib then
  2116. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2117. end;
  2118. end;
  2119. // optional: implementation uses section
  2120. if JSModuleCallArgs.Elements.Count<4 then
  2121. exit;
  2122. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2123. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2124. end;
  2125. procedure TCustomTestModule.ConvertProgram;
  2126. begin
  2127. Add('end.');
  2128. ParseProgram;
  2129. ConvertModule;
  2130. end;
  2131. procedure TCustomTestModule.ConvertLibrary;
  2132. begin
  2133. Add('end.');
  2134. ParseLibrary;
  2135. ConvertModule;
  2136. end;
  2137. procedure TCustomTestModule.ConvertUnit;
  2138. begin
  2139. Add('end.');
  2140. ParseUnit;
  2141. ConvertModule;
  2142. end;
  2143. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2144. begin
  2145. Result:=tcmodules.JSToStr(El);
  2146. end;
  2147. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2148. DottedName: string);
  2149. begin
  2150. if DottedName='' then
  2151. begin
  2152. AssertNull(Msg,El);
  2153. end
  2154. else
  2155. begin
  2156. AssertNotNull(Msg,El);
  2157. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2158. end;
  2159. end;
  2160. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2161. begin
  2162. if El=nil then
  2163. Result:=''
  2164. else if El is TJSPrimaryExpressionIdent then
  2165. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2166. else if El is TJSDotMemberExpression then
  2167. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2168. else
  2169. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2170. end;
  2171. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2172. InitStatements: string; ImplStatements: string);
  2173. var
  2174. ActualSrc, ExpectedSrc, InitName: String;
  2175. IsProg, IsLib: Boolean;
  2176. begin
  2177. ActualSrc:=JSToStr(JSModuleSrc);
  2178. if coUseStrict in Converter.Options then
  2179. ExpectedSrc:='"use strict";'+LineEnding
  2180. else
  2181. ExpectedSrc:='';
  2182. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2183. ExpectedSrc:=ExpectedSrc+Statements;
  2184. // unit implementation
  2185. if (Trim(ImplStatements)<>'') then
  2186. ExpectedSrc:=ExpectedSrc+LineEnding
  2187. +'$mod.$implcode = function () {'+LineEnding
  2188. +ImplStatements
  2189. +'};'+LineEnding;
  2190. // program main or unit initialization
  2191. IsProg:=false;
  2192. IsLib:=false;
  2193. if Module is TPasProgram then
  2194. IsProg:=true
  2195. else if Module is TPasLibrary then
  2196. IsLib:=true;
  2197. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2198. begin
  2199. if IsProg or IsLib then
  2200. InitName:='$main'
  2201. else
  2202. InitName:='$init';
  2203. ExpectedSrc:=ExpectedSrc+LineEnding
  2204. +'$mod.'+InitName+' = function () {'+LineEnding
  2205. +InitStatements
  2206. +'};'+LineEnding;
  2207. end;
  2208. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2209. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2210. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2211. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2212. end;
  2213. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2214. var
  2215. ActualSrc: String;
  2216. begin
  2217. ActualSrc:=JSToStr(JSModule);
  2218. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2219. end;
  2220. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2221. // search diff, ignore changes in spaces
  2222. var
  2223. s: string;
  2224. begin
  2225. if CheckSrcDiff(Expected,Actual,s) then exit;
  2226. Fail(Msg+': '+s);
  2227. end;
  2228. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2229. var
  2230. aResolver: TTestEnginePasResolver;
  2231. aConverter: TPasToJSConverter;
  2232. aJSModule: TJSSourceElements;
  2233. ActualSrc: String;
  2234. begin
  2235. aResolver:=GetResolver(Filename);
  2236. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2237. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2238. {$IFDEF VerbosePas2JS}
  2239. writeln('CheckUnit '+Filename+' converting ...');
  2240. {$ENDIF}
  2241. aConverter:=CreateConverter;
  2242. aJSModule:=nil;
  2243. try
  2244. try
  2245. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2246. except
  2247. on E: Exception do
  2248. HandleException(E);
  2249. end;
  2250. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2251. {$IFDEF VerbosePas2JS}
  2252. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2253. write(aResolver.Source);
  2254. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2255. write(ActualSrc);
  2256. {$ENDIF}
  2257. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2258. finally
  2259. aJSModule.Free;
  2260. aConverter.Free;
  2261. end;
  2262. end;
  2263. procedure TCustomTestModule.CheckReferenceDirectives;
  2264. var
  2265. CurFilename: string;
  2266. LineNumber: Integer;
  2267. SrcLine: String;
  2268. CommentStartP, CommentEndP: PChar;
  2269. procedure RaiseError(Msg: string; p: PChar);
  2270. begin
  2271. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2272. end;
  2273. procedure AddMarker(Marker: PSrcMarker);
  2274. begin
  2275. if LastSrcMarker<>nil then
  2276. LastSrcMarker^.Next:=Marker
  2277. else
  2278. FirstSrcMarker:=Marker;
  2279. LastSrcMarker:=Marker;
  2280. end;
  2281. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2282. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2283. begin
  2284. New(Result);
  2285. Result^.Kind:=Kind;
  2286. Result^.Filename:=aFilename;
  2287. Result^.Row:=aLine;
  2288. Result^.StartCol:=aStartCol;
  2289. Result^.EndCol:=aEndCol;
  2290. Result^.Identifier:=Identifier;
  2291. Result^.Next:=nil;
  2292. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2293. AddMarker(Result);
  2294. end;
  2295. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2296. const Identifier: string): PSrcMarker;
  2297. var
  2298. TokenStart, p: PChar;
  2299. begin
  2300. p:=CommentEndP;
  2301. ReadNextPascalToken(p,TokenStart,false,false);
  2302. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2303. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2304. end;
  2305. function ReadIdentifier(var p: PChar): string;
  2306. var
  2307. StartP: PChar;
  2308. begin
  2309. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2310. RaiseError('identifier expected',p);
  2311. StartP:=p;
  2312. inc(p);
  2313. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2314. Result:='';
  2315. SetLength(Result,p-StartP);
  2316. Move(StartP^,Result[1],length(Result)*sizeof(char));
  2317. end;
  2318. procedure AddLabel;
  2319. var
  2320. Identifier: String;
  2321. p: PChar;
  2322. begin
  2323. p:=CommentStartP+2;
  2324. Identifier:=ReadIdentifier(p);
  2325. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2326. if FindSrcLabel(Identifier)<>nil then
  2327. RaiseError('duplicate label "'+Identifier+'"',p);
  2328. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2329. end;
  2330. procedure AddResolverReference;
  2331. var
  2332. Identifier: String;
  2333. p: PChar;
  2334. begin
  2335. p:=CommentStartP+2;
  2336. Identifier:=ReadIdentifier(p);
  2337. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2338. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2339. end;
  2340. procedure AddDirectReference;
  2341. var
  2342. Identifier: String;
  2343. p: PChar;
  2344. begin
  2345. p:=CommentStartP+2;
  2346. Identifier:=ReadIdentifier(p);
  2347. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2348. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2349. end;
  2350. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2351. var
  2352. p,pstart,pend: PChar;
  2353. IsDirective: Boolean;
  2354. begin
  2355. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2356. CurFilename:=aFilename;
  2357. // parse code, find all labels
  2358. LineNumber:=0;
  2359. while LineNumber<SrcLines.Count do
  2360. begin
  2361. inc(LineNumber);
  2362. SrcLine:=SrcLines[LineNumber-1];
  2363. if SrcLine='' then continue;
  2364. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2365. pstart:=PChar(SrcLine);
  2366. pend:=pstart;
  2367. inc(PEnd,length(SrcLine));
  2368. p:=pstart;
  2369. repeat
  2370. case p^ of
  2371. #0: if (p>=pend) then break;
  2372. '{':
  2373. begin
  2374. CommentStartP:=p;
  2375. inc(p);
  2376. IsDirective:=p^ in ['#','@','='];
  2377. // skip to end of comment
  2378. repeat
  2379. case p^ of
  2380. #0:
  2381. if (p>=pend) then
  2382. begin
  2383. // multi line comment
  2384. if IsDirective then
  2385. RaiseError('directive missing closing bracket',CommentStartP);
  2386. repeat
  2387. inc(LineNumber);
  2388. if LineNumber>SrcLines.Count then exit;
  2389. SrcLine:=SrcLines[LineNumber-1];
  2390. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2391. until SrcLine<>'';
  2392. pstart:=PChar(SrcLine);
  2393. pend:=pstart;
  2394. inc(PEnd,length(SrcLine));
  2395. p:=pstart;
  2396. continue;
  2397. end;
  2398. '}':
  2399. begin
  2400. inc(p);
  2401. break;
  2402. end;
  2403. end;
  2404. inc(p);
  2405. until false;
  2406. CommentEndP:=p;
  2407. case CommentStartP[1] of
  2408. '#': AddLabel;
  2409. '@': AddResolverReference;
  2410. '=': AddDirectReference;
  2411. end;
  2412. p:=CommentEndP;
  2413. continue;
  2414. end;
  2415. '/':
  2416. if p[1]='/' then
  2417. break; // rest of line is comment -> skip
  2418. end;
  2419. inc(p);
  2420. until false;
  2421. end;
  2422. end;
  2423. procedure CheckResolverReference(aMarker: PSrcMarker);
  2424. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2425. var
  2426. aLabel: PSrcMarker;
  2427. ReferenceElements, LabelElements: TFPList;
  2428. i, j, aLine, aCol: Integer;
  2429. El, Ref, LabelEl: TPasElement;
  2430. begin
  2431. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2432. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2433. if aLabel=nil then
  2434. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2435. LabelElements:=nil;
  2436. ReferenceElements:=nil;
  2437. try
  2438. LabelElements:=FindElementsAt(aLabel);
  2439. ReferenceElements:=FindElementsAt(aMarker);
  2440. for i:=0 to ReferenceElements.Count-1 do
  2441. begin
  2442. El:=TPasElement(ReferenceElements[i]);
  2443. Ref:=nil;
  2444. if El.CustomData is TResolvedReference then
  2445. Ref:=TResolvedReference(El.CustomData).Declaration
  2446. else if El.CustomData is TPasPropertyScope then
  2447. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2448. else if El.CustomData is TPasSpecializeTypeData then
  2449. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2450. if Ref<>nil then
  2451. for j:=0 to LabelElements.Count-1 do
  2452. begin
  2453. LabelEl:=TPasElement(LabelElements[j]);
  2454. if Ref=LabelEl then
  2455. exit; // success
  2456. end;
  2457. end;
  2458. // failure write candidates
  2459. for i:=0 to ReferenceElements.Count-1 do
  2460. begin
  2461. El:=TPasElement(ReferenceElements[i]);
  2462. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2463. write(' El=',GetObjName(El));
  2464. if EL is TPrimitiveExpr then
  2465. begin
  2466. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2467. end;
  2468. Ref:=nil;
  2469. if El.CustomData is TResolvedReference then
  2470. Ref:=TResolvedReference(El.CustomData).Declaration
  2471. else if El.CustomData is TPasPropertyScope then
  2472. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2473. else if El.CustomData is TPasSpecializeTypeData then
  2474. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2475. if Ref<>nil then
  2476. begin
  2477. write(' Decl=',GetObjName(Ref));
  2478. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2479. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2480. end
  2481. else
  2482. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2483. writeln;
  2484. end;
  2485. for i:=0 to LabelElements.Count-1 do
  2486. begin
  2487. El:=TPasElement(LabelElements[i]);
  2488. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2489. write(' El=',GetObjName(El));
  2490. writeln;
  2491. end;
  2492. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2493. finally
  2494. LabelElements.Free;
  2495. ReferenceElements.Free;
  2496. end;
  2497. end;
  2498. procedure CheckDirectReference(aMarker: PSrcMarker);
  2499. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2500. var
  2501. aLabel: PSrcMarker;
  2502. ReferenceElements, LabelElements: TFPList;
  2503. i, LabelLine, LabelCol, j: Integer;
  2504. El, LabelEl: TPasElement;
  2505. DeclEl, TypeEl: TPasType;
  2506. begin
  2507. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2508. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2509. if aLabel=nil then
  2510. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2511. LabelElements:=nil;
  2512. ReferenceElements:=nil;
  2513. try
  2514. //writeln('CheckDirectReference finding elements at label ...');
  2515. LabelElements:=FindElementsAt(aLabel);
  2516. //writeln('CheckDirectReference finding elements at reference ...');
  2517. ReferenceElements:=FindElementsAt(aMarker);
  2518. for i:=0 to ReferenceElements.Count-1 do
  2519. begin
  2520. El:=TPasElement(ReferenceElements[i]);
  2521. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2522. if El.ClassType=TPasVariable then
  2523. begin
  2524. if TPasVariable(El).VarType=nil then
  2525. begin
  2526. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2527. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2528. end;
  2529. TypeEl:=TPasVariable(El).VarType;
  2530. for j:=0 to LabelElements.Count-1 do
  2531. begin
  2532. LabelEl:=TPasElement(LabelElements[j]);
  2533. if TypeEl=LabelEl then
  2534. exit; // success
  2535. end;
  2536. end
  2537. else if El is TPasAliasType then
  2538. begin
  2539. DeclEl:=TPasAliasType(El).DestType;
  2540. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2541. if (aLabel^.Filename=DeclEl.SourceFilename)
  2542. and (integer(aLabel^.Row)=LabelLine)
  2543. and (aLabel^.StartCol<=LabelCol)
  2544. and (aLabel^.EndCol>=LabelCol) then
  2545. exit; // success
  2546. end
  2547. else if El.ClassType=TPasArgument then
  2548. begin
  2549. TypeEl:=TPasArgument(El).ArgType;
  2550. for j:=0 to LabelElements.Count-1 do
  2551. begin
  2552. LabelEl:=TPasElement(LabelElements[j]);
  2553. if TypeEl=LabelEl then
  2554. exit; // success
  2555. end;
  2556. end;
  2557. end;
  2558. // failed -> show candidates
  2559. writeln('CheckDirectReference failed: Labels:');
  2560. for j:=0 to LabelElements.Count-1 do
  2561. begin
  2562. LabelEl:=TPasElement(LabelElements[j]);
  2563. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2564. end;
  2565. writeln('CheckDirectReference failed: References:');
  2566. for i:=0 to ReferenceElements.Count-1 do
  2567. begin
  2568. El:=TPasElement(ReferenceElements[i]);
  2569. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2570. //if EL is TPasVariable then
  2571. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2572. end;
  2573. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2574. finally
  2575. LabelElements.Free;
  2576. ReferenceElements.Free;
  2577. end;
  2578. end;
  2579. var
  2580. aMarker: PSrcMarker;
  2581. i: Integer;
  2582. SrcLines: TStringList;
  2583. begin
  2584. Module.ForEachCall(@OnCheckElementParent,nil);
  2585. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2586. // find all markers
  2587. for i:=0 to FileResolver.Streams.Count-1 do
  2588. begin
  2589. GetSrc(i,SrcLines,CurFilename);
  2590. ParseCode(SrcLines,CurFilename);
  2591. SrcLines.Free;
  2592. end;
  2593. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2594. // check references
  2595. aMarker:=FirstSrcMarker;
  2596. while aMarker<>nil do
  2597. begin
  2598. case aMarker^.Kind of
  2599. mkResolverReference: CheckResolverReference(aMarker);
  2600. mkDirectReference: CheckDirectReference(aMarker);
  2601. end;
  2602. aMarker:=aMarker^.Next;
  2603. end;
  2604. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2605. end;
  2606. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2607. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2608. var
  2609. i: Integer;
  2610. Item: TTestHintMessage;
  2611. Expected,Actual: string;
  2612. begin
  2613. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2614. for i:=0 to MsgCount-1 do
  2615. begin
  2616. Item:=Msgs[i];
  2617. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2618. if (Marker<>nil) then
  2619. begin
  2620. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2621. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2622. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2623. end;
  2624. // found
  2625. FHintMsgsGood.Add(Item);
  2626. str(Item.MsgType,Actual);
  2627. str(MsgType,Expected);
  2628. AssertEquals('MsgType',Expected,Actual);
  2629. exit;
  2630. end;
  2631. // needed message missing -> show emitted messages
  2632. WriteSources('',0,0);
  2633. for i:=0 to MsgCount-1 do
  2634. begin
  2635. Item:=Msgs[i];
  2636. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2637. ' ('+IntToStr(Item.MsgNumber),')');
  2638. if Marker<>nil then
  2639. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2640. writeln(' {',Item.Msg,'}');
  2641. end;
  2642. str(MsgType,Expected);
  2643. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2644. if Marker<>nil then
  2645. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2646. Actual:=Actual+' '+Msg;
  2647. Fail(Actual);
  2648. end;
  2649. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2650. );
  2651. var
  2652. i: Integer;
  2653. s, Txt: String;
  2654. Msg: TTestHintMessage;
  2655. begin
  2656. for i:=0 to MsgCount-1 do
  2657. begin
  2658. Msg:=Msgs[i];
  2659. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2660. s:='';
  2661. str(Msg.MsgType,s);
  2662. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2663. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2664. if WithSourcePos then
  2665. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2666. Txt:=Txt+' {'+Msg.Msg+'}';
  2667. Fail(Txt);
  2668. end;
  2669. end;
  2670. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2671. MsgNumber: integer);
  2672. begin
  2673. ExpectedErrorClass:=EScannerError;
  2674. ExpectedErrorMsg:=Msg;
  2675. ExpectedErrorNumber:=MsgNumber;
  2676. end;
  2677. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2678. MsgNumber: integer);
  2679. begin
  2680. ExpectedErrorClass:=EParserError;
  2681. ExpectedErrorMsg:=Msg;
  2682. ExpectedErrorNumber:=MsgNumber;
  2683. end;
  2684. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2685. MsgNumber: integer);
  2686. begin
  2687. ExpectedErrorClass:=EPasResolve;
  2688. ExpectedErrorMsg:=Msg;
  2689. ExpectedErrorNumber:=MsgNumber;
  2690. end;
  2691. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2692. MsgNumber: integer);
  2693. begin
  2694. ExpectedErrorClass:=EPas2JS;
  2695. ExpectedErrorMsg:=Msg;
  2696. ExpectedErrorNumber:=MsgNumber;
  2697. end;
  2698. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2699. var
  2700. MsgNumber: Integer;
  2701. Msg: String;
  2702. begin
  2703. Result:=false;
  2704. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2705. Msg:=E.Message;
  2706. if E is EPas2JS then
  2707. MsgNumber:=EPas2JS(E).MsgNumber
  2708. else if E is EPasResolve then
  2709. MsgNumber:=EPasResolve(E).MsgNumber
  2710. else if E is EParserError then
  2711. MsgNumber:=Parser.LastMsgNumber
  2712. else if E is EScannerError then
  2713. begin
  2714. MsgNumber:=Scanner.LastMsgNumber;
  2715. Msg:=Scanner.LastMsg;
  2716. end
  2717. else
  2718. MsgNumber:=0;
  2719. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2720. if Result then
  2721. SkipTests:=true;
  2722. end;
  2723. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2724. const aFilename: string; aRow, aCol: integer);
  2725. var
  2726. s: String;
  2727. begin
  2728. WriteSources(aFilename,aRow,aCol);
  2729. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2730. writeln('ERROR: ',s);
  2731. Fail(s);
  2732. end;
  2733. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2734. aMarker: PSrcMarker);
  2735. begin
  2736. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2737. end;
  2738. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2739. begin
  2740. if IsErrorExpected(E) then exit;
  2741. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2742. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2743. +' '+Scanner.CurFilename
  2744. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2745. FailException(E);
  2746. end;
  2747. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2748. begin
  2749. if IsErrorExpected(E) then exit;
  2750. WriteSources(E.Filename,E.Row,E.Column);
  2751. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2752. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2753. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2754. );
  2755. FailException(E);
  2756. end;
  2757. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2758. var
  2759. P: TPasSourcePos;
  2760. begin
  2761. if IsErrorExpected(E) then exit;
  2762. P:=E.SourcePos;
  2763. WriteSources(P.FileName,P.Row,P.Column);
  2764. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2765. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2766. FailException(E);
  2767. end;
  2768. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2769. var
  2770. Row, Col: integer;
  2771. begin
  2772. if IsErrorExpected(E) then exit;
  2773. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2774. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2775. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2776. +' '+E.PasElement.SourceFilename
  2777. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2778. FailException(E);
  2779. end;
  2780. procedure TCustomTestModule.HandleException(E: Exception);
  2781. begin
  2782. if E is EScannerError then
  2783. HandleScannerError(EScannerError(E))
  2784. else if E is EParserError then
  2785. HandleParserError(EParserError(E))
  2786. else if E is EPasResolve then
  2787. HandlePasResolveError(EPasResolve(E))
  2788. else if E is EPas2JS then
  2789. HandlePas2JSError(EPas2JS(E))
  2790. else
  2791. begin
  2792. if IsErrorExpected(E) then exit;
  2793. if not (E is EAssertionFailedError) then
  2794. begin
  2795. WriteSources('',0,0);
  2796. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2797. end;
  2798. FailException(E);
  2799. end;
  2800. end;
  2801. procedure TCustomTestModule.FailException(E: Exception);
  2802. var
  2803. MsgNumber: Integer;
  2804. begin
  2805. if ExpectedErrorClass<>nil then
  2806. begin
  2807. if FExpectedErrorClass=E.ClassType then
  2808. begin
  2809. if E is EPas2JS then
  2810. MsgNumber:=EPas2JS(E).MsgNumber
  2811. else if E is EPasResolve then
  2812. MsgNumber:=EPasResolve(E).MsgNumber
  2813. else if E is EParserError then
  2814. MsgNumber:=Parser.LastMsgNumber
  2815. else if E is EScannerError then
  2816. MsgNumber:=Scanner.LastMsgNumber
  2817. else
  2818. MsgNumber:=0;
  2819. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2820. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2821. ExpectedErrorNumber,MsgNumber);
  2822. end else begin
  2823. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2824. end;
  2825. end;
  2826. Fail(E.Message);
  2827. end;
  2828. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2829. aCol: integer);
  2830. var
  2831. IsSrc: Boolean;
  2832. i, j: Integer;
  2833. SrcLines: TStringList;
  2834. Line: string;
  2835. aModule: TTestEnginePasResolver;
  2836. begin
  2837. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2838. for i:=0 to ResolverCount-1 do
  2839. begin
  2840. aModule:=Resolvers[i];
  2841. SrcLines:=TStringList.Create;
  2842. try
  2843. SrcLines.Text:=aModule.Source;
  2844. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2845. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2846. for j:=1 to SrcLines.Count do
  2847. begin
  2848. Line:=SrcLines[j-1];
  2849. if IsSrc and (j=aRow) then
  2850. begin
  2851. write('*');
  2852. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2853. end;
  2854. writeln(Format('%:4d: ',[j]),Line);
  2855. end;
  2856. finally
  2857. SrcLines.Free;
  2858. end;
  2859. end;
  2860. end;
  2861. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2862. var
  2863. i: Integer;
  2864. begin
  2865. for i:=0 to ResolverCount-1 do
  2866. if Filename=Resolvers[i].Filename then exit(i);
  2867. Result:=-1;
  2868. end;
  2869. function TCustomTestModule.GetResolver(const Filename: string
  2870. ): TTestEnginePasResolver;
  2871. var
  2872. i: Integer;
  2873. begin
  2874. i:=IndexOfResolver(Filename);
  2875. if i<0 then exit(nil);
  2876. Result:=Resolvers[i];
  2877. end;
  2878. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2879. out aFilename: string);
  2880. var
  2881. aStream: TStream;
  2882. begin
  2883. SrcLines:=TStringList.Create;
  2884. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2885. aStream.Position:=0;
  2886. SrcLines.LoadFromStream(aStream);
  2887. aFilename:=FileResolver.Streams[Index];
  2888. end;
  2889. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2890. aEndCol: integer): TFPList;
  2891. var
  2892. ok: Boolean;
  2893. FoundRefs: TTestResolverReferenceData;
  2894. i: Integer;
  2895. CurResolver: TTestEnginePasResolver;
  2896. begin
  2897. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2898. FoundRefs:=Default(TTestResolverReferenceData);
  2899. FoundRefs.Filename:=aFilename;
  2900. FoundRefs.Row:=aLine;
  2901. FoundRefs.StartCol:=aStartCol;
  2902. FoundRefs.EndCol:=aEndCol;
  2903. FoundRefs.Found:=TFPList.Create;
  2904. ok:=false;
  2905. try
  2906. // find all markers
  2907. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2908. for i:=0 to ResolverCount-1 do
  2909. begin
  2910. CurResolver:=Resolvers[i];
  2911. if CurResolver.Module=Module then continue;
  2912. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2913. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2914. end;
  2915. ok:=true;
  2916. finally
  2917. if not ok then
  2918. FreeAndNil(FoundRefs.Found);
  2919. end;
  2920. Result:=FoundRefs.Found;
  2921. FoundRefs.Found:=nil;
  2922. end;
  2923. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2924. ErrorOnNoElements: boolean): TFPList;
  2925. begin
  2926. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2927. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2928. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2929. end;
  2930. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2931. begin
  2932. Result:=FirstSrcMarker;
  2933. while Result<>nil do
  2934. begin
  2935. if (Result^.Kind=mkLabel)
  2936. and (CompareText(Result^.Identifier,Identifier)=0) then
  2937. exit;
  2938. Result:=Result^.Next;
  2939. end;
  2940. end;
  2941. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2942. ErrorOnNoElements: boolean): TFPList;
  2943. var
  2944. SrcLabel: PSrcMarker;
  2945. begin
  2946. SrcLabel:=FindSrcLabel(Identifier);
  2947. if SrcLabel=nil then
  2948. Fail('missing label "'+Identifier+'"');
  2949. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2950. end;
  2951. function TCustomTestModule.GetDefaultNamespace: string;
  2952. var
  2953. C: TClass;
  2954. begin
  2955. Result:='';
  2956. if FModule=nil then exit;
  2957. C:=FModule.ClassType;
  2958. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2959. Result:=ResolverEngine.DefaultNameSpace;
  2960. end;
  2961. constructor TCustomTestModule.Create;
  2962. begin
  2963. inherited Create;
  2964. FHintMsgs:=TObjectList.Create(true);
  2965. FHintMsgsGood:=TFPList.Create;
  2966. end;
  2967. destructor TCustomTestModule.Destroy;
  2968. begin
  2969. FreeAndNil(FHintMsgs);
  2970. FreeAndNil(FHintMsgsGood);
  2971. inherited Destroy;
  2972. end;
  2973. { TTestModule }
  2974. procedure TTestModule.TestReservedWords;
  2975. var
  2976. i: integer;
  2977. begin
  2978. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2979. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2980. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2981. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2982. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2983. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2984. end;
  2985. procedure TTestModule.TestEmptyProgram;
  2986. begin
  2987. StartProgram(false);
  2988. Add('begin');
  2989. ConvertProgram;
  2990. CheckSource('TestEmptyProgram','','');
  2991. end;
  2992. procedure TTestModule.TestEmptyProgramUseStrict;
  2993. begin
  2994. Converter.Options:=Converter.Options+[coUseStrict];
  2995. StartProgram(false);
  2996. Add('begin');
  2997. ConvertProgram;
  2998. CheckSource('TestEmptyProgramUseStrict','','');
  2999. end;
  3000. procedure TTestModule.TestEmptyUnit;
  3001. begin
  3002. StartUnit(false);
  3003. Add('interface');
  3004. Add('implementation');
  3005. ConvertUnit;
  3006. CheckSource('TestEmptyUnit',
  3007. LinesToStr([
  3008. ]),
  3009. '');
  3010. end;
  3011. procedure TTestModule.TestEmptyUnitUseStrict;
  3012. begin
  3013. Converter.Options:=Converter.Options+[coUseStrict];
  3014. StartUnit(false);
  3015. Add('interface');
  3016. Add('implementation');
  3017. ConvertUnit;
  3018. CheckSource('TestEmptyUnitUseStrict',
  3019. LinesToStr([
  3020. ''
  3021. ]),
  3022. '');
  3023. end;
  3024. procedure TTestModule.TestDottedUnitNames;
  3025. begin
  3026. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3027. LinesToStr([
  3028. 'var iV: longint;'
  3029. ]),
  3030. '');
  3031. FFilename:='ns1.test1.pp';
  3032. StartProgram(true);
  3033. Add('uses unIt2;');
  3034. Add('var');
  3035. Add(' i: longint;');
  3036. Add('begin');
  3037. Add(' i:=iv;');
  3038. Add(' i:=uNit2.iv;');
  3039. Add(' i:=Ns1.TEst1.i;');
  3040. ConvertProgram;
  3041. CheckSource('TestDottedUnitNames',
  3042. LinesToStr([
  3043. 'this.i = 0;',
  3044. '']),
  3045. LinesToStr([ // this.$init
  3046. '$mod.i = pas["NS1.Unit2"].iV;',
  3047. '$mod.i = pas["NS1.Unit2"].iV;',
  3048. '$mod.i = $mod.i;',
  3049. '']) );
  3050. end;
  3051. procedure TTestModule.TestDottedUnitNameImpl;
  3052. begin
  3053. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3054. LinesToStr([
  3055. 'type',
  3056. ' TObject = class end;',
  3057. ' TTestA = class',
  3058. ' end;'
  3059. ]),
  3060. LinesToStr(['uses TEST.UnitB;'])
  3061. );
  3062. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3063. LinesToStr([
  3064. 'uses TEST.UnitA;',
  3065. 'type TTestB = class(TTestA);'
  3066. ]),
  3067. ''
  3068. );
  3069. StartProgram(true);
  3070. Add('uses TEST.UnitA;');
  3071. Add('begin');
  3072. ConvertProgram;
  3073. CheckSource('TestDottedUnitNameImpl',
  3074. LinesToStr([
  3075. '']),
  3076. LinesToStr([ // this.$init
  3077. '']) );
  3078. CheckUnit('TEST.UnitA.pas',
  3079. LinesToStr([
  3080. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3081. ' var $mod = this;',
  3082. ' rtl.createClass(this, "TObject", null, function () {',
  3083. ' this.$init = function () {',
  3084. ' };',
  3085. ' this.$final = function () {',
  3086. ' };',
  3087. ' });',
  3088. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3089. ' });',
  3090. '}, ["TEST.UnitB"]);'
  3091. ]));
  3092. CheckUnit('TEST.UnitB.pas',
  3093. LinesToStr([
  3094. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3095. ' var $mod = this;',
  3096. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3097. ' });',
  3098. '});'
  3099. ]));
  3100. end;
  3101. procedure TTestModule.TestDottedUnitExpr;
  3102. begin
  3103. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3104. LinesToStr([
  3105. 'procedure DoIt;'
  3106. ]),
  3107. 'procedure DoIt; begin end;');
  3108. FFilename:='Ns1.SubNs1.Test1.pp';
  3109. StartProgram(true);
  3110. Add('uses Ns2.sUbnS2.unIt2;');
  3111. Add('var');
  3112. Add(' i: longint;');
  3113. Add('begin');
  3114. Add(' ns2.subns2.unit2.doit;');
  3115. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3116. ConvertProgram;
  3117. CheckSource('TestDottedUnitExpr',
  3118. LinesToStr([
  3119. 'this.i = 0;',
  3120. '']),
  3121. LinesToStr([ // this.$init
  3122. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3123. '$mod.i = $mod.i;',
  3124. '']) );
  3125. end;
  3126. procedure TTestModule.Test_ModeFPCFail;
  3127. begin
  3128. StartProgram(false);
  3129. Add('{$mode FPC}');
  3130. Add('begin');
  3131. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3132. ConvertProgram;
  3133. end;
  3134. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3135. begin
  3136. StartProgram(false);
  3137. Add('{$modeswitch cblocks-}');
  3138. Add('begin');
  3139. ConvertProgram;
  3140. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3141. CheckResolverUnexpectedHints();
  3142. end;
  3143. procedure TTestModule.TestUnit_UseSystem;
  3144. begin
  3145. StartUnit(true);
  3146. Add([
  3147. 'interface',
  3148. 'var i: integer;',
  3149. 'implementation']);
  3150. ConvertUnit;
  3151. CheckSource('TestUnit_UseSystem',
  3152. LinesToStr([
  3153. 'this.i = 0;',
  3154. '']),
  3155. LinesToStr([
  3156. '']) );
  3157. end;
  3158. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3159. begin
  3160. AddModuleWithIntfImplSrc('unit1.pp',
  3161. LinesToStr([
  3162. 'type number = longint;']),
  3163. LinesToStr([
  3164. 'uses test1;',
  3165. 'procedure DoIt;',
  3166. 'begin',
  3167. ' i:=3;',
  3168. 'end;']));
  3169. StartUnit(true);
  3170. Add([
  3171. 'interface',
  3172. 'uses unit1;',
  3173. 'var i: number;',
  3174. 'implementation']);
  3175. ConvertUnit;
  3176. CheckSource('TestUnit_Intf1Impl2Intf1',
  3177. LinesToStr([
  3178. 'this.i = 0;',
  3179. '']),
  3180. LinesToStr([
  3181. '']) );
  3182. end;
  3183. procedure TTestModule.TestIncludeVersion;
  3184. begin
  3185. StartProgram(false);
  3186. Add([
  3187. 'var',
  3188. ' s: string;',
  3189. ' i: word;',
  3190. 'begin',
  3191. ' s:={$I %line%};',
  3192. ' i:={$I %linenum%};',
  3193. ' s:={$I %currentroutine%};',
  3194. ' s:={$I %pas2jsversion%};',
  3195. ' s:={$I %pas2jstarget%};',
  3196. ' s:={$I %pas2jstargetos%};',
  3197. ' s:={$I %pas2jstargetcpu%};',
  3198. ' s:={$I %file%};',
  3199. '']);
  3200. ConvertProgram;
  3201. CheckSource('TestIncludeVersion',
  3202. LinesToStr([
  3203. 'this.s="";',
  3204. 'this.i = 0;']),
  3205. LinesToStr([
  3206. '$mod.s = "7";',
  3207. '$mod.i = 8;',
  3208. '$mod.s = "<anonymous>";',
  3209. '$mod.s = "Comp.Ver.tcmodules";',
  3210. '$mod.s = "Browser";',
  3211. '$mod.s = "Browser";',
  3212. '$mod.s = "ECMAScript5";',
  3213. '$mod.s = "test1.pp";',
  3214. '']));
  3215. end;
  3216. procedure TTestModule.TestVarInt;
  3217. begin
  3218. StartProgram(false);
  3219. Add('var MyI: longint;');
  3220. Add('begin');
  3221. ConvertProgram;
  3222. CheckSource('TestVarInt','this.MyI=0;','');
  3223. end;
  3224. procedure TTestModule.TestVarBaseTypes;
  3225. begin
  3226. StartProgram(false);
  3227. Add('var');
  3228. Add(' i: longint;');
  3229. Add(' s: string;');
  3230. Add(' c: AnsiChar;');
  3231. Add(' b: boolean;');
  3232. Add(' d: double;');
  3233. Add(' i2: longint = 3;');
  3234. Add(' s2: string = ''foo'';');
  3235. Add(' c2: AnsiChar = ''4'';');
  3236. Add(' b2: boolean = true;');
  3237. Add(' d2: double = 5.6;');
  3238. Add(' i3: longint = $707;');
  3239. Add(' i4: nativeint = 9007199254740991;');
  3240. Add(' i5: nativeint = -9007199254740991-1;');
  3241. Add(' i6: nativeint = $fffffffffffff;');
  3242. Add(' i7: nativeint = -$fffffffffffff-1;');
  3243. Add(' i8: byte = 00;');
  3244. Add(' u8: nativeuint = $fffffffffffff;');
  3245. Add(' u9: nativeuint = $0000000000000;');
  3246. Add(' u10: nativeuint = $00ff00;');
  3247. Add('begin');
  3248. ConvertProgram;
  3249. CheckSource('TestVarBaseTypes',
  3250. LinesToStr([
  3251. 'this.i = 0;',
  3252. 'this.s = "";',
  3253. 'this.c = "";',
  3254. 'this.b = false;',
  3255. 'this.d = 0.0;',
  3256. 'this.i2 = 3;',
  3257. 'this.s2 = "foo";',
  3258. 'this.c2 = "4";',
  3259. 'this.b2 = true;',
  3260. 'this.d2 = 5.6;',
  3261. 'this.i3 = 0x707;',
  3262. 'this.i4 = 9007199254740991;',
  3263. 'this.i5 = -9007199254740991-1;',
  3264. 'this.i6 = 0xfffffffffffff;',
  3265. 'this.i7 =-0xfffffffffffff-1;',
  3266. 'this.i8 = 0;',
  3267. 'this.u8 = 0xfffffffffffff;',
  3268. 'this.u9 = 0x0;',
  3269. 'this.u10 = 0xff00;'
  3270. ]),
  3271. '');
  3272. end;
  3273. procedure TTestModule.TestBaseTypeSingleFail;
  3274. begin
  3275. StartProgram(false);
  3276. Add('var s: single;');
  3277. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3278. ConvertProgram;
  3279. end;
  3280. procedure TTestModule.TestBaseTypeExtendedFail;
  3281. begin
  3282. StartProgram(false);
  3283. Add('var e: extended;');
  3284. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3285. ConvertProgram;
  3286. end;
  3287. procedure TTestModule.TestConstBaseTypes;
  3288. begin
  3289. StartProgram(false);
  3290. Add('const');
  3291. Add(' i: longint = 3;');
  3292. Add(' s: string = ''foo'';');
  3293. Add(' c: AnsiChar = ''4'';');
  3294. Add(' b: boolean = true;');
  3295. Add(' d: double = 5.6;');
  3296. Add(' e = low(word);');
  3297. Add(' f = high(word);');
  3298. Add('begin');
  3299. ConvertProgram;
  3300. CheckSource('TestVarBaseTypes',
  3301. LinesToStr([
  3302. 'this.i=3;',
  3303. 'this.s="foo";',
  3304. 'this.c="4";',
  3305. 'this.b=true;',
  3306. 'this.d=5.6;',
  3307. 'this.e = 0;',
  3308. 'this.f = 65535;'
  3309. ]),
  3310. '');
  3311. end;
  3312. procedure TTestModule.TestAliasTypeRef;
  3313. begin
  3314. StartProgram(false);
  3315. Add('type');
  3316. Add(' a=longint;');
  3317. Add(' b=a;');
  3318. Add('var');
  3319. Add(' c: A;');
  3320. Add(' d: B;');
  3321. Add('begin');
  3322. ConvertProgram;
  3323. CheckSource('TestAliasTypeRef',
  3324. LinesToStr([ // statements
  3325. 'this.c = 0;',
  3326. 'this.d = 0;'
  3327. ]),
  3328. LinesToStr([ // this.$main
  3329. ''
  3330. ]));
  3331. end;
  3332. procedure TTestModule.TestTypeCast_BaseTypes;
  3333. begin
  3334. StartProgram(false);
  3335. Add([
  3336. 'var',
  3337. ' i: longint;',
  3338. ' b: boolean;',
  3339. ' d: double;',
  3340. ' s: string;',
  3341. ' c: AnsiChar;',
  3342. 'begin',
  3343. ' i:=longint(i);',
  3344. ' i:=longint(b);',
  3345. ' b:=boolean(b);',
  3346. ' b:=boolean(i);',
  3347. ' d:=double(d);',
  3348. ' d:=double(i);',
  3349. ' s:=string(s);',
  3350. ' s:=string(c);',
  3351. ' c:=AnsiChar(c);',
  3352. ' c:=AnsiChar(i);',
  3353. ' c:=AnsiChar(65);',
  3354. ' c:=AnsiChar(#10);',
  3355. ' c:=AnsiChar(#$E000);',
  3356. '']);
  3357. ConvertProgram;
  3358. CheckSource('TestAliasTypeRef',
  3359. LinesToStr([ // statements
  3360. 'this.i = 0;',
  3361. 'this.b = false;',
  3362. 'this.d = 0.0;',
  3363. 'this.s = "";',
  3364. 'this.c = "";',
  3365. '']),
  3366. LinesToStr([ // this.$main
  3367. '$mod.i = $mod.i;',
  3368. '$mod.i = ($mod.b ? 1 : 0);',
  3369. '$mod.b = $mod.b;',
  3370. '$mod.b = $mod.i != 0;',
  3371. '$mod.d = $mod.d;',
  3372. '$mod.d = $mod.i;',
  3373. '$mod.s = $mod.s;',
  3374. '$mod.s = $mod.c;',
  3375. '$mod.c = $mod.c;',
  3376. '$mod.c = String.fromCharCode($mod.i);',
  3377. '$mod.c = "A";',
  3378. '$mod.c = "\n";',
  3379. '$mod.c = "";',
  3380. '']));
  3381. end;
  3382. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3383. begin
  3384. StartProgram(false);
  3385. Add('type');
  3386. Add(' integer = longint;');
  3387. Add(' TYesNo = boolean;');
  3388. Add(' TFloat = double;');
  3389. Add(' TCaption = string;');
  3390. Add(' TChar = AnsiChar;');
  3391. Add('var');
  3392. Add(' i: integer;');
  3393. Add(' b: TYesNo;');
  3394. Add(' d: TFloat;');
  3395. Add(' s: TCaption;');
  3396. Add(' c: TChar;');
  3397. Add('begin');
  3398. Add(' i:=integer(i);');
  3399. Add(' i:=integer(b);');
  3400. Add(' b:=TYesNo(b);');
  3401. Add(' b:=TYesNo(i);');
  3402. Add(' d:=TFloat(d);');
  3403. Add(' d:=TFloat(i);');
  3404. Add(' s:=TCaption(s);');
  3405. Add(' s:=TCaption(c);');
  3406. Add(' c:=TChar(c);');
  3407. ConvertProgram;
  3408. CheckSource('TestAliasTypeRef',
  3409. LinesToStr([ // statements
  3410. 'this.i = 0;',
  3411. 'this.b = false;',
  3412. 'this.d = 0.0;',
  3413. 'this.s = "";',
  3414. 'this.c = "";',
  3415. '']),
  3416. LinesToStr([ // this.$main
  3417. '$mod.i = $mod.i;',
  3418. '$mod.i = ($mod.b ? 1 : 0);',
  3419. '$mod.b = $mod.b;',
  3420. '$mod.b = $mod.i != 0;',
  3421. '$mod.d = $mod.d;',
  3422. '$mod.d = $mod.i;',
  3423. '$mod.s = $mod.s;',
  3424. '$mod.s = $mod.c;',
  3425. '$mod.c = $mod.c;',
  3426. '']));
  3427. end;
  3428. procedure TTestModule.TestEmptyProc;
  3429. begin
  3430. StartProgram(false);
  3431. Add('procedure Test;');
  3432. Add('begin');
  3433. Add('end;');
  3434. Add('begin');
  3435. ConvertProgram;
  3436. CheckSource('TestEmptyProc',
  3437. LinesToStr([ // statements
  3438. 'this.Test = function () {',
  3439. '};'
  3440. ]),
  3441. LinesToStr([ // this.$main
  3442. ''
  3443. ]));
  3444. end;
  3445. procedure TTestModule.TestProcOneParam;
  3446. begin
  3447. StartProgram(false);
  3448. Add('procedure ProcA(i: longint);');
  3449. Add('begin');
  3450. Add('end;');
  3451. Add('begin');
  3452. Add(' PROCA(3);');
  3453. ConvertProgram;
  3454. CheckSource('TestProcOneParam',
  3455. LinesToStr([ // statements
  3456. 'this.ProcA = function (i) {',
  3457. '};'
  3458. ]),
  3459. LinesToStr([ // this.$main
  3460. '$mod.ProcA(3);'
  3461. ]));
  3462. end;
  3463. procedure TTestModule.TestFunctionWithoutParams;
  3464. begin
  3465. StartProgram(false);
  3466. Add('function FuncA: longint;');
  3467. Add('begin');
  3468. Add('end;');
  3469. Add('var i: longint;');
  3470. Add('begin');
  3471. Add(' I:=FUNCA();');
  3472. Add(' I:=FUNCA;');
  3473. Add(' FUNCA();');
  3474. Add(' FUNCA;');
  3475. ConvertProgram;
  3476. CheckSource('TestProcWithoutParams',
  3477. LinesToStr([ // statements
  3478. 'this.FuncA = function () {',
  3479. ' var Result = 0;',
  3480. ' return Result;',
  3481. '};',
  3482. 'this.i=0;'
  3483. ]),
  3484. LinesToStr([ // this.$main
  3485. '$mod.i=$mod.FuncA();',
  3486. '$mod.i=$mod.FuncA();',
  3487. '$mod.FuncA();',
  3488. '$mod.FuncA();'
  3489. ]));
  3490. end;
  3491. procedure TTestModule.TestProcedureWithoutParams;
  3492. begin
  3493. StartProgram(false);
  3494. Add('procedure ProcA;');
  3495. Add('begin');
  3496. Add('end;');
  3497. Add('begin');
  3498. Add(' PROCA();');
  3499. Add(' PROCA;');
  3500. ConvertProgram;
  3501. CheckSource('TestProcWithoutParams',
  3502. LinesToStr([ // statements
  3503. 'this.ProcA = function () {',
  3504. '};'
  3505. ]),
  3506. LinesToStr([ // this.$main
  3507. '$mod.ProcA();',
  3508. '$mod.ProcA();'
  3509. ]));
  3510. end;
  3511. procedure TTestModule.TestIncDec;
  3512. begin
  3513. StartProgram(false);
  3514. Add([
  3515. 'procedure DoIt(var i: longint);',
  3516. 'begin',
  3517. ' inc(i);',
  3518. ' inc(i,2);',
  3519. 'end;',
  3520. 'var',
  3521. ' Bar: longint;',
  3522. 'begin',
  3523. ' inc(bar);',
  3524. ' inc(bar,2);',
  3525. ' dec(bar);',
  3526. ' dec(bar,3);',
  3527. '']);
  3528. ConvertProgram;
  3529. CheckSource('TestIncDec',
  3530. LinesToStr([ // statements
  3531. 'this.DoIt = function (i) {',
  3532. ' i.set(i.get()+1);',
  3533. ' i.set(i.get()+2);',
  3534. '};',
  3535. 'this.Bar = 0;'
  3536. ]),
  3537. LinesToStr([ // this.$main
  3538. '$mod.Bar+=1;',
  3539. '$mod.Bar+=2;',
  3540. '$mod.Bar-=1;',
  3541. '$mod.Bar-=3;'
  3542. ]));
  3543. end;
  3544. procedure TTestModule.TestLoHiFpcMode;
  3545. begin
  3546. StartProgram(false);
  3547. Add([
  3548. '{$mode objfpc}',
  3549. 'const',
  3550. ' LoByte1 = Lo(Word($1234));',
  3551. ' HiByte1 = Hi(Word($1234));',
  3552. ' LoByte2 = Lo(SmallInt($1234));',
  3553. ' HiByte2 = Hi(SmallInt($1234));',
  3554. ' LoWord1 = Lo($1234CDEF);',
  3555. ' HiWord1 = Hi($1234CDEF);',
  3556. ' LoWord2 = Lo(-$1234CDEF);',
  3557. ' HiWord2 = Hi(-$1234CDEF);',
  3558. ' lo4:byte=lo(byte($34));',
  3559. ' hi4:byte=hi(byte($34));',
  3560. ' lo5:byte=lo(shortint(-$34));',
  3561. ' hi5:byte=hi(shortint(-$34));',
  3562. ' lo6:longword=lo($123456789ABCD);',
  3563. ' hi6:longword=hi($123456789ABCD);',
  3564. ' lo7:longword=lo(-$123456789ABCD);',
  3565. ' hi7:longword=hi(-$123456789ABCD);',
  3566. 'var',
  3567. ' b: Byte;',
  3568. ' ss: shortint;',
  3569. ' w: Word;',
  3570. ' si: SmallInt;',
  3571. ' lw: LongWord;',
  3572. ' li: LongInt;',
  3573. ' b2: Byte;',
  3574. ' ni: nativeint;',
  3575. 'begin',
  3576. ' w := $1234;',
  3577. ' ss := -$12;',
  3578. ' b := lo(ss);',
  3579. ' b := HI(ss);',
  3580. ' b := lo(w);',
  3581. ' b := HI(w);',
  3582. ' b2 := lo(b);',
  3583. ' b2 := hi(b);',
  3584. ' lw := $1234CDEF;',
  3585. ' w := lo(lw);',
  3586. ' w := hi(lw);',
  3587. ' ni := $123456789ABCD;',
  3588. ' lw := lo(ni);',
  3589. ' lw := hi(ni);',
  3590. '']);
  3591. ConvertProgram;
  3592. CheckSource('TestLoHiFpcMode',
  3593. LinesToStr([ // statements
  3594. 'this.LoByte1 = 0x1234 & 0xFF;',
  3595. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3596. 'this.LoByte2 = 0x1234 & 0xFF;',
  3597. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3598. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3599. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3600. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3601. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3602. 'this.lo4 = 0x34 & 0xF;',
  3603. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3604. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3605. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3606. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3607. 'this.hi6 = 74565 >>> 0;',
  3608. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3609. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3610. 'this.b = 0;',
  3611. 'this.ss = 0;',
  3612. 'this.w = 0;',
  3613. 'this.si = 0;',
  3614. 'this.lw = 0;',
  3615. 'this.li = 0;',
  3616. 'this.b2 = 0;',
  3617. 'this.ni = 0;',
  3618. '']),
  3619. LinesToStr([ // this.$main
  3620. '$mod.w = 0x1234;',
  3621. '$mod.ss = -0x12;',
  3622. '$mod.b = $mod.ss & 0xFF;',
  3623. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3624. '$mod.b = $mod.w & 0xFF;',
  3625. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3626. '$mod.b2 = $mod.b & 0xF;',
  3627. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3628. '$mod.lw = 0x1234CDEF;',
  3629. '$mod.w = $mod.lw & 0xFFFF;',
  3630. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3631. '$mod.ni = 0x123456789ABCD;',
  3632. '$mod.lw = $mod.ni >>> 0;',
  3633. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3634. '']));
  3635. end;
  3636. procedure TTestModule.TestLoHiDelphiMode;
  3637. begin
  3638. StartProgram(false);
  3639. Add([
  3640. '{$mode delphi}',
  3641. 'const',
  3642. ' LoByte1 = Lo(Word($1234));',
  3643. ' HiByte1 = Hi(Word($1234));',
  3644. ' LoByte2 = Lo(SmallInt($1234));',
  3645. ' HiByte2 = Hi(SmallInt($1234));',
  3646. ' LoByte3 = Lo($1234CDEF);',
  3647. ' HiByte3 = Hi($1234CDEF);',
  3648. ' LoByte4 = Lo(-$1234CDEF);',
  3649. ' HiByte4 = Hi(-$1234CDEF);',
  3650. 'var',
  3651. ' b: Byte;',
  3652. ' w: Word;',
  3653. ' si: SmallInt;',
  3654. ' lw: LongWord;',
  3655. ' li: LongInt;',
  3656. 'begin',
  3657. ' w := $1234;',
  3658. ' b := lo(w);',
  3659. ' b := HI(w);',
  3660. ' lw := $1234CDEF;',
  3661. ' b := lo(lw);',
  3662. ' b := hi(lw);',
  3663. '']);
  3664. ConvertProgram;
  3665. CheckSource('TestLoHiDelphiMode',
  3666. LinesToStr([ // statements
  3667. 'this.LoByte1 = 0x1234 & 0xFF;',
  3668. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3669. 'this.LoByte2 = 0x1234 & 0xFF;',
  3670. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3671. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3672. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3673. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3674. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3675. 'this.b = 0;',
  3676. 'this.w = 0;',
  3677. 'this.si = 0;',
  3678. 'this.lw = 0;',
  3679. 'this.li = 0;'
  3680. ]),
  3681. LinesToStr([ // this.$main
  3682. '$mod.w = 0x1234;',
  3683. '$mod.b = $mod.w & 0xFF;',
  3684. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3685. '$mod.lw = 0x1234CDEF;',
  3686. '$mod.b = $mod.lw & 0xFF;',
  3687. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3688. ]));
  3689. end;
  3690. procedure TTestModule.TestAssignments;
  3691. begin
  3692. StartProgram(false);
  3693. Parser.Options:=Parser.Options+[po_cassignments];
  3694. Add('var');
  3695. Add(' Bar:longint;');
  3696. Add('begin');
  3697. Add(' bar:=3;');
  3698. Add(' bar+=4;');
  3699. Add(' bar-=5;');
  3700. Add(' bar*=6;');
  3701. ConvertProgram;
  3702. CheckSource('TestAssignments',
  3703. LinesToStr([ // statements
  3704. 'this.Bar = 0;'
  3705. ]),
  3706. LinesToStr([ // this.$main
  3707. '$mod.Bar=3;',
  3708. '$mod.Bar+=4;',
  3709. '$mod.Bar-=5;',
  3710. '$mod.Bar*=6;'
  3711. ]));
  3712. end;
  3713. procedure TTestModule.TestArithmeticOperators1;
  3714. begin
  3715. StartProgram(false);
  3716. Add('var');
  3717. Add(' vA,vB,vC:longint;');
  3718. Add('begin');
  3719. Add(' va:=1;');
  3720. Add(' vb:=va+va;');
  3721. Add(' vb:=va div vb;');
  3722. Add(' vb:=va mod vb;');
  3723. Add(' vb:=va+va*vb+va div vb;');
  3724. Add(' vc:=-va;');
  3725. Add(' va:=va-vb;');
  3726. Add(' vb:=va;');
  3727. Add(' if va<vb then vc:=va else vc:=vb;');
  3728. ConvertProgram;
  3729. CheckSource('TestArithmeticOperators1',
  3730. LinesToStr([ // statements
  3731. 'this.vA = 0;',
  3732. 'this.vB = 0;',
  3733. 'this.vC = 0;'
  3734. ]),
  3735. LinesToStr([ // this.$main
  3736. '$mod.vA = 1;',
  3737. '$mod.vB = $mod.vA + $mod.vA;',
  3738. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3739. '$mod.vB = $mod.vA % $mod.vB;',
  3740. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3741. '$mod.vC = -$mod.vA;',
  3742. '$mod.vA = $mod.vA - $mod.vB;',
  3743. '$mod.vB = $mod.vA;',
  3744. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3745. ]));
  3746. end;
  3747. procedure TTestModule.TestMultiAdd;
  3748. begin
  3749. StartProgram(false);
  3750. Add([
  3751. 'function Fly: string; external name ''fly'';',
  3752. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3753. 'var',
  3754. ' Date: double;',
  3755. 'begin',
  3756. ' Result:=(Year>0) and (Year<10000) and',
  3757. ' (Month >= 1) and (Month<=12) and',
  3758. ' (Day>0) and (Day<=31);',
  3759. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3760. 'end;',
  3761. 'var s: string;',
  3762. 'begin',
  3763. ' s:=''a''+''b''+''c''+''d'';',
  3764. ' s:=s+Fly+''e'';',
  3765. ' s:=Fly+Fly+Fly;',
  3766. '']);
  3767. ConvertProgram;
  3768. CheckSource('TestMultiAdd',
  3769. LinesToStr([ // statements
  3770. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3771. ' var Result = false;',
  3772. ' var date = 0.0;',
  3773. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3774. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3775. ' return Result;',
  3776. '};',
  3777. 'this.s = "";',
  3778. '']),
  3779. LinesToStr([ // this.$main
  3780. '$mod.s = "a" + "b" + "c" + "d";',
  3781. '$mod.s = $mod.s + fly() + "e";',
  3782. '$mod.s = fly() + fly() + fly();',
  3783. '']));
  3784. end;
  3785. procedure TTestModule.TestLogicalOperators;
  3786. begin
  3787. StartProgram(false);
  3788. Add('var');
  3789. Add(' vA,vB,vC:boolean;');
  3790. Add('begin');
  3791. Add(' va:=vb and vc;');
  3792. Add(' va:=vb or vc;');
  3793. Add(' va:=vb xor vc;');
  3794. Add(' va:=true and vc;');
  3795. Add(' va:=(vb and vc) or (va and vb);');
  3796. Add(' va:=not vb;');
  3797. ConvertProgram;
  3798. CheckSource('TestLogicalOperators',
  3799. LinesToStr([ // statements
  3800. 'this.vA = false;',
  3801. 'this.vB = false;',
  3802. 'this.vC = false;'
  3803. ]),
  3804. LinesToStr([ // this.$main
  3805. '$mod.vA = $mod.vB && $mod.vC;',
  3806. '$mod.vA = $mod.vB || $mod.vC;',
  3807. '$mod.vA = $mod.vB ^ $mod.vC;',
  3808. '$mod.vA = true && $mod.vC;',
  3809. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3810. '$mod.vA = !$mod.vB;'
  3811. ]));
  3812. end;
  3813. procedure TTestModule.TestBitwiseOperators;
  3814. begin
  3815. StartProgram(false);
  3816. Add([
  3817. 'var',
  3818. ' vA,vB,vC:longint;',
  3819. ' X,Y,Z: nativeint;',
  3820. 'begin',
  3821. ' va:=vb and vc;',
  3822. ' va:=vb or vc;',
  3823. ' va:=vb xor vc;',
  3824. ' va:=vb shl vc;',
  3825. ' va:=vb shr vc;',
  3826. ' va:=3 and vc;',
  3827. ' va:=(vb and vc) or (va and vb);',
  3828. ' va:=not vb;',
  3829. ' X:=Y and Z;',
  3830. ' X:=Y and va;',
  3831. ' X:=Y or Z;',
  3832. ' X:=Y or va;',
  3833. ' X:=Y xor Z;',
  3834. ' X:=Y xor va;',
  3835. '']);
  3836. ConvertProgram;
  3837. CheckSource('TestBitwiseOperators',
  3838. LinesToStr([ // statements
  3839. 'this.vA = 0;',
  3840. 'this.vB = 0;',
  3841. 'this.vC = 0;',
  3842. 'this.X = 0;',
  3843. 'this.Y = 0;',
  3844. 'this.Z = 0;',
  3845. '']),
  3846. LinesToStr([ // this.$main
  3847. '$mod.vA = $mod.vB & $mod.vC;',
  3848. '$mod.vA = $mod.vB | $mod.vC;',
  3849. '$mod.vA = $mod.vB ^ $mod.vC;',
  3850. '$mod.vA = $mod.vB << $mod.vC;',
  3851. '$mod.vA = $mod.vB >>> $mod.vC;',
  3852. '$mod.vA = 3 & $mod.vC;',
  3853. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3854. '$mod.vA = ~$mod.vB;',
  3855. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3856. '$mod.X = $mod.Y & $mod.vA;',
  3857. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3858. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3859. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3860. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3861. '']));
  3862. end;
  3863. procedure TTestModule.TestBitwiseOperatorsLongword;
  3864. begin
  3865. StartProgram(false);
  3866. Add([
  3867. 'var',
  3868. ' a,b,c:longword;',
  3869. ' i: longint;',
  3870. 'begin',
  3871. ' a:=$12345678;',
  3872. ' b:=$EDCBA987;',
  3873. ' c:=not a;',
  3874. ' c:=a and b;',
  3875. ' c:=a and $ffff0000;',
  3876. ' c:=a or b;',
  3877. ' c:=a or $ff00ff00;',
  3878. ' c:=a xor b;',
  3879. ' c:=a xor $f0f0f0f0;',
  3880. ' c:=a shl 1;',
  3881. ' c:=a shl 16;',
  3882. ' c:=a shl 24;',
  3883. ' c:=a shl b;',
  3884. ' c:=a shr 1;',
  3885. ' c:=a shr 16;',
  3886. ' c:=a shr 24;',
  3887. ' c:=a shr b;',
  3888. ' c:=(b and c) or (a and b);',
  3889. ' c:=i and a;',
  3890. ' c:=i or a;',
  3891. ' c:=i xor a;',
  3892. '']);
  3893. ConvertProgram;
  3894. CheckSource('TestBitwiseOperatorsLongword',
  3895. LinesToStr([ // statements
  3896. 'this.a = 0;',
  3897. 'this.b = 0;',
  3898. 'this.c = 0;',
  3899. 'this.i = 0;',
  3900. '']),
  3901. LinesToStr([ // this.$main
  3902. '$mod.a = 0x12345678;',
  3903. '$mod.b = 0xEDCBA987;',
  3904. '$mod.c = rtl.lw(~$mod.a);',
  3905. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3906. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3907. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3908. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3909. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3910. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3911. '$mod.c = rtl.lw($mod.a << 1);',
  3912. '$mod.c = rtl.lw($mod.a << 16);',
  3913. '$mod.c = rtl.lw($mod.a << 24);',
  3914. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3915. '$mod.c = rtl.lw($mod.a >>> 1);',
  3916. '$mod.c = rtl.lw($mod.a >>> 16);',
  3917. '$mod.c = rtl.lw($mod.a >>> 24);',
  3918. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3919. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3920. '$mod.c = $mod.i & $mod.a;',
  3921. '$mod.c = $mod.i | $mod.a;',
  3922. '$mod.c = $mod.i ^ $mod.a;',
  3923. '']));
  3924. end;
  3925. procedure TTestModule.TestPrgProcVar;
  3926. begin
  3927. StartProgram(false);
  3928. Add('procedure Proc1;');
  3929. Add('type');
  3930. Add(' t1=longint;');
  3931. Add('var');
  3932. Add(' vA:t1;');
  3933. Add('begin');
  3934. Add('end;');
  3935. Add('begin');
  3936. ConvertProgram;
  3937. CheckSource('TestPrgProcVar',
  3938. LinesToStr([ // statements
  3939. 'this.Proc1 = function () {',
  3940. ' var vA=0;',
  3941. '};'
  3942. ]),
  3943. LinesToStr([ // this.$main
  3944. ''
  3945. ]));
  3946. end;
  3947. procedure TTestModule.TestUnitProcVar;
  3948. begin
  3949. StartUnit(false);
  3950. Add('interface');
  3951. Add('');
  3952. Add('type tA=string; // unit scope');
  3953. Add('procedure Proc1;');
  3954. Add('');
  3955. Add('implementation');
  3956. Add('');
  3957. Add('procedure Proc1;');
  3958. Add('type tA=longint; // local proc scope');
  3959. Add('var v1:tA; // using local tA');
  3960. Add('begin');
  3961. Add('end;');
  3962. Add('var v2:tA; // using interface tA');
  3963. ConvertUnit;
  3964. CheckSource('TestUnitProcVar',
  3965. LinesToStr([ // statements
  3966. 'var $impl = $mod.$impl;',
  3967. 'this.Proc1 = function () {',
  3968. ' var v1 = 0;',
  3969. '};',
  3970. '']),
  3971. // this.$init
  3972. '',
  3973. // implementation
  3974. LinesToStr([
  3975. '$impl.v2 = "";',
  3976. '']));
  3977. end;
  3978. procedure TTestModule.TestImplProc;
  3979. begin
  3980. StartUnit(false);
  3981. Add('interface');
  3982. Add('');
  3983. Add('procedure Proc1;');
  3984. Add('');
  3985. Add('implementation');
  3986. Add('');
  3987. Add('procedure Proc1; begin end;');
  3988. Add('procedure Proc2; begin end;');
  3989. Add('initialization');
  3990. Add(' Proc1;');
  3991. Add(' Proc2;');
  3992. ConvertUnit;
  3993. CheckSource('TestImplProc',
  3994. LinesToStr([ // statements
  3995. 'var $impl = $mod.$impl;',
  3996. 'this.Proc1 = function () {',
  3997. '};',
  3998. '']),
  3999. LinesToStr([ // this.$init
  4000. '$mod.Proc1();',
  4001. '$impl.Proc2();',
  4002. '']),
  4003. LinesToStr([ // implementation
  4004. '$impl.Proc2 = function () {',
  4005. '};',
  4006. ''])
  4007. );
  4008. end;
  4009. procedure TTestModule.TestFunctionResult;
  4010. begin
  4011. StartProgram(false);
  4012. Add('function Func1: longint;');
  4013. Add('begin');
  4014. Add(' Result:=3;');
  4015. Add(' Func1:=4;');
  4016. Add('end;');
  4017. Add('begin');
  4018. ConvertProgram;
  4019. CheckSource('TestFunctionResult',
  4020. LinesToStr([ // statements
  4021. 'this.Func1 = function () {',
  4022. ' var Result = 0;',
  4023. ' Result = 3;',
  4024. ' Result = 4;',
  4025. ' return Result;',
  4026. '};'
  4027. ]),
  4028. '');
  4029. end;
  4030. procedure TTestModule.TestNestedProc;
  4031. begin
  4032. StartProgram(false);
  4033. Add([
  4034. 'var vInUnit: longint;',
  4035. 'function DoIt(pA,pD: longint): longint;',
  4036. 'var',
  4037. ' vB: longint;',
  4038. ' vC: longint;',
  4039. ' function Nesty(pA: longint): longint; ',
  4040. ' var vB: longint;',
  4041. ' begin',
  4042. ' Result:=pa+vb+vc+pd+vInUnit;',
  4043. ' nesty:=3;',
  4044. ' doit:=4;',
  4045. ' exit;',
  4046. ' end;',
  4047. 'begin',
  4048. ' Result:=pa+vb+vc;',
  4049. ' doit:=6;',
  4050. ' exit;',
  4051. 'end;',
  4052. 'begin']);
  4053. ConvertProgram;
  4054. CheckSource('TestNestedProc',
  4055. LinesToStr([ // statements
  4056. 'this.vInUnit = 0;',
  4057. 'this.DoIt = function (pA, pD) {',
  4058. ' var Result = 0;',
  4059. ' var vB = 0;',
  4060. ' var vC = 0;',
  4061. ' function Nesty(pA) {',
  4062. ' var Result$1 = 0;',
  4063. ' var vB = 0;',
  4064. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4065. ' Result$1 = 3;',
  4066. ' Result = 4;',
  4067. ' return Result$1;',
  4068. ' return Result$1;',
  4069. ' };',
  4070. ' Result = pA + vB + vC;',
  4071. ' Result = 6;',
  4072. ' return Result;',
  4073. ' return Result;',
  4074. '};'
  4075. ]),
  4076. '');
  4077. end;
  4078. procedure TTestModule.TestNestedProc_ResultString;
  4079. begin
  4080. StartProgram(false);
  4081. Add([
  4082. 'function DoIt: string;',
  4083. ' function Nesty: string; ',
  4084. ' begin',
  4085. ' nesty:=#65#66;',
  4086. ' nesty[1]:=#67;',
  4087. ' doit:=#68;',
  4088. ' doit[2]:=#69;',
  4089. ' end;',
  4090. 'begin',
  4091. ' doit:=#70;',
  4092. ' doit[3]:=#71;',
  4093. 'end;',
  4094. 'begin']);
  4095. ConvertProgram;
  4096. CheckSource('TestNestedProc_ResultString',
  4097. LinesToStr([ // statements
  4098. 'this.DoIt = function () {',
  4099. ' var Result = "";',
  4100. ' function Nesty() {',
  4101. ' var Result$1 = "";',
  4102. ' Result$1 = "AB";',
  4103. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4104. ' Result = "D";',
  4105. ' Result = rtl.setCharAt(Result, 1, "E");',
  4106. ' return Result$1;',
  4107. ' };',
  4108. ' Result = "F";',
  4109. ' Result = rtl.setCharAt(Result, 2, "G");',
  4110. ' return Result;',
  4111. '};'
  4112. ]),
  4113. '');
  4114. end;
  4115. procedure TTestModule.TestForwardProc;
  4116. begin
  4117. StartProgram(false);
  4118. Add('procedure FuncA(Bar: longint); forward;');
  4119. Add('procedure FuncB(Bar: longint);');
  4120. Add('begin');
  4121. Add(' funca(bar);');
  4122. Add('end;');
  4123. Add('procedure funca(bar: longint);');
  4124. Add('begin');
  4125. Add(' if bar=3 then ;');
  4126. Add('end;');
  4127. Add('begin');
  4128. Add(' funca(4);');
  4129. Add(' funcb(5);');
  4130. ConvertProgram;
  4131. CheckSource('TestForwardProc',
  4132. LinesToStr([ // statements'
  4133. 'this.FuncB = function (Bar) {',
  4134. ' $mod.FuncA(Bar);',
  4135. '};',
  4136. 'this.FuncA = function (Bar) {',
  4137. ' if (Bar === 3);',
  4138. '};'
  4139. ]),
  4140. LinesToStr([
  4141. '$mod.FuncA(4);',
  4142. '$mod.FuncB(5);'
  4143. ])
  4144. );
  4145. end;
  4146. procedure TTestModule.TestNestedForwardProc;
  4147. begin
  4148. StartProgram(false);
  4149. Add('procedure FuncA;');
  4150. Add(' procedure FuncB(i: longint); forward;');
  4151. Add(' procedure FuncC(i: longint);');
  4152. Add(' begin');
  4153. Add(' funcb(i);');
  4154. Add(' end;');
  4155. Add(' procedure FuncB(i: longint);');
  4156. Add(' begin');
  4157. Add(' if i=3 then ;');
  4158. Add(' end;');
  4159. Add('begin');
  4160. Add(' funcc(4)');
  4161. Add('end;');
  4162. Add('begin');
  4163. Add(' funca;');
  4164. ConvertProgram;
  4165. CheckSource('TestNestedForwardProc',
  4166. LinesToStr([ // statements'
  4167. 'this.FuncA = function () {',
  4168. ' function FuncC(i) {',
  4169. ' FuncB(i);',
  4170. ' };',
  4171. ' function FuncB(i) {',
  4172. ' if (i === 3);',
  4173. ' };',
  4174. ' FuncC(4);',
  4175. '};'
  4176. ]),
  4177. LinesToStr([
  4178. '$mod.FuncA();'
  4179. ])
  4180. );
  4181. end;
  4182. procedure TTestModule.TestAssignFunctionResult;
  4183. begin
  4184. StartProgram(false);
  4185. Add('function Func1: longint;');
  4186. Add('begin');
  4187. Add('end;');
  4188. Add('var i: longint;');
  4189. Add('begin');
  4190. Add(' i:=func1();');
  4191. Add(' i:=func1()+func1();');
  4192. ConvertProgram;
  4193. CheckSource('TestAssignFunctionResult',
  4194. LinesToStr([ // statements
  4195. 'this.Func1 = function () {',
  4196. ' var Result = 0;',
  4197. ' return Result;',
  4198. '};',
  4199. 'this.i = 0;'
  4200. ]),
  4201. LinesToStr([
  4202. '$mod.i = $mod.Func1();',
  4203. '$mod.i = $mod.Func1() + $mod.Func1();'
  4204. ]));
  4205. end;
  4206. procedure TTestModule.TestFunctionResultInCondition;
  4207. begin
  4208. StartProgram(false);
  4209. Add('function Func1: longint;');
  4210. Add('begin');
  4211. Add('end;');
  4212. Add('function Func2: boolean;');
  4213. Add('begin');
  4214. Add('end;');
  4215. Add('var i: longint;');
  4216. Add('begin');
  4217. Add(' if func2 then ;');
  4218. Add(' if i=func1() then ;');
  4219. Add(' if i=func1 then ;');
  4220. ConvertProgram;
  4221. CheckSource('TestFunctionResultInCondition',
  4222. LinesToStr([ // statements
  4223. 'this.Func1 = function () {',
  4224. ' var Result = 0;',
  4225. ' return Result;',
  4226. '};',
  4227. 'this.Func2 = function () {',
  4228. ' var Result = false;',
  4229. ' return Result;',
  4230. '};',
  4231. 'this.i = 0;'
  4232. ]),
  4233. LinesToStr([
  4234. 'if ($mod.Func2());',
  4235. 'if ($mod.i === $mod.Func1());',
  4236. 'if ($mod.i === $mod.Func1());'
  4237. ]));
  4238. end;
  4239. procedure TTestModule.TestFunctionResultInForLoop;
  4240. begin
  4241. StartProgram(false);
  4242. Add([
  4243. 'function Func1(a: array of longint): longint;',
  4244. 'begin',
  4245. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4246. ' for Result in a do if a[Result]=0 then exit;',
  4247. 'end;',
  4248. 'begin',
  4249. ' Func1([1,2,3])']);
  4250. ConvertProgram;
  4251. CheckSource('TestFunctionResultInForLoop',
  4252. LinesToStr([ // statements
  4253. 'this.Func1 = function (a) {',
  4254. ' var Result = 0;',
  4255. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4256. ' Result = $l;',
  4257. ' if (a[Result] === 0) return Result;',
  4258. ' };',
  4259. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4260. ' Result = $in[$l1];',
  4261. ' if (a[Result] === 0) return Result;',
  4262. ' };',
  4263. ' return Result;',
  4264. '};',
  4265. '']),
  4266. LinesToStr([
  4267. '$mod.Func1([1, 2, 3]);'
  4268. ]));
  4269. end;
  4270. procedure TTestModule.TestFunctionResultInTypeCast;
  4271. begin
  4272. StartProgram(false);
  4273. Add([
  4274. 'function GetInt: longint;',
  4275. 'begin',
  4276. 'end;',
  4277. 'begin',
  4278. ' if Byte(GetInt)=0 then ;',
  4279. '']);
  4280. ConvertProgram;
  4281. CheckSource('TestFunctionResultInTypeCast',
  4282. LinesToStr([ // statements
  4283. 'this.GetInt = function () {',
  4284. ' var Result = 0;',
  4285. ' return Result;',
  4286. '};',
  4287. '']),
  4288. LinesToStr([
  4289. 'if (($mod.GetInt() & 255) === 0) ;'
  4290. ]));
  4291. end;
  4292. procedure TTestModule.TestExit;
  4293. begin
  4294. StartProgram(false);
  4295. Add('procedure ProcA;');
  4296. Add('begin');
  4297. Add(' exit;');
  4298. Add('end;');
  4299. Add('function FuncB: longint;');
  4300. Add('begin');
  4301. Add(' exit;');
  4302. Add(' exit(3);');
  4303. Add('end;');
  4304. Add('function FuncC: string;');
  4305. Add('begin');
  4306. Add(' exit;');
  4307. Add(' exit(''a'');');
  4308. Add(' exit(''abc'');');
  4309. Add('end;');
  4310. Add('begin');
  4311. Add(' exit;');
  4312. Add(' exit(1);');
  4313. ConvertProgram;
  4314. CheckSource('TestExit',
  4315. LinesToStr([ // statements
  4316. 'this.ProcA = function () {',
  4317. ' return;',
  4318. '};',
  4319. 'this.FuncB = function () {',
  4320. ' var Result = 0;',
  4321. ' return Result;',
  4322. ' return 3;',
  4323. ' return Result;',
  4324. '};',
  4325. 'this.FuncC = function () {',
  4326. ' var Result = "";',
  4327. ' return Result;',
  4328. ' return "a";',
  4329. ' return "abc";',
  4330. ' return Result;',
  4331. '};'
  4332. ]),
  4333. LinesToStr([
  4334. 'return;',
  4335. 'return 1;',
  4336. '']));
  4337. end;
  4338. procedure TTestModule.TestExit_ResultInFinally;
  4339. begin
  4340. StartProgram(false);
  4341. Add([
  4342. 'function Run: word;',
  4343. 'begin',
  4344. ' try',
  4345. ' exit(3);', // no Result in finally -> use return 3
  4346. ' finally',
  4347. ' end;',
  4348. 'end;',
  4349. 'function Fly: word;',
  4350. 'begin',
  4351. ' try',
  4352. ' exit(3);',
  4353. ' finally',
  4354. ' if Result>0 then ;',
  4355. ' end;',
  4356. 'end;',
  4357. 'function Jump: word;',
  4358. 'begin',
  4359. ' try',
  4360. ' try',
  4361. ' exit(4);',
  4362. ' finally',
  4363. ' end;',
  4364. ' finally',
  4365. ' if Result>0 then ;',
  4366. ' end;',
  4367. 'end;',
  4368. 'begin',
  4369. '']);
  4370. ConvertProgram;
  4371. CheckSource('TestExit_ResultInFinally',
  4372. LinesToStr([ // statements
  4373. 'this.Run = function () {',
  4374. ' var Result = 0;',
  4375. ' try {',
  4376. ' return 3;',
  4377. ' } finally {',
  4378. ' };',
  4379. ' return Result;',
  4380. '};',
  4381. 'this.Fly = function () {',
  4382. ' var Result = 0;',
  4383. ' try {',
  4384. ' Result = 3;',
  4385. ' return Result;',
  4386. ' } finally {',
  4387. ' if (Result > 0) ;',
  4388. ' };',
  4389. ' return Result;',
  4390. '};',
  4391. 'this.Jump = function () {',
  4392. ' var Result = 0;',
  4393. ' try {',
  4394. ' try {',
  4395. ' Result = 4;',
  4396. ' return Result;',
  4397. ' } finally {',
  4398. ' };',
  4399. ' } finally {',
  4400. ' if (Result > 0) ;',
  4401. ' };',
  4402. ' return Result;',
  4403. '};',
  4404. '']),
  4405. LinesToStr([
  4406. '']));
  4407. end;
  4408. procedure TTestModule.TestBreak;
  4409. begin
  4410. StartProgram(false);
  4411. Add([
  4412. 'var',
  4413. ' i: longint;',
  4414. 'begin',
  4415. ' repeat',
  4416. ' break;',
  4417. ' until true;',
  4418. ' while true do',
  4419. ' break;',
  4420. ' for i:=1 to 2 do',
  4421. ' break;']);
  4422. ConvertProgram;
  4423. CheckSource('TestBreak',
  4424. LinesToStr([ // statements
  4425. 'this.i = 0;'
  4426. ]),
  4427. LinesToStr([
  4428. 'do {',
  4429. ' break;',
  4430. '} while (!true);',
  4431. 'while (true) break;',
  4432. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4433. '']));
  4434. end;
  4435. procedure TTestModule.TestBreakAsVar;
  4436. begin
  4437. StartProgram(false);
  4438. Add([
  4439. 'procedure DoIt(break: boolean);',
  4440. 'begin',
  4441. ' if break then ;',
  4442. 'end;',
  4443. 'var',
  4444. ' break: boolean;',
  4445. 'begin',
  4446. ' if break then ;']);
  4447. ConvertProgram;
  4448. CheckSource('TestBreakAsVar',
  4449. LinesToStr([ // statements
  4450. 'this.DoIt = function (Break) {',
  4451. ' if (Break) ;',
  4452. '};',
  4453. 'this.Break = false;',
  4454. '']),
  4455. LinesToStr([
  4456. 'if($mod.Break) ;',
  4457. '']));
  4458. end;
  4459. procedure TTestModule.TestContinue;
  4460. begin
  4461. StartProgram(false);
  4462. Add('var i: longint;');
  4463. Add('begin');
  4464. Add(' repeat');
  4465. Add(' continue;');
  4466. Add(' until true;');
  4467. Add(' while true do');
  4468. Add(' continue;');
  4469. Add(' for i:=1 to 2 do');
  4470. Add(' continue;');
  4471. ConvertProgram;
  4472. CheckSource('TestContinue',
  4473. LinesToStr([ // statements
  4474. 'this.i = 0;'
  4475. ]),
  4476. LinesToStr([
  4477. 'do {',
  4478. ' continue;',
  4479. '} while (!true);',
  4480. 'while (true) continue;',
  4481. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4482. '']));
  4483. end;
  4484. procedure TTestModule.TestProc_External;
  4485. begin
  4486. StartProgram(false);
  4487. Add('procedure Foo; external name ''console.log'';');
  4488. Add('function Bar: longint; external name ''get.item'';');
  4489. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4490. Add('var');
  4491. Add(' i: longint;');
  4492. Add('begin');
  4493. Add(' Foo;');
  4494. Add(' i:=Bar;');
  4495. Add(' i:=Bla(''abc'');');
  4496. ConvertProgram;
  4497. CheckSource('TestProc_External',
  4498. LinesToStr([ // statements
  4499. 'this.i = 0;'
  4500. ]),
  4501. LinesToStr([
  4502. 'console.log();',
  4503. '$mod.i = get.item();',
  4504. '$mod.i = apply.something("abc");'
  4505. ]));
  4506. end;
  4507. procedure TTestModule.TestProc_ExternalOtherUnit;
  4508. begin
  4509. AddModuleWithIntfImplSrc('unit2.pas',
  4510. LinesToStr([
  4511. 'procedure Now; external name ''Date.now'';',
  4512. 'procedure DoIt;'
  4513. ]),
  4514. 'procedure doit; begin end;');
  4515. StartUnit(true);
  4516. Add('interface');
  4517. Add('uses unit2;');
  4518. Add('implementation');
  4519. Add('begin');
  4520. Add(' now;');
  4521. Add(' now();');
  4522. Add(' uNit2.now;');
  4523. Add(' uNit2.now();');
  4524. Add(' doit;');
  4525. Add(' uNit2.doit;');
  4526. ConvertUnit;
  4527. CheckSource('TestProc_ExternalOtherUnit',
  4528. LinesToStr([
  4529. '']),
  4530. LinesToStr([
  4531. 'Date.now();',
  4532. 'Date.now();',
  4533. 'Date.now();',
  4534. 'Date.now();',
  4535. 'pas.unit2.DoIt();',
  4536. 'pas.unit2.DoIt();',
  4537. '']));
  4538. end;
  4539. procedure TTestModule.TestProc_Asm;
  4540. begin
  4541. StartProgram(false);
  4542. Add([
  4543. '{$mode delphi}',
  4544. 'function DoIt: longint;',
  4545. 'begin;',
  4546. ' asm',
  4547. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4548. ' end;',
  4549. ' asm console.log(); end;',
  4550. ' asm',
  4551. ' s = "'' ";',
  4552. ' s = ''" '';',
  4553. ' s = s + "world" + "''";',
  4554. ' // end',
  4555. ' s = ''end'';',
  4556. ' s = "end";',
  4557. ' s = "foo\"bar";',
  4558. ' s = ''a\''b'';',
  4559. ' s = `${expr}\`-"-''-`;',
  4560. ' s = `multi',
  4561. 'line`;',
  4562. ' end;',
  4563. 'end;',
  4564. 'procedure Fly;',
  4565. 'asm',
  4566. ' return;',
  4567. 'end;',
  4568. 'begin']);
  4569. ConvertProgram;
  4570. CheckSource('TestProc_Asm',
  4571. LinesToStr([ // statements
  4572. 'this.DoIt = function () {',
  4573. ' var Result = 0;',
  4574. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4575. ' console.log();',
  4576. ' s = "'' ";',
  4577. ' s = ''" '';',
  4578. ' s = s + "world" + "''";',
  4579. ' // end',
  4580. ' s = ''end'';',
  4581. ' s = "end";',
  4582. ' s = "foo\"bar";',
  4583. ' s = ''a\''b'';',
  4584. ' s = `${expr}\`-"-''-`;',
  4585. ' s = `multi',
  4586. 'line`;',
  4587. ' return Result;',
  4588. '};',
  4589. 'this.Fly = function () {',
  4590. ' return;',
  4591. '};',
  4592. '']),
  4593. LinesToStr([
  4594. ''
  4595. ]));
  4596. end;
  4597. procedure TTestModule.TestProc_AsmSubBlock;
  4598. begin
  4599. StartProgram(true,[supTObject]);
  4600. Add([
  4601. '{$mode delphi}',
  4602. 'type',
  4603. ' TBird = class end;',
  4604. 'procedure Run(w: word);',
  4605. 'begin;',
  4606. ' if true then asm console.log(); end;',
  4607. ' if w>3 then asm',
  4608. ' var a = w+1;',
  4609. ' w = a+3;',
  4610. ' end;',
  4611. ' while (w>7) do asm',
  4612. ' w+=3; w*=2;',
  4613. ' end;',
  4614. ' try',
  4615. ' except',
  4616. ' on E: TBird do',
  4617. ' asm console.log(E); end;',
  4618. ' on E: TObject do',
  4619. ' asm var i=3; i--; end;',
  4620. ' else asm Fly; High; end;',
  4621. ' end;',
  4622. 'end;',
  4623. 'begin']);
  4624. ConvertProgram;
  4625. CheckSource('TestProc_AsmSubBlock',
  4626. LinesToStr([ // statements
  4627. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4628. '});',
  4629. 'this.Run = function (w) {',
  4630. ' if (true) console.log();',
  4631. ' if (w > 3) {',
  4632. ' var a = w+1;',
  4633. ' w = a+3;',
  4634. ' };',
  4635. ' while (w > 7) {',
  4636. ' w+=3; w*=2;',
  4637. ' };',
  4638. ' try {} catch ($e) {',
  4639. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4640. ' var E = $e;',
  4641. ' console.log(E);',
  4642. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4643. ' var E = $e;',
  4644. ' var i=3; i--;',
  4645. ' } else {',
  4646. ' Fly; High;',
  4647. ' }',
  4648. ' };',
  4649. '};',
  4650. '']),
  4651. LinesToStr([
  4652. ''
  4653. ]));
  4654. end;
  4655. procedure TTestModule.TestProc_Assembler;
  4656. begin
  4657. StartProgram(false);
  4658. Add('function DoIt: longint; assembler;');
  4659. Add('asm');
  4660. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4661. Add('end;');
  4662. Add('begin');
  4663. ConvertProgram;
  4664. CheckSource('TestProc_Assembler',
  4665. LinesToStr([ // statements
  4666. 'this.DoIt = function () {',
  4667. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4668. '};'
  4669. ]),
  4670. LinesToStr([
  4671. ''
  4672. ]));
  4673. end;
  4674. procedure TTestModule.TestProc_VarParam;
  4675. begin
  4676. StartProgram(false);
  4677. Add('type integer = longint;');
  4678. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4679. Add('var vJ: integer;');
  4680. Add('begin');
  4681. Add(' vg:=vg+1;');
  4682. Add(' vj:=vh+2;');
  4683. Add(' vi:=vi+3;');
  4684. Add(' doit(vg,vg,vg);');
  4685. Add(' doit(vh,vh,vj);');
  4686. Add(' doit(vi,vi,vi);');
  4687. Add(' doit(vj,vj,vj);');
  4688. Add('end;');
  4689. Add('var i: integer;');
  4690. Add('begin');
  4691. Add(' doit(i,i,i);');
  4692. ConvertProgram;
  4693. CheckSource('TestProc_VarParam',
  4694. LinesToStr([ // statements
  4695. 'this.DoIt = function (vG,vH,vI) {',
  4696. ' var vJ = 0;',
  4697. ' vG = vG + 1;',
  4698. ' vJ = vH + 2;',
  4699. ' vI.set(vI.get()+3);',
  4700. ' $mod.DoIt(vG, vG, {',
  4701. ' get: function () {',
  4702. ' return vG;',
  4703. ' },',
  4704. ' set: function (v) {',
  4705. ' vG = v;',
  4706. ' }',
  4707. ' });',
  4708. ' $mod.DoIt(vH, vH, {',
  4709. ' get: function () {',
  4710. ' return vJ;',
  4711. ' },',
  4712. ' set: function (v) {',
  4713. ' vJ = v;',
  4714. ' }',
  4715. ' });',
  4716. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4717. ' $mod.DoIt(vJ, vJ, {',
  4718. ' get: function () {',
  4719. ' return vJ;',
  4720. ' },',
  4721. ' set: function (v) {',
  4722. ' vJ = v;',
  4723. ' }',
  4724. ' });',
  4725. '};',
  4726. 'this.i = 0;'
  4727. ]),
  4728. LinesToStr([
  4729. '$mod.DoIt($mod.i,$mod.i,{',
  4730. ' p: $mod,',
  4731. ' get: function () {',
  4732. ' return this.p.i;',
  4733. ' },',
  4734. ' set: function (v) {',
  4735. ' this.p.i = v;',
  4736. ' }',
  4737. '});'
  4738. ]));
  4739. end;
  4740. procedure TTestModule.TestProc_VarParamString;
  4741. begin
  4742. StartProgram(false);
  4743. Add(['type TCaption = string;',
  4744. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4745. 'var c: AnsiChar;',
  4746. 'begin',
  4747. ' va[1]:=c;',
  4748. ' vb[2]:=c;',
  4749. ' vc[3]:=c;',
  4750. 'end;',
  4751. 'begin']);
  4752. ConvertProgram;
  4753. CheckSource('TestProc_VarParamString',
  4754. LinesToStr([ // statements
  4755. 'this.DoIt = function (vA,vB,vC) {',
  4756. ' var c = "";',
  4757. ' vA = rtl.setCharAt(vA, 0, c);',
  4758. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4759. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4760. '};',
  4761. '']),
  4762. LinesToStr([
  4763. ]));
  4764. end;
  4765. procedure TTestModule.TestProc_VarParamV;
  4766. begin
  4767. StartProgram(false);
  4768. Add([
  4769. 'procedure Inc2(var i: longint);',
  4770. 'begin',
  4771. ' i:=i+2;',
  4772. 'end;',
  4773. 'procedure DoIt(v: longint);',
  4774. 'var p: array of longint;',
  4775. 'begin',
  4776. ' Inc2(v);',
  4777. ' Inc2(p[v]);',
  4778. 'end;',
  4779. 'begin']);
  4780. ConvertProgram;
  4781. CheckSource('TestProc_VarParamV',
  4782. LinesToStr([ // statements
  4783. 'this.Inc2 = function (i) {',
  4784. ' i.set(i.get()+2);',
  4785. '};',
  4786. 'this.DoIt = function (v) {',
  4787. ' var p = [];',
  4788. ' $mod.Inc2({get: function () {',
  4789. ' return v;',
  4790. ' }, set: function (w) {',
  4791. ' v = w;',
  4792. ' }});',
  4793. ' $mod.Inc2({',
  4794. ' a: v,',
  4795. ' p: p,',
  4796. ' get: function () {',
  4797. ' return this.p[this.a];',
  4798. ' },',
  4799. ' set: function (v) {',
  4800. ' this.p[this.a] = v;',
  4801. ' }',
  4802. ' });',
  4803. '};',
  4804. '']),
  4805. LinesToStr([
  4806. '']));
  4807. end;
  4808. procedure TTestModule.TestProc_Overload;
  4809. begin
  4810. StartProgram(false);
  4811. Add('procedure DoIt(vI: longint); begin end;');
  4812. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4813. Add('procedure DoIt(vD: double); begin end;');
  4814. Add('begin');
  4815. Add(' DoIt(1);');
  4816. Add(' DoIt(2,3);');
  4817. Add(' DoIt(4.5);');
  4818. ConvertProgram;
  4819. CheckSource('TestProcedureOverload',
  4820. LinesToStr([ // statements
  4821. 'this.DoIt = function (vI) {',
  4822. '};',
  4823. 'this.DoIt$1 = function (vI, vJ) {',
  4824. '};',
  4825. 'this.DoIt$2 = function (vD) {',
  4826. '};',
  4827. '']),
  4828. LinesToStr([
  4829. '$mod.DoIt(1);',
  4830. '$mod.DoIt$1(2, 3);',
  4831. '$mod.DoIt$2(4.5);',
  4832. '']));
  4833. end;
  4834. procedure TTestModule.TestProc_OverloadForward;
  4835. begin
  4836. StartProgram(false);
  4837. Add('procedure DoIt(vI: longint); forward;');
  4838. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4839. Add('procedure doit(vi: longint); begin end;');
  4840. Add('begin');
  4841. Add(' doit(1);');
  4842. Add(' doit(2,3);');
  4843. ConvertProgram;
  4844. CheckSource('TestProcedureOverloadForward',
  4845. LinesToStr([ // statements
  4846. 'this.DoIt$1 = function (vI, vJ) {',
  4847. '};',
  4848. 'this.DoIt = function (vI) {',
  4849. '};',
  4850. '']),
  4851. LinesToStr([
  4852. '$mod.DoIt(1);',
  4853. '$mod.DoIt$1(2, 3);',
  4854. '']));
  4855. end;
  4856. procedure TTestModule.TestProc_OverloadIntfImpl;
  4857. begin
  4858. StartUnit(false);
  4859. Add('interface');
  4860. Add('procedure DoIt(vI: longint);');
  4861. Add('procedure DoIt(vI, vJ: longint);');
  4862. Add('implementation');
  4863. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4864. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4865. Add('procedure DoIt(vi: longint); begin end;');
  4866. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4867. Add('procedure DoIt(vi, vj: longint); begin end;');
  4868. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4869. Add('begin');
  4870. Add(' doit(1);');
  4871. Add(' doit(2,3);');
  4872. Add(' doit(4,5,6);');
  4873. Add(' doit(7,8,9,10);');
  4874. Add(' doit(11,12,13,14,15);');
  4875. ConvertUnit;
  4876. CheckSource('TestProcedureOverloadUnit',
  4877. LinesToStr([ // statements
  4878. 'var $impl = $mod.$impl;',
  4879. 'this.DoIt = function (vI) {',
  4880. '};',
  4881. 'this.DoIt$1 = function (vI, vJ) {',
  4882. '};',
  4883. '']),
  4884. LinesToStr([ // this.$init
  4885. '$mod.DoIt(1);',
  4886. '$mod.DoIt$1(2, 3);',
  4887. '$impl.DoIt$3(4,5,6);',
  4888. '$impl.DoIt$4(7,8,9,10);',
  4889. '$impl.DoIt$2(11,12,13,14,15);',
  4890. '']),
  4891. LinesToStr([ // implementation
  4892. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4893. '};',
  4894. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4895. '};',
  4896. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4897. '};',
  4898. '']));
  4899. end;
  4900. procedure TTestModule.TestProc_OverloadNested;
  4901. begin
  4902. StartProgram(false);
  4903. Add([
  4904. 'procedure doit(vA: longint);',
  4905. ' procedure DoIt(vA, vB: longint); overload;',
  4906. ' begin',
  4907. ' doit(1);',
  4908. ' doit(1,2);',
  4909. ' end;',
  4910. ' procedure doit(vA, vB, vC: longint);',
  4911. ' begin',
  4912. ' doit(1);',
  4913. ' doit(1,2);',
  4914. ' doit(1,2,3);',
  4915. ' end;',
  4916. 'begin',
  4917. ' doit(1);',
  4918. ' doit(1,2);',
  4919. ' doit(1,2,3);',
  4920. 'end;',
  4921. 'begin // main',
  4922. ' doit(1);']);
  4923. ConvertProgram;
  4924. CheckSource('TestProcedureOverloadNested',
  4925. LinesToStr([ // statements
  4926. 'this.doit = function (vA) {',
  4927. ' function DoIt$1(vA, vB) {',
  4928. ' $mod.doit(1);',
  4929. ' DoIt$1(1, 2);',
  4930. ' };',
  4931. ' function doit$2(vA, vB, vC) {',
  4932. ' $mod.doit(1);',
  4933. ' DoIt$1(1, 2);',
  4934. ' doit$2(1, 2, 3);',
  4935. ' };',
  4936. ' $mod.doit(1);',
  4937. ' DoIt$1(1, 2);',
  4938. ' doit$2(1, 2, 3);',
  4939. '};',
  4940. '']),
  4941. LinesToStr([
  4942. '$mod.doit(1);',
  4943. '']));
  4944. end;
  4945. procedure TTestModule.TestProc_OverloadNestedForward;
  4946. begin
  4947. StartProgram(false);
  4948. Add([
  4949. 'procedure DoIt(vA: longint); overload; forward;',
  4950. 'procedure DoIt(vB, vC: longint); overload;',
  4951. 'begin // 2 param overload',
  4952. ' doit(1);',
  4953. ' doit(1,2);',
  4954. 'end;',
  4955. 'procedure doit(vA: longint);',
  4956. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4957. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4958. ' begin // 4 param overload',
  4959. ' doit(1);',
  4960. ' doit(1,2);',
  4961. ' doit(1,2,3);',
  4962. ' doit(1,2,3,4);',
  4963. ' end;',
  4964. ' procedure doit(vA, vB, vC: longint);',
  4965. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4966. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4967. ' begin // 6 param overload',
  4968. ' doit(1);',
  4969. ' doit(1,2);',
  4970. ' doit(1,2,3);',
  4971. ' doit(1,2,3,4);',
  4972. ' doit(1,2,3,4,5);',
  4973. ' doit(1,2,3,4,5,6);',
  4974. ' end;',
  4975. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4976. ' begin // 5 param overload',
  4977. ' doit(1);',
  4978. ' doit(1,2);',
  4979. ' doit(1,2,3);',
  4980. ' doit(1,2,3,4);',
  4981. ' doit(1,2,3,4,5);',
  4982. ' doit(1,2,3,4,5,6);',
  4983. ' end;',
  4984. ' begin // 3 param overload',
  4985. ' doit(1);',
  4986. ' doit(1,2);',
  4987. ' doit(1,2,3);',
  4988. ' doit(1,2,3,4);',
  4989. ' doit(1,2,3,4,5);',
  4990. ' doit(1,2,3,4,5,6);',
  4991. ' end;',
  4992. 'begin // 1 param overload',
  4993. ' doit(1);',
  4994. ' doit(1,2);',
  4995. ' doit(1,2,3);',
  4996. ' doit(1,2,3,4);',
  4997. 'end;',
  4998. 'begin // main',
  4999. ' doit(1);',
  5000. ' doit(1,2);']);
  5001. ConvertProgram;
  5002. CheckSource('TestProc_OverloadNestedForward',
  5003. LinesToStr([ // statements
  5004. 'this.DoIt$1 = function (vB, vC) {',
  5005. ' $mod.DoIt(1);',
  5006. ' $mod.DoIt$1(1, 2);',
  5007. '};',
  5008. 'this.DoIt = function (vA) {',
  5009. ' function DoIt$3(vA, vB, vC, vD) {',
  5010. ' $mod.DoIt(1);',
  5011. ' $mod.DoIt$1(1, 2);',
  5012. ' DoIt$2(1, 2, 3);',
  5013. ' DoIt$3(1, 2, 3, 4);',
  5014. ' };',
  5015. ' function DoIt$2(vA, vB, vC) {',
  5016. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5017. ' $mod.DoIt(1);',
  5018. ' $mod.DoIt$1(1, 2);',
  5019. ' DoIt$2(1, 2, 3);',
  5020. ' DoIt$3(1, 2, 3, 4);',
  5021. ' DoIt$4(1, 2, 3, 4, 5);',
  5022. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5023. ' };',
  5024. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5025. ' $mod.DoIt(1);',
  5026. ' $mod.DoIt$1(1, 2);',
  5027. ' DoIt$2(1, 2, 3);',
  5028. ' DoIt$3(1, 2, 3, 4);',
  5029. ' DoIt$4(1, 2, 3, 4, 5);',
  5030. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5031. ' };',
  5032. ' $mod.DoIt(1);',
  5033. ' $mod.DoIt$1(1, 2);',
  5034. ' DoIt$2(1, 2, 3);',
  5035. ' DoIt$3(1, 2, 3, 4);',
  5036. ' DoIt$4(1, 2, 3, 4, 5);',
  5037. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5038. ' };',
  5039. ' $mod.DoIt(1);',
  5040. ' $mod.DoIt$1(1, 2);',
  5041. ' DoIt$2(1, 2, 3);',
  5042. ' DoIt$3(1, 2, 3, 4);',
  5043. '};',
  5044. '']),
  5045. LinesToStr([
  5046. '$mod.DoIt(1);',
  5047. '$mod.DoIt$1(1, 2);',
  5048. '']));
  5049. end;
  5050. procedure TTestModule.TestProc_OverloadUnitCycle;
  5051. begin
  5052. AddModuleWithIntfImplSrc('Unit2.pas',
  5053. LinesToStr([
  5054. 'type',
  5055. ' TObject = class',
  5056. ' procedure DoIt(b: boolean); virtual; abstract;',
  5057. ' procedure DoIt(i: longint); virtual; abstract;',
  5058. ' end;',
  5059. '']),
  5060. 'uses test1;');
  5061. StartUnit(true);
  5062. Add([
  5063. 'interface',
  5064. 'uses unit2;',
  5065. 'type',
  5066. ' TEagle = class(TObject)',
  5067. ' procedure DoIt(b: boolean); override;',
  5068. ' procedure DoIt(i: longint); override;',
  5069. ' end;',
  5070. 'implementation',
  5071. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5072. 'procedure TEagle.DoIt(i: longint); begin end;',
  5073. '']);
  5074. ConvertUnit;
  5075. CheckSource('TestProc_OverloadUnitCycle',
  5076. LinesToStr([ // statements
  5077. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5078. ' this.DoIt = function (b) {',
  5079. ' };',
  5080. ' this.DoIt$1 = function (i) {',
  5081. ' };',
  5082. '});',
  5083. '']),
  5084. '',
  5085. LinesToStr([
  5086. '']));
  5087. end;
  5088. procedure TTestModule.TestProc_Varargs;
  5089. begin
  5090. StartProgram(false);
  5091. Add([
  5092. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5093. 'procedure ProcB; varargs; external name ''ProcB'';',
  5094. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5095. 'function GetIt: longint; begin end;',
  5096. 'begin',
  5097. ' ProcA(1);',
  5098. ' ProcA(1,2);',
  5099. ' ProcA(1,2.0);',
  5100. ' ProcA(1,2,3);',
  5101. ' ProcA(1,''2'');',
  5102. ' ProcA(2,'''');',
  5103. ' ProcA(3,false);',
  5104. ' ProcB;',
  5105. ' ProcB();',
  5106. ' ProcB(4);',
  5107. ' ProcB(''foo'');',
  5108. ' ProcC;',
  5109. ' ProcC();',
  5110. ' ProcC(4);',
  5111. ' ProcC(5,''foo'');',
  5112. ' ProcB(GetIt);',
  5113. ' ProcB(GetIt());',
  5114. ' ProcB(GetIt,GetIt());']);
  5115. ConvertProgram;
  5116. CheckSource('TestProc_Varargs',
  5117. LinesToStr([ // statements
  5118. 'this.GetIt = function () {',
  5119. ' var Result = 0;',
  5120. ' return Result;',
  5121. '};',
  5122. '']),
  5123. LinesToStr([
  5124. 'ProcA(1);',
  5125. 'ProcA(1, 2);',
  5126. 'ProcA(1, 2.0);',
  5127. 'ProcA(1, 2, 3);',
  5128. 'ProcA(1, "2");',
  5129. 'ProcA(2, "");',
  5130. 'ProcA(3, false);',
  5131. 'ProcB();',
  5132. 'ProcB();',
  5133. 'ProcB(4);',
  5134. 'ProcB("foo");',
  5135. 'ProcC(17);',
  5136. 'ProcC(17);',
  5137. 'ProcC(4);',
  5138. 'ProcC(5, "foo");',
  5139. 'ProcB($mod.GetIt());',
  5140. 'ProcB($mod.GetIt());',
  5141. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5142. '']));
  5143. end;
  5144. procedure TTestModule.TestProc_ConstOrder;
  5145. begin
  5146. StartProgram(false);
  5147. Add([
  5148. 'const A = 3;',
  5149. 'const B = A+1;',
  5150. 'procedure DoIt;',
  5151. 'const C = A+1;',
  5152. 'const D = B+1;',
  5153. 'const E = D+C+B+A;',
  5154. 'begin',
  5155. 'end;',
  5156. 'begin'
  5157. ]);
  5158. ConvertProgram;
  5159. CheckSource('TestProc_ConstOrder',
  5160. LinesToStr([ // statements
  5161. 'this.A = 3;',
  5162. 'this.B = 3 + 1;',
  5163. 'var C = 3 + 1;',
  5164. 'var D = 4 + 1;',
  5165. 'var E = 5 + 4 + 4 + 3;',
  5166. 'this.DoIt = function () {',
  5167. '};',
  5168. '']),
  5169. LinesToStr([
  5170. ''
  5171. ]));
  5172. end;
  5173. procedure TTestModule.TestProc_DuplicateConst;
  5174. begin
  5175. StartProgram(false);
  5176. Add([
  5177. 'const A = 1;',
  5178. 'procedure DoIt;',
  5179. 'const A = 2;',
  5180. ' procedure SubIt;',
  5181. ' const A = 21;',
  5182. ' begin',
  5183. ' end;',
  5184. 'begin',
  5185. 'end;',
  5186. 'procedure DoSome;',
  5187. 'const A = 3;',
  5188. 'begin',
  5189. 'end;',
  5190. 'begin'
  5191. ]);
  5192. ConvertProgram;
  5193. CheckSource('TestProc_DuplicateConst',
  5194. LinesToStr([ // statements
  5195. 'this.A = 1;',
  5196. 'var A$1 = 2;',
  5197. 'var A$2 = 21;',
  5198. 'this.DoIt = function () {',
  5199. ' function SubIt() {',
  5200. ' };',
  5201. '};',
  5202. 'var A$3 = 3;',
  5203. 'this.DoSome = function () {',
  5204. '};',
  5205. '']),
  5206. LinesToStr([
  5207. ''
  5208. ]));
  5209. end;
  5210. procedure TTestModule.TestProc_LocalVarAbsolute;
  5211. begin
  5212. StartProgram(false);
  5213. Add([
  5214. 'type',
  5215. ' TObject = class',
  5216. ' Index: longint;',
  5217. ' procedure DoAbs(Item: pointer);',
  5218. ' end;',
  5219. 'procedure TObject.DoAbs(Item: pointer);',
  5220. 'var',
  5221. ' o: TObject absolute Item;',
  5222. 'begin',
  5223. ' if o.Index<o.Index then o.Index:=o.Index;',
  5224. 'end;',
  5225. 'procedure DoIt(i: longint; p: pointer);',
  5226. 'var',
  5227. ' d: double absolute i;',
  5228. ' s: string absolute d;',
  5229. ' oi: TObject absolute i;',
  5230. ' op: TObject absolute p;',
  5231. 'begin',
  5232. ' if d=d then d:=d;',
  5233. ' if s=s then s:=s;',
  5234. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5235. ' if op.Index=op.Index then op.Index:=op.Index;',
  5236. 'end;',
  5237. 'begin']);
  5238. ConvertProgram;
  5239. CheckSource('TestProc_LocalVarAbsolute',
  5240. LinesToStr([ // statements
  5241. 'rtl.createClass(this, "TObject", null, function () {',
  5242. ' this.$init = function () {',
  5243. ' this.Index = 0;',
  5244. ' };',
  5245. ' this.$final = function () {',
  5246. ' };',
  5247. ' this.DoAbs = function (Item) {',
  5248. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5249. ' };',
  5250. '});',
  5251. 'this.DoIt = function (i, p) {',
  5252. ' if (i === i) i = i;',
  5253. ' if (i === i) i = i;',
  5254. ' if (i.Index < i.Index) i.Index = i.Index;',
  5255. ' if (p.Index === p.Index) p.Index = p.Index;',
  5256. '};'
  5257. ]),
  5258. LinesToStr([
  5259. ]));
  5260. end;
  5261. procedure TTestModule.TestProc_ResultAbsolute;
  5262. begin
  5263. StartProgram(false);
  5264. Add([
  5265. 'type',
  5266. ' TObject = class',
  5267. ' Index: longint;',
  5268. ' function DoAbs: pointer;',
  5269. ' end;',
  5270. 'function TObject.DoAbs: pointer;',
  5271. 'var',
  5272. ' o: TObject absolute Result;',
  5273. 'begin',
  5274. ' if o.Index<o.Index then o.Index:=o.Index;',
  5275. 'end;',
  5276. 'function DoIt: jsvalue;',
  5277. 'var',
  5278. ' d: double absolute Result;',
  5279. ' s: string absolute Result;',
  5280. ' o: TObject absolute Result;',
  5281. 'begin',
  5282. ' if d=d then d:=d;',
  5283. ' if s=s then s:=s;',
  5284. ' if o.Index<o.Index then o.Index:=o.Index;',
  5285. 'end;',
  5286. 'begin']);
  5287. ConvertProgram;
  5288. CheckSource('TestProc_ResultAbsolute',
  5289. LinesToStr([ // statements
  5290. 'rtl.createClass(this, "TObject", null, function () {',
  5291. ' this.$init = function () {',
  5292. ' this.Index = 0;',
  5293. ' };',
  5294. ' this.$final = function () {',
  5295. ' };',
  5296. ' this.DoAbs = function () {',
  5297. ' var Result = null;',
  5298. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5299. ' return Result;',
  5300. ' };',
  5301. '});',
  5302. 'this.DoIt = function () {',
  5303. ' var Result = undefined;',
  5304. ' if (Result === Result) Result = Result;',
  5305. ' if (Result === Result) Result = Result;',
  5306. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5307. ' return Result;',
  5308. '};',
  5309. '']),
  5310. LinesToStr([
  5311. ]));
  5312. end;
  5313. procedure TTestModule.TestProc_LocalVarInit;
  5314. begin
  5315. StartProgram(false);
  5316. Add([
  5317. 'type TBytes = array of byte;',
  5318. 'procedure DoIt;',
  5319. 'const c = 4;',
  5320. 'var',
  5321. ' b: byte = 1;',
  5322. ' w: word = 2+c;',
  5323. ' p: pointer = nil;',
  5324. ' Buffer: TBytes = nil;',
  5325. 'begin',
  5326. 'end;',
  5327. 'begin']);
  5328. ConvertProgram;
  5329. CheckSource('TestProc_LocalVarInit',
  5330. LinesToStr([ // statements
  5331. 'var c = 4;',
  5332. 'this.DoIt = function () {',
  5333. ' var b = 1;',
  5334. ' var w = 2 + 4;',
  5335. ' var p = null;',
  5336. ' var Buffer = [];',
  5337. '};',
  5338. '']),
  5339. LinesToStr([
  5340. ]));
  5341. end;
  5342. procedure TTestModule.TestProc_ReservedWords;
  5343. begin
  5344. StartProgram(false);
  5345. Add([
  5346. 'procedure Date(ArrayBuffer: longint);',
  5347. 'const',
  5348. ' NaN: longint = 3;',
  5349. 'var',
  5350. ' &Boolean: longint;',
  5351. ' procedure Error(ArrayBuffer: longint);',
  5352. ' begin',
  5353. ' end;',
  5354. 'begin',
  5355. ' Nan:=&bOolean;',
  5356. 'end;',
  5357. 'begin',
  5358. ' Date(1);']);
  5359. ConvertProgram;
  5360. CheckSource('TestProc_ReservedWords',
  5361. LinesToStr([ // statements
  5362. 'var naN = 3;',
  5363. 'this.Date = function (arrayBuffer) {',
  5364. ' var boolean = 0;',
  5365. ' function error(arrayBuffer) {',
  5366. ' };',
  5367. ' naN = boolean;',
  5368. '};',
  5369. '']),
  5370. LinesToStr([
  5371. ' $mod.Date(1);'
  5372. ]));
  5373. end;
  5374. procedure TTestModule.TestProc_ConstRefWord;
  5375. begin
  5376. StartProgram(false);
  5377. Add([
  5378. 'procedure Run(constref w: word);',
  5379. 'var l: word;',
  5380. 'begin',
  5381. ' l:=w;',
  5382. ' Run(w);',
  5383. ' Run(l);',
  5384. 'end;',
  5385. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5386. 'begin',
  5387. ' Run(a);',
  5388. ' Run(b);',
  5389. ' Run(c);',
  5390. ' Run(d);',
  5391. ' Run(e);',
  5392. 'end;',
  5393. 'begin',
  5394. ' Run(1);']);
  5395. ConvertProgram;
  5396. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5397. CheckSource('TestProc_ConstRefWord',
  5398. LinesToStr([ // statements
  5399. 'this.Run = function (w) {',
  5400. ' var l = 0;',
  5401. ' l = w;',
  5402. ' $mod.Run(w);',
  5403. ' $mod.Run(l);',
  5404. '};',
  5405. 'this.Fly = function (a, b, c, d, e) {',
  5406. ' $mod.Run(a);',
  5407. ' $mod.Run(b.get());',
  5408. ' $mod.Run(c.get());',
  5409. ' $mod.Run(d);',
  5410. ' $mod.Run(e);',
  5411. '};',
  5412. '']),
  5413. LinesToStr([
  5414. '$mod.Run(1);'
  5415. ]));
  5416. end;
  5417. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5418. begin
  5419. StartProgram(false);
  5420. Add([
  5421. '{$mode objfpc}',
  5422. 'type',
  5423. ' TFunc = reference to function(x: word): word;',
  5424. 'var Func: TFunc;',
  5425. 'procedure DoIt(a: word);',
  5426. 'begin',
  5427. ' Func:=function(b:word): word',
  5428. ' begin',
  5429. ' Result:=a+b;',
  5430. ' exit(b);',
  5431. ' exit(Result);',
  5432. ' end;',// test semicolon
  5433. ' a:=3;',
  5434. 'end;',
  5435. 'begin',
  5436. ' Func:=function(c:word):word begin',
  5437. ' Result:=3+c;',
  5438. ' exit(c);',
  5439. ' exit(Result);',
  5440. ' end;']);
  5441. ConvertProgram;
  5442. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5443. LinesToStr([ // statements
  5444. 'this.Func = null;',
  5445. 'this.DoIt = function (a) {',
  5446. ' $mod.Func = function (b) {',
  5447. ' var Result = 0;',
  5448. ' Result = a + b;',
  5449. ' return b;',
  5450. ' return Result;',
  5451. ' return Result;',
  5452. ' };',
  5453. ' a = 3;',
  5454. '};',
  5455. '']),
  5456. LinesToStr([
  5457. '$mod.Func = function (c) {',
  5458. ' var Result = 0;',
  5459. ' Result = 3 + c;',
  5460. ' return c;',
  5461. ' return Result;',
  5462. ' return Result;',
  5463. '};',
  5464. '']));
  5465. end;
  5466. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5467. begin
  5468. StartProgram(false);
  5469. Add([
  5470. '{$mode delphi}',
  5471. 'type',
  5472. ' TProc = reference to procedure(x: word);',
  5473. 'procedure DoIt(a: word);',
  5474. 'var Proc: TProc;',
  5475. 'begin',
  5476. ' Proc:=procedure(b:word) begin end;',
  5477. 'end;',
  5478. 'var Proc: TProc;',
  5479. 'begin',
  5480. ' Proc:=procedure(c:word) begin end;',
  5481. '']);
  5482. ConvertProgram;
  5483. CheckSource('TestAnonymousProc_Assign_Delphi',
  5484. LinesToStr([ // statements
  5485. 'this.DoIt = function (a) {',
  5486. ' var Proc = null;',
  5487. ' Proc = function (b) {',
  5488. ' };',
  5489. '};',
  5490. 'this.Proc = null;',
  5491. '']),
  5492. LinesToStr([
  5493. '$mod.Proc = function (c) {',
  5494. '};',
  5495. '']));
  5496. end;
  5497. procedure TTestModule.TestAnonymousProc_Arg;
  5498. begin
  5499. StartProgram(false);
  5500. Add([
  5501. 'type',
  5502. ' TProc = reference to procedure;',
  5503. ' TFunc = reference to function(x: word): word;',
  5504. 'procedure DoMore(f,g: TProc);',
  5505. 'begin',
  5506. 'end;',
  5507. 'procedure DoOdd(v: jsvalue);',
  5508. 'begin',
  5509. 'end;',
  5510. 'procedure DoIt(f: TFunc);',
  5511. 'begin',
  5512. ' DoIt(function(b:word): word',
  5513. ' begin',
  5514. ' Result:=1+b;',
  5515. ' end);',
  5516. ' DoMore(procedure begin end, procedure begin end);',
  5517. ' DoOdd(procedure begin end);',
  5518. 'end;',
  5519. 'begin',
  5520. ' DoMore(procedure begin end,',
  5521. ' procedure assembler asm',
  5522. ' console.log("c");',
  5523. ' end);',
  5524. '']);
  5525. ConvertProgram;
  5526. CheckSource('TestAnonymousProc_Arg',
  5527. LinesToStr([ // statements
  5528. 'this.DoMore = function (f, g) {',
  5529. '};',
  5530. 'this.DoOdd = function (v) {',
  5531. '};',
  5532. 'this.DoIt = function (f) {',
  5533. ' $mod.DoIt(function (b) {',
  5534. ' var Result = 0;',
  5535. ' Result = 1 + b;',
  5536. ' return Result;',
  5537. ' });',
  5538. ' $mod.DoMore(function () {',
  5539. ' }, function () {',
  5540. ' });',
  5541. ' $mod.DoOdd(function () {',
  5542. ' });',
  5543. '};',
  5544. '']),
  5545. LinesToStr([
  5546. '$mod.DoMore(function () {',
  5547. '}, function () {',
  5548. ' console.log("c");',
  5549. '});',
  5550. '']));
  5551. end;
  5552. procedure TTestModule.TestAnonymousProc_Typecast;
  5553. begin
  5554. StartProgram(false);
  5555. Add([
  5556. 'type',
  5557. ' TProc = reference to procedure(w: word);',
  5558. ' TArr = array of word;',
  5559. ' TFuncArr = reference to function: TArr;',
  5560. 'procedure DoIt(p: TProc);',
  5561. 'var',
  5562. ' w: word;',
  5563. ' a: TArr;',
  5564. 'begin',
  5565. ' p:=TProc(procedure(b: smallint) begin end);',
  5566. ' a:=TFuncArr(function: TArr begin end)();',
  5567. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5568. 'end;',
  5569. 'begin']);
  5570. ConvertProgram;
  5571. CheckSource('TestAnonymousProc_Typecast',
  5572. LinesToStr([ // statements
  5573. 'this.DoIt = function (p) {',
  5574. ' var w = 0;',
  5575. ' var a = [];',
  5576. ' p = function (b) {',
  5577. ' };',
  5578. ' a = function () {',
  5579. ' var Result = [];',
  5580. ' return Result;',
  5581. ' }();',
  5582. ' w = function () {',
  5583. ' var Result = [];',
  5584. ' return Result;',
  5585. ' }()[3];',
  5586. '};',
  5587. '']),
  5588. LinesToStr([
  5589. '']));
  5590. end;
  5591. procedure TTestModule.TestAnonymousProc_With;
  5592. begin
  5593. StartProgram(false);
  5594. Add([
  5595. 'type',
  5596. ' TProc = reference to procedure(w: word);',
  5597. ' TObject = class',
  5598. ' b: boolean;',
  5599. ' end;',
  5600. 'var',
  5601. ' p: TProc;',
  5602. ' bird: TObject;',
  5603. 'begin',
  5604. ' with bird do',
  5605. ' p:=procedure(w: word)',
  5606. ' begin',
  5607. ' b:=w>2;',
  5608. ' end;',
  5609. '']);
  5610. ConvertProgram;
  5611. CheckSource('TestAnonymousProc_With',
  5612. LinesToStr([ // statements
  5613. 'rtl.createClass(this, "TObject", null, function () {',
  5614. ' this.$init = function () {',
  5615. ' this.b = false;',
  5616. ' };',
  5617. ' this.$final = function () {',
  5618. ' };',
  5619. '});',
  5620. 'this.p = null;',
  5621. 'this.bird = null;',
  5622. '']),
  5623. LinesToStr([
  5624. 'var $with = $mod.bird;',
  5625. '$mod.p = function (w) {',
  5626. ' $with.b = w > 2;',
  5627. '};',
  5628. '']));
  5629. end;
  5630. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5631. begin
  5632. StartProgram(false);
  5633. Add([
  5634. 'type',
  5635. ' TProc = reference to procedure;',
  5636. ' TObject = class',
  5637. ' b: boolean;',
  5638. ' end;',
  5639. 'procedure DoIt;',
  5640. 'var',
  5641. ' p: TProc;',
  5642. 'begin',
  5643. ' try',
  5644. ' except',
  5645. ' on E: TObject do',
  5646. ' p:=procedure',
  5647. ' begin',
  5648. ' E.b:=true;',
  5649. ' end;',
  5650. ' end;',
  5651. 'end;',
  5652. 'begin']);
  5653. ConvertProgram;
  5654. CheckSource('TestAnonymousProc_ExceptOn',
  5655. LinesToStr([ // statements
  5656. 'rtl.createClass(this, "TObject", null, function () {',
  5657. ' this.$init = function () {',
  5658. ' this.b = false;',
  5659. ' };',
  5660. ' this.$final = function () {',
  5661. ' };',
  5662. '});',
  5663. 'this.DoIt = function () {',
  5664. ' var p = null;',
  5665. ' try {} catch ($e) {',
  5666. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5667. ' var E = $e;',
  5668. ' p = function () {',
  5669. ' E.b = true;',
  5670. ' };',
  5671. ' } else throw $e',
  5672. ' };',
  5673. '};',
  5674. '']),
  5675. LinesToStr([
  5676. '']));
  5677. end;
  5678. procedure TTestModule.TestAnonymousProc_Nested;
  5679. begin
  5680. StartProgram(false);
  5681. Add([
  5682. 'type',
  5683. ' TProc = reference to procedure;',
  5684. ' TObject = class',
  5685. ' i: byte;',
  5686. ' procedure DoIt;',
  5687. ' end;',
  5688. 'procedure TObject.DoIt;',
  5689. 'var',
  5690. ' p: TProc;',
  5691. ' procedure Sub;',
  5692. ' begin',
  5693. ' p:=procedure',
  5694. ' begin',
  5695. ' i:=3;',
  5696. ' Self.i:=4;',
  5697. ' p:=procedure',
  5698. ' procedure SubSub;',
  5699. ' begin',
  5700. ' i:=13;',
  5701. ' Self.i:=14;',
  5702. ' end;',
  5703. ' begin',
  5704. ' i:=13;',
  5705. ' Self.i:=14;',
  5706. ' end;',
  5707. ' end;',
  5708. ' end;',
  5709. 'begin',
  5710. 'end;',
  5711. 'begin']);
  5712. ConvertProgram;
  5713. CheckSource('TestAnonymousProc_Nested',
  5714. LinesToStr([ // statements
  5715. 'rtl.createClass(this, "TObject", null, function () {',
  5716. ' this.$init = function () {',
  5717. ' this.i = 0;',
  5718. ' };',
  5719. ' this.$final = function () {',
  5720. ' };',
  5721. ' this.DoIt = function () {',
  5722. ' var $Self = this;',
  5723. ' var p = null;',
  5724. ' function Sub() {',
  5725. ' p = function () {',
  5726. ' $Self.i = 3;',
  5727. ' $Self.i = 4;',
  5728. ' p = function () {',
  5729. ' function SubSub() {',
  5730. ' $Self.i = 13;',
  5731. ' $Self.i = 14;',
  5732. ' };',
  5733. ' $Self.i = 13;',
  5734. ' $Self.i = 14;',
  5735. ' };',
  5736. ' };',
  5737. ' };',
  5738. ' };',
  5739. '});',
  5740. '']),
  5741. LinesToStr([
  5742. '']));
  5743. end;
  5744. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5745. begin
  5746. StartProgram(false);
  5747. Add([
  5748. 'type',
  5749. ' TProc = reference to procedure;',
  5750. 'function DoIt: TProc;',
  5751. ' function Sub: TProc;',
  5752. ' begin',
  5753. ' Result:=procedure',
  5754. ' begin',
  5755. ' Sub:=procedure',
  5756. ' procedure SubSub;',
  5757. ' begin',
  5758. ' Result:=nil;',
  5759. ' Sub:=nil;',
  5760. ' DoIt:=nil;',
  5761. ' end;',
  5762. ' begin',
  5763. ' Result:=nil;',
  5764. ' Sub:=nil;',
  5765. ' DoIt:=nil;',
  5766. ' end;',
  5767. ' end;',
  5768. ' end;',
  5769. 'begin',
  5770. 'end;',
  5771. 'begin']);
  5772. ConvertProgram;
  5773. CheckSource('TestAnonymousProc_NestedAssignResult',
  5774. LinesToStr([ // statements
  5775. 'this.DoIt = function () {',
  5776. ' var Result = null;',
  5777. ' function Sub() {',
  5778. ' var Result$1 = null;',
  5779. ' Result$1 = function () {',
  5780. ' Result$1 = function () {',
  5781. ' function SubSub() {',
  5782. ' Result$1 = null;',
  5783. ' Result$1 = null;',
  5784. ' Result = null;',
  5785. ' };',
  5786. ' Result$1 = null;',
  5787. ' Result$1 = null;',
  5788. ' Result = null;',
  5789. ' };',
  5790. ' };',
  5791. ' return Result$1;',
  5792. ' };',
  5793. ' return Result;',
  5794. '};',
  5795. '']),
  5796. LinesToStr([
  5797. '']));
  5798. end;
  5799. procedure TTestModule.TestAnonymousProc_Class;
  5800. begin
  5801. StartProgram(false);
  5802. Add([
  5803. 'type',
  5804. ' TProc = reference to procedure;',
  5805. ' TEvent = procedure of object;',
  5806. ' TObject = class',
  5807. ' Size: word;',
  5808. ' function GetIt: TProc;',
  5809. ' procedure DoIt; virtual; abstract;',
  5810. ' end;',
  5811. 'function TObject.GetIt: TProc;',
  5812. 'begin',
  5813. ' Result:=procedure',
  5814. ' var p: TEvent;',
  5815. ' begin',
  5816. ' Size:=Size;',
  5817. ' Size:=Self.Size;',
  5818. ' p:=@DoIt;',
  5819. ' p:[email protected];',
  5820. ' end;',
  5821. 'end;',
  5822. 'begin']);
  5823. ConvertProgram;
  5824. CheckSource('TestAnonymousProc_Class',
  5825. LinesToStr([ // statements
  5826. 'rtl.createClass(this, "TObject", null, function () {',
  5827. ' this.$init = function () {',
  5828. ' this.Size = 0;',
  5829. ' };',
  5830. ' this.$final = function () {',
  5831. ' };',
  5832. ' this.GetIt = function () {',
  5833. ' var $Self = this;',
  5834. ' var Result = null;',
  5835. ' Result = function () {',
  5836. ' var p = null;',
  5837. ' $Self.Size = $Self.Size;',
  5838. ' $Self.Size = $Self.Size;',
  5839. ' p = rtl.createCallback($Self, "DoIt");',
  5840. ' p = rtl.createCallback($Self, "DoIt");',
  5841. ' };',
  5842. ' return Result;',
  5843. ' };',
  5844. '});',
  5845. '']),
  5846. LinesToStr([
  5847. '']));
  5848. end;
  5849. procedure TTestModule.TestAnonymousProc_ForLoop;
  5850. begin
  5851. StartProgram(false);
  5852. Add([
  5853. 'type TProc = reference to procedure;',
  5854. 'procedure Foo(p: TProc);',
  5855. 'begin',
  5856. 'end;',
  5857. 'procedure DoIt;',
  5858. 'var i: word;',
  5859. ' a: word;',
  5860. 'begin',
  5861. ' for i:=1 to 10 do begin',
  5862. ' Foo(procedure begin a:=3; end);',
  5863. ' end;',
  5864. 'end;',
  5865. 'begin',
  5866. ' DoIt;']);
  5867. ConvertProgram;
  5868. CheckSource('TestAnonymousProc_ForLoop',
  5869. LinesToStr([ // statements
  5870. 'this.Foo = function (p) {',
  5871. '};',
  5872. 'this.DoIt = function () {',
  5873. ' var i = 0;',
  5874. ' var a = 0;',
  5875. ' for (i = 1; i <= 10; i++) {',
  5876. ' $mod.Foo(function () {',
  5877. ' a = 3;',
  5878. ' });',
  5879. ' };',
  5880. '};',
  5881. '']),
  5882. LinesToStr([
  5883. '$mod.DoIt();'
  5884. ]));
  5885. end;
  5886. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5887. begin
  5888. StartProgram(false);
  5889. Add([
  5890. '{$mode delphi}',
  5891. 'type',
  5892. ' TProc = reference to procedure;',
  5893. ' TFunc = reference to function(x: word): word;',
  5894. 'procedure Run;',
  5895. 'asm',
  5896. 'end;',
  5897. 'procedure Walk(p: TProc; f: TFunc);',
  5898. 'begin',
  5899. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5900. 'end;',
  5901. 'begin',
  5902. ' Walk(procedure',
  5903. ' asm',
  5904. ' console.log("a");',
  5905. ' end,',
  5906. ' function(x: word): word asm',
  5907. ' console.log("c");',
  5908. ' end);',
  5909. '']);
  5910. ConvertProgram;
  5911. CheckSource('TestAnonymousProc_AsmDelphi',
  5912. LinesToStr([ // statements
  5913. 'this.Run = function () {',
  5914. '};',
  5915. 'this.Walk = function (p, f) {',
  5916. ' $mod.Walk(function () {',
  5917. ' }, function (b) {',
  5918. ' return 1+b;',
  5919. ' });',
  5920. '};',
  5921. '']),
  5922. LinesToStr([
  5923. '$mod.Walk(function () {',
  5924. ' console.log("a");',
  5925. '}, function (x) {',
  5926. ' console.log("c");',
  5927. '});',
  5928. '']));
  5929. end;
  5930. procedure TTestModule.TestEnum_Name;
  5931. begin
  5932. StartProgram(false);
  5933. Add('type TMyEnum = (Red, Green, Blue);');
  5934. Add('var e: TMyEnum;');
  5935. Add('var f: TMyEnum = Blue;');
  5936. Add('begin');
  5937. Add(' e:=green;');
  5938. Add(' e:=default(TMyEnum);');
  5939. ConvertProgram;
  5940. CheckSource('TestEnum_Name',
  5941. LinesToStr([ // statements
  5942. 'this.TMyEnum = {',
  5943. ' "0":"Red",',
  5944. ' Red:0,',
  5945. ' "1":"Green",',
  5946. ' Green:1,',
  5947. ' "2":"Blue",',
  5948. ' Blue:2',
  5949. ' };',
  5950. 'this.e = 0;',
  5951. 'this.f = this.TMyEnum.Blue;'
  5952. ]),
  5953. LinesToStr([
  5954. '$mod.e=$mod.TMyEnum.Green;',
  5955. '$mod.e=$mod.TMyEnum.Red;'
  5956. ]));
  5957. end;
  5958. procedure TTestModule.TestEnum_Number;
  5959. begin
  5960. Converter.Options:=Converter.Options+[coEnumNumbers];
  5961. StartProgram(false);
  5962. Add('type TMyEnum = (Red, Green);');
  5963. Add('var');
  5964. Add(' e: TMyEnum;');
  5965. Add(' f: TMyEnum = Green;');
  5966. Add(' i: longint;');
  5967. Add('begin');
  5968. Add(' e:=green;');
  5969. Add(' i:=longint(e);');
  5970. ConvertProgram;
  5971. CheckSource('TestEnumNumber',
  5972. LinesToStr([ // statements
  5973. 'this.TMyEnum = {',
  5974. ' "0":"Red",',
  5975. ' Red:0,',
  5976. ' "1":"Green",',
  5977. ' Green:1',
  5978. ' };',
  5979. 'this.e = 0;',
  5980. 'this.f = 1;',
  5981. 'this.i = 0;'
  5982. ]),
  5983. LinesToStr([
  5984. '$mod.e=1;',
  5985. '$mod.i=$mod.e;'
  5986. ]));
  5987. end;
  5988. procedure TTestModule.TestEnum_ConstFail;
  5989. begin
  5990. StartProgram(false);
  5991. Add([
  5992. 'type TMyEnum = (Red = 100, Green = 101);',
  5993. 'var',
  5994. ' e: TMyEnum;',
  5995. ' f: TMyEnum = Green;',
  5996. 'begin',
  5997. ' e:=green;']);
  5998. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5999. ConvertProgram;
  6000. end;
  6001. procedure TTestModule.TestEnum_Functions;
  6002. begin
  6003. StartProgram(false);
  6004. Add([
  6005. 'type TMyEnum = (Red, Green);',
  6006. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6007. 'var',
  6008. ' v: longint;',
  6009. ' s: string;',
  6010. 'begin',
  6011. ' val(s,e,v);',
  6012. ' val(s,e,i);',
  6013. 'end;',
  6014. 'var',
  6015. ' e: TMyEnum;',
  6016. ' i: longint;',
  6017. ' s: string;',
  6018. ' b: boolean;',
  6019. 'begin',
  6020. ' i:=ord(red);',
  6021. ' i:=ord(green);',
  6022. ' i:=ord(e);',
  6023. ' i:=ord(b);',
  6024. ' e:=low(tmyenum);',
  6025. ' e:=low(e);',
  6026. ' b:=low(boolean);',
  6027. ' e:=high(tmyenum);',
  6028. ' e:=high(e);',
  6029. ' b:=high(boolean);',
  6030. ' e:=pred(green);',
  6031. ' e:=pred(e);',
  6032. ' b:=pred(b);',
  6033. ' e:=succ(red);',
  6034. ' e:=succ(e);',
  6035. ' b:=succ(b);',
  6036. ' e:=tmyenum(1);',
  6037. ' e:=tmyenum(i);',
  6038. ' s:=str(e);',
  6039. ' str(e,s);',
  6040. ' str(red,s);',
  6041. ' s:=str(e:3);',
  6042. ' writestr(s,e:3,red);',
  6043. ' val(s,e,i);',
  6044. ' i:=longint(e);']);
  6045. ConvertProgram;
  6046. CheckSource('TestEnum_Functions',
  6047. LinesToStr([ // statements
  6048. 'this.TMyEnum = {',
  6049. ' "0":"Red",',
  6050. ' Red:0,',
  6051. ' "1":"Green",',
  6052. ' Green:1',
  6053. ' };',
  6054. 'this.DoIt = function (e, i) {',
  6055. ' var v = 0;',
  6056. ' var s = "";',
  6057. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6058. ' v = w;',
  6059. ' }));',
  6060. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6061. '};',
  6062. 'this.e = 0;',
  6063. 'this.i = 0;',
  6064. 'this.s = "";',
  6065. 'this.b = false;',
  6066. '']),
  6067. LinesToStr([
  6068. '$mod.i=$mod.TMyEnum.Red;',
  6069. '$mod.i=$mod.TMyEnum.Green;',
  6070. '$mod.i=$mod.e;',
  6071. '$mod.i=$mod.b+0;',
  6072. '$mod.e=$mod.TMyEnum.Red;',
  6073. '$mod.e=$mod.TMyEnum.Red;',
  6074. '$mod.b=false;',
  6075. '$mod.e=$mod.TMyEnum.Green;',
  6076. '$mod.e=$mod.TMyEnum.Green;',
  6077. '$mod.b=true;',
  6078. '$mod.e=$mod.TMyEnum.Green-1;',
  6079. '$mod.e=$mod.e-1;',
  6080. '$mod.b=false;',
  6081. '$mod.e=$mod.TMyEnum.Red+1;',
  6082. '$mod.e=$mod.e+1;',
  6083. '$mod.b=true;',
  6084. '$mod.e=1;',
  6085. '$mod.e=$mod.i;',
  6086. '$mod.s = $mod.TMyEnum[$mod.e];',
  6087. '$mod.s = $mod.TMyEnum[$mod.e];',
  6088. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6089. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6090. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6091. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6092. ' $mod.i = v;',
  6093. '});',
  6094. '$mod.i=$mod.e;',
  6095. '']));
  6096. end;
  6097. procedure TTestModule.TestEnumRg_Functions;
  6098. begin
  6099. StartProgram(false);
  6100. Add([
  6101. 'type',
  6102. ' TEnum = (Red, Green, Blue);',
  6103. ' TEnumRg = Green..Blue;',
  6104. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6105. 'var',
  6106. ' v: longint;',
  6107. ' s: string;',
  6108. 'begin',
  6109. ' val(s,e,v);',
  6110. ' val(s,e,i);',
  6111. 'end;',
  6112. 'var',
  6113. ' e: TEnumRg;',
  6114. ' i: longint;',
  6115. ' s: string;',
  6116. 'begin',
  6117. ' i:=ord(green);',
  6118. ' i:=ord(e);',
  6119. ' e:=low(tenumrg);',
  6120. ' e:=low(e);',
  6121. ' e:=high(tenumrg);',
  6122. ' e:=high(e);',
  6123. ' e:=pred(blue);',
  6124. ' e:=pred(e);',
  6125. ' e:=succ(green);',
  6126. ' e:=succ(e);',
  6127. ' e:=tenumrg(1);',
  6128. ' e:=tenumrg(i);',
  6129. ' s:=str(e);',
  6130. ' str(e,s);',
  6131. ' str(red,s);',
  6132. ' s:=str(e:3);',
  6133. ' writestr(s,e:3,blue);',
  6134. ' val(s,e,i);',
  6135. ' i:=longint(e);']);
  6136. ConvertProgram;
  6137. CheckSource('TestEnumRg_Functions',
  6138. LinesToStr([ // statements
  6139. 'this.TEnum = {',
  6140. ' "0":"Red",',
  6141. ' Red:0,',
  6142. ' "1":"Green",',
  6143. ' Green:1,',
  6144. ' "2":"Blue",',
  6145. ' Blue:2',
  6146. ' };',
  6147. 'this.DoIt = function (e, i) {',
  6148. ' var v = 0;',
  6149. ' var s = "";',
  6150. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6151. ' v = w;',
  6152. ' }));',
  6153. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6154. '};',
  6155. 'this.e = this.TEnum.Green;',
  6156. 'this.i = 0;',
  6157. 'this.s = "";',
  6158. '']),
  6159. LinesToStr([
  6160. '$mod.i=$mod.TEnum.Green;',
  6161. '$mod.i=$mod.e;',
  6162. '$mod.e=$mod.TEnum.Green;',
  6163. '$mod.e=$mod.TEnum.Green;',
  6164. '$mod.e=$mod.TEnum.Blue;',
  6165. '$mod.e=$mod.TEnum.Blue;',
  6166. '$mod.e=$mod.TEnum.Blue-1;',
  6167. '$mod.e=$mod.e-1;',
  6168. '$mod.e=$mod.TEnum.Green+1;',
  6169. '$mod.e=$mod.e+1;',
  6170. '$mod.e=1;',
  6171. '$mod.e=$mod.i;',
  6172. '$mod.s = $mod.TEnum[$mod.e];',
  6173. '$mod.s = $mod.TEnum[$mod.e];',
  6174. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6175. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6176. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6177. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6178. ' $mod.i = v;',
  6179. '});',
  6180. '$mod.i=$mod.e;',
  6181. '']));
  6182. end;
  6183. procedure TTestModule.TestEnum_AsParams;
  6184. begin
  6185. StartProgram(false);
  6186. Add('type TEnum = (Red,Blue);');
  6187. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6188. Add('var vJ: TEnum;');
  6189. Add('begin');
  6190. Add(' vg:=vg;');
  6191. Add(' vj:=vh;');
  6192. Add(' vi:=vi;');
  6193. Add(' doit(vg,vg,vg);');
  6194. Add(' doit(vh,vh,vj);');
  6195. Add(' doit(vi,vi,vi);');
  6196. Add(' doit(vj,vj,vj);');
  6197. Add('end;');
  6198. Add('var i: TEnum;');
  6199. Add('begin');
  6200. Add(' doit(i,i,i);');
  6201. ConvertProgram;
  6202. CheckSource('TestEnum_AsParams',
  6203. LinesToStr([ // statements
  6204. 'this.TEnum = {',
  6205. ' "0": "Red",',
  6206. ' Red: 0,',
  6207. ' "1": "Blue",',
  6208. ' Blue: 1',
  6209. '};',
  6210. 'this.DoIt = function (vG,vH,vI) {',
  6211. ' var vJ = 0;',
  6212. ' vG = vG;',
  6213. ' vJ = vH;',
  6214. ' vI.set(vI.get());',
  6215. ' $mod.DoIt(vG, vG, {',
  6216. ' get: function () {',
  6217. ' return vG;',
  6218. ' },',
  6219. ' set: function (v) {',
  6220. ' vG = v;',
  6221. ' }',
  6222. ' });',
  6223. ' $mod.DoIt(vH, vH, {',
  6224. ' get: function () {',
  6225. ' return vJ;',
  6226. ' },',
  6227. ' set: function (v) {',
  6228. ' vJ = v;',
  6229. ' }',
  6230. ' });',
  6231. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6232. ' $mod.DoIt(vJ, vJ, {',
  6233. ' get: function () {',
  6234. ' return vJ;',
  6235. ' },',
  6236. ' set: function (v) {',
  6237. ' vJ = v;',
  6238. ' }',
  6239. ' });',
  6240. '};',
  6241. 'this.i = 0;'
  6242. ]),
  6243. LinesToStr([
  6244. '$mod.DoIt($mod.i,$mod.i,{',
  6245. ' p: $mod,',
  6246. ' get: function () {',
  6247. ' return this.p.i;',
  6248. ' },',
  6249. ' set: function (v) {',
  6250. ' this.p.i = v;',
  6251. ' }',
  6252. '});'
  6253. ]));
  6254. end;
  6255. procedure TTestModule.TestEnumRange_Array;
  6256. begin
  6257. StartProgram(false);
  6258. Add([
  6259. 'type',
  6260. ' TEnum = (Red, Green, Blue);',
  6261. ' TEnumRg = green..blue;',
  6262. ' TArr = array[TEnumRg] of byte;',
  6263. ' TArr2 = array[green..blue] of byte;',
  6264. 'var',
  6265. ' a: TArr;',
  6266. ' b: TArr = (3,4);',
  6267. ' c: TArr2 = (5,6);',
  6268. 'begin',
  6269. ' a[green] := b[blue];',
  6270. ' c[green] := c[blue];',
  6271. '']);
  6272. ConvertProgram;
  6273. CheckSource('TestEnumRange_Array',
  6274. LinesToStr([ // statements
  6275. 'this.TEnum = {',
  6276. ' "0": "Red",',
  6277. ' Red: 0,',
  6278. ' "1": "Green",',
  6279. ' Green: 1,',
  6280. ' "2": "Blue",',
  6281. ' Blue: 2',
  6282. '};',
  6283. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6284. 'this.b = [3, 4];',
  6285. 'this.c = [5, 6];',
  6286. '']),
  6287. LinesToStr([
  6288. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6289. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6290. '']));
  6291. end;
  6292. procedure TTestModule.TestEnum_ForIn;
  6293. begin
  6294. StartProgram(false);
  6295. Add([
  6296. 'type',
  6297. ' TEnum = (Red, Green, Blue);',
  6298. ' TEnumRg = green..blue;',
  6299. ' TArr = array[TEnum] of byte;',
  6300. ' TArrRg = array[TEnumRg] of byte;',
  6301. 'var',
  6302. ' e: TEnum;',
  6303. ' a1: TArr = (3,4,5);',
  6304. ' a2: TArrRg = (11,12);',
  6305. ' b: byte;',
  6306. 'begin',
  6307. ' for e in TEnum do ;',
  6308. ' for e in TEnumRg do ;',
  6309. ' for e in TArr do ;',
  6310. ' for e in TArrRg do ;',
  6311. ' for b in a1 do ;',
  6312. ' for b in a2 do ;',
  6313. '']);
  6314. ConvertProgram;
  6315. CheckSource('TestEnum_ForIn',
  6316. LinesToStr([ // statements
  6317. 'this.TEnum = {',
  6318. ' "0": "Red",',
  6319. ' Red: 0,',
  6320. ' "1": "Green",',
  6321. ' Green: 1,',
  6322. ' "2": "Blue",',
  6323. ' Blue: 2',
  6324. '};',
  6325. 'this.e = 0;',
  6326. 'this.a1 = [3, 4, 5];',
  6327. 'this.a2 = [11, 12];',
  6328. 'this.b = 0;',
  6329. '']),
  6330. LinesToStr([
  6331. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6332. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6333. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6334. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6335. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6336. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6337. '']));
  6338. end;
  6339. procedure TTestModule.TestEnum_ScopedNumber;
  6340. begin
  6341. Converter.Options:=Converter.Options+[coEnumNumbers];
  6342. StartProgram(false);
  6343. Add([
  6344. 'type',
  6345. ' TEnum = (Red, Green);',
  6346. 'var',
  6347. ' e: TEnum;',
  6348. 'begin',
  6349. ' e:=TEnum.Green;',
  6350. '']);
  6351. ConvertProgram;
  6352. CheckSource('TestEnum_ScopedNumber',
  6353. LinesToStr([ // statements
  6354. 'this.TEnum = {',
  6355. ' "0": "Red",',
  6356. ' Red: 0,',
  6357. ' "1": "Green",',
  6358. ' Green: 1',
  6359. '};',
  6360. 'this.e = 0;',
  6361. '']),
  6362. LinesToStr([
  6363. '$mod.e = 1;']));
  6364. end;
  6365. procedure TTestModule.TestEnum_InFunction;
  6366. begin
  6367. StartProgram(false);
  6368. Add([
  6369. 'const TEnum = 3;',
  6370. 'procedure DoIt;',
  6371. 'type',
  6372. ' TEnum = (Red, Green, Blue);',
  6373. ' procedure Sub;',
  6374. ' type',
  6375. ' TEnumSub = (Left, Right);',
  6376. ' var',
  6377. ' es: TEnumSub;',
  6378. ' begin',
  6379. ' es:=Left;',
  6380. ' end;',
  6381. 'var',
  6382. ' e, e2: TEnum;',
  6383. 'begin',
  6384. ' if e in [red,blue] then e2:=e;',
  6385. 'end;',
  6386. 'begin']);
  6387. ConvertProgram;
  6388. CheckSource('TestEnum_InFunction',
  6389. LinesToStr([ // statements
  6390. 'this.TEnum = 3;',
  6391. 'var TEnum$1 = {',
  6392. ' "0":"Red",',
  6393. ' Red:0,',
  6394. ' "1":"Green",',
  6395. ' Green:1,',
  6396. ' "2":"Blue",',
  6397. ' Blue:2',
  6398. ' };',
  6399. 'var TEnumSub = {',
  6400. ' "0": "Left",',
  6401. ' Left: 0,',
  6402. ' "1": "Right",',
  6403. ' Right: 1',
  6404. '};',
  6405. 'this.DoIt = function () {',
  6406. ' function Sub() {',
  6407. ' var es = 0;',
  6408. ' es = TEnumSub.Left;',
  6409. ' };',
  6410. ' var e = 0;',
  6411. ' var e2 = 0;',
  6412. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6413. '};',
  6414. '']),
  6415. LinesToStr([
  6416. '']));
  6417. end;
  6418. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6419. begin
  6420. StartUnit(true);
  6421. Add([
  6422. 'interface',
  6423. 'var color: (red, green);',
  6424. 'implementation',
  6425. 'initialization',
  6426. ' color:=green;',
  6427. '']);
  6428. ConvertUnit;
  6429. CheckSource('TestEnum_Name_Anonymous_Unit',
  6430. LinesToStr([
  6431. 'this.color$a = {',
  6432. ' "0": "red",',
  6433. ' red: 0,',
  6434. ' "1": "green",',
  6435. ' green: 1',
  6436. '};',
  6437. 'this.color = 0;',
  6438. '']),
  6439. LinesToStr([ // this.$init
  6440. '$mod.color = $mod.color$a.green;',
  6441. '']),
  6442. LinesToStr([ // implementation
  6443. '']) );
  6444. end;
  6445. procedure TTestModule.TestSet_Enum;
  6446. begin
  6447. StartProgram(false);
  6448. Add([
  6449. 'type',
  6450. ' TColor = (Red, Green, Blue);',
  6451. ' TColors = set of TColor;',
  6452. 'var',
  6453. ' c: TColor;',
  6454. ' s: TColors;',
  6455. ' t: TColors = [];',
  6456. ' u: TColors = [Red];',
  6457. 'begin',
  6458. ' s:=[];',
  6459. ' s:=[Green];',
  6460. ' s:=[Green,Blue];',
  6461. ' s:=[Red..Blue];',
  6462. ' s:=[Red,Green..Blue];',
  6463. ' s:=[Red,c];',
  6464. ' s:=t;',
  6465. ' s:=default(TColors);',
  6466. '']);
  6467. ConvertProgram;
  6468. CheckSource('TestSet',
  6469. LinesToStr([ // statements
  6470. 'this.TColor = {',
  6471. ' "0":"Red",',
  6472. ' Red:0,',
  6473. ' "1":"Green",',
  6474. ' Green:1,',
  6475. ' "2":"Blue",',
  6476. ' Blue:2',
  6477. ' };',
  6478. 'this.c = 0;',
  6479. 'this.s = {};',
  6480. 'this.t = {};',
  6481. 'this.u = rtl.createSet(this.TColor.Red);'
  6482. ]),
  6483. LinesToStr([
  6484. '$mod.s={};',
  6485. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6486. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6487. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6488. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6489. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6490. '$mod.s=rtl.refSet($mod.t);',
  6491. '$mod.s={};',
  6492. '']));
  6493. end;
  6494. procedure TTestModule.TestSet_Operators;
  6495. begin
  6496. StartProgram(false);
  6497. Add('type');
  6498. Add(' TColor = (Red, Green, Blue);');
  6499. Add(' TColors = set of tcolor;');
  6500. Add('var');
  6501. Add(' vC: TColor;');
  6502. Add(' vS: TColors;');
  6503. Add(' vT: TColors;');
  6504. Add(' vU: TColors;');
  6505. Add(' B: boolean;');
  6506. Add('begin');
  6507. Add(' include(vs,green);');
  6508. Add(' exclude(vs,vc);');
  6509. Add(' vs:=vt+vu;');
  6510. Add(' vs:=vt+[red];');
  6511. Add(' vs:=[red]+vt;');
  6512. Add(' vs:=[red]+[green];');
  6513. Add(' vs:=vt-vu;');
  6514. Add(' vs:=vt-[red];');
  6515. Add(' vs:=[red]-vt;');
  6516. Add(' vs:=[red]-[green];');
  6517. Add(' vs:=vt*vu;');
  6518. Add(' vs:=vt*[red];');
  6519. Add(' vs:=[red]*vt;');
  6520. Add(' vs:=[red]*[green];');
  6521. Add(' vs:=vt><vu;');
  6522. Add(' vs:=vt><[red];');
  6523. Add(' vs:=[red]><vt;');
  6524. Add(' vs:=[red]><[green];');
  6525. Add(' b:=vt=vu;');
  6526. Add(' b:=vt=[red];');
  6527. Add(' b:=[red]=vt;');
  6528. Add(' b:=[red]=[green];');
  6529. Add(' b:=vt<>vu;');
  6530. Add(' b:=vt<>[red];');
  6531. Add(' b:=[red]<>vt;');
  6532. Add(' b:=[red]<>[green];');
  6533. Add(' b:=vt<=vu;');
  6534. Add(' b:=vt<=[red];');
  6535. Add(' b:=[red]<=vt;');
  6536. Add(' b:=[red]<=[green];');
  6537. Add(' b:=vt>=vu;');
  6538. Add(' b:=vt>=[red];');
  6539. Add(' b:=[red]>=vt;');
  6540. Add(' b:=[red]>=[green];');
  6541. ConvertProgram;
  6542. CheckSource('TestSet_Operators',
  6543. LinesToStr([ // statements
  6544. 'this.TColor = {',
  6545. ' "0":"Red",',
  6546. ' Red:0,',
  6547. ' "1":"Green",',
  6548. ' Green:1,',
  6549. ' "2":"Blue",',
  6550. ' Blue:2',
  6551. ' };',
  6552. 'this.vC = 0;',
  6553. 'this.vS = {};',
  6554. 'this.vT = {};',
  6555. 'this.vU = {};',
  6556. 'this.B = false;'
  6557. ]),
  6558. LinesToStr([
  6559. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6560. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6561. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6562. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6563. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6564. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6565. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6566. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6567. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6568. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6569. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6570. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6571. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6572. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6573. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6574. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6575. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6576. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6577. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6578. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6579. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6580. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6581. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6582. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6583. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6584. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6585. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6586. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6587. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6588. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6589. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6590. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6591. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6592. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6593. '']));
  6594. end;
  6595. procedure TTestModule.TestSet_Operator_In;
  6596. begin
  6597. StartProgram(false);
  6598. Add([
  6599. 'type',
  6600. ' TColor = (Red, Green, Blue);',
  6601. ' TColors = set of tcolor;',
  6602. ' TColorRg = green..blue;',
  6603. 'var',
  6604. ' vC: tcolor;',
  6605. ' vT: tcolors;',
  6606. ' B: boolean;',
  6607. ' rg: TColorRg;',
  6608. 'begin',
  6609. ' b:=red in vt;',
  6610. ' b:=vc in vt;',
  6611. ' b:=green in [red..blue];',
  6612. ' b:=vc in [red..blue];',
  6613. ' ',
  6614. ' if red in vt then ;',
  6615. ' while vC in vt do ;',
  6616. ' repeat',
  6617. ' until vC in vt;',
  6618. ' if rg in [green..blue] then ;',
  6619. '']);
  6620. ConvertProgram;
  6621. CheckSource('TestSet_Operator_In',
  6622. LinesToStr([ // statements
  6623. 'this.TColor = {',
  6624. ' "0":"Red",',
  6625. ' Red:0,',
  6626. ' "1":"Green",',
  6627. ' Green:1,',
  6628. ' "2":"Blue",',
  6629. ' Blue:2',
  6630. ' };',
  6631. 'this.vC = 0;',
  6632. 'this.vT = {};',
  6633. 'this.B = false;',
  6634. 'this.rg = this.TColor.Green;',
  6635. '']),
  6636. LinesToStr([
  6637. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6638. '$mod.B = $mod.vC in $mod.vT;',
  6639. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6640. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6641. 'if ($mod.TColor.Red in $mod.vT) ;',
  6642. 'while ($mod.vC in $mod.vT) {',
  6643. '};',
  6644. 'do {',
  6645. '} while (!($mod.vC in $mod.vT));',
  6646. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6647. '']));
  6648. end;
  6649. procedure TTestModule.TestSet_Functions;
  6650. begin
  6651. StartProgram(false);
  6652. Add('type');
  6653. Add(' TMyEnum = (Red, Green);');
  6654. Add(' TMyEnums = set of TMyEnum;');
  6655. Add('var');
  6656. Add(' e: TMyEnum;');
  6657. Add(' s: TMyEnums;');
  6658. Add('begin');
  6659. Add(' e:=Low(TMyEnums);');
  6660. Add(' e:=Low(s);');
  6661. Add(' e:=High(TMyEnums);');
  6662. Add(' e:=High(s);');
  6663. ConvertProgram;
  6664. CheckSource('TestSetFunctions',
  6665. LinesToStr([ // statements
  6666. 'this.TMyEnum = {',
  6667. ' "0":"Red",',
  6668. ' Red:0,',
  6669. ' "1":"Green",',
  6670. ' Green:1',
  6671. ' };',
  6672. 'this.e = 0;',
  6673. 'this.s = {};'
  6674. ]),
  6675. LinesToStr([
  6676. '$mod.e=$mod.TMyEnum.Red;',
  6677. '$mod.e=$mod.TMyEnum.Red;',
  6678. '$mod.e=$mod.TMyEnum.Green;',
  6679. '$mod.e=$mod.TMyEnum.Green;',
  6680. '']));
  6681. end;
  6682. procedure TTestModule.TestSet_PassAsArgClone;
  6683. begin
  6684. StartProgram(false);
  6685. Add('type');
  6686. Add(' TMyEnum = (Red, Green);');
  6687. Add(' TMyEnums = set of TMyEnum;');
  6688. Add('procedure DoDefault(s: tmyenums); begin end;');
  6689. Add('procedure DoConst(const s: tmyenums); begin end;');
  6690. Add('var');
  6691. Add(' aSet: tmyenums;');
  6692. Add('begin');
  6693. Add(' dodefault(aset);');
  6694. Add(' doconst(aset);');
  6695. ConvertProgram;
  6696. CheckSource('TestSetFunctions',
  6697. LinesToStr([ // statements
  6698. 'this.TMyEnum = {',
  6699. ' "0":"Red",',
  6700. ' Red:0,',
  6701. ' "1":"Green",',
  6702. ' Green:1',
  6703. ' };',
  6704. 'this.DoDefault = function (s) {',
  6705. '};',
  6706. 'this.DoConst = function (s) {',
  6707. '};',
  6708. 'this.aSet = {};'
  6709. ]),
  6710. LinesToStr([
  6711. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6712. '$mod.DoConst($mod.aSet);',
  6713. '']));
  6714. end;
  6715. procedure TTestModule.TestSet_AsParams;
  6716. begin
  6717. StartProgram(false);
  6718. Add([
  6719. 'type TEnum = (Red,Blue);',
  6720. 'type TEnums = set of TEnum;',
  6721. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6722. 'var vJ: TEnums;',
  6723. 'begin',
  6724. ' Include(vg,red);',
  6725. ' Include(result,blue);',
  6726. ' vg:=vg;',
  6727. ' vj:=vh;',
  6728. ' vi:=vi;',
  6729. ' doit(vg,vg,vg);',
  6730. ' doit(vh,vh,vj);',
  6731. ' doit(vi,vi,vi);',
  6732. ' doit(vj,vj,vj);',
  6733. 'end;',
  6734. 'var i: TEnums;',
  6735. 'begin',
  6736. ' doit(i,i,i);']);
  6737. ConvertProgram;
  6738. CheckSource('TestSet_AsParams',
  6739. LinesToStr([ // statements
  6740. 'this.TEnum = {',
  6741. ' "0": "Red",',
  6742. ' Red: 0,',
  6743. ' "1": "Blue",',
  6744. ' Blue: 1',
  6745. '};',
  6746. 'this.DoIt = function (vG,vH,vI) {',
  6747. ' var Result = {};',
  6748. ' var vJ = {};',
  6749. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6750. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6751. ' vG = rtl.refSet(vG);',
  6752. ' vJ = rtl.refSet(vH);',
  6753. ' vI.set(rtl.refSet(vI.get()));',
  6754. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6755. ' get: function () {',
  6756. ' return vG;',
  6757. ' },',
  6758. ' set: function (v) {',
  6759. ' vG = v;',
  6760. ' }',
  6761. ' });',
  6762. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6763. ' get: function () {',
  6764. ' return vJ;',
  6765. ' },',
  6766. ' set: function (v) {',
  6767. ' vJ = v;',
  6768. ' }',
  6769. ' });',
  6770. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6771. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6772. ' get: function () {',
  6773. ' return vJ;',
  6774. ' },',
  6775. ' set: function (v) {',
  6776. ' vJ = v;',
  6777. ' }',
  6778. ' });',
  6779. ' return Result;',
  6780. '};',
  6781. 'this.i = {};'
  6782. ]),
  6783. LinesToStr([
  6784. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6785. ' p: $mod,',
  6786. ' get: function () {',
  6787. ' return this.p.i;',
  6788. ' },',
  6789. ' set: function (v) {',
  6790. ' this.p.i = v;',
  6791. ' }',
  6792. '});'
  6793. ]));
  6794. end;
  6795. procedure TTestModule.TestSet_Property;
  6796. begin
  6797. StartProgram(false);
  6798. Add('type');
  6799. Add(' TEnum = (Red,Blue);');
  6800. Add(' TEnums = set of TEnum;');
  6801. Add(' TObject = class');
  6802. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6803. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6804. Add(' property Colors: TEnums read GetColors write SetColors;');
  6805. Add(' end;');
  6806. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6807. Add('begin end;');
  6808. Add('var Obj: TObject;');
  6809. Add('begin');
  6810. Add(' Include(Obj.Colors,Red);');
  6811. Add(' Exclude(Obj.Colors,Red);');
  6812. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6813. ConvertProgram;
  6814. CheckSource('TestSet_Property',
  6815. LinesToStr([ // statements
  6816. 'this.TEnum = {',
  6817. ' "0": "Red",',
  6818. ' Red: 0,',
  6819. ' "1": "Blue",',
  6820. ' Blue: 1',
  6821. '};',
  6822. 'rtl.createClass(this, "TObject", null, function () {',
  6823. ' this.$init = function () {',
  6824. ' };',
  6825. ' this.$final = function () {',
  6826. ' };',
  6827. '});',
  6828. 'this.DoIt = function (i, j, k, l) {',
  6829. '};',
  6830. 'this.Obj = null;',
  6831. '']),
  6832. LinesToStr([
  6833. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6834. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6835. '']));
  6836. end;
  6837. procedure TTestModule.TestSet_EnumConst;
  6838. begin
  6839. StartProgram(false);
  6840. Add([
  6841. 'type',
  6842. ' TEnum = (Red,Blue);',
  6843. ' TEnums = set of TEnum;',
  6844. 'const',
  6845. ' Orange = red;',
  6846. 'var',
  6847. ' Enum: tenum;',
  6848. ' Enums: tenums;',
  6849. 'begin',
  6850. ' Include(enums,orange);',
  6851. ' Exclude(enums,orange);',
  6852. ' if orange in enums then;',
  6853. ' if orange in [orange,red] then;']);
  6854. ConvertProgram;
  6855. CheckSource('TestSet_EnumConst',
  6856. LinesToStr([ // statements
  6857. 'this.TEnum = {',
  6858. ' "0": "Red",',
  6859. ' Red: 0,',
  6860. ' "1": "Blue",',
  6861. ' Blue: 1',
  6862. '};',
  6863. 'this.Orange = this.TEnum.Red;',
  6864. 'this.Enum = 0;',
  6865. 'this.Enums = {};',
  6866. '']),
  6867. LinesToStr([
  6868. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6869. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6870. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6871. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6872. '']));
  6873. end;
  6874. procedure TTestModule.TestSet_IntConst;
  6875. begin
  6876. StartProgram(false);
  6877. Add([
  6878. 'type',
  6879. ' TEnums = set of Byte;',
  6880. 'const',
  6881. ' Orange = 0;',
  6882. 'var',
  6883. ' Enum: byte;',
  6884. ' Enums: tenums;',
  6885. 'begin',
  6886. ' Enums:=[];',
  6887. ' Enums:=[0];',
  6888. ' Enums:=[1..2];',
  6889. //' Include(enums,orange);',
  6890. //' Exclude(enums,orange);',
  6891. ' if orange in enums then;',
  6892. ' if orange in [orange,1] then;']);
  6893. ConvertProgram;
  6894. CheckSource('TestSet_IntConst',
  6895. LinesToStr([ // statements
  6896. 'this.Orange = 0;',
  6897. 'this.Enum = 0;',
  6898. 'this.Enums = {};',
  6899. '']),
  6900. LinesToStr([
  6901. '$mod.Enums = {};',
  6902. '$mod.Enums = rtl.createSet(0);',
  6903. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6904. 'if (0 in $mod.Enums) ;',
  6905. 'if (0 in rtl.createSet(0, 1)) ;',
  6906. '']));
  6907. end;
  6908. procedure TTestModule.TestSet_IntRange;
  6909. begin
  6910. StartProgram(false);
  6911. Add([
  6912. 'type',
  6913. ' TRange = 1..3;',
  6914. ' TEnums = set of TRange;',
  6915. 'const',
  6916. ' Orange = 2;',
  6917. 'var',
  6918. ' Enum: byte;',
  6919. ' Enums: TEnums;',
  6920. 'begin',
  6921. ' Enums:=[];',
  6922. ' Enums:=[1];',
  6923. ' Enums:=[2..3];',
  6924. ' Include(enums,orange);',
  6925. ' Exclude(enums,orange);',
  6926. ' if orange in enums then;',
  6927. ' if orange in [orange,1] then;']);
  6928. ConvertProgram;
  6929. CheckSource('TestSet_IntRange',
  6930. LinesToStr([ // statements
  6931. 'this.Orange = 2;',
  6932. 'this.Enum = 0;',
  6933. 'this.Enums = {};',
  6934. '']),
  6935. LinesToStr([
  6936. '$mod.Enums = {};',
  6937. '$mod.Enums = rtl.createSet(1);',
  6938. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6939. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6940. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6941. 'if (2 in $mod.Enums) ;',
  6942. 'if (2 in rtl.createSet(2, 1)) ;',
  6943. '']));
  6944. end;
  6945. procedure TTestModule.TestSet_AnonymousEnumType;
  6946. begin
  6947. StartProgram(false);
  6948. Add('type');
  6949. Add(' TFlags = set of (red, green);');
  6950. Add('const');
  6951. Add(' favorite = red;');
  6952. Add('var');
  6953. Add(' f: TFlags;');
  6954. Add(' i: longint;');
  6955. Add('begin');
  6956. Add(' Include(f,red);');
  6957. Add(' Include(f,favorite);');
  6958. Add(' i:=ord(red);');
  6959. Add(' i:=ord(favorite);');
  6960. Add(' i:=ord(low(TFlags));');
  6961. Add(' i:=ord(low(f));');
  6962. Add(' i:=ord(low(favorite));');
  6963. Add(' i:=ord(high(TFlags));');
  6964. Add(' i:=ord(high(f));');
  6965. Add(' i:=ord(high(favorite));');
  6966. Add(' f:=[green,favorite];');
  6967. ConvertProgram;
  6968. CheckSource('TestSet_AnonymousEnumType',
  6969. LinesToStr([ // statements
  6970. 'this.TFlags$a = {',
  6971. ' "0": "red",',
  6972. ' red: 0,',
  6973. ' "1": "green",',
  6974. ' green: 1',
  6975. '};',
  6976. 'this.favorite = this.TFlags$a.red;',
  6977. 'this.f = {};',
  6978. 'this.i = 0;',
  6979. '']),
  6980. LinesToStr([
  6981. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6982. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6983. '$mod.i = $mod.TFlags$a.red;',
  6984. '$mod.i = $mod.TFlags$a.red;',
  6985. '$mod.i = $mod.TFlags$a.red;',
  6986. '$mod.i = $mod.TFlags$a.red;',
  6987. '$mod.i = $mod.TFlags$a.red;',
  6988. '$mod.i = $mod.TFlags$a.green;',
  6989. '$mod.i = $mod.TFlags$a.green;',
  6990. '$mod.i = $mod.TFlags$a.green;',
  6991. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6992. '']));
  6993. end;
  6994. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6995. begin
  6996. exit;
  6997. StartProgram(false);
  6998. Add([
  6999. 'type',
  7000. ' TAtoZ = ''A''..''Z'';',
  7001. ' TSetOfAZ = set of TAtoZ;',
  7002. 'var',
  7003. ' c: AnsiChar;',
  7004. ' a: TAtoZ;',
  7005. ' s: TSetOfAZ = [''P'',''A''];',
  7006. ' i: longint;',
  7007. 'begin',
  7008. ' Include(s,''S'');',
  7009. ' Include(s,c);',
  7010. ' Include(s,a);',
  7011. ' c:=low(TAtoZ);',
  7012. ' i:=ord(low(TAtoZ));',
  7013. ' a:=high(TAtoZ);',
  7014. ' a:=high(TSetOfAtoZ);',
  7015. ' s:=[a,c,''M''];',
  7016. '']);
  7017. ConvertProgram;
  7018. CheckSource('TestSet_AnonymousEnumTypeChar',
  7019. LinesToStr([ // statements
  7020. '']),
  7021. LinesToStr([
  7022. '']));
  7023. end;
  7024. procedure TTestModule.TestSet_ConstEnum;
  7025. begin
  7026. StartProgram(false);
  7027. Add([
  7028. 'type',
  7029. ' TEnum = (red,blue,green);',
  7030. ' TEnums = set of TEnum;',
  7031. 'const',
  7032. ' teAny = [low(TEnum)..high(TEnum)];',
  7033. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7034. 'var',
  7035. ' e: TEnum;',
  7036. ' s: TEnums;',
  7037. 'begin',
  7038. ' if blue in teAny then;',
  7039. ' if blue in teAny+[e] then;',
  7040. ' if blue in teAny+teRedBlue then;',
  7041. ' if e in [red,blue] then;',
  7042. ' s:=teAny;',
  7043. ' s:=teAny+[e];',
  7044. ' s:=[e]+teAny;',
  7045. ' s:=teAny+teRedBlue;',
  7046. ' s:=teAny+teRedBlue+[e];',
  7047. '']);
  7048. ConvertProgram;
  7049. CheckSource('TestSet_ConstEnum',
  7050. LinesToStr([ // statements
  7051. 'this.TEnum = {',
  7052. ' "0": "red",',
  7053. ' red: 0,',
  7054. ' "1": "blue",',
  7055. ' blue: 1,',
  7056. ' "2": "green",',
  7057. ' green: 2',
  7058. '};',
  7059. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7060. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7061. 'this.e = 0;',
  7062. 'this.s = {};',
  7063. '']),
  7064. LinesToStr([
  7065. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7066. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7067. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7068. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7069. '$mod.s = rtl.refSet($mod.teAny);',
  7070. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7071. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7072. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7073. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7074. '']));
  7075. end;
  7076. procedure TTestModule.TestSet_ConstChar;
  7077. begin
  7078. StartProgram(false);
  7079. Add([
  7080. 'const',
  7081. ' LowChars = [''a''..''z''];',
  7082. ' Chars = LowChars+[''A''..''Z''];',
  7083. ' sc = [''А'', ''Я''];',
  7084. 'var',
  7085. ' c: AnsiChar;',
  7086. ' s: string;',
  7087. 'begin',
  7088. ' if c in lowchars then ;',
  7089. ' if ''a'' in lowchars then ;',
  7090. ' if s[1] in lowchars then ;',
  7091. ' if c in chars then ;',
  7092. ' if c in [''a''..''z'',''_''] then ;',
  7093. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7094. ' if ''Я'' in sc then ;',
  7095. ' if 3=ord('' '') then ;',
  7096. '']);
  7097. ConvertProgram;
  7098. CheckSource('TestSet_ConstChar',
  7099. LinesToStr([ // statements
  7100. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7101. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7102. 'this.sc = rtl.createSet(1040, 1071);',
  7103. 'this.c = "";',
  7104. 'this.s = "";',
  7105. '']),
  7106. LinesToStr([
  7107. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7108. 'if (97 in $mod.LowChars) ;',
  7109. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7110. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7111. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7112. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7113. 'if (1071 in $mod.sc) ;',
  7114. 'if (3 === 32) ;',
  7115. '']));
  7116. end;
  7117. procedure TTestModule.TestSet_ConstInt;
  7118. begin
  7119. StartProgram(false);
  7120. Add([
  7121. 'const',
  7122. ' Months = [1..12];',
  7123. ' Mirror = [-12..-1]+Months;',
  7124. 'var',
  7125. ' i: smallint;',
  7126. 'begin',
  7127. ' if 3 in Months then;',
  7128. ' if i in Months+[i] then;',
  7129. ' if i in Months+Mirror then;',
  7130. ' if i in [4..6,8] then;',
  7131. '']);
  7132. ConvertProgram;
  7133. CheckSource('TestSet_ConstInt',
  7134. LinesToStr([ // statements
  7135. 'this.Months = rtl.createSet(null, 1, 12);',
  7136. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7137. 'this.i = 0;',
  7138. '']),
  7139. LinesToStr([
  7140. 'if (3 in $mod.Months) ;',
  7141. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7142. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7143. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7144. '']));
  7145. end;
  7146. procedure TTestModule.TestSet_InFunction;
  7147. begin
  7148. StartProgram(false);
  7149. Add([
  7150. 'const',
  7151. ' TEnum = 3;',
  7152. ' TSetOfEnum = 4;',
  7153. ' TSetOfAno = 5;',
  7154. 'procedure DoIt;',
  7155. 'type',
  7156. ' TEnum = (red, blue);',
  7157. ' TSetOfEnum = set of TEnum;',
  7158. ' TSetOfAno = set of (up,down);',
  7159. 'var',
  7160. ' e: TEnum;',
  7161. ' se: TSetOfEnum;',
  7162. ' sa: TSetOfAno;',
  7163. 'begin',
  7164. ' se:=[e];',
  7165. ' sa:=[up];',
  7166. 'end;',
  7167. 'begin',
  7168. '']);
  7169. ConvertProgram;
  7170. CheckSource('TestSet_InFunction',
  7171. LinesToStr([ // statements
  7172. 'this.TEnum = 3;',
  7173. 'this.TSetOfEnum = 4;',
  7174. 'this.TSetOfAno = 5;',
  7175. 'var TEnum$1 = {',
  7176. ' "0": "red",',
  7177. ' red: 0,',
  7178. ' "1": "blue",',
  7179. ' blue: 1',
  7180. '};',
  7181. 'var TSetOfAno$a = {',
  7182. ' "0": "up",',
  7183. ' up: 0,',
  7184. ' "1": "down",',
  7185. ' down: 1',
  7186. '};',
  7187. 'this.DoIt = function () {',
  7188. ' var e = 0;',
  7189. ' var se = {};',
  7190. ' var sa = {};',
  7191. ' se = rtl.createSet(e);',
  7192. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7193. '};',
  7194. '']),
  7195. LinesToStr([
  7196. '']));
  7197. end;
  7198. procedure TTestModule.TestSet_ForIn;
  7199. begin
  7200. StartProgram(false);
  7201. Add([
  7202. 'type',
  7203. ' TEnum = (Red, Green, Blue);',
  7204. ' TEnumRg = green..blue;',
  7205. ' TSetOfEnum = set of TEnum;',
  7206. ' TSetOfEnumRg = set of TEnumRg;',
  7207. 'var',
  7208. ' e, e2: TEnum;',
  7209. ' er: TEnum;',
  7210. ' s: TSetOfEnum;',
  7211. 'begin',
  7212. ' for e in TSetOfEnum do ;',
  7213. ' for e in TSetOfEnumRg do ;',
  7214. ' for e in [] do e2:=e;',
  7215. ' for e in [red..green] do e2:=e;',
  7216. ' for e in [green,blue] do e2:=e;',
  7217. ' for e in [red,blue] do e2:=e;',
  7218. ' for e in s do e2:=e;',
  7219. ' for er in TSetOfEnumRg do ;',
  7220. '']);
  7221. ConvertProgram;
  7222. CheckSource('TestSet_ForIn',
  7223. LinesToStr([ // statements
  7224. 'this.TEnum = {',
  7225. ' "0":"Red",',
  7226. ' Red:0,',
  7227. ' "1":"Green",',
  7228. ' Green:1,',
  7229. ' "2":"Blue",',
  7230. ' Blue:2',
  7231. ' };',
  7232. 'this.e = 0;',
  7233. 'this.e2 = 0;',
  7234. 'this.er = 0;',
  7235. 'this.s = {};',
  7236. '']),
  7237. LinesToStr([
  7238. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7239. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7240. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7241. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7242. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7243. 'for (var $l in $mod.s){',
  7244. ' $mod.e = +$l;',
  7245. ' $mod.e2 = $mod.e;',
  7246. '};',
  7247. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7248. '']));
  7249. end;
  7250. procedure TTestModule.TestNestBegin;
  7251. begin
  7252. StartProgram(false);
  7253. Add('begin');
  7254. Add(' begin');
  7255. Add(' begin');
  7256. Add(' end;');
  7257. Add(' begin');
  7258. Add(' if true then ;');
  7259. Add(' end;');
  7260. Add(' end;');
  7261. ConvertProgram;
  7262. CheckSource('TestNestBegin',
  7263. '',
  7264. 'if (true) ;');
  7265. end;
  7266. procedure TTestModule.TestUnitImplVars;
  7267. begin
  7268. StartUnit(false);
  7269. Add('interface');
  7270. Add('implementation');
  7271. Add('var');
  7272. Add(' V1:longint;');
  7273. Add(' V2:longint = 3;');
  7274. Add(' V3:string = ''abc'';');
  7275. ConvertUnit;
  7276. CheckSource('TestUnitImplVars',
  7277. LinesToStr([ // statements
  7278. 'var $impl = $mod.$impl;',
  7279. '']),
  7280. '', // this.$init
  7281. LinesToStr([ // implementation
  7282. '$impl.V1 = 0;',
  7283. '$impl.V2 = 3;',
  7284. '$impl.V3 = "abc";',
  7285. '']) );
  7286. end;
  7287. procedure TTestModule.TestUnitImplConsts;
  7288. begin
  7289. StartUnit(false);
  7290. Add('interface');
  7291. Add('implementation');
  7292. Add('const');
  7293. Add(' v1 = 3;');
  7294. Add(' v2:longint = 4;');
  7295. Add(' v3:string = ''abc'';');
  7296. ConvertUnit;
  7297. CheckSource('TestUnitImplConsts',
  7298. LinesToStr([ // statements
  7299. 'var $impl = $mod.$impl;',
  7300. '']),
  7301. '', // this.$init
  7302. LinesToStr([ // implementation
  7303. '$impl.v1 = 3;',
  7304. '$impl.v2 = 4;',
  7305. '$impl.v3 = "abc";',
  7306. '']) );
  7307. end;
  7308. procedure TTestModule.TestUnitImplRecord;
  7309. begin
  7310. StartUnit(false);
  7311. Add('interface');
  7312. Add('implementation');
  7313. Add('type');
  7314. Add(' TMyRecord = record');
  7315. Add(' i: longint;');
  7316. Add(' end;');
  7317. Add('var aRec: TMyRecord;');
  7318. Add('initialization');
  7319. Add(' arec.i:=3;');
  7320. ConvertUnit;
  7321. CheckSource('TestUnitImplRecord',
  7322. LinesToStr([ // statements
  7323. 'var $impl = $mod.$impl;',
  7324. '']),
  7325. // this.$init
  7326. '$impl.aRec.i = 3;',
  7327. LinesToStr([ // implementation
  7328. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7329. ' this.i = 0;',
  7330. ' this.$eq = function (b) {',
  7331. ' return this.i === b.i;',
  7332. ' };',
  7333. ' this.$assign = function (s) {',
  7334. ' this.i = s.i;',
  7335. ' return this;',
  7336. ' };',
  7337. '});',
  7338. '$impl.aRec = $impl.TMyRecord.$new();',
  7339. '']) );
  7340. end;
  7341. procedure TTestModule.TestRenameJSNameConflict;
  7342. begin
  7343. StartProgram(false);
  7344. Add('var apply: longint;');
  7345. Add('var bind: longint;');
  7346. Add('var call: longint;');
  7347. Add('begin');
  7348. ConvertProgram;
  7349. CheckSource('TestRenameJSNameConflict',
  7350. LinesToStr([ // statements
  7351. 'this.Apply = 0;',
  7352. 'this.Bind = 0;',
  7353. 'this.Call = 0;'
  7354. ]),
  7355. LinesToStr([ // this.$main
  7356. ''
  7357. ]));
  7358. end;
  7359. procedure TTestModule.TestLocalConst;
  7360. begin
  7361. StartProgram(false);
  7362. Add('procedure DoIt;');
  7363. Add('const');
  7364. Add(' cA: longint = 1;');
  7365. Add(' cB = 2;');
  7366. Add(' procedure Sub;');
  7367. Add(' const');
  7368. Add(' csA = 3;');
  7369. Add(' cB: double = 4;');
  7370. Add(' begin');
  7371. Add(' cb:=cb+csa;');
  7372. Add(' ca:=ca+csa+5;');
  7373. Add(' end;');
  7374. Add('begin');
  7375. Add(' ca:=ca+cb+6;');
  7376. Add('end;');
  7377. Add('begin');
  7378. ConvertProgram;
  7379. CheckSource('TestLocalConst',
  7380. LinesToStr([
  7381. 'var cA = 1;',
  7382. 'var cB = 2;',
  7383. 'var csA = 3;',
  7384. 'var cB$1 = 4;',
  7385. 'this.DoIt = function () {',
  7386. ' function Sub() {',
  7387. ' cB$1 = cB$1 + 3;',
  7388. ' cA = cA + 3 + 5;',
  7389. ' };',
  7390. ' cA = cA + 2 + 6;',
  7391. '};'
  7392. ]),
  7393. LinesToStr([
  7394. ]));
  7395. end;
  7396. procedure TTestModule.TestVarExternal;
  7397. begin
  7398. StartProgram(false);
  7399. Add('var');
  7400. Add(' NaN: double; external name ''Global.NaN'';');
  7401. Add(' d: double;');
  7402. Add('begin');
  7403. Add(' d:=NaN;');
  7404. ConvertProgram;
  7405. CheckSource('TestVarExternal',
  7406. LinesToStr([
  7407. 'this.d = 0.0;'
  7408. ]),
  7409. LinesToStr([
  7410. '$mod.d = Global.NaN;'
  7411. ]));
  7412. end;
  7413. procedure TTestModule.TestVarExternalOtherUnit;
  7414. begin
  7415. AddModuleWithIntfImplSrc('unit2.pas',
  7416. LinesToStr([
  7417. 'var NaN: double; external name ''Global.NaN'';',
  7418. 'var iV: longint;'
  7419. ]),
  7420. '');
  7421. StartUnit(true);
  7422. Add('interface');
  7423. Add('uses unit2;');
  7424. Add('implementation');
  7425. Add('var');
  7426. Add(' d: double;');
  7427. Add(' i: longint; external name ''$i'';');
  7428. Add('begin');
  7429. Add(' d:=nan;');
  7430. Add(' d:=uNit2.nan;');
  7431. Add(' d:=test1.d;');
  7432. Add(' i:=iv;');
  7433. Add(' i:=uNit2.iv;');
  7434. Add(' i:=test1.i;');
  7435. ConvertUnit;
  7436. CheckSource('TestVarExternalOtherUnit',
  7437. LinesToStr([
  7438. 'var $impl = $mod.$impl;',
  7439. '']),
  7440. LinesToStr([ // this.$init
  7441. '$impl.d = Global.NaN;',
  7442. '$impl.d = Global.NaN;',
  7443. '$impl.d = $impl.d;',
  7444. '$i = pas.unit2.iV;',
  7445. '$i = pas.unit2.iV;',
  7446. '$i = $i;',
  7447. '']),
  7448. LinesToStr([ // implementation
  7449. '$impl.d = 0.0;',
  7450. '']) );
  7451. end;
  7452. procedure TTestModule.TestVarAbsoluteFail;
  7453. begin
  7454. StartProgram(false);
  7455. Add([
  7456. 'var',
  7457. ' a: longint;',
  7458. ' b: longword absolute a;',
  7459. 'begin']);
  7460. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7461. ConvertProgram;
  7462. end;
  7463. procedure TTestModule.TestConstExternal;
  7464. begin
  7465. StartProgram(false);
  7466. Add([
  7467. 'const',
  7468. ' PI: double; external name ''Global.PI'';',
  7469. ' Tau = 2*pi;',
  7470. 'var d: double;',
  7471. 'begin',
  7472. ' d:=pi;',
  7473. ' d:=tau+pi;']);
  7474. ConvertProgram;
  7475. CheckSource('TestConstExternal',
  7476. LinesToStr([
  7477. 'this.Tau = 2*Global.PI;',
  7478. 'this.d = 0.0;'
  7479. ]),
  7480. LinesToStr([
  7481. '$mod.d = Global.PI;',
  7482. '$mod.d = $mod.Tau + Global.PI;'
  7483. ]));
  7484. end;
  7485. procedure TTestModule.TestDouble;
  7486. begin
  7487. StartProgram(false);
  7488. Add([
  7489. 'type',
  7490. ' TDateTime = double;',
  7491. 'const',
  7492. ' a = TDateTime(2.7);',
  7493. ' b = a + TDateTime(1.7);',
  7494. ' c = 0.9 + 0.1;',
  7495. ' f0_1 = 0.1;',
  7496. ' f0_3 = 0.3;',
  7497. ' fn0_1 = -0.1;',
  7498. ' fn0_3 = -0.3;',
  7499. ' fn0_003 = -0.003;',
  7500. ' fn0_123456789 = -0.123456789;',
  7501. ' fn300_0 = -300.0;',
  7502. ' fn123456_0 = -123456.0;',
  7503. ' fn1234567_8 = -1234567.8;',
  7504. ' fn12345678_9 = -12345678.9;',
  7505. ' f1_0En12 = 1E-12;',
  7506. ' fn1_0En12 = -1E-12;',
  7507. ' maxdouble = 1.7e+308;',
  7508. ' mindouble = -1.7e+308;',
  7509. ' MinSafeIntDouble = -$1fffffffffffff;',
  7510. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7511. ' MaxSafeIntDouble = $1fffffffffffff;',
  7512. ' DZeroResolution = 1E-12;',
  7513. ' Minus1 = -1E-12;',
  7514. ' EPS = 1E-9;',
  7515. ' DELTA = 0.001;',
  7516. ' Big = 129.789E+100;',
  7517. ' Test0_15 = 0.15;',
  7518. ' Test999 = 2.9999999999999;',
  7519. ' Test111999 = 211199999999999000.0;',
  7520. ' TestMinus111999 = -211199999999999000.0;',
  7521. ' Inf = 1.0 / 0.0;',
  7522. ' NegInf = -1.0 / 0.0;',
  7523. 'procedure Run(d: double); external name ''Run'';',
  7524. 'var',
  7525. ' d: double = b;',
  7526. 'begin',
  7527. ' d:=1.0;',
  7528. ' d:=1.0/3.0;',
  7529. ' d:=1.0/(3-2-1);',
  7530. ' d:=1/3;',
  7531. ' d:=5.0E-324;',
  7532. ' d:=1.7E308;',
  7533. ' d:=001.00E00;',
  7534. ' d:=002.00E001;',
  7535. ' d:=003.000E000;',
  7536. ' d:=-004.00E-00;',
  7537. ' d:=-005.00E-001;',
  7538. ' d:=10**3;',
  7539. ' d:=10 mod 3;',
  7540. ' d:=10 div 3;',
  7541. ' d:=c;',
  7542. ' d:=f0_1;',
  7543. ' d:=f0_3;',
  7544. ' d:=fn0_1;',
  7545. ' d:=fn0_3;',
  7546. ' d:=fn0_003;',
  7547. ' d:=fn0_123456789;',
  7548. ' d:=fn300_0;',
  7549. ' d:=fn123456_0;',
  7550. ' d:=fn1234567_8;',
  7551. ' d:=fn12345678_9;',
  7552. ' d:=f1_0En12;',
  7553. ' d:=fn1_0En12;',
  7554. ' d:=maxdouble;',
  7555. ' d:=mindouble;',
  7556. ' d:=MinSafeIntDouble;',
  7557. ' d:=double(MinSafeIntDouble);',
  7558. ' d:=MinSafeIntDouble2;',
  7559. ' d:=double(MinSafeIntDouble2);',
  7560. ' d:=MaxSafeIntDouble;',
  7561. ' d:=default(double);',
  7562. ' Run(Inf);',
  7563. ' Run(NegInf);',
  7564. '']);
  7565. ConvertProgram;
  7566. CheckSource('TestDouble',
  7567. LinesToStr([
  7568. 'this.a = 2.7;',
  7569. 'this.b = 2.7 + 1.7;',
  7570. 'this.c = 0.9 + 0.1;',
  7571. 'this.f0_1 = 0.1;',
  7572. 'this.f0_3 = 0.3;',
  7573. 'this.fn0_1 = -0.1;',
  7574. 'this.fn0_3 = -0.3;',
  7575. 'this.fn0_003 = -0.003;',
  7576. 'this.fn0_123456789 = -0.123456789;',
  7577. 'this.fn300_0 = -300.0;',
  7578. 'this.fn123456_0 = -123456.0;',
  7579. 'this.fn1234567_8 = -1234567.8;',
  7580. 'this.fn12345678_9 = -12345678.9;',
  7581. 'this.f1_0En12 = 1E-12;',
  7582. 'this.fn1_0En12 = -1E-12;',
  7583. 'this.maxdouble = 1.7e+308;',
  7584. 'this.mindouble = -1.7e+308;',
  7585. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7586. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7587. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7588. 'this.DZeroResolution = 1E-12;',
  7589. 'this.Minus1 = -1E-12;',
  7590. 'this.EPS = 1E-9;',
  7591. 'this.DELTA = 0.001;',
  7592. 'this.Big = 129.789E+100;',
  7593. 'this.Test0_15 = 0.15;',
  7594. 'this.Test999 = 2.9999999999999;',
  7595. 'this.Test111999 = 211199999999999000.0;',
  7596. 'this.TestMinus111999 = -211199999999999000.0;',
  7597. 'this.Inf = 1.0 / 0.0;',
  7598. 'this.NegInf = -1.0 / 0.0;',
  7599. 'this.d = 4.4;',
  7600. '']),
  7601. LinesToStr([
  7602. '$mod.d = 1.0;',
  7603. '$mod.d = 1.0 / 3.0;',
  7604. '$mod.d = 1.0 / (3 - 2 - 1);',
  7605. '$mod.d = 1 / 3;',
  7606. '$mod.d = 5.0E-324;',
  7607. '$mod.d = 1.7E308;',
  7608. '$mod.d = 1.00E0;',
  7609. '$mod.d = 2.00E1;',
  7610. '$mod.d = 3.000E0;',
  7611. '$mod.d = -4.00E-0;',
  7612. '$mod.d = -5.00E-1;',
  7613. '$mod.d = Math.pow(10, 3);',
  7614. '$mod.d = 10 % 3;',
  7615. '$mod.d = rtl.trunc(10 / 3);',
  7616. '$mod.d = 1;',
  7617. '$mod.d = 0.1;',
  7618. '$mod.d = 0.3;',
  7619. '$mod.d = -0.1;',
  7620. '$mod.d = -0.3;',
  7621. '$mod.d = -3E-3;',
  7622. '$mod.d = -0.123456789;',
  7623. '$mod.d = -300;',
  7624. '$mod.d = -123456;',
  7625. '$mod.d = -1234567.8;',
  7626. '$mod.d = -1.23456789E7;',
  7627. '$mod.d = 1E-12;',
  7628. '$mod.d = -1E-12;',
  7629. '$mod.d = 1.7E308;',
  7630. '$mod.d = -1.7E308;',
  7631. '$mod.d = -9007199254740991;',
  7632. '$mod.d = -9007199254740991;',
  7633. '$mod.d = -9.007199254740992E15;',
  7634. '$mod.d = -9.007199254740992E15;',
  7635. '$mod.d = 9007199254740991;',
  7636. '$mod.d = 0.0;',
  7637. 'Run(1 / 0);',
  7638. 'Run(-1 / 0);',
  7639. '']));
  7640. end;
  7641. procedure TTestModule.TestDoubleSmall;
  7642. begin
  7643. StartProgram(false);
  7644. Add([
  7645. 'const',
  7646. ' a = 1e-1;',
  7647. ' b = 1e-2;',
  7648. ' c = 1e-3;',
  7649. ' d = 1e-4;',
  7650. ' e = 1e-5;',
  7651. ' f = 1e-6;',
  7652. ' g = 1e-7;',
  7653. ' h = -1e-1;',
  7654. ' i = -1e-2;',
  7655. 'procedure Fly(d: double);',
  7656. 'begin',
  7657. 'end;',
  7658. 'begin',
  7659. ' Fly(a);',
  7660. ' Fly(b);',
  7661. ' Fly(c);',
  7662. ' Fly(d);',
  7663. ' Fly(e);',
  7664. ' Fly(f);',
  7665. ' Fly(g);',
  7666. ' Fly(h);',
  7667. ' Fly(i);',
  7668. '']);
  7669. ConvertProgram;
  7670. CheckSource('TestDoubleSmall',
  7671. LinesToStr([
  7672. 'this.a = 1e-1;',
  7673. 'this.b = 1e-2;',
  7674. 'this.c = 1e-3;',
  7675. 'this.d = 1e-4;',
  7676. 'this.e = 1e-5;',
  7677. 'this.f = 1e-6;',
  7678. 'this.g = 1e-7;',
  7679. 'this.h = -1e-1;',
  7680. 'this.i = -1e-2;',
  7681. 'this.Fly = function (d) {',
  7682. '};',
  7683. '']),
  7684. LinesToStr([
  7685. '$mod.Fly(0.1);',
  7686. '$mod.Fly(0.01);',
  7687. '$mod.Fly(1E-3);',
  7688. '$mod.Fly(1E-4);',
  7689. '$mod.Fly(1E-5);',
  7690. '$mod.Fly(1E-6);',
  7691. '$mod.Fly(1E-7);',
  7692. '$mod.Fly(-0.1);',
  7693. '$mod.Fly(-0.01);',
  7694. '']));
  7695. end;
  7696. procedure TTestModule.TestInteger;
  7697. begin
  7698. StartProgram(false);
  7699. Add([
  7700. 'const',
  7701. ' MinInt = low(NativeInt);',
  7702. ' MaxInt = high(NativeInt);',
  7703. 'type',
  7704. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7705. 'const',
  7706. ' a = low(TMyInt)+High(TMyInt);',
  7707. 'var',
  7708. ' i: TMyInt;',
  7709. 'begin',
  7710. ' i:=-MinInt;',
  7711. ' i:=default(TMyInt);',
  7712. ' i:=low(i)+high(i);',
  7713. '']);
  7714. ConvertProgram;
  7715. CheckSource('TestIntegerRange',
  7716. LinesToStr([
  7717. 'this.MinInt = -9007199254740991;',
  7718. 'this.MaxInt = 9007199254740991;',
  7719. 'this.a = -9007199254740991 + 9007199254740991;',
  7720. 'this.i = 0;',
  7721. '']),
  7722. LinesToStr([
  7723. '$mod.i = - -9007199254740991;',
  7724. '$mod.i = -9007199254740991;',
  7725. '$mod.i = -9007199254740991 + 9007199254740991;',
  7726. '']));
  7727. end;
  7728. procedure TTestModule.TestIntegerRange;
  7729. begin
  7730. StartProgram(false);
  7731. Add([
  7732. 'const',
  7733. ' MinInt = -1;',
  7734. ' MaxInt = +1;',
  7735. 'type',
  7736. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7737. ' TInt2 = 1..3;',
  7738. 'const',
  7739. ' a = low(TMyInt)+High(TMyInt);',
  7740. ' b = low(TInt2)+High(TInt2);',
  7741. ' s1 = [1];',
  7742. ' s2 = [1,2];',
  7743. ' s3 = [1..3];',
  7744. ' s4 = [low(shortint)..high(shortint)];',
  7745. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7746. ' s6 = 1 in s2;',
  7747. 'var',
  7748. ' i: TMyInt;',
  7749. ' i2: TInt2;',
  7750. 'begin',
  7751. ' i:=i2;',
  7752. ' i:=default(TMyInt);',
  7753. ' if i=i2 then ;',
  7754. ' i:=ord(i2);',
  7755. '']);
  7756. ConvertProgram;
  7757. CheckSource('TestIntegerRange',
  7758. LinesToStr([
  7759. 'this.MinInt = -1;',
  7760. 'this.MaxInt = +1;',
  7761. 'this.a = -1 + 1;',
  7762. 'this.b = 1 + 3;',
  7763. 'this.s1 = rtl.createSet(1);',
  7764. 'this.s2 = rtl.createSet(1, 2);',
  7765. 'this.s3 = rtl.createSet(null, 1, 3);',
  7766. 'this.s4 = rtl.createSet(null, -128, 127);',
  7767. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7768. 'this.s6 = 1 in this.s2;',
  7769. 'this.i = 0;',
  7770. 'this.i2 = 0;',
  7771. '']),
  7772. LinesToStr([
  7773. '$mod.i = $mod.i2;',
  7774. '$mod.i = -1;',
  7775. 'if ($mod.i === $mod.i2) ;',
  7776. '$mod.i = $mod.i2;',
  7777. '']));
  7778. end;
  7779. procedure TTestModule.TestIntegerTypecasts;
  7780. begin
  7781. StartProgram(false);
  7782. Add([
  7783. 'var',
  7784. ' i: nativeint;',
  7785. ' b: byte;',
  7786. ' sh: shortint;',
  7787. ' w: word;',
  7788. ' sm: smallint;',
  7789. ' lw: longword;',
  7790. ' li: longint;',
  7791. 'begin',
  7792. ' b:=byte(i);',
  7793. ' sh:=shortint(i);',
  7794. ' w:=word(i);',
  7795. ' sm:=smallint(i);',
  7796. ' lw:=longword(i);',
  7797. ' li:=longint(i);',
  7798. '']);
  7799. ConvertProgram;
  7800. CheckSource('TestIntegerTypecasts',
  7801. LinesToStr([
  7802. 'this.i = 0;',
  7803. 'this.b = 0;',
  7804. 'this.sh = 0;',
  7805. 'this.w = 0;',
  7806. 'this.sm = 0;',
  7807. 'this.lw = 0;',
  7808. 'this.li = 0;',
  7809. '']),
  7810. LinesToStr([
  7811. '$mod.b = $mod.i & 255;',
  7812. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7813. '$mod.w = $mod.i & 65535;',
  7814. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7815. '$mod.lw = $mod.i >>> 0;',
  7816. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7817. '']));
  7818. end;
  7819. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7820. begin
  7821. StartProgram(false);
  7822. Add([
  7823. 'var',
  7824. ' i,j: nativeint;',
  7825. 'begin',
  7826. ' i:=i shr 0;',
  7827. ' i:=i shr 1;',
  7828. ' i:=i shr 3;',
  7829. ' i:=i shr 54;',
  7830. ' i:=j shr i;',
  7831. '']);
  7832. ConvertProgram;
  7833. CheckResolverUnexpectedHints;
  7834. CheckSource('TestInteger_BitwiseShrNativeInt',
  7835. LinesToStr([
  7836. 'this.i = 0;',
  7837. 'this.j = 0;',
  7838. '']),
  7839. LinesToStr([
  7840. '$mod.i = $mod.i;',
  7841. '$mod.i = Math.floor($mod.i / 2);',
  7842. '$mod.i = Math.floor($mod.i / 8);',
  7843. '$mod.i = 0;',
  7844. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7845. '']));
  7846. end;
  7847. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7848. begin
  7849. StartProgram(false);
  7850. Add([
  7851. 'var',
  7852. ' i: nativeint;',
  7853. 'begin',
  7854. ' i:=i shl 0;',
  7855. ' i:=i shl 54;',
  7856. ' i:=123456789012 shl 1;',
  7857. ' i:=i shl 1;',
  7858. '']);
  7859. ConvertProgram;
  7860. CheckResolverUnexpectedHints;
  7861. CheckSource('TestInteger_BitwiseShrNativeInt',
  7862. LinesToStr([
  7863. 'this.i = 0;',
  7864. '']),
  7865. LinesToStr([
  7866. '$mod.i = $mod.i;',
  7867. '$mod.i = 0;',
  7868. '$mod.i = 246913578024;',
  7869. '$mod.i = rtl.shl($mod.i, 1);',
  7870. '']));
  7871. end;
  7872. procedure TTestModule.TestInteger_SystemFunc;
  7873. begin
  7874. StartProgram(true);
  7875. Add([
  7876. 'var',
  7877. ' i: byte;',
  7878. ' s: string;',
  7879. 'begin',
  7880. ' system.inc(i);',
  7881. ' system.str(i,s);',
  7882. ' s:=system.str(i);',
  7883. ' i:=system.low(i);',
  7884. ' i:=system.high(i);',
  7885. ' i:=system.pred(i);',
  7886. ' i:=system.succ(i);',
  7887. ' i:=system.ord(i);',
  7888. '']);
  7889. ConvertProgram;
  7890. CheckResolverUnexpectedHints;
  7891. CheckSource('TestInteger_SystemFunc',
  7892. LinesToStr([
  7893. 'this.i = 0;',
  7894. 'this.s = "";',
  7895. '']),
  7896. LinesToStr([
  7897. '$mod.i += 1;',
  7898. '$mod.s = "" + $mod.i;',
  7899. '$mod.s = "" + $mod.i;',
  7900. '$mod.i = 0;',
  7901. '$mod.i = 255;',
  7902. '$mod.i = $mod.i - 1;',
  7903. '$mod.i = $mod.i + 1;',
  7904. '$mod.i = $mod.i;',
  7905. '']));
  7906. end;
  7907. procedure TTestModule.TestInteger_AssignOutsideConst;
  7908. begin
  7909. StartProgram(false);
  7910. Add([
  7911. 'const',
  7912. ' MinInt = low(longint);',
  7913. ' MaxInt = high(longint);',
  7914. 'type',
  7915. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7916. 'var',
  7917. ' i: TMyInt;',
  7918. ' aByte: byte;',
  7919. ' aShortInt: shortint;',
  7920. ' aWord: word;',
  7921. ' aSmallInt: smallint;',
  7922. ' aLongWord: longword;',
  7923. ' aLongInt: longint;',
  7924. ' aNativeInt: nativeint;',
  7925. ' aNativeUInt: nativeuint;',
  7926. 'begin',
  7927. ' aByte:=$FF;',
  7928. ' aByte:=$100;',
  7929. ' aByte:=-1;',
  7930. ' aByte:=-127;',
  7931. ' aByte:=-128;',
  7932. ' aByte:=-254;',
  7933. ' aByte:=-255;',
  7934. ' aByte:=-256;',
  7935. ' aShortInt:=127;',
  7936. ' aShortInt:=128;',
  7937. ' aShortInt:=-128;',
  7938. ' aShortInt:=-129;',
  7939. ' aWord:=$ffff;',
  7940. ' aWord:=$10000;',
  7941. ' aWord:=-1;',
  7942. ' aWord:=-$ffff;',
  7943. ' aWord:=-$10000;',
  7944. ' aWord:=-$10001;',
  7945. ' aSmallInt:=$7fff;',
  7946. ' aSmallInt:=$8000;',
  7947. ' aSmallInt:=-$8000;',
  7948. ' aSmallInt:=-$8001;',
  7949. ' aLongWord:=$ffffffff;',
  7950. ' aLongWord:=$100000000;',
  7951. ' aLongWord:=-1;',
  7952. ' aLongWord:=-$ffffffff;',
  7953. ' aNativeInt:=$1fffffffffffff;',
  7954. ' aNativeInt:=-$1fffffffffffff;',
  7955. ' aNativeUInt:=$1fffffffffffff;',
  7956. ' aNativeUInt:=-$1fffffffffffff;',
  7957. '']);
  7958. ConvertProgram;
  7959. CheckSource('TestInteger_AssignOutsideConst',
  7960. LinesToStr([
  7961. 'this.MinInt = -2147483648;',
  7962. 'this.MaxInt = 2147483647;',
  7963. 'this.i = 0;',
  7964. 'this.aByte = 0;',
  7965. 'this.aShortInt = 0;',
  7966. 'this.aWord = 0;',
  7967. 'this.aSmallInt = 0;',
  7968. 'this.aLongWord = 0;',
  7969. 'this.aLongInt = 0;',
  7970. 'this.aNativeInt = 0;',
  7971. 'this.aNativeUInt = 0;',
  7972. '']),
  7973. LinesToStr([
  7974. '$mod.aByte = 0xFF;',
  7975. '$mod.aByte = 0;',
  7976. '$mod.aByte = 255;',
  7977. '$mod.aByte = 129;',
  7978. '$mod.aByte = 128;',
  7979. '$mod.aByte = 2;',
  7980. '$mod.aByte = 1;',
  7981. '$mod.aByte = 0;',
  7982. '$mod.aShortInt = 127;',
  7983. '$mod.aShortInt = -128;',
  7984. '$mod.aShortInt = -128;',
  7985. '$mod.aShortInt = 127;',
  7986. '$mod.aWord = 0xffff;',
  7987. '$mod.aWord = 0;',
  7988. '$mod.aWord = 65535;',
  7989. '$mod.aWord = 1;',
  7990. '$mod.aWord = 0;',
  7991. '$mod.aWord = 65535;',
  7992. '$mod.aSmallInt = 0x7fff;',
  7993. '$mod.aSmallInt = -32768;',
  7994. '$mod.aSmallInt = -0x8000;',
  7995. '$mod.aSmallInt = 32767;',
  7996. '$mod.aLongWord = 0xffffffff;',
  7997. '$mod.aLongWord = 0;',
  7998. '$mod.aLongWord = 4294967295;',
  7999. '$mod.aLongWord = 1;',
  8000. '$mod.aNativeInt = 0x1fffffffffffff;',
  8001. '$mod.aNativeInt = -0x1fffffffffffff;',
  8002. '$mod.aNativeUInt = 0x1fffffffffffff;',
  8003. '$mod.aNativeUInt = 1;',
  8004. '']));
  8005. end;
  8006. procedure TTestModule.TestCurrency;
  8007. begin
  8008. StartProgram(false);
  8009. Add([
  8010. 'type',
  8011. ' TCoin = currency;',
  8012. 'const',
  8013. ' a = TCoin(2.7);',
  8014. ' b = a + TCoin(1.7);',
  8015. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  8016. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  8017. 'var',
  8018. ' c: TCoin = b;',
  8019. ' i: nativeint;',
  8020. ' d: double;',
  8021. ' j: jsvalue;',
  8022. 'function DoIt(c: currency): currency; begin end;',
  8023. 'function GetIt(d: double): double; begin end;',
  8024. 'procedure Write(v: jsvalue); begin end;',
  8025. 'begin',
  8026. ' c:=1.0;',
  8027. ' c:=0.1;',
  8028. ' c:=1.0/3.0;',
  8029. ' c:=1/3;',
  8030. ' c:=a;',
  8031. ' d:=c;',
  8032. ' c:=d;',
  8033. ' c:=currency(c);',
  8034. ' c:=currency(d);',
  8035. ' d:=double(c);',
  8036. ' c:=i;',
  8037. ' c:=currency(i);',
  8038. //' i:=c;', not allowed
  8039. ' i:=nativeint(c);',
  8040. ' c:=c+a;',
  8041. ' c:=-c-a;',
  8042. ' c:=d+c;',
  8043. ' c:=c+d;',
  8044. ' c:=d-c;',
  8045. ' c:=c-d;',
  8046. ' c:=c*a;',
  8047. ' c:=a*c;',
  8048. ' c:=d*c;',
  8049. ' c:=c*d;',
  8050. ' c:=c/a;',
  8051. ' c:=a/c;',
  8052. ' c:=d/c;',
  8053. ' c:=c/d;',
  8054. ' c:=c**a;',
  8055. ' c:=a**c;',
  8056. ' c:=d**c;',
  8057. ' c:=c**d;',
  8058. ' if c=c then ;',
  8059. ' if c=a then ;',
  8060. ' if a=c then ;',
  8061. ' if d=c then ;',
  8062. ' if c=d then ;',
  8063. ' c:=DoIt(c);',
  8064. ' c:=DoIt(i);',
  8065. ' c:=DoIt(d);',
  8066. ' c:=GetIt(c);',
  8067. ' j:=c;',
  8068. ' Write(c);',
  8069. ' c:=default(currency);',
  8070. ' j:=str(c);',
  8071. ' j:=str(c:0:3);',
  8072. '']);
  8073. ConvertProgram;
  8074. CheckSource('TestCurrency',
  8075. LinesToStr([
  8076. 'this.a = 27000;',
  8077. 'this.b = this.a + 17000;',
  8078. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8079. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8080. 'this.c = this.b;',
  8081. 'this.i = 0;',
  8082. 'this.d = 0.0;',
  8083. 'this.j = undefined;',
  8084. 'this.DoIt = function (c) {',
  8085. ' var Result = 0;',
  8086. ' return Result;',
  8087. '};',
  8088. 'this.GetIt = function (d) {',
  8089. ' var Result = 0.0;',
  8090. ' return Result;',
  8091. '};',
  8092. 'this.Write = function (v) {',
  8093. '};',
  8094. '']),
  8095. LinesToStr([
  8096. '$mod.c = 10000;',
  8097. '$mod.c = 1000;',
  8098. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8099. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8100. '$mod.c = $mod.a;',
  8101. '$mod.d = $mod.c / 10000;',
  8102. '$mod.c = rtl.trunc($mod.d * 10000);',
  8103. '$mod.c = $mod.c;',
  8104. '$mod.c = $mod.d * 10000;',
  8105. '$mod.d = $mod.c / 10000;',
  8106. '$mod.c = $mod.i * 10000;',
  8107. '$mod.c = $mod.i * 10000;',
  8108. '$mod.i = rtl.trunc($mod.c / 10000);',
  8109. '$mod.c = $mod.c + $mod.a;',
  8110. '$mod.c = -$mod.c - $mod.a;',
  8111. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8112. '$mod.c = $mod.c + ($mod.d * 10000);',
  8113. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8114. '$mod.c = $mod.c - ($mod.d * 10000);',
  8115. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8116. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8117. '$mod.c = $mod.d * $mod.c;',
  8118. '$mod.c = $mod.c * $mod.d;',
  8119. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8120. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8121. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8122. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8123. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8124. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8125. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8126. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8127. 'if ($mod.c === $mod.c) ;',
  8128. 'if ($mod.c === $mod.a) ;',
  8129. 'if ($mod.a === $mod.c) ;',
  8130. 'if (($mod.d * 10000) === $mod.c) ;',
  8131. 'if ($mod.c === ($mod.d * 10000)) ;',
  8132. '$mod.c = $mod.DoIt($mod.c);',
  8133. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8134. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8135. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8136. '$mod.j = $mod.c / 10000;',
  8137. '$mod.Write($mod.c / 10000);',
  8138. '$mod.c = 0;',
  8139. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8140. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8141. '']));
  8142. end;
  8143. procedure TTestModule.TestForBoolDo;
  8144. begin
  8145. StartProgram(false);
  8146. Add([
  8147. 'var b: boolean;',
  8148. 'begin',
  8149. ' for b:=false to true do ;',
  8150. ' for b:=b downto false do ;',
  8151. ' for b in boolean do ;',
  8152. '']);
  8153. ConvertProgram;
  8154. CheckSource('TestForBoolDo',
  8155. LinesToStr([ // statements
  8156. 'this.b = false;']),
  8157. LinesToStr([ // this.$main
  8158. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8159. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8160. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8161. '']));
  8162. end;
  8163. procedure TTestModule.TestForIntDo;
  8164. begin
  8165. StartProgram(false);
  8166. Add([
  8167. 'var i: longint;',
  8168. 'begin',
  8169. ' for i:=3 to 5 do ;',
  8170. ' for i:=i downto 2 do ;',
  8171. ' for i in byte do ;',
  8172. '']);
  8173. ConvertProgram;
  8174. CheckSource('TestForIntDo',
  8175. LinesToStr([ // statements
  8176. 'this.i = 0;']),
  8177. LinesToStr([ // this.$main
  8178. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8179. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8180. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8181. '']));
  8182. end;
  8183. procedure TTestModule.TestForIntInDo;
  8184. begin
  8185. StartProgram(false);
  8186. Add([
  8187. 'type',
  8188. ' TSetOfInt = set of byte;',
  8189. ' TIntRg = 3..7;',
  8190. ' TSetOfIntRg = set of TIntRg;',
  8191. 'var',
  8192. ' i,i2: longint;',
  8193. ' a1: array of byte;',
  8194. ' a2: array[1..3] of byte;',
  8195. ' soi: TSetOfInt;',
  8196. ' soir: TSetOfIntRg;',
  8197. ' ir: TIntRg;',
  8198. 'begin',
  8199. ' for i in byte do ;',
  8200. ' for i in a1 do ;',
  8201. ' for i in a2 do ;',
  8202. ' for i in [11..13] do ;',
  8203. ' for i in TSetOfInt do ;',
  8204. ' for i in TIntRg do ;',
  8205. ' for i in soi do i2:=i;',
  8206. ' for i in TSetOfIntRg do ;',
  8207. ' for i in soir do ;',
  8208. ' for ir in TIntRg do ;',
  8209. ' for ir in TSetOfIntRg do ;',
  8210. ' for ir in soir do ;',
  8211. '']);
  8212. ConvertProgram;
  8213. CheckSource('TestForIntInDo',
  8214. LinesToStr([ // statements
  8215. 'this.i = 0;',
  8216. 'this.i2 = 0;',
  8217. 'this.a1 = [];',
  8218. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8219. 'this.soi = {};',
  8220. 'this.soir = {};',
  8221. 'this.ir = 0;',
  8222. '']),
  8223. LinesToStr([ // this.$main
  8224. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8225. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8226. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8227. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8228. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8229. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8230. 'for (var $l6 in $mod.soi) {',
  8231. ' $mod.i = +$l6;',
  8232. ' $mod.i2 = $mod.i;',
  8233. '};',
  8234. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8235. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8236. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8237. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8238. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8239. '']));
  8240. end;
  8241. procedure TTestModule.TestCharConst;
  8242. begin
  8243. StartProgram(false);
  8244. Add([
  8245. 'const',
  8246. ' a = #$00F3;',
  8247. ' c: AnsiChar = ''1'';',
  8248. ' wc: widechar = ''ä'';',
  8249. 'begin',
  8250. ' c:=#0;',
  8251. ' c:=#1;',
  8252. ' c:=#9;',
  8253. ' c:=#10;',
  8254. ' c:=#13;',
  8255. ' c:=#31;',
  8256. ' c:=#32;',
  8257. ' c:=#$A;',
  8258. ' c:=#$0A;',
  8259. ' c:=#$b;',
  8260. ' c:=#$0b;',
  8261. ' c:=^A;',
  8262. ' c:=''"'';',
  8263. ' c:=default(AnsiChar);',
  8264. ' c:=#$00E4;', // ä
  8265. ' c:=''ä'';',
  8266. ' c:=#$E4;', // ä
  8267. ' c:=#$D800;', // invalid UTF-16
  8268. ' c:=#$DFFF;', // invalid UTF-16
  8269. ' c:=#$FFFF;', // last UCS-2
  8270. ' c:=high(c);', // last UCS-2
  8271. ' c:=#269;',
  8272. '']);
  8273. ConvertProgram;
  8274. CheckSource('TestCharConst',
  8275. LinesToStr([
  8276. 'this.a="ó";',
  8277. 'this.c="1";',
  8278. 'this.wc="ä";'
  8279. ]),
  8280. LinesToStr([
  8281. '$mod.c="\x00";',
  8282. '$mod.c="\x01";',
  8283. '$mod.c="\t";',
  8284. '$mod.c="\n";',
  8285. '$mod.c="\r";',
  8286. '$mod.c="\x1F";',
  8287. '$mod.c=" ";',
  8288. '$mod.c="\n";',
  8289. '$mod.c="\n";',
  8290. '$mod.c="\x0B";',
  8291. '$mod.c="\x0B";',
  8292. '$mod.c="\x01";',
  8293. '$mod.c=''"'';',
  8294. '$mod.c="\x00";',
  8295. '$mod.c = "ä";',
  8296. '$mod.c = "ä";',
  8297. '$mod.c = "ä";',
  8298. '$mod.c="\uD800";',
  8299. '$mod.c="\uDFFF";',
  8300. '$mod.c="\uFFFF";',
  8301. '$mod.c="\uFFFF";',
  8302. '$mod.c = "č";',
  8303. '']));
  8304. end;
  8305. procedure TTestModule.TestChar_Compare;
  8306. begin
  8307. StartProgram(false);
  8308. Add('var');
  8309. Add(' c: AnsiChar;');
  8310. Add(' b: boolean;');
  8311. Add('begin');
  8312. Add(' b:=c=''1'';');
  8313. Add(' b:=''2''=c;');
  8314. Add(' b:=''3''=''4'';');
  8315. Add(' b:=c<>''5'';');
  8316. Add(' b:=''6''<>c;');
  8317. Add(' b:=c>''7'';');
  8318. Add(' b:=''8''>c;');
  8319. Add(' b:=c>=''9'';');
  8320. Add(' b:=''A''>=c;');
  8321. Add(' b:=c<''B'';');
  8322. Add(' b:=''C''<c;');
  8323. Add(' b:=c<=''D'';');
  8324. Add(' b:=''E''<=c;');
  8325. ConvertProgram;
  8326. CheckSource('TestChar_Compare',
  8327. LinesToStr([
  8328. 'this.c="";',
  8329. 'this.b = false;'
  8330. ]),
  8331. LinesToStr([
  8332. '$mod.b = $mod.c === "1";',
  8333. '$mod.b = "2" === $mod.c;',
  8334. '$mod.b = "3" === "4";',
  8335. '$mod.b = $mod.c !== "5";',
  8336. '$mod.b = "6" !== $mod.c;',
  8337. '$mod.b = $mod.c > "7";',
  8338. '$mod.b = "8" > $mod.c;',
  8339. '$mod.b = $mod.c >= "9";',
  8340. '$mod.b = "A" >= $mod.c;',
  8341. '$mod.b = $mod.c < "B";',
  8342. '$mod.b = "C" < $mod.c;',
  8343. '$mod.b = $mod.c <= "D";',
  8344. '$mod.b = "E" <= $mod.c;',
  8345. '']));
  8346. end;
  8347. procedure TTestModule.TestChar_BuiltInProcs;
  8348. begin
  8349. StartProgram(false);
  8350. Add([
  8351. 'var',
  8352. ' c: AnsiChar;',
  8353. ' i: longint;',
  8354. ' s: string;',
  8355. 'begin',
  8356. ' i:=ord(c);',
  8357. ' i:=ord(s[i]);',
  8358. ' c:=chr(i);',
  8359. ' c:=pred(c);',
  8360. ' c:=succ(c);',
  8361. ' c:=low(c);',
  8362. ' c:=high(c);',
  8363. ' i:=byte(c);',
  8364. ' i:=word(c);',
  8365. ' i:=longint(c);',
  8366. '']);
  8367. ConvertProgram;
  8368. CheckSource('TestChar_BuiltInProcs',
  8369. LinesToStr([
  8370. 'this.c = "";',
  8371. 'this.i = 0;',
  8372. 'this.s = "";'
  8373. ]),
  8374. LinesToStr([
  8375. '$mod.i = $mod.c.charCodeAt();',
  8376. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8377. '$mod.c = String.fromCharCode($mod.i);',
  8378. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8379. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8380. '$mod.c = "\x00";',
  8381. '$mod.c = "\uFFFF";',
  8382. '$mod.i = $mod.c.charCodeAt() & 255;',
  8383. '$mod.i = $mod.c.charCodeAt();',
  8384. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8385. '']));
  8386. end;
  8387. procedure TTestModule.TestStringConst;
  8388. begin
  8389. StartProgram(false);
  8390. Add([
  8391. '{$H+}',
  8392. 'const',
  8393. ' a = #$00F3#$017C;', // first <256, then >=256
  8394. ' b = string(''a'');',
  8395. ' c = string(''ä'');',
  8396. ' d = UnicodeString(''b'');',
  8397. ' e = UnicodeString(''ö'');',
  8398. ' f = low(a)+high(b);',
  8399. ' g: word = low(a);',
  8400. 'var',
  8401. ' s: string = ''abc'';',
  8402. ' i: longint;',
  8403. 'begin',
  8404. ' s:='''';',
  8405. ' s:=#13#10;',
  8406. ' s:=#9''foo'';',
  8407. ' s:=#$A9;',
  8408. ' s:=''foo''#13''bar'';',
  8409. ' s:=''"'';',
  8410. ' s:=''"''''"'';',
  8411. ' s:=#$20AC;', // euro
  8412. ' s:=#$10437;', // outside BMP
  8413. ' s:=''abc''#$20AC;', // ascii,#
  8414. ' s:=''ä''#$20AC;', // non ascii,#
  8415. ' s:=#$20AC''abc'';', // #, ascii
  8416. ' s:=#$20AC''ä'';', // #, non ascii
  8417. ' s:=default(string);',
  8418. ' s:=concat(s);',
  8419. ' s:=concat(s,''a'',s);',
  8420. ' s:=#250#269;',
  8421. ' i:=low(s)+high(a);',
  8422. ' s:=''a/b'';',
  8423. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8424. '']);
  8425. ConvertProgram;
  8426. CheckSource('TestStringConst',
  8427. LinesToStr([
  8428. 'this.a = "óż";',
  8429. 'this.b = "a";',
  8430. 'this.c = "ä";',
  8431. 'this.d = "b";',
  8432. 'this.e = "ö";',
  8433. 'this.f = 1 + this.b.length;',
  8434. 'this.g = 1;',
  8435. 'this.s="abc";',
  8436. 'this.i = 0;',
  8437. '']),
  8438. LinesToStr([
  8439. '$mod.s="";',
  8440. '$mod.s="\r\n";',
  8441. '$mod.s="\tfoo";',
  8442. '$mod.s="©";',
  8443. '$mod.s="foo\rbar";',
  8444. '$mod.s=''"'';',
  8445. '$mod.s=''"\''"'';',
  8446. '$mod.s="€";',
  8447. '$mod.s="'#$F0#$90#$90#$B7'";',
  8448. '$mod.s = "abc€";',
  8449. '$mod.s = "ä€";',
  8450. '$mod.s = "€abc";',
  8451. '$mod.s = "ۊ";',
  8452. '$mod.s="";',
  8453. '$mod.s = $mod.s;',
  8454. '$mod.s = $mod.s.concat("a", $mod.s);',
  8455. '$mod.s = "úč";',
  8456. '$mod.i = 1 + $mod.a.length;',
  8457. '$mod.s = "a/b";',
  8458. '']));
  8459. end;
  8460. procedure TTestModule.TestStringConst_InvalidUTF16;
  8461. begin
  8462. StartProgram(false);
  8463. Add([
  8464. 'const',
  8465. ' a: AnsiChar = #$D87E;',
  8466. ' b: string = #$D87E;',
  8467. ' c: string = #$D87E#43;',
  8468. 'begin',
  8469. ' c:=''abc''#$D87E;',
  8470. ' c:=#0#1#2;',
  8471. ' c:=#127;',
  8472. ' c:=#128;',
  8473. ' c:=#255;',
  8474. ' c:=#256;',
  8475. '']);
  8476. ConvertProgram;
  8477. CheckSource('TestStringConst',
  8478. LinesToStr([
  8479. 'this.a = "\uD87E";',
  8480. 'this.b = "\uD87E";',
  8481. 'this.c = "\uD87E+";',
  8482. '']),
  8483. LinesToStr([
  8484. '$mod.c = "abc\uD87E";',
  8485. '$mod.c = "\x00\x01\x02";',
  8486. '$mod.c = "'#127'";',
  8487. '$mod.c = "'#$c2#$80'";',
  8488. '$mod.c = "'#$c3#$BF'";',
  8489. '$mod.c = "'#$c4#$80'";',
  8490. '']));
  8491. end;
  8492. procedure TTestModule.TestStringConstSurrogate;
  8493. begin
  8494. StartProgram(false);
  8495. Add([
  8496. 'var',
  8497. ' s: string;',
  8498. 'begin',
  8499. ' s:=''😊'';', // 1F60A
  8500. '']);
  8501. ConvertProgram;
  8502. CheckSource('TestStringConstSurrogate',
  8503. LinesToStr([
  8504. 'this.s="";'
  8505. ]),
  8506. LinesToStr([
  8507. '$mod.s="😊";'
  8508. ]));
  8509. end;
  8510. procedure TTestModule.TestStringConst_Multiline;
  8511. begin
  8512. StartProgram(false);
  8513. Add([
  8514. '{$modeswitch multilinestrings}',
  8515. 'const',
  8516. ' a = ``;',
  8517. ' b = `',
  8518. 'line`;',
  8519. ' c = `Single`;',
  8520. ' d = ````;',
  8521. ' e = `abc``xyz`;',
  8522. ' f = `first''line',
  8523. ' second''line`#10;',
  8524. 'begin',
  8525. '']);
  8526. ConvertProgram;
  8527. CheckSource('TestStringConst_Multiline',
  8528. LinesToStr([
  8529. 'this.a = "";',
  8530. 'this.b = "\nline";',
  8531. 'this.c = "Single";',
  8532. 'this.d = "`";',
  8533. 'this.e = "abc`xyz";',
  8534. 'this.f = "first''line\n second''line\n";',
  8535. '']),
  8536. LinesToStr([
  8537. ]));
  8538. end;
  8539. procedure TTestModule.TestString_Length;
  8540. begin
  8541. StartProgram(false);
  8542. Add('const c = ''foo'';');
  8543. Add('var');
  8544. Add(' s: string;');
  8545. Add(' i: longint;');
  8546. Add('begin');
  8547. Add(' i:=length(s);');
  8548. Add(' i:=length(s+s);');
  8549. Add(' i:=length(''abc'');');
  8550. Add(' i:=length(c);');
  8551. ConvertProgram;
  8552. CheckSource('TestString_Length',
  8553. LinesToStr([
  8554. 'this.c = "foo";',
  8555. 'this.s = "";',
  8556. 'this.i = 0;',
  8557. '']),
  8558. LinesToStr([
  8559. '$mod.i = $mod.s.length;',
  8560. '$mod.i = ($mod.s+$mod.s).length;',
  8561. '$mod.i = "abc".length;',
  8562. '$mod.i = $mod.c.length;',
  8563. '']));
  8564. end;
  8565. procedure TTestModule.TestString_Compare;
  8566. begin
  8567. StartProgram(false);
  8568. Add('var');
  8569. Add(' s, t: string;');
  8570. Add(' b: boolean;');
  8571. Add('begin');
  8572. Add(' b:=s=t;');
  8573. Add(' b:=s<>t;');
  8574. Add(' b:=s>t;');
  8575. Add(' b:=s>=t;');
  8576. Add(' b:=s<t;');
  8577. Add(' b:=s<=t;');
  8578. ConvertProgram;
  8579. CheckSource('TestString_Compare',
  8580. LinesToStr([ // statements
  8581. 'this.s = "";',
  8582. 'this.t = "";',
  8583. 'this.b =false;'
  8584. ]),
  8585. LinesToStr([ // this.$main
  8586. '$mod.b = $mod.s === $mod.t;',
  8587. '$mod.b = $mod.s !== $mod.t;',
  8588. '$mod.b = $mod.s > $mod.t;',
  8589. '$mod.b = $mod.s >= $mod.t;',
  8590. '$mod.b = $mod.s < $mod.t;',
  8591. '$mod.b = $mod.s <= $mod.t;',
  8592. '']));
  8593. end;
  8594. procedure TTestModule.TestString_SetLength;
  8595. begin
  8596. StartProgram(false);
  8597. Add([
  8598. 'procedure Fly(var s: string);',
  8599. 'begin',
  8600. ' SetLength(s,1);',
  8601. 'end;',
  8602. 'procedure Run(var s: unicodestring);',
  8603. 'begin',
  8604. ' SetLength(s,2);',
  8605. 'end;',
  8606. 'var s: string;',
  8607. ' u: unicodestring;',
  8608. 'begin',
  8609. ' SetLength(s,3);',
  8610. ' SetLength(u,4);',
  8611. '']);
  8612. ConvertProgram;
  8613. CheckSource('TestString_SetLength',
  8614. LinesToStr([ // statements
  8615. 'this.Fly = function (s) {',
  8616. ' s.set(rtl.strSetLength(s.get(), 1));',
  8617. '};',
  8618. 'this.Run = function (s) {',
  8619. ' s.set(rtl.strSetLength(s.get(), 2));',
  8620. '};',
  8621. 'this.s = "";',
  8622. 'this.u = "";',
  8623. '']),
  8624. LinesToStr([ // this.$main
  8625. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8626. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8627. ]));
  8628. end;
  8629. procedure TTestModule.TestString_CharAt;
  8630. begin
  8631. StartProgram(false);
  8632. Add([
  8633. 'var',
  8634. ' s: string;',
  8635. ' c: AnsiChar;',
  8636. ' b: boolean;',
  8637. 'begin',
  8638. ' b:= s[1] = c;',
  8639. ' b:= c = s[1];',
  8640. ' b:= c <> s[1];',
  8641. ' b:= c > s[1];',
  8642. ' b:= c >= s[1];',
  8643. ' b:= c < s[2];',
  8644. ' b:= c <= s[1];',
  8645. ' s[1] := c;',
  8646. ' s[2+3] := c;']);
  8647. ConvertProgram;
  8648. CheckSource('TestString_CharAt',
  8649. LinesToStr([ // statements
  8650. 'this.s = "";',
  8651. 'this.c = "";',
  8652. 'this.b = false;'
  8653. ]),
  8654. LinesToStr([ // this.$main
  8655. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8656. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8657. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8658. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8659. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8660. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8661. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8662. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8663. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8664. '']));
  8665. end;
  8666. procedure TTestModule.TestStringHMinusFail;
  8667. begin
  8668. StartProgram(false);
  8669. Add([
  8670. '{$H-}',
  8671. 'var s: string;',
  8672. 'begin']);
  8673. ConvertProgram;
  8674. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8675. end;
  8676. procedure TTestModule.TestStr;
  8677. begin
  8678. StartProgram(false);
  8679. Add('var');
  8680. Add(' b: boolean;');
  8681. Add(' i: longint;');
  8682. Add(' d: double;');
  8683. Add(' s: string;');
  8684. Add('begin');
  8685. Add(' str(b,s);');
  8686. Add(' str(i,s);');
  8687. Add(' str(d,s);');
  8688. Add(' str(i:3,s);');
  8689. Add(' str(d:3:2,s);');
  8690. Add(' Str(12.456:12:1,s);');
  8691. Add(' Str(12.456:12,s);');
  8692. Add(' s:=str(b);');
  8693. Add(' s:=str(i);');
  8694. Add(' s:=str(d);');
  8695. Add(' s:=str(i,i);');
  8696. Add(' s:=str(i:3);');
  8697. Add(' s:=str(d:3:2);');
  8698. Add(' s:=str(i:4,i);');
  8699. Add(' s:=str(i,i:5);');
  8700. Add(' s:=str(i:4,i:5);');
  8701. Add(' s:=str(s,s);');
  8702. Add(' s:=str(s,''foo'');');
  8703. ConvertProgram;
  8704. CheckSource('TestStr',
  8705. LinesToStr([ // statements
  8706. 'this.b = false;',
  8707. 'this.i = 0;',
  8708. 'this.d = 0.0;',
  8709. 'this.s = "";',
  8710. '']),
  8711. LinesToStr([ // this.$main
  8712. '$mod.s = ""+$mod.b;',
  8713. '$mod.s = ""+$mod.i;',
  8714. '$mod.s = rtl.floatToStr($mod.d);',
  8715. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8716. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8717. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8718. '$mod.s = rtl.floatToStr(12.456,12);',
  8719. '$mod.s = ""+$mod.b;',
  8720. '$mod.s = ""+$mod.i;',
  8721. '$mod.s = rtl.floatToStr($mod.d);',
  8722. '$mod.s = ""+$mod.i+$mod.i;',
  8723. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8724. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8725. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8726. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8727. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8728. '$mod.s = $mod.s + $mod.s;',
  8729. '$mod.s = $mod.s + "foo";',
  8730. '']));
  8731. end;
  8732. procedure TTestModule.TestBaseType_AnsiStringFail;
  8733. begin
  8734. StartProgram(false);
  8735. Add('var s: AnsiString');
  8736. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8737. ConvertProgram;
  8738. end;
  8739. procedure TTestModule.TestBaseType_WideStringFail;
  8740. begin
  8741. StartProgram(false);
  8742. Add('var s: WideString');
  8743. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8744. ConvertProgram;
  8745. end;
  8746. procedure TTestModule.TestBaseType_ShortStringFail;
  8747. begin
  8748. StartProgram(false);
  8749. Add('var s: ShortString');
  8750. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8751. ConvertProgram;
  8752. end;
  8753. procedure TTestModule.TestBaseType_RawByteStringFail;
  8754. begin
  8755. StartProgram(false);
  8756. Add('var s: RawByteString');
  8757. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8758. ConvertProgram;
  8759. end;
  8760. procedure TTestModule.TestTypeShortstring_Fail;
  8761. begin
  8762. StartProgram(false);
  8763. Add('type t = string[12];');
  8764. Add('var s: t;');
  8765. Add('begin');
  8766. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8767. ConvertProgram;
  8768. end;
  8769. procedure TTestModule.TestCharSet_Custom;
  8770. begin
  8771. StartProgram(false);
  8772. Add([
  8773. 'type',
  8774. ' TCharRg = ''a''..''z'';',
  8775. ' TSetOfCharRg = set of TCharRg;',
  8776. ' TCharRg2 = ''m''..''p'';',
  8777. 'const',
  8778. ' crg: TCharRg = ''b'';',
  8779. 'var',
  8780. ' c: AnsiChar;',
  8781. ' crg2: TCharRg2;',
  8782. ' s: TSetOfCharRg;',
  8783. 'begin',
  8784. ' c:=crg;',
  8785. ' crg:=c;',
  8786. ' crg2:=crg;',
  8787. ' if c=crg then ;',
  8788. ' if crg=c then ;',
  8789. ' if crg=crg2 then ;',
  8790. ' if c in s then ;',
  8791. ' if crg2 in s then ;',
  8792. ' c:=default(TCharRg);',
  8793. '']);
  8794. ConvertProgram;
  8795. CheckSource('TestCharSet_Custom',
  8796. LinesToStr([ // statements
  8797. 'this.crg = "b";',
  8798. 'this.c = "";',
  8799. 'this.crg2 = "m";',
  8800. 'this.s = {};',
  8801. '']),
  8802. LinesToStr([ // this.$main
  8803. '$mod.c = $mod.crg;',
  8804. '$mod.crg = $mod.c;',
  8805. '$mod.crg2 = $mod.crg;',
  8806. 'if ($mod.c === $mod.crg) ;',
  8807. 'if ($mod.crg === $mod.c) ;',
  8808. 'if ($mod.crg === $mod.crg2) ;',
  8809. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8810. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8811. '$mod.c = "a";',
  8812. '']));
  8813. end;
  8814. procedure TTestModule.TestWideChar;
  8815. begin
  8816. StartProgram(false);
  8817. Add([
  8818. 'procedure Fly(var c: AnsiChar);',
  8819. 'begin',
  8820. 'end;',
  8821. 'procedure Run(var c: widechar);',
  8822. 'begin',
  8823. 'end;',
  8824. 'var',
  8825. ' c: AnsiChar;',
  8826. ' wc: widechar;',
  8827. ' w: word;',
  8828. 'begin',
  8829. ' Fly(wc);',
  8830. ' Run(c);',
  8831. ' wc:=WideChar(w);',
  8832. ' w:=ord(wc);',
  8833. '']);
  8834. ConvertProgram;
  8835. CheckSource('TestWideChar_VarArg',
  8836. LinesToStr([ // statements
  8837. 'this.Fly = function (c) {',
  8838. '};',
  8839. 'this.Run = function (c) {',
  8840. '};',
  8841. 'this.c = "";',
  8842. 'this.wc = "";',
  8843. 'this.w = 0;',
  8844. '']),
  8845. LinesToStr([ // this.$main
  8846. '$mod.Fly({',
  8847. ' p: $mod,',
  8848. ' get: function () {',
  8849. ' return this.p.wc;',
  8850. ' },',
  8851. ' set: function (v) {',
  8852. ' this.p.wc = v;',
  8853. ' }',
  8854. '});',
  8855. '$mod.Run({',
  8856. ' p: $mod,',
  8857. ' get: function () {',
  8858. ' return this.p.c;',
  8859. ' },',
  8860. ' set: function (v) {',
  8861. ' this.p.c = v;',
  8862. ' }',
  8863. '});',
  8864. '$mod.wc = String.fromCharCode($mod.w);',
  8865. '$mod.w = $mod.wc.charCodeAt();',
  8866. '',
  8867. '']));
  8868. end;
  8869. procedure TTestModule.TestForCharDo;
  8870. begin
  8871. StartProgram(false);
  8872. Add([
  8873. 'var c: AnsiChar;',
  8874. 'begin',
  8875. ' for c:=''a'' to ''c'' do ;',
  8876. ' for c:=c downto ''a'' do ;',
  8877. ' for c:=''Б'' to ''Я'' do ;',
  8878. '']);
  8879. ConvertProgram;
  8880. CheckSource('TestForCharDo',
  8881. LinesToStr([ // statements
  8882. 'this.c = "";']),
  8883. LinesToStr([ // this.$main
  8884. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8885. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8886. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8887. '']));
  8888. end;
  8889. procedure TTestModule.TestForCharInDo;
  8890. begin
  8891. StartProgram(false);
  8892. Add([
  8893. 'type',
  8894. ' TSetOfChar = set of AnsiChar;',
  8895. ' TCharRg = ''a''..''z'';',
  8896. ' TSetOfCharRg = set of TCharRg;',
  8897. 'const Foo = ''foo'';',
  8898. 'var',
  8899. ' c,c2: AnsiChar;',
  8900. ' s: string;',
  8901. ' a1: array of AnsiChar;',
  8902. ' a2: array[1..3] of AnsiChar;',
  8903. ' soc: TSetOfChar;',
  8904. ' socr: TSetOfCharRg;',
  8905. ' cr: TCharRg;',
  8906. 'begin',
  8907. ' for c in foo do ;',
  8908. ' for c in s do ;',
  8909. ' for c in AnsiChar do ;',
  8910. ' for c in a1 do ;',
  8911. ' for c in a2 do ;',
  8912. ' for c in [''1''..''3''] do ;',
  8913. ' for c in TSetOfChar do ;',
  8914. ' for c in TCharRg do ;',
  8915. ' for c in soc do c2:=c;',
  8916. ' for c in TSetOfCharRg do ;',
  8917. ' for c in socr do ;',
  8918. ' for cr in TCharRg do ;',
  8919. ' for cr in TSetOfCharRg do ;',
  8920. ' for cr in socr do ;',
  8921. '']);
  8922. ConvertProgram;
  8923. CheckSource('TestForCharInDo',
  8924. LinesToStr([ // statements
  8925. 'this.Foo = "foo";',
  8926. 'this.c = "";',
  8927. 'this.c2 = "";',
  8928. 'this.s = "";',
  8929. 'this.a1 = [];',
  8930. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8931. 'this.soc = {};',
  8932. 'this.socr = {};',
  8933. 'this.cr = "a";',
  8934. '']),
  8935. LinesToStr([ // this.$main
  8936. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8937. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8938. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8939. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8940. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8941. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8942. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8943. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8944. 'for (var $l8 in $mod.soc) {',
  8945. ' $mod.c = String.fromCharCode($l8);',
  8946. ' $mod.c2 = $mod.c;',
  8947. '};',
  8948. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8949. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8950. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8951. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8952. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8953. '']));
  8954. end;
  8955. procedure TTestModule.TestProcTwoArgs;
  8956. begin
  8957. StartProgram(false);
  8958. Add('procedure Test(a,b: longint);');
  8959. Add('begin');
  8960. Add('end;');
  8961. Add('begin');
  8962. ConvertProgram;
  8963. CheckSource('TestProcTwoArgs',
  8964. LinesToStr([ // statements
  8965. 'this.Test = function (a,b) {',
  8966. '};'
  8967. ]),
  8968. LinesToStr([ // this.$main
  8969. ''
  8970. ]));
  8971. end;
  8972. procedure TTestModule.TestProc_DefaultValue;
  8973. begin
  8974. StartProgram(false);
  8975. Add('procedure p1(i: longint = 1);');
  8976. Add('begin');
  8977. Add('end;');
  8978. Add('procedure p2(i: longint = 1; c: AnsiChar = ''a'');');
  8979. Add('begin');
  8980. Add('end;');
  8981. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8982. Add('begin');
  8983. Add('end;');
  8984. Add('begin');
  8985. Add(' p1;');
  8986. Add(' p1();');
  8987. Add(' p1(11);');
  8988. Add(' p2;');
  8989. Add(' p2();');
  8990. Add(' p2(12);');
  8991. Add(' p2(13,''b'');');
  8992. Add(' p3();');
  8993. ConvertProgram;
  8994. CheckSource('TestProc_DefaultValue',
  8995. LinesToStr([ // statements
  8996. 'this.p1 = function (i) {',
  8997. '};',
  8998. 'this.p2 = function (i,c) {',
  8999. '};',
  9000. 'this.p3 = function (d,b,s) {',
  9001. '};'
  9002. ]),
  9003. LinesToStr([ // this.$main
  9004. ' $mod.p1(1);',
  9005. ' $mod.p1(1);',
  9006. ' $mod.p1(11);',
  9007. ' $mod.p2(1,"a");',
  9008. ' $mod.p2(1,"a");',
  9009. ' $mod.p2(12,"a");',
  9010. ' $mod.p2(13,"b");',
  9011. ' $mod.p3(1.0,false,"abc");'
  9012. ]));
  9013. end;
  9014. procedure TTestModule.TestFunctionInt;
  9015. begin
  9016. StartProgram(false);
  9017. Add('function MyTest(Bar: longint): longint;');
  9018. Add('begin');
  9019. Add(' Result:=2*bar');
  9020. Add('end;');
  9021. Add('begin');
  9022. ConvertProgram;
  9023. CheckSource('TestFunctionInt',
  9024. LinesToStr([ // statements
  9025. 'this.MyTest = function (Bar) {',
  9026. ' var Result = 0;',
  9027. ' Result = 2*Bar;',
  9028. ' return Result;',
  9029. '};'
  9030. ]),
  9031. LinesToStr([ // this.$main
  9032. ''
  9033. ]));
  9034. end;
  9035. procedure TTestModule.TestFunctionString;
  9036. begin
  9037. StartProgram(false);
  9038. Add('function Test(Bar: string): string;');
  9039. Add('begin');
  9040. Add(' Result:=bar+BAR');
  9041. Add('end;');
  9042. Add('begin');
  9043. ConvertProgram;
  9044. CheckSource('TestFunctionString',
  9045. LinesToStr([ // statements
  9046. 'this.Test = function (Bar) {',
  9047. ' var Result = "";',
  9048. ' Result = Bar+Bar;',
  9049. ' return Result;',
  9050. '};'
  9051. ]),
  9052. LinesToStr([ // this.$main
  9053. ''
  9054. ]));
  9055. end;
  9056. procedure TTestModule.TestIfThen;
  9057. begin
  9058. StartProgram(false);
  9059. Add([
  9060. 'var b: boolean;',
  9061. 'begin',
  9062. ' if b then ;',
  9063. ' if b then else ;']);
  9064. ConvertProgram;
  9065. CheckSource('TestIfThen',
  9066. LinesToStr([ // statements
  9067. 'this.b = false;',
  9068. '']),
  9069. LinesToStr([ // this.$main
  9070. 'if ($mod.b) ;',
  9071. 'if ($mod.b) ;',
  9072. '']));
  9073. end;
  9074. procedure TTestModule.TestForLoop;
  9075. begin
  9076. StartProgram(false);
  9077. Add('var');
  9078. Add(' vI, vJ, vN: longint;');
  9079. Add('begin');
  9080. Add(' VJ:=0;');
  9081. Add(' VN:=3;');
  9082. Add(' for VI:=1 to VN do');
  9083. Add(' begin');
  9084. Add(' VJ:=VJ+VI;');
  9085. Add(' end;');
  9086. ConvertProgram;
  9087. CheckSource('TestForLoop',
  9088. LinesToStr([ // statements
  9089. 'this.vI = 0;',
  9090. 'this.vJ = 0;',
  9091. 'this.vN = 0;'
  9092. ]),
  9093. LinesToStr([ // this.$main
  9094. ' $mod.vJ = 0;',
  9095. ' $mod.vN = 3;',
  9096. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9097. ' $mod.vI = $l;',
  9098. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9099. ' };',
  9100. '']));
  9101. end;
  9102. procedure TTestModule.TestForLoopInsideFunction;
  9103. begin
  9104. StartProgram(false);
  9105. Add('function SumNumbers(Count: longint): longint;');
  9106. Add('var');
  9107. Add(' vI, vJ: longint;');
  9108. Add('begin');
  9109. Add(' vj:=0;');
  9110. Add(' for vi:=1 to count do');
  9111. Add(' begin');
  9112. Add(' vj:=vj+vi;');
  9113. Add(' end;');
  9114. Add('end;');
  9115. Add('begin');
  9116. Add(' sumnumbers(3);');
  9117. ConvertProgram;
  9118. CheckSource('TestForLoopInsideFunction',
  9119. LinesToStr([ // statements
  9120. 'this.SumNumbers = function (Count) {',
  9121. ' var Result = 0;',
  9122. ' var vI = 0;',
  9123. ' var vJ = 0;',
  9124. ' vJ = 0;',
  9125. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9126. ' vI = $l;',
  9127. ' vJ = vJ + vI;',
  9128. ' };',
  9129. ' return Result;',
  9130. '};'
  9131. ]),
  9132. LinesToStr([ // $mod.$main
  9133. ' $mod.SumNumbers(3);'
  9134. ]));
  9135. end;
  9136. procedure TTestModule.TestForLoop_ReadVarAfter;
  9137. begin
  9138. StartProgram(false);
  9139. Add('var');
  9140. Add(' vI: longint;');
  9141. Add('begin');
  9142. Add(' for vi:=1 to 2 do ;');
  9143. Add(' if vi=3 then ;');
  9144. ConvertProgram;
  9145. CheckSource('TestForLoop',
  9146. LinesToStr([ // statements
  9147. 'this.vI = 0;'
  9148. ]),
  9149. LinesToStr([ // this.$main
  9150. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9151. ' if ($mod.vI===3) ;'
  9152. ]));
  9153. end;
  9154. procedure TTestModule.TestForLoop_Nested;
  9155. begin
  9156. StartProgram(false);
  9157. Add('function SumNumbers(Count: longint): longint;');
  9158. Add('var');
  9159. Add(' vI, vJ, vK: longint;');
  9160. Add('begin');
  9161. Add(' VK:=0;');
  9162. Add(' for VI:=1 to count do');
  9163. Add(' begin');
  9164. Add(' for vj:=1 to vi do');
  9165. Add(' begin');
  9166. Add(' vk:=VK+VI;');
  9167. Add(' end;');
  9168. Add(' end;');
  9169. Add('end;');
  9170. Add('begin');
  9171. Add(' sumnumbers(3);');
  9172. ConvertProgram;
  9173. CheckSource('TestForLoopInFunction',
  9174. LinesToStr([ // statements
  9175. 'this.SumNumbers = function (Count) {',
  9176. ' var Result = 0;',
  9177. ' var vI = 0;',
  9178. ' var vJ = 0;',
  9179. ' var vK = 0;',
  9180. ' vK = 0;',
  9181. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9182. ' vI = $l;',
  9183. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9184. ' vJ = $l1;',
  9185. ' vK = vK + vI;',
  9186. ' };',
  9187. ' };',
  9188. ' return Result;',
  9189. '};'
  9190. ]),
  9191. LinesToStr([ // $mod.$main
  9192. ' $mod.SumNumbers(3);'
  9193. ]));
  9194. end;
  9195. procedure TTestModule.TestRepeatUntil;
  9196. begin
  9197. StartProgram(false);
  9198. Add('var');
  9199. Add(' vI, vJ, vN: longint;');
  9200. Add('begin');
  9201. Add(' vn:=3;');
  9202. Add(' vj:=0;');
  9203. Add(' VI:=0;');
  9204. Add(' repeat');
  9205. Add(' VI:=vi+1;');
  9206. Add(' vj:=VJ+vI;');
  9207. Add(' until vi>=vn');
  9208. ConvertProgram;
  9209. CheckSource('TestRepeatUntil',
  9210. LinesToStr([ // statements
  9211. 'this.vI = 0;',
  9212. 'this.vJ = 0;',
  9213. 'this.vN = 0;'
  9214. ]),
  9215. LinesToStr([ // $mod.$main
  9216. ' $mod.vN = 3;',
  9217. ' $mod.vJ = 0;',
  9218. ' $mod.vI = 0;',
  9219. ' do{',
  9220. ' $mod.vI = $mod.vI + 1;',
  9221. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9222. ' }while(!($mod.vI>=$mod.vN));'
  9223. ]));
  9224. end;
  9225. procedure TTestModule.TestAsmBlock;
  9226. begin
  9227. StartProgram(false);
  9228. Add([
  9229. 'var',
  9230. ' vI: longint;',
  9231. 'begin',
  9232. ' vi:=1;',
  9233. ' asm',
  9234. ' if (vI===1) {',
  9235. ' vI=2;',
  9236. //' console.log(''end;'');', ToDo
  9237. ' }',
  9238. ' if (vI===2){ vI=3; }',
  9239. ' end;',
  9240. ' VI:=4;']);
  9241. ConvertProgram;
  9242. CheckSource('TestAsmBlock',
  9243. LinesToStr([ // statements
  9244. 'this.vI = 0;'
  9245. ]),
  9246. LinesToStr([ // $mod.$main
  9247. '$mod.vI = 1;',
  9248. 'if (vI===1) {',
  9249. ' vI=2;',
  9250. '}',
  9251. 'if (vI===2){ vI=3; }',
  9252. ';',
  9253. '$mod.vI = 4;'
  9254. ]));
  9255. end;
  9256. procedure TTestModule.TestAsmPas_Impl;
  9257. begin
  9258. StartUnit(false);
  9259. Add('interface');
  9260. Add('const cIntf: longint = 1;');
  9261. Add('var vIntf: longint;');
  9262. Add('implementation');
  9263. Add('const cImpl: longint = 2;');
  9264. Add('var vImpl: longint;');
  9265. Add('procedure DoIt;');
  9266. Add('const cLoc: longint = 3;');
  9267. Add('var vLoc: longint;');
  9268. Add('begin;');
  9269. Add(' asm');
  9270. //Add(' pas(vIntf)=pas(cIntf);');
  9271. //Add(' pas(vImpl)=pas(cImpl);');
  9272. //Add(' pas(vLoc)=pas(cLoc);');
  9273. Add(' end;');
  9274. Add('end;');
  9275. ConvertUnit;
  9276. CheckSource('TestAsmPas_Impl',
  9277. LinesToStr([
  9278. 'var $impl = $mod.$impl;',
  9279. 'this.cIntf = 1;',
  9280. 'this.vIntf = 0;',
  9281. '']),
  9282. '', // this.$init
  9283. LinesToStr([ // implementation
  9284. '$impl.cImpl = 2;',
  9285. '$impl.vImpl = 0;',
  9286. 'var cLoc = 3;',
  9287. '$impl.DoIt = function () {',
  9288. ' var vLoc = 0;',
  9289. '};',
  9290. '']) );
  9291. end;
  9292. procedure TTestModule.TestTryFinally;
  9293. begin
  9294. StartProgram(false);
  9295. Add('var i: longint;');
  9296. Add('begin');
  9297. Add(' try');
  9298. Add(' i:=0; i:=2 div i;');
  9299. Add(' finally');
  9300. Add(' i:=3');
  9301. Add(' end;');
  9302. ConvertProgram;
  9303. CheckSource('TestTryFinally',
  9304. LinesToStr([ // statements
  9305. 'this.i = 0;'
  9306. ]),
  9307. LinesToStr([ // $mod.$main
  9308. 'try {',
  9309. ' $mod.i = 0;',
  9310. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9311. '} finally {',
  9312. ' $mod.i = 3;',
  9313. '};'
  9314. ]));
  9315. end;
  9316. procedure TTestModule.TestTryExcept;
  9317. begin
  9318. StartProgram(false);
  9319. Add([
  9320. 'type',
  9321. ' TObject = class end;',
  9322. ' Exception = class Msg: string; end;',
  9323. ' EInvalidCast = class(Exception) end;',
  9324. 'var vI: longint;',
  9325. 'begin',
  9326. ' try',
  9327. ' vi:=1;',
  9328. ' except',
  9329. ' vi:=2',
  9330. ' end;',
  9331. ' try',
  9332. ' vi:=3;',
  9333. ' except',
  9334. ' raise;',
  9335. ' end;',
  9336. ' try',
  9337. ' VI:=4;',
  9338. ' except',
  9339. ' on einvalidcast do',
  9340. ' raise;',
  9341. ' on E: exception do',
  9342. ' if e.msg='''' then',
  9343. ' raise e;',
  9344. ' else',
  9345. ' vi:=5',
  9346. ' end;',
  9347. ' try',
  9348. ' VI:=6;',
  9349. ' except',
  9350. ' on einvalidcast do ;',
  9351. ' end;',
  9352. '']);
  9353. ConvertProgram;
  9354. CheckSource('TestTryExcept',
  9355. LinesToStr([ // statements
  9356. 'rtl.createClass(this, "TObject", null, function () {',
  9357. ' this.$init = function () {',
  9358. ' };',
  9359. ' this.$final = function () {',
  9360. ' };',
  9361. '});',
  9362. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9363. ' this.$init = function () {',
  9364. ' $mod.TObject.$init.call(this);',
  9365. ' this.Msg = "";',
  9366. ' };',
  9367. '});',
  9368. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9369. '});',
  9370. 'this.vI = 0;'
  9371. ]),
  9372. LinesToStr([ // $mod.$main
  9373. 'try {',
  9374. ' $mod.vI = 1;',
  9375. '} catch ($e) {',
  9376. ' $mod.vI = 2;',
  9377. '};',
  9378. 'try {',
  9379. ' $mod.vI = 3;',
  9380. '} catch ($e) {',
  9381. ' throw $e;',
  9382. '};',
  9383. 'try {',
  9384. ' $mod.vI = 4;',
  9385. '} catch ($e) {',
  9386. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9387. ' throw $e',
  9388. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9389. ' var E = $e;',
  9390. ' if (E.Msg === "") throw E;',
  9391. ' } else {',
  9392. ' $mod.vI = 5;',
  9393. ' }',
  9394. '};',
  9395. 'try {',
  9396. ' $mod.vI = 6;',
  9397. '} catch ($e) {',
  9398. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9399. ' } else throw $e',
  9400. '};',
  9401. '']));
  9402. end;
  9403. procedure TTestModule.TestTryExcept_ReservedWords;
  9404. begin
  9405. StartProgram(false);
  9406. Add([
  9407. 'type',
  9408. ' TObject = class end;',
  9409. ' Exception = class',
  9410. ' Symbol: string;',
  9411. ' end;',
  9412. 'var &try: longint;',
  9413. 'begin',
  9414. ' try',
  9415. ' &try:=4;',
  9416. ' except',
  9417. ' on Error: exception do',
  9418. ' if errOR.symBol='''' then',
  9419. ' raise ERRor;',
  9420. ' end;',
  9421. '']);
  9422. ConvertProgram;
  9423. CheckSource('TestTryExcept_ReservedWords',
  9424. LinesToStr([ // statements
  9425. 'rtl.createClass(this, "TObject", null, function () {',
  9426. ' this.$init = function () {',
  9427. ' };',
  9428. ' this.$final = function () {',
  9429. ' };',
  9430. '});',
  9431. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9432. ' this.$init = function () {',
  9433. ' $mod.TObject.$init.call(this);',
  9434. ' this.Symbol = "";',
  9435. ' };',
  9436. '});',
  9437. 'this.Try = 0;',
  9438. '']),
  9439. LinesToStr([ // $mod.$main
  9440. 'try {',
  9441. ' $mod.Try = 4;',
  9442. '} catch ($e) {',
  9443. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9444. ' var error = $e;',
  9445. ' if (error.Symbol === "") throw error;',
  9446. ' } else throw $e',
  9447. '};',
  9448. '']));
  9449. end;
  9450. procedure TTestModule.TestIfThenRaiseElse;
  9451. begin
  9452. StartProgram(false);
  9453. Add([
  9454. 'type',
  9455. ' TObject = class',
  9456. ' constructor Create;',
  9457. ' end;',
  9458. 'constructor TObject.Create;',
  9459. 'begin',
  9460. 'end;',
  9461. 'var b: boolean;',
  9462. 'begin',
  9463. ' if b then',
  9464. ' raise TObject.Create',
  9465. ' else',
  9466. ' b:=false;',
  9467. '']);
  9468. ConvertProgram;
  9469. CheckSource('TestIfThenRaiseElse',
  9470. LinesToStr([ // statements
  9471. 'rtl.createClass(this, "TObject", null, function () {',
  9472. ' this.$init = function () {',
  9473. ' };',
  9474. ' this.$final = function () {',
  9475. ' };',
  9476. ' this.Create = function () {',
  9477. ' return this;',
  9478. ' };',
  9479. '});',
  9480. 'this.b = false;',
  9481. '']),
  9482. LinesToStr([ // $mod.$main
  9483. 'if ($mod.b) {',
  9484. ' throw $mod.TObject.$create("Create")}',
  9485. ' else $mod.b = false;',
  9486. '']));
  9487. end;
  9488. procedure TTestModule.TestCaseOf;
  9489. begin
  9490. StartProgram(false);
  9491. Add([
  9492. 'const e: longint; external name ''$e'';',
  9493. 'var vI: longint;',
  9494. 'begin',
  9495. ' case vi of',
  9496. ' 1: ;',
  9497. ' 2: vi:=3;',
  9498. ' e: ;',
  9499. ' else',
  9500. ' VI:=4',
  9501. ' end;']);
  9502. ConvertProgram;
  9503. CheckSource('TestCaseOf',
  9504. LinesToStr([ // statements
  9505. 'this.vI = 0;'
  9506. ]),
  9507. LinesToStr([ // $mod.$main
  9508. 'var $tmp = $mod.vI;',
  9509. 'if ($tmp === 1) {}',
  9510. 'else if ($tmp === 2) {',
  9511. ' $mod.vI = 3}',
  9512. ' else if ($tmp === $e) {}',
  9513. 'else {',
  9514. ' $mod.vI = 4;',
  9515. '};'
  9516. ]));
  9517. end;
  9518. procedure TTestModule.TestCaseOf_UseSwitch;
  9519. begin
  9520. StartProgram(false);
  9521. Converter.UseSwitchStatement:=true;
  9522. Add('var Vi: longint;');
  9523. Add('begin');
  9524. Add(' case vi of');
  9525. Add(' 1: ;');
  9526. Add(' 2: VI:=3;');
  9527. Add(' else');
  9528. Add(' vi:=4');
  9529. Add(' end;');
  9530. ConvertProgram;
  9531. CheckSource('TestCaseOf_UseSwitch',
  9532. LinesToStr([ // statements
  9533. 'this.Vi = 0;'
  9534. ]),
  9535. LinesToStr([ // $mod.$main
  9536. 'switch ($mod.Vi) {',
  9537. 'case 1:',
  9538. ' break;',
  9539. 'case 2:',
  9540. ' $mod.Vi = 3;',
  9541. ' break;',
  9542. 'default:',
  9543. ' $mod.Vi = 4;',
  9544. '};'
  9545. ]));
  9546. end;
  9547. procedure TTestModule.TestCaseOfNoElse;
  9548. begin
  9549. StartProgram(false);
  9550. Add('var Vi: longint;');
  9551. Add('begin');
  9552. Add(' case vi of');
  9553. Add(' 1: begin vi:=2; VI:=3; end;');
  9554. Add(' end;');
  9555. ConvertProgram;
  9556. CheckSource('TestCaseOfNoElse',
  9557. LinesToStr([ // statements
  9558. 'this.Vi = 0;'
  9559. ]),
  9560. LinesToStr([ // $mod.$main
  9561. 'var $tmp = $mod.Vi;',
  9562. 'if ($tmp === 1) {',
  9563. ' $mod.Vi = 2;',
  9564. ' $mod.Vi = 3;',
  9565. '};'
  9566. ]));
  9567. end;
  9568. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9569. begin
  9570. StartProgram(false);
  9571. Converter.UseSwitchStatement:=true;
  9572. Add('var vI: longint;');
  9573. Add('begin');
  9574. Add(' case vi of');
  9575. Add(' 1: begin VI:=2; vi:=3; end;');
  9576. Add(' end;');
  9577. ConvertProgram;
  9578. CheckSource('TestCaseOfNoElse_UseSwitch',
  9579. LinesToStr([ // statements
  9580. 'this.vI = 0;'
  9581. ]),
  9582. LinesToStr([ // $mod.$main
  9583. 'switch ($mod.vI) {',
  9584. 'case 1:',
  9585. ' $mod.vI = 2;',
  9586. ' $mod.vI = 3;',
  9587. ' break;',
  9588. '};'
  9589. ]));
  9590. end;
  9591. procedure TTestModule.TestCaseOfRange;
  9592. begin
  9593. StartProgram(false);
  9594. Add('var vI: longint;');
  9595. Add('begin');
  9596. Add(' case vi of');
  9597. Add(' 1..3: vi:=14;');
  9598. Add(' 4,5: vi:=16;');
  9599. Add(' 6..7,9..10: ;');
  9600. Add(' else ;');
  9601. Add(' end;');
  9602. ConvertProgram;
  9603. CheckSource('TestCaseOfRange',
  9604. LinesToStr([ // statements
  9605. 'this.vI = 0;'
  9606. ]),
  9607. LinesToStr([ // $mod.$main
  9608. 'var $tmp = $mod.vI;',
  9609. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9610. ' $mod.vI = 14',
  9611. '} else if (($tmp === 4) || ($tmp === 5)){',
  9612. ' $mod.vI = 16',
  9613. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9614. ]));
  9615. end;
  9616. procedure TTestModule.TestCaseOfString;
  9617. begin
  9618. StartProgram(false);
  9619. Add([
  9620. 'var s,h: string;',
  9621. 'begin',
  9622. ' case s of',
  9623. ' ''foo'': s:=h;',
  9624. ' ''a''..''z'': h:=s;',
  9625. ' ''ў'', ''ё'': ;',
  9626. ' ''Б''..''Я'': ;',
  9627. ' end;',
  9628. '']);
  9629. ConvertProgram;
  9630. CheckSource('TestCaseOfString',
  9631. LinesToStr([ // statements
  9632. 'this.s = "";',
  9633. 'this.h = "";',
  9634. '']),
  9635. LinesToStr([ // $mod.$main
  9636. 'var $tmp = $mod.s;',
  9637. 'if ($tmp === "foo") {',
  9638. ' $mod.s = $mod.h}',
  9639. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9640. ' $mod.h = $mod.s}',
  9641. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9642. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9643. '']));
  9644. end;
  9645. procedure TTestModule.TestCaseOfChar;
  9646. begin
  9647. StartProgram(false);
  9648. Add([
  9649. 'var s,h: AnsiChar;',
  9650. 'begin',
  9651. ' case s of',
  9652. ' ''a''..''z'': h:=s;',
  9653. ' ''ä'': ;',
  9654. ' ''ў'', ''ё'': ;',
  9655. ' ''Б''..''Я'': ;',
  9656. ' end;',
  9657. '']);
  9658. ConvertProgram;
  9659. CheckSource('TestCaseOfString',
  9660. LinesToStr([ // statements
  9661. 'this.s = "";',
  9662. 'this.h = "";',
  9663. '']),
  9664. LinesToStr([ // $mod.$main
  9665. 'var $tmp = $mod.s;',
  9666. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9667. ' $mod.h = $mod.s}',
  9668. ' else if ($tmp === "ä") {}',
  9669. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9670. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9671. '']));
  9672. end;
  9673. procedure TTestModule.TestCaseOfExternalClassConst;
  9674. begin
  9675. StartProgram(false);
  9676. Add([
  9677. '{$modeswitch externalclass}',
  9678. 'type',
  9679. ' TBird = class external name ''Bird''',
  9680. ' const e: longint;',
  9681. ' end;',
  9682. 'var vI: longint;',
  9683. 'begin',
  9684. ' case vi of',
  9685. ' 1: vi:=3;',
  9686. ' TBird.e: ;',
  9687. ' end;']);
  9688. ConvertProgram;
  9689. CheckSource('TestCaseOfExternalClassConst',
  9690. LinesToStr([ // statements
  9691. 'this.vI = 0;'
  9692. ]),
  9693. LinesToStr([ // $mod.$main
  9694. 'var $tmp = $mod.vI;',
  9695. 'if ($tmp === 1) {',
  9696. ' $mod.vI = 3}',
  9697. ' else if ($tmp === Bird.e) ;'
  9698. ]));
  9699. end;
  9700. procedure TTestModule.TestDebugger;
  9701. begin
  9702. StartProgram(false);
  9703. Add([
  9704. 'procedure DoIt;',
  9705. 'begin',
  9706. ' deBugger;',
  9707. ' DeBugger();',
  9708. 'end;',
  9709. 'begin',
  9710. ' Debugger;']);
  9711. ConvertProgram;
  9712. CheckSource('TestDebugger',
  9713. LinesToStr([ // statements
  9714. 'this.DoIt = function () {',
  9715. ' debugger;',
  9716. ' debugger;',
  9717. '};',
  9718. '']),
  9719. LinesToStr([ // $mod.$main
  9720. 'debugger;',
  9721. '']));
  9722. end;
  9723. procedure TTestModule.TestArray_Dynamic;
  9724. begin
  9725. StartProgram(false);
  9726. Add([
  9727. 'type',
  9728. ' TArrayInt = array of longint;',
  9729. 'var',
  9730. ' Arr: TArrayInt;',
  9731. ' i: longint;',
  9732. ' b: boolean;',
  9733. 'begin',
  9734. ' SetLength(arr,3);',
  9735. ' arr[0]:=4;',
  9736. ' arr[1]:=length(arr)+arr[0];',
  9737. ' arr[i]:=5;',
  9738. ' arr[arr[i]]:=arr[6];',
  9739. ' i:=low(arr);',
  9740. ' i:=high(arr);',
  9741. ' b:=Assigned(arr);',
  9742. ' Arr:=default(TArrayInt);']);
  9743. ConvertProgram;
  9744. CheckSource('TestArray_Dynamic',
  9745. LinesToStr([ // statements
  9746. 'this.Arr = [];',
  9747. 'this.i = 0;',
  9748. 'this.b = false;'
  9749. ]),
  9750. LinesToStr([ // $mod.$main
  9751. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9752. '$mod.Arr[0] = 4;',
  9753. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9754. '$mod.Arr[$mod.i] = 5;',
  9755. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9756. '$mod.i = 0;',
  9757. '$mod.i = rtl.length($mod.Arr) - 1;',
  9758. '$mod.b = rtl.length($mod.Arr) > 0;',
  9759. '$mod.Arr = [];',
  9760. '']));
  9761. end;
  9762. procedure TTestModule.TestArray_Dynamic_Nil;
  9763. begin
  9764. StartProgram(false);
  9765. Add('type');
  9766. Add(' TArrayInt = array of longint;');
  9767. Add('var');
  9768. Add(' Arr: TArrayInt;');
  9769. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9770. Add('begin');
  9771. Add(' arr:=nil;');
  9772. Add(' if arr=nil then;');
  9773. Add(' if nil=arr then;');
  9774. Add(' if arr<>nil then;');
  9775. Add(' if nil<>arr then;');
  9776. Add(' DoIt(nil,nil);');
  9777. ConvertProgram;
  9778. CheckSource('TestArray_Dynamic',
  9779. LinesToStr([ // statements
  9780. 'this.Arr = [];',
  9781. 'this.DoIt = function(i,j){',
  9782. '};'
  9783. ]),
  9784. LinesToStr([ // $mod.$main
  9785. '$mod.Arr = [];',
  9786. 'if (rtl.length($mod.Arr) === 0) ;',
  9787. 'if (rtl.length($mod.Arr) === 0) ;',
  9788. 'if (rtl.length($mod.Arr) > 0) ;',
  9789. 'if (rtl.length($mod.Arr) > 0) ;',
  9790. '$mod.DoIt([],[]);',
  9791. '']));
  9792. end;
  9793. procedure TTestModule.TestArray_DynMultiDimensional;
  9794. begin
  9795. StartProgram(false);
  9796. Add([
  9797. 'type',
  9798. ' TArrayInt = array of longint;',
  9799. ' TArrayArrayInt = array of TArrayInt;',
  9800. 'var',
  9801. ' Arr: TArrayInt;',
  9802. ' Arr2: TArrayArrayInt;',
  9803. ' i: longint;',
  9804. 'begin',
  9805. ' arr2:=nil;',
  9806. ' if arr2=nil then;',
  9807. ' if nil=arr2 then;',
  9808. ' i:=low(arr2);',
  9809. ' i:=low(arr2[1]);',
  9810. ' i:=high(arr2);',
  9811. ' i:=high(arr2[2]);',
  9812. ' arr2[3]:=arr;',
  9813. ' arr2[4][5]:=i;',
  9814. ' i:=arr2[6][7];',
  9815. ' arr2[8,9]:=i;',
  9816. ' i:=arr2[10,11];',
  9817. ' SetLength(arr2,14);',
  9818. ' SetLength(arr2[15],16);']);
  9819. ConvertProgram;
  9820. CheckSource('TestArray_Dynamic',
  9821. LinesToStr([ // statements
  9822. 'this.Arr = [];',
  9823. 'this.Arr2 = [];',
  9824. 'this.i = 0;'
  9825. ]),
  9826. LinesToStr([ // $mod.$main
  9827. '$mod.Arr2 = [];',
  9828. 'if (rtl.length($mod.Arr2) === 0) ;',
  9829. 'if (rtl.length($mod.Arr2) === 0) ;',
  9830. '$mod.i = 0;',
  9831. '$mod.i = 0;',
  9832. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9833. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9834. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9835. '$mod.Arr2[4][5] = $mod.i;',
  9836. '$mod.i = $mod.Arr2[6][7];',
  9837. '$mod.Arr2[8][9] = $mod.i;',
  9838. '$mod.i = $mod.Arr2[10][11];',
  9839. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9840. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9841. '']));
  9842. end;
  9843. procedure TTestModule.TestArray_DynamicAssign;
  9844. begin
  9845. StartProgram(false);
  9846. Add([
  9847. 'type',
  9848. ' TArrayInt = array of longint;',
  9849. ' TArrayArrayInt = array of TArrayInt;',
  9850. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9851. 'begin',
  9852. 'end;',
  9853. 'procedure Fly(var a: TArrayInt);',
  9854. 'begin',
  9855. 'end;',
  9856. 'var',
  9857. ' Arr: TArrayInt;',
  9858. ' Arr2: TArrayArrayInt;',
  9859. 'begin',
  9860. ' arr:=nil;',
  9861. ' arr2:=nil;',
  9862. ' arr2[1]:=nil;',
  9863. ' arr2[2]:=arr;',
  9864. ' Run(arr,arr,arr);',
  9865. ' Fly(arr);',
  9866. ' Run(arr2[4],arr2[5],arr2[6]);',
  9867. ' Fly(arr2[7]);',
  9868. '']);
  9869. ConvertProgram;
  9870. CheckSource('TestArray_DynamicAssign',
  9871. LinesToStr([ // statements
  9872. 'this.Run = function (a, b, c) {',
  9873. '};',
  9874. 'this.Fly = function (a) {',
  9875. '};',
  9876. 'this.Arr = [];',
  9877. 'this.Arr2 = [];',
  9878. '']),
  9879. LinesToStr([ // $mod.$main
  9880. '$mod.Arr = [];',
  9881. '$mod.Arr2 = [];',
  9882. '$mod.Arr2[1] = [];',
  9883. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9884. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9885. '$mod.Fly({',
  9886. ' p: $mod,',
  9887. ' get: function () {',
  9888. ' return this.p.Arr;',
  9889. ' },',
  9890. ' set: function (v) {',
  9891. ' this.p.Arr = v;',
  9892. ' }',
  9893. '});',
  9894. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9895. '$mod.Fly({',
  9896. ' a: 7,',
  9897. ' p: $mod.Arr2,',
  9898. ' get: function () {',
  9899. ' return this.p[this.a];',
  9900. ' },',
  9901. ' set: function (v) {',
  9902. ' this.p[this.a] = v;',
  9903. ' }',
  9904. '});',
  9905. '']));
  9906. end;
  9907. procedure TTestModule.TestArray_StaticInt;
  9908. begin
  9909. StartProgram(false);
  9910. Add('type');
  9911. Add(' TArrayInt = array[2..4] of longint;');
  9912. Add('var');
  9913. Add(' Arr: TArrayInt;');
  9914. Add(' Arr2: TArrayInt = (5,6,7);');
  9915. Add(' i: longint;');
  9916. Add(' b: boolean;');
  9917. Add('begin');
  9918. Add(' arr[2]:=4;');
  9919. Add(' arr[3]:=arr[2]+arr[3];');
  9920. Add(' arr[i]:=5;');
  9921. Add(' arr[arr[i]]:=arr[high(arr)];');
  9922. Add(' i:=low(arr);');
  9923. Add(' i:=high(arr);');
  9924. Add(' b:=arr[2]=arr[3];');
  9925. Add(' arr:=default(TArrayInt);');
  9926. ConvertProgram;
  9927. CheckSource('TestArray_StaticInt',
  9928. LinesToStr([ // statements
  9929. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9930. 'this.Arr2 = [5, 6, 7];',
  9931. 'this.i = 0;',
  9932. 'this.b = false;'
  9933. ]),
  9934. LinesToStr([ // $mod.$main
  9935. '$mod.Arr[0] = 4;',
  9936. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9937. '$mod.Arr[$mod.i-2] = 5;',
  9938. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9939. '$mod.i = 2;',
  9940. '$mod.i = 4;',
  9941. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9942. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9943. '']));
  9944. end;
  9945. procedure TTestModule.TestArray_StaticBool;
  9946. begin
  9947. StartProgram(false);
  9948. Add('type');
  9949. Add(' TBools = array[boolean] of boolean;');
  9950. Add(' TBool2 = array[true..true] of boolean;');
  9951. Add('var');
  9952. Add(' Arr: TBools;');
  9953. Add(' Arr2: TBool2;');
  9954. Add(' Arr3: TBools = (true,false);');
  9955. Add(' b: boolean;');
  9956. Add('begin');
  9957. Add(' b:=low(arr);');
  9958. Add(' b:=high(arr);');
  9959. Add(' arr[true]:=false;');
  9960. Add(' arr[false]:=arr[b] or arr[true];');
  9961. Add(' arr[b]:=true;');
  9962. Add(' arr[arr[b]]:=arr[high(arr)];');
  9963. Add(' b:=arr[false]=arr[true];');
  9964. Add(' b:=low(arr2);');
  9965. Add(' b:=high(arr2);');
  9966. Add(' arr2[true]:=true;');
  9967. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9968. Add(' arr2[b]:=false;');
  9969. ConvertProgram;
  9970. CheckSource('TestArray_StaticBool',
  9971. LinesToStr([ // statements
  9972. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9973. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9974. 'this.Arr3 = [true, false];',
  9975. 'this.b = false;'
  9976. ]),
  9977. LinesToStr([ // $mod.$main
  9978. '$mod.b = false;',
  9979. '$mod.b = true;',
  9980. '$mod.Arr[1] = false;',
  9981. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9982. '$mod.Arr[+$mod.b] = true;',
  9983. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9984. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9985. '$mod.b = true;',
  9986. '$mod.b = true;',
  9987. '$mod.Arr2[0] = true;',
  9988. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9989. '$mod.Arr2[1-$mod.b] = false;',
  9990. '']));
  9991. end;
  9992. procedure TTestModule.TestArray_StaticChar;
  9993. begin
  9994. StartProgram(false);
  9995. Add([
  9996. 'type',
  9997. ' TChars = array[AnsiChar] of AnsiChar;',
  9998. ' TChars2 = array[''a''..''z''] of AnsiChar;',
  9999. 'var',
  10000. ' Arr: TChars;',
  10001. ' Arr2: TChars2;',
  10002. ' Arr3: array[2..4] of AnsiChar = (''p'',''a'',''s'');',
  10003. ' Arr4: array[11..13] of AnsiChar = ''pas'';',
  10004. ' Arr5: array[21..22] of AnsiChar = ''äö'';',
  10005. ' Arr6: array[31..32] of AnsiChar = ''ä''+''ö'';',
  10006. ' c: AnsiChar;',
  10007. ' b: boolean;',
  10008. 'begin',
  10009. ' c:=low(arr);',
  10010. ' c:=high(arr);',
  10011. ' arr[''B'']:=''a'';',
  10012. ' arr[''D'']:=arr[c];',
  10013. ' arr[c]:=arr[''d''];',
  10014. ' arr[arr[c]]:=arr[high(arr)];',
  10015. ' b:=arr[low(arr)]=arr[''e''];',
  10016. ' c:=low(arr2);',
  10017. ' c:=high(arr2);',
  10018. ' arr2[''b'']:=''f'';',
  10019. ' arr2[''a'']:=arr2[c];',
  10020. ' arr2[c]:=arr2[''g''];']);
  10021. ConvertProgram;
  10022. CheckSource('TestArray_StaticChar',
  10023. LinesToStr([ // statements
  10024. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  10025. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  10026. 'this.Arr3 = ["p", "a", "s"];',
  10027. 'this.Arr4 = ["p", "a", "s"];',
  10028. 'this.Arr5 = ["ä", "ö"];',
  10029. 'this.Arr6 = ["ä", "ö"];',
  10030. 'this.c = "";',
  10031. 'this.b = false;',
  10032. '']),
  10033. LinesToStr([ // $mod.$main
  10034. '$mod.c = "\x00";',
  10035. '$mod.c = "\uFFFF";',
  10036. '$mod.Arr[66] = "a";',
  10037. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10038. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10039. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10040. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10041. '$mod.c = "a";',
  10042. '$mod.c = "z";',
  10043. '$mod.Arr2[1] = "f";',
  10044. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10045. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10046. '']));
  10047. end;
  10048. procedure TTestModule.TestArray_StaticMultiDim;
  10049. begin
  10050. StartProgram(false);
  10051. Add([
  10052. 'type',
  10053. ' TArrayInt = array[1..3] of longint;',
  10054. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10055. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10056. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10057. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10058. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10059. 'var',
  10060. ' Arr: TArrayInt;',
  10061. ' Arr2: TArrayArrayInt;',
  10062. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10063. ' Arr4: TArrayArrayInt;',
  10064. ' ArrDim2: TArrayDim2Int;',
  10065. ' ArrDim3: TArrayDim3Int;',
  10066. ' ArrDim4: TArrayDim4Int;',
  10067. ' i: longint;',
  10068. 'begin',
  10069. ' i:=low(arr);',
  10070. ' i:=low(arr2);',
  10071. ' i:=low(arr2[5]);',
  10072. ' i:=high(arr);',
  10073. ' i:=high(arr2);',
  10074. ' i:=high(arr2[6]);',
  10075. ' arr2[5]:=arr;',
  10076. ' arr2[6][2]:=i;',
  10077. ' i:=arr2[6][3];',
  10078. ' arr2[6,3]:=i;',
  10079. ' i:=arr2[5,2];',
  10080. ' arr2:=arr2;',// clone multi dim static array
  10081. ' arr3:=arr3;',// clone anonymous multi dim static array
  10082. ' arr4:=arr4;',
  10083. ' Arr:=Arr;',
  10084. ' ArrDim2:=ArrDim2;',
  10085. ' ArrDim3:=ArrDim3;',
  10086. ' ArrDim4:=ArrDim4;',
  10087. '']);
  10088. ConvertProgram;
  10089. CheckSource('TestArray_StaticMultiDim',
  10090. LinesToStr([ // statements
  10091. 'this.TArrayArrayInt$clone = function (a) {',
  10092. ' var b = [];',
  10093. ' b.length = 2;',
  10094. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10095. ' return b;',
  10096. '};',
  10097. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10098. ' var b = [];',
  10099. ' b.length = 2;',
  10100. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10101. ' return b;',
  10102. '};',
  10103. 'this.TArrayDim2Int$clone = function (a) {',
  10104. ' var b = [];',
  10105. ' b.length = 2;',
  10106. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10107. ' return b;',
  10108. '};',
  10109. 'this.TArrayDim3Int$clone = function (a) {',
  10110. ' var b = [];',
  10111. ' b.length = 2;',
  10112. ' for (var c = 0; c < 2; c++) {',
  10113. ' var d = b[c] = [];',
  10114. ' d.length = 3;',
  10115. ' var e = a[c];',
  10116. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10117. ' };',
  10118. ' return b;',
  10119. '};',
  10120. 'this.TArrayDim4Int$clone = function (a) {',
  10121. ' var b = [];',
  10122. ' b.length = 2;',
  10123. ' for (var c = 0; c < 2; c++) {',
  10124. ' var d = b[c] = [];',
  10125. ' d.length = 3;',
  10126. ' var e = a[c];',
  10127. ' for (var f = 0; f < 3; f++) {',
  10128. ' var g = d[f] = [];',
  10129. ' g.length = 4;',
  10130. ' var h = e[f];',
  10131. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10132. ' };',
  10133. ' };',
  10134. ' return b;',
  10135. '};',
  10136. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10137. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10138. 'this.Arr3$a$clone = function (a) {',
  10139. ' var b = [];',
  10140. ' b.length = 2;',
  10141. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10142. ' return b;',
  10143. '};',
  10144. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10145. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10146. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10147. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10148. 'this.ArrDim4 = rtl.arraySetLength(',
  10149. ' null,',
  10150. ' 0,',
  10151. ' 2,',
  10152. ' 3,',
  10153. ' 4,',
  10154. ' 5',
  10155. ');',
  10156. 'this.i = 0;'
  10157. ]),
  10158. LinesToStr([ // $mod.$main
  10159. '$mod.i = 1;',
  10160. '$mod.i = 5;',
  10161. '$mod.i = 1;',
  10162. '$mod.i = 3;',
  10163. '$mod.i = 6;',
  10164. '$mod.i = 3;',
  10165. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10166. '$mod.Arr2[1][1] = $mod.i;',
  10167. '$mod.i = $mod.Arr2[1][2];',
  10168. '$mod.Arr2[1][2] = $mod.i;',
  10169. '$mod.i = $mod.Arr2[0][1];',
  10170. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10171. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10172. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10173. '$mod.Arr = $mod.Arr.slice(0);',
  10174. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10175. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10176. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10177. '']));
  10178. end;
  10179. procedure TTestModule.TestArray_StaticInFunction;
  10180. begin
  10181. StartProgram(false);
  10182. Add([
  10183. 'const TArrayInt = 3;',
  10184. 'const TArrayArrayInt = 4;',
  10185. 'procedure DoIt;',
  10186. 'type',
  10187. ' TArrayInt = array[1..3] of longint;',
  10188. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10189. 'var',
  10190. ' Arr: TArrayInt;',
  10191. ' Arr2: TArrayArrayInt;',
  10192. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10193. ' i: longint;',
  10194. 'begin',
  10195. ' arr2[5]:=arr;',
  10196. ' arr2:=arr2;',// clone multi dim static array
  10197. ' arr3:=arr3;',// clone multi dim anonymous static array
  10198. 'end;',
  10199. 'begin',
  10200. '']);
  10201. ConvertProgram;
  10202. CheckSource('TestArray_StaticInFunction',
  10203. LinesToStr([ // statements
  10204. 'this.TArrayInt = 3;',
  10205. 'this.TArrayArrayInt = 4;',
  10206. 'var TArrayArrayInt$1$clone = function (a) {',
  10207. ' var b = [];',
  10208. ' b.length = 2;',
  10209. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10210. ' return b;',
  10211. '};',
  10212. 'var Arr3$a$clone = function (a) {',
  10213. ' var b = [];',
  10214. ' b.length = 2;',
  10215. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10216. ' return b;',
  10217. '};',
  10218. 'this.DoIt = function () {',
  10219. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10220. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10221. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10222. ' var i = 0;',
  10223. ' Arr2[0] = Arr.slice(0);',
  10224. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10225. ' Arr3 = Arr3$a$clone(Arr3);',
  10226. '};',
  10227. '']),
  10228. LinesToStr([ // $mod.$main
  10229. '']));
  10230. end;
  10231. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10232. begin
  10233. StartProgram(false);
  10234. Add([
  10235. 'type',
  10236. ' TArrayInt = array[1..3,1..2] of longint;',
  10237. 'var',
  10238. ' a,b: TArrayInt;',
  10239. 'begin',
  10240. ' if a=b then ;',
  10241. '']);
  10242. SetExpectedPasResolverError('compare static array is not supported',
  10243. nXIsNotSupported);
  10244. ConvertProgram;
  10245. end;
  10246. procedure TTestModule.TestArrayOfRecord;
  10247. begin
  10248. StartProgram(false);
  10249. Add([
  10250. 'type',
  10251. ' TRec = record',
  10252. ' Int: longint;',
  10253. ' end;',
  10254. ' TArrayRec = array of TRec;',
  10255. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10256. 'begin',
  10257. 'end;',
  10258. 'var',
  10259. ' Arr: TArrayRec;',
  10260. ' r: TRec;',
  10261. ' i: longint;',
  10262. 'begin',
  10263. ' SetLength(arr,3);',
  10264. ' arr[0].int:=4;',
  10265. ' arr[1].int:=length(arr)+arr[2].int;',
  10266. ' arr[arr[i].int].int:=arr[5].int;',
  10267. ' arr[7]:=r;',
  10268. ' r:=arr[8];',
  10269. ' i:=low(arr);',
  10270. ' i:=high(arr);',
  10271. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10272. ConvertProgram;
  10273. CheckSource('TestArrayOfRecord',
  10274. LinesToStr([ // statements
  10275. 'rtl.recNewT(this, "TRec", function () {',
  10276. ' this.Int = 0;',
  10277. ' this.$eq = function (b) {',
  10278. ' return this.Int === b.Int;',
  10279. ' };',
  10280. ' this.$assign = function (s) {',
  10281. ' this.Int = s.Int;',
  10282. ' return this;',
  10283. ' };',
  10284. '});',
  10285. 'this.DoIt = function (vd, vc, vv) {',
  10286. '};',
  10287. 'this.Arr = [];',
  10288. 'this.r = this.TRec.$new();',
  10289. 'this.i = 0;'
  10290. ]),
  10291. LinesToStr([ // $mod.$main
  10292. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10293. '$mod.Arr[0].Int = 4;',
  10294. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10295. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10296. '$mod.Arr[7].$assign($mod.r);',
  10297. '$mod.r.$assign($mod.Arr[8]);',
  10298. '$mod.i = 0;',
  10299. '$mod.i = rtl.length($mod.Arr)-1;',
  10300. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10301. '']));
  10302. end;
  10303. procedure TTestModule.TestArray_StaticRecord;
  10304. begin
  10305. StartProgram(false);
  10306. Add([
  10307. 'type',
  10308. ' TRec = record',
  10309. ' Int: longint;',
  10310. ' end;',
  10311. ' TArrayRec = array[1..2] of TRec;',
  10312. 'var',
  10313. ' Arr: TArrayRec;',
  10314. 'begin',
  10315. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10316. '']);
  10317. ConvertProgram;
  10318. CheckSource('TestArray_StaticRecord',
  10319. LinesToStr([ // statements
  10320. 'rtl.recNewT(this, "TRec", function () {',
  10321. ' this.Int = 0;',
  10322. ' this.$eq = function (b) {',
  10323. ' return this.Int === b.Int;',
  10324. ' };',
  10325. ' this.$assign = function (s) {',
  10326. ' this.Int = s.Int;',
  10327. ' return this;',
  10328. ' };',
  10329. '});',
  10330. 'this.TArrayRec$clone = function (a) {',
  10331. ' var b = [];',
  10332. ' b.length = 2;',
  10333. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10334. ' return b;',
  10335. '};',
  10336. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10337. '']),
  10338. LinesToStr([ // $mod.$main
  10339. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10340. end;
  10341. procedure TTestModule.TestArrayOfSet;
  10342. begin
  10343. StartProgram(false);
  10344. Add([
  10345. 'type',
  10346. ' TFlag = (big,small);',
  10347. ' TSetOfFlag = set of tflag;',
  10348. ' TArrayFlag = array of TSetOfFlag;',
  10349. 'procedure DoIt(const a: Tarrayflag);',
  10350. 'begin',
  10351. 'end;',
  10352. 'var',
  10353. ' f: TFlag;',
  10354. ' s: TSetOfFlag;',
  10355. ' Arr: TArrayFlag;',
  10356. ' i: longint;',
  10357. 'begin',
  10358. ' SetLength(arr,3);',
  10359. ' arr[0]:=s;',
  10360. ' arr[1]:=[big];',
  10361. ' arr[2]:=[big]+s;',
  10362. ' arr[3]:=s+[big];',
  10363. ' arr[4]:=arr[5];',
  10364. ' s:=arr[6];',
  10365. ' i:=low(arr);',
  10366. ' i:=high(arr);',
  10367. ' DoIt(arr);',
  10368. ' DoIt([s]);',
  10369. ' DoIt([[],s]);',
  10370. ' DoIt([s,[]]);',
  10371. '']);
  10372. ConvertProgram;
  10373. CheckSource('TestArrayOfSet',
  10374. LinesToStr([ // statements
  10375. 'this.TFlag = {',
  10376. ' "0": "big",',
  10377. ' big: 0,',
  10378. ' "1": "small",',
  10379. ' small: 1',
  10380. '};',
  10381. 'this.DoIt = function (a) {',
  10382. '};',
  10383. 'this.f = 0;',
  10384. 'this.s = {};',
  10385. 'this.Arr = [];',
  10386. 'this.i = 0;',
  10387. '']),
  10388. LinesToStr([ // $mod.$main
  10389. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10390. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10391. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10392. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10393. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10394. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10395. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10396. '$mod.i = 0;',
  10397. '$mod.i = rtl.length($mod.Arr) - 1;',
  10398. '$mod.DoIt($mod.Arr);',
  10399. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10400. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10401. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10402. '']));
  10403. end;
  10404. procedure TTestModule.TestArray_DynAsParam;
  10405. begin
  10406. StartProgram(false);
  10407. Add([
  10408. 'type integer = longint;',
  10409. 'type TArrInt = array of integer;',
  10410. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10411. 'var vJ: TArrInt;',
  10412. 'begin',
  10413. ' vg:=vg;',
  10414. ' vj:=vh;',
  10415. ' vi:=vi;',
  10416. ' doit(vg,vg,vg);',
  10417. ' doit(vh,vh,vj);',
  10418. ' doit(vi,vi,vi);',
  10419. ' doit(vj,vj,vj);',
  10420. 'end;',
  10421. 'var i: TArrInt;',
  10422. 'begin',
  10423. ' doit(i,i,i);']);
  10424. ConvertProgram;
  10425. CheckSource('TestArray_DynAsParams',
  10426. LinesToStr([ // statements
  10427. 'this.DoIt = function (vG,vH,vI) {',
  10428. ' var vJ = [];',
  10429. ' vG = rtl.arrayRef(vG);',
  10430. ' vJ = rtl.arrayRef(vH);',
  10431. ' vI.set(rtl.arrayRef(vI.get()));',
  10432. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10433. ' get: function () {',
  10434. ' return vG;',
  10435. ' },',
  10436. ' set: function (v) {',
  10437. ' vG = v;',
  10438. ' }',
  10439. ' });',
  10440. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10441. ' get: function () {',
  10442. ' return vJ;',
  10443. ' },',
  10444. ' set: function (v) {',
  10445. ' vJ = v;',
  10446. ' }',
  10447. ' });',
  10448. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10449. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10450. ' get: function () {',
  10451. ' return vJ;',
  10452. ' },',
  10453. ' set: function (v) {',
  10454. ' vJ = v;',
  10455. ' }',
  10456. ' });',
  10457. '};',
  10458. 'this.i = [];'
  10459. ]),
  10460. LinesToStr([
  10461. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10462. ' p: $mod,',
  10463. ' get: function () {',
  10464. ' return this.p.i;',
  10465. ' },',
  10466. ' set: function (v) {',
  10467. ' this.p.i = v;',
  10468. ' }',
  10469. '});'
  10470. ]));
  10471. end;
  10472. procedure TTestModule.TestArray_StaticAsParam;
  10473. begin
  10474. StartProgram(false);
  10475. Add([
  10476. 'type integer = longint;',
  10477. 'type TArrInt = array[1..2] of integer;',
  10478. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10479. 'var vJ: TArrInt;',
  10480. 'begin',
  10481. ' vg:=vg;',
  10482. ' vj:=vh;',
  10483. ' vi:=vi;',
  10484. ' doit(vg,vg,vg);',
  10485. ' doit(vh,vh,vj);',
  10486. ' doit(vi,vi,vi);',
  10487. ' doit(vj,vj,vj);',
  10488. 'end;',
  10489. 'var i: TArrInt;',
  10490. 'begin',
  10491. ' doit(i,i,i);']);
  10492. ConvertProgram;
  10493. CheckSource('TestArray_StaticAsParams',
  10494. LinesToStr([ // statements
  10495. 'this.DoIt = function (vG,vH,vI) {',
  10496. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10497. ' vG = vG.slice(0);',
  10498. ' vJ = vH.slice(0);',
  10499. ' vI.set(vI.get().slice(0));',
  10500. ' $mod.DoIt(vG.slice(0), vG, {',
  10501. ' get: function () {',
  10502. ' return vG;',
  10503. ' },',
  10504. ' set: function (v) {',
  10505. ' vG = v;',
  10506. ' }',
  10507. ' });',
  10508. ' $mod.DoIt(vH.slice(0), vH, {',
  10509. ' get: function () {',
  10510. ' return vJ;',
  10511. ' },',
  10512. ' set: function (v) {',
  10513. ' vJ = v;',
  10514. ' }',
  10515. ' });',
  10516. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10517. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10518. ' get: function () {',
  10519. ' return vJ;',
  10520. ' },',
  10521. ' set: function (v) {',
  10522. ' vJ = v;',
  10523. ' }',
  10524. ' });',
  10525. '};',
  10526. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10527. ]),
  10528. LinesToStr([
  10529. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10530. ' p: $mod,',
  10531. ' get: function () {',
  10532. ' return this.p.i;',
  10533. ' },',
  10534. ' set: function (v) {',
  10535. ' this.p.i = v;',
  10536. ' }',
  10537. '});'
  10538. ]));
  10539. end;
  10540. procedure TTestModule.TestArrayElement_AsParams;
  10541. begin
  10542. StartProgram(false);
  10543. Add('type integer = longint;');
  10544. Add('type TArrayInt = array of integer;');
  10545. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10546. Add('var vJ: tarrayint;');
  10547. Add('begin');
  10548. Add(' vi:=vi;');
  10549. Add(' doit(vi,vi,vi);');
  10550. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10551. Add('end;');
  10552. Add('var a: TArrayInt;');
  10553. Add('begin');
  10554. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10555. ConvertProgram;
  10556. CheckSource('TestArrayElement_AsParams',
  10557. LinesToStr([ // statements
  10558. 'this.DoIt = function (vG,vH,vI) {',
  10559. ' var vJ = [];',
  10560. ' vI.set(vI.get());',
  10561. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10562. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10563. ' a:1+3,',
  10564. ' p:vJ,',
  10565. ' get: function () {',
  10566. ' return this.p[this.a];',
  10567. ' },',
  10568. ' set: function (v) {',
  10569. ' this.p[this.a] = v;',
  10570. ' }',
  10571. ' });',
  10572. '};',
  10573. 'this.a = [];'
  10574. ]),
  10575. LinesToStr([
  10576. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10577. ' a: 1+6,',
  10578. ' p: $mod.a,',
  10579. ' get: function () {',
  10580. ' return this.p[this.a];',
  10581. ' },',
  10582. ' set: function (v) {',
  10583. ' this.p[this.a] = v;',
  10584. ' }',
  10585. '});'
  10586. ]));
  10587. end;
  10588. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10589. begin
  10590. StartProgram(false);
  10591. Add('type Integer = longint;');
  10592. Add('type TArrayInt = array of integer;');
  10593. Add('function GetArr(vB: integer = 0): tarrayint;');
  10594. Add('begin');
  10595. Add('end;');
  10596. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10597. Add('begin');
  10598. Add('end;');
  10599. Add('begin');
  10600. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10601. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10602. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10603. ConvertProgram;
  10604. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10605. LinesToStr([ // statements
  10606. 'this.GetArr = function (vB) {',
  10607. ' var Result = [];',
  10608. ' return Result;',
  10609. '};',
  10610. 'this.DoIt = function (vG,vH,vI) {',
  10611. '};'
  10612. ]),
  10613. LinesToStr([
  10614. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10615. ' a: 1+3,',
  10616. ' p: $mod.GetArr(0),',
  10617. ' get: function () {',
  10618. ' return this.p[this.a];',
  10619. ' },',
  10620. ' set: function (v) {',
  10621. ' this.p[this.a] = v;',
  10622. ' }',
  10623. '});',
  10624. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10625. ' a: 2+3,',
  10626. ' p: $mod.GetArr(0),',
  10627. ' get: function () {',
  10628. ' return this.p[this.a];',
  10629. ' },',
  10630. ' set: function (v) {',
  10631. ' this.p[this.a] = v;',
  10632. ' }',
  10633. '});',
  10634. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10635. ' a: 3+3,',
  10636. ' p: $mod.GetArr(9),',
  10637. ' get: function () {',
  10638. ' return this.p[this.a];',
  10639. ' },',
  10640. ' set: function (v) {',
  10641. ' this.p[this.a] = v;',
  10642. ' }',
  10643. '});',
  10644. '']));
  10645. end;
  10646. procedure TTestModule.TestArrayEnumTypeRange;
  10647. begin
  10648. StartProgram(false);
  10649. Add([
  10650. 'type',
  10651. ' TEnum = (red,blue);',
  10652. ' TEnumArray = array[TEnum] of longint;',
  10653. 'var',
  10654. ' e: TEnum;',
  10655. ' i: longint;',
  10656. ' a: TEnumArray;',
  10657. ' numbers: TEnumArray = (1,2);',
  10658. ' names: array[TEnum] of string = (''red'',''blue'');',
  10659. 'begin',
  10660. ' e:=low(a);',
  10661. ' e:=high(a);',
  10662. ' i:=a[red];',
  10663. ' a[e]:=a[e];']);
  10664. ConvertProgram;
  10665. CheckSource('TestArrayEnumTypeRange',
  10666. LinesToStr([ // statements
  10667. ' this.TEnum = {',
  10668. ' "0": "red",',
  10669. ' red: 0,',
  10670. ' "1": "blue",',
  10671. ' blue: 1',
  10672. '};',
  10673. 'this.e = 0;',
  10674. 'this.i = 0;',
  10675. 'this.a = rtl.arraySetLength(null,0,2);',
  10676. 'this.numbers = [1, 2];',
  10677. 'this.names = ["red", "blue"];',
  10678. '']),
  10679. LinesToStr([ // $mod.$main
  10680. '$mod.e = $mod.TEnum.red;',
  10681. '$mod.e = $mod.TEnum.blue;',
  10682. '$mod.i = $mod.a[$mod.TEnum.red];',
  10683. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10684. '']));
  10685. end;
  10686. procedure TTestModule.TestArray_SetLengthOutArg;
  10687. begin
  10688. StartProgram(false);
  10689. Add([
  10690. 'type TArrInt = array of longint;',
  10691. 'procedure DoIt(out a: TArrInt);',
  10692. 'begin',
  10693. ' SetLength(a,2);',
  10694. 'end;',
  10695. 'begin',
  10696. '']);
  10697. ConvertProgram;
  10698. CheckSource('TestArray_SetLengthOutArg',
  10699. LinesToStr([ // statements
  10700. 'this.DoIt = function (a) {',
  10701. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10702. '};',
  10703. '']),
  10704. LinesToStr([
  10705. '']));
  10706. end;
  10707. procedure TTestModule.TestArray_SetLengthProperty;
  10708. begin
  10709. StartProgram(false);
  10710. Add('type');
  10711. Add(' TArrInt = array of longint;');
  10712. Add(' TObject = class');
  10713. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10714. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10715. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10716. Add(' end;');
  10717. Add('var Obj: TObject;');
  10718. Add('begin');
  10719. Add(' SetLength(Obj.Colors,2);');
  10720. ConvertProgram;
  10721. CheckSource('TestArray_SetLengthProperty',
  10722. LinesToStr([ // statements
  10723. 'rtl.createClass(this, "TObject", null, function () {',
  10724. ' this.$init = function () {',
  10725. ' };',
  10726. ' this.$final = function () {',
  10727. ' };',
  10728. '});',
  10729. 'this.Obj = null;',
  10730. '']),
  10731. LinesToStr([
  10732. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10733. '']));
  10734. end;
  10735. procedure TTestModule.TestArray_SetLengthMultiDim;
  10736. begin
  10737. StartProgram(false);
  10738. Add([
  10739. 'type',
  10740. ' TArrArrInt = array of array of longint;',
  10741. ' TArrStaInt = array of array[1..2] of longint;',
  10742. 'var',
  10743. ' a: TArrArrInt;',
  10744. ' b: TArrStaInt;',
  10745. 'begin',
  10746. ' SetLength(a,2);',
  10747. ' SetLength(a,3,4);',
  10748. ' SetLength(b,5);',
  10749. '']);
  10750. ConvertProgram;
  10751. CheckSource('TestArray_SetLengthMultiDim',
  10752. LinesToStr([ // statements
  10753. 'this.a = [];',
  10754. 'this.b = [];',
  10755. '']),
  10756. LinesToStr([
  10757. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10758. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10759. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10760. '']));
  10761. end;
  10762. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10763. begin
  10764. StartProgram(false);
  10765. Add([
  10766. 'type',
  10767. ' TStaArr1 = array[1..3] of boolean;',
  10768. //' TStaArr2 = array[5..6] of TStaArr1;',
  10769. ' TDynArr1StaArr1 = array of TStaArr1;',
  10770. //' TDynArr1StaArr2 = array of TStaArr2;',
  10771. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10772. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10773. 'var',
  10774. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10775. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10776. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10777. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10778. 'begin',
  10779. ' SetLength(DynArr1StaArr1,11);',
  10780. ' SetLength(DynArr2StaArr1,12);',
  10781. ' SetLength(DynArr2StaArr1[13],14);',
  10782. ' SetLength(DynArr2StaArr1,15,16);',
  10783. //' SetLength(DynArr1StaArr2,21);',
  10784. //' SetLength(DynArr2StaArr2,22);',
  10785. //' SetLength(DynArr2StaArr2[23],24);',
  10786. //' SetLength(DynArr2StaArr2,25,26);',
  10787. '']);
  10788. ConvertProgram;
  10789. CheckSource('TestArray_DynOfStatic',
  10790. LinesToStr([ // statements
  10791. 'this.DynArr1StaArr1 = [];',
  10792. 'this.DynArr2StaArr1 = [];',
  10793. '']),
  10794. LinesToStr([ // $mod.$main
  10795. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10796. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10797. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10798. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10799. ' $mod.DynArr2StaArr1,',
  10800. ' false,',
  10801. ' 15,',
  10802. ' 16,',
  10803. ' "s",',
  10804. ' 3',
  10805. ');',
  10806. '']));
  10807. end;
  10808. procedure TTestModule.TestArray_OpenArrayOfString;
  10809. begin
  10810. StartProgram(false);
  10811. Add('procedure DoIt(const a: array of String);');
  10812. Add('var');
  10813. Add(' i: longint;');
  10814. Add(' s: string;');
  10815. Add('begin');
  10816. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10817. Add('end;');
  10818. Add('var s: string;');
  10819. Add('begin');
  10820. Add(' DoIt([]);');
  10821. Add(' DoIt([s,''foo'','''',s+s]);');
  10822. ConvertProgram;
  10823. CheckSource('TestArray_OpenArrayOfString',
  10824. LinesToStr([ // statements
  10825. 'this.DoIt = function (a) {',
  10826. ' var i = 0;',
  10827. ' var s = "";',
  10828. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10829. ' i = $l;',
  10830. ' s = a[rtl.length(a) - i - 1];',
  10831. ' };',
  10832. '};',
  10833. 'this.s = "";',
  10834. '']),
  10835. LinesToStr([
  10836. '$mod.DoIt([]);',
  10837. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10838. '']));
  10839. end;
  10840. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10841. begin
  10842. StartProgram(false);
  10843. Add([
  10844. 'type TArr = array of AnsiChar;',
  10845. 'var',
  10846. ' c: AnsiChar;',
  10847. ' s: string;',
  10848. ' a: TArr;',
  10849. 'procedure Run(const a: array of AnsiChar);',
  10850. 'begin',
  10851. ' Run(c);',
  10852. ' Run(s);',
  10853. 'end;',
  10854. 'begin',
  10855. ' a:=c;',
  10856. ' a:=s;',
  10857. ' a:=#13;',
  10858. ' a:=''Foo'';',
  10859. ' Run(c);',
  10860. ' Run(s);',
  10861. '']);
  10862. ConvertProgram;
  10863. CheckSource('TestArray_ArrayOfCharAssignString',
  10864. LinesToStr([ // statements
  10865. 'this.c = "";',
  10866. 'this.s = "";',
  10867. 'this.a = [];',
  10868. 'this.Run = function (a) {',
  10869. ' $mod.Run($mod.c.split(""));',
  10870. ' $mod.Run($mod.s.split(""));',
  10871. '};',
  10872. '']),
  10873. LinesToStr([
  10874. '$mod.a = $mod.c.split("");',
  10875. '$mod.a = $mod.s.split("");',
  10876. '$mod.a = "\r".split("");',
  10877. '$mod.a = "Foo".split("");',
  10878. '$mod.Run($mod.c.split(""));',
  10879. '$mod.Run($mod.s.split(""));',
  10880. '']));
  10881. end;
  10882. procedure TTestModule.TestArray_ConstRef;
  10883. begin
  10884. StartProgram(false);
  10885. Add([
  10886. 'type TArr = array of word;',
  10887. 'procedure Run(constref a: TArr);',
  10888. 'begin',
  10889. 'end;',
  10890. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10891. 'var l: TArr;',
  10892. 'begin',
  10893. ' Run(l);',
  10894. ' Run(a);',
  10895. ' Run(b);',
  10896. ' Run(c);',
  10897. ' Run(d);',
  10898. ' Run(e);',
  10899. 'end;',
  10900. 'begin',
  10901. '']);
  10902. ConvertProgram;
  10903. CheckResolverUnexpectedHints();
  10904. CheckSource('TestArray_ConstRef',
  10905. LinesToStr([ // statements
  10906. 'this.Run = function (a) {',
  10907. '};',
  10908. 'this.Fly = function (a, b, c, d, e) {',
  10909. ' var l = [];',
  10910. ' $mod.Run(l);',
  10911. ' $mod.Run(a);',
  10912. ' $mod.Run(b.get());',
  10913. ' $mod.Run(c.get());',
  10914. ' $mod.Run(d);',
  10915. ' $mod.Run(e);',
  10916. '};',
  10917. '']),
  10918. LinesToStr([
  10919. '']));
  10920. end;
  10921. procedure TTestModule.TestArray_Concat;
  10922. begin
  10923. StartProgram(false);
  10924. Add([
  10925. 'type',
  10926. ' integer = longint;',
  10927. ' TFlag = (big,small);',
  10928. ' TFlags = set of TFlag;',
  10929. ' TRec = record',
  10930. ' i: integer;',
  10931. ' end;',
  10932. ' TArrInt = array of integer;',
  10933. ' TArrRec = array of TRec;',
  10934. ' TArrFlag = array of TFlag;',
  10935. ' TArrSet = array of TFlags;',
  10936. ' TArrJSValue = array of jsvalue;',
  10937. 'var',
  10938. ' ArrInt1, ArrInt2: tarrint;',
  10939. ' ArrRec1, ArrRec2: tarrrec;',
  10940. ' ArrFlag1, ArrFlag2: tarrflag;',
  10941. ' ArrSet1, ArrSet2: tarrset;',
  10942. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10943. 'begin',
  10944. ' arrint1:=concat(arrint2);',
  10945. ' arrint1:=concat(arrint2,arrint2);',
  10946. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10947. ' arrrec1:=concat(arrrec2);',
  10948. ' arrrec1:=concat(arrrec2,arrrec2);',
  10949. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10950. ' arrset1:=concat(arrset2);',
  10951. ' arrset1:=concat(arrset2,arrset2);',
  10952. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10953. ' arrjsvalue1:=concat(arrjsvalue2);',
  10954. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10955. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10956. ' arrint1:=concat([1],arrint2);',
  10957. ' arrflag1:=concat([big]);',
  10958. ' arrflag1:=concat([big],arrflag2);',
  10959. ' arrflag1:=concat(arrflag2,[small]);',
  10960. '']);
  10961. ConvertProgram;
  10962. CheckSource('TestArray_Concat',
  10963. LinesToStr([ // statements
  10964. 'this.TFlag = {',
  10965. ' "0": "big",',
  10966. ' big: 0,',
  10967. ' "1": "small",',
  10968. ' small: 1',
  10969. '};',
  10970. 'rtl.recNewT(this, "TRec", function () {',
  10971. ' this.i = 0;',
  10972. ' this.$eq = function (b) {',
  10973. ' return this.i === b.i;',
  10974. ' };',
  10975. ' this.$assign = function (s) {',
  10976. ' this.i = s.i;',
  10977. ' return this;',
  10978. ' };',
  10979. '});',
  10980. 'this.ArrInt1 = [];',
  10981. 'this.ArrInt2 = [];',
  10982. 'this.ArrRec1 = [];',
  10983. 'this.ArrRec2 = [];',
  10984. 'this.ArrFlag1 = [];',
  10985. 'this.ArrFlag2 = [];',
  10986. 'this.ArrSet1 = [];',
  10987. 'this.ArrSet2 = [];',
  10988. 'this.ArrJSValue1 = [];',
  10989. 'this.ArrJSValue2 = [];',
  10990. '']),
  10991. LinesToStr([ // $mod.$main
  10992. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  10993. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  10994. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  10995. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  10996. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  10997. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  10998. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  10999. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  11000. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  11001. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  11002. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  11003. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  11004. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  11005. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  11006. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11007. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11008. '']));
  11009. end;
  11010. procedure TTestModule.TestArray_Concat_Append;
  11011. begin
  11012. StartProgram(false);
  11013. Add([
  11014. 'type',
  11015. ' integer = longint;',
  11016. ' TFlag = (big,small);',
  11017. ' TFlags = set of TFlag;',
  11018. ' TRec = record',
  11019. ' i: integer;',
  11020. ' end;',
  11021. ' TArrInt = array of integer;',
  11022. ' TArrRec = array of TRec;',
  11023. ' TArrFlag = array of TFlag;',
  11024. ' TArrSet = array of TFlags;',
  11025. ' TArrJSValue = array of jsvalue;',
  11026. 'var',
  11027. ' ArrInt: tarrint;',
  11028. ' ArrRec: tarrrec;',
  11029. ' ArrFlag: tarrflag;',
  11030. ' ArrSet: tarrset;',
  11031. ' ArrJSValue: tarrjsvalue;',
  11032. ' r: TRec;',
  11033. ' f: TFlags;',
  11034. 'begin',
  11035. ' // append',
  11036. ' arrint:=concat(arrint);',
  11037. ' arrint:=concat(arrint,[2]);',
  11038. ' arrint:=concat(arrint,[3,4]);',
  11039. ' arrrec:=concat(arrrec);',
  11040. ' arrrec:=concat(arrrec,[r]);',
  11041. ' arrrec:=concat(arrrec,[r,r]);',
  11042. ' arrset:=concat(arrset);',
  11043. ' arrset:=concat(arrset,[f]);',
  11044. ' arrset:=concat(arrset,[f,f]);',
  11045. ' arrjsvalue:=concat(arrjsvalue);',
  11046. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11047. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11048. ' arrflag:=concat(arrflag);',
  11049. ' arrflag:=concat(arrflag,[small]);',
  11050. ' arrflag:=concat(arrflag,[small,big]);',
  11051. '']);
  11052. ConvertProgram;
  11053. CheckSource('TestArray_Concat_Append',
  11054. LinesToStr([ // statements
  11055. 'this.TFlag = {',
  11056. ' "0": "big",',
  11057. ' big: 0,',
  11058. ' "1": "small",',
  11059. ' small: 1',
  11060. '};',
  11061. 'rtl.recNewT(this, "TRec", function () {',
  11062. ' this.i = 0;',
  11063. ' this.$eq = function (b) {',
  11064. ' return this.i === b.i;',
  11065. ' };',
  11066. ' this.$assign = function (s) {',
  11067. ' this.i = s.i;',
  11068. ' return this;',
  11069. ' };',
  11070. '});',
  11071. 'this.ArrInt = [];',
  11072. 'this.ArrRec = [];',
  11073. 'this.ArrFlag = [];',
  11074. 'this.ArrSet = [];',
  11075. 'this.ArrJSValue = [];',
  11076. 'this.r = this.TRec.$new();',
  11077. 'this.f = {};',
  11078. '']),
  11079. LinesToStr([ // $mod.$main
  11080. '$mod.ArrInt = $mod.ArrInt;',
  11081. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11082. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11083. '$mod.ArrRec = $mod.ArrRec;',
  11084. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11085. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11086. '$mod.ArrSet = $mod.ArrSet;',
  11087. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11088. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11089. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11090. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11091. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11092. '$mod.ArrFlag = $mod.ArrFlag;',
  11093. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11094. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11095. '']));
  11096. end;
  11097. procedure TTestModule.TestArray_Concat_Append_Var;
  11098. begin
  11099. StartProgram(false);
  11100. Add([
  11101. 'type',
  11102. ' TArrInt = array of word;',
  11103. '',
  11104. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11105. 'begin',
  11106. ' a:=concat(a,[2]);',
  11107. ' b:=concat(b,[2]);',
  11108. 'end;',
  11109. 'var',
  11110. ' ArrInt: tarrint;',
  11111. 'begin',
  11112. ' Fly(ArrInt,ArrInt);',
  11113. '']);
  11114. ConvertProgram;
  11115. CheckSource('TestArray_Concat_Append_Var',
  11116. LinesToStr([ // statements
  11117. 'this.Fly = function (a, b) {',
  11118. ' a = rtl.arrayPushN(a, 2);',
  11119. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11120. '};',
  11121. 'this.ArrInt = [];',
  11122. '']),
  11123. LinesToStr([ // $mod.$main
  11124. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11125. ' p: $mod,',
  11126. ' get: function () {',
  11127. ' return this.p.ArrInt;',
  11128. ' },',
  11129. ' set: function (v) {',
  11130. ' this.p.ArrInt = v;',
  11131. ' }',
  11132. '});',
  11133. '']));
  11134. end;
  11135. procedure TTestModule.TestArray_Copy;
  11136. begin
  11137. StartProgram(false);
  11138. Add([
  11139. 'type',
  11140. ' integer = longint;',
  11141. ' TFlag = (big,small);',
  11142. ' TFlags = set of TFlag;',
  11143. ' TRec = record',
  11144. ' i: integer;',
  11145. ' end;',
  11146. ' TArrInt = array of integer;',
  11147. ' TArrRec = array of TRec;',
  11148. ' TArrSet = array of TFlags;',
  11149. ' TArrJSValue = array of jsvalue;',
  11150. 'var',
  11151. ' ArrInt: tarrint;',
  11152. ' ArrRec: tarrrec;',
  11153. ' ArrSet: tarrset;',
  11154. ' ArrJSValue: tarrjsvalue;',
  11155. 'begin',
  11156. ' arrint:=copy(arrint);',
  11157. ' arrint:=copy(arrint,2);',
  11158. ' arrint:=copy(arrint,3,4);',
  11159. ' arrint:=copy([1,1],1,2);',
  11160. ' arrrec:=copy(arrrec);',
  11161. ' arrrec:=copy(arrrec,5);',
  11162. ' arrrec:=copy(arrrec,6,7);',
  11163. ' arrset:=copy(arrset);',
  11164. ' arrset:=copy(arrset,8);',
  11165. ' arrset:=copy(arrset,9,10);',
  11166. ' arrjsvalue:=copy(arrjsvalue);',
  11167. ' arrjsvalue:=copy(arrjsvalue,11);',
  11168. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11169. ' ']);
  11170. ConvertProgram;
  11171. CheckSource('TestArray_Copy',
  11172. LinesToStr([ // statements
  11173. 'this.TFlag = {',
  11174. ' "0": "big",',
  11175. ' big: 0,',
  11176. ' "1": "small",',
  11177. ' small: 1',
  11178. '};',
  11179. 'rtl.recNewT(this, "TRec", function () {',
  11180. ' this.i = 0;',
  11181. ' this.$eq = function (b) {',
  11182. ' return this.i === b.i;',
  11183. ' };',
  11184. ' this.$assign = function (s) {',
  11185. ' this.i = s.i;',
  11186. ' return this;',
  11187. ' };',
  11188. '});',
  11189. 'this.ArrInt = [];',
  11190. 'this.ArrRec = [];',
  11191. 'this.ArrSet = [];',
  11192. 'this.ArrJSValue = [];',
  11193. '']),
  11194. LinesToStr([ // $mod.$main
  11195. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11196. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11197. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11198. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11199. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11200. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11201. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11202. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11203. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11204. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11205. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11206. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11207. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11208. '']));
  11209. end;
  11210. procedure TTestModule.TestArray_InsertDelete;
  11211. begin
  11212. StartProgram(false);
  11213. Add([
  11214. 'type',
  11215. ' integer = longint;',
  11216. ' TFlag = (big,small);',
  11217. ' TFlags = set of TFlag;',
  11218. ' TRec = record',
  11219. ' i: integer;',
  11220. ' end;',
  11221. ' TArrInt = array of integer;',
  11222. ' TArrRec = array of TRec;',
  11223. ' TArrSet = array of TFlags;',
  11224. ' TArrJSValue = array of jsvalue;',
  11225. ' TArrArrInt = array of TArrInt;',
  11226. 'var',
  11227. ' ArrInt: tarrint;',
  11228. ' ArrRec: tarrrec;',
  11229. ' ArrSet: tarrset;',
  11230. ' ArrJSValue: tarrjsvalue;',
  11231. ' ArrArrInt: TArrArrInt;',
  11232. 'begin',
  11233. ' Insert(1,arrint,2);',
  11234. ' Insert(arrint[3],arrint,4);',
  11235. ' Insert(arrrec[5],arrrec,6);',
  11236. ' Insert(arrset[7],arrset,7);',
  11237. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11238. ' Insert(10,arrjsvalue,11);',
  11239. ' Insert([23],arrarrint,22);',
  11240. ' Delete(arrint,12,13);',
  11241. ' Delete(arrrec,14,15);',
  11242. ' Delete(arrset,17,18);',
  11243. ' Delete(arrjsvalue,19,10);']);
  11244. ConvertProgram;
  11245. CheckSource('TestArray_InsertDelete',
  11246. LinesToStr([ // statements
  11247. 'this.TFlag = {',
  11248. ' "0": "big",',
  11249. ' big: 0,',
  11250. ' "1": "small",',
  11251. ' small: 1',
  11252. '};',
  11253. 'rtl.recNewT(this, "TRec", function () {',
  11254. ' this.i = 0;',
  11255. ' this.$eq = function (b) {',
  11256. ' return this.i === b.i;',
  11257. ' };',
  11258. ' this.$assign = function (s) {',
  11259. ' this.i = s.i;',
  11260. ' return this;',
  11261. ' };',
  11262. '});',
  11263. 'this.ArrInt = [];',
  11264. 'this.ArrRec = [];',
  11265. 'this.ArrSet = [];',
  11266. 'this.ArrJSValue = [];',
  11267. 'this.ArrArrInt = [];',
  11268. '']),
  11269. LinesToStr([ // $mod.$main
  11270. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11271. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11272. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11273. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11274. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11275. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11276. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11277. '$mod.ArrInt.splice(12, 13);',
  11278. '$mod.ArrRec.splice(14, 15);',
  11279. '$mod.ArrSet.splice(17, 18);',
  11280. '$mod.ArrJSValue.splice(19, 10);',
  11281. '']));
  11282. end;
  11283. procedure TTestModule.TestArray_Add_Append;
  11284. begin
  11285. StartProgram(false);
  11286. Add([
  11287. '{$modeswitch arrayoperators}',
  11288. 'type',
  11289. ' integer = longint;',
  11290. ' TFlag = (big,small);',
  11291. ' TFlags = set of TFlag;',
  11292. ' TRec = record',
  11293. ' i: integer;',
  11294. ' end;',
  11295. ' TArrInt = array of integer;',
  11296. ' TArrRec = array of TRec;',
  11297. ' TArrFlag = array of TFlag;',
  11298. ' TArrSet = array of TFlags;',
  11299. ' TArrJSValue = array of jsvalue;',
  11300. 'var',
  11301. ' ArrInt: tarrint;',
  11302. ' ArrRec: tarrrec;',
  11303. ' ArrFlag: tarrflag;',
  11304. ' ArrSet: tarrset;',
  11305. ' ArrJSValue: tarrjsvalue;',
  11306. ' r: TRec;',
  11307. ' f: TFlags;',
  11308. 'begin',
  11309. ' // append',
  11310. ' arrint:=arrint+[2];',
  11311. ' arrint:=arrint+[3,4];',
  11312. ' arrrec:=arrrec+[r];',
  11313. ' arrrec:=arrrec+[r,r];',
  11314. ' arrset:=arrset+[f];',
  11315. ' arrset:=arrset+[f,f];',
  11316. ' arrjsvalue:=arrjsvalue+[11];',
  11317. ' arrjsvalue:=arrjsvalue+[12,13];',
  11318. ' arrflag:=arrflag+[small];',
  11319. ' arrflag:=arrflag+[small,big];',
  11320. '']);
  11321. ConvertProgram;
  11322. CheckSource('TestArray_Add_Append',
  11323. LinesToStr([ // statements
  11324. 'this.TFlag = {',
  11325. ' "0": "big",',
  11326. ' big: 0,',
  11327. ' "1": "small",',
  11328. ' small: 1',
  11329. '};',
  11330. 'rtl.recNewT(this, "TRec", function () {',
  11331. ' this.i = 0;',
  11332. ' this.$eq = function (b) {',
  11333. ' return this.i === b.i;',
  11334. ' };',
  11335. ' this.$assign = function (s) {',
  11336. ' this.i = s.i;',
  11337. ' return this;',
  11338. ' };',
  11339. '});',
  11340. 'this.ArrInt = [];',
  11341. 'this.ArrRec = [];',
  11342. 'this.ArrFlag = [];',
  11343. 'this.ArrSet = [];',
  11344. 'this.ArrJSValue = [];',
  11345. 'this.r = this.TRec.$new();',
  11346. 'this.f = {};',
  11347. '']),
  11348. LinesToStr([ // $mod.$main
  11349. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11350. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11351. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11352. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11353. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11354. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11355. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11356. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11357. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11358. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11359. '']));
  11360. end;
  11361. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11362. begin
  11363. Parser.Options:=Parser.Options+[po_cassignments];
  11364. StartProgram(false);
  11365. Add([
  11366. '{$modeswitch arrayoperators}',
  11367. 'type',
  11368. ' integer = longint;',
  11369. ' TArrInt = array of integer;',
  11370. ' TArrStr = array of string;',
  11371. 'const',
  11372. ' Ints: TArrInt = (1,2,3);',
  11373. ' Aliases: TarrStr = (''foo'',''b'');',
  11374. ' OneInt: TArrInt = (7);',
  11375. ' OneStr: array of integer = (7);',
  11376. ' Chars: array of AnsiChar = ''aoc'';',
  11377. ' Names: array of string = (''a'',''foo'');',
  11378. ' NameCount = low(Names)+high(Names)+length(Names);',
  11379. 'var i: integer;',
  11380. 'begin',
  11381. ' Ints:=[];',
  11382. ' Ints:=[1,1];',
  11383. ' Ints:=[1]+[2];',
  11384. ' Ints:=[2];',
  11385. ' Ints:=[]+ints;',
  11386. ' Ints:=Ints+[];',
  11387. ' Ints:=Ints+OneInt;',
  11388. ' Ints:=Ints+[1,1];',
  11389. ' Ints:=[i,i]+Ints;',
  11390. ' Ints:=[1]+[i]+[3];',
  11391. '']);
  11392. ConvertProgram;
  11393. CheckSource('TestArray_DynArrayConstObjFPC',
  11394. LinesToStr([ // statements
  11395. 'this.Ints = [1, 2, 3];',
  11396. 'this.Aliases = ["foo", "b"];',
  11397. 'this.OneInt = [7];',
  11398. 'this.OneStr = [7];',
  11399. 'this.Chars = ["a", "o", "c"];',
  11400. 'this.Names = ["a", "foo"];',
  11401. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11402. 'this.i = 0;',
  11403. '']),
  11404. LinesToStr([ // $mod.$main
  11405. '$mod.Ints = [];',
  11406. '$mod.Ints = [1, 1];',
  11407. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11408. '$mod.Ints = [2];',
  11409. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11410. '$mod.Ints = $mod.Ints;',
  11411. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11412. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11413. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11414. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11415. '']));
  11416. end;
  11417. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11418. begin
  11419. StartProgram(false);
  11420. // Note: const c = [1,1]; defines a set!
  11421. Add([
  11422. '{$mode delphi}',
  11423. 'type',
  11424. ' integer = longint;',
  11425. ' TArrInt = array of integer;',
  11426. ' TArrStr = array of string;',
  11427. 'const',
  11428. ' Ints: TArrInt = [1,1,2];',
  11429. ' Aliases: TarrStr = [''foo'',''b''];',
  11430. ' OneInt: TArrInt = [7];',
  11431. ' OneStr: array of integer = [7]+[8];',
  11432. ' Chars: array of AnsiChar = ''aoc'';',
  11433. ' Names: array of string = [''a'',''a''];',
  11434. ' NameCount = low(Names)+high(Names)+length(Names);',
  11435. 'begin',
  11436. '']);
  11437. ConvertProgram;
  11438. CheckSource('TestArray_DynArrayConstDelphi',
  11439. LinesToStr([ // statements
  11440. 'this.Ints = [1, 1, 2];',
  11441. 'this.Aliases = ["foo", "b"];',
  11442. 'this.OneInt = [7];',
  11443. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11444. 'this.Chars = ["a", "o", "c"];',
  11445. 'this.Names = ["a", "a"];',
  11446. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11447. '']),
  11448. LinesToStr([ // $mod.$main
  11449. '']));
  11450. end;
  11451. procedure TTestModule.TestArray_ArrayLitAsParam;
  11452. begin
  11453. StartProgram(false);
  11454. Add([
  11455. '{$modeswitch arrayoperators}',
  11456. 'type',
  11457. ' integer = longint;',
  11458. ' TArrInt = array of integer;',
  11459. ' TArrSet = array of (red,green,blue);',
  11460. 'procedure DoOpenInt(const a: array of integer); forward;',
  11461. 'procedure DoInt(const a: TArrInt);',
  11462. 'begin',
  11463. ' DoInt(a+[1]);',
  11464. ' DoInt([1]+a);',
  11465. ' DoOpenInt(a);',
  11466. ' DoOpenInt(a+[1]);',
  11467. ' DoOpenInt([1]+a);',
  11468. 'end;',
  11469. 'procedure DoOpenInt(const a: array of integer);',
  11470. 'begin',
  11471. ' DoOpenInt(a+[1]);',
  11472. ' DoOpenInt([1]+a);',
  11473. ' DoInt(a);',
  11474. ' DoInt(a+[1]);',
  11475. ' DoInt([1]+a);',
  11476. 'end;',
  11477. 'procedure DoSet(const a: TArrSet);',
  11478. 'begin',
  11479. ' DoSet(a+[red]);',
  11480. ' DoSet([blue]+a);',
  11481. 'end;',
  11482. 'var',
  11483. ' i: TArrInt;',
  11484. ' s: TArrSet;',
  11485. 'begin',
  11486. ' DoInt([1]);',
  11487. ' DoInt([1]+[2]);',
  11488. ' DoInt(i+[1]);',
  11489. ' DoInt([1]+i);',
  11490. ' DoOpenInt([1]);',
  11491. ' DoOpenInt([1]+[2]);',
  11492. ' DoOpenInt(i+[1]);',
  11493. ' DoOpenInt([1]+i);',
  11494. ' DoSet([red]);',
  11495. ' DoSet([blue]+[green]);',
  11496. ' DoSet(s+[blue]);',
  11497. ' DoSet([red]+s);',
  11498. '']);
  11499. ConvertProgram;
  11500. CheckSource('TestArray_ArrayLitAsParam',
  11501. LinesToStr([ // statements
  11502. 'this.TArrSet$a = {',
  11503. ' "0": "red",',
  11504. ' red: 0,',
  11505. ' "1": "green",',
  11506. ' green: 1,',
  11507. ' "2": "blue",',
  11508. ' blue: 2',
  11509. '};',
  11510. 'this.DoInt = function (a) {',
  11511. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11512. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11513. ' $mod.DoOpenInt(a);',
  11514. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11515. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11516. '};',
  11517. 'this.DoOpenInt = function (a) {',
  11518. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11519. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11520. ' $mod.DoInt(a);',
  11521. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11522. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11523. '};',
  11524. 'this.DoSet = function (a) {',
  11525. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11526. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11527. '};',
  11528. 'this.i = [];',
  11529. 'this.s = [];',
  11530. '']),
  11531. LinesToStr([ // $mod.$main
  11532. '$mod.DoInt([1]);',
  11533. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11534. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11535. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11536. '$mod.DoOpenInt([1]);',
  11537. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11538. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11539. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11540. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11541. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11542. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11543. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11544. '']));
  11545. end;
  11546. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11547. begin
  11548. StartProgram(false);
  11549. Add([
  11550. '{$modeswitch arrayoperators}',
  11551. 'type',
  11552. ' integer = longint;',
  11553. ' TArrInt = array of integer;',
  11554. ' TArrArrInt = array of TArrInt;',
  11555. 'procedure DoInt(const a: TArrArrInt);',
  11556. 'begin',
  11557. ' DoInt(a+[[1]]);',
  11558. ' DoInt([[1]]+a);',
  11559. ' DoInt(a);',
  11560. 'end;',
  11561. 'var',
  11562. ' i: TArrInt;',
  11563. ' a: TArrArrInt;',
  11564. 'begin',
  11565. ' a:=[[1]];',
  11566. ' a:=[i];',
  11567. ' a:=a+[i];',
  11568. ' a:=[i]+a;',
  11569. ' a:=[[1]+i];',
  11570. ' a:=[[1]+[2]];',
  11571. ' a:=[i+[2]];',
  11572. ' DoInt([[1]]);',
  11573. ' DoInt([[1]+[2],[3,4],[5]]);',
  11574. ' DoInt([i+[1]]+a);',
  11575. ' DoInt([i]+a);',
  11576. '']);
  11577. ConvertProgram;
  11578. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11579. LinesToStr([ // statements
  11580. 'this.DoInt = function (a) {',
  11581. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11582. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11583. ' $mod.DoInt(a);',
  11584. '};',
  11585. 'this.i = [];',
  11586. 'this.a = [];',
  11587. '']),
  11588. LinesToStr([ // $mod.$main
  11589. '$mod.a = [[1]];',
  11590. '$mod.a = [$mod.i];',
  11591. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11592. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11593. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11594. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11595. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11596. '$mod.DoInt([[1]]);',
  11597. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11598. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11599. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11600. '']));
  11601. end;
  11602. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11603. begin
  11604. StartProgram(false);
  11605. Add([
  11606. '{$modeswitch arrayoperators}',
  11607. 'type',
  11608. ' integer = longint;',
  11609. ' TArrInt = array[1..2] of integer;',
  11610. ' TArrArrInt = array of TArrInt;',
  11611. 'procedure DoInt(const a: TArrArrInt);',
  11612. 'begin',
  11613. ' DoInt(a+[[1,2]]);',
  11614. ' DoInt([[1,2]]+a);',
  11615. ' DoInt(a);',
  11616. 'end;',
  11617. 'var',
  11618. ' i: TArrInt;',
  11619. ' a: TArrArrInt;',
  11620. 'begin',
  11621. ' a:=[[1,1]];',
  11622. ' a:=[i];',
  11623. ' a:=a+[i];',
  11624. ' a:=[i]+a;',
  11625. ' DoInt([[1,1]]);',
  11626. ' DoInt([[1,2],[3,4]]);',
  11627. '']);
  11628. ConvertProgram;
  11629. CheckSource('TestArray_ArrayLitStaticAsParam',
  11630. LinesToStr([ // statements
  11631. 'this.DoInt = function (a) {',
  11632. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11633. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11634. ' $mod.DoInt(a);',
  11635. '};',
  11636. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11637. 'this.a = [];',
  11638. '']),
  11639. LinesToStr([ // $mod.$main
  11640. '$mod.a = [[1, 1]];',
  11641. '$mod.a = [$mod.i.slice(0)];',
  11642. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11643. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11644. '$mod.DoInt([[1, 1]]);',
  11645. '$mod.DoInt([[1, 2], [3, 4]]);',
  11646. '']));
  11647. end;
  11648. procedure TTestModule.TestArray_ForInArrOfString;
  11649. begin
  11650. StartProgram(false);
  11651. Add([
  11652. 'type',
  11653. 'type',
  11654. ' TMonthNameArray = array [1..12] of string;',
  11655. ' TMonthNames = TMonthNameArray;',
  11656. ' TObject = class',
  11657. ' private',
  11658. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11659. ' public',
  11660. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11661. ' end;',
  11662. 'var',
  11663. ' f: TObject;',
  11664. ' Month: string;',
  11665. ' Names: array of string = (''a'',''foo'',''bar'');',
  11666. ' i: longint;',
  11667. 'begin',
  11668. ' for Month in f.LongMonthNames do ;',
  11669. ' for Month in Names do ;',
  11670. ' for i:=low(Names) to high(Names) do ;',
  11671. '']);
  11672. ConvertProgram;
  11673. CheckSource('TestArray_ForInArrOfString',
  11674. LinesToStr([ // statements
  11675. 'rtl.createClass(this, "TObject", null, function () {',
  11676. ' this.$init = function () {',
  11677. ' };',
  11678. ' this.$final = function () {',
  11679. ' };',
  11680. '});',
  11681. 'this.f = null;',
  11682. 'this.Month = "";',
  11683. 'this.Names = ["a", "foo", "bar"];',
  11684. 'this.i = 0;',
  11685. '']),
  11686. LinesToStr([ // $mod.$main
  11687. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11688. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11689. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11690. '']));
  11691. end;
  11692. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11693. begin
  11694. StartProgram(false);
  11695. Add([
  11696. '{$modeswitch externalclass}',
  11697. 'type',
  11698. ' TJSObject = class external name ''Object''',
  11699. ' end;',
  11700. ' TJSArray = class external name ''Array''',
  11701. ' class function isArray(Value: JSValue) : boolean;',
  11702. ' function concat() : TJSArray; varargs;',
  11703. ' end;',
  11704. 'var',
  11705. ' aObj: TJSArray;',
  11706. ' a: array of longint;',
  11707. ' o: TJSObject;',
  11708. 'begin',
  11709. ' if TJSArray.isArray(65) then ;',
  11710. ' aObj:=TJSArray(a).concat(a);',
  11711. ' o:=TJSObject(a);',
  11712. ' aObj:=TJSArray([''bird'',''ant'']);',
  11713. '']);
  11714. ConvertProgram;
  11715. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11716. LinesToStr([ // statements
  11717. 'this.aObj = null;',
  11718. 'this.a = [];',
  11719. 'this.o = null;',
  11720. '']),
  11721. LinesToStr([ // $mod.$main
  11722. 'if (Array.isArray(65)) ;',
  11723. '$mod.aObj = $mod.a.concat($mod.a);',
  11724. '$mod.o = $mod.a;',
  11725. '$mod.aObj = ["bird", "ant"];',
  11726. '']));
  11727. end;
  11728. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11729. begin
  11730. StartProgram(false);
  11731. Add([
  11732. '{$modeswitch externalclass}',
  11733. 'type',
  11734. ' TArrStr = array of string;',
  11735. ' TJSArray = class external name ''Array''',
  11736. ' end;',
  11737. ' TJSObject = class external name ''Object''',
  11738. ' end;',
  11739. 'var',
  11740. ' aObj: TJSArray;',
  11741. ' a: TArrStr;',
  11742. ' jo: TJSObject;',
  11743. 'begin',
  11744. ' a:=TArrStr(aObj);',
  11745. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11746. ' a:=TarrStr(jo);',
  11747. '']);
  11748. ConvertProgram;
  11749. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11750. LinesToStr([ // statements
  11751. 'this.aObj = null;',
  11752. 'this.a = [];',
  11753. 'this.jo = null;',
  11754. '']),
  11755. LinesToStr([ // $mod.$main
  11756. '$mod.a = $mod.aObj;',
  11757. '$mod.aObj[1] = $mod.aObj[2];',
  11758. '$mod.a = $mod.jo;',
  11759. '']));
  11760. end;
  11761. procedure TTestModule.TestArrayOfConst_TVarRec;
  11762. begin
  11763. StartProgram(true,[supTVarRec]);
  11764. Add([
  11765. 'procedure Say(args: array of const);',
  11766. 'var',
  11767. ' i: longint;',
  11768. ' v: TVarRec;',
  11769. 'begin',
  11770. ' for i:=low(args) to high(args) do begin',
  11771. ' v:=args[i];',
  11772. ' case v.vtype of',
  11773. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11774. ' end;',
  11775. ' end;',
  11776. ' for v in args do ;',
  11777. ' args:=nil;',
  11778. ' SetLength(args,2);',
  11779. 'end;',
  11780. 'begin']);
  11781. ConvertProgram;
  11782. CheckSource('TestArrayOfConst_TVarRec',
  11783. LinesToStr([ // statements
  11784. 'this.Say = function (args) {',
  11785. ' var i = 0;',
  11786. ' var v = pas.system.TVarRec.$new();',
  11787. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11788. ' i = $l;',
  11789. ' v.$assign(args[i]);',
  11790. ' var $tmp = v.VType;',
  11791. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11792. ' };',
  11793. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11794. ' args = [];',
  11795. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11796. '};',
  11797. '']),
  11798. LinesToStr([ // $mod.$main
  11799. ]));
  11800. end;
  11801. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11802. begin
  11803. StartProgram(true,[supTVarRec]);
  11804. Add([
  11805. 'procedure Say(args: array of const);',
  11806. 'begin',
  11807. ' Say(args);',
  11808. 'end;',
  11809. 'var',
  11810. ' p: Pointer;',
  11811. ' j: jsvalue;',
  11812. ' c: currency;',
  11813. 'begin',
  11814. ' Say([]);',
  11815. ' Say([1]);',
  11816. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11817. '']);
  11818. ConvertProgram;
  11819. CheckSource('TestArrayOfConst_PassBaseTypes',
  11820. LinesToStr([ // statements
  11821. 'this.Say = function (args) {',
  11822. ' $mod.Say(args);',
  11823. '};',
  11824. 'this.p = null;',
  11825. 'this.j = undefined;',
  11826. 'this.c = 0;',
  11827. '']),
  11828. LinesToStr([ // $mod.$main
  11829. '$mod.Say([]);',
  11830. '$mod.Say(pas.system.VarRecs(0, 1));',
  11831. '$mod.Say(pas.system.VarRecs(',
  11832. ' 9,',
  11833. ' "c",',
  11834. ' 18,',
  11835. ' "foo",',
  11836. ' 5,',
  11837. ' null,',
  11838. ' 1,',
  11839. ' true,',
  11840. ' 3,',
  11841. ' 1.3,',
  11842. ' 5,',
  11843. ' $mod.p,',
  11844. ' 20,',
  11845. ' $mod.j,',
  11846. ' 12,',
  11847. ' $mod.c',
  11848. ' ));',
  11849. '']));
  11850. end;
  11851. procedure TTestModule.TestArrayOfConst_PassObj;
  11852. begin
  11853. StartProgram(true,[supTVarRec]);
  11854. Add([
  11855. '{$interfaces corba}',
  11856. 'type',
  11857. ' TObject = class',
  11858. ' end;',
  11859. ' TClass = class of TObject;',
  11860. ' IUnknown = interface',
  11861. ' end;',
  11862. 'procedure Say(args: array of const);',
  11863. 'begin',
  11864. 'end;',
  11865. 'var',
  11866. ' o: TObject;',
  11867. ' c: TClass;',
  11868. ' i: IUnknown;',
  11869. 'begin',
  11870. ' Say([o,c,TObject]);',
  11871. ' Say([nil,i]);',
  11872. '']);
  11873. ConvertProgram;
  11874. CheckSource('TestArrayOfConst_PassObj',
  11875. LinesToStr([ // statements
  11876. 'rtl.createClass(this, "TObject", null, function () {',
  11877. ' this.$init = function () {',
  11878. ' };',
  11879. ' this.$final = function () {',
  11880. ' };',
  11881. '});',
  11882. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11883. 'this.Say = function (args) {',
  11884. '};',
  11885. 'this.o = null;',
  11886. 'this.c = null;',
  11887. 'this.i = null;',
  11888. '']),
  11889. LinesToStr([ // $mod.$main
  11890. '$mod.Say(pas.system.VarRecs(',
  11891. ' 7,',
  11892. ' $mod.o,',
  11893. ' 8,',
  11894. ' $mod.c,',
  11895. ' 8,',
  11896. ' $mod.TObject',
  11897. '));',
  11898. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11899. '']));
  11900. end;
  11901. procedure TTestModule.TestRecord_Empty;
  11902. begin
  11903. StartProgram(false);
  11904. Add([
  11905. 'type',
  11906. ' TRecA = record',
  11907. ' end;',
  11908. 'var a,b: TRecA;',
  11909. 'begin',
  11910. ' if a=b then ;']);
  11911. ConvertProgram;
  11912. CheckSource('TestRecord_Empty',
  11913. LinesToStr([ // statements
  11914. 'rtl.recNewT(this, "TRecA", function () {',
  11915. ' this.$eq = function (b) {',
  11916. ' return true;',
  11917. ' };',
  11918. ' this.$assign = function (s) {',
  11919. ' return this;',
  11920. ' };',
  11921. '});',
  11922. 'this.a = this.TRecA.$new();',
  11923. 'this.b = this.TRecA.$new();',
  11924. '']),
  11925. LinesToStr([ // $mod.$main
  11926. 'if ($mod.a.$eq($mod.b)) ;'
  11927. ]));
  11928. end;
  11929. procedure TTestModule.TestRecord_Var;
  11930. begin
  11931. StartProgram(false);
  11932. Add('type');
  11933. Add(' TRecA = record');
  11934. Add(' Bold: longint;');
  11935. Add(' end;');
  11936. Add('var Rec: TRecA;');
  11937. Add('begin');
  11938. Add(' rec.bold:=123');
  11939. ConvertProgram;
  11940. CheckSource('TestRecord_Var',
  11941. LinesToStr([ // statements
  11942. 'rtl.recNewT(this, "TRecA", function () {',
  11943. ' this.Bold = 0;',
  11944. ' this.$eq = function (b) {',
  11945. ' return this.Bold === b.Bold;',
  11946. ' };',
  11947. ' this.$assign = function (s) {',
  11948. ' this.Bold = s.Bold;',
  11949. ' return this;',
  11950. ' };',
  11951. '});',
  11952. 'this.Rec = this.TRecA.$new();',
  11953. '']),
  11954. LinesToStr([ // $mod.$main
  11955. '$mod.Rec.Bold = 123;'
  11956. ]));
  11957. end;
  11958. procedure TTestModule.TestRecord_VarExternal;
  11959. begin
  11960. StartProgram(false);
  11961. Add([
  11962. '{$modeswitch externalclass}',
  11963. 'type',
  11964. ' TRecA = record',
  11965. ' i: byte;',
  11966. ' length_: longint external name ''length'';',
  11967. ' end;',
  11968. 'var Rec: TRecA;',
  11969. 'begin',
  11970. ' rec.length_ := rec.length_',
  11971. '']);
  11972. ConvertProgram;
  11973. CheckSource('TestRecord_VarExternal',
  11974. LinesToStr([ // statements
  11975. 'rtl.recNewT(this, "TRecA", function () {',
  11976. ' this.i = 0;',
  11977. ' this.$eq = function (b) {',
  11978. ' return (this.i === b.i) && (this.length === b.length);',
  11979. ' };',
  11980. ' this.$assign = function (s) {',
  11981. ' this.i = s.i;',
  11982. ' this.length = s.length;',
  11983. ' return this;',
  11984. ' };',
  11985. '});',
  11986. 'this.Rec = this.TRecA.$new();',
  11987. '']),
  11988. LinesToStr([ // $mod.$main
  11989. '$mod.Rec.length = $mod.Rec.length;'
  11990. ]));
  11991. end;
  11992. procedure TTestModule.TestRecord_WithDo;
  11993. begin
  11994. StartProgram(false);
  11995. Add('type');
  11996. Add(' TRec = record');
  11997. Add(' vI: longint;');
  11998. Add(' end;');
  11999. Add('var');
  12000. Add(' Int: longint;');
  12001. Add(' r: TRec;');
  12002. Add('begin');
  12003. Add(' with r do');
  12004. Add(' int:=vi;');
  12005. Add(' with r do begin');
  12006. Add(' int:=vi;');
  12007. Add(' vi:=int;');
  12008. Add(' end;');
  12009. ConvertProgram;
  12010. CheckSource('TestWithRecordDo',
  12011. LinesToStr([ // statements
  12012. 'rtl.recNewT(this, "TRec", function () {',
  12013. ' this.vI = 0;',
  12014. ' this.$eq = function (b) {',
  12015. ' return this.vI === b.vI;',
  12016. ' };',
  12017. ' this.$assign = function (s) {',
  12018. ' this.vI = s.vI;',
  12019. ' return this;',
  12020. ' };',
  12021. '});',
  12022. 'this.Int = 0;',
  12023. 'this.r = this.TRec.$new();',
  12024. '']),
  12025. LinesToStr([ // $mod.$main
  12026. 'var $with = $mod.r;',
  12027. '$mod.Int = $with.vI;',
  12028. 'var $with1 = $mod.r;',
  12029. '$mod.Int = $with1.vI;',
  12030. '$with1.vI = $mod.Int;'
  12031. ]));
  12032. end;
  12033. procedure TTestModule.TestRecord_Assign;
  12034. begin
  12035. StartProgram(false);
  12036. Add([
  12037. 'type',
  12038. ' TEnum = (red,green);',
  12039. ' TEnums = set of TEnum;',
  12040. ' TSmallRec = record',
  12041. ' N: longint;',
  12042. ' end;',
  12043. ' TBigRec = record',
  12044. ' Int: longint;',
  12045. ' D: double;',
  12046. ' Arr: array of longint;',
  12047. ' Arr2: array[1..2] of longint;',
  12048. ' Small: TSmallRec;',
  12049. ' Enums: TEnums;',
  12050. ' end;',
  12051. 'var',
  12052. ' r, s: TBigRec;',
  12053. 'begin',
  12054. ' r:=s;',
  12055. ' r:=default(TBigRec);',
  12056. ' r:=default(s);',
  12057. '']);
  12058. ConvertProgram;
  12059. CheckSource('TestRecord_Assign',
  12060. LinesToStr([ // statements
  12061. 'this.TEnum = {',
  12062. ' "0": "red",',
  12063. ' red: 0,',
  12064. ' "1": "green",',
  12065. ' green: 1',
  12066. '};',
  12067. 'rtl.recNewT(this, "TSmallRec", function () {',
  12068. ' this.N = 0;',
  12069. ' this.$eq = function (b) {',
  12070. ' return this.N === b.N;',
  12071. ' };',
  12072. ' this.$assign = function (s) {',
  12073. ' this.N = s.N;',
  12074. ' return this;',
  12075. ' };',
  12076. '});',
  12077. 'rtl.recNewT(this, "TBigRec", function () {',
  12078. ' this.Int = 0;',
  12079. ' this.D = 0.0;',
  12080. ' this.$new = function () {',
  12081. ' var r = Object.create(this);',
  12082. ' r.Arr = [];',
  12083. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12084. ' r.Small = $mod.TSmallRec.$new();',
  12085. ' r.Enums = {};',
  12086. ' return r;',
  12087. ' };',
  12088. ' this.$eq = function (b) {',
  12089. ' 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);',
  12090. ' };',
  12091. ' this.$assign = function (s) {',
  12092. ' this.Int = s.Int;',
  12093. ' this.D = s.D;',
  12094. ' this.Arr = rtl.arrayRef(s.Arr);',
  12095. ' this.Arr2 = s.Arr2.slice(0);',
  12096. ' this.Small.$assign(s.Small);',
  12097. ' this.Enums = rtl.refSet(s.Enums);',
  12098. ' return this;',
  12099. ' };',
  12100. '});',
  12101. 'this.r = this.TBigRec.$new();',
  12102. 'this.s = this.TBigRec.$new();',
  12103. '']),
  12104. LinesToStr([ // $mod.$main
  12105. '$mod.r.$assign($mod.s);',
  12106. '$mod.r.$assign($mod.TBigRec.$new());',
  12107. '$mod.r.$assign($mod.TBigRec.$new());',
  12108. '']));
  12109. end;
  12110. procedure TTestModule.TestRecord_AsParams;
  12111. begin
  12112. StartProgram(false);
  12113. Add([
  12114. 'type',
  12115. ' integer = longint;',
  12116. ' TRecord = record',
  12117. ' i: integer;',
  12118. ' end;',
  12119. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12120. 'var vL: TRecord;',
  12121. 'begin',
  12122. ' vd:=vd;',
  12123. ' vd.i:=vd.i;',
  12124. ' vl:=vc;',
  12125. ' vv:=vv;',
  12126. ' vv.i:=vv.i;',
  12127. ' U:=vl;',
  12128. ' U:=vd;',
  12129. ' U:=vc;',
  12130. ' U:=vv;',
  12131. ' vl:=TRecord(U);',
  12132. ' vd:=TRecord(U);',
  12133. ' vv:=TRecord(U);',
  12134. ' doit(vd,vd,vd,vd);',
  12135. ' doit(vc,vc,vl,vl);',
  12136. ' doit(vv,vv,vv,vv);',
  12137. ' doit(vl,vl,vl,vl);',
  12138. ' TRecord(U).i:=3;',
  12139. 'end;',
  12140. 'var i: TRecord;',
  12141. 'begin',
  12142. ' doit(i,i,i,i);',
  12143. '']);
  12144. ConvertProgram;
  12145. CheckSource('TestRecord_AsParams',
  12146. LinesToStr([ // statements
  12147. 'rtl.recNewT(this, "TRecord", function () {',
  12148. ' this.i = 0;',
  12149. ' this.$eq = function (b) {',
  12150. ' return this.i === b.i;',
  12151. ' };',
  12152. ' this.$assign = function (s) {',
  12153. ' this.i = s.i;',
  12154. ' return this;',
  12155. ' };',
  12156. '});',
  12157. 'this.DoIt = function (vD, vC, vV, U) {',
  12158. ' var vL = $mod.TRecord.$new();',
  12159. ' vD.$assign(vD);',
  12160. ' vD.i = vD.i;',
  12161. ' vL.$assign(vC);',
  12162. ' vV.$assign(vV);',
  12163. ' vV.i = vV.i;',
  12164. ' U.$assign(vL);',
  12165. ' U.$assign(vD);',
  12166. ' U.$assign(vC);',
  12167. ' U.$assign(vV);',
  12168. ' vL.$assign(U);',
  12169. ' vD.$assign(U);',
  12170. ' vV.$assign(U);',
  12171. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12172. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12173. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12174. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12175. ' U.i = 3;',
  12176. '};',
  12177. 'this.i = this.TRecord.$new();'
  12178. ]),
  12179. LinesToStr([
  12180. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12181. '']));
  12182. end;
  12183. procedure TTestModule.TestRecord_ConstRef;
  12184. begin
  12185. StartProgram(false);
  12186. Add([
  12187. 'type TRec = record i: word; end;',
  12188. 'procedure Run(constref a: TRec);',
  12189. 'begin',
  12190. 'end;',
  12191. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12192. 'var l: TRec;',
  12193. 'begin',
  12194. ' Run(l);',
  12195. ' Run(a);',
  12196. ' Run(b);',
  12197. ' Run(c);',
  12198. ' Run(d);',
  12199. ' Run(e);',
  12200. 'end;',
  12201. 'begin',
  12202. '']);
  12203. ConvertProgram;
  12204. CheckResolverUnexpectedHints();
  12205. CheckSource('TestRecord_ConstRef',
  12206. LinesToStr([ // statements
  12207. 'rtl.recNewT(this, "TRec", function () {',
  12208. ' this.i = 0;',
  12209. ' this.$eq = function (b) {',
  12210. ' return this.i === b.i;',
  12211. ' };',
  12212. ' this.$assign = function (s) {',
  12213. ' this.i = s.i;',
  12214. ' return this;',
  12215. ' };',
  12216. '});',
  12217. 'this.Run = function (a) {',
  12218. '};',
  12219. 'this.Fly = function (a, b, c, d, e) {',
  12220. ' var l = $mod.TRec.$new();',
  12221. ' $mod.Run(l);',
  12222. ' $mod.Run(a);',
  12223. ' $mod.Run(b);',
  12224. ' $mod.Run(c);',
  12225. ' $mod.Run(d);',
  12226. ' $mod.Run(e);',
  12227. '};',
  12228. '']),
  12229. LinesToStr([
  12230. '']));
  12231. end;
  12232. procedure TTestModule.TestRecordElement_AsParams;
  12233. begin
  12234. StartProgram(false);
  12235. Add('type');
  12236. Add(' integer = longint;');
  12237. Add(' TRecord = record');
  12238. Add(' i: integer;');
  12239. Add(' end;');
  12240. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12241. Add('var vJ: TRecord;');
  12242. Add('begin');
  12243. Add(' doit(vj.i,vj.i,vj.i);');
  12244. Add('end;');
  12245. Add('var r: TRecord;');
  12246. Add('begin');
  12247. Add(' doit(r.i,r.i,r.i);');
  12248. ConvertProgram;
  12249. CheckSource('TestRecordElement_AsParams',
  12250. LinesToStr([ // statements
  12251. 'rtl.recNewT(this, "TRecord", function () {',
  12252. ' this.i = 0;',
  12253. ' this.$eq = function (b) {',
  12254. ' return this.i === b.i;',
  12255. ' };',
  12256. ' this.$assign = function (s) {',
  12257. ' this.i = s.i;',
  12258. ' return this;',
  12259. ' };',
  12260. '});',
  12261. 'this.DoIt = function (vG,vH,vI) {',
  12262. ' var vJ = $mod.TRecord.$new();',
  12263. ' $mod.DoIt(vJ.i, vJ.i, {',
  12264. ' p: vJ,',
  12265. ' get: function () {',
  12266. ' return this.p.i;',
  12267. ' },',
  12268. ' set: function (v) {',
  12269. ' this.p.i = v;',
  12270. ' }',
  12271. ' });',
  12272. '};',
  12273. 'this.r = this.TRecord.$new();'
  12274. ]),
  12275. LinesToStr([
  12276. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12277. ' p: $mod.r,',
  12278. ' get: function () {',
  12279. ' return this.p.i;',
  12280. ' },',
  12281. ' set: function (v) {',
  12282. ' this.p.i = v;',
  12283. ' }',
  12284. '});'
  12285. ]));
  12286. end;
  12287. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12288. begin
  12289. StartProgram(false);
  12290. Add('type');
  12291. Add(' integer = longint;');
  12292. Add(' TRecord = record');
  12293. Add(' i: integer;');
  12294. Add(' end;');
  12295. Add('function GetRec(vB: integer = 0): TRecord;');
  12296. Add('begin');
  12297. Add('end;');
  12298. Add('procedure DoIt(vG: integer; const vH: integer);');
  12299. Add('begin');
  12300. Add('end;');
  12301. Add('begin');
  12302. Add(' doit(getrec.i,getrec.i);');
  12303. Add(' doit(getrec().i,getrec().i);');
  12304. Add(' doit(getrec(1).i,getrec(2).i);');
  12305. ConvertProgram;
  12306. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12307. LinesToStr([ // statements
  12308. 'rtl.recNewT(this, "TRecord", function () {',
  12309. ' this.i = 0;',
  12310. ' this.$eq = function (b) {',
  12311. ' return this.i === b.i;',
  12312. ' };',
  12313. ' this.$assign = function (s) {',
  12314. ' this.i = s.i;',
  12315. ' return this;',
  12316. ' };',
  12317. '});',
  12318. 'this.GetRec = function (vB) {',
  12319. ' var Result = $mod.TRecord.$new();',
  12320. ' return Result;',
  12321. '};',
  12322. 'this.DoIt = function (vG, vH) {',
  12323. '};',
  12324. '']),
  12325. LinesToStr([
  12326. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12327. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12328. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12329. '']));
  12330. end;
  12331. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12332. begin
  12333. StartProgram(false);
  12334. Add('type');
  12335. Add(' integer = longint;');
  12336. Add(' TRecord = record');
  12337. Add(' i: integer;');
  12338. Add(' end;');
  12339. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12340. Add('begin');
  12341. Add('end;');
  12342. Add('var r: trecord;');
  12343. Add('begin');
  12344. Add(' with r do ');
  12345. Add(' doit(i,i,i);');
  12346. ConvertProgram;
  12347. CheckSource('TestRecordElementFromWith_AsParams',
  12348. LinesToStr([ // statements
  12349. 'rtl.recNewT(this, "TRecord", function () {',
  12350. ' this.i = 0;',
  12351. ' this.$eq = function (b) {',
  12352. ' return this.i === b.i;',
  12353. ' };',
  12354. ' this.$assign = function (s) {',
  12355. ' this.i = s.i;',
  12356. ' return this;',
  12357. ' };',
  12358. '});',
  12359. 'this.DoIt = function (vG,vH,vI) {',
  12360. '};',
  12361. 'this.r = this.TRecord.$new();'
  12362. ]),
  12363. LinesToStr([
  12364. 'var $with = $mod.r;',
  12365. '$mod.DoIt($with.i,$with.i,{',
  12366. ' p: $with,',
  12367. ' get: function () {',
  12368. ' return this.p.i;',
  12369. ' },',
  12370. ' set: function (v) {',
  12371. ' this.p.i = v;',
  12372. ' }',
  12373. '});',
  12374. '']));
  12375. end;
  12376. procedure TTestModule.TestRecord_Equal;
  12377. begin
  12378. StartProgram(false);
  12379. Add('type');
  12380. Add(' integer = longint;');
  12381. Add(' TFlag = (red,blue);');
  12382. Add(' TFlags = set of TFlag;');
  12383. Add(' TProc = procedure;');
  12384. Add(' TRecord = record');
  12385. Add(' i: integer;');
  12386. Add(' Event: TProc;');
  12387. Add(' f: TFlags;');
  12388. Add(' end;');
  12389. Add(' TNested = record');
  12390. Add(' r: TRecord;');
  12391. Add(' end;');
  12392. Add('var');
  12393. Add(' b: boolean;');
  12394. Add(' r,s: trecord;');
  12395. Add('begin');
  12396. Add(' b:=r=s;');
  12397. Add(' b:=r<>s;');
  12398. ConvertProgram;
  12399. CheckSource('TestRecord_Equal',
  12400. LinesToStr([ // statements
  12401. 'this.TFlag = {',
  12402. ' "0": "red",',
  12403. ' red: 0,',
  12404. ' "1": "blue",',
  12405. ' blue: 1',
  12406. '};',
  12407. 'rtl.recNewT(this, "TRecord", function () {',
  12408. ' this.i = 0;',
  12409. ' this.Event = null;',
  12410. ' this.$new = function () {',
  12411. ' var r = Object.create(this);',
  12412. ' r.f = {};',
  12413. ' return r;',
  12414. ' };',
  12415. ' this.$eq = function (b) {',
  12416. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12417. ' };',
  12418. ' this.$assign = function (s) {',
  12419. ' this.i = s.i;',
  12420. ' this.Event = s.Event;',
  12421. ' this.f = rtl.refSet(s.f);',
  12422. ' return this;',
  12423. ' };',
  12424. '});',
  12425. 'rtl.recNewT(this, "TNested", function () {',
  12426. ' this.$new = function () {',
  12427. ' var r = Object.create(this);',
  12428. ' r.r = $mod.TRecord.$new();',
  12429. ' return r;',
  12430. ' };',
  12431. ' this.$eq = function (b) {',
  12432. ' return this.r.$eq(b.r);',
  12433. ' };',
  12434. ' this.$assign = function (s) {',
  12435. ' this.r.$assign(s.r);',
  12436. ' return this;',
  12437. ' };',
  12438. '});',
  12439. 'this.b = false;',
  12440. 'this.r = this.TRecord.$new();',
  12441. 'this.s = this.TRecord.$new();',
  12442. '']),
  12443. LinesToStr([
  12444. '$mod.b = $mod.r.$eq($mod.s);',
  12445. '$mod.b = !$mod.r.$eq($mod.s);',
  12446. '']));
  12447. end;
  12448. procedure TTestModule.TestRecord_JSValue;
  12449. begin
  12450. StartProgram(false);
  12451. Add([
  12452. 'type',
  12453. ' TRecord = record',
  12454. ' i: longint;',
  12455. ' end;',
  12456. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12457. 'begin',
  12458. 'end;',
  12459. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12460. 'begin',
  12461. ' if jsvalue(d) then ;',
  12462. ' if jsvalue(c) then ;',
  12463. ' if jsvalue(v) then ;',
  12464. 'end;',
  12465. 'var',
  12466. ' Jv: jsvalue;',
  12467. ' Rec: trecord;',
  12468. 'begin',
  12469. ' rec:=trecord(jv);',
  12470. ' jv:=rec;',
  12471. ' Fly(rec,rec);',
  12472. ' Fly(@rec,@rec);',
  12473. ' if jsvalue(Rec) then ;',
  12474. ' Run(trecord(jv),trecord(jv),rec);',
  12475. '']);
  12476. ConvertProgram;
  12477. CheckSource('TestRecord_JSValue',
  12478. LinesToStr([ // statements
  12479. 'rtl.recNewT(this, "TRecord", function () {',
  12480. ' this.i = 0;',
  12481. ' this.$eq = function (b) {',
  12482. ' return this.i === b.i;',
  12483. ' };',
  12484. ' this.$assign = function (s) {',
  12485. ' this.i = s.i;',
  12486. ' return this;',
  12487. ' };',
  12488. '});',
  12489. 'this.Fly = function (d, c) {',
  12490. '};',
  12491. 'this.Run = function (d, c, v) {',
  12492. ' if (d) ;',
  12493. ' if (c) ;',
  12494. ' if (v) ;',
  12495. '};',
  12496. 'this.Jv = undefined;',
  12497. 'this.Rec = this.TRecord.$new();',
  12498. '']),
  12499. LinesToStr([
  12500. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12501. '$mod.Jv = $mod.Rec;',
  12502. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12503. '$mod.Fly($mod.Rec, $mod.Rec);',
  12504. 'if ($mod.Rec) ;',
  12505. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12506. '']));
  12507. end;
  12508. procedure TTestModule.TestRecord_VariantFail;
  12509. begin
  12510. StartProgram(false);
  12511. Add([
  12512. 'type',
  12513. ' TRec = record',
  12514. ' case word of',
  12515. ' 0: (b0, b1: Byte);',
  12516. ' 1: (i: word);',
  12517. ' end;',
  12518. 'begin']);
  12519. SetExpectedPasResolverError('Not supported: variant record',
  12520. nNotSupportedX);
  12521. ConvertProgram;
  12522. end;
  12523. procedure TTestModule.TestRecord_FieldArray;
  12524. begin
  12525. StartProgram(false);
  12526. Add([
  12527. 'type',
  12528. ' TArrInt = array[3..4] of longint;',
  12529. ' TArrArrInt = array[3..4] of longint;',
  12530. ' TRec = record',
  12531. ' a: array of longint;',
  12532. ' s: array[1..2] of longint;',
  12533. ' m: array[1..2,3..4] of longint;',
  12534. ' o: TArrArrInt;',
  12535. ' end;',
  12536. 'begin']);
  12537. ConvertProgram;
  12538. CheckSource('TestRecord_FieldArray',
  12539. LinesToStr([ // statements
  12540. 'rtl.recNewT(this, "TRec", function () {',
  12541. ' this.m$a$clone = function (a) {',
  12542. ' var b = [];',
  12543. ' b.length = 2;',
  12544. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12545. ' return b;',
  12546. ' };',
  12547. ' this.$new = function () {',
  12548. ' var r = Object.create(this);',
  12549. ' r.a = [];',
  12550. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12551. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12552. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12553. ' return r;',
  12554. ' };',
  12555. ' this.$eq = function (b) {',
  12556. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12557. ' };',
  12558. ' this.$assign = function (s) {',
  12559. ' this.a = rtl.arrayRef(s.a);',
  12560. ' this.s = s.s.slice(0);',
  12561. ' this.m = this.m$a$clone(s.m);',
  12562. ' this.o = s.o.slice(0);',
  12563. ' return this;',
  12564. ' };',
  12565. '});',
  12566. '']),
  12567. LinesToStr([ // $mod.$main
  12568. '']));
  12569. end;
  12570. procedure TTestModule.TestRecord_Const;
  12571. begin
  12572. StartProgram(false);
  12573. Add([
  12574. 'type',
  12575. ' TArrInt = array[3..4] of longint;',
  12576. ' TPoint = record x,y: longint; end;',
  12577. ' TRec = record',
  12578. ' i: longint;',
  12579. ' a: array of longint;',
  12580. ' s: array[1..2] of longint;',
  12581. ' m: array[1..2,3..4] of longint;',
  12582. ' p: TPoint;',
  12583. ' end;',
  12584. ' TPoints = array of TPoint;',
  12585. 'const',
  12586. ' r: TRec = (',
  12587. ' i:1;',
  12588. ' a:(2,3);',
  12589. ' s:(4,5);',
  12590. ' m:( (11,12), (13,14) );',
  12591. ' p: (x:21; y:22)',
  12592. ' );',
  12593. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12594. 'begin']);
  12595. ConvertProgram;
  12596. CheckSource('TestRecord_Const',
  12597. LinesToStr([ // statements
  12598. 'rtl.recNewT(this, "TPoint", function () {',
  12599. ' this.x = 0;',
  12600. ' this.y = 0;',
  12601. ' this.$eq = function (b) {',
  12602. ' return (this.x === b.x) && (this.y === b.y);',
  12603. ' };',
  12604. ' this.$assign = function (s) {',
  12605. ' this.x = s.x;',
  12606. ' this.y = s.y;',
  12607. ' return this;',
  12608. ' };',
  12609. '});',
  12610. 'rtl.recNewT(this, "TRec", function () {',
  12611. ' this.i = 0;',
  12612. ' this.m$a$clone = function (a) {',
  12613. ' var b = [];',
  12614. ' b.length = 2;',
  12615. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12616. ' return b;',
  12617. ' };',
  12618. ' this.$new = function () {',
  12619. ' var r = Object.create(this);',
  12620. ' r.a = [];',
  12621. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12622. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12623. ' r.p = $mod.TPoint.$new();',
  12624. ' return r;',
  12625. ' };',
  12626. ' this.$eq = function (b) {',
  12627. ' 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);',
  12628. ' };',
  12629. ' this.$assign = function (s) {',
  12630. ' this.i = s.i;',
  12631. ' this.a = rtl.arrayRef(s.a);',
  12632. ' this.s = s.s.slice(0);',
  12633. ' this.m = this.m$a$clone(s.m);',
  12634. ' this.p.$assign(s.p);',
  12635. ' return this;',
  12636. ' };',
  12637. '});',
  12638. 'this.r = this.TRec.$clone({',
  12639. ' i: 1,',
  12640. ' a: [2, 3],',
  12641. ' s: [4, 5],',
  12642. ' m: [[11, 12], [13, 14]],',
  12643. ' p: this.TPoint.$clone({',
  12644. ' x: 21,',
  12645. ' y: 22',
  12646. ' })',
  12647. '});',
  12648. 'this.p = [this.TPoint.$clone({',
  12649. ' x: 1,',
  12650. ' y: 2',
  12651. '}), this.TPoint.$clone({',
  12652. ' x: 3,',
  12653. ' y: 4',
  12654. '})];',
  12655. '']),
  12656. LinesToStr([ // $mod.$main
  12657. '']));
  12658. end;
  12659. procedure TTestModule.TestRecord_TypecastFail;
  12660. begin
  12661. StartProgram(false);
  12662. Add([
  12663. 'type',
  12664. ' TPoint = record x,y: longint; end;',
  12665. ' TRec = record l: longint end;',
  12666. 'var p: TPoint;',
  12667. 'begin',
  12668. ' if TRec(p).l=2 then ;']);
  12669. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12670. nIllegalTypeConversionTo);
  12671. ConvertProgram;
  12672. end;
  12673. procedure TTestModule.TestRecord_InFunction;
  12674. begin
  12675. StartProgram(false);
  12676. Add([
  12677. 'var TPoint: longint = 3;',
  12678. 'procedure DoIt;',
  12679. 'type',
  12680. ' TPoint = record x,y: longint; end;',
  12681. ' TPoints = array of TPoint;',
  12682. 'var',
  12683. ' r: TPoint;',
  12684. ' p: TPoints;',
  12685. 'begin',
  12686. ' SetLength(p,2);',
  12687. 'end;',
  12688. 'begin']);
  12689. ConvertProgram;
  12690. CheckSource('TestRecord_InFunction',
  12691. LinesToStr([ // statements
  12692. 'this.TPoint = 3;',
  12693. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12694. ' this.x = 0;',
  12695. ' this.y = 0;',
  12696. ' this.$eq = function (b) {',
  12697. ' return (this.x === b.x) && (this.y === b.y);',
  12698. ' };',
  12699. ' this.$assign = function (s) {',
  12700. ' this.x = s.x;',
  12701. ' this.y = s.y;',
  12702. ' return this;',
  12703. ' };',
  12704. '});',
  12705. 'this.DoIt = function () {',
  12706. ' var r = TPoint$1.$new();',
  12707. ' var p = [];',
  12708. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12709. '};',
  12710. '']),
  12711. LinesToStr([ // $mod.$main
  12712. '']));
  12713. end;
  12714. procedure TTestModule.TestRecordAnonym_Field;
  12715. begin
  12716. StartProgram(false);
  12717. Add(['',
  12718. 'var Rec: record',
  12719. ' Bold: longint;',
  12720. ' end;',
  12721. 'begin',
  12722. ' rec.bold:=123;',
  12723. ' rec.bold:=rec.bold+7;',
  12724. '']);
  12725. ConvertProgram;
  12726. CheckSource('TestRecordAnonym_Field',
  12727. LinesToStr([ // statements
  12728. 'rtl.recNewT(this, "Rec$a", function () {',
  12729. ' this.Bold = 0;',
  12730. ' this.$eq = function (b) {',
  12731. ' return this.Bold === b.Bold;',
  12732. ' };',
  12733. ' this.$assign = function (s) {',
  12734. ' this.Bold = s.Bold;',
  12735. ' return this;',
  12736. ' };',
  12737. '});',
  12738. 'this.Rec = this.Rec$a.$new();',
  12739. '']),
  12740. LinesToStr([ // $mod.$main
  12741. '$mod.Rec.Bold = 123;',
  12742. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12743. '']));
  12744. end;
  12745. procedure TTestModule.TestRecordAnonym_Assign;
  12746. begin
  12747. StartProgram(false);
  12748. Add(['',
  12749. 'var S,T: record',
  12750. ' Bold: longint;',
  12751. ' end;',
  12752. ' b: boolean;',
  12753. 'begin',
  12754. ' S:=T;',
  12755. ' b:=s=t;',
  12756. '']);
  12757. ConvertProgram;
  12758. CheckSource('TestRecordAnonym_Assign',
  12759. LinesToStr([ // statements
  12760. 'rtl.recNewT(this, "T$a", function () {',
  12761. ' this.Bold = 0;',
  12762. ' this.$eq = function (b) {',
  12763. ' return this.Bold === b.Bold;',
  12764. ' };',
  12765. ' this.$assign = function (s) {',
  12766. ' this.Bold = s.Bold;',
  12767. ' return this;',
  12768. ' };',
  12769. '});',
  12770. 'this.S = this.T$a.$new();',
  12771. 'this.T = this.T$a.$new();',
  12772. 'this.b = false;',
  12773. '']),
  12774. LinesToStr([ // $mod.$main
  12775. '$mod.S.$assign($mod.T);',
  12776. '$mod.b = $mod.S.$eq($mod.T);',
  12777. '']));
  12778. end;
  12779. procedure TTestModule.TestRecordAnonym_Nested;
  12780. begin
  12781. StartProgram(false);
  12782. Add(['',
  12783. 'var S,T: record',
  12784. ' Bold: longint;',
  12785. ' Sub: record',
  12786. ' Color: word;',
  12787. ' end;',
  12788. ' end;',
  12789. ' b: boolean;',
  12790. 'begin',
  12791. ' S:=T;',
  12792. ' S.Sub:=T.Sub;',
  12793. ' S.Sub.Color:=T.Sub.Color+3;',
  12794. ' b:=s=t;',
  12795. ' b:=s.Sub=t.Sub;',
  12796. '']);
  12797. ConvertProgram;
  12798. CheckSource('TestRecordAnonym_Nested',
  12799. LinesToStr([ // statements
  12800. 'rtl.recNewT(this, "T$a", function () {',
  12801. ' this.Bold = 0;',
  12802. ' rtl.recNewT(this, "Sub$a", function () {',
  12803. ' this.Color = 0;',
  12804. ' this.$eq = function (b) {',
  12805. ' return this.Color === b.Color;',
  12806. ' };',
  12807. ' this.$assign = function (s) {',
  12808. ' this.Color = s.Color;',
  12809. ' return this;',
  12810. ' };',
  12811. ' });',
  12812. ' this.$new = function () {',
  12813. ' var r = Object.create(this);',
  12814. ' r.Sub = this.Sub$a.$new();',
  12815. ' return r;',
  12816. ' };',
  12817. ' this.$eq = function (b) {',
  12818. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12819. ' };',
  12820. ' this.$assign = function (s) {',
  12821. ' this.Bold = s.Bold;',
  12822. ' this.Sub.$assign(s.Sub);',
  12823. ' return this;',
  12824. ' };',
  12825. '}, true);',
  12826. 'this.S = this.T$a.$new();',
  12827. 'this.T = this.T$a.$new();',
  12828. 'this.b = false;',
  12829. '']),
  12830. LinesToStr([ // $mod.$main
  12831. '$mod.S.$assign($mod.T);',
  12832. '$mod.S.Sub.$assign($mod.T.Sub);',
  12833. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12834. '$mod.b = $mod.S.$eq($mod.T);',
  12835. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12836. '']));
  12837. end;
  12838. procedure TTestModule.TestRecordAnonym_Const;
  12839. begin
  12840. StartProgram(false);
  12841. Add(['',
  12842. 'var T: record',
  12843. ' Bold: longint;',
  12844. ' Sub: record',
  12845. ' Color: word;',
  12846. ' end;',
  12847. ' end = (Bold: 2; Sub: (Color: 3));',
  12848. 'begin',
  12849. '']);
  12850. ConvertProgram;
  12851. CheckSource('TestRecordAnonym_Const',
  12852. LinesToStr([ // statements
  12853. 'rtl.recNewT(this, "T$a", function () {',
  12854. ' this.Bold = 0;',
  12855. ' rtl.recNewT(this, "Sub$a", function () {',
  12856. ' this.Color = 0;',
  12857. ' this.$eq = function (b) {',
  12858. ' return this.Color === b.Color;',
  12859. ' };',
  12860. ' this.$assign = function (s) {',
  12861. ' this.Color = s.Color;',
  12862. ' return this;',
  12863. ' };',
  12864. ' });',
  12865. ' this.$new = function () {',
  12866. ' var r = Object.create(this);',
  12867. ' r.Sub = this.Sub$a.$new();',
  12868. ' return r;',
  12869. ' };',
  12870. ' this.$eq = function (b) {',
  12871. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12872. ' };',
  12873. ' this.$assign = function (s) {',
  12874. ' this.Bold = s.Bold;',
  12875. ' this.Sub.$assign(s.Sub);',
  12876. ' return this;',
  12877. ' };',
  12878. '}, true);',
  12879. 'this.T = this.T$a.$clone({',
  12880. ' Bold: 2,',
  12881. ' Sub: this.T$a.Sub$a.$clone({',
  12882. ' Color: 3',
  12883. ' })',
  12884. '});',
  12885. '']),
  12886. LinesToStr([ // $mod.$main
  12887. '']));
  12888. end;
  12889. procedure TTestModule.TestRecordAnonym_InFunction;
  12890. begin
  12891. StartProgram(false);
  12892. Add(['',
  12893. 'procedure Fly;',
  12894. 'var T: record',
  12895. ' Bold: longint;',
  12896. ' Sub: record',
  12897. ' Color: word;',
  12898. ' end;',
  12899. ' end = (Bold: 2; Sub: (Color: 3));',
  12900. 'begin',
  12901. 'end;',
  12902. 'begin',
  12903. '']);
  12904. ConvertProgram;
  12905. CheckSource('TestRecordAnonym_InFunction',
  12906. LinesToStr([ // statements
  12907. 'var T$a = rtl.recNewT(null, "", function () {',
  12908. ' this.Bold = 0;',
  12909. ' rtl.recNewT(this, "Sub$a", function () {',
  12910. ' this.Color = 0;',
  12911. ' this.$eq = function (b) {',
  12912. ' return this.Color === b.Color;',
  12913. ' };',
  12914. ' this.$assign = function (s) {',
  12915. ' this.Color = s.Color;',
  12916. ' return this;',
  12917. ' };',
  12918. ' });',
  12919. ' this.$new = function () {',
  12920. ' var r = Object.create(this);',
  12921. ' r.Sub = this.Sub$a.$new();',
  12922. ' return r;',
  12923. ' };',
  12924. ' this.$eq = function (b) {',
  12925. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12926. ' };',
  12927. ' this.$assign = function (s) {',
  12928. ' this.Bold = s.Bold;',
  12929. ' this.Sub.$assign(s.Sub);',
  12930. ' return this;',
  12931. ' };',
  12932. '}, true);',
  12933. 'this.Fly = function () {',
  12934. ' var T = T$a.$clone({',
  12935. ' Bold: 2,',
  12936. ' Sub: T$a.Sub$a.$clone({',
  12937. ' Color: 3',
  12938. ' })',
  12939. ' });',
  12940. '};',
  12941. '']),
  12942. LinesToStr([ // $mod.$main
  12943. '']));
  12944. end;
  12945. procedure TTestModule.TestAdvRecord_Function;
  12946. begin
  12947. StartProgram(false);
  12948. Parser.Options:=Parser.Options+[po_cassignments];
  12949. Add([
  12950. '{$modeswitch AdvancedRecords}',
  12951. 'type',
  12952. ' TPoint = record',
  12953. ' x,y: word;',
  12954. ' function Add(const apt: TPoint): TPoint;',
  12955. ' end;',
  12956. 'function TPoint.Add(const apt: TPoint): TPoint;',
  12957. 'begin',
  12958. ' Result:=Self;',
  12959. ' Result.x+=apt.x;',
  12960. ' Result.y:=Result.y+apt.y;',
  12961. ' Self:=apt;',
  12962. 'end;',
  12963. 'var p,q: TPoint;',
  12964. 'begin',
  12965. ' p.add(q);',
  12966. ' p:=default(TPoint);',
  12967. ' p:=q;',
  12968. '']);
  12969. ConvertProgram;
  12970. CheckSource('TestAdvRecord_Function',
  12971. LinesToStr([ // statements
  12972. 'rtl.recNewT(this, "TPoint", function () {',
  12973. ' this.x = 0;',
  12974. ' this.y = 0;',
  12975. ' this.$eq = function (b) {',
  12976. ' return (this.x === b.x) && (this.y === b.y);',
  12977. ' };',
  12978. ' this.$assign = function (s) {',
  12979. ' this.x = s.x;',
  12980. ' this.y = s.y;',
  12981. ' return this;',
  12982. ' };',
  12983. ' this.Add = function (apt) {',
  12984. ' var Result = $mod.TPoint.$new();',
  12985. ' Result.$assign(this);',
  12986. ' Result.x += apt.x;',
  12987. ' Result.y = Result.y + apt.y;',
  12988. ' this.$assign(apt);',
  12989. ' return Result;',
  12990. ' };',
  12991. '});',
  12992. 'this.p = this.TPoint.$new();',
  12993. 'this.q = this.TPoint.$new();',
  12994. '']),
  12995. LinesToStr([ // $mod.$main
  12996. '$mod.p.Add($mod.q);',
  12997. '$mod.p.$assign($mod.TPoint.$new());',
  12998. '$mod.p.$assign($mod.q);',
  12999. '']));
  13000. end;
  13001. procedure TTestModule.TestAdvRecord_Property;
  13002. begin
  13003. StartProgram(false);
  13004. Add([
  13005. '{$modeswitch AdvancedRecords}',
  13006. 'type',
  13007. ' TPoint = record',
  13008. ' x,y: word;',
  13009. ' strict private',
  13010. ' function GetSize: longword;',
  13011. ' procedure SetSize(Value: longword);',
  13012. ' public',
  13013. ' property Size: longword read GetSize write SetSize;',
  13014. ' property Left: word read x write y;',
  13015. ' end;',
  13016. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13017. 'function TPoint.GetSize: longword;',
  13018. 'begin',
  13019. ' x:=y;',
  13020. ' Size:=Size;',
  13021. ' Left:=Left;',
  13022. 'end;',
  13023. 'procedure TPoint.SetSize(Value: longword);',
  13024. 'begin',
  13025. 'end;',
  13026. 'var p,q: TPoint;',
  13027. 'begin',
  13028. ' p.Size:=q.Size;',
  13029. ' p.Left:=q.Left;',
  13030. '']);
  13031. ConvertProgram;
  13032. CheckSource('TestAdvRecord_Property',
  13033. LinesToStr([ // statements
  13034. 'rtl.recNewT(this, "TPoint", function () {',
  13035. ' this.x = 0;',
  13036. ' this.y = 0;',
  13037. ' this.$eq = function (b) {',
  13038. ' return (this.x === b.x) && (this.y === b.y);',
  13039. ' };',
  13040. ' this.$assign = function (s) {',
  13041. ' this.x = s.x;',
  13042. ' this.y = s.y;',
  13043. ' return this;',
  13044. ' };',
  13045. ' this.GetSize = function () {',
  13046. ' var Result = 0;',
  13047. ' this.x = this.y;',
  13048. ' this.SetSize(this.GetSize());',
  13049. ' this.y = this.x;',
  13050. ' return Result;',
  13051. ' };',
  13052. ' this.SetSize = function (Value) {',
  13053. ' };',
  13054. '});',
  13055. 'this.SetSize = function (Value) {',
  13056. '};',
  13057. 'this.p = this.TPoint.$new();',
  13058. 'this.q = this.TPoint.$new();',
  13059. '']),
  13060. LinesToStr([ // $mod.$main
  13061. '$mod.p.SetSize($mod.q.GetSize());',
  13062. '$mod.p.y = $mod.q.x;',
  13063. '']));
  13064. end;
  13065. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13066. begin
  13067. StartProgram(false);
  13068. Add([
  13069. '{$modeswitch AdvancedRecords}',
  13070. 'type',
  13071. ' TPoint = record',
  13072. ' strict private',
  13073. ' function GetItems(Index: word): word;',
  13074. ' procedure SetItems(Index: word; Value: word);',
  13075. ' public',
  13076. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13077. ' end;',
  13078. 'function TPoint.GetItems(Index: word): word;',
  13079. 'begin',
  13080. ' Items[index]:=Items[index];',
  13081. ' self.Items[index]:=self.Items[index];',
  13082. 'end;',
  13083. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13084. 'begin',
  13085. 'end;',
  13086. 'var p: TPoint;',
  13087. 'begin',
  13088. ' p[1]:=p[2];',
  13089. ' p.Items[3]:=p.Items[4];',
  13090. '']);
  13091. ConvertProgram;
  13092. CheckSource('TestAdvRecord_PropertyDefault',
  13093. LinesToStr([ // statements
  13094. 'rtl.recNewT(this, "TPoint", function () {',
  13095. ' this.$eq = function (b) {',
  13096. ' return true;',
  13097. ' };',
  13098. ' this.$assign = function (s) {',
  13099. ' return this;',
  13100. ' };',
  13101. ' this.GetItems = function (Index) {',
  13102. ' var Result = 0;',
  13103. ' this.SetItems(Index, this.GetItems(Index));',
  13104. ' this.SetItems(Index, this.GetItems(Index));',
  13105. ' return Result;',
  13106. ' };',
  13107. ' this.SetItems = function (Index, Value) {',
  13108. ' };',
  13109. '});',
  13110. 'this.p = this.TPoint.$new();',
  13111. '']),
  13112. LinesToStr([ // $mod.$main
  13113. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13114. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13115. '']));
  13116. end;
  13117. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13118. begin
  13119. StartProgram(false);
  13120. Add([
  13121. '{$modeswitch AdvancedRecords}',
  13122. 'type',
  13123. ' TRec = record',
  13124. ' class var',
  13125. ' Fx: longint;',
  13126. ' Fy: longint;',
  13127. ' class function GetInt: longint; static;',
  13128. ' class procedure SetInt(Value: longint); static;',
  13129. ' class procedure DoIt; static;',
  13130. ' class property IntA: longint read Fx write Fy;',
  13131. ' class property IntB: longint read GetInt write SetInt;',
  13132. ' end;',
  13133. 'class function trec.getint: longint;',
  13134. 'begin',
  13135. ' result:=fx;',
  13136. 'end;',
  13137. 'class procedure trec.setint(value: longint);',
  13138. 'begin',
  13139. 'end;',
  13140. 'class procedure trec.doit;',
  13141. 'begin',
  13142. ' IntA:=IntA+1;',
  13143. ' IntB:=IntB+1;',
  13144. 'end;',
  13145. 'var r: trec;',
  13146. 'begin',
  13147. ' trec.inta:=trec.inta+1;',
  13148. ' if trec.intb=2 then;',
  13149. ' trec.intb:=trec.intb+2;',
  13150. ' trec.setint(trec.inta);',
  13151. ' r.inta:=r.inta+1;',
  13152. ' if r.intb=2 then;',
  13153. ' r.intb:=r.intb+2;',
  13154. ' r.setint(r.inta);']);
  13155. ConvertProgram;
  13156. CheckSource('TestAdvRecord_Property_ClassMethod',
  13157. LinesToStr([ // statements
  13158. 'rtl.recNewT(this, "TRec", function () {',
  13159. ' this.Fx = 0;',
  13160. ' this.Fy = 0;',
  13161. ' this.$eq = function (b) {',
  13162. ' return true;',
  13163. ' };',
  13164. ' this.$assign = function (s) {',
  13165. ' return this;',
  13166. ' };',
  13167. ' this.GetInt = function () {',
  13168. ' var Result = 0;',
  13169. ' Result = $mod.TRec.Fx;',
  13170. ' return Result;',
  13171. ' };',
  13172. ' this.SetInt = function (Value) {',
  13173. ' };',
  13174. ' this.DoIt = function () {',
  13175. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13176. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13177. ' };',
  13178. '}, true);',
  13179. 'this.r = this.TRec.$new();',
  13180. '']),
  13181. LinesToStr([ // $mod.$main
  13182. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13183. 'if ($mod.TRec.GetInt() === 2) ;',
  13184. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13185. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13186. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13187. 'if ($mod.TRec.GetInt() === 2) ;',
  13188. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13189. '$mod.TRec.SetInt($mod.r.Fx);',
  13190. '']));
  13191. end;
  13192. procedure TTestModule.TestAdvRecord_Const;
  13193. begin
  13194. StartProgram(false);
  13195. Add([
  13196. '{$modeswitch AdvancedRecords}',
  13197. 'type',
  13198. ' TArrInt = array[3..4] of longint;',
  13199. ' TPoint = record',
  13200. ' x,y: longint;',
  13201. ' class var Count: nativeint;',
  13202. ' end;',
  13203. ' TRec = record',
  13204. ' i: longint;',
  13205. ' a: array of longint;',
  13206. ' s: array[1..2] of longint;',
  13207. ' m: array[1..2,3..4] of longint;',
  13208. ' p: TPoint;',
  13209. ' end;',
  13210. ' TPoints = array of TPoint;',
  13211. 'const',
  13212. ' r: TRec = (',
  13213. ' i:1;',
  13214. ' a:(2,3);',
  13215. ' s:(4,5);',
  13216. ' m:( (11,12), (13,14) );',
  13217. ' p: (x:21)',
  13218. ' );',
  13219. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13220. 'begin']);
  13221. ConvertProgram;
  13222. CheckSource('TestAdvRecord_Const',
  13223. LinesToStr([ // statements
  13224. 'rtl.recNewT(this, "TPoint", function () {',
  13225. ' this.x = 0;',
  13226. ' this.y = 0;',
  13227. ' this.Count = 0;',
  13228. ' this.$eq = function (b) {',
  13229. ' return (this.x === b.x) && (this.y === b.y);',
  13230. ' };',
  13231. ' this.$assign = function (s) {',
  13232. ' this.x = s.x;',
  13233. ' this.y = s.y;',
  13234. ' return this;',
  13235. ' };',
  13236. '}, true);',
  13237. 'rtl.recNewT(this, "TRec", function () {',
  13238. ' this.i = 0;',
  13239. ' this.m$a$clone = function (a) {',
  13240. ' var b = [];',
  13241. ' b.length = 2;',
  13242. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13243. ' return b;',
  13244. ' };',
  13245. ' this.$new = function () {',
  13246. ' var r = Object.create(this);',
  13247. ' r.a = [];',
  13248. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13249. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13250. ' r.p = $mod.TPoint.$new();',
  13251. ' return r;',
  13252. ' };',
  13253. ' this.$eq = function (b) {',
  13254. ' 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);',
  13255. ' };',
  13256. ' this.$assign = function (s) {',
  13257. ' this.i = s.i;',
  13258. ' this.a = rtl.arrayRef(s.a);',
  13259. ' this.s = s.s.slice(0);',
  13260. ' this.m = this.m$a$clone(s.m);',
  13261. ' this.p.$assign(s.p);',
  13262. ' return this;',
  13263. ' };',
  13264. '});',
  13265. 'this.r = this.TRec.$clone({',
  13266. ' i: 1,',
  13267. ' a: [2, 3],',
  13268. ' s: [4, 5],',
  13269. ' m: [[11, 12], [13, 14]],',
  13270. ' p: this.TPoint.$clone({',
  13271. ' x: 21,',
  13272. ' y: 0',
  13273. ' })',
  13274. '});',
  13275. 'this.p = [this.TPoint.$clone({',
  13276. ' x: 1,',
  13277. ' y: 2',
  13278. '}), this.TPoint.$clone({',
  13279. ' x: 3,',
  13280. ' y: 4',
  13281. '})];',
  13282. '']),
  13283. LinesToStr([ // $mod.$main
  13284. '']));
  13285. end;
  13286. procedure TTestModule.TestAdvRecord_ExternalField;
  13287. begin
  13288. StartProgram(false);
  13289. Add([
  13290. '{$modeswitch AdvancedRecords}',
  13291. '{$modeswitch externalclass}',
  13292. 'type',
  13293. ' TCar = record',
  13294. ' public',
  13295. ' Intern: longint external name ''$Intern'';',
  13296. ' Intern2: longint external name ''$Intern2'';',
  13297. ' Bracket: longint external name ''["A B"]'';',
  13298. ' procedure DoIt;',
  13299. ' end;',
  13300. 'procedure tcar.doit;',
  13301. 'begin',
  13302. ' Intern:=Intern+1;',
  13303. ' Intern2:=Intern2+2;',
  13304. ' Bracket:=Bracket+3;',
  13305. 'end;',
  13306. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13307. 'begin',
  13308. ' Rec.intern:=Rec.intern+1;',
  13309. ' Rec.intern2:=Rec.intern2+2;',
  13310. ' Rec.Bracket:=Rec.Bracket+3;',
  13311. ' with Rec do begin',
  13312. ' intern:=intern+1;',
  13313. ' intern2:=intern2+2;',
  13314. ' Bracket:=Bracket+3;',
  13315. ' end;']);
  13316. ConvertProgram;
  13317. CheckSource('TestAdvRecord_ExternalField',
  13318. LinesToStr([ // statements
  13319. 'rtl.recNewT(this, "TCar", function () {',
  13320. ' this.$eq = function (b) {',
  13321. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13322. ' };',
  13323. ' this.$assign = function (s) {',
  13324. ' this.$Intern = s.$Intern;',
  13325. ' this.$Intern2 = s.$Intern2;',
  13326. ' this["A B"] = s["A B"];',
  13327. ' return this;',
  13328. ' };',
  13329. ' this.DoIt = function () {',
  13330. ' this.$Intern = this.$Intern + 1;',
  13331. ' this.$Intern2 = this.$Intern2 + 2;',
  13332. ' this["A B"] = this["A B"] + 3;',
  13333. ' };',
  13334. '});',
  13335. 'this.Rec = this.TCar.$clone({',
  13336. ' $Intern: 11,',
  13337. ' $Intern2: 12,',
  13338. ' "A B": 13',
  13339. '});',
  13340. '']),
  13341. LinesToStr([ // $mod.$main
  13342. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13343. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13344. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13345. 'var $with = $mod.Rec;',
  13346. '$with.$Intern = $with.$Intern + 1;',
  13347. '$with.$Intern2 = $with.$Intern2 + 2;',
  13348. '$with["A B"] = $with["A B"] + 3;',
  13349. '']));
  13350. end;
  13351. procedure TTestModule.TestAdvRecord_SubRecord;
  13352. begin
  13353. StartProgram(false);
  13354. Add([
  13355. '{$modeswitch AdvancedRecords}',
  13356. 'type',
  13357. ' TRec = record',
  13358. ' type',
  13359. ' TPoint = record',
  13360. ' x,y: longint;',
  13361. ' class var Count: nativeint;',
  13362. ' procedure DoIt;',
  13363. ' class procedure DoThat; static;',
  13364. ' end;',
  13365. ' var',
  13366. ' i: longint;',
  13367. ' p: TPoint;',
  13368. ' procedure DoSome;',
  13369. ' end;',
  13370. 'const',
  13371. ' r: TRec = (',
  13372. ' i:1;',
  13373. ' p: (x:21;y:22)',
  13374. ' );',
  13375. 'procedure TRec.DoSome;',
  13376. 'begin',
  13377. ' p.x:=p.y+1;',
  13378. ' p.Count:=p.Count+2;',
  13379. 'end;',
  13380. 'procedure TRec.TPoint.DoIt;',
  13381. 'begin',
  13382. ' Count:=Count+3;',
  13383. 'end;',
  13384. 'class procedure TRec.TPoint.DoThat;',
  13385. 'begin',
  13386. ' Count:=Count+4;',
  13387. 'end;',
  13388. 'begin']);
  13389. ConvertProgram;
  13390. CheckSource('TestAdvRecord_SubRecord',
  13391. LinesToStr([ // statements
  13392. 'rtl.recNewT(this, "TRec", function () {',
  13393. ' rtl.recNewT(this, "TPoint", function () {',
  13394. ' this.x = 0;',
  13395. ' this.y = 0;',
  13396. ' this.Count = 0;',
  13397. ' this.$eq = function (b) {',
  13398. ' return (this.x === b.x) && (this.y === b.y);',
  13399. ' };',
  13400. ' this.$assign = function (s) {',
  13401. ' this.x = s.x;',
  13402. ' this.y = s.y;',
  13403. ' return this;',
  13404. ' };',
  13405. ' this.DoIt = function () {',
  13406. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13407. ' };',
  13408. ' this.DoThat = function () {',
  13409. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13410. ' };',
  13411. ' }, true);',
  13412. ' this.i = 0;',
  13413. ' this.$new = function () {',
  13414. ' var r = Object.create(this);',
  13415. ' r.p = this.TPoint.$new();',
  13416. ' return r;',
  13417. ' };',
  13418. ' this.$eq = function (b) {',
  13419. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13420. ' };',
  13421. ' this.$assign = function (s) {',
  13422. ' this.i = s.i;',
  13423. ' this.p.$assign(s.p);',
  13424. ' return this;',
  13425. ' };',
  13426. ' this.DoSome = function () {',
  13427. ' this.p.x = this.p.y + 1;',
  13428. ' this.TPoint.Count = this.p.Count + 2;',
  13429. ' };',
  13430. '}, true);',
  13431. 'this.r = this.TRec.$clone({',
  13432. ' i: 1,',
  13433. ' p: this.TRec.TPoint.$clone({',
  13434. ' x: 21,',
  13435. ' y: 22',
  13436. ' })',
  13437. '});',
  13438. '']),
  13439. LinesToStr([ // $mod.$main
  13440. '']));
  13441. end;
  13442. procedure TTestModule.TestAdvRecord_SubClass;
  13443. begin
  13444. StartProgram(false);
  13445. Add([
  13446. '{$modeswitch AdvancedRecords}',
  13447. 'type',
  13448. ' TObject = class end;',
  13449. ' TPoint = record',
  13450. ' type',
  13451. ' TBird = class',
  13452. ' procedure DoIt;',
  13453. ' class procedure Glob;',
  13454. ' end;',
  13455. ' procedure DoIt(b: TBird);',
  13456. ' end;',
  13457. 'procedure TPoint.TBird.DoIt;',
  13458. 'begin',
  13459. ' doit;',
  13460. ' self.doit;',
  13461. ' glob;',
  13462. ' self.glob;',
  13463. 'end;',
  13464. 'class procedure TPoint.TBird.Glob;',
  13465. 'begin',
  13466. ' glob;',
  13467. ' self.glob;',
  13468. 'end;',
  13469. 'procedure TPoint.DoIt(b: TBird);',
  13470. 'begin',
  13471. ' b.doit;',
  13472. ' b.glob;',
  13473. ' TBird.glob;',
  13474. 'end;',
  13475. 'begin',
  13476. '']);
  13477. ConvertProgram;
  13478. CheckSource('TestAdvRecord_SubClass',
  13479. LinesToStr([ // statements
  13480. 'rtl.createClass(this, "TObject", null, function () {',
  13481. ' this.$init = function () {',
  13482. ' };',
  13483. ' this.$final = function () {',
  13484. ' };',
  13485. '});',
  13486. 'rtl.recNewT(this, "TPoint", function () {',
  13487. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13488. ' this.DoIt = function () {',
  13489. ' this.DoIt();',
  13490. ' this.DoIt();',
  13491. ' this.$class.Glob();',
  13492. ' this.$class.Glob();',
  13493. ' };',
  13494. ' this.Glob = function () {',
  13495. ' this.Glob();',
  13496. ' this.Glob();',
  13497. ' };',
  13498. ' }, "TPoint.TBird");',
  13499. ' this.$eq = function (b) {',
  13500. ' return true;',
  13501. ' };',
  13502. ' this.$assign = function (s) {',
  13503. ' return this;',
  13504. ' };',
  13505. ' this.DoIt = function (b) {',
  13506. ' b.DoIt();',
  13507. ' b.$class.Glob();',
  13508. ' this.TBird.Glob();',
  13509. ' };',
  13510. '}, true);',
  13511. '']),
  13512. LinesToStr([ // $mod.$main
  13513. '']));
  13514. end;
  13515. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13516. begin
  13517. StartProgram(false);
  13518. Add([
  13519. '{$modeswitch AdvancedRecords}',
  13520. 'type',
  13521. ' IUnknown = interface end;',
  13522. ' TPoint = record',
  13523. ' type IBird = interface end;',
  13524. ' end;',
  13525. 'begin',
  13526. '']);
  13527. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13528. nNotYetImplemented);
  13529. ParseProgram;
  13530. end;
  13531. procedure TTestModule.TestAdvRecord_Constructor;
  13532. begin
  13533. StartProgram(false);
  13534. Add([
  13535. '{$modeswitch AdvancedRecords}',
  13536. 'type',
  13537. ' TPoint = record',
  13538. ' x,y: longint;',
  13539. ' class procedure Run(w: longint = 13); static;',
  13540. ' constructor Create(ax: longint; ay: longint = -1);',
  13541. ' end;',
  13542. 'class procedure tpoint.run(w: longint);',
  13543. 'begin',
  13544. ' run;',
  13545. ' run();',
  13546. 'end;',
  13547. 'constructor tpoint.create(ax,ay: longint);',
  13548. 'begin',
  13549. ' x:=ax;',
  13550. ' self.y:=ay;',
  13551. ' run;',
  13552. ' run(ax);',
  13553. 'end;',
  13554. 'var r: TPoint;',
  13555. 'begin',
  13556. ' r:=TPoint.Create(1,2);',
  13557. ' with TPoint do r:=Create(1,2);',
  13558. ' r.Create(3);',
  13559. ' r:=r.Create(4);',
  13560. '']);
  13561. ConvertProgram;
  13562. CheckSource('TestAdvRecord_Constructor',
  13563. LinesToStr([ // statements
  13564. 'rtl.recNewT(this, "TPoint", function () {',
  13565. ' this.x = 0;',
  13566. ' this.y = 0;',
  13567. ' this.$eq = function (b) {',
  13568. ' return (this.x === b.x) && (this.y === b.y);',
  13569. ' };',
  13570. ' this.$assign = function (s) {',
  13571. ' this.x = s.x;',
  13572. ' this.y = s.y;',
  13573. ' return this;',
  13574. ' };',
  13575. ' this.Run = function (w) {',
  13576. ' $mod.TPoint.Run(13);',
  13577. ' $mod.TPoint.Run(13);',
  13578. ' };',
  13579. ' this.Create = function (ax, ay) {',
  13580. ' this.x = ax;',
  13581. ' this.y = ay;',
  13582. ' this.Run(13);',
  13583. ' this.Run(ax);',
  13584. ' return this;',
  13585. ' };',
  13586. '});',
  13587. 'this.r = this.TPoint.$new();',
  13588. '']),
  13589. LinesToStr([ // $mod.$main
  13590. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13591. 'var $with = $mod.TPoint;',
  13592. '$mod.r.$assign($with.$new().Create(1, 2));',
  13593. '$mod.r.Create(3, -1);',
  13594. '$mod.r.$assign($mod.r.Create(4, -1));',
  13595. '']));
  13596. end;
  13597. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13598. begin
  13599. StartProgram(false);
  13600. Add([
  13601. '{$modeswitch AdvancedRecords}',
  13602. 'type',
  13603. ' TPoint = record',
  13604. ' class var x: longint;',
  13605. ' class procedure Fly; static;',
  13606. ' class constructor Init;',
  13607. ' end;',
  13608. 'var count: word;',
  13609. 'class procedure Tpoint.Fly;',
  13610. 'begin',
  13611. 'end;',
  13612. 'class constructor tpoint.init;',
  13613. 'begin',
  13614. ' count:=count+1;',
  13615. ' x:=x+3;',
  13616. ' tpoint.x:=tpoint.x+4;',
  13617. ' fly;',
  13618. ' tpoint.fly;',
  13619. 'end;',
  13620. 'var r: TPoint;',
  13621. 'begin',
  13622. ' r.x:=r.x+10;',
  13623. ' r.Fly;',
  13624. ' r.Fly();',
  13625. '']);
  13626. ConvertProgram;
  13627. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13628. LinesToStr([ // statements
  13629. 'rtl.recNewT(this, "TPoint", function () {',
  13630. ' this.x = 0;',
  13631. ' this.$eq = function (b) {',
  13632. ' return true;',
  13633. ' };',
  13634. ' this.$assign = function (s) {',
  13635. ' return this;',
  13636. ' };',
  13637. ' this.Fly = function () {',
  13638. ' };',
  13639. '}, true);',
  13640. 'this.count = 0;',
  13641. 'this.r = this.TPoint.$new();',
  13642. '']),
  13643. LinesToStr([ // $mod.$main
  13644. '(function () {',
  13645. ' $mod.count = $mod.count + 1;',
  13646. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13647. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13648. ' $mod.TPoint.Fly();',
  13649. ' $mod.TPoint.Fly();',
  13650. '})();',
  13651. '$mod.TPoint.x = $mod.r.x + 10;',
  13652. '$mod.TPoint.Fly();',
  13653. '$mod.TPoint.Fly();',
  13654. '']));
  13655. end;
  13656. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13657. begin
  13658. StartUnit(false);
  13659. Add([
  13660. 'interface',
  13661. '{$modeswitch AdvancedRecords}',
  13662. 'type',
  13663. ' TPoint = record',
  13664. ' class var x: longint;',
  13665. ' class procedure Fly; static;',
  13666. ' class constructor Init;',
  13667. ' end;',
  13668. 'implementation',
  13669. 'var count: word;',
  13670. 'class procedure Tpoint.Fly;',
  13671. 'begin',
  13672. 'end;',
  13673. 'class constructor tpoint.init;',
  13674. 'begin',
  13675. ' count:=count+1;',
  13676. ' x:=3;',
  13677. ' tpoint.x:=4;',
  13678. ' fly;',
  13679. ' tpoint.fly;',
  13680. 'end;',
  13681. '']);
  13682. ConvertUnit;
  13683. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13684. LinesToStr([ // statements
  13685. 'var $impl = $mod.$impl;',
  13686. 'rtl.recNewT(this, "TPoint", function () {',
  13687. ' this.x = 0;',
  13688. ' this.$eq = function (b) {',
  13689. ' return true;',
  13690. ' };',
  13691. ' this.$assign = function (s) {',
  13692. ' return this;',
  13693. ' };',
  13694. ' this.Fly = function () {',
  13695. ' };',
  13696. '}, true);',
  13697. '']),
  13698. LinesToStr([ // $mod.$init
  13699. '(function () {',
  13700. ' $impl.count = $impl.count + 1;',
  13701. ' $mod.TPoint.x = 3;',
  13702. ' $mod.TPoint.x = 4;',
  13703. ' $mod.TPoint.Fly();',
  13704. ' $mod.TPoint.Fly();',
  13705. '})();',
  13706. '']),
  13707. LinesToStr([ // $mod.$main
  13708. '$impl.count = 0;',
  13709. '']));
  13710. end;
  13711. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13712. begin
  13713. StartProgram(false);
  13714. Add(['type',
  13715. ' TObject = class',
  13716. ' public',
  13717. ' constructor Create;',
  13718. ' destructor Destroy;',
  13719. ' end;',
  13720. ' TBird = TObject;',
  13721. 'constructor tobject.create;',
  13722. 'begin end;',
  13723. 'destructor tobject.destroy;',
  13724. 'begin end;',
  13725. 'var Obj: tobject;',
  13726. 'begin',
  13727. ' obj:=tobject.create;',
  13728. ' obj:=tobject.create();',
  13729. ' obj:=tbird.create;',
  13730. ' obj:=tbird.create();',
  13731. ' obj:=obj.create();',
  13732. ' obj.destroy;',
  13733. '']);
  13734. ConvertProgram;
  13735. CheckSource('TestClass_TObjectDefaultConstructor',
  13736. LinesToStr([ // statements
  13737. 'rtl.createClass(this,"TObject",null,function(){',
  13738. ' this.$init = function () {',
  13739. ' };',
  13740. ' this.$final = function () {',
  13741. ' };',
  13742. ' this.Create = function(){',
  13743. ' return this;',
  13744. ' };',
  13745. ' this.Destroy = function(){',
  13746. ' };',
  13747. '});',
  13748. 'this.Obj = null;'
  13749. ]),
  13750. LinesToStr([ // $mod.$main
  13751. '$mod.Obj = $mod.TObject.$create("Create");',
  13752. '$mod.Obj = $mod.TObject.$create("Create");',
  13753. '$mod.Obj = $mod.TObject.$create("Create");',
  13754. '$mod.Obj = $mod.TObject.$create("Create");',
  13755. '$mod.Obj = $mod.Obj.Create();',
  13756. '$mod.Obj.$destroy("Destroy");',
  13757. '']));
  13758. end;
  13759. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13760. begin
  13761. StartProgram(false);
  13762. Add('type');
  13763. Add(' TObject = class');
  13764. Add(' public');
  13765. Add(' constructor Create(Par: longint);');
  13766. Add(' end;');
  13767. Add('constructor tobject.create(par: longint);');
  13768. Add('begin end;');
  13769. Add('var Obj: tobject;');
  13770. Add('begin');
  13771. Add(' obj:=tobject.create(3);');
  13772. ConvertProgram;
  13773. CheckSource('TestClass_TObjectConstructorWithParams',
  13774. LinesToStr([ // statements
  13775. 'rtl.createClass(this,"TObject",null,function(){',
  13776. ' this.$init = function () {',
  13777. ' };',
  13778. ' this.$final = function () {',
  13779. ' };',
  13780. ' this.Create = function(Par){',
  13781. ' return this;',
  13782. ' };',
  13783. '});',
  13784. 'this.Obj = null;'
  13785. ]),
  13786. LinesToStr([ // $mod.$main
  13787. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13788. ]));
  13789. end;
  13790. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13791. begin
  13792. StartProgram(false);
  13793. Add('type');
  13794. Add(' TObject = class');
  13795. Add(' public');
  13796. Add(' constructor Create;');
  13797. Add(' end;');
  13798. Add(' TTest = class(TObject)');
  13799. Add(' public');
  13800. Add(' constructor Create(const Par: longint = 1);');
  13801. Add(' end;');
  13802. Add('constructor tobject.create;');
  13803. Add('begin end;');
  13804. Add('constructor ttest.create(const par: longint);');
  13805. Add('begin end;');
  13806. Add('var t: ttest;');
  13807. Add('begin');
  13808. Add(' t:=ttest.create;');
  13809. Add(' t:=ttest.create(2);');
  13810. ConvertProgram;
  13811. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13812. LinesToStr([ // statements
  13813. 'rtl.createClass(this,"TObject",null,function(){',
  13814. ' this.$init = function () {',
  13815. ' };',
  13816. ' this.$final = function () {',
  13817. ' };',
  13818. ' this.Create = function(){',
  13819. ' return this;',
  13820. ' };',
  13821. '});',
  13822. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13823. ' this.Create$1 = function (Par) {',
  13824. ' return this;',
  13825. ' };',
  13826. '});',
  13827. 'this.t = null;'
  13828. ]),
  13829. LinesToStr([ // $mod.$main
  13830. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13831. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13832. ]));
  13833. end;
  13834. procedure TTestModule.TestClass_Var;
  13835. begin
  13836. StartProgram(false);
  13837. Add([
  13838. 'type',
  13839. ' TObject = class',
  13840. ' public',
  13841. ' vI: longint;',
  13842. ' constructor Create(Par: longint);',
  13843. ' end;',
  13844. 'constructor tobject.create(par: longint);',
  13845. 'begin',
  13846. ' vi:=par+3',
  13847. 'end;',
  13848. 'var Obj: tobject;',
  13849. 'begin',
  13850. ' obj:=tobject.create(4);',
  13851. ' obj.vi:=obj.VI+5;']);
  13852. ConvertProgram;
  13853. CheckSource('TestClass_Var',
  13854. LinesToStr([ // statements
  13855. 'rtl.createClass(this,"TObject",null,function(){',
  13856. ' this.$init = function () {',
  13857. ' this.vI = 0;',
  13858. ' };',
  13859. ' this.$final = function () {',
  13860. ' };',
  13861. ' this.Create = function(Par){',
  13862. ' this.vI = Par+3;',
  13863. ' return this;',
  13864. ' };',
  13865. '});',
  13866. 'this.Obj = null;'
  13867. ]),
  13868. LinesToStr([ // $mod.$main
  13869. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13870. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13871. ]));
  13872. end;
  13873. procedure TTestModule.TestClass_Method;
  13874. begin
  13875. StartProgram(false);
  13876. Add('type');
  13877. Add(' TObject = class');
  13878. Add(' public');
  13879. Add(' vI: longint;');
  13880. Add(' Sub: TObject;');
  13881. Add(' constructor Create;');
  13882. Add(' function GetIt(Par: longint): tobject;');
  13883. Add(' end;');
  13884. Add('constructor tobject.create; begin end;');
  13885. Add('function tobject.getit(par: longint): tobject;');
  13886. Add('begin');
  13887. Add(' Self.vi:=par+3;');
  13888. Add(' Result:=self.sub;');
  13889. Add('end;');
  13890. Add('var Obj: tobject;');
  13891. Add('begin');
  13892. Add(' obj:=tobject.create;');
  13893. Add(' obj.getit(4);');
  13894. Add(' obj.sub.sub:=nil;');
  13895. Add(' obj.sub.getit(5);');
  13896. Add(' obj.sub.getit(6).SUB:=nil;');
  13897. Add(' obj.sub.getit(7).GETIT(8);');
  13898. Add(' obj.sub.getit(9).SuB.getit(10);');
  13899. ConvertProgram;
  13900. CheckSource('TestClass_Method',
  13901. LinesToStr([ // statements
  13902. 'rtl.createClass(this,"TObject",null,function(){',
  13903. ' this.$init = function () {',
  13904. ' this.vI = 0;',
  13905. ' this.Sub = null;',
  13906. ' };',
  13907. ' this.$final = function () {',
  13908. ' this.Sub = undefined;',
  13909. ' };',
  13910. ' this.Create = function(){',
  13911. ' return this;',
  13912. ' };',
  13913. ' this.GetIt = function(Par){',
  13914. ' var Result = null;',
  13915. ' this.vI = Par + 3;',
  13916. ' Result = this.Sub;',
  13917. ' return Result;',
  13918. ' };',
  13919. '});',
  13920. 'this.Obj = null;'
  13921. ]),
  13922. LinesToStr([ // $mod.$main
  13923. '$mod.Obj = $mod.TObject.$create("Create");',
  13924. '$mod.Obj.GetIt(4);',
  13925. '$mod.Obj.Sub.Sub=null;',
  13926. '$mod.Obj.Sub.GetIt(5);',
  13927. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13928. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13929. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13930. ]));
  13931. end;
  13932. procedure TTestModule.TestClass_Implementation;
  13933. begin
  13934. StartUnit(false);
  13935. Add([
  13936. 'interface',
  13937. 'type',
  13938. ' TObject = class',
  13939. ' constructor Create;',
  13940. ' end;',
  13941. 'implementation',
  13942. 'type',
  13943. ' TIntClass = class',
  13944. ' constructor Create; reintroduce;',
  13945. ' class procedure DoGlob;',
  13946. ' end;',
  13947. 'constructor tintclass.create;',
  13948. 'begin',
  13949. ' inherited;',
  13950. ' inherited create;',
  13951. ' doglob;',
  13952. 'end;',
  13953. 'class procedure tintclass.doglob;',
  13954. 'begin',
  13955. 'end;',
  13956. 'constructor tobject.create;',
  13957. 'var',
  13958. ' iC: tintclass;',
  13959. 'begin',
  13960. ' ic:=tintclass.create;',
  13961. ' tintclass.doglob;',
  13962. ' ic.doglob;',
  13963. 'end;',
  13964. 'initialization',
  13965. ' tintclass.doglob;',
  13966. '']);
  13967. ConvertUnit;
  13968. CheckSource('TestClass_Implementation',
  13969. LinesToStr([ // statements
  13970. 'var $impl = $mod.$impl;',
  13971. 'rtl.createClass(this, "TObject", null, function () {',
  13972. ' this.$init = function () {',
  13973. ' };',
  13974. ' this.$final = function () {',
  13975. ' };',
  13976. ' this.Create = function () {',
  13977. ' var iC = null;',
  13978. ' iC = $impl.TIntClass.$create("Create$1");',
  13979. ' $impl.TIntClass.DoGlob();',
  13980. ' iC.$class.DoGlob();',
  13981. ' return this;',
  13982. ' };',
  13983. '});',
  13984. '']),
  13985. LinesToStr([ // $mod.$main
  13986. '$impl.TIntClass.DoGlob();',
  13987. '']),
  13988. LinesToStr([
  13989. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  13990. ' this.Create$1 = function () {',
  13991. ' $mod.TObject.Create.call(this);',
  13992. ' $mod.TObject.Create.call(this);',
  13993. ' this.$class.DoGlob();',
  13994. ' return this;',
  13995. ' };',
  13996. ' this.DoGlob = function () {',
  13997. ' };',
  13998. '});',
  13999. '']));
  14000. end;
  14001. procedure TTestModule.TestClass_Inheritance;
  14002. begin
  14003. StartProgram(false);
  14004. Add('type');
  14005. Add(' TObject = class');
  14006. Add(' public');
  14007. Add(' constructor Create;');
  14008. Add(' end;');
  14009. Add(' TClassA = class');
  14010. Add(' end;');
  14011. Add(' TClassB = class(TObject)');
  14012. Add(' procedure ProcB;');
  14013. Add(' end;');
  14014. Add('constructor tobject.create; begin end;');
  14015. Add('procedure tclassb.procb; begin end;');
  14016. Add('var');
  14017. Add(' oO: TObject;');
  14018. Add(' oA: TClassA;');
  14019. Add(' oB: TClassB;');
  14020. Add('begin');
  14021. Add(' oO:=tobject.Create;');
  14022. Add(' oA:=tclassa.Create;');
  14023. Add(' ob:=tclassb.Create;');
  14024. Add(' if oo is tclassa then ;');
  14025. Add(' ob:=oo as tclassb;');
  14026. Add(' (oo as tclassb).procb;');
  14027. ConvertProgram;
  14028. CheckSource('TestClass_Inheritance',
  14029. LinesToStr([ // statements
  14030. 'rtl.createClass(this,"TObject",null,function(){',
  14031. ' this.$init = function () {',
  14032. ' };',
  14033. ' this.$final = function () {',
  14034. ' };',
  14035. ' this.Create = function () {',
  14036. ' return this;',
  14037. ' };',
  14038. '});',
  14039. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14040. '});',
  14041. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14042. ' this.ProcB = function () {',
  14043. ' };',
  14044. '});',
  14045. 'this.oO = null;',
  14046. 'this.oA = null;',
  14047. 'this.oB = null;'
  14048. ]),
  14049. LinesToStr([ // $mod.$main
  14050. '$mod.oO = $mod.TObject.$create("Create");',
  14051. '$mod.oA = $mod.TClassA.$create("Create");',
  14052. '$mod.oB = $mod.TClassB.$create("Create");',
  14053. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14054. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14055. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14056. ]));
  14057. end;
  14058. procedure TTestModule.TestClass_TypeAlias;
  14059. begin
  14060. StartProgram(false);
  14061. Add([
  14062. '{$interfaces corba}',
  14063. 'type',
  14064. ' IObject = interface',
  14065. ' end;',
  14066. ' IBird = type IObject;',
  14067. ' TObject = class',
  14068. ' end;',
  14069. ' TBird = type TObject;',
  14070. 'var',
  14071. ' oObj: TObject;',
  14072. ' oBird: TBird;',
  14073. ' IntfObj: IObject;',
  14074. ' IntfBird: IBird;',
  14075. 'begin',
  14076. ' oObj:=oBird;',
  14077. '']);
  14078. ConvertProgram;
  14079. CheckSource('TestClass_TypeAlias',
  14080. LinesToStr([ // statements
  14081. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14082. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14083. 'rtl.createClass(this, "TObject", null, function () {',
  14084. ' this.$init = function () {',
  14085. ' };',
  14086. ' this.$final = function () {',
  14087. ' };',
  14088. '});',
  14089. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14090. '});',
  14091. 'this.oObj = null;',
  14092. 'this.oBird = null;',
  14093. 'this.IntfObj = null;',
  14094. 'this.IntfBird = null;',
  14095. '']),
  14096. LinesToStr([ // $mod.$main
  14097. '$mod.oObj = $mod.oBird;',
  14098. '']));
  14099. end;
  14100. procedure TTestModule.TestClass_AbstractMethod;
  14101. begin
  14102. StartProgram(false);
  14103. Add('type');
  14104. Add(' TObject = class');
  14105. Add(' public');
  14106. Add(' procedure DoIt; virtual; abstract;');
  14107. Add(' end;');
  14108. Add('begin');
  14109. ConvertProgram;
  14110. CheckSource('TestClass_AbstractMethod',
  14111. LinesToStr([ // statements
  14112. 'rtl.createClass(this,"TObject",null,function(){',
  14113. ' this.$init = function () {',
  14114. ' };',
  14115. ' this.$final = function () {',
  14116. ' };',
  14117. '});'
  14118. ]),
  14119. LinesToStr([ // this.$main
  14120. ''
  14121. ]));
  14122. end;
  14123. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14124. begin
  14125. StartProgram(false);
  14126. Add([
  14127. 'type',
  14128. ' TObject = class',
  14129. ' procedure DoAbstract; virtual; abstract;',
  14130. ' procedure DoVirtual; virtual;',
  14131. ' procedure DoIt;',
  14132. ' end;',
  14133. ' TA = class',
  14134. ' procedure doabstract; override;',
  14135. ' procedure dovirtual; override;',
  14136. ' procedure DoSome;',
  14137. ' end;',
  14138. 'procedure tobject.dovirtual;',
  14139. 'begin',
  14140. ' inherited; // call non existing ancestor -> ignore silently',
  14141. 'end;',
  14142. 'procedure tobject.doit;',
  14143. 'begin',
  14144. 'end;',
  14145. 'procedure ta.doabstract;',
  14146. 'begin',
  14147. ' inherited dovirtual; // call TObject.DoVirtual',
  14148. 'end;',
  14149. 'procedure ta.dovirtual;',
  14150. 'begin',
  14151. ' inherited; // call TObject.DoVirtual',
  14152. ' inherited dovirtual; // call TObject.DoVirtual',
  14153. ' inherited dovirtual(); // call TObject.DoVirtual',
  14154. ' doit;',
  14155. ' doit();',
  14156. 'end;',
  14157. 'procedure ta.dosome;',
  14158. 'begin',
  14159. ' inherited; // call non existing ancestor method -> silently ignore',
  14160. 'end;',
  14161. 'begin']);
  14162. ConvertProgram;
  14163. CheckSource('TestClass_CallInherited_ProcNoParams',
  14164. LinesToStr([ // statements
  14165. 'rtl.createClass(this,"TObject",null,function(){',
  14166. ' this.$init = function () {',
  14167. ' };',
  14168. ' this.$final = function () {',
  14169. ' };',
  14170. ' this.DoVirtual = function () {',
  14171. ' };',
  14172. ' this.DoIt = function () {',
  14173. ' };',
  14174. '});',
  14175. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14176. ' this.DoAbstract = function () {',
  14177. ' $mod.TObject.DoVirtual.call(this);',
  14178. ' };',
  14179. ' this.DoVirtual = function () {',
  14180. ' $mod.TObject.DoVirtual.call(this);',
  14181. ' $mod.TObject.DoVirtual.call(this);',
  14182. ' $mod.TObject.DoVirtual.call(this);',
  14183. ' this.DoIt();',
  14184. ' this.DoIt();',
  14185. ' };',
  14186. ' this.DoSome = function () {',
  14187. ' };',
  14188. '});'
  14189. ]),
  14190. LinesToStr([ // this.$main
  14191. ''
  14192. ]));
  14193. end;
  14194. procedure TTestModule.TestClass_CallInherited_WithParams;
  14195. begin
  14196. StartProgram(false);
  14197. Add([
  14198. 'type',
  14199. ' TObject = class',
  14200. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14201. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14202. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14203. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14204. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14205. ' end;',
  14206. ' TClassA = class',
  14207. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14208. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14209. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14210. ' end;',
  14211. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14212. 'begin',
  14213. 'end;',
  14214. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14215. 'begin',
  14216. 'end;',
  14217. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14218. 'begin',
  14219. 'end;',
  14220. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14221. 'begin',
  14222. 'end;',
  14223. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14224. 'begin',
  14225. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14226. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14227. 'end;',
  14228. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14229. 'begin',
  14230. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14231. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14232. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14233. ' doit(pa,pb);',
  14234. ' doit(pa);',
  14235. ' doit2(pa);',
  14236. ' doit2;',
  14237. 'end;',
  14238. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14239. 'begin',
  14240. ' pa:=inherited;',
  14241. 'end;',
  14242. 'begin']);
  14243. ConvertProgram;
  14244. CheckSource('TestClass_CallInherited_WithParams',
  14245. LinesToStr([ // statements
  14246. 'rtl.createClass(this,"TObject",null,function(){',
  14247. ' this.$init = function () {',
  14248. ' };',
  14249. ' this.$final = function () {',
  14250. ' };',
  14251. ' this.DoVirtual = function (pA,pB) {',
  14252. ' };',
  14253. ' this.DoIt = function (pA,pB) {',
  14254. ' };',
  14255. ' this.DoIt2 = function (pA,pB) {',
  14256. ' };',
  14257. ' this.GetIt = function (pA, pB) {',
  14258. ' var Result = 0;',
  14259. ' return Result;',
  14260. ' };',
  14261. '});',
  14262. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14263. ' this.DoAbstract = function (pA,pB) {',
  14264. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14265. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14266. ' };',
  14267. ' this.DoVirtual = function (pA,pB) {',
  14268. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14269. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14270. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14271. ' this.DoIt(pA,pB);',
  14272. ' this.DoIt(pA,0);',
  14273. ' this.DoIt2(pA,2);',
  14274. ' this.DoIt2(1,2);',
  14275. ' };',
  14276. ' this.GetIt$1 = function (pA, pB) {',
  14277. ' var Result = 0;',
  14278. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14279. ' return Result;',
  14280. ' };',
  14281. '});'
  14282. ]),
  14283. LinesToStr([ // this.$main
  14284. ''
  14285. ]));
  14286. end;
  14287. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14288. begin
  14289. StartProgram(false);
  14290. Add('type');
  14291. Add(' TObject = class');
  14292. Add(' constructor Create; virtual;');
  14293. Add(' constructor CreateWithB(b: boolean);');
  14294. Add(' end;');
  14295. Add(' TA = class');
  14296. Add(' constructor Create; override;');
  14297. Add(' constructor CreateWithC(c: AnsiChar);');
  14298. Add(' procedure DoIt;');
  14299. Add(' class function DoSome: TObject;');
  14300. Add(' end;');
  14301. Add('constructor tobject.create;');
  14302. Add('begin');
  14303. Add(' inherited; // call non existing ancestor -> ignore silently');
  14304. Add('end;');
  14305. Add('constructor tobject.createwithb(b: boolean);');
  14306. Add('begin');
  14307. Add(' inherited; // call non existing ancestor -> ignore silently');
  14308. Add(' create; // normal call');
  14309. Add('end;');
  14310. Add('constructor ta.create;');
  14311. Add('begin');
  14312. Add(' inherited; // normal call TObject.Create');
  14313. Add(' inherited create; // normal call TObject.Create');
  14314. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14315. Add('end;');
  14316. Add('constructor ta.createwithc(c: AnsiChar);');
  14317. Add('begin');
  14318. Add(' inherited create; // call TObject.Create');
  14319. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14320. Add(' doit;');
  14321. Add(' doit();');
  14322. Add(' dosome;');
  14323. Add('end;');
  14324. Add('procedure ta.doit;');
  14325. Add('begin');
  14326. Add(' create; // normal call');
  14327. Add(' createwithb(false); // normal call');
  14328. Add(' createwithc(''c''); // normal call');
  14329. Add('end;');
  14330. Add('class function ta.dosome: TObject;');
  14331. Add('begin');
  14332. Add(' Result:=create; // constructor');
  14333. Add(' Result:=createwithb(true); // constructor');
  14334. Add(' Result:=createwithc(''c''); // constructor');
  14335. Add('end;');
  14336. Add('begin');
  14337. ConvertProgram;
  14338. CheckSource('TestClass_CallInheritedConstructor',
  14339. LinesToStr([ // statements
  14340. 'rtl.createClass(this,"TObject",null,function(){',
  14341. ' this.$init = function () {',
  14342. ' };',
  14343. ' this.$final = function () {',
  14344. ' };',
  14345. ' this.Create = function () {',
  14346. ' return this;',
  14347. ' };',
  14348. ' this.CreateWithB = function (b) {',
  14349. ' this.Create();',
  14350. ' return this;',
  14351. ' };',
  14352. '});',
  14353. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14354. ' this.Create = function () {',
  14355. ' $mod.TObject.Create.call(this);',
  14356. ' $mod.TObject.Create.call(this);',
  14357. ' $mod.TObject.CreateWithB.call(this, false);',
  14358. ' return this;',
  14359. ' };',
  14360. ' this.CreateWithC = function (c) {',
  14361. ' $mod.TObject.Create.call(this);',
  14362. ' $mod.TObject.CreateWithB.call(this, true);',
  14363. ' this.DoIt();',
  14364. ' this.DoIt();',
  14365. ' this.$class.DoSome();',
  14366. ' return this;',
  14367. ' };',
  14368. ' this.DoIt = function () {',
  14369. ' this.Create();',
  14370. ' this.CreateWithB(false);',
  14371. ' this.CreateWithC("c");',
  14372. ' };',
  14373. ' this.DoSome = function () {',
  14374. ' var Result = null;',
  14375. ' Result = this.$create("Create");',
  14376. ' Result = this.$create("CreateWithB", [true]);',
  14377. ' Result = this.$create("CreateWithC", ["c"]);',
  14378. ' return Result;',
  14379. ' };',
  14380. '});'
  14381. ]),
  14382. LinesToStr([ // this.$main
  14383. ''
  14384. ]));
  14385. end;
  14386. procedure TTestModule.TestClass_ClassVar_Assign;
  14387. begin
  14388. StartProgram(false);
  14389. Add([
  14390. 'type',
  14391. ' TObject = class',
  14392. ' public',
  14393. ' class var vI: longint;',
  14394. ' class var Sub: TObject;',
  14395. ' constructor Create;',
  14396. ' class function GetIt(var Par: longint): tobject;',
  14397. ' end;',
  14398. 'constructor tobject.create;',
  14399. 'begin',
  14400. ' vi:=vi+1;',
  14401. ' Self.vi:=Self.vi+1;',
  14402. ' inc(vi);',
  14403. 'end;',
  14404. 'class function tobject.getit(var par: longint): tobject;',
  14405. 'begin',
  14406. ' vi:=vi+3;',
  14407. ' Self.vi:=Self.vi+4;',
  14408. ' inc(vi);',
  14409. ' Result:=self.sub;',
  14410. ' GetIt(vi);',
  14411. 'end;',
  14412. 'var Obj: tobject;',
  14413. 'begin',
  14414. ' obj:=tobject.create;',
  14415. ' tobject.vi:=3;',
  14416. ' if tobject.vi=4 then ;',
  14417. ' tobject.sub:=nil;',
  14418. ' obj.sub:=nil;',
  14419. ' obj.sub.sub:=nil;']);
  14420. ConvertProgram;
  14421. CheckSource('TestClass_ClassVar_Assign',
  14422. LinesToStr([ // statements
  14423. 'rtl.createClass(this,"TObject",null,function(){',
  14424. ' this.vI = 0;',
  14425. ' this.Sub = null;',
  14426. ' this.$init = function () {',
  14427. ' };',
  14428. ' this.$final = function () {',
  14429. ' };',
  14430. ' this.Create = function(){',
  14431. ' $mod.TObject.vI = this.vI+1;',
  14432. ' $mod.TObject.vI = this.vI+1;',
  14433. ' $mod.TObject.vI += 1;',
  14434. ' return this;',
  14435. ' };',
  14436. ' this.GetIt = function(Par){',
  14437. ' var Result = null;',
  14438. ' $mod.TObject.vI = this.vI + 3;',
  14439. ' $mod.TObject.vI = this.vI + 4;',
  14440. ' $mod.TObject.vI += 1;',
  14441. ' Result = this.Sub;',
  14442. ' this.GetIt({',
  14443. ' p: $mod.TObject,',
  14444. ' get: function () {',
  14445. ' return this.p.vI;',
  14446. ' },',
  14447. ' set: function (v) {',
  14448. ' this.p.vI = v;',
  14449. ' }',
  14450. ' });',
  14451. ' return Result;',
  14452. ' };',
  14453. '});',
  14454. 'this.Obj = null;'
  14455. ]),
  14456. LinesToStr([ // $mod.$main
  14457. '$mod.Obj = $mod.TObject.$create("Create");',
  14458. '$mod.TObject.vI = 3;',
  14459. 'if ($mod.TObject.vI === 4);',
  14460. '$mod.TObject.Sub=null;',
  14461. '$mod.TObject.Sub=null;',
  14462. '$mod.TObject.Sub=null;',
  14463. '']));
  14464. end;
  14465. procedure TTestModule.TestClass_CallClassMethod;
  14466. begin
  14467. StartProgram(false);
  14468. Add('type');
  14469. Add(' TObject = class');
  14470. Add(' public');
  14471. Add(' class var vI: longint;');
  14472. Add(' class var Sub: TObject;');
  14473. Add(' constructor Create;');
  14474. Add(' function GetMore(Par: longint): longint;');
  14475. Add(' class function GetIt(Par: longint): tobject;');
  14476. Add(' end;');
  14477. Add('constructor tobject.create;');
  14478. Add('begin');
  14479. Add(' sub:=getit(3);');
  14480. Add(' vi:=getmore(4);');
  14481. Add(' sub:=Self.getit(5);');
  14482. Add(' vi:=Self.getmore(6);');
  14483. Add('end;');
  14484. Add('function tobject.getmore(par: longint): longint;');
  14485. Add('begin');
  14486. Add(' sub:=getit(11);');
  14487. Add(' vi:=getmore(12);');
  14488. Add(' sub:=self.getit(13);');
  14489. Add(' vi:=self.getmore(14);');
  14490. Add('end;');
  14491. Add('class function tobject.getit(par: longint): tobject;');
  14492. Add('begin');
  14493. Add(' sub:=getit(21);');
  14494. Add(' vi:=sub.getmore(22);');
  14495. Add(' sub:=self.getit(23);');
  14496. Add(' vi:=self.sub.getmore(24);');
  14497. Add('end;');
  14498. Add('var Obj: tobject;');
  14499. Add('begin');
  14500. Add(' obj:=tobject.create;');
  14501. Add(' tobject.getit(5);');
  14502. Add(' obj.getit(6);');
  14503. Add(' obj.sub.getit(7);');
  14504. Add(' obj.sub.getit(8).SUB:=nil;');
  14505. Add(' obj.sub.getit(9).GETIT(10);');
  14506. Add(' obj.sub.getit(11).SuB.getit(12);');
  14507. ConvertProgram;
  14508. CheckSource('TestClass_CallClassMethod',
  14509. LinesToStr([ // statements
  14510. 'rtl.createClass(this,"TObject",null,function(){',
  14511. ' this.vI = 0;',
  14512. ' this.Sub = null;',
  14513. ' this.$init = function () {',
  14514. ' };',
  14515. ' this.$final = function () {',
  14516. ' };',
  14517. ' this.Create = function(){',
  14518. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14519. ' $mod.TObject.vI = this.GetMore(4);',
  14520. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14521. ' $mod.TObject.vI = this.GetMore(6);',
  14522. ' return this;',
  14523. ' };',
  14524. ' this.GetMore = function(Par){',
  14525. ' var Result = 0;',
  14526. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14527. ' $mod.TObject.vI = this.GetMore(12);',
  14528. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14529. ' $mod.TObject.vI = this.GetMore(14);',
  14530. ' return Result;',
  14531. ' };',
  14532. ' this.GetIt = function(Par){',
  14533. ' var Result = null;',
  14534. ' $mod.TObject.Sub = this.GetIt(21);',
  14535. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14536. ' $mod.TObject.Sub = this.GetIt(23);',
  14537. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14538. ' return Result;',
  14539. ' };',
  14540. '});',
  14541. 'this.Obj = null;'
  14542. ]),
  14543. LinesToStr([ // $mod.$main
  14544. '$mod.Obj = $mod.TObject.$create("Create");',
  14545. '$mod.TObject.GetIt(5);',
  14546. '$mod.Obj.$class.GetIt(6);',
  14547. '$mod.Obj.Sub.$class.GetIt(7);',
  14548. '$mod.TObject.Sub=null;',
  14549. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14550. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14551. '']));
  14552. end;
  14553. procedure TTestModule.TestClass_CallClassMethodStatic;
  14554. begin
  14555. StartProgram(false);
  14556. Add([
  14557. 'type',
  14558. ' TObject = class',
  14559. ' public',
  14560. ' class function Fly: tobject; static;',
  14561. ' end;',
  14562. 'class function tobject.Fly: tobject;',
  14563. 'begin',
  14564. ' Result.Fly;',
  14565. ' Result.Fly();',
  14566. ' Fly;',
  14567. ' Fly();',
  14568. ' Fly.Fly;',
  14569. ' Fly.Fly();',
  14570. 'end;',
  14571. 'var Obj: tobject;',
  14572. 'begin',
  14573. ' obj.Fly;',
  14574. ' obj.Fly();',
  14575. ' with obj do begin',
  14576. ' Fly;',
  14577. ' Fly();',
  14578. ' end;',
  14579. '']);
  14580. ConvertProgram;
  14581. CheckSource('TestClass_CallClassMethodStatic',
  14582. LinesToStr([ // statements
  14583. 'rtl.createClass(this, "TObject", null, function () {',
  14584. ' this.$init = function () {',
  14585. ' };',
  14586. ' this.$final = function () {',
  14587. ' };',
  14588. ' this.Fly = function () {',
  14589. ' var Result = null;',
  14590. ' $mod.TObject.Fly();',
  14591. ' $mod.TObject.Fly();',
  14592. ' $mod.TObject.Fly();',
  14593. ' $mod.TObject.Fly();',
  14594. ' $mod.TObject.Fly();',
  14595. ' $mod.TObject.Fly();',
  14596. ' return Result;',
  14597. ' };',
  14598. '});',
  14599. 'this.Obj = null;'
  14600. ]),
  14601. LinesToStr([ // $mod.$main
  14602. '$mod.TObject.Fly();',
  14603. '$mod.TObject.Fly();',
  14604. 'var $with = $mod.Obj;',
  14605. '$with.Fly();',
  14606. '$with.Fly();',
  14607. '']));
  14608. end;
  14609. procedure TTestModule.TestClass_Property;
  14610. begin
  14611. StartProgram(false);
  14612. Add('type');
  14613. Add(' TObject = class');
  14614. Add(' Fx: longint;');
  14615. Add(' Fy: longint;');
  14616. Add(' function GetInt: longint;');
  14617. Add(' procedure SetInt(Value: longint);');
  14618. Add(' procedure DoIt;');
  14619. Add(' property IntA: longint read Fx write Fy;');
  14620. Add(' property IntB: longint read GetInt write SetInt;');
  14621. Add(' end;');
  14622. Add('function tobject.getint: longint;');
  14623. Add('begin');
  14624. Add(' result:=fx;');
  14625. Add('end;');
  14626. Add('procedure tobject.setint(value: longint);');
  14627. Add('begin');
  14628. Add(' if value=fy then exit;');
  14629. Add(' fy:=value;');
  14630. Add('end;');
  14631. Add('procedure tobject.doit;');
  14632. Add('begin');
  14633. Add(' IntA:=IntA+1;');
  14634. Add(' Self.IntA:=Self.IntA+1;');
  14635. Add(' IntB:=IntB+1;');
  14636. Add(' Self.IntB:=Self.IntB+1;');
  14637. Add('end;');
  14638. Add('var Obj: tobject;');
  14639. Add('begin');
  14640. Add(' obj.inta:=obj.inta+1;');
  14641. Add(' if obj.intb=2 then;');
  14642. Add(' obj.intb:=obj.intb+2;');
  14643. Add(' obj.setint(obj.inta);');
  14644. ConvertProgram;
  14645. CheckSource('TestClass_Property',
  14646. LinesToStr([ // statements
  14647. 'rtl.createClass(this, "TObject", null, function () {',
  14648. ' this.$init = function () {',
  14649. ' this.Fx = 0;',
  14650. ' this.Fy = 0;',
  14651. ' };',
  14652. ' this.$final = function () {',
  14653. ' };',
  14654. ' this.GetInt = function () {',
  14655. ' var Result = 0;',
  14656. ' Result = this.Fx;',
  14657. ' return Result;',
  14658. ' };',
  14659. ' this.SetInt = function (Value) {',
  14660. ' if (Value === this.Fy) return;',
  14661. ' this.Fy = Value;',
  14662. ' };',
  14663. ' this.DoIt = function () {',
  14664. ' this.Fy = this.Fx + 1;',
  14665. ' this.Fy = this.Fx + 1;',
  14666. ' this.SetInt(this.GetInt() + 1);',
  14667. ' this.SetInt(this.GetInt() + 1);',
  14668. ' };',
  14669. '});',
  14670. 'this.Obj = null;'
  14671. ]),
  14672. LinesToStr([ // $mod.$main
  14673. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14674. 'if ($mod.Obj.GetInt() === 2);',
  14675. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14676. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14677. ]));
  14678. end;
  14679. procedure TTestModule.TestClass_Property_ClassMethod;
  14680. begin
  14681. StartProgram(false);
  14682. Add([
  14683. 'type',
  14684. ' TObject = class',
  14685. ' class var Fx: longint;',
  14686. ' class var Fy: longint;',
  14687. ' class function GetInt: longint;',
  14688. ' class procedure SetInt(Value: longint);',
  14689. ' end;',
  14690. ' TBird = class',
  14691. ' class procedure DoIt;',
  14692. ' class property IntA: longint read Fx write Fy;',
  14693. ' class property IntB: longint read GetInt write SetInt;',
  14694. ' end;',
  14695. 'class function tobject.getint: longint;',
  14696. 'begin',
  14697. ' result:=fx;',
  14698. 'end;',
  14699. 'class procedure tobject.setint(value: longint);',
  14700. 'begin',
  14701. 'end;',
  14702. 'class procedure tbird.doit;',
  14703. 'begin',
  14704. ' FX:=3;',
  14705. ' IntA:=IntA+1;',
  14706. ' Self.IntA:=Self.IntA+1;',
  14707. ' IntB:=IntB+1;',
  14708. ' Self.IntB:=Self.IntB+1;',
  14709. ' with Self do begin',
  14710. ' FX:=11;',
  14711. ' IntA:=IntA+12;',
  14712. ' IntB:=IntB+13;',
  14713. ' end;',
  14714. 'end;',
  14715. 'var Obj: tbird;',
  14716. 'begin',
  14717. ' tbird.fx:=tbird.fx+1;',
  14718. ' tbird.inta:=tbird.inta+1;',
  14719. ' if tbird.intb=2 then;',
  14720. ' tbird.intb:=tbird.intb+2;',
  14721. ' tbird.setint(tbird.inta);',
  14722. ' obj.inta:=obj.inta+1;',
  14723. ' if obj.intb=2 then;',
  14724. ' obj.intb:=obj.intb+2;',
  14725. ' obj.setint(obj.inta);',
  14726. ' with Tbird do begin',
  14727. ' FX:=FY+1;',
  14728. ' inta:=inta+2;',
  14729. ' intb:=intb+3;',
  14730. ' end;',
  14731. ' with Obj do begin',
  14732. ' FX:=FY+1;',
  14733. ' inta:=inta+2;',
  14734. ' intb:=intb+3;',
  14735. ' end;',
  14736. '']);
  14737. ConvertProgram;
  14738. CheckSource('TestClass_Property_ClassMethod',
  14739. LinesToStr([ // statements
  14740. 'rtl.createClass(this, "TObject", null, function () {',
  14741. ' this.Fx = 0;',
  14742. ' this.Fy = 0;',
  14743. ' this.$init = function () {',
  14744. ' };',
  14745. ' this.$final = function () {',
  14746. ' };',
  14747. ' this.GetInt = function () {',
  14748. ' var Result = 0;',
  14749. ' Result = this.Fx;',
  14750. ' return Result;',
  14751. ' };',
  14752. ' this.SetInt = function (Value) {',
  14753. ' };',
  14754. '});',
  14755. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14756. ' this.DoIt = function () {',
  14757. ' $mod.TObject.Fx = 3;',
  14758. ' $mod.TObject.Fy = this.Fx + 1;',
  14759. ' $mod.TObject.Fy = this.Fx + 1;',
  14760. ' this.SetInt(this.GetInt() + 1);',
  14761. ' this.SetInt(this.GetInt() + 1);',
  14762. ' $mod.TObject.Fx = 11;',
  14763. ' $mod.TObject.Fy = this.Fx + 12;',
  14764. ' this.SetInt(this.GetInt() + 13);',
  14765. ' };',
  14766. '});',
  14767. 'this.Obj = null;'
  14768. ]),
  14769. LinesToStr([ // $mod.$main
  14770. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14771. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14772. 'if ($mod.TBird.GetInt() === 2);',
  14773. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14774. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14775. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14776. 'if ($mod.Obj.$class.GetInt() === 2);',
  14777. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14778. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14779. 'var $with = $mod.TBird;',
  14780. '$mod.TObject.Fx = $with.Fy + 1;',
  14781. '$mod.TObject.Fy = $with.Fx + 2;',
  14782. '$with.SetInt($with.GetInt() + 3);',
  14783. 'var $with1 = $mod.Obj;',
  14784. '$mod.TObject.Fx = $with1.Fy + 1;',
  14785. '$mod.TObject.Fy = $with1.Fx + 2;',
  14786. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14787. '']));
  14788. end;
  14789. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14790. begin
  14791. StartProgram(false);
  14792. Add([
  14793. 'type',
  14794. ' TObject = class',
  14795. ' class function GetInt: longint; static;',
  14796. ' class procedure SetInt(Value: longint); static;',
  14797. ' class function GetItems(Index: word): longint; static;',
  14798. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14799. ' end;',
  14800. ' TBird = class',
  14801. ' class procedure Fly;',
  14802. ' class property IntA: longint read GetInt write SetInt;',
  14803. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14804. ' end;',
  14805. 'class function tobject.getint: longint;',
  14806. 'begin',
  14807. 'end;',
  14808. 'class procedure tobject.setint(value: longint);',
  14809. 'begin',
  14810. 'end;',
  14811. 'class function tobject.GetItems(Index: word): longint;',
  14812. 'begin',
  14813. 'end;',
  14814. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14815. 'begin',
  14816. 'end;',
  14817. 'class procedure tbird.fly;',
  14818. 'var w: longint;',
  14819. 'begin',
  14820. ' inta:=inta+51;',
  14821. ' w:=items[52];',
  14822. ' items[53]:=54;',
  14823. 'end;',
  14824. 'var Obj: tbird;',
  14825. ' i: longint;',
  14826. 'begin',
  14827. ' tbird.inta:=tbird.inta+1;',
  14828. ' i:=tbird.items[2];',
  14829. ' tbird.items[3]:=4;',
  14830. ' obj.inta:=obj.inta+11;',
  14831. ' i:=obj.items[12];',
  14832. ' obj.items[13]:=14;',
  14833. ' with Tbird do begin',
  14834. ' inta:=inta+21;',
  14835. ' i:=items[22];',
  14836. ' items[23]:=24;',
  14837. ' end;',
  14838. ' with Obj do begin',
  14839. ' inta:=inta+31;',
  14840. ' i:=items[32];',
  14841. ' items[33]:=34;',
  14842. ' end;',
  14843. '']);
  14844. ConvertProgram;
  14845. CheckSource('TestClass_Property_ClassMethod',
  14846. LinesToStr([ // statements
  14847. 'rtl.createClass(this, "TObject", null, function () {',
  14848. ' this.$init = function () {',
  14849. ' };',
  14850. ' this.$final = function () {',
  14851. ' };',
  14852. ' this.GetInt = function () {',
  14853. ' var Result = 0;',
  14854. ' return Result;',
  14855. ' };',
  14856. ' this.SetInt = function (Value) {',
  14857. ' };',
  14858. ' this.GetItems = function (Index) {',
  14859. ' var Result = 0;',
  14860. ' return Result;',
  14861. ' };',
  14862. ' this.SetItems = function (Index, Value) {',
  14863. ' };',
  14864. '});',
  14865. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14866. ' this.Fly = function () {',
  14867. ' var w = 0;',
  14868. ' this.SetInt(this.GetInt() + 51);',
  14869. ' w = this.GetItems(52);',
  14870. ' this.SetItems(53, 54);',
  14871. ' };',
  14872. '});',
  14873. 'this.Obj = null;',
  14874. 'this.i = 0;',
  14875. '']),
  14876. LinesToStr([ // $mod.$main
  14877. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14878. '$mod.i = $mod.TObject.GetItems(2);',
  14879. '$mod.TObject.SetItems(3, 4);',
  14880. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14881. '$mod.i = $mod.TObject.GetItems(12);',
  14882. '$mod.TObject.SetItems(13, 14);',
  14883. 'var $with = $mod.TBird;',
  14884. '$with.SetInt($with.GetInt() + 21);',
  14885. '$mod.i = $with.GetItems(22);',
  14886. '$with.SetItems(23, 24);',
  14887. 'var $with1 = $mod.Obj;',
  14888. '$with1.SetInt($with1.GetInt() + 31);',
  14889. '$mod.i = $with1.GetItems(32);',
  14890. '$with1.SetItems(33, 34);',
  14891. '']));
  14892. end;
  14893. procedure TTestModule.TestClass_Property_Indexed;
  14894. begin
  14895. StartProgram(false);
  14896. Add([
  14897. 'type',
  14898. ' TObject = class',
  14899. ' FItems: array of longint;',
  14900. ' function GetItems(Index: longint): longint;',
  14901. ' procedure SetItems(Index: longint; Value: longint);',
  14902. ' procedure DoIt;',
  14903. ' property Items[Index: longint]: longint read getitems write setitems;',
  14904. ' end;',
  14905. 'function tobject.getitems(index: longint): longint;',
  14906. 'begin',
  14907. ' Result:=fitems[index];',
  14908. 'end;',
  14909. 'procedure tobject.setitems(index: longint; value: longint);',
  14910. 'begin',
  14911. ' fitems[index]:=value;',
  14912. 'end;',
  14913. 'procedure tobject.doit;',
  14914. 'begin',
  14915. ' items[1]:=2;',
  14916. ' items[3]:=items[4];',
  14917. ' self.items[5]:=self.items[6];',
  14918. ' items[items[7]]:=items[items[8]];',
  14919. 'end;',
  14920. 'var Obj: tobject;',
  14921. 'begin',
  14922. ' obj.Items[11]:=obj.Items[12];',
  14923. '']);
  14924. ConvertProgram;
  14925. CheckSource('TestClass_Property_Indexed',
  14926. LinesToStr([ // statements
  14927. 'rtl.createClass(this, "TObject", null, function () {',
  14928. ' this.$init = function () {',
  14929. ' this.FItems = [];',
  14930. ' };',
  14931. ' this.$final = function () {',
  14932. ' this.FItems = undefined;',
  14933. ' };',
  14934. ' this.GetItems = function (Index) {',
  14935. ' var Result = 0;',
  14936. ' Result = this.FItems[Index];',
  14937. ' return Result;',
  14938. ' };',
  14939. ' this.SetItems = function (Index, Value) {',
  14940. ' this.FItems[Index] = Value;',
  14941. ' };',
  14942. ' this.DoIt = function () {',
  14943. ' this.SetItems(1, 2);',
  14944. ' this.SetItems(3,this.GetItems(4));',
  14945. ' this.SetItems(5,this.GetItems(6));',
  14946. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14947. ' };',
  14948. '});',
  14949. 'this.Obj = null;'
  14950. ]),
  14951. LinesToStr([ // $mod.$main
  14952. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  14953. ]));
  14954. end;
  14955. procedure TTestModule.TestClass_Property_IndexSpec;
  14956. begin
  14957. StartProgram(false);
  14958. Add([
  14959. 'type',
  14960. ' TEnum = (red, blue);',
  14961. ' TObject = class',
  14962. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  14963. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  14964. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  14965. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  14966. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  14967. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  14968. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  14969. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  14970. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  14971. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  14972. ' end;',
  14973. 'procedure DoIt(b: boolean); begin end;',
  14974. 'var',
  14975. ' o: TObject;',
  14976. 'begin',
  14977. ' o.B1:=o.B1;',
  14978. ' o.B2:=o.B2;',
  14979. ' o.B3:=o.B3;',
  14980. ' o.I1[''a'']:=o.I1[''b''];',
  14981. ' doit(o.b1);',
  14982. ' doit(o.b2);',
  14983. ' doit(o.i1[''c'']);',
  14984. '']);
  14985. ConvertProgram;
  14986. CheckSource('TestClass_Property_IndexSpec',
  14987. LinesToStr([ // statements
  14988. 'this.TEnum = {',
  14989. ' "0": "red",',
  14990. ' red: 0,',
  14991. ' "1": "blue",',
  14992. ' blue: 1',
  14993. '};',
  14994. 'rtl.createClass(this, "TObject", null, function () {',
  14995. ' this.$init = function () {',
  14996. ' };',
  14997. ' this.$final = function () {',
  14998. ' };',
  14999. '});',
  15000. 'this.DoIt = function (b) {',
  15001. '};',
  15002. 'this.o = null;',
  15003. '']),
  15004. LinesToStr([ // $mod.$main
  15005. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  15006. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15007. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15008. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15009. '$mod.DoIt($mod.o.GetIntBool(1));',
  15010. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15011. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15012. '']));
  15013. end;
  15014. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15015. begin
  15016. StartProgram(false);
  15017. Add('type');
  15018. Add(' TArray = array of longint;');
  15019. Add(' TObject = class');
  15020. Add(' FItems: TArray;');
  15021. Add(' function GetItems: tarray;');
  15022. Add(' procedure SetItems(Value: tarray);');
  15023. Add(' property Items: tarray read getitems write setitems;');
  15024. Add(' procedure SetNumbers(const Value: tarray);');
  15025. Add(' property Numbers: tarray write setnumbers;');
  15026. Add(' end;');
  15027. Add('function tobject.getitems: tarray;');
  15028. Add('begin');
  15029. Add(' Result:=fitems;');
  15030. Add('end;');
  15031. Add('procedure tobject.setitems(value: tarray);');
  15032. Add('begin');
  15033. Add(' fitems:=value;');
  15034. Add(' fitems:=nil;');
  15035. Add(' Items:=nil;');
  15036. Add(' Items:=Items;');
  15037. Add(' Items[1]:=2;');
  15038. Add(' fitems[3]:=Items[4];');
  15039. Add(' Items[5]:=Items[6];');
  15040. Add(' Self.Items[7]:=8;');
  15041. Add(' Self.Items[9]:=Self.Items[10];');
  15042. Add(' Items[Items[11]]:=Items[Items[12]];');
  15043. Add('end;');
  15044. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15045. Add('begin;');
  15046. Add(' Numbers:=nil;');
  15047. Add(' Numbers:=Value;');
  15048. Add(' Self.Numbers:=Value;');
  15049. Add('end;');
  15050. Add('var Obj: tobject;');
  15051. Add('begin');
  15052. Add(' obj.items:=nil;');
  15053. Add(' obj.items:=obj.items;');
  15054. Add(' obj.items[11]:=obj.items[12];');
  15055. ConvertProgram;
  15056. CheckSource('TestClass_PropertyOfTypeArray',
  15057. LinesToStr([ // statements
  15058. 'rtl.createClass(this, "TObject", null, function () {',
  15059. ' this.$init = function () {',
  15060. ' this.FItems = [];',
  15061. ' };',
  15062. ' this.$final = function () {',
  15063. ' this.FItems = undefined;',
  15064. ' };',
  15065. ' this.GetItems = function () {',
  15066. ' var Result = [];',
  15067. ' Result = rtl.arrayRef(this.FItems);',
  15068. ' return Result;',
  15069. ' };',
  15070. ' this.SetItems = function (Value) {',
  15071. ' this.FItems = rtl.arrayRef(Value);',
  15072. ' this.FItems = [];',
  15073. ' this.SetItems([]);',
  15074. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15075. ' this.GetItems()[1] = 2;',
  15076. ' this.FItems[3] = this.GetItems()[4];',
  15077. ' this.GetItems()[5] = this.GetItems()[6];',
  15078. ' this.GetItems()[7] = 8;',
  15079. ' this.GetItems()[9] = this.GetItems()[10];',
  15080. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15081. ' };',
  15082. ' this.SetNumbers = function (Value) {',
  15083. ' this.SetNumbers([]);',
  15084. ' this.SetNumbers(Value);',
  15085. ' this.SetNumbers(Value);',
  15086. ' };',
  15087. '});',
  15088. 'this.Obj = null;'
  15089. ]),
  15090. LinesToStr([ // $mod.$main
  15091. '$mod.Obj.SetItems([]);',
  15092. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15093. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15094. ]));
  15095. end;
  15096. procedure TTestModule.TestClass_PropertyDefault;
  15097. begin
  15098. StartProgram(false);
  15099. Add([
  15100. 'type',
  15101. ' TArray = array of longint;',
  15102. ' TObject = class',
  15103. ' end;',
  15104. ' TBird = class',
  15105. ' FItems: TArray;',
  15106. ' function GetItems(Index: longint): longint;',
  15107. ' procedure SetItems(Index, Value: longint);',
  15108. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15109. ' end;',
  15110. 'function TBird.getitems(index: longint): longint;',
  15111. 'begin',
  15112. 'end;',
  15113. 'procedure TBird.setitems(index, value: longint);',
  15114. 'begin',
  15115. ' Self[1]:=2;',
  15116. ' Self[3]:=Self[index];',
  15117. ' Self[index]:=Self[Self[value]];',
  15118. ' Self[Self[4]]:=value;',
  15119. 'end;',
  15120. 'var',
  15121. ' Bird: TBird;',
  15122. ' Obj: TObject;',
  15123. 'begin',
  15124. ' bird[11]:=12;',
  15125. ' bird[13]:=bird[14];',
  15126. ' bird[Bird[15]]:=bird[Bird[15]];',
  15127. ' TBird(obj)[16]:=TBird(obj)[17];',
  15128. ' (obj as tbird)[18]:=19;',
  15129. '']);
  15130. ConvertProgram;
  15131. CheckSource('TestClass_PropertyDefault',
  15132. LinesToStr([ // statements
  15133. 'rtl.createClass(this, "TObject", null, function () {',
  15134. ' this.$init = function () {',
  15135. ' };',
  15136. ' this.$final = function () {',
  15137. ' };',
  15138. '});',
  15139. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15140. ' this.$init = function () {',
  15141. ' $mod.TObject.$init.call(this);',
  15142. ' this.FItems = [];',
  15143. ' };',
  15144. ' this.$final = function () {',
  15145. ' this.FItems = undefined;',
  15146. ' $mod.TObject.$final.call(this);',
  15147. ' };',
  15148. ' this.GetItems = function (Index) {',
  15149. ' var Result = 0;',
  15150. ' return Result;',
  15151. ' };',
  15152. ' this.SetItems = function (Index, Value) {',
  15153. ' this.SetItems(1, 2);',
  15154. ' this.SetItems(3, this.GetItems(Index));',
  15155. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15156. ' this.SetItems(this.GetItems(4), Value);',
  15157. ' };',
  15158. '});',
  15159. 'this.Bird = null;',
  15160. 'this.Obj = null;',
  15161. '']),
  15162. LinesToStr([ // $mod.$main
  15163. '$mod.Bird.SetItems(11, 12);',
  15164. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15165. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15166. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15167. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15168. '']));
  15169. end;
  15170. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15171. begin
  15172. StartProgram(false);
  15173. Add([
  15174. 'type',
  15175. ' TObject = class end;',
  15176. ' TAlphaList = class',
  15177. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15178. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15179. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15180. ' end;',
  15181. ' TBetaList = class',
  15182. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15183. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15184. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15185. ' end;',
  15186. ' TBird = class',
  15187. ' procedure DoIt;',
  15188. ' end;',
  15189. 'procedure TBird.DoIt;',
  15190. 'var',
  15191. ' List: TAlphaList;',
  15192. 'begin',
  15193. ' if TBetaList(List[true])[3]=nil then ;',
  15194. ' TBetaList(List[false])[5]:=nil;',
  15195. 'end;',
  15196. 'var',
  15197. ' List: TAlphaList;',
  15198. 'begin',
  15199. ' if TBetaList(List[true])[3]=nil then ;',
  15200. ' TBetaList(List[false])[5]:=nil;',
  15201. '']);
  15202. ConvertProgram;
  15203. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15204. LinesToStr([ // statements
  15205. 'rtl.createClass(this, "TObject", null, function () {',
  15206. ' this.$init = function () {',
  15207. ' };',
  15208. ' this.$final = function () {',
  15209. ' };',
  15210. '});',
  15211. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15212. '});',
  15213. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15214. '});',
  15215. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15216. ' this.DoIt = function () {',
  15217. ' var List = null;',
  15218. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15219. ' List.GetAlphas(false).SetBetas(5, null);',
  15220. ' };',
  15221. '});',
  15222. 'this.List = null;',
  15223. '']),
  15224. LinesToStr([ // $mod.$main
  15225. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15226. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15227. '']));
  15228. end;
  15229. procedure TTestModule.TestClass_PropertyOverride;
  15230. begin
  15231. StartProgram(false);
  15232. Add('type');
  15233. Add(' integer = longint;');
  15234. Add(' TObject = class');
  15235. Add(' FItem: integer;');
  15236. Add(' function GetItem: integer; external name ''GetItem'';');
  15237. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15238. Add(' property Item: integer read getitem write setitem;');
  15239. Add(' end;');
  15240. Add(' TCar = class');
  15241. Add(' FBag: integer;');
  15242. Add(' function GetBag: integer; external name ''GetBag'';');
  15243. Add(' property Item read getbag;');
  15244. Add(' end;');
  15245. Add('var');
  15246. Add(' Obj: tobject;');
  15247. Add(' Car: tcar;');
  15248. Add('begin');
  15249. Add(' Obj.Item:=Obj.Item;');
  15250. Add(' Car.Item:=Car.Item;');
  15251. ConvertProgram;
  15252. CheckSource('TestClass_PropertyOverride',
  15253. LinesToStr([ // statements
  15254. 'rtl.createClass(this, "TObject", null, function () {',
  15255. ' this.$init = function () {',
  15256. ' this.FItem = 0;',
  15257. ' };',
  15258. ' this.$final = function () {',
  15259. ' };',
  15260. '});',
  15261. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15262. ' this.$init = function () {',
  15263. ' $mod.TObject.$init.call(this);',
  15264. ' this.FBag = 0;',
  15265. ' };',
  15266. '});',
  15267. 'this.Obj = null;',
  15268. 'this.Car = null;',
  15269. '']),
  15270. LinesToStr([ // $mod.$main
  15271. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15272. '$mod.Car.SetItem($mod.Car.GetBag());',
  15273. '']));
  15274. end;
  15275. procedure TTestModule.TestClass_PropertyIncVisibility;
  15276. begin
  15277. AddModuleWithIntfImplSrc('unit1.pp',
  15278. LinesToStr([
  15279. 'type',
  15280. ' TNumber = longint;',
  15281. ' TInteger = longint;',
  15282. ' TObject = class',
  15283. ' private',
  15284. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15285. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15286. ' protected',
  15287. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15288. ' end;']),
  15289. LinesToStr([
  15290. '']));
  15291. StartProgram(true);
  15292. Add([
  15293. 'uses unit1;',
  15294. 'type',
  15295. ' TBird = class',
  15296. ' public',
  15297. ' property Items;',
  15298. ' end;',
  15299. 'procedure DoIt(i: TInteger);',
  15300. 'begin',
  15301. 'end;',
  15302. 'var b: TBird;',
  15303. 'begin',
  15304. ' b.Items[1]:=2;',
  15305. ' b.Items[3]:=b.Items[4];',
  15306. ' DoIt(b.Items[5]);',
  15307. '']);
  15308. ConvertProgram;
  15309. CheckSource('TestClass_PropertyIncVisibility',
  15310. LinesToStr([ // statements
  15311. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15312. '});',
  15313. 'this.DoIt = function (i) {',
  15314. '};',
  15315. 'this.b = null;'
  15316. ]),
  15317. LinesToStr([ // $mod.$main
  15318. '$mod.b.SetItems(1, 2);',
  15319. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15320. '$mod.DoIt($mod.b.GetItems(5));'
  15321. ]));
  15322. end;
  15323. procedure TTestModule.TestClass_Assigned;
  15324. begin
  15325. StartProgram(false);
  15326. Add('type');
  15327. Add(' TObject = class');
  15328. Add(' end;');
  15329. Add('var');
  15330. Add(' Obj: tobject;');
  15331. Add(' b: boolean;');
  15332. Add('begin');
  15333. Add(' if Assigned(obj) then ;');
  15334. Add(' b:=Assigned(obj) or false;');
  15335. ConvertProgram;
  15336. CheckSource('TestClass_Assigned',
  15337. LinesToStr([ // statements
  15338. 'rtl.createClass(this, "TObject", null, function () {',
  15339. ' this.$init = function () {',
  15340. ' };',
  15341. ' this.$final = function () {',
  15342. ' };',
  15343. '});',
  15344. 'this.Obj = null;',
  15345. 'this.b = false;'
  15346. ]),
  15347. LinesToStr([ // $mod.$main
  15348. 'if ($mod.Obj != null);',
  15349. '$mod.b = ($mod.Obj != null) || false;'
  15350. ]));
  15351. end;
  15352. procedure TTestModule.TestClass_WithClassDoCreate;
  15353. begin
  15354. StartProgram(false);
  15355. Add('type');
  15356. Add(' TObject = class');
  15357. Add(' aBool: boolean;');
  15358. Add(' Arr: array of boolean;');
  15359. Add(' constructor Create;');
  15360. Add(' end;');
  15361. Add('constructor TObject.Create; begin end;');
  15362. Add('var');
  15363. Add(' Obj: tobject;');
  15364. Add(' b: boolean;');
  15365. Add('begin');
  15366. Add(' with tobject.create do begin');
  15367. Add(' b:=abool;');
  15368. Add(' abool:=b;');
  15369. Add(' b:=arr[1];');
  15370. Add(' arr[2]:=b;');
  15371. Add(' end;');
  15372. Add(' with tobject do');
  15373. Add(' obj:=create;');
  15374. Add(' with obj do begin');
  15375. Add(' create;');
  15376. Add(' b:=abool;');
  15377. Add(' abool:=b;');
  15378. Add(' b:=arr[3];');
  15379. Add(' arr[4]:=b;');
  15380. Add(' end;');
  15381. ConvertProgram;
  15382. CheckSource('TestClass_WithClassDoCreate',
  15383. LinesToStr([ // statements
  15384. 'rtl.createClass(this, "TObject", null, function () {',
  15385. ' this.$init = function () {',
  15386. ' this.aBool = false;',
  15387. ' this.Arr = [];',
  15388. ' };',
  15389. ' this.$final = function () {',
  15390. ' this.Arr = undefined;',
  15391. ' };',
  15392. ' this.Create = function () {',
  15393. ' return this;',
  15394. ' };',
  15395. '});',
  15396. 'this.Obj = null;',
  15397. 'this.b = false;'
  15398. ]),
  15399. LinesToStr([ // $mod.$main
  15400. 'var $with = $mod.TObject.$create("Create");',
  15401. '$mod.b = $with.aBool;',
  15402. '$with.aBool = $mod.b;',
  15403. '$mod.b = $with.Arr[1];',
  15404. '$with.Arr[2] = $mod.b;',
  15405. 'var $with1 = $mod.TObject;',
  15406. '$mod.Obj = $with1.$create("Create");',
  15407. 'var $with2 = $mod.Obj;',
  15408. '$with2.Create();',
  15409. '$mod.b = $with2.aBool;',
  15410. '$with2.aBool = $mod.b;',
  15411. '$mod.b = $with2.Arr[3];',
  15412. '$with2.Arr[4] = $mod.b;',
  15413. '']));
  15414. end;
  15415. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15416. begin
  15417. StartProgram(false);
  15418. Add('type');
  15419. Add(' TObject = class');
  15420. Add(' FInt: longint;');
  15421. Add(' constructor Create;');
  15422. Add(' function GetSize: longint;');
  15423. Add(' procedure SetSize(Value: longint);');
  15424. Add(' property Int: longint read FInt write FInt;');
  15425. Add(' property Size: longint read GetSize write SetSize;');
  15426. Add(' end;');
  15427. Add('constructor TObject.Create; begin end;');
  15428. Add('function TObject.GetSize: longint; begin; end;');
  15429. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15430. Add('var');
  15431. Add(' Obj: tobject;');
  15432. Add(' i: longint;');
  15433. Add('begin');
  15434. Add(' with TObject.Create do begin');
  15435. Add(' i:=int;');
  15436. Add(' int:=i;');
  15437. Add(' i:=size;');
  15438. Add(' size:=i;');
  15439. Add(' end;');
  15440. Add(' with obj do begin');
  15441. Add(' i:=int;');
  15442. Add(' int:=i;');
  15443. Add(' i:=size;');
  15444. Add(' size:=i;');
  15445. Add(' end;');
  15446. ConvertProgram;
  15447. CheckSource('TestClass_WithClassInstDoProperty',
  15448. LinesToStr([ // statements
  15449. 'rtl.createClass(this, "TObject", null, function () {',
  15450. ' this.$init = function () {',
  15451. ' this.FInt = 0;',
  15452. ' };',
  15453. ' this.$final = function () {',
  15454. ' };',
  15455. ' this.Create = function () {',
  15456. ' return this;',
  15457. ' };',
  15458. ' this.GetSize = function () {',
  15459. ' var Result = 0;',
  15460. ' return Result;',
  15461. ' };',
  15462. ' this.SetSize = function (Value) {',
  15463. ' };',
  15464. '});',
  15465. 'this.Obj = null;',
  15466. 'this.i = 0;'
  15467. ]),
  15468. LinesToStr([ // $mod.$main
  15469. 'var $with = $mod.TObject.$create("Create");',
  15470. '$mod.i = $with.FInt;',
  15471. '$with.FInt = $mod.i;',
  15472. '$mod.i = $with.GetSize();',
  15473. '$with.SetSize($mod.i);',
  15474. 'var $with1 = $mod.Obj;',
  15475. '$mod.i = $with1.FInt;',
  15476. '$with1.FInt = $mod.i;',
  15477. '$mod.i = $with1.GetSize();',
  15478. '$with1.SetSize($mod.i);',
  15479. '']));
  15480. end;
  15481. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15482. begin
  15483. StartProgram(false);
  15484. Add('type');
  15485. Add(' TObject = class');
  15486. Add(' constructor Create;');
  15487. Add(' function GetItems(Index: longint): longint;');
  15488. Add(' procedure SetItems(Index, Value: longint);');
  15489. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15490. Add(' end;');
  15491. Add('constructor TObject.Create; begin end;');
  15492. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15493. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15494. Add('var');
  15495. Add(' Obj: tobject;');
  15496. Add(' i: longint;');
  15497. Add('begin');
  15498. Add(' with TObject.Create do begin');
  15499. Add(' i:=Items[1];');
  15500. Add(' Items[2]:=i;');
  15501. Add(' end;');
  15502. Add(' with obj do begin');
  15503. Add(' i:=Items[3];');
  15504. Add(' Items[4]:=i;');
  15505. Add(' end;');
  15506. ConvertProgram;
  15507. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15508. LinesToStr([ // statements
  15509. 'rtl.createClass(this, "TObject", null, function () {',
  15510. ' this.$init = function () {',
  15511. ' };',
  15512. ' this.$final = function () {',
  15513. ' };',
  15514. ' this.Create = function () {',
  15515. ' return this;',
  15516. ' };',
  15517. ' this.GetItems = function (Index) {',
  15518. ' var Result = 0;',
  15519. ' return Result;',
  15520. ' };',
  15521. ' this.SetItems = function (Index, Value) {',
  15522. ' };',
  15523. '});',
  15524. 'this.Obj = null;',
  15525. 'this.i = 0;'
  15526. ]),
  15527. LinesToStr([ // $mod.$main
  15528. 'var $with = $mod.TObject.$create("Create");',
  15529. '$mod.i = $with.GetItems(1);',
  15530. '$with.SetItems(2, $mod.i);',
  15531. 'var $with1 = $mod.Obj;',
  15532. '$mod.i = $with1.GetItems(3);',
  15533. '$with1.SetItems(4, $mod.i);',
  15534. '']));
  15535. end;
  15536. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15537. begin
  15538. StartProgram(false);
  15539. Add('type');
  15540. Add(' TObject = class');
  15541. Add(' constructor Create;');
  15542. Add(' function GetSize: longint;');
  15543. Add(' procedure SetSize(Value: longint);');
  15544. Add(' end;');
  15545. Add('constructor TObject.Create; begin end;');
  15546. Add('function TObject.GetSize: longint; begin; end;');
  15547. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15548. Add('var');
  15549. Add(' Obj: tobject;');
  15550. Add(' i: longint;');
  15551. Add('begin');
  15552. Add(' with TObject.Create do begin');
  15553. Add(' i:=GetSize;');
  15554. Add(' i:=GetSize();');
  15555. Add(' SetSize(i);');
  15556. Add(' end;');
  15557. Add(' with obj do begin');
  15558. Add(' i:=GetSize;');
  15559. Add(' i:=GetSize();');
  15560. Add(' SetSize(i);');
  15561. Add(' end;');
  15562. ConvertProgram;
  15563. CheckSource('TestClass_WithClassInstDoFunc',
  15564. LinesToStr([ // statements
  15565. 'rtl.createClass(this, "TObject", null, function () {',
  15566. ' this.$init = function () {',
  15567. ' };',
  15568. ' this.$final = function () {',
  15569. ' };',
  15570. ' this.Create = function () {',
  15571. ' return this;',
  15572. ' };',
  15573. ' this.GetSize = function () {',
  15574. ' var Result = 0;',
  15575. ' return Result;',
  15576. ' };',
  15577. ' this.SetSize = function (Value) {',
  15578. ' };',
  15579. '});',
  15580. 'this.Obj = null;',
  15581. 'this.i = 0;'
  15582. ]),
  15583. LinesToStr([ // $mod.$main
  15584. 'var $with = $mod.TObject.$create("Create");',
  15585. '$mod.i = $with.GetSize();',
  15586. '$mod.i = $with.GetSize();',
  15587. '$with.SetSize($mod.i);',
  15588. 'var $with1 = $mod.Obj;',
  15589. '$mod.i = $with1.GetSize();',
  15590. '$mod.i = $with1.GetSize();',
  15591. '$with1.SetSize($mod.i);',
  15592. '']));
  15593. end;
  15594. procedure TTestModule.TestClass_TypeCast;
  15595. begin
  15596. StartProgram(false);
  15597. Add('type');
  15598. Add(' TObject = class');
  15599. Add(' Next: TObject;');
  15600. Add(' constructor Create;');
  15601. Add(' end;');
  15602. Add(' TControl = class(TObject)');
  15603. Add(' Arr: array of TObject;');
  15604. Add(' function GetIt(vI: longint = 0): TObject;');
  15605. Add(' end;');
  15606. Add('constructor tobject.create; begin end;');
  15607. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15608. Add('var');
  15609. Add(' Obj: tobject;');
  15610. Add('begin');
  15611. Add(' obj:=tcontrol(obj).next;');
  15612. Add(' tcontrol(obj):=nil;');
  15613. Add(' obj:=tcontrol(obj);');
  15614. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15615. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15616. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15617. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15618. Add(' obj:=tcontrol(nil);');
  15619. ConvertProgram;
  15620. CheckSource('TestClass_TypeCast',
  15621. LinesToStr([ // statements
  15622. 'rtl.createClass(this, "TObject", null, function () {',
  15623. ' this.$init = function () {',
  15624. ' this.Next = null;',
  15625. ' };',
  15626. ' this.$final = function () {',
  15627. ' this.Next = undefined;',
  15628. ' };',
  15629. ' this.Create = function () {',
  15630. ' return this;',
  15631. ' };',
  15632. '});',
  15633. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15634. ' this.$init = function () {',
  15635. ' $mod.TObject.$init.call(this);',
  15636. ' this.Arr = [];',
  15637. ' };',
  15638. ' this.$final = function () {',
  15639. ' this.Arr = undefined;',
  15640. ' $mod.TObject.$final.call(this);',
  15641. ' };',
  15642. ' this.GetIt = function (vI) {',
  15643. ' var Result = null;',
  15644. ' return Result;',
  15645. ' };',
  15646. '});',
  15647. 'this.Obj = null;'
  15648. ]),
  15649. LinesToStr([ // $mod.$main
  15650. '$mod.Obj = $mod.Obj.Next;',
  15651. '$mod.Obj = null;',
  15652. '$mod.Obj = $mod.Obj;',
  15653. '$mod.Obj = $mod.Obj.GetIt(0);',
  15654. '$mod.Obj = $mod.Obj.GetIt(0);',
  15655. '$mod.Obj = $mod.Obj.GetIt(1);',
  15656. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15657. '$mod.Obj = null;',
  15658. '']));
  15659. end;
  15660. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15661. begin
  15662. StartProgram(false);
  15663. Add('type');
  15664. Add(' TObject = class end;');
  15665. Add('procedure ProcA(var A);');
  15666. Add('begin');
  15667. Add(' TObject(A):=nil;');
  15668. Add(' TObject(A):=TObject(A);');
  15669. Add(' if TObject(A)=nil then ;');
  15670. Add(' if nil=TObject(A) then ;');
  15671. Add('end;');
  15672. Add('procedure ProcB(out A);');
  15673. Add('begin');
  15674. Add(' TObject(A):=nil;');
  15675. Add(' TObject(A):=TObject(A);');
  15676. Add(' if TObject(A)=nil then ;');
  15677. Add(' if nil=TObject(A) then ;');
  15678. Add('end;');
  15679. Add('procedure ProcC(const A);');
  15680. Add('begin');
  15681. Add(' if TObject(A)=nil then ;');
  15682. Add(' if nil=TObject(A) then ;');
  15683. Add('end;');
  15684. Add('var o: TObject;');
  15685. Add('begin');
  15686. Add(' ProcA(o);');
  15687. Add(' ProcB(o);');
  15688. Add(' ProcC(o);');
  15689. ConvertProgram;
  15690. CheckSource('TestClass_TypeCastUntypedParam',
  15691. LinesToStr([ // statements
  15692. 'rtl.createClass(this, "TObject", null, function () {',
  15693. ' this.$init = function () {',
  15694. ' };',
  15695. ' this.$final = function () {',
  15696. ' };',
  15697. '});',
  15698. 'this.ProcA = function (A) {',
  15699. ' A.set(null);',
  15700. ' A.set(A.get());',
  15701. ' if (A.get() === null);',
  15702. ' if (null === A.get());',
  15703. '};',
  15704. 'this.ProcB = function (A) {',
  15705. ' A.set(null);',
  15706. ' A.set(A.get());',
  15707. ' if (A.get() === null);',
  15708. ' if (null === A.get());',
  15709. '};',
  15710. 'this.ProcC = function (A) {',
  15711. ' if (A === null);',
  15712. ' if (null === A);',
  15713. '};',
  15714. 'this.o = null;',
  15715. '']),
  15716. LinesToStr([ // $mod.$main
  15717. '$mod.ProcA({',
  15718. ' p: $mod,',
  15719. ' get: function () {',
  15720. ' return this.p.o;',
  15721. ' },',
  15722. ' set: function (v) {',
  15723. ' this.p.o = v;',
  15724. ' }',
  15725. '});',
  15726. '$mod.ProcB({',
  15727. ' p: $mod,',
  15728. ' get: function () {',
  15729. ' return this.p.o;',
  15730. ' },',
  15731. ' set: function (v) {',
  15732. ' this.p.o = v;',
  15733. ' }',
  15734. '});',
  15735. '$mod.ProcC($mod.o);',
  15736. '']));
  15737. end;
  15738. procedure TTestModule.TestClass_Overloads;
  15739. begin
  15740. StartProgram(false);
  15741. Add('type');
  15742. Add(' TObject = class');
  15743. Add(' procedure DoIt;');
  15744. Add(' procedure DoIt(vI: longint);');
  15745. Add(' end;');
  15746. Add('procedure TObject.DoIt;');
  15747. Add('begin');
  15748. Add(' DoIt;');
  15749. Add(' DoIt(1);');
  15750. Add('end;');
  15751. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15752. Add('begin');
  15753. ConvertProgram;
  15754. CheckSource('TestClass_Overloads',
  15755. LinesToStr([ // statements
  15756. 'rtl.createClass(this, "TObject", null, function () {',
  15757. ' this.$init = function () {',
  15758. ' };',
  15759. ' this.$final = function () {',
  15760. ' };',
  15761. ' this.DoIt = function () {',
  15762. ' this.DoIt();',
  15763. ' this.DoIt$1(1);',
  15764. ' };',
  15765. ' this.DoIt$1 = function (vI) {',
  15766. ' };',
  15767. '});',
  15768. '']),
  15769. LinesToStr([ // $mod.$main
  15770. '']));
  15771. end;
  15772. procedure TTestModule.TestClass_OverloadsAncestor;
  15773. begin
  15774. StartProgram(false);
  15775. Add('type');
  15776. Add(' TObject = class;');
  15777. Add(' TObject = class');
  15778. Add(' procedure DoIt(vA: longint);');
  15779. Add(' procedure DoIt(vA, vB: longint);');
  15780. Add(' end;');
  15781. Add(' TCar = class;');
  15782. Add(' TCar = class');
  15783. Add(' procedure DoIt(vA: longint);');
  15784. Add(' procedure DoIt(vA, vB: longint);');
  15785. Add(' end;');
  15786. Add('procedure tobject.doit(va: longint);');
  15787. Add('begin');
  15788. Add(' doit(1);');
  15789. Add(' doit(1,2);');
  15790. Add('end;');
  15791. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15792. Add('procedure tcar.doit(va: longint);');
  15793. Add('begin');
  15794. Add(' doit(1);');
  15795. Add(' doit(1,2);');
  15796. Add(' inherited doit(1);');
  15797. Add(' inherited doit(1,2);');
  15798. Add('end;');
  15799. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15800. Add('begin');
  15801. ConvertProgram;
  15802. CheckSource('TestClass_OverloadsAncestor',
  15803. LinesToStr([ // statements
  15804. 'rtl.createClass(this, "TObject", null, function () {',
  15805. ' this.$init = function () {',
  15806. ' };',
  15807. ' this.$final = function () {',
  15808. ' };',
  15809. ' this.DoIt = function (vA) {',
  15810. ' this.DoIt(1);',
  15811. ' this.DoIt$1(1,2);',
  15812. ' };',
  15813. ' this.DoIt$1 = function (vA, vB) {',
  15814. ' };',
  15815. '});',
  15816. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15817. ' this.DoIt$2 = function (vA) {',
  15818. ' this.DoIt$2(1);',
  15819. ' this.DoIt$3(1, 2);',
  15820. ' $mod.TObject.DoIt.call(this, 1);',
  15821. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15822. ' };',
  15823. ' this.DoIt$3 = function (vA, vB) {',
  15824. ' };',
  15825. '});',
  15826. '']),
  15827. LinesToStr([ // $mod.$main
  15828. '']));
  15829. end;
  15830. procedure TTestModule.TestClass_OverloadConstructor;
  15831. begin
  15832. StartProgram(false);
  15833. Add('type');
  15834. Add(' TObject = class');
  15835. Add(' constructor Create(vA: longint);');
  15836. Add(' constructor Create(vA, vB: longint);');
  15837. Add(' end;');
  15838. Add(' TCar = class');
  15839. Add(' constructor Create(vA: longint);');
  15840. Add(' constructor Create(vA, vB: longint);');
  15841. Add(' end;');
  15842. Add('constructor tobject.create(va: longint);');
  15843. Add('begin');
  15844. Add(' create(1);');
  15845. Add(' create(1,2);');
  15846. Add('end;');
  15847. Add('constructor tobject.create(va, vb: longint); begin end;');
  15848. Add('constructor tcar.create(va: longint);');
  15849. Add('begin');
  15850. Add(' create(1);');
  15851. Add(' create(1,2);');
  15852. Add(' inherited create(1);');
  15853. Add(' inherited create(1,2);');
  15854. Add('end;');
  15855. Add('constructor tcar.create(va, vb: longint); begin end;');
  15856. Add('begin');
  15857. Add(' tobject.create(1);');
  15858. Add(' tobject.create(1,2);');
  15859. Add(' tcar.create(1);');
  15860. Add(' tcar.create(1,2);');
  15861. ConvertProgram;
  15862. CheckSource('TestClass_OverloadConstructor',
  15863. LinesToStr([ // statements
  15864. 'rtl.createClass(this, "TObject", null, function () {',
  15865. ' this.$init = function () {',
  15866. ' };',
  15867. ' this.$final = function () {',
  15868. ' };',
  15869. ' this.Create = function (vA) {',
  15870. ' this.Create(1);',
  15871. ' this.Create$1(1,2);',
  15872. ' return this;',
  15873. ' };',
  15874. ' this.Create$1 = function (vA, vB) {',
  15875. ' return this;',
  15876. ' };',
  15877. '});',
  15878. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15879. ' this.Create$2 = function (vA) {',
  15880. ' this.Create$2(1);',
  15881. ' this.Create$3(1, 2);',
  15882. ' $mod.TObject.Create.call(this, 1);',
  15883. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15884. ' return this;',
  15885. ' };',
  15886. ' this.Create$3 = function (vA, vB) {',
  15887. ' return this;',
  15888. ' };',
  15889. '});',
  15890. '']),
  15891. LinesToStr([ // $mod.$main
  15892. '$mod.TObject.$create("Create", [1]);',
  15893. '$mod.TObject.$create("Create$1", [1, 2]);',
  15894. '$mod.TCar.$create("Create$2", [1]);',
  15895. '$mod.TCar.$create("Create$3", [1, 2]);',
  15896. '']));
  15897. end;
  15898. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15899. begin
  15900. StartProgram(false);
  15901. Add([
  15902. '{$mode delphi}',
  15903. 'type',
  15904. ' TObject = class end;',
  15905. ' TBird = class',
  15906. ' function {#a}GetValue: longint; overload; virtual;',
  15907. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15908. ' end;',
  15909. ' TEagle = class(TBird)',
  15910. ' function {#c}GetValue: longint; overload; override;',
  15911. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15912. ' end;',
  15913. 'function TBird.GetValue: longint;',
  15914. 'begin',
  15915. ' if 3={@a}GetValue then ;',
  15916. ' if 4={@b}GetValue(5) then ;',
  15917. 'end;',
  15918. 'function TBird.GetValue(AValue: longint): longint;',
  15919. 'begin',
  15920. 'end;',
  15921. 'function TEagle.GetValue: longint;',
  15922. 'begin',
  15923. ' if 13={@c}GetValue then ;',
  15924. ' if 14={@d}GetValue(15) then ;',
  15925. ' if 15=inherited {@a}GetValue then ;',
  15926. ' if 16=inherited {@b}GetValue(17) then ;',
  15927. 'end;',
  15928. 'function TEagle.GetValue(AValue: longint): longint;',
  15929. 'begin',
  15930. 'end;',
  15931. 'var',
  15932. ' e: TEagle;',
  15933. 'begin',
  15934. ' if 23=e.{@c}GetValue then ;',
  15935. ' if 24=e.{@d}GetValue(25) then ;']);
  15936. ConvertProgram;
  15937. CheckSource('TestClass_OverloadDelphiOverride',
  15938. LinesToStr([ // statements
  15939. 'rtl.createClass(this, "TObject", null, function () {',
  15940. ' this.$init = function () {',
  15941. ' };',
  15942. ' this.$final = function () {',
  15943. ' };',
  15944. '});',
  15945. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15946. ' this.GetValue = function () {',
  15947. ' var Result = 0;',
  15948. ' if (3 === this.GetValue()) ;',
  15949. ' if (4 === this.GetValue$1(5)) ;',
  15950. ' return Result;',
  15951. ' };',
  15952. ' this.GetValue$1 = function (AValue) {',
  15953. ' var Result = 0;',
  15954. ' return Result;',
  15955. ' };',
  15956. '});',
  15957. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15958. ' this.GetValue = function () {',
  15959. ' var Result = 0;',
  15960. ' if (13 === this.GetValue()) ;',
  15961. ' if (14 === this.GetValue$1(15)) ;',
  15962. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  15963. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  15964. ' return Result;',
  15965. ' };',
  15966. ' this.GetValue$1 = function (AValue) {',
  15967. ' var Result = 0;',
  15968. ' return Result;',
  15969. ' };',
  15970. '});',
  15971. 'this.e = null;',
  15972. '']),
  15973. LinesToStr([ // $mod.$main
  15974. 'if (23 === $mod.e.GetValue()) ;',
  15975. 'if (24 === $mod.e.GetValue$1(25)) ;',
  15976. '']));
  15977. end;
  15978. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  15979. begin
  15980. StartProgram(false);
  15981. Add([
  15982. '{$mode delphi}',
  15983. 'type',
  15984. ' TObject = class end;',
  15985. ' TAnimal = class',
  15986. ' public',
  15987. ' {#animal_a}A: longint;',
  15988. ' function {#animal_b}B: longint;',
  15989. ' end;',
  15990. ' TBird = class(TAnimal)',
  15991. ' public',
  15992. ' {#bird_a}A: double;',
  15993. ' {#bird_b}B: boolean;',
  15994. ' end;',
  15995. ' TEagle = class(TBird)',
  15996. ' public',
  15997. ' function {#eagle_a}A: boolean;',
  15998. ' {#eagle_b}B: double;',
  15999. ' end;',
  16000. 'function TAnimal.B: longint;',
  16001. 'begin',
  16002. 'end;',
  16003. 'function TEagle.A: boolean;',
  16004. 'begin',
  16005. ' {@eagle_b}B:=3.3;',
  16006. ' {@eagle_a}A();',
  16007. ' TBird(Self).{@bird_b}B:=true;',
  16008. ' TAnimal(Self).{@animal_a}A:=17;',
  16009. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16010. 'end;',
  16011. 'var',
  16012. ' e: TEagle;',
  16013. 'begin',
  16014. ' e.{@eagle_b}B:=5.3;',
  16015. ' if e.{@eagle_a}A then ;',
  16016. '']);
  16017. ConvertProgram;
  16018. CheckSource('TestClass_ReintroduceVarDelphi',
  16019. LinesToStr([ // statements
  16020. 'rtl.createClass(this, "TObject", null, function () {',
  16021. ' this.$init = function () {',
  16022. ' };',
  16023. ' this.$final = function () {',
  16024. ' };',
  16025. '});',
  16026. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16027. ' this.$init = function () {',
  16028. ' $mod.TObject.$init.call(this);',
  16029. ' this.A = 0;',
  16030. ' };',
  16031. ' this.B = function () {',
  16032. ' var Result = 0;',
  16033. ' return Result;',
  16034. ' };',
  16035. '});',
  16036. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16037. ' this.$init = function () {',
  16038. ' $mod.TAnimal.$init.call(this);',
  16039. ' this.A$1 = 0.0;',
  16040. ' this.B$1 = false;',
  16041. ' };',
  16042. '});',
  16043. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16044. ' this.$init = function () {',
  16045. ' $mod.TBird.$init.call(this);',
  16046. ' this.B$2 = 0.0;',
  16047. ' };',
  16048. ' this.A$2 = function () {',
  16049. ' var Result = false;',
  16050. ' this.B$2 = 3.3;',
  16051. ' this.A$2();',
  16052. ' this.B$1 = true;',
  16053. ' this.A = 17;',
  16054. ' this.B$1 = this.A$1 > 1;',
  16055. ' return Result;',
  16056. ' };',
  16057. '});',
  16058. 'this.e = null;',
  16059. '']),
  16060. LinesToStr([ // $mod.$main
  16061. '$mod.e.B$2 = 5.3;',
  16062. 'if ($mod.e.A$2()) ;',
  16063. '']));
  16064. end;
  16065. procedure TTestModule.TestClass_ReintroducedVar;
  16066. begin
  16067. StartProgram(false);
  16068. Add('type');
  16069. Add(' TObject = class');
  16070. Add(' strict private');
  16071. Add(' Some: longint;');
  16072. Add(' end;');
  16073. Add(' TMobile = class');
  16074. Add(' strict private');
  16075. Add(' Some: string;');
  16076. Add(' end;');
  16077. Add(' TCar = class(tmobile)');
  16078. Add(' procedure Some;');
  16079. Add(' procedure Some(vA: longint);');
  16080. Add(' end;');
  16081. Add('procedure tcar.some;');
  16082. Add('begin');
  16083. Add(' Some;');
  16084. Add(' Some(1);');
  16085. Add('end;');
  16086. Add('procedure tcar.some(va: longint); begin end;');
  16087. Add('begin');
  16088. ConvertProgram;
  16089. CheckSource('TestClass_ReintroducedVar',
  16090. LinesToStr([ // statements
  16091. 'rtl.createClass(this, "TObject", null, function () {',
  16092. ' this.$init = function () {',
  16093. ' this.Some = 0;',
  16094. ' };',
  16095. ' this.$final = function () {',
  16096. ' };',
  16097. '});',
  16098. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16099. ' this.$init = function () {',
  16100. ' $mod.TObject.$init.call(this);',
  16101. ' this.Some$1 = "";',
  16102. ' };',
  16103. '});',
  16104. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16105. ' this.Some$2 = function () {',
  16106. ' this.Some$2();',
  16107. ' this.Some$3(1);',
  16108. ' };',
  16109. ' this.Some$3 = function (vA) {',
  16110. ' };',
  16111. '});',
  16112. '']),
  16113. LinesToStr([ // $mod.$main
  16114. '']));
  16115. end;
  16116. procedure TTestModule.TestClass_RaiseDescendant;
  16117. begin
  16118. StartProgram(false);
  16119. Add([
  16120. 'type',
  16121. ' TObject = class',
  16122. ' constructor Create(Msg: string);',
  16123. ' end;',
  16124. ' Exception = class',
  16125. ' end;',
  16126. ' EConvertError = class(Exception)',
  16127. ' end;',
  16128. 'constructor TObject.Create(Msg: string); begin end;',
  16129. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16130. 'begin',
  16131. ' raise Exception.Create(''Bar1'');',
  16132. ' raise EConvertError.Create(''Bar2'');',
  16133. ' raise AssertConv(''Bar2'');',
  16134. ' raise AssertConv;',
  16135. '']);
  16136. ConvertProgram;
  16137. CheckSource('TestClass_RaiseDescendant',
  16138. LinesToStr([ // statements
  16139. 'rtl.createClass(this, "TObject", null, function () {',
  16140. ' this.$init = function () {',
  16141. ' };',
  16142. ' this.$final = function () {',
  16143. ' };',
  16144. ' this.Create = function (Msg) {',
  16145. ' return this;',
  16146. ' };',
  16147. '});',
  16148. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16149. '});',
  16150. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16151. '});',
  16152. 'this.AssertConv = function (Msg) {',
  16153. ' var Result = null;',
  16154. ' return Result;',
  16155. '};',
  16156. '']),
  16157. LinesToStr([ // $mod.$main
  16158. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16159. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16160. 'throw $mod.AssertConv("Bar2");',
  16161. 'throw $mod.AssertConv("def");',
  16162. '']));
  16163. end;
  16164. procedure TTestModule.TestClass_ExternalMethod;
  16165. begin
  16166. AddModuleWithIntfImplSrc('unit2.pas',
  16167. LinesToStr([
  16168. 'type',
  16169. ' TObject = class',
  16170. ' public',
  16171. ' procedure Intern; external name ''$DoIntern'';',
  16172. ' end;',
  16173. '']),
  16174. LinesToStr([
  16175. '']));
  16176. StartUnit(true);
  16177. Add('interface');
  16178. Add('uses unit2;');
  16179. Add('type');
  16180. Add(' TCar = class(TObject)');
  16181. Add(' public');
  16182. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16183. Add(' procedure DoIt;');
  16184. Add(' end;');
  16185. Add('implementation');
  16186. Add('procedure tcar.doit;');
  16187. Add('begin');
  16188. Add(' Intern;');
  16189. Add(' Intern();');
  16190. Add(' Intern2;');
  16191. Add(' Intern2();');
  16192. Add('end;');
  16193. Add('var Obj: TCar;');
  16194. Add('begin');
  16195. Add(' obj.intern;');
  16196. Add(' obj.intern();');
  16197. Add(' obj.intern2;');
  16198. Add(' obj.intern2();');
  16199. Add(' obj.doit;');
  16200. Add(' obj.doit();');
  16201. Add(' with obj do begin');
  16202. Add(' Intern;');
  16203. Add(' Intern();');
  16204. Add(' Intern2;');
  16205. Add(' Intern2();');
  16206. Add(' end;');
  16207. ConvertUnit;
  16208. CheckSource('TestClass_ExternalMethod',
  16209. LinesToStr([
  16210. 'var $impl = $mod.$impl;',
  16211. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16212. ' this.DoIt = function () {',
  16213. ' this.$DoIntern();',
  16214. ' this.$DoIntern();',
  16215. ' this.$DoIntern2();',
  16216. ' this.$DoIntern2();',
  16217. ' };',
  16218. ' });',
  16219. '']),
  16220. LinesToStr([ // this.$init
  16221. '$impl.Obj.$DoIntern();',
  16222. '$impl.Obj.$DoIntern();',
  16223. '$impl.Obj.$DoIntern2();',
  16224. '$impl.Obj.$DoIntern2();',
  16225. '$impl.Obj.DoIt();',
  16226. '$impl.Obj.DoIt();',
  16227. 'var $with = $impl.Obj;',
  16228. '$with.$DoIntern();',
  16229. '$with.$DoIntern();',
  16230. '$with.$DoIntern2();',
  16231. '$with.$DoIntern2();',
  16232. '']),
  16233. LinesToStr([ // implementation
  16234. '$impl.Obj = null;',
  16235. '']) );
  16236. end;
  16237. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16238. begin
  16239. StartProgram(false);
  16240. Add('type');
  16241. Add(' TObject = class');
  16242. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16243. Add(' end;');
  16244. Add('begin');
  16245. SetExpectedPasResolverError('Virtual method name must match external',
  16246. nVirtualMethodNameMustMatchExternal);
  16247. ConvertProgram;
  16248. end;
  16249. procedure TTestModule.TestClass_ExternalOverrideFail;
  16250. begin
  16251. StartProgram(false);
  16252. Add('type');
  16253. Add(' TObject = class');
  16254. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16255. Add(' end;');
  16256. Add(' TCar = class');
  16257. Add(' procedure DoIt; override; external name ''DoIt'';');
  16258. Add(' end;');
  16259. Add('begin');
  16260. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16261. nInvalidXModifierY);
  16262. ConvertProgram;
  16263. end;
  16264. procedure TTestModule.TestClass_ExternalVar;
  16265. begin
  16266. AddModuleWithIntfImplSrc('unit2.pas',
  16267. LinesToStr([
  16268. '{$modeswitch externalclass}',
  16269. 'type',
  16270. ' TObject = class',
  16271. ' public',
  16272. ' Intern: longint external name ''$Intern'';',
  16273. ' Bracket: longint external name ''["A B"]'';',
  16274. ' end;',
  16275. '']),
  16276. LinesToStr([
  16277. '']));
  16278. StartUnit(true);
  16279. Add([
  16280. 'interface',
  16281. 'uses unit2;',
  16282. '{$modeswitch externalclass}',
  16283. 'type',
  16284. ' TCar = class(tobject)',
  16285. ' public',
  16286. ' Intern2: longint external name ''$Intern2'';',
  16287. ' procedure DoIt;',
  16288. ' end;',
  16289. 'implementation',
  16290. 'procedure tcar.doit;',
  16291. 'begin',
  16292. ' Intern:=Intern+1;',
  16293. ' Intern2:=Intern2+2;',
  16294. ' Bracket:=Bracket+3;',
  16295. 'end;',
  16296. 'var Obj: TCar;',
  16297. 'begin',
  16298. ' obj.intern:=obj.intern+1;',
  16299. ' obj.intern2:=obj.intern2+2;',
  16300. ' obj.Bracket:=obj.Bracket+3;',
  16301. ' with obj do begin',
  16302. ' intern:=intern+1;',
  16303. ' intern2:=intern2+2;',
  16304. ' Bracket:=Bracket+3;',
  16305. ' end;']);
  16306. ConvertUnit;
  16307. CheckSource('TestClass_ExternalVar',
  16308. LinesToStr([
  16309. 'var $impl = $mod.$impl;',
  16310. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16311. ' this.DoIt = function () {',
  16312. ' this.$Intern = this.$Intern + 1;',
  16313. ' this.$Intern2 = this.$Intern2 + 2;',
  16314. ' this["A B"] = this["A B"] + 3;',
  16315. ' };',
  16316. ' });',
  16317. '']),
  16318. LinesToStr([
  16319. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16320. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16321. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16322. 'var $with = $impl.Obj;',
  16323. '$with.$Intern = $with.$Intern + 1;',
  16324. '$with.$Intern2 = $with.$Intern2 + 2;',
  16325. '$with["A B"] = $with["A B"] + 3;',
  16326. '']),
  16327. LinesToStr([ // implementation
  16328. '$impl.Obj = null;',
  16329. '']));
  16330. end;
  16331. procedure TTestModule.TestClass_Const;
  16332. begin
  16333. StartProgram(false);
  16334. Add([
  16335. 'type',
  16336. ' integer = longint;',
  16337. ' TClass = class of TObject;',
  16338. ' TObject = class',
  16339. ' public',
  16340. ' const cI: integer = 3;',
  16341. ' procedure DoIt;',
  16342. ' class procedure DoMore;',
  16343. ' end;',
  16344. 'procedure tobject.doit;',
  16345. 'begin',
  16346. ' if cI=4 then;',
  16347. ' if 5=cI then;',
  16348. ' if Self.cI=6 then;',
  16349. ' if 7=Self.cI then;',
  16350. ' with Self do begin',
  16351. ' if cI=11 then;',
  16352. ' if 12=cI then;',
  16353. ' end;',
  16354. 'end;',
  16355. 'class procedure tobject.domore;',
  16356. 'begin',
  16357. ' if cI=8 then;',
  16358. ' if Self.cI=9 then;',
  16359. ' if 10=cI then;',
  16360. ' if 11=Self.cI then;',
  16361. ' with Self do begin',
  16362. ' if cI=13 then;',
  16363. ' if 14=cI then;',
  16364. ' end;',
  16365. 'end;',
  16366. 'var',
  16367. ' Obj: TObject;',
  16368. ' Cla: TClass;',
  16369. 'begin',
  16370. ' if TObject.cI=21 then ;',
  16371. ' if Obj.cI=22 then ;',
  16372. ' if Cla.cI=23 then ;',
  16373. ' with obj do if ci=24 then;',
  16374. ' with TObject do if ci=25 then;',
  16375. ' with Cla do if ci=26 then;']);
  16376. ConvertProgram;
  16377. CheckSource('TestClass_Const',
  16378. LinesToStr([
  16379. 'rtl.createClass(this, "TObject", null, function () {',
  16380. ' this.cI = 3;',
  16381. ' this.$init = function () {',
  16382. ' };',
  16383. ' this.$final = function () {',
  16384. ' };',
  16385. ' this.DoIt = function () {',
  16386. ' if (this.cI === 4) ;',
  16387. ' if (5 === this.cI) ;',
  16388. ' if (this.cI === 6) ;',
  16389. ' if (7 === this.cI) ;',
  16390. ' if (this.cI === 11) ;',
  16391. ' if (12 === this.cI) ;',
  16392. ' };',
  16393. ' this.DoMore = function () {',
  16394. ' if (this.cI === 8) ;',
  16395. ' if (this.cI === 9) ;',
  16396. ' if (10 === this.cI) ;',
  16397. ' if (11 === this.cI) ;',
  16398. ' if (this.cI === 13) ;',
  16399. ' if (14 === this.cI) ;',
  16400. ' };',
  16401. '});',
  16402. 'this.Obj = null;',
  16403. 'this.Cla = null;',
  16404. '']),
  16405. LinesToStr([
  16406. 'if ($mod.TObject.cI === 21) ;',
  16407. 'if ($mod.Obj.cI === 22) ;',
  16408. 'if ($mod.Cla.cI === 23) ;',
  16409. 'var $with = $mod.Obj;',
  16410. 'if ($with.cI === 24) ;',
  16411. 'var $with1 = $mod.TObject;',
  16412. 'if ($with1.cI === 25) ;',
  16413. 'var $with2 = $mod.Cla;',
  16414. 'if ($with2.cI === 26) ;',
  16415. '']));
  16416. end;
  16417. procedure TTestModule.TestClass_ConstEnum;
  16418. begin
  16419. StartProgram(false);
  16420. Add([
  16421. 'type',
  16422. ' TEnum = (red,blue);',
  16423. ' TObject = class',
  16424. ' end;',
  16425. ' TAnimal = class',
  16426. ' public',
  16427. ' type TSubEnum = (light,dark);',
  16428. ' const a = high(TEnum);',
  16429. ' const b = high(TSubEnum);',
  16430. ' end;',
  16431. ' TBird = class(TAnimal)',
  16432. ' public',
  16433. ' const c = high(TEnum);',
  16434. ' const d = high(TSubEnum);',
  16435. ' end;',
  16436. ' TAnt = class',
  16437. ' public',
  16438. ' const e = high(TEnum);',
  16439. ' const f = high(TBird.TSubEnum);',
  16440. ' end;',
  16441. 'begin',
  16442. '']);
  16443. ConvertProgram;
  16444. CheckSource('TestClass_ConstEnum',
  16445. LinesToStr([
  16446. 'this.TEnum = {',
  16447. ' "0": "red",',
  16448. ' red: 0,',
  16449. ' "1": "blue",',
  16450. ' blue: 1',
  16451. '};',
  16452. 'rtl.createClass(this, "TObject", null, function () {',
  16453. ' this.$init = function () {',
  16454. ' };',
  16455. ' this.$final = function () {',
  16456. ' };',
  16457. '});',
  16458. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16459. ' this.TSubEnum = {',
  16460. ' "0": "light",',
  16461. ' light: 0,',
  16462. ' "1": "dark",',
  16463. ' dark: 1',
  16464. ' };',
  16465. ' this.a = $mod.TEnum.blue;',
  16466. ' this.b = this.TSubEnum.dark;',
  16467. '});',
  16468. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16469. ' this.c = $mod.TEnum.blue;',
  16470. ' this.d = this.TSubEnum.dark;',
  16471. '});',
  16472. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16473. ' this.e = $mod.TEnum.blue;',
  16474. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16475. '});',
  16476. '']),
  16477. LinesToStr([
  16478. '']));
  16479. end;
  16480. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16481. begin
  16482. StartProgram(false);
  16483. Add([
  16484. 'type',
  16485. ' TObject = class',
  16486. ' const cI: longint = 3;',
  16487. ' procedure Fly;',
  16488. ' procedure Run;',
  16489. ' end;',
  16490. ' TBird = class',
  16491. ' procedure Go;',
  16492. ' end;',
  16493. 'procedure tobject.fly;',
  16494. 'const cI: word = 4;',
  16495. 'begin',
  16496. ' if cI=Self.cI then ;',
  16497. 'end;',
  16498. 'procedure tobject.run;',
  16499. 'const cI: word = 5;',
  16500. 'begin',
  16501. ' if cI=Self.cI then ;',
  16502. 'end;',
  16503. 'procedure tbird.go;',
  16504. 'const cI: word = 6;',
  16505. 'begin',
  16506. ' if cI=Self.cI then ;',
  16507. 'end;',
  16508. 'begin',
  16509. '']);
  16510. ConvertProgram;
  16511. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16512. LinesToStr([
  16513. 'rtl.createClass(this, "TObject", null, function () {',
  16514. ' this.cI = 3;',
  16515. ' this.$init = function () {',
  16516. ' };',
  16517. ' this.$final = function () {',
  16518. ' };',
  16519. ' var cI$1 = 4;',
  16520. ' this.Fly = function () {',
  16521. ' if (cI$1 === this.cI) ;',
  16522. ' };',
  16523. ' var cI$2 = 5;',
  16524. ' this.Run = function () {',
  16525. ' if (cI$2 === this.cI) ;',
  16526. ' };',
  16527. '});',
  16528. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16529. ' var cI$3 = 6;',
  16530. ' this.Go = function () {',
  16531. ' if (cI$3 === this.cI) ;',
  16532. ' };',
  16533. '});',
  16534. '']),
  16535. LinesToStr([
  16536. '']));
  16537. end;
  16538. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16539. begin
  16540. StartUnit(false);
  16541. Add([
  16542. 'interface',
  16543. 'type',
  16544. ' TObject = class',
  16545. ' const cI: longint = 3;',
  16546. ' procedure Fly;',
  16547. ' procedure Run;',
  16548. ' end;',
  16549. ' TBird = class',
  16550. ' procedure Go;',
  16551. ' end;',
  16552. 'implementation',
  16553. 'procedure tobject.fly;',
  16554. 'const cI: word = 4;',
  16555. 'begin',
  16556. ' if cI=Self.cI then ;',
  16557. 'end;',
  16558. 'procedure tobject.run;',
  16559. 'const cI: word = 5;',
  16560. 'begin',
  16561. ' if cI=Self.cI then ;',
  16562. 'end;',
  16563. 'procedure tbird.go;',
  16564. 'const cI: word = 6;',
  16565. 'begin',
  16566. ' if cI=Self.cI then ;',
  16567. 'end;',
  16568. '']);
  16569. ConvertUnit;
  16570. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16571. LinesToStr([
  16572. 'rtl.createClass(this, "TObject", null, function () {',
  16573. ' this.cI = 3;',
  16574. ' this.$init = function () {',
  16575. ' };',
  16576. ' this.$final = function () {',
  16577. ' };',
  16578. ' var cI$1 = 4;',
  16579. ' this.Fly = function () {',
  16580. ' if (cI$1 === this.cI) ;',
  16581. ' };',
  16582. ' var cI$2 = 5;',
  16583. ' this.Run = function () {',
  16584. ' if (cI$2 === this.cI) ;',
  16585. ' };',
  16586. '});',
  16587. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16588. ' var cI$3 = 6;',
  16589. ' this.Go = function () {',
  16590. ' if (cI$3 === this.cI) ;',
  16591. ' };',
  16592. '});',
  16593. '']),
  16594. '',
  16595. '');
  16596. end;
  16597. procedure TTestModule.TestClass_LocalVarSelfFail;
  16598. begin
  16599. StartProgram(false);
  16600. Add([
  16601. 'type',
  16602. ' TObject = class',
  16603. ' constructor Create;',
  16604. ' end;',
  16605. 'constructor tobject.create;',
  16606. 'var self: longint;',
  16607. 'begin',
  16608. 'end',
  16609. 'begin',
  16610. '']);
  16611. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16612. ConvertProgram;
  16613. end;
  16614. procedure TTestModule.TestClass_ArgSelfFail;
  16615. begin
  16616. StartProgram(false);
  16617. Add([
  16618. 'type',
  16619. ' TObject = class',
  16620. ' procedure DoIt(Self: longint);',
  16621. ' end;',
  16622. 'procedure tobject.doit(self: longint);',
  16623. 'begin',
  16624. 'end',
  16625. 'begin',
  16626. '']);
  16627. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16628. ConvertProgram;
  16629. end;
  16630. procedure TTestModule.TestClass_NestedProcSelf;
  16631. begin
  16632. StartProgram(false);
  16633. Add([
  16634. 'type',
  16635. ' TObject = class',
  16636. ' Key: longint;',
  16637. ' class var State: longint;',
  16638. ' procedure DoIt;',
  16639. ' function GetSize: longint; virtual; abstract;',
  16640. ' procedure SetSize(Value: longint); virtual; abstract;',
  16641. ' property Size: longint read GetSize write SetSize;',
  16642. ' end;',
  16643. 'procedure tobject.doit;',
  16644. ' procedure Sub;',
  16645. ' begin',
  16646. ' key:=key+2;',
  16647. ' self.key:=self.key+3;',
  16648. ' state:=state+4;',
  16649. ' self.state:=self.state+5;',
  16650. ' tobject.state:=tobject.state+6;',
  16651. ' size:=size+7;',
  16652. ' self.size:=self.size+8;',
  16653. ' end;',
  16654. 'begin',
  16655. ' sub;',
  16656. ' key:=key+12;',
  16657. ' self.key:=self.key+13;',
  16658. ' state:=state+14;',
  16659. ' self.state:=self.state+15;',
  16660. ' tobject.state:=tobject.state+16;',
  16661. ' size:=size+17;',
  16662. ' self.size:=self.size+18;',
  16663. 'end;',
  16664. 'begin',
  16665. '']);
  16666. ConvertProgram;
  16667. CheckSource('TestClass_NestedProcSelf',
  16668. LinesToStr([ // statements
  16669. 'rtl.createClass(this, "TObject", null, function () {',
  16670. ' this.State = 0;',
  16671. ' this.$init = function () {',
  16672. ' this.Key = 0;',
  16673. ' };',
  16674. ' this.$final = function () {',
  16675. ' };',
  16676. ' this.DoIt = function () {',
  16677. ' var $Self = this;',
  16678. ' function Sub() {',
  16679. ' $Self.Key = $Self.Key + 2;',
  16680. ' $Self.Key = $Self.Key + 3;',
  16681. ' $mod.TObject.State = $Self.State + 4;',
  16682. ' $mod.TObject.State = $Self.State + 5;',
  16683. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16684. ' $Self.SetSize($Self.GetSize() + 7);',
  16685. ' $Self.SetSize($Self.GetSize() + 8);',
  16686. ' };',
  16687. ' Sub();',
  16688. ' this.Key = this.Key + 12;',
  16689. ' $Self.Key = $Self.Key + 13;',
  16690. ' $mod.TObject.State = this.State + 14;',
  16691. ' $mod.TObject.State = $Self.State + 15;',
  16692. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16693. ' this.SetSize(this.GetSize() + 17);',
  16694. ' $Self.SetSize($Self.GetSize() + 18);',
  16695. ' };',
  16696. '});',
  16697. '']),
  16698. LinesToStr([ // $mod.$main
  16699. '']));
  16700. end;
  16701. procedure TTestModule.TestClass_NestedProcSelf2;
  16702. begin
  16703. StartProgram(false);
  16704. Add([
  16705. 'type',
  16706. ' TObject = class',
  16707. ' Key: longint;',
  16708. ' class var State: longint;',
  16709. ' function GetSize: longint; virtual; abstract;',
  16710. ' procedure SetSize(Value: longint); virtual; abstract;',
  16711. ' property Size: longint read GetSize write SetSize;',
  16712. ' end;',
  16713. ' TBird = class',
  16714. ' procedure DoIt;',
  16715. ' end;',
  16716. 'procedure tbird.doit;',
  16717. ' procedure Sub;',
  16718. ' begin',
  16719. ' key:=key+2;',
  16720. ' self.key:=self.key+3;',
  16721. ' state:=state+4;',
  16722. ' self.state:=self.state+5;',
  16723. ' tobject.state:=tobject.state+6;',
  16724. ' size:=size+7;',
  16725. ' self.size:=self.size+8;',
  16726. ' end;',
  16727. 'begin',
  16728. ' sub;',
  16729. ' key:=key+12;',
  16730. ' self.key:=self.key+13;',
  16731. ' state:=state+14;',
  16732. ' self.state:=self.state+15;',
  16733. ' tobject.state:=tobject.state+16;',
  16734. ' size:=size+17;',
  16735. ' self.size:=self.size+18;',
  16736. 'end;',
  16737. 'begin',
  16738. '']);
  16739. ConvertProgram;
  16740. CheckSource('TestClass_NestedProcSelf2',
  16741. LinesToStr([ // statements
  16742. 'rtl.createClass(this, "TObject", null, function () {',
  16743. ' this.State = 0;',
  16744. ' this.$init = function () {',
  16745. ' this.Key = 0;',
  16746. ' };',
  16747. ' this.$final = function () {',
  16748. ' };',
  16749. '});',
  16750. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16751. ' this.DoIt = function () {',
  16752. ' var $Self = this;',
  16753. ' function Sub() {',
  16754. ' $Self.Key = $Self.Key + 2;',
  16755. ' $Self.Key = $Self.Key + 3;',
  16756. ' $mod.TObject.State = $Self.State + 4;',
  16757. ' $mod.TObject.State = $Self.State + 5;',
  16758. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16759. ' $Self.SetSize($Self.GetSize() + 7);',
  16760. ' $Self.SetSize($Self.GetSize() + 8);',
  16761. ' };',
  16762. ' Sub();',
  16763. ' this.Key = this.Key + 12;',
  16764. ' $Self.Key = $Self.Key + 13;',
  16765. ' $mod.TObject.State = this.State + 14;',
  16766. ' $mod.TObject.State = $Self.State + 15;',
  16767. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16768. ' this.SetSize(this.GetSize() + 17);',
  16769. ' $Self.SetSize($Self.GetSize() + 18);',
  16770. ' };',
  16771. '});',
  16772. '']),
  16773. LinesToStr([ // $mod.$main
  16774. '']));
  16775. end;
  16776. procedure TTestModule.TestClass_NestedProcClassSelf;
  16777. begin
  16778. StartProgram(false);
  16779. Add([
  16780. 'type',
  16781. ' TObject = class',
  16782. ' class var State: longint;',
  16783. ' class procedure DoIt;',
  16784. ' class function GetSize: longint; virtual; abstract;',
  16785. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16786. ' class property Size: longint read GetSize write SetSize;',
  16787. ' end;',
  16788. 'class procedure tobject.doit;',
  16789. ' procedure Sub;',
  16790. ' begin',
  16791. ' state:=state+2;',
  16792. ' self.state:=self.state+3;',
  16793. ' tobject.state:=tobject.state+4;',
  16794. ' size:=size+5;',
  16795. ' self.size:=self.size+6;',
  16796. ' tobject.size:=tobject.size+7;',
  16797. ' end;',
  16798. 'begin',
  16799. ' sub;',
  16800. ' state:=state+12;',
  16801. ' self.state:=self.state+13;',
  16802. ' tobject.state:=tobject.state+14;',
  16803. ' size:=size+15;',
  16804. ' self.size:=self.size+16;',
  16805. ' tobject.size:=tobject.size+17;',
  16806. 'end;',
  16807. 'begin',
  16808. '']);
  16809. ConvertProgram;
  16810. CheckSource('TestClass_NestedProcClassSelf',
  16811. LinesToStr([ // statements
  16812. 'rtl.createClass(this, "TObject", null, function () {',
  16813. ' this.State = 0;',
  16814. ' this.$init = function () {',
  16815. ' };',
  16816. ' this.$final = function () {',
  16817. ' };',
  16818. ' this.DoIt = function () {',
  16819. ' var $Self = this;',
  16820. ' function Sub() {',
  16821. ' $mod.TObject.State = $Self.State + 2;',
  16822. ' $mod.TObject.State = $Self.State + 3;',
  16823. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16824. ' $Self.SetSize($Self.GetSize() + 5);',
  16825. ' $Self.SetSize($Self.GetSize() + 6);',
  16826. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16827. ' };',
  16828. ' Sub();',
  16829. ' $mod.TObject.State = this.State + 12;',
  16830. ' $mod.TObject.State = $Self.State + 13;',
  16831. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16832. ' this.SetSize(this.GetSize() + 15);',
  16833. ' $Self.SetSize($Self.GetSize() + 16);',
  16834. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16835. ' };',
  16836. '});',
  16837. '']),
  16838. LinesToStr([ // $mod.$main
  16839. '']));
  16840. end;
  16841. procedure TTestModule.TestClass_NestedProcCallInherited;
  16842. begin
  16843. StartProgram(false);
  16844. Add([
  16845. 'type',
  16846. ' TObject = class',
  16847. ' function DoIt(k: boolean): longint; virtual;',
  16848. ' end;',
  16849. ' TBird = class',
  16850. ' function DoIt(k: boolean): longint; override;',
  16851. ' end;',
  16852. 'function tobject.doit(k: boolean): longint;',
  16853. 'begin',
  16854. 'end;',
  16855. 'function tbird.doit(k: boolean): longint;',
  16856. ' procedure Sub;',
  16857. ' begin',
  16858. ' inherited DoIt(true);',
  16859. //' if inherited DoIt(false)=4 then ;',
  16860. ' end;',
  16861. 'begin',
  16862. ' Sub;',
  16863. ' inherited;',
  16864. ' inherited DoIt(true);',
  16865. //' if inherited DoIt(false)=14 then ;',
  16866. 'end;',
  16867. 'begin',
  16868. '']);
  16869. ConvertProgram;
  16870. CheckSource('TestClass_NestedProcCallInherited',
  16871. LinesToStr([ // statements
  16872. 'rtl.createClass(this, "TObject", null, function () {',
  16873. ' this.$init = function () {',
  16874. ' };',
  16875. ' this.$final = function () {',
  16876. ' };',
  16877. ' this.DoIt = function (k) {',
  16878. ' var Result = 0;',
  16879. ' return Result;',
  16880. ' };',
  16881. '});',
  16882. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16883. ' this.DoIt = function (k) {',
  16884. ' var $Self = this;',
  16885. ' var Result = 0;',
  16886. ' function Sub() {',
  16887. ' $mod.TObject.DoIt.call($Self, true);',
  16888. ' };',
  16889. ' Sub();',
  16890. ' $mod.TObject.DoIt.apply(this, arguments);',
  16891. ' $mod.TObject.DoIt.call(this, true);',
  16892. ' return Result;',
  16893. ' };',
  16894. '});',
  16895. '']),
  16896. LinesToStr([ // $mod.$main
  16897. '']));
  16898. end;
  16899. procedure TTestModule.TestClass_TObjectFree;
  16900. begin
  16901. StartProgram(false);
  16902. Add([
  16903. 'type',
  16904. ' TObject = class',
  16905. ' Obj: tobject;',
  16906. ' procedure Free;',
  16907. ' procedure Release;',
  16908. ' end;',
  16909. 'procedure tobject.free;',
  16910. 'begin',
  16911. 'end;',
  16912. 'procedure tobject.release;',
  16913. 'begin',
  16914. ' free;',
  16915. ' if true then free;',
  16916. 'end;',
  16917. 'function DoIt(o: tobject): tobject;',
  16918. 'var l: tobject;',
  16919. 'begin',
  16920. ' o.free;',
  16921. ' o.free();',
  16922. ' l.free;',
  16923. ' l.free();',
  16924. ' o.obj.free;',
  16925. ' o.obj.free();',
  16926. ' with o do obj.free;',
  16927. ' with o do obj.free();',
  16928. ' result.Free;',
  16929. ' result.Free();',
  16930. 'end;',
  16931. 'var o: tobject;',
  16932. ' a: array of tobject;',
  16933. 'begin',
  16934. ' o.free;',
  16935. ' o.obj.free;',
  16936. ' a[1+2].free;',
  16937. '']);
  16938. ConvertProgram;
  16939. CheckSource('TestClass_TObjectFree',
  16940. LinesToStr([ // statements
  16941. 'rtl.createClass(this, "TObject", null, function () {',
  16942. ' this.$init = function () {',
  16943. ' this.Obj = null;',
  16944. ' };',
  16945. ' this.$final = function () {',
  16946. ' this.Obj = undefined;',
  16947. ' };',
  16948. ' this.Free = function () {',
  16949. ' };',
  16950. ' this.Release = function () {',
  16951. ' this.Free();',
  16952. ' if (true) this.Free();',
  16953. ' };',
  16954. '});',
  16955. 'this.DoIt = function (o) {',
  16956. ' var Result = null;',
  16957. ' var l = null;',
  16958. ' o = rtl.freeLoc(o);',
  16959. ' o = rtl.freeLoc(o);',
  16960. ' l = rtl.freeLoc(l);',
  16961. ' l = rtl.freeLoc(l);',
  16962. ' rtl.free(o, "Obj");',
  16963. ' rtl.free(o, "Obj");',
  16964. ' rtl.free(o, "Obj");',
  16965. ' rtl.free(o, "Obj");',
  16966. ' Result = rtl.freeLoc(Result);',
  16967. ' Result = rtl.freeLoc(Result);',
  16968. ' return Result;',
  16969. '};',
  16970. 'this.o = null;',
  16971. 'this.a = [];',
  16972. '']),
  16973. LinesToStr([ // $mod.$main
  16974. 'rtl.free($mod, "o");',
  16975. 'rtl.free($mod.o, "Obj");',
  16976. 'rtl.free($mod.a, 1 + 2);',
  16977. '']));
  16978. end;
  16979. procedure TTestModule.TestClass_TObjectFree_VarArg;
  16980. begin
  16981. StartProgram(false);
  16982. Add([
  16983. 'type',
  16984. ' TObject = class',
  16985. ' Obj: tobject;',
  16986. ' procedure Free;',
  16987. ' end;',
  16988. 'procedure tobject.free;',
  16989. 'begin',
  16990. 'end;',
  16991. 'procedure DoIt(var o: tobject);',
  16992. 'begin',
  16993. ' o.free;',
  16994. ' o.free();',
  16995. 'end;',
  16996. 'begin',
  16997. '']);
  16998. ConvertProgram;
  16999. CheckSource('TestClass_TObjectFree_VarArg',
  17000. LinesToStr([ // statements
  17001. 'rtl.createClass(this, "TObject", null, function () {',
  17002. ' this.$init = function () {',
  17003. ' this.Obj = null;',
  17004. ' };',
  17005. ' this.$final = function () {',
  17006. ' this.Obj = undefined;',
  17007. ' };',
  17008. ' this.Free = function () {',
  17009. ' };',
  17010. '});',
  17011. 'this.DoIt = function (o) {',
  17012. ' o.set(rtl.freeLoc(o.get()));',
  17013. ' o.set(rtl.freeLoc(o.get()));',
  17014. '};',
  17015. '']),
  17016. LinesToStr([ // $mod.$main
  17017. '']));
  17018. end;
  17019. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17020. begin
  17021. StartProgram(false);
  17022. Add([
  17023. 'type',
  17024. ' TObject = class',
  17025. ' constructor Create;',
  17026. ' procedure Free;',
  17027. ' end;',
  17028. 'constructor TObject.Create; begin end;',
  17029. 'procedure tobject.free; begin end;',
  17030. 'begin',
  17031. ' with tobject.create do free;',
  17032. '']);
  17033. ConvertProgram;
  17034. CheckSource('TestClass_TObjectFreeNewInstance',
  17035. LinesToStr([ // statements
  17036. 'rtl.createClass(this, "TObject", null, function () {',
  17037. ' this.$init = function () {',
  17038. ' };',
  17039. ' this.$final = function () {',
  17040. ' };',
  17041. ' this.Create = function () {',
  17042. ' return this;',
  17043. ' };',
  17044. ' this.Free = function () {',
  17045. ' };',
  17046. '});',
  17047. '']),
  17048. LinesToStr([ // $mod.$main
  17049. 'var $with = $mod.TObject.$create("Create");',
  17050. '$with=rtl.freeLoc($with);',
  17051. '']));
  17052. end;
  17053. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17054. begin
  17055. StartProgram(false);
  17056. Add([
  17057. 'type',
  17058. ' TObject = class',
  17059. ' destructor Destroy;',
  17060. ' procedure Free;',
  17061. ' end;',
  17062. 'destructor TObject.Destroy; begin end;',
  17063. 'procedure tobject.free; begin end;',
  17064. 'var o: tobject;',
  17065. 'begin',
  17066. ' o.free;',
  17067. '']);
  17068. Converter.UseLowerCase:=true;
  17069. ConvertProgram;
  17070. CheckSource('TestClass_TObjectFreeLowerCase',
  17071. LinesToStr([ // statements
  17072. 'rtl.createClass(this, "tobject", null, function () {',
  17073. ' this.$init = function () {',
  17074. ' };',
  17075. ' this.$final = function () {',
  17076. ' };',
  17077. ' rtl.tObjectDestroy = "destroy";',
  17078. ' this.destroy = function () {',
  17079. ' };',
  17080. ' this.free = function () {',
  17081. ' };',
  17082. '});',
  17083. 'this.o = null;',
  17084. '']),
  17085. LinesToStr([ // $mod.$main
  17086. 'rtl.free($mod, "o");',
  17087. '']));
  17088. end;
  17089. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17090. begin
  17091. StartProgram(false);
  17092. Add([
  17093. 'type',
  17094. ' TObject = class',
  17095. ' procedure Free;',
  17096. ' function GetObj: tobject; virtual; abstract;',
  17097. ' end;',
  17098. 'procedure tobject.free;',
  17099. 'begin',
  17100. 'end;',
  17101. 'var o: tobject;',
  17102. 'begin',
  17103. ' o.getobj.free;',
  17104. '']);
  17105. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17106. ConvertProgram;
  17107. end;
  17108. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17109. begin
  17110. StartProgram(false);
  17111. Add([
  17112. 'type',
  17113. ' TObject = class',
  17114. ' procedure Free;',
  17115. ' FObj: TObject;',
  17116. ' property Obj: tobject read FObj write FObj;',
  17117. ' end;',
  17118. 'procedure tobject.free;',
  17119. 'begin',
  17120. 'end;',
  17121. 'var o: tobject;',
  17122. 'begin',
  17123. ' o.obj.free;',
  17124. '']);
  17125. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17126. ConvertProgram;
  17127. end;
  17128. procedure TTestModule.TestClass_ForIn;
  17129. begin
  17130. StartProgram(false);
  17131. Add([
  17132. 'type',
  17133. ' TObject = class end;',
  17134. ' TItem = TObject;',
  17135. ' TEnumerator = class',
  17136. ' FCurrent: TItem;',
  17137. ' property Current: TItem read FCurrent;',
  17138. ' function MoveNext: boolean;',
  17139. ' end;',
  17140. ' TBird = class',
  17141. ' function GetEnumerator: TEnumerator;',
  17142. ' end;',
  17143. 'function TEnumerator.MoveNext: boolean;',
  17144. 'begin',
  17145. 'end;',
  17146. 'function TBird.GetEnumerator: TEnumerator;',
  17147. 'begin',
  17148. 'end;',
  17149. 'var',
  17150. ' b: TBird;',
  17151. ' i, i2: TItem;',
  17152. 'begin',
  17153. ' for i in b do i2:=i;']);
  17154. ConvertProgram;
  17155. CheckSource('TestClass_ForIn',
  17156. LinesToStr([ // statements
  17157. 'rtl.createClass(this, "TObject", null, function () {',
  17158. ' this.$init = function () {',
  17159. ' };',
  17160. ' this.$final = function () {',
  17161. ' };',
  17162. '});',
  17163. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17164. ' this.$init = function () {',
  17165. ' $mod.TObject.$init.call(this);',
  17166. ' this.FCurrent = null;',
  17167. ' };',
  17168. ' this.$final = function () {',
  17169. ' this.FCurrent = undefined;',
  17170. ' $mod.TObject.$final.call(this);',
  17171. ' };',
  17172. ' this.MoveNext = function () {',
  17173. ' var Result = false;',
  17174. ' return Result;',
  17175. ' };',
  17176. '});',
  17177. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17178. ' this.GetEnumerator = function () {',
  17179. ' var Result = null;',
  17180. ' return Result;',
  17181. ' };',
  17182. '});',
  17183. 'this.b = null;',
  17184. 'this.i = null;',
  17185. 'this.i2 = null;'
  17186. ]),
  17187. LinesToStr([ // $mod.$main
  17188. 'var $in = $mod.b.GetEnumerator();',
  17189. 'try {',
  17190. ' while ($in.MoveNext()){',
  17191. ' $mod.i = $in.FCurrent;',
  17192. ' $mod.i2 = $mod.i;',
  17193. ' }',
  17194. '} finally {',
  17195. ' $in = rtl.freeLoc($in)',
  17196. '};',
  17197. '']));
  17198. end;
  17199. procedure TTestModule.TestClass_DispatchMessage;
  17200. begin
  17201. StartProgram(false);
  17202. Add([
  17203. 'type',
  17204. ' {$DispatchField DispInt}',
  17205. ' {$DispatchStrField DispStr}',
  17206. ' TObject = class',
  17207. ' procedure Dispatch(var Msg); virtual; abstract;',
  17208. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17209. ' end;',
  17210. ' THopMsg = record',
  17211. ' DispInt: longint;',
  17212. ' end;',
  17213. ' TPutMsg = record',
  17214. ' DispStr: string;',
  17215. ' end;',
  17216. ' TBird = class',
  17217. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17218. ' procedure Run; overload; virtual; abstract;',
  17219. ' procedure Run(var Msg); overload; message ''Fast'';',
  17220. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17221. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17222. ' end;',
  17223. 'procedure TBird.Run(var Msg);',
  17224. 'begin',
  17225. 'end;',
  17226. 'begin',
  17227. '']);
  17228. ConvertProgram;
  17229. CheckResolverUnexpectedHints(true);
  17230. CheckSource('TestClass_Message',
  17231. LinesToStr([ // statements
  17232. 'rtl.createClass(this, "TObject", null, function () {',
  17233. ' this.$init = function () {',
  17234. ' };',
  17235. ' this.$final = function () {',
  17236. ' };',
  17237. '});',
  17238. 'rtl.recNewT(this, "THopMsg", function () {',
  17239. ' this.DispInt = 0;',
  17240. ' this.$eq = function (b) {',
  17241. ' return this.DispInt === b.DispInt;',
  17242. ' };',
  17243. ' this.$assign = function (s) {',
  17244. ' this.DispInt = s.DispInt;',
  17245. ' return this;',
  17246. ' };',
  17247. '});',
  17248. 'rtl.recNewT(this, "TPutMsg", function () {',
  17249. ' this.DispStr = "";',
  17250. ' this.$eq = function (b) {',
  17251. ' return this.DispStr === b.DispStr;',
  17252. ' };',
  17253. ' this.$assign = function (s) {',
  17254. ' this.DispStr = s.DispStr;',
  17255. ' return this;',
  17256. ' };',
  17257. '});',
  17258. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17259. ' this.Run$1 = function (Msg) {',
  17260. ' };',
  17261. ' this.$msgint = {',
  17262. ' "2": "Fly",',
  17263. ' "3": "Hop"',
  17264. ' };',
  17265. ' this.$msgstr = {',
  17266. ' Fast: "Run$1",',
  17267. ' foo: "Put"',
  17268. ' };',
  17269. '});',
  17270. '']),
  17271. LinesToStr([ // $mod.$main
  17272. '']));
  17273. end;
  17274. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17275. begin
  17276. StartProgram(false);
  17277. Add([
  17278. 'type',
  17279. ' TObject = class',
  17280. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17281. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17282. ' end;',
  17283. 'begin',
  17284. '']);
  17285. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17286. ConvertProgram;
  17287. end;
  17288. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17289. begin
  17290. StartProgram(false);
  17291. Add([
  17292. 'type',
  17293. ' TObject = class',
  17294. ' procedure Dispatch(var Msg); virtual; abstract;',
  17295. ' end;',
  17296. ' TFlyMsg = record',
  17297. ' FlyId: longint;',
  17298. ' end;',
  17299. ' TBird = class',
  17300. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17301. ' end;',
  17302. 'begin',
  17303. '']);
  17304. ConvertProgram;
  17305. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17306. end;
  17307. procedure TTestModule.TestClassOf_Create;
  17308. begin
  17309. StartProgram(false);
  17310. Add('type');
  17311. Add(' TObject = class');
  17312. Add(' constructor Create;');
  17313. Add(' end;');
  17314. Add(' TClass = class of TObject;');
  17315. Add('constructor tobject.create; begin end;');
  17316. Add('var');
  17317. Add(' Obj: tobject;');
  17318. Add(' C: tclass;');
  17319. Add('begin');
  17320. Add(' obj:=C.create;');
  17321. Add(' with c do obj:=create;');
  17322. ConvertProgram;
  17323. CheckSource('TestClassOf_Create',
  17324. LinesToStr([ // statements
  17325. 'rtl.createClass(this, "TObject", null, function () {',
  17326. ' this.$init = function () {',
  17327. ' };',
  17328. ' this.$final = function () {',
  17329. ' };',
  17330. ' this.Create = function () {',
  17331. ' return this;',
  17332. ' };',
  17333. '});',
  17334. 'this.Obj = null;',
  17335. 'this.C = null;'
  17336. ]),
  17337. LinesToStr([ // $mod.$main
  17338. '$mod.Obj = $mod.C.$create("Create");',
  17339. 'var $with = $mod.C;',
  17340. '$mod.Obj = $with.$create("Create");',
  17341. '']));
  17342. end;
  17343. procedure TTestModule.TestClassOf_Call;
  17344. begin
  17345. StartProgram(false);
  17346. Add('type');
  17347. Add(' TObject = class');
  17348. Add(' class procedure DoIt;');
  17349. Add(' end;');
  17350. Add(' TClass = class of TObject;');
  17351. Add('class procedure tobject.doit; begin end;');
  17352. Add('var');
  17353. Add(' C: tclass;');
  17354. Add('begin');
  17355. Add(' c.doit;');
  17356. Add(' with c do doit;');
  17357. ConvertProgram;
  17358. CheckSource('TestClassOf_Call',
  17359. LinesToStr([ // statements
  17360. 'rtl.createClass(this, "TObject", null, function () {',
  17361. ' this.$init = function () {',
  17362. ' };',
  17363. ' this.$final = function () {',
  17364. ' };',
  17365. ' this.DoIt = function () {',
  17366. ' };',
  17367. '});',
  17368. 'this.C = null;'
  17369. ]),
  17370. LinesToStr([ // $mod.$main
  17371. '$mod.C.DoIt();',
  17372. 'var $with = $mod.C;',
  17373. '$with.DoIt();',
  17374. '']));
  17375. end;
  17376. procedure TTestModule.TestClassOf_Assign;
  17377. begin
  17378. StartProgram(false);
  17379. Add('type');
  17380. Add(' TClass = class of TObject;');
  17381. Add(' TObject = class');
  17382. Add(' ClassType: TClass; ');
  17383. Add(' end;');
  17384. Add('var');
  17385. Add(' Obj: tobject;');
  17386. Add(' C: tclass;');
  17387. Add('begin');
  17388. Add(' c:=nil;');
  17389. Add(' c:=obj.classtype;');
  17390. ConvertProgram;
  17391. CheckSource('TestClassOf_Assign',
  17392. LinesToStr([ // statements
  17393. 'rtl.createClass(this, "TObject", null, function () {',
  17394. ' this.$init = function () {',
  17395. ' this.ClassType = null;',
  17396. ' };',
  17397. ' this.$final = function () {',
  17398. ' this.ClassType = undefined;',
  17399. ' };',
  17400. '});',
  17401. 'this.Obj = null;',
  17402. 'this.C = null;'
  17403. ]),
  17404. LinesToStr([ // $mod.$main
  17405. '$mod.C = null;',
  17406. '$mod.C = $mod.Obj.ClassType;',
  17407. '']));
  17408. end;
  17409. procedure TTestModule.TestClassOf_Is;
  17410. begin
  17411. StartProgram(false);
  17412. Add('type');
  17413. Add(' TClass = class of TObject;');
  17414. Add(' TObject = class');
  17415. Add(' end;');
  17416. Add(' TCar = class');
  17417. Add(' end;');
  17418. Add(' TCars = class of TCar;');
  17419. Add('var');
  17420. Add(' Obj: tobject;');
  17421. Add(' C: tclass;');
  17422. Add(' Cars: tcars;');
  17423. Add('begin');
  17424. Add(' if c is tcar then ;');
  17425. Add(' if c is tcars then ;');
  17426. ConvertProgram;
  17427. CheckSource('TestClassOf_Is',
  17428. LinesToStr([ // statements
  17429. 'rtl.createClass(this, "TObject", null, function () {',
  17430. ' this.$init = function () {',
  17431. ' };',
  17432. ' this.$final = function () {',
  17433. ' };',
  17434. '});',
  17435. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17436. '});',
  17437. 'this.Obj = null;',
  17438. 'this.C = null;',
  17439. 'this.Cars = null;'
  17440. ]),
  17441. LinesToStr([ // $mod.$main
  17442. 'if(rtl.is($mod.C,$mod.TCar));',
  17443. 'if(rtl.is($mod.C,$mod.TCar));',
  17444. '']));
  17445. end;
  17446. procedure TTestModule.TestClassOf_Compare;
  17447. begin
  17448. StartProgram(false);
  17449. Add('type');
  17450. Add(' TClass = class of TObject;');
  17451. Add(' TObject = class');
  17452. Add(' ClassType: TClass; ');
  17453. Add(' end;');
  17454. Add('var');
  17455. Add(' b: boolean;');
  17456. Add(' Obj: tobject;');
  17457. Add(' C: tclass;');
  17458. Add('begin');
  17459. Add(' b:=c=nil;');
  17460. Add(' b:=nil=c;');
  17461. Add(' b:=c=obj.classtype;');
  17462. Add(' b:=obj.classtype=c;');
  17463. Add(' b:=c=TObject;');
  17464. Add(' b:=TObject=c;');
  17465. Add(' b:=c<>nil;');
  17466. Add(' b:=nil<>c;');
  17467. Add(' b:=c<>obj.classtype;');
  17468. Add(' b:=obj.classtype<>c;');
  17469. Add(' b:=c<>TObject;');
  17470. Add(' b:=TObject<>c;');
  17471. ConvertProgram;
  17472. CheckSource('TestClassOf_Compare',
  17473. LinesToStr([ // statements
  17474. 'rtl.createClass(this, "TObject", null, function () {',
  17475. ' this.$init = function () {',
  17476. ' this.ClassType = null;',
  17477. ' };',
  17478. ' this.$final = function () {',
  17479. ' this.ClassType = undefined;',
  17480. ' };',
  17481. '});',
  17482. 'this.b = false;',
  17483. 'this.Obj = null;',
  17484. 'this.C = null;'
  17485. ]),
  17486. LinesToStr([ // $mod.$main
  17487. '$mod.b = $mod.C === null;',
  17488. '$mod.b = null === $mod.C;',
  17489. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17490. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17491. '$mod.b = $mod.C === $mod.TObject;',
  17492. '$mod.b = $mod.TObject === $mod.C;',
  17493. '$mod.b = $mod.C !== null;',
  17494. '$mod.b = null !== $mod.C;',
  17495. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17496. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17497. '$mod.b = $mod.C !== $mod.TObject;',
  17498. '$mod.b = $mod.TObject !== $mod.C;',
  17499. '']));
  17500. end;
  17501. procedure TTestModule.TestClassOf_ClassVar;
  17502. begin
  17503. StartProgram(false);
  17504. Add('type');
  17505. Add(' TObject = class');
  17506. Add(' class var id: longint;');
  17507. Add(' end;');
  17508. Add(' TClass = class of TObject;');
  17509. Add('var');
  17510. Add(' C: tclass;');
  17511. Add('begin');
  17512. Add(' C.id:=C.id;');
  17513. ConvertProgram;
  17514. CheckSource('TestClassOf_ClassVar',
  17515. LinesToStr([ // statements
  17516. 'rtl.createClass(this, "TObject", null, function () {',
  17517. ' this.id = 0;',
  17518. ' this.$init = function () {',
  17519. ' };',
  17520. ' this.$final = function () {',
  17521. ' };',
  17522. '});',
  17523. 'this.C = null;'
  17524. ]),
  17525. LinesToStr([ // $mod.$main
  17526. '$mod.TObject.id = $mod.C.id;',
  17527. '']));
  17528. end;
  17529. procedure TTestModule.TestClassOf_ClassMethod;
  17530. begin
  17531. StartProgram(false);
  17532. Add('type');
  17533. Add(' TObject = class');
  17534. Add(' class function DoIt(i: longint = 0): longint;');
  17535. Add(' end;');
  17536. Add(' TClass = class of TObject;');
  17537. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17538. Add('var');
  17539. Add(' i: longint;');
  17540. Add(' C: tclass;');
  17541. Add('begin');
  17542. Add(' C.DoIt;');
  17543. Add(' C.DoIt();');
  17544. Add(' i:=C.DoIt;');
  17545. Add(' i:=C.DoIt();');
  17546. ConvertProgram;
  17547. CheckSource('TestClassOf_ClassMethod',
  17548. LinesToStr([ // statements
  17549. 'rtl.createClass(this, "TObject", null, function () {',
  17550. ' this.$init = function () {',
  17551. ' };',
  17552. ' this.$final = function () {',
  17553. ' };',
  17554. ' this.DoIt = function (i) {',
  17555. ' var Result = 0;',
  17556. ' return Result;',
  17557. ' };',
  17558. '});',
  17559. 'this.i = 0;',
  17560. 'this.C = null;'
  17561. ]),
  17562. LinesToStr([ // $mod.$main
  17563. '$mod.C.DoIt(0);',
  17564. '$mod.C.DoIt(0);',
  17565. '$mod.i = $mod.C.DoIt(0);',
  17566. '$mod.i = $mod.C.DoIt(0);',
  17567. '']));
  17568. end;
  17569. procedure TTestModule.TestClassOf_ClassProperty;
  17570. begin
  17571. StartProgram(false);
  17572. Add([
  17573. 'type',
  17574. ' TObject = class',
  17575. ' class var FA: longint;',
  17576. ' class function GetA: longint;',
  17577. ' class procedure SetA(Value: longint);',
  17578. ' class property pA: longint read fa write fa;',
  17579. ' class property pB: longint read geta write seta;',
  17580. ' end;',
  17581. ' TObjectClass = class of tobject;',
  17582. 'class function tobject.geta: longint; begin end;',
  17583. 'class procedure tobject.seta(value: longint); begin end;',
  17584. 'var',
  17585. ' b: boolean;',
  17586. ' Obj: tobject;',
  17587. ' Cla: tobjectclass;',
  17588. 'begin',
  17589. ' obj.pa:=obj.pa;',
  17590. ' obj.pb:=obj.pb;',
  17591. ' b:=obj.pa=4;',
  17592. ' b:=obj.pb=obj.pb;',
  17593. ' b:=5=obj.pa;',
  17594. ' cla.pa:=6;',
  17595. ' cla.pa:=cla.pa;',
  17596. ' cla.pb:=cla.pb;',
  17597. ' b:=cla.pa=7;',
  17598. ' b:=cla.pb=cla.pb;',
  17599. ' b:=8=cla.pa;',
  17600. ' tobject.pa:=9;',
  17601. ' tobject.pb:=tobject.pb;',
  17602. ' b:=tobject.pa=10;',
  17603. ' b:=11=tobject.pa;',
  17604. '']);
  17605. ConvertProgram;
  17606. CheckSource('TestClassOf_ClassProperty',
  17607. LinesToStr([ // statements
  17608. 'rtl.createClass(this, "TObject", null, function () {',
  17609. ' this.FA = 0;',
  17610. ' this.$init = function () {',
  17611. ' };',
  17612. ' this.$final = function () {',
  17613. ' };',
  17614. ' this.GetA = function () {',
  17615. ' var Result = 0;',
  17616. ' return Result;',
  17617. ' };',
  17618. ' this.SetA = function (Value) {',
  17619. ' };',
  17620. '});',
  17621. 'this.b = false;',
  17622. 'this.Obj = null;',
  17623. 'this.Cla = null;'
  17624. ]),
  17625. LinesToStr([ // $mod.$main
  17626. '$mod.TObject.FA = $mod.Obj.FA;',
  17627. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17628. '$mod.b = $mod.Obj.FA === 4;',
  17629. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17630. '$mod.b = 5 === $mod.Obj.FA;',
  17631. '$mod.TObject.FA = 6;',
  17632. '$mod.TObject.FA = $mod.Cla.FA;',
  17633. '$mod.Cla.SetA($mod.Cla.GetA());',
  17634. '$mod.b = $mod.Cla.FA === 7;',
  17635. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17636. '$mod.b = 8 === $mod.Cla.FA;',
  17637. '$mod.TObject.FA = 9;',
  17638. '$mod.TObject.SetA($mod.TObject.GetA());',
  17639. '$mod.b = $mod.TObject.FA === 10;',
  17640. '$mod.b = 11 === $mod.TObject.FA;',
  17641. '']));
  17642. end;
  17643. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17644. begin
  17645. StartProgram(false);
  17646. Add('type');
  17647. Add(' TObject = class');
  17648. Add(' class var GlobalId: longint;');
  17649. Add(' class procedure ProcA;');
  17650. Add(' end;');
  17651. Add('class procedure tobject.proca;');
  17652. Add('var b: boolean;');
  17653. Add('begin');
  17654. Add(' b:=self=nil;');
  17655. Add(' b:=self.globalid=3;');
  17656. Add(' b:=4=self.globalid;');
  17657. Add(' self.globalid:=5;');
  17658. Add(' self.proca;');
  17659. Add('end;');
  17660. Add('begin');
  17661. ConvertProgram;
  17662. CheckSource('TestClassOf_ClassMethodSelf',
  17663. LinesToStr([ // statements
  17664. 'rtl.createClass(this, "TObject", null, function () {',
  17665. ' this.GlobalId = 0;',
  17666. ' this.$init = function () {',
  17667. ' };',
  17668. ' this.$final = function () {',
  17669. ' };',
  17670. ' this.ProcA = function () {',
  17671. ' var b = false;',
  17672. ' b = this === null;',
  17673. ' b = this.GlobalId === 3;',
  17674. ' b = 4 === this.GlobalId;',
  17675. ' $mod.TObject.GlobalId = 5;',
  17676. ' this.ProcA();',
  17677. ' };',
  17678. '});'
  17679. ]),
  17680. LinesToStr([ // $mod.$main
  17681. '']));
  17682. end;
  17683. procedure TTestModule.TestClassOf_TypeCast;
  17684. begin
  17685. StartProgram(false);
  17686. Add('type');
  17687. Add(' TObject = class');
  17688. Add(' class procedure {#TObject_DoIt}DoIt;');
  17689. Add(' end;');
  17690. Add(' TClass = class of TObject;');
  17691. Add(' TMobile = class');
  17692. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17693. Add(' end;');
  17694. Add(' TMobileClass = class of TMobile;');
  17695. Add(' TCar = class(TMobile)');
  17696. Add(' class procedure {#TCar_DoIt}DoIt;');
  17697. Add(' end;');
  17698. Add(' TCarClass = class of TCar;');
  17699. Add('class procedure TObject.DoIt;');
  17700. Add('begin');
  17701. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17702. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17703. Add('end;');
  17704. Add('class procedure TMobile.DoIt;');
  17705. Add('begin');
  17706. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17707. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17708. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17709. Add('end;');
  17710. Add('class procedure TCar.DoIt; begin end;');
  17711. Add('var');
  17712. Add(' ObjC: TClass;');
  17713. Add(' MobileC: TMobileClass;');
  17714. Add(' CarC: TCarClass;');
  17715. Add('begin');
  17716. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17717. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17718. Add(' CarC.{@TCar_DoIt}DoIt;');
  17719. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17720. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17721. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17722. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17723. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17724. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17725. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17726. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17727. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17728. ConvertProgram;
  17729. CheckSource('TestClassOf_TypeCast',
  17730. LinesToStr([ // statements
  17731. 'rtl.createClass(this, "TObject", null, function () {',
  17732. ' this.$init = function () {',
  17733. ' };',
  17734. ' this.$final = function () {',
  17735. ' };',
  17736. ' this.DoIt = function () {',
  17737. ' this.DoIt();',
  17738. ' this.DoIt$1();',
  17739. ' };',
  17740. '});',
  17741. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17742. ' this.DoIt$1 = function () {',
  17743. ' this.DoIt();',
  17744. ' this.DoIt$1();',
  17745. ' this.DoIt$2();',
  17746. ' };',
  17747. '});',
  17748. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17749. ' this.DoIt$2 = function () {',
  17750. ' };',
  17751. '});',
  17752. 'this.ObjC = null;',
  17753. 'this.MobileC = null;',
  17754. 'this.CarC = null;',
  17755. '']),
  17756. LinesToStr([ // $mod.$main
  17757. '$mod.ObjC.DoIt();',
  17758. '$mod.MobileC.DoIt$1();',
  17759. '$mod.CarC.DoIt$2();',
  17760. '$mod.ObjC.DoIt();',
  17761. '$mod.ObjC.DoIt$1();',
  17762. '$mod.ObjC.DoIt$2();',
  17763. '$mod.MobileC.DoIt();',
  17764. '$mod.MobileC.DoIt$1();',
  17765. '$mod.MobileC.DoIt$2();',
  17766. '$mod.CarC.DoIt();',
  17767. '$mod.CarC.DoIt$1();',
  17768. '$mod.CarC.DoIt$2();',
  17769. '']));
  17770. end;
  17771. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17772. begin
  17773. StartProgram(false);
  17774. Add('type');
  17775. Add(' TObject = class');
  17776. Add(' function CurNow: longint; ');
  17777. Add(' class function Now: longint; ');
  17778. Add(' end;');
  17779. Add('function TObject.CurNow: longint; begin end;');
  17780. Add('class function TObject.Now: longint; begin end;');
  17781. Add('var');
  17782. Add(' Obj: tobject;');
  17783. Add(' vI: longint;');
  17784. Add('begin');
  17785. Add(' obj.curnow;');
  17786. Add(' vi:=obj.curnow;');
  17787. Add(' tobject.now;');
  17788. Add(' vi:=tobject.now;');
  17789. ConvertProgram;
  17790. CheckSource('TestClassOf_ImplicitFunctionCall',
  17791. LinesToStr([ // statements
  17792. 'rtl.createClass(this, "TObject", null, function () {',
  17793. ' this.$init = function () {',
  17794. ' };',
  17795. ' this.$final = function () {',
  17796. ' };',
  17797. ' this.CurNow = function () {',
  17798. ' var Result = 0;',
  17799. ' return Result;',
  17800. ' };',
  17801. ' this.Now = function () {',
  17802. ' var Result = 0;',
  17803. ' return Result;',
  17804. ' };',
  17805. '});',
  17806. 'this.Obj = null;',
  17807. 'this.vI = 0;',
  17808. '']),
  17809. LinesToStr([ // $mod.$main
  17810. '$mod.Obj.CurNow();',
  17811. '$mod.vI = $mod.Obj.CurNow();',
  17812. '$mod.TObject.Now();',
  17813. '$mod.vI = $mod.TObject.Now();',
  17814. '']));
  17815. end;
  17816. procedure TTestModule.TestClassOf_Const;
  17817. begin
  17818. StartProgram(false);
  17819. Add([
  17820. 'type',
  17821. ' TObject = class',
  17822. ' end;',
  17823. ' TBird = TObject;',
  17824. ' TBirds = class of TBird;',
  17825. ' TEagles = TBirds;',
  17826. ' THawk = class(TBird);',
  17827. 'const',
  17828. ' Hawk: TEagles = THawk;',
  17829. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17830. ' TBird,',
  17831. ' THawk',
  17832. ' );',
  17833. 'begin']);
  17834. ConvertProgram;
  17835. CheckSource('TestClassOf_Const',
  17836. LinesToStr([ // statements
  17837. 'rtl.createClass(this, "TObject", null, function () {',
  17838. ' this.$init = function () {',
  17839. ' };',
  17840. ' this.$final = function () {',
  17841. ' };',
  17842. '});',
  17843. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17844. '});',
  17845. 'this.Hawk = this.THawk;',
  17846. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17847. '']),
  17848. LinesToStr([ // $mod.$main
  17849. '']));
  17850. end;
  17851. procedure TTestModule.TestNestedClass_Alias;
  17852. begin
  17853. WithTypeInfo:=true;
  17854. StartProgram(false);
  17855. Add([
  17856. 'type',
  17857. ' TObject = class',
  17858. ' type TNested = type longint;',
  17859. ' end;',
  17860. 'type TAlias = type tobject.tnested;',
  17861. 'var i: tobject.tnested = 3;',
  17862. 'var j: TAlias = 4;',
  17863. 'begin',
  17864. ' if typeinfo(TAlias)=nil then ;',
  17865. ' if typeinfo(tobject.tnested)=nil then ;',
  17866. '']);
  17867. ConvertProgram;
  17868. CheckSource('TestNestedClass_Alias',
  17869. LinesToStr([ // statements
  17870. 'rtl.createClass(this, "TObject", null, function () {',
  17871. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17872. ' this.$init = function () {',
  17873. ' };',
  17874. ' this.$final = function () {',
  17875. ' };',
  17876. '});',
  17877. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17878. 'this.i = 3;',
  17879. 'this.j = 4;',
  17880. '']),
  17881. LinesToStr([ // $mod.$main
  17882. 'if ($mod.$rtti["TAlias"] === null) ;',
  17883. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17884. '']));
  17885. end;
  17886. procedure TTestModule.TestNestedClass_Record;
  17887. begin
  17888. WithTypeInfo:=true;
  17889. StartProgram(false);
  17890. Add([
  17891. 'type',
  17892. ' TObject = class',
  17893. ' type TPoint = record',
  17894. ' x,y: byte;',
  17895. ' end;',
  17896. ' procedure DoIt(t: TPoint);',
  17897. ' end;',
  17898. 'procedure tobject.DoIt(t: TPoint);',
  17899. 'var p: TPoint;',
  17900. 'begin',
  17901. ' t.x:=t.y;',
  17902. ' p:=t;',
  17903. 'end;',
  17904. 'var',
  17905. ' p: tobject.tpoint = (x:2; y:4);',
  17906. ' o: TObject;',
  17907. 'begin',
  17908. ' p:=p;',
  17909. ' o.doit(p);',
  17910. '']);
  17911. ConvertProgram;
  17912. CheckSource('TestNestedClass_Record',
  17913. LinesToStr([ // statements
  17914. 'rtl.createClass(this, "TObject", null, function () {',
  17915. ' rtl.recNewT(this, "TPoint", function () {',
  17916. ' this.x = 0;',
  17917. ' this.y = 0;',
  17918. ' this.$eq = function (b) {',
  17919. ' return (this.x === b.x) && (this.y === b.y);',
  17920. ' };',
  17921. ' this.$assign = function (s) {',
  17922. ' this.x = s.x;',
  17923. ' this.y = s.y;',
  17924. ' return this;',
  17925. ' };',
  17926. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17927. ' $r.addField("x", rtl.byte);',
  17928. ' $r.addField("y", rtl.byte);',
  17929. ' });',
  17930. ' this.$init = function () {',
  17931. ' };',
  17932. ' this.$final = function () {',
  17933. ' };',
  17934. ' this.DoIt = function (t) {',
  17935. ' var p = this.TPoint.$new();',
  17936. ' t.x = t.y;',
  17937. ' p.$assign(t);',
  17938. ' };',
  17939. '});',
  17940. 'this.p = this.TObject.TPoint.$clone({',
  17941. ' x: 2,',
  17942. ' y: 4',
  17943. '});',
  17944. 'this.o = null;',
  17945. '']),
  17946. LinesToStr([ // $mod.$main
  17947. '$mod.p.$assign($mod.p);',
  17948. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17949. '']));
  17950. end;
  17951. procedure TTestModule.TestNestedClass_Class;
  17952. begin
  17953. StartProgram(false);
  17954. Add([
  17955. 'type',
  17956. ' TObject = class end;',
  17957. ' TBird = class',
  17958. ' type TLeg = class',
  17959. ' FId: longint;',
  17960. ' constructor Create;',
  17961. ' function Create(i: longint): TLeg;',
  17962. ' end;',
  17963. ' function DoIt(b: TBird): Tleg;',
  17964. ' end;',
  17965. 'constructor tbird.tleg.create;',
  17966. 'begin',
  17967. ' FId:=3;',
  17968. 'end;',
  17969. 'function tbird.tleg.Create(i: longint): TLeg;',
  17970. 'begin',
  17971. ' Create;',
  17972. ' Result:=TLeg.Create;',
  17973. ' Result:=TBird.TLeg.Create;',
  17974. ' Result:=Create(3);',
  17975. ' FId:=i;',
  17976. 'end;',
  17977. 'function tbird.DoIt(b: tbird): tleg;',
  17978. 'begin',
  17979. ' Result.Create;',
  17980. ' Result:=TLeg.Create;',
  17981. ' Result:=TBird.TLeg.Create;',
  17982. ' Result:=Result.Create(3);',
  17983. 'end;',
  17984. 'var',
  17985. ' b: Tbird.tleg;',
  17986. 'begin',
  17987. ' b.Create;',
  17988. ' b:=TBird.TLeg.Create;',
  17989. ' b:=b.Create(3);',
  17990. '']);
  17991. ConvertProgram;
  17992. CheckSource('TestNestedClass_Class',
  17993. LinesToStr([ // statements
  17994. 'rtl.createClass(this, "TObject", null, function () {',
  17995. ' this.$init = function () {',
  17996. ' };',
  17997. ' this.$final = function () {',
  17998. ' };',
  17999. '});',
  18000. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18001. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  18002. ' this.$init = function () {',
  18003. ' $mod.TObject.$init.call(this);',
  18004. ' this.FId = 0;',
  18005. ' };',
  18006. ' this.Create = function () {',
  18007. ' this.FId = 3;',
  18008. ' return this;',
  18009. ' };',
  18010. ' this.Create$1 = function (i) {',
  18011. ' var Result = null;',
  18012. ' this.Create();',
  18013. ' Result = $mod.TBird.TLeg.$create("Create");',
  18014. ' Result = $mod.TBird.TLeg.$create("Create");',
  18015. ' Result = this.Create$1(3);',
  18016. ' this.FId = i;',
  18017. ' return Result;',
  18018. ' };',
  18019. ' }, "TBird.TLeg");',
  18020. ' this.DoIt = function (b) {',
  18021. ' var Result = null;',
  18022. ' Result.Create();',
  18023. ' Result = this.TLeg.$create("Create");',
  18024. ' Result = $mod.TBird.TLeg.$create("Create");',
  18025. ' Result = Result.Create$1(3);',
  18026. ' return Result;',
  18027. ' };',
  18028. '});',
  18029. 'this.b = null;',
  18030. '']),
  18031. LinesToStr([ // $mod.$main
  18032. '$mod.b.Create();',
  18033. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18034. '$mod.b = $mod.b.Create$1(3);',
  18035. '']));
  18036. end;
  18037. procedure TTestModule.TestNestedClass_CallInherited;
  18038. begin
  18039. StartProgram(false);
  18040. Add([
  18041. 'type',
  18042. ' TObject = class end;',
  18043. ' TBird = class',
  18044. ' type',
  18045. ' TWing = class',
  18046. ' function Fly(w: word = 17): word; virtual;',
  18047. ' end;',
  18048. ' end;',
  18049. ' TEagle = class(TBird)',
  18050. ' type',
  18051. ' TEagleWing = class(TWing)',
  18052. ' function Fly(w: word): word; override;',
  18053. ' end;',
  18054. ' end;',
  18055. 'function TBird.TWing.Fly(w: word): word;',
  18056. 'begin',
  18057. 'end;',
  18058. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18059. 'begin',
  18060. ' inherited;',
  18061. ' inherited Fly;',
  18062. ' inherited Fly(3);',
  18063. ' Result:=inherited Fly;',
  18064. ' Result:=inherited Fly(4);',
  18065. 'end;',
  18066. 'begin',
  18067. '']);
  18068. ConvertProgram;
  18069. CheckSource('TestNestedClass_CallInherited',
  18070. LinesToStr([ // statements
  18071. 'rtl.createClass(this, "TObject", null, function () {',
  18072. ' this.$init = function () {',
  18073. ' };',
  18074. ' this.$final = function () {',
  18075. ' };',
  18076. '});',
  18077. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18078. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18079. ' this.Fly = function (w) {',
  18080. ' var Result = 0;',
  18081. ' return Result;',
  18082. ' };',
  18083. ' }, "TBird.TWing");',
  18084. '});',
  18085. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18086. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18087. ' this.Fly = function (w) {',
  18088. ' var Result = 0;',
  18089. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18090. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18091. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18092. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18093. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18094. ' return Result;',
  18095. ' };',
  18096. ' }, "TEagle.TEagleWing");',
  18097. '});',
  18098. '']),
  18099. LinesToStr([ // $mod.$main
  18100. '']));
  18101. end;
  18102. procedure TTestModule.TestExternalClass_Var;
  18103. begin
  18104. StartProgram(false);
  18105. Add([
  18106. '{$modeswitch externalclass}',
  18107. 'type',
  18108. ' TExtA = class external name ''ExtObj''',
  18109. ' Id: longint external name ''$Id'';',
  18110. ' B: longint;',
  18111. ' end;',
  18112. 'var Obj: TExtA;',
  18113. 'begin',
  18114. ' obj.id:=obj.id+1;',
  18115. ' obj.B:=obj.B+1;']);
  18116. ConvertProgram;
  18117. CheckSource('TestExternalClass_Var',
  18118. LinesToStr([ // statements
  18119. 'this.Obj = null;',
  18120. '']),
  18121. LinesToStr([ // $mod.$main
  18122. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18123. '$mod.Obj.B = $mod.Obj.B + 1;',
  18124. '']));
  18125. end;
  18126. procedure TTestModule.TestExternalClass_Const;
  18127. begin
  18128. StartProgram(false);
  18129. Add([
  18130. '{$modeswitch externalclass}',
  18131. 'type',
  18132. ' TExtA = class external name ''ExtObj''',
  18133. ' const Two: longint = 2;',
  18134. ' const Three = 3;',
  18135. ' const Id: longint;',
  18136. ' end;',
  18137. ' TExtB = class external name ''ExtB''',
  18138. ' A: TExtA;',
  18139. ' end;',
  18140. 'var',
  18141. ' A: texta;',
  18142. ' B: textb;',
  18143. ' i: longint;',
  18144. 'begin',
  18145. ' i:=a.two;',
  18146. ' i:=texta.two;',
  18147. ' i:=a.three;',
  18148. ' i:=texta.three;',
  18149. ' i:=a.id;',
  18150. ' i:=texta.id;',
  18151. '']);
  18152. ConvertProgram;
  18153. CheckSource('TestExternalClass_Const',
  18154. LinesToStr([ // statements
  18155. 'this.A = null;',
  18156. 'this.B = null;',
  18157. 'this.i = 0;',
  18158. '']),
  18159. LinesToStr([ // $mod.$main
  18160. '$mod.i = 2;',
  18161. '$mod.i = 2;',
  18162. '$mod.i = 3;',
  18163. '$mod.i = 3;',
  18164. '$mod.i = $mod.A.Id;',
  18165. '$mod.i = ExtObj.Id;',
  18166. '']));
  18167. end;
  18168. procedure TTestModule.TestExternalClass_Dollar;
  18169. begin
  18170. StartProgram(false);
  18171. Add([
  18172. '{$modeswitch externalclass}',
  18173. 'type',
  18174. ' TExtA = class external name ''$''',
  18175. ' Id: longint external name ''$'';',
  18176. ' function Bla(i: longint): longint; external name ''$'';',
  18177. ' end;',
  18178. 'function dollar(k: longint): longint; external name ''$'';',
  18179. 'var Obj: TExtA;',
  18180. 'begin',
  18181. ' dollar(1);',
  18182. ' obj.id:=obj.id+2;',
  18183. ' obj.Bla(3);',
  18184. '']);
  18185. ConvertProgram;
  18186. CheckSource('TestExternalClass_Dollar',
  18187. LinesToStr([ // statements
  18188. 'this.Obj = null;',
  18189. '']),
  18190. LinesToStr([ // $mod.$main
  18191. '$(1);',
  18192. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18193. '$mod.Obj.$(3);',
  18194. '']));
  18195. end;
  18196. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18197. begin
  18198. StartProgram(false);
  18199. Add('{$modeswitch externalclass}');
  18200. Add('type');
  18201. Add(' TExtA = class external name ''ExtA''');
  18202. Add(' Id: longint external name ''$Id'';');
  18203. Add(' end;');
  18204. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18205. Add(' Id: longint;');
  18206. Add(' end;');
  18207. Add('begin');
  18208. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18209. ConvertProgram;
  18210. end;
  18211. procedure TTestModule.TestExternalClass_Method;
  18212. begin
  18213. StartProgram(false);
  18214. Add(['{$modeswitch externalclass}',
  18215. 'type',
  18216. ' TExtA = class external name ''ExtObj''',
  18217. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18218. ' procedure DoSome(Id: longint = 1);',
  18219. ' end;',
  18220. 'var Obj: texta;',
  18221. 'begin',
  18222. ' obj.doit;',
  18223. ' obj.doit();',
  18224. ' obj.doit(2);',
  18225. ' with obj do begin',
  18226. ' doit;',
  18227. ' doit();',
  18228. ' doit(3);',
  18229. ' end;']);
  18230. ConvertProgram;
  18231. CheckSource('TestExternalClass_Method',
  18232. LinesToStr([ // statements
  18233. 'this.Obj = null;',
  18234. '']),
  18235. LinesToStr([ // $mod.$main
  18236. '$mod.Obj.$Execute(1);',
  18237. '$mod.Obj.$Execute(1);',
  18238. '$mod.Obj.$Execute(2);',
  18239. 'var $with = $mod.Obj;',
  18240. '$with.$Execute(1);',
  18241. '$with.$Execute(1);',
  18242. '$with.$Execute(3);',
  18243. '']));
  18244. end;
  18245. procedure TTestModule.TestExternalClass_ClassMethod;
  18246. begin
  18247. StartProgram(false);
  18248. Add([
  18249. '{$modeswitch externalclass}',
  18250. 'type',
  18251. ' TExtA = class external name ''ExtObj''',
  18252. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18253. ' end;',
  18254. ' TExtB = TExtA;',
  18255. 'var p: Pointer;',
  18256. 'begin',
  18257. ' texta.doit;',
  18258. ' texta.doit();',
  18259. ' texta.doit(2);',
  18260. ' p:[email protected];',
  18261. ' with texta do begin',
  18262. ' doit;',
  18263. ' doit();',
  18264. ' doit(3);',
  18265. ' p:=@DoIt;',
  18266. ' end;',
  18267. ' textb.doit;',
  18268. ' textb.doit();',
  18269. ' textb.doit(4);',
  18270. ' with textb do begin',
  18271. ' doit;',
  18272. ' doit();',
  18273. ' doit(5);',
  18274. ' end;',
  18275. '']);
  18276. ConvertProgram;
  18277. CheckSource('TestExternalClass_ClassMethod',
  18278. LinesToStr([ // statements
  18279. 'this.p = null;',
  18280. '']),
  18281. LinesToStr([ // $mod.$main
  18282. 'ExtObj.$Execute(1);',
  18283. 'ExtObj.$Execute(1);',
  18284. 'ExtObj.$Execute(2);',
  18285. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18286. 'ExtObj.$Execute(1);',
  18287. 'ExtObj.$Execute(1);',
  18288. 'ExtObj.$Execute(3);',
  18289. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18290. 'ExtObj.$Execute(1);',
  18291. 'ExtObj.$Execute(1);',
  18292. 'ExtObj.$Execute(4);',
  18293. 'ExtObj.$Execute(1);',
  18294. 'ExtObj.$Execute(1);',
  18295. 'ExtObj.$Execute(5);',
  18296. '']));
  18297. end;
  18298. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18299. begin
  18300. StartProgram(false);
  18301. Add([
  18302. '{$modeswitch externalclass}',
  18303. 'type',
  18304. ' TExtA = class external name ''ExtObj''',
  18305. ' class procedure DoIt(Id: longint = 1); static;',
  18306. ' end;',
  18307. 'var p: Pointer;',
  18308. 'begin',
  18309. ' texta.doit;',
  18310. ' texta.doit();',
  18311. ' texta.doit(2);',
  18312. ' p:[email protected];',
  18313. ' with texta do begin',
  18314. ' doit;',
  18315. ' doit();',
  18316. ' doit(3);',
  18317. ' p:=@DoIt;',
  18318. ' end;',
  18319. '']);
  18320. ConvertProgram;
  18321. CheckSource('TestExternalClass_ClassMethodStatic',
  18322. LinesToStr([ // statements
  18323. 'this.p = null;',
  18324. '']),
  18325. LinesToStr([ // $mod.$main
  18326. 'ExtObj.DoIt(1);',
  18327. 'ExtObj.DoIt(1);',
  18328. 'ExtObj.DoIt(2);',
  18329. '$mod.p = ExtObj.DoIt;',
  18330. 'ExtObj.DoIt(1);',
  18331. 'ExtObj.DoIt(1);',
  18332. 'ExtObj.DoIt(3);',
  18333. '$mod.p = ExtObj.DoIt;',
  18334. '']));
  18335. end;
  18336. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18337. begin
  18338. StartProgram(false);
  18339. Add([
  18340. '{$modeswitch externalclass}',
  18341. 'type',
  18342. ' TBird = class external name ''Array''',
  18343. ' end;',
  18344. 'function GetPtr: Pointer;',
  18345. 'begin',
  18346. 'end;',
  18347. 'procedure Write(const p);',
  18348. 'begin',
  18349. 'end;',
  18350. 'procedure WriteLn; varargs;',
  18351. 'begin',
  18352. 'end;',
  18353. 'begin',
  18354. ' if TBird(GetPtr)=nil then ;',
  18355. ' Write(GetPtr);',
  18356. ' WriteLn(GetPtr);',
  18357. ' Write(TBird(GetPtr));',
  18358. ' WriteLn(TBird(GetPtr));',
  18359. '']);
  18360. ConvertProgram;
  18361. CheckSource('TestFunctionResultInTypeCast',
  18362. LinesToStr([ // statements
  18363. 'this.GetPtr = function () {',
  18364. ' var Result = null;',
  18365. ' return Result;',
  18366. '};',
  18367. 'this.Write = function (p) {',
  18368. '};',
  18369. 'this.WriteLn = function () {',
  18370. '};',
  18371. '']),
  18372. LinesToStr([
  18373. 'if ($mod.GetPtr() === null) ;',
  18374. '$mod.Write($mod.GetPtr());',
  18375. '$mod.WriteLn($mod.GetPtr());',
  18376. '$mod.Write($mod.GetPtr());',
  18377. '$mod.WriteLn($mod.GetPtr());',
  18378. '']));
  18379. end;
  18380. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18381. begin
  18382. StartProgram(false);
  18383. Add([
  18384. '{$modeswitch externalclass}',
  18385. 'type',
  18386. ' TExtA = class external name ''ExtObjA''',
  18387. ' procedure ProcA; virtual;',
  18388. ' procedure ProcB; virtual;',
  18389. ' end;',
  18390. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18391. ' end;',
  18392. ' TExtC = class (TExtB)',
  18393. ' procedure ProcA; override;',
  18394. ' end;',
  18395. 'procedure TExtC.ProcA;',
  18396. 'begin',
  18397. ' ProcA;',
  18398. ' Self.ProcA;',
  18399. ' ProcB;',
  18400. ' Self.ProcB;',
  18401. 'end;',
  18402. 'var',
  18403. ' A: texta;',
  18404. ' B: textb;',
  18405. ' C: textc;',
  18406. 'begin',
  18407. ' a.proca;',
  18408. ' b.proca;',
  18409. ' c.proca;']);
  18410. ConvertProgram;
  18411. CheckSource('TestExternalClass_NonExternalOverride',
  18412. LinesToStr([ // statements
  18413. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18414. ' this.$init = function () {',
  18415. ' };',
  18416. ' this.$final = function () {',
  18417. ' };',
  18418. ' this.ProcA = function () {',
  18419. ' this.ProcA();',
  18420. ' this.ProcA();',
  18421. ' this.ProcB();',
  18422. ' this.ProcB();',
  18423. ' };',
  18424. '});',
  18425. 'this.A = null;',
  18426. 'this.B = null;',
  18427. 'this.C = null;',
  18428. '']),
  18429. LinesToStr([ // $mod.$main
  18430. '$mod.A.ProcA();',
  18431. '$mod.B.ProcA();',
  18432. '$mod.C.ProcA();',
  18433. '']));
  18434. end;
  18435. procedure TTestModule.TestExternalClass_OverloadHint;
  18436. begin
  18437. StartProgram(false);
  18438. Add([
  18439. '{$modeswitch externalclass}',
  18440. 'type',
  18441. ' TExtA = class external name ''ExtObjA''',
  18442. ' procedure DoIt;',
  18443. ' procedure DoIt(i: longint);',
  18444. ' end;',
  18445. 'begin',
  18446. '']);
  18447. ConvertProgram;
  18448. CheckResolverUnexpectedHints(true);
  18449. CheckSource('TestExternalClass_OverloadHint',
  18450. LinesToStr([ // statements
  18451. '']),
  18452. LinesToStr([ // $mod.$main
  18453. '']));
  18454. end;
  18455. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18456. begin
  18457. WithTypeInfo:=true;
  18458. StartProgram(false);
  18459. Add([
  18460. '{$modeswitch externalclass}',
  18461. 'type',
  18462. ' JSwiper = class external name ''Swiper''',
  18463. ' constructor New;',
  18464. ' end;',
  18465. ' TObject = class',
  18466. ' private',
  18467. ' FSwiper: JSwiper;',
  18468. ' published',
  18469. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18470. ' end;',
  18471. 'begin',
  18472. ' JSwiper.new;',
  18473. '']);
  18474. ConvertProgram;
  18475. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18476. LinesToStr([ // statements
  18477. 'this.$rtti.$ExtClass("JSwiper", {',
  18478. ' jsclass: "Swiper"',
  18479. '});',
  18480. 'rtl.createClass(this, "TObject", null, function () {',
  18481. ' this.$init = function () {',
  18482. ' this.FSwiper = null;',
  18483. ' };',
  18484. ' this.$final = function () {',
  18485. ' this.FSwiper = undefined;',
  18486. ' };',
  18487. ' var $r = this.$rtti;',
  18488. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18489. '});',
  18490. '']),
  18491. LinesToStr([ // $mod.$main
  18492. 'new Swiper();',
  18493. '']));
  18494. end;
  18495. procedure TTestModule.TestExternalClass_Property;
  18496. begin
  18497. StartProgram(false);
  18498. Add([
  18499. '{$modeswitch externalclass}',
  18500. 'type',
  18501. ' TExtA = class external name ''ExtA''',
  18502. ' function getYear: longint;',
  18503. ' procedure setYear(Value: longint);',
  18504. ' property Year: longint read getyear write setyear;',
  18505. ' end;',
  18506. ' TExtB = class (TExtA)',
  18507. ' procedure OtherSetYear(Value: longint);',
  18508. ' property year write othersetyear;',
  18509. ' end;',
  18510. 'procedure textb.othersetyear(value: longint);',
  18511. 'begin',
  18512. ' setYear(Value+4);',
  18513. 'end;',
  18514. 'var',
  18515. ' A: texta;',
  18516. ' B: textb;',
  18517. 'begin',
  18518. ' a.year:=a.year+1;',
  18519. ' b.year:=b.year+2;']);
  18520. ConvertProgram;
  18521. CheckSource('TestExternalClass_NonExternalOverride',
  18522. LinesToStr([ // statements
  18523. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18524. ' this.$init = function () {',
  18525. ' };',
  18526. ' this.$final = function () {',
  18527. ' };',
  18528. ' this.OtherSetYear = function (Value) {',
  18529. ' this.setYear(Value+4);',
  18530. ' };',
  18531. '});',
  18532. 'this.A = null;',
  18533. 'this.B = null;',
  18534. '']),
  18535. LinesToStr([ // $mod.$main
  18536. '$mod.A.setYear($mod.A.getYear()+1);',
  18537. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18538. '']));
  18539. end;
  18540. procedure TTestModule.TestExternalClass_PropertyDate;
  18541. begin
  18542. StartProgram(false);
  18543. Add([
  18544. '{$modeswitch externalclass}',
  18545. 'type',
  18546. ' TExtA = class external name ''ExtA''',
  18547. ' end;',
  18548. ' TExtB = class (TExtA)',
  18549. ' FDate: string;',
  18550. ' property Date: string read FDate write FDate;',
  18551. ' property ExtA: string read FDate write FDate;',
  18552. ' end;',
  18553. ' {$M+}',
  18554. ' TObject = class',
  18555. ' FDate: string;',
  18556. ' published',
  18557. ' property Date: string read FDate write FDate;',
  18558. ' property ExtA: string read FDate write FDate;',
  18559. ' end;',
  18560. 'var',
  18561. ' B: textb;',
  18562. ' o: TObject;',
  18563. 'begin',
  18564. ' b.date:=b.exta;',
  18565. ' o.date:=o.exta;']);
  18566. ConvertProgram;
  18567. CheckSource('TestExternalClass_PropertyDate',
  18568. LinesToStr([ // statements
  18569. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18570. ' this.$init = function () {',
  18571. ' this.FDate = "";',
  18572. ' };',
  18573. ' this.$final = function () {',
  18574. ' };',
  18575. '});',
  18576. 'rtl.createClass(this, "TObject", null, function () {',
  18577. ' this.$init = function () {',
  18578. ' this.FDate = "";',
  18579. ' };',
  18580. ' this.$final = function () {',
  18581. ' };',
  18582. ' var $r = this.$rtti;',
  18583. ' $r.addField("FDate", rtl.string);',
  18584. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18585. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18586. '});',
  18587. 'this.B = null;',
  18588. 'this.o = null;',
  18589. '']),
  18590. LinesToStr([ // $mod.$main
  18591. '$mod.B.FDate = $mod.B.FDate;',
  18592. '$mod.o.FDate = $mod.o.FDate;',
  18593. '']));
  18594. end;
  18595. procedure TTestModule.TestExternalClass_ClassProperty;
  18596. begin
  18597. StartProgram(false);
  18598. Add('{$modeswitch externalclass}');
  18599. Add('type');
  18600. Add(' TExtA = class external name ''ExtA''');
  18601. Add(' class function getYear: longint;');
  18602. Add(' class procedure setYear(Value: longint);');
  18603. Add(' class property Year: longint read getyear write setyear;');
  18604. Add(' end;');
  18605. Add(' TExtB = class (TExtA)');
  18606. Add(' class function GetCentury: longint;');
  18607. Add(' class procedure SetCentury(Value: longint);');
  18608. Add(' class property Century: longint read getcentury write setcentury;');
  18609. Add(' end;');
  18610. Add('class function textb.getcentury: longint;');
  18611. Add('begin');
  18612. Add('end;');
  18613. Add('class procedure textb.setcentury(value: longint);');
  18614. Add('begin');
  18615. Add(' setyear(value+11);');
  18616. Add(' texta.year:=texta.year+12;');
  18617. Add(' year:=year+13;');
  18618. Add(' textb.century:=textb.century+14;');
  18619. Add(' century:=century+15;');
  18620. Add('end;');
  18621. Add('var');
  18622. Add(' A: texta;');
  18623. Add(' B: textb;');
  18624. Add('begin');
  18625. Add(' texta.year:=texta.year+1;');
  18626. Add(' textb.year:=textb.year+2;');
  18627. Add(' TextA.year:=TextA.year+3;');
  18628. Add(' b.year:=b.year+4;');
  18629. Add(' textb.century:=textb.century+5;');
  18630. Add(' b.century:=b.century+6;');
  18631. ConvertProgram;
  18632. CheckSource('TestExternalClass_ClassProperty',
  18633. LinesToStr([ // statements
  18634. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18635. ' this.$init = function () {',
  18636. ' };',
  18637. ' this.$final = function () {',
  18638. ' };',
  18639. ' this.GetCentury = function () {',
  18640. ' var Result = 0;',
  18641. ' return Result;',
  18642. ' };',
  18643. ' this.SetCentury = function (Value) {',
  18644. ' this.setYear(Value + 11);',
  18645. ' ExtA.setYear(ExtA.getYear() + 12);',
  18646. ' this.setYear(this.getYear() + 13);',
  18647. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18648. ' this.SetCentury(this.GetCentury() + 15);',
  18649. ' };',
  18650. '});',
  18651. 'this.A = null;',
  18652. 'this.B = null;',
  18653. '']),
  18654. LinesToStr([ // $mod.$main
  18655. 'ExtA.setYear(ExtA.getYear() + 1);',
  18656. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18657. 'ExtA.setYear(ExtA.getYear() + 3);',
  18658. '$mod.B.setYear($mod.B.getYear() + 4);',
  18659. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18660. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18661. '']));
  18662. end;
  18663. procedure TTestModule.TestExternalClass_ClassOf;
  18664. begin
  18665. StartProgram(false);
  18666. Add('{$modeswitch externalclass}');
  18667. Add('type');
  18668. Add(' TExtA = class external name ''ExtA''');
  18669. Add(' procedure ProcA; virtual;');
  18670. Add(' procedure ProcB; virtual;');
  18671. Add(' end;');
  18672. Add(' TExtAClass = class of TExtA;');
  18673. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18674. Add(' end;');
  18675. Add(' TExtBClass = class of TExtB;');
  18676. Add(' TExtC = class (TExtB)');
  18677. Add(' procedure ProcA; override;');
  18678. Add(' end;');
  18679. Add(' TExtCClass = class of TExtC;');
  18680. Add('procedure TExtC.ProcA; begin end;');
  18681. Add('var');
  18682. Add(' A: texta; ClA: TExtAClass;');
  18683. Add(' B: textb; ClB: TExtBClass;');
  18684. Add(' C: textc; ClC: TExtCClass;');
  18685. Add('begin');
  18686. Add(' ClA:=texta;');
  18687. Add(' ClA:=textb;');
  18688. Add(' ClA:=textc;');
  18689. Add(' ClB:=textb;');
  18690. Add(' ClB:=textc;');
  18691. Add(' ClC:=textc;');
  18692. ConvertProgram;
  18693. CheckSource('TestExternalClass_ClassOf',
  18694. LinesToStr([ // statements
  18695. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18696. ' this.$init = function () {',
  18697. ' };',
  18698. ' this.$final = function () {',
  18699. ' };',
  18700. ' this.ProcA = function () {',
  18701. ' };',
  18702. '});',
  18703. 'this.A = null;',
  18704. 'this.ClA = null;',
  18705. 'this.B = null;',
  18706. 'this.ClB = null;',
  18707. 'this.C = null;',
  18708. 'this.ClC = null;',
  18709. '']),
  18710. LinesToStr([ // $mod.$main
  18711. '$mod.ClA = ExtA;',
  18712. '$mod.ClA = ExtB;',
  18713. '$mod.ClA = $mod.TExtC;',
  18714. '$mod.ClB = ExtB;',
  18715. '$mod.ClB = $mod.TExtC;',
  18716. '$mod.ClC = $mod.TExtC;',
  18717. '']));
  18718. end;
  18719. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18720. begin
  18721. AddModuleWithIntfImplSrc('unit2.pas',
  18722. LinesToStr([
  18723. '{$modeswitch externalclass}',
  18724. 'type',
  18725. ' TExtA = class external name ''ExtA''',
  18726. ' class var Id: longint;',
  18727. ' end;',
  18728. '']),
  18729. '');
  18730. StartUnit(true);
  18731. Add('interface');
  18732. Add('uses unit2;');
  18733. Add('implementation');
  18734. Add('begin');
  18735. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18736. ConvertUnit;
  18737. CheckSource('TestExternalClass_ClassOtherUnit',
  18738. LinesToStr([
  18739. '']),
  18740. LinesToStr([
  18741. 'ExtA.Id = ExtA.Id + 1;',
  18742. '']));
  18743. end;
  18744. procedure TTestModule.TestExternalClass_Is;
  18745. begin
  18746. StartProgram(false);
  18747. Add([
  18748. '{$modeswitch externalclass}',
  18749. 'type',
  18750. ' TExtA = class external name ''ExtA''',
  18751. ' end;',
  18752. ' TExtAClass = class of TExtA;',
  18753. ' TExtB = class external name ''ExtB'' (TExtA)',
  18754. ' end;',
  18755. ' TExtBClass = class of TExtB;',
  18756. ' TExtC = class (TExtB)',
  18757. ' end;',
  18758. ' TExtCClass = class of TExtC;',
  18759. 'var',
  18760. ' A: texta; ClA: TExtAClass;',
  18761. ' B: textb; ClB: TExtBClass;',
  18762. ' C: textc; ClC: TExtCClass;',
  18763. 'begin',
  18764. ' if a is textb then ;',
  18765. ' if a is textc then ;',
  18766. ' if b is textc then ;',
  18767. ' if cla is textb then ;',
  18768. ' if cla is textc then ;',
  18769. ' if clb is textc then ;',
  18770. ' try',
  18771. ' except',
  18772. ' on TExtA do ;',
  18773. ' on e: TExtB do ;',
  18774. ' end;',
  18775. '']);
  18776. ConvertProgram;
  18777. CheckSource('TestExternalClass_Is',
  18778. LinesToStr([ // statements
  18779. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18780. ' this.$init = function () {',
  18781. ' };',
  18782. ' this.$final = function () {',
  18783. ' };',
  18784. '});',
  18785. 'this.A = null;',
  18786. 'this.ClA = null;',
  18787. 'this.B = null;',
  18788. 'this.ClB = null;',
  18789. 'this.C = null;',
  18790. 'this.ClC = null;',
  18791. '']),
  18792. LinesToStr([ // $mod.$main
  18793. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18794. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18795. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18796. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18797. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18798. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18799. 'try {} catch ($e) {',
  18800. ' if (rtl.isExt($e,ExtA)) {}',
  18801. ' else if (rtl.isExt($e,ExtB)) {',
  18802. ' var e = $e;',
  18803. ' } else throw $e',
  18804. '};',
  18805. '']));
  18806. end;
  18807. procedure TTestModule.TestExternalClass_As;
  18808. begin
  18809. StartProgram(false);
  18810. Add('{$modeswitch externalclass}');
  18811. Add('type');
  18812. Add(' TExtA = class external name ''ExtA''');
  18813. Add(' end;');
  18814. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18815. Add(' end;');
  18816. Add(' TExtC = class (TExtB)');
  18817. Add(' end;');
  18818. Add('var');
  18819. Add(' A: texta;');
  18820. Add(' B: textb;');
  18821. Add(' C: textc;');
  18822. Add('begin');
  18823. Add(' b:=a as textb;');
  18824. Add(' c:=a as textc;');
  18825. Add(' c:=b as textc;');
  18826. ConvertProgram;
  18827. CheckSource('TestExternalClass_Is',
  18828. LinesToStr([ // statements
  18829. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18830. ' this.$init = function () {',
  18831. ' };',
  18832. ' this.$final = function () {',
  18833. ' };',
  18834. '});',
  18835. 'this.A = null;',
  18836. 'this.B = null;',
  18837. 'this.C = null;',
  18838. '']),
  18839. LinesToStr([ // $mod.$main
  18840. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18841. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18842. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18843. '']));
  18844. end;
  18845. procedure TTestModule.TestExternalClass_DestructorFail;
  18846. begin
  18847. StartProgram(false);
  18848. Add('{$modeswitch externalclass}');
  18849. Add('type');
  18850. Add(' TExtA = class external name ''ExtA''');
  18851. Add(' destructor Free;');
  18852. Add(' end;');
  18853. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18854. nPasElementNotSupported);
  18855. ConvertProgram;
  18856. end;
  18857. procedure TTestModule.TestExternalClass_New;
  18858. begin
  18859. StartProgram(false);
  18860. Add([
  18861. '{$modeswitch externalclass}',
  18862. 'type',
  18863. ' TExtA = class external name ''ExtA''',
  18864. ' constructor New;',
  18865. ' constructor New(i: longint; j: longint = 2);',
  18866. ' end;',
  18867. 'var',
  18868. ' A: texta;',
  18869. 'begin',
  18870. ' a:=texta.new;',
  18871. ' a:=texta(texta.new);',
  18872. ' a:=texta.new();',
  18873. ' a:=texta.new(1);',
  18874. ' with texta do begin',
  18875. ' a:=new;',
  18876. ' a:=new();',
  18877. ' a:=new(2);',
  18878. ' end;',
  18879. ' a:=test1.texta.new;',
  18880. ' a:=test1.texta.new();',
  18881. ' a:=test1.texta.new(3);',
  18882. '']);
  18883. ConvertProgram;
  18884. CheckSource('TestExternalClass_New',
  18885. LinesToStr([ // statements
  18886. 'this.A = null;',
  18887. '']),
  18888. LinesToStr([ // $mod.$main
  18889. '$mod.A = new ExtA();',
  18890. '$mod.A = new ExtA();',
  18891. '$mod.A = new ExtA();',
  18892. '$mod.A = new ExtA(1,2);',
  18893. '$mod.A = new ExtA();',
  18894. '$mod.A = new ExtA();',
  18895. '$mod.A = new ExtA(2,2);',
  18896. '$mod.A = new ExtA();',
  18897. '$mod.A = new ExtA();',
  18898. '$mod.A = new ExtA(3,2);',
  18899. '']));
  18900. end;
  18901. procedure TTestModule.TestExternalClass_ClassOf_New;
  18902. begin
  18903. StartProgram(false);
  18904. Add('{$modeswitch externalclass}');
  18905. Add('type');
  18906. Add(' TExtAClass = class of TExtA;');
  18907. Add(' TExtA = class external name ''ExtA''');
  18908. Add(' C: TExtAClass;');
  18909. Add(' constructor New;');
  18910. Add(' end;');
  18911. Add('var');
  18912. Add(' A: texta;');
  18913. Add(' C: textaclass;');
  18914. Add('begin');
  18915. Add(' a:=c.new;');
  18916. Add(' a:=c.new();');
  18917. Add(' with C do begin');
  18918. Add(' a:=new;');
  18919. Add(' a:=new();');
  18920. Add(' end;');
  18921. Add(' a:=test1.c.new;');
  18922. Add(' a:=test1.c.new();');
  18923. Add(' a:=A.c.new();');
  18924. ConvertProgram;
  18925. CheckSource('TestExternalClass_ClassOf_New',
  18926. LinesToStr([ // statements
  18927. 'this.A = null;',
  18928. 'this.C = null;',
  18929. '']),
  18930. LinesToStr([ // $mod.$main
  18931. '$mod.A = new $mod.C();',
  18932. '$mod.A = new $mod.C();',
  18933. 'var $with = $mod.C;',
  18934. '$mod.A = new $with();',
  18935. '$mod.A = new $with();',
  18936. '$mod.A = new $mod.C();',
  18937. '$mod.A = new $mod.C();',
  18938. '$mod.A = new $mod.A.C();',
  18939. '']));
  18940. end;
  18941. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18942. begin
  18943. StartProgram(false);
  18944. Add([
  18945. '{$modeswitch externalclass}',
  18946. 'type',
  18947. ' TExtAClass = class of TExtA;',
  18948. ' TExtA = class external name ''ExtA''',
  18949. ' constructor New;',
  18950. ' end;',
  18951. 'function GetCreator: TExtAClass;',
  18952. 'begin',
  18953. ' Result:=TExtA;',
  18954. 'end;',
  18955. 'var',
  18956. ' A: texta;',
  18957. 'begin',
  18958. ' a:=getcreator.new;',
  18959. ' a:=getcreator().new;',
  18960. ' a:=getcreator().new();',
  18961. ' a:=getcreator.new();',
  18962. ' with getcreator do begin',
  18963. ' a:=new;',
  18964. ' a:=new();',
  18965. ' end;']);
  18966. ConvertProgram;
  18967. CheckSource('TestExternalClass_FuncClassOf_New',
  18968. LinesToStr([ // statements
  18969. 'this.GetCreator = function () {',
  18970. ' var Result = null;',
  18971. ' Result = ExtA;',
  18972. ' return Result;',
  18973. '};',
  18974. 'this.A = null;',
  18975. '']),
  18976. LinesToStr([ // $mod.$main
  18977. '$mod.A = new ($mod.GetCreator())();',
  18978. '$mod.A = new ($mod.GetCreator())();',
  18979. '$mod.A = new ($mod.GetCreator())();',
  18980. '$mod.A = new ($mod.GetCreator())();',
  18981. 'var $with = $mod.GetCreator();',
  18982. '$mod.A = new $with();',
  18983. '$mod.A = new $with();',
  18984. '']));
  18985. end;
  18986. procedure TTestModule.TestExternalClass_New_PasClassFail;
  18987. begin
  18988. StartProgram(false);
  18989. Add([
  18990. '{$modeswitch externalclass}',
  18991. 'type',
  18992. ' TExtA = class external name ''ExtA''',
  18993. ' constructor New;',
  18994. ' end;',
  18995. ' TBird = class(TExtA)',
  18996. ' end;',
  18997. 'begin',
  18998. ' TBird.new;',
  18999. '']);
  19000. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19001. ConvertProgram;
  19002. end;
  19003. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  19004. begin
  19005. StartProgram(false);
  19006. Add([
  19007. '{$modeswitch externalclass}',
  19008. 'type',
  19009. ' TExtA = class external name ''ExtA''',
  19010. ' constructor New;',
  19011. ' end;',
  19012. ' TBird = class(TExtA)',
  19013. ' end;',
  19014. 'begin',
  19015. ' TBird.new();',
  19016. '']);
  19017. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19018. ConvertProgram;
  19019. end;
  19020. procedure TTestModule.TestExternalClass_NewExtName;
  19021. begin
  19022. StartProgram(false);
  19023. Add([
  19024. '{$modeswitch externalclass}',
  19025. 'type',
  19026. ' TExtA = class external name ''ExtA''',
  19027. ' constructor New; external name ''Other'';',
  19028. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19029. ' end;',
  19030. 'var',
  19031. ' A: texta;',
  19032. 'begin',
  19033. ' a:=texta.new;',
  19034. ' a:=texta(texta.new);',
  19035. ' a:=texta.new();',
  19036. ' a:=texta.new(1);',
  19037. ' with texta do begin',
  19038. ' a:=new;',
  19039. ' a:=new();',
  19040. ' a:=new(2);',
  19041. ' end;',
  19042. ' a:=test1.texta.new;',
  19043. ' a:=test1.texta.new();',
  19044. ' a:=test1.texta.new(3);',
  19045. '']);
  19046. ConvertProgram;
  19047. CheckSource('TestExternalClass_NewExtName',
  19048. LinesToStr([ // statements
  19049. 'this.A = null;',
  19050. '']),
  19051. LinesToStr([ // $mod.$main
  19052. '$mod.A = new Other();',
  19053. '$mod.A = new Other();',
  19054. '$mod.A = new Other();',
  19055. '$mod.A = new A.B(1,2);',
  19056. '$mod.A = new Other();',
  19057. '$mod.A = new Other();',
  19058. '$mod.A = new A.B(2,2);',
  19059. '$mod.A = new Other();',
  19060. '$mod.A = new Other();',
  19061. '$mod.A = new A.B(3,2);',
  19062. '']));
  19063. end;
  19064. procedure TTestModule.TestExternalClass_Constructor;
  19065. begin
  19066. StartProgram(false);
  19067. Add([
  19068. '{$modeswitch externalclass}',
  19069. 'type',
  19070. ' TExtA = class external name ''ExtA''',
  19071. ' public type',
  19072. ' TExtB = class external name ''ExtB''',
  19073. ' public type',
  19074. ' TExtC = class external name ''ExtC''',
  19075. ' constructor New;',
  19076. ' constructor New(i: word);',
  19077. ' end;',
  19078. ' end;',
  19079. ' constructor Create;',
  19080. ' constructor Create(i: longint; j: longint = 2);',
  19081. ' end;',
  19082. 'var',
  19083. ' A: texta;',
  19084. ' C: texta.textb.textc;',
  19085. 'begin',
  19086. ' a:=texta.create;',
  19087. ' a:=texta(texta.create);',
  19088. ' a:=texta.create();',
  19089. ' a:=texta.create(1);',
  19090. ' with texta do begin',
  19091. ' a:=create;',
  19092. ' a:=create();',
  19093. ' a:=create(2);',
  19094. ' end;',
  19095. ' a:=test1.texta.create;',
  19096. ' a:=test1.texta.create();',
  19097. ' a:=test1.texta.create(3);',
  19098. ' c:=texta.textb.textc.new;',
  19099. ' c:=texta.textb.textc.new();',
  19100. ' c:=texta.textb.textc.new(4);',
  19101. '']);
  19102. ConvertProgram;
  19103. CheckSource('TestExternalClass_Constructor',
  19104. LinesToStr([ // statements
  19105. 'this.A = null;',
  19106. 'this.C = null;',
  19107. '']),
  19108. LinesToStr([ // $mod.$main
  19109. '$mod.A = new ExtA.Create();',
  19110. '$mod.A = new ExtA.Create();',
  19111. '$mod.A = new ExtA.Create();',
  19112. '$mod.A = new ExtA.Create(1,2);',
  19113. '$mod.A = new ExtA.Create();',
  19114. '$mod.A = new ExtA.Create();',
  19115. '$mod.A = new ExtA.Create(2,2);',
  19116. '$mod.A = new ExtA.Create();',
  19117. '$mod.A = new ExtA.Create();',
  19118. '$mod.A = new ExtA.Create(3,2);',
  19119. '$mod.C = new ExtA.ExtB.ExtC();',
  19120. '$mod.C = new ExtA.ExtB.ExtC();',
  19121. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19122. '']));
  19123. end;
  19124. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19125. begin
  19126. StartProgram(false);
  19127. Add([
  19128. '{$modeswitch externalclass}',
  19129. 'type',
  19130. ' TExtA = class external name ''ExtA''',
  19131. ' constructor Create; external name ''{}'';',
  19132. ' end;',
  19133. 'var',
  19134. ' A: texta;',
  19135. 'begin',
  19136. ' a:=texta.create;',
  19137. ' a:=texta(texta.create);',
  19138. ' a:=texta.create();',
  19139. ' with texta do begin',
  19140. ' a:=create;',
  19141. ' a:=create();',
  19142. ' end;',
  19143. ' a:=test1.texta.create;',
  19144. ' a:=test1.texta.create();',
  19145. '']);
  19146. ConvertProgram;
  19147. CheckSource('TestExternalClass_ConstructorBrackets',
  19148. LinesToStr([ // statements
  19149. 'this.A = null;',
  19150. '']),
  19151. LinesToStr([ // $mod.$main
  19152. '$mod.A = {};',
  19153. '$mod.A = {};',
  19154. '$mod.A = {};',
  19155. '$mod.A = {};',
  19156. '$mod.A = {};',
  19157. '$mod.A = {};',
  19158. '$mod.A = {};',
  19159. '']));
  19160. end;
  19161. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19162. begin
  19163. StartProgram(false);
  19164. Add('{$modeswitch externalclass}');
  19165. Add('type');
  19166. Add(' TExtA = class external name ''ExtA''');
  19167. Add(' constructor New;');
  19168. Add(' end;');
  19169. Add('function DoIt: longint;');
  19170. Add('const ExtA: longint = 3;');
  19171. Add('begin');
  19172. Add(' Result:=ExtA;');
  19173. Add('end;');
  19174. Add('var');
  19175. Add(' A: texta;');
  19176. Add('begin');
  19177. Add(' a:=texta.new;');
  19178. ConvertProgram;
  19179. CheckSource('TestExternalClass_LocalConstSameName',
  19180. LinesToStr([ // statements
  19181. 'var ExtA$1 = 3;',
  19182. 'this.DoIt = function () {',
  19183. ' var Result = 0;',
  19184. ' Result = ExtA$1;',
  19185. ' return Result;',
  19186. '};',
  19187. 'this.A = null;',
  19188. '']),
  19189. LinesToStr([ // $mod.$main
  19190. '$mod.A = new ExtA();',
  19191. '']));
  19192. end;
  19193. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19194. begin
  19195. StartProgram(false);
  19196. Add('{$modeswitch externalclass}');
  19197. Add('type');
  19198. Add(' TExtA = class external name ''ExtA''');
  19199. Add(' procedure DoIt;');
  19200. Add(' end;');
  19201. Add(' TMyA = class(TExtA)');
  19202. Add(' procedure DoIt;');
  19203. Add(' end;');
  19204. Add('procedure TMyA.DoIt; begin end;');
  19205. Add('begin');
  19206. ConvertProgram;
  19207. CheckSource('TestExternalClass_ReintroduceOverload',
  19208. LinesToStr([ // statements
  19209. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19210. ' this.$init = function () {',
  19211. ' };',
  19212. ' this.$final = function () {',
  19213. ' };',
  19214. ' this.DoIt$1 = function () {',
  19215. ' };',
  19216. '});',
  19217. '']),
  19218. LinesToStr([ // $mod.$main
  19219. '']));
  19220. end;
  19221. procedure TTestModule.TestExternalClass_Inherited;
  19222. begin
  19223. StartProgram(false);
  19224. Add('{$modeswitch externalclass}');
  19225. Add('type');
  19226. Add(' TExtA = class external name ''ExtA''');
  19227. Add(' procedure DoIt(i: longint = 1); virtual;');
  19228. Add(' procedure DoSome(j: longint = 2);');
  19229. Add(' end;');
  19230. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19231. Add(' end;');
  19232. Add(' TMyC = class(TExtB)');
  19233. Add(' procedure DoIt(i: longint = 1); override;');
  19234. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19235. Add(' end;');
  19236. Add('procedure TMyC.DoIt(i: longint);');
  19237. Add('begin');
  19238. Add(' inherited;');
  19239. Add(' inherited DoIt;');
  19240. Add(' inherited DoIt();');
  19241. Add(' inherited DoIt(3);');
  19242. Add(' inherited DoSome;');
  19243. Add(' inherited DoSome();');
  19244. Add(' inherited DoSome(4);');
  19245. Add('end;');
  19246. Add('procedure TMyC.DoSome(j: longint);');
  19247. Add('begin');
  19248. Add(' inherited;');
  19249. Add('end;');
  19250. Add('begin');
  19251. ConvertProgram;
  19252. CheckSource('TestExternalClass_ReintroduceOverload',
  19253. LinesToStr([ // statements
  19254. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19255. ' this.$init = function () {',
  19256. ' };',
  19257. ' this.$final = function () {',
  19258. ' };',
  19259. ' this.DoIt = function (i) {',
  19260. ' ExtB.DoIt.apply(this, arguments);',
  19261. ' ExtB.DoIt.call(this, 1);',
  19262. ' ExtB.DoIt.call(this, 1);',
  19263. ' ExtB.DoIt.call(this, 3);',
  19264. ' ExtB.DoSome.call(this, 2);',
  19265. ' ExtB.DoSome.call(this, 2);',
  19266. ' ExtB.DoSome.call(this, 4);',
  19267. ' };',
  19268. ' this.DoSome$1 = function (j) {',
  19269. ' ExtB.DoSome.apply(this, arguments);',
  19270. ' };',
  19271. '});',
  19272. '']),
  19273. LinesToStr([ // $mod.$main
  19274. '']));
  19275. end;
  19276. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19277. begin
  19278. StartProgram(false);
  19279. Add('{$modeswitch externalclass}');
  19280. Add('type');
  19281. Add(' TObject = class');
  19282. Add(' end;');
  19283. Add(' TExtA = class external name ''ExtA''(TObject)');
  19284. Add(' end;');
  19285. Add('begin');
  19286. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19287. ConvertProgram;
  19288. end;
  19289. procedure TTestModule.TestExternalClass_NewInstance;
  19290. begin
  19291. StartProgram(false);
  19292. Add('{$modeswitch externalclass}');
  19293. Add('type');
  19294. Add(' TExtA = class external name ''ExtA''');
  19295. Add(' end;');
  19296. Add(' TMyB = class(TExtA)');
  19297. Add(' protected');
  19298. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19299. Add(' end;');
  19300. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19301. Add('begin end;');
  19302. Add('begin');
  19303. ConvertProgram;
  19304. CheckSource('TestExternalClass_NewInstance',
  19305. LinesToStr([ // statements
  19306. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19307. ' this.$init = function () {',
  19308. ' };',
  19309. ' this.$final = function () {',
  19310. ' };',
  19311. ' this.NewInstance = function (fnname, paramarray) {',
  19312. ' var Result = null;',
  19313. ' return Result;',
  19314. ' };',
  19315. '});',
  19316. '']),
  19317. LinesToStr([ // $mod.$main
  19318. '']));
  19319. end;
  19320. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19321. begin
  19322. StartProgram(false);
  19323. Add('{$modeswitch externalclass}');
  19324. Add('type');
  19325. Add(' TExtA = class external name ''ExtA''');
  19326. Add(' end;');
  19327. Add(' TMyB = class(TExtA)');
  19328. Add(' protected');
  19329. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19330. Add(' end;');
  19331. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19332. Add('begin end;');
  19333. Add('begin');
  19334. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19335. ConvertProgram;
  19336. end;
  19337. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19338. begin
  19339. StartProgram(false);
  19340. Add('{$modeswitch externalclass}');
  19341. Add('type');
  19342. Add(' TExtA = class external name ''ExtA''');
  19343. Add(' end;');
  19344. Add(' TMyB = class(TExtA)');
  19345. Add(' protected');
  19346. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19347. Add(' end;');
  19348. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19349. Add('begin end;');
  19350. Add('begin');
  19351. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19352. nIncompatibleTypeArgNo);
  19353. ConvertProgram;
  19354. end;
  19355. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19356. begin
  19357. StartProgram(false);
  19358. Add('{$modeswitch externalclass}');
  19359. Add('type');
  19360. Add(' TExtA = class external name ''ExtA''');
  19361. Add(' end;');
  19362. Add(' TMyB = class(TExtA)');
  19363. Add(' protected');
  19364. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19365. Add(' end;');
  19366. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19367. Add('begin end;');
  19368. Add('begin');
  19369. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19370. nIncompatibleTypeArgNo);
  19371. ConvertProgram;
  19372. end;
  19373. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19374. begin
  19375. StartProgram(false);
  19376. Add([
  19377. '{$modeswitch externalclass}',
  19378. 'type',
  19379. ' TJSFunction = class external name ''Function''',
  19380. ' end;',
  19381. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19382. ' constructor New(w: word);',
  19383. ' end;',
  19384. ' TBird = class (TExtA)',
  19385. ' public',
  19386. ' Size: word;',
  19387. ' class var Legs: word;',
  19388. ' constructor Create(a: word);',
  19389. ' end;',
  19390. ' TEagle = class (TBird)',
  19391. ' public',
  19392. ' constructor Create(b: word); reintroduce;',
  19393. ' end;',
  19394. 'constructor TBird.Create(a: word);',
  19395. 'begin',
  19396. ' inherited;', // silently ignored
  19397. ' inherited New(a);', // this.$func(a)
  19398. 'end;',
  19399. 'constructor TEagle.Create(b: word);',
  19400. 'begin',
  19401. ' inherited Create(b);',
  19402. 'end;',
  19403. 'var',
  19404. ' Bird: TBird;',
  19405. ' Eagle: TEagle;',
  19406. 'begin',
  19407. ' Bird:=TBird.Create(3);',
  19408. ' Eagle:=TEagle.Create(4);',
  19409. ' Bird.Size:=Bird.Size+5;',
  19410. ' Bird.Legs:=Bird.Legs+6;',
  19411. ' Eagle.Size:=Eagle.Size+5;',
  19412. ' Eagle.Legs:=Eagle.Legs+6;',
  19413. '']);
  19414. ConvertProgram;
  19415. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19416. LinesToStr([ // statements
  19417. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19418. ' this.Legs = 0;',
  19419. ' this.$init = function () {',
  19420. ' this.Size = 0;',
  19421. ' };',
  19422. ' this.$final = function () {',
  19423. ' };',
  19424. ' this.Create = function (a) {',
  19425. ' this.$ancestorfunc(a);',
  19426. ' return this;',
  19427. ' };',
  19428. '});',
  19429. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19430. ' this.Create$1 = function (b) {',
  19431. ' $mod.TBird.Create.call(this, b);',
  19432. ' return this;',
  19433. ' };',
  19434. '});',
  19435. 'this.Bird = null;',
  19436. 'this.Eagle = null;',
  19437. '']),
  19438. LinesToStr([ // $mod.$main
  19439. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19440. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19441. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19442. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19443. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19444. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19445. '']));
  19446. end;
  19447. procedure TTestModule.TestExternalClass_PascalProperty;
  19448. begin
  19449. StartProgram(false);
  19450. Add('{$modeswitch externalclass}');
  19451. Add('type');
  19452. Add(' TJSElement = class;');
  19453. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19454. Add(' TJSElement = class external name ''ExtA''');
  19455. Add(' end;');
  19456. Add(' TControl = class(TJSElement)');
  19457. Add(' private');
  19458. Add(' FOnClick: TJSNotifyEvent;');
  19459. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19460. Add(' procedure Click(Sender: TJSElement);');
  19461. Add(' end;');
  19462. Add('procedure TControl.Click(Sender: TJSElement);');
  19463. Add('begin');
  19464. Add(' OnClick(Self);');
  19465. Add('end;');
  19466. Add('var');
  19467. Add(' Ctrl: TControl;');
  19468. Add('begin');
  19469. Add(' Ctrl.OnClick:[email protected];');
  19470. Add(' Ctrl.OnClick(Ctrl);');
  19471. ConvertProgram;
  19472. CheckSource('TestExternalClass_PascalProperty',
  19473. LinesToStr([ // statements
  19474. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19475. ' this.$init = function () {',
  19476. ' this.FOnClick = null;',
  19477. ' };',
  19478. ' this.$final = function () {',
  19479. ' this.FOnClick = undefined;',
  19480. ' };',
  19481. ' this.Click = function (Sender) {',
  19482. ' this.FOnClick(this);',
  19483. ' };',
  19484. '});',
  19485. 'this.Ctrl = null;',
  19486. '']),
  19487. LinesToStr([ // $mod.$main
  19488. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19489. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19490. '']));
  19491. end;
  19492. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19493. begin
  19494. StartProgram(false);
  19495. Add([
  19496. '{$modeswitch externalclass}',
  19497. 'type',
  19498. ' IUnknown = interface end;',
  19499. ' TObject = class',
  19500. ' end;',
  19501. ' TChild = class',
  19502. ' end;',
  19503. ' TExtRootA = class external name ''ExtRootA''',
  19504. ' end;',
  19505. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19506. ' end;',
  19507. ' TExtRootB = class external name ''ExtRootB''',
  19508. ' end;',
  19509. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19510. ' end;',
  19511. ' TExtString = class external name ''String''',
  19512. ' function charAt(aIndex : NativeInt) : string;',
  19513. ' end;',
  19514. 'var',
  19515. ' Obj: TObject;',
  19516. ' Child: TChild;',
  19517. ' RootA: TExtRootA;',
  19518. ' ChildA: TExtChildA;',
  19519. ' RootB: TExtRootB;',
  19520. ' ChildB: TExtChildB;',
  19521. ' i: IUnknown;',
  19522. ' s: string;',
  19523. ' v: jsvalue;',
  19524. 'begin',
  19525. ' obj:=tobject(roota);',
  19526. ' obj:=tobject(childa);',
  19527. ' child:=tchild(tobject(roota));',
  19528. ' roota:=textroota(obj);',
  19529. ' roota:=textroota(child);',
  19530. ' roota:=textroota(rootb);',
  19531. ' roota:=textroota(childb);',
  19532. ' childa:=textchilda(textroota(obj));',
  19533. ' roota:=TExtRootA(i);',
  19534. ' s:=TExtString(s).charAt(7);',
  19535. ' s:=TExtString(v).charAt(8);',
  19536. '']);
  19537. ConvertProgram;
  19538. CheckSource('TestExternalClass_TypeCastToRootClass',
  19539. LinesToStr([ // statements
  19540. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19541. 'rtl.createClass(this, "TObject", null, function () {',
  19542. ' this.$init = function () {',
  19543. ' };',
  19544. ' this.$final = function () {',
  19545. ' };',
  19546. '});',
  19547. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19548. '});',
  19549. 'this.Obj = null;',
  19550. 'this.Child = null;',
  19551. 'this.RootA = null;',
  19552. 'this.ChildA = null;',
  19553. 'this.RootB = null;',
  19554. 'this.ChildB = null;',
  19555. 'this.i = null;',
  19556. 'this.s = "";',
  19557. 'this.v = undefined;',
  19558. '']),
  19559. LinesToStr([ // $mod.$main
  19560. '$mod.Obj = $mod.RootA;',
  19561. '$mod.Obj = $mod.ChildA;',
  19562. '$mod.Child = $mod.RootA;',
  19563. '$mod.RootA = $mod.Obj;',
  19564. '$mod.RootA = $mod.Child;',
  19565. '$mod.RootA = $mod.RootB;',
  19566. '$mod.RootA = $mod.ChildB;',
  19567. '$mod.ChildA = $mod.Obj;',
  19568. '$mod.RootA = $mod.i;',
  19569. '$mod.s = $mod.s.charAt(7);',
  19570. '$mod.s = $mod.v.charAt(8);',
  19571. '']));
  19572. end;
  19573. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19574. begin
  19575. StartProgram(false);
  19576. Add([
  19577. '{$modeswitch externalclass}',
  19578. 'type',
  19579. ' IUnknown = interface end;',
  19580. ' IBird = interface(IUnknown) end;',
  19581. ' TClass = class of TObject;',
  19582. ' TObject = class',
  19583. ' end;',
  19584. ' TChild = class',
  19585. ' end;',
  19586. ' TJSObject = class external name ''Object''',
  19587. ' end;',
  19588. ' TRec = record end;',
  19589. 'var',
  19590. ' Obj: TObject;',
  19591. ' Child: TChild;',
  19592. ' i: IUnknown;',
  19593. ' Bird: IBird;',
  19594. ' j: TJSObject;',
  19595. ' r: TRec;',
  19596. ' c: TClass;',
  19597. 'begin',
  19598. ' j:=tjsobject(IUnknown);',
  19599. ' j:=tjsobject(IBird);',
  19600. ' j:=tjsobject(TObject);',
  19601. ' j:=tjsobject(TChild);',
  19602. ' j:=tjsobject(TRec);',
  19603. ' j:=tjsobject(Obj);',
  19604. ' j:=tjsobject(Child);',
  19605. ' j:=tjsobject(i);',
  19606. ' j:=tjsobject(Bird);',
  19607. ' j:=tjsobject(r);',
  19608. ' j:=tjsobject(c);',
  19609. '']);
  19610. ConvertProgram;
  19611. CheckSource('TestExternalClass_TypeCastToJSObject',
  19612. LinesToStr([ // statements
  19613. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19614. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19615. 'rtl.createClass(this, "TObject", null, function () {',
  19616. ' this.$init = function () {',
  19617. ' };',
  19618. ' this.$final = function () {',
  19619. ' };',
  19620. '});',
  19621. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19622. '});',
  19623. 'rtl.recNewT(this, "TRec", function () {',
  19624. ' this.$eq = function (b) {',
  19625. ' return true;',
  19626. ' };',
  19627. ' this.$assign = function (s) {',
  19628. ' return this;',
  19629. ' };',
  19630. '});',
  19631. 'this.Obj = null;',
  19632. 'this.Child = null;',
  19633. 'this.i = null;',
  19634. 'this.Bird = null;',
  19635. 'this.j = null;',
  19636. 'this.r = this.TRec.$new();',
  19637. 'this.c = null;',
  19638. '']),
  19639. LinesToStr([ // $mod.$main
  19640. '$mod.j = $mod.IUnknown;',
  19641. '$mod.j = $mod.IBird;',
  19642. '$mod.j = $mod.TObject;',
  19643. '$mod.j = $mod.TChild;',
  19644. '$mod.j = $mod.TRec;',
  19645. '$mod.j = $mod.Obj;',
  19646. '$mod.j = $mod.Child;',
  19647. '$mod.j = $mod.i;',
  19648. '$mod.j = $mod.Bird;',
  19649. '$mod.j = $mod.r;',
  19650. '$mod.j = $mod.c;',
  19651. '']));
  19652. end;
  19653. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19654. begin
  19655. StartProgram(false);
  19656. Add('{$modeswitch externalclass}');
  19657. Add('type');
  19658. Add(' TJSString = class external name ''String''');
  19659. Add(' class function fromCharCode() : string; varargs;');
  19660. Add(' function anchor(const aName : string) : string;');
  19661. Add(' end;');
  19662. Add('var');
  19663. Add(' s: string;');
  19664. Add('begin');
  19665. Add(' s:=TJSString.fromCharCode(65,66);');
  19666. Add(' s:=TJSString(s).anchor(s);');
  19667. Add(' s:=TJSString(''foo'').anchor(s);');
  19668. ConvertProgram;
  19669. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19670. LinesToStr([ // statements
  19671. 'this.s = "";',
  19672. '']),
  19673. LinesToStr([ // $mod.$main
  19674. '$mod.s = String.fromCharCode(65, 66);',
  19675. '$mod.s = $mod.s.anchor($mod.s);',
  19676. '$mod.s = "foo".anchor($mod.s);',
  19677. '']));
  19678. end;
  19679. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19680. begin
  19681. StartProgram(false);
  19682. Add([
  19683. '{$modeswitch externalclass}',
  19684. 'type',
  19685. ' TJSObject = class external name ''Object'' end;',
  19686. ' TJSFunction = class external name ''Function''',
  19687. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19688. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19689. ' end;',
  19690. ' TObject = class',
  19691. ' procedure DoIt(i: longint);',
  19692. ' end;',
  19693. ' TFuncInt = function(o: TObject): longint;',
  19694. 'function GetIt(o: TObject): longint;',
  19695. ' procedure Sub; begin end;',
  19696. 'var',
  19697. ' f: TJSFunction;',
  19698. ' fi: TFuncInt;',
  19699. 'begin',
  19700. ' fi:=TFuncInt(f);',
  19701. ' f:=TJSFunction(fi);',
  19702. ' f:=TJSFunction(@GetIt);',
  19703. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19704. ' f:=TJSFunction(@Sub);',
  19705. ' f:=TJSFunction(@o.doit);',
  19706. ' f:=TJSFunction(fi).bind(nil,4)',
  19707. 'end;',
  19708. 'procedure TObject.DoIt(i: longint);',
  19709. ' procedure Sub; begin end;',
  19710. 'var f: TJSFunction;',
  19711. 'begin',
  19712. ' f:=TJSFunction(@DoIt);',
  19713. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19714. ' f:=TJSFunction(@Sub);',
  19715. ' f:=TJSFunction(@GetIt);',
  19716. 'end;',
  19717. 'begin']);
  19718. ConvertProgram;
  19719. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19720. LinesToStr([ // statements
  19721. 'rtl.createClass(this, "TObject", null, function () {',
  19722. ' this.$init = function () {',
  19723. ' };',
  19724. ' this.$final = function () {',
  19725. ' };',
  19726. ' this.DoIt = function (i) {',
  19727. ' var $Self = this;',
  19728. ' function Sub() {',
  19729. ' };',
  19730. ' var f = null;',
  19731. ' f = this.DoIt;',
  19732. ' f = this.DoIt.bind(null, 13);',
  19733. ' f = Sub;',
  19734. ' f = $mod.GetIt;',
  19735. ' };',
  19736. '});',
  19737. 'this.GetIt = function (o) {',
  19738. ' var Result = 0;',
  19739. ' function Sub() {',
  19740. ' };',
  19741. ' var f = null;',
  19742. ' var fi = null;',
  19743. ' fi = f;',
  19744. ' f = fi;',
  19745. ' f = $mod.GetIt;',
  19746. ' f = $mod.GetIt.bind(null, 3);',
  19747. ' f = Sub;',
  19748. ' f = $mod.TObject.DoIt;',
  19749. ' f = fi.bind(null, 4);',
  19750. ' return Result;',
  19751. '};',
  19752. '']),
  19753. LinesToStr([ // $mod.$main
  19754. '']));
  19755. end;
  19756. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19757. begin
  19758. StartProgram(false);
  19759. Add([
  19760. '{$mode delphi}',
  19761. '{$modeswitch externalclass}',
  19762. 'type',
  19763. ' TJSObject = class external name ''Object'' end;',
  19764. ' TJSWindow = class external name ''Window''(TJSObject)',
  19765. ' procedure Open;',
  19766. ' end;',
  19767. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19768. ' procedure Execute;',
  19769. ' end;',
  19770. 'procedure Fly;',
  19771. 'var',
  19772. ' w: TJSWindow;',
  19773. ' e: TJSEventTarget;',
  19774. 'begin',
  19775. ' w:=TJSWindow(e);',
  19776. ' e:=TJSEventTarget(w);',
  19777. 'end;',
  19778. 'begin']);
  19779. ConvertProgram;
  19780. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19781. LinesToStr([ // statements
  19782. 'this.Fly = function () {',
  19783. ' var w = null;',
  19784. ' var e = null;',
  19785. ' w = e;',
  19786. ' e = w;',
  19787. '};',
  19788. '']),
  19789. LinesToStr([ // $mod.$main
  19790. '']));
  19791. end;
  19792. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19793. begin
  19794. StartProgram(false);
  19795. Add('{$modeswitch externalclass}');
  19796. Add('type');
  19797. Add(' TJSString = class external name ''String''');
  19798. Add(' class function fromCharCode() : string; varargs;');
  19799. Add(' end;');
  19800. Add('var');
  19801. Add(' s: string;');
  19802. Add(' sObj: TJSString;');
  19803. Add('begin');
  19804. Add(' s:=sObj.fromCharCode(65,66);');
  19805. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19806. nExternalClassInstanceCannotAccessStaticX);
  19807. ConvertProgram;
  19808. end;
  19809. procedure TTestModule.TestExternalClass_BracketAccessor;
  19810. begin
  19811. StartProgram(false);
  19812. Add([
  19813. '{$modeswitch externalclass}',
  19814. 'type',
  19815. ' TJSArray = class external name ''Array2''',
  19816. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19817. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19818. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19819. ' end;',
  19820. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19821. 'begin end;',
  19822. 'var',
  19823. ' Arr: tjsarray;',
  19824. ' s: string;',
  19825. ' i: longint;',
  19826. ' v: jsvalue;',
  19827. 'begin',
  19828. ' v:=arr[0];',
  19829. ' v:=arr.items[1];',
  19830. ' arr[2]:=s;',
  19831. ' arr.items[3]:=s;',
  19832. ' arr[4]:=i;',
  19833. ' arr[5]:=arr[6];',
  19834. ' arr.items[7]:=arr.items[8];',
  19835. ' with arr do items[9]:=items[10];',
  19836. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19837. ' with arr do begin',
  19838. ' v:=GetItems(14);',
  19839. ' setitems(15,16);',
  19840. ' end;',
  19841. ' v:=test1.arr.items[17];',
  19842. ' test1.arr.items[18]:=v;',
  19843. '']);
  19844. ConvertProgram;
  19845. CheckSource('TestExternalClass_BracketAccessor',
  19846. LinesToStr([ // statements
  19847. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19848. '};',
  19849. 'this.Arr = null;',
  19850. 'this.s = "";',
  19851. 'this.i = 0;',
  19852. 'this.v = undefined;',
  19853. '']),
  19854. LinesToStr([ // $mod.$main
  19855. '$mod.v = $mod.Arr[0];',
  19856. '$mod.v = $mod.Arr[1];',
  19857. '$mod.Arr[2] = $mod.s;',
  19858. '$mod.Arr[3] = $mod.s;',
  19859. '$mod.Arr[4] = $mod.i;',
  19860. '$mod.Arr[5] = $mod.Arr[6];',
  19861. '$mod.Arr[7] = $mod.Arr[8];',
  19862. 'var $with = $mod.Arr;',
  19863. '$with[9] = $with[10];',
  19864. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19865. ' a: 9,',
  19866. ' p: $mod.Arr,',
  19867. ' get: function () {',
  19868. ' return this.p[this.a];',
  19869. ' },',
  19870. ' set: function (v) {',
  19871. ' this.p[this.a] = v;',
  19872. ' }',
  19873. '}, {',
  19874. ' a: 10,',
  19875. ' p: $mod.Arr,',
  19876. ' get: function () {',
  19877. ' return this.p[this.a];',
  19878. ' },',
  19879. ' set: function (v) {',
  19880. ' this.p[this.a] = v;',
  19881. ' }',
  19882. '});',
  19883. 'var $with1 = $mod.Arr;',
  19884. '$mod.v = $with1[14];',
  19885. '$with1[15] = 16;',
  19886. '$mod.v = $mod.Arr[17];',
  19887. '$mod.Arr[18] = $mod.v;',
  19888. '']));
  19889. end;
  19890. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19891. begin
  19892. StartProgram(false);
  19893. Add([
  19894. '{$modeswitch externalclass}',
  19895. 'type',
  19896. ' TJSArray = class external name ''Array2''',
  19897. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19898. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19899. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19900. ' end;',
  19901. ' TMyArr = class(TJSArray)',
  19902. ' procedure DoIt;',
  19903. ' end;',
  19904. 'procedure tmyarr.DoIt;',
  19905. 'begin',
  19906. ' Items[1]:=Items[2];',
  19907. ' SetItems(3,getItems(4));',
  19908. 'end;',
  19909. 'var',
  19910. ' Arr: tmyarr;',
  19911. ' s: string;',
  19912. ' i: longint;',
  19913. ' v: jsvalue;',
  19914. 'begin',
  19915. ' v:=arr[0];',
  19916. ' v:=arr.items[1];',
  19917. ' arr[2]:=s;',
  19918. ' arr.items[3]:=s;',
  19919. ' arr[4]:=i;',
  19920. ' arr[5]:=arr[6];',
  19921. ' arr.items[7]:=arr.items[8];',
  19922. ' with arr do items[9]:=items[10];',
  19923. ' with arr do begin',
  19924. ' v:=GetItems(14);',
  19925. ' setitems(15,16);',
  19926. ' end;',
  19927. '']);
  19928. ConvertProgram;
  19929. CheckSource('TestExternalClass_BracketAccessor_Call',
  19930. LinesToStr([ // statements
  19931. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19932. ' this.$init = function () {',
  19933. ' };',
  19934. ' this.$final = function () {',
  19935. ' };',
  19936. ' this.DoIt = function () {',
  19937. ' this[1] = this[2];',
  19938. ' this[3] = this[4];',
  19939. ' };',
  19940. '});',
  19941. 'this.Arr = null;',
  19942. 'this.s = "";',
  19943. 'this.i = 0;',
  19944. 'this.v = undefined;',
  19945. '']),
  19946. LinesToStr([ // $mod.$main
  19947. '$mod.v = $mod.Arr[0];',
  19948. '$mod.v = $mod.Arr[1];',
  19949. '$mod.Arr[2] = $mod.s;',
  19950. '$mod.Arr[3] = $mod.s;',
  19951. '$mod.Arr[4] = $mod.i;',
  19952. '$mod.Arr[5] = $mod.Arr[6];',
  19953. '$mod.Arr[7] = $mod.Arr[8];',
  19954. 'var $with = $mod.Arr;',
  19955. '$with[9] = $with[10];',
  19956. 'var $with1 = $mod.Arr;',
  19957. '$mod.v = $with1[14];',
  19958. '$with1[15] = 16;',
  19959. '']));
  19960. end;
  19961. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  19962. begin
  19963. StartProgram(false);
  19964. Add('{$modeswitch externalclass}');
  19965. Add('type');
  19966. Add(' TJSArray = class external name ''Array2''');
  19967. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  19968. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  19969. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  19970. Add(' end;');
  19971. Add('begin');
  19972. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  19973. nBracketAccessorOfExternalClassMustHaveOneParameter);
  19974. ConvertProgram;
  19975. end;
  19976. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  19977. begin
  19978. StartProgram(false);
  19979. Add('{$modeswitch externalclass}');
  19980. Add('type');
  19981. Add(' TJSArray = class external name ''Array2''');
  19982. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19983. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  19984. Add(' end;');
  19985. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  19986. Add('begin end;');
  19987. Add('var');
  19988. Add(' Arr: tjsarray;');
  19989. Add(' v: jsvalue;');
  19990. Add('begin');
  19991. Add(' v:=arr[0];');
  19992. Add(' v:=arr.items[1];');
  19993. Add(' with arr do v:=items[2];');
  19994. Add(' doit(arr[3],arr[4]);');
  19995. ConvertProgram;
  19996. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  19997. LinesToStr([ // statements
  19998. 'this.DoIt = function (vI, vJ) {',
  19999. '};',
  20000. 'this.Arr = null;',
  20001. 'this.v = undefined;',
  20002. '']),
  20003. LinesToStr([ // $mod.$main
  20004. '$mod.v = $mod.Arr[0];',
  20005. '$mod.v = $mod.Arr[1];',
  20006. 'var $with = $mod.Arr;',
  20007. '$mod.v = $with[2];',
  20008. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20009. '']));
  20010. end;
  20011. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20012. begin
  20013. StartProgram(false);
  20014. Add('{$modeswitch externalclass}');
  20015. Add('type');
  20016. Add(' TJSArray = class external name ''Array2''');
  20017. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20018. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20019. Add(' end;');
  20020. Add('var');
  20021. Add(' Arr: tjsarray;');
  20022. Add(' s: string;');
  20023. Add(' i: longint;');
  20024. Add(' v: jsvalue;');
  20025. Add('begin');
  20026. Add(' arr[2]:=s;');
  20027. Add(' arr.items[3]:=s;');
  20028. Add(' arr[4]:=i;');
  20029. Add(' with arr do items[5]:=i;');
  20030. ConvertProgram;
  20031. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20032. LinesToStr([ // statements
  20033. 'this.Arr = null;',
  20034. 'this.s = "";',
  20035. 'this.i = 0;',
  20036. 'this.v = undefined;',
  20037. '']),
  20038. LinesToStr([ // $mod.$main
  20039. '$mod.Arr[2] = $mod.s;',
  20040. '$mod.Arr[3] = $mod.s;',
  20041. '$mod.Arr[4] = $mod.i;',
  20042. 'var $with = $mod.Arr;',
  20043. '$with[5] = $mod.i;',
  20044. '']));
  20045. end;
  20046. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20047. begin
  20048. StartProgram(false);
  20049. Add('{$modeswitch externalclass}');
  20050. Add('type');
  20051. Add(' TJSArray = class external name ''Array2''');
  20052. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20053. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20054. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20055. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20056. Add(' end;');
  20057. Add('var');
  20058. Add(' Arr: tjsarray;');
  20059. Add(' s: string;');
  20060. Add(' i: longint;');
  20061. Add(' v: jsvalue;');
  20062. Add('begin');
  20063. Add(' arr[2]:=s;');
  20064. Add(' arr.items[3]:=s;');
  20065. Add(' arr.numbers[4]:=i;');
  20066. Add(' with arr do items[5]:=i;');
  20067. Add(' with arr do numbers[6]:=i;');
  20068. ConvertProgram;
  20069. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20070. LinesToStr([ // statements
  20071. 'this.Arr = null;',
  20072. 'this.s = "";',
  20073. 'this.i = 0;',
  20074. 'this.v = undefined;',
  20075. '']),
  20076. LinesToStr([ // $mod.$main
  20077. '$mod.Arr[2] = $mod.s;',
  20078. '$mod.Arr[3] = $mod.s;',
  20079. '$mod.Arr[4] = $mod.i;',
  20080. 'var $with = $mod.Arr;',
  20081. '$with[5] = $mod.i;',
  20082. 'var $with1 = $mod.Arr;',
  20083. '$with1[6] = $mod.i;',
  20084. '']));
  20085. end;
  20086. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20087. begin
  20088. StartProgram(false);
  20089. Add('{$modeswitch externalclass}');
  20090. Add('type');
  20091. Add(' TJSArray = class external name ''Array2''');
  20092. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20093. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20094. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20095. Add(' end;');
  20096. Add('var');
  20097. Add(' Arr: tjsarray;');
  20098. Add(' i: longint;');
  20099. Add(' IntArr: array of longint;');
  20100. Add(' v: jsvalue;');
  20101. Add('begin');
  20102. Add(' v:=arr.items[i];');
  20103. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20104. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20105. ConvertProgram;
  20106. CheckSource('TestExternalClass_BracketAccessor_Index',
  20107. LinesToStr([ // statements
  20108. 'this.Arr = null;',
  20109. 'this.i = 0;',
  20110. 'this.IntArr = [];',
  20111. 'this.v = undefined;',
  20112. '']),
  20113. LinesToStr([ // $mod.$main
  20114. '$mod.v = $mod.Arr[$mod.i];',
  20115. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20116. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20117. '']));
  20118. end;
  20119. procedure TTestModule.TestExternalClass_ForInJSObject;
  20120. begin
  20121. StartProgram(false);
  20122. Add([
  20123. '{$modeswitch externalclass}',
  20124. 'type',
  20125. ' TJSObject = class external name ''Object''',
  20126. ' end;',
  20127. 'var',
  20128. ' o: TJSObject;',
  20129. ' key: string;',
  20130. 'begin',
  20131. ' for key in o do',
  20132. ' if key=''abc'' then ;',
  20133. '']);
  20134. ConvertProgram;
  20135. CheckSource('TestExternalClass_ForInJSObject',
  20136. LinesToStr([ // statements
  20137. 'this.o = null;',
  20138. 'this.key = "";',
  20139. '']),
  20140. LinesToStr([ // $mod.$main
  20141. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20142. '']));
  20143. end;
  20144. procedure TTestModule.TestExternalClass_ForInJSArray;
  20145. begin
  20146. StartProgram(false);
  20147. Add([
  20148. '{$modeswitch externalclass}',
  20149. 'type',
  20150. ' TJSInt8Array = class external name ''Int8Array''',
  20151. ' private',
  20152. ' flength: NativeInt external name ''length'';',
  20153. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20154. ' public',
  20155. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20156. ' property Length: NativeInt read flength;',
  20157. ' end;',
  20158. 'var',
  20159. ' a: TJSInt8Array;',
  20160. ' value: shortint;',
  20161. 'begin',
  20162. ' for value in a do',
  20163. ' if value=3 then ;',
  20164. '']);
  20165. ConvertProgram;
  20166. CheckSource('TestExternalClass_ForInJSArray',
  20167. LinesToStr([ // statements
  20168. 'this.a = null;',
  20169. 'this.value = 0;',
  20170. '']),
  20171. LinesToStr([ // $mod.$main
  20172. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20173. ' $mod.value = $in[$l];',
  20174. ' if ($mod.value === 3) ;',
  20175. '};',
  20176. '']));
  20177. end;
  20178. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20179. begin
  20180. AddModuleWithIntfImplSrc('unit2.pas',
  20181. LinesToStr([
  20182. '{$modeswitch externalclass}',
  20183. 'type',
  20184. ' TJSBufferSource = class external name ''BufferSource''',
  20185. ' end;',
  20186. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20187. '']),
  20188. '');
  20189. AddModuleWithIntfImplSrc('unit3.pas',
  20190. LinesToStr([
  20191. '{$modeswitch externalclass}',
  20192. 'type',
  20193. ' TJSBufferSource = class external name ''BufferSource''',
  20194. ' end;',
  20195. '']),
  20196. '');
  20197. StartUnit(true);
  20198. Add([
  20199. 'interface',
  20200. 'uses unit2, unit3;',
  20201. 'procedure DoSome(s: TJSBufferSource);',
  20202. 'implementation',
  20203. 'procedure DoSome(s: TJSBufferSource);',
  20204. 'begin',
  20205. ' DoIt(s);',
  20206. 'end;',
  20207. '']);
  20208. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20209. nIncompatibleTypeArgNo);
  20210. ConvertUnit;
  20211. end;
  20212. procedure TTestModule.TestExternalClass_NestedConstructor;
  20213. begin
  20214. StartProgram(false);
  20215. Add([
  20216. '{$modeswitch externalclass}',
  20217. 'type',
  20218. ' TJSObject = class external name ''Object''',
  20219. ' type TBird = class external name ''Bird''',
  20220. ' type TWing = class external name ''Wing''',
  20221. ' constructor New;',
  20222. ' constructor Create(w: word = 3);',
  20223. ' end;',
  20224. ' end;',
  20225. ' end;',
  20226. 'var',
  20227. ' w: TJSObject.TBird.TWing;',
  20228. 'begin',
  20229. ' w:=tjsobject.tbird.twing.new;',
  20230. ' w:=tjsobject.tbird.twing.new();',
  20231. ' w:=tjsobject.tbird.twing.create;',
  20232. ' w:=tjsobject.tbird.twing.create(4);',
  20233. ' with tjsobject do begin',
  20234. ' w:=tbird.twing.new;',
  20235. ' w:=tbird.twing.new();',
  20236. ' w:=tbird.twing.create;',
  20237. ' w:=tbird.twing.create(11);',
  20238. ' end;',
  20239. ' with tjsobject.tbird do begin',
  20240. ' w:=twing.new;',
  20241. ' w:=twing.new();',
  20242. ' w:=twing.create;',
  20243. ' w:=twing.create(21);',
  20244. ' end;',
  20245. ' with tjsobject.tbird.twing do begin',
  20246. ' w:=new;',
  20247. ' w:=new();',
  20248. ' w:=create;',
  20249. ' w:=create(31);',
  20250. ' end;',
  20251. '']);
  20252. ConvertProgram;
  20253. CheckSource('TestExternalClass_NestedConstructor',
  20254. LinesToStr([ // statements
  20255. 'this.w = null;',
  20256. '']),
  20257. LinesToStr([ // $mod.$main
  20258. '$mod.w = new Object.Bird.Wing();',
  20259. '$mod.w = new Object.Bird.Wing();',
  20260. '$mod.w = new Object.Bird.Wing.Create();',
  20261. '$mod.w = new Object.Bird.Wing.Create(4);',
  20262. '$mod.w = new Object.Bird.Wing();',
  20263. '$mod.w = new Object.Bird.Wing();',
  20264. '$mod.w = new Object.Bird.Wing.Create();',
  20265. '$mod.w = new Object.Bird.Wing.Create(11);',
  20266. 'var $with = Object.Bird;',
  20267. '$mod.w = new Object.Bird.Wing();',
  20268. '$mod.w = new Object.Bird.Wing();',
  20269. '$mod.w = new Object.Bird.Wing.Create();',
  20270. '$mod.w = new Object.Bird.Wing.Create(21);',
  20271. 'var $with1 = Object.Bird.Wing;',
  20272. '$mod.w = new $with1();',
  20273. '$mod.w = new $with1();',
  20274. '$mod.w = new Object.Bird.Wing.Create();',
  20275. '$mod.w = new Object.Bird.Wing.Create(31);',
  20276. '']));
  20277. end;
  20278. procedure TTestModule.TestClassInterface_Corba;
  20279. begin
  20280. StartProgram(false);
  20281. Add([
  20282. '{$interfaces corba}',
  20283. 'type',
  20284. ' IUnknown = interface;',
  20285. ' IUnknown = interface',
  20286. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20287. ' end;',
  20288. ' IInterface = IUnknown;',
  20289. ' IBird = interface(IInterface)',
  20290. ' function GetSize: longint;',
  20291. ' procedure SetSize(i: longint);',
  20292. ' property Size: longint read GetSize write SetSize;',
  20293. ' procedure DoIt(i: longint);',
  20294. ' end;',
  20295. ' TObject = class',
  20296. ' end;',
  20297. ' TBird = class(TObject,IBird)',
  20298. ' function GetSize: longint; virtual; abstract;',
  20299. ' procedure SetSize(i: longint); virtual; abstract;',
  20300. ' procedure DoIt(i: longint); virtual; abstract;',
  20301. ' end;',
  20302. 'var',
  20303. ' BirdIntf: IBird;',
  20304. 'begin',
  20305. ' BirdIntf.Size:=BirdIntf.Size;',
  20306. '']);
  20307. ConvertProgram;
  20308. CheckSource('TestClassInterface_Corba',
  20309. LinesToStr([ // statements
  20310. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20311. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20312. 'rtl.createClass(this, "TObject", null, function () {',
  20313. ' this.$init = function () {',
  20314. ' };',
  20315. ' this.$final = function () {',
  20316. ' };',
  20317. '});',
  20318. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20319. ' rtl.addIntf(this, $mod.IBird);',
  20320. '});',
  20321. 'this.BirdIntf = null;',
  20322. '']),
  20323. LinesToStr([ // $mod.$main
  20324. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20325. '']));
  20326. end;
  20327. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20328. begin
  20329. StartProgram(false);
  20330. Add([
  20331. '{$interfaces corba}',
  20332. 'type',
  20333. ' IUnknown = interface',
  20334. ' procedure DoIt; external name ''foo'';',
  20335. ' end;',
  20336. 'begin']);
  20337. SetExpectedParserError(
  20338. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20339. nParserNoFieldsAllowed);
  20340. ConvertProgram;
  20341. end;
  20342. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20343. begin
  20344. StartProgram(false);
  20345. Add([
  20346. '{$interfaces corba}',
  20347. 'type',
  20348. ' integer = longint;',
  20349. ' IUnknown = interface',
  20350. ' procedure DoIt(i: integer);',
  20351. ' procedure DoIt(s: string);',
  20352. ' end;',
  20353. ' IBird = interface(IUnknown)',
  20354. ' procedure DoIt(b: boolean); overload;',
  20355. ' end;',
  20356. ' TObject = class',
  20357. ' end;',
  20358. ' TBird = class(TObject,IBird)',
  20359. ' procedure DoIt(o: TObject);',
  20360. ' procedure DoIt(s: string);',
  20361. ' procedure DoIt(i: integer);',
  20362. ' procedure DoIt(b: boolean);',
  20363. ' end;',
  20364. 'procedure TBird.DoIt(o: TObject); begin end;',
  20365. 'procedure TBird.DoIt(s: string); begin end;',
  20366. 'procedure TBird.DoIt(i: integer); begin end;',
  20367. 'procedure TBird.DoIt(b: boolean); begin end;',
  20368. 'var',
  20369. ' BirdIntf: IBird;',
  20370. 'begin',
  20371. ' BirdIntf.DoIt(3);',
  20372. ' BirdIntf.DoIt(''abc'');',
  20373. ' BirdIntf.DoIt(true);',
  20374. '']);
  20375. ConvertProgram;
  20376. CheckSource('TestClassInterface_Overloads',
  20377. LinesToStr([ // statements
  20378. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20379. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20380. 'rtl.createClass(this, "TObject", null, function () {',
  20381. ' this.$init = function () {',
  20382. ' };',
  20383. ' this.$final = function () {',
  20384. ' };',
  20385. '});',
  20386. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20387. ' this.DoIt = function (o) {',
  20388. ' };',
  20389. ' this.DoIt$1 = function (s) {',
  20390. ' };',
  20391. ' this.DoIt$2 = function (i) {',
  20392. ' };',
  20393. ' this.DoIt$3 = function (b) {',
  20394. ' };',
  20395. ' rtl.addIntf(this, $mod.IBird, {',
  20396. ' DoIt$2: "DoIt$3",',
  20397. ' DoIt: "DoIt$2"',
  20398. ' });',
  20399. '});',
  20400. 'this.BirdIntf = null;',
  20401. '']),
  20402. LinesToStr([ // $mod.$main
  20403. '$mod.BirdIntf.DoIt(3);',
  20404. '$mod.BirdIntf.DoIt$1("abc");',
  20405. '$mod.BirdIntf.DoIt$2(true);',
  20406. '']));
  20407. end;
  20408. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20409. begin
  20410. StartProgram(false);
  20411. Add([
  20412. '{$interfaces corba}',
  20413. 'type',
  20414. ' IBird = interface',
  20415. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20416. ' end;',
  20417. ' IDog = interface',
  20418. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20419. ' end;',
  20420. ' TObject = class(IBird,IDog)',
  20421. ' end;',
  20422. 'begin']);
  20423. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20424. nDuplicateGUIDXInYZ);
  20425. ConvertProgram;
  20426. end;
  20427. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20428. begin
  20429. StartProgram(false);
  20430. Add([
  20431. '{$interfaces corba}',
  20432. 'type',
  20433. ' IAnimal = interface',
  20434. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20435. ' end;',
  20436. ' IBird = interface(IAnimal)',
  20437. ' end;',
  20438. ' IHawk = interface(IBird)',
  20439. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20440. ' end;',
  20441. 'begin']);
  20442. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20443. nDuplicateGUIDXInYZ);
  20444. ConvertProgram;
  20445. end;
  20446. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20447. begin
  20448. StartProgram(false);
  20449. Add([
  20450. '{$interfaces corba}',
  20451. 'type',
  20452. ' integer = longint;',
  20453. ' IUnknown = interface',
  20454. ' procedure DoIt(i: integer);',
  20455. ' end;',
  20456. ' IBird = interface',
  20457. ' procedure Fly(i: integer);',
  20458. ' end;',
  20459. ' TObject = class(IUnknown)',
  20460. ' procedure DoIt(i: integer);',
  20461. ' end;',
  20462. ' TBird = class(IBird)',
  20463. ' procedure Fly(i: integer);',
  20464. ' end;',
  20465. 'procedure TObject.DoIt(i: integer); begin end;',
  20466. 'procedure TBird.Fly(i: integer); begin end;',
  20467. 'begin',
  20468. '']);
  20469. ConvertProgram;
  20470. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20471. LinesToStr([ // statements
  20472. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20473. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20474. 'rtl.createClass(this, "TObject", null, function () {',
  20475. ' this.$init = function () {',
  20476. ' };',
  20477. ' this.$final = function () {',
  20478. ' };',
  20479. ' this.DoIt = function (i) {',
  20480. ' };',
  20481. ' rtl.addIntf(this, $mod.IUnknown);',
  20482. '});',
  20483. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20484. ' this.Fly = function (i) {',
  20485. ' };',
  20486. ' rtl.addIntf(this, $mod.IBird);',
  20487. ' rtl.addIntf(this, $mod.IUnknown);',
  20488. '});',
  20489. '']),
  20490. LinesToStr([ // $mod.$main
  20491. '']));
  20492. end;
  20493. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20494. begin
  20495. StartProgram(false);
  20496. Add([
  20497. '{$interfaces corba}',
  20498. 'type',
  20499. ' integer = longint;',
  20500. ' IBird = interface',
  20501. ' procedure DoIt(i: integer);',
  20502. ' end;',
  20503. ' TObject = class',
  20504. ' procedure DoIt(i: integer);',
  20505. ' end;',
  20506. ' TBird = class(IBird)',
  20507. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20508. ' end;',
  20509. 'procedure TObject.DoIt(i: integer); begin end;',
  20510. 'procedure TBird.DoIt(i: integer); begin end;',
  20511. 'begin',
  20512. '']);
  20513. ConvertProgram;
  20514. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20515. LinesToStr([ // statements
  20516. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20517. 'rtl.createClass(this, "TObject", null, function () {',
  20518. ' this.$init = function () {',
  20519. ' };',
  20520. ' this.$final = function () {',
  20521. ' };',
  20522. ' this.DoIt = function (i) {',
  20523. ' };',
  20524. '});',
  20525. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20526. ' this.DoIt$1 = function (i) {',
  20527. ' };',
  20528. ' rtl.addIntf(this, $mod.IBird, {',
  20529. ' DoIt: "DoIt$1"',
  20530. ' });',
  20531. '});',
  20532. '']),
  20533. LinesToStr([ // $mod.$main
  20534. '']));
  20535. end;
  20536. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20537. begin
  20538. StartProgram(false);
  20539. Add([
  20540. '{$interfaces corba}',
  20541. 'type',
  20542. ' IUnknown = interface',
  20543. ' procedure Walk(i: longint);',
  20544. ' end;',
  20545. ' IBird = interface(IUnknown)',
  20546. ' procedure Walk(b: boolean); overload;',
  20547. ' procedure Fly(s: string);',
  20548. ' end;',
  20549. ' TObject = class',
  20550. ' end;',
  20551. ' TBird = class(TObject,IBird)',
  20552. ' procedure IBird.Fly = Move;',
  20553. ' procedure IBird.Walk = Hop;',
  20554. ' procedure Hop(i: longint);',
  20555. ' procedure Move(s: string);',
  20556. ' procedure Hop(b: boolean);',
  20557. ' end;',
  20558. 'procedure TBird.Move(s: string); begin end;',
  20559. 'procedure TBird.Hop(i: longint); begin end;',
  20560. 'procedure TBird.Hop(b: boolean); begin end;',
  20561. 'var',
  20562. ' BirdIntf: IBird;',
  20563. 'begin',
  20564. ' BirdIntf.Walk(3);',
  20565. ' BirdIntf.Walk(true);',
  20566. ' BirdIntf.Fly(''abc'');',
  20567. '']);
  20568. ConvertProgram;
  20569. CheckSource('TestClassInterface_Corba_MethodResolution',
  20570. LinesToStr([ // statements
  20571. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20572. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20573. 'rtl.createClass(this, "TObject", null, function () {',
  20574. ' this.$init = function () {',
  20575. ' };',
  20576. ' this.$final = function () {',
  20577. ' };',
  20578. '});',
  20579. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20580. ' this.Hop = function (i) {',
  20581. ' };',
  20582. ' this.Move = function (s) {',
  20583. ' };',
  20584. ' this.Hop$1 = function (b) {',
  20585. ' };',
  20586. ' rtl.addIntf(this, $mod.IBird, {',
  20587. ' Walk$1: "Hop$1",',
  20588. ' Fly: "Move",',
  20589. ' Walk: "Hop"',
  20590. ' });',
  20591. '});',
  20592. 'this.BirdIntf = null;',
  20593. '']),
  20594. LinesToStr([ // $mod.$main
  20595. '$mod.BirdIntf.Walk(3);',
  20596. '$mod.BirdIntf.Walk$1(true);',
  20597. '$mod.BirdIntf.Fly("abc");',
  20598. '']));
  20599. end;
  20600. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20601. begin
  20602. StartProgram(false);
  20603. Add([
  20604. '{$interfaces com}',
  20605. 'type',
  20606. ' IUnknown = interface',
  20607. ' function _AddRef: longint;',
  20608. ' procedure Walk;',
  20609. ' end;',
  20610. ' IBird = interface end;',
  20611. ' IDog = interface end;',
  20612. ' TObject = class(IBird,IDog)',
  20613. ' function _AddRef: longint; virtual; abstract;',
  20614. ' procedure Walk; virtual; abstract;',
  20615. ' end;',
  20616. ' TBird = class(IUnknown)',
  20617. ' end;',
  20618. 'begin',
  20619. '']);
  20620. ConvertProgram;
  20621. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20622. LinesToStr([ // statements
  20623. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20624. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20625. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20626. 'rtl.createClass(this, "TObject", null, function () {',
  20627. ' this.$init = function () {',
  20628. ' };',
  20629. ' this.$final = function () {',
  20630. ' };',
  20631. ' rtl.addIntf(this, $mod.IBird);',
  20632. ' rtl.addIntf(this, $mod.IDog);',
  20633. '});',
  20634. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20635. ' rtl.addIntf(this, $mod.IUnknown);',
  20636. ' rtl.addIntf(this, $mod.IBird);',
  20637. ' rtl.addIntf(this, $mod.IDog);',
  20638. '});',
  20639. '']),
  20640. LinesToStr([ // $mod.$main
  20641. '']));
  20642. end;
  20643. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20644. begin
  20645. StartProgram(false);
  20646. Add([
  20647. '{$interfaces corba}',
  20648. 'type',
  20649. ' IUnknown = interface',
  20650. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20651. ' procedure Go;',
  20652. ' end;',
  20653. ' TObject = class(IUnknown)',
  20654. ' procedure Go; virtual; abstract;',
  20655. ' end;',
  20656. ' TBird = class',
  20657. ' procedure Go; override;',
  20658. ' end;',
  20659. ' TCat = class(TObject)',
  20660. ' procedure Go; override;',
  20661. ' end;',
  20662. ' TDog = class(TObject, IUnknown)',
  20663. ' procedure Go; override;',
  20664. ' end;',
  20665. 'procedure TBird.Go; begin end;',
  20666. 'procedure TCat.Go; begin end;',
  20667. 'procedure TDog.Go; begin end;',
  20668. 'begin',
  20669. '']);
  20670. ConvertProgram;
  20671. CheckSource('TestClassInterface_Corba_MethodOverride',
  20672. LinesToStr([ // statements
  20673. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20674. 'rtl.createClass(this, "TObject", null, function () {',
  20675. ' this.$init = function () {',
  20676. ' };',
  20677. ' this.$final = function () {',
  20678. ' };',
  20679. ' rtl.addIntf(this, $mod.IUnknown);',
  20680. '});',
  20681. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20682. ' this.Go = function () {',
  20683. ' };',
  20684. ' rtl.addIntf(this, $mod.IUnknown);',
  20685. '});',
  20686. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20687. ' this.Go = function () {',
  20688. ' };',
  20689. ' rtl.addIntf(this, $mod.IUnknown);',
  20690. '});',
  20691. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20692. ' this.Go = function () {',
  20693. ' };',
  20694. ' rtl.addIntf(this, $mod.IUnknown);',
  20695. '});',
  20696. '']),
  20697. LinesToStr([ // $mod.$main
  20698. '']));
  20699. end;
  20700. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20701. begin
  20702. StartProgram(false);
  20703. Add([
  20704. '{$interfaces corba}',
  20705. 'type',
  20706. ' IUnknown = interface',
  20707. ' end;',
  20708. ' IBird = interface(IUnknown)',
  20709. ' procedure Fly(s: string);',
  20710. ' end;',
  20711. ' IEagle = interface(IBird)',
  20712. ' end;',
  20713. ' IDove = interface(IBird)',
  20714. ' end;',
  20715. ' ISwallow = interface(IBird)',
  20716. ' end;',
  20717. ' TObject = class',
  20718. ' end;',
  20719. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20720. ' procedure Fly(s: string); virtual; abstract;',
  20721. ' end;',
  20722. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20723. ' FBirdIntf: IBird;',
  20724. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20725. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20726. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20727. ' FDoveObj: TBird;',
  20728. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20729. ' function GetSwallowObj: TBird; virtual; abstract;',
  20730. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20731. ' end;',
  20732. 'begin',
  20733. '']);
  20734. ConvertProgram;
  20735. CheckSource('TestClassInterface_Corba_Delegation',
  20736. LinesToStr([ // statements
  20737. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20738. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20739. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20740. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20741. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20742. 'rtl.createClass(this, "TObject", null, function () {',
  20743. ' this.$init = function () {',
  20744. ' };',
  20745. ' this.$final = function () {',
  20746. ' };',
  20747. '});',
  20748. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20749. ' rtl.addIntf(this, $mod.IBird);',
  20750. ' rtl.addIntf(this, $mod.IEagle);',
  20751. ' rtl.addIntf(this, $mod.IDove);',
  20752. ' rtl.addIntf(this, $mod.ISwallow);',
  20753. '});',
  20754. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20755. ' this.$init = function () {',
  20756. ' $mod.TObject.$init.call(this);',
  20757. ' this.FBirdIntf = null;',
  20758. ' this.FDoveObj = null;',
  20759. ' };',
  20760. ' this.$final = function () {',
  20761. ' this.FBirdIntf = undefined;',
  20762. ' this.FDoveObj = undefined;',
  20763. ' $mod.TObject.$final.call(this);',
  20764. ' };',
  20765. ' this.$intfmaps = {',
  20766. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20767. ' return this.FBirdIntf;',
  20768. ' },',
  20769. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20770. ' return this.GetEagleIntf();',
  20771. ' },',
  20772. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20773. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20774. ' },',
  20775. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20776. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20777. ' }',
  20778. ' };',
  20779. '});',
  20780. '']),
  20781. LinesToStr([ // $mod.$main
  20782. '']));
  20783. end;
  20784. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20785. begin
  20786. StartProgram(false);
  20787. Add([
  20788. '{$interfaces corba}',
  20789. 'type',
  20790. ' IUnknown = interface',
  20791. ' end;',
  20792. ' IBird = interface(IUnknown)',
  20793. ' procedure Fly(s: string);',
  20794. ' end;',
  20795. ' IEagle = interface(IBird)',
  20796. ' end;',
  20797. ' IDove = interface(IBird)',
  20798. ' end;',
  20799. ' ISwallow = interface(IBird)',
  20800. ' end;',
  20801. ' TObject = class',
  20802. ' end;',
  20803. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20804. ' procedure Fly(s: string); virtual; abstract;',
  20805. ' end;',
  20806. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20807. ' private',
  20808. ' class var FBirdIntf: IBird;',
  20809. ' class var FDoveObj: TBird;',
  20810. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20811. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20812. ' protected',
  20813. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20814. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20815. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20816. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20817. ' end;',
  20818. 'begin',
  20819. '']);
  20820. ConvertProgram;
  20821. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20822. LinesToStr([ // statements
  20823. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20824. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20825. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20826. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20827. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20828. 'rtl.createClass(this, "TObject", null, function () {',
  20829. ' this.$init = function () {',
  20830. ' };',
  20831. ' this.$final = function () {',
  20832. ' };',
  20833. '});',
  20834. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20835. ' rtl.addIntf(this, $mod.IBird);',
  20836. ' rtl.addIntf(this, $mod.IEagle);',
  20837. ' rtl.addIntf(this, $mod.IDove);',
  20838. ' rtl.addIntf(this, $mod.ISwallow);',
  20839. '});',
  20840. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20841. ' this.FBirdIntf = null;',
  20842. ' this.FDoveObj = null;',
  20843. ' this.$intfmaps = {',
  20844. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20845. ' return this.FBirdIntf;',
  20846. ' },',
  20847. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20848. ' return this.GetEagleIntf();',
  20849. ' },',
  20850. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20851. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20852. ' },',
  20853. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20854. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20855. ' }',
  20856. ' };',
  20857. '});',
  20858. '']),
  20859. LinesToStr([ // $mod.$main
  20860. '']));
  20861. end;
  20862. procedure TTestModule.TestClassInterface_Corba_Operators;
  20863. begin
  20864. StartProgram(false);
  20865. Add([
  20866. '{$interfaces corba}',
  20867. 'type',
  20868. ' IUnknown = interface',
  20869. ' end;',
  20870. ' IBird = interface(IUnknown)',
  20871. ' function GetItems(Index: longint): longint;',
  20872. ' procedure SetItems(Index: longint; Value: longint);',
  20873. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20874. ' end;',
  20875. ' TObject = class',
  20876. ' end;',
  20877. ' TBird = class(TObject,IBird)',
  20878. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20879. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20880. ' end;',
  20881. 'var',
  20882. ' IntfVar: IBird = nil;',
  20883. ' IntfVar2: IBird;',
  20884. ' ObjVar: TBird;',
  20885. ' v: JSValue;',
  20886. 'begin',
  20887. ' IntfVar:=nil;',
  20888. ' IntfVar[3]:=IntfVar[4];',
  20889. ' if Assigned(IntfVar) then ;',
  20890. ' IntfVar:=IntfVar2;',
  20891. ' IntfVar:=ObjVar;',
  20892. ' if IntfVar=IntfVar2 then ;',
  20893. ' if IntfVar<>IntfVar2 then ;',
  20894. ' if IntfVar is IBird then ;',
  20895. ' if IntfVar is TBird then ;',
  20896. ' if ObjVar is IBird then ;',
  20897. ' IntfVar:=IntfVar2 as IBird;',
  20898. ' ObjVar:=IntfVar2 as TBird;',
  20899. ' IntfVar:=ObjVar as IBird;',
  20900. ' IntfVar:=IBird(IntfVar2);',
  20901. ' ObjVar:=TBird(IntfVar);',
  20902. ' IntfVar:=IBird(ObjVar);',
  20903. ' v:=IntfVar;',
  20904. ' IntfVar:=IBird(v);',
  20905. ' if v is IBird then ;',
  20906. ' v:=JSValue(IntfVar);',
  20907. ' v:=IBird;',
  20908. '']);
  20909. ConvertProgram;
  20910. CheckSource('TestClassInterface_Corba_Operators',
  20911. LinesToStr([ // statements
  20912. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20913. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20914. 'rtl.createClass(this, "TObject", null, function () {',
  20915. ' this.$init = function () {',
  20916. ' };',
  20917. ' this.$final = function () {',
  20918. ' };',
  20919. '});',
  20920. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20921. ' rtl.addIntf(this, $mod.IBird);',
  20922. '});',
  20923. 'this.IntfVar = null;',
  20924. 'this.IntfVar2 = null;',
  20925. 'this.ObjVar = null;',
  20926. 'this.v = undefined;',
  20927. '']),
  20928. LinesToStr([ // $mod.$main
  20929. '$mod.IntfVar = null;',
  20930. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20931. 'if ($mod.IntfVar != null) ;',
  20932. '$mod.IntfVar = $mod.IntfVar2;',
  20933. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20934. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20935. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20936. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20937. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20938. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20939. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20940. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20941. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20942. '$mod.IntfVar = $mod.IntfVar2;',
  20943. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20944. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20945. '$mod.v = $mod.IntfVar;',
  20946. '$mod.IntfVar = rtl.getObject($mod.v);',
  20947. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20948. '$mod.v = $mod.IntfVar;',
  20949. '$mod.v = $mod.IBird;',
  20950. '']));
  20951. end;
  20952. procedure TTestModule.TestClassInterface_Corba_Args;
  20953. begin
  20954. StartProgram(false);
  20955. Add([
  20956. '{$interfaces corba}',
  20957. 'type',
  20958. ' IUnknown = interface',
  20959. ' end;',
  20960. ' IBird = interface(IUnknown)',
  20961. ' end;',
  20962. ' TObject = class',
  20963. ' end;',
  20964. ' TBird = class(TObject,IBird)',
  20965. ' end;',
  20966. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  20967. 'begin',
  20968. ' DoIt(i,i,i);',
  20969. 'end;',
  20970. 'procedure Change(var i: IBird; out j: IBird);',
  20971. 'begin',
  20972. ' DoIt(i,i,i);',
  20973. ' Change(i,i);',
  20974. 'end;',
  20975. 'var',
  20976. ' i: IBird;',
  20977. ' o: TBird;',
  20978. 'begin',
  20979. ' DoIt(i,i,i);',
  20980. ' Change(i,i);',
  20981. ' DoIt(o,o,o);',
  20982. '']);
  20983. ConvertProgram;
  20984. CheckSource('TestClassInterface_Corba_Args',
  20985. LinesToStr([ // statements
  20986. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20987. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  20988. 'rtl.createClass(this, "TObject", null, function () {',
  20989. ' this.$init = function () {',
  20990. ' };',
  20991. ' this.$final = function () {',
  20992. ' };',
  20993. '});',
  20994. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20995. ' rtl.addIntf(this, $mod.IBird);',
  20996. '});',
  20997. 'this.DoIt = function (u, i, j) {',
  20998. ' $mod.DoIt({',
  20999. ' get: function () {',
  21000. ' return i;',
  21001. ' },',
  21002. ' set: function (v) {',
  21003. ' i = v;',
  21004. ' }',
  21005. ' }, i, i);',
  21006. '};',
  21007. 'this.Change = function (i, j) {',
  21008. ' $mod.DoIt(i, i.get(), i.get());',
  21009. ' $mod.Change(i, i);',
  21010. '};',
  21011. 'this.i = null;',
  21012. 'this.o = null;',
  21013. '']),
  21014. LinesToStr([ // $mod.$main
  21015. '$mod.DoIt({',
  21016. ' p: $mod,',
  21017. ' get: function () {',
  21018. ' return this.p.i;',
  21019. ' },',
  21020. ' set: function (v) {',
  21021. ' this.p.i = v;',
  21022. ' }',
  21023. '}, $mod.i, $mod.i);',
  21024. '$mod.Change({',
  21025. ' p: $mod,',
  21026. ' get: function () {',
  21027. ' return this.p.i;',
  21028. ' },',
  21029. ' set: function (v) {',
  21030. ' this.p.i = v;',
  21031. ' }',
  21032. '}, {',
  21033. ' p: $mod,',
  21034. ' get: function () {',
  21035. ' return this.p.i;',
  21036. ' },',
  21037. ' set: function (v) {',
  21038. ' this.p.i = v;',
  21039. ' }',
  21040. '});',
  21041. '$mod.DoIt({',
  21042. ' p: $mod,',
  21043. ' get: function () {',
  21044. ' return this.p.o;',
  21045. ' },',
  21046. ' set: function (v) {',
  21047. ' this.p.o = v;',
  21048. ' }',
  21049. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21050. '']));
  21051. end;
  21052. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21053. begin
  21054. StartProgram(false);
  21055. Add([
  21056. '{$interfaces corba}',
  21057. 'type',
  21058. ' IUnknown = interface end;',
  21059. ' TObject = class',
  21060. ' Id: longint;',
  21061. ' end;',
  21062. ' IEnumerator = interface(IUnknown)',
  21063. ' function GetCurrent: TObject;',
  21064. ' function MoveNext: Boolean;',
  21065. ' property Current: TObject read GetCurrent;',
  21066. ' end;',
  21067. ' IEnumerable = interface(IUnknown)',
  21068. ' function GetEnumerator: IEnumerator;',
  21069. ' end;',
  21070. 'var',
  21071. ' o: TObject;',
  21072. ' i: IEnumerable;',
  21073. 'begin',
  21074. ' for o in i do o.Id:=3;',
  21075. '']);
  21076. ConvertProgram;
  21077. CheckSource('TestClassInterface_Corba_ForIn',
  21078. LinesToStr([ // statements
  21079. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21080. 'rtl.createClass(this, "TObject", null, function () {',
  21081. ' this.$init = function () {',
  21082. ' this.Id = 0;',
  21083. ' };',
  21084. ' this.$final = function () {',
  21085. ' };',
  21086. '});',
  21087. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21088. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21089. 'this.o = null;',
  21090. 'this.i = null;',
  21091. '']),
  21092. LinesToStr([ // $mod.$main
  21093. 'var $in = $mod.i.GetEnumerator();',
  21094. 'while ($in.MoveNext()) {',
  21095. ' $mod.o = $in.GetCurrent();',
  21096. ' $mod.o.Id = 3;',
  21097. '};',
  21098. '']));
  21099. end;
  21100. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21101. begin
  21102. StartProgram(false);
  21103. Add([
  21104. '{$interfaces corba}',
  21105. 'type',
  21106. ' IUnknown = interface end;',
  21107. ' IBird = interface(IUnknown)',
  21108. ' function Fly(w: word): word;',
  21109. ' end;',
  21110. ' TBirdArray = array of IBird;',
  21111. 'var',
  21112. ' i: IBird;',
  21113. ' a: TBirdArray;',
  21114. 'begin',
  21115. ' SetLength(a,3);',
  21116. ' i:=a[1];',
  21117. ' a[2]:=i;',
  21118. ' for i in a do i.fly(3);',
  21119. '']);
  21120. ConvertProgram;
  21121. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21122. LinesToStr([ // statements
  21123. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21124. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21125. 'this.i = null;',
  21126. 'this.a = [];',
  21127. '']),
  21128. LinesToStr([ // $mod.$main
  21129. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21130. '$mod.i = $mod.a[1];',
  21131. '$mod.a[2] = $mod.i;',
  21132. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21133. ' $mod.i = $in[$l];',
  21134. ' $mod.i.Fly(3);',
  21135. '};',
  21136. '']));
  21137. end;
  21138. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21139. begin
  21140. StartProgram(false);
  21141. Add([
  21142. '{$interfaces com}',
  21143. 'type',
  21144. ' IUnknown = interface',
  21145. ' function _AddRef: longint;',
  21146. ' function _Release: longint;',
  21147. ' end;',
  21148. ' TObject = class(IUnknown)',
  21149. ' function _AddRef: longint; virtual; abstract;',
  21150. ' function _Release: longint; virtual; abstract;',
  21151. ' end;',
  21152. 'var',
  21153. ' i: IUnknown;',
  21154. 'procedure DoGlobal(o: TObject);',
  21155. 'begin',
  21156. ' i:=nil;',
  21157. ' i:=o;',
  21158. ' i:=i;',
  21159. 'end;',
  21160. 'procedure DoLocal(o: TObject);',
  21161. 'const k: IUnknown = nil;',
  21162. 'var j: IUnknown;',
  21163. 'begin',
  21164. ' k:=o;',
  21165. ' k:=i;',
  21166. ' j:=o;',
  21167. ' j:=i;',
  21168. 'end;',
  21169. 'var o: TObject;',
  21170. 'begin',
  21171. ' i:=nil;',
  21172. ' i:=o;',
  21173. '']);
  21174. ConvertProgram;
  21175. CheckSource('TestClassInterface_COM_AssignVar',
  21176. LinesToStr([ // statements
  21177. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21178. 'rtl.createClass(this, "TObject", null, function () {',
  21179. ' this.$init = function () {',
  21180. ' };',
  21181. ' this.$final = function () {',
  21182. ' };',
  21183. ' rtl.addIntf(this, $mod.IUnknown);',
  21184. '});',
  21185. 'this.i = null;',
  21186. 'this.DoGlobal = function (o) {',
  21187. ' rtl.setIntfP($mod, "i", null);',
  21188. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21189. ' rtl.setIntfP($mod, "i", $mod.i);',
  21190. '};',
  21191. 'var k = null;',
  21192. 'this.DoLocal = function (o) {',
  21193. ' var j = null;',
  21194. ' try{',
  21195. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21196. ' k = rtl.setIntfL(k, $mod.i);',
  21197. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21198. ' j = rtl.setIntfL(j, $mod.i);',
  21199. ' }finally{',
  21200. ' rtl._Release(j);',
  21201. ' };',
  21202. '};',
  21203. 'this.o = null;',
  21204. '']),
  21205. LinesToStr([ // $mod.$main
  21206. 'rtl.setIntfP($mod, "i", null);',
  21207. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21208. '']));
  21209. end;
  21210. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21211. begin
  21212. StartProgram(false);
  21213. Add([
  21214. '{$interfaces com}',
  21215. 'type',
  21216. ' IUnknown = interface',
  21217. ' function _AddRef: longint;',
  21218. ' function _Release: longint;',
  21219. ' end;',
  21220. ' TObject = class(IUnknown)',
  21221. ' function _AddRef: longint; virtual; abstract;',
  21222. ' function _Release: longint; virtual; abstract;',
  21223. ' end;',
  21224. 'procedure DoDefault(i, j: IUnknown);',
  21225. 'begin',
  21226. ' i:=nil;',
  21227. ' i:=j;',
  21228. 'end;',
  21229. 'begin',
  21230. '']);
  21231. ConvertProgram;
  21232. CheckSource('TestClassInterface_COM_AssignArg',
  21233. LinesToStr([ // statements
  21234. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21235. 'rtl.createClass(this, "TObject", null, function () {',
  21236. ' this.$init = function () {',
  21237. ' };',
  21238. ' this.$final = function () {',
  21239. ' };',
  21240. ' rtl.addIntf(this, $mod.IUnknown);',
  21241. '});',
  21242. 'this.DoDefault = function (i, j) {',
  21243. ' rtl._AddRef(i);',
  21244. ' try {',
  21245. ' i = rtl.setIntfL(i, null);',
  21246. ' i = rtl.setIntfL(i, j);',
  21247. ' } finally {',
  21248. ' rtl._Release(i);',
  21249. ' };',
  21250. '};',
  21251. '']),
  21252. LinesToStr([ // $mod.$main
  21253. '']));
  21254. end;
  21255. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21256. begin
  21257. StartProgram(false);
  21258. Add([
  21259. '{$interfaces com}',
  21260. 'type',
  21261. ' IUnknown = interface',
  21262. ' function _AddRef: longint;',
  21263. ' function _Release: longint;',
  21264. ' end;',
  21265. ' TObject = class(IUnknown)',
  21266. ' function _AddRef: longint; virtual; abstract;',
  21267. ' function _Release: longint; virtual; abstract;',
  21268. ' end;',
  21269. 'function DoDefault(i: IUnknown): IUnknown;',
  21270. 'begin',
  21271. ' Result:=i;',
  21272. ' if Result<>nil then exit;',
  21273. 'end;',
  21274. 'begin',
  21275. '']);
  21276. ConvertProgram;
  21277. CheckSource('TestClassInterface_COM_FunctionResult',
  21278. LinesToStr([ // statements
  21279. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21280. 'rtl.createClass(this, "TObject", null, function () {',
  21281. ' this.$init = function () {',
  21282. ' };',
  21283. ' this.$final = function () {',
  21284. ' };',
  21285. ' rtl.addIntf(this, $mod.IUnknown);',
  21286. '});',
  21287. 'this.DoDefault = function (i) {',
  21288. ' var Result = null;',
  21289. ' var $ok = false;',
  21290. ' try {',
  21291. ' Result = rtl.setIntfL(Result, i);',
  21292. ' if(Result !== null){',
  21293. ' $ok = true;',
  21294. ' return Result;',
  21295. ' };',
  21296. ' $ok = true;',
  21297. ' } finally {',
  21298. ' if(!$ok) rtl._Release(Result);',
  21299. ' };',
  21300. ' return Result;',
  21301. '};',
  21302. '']),
  21303. LinesToStr([ // $mod.$main
  21304. '']));
  21305. end;
  21306. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21307. begin
  21308. StartProgram(false);
  21309. Add([
  21310. '{$interfaces com}',
  21311. 'type',
  21312. ' IUnknown = interface',
  21313. ' function _AddRef: longint;',
  21314. ' function _Release: longint;',
  21315. ' end;',
  21316. ' TObject = class(IUnknown)',
  21317. ' function _AddRef: longint; virtual; abstract;',
  21318. ' function _Release: longint; virtual; abstract;',
  21319. ' function GetIntf: IUnknown; virtual;',
  21320. ' end;',
  21321. ' TMouse = class',
  21322. ' function GetIntf: IUnknown; override;',
  21323. ' end;',
  21324. 'function TObject.GetIntf: IUnknown; begin end;',
  21325. 'function TMouse.GetIntf: IUnknown;',
  21326. 'var i: IUnknown;',
  21327. 'begin',
  21328. ' inherited;',
  21329. ' inherited GetIntf;',
  21330. ' inherited GetIntf();',
  21331. ' Result:=inherited GetIntf;',
  21332. ' Result:=inherited GetIntf();',
  21333. ' i:=inherited GetIntf;',
  21334. ' i:=inherited GetIntf();',
  21335. 'end;',
  21336. 'begin',
  21337. '']);
  21338. ConvertProgram;
  21339. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21340. LinesToStr([ // statements
  21341. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21342. 'rtl.createClass(this, "TObject", null, function () {',
  21343. ' this.$init = function () {',
  21344. ' };',
  21345. ' this.$final = function () {',
  21346. ' };',
  21347. ' this.GetIntf = function () {',
  21348. ' var Result = null;',
  21349. ' return Result;',
  21350. ' };',
  21351. ' rtl.addIntf(this, $mod.IUnknown);',
  21352. '});',
  21353. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21354. ' this.GetIntf = function () {',
  21355. ' var Result = null;',
  21356. ' var i = null;',
  21357. ' var $ir = rtl.createIntfRefs();',
  21358. ' var $ok = false;',
  21359. ' try {',
  21360. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21361. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21362. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21363. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21364. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21365. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21366. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21367. ' $ok = true;',
  21368. ' } finally {',
  21369. ' $ir.free();',
  21370. ' rtl._Release(i);',
  21371. ' if (!$ok) rtl._Release(Result);',
  21372. ' };',
  21373. ' return Result;',
  21374. ' };',
  21375. ' rtl.addIntf(this, $mod.IUnknown);',
  21376. '});',
  21377. '']),
  21378. LinesToStr([ // $mod.$main
  21379. '']));
  21380. end;
  21381. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21382. begin
  21383. StartProgram(false);
  21384. Add([
  21385. '{$interfaces com}',
  21386. 'type',
  21387. ' IUnknown = interface',
  21388. ' function _AddRef: longint;',
  21389. ' function _Release: longint;',
  21390. ' end;',
  21391. ' TObject = class(IUnknown)',
  21392. ' function _AddRef: longint; virtual; abstract;',
  21393. ' function _Release: longint; virtual; abstract;',
  21394. ' end;',
  21395. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21396. 'begin',
  21397. ' if i is IUnknown then ;',
  21398. ' if o is IUnknown then ;',
  21399. ' if i is TObject then ;',
  21400. ' i:=j as IUnknown;',
  21401. ' i:=o as IUnknown;',
  21402. ' o:=j as TObject;',
  21403. ' i:=IUnknown(j);',
  21404. ' i:=IUnknown(o);',
  21405. ' o:=TObject(i);',
  21406. 'end;',
  21407. 'begin',
  21408. '']);
  21409. ConvertProgram;
  21410. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21411. LinesToStr([ // statements
  21412. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21413. 'rtl.createClass(this, "TObject", null, function () {',
  21414. ' this.$init = function () {',
  21415. ' };',
  21416. ' this.$final = function () {',
  21417. ' };',
  21418. ' rtl.addIntf(this, $mod.IUnknown);',
  21419. '});',
  21420. 'this.DoDefault = function (i, j, o) {',
  21421. ' rtl._AddRef(i);',
  21422. ' try {',
  21423. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21424. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21425. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21426. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21427. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21428. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21429. ' i = rtl.setIntfL(i, j);',
  21430. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21431. ' o = rtl.intfToClass(i, $mod.TObject);',
  21432. ' } finally {',
  21433. ' rtl._Release(i);',
  21434. ' };',
  21435. '};',
  21436. '']),
  21437. LinesToStr([ // $mod.$main
  21438. '']));
  21439. end;
  21440. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21441. begin
  21442. StartProgram(false);
  21443. Add([
  21444. '{$interfaces com}',
  21445. 'type',
  21446. ' IUnknown = interface',
  21447. ' function _AddRef: longint;',
  21448. ' function _Release: longint;',
  21449. ' end;',
  21450. ' TObject = class(IUnknown)',
  21451. ' function _AddRef: longint; virtual; abstract;',
  21452. ' function _Release: longint; virtual; abstract;',
  21453. ' end;',
  21454. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21455. 'var o: TObject;',
  21456. 'begin',
  21457. ' DoIt(v,v,v,v);',
  21458. ' DoIt(o,o,k,k);',
  21459. 'end;',
  21460. 'procedure DoSome;',
  21461. 'var v: IUnknown;',
  21462. 'begin',
  21463. ' DoIt(v,v,v,v);',
  21464. 'end;',
  21465. 'var i: IUnknown;',
  21466. 'begin',
  21467. ' DoIt(i,i,i,i);',
  21468. '']);
  21469. ConvertProgram;
  21470. CheckSource('TestClassInterface_COM_PassAsArg',
  21471. LinesToStr([ // statements
  21472. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21473. 'rtl.createClass(this, "TObject", null, function () {',
  21474. ' this.$init = function () {',
  21475. ' };',
  21476. ' this.$final = function () {',
  21477. ' };',
  21478. ' rtl.addIntf(this, $mod.IUnknown);',
  21479. '});',
  21480. 'this.DoIt = function (v, j, k, l) {',
  21481. ' var o = null;',
  21482. ' var $ir = rtl.createIntfRefs();',
  21483. ' rtl._AddRef(v);',
  21484. ' try {',
  21485. ' $mod.DoIt(v, v, {',
  21486. ' get: function () {',
  21487. ' return v;',
  21488. ' },',
  21489. ' set: function (w) {',
  21490. ' v = rtl.setIntfL(v, w);',
  21491. ' }',
  21492. ' }, {',
  21493. ' get: function () {',
  21494. ' return v;',
  21495. ' },',
  21496. ' set: function (w) {',
  21497. ' v = rtl.setIntfL(v, w);',
  21498. ' }',
  21499. ' });',
  21500. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21501. ' } finally {',
  21502. ' $ir.free();',
  21503. ' rtl._Release(v);',
  21504. ' };',
  21505. '};',
  21506. 'this.DoSome = function () {',
  21507. ' var v = null;',
  21508. ' try {',
  21509. ' $mod.DoIt(v, v, {',
  21510. ' get: function () {',
  21511. ' return v;',
  21512. ' },',
  21513. ' set: function (w) {',
  21514. ' v = rtl.setIntfL(v, w);',
  21515. ' }',
  21516. ' }, {',
  21517. ' get: function () {',
  21518. ' return v;',
  21519. ' },',
  21520. ' set: function (w) {',
  21521. ' v = rtl.setIntfL(v, w);',
  21522. ' }',
  21523. ' });',
  21524. ' } finally {',
  21525. ' rtl._Release(v);',
  21526. ' };',
  21527. '};',
  21528. 'this.i = null;',
  21529. '']),
  21530. LinesToStr([ // $mod.$main
  21531. '$mod.DoIt($mod.i, $mod.i, {',
  21532. ' p: $mod,',
  21533. ' get: function () {',
  21534. ' return this.p.i;',
  21535. ' },',
  21536. ' set: function (v) {',
  21537. ' rtl.setIntfP(this.p, "i", v);',
  21538. ' }',
  21539. '}, {',
  21540. ' p: $mod,',
  21541. ' get: function () {',
  21542. ' return this.p.i;',
  21543. ' },',
  21544. ' set: function (v) {',
  21545. ' rtl.setIntfP(this.p, "i", v);',
  21546. ' }',
  21547. '});',
  21548. '']));
  21549. end;
  21550. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21551. begin
  21552. StartProgram(false);
  21553. Add([
  21554. '{$interfaces com}',
  21555. 'type',
  21556. ' IUnknown = interface',
  21557. ' function _AddRef: longint;',
  21558. ' function _Release: longint;',
  21559. ' end;',
  21560. ' TObject = class(IUnknown)',
  21561. ' function _AddRef: longint; virtual; abstract;',
  21562. ' function _Release: longint; virtual; abstract;',
  21563. ' end;',
  21564. 'procedure DoIt(out i);',
  21565. 'begin end;',
  21566. 'procedure DoSome;',
  21567. 'var v: IUnknown;',
  21568. 'begin',
  21569. ' DoIt(v);',
  21570. 'end;',
  21571. 'function GetIt: IUnknown;',
  21572. 'begin',
  21573. ' DoIt(Result);',
  21574. 'end;',
  21575. 'var i: IUnknown;',
  21576. 'begin',
  21577. ' DoIt(i);',
  21578. '']);
  21579. ConvertProgram;
  21580. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21581. LinesToStr([ // statements
  21582. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21583. 'rtl.createClass(this, "TObject", null, function () {',
  21584. ' this.$init = function () {',
  21585. ' };',
  21586. ' this.$final = function () {',
  21587. ' };',
  21588. ' rtl.addIntf(this, $mod.IUnknown);',
  21589. '});',
  21590. 'this.DoIt = function (i) {',
  21591. '};',
  21592. 'this.DoSome = function () {',
  21593. ' var v = null;',
  21594. ' try {',
  21595. ' $mod.DoIt({',
  21596. ' get: function () {',
  21597. ' return v;',
  21598. ' },',
  21599. ' set: function (w) {',
  21600. ' v = w;',
  21601. ' }',
  21602. ' });',
  21603. ' } finally {',
  21604. ' rtl._Release(v);',
  21605. ' };',
  21606. '};',
  21607. 'this.GetIt = function () {',
  21608. ' var Result = null;',
  21609. ' var $ok = false;',
  21610. ' try {',
  21611. ' $mod.DoIt({',
  21612. ' get: function () {',
  21613. ' return Result;',
  21614. ' },',
  21615. ' set: function (v) {',
  21616. ' Result = v;',
  21617. ' }',
  21618. ' });',
  21619. ' $ok = true;',
  21620. ' } finally {',
  21621. ' if (!$ok) rtl._Release(Result);',
  21622. ' };',
  21623. ' return Result;',
  21624. '};',
  21625. 'this.i = null;',
  21626. '']),
  21627. LinesToStr([ // $mod.$main
  21628. 'try {',
  21629. ' $mod.DoIt({',
  21630. ' p: $mod,',
  21631. ' get: function () {',
  21632. ' return this.p.i;',
  21633. ' },',
  21634. ' set: function (v) {',
  21635. ' this.p.i = v;',
  21636. ' }',
  21637. ' });',
  21638. '} finally {',
  21639. ' rtl._Release($mod.i);',
  21640. '};',
  21641. '']));
  21642. end;
  21643. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21644. begin
  21645. StartProgram(false);
  21646. Add([
  21647. '{$interfaces com}',
  21648. 'type',
  21649. ' IUnknown = interface',
  21650. ' function _AddRef: longint;',
  21651. ' function _Release: longint;',
  21652. ' end;',
  21653. ' TObject = class(IUnknown)',
  21654. ' function _AddRef: longint; virtual; abstract;',
  21655. ' function _Release: longint; virtual; abstract;',
  21656. ' end;',
  21657. 'function GetIt: IUnknown;',
  21658. 'begin',
  21659. 'end;',
  21660. 'procedure DoSome;',
  21661. 'var v: IUnknown;',
  21662. ' i: longint;',
  21663. 'begin',
  21664. ' v:=GetIt;',
  21665. ' v:=GetIt();',
  21666. ' GetIt()._AddRef;',
  21667. ' i:=GetIt()._AddRef;',
  21668. 'end;',
  21669. 'var v: IUnknown;',
  21670. ' i: longint;',
  21671. 'begin',
  21672. ' v:=GetIt;',
  21673. ' v:=GetIt();',
  21674. ' GetIt()._AddRef;',
  21675. ' i:=GetIt()._AddRef;',
  21676. '']);
  21677. ConvertProgram;
  21678. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21679. LinesToStr([ // statements
  21680. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21681. 'rtl.createClass(this, "TObject", null, function () {',
  21682. ' this.$init = function () {',
  21683. ' };',
  21684. ' this.$final = function () {',
  21685. ' };',
  21686. ' rtl.addIntf(this, $mod.IUnknown);',
  21687. '});',
  21688. 'this.GetIt = function () {',
  21689. ' var Result = null;',
  21690. ' return Result;',
  21691. '};',
  21692. 'this.DoSome = function () {',
  21693. ' var v = null;',
  21694. ' var i = 0;',
  21695. ' var $ir = rtl.createIntfRefs();',
  21696. ' try {',
  21697. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21698. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21699. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21700. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21701. ' } finally {',
  21702. ' $ir.free();',
  21703. ' rtl._Release(v);',
  21704. ' };',
  21705. '};',
  21706. 'this.v = null;',
  21707. 'this.i = 0;',
  21708. '']),
  21709. LinesToStr([ // $mod.$main
  21710. 'var $ir = rtl.createIntfRefs();',
  21711. 'try {',
  21712. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21713. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21714. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21715. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21716. '} finally {',
  21717. ' $ir.free();',
  21718. '};',
  21719. '']));
  21720. end;
  21721. procedure TTestModule.TestClassInterface_COM_Property;
  21722. begin
  21723. StartProgram(false);
  21724. Add([
  21725. '{$interfaces com}',
  21726. 'type',
  21727. ' IUnknown = interface',
  21728. ' function _AddRef: longint;',
  21729. ' function _Release: longint;',
  21730. ' end;',
  21731. ' TObject = class(IUnknown)',
  21732. ' FAnt: IUnknown;',
  21733. ' function _AddRef: longint; virtual; abstract;',
  21734. ' function _Release: longint; virtual; abstract;',
  21735. ' function GetBird: IUnknown; virtual; abstract;',
  21736. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21737. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21738. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21739. ' property Ant: IUnknown read FAnt write FAnt;',
  21740. ' property Bird: IUnknown read GetBird write SetBird;',
  21741. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21742. ' end;',
  21743. 'procedure DoIt;',
  21744. 'var',
  21745. ' o: TObject;',
  21746. ' v: IUnknown;',
  21747. 'begin',
  21748. ' v:=o.Ant;',
  21749. ' o.Ant:=v;',
  21750. ' o.Ant:=o.Ant;',
  21751. ' v:=o.Bird;',
  21752. ' o.Bird:=v;',
  21753. ' o.Bird:=o.Bird;',
  21754. ' v:=o.Items[1];',
  21755. ' o.Items[2]:=v;',
  21756. ' o.Items[3]:=o.Items[4];',
  21757. ' v:=o[5];',
  21758. ' o[6]:=v;',
  21759. ' o[7]:=o[8];',
  21760. 'end;',
  21761. 'begin',
  21762. '']);
  21763. ConvertProgram;
  21764. CheckSource('TestClassInterface_COM_Property',
  21765. LinesToStr([ // statements
  21766. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21767. 'rtl.createClass(this, "TObject", null, function () {',
  21768. ' this.$init = function () {',
  21769. ' this.FAnt = null;',
  21770. ' };',
  21771. ' this.$final = function () {',
  21772. ' rtl.setIntfP(this, "FAnt", null);',
  21773. ' };',
  21774. ' rtl.addIntf(this, $mod.IUnknown);',
  21775. '});',
  21776. 'this.DoIt = function () {',
  21777. ' var o = null;',
  21778. ' var v = null;',
  21779. ' var $ir = rtl.createIntfRefs();',
  21780. ' try {',
  21781. ' v = rtl.setIntfL(v, o.FAnt);',
  21782. ' rtl.setIntfP(o, "FAnt", v);',
  21783. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21784. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21785. ' o.SetBird(v);',
  21786. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21787. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21788. ' o.SetItems(2, v);',
  21789. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21790. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21791. ' o.SetItems(6, v);',
  21792. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21793. ' } finally {',
  21794. ' $ir.free();',
  21795. ' rtl._Release(v);',
  21796. ' };',
  21797. '};',
  21798. '']),
  21799. LinesToStr([ // $mod.$main
  21800. '']));
  21801. end;
  21802. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21803. begin
  21804. StartProgram(false);
  21805. Add([
  21806. '{$interfaces com}',
  21807. 'type',
  21808. ' IUnknown = interface',
  21809. ' function _AddRef: longint;',
  21810. ' function _Release: longint;',
  21811. ' function GetBird: IUnknown;',
  21812. ' procedure SetBird(Value: IUnknown);',
  21813. ' function GetItems(Index: longint): IUnknown;',
  21814. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21815. ' property Bird: IUnknown read GetBird write SetBird;',
  21816. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21817. ' end;',
  21818. ' TObject = class(IUnknown)',
  21819. ' function _AddRef: longint; virtual; abstract;',
  21820. ' function _Release: longint; virtual; abstract;',
  21821. ' function GetBird: IUnknown; virtual; abstract;',
  21822. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21823. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21824. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21825. ' end;',
  21826. 'procedure DoIt;',
  21827. 'var',
  21828. ' o: TObject;',
  21829. ' v: IUnknown;',
  21830. 'begin',
  21831. ' v:=v.Items[1];',
  21832. ' v.Items[2]:=v;',
  21833. ' v.Items[3]:=v.Items[4];',
  21834. ' v:=v[5];',
  21835. ' v[6]:=v;',
  21836. ' v[7]:=v[8];',
  21837. ' v[9].Bird.Bird:=v;',
  21838. ' v:=v.Bird[10].Bird',
  21839. 'end;',
  21840. 'begin',
  21841. '']);
  21842. ConvertProgram;
  21843. CheckSource('TestClassInterface_COM_IntfProperty',
  21844. LinesToStr([ // statements
  21845. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21846. ' "_AddRef",',
  21847. ' "_Release",',
  21848. ' "GetBird",',
  21849. ' "SetBird",',
  21850. ' "GetItems",',
  21851. ' "SetItems"',
  21852. '], null);',
  21853. 'rtl.createClass(this, "TObject", null, function () {',
  21854. ' this.$init = function () {',
  21855. ' };',
  21856. ' this.$final = function () {',
  21857. ' };',
  21858. ' rtl.addIntf(this, $mod.IUnknown);',
  21859. '});',
  21860. 'this.DoIt = function () {',
  21861. ' var o = null;',
  21862. ' var v = null;',
  21863. ' var $ir = rtl.createIntfRefs();',
  21864. ' try {',
  21865. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21866. ' v.SetItems(2, v);',
  21867. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21868. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21869. ' v.SetItems(6, v);',
  21870. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21871. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21872. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21873. ' } finally {',
  21874. ' $ir.free();',
  21875. ' rtl._Release(v);',
  21876. ' };',
  21877. '};',
  21878. '']),
  21879. LinesToStr([ // $mod.$main
  21880. '']));
  21881. end;
  21882. procedure TTestModule.TestClassInterface_COM_Delegation;
  21883. begin
  21884. StartProgram(false);
  21885. Add([
  21886. '{$interfaces com}',
  21887. 'type',
  21888. ' IUnknown = interface',
  21889. ' function _AddRef: longint;',
  21890. ' function _Release: longint;',
  21891. ' end;',
  21892. ' IBird = interface(IUnknown)',
  21893. ' procedure Fly(s: string);',
  21894. ' end;',
  21895. ' IEagle = interface(IBird) end;',
  21896. ' IDove = interface(IBird) end;',
  21897. ' ISwallow = interface(IBird) end;',
  21898. ' TObject = class',
  21899. ' end;',
  21900. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21901. ' function _AddRef: longint; virtual; abstract;',
  21902. ' function _Release: longint; virtual; abstract;',
  21903. ' procedure Fly(s: string); virtual; abstract;',
  21904. ' end;',
  21905. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21906. ' function _AddRef: longint; virtual; abstract;',
  21907. ' function _Release: longint; virtual; abstract;',
  21908. ' FBirdIntf: IBird;',
  21909. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21910. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21911. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21912. ' FDoveObj: TBird;',
  21913. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21914. ' function GetSwallowObj: TBird; virtual; abstract;',
  21915. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21916. ' end;',
  21917. 'begin',
  21918. '']);
  21919. ConvertProgram;
  21920. CheckSource('TestClassInterface_COM_Delegation',
  21921. LinesToStr([ // statements
  21922. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21923. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21924. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21925. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21926. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21927. 'rtl.createClass(this, "TObject", null, function () {',
  21928. ' this.$init = function () {',
  21929. ' };',
  21930. ' this.$final = function () {',
  21931. ' };',
  21932. '});',
  21933. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21934. ' rtl.addIntf(this, $mod.IBird);',
  21935. ' rtl.addIntf(this, $mod.IEagle);',
  21936. ' rtl.addIntf(this, $mod.IDove);',
  21937. ' rtl.addIntf(this, $mod.ISwallow);',
  21938. '});',
  21939. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21940. ' this.$init = function () {',
  21941. ' $mod.TObject.$init.call(this);',
  21942. ' this.FBirdIntf = null;',
  21943. ' this.FDoveObj = null;',
  21944. ' };',
  21945. ' this.$final = function () {',
  21946. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21947. ' this.FDoveObj = undefined;',
  21948. ' $mod.TObject.$final.call(this);',
  21949. ' };',
  21950. ' this.$intfmaps = {',
  21951. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  21952. ' return rtl._AddRef(this.FBirdIntf);',
  21953. ' },',
  21954. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  21955. ' return this.GetEagleIntf();',
  21956. ' },',
  21957. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  21958. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  21959. ' },',
  21960. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  21961. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  21962. ' }',
  21963. ' };',
  21964. '});',
  21965. '']),
  21966. LinesToStr([ // $mod.$main
  21967. '']));
  21968. end;
  21969. procedure TTestModule.TestClassInterface_COM_With;
  21970. begin
  21971. StartProgram(false);
  21972. Add([
  21973. '{$interfaces com}',
  21974. 'type',
  21975. ' IUnknown = interface',
  21976. ' function _AddRef: longint;',
  21977. ' function _Release: longint;',
  21978. ' function GetAnt: IUnknown;',
  21979. ' property Ant: IUnknown read GetAnt;',
  21980. ' end;',
  21981. ' TObject = class(IUnknown)',
  21982. ' function _AddRef: longint; virtual; abstract;',
  21983. ' function _Release: longint; virtual; abstract;',
  21984. ' function GetAnt: IUnknown; virtual; abstract;',
  21985. ' property Ant: IUnknown read GetAnt;',
  21986. ' end;',
  21987. 'procedure DoIt;',
  21988. 'var',
  21989. ' i: IUnknown;',
  21990. 'begin',
  21991. ' with i do ',
  21992. ' GetAnt;',
  21993. ' with i.Ant, Ant do ',
  21994. ' GetAnt;',
  21995. 'end;',
  21996. 'begin',
  21997. '']);
  21998. ConvertProgram;
  21999. CheckSource('TestClassInterface_COM_With',
  22000. LinesToStr([ // statements
  22001. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  22002. 'rtl.createClass(this, "TObject", null, function () {',
  22003. ' this.$init = function () {',
  22004. ' };',
  22005. ' this.$final = function () {',
  22006. ' };',
  22007. ' rtl.addIntf(this, $mod.IUnknown);',
  22008. '});',
  22009. 'this.DoIt = function () {',
  22010. ' var i = null;',
  22011. ' var $ir = rtl.createIntfRefs();',
  22012. ' try {',
  22013. ' $ir.ref(1, i.GetAnt());',
  22014. ' var $with = $ir.ref(2, i.GetAnt());',
  22015. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22016. ' $ir.ref(4, $with1.GetAnt());',
  22017. ' } finally {',
  22018. ' $ir.free();',
  22019. ' };',
  22020. '};',
  22021. '']),
  22022. LinesToStr([ // $mod.$main
  22023. '']));
  22024. end;
  22025. procedure TTestModule.TestClassInterface_COM_ForIn;
  22026. begin
  22027. StartProgram(false);
  22028. Add([
  22029. '{$interfaces com}',
  22030. 'type',
  22031. ' IUnknown = interface end;',
  22032. ' TObject = class',
  22033. ' Id: longint;',
  22034. ' end;',
  22035. ' IEnumerator = interface(IUnknown)',
  22036. ' function GetCurrent: TObject;',
  22037. ' function MoveNext: Boolean;',
  22038. ' property Current: TObject read GetCurrent;',
  22039. ' end;',
  22040. ' IEnumerable = interface(IUnknown)',
  22041. ' function GetEnumerator: IEnumerator;',
  22042. ' end;',
  22043. 'var',
  22044. ' o: TObject;',
  22045. ' i: IEnumerable;',
  22046. 'begin',
  22047. ' for o in i do o.Id:=3;',
  22048. '']);
  22049. ConvertProgram;
  22050. CheckSource('TestClassInterface_COM_ForIn',
  22051. LinesToStr([ // statements
  22052. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22053. 'rtl.createClass(this, "TObject", null, function () {',
  22054. ' this.$init = function () {',
  22055. ' this.Id = 0;',
  22056. ' };',
  22057. ' this.$final = function () {',
  22058. ' };',
  22059. '});',
  22060. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22061. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22062. 'this.o = null;',
  22063. 'this.i = null;',
  22064. '']),
  22065. LinesToStr([ // $mod.$main
  22066. 'var $in = $mod.i.GetEnumerator();',
  22067. 'try {',
  22068. ' while ($in.MoveNext()) {',
  22069. ' $mod.o = $in.GetCurrent();',
  22070. ' $mod.o.Id = 3;',
  22071. ' }',
  22072. '} finally {',
  22073. ' rtl._Release($in)',
  22074. '};',
  22075. '']));
  22076. end;
  22077. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf;
  22078. begin
  22079. {$IFNDEF EnableCOMArrayOfIntf}
  22080. exit;
  22081. {$ENDIF}
  22082. StartProgram(false);
  22083. Add([
  22084. '{$interfaces com}',
  22085. 'type',
  22086. ' IUnknown = interface end;',
  22087. ' IBird = interface(IUnknown)',
  22088. ' function Fly(w: word): word;',
  22089. ' end;',
  22090. ' TBirdArray = array of IBird;',
  22091. 'procedure Run;',
  22092. 'var',
  22093. ' i: IBird;',
  22094. ' a,b: TBirdArray;',
  22095. 'begin',
  22096. //' SetLength(a,3);',
  22097. ' a:=b;',
  22098. ' i:=a[1];',
  22099. ' a[2]:=i;',
  22100. //' for i in a do i.fly(3);',
  22101. // a:=copy(b,1,2);
  22102. // a:=concat(b,a);
  22103. // insert(i,b,1);
  22104. // a:=[i,i];
  22105. 'end;',
  22106. // ToDo: pass TBirdArray as arg
  22107. 'begin',
  22108. '']);
  22109. ConvertProgram;
  22110. CheckSource('TestClassInterface_COM_ArrayOfIntf',
  22111. LinesToStr([ // statements
  22112. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22113. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22114. 'this.Run = function () {',
  22115. ' var i = null;',
  22116. ' var a = [];',
  22117. ' var b = [];',
  22118. ' try {',
  22119. ' a = rtl.arrayRef(b);',
  22120. ' i = rtl.setIntfL(i, a[1]);',
  22121. ' rtl.setIntfP(a, 2, i);',
  22122. ' } finally {',
  22123. ' rtl._Release(i);',
  22124. ' rtl._ReleaseArray(a,1);',
  22125. ' };',
  22126. '};',
  22127. '']),
  22128. LinesToStr([ // $mod.$main
  22129. '']));
  22130. end;
  22131. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  22132. begin
  22133. StartProgram(false);
  22134. Add([
  22135. '{$interfaces com}',
  22136. 'type',
  22137. ' IUnknown = interface',
  22138. ' function _AddRef: longint;',
  22139. ' function _Release: longint;',
  22140. ' end;',
  22141. ' TObject = class',
  22142. ' end;',
  22143. ' TArrOfIntf = array of IUnknown;',
  22144. 'begin',
  22145. '']);
  22146. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  22147. ConvertProgram;
  22148. end;
  22149. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22150. begin
  22151. StartProgram(false);
  22152. Add([
  22153. '{$interfaces com}',
  22154. 'type',
  22155. ' IUnknown = interface',
  22156. ' function _AddRef: longint;',
  22157. ' function _Release: longint;',
  22158. ' end;',
  22159. ' TRec = record',
  22160. ' i: IUnknown;',
  22161. ' end;',
  22162. 'begin',
  22163. '']);
  22164. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22165. ConvertProgram;
  22166. end;
  22167. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22168. begin
  22169. StartUnit(false);
  22170. Add([
  22171. '{$interfaces com}',
  22172. 'interface',
  22173. 'implementation',
  22174. 'type',
  22175. ' IUnknown = interface',
  22176. ' function _AddRef: longint;',
  22177. ' end;',
  22178. ' TObject = class(IUnknown)',
  22179. ' function _AddRef: longint;',
  22180. ' end;',
  22181. 'function TObject._AddRef: longint; begin end;',
  22182. 'var i: IUnknown;',
  22183. ' o: TObject;',
  22184. 'initialization',
  22185. ' i:=nil;',
  22186. ' i:=i;',
  22187. ' i:=o;',
  22188. ' if (o as IUnknown)=nil then ;',
  22189. '']);
  22190. ConvertUnit;
  22191. CheckSource('TestClassInterface_COM_UnitInitialization',
  22192. LinesToStr([ // statements
  22193. 'var $impl = $mod.$impl;',
  22194. '']),
  22195. LinesToStr([ // this.$init
  22196. 'var $ir = rtl.createIntfRefs();',
  22197. 'try {',
  22198. ' rtl.setIntfP($impl, "i", null);',
  22199. ' rtl.setIntfP($impl, "i", $impl.i);',
  22200. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22201. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22202. '} finally {',
  22203. ' $ir.free();',
  22204. '};',
  22205. '']),
  22206. LinesToStr([ // implementation
  22207. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22208. 'rtl.createClass($impl, "TObject", null, function () {',
  22209. ' this.$init = function () {',
  22210. ' };',
  22211. ' this.$final = function () {',
  22212. ' };',
  22213. ' this._AddRef = function () {',
  22214. ' var Result = 0;',
  22215. ' return Result;',
  22216. ' };',
  22217. ' rtl.addIntf(this, $impl.IUnknown);',
  22218. '});',
  22219. '$impl.i = null;',
  22220. '$impl.o = null;',
  22221. ''])
  22222. );
  22223. end;
  22224. procedure TTestModule.TestClassInterface_Corba_GUID;
  22225. begin
  22226. StartProgram(false);
  22227. Add([
  22228. '{$interfaces corba}',
  22229. 'type',
  22230. ' IUnknown = interface',
  22231. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22232. ' end;',
  22233. ' TObject = class end;',
  22234. ' TGUID = record D1, D2, D3, D4: word; end;',
  22235. ' TAliasGUID = TGUID;',
  22236. ' TGUIDString = type string;',
  22237. ' TAliasGUIDString = TGUIDString;',
  22238. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  22239. 'begin end;',
  22240. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  22241. 'begin end;',
  22242. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  22243. 'begin end;',
  22244. 'var',
  22245. ' i: IUnknown;',
  22246. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  22247. ' s: TAliasGUIDString;',
  22248. 'begin',
  22249. ' DoConstGUIDIt(IUnknown);',
  22250. ' DoDefGUID(IUnknown);',
  22251. ' DoStr(IUnknown);',
  22252. ' DoConstGUIDIt(i);',
  22253. ' DoDefGUID(i);',
  22254. ' DoStr(i);',
  22255. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22256. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22257. ' DoStr(g);',
  22258. ' g:=i;',
  22259. ' g:=IUnknown;',
  22260. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22261. ' s:=i;',
  22262. ' s:=IUnknown;',
  22263. ' s:=g;',
  22264. ' if g=i then ;',
  22265. ' if i=g then ;',
  22266. ' if g=IUnknown then ;',
  22267. ' if IUnknown=g then ;',
  22268. ' if s=i then ;',
  22269. ' if i=s then ;',
  22270. ' if s=IUnknown then ;',
  22271. ' if IUnknown=s then ;',
  22272. ' if s=g then ;',
  22273. ' if g=s then ;',
  22274. '']);
  22275. ConvertProgram;
  22276. CheckSource('TestClassInterface_Corba_GUID',
  22277. LinesToStr([ // statements
  22278. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22279. 'rtl.createClass(this, "TObject", null, function () {',
  22280. ' this.$init = function () {',
  22281. ' };',
  22282. ' this.$final = function () {',
  22283. ' };',
  22284. '});',
  22285. 'rtl.recNewT(this, "TGUID", function () {',
  22286. ' this.D1 = 0;',
  22287. ' this.D2 = 0;',
  22288. ' this.D3 = 0;',
  22289. ' this.D4 = 0;',
  22290. ' this.$eq = function (b) {',
  22291. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22292. ' };',
  22293. ' this.$assign = function (s) {',
  22294. ' this.D1 = s.D1;',
  22295. ' this.D2 = s.D2;',
  22296. ' this.D3 = s.D3;',
  22297. ' this.D4 = s.D4;',
  22298. ' return this;',
  22299. ' };',
  22300. '});',
  22301. 'this.DoConstGUIDIt = function (g) {',
  22302. '};',
  22303. 'this.DoDefGUID = function (g) {',
  22304. '};',
  22305. 'this.DoStr = function (s) {',
  22306. '};',
  22307. 'this.i = null;',
  22308. 'this.g = this.TGUID.$clone({',
  22309. ' D1: 0xD91C9AF4,',
  22310. ' D2: 0x3C93,',
  22311. ' D3: 0x420F,',
  22312. ' D4: [',
  22313. ' 0xA3,',
  22314. ' 0x03,',
  22315. ' 0xBF,',
  22316. ' 0x5B,',
  22317. ' 0xA8,',
  22318. ' 0x2B,',
  22319. ' 0xFD,',
  22320. ' 0x23',
  22321. ' ]',
  22322. '});',
  22323. 'this.s = "";',
  22324. '']),
  22325. LinesToStr([ // $mod.$main
  22326. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  22327. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  22328. '$mod.DoStr($mod.IUnknown.$guid);',
  22329. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  22330. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  22331. '$mod.DoStr($mod.i.$guid);',
  22332. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22333. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22334. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  22335. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  22336. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  22337. '$mod.g.$assign({',
  22338. ' D1: 0xD91C9AF4,',
  22339. ' D2: 0x3C93,',
  22340. ' D3: 0x420F,',
  22341. ' D4: [',
  22342. ' 0xA3,',
  22343. ' 0x03,',
  22344. ' 0xBF,',
  22345. ' 0x5B,',
  22346. ' 0xA8,',
  22347. ' 0x2B,',
  22348. ' 0xFD,',
  22349. ' 0x23',
  22350. ' ]',
  22351. '});',
  22352. '$mod.s = $mod.i.$guid;',
  22353. '$mod.s = $mod.IUnknown.$guid;',
  22354. '$mod.s = rtl.guidrToStr($mod.g);',
  22355. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22356. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22357. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22358. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22359. 'if ($mod.s === $mod.i.$guid) ;',
  22360. 'if ($mod.i.$guid === $mod.s) ;',
  22361. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22362. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22363. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22364. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22365. '']));
  22366. end;
  22367. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  22368. begin
  22369. StartProgram(false);
  22370. Add([
  22371. '{$interfaces corba}',
  22372. 'type',
  22373. ' IUnknown = interface',
  22374. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22375. ' end;',
  22376. ' TGUID = record D1, D2, D3, D4: word; end;',
  22377. ' TAliasGUID = TGUID;',
  22378. ' TGUIDString = type string;',
  22379. ' TAliasGUIDString = TGUIDString;',
  22380. ' TObject = class',
  22381. ' function GetG: TAliasGUID; virtual; abstract;',
  22382. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22383. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22384. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22385. ' property g: TAliasGUID read GetG write SetG;',
  22386. ' property s: TAliasGUIDString read GetS write SetS;',
  22387. ' end;',
  22388. 'var o: TObject;',
  22389. 'begin',
  22390. ' o.g:=IUnknown;',
  22391. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22392. ' o.s:=IUnknown;',
  22393. ' o.s:=o.g;',
  22394. '']);
  22395. ConvertProgram;
  22396. CheckSource('TestClassInterface_GUIDProperty',
  22397. LinesToStr([ // statements
  22398. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22399. 'rtl.recNewT(this, "TGUID", function () {',
  22400. ' this.D1 = 0;',
  22401. ' this.D2 = 0;',
  22402. ' this.D3 = 0;',
  22403. ' this.D4 = 0;',
  22404. ' this.$eq = function (b) {',
  22405. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22406. ' };',
  22407. ' this.$assign = function (s) {',
  22408. ' this.D1 = s.D1;',
  22409. ' this.D2 = s.D2;',
  22410. ' this.D3 = s.D3;',
  22411. ' this.D4 = s.D4;',
  22412. ' return this;',
  22413. ' };',
  22414. '});',
  22415. 'rtl.createClass(this, "TObject", null, function () {',
  22416. ' this.$init = function () {',
  22417. ' };',
  22418. ' this.$final = function () {',
  22419. ' };',
  22420. '});',
  22421. 'this.o = null;',
  22422. '']),
  22423. LinesToStr([ // $mod.$main
  22424. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22425. '$mod.o.SetG({',
  22426. ' D1: 0xD91C9AF4,',
  22427. ' D2: 0x3C93,',
  22428. ' D3: 0x420F,',
  22429. ' D4: [',
  22430. ' 0xA3,',
  22431. ' 0x03,',
  22432. ' 0xBF,',
  22433. ' 0x5B,',
  22434. ' 0xA8,',
  22435. ' 0x2B,',
  22436. ' 0xFD,',
  22437. ' 0x23',
  22438. ' ]',
  22439. '});',
  22440. '$mod.o.SetS($mod.IUnknown.$guid);',
  22441. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22442. '']));
  22443. end;
  22444. procedure TTestModule.TestClassHelper_ClassVar;
  22445. begin
  22446. StartProgram(false);
  22447. Add([
  22448. 'type',
  22449. ' TObject = class',
  22450. ' end;',
  22451. ' THelper = class helper for TObject',
  22452. ' const',
  22453. ' One = 1;',
  22454. ' Two: word = 2;',
  22455. ' class var',
  22456. ' Glob: word;',
  22457. ' function Foo(w: word): word;',
  22458. ' class function Bar(w: word): word;',
  22459. ' end;',
  22460. 'function THelper.foo(w: word): word;',
  22461. 'begin',
  22462. ' Result:=w;',
  22463. ' Two:=One+w;',
  22464. ' Glob:=Glob;',
  22465. ' Result:=Self.Glob;',
  22466. ' Self.Glob:=Self.Glob;',
  22467. ' with Self do Glob:=Glob;',
  22468. 'end;',
  22469. 'class function THelper.bar(w: word): word;',
  22470. 'begin',
  22471. ' Result:=w;',
  22472. ' Two:=One;',
  22473. ' Glob:=Glob;',
  22474. ' Self.Glob:=Self.Glob;',
  22475. ' with Self do Glob:=Glob;',
  22476. 'end;',
  22477. 'var o: TObject;',
  22478. 'begin',
  22479. ' tobject.two:=tobject.one;',
  22480. ' tobject.Glob:=tobject.Glob;',
  22481. ' with tobject do begin',
  22482. ' two:=one;',
  22483. ' Glob:=Glob;',
  22484. ' end;',
  22485. ' o.two:=o.one;',
  22486. ' o.Glob:=o.Glob;',
  22487. ' with o do begin',
  22488. ' two:=one;',
  22489. ' Glob:=Glob;',
  22490. ' end;',
  22491. '']);
  22492. ConvertProgram;
  22493. CheckSource('TestClassHelper_ClassVar',
  22494. LinesToStr([ // statements
  22495. 'rtl.createClass(this, "TObject", null, function () {',
  22496. ' this.$init = function () {',
  22497. ' };',
  22498. ' this.$final = function () {',
  22499. ' };',
  22500. '});',
  22501. 'rtl.createHelper(this, "THelper", null, function () {',
  22502. ' this.One = 1;',
  22503. ' this.Two = 2;',
  22504. ' this.Glob = 0;',
  22505. ' this.Foo = function (w) {',
  22506. ' var Result = 0;',
  22507. ' Result = w;',
  22508. ' $mod.THelper.Two = 1 + w;',
  22509. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22510. ' Result = $mod.THelper.Glob;',
  22511. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22512. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22513. ' return Result;',
  22514. ' };',
  22515. ' this.Bar = function (w) {',
  22516. ' var Result = 0;',
  22517. ' Result = w;',
  22518. ' $mod.THelper.Two = 1;',
  22519. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22520. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22521. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22522. ' return Result;',
  22523. ' };',
  22524. '});',
  22525. 'this.o = null;',
  22526. '']),
  22527. LinesToStr([ // $mod.$main
  22528. '$mod.THelper.Two = 1;',
  22529. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22530. 'var $with = $mod.TObject;',
  22531. '$mod.THelper.Two = 1;',
  22532. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22533. '$mod.THelper.Two = 1;',
  22534. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22535. 'var $with1 = $mod.o;',
  22536. '$mod.THelper.Two = 1;',
  22537. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22538. '']));
  22539. end;
  22540. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22541. begin
  22542. StartProgram(false);
  22543. Add([
  22544. 'type',
  22545. ' TObject = class',
  22546. ' FSize: word;',
  22547. ' property Size: word read FSize write FSize;',
  22548. ' end;',
  22549. ' THelper = class helper for TObject',
  22550. ' function Foo(w: word = 1): word;',
  22551. ' end;',
  22552. 'function THelper.foo(w: word): word;',
  22553. 'begin',
  22554. ' Result:=Size;',
  22555. ' Size:=Size+2;',
  22556. ' Self.Size:=Self.Size+3;',
  22557. ' FSize:=FSize+4;',
  22558. ' Self.FSize:=Self.FSize+5;',
  22559. ' with Self do begin',
  22560. ' Size:=Size+6;',
  22561. ' FSize:=FSize+7;',
  22562. ' FSize:=FSize+8;',
  22563. ' end;',
  22564. 'end;',
  22565. 'begin',
  22566. '']);
  22567. ConvertProgram;
  22568. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22569. LinesToStr([ // statements
  22570. 'rtl.createClass(this, "TObject", null, function () {',
  22571. ' this.$init = function () {',
  22572. ' this.FSize = 0;',
  22573. ' };',
  22574. ' this.$final = function () {',
  22575. ' };',
  22576. '});',
  22577. 'rtl.createHelper(this, "THelper", null, function () {',
  22578. ' this.Foo = function (w) {',
  22579. ' var Result = 0;',
  22580. ' Result = this.FSize;',
  22581. ' this.FSize = this.FSize + 2;',
  22582. ' this.FSize = this.FSize + 3;',
  22583. ' this.FSize = this.FSize + 4;',
  22584. ' this.FSize = this.FSize + 5;',
  22585. ' this.FSize = this.FSize + 6;',
  22586. ' this.FSize = this.FSize + 7;',
  22587. ' this.FSize = this.FSize + 8;',
  22588. ' return Result;',
  22589. ' };',
  22590. '});',
  22591. '']),
  22592. LinesToStr([ // $mod.$main
  22593. '']));
  22594. end;
  22595. procedure TTestModule.TestClassHelper_Method_Call;
  22596. begin
  22597. StartProgram(false);
  22598. Add([
  22599. 'type',
  22600. ' TObject = class',
  22601. ' procedure Run(w: word = 10);',
  22602. ' end;',
  22603. ' THelper = class helper for TObject',
  22604. ' function Foo(w: word = 1): word;',
  22605. ' end;',
  22606. 'procedure TObject.Run(w: word);',
  22607. 'var o: TObject;',
  22608. 'begin',
  22609. ' Foo;',
  22610. ' Foo();',
  22611. ' Foo(2);',
  22612. ' Self.Foo;',
  22613. ' Self.Foo();',
  22614. ' Self.Foo(3);',
  22615. ' with Self do begin',
  22616. ' Foo;',
  22617. ' Foo();',
  22618. ' Foo(4);',
  22619. ' end;',
  22620. ' with o do Foo(5);',
  22621. 'end;',
  22622. 'function THelper.foo(w: word): word;',
  22623. 'begin',
  22624. ' Run;',
  22625. ' Run();',
  22626. ' Run(11);',
  22627. ' Foo;',
  22628. ' Foo();',
  22629. ' Foo(12);',
  22630. ' Self.Foo;',
  22631. ' Self.Foo();',
  22632. ' Self.Foo(13);',
  22633. ' with Self do begin',
  22634. ' Foo;',
  22635. ' Foo();',
  22636. ' Foo(14);',
  22637. ' end;',
  22638. 'end;',
  22639. 'var Obj: TObject;',
  22640. 'begin',
  22641. ' obj.Foo;',
  22642. ' obj.Foo();',
  22643. ' obj.Foo(21);',
  22644. ' with obj do begin',
  22645. ' Foo;',
  22646. ' Foo();',
  22647. ' Foo(22);',
  22648. ' end;',
  22649. '']);
  22650. ConvertProgram;
  22651. CheckSource('TestClassHelper_Method_Call',
  22652. LinesToStr([ // statements
  22653. 'rtl.createClass(this, "TObject", null, function () {',
  22654. ' this.$init = function () {',
  22655. ' };',
  22656. ' this.$final = function () {',
  22657. ' };',
  22658. ' this.Run = function (w) {',
  22659. ' var o = null;',
  22660. ' $mod.THelper.Foo.call(this, 1);',
  22661. ' $mod.THelper.Foo.call(this, 1);',
  22662. ' $mod.THelper.Foo.call(this, 2);',
  22663. ' $mod.THelper.Foo.call(this, 1);',
  22664. ' $mod.THelper.Foo.call(this, 1);',
  22665. ' $mod.THelper.Foo.call(this, 3);',
  22666. ' $mod.THelper.Foo.call(this, 1);',
  22667. ' $mod.THelper.Foo.call(this, 1);',
  22668. ' $mod.THelper.Foo.call(this, 4);',
  22669. ' $mod.THelper.Foo.call(o, 5);',
  22670. ' };',
  22671. '});',
  22672. 'rtl.createHelper(this, "THelper", null, function () {',
  22673. ' this.Foo = function (w) {',
  22674. ' var Result = 0;',
  22675. ' this.Run(10);',
  22676. ' this.Run(10);',
  22677. ' this.Run(11);',
  22678. ' $mod.THelper.Foo.call(this, 1);',
  22679. ' $mod.THelper.Foo.call(this, 1);',
  22680. ' $mod.THelper.Foo.call(this, 12);',
  22681. ' $mod.THelper.Foo.call(this, 1);',
  22682. ' $mod.THelper.Foo.call(this, 1);',
  22683. ' $mod.THelper.Foo.call(this, 13);',
  22684. ' $mod.THelper.Foo.call(this, 1);',
  22685. ' $mod.THelper.Foo.call(this, 1);',
  22686. ' $mod.THelper.Foo.call(this, 14);',
  22687. ' return Result;',
  22688. ' };',
  22689. '});',
  22690. 'this.Obj = null;',
  22691. '']),
  22692. LinesToStr([ // $mod.$main
  22693. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22694. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22695. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22696. 'var $with = $mod.Obj;',
  22697. '$mod.THelper.Foo.call($with, 1);',
  22698. '$mod.THelper.Foo.call($with, 1);',
  22699. '$mod.THelper.Foo.call($with, 22);',
  22700. '']));
  22701. end;
  22702. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22703. begin
  22704. StartProgram(false);
  22705. Add([
  22706. 'type',
  22707. ' TObject = class',
  22708. ' procedure Run(w: word = 10);',
  22709. ' end;',
  22710. ' THelper = class helper for TObject',
  22711. ' function Foo(w: word = 1): word;',
  22712. ' end;',
  22713. 'procedure TObject.Run(w: word);',
  22714. ' procedure Sub(Self: TObject);',
  22715. ' begin',
  22716. ' Foo;',
  22717. ' Foo();',
  22718. ' Self.Foo;',
  22719. ' Self.Foo();',
  22720. ' with Self do begin',
  22721. ' Foo;',
  22722. ' Foo();',
  22723. ' end;',
  22724. ' end;',
  22725. 'begin',
  22726. 'end;',
  22727. 'function THelper.foo(w: word): word;',
  22728. ' procedure Sub(Self: TObject);',
  22729. ' begin',
  22730. ' Run;',
  22731. ' Run();',
  22732. ' Foo;',
  22733. ' Foo();',
  22734. ' Self.Foo;',
  22735. ' Self.Foo();',
  22736. ' with Self do begin',
  22737. ' Foo;',
  22738. ' Foo();',
  22739. ' end;',
  22740. ' end;',
  22741. 'begin',
  22742. 'end;',
  22743. 'begin',
  22744. '']);
  22745. ConvertProgram;
  22746. CheckSource('TestClassHelper_Method_Nested_Call',
  22747. LinesToStr([ // statements
  22748. 'rtl.createClass(this, "TObject", null, function () {',
  22749. ' this.$init = function () {',
  22750. ' };',
  22751. ' this.$final = function () {',
  22752. ' };',
  22753. ' this.Run = function (w) {',
  22754. ' var $Self = this;',
  22755. ' function Sub(Self) {',
  22756. ' $mod.THelper.Foo.call($Self, 1);',
  22757. ' $mod.THelper.Foo.call($Self, 1);',
  22758. ' $mod.THelper.Foo.call(Self, 1);',
  22759. ' $mod.THelper.Foo.call(Self, 1);',
  22760. ' $mod.THelper.Foo.call(Self, 1);',
  22761. ' $mod.THelper.Foo.call(Self, 1);',
  22762. ' };',
  22763. ' };',
  22764. '});',
  22765. 'rtl.createHelper(this, "THelper", null, function () {',
  22766. ' this.Foo = function (w) {',
  22767. ' var $Self = this;',
  22768. ' var Result = 0;',
  22769. ' function Sub(Self) {',
  22770. ' $Self.Run(10);',
  22771. ' $Self.Run(10);',
  22772. ' $mod.THelper.Foo.call($Self, 1);',
  22773. ' $mod.THelper.Foo.call($Self, 1);',
  22774. ' $mod.THelper.Foo.call(Self, 1);',
  22775. ' $mod.THelper.Foo.call(Self, 1);',
  22776. ' $mod.THelper.Foo.call(Self, 1);',
  22777. ' $mod.THelper.Foo.call(Self, 1);',
  22778. ' };',
  22779. ' return Result;',
  22780. ' };',
  22781. '});',
  22782. '']),
  22783. LinesToStr([ // $mod.$main
  22784. '']));
  22785. end;
  22786. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22787. begin
  22788. StartProgram(false);
  22789. Add([
  22790. 'type',
  22791. ' TObject = class',
  22792. ' class procedure Run(w: word = 10);',
  22793. ' end;',
  22794. ' THelper = class helper for TObject',
  22795. ' class function Foo(w: word = 1): word;',
  22796. ' end;',
  22797. 'class procedure TObject.Run(w: word);',
  22798. 'begin',
  22799. ' Foo;',
  22800. ' Foo();',
  22801. ' Self.Foo;',
  22802. ' Self.Foo();',
  22803. ' with Self do begin',
  22804. ' Foo;',
  22805. ' Foo();',
  22806. ' end;',
  22807. 'end;',
  22808. 'class function THelper.foo(w: word): word;',
  22809. 'begin',
  22810. ' Run;',
  22811. ' Run();',
  22812. ' Foo;',
  22813. ' Foo();',
  22814. ' Self.Foo;',
  22815. ' Self.Foo();',
  22816. ' with Self do begin',
  22817. ' Foo;',
  22818. ' Foo();',
  22819. ' end;',
  22820. 'end;',
  22821. 'var',
  22822. ' Obj: TObject;',
  22823. 'begin',
  22824. ' obj.Foo;',
  22825. ' obj.Foo();',
  22826. ' with obj do begin',
  22827. ' Foo;',
  22828. ' Foo();',
  22829. ' end;',
  22830. ' tobject.Foo;',
  22831. ' tobject.Foo();',
  22832. ' with tobject do begin',
  22833. ' Foo;',
  22834. ' Foo();',
  22835. ' end;',
  22836. '']);
  22837. ConvertProgram;
  22838. CheckSource('TestClassHelper_ClassMethod_Call',
  22839. LinesToStr([ // statements
  22840. 'rtl.createClass(this, "TObject", null, function () {',
  22841. ' this.$init = function () {',
  22842. ' };',
  22843. ' this.$final = function () {',
  22844. ' };',
  22845. ' this.Run = function (w) {',
  22846. ' $mod.THelper.Foo.call(this, 1);',
  22847. ' $mod.THelper.Foo.call(this, 1);',
  22848. ' $mod.THelper.Foo.call(this, 1);',
  22849. ' $mod.THelper.Foo.call(this, 1);',
  22850. ' $mod.THelper.Foo.call(this, 1);',
  22851. ' $mod.THelper.Foo.call(this, 1);',
  22852. ' };',
  22853. '});',
  22854. 'rtl.createHelper(this, "THelper", null, function () {',
  22855. ' this.Foo = function (w) {',
  22856. ' var Result = 0;',
  22857. ' this.Run(10);',
  22858. ' this.Run(10);',
  22859. ' $mod.THelper.Foo.call(this, 1);',
  22860. ' $mod.THelper.Foo.call(this, 1);',
  22861. ' $mod.THelper.Foo.call(this, 1);',
  22862. ' $mod.THelper.Foo.call(this, 1);',
  22863. ' $mod.THelper.Foo.call(this, 1);',
  22864. ' $mod.THelper.Foo.call(this, 1);',
  22865. ' return Result;',
  22866. ' };',
  22867. '});',
  22868. 'this.Obj = null;',
  22869. '']),
  22870. LinesToStr([ // $mod.$main
  22871. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22872. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22873. 'var $with = $mod.Obj;',
  22874. '$mod.THelper.Foo.call($with.$class, 1);',
  22875. '$mod.THelper.Foo.call($with.$class, 1);',
  22876. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22877. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22878. 'var $with1 = $mod.TObject;',
  22879. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22880. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22881. '']));
  22882. end;
  22883. procedure TTestModule.TestClassHelper_ClassOf;
  22884. begin
  22885. StartProgram(false);
  22886. Add([
  22887. 'type',
  22888. ' TObject = class',
  22889. ' end;',
  22890. ' TClass = class of TObject;',
  22891. ' THelper = class helper for TObject',
  22892. ' class function Foo(w: word = 1): word;',
  22893. ' end;',
  22894. 'class function THelper.foo(w: word): word;',
  22895. 'begin',
  22896. 'end;',
  22897. 'var',
  22898. ' c: TClass;',
  22899. 'begin',
  22900. ' c.Foo;',
  22901. ' c.Foo();',
  22902. ' with c do begin',
  22903. ' Foo;',
  22904. ' Foo();',
  22905. ' end;',
  22906. '']);
  22907. ConvertProgram;
  22908. CheckSource('TestClassHelper_ClassOf',
  22909. LinesToStr([ // statements
  22910. 'rtl.createClass(this, "TObject", null, function () {',
  22911. ' this.$init = function () {',
  22912. ' };',
  22913. ' this.$final = function () {',
  22914. ' };',
  22915. '});',
  22916. 'rtl.createHelper(this, "THelper", null, function () {',
  22917. ' this.Foo = function (w) {',
  22918. ' var Result = 0;',
  22919. ' return Result;',
  22920. ' };',
  22921. '});',
  22922. 'this.c = null;',
  22923. '']),
  22924. LinesToStr([ // $mod.$main
  22925. '$mod.THelper.Foo.call($mod.c, 1);',
  22926. '$mod.THelper.Foo.call($mod.c, 1);',
  22927. 'var $with = $mod.c;',
  22928. '$mod.THelper.Foo.call($with, 1);',
  22929. '$mod.THelper.Foo.call($with, 1);',
  22930. '']));
  22931. end;
  22932. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22933. begin
  22934. StartProgram(false);
  22935. Add([
  22936. '{$mode objfpc}',
  22937. 'type',
  22938. ' TObject = class',
  22939. ' procedure DoIt;',
  22940. ' end;',
  22941. ' THelper = class helper for TObject',
  22942. ' procedure Fly(w: word = 1);',
  22943. ' class procedure Glide(w: word = 1);',
  22944. ' class procedure Run(w: word = 1); static;',
  22945. ' end;',
  22946. ' TFly = procedure(w: word) of object;',
  22947. ' TGlide = TFly;',
  22948. ' TRun = procedure(w: word);',
  22949. 'var',
  22950. ' f: TFly;',
  22951. ' g: TGlide;',
  22952. ' r: TRun;',
  22953. 'procedure TObject.DoIt;',
  22954. 'begin',
  22955. ' f:=@fly;',
  22956. ' g:=@glide;',
  22957. ' r:=@run;',
  22958. ' f:[email protected];',
  22959. ' g:[email protected];',
  22960. ' r:[email protected];',
  22961. ' with self do begin',
  22962. ' f:=@fly;',
  22963. ' g:=@glide;',
  22964. ' r:=@run;',
  22965. ' end;',
  22966. 'end;',
  22967. 'procedure THelper.fly(w: word);',
  22968. 'begin',
  22969. ' f:=@fly;',
  22970. ' g:=@glide;',
  22971. ' r:=@run;',
  22972. 'end;',
  22973. 'class procedure THelper.glide(w: word);',
  22974. 'begin',
  22975. ' g:=@glide;',
  22976. ' r:=@run;',
  22977. 'end;',
  22978. 'class procedure THelper.run(w: word);',
  22979. 'begin',
  22980. ' g:=@glide;',
  22981. ' r:=@run;',
  22982. 'end;',
  22983. 'var',
  22984. ' Obj: TObject;',
  22985. 'begin',
  22986. ' f:[email protected];',
  22987. ' g:[email protected];',
  22988. ' r:[email protected];',
  22989. ' with obj do begin',
  22990. ' f:=@fly;',
  22991. ' g:=@glide;',
  22992. ' r:=@run;',
  22993. ' end;',
  22994. ' g:[email protected];',
  22995. ' r:[email protected];',
  22996. ' with tobject do begin',
  22997. ' g:=@glide;',
  22998. ' r:=@run;',
  22999. ' end;',
  23000. '']);
  23001. ConvertProgram;
  23002. CheckSource('TestClassHelper_MethodRefObjFPC',
  23003. LinesToStr([ // statements
  23004. 'rtl.createClass(this, "TObject", null, function () {',
  23005. ' this.$init = function () {',
  23006. ' };',
  23007. ' this.$final = function () {',
  23008. ' };',
  23009. ' this.DoIt = function () {',
  23010. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23011. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23012. ' $mod.r = $mod.THelper.Run;',
  23013. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23014. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23015. ' $mod.r = $mod.THelper.Run;',
  23016. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23017. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23018. ' $mod.r = $mod.THelper.Run;',
  23019. ' };',
  23020. '});',
  23021. 'rtl.createHelper(this, "THelper", null, function () {',
  23022. ' this.Fly = function (w) {',
  23023. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23024. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23025. ' $mod.r = $mod.THelper.Run;',
  23026. ' };',
  23027. ' this.Glide = function (w) {',
  23028. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23029. ' $mod.r = $mod.THelper.Run;',
  23030. ' };',
  23031. ' this.Run = function (w) {',
  23032. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23033. ' $mod.r = $mod.THelper.Run;',
  23034. ' };',
  23035. '});',
  23036. 'this.f = null;',
  23037. 'this.g = null;',
  23038. 'this.r = null;',
  23039. 'this.Obj = null;',
  23040. '']),
  23041. LinesToStr([ // $mod.$main
  23042. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23043. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23044. '$mod.r = $mod.THelper.Run;',
  23045. 'var $with = $mod.Obj;',
  23046. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23047. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23048. '$mod.r = $mod.THelper.Run;',
  23049. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23050. '$mod.r = $mod.THelper.Run;',
  23051. 'var $with1 = $mod.TObject;',
  23052. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23053. '$mod.r = $mod.THelper.Run;',
  23054. '']));
  23055. end;
  23056. procedure TTestModule.TestClassHelper_Constructor;
  23057. begin
  23058. StartProgram(false);
  23059. Add([
  23060. 'type',
  23061. ' TObject = class',
  23062. ' constructor Create;',
  23063. ' end;',
  23064. ' TClass = class of TObject;',
  23065. ' THelper = class helper for TObject',
  23066. ' constructor NewHlp(w: word);',
  23067. ' end;',
  23068. 'var',
  23069. ' obj: TObject;',
  23070. ' c: TClass;',
  23071. 'constructor TObject.Create;',
  23072. 'begin',
  23073. ' NewHlp(2);', // normal call
  23074. ' tobject.NewHlp(3);', // new instance
  23075. ' c.newhlp(4);', // new instance
  23076. 'end;',
  23077. 'constructor THelper.NewHlp(w: word);',
  23078. 'begin',
  23079. ' create;', // normal call
  23080. ' tobject.create;', // new instance
  23081. ' NewHlp(2);', // normal call
  23082. ' tobject.NewHlp(3);', // new instance
  23083. ' c.newhlp(4);', // new instance
  23084. 'end;',
  23085. 'begin',
  23086. ' obj.newhlp(2);', // normal call
  23087. ' with Obj do newhlp(12);', // normal call
  23088. ' tobject.newhlp(3);', // new instance
  23089. ' with tobject do newhlp(13);', // new instance
  23090. ' c.newhlp(4);', // new instance
  23091. ' with c do newhlp(14);', // new instance
  23092. '']);
  23093. ConvertProgram;
  23094. CheckSource('TestClassHelper_Constructor',
  23095. LinesToStr([ // statements
  23096. 'rtl.createClass(this, "TObject", null, function () {',
  23097. ' this.$init = function () {',
  23098. ' };',
  23099. ' this.$final = function () {',
  23100. ' };',
  23101. ' this.Create = function () {',
  23102. ' $mod.THelper.NewHlp.call(this, 2);',
  23103. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23104. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23105. ' return this;',
  23106. ' };',
  23107. '});',
  23108. 'rtl.createHelper(this, "THelper", null, function () {',
  23109. ' this.NewHlp = function (w) {',
  23110. ' this.Create();',
  23111. ' $mod.TObject.$create("Create");',
  23112. ' $mod.THelper.NewHlp.call(this, 2);',
  23113. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23114. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23115. ' return this;',
  23116. ' };',
  23117. '});',
  23118. 'this.obj = null;',
  23119. 'this.c = null;',
  23120. '']),
  23121. LinesToStr([ // $mod.$main
  23122. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23123. 'var $with = $mod.obj;',
  23124. '$mod.THelper.NewHlp.call($with, 12);',
  23125. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23126. 'var $with1 = $mod.TObject;',
  23127. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23128. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23129. 'var $with2 = $mod.c;',
  23130. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23131. '']));
  23132. end;
  23133. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23134. begin
  23135. StartProgram(false);
  23136. Add([
  23137. 'type',
  23138. ' TObject = class',
  23139. ' procedure Fly;',
  23140. ' end;',
  23141. ' TObjHelper = class helper for TObject',
  23142. ' procedure Fly;',
  23143. ' end;',
  23144. ' TBird = class',
  23145. ' procedure Fly;',
  23146. ' end;',
  23147. ' TBirdHelper = class helper for TBird',
  23148. ' procedure Fly;',
  23149. ' procedure Walk(w: word);',
  23150. ' end;',
  23151. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23152. ' procedure Fly;',
  23153. ' procedure Walk(w: word);',
  23154. ' end;',
  23155. 'procedure Tobject.fly;',
  23156. 'begin',
  23157. ' inherited;', // ignore
  23158. 'end;',
  23159. 'procedure Tobjhelper.fly;',
  23160. 'begin',
  23161. ' {@TObject_Fly}inherited;',
  23162. ' inherited {@TObject_Fly}Fly;',
  23163. 'end;',
  23164. 'procedure Tbird.fly;',
  23165. 'begin',
  23166. ' {@TObjHelper_Fly}inherited;',
  23167. ' inherited {@TObjHelper_Fly}Fly;',
  23168. 'end;',
  23169. 'procedure Tbirdhelper.fly;',
  23170. 'begin',
  23171. ' {@TBird_Fly}inherited;',
  23172. ' inherited {@TBird_Fly}Fly;',
  23173. 'end;',
  23174. 'procedure Tbirdhelper.walk(w: word);',
  23175. 'begin',
  23176. 'end;',
  23177. 'procedure teagleHelper.fly;',
  23178. 'begin',
  23179. ' {@TBird_Fly}inherited;',
  23180. ' inherited {@TBird_Fly}Fly;',
  23181. 'end;',
  23182. 'procedure teagleHelper.walk(w: word);',
  23183. 'begin',
  23184. ' {@TBirdHelper_Walk}inherited;',
  23185. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23186. 'end;',
  23187. 'begin',
  23188. '']);
  23189. ConvertProgram;
  23190. CheckSource('TestClassHelper_InheritedObjFPC',
  23191. LinesToStr([ // statements
  23192. 'rtl.createClass(this, "TObject", null, function () {',
  23193. ' this.$init = function () {',
  23194. ' };',
  23195. ' this.$final = function () {',
  23196. ' };',
  23197. ' this.Fly = function () {',
  23198. ' };',
  23199. '});',
  23200. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23201. ' this.Fly = function () {',
  23202. ' $mod.TObject.Fly.call(this);',
  23203. ' $mod.TObject.Fly.call(this);',
  23204. ' };',
  23205. '});',
  23206. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23207. ' this.Fly$1 = function () {',
  23208. ' $mod.TObjHelper.Fly.call(this);',
  23209. ' $mod.TObjHelper.Fly.call(this);',
  23210. ' };',
  23211. '});',
  23212. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23213. ' this.Fly = function () {',
  23214. ' $mod.TBird.Fly$1.call(this);',
  23215. ' $mod.TBird.Fly$1.call(this);',
  23216. ' };',
  23217. ' this.Walk = function (w) {',
  23218. ' };',
  23219. '});',
  23220. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23221. ' this.Fly$1 = function () {',
  23222. ' $mod.TBird.Fly$1.call(this);',
  23223. ' $mod.TBird.Fly$1.call(this);',
  23224. ' };',
  23225. ' this.Walk$1 = function (w) {',
  23226. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  23227. ' $mod.TBirdHelper.Walk.call(this, 3);',
  23228. ' };',
  23229. '});',
  23230. '']),
  23231. LinesToStr([ // $mod.$main
  23232. '']));
  23233. end;
  23234. procedure TTestModule.TestClassHelper_Property;
  23235. begin
  23236. StartProgram(false);
  23237. Add([
  23238. 'type',
  23239. ' TObject = class',
  23240. ' FSize: word;',
  23241. ' function GetSpeed: word;',
  23242. ' procedure SetSpeed(Value: word);',
  23243. ' end;',
  23244. ' TObjHelper = class helper for TObject',
  23245. ' function GetLeft: word;',
  23246. ' procedure SetLeft(Value: word);',
  23247. ' property Size: word read FSize write FSize;',
  23248. ' property Speed: word read GetSpeed write SetSpeed;',
  23249. ' property Left: word read GetLeft write SetLeft;',
  23250. ' end;',
  23251. ' TBird = class',
  23252. ' property NotRight: word read GetLeft write SetLeft;',
  23253. ' procedure DoIt;',
  23254. ' end;',
  23255. 'var',
  23256. ' b: TBird;',
  23257. 'function Tobject.GetSpeed: word;',
  23258. 'begin',
  23259. ' Size:=Size+11;',
  23260. ' Speed:=Speed+12;',
  23261. ' Result:=Left+13;',
  23262. ' Left:=13;',
  23263. ' Left:=Left+13;',
  23264. ' Self.Size:=Self.Size+21;',
  23265. ' Self.Speed:=Self.Speed+22;',
  23266. ' Self.Left:=Self.Left+23;',
  23267. ' with Self do begin',
  23268. ' Size:=Size+31;',
  23269. ' Speed:=Speed+32;',
  23270. ' Left:=Left+33;',
  23271. ' end;',
  23272. 'end;',
  23273. 'procedure Tobject.SetSpeed(Value: word);',
  23274. 'begin',
  23275. 'end;',
  23276. 'function TObjHelper.GetLeft: word;',
  23277. 'begin',
  23278. ' Size:=Size+11;',
  23279. ' Speed:=Speed+12;',
  23280. ' Left:=Left+13;',
  23281. ' Self.Size:=Self.Size+21;',
  23282. ' Self.Speed:=Self.Speed+22;',
  23283. ' Self.Left:=Self.Left+23;',
  23284. ' with Self do begin',
  23285. ' Size:=Size+31;',
  23286. ' Speed:=Speed+32;',
  23287. ' Left:=Left+33;',
  23288. ' end;',
  23289. 'end;',
  23290. 'procedure TObjHelper.SetLeft(Value: word);',
  23291. 'begin',
  23292. 'end;',
  23293. 'procedure TBird.DoIt;',
  23294. 'begin',
  23295. ' NotRight:=NotRight+11;',
  23296. ' Self.NotRight:=Self.NotRight+21;',
  23297. ' with Self do begin',
  23298. ' NotRight:=NotRight+31;',
  23299. ' end;',
  23300. 'end;',
  23301. 'begin',
  23302. ' b.Size:=b.Size+11;',
  23303. ' b.Speed:=b.Speed+12;',
  23304. ' b.Left:=b.Left+13;',
  23305. ' b.NotRight:=b.NotRight+14;',
  23306. ' with b do begin',
  23307. ' Size:=Size+31;',
  23308. ' Speed:=Speed+32;',
  23309. ' Left:=Left+33;',
  23310. ' NotRight:=NotRight+34;',
  23311. ' end;',
  23312. '']);
  23313. ConvertProgram;
  23314. CheckSource('TestClassHelper_Property',
  23315. LinesToStr([ // statements
  23316. 'rtl.createClass(this, "TObject", null, function () {',
  23317. ' this.$init = function () {',
  23318. ' this.FSize = 0;',
  23319. ' };',
  23320. ' this.$final = function () {',
  23321. ' };',
  23322. ' this.GetSpeed = function () {',
  23323. ' var Result = 0;',
  23324. ' this.FSize = this.FSize + 11;',
  23325. ' this.SetSpeed(this.GetSpeed() + 12);',
  23326. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  23327. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  23328. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23329. ' this.FSize = this.FSize + 21;',
  23330. ' this.SetSpeed(this.GetSpeed() + 22);',
  23331. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23332. ' this.FSize = this.FSize + 31;',
  23333. ' this.SetSpeed(this.GetSpeed() + 32);',
  23334. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23335. ' return Result;',
  23336. ' };',
  23337. ' this.SetSpeed = function (Value) {',
  23338. ' };',
  23339. '});',
  23340. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23341. ' this.GetLeft = function () {',
  23342. ' var Result = 0;',
  23343. ' this.FSize = this.FSize + 11;',
  23344. ' this.SetSpeed(this.GetSpeed() + 12);',
  23345. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23346. ' this.FSize = this.FSize + 21;',
  23347. ' this.SetSpeed(this.GetSpeed() + 22);',
  23348. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23349. ' this.FSize = this.FSize + 31;',
  23350. ' this.SetSpeed(this.GetSpeed() + 32);',
  23351. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23352. ' return Result;',
  23353. ' };',
  23354. ' this.SetLeft = function (Value) {',
  23355. ' };',
  23356. '});',
  23357. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23358. ' this.DoIt = function () {',
  23359. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23360. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23361. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23362. ' };',
  23363. '});',
  23364. 'this.b = null;',
  23365. '']),
  23366. LinesToStr([ // $mod.$main
  23367. '$mod.b.FSize = $mod.b.FSize + 11;',
  23368. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23369. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23370. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23371. 'var $with = $mod.b;',
  23372. '$with.FSize = $with.FSize + 31;',
  23373. '$with.SetSpeed($with.GetSpeed() + 32);',
  23374. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23375. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23376. '']));
  23377. end;
  23378. procedure TTestModule.TestClassHelper_Property_Array;
  23379. begin
  23380. StartProgram(false);
  23381. Add([
  23382. 'type',
  23383. ' TObject = class',
  23384. ' function GetSpeed(Index: boolean): word;',
  23385. ' procedure SetSpeed(Index: boolean; Value: word);',
  23386. ' end;',
  23387. ' TObjHelper = class helper for TObject',
  23388. ' function GetSize(Index: boolean): word;',
  23389. ' procedure SetSize(Index: boolean; Value: word);',
  23390. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23391. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23392. ' end;',
  23393. ' TBird = class',
  23394. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23395. ' procedure DoIt;',
  23396. ' end;',
  23397. 'var',
  23398. ' b: TBird;',
  23399. 'function Tobject.GetSpeed(Index: boolean): word;',
  23400. 'begin',
  23401. ' Result:=Size[false];',
  23402. ' Size[true]:=Size[false]+11;',
  23403. ' Speed[true]:=Speed[false]+12;',
  23404. ' Self.Size[true]:=Self.Size[false]+21;',
  23405. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23406. ' with Self do begin',
  23407. ' Size[true]:=Size[false]+31;',
  23408. ' Speed[true]:=Speed[false]+32;',
  23409. ' end;',
  23410. 'end;',
  23411. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23412. 'begin',
  23413. 'end;',
  23414. 'function TObjHelper.GetSize(Index: boolean): word;',
  23415. 'begin',
  23416. ' Size[true]:=Size[false]+11;',
  23417. ' Speed[true]:=Speed[false]+12;',
  23418. ' Self.Size[true]:=Self.Size[false]+21;',
  23419. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23420. ' with Self do begin',
  23421. ' Size[true]:=Size[false]+31;',
  23422. ' Speed[true]:=Speed[false]+32;',
  23423. ' end;',
  23424. 'end;',
  23425. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23426. 'begin',
  23427. 'end;',
  23428. 'procedure TBird.DoIt;',
  23429. 'begin',
  23430. ' Items[true]:=Items[false]+11;',
  23431. ' Self.Items[true]:=Self.Items[false]+21;',
  23432. ' with Self do Items[true]:=Items[false]+31;',
  23433. 'end;',
  23434. 'begin',
  23435. ' b.Size[true]:=b.Size[false]+11;',
  23436. ' b.Speed[true]:=b.Speed[false]+12;',
  23437. ' b.Items[true]:=b.Items[false]+13;',
  23438. ' with b do begin',
  23439. ' Size[true]:=Size[false]+21;',
  23440. ' Speed[true]:=Speed[false]+22;',
  23441. ' Items[true]:=Items[false]+23;',
  23442. ' end;',
  23443. '']);
  23444. ConvertProgram;
  23445. CheckSource('TestClassHelper_Property_Array',
  23446. LinesToStr([ // statements
  23447. 'rtl.createClass(this, "TObject", null, function () {',
  23448. ' this.$init = function () {',
  23449. ' };',
  23450. ' this.$final = function () {',
  23451. ' };',
  23452. ' this.GetSpeed = function (Index) {',
  23453. ' var Result = 0;',
  23454. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23455. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23456. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23457. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23458. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23459. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23460. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23461. ' return Result;',
  23462. ' };',
  23463. ' this.SetSpeed = function (Index, Value) {',
  23464. ' };',
  23465. '});',
  23466. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23467. ' this.GetSize = function (Index) {',
  23468. ' var Result = 0;',
  23469. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23470. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23471. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23472. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23473. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23474. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23475. ' return Result;',
  23476. ' };',
  23477. ' this.SetSize = function (Index, Value) {',
  23478. ' };',
  23479. '});',
  23480. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23481. ' this.DoIt = function () {',
  23482. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23483. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23484. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23485. ' };',
  23486. '});',
  23487. 'this.b = null;',
  23488. '']),
  23489. LinesToStr([ // $mod.$main
  23490. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23491. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23492. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23493. 'var $with = $mod.b;',
  23494. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23495. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23496. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23497. '']));
  23498. end;
  23499. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23500. begin
  23501. StartProgram(false);
  23502. Add([
  23503. 'type',
  23504. ' TObject = class',
  23505. ' function GetSpeed(Index: boolean): word;',
  23506. ' procedure SetSpeed(Index: boolean; Value: word);',
  23507. ' end;',
  23508. ' TObjHelper = class helper for TObject',
  23509. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23510. ' end;',
  23511. ' TBird = class',
  23512. ' end;',
  23513. ' TBirdHelper = class helper for TBird',
  23514. ' function GetSize(Index: word): boolean;',
  23515. ' procedure SetSize(Index: word; Value: boolean);',
  23516. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23517. ' end;',
  23518. 'function Tobject.GetSpeed(Index: boolean): word;',
  23519. 'begin',
  23520. ' Self[true]:=Self[false]+1;',
  23521. 'end;',
  23522. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23523. 'begin',
  23524. 'end;',
  23525. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23526. 'begin',
  23527. ' Self[1]:=not Self[2];',
  23528. 'end;',
  23529. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23530. 'begin',
  23531. 'end;',
  23532. 'var',
  23533. ' o: TObject;',
  23534. ' b: TBird;',
  23535. 'begin',
  23536. ' o[true]:=o[false]+1;',
  23537. ' b[3]:=not b[4];',
  23538. '']);
  23539. ConvertProgram;
  23540. CheckSource('TestClassHelper_Property_Array_Default',
  23541. LinesToStr([ // statements
  23542. 'rtl.createClass(this, "TObject", null, function () {',
  23543. ' this.$init = function () {',
  23544. ' };',
  23545. ' this.$final = function () {',
  23546. ' };',
  23547. ' this.GetSpeed = function (Index) {',
  23548. ' var Result = 0;',
  23549. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23550. ' return Result;',
  23551. ' };',
  23552. ' this.SetSpeed = function (Index, Value) {',
  23553. ' };',
  23554. '});',
  23555. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23556. '});',
  23557. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23558. '});',
  23559. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23560. ' this.GetSize = function (Index) {',
  23561. ' var Result = false;',
  23562. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23563. ' return Result;',
  23564. ' };',
  23565. ' this.SetSize = function (Index, Value) {',
  23566. ' };',
  23567. '});',
  23568. 'this.o = null;',
  23569. 'this.b = null;',
  23570. '']),
  23571. LinesToStr([ // $mod.$main
  23572. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23573. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23574. '']));
  23575. end;
  23576. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23577. begin
  23578. StartProgram(false);
  23579. Add([
  23580. 'type',
  23581. ' TObject = class',
  23582. ' end;',
  23583. ' TObjHelper = class helper for TObject',
  23584. ' function GetItems(Index: word): TObject;',
  23585. ' procedure SetItems(Index: word; Value: TObject);',
  23586. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23587. ' end;',
  23588. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23589. 'begin',
  23590. ' Self[1][2]:=Self[3][4];',
  23591. 'end;',
  23592. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23593. 'begin',
  23594. 'end;',
  23595. 'var',
  23596. ' o: TObject;',
  23597. 'begin',
  23598. ' o[1][2]:=o[3][4];',
  23599. '']);
  23600. ConvertProgram;
  23601. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23602. LinesToStr([ // statements
  23603. 'rtl.createClass(this, "TObject", null, function () {',
  23604. ' this.$init = function () {',
  23605. ' };',
  23606. ' this.$final = function () {',
  23607. ' };',
  23608. '});',
  23609. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23610. ' this.GetItems = function (Index) {',
  23611. ' var Result = null;',
  23612. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23613. ' return Result;',
  23614. ' };',
  23615. ' this.SetItems = function (Index, Value) {',
  23616. ' };',
  23617. '});',
  23618. 'this.o = null;',
  23619. '']),
  23620. LinesToStr([ // $mod.$main
  23621. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23622. '']));
  23623. end;
  23624. procedure TTestModule.TestClassHelper_ClassProperty;
  23625. begin
  23626. StartProgram(false);
  23627. Add([
  23628. 'type',
  23629. ' TObject = class',
  23630. ' class var FSize: word;',
  23631. ' class function GetSpeed: word;',
  23632. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23633. ' end;',
  23634. ' TObjHelper = class helper for TObject',
  23635. ' class function GetLeft: word;',
  23636. ' class procedure SetLeft(Value: word);',
  23637. ' class property Size: word read FSize write FSize;',
  23638. ' class property Speed: word read GetSpeed write SetSpeed;',
  23639. ' class property Left: word read GetLeft write SetLeft;',
  23640. ' end;',
  23641. ' TBird = class',
  23642. ' class property NotRight: word read GetLeft write SetLeft;',
  23643. ' class procedure DoIt;',
  23644. ' end;',
  23645. ' TBirdClass = class of TBird;',
  23646. 'class function Tobject.GetSpeed: word;',
  23647. 'begin',
  23648. ' Size:=Size+11;',
  23649. ' Speed:=Speed+12;',
  23650. ' Left:=Left+13;',
  23651. ' Self.Size:=Self.Size+21;',
  23652. ' Self.Speed:=Self.Speed+22;',
  23653. ' Self.Left:=Self.Left+23;',
  23654. ' with Self do begin',
  23655. ' Size:=Size+31;',
  23656. ' Speed:=Speed+32;',
  23657. ' Left:=Left+33;',
  23658. ' end;',
  23659. 'end;',
  23660. 'class function TObjHelper.GetLeft: word;',
  23661. 'begin',
  23662. ' Size:=Size+11;',
  23663. ' Speed:=Speed+12;',
  23664. ' Left:=Left+13;',
  23665. ' Self.Size:=Self.Size+21;',
  23666. ' Self.Speed:=Self.Speed+22;',
  23667. ' Self.Left:=Self.Left+23;',
  23668. ' with Self do begin',
  23669. ' Size:=Size+31;',
  23670. ' Speed:=Speed+32;',
  23671. ' Left:=Left+33;',
  23672. ' end;',
  23673. 'end;',
  23674. 'class procedure TObjHelper.SetLeft(Value: word);',
  23675. 'begin',
  23676. 'end;',
  23677. 'class procedure TBird.DoIt;',
  23678. 'begin',
  23679. ' NotRight:=NotRight+11;',
  23680. ' Self.NotRight:=Self.NotRight+21;',
  23681. ' with Self do NotRight:=NotRight+31;',
  23682. 'end;',
  23683. 'var',
  23684. ' b: TBird;',
  23685. ' c: TBirdClass;',
  23686. 'begin',
  23687. ' b.Size:=b.Size+11;',
  23688. ' b.Speed:=b.Speed+12;',
  23689. ' b.Left:=b.Left+13;',
  23690. ' b.NotRight:=b.NotRight+14;',
  23691. ' with b do begin',
  23692. ' Size:=Size+31;',
  23693. ' Speed:=Speed+32;',
  23694. ' Left:=Left+33;',
  23695. ' NotRight:=NotRight+34;',
  23696. ' end;',
  23697. ' c.Size:=c.Size+11;',
  23698. ' c.Speed:=c.Speed+12;',
  23699. ' c.Left:=c.Left+13;',
  23700. ' c.NotRight:=c.NotRight+14;',
  23701. ' with c do begin',
  23702. ' Size:=Size+31;',
  23703. ' Speed:=Speed+32;',
  23704. ' Left:=Left+33;',
  23705. ' NotRight:=NotRight+34;',
  23706. ' end;',
  23707. ' tbird.Size:=tbird.Size+11;',
  23708. ' tbird.Speed:=tbird.Speed+12;',
  23709. ' tbird.Left:=tbird.Left+13;',
  23710. ' tbird.NotRight:=tbird.NotRight+14;',
  23711. ' with tbird do begin',
  23712. ' Size:=Size+31;',
  23713. ' Speed:=Speed+32;',
  23714. ' Left:=Left+33;',
  23715. ' NotRight:=NotRight+34;',
  23716. ' end;',
  23717. '']);
  23718. ConvertProgram;
  23719. CheckSource('TestClassHelper_ClassProperty',
  23720. LinesToStr([ // statements
  23721. 'rtl.createClass(this, "TObject", null, function () {',
  23722. ' this.FSize = 0;',
  23723. ' this.$init = function () {',
  23724. ' };',
  23725. ' this.$final = function () {',
  23726. ' };',
  23727. ' this.GetSpeed = function () {',
  23728. ' var Result = 0;',
  23729. ' $mod.TObject.FSize = this.FSize + 11;',
  23730. ' this.SetSpeed(this.GetSpeed() + 12);',
  23731. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23732. ' $mod.TObject.FSize = this.FSize + 21;',
  23733. ' this.SetSpeed(this.GetSpeed() + 22);',
  23734. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23735. ' $mod.TObject.FSize = this.FSize + 31;',
  23736. ' this.SetSpeed(this.GetSpeed() + 32);',
  23737. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23738. ' return Result;',
  23739. ' };',
  23740. '});',
  23741. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23742. ' this.GetLeft = function () {',
  23743. ' var Result = 0;',
  23744. ' $mod.TObject.FSize = this.FSize + 11;',
  23745. ' this.SetSpeed(this.GetSpeed() + 12);',
  23746. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23747. ' $mod.TObject.FSize = this.FSize + 21;',
  23748. ' this.SetSpeed(this.GetSpeed() + 22);',
  23749. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23750. ' $mod.TObject.FSize = this.FSize + 31;',
  23751. ' this.SetSpeed(this.GetSpeed() + 32);',
  23752. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23753. ' return Result;',
  23754. ' };',
  23755. ' this.SetLeft = function (Value) {',
  23756. ' };',
  23757. '});',
  23758. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23759. ' this.DoIt = function () {',
  23760. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23761. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23762. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23763. ' };',
  23764. '});',
  23765. 'this.b = null;',
  23766. 'this.c = null;',
  23767. '']),
  23768. LinesToStr([ // $mod.$main
  23769. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23770. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23771. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23772. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23773. 'var $with = $mod.b;',
  23774. '$mod.TObject.FSize = $with.FSize + 31;',
  23775. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23776. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23777. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23778. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23779. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23780. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23781. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23782. 'var $with1 = $mod.c;',
  23783. '$mod.TObject.FSize = $with1.FSize + 31;',
  23784. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23785. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23786. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23787. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23788. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23789. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23790. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23791. 'var $with2 = $mod.TBird;',
  23792. '$mod.TObject.FSize = $with2.FSize + 31;',
  23793. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23794. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23795. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23796. '']));
  23797. end;
  23798. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23799. begin
  23800. StartProgram(false);
  23801. Add([
  23802. 'type',
  23803. ' TObject = class',
  23804. ' class function GetSpeed: word; static;',
  23805. ' class procedure SetSpeed(Value: word); static;',
  23806. ' end;',
  23807. ' TObjHelper = class helper for TObject',
  23808. ' class function GetLeft: word; static;',
  23809. ' class procedure SetLeft(Value: word); static;',
  23810. ' class property Speed: word read GetSpeed write SetSpeed;',
  23811. ' class property Left: word read GetLeft write SetLeft;',
  23812. ' end;',
  23813. ' TBird = class',
  23814. ' class property NotRight: word read GetLeft write SetLeft;',
  23815. ' class procedure DoIt; static;',
  23816. ' class procedure DoSome;',
  23817. ' end;',
  23818. ' TBirdClass = class of TBird;',
  23819. 'class function Tobject.GetSpeed: word;',
  23820. 'begin',
  23821. ' Speed:=Speed+12;',
  23822. ' Left:=Left+13;',
  23823. 'end;',
  23824. 'class procedure TObject.SetSpeed(Value: word);',
  23825. 'begin',
  23826. 'end;',
  23827. 'class function TObjHelper.GetLeft: word;',
  23828. 'begin',
  23829. ' Speed:=Speed+12;',
  23830. ' Left:=Left+13;',
  23831. 'end;',
  23832. 'class procedure TObjHelper.SetLeft(Value: word);',
  23833. 'begin',
  23834. 'end;',
  23835. 'class procedure TBird.DoIt;',
  23836. 'begin',
  23837. ' NotRight:=NotRight+11;',
  23838. 'end;',
  23839. 'class procedure TBird.DoSome;',
  23840. 'begin',
  23841. ' Speed:=Speed+12;',
  23842. ' Left:=Left+13;',
  23843. ' Self.Speed:=Self.Speed+22;',
  23844. ' Self.Left:=Self.Left+23;',
  23845. ' with Self do begin',
  23846. ' Speed:=Speed+32;',
  23847. ' Left:=Left+33;',
  23848. ' end;',
  23849. ' NotRight:=NotRight+11;',
  23850. ' Self.NotRight:=Self.NotRight+21;',
  23851. ' with Self do NotRight:=NotRight+31;',
  23852. 'end;',
  23853. 'var',
  23854. ' b: TBird;',
  23855. ' c: TBirdClass;',
  23856. 'begin',
  23857. ' b.Speed:=b.Speed+12;',
  23858. ' b.Left:=b.Left+13;',
  23859. ' b.NotRight:=b.NotRight+14;',
  23860. ' with b do begin',
  23861. ' Speed:=Speed+32;',
  23862. ' Left:=Left+33;',
  23863. ' NotRight:=NotRight+34;',
  23864. ' end;',
  23865. ' c.Speed:=c.Speed+12;',
  23866. ' c.Left:=c.Left+13;',
  23867. ' c.NotRight:=c.NotRight+14;',
  23868. ' with c do begin',
  23869. ' Speed:=Speed+32;',
  23870. ' Left:=Left+33;',
  23871. ' NotRight:=NotRight+34;',
  23872. ' end;',
  23873. ' tbird.Speed:=tbird.Speed+12;',
  23874. ' tbird.Left:=tbird.Left+13;',
  23875. ' tbird.NotRight:=tbird.NotRight+14;',
  23876. ' with tbird do begin',
  23877. ' Speed:=Speed+32;',
  23878. ' Left:=Left+33;',
  23879. ' NotRight:=NotRight+34;',
  23880. ' end;',
  23881. '']);
  23882. ConvertProgram;
  23883. CheckSource('TestClassHelper_ClassPropertyStatic',
  23884. LinesToStr([ // statements
  23885. 'rtl.createClass(this, "TObject", null, function () {',
  23886. ' this.$init = function () {',
  23887. ' };',
  23888. ' this.$final = function () {',
  23889. ' };',
  23890. ' this.GetSpeed = function () {',
  23891. ' var Result = 0;',
  23892. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23893. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23894. ' return Result;',
  23895. ' };',
  23896. ' this.SetSpeed = function (Value) {',
  23897. ' };',
  23898. '});',
  23899. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23900. ' this.GetLeft = function () {',
  23901. ' var Result = 0;',
  23902. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23903. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23904. ' return Result;',
  23905. ' };',
  23906. ' this.SetLeft = function (Value) {',
  23907. ' };',
  23908. '});',
  23909. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23910. ' this.DoIt = function () {',
  23911. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23912. ' };',
  23913. ' this.DoSome = function () {',
  23914. ' this.SetSpeed(this.GetSpeed() + 12);',
  23915. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23916. ' this.SetSpeed(this.GetSpeed() + 22);',
  23917. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23918. ' this.SetSpeed(this.GetSpeed() + 32);',
  23919. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23920. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23921. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23922. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23923. ' };',
  23924. '});',
  23925. 'this.b = null;',
  23926. 'this.c = null;',
  23927. '']),
  23928. LinesToStr([ // $mod.$main
  23929. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23930. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23931. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23932. 'var $with = $mod.b;',
  23933. '$with.SetSpeed($with.GetSpeed() + 32);',
  23934. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23935. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23936. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23937. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23938. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23939. 'var $with1 = $mod.c;',
  23940. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23941. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23942. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23943. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23944. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23945. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23946. 'var $with2 = $mod.TBird;',
  23947. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23948. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23949. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23950. '']));
  23951. end;
  23952. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  23953. begin
  23954. StartProgram(false);
  23955. Add([
  23956. 'type',
  23957. ' TObject = class',
  23958. ' class function GetSpeed(Index: boolean): word;',
  23959. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  23960. ' end;',
  23961. ' TObjHelper = class helper for TObject',
  23962. ' class function GetSize(Index: boolean): word;',
  23963. ' class procedure SetSize(Index: boolean; Value: word);',
  23964. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  23965. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23966. ' end;',
  23967. ' TBird = class',
  23968. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  23969. ' class procedure DoIt;',
  23970. ' end;',
  23971. ' TBirdClass = class of TBird;',
  23972. 'class function Tobject.GetSpeed(Index: boolean): word;',
  23973. 'begin',
  23974. ' Size[true]:=Size[false]+11;',
  23975. ' Speed[true]:=Speed[false]+12;',
  23976. ' Self.Size[true]:=Self.Size[false]+21;',
  23977. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23978. ' with Self do begin',
  23979. ' Size[true]:=Size[false]+31;',
  23980. ' Speed[true]:=Speed[false]+32;',
  23981. ' end;',
  23982. 'end;',
  23983. 'class function TObjHelper.GetSize(Index: boolean): word;',
  23984. 'begin',
  23985. ' Size[true]:=Size[false]+11;',
  23986. ' Speed[true]:=Speed[false]+12;',
  23987. ' Self.Size[true]:=Self.Size[false]+21;',
  23988. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23989. ' with Self do begin',
  23990. ' Size[true]:=Size[false]+31;',
  23991. ' Speed[true]:=Speed[false]+32;',
  23992. ' end;',
  23993. 'end;',
  23994. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23995. 'begin',
  23996. 'end;',
  23997. 'class procedure TBird.DoIt;',
  23998. 'begin',
  23999. ' Items[true]:=Items[false]+11;',
  24000. ' Self.Items[true]:=Self.Items[false]+21;',
  24001. ' with Self do Items[true]:=Items[false]+31;',
  24002. 'end;',
  24003. 'var',
  24004. ' b: TBird;',
  24005. ' c: TBirdClass;',
  24006. 'begin',
  24007. ' b.Size[true]:=b.Size[false]+11;',
  24008. ' b.Speed[true]:=b.Speed[false]+12;',
  24009. ' b.Items[true]:=b.Items[false]+13;',
  24010. ' with b do begin',
  24011. ' Size[true]:=Size[false]+21;',
  24012. ' Speed[true]:=Speed[false]+22;',
  24013. ' Items[true]:=Items[false]+23;',
  24014. ' end;',
  24015. ' c.Size[true]:=c.Size[false]+11;',
  24016. ' c.Speed[true]:=c.Speed[false]+12;',
  24017. ' c.Items[true]:=c.Items[false]+13;',
  24018. ' with c do begin',
  24019. ' Size[true]:=Size[false]+21;',
  24020. ' Speed[true]:=Speed[false]+22;',
  24021. ' Items[true]:=Items[false]+23;',
  24022. ' end;',
  24023. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24024. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24025. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24026. ' with TBird do begin',
  24027. ' Size[true]:=Size[false]+21;',
  24028. ' Speed[true]:=Speed[false]+22;',
  24029. ' Items[true]:=Items[false]+23;',
  24030. ' end;',
  24031. '']);
  24032. ConvertProgram;
  24033. CheckSource('TestClassHelper_ClassProperty_Array',
  24034. LinesToStr([ // statements
  24035. 'rtl.createClass(this, "TObject", null, function () {',
  24036. ' this.$init = function () {',
  24037. ' };',
  24038. ' this.$final = function () {',
  24039. ' };',
  24040. ' this.GetSpeed = function (Index) {',
  24041. ' var Result = 0;',
  24042. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24043. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24044. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24045. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24046. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24047. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24048. ' return Result;',
  24049. ' };',
  24050. '});',
  24051. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24052. ' this.GetSize = function (Index) {',
  24053. ' var Result = 0;',
  24054. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24055. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24056. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24057. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24058. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24059. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24060. ' return Result;',
  24061. ' };',
  24062. ' this.SetSize = function (Index, Value) {',
  24063. ' };',
  24064. '});',
  24065. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24066. ' this.DoIt = function () {',
  24067. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24068. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24069. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24070. ' };',
  24071. '});',
  24072. 'this.b = null;',
  24073. 'this.c = null;',
  24074. '']),
  24075. LinesToStr([ // $mod.$main
  24076. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24077. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24078. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24079. 'var $with = $mod.b;',
  24080. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24081. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24082. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24083. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24084. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24085. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24086. 'var $with1 = $mod.c;',
  24087. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24088. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24089. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24090. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24091. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24092. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24093. 'var $with2 = $mod.TBird;',
  24094. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24095. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24096. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24097. '']));
  24098. end;
  24099. procedure TTestModule.TestClassHelper_ForIn;
  24100. begin
  24101. StartProgram(false);
  24102. Add([
  24103. 'type',
  24104. ' TObject = class end;',
  24105. ' TItem = TObject;',
  24106. ' TEnumerator = class',
  24107. ' FCurrent: TItem;',
  24108. ' property Current: TItem read FCurrent;',
  24109. ' function MoveNext: boolean;',
  24110. ' end;',
  24111. ' TBird = class',
  24112. ' end;',
  24113. ' TBirdHelper = class helper for TBird',
  24114. ' function GetEnumerator: TEnumerator;',
  24115. ' end;',
  24116. 'function TEnumerator.MoveNext: boolean;',
  24117. 'begin',
  24118. 'end;',
  24119. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24120. 'begin',
  24121. 'end;',
  24122. 'var',
  24123. ' b: TBird;',
  24124. ' i, i2: TItem;',
  24125. 'begin',
  24126. ' for i in b do i2:=i;']);
  24127. ConvertProgram;
  24128. CheckSource('TestClassHelper_ForIn',
  24129. LinesToStr([ // statements
  24130. 'rtl.createClass(this, "TObject", null, function () {',
  24131. ' this.$init = function () {',
  24132. ' };',
  24133. ' this.$final = function () {',
  24134. ' };',
  24135. '});',
  24136. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24137. ' this.$init = function () {',
  24138. ' $mod.TObject.$init.call(this);',
  24139. ' this.FCurrent = null;',
  24140. ' };',
  24141. ' this.$final = function () {',
  24142. ' this.FCurrent = undefined;',
  24143. ' $mod.TObject.$final.call(this);',
  24144. ' };',
  24145. ' this.MoveNext = function () {',
  24146. ' var Result = false;',
  24147. ' return Result;',
  24148. ' };',
  24149. '});',
  24150. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24151. '});',
  24152. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24153. ' this.GetEnumerator = function () {',
  24154. ' var Result = null;',
  24155. ' return Result;',
  24156. ' };',
  24157. '});',
  24158. 'this.b = null;',
  24159. 'this.i = null;',
  24160. 'this.i2 = null;'
  24161. ]),
  24162. LinesToStr([ // $mod.$main
  24163. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24164. 'try {',
  24165. ' while ($in.MoveNext()){',
  24166. ' $mod.i = $in.FCurrent;',
  24167. ' $mod.i2 = $mod.i;',
  24168. ' }',
  24169. '} finally {',
  24170. ' $in = rtl.freeLoc($in)',
  24171. '};',
  24172. '']));
  24173. end;
  24174. procedure TTestModule.TestClassHelper_PassProperty;
  24175. begin
  24176. StartProgram(false);
  24177. Add([
  24178. 'type',
  24179. ' TObject = class',
  24180. ' FField: TObject;',
  24181. ' property Field: TObject read FField write FField;',
  24182. ' end;',
  24183. ' THelper = class helper for TObject',
  24184. ' procedure Fly;',
  24185. ' class procedure Run;',
  24186. ' class procedure Jump; static;',
  24187. ' end;',
  24188. 'procedure THelper.Fly;',
  24189. 'begin',
  24190. ' Field.Fly;',
  24191. ' Field.Run;',
  24192. ' Field.Jump;',
  24193. ' with Field do begin',
  24194. ' Fly;',
  24195. ' Run;',
  24196. ' Jump;',
  24197. ' end;',
  24198. 'end;',
  24199. 'class procedure THelper.Run;',
  24200. 'begin',
  24201. 'end;',
  24202. 'class procedure THelper.Jump;',
  24203. 'begin',
  24204. 'end;',
  24205. 'var',
  24206. ' b: TObject;',
  24207. 'begin',
  24208. ' b.Field.Fly;',
  24209. ' b.Field.Run;',
  24210. ' b.Field.Jump;',
  24211. ' with b do begin',
  24212. ' Field.Run;',
  24213. ' Field.Fly;',
  24214. ' Field.Jump;',
  24215. ' end;',
  24216. ' with b.Field do begin',
  24217. ' Run;',
  24218. ' Fly;',
  24219. ' Jump;',
  24220. ' end;',
  24221. '']);
  24222. ConvertProgram;
  24223. CheckSource('TestClassHelper_PassProperty',
  24224. LinesToStr([ // statements
  24225. 'rtl.createClass(this, "TObject", null, function () {',
  24226. ' this.$init = function () {',
  24227. ' this.FField = null;',
  24228. ' };',
  24229. ' this.$final = function () {',
  24230. ' this.FField = undefined;',
  24231. ' };',
  24232. '});',
  24233. 'rtl.createHelper(this, "THelper", null, function () {',
  24234. ' this.Fly = function () {',
  24235. ' $mod.THelper.Fly.call(this.FField);',
  24236. ' $mod.THelper.Run.call(this.FField.$class);',
  24237. ' $mod.THelper.Jump();',
  24238. ' var $with = this.FField;',
  24239. ' $mod.THelper.Fly.call($with);',
  24240. ' $mod.THelper.Run.call($with.$class);',
  24241. ' $mod.THelper.Jump();',
  24242. ' };',
  24243. ' this.Run = function () {',
  24244. ' };',
  24245. ' this.Jump = function () {',
  24246. ' };',
  24247. '});',
  24248. 'this.b = null;',
  24249. '']),
  24250. LinesToStr([ // $mod.$main
  24251. '$mod.THelper.Fly.call($mod.b.FField);',
  24252. '$mod.THelper.Run.call($mod.b.FField.$class);',
  24253. '$mod.THelper.Jump();',
  24254. 'var $with = $mod.b;',
  24255. '$mod.THelper.Run.call($with.FField.$class);',
  24256. '$mod.THelper.Fly.call($with.FField);',
  24257. '$mod.THelper.Jump();',
  24258. 'var $with1 = $mod.b.FField;',
  24259. '$mod.THelper.Run.call($with1.$class);',
  24260. '$mod.THelper.Fly.call($with1);',
  24261. '$mod.THelper.Jump();',
  24262. '']));
  24263. end;
  24264. procedure TTestModule.TestExtClassHelper_ClassVar;
  24265. begin
  24266. StartProgram(false);
  24267. Add([
  24268. '{$modeswitch externalclass}',
  24269. 'type',
  24270. ' TExtA = class external name ''ExtObj''',
  24271. ' end;',
  24272. ' THelper = class helper for TExtA',
  24273. ' const',
  24274. ' One = 1;',
  24275. ' Two: word = 2;',
  24276. ' class var',
  24277. ' Glob: word;',
  24278. ' function Foo(w: word): word;',
  24279. ' class function Bar(w: word): word; static;',
  24280. ' end;',
  24281. 'function THelper.foo(w: word): word;',
  24282. 'begin',
  24283. ' Result:=w;',
  24284. ' Two:=One+w;',
  24285. ' Glob:=Glob;',
  24286. ' Result:=Self.Glob;',
  24287. ' Self.Glob:=Self.Glob;',
  24288. ' with Self do Glob:=Glob;',
  24289. 'end;',
  24290. 'class function THelper.bar(w: word): word;',
  24291. 'begin',
  24292. ' Result:=w;',
  24293. ' Two:=One;',
  24294. ' Glob:=Glob;',
  24295. 'end;',
  24296. 'var o: TExtA;',
  24297. 'begin',
  24298. ' texta.two:=texta.one;',
  24299. ' texta.Glob:=texta.Glob;',
  24300. ' with texta do begin',
  24301. ' two:=one;',
  24302. ' Glob:=Glob;',
  24303. ' end;',
  24304. ' o.two:=o.one;',
  24305. ' o.Glob:=o.Glob;',
  24306. ' with o do begin',
  24307. ' two:=one;',
  24308. ' Glob:=Glob;',
  24309. ' end;',
  24310. '']);
  24311. ConvertProgram;
  24312. CheckSource('TestExtClassHelper_ClassVar',
  24313. LinesToStr([ // statements
  24314. 'rtl.createHelper(this, "THelper", null, function () {',
  24315. ' this.One = 1;',
  24316. ' this.Two = 2;',
  24317. ' this.Glob = 0;',
  24318. ' this.Foo = function (w) {',
  24319. ' var Result = 0;',
  24320. ' Result = w;',
  24321. ' $mod.THelper.Two = 1 + w;',
  24322. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24323. ' Result = $mod.THelper.Glob;',
  24324. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24325. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24326. ' return Result;',
  24327. ' };',
  24328. ' this.Bar = function (w) {',
  24329. ' var Result = 0;',
  24330. ' Result = w;',
  24331. ' $mod.THelper.Two = 1;',
  24332. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24333. ' return Result;',
  24334. ' };',
  24335. '});',
  24336. 'this.o = null;',
  24337. '']),
  24338. LinesToStr([ // $mod.$main
  24339. '$mod.THelper.Two = 1;',
  24340. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24341. '$mod.THelper.Two = 1;',
  24342. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24343. '$mod.THelper.Two = 1;',
  24344. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24345. 'var $with = $mod.o;',
  24346. '$mod.THelper.Two = 1;',
  24347. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24348. '']));
  24349. end;
  24350. procedure TTestModule.TestExtClassHelper_Method_Call;
  24351. begin
  24352. StartProgram(false);
  24353. Add([
  24354. '{$modeswitch externalclass}',
  24355. 'type',
  24356. ' TFly = function(w: word): word of object;',
  24357. ' TExtA = class external name ''ExtObj''',
  24358. ' procedure Run(w: word = 10);',
  24359. ' end;',
  24360. ' THelper = class helper for TExtA',
  24361. ' function Foo(w: word = 1): word;',
  24362. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24363. ' end;',
  24364. 'var p: TFly;',
  24365. 'function THelper.foo(w: word): word;',
  24366. 'begin',
  24367. ' Run;',
  24368. ' Run();',
  24369. ' Run(11);',
  24370. ' Foo;',
  24371. ' Foo();',
  24372. ' Foo(12);',
  24373. ' Self.Foo;',
  24374. ' Self.Foo();',
  24375. ' Self.Foo(13);',
  24376. ' Fly;',
  24377. ' Fly();',
  24378. ' with Self do begin',
  24379. ' Foo;',
  24380. ' Foo();',
  24381. ' Foo(14);',
  24382. ' Fly;',
  24383. ' Fly();',
  24384. ' end;',
  24385. ' p:=@Fly;',
  24386. 'end;',
  24387. 'var Obj: TExtA;',
  24388. 'begin',
  24389. ' obj.Foo;',
  24390. ' obj.Foo();',
  24391. ' obj.Foo(21);',
  24392. ' obj.Fly;',
  24393. ' obj.Fly();',
  24394. ' with obj do begin',
  24395. ' Foo;',
  24396. ' Foo();',
  24397. ' Foo(22);',
  24398. ' Fly;',
  24399. ' Fly();',
  24400. ' end;',
  24401. ' p:[email protected];',
  24402. '']);
  24403. ConvertProgram;
  24404. CheckSource('TestExtClassHelper_Method_Call',
  24405. LinesToStr([ // statements
  24406. 'rtl.createHelper(this, "THelper", null, function () {',
  24407. ' this.Foo = function (w) {',
  24408. ' var Result = 0;',
  24409. ' this.Run(10);',
  24410. ' this.Run(10);',
  24411. ' this.Run(11);',
  24412. ' $mod.THelper.Foo.call(this, 1);',
  24413. ' $mod.THelper.Foo.call(this, 1);',
  24414. ' $mod.THelper.Foo.call(this, 12);',
  24415. ' $mod.THelper.Foo.call(this, 1);',
  24416. ' $mod.THelper.Foo.call(this, 1);',
  24417. ' $mod.THelper.Foo.call(this, 13);',
  24418. ' this.Fly(2);',
  24419. ' this.Fly(2);',
  24420. ' $mod.THelper.Foo.call(this, 1);',
  24421. ' $mod.THelper.Foo.call(this, 1);',
  24422. ' $mod.THelper.Foo.call(this, 14);',
  24423. ' this.Fly(2);',
  24424. ' this.Fly(2);',
  24425. ' $mod.p = rtl.createCallback(this, "Fly");',
  24426. ' return Result;',
  24427. ' };',
  24428. '});',
  24429. 'this.p = null;',
  24430. 'this.Obj = null;',
  24431. '']),
  24432. LinesToStr([ // $mod.$main
  24433. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24434. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24435. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24436. '$mod.Obj.Fly(2);',
  24437. '$mod.Obj.Fly(2);',
  24438. 'var $with = $mod.Obj;',
  24439. '$mod.THelper.Foo.call($with, 1);',
  24440. '$mod.THelper.Foo.call($with, 1);',
  24441. '$mod.THelper.Foo.call($with, 22);',
  24442. '$with.Fly(2);',
  24443. '$with.Fly(2);',
  24444. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24445. '']));
  24446. end;
  24447. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24448. begin
  24449. StartProgram(false);
  24450. Add([
  24451. '{$modeswitch externalclass}',
  24452. 'type',
  24453. ' TExtA = class external name ''ExtObj''',
  24454. ' procedure Run(w: word = 10);',
  24455. ' end;',
  24456. ' THelper = class helper for TExtA',
  24457. ' class procedure Fly;',
  24458. ' end;',
  24459. 'class procedure THelper.Fly;',
  24460. 'begin end;',
  24461. 'begin',
  24462. '']);
  24463. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24464. nHelperClassMethodForExtClassMustBeStatic);
  24465. ConvertProgram;
  24466. end;
  24467. procedure TTestModule.TestRecordHelper_ClassVar;
  24468. begin
  24469. StartProgram(false);
  24470. Add([
  24471. 'type',
  24472. ' TRec = record',
  24473. ' end;',
  24474. ' THelper = record helper for TRec',
  24475. ' const',
  24476. ' One = 1;',
  24477. ' Two: word = 2;',
  24478. ' class var',
  24479. ' Glob: word;',
  24480. ' function Foo(w: word): word;',
  24481. ' class function Bar(w: word): word; static;',
  24482. ' end;',
  24483. 'function THelper.foo(w: word): word;',
  24484. 'begin',
  24485. ' Result:=w;',
  24486. ' Two:=One+w;',
  24487. ' Glob:=Glob;',
  24488. ' Result:=Self.Glob;',
  24489. ' Self.Glob:=Self.Glob;',
  24490. ' with Self do Glob:=Glob;',
  24491. ' Self:=Self;',
  24492. 'end;',
  24493. 'class function THelper.bar(w: word): word;',
  24494. 'begin',
  24495. ' Result:=w;',
  24496. ' Two:=One;',
  24497. ' Glob:=Glob;',
  24498. 'end;',
  24499. 'var r: TRec;',
  24500. 'begin',
  24501. ' trec.two:=trec.one;',
  24502. ' trec.Glob:=trec.Glob;',
  24503. ' with trec do begin',
  24504. ' two:=one;',
  24505. ' Glob:=Glob;',
  24506. ' end;',
  24507. ' r.two:=r.one;',
  24508. ' r.Glob:=r.Glob;',
  24509. ' with r do begin',
  24510. ' two:=one;',
  24511. ' Glob:=Glob;',
  24512. ' end;',
  24513. '']);
  24514. ConvertProgram;
  24515. CheckSource('TestRecordHelper_ClassVar',
  24516. LinesToStr([ // statements
  24517. 'rtl.recNewT(this, "TRec", function () {',
  24518. ' this.$eq = function (b) {',
  24519. ' return true;',
  24520. ' };',
  24521. ' this.$assign = function (s) {',
  24522. ' return this;',
  24523. ' };',
  24524. '});',
  24525. 'rtl.createHelper(this, "THelper", null, function () {',
  24526. ' this.One = 1;',
  24527. ' this.Two = 2;',
  24528. ' this.Glob = 0;',
  24529. ' this.Foo = function (w) {',
  24530. ' var Result = 0;',
  24531. ' Result = w;',
  24532. ' $mod.THelper.Two = 1 + w;',
  24533. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24534. ' Result = $mod.THelper.Glob;',
  24535. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24536. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24537. ' this.$assign(this);',
  24538. ' return Result;',
  24539. ' };',
  24540. ' this.Bar = function (w) {',
  24541. ' var Result = 0;',
  24542. ' Result = w;',
  24543. ' $mod.THelper.Two = 1;',
  24544. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24545. ' return Result;',
  24546. ' };',
  24547. '});',
  24548. 'this.r = this.TRec.$new();',
  24549. '']),
  24550. LinesToStr([ // $mod.$main
  24551. '$mod.THelper.Two = 1;',
  24552. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24553. 'var $with = $mod.TRec;',
  24554. '$mod.THelper.Two = 1;',
  24555. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24556. '$mod.THelper.Two = 1;',
  24557. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24558. 'var $with1 = $mod.r;',
  24559. '$mod.THelper.Two = 1;',
  24560. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24561. '']));
  24562. end;
  24563. procedure TTestModule.TestRecordHelper_Method_Call;
  24564. begin
  24565. StartProgram(false);
  24566. Add([
  24567. '{$modeswitch AdvancedRecords}',
  24568. 'type',
  24569. ' TRec = record',
  24570. ' procedure Run(w: word = 10);',
  24571. ' end;',
  24572. ' THelper = record helper for TRec',
  24573. ' function Foo(w: word = 1): word;',
  24574. ' end;',
  24575. 'procedure TRec.Run(w: word);',
  24576. 'begin',
  24577. ' Foo;',
  24578. ' Foo();',
  24579. ' Foo(2);',
  24580. ' Self.Foo;',
  24581. ' Self.Foo();',
  24582. ' Self.Foo(3);',
  24583. ' with Self do begin',
  24584. ' Foo;',
  24585. ' Foo();',
  24586. ' Foo(4);',
  24587. ' end;',
  24588. 'end;',
  24589. 'function THelper.foo(w: word): word;',
  24590. 'begin',
  24591. ' Run;',
  24592. ' Run();',
  24593. ' Run(11);',
  24594. ' Foo;',
  24595. ' Foo();',
  24596. ' Foo(12);',
  24597. ' Self.Foo;',
  24598. ' Self.Foo();',
  24599. ' Self.Foo(13);',
  24600. ' with Self do begin',
  24601. ' Foo;',
  24602. ' Foo();',
  24603. ' Foo(14);',
  24604. ' end;',
  24605. 'end;',
  24606. 'var Rec: TRec;',
  24607. 'begin',
  24608. ' Rec.Foo;',
  24609. ' Rec.Foo();',
  24610. ' Rec.Foo(21);',
  24611. ' with Rec do begin',
  24612. ' Foo;',
  24613. ' Foo();',
  24614. ' Foo(22);',
  24615. ' end;',
  24616. '']);
  24617. ConvertProgram;
  24618. CheckSource('TestRecordHelper_Method_Call',
  24619. LinesToStr([ // statements
  24620. 'rtl.recNewT(this, "TRec", function () {',
  24621. ' this.$eq = function (b) {',
  24622. ' return true;',
  24623. ' };',
  24624. ' this.$assign = function (s) {',
  24625. ' return this;',
  24626. ' };',
  24627. ' this.Run = function (w) {',
  24628. ' $mod.THelper.Foo.call(this, 1);',
  24629. ' $mod.THelper.Foo.call(this, 1);',
  24630. ' $mod.THelper.Foo.call(this, 2);',
  24631. ' $mod.THelper.Foo.call(this, 1);',
  24632. ' $mod.THelper.Foo.call(this, 1);',
  24633. ' $mod.THelper.Foo.call(this, 3);',
  24634. ' $mod.THelper.Foo.call(this, 1);',
  24635. ' $mod.THelper.Foo.call(this, 1);',
  24636. ' $mod.THelper.Foo.call(this, 4);',
  24637. ' };',
  24638. '});',
  24639. 'rtl.createHelper(this, "THelper", null, function () {',
  24640. ' this.Foo = function (w) {',
  24641. ' var Result = 0;',
  24642. ' this.Run(10);',
  24643. ' this.Run(10);',
  24644. ' this.Run(11);',
  24645. ' $mod.THelper.Foo.call(this, 1);',
  24646. ' $mod.THelper.Foo.call(this, 1);',
  24647. ' $mod.THelper.Foo.call(this, 12);',
  24648. ' $mod.THelper.Foo.call(this, 1);',
  24649. ' $mod.THelper.Foo.call(this, 1);',
  24650. ' $mod.THelper.Foo.call(this, 13);',
  24651. ' $mod.THelper.Foo.call(this, 1);',
  24652. ' $mod.THelper.Foo.call(this, 1);',
  24653. ' $mod.THelper.Foo.call(this, 14);',
  24654. ' return Result;',
  24655. ' };',
  24656. '});',
  24657. 'this.Rec = this.TRec.$new();',
  24658. '']),
  24659. LinesToStr([ // $mod.$main
  24660. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24661. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24662. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24663. 'var $with = $mod.Rec;',
  24664. '$mod.THelper.Foo.call($with, 1);',
  24665. '$mod.THelper.Foo.call($with, 1);',
  24666. '$mod.THelper.Foo.call($with, 22);',
  24667. '']));
  24668. end;
  24669. procedure TTestModule.TestRecordHelper_Constructor;
  24670. begin
  24671. StartProgram(false);
  24672. Add([
  24673. '{$modeswitch AdvancedRecords}',
  24674. 'type',
  24675. ' TRec = record',
  24676. ' constructor Create(w: word);',
  24677. ' end;',
  24678. ' THelper = record helper for TRec',
  24679. ' constructor NewHlp(w: word);',
  24680. ' end;',
  24681. 'var',
  24682. ' Rec: TRec;',
  24683. 'constructor TRec.Create(w: word);',
  24684. 'begin',
  24685. ' NewHlp(2);', // normal call
  24686. ' trec.NewHlp(3);', // new instance
  24687. 'end;',
  24688. 'constructor THelper.NewHlp(w: word);',
  24689. 'begin',
  24690. ' create(2);', // normal call
  24691. ' trec.create(3);', // new instance
  24692. ' NewHlp(4);', // normal call
  24693. ' trec.NewHlp(5);', // new instance
  24694. 'end;',
  24695. 'begin',
  24696. ' rec.newhlp(2);', // normal call
  24697. ' with rec do newhlp(12);', // normal call
  24698. ' trec.newhlp(3);', // new instance
  24699. ' with trec do newhlp(13);', // new instance
  24700. '']);
  24701. ConvertProgram;
  24702. CheckSource('TestRecordHelper_Constructor',
  24703. LinesToStr([ // statements
  24704. 'rtl.recNewT(this, "TRec", function () {',
  24705. ' this.$eq = function (b) {',
  24706. ' return true;',
  24707. ' };',
  24708. ' this.$assign = function (s) {',
  24709. ' return this;',
  24710. ' };',
  24711. ' this.Create = function (w) {',
  24712. ' $mod.THelper.NewHlp.call(this, 2);',
  24713. ' $mod.THelper.$new("NewHlp", [3]);',
  24714. ' return this;',
  24715. ' };',
  24716. '});',
  24717. 'rtl.createHelper(this, "THelper", null, function () {',
  24718. ' this.NewHlp = function (w) {',
  24719. ' this.Create(2);',
  24720. ' $mod.TRec.$new().Create(3);',
  24721. ' $mod.THelper.NewHlp.call(this, 4);',
  24722. ' $mod.THelper.$new("NewHlp", [5]);',
  24723. ' return this;',
  24724. ' };',
  24725. ' this.$new = function (fn, args) {',
  24726. ' return this[fn].apply($mod.TRec.$new(), args);',
  24727. ' };',
  24728. '});',
  24729. 'this.Rec = this.TRec.$new();',
  24730. '']),
  24731. LinesToStr([ // $mod.$main
  24732. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24733. 'var $with = $mod.Rec;',
  24734. '$mod.THelper.NewHlp.call($with, 12);',
  24735. '$mod.THelper.$new("NewHlp", [3]);',
  24736. 'var $with1 = $mod.TRec;',
  24737. '$mod.THelper.$new("NewHlp", [13]);',
  24738. '']));
  24739. end;
  24740. procedure TTestModule.TestTypeHelper_ClassVar;
  24741. begin
  24742. StartProgram(false);
  24743. Add([
  24744. '{$modeswitch typehelpers}',
  24745. 'type',
  24746. ' THelper = type helper for byte',
  24747. ' const',
  24748. ' One = 1;',
  24749. ' Two: word = 2;',
  24750. ' class var',
  24751. ' Glob: word;',
  24752. ' function Foo(w: word): word;',
  24753. ' class function Bar(w: word): word; static;',
  24754. ' end;',
  24755. 'function THelper.foo(w: word): word;',
  24756. 'begin',
  24757. ' Result:=w;',
  24758. ' Two:=One+w;',
  24759. ' Glob:=Glob;',
  24760. ' Result:=Self.Glob;',
  24761. ' Self.Glob:=Self.Glob;',
  24762. ' with Self do Glob:=Glob;',
  24763. 'end;',
  24764. 'class function THelper.bar(w: word): word;',
  24765. 'begin',
  24766. ' Result:=w;',
  24767. ' Two:=One;',
  24768. ' Glob:=Glob;',
  24769. 'end;',
  24770. 'var b: byte;',
  24771. 'begin',
  24772. ' byte.two:=byte.one;',
  24773. ' byte.Glob:=byte.Glob;',
  24774. ' with byte do begin',
  24775. ' two:=one;',
  24776. ' Glob:=Glob;',
  24777. ' end;',
  24778. ' b.two:=b.one;',
  24779. ' b.Glob:=b.Glob;',
  24780. ' with b do begin',
  24781. ' two:=one;',
  24782. ' Glob:=Glob;',
  24783. ' end;',
  24784. '']);
  24785. ConvertProgram;
  24786. CheckSource('TestTypeHelper_ClassVar',
  24787. LinesToStr([ // statements
  24788. 'rtl.createHelper(this, "THelper", null, function () {',
  24789. ' this.One = 1;',
  24790. ' this.Two = 2;',
  24791. ' this.Glob = 0;',
  24792. ' this.Foo = function (w) {',
  24793. ' var Result = 0;',
  24794. ' Result = w;',
  24795. ' $mod.THelper.Two = 1 + w;',
  24796. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24797. ' Result = $mod.THelper.Glob;',
  24798. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24799. ' var $with = this.get();',
  24800. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24801. ' return Result;',
  24802. ' };',
  24803. ' this.Bar = function (w) {',
  24804. ' var Result = 0;',
  24805. ' Result = w;',
  24806. ' $mod.THelper.Two = 1;',
  24807. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24808. ' return Result;',
  24809. ' };',
  24810. '});',
  24811. 'this.b = 0;',
  24812. '']),
  24813. LinesToStr([ // $mod.$main
  24814. '$mod.THelper.Two = 1;',
  24815. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24816. '$mod.THelper.Two = 1;',
  24817. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24818. '$mod.THelper.Two = 1;',
  24819. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24820. 'var $with = $mod.b;',
  24821. '$mod.THelper.Two = 1;',
  24822. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24823. '']));
  24824. end;
  24825. procedure TTestModule.TestTypeHelper_PassResultElement;
  24826. begin
  24827. StartProgram(false);
  24828. Add([
  24829. '{$modeswitch typehelpers}',
  24830. 'type',
  24831. ' THelper = type helper for word',
  24832. ' procedure DoIt(e: byte = 123);',
  24833. ' class procedure DoSome(e: byte = 456); static;',
  24834. ' end;',
  24835. 'procedure THelper.DoIt(e: byte);',
  24836. 'begin',
  24837. 'end;',
  24838. 'class procedure THelper.DoSome(e: byte);',
  24839. 'begin',
  24840. 'end;',
  24841. 'function Foo(w: word): word;',
  24842. 'begin',
  24843. ' Result.DoIt;',
  24844. ' Result.DoIt();',
  24845. ' Result.DoSome;',
  24846. ' Result.DoSome();',
  24847. ' with Result do begin',
  24848. ' DoIt;',
  24849. ' DoIt();',
  24850. ' DoSome;',
  24851. ' DoSome();',
  24852. ' end;',
  24853. 'end;',
  24854. 'begin',
  24855. '']);
  24856. ConvertProgram;
  24857. CheckSource('TestTypeHelper_PassResultElement',
  24858. LinesToStr([ // statements
  24859. 'rtl.createHelper(this, "THelper", null, function () {',
  24860. ' this.DoIt = function (e) {',
  24861. ' };',
  24862. ' this.DoSome = function (e) {',
  24863. ' };',
  24864. '});',
  24865. 'this.Foo = function (w) {',
  24866. ' var Result = 0;',
  24867. ' $mod.THelper.DoIt.call({',
  24868. ' get: function () {',
  24869. ' return Result;',
  24870. ' },',
  24871. ' set: function (v) {',
  24872. ' Result = v;',
  24873. ' }',
  24874. ' }, 123);',
  24875. ' $mod.THelper.DoIt.call({',
  24876. ' get: function () {',
  24877. ' return Result;',
  24878. ' },',
  24879. ' set: function (v) {',
  24880. ' Result = v;',
  24881. ' }',
  24882. ' }, 123);',
  24883. ' $mod.THelper.DoSome(456);',
  24884. ' $mod.THelper.DoSome(456);',
  24885. ' $mod.THelper.DoIt.call({',
  24886. ' get: function () {',
  24887. ' return Result;',
  24888. ' },',
  24889. ' set: function (v) {',
  24890. ' Result = v;',
  24891. ' }',
  24892. ' }, 123);',
  24893. ' $mod.THelper.DoIt.call({',
  24894. ' get: function () {',
  24895. ' return Result;',
  24896. ' },',
  24897. ' set: function (v) {',
  24898. ' Result = v;',
  24899. ' }',
  24900. ' }, 123);',
  24901. ' $mod.THelper.DoSome(456);',
  24902. ' $mod.THelper.DoSome(456);',
  24903. ' return Result;',
  24904. '};',
  24905. '']),
  24906. LinesToStr([ // $mod.$main
  24907. '']));
  24908. end;
  24909. procedure TTestModule.TestTypeHelper_PassArgs;
  24910. begin
  24911. StartProgram(false);
  24912. Add([
  24913. '{$modeswitch typehelpers}',
  24914. 'type',
  24915. ' THelper = type helper for word',
  24916. ' procedure DoIt(e: byte = 123);',
  24917. ' end;',
  24918. 'procedure THelper.DoIt(e: byte);',
  24919. 'begin',
  24920. 'end;',
  24921. 'procedure FooDefault(a: word);',
  24922. 'begin',
  24923. ' a.DoIt;',
  24924. ' with a do DoIt;',
  24925. 'end;',
  24926. 'procedure FooConst(const a: word);',
  24927. 'begin',
  24928. ' a.DoIt;',
  24929. ' with a do DoIt;',
  24930. 'end;',
  24931. 'procedure FooVar(var a: word);',
  24932. 'begin',
  24933. ' a.DoIt;',
  24934. ' with a do DoIt;',
  24935. 'end;',
  24936. 'begin',
  24937. '']);
  24938. ConvertProgram;
  24939. CheckSource('TestTypeHelper_PassArgs',
  24940. LinesToStr([ // statements
  24941. 'rtl.createHelper(this, "THelper", null, function () {',
  24942. ' this.DoIt = function (e) {',
  24943. ' };',
  24944. '});',
  24945. 'this.FooDefault = function (a) {',
  24946. ' $mod.THelper.DoIt.call({',
  24947. ' get: function () {',
  24948. ' return a;',
  24949. ' },',
  24950. ' set: function (v) {',
  24951. ' a = v;',
  24952. ' }',
  24953. ' }, 123);',
  24954. ' $mod.THelper.DoIt.call({',
  24955. ' get: function () {',
  24956. ' return a;',
  24957. ' },',
  24958. ' set: function (v) {',
  24959. ' a = v;',
  24960. ' }',
  24961. ' }, 123);',
  24962. '};',
  24963. 'this.FooConst = function (a) {',
  24964. ' $mod.THelper.DoIt.call({',
  24965. ' get: function () {',
  24966. ' return a;',
  24967. ' },',
  24968. ' set: function (v) {',
  24969. ' rtl.raiseE("EPropReadOnly");',
  24970. ' }',
  24971. ' }, 123);',
  24972. ' $mod.THelper.DoIt.call({',
  24973. ' get: function () {',
  24974. ' return a;',
  24975. ' },',
  24976. ' set: function () {',
  24977. ' rtl.raiseE("EPropReadOnly");',
  24978. ' }',
  24979. ' }, 123);',
  24980. '};',
  24981. 'this.FooVar = function (a) {',
  24982. ' $mod.THelper.DoIt.call(a, 123);',
  24983. ' var $with = a.get();',
  24984. ' $mod.THelper.DoIt.call(a, 123);',
  24985. '};',
  24986. '']),
  24987. LinesToStr([ // $mod.$main
  24988. '']));
  24989. end;
  24990. procedure TTestModule.TestTypeHelper_PassVarConst;
  24991. begin
  24992. StartProgram(false);
  24993. Add([
  24994. '{$modeswitch typehelpers}',
  24995. 'type',
  24996. ' THelper = type helper for word',
  24997. ' procedure DoIt(e: byte = 123);',
  24998. ' end;',
  24999. 'procedure THelper.DoIt(e: byte);',
  25000. 'begin',
  25001. 'end;',
  25002. 'var a: word;',
  25003. 'const c: word = 2;',
  25004. '{$writeableconst off}',
  25005. 'const r: word = 3;',
  25006. 'begin',
  25007. ' a.DoIt;',
  25008. ' with a do DoIt;',
  25009. ' c.DoIt;',
  25010. ' with c do DoIt;',
  25011. ' r.DoIt;',
  25012. ' with r do DoIt;',
  25013. '']);
  25014. ConvertProgram;
  25015. CheckSource('TestTypeHelper_PassVarConst',
  25016. LinesToStr([ // statements
  25017. 'rtl.createHelper(this, "THelper", null, function () {',
  25018. ' this.DoIt = function (e) {',
  25019. ' };',
  25020. '});',
  25021. 'this.a = 0;',
  25022. 'this.c = 2;',
  25023. 'this.r = 3;',
  25024. '']),
  25025. LinesToStr([ // $mod.$main
  25026. '$mod.THelper.DoIt.call({',
  25027. ' p: $mod,',
  25028. ' get: function () {',
  25029. ' return this.p.a;',
  25030. ' },',
  25031. ' set: function (v) {',
  25032. ' this.p.a = v;',
  25033. ' }',
  25034. '}, 123);',
  25035. 'var $with = $mod.a;',
  25036. '$mod.THelper.DoIt.call({',
  25037. ' get: function () {',
  25038. ' return $with;',
  25039. ' },',
  25040. ' set: function (v) {',
  25041. ' $with = v;',
  25042. ' }',
  25043. '}, 123);',
  25044. '$mod.THelper.DoIt.call({',
  25045. ' p: $mod,',
  25046. ' get: function () {',
  25047. ' return this.p.c;',
  25048. ' },',
  25049. ' set: function (v) {',
  25050. ' this.p.c = v;',
  25051. ' }',
  25052. '}, 123);',
  25053. 'var $with1 = $mod.c;',
  25054. '$mod.THelper.DoIt.call({',
  25055. ' get: function () {',
  25056. ' return $with1;',
  25057. ' },',
  25058. ' set: function (v) {',
  25059. ' $with1 = v;',
  25060. ' }',
  25061. '}, 123);',
  25062. '$mod.THelper.DoIt.call({',
  25063. ' get: function () {',
  25064. ' return 3;',
  25065. ' },',
  25066. ' set: function (v) {',
  25067. ' rtl.raiseE("EPropReadOnly");',
  25068. ' }',
  25069. '}, 123);',
  25070. 'var $with2 = 3;',
  25071. ' $mod.THelper.DoIt.call({',
  25072. ' get: function () {',
  25073. ' return $with2;',
  25074. ' },',
  25075. ' set: function () {',
  25076. ' rtl.raiseE("EPropReadOnly");',
  25077. ' }',
  25078. ' }, 123);',
  25079. '']));
  25080. end;
  25081. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25082. begin
  25083. StartProgram(false);
  25084. Add([
  25085. '{$modeswitch typehelpers}',
  25086. 'type',
  25087. ' THelper = type helper for word',
  25088. ' procedure DoIt(e: byte = 123);',
  25089. ' end;',
  25090. 'procedure THelper.DoIt(e: byte);',
  25091. 'begin',
  25092. 'end;',
  25093. 'function Foo(b: byte = 1): word;',
  25094. 'begin',
  25095. 'end;',
  25096. 'begin',
  25097. ' Foo.DoIt;',
  25098. ' Foo().DoIt;',
  25099. ' with Foo do DoIt;',
  25100. ' with Foo() do DoIt;',
  25101. '']);
  25102. ConvertProgram;
  25103. CheckSource('TestTypeHelper_PassFuncResult',
  25104. LinesToStr([ // statements
  25105. 'rtl.createHelper(this, "THelper", null, function () {',
  25106. ' this.DoIt = function (e) {',
  25107. ' };',
  25108. '});',
  25109. 'this.Foo = function (b) {',
  25110. ' var Result = 0;',
  25111. ' return Result;',
  25112. '};',
  25113. '']),
  25114. LinesToStr([ // $mod.$main
  25115. '$mod.THelper.DoIt.call({',
  25116. ' a: $mod.Foo(1),',
  25117. ' get: function () {',
  25118. ' return this.a;',
  25119. ' },',
  25120. ' set: function (v) {',
  25121. ' this.a = v;',
  25122. ' }',
  25123. '}, 123);',
  25124. '$mod.THelper.DoIt.call({',
  25125. ' a: $mod.Foo(1),',
  25126. ' get: function () {',
  25127. ' return this.a;',
  25128. ' },',
  25129. ' set: function (v) {',
  25130. ' this.a = v;',
  25131. ' }',
  25132. '}, 123);',
  25133. 'var $with = $mod.Foo(1);',
  25134. '$mod.THelper.DoIt.call({',
  25135. ' get: function () {',
  25136. ' return $with;',
  25137. ' },',
  25138. ' set: function (v) {',
  25139. ' $with = v;',
  25140. ' }',
  25141. '}, 123);',
  25142. 'var $with1 = $mod.Foo(1);',
  25143. '$mod.THelper.DoIt.call({',
  25144. ' get: function () {',
  25145. ' return $with1;',
  25146. ' },',
  25147. ' set: function (v) {',
  25148. ' $with1 = v;',
  25149. ' }',
  25150. '}, 123);',
  25151. '']));
  25152. end;
  25153. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25154. begin
  25155. StartProgram(false);
  25156. Add([
  25157. '{$modeswitch typehelpers}',
  25158. 'type',
  25159. ' TObject = class',
  25160. ' FField: word;',
  25161. ' procedure SetField(Value: word);',
  25162. ' property Field: word read FField write SetField;',
  25163. ' end;',
  25164. ' THelper = type helper for word',
  25165. ' procedure Fly;',
  25166. ' class procedure Run; static;',
  25167. ' end;',
  25168. 'procedure TObject.SetField(Value: word);',
  25169. 'begin',
  25170. ' Field.Fly;',
  25171. ' Field.Run;',
  25172. ' Self.Field.Fly;',
  25173. ' Self.Field.Run;',
  25174. ' with Self do begin',
  25175. ' Field.Fly;',
  25176. ' Field.Run;',
  25177. ' end;',
  25178. ' with Self.Field do begin',
  25179. ' Fly;',
  25180. ' Run;',
  25181. ' end;',
  25182. 'end;',
  25183. 'procedure THelper.Fly;',
  25184. 'begin',
  25185. 'end;',
  25186. 'class procedure THelper.Run;',
  25187. 'begin',
  25188. 'end;',
  25189. 'var',
  25190. ' o: TObject;',
  25191. 'begin',
  25192. ' o.Field.Fly;',
  25193. ' o.Field.Run;',
  25194. ' with o do begin',
  25195. ' Field.Fly;',
  25196. ' Field.Run;',
  25197. ' end;',
  25198. ' with o.Field do begin',
  25199. ' Fly;',
  25200. ' Run;',
  25201. ' end;',
  25202. '']);
  25203. ConvertProgram;
  25204. CheckSource('TestTypeHelper_PassPropertyField',
  25205. LinesToStr([ // statements
  25206. 'rtl.createClass(this, "TObject", null, function () {',
  25207. ' this.$init = function () {',
  25208. ' this.FField = 0;',
  25209. ' };',
  25210. ' this.$final = function () {',
  25211. ' };',
  25212. ' this.SetField = function (Value) {',
  25213. ' $mod.THelper.Fly.call({',
  25214. ' p: this,',
  25215. ' get: function () {',
  25216. ' return this.p.FField;',
  25217. ' },',
  25218. ' set: function (v) {',
  25219. ' this.p.FField = v;',
  25220. ' }',
  25221. ' });',
  25222. ' $mod.THelper.Run();',
  25223. ' $mod.THelper.Fly.call({',
  25224. ' p: this,',
  25225. ' get: function () {',
  25226. ' return this.p.FField;',
  25227. ' },',
  25228. ' set: function (v) {',
  25229. ' this.p.FField = v;',
  25230. ' }',
  25231. ' });',
  25232. ' $mod.THelper.Run();',
  25233. ' $mod.THelper.Fly.call({',
  25234. ' p: this,',
  25235. ' get: function () {',
  25236. ' return this.p.FField;',
  25237. ' },',
  25238. ' set: function (v) {',
  25239. ' this.p.FField = v;',
  25240. ' }',
  25241. ' });',
  25242. ' $mod.THelper.Run();',
  25243. ' var $with = this.FField;',
  25244. ' $mod.THelper.Fly.call({',
  25245. ' get: function () {',
  25246. ' return $with;',
  25247. ' },',
  25248. ' set: function (v) {',
  25249. ' $with = v;',
  25250. ' }',
  25251. ' });',
  25252. ' $mod.THelper.Run();',
  25253. ' };',
  25254. '});',
  25255. 'rtl.createHelper(this, "THelper", null, function () {',
  25256. ' this.Fly = function () {',
  25257. ' };',
  25258. ' this.Run = function () {',
  25259. ' };',
  25260. '});',
  25261. 'this.o = null;',
  25262. '']),
  25263. LinesToStr([ // $mod.$main
  25264. '$mod.THelper.Fly.call({',
  25265. ' p: $mod.o,',
  25266. ' get: function () {',
  25267. ' return this.p.FField;',
  25268. ' },',
  25269. ' set: function (v) {',
  25270. ' this.p.FField = v;',
  25271. ' }',
  25272. '});',
  25273. '$mod.THelper.Run();',
  25274. 'var $with = $mod.o;',
  25275. '$mod.THelper.Fly.call({',
  25276. ' p: $with,',
  25277. ' get: function () {',
  25278. ' return this.p.FField;',
  25279. ' },',
  25280. ' set: function (v) {',
  25281. ' this.p.FField = v;',
  25282. ' }',
  25283. '});',
  25284. '$mod.THelper.Run();',
  25285. 'var $with1 = $mod.o.FField;',
  25286. '$mod.THelper.Fly.call({',
  25287. ' get: function () {',
  25288. ' return $with1;',
  25289. ' },',
  25290. ' set: function (v) {',
  25291. ' $with1 = v;',
  25292. ' }',
  25293. '});',
  25294. '$mod.THelper.Run();',
  25295. '']));
  25296. end;
  25297. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  25298. begin
  25299. StartProgram(false);
  25300. Add([
  25301. '{$modeswitch typehelpers}',
  25302. 'type',
  25303. ' TObject = class',
  25304. ' FField: word;',
  25305. ' function GetField: word;',
  25306. ' property Field: word read GetField write FField;',
  25307. ' end;',
  25308. ' THelper = type helper for word',
  25309. ' procedure Fly;',
  25310. ' class procedure Run; static;',
  25311. ' end;',
  25312. 'function TObject.GetField: word;',
  25313. 'begin',
  25314. ' Field.Fly;',
  25315. ' Field.Run;',
  25316. ' Self.Field.Fly;',
  25317. ' Self.Field.Run;',
  25318. ' with Self do begin',
  25319. ' Field.Fly;',
  25320. ' Field.Run;',
  25321. ' end;',
  25322. ' with Self.Field do begin',
  25323. ' Fly;',
  25324. ' Run;',
  25325. ' end;',
  25326. 'end;',
  25327. 'procedure THelper.Fly;',
  25328. 'begin',
  25329. 'end;',
  25330. 'class procedure THelper.Run;',
  25331. 'begin',
  25332. 'end;',
  25333. 'var',
  25334. ' o: TObject;',
  25335. 'begin',
  25336. ' o.Field.Fly;',
  25337. ' o.Field.Run;',
  25338. ' with o do begin',
  25339. ' Field.Fly;',
  25340. ' Field.Run;',
  25341. ' end;',
  25342. ' with o.Field do begin',
  25343. ' Fly;',
  25344. ' Run;',
  25345. ' end;',
  25346. '']);
  25347. ConvertProgram;
  25348. CheckSource('TestTypeHelper_PassPropertyGetter',
  25349. LinesToStr([ // statements
  25350. 'rtl.createClass(this, "TObject", null, function () {',
  25351. ' this.$init = function () {',
  25352. ' this.FField = 0;',
  25353. ' };',
  25354. ' this.$final = function () {',
  25355. ' };',
  25356. ' this.GetField = function () {',
  25357. ' var Result = 0;',
  25358. ' $mod.THelper.Fly.call({',
  25359. ' p: this.GetField(),',
  25360. ' get: function () {',
  25361. ' return this.p;',
  25362. ' },',
  25363. ' set: function (v) {',
  25364. ' this.p = v;',
  25365. ' }',
  25366. ' });',
  25367. ' $mod.THelper.Run();',
  25368. ' $mod.THelper.Fly.call({',
  25369. ' p: this.GetField(),',
  25370. ' get: function () {',
  25371. ' return this.p;',
  25372. ' },',
  25373. ' set: function (v) {',
  25374. ' this.p = v;',
  25375. ' }',
  25376. ' });',
  25377. ' $mod.THelper.Run();',
  25378. ' $mod.THelper.Fly.call({',
  25379. ' p: this.GetField(),',
  25380. ' get: function () {',
  25381. ' return this.p;',
  25382. ' },',
  25383. ' set: function (v) {',
  25384. ' this.p = v;',
  25385. ' }',
  25386. ' });',
  25387. ' $mod.THelper.Run();',
  25388. ' var $with = this.GetField();',
  25389. ' $mod.THelper.Fly.call({',
  25390. ' get: function () {',
  25391. ' return $with;',
  25392. ' },',
  25393. ' set: function (v) {',
  25394. ' $with = v;',
  25395. ' }',
  25396. ' });',
  25397. ' $mod.THelper.Run();',
  25398. ' return Result;',
  25399. ' };',
  25400. '});',
  25401. 'rtl.createHelper(this, "THelper", null, function () {',
  25402. ' this.Fly = function () {',
  25403. ' };',
  25404. ' this.Run = function () {',
  25405. ' };',
  25406. '});',
  25407. 'this.o = null;',
  25408. '']),
  25409. LinesToStr([ // $mod.$main
  25410. '$mod.THelper.Fly.call({',
  25411. ' p: $mod.o.GetField(),',
  25412. ' get: function () {',
  25413. ' return this.p;',
  25414. ' },',
  25415. ' set: function (v) {',
  25416. ' this.p = v;',
  25417. ' }',
  25418. '});',
  25419. '$mod.THelper.Run();',
  25420. 'var $with = $mod.o;',
  25421. '$mod.THelper.Fly.call({',
  25422. ' p: $with.GetField(),',
  25423. ' get: function () {',
  25424. ' return this.p;',
  25425. ' },',
  25426. ' set: function (v) {',
  25427. ' this.p = v;',
  25428. ' }',
  25429. '});',
  25430. '$mod.THelper.Run();',
  25431. 'var $with1 = $mod.o.GetField();',
  25432. '$mod.THelper.Fly.call({',
  25433. ' get: function () {',
  25434. ' return $with1;',
  25435. ' },',
  25436. ' set: function (v) {',
  25437. ' $with1 = v;',
  25438. ' }',
  25439. '});',
  25440. '$mod.THelper.Run();',
  25441. '']));
  25442. end;
  25443. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25444. begin
  25445. StartProgram(false);
  25446. Add([
  25447. '{$modeswitch typehelpers}',
  25448. 'type',
  25449. ' TObject = class',
  25450. ' class var FField: word;',
  25451. ' class procedure SetField(Value: word);',
  25452. ' class property Field: word read FField write SetField;',
  25453. ' end;',
  25454. ' THelper = type helper for word',
  25455. ' procedure Fly(n: byte);',
  25456. ' end;',
  25457. 'class procedure TObject.SetField(Value: word);',
  25458. 'begin',
  25459. ' Field.Fly(1);',
  25460. ' Self.Field.Fly(2);',
  25461. ' with Self do Field.Fly(3);',
  25462. ' with Self.Field do Fly(4);',
  25463. ' TObject.Field.Fly(5);',
  25464. ' with TObject do Field.Fly(6);',
  25465. ' with TObject.Field do Fly(7);',
  25466. 'end;',
  25467. 'procedure THelper.Fly(n: byte);',
  25468. 'begin',
  25469. 'end;',
  25470. 'var',
  25471. ' o: TObject;',
  25472. 'begin',
  25473. ' o.Field.Fly(11);',
  25474. ' with o do Field.Fly(12);',
  25475. ' with o.Field do Fly(13);',
  25476. ' TObject.Field.Fly(14);',
  25477. ' with TObject do Field.Fly(15);',
  25478. ' with TObject.Field do Fly(16);',
  25479. '']);
  25480. ConvertProgram;
  25481. CheckSource('TestTypeHelper_PassClassPropertyField',
  25482. LinesToStr([ // statements
  25483. 'rtl.createClass(this, "TObject", null, function () {',
  25484. ' this.FField = 0;',
  25485. ' this.$init = function () {',
  25486. ' };',
  25487. ' this.$final = function () {',
  25488. ' };',
  25489. ' this.SetField = function (Value) {',
  25490. ' $mod.THelper.Fly.call({',
  25491. ' p: this,',
  25492. ' get: function () {',
  25493. ' return this.p.FField;',
  25494. ' },',
  25495. ' set: function (v) {',
  25496. ' $mod.TObject.FField = v;',
  25497. ' }',
  25498. ' }, 1);',
  25499. ' $mod.THelper.Fly.call({',
  25500. ' p: this,',
  25501. ' get: function () {',
  25502. ' return this.p.FField;',
  25503. ' },',
  25504. ' set: function (v) {',
  25505. ' $mod.TObject.FField = v;',
  25506. ' }',
  25507. ' }, 2);',
  25508. ' $mod.THelper.Fly.call({',
  25509. ' p: this,',
  25510. ' get: function () {',
  25511. ' return this.p.FField;',
  25512. ' },',
  25513. ' set: function (v) {',
  25514. ' $mod.TObject.FField = v;',
  25515. ' }',
  25516. ' }, 3);',
  25517. ' var $with = this.FField;',
  25518. ' $mod.THelper.Fly.call({',
  25519. ' get: function () {',
  25520. ' return $with;',
  25521. ' },',
  25522. ' set: function (v) {',
  25523. ' $with = v;',
  25524. ' }',
  25525. ' }, 4);',
  25526. ' $mod.THelper.Fly.call({',
  25527. ' p: $mod.TObject,',
  25528. ' get: function () {',
  25529. ' return this.p.FField;',
  25530. ' },',
  25531. ' set: function (v) {',
  25532. ' $mod.TObject.FField = v;',
  25533. ' }',
  25534. ' }, 5);',
  25535. ' var $with1 = $mod.TObject;',
  25536. ' $mod.THelper.Fly.call({',
  25537. ' p: $with1,',
  25538. ' get: function () {',
  25539. ' return this.p.FField;',
  25540. ' },',
  25541. ' set: function (v) {',
  25542. ' $mod.TObject.FField = v;',
  25543. ' }',
  25544. ' }, 6);',
  25545. ' var $with2 = $mod.TObject.FField;',
  25546. ' $mod.THelper.Fly.call({',
  25547. ' get: function () {',
  25548. ' return $with2;',
  25549. ' },',
  25550. ' set: function (v) {',
  25551. ' $with2 = v;',
  25552. ' }',
  25553. ' }, 7);',
  25554. ' };',
  25555. '});',
  25556. 'rtl.createHelper(this, "THelper", null, function () {',
  25557. ' this.Fly = function (n) {',
  25558. ' };',
  25559. '});',
  25560. 'this.o = null;',
  25561. '']),
  25562. LinesToStr([ // $mod.$main
  25563. '$mod.THelper.Fly.call({',
  25564. ' p: $mod.o,',
  25565. ' get: function () {',
  25566. ' return this.p.FField;',
  25567. ' },',
  25568. ' set: function (v) {',
  25569. ' $mod.TObject.FField = v;',
  25570. ' }',
  25571. '}, 11);',
  25572. 'var $with = $mod.o;',
  25573. '$mod.THelper.Fly.call({',
  25574. ' p: $with,',
  25575. ' get: function () {',
  25576. ' return this.p.FField;',
  25577. ' },',
  25578. ' set: function (v) {',
  25579. ' $mod.TObject.FField = v;',
  25580. ' }',
  25581. '}, 12);',
  25582. 'var $with1 = $mod.o.FField;',
  25583. '$mod.THelper.Fly.call({',
  25584. ' get: function () {',
  25585. ' return $with1;',
  25586. ' },',
  25587. ' set: function (v) {',
  25588. ' $with1 = v;',
  25589. ' }',
  25590. '}, 13);',
  25591. '$mod.THelper.Fly.call({',
  25592. ' p: $mod.TObject,',
  25593. ' get: function () {',
  25594. ' return this.p.FField;',
  25595. ' },',
  25596. ' set: function (v) {',
  25597. ' $mod.TObject.FField = v;',
  25598. ' }',
  25599. '}, 14);',
  25600. 'var $with2 = $mod.TObject;',
  25601. '$mod.THelper.Fly.call({',
  25602. ' p: $with2,',
  25603. ' get: function () {',
  25604. ' return this.p.FField;',
  25605. ' },',
  25606. ' set: function (v) {',
  25607. ' $mod.TObject.FField = v;',
  25608. ' }',
  25609. '}, 15);',
  25610. 'var $with3 = $mod.TObject.FField;',
  25611. '$mod.THelper.Fly.call({',
  25612. ' get: function () {',
  25613. ' return $with3;',
  25614. ' },',
  25615. ' set: function (v) {',
  25616. ' $with3 = v;',
  25617. ' }',
  25618. '}, 16);',
  25619. '']));
  25620. end;
  25621. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25622. begin
  25623. StartProgram(false);
  25624. Add([
  25625. '{$modeswitch typehelpers}',
  25626. 'type',
  25627. ' TObject = class',
  25628. ' class var FField: word;',
  25629. ' class function GetField: word; static;',
  25630. ' class property Field: word read GetField write FField;',
  25631. ' end;',
  25632. ' THelper = type helper for word',
  25633. ' procedure Fly(n: byte);',
  25634. ' end;',
  25635. 'class function TObject.GetField: word;',
  25636. 'begin',
  25637. ' Field.Fly(1);',
  25638. ' TObject.Field.Fly(5);',
  25639. ' with TObject do Field.Fly(6);',
  25640. ' with TObject.Field do Fly(7);',
  25641. 'end;',
  25642. 'procedure THelper.Fly(n: byte);',
  25643. 'begin',
  25644. 'end;',
  25645. 'var',
  25646. ' o: TObject;',
  25647. 'begin',
  25648. ' o.Field.Fly(11);',
  25649. ' with o do Field.Fly(12);',
  25650. ' with o.Field do Fly(13);',
  25651. '']);
  25652. ConvertProgram;
  25653. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25654. LinesToStr([ // statements
  25655. 'rtl.createClass(this, "TObject", null, function () {',
  25656. ' this.FField = 0;',
  25657. ' this.$init = function () {',
  25658. ' };',
  25659. ' this.$final = function () {',
  25660. ' };',
  25661. ' this.GetField = function () {',
  25662. ' var Result = 0;',
  25663. ' $mod.THelper.Fly.call({',
  25664. ' p: $mod.TObject.GetField(),',
  25665. ' get: function () {',
  25666. ' return this.p;',
  25667. ' },',
  25668. ' set: function (v) {',
  25669. ' this.p = v;',
  25670. ' }',
  25671. ' }, 1);',
  25672. ' $mod.THelper.Fly.call({',
  25673. ' p: $mod.TObject.GetField(),',
  25674. ' get: function () {',
  25675. ' return this.p;',
  25676. ' },',
  25677. ' set: function (v) {',
  25678. ' this.p = v;',
  25679. ' }',
  25680. ' }, 5);',
  25681. ' var $with = $mod.TObject;',
  25682. ' $mod.THelper.Fly.call({',
  25683. ' p: $with.GetField(),',
  25684. ' get: function () {',
  25685. ' return this.p;',
  25686. ' },',
  25687. ' set: function (v) {',
  25688. ' this.p = v;',
  25689. ' }',
  25690. ' }, 6);',
  25691. ' var $with1 = $mod.TObject.GetField();',
  25692. ' $mod.THelper.Fly.call({',
  25693. ' get: function () {',
  25694. ' return $with1;',
  25695. ' },',
  25696. ' set: function (v) {',
  25697. ' $with1 = v;',
  25698. ' }',
  25699. ' }, 7);',
  25700. ' return Result;',
  25701. ' };',
  25702. '});',
  25703. 'rtl.createHelper(this, "THelper", null, function () {',
  25704. ' this.Fly = function (n) {',
  25705. ' };',
  25706. '});',
  25707. 'this.o = null;',
  25708. '']),
  25709. LinesToStr([ // $mod.$main
  25710. '$mod.THelper.Fly.call({',
  25711. ' p: $mod.TObject.GetField(),',
  25712. ' get: function () {',
  25713. ' return this.p;',
  25714. ' },',
  25715. ' set: function (v) {',
  25716. ' this.p = v;',
  25717. ' }',
  25718. '}, 11);',
  25719. 'var $with = $mod.o;',
  25720. '$mod.THelper.Fly.call({',
  25721. ' p: $with.GetField(),',
  25722. ' get: function () {',
  25723. ' return this.p;',
  25724. ' },',
  25725. ' set: function (v) {',
  25726. ' this.p = v;',
  25727. ' }',
  25728. '}, 12);',
  25729. 'var $with1 = $mod.TObject.GetField();',
  25730. '$mod.THelper.Fly.call({',
  25731. ' get: function () {',
  25732. ' return $with1;',
  25733. ' },',
  25734. ' set: function (v) {',
  25735. ' $with1 = v;',
  25736. ' }',
  25737. '}, 13);',
  25738. '']));
  25739. end;
  25740. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25741. begin
  25742. StartProgram(false);
  25743. Add([
  25744. '{$modeswitch typehelpers}',
  25745. 'type',
  25746. ' TObject = class',
  25747. ' class var FField: word;',
  25748. ' class function GetField: word;',
  25749. ' class property Field: word read GetField write FField;',
  25750. ' end;',
  25751. ' TClass = class of TObject;',
  25752. ' THelper = type helper for word',
  25753. ' procedure Fly(n: byte);',
  25754. ' end;',
  25755. 'class function TObject.GetField: word;',
  25756. 'begin',
  25757. ' Field.Fly(1);',
  25758. ' Self.Field.Fly(5);',
  25759. ' with Self do Field.Fly(6);',
  25760. ' with Self.Field do Fly(7);',
  25761. 'end;',
  25762. 'procedure THelper.Fly(n: byte);',
  25763. 'begin',
  25764. 'end;',
  25765. 'var',
  25766. ' o: TObject;',
  25767. ' c: TClass;',
  25768. 'begin',
  25769. ' o.Field.Fly(11);',
  25770. ' with o do Field.Fly(12);',
  25771. ' with o.Field do Fly(13);',
  25772. ' c.Field.Fly(14);',
  25773. ' with c do Field.Fly(15);',
  25774. ' with c.Field do Fly(16);',
  25775. '']);
  25776. ConvertProgram;
  25777. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25778. LinesToStr([ // statements
  25779. 'rtl.createClass(this, "TObject", null, function () {',
  25780. ' this.FField = 0;',
  25781. ' this.$init = function () {',
  25782. ' };',
  25783. ' this.$final = function () {',
  25784. ' };',
  25785. ' this.GetField = function () {',
  25786. ' var Result = 0;',
  25787. ' $mod.THelper.Fly.call({',
  25788. ' p: this.GetField(),',
  25789. ' get: function () {',
  25790. ' return this.p;',
  25791. ' },',
  25792. ' set: function (v) {',
  25793. ' this.p = v;',
  25794. ' }',
  25795. ' }, 1);',
  25796. ' $mod.THelper.Fly.call({',
  25797. ' p: this.GetField(),',
  25798. ' get: function () {',
  25799. ' return this.p;',
  25800. ' },',
  25801. ' set: function (v) {',
  25802. ' this.p = v;',
  25803. ' }',
  25804. ' }, 5);',
  25805. ' $mod.THelper.Fly.call({',
  25806. ' p: this.GetField(),',
  25807. ' get: function () {',
  25808. ' return this.p;',
  25809. ' },',
  25810. ' set: function (v) {',
  25811. ' this.p = v;',
  25812. ' }',
  25813. ' }, 6);',
  25814. ' var $with = this.GetField();',
  25815. ' $mod.THelper.Fly.call({',
  25816. ' get: function () {',
  25817. ' return $with;',
  25818. ' },',
  25819. ' set: function (v) {',
  25820. ' $with = v;',
  25821. ' }',
  25822. ' }, 7);',
  25823. ' return Result;',
  25824. ' };',
  25825. '});',
  25826. 'rtl.createHelper(this, "THelper", null, function () {',
  25827. ' this.Fly = function (n) {',
  25828. ' };',
  25829. '});',
  25830. 'this.o = null;',
  25831. 'this.c = null;',
  25832. '']),
  25833. LinesToStr([ // $mod.$main
  25834. '$mod.THelper.Fly.call({',
  25835. ' p: $mod.o.$class.GetField(),',
  25836. ' get: function () {',
  25837. ' return this.p;',
  25838. ' },',
  25839. ' set: function (v) {',
  25840. ' this.p = v;',
  25841. ' }',
  25842. '}, 11);',
  25843. 'var $with = $mod.o;',
  25844. '$mod.THelper.Fly.call({',
  25845. ' p: $with.$class.GetField(),',
  25846. ' get: function () {',
  25847. ' return this.p;',
  25848. ' },',
  25849. ' set: function (v) {',
  25850. ' this.p = v;',
  25851. ' }',
  25852. '}, 12);',
  25853. 'var $with1 = $mod.o.$class.GetField();',
  25854. '$mod.THelper.Fly.call({',
  25855. ' get: function () {',
  25856. ' return $with1;',
  25857. ' },',
  25858. ' set: function (v) {',
  25859. ' $with1 = v;',
  25860. ' }',
  25861. '}, 13);',
  25862. '$mod.THelper.Fly.call({',
  25863. ' p: $mod.c.GetField(),',
  25864. ' get: function () {',
  25865. ' return this.p;',
  25866. ' },',
  25867. ' set: function (v) {',
  25868. ' this.p = v;',
  25869. ' }',
  25870. '}, 14);',
  25871. 'var $with2 = $mod.c;',
  25872. '$mod.THelper.Fly.call({',
  25873. ' p: $with2.GetField(),',
  25874. ' get: function () {',
  25875. ' return this.p;',
  25876. ' },',
  25877. ' set: function (v) {',
  25878. ' this.p = v;',
  25879. ' }',
  25880. '}, 15);',
  25881. 'var $with3 = $mod.c.GetField();',
  25882. '$mod.THelper.Fly.call({',
  25883. ' get: function () {',
  25884. ' return $with3;',
  25885. ' },',
  25886. ' set: function (v) {',
  25887. ' $with3 = v;',
  25888. ' }',
  25889. '}, 16);',
  25890. '']));
  25891. end;
  25892. procedure TTestModule.TestTypeHelper_Property;
  25893. begin
  25894. StartProgram(false);
  25895. Add([
  25896. '{$modeswitch typehelpers}',
  25897. 'type',
  25898. ' THelper = type helper for word',
  25899. ' function GetSize: longint;',
  25900. ' procedure SetSize(Value: longint);',
  25901. ' property Size: longint read GetSize write SetSize;',
  25902. ' end;',
  25903. 'function THelper.GetSize: longint;',
  25904. 'begin',
  25905. ' Result:=Size+1;',
  25906. ' Size:=2;',
  25907. ' Result:=Self.Size+3;',
  25908. ' Self.Size:=4;',
  25909. ' with Self do begin',
  25910. ' Result:=Size+5;',
  25911. ' Size:=6;',
  25912. ' end;',
  25913. 'end;',
  25914. 'procedure THelper.SetSize(Value: longint);',
  25915. 'begin',
  25916. 'end;',
  25917. 'var w: word;',
  25918. 'begin',
  25919. ' w:=w.Size+7;',
  25920. ' w.Size:=w+8;',
  25921. ' with w do begin',
  25922. ' w:=Size+9;',
  25923. ' Size:=w+10;',
  25924. ' end;',
  25925. '']);
  25926. ConvertProgram;
  25927. CheckSource('TestTypeHelper_Property',
  25928. LinesToStr([ // statements
  25929. 'rtl.createHelper(this, "THelper", null, function () {',
  25930. ' this.GetSize = function () {',
  25931. ' var Result = 0;',
  25932. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25933. ' $mod.THelper.SetSize.call(this, 2);',
  25934. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25935. ' $mod.THelper.SetSize.call(this, 4);',
  25936. ' var $with = this.get();',
  25937. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25938. ' $mod.THelper.SetSize.call(this, 6);',
  25939. ' return Result;',
  25940. ' };',
  25941. ' this.SetSize = function (Value) {',
  25942. ' };',
  25943. '});',
  25944. 'this.w = 0;',
  25945. '']),
  25946. LinesToStr([ // $mod.$main
  25947. '$mod.w = $mod.THelper.GetSize.call({',
  25948. ' p: $mod,',
  25949. ' get: function () {',
  25950. ' return this.p.w;',
  25951. ' },',
  25952. ' set: function (v) {',
  25953. ' this.p.w = v;',
  25954. ' }',
  25955. '}) + 7;',
  25956. '$mod.THelper.SetSize.call({',
  25957. ' p: $mod,',
  25958. ' get: function () {',
  25959. ' return this.p.w;',
  25960. ' },',
  25961. ' set: function (v) {',
  25962. ' this.p.w = v;',
  25963. ' }',
  25964. '}, $mod.w + 8);',
  25965. 'var $with = $mod.w;',
  25966. '$mod.w = $mod.THelper.GetSize.call({',
  25967. ' get: function () {',
  25968. ' return $with;',
  25969. ' },',
  25970. ' set: function (v) {',
  25971. ' $with = v;',
  25972. ' }',
  25973. '}) + 9;',
  25974. '$mod.THelper.SetSize.call({',
  25975. ' get: function () {',
  25976. ' return $with;',
  25977. ' },',
  25978. ' set: function (v) {',
  25979. ' $with = v;',
  25980. ' }',
  25981. '}, $mod.w + 10);',
  25982. '']));
  25983. end;
  25984. procedure TTestModule.TestTypeHelper_Property_Array;
  25985. begin
  25986. StartProgram(false);
  25987. Add([
  25988. '{$modeswitch typehelpers}',
  25989. 'type',
  25990. ' THelper = type helper for word',
  25991. ' function GetItems(Index: byte): boolean;',
  25992. ' procedure SetItems(Index: byte; Value: boolean);',
  25993. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  25994. ' end;',
  25995. 'function THelper.GetItems(Index: byte): boolean;',
  25996. 'begin',
  25997. ' Result:=Items[1];',
  25998. ' Items[2]:=false;',
  25999. ' Result:=Self.Items[3];',
  26000. ' Self.Items[4]:=true;',
  26001. ' with Self do begin',
  26002. ' Result:=Items[5];',
  26003. ' Items[6]:=false;',
  26004. ' end;',
  26005. 'end;',
  26006. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26007. 'begin',
  26008. 'end;',
  26009. 'var',
  26010. ' w: word;',
  26011. ' b: boolean;',
  26012. 'begin',
  26013. ' b:=w.Items[1];',
  26014. ' w.Items[2]:=b;',
  26015. ' with w do begin',
  26016. ' b:=Items[3];',
  26017. ' Items[4]:=b;',
  26018. ' end;',
  26019. '']);
  26020. ConvertProgram;
  26021. CheckSource('TestTypeHelper_Property_Array',
  26022. LinesToStr([ // statements
  26023. 'rtl.createHelper(this, "THelper", null, function () {',
  26024. ' this.GetItems = function (Index) {',
  26025. ' var Result = false;',
  26026. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26027. ' $mod.THelper.SetItems.call(this, 2, false);',
  26028. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26029. ' $mod.THelper.SetItems.call(this, 4, true);',
  26030. ' var $with = this.get();',
  26031. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26032. ' $mod.THelper.SetItems.call(this, 6, false);',
  26033. ' return Result;',
  26034. ' };',
  26035. ' this.SetItems = function (Index, Value) {',
  26036. ' };',
  26037. '});',
  26038. 'this.w = 0;',
  26039. 'this.b = false;',
  26040. '']),
  26041. LinesToStr([ // $mod.$main
  26042. '$mod.b = $mod.THelper.GetItems.call({',
  26043. ' p: $mod,',
  26044. ' get: function () {',
  26045. ' return this.p.w;',
  26046. ' },',
  26047. ' set: function (v) {',
  26048. ' this.p.w = v;',
  26049. ' }',
  26050. '}, 1);',
  26051. '$mod.THelper.SetItems.call({',
  26052. ' p: $mod,',
  26053. ' get: function () {',
  26054. ' return this.p.w;',
  26055. ' },',
  26056. ' set: function (v) {',
  26057. ' this.p.w = v;',
  26058. ' }',
  26059. '}, 2, $mod.b);',
  26060. 'var $with = $mod.w;',
  26061. '$mod.b = $mod.THelper.GetItems.call({',
  26062. ' get: function () {',
  26063. ' return $with;',
  26064. ' },',
  26065. ' set: function (v) {',
  26066. ' $with = v;',
  26067. ' }',
  26068. '}, 3);',
  26069. '$mod.THelper.SetItems.call({',
  26070. ' get: function () {',
  26071. ' return $with;',
  26072. ' },',
  26073. ' set: function (v) {',
  26074. ' $with = v;',
  26075. ' }',
  26076. '}, 4, $mod.b);',
  26077. '']));
  26078. end;
  26079. procedure TTestModule.TestTypeHelper_ClassProperty;
  26080. begin
  26081. StartProgram(false);
  26082. Add([
  26083. '{$modeswitch typehelpers}',
  26084. 'type',
  26085. ' THelper = type helper for word',
  26086. ' class function GetSize: longint; static;',
  26087. ' class procedure SetSize(Value: longint); static;',
  26088. ' class property Size: longint read GetSize write SetSize;',
  26089. ' end;',
  26090. 'class function THelper.GetSize: longint;',
  26091. 'begin',
  26092. ' Result:=Size+1;',
  26093. ' Size:=2;',
  26094. 'end;',
  26095. 'class procedure THelper.SetSize(Value: longint);',
  26096. 'begin',
  26097. 'end;',
  26098. 'begin',
  26099. '']);
  26100. ConvertProgram;
  26101. CheckSource('TestTypeHelper_ClassProperty',
  26102. LinesToStr([ // statements
  26103. 'rtl.createHelper(this, "THelper", null, function () {',
  26104. ' this.GetSize = function () {',
  26105. ' var Result = 0;',
  26106. ' Result = $mod.THelper.GetSize() + 1;',
  26107. ' $mod.THelper.SetSize(2);',
  26108. ' return Result;',
  26109. ' };',
  26110. ' this.SetSize = function (Value) {',
  26111. ' };',
  26112. '});',
  26113. '']),
  26114. LinesToStr([ // $mod.$main
  26115. '']));
  26116. end;
  26117. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26118. begin
  26119. StartProgram(false);
  26120. Add([
  26121. '{$modeswitch typehelpers}',
  26122. 'type',
  26123. ' THelper = type helper for word',
  26124. ' class function GetItems(Index: byte): boolean; static;',
  26125. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26126. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26127. ' end;',
  26128. 'class function THelper.GetItems(Index: byte): boolean;',
  26129. 'begin',
  26130. ' Result:=Items[1];',
  26131. ' Items[2]:=false;',
  26132. 'end;',
  26133. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26134. 'begin',
  26135. 'end;',
  26136. 'var',
  26137. ' w: word;',
  26138. ' b: boolean;',
  26139. 'begin',
  26140. ' b:=w.Items[1];',
  26141. ' w.Items[2]:=b;',
  26142. ' with w do begin',
  26143. ' b:=Items[3];',
  26144. ' Items[4]:=b;',
  26145. ' end;',
  26146. '']);
  26147. ConvertProgram;
  26148. CheckSource('TestTypeHelper_ClassProperty_Array',
  26149. LinesToStr([ // statements
  26150. 'rtl.createHelper(this, "THelper", null, function () {',
  26151. ' this.GetItems = function (Index) {',
  26152. ' var Result = false;',
  26153. ' Result = $mod.THelper.GetItems(1);',
  26154. ' $mod.THelper.SetItems(2, false);',
  26155. ' return Result;',
  26156. ' };',
  26157. ' this.SetItems = function (Index, Value) {',
  26158. ' };',
  26159. '});',
  26160. 'this.w = 0;',
  26161. 'this.b = false;',
  26162. '']),
  26163. LinesToStr([ // $mod.$main
  26164. '$mod.b = $mod.THelper.GetItems(1);',
  26165. '$mod.THelper.SetItems(2, $mod.b);',
  26166. 'var $with = $mod.w;',
  26167. '$mod.b = $mod.THelper.GetItems(3);',
  26168. '$mod.THelper.SetItems(4, $mod.b);',
  26169. '']));
  26170. end;
  26171. procedure TTestModule.TestTypeHelper_ClassMethod;
  26172. begin
  26173. StartProgram(false);
  26174. Add([
  26175. '{$modeswitch typehelpers}',
  26176. 'type',
  26177. ' THelper = type helper for word',
  26178. ' class procedure DoStatic; static;',
  26179. ' end;',
  26180. 'class procedure THelper.DoStatic;',
  26181. 'begin',
  26182. ' DoStatic;',
  26183. ' DoStatic();',
  26184. 'end;',
  26185. 'var w: word;',
  26186. 'begin',
  26187. ' w.DoStatic;',
  26188. ' w.DoStatic();',
  26189. '']);
  26190. ConvertProgram;
  26191. CheckSource('TestTypeHelper_ClassMethod',
  26192. LinesToStr([ // statements
  26193. 'rtl.createHelper(this, "THelper", null, function () {',
  26194. ' this.DoStatic = function () {',
  26195. ' $mod.THelper.DoStatic();',
  26196. ' $mod.THelper.DoStatic();',
  26197. ' };',
  26198. '});',
  26199. 'this.w = 0;',
  26200. '']),
  26201. LinesToStr([ // $mod.$main
  26202. '$mod.THelper.DoStatic();',
  26203. '$mod.THelper.DoStatic();',
  26204. '']));
  26205. end;
  26206. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26207. begin
  26208. StartProgram(false);
  26209. Add([
  26210. '{$modeswitch typehelpers}',
  26211. 'type',
  26212. ' THelper = type helper for word',
  26213. ' procedure Run; external name ''Run'';',
  26214. ' end;',
  26215. 'var w: word;',
  26216. 'begin',
  26217. ' w.Run;',
  26218. '']);
  26219. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26220. ConvertProgram;
  26221. end;
  26222. procedure TTestModule.TestTypeHelper_Constructor;
  26223. begin
  26224. StartProgram(false);
  26225. Add([
  26226. '{$modeswitch typehelpers}',
  26227. 'type',
  26228. ' THelper = type helper for word',
  26229. ' constructor Init(e: longint);',
  26230. ' end;',
  26231. 'constructor THelper.Init(e: longint);',
  26232. 'begin',
  26233. ' Self:=e;',
  26234. ' Init(e+1);',
  26235. 'end;',
  26236. 'var w: word;',
  26237. 'begin',
  26238. ' w:=word.Init(2);',
  26239. ' w:=w.Init(3);',
  26240. ' with word do w:=Init(4);',
  26241. ' with w do w:=Init(5);',
  26242. '']);
  26243. ConvertProgram;
  26244. CheckSource('TestTypeHelper_Constructor',
  26245. LinesToStr([ // statements
  26246. 'rtl.createHelper(this, "THelper", null, function () {',
  26247. ' this.Init = function (e) {',
  26248. ' this.set(e);',
  26249. ' $mod.THelper.Init.call(this, e + 1);',
  26250. ' return this.get();',
  26251. ' };',
  26252. ' this.$new = function (fn, args) {',
  26253. ' return this[fn].apply({',
  26254. ' p: 0,',
  26255. ' get: function () {',
  26256. ' return this.p;',
  26257. ' },',
  26258. ' set: function (v) {',
  26259. ' this.p = v;',
  26260. ' }',
  26261. ' }, args);',
  26262. ' };',
  26263. '});',
  26264. 'this.w = 0;',
  26265. '']),
  26266. LinesToStr([ // $mod.$main
  26267. '$mod.w = $mod.THelper.$new("Init", [2]);',
  26268. '$mod.w = $mod.THelper.Init.call({',
  26269. ' p: $mod,',
  26270. ' get: function () {',
  26271. ' return this.p.w;',
  26272. ' },',
  26273. ' set: function (v) {',
  26274. ' this.p.w = v;',
  26275. ' }',
  26276. '}, 3);',
  26277. '$mod.w = $mod.THelper.$new("Init", [4]);',
  26278. 'var $with = $mod.w;',
  26279. '$mod.w = $mod.THelper.Init.call({',
  26280. ' get: function () {',
  26281. ' return $with;',
  26282. ' },',
  26283. ' set: function (v) {',
  26284. ' $with = v;',
  26285. ' }',
  26286. '}, 5);',
  26287. '']));
  26288. end;
  26289. procedure TTestModule.TestTypeHelper_Word;
  26290. begin
  26291. StartProgram(false);
  26292. Add([
  26293. '{$modeswitch typehelpers}',
  26294. 'type',
  26295. ' THelper = type helper for word',
  26296. ' procedure DoIt(e: byte = 123);',
  26297. ' end;',
  26298. 'procedure THelper.DoIt(e: byte);',
  26299. 'begin',
  26300. ' Self:=e;',
  26301. ' Self:=Self+1;',
  26302. ' with Self do Doit;',
  26303. 'end;',
  26304. 'begin',
  26305. ' word(3).DoIt;',
  26306. '']);
  26307. ConvertProgram;
  26308. CheckSource('TestTypeHelper_Word',
  26309. LinesToStr([ // statements
  26310. 'rtl.createHelper(this, "THelper", null, function () {',
  26311. ' this.DoIt = function (e) {',
  26312. ' this.set(e);',
  26313. ' this.set(this.get() + 1);',
  26314. ' var $with = this.get();',
  26315. ' $mod.THelper.DoIt.call(this, 123);',
  26316. ' };',
  26317. '});',
  26318. '']),
  26319. LinesToStr([ // $mod.$main
  26320. '$mod.THelper.DoIt.call({',
  26321. ' get: function () {',
  26322. ' return 3;',
  26323. ' },',
  26324. ' set: function (v) {',
  26325. ' rtl.raiseE("EPropReadOnly");',
  26326. ' }',
  26327. '}, 123);',
  26328. '']));
  26329. end;
  26330. procedure TTestModule.TestTypeHelper_Boolean;
  26331. begin
  26332. StartProgram(false);
  26333. Add([
  26334. '{$modeswitch typehelpers}',
  26335. 'type',
  26336. ' Integer = longint;',
  26337. ' THelper = type helper for boolean',
  26338. ' procedure Run(e: wordbool = true);',
  26339. ' end;',
  26340. 'procedure THelper.Run(e: wordbool);',
  26341. 'begin',
  26342. ' Self:=e;',
  26343. ' Self:=not Self;',
  26344. ' with Self do Run;',
  26345. ' if Integer(Self)=0 then ;',
  26346. 'end;',
  26347. 'begin',
  26348. ' boolean(3).Run;',
  26349. '']);
  26350. ConvertProgram;
  26351. CheckSource('TestTypeHelper_Boolean',
  26352. LinesToStr([ // statements
  26353. 'rtl.createHelper(this, "THelper", null, function () {',
  26354. ' this.Run = function (e) {',
  26355. ' this.set(e);',
  26356. ' this.set(!this.get());',
  26357. ' var $with = this.get();',
  26358. ' $mod.THelper.Run.call(this, true);',
  26359. ' if ((this.get() ? 1 : 0) === 0) ;',
  26360. ' };',
  26361. '});',
  26362. '']),
  26363. LinesToStr([ // $mod.$main
  26364. '$mod.THelper.Run.call({',
  26365. ' a: 3 != 0,',
  26366. ' get: function () {',
  26367. ' return this.a;',
  26368. ' },',
  26369. ' set: function (v) {',
  26370. ' rtl.raiseE("EPropReadOnly");',
  26371. ' }',
  26372. '}, true);',
  26373. '']));
  26374. end;
  26375. procedure TTestModule.TestTypeHelper_WordBool;
  26376. begin
  26377. StartProgram(false);
  26378. Add([
  26379. '{$modeswitch typehelpers}',
  26380. 'type',
  26381. ' Integer = longint;',
  26382. ' THelper = type helper for WordBool',
  26383. ' procedure Run(e: wordbool = true);',
  26384. ' end;',
  26385. 'procedure THelper.Run(e: wordbool);',
  26386. 'var i: integer;',
  26387. 'begin',
  26388. ' i:=Integer(Self);',
  26389. 'end;',
  26390. 'var w: wordbool;',
  26391. 'begin',
  26392. ' w.Run;',
  26393. ' wordbool(3).Run;',
  26394. '']);
  26395. ConvertProgram;
  26396. CheckSource('TestTypeHelper_WordBool',
  26397. LinesToStr([ // statements
  26398. 'rtl.createHelper(this, "THelper", null, function () {',
  26399. ' this.Run = function (e) {',
  26400. ' var i = 0;',
  26401. ' i = (this.get() ? 1 : 0);',
  26402. ' };',
  26403. '});',
  26404. 'this.w = false;',
  26405. '']),
  26406. LinesToStr([ // $mod.$main
  26407. '$mod.THelper.Run.call({',
  26408. ' p: $mod,',
  26409. ' get: function () {',
  26410. ' return this.p.w;',
  26411. ' },',
  26412. ' set: function (v) {',
  26413. ' this.p.w = v;',
  26414. ' }',
  26415. '}, true);',
  26416. '$mod.THelper.Run.call({',
  26417. ' a: 3 != 0,',
  26418. ' get: function () {',
  26419. ' return this.a;',
  26420. ' },',
  26421. ' set: function (v) {',
  26422. ' rtl.raiseE("EPropReadOnly");',
  26423. ' }',
  26424. '}, true);',
  26425. '']));
  26426. end;
  26427. procedure TTestModule.TestTypeHelper_Double;
  26428. begin
  26429. StartProgram(false);
  26430. Add([
  26431. '{$modeswitch typehelpers}',
  26432. 'type',
  26433. ' Float = type double;',
  26434. ' THelper = type helper for Float',
  26435. ' const NPI = 3.141592;',
  26436. ' function ToStr: String;',
  26437. ' end;',
  26438. 'function THelper.ToStr: String;',
  26439. 'begin',
  26440. 'end;',
  26441. 'procedure DoIt(s: string);',
  26442. 'begin',
  26443. 'end;',
  26444. 'var f: Float;',
  26445. 'begin',
  26446. ' DoIt(f.toStr);',
  26447. ' DoIt(f.toStr());',
  26448. ' (f*f).toStr;',
  26449. ' DoIt((f*f).toStr);',
  26450. '']);
  26451. ConvertProgram;
  26452. CheckSource('TestTypeHelper_Double',
  26453. LinesToStr([ // statements
  26454. 'rtl.createHelper(this, "THelper", null, function () {',
  26455. ' this.NPI = 3.141592;',
  26456. ' this.ToStr = function () {',
  26457. ' var Result = "";',
  26458. ' return Result;',
  26459. ' };',
  26460. '});',
  26461. 'this.DoIt = function (s) {',
  26462. '};',
  26463. 'this.f = 0.0;',
  26464. '']),
  26465. LinesToStr([ // $mod.$main
  26466. '$mod.DoIt($mod.THelper.ToStr.call({',
  26467. ' p: $mod,',
  26468. ' get: function () {',
  26469. ' return this.p.f;',
  26470. ' },',
  26471. ' set: function (v) {',
  26472. ' this.p.f = v;',
  26473. ' }',
  26474. '}));',
  26475. '$mod.DoIt($mod.THelper.ToStr.call({',
  26476. ' p: $mod,',
  26477. ' get: function () {',
  26478. ' return this.p.f;',
  26479. ' },',
  26480. ' set: function (v) {',
  26481. ' this.p.f = v;',
  26482. ' }',
  26483. '}));',
  26484. '$mod.THelper.ToStr.call({',
  26485. ' a: $mod.f * $mod.f,',
  26486. ' get: function () {',
  26487. ' return this.a;',
  26488. ' },',
  26489. ' set: function (v) {',
  26490. ' rtl.raiseE("EPropReadOnly");',
  26491. ' }',
  26492. '});',
  26493. '$mod.DoIt($mod.THelper.ToStr.call({',
  26494. ' a: $mod.f * $mod.f,',
  26495. ' get: function () {',
  26496. ' return this.a;',
  26497. ' },',
  26498. ' set: function (v) {',
  26499. ' rtl.raiseE("EPropReadOnly");',
  26500. ' }',
  26501. '}));',
  26502. '']));
  26503. end;
  26504. procedure TTestModule.TestTypeHelper_NativeInt;
  26505. begin
  26506. StartProgram(false);
  26507. Add([
  26508. '{$modeswitch typehelpers}',
  26509. 'type',
  26510. ' MaxInt = type nativeint;',
  26511. ' THelperI = type helper for MaxInt',
  26512. ' function ToStr: String;',
  26513. ' end;',
  26514. ' MaxUInt = type nativeuint;',
  26515. ' THelperU = type helper for MaxUInt',
  26516. ' function ToStr: String;',
  26517. ' end;',
  26518. 'function THelperI.ToStr: String;',
  26519. 'begin',
  26520. ' Result:=str(Self);',
  26521. 'end;',
  26522. 'function THelperU.ToStr: String;',
  26523. 'begin',
  26524. ' Result:=str(Self);',
  26525. 'end;',
  26526. 'procedure DoIt(s: string);',
  26527. 'begin',
  26528. 'end;',
  26529. 'var i: MaxInt;',
  26530. 'begin',
  26531. ' DoIt(i.toStr);',
  26532. ' DoIt(i.toStr());',
  26533. ' (i*i).toStr;',
  26534. ' DoIt((i*i).toStr);',
  26535. '']);
  26536. ConvertProgram;
  26537. CheckSource('TestTypeHelper_NativeInt',
  26538. LinesToStr([ // statements
  26539. 'rtl.createHelper(this, "THelperI", null, function () {',
  26540. ' this.ToStr = function () {',
  26541. ' var Result = "";',
  26542. ' Result = "" + this.get();',
  26543. ' return Result;',
  26544. ' };',
  26545. '});',
  26546. 'rtl.createHelper(this, "THelperU", null, function () {',
  26547. ' this.ToStr = function () {',
  26548. ' var Result = "";',
  26549. ' Result = "" + this.get();',
  26550. ' return Result;',
  26551. ' };',
  26552. '});',
  26553. 'this.DoIt = function (s) {',
  26554. '};',
  26555. 'this.i = 0;',
  26556. '']),
  26557. LinesToStr([ // $mod.$main
  26558. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26559. ' p: $mod,',
  26560. ' get: function () {',
  26561. ' return this.p.i;',
  26562. ' },',
  26563. ' set: function (v) {',
  26564. ' this.p.i = v;',
  26565. ' }',
  26566. '}));',
  26567. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26568. ' p: $mod,',
  26569. ' get: function () {',
  26570. ' return this.p.i;',
  26571. ' },',
  26572. ' set: function (v) {',
  26573. ' this.p.i = v;',
  26574. ' }',
  26575. '}));',
  26576. '$mod.THelperI.ToStr.call({',
  26577. ' a: $mod.i * $mod.i,',
  26578. ' get: function () {',
  26579. ' return this.a;',
  26580. ' },',
  26581. ' set: function (v) {',
  26582. ' rtl.raiseE("EPropReadOnly");',
  26583. ' }',
  26584. '});',
  26585. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26586. ' a: $mod.i * $mod.i,',
  26587. ' get: function () {',
  26588. ' return this.a;',
  26589. ' },',
  26590. ' set: function (v) {',
  26591. ' rtl.raiseE("EPropReadOnly");',
  26592. ' }',
  26593. '}));',
  26594. '']));
  26595. end;
  26596. procedure TTestModule.TestTypeHelper_StringChar;
  26597. begin
  26598. StartProgram(false);
  26599. Add([
  26600. '{$modeswitch typehelpers}',
  26601. 'type',
  26602. ' TStringHelper = type helper for string',
  26603. ' procedure DoIt(e: byte = 123);',
  26604. ' end;',
  26605. ' TCharHelper = type helper for AnsiChar',
  26606. ' procedure Fly;',
  26607. ' end;',
  26608. 'procedure TStringHelper.DoIt(e: byte);',
  26609. 'begin',
  26610. ' Self[1]:=''c'';',
  26611. ' Self[2]:=Self[3];',
  26612. 'end;',
  26613. 'procedure TCharHelper.Fly;',
  26614. 'begin',
  26615. ' Self:=''c'';',
  26616. 'end;',
  26617. 'begin',
  26618. ' ''abc''.DoIt;',
  26619. ' ''xyz''.DoIt();',
  26620. ' ''c''.Fly();',
  26621. '']);
  26622. ConvertProgram;
  26623. CheckSource('TestTypeHelper_StringChar',
  26624. LinesToStr([ // statements
  26625. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26626. ' this.DoIt = function (e) {',
  26627. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26628. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26629. ' };',
  26630. '});',
  26631. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26632. ' this.Fly = function () {',
  26633. ' this.set("c");',
  26634. ' };',
  26635. '});',
  26636. '']),
  26637. LinesToStr([ // $mod.$main
  26638. '$mod.TStringHelper.DoIt.call({',
  26639. ' get: function () {',
  26640. ' return "abc";',
  26641. ' },',
  26642. ' set: function (v) {',
  26643. ' rtl.raiseE("EPropReadOnly");',
  26644. ' }',
  26645. '}, 123);',
  26646. '$mod.TStringHelper.DoIt.call({',
  26647. ' get: function () {',
  26648. ' return "xyz";',
  26649. ' },',
  26650. ' set: function (v) {',
  26651. ' rtl.raiseE("EPropReadOnly");',
  26652. ' }',
  26653. '}, 123);',
  26654. '$mod.TCharHelper.Fly.call({',
  26655. ' get: function () {',
  26656. ' return "c";',
  26657. ' },',
  26658. ' set: function (v) {',
  26659. ' rtl.raiseE("EPropReadOnly");',
  26660. ' }',
  26661. '});',
  26662. '']));
  26663. end;
  26664. procedure TTestModule.TestTypeHelper_JSValue;
  26665. begin
  26666. StartProgram(false);
  26667. Add([
  26668. '{$modeswitch typehelpers}',
  26669. 'type',
  26670. ' TExtValue = type jsvalue;',
  26671. ' THelper = type helper for TExtValue',
  26672. ' function ToStr: String;',
  26673. ' end;',
  26674. 'function THelper.ToStr: String;',
  26675. 'begin',
  26676. 'end;',
  26677. 'var',
  26678. ' s: string;',
  26679. ' v: TExtValue;',
  26680. 'begin',
  26681. ' s:=v.toStr;',
  26682. ' s:=v.toStr();',
  26683. ' TExtValue(s).toStr;',
  26684. '']);
  26685. ConvertProgram;
  26686. CheckSource('TestTypeHelper_JSValue',
  26687. LinesToStr([ // statements
  26688. 'rtl.createHelper(this, "THelper", null, function () {',
  26689. ' this.ToStr = function () {',
  26690. ' var Result = "";',
  26691. ' return Result;',
  26692. ' };',
  26693. '});',
  26694. 'this.s = "";',
  26695. 'this.v = undefined;',
  26696. '']),
  26697. LinesToStr([ // $mod.$main
  26698. '$mod.s = $mod.THelper.ToStr.call({',
  26699. ' p: $mod,',
  26700. ' get: function () {',
  26701. ' return this.p.v;',
  26702. ' },',
  26703. ' set: function (v) {',
  26704. ' this.p.v = v;',
  26705. ' }',
  26706. '});',
  26707. '$mod.s = $mod.THelper.ToStr.call({',
  26708. ' p: $mod,',
  26709. ' get: function () {',
  26710. ' return this.p.v;',
  26711. ' },',
  26712. ' set: function (v) {',
  26713. ' this.p.v = v;',
  26714. ' }',
  26715. '});',
  26716. '$mod.THelper.ToStr.call({',
  26717. ' p: $mod,',
  26718. ' get: function () {',
  26719. ' return this.p.s;',
  26720. ' },',
  26721. ' set: function (v) {',
  26722. ' rtl.raiseE("EPropReadOnly");',
  26723. ' }',
  26724. '});',
  26725. '']));
  26726. end;
  26727. procedure TTestModule.TestTypeHelper_Array;
  26728. begin
  26729. StartProgram(false);
  26730. Add([
  26731. '{$modeswitch typehelpers}',
  26732. 'type',
  26733. ' TArrOfBool = array of boolean;',
  26734. ' TArrOfJS = array of jsvalue;',
  26735. ' THelper = type helper for TArrOfBool',
  26736. ' procedure DoIt(e: byte = 123);',
  26737. ' end;',
  26738. 'procedure THelper.DoIt(e: byte);',
  26739. 'begin',
  26740. ' Self[1]:=true;',
  26741. ' Self[2]:=not Self[3];',
  26742. ' SetLength(Self,4);',
  26743. ' Self:=Concat(Self,[true]);',
  26744. 'end;',
  26745. 'var',
  26746. ' b: TArrOfBool;',
  26747. ' j: TArrOfJS;',
  26748. 'begin',
  26749. ' b.DoIt;',
  26750. ' TArrOfBool(j).DoIt();',
  26751. '']);
  26752. ConvertProgram;
  26753. CheckSource('TestTypeHelper_Array',
  26754. LinesToStr([ // statements
  26755. 'rtl.createHelper(this, "THelper", null, function () {',
  26756. ' this.DoIt = function (e) {',
  26757. ' this.get()[1] = true;',
  26758. ' this.get()[2] = !this.get()[3];',
  26759. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26760. ' this.set(rtl.arrayPushN(this.get(), true));',
  26761. ' };',
  26762. '});',
  26763. 'this.b = [];',
  26764. 'this.j = [];',
  26765. '']),
  26766. LinesToStr([ // $mod.$main
  26767. '$mod.THelper.DoIt.call({',
  26768. ' p: $mod,',
  26769. ' get: function () {',
  26770. ' return this.p.b;',
  26771. ' },',
  26772. ' set: function (v) {',
  26773. ' this.p.b = v;',
  26774. ' }',
  26775. '}, 123);',
  26776. '$mod.THelper.DoIt.call({',
  26777. ' p: $mod,',
  26778. ' get: function () {',
  26779. ' return this.p.j;',
  26780. ' },',
  26781. ' set: function (v) {',
  26782. ' this.p.j = v;',
  26783. ' }',
  26784. '}, 123);',
  26785. '']));
  26786. end;
  26787. procedure TTestModule.TestTypeHelper_EnumType;
  26788. begin
  26789. StartProgram(false);
  26790. Add([
  26791. '{$modeswitch typehelpers}',
  26792. 'type',
  26793. ' TEnum = (red,blue);',
  26794. ' THelper = type helper for TEnum',
  26795. ' procedure DoIt(e: byte = 123);',
  26796. ' class procedure Swing(w: word); static;',
  26797. ' end;',
  26798. 'procedure THelper.DoIt(e: byte);',
  26799. 'begin',
  26800. ' Self:=red;',
  26801. ' Self:=succ(Self);',
  26802. ' with Self do Doit;',
  26803. 'end;',
  26804. 'class procedure THelper.Swing(w: word);',
  26805. 'begin',
  26806. 'end;',
  26807. 'var e: TEnum;',
  26808. 'begin',
  26809. ' e.DoIt;',
  26810. ' red.DoIt;',
  26811. ' TEnum.blue.DoIt;',
  26812. ' TEnum(1).DoIt;',
  26813. ' TEnum.Swing(3);',
  26814. '']);
  26815. ConvertProgram;
  26816. CheckSource('TestTypeHelper_EnumType',
  26817. LinesToStr([ // statements
  26818. 'this.TEnum = {',
  26819. ' "0": "red",',
  26820. ' red: 0,',
  26821. ' "1": "blue",',
  26822. ' blue: 1',
  26823. '};',
  26824. 'rtl.createHelper(this, "THelper", null, function () {',
  26825. ' this.DoIt = function (e) {',
  26826. ' this.set($mod.TEnum.red);',
  26827. ' this.set(this.get() + 1);',
  26828. ' var $with = this.get();',
  26829. ' $mod.THelper.DoIt.call(this, 123);',
  26830. ' };',
  26831. ' this.Swing = function (w) {',
  26832. ' };',
  26833. '});',
  26834. 'this.e = 0;',
  26835. '']),
  26836. LinesToStr([ // $mod.$main
  26837. '$mod.THelper.DoIt.call({',
  26838. ' p: $mod,',
  26839. ' get: function () {',
  26840. ' return this.p.e;',
  26841. ' },',
  26842. ' set: function (v) {',
  26843. ' this.p.e = v;',
  26844. ' }',
  26845. '}, 123);',
  26846. '$mod.THelper.DoIt.call({',
  26847. ' p: $mod.TEnum,',
  26848. ' get: function () {',
  26849. ' return this.p.red;',
  26850. ' },',
  26851. ' set: function (v) {',
  26852. ' rtl.raiseE("EPropReadOnly");',
  26853. ' }',
  26854. '}, 123);',
  26855. '$mod.THelper.DoIt.call({',
  26856. ' p: $mod.TEnum,',
  26857. ' get: function () {',
  26858. ' return this.p.blue;',
  26859. ' },',
  26860. ' set: function (v) {',
  26861. ' rtl.raiseE("EPropReadOnly");',
  26862. ' }',
  26863. '}, 123);',
  26864. '$mod.THelper.DoIt.call({',
  26865. ' get: function () {',
  26866. ' return 1;',
  26867. ' },',
  26868. ' set: function (v) {',
  26869. ' rtl.raiseE("EPropReadOnly");',
  26870. ' }',
  26871. '}, 123);',
  26872. '$mod.THelper.Swing(3);',
  26873. '']));
  26874. end;
  26875. procedure TTestModule.TestTypeHelper_SetType;
  26876. begin
  26877. StartProgram(false);
  26878. Add([
  26879. '{$modeswitch typehelpers}',
  26880. 'type',
  26881. ' TEnum = (red,blue);',
  26882. ' TSetOfEnum = set of TEnum;',
  26883. ' THelper = type helper for TSetOfEnum',
  26884. ' procedure DoIt(e: byte = 123);',
  26885. ' constructor Init(e: TEnum);',
  26886. ' constructor InitEmpty;',
  26887. ' end;',
  26888. 'procedure THelper.DoIt(e: byte);',
  26889. 'begin',
  26890. ' Self:=[];',
  26891. ' Self:=[red];',
  26892. ' Include(Self,blue);',
  26893. 'end;',
  26894. 'constructor THelper.Init(e: TEnum);',
  26895. 'begin',
  26896. ' Self:=[];',
  26897. ' Self:=[e];',
  26898. ' Include(Self,blue);',
  26899. 'end;',
  26900. 'constructor THelper.InitEmpty;',
  26901. 'begin',
  26902. 'end;',
  26903. 'var s: TSetOfEnum;',
  26904. 'begin',
  26905. ' s.DoIt;',
  26906. //' [red].DoIt;',
  26907. //' with s do DoIt;',
  26908. //' with [red,blue] do DoIt;',
  26909. ' s:=TSetOfEnum.Init(blue);',
  26910. ' s:=s.Init(blue);',
  26911. '']);
  26912. ConvertProgram;
  26913. CheckSource('TestTypeHelper_SetType',
  26914. LinesToStr([ // statements
  26915. 'this.TEnum = {',
  26916. ' "0": "red",',
  26917. ' red: 0,',
  26918. ' "1": "blue",',
  26919. ' blue: 1',
  26920. '};',
  26921. 'rtl.createHelper(this, "THelper", null, function () {',
  26922. ' this.DoIt = function (e) {',
  26923. ' this.set({});',
  26924. ' this.set(rtl.createSet($mod.TEnum.red));',
  26925. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26926. ' };',
  26927. ' this.Init = function (e) {',
  26928. ' this.set({});',
  26929. ' this.set(rtl.createSet(e));',
  26930. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26931. ' return this.get();',
  26932. ' };',
  26933. ' this.InitEmpty = function () {',
  26934. ' return this.get();',
  26935. ' };',
  26936. ' this.$new = function (fn, args) {',
  26937. ' return this[fn].apply({',
  26938. ' p: {},',
  26939. ' get: function () {',
  26940. ' return this.p;',
  26941. ' },',
  26942. ' set: function (v) {',
  26943. ' this.p = v;',
  26944. ' }',
  26945. ' }, args);',
  26946. ' };',
  26947. '});',
  26948. 'this.s = {};',
  26949. '']),
  26950. LinesToStr([ // $mod.$main
  26951. '$mod.THelper.DoIt.call({',
  26952. ' p: $mod,',
  26953. ' get: function () {',
  26954. ' return this.p.s;',
  26955. ' },',
  26956. ' set: function (v) {',
  26957. ' this.p.s = v;',
  26958. ' }',
  26959. '}, 123);',
  26960. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  26961. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  26962. ' p: $mod,',
  26963. ' get: function () {',
  26964. ' return this.p.s;',
  26965. ' },',
  26966. ' set: function (v) {',
  26967. ' this.p.s = v;',
  26968. ' }',
  26969. '}, $mod.TEnum.blue));',
  26970. '']));
  26971. end;
  26972. procedure TTestModule.TestTypeHelper_InterfaceType;
  26973. begin
  26974. StartProgram(false);
  26975. Add([
  26976. '{$interfaces com}',
  26977. '{$modeswitch typehelpers}',
  26978. 'type',
  26979. ' IUnknown = interface',
  26980. ' function _AddRef: longint;',
  26981. ' function _Release: longint;',
  26982. ' end;',
  26983. ' TObject = class(IUnknown)',
  26984. ' function _AddRef: longint; virtual; abstract;',
  26985. ' function _Release: longint; virtual; abstract;',
  26986. ' end;',
  26987. ' THelper = type helper for IUnknown',
  26988. ' procedure Fly(e: byte = 123);',
  26989. ' class procedure Run; static;',
  26990. ' end;',
  26991. 'var',
  26992. ' i: IUnknown;',
  26993. ' o: TObject;',
  26994. 'procedure THelper.Fly(e: byte);',
  26995. 'begin',
  26996. ' i:=Self;',
  26997. ' o:=Self as TObject;',
  26998. ' Self:=nil;',
  26999. ' Self:=i;',
  27000. ' Self:=o;',
  27001. ' with Self do begin',
  27002. ' Fly;',
  27003. ' Fly();',
  27004. ' end;',
  27005. 'end;',
  27006. 'class procedure THelper.Run;',
  27007. 'var l: IUnknown;',
  27008. 'begin',
  27009. ' l.Fly;',
  27010. ' l.Fly();',
  27011. 'end;',
  27012. 'begin',
  27013. ' i.Fly;',
  27014. ' i.Fly();',
  27015. ' i.Run;',
  27016. ' i.Run();',
  27017. ' IUnknown.Run;',
  27018. ' IUnknown.Run();',
  27019. '']);
  27020. ConvertProgram;
  27021. CheckSource('TestTypeHelper_InterfaceType',
  27022. LinesToStr([ // statements
  27023. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27024. 'rtl.createClass(this, "TObject", null, function () {',
  27025. ' this.$init = function () {',
  27026. ' };',
  27027. ' this.$final = function () {',
  27028. ' };',
  27029. ' rtl.addIntf(this, $mod.IUnknown);',
  27030. '});',
  27031. 'rtl.createHelper(this, "THelper", null, function () {',
  27032. ' this.Fly = function (e) {',
  27033. ' var $ir = rtl.createIntfRefs();',
  27034. ' try {',
  27035. ' rtl.setIntfP($mod, "i", this.get());',
  27036. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27037. ' this.set(null);',
  27038. ' this.set($mod.i);',
  27039. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27040. ' var $with = this.get();',
  27041. ' $mod.THelper.Fly.call(this, 123);',
  27042. ' $mod.THelper.Fly.call(this, 123);',
  27043. ' } finally {',
  27044. ' $ir.free();',
  27045. ' };',
  27046. ' };',
  27047. ' this.Run = function () {',
  27048. ' var l = null;',
  27049. ' try {',
  27050. ' $mod.THelper.Fly.call({',
  27051. ' get: function () {',
  27052. ' return l;',
  27053. ' },',
  27054. ' set: function (v) {',
  27055. ' l = rtl.setIntfL(l, v);',
  27056. ' }',
  27057. ' }, 123);',
  27058. ' $mod.THelper.Fly.call({',
  27059. ' get: function () {',
  27060. ' return l;',
  27061. ' },',
  27062. ' set: function (v) {',
  27063. ' l = rtl.setIntfL(l, v);',
  27064. ' }',
  27065. ' }, 123);',
  27066. ' } finally {',
  27067. ' rtl._Release(l);',
  27068. ' };',
  27069. ' };',
  27070. '});',
  27071. 'this.i = null;',
  27072. 'this.o = null;',
  27073. '']),
  27074. LinesToStr([ // $mod.$main
  27075. '$mod.THelper.Fly.call({',
  27076. ' p: $mod,',
  27077. ' get: function () {',
  27078. ' return this.p.i;',
  27079. ' },',
  27080. ' set: function (v) {',
  27081. ' rtl.setIntfP(this.p, "i", v);',
  27082. ' }',
  27083. '}, 123);',
  27084. '$mod.THelper.Fly.call({',
  27085. ' p: $mod,',
  27086. ' get: function () {',
  27087. ' return this.p.i;',
  27088. ' },',
  27089. ' set: function (v) {',
  27090. ' rtl.setIntfP(this.p, "i", v);',
  27091. ' }',
  27092. '}, 123);',
  27093. '$mod.THelper.Run();',
  27094. '$mod.THelper.Run();',
  27095. '$mod.THelper.Run();',
  27096. '$mod.THelper.Run();',
  27097. '']));
  27098. end;
  27099. procedure TTestModule.TestTypeHelper_NestedSelf;
  27100. begin
  27101. StartProgram(false);
  27102. Add([
  27103. '{$modeswitch typehelpers}',
  27104. 'type',
  27105. ' THelper = type helper for string',
  27106. ' procedure Run(Value: string);',
  27107. ' end;',
  27108. 'procedure THelper.Run(Value: string);',
  27109. ' function Sub(i: nativeint): boolean;',
  27110. ' begin',
  27111. ' Result:=Self[i+1]=Value[i];',
  27112. ' end;',
  27113. 'begin',
  27114. ' if Self[3]=Value[4] then ;',
  27115. 'end;',
  27116. 'begin',
  27117. '']);
  27118. ConvertProgram;
  27119. CheckSource('TestTypeHelper_NestedSelf',
  27120. LinesToStr([ // statements
  27121. 'rtl.createHelper(this, "THelper", null, function () {',
  27122. ' this.Run = function (Value) {',
  27123. ' var $Self = this;',
  27124. ' function Sub(i) {',
  27125. ' var Result = false;',
  27126. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27127. ' return Result;',
  27128. ' };',
  27129. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27130. ' };',
  27131. '});',
  27132. '']),
  27133. LinesToStr([ // $mod.$main
  27134. '']));
  27135. end;
  27136. procedure TTestModule.TestProcType;
  27137. begin
  27138. StartProgram(false);
  27139. Add([
  27140. 'type',
  27141. ' TProcInt = procedure(vI: longint = 1);',
  27142. 'procedure DoIt(vJ: longint);',
  27143. 'begin end;',
  27144. 'var',
  27145. ' b: boolean;',
  27146. ' vP, vQ: tprocint;',
  27147. 'begin',
  27148. ' vp:=nil;',
  27149. ' vp:=vp;',
  27150. ' vp:=@doit;',
  27151. ' vp;',
  27152. ' vp();',
  27153. ' vp(2);',
  27154. ' b:=vp=nil;',
  27155. ' b:=nil=vp;',
  27156. ' b:=vp=vq;',
  27157. ' b:=vp=@doit;',
  27158. ' b:=@doit=vp;',
  27159. ' b:=vp<>nil;',
  27160. ' b:=nil<>vp;',
  27161. ' b:=vp<>vq;',
  27162. ' b:=vp<>@doit;',
  27163. ' b:=@doit<>vp;',
  27164. ' b:=Assigned(vp);',
  27165. ' if Assigned(vp) then ;']);
  27166. ConvertProgram;
  27167. CheckSource('TestProcType',
  27168. LinesToStr([ // statements
  27169. 'this.DoIt = function(vJ) {',
  27170. '};',
  27171. 'this.b = false;',
  27172. 'this.vP = null;',
  27173. 'this.vQ = null;'
  27174. ]),
  27175. LinesToStr([ // $mod.$main
  27176. '$mod.vP = null;',
  27177. '$mod.vP = $mod.vP;',
  27178. '$mod.vP = $mod.DoIt;',
  27179. '$mod.vP(1);',
  27180. '$mod.vP(1);',
  27181. '$mod.vP(2);',
  27182. '$mod.b = $mod.vP === null;',
  27183. '$mod.b = null === $mod.vP;',
  27184. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27185. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27186. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27187. '$mod.b = $mod.vP !== null;',
  27188. '$mod.b = null !== $mod.vP;',
  27189. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27190. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27191. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27192. '$mod.b = $mod.vP != null;',
  27193. 'if ($mod.vP != null) ;',
  27194. '']));
  27195. end;
  27196. procedure TTestModule.TestProcType_Arg;
  27197. begin
  27198. StartProgram(false);
  27199. Add([
  27200. 'type',
  27201. ' TProcInt = procedure(vI: longint = 1);',
  27202. 'procedure DoIt(vJ: longint); begin end;',
  27203. 'procedure DoSome(vP, vQ: TProcInt);',
  27204. 'var',
  27205. ' b: boolean;',
  27206. 'begin',
  27207. ' vp:=nil;',
  27208. ' vp:=vp;',
  27209. ' vp:=@doit;',
  27210. ' vp;',
  27211. ' vp();',
  27212. ' vp(2);',
  27213. ' b:=vp=nil;',
  27214. ' b:=nil=vp;',
  27215. ' b:=vp=vq;',
  27216. ' b:=vp=@doit;',
  27217. ' b:=@doit=vp;',
  27218. ' b:=vp<>nil;',
  27219. ' b:=nil<>vp;',
  27220. ' b:=vp<>vq;',
  27221. ' b:=vp<>@doit;',
  27222. ' b:=@doit<>vp;',
  27223. ' b:=Assigned(vp);',
  27224. ' if Assigned(vp) then ;',
  27225. 'end;',
  27226. 'begin',
  27227. ' DoSome(@DoIt,nil);']);
  27228. ConvertProgram;
  27229. CheckSource('TestProcType_Arg',
  27230. LinesToStr([ // statements
  27231. 'this.DoIt = function(vJ) {',
  27232. '};',
  27233. 'this.DoSome = function(vP, vQ) {',
  27234. ' var b = false;',
  27235. ' vP = null;',
  27236. ' vP = vP;',
  27237. ' vP = $mod.DoIt;',
  27238. ' vP(1);',
  27239. ' vP(1);',
  27240. ' vP(2);',
  27241. ' b = vP === null;',
  27242. ' b = null === vP;',
  27243. ' b = rtl.eqCallback(vP,vQ);',
  27244. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  27245. ' b = rtl.eqCallback($mod.DoIt, vP);',
  27246. ' b = vP !== null;',
  27247. ' b = null !== vP;',
  27248. ' b = !rtl.eqCallback(vP, vQ);',
  27249. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  27250. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  27251. ' b = vP != null;',
  27252. ' if (vP != null) ;',
  27253. '};',
  27254. '']),
  27255. LinesToStr([ // $mod.$main
  27256. '$mod.DoSome($mod.DoIt,null);',
  27257. '']));
  27258. end;
  27259. procedure TTestModule.TestProcType_FunctionFPC;
  27260. begin
  27261. StartProgram(false);
  27262. Add('type');
  27263. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27264. Add('function DoIt(vI: longint): longint;');
  27265. Add('begin end;');
  27266. Add('var');
  27267. Add(' b: boolean;');
  27268. Add(' vP, vQ: tfuncint;');
  27269. Add('begin');
  27270. Add(' vp:=nil;');
  27271. Add(' vp:=vp;');
  27272. Add(' vp:=@doit;'); // ok in fpc and delphi
  27273. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27274. Add(' vp;'); // ok in fpc and delphi
  27275. Add(' vp();');
  27276. Add(' vp(2);');
  27277. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27278. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27279. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27280. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27281. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27282. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27283. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27284. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27285. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27286. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27287. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27288. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27289. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27290. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27291. Add(' b:=Assigned(vp);');
  27292. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27293. Add(' doit(vp());'); // ok in fpc and delphi
  27294. Add(' doit(vp(2));'); // ok in fpc and delphi
  27295. ConvertProgram;
  27296. CheckSource('TestProcType_FunctionFPC',
  27297. LinesToStr([ // statements
  27298. 'this.DoIt = function(vI) {',
  27299. ' var Result = 0;',
  27300. ' return Result;',
  27301. '};',
  27302. 'this.b = false;',
  27303. 'this.vP = null;',
  27304. 'this.vQ = null;'
  27305. ]),
  27306. LinesToStr([ // $mod.$main
  27307. '$mod.vP = null;',
  27308. '$mod.vP = $mod.vP;',
  27309. '$mod.vP = $mod.DoIt;',
  27310. '$mod.vP(1);',
  27311. '$mod.vP(1);',
  27312. '$mod.vP(2);',
  27313. '$mod.b = $mod.vP === null;',
  27314. '$mod.b = null === $mod.vP;',
  27315. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27316. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27317. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27318. '$mod.b = 4 === $mod.vP(1);',
  27319. '$mod.b = $mod.vP !== null;',
  27320. '$mod.b = null !== $mod.vP;',
  27321. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27322. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27323. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27324. '$mod.b = 6 !== $mod.vP(1);',
  27325. '$mod.b = $mod.vP != null;',
  27326. '$mod.DoIt($mod.vP(1));',
  27327. '$mod.DoIt($mod.vP(2));',
  27328. '']));
  27329. end;
  27330. procedure TTestModule.TestProcType_FunctionDelphi;
  27331. begin
  27332. StartProgram(false);
  27333. Add('{$mode Delphi}');
  27334. Add('type');
  27335. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27336. Add('function DoIt(vI: longint): longint;');
  27337. Add('begin end;');
  27338. Add('var');
  27339. Add(' b: boolean;');
  27340. Add(' vP, vQ: tfuncint;');
  27341. Add('begin');
  27342. Add(' vp:=nil;');
  27343. Add(' vp:=vp;');
  27344. Add(' vp:=@doit;'); // ok in fpc and delphi
  27345. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27346. Add(' vp;'); // ok in fpc and delphi
  27347. Add(' vp();');
  27348. Add(' vp(2);');
  27349. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27350. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27351. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27352. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27353. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27354. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27355. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27356. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27357. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27358. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27359. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27360. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27361. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27362. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27363. Add(' b:=Assigned(vp);');
  27364. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27365. Add(' doit(vp());'); // ok in fpc and delphi
  27366. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27367. ConvertProgram;
  27368. CheckSource('TestProcType_FunctionDelphi',
  27369. LinesToStr([ // statements
  27370. 'this.DoIt = function(vI) {',
  27371. ' var Result = 0;',
  27372. ' return Result;',
  27373. '};',
  27374. 'this.b = false;',
  27375. 'this.vP = null;',
  27376. 'this.vQ = null;'
  27377. ]),
  27378. LinesToStr([ // $mod.$main
  27379. '$mod.vP = null;',
  27380. '$mod.vP = $mod.vP;',
  27381. '$mod.vP = $mod.DoIt;',
  27382. '$mod.vP = $mod.DoIt;',
  27383. '$mod.vP(1);',
  27384. '$mod.vP(1);',
  27385. '$mod.vP(2);',
  27386. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27387. '$mod.b = $mod.vP(1) === 3;',
  27388. '$mod.b = 4 === $mod.vP(1);',
  27389. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27390. '$mod.b = $mod.vP(1) !== 5;',
  27391. '$mod.b = 6 !== $mod.vP(1);',
  27392. '$mod.b = $mod.vP != null;',
  27393. '$mod.DoIt($mod.vP(1));',
  27394. '$mod.DoIt($mod.vP(1));',
  27395. '$mod.DoIt($mod.vP(2));',
  27396. '']));
  27397. end;
  27398. procedure TTestModule.TestProcType_ProcedureDelphi;
  27399. begin
  27400. StartProgram(false);
  27401. Add('{$mode Delphi}');
  27402. Add('type');
  27403. Add(' TProc = procedure;');
  27404. Add('procedure DoIt;');
  27405. Add('begin end;');
  27406. Add('var');
  27407. Add(' b: boolean;');
  27408. Add(' vP, vQ: tproc;');
  27409. Add('begin');
  27410. Add(' vp:=nil;');
  27411. Add(' vp:=vp;');
  27412. Add(' vp:=vq;');
  27413. 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
  27414. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27415. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27416. Add(' vp;'); // ok in fpc and delphi
  27417. Add(' vp();');
  27418. // equal
  27419. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27420. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27421. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27422. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27423. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27424. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27425. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27426. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27427. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27428. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27429. // unequal
  27430. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27431. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27432. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27433. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27434. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27435. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27436. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27437. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27438. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27439. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27440. Add(' b:=Assigned(vp);');
  27441. ConvertProgram;
  27442. CheckSource('TestProcType_ProcedureDelphi',
  27443. LinesToStr([ // statements
  27444. 'this.DoIt = function() {',
  27445. '};',
  27446. 'this.b = false;',
  27447. 'this.vP = null;',
  27448. 'this.vQ = null;'
  27449. ]),
  27450. LinesToStr([ // $mod.$main
  27451. '$mod.vP = null;',
  27452. '$mod.vP = $mod.vP;',
  27453. '$mod.vP = $mod.vQ;',
  27454. '$mod.vP = $mod.DoIt;',
  27455. '$mod.vP = $mod.DoIt;',
  27456. '$mod.vP();',
  27457. '$mod.vP();',
  27458. '$mod.b = $mod.vP === null;',
  27459. '$mod.b = null === $mod.vP;',
  27460. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27461. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27462. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27463. '$mod.b = $mod.vP !== null;',
  27464. '$mod.b = null !== $mod.vP;',
  27465. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27466. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27467. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27468. '$mod.b = $mod.vP != null;',
  27469. '']));
  27470. end;
  27471. procedure TTestModule.TestProcType_AsParam;
  27472. begin
  27473. StartProgram(false);
  27474. Add('type');
  27475. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27476. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27477. Add('var vJ: tfuncint;');
  27478. Add('begin');
  27479. Add(' vg:=vg;');
  27480. Add(' vj:=vh;');
  27481. Add(' vi:=vi;');
  27482. Add(' doit(vg,vg,vg);');
  27483. Add(' doit(vh,vh,vj);');
  27484. Add(' doit(vi,vi,vi);');
  27485. Add(' doit(vj,vj,vj);');
  27486. Add('end;');
  27487. Add('var i: tfuncint;');
  27488. Add('begin');
  27489. Add(' doit(i,i,i);');
  27490. ConvertProgram;
  27491. CheckSource('TestProcType_AsParam',
  27492. LinesToStr([ // statements
  27493. 'this.DoIt = function (vG,vH,vI) {',
  27494. ' var vJ = null;',
  27495. ' vG = vG;',
  27496. ' vJ = vH;',
  27497. ' vI.set(vI.get());',
  27498. ' $mod.DoIt(vG, vG, {',
  27499. ' get: function () {',
  27500. ' return vG;',
  27501. ' },',
  27502. ' set: function (v) {',
  27503. ' vG = v;',
  27504. ' }',
  27505. ' });',
  27506. ' $mod.DoIt(vH, vH, {',
  27507. ' get: function () {',
  27508. ' return vJ;',
  27509. ' },',
  27510. ' set: function (v) {',
  27511. ' vJ = v;',
  27512. ' }',
  27513. ' });',
  27514. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27515. ' $mod.DoIt(vJ, vJ, {',
  27516. ' get: function () {',
  27517. ' return vJ;',
  27518. ' },',
  27519. ' set: function (v) {',
  27520. ' vJ = v;',
  27521. ' }',
  27522. ' });',
  27523. '};',
  27524. 'this.i = null;'
  27525. ]),
  27526. LinesToStr([
  27527. '$mod.DoIt($mod.i,$mod.i,{',
  27528. ' p: $mod,',
  27529. ' get: function () {',
  27530. ' return this.p.i;',
  27531. ' },',
  27532. ' set: function (v) {',
  27533. ' this.p.i = v;',
  27534. ' }',
  27535. '});'
  27536. ]));
  27537. end;
  27538. procedure TTestModule.TestProcType_MethodFPC;
  27539. begin
  27540. StartProgram(false);
  27541. Add('type');
  27542. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27543. Add(' TObject = class');
  27544. Add(' function DoIt(vA: longint = 1): longint;');
  27545. Add(' end;');
  27546. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27547. Add('begin');
  27548. Add('end;');
  27549. Add('var');
  27550. Add(' Obj: TObject;');
  27551. Add(' vP: tfuncint;');
  27552. Add(' b: boolean;');
  27553. Add('begin');
  27554. Add(' vp:[email protected];'); // ok in fpc and delphi
  27555. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27556. Add(' vp;'); // ok in fpc and delphi
  27557. Add(' vp();');
  27558. Add(' vp(2);');
  27559. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27560. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27561. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27562. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27563. ConvertProgram;
  27564. CheckSource('TestProcType_MethodFPC',
  27565. LinesToStr([ // statements
  27566. 'rtl.createClass(this, "TObject", null, function () {',
  27567. ' this.$init = function () {',
  27568. ' };',
  27569. ' this.$final = function () {',
  27570. ' };',
  27571. ' this.DoIt = function (vA) {',
  27572. ' var Result = 0;',
  27573. ' return Result;',
  27574. ' };',
  27575. '});',
  27576. 'this.Obj = null;',
  27577. 'this.vP = null;',
  27578. 'this.b = false;'
  27579. ]),
  27580. LinesToStr([
  27581. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27582. '$mod.vP(1);',
  27583. '$mod.vP(1);',
  27584. '$mod.vP(2);',
  27585. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27586. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27587. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27588. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27589. '']));
  27590. end;
  27591. procedure TTestModule.TestProcType_MethodDelphi;
  27592. begin
  27593. StartProgram(false);
  27594. Add([
  27595. '{$mode delphi}',
  27596. 'type',
  27597. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27598. ' TObject = class',
  27599. ' function DoIt(vA: longint = 1): longint;',
  27600. ' end;',
  27601. 'function TObject.DoIt(vA: longint = 1): longint;',
  27602. 'begin',
  27603. 'end;',
  27604. 'var',
  27605. ' Obj: TObject;',
  27606. ' vP: tfuncint;',
  27607. ' b: boolean;',
  27608. 'begin',
  27609. ' vp:[email protected];', // ok in fpc and delphi
  27610. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27611. ' vp;', // ok in fpc and delphi
  27612. ' vp();',
  27613. ' vp(2);',
  27614. //' b:[email protected];', // ok in fpc, illegal in delphi
  27615. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27616. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27617. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27618. '']);
  27619. ConvertProgram;
  27620. CheckSource('TestProcType_MethodDelphi',
  27621. LinesToStr([ // statements
  27622. 'rtl.createClass(this, "TObject", null, function () {',
  27623. ' this.$init = function () {',
  27624. ' };',
  27625. ' this.$final = function () {',
  27626. ' };',
  27627. ' this.DoIt = function (vA) {',
  27628. ' var Result = 0;',
  27629. ' return Result;',
  27630. ' };',
  27631. '});',
  27632. 'this.Obj = null;',
  27633. 'this.vP = null;',
  27634. 'this.b = false;'
  27635. ]),
  27636. LinesToStr([
  27637. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27638. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27639. '$mod.vP(1);',
  27640. '$mod.vP(1);',
  27641. '$mod.vP(2);',
  27642. '']));
  27643. end;
  27644. procedure TTestModule.TestProcType_PropertyFPC;
  27645. begin
  27646. StartProgram(false);
  27647. Add('type');
  27648. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27649. Add(' TObject = class');
  27650. Add(' FOnFoo: TFuncInt;');
  27651. Add(' function DoIt(vA: longint = 1): longint;');
  27652. Add(' function GetFoo: TFuncInt;');
  27653. Add(' procedure SetFoo(const Value: TFuncInt);');
  27654. Add(' function GetEvents(Index: longint): TFuncInt;');
  27655. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27656. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27657. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27658. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27659. Add(' end;');
  27660. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27661. Add('function tobject.getfoo: tfuncint; begin end;');
  27662. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27663. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27664. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27665. Add('var');
  27666. Add(' Obj: TObject;');
  27667. Add(' vP: tfuncint;');
  27668. Add(' b: boolean;');
  27669. Add('begin');
  27670. Add(' obj.onfoo:=nil;');
  27671. Add(' obj.onbar:=nil;');
  27672. Add(' obj.events[1]:=nil;');
  27673. Add(' obj.onfoo:=obj.onfoo;');
  27674. Add(' obj.onbar:=obj.onbar;');
  27675. Add(' obj.events[2]:=obj.events[3];');
  27676. Add(' obj.onfoo:[email protected];');
  27677. Add(' obj.onbar:[email protected];');
  27678. Add(' obj.events[4]:[email protected];');
  27679. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27680. //Add(' obj.onbar:=obj.doit;'); // delphi
  27681. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27682. Add(' obj.onfoo;');
  27683. Add(' obj.onbar;');
  27684. //Add(' obj.events[5];'); ToDo in pasresolver
  27685. Add(' obj.onfoo();');
  27686. Add(' obj.onbar();');
  27687. Add(' obj.events[6]();');
  27688. Add(' b:=obj.onfoo=nil;');
  27689. Add(' b:=obj.onbar=nil;');
  27690. Add(' b:=obj.events[7]=nil;');
  27691. Add(' b:=obj.onfoo<>nil;');
  27692. Add(' b:=obj.onbar<>nil;');
  27693. Add(' b:=obj.events[8]<>nil;');
  27694. Add(' b:=obj.onfoo=vp;');
  27695. Add(' b:=obj.onbar=vp;');
  27696. Add(' b:=obj.events[9]=vp;');
  27697. Add(' b:=obj.onfoo=obj.onfoo;');
  27698. Add(' b:=obj.onbar=obj.onfoo;');
  27699. Add(' b:=obj.events[10]=obj.onfoo;');
  27700. Add(' b:=obj.onfoo<>obj.onfoo;');
  27701. Add(' b:=obj.onbar<>obj.onfoo;');
  27702. Add(' b:=obj.events[11]<>obj.onfoo;');
  27703. Add(' b:[email protected];');
  27704. Add(' b:[email protected];');
  27705. Add(' b:=obj.events[12][email protected];');
  27706. Add(' b:=obj.onfoo<>@obj.doit;');
  27707. Add(' b:=obj.onbar<>@obj.doit;');
  27708. Add(' b:=obj.events[12]<>@obj.doit;');
  27709. Add(' b:=Assigned(obj.onfoo);');
  27710. Add(' b:=Assigned(obj.onbar);');
  27711. Add(' b:=Assigned(obj.events[13]);');
  27712. ConvertProgram;
  27713. CheckSource('TestProcType_PropertyFPC',
  27714. LinesToStr([ // statements
  27715. 'rtl.createClass(this, "TObject", null, function () {',
  27716. ' this.$init = function () {',
  27717. ' this.FOnFoo = null;',
  27718. ' };',
  27719. ' this.$final = function () {',
  27720. ' this.FOnFoo = undefined;',
  27721. ' };',
  27722. ' this.DoIt = function (vA) {',
  27723. ' var Result = 0;',
  27724. ' return Result;',
  27725. ' };',
  27726. 'this.GetFoo = function () {',
  27727. ' var Result = null;',
  27728. ' return Result;',
  27729. '};',
  27730. 'this.SetFoo = function (Value) {',
  27731. '};',
  27732. 'this.GetEvents = function (Index) {',
  27733. ' var Result = null;',
  27734. ' return Result;',
  27735. '};',
  27736. 'this.SetEvents = function (Index, Value) {',
  27737. '};',
  27738. '});',
  27739. 'this.Obj = null;',
  27740. 'this.vP = null;',
  27741. 'this.b = false;'
  27742. ]),
  27743. LinesToStr([
  27744. '$mod.Obj.FOnFoo = null;',
  27745. '$mod.Obj.SetFoo(null);',
  27746. '$mod.Obj.SetEvents(1, null);',
  27747. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27748. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27749. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27750. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27751. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27752. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27753. '$mod.Obj.FOnFoo(1);',
  27754. '$mod.Obj.GetFoo();',
  27755. '$mod.Obj.FOnFoo(1);',
  27756. '$mod.Obj.GetFoo()(1);',
  27757. '$mod.Obj.GetEvents(6)(1);',
  27758. '$mod.b = $mod.Obj.FOnFoo === null;',
  27759. '$mod.b = $mod.Obj.GetFoo() === null;',
  27760. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27761. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27762. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27763. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27764. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27765. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27766. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27767. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27768. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27769. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27770. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27771. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27772. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27773. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27774. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27775. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27776. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27777. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27778. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27779. '$mod.b = $mod.Obj.FOnFoo != null;',
  27780. '$mod.b = $mod.Obj.GetFoo() != null;',
  27781. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27782. '']));
  27783. end;
  27784. procedure TTestModule.TestProcType_PropertyDelphi;
  27785. begin
  27786. StartProgram(false);
  27787. Add('{$mode delphi}');
  27788. Add('type');
  27789. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27790. Add(' TObject = class');
  27791. Add(' FOnFoo: TFuncInt;');
  27792. Add(' function DoIt(vA: longint = 1): longint;');
  27793. Add(' function GetFoo: TFuncInt;');
  27794. Add(' procedure SetFoo(const Value: TFuncInt);');
  27795. Add(' function GetEvents(Index: longint): TFuncInt;');
  27796. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27797. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27798. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27799. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27800. Add(' end;');
  27801. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27802. Add('function tobject.getfoo: tfuncint; begin end;');
  27803. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27804. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27805. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27806. Add('var');
  27807. Add(' Obj: TObject;');
  27808. Add(' vP: tfuncint;');
  27809. Add(' b: boolean;');
  27810. Add('begin');
  27811. Add(' obj.onfoo:=nil;');
  27812. Add(' obj.onbar:=nil;');
  27813. Add(' obj.events[1]:=nil;');
  27814. Add(' obj.onfoo:=obj.onfoo;');
  27815. Add(' obj.onbar:=obj.onbar;');
  27816. Add(' obj.events[2]:=obj.events[3];');
  27817. Add(' obj.onfoo:[email protected];');
  27818. Add(' obj.onbar:[email protected];');
  27819. Add(' obj.events[4]:[email protected];');
  27820. Add(' obj.onfoo:=obj.doit;'); // delphi
  27821. Add(' obj.onbar:=obj.doit;'); // delphi
  27822. Add(' obj.events[4]:=obj.doit;'); // delphi
  27823. Add(' obj.onfoo;');
  27824. Add(' obj.onbar;');
  27825. //Add(' obj.events[5];'); ToDo in pasresolver
  27826. Add(' obj.onfoo();');
  27827. Add(' obj.onbar();');
  27828. Add(' obj.events[6]();');
  27829. //Add(' b:=obj.onfoo=nil;'); // fpc
  27830. //Add(' b:=obj.onbar=nil;'); // fpc
  27831. //Add(' b:=obj.events[7]=nil;'); // fpc
  27832. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27833. //Add(' b:=obj.onbar<>nil;'); // fpc
  27834. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27835. Add(' b:=obj.onfoo=vp;');
  27836. Add(' b:=obj.onbar=vp;');
  27837. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27838. Add(' b:=obj.onfoo=obj.onfoo;');
  27839. Add(' b:=obj.onbar=obj.onfoo;');
  27840. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27841. Add(' b:=obj.onfoo<>obj.onfoo;');
  27842. Add(' b:=obj.onbar<>obj.onfoo;');
  27843. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27844. //Add(' b:[email protected];'); // fpc
  27845. //Add(' b:[email protected];'); // fpc
  27846. //Add(' b:=obj.events[12][email protected];'); // fpc
  27847. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27848. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27849. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27850. Add(' b:=Assigned(obj.onfoo);');
  27851. Add(' b:=Assigned(obj.onbar);');
  27852. Add(' b:=Assigned(obj.events[13]);');
  27853. ConvertProgram;
  27854. CheckSource('TestProcType_PropertyDelphi',
  27855. LinesToStr([ // statements
  27856. 'rtl.createClass(this, "TObject", null, function () {',
  27857. ' this.$init = function () {',
  27858. ' this.FOnFoo = null;',
  27859. ' };',
  27860. ' this.$final = function () {',
  27861. ' this.FOnFoo = undefined;',
  27862. ' };',
  27863. ' this.DoIt = function (vA) {',
  27864. ' var Result = 0;',
  27865. ' return Result;',
  27866. ' };',
  27867. 'this.GetFoo = function () {',
  27868. ' var Result = null;',
  27869. ' return Result;',
  27870. '};',
  27871. 'this.SetFoo = function (Value) {',
  27872. '};',
  27873. 'this.GetEvents = function (Index) {',
  27874. ' var Result = null;',
  27875. ' return Result;',
  27876. '};',
  27877. 'this.SetEvents = function (Index, Value) {',
  27878. '};',
  27879. '});',
  27880. 'this.Obj = null;',
  27881. 'this.vP = null;',
  27882. 'this.b = false;'
  27883. ]),
  27884. LinesToStr([
  27885. '$mod.Obj.FOnFoo = null;',
  27886. '$mod.Obj.SetFoo(null);',
  27887. '$mod.Obj.SetEvents(1, null);',
  27888. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27889. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27890. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27891. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27892. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27893. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27894. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27895. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27896. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27897. '$mod.Obj.FOnFoo(1);',
  27898. '$mod.Obj.GetFoo();',
  27899. '$mod.Obj.FOnFoo(1);',
  27900. '$mod.Obj.GetFoo()(1);',
  27901. '$mod.Obj.GetEvents(6)(1);',
  27902. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27903. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27904. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27905. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27906. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27907. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27908. '$mod.b = $mod.Obj.FOnFoo != null;',
  27909. '$mod.b = $mod.Obj.GetFoo() != null;',
  27910. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27911. '']));
  27912. end;
  27913. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27914. begin
  27915. StartProgram(false);
  27916. Add('type');
  27917. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27918. Add(' TObject = class');
  27919. Add(' FOnFoo: TFuncInt;');
  27920. Add(' function DoIt(vA: longint = 1): longint;');
  27921. Add(' function GetFoo: TFuncInt;');
  27922. Add(' procedure SetFoo(const Value: TFuncInt);');
  27923. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27924. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27925. Add(' end;');
  27926. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27927. Add('function tobject.getfoo: tfuncint; begin end;');
  27928. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27929. Add('var');
  27930. Add(' Obj: TObject;');
  27931. Add(' vP: tfuncint;');
  27932. Add(' b: boolean;');
  27933. Add('begin');
  27934. Add('with obj do begin');
  27935. Add(' fonfoo:=nil;');
  27936. Add(' onfoo:=nil;');
  27937. Add(' onbar:=nil;');
  27938. Add(' fonfoo:=fonfoo;');
  27939. Add(' onfoo:=onfoo;');
  27940. Add(' onbar:=onbar;');
  27941. Add(' fonfoo:=@doit;');
  27942. Add(' onfoo:=@doit;');
  27943. Add(' onbar:=@doit;');
  27944. //Add(' fonfoo:=doit;'); // delphi
  27945. //Add(' onfoo:=doit;'); // delphi
  27946. //Add(' onbar:=doit;'); // delphi
  27947. Add(' fonfoo;');
  27948. Add(' onfoo;');
  27949. Add(' onbar;');
  27950. Add(' fonfoo();');
  27951. Add(' onfoo();');
  27952. Add(' onbar();');
  27953. Add(' b:=fonfoo=nil;');
  27954. Add(' b:=onfoo=nil;');
  27955. Add(' b:=onbar=nil;');
  27956. Add(' b:=fonfoo<>nil;');
  27957. Add(' b:=onfoo<>nil;');
  27958. Add(' b:=onbar<>nil;');
  27959. Add(' b:=fonfoo=vp;');
  27960. Add(' b:=onfoo=vp;');
  27961. Add(' b:=onbar=vp;');
  27962. Add(' b:=fonfoo=fonfoo;');
  27963. Add(' b:=onfoo=onfoo;');
  27964. Add(' b:=onbar=onfoo;');
  27965. Add(' b:=fonfoo<>fonfoo;');
  27966. Add(' b:=onfoo<>onfoo;');
  27967. Add(' b:=onbar<>onfoo;');
  27968. Add(' b:=fonfoo=@doit;');
  27969. Add(' b:=onfoo=@doit;');
  27970. Add(' b:=onbar=@doit;');
  27971. Add(' b:=fonfoo<>@doit;');
  27972. Add(' b:=onfoo<>@doit;');
  27973. Add(' b:=onbar<>@doit;');
  27974. Add(' b:=Assigned(fonfoo);');
  27975. Add(' b:=Assigned(onfoo);');
  27976. Add(' b:=Assigned(onbar);');
  27977. Add('end;');
  27978. ConvertProgram;
  27979. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  27980. LinesToStr([ // statements
  27981. 'rtl.createClass(this, "TObject", null, function () {',
  27982. ' this.$init = function () {',
  27983. ' this.FOnFoo = null;',
  27984. ' };',
  27985. ' this.$final = function () {',
  27986. ' this.FOnFoo = undefined;',
  27987. ' };',
  27988. ' this.DoIt = function (vA) {',
  27989. ' var Result = 0;',
  27990. ' return Result;',
  27991. ' };',
  27992. ' this.GetFoo = function () {',
  27993. ' var Result = null;',
  27994. ' return Result;',
  27995. ' };',
  27996. ' this.SetFoo = function (Value) {',
  27997. ' };',
  27998. '});',
  27999. 'this.Obj = null;',
  28000. 'this.vP = null;',
  28001. 'this.b = false;'
  28002. ]),
  28003. LinesToStr([
  28004. 'var $with = $mod.Obj;',
  28005. '$with.FOnFoo = null;',
  28006. '$with.FOnFoo = null;',
  28007. '$with.SetFoo(null);',
  28008. '$with.FOnFoo = $with.FOnFoo;',
  28009. '$with.FOnFoo = $with.FOnFoo;',
  28010. '$with.SetFoo($with.GetFoo());',
  28011. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28012. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28013. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28014. '$with.FOnFoo(1);',
  28015. '$with.FOnFoo(1);',
  28016. '$with.GetFoo();',
  28017. '$with.FOnFoo(1);',
  28018. '$with.FOnFoo(1);',
  28019. '$with.GetFoo()(1);',
  28020. '$mod.b = $with.FOnFoo === null;',
  28021. '$mod.b = $with.FOnFoo === null;',
  28022. '$mod.b = $with.GetFoo() === null;',
  28023. '$mod.b = $with.FOnFoo !== null;',
  28024. '$mod.b = $with.FOnFoo !== null;',
  28025. '$mod.b = $with.GetFoo() !== null;',
  28026. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28027. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28028. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28029. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28030. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28031. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28032. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28033. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28034. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28035. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28036. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28037. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28038. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28039. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28040. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28041. '$mod.b = $with.FOnFoo != null;',
  28042. '$mod.b = $with.FOnFoo != null;',
  28043. '$mod.b = $with.GetFoo() != null;',
  28044. '']));
  28045. end;
  28046. procedure TTestModule.TestProcType_Nested;
  28047. begin
  28048. StartProgram(false);
  28049. Add([
  28050. 'type',
  28051. ' TProcInt = procedure(vI: longint = 1);',
  28052. 'procedure DoIt(vJ: longint);',
  28053. 'var aProc: TProcInt;',
  28054. ' b: boolean;',
  28055. ' procedure Sub(vK: longint);',
  28056. ' var aSub: TProcInt;',
  28057. ' procedure SubSub(vK: longint);',
  28058. ' var aSubSub: TProcInt;',
  28059. ' begin;',
  28060. ' aProc:=@DoIt;',
  28061. ' aSub:=@DoIt;',
  28062. ' aSubSub:=@DoIt;',
  28063. ' aProc:=@Sub;',
  28064. ' aSub:=@Sub;',
  28065. ' aSubSub:=@Sub;',
  28066. ' aProc:=@SubSub;',
  28067. ' aSub:=@SubSub;',
  28068. ' aSubSub:=@SubSub;',
  28069. ' end;',
  28070. ' begin;',
  28071. ' end;',
  28072. 'begin;',
  28073. ' aProc:=@Sub;',
  28074. ' b:=aProc=@Sub;',
  28075. ' b:=@Sub=aProc;',
  28076. 'end;',
  28077. 'begin',
  28078. '']);
  28079. ConvertProgram;
  28080. CheckSource('TestProcType_Nested',
  28081. LinesToStr([ // statements
  28082. 'this.DoIt = function (vJ) {',
  28083. ' var aProc = null;',
  28084. ' var b = false;',
  28085. ' function Sub(vK) {',
  28086. ' var aSub = null;',
  28087. ' function SubSub(vK) {',
  28088. ' var aSubSub = null;',
  28089. ' aProc = $mod.DoIt;',
  28090. ' aSub = $mod.DoIt;',
  28091. ' aSubSub = $mod.DoIt;',
  28092. ' aProc = Sub;',
  28093. ' aSub = Sub;',
  28094. ' aSubSub = Sub;',
  28095. ' aProc = SubSub;',
  28096. ' aSub = SubSub;',
  28097. ' aSubSub = SubSub;',
  28098. ' };',
  28099. ' };',
  28100. ' aProc = Sub;',
  28101. ' b = rtl.eqCallback(aProc, Sub);',
  28102. ' b = rtl.eqCallback(Sub, aProc);',
  28103. '};',
  28104. '']),
  28105. LinesToStr([ // $mod.$main
  28106. '']));
  28107. end;
  28108. procedure TTestModule.TestProcType_NestedOfObject;
  28109. begin
  28110. StartProgram(false);
  28111. Add([
  28112. 'type',
  28113. ' TProcInt = procedure(vI: longint = 1) of object;',
  28114. ' TObject = class',
  28115. ' procedure DoIt(vJ: longint);',
  28116. ' end;',
  28117. 'procedure TObject.DoIt(vJ: longint);',
  28118. 'var aProc: TProcInt;',
  28119. ' b: boolean;',
  28120. ' procedure Sub(vK: longint);',
  28121. ' var aSub: TProcInt;',
  28122. ' procedure SubSub(vK: longint);',
  28123. ' var aSubSub: TProcInt;',
  28124. ' begin;',
  28125. ' aProc:=@DoIt;',
  28126. ' aSub:=@DoIt;',
  28127. ' aSubSub:=@DoIt;',
  28128. ' aProc:=@Sub;',
  28129. ' aSub:=@Sub;',
  28130. ' aSubSub:=@Sub;',
  28131. ' aProc:=@SubSub;',
  28132. ' aSub:=@SubSub;',
  28133. ' aSubSub:=@SubSub;',
  28134. ' end;',
  28135. ' begin;',
  28136. ' end;',
  28137. 'begin;',
  28138. ' aProc:=@Sub;',
  28139. ' b:=aProc=@Sub;',
  28140. ' b:=@Sub=aProc;',
  28141. 'end;',
  28142. 'begin',
  28143. '']);
  28144. ConvertProgram;
  28145. CheckSource('TestProcType_Nested',
  28146. LinesToStr([ // statements
  28147. 'rtl.createClass(this, "TObject", null, function () {',
  28148. ' this.$init = function () {',
  28149. ' };',
  28150. ' this.$final = function () {',
  28151. ' };',
  28152. ' this.DoIt = function (vJ) {',
  28153. ' var $Self = this;',
  28154. ' var aProc = null;',
  28155. ' var b = false;',
  28156. ' function Sub(vK) {',
  28157. ' var aSub = null;',
  28158. ' function SubSub(vK) {',
  28159. ' var aSubSub = null;',
  28160. ' aProc = rtl.createCallback($Self, "DoIt");',
  28161. ' aSub = rtl.createCallback($Self, "DoIt");',
  28162. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28163. ' aProc = Sub;',
  28164. ' aSub = Sub;',
  28165. ' aSubSub = Sub;',
  28166. ' aProc = SubSub;',
  28167. ' aSub = SubSub;',
  28168. ' aSubSub = SubSub;',
  28169. ' };',
  28170. ' };',
  28171. ' aProc = Sub;',
  28172. ' b = rtl.eqCallback(aProc, Sub);',
  28173. ' b = rtl.eqCallback(Sub, aProc);',
  28174. ' };',
  28175. '});',
  28176. '']),
  28177. LinesToStr([ // $mod.$main
  28178. '']));
  28179. end;
  28180. procedure TTestModule.TestProcType_ReferenceToProc;
  28181. begin
  28182. StartProgram(false);
  28183. Add([
  28184. 'type',
  28185. ' TProcRef = reference to procedure(i: longint = 0);',
  28186. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28187. 'var',
  28188. ' p: TProcRef;',
  28189. ' f: TFuncRef;',
  28190. 'procedure DoIt(i: longint);',
  28191. 'begin',
  28192. 'end;',
  28193. 'function GetIt(i: longint): longint;',
  28194. 'begin',
  28195. ' p:=@DoIt;',
  28196. ' f:=@GetIt;',
  28197. ' f;',
  28198. ' f();',
  28199. ' f(1);',
  28200. 'end;',
  28201. 'begin',
  28202. ' p:=@DoIt;',
  28203. ' f:=@GetIt;',
  28204. ' f;',
  28205. ' f();',
  28206. ' f(1);',
  28207. ' p:=TProcRef(f);',
  28208. '']);
  28209. ConvertProgram;
  28210. CheckSource('TestProcType_ReferenceToProc',
  28211. LinesToStr([ // statements
  28212. 'this.p = null;',
  28213. 'this.f = null;',
  28214. 'this.DoIt = function (i) {',
  28215. '};',
  28216. 'this.GetIt = function (i) {',
  28217. ' var Result = 0;',
  28218. ' $mod.p = $mod.DoIt;',
  28219. ' $mod.f = $mod.GetIt;',
  28220. ' $mod.f(0);',
  28221. ' $mod.f(0);',
  28222. ' $mod.f(1);',
  28223. ' return Result;',
  28224. '};',
  28225. '']),
  28226. LinesToStr([ // $mod.$main
  28227. '$mod.p = $mod.DoIt;',
  28228. '$mod.f = $mod.GetIt;',
  28229. '$mod.f(0);',
  28230. '$mod.f(0);',
  28231. '$mod.f(1);',
  28232. '$mod.p = $mod.f;',
  28233. '']));
  28234. end;
  28235. procedure TTestModule.TestProcType_ReferenceToMethod;
  28236. begin
  28237. StartProgram(false);
  28238. Add([
  28239. 'type',
  28240. ' TFuncRef = reference to function(i: longint = 5): longint;',
  28241. ' TObject = class',
  28242. ' function Grow(s: longint): longint;',
  28243. ' end;',
  28244. 'var',
  28245. ' f: tfuncref;',
  28246. 'function tobject.grow(s: longint): longint;',
  28247. ' function GrowSub(i: longint): longint;',
  28248. ' begin',
  28249. ' f:=@grow;',
  28250. ' f:=@growsub;',
  28251. ' end;',
  28252. 'begin',
  28253. ' f:=@grow;',
  28254. ' f:=@growsub;',
  28255. 'end;',
  28256. 'begin',
  28257. '']);
  28258. ConvertProgram;
  28259. CheckSource('TestProcType_ReferenceToMethod',
  28260. LinesToStr([ // statements
  28261. 'rtl.createClass(this, "TObject", null, function () {',
  28262. ' this.$init = function () {',
  28263. ' };',
  28264. ' this.$final = function () {',
  28265. ' };',
  28266. ' this.Grow = function (s) {',
  28267. ' var $Self = this;',
  28268. ' var Result = 0;',
  28269. ' function GrowSub(i) {',
  28270. ' var Result = 0;',
  28271. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28272. ' $mod.f = GrowSub;',
  28273. ' return Result;',
  28274. ' };',
  28275. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28276. ' $mod.f = GrowSub;',
  28277. ' return Result;',
  28278. ' };',
  28279. '});',
  28280. 'this.f = null;',
  28281. '']),
  28282. LinesToStr([ // $mod.$main
  28283. '']));
  28284. end;
  28285. procedure TTestModule.TestProcType_Typecast;
  28286. begin
  28287. StartProgram(false);
  28288. Add([
  28289. 'type',
  28290. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  28291. ' TEvent = procedure of object;',
  28292. ' TGetter = function:longint of object;',
  28293. ' TProcA = procedure(i: longint);',
  28294. ' TFuncB = function(i, j: longint): longint;',
  28295. 'procedure DoIt(); varargs; begin end;',
  28296. 'var',
  28297. ' Notify: tnotifyevent;',
  28298. ' Event: tevent;',
  28299. ' Getter: tgetter;',
  28300. ' ProcA: tproca;',
  28301. ' FuncB: tfuncb;',
  28302. ' p: pointer;',
  28303. 'begin',
  28304. ' notify:=tnotifyevent(event);',
  28305. ' event:=tevent(event);',
  28306. ' event:=tevent(notify);',
  28307. ' event:=tevent(getter);',
  28308. ' event:=tevent(proca);',
  28309. ' proca:=tproca(funcb);',
  28310. ' funcb:=tfuncb(funcb);',
  28311. ' funcb:=tfuncb(proca);',
  28312. ' funcb:=tfuncb(getter);',
  28313. ' proca:=tproca(p);',
  28314. ' funcb:=tfuncb(p);',
  28315. ' getter:=tgetter(p);',
  28316. ' p:=pointer(notify);',
  28317. ' p:=notify;',
  28318. ' p:=pointer(proca);',
  28319. ' p:=proca;',
  28320. ' p:=pointer(funcb);',
  28321. ' p:=funcb;',
  28322. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  28323. '']);
  28324. ConvertProgram;
  28325. CheckSource('TestProcType_Typecast',
  28326. LinesToStr([ // statements
  28327. 'this.DoIt = function () {',
  28328. '};',
  28329. 'this.Notify = null;',
  28330. 'this.Event = null;',
  28331. 'this.Getter = null;',
  28332. 'this.ProcA = null;',
  28333. 'this.FuncB = null;',
  28334. 'this.p = null;',
  28335. '']),
  28336. LinesToStr([ // $mod.$main
  28337. '$mod.Notify = $mod.Event;',
  28338. '$mod.Event = $mod.Event;',
  28339. '$mod.Event = $mod.Notify;',
  28340. '$mod.Event = $mod.Getter;',
  28341. '$mod.Event = $mod.ProcA;',
  28342. '$mod.ProcA = $mod.FuncB;',
  28343. '$mod.FuncB = $mod.FuncB;',
  28344. '$mod.FuncB = $mod.ProcA;',
  28345. '$mod.FuncB = $mod.Getter;',
  28346. '$mod.ProcA = $mod.p;',
  28347. '$mod.FuncB = $mod.p;',
  28348. '$mod.Getter = $mod.p;',
  28349. '$mod.p = $mod.Notify;',
  28350. '$mod.p = $mod.Notify;',
  28351. '$mod.p = $mod.ProcA;',
  28352. '$mod.p = $mod.ProcA;',
  28353. '$mod.p = $mod.FuncB;',
  28354. '$mod.p = $mod.FuncB;',
  28355. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  28356. '']));
  28357. end;
  28358. procedure TTestModule.TestProcType_PassProcToUntyped;
  28359. begin
  28360. StartProgram(false);
  28361. Add([
  28362. 'type',
  28363. ' TEvent = procedure of object;',
  28364. ' TFunc = function: longint;',
  28365. 'procedure DoIt(); varargs; begin end;',
  28366. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28367. 'var',
  28368. ' Event: tevent;',
  28369. ' Func: TFunc;',
  28370. 'begin',
  28371. ' doit(event,func);',
  28372. ' dosome(event,event,event);',
  28373. ' dosome(func,func,func);',
  28374. '']);
  28375. ConvertProgram;
  28376. CheckSource('TestProcType_PassProcToUntyped',
  28377. LinesToStr([ // statements
  28378. 'this.DoIt = function () {',
  28379. '};',
  28380. 'this.DoSome = function (a, b, p) {',
  28381. '};',
  28382. 'this.Event = null;',
  28383. 'this.Func = null;',
  28384. '']),
  28385. LinesToStr([ // $mod.$main
  28386. '$mod.DoIt($mod.Event, $mod.Func);',
  28387. '$mod.DoSome($mod.Event, {',
  28388. ' p: $mod,',
  28389. ' get: function () {',
  28390. ' return this.p.Event;',
  28391. ' },',
  28392. ' set: function (v) {',
  28393. ' this.p.Event = v;',
  28394. ' }',
  28395. '}, $mod.Event);',
  28396. '$mod.DoSome($mod.Func, {',
  28397. ' p: $mod,',
  28398. ' get: function () {',
  28399. ' return this.p.Func;',
  28400. ' },',
  28401. ' set: function (v) {',
  28402. ' this.p.Func = v;',
  28403. ' }',
  28404. '}, $mod.Func);',
  28405. '']));
  28406. end;
  28407. procedure TTestModule.TestProcType_PassProcToArray;
  28408. begin
  28409. StartProgram(false);
  28410. Add([
  28411. 'type',
  28412. ' TFunc = function: longint;',
  28413. ' TArrFunc = array of TFunc;',
  28414. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28415. 'function GetIt: longint; begin end;',
  28416. 'var',
  28417. ' Func: tfunc;',
  28418. 'begin',
  28419. ' doit([]);',
  28420. ' doit([@GetIt]);',
  28421. ' doit([Func]);',
  28422. '']);
  28423. ConvertProgram;
  28424. CheckSource('TestProcType_PassProcToArray',
  28425. LinesToStr([ // statements
  28426. 'this.DoIt = function (Arr) {',
  28427. '};',
  28428. 'this.GetIt = function () {',
  28429. ' var Result = 0;',
  28430. ' return Result;',
  28431. '};',
  28432. 'this.Func = null;',
  28433. '']),
  28434. LinesToStr([ // $mod.$main
  28435. '$mod.DoIt([]);',
  28436. '$mod.DoIt([$mod.GetIt]);',
  28437. '$mod.DoIt([$mod.Func]);',
  28438. '']));
  28439. end;
  28440. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28441. begin
  28442. StartProgram(false);
  28443. Add([
  28444. '{$modeswitch externalclass}',
  28445. 'type',
  28446. ' TProc = reference to procedure(i: longint); safecall;',
  28447. ' TEvent = procedure(i: longint) of object; safecall;',
  28448. ' TExtA = class external name ''ExtObj''',
  28449. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28450. ' procedure DoSome(Id: longint = 1);',
  28451. ' procedure SetOnClick(const e: TEvent);',
  28452. ' property OnClick: TEvent write SetOnClick;',
  28453. ' class procedure Fly(Id: longint = 1); static;',
  28454. ' procedure SetOnShow(const p: TProc);',
  28455. ' property OnShow: TProc write SetOnShow;',
  28456. ' end;',
  28457. 'procedure Run(i: longint = 1);',
  28458. 'begin',
  28459. 'end;',
  28460. 'var',
  28461. ' Obj: texta;',
  28462. ' e: TEvent;',
  28463. ' p: TProc;',
  28464. 'begin',
  28465. ' e:=e;',
  28466. ' e:[email protected];',
  28467. ' e:[email protected];',
  28468. ' e:=TEvent(@obj.dosome);', // no safecall
  28469. ' obj.OnClick:[email protected];',
  28470. ' obj.OnClick:[email protected];',
  28471. ' obj.setonclick(@obj.doit);',
  28472. ' obj.setonclick(@obj.dosome);',
  28473. ' p:=@Run;',
  28474. ' p:[email protected];',
  28475. ' obj.OnShow:=@Run;',
  28476. ' obj.OnShow:[email protected];',
  28477. ' obj.setOnShow(@Run);',
  28478. ' obj.setOnShow(@TExtA.Fly);',
  28479. ' with obj do begin',
  28480. ' e:=@doit;',
  28481. ' e:=@dosome;',
  28482. ' OnClick:=@doit;',
  28483. ' OnClick:=@dosome;',
  28484. ' setonclick(@doit);',
  28485. ' setonclick(@dosome);',
  28486. ' OnShow:=@Run;',
  28487. ' setOnShow(@Run);',
  28488. ' end;']);
  28489. ConvertProgram;
  28490. CheckSource('TestProcType_SafeCallObjFPC',
  28491. LinesToStr([ // statements
  28492. 'this.Run = function (i) {',
  28493. '};',
  28494. 'this.Obj = null;',
  28495. 'this.e = null;',
  28496. 'this.p = null;',
  28497. '']),
  28498. LinesToStr([ // $mod.$main
  28499. '$mod.e = $mod.e;',
  28500. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28501. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28502. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28503. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28504. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28505. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28506. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28507. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28508. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28509. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28510. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28511. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28512. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28513. 'var $with = $mod.Obj;',
  28514. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28515. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28516. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28517. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28518. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28519. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28520. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28521. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28522. '']));
  28523. end;
  28524. procedure TTestModule.TestProcType_SafeCallDelphi;
  28525. begin
  28526. StartProgram(false);
  28527. Add([
  28528. '{$mode delphi}',
  28529. '{$modeswitch externalclass}',
  28530. 'type',
  28531. ' TProc = reference to procedure(i: longint); safecall;',
  28532. ' TEvent = procedure(i: longint) of object; safecall;',
  28533. ' TExtA = class external name ''ExtObj''',
  28534. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28535. ' procedure DoSome(Id: longint = 1);',
  28536. ' procedure SetOnClick(const e: TEvent);',
  28537. ' property OnClick: TEvent write SetOnClick;',
  28538. ' class procedure Fly(Id: longint = 1); static;',
  28539. ' procedure SetOnShow(const p: TProc);',
  28540. ' property OnShow: TProc write SetOnShow;',
  28541. ' end;',
  28542. 'procedure Run(i: longint = 1);',
  28543. 'begin',
  28544. 'end;',
  28545. 'var',
  28546. ' Obj: texta;',
  28547. ' e: TEvent;',
  28548. ' p: TProc;',
  28549. 'begin',
  28550. ' e:=e;',
  28551. ' e:=obj.doit;',
  28552. ' e:=obj.dosome;',
  28553. ' e:=TEvent(@obj.dosome);', // no safecall
  28554. ' obj.OnClick:=obj.doit;',
  28555. ' obj.OnClick:=obj.dosome;',
  28556. ' obj.setonclick(obj.doit);',
  28557. ' obj.setonclick(obj.dosome);',
  28558. ' p:=Run;',
  28559. ' p:=TExtA.Fly;',
  28560. ' obj.OnShow:=Run;',
  28561. ' obj.OnShow:=TExtA.Fly;',
  28562. ' obj.setOnShow(Run);',
  28563. ' obj.setOnShow(TExtA.Fly);',
  28564. ' with obj do begin',
  28565. ' e:=doit;',
  28566. ' e:=dosome;',
  28567. ' OnClick:=doit;',
  28568. ' OnClick:=dosome;',
  28569. ' setonclick(doit);',
  28570. ' setonclick(dosome);',
  28571. ' OnShow:=@Run;',
  28572. ' setOnShow(@Run);',
  28573. ' end;']);
  28574. ConvertProgram;
  28575. CheckSource('TestProcType_SafeCallDelphi',
  28576. LinesToStr([ // statements
  28577. 'this.Run = function (i) {',
  28578. '};',
  28579. 'this.Obj = null;',
  28580. 'this.e = null;',
  28581. 'this.p = null;',
  28582. '']),
  28583. LinesToStr([ // $mod.$main
  28584. '$mod.e = $mod.e;',
  28585. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28586. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28587. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28588. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28589. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28590. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28591. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28592. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28593. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28594. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28595. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28596. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28597. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28598. 'var $with = $mod.Obj;',
  28599. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28600. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28601. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28602. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28603. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28604. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28605. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28606. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28607. '']));
  28608. end;
  28609. procedure TTestModule.TestProcType_SafeCall_Arg;
  28610. begin
  28611. StartProgram(false);
  28612. Add([
  28613. 'type',
  28614. ' TSafecallProc = reference to procedure; safecall;',
  28615. 'procedure Fly(const aHandler: TSafecallProc);',
  28616. 'var',
  28617. ' P: TSafecallProc;',
  28618. 'begin',
  28619. ' P := aHandler;',
  28620. ' Fly(P);',
  28621. ' Fly(aHandler);',
  28622. 'end;',
  28623. 'begin',
  28624. ' Fly(nil);',
  28625. '']);
  28626. ConvertProgram;
  28627. CheckSource('TestProcType_SafeCall_Arg',
  28628. LinesToStr([ // statements
  28629. 'this.Fly = function (aHandler) {',
  28630. ' var P = null;',
  28631. ' P = aHandler;',
  28632. ' $mod.Fly(P);',
  28633. ' $mod.Fly(aHandler);',
  28634. '};',
  28635. '']),
  28636. LinesToStr([ // $mod.$main
  28637. '$mod.Fly(null);',
  28638. '']));
  28639. end;
  28640. procedure TTestModule.TestPointer;
  28641. begin
  28642. StartProgram(false);
  28643. Add(['type',
  28644. ' TObject = class end;',
  28645. ' TClass = class of TObject;',
  28646. ' TArrInt = array of longint;',
  28647. 'const',
  28648. ' n = nil;',
  28649. 'var',
  28650. ' v: jsvalue;',
  28651. ' Obj: tobject;',
  28652. ' C: tclass;',
  28653. ' a: tarrint;',
  28654. ' p: Pointer = nil;',
  28655. ' s: string;',
  28656. 'begin',
  28657. ' p:=p;',
  28658. ' p:=nil;',
  28659. ' if p=nil then;',
  28660. ' if nil=p then;',
  28661. ' if Assigned(p) then;',
  28662. ' p:=Pointer(v);',
  28663. ' p:=obj;',
  28664. ' p:=c;',
  28665. ' p:=a;',
  28666. ' p:=tobject;',
  28667. ' obj:=TObject(p);',
  28668. ' c:=TClass(p);',
  28669. ' a:=TArrInt(p);',
  28670. ' p:=n;',
  28671. ' p:=Pointer(a);',
  28672. ' p:=pointer(s);',
  28673. ' s:=string(p);',
  28674. '']);
  28675. ConvertProgram;
  28676. CheckSource('TestPointer',
  28677. LinesToStr([ // statements
  28678. 'rtl.createClass(this, "TObject", null, function () {',
  28679. ' this.$init = function () {',
  28680. ' };',
  28681. ' this.$final = function () {',
  28682. ' };',
  28683. '});',
  28684. 'this.n = null;',
  28685. 'this.v = undefined;',
  28686. 'this.Obj = null;',
  28687. 'this.C = null;',
  28688. 'this.a = [];',
  28689. 'this.p = null;',
  28690. 'this.s = "";',
  28691. '']),
  28692. LinesToStr([ // $mod.$main
  28693. '$mod.p = $mod.p;',
  28694. '$mod.p = null;',
  28695. 'if ($mod.p === null) ;',
  28696. 'if (null === $mod.p) ;',
  28697. 'if ($mod.p != null) ;',
  28698. '$mod.p = $mod.v;',
  28699. '$mod.p = $mod.Obj;',
  28700. '$mod.p = $mod.C;',
  28701. '$mod.p = $mod.a;',
  28702. '$mod.p = $mod.TObject;',
  28703. '$mod.Obj = $mod.p;',
  28704. '$mod.C = $mod.p;',
  28705. '$mod.a = $mod.p;',
  28706. '$mod.p = null;',
  28707. '$mod.p = $mod.a;',
  28708. '$mod.p = $mod.s;',
  28709. '$mod.s = $mod.p;',
  28710. '']));
  28711. end;
  28712. procedure TTestModule.TestPointer_Proc;
  28713. begin
  28714. StartProgram(false);
  28715. Add('type');
  28716. Add(' TObject = class');
  28717. Add(' procedure DoIt; virtual; abstract;');
  28718. Add(' end;');
  28719. Add('procedure DoSome; begin end;');
  28720. Add('var');
  28721. Add(' o: TObject;');
  28722. Add(' p: Pointer;');
  28723. Add('begin');
  28724. Add(' p:=@DoSome;');
  28725. Add(' p:[email protected];');
  28726. ConvertProgram;
  28727. CheckSource('TestPointer_Proc',
  28728. LinesToStr([ // statements
  28729. 'rtl.createClass(this, "TObject", null, function () {',
  28730. ' this.$init = function () {',
  28731. ' };',
  28732. ' this.$final = function () {',
  28733. ' };',
  28734. '});',
  28735. 'this.DoSome = function () {',
  28736. '};',
  28737. 'this.o = null;',
  28738. 'this.p = null;',
  28739. '']),
  28740. LinesToStr([ // $mod.$main
  28741. '$mod.p = $mod.DoSome;',
  28742. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28743. '']));
  28744. end;
  28745. procedure TTestModule.TestPointer_AssignRecordFail;
  28746. begin
  28747. StartProgram(false);
  28748. Add('type');
  28749. Add(' TRec = record end;');
  28750. Add('var');
  28751. Add(' p: Pointer;');
  28752. Add(' r: TRec;');
  28753. Add('begin');
  28754. Add(' p:=r;');
  28755. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28756. nIncompatibleTypesGotExpected);
  28757. ConvertProgram;
  28758. end;
  28759. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28760. begin
  28761. StartProgram(false);
  28762. Add('type');
  28763. Add(' TArr = array[boolean] of longint;');
  28764. Add('var');
  28765. Add(' p: Pointer;');
  28766. Add(' a: TArr;');
  28767. Add('begin');
  28768. Add(' p:=a;');
  28769. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28770. nIncompatibleTypesGotExpected);
  28771. ConvertProgram;
  28772. end;
  28773. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28774. begin
  28775. StartProgram(false);
  28776. Add([
  28777. 'procedure DoIt(args: array of jsvalue); begin end;',
  28778. 'procedure DoAll; varargs; begin end;',
  28779. 'var',
  28780. ' v: jsvalue;',
  28781. 'begin',
  28782. ' DoIt([pointer(v)]);',
  28783. ' DoAll(pointer(v));',
  28784. '']);
  28785. ConvertProgram;
  28786. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28787. LinesToStr([ // statements
  28788. 'this.DoIt = function (args) {',
  28789. '};',
  28790. 'this.DoAll = function () {',
  28791. '};',
  28792. 'this.v = undefined;',
  28793. '']),
  28794. LinesToStr([ // $mod.$main
  28795. '$mod.DoIt([$mod.v]);',
  28796. '$mod.DoAll($mod.v);',
  28797. '']));
  28798. end;
  28799. procedure TTestModule.TestPointer_NonRecordFail;
  28800. begin
  28801. StartProgram(false);
  28802. Add([
  28803. 'type',
  28804. ' p = ^longint;',
  28805. 'begin',
  28806. '']);
  28807. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28808. ConvertProgram;
  28809. end;
  28810. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28811. begin
  28812. StartProgram(false);
  28813. Add([
  28814. 'procedure DoIt(p: ^longint); begin end;',
  28815. 'begin',
  28816. '']);
  28817. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28818. ConvertProgram;
  28819. end;
  28820. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28821. begin
  28822. StartProgram(false);
  28823. Add([
  28824. 'var p: ^longint;',
  28825. 'begin',
  28826. '']);
  28827. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28828. ConvertProgram;
  28829. end;
  28830. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28831. begin
  28832. StartProgram(false);
  28833. Add([
  28834. 'function DoIt: ^longint; begin end;',
  28835. 'begin',
  28836. '']);
  28837. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28838. ConvertProgram;
  28839. end;
  28840. procedure TTestModule.TestPointer_AddrOperatorFail;
  28841. begin
  28842. StartProgram(false);
  28843. Add([
  28844. 'var i: longint;',
  28845. 'begin',
  28846. ' if @i=nil then ;',
  28847. '']);
  28848. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28849. ConvertProgram;
  28850. end;
  28851. procedure TTestModule.TestPointer_ArrayParamsFail;
  28852. begin
  28853. StartProgram(false);
  28854. Add([
  28855. 'var',
  28856. ' p: Pointer;',
  28857. 'begin',
  28858. ' p:=p[1];',
  28859. '']);
  28860. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28861. ConvertProgram;
  28862. end;
  28863. procedure TTestModule.TestPointer_PointerAddFail;
  28864. begin
  28865. StartProgram(false);
  28866. Add([
  28867. 'var',
  28868. ' p: Pointer;',
  28869. 'begin',
  28870. ' p:=p+1;',
  28871. '']);
  28872. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28873. ConvertProgram;
  28874. end;
  28875. procedure TTestModule.TestPointer_IncPointerFail;
  28876. begin
  28877. StartProgram(false);
  28878. Add([
  28879. 'var',
  28880. ' p: Pointer;',
  28881. 'begin',
  28882. ' inc(p,1);',
  28883. '']);
  28884. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28885. nIncompatibleTypeArgNo);
  28886. ConvertProgram;
  28887. end;
  28888. procedure TTestModule.TestPointer_Record;
  28889. begin
  28890. StartProgram(false);
  28891. Add([
  28892. 'type',
  28893. ' TRec = record x: longint; end;',
  28894. ' PRec = ^TRec;',
  28895. 'var',
  28896. ' r: TRec;',
  28897. ' p: PRec;',
  28898. ' q: ^TRec;',
  28899. ' Ptr: pointer;',
  28900. 'begin',
  28901. ' new(p);',
  28902. ' p:=@r;',
  28903. ' r:=p^;',
  28904. ' r.x:=p^.x;',
  28905. ' p^.x:=r.x;',
  28906. ' if p^.x=3 then ;',
  28907. ' if 4=p^.x then ;',
  28908. ' dispose(p);',
  28909. ' new(q);',
  28910. ' dispose(q);',
  28911. ' Ptr:=p;',
  28912. ' p:=PRec(ptr);',
  28913. '']);
  28914. ConvertProgram;
  28915. CheckSource('TestPointer_Record',
  28916. LinesToStr([ // statements
  28917. 'rtl.recNewT(this, "TRec", function () {',
  28918. ' this.x = 0;',
  28919. ' this.$eq = function (b) {',
  28920. ' return this.x === b.x;',
  28921. ' };',
  28922. ' this.$assign = function (s) {',
  28923. ' this.x = s.x;',
  28924. ' return this;',
  28925. ' };',
  28926. '});',
  28927. 'this.r = this.TRec.$new();',
  28928. 'this.p = null;',
  28929. 'this.q = null;',
  28930. 'this.Ptr = null;',
  28931. '']),
  28932. LinesToStr([ // $mod.$main
  28933. '$mod.p = $mod.TRec.$new();',
  28934. '$mod.p = $mod.r;',
  28935. '$mod.r.$assign($mod.p);',
  28936. '$mod.r.x = $mod.p.x;',
  28937. '$mod.p.x = $mod.r.x;',
  28938. 'if ($mod.p.x === 3) ;',
  28939. 'if (4 === $mod.p.x) ;',
  28940. '$mod.p = null;',
  28941. '$mod.q = $mod.TRec.$new();',
  28942. '$mod.q = null;',
  28943. '$mod.Ptr = $mod.p;',
  28944. '$mod.p = $mod.Ptr;',
  28945. '']));
  28946. end;
  28947. procedure TTestModule.TestPointer_RecordArg;
  28948. begin
  28949. StartProgram(false);
  28950. Add([
  28951. '{$modeswitch autoderef}',
  28952. 'type',
  28953. ' TRec = record x: longint; end;',
  28954. ' PRec = ^TRec;',
  28955. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  28956. 'begin',
  28957. ' a.x:=a.x;',
  28958. ' a^.x:=a^.x;',
  28959. ' with a^ do',
  28960. ' x:=x;',
  28961. 'end;',
  28962. 'function GetIt(p: PRec): PRec;',
  28963. 'begin',
  28964. ' p.x:=p.x;',
  28965. ' p^.x:=p^.x;',
  28966. ' with p^ do',
  28967. ' x:=x;',
  28968. 'end;',
  28969. 'var',
  28970. ' r: TRec;',
  28971. ' p: PRec;',
  28972. 'begin',
  28973. ' p:=GetIt(p);',
  28974. ' p^:=GetIt(@r)^;',
  28975. ' DoIt(p,p,p);',
  28976. ' DoIt(@r,p,p);',
  28977. '']);
  28978. ConvertProgram;
  28979. CheckSource('TestPointer_RecordArg',
  28980. LinesToStr([ // statements
  28981. 'rtl.recNewT(this, "TRec", function () {',
  28982. ' this.x = 0;',
  28983. ' this.$eq = function (b) {',
  28984. ' return this.x === b.x;',
  28985. ' };',
  28986. ' this.$assign = function (s) {',
  28987. ' this.x = s.x;',
  28988. ' return this;',
  28989. ' };',
  28990. '});',
  28991. 'this.DoIt = function (a, b, c) {',
  28992. ' var Result = $mod.TRec.$new();',
  28993. ' a.x = a.x;',
  28994. ' a.x = a.x;',
  28995. ' a.x = a.x;',
  28996. ' return Result;',
  28997. '};',
  28998. 'this.GetIt = function (p) {',
  28999. ' var Result = null;',
  29000. ' p.x = p.x;',
  29001. ' p.x = p.x;',
  29002. ' p.x = p.x;',
  29003. ' return Result;',
  29004. '};',
  29005. 'this.r = this.TRec.$new();',
  29006. 'this.p = null;',
  29007. '']),
  29008. LinesToStr([ // $mod.$main
  29009. '$mod.p = $mod.GetIt($mod.p);',
  29010. '$mod.p.$assign($mod.GetIt($mod.r));',
  29011. '$mod.DoIt($mod.p, {',
  29012. ' p: $mod,',
  29013. ' get: function () {',
  29014. ' return this.p.p;',
  29015. ' },',
  29016. ' set: function (v) {',
  29017. ' this.p.p = v;',
  29018. ' }',
  29019. '}, {',
  29020. ' p: $mod,',
  29021. ' get: function () {',
  29022. ' return this.p.p;',
  29023. ' },',
  29024. ' set: function (v) {',
  29025. ' this.p.p = v;',
  29026. ' }',
  29027. '});',
  29028. '$mod.DoIt($mod.r, {',
  29029. ' p: $mod,',
  29030. ' get: function () {',
  29031. ' return this.p.p;',
  29032. ' },',
  29033. ' set: function (v) {',
  29034. ' this.p.p = v;',
  29035. ' }',
  29036. '}, {',
  29037. ' p: $mod,',
  29038. ' get: function () {',
  29039. ' return this.p.p;',
  29040. ' },',
  29041. ' set: function (v) {',
  29042. ' this.p.p = v;',
  29043. ' }',
  29044. '});',
  29045. '']));
  29046. end;
  29047. procedure TTestModule.TestJSValue_AssignToJSValue;
  29048. begin
  29049. StartProgram(false);
  29050. Add('var');
  29051. Add(' v: jsvalue;');
  29052. Add(' i: longint;');
  29053. Add(' s: string;');
  29054. Add(' b: boolean;');
  29055. Add(' d: double;');
  29056. Add(' p: pointer;');
  29057. Add('begin');
  29058. Add(' v:=v;');
  29059. Add(' v:=1;');
  29060. Add(' v:=i;');
  29061. Add(' v:='''';');
  29062. Add(' v:=''c'';');
  29063. Add(' v:=''foo'';');
  29064. Add(' v:=s;');
  29065. Add(' v:=false;');
  29066. Add(' v:=true;');
  29067. Add(' v:=b;');
  29068. Add(' v:=0.1;');
  29069. Add(' v:=d;');
  29070. Add(' v:=nil;');
  29071. Add(' v:=p;');
  29072. ConvertProgram;
  29073. CheckSource('TestJSValue_AssignToJSValue',
  29074. LinesToStr([ // statements
  29075. 'this.v = undefined;',
  29076. 'this.i = 0;',
  29077. 'this.s = "";',
  29078. 'this.b = false;',
  29079. 'this.d = 0.0;',
  29080. 'this.p = null;',
  29081. '']),
  29082. LinesToStr([ // $mod.$main
  29083. '$mod.v = $mod.v;',
  29084. '$mod.v = 1;',
  29085. '$mod.v = $mod.i;',
  29086. '$mod.v = "";',
  29087. '$mod.v = "c";',
  29088. '$mod.v = "foo";',
  29089. '$mod.v = $mod.s;',
  29090. '$mod.v = false;',
  29091. '$mod.v = true;',
  29092. '$mod.v = $mod.b;',
  29093. '$mod.v = 0.1;',
  29094. '$mod.v = $mod.d;',
  29095. '$mod.v = null;',
  29096. '$mod.v = $mod.p;',
  29097. '']));
  29098. end;
  29099. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29100. begin
  29101. StartProgram(false);
  29102. Add('type');
  29103. Add(' integer = longint;');
  29104. Add(' TYesNo = boolean;');
  29105. Add(' TFloat = double;');
  29106. Add(' TCaption = string;');
  29107. Add(' TChar = AnsiChar;');
  29108. Add('var');
  29109. Add(' v: jsvalue;');
  29110. Add(' i: integer;');
  29111. Add(' s: TCaption;');
  29112. Add(' b: TYesNo;');
  29113. Add(' d: TFloat;');
  29114. Add(' c: AnsiChar;');
  29115. Add('begin');
  29116. Add(' i:=longint(v);');
  29117. Add(' i:=integer(v);');
  29118. Add(' s:=string(v);');
  29119. Add(' s:=TCaption(v);');
  29120. Add(' b:=boolean(v);');
  29121. Add(' b:=TYesNo(v);');
  29122. Add(' d:=double(v);');
  29123. Add(' d:=TFloat(v);');
  29124. Add(' c:=AnsiChar(v);');
  29125. Add(' c:=TChar(v);');
  29126. ConvertProgram;
  29127. CheckSource('TestJSValue_TypeCastToBaseType',
  29128. LinesToStr([ // statements
  29129. 'this.v = undefined;',
  29130. 'this.i = 0;',
  29131. 'this.s = "";',
  29132. 'this.b = false;',
  29133. 'this.d = 0.0;',
  29134. 'this.c = "";',
  29135. '']),
  29136. LinesToStr([ // $mod.$main
  29137. '$mod.i = rtl.trunc($mod.v);',
  29138. '$mod.i = rtl.trunc($mod.v);',
  29139. '$mod.s = "" + $mod.v;',
  29140. '$mod.s = "" + $mod.v;',
  29141. '$mod.b = !($mod.v == false);',
  29142. '$mod.b = !($mod.v == false);',
  29143. '$mod.d = rtl.getNumber($mod.v);',
  29144. '$mod.d = rtl.getNumber($mod.v);',
  29145. '$mod.c = rtl.getChar($mod.v);',
  29146. '$mod.c = rtl.getChar($mod.v);',
  29147. '']));
  29148. end;
  29149. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29150. begin
  29151. StartProgram(false);
  29152. Add([
  29153. 'type',
  29154. ' TArr = array of word;',
  29155. ' TRec = record end;',
  29156. ' TSet = set of boolean;',
  29157. 'procedure Fly(v: jsvalue);',
  29158. 'begin',
  29159. 'end;',
  29160. 'var',
  29161. ' a: TArr;',
  29162. ' r: TRec;',
  29163. ' s: TSet;',
  29164. 'begin',
  29165. ' Fly(jsvalue(a));',
  29166. ' Fly(jsvalue(r));',
  29167. ' Fly(jsvalue(s));',
  29168. '']);
  29169. ConvertProgram;
  29170. CheckSource('TestJSValue_TypecastToJSValue',
  29171. LinesToStr([ // statements
  29172. 'rtl.recNewT(this, "TRec", function () {',
  29173. ' this.$eq = function (b) {',
  29174. ' return true;',
  29175. ' };',
  29176. ' this.$assign = function (s) {',
  29177. ' return this;',
  29178. ' };',
  29179. '});',
  29180. 'this.Fly = function (v) {',
  29181. '};',
  29182. 'this.a = [];',
  29183. 'this.r = this.TRec.$new();',
  29184. 'this.s = {};',
  29185. '']),
  29186. LinesToStr([ // $mod.$main
  29187. '$mod.Fly($mod.a);',
  29188. '$mod.Fly($mod.r);',
  29189. '$mod.Fly($mod.s);',
  29190. '']));
  29191. end;
  29192. procedure TTestModule.TestJSValue_Equal;
  29193. begin
  29194. StartProgram(false);
  29195. Add('type');
  29196. Add(' integer = longint;');
  29197. Add(' TYesNo = boolean;');
  29198. Add(' TFloat = double;');
  29199. Add(' TCaption = string;');
  29200. Add(' TChar = AnsiChar;');
  29201. Add(' TMulti = JSValue;');
  29202. Add('var');
  29203. Add(' v: jsvalue;');
  29204. Add(' i: integer;');
  29205. Add(' s: TCaption;');
  29206. Add(' b: TYesNo;');
  29207. Add(' d: TFloat;');
  29208. Add(' c: AnsiChar;');
  29209. Add(' m: TMulti;');
  29210. Add('begin');
  29211. Add(' b:=v=v;');
  29212. Add(' b:=v<>v;');
  29213. Add(' b:=v=1;');
  29214. Add(' b:=v<>1;');
  29215. Add(' b:=2=v;');
  29216. Add(' b:=2<>v;');
  29217. Add(' b:=v=i;');
  29218. Add(' b:=i=v;');
  29219. Add(' b:=v=nil;');
  29220. Add(' b:=nil=v;');
  29221. Add(' b:=v=false;');
  29222. Add(' b:=true=v;');
  29223. Add(' b:=v=b;');
  29224. Add(' b:=b=v;');
  29225. Add(' b:=v=s;');
  29226. Add(' b:=s=v;');
  29227. Add(' b:=v=''foo'';');
  29228. Add(' b:=''''=v;');
  29229. Add(' b:=v=d;');
  29230. Add(' b:=d=v;');
  29231. Add(' b:=v=3.4;');
  29232. Add(' b:=5.6=v;');
  29233. Add(' b:=v=c;');
  29234. Add(' b:=c=v;');
  29235. Add(' b:=m=m;');
  29236. Add(' b:=v=m;');
  29237. Add(' b:=m=v;');
  29238. ConvertProgram;
  29239. CheckSource('TestJSValue_Equal',
  29240. LinesToStr([ // statements
  29241. 'this.v = undefined;',
  29242. 'this.i = 0;',
  29243. 'this.s = "";',
  29244. 'this.b = false;',
  29245. 'this.d = 0.0;',
  29246. 'this.c = "";',
  29247. 'this.m = undefined;',
  29248. '']),
  29249. LinesToStr([ // $mod.$main
  29250. '$mod.b = $mod.v == $mod.v;',
  29251. '$mod.b = $mod.v != $mod.v;',
  29252. '$mod.b = $mod.v == 1;',
  29253. '$mod.b = $mod.v != 1;',
  29254. '$mod.b = 2 == $mod.v;',
  29255. '$mod.b = 2 != $mod.v;',
  29256. '$mod.b = $mod.v == $mod.i;',
  29257. '$mod.b = $mod.i == $mod.v;',
  29258. '$mod.b = $mod.v == null;',
  29259. '$mod.b = null == $mod.v;',
  29260. '$mod.b = $mod.v == false;',
  29261. '$mod.b = true == $mod.v;',
  29262. '$mod.b = $mod.v == $mod.b;',
  29263. '$mod.b = $mod.b == $mod.v;',
  29264. '$mod.b = $mod.v == $mod.s;',
  29265. '$mod.b = $mod.s == $mod.v;',
  29266. '$mod.b = $mod.v == "foo";',
  29267. '$mod.b = "" == $mod.v;',
  29268. '$mod.b = $mod.v == $mod.d;',
  29269. '$mod.b = $mod.d == $mod.v;',
  29270. '$mod.b = $mod.v == 3.4;',
  29271. '$mod.b = 5.6 == $mod.v;',
  29272. '$mod.b = $mod.v == $mod.c;',
  29273. '$mod.b = $mod.c == $mod.v;',
  29274. '$mod.b = $mod.m == $mod.m;',
  29275. '$mod.b = $mod.v == $mod.m;',
  29276. '$mod.b = $mod.m == $mod.v;',
  29277. '']));
  29278. end;
  29279. procedure TTestModule.TestJSValue_If;
  29280. begin
  29281. StartProgram(false);
  29282. Add([
  29283. 'procedure Fly(var u);',
  29284. 'begin',
  29285. ' if jsvalue(u) then ;',
  29286. 'end;',
  29287. 'var',
  29288. ' v: jsvalue;',
  29289. 'begin',
  29290. ' if v then ;',
  29291. ' while v do ;',
  29292. ' repeat until v;',
  29293. '']);
  29294. ConvertProgram;
  29295. CheckSource('TestJSValue_If',
  29296. LinesToStr([ // statements
  29297. 'this.Fly = function (u) {',
  29298. ' if (u.get()) ;',
  29299. '};',
  29300. 'this.v = undefined;',
  29301. '']),
  29302. LinesToStr([ // $mod.$main
  29303. 'if ($mod.v) ;',
  29304. 'while($mod.v){',
  29305. '};',
  29306. 'do{',
  29307. '} while(!$mod.v);',
  29308. '']));
  29309. end;
  29310. procedure TTestModule.TestJSValue_Not;
  29311. begin
  29312. StartProgram(false);
  29313. Add([
  29314. 'var',
  29315. ' v: jsvalue;',
  29316. ' b: boolean;',
  29317. 'begin',
  29318. ' b:=not v;',
  29319. ' if not v then ;',
  29320. ' while not v do ;',
  29321. ' repeat until not v;',
  29322. '']);
  29323. ConvertProgram;
  29324. CheckSource('TestJSValue_If',
  29325. LinesToStr([ // statements
  29326. 'this.v = undefined;',
  29327. 'this.b = false;',
  29328. '']),
  29329. LinesToStr([ // $mod.$main
  29330. '$mod.b=!$mod.v;',
  29331. 'if (!$mod.v) ;',
  29332. 'while(!$mod.v){',
  29333. '};',
  29334. 'do{',
  29335. '} while($mod.v);',
  29336. '']));
  29337. end;
  29338. procedure TTestModule.TestJSValue_Enum;
  29339. begin
  29340. StartProgram(false);
  29341. Add('type');
  29342. Add(' TColor = (red, blue);');
  29343. Add(' TRedBlue = TColor;');
  29344. Add('var');
  29345. Add(' v: jsvalue;');
  29346. Add(' e: TColor;');
  29347. Add('begin');
  29348. Add(' v:=e;');
  29349. Add(' v:=TColor(e);');
  29350. Add(' v:=TRedBlue(e);');
  29351. Add(' e:=TColor(v);');
  29352. Add(' e:=TRedBlue(v);');
  29353. ConvertProgram;
  29354. CheckSource('TestJSValue_Enum',
  29355. LinesToStr([ // statements
  29356. 'this.TColor = {',
  29357. ' "0": "red",',
  29358. ' red: 0,',
  29359. ' "1": "blue",',
  29360. ' blue: 1',
  29361. '};',
  29362. 'this.v = undefined;',
  29363. 'this.e = 0;',
  29364. '']),
  29365. LinesToStr([ // $mod.$main
  29366. '$mod.v = $mod.e;',
  29367. '$mod.v = $mod.e;',
  29368. '$mod.v = $mod.e;',
  29369. '$mod.e = $mod.v;',
  29370. '$mod.e = $mod.v;',
  29371. '']));
  29372. end;
  29373. procedure TTestModule.TestJSValue_ClassInstance;
  29374. begin
  29375. StartProgram(false);
  29376. Add([
  29377. 'type',
  29378. ' TObject = class',
  29379. ' end;',
  29380. ' TBirdObject = TObject;',
  29381. 'var',
  29382. ' v: jsvalue;',
  29383. ' o: TObject;',
  29384. 'begin',
  29385. ' v:=o;',
  29386. ' v:=TObject(o);',
  29387. ' v:=TBirdObject(o);',
  29388. ' o:=TObject(v);',
  29389. ' o:=TBirdObject(v);',
  29390. ' if v is TObject then ;',
  29391. '']);
  29392. ConvertProgram;
  29393. CheckSource('TestJSValue_ClassInstance',
  29394. LinesToStr([ // statements
  29395. 'rtl.createClass(this, "TObject", null, function () {',
  29396. ' this.$init = function () {',
  29397. ' };',
  29398. ' this.$final = function () {',
  29399. ' };',
  29400. '});',
  29401. 'this.v = undefined;',
  29402. 'this.o = null;',
  29403. '']),
  29404. LinesToStr([ // $mod.$main
  29405. '$mod.v = $mod.o;',
  29406. '$mod.v = $mod.o;',
  29407. '$mod.v = $mod.o;',
  29408. '$mod.o = rtl.getObject($mod.v);',
  29409. '$mod.o = rtl.getObject($mod.v);',
  29410. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29411. '']));
  29412. end;
  29413. procedure TTestModule.TestJSValue_ClassOf;
  29414. begin
  29415. StartProgram(false);
  29416. Add([
  29417. 'type',
  29418. ' TClass = class of TObject;',
  29419. ' TObject = class',
  29420. ' end;',
  29421. ' TBirds = class of TBird;',
  29422. ' TBird = class(TObject) end;',
  29423. 'var',
  29424. ' v: jsvalue;',
  29425. ' c: TClass;',
  29426. 'begin',
  29427. ' v:=c;',
  29428. ' v:=TObject;',
  29429. ' v:=TClass(c);',
  29430. ' v:=TBirds(c);',
  29431. ' c:=TClass(v);',
  29432. ' c:=TBirds(v);',
  29433. ' if v is TClass then ;',
  29434. '']);
  29435. ConvertProgram;
  29436. CheckSource('TestJSValue_ClassOf',
  29437. LinesToStr([ // statements
  29438. 'rtl.createClass(this, "TObject", null, function () {',
  29439. ' this.$init = function () {',
  29440. ' };',
  29441. ' this.$final = function () {',
  29442. ' };',
  29443. '});',
  29444. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29445. '});',
  29446. 'this.v = undefined;',
  29447. 'this.c = null;',
  29448. '']),
  29449. LinesToStr([ // $mod.$main
  29450. '$mod.v = $mod.c;',
  29451. '$mod.v = $mod.TObject;',
  29452. '$mod.v = $mod.c;',
  29453. '$mod.v = $mod.c;',
  29454. '$mod.c = rtl.getObject($mod.v);',
  29455. '$mod.c = rtl.getObject($mod.v);',
  29456. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29457. '']));
  29458. end;
  29459. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29460. begin
  29461. StartProgram(false);
  29462. Add([
  29463. 'type',
  29464. ' integer = longint;',
  29465. ' TArray = array of JSValue;',
  29466. ' TArrgh = tarray;',
  29467. ' TArrInt = array of integer;',
  29468. 'var',
  29469. ' v: jsvalue;',
  29470. ' TheArray: tarray = (1,''2'');',
  29471. ' Arr: tarrgh;',
  29472. ' i: integer;',
  29473. ' ArrInt: tarrint;',
  29474. 'begin',
  29475. ' arr:=thearray;',
  29476. ' thearray:=arr;',
  29477. ' setlength(arr,2);',
  29478. ' setlength(thearray,3);',
  29479. ' arr[4]:=v;',
  29480. ' arr[5]:=length(thearray);',
  29481. ' arr[6]:=nil;',
  29482. ' arr[7]:=thearray[8];',
  29483. ' arr[low(arr)]:=high(thearray);',
  29484. ' arr:=arrint;',
  29485. ' arrInt:=tarrint(arr);',
  29486. ' if TheArray = nil then ;',
  29487. ' if nil = TheArray then ;',
  29488. ' if TheArray <> nil then ;',
  29489. ' if nil <> TheArray then ;',
  29490. '']);
  29491. ConvertProgram;
  29492. CheckSource('TestJSValue_ArrayOfJSValue',
  29493. LinesToStr([ // statements
  29494. 'this.v = undefined;',
  29495. 'this.TheArray = [1, "2"];',
  29496. 'this.Arr = [];',
  29497. 'this.i = 0;',
  29498. 'this.ArrInt = [];',
  29499. '']),
  29500. LinesToStr([ // $mod.$main
  29501. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29502. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29503. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29504. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29505. '$mod.Arr[4] = $mod.v;',
  29506. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29507. '$mod.Arr[6] = null;',
  29508. '$mod.Arr[7] = $mod.TheArray[8];',
  29509. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29510. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29511. '$mod.ArrInt = $mod.Arr;',
  29512. 'if (rtl.length($mod.TheArray) === 0) ;',
  29513. 'if (rtl.length($mod.TheArray) === 0) ;',
  29514. 'if (rtl.length($mod.TheArray) > 0) ;',
  29515. 'if (rtl.length($mod.TheArray) > 0) ;',
  29516. '']));
  29517. end;
  29518. procedure TTestModule.TestJSValue_ArrayLit;
  29519. begin
  29520. StartProgram(false);
  29521. Add([
  29522. 'type',
  29523. ' TFlag = (big,small);',
  29524. ' TArray = array of JSValue;',
  29525. ' TObject = class end;',
  29526. ' TClass = class of TObject;',
  29527. 'var',
  29528. ' v: jsvalue;',
  29529. ' a: TArray;',
  29530. ' o: TObject;',
  29531. 'begin',
  29532. ' a:=[];',
  29533. ' a:=[1];',
  29534. ' a:=[1,2];',
  29535. ' a:=[big];',
  29536. ' a:=[1,big];',
  29537. ' a:=[o,nil];',
  29538. '']);
  29539. ConvertProgram;
  29540. CheckSource('TestJSValue_ArrayLit',
  29541. LinesToStr([ // statements
  29542. 'this.TFlag = {',
  29543. ' "0": "big",',
  29544. ' big: 0,',
  29545. ' "1": "small",',
  29546. ' small: 1',
  29547. '};',
  29548. 'rtl.createClass(this, "TObject", null, function () {',
  29549. ' this.$init = function () {',
  29550. ' };',
  29551. ' this.$final = function () {',
  29552. ' };',
  29553. '});',
  29554. 'this.v = undefined;',
  29555. 'this.a = [];',
  29556. 'this.o = null;',
  29557. '']),
  29558. LinesToStr([ // $mod.$main
  29559. '$mod.a = [];',
  29560. '$mod.a = [1];',
  29561. '$mod.a = [1, 2];',
  29562. '$mod.a = [$mod.TFlag.big];',
  29563. '$mod.a = [1, $mod.TFlag.big];',
  29564. '$mod.a = [$mod.o, null];',
  29565. '']));
  29566. end;
  29567. procedure TTestModule.TestJSValue_Params;
  29568. begin
  29569. StartProgram(false);
  29570. Add('type');
  29571. Add(' integer = longint;');
  29572. Add(' TYesNo = boolean;');
  29573. Add(' TFloat = double;');
  29574. Add(' TCaption = string;');
  29575. Add(' TChar = AnsiChar;');
  29576. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29577. Add('var');
  29578. Add(' l: jsvalue;');
  29579. Add('begin');
  29580. Add(' a:=a;');
  29581. Add(' l:=b;');
  29582. Add(' c:=c;');
  29583. Add(' d:=d;');
  29584. Add(' Result:=l;');
  29585. Add('end;');
  29586. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29587. Add('var');
  29588. Add(' v: jsvalue;');
  29589. Add(' i: integer;');
  29590. Add(' b: TYesNo;');
  29591. Add(' d: TFloat;');
  29592. Add(' s: TCaption;');
  29593. Add(' c: TChar;');
  29594. Add('begin');
  29595. Add(' v:=doit(v,v,v,v);');
  29596. Add(' i:=integer(dosome(i,i));');
  29597. Add(' b:=TYesNo(dosome(b,b));');
  29598. Add(' d:=TFloat(dosome(d,d));');
  29599. Add(' s:=TCaption(dosome(s,s));');
  29600. Add(' c:=TChar(dosome(c,c));');
  29601. ConvertProgram;
  29602. CheckSource('TestJSValue_Params',
  29603. LinesToStr([ // statements
  29604. 'this.DoIt = function (a, b, c, d) {',
  29605. ' var Result = undefined;',
  29606. ' var l = undefined;',
  29607. ' a = a;',
  29608. ' l = b;',
  29609. ' c.set(c.get());',
  29610. ' d.set(d.get());',
  29611. ' Result = l;',
  29612. ' return Result;',
  29613. '};',
  29614. 'this.DoSome = function (a, b) {',
  29615. ' var Result = undefined;',
  29616. ' return Result;',
  29617. '};',
  29618. 'this.v = undefined;',
  29619. 'this.i = 0;',
  29620. 'this.b = false;',
  29621. 'this.d = 0.0;',
  29622. 'this.s = "";',
  29623. 'this.c = "";',
  29624. '']),
  29625. LinesToStr([ // $mod.$main
  29626. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29627. ' p: $mod,',
  29628. ' get: function () {',
  29629. ' return this.p.v;',
  29630. ' },',
  29631. ' set: function (v) {',
  29632. ' this.p.v = v;',
  29633. ' }',
  29634. '}, {',
  29635. ' p: $mod,',
  29636. ' get: function () {',
  29637. ' return this.p.v;',
  29638. ' },',
  29639. ' set: function (v) {',
  29640. ' this.p.v = v;',
  29641. ' }',
  29642. '});',
  29643. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29644. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29645. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29646. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29647. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29648. '']));
  29649. end;
  29650. procedure TTestModule.TestJSValue_UntypedParam;
  29651. begin
  29652. StartProgram(false);
  29653. Add('function DoIt(const a; var b; out c): jsvalue;');
  29654. Add('begin');
  29655. Add(' Result:=a;');
  29656. Add(' Result:=b;');
  29657. Add(' Result:=c;');
  29658. Add(' b:=Result;');
  29659. Add(' c:=Result;');
  29660. Add('end;');
  29661. Add('var i: longint;');
  29662. Add('begin');
  29663. Add(' doit(i,i,i);');
  29664. ConvertProgram;
  29665. CheckSource('TestJSValue_UntypedParam',
  29666. LinesToStr([ // statements
  29667. 'this.DoIt = function (a, b, c) {',
  29668. ' var Result = undefined;',
  29669. ' Result = a;',
  29670. ' Result = b.get();',
  29671. ' Result = c.get();',
  29672. ' b.set(Result);',
  29673. ' c.set(Result);',
  29674. ' return Result;',
  29675. '};',
  29676. 'this.i = 0;',
  29677. '']),
  29678. LinesToStr([ // $mod.$main
  29679. '$mod.DoIt($mod.i, {',
  29680. ' p: $mod,',
  29681. ' get: function () {',
  29682. ' return this.p.i;',
  29683. ' },',
  29684. ' set: function (v) {',
  29685. ' this.p.i = v;',
  29686. ' }',
  29687. '}, {',
  29688. ' p: $mod,',
  29689. ' get: function () {',
  29690. ' return this.p.i;',
  29691. ' },',
  29692. ' set: function (v) {',
  29693. ' this.p.i = v;',
  29694. ' }',
  29695. '});',
  29696. '']));
  29697. end;
  29698. procedure TTestModule.TestJSValue_FuncResultType;
  29699. begin
  29700. StartProgram(false);
  29701. Add('type');
  29702. Add(' integer = longint;');
  29703. Add(' TJSValueArray = array of JSValue;');
  29704. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29705. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29706. Add('begin');
  29707. Add(' while Compare(P,aList[0])>0 do ;');
  29708. Add('end;');
  29709. Add('var');
  29710. Add(' Compare: TListSortCompare;');
  29711. Add(' V: JSValue;');
  29712. Add(' i: integer;');
  29713. Add('begin');
  29714. Add(' if Compare(V,V)>0 then ;');
  29715. Add(' if Compare(i,i)>1 then ;');
  29716. Add(' if Compare(nil,false)>2 then ;');
  29717. Add(' if Compare(1,true)>3 then ;');
  29718. ConvertProgram;
  29719. CheckSource('TestJSValue_UntypedParam',
  29720. LinesToStr([ // statements
  29721. 'this.Sort = function (P, aList, Compare) {',
  29722. ' while (Compare(P, aList[0]) > 0) {',
  29723. ' };',
  29724. '};',
  29725. 'this.Compare = null;',
  29726. 'this.V = undefined;',
  29727. 'this.i = 0;',
  29728. '']),
  29729. LinesToStr([ // $mod.$main
  29730. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29731. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29732. 'if ($mod.Compare(null, false) > 2) ;',
  29733. 'if ($mod.Compare(1, true) > 3) ;',
  29734. '']));
  29735. end;
  29736. procedure TTestModule.TestJSValue_ProcType_Assign;
  29737. begin
  29738. StartProgram(false);
  29739. Add('type');
  29740. Add(' integer = longint;');
  29741. Add(' TObject = class');
  29742. Add(' class function GetGlob: integer;');
  29743. Add(' function Getter: integer;');
  29744. Add(' end;');
  29745. Add('class function TObject.GetGlob: integer;');
  29746. Add('var v1: jsvalue;');
  29747. Add('begin');
  29748. Add(' v1:=@GetGlob;');
  29749. Add(' v1:[email protected];');
  29750. Add('end;');
  29751. Add('function TObject.Getter: integer;');
  29752. Add('var v2: jsvalue;');
  29753. Add('begin');
  29754. Add(' v2:=@Getter;');
  29755. Add(' v2:[email protected];');
  29756. Add(' v2:=@GetGlob;');
  29757. Add(' v2:[email protected];');
  29758. Add('end;');
  29759. Add('function GetIt(i: integer): integer;');
  29760. Add('var v3: jsvalue;');
  29761. Add('begin');
  29762. Add(' v3:=@GetIt;');
  29763. Add('end;');
  29764. Add('var');
  29765. Add(' V: JSValue;');
  29766. Add(' o: TObject;');
  29767. Add('begin');
  29768. Add(' v:=@GetIt;');
  29769. Add(' v:[email protected];');
  29770. Add(' v:[email protected];');
  29771. ConvertProgram;
  29772. CheckSource('TestJSValue_ProcType_Assign',
  29773. LinesToStr([ // statements
  29774. 'rtl.createClass(this, "TObject", null, function () {',
  29775. ' this.$init = function () {',
  29776. ' };',
  29777. ' this.$final = function () {',
  29778. ' };',
  29779. ' this.GetGlob = function () {',
  29780. ' var Result = 0;',
  29781. ' var v1 = undefined;',
  29782. ' v1 = rtl.createCallback(this, "GetGlob");',
  29783. ' v1 = rtl.createCallback(this, "GetGlob");',
  29784. ' return Result;',
  29785. ' };',
  29786. ' this.Getter = function () {',
  29787. ' var Result = 0;',
  29788. ' var v2 = undefined;',
  29789. ' v2 = rtl.createCallback(this, "Getter");',
  29790. ' v2 = rtl.createCallback(this, "Getter");',
  29791. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29792. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29793. ' return Result;',
  29794. ' };',
  29795. '});',
  29796. 'this.GetIt = function (i) {',
  29797. ' var Result = 0;',
  29798. ' var v3 = undefined;',
  29799. ' v3 = $mod.GetIt;',
  29800. ' return Result;',
  29801. '};',
  29802. 'this.V = undefined;',
  29803. 'this.o = null;',
  29804. '']),
  29805. LinesToStr([ // $mod.$main
  29806. '$mod.V = $mod.GetIt;',
  29807. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29808. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29809. '']));
  29810. end;
  29811. procedure TTestModule.TestJSValue_ProcType_Equal;
  29812. begin
  29813. StartProgram(false);
  29814. Add('type');
  29815. Add(' integer = longint;');
  29816. Add(' TObject = class');
  29817. Add(' class function GetGlob: integer;');
  29818. Add(' function Getter: integer;');
  29819. Add(' end;');
  29820. Add('class function TObject.GetGlob: integer;');
  29821. Add('var v1: jsvalue;');
  29822. Add('begin');
  29823. Add(' if v1=@GetGlob then;');
  29824. Add(' if [email protected] then ;');
  29825. Add('end;');
  29826. Add('function TObject.Getter: integer;');
  29827. Add('var v2: jsvalue;');
  29828. Add('begin');
  29829. Add(' if v2=@Getter then;');
  29830. Add(' if [email protected] then ;');
  29831. Add(' if v2=@GetGlob then;');
  29832. Add(' if [email protected] then;');
  29833. Add('end;');
  29834. Add('function GetIt(i: integer): integer;');
  29835. Add('var v3: jsvalue;');
  29836. Add('begin');
  29837. Add(' if v3=@GetIt then;');
  29838. Add('end;');
  29839. Add('var');
  29840. Add(' V: JSValue;');
  29841. Add(' o: TObject;');
  29842. Add('begin');
  29843. Add(' if v=@GetIt then;');
  29844. Add(' if [email protected] then;');
  29845. Add(' if [email protected] then;');
  29846. Add(' if @GetIt=v then;');
  29847. Add(' if @o.Getter=v then;');
  29848. Add(' if @o.GetGlob=v then;');
  29849. ConvertProgram;
  29850. CheckSource('TestJSValue_ProcType_Equal',
  29851. LinesToStr([ // statements
  29852. 'rtl.createClass(this, "TObject", null, function () {',
  29853. ' this.$init = function () {',
  29854. ' };',
  29855. ' this.$final = function () {',
  29856. ' };',
  29857. ' this.GetGlob = function () {',
  29858. ' var Result = 0;',
  29859. ' var v1 = undefined;',
  29860. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29861. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29862. ' return Result;',
  29863. ' };',
  29864. ' this.Getter = function () {',
  29865. ' var Result = 0;',
  29866. ' var v2 = undefined;',
  29867. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29868. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29869. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29870. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29871. ' return Result;',
  29872. ' };',
  29873. '});',
  29874. 'this.GetIt = function (i) {',
  29875. ' var Result = 0;',
  29876. ' var v3 = undefined;',
  29877. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29878. ' return Result;',
  29879. '};',
  29880. 'this.V = undefined;',
  29881. 'this.o = null;',
  29882. '']),
  29883. LinesToStr([ // $mod.$main
  29884. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29885. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29886. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29887. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29888. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29889. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29890. '']));
  29891. end;
  29892. procedure TTestModule.TestJSValue_ProcType_Param;
  29893. begin
  29894. StartProgram(false);
  29895. Add([
  29896. 'type',
  29897. ' variant = jsvalue;',
  29898. ' TArrVariant = array of variant;',
  29899. ' TArrVar2 = TArrVariant;',
  29900. ' TFuncInt = function: longint;',
  29901. 'function GetIt: longint;',
  29902. 'begin',
  29903. 'end;',
  29904. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29905. 'var v: variant;',
  29906. 'begin',
  29907. ' v:=arr[1];',
  29908. 'end;',
  29909. 'var s: string;',
  29910. 'begin',
  29911. ' DoIt(GetIt,[]);',
  29912. ' DoIt(@GetIt,[]);',
  29913. ' DoIt(1,[s,GetIt]);',
  29914. ' DoIt(1,[s,@GetIt]);',
  29915. '']);
  29916. ConvertProgram;
  29917. CheckSource('TestJSValue_ProcType_Param',
  29918. LinesToStr([ // statements
  29919. 'this.GetIt = function () {',
  29920. ' var Result = 0;',
  29921. ' return Result;',
  29922. '};',
  29923. 'this.DoIt = function (p, Arr) {',
  29924. ' var v = undefined;',
  29925. ' v = Arr[1];',
  29926. '};',
  29927. 'this.s = "";',
  29928. '']),
  29929. LinesToStr([ // $mod.$main
  29930. '$mod.DoIt($mod.GetIt(), []);',
  29931. '$mod.DoIt($mod.GetIt, []);',
  29932. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29933. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29934. '']));
  29935. end;
  29936. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29937. begin
  29938. StartProgram(false);
  29939. Add([
  29940. 'var',
  29941. ' v: JSValue;',
  29942. ' p: Pointer;',
  29943. 'begin',
  29944. ' p:=v;',
  29945. '']);
  29946. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29947. nIncompatibleTypesGotExpected);
  29948. ConvertProgram;
  29949. end;
  29950. procedure TTestModule.TestJSValue_OverloadDouble;
  29951. begin
  29952. StartProgram(false);
  29953. Add([
  29954. 'type',
  29955. ' integer = longint;',
  29956. ' tdatetime = double;',
  29957. 'procedure DoIt(d: double); begin end;',
  29958. 'procedure DoIt(v: jsvalue); begin end;',
  29959. 'var',
  29960. ' d: double;',
  29961. ' dt: tdatetime;',
  29962. ' i: integer;',
  29963. ' b: byte;',
  29964. ' shi: shortint;',
  29965. ' w: word;',
  29966. ' smi: smallint;',
  29967. ' lw: longword;',
  29968. ' li: longint;',
  29969. ' ni: nativeint;',
  29970. ' nu: nativeuint;',
  29971. 'begin',
  29972. ' DoIt(d);',
  29973. ' DoIt(dt);',
  29974. ' DoIt(i);',
  29975. ' DoIt(b);',
  29976. ' DoIt(shi);',
  29977. ' DoIt(w);',
  29978. ' DoIt(smi);',
  29979. ' DoIt(lw);',
  29980. ' DoIt(li);',
  29981. ' DoIt(ni);',
  29982. ' DoIt(nu);',
  29983. '']);
  29984. ConvertProgram;
  29985. CheckSource('TestJSValue_OverloadDouble',
  29986. LinesToStr([ // statements
  29987. 'this.DoIt = function (d) {',
  29988. '};',
  29989. 'this.DoIt$1 = function (v) {',
  29990. '};',
  29991. 'this.d = 0.0;',
  29992. 'this.dt = 0.0;',
  29993. 'this.i = 0;',
  29994. 'this.b = 0;',
  29995. 'this.shi = 0;',
  29996. 'this.w = 0;',
  29997. 'this.smi = 0;',
  29998. 'this.lw = 0;',
  29999. 'this.li = 0;',
  30000. 'this.ni = 0;',
  30001. 'this.nu = 0;',
  30002. '']),
  30003. LinesToStr([ // $mod.$main
  30004. '$mod.DoIt($mod.d);',
  30005. '$mod.DoIt($mod.dt);',
  30006. '$mod.DoIt$1($mod.i);',
  30007. '$mod.DoIt$1($mod.b);',
  30008. '$mod.DoIt$1($mod.shi);',
  30009. '$mod.DoIt$1($mod.w);',
  30010. '$mod.DoIt$1($mod.smi);',
  30011. '$mod.DoIt$1($mod.lw);',
  30012. '$mod.DoIt$1($mod.li);',
  30013. '$mod.DoIt$1($mod.ni);',
  30014. '$mod.DoIt$1($mod.nu);',
  30015. '']));
  30016. end;
  30017. procedure TTestModule.TestJSValue_OverloadNativeInt;
  30018. begin
  30019. StartProgram(false);
  30020. Add([
  30021. 'type',
  30022. ' integer = longint;',
  30023. ' int53 = nativeint;',
  30024. ' tdatetime = double;',
  30025. 'procedure DoIt(n: nativeint); begin end;',
  30026. 'procedure DoIt(v: jsvalue); begin end;',
  30027. 'var',
  30028. ' d: double;',
  30029. ' dt: tdatetime;',
  30030. ' i: integer;',
  30031. ' b: byte;',
  30032. ' shi: shortint;',
  30033. ' w: word;',
  30034. ' smi: smallint;',
  30035. ' lw: longword;',
  30036. ' li: longint;',
  30037. ' ni: nativeint;',
  30038. ' nu: nativeuint;',
  30039. 'begin',
  30040. ' DoIt(d);',
  30041. ' DoIt(dt);',
  30042. ' DoIt(i);',
  30043. ' DoIt(b);',
  30044. ' DoIt(shi);',
  30045. ' DoIt(w);',
  30046. ' DoIt(smi);',
  30047. ' DoIt(lw);',
  30048. ' DoIt(li);',
  30049. ' DoIt(ni);',
  30050. ' DoIt(nu);',
  30051. '']);
  30052. ConvertProgram;
  30053. CheckSource('TestJSValue_OverloadNativeInt',
  30054. LinesToStr([ // statements
  30055. 'this.DoIt = function (n) {',
  30056. '};',
  30057. 'this.DoIt$1 = function (v) {',
  30058. '};',
  30059. 'this.d = 0.0;',
  30060. 'this.dt = 0.0;',
  30061. 'this.i = 0;',
  30062. 'this.b = 0;',
  30063. 'this.shi = 0;',
  30064. 'this.w = 0;',
  30065. 'this.smi = 0;',
  30066. 'this.lw = 0;',
  30067. 'this.li = 0;',
  30068. 'this.ni = 0;',
  30069. 'this.nu = 0;',
  30070. '']),
  30071. LinesToStr([ // $mod.$main
  30072. '$mod.DoIt$1($mod.d);',
  30073. '$mod.DoIt$1($mod.dt);',
  30074. '$mod.DoIt($mod.i);',
  30075. '$mod.DoIt($mod.b);',
  30076. '$mod.DoIt($mod.shi);',
  30077. '$mod.DoIt($mod.w);',
  30078. '$mod.DoIt($mod.smi);',
  30079. '$mod.DoIt($mod.lw);',
  30080. '$mod.DoIt($mod.li);',
  30081. '$mod.DoIt($mod.ni);',
  30082. '$mod.DoIt($mod.nu);',
  30083. '']));
  30084. end;
  30085. procedure TTestModule.TestJSValue_OverloadWord;
  30086. begin
  30087. StartProgram(false);
  30088. Add([
  30089. 'type',
  30090. ' integer = longint;',
  30091. ' int53 = nativeint;',
  30092. ' tdatetime = double;',
  30093. 'procedure DoIt(w: word); begin end;',
  30094. 'procedure DoIt(v: jsvalue); begin end;',
  30095. 'var',
  30096. ' d: double;',
  30097. ' dt: tdatetime;',
  30098. ' i: integer;',
  30099. ' b: byte;',
  30100. ' shi: shortint;',
  30101. ' w: word;',
  30102. ' smi: smallint;',
  30103. ' lw: longword;',
  30104. ' li: longint;',
  30105. ' ni: nativeint;',
  30106. ' nu: nativeuint;',
  30107. 'begin',
  30108. ' DoIt(d);',
  30109. ' DoIt(dt);',
  30110. ' DoIt(i);',
  30111. ' DoIt(b);',
  30112. ' DoIt(shi);',
  30113. ' DoIt(w);',
  30114. ' DoIt(smi);',
  30115. ' DoIt(lw);',
  30116. ' DoIt(li);',
  30117. ' DoIt(ni);',
  30118. ' DoIt(nu);',
  30119. '']);
  30120. ConvertProgram;
  30121. CheckSource('TestJSValue_OverloadWord',
  30122. LinesToStr([ // statements
  30123. 'this.DoIt = function (w) {',
  30124. '};',
  30125. 'this.DoIt$1 = function (v) {',
  30126. '};',
  30127. 'this.d = 0.0;',
  30128. 'this.dt = 0.0;',
  30129. 'this.i = 0;',
  30130. 'this.b = 0;',
  30131. 'this.shi = 0;',
  30132. 'this.w = 0;',
  30133. 'this.smi = 0;',
  30134. 'this.lw = 0;',
  30135. 'this.li = 0;',
  30136. 'this.ni = 0;',
  30137. 'this.nu = 0;',
  30138. '']),
  30139. LinesToStr([ // $mod.$main
  30140. '$mod.DoIt$1($mod.d);',
  30141. '$mod.DoIt$1($mod.dt);',
  30142. '$mod.DoIt$1($mod.i);',
  30143. '$mod.DoIt($mod.b);',
  30144. '$mod.DoIt($mod.shi);',
  30145. '$mod.DoIt($mod.w);',
  30146. '$mod.DoIt$1($mod.smi);',
  30147. '$mod.DoIt$1($mod.lw);',
  30148. '$mod.DoIt$1($mod.li);',
  30149. '$mod.DoIt$1($mod.ni);',
  30150. '$mod.DoIt$1($mod.nu);',
  30151. '']));
  30152. end;
  30153. procedure TTestModule.TestJSValue_OverloadString;
  30154. begin
  30155. StartProgram(false);
  30156. Add([
  30157. 'type',
  30158. ' uni = string;',
  30159. ' WChar = AnsiChar;',
  30160. 'procedure DoIt(s: string); begin end;',
  30161. 'procedure DoIt(v: jsvalue); begin end;',
  30162. 'var',
  30163. ' s: string;',
  30164. ' c: AnsiChar;',
  30165. ' u: uni;',
  30166. 'begin',
  30167. ' DoIt(s);',
  30168. ' DoIt(c);',
  30169. ' DoIt(u);',
  30170. '']);
  30171. ConvertProgram;
  30172. CheckSource('TestJSValue_OverloadString',
  30173. LinesToStr([ // statements
  30174. 'this.DoIt = function (s) {',
  30175. '};',
  30176. 'this.DoIt$1 = function (v) {',
  30177. '};',
  30178. 'this.s = "";',
  30179. 'this.c = "";',
  30180. 'this.u = "";',
  30181. '']),
  30182. LinesToStr([ // $mod.$main
  30183. '$mod.DoIt($mod.s);',
  30184. '$mod.DoIt($mod.c);',
  30185. '$mod.DoIt($mod.u);',
  30186. '']));
  30187. end;
  30188. procedure TTestModule.TestJSValue_OverloadChar;
  30189. begin
  30190. StartProgram(false);
  30191. Add([
  30192. 'type',
  30193. ' uni = string;',
  30194. ' WChar = AnsiChar;',
  30195. 'procedure DoIt(c: AnsiChar); begin end;',
  30196. 'procedure DoIt(v: jsvalue); begin end;',
  30197. 'var',
  30198. ' s: string;',
  30199. ' c: AnsiChar;',
  30200. ' u: uni;',
  30201. 'begin',
  30202. ' DoIt(s);',
  30203. ' DoIt(c);',
  30204. ' DoIt(u);',
  30205. '']);
  30206. ConvertProgram;
  30207. CheckSource('TestJSValue_OverloadChar',
  30208. LinesToStr([ // statements
  30209. 'this.DoIt = function (c) {',
  30210. '};',
  30211. 'this.DoIt$1 = function (v) {',
  30212. '};',
  30213. 'this.s = "";',
  30214. 'this.c = "";',
  30215. 'this.u = "";',
  30216. '']),
  30217. LinesToStr([ // $mod.$main
  30218. '$mod.DoIt$1($mod.s);',
  30219. '$mod.DoIt($mod.c);',
  30220. '$mod.DoIt$1($mod.u);',
  30221. '']));
  30222. end;
  30223. procedure TTestModule.TestJSValue_OverloadPointer;
  30224. begin
  30225. StartProgram(false);
  30226. Add([
  30227. 'type',
  30228. ' TObject = class end;',
  30229. 'procedure DoIt(p: pointer); begin end;',
  30230. 'procedure DoIt(v: jsvalue); begin end;',
  30231. 'var',
  30232. ' o: TObject;',
  30233. 'begin',
  30234. ' DoIt(o);',
  30235. '']);
  30236. ConvertProgram;
  30237. CheckSource('TestJSValue_OverloadPointer',
  30238. LinesToStr([ // statements
  30239. 'rtl.createClass(this, "TObject", null, function () {',
  30240. ' this.$init = function () {',
  30241. ' };',
  30242. ' this.$final = function () {',
  30243. ' };',
  30244. '});',
  30245. 'this.DoIt = function (p) {',
  30246. '};',
  30247. 'this.DoIt$1 = function (v) {',
  30248. '};',
  30249. 'this.o = null;',
  30250. '']),
  30251. LinesToStr([ // $mod.$main
  30252. '$mod.DoIt($mod.o);',
  30253. '']));
  30254. end;
  30255. procedure TTestModule.TestJSValue_ForIn;
  30256. begin
  30257. StartProgram(false);
  30258. Add([
  30259. 'var',
  30260. ' v: JSValue;',
  30261. ' key: string;',
  30262. 'begin',
  30263. ' for key in v do begin',
  30264. ' if key=''abc'' then ;',
  30265. ' end;',
  30266. '']);
  30267. ConvertProgram;
  30268. CheckSource('TestJSValue_ForIn',
  30269. LinesToStr([ // statements
  30270. 'this.v = undefined;',
  30271. 'this.key = "";',
  30272. '']),
  30273. LinesToStr([ // $mod.$main
  30274. 'for ($mod.key in $mod.v) {',
  30275. ' if ($mod.key === "abc") ;',
  30276. '};',
  30277. '']));
  30278. end;
  30279. procedure TTestModule.TestRTTI_IntRange;
  30280. begin
  30281. WithTypeInfo:=true;
  30282. StartProgram(true,[supTypeInfo]);
  30283. Add([
  30284. '{$modeswitch externalclass}',
  30285. 'type',
  30286. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  30287. ' TColor = type TGraphicsColor;',
  30288. 'var',
  30289. ' p: TTypeInfo;',
  30290. ' k: TTypeKind;',
  30291. 'begin',
  30292. ' p:=typeinfo(TGraphicsColor);',
  30293. ' p:=typeinfo(TColor);',
  30294. ' k:=GetTypeKind(TGraphicsColor);',
  30295. ' k:=GetTypeKind(TColor);',
  30296. '']);
  30297. ConvertProgram;
  30298. CheckSource('TestRTTI_IntRange',
  30299. LinesToStr([ // statements
  30300. 'this.$rtti.$Int("TGraphicsColor", {',
  30301. ' minvalue: -2147483648,',
  30302. ' maxvalue: 2147483647,',
  30303. ' ordtype: 4',
  30304. '});',
  30305. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  30306. 'this.p = null;',
  30307. 'this.k = 0;',
  30308. '']),
  30309. LinesToStr([ // $mod.$main
  30310. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  30311. '$mod.p = $mod.$rtti["TColor"];',
  30312. '$mod.k = 1;',
  30313. '$mod.k = 1;',
  30314. '']));
  30315. end;
  30316. procedure TTestModule.TestRTTI_Double;
  30317. begin
  30318. WithTypeInfo:=true;
  30319. StartProgram(true,[supTypeInfo]);
  30320. Add([
  30321. '{$modeswitch externalclass}',
  30322. 'type',
  30323. ' TFloat = type double;',
  30324. 'var',
  30325. ' p: TTypeInfo;',
  30326. 'begin',
  30327. ' p:=typeinfo(double);',
  30328. ' p:=typeinfo(TFloat);',
  30329. '']);
  30330. ConvertProgram;
  30331. CheckSource('TestRTTI_Double',
  30332. LinesToStr([ // statements
  30333. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30334. 'this.p = null;',
  30335. '']),
  30336. LinesToStr([ // $mod.$main
  30337. '$mod.p = rtl.double;',
  30338. '$mod.p = $mod.$rtti["TFloat"];',
  30339. '']));
  30340. end;
  30341. procedure TTestModule.TestRTTI_ProcType;
  30342. begin
  30343. WithTypeInfo:=true;
  30344. StartProgram(false);
  30345. Add('type');
  30346. Add(' TProcA = procedure;');
  30347. Add(' TMethodB = procedure of object;');
  30348. Add(' TProcC = procedure; varargs;');
  30349. Add(' TProcD = procedure(i: longint; const j: string; var c: AnsiChar; out d: double);');
  30350. Add(' TProcE = function: nativeint;');
  30351. Add(' TProcF = function(const p: TProcA): nativeuint;');
  30352. Add('var p: pointer;');
  30353. Add('begin');
  30354. Add(' p:=typeinfo(tproca);');
  30355. ConvertProgram;
  30356. CheckSource('TestRTTI_ProcType',
  30357. LinesToStr([ // statements
  30358. 'this.$rtti.$ProcVar("TProcA", {',
  30359. ' procsig: rtl.newTIProcSig([])',
  30360. '});',
  30361. 'this.$rtti.$MethodVar("TMethodB", {',
  30362. ' procsig: rtl.newTIProcSig([]),',
  30363. ' methodkind: 0',
  30364. '});',
  30365. 'this.$rtti.$ProcVar("TProcC", {',
  30366. ' procsig: rtl.newTIProcSig([], null, 2)',
  30367. '});',
  30368. 'this.$rtti.$ProcVar("TProcD", {',
  30369. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.AnsiChar, 1], ["d", rtl.double, 4]])',
  30370. '});',
  30371. 'this.$rtti.$ProcVar("TProcE", {',
  30372. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30373. '});',
  30374. 'this.$rtti.$ProcVar("TProcF", {',
  30375. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  30376. '});',
  30377. 'this.p = null;',
  30378. '']),
  30379. LinesToStr([ // $mod.$main
  30380. '$mod.p = $mod.$rtti["TProcA"];',
  30381. '']));
  30382. end;
  30383. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  30384. begin
  30385. WithTypeInfo:=true;
  30386. AddModuleWithIntfImplSrc('unit2.pas',
  30387. LinesToStr([
  30388. 'type',
  30389. ' TObject = class end;'
  30390. ]),
  30391. '');
  30392. StartUnit(true);
  30393. Add('interface');
  30394. Add('uses unit2;');
  30395. Add('type');
  30396. Add(' TProcA = function(o: tobject): tobject;');
  30397. Add('implementation');
  30398. Add('type');
  30399. Add(' TProcB = function(o: tobject): tobject;');
  30400. Add('var p: Pointer;');
  30401. Add('initialization');
  30402. Add(' p:=typeinfo(tproca);');
  30403. Add(' p:=typeinfo(tprocb);');
  30404. ConvertUnit;
  30405. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30406. LinesToStr([ // statements
  30407. 'var $impl = $mod.$impl;',
  30408. 'this.$rtti.$ProcVar("TProcA", {',
  30409. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30410. '});',
  30411. '']),
  30412. LinesToStr([ // this.$init
  30413. '$impl.p = $mod.$rtti["TProcA"];',
  30414. '$impl.p = $mod.$rtti["TProcB"];',
  30415. '']),
  30416. LinesToStr([ // implementation
  30417. '$mod.$rtti.$ProcVar("TProcB", {',
  30418. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30419. '});',
  30420. '$impl.p = null;',
  30421. '']) );
  30422. end;
  30423. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30424. begin
  30425. WithTypeInfo:=true;
  30426. StartProgram(false);
  30427. Add(['var',
  30428. ' ProcA: procedure;',
  30429. ' MethodB: procedure of object;',
  30430. ' ProcC: procedure; varargs;',
  30431. ' ProcD: procedure(i: longint; const j: string; var c: AnsiChar; out d: double);',
  30432. ' ProcE: function: nativeint;',
  30433. ' p: pointer;',
  30434. 'begin',
  30435. ' p:=typeinfo(proca);']);
  30436. ConvertProgram;
  30437. CheckSource('TestRTTI_ProcTypeAnonymous',
  30438. LinesToStr([ // statements
  30439. 'this.$rtti.$ProcVar("ProcA$a", {',
  30440. ' procsig: rtl.newTIProcSig([])',
  30441. '});',
  30442. 'this.ProcA = null;',
  30443. 'this.$rtti.$MethodVar("MethodB$a", {',
  30444. ' procsig: rtl.newTIProcSig([]),',
  30445. ' methodkind: 0',
  30446. '});',
  30447. 'this.MethodB = null;',
  30448. 'this.$rtti.$ProcVar("ProcC$a", {',
  30449. ' procsig: rtl.newTIProcSig([], null, 2)',
  30450. '});',
  30451. 'this.ProcC = null;',
  30452. 'this.$rtti.$ProcVar("ProcD$a", {',
  30453. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.AnsiChar, 1], ["d", rtl.double, 4]])',
  30454. '});',
  30455. 'this.ProcD = null;',
  30456. 'this.$rtti.$ProcVar("ProcE$a", {',
  30457. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30458. '});',
  30459. 'this.ProcE = null;',
  30460. 'this.p = null;',
  30461. '']),
  30462. LinesToStr([ // $mod.$main
  30463. '$mod.p = $mod.$rtti["ProcA$a"];',
  30464. '']));
  30465. end;
  30466. procedure TTestModule.TestRTTI_EnumAndSetType;
  30467. begin
  30468. WithTypeInfo:=true;
  30469. StartProgram(false);
  30470. Add('type');
  30471. Add(' TFlag = (light,dark);');
  30472. Add(' TFlags = set of TFlag;');
  30473. Add(' TProc = function(f: TFlags): TFlag;');
  30474. Add('var p: pointer;');
  30475. Add('begin');
  30476. Add(' p:=typeinfo(tflag);');
  30477. Add(' p:=typeinfo(tflags);');
  30478. ConvertProgram;
  30479. CheckSource('TestRTTI_EnumAndType',
  30480. LinesToStr([ // statements
  30481. 'this.TFlag = {',
  30482. ' "0": "light",',
  30483. ' light: 0,',
  30484. ' "1": "dark",',
  30485. ' dark: 1',
  30486. '};',
  30487. 'this.$rtti.$Enum("TFlag", {',
  30488. ' minvalue: 0,',
  30489. ' maxvalue: 1,',
  30490. ' ordtype: 1,',
  30491. ' enumtype: this.TFlag',
  30492. '});',
  30493. 'this.$rtti.$Set("TFlags", {',
  30494. ' comptype: this.$rtti["TFlag"]',
  30495. '});',
  30496. 'this.$rtti.$ProcVar("TProc", {',
  30497. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30498. '});',
  30499. 'this.p = null;',
  30500. '']),
  30501. LinesToStr([ // $mod.$main
  30502. '$mod.p = $mod.$rtti["TFlag"];',
  30503. '$mod.p = $mod.$rtti["TFlags"];',
  30504. '']));
  30505. end;
  30506. procedure TTestModule.TestRTTI_EnumRange;
  30507. begin
  30508. WithTypeInfo:=true;
  30509. StartProgram(false);
  30510. Add([
  30511. 'type',
  30512. ' TCol = (red,green,blue);',
  30513. ' TColRg = green..blue;',
  30514. ' TSetOfColRg = set of TColRg;',
  30515. 'var p: pointer;',
  30516. 'begin',
  30517. ' p:=typeinfo(tcolrg);',
  30518. ' p:=typeinfo(tsetofcolrg);',
  30519. '']);
  30520. ConvertProgram;
  30521. end;
  30522. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30523. begin
  30524. WithTypeInfo:=true;
  30525. StartProgram(false);
  30526. Add('type');
  30527. Add(' TFlags = set of (red, green);');
  30528. Add('var');
  30529. Add(' f: TFlags;');
  30530. Add('begin');
  30531. Add(' Include(f,red);');
  30532. ConvertProgram;
  30533. CheckSource('TestRTTI_AnonymousEnumType',
  30534. LinesToStr([ // statements
  30535. 'this.TFlags$a = {',
  30536. ' "0": "red",',
  30537. ' red: 0,',
  30538. ' "1": "green",',
  30539. ' green: 1',
  30540. '};',
  30541. 'this.$rtti.$Enum("TFlags$a", {',
  30542. ' minvalue: 0,',
  30543. ' maxvalue: 1,',
  30544. ' ordtype: 1,',
  30545. ' enumtype: this.TFlags$a',
  30546. '});',
  30547. 'this.$rtti.$Set("TFlags", {',
  30548. ' comptype: this.$rtti["TFlags$a"]',
  30549. '});',
  30550. 'this.f = {};',
  30551. '']),
  30552. LinesToStr([
  30553. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30554. '']));
  30555. end;
  30556. procedure TTestModule.TestRTTI_StaticArray;
  30557. begin
  30558. WithTypeInfo:=true;
  30559. StartProgram(false);
  30560. Add('type');
  30561. Add(' TFlag = (light,dark);');
  30562. Add(' TFlagNames = array[TFlag] of string;');
  30563. Add(' TBoolNames = array[boolean] of string;');
  30564. Add(' TByteArray = array[1..32768] of byte;');
  30565. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30566. Add('var p: pointer;');
  30567. Add('begin');
  30568. Add(' p:=typeinfo(TFlagNames);');
  30569. Add(' p:=typeinfo(TBoolNames);');
  30570. ConvertProgram;
  30571. CheckSource('TestRTTI_StaticArray',
  30572. LinesToStr([ // statements
  30573. 'this.TFlag = {',
  30574. ' "0": "light",',
  30575. ' light: 0,',
  30576. ' "1": "dark",',
  30577. ' dark: 1',
  30578. '};',
  30579. 'this.$rtti.$Enum("TFlag", {',
  30580. ' minvalue: 0,',
  30581. ' maxvalue: 1,',
  30582. ' ordtype: 1,',
  30583. ' enumtype: this.TFlag',
  30584. '});',
  30585. 'this.$rtti.$StaticArray("TFlagNames", {',
  30586. ' dims: [2],',
  30587. ' eltype: rtl.string',
  30588. '});',
  30589. 'this.$rtti.$StaticArray("TBoolNames", {',
  30590. ' dims: [2],',
  30591. ' eltype: rtl.string',
  30592. '});',
  30593. 'this.$rtti.$StaticArray("TByteArray", {',
  30594. ' dims: [32768],',
  30595. ' eltype: rtl.byte',
  30596. '});',
  30597. 'this.$rtti.$ProcVar("TProc", {',
  30598. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30599. '});',
  30600. 'this.p = null;',
  30601. '']),
  30602. LinesToStr([ // $mod.$main
  30603. '$mod.p = $mod.$rtti["TFlagNames"];',
  30604. '$mod.p = $mod.$rtti["TBoolNames"];',
  30605. '']));
  30606. end;
  30607. procedure TTestModule.TestRTTI_DynArray;
  30608. begin
  30609. WithTypeInfo:=true;
  30610. StartProgram(false);
  30611. Add('type');
  30612. Add(' TArrStr = array of string;');
  30613. Add(' TArr2Dim = array of tarrstr;');
  30614. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30615. Add('var p: pointer;');
  30616. Add('begin');
  30617. Add(' p:=typeinfo(tarrstr);');
  30618. Add(' p:=typeinfo(tarr2dim);');
  30619. ConvertProgram;
  30620. CheckSource('TestRTTI_DynArray',
  30621. LinesToStr([ // statements
  30622. 'this.$rtti.$DynArray("TArrStr", {',
  30623. ' eltype: rtl.string',
  30624. '});',
  30625. 'this.$rtti.$DynArray("TArr2Dim", {',
  30626. ' eltype: this.$rtti["TArrStr"]',
  30627. '});',
  30628. 'this.$rtti.$ProcVar("TProc", {',
  30629. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30630. '});',
  30631. 'this.p = null;',
  30632. '']),
  30633. LinesToStr([ // $mod.$main
  30634. '$mod.p = $mod.$rtti["TArrStr"];',
  30635. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30636. '']));
  30637. end;
  30638. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30639. begin
  30640. WithTypeInfo:=true;
  30641. StartProgram(false);
  30642. Add('type');
  30643. Add(' TArr = array of array of longint;');
  30644. Add('var a: TArr;');
  30645. Add('begin');
  30646. ConvertProgram;
  30647. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30648. LinesToStr([ // statements
  30649. 'this.$rtti.$DynArray("TArr$a", {',
  30650. ' eltype: rtl.longint',
  30651. '});',
  30652. 'this.$rtti.$DynArray("TArr", {',
  30653. ' eltype: this.$rtti["TArr$a"]',
  30654. '});',
  30655. 'this.a = [];',
  30656. '']),
  30657. LinesToStr([ // $mod.$main
  30658. ]));
  30659. end;
  30660. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30661. begin
  30662. WithTypeInfo:=true;
  30663. StartProgram(false);
  30664. Add('type');
  30665. Add(' TObject = class');
  30666. Add(' published');
  30667. Add(' procedure Proc; virtual; abstract;');
  30668. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30669. Add(' end;');
  30670. Add('begin');
  30671. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30672. nDuplicatePublishedMethodXAtY);
  30673. ConvertProgram;
  30674. end;
  30675. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30676. begin
  30677. WithTypeInfo:=true;
  30678. StartUnit(false);
  30679. Add([
  30680. 'interface',
  30681. 'type',
  30682. ' TObject = class',
  30683. ' end;',
  30684. ' {$M+}',
  30685. ' TBird = class',
  30686. ' procedure Fly;',
  30687. ' end;',
  30688. ' {$M-}',
  30689. 'type',
  30690. ' TEagle = class(TBird)',
  30691. ' procedure Fly;',
  30692. ' end;',
  30693. 'implementation',
  30694. 'procedure TBird.Fly;',
  30695. 'begin',
  30696. 'end;',
  30697. 'procedure TEagle.Fly;',
  30698. 'begin',
  30699. 'end;',
  30700. '']);
  30701. ConvertUnit;
  30702. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30703. LinesToStr([ // statements
  30704. 'rtl.createClass(this, "TObject", null, function () {',
  30705. ' this.$init = function () {',
  30706. ' };',
  30707. ' this.$final = function () {',
  30708. ' };',
  30709. '});',
  30710. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30711. ' this.Fly = function () {',
  30712. ' };',
  30713. ' var $r = this.$rtti;',
  30714. ' $r.addMethod("Fly", 0, []);',
  30715. '});',
  30716. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30717. ' this.Fly = function () {',
  30718. ' };',
  30719. ' var $r = this.$rtti;',
  30720. ' $r.addMethod("Fly", 0, []);',
  30721. '});',
  30722. '']),
  30723. LinesToStr([ // $mod.$main
  30724. ]));
  30725. CheckResolverUnexpectedHints(true);
  30726. end;
  30727. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30728. begin
  30729. WithTypeInfo:=true;
  30730. StartProgram(false);
  30731. Add('type');
  30732. Add(' TObject = class');
  30733. Add(' published');
  30734. Add(' procedure Proc; external name ''foo'';');
  30735. Add(' end;');
  30736. Add('begin');
  30737. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30738. nPublishedNameMustMatchExternal);
  30739. ConvertProgram;
  30740. end;
  30741. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30742. begin
  30743. WithTypeInfo:=true;
  30744. StartProgram(false);
  30745. Add('type');
  30746. Add(' TObject = class');
  30747. Add(' class var FA: longint;');
  30748. Add(' published');
  30749. Add(' class property A: longint read FA;');
  30750. Add(' end;');
  30751. Add('begin');
  30752. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30753. nInvalidXModifierY);
  30754. ConvertProgram;
  30755. end;
  30756. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30757. begin
  30758. WithTypeInfo:=true;
  30759. StartProgram(false);
  30760. Add('type');
  30761. Add(' TObject = class');
  30762. Add(' published');
  30763. Add(' class var FA: longint;');
  30764. Add(' end;');
  30765. Add('begin');
  30766. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30767. nSymbolCannotBePublished);
  30768. ConvertProgram;
  30769. end;
  30770. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30771. begin
  30772. WithTypeInfo:=true;
  30773. StartProgram(false);
  30774. Add('{$modeswitch externalclass}');
  30775. Add('type');
  30776. Add(' TObject = class');
  30777. Add(' published');
  30778. Add(' V: longint; external name ''foo'';');
  30779. Add(' end;');
  30780. Add('begin');
  30781. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30782. nPublishedNameMustMatchExternal);
  30783. ConvertProgram;
  30784. end;
  30785. procedure TTestModule.TestRTTI_Class_Field;
  30786. begin
  30787. WithTypeInfo:=true;
  30788. StartProgram(false);
  30789. Add('{$modeswitch externalclass}');
  30790. Add('type');
  30791. Add(' TObject = class');
  30792. Add(' private');
  30793. Add(' FPropA: string;');
  30794. Add(' published');
  30795. Add(' VarLI: longint;');
  30796. Add(' VarC: AnsiChar;');
  30797. Add(' VarS: string;');
  30798. Add(' VarD: double;');
  30799. Add(' VarB: boolean;');
  30800. Add(' VarLW: longword;');
  30801. Add(' VarSmI: smallint;');
  30802. Add(' VarW: word;');
  30803. Add(' VarShI: shortint;');
  30804. Add(' VarBy: byte;');
  30805. Add(' VarExt: longint external name ''VarExt'';');
  30806. Add(' ArrA, ArrB: array of byte;');
  30807. Add(' end;');
  30808. Add('var p: pointer;');
  30809. Add(' Obj: tobject;');
  30810. Add('begin');
  30811. Add(' p:=typeinfo(tobject);');
  30812. Add(' p:=typeinfo(p);');
  30813. Add(' p:=typeinfo(obj);');
  30814. ConvertProgram;
  30815. CheckSource('TestRTTI_Class_Field',
  30816. LinesToStr([ // statements
  30817. 'rtl.createClass(this, "TObject", null, function () {',
  30818. ' this.$init = function () {',
  30819. ' this.FPropA = "";',
  30820. ' this.VarLI = 0;',
  30821. ' this.VarC = "";',
  30822. ' this.VarS = "";',
  30823. ' this.VarD = 0.0;',
  30824. ' this.VarB = false;',
  30825. ' this.VarLW = 0;',
  30826. ' this.VarSmI = 0;',
  30827. ' this.VarW = 0;',
  30828. ' this.VarShI = 0;',
  30829. ' this.VarBy = 0;',
  30830. ' this.ArrA = [];',
  30831. ' this.ArrB = [];',
  30832. ' };',
  30833. ' this.$final = function () {',
  30834. ' this.ArrA = undefined;',
  30835. ' this.ArrB = undefined;',
  30836. ' };',
  30837. ' var $r = this.$rtti;',
  30838. ' $r.addField("VarLI", rtl.longint);',
  30839. ' $r.addField("VarC", rtl.AnsiChar);',
  30840. ' $r.addField("VarS", rtl.string);',
  30841. ' $r.addField("VarD", rtl.double);',
  30842. ' $r.addField("VarB", rtl.boolean);',
  30843. ' $r.addField("VarLW", rtl.longword);',
  30844. ' $r.addField("VarSmI", rtl.smallint);',
  30845. ' $r.addField("VarW", rtl.word);',
  30846. ' $r.addField("VarShI", rtl.shortint);',
  30847. ' $r.addField("VarBy", rtl.byte);',
  30848. ' $r.addField("VarExt", rtl.longint);',
  30849. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30850. ' eltype: rtl.byte',
  30851. ' });',
  30852. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30853. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30854. '});',
  30855. 'this.p = null;',
  30856. 'this.Obj = null;',
  30857. '']),
  30858. LinesToStr([ // $mod.$main
  30859. '$mod.p = $mod.$rtti["TObject"];',
  30860. '$mod.p = rtl.pointer;',
  30861. '$mod.p = $mod.Obj.$rtti;',
  30862. '']));
  30863. end;
  30864. procedure TTestModule.TestRTTI_Class_Method;
  30865. begin
  30866. WithTypeInfo:=true;
  30867. StartProgram(false);
  30868. Add([
  30869. 'type',
  30870. ' TObject = class',
  30871. ' private',
  30872. ' procedure Internal; external name ''$intern'';',
  30873. ' published',
  30874. ' procedure Click; virtual; abstract;',
  30875. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30876. ' function GetNotify: boolean; external name ''GetNotify'';',
  30877. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30878. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30879. ' end;',
  30880. 'begin']);
  30881. ConvertProgram;
  30882. CheckSource('TestRTTI_Class_Method',
  30883. LinesToStr([ // statements
  30884. 'rtl.createClass(this, "TObject", null, function () {',
  30885. ' this.$init = function () {',
  30886. ' };',
  30887. ' this.$final = function () {',
  30888. ' };',
  30889. ' var $r = this.$rtti;',
  30890. ' $r.addMethod("Click", 0, []);',
  30891. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30892. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30893. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30894. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30895. '});',
  30896. '']),
  30897. LinesToStr([ // $mod.$main
  30898. '']));
  30899. end;
  30900. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30901. begin
  30902. WithTypeInfo:=true;
  30903. StartProgram(false);
  30904. Add('type');
  30905. Add(' TObject = class');
  30906. Add(' published');
  30907. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30908. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30909. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30910. Add(' end;');
  30911. Add('begin');
  30912. ConvertProgram;
  30913. CheckSource('TestRTTI_Class_MethodOpenArray',
  30914. LinesToStr([ // statements
  30915. 'rtl.createClass(this, "TObject", null, function () {',
  30916. ' this.$init = function () {',
  30917. ' };',
  30918. ' this.$final = function () {',
  30919. ' };',
  30920. ' var $r = this.$rtti;',
  30921. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30922. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30923. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30924. '});',
  30925. '']),
  30926. LinesToStr([ // $mod.$main
  30927. '']));
  30928. end;
  30929. procedure TTestModule.TestRTTI_Class_Property;
  30930. begin
  30931. WithTypeInfo:=true;
  30932. StartProgram(false);
  30933. Add('{$modeswitch externalclass}');
  30934. Add('type');
  30935. Add(' TObject = class');
  30936. Add(' private');
  30937. Add(' FColor: longint;');
  30938. Add(' FColorStored: boolean;');
  30939. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30940. Add(' function GetColor: longint; virtual; abstract;');
  30941. Add(' function GetColorStored: boolean; virtual; abstract;');
  30942. Add(' FExtSize: longint external name ''$extSize'';');
  30943. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30944. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30945. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30946. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30947. Add(' published');
  30948. Add(' property ColorA: longint read FColor;');
  30949. Add(' property ColorB: longint write FColor;');
  30950. Add(' property ColorC: longint read GetColor write SetColor;');
  30951. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  30952. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  30953. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  30954. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  30955. Add(' end;');
  30956. Add('begin');
  30957. ConvertProgram;
  30958. CheckSource('TestRTTI_Class_Property',
  30959. LinesToStr([ // statements
  30960. 'rtl.createClass(this, "TObject", null, function () {',
  30961. ' this.$init = function () {',
  30962. ' this.FColor = 0;',
  30963. ' this.FColorStored = false;',
  30964. ' };',
  30965. ' this.$final = function () {',
  30966. ' };',
  30967. ' var $r = this.$rtti;',
  30968. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  30969. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  30970. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  30971. ' $r.addProperty(',
  30972. ' "ColorD",',
  30973. ' 8,',
  30974. ' rtl.longint,',
  30975. ' "FColor",',
  30976. ' "FColor",',
  30977. ' {',
  30978. ' stored: "FColorStored"',
  30979. ' }',
  30980. ' );',
  30981. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  30982. ' $r.addProperty(',
  30983. ' "ExtSizeB",',
  30984. ' 11,',
  30985. ' rtl.longint,',
  30986. ' "$getSize",',
  30987. ' "$setSize",',
  30988. ' {',
  30989. ' stored: "$extSizeStored"',
  30990. ' }',
  30991. ' );',
  30992. ' $r.addProperty(',
  30993. ' "ExtSizeC",',
  30994. ' 12,',
  30995. ' rtl.longint,',
  30996. ' "$extSize",',
  30997. ' "$extSize",',
  30998. ' {',
  30999. ' stored: "$getExtSizeStored"',
  31000. ' }',
  31001. ' );',
  31002. '});',
  31003. '']),
  31004. LinesToStr([ // $mod.$main
  31005. '']));
  31006. end;
  31007. procedure TTestModule.TestRTTI_Class_PropertyParams;
  31008. begin
  31009. WithTypeInfo:=true;
  31010. StartProgram(false);
  31011. Add('{$modeswitch externalclass}');
  31012. Add('type');
  31013. Add(' integer = longint;');
  31014. Add(' TObject = class');
  31015. Add(' private');
  31016. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  31017. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  31018. Add(' function GetValues(const i: integer; var b: boolean): AnsiChar; virtual; abstract;');
  31019. Add(' procedure SetValues(const i: integer; var b: boolean; value: AnsiChar); virtual; abstract;');
  31020. Add(' published');
  31021. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  31022. Add(' property Values[const keya: integer; var keyb: boolean]: AnsiChar read getvalues write setvalues;');
  31023. Add(' end;');
  31024. Add('begin');
  31025. ConvertProgram;
  31026. CheckSource('TestRTTI_Class_PropertyParams',
  31027. LinesToStr([ // statements
  31028. 'rtl.createClass(this, "TObject", null, function () {',
  31029. ' this.$init = function () {',
  31030. ' };',
  31031. ' this.$final = function () {',
  31032. ' };',
  31033. ' var $r = this.$rtti;',
  31034. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  31035. ' $r.addProperty("Values", 3, rtl.AnsiChar, "GetValues", "SetValues");',
  31036. '});',
  31037. '']),
  31038. LinesToStr([ // $mod.$main
  31039. '']));
  31040. end;
  31041. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  31042. begin
  31043. WithTypeInfo:=true;
  31044. AddModuleWithIntfImplSrc('unit1.pas',
  31045. 'type TColor = -5..5;',
  31046. '');
  31047. StartProgram(true);
  31048. Add([
  31049. 'uses unit1;',
  31050. 'type',
  31051. ' TColorAlias = TColor;',
  31052. ' TColorTypeAlias = type TColor;',
  31053. ' TObject = class',
  31054. ' private',
  31055. ' fColor: TColor;',
  31056. ' fAlias: TColorAlias;',
  31057. ' fTypeAlias: TColorTypeAlias;',
  31058. ' published',
  31059. ' property Color: TColor read fcolor;',
  31060. ' property Alias: TColorAlias read falias;',
  31061. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  31062. ' end;',
  31063. 'begin',
  31064. '']);
  31065. ConvertProgram;
  31066. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  31067. LinesToStr([ // statements
  31068. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  31069. 'rtl.createClass(this, "TObject", null, function () {',
  31070. ' this.$init = function () {',
  31071. ' this.fColor = 0;',
  31072. ' this.fAlias = 0;',
  31073. ' this.fTypeAlias = 0;',
  31074. ' };',
  31075. ' this.$final = function () {',
  31076. ' };',
  31077. ' var $r = this.$rtti;',
  31078. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  31079. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  31080. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  31081. '});',
  31082. '']),
  31083. LinesToStr([ // $mod.$main
  31084. '']));
  31085. end;
  31086. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  31087. begin
  31088. WithTypeInfo:=true;
  31089. StartProgram(false);
  31090. Add([
  31091. '{$modeswitch omitrtti}',
  31092. 'type',
  31093. ' TObject = class',
  31094. ' private',
  31095. ' FA: byte;',
  31096. ' published',
  31097. ' property A: byte read FA write FA;',
  31098. ' end;',
  31099. 'begin']);
  31100. ConvertProgram;
  31101. CheckSource('TestRTTI_Class_OmitRTTI',
  31102. LinesToStr([ // statements
  31103. 'rtl.createClass(this, "TObject", null, function () {',
  31104. ' this.$init = function () {',
  31105. ' this.FA = 0;',
  31106. ' };',
  31107. ' this.$final = function () {',
  31108. ' };',
  31109. '});',
  31110. '']),
  31111. LinesToStr([ // $mod.$main
  31112. '']));
  31113. end;
  31114. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  31115. begin
  31116. WithTypeInfo:=true;
  31117. StartUnit(true,[supTObject]);
  31118. Add([
  31119. 'interface',
  31120. 'type',
  31121. ' {$M+}',
  31122. ' TBird = class',
  31123. ' published',
  31124. ' Swarm: array of TBird;',
  31125. ' end;',
  31126. 'implementation',
  31127. '']);
  31128. ConvertUnit;
  31129. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  31130. LinesToStr([ // statements
  31131. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  31132. ' this.$init = function () {',
  31133. ' pas.system.TObject.$init.call(this);',
  31134. ' this.Swarm = [];',
  31135. ' };',
  31136. ' this.$final = function () {',
  31137. ' this.Swarm = undefined;',
  31138. ' pas.system.TObject.$final.call(this);',
  31139. ' };',
  31140. ' var $r = this.$rtti;',
  31141. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  31142. ' eltype: $r',
  31143. ' });',
  31144. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  31145. '});',
  31146. '']),
  31147. LinesToStr([ // $mod.$main
  31148. '']));
  31149. end;
  31150. procedure TTestModule.TestRTTI_IndexModifier;
  31151. begin
  31152. WithTypeInfo:=true;
  31153. StartProgram(false);
  31154. Add([
  31155. 'type',
  31156. ' TEnum = (red, blue);',
  31157. ' TObject = class',
  31158. ' FB: boolean;',
  31159. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  31160. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  31161. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  31162. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  31163. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  31164. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  31165. ' published',
  31166. ' property B1: boolean index 1 read FB write SetIntBool;',
  31167. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  31168. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  31169. ' end;',
  31170. 'begin']);
  31171. ConvertProgram;
  31172. CheckSource('TestRTTI_IndexModifier',
  31173. LinesToStr([ // statements
  31174. 'this.TEnum = {',
  31175. ' "0": "red",',
  31176. ' red: 0,',
  31177. ' "1": "blue",',
  31178. ' blue: 1',
  31179. '};',
  31180. 'this.$rtti.$Enum("TEnum", {',
  31181. ' minvalue: 0,',
  31182. ' maxvalue: 1,',
  31183. ' ordtype: 1,',
  31184. ' enumtype: this.TEnum',
  31185. '});',
  31186. 'rtl.createClass(this, "TObject", null, function () {',
  31187. ' this.$init = function () {',
  31188. ' this.FB = false;',
  31189. ' };',
  31190. ' this.$final = function () {',
  31191. ' };',
  31192. ' var $r = this.$rtti;',
  31193. ' $r.addProperty(',
  31194. ' "B1",',
  31195. ' 18,',
  31196. ' rtl.boolean,',
  31197. ' "FB",',
  31198. ' "SetIntBool",',
  31199. ' {',
  31200. ' index: 1',
  31201. ' }',
  31202. ' );',
  31203. ' $r.addProperty(',
  31204. ' "B2",',
  31205. ' 17,',
  31206. ' rtl.boolean,',
  31207. ' "GetEnumBool",',
  31208. ' "FB",',
  31209. ' {',
  31210. ' index: $mod.TEnum.blue',
  31211. ' }',
  31212. ' );',
  31213. ' $r.addProperty(',
  31214. ' "I1",',
  31215. ' 19,',
  31216. ' rtl.boolean,',
  31217. ' "GetStrIntBool",',
  31218. ' "SetStrIntBool",',
  31219. ' {',
  31220. ' index: 2',
  31221. ' }',
  31222. ' );',
  31223. '});',
  31224. '']),
  31225. LinesToStr([ // $mod.$main
  31226. '']));
  31227. end;
  31228. procedure TTestModule.TestRTTI_StoredModifier;
  31229. begin
  31230. WithTypeInfo:=true;
  31231. StartProgram(false);
  31232. Add([
  31233. 'const',
  31234. ' ConstB = true;',
  31235. 'type',
  31236. ' TObject = class',
  31237. ' private',
  31238. ' FB: boolean;',
  31239. ' function IsBStored: boolean; virtual; abstract;',
  31240. ' published',
  31241. ' property BoolA: boolean read FB stored true;',
  31242. ' property BoolB: boolean read FB stored false;',
  31243. ' property BoolC: boolean read FB stored FB;',
  31244. ' property BoolD: boolean read FB stored ConstB;',
  31245. ' property BoolE: boolean read FB stored IsBStored;',
  31246. ' end;',
  31247. 'begin']);
  31248. ConvertProgram;
  31249. CheckSource('TestRTTI_StoredModifier',
  31250. LinesToStr([ // statements
  31251. 'this.ConstB = true;',
  31252. 'rtl.createClass(this, "TObject", null, function () {',
  31253. ' this.$init = function () {',
  31254. ' this.FB = false;',
  31255. ' };',
  31256. ' this.$final = function () {',
  31257. ' };',
  31258. ' var $r = this.$rtti;',
  31259. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  31260. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  31261. ' $r.addProperty(',
  31262. ' "BoolC",',
  31263. ' 8,',
  31264. ' rtl.boolean,',
  31265. ' "FB",',
  31266. ' "",',
  31267. ' {',
  31268. ' stored: "FB"',
  31269. ' }',
  31270. ' );',
  31271. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  31272. ' $r.addProperty(',
  31273. ' "BoolE",',
  31274. ' 12,',
  31275. ' rtl.boolean,',
  31276. ' "FB",',
  31277. ' "",',
  31278. ' {',
  31279. ' stored: "IsBStored"',
  31280. ' }',
  31281. ' );',
  31282. '});',
  31283. '']),
  31284. LinesToStr([ // $mod.$main
  31285. '']));
  31286. end;
  31287. procedure TTestModule.TestRTTI_DefaultValue;
  31288. begin
  31289. WithTypeInfo:=true;
  31290. StartProgram(false);
  31291. Add([
  31292. 'type',
  31293. ' TEnum = (red, blue);',
  31294. 'const',
  31295. ' CB = true or false;',
  31296. ' CI = 1+2;',
  31297. 'type',
  31298. ' TObject = class',
  31299. ' FB: boolean;',
  31300. ' FI: longint;',
  31301. ' FE: TEnum;',
  31302. ' published',
  31303. ' property B1: boolean read FB default true;',
  31304. ' property B2: boolean read FB default CB;',
  31305. ' property B3: boolean read FB default test1.cb;',
  31306. ' property I1: longint read FI default 2;',
  31307. ' property I2: longint read FI default CI;',
  31308. ' property E1: TEnum read FE default red;',
  31309. ' property E2: TEnum read FE default TEnum.blue;',
  31310. ' end;',
  31311. 'begin']);
  31312. ConvertProgram;
  31313. CheckSource('TestRTTI_DefaultValue',
  31314. LinesToStr([ // statements
  31315. 'this.TEnum = {',
  31316. ' "0": "red",',
  31317. ' red: 0,',
  31318. ' "1": "blue",',
  31319. ' blue: 1',
  31320. '};',
  31321. 'this.$rtti.$Enum("TEnum", {',
  31322. ' minvalue: 0,',
  31323. ' maxvalue: 1,',
  31324. ' ordtype: 1,',
  31325. ' enumtype: this.TEnum',
  31326. '});',
  31327. 'this.CB = true || false;',
  31328. 'this.CI = 1 + 2;',
  31329. 'rtl.createClass(this, "TObject", null, function () {',
  31330. ' this.$init = function () {',
  31331. ' this.FB = false;',
  31332. ' this.FI = 0;',
  31333. ' this.FE = 0;',
  31334. ' };',
  31335. ' this.$final = function () {',
  31336. ' };',
  31337. ' var $r = this.$rtti;',
  31338. ' $r.addProperty(',
  31339. ' "B1",',
  31340. ' 0,',
  31341. ' rtl.boolean,',
  31342. ' "FB",',
  31343. ' "",',
  31344. ' {',
  31345. ' Default: true',
  31346. ' }',
  31347. ' );',
  31348. ' $r.addProperty(',
  31349. ' "B2",',
  31350. ' 0,',
  31351. ' rtl.boolean,',
  31352. ' "FB",',
  31353. ' "",',
  31354. ' {',
  31355. ' Default: true',
  31356. ' }',
  31357. ' );',
  31358. ' $r.addProperty(',
  31359. ' "B3",',
  31360. ' 0,',
  31361. ' rtl.boolean,',
  31362. ' "FB",',
  31363. ' "",',
  31364. ' {',
  31365. ' Default: true',
  31366. ' }',
  31367. ' );',
  31368. ' $r.addProperty(',
  31369. ' "I1",',
  31370. ' 0,',
  31371. ' rtl.longint,',
  31372. ' "FI",',
  31373. ' "",',
  31374. ' {',
  31375. ' Default: 2',
  31376. ' }',
  31377. ' );',
  31378. ' $r.addProperty(',
  31379. ' "I2",',
  31380. ' 0,',
  31381. ' rtl.longint,',
  31382. ' "FI",',
  31383. ' "",',
  31384. ' {',
  31385. ' Default: 3',
  31386. ' }',
  31387. ' );',
  31388. ' $r.addProperty(',
  31389. ' "E1",',
  31390. ' 0,',
  31391. ' $mod.$rtti["TEnum"],',
  31392. ' "FE",',
  31393. ' "",',
  31394. ' {',
  31395. ' Default: $mod.TEnum.red',
  31396. ' }',
  31397. ' );',
  31398. ' $r.addProperty(',
  31399. ' "E2",',
  31400. ' 0,',
  31401. ' $mod.$rtti["TEnum"],',
  31402. ' "FE",',
  31403. ' "",',
  31404. ' {',
  31405. ' Default: $mod.TEnum.blue',
  31406. ' }',
  31407. ' );',
  31408. '});',
  31409. '']),
  31410. LinesToStr([ // $mod.$main
  31411. '']));
  31412. end;
  31413. procedure TTestModule.TestRTTI_DefaultValueSet;
  31414. begin
  31415. WithTypeInfo:=true;
  31416. StartProgram(false);
  31417. Add([
  31418. 'type',
  31419. ' TEnum = (red, blue);',
  31420. ' TSet = set of TEnum;',
  31421. 'const',
  31422. ' CSet = [red,blue];',
  31423. 'type',
  31424. ' TObject = class',
  31425. ' FSet: TSet;',
  31426. ' published',
  31427. ' property Set1: TSet read FSet default [];',
  31428. ' property Set2: TSet read FSet default [red];',
  31429. ' property Set3: TSet read FSet default [red,blue];',
  31430. ' property Set4: TSet read FSet default CSet;',
  31431. ' end;',
  31432. 'begin']);
  31433. ConvertProgram;
  31434. CheckSource('TestRTTI_DefaultValueSet',
  31435. LinesToStr([ // statements
  31436. 'this.TEnum = {',
  31437. ' "0": "red",',
  31438. ' red: 0,',
  31439. ' "1": "blue",',
  31440. ' blue: 1',
  31441. '};',
  31442. 'this.$rtti.$Enum("TEnum", {',
  31443. ' minvalue: 0,',
  31444. ' maxvalue: 1,',
  31445. ' ordtype: 1,',
  31446. ' enumtype: this.TEnum',
  31447. '});',
  31448. 'this.$rtti.$Set("TSet", {',
  31449. ' comptype: this.$rtti["TEnum"]',
  31450. '});',
  31451. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31452. 'rtl.createClass(this, "TObject", null, function () {',
  31453. ' this.$init = function () {',
  31454. ' this.FSet = {};',
  31455. ' };',
  31456. ' this.$final = function () {',
  31457. ' this.FSet = undefined;',
  31458. ' };',
  31459. ' var $r = this.$rtti;',
  31460. ' $r.addProperty(',
  31461. ' "Set1",',
  31462. ' 0,',
  31463. ' $mod.$rtti["TSet"],',
  31464. ' "FSet",',
  31465. ' "",',
  31466. ' {',
  31467. ' Default: {}',
  31468. ' }',
  31469. ' );',
  31470. ' $r.addProperty(',
  31471. ' "Set2",',
  31472. ' 0,',
  31473. ' $mod.$rtti["TSet"],',
  31474. ' "FSet",',
  31475. ' "",',
  31476. ' {',
  31477. ' Default: rtl.createSet($mod.TEnum.red)',
  31478. ' }',
  31479. ' );',
  31480. ' $r.addProperty(',
  31481. ' "Set3",',
  31482. ' 0,',
  31483. ' $mod.$rtti["TSet"],',
  31484. ' "FSet",',
  31485. ' "",',
  31486. ' {',
  31487. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31488. ' }',
  31489. ' );',
  31490. ' $r.addProperty(',
  31491. ' "Set4",',
  31492. ' 0,',
  31493. ' $mod.$rtti["TSet"],',
  31494. ' "FSet",',
  31495. ' "",',
  31496. ' {',
  31497. ' Default: $mod.CSet',
  31498. ' }',
  31499. ' );',
  31500. '});',
  31501. '']),
  31502. LinesToStr([ // $mod.$main
  31503. '']));
  31504. end;
  31505. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31506. begin
  31507. WithTypeInfo:=true;
  31508. StartProgram(false);
  31509. Add([
  31510. 'type',
  31511. ' TRg = -1..1;',
  31512. 'const',
  31513. ' l = low(TRg);',
  31514. ' h = high(TRg);',
  31515. 'type',
  31516. ' TObject = class',
  31517. ' FV: TRg;',
  31518. ' published',
  31519. ' property V1: TRg read FV default -1;',
  31520. ' end;',
  31521. 'begin']);
  31522. ConvertProgram;
  31523. CheckSource('TestRTTI_DefaultValueRangeType',
  31524. LinesToStr([ // statements
  31525. 'this.$rtti.$Int("TRg", {',
  31526. ' minvalue: -1,',
  31527. ' maxvalue: 1,',
  31528. ' ordtype: 0',
  31529. '});',
  31530. 'this.l = -1;',
  31531. 'this.h = 1;',
  31532. 'rtl.createClass(this, "TObject", null, function () {',
  31533. ' this.$init = function () {',
  31534. ' this.FV = 0;',
  31535. ' };',
  31536. ' this.$final = function () {',
  31537. ' };',
  31538. ' var $r = this.$rtti;',
  31539. ' $r.addProperty(',
  31540. ' "V1",',
  31541. ' 0,',
  31542. ' $mod.$rtti["TRg"],',
  31543. ' "FV",',
  31544. ' "",',
  31545. ' {',
  31546. ' Default: -1',
  31547. ' }',
  31548. ' );',
  31549. '});',
  31550. '']),
  31551. LinesToStr([ // $mod.$main
  31552. '']));
  31553. end;
  31554. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31555. begin
  31556. WithTypeInfo:=true;
  31557. StartProgram(false);
  31558. Add([
  31559. 'type',
  31560. ' TObject = class',
  31561. ' FA, FB: byte;',
  31562. ' property A: byte read FA default 1;',
  31563. ' property B: byte read FB default 2;',
  31564. ' end;',
  31565. ' TBird = class',
  31566. ' published',
  31567. ' property A;',
  31568. ' property B nodefault;',
  31569. ' end;',
  31570. 'begin']);
  31571. ConvertProgram;
  31572. CheckSource('TestRTTI_DefaultValueInherit',
  31573. LinesToStr([ // statements
  31574. 'rtl.createClass(this, "TObject", null, function () {',
  31575. ' this.$init = function () {',
  31576. ' this.FA = 0;',
  31577. ' this.FB = 0;',
  31578. ' };',
  31579. ' this.$final = function () {',
  31580. ' };',
  31581. '});',
  31582. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31583. ' var $r = this.$rtti;',
  31584. ' $r.addProperty(',
  31585. ' "A",',
  31586. ' 0,',
  31587. ' rtl.byte,',
  31588. ' "FA",',
  31589. ' "",',
  31590. ' {',
  31591. ' Default: 1',
  31592. ' }',
  31593. ' );',
  31594. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31595. '});',
  31596. '']),
  31597. LinesToStr([ // $mod.$main
  31598. '']));
  31599. end;
  31600. procedure TTestModule.TestRTTI_OverrideMethod;
  31601. begin
  31602. WithTypeInfo:=true;
  31603. StartProgram(false);
  31604. Add('type');
  31605. Add(' TObject = class');
  31606. Add(' published');
  31607. Add(' procedure DoIt; virtual; abstract;');
  31608. Add(' end;');
  31609. Add(' TSky = class');
  31610. Add(' published');
  31611. Add(' procedure DoIt; override;');
  31612. Add(' end;');
  31613. Add('procedure TSky.DoIt; begin end;');
  31614. Add('begin');
  31615. ConvertProgram;
  31616. CheckSource('TestRTTI_OverrideMethod',
  31617. LinesToStr([ // statements
  31618. 'rtl.createClass(this, "TObject", null, function () {',
  31619. ' this.$init = function () {',
  31620. ' };',
  31621. ' this.$final = function () {',
  31622. ' };',
  31623. ' var $r = this.$rtti;',
  31624. ' $r.addMethod("DoIt", 0, []);',
  31625. '});',
  31626. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31627. ' this.DoIt = function () {',
  31628. ' };',
  31629. '});',
  31630. '']),
  31631. LinesToStr([ // $mod.$main
  31632. '']));
  31633. end;
  31634. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31635. begin
  31636. WithTypeInfo:=true;
  31637. StartProgram(false);
  31638. Add([
  31639. 'type',
  31640. ' TObject = class',
  31641. ' published',
  31642. ' procedure DoIt;',
  31643. ' end;',
  31644. ' TSky = class',
  31645. ' published',
  31646. ' procedure DoIt; reintroduce;',
  31647. ' end;',
  31648. 'procedure TObject.DoIt; begin end;',
  31649. 'procedure TSky.DoIt;',
  31650. 'begin',
  31651. ' inherited DoIt;',
  31652. 'end;',
  31653. 'begin']);
  31654. ConvertProgram;
  31655. CheckSource('TestRTTI_ReintroduceMethod',
  31656. LinesToStr([ // statements
  31657. 'rtl.createClass(this, "TObject", null, function () {',
  31658. ' this.$init = function () {',
  31659. ' };',
  31660. ' this.$final = function () {',
  31661. ' };',
  31662. ' this.DoIt = function () {',
  31663. ' };',
  31664. ' var $r = this.$rtti;',
  31665. ' $r.addMethod("DoIt", 0, []);',
  31666. '});',
  31667. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31668. ' this.DoIt = function () {',
  31669. ' $mod.TObject.DoIt.call(this);',
  31670. ' };',
  31671. ' var $r = this.$rtti;',
  31672. ' $r.addMethod("DoIt", 0, []);',
  31673. '});',
  31674. '']),
  31675. LinesToStr([ // $mod.$main
  31676. '']));
  31677. end;
  31678. procedure TTestModule.TestRTTI_OverloadProperty;
  31679. begin
  31680. WithTypeInfo:=true;
  31681. StartProgram(false);
  31682. Add('type');
  31683. Add(' TObject = class');
  31684. Add(' protected');
  31685. Add(' FFlag: longint;');
  31686. Add(' published');
  31687. Add(' property Flag: longint read fflag;');
  31688. Add(' end;');
  31689. Add(' TSky = class');
  31690. Add(' published');
  31691. Add(' property FLAG: longint write fflag;');
  31692. Add(' end;');
  31693. Add('begin');
  31694. ConvertProgram;
  31695. CheckSource('TestRTTI_OverrideMethod',
  31696. LinesToStr([ // statements
  31697. 'rtl.createClass(this, "TObject", null, function () {',
  31698. ' this.$init = function () {',
  31699. ' this.FFlag = 0;',
  31700. ' };',
  31701. ' this.$final = function () {',
  31702. ' };',
  31703. ' var $r = this.$rtti;',
  31704. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  31705. '});',
  31706. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31707. ' var $r = this.$rtti;',
  31708. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  31709. '});',
  31710. '']),
  31711. LinesToStr([ // $mod.$main
  31712. '']));
  31713. end;
  31714. procedure TTestModule.TestRTTI_ClassForward;
  31715. begin
  31716. WithTypeInfo:=true;
  31717. StartProgram(false);
  31718. Add('type');
  31719. Add(' TObject = class end;');
  31720. Add(' tbridge = class;');
  31721. Add(' TProc = function: tbridge;');
  31722. Add(' TOger = class');
  31723. Add(' published');
  31724. Add(' FBridge: tbridge;');
  31725. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  31726. Add(' property Bridge: tbridge read fbridge write setbridge;');
  31727. Add(' end;');
  31728. Add(' TBridge = class');
  31729. Add(' FOger: toger;');
  31730. Add(' end;');
  31731. Add('var p: Pointer;');
  31732. Add(' b: tbridge;');
  31733. Add('begin');
  31734. Add(' p:=typeinfo(tbridge);');
  31735. Add(' p:=typeinfo(b);');
  31736. ConvertProgram;
  31737. CheckSource('TestRTTI_ClassForward',
  31738. LinesToStr([ // statements
  31739. 'rtl.createClass(this, "TObject", null, function () {',
  31740. ' this.$init = function () {',
  31741. ' };',
  31742. ' this.$final = function () {',
  31743. ' };',
  31744. '});',
  31745. 'this.$rtti.$Class("TBridge");',
  31746. 'this.$rtti.$ProcVar("TProc", {',
  31747. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  31748. '});',
  31749. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  31750. ' this.$init = function () {',
  31751. ' $mod.TObject.$init.call(this);',
  31752. ' this.FBridge = null;',
  31753. ' };',
  31754. ' this.$final = function () {',
  31755. ' this.FBridge = undefined;',
  31756. ' $mod.TObject.$final.call(this);',
  31757. ' };',
  31758. ' var $r = this.$rtti;',
  31759. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  31760. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  31761. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  31762. '});',
  31763. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  31764. ' this.$init = function () {',
  31765. ' $mod.TObject.$init.call(this);',
  31766. ' this.FOger = null;',
  31767. ' };',
  31768. ' this.$final = function () {',
  31769. ' this.FOger = undefined;',
  31770. ' $mod.TObject.$final.call(this);',
  31771. ' };',
  31772. '});',
  31773. 'this.p = null;',
  31774. 'this.b = null;',
  31775. '']),
  31776. LinesToStr([ // $mod.$main
  31777. '$mod.p = $mod.$rtti["TBridge"];',
  31778. '$mod.p = $mod.b.$rtti;',
  31779. '']));
  31780. end;
  31781. procedure TTestModule.TestRTTI_ClassOf;
  31782. begin
  31783. WithTypeInfo:=true;
  31784. StartProgram(false);
  31785. Add('type');
  31786. Add(' TClass = class of tobject;');
  31787. Add(' TProcA = function: TClass;');
  31788. Add(' TObject = class');
  31789. Add(' published');
  31790. Add(' C: tclass;');
  31791. Add(' end;');
  31792. Add(' tfox = class;');
  31793. Add(' TBird = class end;');
  31794. Add(' TBirds = class of tbird;');
  31795. Add(' TFox = class end;');
  31796. Add(' TFoxes = class of tfox;');
  31797. Add(' TCows = class of TCow;');
  31798. Add(' TCow = class;');
  31799. Add(' TCow = class end;');
  31800. Add('begin');
  31801. ConvertProgram;
  31802. CheckSource('TestRTTI_ClassOf',
  31803. LinesToStr([ // statements
  31804. 'this.$rtti.$Class("TObject");',
  31805. 'this.$rtti.$ClassRef("TClass", {',
  31806. ' instancetype: this.$rtti["TObject"]',
  31807. '});',
  31808. 'this.$rtti.$ProcVar("TProcA", {',
  31809. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  31810. '});',
  31811. 'rtl.createClass(this, "TObject", null, function () {',
  31812. ' this.$init = function () {',
  31813. ' this.C = null;',
  31814. ' };',
  31815. ' this.$final = function () {',
  31816. ' this.C = undefined;',
  31817. ' };',
  31818. ' var $r = this.$rtti;',
  31819. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31820. '});',
  31821. 'this.$rtti.$Class("TFox");',
  31822. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31823. '});',
  31824. 'this.$rtti.$ClassRef("TBirds", {',
  31825. ' instancetype: this.$rtti["TBird"]',
  31826. '});',
  31827. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31828. '});',
  31829. 'this.$rtti.$ClassRef("TFoxes", {',
  31830. ' instancetype: this.$rtti["TFox"]',
  31831. '});',
  31832. 'this.$rtti.$Class("TCow");',
  31833. 'this.$rtti.$ClassRef("TCows", {',
  31834. ' instancetype: this.$rtti["TCow"]',
  31835. '});',
  31836. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31837. '});',
  31838. '']),
  31839. LinesToStr([ // $mod.$main
  31840. '']));
  31841. end;
  31842. procedure TTestModule.TestRTTI_Record;
  31843. begin
  31844. WithTypeInfo:=true;
  31845. StartProgram(false);
  31846. Add('type');
  31847. Add(' integer = longint;');
  31848. Add(' TPoint = record');
  31849. Add(' x,y: integer;');
  31850. Add(' end;');
  31851. Add('var p: pointer;');
  31852. Add(' r: tpoint;');
  31853. Add('begin');
  31854. Add(' p:=typeinfo(tpoint);');
  31855. Add(' p:=typeinfo(r);');
  31856. Add(' p:=typeinfo(r.x);');
  31857. ConvertProgram;
  31858. CheckSource('TestRTTI_Record',
  31859. LinesToStr([ // statements
  31860. 'rtl.recNewT(this, "TPoint", function () {',
  31861. ' this.x = 0;',
  31862. ' this.y = 0;',
  31863. ' this.$eq = function (b) {',
  31864. ' return (this.x === b.x) && (this.y === b.y);',
  31865. ' };',
  31866. ' this.$assign = function (s) {',
  31867. ' this.x = s.x;',
  31868. ' this.y = s.y;',
  31869. ' return this;',
  31870. ' };',
  31871. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31872. ' $r.addField("x", rtl.longint);',
  31873. ' $r.addField("y", rtl.longint);',
  31874. '});',
  31875. 'this.p = null;',
  31876. 'this.r = this.TPoint.$new();',
  31877. '']),
  31878. LinesToStr([ // $mod.$main
  31879. '$mod.p = $mod.$rtti["TPoint"];',
  31880. '$mod.p = $mod.$rtti["TPoint"];',
  31881. '$mod.p = rtl.longint;',
  31882. '']));
  31883. end;
  31884. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31885. begin
  31886. WithTypeInfo:=true;
  31887. StartProgram(false);
  31888. Add('type');
  31889. Add(' TFloatRec = record');
  31890. Add(' c,d: array of AnsiChar;');
  31891. // Add(' i: array of array of longint;');
  31892. Add(' end;');
  31893. Add('var p: pointer;');
  31894. Add(' r: tfloatrec;');
  31895. Add('begin');
  31896. Add(' p:=typeinfo(tfloatrec);');
  31897. Add(' p:=typeinfo(r);');
  31898. Add(' p:=typeinfo(r.d);');
  31899. ConvertProgram;
  31900. CheckSource('TestRTTI_Record',
  31901. LinesToStr([ // statements
  31902. 'rtl.recNewT(this, "TFloatRec", function () {',
  31903. ' this.$new = function () {',
  31904. ' var r = Object.create(this);',
  31905. ' r.c = [];',
  31906. ' r.d = [];',
  31907. ' return r;',
  31908. ' };',
  31909. ' this.$eq = function (b) {',
  31910. ' return (this.c === b.c) && (this.d === b.d);',
  31911. ' };',
  31912. ' this.$assign = function (s) {',
  31913. ' this.c = rtl.arrayRef(s.c);',
  31914. ' this.d = rtl.arrayRef(s.d);',
  31915. ' return this;',
  31916. ' };',
  31917. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31918. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31919. ' eltype: rtl.AnsiChar',
  31920. ' });',
  31921. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31922. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31923. '});',
  31924. 'this.p = null;',
  31925. 'this.r = this.TFloatRec.$new();',
  31926. '']),
  31927. LinesToStr([ // $mod.$main
  31928. '$mod.p = $mod.$rtti["TFloatRec"];',
  31929. '$mod.p = $mod.$rtti["TFloatRec"];',
  31930. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31931. '']));
  31932. end;
  31933. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31934. begin
  31935. WithTypeInfo:=true;
  31936. StartProgram(false);
  31937. Add([
  31938. '{$modeswitch AdvancedRecords}',
  31939. 'type',
  31940. ' TPoint = record',
  31941. ' type TProc = procedure(w: word);',
  31942. ' class var p: TProc;',
  31943. ' end;',
  31944. 'begin',
  31945. '']);
  31946. ConvertProgram;
  31947. CheckSource('TestRTTI_Record_ClassVarType',
  31948. LinesToStr([ // statements
  31949. 'rtl.recNewT(this, "TPoint", function () {',
  31950. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  31951. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  31952. ' });',
  31953. ' this.p = null;',
  31954. ' this.$eq = function (b) {',
  31955. ' return true;',
  31956. ' };',
  31957. ' this.$assign = function (s) {',
  31958. ' return this;',
  31959. ' };',
  31960. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31961. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  31962. '}, true);',
  31963. '']),
  31964. LinesToStr([ // $mod.$main
  31965. '']));
  31966. end;
  31967. procedure TTestModule.TestRTTI_LocalTypes;
  31968. begin
  31969. WithTypeInfo:=true;
  31970. StartProgram(false);
  31971. Add([
  31972. 'procedure DoIt;',
  31973. 'type',
  31974. ' integer = longint;',
  31975. ' TPoint = record',
  31976. ' x,y: integer;',
  31977. ' end;',
  31978. 'var p: TPoint;',
  31979. 'begin',
  31980. 'end;',
  31981. 'begin']);
  31982. ConvertProgram;
  31983. CheckSource('TestRTTI_LocalTypes',
  31984. LinesToStr([ // statements
  31985. 'var TPoint = rtl.recNewT(null, "", function () {',
  31986. ' this.x = 0;',
  31987. ' this.y = 0;',
  31988. ' this.$eq = function (b) {',
  31989. ' return (this.x === b.x) && (this.y === b.y);',
  31990. ' };',
  31991. ' this.$assign = function (s) {',
  31992. ' this.x = s.x;',
  31993. ' this.y = s.y;',
  31994. ' return this;',
  31995. ' };',
  31996. '});',
  31997. 'this.DoIt = function () {',
  31998. ' var p = TPoint.$new();',
  31999. '};',
  32000. '']),
  32001. LinesToStr([ // $mod.$main
  32002. '']));
  32003. end;
  32004. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  32005. begin
  32006. WithTypeInfo:=true;
  32007. StartProgram(false);
  32008. Add([
  32009. 'type',
  32010. ' TCaption = string;',
  32011. ' TYesNo = boolean;',
  32012. ' TLetter = AnsiChar;',
  32013. ' TFloat = double;',
  32014. ' TPtr = pointer;',
  32015. ' TShortInt = shortint;',
  32016. ' TByte = byte;',
  32017. ' TSmallInt = smallint;',
  32018. ' TWord = word;',
  32019. ' TInt32 = longint;',
  32020. ' TDWord = longword;',
  32021. ' TValue = jsvalue;',
  32022. 'var p: TPtr;',
  32023. 'begin',
  32024. ' p:=typeinfo(string);',
  32025. ' p:=typeinfo(tcaption);',
  32026. ' p:=typeinfo(boolean);',
  32027. ' p:=typeinfo(tyesno);',
  32028. ' p:=typeinfo(AnsiChar);',
  32029. ' p:=typeinfo(tletter);',
  32030. ' p:=typeinfo(double);',
  32031. ' p:=typeinfo(tfloat);',
  32032. ' p:=typeinfo(pointer);',
  32033. ' p:=typeinfo(tptr);',
  32034. ' p:=typeinfo(shortint);',
  32035. ' p:=typeinfo(tshortint);',
  32036. ' p:=typeinfo(byte);',
  32037. ' p:=typeinfo(tbyte);',
  32038. ' p:=typeinfo(smallint);',
  32039. ' p:=typeinfo(tsmallint);',
  32040. ' p:=typeinfo(word);',
  32041. ' p:=typeinfo(tword);',
  32042. ' p:=typeinfo(longword);',
  32043. ' p:=typeinfo(tdword);',
  32044. ' p:=typeinfo(jsvalue);',
  32045. ' p:=typeinfo(tvalue);',
  32046. '']);
  32047. ConvertProgram;
  32048. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  32049. LinesToStr([ // statements
  32050. 'this.p = null;',
  32051. '']),
  32052. LinesToStr([ // $mod.$main
  32053. '$mod.p = rtl.string;',
  32054. '$mod.p = rtl.string;',
  32055. '$mod.p = rtl.boolean;',
  32056. '$mod.p = rtl.boolean;',
  32057. '$mod.p = rtl.AnsiChar;',
  32058. '$mod.p = rtl.AnsiChar;',
  32059. '$mod.p = rtl.double;',
  32060. '$mod.p = rtl.double;',
  32061. '$mod.p = rtl.pointer;',
  32062. '$mod.p = rtl.pointer;',
  32063. '$mod.p = rtl.shortint;',
  32064. '$mod.p = rtl.shortint;',
  32065. '$mod.p = rtl.byte;',
  32066. '$mod.p = rtl.byte;',
  32067. '$mod.p = rtl.smallint;',
  32068. '$mod.p = rtl.smallint;',
  32069. '$mod.p = rtl.word;',
  32070. '$mod.p = rtl.word;',
  32071. '$mod.p = rtl.longword;',
  32072. '$mod.p = rtl.longword;',
  32073. '$mod.p = rtl.jsvalue;',
  32074. '$mod.p = rtl.jsvalue;',
  32075. '']));
  32076. end;
  32077. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  32078. begin
  32079. WithTypeInfo:=true;
  32080. StartProgram(false);
  32081. Add([
  32082. 'type',
  32083. ' TCaption = type string;',
  32084. ' TYesNo = type boolean;',
  32085. ' TLetter = type AnsiChar;',
  32086. ' TFloat = type double;',
  32087. ' TPtr = type pointer;',
  32088. ' TShortInt = type shortint;',
  32089. ' TByte = type byte;',
  32090. ' TSmallInt = type smallint;',
  32091. ' TWord = type word;',
  32092. ' TInt32 = type longint;',
  32093. ' TDWord = type longword;',
  32094. ' TValue = type jsvalue;',
  32095. ' TAliasValue = type TValue;',
  32096. 'var',
  32097. ' p: TPtr;',
  32098. ' a: TAliasValue;',
  32099. 'begin',
  32100. ' p:=typeinfo(tcaption);',
  32101. ' p:=typeinfo(tyesno);',
  32102. ' p:=typeinfo(tletter);',
  32103. ' p:=typeinfo(tfloat);',
  32104. ' p:=typeinfo(tptr);',
  32105. ' p:=typeinfo(tshortint);',
  32106. ' p:=typeinfo(tbyte);',
  32107. ' p:=typeinfo(tsmallint);',
  32108. ' p:=typeinfo(tword);',
  32109. ' p:=typeinfo(tdword);',
  32110. ' p:=typeinfo(tvalue);',
  32111. ' p:=typeinfo(taliasvalue);',
  32112. ' p:=typeinfo(a);',
  32113. '']);
  32114. ConvertProgram;
  32115. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  32116. LinesToStr([ // statements
  32117. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  32118. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  32119. 'this.$rtti.$inherited("TLetter", rtl.AnsiChar, {});',
  32120. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  32121. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  32122. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  32123. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  32124. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  32125. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  32126. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  32127. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  32128. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  32129. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  32130. 'this.p = null;',
  32131. 'this.a = undefined;',
  32132. '']),
  32133. LinesToStr([ // $mod.$main
  32134. '$mod.p = $mod.$rtti["TCaption"];',
  32135. '$mod.p = $mod.$rtti["TYesNo"];',
  32136. '$mod.p = $mod.$rtti["TLetter"];',
  32137. '$mod.p = $mod.$rtti["TFloat"];',
  32138. '$mod.p = $mod.$rtti["TPtr"];',
  32139. '$mod.p = $mod.$rtti["TShortInt"];',
  32140. '$mod.p = $mod.$rtti["TByte"];',
  32141. '$mod.p = $mod.$rtti["TSmallInt"];',
  32142. '$mod.p = $mod.$rtti["TWord"];',
  32143. '$mod.p = $mod.$rtti["TDWord"];',
  32144. '$mod.p = $mod.$rtti["TValue"];',
  32145. '$mod.p = $mod.$rtti["TAliasValue"];',
  32146. '$mod.p = $mod.$rtti["TAliasValue"];',
  32147. '']));
  32148. end;
  32149. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  32150. begin
  32151. WithTypeInfo:=true;
  32152. StartProgram(false);
  32153. Add('procedure DoIt;');
  32154. Add('type');
  32155. Add(' integer = longint;');
  32156. Add(' TPoint = record');
  32157. Add(' x,y: integer;');
  32158. Add(' end;');
  32159. Add('var p: pointer;');
  32160. Add('begin');
  32161. Add(' p:=typeinfo(tpoint);');
  32162. Add('end;');
  32163. Add('begin');
  32164. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  32165. ConvertProgram;
  32166. end;
  32167. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  32168. begin
  32169. WithTypeInfo:=true;
  32170. StartProgram(true,[supTypeInfo]);
  32171. Add([
  32172. '{$modeswitch externalclass}',
  32173. 'type',
  32174. ' TFlag = (up,down);',
  32175. ' TFlags = set of TFlag;',
  32176. 'var',
  32177. ' ti: TTypeInfo;',
  32178. ' tiInt: TTypeInfoInteger;',
  32179. ' tiEnum: TTypeInfoEnum;',
  32180. ' tiSet: TTypeInfoSet;',
  32181. 'begin',
  32182. ' ti:=typeinfo(string);',
  32183. ' ti:=typeinfo(boolean);',
  32184. ' ti:=typeinfo(AnsiChar);',
  32185. ' ti:=typeinfo(double);',
  32186. ' tiInt:=typeinfo(shortint);',
  32187. ' tiInt:=typeinfo(byte);',
  32188. ' tiInt:=typeinfo(smallint);',
  32189. ' tiInt:=typeinfo(word);',
  32190. ' tiInt:=typeinfo(longint);',
  32191. ' tiInt:=typeinfo(longword);',
  32192. ' ti:=typeinfo(jsvalue);',
  32193. ' tiEnum:=typeinfo(tflag);',
  32194. ' tiSet:=typeinfo(tflags);']);
  32195. ConvertProgram;
  32196. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  32197. LinesToStr([ // statements
  32198. 'this.TFlag = {',
  32199. ' "0": "up",',
  32200. ' up: 0,',
  32201. ' "1": "down",',
  32202. ' down: 1',
  32203. '};',
  32204. 'this.$rtti.$Enum("TFlag", {',
  32205. ' minvalue: 0,',
  32206. ' maxvalue: 1,',
  32207. ' ordtype: 1,',
  32208. ' enumtype: this.TFlag',
  32209. '});',
  32210. 'this.$rtti.$Set("TFlags", {',
  32211. ' comptype: this.$rtti["TFlag"]',
  32212. '});',
  32213. 'this.ti = null;',
  32214. 'this.tiInt = null;',
  32215. 'this.tiEnum = null;',
  32216. 'this.tiSet = null;',
  32217. '']),
  32218. LinesToStr([ // $mod.$main
  32219. '$mod.ti = rtl.string;',
  32220. '$mod.ti = rtl.boolean;',
  32221. '$mod.ti = rtl.AnsiChar;',
  32222. '$mod.ti = rtl.double;',
  32223. '$mod.tiInt = rtl.shortint;',
  32224. '$mod.tiInt = rtl.byte;',
  32225. '$mod.tiInt = rtl.smallint;',
  32226. '$mod.tiInt = rtl.word;',
  32227. '$mod.tiInt = rtl.longint;',
  32228. '$mod.tiInt = rtl.longword;',
  32229. '$mod.ti = rtl.jsvalue;',
  32230. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  32231. '$mod.tiSet = $mod.$rtti["TFlags"];',
  32232. '']));
  32233. end;
  32234. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  32235. begin
  32236. WithTypeInfo:=true;
  32237. StartProgram(true,[supTypeInfo]);
  32238. Add('{$modeswitch externalclass}');
  32239. Add('type');
  32240. Add(' TStaticArr = array[boolean] of string;');
  32241. Add(' TDynArr = array of string;');
  32242. Add(' TProc = procedure;');
  32243. Add(' TMethod = procedure of object;');
  32244. Add('var');
  32245. Add(' StaticArray: TStaticArr;');
  32246. Add(' tiStaticArray: TTypeInfoStaticArray;');
  32247. Add(' DynArray: TDynArr;');
  32248. Add(' tiDynArray: TTypeInfoDynArray;');
  32249. Add(' ProcVar: TProc;');
  32250. Add(' tiProcVar: TTypeInfoProcVar;');
  32251. Add(' MethodVar: TMethod;');
  32252. Add(' tiMethodVar: TTypeInfoMethodVar;');
  32253. Add('begin');
  32254. Add(' tiStaticArray:=typeinfo(StaticArray);');
  32255. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  32256. Add(' tiDynArray:=typeinfo(DynArray);');
  32257. Add(' tiDynArray:=typeinfo(TDynArr);');
  32258. Add(' tiProcVar:=typeinfo(ProcVar);');
  32259. Add(' tiProcVar:=typeinfo(TProc);');
  32260. Add(' tiMethodVar:=typeinfo(MethodVar);');
  32261. Add(' tiMethodVar:=typeinfo(TMethod);');
  32262. ConvertProgram;
  32263. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  32264. LinesToStr([ // statements
  32265. 'this.$rtti.$StaticArray("TStaticArr", {',
  32266. ' dims: [2],',
  32267. ' eltype: rtl.string',
  32268. '});',
  32269. 'this.$rtti.$DynArray("TDynArr", {',
  32270. ' eltype: rtl.string',
  32271. '});',
  32272. 'this.$rtti.$ProcVar("TProc", {',
  32273. ' procsig: rtl.newTIProcSig([])',
  32274. '});',
  32275. 'this.$rtti.$MethodVar("TMethod", {',
  32276. ' procsig: rtl.newTIProcSig([]),',
  32277. ' methodkind: 0',
  32278. '});',
  32279. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  32280. 'this.tiStaticArray = null;',
  32281. 'this.DynArray = [];',
  32282. 'this.tiDynArray = null;',
  32283. 'this.ProcVar = null;',
  32284. 'this.tiProcVar = null;',
  32285. 'this.MethodVar = null;',
  32286. 'this.tiMethodVar = null;',
  32287. '']),
  32288. LinesToStr([ // $mod.$main
  32289. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32290. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32291. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32292. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32293. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32294. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32295. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32296. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32297. '']));
  32298. end;
  32299. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  32300. begin
  32301. WithTypeInfo:=true;
  32302. StartProgram(true,[supTypeInfo]);
  32303. Add('{$modeswitch externalclass}');
  32304. Add('type');
  32305. Add(' TRec = record end;');
  32306. // ToDo: ^TRec
  32307. Add(' TObject = class end;');
  32308. Add(' TClass = class of tobject;');
  32309. Add('var');
  32310. Add(' Rec: trec;');
  32311. Add(' tiRecord: ttypeinforecord;');
  32312. Add(' Obj: tobject;');
  32313. Add(' tiClass: ttypeinfoclass;');
  32314. Add(' aClass: tclass;');
  32315. Add(' tiClassRef: ttypeinfoclassref;');
  32316. // ToDo: ^TRec
  32317. Add(' tiPointer: ttypeinfopointer;');
  32318. Add('begin');
  32319. Add(' tirecord:=typeinfo(trec);');
  32320. Add(' tirecord:=typeinfo(trec);');
  32321. Add(' ticlass:=typeinfo(obj);');
  32322. Add(' ticlass:=typeinfo(tobject);');
  32323. Add(' ticlass:=typeinfo(aclass);');
  32324. Add(' ticlassref:=typeinfo(tclass);');
  32325. ConvertProgram;
  32326. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  32327. LinesToStr([ // statements
  32328. 'rtl.recNewT(this, "TRec", function () {',
  32329. ' this.$eq = function (b) {',
  32330. ' return true;',
  32331. ' };',
  32332. ' this.$assign = function (s) {',
  32333. ' return this;',
  32334. ' };',
  32335. ' $mod.$rtti.$Record("TRec", {});',
  32336. '});',
  32337. 'rtl.createClass(this, "TObject", null, function () {',
  32338. ' this.$init = function () {',
  32339. ' };',
  32340. ' this.$final = function () {',
  32341. ' };',
  32342. '});',
  32343. 'this.$rtti.$ClassRef("TClass", {',
  32344. ' instancetype: this.$rtti["TObject"]',
  32345. '});',
  32346. 'this.Rec = this.TRec.$new();',
  32347. 'this.tiRecord = null;',
  32348. 'this.Obj = null;',
  32349. 'this.tiClass = null;',
  32350. 'this.aClass = null;',
  32351. 'this.tiClassRef = null;',
  32352. 'this.tiPointer = null;',
  32353. '']),
  32354. LinesToStr([ // $mod.$main
  32355. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32356. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32357. '$mod.tiClass = $mod.Obj.$rtti;',
  32358. '$mod.tiClass = $mod.$rtti["TObject"];',
  32359. '$mod.tiClass = $mod.aClass.$rtti;',
  32360. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  32361. '']));
  32362. end;
  32363. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  32364. begin
  32365. WithTypeInfo:=true;
  32366. StartProgram(true,[supTypeInfo]);
  32367. Add([
  32368. '{$modeswitch externalclass}',
  32369. 'type',
  32370. ' TClass = class of tobject;',
  32371. ' TObject = class',
  32372. ' function MyClass: TClass;',
  32373. ' class function ClassType: TClass;',
  32374. ' end;',
  32375. 'function TObject.MyClass: TClass;',
  32376. 'var t: TTypeInfoClass;',
  32377. 'begin',
  32378. ' t:=TypeInfo(Self);',
  32379. ' t:=TypeInfo(Result);',
  32380. ' t:=TypeInfo(TObject);',
  32381. 'end;',
  32382. 'class function TObject.ClassType: TClass;',
  32383. 'var t: TTypeInfoClass;',
  32384. 'begin',
  32385. ' t:=TypeInfo(Self);',
  32386. ' t:=TypeInfo(Result);',
  32387. 'end;',
  32388. 'var',
  32389. ' Obj: TObject;',
  32390. ' t: TTypeInfoClass;',
  32391. 'begin',
  32392. ' t:=TypeInfo(TObject.ClassType);',
  32393. ' t:=TypeInfo(Obj.ClassType);',
  32394. ' t:=TypeInfo(Obj.MyClass);',
  32395. '']);
  32396. ConvertProgram;
  32397. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32398. LinesToStr([ // statements
  32399. 'this.$rtti.$Class("TObject");',
  32400. 'this.$rtti.$ClassRef("TClass", {',
  32401. ' instancetype: this.$rtti["TObject"]',
  32402. '});',
  32403. 'rtl.createClass(this, "TObject", null, function () {',
  32404. ' this.$init = function () {',
  32405. ' };',
  32406. ' this.$final = function () {',
  32407. ' };',
  32408. ' this.MyClass = function () {',
  32409. ' var Result = null;',
  32410. ' var t = null;',
  32411. ' t = this.$rtti;',
  32412. ' t = Result.$rtti;',
  32413. ' t = $mod.$rtti["TObject"];',
  32414. ' return Result;',
  32415. ' };',
  32416. ' this.ClassType = function () {',
  32417. ' var Result = null;',
  32418. ' var t = null;',
  32419. ' t = this.$rtti;',
  32420. ' t = Result.$rtti;',
  32421. ' return Result;',
  32422. ' };',
  32423. '});',
  32424. 'this.Obj = null;',
  32425. 'this.t = null;',
  32426. '']),
  32427. LinesToStr([ // $mod.$main
  32428. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32429. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32430. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32431. '']));
  32432. end;
  32433. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32434. begin
  32435. WithTypeInfo:=true;
  32436. AddModuleWithIntfImplSrc('typinfo.pas',
  32437. LinesToStr([
  32438. '{$modeswitch externalclass}',
  32439. 'type',
  32440. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32441. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32442. '']),
  32443. '');
  32444. AddModuleWithIntfImplSrc('unit2.pas',
  32445. LinesToStr([
  32446. 'uses typinfo;',
  32447. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32448. 'procedure DoPtr(p: PTypeInfo);',
  32449. 'procedure DoInfo(t: TTypeInfo);',
  32450. 'procedure DoInt(t: TTypeInfoInteger);',
  32451. '']),
  32452. LinesToStr([
  32453. 'procedure DoPtr(p: PTypeInfo);',
  32454. 'begin end;',
  32455. 'procedure DoInfo(t: TTypeInfo);',
  32456. 'begin end;',
  32457. 'procedure DoInt(t: TTypeInfoInteger);',
  32458. 'begin end;',
  32459. '']));
  32460. StartUnit(true);
  32461. Add([
  32462. 'interface',
  32463. 'uses unit2;', // does not use unit typinfo
  32464. 'implementation',
  32465. 'var',
  32466. ' i: byte;',
  32467. ' p: pointer;',
  32468. ' t: PTypeInfo;',
  32469. 'initialization',
  32470. ' p:=typeinfo(i);',
  32471. ' t:=typeinfo(i);',
  32472. ' if p=t then ;',
  32473. ' if p=typeinfo(i) then ;',
  32474. ' if typeinfo(i)=p then ;',
  32475. ' if t=typeinfo(i) then ;',
  32476. ' if typeinfo(i)=t then ;',
  32477. ' DoPtr(p);',
  32478. ' DoPtr(t);',
  32479. ' DoPtr(typeinfo(i));',
  32480. ' DoInfo(p);',
  32481. ' DoInfo(t);',
  32482. ' DoInfo(typeinfo(i));',
  32483. ' DoInt(typeinfo(i));',
  32484. '']);
  32485. ConvertUnit;
  32486. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32487. LinesToStr([ // statements
  32488. 'var $impl = $mod.$impl;',
  32489. '']),
  32490. LinesToStr([ // this.$init
  32491. '$impl.p = rtl.byte;',
  32492. '$impl.t = rtl.byte;',
  32493. 'if ($impl.p === $impl.t) ;',
  32494. 'if ($impl.p === rtl.byte) ;',
  32495. 'if (rtl.byte === $impl.p) ;',
  32496. 'if ($impl.t === rtl.byte) ;',
  32497. 'if (rtl.byte === $impl.t) ;',
  32498. 'pas.unit2.DoPtr($impl.p);',
  32499. 'pas.unit2.DoPtr($impl.t);',
  32500. 'pas.unit2.DoPtr(rtl.byte);',
  32501. 'pas.unit2.DoInfo($impl.p);',
  32502. 'pas.unit2.DoInfo($impl.t);',
  32503. 'pas.unit2.DoInfo(rtl.byte);',
  32504. 'pas.unit2.DoInt(rtl.byte);',
  32505. '']),
  32506. LinesToStr([ // implementation
  32507. '$impl.i = 0;',
  32508. '$impl.p = null;',
  32509. '$impl.t = null;',
  32510. '']) );
  32511. end;
  32512. procedure TTestModule.TestRTTI_Interface_Corba;
  32513. begin
  32514. WithTypeInfo:=true;
  32515. StartProgram(true,[supTypeInfo]);
  32516. Add([
  32517. '{$interfaces corba}',
  32518. '{$modeswitch externalclass}',
  32519. 'type',
  32520. ' IUnknown = interface',
  32521. ' end;',
  32522. ' IBird = interface',
  32523. ' function GetItem: longint;',
  32524. ' procedure SetItem(Value: longint);',
  32525. ' property Item: longint read GetItem write SetItem;',
  32526. ' end;',
  32527. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32528. 'var',
  32529. ' i: IBird;',
  32530. ' t: TTypeInfoInterface;',
  32531. 'begin',
  32532. ' t:=TypeInfo(IBird);',
  32533. ' t:=TypeInfo(i);',
  32534. ' DoIt(t);',
  32535. ' DoIt(TypeInfo(IBird));',
  32536. '']);
  32537. ConvertProgram;
  32538. CheckSource('TestRTTI_Interface_Corba',
  32539. LinesToStr([ // statements
  32540. 'rtl.createInterface(',
  32541. ' this,',
  32542. ' "IUnknown",',
  32543. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32544. ' [],',
  32545. ' null,',
  32546. ' function () {',
  32547. ' }',
  32548. ');',
  32549. 'rtl.createInterface(',
  32550. ' this,',
  32551. ' "IBird",',
  32552. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32553. ' ["GetItem", "SetItem"],',
  32554. ' null,',
  32555. ' function () {',
  32556. ' var $r = this.$rtti;',
  32557. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32558. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32559. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32560. ' }',
  32561. ');',
  32562. 'this.DoIt = function (t) {',
  32563. '}; ',
  32564. 'this.i = null;',
  32565. 'this.t = null;',
  32566. '']),
  32567. LinesToStr([ // $mod.$main
  32568. '$mod.t = $mod.$rtti["IBird"];',
  32569. '$mod.t = $mod.i.$rtti;',
  32570. '$mod.DoIt($mod.t);',
  32571. '$mod.DoIt($mod.$rtti["IBird"]);',
  32572. '']));
  32573. end;
  32574. procedure TTestModule.TestRTTI_Interface_COM;
  32575. begin
  32576. WithTypeInfo:=true;
  32577. StartProgram(true,[supTypeInfo]);
  32578. Add([
  32579. '{$interfaces com}',
  32580. '{$modeswitch externalclass}',
  32581. 'type',
  32582. ' TGuid = record end;',
  32583. ' integer = longint;',
  32584. ' IUnknown = interface',
  32585. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32586. ' function _AddRef: Integer;',
  32587. ' function _Release: Integer;',
  32588. ' end;',
  32589. ' IBird = interface',
  32590. ' function GetItem: longint;',
  32591. ' procedure SetItem(Value: longint);',
  32592. ' property Item: longint read GetItem write SetItem;',
  32593. ' end;',
  32594. 'var',
  32595. ' i: IBird;',
  32596. ' t: TTypeInfoInterface;',
  32597. 'begin',
  32598. ' t:=TypeInfo(IBird);',
  32599. ' t:=TypeInfo(i);',
  32600. '']);
  32601. ConvertProgram;
  32602. CheckSource('TestRTTI_Interface_COM',
  32603. LinesToStr([ // statements
  32604. 'rtl.recNewT(this, "TGuid", function () {',
  32605. ' this.$eq = function (b) {',
  32606. ' return true;',
  32607. ' };',
  32608. ' this.$assign = function (s) {',
  32609. ' return this;',
  32610. ' };',
  32611. ' $mod.$rtti.$Record("TGuid", {});',
  32612. '});',
  32613. 'rtl.createInterface(',
  32614. ' this,',
  32615. ' "IUnknown",',
  32616. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32617. ' ["QueryInterface", "_AddRef", "_Release"],',
  32618. ' null,',
  32619. ' function () {',
  32620. ' this.$kind = "com";',
  32621. ' var $r = this.$rtti;',
  32622. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32623. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32624. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32625. ' }',
  32626. ');',
  32627. 'rtl.createInterface(',
  32628. ' this,',
  32629. ' "IBird",',
  32630. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32631. ' ["GetItem", "SetItem"],',
  32632. ' this.IUnknown,',
  32633. ' function () {',
  32634. ' var $r = this.$rtti;',
  32635. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32636. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32637. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32638. ' }',
  32639. ');',
  32640. 'this.i = null;',
  32641. 'this.t = null;',
  32642. '']),
  32643. LinesToStr([ // $mod.$main
  32644. '$mod.t = $mod.$rtti["IBird"];',
  32645. '$mod.t = $mod.i.$rtti;',
  32646. '']));
  32647. end;
  32648. procedure TTestModule.TestRTTI_ClassHelper;
  32649. begin
  32650. WithTypeInfo:=true;
  32651. StartProgram(true,[supTypeInfo]);
  32652. Add([
  32653. '{$interfaces com}',
  32654. '{$modeswitch externalclass}',
  32655. 'type',
  32656. ' TObject = class',
  32657. ' end;',
  32658. ' THelper = class helper for TObject',
  32659. ' published',
  32660. ' function GetItem: longint;',
  32661. ' property Item: longint read GetItem;',
  32662. ' end;',
  32663. 'function THelper.GetItem: longint;',
  32664. 'begin',
  32665. 'end;',
  32666. 'var',
  32667. ' t: TTypeInfoHelper;',
  32668. 'begin',
  32669. ' t:=TypeInfo(THelper);',
  32670. '']);
  32671. ConvertProgram;
  32672. CheckSource('TestRTTI_ClassHelper',
  32673. LinesToStr([ // statements
  32674. 'rtl.createClass(this, "TObject", null, function () {',
  32675. ' this.$init = function () {',
  32676. ' };',
  32677. ' this.$final = function () {',
  32678. ' };',
  32679. '});',
  32680. 'rtl.createHelper(this, "THelper", null, function () {',
  32681. ' this.GetItem = function () {',
  32682. ' var Result = 0;',
  32683. ' return Result;',
  32684. ' };',
  32685. ' var $r = this.$rtti;',
  32686. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32687. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32688. '});',
  32689. 'this.t = null;',
  32690. '']),
  32691. LinesToStr([ // $mod.$main
  32692. '$mod.t = $mod.$rtti["THelper"];',
  32693. '']));
  32694. end;
  32695. procedure TTestModule.TestRTTI_ExternalClass;
  32696. begin
  32697. WithTypeInfo:=true;
  32698. StartProgram(true,[supTypeInfo]);
  32699. Add([
  32700. '{$modeswitch externalclass}',
  32701. 'type',
  32702. ' TJSObject = class external name ''Object''',
  32703. ' end;',
  32704. ' TJSArray = class external name ''Array'' (TJSObject)',
  32705. ' end;',
  32706. 'var',
  32707. ' p: Pointer;',
  32708. ' tc: TTypeInfoExtClass;',
  32709. 'begin',
  32710. ' p:=typeinfo(TJSArray);']);
  32711. ConvertProgram;
  32712. CheckSource('TestRTTI_ExternalClass',
  32713. LinesToStr([ // statements
  32714. 'this.$rtti.$ExtClass("TJSObject", {',
  32715. ' jsclass: "Object"',
  32716. '});',
  32717. 'this.$rtti.$ExtClass("TJSArray", {',
  32718. ' ancestor: this.$rtti["TJSObject"],',
  32719. ' jsclass: "Array"',
  32720. '});',
  32721. 'this.p = null;',
  32722. 'this.tc = null;',
  32723. '']),
  32724. LinesToStr([ // $mod.$main
  32725. '$mod.p = $mod.$rtti["TJSArray"];',
  32726. '']));
  32727. end;
  32728. procedure TTestModule.TestRTTI_Unit;
  32729. begin
  32730. WithTypeInfo:=true;
  32731. AddModuleWithIntfImplSrc('unit2.pas',
  32732. LinesToStr([
  32733. '{$mode delphi}',
  32734. 'type',
  32735. ' TWordArray = array of word;',
  32736. ' TArray<T> = array of T;',
  32737. '']),
  32738. '');
  32739. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  32740. Add([
  32741. '{$mode delphi}',
  32742. 'interface',
  32743. 'uses unit2;',
  32744. 'type',
  32745. ' IBird = interface',
  32746. ' function Swoop: TWordArray;',
  32747. ' function Glide: TArray<word>;',
  32748. ' end;',
  32749. 'procedure Fly;',
  32750. 'implementation',
  32751. 'procedure Fly;',
  32752. 'var',
  32753. ' ta: tTypeInfoDynArray;',
  32754. ' ti: tTypeInfoInterface;',
  32755. 'begin',
  32756. ' ta:=typeinfo(TWordArray);',
  32757. ' ta:=typeinfo(TArray<word>);',
  32758. ' ti:=typeinfo(IBird);',
  32759. 'end;',
  32760. '']);
  32761. ConvertUnit;
  32762. CheckSource('TestRTTI_ExternalClass',
  32763. LinesToStr([ // statements
  32764. 'rtl.createInterface(',
  32765. ' this,',
  32766. ' "IBird",',
  32767. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  32768. ' ["Swoop", "Glide"],',
  32769. ' pas.system.IUnknown,',
  32770. ' function () {',
  32771. ' var $r = this.$rtti;',
  32772. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  32773. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  32774. ' }',
  32775. ');',
  32776. 'this.Fly = function () {',
  32777. ' var ta = null;',
  32778. ' var ti = null;',
  32779. ' ta = pas.unit2.$rtti["TWordArray"];',
  32780. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  32781. ' ti = $mod.$rtti["IBird"];',
  32782. '};',
  32783. '']),
  32784. LinesToStr([ // $mod.$main
  32785. '']));
  32786. end;
  32787. procedure TTestModule.TestResourcestringProgram;
  32788. begin
  32789. AddModuleWithIntfImplSrc('unit2.pas',
  32790. LinesToStr([
  32791. 'resourcestring Title = ''Nice'';',
  32792. '']),
  32793. '');
  32794. StartProgram(true);
  32795. Add([
  32796. 'uses unit2;',
  32797. 'const Bar = ''bar'';',
  32798. 'resourcestring',
  32799. ' Red = ''red'';',
  32800. ' Foobar = ''fOo''+bar;',
  32801. 'var s: string;',
  32802. ' c: AnsiChar;',
  32803. 'begin',
  32804. ' s:=red;',
  32805. ' s:=test1.red;',
  32806. ' s:=Title;',
  32807. ' c:=red[1];',
  32808. ' c:=test1.red[2];',
  32809. ' if red=foobar then ;',
  32810. ' if red[3]=red[4] then ;']);
  32811. ConvertProgram;
  32812. CheckSource('TestResourcestringProgram',
  32813. LinesToStr([ // statements
  32814. 'this.Bar = "bar";',
  32815. 'this.s = "";',
  32816. 'this.c = "";',
  32817. '$mod.$resourcestrings = {',
  32818. ' Red: {',
  32819. ' org: "red"',
  32820. ' },',
  32821. ' Foobar: {',
  32822. ' org: "fOobar"',
  32823. ' }',
  32824. '};',
  32825. '']),
  32826. LinesToStr([ // $mod.$main
  32827. '$mod.s = rtl.getResStr($mod, "Red");',
  32828. '$mod.s = rtl.getResStr($mod, "Red");',
  32829. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32830. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32831. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32832. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32833. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32834. '']));
  32835. end;
  32836. procedure TTestModule.TestResourcestringUnit;
  32837. begin
  32838. AddModuleWithIntfImplSrc('unit2.pas',
  32839. LinesToStr([
  32840. 'resourcestring Title = ''Nice'';',
  32841. '']),
  32842. '');
  32843. StartUnit(true);
  32844. Add([
  32845. 'interface',
  32846. 'uses unit2;',
  32847. 'const Red = ''rEd'';',
  32848. 'resourcestring',
  32849. ' Blue = ''blue'';',
  32850. ' NotRed = ''not''+Red;',
  32851. 'var s: string;',
  32852. 'implementation',
  32853. 'resourcestring',
  32854. ' ImplGreen = ''green'';',
  32855. 'initialization',
  32856. ' s:=blue+ImplGreen;',
  32857. ' s:=test1.blue+test1.implgreen;',
  32858. ' s:=blue[1]+implgreen[2];',
  32859. ' s:=Title;',
  32860. '']);
  32861. ConvertUnit;
  32862. CheckSource('TestResourcestringUnit',
  32863. LinesToStr([ // statements
  32864. 'this.Red = "rEd";',
  32865. 'this.s = "";',
  32866. '$mod.$resourcestrings = {',
  32867. ' Blue: {',
  32868. ' org: "blue"',
  32869. ' },',
  32870. ' NotRed: {',
  32871. ' org: "notrEd"',
  32872. ' },',
  32873. ' ImplGreen: {',
  32874. ' org: "green"',
  32875. ' }',
  32876. '};',
  32877. '']),
  32878. LinesToStr([ // $mod.$main
  32879. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32880. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32881. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32882. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32883. '']));
  32884. end;
  32885. procedure TTestModule.TestResourcestringImplementation;
  32886. begin
  32887. StartUnit(false);
  32888. Add([
  32889. 'interface',
  32890. 'implementation',
  32891. 'resourcestring',
  32892. ' ImplRed = ''red'';']);
  32893. ConvertUnit;
  32894. CheckSource('TestResourcestringImplementation',
  32895. LinesToStr([ // intf statements
  32896. 'var $impl = $mod.$impl;']),
  32897. LinesToStr([ // $mod.$init
  32898. '']),
  32899. LinesToStr([ // impl statements
  32900. '$mod.$resourcestrings = {',
  32901. ' ImplRed: {',
  32902. ' org: "red"',
  32903. ' }',
  32904. '};',
  32905. '']));
  32906. end;
  32907. procedure TTestModule.TestAttributes_Members;
  32908. begin
  32909. WithTypeInfo:=true;
  32910. StartProgram(false);
  32911. Add([
  32912. '{$modeswitch PrefixedAttributes}',
  32913. 'type',
  32914. ' TObject = class',
  32915. ' constructor Create;',
  32916. ' end;',
  32917. ' TCustomAttribute = class',
  32918. ' constructor Create(Id: word);',
  32919. ' end;',
  32920. ' [Missing]',
  32921. ' TBird = class',
  32922. ' published',
  32923. ' [Tcustom]',
  32924. ' FField: word;',
  32925. ' [tcustom(14)]',
  32926. ' property Size: word read FField;',
  32927. ' [Tcustom(15)]',
  32928. ' procedure Fly; virtual; abstract;',
  32929. ' end;',
  32930. ' TRec = record',
  32931. ' [Tcustom,tcustom(14)]',
  32932. ' Size: word;',
  32933. ' end;',
  32934. 'constructor TObject.Create; begin end;',
  32935. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32936. 'begin',
  32937. '']);
  32938. ConvertProgram;
  32939. CheckSource('TestAttributes_Members',
  32940. LinesToStr([ // statements
  32941. 'rtl.createClass(this, "TObject", null, function () {',
  32942. ' this.$init = function () {',
  32943. ' };',
  32944. ' this.$final = function () {',
  32945. ' };',
  32946. ' this.Create = function () {',
  32947. ' return this;',
  32948. ' };',
  32949. '});',
  32950. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32951. ' this.Create$1 = function (Id) {',
  32952. ' return this;',
  32953. ' };',
  32954. '});',
  32955. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32956. ' this.$init = function () {',
  32957. ' $mod.TObject.$init.call(this);',
  32958. ' this.FField = 0;',
  32959. ' };',
  32960. ' var $r = this.$rtti;',
  32961. ' $r.addField("FField", rtl.word, {',
  32962. ' attr: [$mod.TCustomAttribute, "Create"]',
  32963. ' });',
  32964. ' $r.addProperty(',
  32965. ' "Size",',
  32966. ' 0,',
  32967. ' rtl.word,',
  32968. ' "FField",',
  32969. ' "",',
  32970. ' {',
  32971. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  32972. ' }',
  32973. ' );',
  32974. ' $r.addMethod("Fly", 0, [], null, 0, {',
  32975. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  32976. ' });',
  32977. '});',
  32978. 'rtl.recNewT(this, "TRec", function () {',
  32979. ' this.Size = 0;',
  32980. ' this.$eq = function (b) {',
  32981. ' return this.Size === b.Size;',
  32982. ' };',
  32983. ' this.$assign = function (s) {',
  32984. ' this.Size = s.Size;',
  32985. ' return this;',
  32986. ' };',
  32987. ' var $r = $mod.$rtti.$Record("TRec", {});',
  32988. ' $r.addField("Size", rtl.word, {',
  32989. ' attr: [',
  32990. ' $mod.TCustomAttribute,',
  32991. ' "Create",',
  32992. ' $mod.TCustomAttribute,',
  32993. ' "Create$1",',
  32994. ' [14]',
  32995. ' ]',
  32996. ' });',
  32997. '});',
  32998. '']),
  32999. LinesToStr([ // $mod.$main
  33000. '']));
  33001. end;
  33002. procedure TTestModule.TestAttributes_Types;
  33003. begin
  33004. WithTypeInfo:=true;
  33005. StartProgram(false);
  33006. Add([
  33007. '{$modeswitch PrefixedAttributes}',
  33008. 'type',
  33009. ' TObject = class',
  33010. ' constructor Create(Id: word);',
  33011. ' end;',
  33012. ' TCustomAttribute = class',
  33013. ' end;',
  33014. ' [TCustom(1)]',
  33015. ' TMyClass = class',
  33016. ' end;',
  33017. ' [TCustom(11)]',
  33018. ' TMyDescendant = class(TMyClass)',
  33019. ' end;',
  33020. ' [TCustom(2)]',
  33021. ' TRec = record',
  33022. ' end;',
  33023. ' [TCustom(3)]',
  33024. ' TInt = type word;',
  33025. 'constructor TObject.Create(Id: word);',
  33026. 'begin',
  33027. 'end;',
  33028. 'var p: pointer;',
  33029. 'begin',
  33030. ' p:=typeinfo(TMyClass);',
  33031. ' p:=typeinfo(TRec);',
  33032. ' p:=typeinfo(TInt);',
  33033. '']);
  33034. ConvertProgram;
  33035. CheckSource('TestAttributes_Types',
  33036. LinesToStr([ // statements
  33037. 'rtl.createClass(this, "TObject", null, function () {',
  33038. ' this.$init = function () {',
  33039. ' };',
  33040. ' this.$final = function () {',
  33041. ' };',
  33042. ' this.Create = function (Id) {',
  33043. ' return this;',
  33044. ' };',
  33045. '});',
  33046. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33047. '});',
  33048. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  33049. ' var $r = this.$rtti;',
  33050. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  33051. '});',
  33052. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  33053. ' var $r = this.$rtti;',
  33054. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  33055. '});',
  33056. 'rtl.recNewT(this, "TRec", function () {',
  33057. ' this.$eq = function (b) {',
  33058. ' return true;',
  33059. ' };',
  33060. ' this.$assign = function (s) {',
  33061. ' return this;',
  33062. ' };',
  33063. ' $mod.$rtti.$Record("TRec", {',
  33064. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  33065. ' });',
  33066. '});',
  33067. 'this.$rtti.$inherited("TInt", rtl.word, {',
  33068. ' attr: [this.TCustomAttribute, "Create", [3]]',
  33069. '});',
  33070. 'this.p = null;',
  33071. '']),
  33072. LinesToStr([ // $mod.$main
  33073. '$mod.p = $mod.$rtti["TMyClass"];',
  33074. '$mod.p = $mod.$rtti["TRec"];',
  33075. '$mod.p = $mod.$rtti["TInt"];',
  33076. '']));
  33077. end;
  33078. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  33079. begin
  33080. WithTypeInfo:=true;
  33081. StartProgram(false);
  33082. Add([
  33083. '{$modeswitch PrefixedAttributes}',
  33084. 'type',
  33085. ' TObject = class',
  33086. ' constructor Create;',
  33087. ' end;',
  33088. ' TCustomAttribute = class',
  33089. ' end;',
  33090. ' THelper = class helper for TCustomAttribute',
  33091. ' constructor Create(Id: word);',
  33092. ' end;',
  33093. ' [TCustom(3)]',
  33094. ' TMyInt = word;',
  33095. 'constructor TObject.Create; begin end;',
  33096. 'constructor THelper.Create(Id: word); begin end;',
  33097. 'begin',
  33098. ' if typeinfo(TMyInt)=nil then ;']);
  33099. ConvertProgram;
  33100. end;
  33101. procedure TTestModule.TestAttributes_InterfacesList;
  33102. begin
  33103. WithTypeInfo:=true;
  33104. StartProgram(false);
  33105. Add([
  33106. '{$mode Delphi}',
  33107. 'type',
  33108. ' TObject = class',
  33109. ' constructor Create;',
  33110. ' end;',
  33111. ' IInterface = interface end;',
  33112. ' TCustomAttribute = class',
  33113. ' end;',
  33114. ' Red = class(TCustomAttribute);',
  33115. ' Blue = class(TCustomAttribute);',
  33116. ' [Red]',
  33117. ' IBird<T> = interface',
  33118. ' procedure Fly;',
  33119. ' end;',
  33120. ' [Blue]',
  33121. ' IEagle = interface(IBird<Word>)',
  33122. ' procedure Dive;',
  33123. ' end;',
  33124. ' TAnt = class(TObject, IEagle)',
  33125. ' procedure Fly; virtual; abstract;',
  33126. ' procedure Dive; virtual; abstract;',
  33127. ' end;',
  33128. 'constructor TObject.Create;',
  33129. 'begin',
  33130. 'end;',
  33131. 'begin',
  33132. '']);
  33133. ConvertProgram;
  33134. CheckSource('TestAttributes_InterfacesList',
  33135. LinesToStr([ // statements
  33136. '$mod.$rtti.$Interface("IBird<System.Word>");',
  33137. 'rtl.createClass(this, "TObject", null, function () {',
  33138. ' this.$init = function () {',
  33139. ' };',
  33140. ' this.$final = function () {',
  33141. ' };',
  33142. ' this.Create = function () {',
  33143. ' return this;',
  33144. ' };',
  33145. '});',
  33146. 'rtl.createInterface(',
  33147. ' this,',
  33148. ' "IInterface",',
  33149. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  33150. ' [],',
  33151. ' null,',
  33152. ' function () {',
  33153. ' this.$kind = "com";',
  33154. ' }',
  33155. ');',
  33156. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33157. '});',
  33158. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  33159. '});',
  33160. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  33161. '});',
  33162. 'rtl.createInterface(',
  33163. ' this,',
  33164. ' "IBird$G1",',
  33165. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  33166. ' ["Fly"],',
  33167. ' this.IInterface,',
  33168. ' function () {',
  33169. ' var $r = this.$rtti;',
  33170. ' $r.addMethod("Fly", 0, []);',
  33171. ' $r.attr = [$mod.Red, "Create"];',
  33172. ' },',
  33173. ' "IBird<System.Word>"',
  33174. ');',
  33175. 'rtl.createInterface(',
  33176. ' this,',
  33177. ' "IEagle",',
  33178. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  33179. ' ["Dive"],',
  33180. ' this.IBird$G1,',
  33181. ' function () {',
  33182. ' var $r = this.$rtti;',
  33183. ' $r.addMethod("Dive", 0, []);',
  33184. ' $r.attr = [$mod.Blue, "Create"];',
  33185. ' }',
  33186. ');',
  33187. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  33188. ' rtl.addIntf(this, $mod.IEagle);',
  33189. '});',
  33190. '']),
  33191. LinesToStr([ // $mod.$main
  33192. '']));
  33193. end;
  33194. procedure TTestModule.TestAssert;
  33195. begin
  33196. StartProgram(false);
  33197. Add([
  33198. 'procedure DoIt;',
  33199. 'var',
  33200. ' b: boolean;',
  33201. ' s: string;',
  33202. 'begin',
  33203. ' {$Assertions on}',
  33204. ' Assert(b);',
  33205. 'end;',
  33206. 'begin',
  33207. ' DoIt;',
  33208. '']);
  33209. ConvertProgram;
  33210. CheckSource('TestAssert',
  33211. LinesToStr([ // statements
  33212. 'this.DoIt = function () {',
  33213. ' var b = false;',
  33214. ' var s = "";',
  33215. ' if (!b) throw "assert failed";',
  33216. '};',
  33217. '']),
  33218. LinesToStr([ // $mod.$main
  33219. '$mod.DoIt();',
  33220. '']));
  33221. end;
  33222. procedure TTestModule.TestAssert_SysUtils;
  33223. begin
  33224. AddModuleWithIntfImplSrc('SysUtils.pas',
  33225. LinesToStr([
  33226. 'type',
  33227. ' TObject = class',
  33228. ' constructor Create;',
  33229. ' end;',
  33230. ' EAssertionFailed = class',
  33231. ' constructor Create(s: string);',
  33232. ' end;',
  33233. '']),
  33234. LinesToStr([
  33235. 'constructor TObject.Create;',
  33236. 'begin end;',
  33237. 'constructor EAssertionFailed.Create(s: string);',
  33238. 'begin end;',
  33239. '']) );
  33240. StartProgram(true);
  33241. Add([
  33242. 'uses sysutils;',
  33243. 'procedure DoIt;',
  33244. 'var',
  33245. ' b: boolean;',
  33246. ' s: string;',
  33247. 'begin',
  33248. ' {$Assertions on}',
  33249. ' Assert(b);',
  33250. ' Assert(b,''msg'');',
  33251. 'end;',
  33252. 'begin',
  33253. ' DoIt;',
  33254. '']);
  33255. ConvertProgram;
  33256. CheckSource('TestAssert_SysUtils',
  33257. LinesToStr([ // statements
  33258. 'this.DoIt = function () {',
  33259. ' var b = false;',
  33260. ' var s = "";',
  33261. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  33262. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  33263. '};',
  33264. '']),
  33265. LinesToStr([ // $mod.$main
  33266. '$mod.DoIt();',
  33267. '']));
  33268. end;
  33269. procedure TTestModule.TestObjectChecks;
  33270. begin
  33271. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  33272. StartProgram(false);
  33273. Add([
  33274. 'type',
  33275. ' TObject = class',
  33276. ' procedure DoIt;',
  33277. ' end;',
  33278. ' TClass = class of tobject;',
  33279. ' TBird = class',
  33280. ' end;',
  33281. ' TBirdClass = class of TBird;',
  33282. 'var',
  33283. ' o : TObject;',
  33284. ' c: TClass;',
  33285. ' b: TBird;',
  33286. ' bc: TBirdClass;',
  33287. 'procedure TObject.DoIt;',
  33288. 'begin',
  33289. ' b:=TBird(o);',
  33290. 'end;',
  33291. 'begin',
  33292. ' o.DoIt;',
  33293. ' b:=TBird(o);',
  33294. ' bc:=TBirdClass(c);',
  33295. '']);
  33296. ConvertProgram;
  33297. CheckSource('TestCheckMethodCall',
  33298. LinesToStr([ // statements
  33299. 'rtl.createClass(this, "TObject", null, function () {',
  33300. ' this.$init = function () {',
  33301. ' };',
  33302. ' this.$final = function () {',
  33303. ' };',
  33304. ' this.DoIt = function () {',
  33305. ' rtl.checkMethodCall(this,$mod.TObject);',
  33306. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  33307. ' };',
  33308. '});',
  33309. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33310. '});',
  33311. 'this.o = null;',
  33312. 'this.c = null;',
  33313. 'this.b = null;',
  33314. 'this.bc = null;',
  33315. '']),
  33316. LinesToStr([ // $mod.$main
  33317. '$mod.o.DoIt();',
  33318. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  33319. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  33320. '']));
  33321. end;
  33322. procedure TTestModule.TestOverflowChecks_Int;
  33323. begin
  33324. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  33325. StartProgram(false);
  33326. Add([
  33327. 'procedure DoIt;',
  33328. 'var',
  33329. ' b: byte;',
  33330. ' n: nativeint;',
  33331. ' u: nativeuint;',
  33332. ' c: currency;',
  33333. 'begin',
  33334. ' n:=n+n;',
  33335. ' n:=n-n;',
  33336. ' n:=n+b;',
  33337. ' n:=b-n;',
  33338. ' n:=n*n;',
  33339. ' n:=n*u;',
  33340. ' c:=c+b;',
  33341. ' c:=b+c;',
  33342. ' c:=c*b;',
  33343. ' c:=b*c;',
  33344. 'end;',
  33345. 'begin',
  33346. '']);
  33347. ConvertProgram;
  33348. CheckSource('TestOverflowChecks_Int',
  33349. LinesToStr([ // statements
  33350. 'this.DoIt = function () {',
  33351. ' var b = 0;',
  33352. ' var n = 0;',
  33353. ' var u = 0;',
  33354. ' var c = 0;',
  33355. ' n = rtl.oc(n + n);',
  33356. ' n = rtl.oc(n - n);',
  33357. ' n = rtl.oc(n + b);',
  33358. ' n = rtl.oc(b - n);',
  33359. ' n = rtl.oc(n * n);',
  33360. ' n = rtl.oc(n * u);',
  33361. ' c = rtl.oc(c + (b * 10000));',
  33362. ' c = rtl.oc((b * 10000) + c);',
  33363. ' c = rtl.oc(c * b);',
  33364. ' c = rtl.oc(b * c);',
  33365. '};',
  33366. '']),
  33367. LinesToStr([ // $mod.$main
  33368. '']));
  33369. end;
  33370. procedure TTestModule.TestRangeChecks_AssignInt;
  33371. begin
  33372. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33373. StartProgram(false);
  33374. Add([
  33375. '{$R+}',
  33376. 'var',
  33377. ' b: byte = 2;',
  33378. ' w: word = 3;',
  33379. 'procedure DoIt(p: byte);',
  33380. 'begin',
  33381. ' b:=w;',
  33382. ' b+=w;',
  33383. ' b:=1;',
  33384. 'end;',
  33385. '{$R-}',
  33386. 'procedure DoSome;',
  33387. 'begin',
  33388. ' DoIt(w);',
  33389. ' b:=w;',
  33390. ' b:=2;',
  33391. 'end;',
  33392. 'begin',
  33393. '{$R+}',
  33394. '']);
  33395. ConvertProgram;
  33396. CheckSource('TestRangeChecks_AssignInt',
  33397. LinesToStr([ // statements
  33398. 'this.b = 2;',
  33399. 'this.w = 3;',
  33400. 'this.DoIt = function (p) {',
  33401. ' rtl.rc(p, 0, 255);',
  33402. ' $mod.b = rtl.rc($mod.w,0,255);',
  33403. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33404. ' $mod.b = 1;',
  33405. '};',
  33406. 'this.DoSome = function () {',
  33407. ' $mod.DoIt($mod.w);',
  33408. ' $mod.b = $mod.w;',
  33409. ' $mod.b = 2;',
  33410. '};',
  33411. '']),
  33412. LinesToStr([ // $mod.$main
  33413. '']));
  33414. end;
  33415. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33416. begin
  33417. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33418. StartProgram(false);
  33419. Add([
  33420. '{$R+}',
  33421. 'type Ten = 1..10;',
  33422. 'var',
  33423. ' b: Ten = 2;',
  33424. ' w: Ten = 3;',
  33425. 'procedure DoIt(p: Ten);',
  33426. 'begin',
  33427. ' b:=w;',
  33428. ' b+=w;',
  33429. ' b:=1;',
  33430. 'end;',
  33431. '{$R-}',
  33432. 'procedure DoSome;',
  33433. 'begin',
  33434. ' DoIt(w);',
  33435. ' b:=w;',
  33436. ' b:=2;',
  33437. 'end;',
  33438. 'begin',
  33439. '{$R+}',
  33440. '']);
  33441. ConvertProgram;
  33442. CheckSource('TestRangeChecks_AssignIntRange',
  33443. LinesToStr([ // statements
  33444. 'this.b = 2;',
  33445. 'this.w = 3;',
  33446. 'this.DoIt = function (p) {',
  33447. ' rtl.rc(p, 1, 10);',
  33448. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33449. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33450. ' $mod.b = 1;',
  33451. '};',
  33452. 'this.DoSome = function () {',
  33453. ' $mod.DoIt($mod.w);',
  33454. ' $mod.b = $mod.w;',
  33455. ' $mod.b = 2;',
  33456. '};',
  33457. '']),
  33458. LinesToStr([ // $mod.$main
  33459. '']));
  33460. end;
  33461. procedure TTestModule.TestRangeChecks_AssignEnum;
  33462. begin
  33463. StartProgram(false);
  33464. Add([
  33465. '{$R+}',
  33466. 'type TEnum = (red,green);',
  33467. 'var',
  33468. ' e: TEnum = red;',
  33469. 'procedure DoIt(p: TEnum);',
  33470. 'begin',
  33471. ' e:=p;',
  33472. ' p:=TEnum(0);',
  33473. ' p:=succ(e);',
  33474. 'end;',
  33475. '{$R-}',
  33476. 'procedure DoSome;',
  33477. 'begin',
  33478. ' DoIt(e);',
  33479. ' e:=TEnum(1);',
  33480. ' e:=pred(e);',
  33481. 'end;',
  33482. 'begin',
  33483. '{$R+}',
  33484. '']);
  33485. ConvertProgram;
  33486. CheckSource('TestRangeChecks_AssignEnum',
  33487. LinesToStr([ // statements
  33488. 'this.TEnum = {',
  33489. ' "0": "red",',
  33490. ' red: 0,',
  33491. ' "1": "green",',
  33492. ' green: 1',
  33493. '};',
  33494. 'this.e = this.TEnum.red;',
  33495. 'this.DoIt = function (p) {',
  33496. ' rtl.rc(p, 0, 1);',
  33497. ' $mod.e = rtl.rc(p, 0, 1);',
  33498. ' p = 0;',
  33499. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33500. '};',
  33501. 'this.DoSome = function () {',
  33502. ' $mod.DoIt($mod.e);',
  33503. ' $mod.e = 1;',
  33504. ' $mod.e = $mod.e - 1;',
  33505. '};',
  33506. '']),
  33507. LinesToStr([ // $mod.$main
  33508. '']));
  33509. end;
  33510. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33511. begin
  33512. StartProgram(false);
  33513. Add([
  33514. '{$R+}',
  33515. 'type',
  33516. ' TEnum = (red,green);',
  33517. ' TEnumRg = red..green;',
  33518. 'var',
  33519. ' e: TEnumRg = red;',
  33520. 'procedure DoIt(p: TEnumRg);',
  33521. 'begin',
  33522. ' e:=p;',
  33523. ' p:=TEnumRg(0);',
  33524. ' p:=succ(e);',
  33525. 'end;',
  33526. '{$R-}',
  33527. 'procedure DoSome;',
  33528. 'begin',
  33529. ' DoIt(e);',
  33530. ' e:=TEnum(1);',
  33531. ' e:=pred(e);',
  33532. 'end;',
  33533. 'begin',
  33534. '{$R+}',
  33535. '']);
  33536. ConvertProgram;
  33537. CheckSource('TestRangeChecks_AssignEnumRange',
  33538. LinesToStr([ // statements
  33539. 'this.TEnum = {',
  33540. ' "0": "red",',
  33541. ' red: 0,',
  33542. ' "1": "green",',
  33543. ' green: 1',
  33544. '};',
  33545. 'this.e = this.TEnum.red;',
  33546. 'this.DoIt = function (p) {',
  33547. ' rtl.rc(p, 0, 1);',
  33548. ' $mod.e = rtl.rc(p, 0, 1);',
  33549. ' p = 0;',
  33550. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33551. '};',
  33552. 'this.DoSome = function () {',
  33553. ' $mod.DoIt($mod.e);',
  33554. ' $mod.e = 1;',
  33555. ' $mod.e = $mod.e - 1;',
  33556. '};',
  33557. '']),
  33558. LinesToStr([ // $mod.$main
  33559. '']));
  33560. end;
  33561. procedure TTestModule.TestRangeChecks_AssignChar;
  33562. begin
  33563. StartProgram(false);
  33564. Add([
  33565. '{$R+}',
  33566. 'type',
  33567. ' TLetter = AnsiChar;',
  33568. 'var',
  33569. ' b: TLetter = ''2'';',
  33570. ' w: TLetter = ''3'';',
  33571. 'procedure DoIt(p: TLetter);',
  33572. 'begin',
  33573. ' b:=w;',
  33574. ' b:=''1'';',
  33575. 'end;',
  33576. '{$R-}',
  33577. 'procedure DoSome;',
  33578. 'begin',
  33579. ' DoIt(w);',
  33580. ' b:=w;',
  33581. ' b:=''2'';',
  33582. 'end;',
  33583. 'begin',
  33584. '{$R+}',
  33585. '']);
  33586. ConvertProgram;
  33587. CheckSource('TestRangeChecks_AssignChar',
  33588. LinesToStr([ // statements
  33589. 'this.b = "2";',
  33590. 'this.w = "3";',
  33591. 'this.DoIt = function (p) {',
  33592. ' rtl.rcc(p, 0, 65535);',
  33593. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33594. ' $mod.b = "1";',
  33595. '};',
  33596. 'this.DoSome = function () {',
  33597. ' $mod.DoIt($mod.w);',
  33598. ' $mod.b = $mod.w;',
  33599. ' $mod.b = "2";',
  33600. '};',
  33601. '']),
  33602. LinesToStr([ // $mod.$main
  33603. '']));
  33604. end;
  33605. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33606. begin
  33607. StartProgram(false);
  33608. Add([
  33609. '{$R+}',
  33610. 'type TDigit = ''0''..''9'';',
  33611. 'var',
  33612. ' b: TDigit = ''2'';',
  33613. ' w: TDigit = ''3'';',
  33614. 'procedure DoIt(p: TDigit);',
  33615. 'begin',
  33616. ' b:=w;',
  33617. ' b:=''1'';',
  33618. 'end;',
  33619. '{$R-}',
  33620. 'procedure DoSome;',
  33621. 'begin',
  33622. ' DoIt(w);',
  33623. ' b:=w;',
  33624. ' b:=''2'';',
  33625. 'end;',
  33626. 'begin',
  33627. '{$R+}',
  33628. '']);
  33629. ConvertProgram;
  33630. CheckSource('TestRangeChecks_AssignCharRange',
  33631. LinesToStr([ // statements
  33632. 'this.b = "2";',
  33633. 'this.w = "3";',
  33634. 'this.DoIt = function (p) {',
  33635. ' rtl.rcc(p, 48, 57);',
  33636. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33637. ' $mod.b = "1";',
  33638. '};',
  33639. 'this.DoSome = function () {',
  33640. ' $mod.DoIt($mod.w);',
  33641. ' $mod.b = $mod.w;',
  33642. ' $mod.b = "2";',
  33643. '};',
  33644. '']),
  33645. LinesToStr([ // $mod.$main
  33646. '']));
  33647. end;
  33648. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33649. begin
  33650. StartProgram(false);
  33651. Add([
  33652. '{$R+}',
  33653. 'type',
  33654. ' Ten = 1..10;',
  33655. ' TArr = array of Ten;',
  33656. ' TArrArr = array of TArr;',
  33657. ' TArrByte = array[byte] of Ten;',
  33658. ' TArrChar = array[''0''..''9''] of Ten;',
  33659. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33660. ' TObject = class',
  33661. ' A: TArr;',
  33662. ' end;',
  33663. 'procedure DoIt;',
  33664. 'var',
  33665. ' Arr: TArr;',
  33666. ' ArrArr: TArrArr;',
  33667. ' ArrByte: TArrByte;',
  33668. ' ArrChar: TArrChar;',
  33669. ' ArrByteChar: TArrByteChar;',
  33670. ' i: Ten;',
  33671. ' c: AnsiChar;',
  33672. ' o: tobject;',
  33673. 'begin',
  33674. ' i:=Arr[1];',
  33675. ' i:=ArrByteChar[1,''2''];',
  33676. ' Arr[1]:=Arr[1];',
  33677. ' Arr[i]:=Arr[i];',
  33678. ' ArrByte[3]:=ArrByte[3];',
  33679. ' ArrByte[i]:=ArrByte[i];',
  33680. ' ArrChar[''5'']:=ArrChar[''5''];',
  33681. ' ArrChar[c]:=ArrChar[c];',
  33682. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  33683. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  33684. ' o.a[i]:=o.a[i];',
  33685. 'end;',
  33686. 'begin',
  33687. '']);
  33688. ConvertProgram;
  33689. CheckSource('TestRangeChecks_ArrayIndex',
  33690. LinesToStr([ // statements
  33691. 'this.TArrByteChar$clone = function (a) {',
  33692. ' var b = [];',
  33693. ' b.length = 256;',
  33694. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  33695. ' return b;',
  33696. '};',
  33697. 'rtl.createClass(this, "TObject", null, function () {',
  33698. ' this.$init = function () {',
  33699. ' this.A = [];',
  33700. ' };',
  33701. ' this.$final = function () {',
  33702. ' this.A = undefined;',
  33703. ' };',
  33704. '});',
  33705. 'this.DoIt = function () {',
  33706. ' var Arr = [];',
  33707. ' var ArrArr = [];',
  33708. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  33709. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  33710. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  33711. ' var i = 0;',
  33712. ' var c = "";',
  33713. ' var o = null;',
  33714. ' i = rtl.rc(Arr[1], 1, 10);',
  33715. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  33716. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  33717. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  33718. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  33719. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  33720. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  33721. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  33722. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  33723. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  33724. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  33725. '};',
  33726. '']),
  33727. LinesToStr([ // $mod.$main
  33728. '']));
  33729. end;
  33730. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  33731. begin
  33732. StartProgram(false);
  33733. Add([
  33734. '{$R+}',
  33735. 'type',
  33736. ' Ten = 1..10;',
  33737. ' TRec = record x: Ten end;',
  33738. ' TArr = array of TRec;',
  33739. ' TArrArr = array of TArr;',
  33740. ' TObject = class',
  33741. ' A: TArr;',
  33742. ' end;',
  33743. 'procedure DoIt;',
  33744. 'var',
  33745. ' Arr: TArr;',
  33746. ' ArrArr: TArrArr;',
  33747. ' i: Ten;',
  33748. ' o: tobject;',
  33749. 'begin',
  33750. ' Arr[1]:=Arr[1];',
  33751. ' Arr[i]:=Arr[i+1];',
  33752. ' o.a[i]:=o.a[i+2];',
  33753. 'end;',
  33754. 'begin',
  33755. '']);
  33756. ConvertProgram;
  33757. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  33758. LinesToStr([ // statements
  33759. 'rtl.recNewT(this, "TRec", function () {',
  33760. ' this.x = 0;',
  33761. ' this.$eq = function (b) {',
  33762. ' return this.x === b.x;',
  33763. ' };',
  33764. ' this.$assign = function (s) {',
  33765. ' this.x = s.x;',
  33766. ' return this;',
  33767. ' };',
  33768. '});',
  33769. 'rtl.createClass(this, "TObject", null, function () {',
  33770. ' this.$init = function () {',
  33771. ' this.A = [];',
  33772. ' };',
  33773. ' this.$final = function () {',
  33774. ' this.A = undefined;',
  33775. ' };',
  33776. '});',
  33777. 'this.DoIt = function () {',
  33778. ' var Arr = [];',
  33779. ' var ArrArr = [];',
  33780. ' var i = 0;',
  33781. ' var o = null;',
  33782. ' Arr[1].$assign(Arr[1]);',
  33783. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  33784. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  33785. '};',
  33786. '']),
  33787. LinesToStr([ // $mod.$main
  33788. '']));
  33789. end;
  33790. procedure TTestModule.TestRangeChecks_StringIndex;
  33791. begin
  33792. StartProgram(false);
  33793. Add([
  33794. 'type',
  33795. ' TObject = class',
  33796. ' S: string;',
  33797. ' end;',
  33798. '{$R+}',
  33799. 'procedure DoIt(var h: string);',
  33800. 'var',
  33801. ' s: string;',
  33802. ' i: longint;',
  33803. ' c: AnsiChar;',
  33804. ' o: tobject;',
  33805. 'begin',
  33806. ' c:=s[1];',
  33807. ' s[i]:=s[i];',
  33808. ' h[i]:=h[i];',
  33809. ' c:=o.s[i];',
  33810. ' o.s[i]:=c;',
  33811. 'end;',
  33812. 'begin',
  33813. '']);
  33814. ConvertProgram;
  33815. CheckSource('TestRangeChecks_StringIndex',
  33816. LinesToStr([ // statements
  33817. 'rtl.createClass(this, "TObject", null, function () {',
  33818. ' this.$init = function () {',
  33819. ' this.S = "";',
  33820. ' };',
  33821. ' this.$final = function () {',
  33822. ' };',
  33823. '});',
  33824. 'this.DoIt = function (h) {',
  33825. ' var s = "";',
  33826. ' var i = 0;',
  33827. ' var c = "";',
  33828. ' var o = null;',
  33829. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33830. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33831. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33832. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33833. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33834. '};',
  33835. '']),
  33836. LinesToStr([ // $mod.$main
  33837. '']));
  33838. end;
  33839. procedure TTestModule.TestRangeChecks_TypecastInt;
  33840. begin
  33841. StartProgram(false);
  33842. Add([
  33843. '{$R+}',
  33844. 'var',
  33845. ' i: nativeint;',
  33846. ' b: byte;',
  33847. ' sh: shortint;',
  33848. ' w: word;',
  33849. ' sm: smallint;',
  33850. ' lw: longword;',
  33851. ' li: longint;',
  33852. 'begin',
  33853. ' b:=12+byte(i);',
  33854. ' sh:=12+shortint(i);',
  33855. ' w:=12+word(i);',
  33856. ' sm:=12+smallint(i);',
  33857. ' lw:=12+longword(i);',
  33858. ' li:=12+longint(i);',
  33859. '']);
  33860. ConvertProgram;
  33861. CheckSource('TestRangeChecks_TypecastInt',
  33862. LinesToStr([
  33863. 'this.i = 0;',
  33864. 'this.b = 0;',
  33865. 'this.sh = 0;',
  33866. 'this.w = 0;',
  33867. 'this.sm = 0;',
  33868. 'this.lw = 0;',
  33869. 'this.li = 0;',
  33870. '']),
  33871. LinesToStr([
  33872. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33873. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33874. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33875. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33876. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33877. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33878. '']));
  33879. end;
  33880. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33881. begin
  33882. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33883. StartProgram(false);
  33884. Add([
  33885. '{$modeswitch typehelpers}',
  33886. '{$R+}',
  33887. 'type',
  33888. ' TObject = class',
  33889. ' FSize: byte;',
  33890. ' property Size: byte read FSize;',
  33891. ' end;',
  33892. ' THelper = type helper for byte',
  33893. ' procedure SetIt(w: word);',
  33894. ' end;',
  33895. 'procedure THelper.SetIt(w: word);',
  33896. 'begin',
  33897. ' Self:=w;',
  33898. 'end;',
  33899. 'function GetIt: byte;',
  33900. 'begin',
  33901. ' Result.SetIt(2);',
  33902. 'end;',
  33903. 'var',
  33904. ' b: byte = 3;',
  33905. ' o: TObject;',
  33906. 'begin',
  33907. ' b.SetIt(14);',
  33908. ' with b do SetIt(15);',
  33909. ' o.Size.SetIt(16);',
  33910. '']);
  33911. ConvertProgram;
  33912. CheckSource('TestRangeChecks_AssignInt',
  33913. LinesToStr([ // statements
  33914. 'rtl.createClass(this, "TObject", null, function () {',
  33915. ' this.$init = function () {',
  33916. ' this.FSize = 0;',
  33917. ' };',
  33918. ' this.$final = function () {',
  33919. ' };',
  33920. '});',
  33921. 'rtl.createHelper(this, "THelper", null, function () {',
  33922. ' this.SetIt = function (w) {',
  33923. ' rtl.rc(w, 0, 65535);',
  33924. ' this.set(w);',
  33925. ' };',
  33926. '});',
  33927. 'this.GetIt = function () {',
  33928. ' var Result = 0;',
  33929. ' $mod.THelper.SetIt.call({',
  33930. ' get: function () {',
  33931. ' return Result;',
  33932. ' },',
  33933. ' set: function (v) {',
  33934. ' rtl.rc(v, 0, 255);',
  33935. ' Result = v;',
  33936. ' }',
  33937. ' }, 2);',
  33938. ' return Result;',
  33939. '};',
  33940. 'this.b = 3;',
  33941. 'this.o = null;',
  33942. '']),
  33943. LinesToStr([ // $mod.$main
  33944. '$mod.THelper.SetIt.call({',
  33945. ' p: $mod,',
  33946. ' get: function () {',
  33947. ' return this.p.b;',
  33948. ' },',
  33949. ' set: function (v) {',
  33950. ' rtl.rc(v, 0, 255);',
  33951. ' this.p.b = v;',
  33952. ' }',
  33953. '}, 14);',
  33954. 'var $with = $mod.b;',
  33955. '$mod.THelper.SetIt.call({',
  33956. ' get: function () {',
  33957. ' return $with;',
  33958. ' },',
  33959. ' set: function (v) {',
  33960. ' rtl.rc(v, 0, 255);',
  33961. ' $with = v;',
  33962. ' }',
  33963. '}, 15);',
  33964. '$mod.THelper.SetIt.call({',
  33965. ' p: $mod.o,',
  33966. ' get: function () {',
  33967. ' return this.p.FSize;',
  33968. ' },',
  33969. ' set: function (v) {',
  33970. ' rtl.rc(v, 0, 255);',
  33971. ' this.p.FSize = v;',
  33972. ' }',
  33973. '}, 16);',
  33974. '']));
  33975. end;
  33976. procedure TTestModule.TestAsync_Proc;
  33977. begin
  33978. StartProgram(false);
  33979. Add([
  33980. 'procedure Fly(w: word = 1); async; forward;',
  33981. 'procedure Run(w: word = 2); async;',
  33982. 'begin',
  33983. ' Fly(w);',
  33984. ' Fly;',
  33985. ' await(Fly(w));',
  33986. ' await(Fly);',
  33987. 'end;',
  33988. 'procedure Fly(w: word); ',
  33989. 'begin',
  33990. 'end;',
  33991. 'begin',
  33992. ' Run;',
  33993. ' Run(3);',
  33994. '']);
  33995. CheckResolverUnexpectedHints();
  33996. ConvertProgram;
  33997. CheckSource('TestAsync_Proc',
  33998. LinesToStr([ // statements
  33999. 'this.Run = async function (w) {',
  34000. ' $mod.Fly(w);',
  34001. ' $mod.Fly(1);',
  34002. ' await $mod.Fly(w);',
  34003. ' await $mod.Fly(1);',
  34004. '};',
  34005. 'this.Fly = async function (w) {',
  34006. '};',
  34007. '']),
  34008. LinesToStr([
  34009. '$mod.Run(2);',
  34010. '$mod.Run(3);',
  34011. '']));
  34012. end;
  34013. procedure TTestModule.TestAsync_CallResultIsPromise;
  34014. begin
  34015. StartProgram(false);
  34016. Add([
  34017. '{$modeswitch externalclass}',
  34018. 'type',
  34019. ' TObject = class',
  34020. ' end;',
  34021. ' TJSPromise = class external name ''Promise''',
  34022. ' end;',
  34023. ' TBird = class',
  34024. ' function Fly: word; async; ',
  34025. ' end;',
  34026. 'function TBird.Fly: word; async; ',
  34027. 'begin',
  34028. ' Result:=3;',
  34029. ' Fly:=4+Result;',
  34030. ' if Result=5 then ;',
  34031. ' exit(6);',
  34032. 'end;',
  34033. 'function Run: word; async;',
  34034. 'begin',
  34035. ' Result:=11+Result;',
  34036. ' inc(Result);',
  34037. 'end;',
  34038. 'var',
  34039. ' p: TJSPromise;',
  34040. ' o: TBird;',
  34041. 'begin',
  34042. ' p:=Run;',
  34043. ' p:=Run();',
  34044. ' if Run=p then ;',
  34045. ' if p=Run then ;',
  34046. ' if Run()=p then ;',
  34047. ' if p=Run() then ;',
  34048. ' p:=o.Fly;',
  34049. ' p:=o.Fly();',
  34050. ' if o.Fly=p then ;',
  34051. ' if o.Fly()=p then ;',
  34052. ' with o do begin',
  34053. ' p:=Fly;',
  34054. ' p:=Fly();',
  34055. ' if Fly=p then ;',
  34056. ' if Fly()=p then ;',
  34057. ' end;',
  34058. '']);
  34059. CheckResolverUnexpectedHints();
  34060. ConvertProgram;
  34061. CheckSource('TestAsync_CallResultIsPromise',
  34062. LinesToStr([ // statements
  34063. 'rtl.createClass(this, "TObject", null, function () {',
  34064. ' this.$init = function () {',
  34065. ' };',
  34066. ' this.$final = function () {',
  34067. ' };',
  34068. '});',
  34069. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34070. ' this.Fly = async function () {',
  34071. ' var Result = 0;',
  34072. ' Result = 3;',
  34073. ' Result = 4 + Result;',
  34074. ' if (Result === 5) ;',
  34075. ' return 6;',
  34076. ' return Result;',
  34077. ' };',
  34078. '});',
  34079. 'this.Run = async function () {',
  34080. ' var Result = 0;',
  34081. ' Result = 11 + Result;',
  34082. ' Result += 1;',
  34083. ' return Result;',
  34084. '};',
  34085. 'this.p = null;',
  34086. 'this.o = null;',
  34087. '']),
  34088. LinesToStr([
  34089. '$mod.p = $mod.Run();',
  34090. '$mod.p = $mod.Run();',
  34091. 'if ($mod.Run() === $mod.p) ;',
  34092. 'if ($mod.p === $mod.Run()) ;',
  34093. 'if ($mod.Run() === $mod.p) ;',
  34094. 'if ($mod.p === $mod.Run()) ;',
  34095. '$mod.p = $mod.o.Fly();',
  34096. '$mod.p = $mod.o.Fly();',
  34097. 'if ($mod.o.Fly() === $mod.p) ;',
  34098. 'if ($mod.o.Fly() === $mod.p) ;',
  34099. 'var $with = $mod.o;',
  34100. '$mod.p = $with.Fly();',
  34101. '$mod.p = $with.Fly();',
  34102. 'if ($with.Fly() === $mod.p) ;',
  34103. 'if ($with.Fly() === $mod.p) ;',
  34104. '']));
  34105. end;
  34106. procedure TTestModule.TestAsync_ConstructorFail;
  34107. begin
  34108. StartProgram(false);
  34109. Add([
  34110. 'type',
  34111. ' TObject = class',
  34112. ' end;',
  34113. ' TBird = class',
  34114. ' constructor Create; async;',
  34115. ' end;',
  34116. 'constructor TBird.Create; async;',
  34117. 'begin',
  34118. 'end;',
  34119. 'begin',
  34120. '']);
  34121. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  34122. ConvertProgram;
  34123. end;
  34124. procedure TTestModule.TestAsync_PropertyGetterFail;
  34125. begin
  34126. StartProgram(false);
  34127. Add([
  34128. 'type',
  34129. ' TObject = class',
  34130. ' end;',
  34131. ' TBird = class',
  34132. ' function GetSize: word; async;',
  34133. ' property Size: word read GetSize;',
  34134. ' end;',
  34135. 'function TBird.GetSize: word; async;',
  34136. 'begin',
  34137. 'end;',
  34138. 'begin',
  34139. '']);
  34140. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  34141. ConvertProgram;
  34142. end;
  34143. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  34144. begin
  34145. StartProgram(false);
  34146. Add([
  34147. 'procedure Run; async;',
  34148. 'begin',
  34149. ' await(word,1);',
  34150. 'end;',
  34151. 'begin',
  34152. '']);
  34153. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  34154. ConvertProgram;
  34155. end;
  34156. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  34157. begin
  34158. StartProgram(false);
  34159. Add([
  34160. 'type',
  34161. ' TObject = class',
  34162. ' end;',
  34163. ' TBird = class',
  34164. ' end;',
  34165. 'function Fly: TObject; async;',
  34166. 'begin',
  34167. 'end;',
  34168. 'procedure Run; async;',
  34169. 'begin',
  34170. ' await(TBird,Fly);',
  34171. 'end;',
  34172. 'begin',
  34173. '']);
  34174. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  34175. ConvertProgram;
  34176. end;
  34177. procedure TTestModule.TestAWait_OutsideAsyncFail;
  34178. begin
  34179. StartProgram(false);
  34180. Add([
  34181. 'procedure Crawl(w: double); ',
  34182. 'begin',
  34183. 'end;',
  34184. 'procedure Run(w: double);',
  34185. 'begin',
  34186. ' await(Crawl(w));',
  34187. 'end;',
  34188. 'begin',
  34189. ' Run(1);']);
  34190. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  34191. ConvertProgram;
  34192. end;
  34193. procedure TTestModule.TestAWait_IntegerFail;
  34194. begin
  34195. StartProgram(false);
  34196. Add([
  34197. 'function Run: word;',
  34198. 'begin',
  34199. 'end;',
  34200. 'procedure Fly(w: word); async;',
  34201. 'begin',
  34202. ' await(Run());',
  34203. 'end;',
  34204. 'begin',
  34205. ' Fly(1);']);
  34206. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  34207. ConvertProgram;
  34208. end;
  34209. procedure TTestModule.TestAWait_ExternalClassPromise;
  34210. begin
  34211. StartProgram(false);
  34212. Add([
  34213. '{$modeswitch externalclass}',
  34214. 'type',
  34215. ' TJSPromise = class external name ''Promise''',
  34216. ' end;',
  34217. ' TJSThenable = class external name ''Thenable''',
  34218. ' end;',
  34219. 'function Fly(w: word): TJSPromise;',
  34220. 'begin',
  34221. 'end;',
  34222. 'function Jump(w: word): word; async;',
  34223. 'begin',
  34224. 'end;',
  34225. 'function Eat(w: word): TJSPromise; async;',
  34226. 'begin',
  34227. 'end;',
  34228. 'function Run(d: double): word; async;',
  34229. 'var',
  34230. ' p: TJSPromise;',
  34231. 'begin',
  34232. ' Result:=await(word,p);', // promise needs type
  34233. ' Result:=await(word,Fly(3));', // promise needs type
  34234. ' Result:=await(Jump(4));', // async non promise must omit the type
  34235. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  34236. ' Result:=await(word,Eat(6));', // promise needs type
  34237. 'end;',
  34238. 'begin',
  34239. '']);
  34240. ConvertProgram;
  34241. CheckSource('TestAWait_ExternalClassPromise',
  34242. LinesToStr([ // statements
  34243. 'this.Fly = function (w) {',
  34244. ' var Result = null;',
  34245. ' return Result;',
  34246. '};',
  34247. 'this.Jump = async function (w) {',
  34248. ' var Result = 0;',
  34249. ' return Result;',
  34250. '};',
  34251. 'this.Eat = async function (w) {',
  34252. ' var Result = null;',
  34253. ' return Result;',
  34254. '};',
  34255. 'this.Run = async function (d) {',
  34256. ' var Result = 0;',
  34257. ' var p = null;',
  34258. ' Result = await p;',
  34259. ' Result = await $mod.Fly(3);',
  34260. ' Result = await $mod.Jump(4);',
  34261. ' Result = await $mod.Jump(5);',
  34262. ' Result = await $mod.Eat(6);',
  34263. ' return Result;',
  34264. '};',
  34265. '']),
  34266. LinesToStr([
  34267. ]));
  34268. CheckResolverUnexpectedHints();
  34269. end;
  34270. procedure TTestModule.TestAWait_JSValue;
  34271. begin
  34272. StartProgram(false);
  34273. Add([
  34274. '{$modeswitch externalclass}',
  34275. 'type',
  34276. ' TJSPromise = class external name ''Promise''',
  34277. ' end;',
  34278. 'function Fly(w: word): jsvalue; async;',
  34279. 'begin',
  34280. 'end;',
  34281. 'function Run(d: jsvalue; var e): word; async;',
  34282. 'begin',
  34283. ' Result:=await(word,d);', // promise needs type
  34284. ' d:=await(Fly(4));', // async non promise must omit the type
  34285. ' Result:=await(word,e);', // promise needs type
  34286. 'end;',
  34287. 'begin',
  34288. '']);
  34289. ConvertProgram;
  34290. CheckSource('TestAWait_JSValue',
  34291. LinesToStr([ // statements
  34292. 'this.Fly = async function (w) {',
  34293. ' var Result = undefined;',
  34294. ' return Result;',
  34295. '};',
  34296. 'this.Run = async function (d, e) {',
  34297. ' var Result = 0;',
  34298. ' Result = await d;',
  34299. ' d = await $mod.Fly(4);',
  34300. ' Result = await e.get();',
  34301. ' return Result;',
  34302. '};',
  34303. '']),
  34304. LinesToStr([
  34305. ]));
  34306. CheckResolverUnexpectedHints();
  34307. end;
  34308. procedure TTestModule.TestAWait_Result;
  34309. begin
  34310. StartProgram(false);
  34311. Add([
  34312. '{$modeswitch externalclass}',
  34313. 'type',
  34314. ' TJSPromise = class external name ''Promise''',
  34315. ' end;',
  34316. 'function Crawl(d: double = 1.3): TJSPromise; ',
  34317. 'begin',
  34318. 'end;',
  34319. 'function Run(d: double = 1.6): word; async;',
  34320. 'begin',
  34321. ' Result:=await(word,Crawl);',
  34322. ' Result:=await(word,Crawl(4.5));',
  34323. ' Result:=await(Run);',
  34324. ' Result:=await(Run(6.7));',
  34325. 'end;',
  34326. 'begin',
  34327. ' Run(1);']);
  34328. ConvertProgram;
  34329. CheckSource('TestAWait_Result',
  34330. LinesToStr([ // statements
  34331. 'this.Crawl = function (d) {',
  34332. ' var Result = null;',
  34333. ' return Result;',
  34334. '};',
  34335. 'this.Run = async function (d) {',
  34336. ' var Result = 0;',
  34337. ' Result = await $mod.Crawl(1.3);',
  34338. ' Result = await $mod.Crawl(4.5);',
  34339. ' Result = await $mod.Run(1.6);',
  34340. ' Result = await $mod.Run(6.7);',
  34341. ' return Result;',
  34342. '};',
  34343. '']),
  34344. LinesToStr([
  34345. '$mod.Run(1);'
  34346. ]));
  34347. CheckResolverUnexpectedHints();
  34348. end;
  34349. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  34350. begin
  34351. StartProgram(false);
  34352. Add([
  34353. '{$mode objfpc}',
  34354. '{$modeswitch externalclass}',
  34355. 'type',
  34356. ' TJSPromise = class external name ''Promise''',
  34357. ' end;',
  34358. 'function Run: TJSPromise; async;',
  34359. 'begin',
  34360. 'end;',
  34361. 'procedure Fly(w: word); async;',
  34362. 'begin',
  34363. ' await(Run());',
  34364. 'end;',
  34365. 'begin',
  34366. ' Fly(1);']);
  34367. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  34368. nWrongNumberOfParametersForCallTo);
  34369. ConvertProgram;
  34370. end;
  34371. procedure TTestModule.TestAsync_AnonymousProc;
  34372. begin
  34373. StartProgram(false);
  34374. Add([
  34375. '{$mode objfpc}',
  34376. '{$modeswitch externalclass}',
  34377. 'type',
  34378. ' TJSPromise = class external name ''Promise''',
  34379. ' end;',
  34380. 'type',
  34381. ' TFunc = reference to function(x: double): word; async;',
  34382. 'function Crawl(d: double = 1.3): word; async;',
  34383. 'begin',
  34384. 'end;',
  34385. 'var Func: TFunc;',
  34386. 'begin',
  34387. ' Func:=function(c:double):word async begin',
  34388. ' Result:=await(Crawl(c));',
  34389. ' end;',
  34390. ' Func:=function(c:double):word async assembler asm',
  34391. ' end;',
  34392. '']);
  34393. ConvertProgram;
  34394. CheckSource('TestAsync_AnonymousProc',
  34395. LinesToStr([ // statements
  34396. 'this.Crawl = async function (d) {',
  34397. ' var Result = 0;',
  34398. ' return Result;',
  34399. '};',
  34400. 'this.Func = null;',
  34401. '']),
  34402. LinesToStr([
  34403. '$mod.Func = async function (c) {',
  34404. ' var Result = 0;',
  34405. ' Result = await $mod.Crawl(c);',
  34406. ' return Result;',
  34407. '};',
  34408. '$mod.Func = async function (c) {',
  34409. '};',
  34410. '']));
  34411. CheckResolverUnexpectedHints();
  34412. end;
  34413. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34414. begin
  34415. StartProgram(false);
  34416. Add([
  34417. '{$mode objfpc}',
  34418. '{$modeswitch externalclass}',
  34419. 'type',
  34420. ' TJSPromise = class external name ''Promise''',
  34421. ' end;',
  34422. ' TObject = class',
  34423. ' public',
  34424. ' procedure Fly(Prom: TJSPromise);',
  34425. ' end;',
  34426. ' TFunc = reference to procedure(Bird: TObject);',
  34427. 'procedure TObject.Fly(Prom: TJSPromise);',
  34428. 'begin',
  34429. 'end;',
  34430. 'function Crawl: jsvalue; async;',
  34431. 'begin',
  34432. 'end;',
  34433. 'procedure Add(Func: TFunc);',
  34434. 'begin',
  34435. 'end;',
  34436. 'begin',
  34437. ' Add(procedure(Bird: TObject)',
  34438. ' begin',
  34439. ' Bird.Fly(Crawl());',
  34440. ' end);',
  34441. '']);
  34442. ConvertProgram;
  34443. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34444. LinesToStr([ // statements
  34445. 'rtl.createClass(this, "TObject", null, function () {',
  34446. ' this.$init = function () {',
  34447. ' };',
  34448. ' this.$final = function () {',
  34449. ' };',
  34450. ' this.Fly = function (Prom) {',
  34451. ' };',
  34452. '});',
  34453. 'this.Crawl = async function () {',
  34454. ' var Result = undefined;',
  34455. ' return Result;',
  34456. '};',
  34457. 'this.Add = function (Func) {',
  34458. '};',
  34459. '']),
  34460. LinesToStr([
  34461. '$mod.Add(function (Bird) {',
  34462. ' Bird.Fly($mod.Crawl());',
  34463. '});',
  34464. '']));
  34465. end;
  34466. procedure TTestModule.TestAsync_ProcType;
  34467. begin
  34468. StartProgram(false);
  34469. Add([
  34470. '{$mode objfpc}',
  34471. 'type',
  34472. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34473. ' TFunc = function(x: double = 1.1): word; async;',
  34474. ' TProc = procedure(x: longint = 7); async;',
  34475. 'function Crawl(d: double): word; async;',
  34476. 'begin',
  34477. 'end;',
  34478. 'procedure Run(e:longint); async;',
  34479. 'begin',
  34480. 'end;',
  34481. 'procedure Fly(p: TProc); async;',
  34482. 'begin',
  34483. ' await(p);',
  34484. ' await(p());',
  34485. 'end;',
  34486. 'var',
  34487. ' RefFunc: TRefFunc;',
  34488. ' Func: TFunc;',
  34489. ' Proc, ProcB: TProc;',
  34490. 'begin',
  34491. ' Func:=@Crawl;',
  34492. ' RefFunc:=@Crawl;',
  34493. ' RefFunc:=function(c:double):word async begin',
  34494. ' Result:=await(RefFunc);',
  34495. ' Result:=await(RefFunc());',
  34496. ' Result:=await(Func);',
  34497. ' Result:=await(Func());',
  34498. ' await(Proc);',
  34499. ' await(Proc());',
  34500. ' await(Proc(13));',
  34501. ' end;',
  34502. ' Proc:=@Run;',
  34503. ' if Proc=ProcB then ;',
  34504. ' ']);
  34505. ConvertProgram;
  34506. CheckResolverUnexpectedHints();
  34507. CheckSource('TestAsync_ProcType',
  34508. LinesToStr([ // statements
  34509. 'this.Crawl = async function (d) {',
  34510. ' var Result = 0;',
  34511. ' return Result;',
  34512. '};',
  34513. 'this.Run = async function (e) {',
  34514. '};',
  34515. 'this.Fly = async function (p) {',
  34516. ' await p(7);',
  34517. ' await p(7);',
  34518. '};',
  34519. 'this.RefFunc = null;',
  34520. 'this.Func = null;',
  34521. 'this.Proc = null;',
  34522. 'this.ProcB = null;',
  34523. '']),
  34524. LinesToStr([
  34525. '$mod.Func = $mod.Crawl;',
  34526. '$mod.RefFunc = $mod.Crawl;',
  34527. '$mod.RefFunc = async function (c) {',
  34528. ' var Result = 0;',
  34529. ' Result = await $mod.RefFunc(1.3);',
  34530. ' Result = await $mod.RefFunc(1.3);',
  34531. ' Result = await $mod.Func(1.1);',
  34532. ' Result = await $mod.Func(1.1);',
  34533. ' await $mod.Proc(7);',
  34534. ' await $mod.Proc(7);',
  34535. ' await $mod.Proc(13);',
  34536. ' return Result;',
  34537. '};',
  34538. '$mod.Proc = $mod.Run;',
  34539. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34540. '']));
  34541. end;
  34542. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34543. begin
  34544. StartProgram(false);
  34545. Add([
  34546. '{$mode objfpc}',
  34547. 'type',
  34548. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34549. 'function Crawl(d: double): word; async;',
  34550. 'begin',
  34551. 'end;',
  34552. 'var',
  34553. ' RefFunc: TRefFunc;',
  34554. 'begin',
  34555. ' RefFunc:=@Crawl;',
  34556. ' ']);
  34557. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34558. ConvertProgram;
  34559. end;
  34560. procedure TTestModule.TestAsync_Inherited;
  34561. begin
  34562. StartProgram(false);
  34563. Add([
  34564. '{$mode objfpc}',
  34565. '{$modeswitch externalclass}',
  34566. 'type',
  34567. ' TJSPromise = class external name ''Promise''',
  34568. ' end;',
  34569. ' TObject = class',
  34570. ' function Run(w: word = 3): word; async; virtual;',
  34571. ' end;',
  34572. ' TBird = class',
  34573. ' function Run(w: word = 3): word; async; override;',
  34574. ' end;',
  34575. 'function TObject.Run(w: word = 3): word; async;',
  34576. 'begin',
  34577. 'end;',
  34578. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34579. 'var p: TJSPromise;',
  34580. 'begin',
  34581. ' p:=inherited;',
  34582. ' p:=inherited Run;',
  34583. ' p:=inherited Run();',
  34584. ' p:=inherited Run(4);',
  34585. ' exit(p);',
  34586. ' exit(inherited);',
  34587. ' exit(inherited Run);',
  34588. ' exit(inherited Run(5));',
  34589. ' exit(6);',
  34590. 'end;',
  34591. 'begin',
  34592. ' ']);
  34593. ConvertProgram;
  34594. CheckSource('TestAsync_Inherited',
  34595. LinesToStr([ // statements
  34596. 'rtl.createClass(this, "TObject", null, function () {',
  34597. ' this.$init = function () {',
  34598. ' };',
  34599. ' this.$final = function () {',
  34600. ' };',
  34601. ' this.Run = async function (w) {',
  34602. ' var Result = 0;',
  34603. ' return Result;',
  34604. ' };',
  34605. '});',
  34606. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34607. ' this.Run = async function (w) {',
  34608. ' var Result = 0;',
  34609. ' var p = null;',
  34610. ' p = $mod.TObject.Run.apply(this, arguments);',
  34611. ' p = $mod.TObject.Run.call(this, 3);',
  34612. ' p = $mod.TObject.Run.call(this, 3);',
  34613. ' p = $mod.TObject.Run.call(this, 4);',
  34614. ' return p;',
  34615. ' return $mod.TObject.Run.apply(this, arguments);',
  34616. ' return $mod.TObject.Run.call(this, 3);',
  34617. ' return $mod.TObject.Run.call(this, 5);',
  34618. ' return 6;',
  34619. ' return Result;',
  34620. ' };',
  34621. '});',
  34622. '']),
  34623. LinesToStr([
  34624. '']));
  34625. CheckResolverUnexpectedHints();
  34626. end;
  34627. procedure TTestModule.TestAsync_ClassInterface;
  34628. begin
  34629. StartProgram(false);
  34630. Add([
  34631. '{$mode objfpc}',
  34632. '{$modeswitch externalclass}',
  34633. 'type',
  34634. ' TJSPromise = class external name ''Promise''',
  34635. ' end;',
  34636. ' IUnknown = interface',
  34637. ' function _AddRef: longint;',
  34638. ' function _Release: longint;',
  34639. ' end;',
  34640. 'function Say(i: IUnknown): IUnknown; async;',
  34641. 'begin',
  34642. 'end;',
  34643. 'function Run: IUnknown; async;',
  34644. 'begin',
  34645. ' Result:=await(Run);',
  34646. ' Result:=await(Run());',
  34647. ' Result:=await(Run) as IUnknown;',
  34648. ' Result:=await(Say(nil));',
  34649. ' Result:=await(Say(await(Run())));',
  34650. ' Result:=await(Say(await(Run()) as IUnknown));',
  34651. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  34652. 'end;',
  34653. 'procedure Fly;',
  34654. 'var p: TJSPromise;',
  34655. 'begin',
  34656. ' Run;',
  34657. ' Run();',
  34658. ' p:=Run;',
  34659. ' p:=Run();',
  34660. 'end;',
  34661. 'begin',
  34662. ' ']);
  34663. ConvertProgram;
  34664. CheckSource('TestAsync_ClassInterface',
  34665. LinesToStr([ // statements
  34666. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  34667. 'this.Say = async function (i) {',
  34668. ' var Result = null;',
  34669. ' return Result;',
  34670. '};',
  34671. 'this.Run = async function () {',
  34672. ' var Result = null;',
  34673. ' var $ok = false;',
  34674. ' try {',
  34675. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34676. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34677. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  34678. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  34679. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  34680. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  34681. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  34682. ' $ok = true;',
  34683. ' } finally {',
  34684. ' if (!$ok) rtl._Release(Result);',
  34685. ' };',
  34686. ' return Result;',
  34687. '};',
  34688. 'this.Fly = function () {',
  34689. ' var p = null;',
  34690. ' $mod.Run();',
  34691. ' $mod.Run();',
  34692. ' p = $mod.Run();',
  34693. ' p = $mod.Run();',
  34694. '};',
  34695. '']),
  34696. LinesToStr([
  34697. '']));
  34698. CheckResolverUnexpectedHints();
  34699. end;
  34700. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  34701. begin
  34702. StartProgram(true,[supTInterfacedObject]);
  34703. Add([
  34704. '{$mode objfpc}',
  34705. '{$modeswitch externalclass}',
  34706. 'type',
  34707. ' TJSPromise = class external name ''Promise''',
  34708. ' end;',
  34709. ' IBird = interface',
  34710. ' procedure Run;',
  34711. ' end;',
  34712. ' TBird = class(TInterfacedObject,IBird)',
  34713. ' procedure Run; async;',
  34714. ' end;',
  34715. 'procedure TBird.Run;',
  34716. 'begin',
  34717. 'end;',
  34718. 'begin',
  34719. ' ']);
  34720. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34721. ConvertProgram;
  34722. end;
  34723. procedure TTestModule.TestAWait_ClassAs;
  34724. begin
  34725. StartProgram(false);
  34726. Add([
  34727. '{$mode objfpc}',
  34728. '{$modeswitch externalclass}',
  34729. 'type',
  34730. ' TJSPromise = class external name ''Promise''',
  34731. ' end;',
  34732. ' TObject = class',
  34733. ' function Run: TObject; async;',
  34734. ' end;',
  34735. ' TBird = class',
  34736. ' function Fly: TBird; async;',
  34737. ' end;',
  34738. 'function TObject.Run: TObject; async;',
  34739. 'begin',
  34740. 'end;',
  34741. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  34742. 'var o: TObject;',
  34743. 'begin',
  34744. ' o:=await(TObject,Run);',
  34745. ' o:=await(TObject,Fly);',
  34746. ' o:=await(TBird,Fly);',
  34747. ' o:=await(TObject,inherited Run);',
  34748. ' o:=await(TObject,inherited Run) as TBird;',
  34749. 'end;',
  34750. 'begin',
  34751. ' ']);
  34752. ConvertProgram;
  34753. CheckSource('TestAWait_ClassAs',
  34754. LinesToStr([ // statements
  34755. 'rtl.createClass(this, "TObject", null, function () {',
  34756. ' this.$init = function () {',
  34757. ' };',
  34758. ' this.$final = function () {',
  34759. ' };',
  34760. ' this.Run = async function () {',
  34761. ' var Result = null;',
  34762. ' return Result;',
  34763. ' };',
  34764. '});',
  34765. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34766. ' this.Fly = async function () {',
  34767. ' var Result = null;',
  34768. ' var o = null;',
  34769. ' o = await this.Run();',
  34770. ' o = await this.Fly();',
  34771. ' o = await this.Fly();',
  34772. ' o = await $mod.TObject.Run.call(this);',
  34773. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  34774. ' return Result;',
  34775. ' };',
  34776. '});',
  34777. '']),
  34778. LinesToStr([
  34779. '']));
  34780. CheckResolverUnexpectedHints();
  34781. end;
  34782. procedure TTestModule.TestLibrary_Empty;
  34783. begin
  34784. StartLibrary(false);
  34785. Add([
  34786. '']);
  34787. ConvertLibrary;
  34788. CheckFullSource('TestLibrary_Empty',
  34789. LinesToStr([ // statements
  34790. 'rtl.module("library", [], function () {',
  34791. ' var $mod = this;',
  34792. ' $mod.$main = function () {',
  34793. ' };',
  34794. '});',
  34795. 'rtl.run("library");',
  34796. '']));
  34797. CheckResolverUnexpectedHints();
  34798. end;
  34799. procedure TTestModule.TestLibrary_ExportFunc;
  34800. begin
  34801. StartLibrary(false);
  34802. Add([
  34803. 'procedure Run(w: word);',
  34804. 'begin',
  34805. 'end;',
  34806. 'exports',
  34807. ' Run;',
  34808. ' run name ''Foo'';',
  34809. ' test1.run name ''Test1Run'';',
  34810. '']);
  34811. ConvertLibrary;
  34812. CheckFullSource('TestLibrary_ExportFunc',
  34813. LinesToStr([ // statements
  34814. 'rtl.module("library", [], function () {',
  34815. ' var $mod = this;',
  34816. ' this.Run = function (w) {',
  34817. ' };',
  34818. ' $mod.$main = function () {',
  34819. ' };',
  34820. '});',
  34821. 'rtl.run("library");',
  34822. 'export const Run = pas.library.Run;',
  34823. 'export const Foo = pas.library.Run;',
  34824. 'export const Test1Run = pas.library.Run;',
  34825. '']));
  34826. CheckResolverUnexpectedHints();
  34827. end;
  34828. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  34829. begin
  34830. StartLibrary(false);
  34831. Add([
  34832. 'procedure Run(w: word); overload;',
  34833. 'begin',
  34834. 'end;',
  34835. 'procedure Run(s: string); overload;',
  34836. 'begin',
  34837. 'end;',
  34838. 'exports',
  34839. ' Run;',
  34840. '']);
  34841. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  34842. nCantDetermineWhichOverloadedFunctionToCall);
  34843. ConvertLibrary;
  34844. end;
  34845. procedure TTestModule.TestLibrary_Export_Index_Fail;
  34846. begin
  34847. StartLibrary(false);
  34848. Add([
  34849. 'procedure Run(w: word);',
  34850. 'begin',
  34851. 'end;',
  34852. 'exports',
  34853. ' Run index 3;',
  34854. '']);
  34855. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  34856. ConvertLibrary;
  34857. end;
  34858. procedure TTestModule.TestLibrary_ExportVar;
  34859. begin
  34860. StartLibrary(false);
  34861. Add([
  34862. 'var Wing: word;',
  34863. 'exports',
  34864. ' Wing, wing name ''BirdArm'';',
  34865. '']);
  34866. ConvertLibrary;
  34867. CheckFullSource('TestLibrary_ExportVar',
  34868. LinesToStr([ // statements
  34869. 'rtl.module("library", [], function () {',
  34870. ' var $mod = this;',
  34871. ' this.Wing = 0;',
  34872. ' $mod.$main = function () {',
  34873. ' };',
  34874. '});',
  34875. 'rtl.run("library");',
  34876. 'export const vars = {};',
  34877. 'Object.defineProperties(vars, {',
  34878. ' Wing: {',
  34879. ' enumerable: true,',
  34880. ' get: function () {',
  34881. ' return pas.library.Wing;',
  34882. ' },',
  34883. ' set: function (v) {',
  34884. ' pas.library.Wing = v;',
  34885. ' }',
  34886. ' },',
  34887. ' BirdArm: {',
  34888. ' enumerable: true,',
  34889. ' get: function () {',
  34890. ' return pas.library.Wing;',
  34891. ' },',
  34892. ' set: function (v) {',
  34893. ' pas.library.Wing = v;',
  34894. ' }',
  34895. ' }',
  34896. '});',
  34897. '']));
  34898. CheckResolverUnexpectedHints();
  34899. end;
  34900. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34901. begin
  34902. AddModuleWithIntfImplSrc('Unit1.pas',
  34903. LinesToStr([
  34904. 'type',
  34905. ' TAnt = class',
  34906. ' class function Crawl: word; static;',
  34907. ' end;',
  34908. 'function Fly: word;',
  34909. '']),
  34910. LinesToStr([
  34911. 'function Fly: word;',
  34912. 'begin',
  34913. 'end;',
  34914. 'class function TAnt.Crawl: word;',
  34915. 'begin',
  34916. 'end;',
  34917. '']));
  34918. StartLibrary(true,[supTObject]);
  34919. Add([
  34920. 'uses unit1;',
  34921. 'exports',
  34922. ' Fly;',
  34923. ' TAnt.Crawl;',
  34924. '']);
  34925. ConvertLibrary;
  34926. CheckFullSource('TestLibrary_ExportUnitFunc',
  34927. LinesToStr([ // statements
  34928. 'rtl.module("library", ["system", "Unit1"], function () {',
  34929. ' var $mod = this;',
  34930. ' $mod.$main = function () {',
  34931. ' };',
  34932. '});',
  34933. 'rtl.run("library");',
  34934. 'export const Fly = pas.Unit1.Fly;',
  34935. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  34936. '']));
  34937. CheckResolverUnexpectedHints();
  34938. end;
  34939. Initialization
  34940. RegisterTests([TTestModule]);
  34941. end.