tcmodules.pas 818 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004
  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,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [coNoTypeInfo];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo
  46. );
  47. TSystemUnitParts = set of TSystemUnitPart;
  48. { TTestHintMessage }
  49. TTestHintMessage = class
  50. public
  51. Id: int64;
  52. MsgType: TMessageType;
  53. MsgNumber: integer;
  54. Msg: string;
  55. SourcePos: TPasSourcePos;
  56. end;
  57. { TTestPasParser }
  58. TTestPasParser = Class(TPasParser)
  59. end;
  60. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  61. { TTestEnginePasResolver }
  62. TTestEnginePasResolver = class(TPas2JsResolver)
  63. private
  64. FFilename: string;
  65. FModule: TPasModule;
  66. FOnFindUnit: TOnFindUnit;
  67. FParser: TTestPasParser;
  68. FStreamResolver: TStreamResolver;
  69. FScanner: TPas2jsPasScanner;
  70. FSource: string;
  71. public
  72. destructor Destroy; override;
  73. function FindUnit(const AName, InFilename: String; NameExpr,
  74. InFileExpr: TPasExpr): TPasModule; override;
  75. procedure UsedInterfacesFinished(Section: TPasSection); override;
  76. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  77. property Filename: string read FFilename write FFilename;
  78. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  79. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  80. property Parser: TTestPasParser read FParser write FParser;
  81. property Source: string read FSource write FSource;
  82. property Module: TPasModule read FModule;
  83. end;
  84. { TCustomTestModule }
  85. TCustomTestModule = Class(TTestCase)
  86. private
  87. FConverter: TPasToJSConverter;
  88. FEngine: TTestEnginePasResolver;
  89. FExpectedErrorClass: ExceptClass;
  90. FExpectedErrorMsg: string;
  91. FExpectedErrorNumber: integer;
  92. FFilename: string;
  93. FFileResolver: TStreamResolver;
  94. FJSImplementationSrc: TJSSourceElements;
  95. FJSImplementationUses: TJSArrayLiteral;
  96. FJSInitBody: TJSFunctionBody;
  97. FJSImplentationUses: TJSArrayLiteral;
  98. FJSInterfaceUses: TJSArrayLiteral;
  99. FJSModule: TJSSourceElements;
  100. FJSModuleSrc: TJSSourceElements;
  101. FJSSource: TStringList;
  102. FModule: TPasModule;
  103. FJSModuleCallArgs: TJSArguments;
  104. FModules: TObjectList;// list of TTestEnginePasResolver
  105. FParser: TTestPasParser;
  106. FPasProgram: TPasProgram;
  107. FHintMsgs: TObjectList; // list of TTestHintMessage
  108. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  109. FJSRegModuleCall: TJSCallExpression;
  110. FScanner: TPas2jsPasScanner;
  111. FSkipTests: boolean;
  112. FSource: TStringList;
  113. FFirstPasStatement: TPasImplBlock;
  114. {$IFDEF EnablePasTreeGlobalRefCount}
  115. FElementRefCountAtSetup: int64;
  116. {$ENDIF}
  117. function GetMsgCount: integer;
  118. function GetMsgs(Index: integer): TTestHintMessage;
  119. function GetResolverCount: integer;
  120. function GetResolvers(Index: integer): TTestEnginePasResolver;
  121. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  122. procedure OnParserLog(Sender: TObject; const Msg: String);
  123. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  124. procedure OnScannerLog(Sender: TObject; const Msg: String);
  125. protected
  126. procedure SetUp; override;
  127. function CreateConverter: TPasToJSConverter; virtual;
  128. function LoadUnit(const aUnitName: String): TPasModule;
  129. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  130. procedure TearDown; override;
  131. Procedure Add(Line: string); virtual;
  132. Procedure Add(const Lines: array of string);
  133. Procedure StartParsing; virtual;
  134. procedure ParseModuleQueue; virtual;
  135. procedure ParseModule; virtual;
  136. procedure ParseProgram; virtual;
  137. procedure ParseUnit; virtual;
  138. protected
  139. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  140. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  141. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  142. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  143. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  144. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  145. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  146. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  147. procedure ConvertModule; virtual;
  148. procedure ConvertProgram; virtual;
  149. procedure ConvertUnit; virtual;
  150. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  151. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  152. function GetDottedIdentifier(El: TJSElement): string;
  153. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  154. ImplStatements: string = ''); virtual;
  155. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  156. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  157. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  158. Msg: string; Marker: PSrcMarker = nil); virtual;
  159. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  160. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  161. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  162. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  163. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  164. function IsErrorExpected(E: Exception): boolean;
  165. procedure HandleScannerError(E: EScannerError);
  166. procedure HandleParserError(E: EParserError);
  167. procedure HandlePasResolveError(E: EPasResolve);
  168. procedure HandlePas2JSError(E: EPas2JS);
  169. procedure HandleException(E: Exception);
  170. procedure FailException(E: Exception);
  171. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  172. function IndexOfResolver(const Filename: string): integer;
  173. function GetResolver(const Filename: string): TTestEnginePasResolver;
  174. function GetDefaultNamespace: string;
  175. property PasProgram: TPasProgram Read FPasProgram;
  176. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  177. property ResolverCount: integer read GetResolverCount;
  178. property Engine: TTestEnginePasResolver read FEngine;
  179. property Filename: string read FFilename;
  180. Property Module: TPasModule Read FModule;
  181. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  182. property Converter: TPasToJSConverter read FConverter;
  183. property JSSource: TStringList read FJSSource;
  184. property JSModule: TJSSourceElements read FJSModule;
  185. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  186. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  187. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  188. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  189. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  190. property JSInitBody: TJSFunctionBody read FJSInitBody;
  191. property JSImplementationSrc: TJSSourceElements read FJSImplementationSrc;
  192. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  193. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  194. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  195. property SkipTests: boolean read FSkipTests write FSkipTests;
  196. public
  197. constructor Create; override;
  198. destructor Destroy; override;
  199. property Source: TStringList read FSource;
  200. property FileResolver: TStreamResolver read FFileResolver;
  201. property Scanner: TPas2jsPasScanner read FScanner;
  202. property Parser: TTestPasParser read FParser;
  203. property MsgCount: integer read GetMsgCount;
  204. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  205. end;
  206. { TTestModule }
  207. TTestModule = class(TCustomTestModule)
  208. Published
  209. Procedure TestReservedWords;
  210. // program/units
  211. Procedure TestEmptyProgram;
  212. Procedure TestEmptyProgramUseStrict;
  213. Procedure TestEmptyUnit;
  214. Procedure TestEmptyUnitUseStrict;
  215. Procedure TestDottedUnitNames;
  216. Procedure TestDottedUnitNameImpl;
  217. Procedure TestDottedUnitExpr;
  218. Procedure Test_ModeFPCFail;
  219. Procedure Test_ModeSwitchCBlocksFail;
  220. Procedure TestUnit_UseSystem;
  221. Procedure TestUnit_Intf1Impl2Intf1;
  222. Procedure TestIncludeVersion;
  223. // vars/const
  224. Procedure TestVarInt;
  225. Procedure TestVarBaseTypes;
  226. Procedure TestBaseTypeSingleFail;
  227. Procedure TestBaseTypeExtendedFail;
  228. Procedure TestConstBaseTypes;
  229. Procedure TestUnitImplVars;
  230. Procedure TestUnitImplConsts;
  231. Procedure TestUnitImplRecord;
  232. Procedure TestRenameJSNameConflict;
  233. Procedure TestLocalConst;
  234. Procedure TestVarExternal;
  235. Procedure TestVarExternalOtherUnit;
  236. Procedure TestVarAbsoluteFail;
  237. Procedure TestConstExternal;
  238. // numbers
  239. Procedure TestDouble;
  240. Procedure TestInteger;
  241. Procedure TestIntegerRange;
  242. Procedure TestIntegerTypecasts;
  243. Procedure TestInteger_BitwiseShrNativeInt;
  244. Procedure TestInteger_BitwiseShlNativeInt;
  245. Procedure TestCurrency;
  246. Procedure TestForBoolDo;
  247. Procedure TestForIntDo;
  248. Procedure TestForIntInDo;
  249. // strings
  250. Procedure TestCharConst;
  251. Procedure TestChar_Compare;
  252. Procedure TestChar_BuiltInProcs;
  253. Procedure TestStringConst;
  254. Procedure TestStringConstSurrogate;
  255. Procedure TestString_Length;
  256. Procedure TestString_Compare;
  257. Procedure TestString_SetLength;
  258. Procedure TestString_CharAt;
  259. Procedure TestStringHMinusFail;
  260. Procedure TestStr;
  261. Procedure TestBaseType_AnsiStringFail;
  262. Procedure TestBaseType_WideStringFail;
  263. Procedure TestBaseType_ShortStringFail;
  264. Procedure TestBaseType_RawByteStringFail;
  265. Procedure TestTypeShortstring_Fail;
  266. Procedure TestCharSet_Custom;
  267. Procedure TestForCharDo;
  268. Procedure TestForCharInDo;
  269. // alias types
  270. Procedure TestAliasTypeRef;
  271. Procedure TestTypeCast_BaseTypes;
  272. Procedure TestTypeCast_AliasBaseTypes;
  273. // functions
  274. Procedure TestEmptyProc;
  275. Procedure TestProcOneParam;
  276. Procedure TestFunctionWithoutParams;
  277. Procedure TestProcedureWithoutParams;
  278. Procedure TestPrgProcVar;
  279. Procedure TestProcTwoArgs;
  280. Procedure TestProc_DefaultValue;
  281. Procedure TestUnitProcVar;
  282. Procedure TestImplProc;
  283. Procedure TestFunctionResult;
  284. Procedure TestNestedProc;
  285. Procedure TestNestedProc_ResultString;
  286. Procedure TestForwardProc;
  287. Procedure TestNestedForwardProc;
  288. Procedure TestAssignFunctionResult;
  289. Procedure TestFunctionResultInCondition;
  290. Procedure TestFunctionResultInForLoop;
  291. Procedure TestFunctionResultInTypeCast;
  292. Procedure TestExit;
  293. Procedure TestBreak;
  294. Procedure TestBreakAsVar;
  295. Procedure TestContinue;
  296. Procedure TestProc_External;
  297. Procedure TestProc_ExternalOtherUnit;
  298. Procedure TestProc_Asm;
  299. Procedure TestProc_Assembler;
  300. Procedure TestProc_VarParam;
  301. Procedure TestProc_VarParamString;
  302. Procedure TestProc_VarParamV;
  303. Procedure TestProc_Overload;
  304. Procedure TestProc_OverloadForward;
  305. Procedure TestProc_OverloadIntfImpl;
  306. Procedure TestProc_OverloadNested;
  307. Procedure TestProc_OverloadNestedForward;
  308. Procedure TestProc_OverloadUnitCycle;
  309. Procedure TestProc_Varargs;
  310. Procedure TestProc_ConstOrder;
  311. Procedure TestProc_DuplicateConst;
  312. Procedure TestProc_LocalVarAbsolute;
  313. Procedure TestProc_LocalVarInit;
  314. Procedure TestProc_ReservedWords;
  315. Procedure TestProc_ConstRefWord;
  316. // anonymous functions
  317. Procedure TestAnonymousProc_Assign_ObjFPC;
  318. Procedure TestAnonymousProc_Assign_Delphi;
  319. Procedure TestAnonymousProc_Arg;
  320. Procedure TestAnonymousProc_Typecast;
  321. Procedure TestAnonymousProc_With;
  322. Procedure TestAnonymousProc_ExceptOn;
  323. Procedure TestAnonymousProc_Nested;
  324. Procedure TestAnonymousProc_NestedAssignResult;
  325. Procedure TestAnonymousProc_Class;
  326. Procedure TestAnonymousProc_ForLoop;
  327. // enums, sets
  328. Procedure TestEnum_Name;
  329. Procedure TestEnum_Number;
  330. Procedure TestEnum_ConstFail;
  331. Procedure TestEnum_Functions;
  332. Procedure TestEnum_AsParams;
  333. Procedure TestEnumRange_Array;
  334. Procedure TestEnum_ForIn;
  335. Procedure TestEnum_ScopedNumber;
  336. Procedure TestEnum_InFunction;
  337. Procedure TestSet_Enum;
  338. Procedure TestSet_Operators;
  339. Procedure TestSet_Operator_In;
  340. Procedure TestSet_Functions;
  341. Procedure TestSet_PassAsArgClone;
  342. Procedure TestSet_AsParams;
  343. Procedure TestSet_Property;
  344. Procedure TestSet_EnumConst;
  345. Procedure TestSet_IntConst;
  346. Procedure TestSet_AnonymousEnumType;
  347. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  348. Procedure TestSet_ConstEnum;
  349. Procedure TestSet_ConstChar;
  350. Procedure TestSet_ConstInt;
  351. Procedure TestSet_InFunction;
  352. Procedure TestSet_ForIn;
  353. // statements
  354. Procedure TestNestBegin;
  355. Procedure TestIncDec;
  356. Procedure TestLoHiFpcMode;
  357. Procedure TestLoHiDelphiMode;
  358. Procedure TestAssignments;
  359. Procedure TestArithmeticOperators1;
  360. Procedure TestLogicalOperators;
  361. Procedure TestBitwiseOperators;
  362. Procedure TestBitwiseOperatorsLongword;
  363. Procedure TestFunctionInt;
  364. Procedure TestFunctionString;
  365. Procedure TestIfThen;
  366. Procedure TestForLoop;
  367. Procedure TestForLoopInsideFunction;
  368. Procedure TestForLoop_ReadVarAfter;
  369. Procedure TestForLoop_Nested;
  370. Procedure TestRepeatUntil;
  371. Procedure TestAsmBlock;
  372. Procedure TestAsmPas_Impl; // ToDo
  373. Procedure TestTryFinally;
  374. Procedure TestTryExcept;
  375. Procedure TestTryExcept_ReservedWords;
  376. Procedure TestIfThenRaiseElse;
  377. Procedure TestCaseOf;
  378. Procedure TestCaseOf_UseSwitch;
  379. Procedure TestCaseOfNoElse;
  380. Procedure TestCaseOfNoElse_UseSwitch;
  381. Procedure TestCaseOfRange;
  382. Procedure TestCaseOfString;
  383. Procedure TestCaseOfChar;
  384. Procedure TestCaseOfExternalClassConst;
  385. Procedure TestDebugger;
  386. // arrays
  387. Procedure TestArray_Dynamic;
  388. Procedure TestArray_Dynamic_Nil;
  389. Procedure TestArray_DynMultiDimensional;
  390. Procedure TestArray_StaticInt;
  391. Procedure TestArray_StaticBool;
  392. Procedure TestArray_StaticChar;
  393. Procedure TestArray_StaticMultiDim;
  394. Procedure TestArray_StaticInFunction;
  395. Procedure TestArrayOfRecord;
  396. Procedure TestArray_StaticRecord;
  397. Procedure TestArrayOfSet;
  398. Procedure TestArray_DynAsParam;
  399. Procedure TestArray_StaticAsParam;
  400. Procedure TestArrayElement_AsParams;
  401. Procedure TestArrayElementFromFuncResult_AsParams;
  402. Procedure TestArrayEnumTypeRange;
  403. Procedure TestArray_SetLengthOutArg;
  404. Procedure TestArray_SetLengthProperty;
  405. Procedure TestArray_SetLengthMultiDim;
  406. Procedure TestArray_OpenArrayOfString;
  407. Procedure TestArray_ConstRef;
  408. Procedure TestArray_Concat;
  409. Procedure TestArray_Copy;
  410. Procedure TestArray_InsertDelete;
  411. Procedure TestArray_DynArrayConstObjFPC;
  412. Procedure TestArray_DynArrayConstDelphi;
  413. Procedure TestArray_ArrayLitAsParam;
  414. Procedure TestArray_ArrayLitMultiDimAsParam;
  415. Procedure TestArray_ArrayLitStaticAsParam;
  416. Procedure TestArray_ForInArrOfString;
  417. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  418. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  419. Procedure TestArrayOfConst_TVarRec;
  420. Procedure TestArrayOfConst_PassBaseTypes;
  421. Procedure TestArrayOfConst_PassObj;
  422. // record
  423. Procedure TestRecord_Empty;
  424. Procedure TestRecord_Var;
  425. Procedure TestRecord_VarExternal;
  426. Procedure TestRecord_WithDo;
  427. Procedure TestRecord_Assign;
  428. Procedure TestRecord_AsParams;
  429. Procedure TestRecord_ConstRef;
  430. Procedure TestRecordElement_AsParams;
  431. Procedure TestRecordElementFromFuncResult_AsParams;
  432. Procedure TestRecordElementFromWith_AsParams;
  433. Procedure TestRecord_Equal;
  434. Procedure TestRecord_JSValue;
  435. Procedure TestRecord_VariantFail;
  436. Procedure TestRecord_FieldArray;
  437. Procedure TestRecord_Const;
  438. Procedure TestRecord_TypecastFail;
  439. Procedure TestRecord_InFunction;
  440. Procedure TestRecord_AnonymousFail;
  441. // ToDo: RTTI of local record
  442. // ToDo: pcu local record, name clash and rtti
  443. // advanced record
  444. Procedure TestAdvRecord_Function;
  445. Procedure TestAdvRecord_Property;
  446. Procedure TestAdvRecord_PropertyDefault;
  447. Procedure TestAdvRecord_Property_ClassMethod;
  448. Procedure TestAdvRecord_Const;
  449. Procedure TestAdvRecord_ExternalField;
  450. Procedure TestAdvRecord_SubRecord;
  451. Procedure TestAdvRecord_SubClass;
  452. Procedure TestAdvRecord_SubInterfaceFail;
  453. Procedure TestAdvRecord_Constructor;
  454. Procedure TestAdvRecord_ClassConstructor_Program;
  455. Procedure TestAdvRecord_ClassConstructor_Unit;
  456. // classes
  457. Procedure TestClass_TObjectDefaultConstructor;
  458. Procedure TestClass_TObjectConstructorWithParams;
  459. Procedure TestClass_TObjectConstructorWithDefaultParam;
  460. Procedure TestClass_Var;
  461. Procedure TestClass_Method;
  462. Procedure TestClass_Implementation;
  463. Procedure TestClass_Inheritance;
  464. Procedure TestClass_TypeAlias;
  465. Procedure TestClass_AbstractMethod;
  466. Procedure TestClass_CallInherited_ProcNoParams;
  467. Procedure TestClass_CallInherited_WithParams;
  468. Procedure TestClasS_CallInheritedConstructor;
  469. Procedure TestClass_ClassVar_Assign;
  470. Procedure TestClass_CallClassMethod;
  471. Procedure TestClass_Property;
  472. Procedure TestClass_Property_ClassMethod;
  473. Procedure TestClass_Property_Indexed;
  474. Procedure TestClass_Property_IndexSpec;
  475. Procedure TestClass_PropertyOfTypeArray;
  476. Procedure TestClass_PropertyDefault;
  477. Procedure TestClass_PropertyDefault2;
  478. Procedure TestClass_PropertyOverride;
  479. Procedure TestClass_PropertyIncVisibility;
  480. Procedure TestClass_Assigned;
  481. Procedure TestClass_WithClassDoCreate;
  482. Procedure TestClass_WithClassInstDoProperty;
  483. Procedure TestClass_WithClassInstDoPropertyWithParams;
  484. Procedure TestClass_WithClassInstDoFunc;
  485. Procedure TestClass_TypeCast;
  486. Procedure TestClass_TypeCastUntypedParam;
  487. Procedure TestClass_Overloads;
  488. Procedure TestClass_OverloadsAncestor;
  489. Procedure TestClass_OverloadConstructor;
  490. Procedure TestClass_OverloadDelphiOverride;
  491. Procedure TestClass_ReintroduceVarDelphi;
  492. Procedure TestClass_ReintroducedVar;
  493. Procedure TestClass_RaiseDescendant;
  494. Procedure TestClass_ExternalMethod;
  495. Procedure TestClass_ExternalVirtualNameMismatchFail;
  496. Procedure TestClass_ExternalOverrideFail;
  497. Procedure TestClass_ExternalVar;
  498. Procedure TestClass_Const;
  499. Procedure TestClass_LocalConstDuplicate_Prg;
  500. Procedure TestClass_LocalConstDuplicate_Unit;
  501. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  502. Procedure TestClass_LocalVarSelfFail;
  503. Procedure TestClass_ArgSelfFail;
  504. Procedure TestClass_NestedProcSelf;
  505. Procedure TestClass_NestedProcSelf2;
  506. Procedure TestClass_NestedProcClassSelf;
  507. Procedure TestClass_NestedProcCallInherited;
  508. Procedure TestClass_TObjectFree;
  509. Procedure TestClass_TObjectFree_VarArg;
  510. Procedure TestClass_TObjectFreeNewInstance;
  511. Procedure TestClass_TObjectFreeLowerCase;
  512. Procedure TestClass_TObjectFreeFunctionFail;
  513. Procedure TestClass_TObjectFreePropertyFail;
  514. Procedure TestClass_ForIn;
  515. Procedure TestClass_DispatchMessage;
  516. Procedure TestClass_Message_DuplicateIntFail;
  517. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  518. // class of
  519. Procedure TestClassOf_Create;
  520. Procedure TestClassOf_Call;
  521. Procedure TestClassOf_Assign;
  522. Procedure TestClassOf_Is;
  523. Procedure TestClassOf_Compare;
  524. Procedure TestClassOf_ClassVar;
  525. Procedure TestClassOf_ClassMethod;
  526. Procedure TestClassOf_ClassProperty;
  527. Procedure TestClassOf_ClassMethodSelf;
  528. Procedure TestClassOf_TypeCast;
  529. Procedure TestClassOf_ImplicitFunctionCall;
  530. Procedure TestClassOf_Const;
  531. // nested class
  532. Procedure TestNestedClass_Alias;
  533. Procedure TestNestedClass_Record;
  534. Procedure TestNestedClass_Class;
  535. // external class
  536. Procedure TestExternalClass_Var;
  537. Procedure TestExternalClass_Const;
  538. Procedure TestExternalClass_Dollar;
  539. Procedure TestExternalClass_DuplicateVarFail;
  540. Procedure TestExternalClass_Method;
  541. Procedure TestExternalClass_ClassMethod;
  542. Procedure TestExternalClass_ClassMethodStatic;
  543. Procedure TestExternalClass_FunctionResultInTypeCast;
  544. Procedure TestExternalClass_NonExternalOverride;
  545. Procedure TestExternalClass_OverloadHint;
  546. Procedure TestExternalClass_SameNamePublishedProperty;
  547. Procedure TestExternalClass_Property;
  548. Procedure TestExternalClass_PropertyDate;
  549. Procedure TestExternalClass_ClassProperty;
  550. Procedure TestExternalClass_ClassOf;
  551. Procedure TestExternalClass_ClassOtherUnit;
  552. Procedure TestExternalClass_Is;
  553. Procedure TestExternalClass_As;
  554. Procedure TestExternalClass_DestructorFail;
  555. Procedure TestExternalClass_New;
  556. Procedure TestExternalClass_ClassOf_New;
  557. Procedure TestExternalClass_FuncClassOf_New;
  558. Procedure TestExternalClass_New_PasClassFail;
  559. Procedure TestExternalClass_New_PasClassBracketsFail;
  560. Procedure TestExternalClass_NewExtName;
  561. Procedure TestExternalClass_Constructor;
  562. Procedure TestExternalClass_ConstructorBrackets;
  563. Procedure TestExternalClass_LocalConstSameName;
  564. Procedure TestExternalClass_ReintroduceOverload;
  565. Procedure TestExternalClass_Inherited;
  566. Procedure TestExternalClass_PascalAncestorFail;
  567. Procedure TestExternalClass_NewInstance;
  568. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  569. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  570. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  571. Procedure TestExternalClass_PascalProperty;
  572. Procedure TestExternalClass_TypeCastToRootClass;
  573. Procedure TestExternalClass_TypeCastToJSObject;
  574. Procedure TestExternalClass_TypeCastStringToExternalString;
  575. Procedure TestExternalClass_TypeCastToJSFunction;
  576. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  577. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  578. Procedure TestExternalClass_BracketAccessor;
  579. Procedure TestExternalClass_BracketAccessor_Call;
  580. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  581. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  582. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  583. Procedure TestExternalClass_BracketAccessor_MultiType;
  584. Procedure TestExternalClass_BracketAccessor_Index;
  585. Procedure TestExternalClass_ForInJSObject;
  586. Procedure TestExternalClass_ForInJSArray;
  587. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  588. // class interfaces
  589. Procedure TestClassInterface_Corba;
  590. Procedure TestClassInterface_ProcExternalFail;
  591. Procedure TestClassInterface_Overloads;
  592. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  593. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  594. Procedure TestClassInterface_AncestorImpl;
  595. Procedure TestClassInterface_ImplReintroduce;
  596. Procedure TestClassInterface_MethodResolution;
  597. Procedure TestClassInterface_AncestorMoreInterfaces;
  598. Procedure TestClassInterface_MethodOverride;
  599. Procedure TestClassInterface_Corba_Delegation;
  600. Procedure TestClassInterface_Corba_DelegationStatic;
  601. Procedure TestClassInterface_Corba_Operators;
  602. Procedure TestClassInterface_Corba_Args;
  603. Procedure TestClassInterface_Corba_ForIn;
  604. Procedure TestClassInterface_COM_AssignVar;
  605. Procedure TestClassInterface_COM_AssignArg;
  606. Procedure TestClassInterface_COM_FunctionResult;
  607. Procedure TestClassInterface_COM_InheritedFuncResult;
  608. Procedure TestClassInterface_COM_IsAsTypeCasts;
  609. Procedure TestClassInterface_COM_PassAsArg;
  610. Procedure TestClassInterface_COM_PassToUntypedParam;
  611. Procedure TestClassInterface_COM_FunctionInExpr;
  612. Procedure TestClassInterface_COM_Property;
  613. Procedure TestClassInterface_COM_IntfProperty;
  614. Procedure TestClassInterface_COM_Delegation;
  615. Procedure TestClassInterface_COM_With;
  616. Procedure TestClassInterface_COM_ForIn;
  617. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  618. Procedure TestClassInterface_COM_RecordIntfFail;
  619. Procedure TestClassInterface_COM_UnitInitialization;
  620. Procedure TestClassInterface_GUID;
  621. Procedure TestClassInterface_GUIDProperty;
  622. // helpers
  623. Procedure TestClassHelper_ClassVar;
  624. Procedure TestClassHelper_Method_AccessInstanceFields;
  625. Procedure TestClassHelper_Method_Call;
  626. Procedure TestClassHelper_Method_Nested_Call;
  627. Procedure TestClassHelper_ClassMethod_Call;
  628. Procedure TestClassHelper_ClassOf;
  629. Procedure TestClassHelper_MethodRefObjFPC;
  630. Procedure TestClassHelper_Constructor;
  631. Procedure TestClassHelper_InheritedObjFPC;
  632. Procedure TestClassHelper_Property;
  633. Procedure TestClassHelper_Property_Array;
  634. Procedure TestClassHelper_Property_Array_Default;
  635. Procedure TestClassHelper_Property_Array_DefaultDefault;
  636. Procedure TestClassHelper_ClassProperty;
  637. Procedure TestClassHelper_ClassPropertyStatic;
  638. Procedure TestClassHelper_ClassProperty_Array;
  639. Procedure TestClassHelper_ForIn;
  640. Procedure TestClassHelper_PassProperty;
  641. Procedure TestExtClassHelper_ClassVar;
  642. Procedure TestExtClassHelper_Method_Call;
  643. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  644. Procedure TestRecordHelper_ClassVar;
  645. Procedure TestRecordHelper_Method_Call;
  646. Procedure TestRecordHelper_Constructor;
  647. Procedure TestTypeHelper_ClassVar;
  648. Procedure TestTypeHelper_PassResultElement;
  649. Procedure TestTypeHelper_PassArgs;
  650. Procedure TestTypeHelper_PassVarConst;
  651. Procedure TestTypeHelper_PassFuncResult;
  652. Procedure TestTypeHelper_PassPropertyField;
  653. Procedure TestTypeHelper_PassPropertyGetter;
  654. Procedure TestTypeHelper_PassClassPropertyField;
  655. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  656. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  657. Procedure TestTypeHelper_Property;
  658. Procedure TestTypeHelper_Property_Array;
  659. Procedure TestTypeHelper_ClassProperty;
  660. Procedure TestTypeHelper_ClassProperty_Array;
  661. Procedure TestTypeHelper_ClassMethod;
  662. Procedure TestTypeHelper_ExtClassMethodFail;
  663. Procedure TestTypeHelper_Constructor;
  664. Procedure TestTypeHelper_Word;
  665. Procedure TestTypeHelper_Double;
  666. Procedure TestTypeHelper_StringChar;
  667. Procedure TestTypeHelper_JSValue;
  668. Procedure TestTypeHelper_Array;
  669. Procedure TestTypeHelper_EnumType;
  670. Procedure TestTypeHelper_SetType;
  671. Procedure TestTypeHelper_InterfaceType;
  672. // proc types
  673. Procedure TestProcType;
  674. Procedure TestProcType_Arg;
  675. Procedure TestProcType_FunctionFPC;
  676. Procedure TestProcType_FunctionDelphi;
  677. Procedure TestProcType_ProcedureDelphi;
  678. Procedure TestProcType_AsParam;
  679. Procedure TestProcType_MethodFPC;
  680. Procedure TestProcType_MethodDelphi;
  681. Procedure TestProcType_PropertyFPC;
  682. Procedure TestProcType_PropertyDelphi;
  683. Procedure TestProcType_WithClassInstDoPropertyFPC;
  684. Procedure TestProcType_Nested;
  685. Procedure TestProcType_NestedOfObject;
  686. Procedure TestProcType_ReferenceToProc;
  687. Procedure TestProcType_ReferenceToMethod;
  688. Procedure TestProcType_Typecast;
  689. Procedure TestProcType_PassProcToUntyped;
  690. Procedure TestProcType_PassProcToArray;
  691. // pointer
  692. Procedure TestPointer;
  693. Procedure TestPointer_Proc;
  694. Procedure TestPointer_AssignRecordFail;
  695. Procedure TestPointer_AssignStaticArrayFail;
  696. Procedure TestPointer_TypeCastJSValueToPointer;
  697. Procedure TestPointer_NonRecordFail;
  698. Procedure TestPointer_AnonymousArgTypeFail;
  699. Procedure TestPointer_AnonymousVarTypeFail;
  700. Procedure TestPointer_AnonymousResultTypeFail;
  701. Procedure TestPointer_AddrOperatorFail;
  702. Procedure TestPointer_ArrayParamsFail;
  703. Procedure TestPointer_PointerAddFail;
  704. Procedure TestPointer_IncPointerFail;
  705. Procedure TestPointer_Record;
  706. Procedure TestPointer_RecordArg;
  707. // jsvalue
  708. Procedure TestJSValue_AssignToJSValue;
  709. Procedure TestJSValue_TypeCastToBaseType;
  710. Procedure TestJSValue_TypecastToJSValue;
  711. Procedure TestJSValue_Equal;
  712. Procedure TestJSValue_If;
  713. Procedure TestJSValue_Not;
  714. Procedure TestJSValue_Enum;
  715. Procedure TestJSValue_ClassInstance;
  716. Procedure TestJSValue_ClassOf;
  717. Procedure TestJSValue_ArrayOfJSValue;
  718. Procedure TestJSValue_ArrayLit;
  719. Procedure TestJSValue_Params;
  720. Procedure TestJSValue_UntypedParam;
  721. Procedure TestJSValue_FuncResultType;
  722. Procedure TestJSValue_ProcType_Assign;
  723. Procedure TestJSValue_ProcType_Equal;
  724. Procedure TestJSValue_ProcType_Param;
  725. Procedure TestJSValue_AssignToPointerFail;
  726. Procedure TestJSValue_OverloadDouble;
  727. Procedure TestJSValue_OverloadNativeInt;
  728. Procedure TestJSValue_OverloadWord;
  729. Procedure TestJSValue_OverloadString;
  730. Procedure TestJSValue_OverloadChar;
  731. Procedure TestJSValue_OverloadPointer;
  732. Procedure TestJSValue_ForIn;
  733. // RTTI
  734. Procedure TestRTTI_IntRange;
  735. Procedure TestRTTI_Double;
  736. Procedure TestRTTI_ProcType;
  737. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  738. Procedure TestRTTI_EnumAndSetType;
  739. Procedure TestRTTI_EnumRange;
  740. Procedure TestRTTI_AnonymousEnumType;
  741. Procedure TestRTTI_StaticArray;
  742. Procedure TestRTTI_DynArray;
  743. Procedure TestRTTI_ArrayNestedAnonymous;
  744. Procedure TestRTTI_PublishedMethodOverloadFail;
  745. Procedure TestRTTI_PublishedMethodExternalFail;
  746. Procedure TestRTTI_PublishedClassPropertyFail;
  747. Procedure TestRTTI_PublishedClassFieldFail;
  748. Procedure TestRTTI_PublishedFieldExternalFail;
  749. Procedure TestRTTI_Class_Field;
  750. Procedure TestRTTI_Class_Method;
  751. Procedure TestRTTI_Class_MethodArgFlags;
  752. Procedure TestRTTI_Class_Property;
  753. Procedure TestRTTI_Class_PropertyParams;
  754. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  755. Procedure TestRTTI_Class_OmitRTTI;
  756. Procedure TestRTTI_IndexModifier;
  757. Procedure TestRTTI_StoredModifier;
  758. Procedure TestRTTI_DefaultValue;
  759. Procedure TestRTTI_DefaultValueSet;
  760. Procedure TestRTTI_DefaultValueRangeType;
  761. Procedure TestRTTI_DefaultValueInherit;
  762. Procedure TestRTTI_OverrideMethod;
  763. Procedure TestRTTI_ReintroduceMethod;
  764. Procedure TestRTTI_OverloadProperty;
  765. // ToDo: array argument
  766. Procedure TestRTTI_ClassForward;
  767. Procedure TestRTTI_ClassOf;
  768. Procedure TestRTTI_Record;
  769. Procedure TestRTTI_RecordAnonymousArray;
  770. Procedure TestRTTI_LocalTypes;
  771. Procedure TestRTTI_TypeInfo_BaseTypes;
  772. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  773. Procedure TestRTTI_TypeInfo_LocalFail;
  774. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  775. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  776. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  777. Procedure TestRTTI_TypeInfo_FunctionClassType;
  778. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  779. Procedure TestRTTI_Interface_Corba;
  780. Procedure TestRTTI_Interface_COM;
  781. Procedure TestRTTI_ClassHelper;
  782. Procedure TestRTTI_ExternalClass;
  783. // Resourcestring
  784. Procedure TestResourcestringProgram;
  785. Procedure TestResourcestringUnit;
  786. Procedure TestResourcestringImplementation;
  787. // Attributes
  788. Procedure TestAttributes_Members;
  789. Procedure TestAttributes_Types;
  790. Procedure TestAttributes_HelperConstructor_Fail;
  791. // Assertions, checks
  792. procedure TestAssert;
  793. procedure TestAssert_SysUtils;
  794. procedure TestObjectChecks;
  795. procedure TestOverflowChecks_Int;
  796. procedure TestRangeChecks_AssignInt;
  797. procedure TestRangeChecks_AssignIntRange;
  798. procedure TestRangeChecks_AssignEnum;
  799. procedure TestRangeChecks_AssignEnumRange;
  800. procedure TestRangeChecks_AssignChar;
  801. procedure TestRangeChecks_AssignCharRange;
  802. procedure TestRangeChecks_ArrayIndex;
  803. procedure TestRangeChecks_ArrayOfRecIndex;
  804. procedure TestRangeChecks_StringIndex;
  805. procedure TestRangeChecks_TypecastInt;
  806. procedure TestRangeChecks_TypeHelperInt;
  807. end;
  808. function LinesToStr(Args: array of const): string;
  809. function ExtractFileUnitName(aFilename: string): string;
  810. function JSToStr(El: TJSElement): string;
  811. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  812. implementation
  813. function LinesToStr(Args: array of const): string;
  814. var
  815. s: String;
  816. i: Integer;
  817. begin
  818. s:='';
  819. for i:=Low(Args) to High(Args) do
  820. case Args[i].VType of
  821. vtChar: s += Args[i].VChar+LineEnding;
  822. vtString: s += Args[i].VString^+LineEnding;
  823. vtPChar: s += Args[i].VPChar+LineEnding;
  824. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  825. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  826. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  827. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  828. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  829. end;
  830. Result:=s;
  831. end;
  832. function ExtractFileUnitName(aFilename: string): string;
  833. var
  834. p: Integer;
  835. begin
  836. Result:=ExtractFileName(aFilename);
  837. if Result='' then exit;
  838. for p:=length(Result) downto 1 do
  839. case Result[p] of
  840. '/','\': exit;
  841. '.':
  842. begin
  843. Delete(Result,p,length(Result));
  844. exit;
  845. end;
  846. end;
  847. end;
  848. function JSToStr(El: TJSElement): string;
  849. var
  850. aWriter: TBufferWriter;
  851. aJSWriter: TJSWriter;
  852. begin
  853. aJSWriter:=nil;
  854. aWriter:=TBufferWriter.Create(1000);
  855. try
  856. aJSWriter:=TJSWriter.Create(aWriter);
  857. aJSWriter.IndentSize:=2;
  858. aJSWriter.WriteJS(El);
  859. Result:=aWriter.AsString;
  860. finally
  861. aJSWriter.Free;
  862. aWriter.Free;
  863. end;
  864. end;
  865. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  866. // search diff, ignore changes in spaces
  867. const
  868. SpaceChars = [#9,#10,#13,' '];
  869. var
  870. ExpectedP, ActualP: PChar;
  871. function FindLineEnd(p: PChar): PChar;
  872. begin
  873. Result:=p;
  874. while not (Result^ in [#0,#10,#13]) do inc(Result);
  875. end;
  876. function FindLineStart(p, MinP: PChar): PChar;
  877. begin
  878. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  879. Result:=p;
  880. end;
  881. procedure SkipLineEnd(var p: PChar);
  882. begin
  883. if p^ in [#10,#13] then
  884. begin
  885. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  886. inc(p,2)
  887. else
  888. inc(p);
  889. end;
  890. end;
  891. procedure DiffFound;
  892. var
  893. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  894. ExpLine, ActLine: String;
  895. i, LineNo, DiffLineNo: Integer;
  896. begin
  897. writeln('Diff found "',Msg,'". Lines:');
  898. // write correct lines
  899. p:=PChar(Expected);
  900. LineNo:=0;
  901. DiffLineNo:=0;
  902. repeat
  903. StartPos:=p;
  904. while not (p^ in [#0,#10,#13]) do inc(p);
  905. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  906. SkipLineEnd(p);
  907. inc(LineNo);
  908. if (p<=ExpectedP) and (p^<>#0) then
  909. begin
  910. writeln('= ',ExpLine);
  911. end else begin
  912. // diff line
  913. if DiffLineNo=0 then DiffLineNo:=LineNo;
  914. // write actual line
  915. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  916. ActLineEndP:=FindLineEnd(ActualP);
  917. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  918. writeln('- ',ActLine);
  919. // write expected line
  920. writeln('+ ',ExpLine);
  921. // write empty line with pointer ^
  922. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  923. writeln('^');
  924. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  925. CheckSrcDiff:=false;
  926. // write up to three following actual lines to get some context
  927. for i:=1 to 3 do begin
  928. ActLineStartP:=ActLineEndP;
  929. SkipLineEnd(ActLineStartP);
  930. if ActLineStartP^=#0 then break;
  931. ActLineEndP:=FindLineEnd(ActLineStartP);
  932. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  933. writeln('~ ',ActLine);
  934. end;
  935. exit;
  936. end;
  937. until p^=#0;
  938. writeln('DiffFound Actual:-----------------------');
  939. writeln(Actual);
  940. writeln('DiffFound Expected:---------------------');
  941. writeln(Expected);
  942. writeln('DiffFound ------------------------------');
  943. Msg:='diff found, but lines are the same, internal error';
  944. CheckSrcDiff:=false;
  945. end;
  946. var
  947. IsSpaceNeeded: Boolean;
  948. LastChar, Quote: Char;
  949. begin
  950. Result:=true;
  951. Msg:='';
  952. if Expected='' then Expected:=' ';
  953. if Actual='' then Actual:=' ';
  954. ExpectedP:=PChar(Expected);
  955. ActualP:=PChar(Actual);
  956. repeat
  957. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  958. case ExpectedP^ of
  959. #0:
  960. begin
  961. // check that rest of Actual has only spaces
  962. while ActualP^ in SpaceChars do inc(ActualP);
  963. if ActualP^<>#0 then
  964. begin
  965. DiffFound;
  966. exit;
  967. end;
  968. exit(true);
  969. end;
  970. ' ',#9,#10,#13:
  971. begin
  972. // skip space in Expected
  973. IsSpaceNeeded:=false;
  974. if ExpectedP>PChar(Expected) then
  975. LastChar:=ExpectedP[-1]
  976. else
  977. LastChar:=#0;
  978. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  979. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  980. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  981. IsSpaceNeeded:=true;
  982. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  983. begin
  984. DiffFound;
  985. exit;
  986. end;
  987. while ActualP^ in SpaceChars do inc(ActualP);
  988. end;
  989. '''','"':
  990. begin
  991. while ActualP^ in SpaceChars do inc(ActualP);
  992. if ExpectedP^<>ActualP^ then
  993. begin
  994. DiffFound;
  995. exit;
  996. end;
  997. Quote:=ExpectedP^;
  998. repeat
  999. inc(ExpectedP);
  1000. inc(ActualP);
  1001. if ExpectedP^<>ActualP^ then
  1002. begin
  1003. DiffFound;
  1004. exit;
  1005. end;
  1006. if (ExpectedP^ in [#0,#10,#13]) then
  1007. break
  1008. else if (ExpectedP^=Quote) then
  1009. begin
  1010. inc(ExpectedP);
  1011. inc(ActualP);
  1012. break;
  1013. end;
  1014. until false;
  1015. end;
  1016. else
  1017. while ActualP^ in SpaceChars do inc(ActualP);
  1018. if ExpectedP^<>ActualP^ then
  1019. begin
  1020. DiffFound;
  1021. exit;
  1022. end;
  1023. inc(ExpectedP);
  1024. inc(ActualP);
  1025. end;
  1026. until false;
  1027. end;
  1028. { TTestEnginePasResolver }
  1029. destructor TTestEnginePasResolver.Destroy;
  1030. begin
  1031. FreeAndNil(FStreamResolver);
  1032. FreeAndNil(FParser);
  1033. FreeAndNil(FScanner);
  1034. FreeAndNil(FStreamResolver);
  1035. if Module<>nil then
  1036. begin
  1037. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1038. FModule:=nil;
  1039. end;
  1040. inherited Destroy;
  1041. end;
  1042. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1043. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1044. begin
  1045. Result:=nil;
  1046. if InFilename<>'' then
  1047. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1048. if Assigned(OnFindUnit) then
  1049. Result:=OnFindUnit(AName);
  1050. if NameExpr=nil then ;
  1051. end;
  1052. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1053. begin
  1054. // do not parse recursively
  1055. // parse via the queue
  1056. if Section=nil then ;
  1057. end;
  1058. { TCustomTestModule }
  1059. function TCustomTestModule.GetMsgCount: integer;
  1060. begin
  1061. Result:=FHintMsgs.Count;
  1062. end;
  1063. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1064. begin
  1065. Result:=TTestHintMessage(FHintMsgs[Index]);
  1066. end;
  1067. function TCustomTestModule.GetResolverCount: integer;
  1068. begin
  1069. Result:=FModules.Count;
  1070. end;
  1071. function TCustomTestModule.GetResolvers(Index: integer
  1072. ): TTestEnginePasResolver;
  1073. begin
  1074. Result:=TTestEnginePasResolver(FModules[Index]);
  1075. end;
  1076. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1077. ): TPasModule;
  1078. var
  1079. DefNamespace: String;
  1080. begin
  1081. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1082. if (Pos('.',aUnitName)<1) then
  1083. begin
  1084. DefNamespace:=GetDefaultNamespace;
  1085. if DefNamespace<>'' then
  1086. begin
  1087. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1088. if Result<>nil then exit;
  1089. end;
  1090. end;
  1091. Result:=LoadUnit(aUnitName);
  1092. if Result<>nil then exit;
  1093. {$IFDEF VerbosePas2JS}
  1094. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1095. {$ENDIF}
  1096. Fail('can''t find unit "'+aUnitName+'"');
  1097. end;
  1098. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1099. var
  1100. aParser: TPasParser;
  1101. Item: TTestHintMessage;
  1102. begin
  1103. aParser:=Sender as TPasParser;
  1104. Item:=TTestHintMessage.Create;
  1105. Item.Id:=aParser.LastMsgNumber;
  1106. Item.MsgType:=aParser.LastMsgType;
  1107. Item.MsgNumber:=aParser.LastMsgNumber;
  1108. Item.Msg:=Msg;
  1109. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1110. {$IFDEF VerbosePas2JS}
  1111. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1112. {$ENDIF}
  1113. FHintMsgs.Add(Item);
  1114. end;
  1115. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1116. );
  1117. var
  1118. aResolver: TTestEnginePasResolver;
  1119. Item: TTestHintMessage;
  1120. begin
  1121. aResolver:=Sender as TTestEnginePasResolver;
  1122. Item:=TTestHintMessage.Create;
  1123. Item.Id:=aResolver.LastMsgId;
  1124. Item.MsgType:=aResolver.LastMsgType;
  1125. Item.MsgNumber:=aResolver.LastMsgNumber;
  1126. Item.Msg:=Msg;
  1127. Item.SourcePos:=aResolver.LastSourcePos;
  1128. {$IFDEF VerbosePas2JS}
  1129. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1130. {$ENDIF}
  1131. FHintMsgs.Add(Item);
  1132. end;
  1133. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1134. var
  1135. Item: TTestHintMessage;
  1136. aScanner: TPas2jsPasScanner;
  1137. begin
  1138. aScanner:=Sender as TPas2jsPasScanner;
  1139. Item:=TTestHintMessage.Create;
  1140. Item.Id:=aScanner.LastMsgNumber;
  1141. Item.MsgType:=aScanner.LastMsgType;
  1142. Item.MsgNumber:=aScanner.LastMsgNumber;
  1143. Item.Msg:=Msg;
  1144. Item.SourcePos:=aScanner.CurSourcePos;
  1145. {$IFDEF VerbosePas2JS}
  1146. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1147. {$ENDIF}
  1148. FHintMsgs.Add(Item);
  1149. end;
  1150. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1151. var
  1152. i: Integer;
  1153. CurEngine: TTestEnginePasResolver;
  1154. CurUnitName: String;
  1155. begin
  1156. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1157. Result:=nil;
  1158. if (Module.ClassType=TPasModule)
  1159. and (CompareText(Module.Name,aUnitName)=0) then
  1160. exit(Module);
  1161. for i:=0 to ResolverCount-1 do
  1162. begin
  1163. CurEngine:=Resolvers[i];
  1164. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1165. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1166. if CompareText(aUnitName,CurUnitName)=0 then
  1167. begin
  1168. Result:=CurEngine.Module;
  1169. if Result<>nil then exit;
  1170. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1171. FileResolver.FindSourceFile(aUnitName);
  1172. CurEngine.StreamResolver:=TStreamResolver.Create;
  1173. CurEngine.StreamResolver.OwnsStreams:=True;
  1174. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1175. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1176. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1177. InitScanner(CurEngine.Scanner);
  1178. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1179. CurEngine.Parser.Options:=po_tcmodules;
  1180. if CompareText(CurUnitName,'System')=0 then
  1181. CurEngine.Parser.ImplicitUses.Clear;
  1182. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1183. try
  1184. CurEngine.Parser.NextToken;
  1185. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1186. except
  1187. on E: Exception do
  1188. HandleException(E);
  1189. end;
  1190. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1191. Result:=CurEngine.Module;
  1192. exit;
  1193. end;
  1194. end;
  1195. end;
  1196. procedure TCustomTestModule.SetUp;
  1197. begin
  1198. {$IFDEF EnablePasTreeGlobalRefCount}
  1199. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1200. {$ENDIF}
  1201. if FModules<>nil then
  1202. begin
  1203. writeln('TCustomTestModule.SetUp FModules<>nil');
  1204. Halt;
  1205. end;
  1206. inherited SetUp;
  1207. FSkipTests:=false;
  1208. FSource:=TStringList.Create;
  1209. FModules:=TObjectList.Create(true);
  1210. FFilename:='test1.pp';
  1211. FFileResolver:=TStreamResolver.Create;
  1212. FFileResolver.OwnsStreams:=True;
  1213. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1214. InitScanner(FScanner);
  1215. FEngine:=AddModule(Filename);
  1216. FEngine.Scanner:=FScanner;
  1217. FScanner.Resolver:=FEngine;
  1218. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1219. FParser.OnLog:=@OnParserLog;
  1220. FEngine.Parser:=FParser;
  1221. Parser.Options:=po_tcmodules;
  1222. FModule:=Nil;
  1223. FConverter:=CreateConverter;
  1224. FExpectedErrorClass:=nil;
  1225. end;
  1226. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1227. begin
  1228. Result:=TPasToJSConverter.Create;
  1229. Result.Options:=co_tcmodules;
  1230. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1231. end;
  1232. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1233. begin
  1234. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1235. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1236. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1237. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1238. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1239. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1240. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1241. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1242. aScanner.OnLog:=@OnScannerLog;
  1243. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1244. end;
  1245. procedure TCustomTestModule.TearDown;
  1246. {$IFDEF CheckPasTreeRefCount}
  1247. var
  1248. El: TPasElement;
  1249. {$ENDIF}
  1250. var
  1251. i: Integer;
  1252. CurModule: TPasModule;
  1253. begin
  1254. FHintMsgs.Clear;
  1255. FHintMsgsGood.Clear;
  1256. FSkipTests:=false;
  1257. FJSRegModuleCall:=nil;
  1258. FJSModuleCallArgs:=nil;
  1259. FJSImplentationUses:=nil;
  1260. FJSInterfaceUses:=nil;
  1261. FJSModuleSrc:=nil;
  1262. FJSInitBody:=nil;
  1263. FreeAndNil(FJSSource);
  1264. FreeAndNil(FJSModule);
  1265. FreeAndNil(FConverter);
  1266. Engine.Clear;
  1267. FreeAndNil(FSource);
  1268. FreeAndNil(FFileResolver);
  1269. if FModules<>nil then
  1270. begin
  1271. for i:=0 to FModules.Count-1 do
  1272. begin
  1273. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1274. if CurModule=nil then continue;
  1275. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1276. CurModule.ReleaseUsedUnits;
  1277. end;
  1278. if FModule<>nil then
  1279. FModule.ReleaseUsedUnits;
  1280. for i:=0 to FModules.Count-1 do
  1281. begin
  1282. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1283. if CurModule=nil then continue;
  1284. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1285. end;
  1286. FreeAndNil(FModules);
  1287. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1288. FEngine:=nil;
  1289. end;
  1290. inherited TearDown;
  1291. {$IFDEF EnablePasTreeGlobalRefCount}
  1292. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1293. begin
  1294. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1295. {$IFDEF CheckPasTreeRefCount}
  1296. El:=TPasElement.FirstRefEl;
  1297. while El<>nil do
  1298. begin
  1299. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1300. for i:=0 to El.RefIds.Count-1 do
  1301. writeln(' ',El.RefIds[i]);
  1302. El:=El.NextRefEl;
  1303. end;
  1304. {$ENDIF}
  1305. Halt;
  1306. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1307. end;
  1308. {$ENDIF}
  1309. end;
  1310. procedure TCustomTestModule.Add(Line: string);
  1311. begin
  1312. Source.Add(Line);
  1313. end;
  1314. procedure TCustomTestModule.Add(const Lines: array of string);
  1315. var
  1316. i: Integer;
  1317. begin
  1318. for i:=low(Lines) to high(Lines) do
  1319. Add(Lines[i]);
  1320. end;
  1321. procedure TCustomTestModule.StartParsing;
  1322. var
  1323. Src: String;
  1324. begin
  1325. Src:=Source.Text;
  1326. FEngine.Source:=Src;
  1327. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1328. Scanner.OpenFile(FileName);
  1329. Writeln('// Test : ',Self.TestName);
  1330. Writeln(Src);
  1331. end;
  1332. procedure TCustomTestModule.ParseModuleQueue;
  1333. var
  1334. i: Integer;
  1335. CurResolver: TTestEnginePasResolver;
  1336. Found: Boolean;
  1337. Section: TPasSection;
  1338. begin
  1339. // parse til exception or all modules finished
  1340. while not SkipTests do
  1341. begin
  1342. Found:=false;
  1343. for i:=0 to ResolverCount-1 do
  1344. begin
  1345. CurResolver:=Resolvers[i];
  1346. if CurResolver.CurrentParser=nil then continue;
  1347. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1348. continue;
  1349. CurResolver.Parser.ParseContinue;
  1350. Found:=true;
  1351. break;
  1352. end;
  1353. if not Found then break;
  1354. end;
  1355. for i:=0 to ResolverCount-1 do
  1356. begin
  1357. CurResolver:=Resolvers[i];
  1358. if CurResolver.Parser=nil then
  1359. begin
  1360. if CurResolver.CurrentParser<>nil then
  1361. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1362. continue;
  1363. end;
  1364. if CurResolver.Parser.CurModule<>nil then
  1365. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1366. end;
  1367. end;
  1368. procedure TCustomTestModule.ParseModule;
  1369. begin
  1370. if SkipTests then exit;
  1371. FFirstPasStatement:=nil;
  1372. try
  1373. StartParsing;
  1374. Parser.ParseMain(FModule);
  1375. ParseModuleQueue;
  1376. except
  1377. on E: Exception do
  1378. HandleException(E);
  1379. end;
  1380. if SkipTests then exit;
  1381. AssertNotNull('Module resulted in Module',Module);
  1382. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1383. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1384. end;
  1385. procedure TCustomTestModule.ParseProgram;
  1386. begin
  1387. if SkipTests then exit;
  1388. ParseModule;
  1389. if SkipTests then exit;
  1390. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1391. FPasProgram:=TPasProgram(Module);
  1392. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1393. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1394. if (PasProgram.InitializationSection.Elements.Count>0) then
  1395. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1396. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1397. end;
  1398. procedure TCustomTestModule.ParseUnit;
  1399. begin
  1400. if SkipTests then exit;
  1401. ParseModule;
  1402. if SkipTests then exit;
  1403. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1404. AssertNotNull('Has interface section',Module.InterfaceSection);
  1405. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1406. if (Module.InitializationSection<>nil)
  1407. and (Module.InitializationSection.Elements.Count>0)
  1408. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1409. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1410. end;
  1411. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1412. ): TTestEnginePasResolver;
  1413. var
  1414. i: Integer;
  1415. begin
  1416. for i:=0 to ResolverCount-1 do
  1417. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1418. exit(Resolvers[i]);
  1419. Result:=nil;
  1420. end;
  1421. function TCustomTestModule.AddModule(aFilename: string
  1422. ): TTestEnginePasResolver;
  1423. begin
  1424. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1425. if FindModuleWithFilename(aFilename)<>nil then
  1426. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1427. Result:=TTestEnginePasResolver.Create;
  1428. Result.Filename:=aFilename;
  1429. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1430. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1431. Result.OnLog:=@OnPasResolverLog;
  1432. FModules.Add(Result);
  1433. end;
  1434. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1435. ): TTestEnginePasResolver;
  1436. begin
  1437. Result:=AddModule(aFilename);
  1438. Result.Source:=Src;
  1439. end;
  1440. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1441. ImplementationSrc: string): TTestEnginePasResolver;
  1442. var
  1443. Src: String;
  1444. begin
  1445. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1446. Src+=LineEnding;
  1447. Src+='interface'+LineEnding;
  1448. Src+=LineEnding;
  1449. Src+=InterfaceSrc;
  1450. Src+='implementation'+LineEnding;
  1451. Src+=LineEnding;
  1452. Src+=ImplementationSrc;
  1453. Src+='end.'+LineEnding;
  1454. Result:=AddModuleWithSrc(aFilename,Src);
  1455. end;
  1456. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1457. var
  1458. Intf, Impl: TStringList;
  1459. begin
  1460. Intf:=TStringList.Create;
  1461. // interface
  1462. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1463. Intf.Add('{$modeswitch externalclass}');
  1464. Intf.Add('type');
  1465. Intf.Add(' integer=longint;');
  1466. Intf.Add(' sizeint=nativeint;');
  1467. //'const',
  1468. //' LineEnding = #10;',
  1469. //' DirectorySeparator = ''/'';',
  1470. //' DriveSeparator = '''';',
  1471. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1472. //' AllowDriveSeparators : set of char = [];',
  1473. if supTObject in Parts then
  1474. Intf.AddStrings([
  1475. 'type',
  1476. ' TClass = class of TObject;',
  1477. ' TObject = class',
  1478. ' constructor Create;',
  1479. ' destructor Destroy; virtual;',
  1480. ' class function ClassType: TClass; assembler;',
  1481. ' class function ClassName: String; assembler;',
  1482. ' class function ClassNameIs(const Name: string): boolean;',
  1483. ' class function ClassParent: TClass; assembler;',
  1484. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1485. ' class function UnitName: String; assembler;',
  1486. ' procedure AfterConstruction; virtual;',
  1487. ' procedure BeforeDestruction;virtual;',
  1488. ' function Equals(Obj: TObject): boolean; virtual;',
  1489. ' function ToString: String; virtual;',
  1490. ' end;']);
  1491. if supTVarRec in Parts then
  1492. Intf.AddStrings([
  1493. 'const',
  1494. ' vtInteger = 0;',
  1495. ' vtBoolean = 1;',
  1496. ' vtJSValue = 19;',
  1497. 'type',
  1498. ' PVarRec = ^TVarRec;',
  1499. ' TVarRec = record',
  1500. ' VType : byte;',
  1501. ' VJSValue: JSValue;',
  1502. ' vInteger: longint external name ''VJSValue'';',
  1503. ' vBoolean: boolean external name ''VJSValue'';',
  1504. ' end;',
  1505. ' TVarRecArray = array of TVarRec;',
  1506. 'function VarRecs: TVarRecArray; varargs;',
  1507. '']);
  1508. if supTypeInfo in Parts then
  1509. begin
  1510. Intf.AddStrings([
  1511. 'type',
  1512. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1513. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1514. ' end;',
  1515. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1516. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1517. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1518. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1519. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1520. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1521. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1522. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1523. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1524. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1525. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1526. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1527. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1528. '']);
  1529. end;
  1530. Intf.Add('var');
  1531. Intf.Add(' ExitCode: Longint = 0;');
  1532. // implementation
  1533. Impl:=TStringList.Create;
  1534. if supTObject in Parts then
  1535. Impl.AddStrings([
  1536. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1537. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1538. 'asm',
  1539. 'end;',
  1540. 'constructor TObject.Create; begin end;',
  1541. 'destructor TObject.Destroy; begin end;',
  1542. 'class function TObject.ClassType: TClass; assembler;',
  1543. 'asm',
  1544. 'end;',
  1545. 'class function TObject.ClassName: String; assembler;',
  1546. 'asm',
  1547. 'end;',
  1548. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1549. 'begin',
  1550. ' Result:=SameText(Name,ClassName);',
  1551. 'end;',
  1552. 'class function TObject.ClassParent: TClass; assembler;',
  1553. 'asm',
  1554. 'end;',
  1555. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1556. 'asm',
  1557. 'end;',
  1558. 'class function TObject.UnitName: String; assembler;',
  1559. 'asm',
  1560. 'end;',
  1561. 'procedure TObject.AfterConstruction; begin end;',
  1562. 'procedure TObject.BeforeDestruction; begin end;',
  1563. 'function TObject.Equals(Obj: TObject): boolean;',
  1564. 'begin',
  1565. ' Result:=Obj=Self;',
  1566. 'end;',
  1567. 'function TObject.ToString: String;',
  1568. 'begin',
  1569. ' Result:=ClassName;',
  1570. 'end;'
  1571. ]);
  1572. if supTVarRec in Parts then
  1573. Impl.AddStrings([
  1574. 'function VarRecs: TVarRecArray; varargs;',
  1575. 'var',
  1576. ' v: PVarRec;',
  1577. 'begin',
  1578. ' v^.VType:=1;',
  1579. ' v^.VJSValue:=2;',
  1580. 'end;',
  1581. '']);
  1582. try
  1583. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1584. finally
  1585. Intf.Free;
  1586. Impl.Free;
  1587. end;
  1588. end;
  1589. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1590. SystemUnitParts: TSystemUnitParts);
  1591. begin
  1592. if NeedSystemUnit then
  1593. AddSystemUnit(SystemUnitParts)
  1594. else
  1595. Parser.ImplicitUses.Clear;
  1596. Add('program '+ExtractFileUnitName(Filename)+';');
  1597. Add('');
  1598. end;
  1599. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1600. SystemUnitParts: TSystemUnitParts);
  1601. begin
  1602. if NeedSystemUnit then
  1603. AddSystemUnit(SystemUnitParts)
  1604. else
  1605. Parser.ImplicitUses.Clear;
  1606. Add('unit Test1;');
  1607. Add('');
  1608. end;
  1609. procedure TCustomTestModule.ConvertModule;
  1610. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1611. out UsesLit: TJSArrayLiteral);
  1612. var
  1613. i: Integer;
  1614. Item: TJSElement;
  1615. Lit: TJSLiteral;
  1616. begin
  1617. UsesLit:=nil;
  1618. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1619. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1620. exit; // null is ok
  1621. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1622. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1623. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1624. begin
  1625. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1626. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1627. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1628. Lit:=TJSLiteral(Item);
  1629. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1630. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1631. end;
  1632. end;
  1633. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1634. out Src: TJSSourceElements);
  1635. var
  1636. FunDecl: TJSFunctionDeclarationStatement;
  1637. FunDef: TJSFuncDef;
  1638. FunBody: TJSFunctionBody;
  1639. begin
  1640. Src:=nil;
  1641. AssertNotNull(ParamName,Arg.Expr);
  1642. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1643. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1644. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1645. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1646. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1647. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1648. AssertNotNull(ParamName+' body',FunDef.Body);
  1649. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1650. FunBody:=FunDef.Body as TJSFunctionBody;
  1651. AssertNotNull(ParamName+' body.A',FunBody.A);
  1652. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1653. Src:=FunBody.A as TJSSourceElements;
  1654. end;
  1655. var
  1656. ModuleNameExpr: TJSLiteral;
  1657. InitFunction: TJSFunctionDeclarationStatement;
  1658. InitAssign: TJSSimpleAssignStatement;
  1659. InitName: String;
  1660. LastNode: TJSElement;
  1661. Arg: TJSArrayLiteralElement;
  1662. begin
  1663. if SkipTests then exit;
  1664. try
  1665. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1666. except
  1667. on E: Exception do
  1668. HandleException(E);
  1669. end;
  1670. if SkipTests then exit;
  1671. if ExpectedErrorClass<>nil then
  1672. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1673. FJSSource:=TStringList.Create;
  1674. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1675. {$IFDEF VerbosePas2JS}
  1676. writeln('TTestModule.ConvertModule JS:');
  1677. write(FJSSource.Text);
  1678. {$ENDIF}
  1679. // rtl.module(...
  1680. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1681. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1682. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1683. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1684. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1685. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1686. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1687. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1688. // parameter 'unitname'
  1689. if JSModuleCallArgs.Elements.Count<1 then
  1690. Fail('rtl.module first param unit missing');
  1691. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1692. AssertNotNull('module name param',Arg.Expr);
  1693. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1694. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1695. if Module is TPasProgram then
  1696. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1697. else
  1698. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1699. // main uses section
  1700. if JSModuleCallArgs.Elements.Count<2 then
  1701. Fail('rtl.module second param main uses missing');
  1702. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1703. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1704. // program/library/interface function()
  1705. if JSModuleCallArgs.Elements.Count<3 then
  1706. Fail('rtl.module third param intf-function missing');
  1707. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1708. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1709. // search for $mod.$init or $mod.$main - the last statement
  1710. if Module is TPasProgram then
  1711. begin
  1712. InitName:='$main';
  1713. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1714. end
  1715. else
  1716. InitName:='$init';
  1717. FJSInitBody:=nil;
  1718. if JSModuleSrc.Statements.Count>0 then
  1719. begin
  1720. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1721. if LastNode is TJSSimpleAssignStatement then
  1722. begin
  1723. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1724. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1725. begin
  1726. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1727. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1728. end
  1729. else if Module is TPasProgram then
  1730. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1731. end;
  1732. end;
  1733. // optional: implementation uses section
  1734. if JSModuleCallArgs.Elements.Count<4 then
  1735. exit;
  1736. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1737. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1738. // optional: implementation function()
  1739. if JSModuleCallArgs.Elements.Count<5 then
  1740. exit;
  1741. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1742. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1743. end;
  1744. procedure TCustomTestModule.ConvertProgram;
  1745. begin
  1746. Add('end.');
  1747. ParseProgram;
  1748. ConvertModule;
  1749. end;
  1750. procedure TCustomTestModule.ConvertUnit;
  1751. begin
  1752. Add('end.');
  1753. ParseUnit;
  1754. ConvertModule;
  1755. end;
  1756. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1757. begin
  1758. Result:=tcmodules.JSToStr(El);
  1759. end;
  1760. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1761. DottedName: string);
  1762. begin
  1763. if DottedName='' then
  1764. begin
  1765. AssertNull(Msg,El);
  1766. end
  1767. else
  1768. begin
  1769. AssertNotNull(Msg,El);
  1770. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1771. end;
  1772. end;
  1773. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1774. begin
  1775. if El=nil then
  1776. Result:=''
  1777. else if El is TJSPrimaryExpressionIdent then
  1778. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1779. else if El is TJSDotMemberExpression then
  1780. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1781. else
  1782. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1783. end;
  1784. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1785. InitStatements: string; ImplStatements: string);
  1786. var
  1787. ActualSrc, ExpectedSrc, InitName: String;
  1788. begin
  1789. ActualSrc:=JSToStr(JSModuleSrc);
  1790. ExpectedSrc:=
  1791. 'var $mod = this;'+LineEnding
  1792. +Statements;
  1793. if coUseStrict in Converter.Options then
  1794. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1795. if Module is TPasProgram then
  1796. InitName:='$main'
  1797. else
  1798. InitName:='$init';
  1799. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1800. ExpectedSrc:=ExpectedSrc+LineEnding
  1801. +'$mod.'+InitName+' = function () {'+LineEnding
  1802. +InitStatements
  1803. +'};'+LineEnding;
  1804. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1805. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1806. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1807. if (JSImplementationSrc<>nil) then
  1808. begin
  1809. ActualSrc:=JSToStr(JSImplementationSrc);
  1810. ExpectedSrc:=
  1811. 'var $mod = this;'+LineEnding
  1812. +'var $impl = $mod.$impl;'+LineEnding
  1813. +ImplStatements;
  1814. end
  1815. else
  1816. begin
  1817. ActualSrc:='';
  1818. ExpectedSrc:=ImplStatements;
  1819. end;
  1820. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1821. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1822. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1823. end;
  1824. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1825. // search diff, ignore changes in spaces
  1826. var
  1827. s: string;
  1828. begin
  1829. if CheckSrcDiff(Expected,Actual,s) then exit;
  1830. Fail(Msg+': '+s);
  1831. end;
  1832. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1833. var
  1834. aResolver: TTestEnginePasResolver;
  1835. aConverter: TPasToJSConverter;
  1836. aJSModule: TJSSourceElements;
  1837. ActualSrc: String;
  1838. begin
  1839. aResolver:=GetResolver(Filename);
  1840. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1841. {$IFDEF VerbosePas2JS}
  1842. writeln('CheckUnit '+Filename+' converting ...');
  1843. {$ENDIF}
  1844. aConverter:=CreateConverter;
  1845. aJSModule:=nil;
  1846. try
  1847. try
  1848. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1849. except
  1850. on E: Exception do
  1851. HandleException(E);
  1852. end;
  1853. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1854. {$IFDEF VerbosePas2JS}
  1855. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1856. write(aResolver.Source);
  1857. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1858. write(ActualSrc);
  1859. {$ENDIF}
  1860. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1861. finally
  1862. aJSModule.Free;
  1863. aConverter.Free;
  1864. end;
  1865. end;
  1866. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1867. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1868. var
  1869. i: Integer;
  1870. Item: TTestHintMessage;
  1871. Expected,Actual: string;
  1872. begin
  1873. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1874. for i:=0 to MsgCount-1 do
  1875. begin
  1876. Item:=Msgs[i];
  1877. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1878. if (Marker<>nil) then
  1879. begin
  1880. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1881. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1882. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1883. end;
  1884. // found
  1885. FHintMsgsGood.Add(Item);
  1886. str(Item.MsgType,Actual);
  1887. str(MsgType,Expected);
  1888. AssertEquals('MsgType',Expected,Actual);
  1889. exit;
  1890. end;
  1891. // needed message missing -> show emitted messages
  1892. WriteSources('',0,0);
  1893. for i:=0 to MsgCount-1 do
  1894. begin
  1895. Item:=Msgs[i];
  1896. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1897. ' ('+IntToStr(Item.MsgNumber),')');
  1898. if Marker<>nil then
  1899. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1900. writeln(' {',Item.Msg,'}');
  1901. end;
  1902. str(MsgType,Expected);
  1903. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1904. if Marker<>nil then
  1905. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1906. Actual:=Actual+' '+Msg;
  1907. Fail(Actual);
  1908. end;
  1909. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1910. );
  1911. var
  1912. i: Integer;
  1913. s, Txt: String;
  1914. Msg: TTestHintMessage;
  1915. begin
  1916. for i:=0 to MsgCount-1 do
  1917. begin
  1918. Msg:=Msgs[i];
  1919. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1920. s:='';
  1921. str(Msg.MsgType,s);
  1922. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1923. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1924. if WithSourcePos then
  1925. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1926. Txt:=Txt+' {'+Msg.Msg+'}';
  1927. Fail(Txt);
  1928. end;
  1929. end;
  1930. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1931. MsgNumber: integer);
  1932. begin
  1933. ExpectedErrorClass:=EScannerError;
  1934. ExpectedErrorMsg:=Msg;
  1935. ExpectedErrorNumber:=MsgNumber;
  1936. end;
  1937. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1938. MsgNumber: integer);
  1939. begin
  1940. ExpectedErrorClass:=EParserError;
  1941. ExpectedErrorMsg:=Msg;
  1942. ExpectedErrorNumber:=MsgNumber;
  1943. end;
  1944. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1945. MsgNumber: integer);
  1946. begin
  1947. ExpectedErrorClass:=EPasResolve;
  1948. ExpectedErrorMsg:=Msg;
  1949. ExpectedErrorNumber:=MsgNumber;
  1950. end;
  1951. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1952. MsgNumber: integer);
  1953. begin
  1954. ExpectedErrorClass:=EPas2JS;
  1955. ExpectedErrorMsg:=Msg;
  1956. ExpectedErrorNumber:=MsgNumber;
  1957. end;
  1958. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1959. var
  1960. MsgNumber: Integer;
  1961. Msg: String;
  1962. begin
  1963. Result:=false;
  1964. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1965. Msg:=E.Message;
  1966. if E is EPas2JS then
  1967. MsgNumber:=EPas2JS(E).MsgNumber
  1968. else if E is EPasResolve then
  1969. MsgNumber:=EPasResolve(E).MsgNumber
  1970. else if E is EParserError then
  1971. MsgNumber:=Parser.LastMsgNumber
  1972. else if E is EScannerError then
  1973. begin
  1974. MsgNumber:=Scanner.LastMsgNumber;
  1975. Msg:=Scanner.LastMsg;
  1976. end
  1977. else
  1978. MsgNumber:=0;
  1979. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  1980. if Result then
  1981. SkipTests:=true;
  1982. end;
  1983. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1984. begin
  1985. if IsErrorExpected(E) then exit;
  1986. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  1987. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  1988. +' '+Scanner.CurFilename
  1989. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  1990. FailException(E);
  1991. end;
  1992. procedure TCustomTestModule.HandleParserError(E: EParserError);
  1993. begin
  1994. if IsErrorExpected(E) then exit;
  1995. WriteSources(E.Filename,E.Row,E.Column);
  1996. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  1997. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  1998. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  1999. );
  2000. FailException(E);
  2001. end;
  2002. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2003. var
  2004. P: TPasSourcePos;
  2005. begin
  2006. if IsErrorExpected(E) then exit;
  2007. P:=E.SourcePos;
  2008. WriteSources(P.FileName,P.Row,P.Column);
  2009. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2010. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2011. FailException(E);
  2012. end;
  2013. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2014. var
  2015. Row, Col: integer;
  2016. begin
  2017. if IsErrorExpected(E) then exit;
  2018. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2019. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2020. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2021. +' '+E.PasElement.SourceFilename
  2022. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2023. FailException(E);
  2024. end;
  2025. procedure TCustomTestModule.HandleException(E: Exception);
  2026. begin
  2027. if E is EScannerError then
  2028. HandleScannerError(EScannerError(E))
  2029. else if E is EParserError then
  2030. HandleParserError(EParserError(E))
  2031. else if E is EPasResolve then
  2032. HandlePasResolveError(EPasResolve(E))
  2033. else if E is EPas2JS then
  2034. HandlePas2JSError(EPas2JS(E))
  2035. else
  2036. begin
  2037. if IsErrorExpected(E) then exit;
  2038. if not (E is EAssertionFailedError) then
  2039. begin
  2040. WriteSources('',0,0);
  2041. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2042. end;
  2043. FailException(E);
  2044. end;
  2045. end;
  2046. procedure TCustomTestModule.FailException(E: Exception);
  2047. var
  2048. MsgNumber: Integer;
  2049. begin
  2050. if ExpectedErrorClass<>nil then
  2051. begin
  2052. if FExpectedErrorClass=E.ClassType then
  2053. begin
  2054. if E is EPas2JS then
  2055. MsgNumber:=EPas2JS(E).MsgNumber
  2056. else if E is EPasResolve then
  2057. MsgNumber:=EPasResolve(E).MsgNumber
  2058. else if E is EParserError then
  2059. MsgNumber:=Parser.LastMsgNumber
  2060. else if E is EScannerError then
  2061. MsgNumber:=Scanner.LastMsgNumber
  2062. else
  2063. MsgNumber:=0;
  2064. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2065. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2066. ExpectedErrorNumber,MsgNumber);
  2067. end else begin
  2068. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2069. end;
  2070. end;
  2071. Fail(E.Message);
  2072. end;
  2073. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2074. aCol: integer);
  2075. var
  2076. IsSrc: Boolean;
  2077. i, j: Integer;
  2078. SrcLines: TStringList;
  2079. Line: string;
  2080. aModule: TTestEnginePasResolver;
  2081. begin
  2082. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2083. for i:=0 to ResolverCount-1 do
  2084. begin
  2085. aModule:=Resolvers[i];
  2086. SrcLines:=TStringList.Create;
  2087. try
  2088. SrcLines.Text:=aModule.Source;
  2089. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2090. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2091. for j:=1 to SrcLines.Count do
  2092. begin
  2093. Line:=SrcLines[j-1];
  2094. if IsSrc and (j=aRow) then
  2095. begin
  2096. write('*');
  2097. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2098. end;
  2099. writeln(Format('%:4d: ',[j]),Line);
  2100. end;
  2101. finally
  2102. SrcLines.Free;
  2103. end;
  2104. end;
  2105. end;
  2106. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2107. var
  2108. i: Integer;
  2109. begin
  2110. for i:=0 to ResolverCount-1 do
  2111. if Filename=Resolvers[i].Filename then exit(i);
  2112. Result:=-1;
  2113. end;
  2114. function TCustomTestModule.GetResolver(const Filename: string
  2115. ): TTestEnginePasResolver;
  2116. var
  2117. i: Integer;
  2118. begin
  2119. i:=IndexOfResolver(Filename);
  2120. if i<0 then exit(nil);
  2121. Result:=Resolvers[i];
  2122. end;
  2123. function TCustomTestModule.GetDefaultNamespace: string;
  2124. var
  2125. C: TClass;
  2126. begin
  2127. Result:='';
  2128. if FModule=nil then exit;
  2129. C:=FModule.ClassType;
  2130. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2131. Result:=Engine.DefaultNameSpace;
  2132. end;
  2133. constructor TCustomTestModule.Create;
  2134. begin
  2135. inherited Create;
  2136. FHintMsgs:=TObjectList.Create(true);
  2137. FHintMsgsGood:=TFPList.Create;
  2138. end;
  2139. destructor TCustomTestModule.Destroy;
  2140. begin
  2141. FreeAndNil(FHintMsgs);
  2142. FreeAndNil(FHintMsgsGood);
  2143. inherited Destroy;
  2144. end;
  2145. { TTestModule }
  2146. procedure TTestModule.TestReservedWords;
  2147. var
  2148. i: integer;
  2149. begin
  2150. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2151. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2152. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2153. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2154. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2155. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2156. end;
  2157. procedure TTestModule.TestEmptyProgram;
  2158. begin
  2159. StartProgram(false);
  2160. Add('begin');
  2161. ConvertProgram;
  2162. CheckSource('TestEmptyProgram','','');
  2163. end;
  2164. procedure TTestModule.TestEmptyProgramUseStrict;
  2165. begin
  2166. Converter.Options:=Converter.Options+[coUseStrict];
  2167. StartProgram(false);
  2168. Add('begin');
  2169. ConvertProgram;
  2170. CheckSource('TestEmptyProgramUseStrict','','');
  2171. end;
  2172. procedure TTestModule.TestEmptyUnit;
  2173. begin
  2174. StartUnit(false);
  2175. Add('interface');
  2176. Add('implementation');
  2177. ConvertUnit;
  2178. CheckSource('TestEmptyUnit',
  2179. LinesToStr([
  2180. ]),
  2181. '');
  2182. end;
  2183. procedure TTestModule.TestEmptyUnitUseStrict;
  2184. begin
  2185. Converter.Options:=Converter.Options+[coUseStrict];
  2186. StartUnit(false);
  2187. Add('interface');
  2188. Add('implementation');
  2189. ConvertUnit;
  2190. CheckSource('TestEmptyUnitUseStrict',
  2191. LinesToStr([
  2192. ''
  2193. ]),
  2194. '');
  2195. end;
  2196. procedure TTestModule.TestDottedUnitNames;
  2197. begin
  2198. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2199. LinesToStr([
  2200. 'var iV: longint;'
  2201. ]),
  2202. '');
  2203. FFilename:='ns1.test1.pp';
  2204. StartProgram(true);
  2205. Add('uses unIt2;');
  2206. Add('var');
  2207. Add(' i: longint;');
  2208. Add('begin');
  2209. Add(' i:=iv;');
  2210. Add(' i:=uNit2.iv;');
  2211. Add(' i:=Ns1.TEst1.i;');
  2212. ConvertProgram;
  2213. CheckSource('TestDottedUnitNames',
  2214. LinesToStr([
  2215. 'this.i = 0;',
  2216. '']),
  2217. LinesToStr([ // this.$init
  2218. '$mod.i = pas["NS1.Unit2"].iV;',
  2219. '$mod.i = pas["NS1.Unit2"].iV;',
  2220. '$mod.i = $mod.i;',
  2221. '']) );
  2222. end;
  2223. procedure TTestModule.TestDottedUnitNameImpl;
  2224. begin
  2225. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2226. LinesToStr([
  2227. 'type',
  2228. ' TObject = class end;',
  2229. ' TTestA = class',
  2230. ' end;'
  2231. ]),
  2232. LinesToStr(['uses TEST.UnitB;'])
  2233. );
  2234. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2235. LinesToStr([
  2236. 'uses TEST.UnitA;',
  2237. 'type TTestB = class(TTestA);'
  2238. ]),
  2239. ''
  2240. );
  2241. StartProgram(true);
  2242. Add('uses TEST.UnitA;');
  2243. Add('begin');
  2244. ConvertProgram;
  2245. CheckSource('TestDottedUnitNameImpl',
  2246. LinesToStr([
  2247. '']),
  2248. LinesToStr([ // this.$init
  2249. '']) );
  2250. CheckUnit('TEST.UnitA.pas',
  2251. LinesToStr([
  2252. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2253. ' var $mod = this;',
  2254. ' rtl.createClass($mod, "TObject", null, function () {',
  2255. ' this.$init = function () {',
  2256. ' };',
  2257. ' this.$final = function () {',
  2258. ' };',
  2259. ' });',
  2260. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2261. ' });',
  2262. '}, ["TEST.UnitB"]);'
  2263. ]));
  2264. CheckUnit('TEST.UnitB.pas',
  2265. LinesToStr([
  2266. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2267. ' var $mod = this;',
  2268. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2269. ' });',
  2270. '});'
  2271. ]));
  2272. end;
  2273. procedure TTestModule.TestDottedUnitExpr;
  2274. begin
  2275. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2276. LinesToStr([
  2277. 'procedure DoIt;'
  2278. ]),
  2279. 'procedure DoIt; begin end;');
  2280. FFilename:='Ns1.SubNs1.Test1.pp';
  2281. StartProgram(true);
  2282. Add('uses Ns2.sUbnS2.unIt2;');
  2283. Add('var');
  2284. Add(' i: longint;');
  2285. Add('begin');
  2286. Add(' ns2.subns2.unit2.doit;');
  2287. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2288. ConvertProgram;
  2289. CheckSource('TestDottedUnitExpr',
  2290. LinesToStr([
  2291. 'this.i = 0;',
  2292. '']),
  2293. LinesToStr([ // this.$init
  2294. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2295. '$mod.i = $mod.i;',
  2296. '']) );
  2297. end;
  2298. procedure TTestModule.Test_ModeFPCFail;
  2299. begin
  2300. StartProgram(false);
  2301. Add('{$mode FPC}');
  2302. Add('begin');
  2303. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2304. ConvertProgram;
  2305. end;
  2306. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2307. begin
  2308. StartProgram(false);
  2309. Add('{$modeswitch cblocks-}');
  2310. Add('begin');
  2311. ConvertProgram;
  2312. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2313. CheckResolverUnexpectedHints();
  2314. end;
  2315. procedure TTestModule.TestUnit_UseSystem;
  2316. begin
  2317. StartUnit(true);
  2318. Add([
  2319. 'interface',
  2320. 'var i: integer;',
  2321. 'implementation']);
  2322. ConvertUnit;
  2323. CheckSource('TestUnit_UseSystem',
  2324. LinesToStr([
  2325. 'this.i = 0;',
  2326. '']),
  2327. LinesToStr([
  2328. '']) );
  2329. end;
  2330. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2331. begin
  2332. AddModuleWithIntfImplSrc('unit1.pp',
  2333. LinesToStr([
  2334. 'type number = longint;']),
  2335. LinesToStr([
  2336. 'uses test1;',
  2337. 'procedure DoIt;',
  2338. 'begin',
  2339. ' i:=3;',
  2340. 'end;']));
  2341. StartUnit(true);
  2342. Add([
  2343. 'interface',
  2344. 'uses unit1;',
  2345. 'var i: number;',
  2346. 'implementation']);
  2347. ConvertUnit;
  2348. CheckSource('TestUnit_Intf1Impl2Intf1',
  2349. LinesToStr([
  2350. 'this.i = 0;',
  2351. '']),
  2352. LinesToStr([
  2353. '']) );
  2354. end;
  2355. procedure TTestModule.TestIncludeVersion;
  2356. begin
  2357. StartProgram(false);
  2358. Add([
  2359. 'var',
  2360. ' s: string;',
  2361. ' i: word;',
  2362. 'begin',
  2363. ' s:={$I %line%};',
  2364. ' i:={$I %linenum%};',
  2365. ' s:={$I %currentroutine%};',
  2366. ' s:={$I %pas2jsversion%};',
  2367. ' s:={$I %pas2jstarget%};',
  2368. ' s:={$I %pas2jstargetos%};',
  2369. ' s:={$I %pas2jstargetcpu%};',
  2370. ' s:={$I %file%};',
  2371. '']);
  2372. ConvertProgram;
  2373. CheckSource('TestIncludeVersion',
  2374. LinesToStr([
  2375. 'this.s="";',
  2376. 'this.i = 0;']),
  2377. LinesToStr([
  2378. '$mod.s = "7";',
  2379. '$mod.i = 8;',
  2380. '$mod.s = "<anonymous>";',
  2381. '$mod.s = "Comp.Ver.tcmodules";',
  2382. '$mod.s = "Browser";',
  2383. '$mod.s = "Browser";',
  2384. '$mod.s = "ECMAScript5";',
  2385. '$mod.s = "test1.pp";',
  2386. '']));
  2387. end;
  2388. procedure TTestModule.TestVarInt;
  2389. begin
  2390. StartProgram(false);
  2391. Add('var MyI: longint;');
  2392. Add('begin');
  2393. ConvertProgram;
  2394. CheckSource('TestVarInt','this.MyI=0;','');
  2395. end;
  2396. procedure TTestModule.TestVarBaseTypes;
  2397. begin
  2398. StartProgram(false);
  2399. Add('var');
  2400. Add(' i: longint;');
  2401. Add(' s: string;');
  2402. Add(' c: char;');
  2403. Add(' b: boolean;');
  2404. Add(' d: double;');
  2405. Add(' i2: longint = 3;');
  2406. Add(' s2: string = ''foo'';');
  2407. Add(' c2: char = ''4'';');
  2408. Add(' b2: boolean = true;');
  2409. Add(' d2: double = 5.6;');
  2410. Add(' i3: longint = $707;');
  2411. Add(' i4: nativeint = 9007199254740991;');
  2412. Add(' i5: nativeint = -9007199254740991-1;');
  2413. Add(' i6: nativeint = $fffffffffffff;');
  2414. Add(' i7: nativeint = -$fffffffffffff-1;');
  2415. Add(' i8: byte = 00;');
  2416. Add(' u8: nativeuint = $fffffffffffff;');
  2417. Add(' u9: nativeuint = $0000000000000;');
  2418. Add(' u10: nativeuint = $00ff00;');
  2419. Add('begin');
  2420. ConvertProgram;
  2421. CheckSource('TestVarBaseTypes',
  2422. LinesToStr([
  2423. 'this.i = 0;',
  2424. 'this.s = "";',
  2425. 'this.c = "";',
  2426. 'this.b = false;',
  2427. 'this.d = 0.0;',
  2428. 'this.i2 = 3;',
  2429. 'this.s2 = "foo";',
  2430. 'this.c2 = "4";',
  2431. 'this.b2 = true;',
  2432. 'this.d2 = 5.6;',
  2433. 'this.i3 = 0x707;',
  2434. 'this.i4 = 9007199254740991;',
  2435. 'this.i5 = -9007199254740991-1;',
  2436. 'this.i6 = 0xfffffffffffff;',
  2437. 'this.i7 =-0xfffffffffffff-1;',
  2438. 'this.i8 = 0;',
  2439. 'this.u8 = 0xfffffffffffff;',
  2440. 'this.u9 = 0x0;',
  2441. 'this.u10 = 0xff00;'
  2442. ]),
  2443. '');
  2444. end;
  2445. procedure TTestModule.TestBaseTypeSingleFail;
  2446. begin
  2447. StartProgram(false);
  2448. Add('var s: single;');
  2449. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2450. ConvertProgram;
  2451. end;
  2452. procedure TTestModule.TestBaseTypeExtendedFail;
  2453. begin
  2454. StartProgram(false);
  2455. Add('var e: extended;');
  2456. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2457. ConvertProgram;
  2458. end;
  2459. procedure TTestModule.TestConstBaseTypes;
  2460. begin
  2461. StartProgram(false);
  2462. Add('const');
  2463. Add(' i: longint = 3;');
  2464. Add(' s: string = ''foo'';');
  2465. Add(' c: char = ''4'';');
  2466. Add(' b: boolean = true;');
  2467. Add(' d: double = 5.6;');
  2468. Add(' e = low(word);');
  2469. Add(' f = high(word);');
  2470. Add('begin');
  2471. ConvertProgram;
  2472. CheckSource('TestVarBaseTypes',
  2473. LinesToStr([
  2474. 'this.i=3;',
  2475. 'this.s="foo";',
  2476. 'this.c="4";',
  2477. 'this.b=true;',
  2478. 'this.d=5.6;',
  2479. 'this.e = 0;',
  2480. 'this.f = 65535;'
  2481. ]),
  2482. '');
  2483. end;
  2484. procedure TTestModule.TestAliasTypeRef;
  2485. begin
  2486. StartProgram(false);
  2487. Add('type');
  2488. Add(' a=longint;');
  2489. Add(' b=a;');
  2490. Add('var');
  2491. Add(' c: A;');
  2492. Add(' d: B;');
  2493. Add('begin');
  2494. ConvertProgram;
  2495. CheckSource('TestAliasTypeRef',
  2496. LinesToStr([ // statements
  2497. 'this.c = 0;',
  2498. 'this.d = 0;'
  2499. ]),
  2500. LinesToStr([ // this.$main
  2501. ''
  2502. ]));
  2503. end;
  2504. procedure TTestModule.TestTypeCast_BaseTypes;
  2505. begin
  2506. StartProgram(false);
  2507. Add([
  2508. 'var',
  2509. ' i: longint;',
  2510. ' b: boolean;',
  2511. ' d: double;',
  2512. ' s: string;',
  2513. ' c: char;',
  2514. 'begin',
  2515. ' i:=longint(i);',
  2516. ' i:=longint(b);',
  2517. ' b:=boolean(b);',
  2518. ' b:=boolean(i);',
  2519. ' d:=double(d);',
  2520. ' d:=double(i);',
  2521. ' s:=string(s);',
  2522. ' s:=string(c);',
  2523. ' c:=char(c);',
  2524. ' c:=char(i);',
  2525. ' c:=char(65);',
  2526. ' c:=char(#10);',
  2527. ' c:=char(#$E000);',
  2528. '']);
  2529. ConvertProgram;
  2530. CheckSource('TestAliasTypeRef',
  2531. LinesToStr([ // statements
  2532. 'this.i = 0;',
  2533. 'this.b = false;',
  2534. 'this.d = 0.0;',
  2535. 'this.s = "";',
  2536. 'this.c = "";',
  2537. '']),
  2538. LinesToStr([ // this.$main
  2539. '$mod.i = $mod.i;',
  2540. '$mod.i = ($mod.b ? 1 : 0);',
  2541. '$mod.b = $mod.b;',
  2542. '$mod.b = $mod.i != 0;',
  2543. '$mod.d = $mod.d;',
  2544. '$mod.d = $mod.i;',
  2545. '$mod.s = $mod.s;',
  2546. '$mod.s = $mod.c;',
  2547. '$mod.c = $mod.c;',
  2548. '$mod.c = String.fromCharCode($mod.i);',
  2549. '$mod.c = "A";',
  2550. '$mod.c = "\n";',
  2551. '$mod.c = "";',
  2552. '']));
  2553. end;
  2554. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2555. begin
  2556. StartProgram(false);
  2557. Add('type');
  2558. Add(' integer = longint;');
  2559. Add(' TYesNo = boolean;');
  2560. Add(' TFloat = double;');
  2561. Add(' TCaption = string;');
  2562. Add(' TChar = char;');
  2563. Add('var');
  2564. Add(' i: integer;');
  2565. Add(' b: TYesNo;');
  2566. Add(' d: TFloat;');
  2567. Add(' s: TCaption;');
  2568. Add(' c: TChar;');
  2569. Add('begin');
  2570. Add(' i:=integer(i);');
  2571. Add(' i:=integer(b);');
  2572. Add(' b:=TYesNo(b);');
  2573. Add(' b:=TYesNo(i);');
  2574. Add(' d:=TFloat(d);');
  2575. Add(' d:=TFloat(i);');
  2576. Add(' s:=TCaption(s);');
  2577. Add(' s:=TCaption(c);');
  2578. Add(' c:=TChar(c);');
  2579. ConvertProgram;
  2580. CheckSource('TestAliasTypeRef',
  2581. LinesToStr([ // statements
  2582. 'this.i = 0;',
  2583. 'this.b = false;',
  2584. 'this.d = 0.0;',
  2585. 'this.s = "";',
  2586. 'this.c = "";',
  2587. '']),
  2588. LinesToStr([ // this.$main
  2589. '$mod.i = $mod.i;',
  2590. '$mod.i = ($mod.b ? 1 : 0);',
  2591. '$mod.b = $mod.b;',
  2592. '$mod.b = $mod.i != 0;',
  2593. '$mod.d = $mod.d;',
  2594. '$mod.d = $mod.i;',
  2595. '$mod.s = $mod.s;',
  2596. '$mod.s = $mod.c;',
  2597. '$mod.c = $mod.c;',
  2598. '']));
  2599. end;
  2600. procedure TTestModule.TestEmptyProc;
  2601. begin
  2602. StartProgram(false);
  2603. Add('procedure Test;');
  2604. Add('begin');
  2605. Add('end;');
  2606. Add('begin');
  2607. ConvertProgram;
  2608. CheckSource('TestEmptyProc',
  2609. LinesToStr([ // statements
  2610. 'this.Test = function () {',
  2611. '};'
  2612. ]),
  2613. LinesToStr([ // this.$main
  2614. ''
  2615. ]));
  2616. end;
  2617. procedure TTestModule.TestProcOneParam;
  2618. begin
  2619. StartProgram(false);
  2620. Add('procedure ProcA(i: longint);');
  2621. Add('begin');
  2622. Add('end;');
  2623. Add('begin');
  2624. Add(' PROCA(3);');
  2625. ConvertProgram;
  2626. CheckSource('TestProcOneParam',
  2627. LinesToStr([ // statements
  2628. 'this.ProcA = function (i) {',
  2629. '};'
  2630. ]),
  2631. LinesToStr([ // this.$main
  2632. '$mod.ProcA(3);'
  2633. ]));
  2634. end;
  2635. procedure TTestModule.TestFunctionWithoutParams;
  2636. begin
  2637. StartProgram(false);
  2638. Add('function FuncA: longint;');
  2639. Add('begin');
  2640. Add('end;');
  2641. Add('var i: longint;');
  2642. Add('begin');
  2643. Add(' I:=FUNCA();');
  2644. Add(' I:=FUNCA;');
  2645. Add(' FUNCA();');
  2646. Add(' FUNCA;');
  2647. ConvertProgram;
  2648. CheckSource('TestProcWithoutParams',
  2649. LinesToStr([ // statements
  2650. 'this.FuncA = function () {',
  2651. ' var Result = 0;',
  2652. ' return Result;',
  2653. '};',
  2654. 'this.i=0;'
  2655. ]),
  2656. LinesToStr([ // this.$main
  2657. '$mod.i=$mod.FuncA();',
  2658. '$mod.i=$mod.FuncA();',
  2659. '$mod.FuncA();',
  2660. '$mod.FuncA();'
  2661. ]));
  2662. end;
  2663. procedure TTestModule.TestProcedureWithoutParams;
  2664. begin
  2665. StartProgram(false);
  2666. Add('procedure ProcA;');
  2667. Add('begin');
  2668. Add('end;');
  2669. Add('begin');
  2670. Add(' PROCA();');
  2671. Add(' PROCA;');
  2672. ConvertProgram;
  2673. CheckSource('TestProcWithoutParams',
  2674. LinesToStr([ // statements
  2675. 'this.ProcA = function () {',
  2676. '};'
  2677. ]),
  2678. LinesToStr([ // this.$main
  2679. '$mod.ProcA();',
  2680. '$mod.ProcA();'
  2681. ]));
  2682. end;
  2683. procedure TTestModule.TestIncDec;
  2684. begin
  2685. StartProgram(false);
  2686. Add([
  2687. 'procedure DoIt(var i: longint);',
  2688. 'begin',
  2689. ' inc(i);',
  2690. ' inc(i,2);',
  2691. 'end;',
  2692. 'var',
  2693. ' Bar: longint;',
  2694. 'begin',
  2695. ' inc(bar);',
  2696. ' inc(bar,2);',
  2697. ' dec(bar);',
  2698. ' dec(bar,3);',
  2699. '']);
  2700. ConvertProgram;
  2701. CheckSource('TestIncDec',
  2702. LinesToStr([ // statements
  2703. 'this.DoIt = function (i) {',
  2704. ' i.set(i.get()+1);',
  2705. ' i.set(i.get()+2);',
  2706. '};',
  2707. 'this.Bar = 0;'
  2708. ]),
  2709. LinesToStr([ // this.$main
  2710. '$mod.Bar+=1;',
  2711. '$mod.Bar+=2;',
  2712. '$mod.Bar-=1;',
  2713. '$mod.Bar-=3;'
  2714. ]));
  2715. end;
  2716. procedure TTestModule.TestLoHiFpcMode;
  2717. begin
  2718. StartProgram(false);
  2719. Add([
  2720. '{$mode objfpc}',
  2721. 'const',
  2722. ' LoByte1 = Lo(Word($1234));',
  2723. ' HiByte1 = Hi(Word($1234));',
  2724. ' LoByte2 = Lo(SmallInt($1234));',
  2725. ' HiByte2 = Hi(SmallInt($1234));',
  2726. ' LoWord1 = Lo($1234CDEF);',
  2727. ' HiWord1 = Hi($1234CDEF);',
  2728. ' LoWord2 = Lo(-$1234CDEF);',
  2729. ' HiWord2 = Hi(-$1234CDEF);',
  2730. ' lo4:byte=lo(byte($34));',
  2731. ' hi4:byte=hi(byte($34));',
  2732. ' lo5:byte=lo(shortint(-$34));',
  2733. ' hi5:byte=hi(shortint(-$34));',
  2734. ' lo6:longword=lo($123456789ABCD);',
  2735. ' hi6:longword=hi($123456789ABCD);',
  2736. ' lo7:longword=lo(-$123456789ABCD);',
  2737. ' hi7:longword=hi(-$123456789ABCD);',
  2738. 'var',
  2739. ' b: Byte;',
  2740. ' ss: shortint;',
  2741. ' w: Word;',
  2742. ' si: SmallInt;',
  2743. ' lw: LongWord;',
  2744. ' li: LongInt;',
  2745. ' b2: Byte;',
  2746. ' ni: nativeint;',
  2747. 'begin',
  2748. ' w := $1234;',
  2749. ' ss := -$12;',
  2750. ' b := lo(ss);',
  2751. ' b := HI(ss);',
  2752. ' b := lo(w);',
  2753. ' b := HI(w);',
  2754. ' b2 := lo(b);',
  2755. ' b2 := hi(b);',
  2756. ' lw := $1234CDEF;',
  2757. ' w := lo(lw);',
  2758. ' w := hi(lw);',
  2759. ' ni := $123456789ABCD;',
  2760. ' lw := lo(ni);',
  2761. ' lw := hi(ni);',
  2762. '']);
  2763. ConvertProgram;
  2764. CheckSource('TestLoHiFpcMode',
  2765. LinesToStr([ // statements
  2766. 'this.LoByte1 = 0x1234 & 0xFF;',
  2767. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2768. 'this.LoByte2 = 0x1234 & 0xFF;',
  2769. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2770. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2771. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2772. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2773. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2774. 'this.lo4 = 0x34 & 0xF;',
  2775. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2776. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2777. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2778. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2779. 'this.hi6 = 74565 >>> 0;',
  2780. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2781. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2782. 'this.b = 0;',
  2783. 'this.ss = 0;',
  2784. 'this.w = 0;',
  2785. 'this.si = 0;',
  2786. 'this.lw = 0;',
  2787. 'this.li = 0;',
  2788. 'this.b2 = 0;',
  2789. 'this.ni = 0;',
  2790. '']),
  2791. LinesToStr([ // this.$main
  2792. '$mod.w = 0x1234;',
  2793. '$mod.ss = -0x12;',
  2794. '$mod.b = $mod.ss & 0xFF;',
  2795. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2796. '$mod.b = $mod.w & 0xFF;',
  2797. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2798. '$mod.b2 = $mod.b & 0xF;',
  2799. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2800. '$mod.lw = 0x1234CDEF;',
  2801. '$mod.w = $mod.lw & 0xFFFF;',
  2802. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2803. '$mod.ni = 0x123456789ABCD;',
  2804. '$mod.lw = $mod.ni >>> 0;',
  2805. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2806. '']));
  2807. end;
  2808. procedure TTestModule.TestLoHiDelphiMode;
  2809. begin
  2810. StartProgram(false);
  2811. Add([
  2812. '{$mode delphi}',
  2813. 'const',
  2814. ' LoByte1 = Lo(Word($1234));',
  2815. ' HiByte1 = Hi(Word($1234));',
  2816. ' LoByte2 = Lo(SmallInt($1234));',
  2817. ' HiByte2 = Hi(SmallInt($1234));',
  2818. ' LoByte3 = Lo($1234CDEF);',
  2819. ' HiByte3 = Hi($1234CDEF);',
  2820. ' LoByte4 = Lo(-$1234CDEF);',
  2821. ' HiByte4 = Hi(-$1234CDEF);',
  2822. 'var',
  2823. ' b: Byte;',
  2824. ' w: Word;',
  2825. ' si: SmallInt;',
  2826. ' lw: LongWord;',
  2827. ' li: LongInt;',
  2828. 'begin',
  2829. ' w := $1234;',
  2830. ' b := lo(w);',
  2831. ' b := HI(w);',
  2832. ' lw := $1234CDEF;',
  2833. ' b := lo(lw);',
  2834. ' b := hi(lw);',
  2835. '']);
  2836. ConvertProgram;
  2837. CheckSource('TestLoHiDelphiMode',
  2838. LinesToStr([ // statements
  2839. 'this.LoByte1 = 0x1234 & 0xFF;',
  2840. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2841. 'this.LoByte2 = 0x1234 & 0xFF;',
  2842. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2843. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2844. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2845. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2846. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2847. 'this.b = 0;',
  2848. 'this.w = 0;',
  2849. 'this.si = 0;',
  2850. 'this.lw = 0;',
  2851. 'this.li = 0;'
  2852. ]),
  2853. LinesToStr([ // this.$main
  2854. '$mod.w = 0x1234;',
  2855. '$mod.b = $mod.w & 0xFF;',
  2856. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2857. '$mod.lw = 0x1234CDEF;',
  2858. '$mod.b = $mod.lw & 0xFF;',
  2859. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2860. ]));
  2861. end;
  2862. procedure TTestModule.TestAssignments;
  2863. begin
  2864. StartProgram(false);
  2865. Parser.Options:=Parser.Options+[po_cassignments];
  2866. Add('var');
  2867. Add(' Bar:longint;');
  2868. Add('begin');
  2869. Add(' bar:=3;');
  2870. Add(' bar+=4;');
  2871. Add(' bar-=5;');
  2872. Add(' bar*=6;');
  2873. ConvertProgram;
  2874. CheckSource('TestAssignments',
  2875. LinesToStr([ // statements
  2876. 'this.Bar = 0;'
  2877. ]),
  2878. LinesToStr([ // this.$main
  2879. '$mod.Bar=3;',
  2880. '$mod.Bar+=4;',
  2881. '$mod.Bar-=5;',
  2882. '$mod.Bar*=6;'
  2883. ]));
  2884. end;
  2885. procedure TTestModule.TestArithmeticOperators1;
  2886. begin
  2887. StartProgram(false);
  2888. Add('var');
  2889. Add(' vA,vB,vC:longint;');
  2890. Add('begin');
  2891. Add(' va:=1;');
  2892. Add(' vb:=va+va;');
  2893. Add(' vb:=va div vb;');
  2894. Add(' vb:=va mod vb;');
  2895. Add(' vb:=va+va*vb+va div vb;');
  2896. Add(' vc:=-va;');
  2897. Add(' va:=va-vb;');
  2898. Add(' vb:=va;');
  2899. Add(' if va<vb then vc:=va else vc:=vb;');
  2900. ConvertProgram;
  2901. CheckSource('TestArithmeticOperators1',
  2902. LinesToStr([ // statements
  2903. 'this.vA = 0;',
  2904. 'this.vB = 0;',
  2905. 'this.vC = 0;'
  2906. ]),
  2907. LinesToStr([ // this.$main
  2908. '$mod.vA = 1;',
  2909. '$mod.vB = $mod.vA + $mod.vA;',
  2910. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  2911. '$mod.vB = $mod.vA % $mod.vB;',
  2912. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  2913. '$mod.vC = -$mod.vA;',
  2914. '$mod.vA = $mod.vA - $mod.vB;',
  2915. '$mod.vB = $mod.vA;',
  2916. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2917. ]));
  2918. end;
  2919. procedure TTestModule.TestLogicalOperators;
  2920. begin
  2921. StartProgram(false);
  2922. Add('var');
  2923. Add(' vA,vB,vC:boolean;');
  2924. Add('begin');
  2925. Add(' va:=vb and vc;');
  2926. Add(' va:=vb or vc;');
  2927. Add(' va:=vb xor vc;');
  2928. Add(' va:=true and vc;');
  2929. Add(' va:=(vb and vc) or (va and vb);');
  2930. Add(' va:=not vb;');
  2931. ConvertProgram;
  2932. CheckSource('TestLogicalOperators',
  2933. LinesToStr([ // statements
  2934. 'this.vA = false;',
  2935. 'this.vB = false;',
  2936. 'this.vC = false;'
  2937. ]),
  2938. LinesToStr([ // this.$main
  2939. '$mod.vA = $mod.vB && $mod.vC;',
  2940. '$mod.vA = $mod.vB || $mod.vC;',
  2941. '$mod.vA = $mod.vB ^ $mod.vC;',
  2942. '$mod.vA = true && $mod.vC;',
  2943. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  2944. '$mod.vA = !$mod.vB;'
  2945. ]));
  2946. end;
  2947. procedure TTestModule.TestBitwiseOperators;
  2948. begin
  2949. StartProgram(false);
  2950. Add([
  2951. 'var',
  2952. ' vA,vB,vC:longint;',
  2953. ' X,Y,Z: nativeint;',
  2954. 'begin',
  2955. ' va:=vb and vc;',
  2956. ' va:=vb or vc;',
  2957. ' va:=vb xor vc;',
  2958. ' va:=vb shl vc;',
  2959. ' va:=vb shr vc;',
  2960. ' va:=3 and vc;',
  2961. ' va:=(vb and vc) or (va and vb);',
  2962. ' va:=not vb;',
  2963. ' X:=Y and Z;',
  2964. ' X:=Y and va;',
  2965. ' X:=Y or Z;',
  2966. ' X:=Y or va;',
  2967. ' X:=Y xor Z;',
  2968. ' X:=Y xor va;',
  2969. '']);
  2970. ConvertProgram;
  2971. CheckSource('TestBitwiseOperators',
  2972. LinesToStr([ // statements
  2973. 'this.vA = 0;',
  2974. 'this.vB = 0;',
  2975. 'this.vC = 0;',
  2976. 'this.X = 0;',
  2977. 'this.Y = 0;',
  2978. 'this.Z = 0;',
  2979. '']),
  2980. LinesToStr([ // this.$main
  2981. '$mod.vA = $mod.vB & $mod.vC;',
  2982. '$mod.vA = $mod.vB | $mod.vC;',
  2983. '$mod.vA = $mod.vB ^ $mod.vC;',
  2984. '$mod.vA = $mod.vB << $mod.vC;',
  2985. '$mod.vA = $mod.vB >>> $mod.vC;',
  2986. '$mod.vA = 3 & $mod.vC;',
  2987. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  2988. '$mod.vA = ~$mod.vB;',
  2989. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  2990. '$mod.X = $mod.Y & $mod.vA;',
  2991. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  2992. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  2993. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  2994. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  2995. '']));
  2996. end;
  2997. procedure TTestModule.TestBitwiseOperatorsLongword;
  2998. begin
  2999. StartProgram(false);
  3000. Add([
  3001. 'var',
  3002. ' a,b,c:longword;',
  3003. ' i: longint;',
  3004. 'begin',
  3005. ' a:=$12345678;',
  3006. ' b:=$EDCBA987;',
  3007. ' c:=not a;',
  3008. ' c:=a and b;',
  3009. ' c:=a and $ffff0000;',
  3010. ' c:=a or b;',
  3011. ' c:=a or $ff00ff00;',
  3012. ' c:=a xor b;',
  3013. ' c:=a xor $f0f0f0f0;',
  3014. ' c:=a shl 1;',
  3015. ' c:=a shl 16;',
  3016. ' c:=a shl 24;',
  3017. ' c:=a shl b;',
  3018. ' c:=a shr 1;',
  3019. ' c:=a shr 16;',
  3020. ' c:=a shr 24;',
  3021. ' c:=a shr b;',
  3022. ' c:=(b and c) or (a and b);',
  3023. ' c:=i and a;',
  3024. ' c:=i or a;',
  3025. ' c:=i xor a;',
  3026. '']);
  3027. ConvertProgram;
  3028. CheckSource('TestBitwiseOperatorsLongword',
  3029. LinesToStr([ // statements
  3030. 'this.a = 0;',
  3031. 'this.b = 0;',
  3032. 'this.c = 0;',
  3033. 'this.i = 0;',
  3034. '']),
  3035. LinesToStr([ // this.$main
  3036. '$mod.a = 0x12345678;',
  3037. '$mod.b = 0xEDCBA987;',
  3038. '$mod.c = rtl.lw(~$mod.a);',
  3039. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3040. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3041. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3042. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3043. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3044. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3045. '$mod.c = rtl.lw($mod.a << 1);',
  3046. '$mod.c = rtl.lw($mod.a << 16);',
  3047. '$mod.c = rtl.lw($mod.a << 24);',
  3048. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3049. '$mod.c = rtl.lw($mod.a >>> 1);',
  3050. '$mod.c = rtl.lw($mod.a >>> 16);',
  3051. '$mod.c = rtl.lw($mod.a >>> 24);',
  3052. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3053. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3054. '$mod.c = $mod.i & $mod.a;',
  3055. '$mod.c = $mod.i | $mod.a;',
  3056. '$mod.c = $mod.i ^ $mod.a;',
  3057. '']));
  3058. end;
  3059. procedure TTestModule.TestPrgProcVar;
  3060. begin
  3061. StartProgram(false);
  3062. Add('procedure Proc1;');
  3063. Add('type');
  3064. Add(' t1=longint;');
  3065. Add('var');
  3066. Add(' vA:t1;');
  3067. Add('begin');
  3068. Add('end;');
  3069. Add('begin');
  3070. ConvertProgram;
  3071. CheckSource('TestPrgProcVar',
  3072. LinesToStr([ // statements
  3073. 'this.Proc1 = function () {',
  3074. ' var vA=0;',
  3075. '};'
  3076. ]),
  3077. LinesToStr([ // this.$main
  3078. ''
  3079. ]));
  3080. end;
  3081. procedure TTestModule.TestUnitProcVar;
  3082. begin
  3083. StartUnit(false);
  3084. Add('interface');
  3085. Add('');
  3086. Add('type tA=string; // unit scope');
  3087. Add('procedure Proc1;');
  3088. Add('');
  3089. Add('implementation');
  3090. Add('');
  3091. Add('procedure Proc1;');
  3092. Add('type tA=longint; // local proc scope');
  3093. Add('var v1:tA; // using local tA');
  3094. Add('begin');
  3095. Add('end;');
  3096. Add('var v2:tA; // using interface tA');
  3097. ConvertUnit;
  3098. CheckSource('TestUnitProcVar',
  3099. LinesToStr([ // statements
  3100. 'var $impl = $mod.$impl;',
  3101. 'this.Proc1 = function () {',
  3102. ' var v1 = 0;',
  3103. '};',
  3104. '']),
  3105. // this.$init
  3106. '',
  3107. // implementation
  3108. LinesToStr([
  3109. '$impl.v2 = "";',
  3110. '']));
  3111. end;
  3112. procedure TTestModule.TestImplProc;
  3113. begin
  3114. StartUnit(false);
  3115. Add('interface');
  3116. Add('');
  3117. Add('procedure Proc1;');
  3118. Add('');
  3119. Add('implementation');
  3120. Add('');
  3121. Add('procedure Proc1; begin end;');
  3122. Add('procedure Proc2; begin end;');
  3123. Add('initialization');
  3124. Add(' Proc1;');
  3125. Add(' Proc2;');
  3126. ConvertUnit;
  3127. CheckSource('TestImplProc',
  3128. LinesToStr([ // statements
  3129. 'var $impl = $mod.$impl;',
  3130. 'this.Proc1 = function () {',
  3131. '};',
  3132. '']),
  3133. LinesToStr([ // this.$init
  3134. '$mod.Proc1();',
  3135. '$impl.Proc2();',
  3136. '']),
  3137. LinesToStr([ // implementation
  3138. '$impl.Proc2 = function () {',
  3139. '};',
  3140. ''])
  3141. );
  3142. end;
  3143. procedure TTestModule.TestFunctionResult;
  3144. begin
  3145. StartProgram(false);
  3146. Add('function Func1: longint;');
  3147. Add('begin');
  3148. Add(' Result:=3;');
  3149. Add(' Func1:=4;');
  3150. Add('end;');
  3151. Add('begin');
  3152. ConvertProgram;
  3153. CheckSource('TestFunctionResult',
  3154. LinesToStr([ // statements
  3155. 'this.Func1 = function () {',
  3156. ' var Result = 0;',
  3157. ' Result = 3;',
  3158. ' Result = 4;',
  3159. ' return Result;',
  3160. '};'
  3161. ]),
  3162. '');
  3163. end;
  3164. procedure TTestModule.TestNestedProc;
  3165. begin
  3166. StartProgram(false);
  3167. Add([
  3168. 'var vInUnit: longint;',
  3169. 'function DoIt(pA,pD: longint): longint;',
  3170. 'var',
  3171. ' vB: longint;',
  3172. ' vC: longint;',
  3173. ' function Nesty(pA: longint): longint; ',
  3174. ' var vB: longint;',
  3175. ' begin',
  3176. ' Result:=pa+vb+vc+pd+vInUnit;',
  3177. ' nesty:=3;',
  3178. ' doit:=4;',
  3179. ' exit;',
  3180. ' end;',
  3181. 'begin',
  3182. ' Result:=pa+vb+vc;',
  3183. ' doit:=6;',
  3184. ' exit;',
  3185. 'end;',
  3186. 'begin']);
  3187. ConvertProgram;
  3188. CheckSource('TestNestedProc',
  3189. LinesToStr([ // statements
  3190. 'this.vInUnit = 0;',
  3191. 'this.DoIt = function (pA, pD) {',
  3192. ' var Result = 0;',
  3193. ' var vB = 0;',
  3194. ' var vC = 0;',
  3195. ' function Nesty(pA) {',
  3196. ' var Result$1 = 0;',
  3197. ' var vB = 0;',
  3198. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3199. ' Result$1 = 3;',
  3200. ' Result = 4;',
  3201. ' return Result$1;',
  3202. ' return Result$1;',
  3203. ' };',
  3204. ' Result = pA + vB + vC;',
  3205. ' Result = 6;',
  3206. ' return Result;',
  3207. ' return Result;',
  3208. '};'
  3209. ]),
  3210. '');
  3211. end;
  3212. procedure TTestModule.TestNestedProc_ResultString;
  3213. begin
  3214. StartProgram(false);
  3215. Add([
  3216. 'function DoIt: string;',
  3217. ' function Nesty: string; ',
  3218. ' begin',
  3219. ' nesty:=#65#66;',
  3220. ' nesty[1]:=#67;',
  3221. ' doit:=#68;',
  3222. ' doit[2]:=#69;',
  3223. ' end;',
  3224. 'begin',
  3225. ' doit:=#70;',
  3226. ' doit[3]:=#71;',
  3227. 'end;',
  3228. 'begin']);
  3229. ConvertProgram;
  3230. CheckSource('TestNestedProc_ResultString',
  3231. LinesToStr([ // statements
  3232. 'this.DoIt = function () {',
  3233. ' var Result = "";',
  3234. ' function Nesty() {',
  3235. ' var Result$1 = "";',
  3236. ' Result$1 = "AB";',
  3237. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3238. ' Result = "D";',
  3239. ' Result = rtl.setCharAt(Result, 1, "E");',
  3240. ' return Result$1;',
  3241. ' };',
  3242. ' Result = "F";',
  3243. ' Result = rtl.setCharAt(Result, 2, "G");',
  3244. ' return Result;',
  3245. '};'
  3246. ]),
  3247. '');
  3248. end;
  3249. procedure TTestModule.TestForwardProc;
  3250. begin
  3251. StartProgram(false);
  3252. Add('procedure FuncA(Bar: longint); forward;');
  3253. Add('procedure FuncB(Bar: longint);');
  3254. Add('begin');
  3255. Add(' funca(bar);');
  3256. Add('end;');
  3257. Add('procedure funca(bar: longint);');
  3258. Add('begin');
  3259. Add(' if bar=3 then ;');
  3260. Add('end;');
  3261. Add('begin');
  3262. Add(' funca(4);');
  3263. Add(' funcb(5);');
  3264. ConvertProgram;
  3265. CheckSource('TestForwardProc',
  3266. LinesToStr([ // statements'
  3267. 'this.FuncB = function (Bar) {',
  3268. ' $mod.FuncA(Bar);',
  3269. '};',
  3270. 'this.FuncA = function (Bar) {',
  3271. ' if (Bar === 3);',
  3272. '};'
  3273. ]),
  3274. LinesToStr([
  3275. '$mod.FuncA(4);',
  3276. '$mod.FuncB(5);'
  3277. ])
  3278. );
  3279. end;
  3280. procedure TTestModule.TestNestedForwardProc;
  3281. begin
  3282. StartProgram(false);
  3283. Add('procedure FuncA;');
  3284. Add(' procedure FuncB(i: longint); forward;');
  3285. Add(' procedure FuncC(i: longint);');
  3286. Add(' begin');
  3287. Add(' funcb(i);');
  3288. Add(' end;');
  3289. Add(' procedure FuncB(i: longint);');
  3290. Add(' begin');
  3291. Add(' if i=3 then ;');
  3292. Add(' end;');
  3293. Add('begin');
  3294. Add(' funcc(4)');
  3295. Add('end;');
  3296. Add('begin');
  3297. Add(' funca;');
  3298. ConvertProgram;
  3299. CheckSource('TestNestedForwardProc',
  3300. LinesToStr([ // statements'
  3301. 'this.FuncA = function () {',
  3302. ' function FuncC(i) {',
  3303. ' FuncB(i);',
  3304. ' };',
  3305. ' function FuncB(i) {',
  3306. ' if (i === 3);',
  3307. ' };',
  3308. ' FuncC(4);',
  3309. '};'
  3310. ]),
  3311. LinesToStr([
  3312. '$mod.FuncA();'
  3313. ])
  3314. );
  3315. end;
  3316. procedure TTestModule.TestAssignFunctionResult;
  3317. begin
  3318. StartProgram(false);
  3319. Add('function Func1: longint;');
  3320. Add('begin');
  3321. Add('end;');
  3322. Add('var i: longint;');
  3323. Add('begin');
  3324. Add(' i:=func1();');
  3325. Add(' i:=func1()+func1();');
  3326. ConvertProgram;
  3327. CheckSource('TestAssignFunctionResult',
  3328. LinesToStr([ // statements
  3329. 'this.Func1 = function () {',
  3330. ' var Result = 0;',
  3331. ' return Result;',
  3332. '};',
  3333. 'this.i = 0;'
  3334. ]),
  3335. LinesToStr([
  3336. '$mod.i = $mod.Func1();',
  3337. '$mod.i = $mod.Func1() + $mod.Func1();'
  3338. ]));
  3339. end;
  3340. procedure TTestModule.TestFunctionResultInCondition;
  3341. begin
  3342. StartProgram(false);
  3343. Add('function Func1: longint;');
  3344. Add('begin');
  3345. Add('end;');
  3346. Add('function Func2: boolean;');
  3347. Add('begin');
  3348. Add('end;');
  3349. Add('var i: longint;');
  3350. Add('begin');
  3351. Add(' if func2 then ;');
  3352. Add(' if i=func1() then ;');
  3353. Add(' if i=func1 then ;');
  3354. ConvertProgram;
  3355. CheckSource('TestFunctionResultInCondition',
  3356. LinesToStr([ // statements
  3357. 'this.Func1 = function () {',
  3358. ' var Result = 0;',
  3359. ' return Result;',
  3360. '};',
  3361. 'this.Func2 = function () {',
  3362. ' var Result = false;',
  3363. ' return Result;',
  3364. '};',
  3365. 'this.i = 0;'
  3366. ]),
  3367. LinesToStr([
  3368. 'if ($mod.Func2());',
  3369. 'if ($mod.i === $mod.Func1());',
  3370. 'if ($mod.i === $mod.Func1());'
  3371. ]));
  3372. end;
  3373. procedure TTestModule.TestFunctionResultInForLoop;
  3374. begin
  3375. StartProgram(false);
  3376. Add([
  3377. 'function Func1(a: array of longint): longint;',
  3378. 'begin',
  3379. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3380. ' for Result in a do if a[Result]=0 then exit;',
  3381. 'end;',
  3382. 'begin',
  3383. ' Func1([1,2,3])']);
  3384. ConvertProgram;
  3385. CheckSource('TestFunctionResultInForLoop',
  3386. LinesToStr([ // statements
  3387. 'this.Func1 = function (a) {',
  3388. ' var Result = 0;',
  3389. ' for (var $l1 = rtl.length(a) - 1; $l1 >= 0; $l1--) {',
  3390. ' Result = $l1;',
  3391. ' if (a[Result] === 0) return Result;',
  3392. ' };',
  3393. ' for (var $in2 = a, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) {',
  3394. ' Result = $in2[$l3];',
  3395. ' if (a[Result] === 0) return Result;',
  3396. ' };',
  3397. ' return Result;',
  3398. '};',
  3399. '']),
  3400. LinesToStr([
  3401. '$mod.Func1([1, 2, 3]);'
  3402. ]));
  3403. end;
  3404. procedure TTestModule.TestFunctionResultInTypeCast;
  3405. begin
  3406. StartProgram(false);
  3407. Add([
  3408. 'function GetInt: longint;',
  3409. 'begin',
  3410. 'end;',
  3411. 'begin',
  3412. ' if Byte(GetInt)=0 then ;',
  3413. '']);
  3414. ConvertProgram;
  3415. CheckSource('TestFunctionResultInTypeCast',
  3416. LinesToStr([ // statements
  3417. 'this.GetInt = function () {',
  3418. ' var Result = 0;',
  3419. ' return Result;',
  3420. '};',
  3421. '']),
  3422. LinesToStr([
  3423. 'if (($mod.GetInt() & 255) === 0) ;'
  3424. ]));
  3425. end;
  3426. procedure TTestModule.TestExit;
  3427. begin
  3428. StartProgram(false);
  3429. Add('procedure ProcA;');
  3430. Add('begin');
  3431. Add(' exit;');
  3432. Add('end;');
  3433. Add('function FuncB: longint;');
  3434. Add('begin');
  3435. Add(' exit;');
  3436. Add(' exit(3);');
  3437. Add('end;');
  3438. Add('function FuncC: string;');
  3439. Add('begin');
  3440. Add(' exit;');
  3441. Add(' exit(''a'');');
  3442. Add(' exit(''abc'');');
  3443. Add('end;');
  3444. Add('begin');
  3445. Add(' exit;');
  3446. Add(' exit(1);');
  3447. ConvertProgram;
  3448. CheckSource('TestExit',
  3449. LinesToStr([ // statements
  3450. 'this.ProcA = function () {',
  3451. ' return;',
  3452. '};',
  3453. 'this.FuncB = function () {',
  3454. ' var Result = 0;',
  3455. ' return Result;',
  3456. ' return 3;',
  3457. ' return Result;',
  3458. '};',
  3459. 'this.FuncC = function () {',
  3460. ' var Result = "";',
  3461. ' return Result;',
  3462. ' return "a";',
  3463. ' return "abc";',
  3464. ' return Result;',
  3465. '};'
  3466. ]),
  3467. LinesToStr([
  3468. 'return;',
  3469. 'return 1;',
  3470. '']));
  3471. end;
  3472. procedure TTestModule.TestBreak;
  3473. begin
  3474. StartProgram(false);
  3475. Add([
  3476. 'var',
  3477. ' i: longint;',
  3478. 'begin',
  3479. ' repeat',
  3480. ' break;',
  3481. ' until true;',
  3482. ' while true do',
  3483. ' break;',
  3484. ' for i:=1 to 2 do',
  3485. ' break;']);
  3486. ConvertProgram;
  3487. CheckSource('TestBreak',
  3488. LinesToStr([ // statements
  3489. 'this.i = 0;'
  3490. ]),
  3491. LinesToStr([
  3492. 'do {',
  3493. ' break;',
  3494. '} while (!true);',
  3495. 'while (true) break;',
  3496. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3497. '']));
  3498. end;
  3499. procedure TTestModule.TestBreakAsVar;
  3500. begin
  3501. StartProgram(false);
  3502. Add([
  3503. 'procedure DoIt(break: boolean);',
  3504. 'begin',
  3505. ' if break then ;',
  3506. 'end;',
  3507. 'var',
  3508. ' break: boolean;',
  3509. 'begin',
  3510. ' if break then ;']);
  3511. ConvertProgram;
  3512. CheckSource('TestBreakAsVar',
  3513. LinesToStr([ // statements
  3514. 'this.DoIt = function (Break) {',
  3515. ' if (Break) ;',
  3516. '};',
  3517. 'this.Break = false;',
  3518. '']),
  3519. LinesToStr([
  3520. 'if($mod.Break) ;',
  3521. '']));
  3522. end;
  3523. procedure TTestModule.TestContinue;
  3524. begin
  3525. StartProgram(false);
  3526. Add('var i: longint;');
  3527. Add('begin');
  3528. Add(' repeat');
  3529. Add(' continue;');
  3530. Add(' until true;');
  3531. Add(' while true do');
  3532. Add(' continue;');
  3533. Add(' for i:=1 to 2 do');
  3534. Add(' continue;');
  3535. ConvertProgram;
  3536. CheckSource('TestContinue',
  3537. LinesToStr([ // statements
  3538. 'this.i = 0;'
  3539. ]),
  3540. LinesToStr([
  3541. 'do {',
  3542. ' continue;',
  3543. '} while (!true);',
  3544. 'while (true) continue;',
  3545. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3546. '']));
  3547. end;
  3548. procedure TTestModule.TestProc_External;
  3549. begin
  3550. StartProgram(false);
  3551. Add('procedure Foo; external name ''console.log'';');
  3552. Add('function Bar: longint; external name ''get.item'';');
  3553. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3554. Add('var');
  3555. Add(' i: longint;');
  3556. Add('begin');
  3557. Add(' Foo;');
  3558. Add(' i:=Bar;');
  3559. Add(' i:=Bla(''abc'');');
  3560. ConvertProgram;
  3561. CheckSource('TestProc_External',
  3562. LinesToStr([ // statements
  3563. 'this.i = 0;'
  3564. ]),
  3565. LinesToStr([
  3566. 'console.log();',
  3567. '$mod.i = get.item();',
  3568. '$mod.i = apply.something("abc");'
  3569. ]));
  3570. end;
  3571. procedure TTestModule.TestProc_ExternalOtherUnit;
  3572. begin
  3573. AddModuleWithIntfImplSrc('unit2.pas',
  3574. LinesToStr([
  3575. 'procedure Now; external name ''Date.now'';',
  3576. 'procedure DoIt;'
  3577. ]),
  3578. 'procedure doit; begin end;');
  3579. StartUnit(true);
  3580. Add('interface');
  3581. Add('uses unit2;');
  3582. Add('implementation');
  3583. Add('begin');
  3584. Add(' now;');
  3585. Add(' now();');
  3586. Add(' uNit2.now;');
  3587. Add(' uNit2.now();');
  3588. Add(' doit;');
  3589. Add(' uNit2.doit;');
  3590. ConvertUnit;
  3591. CheckSource('TestProc_ExternalOtherUnit',
  3592. LinesToStr([
  3593. '']),
  3594. LinesToStr([
  3595. 'Date.now();',
  3596. 'Date.now();',
  3597. 'Date.now();',
  3598. 'Date.now();',
  3599. 'pas.unit2.DoIt();',
  3600. 'pas.unit2.DoIt();',
  3601. '']));
  3602. end;
  3603. procedure TTestModule.TestProc_Asm;
  3604. begin
  3605. StartProgram(false);
  3606. Add([
  3607. '{$mode delphi}',
  3608. 'function DoIt: longint;',
  3609. 'begin;',
  3610. ' asm',
  3611. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3612. ' end;',
  3613. ' asm console.log(); end;',
  3614. ' asm',
  3615. ' s = "'' ";',
  3616. ' s = ''" '';',
  3617. ' s = s + "world" + "''";',
  3618. ' // end',
  3619. ' s = ''end'';',
  3620. ' s = "end";',
  3621. ' s = "foo\"bar";',
  3622. ' s = ''a\''b'';',
  3623. ' s = `${expr}\`-"-''-`;',
  3624. ' s = `multi',
  3625. 'line`;',
  3626. ' end;',
  3627. 'end;',
  3628. 'procedure Fly;',
  3629. 'asm',
  3630. ' return;',
  3631. 'end;',
  3632. 'begin']);
  3633. ConvertProgram;
  3634. CheckSource('TestProc_Asm',
  3635. LinesToStr([ // statements
  3636. 'this.DoIt = function () {',
  3637. ' var Result = 0;',
  3638. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3639. ' console.log();',
  3640. ' s = "'' ";',
  3641. ' s = ''" '';',
  3642. ' s = s + "world" + "''";',
  3643. ' // end',
  3644. ' s = ''end'';',
  3645. ' s = "end";',
  3646. ' s = "foo\"bar";',
  3647. ' s = ''a\''b'';',
  3648. ' s = `${expr}\`-"-''-`;',
  3649. ' s = `multi',
  3650. 'line`;',
  3651. ' return Result;',
  3652. '};',
  3653. 'this.Fly = function () {',
  3654. ' return;',
  3655. '};',
  3656. '']),
  3657. LinesToStr([
  3658. ''
  3659. ]));
  3660. end;
  3661. procedure TTestModule.TestProc_Assembler;
  3662. begin
  3663. StartProgram(false);
  3664. Add('function DoIt: longint; assembler;');
  3665. Add('asm');
  3666. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3667. Add('end;');
  3668. Add('begin');
  3669. ConvertProgram;
  3670. CheckSource('TestProc_Assembler',
  3671. LinesToStr([ // statements
  3672. 'this.DoIt = function () {',
  3673. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3674. '};'
  3675. ]),
  3676. LinesToStr([
  3677. ''
  3678. ]));
  3679. end;
  3680. procedure TTestModule.TestProc_VarParam;
  3681. begin
  3682. StartProgram(false);
  3683. Add('type integer = longint;');
  3684. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3685. Add('var vJ: integer;');
  3686. Add('begin');
  3687. Add(' vg:=vg+1;');
  3688. Add(' vj:=vh+2;');
  3689. Add(' vi:=vi+3;');
  3690. Add(' doit(vg,vg,vg);');
  3691. Add(' doit(vh,vh,vj);');
  3692. Add(' doit(vi,vi,vi);');
  3693. Add(' doit(vj,vj,vj);');
  3694. Add('end;');
  3695. Add('var i: integer;');
  3696. Add('begin');
  3697. Add(' doit(i,i,i);');
  3698. ConvertProgram;
  3699. CheckSource('TestProc_VarParam',
  3700. LinesToStr([ // statements
  3701. 'this.DoIt = function (vG,vH,vI) {',
  3702. ' var vJ = 0;',
  3703. ' vG = vG + 1;',
  3704. ' vJ = vH + 2;',
  3705. ' vI.set(vI.get()+3);',
  3706. ' $mod.DoIt(vG, vG, {',
  3707. ' get: function () {',
  3708. ' return vG;',
  3709. ' },',
  3710. ' set: function (v) {',
  3711. ' vG = v;',
  3712. ' }',
  3713. ' });',
  3714. ' $mod.DoIt(vH, vH, {',
  3715. ' get: function () {',
  3716. ' return vJ;',
  3717. ' },',
  3718. ' set: function (v) {',
  3719. ' vJ = v;',
  3720. ' }',
  3721. ' });',
  3722. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3723. ' $mod.DoIt(vJ, vJ, {',
  3724. ' get: function () {',
  3725. ' return vJ;',
  3726. ' },',
  3727. ' set: function (v) {',
  3728. ' vJ = v;',
  3729. ' }',
  3730. ' });',
  3731. '};',
  3732. 'this.i = 0;'
  3733. ]),
  3734. LinesToStr([
  3735. '$mod.DoIt($mod.i,$mod.i,{',
  3736. ' p: $mod,',
  3737. ' get: function () {',
  3738. ' return this.p.i;',
  3739. ' },',
  3740. ' set: function (v) {',
  3741. ' this.p.i = v;',
  3742. ' }',
  3743. '});'
  3744. ]));
  3745. end;
  3746. procedure TTestModule.TestProc_VarParamString;
  3747. begin
  3748. StartProgram(false);
  3749. Add(['type TCaption = string;',
  3750. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3751. 'var c: char;',
  3752. 'begin',
  3753. ' va[1]:=c;',
  3754. ' vb[2]:=c;',
  3755. ' vc[3]:=c;',
  3756. 'end;',
  3757. 'begin']);
  3758. ConvertProgram;
  3759. CheckSource('TestProc_VarParamString',
  3760. LinesToStr([ // statements
  3761. 'this.DoIt = function (vA,vB,vC) {',
  3762. ' var c = "";',
  3763. ' vA = rtl.setCharAt(vA, 0, c);',
  3764. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3765. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3766. '};',
  3767. '']),
  3768. LinesToStr([
  3769. ]));
  3770. end;
  3771. procedure TTestModule.TestProc_VarParamV;
  3772. begin
  3773. StartProgram(false);
  3774. Add([
  3775. 'procedure Inc2(var i: longint);',
  3776. 'begin',
  3777. ' i:=i+2;',
  3778. 'end;',
  3779. 'procedure DoIt(v: longint);',
  3780. 'var p: array of longint;',
  3781. 'begin',
  3782. ' Inc2(v);',
  3783. ' Inc2(p[v]);',
  3784. 'end;',
  3785. 'begin']);
  3786. ConvertProgram;
  3787. CheckSource('TestProc_VarParamV',
  3788. LinesToStr([ // statements
  3789. 'this.Inc2 = function (i) {',
  3790. ' i.set(i.get()+2);',
  3791. '};',
  3792. 'this.DoIt = function (v) {',
  3793. ' var p = [];',
  3794. ' $mod.Inc2({get: function () {',
  3795. ' return v;',
  3796. ' }, set: function (w) {',
  3797. ' v = w;',
  3798. ' }});',
  3799. ' $mod.Inc2({',
  3800. ' a: v,',
  3801. ' p: p,',
  3802. ' get: function () {',
  3803. ' return this.p[this.a];',
  3804. ' },',
  3805. ' set: function (v) {',
  3806. ' this.p[this.a] = v;',
  3807. ' }',
  3808. ' });',
  3809. '};',
  3810. '']),
  3811. LinesToStr([
  3812. '']));
  3813. end;
  3814. procedure TTestModule.TestProc_Overload;
  3815. begin
  3816. StartProgram(false);
  3817. Add('procedure DoIt(vI: longint); begin end;');
  3818. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3819. Add('procedure DoIt(vD: double); begin end;');
  3820. Add('begin');
  3821. Add(' DoIt(1);');
  3822. Add(' DoIt(2,3);');
  3823. Add(' DoIt(4.5);');
  3824. ConvertProgram;
  3825. CheckSource('TestProcedureOverload',
  3826. LinesToStr([ // statements
  3827. 'this.DoIt = function (vI) {',
  3828. '};',
  3829. 'this.DoIt$1 = function (vI, vJ) {',
  3830. '};',
  3831. 'this.DoIt$2 = function (vD) {',
  3832. '};',
  3833. '']),
  3834. LinesToStr([
  3835. '$mod.DoIt(1);',
  3836. '$mod.DoIt$1(2, 3);',
  3837. '$mod.DoIt$2(4.5);',
  3838. '']));
  3839. end;
  3840. procedure TTestModule.TestProc_OverloadForward;
  3841. begin
  3842. StartProgram(false);
  3843. Add('procedure DoIt(vI: longint); forward;');
  3844. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3845. Add('procedure doit(vi: longint); begin end;');
  3846. Add('begin');
  3847. Add(' doit(1);');
  3848. Add(' doit(2,3);');
  3849. ConvertProgram;
  3850. CheckSource('TestProcedureOverloadForward',
  3851. LinesToStr([ // statements
  3852. 'this.DoIt$1 = function (vI, vJ) {',
  3853. '};',
  3854. 'this.DoIt = function (vI) {',
  3855. '};',
  3856. '']),
  3857. LinesToStr([
  3858. '$mod.DoIt(1);',
  3859. '$mod.DoIt$1(2, 3);',
  3860. '']));
  3861. end;
  3862. procedure TTestModule.TestProc_OverloadIntfImpl;
  3863. begin
  3864. StartUnit(false);
  3865. Add('interface');
  3866. Add('procedure DoIt(vI: longint);');
  3867. Add('procedure DoIt(vI, vJ: longint);');
  3868. Add('implementation');
  3869. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  3870. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  3871. Add('procedure DoIt(vi: longint); begin end;');
  3872. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  3873. Add('procedure DoIt(vi, vj: longint); begin end;');
  3874. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  3875. Add('begin');
  3876. Add(' doit(1);');
  3877. Add(' doit(2,3);');
  3878. Add(' doit(4,5,6);');
  3879. Add(' doit(7,8,9,10);');
  3880. Add(' doit(11,12,13,14,15);');
  3881. ConvertUnit;
  3882. CheckSource('TestProcedureOverloadUnit',
  3883. LinesToStr([ // statements
  3884. 'var $impl = $mod.$impl;',
  3885. 'this.DoIt = function (vI) {',
  3886. '};',
  3887. 'this.DoIt$1 = function (vI, vJ) {',
  3888. '};',
  3889. '']),
  3890. LinesToStr([ // this.$init
  3891. '$mod.DoIt(1);',
  3892. '$mod.DoIt$1(2, 3);',
  3893. '$impl.DoIt$3(4,5,6);',
  3894. '$impl.DoIt$4(7,8,9,10);',
  3895. '$impl.DoIt$2(11,12,13,14,15);',
  3896. '']),
  3897. LinesToStr([ // implementation
  3898. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  3899. '};',
  3900. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  3901. '};',
  3902. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  3903. '};',
  3904. '']));
  3905. end;
  3906. procedure TTestModule.TestProc_OverloadNested;
  3907. begin
  3908. StartProgram(false);
  3909. Add([
  3910. 'procedure doit(vA: longint);',
  3911. ' procedure DoIt(vA, vB: longint); overload;',
  3912. ' begin',
  3913. ' doit(1);',
  3914. ' doit(1,2);',
  3915. ' end;',
  3916. ' procedure doit(vA, vB, vC: longint);',
  3917. ' begin',
  3918. ' doit(1);',
  3919. ' doit(1,2);',
  3920. ' doit(1,2,3);',
  3921. ' end;',
  3922. 'begin',
  3923. ' doit(1);',
  3924. ' doit(1,2);',
  3925. ' doit(1,2,3);',
  3926. 'end;',
  3927. 'begin // main',
  3928. ' doit(1);']);
  3929. ConvertProgram;
  3930. CheckSource('TestProcedureOverloadNested',
  3931. LinesToStr([ // statements
  3932. 'this.doit = function (vA) {',
  3933. ' function DoIt$1(vA, vB) {',
  3934. ' $mod.doit(1);',
  3935. ' DoIt$1(1, 2);',
  3936. ' };',
  3937. ' function doit$2(vA, vB, vC) {',
  3938. ' $mod.doit(1);',
  3939. ' DoIt$1(1, 2);',
  3940. ' doit$2(1, 2, 3);',
  3941. ' };',
  3942. ' $mod.doit(1);',
  3943. ' DoIt$1(1, 2);',
  3944. ' doit$2(1, 2, 3);',
  3945. '};',
  3946. '']),
  3947. LinesToStr([
  3948. '$mod.doit(1);',
  3949. '']));
  3950. end;
  3951. procedure TTestModule.TestProc_OverloadNestedForward;
  3952. begin
  3953. StartProgram(false);
  3954. Add([
  3955. 'procedure DoIt(vA: longint); overload; forward;',
  3956. 'procedure DoIt(vB, vC: longint); overload;',
  3957. 'begin // 2 param overload',
  3958. ' doit(1);',
  3959. ' doit(1,2);',
  3960. 'end;',
  3961. 'procedure doit(vA: longint);',
  3962. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  3963. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  3964. ' begin // 4 param overload',
  3965. ' doit(1);',
  3966. ' doit(1,2);',
  3967. ' doit(1,2,3);',
  3968. ' doit(1,2,3,4);',
  3969. ' end;',
  3970. ' procedure doit(vA, vB, vC: longint);',
  3971. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  3972. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  3973. ' begin // 6 param overload',
  3974. ' doit(1);',
  3975. ' doit(1,2);',
  3976. ' doit(1,2,3);',
  3977. ' doit(1,2,3,4);',
  3978. ' doit(1,2,3,4,5);',
  3979. ' doit(1,2,3,4,5,6);',
  3980. ' end;',
  3981. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  3982. ' begin // 5 param overload',
  3983. ' doit(1);',
  3984. ' doit(1,2);',
  3985. ' doit(1,2,3);',
  3986. ' doit(1,2,3,4);',
  3987. ' doit(1,2,3,4,5);',
  3988. ' doit(1,2,3,4,5,6);',
  3989. ' end;',
  3990. ' begin // 3 param overload',
  3991. ' doit(1);',
  3992. ' doit(1,2);',
  3993. ' doit(1,2,3);',
  3994. ' doit(1,2,3,4);',
  3995. ' doit(1,2,3,4,5);',
  3996. ' doit(1,2,3,4,5,6);',
  3997. ' end;',
  3998. 'begin // 1 param overload',
  3999. ' doit(1);',
  4000. ' doit(1,2);',
  4001. ' doit(1,2,3);',
  4002. ' doit(1,2,3,4);',
  4003. 'end;',
  4004. 'begin // main',
  4005. ' doit(1);',
  4006. ' doit(1,2);']);
  4007. ConvertProgram;
  4008. CheckSource('TestProc_OverloadNestedForward',
  4009. LinesToStr([ // statements
  4010. 'this.DoIt$1 = function (vB, vC) {',
  4011. ' $mod.DoIt(1);',
  4012. ' $mod.DoIt$1(1, 2);',
  4013. '};',
  4014. 'this.DoIt = function (vA) {',
  4015. ' function DoIt$3(vA, vB, vC, vD) {',
  4016. ' $mod.DoIt(1);',
  4017. ' $mod.DoIt$1(1, 2);',
  4018. ' DoIt$2(1, 2, 3);',
  4019. ' DoIt$3(1, 2, 3, 4);',
  4020. ' };',
  4021. ' function DoIt$2(vA, vB, vC) {',
  4022. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4023. ' $mod.DoIt(1);',
  4024. ' $mod.DoIt$1(1, 2);',
  4025. ' DoIt$2(1, 2, 3);',
  4026. ' DoIt$3(1, 2, 3, 4);',
  4027. ' DoIt$4(1, 2, 3, 4, 5);',
  4028. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4029. ' };',
  4030. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4031. ' $mod.DoIt(1);',
  4032. ' $mod.DoIt$1(1, 2);',
  4033. ' DoIt$2(1, 2, 3);',
  4034. ' DoIt$3(1, 2, 3, 4);',
  4035. ' DoIt$4(1, 2, 3, 4, 5);',
  4036. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4037. ' };',
  4038. ' $mod.DoIt(1);',
  4039. ' $mod.DoIt$1(1, 2);',
  4040. ' DoIt$2(1, 2, 3);',
  4041. ' DoIt$3(1, 2, 3, 4);',
  4042. ' DoIt$4(1, 2, 3, 4, 5);',
  4043. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4044. ' };',
  4045. ' $mod.DoIt(1);',
  4046. ' $mod.DoIt$1(1, 2);',
  4047. ' DoIt$2(1, 2, 3);',
  4048. ' DoIt$3(1, 2, 3, 4);',
  4049. '};',
  4050. '']),
  4051. LinesToStr([
  4052. '$mod.DoIt(1);',
  4053. '$mod.DoIt$1(1, 2);',
  4054. '']));
  4055. end;
  4056. procedure TTestModule.TestProc_OverloadUnitCycle;
  4057. begin
  4058. AddModuleWithIntfImplSrc('Unit2.pas',
  4059. LinesToStr([
  4060. 'type',
  4061. ' TObject = class',
  4062. ' procedure DoIt(b: boolean); virtual; abstract;',
  4063. ' procedure DoIt(i: longint); virtual; abstract;',
  4064. ' end;',
  4065. '']),
  4066. 'uses test1;');
  4067. StartUnit(true);
  4068. Add([
  4069. 'interface',
  4070. 'uses unit2;',
  4071. 'type',
  4072. ' TEagle = class(TObject)',
  4073. ' procedure DoIt(b: boolean); override;',
  4074. ' procedure DoIt(i: longint); override;',
  4075. ' end;',
  4076. 'implementation',
  4077. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4078. 'procedure TEagle.DoIt(i: longint); begin end;',
  4079. '']);
  4080. ConvertUnit;
  4081. CheckSource('TestProc_OverloadUnitCycle',
  4082. LinesToStr([ // statements
  4083. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  4084. ' this.DoIt = function (b) {',
  4085. ' };',
  4086. ' this.DoIt$1 = function (i) {',
  4087. ' };',
  4088. '});',
  4089. '']),
  4090. '',
  4091. LinesToStr([
  4092. '']));
  4093. end;
  4094. procedure TTestModule.TestProc_Varargs;
  4095. begin
  4096. StartProgram(false);
  4097. Add([
  4098. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4099. 'procedure ProcB; varargs; external name ''ProcB'';',
  4100. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4101. 'function GetIt: longint; begin end;',
  4102. 'begin',
  4103. ' ProcA(1);',
  4104. ' ProcA(1,2);',
  4105. ' ProcA(1,2.0);',
  4106. ' ProcA(1,2,3);',
  4107. ' ProcA(1,''2'');',
  4108. ' ProcA(2,'''');',
  4109. ' ProcA(3,false);',
  4110. ' ProcB;',
  4111. ' ProcB();',
  4112. ' ProcB(4);',
  4113. ' ProcB(''foo'');',
  4114. ' ProcC;',
  4115. ' ProcC();',
  4116. ' ProcC(4);',
  4117. ' ProcC(5,''foo'');',
  4118. ' ProcB(GetIt);',
  4119. ' ProcB(GetIt());',
  4120. ' ProcB(GetIt,GetIt());']);
  4121. ConvertProgram;
  4122. CheckSource('TestProc_Varargs',
  4123. LinesToStr([ // statements
  4124. 'this.GetIt = function () {',
  4125. ' var Result = 0;',
  4126. ' return Result;',
  4127. '};',
  4128. '']),
  4129. LinesToStr([
  4130. 'ProcA(1);',
  4131. 'ProcA(1, 2);',
  4132. 'ProcA(1, 2.0);',
  4133. 'ProcA(1, 2, 3);',
  4134. 'ProcA(1, "2");',
  4135. 'ProcA(2, "");',
  4136. 'ProcA(3, false);',
  4137. 'ProcB();',
  4138. 'ProcB();',
  4139. 'ProcB(4);',
  4140. 'ProcB("foo");',
  4141. 'ProcC(17);',
  4142. 'ProcC(17);',
  4143. 'ProcC(4);',
  4144. 'ProcC(5, "foo");',
  4145. 'ProcB($mod.GetIt());',
  4146. 'ProcB($mod.GetIt());',
  4147. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4148. '']));
  4149. end;
  4150. procedure TTestModule.TestProc_ConstOrder;
  4151. begin
  4152. StartProgram(false);
  4153. Add([
  4154. 'const A = 3;',
  4155. 'const B = A+1;',
  4156. 'procedure DoIt;',
  4157. 'const C = A+1;',
  4158. 'const D = B+1;',
  4159. 'const E = D+C+B+A;',
  4160. 'begin',
  4161. 'end;',
  4162. 'begin'
  4163. ]);
  4164. ConvertProgram;
  4165. CheckSource('TestProc_ConstOrder',
  4166. LinesToStr([ // statements
  4167. 'this.A = 3;',
  4168. 'this.B = 3 + 1;',
  4169. 'var C = 3 + 1;',
  4170. 'var D = 4 + 1;',
  4171. 'var E = 5 + 4 + 4 + 3;',
  4172. 'this.DoIt = function () {',
  4173. '};',
  4174. '']),
  4175. LinesToStr([
  4176. ''
  4177. ]));
  4178. end;
  4179. procedure TTestModule.TestProc_DuplicateConst;
  4180. begin
  4181. StartProgram(false);
  4182. Add([
  4183. 'const A = 1;',
  4184. 'procedure DoIt;',
  4185. 'const A = 2;',
  4186. ' procedure SubIt;',
  4187. ' const A = 21;',
  4188. ' begin',
  4189. ' end;',
  4190. 'begin',
  4191. 'end;',
  4192. 'procedure DoSome;',
  4193. 'const A = 3;',
  4194. 'begin',
  4195. 'end;',
  4196. 'begin'
  4197. ]);
  4198. ConvertProgram;
  4199. CheckSource('TestProc_DuplicateConst',
  4200. LinesToStr([ // statements
  4201. 'this.A = 1;',
  4202. 'var A$1 = 2;',
  4203. 'var A$2 = 21;',
  4204. 'this.DoIt = function () {',
  4205. ' function SubIt() {',
  4206. ' };',
  4207. '};',
  4208. 'var A$3 = 3;',
  4209. 'this.DoSome = function () {',
  4210. '};',
  4211. '']),
  4212. LinesToStr([
  4213. ''
  4214. ]));
  4215. end;
  4216. procedure TTestModule.TestProc_LocalVarAbsolute;
  4217. begin
  4218. StartProgram(false);
  4219. Add([
  4220. 'type',
  4221. ' TObject = class',
  4222. ' Index: longint;',
  4223. ' procedure DoAbs(Item: pointer);',
  4224. ' end;',
  4225. 'procedure TObject.DoAbs(Item: pointer);',
  4226. 'var',
  4227. ' o: TObject absolute Item;',
  4228. 'begin',
  4229. ' if o.Index<o.Index then o.Index:=o.Index;',
  4230. 'end;',
  4231. 'procedure DoIt(i: longint; p: pointer);',
  4232. 'var',
  4233. ' d: double absolute i;',
  4234. ' s: string absolute d;',
  4235. ' oi: TObject absolute i;',
  4236. ' op: TObject absolute p;',
  4237. 'begin',
  4238. ' if d=d then d:=d;',
  4239. ' if s=s then s:=s;',
  4240. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4241. ' if op.Index=op.Index then op.Index:=op.Index;',
  4242. 'end;',
  4243. 'begin']);
  4244. ConvertProgram;
  4245. CheckSource('TestProc_LocalVarAbsolute',
  4246. LinesToStr([ // statements
  4247. 'rtl.createClass($mod, "TObject", null, function () {',
  4248. ' this.$init = function () {',
  4249. ' this.Index = 0;',
  4250. ' };',
  4251. ' this.$final = function () {',
  4252. ' };',
  4253. ' this.DoAbs = function (Item) {',
  4254. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4255. ' };',
  4256. '});',
  4257. 'this.DoIt = function (i, p) {',
  4258. ' if (i === i) i = i;',
  4259. ' if (i === i) i = i;',
  4260. ' if (i.Index < i.Index) i.Index = i.Index;',
  4261. ' if (p.Index === p.Index) p.Index = p.Index;',
  4262. '};'
  4263. ]),
  4264. LinesToStr([
  4265. ]));
  4266. end;
  4267. procedure TTestModule.TestProc_LocalVarInit;
  4268. begin
  4269. StartProgram(false);
  4270. Add([
  4271. 'type TBytes = array of byte;',
  4272. 'procedure DoIt;',
  4273. 'const c = 4;',
  4274. 'var',
  4275. ' b: byte = 1;',
  4276. ' w: word = 2+c;',
  4277. ' p: pointer = nil;',
  4278. ' Buffer: TBytes = nil;',
  4279. 'begin',
  4280. 'end;',
  4281. 'begin']);
  4282. ConvertProgram;
  4283. CheckSource('TestProc_LocalVarInit',
  4284. LinesToStr([ // statements
  4285. 'var c = 4;',
  4286. 'this.DoIt = function () {',
  4287. ' var b = 1;',
  4288. ' var w = 2 + 4;',
  4289. ' var p = null;',
  4290. ' var Buffer = [];',
  4291. '};',
  4292. '']),
  4293. LinesToStr([
  4294. ]));
  4295. end;
  4296. procedure TTestModule.TestProc_ReservedWords;
  4297. begin
  4298. StartProgram(false);
  4299. Add([
  4300. 'procedure Date(ArrayBuffer: longint);',
  4301. 'const',
  4302. ' NaN: longint = 3;',
  4303. 'var',
  4304. ' &Boolean: longint;',
  4305. ' procedure Error(ArrayBuffer: longint);',
  4306. ' begin',
  4307. ' end;',
  4308. 'begin',
  4309. ' Nan:=&bOolean;',
  4310. 'end;',
  4311. 'begin',
  4312. ' Date(1);']);
  4313. ConvertProgram;
  4314. CheckSource('TestProc_ReservedWords',
  4315. LinesToStr([ // statements
  4316. 'var naN = 3;',
  4317. 'this.Date = function (arrayBuffer) {',
  4318. ' var boolean = 0;',
  4319. ' function error(arrayBuffer) {',
  4320. ' };',
  4321. ' naN = boolean;',
  4322. '};',
  4323. '']),
  4324. LinesToStr([
  4325. ' $mod.Date(1);'
  4326. ]));
  4327. end;
  4328. procedure TTestModule.TestProc_ConstRefWord;
  4329. begin
  4330. StartProgram(false);
  4331. Add([
  4332. 'procedure Run(constref w: word);',
  4333. 'var l: word;',
  4334. 'begin',
  4335. ' l:=w;',
  4336. ' Run(w);',
  4337. ' Run(l);',
  4338. 'end;',
  4339. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4340. 'begin',
  4341. ' Run(a);',
  4342. ' Run(b);',
  4343. ' Run(c);',
  4344. ' Run(d);',
  4345. ' Run(e);',
  4346. 'end;',
  4347. 'begin',
  4348. ' Run(1);']);
  4349. ConvertProgram;
  4350. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4351. CheckSource('TestProc_ConstRefWord',
  4352. LinesToStr([ // statements
  4353. 'this.Run = function (w) {',
  4354. ' var l = 0;',
  4355. ' l = w;',
  4356. ' $mod.Run(w);',
  4357. ' $mod.Run(l);',
  4358. '};',
  4359. 'this.Fly = function (a, b, c, d, e) {',
  4360. ' $mod.Run(a);',
  4361. ' $mod.Run(b.get());',
  4362. ' $mod.Run(c.get());',
  4363. ' $mod.Run(d);',
  4364. ' $mod.Run(e);',
  4365. '};',
  4366. '']),
  4367. LinesToStr([
  4368. '$mod.Run(1);'
  4369. ]));
  4370. end;
  4371. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4372. begin
  4373. StartProgram(false);
  4374. Add([
  4375. '{$mode objfpc}',
  4376. 'type',
  4377. ' TFunc = reference to function(x: word): word;',
  4378. 'var Func: TFunc;',
  4379. 'procedure DoIt(a: word);',
  4380. 'begin',
  4381. ' Func:=function(b:word): word',
  4382. ' begin',
  4383. ' Result:=a+b;',
  4384. ' exit(b);',
  4385. ' exit(Result);',
  4386. ' end;',// test semicolon
  4387. ' a:=3;',
  4388. 'end;',
  4389. 'begin',
  4390. ' Func:=function(c:word):word begin',
  4391. ' Result:=3+c;',
  4392. ' exit(c);',
  4393. ' exit(Result);',
  4394. ' end;']);
  4395. ConvertProgram;
  4396. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4397. LinesToStr([ // statements
  4398. 'this.Func = null;',
  4399. 'this.DoIt = function (a) {',
  4400. ' $mod.Func = function (b) {',
  4401. ' var Result = 0;',
  4402. ' Result = a + b;',
  4403. ' return b;',
  4404. ' return Result;',
  4405. ' return Result;',
  4406. ' };',
  4407. ' a = 3;',
  4408. '};',
  4409. '']),
  4410. LinesToStr([
  4411. '$mod.Func = function (c) {',
  4412. ' var Result = 0;',
  4413. ' Result = 3 + c;',
  4414. ' return c;',
  4415. ' return Result;',
  4416. ' return Result;',
  4417. '};',
  4418. '']));
  4419. end;
  4420. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4421. begin
  4422. StartProgram(false);
  4423. Add([
  4424. '{$mode delphi}',
  4425. 'type',
  4426. ' TProc = reference to procedure(x: word);',
  4427. 'procedure DoIt(a: word);',
  4428. 'var Proc: TProc;',
  4429. 'begin',
  4430. ' Proc:=procedure(b:word) begin end;',
  4431. 'end;',
  4432. 'var Proc: TProc;',
  4433. 'begin',
  4434. ' Proc:=procedure(c:word) begin end;',
  4435. '']);
  4436. ConvertProgram;
  4437. CheckSource('TestAnonymousProc_Assign_Delphi',
  4438. LinesToStr([ // statements
  4439. 'this.DoIt = function (a) {',
  4440. ' var Proc = null;',
  4441. ' Proc = function (b) {',
  4442. ' };',
  4443. '};',
  4444. 'this.Proc = null;',
  4445. '']),
  4446. LinesToStr([
  4447. '$mod.Proc = function (c) {',
  4448. '};',
  4449. '']));
  4450. end;
  4451. procedure TTestModule.TestAnonymousProc_Arg;
  4452. begin
  4453. StartProgram(false);
  4454. Add([
  4455. 'type',
  4456. ' TProc = reference to procedure;',
  4457. ' TFunc = reference to function(x: word): word;',
  4458. 'procedure DoMore(f,g: TProc);',
  4459. 'begin',
  4460. 'end;',
  4461. 'procedure DoOdd(v: jsvalue);',
  4462. 'begin',
  4463. 'end;',
  4464. 'procedure DoIt(f: TFunc);',
  4465. 'begin',
  4466. ' DoIt(function(b:word): word',
  4467. ' begin',
  4468. ' Result:=1+b;',
  4469. ' end);',
  4470. ' DoMore(procedure begin end, procedure begin end);',
  4471. ' DoOdd(procedure begin end);',
  4472. 'end;',
  4473. 'begin',
  4474. ' DoMore(procedure begin end,',
  4475. ' procedure assembler asm',
  4476. ' console.log("c");',
  4477. ' end);',
  4478. '']);
  4479. ConvertProgram;
  4480. CheckSource('TestAnonymousProc_Arg',
  4481. LinesToStr([ // statements
  4482. 'this.DoMore = function (f, g) {',
  4483. '};',
  4484. 'this.DoOdd = function (v) {',
  4485. '};',
  4486. 'this.DoIt = function (f) {',
  4487. ' $mod.DoIt(function (b) {',
  4488. ' var Result = 0;',
  4489. ' Result = 1 + b;',
  4490. ' return Result;',
  4491. ' });',
  4492. ' $mod.DoMore(function () {',
  4493. ' }, function () {',
  4494. ' });',
  4495. ' $mod.DoOdd(function () {',
  4496. ' });',
  4497. '};',
  4498. '']),
  4499. LinesToStr([
  4500. '$mod.DoMore(function () {',
  4501. '}, function () {',
  4502. ' console.log("c");',
  4503. '});',
  4504. '']));
  4505. end;
  4506. procedure TTestModule.TestAnonymousProc_Typecast;
  4507. begin
  4508. StartProgram(false);
  4509. Add([
  4510. 'type',
  4511. ' TProc = reference to procedure(w: word);',
  4512. ' TArr = array of word;',
  4513. ' TFuncArr = reference to function: TArr;',
  4514. 'procedure DoIt(p: TProc);',
  4515. 'var',
  4516. ' w: word;',
  4517. ' a: TArr;',
  4518. 'begin',
  4519. ' p:=TProc(procedure(b: smallint) begin end);',
  4520. ' a:=TFuncArr(function: TArr begin end)();',
  4521. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4522. 'end;',
  4523. 'begin']);
  4524. ConvertProgram;
  4525. CheckSource('TestAnonymousProc_Typecast',
  4526. LinesToStr([ // statements
  4527. 'this.DoIt = function (p) {',
  4528. ' var w = 0;',
  4529. ' var a = [];',
  4530. ' p = function (b) {',
  4531. ' };',
  4532. ' a = function () {',
  4533. ' var Result = [];',
  4534. ' return Result;',
  4535. ' }();',
  4536. ' w = function () {',
  4537. ' var Result = [];',
  4538. ' return Result;',
  4539. ' }()[3];',
  4540. '};',
  4541. '']),
  4542. LinesToStr([
  4543. '']));
  4544. end;
  4545. procedure TTestModule.TestAnonymousProc_With;
  4546. begin
  4547. StartProgram(false);
  4548. Add([
  4549. 'type',
  4550. ' TProc = reference to procedure(w: word);',
  4551. ' TObject = class',
  4552. ' b: boolean;',
  4553. ' end;',
  4554. 'var',
  4555. ' p: TProc;',
  4556. ' bird: TObject;',
  4557. 'begin',
  4558. ' with bird do',
  4559. ' p:=procedure(w: word)',
  4560. ' begin',
  4561. ' b:=w>2;',
  4562. ' end;',
  4563. '']);
  4564. ConvertProgram;
  4565. CheckSource('TestAnonymousProc_With',
  4566. LinesToStr([ // statements
  4567. 'rtl.createClass($mod, "TObject", null, function () {',
  4568. ' this.$init = function () {',
  4569. ' this.b = false;',
  4570. ' };',
  4571. ' this.$final = function () {',
  4572. ' };',
  4573. '});',
  4574. 'this.p = null;',
  4575. 'this.bird = null;',
  4576. '']),
  4577. LinesToStr([
  4578. 'var $with1 = $mod.bird;',
  4579. '$mod.p = function (w) {',
  4580. ' $with1.b = w > 2;',
  4581. '};',
  4582. '']));
  4583. end;
  4584. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4585. begin
  4586. StartProgram(false);
  4587. Add([
  4588. 'type',
  4589. ' TProc = reference to procedure;',
  4590. ' TObject = class',
  4591. ' b: boolean;',
  4592. ' end;',
  4593. 'procedure DoIt;',
  4594. 'var',
  4595. ' p: TProc;',
  4596. 'begin',
  4597. ' try',
  4598. ' except',
  4599. ' on E: TObject do',
  4600. ' p:=procedure',
  4601. ' begin',
  4602. ' E.b:=true;',
  4603. ' end;',
  4604. ' end;',
  4605. 'end;',
  4606. 'begin']);
  4607. ConvertProgram;
  4608. CheckSource('TestAnonymousProc_ExceptOn',
  4609. LinesToStr([ // statements
  4610. 'rtl.createClass($mod, "TObject", null, function () {',
  4611. ' this.$init = function () {',
  4612. ' this.b = false;',
  4613. ' };',
  4614. ' this.$final = function () {',
  4615. ' };',
  4616. '});',
  4617. 'this.DoIt = function () {',
  4618. ' var p = null;',
  4619. ' try {} catch ($e) {',
  4620. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4621. ' var E = $e;',
  4622. ' p = function () {',
  4623. ' E.b = true;',
  4624. ' };',
  4625. ' } else throw $e',
  4626. ' };',
  4627. '};',
  4628. '']),
  4629. LinesToStr([
  4630. '']));
  4631. end;
  4632. procedure TTestModule.TestAnonymousProc_Nested;
  4633. begin
  4634. StartProgram(false);
  4635. Add([
  4636. 'type',
  4637. ' TProc = reference to procedure;',
  4638. ' TObject = class',
  4639. ' i: byte;',
  4640. ' procedure DoIt;',
  4641. ' end;',
  4642. 'procedure TObject.DoIt;',
  4643. 'var',
  4644. ' p: TProc;',
  4645. ' procedure Sub;',
  4646. ' begin',
  4647. ' p:=procedure',
  4648. ' begin',
  4649. ' i:=3;',
  4650. ' Self.i:=4;',
  4651. ' p:=procedure',
  4652. ' procedure SubSub;',
  4653. ' begin',
  4654. ' i:=13;',
  4655. ' Self.i:=14;',
  4656. ' end;',
  4657. ' begin',
  4658. ' i:=13;',
  4659. ' Self.i:=14;',
  4660. ' end;',
  4661. ' end;',
  4662. ' end;',
  4663. 'begin',
  4664. 'end;',
  4665. 'begin']);
  4666. ConvertProgram;
  4667. CheckSource('TestAnonymousProc_Nested',
  4668. LinesToStr([ // statements
  4669. 'rtl.createClass($mod, "TObject", null, function () {',
  4670. ' this.$init = function () {',
  4671. ' this.i = 0;',
  4672. ' };',
  4673. ' this.$final = function () {',
  4674. ' };',
  4675. ' this.DoIt = function () {',
  4676. ' var $Self = this;',
  4677. ' var p = null;',
  4678. ' function Sub() {',
  4679. ' p = function () {',
  4680. ' $Self.i = 3;',
  4681. ' $Self.i = 4;',
  4682. ' p = function () {',
  4683. ' function SubSub() {',
  4684. ' $Self.i = 13;',
  4685. ' $Self.i = 14;',
  4686. ' };',
  4687. ' $Self.i = 13;',
  4688. ' $Self.i = 14;',
  4689. ' };',
  4690. ' };',
  4691. ' };',
  4692. ' };',
  4693. '});',
  4694. '']),
  4695. LinesToStr([
  4696. '']));
  4697. end;
  4698. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4699. begin
  4700. StartProgram(false);
  4701. Add([
  4702. 'type',
  4703. ' TProc = reference to procedure;',
  4704. 'function DoIt: TProc;',
  4705. ' function Sub: TProc;',
  4706. ' begin',
  4707. ' Result:=procedure',
  4708. ' begin',
  4709. ' Sub:=procedure',
  4710. ' procedure SubSub;',
  4711. ' begin',
  4712. ' Result:=nil;',
  4713. ' Sub:=nil;',
  4714. ' DoIt:=nil;',
  4715. ' end;',
  4716. ' begin',
  4717. ' Result:=nil;',
  4718. ' Sub:=nil;',
  4719. ' DoIt:=nil;',
  4720. ' end;',
  4721. ' end;',
  4722. ' end;',
  4723. 'begin',
  4724. 'end;',
  4725. 'begin']);
  4726. ConvertProgram;
  4727. CheckSource('TestAnonymousProc_NestedAssignResult',
  4728. LinesToStr([ // statements
  4729. 'this.DoIt = function () {',
  4730. ' var Result = null;',
  4731. ' function Sub() {',
  4732. ' var Result$1 = null;',
  4733. ' Result$1 = function () {',
  4734. ' Result$1 = function () {',
  4735. ' function SubSub() {',
  4736. ' Result$1 = null;',
  4737. ' Result$1 = null;',
  4738. ' Result = null;',
  4739. ' };',
  4740. ' Result$1 = null;',
  4741. ' Result$1 = null;',
  4742. ' Result = null;',
  4743. ' };',
  4744. ' };',
  4745. ' return Result$1;',
  4746. ' };',
  4747. ' return Result;',
  4748. '};',
  4749. '']),
  4750. LinesToStr([
  4751. '']));
  4752. end;
  4753. procedure TTestModule.TestAnonymousProc_Class;
  4754. begin
  4755. StartProgram(false);
  4756. Add([
  4757. 'type',
  4758. ' TProc = reference to procedure;',
  4759. ' TEvent = procedure of object;',
  4760. ' TObject = class',
  4761. ' Size: word;',
  4762. ' function GetIt: TProc;',
  4763. ' procedure DoIt; virtual; abstract;',
  4764. ' end;',
  4765. 'function TObject.GetIt: TProc;',
  4766. 'begin',
  4767. ' Result:=procedure',
  4768. ' var p: TEvent;',
  4769. ' begin',
  4770. ' Size:=Size;',
  4771. ' Size:=Self.Size;',
  4772. ' p:=@DoIt;',
  4773. ' p:[email protected];',
  4774. ' end;',
  4775. 'end;',
  4776. 'begin']);
  4777. ConvertProgram;
  4778. CheckSource('TestAnonymousProc_Class',
  4779. LinesToStr([ // statements
  4780. 'rtl.createClass($mod, "TObject", null, function () {',
  4781. ' this.$init = function () {',
  4782. ' this.Size = 0;',
  4783. ' };',
  4784. ' this.$final = function () {',
  4785. ' };',
  4786. ' this.GetIt = function () {',
  4787. ' var $Self = this;',
  4788. ' var Result = null;',
  4789. ' Result = function () {',
  4790. ' var p = null;',
  4791. ' $Self.Size = $Self.Size;',
  4792. ' $Self.Size = $Self.Size;',
  4793. ' p = rtl.createCallback($Self, "DoIt");',
  4794. ' p = rtl.createCallback($Self, "DoIt");',
  4795. ' };',
  4796. ' return Result;',
  4797. ' };',
  4798. '});',
  4799. '']),
  4800. LinesToStr([
  4801. '']));
  4802. end;
  4803. procedure TTestModule.TestAnonymousProc_ForLoop;
  4804. begin
  4805. StartProgram(false);
  4806. Add([
  4807. 'type TProc = reference to procedure;',
  4808. 'procedure Foo(p: TProc);',
  4809. 'begin',
  4810. 'end;',
  4811. 'procedure DoIt;',
  4812. 'var i: word;',
  4813. ' a: word;',
  4814. 'begin',
  4815. ' for i:=1 to 10 do begin',
  4816. ' Foo(procedure begin a:=3; end);',
  4817. ' end;',
  4818. 'end;',
  4819. 'begin',
  4820. ' DoIt;']);
  4821. ConvertProgram;
  4822. CheckSource('TestAnonymousProc_ForLoop',
  4823. LinesToStr([ // statements
  4824. 'this.Foo = function (p) {',
  4825. '};',
  4826. 'this.DoIt = function () {',
  4827. ' var i = 0;',
  4828. ' var a = 0;',
  4829. ' for (i = 1; i <= 10; i++) {',
  4830. ' $mod.Foo(function () {',
  4831. ' a = 3;',
  4832. ' });',
  4833. ' };',
  4834. '};',
  4835. '']),
  4836. LinesToStr([
  4837. '$mod.DoIt();'
  4838. ]));
  4839. end;
  4840. procedure TTestModule.TestEnum_Name;
  4841. begin
  4842. StartProgram(false);
  4843. Add('type TMyEnum = (Red, Green, Blue);');
  4844. Add('var e: TMyEnum;');
  4845. Add('var f: TMyEnum = Blue;');
  4846. Add('begin');
  4847. Add(' e:=green;');
  4848. Add(' e:=default(TMyEnum);');
  4849. ConvertProgram;
  4850. CheckSource('TestEnumName',
  4851. LinesToStr([ // statements
  4852. 'this.TMyEnum = {',
  4853. ' "0":"Red",',
  4854. ' Red:0,',
  4855. ' "1":"Green",',
  4856. ' Green:1,',
  4857. ' "2":"Blue",',
  4858. ' Blue:2',
  4859. ' };',
  4860. 'this.e = 0;',
  4861. 'this.f = $mod.TMyEnum.Blue;'
  4862. ]),
  4863. LinesToStr([
  4864. '$mod.e=$mod.TMyEnum.Green;',
  4865. '$mod.e=$mod.TMyEnum.Red;'
  4866. ]));
  4867. end;
  4868. procedure TTestModule.TestEnum_Number;
  4869. begin
  4870. Converter.Options:=Converter.Options+[coEnumNumbers];
  4871. StartProgram(false);
  4872. Add('type TMyEnum = (Red, Green);');
  4873. Add('var');
  4874. Add(' e: TMyEnum;');
  4875. Add(' f: TMyEnum = Green;');
  4876. Add(' i: longint;');
  4877. Add('begin');
  4878. Add(' e:=green;');
  4879. Add(' i:=longint(e);');
  4880. ConvertProgram;
  4881. CheckSource('TestEnumNumber',
  4882. LinesToStr([ // statements
  4883. 'this.TMyEnum = {',
  4884. ' "0":"Red",',
  4885. ' Red:0,',
  4886. ' "1":"Green",',
  4887. ' Green:1',
  4888. ' };',
  4889. 'this.e = 0;',
  4890. 'this.f = 1;',
  4891. 'this.i = 0;'
  4892. ]),
  4893. LinesToStr([
  4894. '$mod.e=1;',
  4895. '$mod.i=$mod.e;'
  4896. ]));
  4897. end;
  4898. procedure TTestModule.TestEnum_ConstFail;
  4899. begin
  4900. StartProgram(false);
  4901. Add([
  4902. 'type TMyEnum = (Red = 100, Green = 101);',
  4903. 'var',
  4904. ' e: TMyEnum;',
  4905. ' f: TMyEnum = Green;',
  4906. 'begin',
  4907. ' e:=green;']);
  4908. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  4909. ConvertProgram;
  4910. end;
  4911. procedure TTestModule.TestEnum_Functions;
  4912. begin
  4913. StartProgram(false);
  4914. Add([
  4915. 'type TMyEnum = (Red, Green);',
  4916. 'procedure DoIt(var e: TMyEnum; var i: word);',
  4917. 'var',
  4918. ' v: longint;',
  4919. ' s: string;',
  4920. 'begin',
  4921. ' val(s,e,v);',
  4922. ' val(s,e,i);',
  4923. 'end;',
  4924. 'var',
  4925. ' e: TMyEnum;',
  4926. ' i: longint;',
  4927. ' s: string;',
  4928. ' b: boolean;',
  4929. 'begin',
  4930. ' i:=ord(red);',
  4931. ' i:=ord(green);',
  4932. ' i:=ord(e);',
  4933. ' i:=ord(b);',
  4934. ' e:=low(tmyenum);',
  4935. ' e:=low(e);',
  4936. ' b:=low(boolean);',
  4937. ' e:=high(tmyenum);',
  4938. ' e:=high(e);',
  4939. ' b:=high(boolean);',
  4940. ' e:=pred(green);',
  4941. ' e:=pred(e);',
  4942. ' b:=pred(b);',
  4943. ' e:=succ(red);',
  4944. ' e:=succ(e);',
  4945. ' b:=succ(b);',
  4946. ' e:=tmyenum(1);',
  4947. ' e:=tmyenum(i);',
  4948. ' s:=str(e);',
  4949. ' str(e,s);',
  4950. ' str(red,s);',
  4951. ' s:=str(e:3);',
  4952. ' writestr(s,e:3,red);',
  4953. ' val(s,e,i);',
  4954. ' e:=TMyEnum(i);',
  4955. ' i:=longint(e);']);
  4956. ConvertProgram;
  4957. CheckSource('TestEnum_Functions',
  4958. LinesToStr([ // statements
  4959. 'this.TMyEnum = {',
  4960. ' "0":"Red",',
  4961. ' Red:0,',
  4962. ' "1":"Green",',
  4963. ' Green:1',
  4964. ' };',
  4965. 'this.DoIt = function (e, i) {',
  4966. ' var v = 0;',
  4967. ' var s = "";',
  4968. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  4969. ' v = w;',
  4970. ' }));',
  4971. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  4972. '};',
  4973. 'this.e = 0;',
  4974. 'this.i = 0;',
  4975. 'this.s = "";',
  4976. 'this.b = false;',
  4977. '']),
  4978. LinesToStr([
  4979. '$mod.i=$mod.TMyEnum.Red;',
  4980. '$mod.i=$mod.TMyEnum.Green;',
  4981. '$mod.i=$mod.e;',
  4982. '$mod.i=$mod.b+0;',
  4983. '$mod.e=$mod.TMyEnum.Red;',
  4984. '$mod.e=$mod.TMyEnum.Red;',
  4985. '$mod.b=false;',
  4986. '$mod.e=$mod.TMyEnum.Green;',
  4987. '$mod.e=$mod.TMyEnum.Green;',
  4988. '$mod.b=true;',
  4989. '$mod.e=$mod.TMyEnum.Green-1;',
  4990. '$mod.e=$mod.e-1;',
  4991. '$mod.b=false;',
  4992. '$mod.e=$mod.TMyEnum.Red+1;',
  4993. '$mod.e=$mod.e+1;',
  4994. '$mod.b=true;',
  4995. '$mod.e=1;',
  4996. '$mod.e=$mod.i;',
  4997. '$mod.s = $mod.TMyEnum[$mod.e];',
  4998. '$mod.s = $mod.TMyEnum[$mod.e];',
  4999. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5000. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5001. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5002. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5003. ' $mod.i = v;',
  5004. '});',
  5005. '$mod.e=$mod.i;',
  5006. '$mod.i=$mod.e;',
  5007. '']));
  5008. end;
  5009. procedure TTestModule.TestEnum_AsParams;
  5010. begin
  5011. StartProgram(false);
  5012. Add('type TEnum = (Red,Blue);');
  5013. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5014. Add('var vJ: TEnum;');
  5015. Add('begin');
  5016. Add(' vg:=vg;');
  5017. Add(' vj:=vh;');
  5018. Add(' vi:=vi;');
  5019. Add(' doit(vg,vg,vg);');
  5020. Add(' doit(vh,vh,vj);');
  5021. Add(' doit(vi,vi,vi);');
  5022. Add(' doit(vj,vj,vj);');
  5023. Add('end;');
  5024. Add('var i: TEnum;');
  5025. Add('begin');
  5026. Add(' doit(i,i,i);');
  5027. ConvertProgram;
  5028. CheckSource('TestEnum_AsParams',
  5029. LinesToStr([ // statements
  5030. 'this.TEnum = {',
  5031. ' "0": "Red",',
  5032. ' Red: 0,',
  5033. ' "1": "Blue",',
  5034. ' Blue: 1',
  5035. '};',
  5036. 'this.DoIt = function (vG,vH,vI) {',
  5037. ' var vJ = 0;',
  5038. ' vG = vG;',
  5039. ' vJ = vH;',
  5040. ' vI.set(vI.get());',
  5041. ' $mod.DoIt(vG, vG, {',
  5042. ' get: function () {',
  5043. ' return vG;',
  5044. ' },',
  5045. ' set: function (v) {',
  5046. ' vG = v;',
  5047. ' }',
  5048. ' });',
  5049. ' $mod.DoIt(vH, vH, {',
  5050. ' get: function () {',
  5051. ' return vJ;',
  5052. ' },',
  5053. ' set: function (v) {',
  5054. ' vJ = v;',
  5055. ' }',
  5056. ' });',
  5057. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5058. ' $mod.DoIt(vJ, vJ, {',
  5059. ' get: function () {',
  5060. ' return vJ;',
  5061. ' },',
  5062. ' set: function (v) {',
  5063. ' vJ = v;',
  5064. ' }',
  5065. ' });',
  5066. '};',
  5067. 'this.i = 0;'
  5068. ]),
  5069. LinesToStr([
  5070. '$mod.DoIt($mod.i,$mod.i,{',
  5071. ' p: $mod,',
  5072. ' get: function () {',
  5073. ' return this.p.i;',
  5074. ' },',
  5075. ' set: function (v) {',
  5076. ' this.p.i = v;',
  5077. ' }',
  5078. '});'
  5079. ]));
  5080. end;
  5081. procedure TTestModule.TestEnumRange_Array;
  5082. begin
  5083. StartProgram(false);
  5084. Add([
  5085. 'type',
  5086. ' TEnum = (Red, Green, Blue);',
  5087. ' TEnumRg = green..blue;',
  5088. ' TArr = array[TEnumRg] of byte;',
  5089. ' TArr2 = array[green..blue] of byte;',
  5090. 'var',
  5091. ' a: TArr;',
  5092. ' b: TArr = (3,4);',
  5093. ' c: TArr2 = (5,6);',
  5094. 'begin',
  5095. ' a[green] := b[blue];',
  5096. ' c[green] := c[blue];',
  5097. '']);
  5098. ConvertProgram;
  5099. CheckSource('TestEnumRange_Array',
  5100. LinesToStr([ // statements
  5101. 'this.TEnum = {',
  5102. ' "0": "Red",',
  5103. ' Red: 0,',
  5104. ' "1": "Green",',
  5105. ' Green: 1,',
  5106. ' "2": "Blue",',
  5107. ' Blue: 2',
  5108. '};',
  5109. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5110. 'this.b = [3, 4];',
  5111. 'this.c = [5, 6];',
  5112. '']),
  5113. LinesToStr([
  5114. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5115. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5116. '']));
  5117. end;
  5118. procedure TTestModule.TestEnum_ForIn;
  5119. begin
  5120. StartProgram(false);
  5121. Add([
  5122. 'type',
  5123. ' TEnum = (Red, Green, Blue);',
  5124. ' TEnumRg = green..blue;',
  5125. ' TArr = array[TEnum] of byte;',
  5126. ' TArrRg = array[TEnumRg] of byte;',
  5127. 'var',
  5128. ' e: TEnum;',
  5129. ' a1: TArr = (3,4,5);',
  5130. ' a2: TArrRg = (11,12);',
  5131. ' b: byte;',
  5132. 'begin',
  5133. ' for e in TEnum do ;',
  5134. ' for e in TEnumRg do ;',
  5135. ' for e in TArr do ;',
  5136. ' for e in TArrRg do ;',
  5137. ' for b in a1 do ;',
  5138. ' for b in a2 do ;',
  5139. '']);
  5140. ConvertProgram;
  5141. CheckSource('TestEnum_ForIn',
  5142. LinesToStr([ // statements
  5143. 'this.TEnum = {',
  5144. ' "0": "Red",',
  5145. ' Red: 0,',
  5146. ' "1": "Green",',
  5147. ' Green: 1,',
  5148. ' "2": "Blue",',
  5149. ' Blue: 2',
  5150. '};',
  5151. 'this.e = 0;',
  5152. 'this.a1 = [3, 4, 5];',
  5153. 'this.a2 = [11, 12];',
  5154. 'this.b = 0;',
  5155. '']),
  5156. LinesToStr([
  5157. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5158. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5159. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5160. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5161. ' for (var $in1 = $mod.a1, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.b = $in1[$l2];',
  5162. ' for (var $in4 = $mod.a2, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.b = $in4[$l5];',
  5163. '']));
  5164. end;
  5165. procedure TTestModule.TestEnum_ScopedNumber;
  5166. begin
  5167. Converter.Options:=Converter.Options+[coEnumNumbers];
  5168. StartProgram(false);
  5169. Add([
  5170. 'type',
  5171. ' TEnum = (Red, Green);',
  5172. 'var',
  5173. ' e: TEnum;',
  5174. 'begin',
  5175. ' e:=TEnum.Green;',
  5176. '']);
  5177. ConvertProgram;
  5178. CheckSource('TestEnum_ScopedNumber',
  5179. LinesToStr([ // statements
  5180. 'this.TEnum = {',
  5181. ' "0": "Red",',
  5182. ' Red: 0,',
  5183. ' "1": "Green",',
  5184. ' Green: 1',
  5185. '};',
  5186. 'this.e = 0;',
  5187. '']),
  5188. LinesToStr([
  5189. '$mod.e = 1;']));
  5190. end;
  5191. procedure TTestModule.TestEnum_InFunction;
  5192. begin
  5193. StartProgram(false);
  5194. Add([
  5195. 'const TEnum = 3;',
  5196. 'procedure DoIt;',
  5197. 'type',
  5198. ' TEnum = (Red, Green, Blue);',
  5199. ' procedure Sub;',
  5200. ' type',
  5201. ' TEnumSub = (Left, Right);',
  5202. ' var',
  5203. ' es: TEnumSub;',
  5204. ' begin',
  5205. ' es:=Left;',
  5206. ' end;',
  5207. 'var',
  5208. ' e, e2: TEnum;',
  5209. 'begin',
  5210. ' if e in [red,blue] then e2:=e;',
  5211. 'end;',
  5212. 'begin']);
  5213. ConvertProgram;
  5214. CheckSource('TestEnum_InFunction',
  5215. LinesToStr([ // statements
  5216. 'this.TEnum = 3;',
  5217. 'var TEnum$1 = {',
  5218. ' "0":"Red",',
  5219. ' Red:0,',
  5220. ' "1":"Green",',
  5221. ' Green:1,',
  5222. ' "2":"Blue",',
  5223. ' Blue:2',
  5224. ' };',
  5225. 'var TEnumSub = {',
  5226. ' "0": "Left",',
  5227. ' Left: 0,',
  5228. ' "1": "Right",',
  5229. ' Right: 1',
  5230. '};',
  5231. 'this.DoIt = function () {',
  5232. ' function Sub() {',
  5233. ' var es = 0;',
  5234. ' es = TEnumSub.Left;',
  5235. ' };',
  5236. ' var e = 0;',
  5237. ' var e2 = 0;',
  5238. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5239. '};',
  5240. '']),
  5241. LinesToStr([
  5242. '']));
  5243. end;
  5244. procedure TTestModule.TestSet_Enum;
  5245. begin
  5246. StartProgram(false);
  5247. Add([
  5248. 'type',
  5249. ' TColor = (Red, Green, Blue);',
  5250. ' TColors = set of TColor;',
  5251. 'var',
  5252. ' c: TColor;',
  5253. ' s: TColors;',
  5254. ' t: TColors = [];',
  5255. ' u: TColors = [Red];',
  5256. 'begin',
  5257. ' s:=[];',
  5258. ' s:=[Green];',
  5259. ' s:=[Green,Blue];',
  5260. ' s:=[Red..Blue];',
  5261. ' s:=[Red,Green..Blue];',
  5262. ' s:=[Red,c];',
  5263. ' s:=t;',
  5264. ' s:=default(TColors);',
  5265. '']);
  5266. ConvertProgram;
  5267. CheckSource('TestSet',
  5268. LinesToStr([ // statements
  5269. 'this.TColor = {',
  5270. ' "0":"Red",',
  5271. ' Red:0,',
  5272. ' "1":"Green",',
  5273. ' Green:1,',
  5274. ' "2":"Blue",',
  5275. ' Blue:2',
  5276. ' };',
  5277. 'this.c = 0;',
  5278. 'this.s = {};',
  5279. 'this.t = {};',
  5280. 'this.u = rtl.createSet($mod.TColor.Red);'
  5281. ]),
  5282. LinesToStr([
  5283. '$mod.s={};',
  5284. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5285. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5286. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5287. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5288. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5289. '$mod.s=rtl.refSet($mod.t);',
  5290. '$mod.s={};',
  5291. '']));
  5292. end;
  5293. procedure TTestModule.TestSet_Operators;
  5294. begin
  5295. StartProgram(false);
  5296. Add('type');
  5297. Add(' TColor = (Red, Green, Blue);');
  5298. Add(' TColors = set of tcolor;');
  5299. Add('var');
  5300. Add(' vC: TColor;');
  5301. Add(' vS: TColors;');
  5302. Add(' vT: TColors;');
  5303. Add(' vU: TColors;');
  5304. Add(' B: boolean;');
  5305. Add('begin');
  5306. Add(' include(vs,green);');
  5307. Add(' exclude(vs,vc);');
  5308. Add(' vs:=vt+vu;');
  5309. Add(' vs:=vt+[red];');
  5310. Add(' vs:=[red]+vt;');
  5311. Add(' vs:=[red]+[green];');
  5312. Add(' vs:=vt-vu;');
  5313. Add(' vs:=vt-[red];');
  5314. Add(' vs:=[red]-vt;');
  5315. Add(' vs:=[red]-[green];');
  5316. Add(' vs:=vt*vu;');
  5317. Add(' vs:=vt*[red];');
  5318. Add(' vs:=[red]*vt;');
  5319. Add(' vs:=[red]*[green];');
  5320. Add(' vs:=vt><vu;');
  5321. Add(' vs:=vt><[red];');
  5322. Add(' vs:=[red]><vt;');
  5323. Add(' vs:=[red]><[green];');
  5324. Add(' b:=vt=vu;');
  5325. Add(' b:=vt=[red];');
  5326. Add(' b:=[red]=vt;');
  5327. Add(' b:=[red]=[green];');
  5328. Add(' b:=vt<>vu;');
  5329. Add(' b:=vt<>[red];');
  5330. Add(' b:=[red]<>vt;');
  5331. Add(' b:=[red]<>[green];');
  5332. Add(' b:=vt<=vu;');
  5333. Add(' b:=vt<=[red];');
  5334. Add(' b:=[red]<=vt;');
  5335. Add(' b:=[red]<=[green];');
  5336. Add(' b:=vt>=vu;');
  5337. Add(' b:=vt>=[red];');
  5338. Add(' b:=[red]>=vt;');
  5339. Add(' b:=[red]>=[green];');
  5340. ConvertProgram;
  5341. CheckSource('TestSet_Operators',
  5342. LinesToStr([ // statements
  5343. 'this.TColor = {',
  5344. ' "0":"Red",',
  5345. ' Red:0,',
  5346. ' "1":"Green",',
  5347. ' Green:1,',
  5348. ' "2":"Blue",',
  5349. ' Blue:2',
  5350. ' };',
  5351. 'this.vC = 0;',
  5352. 'this.vS = {};',
  5353. 'this.vT = {};',
  5354. 'this.vU = {};',
  5355. 'this.B = false;'
  5356. ]),
  5357. LinesToStr([
  5358. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5359. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5360. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5361. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5362. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5363. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5364. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5365. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5366. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5367. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5368. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5369. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5370. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5371. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5372. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5373. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5374. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5375. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5376. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5377. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5378. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5379. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5380. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5381. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5382. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5383. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5384. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5385. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5386. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5387. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5388. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5389. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5390. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5391. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5392. '']));
  5393. end;
  5394. procedure TTestModule.TestSet_Operator_In;
  5395. begin
  5396. StartProgram(false);
  5397. Add('type');
  5398. Add(' TColor = (Red, Green, Blue);');
  5399. Add(' TColors = set of tcolor;');
  5400. Add('var');
  5401. Add(' vC: tcolor;');
  5402. Add(' vT: tcolors;');
  5403. Add(' B: boolean;');
  5404. Add('begin');
  5405. Add(' b:=red in vt;');
  5406. Add(' b:=vc in vt;');
  5407. Add(' b:=green in [red..blue];');
  5408. Add(' b:=vc in [red..blue];');
  5409. Add(' ');
  5410. Add(' if red in vt then ;');
  5411. Add(' while vC in vt do ;');
  5412. Add(' repeat');
  5413. Add(' until vC in vt;');
  5414. ConvertProgram;
  5415. CheckSource('TestSet_Operator_In',
  5416. LinesToStr([ // statements
  5417. 'this.TColor = {',
  5418. ' "0":"Red",',
  5419. ' Red:0,',
  5420. ' "1":"Green",',
  5421. ' Green:1,',
  5422. ' "2":"Blue",',
  5423. ' Blue:2',
  5424. ' };',
  5425. 'this.vC = 0;',
  5426. 'this.vT = {};',
  5427. 'this.B = false;'
  5428. ]),
  5429. LinesToStr([
  5430. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5431. '$mod.B = $mod.vC in $mod.vT;',
  5432. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5433. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5434. 'if ($mod.TColor.Red in $mod.vT) ;',
  5435. 'while ($mod.vC in $mod.vT) {',
  5436. '};',
  5437. 'do {',
  5438. '} while (!($mod.vC in $mod.vT));',
  5439. '']));
  5440. end;
  5441. procedure TTestModule.TestSet_Functions;
  5442. begin
  5443. StartProgram(false);
  5444. Add('type');
  5445. Add(' TMyEnum = (Red, Green);');
  5446. Add(' TMyEnums = set of TMyEnum;');
  5447. Add('var');
  5448. Add(' e: TMyEnum;');
  5449. Add(' s: TMyEnums;');
  5450. Add('begin');
  5451. Add(' e:=Low(TMyEnums);');
  5452. Add(' e:=Low(s);');
  5453. Add(' e:=High(TMyEnums);');
  5454. Add(' e:=High(s);');
  5455. ConvertProgram;
  5456. CheckSource('TestSetFunctions',
  5457. LinesToStr([ // statements
  5458. 'this.TMyEnum = {',
  5459. ' "0":"Red",',
  5460. ' Red:0,',
  5461. ' "1":"Green",',
  5462. ' Green:1',
  5463. ' };',
  5464. 'this.e = 0;',
  5465. 'this.s = {};'
  5466. ]),
  5467. LinesToStr([
  5468. '$mod.e=$mod.TMyEnum.Red;',
  5469. '$mod.e=$mod.TMyEnum.Red;',
  5470. '$mod.e=$mod.TMyEnum.Green;',
  5471. '$mod.e=$mod.TMyEnum.Green;',
  5472. '']));
  5473. end;
  5474. procedure TTestModule.TestSet_PassAsArgClone;
  5475. begin
  5476. StartProgram(false);
  5477. Add('type');
  5478. Add(' TMyEnum = (Red, Green);');
  5479. Add(' TMyEnums = set of TMyEnum;');
  5480. Add('procedure DoDefault(s: tmyenums); begin end;');
  5481. Add('procedure DoConst(const s: tmyenums); begin end;');
  5482. Add('var');
  5483. Add(' aSet: tmyenums;');
  5484. Add('begin');
  5485. Add(' dodefault(aset);');
  5486. Add(' doconst(aset);');
  5487. ConvertProgram;
  5488. CheckSource('TestSetFunctions',
  5489. LinesToStr([ // statements
  5490. 'this.TMyEnum = {',
  5491. ' "0":"Red",',
  5492. ' Red:0,',
  5493. ' "1":"Green",',
  5494. ' Green:1',
  5495. ' };',
  5496. 'this.DoDefault = function (s) {',
  5497. '};',
  5498. 'this.DoConst = function (s) {',
  5499. '};',
  5500. 'this.aSet = {};'
  5501. ]),
  5502. LinesToStr([
  5503. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5504. '$mod.DoConst($mod.aSet);',
  5505. '']));
  5506. end;
  5507. procedure TTestModule.TestSet_AsParams;
  5508. begin
  5509. StartProgram(false);
  5510. Add([
  5511. 'type TEnum = (Red,Blue);',
  5512. 'type TEnums = set of TEnum;',
  5513. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5514. 'var vJ: TEnums;',
  5515. 'begin',
  5516. ' Include(vg,red);',
  5517. ' Include(result,blue);',
  5518. ' vg:=vg;',
  5519. ' vj:=vh;',
  5520. ' vi:=vi;',
  5521. ' doit(vg,vg,vg);',
  5522. ' doit(vh,vh,vj);',
  5523. ' doit(vi,vi,vi);',
  5524. ' doit(vj,vj,vj);',
  5525. 'end;',
  5526. 'var i: TEnums;',
  5527. 'begin',
  5528. ' doit(i,i,i);']);
  5529. ConvertProgram;
  5530. CheckSource('TestSet_AsParams',
  5531. LinesToStr([ // statements
  5532. 'this.TEnum = {',
  5533. ' "0": "Red",',
  5534. ' Red: 0,',
  5535. ' "1": "Blue",',
  5536. ' Blue: 1',
  5537. '};',
  5538. 'this.DoIt = function (vG,vH,vI) {',
  5539. ' var Result = {};',
  5540. ' var vJ = {};',
  5541. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5542. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5543. ' vG = rtl.refSet(vG);',
  5544. ' vJ = rtl.refSet(vH);',
  5545. ' vI.set(rtl.refSet(vI.get()));',
  5546. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5547. ' get: function () {',
  5548. ' return vG;',
  5549. ' },',
  5550. ' set: function (v) {',
  5551. ' vG = v;',
  5552. ' }',
  5553. ' });',
  5554. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5555. ' get: function () {',
  5556. ' return vJ;',
  5557. ' },',
  5558. ' set: function (v) {',
  5559. ' vJ = v;',
  5560. ' }',
  5561. ' });',
  5562. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5563. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5564. ' get: function () {',
  5565. ' return vJ;',
  5566. ' },',
  5567. ' set: function (v) {',
  5568. ' vJ = v;',
  5569. ' }',
  5570. ' });',
  5571. ' return Result;',
  5572. '};',
  5573. 'this.i = {};'
  5574. ]),
  5575. LinesToStr([
  5576. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5577. ' p: $mod,',
  5578. ' get: function () {',
  5579. ' return this.p.i;',
  5580. ' },',
  5581. ' set: function (v) {',
  5582. ' this.p.i = v;',
  5583. ' }',
  5584. '});'
  5585. ]));
  5586. end;
  5587. procedure TTestModule.TestSet_Property;
  5588. begin
  5589. StartProgram(false);
  5590. Add('type');
  5591. Add(' TEnum = (Red,Blue);');
  5592. Add(' TEnums = set of TEnum;');
  5593. Add(' TObject = class');
  5594. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5595. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5596. Add(' property Colors: TEnums read GetColors write SetColors;');
  5597. Add(' end;');
  5598. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5599. Add('begin end;');
  5600. Add('var Obj: TObject;');
  5601. Add('begin');
  5602. Add(' Include(Obj.Colors,Red);');
  5603. Add(' Exclude(Obj.Colors,Red);');
  5604. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5605. ConvertProgram;
  5606. CheckSource('TestSet_Property',
  5607. LinesToStr([ // statements
  5608. 'this.TEnum = {',
  5609. ' "0": "Red",',
  5610. ' Red: 0,',
  5611. ' "1": "Blue",',
  5612. ' Blue: 1',
  5613. '};',
  5614. 'rtl.createClass($mod, "TObject", null, function () {',
  5615. ' this.$init = function () {',
  5616. ' };',
  5617. ' this.$final = function () {',
  5618. ' };',
  5619. '});',
  5620. 'this.DoIt = function (i, j, k, l) {',
  5621. '};',
  5622. 'this.Obj = null;',
  5623. '']),
  5624. LinesToStr([
  5625. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5626. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5627. '']));
  5628. end;
  5629. procedure TTestModule.TestSet_EnumConst;
  5630. begin
  5631. StartProgram(false);
  5632. Add([
  5633. 'type',
  5634. ' TEnum = (Red,Blue);',
  5635. ' TEnums = set of TEnum;',
  5636. 'const',
  5637. ' Orange = red;',
  5638. 'var',
  5639. ' Enum: tenum;',
  5640. ' Enums: tenums;',
  5641. 'begin',
  5642. ' Include(enums,orange);',
  5643. ' Exclude(enums,orange);',
  5644. ' if orange in enums then;',
  5645. ' if orange in [orange,red] then;']);
  5646. ConvertProgram;
  5647. CheckSource('TestSet_EnumConst',
  5648. LinesToStr([ // statements
  5649. 'this.TEnum = {',
  5650. ' "0": "Red",',
  5651. ' Red: 0,',
  5652. ' "1": "Blue",',
  5653. ' Blue: 1',
  5654. '};',
  5655. 'this.Orange = $mod.TEnum.Red;',
  5656. 'this.Enum = 0;',
  5657. 'this.Enums = {};',
  5658. '']),
  5659. LinesToStr([
  5660. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5661. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5662. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5663. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5664. '']));
  5665. end;
  5666. procedure TTestModule.TestSet_IntConst;
  5667. begin
  5668. StartProgram(false);
  5669. Add([
  5670. 'type',
  5671. ' TEnums = set of Byte;',
  5672. 'const',
  5673. ' Orange = 0;',
  5674. 'var',
  5675. ' Enum: byte;',
  5676. ' Enums: tenums;',
  5677. 'begin',
  5678. ' Enums:=[];',
  5679. ' Enums:=[0];',
  5680. ' Enums:=[1..2];',
  5681. //' Include(enums,orange);',
  5682. //' Exclude(enums,orange);',
  5683. ' if orange in enums then;',
  5684. ' if orange in [orange,1] then;']);
  5685. ConvertProgram;
  5686. CheckSource('TestSet_IntConst',
  5687. LinesToStr([ // statements
  5688. 'this.Orange = 0;',
  5689. 'this.Enum = 0;',
  5690. 'this.Enums = {};',
  5691. '']),
  5692. LinesToStr([
  5693. '$mod.Enums = {};',
  5694. '$mod.Enums = rtl.createSet(0);',
  5695. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5696. 'if (0 in $mod.Enums) ;',
  5697. 'if (0 in rtl.createSet(0, 1)) ;',
  5698. '']));
  5699. end;
  5700. procedure TTestModule.TestSet_AnonymousEnumType;
  5701. begin
  5702. StartProgram(false);
  5703. Add('type');
  5704. Add(' TFlags = set of (red, green);');
  5705. Add('const');
  5706. Add(' favorite = red;');
  5707. Add('var');
  5708. Add(' f: TFlags;');
  5709. Add(' i: longint;');
  5710. Add('begin');
  5711. Add(' Include(f,red);');
  5712. Add(' Include(f,favorite);');
  5713. Add(' i:=ord(red);');
  5714. Add(' i:=ord(favorite);');
  5715. Add(' i:=ord(low(TFlags));');
  5716. Add(' i:=ord(low(f));');
  5717. Add(' i:=ord(low(favorite));');
  5718. Add(' i:=ord(high(TFlags));');
  5719. Add(' i:=ord(high(f));');
  5720. Add(' i:=ord(high(favorite));');
  5721. Add(' f:=[green,favorite];');
  5722. ConvertProgram;
  5723. CheckSource('TestSet_AnonymousEnumType',
  5724. LinesToStr([ // statements
  5725. 'this.TFlags$a = {',
  5726. ' "0": "red",',
  5727. ' red: 0,',
  5728. ' "1": "green",',
  5729. ' green: 1',
  5730. '};',
  5731. 'this.favorite = $mod.TFlags$a.red;',
  5732. 'this.f = {};',
  5733. 'this.i = 0;',
  5734. '']),
  5735. LinesToStr([
  5736. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5737. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5738. '$mod.i = $mod.TFlags$a.red;',
  5739. '$mod.i = $mod.TFlags$a.red;',
  5740. '$mod.i = $mod.TFlags$a.red;',
  5741. '$mod.i = $mod.TFlags$a.red;',
  5742. '$mod.i = $mod.TFlags$a.red;',
  5743. '$mod.i = $mod.TFlags$a.green;',
  5744. '$mod.i = $mod.TFlags$a.green;',
  5745. '$mod.i = $mod.TFlags$a.green;',
  5746. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5747. '']));
  5748. end;
  5749. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5750. begin
  5751. exit;
  5752. StartProgram(false);
  5753. Add([
  5754. 'type',
  5755. ' TAtoZ = ''A''..''Z'';',
  5756. ' TSetOfAZ = set of TAtoZ;',
  5757. 'var',
  5758. ' c: char;',
  5759. ' a: TAtoZ;',
  5760. ' s: TSetOfAZ = [''P'',''A''];',
  5761. ' i: longint;',
  5762. 'begin',
  5763. ' Include(s,''S'');',
  5764. ' Include(s,c);',
  5765. ' Include(s,a);',
  5766. ' c:=low(TAtoZ);',
  5767. ' i:=ord(low(TAtoZ));',
  5768. ' a:=high(TAtoZ);',
  5769. ' a:=high(TSetOfAtoZ);',
  5770. ' s:=[a,c,''M''];',
  5771. '']);
  5772. ConvertProgram;
  5773. CheckSource('TestSet_AnonymousEnumTypeChar',
  5774. LinesToStr([ // statements
  5775. '']),
  5776. LinesToStr([
  5777. '']));
  5778. end;
  5779. procedure TTestModule.TestSet_ConstEnum;
  5780. begin
  5781. StartProgram(false);
  5782. Add([
  5783. 'type',
  5784. ' TEnum = (red,blue,green);',
  5785. ' TEnums = set of TEnum;',
  5786. 'const',
  5787. ' teAny = [low(TEnum)..high(TEnum)];',
  5788. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5789. 'var',
  5790. ' e: TEnum;',
  5791. ' s: TEnums;',
  5792. 'begin',
  5793. ' if blue in teAny then;',
  5794. ' if blue in teAny+[e] then;',
  5795. ' if blue in teAny+teRedBlue then;',
  5796. ' if e in [red,blue] then;',
  5797. ' s:=teAny;',
  5798. ' s:=teAny+[e];',
  5799. ' s:=[e]+teAny;',
  5800. ' s:=teAny+teRedBlue;',
  5801. ' s:=teAny+teRedBlue+[e];',
  5802. '']);
  5803. ConvertProgram;
  5804. CheckSource('TestSet_ConstEnum',
  5805. LinesToStr([ // statements
  5806. 'this.TEnum = {',
  5807. ' "0": "red",',
  5808. ' red: 0,',
  5809. ' "1": "blue",',
  5810. ' blue: 1,',
  5811. ' "2": "green",',
  5812. ' green: 2',
  5813. '};',
  5814. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5815. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5816. 'this.e = 0;',
  5817. 'this.s = {};',
  5818. '']),
  5819. LinesToStr([
  5820. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5821. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5822. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5823. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5824. '$mod.s = rtl.refSet($mod.teAny);',
  5825. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5826. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5827. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5828. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5829. '']));
  5830. end;
  5831. procedure TTestModule.TestSet_ConstChar;
  5832. begin
  5833. StartProgram(false);
  5834. Add([
  5835. 'const',
  5836. ' LowChars = [''a''..''z''];',
  5837. ' Chars = LowChars+[''A''..''Z''];',
  5838. ' sc = [''А'', ''Я''];',
  5839. 'var',
  5840. ' c: char;',
  5841. ' s: string;',
  5842. 'begin',
  5843. ' if c in lowchars then ;',
  5844. ' if ''a'' in lowchars then ;',
  5845. ' if s[1] in lowchars then ;',
  5846. ' if c in chars then ;',
  5847. ' if c in [''a''..''z'',''_''] then ;',
  5848. ' if ''b'' in [''a''..''z'',''_''] then ;',
  5849. ' if ''Я'' in sc then ;',
  5850. ' if 3=ord('' '') then ;',
  5851. '']);
  5852. ConvertProgram;
  5853. CheckSource('TestSet_ConstChar',
  5854. LinesToStr([ // statements
  5855. 'this.LowChars = rtl.createSet(null, 97, 122);',
  5856. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  5857. 'this.sc = rtl.createSet(1040, 1071);',
  5858. 'this.c = "";',
  5859. 'this.s = "";',
  5860. '']),
  5861. LinesToStr([
  5862. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  5863. 'if (97 in $mod.LowChars) ;',
  5864. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  5865. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  5866. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  5867. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  5868. 'if (1071 in $mod.sc) ;',
  5869. 'if (3 === 32) ;',
  5870. '']));
  5871. end;
  5872. procedure TTestModule.TestSet_ConstInt;
  5873. begin
  5874. StartProgram(false);
  5875. Add([
  5876. 'const',
  5877. ' Months = [1..12];',
  5878. ' Mirror = [-12..-1]+Months;',
  5879. 'var',
  5880. ' i: smallint;',
  5881. 'begin',
  5882. ' if 3 in Months then;',
  5883. ' if i in Months+[i] then;',
  5884. ' if i in Months+Mirror then;',
  5885. ' if i in [4..6,8] then;',
  5886. '']);
  5887. ConvertProgram;
  5888. CheckSource('TestSet_ConstInt',
  5889. LinesToStr([ // statements
  5890. 'this.Months = rtl.createSet(null, 1, 12);',
  5891. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  5892. 'this.i = 0;',
  5893. '']),
  5894. LinesToStr([
  5895. 'if (3 in $mod.Months) ;',
  5896. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  5897. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  5898. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  5899. '']));
  5900. end;
  5901. procedure TTestModule.TestSet_InFunction;
  5902. begin
  5903. StartProgram(false);
  5904. Add([
  5905. 'const',
  5906. ' TEnum = 3;',
  5907. ' TSetOfEnum = 4;',
  5908. ' TSetOfAno = 5;',
  5909. 'procedure DoIt;',
  5910. 'type',
  5911. ' TEnum = (red, blue);',
  5912. ' TSetOfEnum = set of TEnum;',
  5913. ' TSetOfAno = set of (up,down);',
  5914. 'var',
  5915. ' e: TEnum;',
  5916. ' se: TSetOfEnum;',
  5917. ' sa: TSetOfAno;',
  5918. 'begin',
  5919. ' se:=[e];',
  5920. ' sa:=[up];',
  5921. 'end;',
  5922. 'begin',
  5923. '']);
  5924. ConvertProgram;
  5925. CheckSource('TestSet_InFunction',
  5926. LinesToStr([ // statements
  5927. 'this.TEnum = 3;',
  5928. 'this.TSetOfEnum = 4;',
  5929. 'this.TSetOfAno = 5;',
  5930. 'var TEnum$1 = {',
  5931. ' "0": "red",',
  5932. ' red: 0,',
  5933. ' "1": "blue",',
  5934. ' blue: 1',
  5935. '};',
  5936. 'var TSetOfAno$a = {',
  5937. ' "0": "up",',
  5938. ' up: 0,',
  5939. ' "1": "down",',
  5940. ' down: 1',
  5941. '};',
  5942. 'this.DoIt = function () {',
  5943. ' var e = 0;',
  5944. ' var se = {};',
  5945. ' var sa = {};',
  5946. ' se = rtl.createSet(e);',
  5947. ' sa = rtl.createSet(TSetOfAno$a.up);',
  5948. '};',
  5949. '']),
  5950. LinesToStr([
  5951. '']));
  5952. end;
  5953. procedure TTestModule.TestSet_ForIn;
  5954. begin
  5955. StartProgram(false);
  5956. Add([
  5957. 'type',
  5958. ' TEnum = (Red, Green, Blue);',
  5959. ' TEnumRg = green..blue;',
  5960. ' TSetOfEnum = set of TEnum;',
  5961. ' TSetOfEnumRg = set of TEnumRg;',
  5962. 'var',
  5963. ' e, e2: TEnum;',
  5964. ' er: TEnum;',
  5965. ' s: TSetOfEnum;',
  5966. 'begin',
  5967. ' for e in TSetOfEnum do ;',
  5968. ' for e in TSetOfEnumRg do ;',
  5969. ' for e in [] do e2:=e;',
  5970. ' for e in [red..green] do e2:=e;',
  5971. ' for e in [green,blue] do e2:=e;',
  5972. ' for e in [red,blue] do e2:=e;',
  5973. ' for e in s do e2:=e;',
  5974. ' for er in TSetOfEnumRg do ;',
  5975. '']);
  5976. ConvertProgram;
  5977. CheckSource('TestSet_ForIn',
  5978. LinesToStr([ // statements
  5979. 'this.TEnum = {',
  5980. ' "0":"Red",',
  5981. ' Red:0,',
  5982. ' "1":"Green",',
  5983. ' Green:1,',
  5984. ' "2":"Blue",',
  5985. ' Blue:2',
  5986. ' };',
  5987. 'this.e = 0;',
  5988. 'this.e2 = 0;',
  5989. 'this.er = 0;',
  5990. 'this.s = {};',
  5991. '']),
  5992. LinesToStr([
  5993. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5994. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5995. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  5996. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  5997. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  5998. 'for (var $l1 in $mod.s){',
  5999. ' $mod.e = +$l1;',
  6000. ' $mod.e2 = $mod.e;',
  6001. '};',
  6002. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6003. '']));
  6004. end;
  6005. procedure TTestModule.TestNestBegin;
  6006. begin
  6007. StartProgram(false);
  6008. Add('begin');
  6009. Add(' begin');
  6010. Add(' begin');
  6011. Add(' end;');
  6012. Add(' begin');
  6013. Add(' if true then ;');
  6014. Add(' end;');
  6015. Add(' end;');
  6016. ConvertProgram;
  6017. CheckSource('TestNestBegin',
  6018. '',
  6019. 'if (true) ;');
  6020. end;
  6021. procedure TTestModule.TestUnitImplVars;
  6022. begin
  6023. StartUnit(false);
  6024. Add('interface');
  6025. Add('implementation');
  6026. Add('var');
  6027. Add(' V1:longint;');
  6028. Add(' V2:longint = 3;');
  6029. Add(' V3:string = ''abc'';');
  6030. ConvertUnit;
  6031. CheckSource('TestUnitImplVars',
  6032. LinesToStr([ // statements
  6033. 'var $impl = $mod.$impl;',
  6034. '']),
  6035. '', // this.$init
  6036. LinesToStr([ // implementation
  6037. '$impl.V1 = 0;',
  6038. '$impl.V2 = 3;',
  6039. '$impl.V3 = "abc";',
  6040. '']) );
  6041. end;
  6042. procedure TTestModule.TestUnitImplConsts;
  6043. begin
  6044. StartUnit(false);
  6045. Add('interface');
  6046. Add('implementation');
  6047. Add('const');
  6048. Add(' v1 = 3;');
  6049. Add(' v2:longint = 4;');
  6050. Add(' v3:string = ''abc'';');
  6051. ConvertUnit;
  6052. CheckSource('TestUnitImplConsts',
  6053. LinesToStr([ // statements
  6054. 'var $impl = $mod.$impl;',
  6055. '']),
  6056. '', // this.$init
  6057. LinesToStr([ // implementation
  6058. '$impl.v1 = 3;',
  6059. '$impl.v2 = 4;',
  6060. '$impl.v3 = "abc";',
  6061. '']) );
  6062. end;
  6063. procedure TTestModule.TestUnitImplRecord;
  6064. begin
  6065. StartUnit(false);
  6066. Add('interface');
  6067. Add('implementation');
  6068. Add('type');
  6069. Add(' TMyRecord = record');
  6070. Add(' i: longint;');
  6071. Add(' end;');
  6072. Add('var aRec: TMyRecord;');
  6073. Add('initialization');
  6074. Add(' arec.i:=3;');
  6075. ConvertUnit;
  6076. CheckSource('TestUnitImplRecord',
  6077. LinesToStr([ // statements
  6078. 'var $impl = $mod.$impl;',
  6079. '']),
  6080. // this.$init
  6081. '$impl.aRec.i = 3;',
  6082. LinesToStr([ // implementation
  6083. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6084. ' this.i = 0;',
  6085. ' this.$eq = function (b) {',
  6086. ' return this.i === b.i;',
  6087. ' };',
  6088. ' this.$assign = function (s) {',
  6089. ' this.i = s.i;',
  6090. ' return this;',
  6091. ' };',
  6092. '});',
  6093. '$impl.aRec = $impl.TMyRecord.$new();',
  6094. '']) );
  6095. end;
  6096. procedure TTestModule.TestRenameJSNameConflict;
  6097. begin
  6098. StartProgram(false);
  6099. Add('var apply: longint;');
  6100. Add('var bind: longint;');
  6101. Add('var call: longint;');
  6102. Add('begin');
  6103. ConvertProgram;
  6104. CheckSource('TestRenameJSNameConflict',
  6105. LinesToStr([ // statements
  6106. 'this.Apply = 0;',
  6107. 'this.Bind = 0;',
  6108. 'this.Call = 0;'
  6109. ]),
  6110. LinesToStr([ // this.$main
  6111. ''
  6112. ]));
  6113. end;
  6114. procedure TTestModule.TestLocalConst;
  6115. begin
  6116. StartProgram(false);
  6117. Add('procedure DoIt;');
  6118. Add('const');
  6119. Add(' cA: longint = 1;');
  6120. Add(' cB = 2;');
  6121. Add(' procedure Sub;');
  6122. Add(' const');
  6123. Add(' csA = 3;');
  6124. Add(' cB: double = 4;');
  6125. Add(' begin');
  6126. Add(' cb:=cb+csa;');
  6127. Add(' ca:=ca+csa+5;');
  6128. Add(' end;');
  6129. Add('begin');
  6130. Add(' ca:=ca+cb+6;');
  6131. Add('end;');
  6132. Add('begin');
  6133. ConvertProgram;
  6134. CheckSource('TestLocalConst',
  6135. LinesToStr([
  6136. 'var cA = 1;',
  6137. 'var cB = 2;',
  6138. 'var csA = 3;',
  6139. 'var cB$1 = 4;',
  6140. 'this.DoIt = function () {',
  6141. ' function Sub() {',
  6142. ' cB$1 = cB$1 + 3;',
  6143. ' cA = cA + 3 + 5;',
  6144. ' };',
  6145. ' cA = cA + 2 + 6;',
  6146. '};'
  6147. ]),
  6148. LinesToStr([
  6149. ]));
  6150. end;
  6151. procedure TTestModule.TestVarExternal;
  6152. begin
  6153. StartProgram(false);
  6154. Add('var');
  6155. Add(' NaN: double; external name ''Global.NaN'';');
  6156. Add(' d: double;');
  6157. Add('begin');
  6158. Add(' d:=NaN;');
  6159. ConvertProgram;
  6160. CheckSource('TestVarExternal',
  6161. LinesToStr([
  6162. 'this.d = 0.0;'
  6163. ]),
  6164. LinesToStr([
  6165. '$mod.d = Global.NaN;'
  6166. ]));
  6167. end;
  6168. procedure TTestModule.TestVarExternalOtherUnit;
  6169. begin
  6170. AddModuleWithIntfImplSrc('unit2.pas',
  6171. LinesToStr([
  6172. 'var NaN: double; external name ''Global.NaN'';',
  6173. 'var iV: longint;'
  6174. ]),
  6175. '');
  6176. StartUnit(true);
  6177. Add('interface');
  6178. Add('uses unit2;');
  6179. Add('implementation');
  6180. Add('var');
  6181. Add(' d: double;');
  6182. Add(' i: longint; external name ''$i'';');
  6183. Add('begin');
  6184. Add(' d:=nan;');
  6185. Add(' d:=uNit2.nan;');
  6186. Add(' d:=test1.d;');
  6187. Add(' i:=iv;');
  6188. Add(' i:=uNit2.iv;');
  6189. Add(' i:=test1.i;');
  6190. ConvertUnit;
  6191. CheckSource('TestVarExternalOtherUnit',
  6192. LinesToStr([
  6193. 'var $impl = $mod.$impl;',
  6194. '']),
  6195. LinesToStr([ // this.$init
  6196. '$impl.d = Global.NaN;',
  6197. '$impl.d = Global.NaN;',
  6198. '$impl.d = $impl.d;',
  6199. '$i = pas.unit2.iV;',
  6200. '$i = pas.unit2.iV;',
  6201. '$i = $i;',
  6202. '']),
  6203. LinesToStr([ // implementation
  6204. '$impl.d = 0.0;',
  6205. '']) );
  6206. end;
  6207. procedure TTestModule.TestVarAbsoluteFail;
  6208. begin
  6209. StartProgram(false);
  6210. Add([
  6211. 'var',
  6212. ' a: longint;',
  6213. ' b: longword absolute a;',
  6214. 'begin']);
  6215. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6216. ConvertProgram;
  6217. end;
  6218. procedure TTestModule.TestConstExternal;
  6219. begin
  6220. StartProgram(false);
  6221. Add([
  6222. 'const',
  6223. ' PI: double; external name ''Global.PI'';',
  6224. ' Tau = 2*pi;',
  6225. 'var d: double;',
  6226. 'begin',
  6227. ' d:=pi;',
  6228. ' d:=tau+pi;']);
  6229. ConvertProgram;
  6230. CheckSource('TestConstExternal',
  6231. LinesToStr([
  6232. 'this.Tau = 2*Global.PI;',
  6233. 'this.d = 0.0;'
  6234. ]),
  6235. LinesToStr([
  6236. '$mod.d = Global.PI;',
  6237. '$mod.d = $mod.Tau + Global.PI;'
  6238. ]));
  6239. end;
  6240. procedure TTestModule.TestDouble;
  6241. begin
  6242. StartProgram(false);
  6243. Add([
  6244. 'type',
  6245. ' TDateTime = double;',
  6246. 'const',
  6247. ' a = TDateTime(2.7);',
  6248. ' b = a + TDateTime(1.7);',
  6249. ' c = 0.9 + 0.1;',
  6250. ' f0_1 = 0.1;',
  6251. ' f0_3 = 0.3;',
  6252. ' fn0_1 = -0.1;',
  6253. ' fn0_3 = -0.3;',
  6254. ' fn0_003 = -0.003;',
  6255. ' fn0_123456789 = -0.123456789;',
  6256. ' fn300_0 = -300.0;',
  6257. ' fn123456_0 = -123456.0;',
  6258. ' fn1234567_8 = -1234567.8;',
  6259. ' fn12345678_9 = -12345678.9;',
  6260. ' f1_0En12 = 1E-12;',
  6261. ' fn1_0En12 = -1E-12;',
  6262. ' maxdouble = 1.7e+308;',
  6263. ' mindouble = -1.7e+308;',
  6264. ' MinSafeIntDouble = -$1fffffffffffff;',
  6265. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6266. ' MaxSafeIntDouble = $1fffffffffffff;',
  6267. ' DZeroResolution = 1E-12;',
  6268. ' Minus1 = -1E-12;',
  6269. ' EPS = 1E-9;',
  6270. ' DELTA = 0.001;',
  6271. ' Big = 129.789E+100;',
  6272. ' Test0_15 = 0.15;',
  6273. ' Test999 = 2.9999999999999;',
  6274. ' Test111999 = 211199999999999000.0;',
  6275. ' TestMinus111999 = -211199999999999000.0;',
  6276. 'var',
  6277. ' d: double = b;',
  6278. 'begin',
  6279. ' d:=1.0;',
  6280. ' d:=1.0/3.0;',
  6281. ' d:=1/3;',
  6282. ' d:=5.0E-324;',
  6283. ' d:=1.7E308;',
  6284. ' d:=001.00E00;',
  6285. ' d:=002.00E001;',
  6286. ' d:=003.000E000;',
  6287. ' d:=-004.00E-00;',
  6288. ' d:=-005.00E-001;',
  6289. ' d:=10**3;',
  6290. ' d:=10 mod 3;',
  6291. ' d:=10 div 3;',
  6292. ' d:=c;',
  6293. ' d:=f0_1;',
  6294. ' d:=f0_3;',
  6295. ' d:=fn0_1;',
  6296. ' d:=fn0_3;',
  6297. ' d:=fn0_003;',
  6298. ' d:=fn0_123456789;',
  6299. ' d:=fn300_0;',
  6300. ' d:=fn123456_0;',
  6301. ' d:=fn1234567_8;',
  6302. ' d:=fn12345678_9;',
  6303. ' d:=f1_0En12;',
  6304. ' d:=fn1_0En12;',
  6305. ' d:=maxdouble;',
  6306. ' d:=mindouble;',
  6307. ' d:=MinSafeIntDouble;',
  6308. ' d:=double(MinSafeIntDouble);',
  6309. ' d:=MinSafeIntDouble2;',
  6310. ' d:=double(MinSafeIntDouble2);',
  6311. ' d:=MaxSafeIntDouble;',
  6312. ' d:=default(double);',
  6313. '']);
  6314. ConvertProgram;
  6315. CheckSource('TestDouble',
  6316. LinesToStr([
  6317. 'this.a = 2.7;',
  6318. 'this.b = 2.7 + 1.7;',
  6319. 'this.c = 0.9 + 0.1;',
  6320. 'this.f0_1 = 0.1;',
  6321. 'this.f0_3 = 0.3;',
  6322. 'this.fn0_1 = -0.1;',
  6323. 'this.fn0_3 = -0.3;',
  6324. 'this.fn0_003 = -0.003;',
  6325. 'this.fn0_123456789 = -0.123456789;',
  6326. 'this.fn300_0 = -300.0;',
  6327. 'this.fn123456_0 = -123456.0;',
  6328. 'this.fn1234567_8 = -1234567.8;',
  6329. 'this.fn12345678_9 = -12345678.9;',
  6330. 'this.f1_0En12 = 1E-12;',
  6331. 'this.fn1_0En12 = -1E-12;',
  6332. 'this.maxdouble = 1.7e+308;',
  6333. 'this.mindouble = -1.7e+308;',
  6334. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6335. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6336. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6337. 'this.DZeroResolution = 1E-12;',
  6338. 'this.Minus1 = -1E-12;',
  6339. 'this.EPS = 1E-9;',
  6340. 'this.DELTA = 0.001;',
  6341. 'this.Big = 129.789E+100;',
  6342. 'this.Test0_15 = 0.15;',
  6343. 'this.Test999 = 2.9999999999999;',
  6344. 'this.Test111999 = 211199999999999000.0;',
  6345. 'this.TestMinus111999 = -211199999999999000.0;',
  6346. 'this.d = 4.4;'
  6347. ]),
  6348. LinesToStr([
  6349. '$mod.d = 1.0;',
  6350. '$mod.d = 1.0 / 3.0;',
  6351. '$mod.d = 1 / 3;',
  6352. '$mod.d = 5.0E-324;',
  6353. '$mod.d = 1.7E308;',
  6354. '$mod.d = 1.00E0;',
  6355. '$mod.d = 2.00E1;',
  6356. '$mod.d = 3.000E0;',
  6357. '$mod.d = -4.00E-0;',
  6358. '$mod.d = -5.00E-1;',
  6359. '$mod.d = Math.pow(10, 3);',
  6360. '$mod.d = 10 % 3;',
  6361. '$mod.d = Math.floor(10 / 3);',
  6362. '$mod.d = 1;',
  6363. '$mod.d = 0.1;',
  6364. '$mod.d = 0.3;',
  6365. '$mod.d = -0.1;',
  6366. '$mod.d = -0.3;',
  6367. '$mod.d = -0.003;',
  6368. '$mod.d = -0.123456789;',
  6369. '$mod.d = -300;',
  6370. '$mod.d = -123456;',
  6371. '$mod.d = -1234567.8;',
  6372. '$mod.d = -1.23456789E7;',
  6373. '$mod.d = 1E-12;',
  6374. '$mod.d = -1E-12;',
  6375. '$mod.d = 1.7E308;',
  6376. '$mod.d = -1.7E308;',
  6377. '$mod.d = -9007199254740991;',
  6378. '$mod.d = -9007199254740991;',
  6379. '$mod.d = -9.007199254740992E15;',
  6380. '$mod.d = -9.007199254740992E15;',
  6381. '$mod.d = 9007199254740991;',
  6382. '$mod.d = 0.0;',
  6383. '']));
  6384. end;
  6385. procedure TTestModule.TestInteger;
  6386. begin
  6387. StartProgram(false);
  6388. Add([
  6389. 'const',
  6390. ' MinInt = low(NativeInt);',
  6391. ' MaxInt = high(NativeInt);',
  6392. 'type',
  6393. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6394. 'const',
  6395. ' a = low(TMyInt)+High(TMyInt);',
  6396. 'var',
  6397. ' i: TMyInt;',
  6398. 'begin',
  6399. ' i:=-MinInt;',
  6400. ' i:=default(TMyInt);',
  6401. ' i:=low(i)+high(i);',
  6402. '']);
  6403. ConvertProgram;
  6404. CheckSource('TestIntegerRange',
  6405. LinesToStr([
  6406. 'this.MinInt = -9007199254740991;',
  6407. 'this.MaxInt = 9007199254740991;',
  6408. 'this.a = -9007199254740991 + 9007199254740991;',
  6409. 'this.i = 0;',
  6410. '']),
  6411. LinesToStr([
  6412. '$mod.i = - -9007199254740991;',
  6413. '$mod.i = -9007199254740991;',
  6414. '$mod.i = -9007199254740991 + 9007199254740991;',
  6415. '']));
  6416. end;
  6417. procedure TTestModule.TestIntegerRange;
  6418. begin
  6419. StartProgram(false);
  6420. Add([
  6421. 'const',
  6422. ' MinInt = -1;',
  6423. ' MaxInt = +1;',
  6424. 'type',
  6425. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6426. ' TInt2 = 1..3;',
  6427. 'const',
  6428. ' a = low(TMyInt)+High(TMyInt);',
  6429. ' b = low(TInt2)+High(TInt2);',
  6430. ' s1 = [1];',
  6431. ' s2 = [1,2];',
  6432. ' s3 = [1..3];',
  6433. ' s4 = [low(shortint)..high(shortint)];',
  6434. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6435. ' s6 = 1 in s2;',
  6436. 'var',
  6437. ' i: TMyInt;',
  6438. ' i2: TInt2;',
  6439. 'begin',
  6440. ' i:=i2;',
  6441. ' i:=default(TMyInt);',
  6442. ' if i=i2 then ;']);
  6443. ConvertProgram;
  6444. CheckSource('TestIntegerRange',
  6445. LinesToStr([
  6446. 'this.MinInt = -1;',
  6447. 'this.MaxInt = +1;',
  6448. 'this.a = -1 + 1;',
  6449. 'this.b = 1 + 3;',
  6450. 'this.s1 = rtl.createSet(1);',
  6451. 'this.s2 = rtl.createSet(1, 2);',
  6452. 'this.s3 = rtl.createSet(null, 1, 3);',
  6453. 'this.s4 = rtl.createSet(null, -128, 127);',
  6454. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6455. 'this.s6 = 1 in $mod.s2;',
  6456. 'this.i = 0;',
  6457. 'this.i2 = 0;',
  6458. '']),
  6459. LinesToStr([
  6460. '$mod.i = $mod.i2;',
  6461. '$mod.i = -1;',
  6462. 'if ($mod.i === $mod.i2) ;',
  6463. '']));
  6464. end;
  6465. procedure TTestModule.TestIntegerTypecasts;
  6466. begin
  6467. StartProgram(false);
  6468. Add([
  6469. 'var',
  6470. ' i: nativeint;',
  6471. ' b: byte;',
  6472. ' sh: shortint;',
  6473. ' w: word;',
  6474. ' sm: smallint;',
  6475. ' lw: longword;',
  6476. ' li: longint;',
  6477. 'begin',
  6478. ' b:=byte(i);',
  6479. ' sh:=shortint(i);',
  6480. ' w:=word(i);',
  6481. ' sm:=smallint(i);',
  6482. ' lw:=longword(i);',
  6483. ' li:=longint(i);',
  6484. '']);
  6485. ConvertProgram;
  6486. CheckSource('TestIntegerTypecasts',
  6487. LinesToStr([
  6488. 'this.i = 0;',
  6489. 'this.b = 0;',
  6490. 'this.sh = 0;',
  6491. 'this.w = 0;',
  6492. 'this.sm = 0;',
  6493. 'this.lw = 0;',
  6494. 'this.li = 0;',
  6495. '']),
  6496. LinesToStr([
  6497. '$mod.b = $mod.i & 255;',
  6498. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6499. '$mod.w = $mod.i & 65535;',
  6500. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6501. '$mod.lw = $mod.i >>> 0;',
  6502. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6503. '']));
  6504. end;
  6505. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6506. begin
  6507. StartProgram(false);
  6508. Add([
  6509. 'var',
  6510. ' i,j: nativeint;',
  6511. 'begin',
  6512. ' i:=i shr 0;',
  6513. ' i:=i shr 1;',
  6514. ' i:=i shr 3;',
  6515. ' i:=i shr 54;',
  6516. ' i:=j shr i;',
  6517. '']);
  6518. ConvertProgram;
  6519. CheckResolverUnexpectedHints;
  6520. CheckSource('TestInteger_BitwiseShrNativeInt',
  6521. LinesToStr([
  6522. 'this.i = 0;',
  6523. 'this.j = 0;',
  6524. '']),
  6525. LinesToStr([
  6526. '$mod.i = $mod.i;',
  6527. '$mod.i = Math.floor($mod.i / 2);',
  6528. '$mod.i = Math.floor($mod.i / 8);',
  6529. '$mod.i = 0;',
  6530. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6531. '']));
  6532. end;
  6533. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6534. begin
  6535. StartProgram(false);
  6536. Add([
  6537. 'var',
  6538. ' i: nativeint;',
  6539. 'begin',
  6540. ' i:=i shl 0;',
  6541. ' i:=i shl 54;',
  6542. ' i:=123456789012 shl 1;',
  6543. ' i:=i shl 1;',
  6544. '']);
  6545. ConvertProgram;
  6546. CheckResolverUnexpectedHints;
  6547. CheckSource('TestInteger_BitwiseShrNativeInt',
  6548. LinesToStr([
  6549. 'this.i = 0;',
  6550. '']),
  6551. LinesToStr([
  6552. '$mod.i = $mod.i;',
  6553. '$mod.i = 0;',
  6554. '$mod.i = 246913578024;',
  6555. '$mod.i = rtl.shl($mod.i, 1);',
  6556. '']));
  6557. end;
  6558. procedure TTestModule.TestCurrency;
  6559. begin
  6560. StartProgram(false);
  6561. Add([
  6562. 'type',
  6563. ' TCoin = currency;',
  6564. 'const',
  6565. ' a = TCoin(2.7);',
  6566. ' b = a + TCoin(1.7);',
  6567. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6568. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6569. 'var',
  6570. ' c: TCoin = b;',
  6571. ' i: nativeint;',
  6572. ' d: double;',
  6573. ' j: jsvalue;',
  6574. 'function DoIt(c: currency): currency; begin end;',
  6575. 'function GetIt(d: double): double; begin end;',
  6576. 'procedure Write(v: jsvalue); begin end;',
  6577. 'begin',
  6578. ' c:=1.0;',
  6579. ' c:=0.1;',
  6580. ' c:=1.0/3.0;',
  6581. ' c:=1/3;',
  6582. ' c:=a;',
  6583. ' d:=c;',
  6584. ' c:=d;',
  6585. ' c:=currency(c);',
  6586. ' c:=currency(d);',
  6587. ' d:=double(c);',
  6588. ' c:=i;',
  6589. ' c:=currency(i);',
  6590. //' i:=c;', not allowed
  6591. ' i:=nativeint(c);',
  6592. ' c:=c+a;',
  6593. ' c:=-c-a;',
  6594. ' c:=d+c;',
  6595. ' c:=c+d;',
  6596. ' c:=d-c;',
  6597. ' c:=c-d;',
  6598. ' c:=c*a;',
  6599. ' c:=a*c;',
  6600. ' c:=d*c;',
  6601. ' c:=c*d;',
  6602. ' c:=c/a;',
  6603. ' c:=a/c;',
  6604. ' c:=d/c;',
  6605. ' c:=c/d;',
  6606. ' c:=c**a;',
  6607. ' c:=a**c;',
  6608. ' c:=d**c;',
  6609. ' c:=c**d;',
  6610. ' if c=c then ;',
  6611. ' if c=a then ;',
  6612. ' if a=c then ;',
  6613. ' if d=c then ;',
  6614. ' if c=d then ;',
  6615. ' c:=DoIt(c);',
  6616. ' c:=DoIt(i);',
  6617. ' c:=DoIt(d);',
  6618. ' c:=GetIt(c);',
  6619. ' j:=c;',
  6620. ' Write(c);',
  6621. ' c:=default(currency);',
  6622. ' j:=str(c);',
  6623. ' j:=str(c:0:3);',
  6624. '']);
  6625. ConvertProgram;
  6626. CheckSource('TestCurrency',
  6627. LinesToStr([
  6628. 'this.a = 27000;',
  6629. 'this.b = $mod.a + 17000;',
  6630. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6631. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6632. 'this.c = $mod.b;',
  6633. 'this.i = 0;',
  6634. 'this.d = 0.0;',
  6635. 'this.j = undefined;',
  6636. 'this.DoIt = function (c) {',
  6637. ' var Result = 0;',
  6638. ' return Result;',
  6639. '};',
  6640. 'this.GetIt = function (d) {',
  6641. ' var Result = 0.0;',
  6642. ' return Result;',
  6643. '};',
  6644. 'this.Write = function (v) {',
  6645. '};',
  6646. '']),
  6647. LinesToStr([
  6648. '$mod.c = 10000;',
  6649. '$mod.c = 1000;',
  6650. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6651. '$mod.c = Math.floor((1 / 3) * 10000);',
  6652. '$mod.c = $mod.a;',
  6653. '$mod.d = $mod.c / 10000;',
  6654. '$mod.c = Math.floor($mod.d * 10000);',
  6655. '$mod.c = $mod.c;',
  6656. '$mod.c = $mod.d * 10000;',
  6657. '$mod.d = $mod.c / 10000;',
  6658. '$mod.c = $mod.i * 10000;',
  6659. '$mod.c = $mod.i * 10000;',
  6660. '$mod.i = Math.floor($mod.c / 10000);',
  6661. '$mod.c = $mod.c + $mod.a;',
  6662. '$mod.c = -$mod.c - $mod.a;',
  6663. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6664. '$mod.c = $mod.c + ($mod.d * 10000);',
  6665. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6666. '$mod.c = $mod.c - ($mod.d * 10000);',
  6667. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6668. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6669. '$mod.c = $mod.d * $mod.c;',
  6670. '$mod.c = $mod.c * $mod.d;',
  6671. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6672. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6673. '$mod.c = Math.floor($mod.d / $mod.c);',
  6674. '$mod.c = Math.floor($mod.c / $mod.d);',
  6675. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6676. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6677. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6678. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6679. 'if ($mod.c === $mod.c) ;',
  6680. 'if ($mod.c === $mod.a) ;',
  6681. 'if ($mod.a === $mod.c) ;',
  6682. 'if (($mod.d * 10000) === $mod.c) ;',
  6683. 'if ($mod.c === ($mod.d * 10000)) ;',
  6684. '$mod.c = $mod.DoIt($mod.c);',
  6685. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6686. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6687. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6688. '$mod.j = $mod.c / 10000;',
  6689. '$mod.Write($mod.c / 10000);',
  6690. '$mod.c = 0;',
  6691. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6692. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6693. '']));
  6694. end;
  6695. procedure TTestModule.TestForBoolDo;
  6696. begin
  6697. StartProgram(false);
  6698. Add([
  6699. 'var b: boolean;',
  6700. 'begin',
  6701. ' for b:=false to true do ;',
  6702. ' for b:=b downto false do ;',
  6703. ' for b in boolean do ;',
  6704. '']);
  6705. ConvertProgram;
  6706. CheckSource('TestForBoolDo',
  6707. LinesToStr([ // statements
  6708. 'this.b = false;']),
  6709. LinesToStr([ // this.$main
  6710. 'for (var $l1 = 0; $l1 <= 1; $l1++) $mod.b = $l1 !== 0;',
  6711. 'for (var $l2 = +$mod.b; $l2 >= 0; $l2--) $mod.b = $l2 !== 0;',
  6712. 'for (var $l3 = 0; $l3 <= 1; $l3++) $mod.b = $l3 !== 0;',
  6713. '']));
  6714. end;
  6715. procedure TTestModule.TestForIntDo;
  6716. begin
  6717. StartProgram(false);
  6718. Add([
  6719. 'var i: longint;',
  6720. 'begin',
  6721. ' for i:=3 to 5 do ;',
  6722. ' for i:=i downto 2 do ;',
  6723. ' for i in byte do ;',
  6724. '']);
  6725. ConvertProgram;
  6726. CheckSource('TestForIntDo',
  6727. LinesToStr([ // statements
  6728. 'this.i = 0;']),
  6729. LinesToStr([ // this.$main
  6730. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6731. 'for (var $l1 = $mod.i; $l1 >= 2; $l1--) $mod.i = $l1;',
  6732. 'for (var $l2 = 0; $l2 <= 255; $l2++) $mod.i = $l2;',
  6733. '']));
  6734. end;
  6735. procedure TTestModule.TestForIntInDo;
  6736. begin
  6737. StartProgram(false);
  6738. Add([
  6739. 'type',
  6740. ' TSetOfInt = set of byte;',
  6741. ' TIntRg = 3..7;',
  6742. ' TSetOfIntRg = set of TIntRg;',
  6743. 'var',
  6744. ' i,i2: longint;',
  6745. ' a1: array of byte;',
  6746. ' a2: array[1..3] of byte;',
  6747. ' soi: TSetOfInt;',
  6748. ' soir: TSetOfIntRg;',
  6749. ' ir: TIntRg;',
  6750. 'begin',
  6751. ' for i in byte do ;',
  6752. ' for i in a1 do ;',
  6753. ' for i in a2 do ;',
  6754. ' for i in [11..13] do ;',
  6755. ' for i in TSetOfInt do ;',
  6756. ' for i in TIntRg do ;',
  6757. ' for i in soi do i2:=i;',
  6758. ' for i in TSetOfIntRg do ;',
  6759. ' for i in soir do ;',
  6760. ' for ir in TIntRg do ;',
  6761. ' for ir in TSetOfIntRg do ;',
  6762. ' for ir in soir do ;',
  6763. '']);
  6764. ConvertProgram;
  6765. CheckSource('TestForIntInDo',
  6766. LinesToStr([ // statements
  6767. 'this.i = 0;',
  6768. 'this.i2 = 0;',
  6769. 'this.a1 = [];',
  6770. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6771. 'this.soi = {};',
  6772. 'this.soir = {};',
  6773. 'this.ir = 0;',
  6774. '']),
  6775. LinesToStr([ // this.$main
  6776. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6777. 'for (var $in2 = $mod.a1, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) $mod.i = $in2[$l3];',
  6778. 'for (var $in5 = $mod.a2, $l6 = 0, $end7 = rtl.length($in5) - 1; $l6 <= $end7; $l6++) $mod.i = $in5[$l6];',
  6779. 'for (var $l8 = 11; $l8 <= 13; $l8++) $mod.i = $l8;',
  6780. 'for (var $l9 = 0; $l9 <= 255; $l9++) $mod.i = $l9;',
  6781. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.i = $l10;',
  6782. 'for (var $l11 in $mod.soi) {',
  6783. ' $mod.i = +$l11;',
  6784. ' $mod.i2 = $mod.i;',
  6785. '};',
  6786. 'for (var $l12 = 3; $l12 <= 7; $l12++) $mod.i = $l12;',
  6787. 'for (var $l13 in $mod.soir) $mod.i = +$l13;',
  6788. 'for (var $l14 = 3; $l14 <= 7; $l14++) $mod.ir = $l14;',
  6789. 'for (var $l15 = 3; $l15 <= 7; $l15++) $mod.ir = $l15;',
  6790. 'for (var $l16 in $mod.soir) $mod.ir = +$l16;',
  6791. '']));
  6792. end;
  6793. procedure TTestModule.TestCharConst;
  6794. begin
  6795. StartProgram(false);
  6796. Add([
  6797. 'const',
  6798. ' a = #$00F3;',
  6799. ' c: char = ''1'';',
  6800. 'begin',
  6801. ' c:=#0;',
  6802. ' c:=#1;',
  6803. ' c:=#9;',
  6804. ' c:=#10;',
  6805. ' c:=#13;',
  6806. ' c:=#31;',
  6807. ' c:=#32;',
  6808. ' c:=#$A;',
  6809. ' c:=#$0A;',
  6810. ' c:=#$b;',
  6811. ' c:=#$0b;',
  6812. ' c:=^A;',
  6813. ' c:=''"'';',
  6814. ' c:=default(char);',
  6815. ' c:=#$00E4;', // ä
  6816. ' c:=''ä'';',
  6817. ' c:=#$E4;', // ä
  6818. ' c:=#$D800;', // invalid UTF-16
  6819. ' c:=#$DFFF;', // invalid UTF-16
  6820. ' c:=#$FFFF;', // last UCS-2
  6821. ' c:=high(c);', // last UCS-2
  6822. '']);
  6823. ConvertProgram;
  6824. CheckSource('TestCharConst',
  6825. LinesToStr([
  6826. 'this.a="ó";',
  6827. 'this.c="1";'
  6828. ]),
  6829. LinesToStr([
  6830. '$mod.c="\x00";',
  6831. '$mod.c="\x01";',
  6832. '$mod.c="\t";',
  6833. '$mod.c="\n";',
  6834. '$mod.c="\r";',
  6835. '$mod.c="\x1F";',
  6836. '$mod.c=" ";',
  6837. '$mod.c="\n";',
  6838. '$mod.c="\n";',
  6839. '$mod.c="\x0B";',
  6840. '$mod.c="\x0B";',
  6841. '$mod.c="\x01";',
  6842. '$mod.c=''"'';',
  6843. '$mod.c="\x00";',
  6844. '$mod.c = "ä";',
  6845. '$mod.c = "ä";',
  6846. '$mod.c = "ä";',
  6847. '$mod.c="\uD800";',
  6848. '$mod.c="\uDFFF";',
  6849. '$mod.c="\uFFFF";',
  6850. '$mod.c="\uFFFF";',
  6851. '']));
  6852. end;
  6853. procedure TTestModule.TestChar_Compare;
  6854. begin
  6855. StartProgram(false);
  6856. Add('var');
  6857. Add(' c: char;');
  6858. Add(' b: boolean;');
  6859. Add('begin');
  6860. Add(' b:=c=''1'';');
  6861. Add(' b:=''2''=c;');
  6862. Add(' b:=''3''=''4'';');
  6863. Add(' b:=c<>''5'';');
  6864. Add(' b:=''6''<>c;');
  6865. Add(' b:=c>''7'';');
  6866. Add(' b:=''8''>c;');
  6867. Add(' b:=c>=''9'';');
  6868. Add(' b:=''A''>=c;');
  6869. Add(' b:=c<''B'';');
  6870. Add(' b:=''C''<c;');
  6871. Add(' b:=c<=''D'';');
  6872. Add(' b:=''E''<=c;');
  6873. ConvertProgram;
  6874. CheckSource('TestChar_Compare',
  6875. LinesToStr([
  6876. 'this.c="";',
  6877. 'this.b = false;'
  6878. ]),
  6879. LinesToStr([
  6880. '$mod.b = $mod.c === "1";',
  6881. '$mod.b = "2" === $mod.c;',
  6882. '$mod.b = "3" === "4";',
  6883. '$mod.b = $mod.c !== "5";',
  6884. '$mod.b = "6" !== $mod.c;',
  6885. '$mod.b = $mod.c > "7";',
  6886. '$mod.b = "8" > $mod.c;',
  6887. '$mod.b = $mod.c >= "9";',
  6888. '$mod.b = "A" >= $mod.c;',
  6889. '$mod.b = $mod.c < "B";',
  6890. '$mod.b = "C" < $mod.c;',
  6891. '$mod.b = $mod.c <= "D";',
  6892. '$mod.b = "E" <= $mod.c;',
  6893. '']));
  6894. end;
  6895. procedure TTestModule.TestChar_BuiltInProcs;
  6896. begin
  6897. StartProgram(false);
  6898. Add([
  6899. 'var',
  6900. ' c: char;',
  6901. ' i: longint;',
  6902. ' s: string;',
  6903. 'begin',
  6904. ' i:=ord(c);',
  6905. ' i:=ord(s[i]);',
  6906. ' c:=chr(i);',
  6907. ' c:=pred(c);',
  6908. ' c:=succ(c);',
  6909. ' c:=low(c);',
  6910. ' c:=high(c);',
  6911. ' i:=byte(c);',
  6912. ' i:=word(c);',
  6913. ' i:=longint(c);',
  6914. '']);
  6915. ConvertProgram;
  6916. CheckSource('TestChar_BuiltInProcs',
  6917. LinesToStr([
  6918. 'this.c = "";',
  6919. 'this.i = 0;',
  6920. 'this.s = "";'
  6921. ]),
  6922. LinesToStr([
  6923. '$mod.i = $mod.c.charCodeAt();',
  6924. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  6925. '$mod.c = String.fromCharCode($mod.i);',
  6926. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  6927. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  6928. '$mod.c = "\x00";',
  6929. '$mod.c = "\uFFFF";',
  6930. '$mod.i = $mod.c.charCodeAt() & 255;',
  6931. '$mod.i = $mod.c.charCodeAt();',
  6932. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  6933. '']));
  6934. end;
  6935. procedure TTestModule.TestStringConst;
  6936. begin
  6937. StartProgram(false);
  6938. Add([
  6939. '{$H+}',
  6940. 'const',
  6941. ' a = #$00F3#$017C;', // first <256, then >=256
  6942. ' b = string(''a'');',
  6943. ' c = string(''ä'');',
  6944. ' d = UnicodeString(''b'');',
  6945. ' e = UnicodeString(''ö'');',
  6946. 'var',
  6947. ' s: string = ''abc'';',
  6948. 'begin',
  6949. ' s:='''';',
  6950. ' s:=#13#10;',
  6951. ' s:=#9''foo'';',
  6952. ' s:=#$A9;',
  6953. ' s:=''foo''#13''bar'';',
  6954. ' s:=''"'';',
  6955. ' s:=''"''''"'';',
  6956. ' s:=#$20AC;', // euro
  6957. ' s:=#$10437;', // outside BMP
  6958. ' s:=default(string);',
  6959. ' s:=concat(s);',
  6960. ' s:=concat(s,''a'',s)',
  6961. '']);
  6962. ConvertProgram;
  6963. CheckSource('TestStringConst',
  6964. LinesToStr([
  6965. 'this.a = "óż";',
  6966. 'this.b = "a";',
  6967. 'this.c = "ä";',
  6968. 'this.d = "b";',
  6969. 'this.e = "ö";',
  6970. 'this.s="abc";',
  6971. '']),
  6972. LinesToStr([
  6973. '$mod.s="";',
  6974. '$mod.s="\r\n";',
  6975. '$mod.s="\tfoo";',
  6976. '$mod.s="©";',
  6977. '$mod.s="foo\rbar";',
  6978. '$mod.s=''"'';',
  6979. '$mod.s=''"\''"'';',
  6980. '$mod.s="€";',
  6981. '$mod.s="'#$F0#$90#$90#$B7'";',
  6982. '$mod.s="";',
  6983. '$mod.s = $mod.s;',
  6984. '$mod.s = $mod.s.concat("a", $mod.s);',
  6985. '']));
  6986. end;
  6987. procedure TTestModule.TestStringConstSurrogate;
  6988. begin
  6989. StartProgram(false);
  6990. Add([
  6991. 'var',
  6992. ' s: string;',
  6993. 'begin',
  6994. ' s:=''😊'';', // 1F60A
  6995. '']);
  6996. ConvertProgram;
  6997. CheckSource('TestStringConstSurrogate',
  6998. LinesToStr([
  6999. 'this.s="";'
  7000. ]),
  7001. LinesToStr([
  7002. '$mod.s="😊";'
  7003. ]));
  7004. end;
  7005. procedure TTestModule.TestString_Length;
  7006. begin
  7007. StartProgram(false);
  7008. Add('const c = ''foo'';');
  7009. Add('var');
  7010. Add(' s: string;');
  7011. Add(' i: longint;');
  7012. Add('begin');
  7013. Add(' i:=length(s);');
  7014. Add(' i:=length(s+s);');
  7015. Add(' i:=length(''abc'');');
  7016. Add(' i:=length(c);');
  7017. ConvertProgram;
  7018. CheckSource('TestString_Length',
  7019. LinesToStr([
  7020. 'this.c = "foo";',
  7021. 'this.s = "";',
  7022. 'this.i = 0;',
  7023. '']),
  7024. LinesToStr([
  7025. '$mod.i = $mod.s.length;',
  7026. '$mod.i = ($mod.s+$mod.s).length;',
  7027. '$mod.i = "abc".length;',
  7028. '$mod.i = $mod.c.length;',
  7029. '']));
  7030. end;
  7031. procedure TTestModule.TestString_Compare;
  7032. begin
  7033. StartProgram(false);
  7034. Add('var');
  7035. Add(' s, t: string;');
  7036. Add(' b: boolean;');
  7037. Add('begin');
  7038. Add(' b:=s=t;');
  7039. Add(' b:=s<>t;');
  7040. Add(' b:=s>t;');
  7041. Add(' b:=s>=t;');
  7042. Add(' b:=s<t;');
  7043. Add(' b:=s<=t;');
  7044. ConvertProgram;
  7045. CheckSource('TestString_Compare',
  7046. LinesToStr([ // statements
  7047. 'this.s = "";',
  7048. 'this.t = "";',
  7049. 'this.b =false;'
  7050. ]),
  7051. LinesToStr([ // this.$main
  7052. '$mod.b = $mod.s === $mod.t;',
  7053. '$mod.b = $mod.s !== $mod.t;',
  7054. '$mod.b = $mod.s > $mod.t;',
  7055. '$mod.b = $mod.s >= $mod.t;',
  7056. '$mod.b = $mod.s < $mod.t;',
  7057. '$mod.b = $mod.s <= $mod.t;',
  7058. '']));
  7059. end;
  7060. procedure TTestModule.TestString_SetLength;
  7061. begin
  7062. StartProgram(false);
  7063. Add([
  7064. 'procedure DoIt(var s: string);',
  7065. 'begin',
  7066. ' SetLength(s,2);',
  7067. 'end;',
  7068. 'var s: string;',
  7069. 'begin',
  7070. ' SetLength(s,3);',
  7071. '']);
  7072. ConvertProgram;
  7073. CheckSource('TestString_SetLength',
  7074. LinesToStr([ // statements
  7075. 'this.DoIt = function (s) {',
  7076. ' s.set(rtl.strSetLength(s.get(), 2));',
  7077. '};',
  7078. 'this.s = "";',
  7079. '']),
  7080. LinesToStr([ // this.$main
  7081. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7082. ]));
  7083. end;
  7084. procedure TTestModule.TestString_CharAt;
  7085. begin
  7086. StartProgram(false);
  7087. Add([
  7088. 'var',
  7089. ' s: string;',
  7090. ' c: char;',
  7091. ' b: boolean;',
  7092. 'begin',
  7093. ' b:= s[1] = c;',
  7094. ' b:= c = s[1];',
  7095. ' b:= c <> s[1];',
  7096. ' b:= c > s[1];',
  7097. ' b:= c >= s[1];',
  7098. ' b:= c < s[2];',
  7099. ' b:= c <= s[1];',
  7100. ' s[1] := c;',
  7101. ' s[2+3] := c;']);
  7102. ConvertProgram;
  7103. CheckSource('TestString_CharAt',
  7104. LinesToStr([ // statements
  7105. 'this.s = "";',
  7106. 'this.c = "";',
  7107. 'this.b = false;'
  7108. ]),
  7109. LinesToStr([ // this.$main
  7110. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7111. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7112. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7113. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7114. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7115. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7116. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7117. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7118. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7119. '']));
  7120. end;
  7121. procedure TTestModule.TestStringHMinusFail;
  7122. begin
  7123. StartProgram(false);
  7124. Add([
  7125. '{$H-}',
  7126. 'var s: string;',
  7127. 'begin']);
  7128. ConvertProgram;
  7129. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7130. end;
  7131. procedure TTestModule.TestStr;
  7132. begin
  7133. StartProgram(false);
  7134. Add('var');
  7135. Add(' b: boolean;');
  7136. Add(' i: longint;');
  7137. Add(' d: double;');
  7138. Add(' s: string;');
  7139. Add('begin');
  7140. Add(' str(b,s);');
  7141. Add(' str(i,s);');
  7142. Add(' str(d,s);');
  7143. Add(' str(i:3,s);');
  7144. Add(' str(d:3:2,s);');
  7145. Add(' Str(12.456:12:1,s);');
  7146. Add(' Str(12.456:12,s);');
  7147. Add(' s:=str(b);');
  7148. Add(' s:=str(i);');
  7149. Add(' s:=str(d);');
  7150. Add(' s:=str(i,i);');
  7151. Add(' s:=str(i:3);');
  7152. Add(' s:=str(d:3:2);');
  7153. Add(' s:=str(i:4,i);');
  7154. Add(' s:=str(i,i:5);');
  7155. Add(' s:=str(i:4,i:5);');
  7156. Add(' s:=str(s,s);');
  7157. Add(' s:=str(s,''foo'');');
  7158. ConvertProgram;
  7159. CheckSource('TestStr',
  7160. LinesToStr([ // statements
  7161. 'this.b = false;',
  7162. 'this.i = 0;',
  7163. 'this.d = 0.0;',
  7164. 'this.s = "";',
  7165. '']),
  7166. LinesToStr([ // this.$main
  7167. '$mod.s = ""+$mod.b;',
  7168. '$mod.s = ""+$mod.i;',
  7169. '$mod.s = rtl.floatToStr($mod.d);',
  7170. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7171. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7172. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7173. '$mod.s = rtl.floatToStr(12.456,12);',
  7174. '$mod.s = ""+$mod.b;',
  7175. '$mod.s = ""+$mod.i;',
  7176. '$mod.s = rtl.floatToStr($mod.d);',
  7177. '$mod.s = ""+$mod.i+$mod.i;',
  7178. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7179. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7180. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7181. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7182. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7183. '$mod.s = $mod.s + $mod.s;',
  7184. '$mod.s = $mod.s + "foo";',
  7185. '']));
  7186. end;
  7187. procedure TTestModule.TestBaseType_AnsiStringFail;
  7188. begin
  7189. StartProgram(false);
  7190. Add('var s: AnsiString');
  7191. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7192. ConvertProgram;
  7193. end;
  7194. procedure TTestModule.TestBaseType_WideStringFail;
  7195. begin
  7196. StartProgram(false);
  7197. Add('var s: WideString');
  7198. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7199. ConvertProgram;
  7200. end;
  7201. procedure TTestModule.TestBaseType_ShortStringFail;
  7202. begin
  7203. StartProgram(false);
  7204. Add('var s: ShortString');
  7205. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7206. ConvertProgram;
  7207. end;
  7208. procedure TTestModule.TestBaseType_RawByteStringFail;
  7209. begin
  7210. StartProgram(false);
  7211. Add('var s: RawByteString');
  7212. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7213. ConvertProgram;
  7214. end;
  7215. procedure TTestModule.TestTypeShortstring_Fail;
  7216. begin
  7217. StartProgram(false);
  7218. Add('type t = string[12];');
  7219. Add('var s: t;');
  7220. Add('begin');
  7221. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7222. ConvertProgram;
  7223. end;
  7224. procedure TTestModule.TestCharSet_Custom;
  7225. begin
  7226. StartProgram(false);
  7227. Add([
  7228. 'type',
  7229. ' TCharRg = ''a''..''z'';',
  7230. ' TSetOfCharRg = set of TCharRg;',
  7231. ' TCharRg2 = ''m''..''p'';',
  7232. 'const',
  7233. ' crg: TCharRg = ''b'';',
  7234. 'var',
  7235. ' c: char;',
  7236. ' crg2: TCharRg2;',
  7237. ' s: TSetOfCharRg;',
  7238. 'begin',
  7239. ' c:=crg;',
  7240. ' crg:=c;',
  7241. ' crg2:=crg;',
  7242. ' if c=crg then ;',
  7243. ' if crg=c then ;',
  7244. ' if crg=crg2 then ;',
  7245. ' if c in s then ;',
  7246. ' if crg2 in s then ;',
  7247. ' c:=default(TCharRg);',
  7248. '']);
  7249. ConvertProgram;
  7250. CheckSource('TestCharSet_Custom',
  7251. LinesToStr([ // statements
  7252. 'this.crg = "b";',
  7253. 'this.c = "";',
  7254. 'this.crg2 = "m";',
  7255. 'this.s = {};',
  7256. '']),
  7257. LinesToStr([ // this.$main
  7258. '$mod.c = $mod.crg;',
  7259. '$mod.crg = $mod.c;',
  7260. '$mod.crg2 = $mod.crg;',
  7261. 'if ($mod.c === $mod.crg) ;',
  7262. 'if ($mod.crg === $mod.c) ;',
  7263. 'if ($mod.crg === $mod.crg2) ;',
  7264. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7265. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7266. '$mod.c = "a";',
  7267. '']));
  7268. end;
  7269. procedure TTestModule.TestForCharDo;
  7270. begin
  7271. StartProgram(false);
  7272. Add([
  7273. 'var c: char;',
  7274. 'begin',
  7275. ' for c:=''a'' to ''c'' do ;',
  7276. ' for c:=c downto ''a'' do ;',
  7277. ' for c:=''Б'' to ''Я'' do ;',
  7278. '']);
  7279. ConvertProgram;
  7280. CheckSource('TestForCharDo',
  7281. LinesToStr([ // statements
  7282. 'this.c = "";']),
  7283. LinesToStr([ // this.$main
  7284. 'for (var $l1 = 97; $l1 <= 99; $l1++) $mod.c = String.fromCharCode($l1);',
  7285. 'for (var $l2 = $mod.c.charCodeAt(); $l2 >= 97; $l2--) $mod.c = String.fromCharCode($l2);',
  7286. 'for (var $l3 = 1041; $l3 <= 1071; $l3++) $mod.c = String.fromCharCode($l3);',
  7287. '']));
  7288. end;
  7289. procedure TTestModule.TestForCharInDo;
  7290. begin
  7291. StartProgram(false);
  7292. Add([
  7293. 'type',
  7294. ' TSetOfChar = set of char;',
  7295. ' TCharRg = ''a''..''z'';',
  7296. ' TSetOfCharRg = set of TCharRg;',
  7297. 'const Foo = ''foo'';',
  7298. 'var',
  7299. ' c,c2: char;',
  7300. ' s: string;',
  7301. ' a1: array of char;',
  7302. ' a2: array[1..3] of char;',
  7303. ' soc: TSetOfChar;',
  7304. ' socr: TSetOfCharRg;',
  7305. ' cr: TCharRg;',
  7306. 'begin',
  7307. ' for c in foo do ;',
  7308. ' for c in s do ;',
  7309. ' for c in char do ;',
  7310. ' for c in a1 do ;',
  7311. ' for c in a2 do ;',
  7312. ' for c in [''1''..''3''] do ;',
  7313. ' for c in TSetOfChar do ;',
  7314. ' for c in TCharRg do ;',
  7315. ' for c in soc do c2:=c;',
  7316. ' for c in TSetOfCharRg do ;',
  7317. ' for c in socr do ;',
  7318. ' for cr in TCharRg do ;',
  7319. ' for cr in TSetOfCharRg do ;',
  7320. ' for cr in socr do ;',
  7321. '']);
  7322. ConvertProgram;
  7323. CheckSource('TestForCharInDo',
  7324. LinesToStr([ // statements
  7325. 'this.Foo = "foo";',
  7326. 'this.c = "";',
  7327. 'this.c2 = "";',
  7328. 'this.s = "";',
  7329. 'this.a1 = [];',
  7330. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7331. 'this.soc = {};',
  7332. 'this.socr = {};',
  7333. 'this.cr = "a";',
  7334. '']),
  7335. LinesToStr([ // this.$main
  7336. 'for (var $in1 = $mod.Foo, $l2 = 0, $end3 = $in1.length - 1; $l2 <= $end3; $l2++) $mod.c = $in1.charAt($l2);',
  7337. 'for (var $in4 = $mod.s, $l5 = 0, $end6 = $in4.length - 1; $l5 <= $end6; $l5++) $mod.c = $in4.charAt($l5);',
  7338. 'for (var $l7 = 0; $l7 <= 65535; $l7++) $mod.c = String.fromCharCode($l7);',
  7339. 'for (var $in8 = $mod.a1, $l9 = 0, $end10 = rtl.length($in8) - 1; $l9 <= $end10; $l9++) $mod.c = $in8[$l9];',
  7340. 'for (var $in11 = $mod.a2, $l12 = 0, $end13 = rtl.length($in11) - 1; $l12 <= $end13; $l12++) $mod.c = $in11[$l12];',
  7341. 'for (var $l14 = 49; $l14 <= 51; $l14++) $mod.c = String.fromCharCode($l14);',
  7342. 'for (var $l15 = 0; $l15 <= 65535; $l15++) $mod.c = String.fromCharCode($l15);',
  7343. 'for (var $l16 = 97; $l16 <= 122; $l16++) $mod.c = String.fromCharCode($l16);',
  7344. 'for (var $l17 in $mod.soc) {',
  7345. ' $mod.c = String.fromCharCode($l17);',
  7346. ' $mod.c2 = $mod.c;',
  7347. '};',
  7348. 'for (var $l18 = 97; $l18 <= 122; $l18++) $mod.c = String.fromCharCode($l18);',
  7349. 'for (var $l19 in $mod.socr) $mod.c = String.fromCharCode($l19);',
  7350. 'for (var $l20 = 97; $l20 <= 122; $l20++) $mod.cr = String.fromCharCode($l20);',
  7351. 'for (var $l21 = 97; $l21 <= 122; $l21++) $mod.cr = String.fromCharCode($l21);',
  7352. 'for (var $l22 in $mod.socr) $mod.cr = String.fromCharCode($l22);',
  7353. '']));
  7354. end;
  7355. procedure TTestModule.TestProcTwoArgs;
  7356. begin
  7357. StartProgram(false);
  7358. Add('procedure Test(a,b: longint);');
  7359. Add('begin');
  7360. Add('end;');
  7361. Add('begin');
  7362. ConvertProgram;
  7363. CheckSource('TestProcTwoArgs',
  7364. LinesToStr([ // statements
  7365. 'this.Test = function (a,b) {',
  7366. '};'
  7367. ]),
  7368. LinesToStr([ // this.$main
  7369. ''
  7370. ]));
  7371. end;
  7372. procedure TTestModule.TestProc_DefaultValue;
  7373. begin
  7374. StartProgram(false);
  7375. Add('procedure p1(i: longint = 1);');
  7376. Add('begin');
  7377. Add('end;');
  7378. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7379. Add('begin');
  7380. Add('end;');
  7381. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7382. Add('begin');
  7383. Add('end;');
  7384. Add('begin');
  7385. Add(' p1;');
  7386. Add(' p1();');
  7387. Add(' p1(11);');
  7388. Add(' p2;');
  7389. Add(' p2();');
  7390. Add(' p2(12);');
  7391. Add(' p2(13,''b'');');
  7392. Add(' p3();');
  7393. ConvertProgram;
  7394. CheckSource('TestProc_DefaultValue',
  7395. LinesToStr([ // statements
  7396. 'this.p1 = function (i) {',
  7397. '};',
  7398. 'this.p2 = function (i,c) {',
  7399. '};',
  7400. 'this.p3 = function (d,b,s) {',
  7401. '};'
  7402. ]),
  7403. LinesToStr([ // this.$main
  7404. ' $mod.p1(1);',
  7405. ' $mod.p1(1);',
  7406. ' $mod.p1(11);',
  7407. ' $mod.p2(1,"a");',
  7408. ' $mod.p2(1,"a");',
  7409. ' $mod.p2(12,"a");',
  7410. ' $mod.p2(13,"b");',
  7411. ' $mod.p3(1.0,false,"abc");'
  7412. ]));
  7413. end;
  7414. procedure TTestModule.TestFunctionInt;
  7415. begin
  7416. StartProgram(false);
  7417. Add('function MyTest(Bar: longint): longint;');
  7418. Add('begin');
  7419. Add(' Result:=2*bar');
  7420. Add('end;');
  7421. Add('begin');
  7422. ConvertProgram;
  7423. CheckSource('TestFunctionInt',
  7424. LinesToStr([ // statements
  7425. 'this.MyTest = function (Bar) {',
  7426. ' var Result = 0;',
  7427. ' Result = 2*Bar;',
  7428. ' return Result;',
  7429. '};'
  7430. ]),
  7431. LinesToStr([ // this.$main
  7432. ''
  7433. ]));
  7434. end;
  7435. procedure TTestModule.TestFunctionString;
  7436. begin
  7437. StartProgram(false);
  7438. Add('function Test(Bar: string): string;');
  7439. Add('begin');
  7440. Add(' Result:=bar+BAR');
  7441. Add('end;');
  7442. Add('begin');
  7443. ConvertProgram;
  7444. CheckSource('TestFunctionString',
  7445. LinesToStr([ // statements
  7446. 'this.Test = function (Bar) {',
  7447. ' var Result = "";',
  7448. ' Result = Bar+Bar;',
  7449. ' return Result;',
  7450. '};'
  7451. ]),
  7452. LinesToStr([ // this.$main
  7453. ''
  7454. ]));
  7455. end;
  7456. procedure TTestModule.TestIfThen;
  7457. begin
  7458. StartProgram(false);
  7459. Add([
  7460. 'var b: boolean;',
  7461. 'begin',
  7462. ' if b then ;',
  7463. ' if b then else ;']);
  7464. ConvertProgram;
  7465. CheckSource('TestIfThen',
  7466. LinesToStr([ // statements
  7467. 'this.b = false;',
  7468. '']),
  7469. LinesToStr([ // this.$main
  7470. 'if ($mod.b) ;',
  7471. 'if ($mod.b) ;',
  7472. '']));
  7473. end;
  7474. procedure TTestModule.TestForLoop;
  7475. begin
  7476. StartProgram(false);
  7477. Add('var');
  7478. Add(' vI, vJ, vN: longint;');
  7479. Add('begin');
  7480. Add(' VJ:=0;');
  7481. Add(' VN:=3;');
  7482. Add(' for VI:=1 to VN do');
  7483. Add(' begin');
  7484. Add(' VJ:=VJ+VI;');
  7485. Add(' end;');
  7486. ConvertProgram;
  7487. CheckSource('TestForLoop',
  7488. LinesToStr([ // statements
  7489. 'this.vI = 0;',
  7490. 'this.vJ = 0;',
  7491. 'this.vN = 0;'
  7492. ]),
  7493. LinesToStr([ // this.$main
  7494. ' $mod.vJ = 0;',
  7495. ' $mod.vN = 3;',
  7496. ' for (var $l1 = 1, $end2 = $mod.vN; $l1 <= $end2; $l1++) {',
  7497. ' $mod.vI = $l1;',
  7498. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7499. ' };',
  7500. '']));
  7501. end;
  7502. procedure TTestModule.TestForLoopInsideFunction;
  7503. begin
  7504. StartProgram(false);
  7505. Add('function SumNumbers(Count: longint): longint;');
  7506. Add('var');
  7507. Add(' vI, vJ: longint;');
  7508. Add('begin');
  7509. Add(' vj:=0;');
  7510. Add(' for vi:=1 to count do');
  7511. Add(' begin');
  7512. Add(' vj:=vj+vi;');
  7513. Add(' end;');
  7514. Add('end;');
  7515. Add('begin');
  7516. Add(' sumnumbers(3);');
  7517. ConvertProgram;
  7518. CheckSource('TestForLoopInsideFunction',
  7519. LinesToStr([ // statements
  7520. 'this.SumNumbers = function (Count) {',
  7521. ' var Result = 0;',
  7522. ' var vI = 0;',
  7523. ' var vJ = 0;',
  7524. ' vJ = 0;',
  7525. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7526. ' vI = $l1;',
  7527. ' vJ = vJ + vI;',
  7528. ' };',
  7529. ' return Result;',
  7530. '};'
  7531. ]),
  7532. LinesToStr([ // $mod.$main
  7533. ' $mod.SumNumbers(3);'
  7534. ]));
  7535. end;
  7536. procedure TTestModule.TestForLoop_ReadVarAfter;
  7537. begin
  7538. StartProgram(false);
  7539. Add('var');
  7540. Add(' vI: longint;');
  7541. Add('begin');
  7542. Add(' for vi:=1 to 2 do ;');
  7543. Add(' if vi=3 then ;');
  7544. ConvertProgram;
  7545. CheckSource('TestForLoop',
  7546. LinesToStr([ // statements
  7547. 'this.vI = 0;'
  7548. ]),
  7549. LinesToStr([ // this.$main
  7550. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7551. ' if ($mod.vI===3) ;'
  7552. ]));
  7553. end;
  7554. procedure TTestModule.TestForLoop_Nested;
  7555. begin
  7556. StartProgram(false);
  7557. Add('function SumNumbers(Count: longint): longint;');
  7558. Add('var');
  7559. Add(' vI, vJ, vK: longint;');
  7560. Add('begin');
  7561. Add(' VK:=0;');
  7562. Add(' for VI:=1 to count do');
  7563. Add(' begin');
  7564. Add(' for vj:=1 to vi do');
  7565. Add(' begin');
  7566. Add(' vk:=VK+VI;');
  7567. Add(' end;');
  7568. Add(' end;');
  7569. Add('end;');
  7570. Add('begin');
  7571. Add(' sumnumbers(3);');
  7572. ConvertProgram;
  7573. CheckSource('TestForLoopInFunction',
  7574. LinesToStr([ // statements
  7575. 'this.SumNumbers = function (Count) {',
  7576. ' var Result = 0;',
  7577. ' var vI = 0;',
  7578. ' var vJ = 0;',
  7579. ' var vK = 0;',
  7580. ' vK = 0;',
  7581. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7582. ' vI = $l1;',
  7583. ' for (var $l3 = 1, $end4 = vI; $l3 <= $end4; $l3++) {',
  7584. ' vJ = $l3;',
  7585. ' vK = vK + vI;',
  7586. ' };',
  7587. ' };',
  7588. ' return Result;',
  7589. '};'
  7590. ]),
  7591. LinesToStr([ // $mod.$main
  7592. ' $mod.SumNumbers(3);'
  7593. ]));
  7594. end;
  7595. procedure TTestModule.TestRepeatUntil;
  7596. begin
  7597. StartProgram(false);
  7598. Add('var');
  7599. Add(' vI, vJ, vN: longint;');
  7600. Add('begin');
  7601. Add(' vn:=3;');
  7602. Add(' vj:=0;');
  7603. Add(' VI:=0;');
  7604. Add(' repeat');
  7605. Add(' VI:=vi+1;');
  7606. Add(' vj:=VJ+vI;');
  7607. Add(' until vi>=vn');
  7608. ConvertProgram;
  7609. CheckSource('TestRepeatUntil',
  7610. LinesToStr([ // statements
  7611. 'this.vI = 0;',
  7612. 'this.vJ = 0;',
  7613. 'this.vN = 0;'
  7614. ]),
  7615. LinesToStr([ // $mod.$main
  7616. ' $mod.vN = 3;',
  7617. ' $mod.vJ = 0;',
  7618. ' $mod.vI = 0;',
  7619. ' do{',
  7620. ' $mod.vI = $mod.vI + 1;',
  7621. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7622. ' }while(!($mod.vI>=$mod.vN));'
  7623. ]));
  7624. end;
  7625. procedure TTestModule.TestAsmBlock;
  7626. begin
  7627. StartProgram(false);
  7628. Add([
  7629. 'var',
  7630. ' vI: longint;',
  7631. 'begin',
  7632. ' vi:=1;',
  7633. ' asm',
  7634. ' if (vI===1) {',
  7635. ' vI=2;',
  7636. //' console.log(''end;'');', ToDo
  7637. ' }',
  7638. ' if (vI===2){ vI=3; }',
  7639. ' end;',
  7640. ' VI:=4;']);
  7641. ConvertProgram;
  7642. CheckSource('TestAsmBlock',
  7643. LinesToStr([ // statements
  7644. 'this.vI = 0;'
  7645. ]),
  7646. LinesToStr([ // $mod.$main
  7647. '$mod.vI = 1;',
  7648. 'if (vI===1) {',
  7649. ' vI=2;',
  7650. '}',
  7651. 'if (vI===2){ vI=3; }',
  7652. ';',
  7653. '$mod.vI = 4;'
  7654. ]));
  7655. end;
  7656. procedure TTestModule.TestAsmPas_Impl;
  7657. begin
  7658. StartUnit(false);
  7659. Add('interface');
  7660. Add('const cIntf: longint = 1;');
  7661. Add('var vIntf: longint;');
  7662. Add('implementation');
  7663. Add('const cImpl: longint = 2;');
  7664. Add('var vImpl: longint;');
  7665. Add('procedure DoIt;');
  7666. Add('const cLoc: longint = 3;');
  7667. Add('var vLoc: longint;');
  7668. Add('begin;');
  7669. Add(' asm');
  7670. //Add(' pas(vIntf)=pas(cIntf);');
  7671. //Add(' pas(vImpl)=pas(cImpl);');
  7672. //Add(' pas(vLoc)=pas(cLoc);');
  7673. Add(' end;');
  7674. Add('end;');
  7675. ConvertUnit;
  7676. CheckSource('TestAsmPas_Impl',
  7677. LinesToStr([
  7678. 'var $impl = $mod.$impl;',
  7679. 'this.cIntf = 1;',
  7680. 'this.vIntf = 0;',
  7681. '']),
  7682. '', // this.$init
  7683. LinesToStr([ // implementation
  7684. '$impl.cImpl = 2;',
  7685. '$impl.vImpl = 0;',
  7686. 'var cLoc = 3;',
  7687. '$impl.DoIt = function () {',
  7688. ' var vLoc = 0;',
  7689. '};',
  7690. '']) );
  7691. end;
  7692. procedure TTestModule.TestTryFinally;
  7693. begin
  7694. StartProgram(false);
  7695. Add('var i: longint;');
  7696. Add('begin');
  7697. Add(' try');
  7698. Add(' i:=0; i:=2 div i;');
  7699. Add(' finally');
  7700. Add(' i:=3');
  7701. Add(' end;');
  7702. ConvertProgram;
  7703. CheckSource('TestTryFinally',
  7704. LinesToStr([ // statements
  7705. 'this.i = 0;'
  7706. ]),
  7707. LinesToStr([ // $mod.$main
  7708. 'try {',
  7709. ' $mod.i = 0;',
  7710. ' $mod.i = Math.floor(2 / $mod.i);',
  7711. '} finally {',
  7712. ' $mod.i = 3;',
  7713. '};'
  7714. ]));
  7715. end;
  7716. procedure TTestModule.TestTryExcept;
  7717. begin
  7718. StartProgram(false);
  7719. Add([
  7720. 'type',
  7721. ' TObject = class end;',
  7722. ' Exception = class Msg: string; end;',
  7723. ' EInvalidCast = class(Exception) end;',
  7724. 'var vI: longint;',
  7725. 'begin',
  7726. ' try',
  7727. ' vi:=1;',
  7728. ' except',
  7729. ' vi:=2',
  7730. ' end;',
  7731. ' try',
  7732. ' vi:=3;',
  7733. ' except',
  7734. ' raise;',
  7735. ' end;',
  7736. ' try',
  7737. ' VI:=4;',
  7738. ' except',
  7739. ' on einvalidcast do',
  7740. ' raise;',
  7741. ' on E: exception do',
  7742. ' if e.msg='''' then',
  7743. ' raise e;',
  7744. ' else',
  7745. ' vi:=5',
  7746. ' end;',
  7747. ' try',
  7748. ' VI:=6;',
  7749. ' except',
  7750. ' on einvalidcast do ;',
  7751. ' end;',
  7752. '']);
  7753. ConvertProgram;
  7754. CheckSource('TestTryExcept',
  7755. LinesToStr([ // statements
  7756. 'rtl.createClass($mod, "TObject", null, function () {',
  7757. ' this.$init = function () {',
  7758. ' };',
  7759. ' this.$final = function () {',
  7760. ' };',
  7761. '});',
  7762. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7763. ' this.$init = function () {',
  7764. ' $mod.TObject.$init.call(this);',
  7765. ' this.Msg = "";',
  7766. ' };',
  7767. '});',
  7768. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7769. '});',
  7770. 'this.vI = 0;'
  7771. ]),
  7772. LinesToStr([ // $mod.$main
  7773. 'try {',
  7774. ' $mod.vI = 1;',
  7775. '} catch ($e) {',
  7776. ' $mod.vI = 2;',
  7777. '};',
  7778. 'try {',
  7779. ' $mod.vI = 3;',
  7780. '} catch ($e) {',
  7781. ' throw $e;',
  7782. '};',
  7783. 'try {',
  7784. ' $mod.vI = 4;',
  7785. '} catch ($e) {',
  7786. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7787. ' throw $e',
  7788. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7789. ' var E = $e;',
  7790. ' if (E.Msg === "") throw E;',
  7791. ' } else {',
  7792. ' $mod.vI = 5;',
  7793. ' }',
  7794. '};',
  7795. 'try {',
  7796. ' $mod.vI = 6;',
  7797. '} catch ($e) {',
  7798. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7799. ' } else throw $e',
  7800. '};',
  7801. '']));
  7802. end;
  7803. procedure TTestModule.TestTryExcept_ReservedWords;
  7804. begin
  7805. StartProgram(false);
  7806. Add([
  7807. 'type',
  7808. ' TObject = class end;',
  7809. ' Exception = class',
  7810. ' Symbol: string;',
  7811. ' end;',
  7812. 'var &try: longint;',
  7813. 'begin',
  7814. ' try',
  7815. ' &try:=4;',
  7816. ' except',
  7817. ' on Error: exception do',
  7818. ' if errOR.symBol='''' then',
  7819. ' raise ERRor;',
  7820. ' end;',
  7821. '']);
  7822. ConvertProgram;
  7823. CheckSource('TestTryExcept_ReservedWords',
  7824. LinesToStr([ // statements
  7825. 'rtl.createClass($mod, "TObject", null, function () {',
  7826. ' this.$init = function () {',
  7827. ' };',
  7828. ' this.$final = function () {',
  7829. ' };',
  7830. '});',
  7831. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7832. ' this.$init = function () {',
  7833. ' $mod.TObject.$init.call(this);',
  7834. ' this.Symbol = "";',
  7835. ' };',
  7836. '});',
  7837. 'this.Try = 0;',
  7838. '']),
  7839. LinesToStr([ // $mod.$main
  7840. 'try {',
  7841. ' $mod.Try = 4;',
  7842. '} catch ($e) {',
  7843. ' if ($mod.Exception.isPrototypeOf($e)) {',
  7844. ' var error = $e;',
  7845. ' if (error.Symbol === "") throw error;',
  7846. ' } else throw $e',
  7847. '};',
  7848. '']));
  7849. end;
  7850. procedure TTestModule.TestIfThenRaiseElse;
  7851. begin
  7852. StartProgram(false);
  7853. Add([
  7854. 'type',
  7855. ' TObject = class',
  7856. ' constructor Create;',
  7857. ' end;',
  7858. 'constructor TObject.Create;',
  7859. 'begin',
  7860. 'end;',
  7861. 'var b: boolean;',
  7862. 'begin',
  7863. ' if b then',
  7864. ' raise TObject.Create',
  7865. ' else',
  7866. ' b:=false;',
  7867. '']);
  7868. ConvertProgram;
  7869. CheckSource('TestIfThenRaiseElse',
  7870. LinesToStr([ // statements
  7871. 'rtl.createClass($mod, "TObject", null, function () {',
  7872. ' this.$init = function () {',
  7873. ' };',
  7874. ' this.$final = function () {',
  7875. ' };',
  7876. ' this.Create = function () {',
  7877. ' return this;',
  7878. ' };',
  7879. '});',
  7880. 'this.b = false;',
  7881. '']),
  7882. LinesToStr([ // $mod.$main
  7883. 'if ($mod.b) {',
  7884. ' throw $mod.TObject.$create("Create")}',
  7885. ' else $mod.b = false;',
  7886. '']));
  7887. end;
  7888. procedure TTestModule.TestCaseOf;
  7889. begin
  7890. StartProgram(false);
  7891. Add([
  7892. 'const e: longint; external name ''$e'';',
  7893. 'var vI: longint;',
  7894. 'begin',
  7895. ' case vi of',
  7896. ' 1: ;',
  7897. ' 2: vi:=3;',
  7898. ' e: ;',
  7899. ' else',
  7900. ' VI:=4',
  7901. ' end;']);
  7902. ConvertProgram;
  7903. CheckSource('TestCaseOf',
  7904. LinesToStr([ // statements
  7905. 'this.vI = 0;'
  7906. ]),
  7907. LinesToStr([ // $mod.$main
  7908. 'var $tmp1 = $mod.vI;',
  7909. 'if ($tmp1 === 1) {}',
  7910. 'else if ($tmp1 === 2) {',
  7911. ' $mod.vI = 3}',
  7912. ' else if ($tmp1 === $e) {}',
  7913. 'else {',
  7914. ' $mod.vI = 4;',
  7915. '};'
  7916. ]));
  7917. end;
  7918. procedure TTestModule.TestCaseOf_UseSwitch;
  7919. begin
  7920. StartProgram(false);
  7921. Converter.UseSwitchStatement:=true;
  7922. Add('var Vi: longint;');
  7923. Add('begin');
  7924. Add(' case vi of');
  7925. Add(' 1: ;');
  7926. Add(' 2: VI:=3;');
  7927. Add(' else');
  7928. Add(' vi:=4');
  7929. Add(' end;');
  7930. ConvertProgram;
  7931. CheckSource('TestCaseOf_UseSwitch',
  7932. LinesToStr([ // statements
  7933. 'this.Vi = 0;'
  7934. ]),
  7935. LinesToStr([ // $mod.$main
  7936. 'switch ($mod.Vi) {',
  7937. 'case 1:',
  7938. ' break;',
  7939. 'case 2:',
  7940. ' $mod.Vi = 3;',
  7941. ' break;',
  7942. 'default:',
  7943. ' $mod.Vi = 4;',
  7944. '};'
  7945. ]));
  7946. end;
  7947. procedure TTestModule.TestCaseOfNoElse;
  7948. begin
  7949. StartProgram(false);
  7950. Add('var Vi: longint;');
  7951. Add('begin');
  7952. Add(' case vi of');
  7953. Add(' 1: begin vi:=2; VI:=3; end;');
  7954. Add(' end;');
  7955. ConvertProgram;
  7956. CheckSource('TestCaseOfNoElse',
  7957. LinesToStr([ // statements
  7958. 'this.Vi = 0;'
  7959. ]),
  7960. LinesToStr([ // $mod.$main
  7961. 'var $tmp1 = $mod.Vi;',
  7962. 'if ($tmp1 === 1) {',
  7963. ' $mod.Vi = 2;',
  7964. ' $mod.Vi = 3;',
  7965. '};'
  7966. ]));
  7967. end;
  7968. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  7969. begin
  7970. StartProgram(false);
  7971. Converter.UseSwitchStatement:=true;
  7972. Add('var vI: longint;');
  7973. Add('begin');
  7974. Add(' case vi of');
  7975. Add(' 1: begin VI:=2; vi:=3; end;');
  7976. Add(' end;');
  7977. ConvertProgram;
  7978. CheckSource('TestCaseOfNoElse_UseSwitch',
  7979. LinesToStr([ // statements
  7980. 'this.vI = 0;'
  7981. ]),
  7982. LinesToStr([ // $mod.$main
  7983. 'switch ($mod.vI) {',
  7984. 'case 1:',
  7985. ' $mod.vI = 2;',
  7986. ' $mod.vI = 3;',
  7987. ' break;',
  7988. '};'
  7989. ]));
  7990. end;
  7991. procedure TTestModule.TestCaseOfRange;
  7992. begin
  7993. StartProgram(false);
  7994. Add('var vI: longint;');
  7995. Add('begin');
  7996. Add(' case vi of');
  7997. Add(' 1..3: vi:=14;');
  7998. Add(' 4,5: vi:=16;');
  7999. Add(' 6..7,9..10: ;');
  8000. Add(' else ;');
  8001. Add(' end;');
  8002. ConvertProgram;
  8003. CheckSource('TestCaseOfRange',
  8004. LinesToStr([ // statements
  8005. 'this.vI = 0;'
  8006. ]),
  8007. LinesToStr([ // $mod.$main
  8008. 'var $tmp1 = $mod.vI;',
  8009. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  8010. ' $mod.vI = 14',
  8011. '} else if (($tmp1 === 4) || ($tmp1 === 5)){',
  8012. ' $mod.vI = 16',
  8013. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  8014. ]));
  8015. end;
  8016. procedure TTestModule.TestCaseOfString;
  8017. begin
  8018. StartProgram(false);
  8019. Add([
  8020. 'var s,h: string;',
  8021. 'begin',
  8022. ' case s of',
  8023. ' ''foo'': s:=h;',
  8024. ' ''a''..''z'': h:=s;',
  8025. ' ''ў'', ''ё'': ;',
  8026. ' ''Б''..''Я'': ;',
  8027. ' end;',
  8028. '']);
  8029. ConvertProgram;
  8030. CheckSource('TestCaseOfString',
  8031. LinesToStr([ // statements
  8032. 'this.s = "";',
  8033. 'this.h = "";',
  8034. '']),
  8035. LinesToStr([ // $mod.$main
  8036. 'var $tmp1 = $mod.s;',
  8037. 'if ($tmp1 === "foo") {',
  8038. ' $mod.s = $mod.h}',
  8039. ' else if (($tmp1.length === 1) && ($tmp1 >= "a") && ($tmp1 <= "z")) {',
  8040. ' $mod.h = $mod.s}',
  8041. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  8042. ' else if (($tmp1.length === 1) && ($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  8043. '']));
  8044. end;
  8045. procedure TTestModule.TestCaseOfChar;
  8046. begin
  8047. StartProgram(false);
  8048. Add([
  8049. 'var s,h: char;',
  8050. 'begin',
  8051. ' case s of',
  8052. ' ''a''..''z'': h:=s;',
  8053. ' ''ä'': ;',
  8054. ' ''ў'', ''ё'': ;',
  8055. ' ''Б''..''Я'': ;',
  8056. ' end;',
  8057. '']);
  8058. ConvertProgram;
  8059. CheckSource('TestCaseOfString',
  8060. LinesToStr([ // statements
  8061. 'this.s = "";',
  8062. 'this.h = "";',
  8063. '']),
  8064. LinesToStr([ // $mod.$main
  8065. 'var $tmp1 = $mod.s;',
  8066. 'if (($tmp1 >= "a") && ($tmp1 <= "z")) {',
  8067. ' $mod.h = $mod.s}',
  8068. ' else if ($tmp1 === "ä") {}',
  8069. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  8070. ' else if (($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  8071. '']));
  8072. end;
  8073. procedure TTestModule.TestCaseOfExternalClassConst;
  8074. begin
  8075. StartProgram(false);
  8076. Add([
  8077. '{$modeswitch externalclass}',
  8078. 'type',
  8079. ' TBird = class external name ''Bird''',
  8080. ' const e: longint;',
  8081. ' end;',
  8082. 'var vI: longint;',
  8083. 'begin',
  8084. ' case vi of',
  8085. ' 1: vi:=3;',
  8086. ' TBird.e: ;',
  8087. ' end;']);
  8088. ConvertProgram;
  8089. CheckSource('TestCaseOfExternalClassConst',
  8090. LinesToStr([ // statements
  8091. 'this.vI = 0;'
  8092. ]),
  8093. LinesToStr([ // $mod.$main
  8094. 'var $tmp1 = $mod.vI;',
  8095. 'if ($tmp1 === 1) {',
  8096. ' $mod.vI = 3}',
  8097. ' else if ($tmp1 === Bird.e) ;'
  8098. ]));
  8099. end;
  8100. procedure TTestModule.TestDebugger;
  8101. begin
  8102. StartProgram(false);
  8103. Add([
  8104. 'procedure DoIt;',
  8105. 'begin',
  8106. ' deBugger;',
  8107. ' DeBugger();',
  8108. 'end;',
  8109. 'begin',
  8110. ' Debugger;']);
  8111. ConvertProgram;
  8112. CheckSource('TestDebugger',
  8113. LinesToStr([ // statements
  8114. 'this.DoIt = function () {',
  8115. ' debugger;',
  8116. ' debugger;',
  8117. '};',
  8118. '']),
  8119. LinesToStr([ // $mod.$main
  8120. 'debugger;',
  8121. '']));
  8122. end;
  8123. procedure TTestModule.TestArray_Dynamic;
  8124. begin
  8125. StartProgram(false);
  8126. Add([
  8127. 'type',
  8128. ' TArrayInt = array of longint;',
  8129. 'var',
  8130. ' Arr: TArrayInt;',
  8131. ' i: longint;',
  8132. ' b: boolean;',
  8133. 'begin',
  8134. ' SetLength(arr,3);',
  8135. ' arr[0]:=4;',
  8136. ' arr[1]:=length(arr)+arr[0];',
  8137. ' arr[i]:=5;',
  8138. ' arr[arr[i]]:=arr[6];',
  8139. ' i:=low(arr);',
  8140. ' i:=high(arr);',
  8141. ' b:=Assigned(arr);',
  8142. ' Arr:=default(TArrayInt);']);
  8143. ConvertProgram;
  8144. CheckSource('TestArray_Dynamic',
  8145. LinesToStr([ // statements
  8146. 'this.Arr = [];',
  8147. 'this.i = 0;',
  8148. 'this.b = false;'
  8149. ]),
  8150. LinesToStr([ // $mod.$main
  8151. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8152. '$mod.Arr[0] = 4;',
  8153. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8154. '$mod.Arr[$mod.i] = 5;',
  8155. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8156. '$mod.i = 0;',
  8157. '$mod.i = rtl.length($mod.Arr) - 1;',
  8158. '$mod.b = rtl.length($mod.Arr) > 0;',
  8159. '$mod.Arr = [];',
  8160. '']));
  8161. end;
  8162. procedure TTestModule.TestArray_Dynamic_Nil;
  8163. begin
  8164. StartProgram(false);
  8165. Add('type');
  8166. Add(' TArrayInt = array of longint;');
  8167. Add('var');
  8168. Add(' Arr: TArrayInt;');
  8169. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8170. Add('begin');
  8171. Add(' arr:=nil;');
  8172. Add(' if arr=nil then;');
  8173. Add(' if nil=arr then;');
  8174. Add(' if arr<>nil then;');
  8175. Add(' if nil<>arr then;');
  8176. Add(' DoIt(nil,nil);');
  8177. ConvertProgram;
  8178. CheckSource('TestArray_Dynamic',
  8179. LinesToStr([ // statements
  8180. 'this.Arr = [];',
  8181. 'this.DoIt = function(i,j){',
  8182. '};'
  8183. ]),
  8184. LinesToStr([ // $mod.$main
  8185. '$mod.Arr = [];',
  8186. 'if (rtl.length($mod.Arr) === 0) ;',
  8187. 'if (rtl.length($mod.Arr) === 0) ;',
  8188. 'if (rtl.length($mod.Arr) > 0) ;',
  8189. 'if (rtl.length($mod.Arr) > 0) ;',
  8190. '$mod.DoIt([],[]);',
  8191. '']));
  8192. end;
  8193. procedure TTestModule.TestArray_DynMultiDimensional;
  8194. begin
  8195. StartProgram(false);
  8196. Add('type');
  8197. Add(' TArrayInt = array of longint;');
  8198. Add(' TArrayArrayInt = array of TArrayInt;');
  8199. Add('var');
  8200. Add(' Arr: TArrayInt;');
  8201. Add(' Arr2: TArrayArrayInt;');
  8202. Add(' i: longint;');
  8203. Add('begin');
  8204. Add(' arr2:=nil;');
  8205. Add(' if arr2=nil then;');
  8206. Add(' if nil=arr2 then;');
  8207. Add(' i:=low(arr2);');
  8208. Add(' i:=low(arr2[1]);');
  8209. Add(' i:=high(arr2);');
  8210. Add(' i:=high(arr2[2]);');
  8211. Add(' arr2[3]:=arr;');
  8212. Add(' arr2[4][5]:=i;');
  8213. Add(' i:=arr2[6][7];');
  8214. Add(' arr2[8,9]:=i;');
  8215. Add(' i:=arr2[10,11];');
  8216. Add(' SetLength(arr2,14);');
  8217. Add(' SetLength(arr2[15],16);');
  8218. ConvertProgram;
  8219. CheckSource('TestArray_Dynamic',
  8220. LinesToStr([ // statements
  8221. 'this.Arr = [];',
  8222. 'this.Arr2 = [];',
  8223. 'this.i = 0;'
  8224. ]),
  8225. LinesToStr([ // $mod.$main
  8226. '$mod.Arr2 = [];',
  8227. 'if (rtl.length($mod.Arr2) === 0) ;',
  8228. 'if (rtl.length($mod.Arr2) === 0) ;',
  8229. '$mod.i = 0;',
  8230. '$mod.i = 0;',
  8231. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8232. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8233. '$mod.Arr2[3] = $mod.Arr;',
  8234. '$mod.Arr2[4][5] = $mod.i;',
  8235. '$mod.i = $mod.Arr2[6][7];',
  8236. '$mod.Arr2[8][9] = $mod.i;',
  8237. '$mod.i = $mod.Arr2[10][11];',
  8238. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8239. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8240. '']));
  8241. end;
  8242. procedure TTestModule.TestArray_StaticInt;
  8243. begin
  8244. StartProgram(false);
  8245. Add('type');
  8246. Add(' TArrayInt = array[2..4] of longint;');
  8247. Add('var');
  8248. Add(' Arr: TArrayInt;');
  8249. Add(' Arr2: TArrayInt = (5,6,7);');
  8250. Add(' i: longint;');
  8251. Add(' b: boolean;');
  8252. Add('begin');
  8253. Add(' arr[2]:=4;');
  8254. Add(' arr[3]:=arr[2]+arr[3];');
  8255. Add(' arr[i]:=5;');
  8256. Add(' arr[arr[i]]:=arr[high(arr)];');
  8257. Add(' i:=low(arr);');
  8258. Add(' i:=high(arr);');
  8259. Add(' b:=arr[2]=arr[3];');
  8260. Add(' arr:=default(TArrayInt);');
  8261. ConvertProgram;
  8262. CheckSource('TestArray_StaticInt',
  8263. LinesToStr([ // statements
  8264. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8265. 'this.Arr2 = [5, 6, 7];',
  8266. 'this.i = 0;',
  8267. 'this.b = false;'
  8268. ]),
  8269. LinesToStr([ // $mod.$main
  8270. '$mod.Arr[0] = 4;',
  8271. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8272. '$mod.Arr[$mod.i-2] = 5;',
  8273. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8274. '$mod.i = 2;',
  8275. '$mod.i = 4;',
  8276. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8277. '$mod.Arr = rtl.arraySetLength(null,0,3).slice(0);',
  8278. '']));
  8279. end;
  8280. procedure TTestModule.TestArray_StaticBool;
  8281. begin
  8282. StartProgram(false);
  8283. Add('type');
  8284. Add(' TBools = array[boolean] of boolean;');
  8285. Add(' TBool2 = array[true..true] of boolean;');
  8286. Add('var');
  8287. Add(' Arr: TBools;');
  8288. Add(' Arr2: TBool2;');
  8289. Add(' Arr3: TBools = (true,false);');
  8290. Add(' b: boolean;');
  8291. Add('begin');
  8292. Add(' b:=low(arr);');
  8293. Add(' b:=high(arr);');
  8294. Add(' arr[true]:=false;');
  8295. Add(' arr[false]:=arr[b] or arr[true];');
  8296. Add(' arr[b]:=true;');
  8297. Add(' arr[arr[b]]:=arr[high(arr)];');
  8298. Add(' b:=arr[false]=arr[true];');
  8299. Add(' b:=low(arr2);');
  8300. Add(' b:=high(arr2);');
  8301. Add(' arr2[true]:=true;');
  8302. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8303. Add(' arr2[b]:=false;');
  8304. ConvertProgram;
  8305. CheckSource('TestArray_StaticBool',
  8306. LinesToStr([ // statements
  8307. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8308. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8309. 'this.Arr3 = [true, false];',
  8310. 'this.b = false;'
  8311. ]),
  8312. LinesToStr([ // $mod.$main
  8313. '$mod.b = false;',
  8314. '$mod.b = true;',
  8315. '$mod.Arr[1] = false;',
  8316. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8317. '$mod.Arr[+$mod.b] = true;',
  8318. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8319. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8320. '$mod.b = true;',
  8321. '$mod.b = true;',
  8322. '$mod.Arr2[0] = true;',
  8323. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8324. '$mod.Arr2[1-$mod.b] = false;',
  8325. '']));
  8326. end;
  8327. procedure TTestModule.TestArray_StaticChar;
  8328. begin
  8329. StartProgram(false);
  8330. Add([
  8331. 'type',
  8332. ' TChars = array[char] of char;',
  8333. ' TChars2 = array[''a''..''z''] of char;',
  8334. 'var',
  8335. ' Arr: TChars;',
  8336. ' Arr2: TChars2;',
  8337. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8338. ' Arr4: array[11..13] of char = ''pas'';',
  8339. ' Arr5: array[21..22] of char = ''äö'';',
  8340. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8341. ' c: char;',
  8342. ' b: boolean;',
  8343. 'begin',
  8344. ' c:=low(arr);',
  8345. ' c:=high(arr);',
  8346. ' arr[''B'']:=''a'';',
  8347. ' arr[''D'']:=arr[c];',
  8348. ' arr[c]:=arr[''d''];',
  8349. ' arr[arr[c]]:=arr[high(arr)];',
  8350. ' b:=arr[low(arr)]=arr[''e''];',
  8351. ' c:=low(arr2);',
  8352. ' c:=high(arr2);',
  8353. ' arr2[''b'']:=''f'';',
  8354. ' arr2[''a'']:=arr2[c];',
  8355. ' arr2[c]:=arr2[''g''];']);
  8356. ConvertProgram;
  8357. CheckSource('TestArray_StaticChar',
  8358. LinesToStr([ // statements
  8359. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8360. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8361. 'this.Arr3 = ["p", "a", "s"];',
  8362. 'this.Arr4 = ["p", "a", "s"];',
  8363. 'this.Arr5 = ["ä", "ö"];',
  8364. 'this.Arr6 = ["ä", "ö"];',
  8365. 'this.c = "";',
  8366. 'this.b = false;',
  8367. '']),
  8368. LinesToStr([ // $mod.$main
  8369. '$mod.c = "\x00";',
  8370. '$mod.c = "\uFFFF";',
  8371. '$mod.Arr[66] = "a";',
  8372. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8373. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8374. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8375. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8376. '$mod.c = "a";',
  8377. '$mod.c = "z";',
  8378. '$mod.Arr2[1] = "f";',
  8379. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8380. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8381. '']));
  8382. end;
  8383. procedure TTestModule.TestArray_StaticMultiDim;
  8384. begin
  8385. StartProgram(false);
  8386. Add([
  8387. 'type',
  8388. ' TArrayInt = array[1..3] of longint;',
  8389. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8390. 'var',
  8391. ' Arr: TArrayInt;',
  8392. ' Arr2: TArrayArrayInt;',
  8393. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8394. ' i: longint;',
  8395. 'begin',
  8396. ' i:=low(arr);',
  8397. ' i:=low(arr2);',
  8398. ' i:=low(arr2[5]);',
  8399. ' i:=high(arr);',
  8400. ' i:=high(arr2);',
  8401. ' i:=high(arr2[6]);',
  8402. ' arr2[5]:=arr;',
  8403. ' arr2[6][2]:=i;',
  8404. ' i:=arr2[6][3];',
  8405. ' arr2[6,3]:=i;',
  8406. ' i:=arr2[5,2];',
  8407. ' arr2:=arr2;',// clone multi dim static array
  8408. //' arr3:=arr3;',// clone anonymous multi dim static array
  8409. '']);
  8410. ConvertProgram;
  8411. CheckSource('TestArray_StaticMultiDim',
  8412. LinesToStr([ // statements
  8413. 'this.TArrayArrayInt$clone = function (a) {',
  8414. ' var r = [];',
  8415. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8416. ' return r;',
  8417. '};',
  8418. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8419. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8420. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8421. 'this.i = 0;'
  8422. ]),
  8423. LinesToStr([ // $mod.$main
  8424. '$mod.i = 1;',
  8425. '$mod.i = 5;',
  8426. '$mod.i = 1;',
  8427. '$mod.i = 3;',
  8428. '$mod.i = 6;',
  8429. '$mod.i = 3;',
  8430. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8431. '$mod.Arr2[1][1] = $mod.i;',
  8432. '$mod.i = $mod.Arr2[1][2];',
  8433. '$mod.Arr2[1][2] = $mod.i;',
  8434. '$mod.i = $mod.Arr2[0][1];',
  8435. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8436. '']));
  8437. end;
  8438. procedure TTestModule.TestArray_StaticInFunction;
  8439. begin
  8440. StartProgram(false);
  8441. Add([
  8442. 'const TArrayInt = 3;',
  8443. 'const TArrayArrayInt = 4;',
  8444. 'procedure DoIt;',
  8445. 'type',
  8446. ' TArrayInt = array[1..3] of longint;',
  8447. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8448. 'var',
  8449. ' Arr: TArrayInt;',
  8450. ' Arr2: TArrayArrayInt;',
  8451. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8452. ' i: longint;',
  8453. 'begin',
  8454. ' arr2[5]:=arr;',
  8455. ' arr2:=arr2;',// clone multi dim static array
  8456. 'end;',
  8457. 'begin',
  8458. '']);
  8459. ConvertProgram;
  8460. CheckSource('TestArray_StaticInFunction',
  8461. LinesToStr([ // statements
  8462. 'this.TArrayInt = 3;',
  8463. 'this.TArrayArrayInt = 4;',
  8464. 'var TArrayArrayInt$1$clone = function (a) {',
  8465. ' var r = [];',
  8466. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8467. ' return r;',
  8468. '};',
  8469. 'this.DoIt = function () {',
  8470. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8471. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8472. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8473. ' var i = 0;',
  8474. ' Arr2[0] = Arr.slice(0);',
  8475. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8476. '};',
  8477. '']),
  8478. LinesToStr([ // $mod.$main
  8479. '']));
  8480. end;
  8481. procedure TTestModule.TestArrayOfRecord;
  8482. begin
  8483. StartProgram(false);
  8484. Add([
  8485. 'type',
  8486. ' TRec = record',
  8487. ' Int: longint;',
  8488. ' end;',
  8489. ' TArrayRec = array of TRec;',
  8490. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8491. 'begin',
  8492. 'end;',
  8493. 'var',
  8494. ' Arr: TArrayRec;',
  8495. ' r: TRec;',
  8496. ' i: longint;',
  8497. 'begin',
  8498. ' SetLength(arr,3);',
  8499. ' arr[0].int:=4;',
  8500. ' arr[1].int:=length(arr)+arr[2].int;',
  8501. ' arr[arr[i].int].int:=arr[5].int;',
  8502. ' arr[7]:=r;',
  8503. ' r:=arr[8];',
  8504. ' i:=low(arr);',
  8505. ' i:=high(arr);',
  8506. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8507. ConvertProgram;
  8508. CheckSource('TestArrayOfRecord',
  8509. LinesToStr([ // statements
  8510. 'rtl.recNewT($mod, "TRec", function () {',
  8511. ' this.Int = 0;',
  8512. ' this.$eq = function (b) {',
  8513. ' return this.Int === b.Int;',
  8514. ' };',
  8515. ' this.$assign = function (s) {',
  8516. ' this.Int = s.Int;',
  8517. ' return this;',
  8518. ' };',
  8519. '});',
  8520. 'this.DoIt = function (vd, vc, vv) {',
  8521. '};',
  8522. 'this.Arr = [];',
  8523. 'this.r = $mod.TRec.$new();',
  8524. 'this.i = 0;'
  8525. ]),
  8526. LinesToStr([ // $mod.$main
  8527. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8528. '$mod.Arr[0].Int = 4;',
  8529. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8530. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8531. '$mod.Arr[7].$assign($mod.r);',
  8532. '$mod.r.$assign($mod.Arr[8]);',
  8533. '$mod.i = 0;',
  8534. '$mod.i = rtl.length($mod.Arr)-1;',
  8535. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8536. '']));
  8537. end;
  8538. procedure TTestModule.TestArray_StaticRecord;
  8539. begin
  8540. StartProgram(false);
  8541. Add([
  8542. 'type',
  8543. ' TRec = record',
  8544. ' Int: longint;',
  8545. ' end;',
  8546. ' TArrayRec = array[1..2] of TRec;',
  8547. 'var',
  8548. ' Arr: TArrayRec;',
  8549. 'begin',
  8550. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8551. '']);
  8552. ConvertProgram;
  8553. CheckSource('TestArray_StaticRecord',
  8554. LinesToStr([ // statements
  8555. 'rtl.recNewT($mod, "TRec", function () {',
  8556. ' this.Int = 0;',
  8557. ' this.$eq = function (b) {',
  8558. ' return this.Int === b.Int;',
  8559. ' };',
  8560. ' this.$assign = function (s) {',
  8561. ' this.Int = s.Int;',
  8562. ' return this;',
  8563. ' };',
  8564. '});',
  8565. 'this.TArrayRec$clone = function (a) {',
  8566. ' var r = [];',
  8567. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8568. ' return r;',
  8569. '};',
  8570. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8571. '']),
  8572. LinesToStr([ // $mod.$main
  8573. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8574. end;
  8575. procedure TTestModule.TestArrayOfSet;
  8576. begin
  8577. StartProgram(false);
  8578. Add([
  8579. 'type',
  8580. ' TFlag = (big,small);',
  8581. ' TSetOfFlag = set of tflag;',
  8582. ' TArrayFlag = array of TSetOfFlag;',
  8583. 'procedure DoIt(const a: Tarrayflag);',
  8584. 'begin',
  8585. 'end;',
  8586. 'var',
  8587. ' f: TFlag;',
  8588. ' s: TSetOfFlag;',
  8589. ' Arr: TArrayFlag;',
  8590. ' i: longint;',
  8591. 'begin',
  8592. ' SetLength(arr,3);',
  8593. ' arr[0]:=s;',
  8594. ' arr[1]:=[big];',
  8595. ' arr[2]:=[big]+s;',
  8596. ' arr[3]:=s+[big];',
  8597. ' arr[4]:=arr[5];',
  8598. ' s:=arr[6];',
  8599. ' i:=low(arr);',
  8600. ' i:=high(arr);',
  8601. ' DoIt(arr);',
  8602. ' DoIt([s]);',
  8603. ' DoIt([[],s]);',
  8604. ' DoIt([s,[]]);',
  8605. '']);
  8606. ConvertProgram;
  8607. CheckSource('TestArrayOfSet',
  8608. LinesToStr([ // statements
  8609. 'this.TFlag = {',
  8610. ' "0": "big",',
  8611. ' big: 0,',
  8612. ' "1": "small",',
  8613. ' small: 1',
  8614. '};',
  8615. 'this.DoIt = function (a) {',
  8616. '};',
  8617. 'this.f = 0;',
  8618. 'this.s = {};',
  8619. 'this.Arr = [];',
  8620. 'this.i = 0;',
  8621. '']),
  8622. LinesToStr([ // $mod.$main
  8623. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8624. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8625. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8626. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8627. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8628. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8629. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8630. '$mod.i = 0;',
  8631. '$mod.i = rtl.length($mod.Arr) - 1;',
  8632. '$mod.DoIt($mod.Arr);',
  8633. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8634. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8635. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8636. '']));
  8637. end;
  8638. procedure TTestModule.TestArray_DynAsParam;
  8639. begin
  8640. StartProgram(false);
  8641. Add([
  8642. 'type integer = longint;',
  8643. 'type TArrInt = array of integer;',
  8644. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8645. 'var vJ: TArrInt;',
  8646. 'begin',
  8647. ' vg:=vg;',
  8648. ' vj:=vh;',
  8649. ' vi:=vi;',
  8650. ' doit(vg,vg,vg);',
  8651. ' doit(vh,vh,vj);',
  8652. ' doit(vi,vi,vi);',
  8653. ' doit(vj,vj,vj);',
  8654. 'end;',
  8655. 'var i: TArrInt;',
  8656. 'begin',
  8657. ' doit(i,i,i);']);
  8658. ConvertProgram;
  8659. CheckSource('TestArray_DynAsParams',
  8660. LinesToStr([ // statements
  8661. 'this.DoIt = function (vG,vH,vI) {',
  8662. ' var vJ = [];',
  8663. ' vG = vG;',
  8664. ' vJ = vH;',
  8665. ' vI.set(vI.get());',
  8666. ' $mod.DoIt(vG, vG, {',
  8667. ' get: function () {',
  8668. ' return vG;',
  8669. ' },',
  8670. ' set: function (v) {',
  8671. ' vG = v;',
  8672. ' }',
  8673. ' });',
  8674. ' $mod.DoIt(vH, vH, {',
  8675. ' get: function () {',
  8676. ' return vJ;',
  8677. ' },',
  8678. ' set: function (v) {',
  8679. ' vJ = v;',
  8680. ' }',
  8681. ' });',
  8682. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8683. ' $mod.DoIt(vJ, vJ, {',
  8684. ' get: function () {',
  8685. ' return vJ;',
  8686. ' },',
  8687. ' set: function (v) {',
  8688. ' vJ = v;',
  8689. ' }',
  8690. ' });',
  8691. '};',
  8692. 'this.i = [];'
  8693. ]),
  8694. LinesToStr([
  8695. '$mod.DoIt($mod.i,$mod.i,{',
  8696. ' p: $mod,',
  8697. ' get: function () {',
  8698. ' return this.p.i;',
  8699. ' },',
  8700. ' set: function (v) {',
  8701. ' this.p.i = v;',
  8702. ' }',
  8703. '});'
  8704. ]));
  8705. end;
  8706. procedure TTestModule.TestArray_StaticAsParam;
  8707. begin
  8708. StartProgram(false);
  8709. Add([
  8710. 'type integer = longint;',
  8711. 'type TArrInt = array[1..2] of integer;',
  8712. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8713. 'var vJ: TArrInt;',
  8714. 'begin',
  8715. ' vg:=vg;',
  8716. ' vj:=vh;',
  8717. ' vi:=vi;',
  8718. ' doit(vg,vg,vg);',
  8719. ' doit(vh,vh,vj);',
  8720. ' doit(vi,vi,vi);',
  8721. ' doit(vj,vj,vj);',
  8722. 'end;',
  8723. 'var i: TArrInt;',
  8724. 'begin',
  8725. ' doit(i,i,i);']);
  8726. ConvertProgram;
  8727. CheckSource('TestArray_StaticAsParams',
  8728. LinesToStr([ // statements
  8729. 'this.DoIt = function (vG,vH,vI) {',
  8730. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  8731. ' vG = vG.slice(0);',
  8732. ' vJ = vH.slice(0);',
  8733. ' vI.set(vI.get().slice(0));',
  8734. ' $mod.DoIt(vG.slice(0), vG, {',
  8735. ' get: function () {',
  8736. ' return vG;',
  8737. ' },',
  8738. ' set: function (v) {',
  8739. ' vG = v;',
  8740. ' }',
  8741. ' });',
  8742. ' $mod.DoIt(vH.slice(0), vH, {',
  8743. ' get: function () {',
  8744. ' return vJ;',
  8745. ' },',
  8746. ' set: function (v) {',
  8747. ' vJ = v;',
  8748. ' }',
  8749. ' });',
  8750. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  8751. ' $mod.DoIt(vJ.slice(0), vJ, {',
  8752. ' get: function () {',
  8753. ' return vJ;',
  8754. ' },',
  8755. ' set: function (v) {',
  8756. ' vJ = v;',
  8757. ' }',
  8758. ' });',
  8759. '};',
  8760. 'this.i = rtl.arraySetLength(null, 0, 2);'
  8761. ]),
  8762. LinesToStr([
  8763. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  8764. ' p: $mod,',
  8765. ' get: function () {',
  8766. ' return this.p.i;',
  8767. ' },',
  8768. ' set: function (v) {',
  8769. ' this.p.i = v;',
  8770. ' }',
  8771. '});'
  8772. ]));
  8773. end;
  8774. procedure TTestModule.TestArrayElement_AsParams;
  8775. begin
  8776. StartProgram(false);
  8777. Add('type integer = longint;');
  8778. Add('type TArrayInt = array of integer;');
  8779. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  8780. Add('var vJ: tarrayint;');
  8781. Add('begin');
  8782. Add(' vi:=vi;');
  8783. Add(' doit(vi,vi,vi);');
  8784. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  8785. Add('end;');
  8786. Add('var a: TArrayInt;');
  8787. Add('begin');
  8788. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  8789. ConvertProgram;
  8790. CheckSource('TestArrayElement_AsParams',
  8791. LinesToStr([ // statements
  8792. 'this.DoIt = function (vG,vH,vI) {',
  8793. ' var vJ = [];',
  8794. ' vI.set(vI.get());',
  8795. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8796. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  8797. ' a:1+3,',
  8798. ' p:vJ,',
  8799. ' get: function () {',
  8800. ' return this.p[this.a];',
  8801. ' },',
  8802. ' set: function (v) {',
  8803. ' this.p[this.a] = v;',
  8804. ' }',
  8805. ' });',
  8806. '};',
  8807. 'this.a = [];'
  8808. ]),
  8809. LinesToStr([
  8810. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  8811. ' a: 1+6,',
  8812. ' p: $mod.a,',
  8813. ' get: function () {',
  8814. ' return this.p[this.a];',
  8815. ' },',
  8816. ' set: function (v) {',
  8817. ' this.p[this.a] = v;',
  8818. ' }',
  8819. '});'
  8820. ]));
  8821. end;
  8822. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  8823. begin
  8824. StartProgram(false);
  8825. Add('type Integer = longint;');
  8826. Add('type TArrayInt = array of integer;');
  8827. Add('function GetArr(vB: integer = 0): tarrayint;');
  8828. Add('begin');
  8829. Add('end;');
  8830. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  8831. Add('begin');
  8832. Add('end;');
  8833. Add('begin');
  8834. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  8835. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  8836. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  8837. ConvertProgram;
  8838. CheckSource('TestArrayElementFromFuncResult_AsParams',
  8839. LinesToStr([ // statements
  8840. 'this.GetArr = function (vB) {',
  8841. ' var Result = [];',
  8842. ' return Result;',
  8843. '};',
  8844. 'this.DoIt = function (vG,vH,vI) {',
  8845. '};'
  8846. ]),
  8847. LinesToStr([
  8848. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  8849. ' a: 1+3,',
  8850. ' p: $mod.GetArr(0),',
  8851. ' get: function () {',
  8852. ' return this.p[this.a];',
  8853. ' },',
  8854. ' set: function (v) {',
  8855. ' this.p[this.a] = v;',
  8856. ' }',
  8857. '});',
  8858. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  8859. ' a: 2+3,',
  8860. ' p: $mod.GetArr(0),',
  8861. ' get: function () {',
  8862. ' return this.p[this.a];',
  8863. ' },',
  8864. ' set: function (v) {',
  8865. ' this.p[this.a] = v;',
  8866. ' }',
  8867. '});',
  8868. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  8869. ' a: 3+3,',
  8870. ' p: $mod.GetArr(9),',
  8871. ' get: function () {',
  8872. ' return this.p[this.a];',
  8873. ' },',
  8874. ' set: function (v) {',
  8875. ' this.p[this.a] = v;',
  8876. ' }',
  8877. '});',
  8878. '']));
  8879. end;
  8880. procedure TTestModule.TestArrayEnumTypeRange;
  8881. begin
  8882. StartProgram(false);
  8883. Add([
  8884. 'type',
  8885. ' TEnum = (red,blue);',
  8886. ' TEnumArray = array[TEnum] of longint;',
  8887. 'var',
  8888. ' e: TEnum;',
  8889. ' i: longint;',
  8890. ' a: TEnumArray;',
  8891. ' numbers: TEnumArray = (1,2);',
  8892. ' names: array[TEnum] of string = (''red'',''blue'');',
  8893. 'begin',
  8894. ' e:=low(a);',
  8895. ' e:=high(a);',
  8896. ' i:=a[red];',
  8897. ' a[e]:=a[e];']);
  8898. ConvertProgram;
  8899. CheckSource('TestArrayEnumTypeRange',
  8900. LinesToStr([ // statements
  8901. ' this.TEnum = {',
  8902. ' "0": "red",',
  8903. ' red: 0,',
  8904. ' "1": "blue",',
  8905. ' blue: 1',
  8906. '};',
  8907. 'this.e = 0;',
  8908. 'this.i = 0;',
  8909. 'this.a = rtl.arraySetLength(null,0,2);',
  8910. 'this.numbers = [1, 2];',
  8911. 'this.names = ["red", "blue"];',
  8912. '']),
  8913. LinesToStr([ // $mod.$main
  8914. '$mod.e = $mod.TEnum.red;',
  8915. '$mod.e = $mod.TEnum.blue;',
  8916. '$mod.i = $mod.a[$mod.TEnum.red];',
  8917. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  8918. '']));
  8919. end;
  8920. procedure TTestModule.TestArray_SetLengthOutArg;
  8921. begin
  8922. StartProgram(false);
  8923. Add([
  8924. 'type TArrInt = array of longint;',
  8925. 'procedure DoIt(out a: TArrInt);',
  8926. 'begin',
  8927. ' SetLength(a,2);',
  8928. 'end;',
  8929. 'begin',
  8930. '']);
  8931. ConvertProgram;
  8932. CheckSource('TestArray_SetLengthOutArg',
  8933. LinesToStr([ // statements
  8934. 'this.DoIt = function (a) {',
  8935. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  8936. '};',
  8937. '']),
  8938. LinesToStr([
  8939. '']));
  8940. end;
  8941. procedure TTestModule.TestArray_SetLengthProperty;
  8942. begin
  8943. StartProgram(false);
  8944. Add('type');
  8945. Add(' TArrInt = array of longint;');
  8946. Add(' TObject = class');
  8947. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  8948. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  8949. Add(' property Colors: TArrInt read GetColors write SetColors;');
  8950. Add(' end;');
  8951. Add('var Obj: TObject;');
  8952. Add('begin');
  8953. Add(' SetLength(Obj.Colors,2);');
  8954. ConvertProgram;
  8955. CheckSource('TestArray_SetLengthProperty',
  8956. LinesToStr([ // statements
  8957. 'rtl.createClass($mod, "TObject", null, function () {',
  8958. ' this.$init = function () {',
  8959. ' };',
  8960. ' this.$final = function () {',
  8961. ' };',
  8962. '});',
  8963. 'this.Obj = null;',
  8964. '']),
  8965. LinesToStr([
  8966. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  8967. '']));
  8968. end;
  8969. procedure TTestModule.TestArray_SetLengthMultiDim;
  8970. begin
  8971. StartProgram(false);
  8972. Add([
  8973. 'type',
  8974. ' TArrArrInt = array of array of longint;',
  8975. ' TArrStaInt = array of array[1..2] of longint;',
  8976. 'var',
  8977. ' a: TArrArrInt;',
  8978. ' b: TArrStaInt;',
  8979. 'begin',
  8980. ' SetLength(a,2);',
  8981. ' SetLength(a,3,4);',
  8982. ' SetLength(b,5);',
  8983. '']);
  8984. ConvertProgram;
  8985. CheckSource('TestArray_SetLengthMultiDim',
  8986. LinesToStr([ // statements
  8987. 'this.a = [];',
  8988. 'this.b = [];',
  8989. '']),
  8990. LinesToStr([
  8991. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  8992. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  8993. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, 2);',
  8994. '']));
  8995. end;
  8996. procedure TTestModule.TestArray_OpenArrayOfString;
  8997. begin
  8998. StartProgram(false);
  8999. Add('procedure DoIt(const a: array of String);');
  9000. Add('var');
  9001. Add(' i: longint;');
  9002. Add(' s: string;');
  9003. Add('begin');
  9004. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9005. Add('end;');
  9006. Add('var s: string;');
  9007. Add('begin');
  9008. Add(' DoIt([]);');
  9009. Add(' DoIt([s,''foo'','''',s+s]);');
  9010. ConvertProgram;
  9011. CheckSource('TestArray_OpenArrayOfString',
  9012. LinesToStr([ // statements
  9013. 'this.DoIt = function (a) {',
  9014. ' var i = 0;',
  9015. ' var s = "";',
  9016. ' for (var $l1 = 0, $end2 = rtl.length(a) - 1; $l1 <= $end2; $l1++) {',
  9017. ' i = $l1;',
  9018. ' s = a[rtl.length(a) - i - 1];',
  9019. ' };',
  9020. '};',
  9021. 'this.s = "";',
  9022. '']),
  9023. LinesToStr([
  9024. '$mod.DoIt([]);',
  9025. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9026. '']));
  9027. end;
  9028. procedure TTestModule.TestArray_ConstRef;
  9029. begin
  9030. StartProgram(false);
  9031. Add([
  9032. 'type TArr = array of word;',
  9033. 'procedure Run(constref a: TArr);',
  9034. 'begin',
  9035. 'end;',
  9036. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9037. 'var l: TArr;',
  9038. 'begin',
  9039. ' Run(l);',
  9040. ' Run(a);',
  9041. ' Run(b);',
  9042. ' Run(c);',
  9043. ' Run(d);',
  9044. ' Run(e);',
  9045. 'end;',
  9046. 'begin',
  9047. '']);
  9048. ConvertProgram;
  9049. CheckResolverUnexpectedHints();
  9050. CheckSource('TestArray_ConstRef',
  9051. LinesToStr([ // statements
  9052. 'this.Run = function (a) {',
  9053. '};',
  9054. 'this.Fly = function (a, b, c, d, e) {',
  9055. ' var l = [];',
  9056. ' $mod.Run(l);',
  9057. ' $mod.Run(a);',
  9058. ' $mod.Run(b.get());',
  9059. ' $mod.Run(c.get());',
  9060. ' $mod.Run(d);',
  9061. ' $mod.Run(e);',
  9062. '};',
  9063. '']),
  9064. LinesToStr([
  9065. '']));
  9066. end;
  9067. procedure TTestModule.TestArray_Concat;
  9068. begin
  9069. StartProgram(false);
  9070. Add([
  9071. 'type',
  9072. ' integer = longint;',
  9073. ' TFlag = (big,small);',
  9074. ' TFlags = set of TFlag;',
  9075. ' TRec = record',
  9076. ' i: integer;',
  9077. ' end;',
  9078. ' TArrInt = array of integer;',
  9079. ' TArrRec = array of TRec;',
  9080. ' TArrFlag = array of TFlag;',
  9081. ' TArrSet = array of TFlags;',
  9082. ' TArrJSValue = array of jsvalue;',
  9083. 'var',
  9084. ' ArrInt: tarrint;',
  9085. ' ArrRec: tarrrec;',
  9086. ' ArrFlag: tarrflag;',
  9087. ' ArrSet: tarrset;',
  9088. ' ArrJSValue: tarrjsvalue;',
  9089. 'begin',
  9090. ' arrint:=concat(arrint);',
  9091. ' arrint:=concat(arrint,arrint);',
  9092. ' arrint:=concat(arrint,arrint,arrint);',
  9093. ' arrrec:=concat(arrrec);',
  9094. ' arrrec:=concat(arrrec,arrrec);',
  9095. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9096. ' arrset:=concat(arrset);',
  9097. ' arrset:=concat(arrset,arrset);',
  9098. ' arrset:=concat(arrset,arrset,arrset);',
  9099. ' arrjsvalue:=concat(arrjsvalue);',
  9100. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9101. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9102. ' arrint:=concat([1],arrint);',
  9103. ' arrflag:=concat([big]);',
  9104. ' arrflag:=concat([big],arrflag);',
  9105. ' arrflag:=concat(arrflag,[small]);',
  9106. '']);
  9107. ConvertProgram;
  9108. CheckSource('TestArray_Concat',
  9109. LinesToStr([ // statements
  9110. 'this.TFlag = {',
  9111. ' "0": "big",',
  9112. ' big: 0,',
  9113. ' "1": "small",',
  9114. ' small: 1',
  9115. '};',
  9116. 'rtl.recNewT($mod, "TRec", function () {',
  9117. ' this.i = 0;',
  9118. ' this.$eq = function (b) {',
  9119. ' return this.i === b.i;',
  9120. ' };',
  9121. ' this.$assign = function (s) {',
  9122. ' this.i = s.i;',
  9123. ' return this;',
  9124. ' };',
  9125. '});',
  9126. 'this.ArrInt = [];',
  9127. 'this.ArrRec = [];',
  9128. 'this.ArrFlag = [];',
  9129. 'this.ArrSet = [];',
  9130. 'this.ArrJSValue = [];',
  9131. '']),
  9132. LinesToStr([ // $mod.$main
  9133. '$mod.ArrInt = $mod.ArrInt;',
  9134. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9135. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9136. '$mod.ArrRec = $mod.ArrRec;',
  9137. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9138. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9139. '$mod.ArrSet = $mod.ArrSet;',
  9140. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9141. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9142. '$mod.ArrJSValue = $mod.ArrJSValue;',
  9143. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9144. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9145. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9146. '$mod.ArrFlag = [$mod.TFlag.big];',
  9147. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9148. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9149. '']));
  9150. end;
  9151. procedure TTestModule.TestArray_Copy;
  9152. begin
  9153. StartProgram(false);
  9154. Add([
  9155. 'type',
  9156. ' integer = longint;',
  9157. ' TFlag = (big,small);',
  9158. ' TFlags = set of TFlag;',
  9159. ' TRec = record',
  9160. ' i: integer;',
  9161. ' end;',
  9162. ' TArrInt = array of integer;',
  9163. ' TArrRec = array of TRec;',
  9164. ' TArrSet = array of TFlags;',
  9165. ' TArrJSValue = array of jsvalue;',
  9166. 'var',
  9167. ' ArrInt: tarrint;',
  9168. ' ArrRec: tarrrec;',
  9169. ' ArrSet: tarrset;',
  9170. ' ArrJSValue: tarrjsvalue;',
  9171. 'begin',
  9172. ' arrint:=copy(arrint);',
  9173. ' arrint:=copy(arrint,2);',
  9174. ' arrint:=copy(arrint,3,4);',
  9175. ' arrint:=copy([1,1],1,2);',
  9176. ' arrrec:=copy(arrrec);',
  9177. ' arrrec:=copy(arrrec,5);',
  9178. ' arrrec:=copy(arrrec,6,7);',
  9179. ' arrset:=copy(arrset);',
  9180. ' arrset:=copy(arrset,8);',
  9181. ' arrset:=copy(arrset,9,10);',
  9182. ' arrjsvalue:=copy(arrjsvalue);',
  9183. ' arrjsvalue:=copy(arrjsvalue,11);',
  9184. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9185. ' ']);
  9186. ConvertProgram;
  9187. CheckSource('TestArray_Copy',
  9188. LinesToStr([ // statements
  9189. 'this.TFlag = {',
  9190. ' "0": "big",',
  9191. ' big: 0,',
  9192. ' "1": "small",',
  9193. ' small: 1',
  9194. '};',
  9195. 'rtl.recNewT($mod, "TRec", function () {',
  9196. ' this.i = 0;',
  9197. ' this.$eq = function (b) {',
  9198. ' return this.i === b.i;',
  9199. ' };',
  9200. ' this.$assign = function (s) {',
  9201. ' this.i = s.i;',
  9202. ' return this;',
  9203. ' };',
  9204. '});',
  9205. 'this.ArrInt = [];',
  9206. 'this.ArrRec = [];',
  9207. 'this.ArrSet = [];',
  9208. 'this.ArrJSValue = [];',
  9209. '']),
  9210. LinesToStr([ // $mod.$main
  9211. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9212. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9213. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9214. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9215. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9216. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9217. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9218. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9219. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9220. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9221. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9222. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9223. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9224. '']));
  9225. end;
  9226. procedure TTestModule.TestArray_InsertDelete;
  9227. begin
  9228. StartProgram(false);
  9229. Add([
  9230. 'type',
  9231. ' integer = longint;',
  9232. ' TFlag = (big,small);',
  9233. ' TFlags = set of TFlag;',
  9234. ' TRec = record',
  9235. ' i: integer;',
  9236. ' end;',
  9237. ' TArrInt = array of integer;',
  9238. ' TArrRec = array of TRec;',
  9239. ' TArrSet = array of TFlags;',
  9240. ' TArrJSValue = array of jsvalue;',
  9241. ' TArrArrInt = array of TArrInt;',
  9242. 'var',
  9243. ' ArrInt: tarrint;',
  9244. ' ArrRec: tarrrec;',
  9245. ' ArrSet: tarrset;',
  9246. ' ArrJSValue: tarrjsvalue;',
  9247. ' ArrArrInt: TArrArrInt;',
  9248. 'begin',
  9249. ' Insert(1,arrint,2);',
  9250. ' Insert(arrint[3],arrint,4);',
  9251. ' Insert(arrrec[5],arrrec,6);',
  9252. ' Insert(arrset[7],arrset,7);',
  9253. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9254. ' Insert(10,arrjsvalue,11);',
  9255. ' Insert([23],arrarrint,22);',
  9256. ' Delete(arrint,12,13);',
  9257. ' Delete(arrrec,14,15);',
  9258. ' Delete(arrset,17,18);',
  9259. ' Delete(arrjsvalue,19,10);']);
  9260. ConvertProgram;
  9261. CheckSource('TestArray_InsertDelete',
  9262. LinesToStr([ // statements
  9263. 'this.TFlag = {',
  9264. ' "0": "big",',
  9265. ' big: 0,',
  9266. ' "1": "small",',
  9267. ' small: 1',
  9268. '};',
  9269. 'rtl.recNewT($mod, "TRec", function () {',
  9270. ' this.i = 0;',
  9271. ' this.$eq = function (b) {',
  9272. ' return this.i === b.i;',
  9273. ' };',
  9274. ' this.$assign = function (s) {',
  9275. ' this.i = s.i;',
  9276. ' return this;',
  9277. ' };',
  9278. '});',
  9279. 'this.ArrInt = [];',
  9280. 'this.ArrRec = [];',
  9281. 'this.ArrSet = [];',
  9282. 'this.ArrJSValue = [];',
  9283. 'this.ArrArrInt = [];',
  9284. '']),
  9285. LinesToStr([ // $mod.$main
  9286. '$mod.ArrInt.splice(2, 0, 1);',
  9287. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9288. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9289. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9290. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9291. '$mod.ArrJSValue.splice(11, 0, 10);',
  9292. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9293. '$mod.ArrInt.splice(12, 13);',
  9294. '$mod.ArrRec.splice(14, 15);',
  9295. '$mod.ArrSet.splice(17, 18);',
  9296. '$mod.ArrJSValue.splice(19, 10);',
  9297. '']));
  9298. end;
  9299. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9300. begin
  9301. StartProgram(false);
  9302. Add([
  9303. '{$modeswitch arrayoperators}',
  9304. 'type',
  9305. ' integer = longint;',
  9306. ' TArrInt = array of integer;',
  9307. ' TArrStr = array of string;',
  9308. 'const',
  9309. ' Ints: TArrInt = (1,2,3);',
  9310. ' Aliases: TarrStr = (''foo'',''b'');',
  9311. ' OneInt: TArrInt = (7);',
  9312. ' OneStr: array of integer = (7);',
  9313. ' Chars: array of char = ''aoc'';',
  9314. ' Names: array of string = (''a'',''foo'');',
  9315. ' NameCount = low(Names)+high(Names)+length(Names);',
  9316. 'var i: integer;',
  9317. 'begin',
  9318. ' Ints:=[];',
  9319. ' Ints:=[1,1];',
  9320. ' Ints:=[1]+[2];',
  9321. ' Ints:=[2];',
  9322. ' Ints:=[]+ints;',
  9323. ' Ints:=Ints+[];',
  9324. ' Ints:=Ints+OneInt;',
  9325. ' Ints:=Ints+[1,1];',
  9326. ' Ints:=[i,i]+Ints;',
  9327. ' Ints:=[1]+[i]+[3];',
  9328. '']);
  9329. ConvertProgram;
  9330. CheckSource('TestArray_DynArrayConstObjFPC',
  9331. LinesToStr([ // statements
  9332. 'this.Ints = [1, 2, 3];',
  9333. 'this.Aliases = ["foo", "b"];',
  9334. 'this.OneInt = [7];',
  9335. 'this.OneStr = [7];',
  9336. 'this.Chars = ["a", "o", "c"];',
  9337. 'this.Names = ["a", "foo"];',
  9338. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9339. 'this.i = 0;',
  9340. '']),
  9341. LinesToStr([ // $mod.$main
  9342. '$mod.Ints = [];',
  9343. '$mod.Ints = [1, 1];',
  9344. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9345. '$mod.Ints = [2];',
  9346. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9347. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9348. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9349. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9350. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9351. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9352. '']));
  9353. end;
  9354. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9355. begin
  9356. StartProgram(false);
  9357. // Note: const c = [1,1]; defines a set!
  9358. Add([
  9359. '{$mode delphi}',
  9360. 'type',
  9361. ' integer = longint;',
  9362. ' TArrInt = array of integer;',
  9363. ' TArrStr = array of string;',
  9364. 'const',
  9365. ' Ints: TArrInt = [1,1,2];',
  9366. ' Aliases: TarrStr = [''foo'',''b''];',
  9367. ' OneInt: TArrInt = [7];',
  9368. ' OneStr: array of integer = [7]+[8];',
  9369. ' Chars: array of char = ''aoc'';',
  9370. ' Names: array of string = [''a'',''a''];',
  9371. ' NameCount = low(Names)+high(Names)+length(Names);',
  9372. 'begin',
  9373. '']);
  9374. ConvertProgram;
  9375. CheckSource('TestArray_DynArrayConstDelphi',
  9376. LinesToStr([ // statements
  9377. 'this.Ints = [1, 1, 2];',
  9378. 'this.Aliases = ["foo", "b"];',
  9379. 'this.OneInt = [7];',
  9380. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9381. 'this.Chars = ["a", "o", "c"];',
  9382. 'this.Names = ["a", "a"];',
  9383. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9384. '']),
  9385. LinesToStr([ // $mod.$main
  9386. '']));
  9387. end;
  9388. procedure TTestModule.TestArray_ArrayLitAsParam;
  9389. begin
  9390. StartProgram(false);
  9391. Add([
  9392. '{$modeswitch arrayoperators}',
  9393. 'type',
  9394. ' integer = longint;',
  9395. ' TArrInt = array of integer;',
  9396. ' TArrSet = array of (red,green,blue);',
  9397. 'procedure DoOpenInt(a: array of integer); forward;',
  9398. 'procedure DoInt(a: TArrInt);',
  9399. 'begin',
  9400. ' DoInt(a+[1]);',
  9401. ' DoInt([1]+a);',
  9402. ' DoOpenInt(a);',
  9403. ' DoOpenInt(a+[1]);',
  9404. ' DoOpenInt([1]+a);',
  9405. 'end;',
  9406. 'procedure DoOpenInt(a: array of integer);',
  9407. 'begin',
  9408. ' DoOpenInt(a+[1]);',
  9409. ' DoOpenInt([1]+a);',
  9410. ' DoInt(a);',
  9411. ' DoInt(a+[1]);',
  9412. ' DoInt([1]+a);',
  9413. 'end;',
  9414. 'procedure DoSet(a: TArrSet);',
  9415. 'begin',
  9416. ' DoSet(a+[red]);',
  9417. ' DoSet([blue]+a);',
  9418. 'end;',
  9419. 'var',
  9420. ' i: TArrInt;',
  9421. ' s: TArrSet;',
  9422. 'begin',
  9423. ' DoInt([1]);',
  9424. ' DoInt([1]+[2]);',
  9425. ' DoInt(i+[1]);',
  9426. ' DoInt([1]+i);',
  9427. ' DoOpenInt([1]);',
  9428. ' DoOpenInt([1]+[2]);',
  9429. ' DoOpenInt(i+[1]);',
  9430. ' DoOpenInt([1]+i);',
  9431. ' DoSet([red]);',
  9432. ' DoSet([blue]+[green]);',
  9433. ' DoSet(s+[blue]);',
  9434. ' DoSet([red]+s);',
  9435. '']);
  9436. ConvertProgram;
  9437. CheckSource('TestArray_ArrayLitAsParam',
  9438. LinesToStr([ // statements
  9439. 'this.TArrSet$a = {',
  9440. ' "0": "red",',
  9441. ' red: 0,',
  9442. ' "1": "green",',
  9443. ' green: 1,',
  9444. ' "2": "blue",',
  9445. ' blue: 2',
  9446. '};',
  9447. 'this.DoInt = function (a) {',
  9448. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9449. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9450. ' $mod.DoOpenInt(a);',
  9451. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9452. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9453. '};',
  9454. 'this.DoOpenInt = function (a) {',
  9455. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9456. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9457. ' $mod.DoInt(a);',
  9458. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9459. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9460. '};',
  9461. 'this.DoSet = function (a) {',
  9462. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9463. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9464. '};',
  9465. 'this.i = [];',
  9466. 'this.s = [];',
  9467. '']),
  9468. LinesToStr([ // $mod.$main
  9469. '$mod.DoInt([1]);',
  9470. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9471. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9472. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9473. '$mod.DoOpenInt([1]);',
  9474. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9475. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9476. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9477. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9478. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9479. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9480. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9481. '']));
  9482. end;
  9483. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9484. begin
  9485. StartProgram(false);
  9486. Add([
  9487. '{$modeswitch arrayoperators}',
  9488. 'type',
  9489. ' integer = longint;',
  9490. ' TArrInt = array of integer;',
  9491. ' TArrArrInt = array of TArrInt;',
  9492. 'procedure DoInt(a: TArrArrInt);',
  9493. 'begin',
  9494. ' DoInt(a+[[1]]);',
  9495. ' DoInt([[1]]+a);',
  9496. ' DoInt(a);',
  9497. 'end;',
  9498. 'var',
  9499. ' i: TArrInt;',
  9500. ' a: TArrArrInt;',
  9501. 'begin',
  9502. ' a:=[[1]];',
  9503. ' a:=[i];',
  9504. ' a:=a+[i];',
  9505. ' a:=[i]+a;',
  9506. ' a:=[[1]+i];',
  9507. ' a:=[[1]+[2]];',
  9508. ' a:=[i+[2]];',
  9509. ' DoInt([[1]]);',
  9510. ' DoInt([[1]+[2],[3,4],[5]]);',
  9511. ' DoInt([i+[1]]+a);',
  9512. ' DoInt([i]+a);',
  9513. '']);
  9514. ConvertProgram;
  9515. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9516. LinesToStr([ // statements
  9517. 'this.DoInt = function (a) {',
  9518. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9519. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9520. ' $mod.DoInt(a);',
  9521. '};',
  9522. 'this.i = [];',
  9523. 'this.a = [];',
  9524. '']),
  9525. LinesToStr([ // $mod.$main
  9526. '$mod.a = [[1]];',
  9527. '$mod.a = [$mod.i];',
  9528. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9529. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9530. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9531. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9532. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9533. '$mod.DoInt([[1]]);',
  9534. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9535. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  9536. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  9537. '']));
  9538. end;
  9539. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  9540. begin
  9541. StartProgram(false);
  9542. Add([
  9543. '{$modeswitch arrayoperators}',
  9544. 'type',
  9545. ' integer = longint;',
  9546. ' TArrInt = array[1..2] of integer;',
  9547. ' TArrArrInt = array of TArrInt;',
  9548. 'procedure DoInt(a: TArrArrInt);',
  9549. 'begin',
  9550. ' DoInt(a+[[1,2]]);',
  9551. ' DoInt([[1,2]]+a);',
  9552. ' DoInt(a);',
  9553. 'end;',
  9554. 'var',
  9555. ' i: TArrInt;',
  9556. ' a: TArrArrInt;',
  9557. 'begin',
  9558. ' a:=[[1,1]];',
  9559. ' a:=[i];',
  9560. ' a:=a+[i];',
  9561. ' a:=[i]+a;',
  9562. ' DoInt([[1,1]]);',
  9563. ' DoInt([[1,2],[3,4]]);',
  9564. '']);
  9565. ConvertProgram;
  9566. CheckSource('TestArray_ArrayLitStaticAsParam',
  9567. LinesToStr([ // statements
  9568. 'this.DoInt = function (a) {',
  9569. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  9570. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  9571. ' $mod.DoInt(a);',
  9572. '};',
  9573. 'this.i = rtl.arraySetLength(null, 0, 2);',
  9574. 'this.a = [];',
  9575. '']),
  9576. LinesToStr([ // $mod.$main
  9577. '$mod.a = [[1, 1]];',
  9578. '$mod.a = [$mod.i.slice(0)];',
  9579. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  9580. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  9581. '$mod.DoInt([[1, 1]]);',
  9582. '$mod.DoInt([[1, 2], [3, 4]]);',
  9583. '']));
  9584. end;
  9585. procedure TTestModule.TestArray_ForInArrOfString;
  9586. begin
  9587. StartProgram(false);
  9588. Add([
  9589. 'type',
  9590. 'type',
  9591. ' TMonthNameArray = array [1..12] of string;',
  9592. ' TMonthNames = TMonthNameArray;',
  9593. ' TObject = class',
  9594. ' private',
  9595. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  9596. ' public',
  9597. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9598. ' end;',
  9599. 'var',
  9600. ' f: TObject;',
  9601. ' Month: string;',
  9602. ' Names: array of string = (''a'',''foo'',''bar'');',
  9603. ' i: longint;',
  9604. 'begin',
  9605. ' for Month in f.LongMonthNames do ;',
  9606. ' for Month in Names do ;',
  9607. ' for i:=low(Names) to high(Names) do ;',
  9608. '']);
  9609. ConvertProgram;
  9610. CheckSource('TestArray_ForInArrOfString',
  9611. LinesToStr([ // statements
  9612. 'rtl.createClass($mod, "TObject", null, function () {',
  9613. ' this.$init = function () {',
  9614. ' };',
  9615. ' this.$final = function () {',
  9616. ' };',
  9617. '});',
  9618. 'this.f = null;',
  9619. 'this.Month = "";',
  9620. 'this.Names = ["a", "foo", "bar"];',
  9621. 'this.i = 0;',
  9622. '']),
  9623. LinesToStr([ // $mod.$main
  9624. 'for (var $in1 = $mod.f.GetLongMonthNames(), $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.Month = $in1[$l2];',
  9625. 'for (var $in4 = $mod.Names, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.Month = $in4[$l5];',
  9626. 'for (var $l7 = 0, $end8 = rtl.length($mod.Names) - 1; $l7 <= $end8; $l7++) $mod.i = $l7;',
  9627. '']));
  9628. end;
  9629. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9630. begin
  9631. StartProgram(false);
  9632. Add([
  9633. '{$modeswitch externalclass}',
  9634. 'type',
  9635. ' TJSObject = class external name ''Object''',
  9636. ' end;',
  9637. ' TJSArray = class external name ''Array''',
  9638. ' class function isArray(Value: JSValue) : boolean;',
  9639. ' function concat() : TJSArray; varargs;',
  9640. ' end;',
  9641. 'var',
  9642. ' aObj: TJSArray;',
  9643. ' a: array of longint;',
  9644. ' o: TJSObject;',
  9645. 'begin',
  9646. ' if TJSArray.isArray(65) then ;',
  9647. ' aObj:=TJSArray(a).concat(a);',
  9648. ' o:=TJSObject(a);']);
  9649. ConvertProgram;
  9650. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  9651. LinesToStr([ // statements
  9652. 'this.aObj = null;',
  9653. 'this.a = [];',
  9654. 'this.o = null;',
  9655. '']),
  9656. LinesToStr([ // $mod.$main
  9657. 'if (Array.isArray(65)) ;',
  9658. '$mod.aObj = $mod.a.concat($mod.a);',
  9659. '$mod.o = $mod.a;',
  9660. '']));
  9661. end;
  9662. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  9663. begin
  9664. StartProgram(false);
  9665. Add([
  9666. '{$modeswitch externalclass}',
  9667. 'type',
  9668. ' TArrStr = array of string;',
  9669. ' TJSArray = class external name ''Array''',
  9670. ' end;',
  9671. ' TJSObject = class external name ''Object''',
  9672. ' end;',
  9673. 'var',
  9674. ' aObj: TJSArray;',
  9675. ' a: TArrStr;',
  9676. ' jo: TJSObject;',
  9677. 'begin',
  9678. ' a:=TArrStr(aObj);',
  9679. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  9680. ' a:=TarrStr(jo);',
  9681. '']);
  9682. ConvertProgram;
  9683. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  9684. LinesToStr([ // statements
  9685. 'this.aObj = null;',
  9686. 'this.a = [];',
  9687. 'this.jo = null;',
  9688. '']),
  9689. LinesToStr([ // $mod.$main
  9690. '$mod.a = $mod.aObj;',
  9691. '$mod.aObj[1] = $mod.aObj[2];',
  9692. '$mod.a = $mod.jo;',
  9693. '']));
  9694. end;
  9695. procedure TTestModule.TestArrayOfConst_TVarRec;
  9696. begin
  9697. StartProgram(true,[supTVarRec]);
  9698. Add([
  9699. 'procedure Say(args: array of const);',
  9700. 'var',
  9701. ' i: longint;',
  9702. ' v: TVarRec;',
  9703. 'begin',
  9704. ' for i:=low(args) to high(args) do begin',
  9705. ' v:=args[i];',
  9706. ' case v.vtype of',
  9707. ' vtInteger: if length(args)=args[i].vInteger then ;',
  9708. ' end;',
  9709. ' end;',
  9710. ' for v in args do ;',
  9711. ' args:=nil;',
  9712. ' SetLength(args,2);',
  9713. 'end;',
  9714. 'begin']);
  9715. ConvertProgram;
  9716. CheckSource('TestArrayOfConst_TVarRec',
  9717. LinesToStr([ // statements
  9718. 'this.Say = function (args) {',
  9719. ' var i = 0;',
  9720. ' var v = pas.system.TVarRec.$new();',
  9721. ' for (var $l1 = 0, $end2 = rtl.length(args) - 1; $l1 <= $end2; $l1++) {',
  9722. ' i = $l1;',
  9723. ' v.$assign(args[i]);',
  9724. ' var $tmp3 = v.VType;',
  9725. ' if ($tmp3 === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  9726. ' };',
  9727. ' for (var $in4 = args, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) v = $in4[$l5];',
  9728. ' args = [];',
  9729. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  9730. '};',
  9731. '']),
  9732. LinesToStr([ // $mod.$main
  9733. ]));
  9734. end;
  9735. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  9736. begin
  9737. StartProgram(true,[supTVarRec]);
  9738. Add([
  9739. 'procedure Say(args: array of const);',
  9740. 'begin',
  9741. ' Say(args);',
  9742. 'end;',
  9743. 'var',
  9744. ' p: Pointer;',
  9745. ' j: jsvalue;',
  9746. ' c: currency;',
  9747. 'begin',
  9748. ' Say([]);',
  9749. ' Say([1]);',
  9750. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  9751. '']);
  9752. ConvertProgram;
  9753. CheckSource('TestArrayOfConst_PassBaseTypes',
  9754. LinesToStr([ // statements
  9755. 'this.Say = function (args) {',
  9756. ' $mod.Say(args);',
  9757. '};',
  9758. 'this.p = null;',
  9759. 'this.j = undefined;',
  9760. 'this.c = 0;',
  9761. '']),
  9762. LinesToStr([ // $mod.$main
  9763. '$mod.Say([]);',
  9764. '$mod.Say(pas.system.VarRecs(0, 1));',
  9765. '$mod.Say(pas.system.VarRecs(',
  9766. ' 9,',
  9767. ' "c",',
  9768. ' 18,',
  9769. ' "foo",',
  9770. ' 5,',
  9771. ' null,',
  9772. ' 1,',
  9773. ' true,',
  9774. ' 3,',
  9775. ' 1.3,',
  9776. ' 5,',
  9777. ' $mod.p,',
  9778. ' 20,',
  9779. ' $mod.j,',
  9780. ' 12,',
  9781. ' $mod.c',
  9782. ' ));',
  9783. '']));
  9784. end;
  9785. procedure TTestModule.TestArrayOfConst_PassObj;
  9786. begin
  9787. StartProgram(true,[supTVarRec]);
  9788. Add([
  9789. '{$interfaces corba}',
  9790. 'type',
  9791. ' TObject = class',
  9792. ' end;',
  9793. ' TClass = class of TObject;',
  9794. ' IUnknown = interface',
  9795. ' end;',
  9796. 'procedure Say(args: array of const);',
  9797. 'begin',
  9798. 'end;',
  9799. 'var',
  9800. ' o: TObject;',
  9801. ' c: TClass;',
  9802. ' i: IUnknown;',
  9803. 'begin',
  9804. ' Say([o,c,TObject]);',
  9805. ' Say([nil,i]);',
  9806. '']);
  9807. ConvertProgram;
  9808. CheckSource('TestArrayOfConst_PassObj',
  9809. LinesToStr([ // statements
  9810. 'rtl.createClass($mod, "TObject", null, function () {',
  9811. ' this.$init = function () {',
  9812. ' };',
  9813. ' this.$final = function () {',
  9814. ' };',
  9815. '});',
  9816. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  9817. 'this.Say = function (args) {',
  9818. '};',
  9819. 'this.o = null;',
  9820. 'this.c = null;',
  9821. 'this.i = null;',
  9822. '']),
  9823. LinesToStr([ // $mod.$main
  9824. '$mod.Say(pas.system.VarRecs(',
  9825. ' 7,',
  9826. ' $mod.o,',
  9827. ' 8,',
  9828. ' $mod.c,',
  9829. ' 8,',
  9830. ' $mod.TObject',
  9831. '));',
  9832. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  9833. '']));
  9834. end;
  9835. procedure TTestModule.TestRecord_Empty;
  9836. begin
  9837. StartProgram(false);
  9838. Add([
  9839. 'type',
  9840. ' TRecA = record',
  9841. ' end;',
  9842. 'var a,b: TRecA;',
  9843. 'begin',
  9844. ' if a=b then ;']);
  9845. ConvertProgram;
  9846. CheckSource('TestRecord_Empty',
  9847. LinesToStr([ // statements
  9848. 'rtl.recNewT($mod, "TRecA", function () {',
  9849. ' this.$eq = function (b) {',
  9850. ' return true;',
  9851. ' };',
  9852. ' this.$assign = function (s) {',
  9853. ' return this;',
  9854. ' };',
  9855. '});',
  9856. 'this.a = $mod.TRecA.$new();',
  9857. 'this.b = $mod.TRecA.$new();',
  9858. '']),
  9859. LinesToStr([ // $mod.$main
  9860. 'if ($mod.a.$eq($mod.b)) ;'
  9861. ]));
  9862. end;
  9863. procedure TTestModule.TestRecord_Var;
  9864. begin
  9865. StartProgram(false);
  9866. Add('type');
  9867. Add(' TRecA = record');
  9868. Add(' Bold: longint;');
  9869. Add(' end;');
  9870. Add('var Rec: TRecA;');
  9871. Add('begin');
  9872. Add(' rec.bold:=123');
  9873. ConvertProgram;
  9874. CheckSource('TestRecord_Var',
  9875. LinesToStr([ // statements
  9876. 'rtl.recNewT($mod, "TRecA", function () {',
  9877. ' this.Bold = 0;',
  9878. ' this.$eq = function (b) {',
  9879. ' return this.Bold === b.Bold;',
  9880. ' };',
  9881. ' this.$assign = function (s) {',
  9882. ' this.Bold = s.Bold;',
  9883. ' return this;',
  9884. ' };',
  9885. '});',
  9886. 'this.Rec = $mod.TRecA.$new();',
  9887. '']),
  9888. LinesToStr([ // $mod.$main
  9889. '$mod.Rec.Bold = 123;'
  9890. ]));
  9891. end;
  9892. procedure TTestModule.TestRecord_VarExternal;
  9893. begin
  9894. StartProgram(false);
  9895. Add([
  9896. '{$modeswitch externalclass}',
  9897. 'type',
  9898. ' TRecA = record',
  9899. ' i: byte;',
  9900. ' length_: longint external name ''length'';',
  9901. ' end;',
  9902. 'var Rec: TRecA;',
  9903. 'begin',
  9904. ' rec.length_ := rec.length_',
  9905. '']);
  9906. ConvertProgram;
  9907. CheckSource('TestRecord_VarExternal',
  9908. LinesToStr([ // statements
  9909. 'rtl.recNewT($mod, "TRecA", function () {',
  9910. ' this.i = 0;',
  9911. ' this.$eq = function (b) {',
  9912. ' return (this.i === b.i) && (this.length === b.length);',
  9913. ' };',
  9914. ' this.$assign = function (s) {',
  9915. ' this.i = s.i;',
  9916. ' this.length = s.length;',
  9917. ' return this;',
  9918. ' };',
  9919. '});',
  9920. 'this.Rec = $mod.TRecA.$new();',
  9921. '']),
  9922. LinesToStr([ // $mod.$main
  9923. '$mod.Rec.length = $mod.Rec.length;'
  9924. ]));
  9925. end;
  9926. procedure TTestModule.TestRecord_WithDo;
  9927. begin
  9928. StartProgram(false);
  9929. Add('type');
  9930. Add(' TRec = record');
  9931. Add(' vI: longint;');
  9932. Add(' end;');
  9933. Add('var');
  9934. Add(' Int: longint;');
  9935. Add(' r: TRec;');
  9936. Add('begin');
  9937. Add(' with r do');
  9938. Add(' int:=vi;');
  9939. Add(' with r do begin');
  9940. Add(' int:=vi;');
  9941. Add(' vi:=int;');
  9942. Add(' end;');
  9943. ConvertProgram;
  9944. CheckSource('TestWithRecordDo',
  9945. LinesToStr([ // statements
  9946. 'rtl.recNewT($mod, "TRec", function () {',
  9947. ' this.vI = 0;',
  9948. ' this.$eq = function (b) {',
  9949. ' return this.vI === b.vI;',
  9950. ' };',
  9951. ' this.$assign = function (s) {',
  9952. ' this.vI = s.vI;',
  9953. ' return this;',
  9954. ' };',
  9955. '});',
  9956. 'this.Int = 0;',
  9957. 'this.r = $mod.TRec.$new();',
  9958. '']),
  9959. LinesToStr([ // $mod.$main
  9960. 'var $with1 = $mod.r;',
  9961. '$mod.Int = $with1.vI;',
  9962. 'var $with2 = $mod.r;',
  9963. '$mod.Int = $with2.vI;',
  9964. '$with2.vI = $mod.Int;'
  9965. ]));
  9966. end;
  9967. procedure TTestModule.TestRecord_Assign;
  9968. begin
  9969. StartProgram(false);
  9970. Add('type');
  9971. Add(' TEnum = (red,green);');
  9972. Add(' TEnums = set of TEnum;');
  9973. Add(' TSmallRec = record');
  9974. Add(' N: longint;');
  9975. Add(' end;');
  9976. Add(' TBigRec = record');
  9977. Add(' Int: longint;');
  9978. Add(' D: double;');
  9979. Add(' Arr: array of longint;');
  9980. Add(' Arr2: array[1..2] of longint;');
  9981. Add(' Small: TSmallRec;');
  9982. Add(' Enums: TEnums;');
  9983. Add(' end;');
  9984. Add('var');
  9985. Add(' r, s: TBigRec;');
  9986. Add('begin');
  9987. Add(' r:=s;');
  9988. Add(' r:=default(TBigRec);');
  9989. Add(' r:=default(s);');
  9990. ConvertProgram;
  9991. CheckSource('TestRecord_Assign',
  9992. LinesToStr([ // statements
  9993. 'this.TEnum = {',
  9994. ' "0": "red",',
  9995. ' red: 0,',
  9996. ' "1": "green",',
  9997. ' green: 1',
  9998. '};',
  9999. 'rtl.recNewT($mod, "TSmallRec", function () {',
  10000. ' this.N = 0;',
  10001. ' this.$eq = function (b) {',
  10002. ' return this.N === b.N;',
  10003. ' };',
  10004. ' this.$assign = function (s) {',
  10005. ' this.N = s.N;',
  10006. ' return this;',
  10007. ' };',
  10008. '});',
  10009. 'rtl.recNewT($mod, "TBigRec", function () {',
  10010. ' this.Int = 0;',
  10011. ' this.D = 0.0;',
  10012. ' this.Arr = [];',
  10013. ' this.$new = function () {',
  10014. ' var r = Object.create(this);',
  10015. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10016. ' r.Small = $mod.TSmallRec.$new();',
  10017. ' r.Enums = {};',
  10018. ' return r;',
  10019. ' };',
  10020. ' this.$eq = function (b) {',
  10021. ' 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);',
  10022. ' };',
  10023. ' this.$assign = function (s) {',
  10024. ' this.Int = s.Int;',
  10025. ' this.D = s.D;',
  10026. ' this.Arr = s.Arr;',
  10027. ' this.Arr2 = s.Arr2.slice(0);',
  10028. ' this.Small.$assign(s.Small);',
  10029. ' this.Enums = rtl.refSet(s.Enums);',
  10030. ' return this;',
  10031. ' };',
  10032. '});',
  10033. 'this.r = $mod.TBigRec.$new();',
  10034. 'this.s = $mod.TBigRec.$new();',
  10035. '']),
  10036. LinesToStr([ // $mod.$main
  10037. '$mod.r.$assign($mod.s);',
  10038. '$mod.r.$assign($mod.TBigRec.$new());',
  10039. '$mod.r.$assign($mod.TBigRec.$new());',
  10040. '']));
  10041. end;
  10042. procedure TTestModule.TestRecord_AsParams;
  10043. begin
  10044. StartProgram(false);
  10045. Add([
  10046. 'type',
  10047. ' integer = longint;',
  10048. ' TRecord = record',
  10049. ' i: integer;',
  10050. ' end;',
  10051. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10052. 'var vL: TRecord;',
  10053. 'begin',
  10054. ' vd:=vd;',
  10055. ' vd.i:=vd.i;',
  10056. ' vl:=vc;',
  10057. ' vv:=vv;',
  10058. ' vv.i:=vv.i;',
  10059. ' U:=vl;',
  10060. ' U:=vd;',
  10061. ' U:=vc;',
  10062. ' U:=vv;',
  10063. ' vl:=TRecord(U);',
  10064. ' vd:=TRecord(U);',
  10065. ' vv:=TRecord(U);',
  10066. ' doit(vd,vd,vd,vd);',
  10067. ' doit(vc,vc,vl,vl);',
  10068. ' doit(vv,vv,vv,vv);',
  10069. ' doit(vl,vl,vl,vl);',
  10070. ' TRecord(U).i:=3;',
  10071. 'end;',
  10072. 'var i: TRecord;',
  10073. 'begin',
  10074. ' doit(i,i,i,i);',
  10075. '']);
  10076. ConvertProgram;
  10077. CheckSource('TestRecord_AsParams',
  10078. LinesToStr([ // statements
  10079. 'rtl.recNewT($mod, "TRecord", function () {',
  10080. ' this.i = 0;',
  10081. ' this.$eq = function (b) {',
  10082. ' return this.i === b.i;',
  10083. ' };',
  10084. ' this.$assign = function (s) {',
  10085. ' this.i = s.i;',
  10086. ' return this;',
  10087. ' };',
  10088. '});',
  10089. 'this.DoIt = function (vD, vC, vV, U) {',
  10090. ' var vL = $mod.TRecord.$new();',
  10091. ' vD.$assign(vD);',
  10092. ' vD.i = vD.i;',
  10093. ' vL.$assign(vC);',
  10094. ' vV.$assign(vV);',
  10095. ' vV.i = vV.i;',
  10096. ' U.$assign(vL);',
  10097. ' U.$assign(vD);',
  10098. ' U.$assign(vC);',
  10099. ' U.$assign(vV);',
  10100. ' vL.$assign(U);',
  10101. ' vD.$assign(U);',
  10102. ' vV.$assign(U);',
  10103. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10104. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10105. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10106. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10107. ' U.i = 3;',
  10108. '};',
  10109. 'this.i = $mod.TRecord.$new();'
  10110. ]),
  10111. LinesToStr([
  10112. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10113. '']));
  10114. end;
  10115. procedure TTestModule.TestRecord_ConstRef;
  10116. begin
  10117. StartProgram(false);
  10118. Add([
  10119. 'type TRec = record i: word; end;',
  10120. 'procedure Run(constref a: TRec);',
  10121. 'begin',
  10122. 'end;',
  10123. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10124. 'var l: TRec;',
  10125. 'begin',
  10126. ' Run(l);',
  10127. ' Run(a);',
  10128. ' Run(b);',
  10129. ' Run(c);',
  10130. ' Run(d);',
  10131. ' Run(e);',
  10132. 'end;',
  10133. 'begin',
  10134. '']);
  10135. ConvertProgram;
  10136. CheckResolverUnexpectedHints();
  10137. CheckSource('TestRecord_ConstRef',
  10138. LinesToStr([ // statements
  10139. 'rtl.recNewT($mod, "TRec", function () {',
  10140. ' this.i = 0;',
  10141. ' this.$eq = function (b) {',
  10142. ' return this.i === b.i;',
  10143. ' };',
  10144. ' this.$assign = function (s) {',
  10145. ' this.i = s.i;',
  10146. ' return this;',
  10147. ' };',
  10148. '});',
  10149. 'this.Run = function (a) {',
  10150. '};',
  10151. 'this.Fly = function (a, b, c, d, e) {',
  10152. ' var l = $mod.TRec.$new();',
  10153. ' $mod.Run(l);',
  10154. ' $mod.Run(a);',
  10155. ' $mod.Run(b);',
  10156. ' $mod.Run(c);',
  10157. ' $mod.Run(d);',
  10158. ' $mod.Run(e);',
  10159. '};',
  10160. '']),
  10161. LinesToStr([
  10162. '']));
  10163. end;
  10164. procedure TTestModule.TestRecordElement_AsParams;
  10165. begin
  10166. StartProgram(false);
  10167. Add('type');
  10168. Add(' integer = longint;');
  10169. Add(' TRecord = record');
  10170. Add(' i: integer;');
  10171. Add(' end;');
  10172. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10173. Add('var vJ: TRecord;');
  10174. Add('begin');
  10175. Add(' doit(vj.i,vj.i,vj.i);');
  10176. Add('end;');
  10177. Add('var r: TRecord;');
  10178. Add('begin');
  10179. Add(' doit(r.i,r.i,r.i);');
  10180. ConvertProgram;
  10181. CheckSource('TestRecordElement_AsParams',
  10182. LinesToStr([ // statements
  10183. 'rtl.recNewT($mod, "TRecord", function () {',
  10184. ' this.i = 0;',
  10185. ' this.$eq = function (b) {',
  10186. ' return this.i === b.i;',
  10187. ' };',
  10188. ' this.$assign = function (s) {',
  10189. ' this.i = s.i;',
  10190. ' return this;',
  10191. ' };',
  10192. '});',
  10193. 'this.DoIt = function (vG,vH,vI) {',
  10194. ' var vJ = $mod.TRecord.$new();',
  10195. ' $mod.DoIt(vJ.i, vJ.i, {',
  10196. ' p: vJ,',
  10197. ' get: function () {',
  10198. ' return this.p.i;',
  10199. ' },',
  10200. ' set: function (v) {',
  10201. ' this.p.i = v;',
  10202. ' }',
  10203. ' });',
  10204. '};',
  10205. 'this.r = $mod.TRecord.$new();'
  10206. ]),
  10207. LinesToStr([
  10208. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10209. ' p: $mod.r,',
  10210. ' get: function () {',
  10211. ' return this.p.i;',
  10212. ' },',
  10213. ' set: function (v) {',
  10214. ' this.p.i = v;',
  10215. ' }',
  10216. '});'
  10217. ]));
  10218. end;
  10219. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10220. begin
  10221. StartProgram(false);
  10222. Add('type');
  10223. Add(' integer = longint;');
  10224. Add(' TRecord = record');
  10225. Add(' i: integer;');
  10226. Add(' end;');
  10227. Add('function GetRec(vB: integer = 0): TRecord;');
  10228. Add('begin');
  10229. Add('end;');
  10230. Add('procedure DoIt(vG: integer; const vH: integer);');
  10231. Add('begin');
  10232. Add('end;');
  10233. Add('begin');
  10234. Add(' doit(getrec.i,getrec.i);');
  10235. Add(' doit(getrec().i,getrec().i);');
  10236. Add(' doit(getrec(1).i,getrec(2).i);');
  10237. ConvertProgram;
  10238. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10239. LinesToStr([ // statements
  10240. 'rtl.recNewT($mod, "TRecord", function () {',
  10241. ' this.i = 0;',
  10242. ' this.$eq = function (b) {',
  10243. ' return this.i === b.i;',
  10244. ' };',
  10245. ' this.$assign = function (s) {',
  10246. ' this.i = s.i;',
  10247. ' return this;',
  10248. ' };',
  10249. '});',
  10250. 'this.GetRec = function (vB) {',
  10251. ' var Result = $mod.TRecord.$new();',
  10252. ' return Result;',
  10253. '};',
  10254. 'this.DoIt = function (vG, vH) {',
  10255. '};',
  10256. '']),
  10257. LinesToStr([
  10258. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10259. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10260. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10261. '']));
  10262. end;
  10263. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10264. begin
  10265. StartProgram(false);
  10266. Add('type');
  10267. Add(' integer = longint;');
  10268. Add(' TRecord = record');
  10269. Add(' i: integer;');
  10270. Add(' end;');
  10271. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10272. Add('begin');
  10273. Add('end;');
  10274. Add('var r: trecord;');
  10275. Add('begin');
  10276. Add(' with r do ');
  10277. Add(' doit(i,i,i);');
  10278. ConvertProgram;
  10279. CheckSource('TestRecordElementFromWith_AsParams',
  10280. LinesToStr([ // statements
  10281. 'rtl.recNewT($mod, "TRecord", function () {',
  10282. ' this.i = 0;',
  10283. ' this.$eq = function (b) {',
  10284. ' return this.i === b.i;',
  10285. ' };',
  10286. ' this.$assign = function (s) {',
  10287. ' this.i = s.i;',
  10288. ' return this;',
  10289. ' };',
  10290. '});',
  10291. 'this.DoIt = function (vG,vH,vI) {',
  10292. '};',
  10293. 'this.r = $mod.TRecord.$new();'
  10294. ]),
  10295. LinesToStr([
  10296. 'var $with1 = $mod.r;',
  10297. '$mod.DoIt($with1.i,$with1.i,{',
  10298. ' p: $with1,',
  10299. ' get: function () {',
  10300. ' return this.p.i;',
  10301. ' },',
  10302. ' set: function (v) {',
  10303. ' this.p.i = v;',
  10304. ' }',
  10305. '});',
  10306. '']));
  10307. end;
  10308. procedure TTestModule.TestRecord_Equal;
  10309. begin
  10310. StartProgram(false);
  10311. Add('type');
  10312. Add(' integer = longint;');
  10313. Add(' TFlag = (red,blue);');
  10314. Add(' TFlags = set of TFlag;');
  10315. Add(' TProc = procedure;');
  10316. Add(' TRecord = record');
  10317. Add(' i: integer;');
  10318. Add(' Event: TProc;');
  10319. Add(' f: TFlags;');
  10320. Add(' end;');
  10321. Add(' TNested = record');
  10322. Add(' r: TRecord;');
  10323. Add(' end;');
  10324. Add('var');
  10325. Add(' b: boolean;');
  10326. Add(' r,s: trecord;');
  10327. Add('begin');
  10328. Add(' b:=r=s;');
  10329. Add(' b:=r<>s;');
  10330. ConvertProgram;
  10331. CheckSource('TestRecord_Equal',
  10332. LinesToStr([ // statements
  10333. 'this.TFlag = {',
  10334. ' "0": "red",',
  10335. ' red: 0,',
  10336. ' "1": "blue",',
  10337. ' blue: 1',
  10338. '};',
  10339. 'rtl.recNewT($mod, "TRecord", function () {',
  10340. ' this.i = 0;',
  10341. ' this.Event = null;',
  10342. ' this.$new = function () {',
  10343. ' var r = Object.create(this);',
  10344. ' r.f = {};',
  10345. ' return r;',
  10346. ' };',
  10347. ' this.$eq = function (b) {',
  10348. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  10349. ' };',
  10350. ' this.$assign = function (s) {',
  10351. ' this.i = s.i;',
  10352. ' this.Event = s.Event;',
  10353. ' this.f = rtl.refSet(s.f);',
  10354. ' return this;',
  10355. ' };',
  10356. '});',
  10357. 'rtl.recNewT($mod, "TNested", function () {',
  10358. ' this.$new = function () {',
  10359. ' var r = Object.create(this);',
  10360. ' r.r = $mod.TRecord.$new();',
  10361. ' return r;',
  10362. ' };',
  10363. ' this.$eq = function (b) {',
  10364. ' return this.r.$eq(b.r);',
  10365. ' };',
  10366. ' this.$assign = function (s) {',
  10367. ' this.r.$assign(s.r);',
  10368. ' return this;',
  10369. ' };',
  10370. '});',
  10371. 'this.b = false;',
  10372. 'this.r = $mod.TRecord.$new();',
  10373. 'this.s = $mod.TRecord.$new();',
  10374. '']),
  10375. LinesToStr([
  10376. '$mod.b = $mod.r.$eq($mod.s);',
  10377. '$mod.b = !$mod.r.$eq($mod.s);',
  10378. '']));
  10379. end;
  10380. procedure TTestModule.TestRecord_JSValue;
  10381. begin
  10382. StartProgram(false);
  10383. Add([
  10384. 'type',
  10385. ' TRecord = record',
  10386. ' i: longint;',
  10387. ' end;',
  10388. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  10389. 'begin',
  10390. 'end;',
  10391. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  10392. 'begin',
  10393. ' if jsvalue(d) then ;',
  10394. ' if jsvalue(c) then ;',
  10395. ' if jsvalue(v) then ;',
  10396. 'end;',
  10397. 'var',
  10398. ' Jv: jsvalue;',
  10399. ' Rec: trecord;',
  10400. 'begin',
  10401. ' rec:=trecord(jv);',
  10402. ' jv:=rec;',
  10403. ' Fly(rec,rec);',
  10404. ' Fly(@rec,@rec);',
  10405. ' if jsvalue(Rec) then ;',
  10406. ' Run(trecord(jv),trecord(jv),rec);',
  10407. '']);
  10408. ConvertProgram;
  10409. CheckSource('TestRecord_JSValue',
  10410. LinesToStr([ // statements
  10411. 'rtl.recNewT($mod, "TRecord", function () {',
  10412. ' this.i = 0;',
  10413. ' this.$eq = function (b) {',
  10414. ' return this.i === b.i;',
  10415. ' };',
  10416. ' this.$assign = function (s) {',
  10417. ' this.i = s.i;',
  10418. ' return this;',
  10419. ' };',
  10420. '});',
  10421. 'this.Fly = function (d, c) {',
  10422. '};',
  10423. 'this.Run = function (d, c, v) {',
  10424. ' if (d) ;',
  10425. ' if (c) ;',
  10426. ' if (v) ;',
  10427. '};',
  10428. 'this.Jv = undefined;',
  10429. 'this.Rec = $mod.TRecord.$new();',
  10430. '']),
  10431. LinesToStr([
  10432. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  10433. '$mod.Jv = $mod.Rec;',
  10434. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  10435. '$mod.Fly($mod.Rec, $mod.Rec);',
  10436. 'if ($mod.Rec) ;',
  10437. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  10438. '']));
  10439. end;
  10440. procedure TTestModule.TestRecord_VariantFail;
  10441. begin
  10442. StartProgram(false);
  10443. Add([
  10444. 'type',
  10445. ' TRec = record',
  10446. ' case word of',
  10447. ' 0: (b0, b1: Byte);',
  10448. ' 1: (i: word);',
  10449. ' end;',
  10450. 'begin']);
  10451. SetExpectedPasResolverError('variant record is not supported',
  10452. nXIsNotSupported);
  10453. ConvertProgram;
  10454. end;
  10455. procedure TTestModule.TestRecord_FieldArray;
  10456. begin
  10457. StartProgram(false);
  10458. Add([
  10459. 'type',
  10460. ' TArrInt = array[3..4] of longint;',
  10461. ' TArrArrInt = array[3..4] of longint;',
  10462. ' TRec = record',
  10463. ' a: array of longint;',
  10464. ' s: array[1..2] of longint;',
  10465. ' m: array[1..2,3..4] of longint;',
  10466. ' o: TArrArrInt;',
  10467. ' end;',
  10468. 'begin']);
  10469. ConvertProgram;
  10470. CheckSource('TestRecord_FieldArray',
  10471. LinesToStr([ // statements
  10472. 'rtl.recNewT($mod, "TRec", function () {',
  10473. ' this.a = [];',
  10474. ' this.$new = function () {',
  10475. ' var r = Object.create(this);',
  10476. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10477. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10478. ' r.o = rtl.arraySetLength(null, 0, 2);',
  10479. ' return r;',
  10480. ' };',
  10481. ' this.$eq = function (b) {',
  10482. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  10483. ' };',
  10484. ' this.$assign = function (s) {',
  10485. ' this.a = s.a;',
  10486. ' this.s = s.s.slice(0);',
  10487. ' this.m = s.m.slice(0);',
  10488. ' this.o = s.o.slice(0);',
  10489. ' return this;',
  10490. ' };',
  10491. '});',
  10492. '']),
  10493. LinesToStr([ // $mod.$main
  10494. '']));
  10495. end;
  10496. procedure TTestModule.TestRecord_Const;
  10497. begin
  10498. StartProgram(false);
  10499. Add([
  10500. 'type',
  10501. ' TArrInt = array[3..4] of longint;',
  10502. ' TPoint = record x,y: longint; end;',
  10503. ' TRec = record',
  10504. ' i: longint;',
  10505. ' a: array of longint;',
  10506. ' s: array[1..2] of longint;',
  10507. ' m: array[1..2,3..4] of longint;',
  10508. ' p: TPoint;',
  10509. ' end;',
  10510. ' TPoints = array of TPoint;',
  10511. 'const',
  10512. ' r: TRec = (',
  10513. ' i:1;',
  10514. ' a:(2,3);',
  10515. ' s:(4,5);',
  10516. ' m:( (11,12), (13,14) );',
  10517. ' p: (x:21; y:22)',
  10518. ' );',
  10519. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10520. 'begin']);
  10521. ConvertProgram;
  10522. CheckSource('TestRecord_Const',
  10523. LinesToStr([ // statements
  10524. 'rtl.recNewT($mod, "TPoint", function () {',
  10525. ' this.x = 0;',
  10526. ' this.y = 0;',
  10527. ' this.$eq = function (b) {',
  10528. ' return (this.x === b.x) && (this.y === b.y);',
  10529. ' };',
  10530. ' this.$assign = function (s) {',
  10531. ' this.x = s.x;',
  10532. ' this.y = s.y;',
  10533. ' return this;',
  10534. ' };',
  10535. '});',
  10536. 'rtl.recNewT($mod, "TRec", function () {',
  10537. ' this.i = 0;',
  10538. ' this.a = [];',
  10539. ' this.$new = function () {',
  10540. ' var r = Object.create(this);',
  10541. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10542. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10543. ' r.p = $mod.TPoint.$new();',
  10544. ' return r;',
  10545. ' };',
  10546. ' this.$eq = function (b) {',
  10547. ' 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);',
  10548. ' };',
  10549. ' this.$assign = function (s) {',
  10550. ' this.i = s.i;',
  10551. ' this.a = s.a;',
  10552. ' this.s = s.s.slice(0);',
  10553. ' this.m = s.m.slice(0);',
  10554. ' this.p.$assign(s.p);',
  10555. ' return this;',
  10556. ' };',
  10557. '});',
  10558. 'this.r = $mod.TRec.$clone({',
  10559. ' i: 1,',
  10560. ' a: [2, 3],',
  10561. ' s: [4, 5],',
  10562. ' m: [[11, 12], [13, 14]],',
  10563. ' p: $mod.TPoint.$clone({',
  10564. ' x: 21,',
  10565. ' y: 22',
  10566. ' })',
  10567. '});',
  10568. 'this.p = [$mod.TPoint.$clone({',
  10569. ' x: 1,',
  10570. ' y: 2',
  10571. '}), $mod.TPoint.$clone({',
  10572. ' x: 3,',
  10573. ' y: 4',
  10574. '})];',
  10575. '']),
  10576. LinesToStr([ // $mod.$main
  10577. '']));
  10578. end;
  10579. procedure TTestModule.TestRecord_TypecastFail;
  10580. begin
  10581. StartProgram(false);
  10582. Add([
  10583. 'type',
  10584. ' TPoint = record x,y: longint; end;',
  10585. ' TRec = record l: longint end;',
  10586. 'var p: TPoint;',
  10587. 'begin',
  10588. ' if TRec(p).l=2 then ;']);
  10589. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  10590. nIllegalTypeConversionTo);
  10591. ConvertProgram;
  10592. end;
  10593. procedure TTestModule.TestRecord_InFunction;
  10594. begin
  10595. StartProgram(false);
  10596. Add([
  10597. 'var TPoint: longint = 3;',
  10598. 'procedure DoIt;',
  10599. 'type',
  10600. ' TPoint = record x,y: longint; end;',
  10601. ' TPoints = array of TPoint;',
  10602. 'var',
  10603. ' r: TPoint;',
  10604. ' p: TPoints;',
  10605. 'begin',
  10606. ' SetLength(p,2);',
  10607. 'end;',
  10608. 'begin']);
  10609. ConvertProgram;
  10610. CheckSource('TestRecord_InFunction',
  10611. LinesToStr([ // statements
  10612. 'this.TPoint = 3;',
  10613. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  10614. ' this.x = 0;',
  10615. ' this.y = 0;',
  10616. ' this.$eq = function (b) {',
  10617. ' return (this.x === b.x) && (this.y === b.y);',
  10618. ' };',
  10619. ' this.$assign = function (s) {',
  10620. ' this.x = s.x;',
  10621. ' this.y = s.y;',
  10622. ' return this;',
  10623. ' };',
  10624. '});',
  10625. 'this.DoIt = function () {',
  10626. ' var r = TPoint$1.$new();',
  10627. ' var p = [];',
  10628. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  10629. '};',
  10630. '']),
  10631. LinesToStr([ // $mod.$main
  10632. '']));
  10633. end;
  10634. procedure TTestModule.TestRecord_AnonymousFail;
  10635. begin
  10636. StartProgram(false);
  10637. Add([
  10638. 'var',
  10639. ' r: record x: word end;',
  10640. 'begin']);
  10641. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  10642. nNotYetImplemented);
  10643. ConvertProgram;
  10644. end;
  10645. procedure TTestModule.TestAdvRecord_Function;
  10646. begin
  10647. StartProgram(false);
  10648. Parser.Options:=Parser.Options+[po_cassignments];
  10649. Add([
  10650. '{$modeswitch AdvancedRecords}',
  10651. 'type',
  10652. ' TPoint = record',
  10653. ' x,y: word;',
  10654. ' function Add(const apt: TPoint): TPoint;',
  10655. ' end;',
  10656. 'function TPoint.Add(const apt: TPoint): TPoint;',
  10657. 'begin',
  10658. ' Result:=Self;',
  10659. ' Result.x+=apt.x;',
  10660. ' Result.y:=Result.y+apt.y;',
  10661. ' Self:=apt;',
  10662. 'end;',
  10663. 'var p,q: TPoint;',
  10664. 'begin',
  10665. ' p.add(q);',
  10666. ' p:=default(TPoint);',
  10667. ' p:=q;',
  10668. '']);
  10669. ConvertProgram;
  10670. CheckSource('TestAdvRecord_Function',
  10671. LinesToStr([ // statements
  10672. 'rtl.recNewT($mod, "TPoint", function () {',
  10673. ' this.x = 0;',
  10674. ' this.y = 0;',
  10675. ' this.$eq = function (b) {',
  10676. ' return (this.x === b.x) && (this.y === b.y);',
  10677. ' };',
  10678. ' this.$assign = function (s) {',
  10679. ' this.x = s.x;',
  10680. ' this.y = s.y;',
  10681. ' return this;',
  10682. ' };',
  10683. ' this.Add = function (apt) {',
  10684. ' var Result = $mod.TPoint.$new();',
  10685. ' Result.$assign(this);',
  10686. ' Result.x += apt.x;',
  10687. ' Result.y = Result.y + apt.y;',
  10688. ' this.$assign(apt);',
  10689. ' return Result;',
  10690. ' };',
  10691. '});',
  10692. 'this.p = $mod.TPoint.$new();',
  10693. 'this.q = $mod.TPoint.$new();',
  10694. '']),
  10695. LinesToStr([ // $mod.$main
  10696. '$mod.p.Add($mod.q);',
  10697. '$mod.p.$assign($mod.TPoint.$new());',
  10698. '$mod.p.$assign($mod.q);',
  10699. '']));
  10700. end;
  10701. procedure TTestModule.TestAdvRecord_Property;
  10702. begin
  10703. StartProgram(false);
  10704. Add([
  10705. '{$modeswitch AdvancedRecords}',
  10706. 'type',
  10707. ' TPoint = record',
  10708. ' x,y: word;',
  10709. ' strict private',
  10710. ' function GetSize: longword;',
  10711. ' procedure SetSize(Value: longword);',
  10712. ' public',
  10713. ' property Size: longword read GetSize write SetSize;',
  10714. ' property Left: word read x write y;',
  10715. ' end;',
  10716. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  10717. 'function TPoint.GetSize: longword;',
  10718. 'begin',
  10719. ' x:=y;',
  10720. ' Size:=Size;',
  10721. ' Left:=Left;',
  10722. 'end;',
  10723. 'procedure TPoint.SetSize(Value: longword);',
  10724. 'begin',
  10725. 'end;',
  10726. 'var p,q: TPoint;',
  10727. 'begin',
  10728. ' p.Size:=q.Size;',
  10729. ' p.Left:=q.Left;',
  10730. '']);
  10731. ConvertProgram;
  10732. CheckSource('TestAdvRecord_Property',
  10733. LinesToStr([ // statements
  10734. 'rtl.recNewT($mod, "TPoint", function () {',
  10735. ' this.x = 0;',
  10736. ' this.y = 0;',
  10737. ' this.$eq = function (b) {',
  10738. ' return (this.x === b.x) && (this.y === b.y);',
  10739. ' };',
  10740. ' this.$assign = function (s) {',
  10741. ' this.x = s.x;',
  10742. ' this.y = s.y;',
  10743. ' return this;',
  10744. ' };',
  10745. ' this.GetSize = function () {',
  10746. ' var Result = 0;',
  10747. ' this.x = this.y;',
  10748. ' this.SetSize(this.GetSize());',
  10749. ' this.y = this.x;',
  10750. ' return Result;',
  10751. ' };',
  10752. ' this.SetSize = function (Value) {',
  10753. ' };',
  10754. '});',
  10755. 'this.SetSize = function (Value) {',
  10756. '};',
  10757. 'this.p = $mod.TPoint.$new();',
  10758. 'this.q = $mod.TPoint.$new();',
  10759. '']),
  10760. LinesToStr([ // $mod.$main
  10761. '$mod.p.SetSize($mod.q.GetSize());',
  10762. '$mod.p.y = $mod.q.x;',
  10763. '']));
  10764. end;
  10765. procedure TTestModule.TestAdvRecord_PropertyDefault;
  10766. begin
  10767. StartProgram(false);
  10768. Add([
  10769. '{$modeswitch AdvancedRecords}',
  10770. 'type',
  10771. ' TPoint = record',
  10772. ' strict private',
  10773. ' function GetItems(Index: word): word;',
  10774. ' procedure SetItems(Index: word; Value: word);',
  10775. ' public',
  10776. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  10777. ' end;',
  10778. 'function TPoint.GetItems(Index: word): word;',
  10779. 'begin',
  10780. ' Items[index]:=Items[index];',
  10781. ' self.Items[index]:=self.Items[index];',
  10782. 'end;',
  10783. 'procedure TPoint.SetItems(Index: word; Value: word);',
  10784. 'begin',
  10785. 'end;',
  10786. 'var p: TPoint;',
  10787. 'begin',
  10788. ' p[1]:=p[2];',
  10789. ' p.Items[3]:=p.Items[4];',
  10790. '']);
  10791. ConvertProgram;
  10792. CheckSource('TestAdvRecord_PropertyDefault',
  10793. LinesToStr([ // statements
  10794. 'rtl.recNewT($mod, "TPoint", function () {',
  10795. ' this.$eq = function (b) {',
  10796. ' return true;',
  10797. ' };',
  10798. ' this.$assign = function (s) {',
  10799. ' return this;',
  10800. ' };',
  10801. ' this.GetItems = function (Index) {',
  10802. ' var Result = 0;',
  10803. ' this.SetItems(Index, this.GetItems(Index));',
  10804. ' this.SetItems(Index, this.GetItems(Index));',
  10805. ' return Result;',
  10806. ' };',
  10807. ' this.SetItems = function (Index, Value) {',
  10808. ' };',
  10809. '});',
  10810. 'this.p = $mod.TPoint.$new();',
  10811. '']),
  10812. LinesToStr([ // $mod.$main
  10813. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  10814. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  10815. '']));
  10816. end;
  10817. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  10818. begin
  10819. StartProgram(false);
  10820. Add([
  10821. '{$modeswitch AdvancedRecords}',
  10822. 'type',
  10823. ' TRec = record',
  10824. ' class var',
  10825. ' Fx: longint;',
  10826. ' Fy: longint;',
  10827. ' class function GetInt: longint; static;',
  10828. ' class procedure SetInt(Value: longint); static;',
  10829. ' class procedure DoIt; static;',
  10830. ' class property IntA: longint read Fx write Fy;',
  10831. ' class property IntB: longint read GetInt write SetInt;',
  10832. ' end;',
  10833. 'class function trec.getint: longint;',
  10834. 'begin',
  10835. ' result:=fx;',
  10836. 'end;',
  10837. 'class procedure trec.setint(value: longint);',
  10838. 'begin',
  10839. 'end;',
  10840. 'class procedure trec.doit;',
  10841. 'begin',
  10842. ' IntA:=IntA+1;',
  10843. ' IntB:=IntB+1;',
  10844. 'end;',
  10845. 'var r: trec;',
  10846. 'begin',
  10847. ' trec.inta:=trec.inta+1;',
  10848. ' if trec.intb=2 then;',
  10849. ' trec.intb:=trec.intb+2;',
  10850. ' trec.setint(trec.inta);',
  10851. ' r.inta:=r.inta+1;',
  10852. ' if r.intb=2 then;',
  10853. ' r.intb:=r.intb+2;',
  10854. ' r.setint(r.inta);']);
  10855. ConvertProgram;
  10856. CheckSource('TestAdvRecord_Property_ClassMethod',
  10857. LinesToStr([ // statements
  10858. 'rtl.recNewT($mod, "TRec", function () {',
  10859. ' this.Fx = 0;',
  10860. ' this.Fy = 0;',
  10861. ' this.$eq = function (b) {',
  10862. ' return true;',
  10863. ' };',
  10864. ' this.$assign = function (s) {',
  10865. ' return this;',
  10866. ' };',
  10867. ' this.GetInt = function () {',
  10868. ' var Result = 0;',
  10869. ' Result = $mod.TRec.Fx;',
  10870. ' return Result;',
  10871. ' };',
  10872. ' this.SetInt = function (Value) {',
  10873. ' };',
  10874. ' this.DoIt = function () {',
  10875. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  10876. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  10877. ' };',
  10878. '}, true);',
  10879. 'this.r = $mod.TRec.$new();',
  10880. '']),
  10881. LinesToStr([ // $mod.$main
  10882. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  10883. 'if ($mod.TRec.GetInt() === 2) ;',
  10884. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  10885. '$mod.TRec.SetInt($mod.TRec.Fx);',
  10886. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  10887. 'if ($mod.r.GetInt() === 2) ;',
  10888. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  10889. '$mod.r.SetInt($mod.r.Fx);',
  10890. '']));
  10891. end;
  10892. procedure TTestModule.TestAdvRecord_Const;
  10893. begin
  10894. StartProgram(false);
  10895. Add([
  10896. '{$modeswitch AdvancedRecords}',
  10897. 'type',
  10898. ' TArrInt = array[3..4] of longint;',
  10899. ' TPoint = record',
  10900. ' x,y: longint;',
  10901. ' class var Count: nativeint;',
  10902. ' end;',
  10903. ' TRec = record',
  10904. ' i: longint;',
  10905. ' a: array of longint;',
  10906. ' s: array[1..2] of longint;',
  10907. ' m: array[1..2,3..4] of longint;',
  10908. ' p: TPoint;',
  10909. ' end;',
  10910. ' TPoints = array of TPoint;',
  10911. 'const',
  10912. ' r: TRec = (',
  10913. ' i:1;',
  10914. ' a:(2,3);',
  10915. ' s:(4,5);',
  10916. ' m:( (11,12), (13,14) );',
  10917. ' p: (x:21)',
  10918. ' );',
  10919. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10920. 'begin']);
  10921. ConvertProgram;
  10922. CheckSource('TestAdvRecord_Const',
  10923. LinesToStr([ // statements
  10924. 'rtl.recNewT($mod, "TPoint", function () {',
  10925. ' this.x = 0;',
  10926. ' this.y = 0;',
  10927. ' this.Count = 0;',
  10928. ' this.$eq = function (b) {',
  10929. ' return (this.x === b.x) && (this.y === b.y);',
  10930. ' };',
  10931. ' this.$assign = function (s) {',
  10932. ' this.x = s.x;',
  10933. ' this.y = s.y;',
  10934. ' return this;',
  10935. ' };',
  10936. '}, true);',
  10937. 'rtl.recNewT($mod, "TRec", function () {',
  10938. ' this.i = 0;',
  10939. ' this.a = [];',
  10940. ' this.$new = function () {',
  10941. ' var r = Object.create(this);',
  10942. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10943. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10944. ' r.p = $mod.TPoint.$new();',
  10945. ' return r;',
  10946. ' };',
  10947. ' this.$eq = function (b) {',
  10948. ' 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);',
  10949. ' };',
  10950. ' this.$assign = function (s) {',
  10951. ' this.i = s.i;',
  10952. ' this.a = s.a;',
  10953. ' this.s = s.s.slice(0);',
  10954. ' this.m = s.m.slice(0);',
  10955. ' this.p.$assign(s.p);',
  10956. ' return this;',
  10957. ' };',
  10958. '});',
  10959. 'this.r = $mod.TRec.$clone({',
  10960. ' i: 1,',
  10961. ' a: [2, 3],',
  10962. ' s: [4, 5],',
  10963. ' m: [[11, 12], [13, 14]],',
  10964. ' p: $mod.TPoint.$clone({',
  10965. ' x: 21,',
  10966. ' y: 0',
  10967. ' })',
  10968. '});',
  10969. 'this.p = [$mod.TPoint.$clone({',
  10970. ' x: 1,',
  10971. ' y: 2',
  10972. '}), $mod.TPoint.$clone({',
  10973. ' x: 3,',
  10974. ' y: 4',
  10975. '})];',
  10976. '']),
  10977. LinesToStr([ // $mod.$main
  10978. '']));
  10979. end;
  10980. procedure TTestModule.TestAdvRecord_ExternalField;
  10981. begin
  10982. StartProgram(false);
  10983. Add([
  10984. '{$modeswitch AdvancedRecords}',
  10985. '{$modeswitch externalclass}',
  10986. 'type',
  10987. ' TCar = record',
  10988. ' public',
  10989. ' Intern: longint external name ''$Intern'';',
  10990. ' Intern2: longint external name ''$Intern2'';',
  10991. ' Bracket: longint external name ''["A B"]'';',
  10992. ' procedure DoIt;',
  10993. ' end;',
  10994. 'procedure tcar.doit;',
  10995. 'begin',
  10996. ' Intern:=Intern+1;',
  10997. ' Intern2:=Intern2+2;',
  10998. ' Bracket:=Bracket+3;',
  10999. 'end;',
  11000. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11001. 'begin',
  11002. ' Rec.intern:=Rec.intern+1;',
  11003. ' Rec.intern2:=Rec.intern2+2;',
  11004. ' Rec.Bracket:=Rec.Bracket+3;',
  11005. ' with Rec do begin',
  11006. ' intern:=intern+1;',
  11007. ' intern2:=intern2+2;',
  11008. ' Bracket:=Bracket+3;',
  11009. ' end;']);
  11010. ConvertProgram;
  11011. CheckSource('TestAdvRecord_ExternalField',
  11012. LinesToStr([ // statements
  11013. 'rtl.recNewT($mod, "TCar", function () {',
  11014. ' this.$eq = function (b) {',
  11015. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11016. ' };',
  11017. ' this.$assign = function (s) {',
  11018. ' this.$Intern = s.$Intern;',
  11019. ' this.$Intern2 = s.$Intern2;',
  11020. ' this["A B"] = s["A B"];',
  11021. ' return this;',
  11022. ' };',
  11023. ' this.DoIt = function () {',
  11024. ' this.$Intern = this.$Intern + 1;',
  11025. ' this.$Intern2 = this.$Intern2 + 2;',
  11026. ' this["A B"] = this["A B"] + 3;',
  11027. ' };',
  11028. '});',
  11029. 'this.Rec = $mod.TCar.$clone({',
  11030. ' $Intern: 11,',
  11031. ' $Intern2: 12,',
  11032. ' "A B": 13',
  11033. '});',
  11034. '']),
  11035. LinesToStr([ // $mod.$main
  11036. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11037. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11038. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11039. 'var $with1 = $mod.Rec;',
  11040. '$with1.$Intern = $with1.$Intern + 1;',
  11041. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  11042. '$with1["A B"] = $with1["A B"] + 3;',
  11043. '']));
  11044. end;
  11045. procedure TTestModule.TestAdvRecord_SubRecord;
  11046. begin
  11047. StartProgram(false);
  11048. Add([
  11049. '{$modeswitch AdvancedRecords}',
  11050. 'type',
  11051. ' TRec = record',
  11052. ' type',
  11053. ' TPoint = record',
  11054. ' x,y: longint;',
  11055. ' class var Count: nativeint;',
  11056. ' procedure DoIt;',
  11057. ' class procedure DoThat; static;',
  11058. ' end;',
  11059. ' var',
  11060. ' i: longint;',
  11061. ' p: TPoint;',
  11062. ' procedure DoSome;',
  11063. ' end;',
  11064. 'const',
  11065. ' r: TRec = (',
  11066. ' i:1;',
  11067. ' p: (x:21;y:22)',
  11068. ' );',
  11069. 'procedure TRec.DoSome;',
  11070. 'begin',
  11071. ' p.x:=p.y+1;',
  11072. ' p.Count:=p.Count+2;',
  11073. 'end;',
  11074. 'procedure TRec.TPoint.DoIt;',
  11075. 'begin',
  11076. ' Count:=Count+3;',
  11077. 'end;',
  11078. 'class procedure TRec.TPoint.DoThat;',
  11079. 'begin',
  11080. ' Count:=Count+4;',
  11081. 'end;',
  11082. 'begin']);
  11083. ConvertProgram;
  11084. CheckSource('TestAdvRecord_SubRecord',
  11085. LinesToStr([ // statements
  11086. 'rtl.recNewT($mod, "TRec", function () {',
  11087. ' rtl.recNewT(this, "TPoint", function () {',
  11088. ' this.x = 0;',
  11089. ' this.y = 0;',
  11090. ' this.Count = 0;',
  11091. ' this.$eq = function (b) {',
  11092. ' return (this.x === b.x) && (this.y === b.y);',
  11093. ' };',
  11094. ' this.$assign = function (s) {',
  11095. ' this.x = s.x;',
  11096. ' this.y = s.y;',
  11097. ' return this;',
  11098. ' };',
  11099. ' this.DoIt = function () {',
  11100. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11101. ' };',
  11102. ' this.DoThat = function () {',
  11103. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11104. ' };',
  11105. ' }, true);',
  11106. ' this.i = 0;',
  11107. ' this.$new = function () {',
  11108. ' var r = Object.create(this);',
  11109. ' r.p = this.TPoint.$new();',
  11110. ' return r;',
  11111. ' };',
  11112. ' this.$eq = function (b) {',
  11113. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11114. ' };',
  11115. ' this.$assign = function (s) {',
  11116. ' this.i = s.i;',
  11117. ' this.p.$assign(s.p);',
  11118. ' return this;',
  11119. ' };',
  11120. ' this.DoSome = function () {',
  11121. ' this.p.x = this.p.y + 1;',
  11122. ' this.TPoint.Count = this.p.Count + 2;',
  11123. ' };',
  11124. '}, true);',
  11125. 'this.r = $mod.TRec.$clone({',
  11126. ' i: 1,',
  11127. ' p: $mod.TRec.TPoint.$clone({',
  11128. ' x: 21,',
  11129. ' y: 22',
  11130. ' })',
  11131. '});',
  11132. '']),
  11133. LinesToStr([ // $mod.$main
  11134. '']));
  11135. end;
  11136. procedure TTestModule.TestAdvRecord_SubClass;
  11137. begin
  11138. StartProgram(false);
  11139. Add([
  11140. '{$modeswitch AdvancedRecords}',
  11141. 'type',
  11142. ' TObject = class end;',
  11143. ' TPoint = record',
  11144. ' type',
  11145. ' TBird = class',
  11146. ' procedure DoIt;',
  11147. ' class procedure Glob;',
  11148. ' end;',
  11149. ' procedure DoIt(b: TBird);',
  11150. ' end;',
  11151. 'procedure TPoint.TBird.DoIt;',
  11152. 'begin',
  11153. ' doit;',
  11154. ' self.doit;',
  11155. ' glob;',
  11156. ' self.glob;',
  11157. 'end;',
  11158. 'class procedure TPoint.TBird.Glob;',
  11159. 'begin',
  11160. ' glob;',
  11161. ' self.glob;',
  11162. 'end;',
  11163. 'procedure TPoint.DoIt(b: TBird);',
  11164. 'begin',
  11165. ' b.doit;',
  11166. ' b.glob;',
  11167. ' TBird.glob;',
  11168. 'end;',
  11169. 'begin',
  11170. '']);
  11171. ConvertProgram;
  11172. CheckSource('TestAdvRecord_SubClass',
  11173. LinesToStr([ // statements
  11174. 'rtl.createClass($mod, "TObject", null, function () {',
  11175. ' this.$init = function () {',
  11176. ' };',
  11177. ' this.$final = function () {',
  11178. ' };',
  11179. '});',
  11180. 'rtl.recNewT($mod, "TPoint", function () {',
  11181. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11182. ' this.DoIt = function () {',
  11183. ' this.DoIt();',
  11184. ' this.DoIt();',
  11185. ' this.$class.Glob();',
  11186. ' this.$class.Glob();',
  11187. ' };',
  11188. ' this.Glob = function () {',
  11189. ' this.Glob();',
  11190. ' this.Glob();',
  11191. ' };',
  11192. ' });',
  11193. ' this.$eq = function (b) {',
  11194. ' return true;',
  11195. ' };',
  11196. ' this.$assign = function (s) {',
  11197. ' return this;',
  11198. ' };',
  11199. ' this.DoIt = function (b) {',
  11200. ' b.DoIt();',
  11201. ' b.$class.Glob();',
  11202. ' this.TBird.Glob();',
  11203. ' };',
  11204. '}, true);',
  11205. '']),
  11206. LinesToStr([ // $mod.$main
  11207. '']));
  11208. end;
  11209. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11210. begin
  11211. StartProgram(false);
  11212. Add([
  11213. '{$modeswitch AdvancedRecords}',
  11214. 'type',
  11215. ' IUnknown = interface end;',
  11216. ' TPoint = record',
  11217. ' type IBird = interface end;',
  11218. ' end;',
  11219. 'begin',
  11220. '']);
  11221. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  11222. nNotYetImplemented);
  11223. ParseProgram;
  11224. end;
  11225. procedure TTestModule.TestAdvRecord_Constructor;
  11226. begin
  11227. StartProgram(false);
  11228. Add([
  11229. '{$modeswitch AdvancedRecords}',
  11230. 'type',
  11231. ' TPoint = record',
  11232. ' x,y: longint;',
  11233. ' constructor Create(ax: longint; ay: longint = -1);',
  11234. ' end;',
  11235. 'constructor tpoint.create(ax,ay: longint);',
  11236. 'begin',
  11237. ' x:=ax;',
  11238. ' self.y:=ay;',
  11239. 'end;',
  11240. 'var r: TPoint;',
  11241. 'begin',
  11242. ' r:=TPoint.Create(1,2);',
  11243. ' with TPoint do r:=Create(1,2);',
  11244. ' r.Create(3);',
  11245. ' r:=r.Create(4);',
  11246. '']);
  11247. ConvertProgram;
  11248. CheckSource('TestAdvRecord_Constructor',
  11249. LinesToStr([ // statements
  11250. 'rtl.recNewT($mod, "TPoint", function () {',
  11251. ' this.x = 0;',
  11252. ' this.y = 0;',
  11253. ' this.$eq = function (b) {',
  11254. ' return (this.x === b.x) && (this.y === b.y);',
  11255. ' };',
  11256. ' this.$assign = function (s) {',
  11257. ' this.x = s.x;',
  11258. ' this.y = s.y;',
  11259. ' return this;',
  11260. ' };',
  11261. ' this.Create = function (ax, ay) {',
  11262. ' this.x = ax;',
  11263. ' this.y = ay;',
  11264. ' return this;',
  11265. ' };',
  11266. '}, true);',
  11267. 'this.r = $mod.TPoint.$new();',
  11268. '']),
  11269. LinesToStr([ // $mod.$main
  11270. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11271. 'var $with1 = $mod.TPoint;',
  11272. '$mod.r.$assign($with1.$new().Create(1, 2));',
  11273. '$mod.r.Create(3, -1);',
  11274. '$mod.r.$assign($mod.r.Create(4, -1));',
  11275. '']));
  11276. end;
  11277. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  11278. begin
  11279. StartProgram(false);
  11280. Add([
  11281. '{$modeswitch AdvancedRecords}',
  11282. 'type',
  11283. ' TPoint = record',
  11284. ' class var x: longint;',
  11285. ' class procedure Fly; static;',
  11286. ' class constructor Init;',
  11287. ' end;',
  11288. 'var count: word;',
  11289. 'class procedure Tpoint.Fly;',
  11290. 'begin',
  11291. 'end;',
  11292. 'class constructor tpoint.init;',
  11293. 'begin',
  11294. ' count:=count+1;',
  11295. ' x:=x+3;',
  11296. ' tpoint.x:=tpoint.x+4;',
  11297. ' fly;',
  11298. ' tpoint.fly;',
  11299. 'end;',
  11300. 'var r: TPoint;',
  11301. 'begin',
  11302. ' r.x:=r.x+10;',
  11303. ' r.Fly;',
  11304. ' r.Fly();',
  11305. '']);
  11306. ConvertProgram;
  11307. CheckSource('TestAdvRecord_ClassConstructor_Program',
  11308. LinesToStr([ // statements
  11309. 'rtl.recNewT($mod, "TPoint", function () {',
  11310. ' this.x = 0;',
  11311. ' this.$eq = function (b) {',
  11312. ' return true;',
  11313. ' };',
  11314. ' this.$assign = function (s) {',
  11315. ' return this;',
  11316. ' };',
  11317. ' this.Fly = function () {',
  11318. ' };',
  11319. '}, true);',
  11320. 'this.count = 0;',
  11321. 'this.r = $mod.TPoint.$new();',
  11322. '']),
  11323. LinesToStr([ // $mod.$main
  11324. '(function () {',
  11325. ' $mod.count = $mod.count + 1;',
  11326. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  11327. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  11328. ' $mod.TPoint.Fly();',
  11329. ' $mod.TPoint.Fly();',
  11330. '})();',
  11331. '$mod.TPoint.x = $mod.r.x + 10;',
  11332. '$mod.r.Fly();',
  11333. '$mod.r.Fly();',
  11334. '']));
  11335. end;
  11336. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  11337. begin
  11338. StartUnit(false);
  11339. Add([
  11340. 'interface',
  11341. '{$modeswitch AdvancedRecords}',
  11342. 'type',
  11343. ' TPoint = record',
  11344. ' class var x: longint;',
  11345. ' class procedure Fly; static;',
  11346. ' class constructor Init;',
  11347. ' end;',
  11348. 'implementation',
  11349. 'var count: word;',
  11350. 'class procedure Tpoint.Fly;',
  11351. 'begin',
  11352. 'end;',
  11353. 'class constructor tpoint.init;',
  11354. 'begin',
  11355. ' count:=count+1;',
  11356. ' x:=3;',
  11357. ' tpoint.x:=4;',
  11358. ' fly;',
  11359. ' tpoint.fly;',
  11360. 'end;',
  11361. '']);
  11362. ConvertUnit;
  11363. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  11364. LinesToStr([ // statements
  11365. 'var $impl = $mod.$impl;',
  11366. 'rtl.recNewT($mod, "TPoint", function () {',
  11367. ' this.x = 0;',
  11368. ' this.$eq = function (b) {',
  11369. ' return true;',
  11370. ' };',
  11371. ' this.$assign = function (s) {',
  11372. ' return this;',
  11373. ' };',
  11374. ' this.Fly = function () {',
  11375. ' };',
  11376. '}, true);',
  11377. '']),
  11378. LinesToStr([ // $mod.$init
  11379. '(function () {',
  11380. ' $impl.count = $impl.count + 1;',
  11381. ' $mod.TPoint.x = 3;',
  11382. ' $mod.TPoint.x = 4;',
  11383. ' $mod.TPoint.Fly();',
  11384. ' $mod.TPoint.Fly();',
  11385. '})();',
  11386. '']),
  11387. LinesToStr([ // $mod.$main
  11388. '$impl.count = 0;',
  11389. '']));
  11390. end;
  11391. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  11392. begin
  11393. StartProgram(false);
  11394. Add(['type',
  11395. ' TObject = class',
  11396. ' public',
  11397. ' constructor Create;',
  11398. ' destructor Destroy;',
  11399. ' end;',
  11400. ' TBird = TObject;',
  11401. 'constructor tobject.create;',
  11402. 'begin end;',
  11403. 'destructor tobject.destroy;',
  11404. 'begin end;',
  11405. 'var Obj: tobject;',
  11406. 'begin',
  11407. ' obj:=tobject.create;',
  11408. ' obj:=tobject.create();',
  11409. ' obj:=tbird.create;',
  11410. ' obj:=tbird.create();',
  11411. ' obj:=obj.create();',
  11412. ' obj.destroy;',
  11413. '']);
  11414. ConvertProgram;
  11415. CheckSource('TestClass_TObjectDefaultConstructor',
  11416. LinesToStr([ // statements
  11417. 'rtl.createClass($mod,"TObject",null,function(){',
  11418. ' this.$init = function () {',
  11419. ' };',
  11420. ' this.$final = function () {',
  11421. ' };',
  11422. ' this.Create = function(){',
  11423. ' return this;',
  11424. ' };',
  11425. ' this.Destroy = function(){',
  11426. ' };',
  11427. '});',
  11428. 'this.Obj = null;'
  11429. ]),
  11430. LinesToStr([ // $mod.$main
  11431. '$mod.Obj = $mod.TObject.$create("Create");',
  11432. '$mod.Obj = $mod.TObject.$create("Create");',
  11433. '$mod.Obj = $mod.TObject.$create("Create");',
  11434. '$mod.Obj = $mod.TObject.$create("Create");',
  11435. '$mod.Obj = $mod.Obj.Create();',
  11436. '$mod.Obj.$destroy("Destroy");',
  11437. '']));
  11438. end;
  11439. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  11440. begin
  11441. StartProgram(false);
  11442. Add('type');
  11443. Add(' TObject = class');
  11444. Add(' public');
  11445. Add(' constructor Create(Par: longint);');
  11446. Add(' end;');
  11447. Add('constructor tobject.create(par: longint);');
  11448. Add('begin end;');
  11449. Add('var Obj: tobject;');
  11450. Add('begin');
  11451. Add(' obj:=tobject.create(3);');
  11452. ConvertProgram;
  11453. CheckSource('TestClass_TObjectConstructorWithParams',
  11454. LinesToStr([ // statements
  11455. 'rtl.createClass($mod,"TObject",null,function(){',
  11456. ' this.$init = function () {',
  11457. ' };',
  11458. ' this.$final = function () {',
  11459. ' };',
  11460. ' this.Create = function(Par){',
  11461. ' return this;',
  11462. ' };',
  11463. '});',
  11464. 'this.Obj = null;'
  11465. ]),
  11466. LinesToStr([ // $mod.$main
  11467. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  11468. ]));
  11469. end;
  11470. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  11471. begin
  11472. StartProgram(false);
  11473. Add('type');
  11474. Add(' TObject = class');
  11475. Add(' public');
  11476. Add(' constructor Create;');
  11477. Add(' end;');
  11478. Add(' TTest = class(TObject)');
  11479. Add(' public');
  11480. Add(' constructor Create(const Par: longint = 1);');
  11481. Add(' end;');
  11482. Add('constructor tobject.create;');
  11483. Add('begin end;');
  11484. Add('constructor ttest.create(const par: longint);');
  11485. Add('begin end;');
  11486. Add('var t: ttest;');
  11487. Add('begin');
  11488. Add(' t:=ttest.create;');
  11489. Add(' t:=ttest.create(2);');
  11490. ConvertProgram;
  11491. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  11492. LinesToStr([ // statements
  11493. 'rtl.createClass($mod,"TObject",null,function(){',
  11494. ' this.$init = function () {',
  11495. ' };',
  11496. ' this.$final = function () {',
  11497. ' };',
  11498. ' this.Create = function(){',
  11499. ' return this;',
  11500. ' };',
  11501. '});',
  11502. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  11503. ' this.Create$1 = function (Par) {',
  11504. ' return this;',
  11505. ' };',
  11506. '});',
  11507. 'this.t = null;'
  11508. ]),
  11509. LinesToStr([ // $mod.$main
  11510. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  11511. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  11512. ]));
  11513. end;
  11514. procedure TTestModule.TestClass_Var;
  11515. begin
  11516. StartProgram(false);
  11517. Add([
  11518. 'type',
  11519. ' TObject = class',
  11520. ' public',
  11521. ' vI: longint;',
  11522. ' constructor Create(Par: longint);',
  11523. ' end;',
  11524. 'constructor tobject.create(par: longint);',
  11525. 'begin',
  11526. ' vi:=par+3',
  11527. 'end;',
  11528. 'var Obj: tobject;',
  11529. 'begin',
  11530. ' obj:=tobject.create(4);',
  11531. ' obj.vi:=obj.VI+5;']);
  11532. ConvertProgram;
  11533. CheckSource('TestClass_Var',
  11534. LinesToStr([ // statements
  11535. 'rtl.createClass($mod,"TObject",null,function(){',
  11536. ' this.$init = function () {',
  11537. ' this.vI = 0;',
  11538. ' };',
  11539. ' this.$final = function () {',
  11540. ' };',
  11541. ' this.Create = function(Par){',
  11542. ' this.vI = Par+3;',
  11543. ' return this;',
  11544. ' };',
  11545. '});',
  11546. 'this.Obj = null;'
  11547. ]),
  11548. LinesToStr([ // $mod.$main
  11549. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  11550. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  11551. ]));
  11552. end;
  11553. procedure TTestModule.TestClass_Method;
  11554. begin
  11555. StartProgram(false);
  11556. Add('type');
  11557. Add(' TObject = class');
  11558. Add(' public');
  11559. Add(' vI: longint;');
  11560. Add(' Sub: TObject;');
  11561. Add(' constructor Create;');
  11562. Add(' function GetIt(Par: longint): tobject;');
  11563. Add(' end;');
  11564. Add('constructor tobject.create; begin end;');
  11565. Add('function tobject.getit(par: longint): tobject;');
  11566. Add('begin');
  11567. Add(' Self.vi:=par+3;');
  11568. Add(' Result:=self.sub;');
  11569. Add('end;');
  11570. Add('var Obj: tobject;');
  11571. Add('begin');
  11572. Add(' obj:=tobject.create;');
  11573. Add(' obj.getit(4);');
  11574. Add(' obj.sub.sub:=nil;');
  11575. Add(' obj.sub.getit(5);');
  11576. Add(' obj.sub.getit(6).SUB:=nil;');
  11577. Add(' obj.sub.getit(7).GETIT(8);');
  11578. Add(' obj.sub.getit(9).SuB.getit(10);');
  11579. ConvertProgram;
  11580. CheckSource('TestClass_Method',
  11581. LinesToStr([ // statements
  11582. 'rtl.createClass($mod,"TObject",null,function(){',
  11583. ' this.$init = function () {',
  11584. ' this.vI = 0;',
  11585. ' this.Sub = null;',
  11586. ' };',
  11587. ' this.$final = function () {',
  11588. ' this.Sub = undefined;',
  11589. ' };',
  11590. ' this.Create = function(){',
  11591. ' return this;',
  11592. ' };',
  11593. ' this.GetIt = function(Par){',
  11594. ' var Result = null;',
  11595. ' this.vI = Par + 3;',
  11596. ' Result = this.Sub;',
  11597. ' return Result;',
  11598. ' };',
  11599. '});',
  11600. 'this.Obj = null;'
  11601. ]),
  11602. LinesToStr([ // $mod.$main
  11603. '$mod.Obj = $mod.TObject.$create("Create");',
  11604. '$mod.Obj.GetIt(4);',
  11605. '$mod.Obj.Sub.Sub=null;',
  11606. '$mod.Obj.Sub.GetIt(5);',
  11607. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  11608. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  11609. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  11610. ]));
  11611. end;
  11612. procedure TTestModule.TestClass_Implementation;
  11613. begin
  11614. StartUnit(false);
  11615. Add([
  11616. 'interface',
  11617. 'type',
  11618. ' TObject = class',
  11619. ' constructor Create;',
  11620. ' end;',
  11621. 'implementation',
  11622. 'type',
  11623. ' TIntClass = class',
  11624. ' constructor Create; reintroduce;',
  11625. ' class procedure DoGlob;',
  11626. ' end;',
  11627. 'constructor tintclass.create;',
  11628. 'begin',
  11629. ' inherited;',
  11630. ' inherited create;',
  11631. ' doglob;',
  11632. 'end;',
  11633. 'class procedure tintclass.doglob;',
  11634. 'begin',
  11635. 'end;',
  11636. 'constructor tobject.create;',
  11637. 'var',
  11638. ' iC: tintclass;',
  11639. 'begin',
  11640. ' ic:=tintclass.create;',
  11641. ' tintclass.doglob;',
  11642. ' ic.doglob;',
  11643. 'end;',
  11644. 'initialization',
  11645. ' tintclass.doglob;',
  11646. '']);
  11647. ConvertUnit;
  11648. CheckSource('TestClass_Implementation',
  11649. LinesToStr([ // statements
  11650. 'var $impl = $mod.$impl;',
  11651. 'rtl.createClass($mod, "TObject", null, function () {',
  11652. ' this.$init = function () {',
  11653. ' };',
  11654. ' this.$final = function () {',
  11655. ' };',
  11656. ' this.Create = function () {',
  11657. ' var iC = null;',
  11658. ' iC = $impl.TIntClass.$create("Create$1");',
  11659. ' $impl.TIntClass.DoGlob();',
  11660. ' iC.$class.DoGlob();',
  11661. ' return this;',
  11662. ' };',
  11663. '});',
  11664. '']),
  11665. LinesToStr([ // $mod.$main
  11666. '$impl.TIntClass.DoGlob();',
  11667. '']),
  11668. LinesToStr([
  11669. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  11670. ' this.Create$1 = function () {',
  11671. ' $mod.TObject.Create.call(this);',
  11672. ' $mod.TObject.Create.call(this);',
  11673. ' this.$class.DoGlob();',
  11674. ' return this;',
  11675. ' };',
  11676. ' this.DoGlob = function () {',
  11677. ' };',
  11678. '});',
  11679. '']));
  11680. end;
  11681. procedure TTestModule.TestClass_Inheritance;
  11682. begin
  11683. StartProgram(false);
  11684. Add('type');
  11685. Add(' TObject = class');
  11686. Add(' public');
  11687. Add(' constructor Create;');
  11688. Add(' end;');
  11689. Add(' TClassA = class');
  11690. Add(' end;');
  11691. Add(' TClassB = class(TObject)');
  11692. Add(' procedure ProcB;');
  11693. Add(' end;');
  11694. Add('constructor tobject.create; begin end;');
  11695. Add('procedure tclassb.procb; begin end;');
  11696. Add('var');
  11697. Add(' oO: TObject;');
  11698. Add(' oA: TClassA;');
  11699. Add(' oB: TClassB;');
  11700. Add('begin');
  11701. Add(' oO:=tobject.Create;');
  11702. Add(' oA:=tclassa.Create;');
  11703. Add(' ob:=tclassb.Create;');
  11704. Add(' if oo is tclassa then ;');
  11705. Add(' ob:=oo as tclassb;');
  11706. Add(' (oo as tclassb).procb;');
  11707. ConvertProgram;
  11708. CheckSource('TestClass_Inheritance',
  11709. LinesToStr([ // statements
  11710. 'rtl.createClass($mod,"TObject",null,function(){',
  11711. ' this.$init = function () {',
  11712. ' };',
  11713. ' this.$final = function () {',
  11714. ' };',
  11715. ' this.Create = function () {',
  11716. ' return this;',
  11717. ' };',
  11718. '});',
  11719. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  11720. '});',
  11721. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  11722. ' this.ProcB = function () {',
  11723. ' };',
  11724. '});',
  11725. 'this.oO = null;',
  11726. 'this.oA = null;',
  11727. 'this.oB = null;'
  11728. ]),
  11729. LinesToStr([ // $mod.$main
  11730. '$mod.oO = $mod.TObject.$create("Create");',
  11731. '$mod.oA = $mod.TClassA.$create("Create");',
  11732. '$mod.oB = $mod.TClassB.$create("Create");',
  11733. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  11734. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  11735. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  11736. ]));
  11737. end;
  11738. procedure TTestModule.TestClass_TypeAlias;
  11739. begin
  11740. StartProgram(false);
  11741. Add([
  11742. '{$interfaces corba}',
  11743. 'type',
  11744. ' IObject = interface',
  11745. ' end;',
  11746. ' IBird = type IObject;',
  11747. ' TObject = class',
  11748. ' end;',
  11749. ' TBird = type TObject;',
  11750. 'var',
  11751. ' oObj: TObject;',
  11752. ' oBird: TBird;',
  11753. ' IntfObj: IObject;',
  11754. ' IntfBird: IBird;',
  11755. 'begin',
  11756. ' oObj:=oBird;',
  11757. '']);
  11758. ConvertProgram;
  11759. CheckSource('TestClass_TypeAlias',
  11760. LinesToStr([ // statements
  11761. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  11762. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  11763. 'rtl.createClass($mod, "TObject", null, function () {',
  11764. ' this.$init = function () {',
  11765. ' };',
  11766. ' this.$final = function () {',
  11767. ' };',
  11768. '});',
  11769. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11770. '});',
  11771. 'this.oObj = null;',
  11772. 'this.oBird = null;',
  11773. 'this.IntfObj = null;',
  11774. 'this.IntfBird = null;',
  11775. '']),
  11776. LinesToStr([ // $mod.$main
  11777. '$mod.oObj = $mod.oBird;',
  11778. '']));
  11779. end;
  11780. procedure TTestModule.TestClass_AbstractMethod;
  11781. begin
  11782. StartProgram(false);
  11783. Add('type');
  11784. Add(' TObject = class');
  11785. Add(' public');
  11786. Add(' procedure DoIt; virtual; abstract;');
  11787. Add(' end;');
  11788. Add('begin');
  11789. ConvertProgram;
  11790. CheckSource('TestClass_AbstractMethod',
  11791. LinesToStr([ // statements
  11792. 'rtl.createClass($mod,"TObject",null,function(){',
  11793. ' this.$init = function () {',
  11794. ' };',
  11795. ' this.$final = function () {',
  11796. ' };',
  11797. '});'
  11798. ]),
  11799. LinesToStr([ // this.$main
  11800. ''
  11801. ]));
  11802. end;
  11803. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  11804. begin
  11805. StartProgram(false);
  11806. Add([
  11807. 'type',
  11808. ' TObject = class',
  11809. ' procedure DoAbstract; virtual; abstract;',
  11810. ' procedure DoVirtual; virtual;',
  11811. ' procedure DoIt;',
  11812. ' end;',
  11813. ' TA = class',
  11814. ' procedure doabstract; override;',
  11815. ' procedure dovirtual; override;',
  11816. ' procedure DoSome;',
  11817. ' end;',
  11818. 'procedure tobject.dovirtual;',
  11819. 'begin',
  11820. ' inherited; // call non existing ancestor -> ignore silently',
  11821. 'end;',
  11822. 'procedure tobject.doit;',
  11823. 'begin',
  11824. 'end;',
  11825. 'procedure ta.doabstract;',
  11826. 'begin',
  11827. ' inherited dovirtual; // call TObject.DoVirtual',
  11828. 'end;',
  11829. 'procedure ta.dovirtual;',
  11830. 'begin',
  11831. ' inherited; // call TObject.DoVirtual',
  11832. ' inherited dovirtual; // call TObject.DoVirtual',
  11833. ' inherited dovirtual(); // call TObject.DoVirtual',
  11834. ' doit;',
  11835. ' doit();',
  11836. 'end;',
  11837. 'procedure ta.dosome;',
  11838. 'begin',
  11839. ' inherited; // call non existing ancestor method -> silently ignore',
  11840. 'end;',
  11841. 'begin']);
  11842. ConvertProgram;
  11843. CheckSource('TestClass_CallInherited_ProcNoParams',
  11844. LinesToStr([ // statements
  11845. 'rtl.createClass($mod,"TObject",null,function(){',
  11846. ' this.$init = function () {',
  11847. ' };',
  11848. ' this.$final = function () {',
  11849. ' };',
  11850. ' this.DoVirtual = function () {',
  11851. ' };',
  11852. ' this.DoIt = function () {',
  11853. ' };',
  11854. '});',
  11855. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11856. ' this.DoAbstract = function () {',
  11857. ' $mod.TObject.DoVirtual.call(this);',
  11858. ' };',
  11859. ' this.DoVirtual = function () {',
  11860. ' $mod.TObject.DoVirtual.call(this);',
  11861. ' $mod.TObject.DoVirtual.call(this);',
  11862. ' $mod.TObject.DoVirtual.call(this);',
  11863. ' this.DoIt();',
  11864. ' this.DoIt();',
  11865. ' };',
  11866. ' this.DoSome = function () {',
  11867. ' };',
  11868. '});'
  11869. ]),
  11870. LinesToStr([ // this.$main
  11871. ''
  11872. ]));
  11873. end;
  11874. procedure TTestModule.TestClass_CallInherited_WithParams;
  11875. begin
  11876. StartProgram(false);
  11877. Add([
  11878. 'type',
  11879. ' TObject = class',
  11880. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  11881. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  11882. ' procedure DoIt(pA: longint; pB: longint = 0);',
  11883. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  11884. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11885. ' end;',
  11886. ' TClassA = class',
  11887. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  11888. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  11889. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11890. ' end;',
  11891. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  11892. 'begin',
  11893. 'end;',
  11894. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  11895. 'begin',
  11896. 'end;',
  11897. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  11898. 'begin',
  11899. 'end;',
  11900. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  11901. 'begin',
  11902. 'end;',
  11903. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  11904. 'begin',
  11905. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11906. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11907. 'end;',
  11908. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  11909. 'begin',
  11910. ' inherited; // call TObject.DoVirtual(pA,pB)',
  11911. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11912. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11913. ' doit(pa,pb);',
  11914. ' doit(pa);',
  11915. ' doit2(pa);',
  11916. ' doit2;',
  11917. 'end;',
  11918. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  11919. 'begin',
  11920. ' pa:=inherited;',
  11921. 'end;',
  11922. 'begin']);
  11923. ConvertProgram;
  11924. CheckSource('TestClass_CallInherited_WithParams',
  11925. LinesToStr([ // statements
  11926. 'rtl.createClass($mod,"TObject",null,function(){',
  11927. ' this.$init = function () {',
  11928. ' };',
  11929. ' this.$final = function () {',
  11930. ' };',
  11931. ' this.DoVirtual = function (pA,pB) {',
  11932. ' };',
  11933. ' this.DoIt = function (pA,pB) {',
  11934. ' };',
  11935. ' this.DoIt2 = function (pA,pB) {',
  11936. ' };',
  11937. ' this.GetIt = function (pA, pB) {',
  11938. ' var Result = 0;',
  11939. ' return Result;',
  11940. ' };',
  11941. '});',
  11942. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  11943. ' this.DoAbstract = function (pA,pB) {',
  11944. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11945. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11946. ' };',
  11947. ' this.DoVirtual = function (pA,pB) {',
  11948. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  11949. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11950. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11951. ' this.DoIt(pA,pB);',
  11952. ' this.DoIt(pA,0);',
  11953. ' this.DoIt2(pA,2);',
  11954. ' this.DoIt2(1,2);',
  11955. ' };',
  11956. ' this.GetIt$1 = function (pA, pB) {',
  11957. ' var Result = 0;',
  11958. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  11959. ' return Result;',
  11960. ' };',
  11961. '});'
  11962. ]),
  11963. LinesToStr([ // this.$main
  11964. ''
  11965. ]));
  11966. end;
  11967. procedure TTestModule.TestClasS_CallInheritedConstructor;
  11968. begin
  11969. StartProgram(false);
  11970. Add('type');
  11971. Add(' TObject = class');
  11972. Add(' constructor Create; virtual;');
  11973. Add(' constructor CreateWithB(b: boolean);');
  11974. Add(' end;');
  11975. Add(' TA = class');
  11976. Add(' constructor Create; override;');
  11977. Add(' constructor CreateWithC(c: char);');
  11978. Add(' procedure DoIt;');
  11979. Add(' class function DoSome: TObject;');
  11980. Add(' end;');
  11981. Add('constructor tobject.create;');
  11982. Add('begin');
  11983. Add(' inherited; // call non existing ancestor -> ignore silently');
  11984. Add('end;');
  11985. Add('constructor tobject.createwithb(b: boolean);');
  11986. Add('begin');
  11987. Add(' inherited; // call non existing ancestor -> ignore silently');
  11988. Add(' create; // normal call');
  11989. Add('end;');
  11990. Add('constructor ta.create;');
  11991. Add('begin');
  11992. Add(' inherited; // normal call TObject.Create');
  11993. Add(' inherited create; // normal call TObject.Create');
  11994. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  11995. Add('end;');
  11996. Add('constructor ta.createwithc(c: char);');
  11997. Add('begin');
  11998. Add(' inherited create; // call TObject.Create');
  11999. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12000. Add(' doit;');
  12001. Add(' doit();');
  12002. Add(' dosome;');
  12003. Add('end;');
  12004. Add('procedure ta.doit;');
  12005. Add('begin');
  12006. Add(' create; // normal call');
  12007. Add(' createwithb(false); // normal call');
  12008. Add(' createwithc(''c''); // normal call');
  12009. Add('end;');
  12010. Add('class function ta.dosome: TObject;');
  12011. Add('begin');
  12012. Add(' Result:=create; // constructor');
  12013. Add(' Result:=createwithb(true); // constructor');
  12014. Add(' Result:=createwithc(''c''); // constructor');
  12015. Add('end;');
  12016. Add('begin');
  12017. ConvertProgram;
  12018. CheckSource('TestClass_CallInheritedConstructor',
  12019. LinesToStr([ // statements
  12020. 'rtl.createClass($mod,"TObject",null,function(){',
  12021. ' this.$init = function () {',
  12022. ' };',
  12023. ' this.$final = function () {',
  12024. ' };',
  12025. ' this.Create = function () {',
  12026. ' return this;',
  12027. ' };',
  12028. ' this.CreateWithB = function (b) {',
  12029. ' this.Create();',
  12030. ' return this;',
  12031. ' };',
  12032. '});',
  12033. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  12034. ' this.Create = function () {',
  12035. ' $mod.TObject.Create.call(this);',
  12036. ' $mod.TObject.Create.call(this);',
  12037. ' $mod.TObject.CreateWithB.call(this, false);',
  12038. ' return this;',
  12039. ' };',
  12040. ' this.CreateWithC = function (c) {',
  12041. ' $mod.TObject.Create.call(this);',
  12042. ' $mod.TObject.CreateWithB.call(this, true);',
  12043. ' this.DoIt();',
  12044. ' this.DoIt();',
  12045. ' this.$class.DoSome();',
  12046. ' return this;',
  12047. ' };',
  12048. ' this.DoIt = function () {',
  12049. ' this.Create();',
  12050. ' this.CreateWithB(false);',
  12051. ' this.CreateWithC("c");',
  12052. ' };',
  12053. ' this.DoSome = function () {',
  12054. ' var Result = null;',
  12055. ' Result = this.$create("Create");',
  12056. ' Result = this.$create("CreateWithB", [true]);',
  12057. ' Result = this.$create("CreateWithC", ["c"]);',
  12058. ' return Result;',
  12059. ' };',
  12060. '});'
  12061. ]),
  12062. LinesToStr([ // this.$main
  12063. ''
  12064. ]));
  12065. end;
  12066. procedure TTestModule.TestClass_ClassVar_Assign;
  12067. begin
  12068. StartProgram(false);
  12069. Add([
  12070. 'type',
  12071. ' TObject = class',
  12072. ' public',
  12073. ' class var vI: longint;',
  12074. ' class var Sub: TObject;',
  12075. ' constructor Create;',
  12076. ' class function GetIt(var Par: longint): tobject;',
  12077. ' end;',
  12078. 'constructor tobject.create;',
  12079. 'begin',
  12080. ' vi:=vi+1;',
  12081. ' Self.vi:=Self.vi+1;',
  12082. ' inc(vi);',
  12083. 'end;',
  12084. 'class function tobject.getit(var par: longint): tobject;',
  12085. 'begin',
  12086. ' vi:=vi+3;',
  12087. ' Self.vi:=Self.vi+4;',
  12088. ' inc(vi);',
  12089. ' Result:=self.sub;',
  12090. ' GetIt(vi);',
  12091. 'end;',
  12092. 'var Obj: tobject;',
  12093. 'begin',
  12094. ' obj:=tobject.create;',
  12095. ' tobject.vi:=3;',
  12096. ' if tobject.vi=4 then ;',
  12097. ' tobject.sub:=nil;',
  12098. ' obj.sub:=nil;',
  12099. ' obj.sub.sub:=nil;']);
  12100. ConvertProgram;
  12101. CheckSource('TestClass_ClassVar_Assign',
  12102. LinesToStr([ // statements
  12103. 'rtl.createClass($mod,"TObject",null,function(){',
  12104. ' this.vI = 0;',
  12105. ' this.Sub = null;',
  12106. ' this.$init = function () {',
  12107. ' };',
  12108. ' this.$final = function () {',
  12109. ' };',
  12110. ' this.Create = function(){',
  12111. ' $mod.TObject.vI = this.vI+1;',
  12112. ' $mod.TObject.vI = this.vI+1;',
  12113. ' $mod.TObject.vI += 1;',
  12114. ' return this;',
  12115. ' };',
  12116. ' this.GetIt = function(Par){',
  12117. ' var Result = null;',
  12118. ' $mod.TObject.vI = this.vI + 3;',
  12119. ' $mod.TObject.vI = this.vI + 4;',
  12120. ' $mod.TObject.vI += 1;',
  12121. ' Result = this.Sub;',
  12122. ' this.GetIt({',
  12123. ' p: $mod.TObject,',
  12124. ' get: function () {',
  12125. ' return this.p.vI;',
  12126. ' },',
  12127. ' set: function (v) {',
  12128. ' this.p.vI = v;',
  12129. ' }',
  12130. ' });',
  12131. ' return Result;',
  12132. ' };',
  12133. '});',
  12134. 'this.Obj = null;'
  12135. ]),
  12136. LinesToStr([ // $mod.$main
  12137. '$mod.Obj = $mod.TObject.$create("Create");',
  12138. '$mod.TObject.vI = 3;',
  12139. 'if ($mod.TObject.vI === 4);',
  12140. '$mod.TObject.Sub=null;',
  12141. '$mod.TObject.Sub=null;',
  12142. '$mod.TObject.Sub=null;',
  12143. '']));
  12144. end;
  12145. procedure TTestModule.TestClass_CallClassMethod;
  12146. begin
  12147. StartProgram(false);
  12148. Add('type');
  12149. Add(' TObject = class');
  12150. Add(' public');
  12151. Add(' class var vI: longint;');
  12152. Add(' class var Sub: TObject;');
  12153. Add(' constructor Create;');
  12154. Add(' function GetMore(Par: longint): longint;');
  12155. Add(' class function GetIt(Par: longint): tobject;');
  12156. Add(' end;');
  12157. Add('constructor tobject.create;');
  12158. Add('begin');
  12159. Add(' sub:=getit(3);');
  12160. Add(' vi:=getmore(4);');
  12161. Add(' sub:=Self.getit(5);');
  12162. Add(' vi:=Self.getmore(6);');
  12163. Add('end;');
  12164. Add('function tobject.getmore(par: longint): longint;');
  12165. Add('begin');
  12166. Add(' sub:=getit(11);');
  12167. Add(' vi:=getmore(12);');
  12168. Add(' sub:=self.getit(13);');
  12169. Add(' vi:=self.getmore(14);');
  12170. Add('end;');
  12171. Add('class function tobject.getit(par: longint): tobject;');
  12172. Add('begin');
  12173. Add(' sub:=getit(21);');
  12174. Add(' vi:=sub.getmore(22);');
  12175. Add(' sub:=self.getit(23);');
  12176. Add(' vi:=self.sub.getmore(24);');
  12177. Add('end;');
  12178. Add('var Obj: tobject;');
  12179. Add('begin');
  12180. Add(' obj:=tobject.create;');
  12181. Add(' tobject.getit(5);');
  12182. Add(' obj.getit(6);');
  12183. Add(' obj.sub.getit(7);');
  12184. Add(' obj.sub.getit(8).SUB:=nil;');
  12185. Add(' obj.sub.getit(9).GETIT(10);');
  12186. Add(' obj.sub.getit(11).SuB.getit(12);');
  12187. ConvertProgram;
  12188. CheckSource('TestClass_CallClassMethod',
  12189. LinesToStr([ // statements
  12190. 'rtl.createClass($mod,"TObject",null,function(){',
  12191. ' this.vI = 0;',
  12192. ' this.Sub = null;',
  12193. ' this.$init = function () {',
  12194. ' };',
  12195. ' this.$final = function () {',
  12196. ' };',
  12197. ' this.Create = function(){',
  12198. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12199. ' $mod.TObject.vI = this.GetMore(4);',
  12200. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12201. ' $mod.TObject.vI = this.GetMore(6);',
  12202. ' return this;',
  12203. ' };',
  12204. ' this.GetMore = function(Par){',
  12205. ' var Result = 0;',
  12206. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12207. ' $mod.TObject.vI = this.GetMore(12);',
  12208. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12209. ' $mod.TObject.vI = this.GetMore(14);',
  12210. ' return Result;',
  12211. ' };',
  12212. ' this.GetIt = function(Par){',
  12213. ' var Result = null;',
  12214. ' $mod.TObject.Sub = this.GetIt(21);',
  12215. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12216. ' $mod.TObject.Sub = this.GetIt(23);',
  12217. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12218. ' return Result;',
  12219. ' };',
  12220. '});',
  12221. 'this.Obj = null;'
  12222. ]),
  12223. LinesToStr([ // $mod.$main
  12224. '$mod.Obj = $mod.TObject.$create("Create");',
  12225. '$mod.TObject.GetIt(5);',
  12226. '$mod.Obj.$class.GetIt(6);',
  12227. '$mod.Obj.Sub.$class.GetIt(7);',
  12228. '$mod.TObject.Sub=null;',
  12229. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12230. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12231. '']));
  12232. end;
  12233. procedure TTestModule.TestClass_Property;
  12234. begin
  12235. StartProgram(false);
  12236. Add('type');
  12237. Add(' TObject = class');
  12238. Add(' Fx: longint;');
  12239. Add(' Fy: longint;');
  12240. Add(' function GetInt: longint;');
  12241. Add(' procedure SetInt(Value: longint);');
  12242. Add(' procedure DoIt;');
  12243. Add(' property IntA: longint read Fx write Fy;');
  12244. Add(' property IntB: longint read GetInt write SetInt;');
  12245. Add(' end;');
  12246. Add('function tobject.getint: longint;');
  12247. Add('begin');
  12248. Add(' result:=fx;');
  12249. Add('end;');
  12250. Add('procedure tobject.setint(value: longint);');
  12251. Add('begin');
  12252. Add(' if value=fy then exit;');
  12253. Add(' fy:=value;');
  12254. Add('end;');
  12255. Add('procedure tobject.doit;');
  12256. Add('begin');
  12257. Add(' IntA:=IntA+1;');
  12258. Add(' Self.IntA:=Self.IntA+1;');
  12259. Add(' IntB:=IntB+1;');
  12260. Add(' Self.IntB:=Self.IntB+1;');
  12261. Add('end;');
  12262. Add('var Obj: tobject;');
  12263. Add('begin');
  12264. Add(' obj.inta:=obj.inta+1;');
  12265. Add(' if obj.intb=2 then;');
  12266. Add(' obj.intb:=obj.intb+2;');
  12267. Add(' obj.setint(obj.inta);');
  12268. ConvertProgram;
  12269. CheckSource('TestClass_Property',
  12270. LinesToStr([ // statements
  12271. 'rtl.createClass($mod, "TObject", null, function () {',
  12272. ' this.$init = function () {',
  12273. ' this.Fx = 0;',
  12274. ' this.Fy = 0;',
  12275. ' };',
  12276. ' this.$final = function () {',
  12277. ' };',
  12278. ' this.GetInt = function () {',
  12279. ' var Result = 0;',
  12280. ' Result = this.Fx;',
  12281. ' return Result;',
  12282. ' };',
  12283. ' this.SetInt = function (Value) {',
  12284. ' if (Value === this.Fy) return;',
  12285. ' this.Fy = Value;',
  12286. ' };',
  12287. ' this.DoIt = function () {',
  12288. ' this.Fy = this.Fx + 1;',
  12289. ' this.Fy = this.Fx + 1;',
  12290. ' this.SetInt(this.GetInt() + 1);',
  12291. ' this.SetInt(this.GetInt() + 1);',
  12292. ' };',
  12293. '});',
  12294. 'this.Obj = null;'
  12295. ]),
  12296. LinesToStr([ // $mod.$main
  12297. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12298. 'if ($mod.Obj.GetInt() === 2);',
  12299. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12300. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12301. ]));
  12302. end;
  12303. procedure TTestModule.TestClass_Property_ClassMethod;
  12304. begin
  12305. StartProgram(false);
  12306. Add([
  12307. 'type',
  12308. ' TObject = class',
  12309. ' class var Fx: longint;',
  12310. ' class var Fy: longint;',
  12311. ' class function GetInt: longint;',
  12312. ' class procedure SetInt(Value: longint);',
  12313. ' end;',
  12314. ' TBird = class',
  12315. ' class procedure DoIt;',
  12316. ' class property IntA: longint read Fx write Fy;',
  12317. ' class property IntB: longint read GetInt write SetInt;',
  12318. ' end;',
  12319. 'class function tobject.getint: longint;',
  12320. 'begin',
  12321. ' result:=fx;',
  12322. 'end;',
  12323. 'class procedure tobject.setint(value: longint);',
  12324. 'begin',
  12325. 'end;',
  12326. 'class procedure tbird.doit;',
  12327. 'begin',
  12328. ' FX:=3;',
  12329. ' IntA:=IntA+1;',
  12330. ' Self.IntA:=Self.IntA+1;',
  12331. ' IntB:=IntB+1;',
  12332. ' Self.IntB:=Self.IntB+1;',
  12333. ' with Self do begin',
  12334. ' FX:=11;',
  12335. ' IntA:=IntA+12;',
  12336. ' IntB:=IntB+13;',
  12337. ' end;',
  12338. 'end;',
  12339. 'var Obj: tbird;',
  12340. 'begin',
  12341. ' tbird.fx:=tbird.fx+1;',
  12342. ' tbird.inta:=tbird.inta+1;',
  12343. ' if tbird.intb=2 then;',
  12344. ' tbird.intb:=tbird.intb+2;',
  12345. ' tbird.setint(tbird.inta);',
  12346. ' obj.inta:=obj.inta+1;',
  12347. ' if obj.intb=2 then;',
  12348. ' obj.intb:=obj.intb+2;',
  12349. ' obj.setint(obj.inta);',
  12350. ' with Tbird do begin',
  12351. ' FX:=FY+1;',
  12352. ' inta:=inta+2;',
  12353. ' intb:=intb+3;',
  12354. ' end;',
  12355. ' with Obj do begin',
  12356. ' FX:=FY+1;',
  12357. ' inta:=inta+2;',
  12358. ' intb:=intb+3;',
  12359. ' end;',
  12360. '']);
  12361. ConvertProgram;
  12362. CheckSource('TestClass_Property_ClassMethod',
  12363. LinesToStr([ // statements
  12364. 'rtl.createClass($mod, "TObject", null, function () {',
  12365. ' this.Fx = 0;',
  12366. ' this.Fy = 0;',
  12367. ' this.$init = function () {',
  12368. ' };',
  12369. ' this.$final = function () {',
  12370. ' };',
  12371. ' this.GetInt = function () {',
  12372. ' var Result = 0;',
  12373. ' Result = this.Fx;',
  12374. ' return Result;',
  12375. ' };',
  12376. ' this.SetInt = function (Value) {',
  12377. ' };',
  12378. '});',
  12379. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12380. ' this.DoIt = function () {',
  12381. ' $mod.TObject.Fx = 3;',
  12382. ' $mod.TObject.Fy = this.Fx + 1;',
  12383. ' $mod.TObject.Fy = this.Fx + 1;',
  12384. ' this.SetInt(this.GetInt() + 1);',
  12385. ' this.SetInt(this.GetInt() + 1);',
  12386. ' $mod.TObject.Fx = 11;',
  12387. ' $mod.TObject.Fy = this.Fx + 12;',
  12388. ' this.SetInt(this.GetInt() + 13);',
  12389. ' };',
  12390. '});',
  12391. 'this.Obj = null;'
  12392. ]),
  12393. LinesToStr([ // $mod.$main
  12394. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  12395. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  12396. 'if ($mod.TBird.GetInt() === 2);',
  12397. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  12398. '$mod.TBird.SetInt($mod.TBird.Fx);',
  12399. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  12400. 'if ($mod.Obj.$class.GetInt() === 2);',
  12401. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  12402. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  12403. 'var $with1 = $mod.TBird;',
  12404. '$mod.TObject.Fx = $with1.Fy + 1;',
  12405. '$mod.TObject.Fy = $with1.Fx + 2;',
  12406. '$with1.SetInt($with1.GetInt() + 3);',
  12407. 'var $with2 = $mod.Obj;',
  12408. '$mod.TObject.Fx = $with2.Fy + 1;',
  12409. '$mod.TObject.Fy = $with2.Fx + 2;',
  12410. '$with2.$class.SetInt($with2.$class.GetInt() + 3);',
  12411. '']));
  12412. end;
  12413. procedure TTestModule.TestClass_Property_Indexed;
  12414. begin
  12415. StartProgram(false);
  12416. Add('type');
  12417. Add(' TObject = class');
  12418. Add(' FItems: array of longint;');
  12419. Add(' function GetItems(Index: longint): longint;');
  12420. Add(' procedure SetItems(Index: longint; Value: longint);');
  12421. Add(' procedure DoIt;');
  12422. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  12423. Add(' end;');
  12424. Add('function tobject.getitems(index: longint): longint;');
  12425. Add('begin');
  12426. Add(' Result:=fitems[index];');
  12427. Add('end;');
  12428. Add('procedure tobject.setitems(index: longint; value: longint);');
  12429. Add('begin');
  12430. Add(' fitems[index]:=value;');
  12431. Add('end;');
  12432. Add('procedure tobject.doit;');
  12433. Add('begin');
  12434. Add(' items[1]:=2;');
  12435. Add(' items[3]:=items[4];');
  12436. Add(' self.items[5]:=self.items[6];');
  12437. Add(' items[items[7]]:=items[items[8]];');
  12438. Add('end;');
  12439. Add('var Obj: tobject;');
  12440. Add('begin');
  12441. Add(' obj.Items[11]:=obj.Items[12];');
  12442. ConvertProgram;
  12443. CheckSource('TestClass_Property_Indexed',
  12444. LinesToStr([ // statements
  12445. 'rtl.createClass($mod, "TObject", null, function () {',
  12446. ' this.$init = function () {',
  12447. ' this.FItems = [];',
  12448. ' };',
  12449. ' this.$final = function () {',
  12450. ' this.FItems = undefined;',
  12451. ' };',
  12452. ' this.GetItems = function (Index) {',
  12453. ' var Result = 0;',
  12454. ' Result = this.FItems[Index];',
  12455. ' return Result;',
  12456. ' };',
  12457. ' this.SetItems = function (Index, Value) {',
  12458. ' this.FItems[Index] = Value;',
  12459. ' };',
  12460. ' this.DoIt = function () {',
  12461. ' this.SetItems(1, 2);',
  12462. ' this.SetItems(3,this.GetItems(4));',
  12463. ' this.SetItems(5,this.GetItems(6));',
  12464. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  12465. ' };',
  12466. '});',
  12467. 'this.Obj = null;'
  12468. ]),
  12469. LinesToStr([ // $mod.$main
  12470. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  12471. ]));
  12472. end;
  12473. procedure TTestModule.TestClass_Property_IndexSpec;
  12474. begin
  12475. StartProgram(false);
  12476. Add([
  12477. 'type',
  12478. ' TEnum = (red, blue);',
  12479. ' TObject = class',
  12480. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  12481. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  12482. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  12483. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  12484. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  12485. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  12486. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  12487. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  12488. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  12489. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  12490. ' end;',
  12491. 'procedure DoIt(b: boolean); begin end;',
  12492. 'var',
  12493. ' o: TObject;',
  12494. 'begin',
  12495. ' o.B1:=o.B1;',
  12496. ' o.B2:=o.B2;',
  12497. ' o.B3:=o.B3;',
  12498. ' o.I1[''a'']:=o.I1[''b''];',
  12499. ' doit(o.b1);',
  12500. ' doit(o.b2);',
  12501. ' doit(o.i1[''c'']);',
  12502. '']);
  12503. ConvertProgram;
  12504. CheckSource('TestClass_Property_IndexSpec',
  12505. LinesToStr([ // statements
  12506. 'this.TEnum = {',
  12507. ' "0": "red",',
  12508. ' red: 0,',
  12509. ' "1": "blue",',
  12510. ' blue: 1',
  12511. '};',
  12512. 'rtl.createClass($mod, "TObject", null, function () {',
  12513. ' this.$init = function () {',
  12514. ' };',
  12515. ' this.$final = function () {',
  12516. ' };',
  12517. '});',
  12518. 'this.DoIt = function (b) {',
  12519. '};',
  12520. 'this.o = null;',
  12521. '']),
  12522. LinesToStr([ // $mod.$main
  12523. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  12524. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  12525. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  12526. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  12527. '$mod.DoIt($mod.o.GetIntBool(1));',
  12528. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  12529. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  12530. '']));
  12531. end;
  12532. procedure TTestModule.TestClass_PropertyOfTypeArray;
  12533. begin
  12534. StartProgram(false);
  12535. Add('type');
  12536. Add(' TArray = array of longint;');
  12537. Add(' TObject = class');
  12538. Add(' FItems: TArray;');
  12539. Add(' function GetItems: tarray;');
  12540. Add(' procedure SetItems(Value: tarray);');
  12541. Add(' property Items: tarray read getitems write setitems;');
  12542. Add(' end;');
  12543. Add('function tobject.getitems: tarray;');
  12544. Add('begin');
  12545. Add(' Result:=fitems;');
  12546. Add('end;');
  12547. Add('procedure tobject.setitems(value: tarray);');
  12548. Add('begin');
  12549. Add(' fitems:=value;');
  12550. Add(' fitems:=nil;');
  12551. Add(' Items:=nil;');
  12552. Add(' Items:=Items;');
  12553. Add(' Items[1]:=2;');
  12554. Add(' fitems[3]:=Items[4];');
  12555. Add(' Items[5]:=Items[6];');
  12556. Add(' Self.Items[7]:=8;');
  12557. Add(' Self.Items[9]:=Self.Items[10];');
  12558. Add(' Items[Items[11]]:=Items[Items[12]];');
  12559. Add('end;');
  12560. Add('var Obj: tobject;');
  12561. Add('begin');
  12562. Add(' obj.items:=nil;');
  12563. Add(' obj.items:=obj.items;');
  12564. Add(' obj.items[11]:=obj.items[12];');
  12565. ConvertProgram;
  12566. CheckSource('TestClass_PropertyOfTypeArray',
  12567. LinesToStr([ // statements
  12568. 'rtl.createClass($mod, "TObject", null, function () {',
  12569. ' this.$init = function () {',
  12570. ' this.FItems = [];',
  12571. ' };',
  12572. ' this.$final = function () {',
  12573. ' this.FItems = undefined;',
  12574. ' };',
  12575. ' this.GetItems = function () {',
  12576. ' var Result = [];',
  12577. ' Result = this.FItems;',
  12578. ' return Result;',
  12579. ' };',
  12580. ' this.SetItems = function (Value) {',
  12581. ' this.FItems = Value;',
  12582. ' this.FItems = [];',
  12583. ' this.SetItems([]);',
  12584. ' this.SetItems(this.GetItems());',
  12585. ' this.GetItems()[1] = 2;',
  12586. ' this.FItems[3] = this.GetItems()[4];',
  12587. ' this.GetItems()[5] = this.GetItems()[6];',
  12588. ' this.GetItems()[7] = 8;',
  12589. ' this.GetItems()[9] = this.GetItems()[10];',
  12590. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  12591. ' };',
  12592. '});',
  12593. 'this.Obj = null;'
  12594. ]),
  12595. LinesToStr([ // $mod.$main
  12596. '$mod.Obj.SetItems([]);',
  12597. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  12598. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  12599. ]));
  12600. end;
  12601. procedure TTestModule.TestClass_PropertyDefault;
  12602. begin
  12603. StartProgram(false);
  12604. Add([
  12605. 'type',
  12606. ' TArray = array of longint;',
  12607. ' TObject = class',
  12608. ' FItems: TArray;',
  12609. ' function GetItems(Index: longint): longint;',
  12610. ' procedure SetItems(Index, Value: longint);',
  12611. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  12612. ' end;',
  12613. 'function tobject.getitems(index: longint): longint;',
  12614. 'begin',
  12615. 'end;',
  12616. 'procedure tobject.setitems(index, value: longint);',
  12617. 'begin',
  12618. ' Self[1]:=2;',
  12619. ' Self[3]:=Self[index];',
  12620. ' Self[index]:=Self[Self[value]];',
  12621. ' Self[Self[4]]:=value;',
  12622. 'end;',
  12623. 'var Obj: tobject;',
  12624. 'begin',
  12625. ' obj[11]:=12;',
  12626. ' obj[13]:=obj[14];',
  12627. ' obj[obj[15]]:=obj[obj[15]];',
  12628. ' TObject(obj)[16]:=TObject(obj)[17];']);
  12629. ConvertProgram;
  12630. CheckSource('TestClass_PropertyDefault',
  12631. LinesToStr([ // statements
  12632. 'rtl.createClass($mod, "TObject", null, function () {',
  12633. ' this.$init = function () {',
  12634. ' this.FItems = [];',
  12635. ' };',
  12636. ' this.$final = function () {',
  12637. ' this.FItems = undefined;',
  12638. ' };',
  12639. ' this.GetItems = function (Index) {',
  12640. ' var Result = 0;',
  12641. ' return Result;',
  12642. ' };',
  12643. ' this.SetItems = function (Index, Value) {',
  12644. ' this.SetItems(1, 2);',
  12645. ' this.SetItems(3, this.GetItems(Index));',
  12646. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  12647. ' this.SetItems(this.GetItems(4), Value);',
  12648. ' };',
  12649. '});',
  12650. 'this.Obj = null;'
  12651. ]),
  12652. LinesToStr([ // $mod.$main
  12653. '$mod.Obj.SetItems(11, 12);',
  12654. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  12655. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));',
  12656. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  12657. '']));
  12658. end;
  12659. procedure TTestModule.TestClass_PropertyDefault2;
  12660. begin
  12661. StartProgram(false);
  12662. Add([
  12663. 'type',
  12664. ' TObject = class end;',
  12665. ' TAlphaList = class',
  12666. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  12667. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  12668. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  12669. ' end;',
  12670. ' TBetaList = class',
  12671. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  12672. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  12673. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  12674. ' end;',
  12675. ' TBird = class',
  12676. ' procedure DoIt;',
  12677. ' end;',
  12678. 'procedure TBird.DoIt;',
  12679. 'var',
  12680. ' List: TAlphaList;',
  12681. 'begin',
  12682. ' if TBetaList(List[true])[3]=nil then ;',
  12683. ' TBetaList(List[false])[5]:=nil;',
  12684. 'end;',
  12685. 'var',
  12686. ' List: TAlphaList;',
  12687. 'begin',
  12688. ' if TBetaList(List[true])[3]=nil then ;',
  12689. ' TBetaList(List[false])[5]:=nil;',
  12690. '']);
  12691. ConvertProgram;
  12692. CheckSource('TestClass_PropertyDefault2',
  12693. LinesToStr([ // statements
  12694. 'rtl.createClass($mod, "TObject", null, function () {',
  12695. ' this.$init = function () {',
  12696. ' };',
  12697. ' this.$final = function () {',
  12698. ' };',
  12699. '});',
  12700. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  12701. '});',
  12702. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  12703. '});',
  12704. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12705. ' this.DoIt = function () {',
  12706. ' var List = null;',
  12707. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  12708. ' List.GetAlphas(false).SetBetas(5, null);',
  12709. ' };',
  12710. '});',
  12711. 'this.List = null;',
  12712. '']),
  12713. LinesToStr([ // $mod.$main
  12714. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  12715. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  12716. '']));
  12717. end;
  12718. procedure TTestModule.TestClass_PropertyOverride;
  12719. begin
  12720. StartProgram(false);
  12721. Add('type');
  12722. Add(' integer = longint;');
  12723. Add(' TObject = class');
  12724. Add(' FItem: integer;');
  12725. Add(' function GetItem: integer; external name ''GetItem'';');
  12726. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  12727. Add(' property Item: integer read getitem write setitem;');
  12728. Add(' end;');
  12729. Add(' TCar = class');
  12730. Add(' FBag: integer;');
  12731. Add(' function GetBag: integer; external name ''GetBag'';');
  12732. Add(' property Item read getbag;');
  12733. Add(' end;');
  12734. Add('var');
  12735. Add(' Obj: tobject;');
  12736. Add(' Car: tcar;');
  12737. Add('begin');
  12738. Add(' Obj.Item:=Obj.Item;');
  12739. Add(' Car.Item:=Car.Item;');
  12740. ConvertProgram;
  12741. CheckSource('TestClass_PropertyOverride',
  12742. LinesToStr([ // statements
  12743. 'rtl.createClass($mod, "TObject", null, function () {',
  12744. ' this.$init = function () {',
  12745. ' this.FItem = 0;',
  12746. ' };',
  12747. ' this.$final = function () {',
  12748. ' };',
  12749. '});',
  12750. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12751. ' this.$init = function () {',
  12752. ' $mod.TObject.$init.call(this);',
  12753. ' this.FBag = 0;',
  12754. ' };',
  12755. '});',
  12756. 'this.Obj = null;',
  12757. 'this.Car = null;',
  12758. '']),
  12759. LinesToStr([ // $mod.$main
  12760. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  12761. '$mod.Car.SetItem($mod.Car.GetBag());',
  12762. '']));
  12763. end;
  12764. procedure TTestModule.TestClass_PropertyIncVisibility;
  12765. begin
  12766. AddModuleWithIntfImplSrc('unit1.pp',
  12767. LinesToStr([
  12768. 'type',
  12769. ' TNumber = longint;',
  12770. ' TInteger = longint;',
  12771. ' TObject = class',
  12772. ' private',
  12773. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  12774. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  12775. ' protected',
  12776. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  12777. ' end;']),
  12778. LinesToStr([
  12779. '']));
  12780. StartProgram(true);
  12781. Add([
  12782. 'uses unit1;',
  12783. 'type',
  12784. ' TBird = class',
  12785. ' public',
  12786. ' property Items;',
  12787. ' end;',
  12788. 'procedure DoIt(i: TInteger);',
  12789. 'begin',
  12790. 'end;',
  12791. 'var b: TBird;',
  12792. 'begin',
  12793. ' b.Items[1]:=2;',
  12794. ' b.Items[3]:=b.Items[4];',
  12795. ' DoIt(b.Items[5]);',
  12796. '']);
  12797. ConvertProgram;
  12798. CheckSource('TestClass_PropertyIncVisibility',
  12799. LinesToStr([ // statements
  12800. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  12801. '});',
  12802. 'this.DoIt = function (i) {',
  12803. '};',
  12804. 'this.b = null;'
  12805. ]),
  12806. LinesToStr([ // $mod.$main
  12807. '$mod.b.SetItems(1, 2);',
  12808. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  12809. '$mod.DoIt($mod.b.GetItems(5));'
  12810. ]));
  12811. end;
  12812. procedure TTestModule.TestClass_Assigned;
  12813. begin
  12814. StartProgram(false);
  12815. Add('type');
  12816. Add(' TObject = class');
  12817. Add(' end;');
  12818. Add('var');
  12819. Add(' Obj: tobject;');
  12820. Add(' b: boolean;');
  12821. Add('begin');
  12822. Add(' if Assigned(obj) then ;');
  12823. Add(' b:=Assigned(obj) or false;');
  12824. ConvertProgram;
  12825. CheckSource('TestClass_Assigned',
  12826. LinesToStr([ // statements
  12827. 'rtl.createClass($mod, "TObject", null, function () {',
  12828. ' this.$init = function () {',
  12829. ' };',
  12830. ' this.$final = function () {',
  12831. ' };',
  12832. '});',
  12833. 'this.Obj = null;',
  12834. 'this.b = false;'
  12835. ]),
  12836. LinesToStr([ // $mod.$main
  12837. 'if ($mod.Obj != null);',
  12838. '$mod.b = ($mod.Obj != null) || false;'
  12839. ]));
  12840. end;
  12841. procedure TTestModule.TestClass_WithClassDoCreate;
  12842. begin
  12843. StartProgram(false);
  12844. Add('type');
  12845. Add(' TObject = class');
  12846. Add(' aBool: boolean;');
  12847. Add(' Arr: array of boolean;');
  12848. Add(' constructor Create;');
  12849. Add(' end;');
  12850. Add('constructor TObject.Create; begin end;');
  12851. Add('var');
  12852. Add(' Obj: tobject;');
  12853. Add(' b: boolean;');
  12854. Add('begin');
  12855. Add(' with tobject.create do begin');
  12856. Add(' b:=abool;');
  12857. Add(' abool:=b;');
  12858. Add(' b:=arr[1];');
  12859. Add(' arr[2]:=b;');
  12860. Add(' end;');
  12861. Add(' with tobject do');
  12862. Add(' obj:=create;');
  12863. Add(' with obj do begin');
  12864. Add(' create;');
  12865. Add(' b:=abool;');
  12866. Add(' abool:=b;');
  12867. Add(' b:=arr[3];');
  12868. Add(' arr[4]:=b;');
  12869. Add(' end;');
  12870. ConvertProgram;
  12871. CheckSource('TestClass_WithClassDoCreate',
  12872. LinesToStr([ // statements
  12873. 'rtl.createClass($mod, "TObject", null, function () {',
  12874. ' this.$init = function () {',
  12875. ' this.aBool = false;',
  12876. ' this.Arr = [];',
  12877. ' };',
  12878. ' this.$final = function () {',
  12879. ' this.Arr = undefined;',
  12880. ' };',
  12881. ' this.Create = function () {',
  12882. ' return this;',
  12883. ' };',
  12884. '});',
  12885. 'this.Obj = null;',
  12886. 'this.b = false;'
  12887. ]),
  12888. LinesToStr([ // $mod.$main
  12889. 'var $with1 = $mod.TObject.$create("Create");',
  12890. '$mod.b = $with1.aBool;',
  12891. '$with1.aBool = $mod.b;',
  12892. '$mod.b = $with1.Arr[1];',
  12893. '$with1.Arr[2] = $mod.b;',
  12894. 'var $with2 = $mod.TObject;',
  12895. '$mod.Obj = $with2.$create("Create");',
  12896. 'var $with3 = $mod.Obj;',
  12897. '$with3.Create();',
  12898. '$mod.b = $with3.aBool;',
  12899. '$with3.aBool = $mod.b;',
  12900. '$mod.b = $with3.Arr[3];',
  12901. '$with3.Arr[4] = $mod.b;',
  12902. '']));
  12903. end;
  12904. procedure TTestModule.TestClass_WithClassInstDoProperty;
  12905. begin
  12906. StartProgram(false);
  12907. Add('type');
  12908. Add(' TObject = class');
  12909. Add(' FInt: longint;');
  12910. Add(' constructor Create;');
  12911. Add(' function GetSize: longint;');
  12912. Add(' procedure SetSize(Value: longint);');
  12913. Add(' property Int: longint read FInt write FInt;');
  12914. Add(' property Size: longint read GetSize write SetSize;');
  12915. Add(' end;');
  12916. Add('constructor TObject.Create; begin end;');
  12917. Add('function TObject.GetSize: longint; begin; end;');
  12918. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12919. Add('var');
  12920. Add(' Obj: tobject;');
  12921. Add(' i: longint;');
  12922. Add('begin');
  12923. Add(' with TObject.Create do begin');
  12924. Add(' i:=int;');
  12925. Add(' int:=i;');
  12926. Add(' i:=size;');
  12927. Add(' size:=i;');
  12928. Add(' end;');
  12929. Add(' with obj do begin');
  12930. Add(' i:=int;');
  12931. Add(' int:=i;');
  12932. Add(' i:=size;');
  12933. Add(' size:=i;');
  12934. Add(' end;');
  12935. ConvertProgram;
  12936. CheckSource('TestClass_WithClassInstDoProperty',
  12937. LinesToStr([ // statements
  12938. 'rtl.createClass($mod, "TObject", null, function () {',
  12939. ' this.$init = function () {',
  12940. ' this.FInt = 0;',
  12941. ' };',
  12942. ' this.$final = function () {',
  12943. ' };',
  12944. ' this.Create = function () {',
  12945. ' return this;',
  12946. ' };',
  12947. ' this.GetSize = function () {',
  12948. ' var Result = 0;',
  12949. ' return Result;',
  12950. ' };',
  12951. ' this.SetSize = function (Value) {',
  12952. ' };',
  12953. '});',
  12954. 'this.Obj = null;',
  12955. 'this.i = 0;'
  12956. ]),
  12957. LinesToStr([ // $mod.$main
  12958. 'var $with1 = $mod.TObject.$create("Create");',
  12959. '$mod.i = $with1.FInt;',
  12960. '$with1.FInt = $mod.i;',
  12961. '$mod.i = $with1.GetSize();',
  12962. '$with1.SetSize($mod.i);',
  12963. 'var $with2 = $mod.Obj;',
  12964. '$mod.i = $with2.FInt;',
  12965. '$with2.FInt = $mod.i;',
  12966. '$mod.i = $with2.GetSize();',
  12967. '$with2.SetSize($mod.i);',
  12968. '']));
  12969. end;
  12970. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  12971. begin
  12972. StartProgram(false);
  12973. Add('type');
  12974. Add(' TObject = class');
  12975. Add(' constructor Create;');
  12976. Add(' function GetItems(Index: longint): longint;');
  12977. Add(' procedure SetItems(Index, Value: longint);');
  12978. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  12979. Add(' end;');
  12980. Add('constructor TObject.Create; begin end;');
  12981. Add('function tobject.getitems(index: longint): longint; begin; end;');
  12982. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  12983. Add('var');
  12984. Add(' Obj: tobject;');
  12985. Add(' i: longint;');
  12986. Add('begin');
  12987. Add(' with TObject.Create do begin');
  12988. Add(' i:=Items[1];');
  12989. Add(' Items[2]:=i;');
  12990. Add(' end;');
  12991. Add(' with obj do begin');
  12992. Add(' i:=Items[3];');
  12993. Add(' Items[4]:=i;');
  12994. Add(' end;');
  12995. ConvertProgram;
  12996. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  12997. LinesToStr([ // statements
  12998. 'rtl.createClass($mod, "TObject", null, function () {',
  12999. ' this.$init = function () {',
  13000. ' };',
  13001. ' this.$final = function () {',
  13002. ' };',
  13003. ' this.Create = function () {',
  13004. ' return this;',
  13005. ' };',
  13006. ' this.GetItems = function (Index) {',
  13007. ' var Result = 0;',
  13008. ' return Result;',
  13009. ' };',
  13010. ' this.SetItems = function (Index, Value) {',
  13011. ' };',
  13012. '});',
  13013. 'this.Obj = null;',
  13014. 'this.i = 0;'
  13015. ]),
  13016. LinesToStr([ // $mod.$main
  13017. 'var $with1 = $mod.TObject.$create("Create");',
  13018. '$mod.i = $with1.GetItems(1);',
  13019. '$with1.SetItems(2, $mod.i);',
  13020. 'var $with2 = $mod.Obj;',
  13021. '$mod.i = $with2.GetItems(3);',
  13022. '$with2.SetItems(4, $mod.i);',
  13023. '']));
  13024. end;
  13025. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13026. begin
  13027. StartProgram(false);
  13028. Add('type');
  13029. Add(' TObject = class');
  13030. Add(' constructor Create;');
  13031. Add(' function GetSize: longint;');
  13032. Add(' procedure SetSize(Value: longint);');
  13033. Add(' end;');
  13034. Add('constructor TObject.Create; begin end;');
  13035. Add('function TObject.GetSize: longint; begin; end;');
  13036. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13037. Add('var');
  13038. Add(' Obj: tobject;');
  13039. Add(' i: longint;');
  13040. Add('begin');
  13041. Add(' with TObject.Create do begin');
  13042. Add(' i:=GetSize;');
  13043. Add(' i:=GetSize();');
  13044. Add(' SetSize(i);');
  13045. Add(' end;');
  13046. Add(' with obj do begin');
  13047. Add(' i:=GetSize;');
  13048. Add(' i:=GetSize();');
  13049. Add(' SetSize(i);');
  13050. Add(' end;');
  13051. ConvertProgram;
  13052. CheckSource('TestClass_WithClassInstDoFunc',
  13053. LinesToStr([ // statements
  13054. 'rtl.createClass($mod, "TObject", null, function () {',
  13055. ' this.$init = function () {',
  13056. ' };',
  13057. ' this.$final = function () {',
  13058. ' };',
  13059. ' this.Create = function () {',
  13060. ' return this;',
  13061. ' };',
  13062. ' this.GetSize = function () {',
  13063. ' var Result = 0;',
  13064. ' return Result;',
  13065. ' };',
  13066. ' this.SetSize = function (Value) {',
  13067. ' };',
  13068. '});',
  13069. 'this.Obj = null;',
  13070. 'this.i = 0;'
  13071. ]),
  13072. LinesToStr([ // $mod.$main
  13073. 'var $with1 = $mod.TObject.$create("Create");',
  13074. '$mod.i = $with1.GetSize();',
  13075. '$mod.i = $with1.GetSize();',
  13076. '$with1.SetSize($mod.i);',
  13077. 'var $with2 = $mod.Obj;',
  13078. '$mod.i = $with2.GetSize();',
  13079. '$mod.i = $with2.GetSize();',
  13080. '$with2.SetSize($mod.i);',
  13081. '']));
  13082. end;
  13083. procedure TTestModule.TestClass_TypeCast;
  13084. begin
  13085. StartProgram(false);
  13086. Add('type');
  13087. Add(' TObject = class');
  13088. Add(' Next: TObject;');
  13089. Add(' constructor Create;');
  13090. Add(' end;');
  13091. Add(' TControl = class(TObject)');
  13092. Add(' Arr: array of TObject;');
  13093. Add(' function GetIt(vI: longint = 0): TObject;');
  13094. Add(' end;');
  13095. Add('constructor tobject.create; begin end;');
  13096. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  13097. Add('var');
  13098. Add(' Obj: tobject;');
  13099. Add('begin');
  13100. Add(' obj:=tcontrol(obj).next;');
  13101. Add(' tcontrol(obj):=nil;');
  13102. Add(' obj:=tcontrol(obj);');
  13103. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  13104. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  13105. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  13106. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  13107. Add(' obj:=tcontrol(nil);');
  13108. ConvertProgram;
  13109. CheckSource('TestClass_TypeCast',
  13110. LinesToStr([ // statements
  13111. 'rtl.createClass($mod, "TObject", null, function () {',
  13112. ' this.$init = function () {',
  13113. ' this.Next = null;',
  13114. ' };',
  13115. ' this.$final = function () {',
  13116. ' this.Next = undefined;',
  13117. ' };',
  13118. ' this.Create = function () {',
  13119. ' return this;',
  13120. ' };',
  13121. '});',
  13122. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  13123. ' this.$init = function () {',
  13124. ' $mod.TObject.$init.call(this);',
  13125. ' this.Arr = [];',
  13126. ' };',
  13127. ' this.$final = function () {',
  13128. ' this.Arr = undefined;',
  13129. ' $mod.TObject.$final.call(this);',
  13130. ' };',
  13131. ' this.GetIt = function (vI) {',
  13132. ' var Result = null;',
  13133. ' return Result;',
  13134. ' };',
  13135. '});',
  13136. 'this.Obj = null;'
  13137. ]),
  13138. LinesToStr([ // $mod.$main
  13139. '$mod.Obj = $mod.Obj.Next;',
  13140. '$mod.Obj = null;',
  13141. '$mod.Obj = $mod.Obj;',
  13142. '$mod.Obj = $mod.Obj.GetIt(0);',
  13143. '$mod.Obj = $mod.Obj.GetIt(0);',
  13144. '$mod.Obj = $mod.Obj.GetIt(1);',
  13145. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13146. '$mod.Obj = null;',
  13147. '']));
  13148. end;
  13149. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13150. begin
  13151. StartProgram(false);
  13152. Add('type');
  13153. Add(' TObject = class end;');
  13154. Add('procedure ProcA(var A);');
  13155. Add('begin');
  13156. Add(' TObject(A):=nil;');
  13157. Add(' TObject(A):=TObject(A);');
  13158. Add(' if TObject(A)=nil then ;');
  13159. Add(' if nil=TObject(A) then ;');
  13160. Add('end;');
  13161. Add('procedure ProcB(out A);');
  13162. Add('begin');
  13163. Add(' TObject(A):=nil;');
  13164. Add(' TObject(A):=TObject(A);');
  13165. Add(' if TObject(A)=nil then ;');
  13166. Add(' if nil=TObject(A) then ;');
  13167. Add('end;');
  13168. Add('procedure ProcC(const A);');
  13169. Add('begin');
  13170. Add(' if TObject(A)=nil then ;');
  13171. Add(' if nil=TObject(A) then ;');
  13172. Add('end;');
  13173. Add('var o: TObject;');
  13174. Add('begin');
  13175. Add(' ProcA(o);');
  13176. Add(' ProcB(o);');
  13177. Add(' ProcC(o);');
  13178. ConvertProgram;
  13179. CheckSource('TestClass_TypeCastUntypedParam',
  13180. LinesToStr([ // statements
  13181. 'rtl.createClass($mod, "TObject", null, function () {',
  13182. ' this.$init = function () {',
  13183. ' };',
  13184. ' this.$final = function () {',
  13185. ' };',
  13186. '});',
  13187. 'this.ProcA = function (A) {',
  13188. ' A.set(null);',
  13189. ' A.set(A.get());',
  13190. ' if (A.get() === null);',
  13191. ' if (null === A.get());',
  13192. '};',
  13193. 'this.ProcB = function (A) {',
  13194. ' A.set(null);',
  13195. ' A.set(A.get());',
  13196. ' if (A.get() === null);',
  13197. ' if (null === A.get());',
  13198. '};',
  13199. 'this.ProcC = function (A) {',
  13200. ' if (A === null);',
  13201. ' if (null === A);',
  13202. '};',
  13203. 'this.o = null;',
  13204. '']),
  13205. LinesToStr([ // $mod.$main
  13206. '$mod.ProcA({',
  13207. ' p: $mod,',
  13208. ' get: function () {',
  13209. ' return this.p.o;',
  13210. ' },',
  13211. ' set: function (v) {',
  13212. ' this.p.o = v;',
  13213. ' }',
  13214. '});',
  13215. '$mod.ProcB({',
  13216. ' p: $mod,',
  13217. ' get: function () {',
  13218. ' return this.p.o;',
  13219. ' },',
  13220. ' set: function (v) {',
  13221. ' this.p.o = v;',
  13222. ' }',
  13223. '});',
  13224. '$mod.ProcC($mod.o);',
  13225. '']));
  13226. end;
  13227. procedure TTestModule.TestClass_Overloads;
  13228. begin
  13229. StartProgram(false);
  13230. Add('type');
  13231. Add(' TObject = class');
  13232. Add(' procedure DoIt;');
  13233. Add(' procedure DoIt(vI: longint);');
  13234. Add(' end;');
  13235. Add('procedure TObject.DoIt;');
  13236. Add('begin');
  13237. Add(' DoIt;');
  13238. Add(' DoIt(1);');
  13239. Add('end;');
  13240. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13241. Add('begin');
  13242. ConvertProgram;
  13243. CheckSource('TestClass_Overloads',
  13244. LinesToStr([ // statements
  13245. 'rtl.createClass($mod, "TObject", null, function () {',
  13246. ' this.$init = function () {',
  13247. ' };',
  13248. ' this.$final = function () {',
  13249. ' };',
  13250. ' this.DoIt = function () {',
  13251. ' this.DoIt();',
  13252. ' this.DoIt$1(1);',
  13253. ' };',
  13254. ' this.DoIt$1 = function (vI) {',
  13255. ' };',
  13256. '});',
  13257. '']),
  13258. LinesToStr([ // $mod.$main
  13259. '']));
  13260. end;
  13261. procedure TTestModule.TestClass_OverloadsAncestor;
  13262. begin
  13263. StartProgram(false);
  13264. Add('type');
  13265. Add(' TObject = class;');
  13266. Add(' TObject = class');
  13267. Add(' procedure DoIt(vA: longint);');
  13268. Add(' procedure DoIt(vA, vB: longint);');
  13269. Add(' end;');
  13270. Add(' TCar = class;');
  13271. Add(' TCar = class');
  13272. Add(' procedure DoIt(vA: longint);');
  13273. Add(' procedure DoIt(vA, vB: longint);');
  13274. Add(' end;');
  13275. Add('procedure tobject.doit(va: longint);');
  13276. Add('begin');
  13277. Add(' doit(1);');
  13278. Add(' doit(1,2);');
  13279. Add('end;');
  13280. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13281. Add('procedure tcar.doit(va: longint);');
  13282. Add('begin');
  13283. Add(' doit(1);');
  13284. Add(' doit(1,2);');
  13285. Add(' inherited doit(1);');
  13286. Add(' inherited doit(1,2);');
  13287. Add('end;');
  13288. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13289. Add('begin');
  13290. ConvertProgram;
  13291. CheckSource('TestClass_OverloadsAncestor',
  13292. LinesToStr([ // statements
  13293. 'rtl.createClass($mod, "TObject", null, function () {',
  13294. ' this.$init = function () {',
  13295. ' };',
  13296. ' this.$final = function () {',
  13297. ' };',
  13298. ' this.DoIt = function (vA) {',
  13299. ' this.DoIt(1);',
  13300. ' this.DoIt$1(1,2);',
  13301. ' };',
  13302. ' this.DoIt$1 = function (vA, vB) {',
  13303. ' };',
  13304. '});',
  13305. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13306. ' this.DoIt$2 = function (vA) {',
  13307. ' this.DoIt$2(1);',
  13308. ' this.DoIt$3(1, 2);',
  13309. ' $mod.TObject.DoIt.call(this, 1);',
  13310. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  13311. ' };',
  13312. ' this.DoIt$3 = function (vA, vB) {',
  13313. ' };',
  13314. '});',
  13315. '']),
  13316. LinesToStr([ // $mod.$main
  13317. '']));
  13318. end;
  13319. procedure TTestModule.TestClass_OverloadConstructor;
  13320. begin
  13321. StartProgram(false);
  13322. Add('type');
  13323. Add(' TObject = class');
  13324. Add(' constructor Create(vA: longint);');
  13325. Add(' constructor Create(vA, vB: longint);');
  13326. Add(' end;');
  13327. Add(' TCar = class');
  13328. Add(' constructor Create(vA: longint);');
  13329. Add(' constructor Create(vA, vB: longint);');
  13330. Add(' end;');
  13331. Add('constructor tobject.create(va: longint);');
  13332. Add('begin');
  13333. Add(' create(1);');
  13334. Add(' create(1,2);');
  13335. Add('end;');
  13336. Add('constructor tobject.create(va, vb: longint); begin end;');
  13337. Add('constructor tcar.create(va: longint);');
  13338. Add('begin');
  13339. Add(' create(1);');
  13340. Add(' create(1,2);');
  13341. Add(' inherited create(1);');
  13342. Add(' inherited create(1,2);');
  13343. Add('end;');
  13344. Add('constructor tcar.create(va, vb: longint); begin end;');
  13345. Add('begin');
  13346. Add(' tobject.create(1);');
  13347. Add(' tobject.create(1,2);');
  13348. Add(' tcar.create(1);');
  13349. Add(' tcar.create(1,2);');
  13350. ConvertProgram;
  13351. CheckSource('TestClass_OverloadConstructor',
  13352. LinesToStr([ // statements
  13353. 'rtl.createClass($mod, "TObject", null, function () {',
  13354. ' this.$init = function () {',
  13355. ' };',
  13356. ' this.$final = function () {',
  13357. ' };',
  13358. ' this.Create = function (vA) {',
  13359. ' this.Create(1);',
  13360. ' this.Create$1(1,2);',
  13361. ' return this;',
  13362. ' };',
  13363. ' this.Create$1 = function (vA, vB) {',
  13364. ' return this;',
  13365. ' };',
  13366. '});',
  13367. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13368. ' this.Create$2 = function (vA) {',
  13369. ' this.Create$2(1);',
  13370. ' this.Create$3(1, 2);',
  13371. ' $mod.TObject.Create.call(this, 1);',
  13372. ' $mod.TObject.Create$1.call(this, 1, 2);',
  13373. ' return this;',
  13374. ' };',
  13375. ' this.Create$3 = function (vA, vB) {',
  13376. ' return this;',
  13377. ' };',
  13378. '});',
  13379. '']),
  13380. LinesToStr([ // $mod.$main
  13381. '$mod.TObject.$create("Create", [1]);',
  13382. '$mod.TObject.$create("Create$1", [1, 2]);',
  13383. '$mod.TCar.$create("Create$2", [1]);',
  13384. '$mod.TCar.$create("Create$3", [1, 2]);',
  13385. '']));
  13386. end;
  13387. procedure TTestModule.TestClass_OverloadDelphiOverride;
  13388. begin
  13389. StartProgram(false);
  13390. Add([
  13391. '{$mode delphi}',
  13392. 'type',
  13393. ' TObject = class end;',
  13394. ' TBird = class',
  13395. ' function {#a}GetValue: longint; overload; virtual;',
  13396. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  13397. ' end;',
  13398. ' TEagle = class(TBird)',
  13399. ' function {#c}GetValue: longint; overload; override;',
  13400. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  13401. ' end;',
  13402. 'function TBird.GetValue: longint;',
  13403. 'begin',
  13404. ' if 3={@a}GetValue then ;',
  13405. ' if 4={@b}GetValue(5) then ;',
  13406. 'end;',
  13407. 'function TBird.GetValue(AValue: longint): longint;',
  13408. 'begin',
  13409. 'end;',
  13410. 'function TEagle.GetValue: longint;',
  13411. 'begin',
  13412. ' if 13={@c}GetValue then ;',
  13413. ' if 14={@d}GetValue(15) then ;',
  13414. ' if 15=inherited {@a}GetValue then ;',
  13415. ' if 16=inherited {@b}GetValue(17) then ;',
  13416. 'end;',
  13417. 'function TEagle.GetValue(AValue: longint): longint;',
  13418. 'begin',
  13419. 'end;',
  13420. 'var',
  13421. ' e: TEagle;',
  13422. 'begin',
  13423. ' if 23=e.{@c}GetValue then ;',
  13424. ' if 24=e.{@d}GetValue(25) then ;']);
  13425. ConvertProgram;
  13426. CheckSource('TestClass_OverloadDelphiOverride',
  13427. LinesToStr([ // statements
  13428. 'rtl.createClass($mod, "TObject", null, function () {',
  13429. ' this.$init = function () {',
  13430. ' };',
  13431. ' this.$final = function () {',
  13432. ' };',
  13433. '});',
  13434. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13435. ' this.GetValue = function () {',
  13436. ' var Result = 0;',
  13437. ' if (3 === this.GetValue()) ;',
  13438. ' if (4 === this.GetValue$1(5)) ;',
  13439. ' return Result;',
  13440. ' };',
  13441. ' this.GetValue$1 = function (AValue) {',
  13442. ' var Result = 0;',
  13443. ' return Result;',
  13444. ' };',
  13445. '});',
  13446. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13447. ' this.GetValue = function () {',
  13448. ' var Result = 0;',
  13449. ' if (13 === this.GetValue()) ;',
  13450. ' if (14 === this.GetValue$1(15)) ;',
  13451. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  13452. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  13453. ' return Result;',
  13454. ' };',
  13455. ' this.GetValue$1 = function (AValue) {',
  13456. ' var Result = 0;',
  13457. ' return Result;',
  13458. ' };',
  13459. '});',
  13460. 'this.e = null;',
  13461. '']),
  13462. LinesToStr([ // $mod.$main
  13463. 'if (23 === $mod.e.GetValue()) ;',
  13464. 'if (24 === $mod.e.GetValue$1(25)) ;',
  13465. '']));
  13466. end;
  13467. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  13468. begin
  13469. StartProgram(false);
  13470. Add([
  13471. '{$mode delphi}',
  13472. 'type',
  13473. ' TObject = class end;',
  13474. ' TAnimal = class',
  13475. ' public',
  13476. ' {#animal_a}A: longint;',
  13477. ' function {#animal_b}B: longint;',
  13478. ' end;',
  13479. ' TBird = class(TAnimal)',
  13480. ' public',
  13481. ' {#bird_a}A: double;',
  13482. ' {#bird_b}B: boolean;',
  13483. ' end;',
  13484. ' TEagle = class(TBird)',
  13485. ' public',
  13486. ' function {#eagle_a}A: boolean;',
  13487. ' {#eagle_b}B: double;',
  13488. ' end;',
  13489. 'function TAnimal.B: longint;',
  13490. 'begin',
  13491. 'end;',
  13492. 'function TEagle.A: boolean;',
  13493. 'begin',
  13494. ' {@eagle_b}B:=3.3;',
  13495. ' {@eagle_a}A();',
  13496. ' TBird(Self).{@bird_b}B:=true;',
  13497. ' TAnimal(Self).{@animal_a}A:=17;',
  13498. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  13499. 'end;',
  13500. 'var',
  13501. ' e: TEagle;',
  13502. 'begin',
  13503. ' e.{@eagle_b}B:=5.3;',
  13504. ' if e.{@eagle_a}A then ;',
  13505. '']);
  13506. ConvertProgram;
  13507. CheckSource('TestClass_ReintroduceVarDelphi',
  13508. LinesToStr([ // statements
  13509. 'rtl.createClass($mod, "TObject", null, function () {',
  13510. ' this.$init = function () {',
  13511. ' };',
  13512. ' this.$final = function () {',
  13513. ' };',
  13514. '});',
  13515. 'rtl.createClass($mod, "TAnimal", $mod.TObject, function () {',
  13516. ' this.$init = function () {',
  13517. ' $mod.TObject.$init.call(this);',
  13518. ' this.A = 0;',
  13519. ' };',
  13520. ' this.B = function () {',
  13521. ' var Result = 0;',
  13522. ' return Result;',
  13523. ' };',
  13524. '});',
  13525. 'rtl.createClass($mod, "TBird", $mod.TAnimal, function () {',
  13526. ' this.$init = function () {',
  13527. ' $mod.TAnimal.$init.call(this);',
  13528. ' this.A$1 = 0.0;',
  13529. ' this.B$1 = false;',
  13530. ' };',
  13531. '});',
  13532. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13533. ' this.$init = function () {',
  13534. ' $mod.TBird.$init.call(this);',
  13535. ' this.B$2 = 0.0;',
  13536. ' };',
  13537. ' this.A$2 = function () {',
  13538. ' var Result = false;',
  13539. ' this.B$2 = 3.3;',
  13540. ' this.A$2();',
  13541. ' this.B$1 = true;',
  13542. ' this.A = 17;',
  13543. ' this.B$1 = this.A$1 > 1;',
  13544. ' return Result;',
  13545. ' };',
  13546. '});',
  13547. 'this.e = null;',
  13548. '']),
  13549. LinesToStr([ // $mod.$main
  13550. '$mod.e.B$2 = 5.3;',
  13551. 'if ($mod.e.A$2()) ;',
  13552. '']));
  13553. end;
  13554. procedure TTestModule.TestClass_ReintroducedVar;
  13555. begin
  13556. StartProgram(false);
  13557. Add('type');
  13558. Add(' TObject = class');
  13559. Add(' strict private');
  13560. Add(' Some: longint;');
  13561. Add(' end;');
  13562. Add(' TMobile = class');
  13563. Add(' strict private');
  13564. Add(' Some: string;');
  13565. Add(' end;');
  13566. Add(' TCar = class(tmobile)');
  13567. Add(' procedure Some;');
  13568. Add(' procedure Some(vA: longint);');
  13569. Add(' end;');
  13570. Add('procedure tcar.some;');
  13571. Add('begin');
  13572. Add(' Some;');
  13573. Add(' Some(1);');
  13574. Add('end;');
  13575. Add('procedure tcar.some(va: longint); begin end;');
  13576. Add('begin');
  13577. ConvertProgram;
  13578. CheckSource('TestClass_ReintroducedVar',
  13579. LinesToStr([ // statements
  13580. 'rtl.createClass($mod, "TObject", null, function () {',
  13581. ' this.$init = function () {',
  13582. ' this.Some = 0;',
  13583. ' };',
  13584. ' this.$final = function () {',
  13585. ' };',
  13586. '});',
  13587. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13588. ' this.$init = function () {',
  13589. ' $mod.TObject.$init.call(this);',
  13590. ' this.Some$1 = "";',
  13591. ' };',
  13592. '});',
  13593. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13594. ' this.Some$2 = function () {',
  13595. ' this.Some$2();',
  13596. ' this.Some$3(1);',
  13597. ' };',
  13598. ' this.Some$3 = function (vA) {',
  13599. ' };',
  13600. '});',
  13601. '']),
  13602. LinesToStr([ // $mod.$main
  13603. '']));
  13604. end;
  13605. procedure TTestModule.TestClass_RaiseDescendant;
  13606. begin
  13607. StartProgram(false);
  13608. Add([
  13609. 'type',
  13610. ' TObject = class',
  13611. ' constructor Create(Msg: string);',
  13612. ' end;',
  13613. ' Exception = class',
  13614. ' end;',
  13615. ' EConvertError = class(Exception)',
  13616. ' end;',
  13617. 'constructor TObject.Create(Msg: string); begin end;',
  13618. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  13619. 'begin',
  13620. ' raise Exception.Create(''Bar1'');',
  13621. ' raise EConvertError.Create(''Bar2'');',
  13622. ' raise AssertConv(''Bar2'');',
  13623. ' raise AssertConv;',
  13624. '']);
  13625. ConvertProgram;
  13626. CheckSource('TestClass_RaiseDescendant',
  13627. LinesToStr([ // statements
  13628. 'rtl.createClass($mod, "TObject", null, function () {',
  13629. ' this.$init = function () {',
  13630. ' };',
  13631. ' this.$final = function () {',
  13632. ' };',
  13633. ' this.Create = function (Msg) {',
  13634. ' return this;',
  13635. ' };',
  13636. '});',
  13637. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  13638. '});',
  13639. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  13640. '});',
  13641. 'this.AssertConv = function (Msg) {',
  13642. ' var Result = null;',
  13643. ' return Result;',
  13644. '};',
  13645. '']),
  13646. LinesToStr([ // $mod.$main
  13647. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  13648. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  13649. 'throw $mod.AssertConv("Bar2");',
  13650. 'throw $mod.AssertConv("def");',
  13651. '']));
  13652. end;
  13653. procedure TTestModule.TestClass_ExternalMethod;
  13654. begin
  13655. AddModuleWithIntfImplSrc('unit2.pas',
  13656. LinesToStr([
  13657. 'type',
  13658. ' TObject = class',
  13659. ' public',
  13660. ' procedure Intern; external name ''$DoIntern'';',
  13661. ' end;',
  13662. '']),
  13663. LinesToStr([
  13664. '']));
  13665. StartUnit(true);
  13666. Add('interface');
  13667. Add('uses unit2;');
  13668. Add('type');
  13669. Add(' TCar = class(TObject)');
  13670. Add(' public');
  13671. Add(' procedure Intern2; external name ''$DoIntern2'';');
  13672. Add(' procedure DoIt;');
  13673. Add(' end;');
  13674. Add('implementation');
  13675. Add('procedure tcar.doit;');
  13676. Add('begin');
  13677. Add(' Intern;');
  13678. Add(' Intern();');
  13679. Add(' Intern2;');
  13680. Add(' Intern2();');
  13681. Add('end;');
  13682. Add('var Obj: TCar;');
  13683. Add('begin');
  13684. Add(' obj.intern;');
  13685. Add(' obj.intern();');
  13686. Add(' obj.intern2;');
  13687. Add(' obj.intern2();');
  13688. Add(' obj.doit;');
  13689. Add(' obj.doit();');
  13690. Add(' with obj do begin');
  13691. Add(' Intern;');
  13692. Add(' Intern();');
  13693. Add(' Intern2;');
  13694. Add(' Intern2();');
  13695. Add(' end;');
  13696. ConvertUnit;
  13697. CheckSource('TestClass_ExternalMethod',
  13698. LinesToStr([
  13699. 'var $impl = $mod.$impl;',
  13700. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13701. ' this.DoIt = function () {',
  13702. ' this.$DoIntern();',
  13703. ' this.$DoIntern();',
  13704. ' this.$DoIntern2();',
  13705. ' this.$DoIntern2();',
  13706. ' };',
  13707. ' });',
  13708. '']),
  13709. LinesToStr([ // this.$init
  13710. '$impl.Obj.$DoIntern();',
  13711. '$impl.Obj.$DoIntern();',
  13712. '$impl.Obj.$DoIntern2();',
  13713. '$impl.Obj.$DoIntern2();',
  13714. '$impl.Obj.DoIt();',
  13715. '$impl.Obj.DoIt();',
  13716. 'var $with1 = $impl.Obj;',
  13717. '$with1.$DoIntern();',
  13718. '$with1.$DoIntern();',
  13719. '$with1.$DoIntern2();',
  13720. '$with1.$DoIntern2();',
  13721. '']),
  13722. LinesToStr([ // implementation
  13723. '$impl.Obj = null;',
  13724. '']) );
  13725. end;
  13726. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  13727. begin
  13728. StartProgram(false);
  13729. Add('type');
  13730. Add(' TObject = class');
  13731. Add(' procedure DoIt; virtual; external name ''Foo'';');
  13732. Add(' end;');
  13733. Add('begin');
  13734. SetExpectedPasResolverError('Virtual method name must match external',
  13735. nVirtualMethodNameMustMatchExternal);
  13736. ConvertProgram;
  13737. end;
  13738. procedure TTestModule.TestClass_ExternalOverrideFail;
  13739. begin
  13740. StartProgram(false);
  13741. Add('type');
  13742. Add(' TObject = class');
  13743. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  13744. Add(' end;');
  13745. Add(' TCar = class');
  13746. Add(' procedure DoIt; override; external name ''DoIt'';');
  13747. Add(' end;');
  13748. Add('begin');
  13749. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  13750. nInvalidXModifierY);
  13751. ConvertProgram;
  13752. end;
  13753. procedure TTestModule.TestClass_ExternalVar;
  13754. begin
  13755. AddModuleWithIntfImplSrc('unit2.pas',
  13756. LinesToStr([
  13757. '{$modeswitch externalclass}',
  13758. 'type',
  13759. ' TObject = class',
  13760. ' public',
  13761. ' Intern: longint external name ''$Intern'';',
  13762. ' Bracket: longint external name ''["A B"]'';',
  13763. ' end;',
  13764. '']),
  13765. LinesToStr([
  13766. '']));
  13767. StartUnit(true);
  13768. Add([
  13769. 'interface',
  13770. 'uses unit2;',
  13771. '{$modeswitch externalclass}',
  13772. 'type',
  13773. ' TCar = class(tobject)',
  13774. ' public',
  13775. ' Intern2: longint external name ''$Intern2'';',
  13776. ' procedure DoIt;',
  13777. ' end;',
  13778. 'implementation',
  13779. 'procedure tcar.doit;',
  13780. 'begin',
  13781. ' Intern:=Intern+1;',
  13782. ' Intern2:=Intern2+2;',
  13783. ' Bracket:=Bracket+3;',
  13784. 'end;',
  13785. 'var Obj: TCar;',
  13786. 'begin',
  13787. ' obj.intern:=obj.intern+1;',
  13788. ' obj.intern2:=obj.intern2+2;',
  13789. ' obj.Bracket:=obj.Bracket+3;',
  13790. ' with obj do begin',
  13791. ' intern:=intern+1;',
  13792. ' intern2:=intern2+2;',
  13793. ' Bracket:=Bracket+3;',
  13794. ' end;']);
  13795. ConvertUnit;
  13796. CheckSource('TestClass_ExternalVar',
  13797. LinesToStr([
  13798. 'var $impl = $mod.$impl;',
  13799. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13800. ' this.DoIt = function () {',
  13801. ' this.$Intern = this.$Intern + 1;',
  13802. ' this.$Intern2 = this.$Intern2 + 2;',
  13803. ' this["A B"] = this["A B"] + 3;',
  13804. ' };',
  13805. ' });',
  13806. '']),
  13807. LinesToStr([
  13808. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  13809. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  13810. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  13811. 'var $with1 = $impl.Obj;',
  13812. '$with1.$Intern = $with1.$Intern + 1;',
  13813. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  13814. '$with1["A B"] = $with1["A B"] + 3;',
  13815. '']),
  13816. LinesToStr([ // implementation
  13817. '$impl.Obj = null;',
  13818. '']));
  13819. end;
  13820. procedure TTestModule.TestClass_Const;
  13821. begin
  13822. StartProgram(false);
  13823. Add('type');
  13824. Add(' integer = longint;');
  13825. Add(' TClass = class of TObject;');
  13826. Add(' TObject = class');
  13827. Add(' public');
  13828. Add(' const cI: integer = 3;');
  13829. Add(' procedure DoIt;');
  13830. Add(' class procedure DoMore;');
  13831. Add(' end;');
  13832. Add('procedure tobject.doit;');
  13833. Add('begin');
  13834. Add(' if cI=4 then;');
  13835. Add(' if 5=cI then;');
  13836. Add(' if Self.cI=6 then;');
  13837. Add(' if 7=Self.cI then;');
  13838. Add(' with Self do begin');
  13839. Add(' if cI=11 then;');
  13840. Add(' if 12=cI then;');
  13841. Add(' end;');
  13842. Add('end;');
  13843. Add('class procedure tobject.domore;');
  13844. Add('begin');
  13845. Add(' if cI=8 then;');
  13846. Add(' if Self.cI=9 then;');
  13847. Add(' if 10=cI then;');
  13848. Add(' if 11=Self.cI then;');
  13849. Add(' with Self do begin');
  13850. Add(' if cI=13 then;');
  13851. Add(' if 14=cI then;');
  13852. Add(' end;');
  13853. Add('end;');
  13854. Add('var');
  13855. Add(' Obj: TObject;');
  13856. Add(' Cla: TClass;');
  13857. Add('begin');
  13858. Add(' if TObject.cI=21 then ;');
  13859. Add(' if Obj.cI=22 then ;');
  13860. Add(' if Cla.cI=23 then ;');
  13861. Add(' with obj do if ci=24 then;');
  13862. Add(' with TObject do if ci=25 then;');
  13863. Add(' with Cla do if ci=26 then;');
  13864. ConvertProgram;
  13865. CheckSource('TestClass_Const',
  13866. LinesToStr([
  13867. 'rtl.createClass($mod, "TObject", null, function () {',
  13868. ' this.cI = 3;',
  13869. ' this.$init = function () {',
  13870. ' };',
  13871. ' this.$final = function () {',
  13872. ' };',
  13873. ' this.DoIt = function () {',
  13874. ' if (this.cI === 4) ;',
  13875. ' if (5 === this.cI) ;',
  13876. ' if (this.cI === 6) ;',
  13877. ' if (7 === this.cI) ;',
  13878. ' if (this.cI === 11) ;',
  13879. ' if (12 === this.cI) ;',
  13880. ' };',
  13881. ' this.DoMore = function () {',
  13882. ' if (this.cI === 8) ;',
  13883. ' if (this.cI === 9) ;',
  13884. ' if (10 === this.cI) ;',
  13885. ' if (11 === this.cI) ;',
  13886. ' if (this.cI === 13) ;',
  13887. ' if (14 === this.cI) ;',
  13888. ' };',
  13889. '});',
  13890. 'this.Obj = null;',
  13891. 'this.Cla = null;',
  13892. '']),
  13893. LinesToStr([
  13894. 'if ($mod.TObject.cI === 21) ;',
  13895. 'if ($mod.Obj.cI === 22) ;',
  13896. 'if ($mod.Cla.cI === 23) ;',
  13897. 'var $with1 = $mod.Obj;',
  13898. 'if ($with1.cI === 24) ;',
  13899. 'var $with2 = $mod.TObject;',
  13900. 'if ($with2.cI === 25) ;',
  13901. 'var $with3 = $mod.Cla;',
  13902. 'if ($with3.cI === 26) ;',
  13903. '']));
  13904. end;
  13905. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  13906. begin
  13907. StartProgram(false);
  13908. Add([
  13909. 'type',
  13910. ' TObject = class',
  13911. ' const cI: longint = 3;',
  13912. ' procedure Fly;',
  13913. ' procedure Run;',
  13914. ' end;',
  13915. ' TBird = class',
  13916. ' procedure Go;',
  13917. ' end;',
  13918. 'procedure tobject.fly;',
  13919. 'const cI: word = 4;',
  13920. 'begin',
  13921. ' if cI=Self.cI then ;',
  13922. 'end;',
  13923. 'procedure tobject.run;',
  13924. 'const cI: word = 5;',
  13925. 'begin',
  13926. ' if cI=Self.cI then ;',
  13927. 'end;',
  13928. 'procedure tbird.go;',
  13929. 'const cI: word = 6;',
  13930. 'begin',
  13931. ' if cI=Self.cI then ;',
  13932. 'end;',
  13933. 'begin',
  13934. '']);
  13935. ConvertProgram;
  13936. CheckSource('TestClass_LocalConstDuplicate_Prg',
  13937. LinesToStr([
  13938. 'rtl.createClass($mod, "TObject", null, function () {',
  13939. ' this.cI = 3;',
  13940. ' this.$init = function () {',
  13941. ' };',
  13942. ' this.$final = function () {',
  13943. ' };',
  13944. ' var cI$1 = 4;',
  13945. ' this.Fly = function () {',
  13946. ' if (cI$1 === this.cI) ;',
  13947. ' };',
  13948. ' var cI$2 = 5;',
  13949. ' this.Run = function () {',
  13950. ' if (cI$2 === this.cI) ;',
  13951. ' };',
  13952. '});',
  13953. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13954. ' var cI$3 = 6;',
  13955. ' this.Go = function () {',
  13956. ' if (cI$3 === this.cI) ;',
  13957. ' };',
  13958. '});',
  13959. '']),
  13960. LinesToStr([
  13961. '']));
  13962. end;
  13963. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  13964. begin
  13965. StartUnit(false);
  13966. Add([
  13967. 'interface',
  13968. 'type',
  13969. ' TObject = class',
  13970. ' const cI: longint = 3;',
  13971. ' procedure Fly;',
  13972. ' procedure Run;',
  13973. ' end;',
  13974. ' TBird = class',
  13975. ' procedure Go;',
  13976. ' end;',
  13977. 'implementation',
  13978. 'procedure tobject.fly;',
  13979. 'const cI: word = 4;',
  13980. 'begin',
  13981. ' if cI=Self.cI then ;',
  13982. 'end;',
  13983. 'procedure tobject.run;',
  13984. 'const cI: word = 5;',
  13985. 'begin',
  13986. ' if cI=Self.cI then ;',
  13987. 'end;',
  13988. 'procedure tbird.go;',
  13989. 'const cI: word = 6;',
  13990. 'begin',
  13991. ' if cI=Self.cI then ;',
  13992. 'end;',
  13993. '']);
  13994. ConvertUnit;
  13995. CheckSource('TestClass_LocalConstDuplicate_Unit',
  13996. LinesToStr([
  13997. 'rtl.createClass($mod, "TObject", null, function () {',
  13998. ' this.cI = 3;',
  13999. ' this.$init = function () {',
  14000. ' };',
  14001. ' this.$final = function () {',
  14002. ' };',
  14003. ' var cI$1 = 4;',
  14004. ' this.Fly = function () {',
  14005. ' if (cI$1 === this.cI) ;',
  14006. ' };',
  14007. ' var cI$2 = 5;',
  14008. ' this.Run = function () {',
  14009. ' if (cI$2 === this.cI) ;',
  14010. ' };',
  14011. '});',
  14012. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14013. ' var cI$3 = 6;',
  14014. ' this.Go = function () {',
  14015. ' if (cI$3 === this.cI) ;',
  14016. ' };',
  14017. '});',
  14018. '']),
  14019. '',
  14020. '');
  14021. end;
  14022. procedure TTestModule.TestClass_LocalVarSelfFail;
  14023. begin
  14024. StartProgram(false);
  14025. Add([
  14026. 'type',
  14027. ' TObject = class',
  14028. ' constructor Create;',
  14029. ' end;',
  14030. 'constructor tobject.create;',
  14031. 'var self: longint;',
  14032. 'begin',
  14033. 'end',
  14034. 'begin',
  14035. '']);
  14036. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  14037. ConvertProgram;
  14038. end;
  14039. procedure TTestModule.TestClass_ArgSelfFail;
  14040. begin
  14041. StartProgram(false);
  14042. Add([
  14043. 'type',
  14044. ' TObject = class',
  14045. ' procedure DoIt(Self: longint);',
  14046. ' end;',
  14047. 'procedure tobject.doit(self: longint);',
  14048. 'begin',
  14049. 'end',
  14050. 'begin',
  14051. '']);
  14052. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  14053. ConvertProgram;
  14054. end;
  14055. procedure TTestModule.TestClass_NestedProcSelf;
  14056. begin
  14057. StartProgram(false);
  14058. Add([
  14059. 'type',
  14060. ' TObject = class',
  14061. ' Key: longint;',
  14062. ' class var State: longint;',
  14063. ' procedure DoIt;',
  14064. ' function GetSize: longint; virtual; abstract;',
  14065. ' procedure SetSize(Value: longint); virtual; abstract;',
  14066. ' property Size: longint read GetSize write SetSize;',
  14067. ' end;',
  14068. 'procedure tobject.doit;',
  14069. ' procedure Sub;',
  14070. ' begin',
  14071. ' key:=key+2;',
  14072. ' self.key:=self.key+3;',
  14073. ' state:=state+4;',
  14074. ' self.state:=self.state+5;',
  14075. ' tobject.state:=tobject.state+6;',
  14076. ' size:=size+7;',
  14077. ' self.size:=self.size+8;',
  14078. ' end;',
  14079. 'begin',
  14080. ' sub;',
  14081. ' key:=key+12;',
  14082. ' self.key:=self.key+13;',
  14083. ' state:=state+14;',
  14084. ' self.state:=self.state+15;',
  14085. ' tobject.state:=tobject.state+16;',
  14086. ' size:=size+17;',
  14087. ' self.size:=self.size+18;',
  14088. 'end;',
  14089. 'begin',
  14090. '']);
  14091. ConvertProgram;
  14092. CheckSource('TestClass_NestedProcSelf',
  14093. LinesToStr([ // statements
  14094. 'rtl.createClass($mod, "TObject", null, function () {',
  14095. ' this.State = 0;',
  14096. ' this.$init = function () {',
  14097. ' this.Key = 0;',
  14098. ' };',
  14099. ' this.$final = function () {',
  14100. ' };',
  14101. ' this.DoIt = function () {',
  14102. ' var $Self = this;',
  14103. ' function Sub() {',
  14104. ' $Self.Key = $Self.Key + 2;',
  14105. ' $Self.Key = $Self.Key + 3;',
  14106. ' $mod.TObject.State = $Self.State + 4;',
  14107. ' $mod.TObject.State = $Self.State + 5;',
  14108. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14109. ' $Self.SetSize($Self.GetSize() + 7);',
  14110. ' $Self.SetSize($Self.GetSize() + 8);',
  14111. ' };',
  14112. ' Sub();',
  14113. ' $Self.Key = $Self.Key + 12;',
  14114. ' $Self.Key = $Self.Key + 13;',
  14115. ' $mod.TObject.State = $Self.State + 14;',
  14116. ' $mod.TObject.State = $Self.State + 15;',
  14117. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14118. ' $Self.SetSize($Self.GetSize() + 17);',
  14119. ' $Self.SetSize($Self.GetSize() + 18);',
  14120. ' };',
  14121. '});',
  14122. '']),
  14123. LinesToStr([ // $mod.$main
  14124. '']));
  14125. end;
  14126. procedure TTestModule.TestClass_NestedProcSelf2;
  14127. begin
  14128. StartProgram(false);
  14129. Add([
  14130. 'type',
  14131. ' TObject = class',
  14132. ' Key: longint;',
  14133. ' class var State: longint;',
  14134. ' function GetSize: longint; virtual; abstract;',
  14135. ' procedure SetSize(Value: longint); virtual; abstract;',
  14136. ' property Size: longint read GetSize write SetSize;',
  14137. ' end;',
  14138. ' TBird = class',
  14139. ' procedure DoIt;',
  14140. ' end;',
  14141. 'procedure tbird.doit;',
  14142. ' procedure Sub;',
  14143. ' begin',
  14144. ' key:=key+2;',
  14145. ' self.key:=self.key+3;',
  14146. ' state:=state+4;',
  14147. ' self.state:=self.state+5;',
  14148. ' tobject.state:=tobject.state+6;',
  14149. ' size:=size+7;',
  14150. ' self.size:=self.size+8;',
  14151. ' end;',
  14152. 'begin',
  14153. ' sub;',
  14154. ' key:=key+12;',
  14155. ' self.key:=self.key+13;',
  14156. ' state:=state+14;',
  14157. ' self.state:=self.state+15;',
  14158. ' tobject.state:=tobject.state+16;',
  14159. ' size:=size+17;',
  14160. ' self.size:=self.size+18;',
  14161. 'end;',
  14162. 'begin',
  14163. '']);
  14164. ConvertProgram;
  14165. CheckSource('TestClass_NestedProcSelf2',
  14166. LinesToStr([ // statements
  14167. 'rtl.createClass($mod, "TObject", null, function () {',
  14168. ' this.State = 0;',
  14169. ' this.$init = function () {',
  14170. ' this.Key = 0;',
  14171. ' };',
  14172. ' this.$final = function () {',
  14173. ' };',
  14174. '});',
  14175. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14176. ' this.DoIt = function () {',
  14177. ' var $Self = this;',
  14178. ' function Sub() {',
  14179. ' $Self.Key = $Self.Key + 2;',
  14180. ' $Self.Key = $Self.Key + 3;',
  14181. ' $mod.TObject.State = $Self.State + 4;',
  14182. ' $mod.TObject.State = $Self.State + 5;',
  14183. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14184. ' $Self.SetSize($Self.GetSize() + 7);',
  14185. ' $Self.SetSize($Self.GetSize() + 8);',
  14186. ' };',
  14187. ' Sub();',
  14188. ' $Self.Key = $Self.Key + 12;',
  14189. ' $Self.Key = $Self.Key + 13;',
  14190. ' $mod.TObject.State = $Self.State + 14;',
  14191. ' $mod.TObject.State = $Self.State + 15;',
  14192. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14193. ' $Self.SetSize($Self.GetSize() + 17);',
  14194. ' $Self.SetSize($Self.GetSize() + 18);',
  14195. ' };',
  14196. '});',
  14197. '']),
  14198. LinesToStr([ // $mod.$main
  14199. '']));
  14200. end;
  14201. procedure TTestModule.TestClass_NestedProcClassSelf;
  14202. begin
  14203. StartProgram(false);
  14204. Add([
  14205. 'type',
  14206. ' TObject = class',
  14207. ' class var State: longint;',
  14208. ' class procedure DoIt;',
  14209. ' class function GetSize: longint; virtual; abstract;',
  14210. ' class procedure SetSize(Value: longint); virtual; abstract;',
  14211. ' class property Size: longint read GetSize write SetSize;',
  14212. ' end;',
  14213. 'class procedure tobject.doit;',
  14214. ' procedure Sub;',
  14215. ' begin',
  14216. ' state:=state+2;',
  14217. ' self.state:=self.state+3;',
  14218. ' tobject.state:=tobject.state+4;',
  14219. ' size:=size+5;',
  14220. ' self.size:=self.size+6;',
  14221. ' tobject.size:=tobject.size+7;',
  14222. ' end;',
  14223. 'begin',
  14224. ' sub;',
  14225. ' state:=state+12;',
  14226. ' self.state:=self.state+13;',
  14227. ' tobject.state:=tobject.state+14;',
  14228. ' size:=size+15;',
  14229. ' self.size:=self.size+16;',
  14230. ' tobject.size:=tobject.size+17;',
  14231. 'end;',
  14232. 'begin',
  14233. '']);
  14234. ConvertProgram;
  14235. CheckSource('TestClass_NestedProcClassSelf',
  14236. LinesToStr([ // statements
  14237. 'rtl.createClass($mod, "TObject", null, function () {',
  14238. ' this.State = 0;',
  14239. ' this.$init = function () {',
  14240. ' };',
  14241. ' this.$final = function () {',
  14242. ' };',
  14243. ' this.DoIt = function () {',
  14244. ' var $Self = this;',
  14245. ' function Sub() {',
  14246. ' $mod.TObject.State = $Self.State + 2;',
  14247. ' $mod.TObject.State = $Self.State + 3;',
  14248. ' $mod.TObject.State = $mod.TObject.State + 4;',
  14249. ' $Self.SetSize($Self.GetSize() + 5);',
  14250. ' $Self.SetSize($Self.GetSize() + 6);',
  14251. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  14252. ' };',
  14253. ' Sub();',
  14254. ' $mod.TObject.State = $Self.State + 12;',
  14255. ' $mod.TObject.State = $Self.State + 13;',
  14256. ' $mod.TObject.State = $mod.TObject.State + 14;',
  14257. ' $Self.SetSize($Self.GetSize() + 15);',
  14258. ' $Self.SetSize($Self.GetSize() + 16);',
  14259. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  14260. ' };',
  14261. '});',
  14262. '']),
  14263. LinesToStr([ // $mod.$main
  14264. '']));
  14265. end;
  14266. procedure TTestModule.TestClass_NestedProcCallInherited;
  14267. begin
  14268. StartProgram(false);
  14269. Add([
  14270. 'type',
  14271. ' TObject = class',
  14272. ' function DoIt(k: boolean): longint; virtual;',
  14273. ' end;',
  14274. ' TBird = class',
  14275. ' function DoIt(k: boolean): longint; override;',
  14276. ' end;',
  14277. 'function tobject.doit(k: boolean): longint;',
  14278. 'begin',
  14279. 'end;',
  14280. 'function tbird.doit(k: boolean): longint;',
  14281. ' procedure Sub;',
  14282. ' begin',
  14283. ' inherited DoIt(true);',
  14284. //' if inherited DoIt(false)=4 then ;',
  14285. ' end;',
  14286. 'begin',
  14287. ' Sub;',
  14288. ' inherited;',
  14289. ' inherited DoIt(true);',
  14290. //' if inherited DoIt(false)=14 then ;',
  14291. 'end;',
  14292. 'begin',
  14293. '']);
  14294. ConvertProgram;
  14295. CheckSource('TestClass_NestedProcCallInherited',
  14296. LinesToStr([ // statements
  14297. 'rtl.createClass($mod, "TObject", null, function () {',
  14298. ' this.$init = function () {',
  14299. ' };',
  14300. ' this.$final = function () {',
  14301. ' };',
  14302. ' this.DoIt = function (k) {',
  14303. ' var Result = 0;',
  14304. ' return Result;',
  14305. ' };',
  14306. '});',
  14307. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14308. ' this.DoIt = function (k) {',
  14309. ' var $Self = this;',
  14310. ' var Result = 0;',
  14311. ' function Sub() {',
  14312. ' $mod.TObject.DoIt.call($Self, true);',
  14313. ' };',
  14314. ' Sub();',
  14315. ' $mod.TObject.DoIt.apply($Self, arguments);',
  14316. ' $mod.TObject.DoIt.call($Self, true);',
  14317. ' return Result;',
  14318. ' };',
  14319. '});',
  14320. '']),
  14321. LinesToStr([ // $mod.$main
  14322. '']));
  14323. end;
  14324. procedure TTestModule.TestClass_TObjectFree;
  14325. begin
  14326. StartProgram(false);
  14327. Add([
  14328. 'type',
  14329. ' TObject = class',
  14330. ' Obj: tobject;',
  14331. ' procedure Free;',
  14332. ' procedure Release;',
  14333. ' end;',
  14334. 'procedure tobject.free;',
  14335. 'begin',
  14336. 'end;',
  14337. 'procedure tobject.release;',
  14338. 'begin',
  14339. ' free;',
  14340. ' if true then free;',
  14341. 'end;',
  14342. 'function DoIt(o: tobject): tobject;',
  14343. 'var l: tobject;',
  14344. 'begin',
  14345. ' o.free;',
  14346. ' o.free();',
  14347. ' l.free;',
  14348. ' l.free();',
  14349. ' o.obj.free;',
  14350. ' o.obj.free();',
  14351. ' with o do obj.free;',
  14352. ' with o do obj.free();',
  14353. ' result.Free;',
  14354. ' result.Free();',
  14355. 'end;',
  14356. 'var o: tobject;',
  14357. ' a: array of tobject;',
  14358. 'begin',
  14359. ' o.free;',
  14360. ' o.obj.free;',
  14361. ' a[1+2].free;',
  14362. '']);
  14363. ConvertProgram;
  14364. CheckSource('TestClass_TObjectFree',
  14365. LinesToStr([ // statements
  14366. 'rtl.createClass($mod, "TObject", null, function () {',
  14367. ' this.$init = function () {',
  14368. ' this.Obj = null;',
  14369. ' };',
  14370. ' this.$final = function () {',
  14371. ' this.Obj = undefined;',
  14372. ' };',
  14373. ' this.Free = function () {',
  14374. ' };',
  14375. ' this.Release = function () {',
  14376. ' this.Free();',
  14377. ' if (true) this.Free();',
  14378. ' };',
  14379. '});',
  14380. 'this.DoIt = function (o) {',
  14381. ' var Result = null;',
  14382. ' var l = null;',
  14383. ' o = rtl.freeLoc(o);',
  14384. ' o = rtl.freeLoc(o);',
  14385. ' l = rtl.freeLoc(l);',
  14386. ' l = rtl.freeLoc(l);',
  14387. ' rtl.free(o, "Obj");',
  14388. ' rtl.free(o, "Obj");',
  14389. ' rtl.free(o, "Obj");',
  14390. ' rtl.free(o, "Obj");',
  14391. ' Result = rtl.freeLoc(Result);',
  14392. ' Result = rtl.freeLoc(Result);',
  14393. ' return Result;',
  14394. '};',
  14395. 'this.o = null;',
  14396. 'this.a = [];',
  14397. '']),
  14398. LinesToStr([ // $mod.$main
  14399. 'rtl.free($mod, "o");',
  14400. 'rtl.free($mod.o, "Obj");',
  14401. 'rtl.free($mod.a, 1 + 2);',
  14402. '']));
  14403. end;
  14404. procedure TTestModule.TestClass_TObjectFree_VarArg;
  14405. begin
  14406. StartProgram(false);
  14407. Add([
  14408. 'type',
  14409. ' TObject = class',
  14410. ' Obj: tobject;',
  14411. ' procedure Free;',
  14412. ' end;',
  14413. 'procedure tobject.free;',
  14414. 'begin',
  14415. 'end;',
  14416. 'procedure DoIt(var o: tobject);',
  14417. 'begin',
  14418. ' o.free;',
  14419. ' o.free();',
  14420. 'end;',
  14421. 'begin',
  14422. '']);
  14423. ConvertProgram;
  14424. CheckSource('TestClass_TObjectFree_VarArg',
  14425. LinesToStr([ // statements
  14426. 'rtl.createClass($mod, "TObject", null, function () {',
  14427. ' this.$init = function () {',
  14428. ' this.Obj = null;',
  14429. ' };',
  14430. ' this.$final = function () {',
  14431. ' this.Obj = undefined;',
  14432. ' };',
  14433. ' this.Free = function () {',
  14434. ' };',
  14435. '});',
  14436. 'this.DoIt = function (o) {',
  14437. ' o.set(rtl.freeLoc(o.get()));',
  14438. ' o.set(rtl.freeLoc(o.get()));',
  14439. '};',
  14440. '']),
  14441. LinesToStr([ // $mod.$main
  14442. '']));
  14443. end;
  14444. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  14445. begin
  14446. StartProgram(false);
  14447. Add([
  14448. 'type',
  14449. ' TObject = class',
  14450. ' constructor Create;',
  14451. ' procedure Free;',
  14452. ' end;',
  14453. 'constructor TObject.Create; begin end;',
  14454. 'procedure tobject.free; begin end;',
  14455. 'begin',
  14456. ' with tobject.create do free;',
  14457. '']);
  14458. ConvertProgram;
  14459. CheckSource('TestClass_TObjectFreeNewInstance',
  14460. LinesToStr([ // statements
  14461. 'rtl.createClass($mod, "TObject", null, function () {',
  14462. ' this.$init = function () {',
  14463. ' };',
  14464. ' this.$final = function () {',
  14465. ' };',
  14466. ' this.Create = function () {',
  14467. ' return this;',
  14468. ' };',
  14469. ' this.Free = function () {',
  14470. ' };',
  14471. '});',
  14472. '']),
  14473. LinesToStr([ // $mod.$main
  14474. 'var $with1 = $mod.TObject.$create("Create");',
  14475. '$with1=rtl.freeLoc($with1);',
  14476. '']));
  14477. end;
  14478. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  14479. begin
  14480. StartProgram(false);
  14481. Add([
  14482. 'type',
  14483. ' TObject = class',
  14484. ' destructor Destroy;',
  14485. ' procedure Free;',
  14486. ' end;',
  14487. 'destructor TObject.Destroy; begin end;',
  14488. 'procedure tobject.free; begin end;',
  14489. 'var o: tobject;',
  14490. 'begin',
  14491. ' o.free;',
  14492. '']);
  14493. Converter.UseLowerCase:=true;
  14494. ConvertProgram;
  14495. CheckSource('TestClass_TObjectFreeLowerCase',
  14496. LinesToStr([ // statements
  14497. 'rtl.createClass($mod, "tobject", null, function () {',
  14498. ' this.$init = function () {',
  14499. ' };',
  14500. ' this.$final = function () {',
  14501. ' };',
  14502. ' rtl.tObjectDestroy = "destroy";',
  14503. ' this.destroy = function () {',
  14504. ' };',
  14505. ' this.free = function () {',
  14506. ' };',
  14507. '});',
  14508. 'this.o = null;',
  14509. '']),
  14510. LinesToStr([ // $mod.$main
  14511. 'rtl.free($mod, "o");',
  14512. '']));
  14513. end;
  14514. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  14515. begin
  14516. StartProgram(false);
  14517. Add([
  14518. 'type',
  14519. ' TObject = class',
  14520. ' procedure Free;',
  14521. ' function GetObj: tobject; virtual; abstract;',
  14522. ' end;',
  14523. 'procedure tobject.free;',
  14524. 'begin',
  14525. 'end;',
  14526. 'var o: tobject;',
  14527. 'begin',
  14528. ' o.getobj.free;',
  14529. '']);
  14530. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14531. ConvertProgram;
  14532. end;
  14533. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  14534. begin
  14535. StartProgram(false);
  14536. Add([
  14537. 'type',
  14538. ' TObject = class',
  14539. ' procedure Free;',
  14540. ' FObj: TObject;',
  14541. ' property Obj: tobject read FObj write FObj;',
  14542. ' end;',
  14543. 'procedure tobject.free;',
  14544. 'begin',
  14545. 'end;',
  14546. 'var o: tobject;',
  14547. 'begin',
  14548. ' o.obj.free;',
  14549. '']);
  14550. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14551. ConvertProgram;
  14552. end;
  14553. procedure TTestModule.TestClass_ForIn;
  14554. begin
  14555. StartProgram(false);
  14556. Add([
  14557. 'type',
  14558. ' TObject = class end;',
  14559. ' TItem = TObject;',
  14560. ' TEnumerator = class',
  14561. ' FCurrent: TItem;',
  14562. ' property Current: TItem read FCurrent;',
  14563. ' function MoveNext: boolean;',
  14564. ' end;',
  14565. ' TBird = class',
  14566. ' function GetEnumerator: TEnumerator;',
  14567. ' end;',
  14568. 'function TEnumerator.MoveNext: boolean;',
  14569. 'begin',
  14570. 'end;',
  14571. 'function TBird.GetEnumerator: TEnumerator;',
  14572. 'begin',
  14573. 'end;',
  14574. 'var',
  14575. ' b: TBird;',
  14576. ' i, i2: TItem;',
  14577. 'begin',
  14578. ' for i in b do i2:=i;']);
  14579. ConvertProgram;
  14580. CheckSource('TestClass_ForIn',
  14581. LinesToStr([ // statements
  14582. 'rtl.createClass($mod, "TObject", null, function () {',
  14583. ' this.$init = function () {',
  14584. ' };',
  14585. ' this.$final = function () {',
  14586. ' };',
  14587. '});',
  14588. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  14589. ' this.$init = function () {',
  14590. ' $mod.TObject.$init.call(this);',
  14591. ' this.FCurrent = null;',
  14592. ' };',
  14593. ' this.$final = function () {',
  14594. ' this.FCurrent = undefined;',
  14595. ' $mod.TObject.$final.call(this);',
  14596. ' };',
  14597. ' this.MoveNext = function () {',
  14598. ' var Result = false;',
  14599. ' return Result;',
  14600. ' };',
  14601. '});',
  14602. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14603. ' this.GetEnumerator = function () {',
  14604. ' var Result = null;',
  14605. ' return Result;',
  14606. ' };',
  14607. '});',
  14608. 'this.b = null;',
  14609. 'this.i = null;',
  14610. 'this.i2 = null;'
  14611. ]),
  14612. LinesToStr([ // $mod.$main
  14613. 'var $in1 = $mod.b.GetEnumerator();',
  14614. 'try {',
  14615. ' while ($in1.MoveNext()){',
  14616. ' $mod.i = $in1.FCurrent;',
  14617. ' $mod.i2 = $mod.i;',
  14618. ' }',
  14619. '} finally {',
  14620. ' $in1 = rtl.freeLoc($in1)',
  14621. '};',
  14622. '']));
  14623. end;
  14624. procedure TTestModule.TestClass_DispatchMessage;
  14625. begin
  14626. StartProgram(false);
  14627. Add([
  14628. 'type',
  14629. ' TObject = class',
  14630. ' {$DispatchField DispInt}',
  14631. ' procedure Dispatch(var Msg); virtual; abstract;',
  14632. ' {$DispatchStrField DispStr}',
  14633. ' procedure DispatchStr(var Msg); virtual; abstract;',
  14634. ' end;',
  14635. ' THopMsg = record',
  14636. ' DispInt: longint;',
  14637. ' end;',
  14638. ' TPutMsg = record',
  14639. ' DispStr: string;',
  14640. ' end;',
  14641. ' TBird = class',
  14642. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  14643. ' procedure Run; overload; virtual; abstract;',
  14644. ' procedure Run(var Msg); overload; message ''Fast'';',
  14645. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  14646. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  14647. ' end;',
  14648. 'procedure TBird.Run(var Msg);',
  14649. 'begin',
  14650. 'end;',
  14651. 'begin',
  14652. '']);
  14653. ConvertProgram;
  14654. CheckSource('TestClass_Message',
  14655. LinesToStr([ // statements
  14656. 'rtl.createClass($mod, "TObject", null, function () {',
  14657. ' this.$init = function () {',
  14658. ' };',
  14659. ' this.$final = function () {',
  14660. ' };',
  14661. '});',
  14662. 'rtl.recNewT($mod, "THopMsg", function () {',
  14663. ' this.DispInt = 0;',
  14664. ' this.$eq = function (b) {',
  14665. ' return this.DispInt === b.DispInt;',
  14666. ' };',
  14667. ' this.$assign = function (s) {',
  14668. ' this.DispInt = s.DispInt;',
  14669. ' return this;',
  14670. ' };',
  14671. '});',
  14672. 'rtl.recNewT($mod, "TPutMsg", function () {',
  14673. ' this.DispStr = "";',
  14674. ' this.$eq = function (b) {',
  14675. ' return this.DispStr === b.DispStr;',
  14676. ' };',
  14677. ' this.$assign = function (s) {',
  14678. ' this.DispStr = s.DispStr;',
  14679. ' return this;',
  14680. ' };',
  14681. '});',
  14682. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14683. ' this.Run$1 = function (Msg) {',
  14684. ' };',
  14685. ' this.$msgint = {',
  14686. ' "2": "Fly",',
  14687. ' "3": "Hop"',
  14688. ' };',
  14689. ' this.$msgstr = {',
  14690. ' Fast: "Run$1",',
  14691. ' foo: "Put"',
  14692. ' };',
  14693. '});',
  14694. '']),
  14695. LinesToStr([ // $mod.$main
  14696. '']));
  14697. end;
  14698. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  14699. begin
  14700. StartProgram(false);
  14701. Add([
  14702. 'type',
  14703. ' TObject = class',
  14704. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  14705. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  14706. ' end;',
  14707. 'begin',
  14708. '']);
  14709. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  14710. ConvertProgram;
  14711. end;
  14712. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  14713. begin
  14714. StartProgram(false);
  14715. Add([
  14716. 'type',
  14717. ' TObject = class',
  14718. ' {$dispatchfield Msg}',
  14719. ' procedure Dispatch(var Msg); virtual; abstract;',
  14720. ' end;',
  14721. ' TFlyMsg = record',
  14722. ' FlyId: longint;',
  14723. ' end;',
  14724. ' TBird = class',
  14725. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  14726. ' end;',
  14727. 'begin',
  14728. '']);
  14729. ConvertProgram;
  14730. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  14731. end;
  14732. procedure TTestModule.TestClassOf_Create;
  14733. begin
  14734. StartProgram(false);
  14735. Add('type');
  14736. Add(' TObject = class');
  14737. Add(' constructor Create;');
  14738. Add(' end;');
  14739. Add(' TClass = class of TObject;');
  14740. Add('constructor tobject.create; begin end;');
  14741. Add('var');
  14742. Add(' Obj: tobject;');
  14743. Add(' C: tclass;');
  14744. Add('begin');
  14745. Add(' obj:=C.create;');
  14746. Add(' with c do obj:=create;');
  14747. ConvertProgram;
  14748. CheckSource('TestClassOf_Create',
  14749. LinesToStr([ // statements
  14750. 'rtl.createClass($mod, "TObject", null, function () {',
  14751. ' this.$init = function () {',
  14752. ' };',
  14753. ' this.$final = function () {',
  14754. ' };',
  14755. ' this.Create = function () {',
  14756. ' return this;',
  14757. ' };',
  14758. '});',
  14759. 'this.Obj = null;',
  14760. 'this.C = null;'
  14761. ]),
  14762. LinesToStr([ // $mod.$main
  14763. '$mod.Obj = $mod.C.$create("Create");',
  14764. 'var $with1 = $mod.C;',
  14765. '$mod.Obj = $with1.$create("Create");',
  14766. '']));
  14767. end;
  14768. procedure TTestModule.TestClassOf_Call;
  14769. begin
  14770. StartProgram(false);
  14771. Add('type');
  14772. Add(' TObject = class');
  14773. Add(' class procedure DoIt;');
  14774. Add(' end;');
  14775. Add(' TClass = class of TObject;');
  14776. Add('class procedure tobject.doit; begin end;');
  14777. Add('var');
  14778. Add(' C: tclass;');
  14779. Add('begin');
  14780. Add(' c.doit;');
  14781. Add(' with c do doit;');
  14782. ConvertProgram;
  14783. CheckSource('TestClassOf_Call',
  14784. LinesToStr([ // statements
  14785. 'rtl.createClass($mod, "TObject", null, function () {',
  14786. ' this.$init = function () {',
  14787. ' };',
  14788. ' this.$final = function () {',
  14789. ' };',
  14790. ' this.DoIt = function () {',
  14791. ' };',
  14792. '});',
  14793. 'this.C = null;'
  14794. ]),
  14795. LinesToStr([ // $mod.$main
  14796. '$mod.C.DoIt();',
  14797. 'var $with1 = $mod.C;',
  14798. '$with1.DoIt();',
  14799. '']));
  14800. end;
  14801. procedure TTestModule.TestClassOf_Assign;
  14802. begin
  14803. StartProgram(false);
  14804. Add('type');
  14805. Add(' TClass = class of TObject;');
  14806. Add(' TObject = class');
  14807. Add(' ClassType: TClass; ');
  14808. Add(' end;');
  14809. Add('var');
  14810. Add(' Obj: tobject;');
  14811. Add(' C: tclass;');
  14812. Add('begin');
  14813. Add(' c:=nil;');
  14814. Add(' c:=obj.classtype;');
  14815. ConvertProgram;
  14816. CheckSource('TestClassOf_Assign',
  14817. LinesToStr([ // statements
  14818. 'rtl.createClass($mod, "TObject", null, function () {',
  14819. ' this.$init = function () {',
  14820. ' this.ClassType = null;',
  14821. ' };',
  14822. ' this.$final = function () {',
  14823. ' this.ClassType = undefined;',
  14824. ' };',
  14825. '});',
  14826. 'this.Obj = null;',
  14827. 'this.C = null;'
  14828. ]),
  14829. LinesToStr([ // $mod.$main
  14830. '$mod.C = null;',
  14831. '$mod.C = $mod.Obj.ClassType;',
  14832. '']));
  14833. end;
  14834. procedure TTestModule.TestClassOf_Is;
  14835. begin
  14836. StartProgram(false);
  14837. Add('type');
  14838. Add(' TClass = class of TObject;');
  14839. Add(' TObject = class');
  14840. Add(' end;');
  14841. Add(' TCar = class');
  14842. Add(' end;');
  14843. Add(' TCars = class of TCar;');
  14844. Add('var');
  14845. Add(' Obj: tobject;');
  14846. Add(' C: tclass;');
  14847. Add(' Cars: tcars;');
  14848. Add('begin');
  14849. Add(' if c is tcar then ;');
  14850. Add(' if c is tcars then ;');
  14851. ConvertProgram;
  14852. CheckSource('TestClassOf_Is',
  14853. LinesToStr([ // statements
  14854. 'rtl.createClass($mod, "TObject", null, function () {',
  14855. ' this.$init = function () {',
  14856. ' };',
  14857. ' this.$final = function () {',
  14858. ' };',
  14859. '});',
  14860. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  14861. '});',
  14862. 'this.Obj = null;',
  14863. 'this.C = null;',
  14864. 'this.Cars = null;'
  14865. ]),
  14866. LinesToStr([ // $mod.$main
  14867. 'if(rtl.is($mod.C,$mod.TCar));',
  14868. 'if(rtl.is($mod.C,$mod.TCar));',
  14869. '']));
  14870. end;
  14871. procedure TTestModule.TestClassOf_Compare;
  14872. begin
  14873. StartProgram(false);
  14874. Add('type');
  14875. Add(' TClass = class of TObject;');
  14876. Add(' TObject = class');
  14877. Add(' ClassType: TClass; ');
  14878. Add(' end;');
  14879. Add('var');
  14880. Add(' b: boolean;');
  14881. Add(' Obj: tobject;');
  14882. Add(' C: tclass;');
  14883. Add('begin');
  14884. Add(' b:=c=nil;');
  14885. Add(' b:=nil=c;');
  14886. Add(' b:=c=obj.classtype;');
  14887. Add(' b:=obj.classtype=c;');
  14888. Add(' b:=c=TObject;');
  14889. Add(' b:=TObject=c;');
  14890. Add(' b:=c<>nil;');
  14891. Add(' b:=nil<>c;');
  14892. Add(' b:=c<>obj.classtype;');
  14893. Add(' b:=obj.classtype<>c;');
  14894. Add(' b:=c<>TObject;');
  14895. Add(' b:=TObject<>c;');
  14896. ConvertProgram;
  14897. CheckSource('TestClassOf_Compare',
  14898. LinesToStr([ // statements
  14899. 'rtl.createClass($mod, "TObject", null, function () {',
  14900. ' this.$init = function () {',
  14901. ' this.ClassType = null;',
  14902. ' };',
  14903. ' this.$final = function () {',
  14904. ' this.ClassType = undefined;',
  14905. ' };',
  14906. '});',
  14907. 'this.b = false;',
  14908. 'this.Obj = null;',
  14909. 'this.C = null;'
  14910. ]),
  14911. LinesToStr([ // $mod.$main
  14912. '$mod.b = $mod.C === null;',
  14913. '$mod.b = null === $mod.C;',
  14914. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  14915. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  14916. '$mod.b = $mod.C === $mod.TObject;',
  14917. '$mod.b = $mod.TObject === $mod.C;',
  14918. '$mod.b = $mod.C !== null;',
  14919. '$mod.b = null !== $mod.C;',
  14920. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  14921. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  14922. '$mod.b = $mod.C !== $mod.TObject;',
  14923. '$mod.b = $mod.TObject !== $mod.C;',
  14924. '']));
  14925. end;
  14926. procedure TTestModule.TestClassOf_ClassVar;
  14927. begin
  14928. StartProgram(false);
  14929. Add('type');
  14930. Add(' TObject = class');
  14931. Add(' class var id: longint;');
  14932. Add(' end;');
  14933. Add(' TClass = class of TObject;');
  14934. Add('var');
  14935. Add(' C: tclass;');
  14936. Add('begin');
  14937. Add(' C.id:=C.id;');
  14938. ConvertProgram;
  14939. CheckSource('TestClassOf_ClassVar',
  14940. LinesToStr([ // statements
  14941. 'rtl.createClass($mod, "TObject", null, function () {',
  14942. ' this.id = 0;',
  14943. ' this.$init = function () {',
  14944. ' };',
  14945. ' this.$final = function () {',
  14946. ' };',
  14947. '});',
  14948. 'this.C = null;'
  14949. ]),
  14950. LinesToStr([ // $mod.$main
  14951. '$mod.TObject.id = $mod.C.id;',
  14952. '']));
  14953. end;
  14954. procedure TTestModule.TestClassOf_ClassMethod;
  14955. begin
  14956. StartProgram(false);
  14957. Add('type');
  14958. Add(' TObject = class');
  14959. Add(' class function DoIt(i: longint = 0): longint;');
  14960. Add(' end;');
  14961. Add(' TClass = class of TObject;');
  14962. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  14963. Add('var');
  14964. Add(' i: longint;');
  14965. Add(' C: tclass;');
  14966. Add('begin');
  14967. Add(' C.DoIt;');
  14968. Add(' C.DoIt();');
  14969. Add(' i:=C.DoIt;');
  14970. Add(' i:=C.DoIt();');
  14971. ConvertProgram;
  14972. CheckSource('TestClassOf_ClassMethod',
  14973. LinesToStr([ // statements
  14974. 'rtl.createClass($mod, "TObject", null, function () {',
  14975. ' this.$init = function () {',
  14976. ' };',
  14977. ' this.$final = function () {',
  14978. ' };',
  14979. ' this.DoIt = function (i) {',
  14980. ' var Result = 0;',
  14981. ' return Result;',
  14982. ' };',
  14983. '});',
  14984. 'this.i = 0;',
  14985. 'this.C = null;'
  14986. ]),
  14987. LinesToStr([ // $mod.$main
  14988. '$mod.C.DoIt(0);',
  14989. '$mod.C.DoIt(0);',
  14990. '$mod.i = $mod.C.DoIt(0);',
  14991. '$mod.i = $mod.C.DoIt(0);',
  14992. '']));
  14993. end;
  14994. procedure TTestModule.TestClassOf_ClassProperty;
  14995. begin
  14996. StartProgram(false);
  14997. Add([
  14998. 'type',
  14999. ' TObject = class',
  15000. ' class var FA: longint;',
  15001. ' class function GetA: longint;',
  15002. ' class procedure SetA(Value: longint);',
  15003. ' class property pA: longint read fa write fa;',
  15004. ' class property pB: longint read geta write seta;',
  15005. ' end;',
  15006. ' TObjectClass = class of tobject;',
  15007. 'class function tobject.geta: longint; begin end;',
  15008. 'class procedure tobject.seta(value: longint); begin end;',
  15009. 'var',
  15010. ' b: boolean;',
  15011. ' Obj: tobject;',
  15012. ' Cla: tobjectclass;',
  15013. 'begin',
  15014. ' obj.pa:=obj.pa;',
  15015. ' obj.pb:=obj.pb;',
  15016. ' b:=obj.pa=4;',
  15017. ' b:=obj.pb=obj.pb;',
  15018. ' b:=5=obj.pa;',
  15019. ' cla.pa:=6;',
  15020. ' cla.pa:=cla.pa;',
  15021. ' cla.pb:=cla.pb;',
  15022. ' b:=cla.pa=7;',
  15023. ' b:=cla.pb=cla.pb;',
  15024. ' b:=8=cla.pa;',
  15025. ' tobject.pa:=9;',
  15026. ' tobject.pb:=tobject.pb;',
  15027. ' b:=tobject.pa=10;',
  15028. ' b:=11=tobject.pa;',
  15029. '']);
  15030. ConvertProgram;
  15031. CheckSource('TestClassOf_ClassProperty',
  15032. LinesToStr([ // statements
  15033. 'rtl.createClass($mod, "TObject", null, function () {',
  15034. ' this.FA = 0;',
  15035. ' this.$init = function () {',
  15036. ' };',
  15037. ' this.$final = function () {',
  15038. ' };',
  15039. ' this.GetA = function () {',
  15040. ' var Result = 0;',
  15041. ' return Result;',
  15042. ' };',
  15043. ' this.SetA = function (Value) {',
  15044. ' };',
  15045. '});',
  15046. 'this.b = false;',
  15047. 'this.Obj = null;',
  15048. 'this.Cla = null;'
  15049. ]),
  15050. LinesToStr([ // $mod.$main
  15051. '$mod.TObject.FA = $mod.Obj.FA;',
  15052. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  15053. '$mod.b = $mod.Obj.FA === 4;',
  15054. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  15055. '$mod.b = 5 === $mod.Obj.FA;',
  15056. '$mod.TObject.FA = 6;',
  15057. '$mod.TObject.FA = $mod.Cla.FA;',
  15058. '$mod.Cla.SetA($mod.Cla.GetA());',
  15059. '$mod.b = $mod.Cla.FA === 7;',
  15060. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  15061. '$mod.b = 8 === $mod.Cla.FA;',
  15062. '$mod.TObject.FA = 9;',
  15063. '$mod.TObject.SetA($mod.TObject.GetA());',
  15064. '$mod.b = $mod.TObject.FA === 10;',
  15065. '$mod.b = 11 === $mod.TObject.FA;',
  15066. '']));
  15067. end;
  15068. procedure TTestModule.TestClassOf_ClassMethodSelf;
  15069. begin
  15070. StartProgram(false);
  15071. Add('type');
  15072. Add(' TObject = class');
  15073. Add(' class var GlobalId: longint;');
  15074. Add(' class procedure ProcA;');
  15075. Add(' end;');
  15076. Add('class procedure tobject.proca;');
  15077. Add('var b: boolean;');
  15078. Add('begin');
  15079. Add(' b:=self=nil;');
  15080. Add(' b:=self.globalid=3;');
  15081. Add(' b:=4=self.globalid;');
  15082. Add(' self.globalid:=5;');
  15083. Add(' self.proca;');
  15084. Add('end;');
  15085. Add('begin');
  15086. ConvertProgram;
  15087. CheckSource('TestClassOf_ClassMethodSelf',
  15088. LinesToStr([ // statements
  15089. 'rtl.createClass($mod, "TObject", null, function () {',
  15090. ' this.GlobalId = 0;',
  15091. ' this.$init = function () {',
  15092. ' };',
  15093. ' this.$final = function () {',
  15094. ' };',
  15095. ' this.ProcA = function () {',
  15096. ' var b = false;',
  15097. ' b = this === null;',
  15098. ' b = this.GlobalId === 3;',
  15099. ' b = 4 === this.GlobalId;',
  15100. ' $mod.TObject.GlobalId = 5;',
  15101. ' this.ProcA();',
  15102. ' };',
  15103. '});'
  15104. ]),
  15105. LinesToStr([ // $mod.$main
  15106. '']));
  15107. end;
  15108. procedure TTestModule.TestClassOf_TypeCast;
  15109. begin
  15110. StartProgram(false);
  15111. Add('type');
  15112. Add(' TObject = class');
  15113. Add(' class procedure {#TObject_DoIt}DoIt;');
  15114. Add(' end;');
  15115. Add(' TClass = class of TObject;');
  15116. Add(' TMobile = class');
  15117. Add(' class procedure {#TMobile_DoIt}DoIt;');
  15118. Add(' end;');
  15119. Add(' TMobileClass = class of TMobile;');
  15120. Add(' TCar = class(TMobile)');
  15121. Add(' class procedure {#TCar_DoIt}DoIt;');
  15122. Add(' end;');
  15123. Add(' TCarClass = class of TCar;');
  15124. Add('class procedure TObject.DoIt;');
  15125. Add('begin');
  15126. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15127. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15128. Add('end;');
  15129. Add('class procedure TMobile.DoIt;');
  15130. Add('begin');
  15131. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15132. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15133. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  15134. Add('end;');
  15135. Add('class procedure TCar.DoIt; begin end;');
  15136. Add('var');
  15137. Add(' ObjC: TClass;');
  15138. Add(' MobileC: TMobileClass;');
  15139. Add(' CarC: TCarClass;');
  15140. Add('begin');
  15141. Add(' ObjC.{@TObject_DoIt}DoIt;');
  15142. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  15143. Add(' CarC.{@TCar_DoIt}DoIt;');
  15144. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  15145. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  15146. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  15147. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  15148. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  15149. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  15150. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  15151. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  15152. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  15153. ConvertProgram;
  15154. CheckSource('TestClassOf_TypeCast',
  15155. LinesToStr([ // statements
  15156. 'rtl.createClass($mod, "TObject", null, function () {',
  15157. ' this.$init = function () {',
  15158. ' };',
  15159. ' this.$final = function () {',
  15160. ' };',
  15161. ' this.DoIt = function () {',
  15162. ' this.DoIt();',
  15163. ' this.DoIt$1();',
  15164. ' };',
  15165. '});',
  15166. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  15167. ' this.DoIt$1 = function () {',
  15168. ' this.DoIt();',
  15169. ' this.DoIt$1();',
  15170. ' this.DoIt$2();',
  15171. ' };',
  15172. '});',
  15173. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  15174. ' this.DoIt$2 = function () {',
  15175. ' };',
  15176. '});',
  15177. 'this.ObjC = null;',
  15178. 'this.MobileC = null;',
  15179. 'this.CarC = null;',
  15180. '']),
  15181. LinesToStr([ // $mod.$main
  15182. '$mod.ObjC.DoIt();',
  15183. '$mod.MobileC.DoIt$1();',
  15184. '$mod.CarC.DoIt$2();',
  15185. '$mod.ObjC.DoIt();',
  15186. '$mod.ObjC.DoIt$1();',
  15187. '$mod.ObjC.DoIt$2();',
  15188. '$mod.MobileC.DoIt();',
  15189. '$mod.MobileC.DoIt$1();',
  15190. '$mod.MobileC.DoIt$2();',
  15191. '$mod.CarC.DoIt();',
  15192. '$mod.CarC.DoIt$1();',
  15193. '$mod.CarC.DoIt$2();',
  15194. '']));
  15195. end;
  15196. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  15197. begin
  15198. StartProgram(false);
  15199. Add('type');
  15200. Add(' TObject = class');
  15201. Add(' function CurNow: longint; ');
  15202. Add(' class function Now: longint; ');
  15203. Add(' end;');
  15204. Add('function TObject.CurNow: longint; begin end;');
  15205. Add('class function TObject.Now: longint; begin end;');
  15206. Add('var');
  15207. Add(' Obj: tobject;');
  15208. Add(' vI: longint;');
  15209. Add('begin');
  15210. Add(' obj.curnow;');
  15211. Add(' vi:=obj.curnow;');
  15212. Add(' tobject.now;');
  15213. Add(' vi:=tobject.now;');
  15214. ConvertProgram;
  15215. CheckSource('TestClassOf_ImplicitFunctionCall',
  15216. LinesToStr([ // statements
  15217. 'rtl.createClass($mod, "TObject", null, function () {',
  15218. ' this.$init = function () {',
  15219. ' };',
  15220. ' this.$final = function () {',
  15221. ' };',
  15222. ' this.CurNow = function () {',
  15223. ' var Result = 0;',
  15224. ' return Result;',
  15225. ' };',
  15226. ' this.Now = function () {',
  15227. ' var Result = 0;',
  15228. ' return Result;',
  15229. ' };',
  15230. '});',
  15231. 'this.Obj = null;',
  15232. 'this.vI = 0;',
  15233. '']),
  15234. LinesToStr([ // $mod.$main
  15235. '$mod.Obj.CurNow();',
  15236. '$mod.vI = $mod.Obj.CurNow();',
  15237. '$mod.TObject.Now();',
  15238. '$mod.vI = $mod.TObject.Now();',
  15239. '']));
  15240. end;
  15241. procedure TTestModule.TestClassOf_Const;
  15242. begin
  15243. StartProgram(false);
  15244. Add([
  15245. 'type',
  15246. ' TObject = class',
  15247. ' end;',
  15248. ' TBird = TObject;',
  15249. ' TBirds = class of TBird;',
  15250. ' TEagles = TBirds;',
  15251. ' THawk = class(TBird);',
  15252. 'const',
  15253. ' Hawk: TEagles = THawk;',
  15254. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  15255. ' TBird,',
  15256. ' THawk',
  15257. ' );',
  15258. 'begin']);
  15259. ConvertProgram;
  15260. CheckSource('TestClassOf_Const',
  15261. LinesToStr([ // statements
  15262. 'rtl.createClass($mod, "TObject", null, function () {',
  15263. ' this.$init = function () {',
  15264. ' };',
  15265. ' this.$final = function () {',
  15266. ' };',
  15267. '});',
  15268. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  15269. '});',
  15270. 'this.Hawk = $mod.THawk;',
  15271. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  15272. '']),
  15273. LinesToStr([ // $mod.$main
  15274. '']));
  15275. end;
  15276. procedure TTestModule.TestNestedClass_Alias;
  15277. begin
  15278. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15279. StartProgram(false);
  15280. Add([
  15281. 'type',
  15282. ' TObject = class',
  15283. ' type TNested = type longint;',
  15284. ' end;',
  15285. 'type TAlias = type tobject.tnested;',
  15286. 'var i: tobject.tnested = 3;',
  15287. 'var j: TAlias = 4;',
  15288. 'begin',
  15289. ' if typeinfo(TAlias)=nil then ;',
  15290. ' if typeinfo(tobject.tnested)=nil then ;',
  15291. '']);
  15292. ConvertProgram;
  15293. CheckSource('TestNestedClass_Alias',
  15294. LinesToStr([ // statements
  15295. 'rtl.createClass($mod, "TObject", null, function () {',
  15296. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  15297. ' this.$init = function () {',
  15298. ' };',
  15299. ' this.$final = function () {',
  15300. ' };',
  15301. '});',
  15302. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  15303. 'this.i = 3;',
  15304. 'this.j = 4;',
  15305. '']),
  15306. LinesToStr([ // $mod.$main
  15307. 'if ($mod.$rtti["TAlias"] === null) ;',
  15308. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  15309. '']));
  15310. end;
  15311. procedure TTestModule.TestNestedClass_Record;
  15312. begin
  15313. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15314. StartProgram(false);
  15315. Add([
  15316. 'type',
  15317. ' TObject = class',
  15318. ' type TPoint = record',
  15319. ' x,y: byte;',
  15320. ' end;',
  15321. ' procedure DoIt(t: TPoint);',
  15322. ' end;',
  15323. 'procedure tobject.DoIt(t: TPoint);',
  15324. 'var p: TPoint;',
  15325. 'begin',
  15326. ' t.x:=t.y;',
  15327. ' p:=t;',
  15328. 'end;',
  15329. 'var',
  15330. ' p: tobject.tpoint = (x:2; y:4);',
  15331. ' o: TObject;',
  15332. 'begin',
  15333. ' p:=p;',
  15334. ' o.doit(p);',
  15335. '']);
  15336. ConvertProgram;
  15337. CheckSource('TestNestedClass_Record',
  15338. LinesToStr([ // statements
  15339. 'rtl.createClass($mod, "TObject", null, function () {',
  15340. ' rtl.recNewT(this, "TPoint", function () {',
  15341. ' this.x = 0;',
  15342. ' this.y = 0;',
  15343. ' this.$eq = function (b) {',
  15344. ' return (this.x === b.x) && (this.y === b.y);',
  15345. ' };',
  15346. ' this.$assign = function (s) {',
  15347. ' this.x = s.x;',
  15348. ' this.y = s.y;',
  15349. ' return this;',
  15350. ' };',
  15351. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  15352. ' $r.addField("x", rtl.byte);',
  15353. ' $r.addField("y", rtl.byte);',
  15354. ' });',
  15355. ' this.$init = function () {',
  15356. ' };',
  15357. ' this.$final = function () {',
  15358. ' };',
  15359. ' this.DoIt = function (t) {',
  15360. ' var p = this.TPoint.$new();',
  15361. ' t.x = t.y;',
  15362. ' p.$assign(t);',
  15363. ' };',
  15364. '});',
  15365. 'this.p = $mod.TObject.TPoint.$clone({',
  15366. ' x: 2,',
  15367. ' y: 4',
  15368. '});',
  15369. 'this.o = null;',
  15370. '']),
  15371. LinesToStr([ // $mod.$main
  15372. '$mod.p.$assign($mod.p);',
  15373. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  15374. '']));
  15375. end;
  15376. procedure TTestModule.TestNestedClass_Class;
  15377. begin
  15378. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15379. StartProgram(false);
  15380. Add([
  15381. 'type',
  15382. ' TObject = class end;',
  15383. ' TBird = class',
  15384. ' type TLeg = class',
  15385. ' FId: longint;',
  15386. ' constructor Create;',
  15387. ' function Create(i: longint): TLeg;',
  15388. ' end;',
  15389. ' function DoIt(b: TBird): Tleg;',
  15390. ' end;',
  15391. 'constructor tbird.tleg.create;',
  15392. 'begin',
  15393. ' FId:=3;',
  15394. 'end;',
  15395. 'function tbird.tleg.Create(i: longint): TLeg;',
  15396. 'begin',
  15397. ' Create;',
  15398. ' Result:=TLeg.Create;',
  15399. ' Result:=TBird.TLeg.Create;',
  15400. ' Result:=Create(3);',
  15401. ' FId:=i;',
  15402. 'end;',
  15403. 'function tbird.DoIt(b: tbird): tleg;',
  15404. 'begin',
  15405. ' Result.Create;',
  15406. ' Result:=TLeg.Create;',
  15407. ' Result:=TBird.TLeg.Create;',
  15408. ' Result:=Result.Create(3);',
  15409. 'end;',
  15410. 'var',
  15411. ' b: Tbird.tleg;',
  15412. 'begin',
  15413. ' b.Create;',
  15414. ' b:=TBird.TLeg.Create;',
  15415. ' b:=b.Create(3);',
  15416. '']);
  15417. ConvertProgram;
  15418. CheckSource('TestNestedClass_Class',
  15419. LinesToStr([ // statements
  15420. 'rtl.createClass($mod, "TObject", null, function () {',
  15421. ' this.$init = function () {',
  15422. ' };',
  15423. ' this.$final = function () {',
  15424. ' };',
  15425. '});',
  15426. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15427. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  15428. ' this.$init = function () {',
  15429. ' $mod.TObject.$init.call(this);',
  15430. ' this.FId = 0;',
  15431. ' };',
  15432. ' this.Create = function () {',
  15433. ' this.FId = 3;',
  15434. ' return this;',
  15435. ' };',
  15436. ' this.Create$1 = function (i) {',
  15437. ' var Result = null;',
  15438. ' this.Create();',
  15439. ' Result = $mod.TBird.TLeg.$create("Create");',
  15440. ' Result = $mod.TBird.TLeg.$create("Create");',
  15441. ' Result = this.Create$1(3);',
  15442. ' this.FId = i;',
  15443. ' return Result;',
  15444. ' };',
  15445. ' });',
  15446. ' this.DoIt = function (b) {',
  15447. ' var Result = null;',
  15448. ' Result.Create();',
  15449. ' Result = this.TLeg.$create("Create");',
  15450. ' Result = $mod.TBird.TLeg.$create("Create");',
  15451. ' Result = Result.Create$1(3);',
  15452. ' return Result;',
  15453. ' };',
  15454. '});',
  15455. 'this.b = null;',
  15456. '']),
  15457. LinesToStr([ // $mod.$main
  15458. '$mod.b.Create();',
  15459. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  15460. '$mod.b = $mod.b.Create$1(3);',
  15461. '']));
  15462. end;
  15463. procedure TTestModule.TestExternalClass_Var;
  15464. begin
  15465. StartProgram(false);
  15466. Add([
  15467. '{$modeswitch externalclass}',
  15468. 'type',
  15469. ' TExtA = class external name ''ExtObj''',
  15470. ' Id: longint external name ''$Id'';',
  15471. ' B: longint;',
  15472. ' end;',
  15473. 'var Obj: TExtA;',
  15474. 'begin',
  15475. ' obj.id:=obj.id+1;',
  15476. ' obj.B:=obj.B+1;']);
  15477. ConvertProgram;
  15478. CheckSource('TestExternalClass_Var',
  15479. LinesToStr([ // statements
  15480. 'this.Obj = null;',
  15481. '']),
  15482. LinesToStr([ // $mod.$main
  15483. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  15484. '$mod.Obj.B = $mod.Obj.B + 1;',
  15485. '']));
  15486. end;
  15487. procedure TTestModule.TestExternalClass_Const;
  15488. begin
  15489. StartProgram(false);
  15490. Add([
  15491. '{$modeswitch externalclass}',
  15492. 'type',
  15493. ' TExtA = class external name ''ExtObj''',
  15494. ' const Two: longint = 2;',
  15495. ' const Three = 3;',
  15496. ' const Id: longint;',
  15497. ' end;',
  15498. ' TExtB = class external name ''ExtB''',
  15499. ' A: TExtA;',
  15500. ' end;',
  15501. 'var',
  15502. ' A: texta;',
  15503. ' B: textb;',
  15504. ' i: longint;',
  15505. 'begin',
  15506. ' i:=a.two;',
  15507. ' i:=texta.two;',
  15508. ' i:=a.three;',
  15509. ' i:=texta.three;',
  15510. ' i:=a.id;',
  15511. ' i:=texta.id;',
  15512. '']);
  15513. ConvertProgram;
  15514. CheckSource('TestExternalClass_Const',
  15515. LinesToStr([ // statements
  15516. 'this.A = null;',
  15517. 'this.B = null;',
  15518. 'this.i = 0;',
  15519. '']),
  15520. LinesToStr([ // $mod.$main
  15521. '$mod.i = 2;',
  15522. '$mod.i = 2;',
  15523. '$mod.i = 3;',
  15524. '$mod.i = 3;',
  15525. '$mod.i = $mod.A.Id;',
  15526. '$mod.i = ExtObj.Id;',
  15527. '']));
  15528. end;
  15529. procedure TTestModule.TestExternalClass_Dollar;
  15530. begin
  15531. StartProgram(false);
  15532. Add([
  15533. '{$modeswitch externalclass}',
  15534. 'type',
  15535. ' TExtA = class external name ''$''',
  15536. ' Id: longint external name ''$'';',
  15537. ' function Bla(i: longint): longint; external name ''$'';',
  15538. ' end;',
  15539. 'function dollar(k: longint): longint; external name ''$'';',
  15540. 'var Obj: TExtA;',
  15541. 'begin',
  15542. ' dollar(1);',
  15543. ' obj.id:=obj.id+2;',
  15544. ' obj.Bla(3);',
  15545. '']);
  15546. ConvertProgram;
  15547. CheckSource('TestExternalClass_Dollar',
  15548. LinesToStr([ // statements
  15549. 'this.Obj = null;',
  15550. '']),
  15551. LinesToStr([ // $mod.$main
  15552. '$(1);',
  15553. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  15554. '$mod.Obj.$(3);',
  15555. '']));
  15556. end;
  15557. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  15558. begin
  15559. StartProgram(false);
  15560. Add('{$modeswitch externalclass}');
  15561. Add('type');
  15562. Add(' TExtA = class external name ''ExtA''');
  15563. Add(' Id: longint external name ''$Id'';');
  15564. Add(' end;');
  15565. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  15566. Add(' Id: longint;');
  15567. Add(' end;');
  15568. Add('begin');
  15569. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  15570. ConvertProgram;
  15571. end;
  15572. procedure TTestModule.TestExternalClass_Method;
  15573. begin
  15574. StartProgram(false);
  15575. Add('{$modeswitch externalclass}');
  15576. Add('type');
  15577. Add(' TExtA = class external name ''ExtObj''');
  15578. Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
  15579. Add(' procedure DoSome(Id: longint = 1);');
  15580. Add(' end;');
  15581. Add('var Obj: texta;');
  15582. Add('begin');
  15583. Add(' obj.doit;');
  15584. Add(' obj.doit();');
  15585. Add(' obj.doit(2);');
  15586. Add(' with obj do begin');
  15587. Add(' doit;');
  15588. Add(' doit();');
  15589. Add(' doit(3);');
  15590. Add(' end;');
  15591. ConvertProgram;
  15592. CheckSource('TestExternalClass_Method',
  15593. LinesToStr([ // statements
  15594. 'this.Obj = null;',
  15595. '']),
  15596. LinesToStr([ // $mod.$main
  15597. '$mod.Obj.$Execute(1);',
  15598. '$mod.Obj.$Execute(1);',
  15599. '$mod.Obj.$Execute(2);',
  15600. 'var $with1 = $mod.Obj;',
  15601. '$with1.$Execute(1);',
  15602. '$with1.$Execute(1);',
  15603. '$with1.$Execute(3);',
  15604. '']));
  15605. end;
  15606. procedure TTestModule.TestExternalClass_ClassMethod;
  15607. begin
  15608. StartProgram(false);
  15609. Add([
  15610. '{$modeswitch externalclass}',
  15611. 'type',
  15612. ' TExtA = class external name ''ExtObj''',
  15613. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  15614. ' end;',
  15615. ' TExtB = TExtA;',
  15616. 'var p: Pointer;',
  15617. 'begin',
  15618. ' texta.doit;',
  15619. ' texta.doit();',
  15620. ' texta.doit(2);',
  15621. ' p:[email protected];',
  15622. ' with texta do begin',
  15623. ' doit;',
  15624. ' doit();',
  15625. ' doit(3);',
  15626. ' p:=@DoIt;',
  15627. ' end;',
  15628. ' textb.doit;',
  15629. ' textb.doit();',
  15630. ' textb.doit(4);',
  15631. ' with textb do begin',
  15632. ' doit;',
  15633. ' doit();',
  15634. ' doit(5);',
  15635. ' end;',
  15636. '']);
  15637. ConvertProgram;
  15638. CheckSource('TestExternalClass_ClassMethod',
  15639. LinesToStr([ // statements
  15640. 'this.p = null;',
  15641. '']),
  15642. LinesToStr([ // $mod.$main
  15643. 'ExtObj.$Execute(1);',
  15644. 'ExtObj.$Execute(1);',
  15645. 'ExtObj.$Execute(2);',
  15646. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  15647. 'ExtObj.$Execute(1);',
  15648. 'ExtObj.$Execute(1);',
  15649. 'ExtObj.$Execute(3);',
  15650. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  15651. 'ExtObj.$Execute(1);',
  15652. 'ExtObj.$Execute(1);',
  15653. 'ExtObj.$Execute(4);',
  15654. 'ExtObj.$Execute(1);',
  15655. 'ExtObj.$Execute(1);',
  15656. 'ExtObj.$Execute(5);',
  15657. '']));
  15658. end;
  15659. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  15660. begin
  15661. StartProgram(false);
  15662. Add([
  15663. '{$modeswitch externalclass}',
  15664. 'type',
  15665. ' TExtA = class external name ''ExtObj''',
  15666. ' class procedure DoIt(Id: longint = 1); static;',
  15667. ' end;',
  15668. 'var p: Pointer;',
  15669. 'begin',
  15670. ' texta.doit;',
  15671. ' texta.doit();',
  15672. ' texta.doit(2);',
  15673. ' p:[email protected];',
  15674. ' with texta do begin',
  15675. ' doit;',
  15676. ' doit();',
  15677. ' doit(3);',
  15678. ' p:=@DoIt;',
  15679. ' end;',
  15680. '']);
  15681. ConvertProgram;
  15682. CheckSource('TestExternalClass_ClassMethodStatic',
  15683. LinesToStr([ // statements
  15684. 'this.p = null;',
  15685. '']),
  15686. LinesToStr([ // $mod.$main
  15687. 'ExtObj.DoIt(1);',
  15688. 'ExtObj.DoIt(1);',
  15689. 'ExtObj.DoIt(2);',
  15690. '$mod.p = ExtObj.DoIt;',
  15691. 'ExtObj.DoIt(1);',
  15692. 'ExtObj.DoIt(1);',
  15693. 'ExtObj.DoIt(3);',
  15694. '$mod.p = ExtObj.DoIt;',
  15695. '']));
  15696. end;
  15697. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  15698. begin
  15699. StartProgram(false);
  15700. Add([
  15701. '{$modeswitch externalclass}',
  15702. 'type',
  15703. ' TBird = class external name ''Array''',
  15704. ' end;',
  15705. 'function GetPtr: Pointer;',
  15706. 'begin',
  15707. 'end;',
  15708. 'procedure Write(const p);',
  15709. 'begin',
  15710. 'end;',
  15711. 'procedure WriteLn; varargs;',
  15712. 'begin',
  15713. 'end;',
  15714. 'begin',
  15715. ' if TBird(GetPtr)=nil then ;',
  15716. ' Write(GetPtr);',
  15717. ' WriteLn(GetPtr);',
  15718. ' Write(TBird(GetPtr));',
  15719. ' WriteLn(TBird(GetPtr));',
  15720. '']);
  15721. ConvertProgram;
  15722. CheckSource('TestFunctionResultInTypeCast',
  15723. LinesToStr([ // statements
  15724. 'this.GetPtr = function () {',
  15725. ' var Result = null;',
  15726. ' return Result;',
  15727. '};',
  15728. 'this.Write = function (p) {',
  15729. '};',
  15730. 'this.WriteLn = function () {',
  15731. '};',
  15732. '']),
  15733. LinesToStr([
  15734. 'if ($mod.GetPtr() === null) ;',
  15735. '$mod.Write($mod.GetPtr());',
  15736. '$mod.WriteLn($mod.GetPtr());',
  15737. '$mod.Write($mod.GetPtr());',
  15738. '$mod.WriteLn($mod.GetPtr());',
  15739. '']));
  15740. end;
  15741. procedure TTestModule.TestExternalClass_NonExternalOverride;
  15742. begin
  15743. StartProgram(false);
  15744. Add([
  15745. '{$modeswitch externalclass}',
  15746. 'type',
  15747. ' TExtA = class external name ''ExtObjA''',
  15748. ' procedure ProcA; virtual;',
  15749. ' procedure ProcB; virtual;',
  15750. ' end;',
  15751. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  15752. ' end;',
  15753. ' TExtC = class (TExtB)',
  15754. ' procedure ProcA; override;',
  15755. ' end;',
  15756. 'procedure TExtC.ProcA;',
  15757. 'begin',
  15758. ' ProcA;',
  15759. ' Self.ProcA;',
  15760. ' ProcB;',
  15761. ' Self.ProcB;',
  15762. 'end;',
  15763. 'var',
  15764. ' A: texta;',
  15765. ' B: textb;',
  15766. ' C: textc;',
  15767. 'begin',
  15768. ' a.proca;',
  15769. ' b.proca;',
  15770. ' c.proca;']);
  15771. ConvertProgram;
  15772. CheckSource('TestExternalClass_NonExternalOverride',
  15773. LinesToStr([ // statements
  15774. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  15775. ' this.$init = function () {',
  15776. ' };',
  15777. ' this.$final = function () {',
  15778. ' };',
  15779. ' this.ProcA = function () {',
  15780. ' this.ProcA();',
  15781. ' this.ProcA();',
  15782. ' this.ProcB();',
  15783. ' this.ProcB();',
  15784. ' };',
  15785. '});',
  15786. 'this.A = null;',
  15787. 'this.B = null;',
  15788. 'this.C = null;',
  15789. '']),
  15790. LinesToStr([ // $mod.$main
  15791. '$mod.A.ProcA();',
  15792. '$mod.B.ProcA();',
  15793. '$mod.C.ProcA();',
  15794. '']));
  15795. end;
  15796. procedure TTestModule.TestExternalClass_OverloadHint;
  15797. begin
  15798. StartProgram(false);
  15799. Add([
  15800. '{$modeswitch externalclass}',
  15801. 'type',
  15802. ' TExtA = class external name ''ExtObjA''',
  15803. ' procedure DoIt;',
  15804. ' procedure DoIt(i: longint);',
  15805. ' end;',
  15806. 'begin',
  15807. '']);
  15808. ConvertProgram;
  15809. CheckResolverUnexpectedHints(true);
  15810. CheckSource('TestExternalClass_OverloadHint',
  15811. LinesToStr([ // statements
  15812. '']),
  15813. LinesToStr([ // $mod.$main
  15814. '']));
  15815. end;
  15816. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  15817. begin
  15818. StartProgram(false);
  15819. Add([
  15820. '{$modeswitch externalclass}',
  15821. 'type',
  15822. ' JSwiper = class external name ''Swiper''',
  15823. ' constructor New;',
  15824. ' end;',
  15825. ' TObject = class',
  15826. ' private',
  15827. ' FSwiper: JSwiper;',
  15828. ' published',
  15829. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  15830. ' end;',
  15831. 'begin',
  15832. ' JSwiper.new;',
  15833. '']);
  15834. ConvertProgram;
  15835. CheckSource('TestExternalClass_SameNamePublishedProperty',
  15836. LinesToStr([ // statements
  15837. 'rtl.createClass($mod, "TObject", null, function () {',
  15838. ' this.$init = function () {',
  15839. ' this.FSwiper = null;',
  15840. ' };',
  15841. ' this.$final = function () {',
  15842. ' this.FSwiper = undefined;',
  15843. ' };',
  15844. ' var $r = this.$rtti;',
  15845. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  15846. '});',
  15847. '']),
  15848. LinesToStr([ // $mod.$main
  15849. 'new Swiper();',
  15850. '']));
  15851. end;
  15852. procedure TTestModule.TestExternalClass_Property;
  15853. begin
  15854. StartProgram(false);
  15855. Add([
  15856. '{$modeswitch externalclass}',
  15857. 'type',
  15858. ' TExtA = class external name ''ExtA''',
  15859. ' function getYear: longint;',
  15860. ' procedure setYear(Value: longint);',
  15861. ' property Year: longint read getyear write setyear;',
  15862. ' end;',
  15863. ' TExtB = class (TExtA)',
  15864. ' procedure OtherSetYear(Value: longint);',
  15865. ' property year write othersetyear;',
  15866. ' end;',
  15867. 'procedure textb.othersetyear(value: longint);',
  15868. 'begin',
  15869. ' setYear(Value+4);',
  15870. 'end;',
  15871. 'var',
  15872. ' A: texta;',
  15873. ' B: textb;',
  15874. 'begin',
  15875. ' a.year:=a.year+1;',
  15876. ' b.year:=b.year+2;']);
  15877. ConvertProgram;
  15878. CheckSource('TestExternalClass_NonExternalOverride',
  15879. LinesToStr([ // statements
  15880. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15881. ' this.$init = function () {',
  15882. ' };',
  15883. ' this.$final = function () {',
  15884. ' };',
  15885. ' this.OtherSetYear = function (Value) {',
  15886. ' this.setYear(Value+4);',
  15887. ' };',
  15888. '});',
  15889. 'this.A = null;',
  15890. 'this.B = null;',
  15891. '']),
  15892. LinesToStr([ // $mod.$main
  15893. '$mod.A.setYear($mod.A.getYear()+1);',
  15894. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  15895. '']));
  15896. end;
  15897. procedure TTestModule.TestExternalClass_PropertyDate;
  15898. begin
  15899. StartProgram(false);
  15900. Add([
  15901. '{$modeswitch externalclass}',
  15902. 'type',
  15903. ' TExtA = class external name ''ExtA''',
  15904. ' end;',
  15905. ' TExtB = class (TExtA)',
  15906. ' FDate: string;',
  15907. ' property Date: string read FDate write FDate;',
  15908. ' property ExtA: string read FDate write FDate;',
  15909. ' end;',
  15910. ' {$M+}',
  15911. ' TObject = class',
  15912. ' FDate: string;',
  15913. ' published',
  15914. ' property Date: string read FDate write FDate;',
  15915. ' property ExtA: string read FDate write FDate;',
  15916. ' end;',
  15917. 'var',
  15918. ' B: textb;',
  15919. ' o: TObject;',
  15920. 'begin',
  15921. ' b.date:=b.exta;',
  15922. ' o.date:=o.exta;']);
  15923. ConvertProgram;
  15924. CheckSource('TestExternalClass_PropertyDate',
  15925. LinesToStr([ // statements
  15926. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15927. ' this.$init = function () {',
  15928. ' this.FDate = "";',
  15929. ' };',
  15930. ' this.$final = function () {',
  15931. ' };',
  15932. '});',
  15933. 'rtl.createClass($mod, "TObject", null, function () {',
  15934. ' this.$init = function () {',
  15935. ' this.FDate = "";',
  15936. ' };',
  15937. ' this.$final = function () {',
  15938. ' };',
  15939. ' var $r = this.$rtti;',
  15940. ' $r.addField("FDate", rtl.string);',
  15941. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  15942. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  15943. '});',
  15944. 'this.B = null;',
  15945. 'this.o = null;',
  15946. '']),
  15947. LinesToStr([ // $mod.$main
  15948. '$mod.B.FDate = $mod.B.FDate;',
  15949. '$mod.o.FDate = $mod.o.FDate;',
  15950. '']));
  15951. end;
  15952. procedure TTestModule.TestExternalClass_ClassProperty;
  15953. begin
  15954. StartProgram(false);
  15955. Add('{$modeswitch externalclass}');
  15956. Add('type');
  15957. Add(' TExtA = class external name ''ExtA''');
  15958. Add(' class function getYear: longint;');
  15959. Add(' class procedure setYear(Value: longint);');
  15960. Add(' class property Year: longint read getyear write setyear;');
  15961. Add(' end;');
  15962. Add(' TExtB = class (TExtA)');
  15963. Add(' class function GetCentury: longint;');
  15964. Add(' class procedure SetCentury(Value: longint);');
  15965. Add(' class property Century: longint read getcentury write setcentury;');
  15966. Add(' end;');
  15967. Add('class function textb.getcentury: longint;');
  15968. Add('begin');
  15969. Add('end;');
  15970. Add('class procedure textb.setcentury(value: longint);');
  15971. Add('begin');
  15972. Add(' setyear(value+11);');
  15973. Add(' texta.year:=texta.year+12;');
  15974. Add(' year:=year+13;');
  15975. Add(' textb.century:=textb.century+14;');
  15976. Add(' century:=century+15;');
  15977. Add('end;');
  15978. Add('var');
  15979. Add(' A: texta;');
  15980. Add(' B: textb;');
  15981. Add('begin');
  15982. Add(' texta.year:=texta.year+1;');
  15983. Add(' textb.year:=textb.year+2;');
  15984. Add(' TextA.year:=TextA.year+3;');
  15985. Add(' b.year:=b.year+4;');
  15986. Add(' textb.century:=textb.century+5;');
  15987. Add(' b.century:=b.century+6;');
  15988. ConvertProgram;
  15989. CheckSource('TestExternalClass_ClassProperty',
  15990. LinesToStr([ // statements
  15991. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15992. ' this.$init = function () {',
  15993. ' };',
  15994. ' this.$final = function () {',
  15995. ' };',
  15996. ' this.GetCentury = function () {',
  15997. ' var Result = 0;',
  15998. ' return Result;',
  15999. ' };',
  16000. ' this.SetCentury = function (Value) {',
  16001. ' this.setYear(Value + 11);',
  16002. ' ExtA.setYear(ExtA.getYear() + 12);',
  16003. ' this.setYear(this.getYear() + 13);',
  16004. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  16005. ' this.SetCentury(this.GetCentury() + 15);',
  16006. ' };',
  16007. '});',
  16008. 'this.A = null;',
  16009. 'this.B = null;',
  16010. '']),
  16011. LinesToStr([ // $mod.$main
  16012. 'ExtA.setYear(ExtA.getYear() + 1);',
  16013. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  16014. 'ExtA.setYear(ExtA.getYear() + 3);',
  16015. '$mod.B.setYear($mod.B.getYear() + 4);',
  16016. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  16017. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  16018. '']));
  16019. end;
  16020. procedure TTestModule.TestExternalClass_ClassOf;
  16021. begin
  16022. StartProgram(false);
  16023. Add('{$modeswitch externalclass}');
  16024. Add('type');
  16025. Add(' TExtA = class external name ''ExtA''');
  16026. Add(' procedure ProcA; virtual;');
  16027. Add(' procedure ProcB; virtual;');
  16028. Add(' end;');
  16029. Add(' TExtAClass = class of TExtA;');
  16030. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16031. Add(' end;');
  16032. Add(' TExtBClass = class of TExtB;');
  16033. Add(' TExtC = class (TExtB)');
  16034. Add(' procedure ProcA; override;');
  16035. Add(' end;');
  16036. Add(' TExtCClass = class of TExtC;');
  16037. Add('procedure TExtC.ProcA; begin end;');
  16038. Add('var');
  16039. Add(' A: texta; ClA: TExtAClass;');
  16040. Add(' B: textb; ClB: TExtBClass;');
  16041. Add(' C: textc; ClC: TExtCClass;');
  16042. Add('begin');
  16043. Add(' ClA:=texta;');
  16044. Add(' ClA:=textb;');
  16045. Add(' ClA:=textc;');
  16046. Add(' ClB:=textb;');
  16047. Add(' ClB:=textc;');
  16048. Add(' ClC:=textc;');
  16049. ConvertProgram;
  16050. CheckSource('TestExternalClass_ClassOf',
  16051. LinesToStr([ // statements
  16052. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16053. ' this.$init = function () {',
  16054. ' };',
  16055. ' this.$final = function () {',
  16056. ' };',
  16057. ' this.ProcA = function () {',
  16058. ' };',
  16059. '});',
  16060. 'this.A = null;',
  16061. 'this.ClA = null;',
  16062. 'this.B = null;',
  16063. 'this.ClB = null;',
  16064. 'this.C = null;',
  16065. 'this.ClC = null;',
  16066. '']),
  16067. LinesToStr([ // $mod.$main
  16068. '$mod.ClA = ExtA;',
  16069. '$mod.ClA = ExtB;',
  16070. '$mod.ClA = $mod.TExtC;',
  16071. '$mod.ClB = ExtB;',
  16072. '$mod.ClB = $mod.TExtC;',
  16073. '$mod.ClC = $mod.TExtC;',
  16074. '']));
  16075. end;
  16076. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  16077. begin
  16078. AddModuleWithIntfImplSrc('unit2.pas',
  16079. LinesToStr([
  16080. '{$modeswitch externalclass}',
  16081. 'type',
  16082. ' TExtA = class external name ''ExtA''',
  16083. ' class var Id: longint;',
  16084. ' end;',
  16085. '']),
  16086. '');
  16087. StartUnit(true);
  16088. Add('interface');
  16089. Add('uses unit2;');
  16090. Add('implementation');
  16091. Add('begin');
  16092. Add(' unit2.texta.id:=unit2.texta.id+1;');
  16093. ConvertUnit;
  16094. CheckSource('TestExternalClass_ClassOtherUnit',
  16095. LinesToStr([
  16096. '']),
  16097. LinesToStr([
  16098. 'ExtA.Id = ExtA.Id + 1;',
  16099. '']));
  16100. end;
  16101. procedure TTestModule.TestExternalClass_Is;
  16102. begin
  16103. StartProgram(false);
  16104. Add('{$modeswitch externalclass}');
  16105. Add('type');
  16106. Add(' TExtA = class external name ''ExtA''');
  16107. Add(' end;');
  16108. Add(' TExtAClass = class of TExtA;');
  16109. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16110. Add(' end;');
  16111. Add(' TExtBClass = class of TExtB;');
  16112. Add(' TExtC = class (TExtB)');
  16113. Add(' end;');
  16114. Add(' TExtCClass = class of TExtC;');
  16115. Add('var');
  16116. Add(' A: texta; ClA: TExtAClass;');
  16117. Add(' B: textb; ClB: TExtBClass;');
  16118. Add(' C: textc; ClC: TExtCClass;');
  16119. Add('begin');
  16120. Add(' if a is textb then ;');
  16121. Add(' if a is textc then ;');
  16122. Add(' if b is textc then ;');
  16123. Add(' if cla is textb then ;');
  16124. Add(' if cla is textc then ;');
  16125. Add(' if clb is textc then ;');
  16126. ConvertProgram;
  16127. CheckSource('TestExternalClass_Is',
  16128. LinesToStr([ // statements
  16129. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16130. ' this.$init = function () {',
  16131. ' };',
  16132. ' this.$final = function () {',
  16133. ' };',
  16134. '});',
  16135. 'this.A = null;',
  16136. 'this.ClA = null;',
  16137. 'this.B = null;',
  16138. 'this.ClB = null;',
  16139. 'this.C = null;',
  16140. 'this.ClC = null;',
  16141. '']),
  16142. LinesToStr([ // $mod.$main
  16143. 'if (rtl.isExt($mod.A, ExtB)) ;',
  16144. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  16145. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  16146. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  16147. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  16148. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  16149. '']));
  16150. end;
  16151. procedure TTestModule.TestExternalClass_As;
  16152. begin
  16153. StartProgram(false);
  16154. Add('{$modeswitch externalclass}');
  16155. Add('type');
  16156. Add(' TExtA = class external name ''ExtA''');
  16157. Add(' end;');
  16158. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16159. Add(' end;');
  16160. Add(' TExtC = class (TExtB)');
  16161. Add(' end;');
  16162. Add('var');
  16163. Add(' A: texta;');
  16164. Add(' B: textb;');
  16165. Add(' C: textc;');
  16166. Add('begin');
  16167. Add(' b:=a as textb;');
  16168. Add(' c:=a as textc;');
  16169. Add(' c:=b as textc;');
  16170. ConvertProgram;
  16171. CheckSource('TestExternalClass_Is',
  16172. LinesToStr([ // statements
  16173. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16174. ' this.$init = function () {',
  16175. ' };',
  16176. ' this.$final = function () {',
  16177. ' };',
  16178. '});',
  16179. 'this.A = null;',
  16180. 'this.B = null;',
  16181. 'this.C = null;',
  16182. '']),
  16183. LinesToStr([ // $mod.$main
  16184. '$mod.B = rtl.asExt($mod.A, ExtB);',
  16185. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  16186. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  16187. '']));
  16188. end;
  16189. procedure TTestModule.TestExternalClass_DestructorFail;
  16190. begin
  16191. StartProgram(false);
  16192. Add('{$modeswitch externalclass}');
  16193. Add('type');
  16194. Add(' TExtA = class external name ''ExtA''');
  16195. Add(' destructor Free;');
  16196. Add(' end;');
  16197. SetExpectedPasResolverError('Pascal element not supported: destructor',
  16198. nPasElementNotSupported);
  16199. ConvertProgram;
  16200. end;
  16201. procedure TTestModule.TestExternalClass_New;
  16202. begin
  16203. StartProgram(false);
  16204. Add([
  16205. '{$modeswitch externalclass}',
  16206. 'type',
  16207. ' TExtA = class external name ''ExtA''',
  16208. ' constructor New;',
  16209. ' constructor New(i: longint; j: longint = 2);',
  16210. ' end;',
  16211. 'var',
  16212. ' A: texta;',
  16213. 'begin',
  16214. ' a:=texta.new;',
  16215. ' a:=texta(texta.new);',
  16216. ' a:=texta.new();',
  16217. ' a:=texta.new(1);',
  16218. ' with texta do begin',
  16219. ' a:=new;',
  16220. ' a:=new();',
  16221. ' a:=new(2);',
  16222. ' end;',
  16223. ' a:=test1.texta.new;',
  16224. ' a:=test1.texta.new();',
  16225. ' a:=test1.texta.new(3);',
  16226. '']);
  16227. ConvertProgram;
  16228. CheckSource('TestExternalClass_New',
  16229. LinesToStr([ // statements
  16230. 'this.A = null;',
  16231. '']),
  16232. LinesToStr([ // $mod.$main
  16233. '$mod.A = new ExtA();',
  16234. '$mod.A = new ExtA();',
  16235. '$mod.A = new ExtA();',
  16236. '$mod.A = new ExtA(1,2);',
  16237. '$mod.A = new ExtA();',
  16238. '$mod.A = new ExtA();',
  16239. '$mod.A = new ExtA(2,2);',
  16240. '$mod.A = new ExtA();',
  16241. '$mod.A = new ExtA();',
  16242. '$mod.A = new ExtA(3,2);',
  16243. '']));
  16244. end;
  16245. procedure TTestModule.TestExternalClass_ClassOf_New;
  16246. begin
  16247. StartProgram(false);
  16248. Add('{$modeswitch externalclass}');
  16249. Add('type');
  16250. Add(' TExtAClass = class of TExtA;');
  16251. Add(' TExtA = class external name ''ExtA''');
  16252. Add(' C: TExtAClass;');
  16253. Add(' constructor New;');
  16254. Add(' end;');
  16255. Add('var');
  16256. Add(' A: texta;');
  16257. Add(' C: textaclass;');
  16258. Add('begin');
  16259. Add(' a:=c.new;');
  16260. Add(' a:=c.new();');
  16261. Add(' with C do begin');
  16262. Add(' a:=new;');
  16263. Add(' a:=new();');
  16264. Add(' end;');
  16265. Add(' a:=test1.c.new;');
  16266. Add(' a:=test1.c.new();');
  16267. Add(' a:=A.c.new();');
  16268. ConvertProgram;
  16269. CheckSource('TestExternalClass_ClassOf_New',
  16270. LinesToStr([ // statements
  16271. 'this.A = null;',
  16272. 'this.C = null;',
  16273. '']),
  16274. LinesToStr([ // $mod.$main
  16275. '$mod.A = new $mod.C();',
  16276. '$mod.A = new $mod.C();',
  16277. 'var $with1 = $mod.C;',
  16278. '$mod.A = new $with1();',
  16279. '$mod.A = new $with1();',
  16280. '$mod.A = new $mod.C();',
  16281. '$mod.A = new $mod.C();',
  16282. '$mod.A = new $mod.A.C();',
  16283. '']));
  16284. end;
  16285. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  16286. begin
  16287. StartProgram(false);
  16288. Add([
  16289. '{$modeswitch externalclass}',
  16290. 'type',
  16291. ' TExtAClass = class of TExtA;',
  16292. ' TExtA = class external name ''ExtA''',
  16293. ' constructor New;',
  16294. ' end;',
  16295. 'function GetCreator: TExtAClass;',
  16296. 'begin',
  16297. ' Result:=TExtA;',
  16298. 'end;',
  16299. 'var',
  16300. ' A: texta;',
  16301. 'begin',
  16302. ' a:=getcreator.new;',
  16303. ' a:=getcreator().new;',
  16304. ' a:=getcreator().new();',
  16305. ' a:=getcreator.new();',
  16306. ' with getcreator do begin',
  16307. ' a:=new;',
  16308. ' a:=new();',
  16309. ' end;']);
  16310. ConvertProgram;
  16311. CheckSource('TestExternalClass_FuncClassOf_New',
  16312. LinesToStr([ // statements
  16313. 'this.GetCreator = function () {',
  16314. ' var Result = null;',
  16315. ' Result = ExtA;',
  16316. ' return Result;',
  16317. '};',
  16318. 'this.A = null;',
  16319. '']),
  16320. LinesToStr([ // $mod.$main
  16321. '$mod.A = new ($mod.GetCreator())();',
  16322. '$mod.A = new ($mod.GetCreator())();',
  16323. '$mod.A = new ($mod.GetCreator())();',
  16324. '$mod.A = new ($mod.GetCreator())();',
  16325. 'var $with1 = $mod.GetCreator();',
  16326. '$mod.A = new $with1();',
  16327. '$mod.A = new $with1();',
  16328. '']));
  16329. end;
  16330. procedure TTestModule.TestExternalClass_New_PasClassFail;
  16331. begin
  16332. StartProgram(false);
  16333. Add([
  16334. '{$modeswitch externalclass}',
  16335. 'type',
  16336. ' TExtA = class external name ''ExtA''',
  16337. ' constructor New;',
  16338. ' end;',
  16339. ' TBird = class(TExtA)',
  16340. ' end;',
  16341. 'begin',
  16342. ' TBird.new;',
  16343. '']);
  16344. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16345. ConvertProgram;
  16346. end;
  16347. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  16348. begin
  16349. StartProgram(false);
  16350. Add([
  16351. '{$modeswitch externalclass}',
  16352. 'type',
  16353. ' TExtA = class external name ''ExtA''',
  16354. ' constructor New;',
  16355. ' end;',
  16356. ' TBird = class(TExtA)',
  16357. ' end;',
  16358. 'begin',
  16359. ' TBird.new();',
  16360. '']);
  16361. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16362. ConvertProgram;
  16363. end;
  16364. procedure TTestModule.TestExternalClass_NewExtName;
  16365. begin
  16366. StartProgram(false);
  16367. Add([
  16368. '{$modeswitch externalclass}',
  16369. 'type',
  16370. ' TExtA = class external name ''ExtA''',
  16371. ' constructor New; external name ''Other'';',
  16372. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  16373. ' end;',
  16374. 'var',
  16375. ' A: texta;',
  16376. 'begin',
  16377. ' a:=texta.new;',
  16378. ' a:=texta(texta.new);',
  16379. ' a:=texta.new();',
  16380. ' a:=texta.new(1);',
  16381. ' with texta do begin',
  16382. ' a:=new;',
  16383. ' a:=new();',
  16384. ' a:=new(2);',
  16385. ' end;',
  16386. ' a:=test1.texta.new;',
  16387. ' a:=test1.texta.new();',
  16388. ' a:=test1.texta.new(3);',
  16389. '']);
  16390. ConvertProgram;
  16391. CheckSource('TestExternalClass_NewExtName',
  16392. LinesToStr([ // statements
  16393. 'this.A = null;',
  16394. '']),
  16395. LinesToStr([ // $mod.$main
  16396. '$mod.A = new Other();',
  16397. '$mod.A = new Other();',
  16398. '$mod.A = new Other();',
  16399. '$mod.A = new A.B(1,2);',
  16400. '$mod.A = new Other();',
  16401. '$mod.A = new Other();',
  16402. '$mod.A = new A.B(2,2);',
  16403. '$mod.A = new Other();',
  16404. '$mod.A = new Other();',
  16405. '$mod.A = new A.B(3,2);',
  16406. '']));
  16407. end;
  16408. procedure TTestModule.TestExternalClass_Constructor;
  16409. begin
  16410. StartProgram(false);
  16411. Add([
  16412. '{$modeswitch externalclass}',
  16413. 'type',
  16414. ' TExtA = class external name ''ExtA''',
  16415. ' constructor Create;',
  16416. ' constructor Create(i: longint; j: longint = 2);',
  16417. ' end;',
  16418. 'var',
  16419. ' A: texta;',
  16420. 'begin',
  16421. ' a:=texta.create;',
  16422. ' a:=texta(texta.create);',
  16423. ' a:=texta.create();',
  16424. ' a:=texta.create(1);',
  16425. ' with texta do begin',
  16426. ' a:=create;',
  16427. ' a:=create();',
  16428. ' a:=create(2);',
  16429. ' end;',
  16430. ' a:=test1.texta.create;',
  16431. ' a:=test1.texta.create();',
  16432. ' a:=test1.texta.create(3);',
  16433. '']);
  16434. ConvertProgram;
  16435. CheckSource('TestExternalClass_Constructor',
  16436. LinesToStr([ // statements
  16437. 'this.A = null;',
  16438. '']),
  16439. LinesToStr([ // $mod.$main
  16440. '$mod.A = new ExtA.Create();',
  16441. '$mod.A = new ExtA.Create();',
  16442. '$mod.A = new ExtA.Create();',
  16443. '$mod.A = new ExtA.Create(1,2);',
  16444. '$mod.A = new ExtA.Create();',
  16445. '$mod.A = new ExtA.Create();',
  16446. '$mod.A = new ExtA.Create(2,2);',
  16447. '$mod.A = new ExtA.Create();',
  16448. '$mod.A = new ExtA.Create();',
  16449. '$mod.A = new ExtA.Create(3,2);',
  16450. '']));
  16451. end;
  16452. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  16453. begin
  16454. StartProgram(false);
  16455. Add([
  16456. '{$modeswitch externalclass}',
  16457. 'type',
  16458. ' TExtA = class external name ''ExtA''',
  16459. ' constructor Create; external name ''{}'';',
  16460. ' end;',
  16461. 'var',
  16462. ' A: texta;',
  16463. 'begin',
  16464. ' a:=texta.create;',
  16465. ' a:=texta(texta.create);',
  16466. ' a:=texta.create();',
  16467. ' with texta do begin',
  16468. ' a:=create;',
  16469. ' a:=create();',
  16470. ' end;',
  16471. ' a:=test1.texta.create;',
  16472. ' a:=test1.texta.create();',
  16473. '']);
  16474. ConvertProgram;
  16475. CheckSource('TestExternalClass_ConstructorBrackets',
  16476. LinesToStr([ // statements
  16477. 'this.A = null;',
  16478. '']),
  16479. LinesToStr([ // $mod.$main
  16480. '$mod.A = {};',
  16481. '$mod.A = {};',
  16482. '$mod.A = {};',
  16483. '$mod.A = {};',
  16484. '$mod.A = {};',
  16485. '$mod.A = {};',
  16486. '$mod.A = {};',
  16487. '']));
  16488. end;
  16489. procedure TTestModule.TestExternalClass_LocalConstSameName;
  16490. begin
  16491. StartProgram(false);
  16492. Add('{$modeswitch externalclass}');
  16493. Add('type');
  16494. Add(' TExtA = class external name ''ExtA''');
  16495. Add(' constructor New;');
  16496. Add(' end;');
  16497. Add('function DoIt: longint;');
  16498. Add('const ExtA: longint = 3;');
  16499. Add('begin');
  16500. Add(' Result:=ExtA;');
  16501. Add('end;');
  16502. Add('var');
  16503. Add(' A: texta;');
  16504. Add('begin');
  16505. Add(' a:=texta.new;');
  16506. ConvertProgram;
  16507. CheckSource('TestExternalClass_LocalConstSameName',
  16508. LinesToStr([ // statements
  16509. 'var ExtA$1 = 3;',
  16510. 'this.DoIt = function () {',
  16511. ' var Result = 0;',
  16512. ' Result = ExtA$1;',
  16513. ' return Result;',
  16514. '};',
  16515. 'this.A = null;',
  16516. '']),
  16517. LinesToStr([ // $mod.$main
  16518. '$mod.A = new ExtA();',
  16519. '']));
  16520. end;
  16521. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  16522. begin
  16523. StartProgram(false);
  16524. Add('{$modeswitch externalclass}');
  16525. Add('type');
  16526. Add(' TExtA = class external name ''ExtA''');
  16527. Add(' procedure DoIt;');
  16528. Add(' end;');
  16529. Add(' TMyA = class(TExtA)');
  16530. Add(' procedure DoIt;');
  16531. Add(' end;');
  16532. Add('procedure TMyA.DoIt; begin end;');
  16533. Add('begin');
  16534. ConvertProgram;
  16535. CheckSource('TestExternalClass_ReintroduceOverload',
  16536. LinesToStr([ // statements
  16537. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  16538. ' this.$init = function () {',
  16539. ' };',
  16540. ' this.$final = function () {',
  16541. ' };',
  16542. ' this.DoIt$1 = function () {',
  16543. ' };',
  16544. '});',
  16545. '']),
  16546. LinesToStr([ // $mod.$main
  16547. '']));
  16548. end;
  16549. procedure TTestModule.TestExternalClass_Inherited;
  16550. begin
  16551. StartProgram(false);
  16552. Add('{$modeswitch externalclass}');
  16553. Add('type');
  16554. Add(' TExtA = class external name ''ExtA''');
  16555. Add(' procedure DoIt(i: longint = 1); virtual;');
  16556. Add(' procedure DoSome(j: longint = 2);');
  16557. Add(' end;');
  16558. Add(' TExtB = class external name ''ExtB''(TExtA)');
  16559. Add(' end;');
  16560. Add(' TMyC = class(TExtB)');
  16561. Add(' procedure DoIt(i: longint = 1); override;');
  16562. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  16563. Add(' end;');
  16564. Add('procedure TMyC.DoIt(i: longint);');
  16565. Add('begin');
  16566. Add(' inherited;');
  16567. Add(' inherited DoIt;');
  16568. Add(' inherited DoIt();');
  16569. Add(' inherited DoIt(3);');
  16570. Add(' inherited DoSome;');
  16571. Add(' inherited DoSome();');
  16572. Add(' inherited DoSome(4);');
  16573. Add('end;');
  16574. Add('procedure TMyC.DoSome(j: longint);');
  16575. Add('begin');
  16576. Add(' inherited;');
  16577. Add('end;');
  16578. Add('begin');
  16579. ConvertProgram;
  16580. CheckSource('TestExternalClass_ReintroduceOverload',
  16581. LinesToStr([ // statements
  16582. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  16583. ' this.$init = function () {',
  16584. ' };',
  16585. ' this.$final = function () {',
  16586. ' };',
  16587. ' this.DoIt = function (i) {',
  16588. ' ExtB.DoIt.apply(this, arguments);',
  16589. ' ExtB.DoIt.call(this, 1);',
  16590. ' ExtB.DoIt.call(this, 1);',
  16591. ' ExtB.DoIt.call(this, 3);',
  16592. ' ExtB.DoSome.call(this, 2);',
  16593. ' ExtB.DoSome.call(this, 2);',
  16594. ' ExtB.DoSome.call(this, 4);',
  16595. ' };',
  16596. ' this.DoSome$1 = function (j) {',
  16597. ' ExtB.DoSome.apply(this, arguments);',
  16598. ' };',
  16599. '});',
  16600. '']),
  16601. LinesToStr([ // $mod.$main
  16602. '']));
  16603. end;
  16604. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  16605. begin
  16606. StartProgram(false);
  16607. Add('{$modeswitch externalclass}');
  16608. Add('type');
  16609. Add(' TObject = class');
  16610. Add(' end;');
  16611. Add(' TExtA = class external name ''ExtA''(TObject)');
  16612. Add(' end;');
  16613. Add('begin');
  16614. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  16615. ConvertProgram;
  16616. end;
  16617. procedure TTestModule.TestExternalClass_NewInstance;
  16618. begin
  16619. StartProgram(false);
  16620. Add('{$modeswitch externalclass}');
  16621. Add('type');
  16622. Add(' TExtA = class external name ''ExtA''');
  16623. Add(' end;');
  16624. Add(' TMyB = class(TExtA)');
  16625. Add(' protected');
  16626. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  16627. Add(' end;');
  16628. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  16629. Add('begin end;');
  16630. Add('begin');
  16631. ConvertProgram;
  16632. CheckSource('TestExternalClass_NewInstance',
  16633. LinesToStr([ // statements
  16634. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  16635. ' this.$init = function () {',
  16636. ' };',
  16637. ' this.$final = function () {',
  16638. ' };',
  16639. ' this.NewInstance = function (fnname, paramarray) {',
  16640. ' var Result = null;',
  16641. ' return Result;',
  16642. ' };',
  16643. '});',
  16644. '']),
  16645. LinesToStr([ // $mod.$main
  16646. '']));
  16647. end;
  16648. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  16649. begin
  16650. StartProgram(false);
  16651. Add('{$modeswitch externalclass}');
  16652. Add('type');
  16653. Add(' TExtA = class external name ''ExtA''');
  16654. Add(' end;');
  16655. Add(' TMyB = class(TExtA)');
  16656. Add(' protected');
  16657. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  16658. Add(' end;');
  16659. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  16660. Add('begin end;');
  16661. Add('begin');
  16662. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  16663. ConvertProgram;
  16664. end;
  16665. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  16666. begin
  16667. StartProgram(false);
  16668. Add('{$modeswitch externalclass}');
  16669. Add('type');
  16670. Add(' TExtA = class external name ''ExtA''');
  16671. Add(' end;');
  16672. Add(' TMyB = class(TExtA)');
  16673. Add(' protected');
  16674. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  16675. Add(' end;');
  16676. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  16677. Add('begin end;');
  16678. Add('begin');
  16679. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  16680. nIncompatibleTypeArgNo);
  16681. ConvertProgram;
  16682. end;
  16683. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  16684. begin
  16685. StartProgram(false);
  16686. Add('{$modeswitch externalclass}');
  16687. Add('type');
  16688. Add(' TExtA = class external name ''ExtA''');
  16689. Add(' end;');
  16690. Add(' TMyB = class(TExtA)');
  16691. Add(' protected');
  16692. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  16693. Add(' end;');
  16694. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  16695. Add('begin end;');
  16696. Add('begin');
  16697. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  16698. nIncompatibleTypeArgNo);
  16699. ConvertProgram;
  16700. end;
  16701. procedure TTestModule.TestExternalClass_PascalProperty;
  16702. begin
  16703. StartProgram(false);
  16704. Add('{$modeswitch externalclass}');
  16705. Add('type');
  16706. Add(' TJSElement = class;');
  16707. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  16708. Add(' TJSElement = class external name ''ExtA''');
  16709. Add(' end;');
  16710. Add(' TControl = class(TJSElement)');
  16711. Add(' private');
  16712. Add(' FOnClick: TJSNotifyEvent;');
  16713. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  16714. Add(' procedure Click(Sender: TJSElement);');
  16715. Add(' end;');
  16716. Add('procedure TControl.Click(Sender: TJSElement);');
  16717. Add('begin');
  16718. Add(' OnClick(Self);');
  16719. Add('end;');
  16720. Add('var');
  16721. Add(' Ctrl: TControl;');
  16722. Add('begin');
  16723. Add(' Ctrl.OnClick:[email protected];');
  16724. Add(' Ctrl.OnClick(Ctrl);');
  16725. ConvertProgram;
  16726. CheckSource('TestExternalClass_PascalProperty',
  16727. LinesToStr([ // statements
  16728. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  16729. ' this.$init = function () {',
  16730. ' this.FOnClick = null;',
  16731. ' };',
  16732. ' this.$final = function () {',
  16733. ' this.FOnClick = undefined;',
  16734. ' };',
  16735. ' this.Click = function (Sender) {',
  16736. ' this.FOnClick(this);',
  16737. ' };',
  16738. '});',
  16739. 'this.Ctrl = null;',
  16740. '']),
  16741. LinesToStr([ // $mod.$main
  16742. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  16743. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  16744. '']));
  16745. end;
  16746. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  16747. begin
  16748. StartProgram(false);
  16749. Add([
  16750. '{$modeswitch externalclass}',
  16751. 'type',
  16752. ' IUnknown = interface end;',
  16753. ' TObject = class',
  16754. ' end;',
  16755. ' TChild = class',
  16756. ' end;',
  16757. ' TExtRootA = class external name ''ExtRootA''',
  16758. ' end;',
  16759. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  16760. ' end;',
  16761. ' TExtRootB = class external name ''ExtRootB''',
  16762. ' end;',
  16763. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  16764. ' end;',
  16765. 'var',
  16766. ' Obj: TObject;',
  16767. ' Child: TChild;',
  16768. ' RootA: TExtRootA;',
  16769. ' ChildA: TExtChildA;',
  16770. ' RootB: TExtRootB;',
  16771. ' ChildB: TExtChildB;',
  16772. ' i: IUnknown;',
  16773. 'begin',
  16774. ' obj:=tobject(roota);',
  16775. ' obj:=tobject(childa);',
  16776. ' child:=tchild(tobject(roota));',
  16777. ' roota:=textroota(obj);',
  16778. ' roota:=textroota(child);',
  16779. ' roota:=textroota(rootb);',
  16780. ' roota:=textroota(childb);',
  16781. ' childa:=textchilda(textroota(obj));',
  16782. ' roota:=TExtRootA(i)',
  16783. '']);
  16784. ConvertProgram;
  16785. CheckSource('TestExternalClass_TypeCastToRootClass',
  16786. LinesToStr([ // statements
  16787. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16788. 'rtl.createClass($mod, "TObject", null, function () {',
  16789. ' this.$init = function () {',
  16790. ' };',
  16791. ' this.$final = function () {',
  16792. ' };',
  16793. '});',
  16794. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  16795. '});',
  16796. 'this.Obj = null;',
  16797. 'this.Child = null;',
  16798. 'this.RootA = null;',
  16799. 'this.ChildA = null;',
  16800. 'this.RootB = null;',
  16801. 'this.ChildB = null;',
  16802. 'this.i = null;',
  16803. '']),
  16804. LinesToStr([ // $mod.$main
  16805. '$mod.Obj = $mod.RootA;',
  16806. '$mod.Obj = $mod.ChildA;',
  16807. '$mod.Child = $mod.RootA;',
  16808. '$mod.RootA = $mod.Obj;',
  16809. '$mod.RootA = $mod.Child;',
  16810. '$mod.RootA = $mod.RootB;',
  16811. '$mod.RootA = $mod.ChildB;',
  16812. '$mod.ChildA = $mod.Obj;',
  16813. '$mod.RootA = $mod.i;',
  16814. '']));
  16815. end;
  16816. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  16817. begin
  16818. StartProgram(false);
  16819. Add([
  16820. '{$modeswitch externalclass}',
  16821. 'type',
  16822. ' IUnknown = interface end;',
  16823. ' IBird = interface(IUnknown) end;',
  16824. ' TClass = class of TObject;',
  16825. ' TObject = class',
  16826. ' end;',
  16827. ' TChild = class',
  16828. ' end;',
  16829. ' TJSObject = class external name ''Object''',
  16830. ' end;',
  16831. ' TRec = record end;',
  16832. 'var',
  16833. ' Obj: TObject;',
  16834. ' Child: TChild;',
  16835. ' i: IUnknown;',
  16836. ' Bird: IBird;',
  16837. ' j: TJSObject;',
  16838. ' r: TRec;',
  16839. ' c: TClass;',
  16840. 'begin',
  16841. ' j:=tjsobject(IUnknown);',
  16842. ' j:=tjsobject(IBird);',
  16843. ' j:=tjsobject(TObject);',
  16844. ' j:=tjsobject(TChild);',
  16845. ' j:=tjsobject(TRec);',
  16846. ' j:=tjsobject(Obj);',
  16847. ' j:=tjsobject(Child);',
  16848. ' j:=tjsobject(i);',
  16849. ' j:=tjsobject(Bird);',
  16850. ' j:=tjsobject(r);',
  16851. ' j:=tjsobject(c);',
  16852. '']);
  16853. ConvertProgram;
  16854. CheckSource('TestExternalClass_TypeCastToJSObject',
  16855. LinesToStr([ // statements
  16856. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16857. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  16858. 'rtl.createClass($mod, "TObject", null, function () {',
  16859. ' this.$init = function () {',
  16860. ' };',
  16861. ' this.$final = function () {',
  16862. ' };',
  16863. '});',
  16864. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  16865. '});',
  16866. 'rtl.recNewT($mod, "TRec", function () {',
  16867. ' this.$eq = function (b) {',
  16868. ' return true;',
  16869. ' };',
  16870. ' this.$assign = function (s) {',
  16871. ' return this;',
  16872. ' };',
  16873. '});',
  16874. 'this.Obj = null;',
  16875. 'this.Child = null;',
  16876. 'this.i = null;',
  16877. 'this.Bird = null;',
  16878. 'this.j = null;',
  16879. 'this.r = $mod.TRec.$new();',
  16880. 'this.c = null;',
  16881. '']),
  16882. LinesToStr([ // $mod.$main
  16883. '$mod.j = $mod.IUnknown;',
  16884. '$mod.j = $mod.IBird;',
  16885. '$mod.j = $mod.TObject;',
  16886. '$mod.j = $mod.TChild;',
  16887. '$mod.j = $mod.TRec;',
  16888. '$mod.j = $mod.Obj;',
  16889. '$mod.j = $mod.Child;',
  16890. '$mod.j = $mod.i;',
  16891. '$mod.j = $mod.Bird;',
  16892. '$mod.j = $mod.r;',
  16893. '$mod.j = $mod.c;',
  16894. '']));
  16895. end;
  16896. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  16897. begin
  16898. StartProgram(false);
  16899. Add('{$modeswitch externalclass}');
  16900. Add('type');
  16901. Add(' TJSString = class external name ''String''');
  16902. Add(' class function fromCharCode() : string; varargs;');
  16903. Add(' function anchor(const aName : string) : string;');
  16904. Add(' end;');
  16905. Add('var');
  16906. Add(' s: string;');
  16907. Add('begin');
  16908. Add(' s:=TJSString.fromCharCode(65,66);');
  16909. Add(' s:=TJSString(s).anchor(s);');
  16910. Add(' s:=TJSString(''foo'').anchor(s);');
  16911. ConvertProgram;
  16912. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  16913. LinesToStr([ // statements
  16914. 'this.s = "";',
  16915. '']),
  16916. LinesToStr([ // $mod.$main
  16917. '$mod.s = String.fromCharCode(65, 66);',
  16918. '$mod.s = $mod.s.anchor($mod.s);',
  16919. '$mod.s = "foo".anchor($mod.s);',
  16920. '']));
  16921. end;
  16922. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  16923. begin
  16924. StartProgram(false);
  16925. Add([
  16926. '{$modeswitch externalclass}',
  16927. 'type',
  16928. ' TJSObject = class external name ''Object'' end;',
  16929. ' TJSFunction = class external name ''Function''',
  16930. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  16931. ' function call(thisArg: TJSObject): JSValue; varargs;',
  16932. ' end;',
  16933. ' TObject = class',
  16934. ' procedure DoIt(i: longint);',
  16935. ' end;',
  16936. ' TFuncInt = function(o: TObject): longint;',
  16937. 'function GetIt(o: TObject): longint;',
  16938. ' procedure Sub; begin end;',
  16939. 'var',
  16940. ' f: TJSFunction;',
  16941. ' fi: TFuncInt;',
  16942. 'begin',
  16943. ' fi:=TFuncInt(f);',
  16944. ' f:=TJSFunction(fi);',
  16945. ' f:=TJSFunction(@GetIt);',
  16946. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  16947. ' f:=TJSFunction(@Sub);',
  16948. ' f:=TJSFunction(@o.doit);',
  16949. ' f:=TJSFunction(fi).bind(nil,4)',
  16950. 'end;',
  16951. 'procedure TObject.DoIt(i: longint);',
  16952. ' procedure Sub; begin end;',
  16953. 'var f: TJSFunction;',
  16954. 'begin',
  16955. ' f:=TJSFunction(@DoIt);',
  16956. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  16957. ' f:=TJSFunction(@Sub);',
  16958. ' f:=TJSFunction(@GetIt);',
  16959. 'end;',
  16960. 'begin']);
  16961. ConvertProgram;
  16962. CheckSource('TestExternalClass_TypeCastToJSFunction',
  16963. LinesToStr([ // statements
  16964. 'rtl.createClass($mod, "TObject", null, function () {',
  16965. ' this.$init = function () {',
  16966. ' };',
  16967. ' this.$final = function () {',
  16968. ' };',
  16969. ' this.DoIt = function (i) {',
  16970. ' var $Self = this;',
  16971. ' function Sub() {',
  16972. ' };',
  16973. ' var f = null;',
  16974. ' f = $Self.DoIt;',
  16975. ' f = $Self.DoIt.bind(null, 13);',
  16976. ' f = Sub;',
  16977. ' f = $mod.GetIt;',
  16978. ' };',
  16979. '});',
  16980. 'this.GetIt = function (o) {',
  16981. ' var Result = 0;',
  16982. ' function Sub() {',
  16983. ' };',
  16984. ' var f = null;',
  16985. ' var fi = null;',
  16986. ' fi = f;',
  16987. ' f = fi;',
  16988. ' f = $mod.GetIt;',
  16989. ' f = $mod.GetIt.bind(null, 3);',
  16990. ' f = Sub;',
  16991. ' f = $mod.TObject.DoIt;',
  16992. ' f = fi.bind(null, 4);',
  16993. ' return Result;',
  16994. '};',
  16995. '']),
  16996. LinesToStr([ // $mod.$main
  16997. '']));
  16998. end;
  16999. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  17000. begin
  17001. StartProgram(false);
  17002. Add([
  17003. '{$mode delphi}',
  17004. '{$modeswitch externalclass}',
  17005. 'type',
  17006. ' TJSObject = class external name ''Object'' end;',
  17007. ' TJSWindow = class external name ''Window''(TJSObject)',
  17008. ' procedure Open;',
  17009. ' end;',
  17010. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  17011. ' procedure Execute;',
  17012. ' end;',
  17013. 'procedure Fly;',
  17014. 'var',
  17015. ' w: TJSWindow;',
  17016. ' e: TJSEventTarget;',
  17017. 'begin',
  17018. ' w:=TJSWindow(e);',
  17019. ' e:=TJSEventTarget(w);',
  17020. 'end;',
  17021. 'begin']);
  17022. ConvertProgram;
  17023. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  17024. LinesToStr([ // statements
  17025. 'this.Fly = function () {',
  17026. ' var w = null;',
  17027. ' var e = null;',
  17028. ' w = e;',
  17029. ' e = w;',
  17030. '};',
  17031. '']),
  17032. LinesToStr([ // $mod.$main
  17033. '']));
  17034. end;
  17035. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  17036. begin
  17037. StartProgram(false);
  17038. Add('{$modeswitch externalclass}');
  17039. Add('type');
  17040. Add(' TJSString = class external name ''String''');
  17041. Add(' class function fromCharCode() : string; varargs;');
  17042. Add(' end;');
  17043. Add('var');
  17044. Add(' s: string;');
  17045. Add(' sObj: TJSString;');
  17046. Add('begin');
  17047. Add(' s:=sObj.fromCharCode(65,66);');
  17048. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  17049. nExternalClassInstanceCannotAccessStaticX);
  17050. ConvertProgram;
  17051. end;
  17052. procedure TTestModule.TestExternalClass_BracketAccessor;
  17053. begin
  17054. StartProgram(false);
  17055. Add([
  17056. '{$modeswitch externalclass}',
  17057. 'type',
  17058. ' TJSArray = class external name ''Array2''',
  17059. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17060. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17061. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17062. ' end;',
  17063. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  17064. 'begin end;',
  17065. 'var',
  17066. ' Arr: tjsarray;',
  17067. ' s: string;',
  17068. ' i: longint;',
  17069. ' v: jsvalue;',
  17070. 'begin',
  17071. ' v:=arr[0];',
  17072. ' v:=arr.items[1];',
  17073. ' arr[2]:=s;',
  17074. ' arr.items[3]:=s;',
  17075. ' arr[4]:=i;',
  17076. ' arr[5]:=arr[6];',
  17077. ' arr.items[7]:=arr.items[8];',
  17078. ' with arr do items[9]:=items[10];',
  17079. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  17080. ' with arr do begin',
  17081. ' v:=GetItems(14);',
  17082. ' setitems(15,16);',
  17083. ' end;',
  17084. ' v:=test1.arr.items[17];',
  17085. ' test1.arr.items[18]:=v;',
  17086. '']);
  17087. ConvertProgram;
  17088. CheckSource('TestExternalClass_BracketAccessor',
  17089. LinesToStr([ // statements
  17090. 'this.DoIt = function (vI, vJ, vK, vL) {',
  17091. '};',
  17092. 'this.Arr = null;',
  17093. 'this.s = "";',
  17094. 'this.i = 0;',
  17095. 'this.v = undefined;',
  17096. '']),
  17097. LinesToStr([ // $mod.$main
  17098. '$mod.v = $mod.Arr[0];',
  17099. '$mod.v = $mod.Arr[1];',
  17100. '$mod.Arr[2] = $mod.s;',
  17101. '$mod.Arr[3] = $mod.s;',
  17102. '$mod.Arr[4] = $mod.i;',
  17103. '$mod.Arr[5] = $mod.Arr[6];',
  17104. '$mod.Arr[7] = $mod.Arr[8];',
  17105. 'var $with1 = $mod.Arr;',
  17106. '$with1[9] = $with1[10];',
  17107. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  17108. ' a: 9,',
  17109. ' p: $mod.Arr,',
  17110. ' get: function () {',
  17111. ' return this.p[this.a];',
  17112. ' },',
  17113. ' set: function (v) {',
  17114. ' this.p[this.a] = v;',
  17115. ' }',
  17116. '}, {',
  17117. ' a: 10,',
  17118. ' p: $mod.Arr,',
  17119. ' get: function () {',
  17120. ' return this.p[this.a];',
  17121. ' },',
  17122. ' set: function (v) {',
  17123. ' this.p[this.a] = v;',
  17124. ' }',
  17125. '});',
  17126. 'var $with2 = $mod.Arr;',
  17127. '$mod.v = $with2[14];',
  17128. '$with2[15] = 16;',
  17129. '$mod.v = $mod.Arr[17];',
  17130. '$mod.Arr[18] = $mod.v;',
  17131. '']));
  17132. end;
  17133. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  17134. begin
  17135. StartProgram(false);
  17136. Add([
  17137. '{$modeswitch externalclass}',
  17138. 'type',
  17139. ' TJSArray = class external name ''Array2''',
  17140. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17141. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17142. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17143. ' end;',
  17144. ' TMyArr = class(TJSArray)',
  17145. ' procedure DoIt;',
  17146. ' end;',
  17147. 'procedure tmyarr.DoIt;',
  17148. 'begin',
  17149. ' Items[1]:=Items[2];',
  17150. ' SetItems(3,getItems(4));',
  17151. 'end;',
  17152. 'var',
  17153. ' Arr: tmyarr;',
  17154. ' s: string;',
  17155. ' i: longint;',
  17156. ' v: jsvalue;',
  17157. 'begin',
  17158. ' v:=arr[0];',
  17159. ' v:=arr.items[1];',
  17160. ' arr[2]:=s;',
  17161. ' arr.items[3]:=s;',
  17162. ' arr[4]:=i;',
  17163. ' arr[5]:=arr[6];',
  17164. ' arr.items[7]:=arr.items[8];',
  17165. ' with arr do items[9]:=items[10];',
  17166. ' with arr do begin',
  17167. ' v:=GetItems(14);',
  17168. ' setitems(15,16);',
  17169. ' end;',
  17170. '']);
  17171. ConvertProgram;
  17172. CheckSource('TestExternalClass_BracketAccessor_Call',
  17173. LinesToStr([ // statements
  17174. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  17175. ' this.$init = function () {',
  17176. ' };',
  17177. ' this.$final = function () {',
  17178. ' };',
  17179. ' this.DoIt = function () {',
  17180. ' this[1] = this[2];',
  17181. ' this[3] = this[4];',
  17182. ' };',
  17183. '});',
  17184. 'this.Arr = null;',
  17185. 'this.s = "";',
  17186. 'this.i = 0;',
  17187. 'this.v = undefined;',
  17188. '']),
  17189. LinesToStr([ // $mod.$main
  17190. '$mod.v = $mod.Arr[0];',
  17191. '$mod.v = $mod.Arr[1];',
  17192. '$mod.Arr[2] = $mod.s;',
  17193. '$mod.Arr[3] = $mod.s;',
  17194. '$mod.Arr[4] = $mod.i;',
  17195. '$mod.Arr[5] = $mod.Arr[6];',
  17196. '$mod.Arr[7] = $mod.Arr[8];',
  17197. 'var $with1 = $mod.Arr;',
  17198. '$with1[9] = $with1[10];',
  17199. 'var $with2 = $mod.Arr;',
  17200. '$mod.v = $with2[14];',
  17201. '$with2[15] = 16;',
  17202. '']));
  17203. end;
  17204. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  17205. begin
  17206. StartProgram(false);
  17207. Add('{$modeswitch externalclass}');
  17208. Add('type');
  17209. Add(' TJSArray = class external name ''Array2''');
  17210. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  17211. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  17212. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  17213. Add(' end;');
  17214. Add('begin');
  17215. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  17216. nBracketAccessorOfExternalClassMustHaveOneParameter);
  17217. ConvertProgram;
  17218. end;
  17219. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  17220. begin
  17221. StartProgram(false);
  17222. Add('{$modeswitch externalclass}');
  17223. Add('type');
  17224. Add(' TJSArray = class external name ''Array2''');
  17225. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17226. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  17227. Add(' end;');
  17228. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  17229. Add('begin end;');
  17230. Add('var');
  17231. Add(' Arr: tjsarray;');
  17232. Add(' v: jsvalue;');
  17233. Add('begin');
  17234. Add(' v:=arr[0];');
  17235. Add(' v:=arr.items[1];');
  17236. Add(' with arr do v:=items[2];');
  17237. Add(' doit(arr[3],arr[4]);');
  17238. ConvertProgram;
  17239. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  17240. LinesToStr([ // statements
  17241. 'this.DoIt = function (vI, vJ) {',
  17242. '};',
  17243. 'this.Arr = null;',
  17244. 'this.v = undefined;',
  17245. '']),
  17246. LinesToStr([ // $mod.$main
  17247. '$mod.v = $mod.Arr[0];',
  17248. '$mod.v = $mod.Arr[1];',
  17249. 'var $with1 = $mod.Arr;',
  17250. '$mod.v = $with1[2];',
  17251. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  17252. '']));
  17253. end;
  17254. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  17255. begin
  17256. StartProgram(false);
  17257. Add('{$modeswitch externalclass}');
  17258. Add('type');
  17259. Add(' TJSArray = class external name ''Array2''');
  17260. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17261. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17262. Add(' end;');
  17263. Add('var');
  17264. Add(' Arr: tjsarray;');
  17265. Add(' s: string;');
  17266. Add(' i: longint;');
  17267. Add(' v: jsvalue;');
  17268. Add('begin');
  17269. Add(' arr[2]:=s;');
  17270. Add(' arr.items[3]:=s;');
  17271. Add(' arr[4]:=i;');
  17272. Add(' with arr do items[5]:=i;');
  17273. ConvertProgram;
  17274. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  17275. LinesToStr([ // statements
  17276. 'this.Arr = null;',
  17277. 'this.s = "";',
  17278. 'this.i = 0;',
  17279. 'this.v = undefined;',
  17280. '']),
  17281. LinesToStr([ // $mod.$main
  17282. '$mod.Arr[2] = $mod.s;',
  17283. '$mod.Arr[3] = $mod.s;',
  17284. '$mod.Arr[4] = $mod.i;',
  17285. 'var $with1 = $mod.Arr;',
  17286. '$with1[5] = $mod.i;',
  17287. '']));
  17288. end;
  17289. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  17290. begin
  17291. StartProgram(false);
  17292. Add('{$modeswitch externalclass}');
  17293. Add('type');
  17294. Add(' TJSArray = class external name ''Array2''');
  17295. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17296. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17297. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  17298. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  17299. Add(' end;');
  17300. Add('var');
  17301. Add(' Arr: tjsarray;');
  17302. Add(' s: string;');
  17303. Add(' i: longint;');
  17304. Add(' v: jsvalue;');
  17305. Add('begin');
  17306. Add(' arr[2]:=s;');
  17307. Add(' arr.items[3]:=s;');
  17308. Add(' arr.numbers[4]:=i;');
  17309. Add(' with arr do items[5]:=i;');
  17310. Add(' with arr do numbers[6]:=i;');
  17311. ConvertProgram;
  17312. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  17313. LinesToStr([ // statements
  17314. 'this.Arr = null;',
  17315. 'this.s = "";',
  17316. 'this.i = 0;',
  17317. 'this.v = undefined;',
  17318. '']),
  17319. LinesToStr([ // $mod.$main
  17320. '$mod.Arr[2] = $mod.s;',
  17321. '$mod.Arr[3] = $mod.s;',
  17322. '$mod.Arr[4] = $mod.i;',
  17323. 'var $with1 = $mod.Arr;',
  17324. '$with1[5] = $mod.i;',
  17325. 'var $with2 = $mod.Arr;',
  17326. '$with2[6] = $mod.i;',
  17327. '']));
  17328. end;
  17329. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  17330. begin
  17331. StartProgram(false);
  17332. Add('{$modeswitch externalclass}');
  17333. Add('type');
  17334. Add(' TJSArray = class external name ''Array2''');
  17335. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17336. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17337. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  17338. Add(' end;');
  17339. Add('var');
  17340. Add(' Arr: tjsarray;');
  17341. Add(' i: longint;');
  17342. Add(' IntArr: array of longint;');
  17343. Add(' v: jsvalue;');
  17344. Add('begin');
  17345. Add(' v:=arr.items[i];');
  17346. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  17347. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  17348. ConvertProgram;
  17349. CheckSource('TestExternalClass_BracketAccessor_Index',
  17350. LinesToStr([ // statements
  17351. 'this.Arr = null;',
  17352. 'this.i = 0;',
  17353. 'this.IntArr = [];',
  17354. 'this.v = undefined;',
  17355. '']),
  17356. LinesToStr([ // $mod.$main
  17357. '$mod.v = $mod.Arr[$mod.i];',
  17358. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  17359. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  17360. '']));
  17361. end;
  17362. procedure TTestModule.TestExternalClass_ForInJSObject;
  17363. begin
  17364. StartProgram(false);
  17365. Add([
  17366. '{$modeswitch externalclass}',
  17367. 'type',
  17368. ' TJSObject = class external name ''Object''',
  17369. ' end;',
  17370. 'var',
  17371. ' o: TJSObject;',
  17372. ' key: string;',
  17373. 'begin',
  17374. ' for key in o do',
  17375. ' if key=''abc'' then ;',
  17376. '']);
  17377. ConvertProgram;
  17378. CheckSource('TestExternalClass_ForInJSObject',
  17379. LinesToStr([ // statements
  17380. 'this.o = null;',
  17381. 'this.key = "";',
  17382. '']),
  17383. LinesToStr([ // $mod.$main
  17384. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  17385. '']));
  17386. end;
  17387. procedure TTestModule.TestExternalClass_ForInJSArray;
  17388. begin
  17389. StartProgram(false);
  17390. Add([
  17391. '{$modeswitch externalclass}',
  17392. 'type',
  17393. ' TJSInt8Array = class external name ''Int8Array''',
  17394. ' private',
  17395. ' flength: NativeInt external name ''length'';',
  17396. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  17397. ' public',
  17398. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  17399. ' property Length: NativeInt read flength;',
  17400. ' end;',
  17401. 'var',
  17402. ' a: TJSInt8Array;',
  17403. ' value: shortint;',
  17404. 'begin',
  17405. ' for value in a do',
  17406. ' if value=3 then ;',
  17407. '']);
  17408. ConvertProgram;
  17409. CheckSource('TestExternalClass_ForInJSArray',
  17410. LinesToStr([ // statements
  17411. 'this.a = null;',
  17412. 'this.value = 0;',
  17413. '']),
  17414. LinesToStr([ // $mod.$main
  17415. 'for (var $in1 = $mod.a, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) {',
  17416. ' $mod.value = $in1[$l2];',
  17417. ' if ($mod.value === 3) ;',
  17418. '};',
  17419. '']));
  17420. end;
  17421. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  17422. begin
  17423. AddModuleWithIntfImplSrc('unit2.pas',
  17424. LinesToStr([
  17425. '{$modeswitch externalclass}',
  17426. 'type',
  17427. ' TJSBufferSource = class external name ''BufferSource''',
  17428. ' end;',
  17429. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  17430. '']),
  17431. '');
  17432. AddModuleWithIntfImplSrc('unit3.pas',
  17433. LinesToStr([
  17434. '{$modeswitch externalclass}',
  17435. 'type',
  17436. ' TJSBufferSource = class external name ''BufferSource''',
  17437. ' end;',
  17438. '']),
  17439. '');
  17440. StartUnit(true);
  17441. Add([
  17442. 'interface',
  17443. 'uses unit2, unit3;',
  17444. 'procedure DoSome(s: TJSBufferSource);',
  17445. 'implementation',
  17446. 'procedure DoSome(s: TJSBufferSource);',
  17447. 'begin',
  17448. ' DoIt(s);',
  17449. 'end;',
  17450. '']);
  17451. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  17452. nIncompatibleTypeArgNo);
  17453. ConvertUnit;
  17454. end;
  17455. procedure TTestModule.TestClassInterface_Corba;
  17456. begin
  17457. StartProgram(false);
  17458. Add([
  17459. '{$interfaces corba}',
  17460. 'type',
  17461. ' IUnknown = interface;',
  17462. ' IUnknown = interface',
  17463. ' [''{00000000-0000-0000-C000-000000000046}'']',
  17464. ' end;',
  17465. ' IInterface = IUnknown;',
  17466. ' IBird = interface(IInterface)',
  17467. ' function GetSize: longint;',
  17468. ' procedure SetSize(i: longint);',
  17469. ' property Size: longint read GetSize write SetSize;',
  17470. ' procedure DoIt(i: longint);',
  17471. ' end;',
  17472. ' TObject = class',
  17473. ' end;',
  17474. ' TBird = class(TObject,IBird)',
  17475. ' function GetSize: longint; virtual; abstract;',
  17476. ' procedure SetSize(i: longint); virtual; abstract;',
  17477. ' procedure DoIt(i: longint); virtual; abstract;',
  17478. ' end;',
  17479. 'var',
  17480. ' BirdIntf: IBird;',
  17481. 'begin',
  17482. ' BirdIntf.Size:=BirdIntf.Size;',
  17483. '']);
  17484. ConvertProgram;
  17485. CheckSource('TestClassInterface_Corba',
  17486. LinesToStr([ // statements
  17487. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  17488. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  17489. 'rtl.createClass($mod, "TObject", null, function () {',
  17490. ' this.$init = function () {',
  17491. ' };',
  17492. ' this.$final = function () {',
  17493. ' };',
  17494. '});',
  17495. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17496. ' rtl.addIntf(this, $mod.IBird);',
  17497. '});',
  17498. 'this.BirdIntf = null;',
  17499. '']),
  17500. LinesToStr([ // $mod.$main
  17501. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  17502. '']));
  17503. end;
  17504. procedure TTestModule.TestClassInterface_ProcExternalFail;
  17505. begin
  17506. StartProgram(false);
  17507. Add([
  17508. '{$interfaces corba}',
  17509. 'type',
  17510. ' IUnknown = interface',
  17511. ' procedure DoIt; external name ''foo'';',
  17512. ' end;',
  17513. 'begin']);
  17514. SetExpectedParserError(
  17515. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  17516. nParserNoFieldsAllowed);
  17517. ConvertProgram;
  17518. end;
  17519. procedure TTestModule.TestClassInterface_Overloads;
  17520. begin
  17521. StartProgram(false);
  17522. Add([
  17523. '{$interfaces corba}',
  17524. 'type',
  17525. ' integer = longint;',
  17526. ' IUnknown = interface',
  17527. ' procedure DoIt(i: integer);',
  17528. ' procedure DoIt(s: string);',
  17529. ' end;',
  17530. ' IBird = interface(IUnknown)',
  17531. ' procedure DoIt(b: boolean); overload;',
  17532. ' end;',
  17533. ' TObject = class',
  17534. ' end;',
  17535. ' TBird = class(TObject,IBird)',
  17536. ' procedure DoIt(o: TObject);',
  17537. ' procedure DoIt(s: string);',
  17538. ' procedure DoIt(i: integer);',
  17539. ' procedure DoIt(b: boolean);',
  17540. ' end;',
  17541. 'procedure TBird.DoIt(o: TObject); begin end;',
  17542. 'procedure TBird.DoIt(s: string); begin end;',
  17543. 'procedure TBird.DoIt(i: integer); begin end;',
  17544. 'procedure TBird.DoIt(b: boolean); begin end;',
  17545. 'var',
  17546. ' BirdIntf: IBird;',
  17547. 'begin',
  17548. ' BirdIntf.DoIt(3);',
  17549. ' BirdIntf.DoIt(''abc'');',
  17550. ' BirdIntf.DoIt(true);',
  17551. '']);
  17552. ConvertProgram;
  17553. CheckSource('TestClassInterface_Overloads',
  17554. LinesToStr([ // statements
  17555. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  17556. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  17557. 'rtl.createClass($mod, "TObject", null, function () {',
  17558. ' this.$init = function () {',
  17559. ' };',
  17560. ' this.$final = function () {',
  17561. ' };',
  17562. '});',
  17563. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17564. ' this.DoIt = function (o) {',
  17565. ' };',
  17566. ' this.DoIt$1 = function (s) {',
  17567. ' };',
  17568. ' this.DoIt$2 = function (i) {',
  17569. ' };',
  17570. ' this.DoIt$3 = function (b) {',
  17571. ' };',
  17572. ' rtl.addIntf(this, $mod.IBird, {',
  17573. ' DoIt$2: "DoIt$3",',
  17574. ' DoIt: "DoIt$2"',
  17575. ' });',
  17576. '});',
  17577. 'this.BirdIntf = null;',
  17578. '']),
  17579. LinesToStr([ // $mod.$main
  17580. '$mod.BirdIntf.DoIt(3);',
  17581. '$mod.BirdIntf.DoIt$1("abc");',
  17582. '$mod.BirdIntf.DoIt$2(true);',
  17583. '']));
  17584. end;
  17585. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  17586. begin
  17587. StartProgram(false);
  17588. Add([
  17589. '{$interfaces corba}',
  17590. 'type',
  17591. ' IBird = interface',
  17592. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17593. ' end;',
  17594. ' IDog = interface',
  17595. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17596. ' end;',
  17597. ' TObject = class(IBird,IDog)',
  17598. ' end;',
  17599. 'begin']);
  17600. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  17601. nDuplicateGUIDXInYZ);
  17602. ConvertProgram;
  17603. end;
  17604. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  17605. begin
  17606. StartProgram(false);
  17607. Add([
  17608. '{$interfaces corba}',
  17609. 'type',
  17610. ' IAnimal = interface',
  17611. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17612. ' end;',
  17613. ' IBird = interface(IAnimal)',
  17614. ' end;',
  17615. ' IHawk = interface(IBird)',
  17616. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17617. ' end;',
  17618. 'begin']);
  17619. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  17620. nDuplicateGUIDXInYZ);
  17621. ConvertProgram;
  17622. end;
  17623. procedure TTestModule.TestClassInterface_AncestorImpl;
  17624. begin
  17625. StartProgram(false);
  17626. Add([
  17627. '{$interfaces corba}',
  17628. 'type',
  17629. ' integer = longint;',
  17630. ' IUnknown = interface',
  17631. ' procedure DoIt(i: integer);',
  17632. ' end;',
  17633. ' IBird = interface',
  17634. ' procedure Fly(i: integer);',
  17635. ' end;',
  17636. ' TObject = class(IUnknown)',
  17637. ' procedure DoIt(i: integer);',
  17638. ' end;',
  17639. ' TBird = class(IBird)',
  17640. ' procedure Fly(i: integer);',
  17641. ' end;',
  17642. 'procedure TObject.DoIt(i: integer); begin end;',
  17643. 'procedure TBird.Fly(i: integer); begin end;',
  17644. 'begin',
  17645. '']);
  17646. ConvertProgram;
  17647. CheckSource('TestClassInterface_AncestorIntf',
  17648. LinesToStr([ // statements
  17649. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  17650. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  17651. 'rtl.createClass($mod, "TObject", null, function () {',
  17652. ' this.$init = function () {',
  17653. ' };',
  17654. ' this.$final = function () {',
  17655. ' };',
  17656. ' this.DoIt = function (i) {',
  17657. ' };',
  17658. ' rtl.addIntf(this, $mod.IUnknown);',
  17659. '});',
  17660. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17661. ' this.Fly = function (i) {',
  17662. ' };',
  17663. ' rtl.addIntf(this, $mod.IBird);',
  17664. ' rtl.addIntf(this, $mod.IUnknown);',
  17665. '});',
  17666. '']),
  17667. LinesToStr([ // $mod.$main
  17668. '']));
  17669. end;
  17670. procedure TTestModule.TestClassInterface_ImplReintroduce;
  17671. begin
  17672. StartProgram(false);
  17673. Add([
  17674. '{$interfaces corba}',
  17675. 'type',
  17676. ' integer = longint;',
  17677. ' IBird = interface',
  17678. ' procedure DoIt(i: integer);',
  17679. ' end;',
  17680. ' TObject = class',
  17681. ' procedure DoIt(i: integer);',
  17682. ' end;',
  17683. ' TBird = class(IBird)',
  17684. ' procedure DoIt(i: integer); virtual; reintroduce;',
  17685. ' end;',
  17686. 'procedure TObject.DoIt(i: integer); begin end;',
  17687. 'procedure TBird.DoIt(i: integer); begin end;',
  17688. 'begin',
  17689. '']);
  17690. ConvertProgram;
  17691. CheckSource('TestClassInterface_ImplReintroduce',
  17692. LinesToStr([ // statements
  17693. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  17694. 'rtl.createClass($mod, "TObject", null, function () {',
  17695. ' this.$init = function () {',
  17696. ' };',
  17697. ' this.$final = function () {',
  17698. ' };',
  17699. ' this.DoIt = function (i) {',
  17700. ' };',
  17701. '});',
  17702. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17703. ' this.DoIt$1 = function (i) {',
  17704. ' };',
  17705. ' rtl.addIntf(this, $mod.IBird, {',
  17706. ' DoIt: "DoIt$1"',
  17707. ' });',
  17708. '});',
  17709. '']),
  17710. LinesToStr([ // $mod.$main
  17711. '']));
  17712. end;
  17713. procedure TTestModule.TestClassInterface_MethodResolution;
  17714. begin
  17715. StartProgram(false);
  17716. Add([
  17717. '{$interfaces corba}',
  17718. 'type',
  17719. ' IUnknown = interface',
  17720. ' procedure Walk(i: longint);',
  17721. ' end;',
  17722. ' IBird = interface(IUnknown)',
  17723. ' procedure Walk(b: boolean); overload;',
  17724. ' procedure Fly(s: string);',
  17725. ' end;',
  17726. ' TObject = class',
  17727. ' end;',
  17728. ' TBird = class(TObject,IBird)',
  17729. ' procedure IBird.Fly = Move;',
  17730. ' procedure IBird.Walk = Hop;',
  17731. ' procedure Hop(i: longint);',
  17732. ' procedure Move(s: string);',
  17733. ' procedure Hop(b: boolean);',
  17734. ' end;',
  17735. 'procedure TBird.Move(s: string); begin end;',
  17736. 'procedure TBird.Hop(i: longint); begin end;',
  17737. 'procedure TBird.Hop(b: boolean); begin end;',
  17738. 'var',
  17739. ' BirdIntf: IBird;',
  17740. 'begin',
  17741. ' BirdIntf.Walk(3);',
  17742. ' BirdIntf.Walk(true);',
  17743. ' BirdIntf.Fly(''abc'');',
  17744. '']);
  17745. ConvertProgram;
  17746. CheckSource('TestClassInterface_MethodResolution',
  17747. LinesToStr([ // statements
  17748. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  17749. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  17750. 'rtl.createClass($mod, "TObject", null, function () {',
  17751. ' this.$init = function () {',
  17752. ' };',
  17753. ' this.$final = function () {',
  17754. ' };',
  17755. '});',
  17756. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17757. ' this.Hop = function (i) {',
  17758. ' };',
  17759. ' this.Move = function (s) {',
  17760. ' };',
  17761. ' this.Hop$1 = function (b) {',
  17762. ' };',
  17763. ' rtl.addIntf(this, $mod.IBird, {',
  17764. ' Walk$1: "Hop$1",',
  17765. ' Fly: "Move",',
  17766. ' Walk: "Hop"',
  17767. ' });',
  17768. '});',
  17769. 'this.BirdIntf = null;',
  17770. '']),
  17771. LinesToStr([ // $mod.$main
  17772. '$mod.BirdIntf.Walk(3);',
  17773. '$mod.BirdIntf.Walk$1(true);',
  17774. '$mod.BirdIntf.Fly("abc");',
  17775. '']));
  17776. end;
  17777. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  17778. begin
  17779. StartProgram(false);
  17780. Add([
  17781. '{$interfaces com}',
  17782. 'type',
  17783. ' IUnknown = interface',
  17784. ' function _AddRef: longint;',
  17785. ' procedure Walk;',
  17786. ' end;',
  17787. ' IBird = interface end;',
  17788. ' IDog = interface end;',
  17789. ' TObject = class(IBird,IDog)',
  17790. ' function _AddRef: longint; virtual; abstract;',
  17791. ' procedure Walk; virtual; abstract;',
  17792. ' end;',
  17793. ' TBird = class(IUnknown)',
  17794. ' end;',
  17795. 'begin',
  17796. '']);
  17797. ConvertProgram;
  17798. CheckSource('TestClassInterface_COM_AncestorLess',
  17799. LinesToStr([ // statements
  17800. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  17801. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  17802. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  17803. 'rtl.createClass($mod, "TObject", null, function () {',
  17804. ' this.$init = function () {',
  17805. ' };',
  17806. ' this.$final = function () {',
  17807. ' };',
  17808. ' rtl.addIntf(this, $mod.IBird);',
  17809. ' rtl.addIntf(this, $mod.IDog);',
  17810. '});',
  17811. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17812. ' rtl.addIntf(this, $mod.IUnknown);',
  17813. ' rtl.addIntf(this, $mod.IBird);',
  17814. ' rtl.addIntf(this, $mod.IDog);',
  17815. '});',
  17816. '']),
  17817. LinesToStr([ // $mod.$main
  17818. '']));
  17819. end;
  17820. procedure TTestModule.TestClassInterface_MethodOverride;
  17821. begin
  17822. StartProgram(false);
  17823. Add([
  17824. '{$interfaces corba}',
  17825. 'type',
  17826. ' IUnknown = interface',
  17827. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  17828. ' procedure Go;',
  17829. ' end;',
  17830. ' TObject = class(IUnknown)',
  17831. ' procedure Go; virtual; abstract;',
  17832. ' end;',
  17833. ' TBird = class',
  17834. ' procedure Go; override;',
  17835. ' end;',
  17836. ' TCat = class(TObject)',
  17837. ' procedure Go; override;',
  17838. ' end;',
  17839. ' TDog = class(TObject, IUnknown)',
  17840. ' procedure Go; override;',
  17841. ' end;',
  17842. 'procedure TBird.Go; begin end;',
  17843. 'procedure TCat.Go; begin end;',
  17844. 'procedure TDog.Go; begin end;',
  17845. 'begin',
  17846. '']);
  17847. ConvertProgram;
  17848. CheckSource('TestClassInterface_MethodOverride',
  17849. LinesToStr([ // statements
  17850. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  17851. 'rtl.createClass($mod, "TObject", null, function () {',
  17852. ' this.$init = function () {',
  17853. ' };',
  17854. ' this.$final = function () {',
  17855. ' };',
  17856. ' rtl.addIntf(this, $mod.IUnknown);',
  17857. '});',
  17858. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17859. ' this.Go = function () {',
  17860. ' };',
  17861. ' rtl.addIntf(this, $mod.IUnknown);',
  17862. '});',
  17863. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  17864. ' this.Go = function () {',
  17865. ' };',
  17866. ' rtl.addIntf(this, $mod.IUnknown);',
  17867. '});',
  17868. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  17869. ' this.Go = function () {',
  17870. ' };',
  17871. ' rtl.addIntf(this, $mod.IUnknown);',
  17872. '});',
  17873. '']),
  17874. LinesToStr([ // $mod.$main
  17875. '']));
  17876. end;
  17877. procedure TTestModule.TestClassInterface_Corba_Delegation;
  17878. begin
  17879. StartProgram(false);
  17880. Add([
  17881. '{$interfaces corba}',
  17882. 'type',
  17883. ' IUnknown = interface',
  17884. ' end;',
  17885. ' IBird = interface(IUnknown)',
  17886. ' procedure Fly(s: string);',
  17887. ' end;',
  17888. ' IEagle = interface(IBird)',
  17889. ' end;',
  17890. ' IDove = interface(IBird)',
  17891. ' end;',
  17892. ' ISwallow = interface(IBird)',
  17893. ' end;',
  17894. ' TObject = class',
  17895. ' end;',
  17896. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17897. ' procedure Fly(s: string); virtual; abstract;',
  17898. ' end;',
  17899. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17900. ' FBirdIntf: IBird;',
  17901. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  17902. ' function GetEagleIntf: IEagle; virtual; abstract;',
  17903. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17904. ' FDoveObj: TBird;',
  17905. ' property DoveObj: TBird read FDoveObj implements IDove;',
  17906. ' function GetSwallowObj: TBird; virtual; abstract;',
  17907. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17908. ' end;',
  17909. 'begin',
  17910. '']);
  17911. ConvertProgram;
  17912. CheckSource('TestClassInterface_Delegation',
  17913. LinesToStr([ // statements
  17914. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17915. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  17916. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  17917. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  17918. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  17919. 'rtl.createClass($mod, "TObject", null, function () {',
  17920. ' this.$init = function () {',
  17921. ' };',
  17922. ' this.$final = function () {',
  17923. ' };',
  17924. '});',
  17925. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17926. ' rtl.addIntf(this, $mod.IBird);',
  17927. ' rtl.addIntf(this, $mod.IEagle);',
  17928. ' rtl.addIntf(this, $mod.IDove);',
  17929. ' rtl.addIntf(this, $mod.ISwallow);',
  17930. '});',
  17931. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17932. ' this.$init = function () {',
  17933. ' $mod.TObject.$init.call(this);',
  17934. ' this.FBirdIntf = null;',
  17935. ' this.FDoveObj = null;',
  17936. ' };',
  17937. ' this.$final = function () {',
  17938. ' this.FBirdIntf = undefined;',
  17939. ' this.FDoveObj = undefined;',
  17940. ' $mod.TObject.$final.call(this);',
  17941. ' };',
  17942. ' this.$intfmaps = {',
  17943. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  17944. ' return this.FBirdIntf;',
  17945. ' },',
  17946. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  17947. ' return this.GetEagleIntf();',
  17948. ' },',
  17949. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  17950. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  17951. ' },',
  17952. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17953. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  17954. ' }',
  17955. ' };',
  17956. '});',
  17957. '']),
  17958. LinesToStr([ // $mod.$main
  17959. '']));
  17960. end;
  17961. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  17962. begin
  17963. StartProgram(false);
  17964. Add([
  17965. '{$interfaces corba}',
  17966. 'type',
  17967. ' IUnknown = interface',
  17968. ' end;',
  17969. ' IBird = interface(IUnknown)',
  17970. ' procedure Fly(s: string);',
  17971. ' end;',
  17972. ' IEagle = interface(IBird)',
  17973. ' end;',
  17974. ' IDove = interface(IBird)',
  17975. ' end;',
  17976. ' ISwallow = interface(IBird)',
  17977. ' end;',
  17978. ' TObject = class',
  17979. ' end;',
  17980. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17981. ' procedure Fly(s: string); virtual; abstract;',
  17982. ' end;',
  17983. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17984. ' private',
  17985. ' class var FBirdIntf: IBird;',
  17986. ' class var FDoveObj: TBird;',
  17987. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  17988. ' class function GetSwallowObj: TBird; virtual; abstract;',
  17989. ' protected',
  17990. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  17991. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17992. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  17993. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17994. ' end;',
  17995. 'begin',
  17996. '']);
  17997. ConvertProgram;
  17998. CheckSource('TestClassInterface_DelegationStatic',
  17999. LinesToStr([ // statements
  18000. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18001. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  18002. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  18003. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  18004. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  18005. 'rtl.createClass($mod, "TObject", null, function () {',
  18006. ' this.$init = function () {',
  18007. ' };',
  18008. ' this.$final = function () {',
  18009. ' };',
  18010. '});',
  18011. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18012. ' rtl.addIntf(this, $mod.IBird);',
  18013. ' rtl.addIntf(this, $mod.IEagle);',
  18014. ' rtl.addIntf(this, $mod.IDove);',
  18015. ' rtl.addIntf(this, $mod.ISwallow);',
  18016. '});',
  18017. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18018. ' this.FBirdIntf = null;',
  18019. ' this.FDoveObj = null;',
  18020. ' this.$intfmaps = {',
  18021. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18022. ' return this.FBirdIntf;',
  18023. ' },',
  18024. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18025. ' return this.GetEagleIntf();',
  18026. ' },',
  18027. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18028. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18029. ' },',
  18030. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18031. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18032. ' }',
  18033. ' };',
  18034. '});',
  18035. '']),
  18036. LinesToStr([ // $mod.$main
  18037. '']));
  18038. end;
  18039. procedure TTestModule.TestClassInterface_Corba_Operators;
  18040. begin
  18041. StartProgram(false);
  18042. Add([
  18043. '{$interfaces corba}',
  18044. 'type',
  18045. ' IUnknown = interface',
  18046. ' end;',
  18047. ' IBird = interface(IUnknown)',
  18048. ' function GetItems(Index: longint): longint;',
  18049. ' procedure SetItems(Index: longint; Value: longint);',
  18050. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  18051. ' end;',
  18052. ' TObject = class',
  18053. ' end;',
  18054. ' TBird = class(TObject,IBird)',
  18055. ' function GetItems(Index: longint): longint; virtual; abstract;',
  18056. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  18057. ' end;',
  18058. 'var',
  18059. ' IntfVar: IBird = nil;',
  18060. ' IntfVar2: IBird;',
  18061. ' ObjVar: TBird;',
  18062. ' v: JSValue;',
  18063. 'begin',
  18064. ' IntfVar:=nil;',
  18065. ' IntfVar[3]:=IntfVar[4];',
  18066. ' if Assigned(IntfVar) then ;',
  18067. ' IntfVar:=IntfVar2;',
  18068. ' IntfVar:=ObjVar;',
  18069. ' if IntfVar=IntfVar2 then ;',
  18070. ' if IntfVar<>IntfVar2 then ;',
  18071. ' if IntfVar is IBird then ;',
  18072. ' if IntfVar is TBird then ;',
  18073. ' if ObjVar is IBird then ;',
  18074. ' IntfVar:=IntfVar2 as IBird;',
  18075. ' ObjVar:=IntfVar2 as TBird;',
  18076. ' IntfVar:=ObjVar as IBird;',
  18077. ' IntfVar:=IBird(IntfVar2);',
  18078. ' ObjVar:=TBird(IntfVar);',
  18079. ' IntfVar:=IBird(ObjVar);',
  18080. ' v:=IntfVar;',
  18081. ' IntfVar:=IBird(v);',
  18082. ' if v is IBird then ;',
  18083. ' v:=JSValue(IntfVar);',
  18084. ' v:=IBird;',
  18085. '']);
  18086. ConvertProgram;
  18087. CheckSource('TestClassInterface_Corba_Operators',
  18088. LinesToStr([ // statements
  18089. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18090. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  18091. 'rtl.createClass($mod, "TObject", null, function () {',
  18092. ' this.$init = function () {',
  18093. ' };',
  18094. ' this.$final = function () {',
  18095. ' };',
  18096. '});',
  18097. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18098. ' rtl.addIntf(this, $mod.IBird);',
  18099. '});',
  18100. 'this.IntfVar = null;',
  18101. 'this.IntfVar2 = null;',
  18102. 'this.ObjVar = null;',
  18103. 'this.v = undefined;',
  18104. '']),
  18105. LinesToStr([ // $mod.$main
  18106. '$mod.IntfVar = null;',
  18107. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  18108. 'if ($mod.IntfVar != null) ;',
  18109. '$mod.IntfVar = $mod.IntfVar2;',
  18110. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  18111. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  18112. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  18113. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  18114. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  18115. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  18116. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  18117. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  18118. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18119. '$mod.IntfVar = $mod.IntfVar2;',
  18120. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  18121. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18122. '$mod.v = $mod.IntfVar;',
  18123. '$mod.IntfVar = rtl.getObject($mod.v);',
  18124. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  18125. '$mod.v = $mod.IntfVar;',
  18126. '$mod.v = $mod.IBird;',
  18127. '']));
  18128. end;
  18129. procedure TTestModule.TestClassInterface_Corba_Args;
  18130. begin
  18131. StartProgram(false);
  18132. Add([
  18133. '{$interfaces corba}',
  18134. 'type',
  18135. ' IUnknown = interface',
  18136. ' end;',
  18137. ' IBird = interface(IUnknown)',
  18138. ' end;',
  18139. ' TObject = class',
  18140. ' end;',
  18141. ' TBird = class(TObject,IBird)',
  18142. ' end;',
  18143. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  18144. 'begin',
  18145. ' DoIt(i,i,i);',
  18146. 'end;',
  18147. 'procedure Change(var i: IBird; out j: IBird);',
  18148. 'begin',
  18149. ' DoIt(i,i,i);',
  18150. ' Change(i,i);',
  18151. 'end;',
  18152. 'var',
  18153. ' i: IBird;',
  18154. ' o: TBird;',
  18155. 'begin',
  18156. ' DoIt(i,i,i);',
  18157. ' Change(i,i);',
  18158. ' DoIt(o,o,o);',
  18159. '']);
  18160. ConvertProgram;
  18161. CheckSource('TestClassInterface_Corba_Args',
  18162. LinesToStr([ // statements
  18163. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18164. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  18165. 'rtl.createClass($mod, "TObject", null, function () {',
  18166. ' this.$init = function () {',
  18167. ' };',
  18168. ' this.$final = function () {',
  18169. ' };',
  18170. '});',
  18171. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18172. ' rtl.addIntf(this, $mod.IBird);',
  18173. '});',
  18174. 'this.DoIt = function (u, i, j) {',
  18175. ' $mod.DoIt({',
  18176. ' get: function () {',
  18177. ' return i;',
  18178. ' },',
  18179. ' set: function (v) {',
  18180. ' i = v;',
  18181. ' }',
  18182. ' }, i, i);',
  18183. '};',
  18184. 'this.Change = function (i, j) {',
  18185. ' $mod.DoIt(i, i.get(), i.get());',
  18186. ' $mod.Change(i, i);',
  18187. '};',
  18188. 'this.i = null;',
  18189. 'this.o = null;',
  18190. '']),
  18191. LinesToStr([ // $mod.$main
  18192. '$mod.DoIt({',
  18193. ' p: $mod,',
  18194. ' get: function () {',
  18195. ' return this.p.i;',
  18196. ' },',
  18197. ' set: function (v) {',
  18198. ' this.p.i = v;',
  18199. ' }',
  18200. '}, $mod.i, $mod.i);',
  18201. '$mod.Change({',
  18202. ' p: $mod,',
  18203. ' get: function () {',
  18204. ' return this.p.i;',
  18205. ' },',
  18206. ' set: function (v) {',
  18207. ' this.p.i = v;',
  18208. ' }',
  18209. '}, {',
  18210. ' p: $mod,',
  18211. ' get: function () {',
  18212. ' return this.p.i;',
  18213. ' },',
  18214. ' set: function (v) {',
  18215. ' this.p.i = v;',
  18216. ' }',
  18217. '});',
  18218. '$mod.DoIt({',
  18219. ' p: $mod,',
  18220. ' get: function () {',
  18221. ' return this.p.o;',
  18222. ' },',
  18223. ' set: function (v) {',
  18224. ' this.p.o = v;',
  18225. ' }',
  18226. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  18227. '']));
  18228. end;
  18229. procedure TTestModule.TestClassInterface_Corba_ForIn;
  18230. begin
  18231. StartProgram(false);
  18232. Add([
  18233. '{$interfaces corba}',
  18234. 'type',
  18235. ' IUnknown = interface end;',
  18236. ' TObject = class',
  18237. ' Id: longint;',
  18238. ' end;',
  18239. ' IEnumerator = interface(IUnknown)',
  18240. ' function GetCurrent: TObject;',
  18241. ' function MoveNext: Boolean;',
  18242. ' property Current: TObject read GetCurrent;',
  18243. ' end;',
  18244. ' IEnumerable = interface(IUnknown)',
  18245. ' function GetEnumerator: IEnumerator;',
  18246. ' end;',
  18247. 'var',
  18248. ' o: TObject;',
  18249. ' i: IEnumerable;',
  18250. 'begin',
  18251. ' for o in i do o.Id:=3;',
  18252. '']);
  18253. ConvertProgram;
  18254. CheckSource('TestClassInterface_Corba_ForIn',
  18255. LinesToStr([ // statements
  18256. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18257. 'rtl.createClass($mod, "TObject", null, function () {',
  18258. ' this.$init = function () {',
  18259. ' this.Id = 0;',
  18260. ' };',
  18261. ' this.$final = function () {',
  18262. ' };',
  18263. '});',
  18264. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  18265. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  18266. 'this.o = null;',
  18267. 'this.i = null;',
  18268. '']),
  18269. LinesToStr([ // $mod.$main
  18270. 'var $in1 = $mod.i.GetEnumerator();',
  18271. 'while ($in1.MoveNext()) {',
  18272. ' $mod.o = $in1.GetCurrent();',
  18273. ' $mod.o.Id = 3;',
  18274. '};',
  18275. '']));
  18276. end;
  18277. procedure TTestModule.TestClassInterface_COM_AssignVar;
  18278. begin
  18279. StartProgram(false);
  18280. Add([
  18281. '{$interfaces com}',
  18282. 'type',
  18283. ' IUnknown = interface',
  18284. ' function _AddRef: longint;',
  18285. ' function _Release: longint;',
  18286. ' end;',
  18287. ' TObject = class(IUnknown)',
  18288. ' function _AddRef: longint; virtual; abstract;',
  18289. ' function _Release: longint; virtual; abstract;',
  18290. ' end;',
  18291. 'var',
  18292. ' i: IUnknown;',
  18293. 'procedure DoGlobal(o: TObject);',
  18294. 'begin',
  18295. ' i:=nil;',
  18296. ' i:=o;',
  18297. ' i:=i;',
  18298. 'end;',
  18299. 'procedure DoLocal(o: TObject);',
  18300. 'const k: IUnknown = nil;',
  18301. 'var j: IUnknown;',
  18302. 'begin',
  18303. ' k:=o;',
  18304. ' k:=i;',
  18305. ' j:=o;',
  18306. ' j:=i;',
  18307. 'end;',
  18308. 'var o: TObject;',
  18309. 'begin',
  18310. ' i:=nil;',
  18311. ' i:=o;',
  18312. '']);
  18313. ConvertProgram;
  18314. CheckSource('TestClassInterface_COM_AssignVar',
  18315. LinesToStr([ // statements
  18316. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18317. 'rtl.createClass($mod, "TObject", null, function () {',
  18318. ' this.$init = function () {',
  18319. ' };',
  18320. ' this.$final = function () {',
  18321. ' };',
  18322. ' rtl.addIntf(this, $mod.IUnknown);',
  18323. '});',
  18324. 'this.i = null;',
  18325. 'this.DoGlobal = function (o) {',
  18326. ' rtl.setIntfP($mod, "i", null);',
  18327. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  18328. ' rtl.setIntfP($mod, "i", $mod.i);',
  18329. '};',
  18330. 'var k = null;',
  18331. 'this.DoLocal = function (o) {',
  18332. ' var j = null;',
  18333. ' try{',
  18334. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18335. ' k = rtl.setIntfL(k, $mod.i);',
  18336. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18337. ' j = rtl.setIntfL(j, $mod.i);',
  18338. ' }finally{',
  18339. ' rtl._Release(j);',
  18340. ' };',
  18341. '};',
  18342. 'this.o = null;',
  18343. '']),
  18344. LinesToStr([ // $mod.$main
  18345. 'rtl.setIntfP($mod, "i", null);',
  18346. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  18347. '']));
  18348. end;
  18349. procedure TTestModule.TestClassInterface_COM_AssignArg;
  18350. begin
  18351. StartProgram(false);
  18352. Add([
  18353. '{$interfaces com}',
  18354. 'type',
  18355. ' IUnknown = interface',
  18356. ' function _AddRef: longint;',
  18357. ' function _Release: longint;',
  18358. ' end;',
  18359. ' TObject = class(IUnknown)',
  18360. ' function _AddRef: longint; virtual; abstract;',
  18361. ' function _Release: longint; virtual; abstract;',
  18362. ' end;',
  18363. 'procedure DoDefault(i, j: IUnknown);',
  18364. 'begin',
  18365. ' i:=nil;',
  18366. ' i:=j;',
  18367. 'end;',
  18368. 'begin',
  18369. '']);
  18370. ConvertProgram;
  18371. CheckSource('TestClassInterface_COM_AssignArg',
  18372. LinesToStr([ // statements
  18373. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18374. 'rtl.createClass($mod, "TObject", null, function () {',
  18375. ' this.$init = function () {',
  18376. ' };',
  18377. ' this.$final = function () {',
  18378. ' };',
  18379. ' rtl.addIntf(this, $mod.IUnknown);',
  18380. '});',
  18381. 'this.DoDefault = function (i, j) {',
  18382. ' rtl._AddRef(i);',
  18383. ' try {',
  18384. ' i = rtl.setIntfL(i, null);',
  18385. ' i = rtl.setIntfL(i, j);',
  18386. ' } finally {',
  18387. ' rtl._Release(i);',
  18388. ' };',
  18389. '};',
  18390. '']),
  18391. LinesToStr([ // $mod.$main
  18392. '']));
  18393. end;
  18394. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  18395. begin
  18396. StartProgram(false);
  18397. Add([
  18398. '{$interfaces com}',
  18399. 'type',
  18400. ' IUnknown = interface',
  18401. ' function _AddRef: longint;',
  18402. ' function _Release: longint;',
  18403. ' end;',
  18404. ' TObject = class(IUnknown)',
  18405. ' function _AddRef: longint; virtual; abstract;',
  18406. ' function _Release: longint; virtual; abstract;',
  18407. ' end;',
  18408. 'function DoDefault(i: IUnknown): IUnknown;',
  18409. 'begin',
  18410. ' Result:=i;',
  18411. ' if Result<>nil then exit;',
  18412. 'end;',
  18413. 'begin',
  18414. '']);
  18415. ConvertProgram;
  18416. CheckSource('TestClassInterface_COM_FunctionResult',
  18417. LinesToStr([ // statements
  18418. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18419. 'rtl.createClass($mod, "TObject", null, function () {',
  18420. ' this.$init = function () {',
  18421. ' };',
  18422. ' this.$final = function () {',
  18423. ' };',
  18424. ' rtl.addIntf(this, $mod.IUnknown);',
  18425. '});',
  18426. 'this.DoDefault = function (i) {',
  18427. ' var Result = null;',
  18428. ' var $ok = false;',
  18429. ' try {',
  18430. ' Result = rtl.setIntfL(Result, i);',
  18431. ' if(Result !== null){',
  18432. ' $ok = true;',
  18433. ' return Result;',
  18434. ' };',
  18435. ' $ok = true;',
  18436. ' } finally {',
  18437. ' if(!$ok) rtl._Release(Result);',
  18438. ' };',
  18439. ' return Result;',
  18440. '};',
  18441. '']),
  18442. LinesToStr([ // $mod.$main
  18443. '']));
  18444. end;
  18445. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  18446. begin
  18447. StartProgram(false);
  18448. Add([
  18449. '{$interfaces com}',
  18450. 'type',
  18451. ' IUnknown = interface',
  18452. ' function _AddRef: longint;',
  18453. ' function _Release: longint;',
  18454. ' end;',
  18455. ' TObject = class(IUnknown)',
  18456. ' function _AddRef: longint; virtual; abstract;',
  18457. ' function _Release: longint; virtual; abstract;',
  18458. ' function GetIntf: IUnknown; virtual;',
  18459. ' end;',
  18460. ' TMouse = class',
  18461. ' function GetIntf: IUnknown; override;',
  18462. ' end;',
  18463. 'function TObject.GetIntf: IUnknown; begin end;',
  18464. 'function TMouse.GetIntf: IUnknown;',
  18465. 'var i: IUnknown;',
  18466. 'begin',
  18467. ' inherited;',
  18468. ' inherited GetIntf;',
  18469. ' inherited GetIntf();',
  18470. ' Result:=inherited GetIntf;',
  18471. ' Result:=inherited GetIntf();',
  18472. ' i:=inherited GetIntf;',
  18473. ' i:=inherited GetIntf();',
  18474. 'end;',
  18475. 'begin',
  18476. '']);
  18477. ConvertProgram;
  18478. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  18479. LinesToStr([ // statements
  18480. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18481. 'rtl.createClass($mod, "TObject", null, function () {',
  18482. ' this.$init = function () {',
  18483. ' };',
  18484. ' this.$final = function () {',
  18485. ' };',
  18486. ' this.GetIntf = function () {',
  18487. ' var Result = null;',
  18488. ' return Result;',
  18489. ' };',
  18490. ' rtl.addIntf(this, $mod.IUnknown);',
  18491. '});',
  18492. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  18493. ' this.GetIntf = function () {',
  18494. ' var Result = null;',
  18495. ' var i = null;',
  18496. ' var $ir = rtl.createIntfRefs();',
  18497. ' var $ok = false;',
  18498. ' try {',
  18499. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  18500. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  18501. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  18502. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18503. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18504. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18505. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18506. ' $ok = true;',
  18507. ' } finally {',
  18508. ' $ir.free();',
  18509. ' rtl._Release(i);',
  18510. ' if (!$ok) rtl._Release(Result);',
  18511. ' };',
  18512. ' return Result;',
  18513. ' };',
  18514. ' rtl.addIntf(this, $mod.IUnknown);',
  18515. '});',
  18516. '']),
  18517. LinesToStr([ // $mod.$main
  18518. '']));
  18519. end;
  18520. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  18521. begin
  18522. StartProgram(false);
  18523. Add([
  18524. '{$interfaces com}',
  18525. 'type',
  18526. ' IUnknown = interface',
  18527. ' function _AddRef: longint;',
  18528. ' function _Release: longint;',
  18529. ' end;',
  18530. ' TObject = class(IUnknown)',
  18531. ' function _AddRef: longint; virtual; abstract;',
  18532. ' function _Release: longint; virtual; abstract;',
  18533. ' end;',
  18534. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  18535. 'begin',
  18536. ' if i is IUnknown then ;',
  18537. ' if o is IUnknown then ;',
  18538. ' if i is TObject then ;',
  18539. ' i:=j as IUnknown;',
  18540. ' i:=o as IUnknown;',
  18541. ' o:=j as TObject;',
  18542. ' i:=IUnknown(j);',
  18543. ' i:=IUnknown(o);',
  18544. ' o:=TObject(i);',
  18545. 'end;',
  18546. 'begin',
  18547. '']);
  18548. ConvertProgram;
  18549. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  18550. LinesToStr([ // statements
  18551. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18552. 'rtl.createClass($mod, "TObject", null, function () {',
  18553. ' this.$init = function () {',
  18554. ' };',
  18555. ' this.$final = function () {',
  18556. ' };',
  18557. ' rtl.addIntf(this, $mod.IUnknown);',
  18558. '});',
  18559. 'this.DoDefault = function (i, j, o) {',
  18560. ' rtl._AddRef(i);',
  18561. ' try {',
  18562. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  18563. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  18564. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  18565. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  18566. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18567. ' o = rtl.intfAsClass(j, $mod.TObject);',
  18568. ' i = rtl.setIntfL(i, j);',
  18569. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18570. ' o = rtl.intfToClass(i, $mod.TObject);',
  18571. ' } finally {',
  18572. ' rtl._Release(i);',
  18573. ' };',
  18574. '};',
  18575. '']),
  18576. LinesToStr([ // $mod.$main
  18577. '']));
  18578. end;
  18579. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  18580. begin
  18581. StartProgram(false);
  18582. Add([
  18583. '{$interfaces com}',
  18584. 'type',
  18585. ' IUnknown = interface',
  18586. ' function _AddRef: longint;',
  18587. ' function _Release: longint;',
  18588. ' end;',
  18589. ' TObject = class(IUnknown)',
  18590. ' function _AddRef: longint; virtual; abstract;',
  18591. ' function _Release: longint; virtual; abstract;',
  18592. ' end;',
  18593. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  18594. 'var o: TObject;',
  18595. 'begin',
  18596. ' DoIt(v,v,v,v);',
  18597. ' DoIt(o,o,k,k);',
  18598. 'end;',
  18599. 'procedure DoSome;',
  18600. 'var v: IUnknown;',
  18601. 'begin',
  18602. ' DoIt(v,v,v,v);',
  18603. 'end;',
  18604. 'var i: IUnknown;',
  18605. 'begin',
  18606. ' DoIt(i,i,i,i);',
  18607. '']);
  18608. ConvertProgram;
  18609. CheckSource('TestClassInterface_COM_PassAsArg',
  18610. LinesToStr([ // statements
  18611. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18612. 'rtl.createClass($mod, "TObject", null, function () {',
  18613. ' this.$init = function () {',
  18614. ' };',
  18615. ' this.$final = function () {',
  18616. ' };',
  18617. ' rtl.addIntf(this, $mod.IUnknown);',
  18618. '});',
  18619. 'this.DoIt = function (v, j, k, l) {',
  18620. ' var o = null;',
  18621. ' var $ir = rtl.createIntfRefs();',
  18622. ' rtl._AddRef(v);',
  18623. ' try {',
  18624. ' $mod.DoIt(v, v, {',
  18625. ' get: function () {',
  18626. ' return v;',
  18627. ' },',
  18628. ' set: function (w) {',
  18629. ' v = rtl.setIntfL(v, w);',
  18630. ' }',
  18631. ' }, {',
  18632. ' get: function () {',
  18633. ' return v;',
  18634. ' },',
  18635. ' set: function (w) {',
  18636. ' v = rtl.setIntfL(v, w);',
  18637. ' }',
  18638. ' });',
  18639. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  18640. ' } finally {',
  18641. ' $ir.free();',
  18642. ' rtl._Release(v);',
  18643. ' };',
  18644. '};',
  18645. 'this.DoSome = function () {',
  18646. ' var v = null;',
  18647. ' try {',
  18648. ' $mod.DoIt(v, v, {',
  18649. ' get: function () {',
  18650. ' return v;',
  18651. ' },',
  18652. ' set: function (w) {',
  18653. ' v = rtl.setIntfL(v, w);',
  18654. ' }',
  18655. ' }, {',
  18656. ' get: function () {',
  18657. ' return v;',
  18658. ' },',
  18659. ' set: function (w) {',
  18660. ' v = rtl.setIntfL(v, w);',
  18661. ' }',
  18662. ' });',
  18663. ' } finally {',
  18664. ' rtl._Release(v);',
  18665. ' };',
  18666. '};',
  18667. 'this.i = null;',
  18668. '']),
  18669. LinesToStr([ // $mod.$main
  18670. '$mod.DoIt($mod.i, $mod.i, {',
  18671. ' p: $mod,',
  18672. ' get: function () {',
  18673. ' return this.p.i;',
  18674. ' },',
  18675. ' set: function (v) {',
  18676. ' rtl.setIntfP(this.p, "i", v);',
  18677. ' }',
  18678. '}, {',
  18679. ' p: $mod,',
  18680. ' get: function () {',
  18681. ' return this.p.i;',
  18682. ' },',
  18683. ' set: function (v) {',
  18684. ' rtl.setIntfP(this.p, "i", v);',
  18685. ' }',
  18686. '});',
  18687. '']));
  18688. end;
  18689. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  18690. begin
  18691. StartProgram(false);
  18692. Add([
  18693. '{$interfaces com}',
  18694. 'type',
  18695. ' IUnknown = interface',
  18696. ' function _AddRef: longint;',
  18697. ' function _Release: longint;',
  18698. ' end;',
  18699. ' TObject = class(IUnknown)',
  18700. ' function _AddRef: longint; virtual; abstract;',
  18701. ' function _Release: longint; virtual; abstract;',
  18702. ' end;',
  18703. 'procedure DoIt(out i);',
  18704. 'begin end;',
  18705. 'procedure DoSome;',
  18706. 'var v: IUnknown;',
  18707. 'begin',
  18708. ' DoIt(v);',
  18709. 'end;',
  18710. 'function GetIt: IUnknown;',
  18711. 'begin',
  18712. ' DoIt(Result);',
  18713. 'end;',
  18714. 'var i: IUnknown;',
  18715. 'begin',
  18716. ' DoIt(i);',
  18717. '']);
  18718. ConvertProgram;
  18719. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  18720. LinesToStr([ // statements
  18721. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18722. 'rtl.createClass($mod, "TObject", null, function () {',
  18723. ' this.$init = function () {',
  18724. ' };',
  18725. ' this.$final = function () {',
  18726. ' };',
  18727. ' rtl.addIntf(this, $mod.IUnknown);',
  18728. '});',
  18729. 'this.DoIt = function (i) {',
  18730. '};',
  18731. 'this.DoSome = function () {',
  18732. ' var v = null;',
  18733. ' try {',
  18734. ' $mod.DoIt({',
  18735. ' get: function () {',
  18736. ' return v;',
  18737. ' },',
  18738. ' set: function (w) {',
  18739. ' v = w;',
  18740. ' }',
  18741. ' });',
  18742. ' } finally {',
  18743. ' rtl._Release(v);',
  18744. ' };',
  18745. '};',
  18746. 'this.GetIt = function () {',
  18747. ' var Result = null;',
  18748. ' var $ok = false;',
  18749. ' try {',
  18750. ' $mod.DoIt({',
  18751. ' get: function () {',
  18752. ' return Result;',
  18753. ' },',
  18754. ' set: function (v) {',
  18755. ' Result = v;',
  18756. ' }',
  18757. ' });',
  18758. ' $ok = true;',
  18759. ' } finally {',
  18760. ' if (!$ok) rtl._Release(Result);',
  18761. ' };',
  18762. ' return Result;',
  18763. '};',
  18764. 'this.i = null;',
  18765. '']),
  18766. LinesToStr([ // $mod.$main
  18767. 'try {',
  18768. ' $mod.DoIt({',
  18769. ' p: $mod,',
  18770. ' get: function () {',
  18771. ' return this.p.i;',
  18772. ' },',
  18773. ' set: function (v) {',
  18774. ' this.p.i = v;',
  18775. ' }',
  18776. ' });',
  18777. '} finally {',
  18778. ' rtl._Release($mod.i);',
  18779. '};',
  18780. '']));
  18781. end;
  18782. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  18783. begin
  18784. StartProgram(false);
  18785. Add([
  18786. '{$interfaces com}',
  18787. 'type',
  18788. ' IUnknown = interface',
  18789. ' function _AddRef: longint;',
  18790. ' function _Release: longint;',
  18791. ' end;',
  18792. ' TObject = class(IUnknown)',
  18793. ' function _AddRef: longint; virtual; abstract;',
  18794. ' function _Release: longint; virtual; abstract;',
  18795. ' end;',
  18796. 'function GetIt: IUnknown;',
  18797. 'begin',
  18798. 'end;',
  18799. 'procedure DoSome;',
  18800. 'var v: IUnknown;',
  18801. ' i: longint;',
  18802. 'begin',
  18803. ' v:=GetIt;',
  18804. ' v:=GetIt();',
  18805. ' GetIt()._AddRef;',
  18806. ' i:=GetIt()._AddRef;',
  18807. 'end;',
  18808. 'var v: IUnknown;',
  18809. ' i: longint;',
  18810. 'begin',
  18811. ' v:=GetIt;',
  18812. ' v:=GetIt();',
  18813. ' GetIt()._AddRef;',
  18814. ' i:=GetIt()._AddRef;',
  18815. '']);
  18816. ConvertProgram;
  18817. CheckSource('TestClassInterface_COM_FunctionInExpr',
  18818. LinesToStr([ // statements
  18819. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18820. 'rtl.createClass($mod, "TObject", null, function () {',
  18821. ' this.$init = function () {',
  18822. ' };',
  18823. ' this.$final = function () {',
  18824. ' };',
  18825. ' rtl.addIntf(this, $mod.IUnknown);',
  18826. '});',
  18827. 'this.GetIt = function () {',
  18828. ' var Result = null;',
  18829. ' return Result;',
  18830. '};',
  18831. 'this.DoSome = function () {',
  18832. ' var v = null;',
  18833. ' var i = 0;',
  18834. ' var $ir = rtl.createIntfRefs();',
  18835. ' try {',
  18836. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  18837. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  18838. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  18839. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  18840. ' } finally {',
  18841. ' $ir.free();',
  18842. ' rtl._Release(v);',
  18843. ' };',
  18844. '};',
  18845. 'this.v = null;',
  18846. 'this.i = 0;',
  18847. '']),
  18848. LinesToStr([ // $mod.$main
  18849. 'var $ir = rtl.createIntfRefs();',
  18850. 'try {',
  18851. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  18852. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  18853. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  18854. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  18855. '} finally {',
  18856. ' $ir.free();',
  18857. '};',
  18858. '']));
  18859. end;
  18860. procedure TTestModule.TestClassInterface_COM_Property;
  18861. begin
  18862. StartProgram(false);
  18863. Add([
  18864. '{$interfaces com}',
  18865. 'type',
  18866. ' IUnknown = interface',
  18867. ' function _AddRef: longint;',
  18868. ' function _Release: longint;',
  18869. ' end;',
  18870. ' TObject = class(IUnknown)',
  18871. ' FAnt: IUnknown;',
  18872. ' function _AddRef: longint; virtual; abstract;',
  18873. ' function _Release: longint; virtual; abstract;',
  18874. ' function GetBird: IUnknown; virtual; abstract;',
  18875. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18876. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18877. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18878. ' property Ant: IUnknown read FAnt write FAnt;',
  18879. ' property Bird: IUnknown read GetBird write SetBird;',
  18880. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18881. ' end;',
  18882. 'procedure DoIt;',
  18883. 'var',
  18884. ' o: TObject;',
  18885. ' v: IUnknown;',
  18886. 'begin',
  18887. ' v:=o.Ant;',
  18888. ' o.Ant:=v;',
  18889. ' o.Ant:=o.Ant;',
  18890. ' v:=o.Bird;',
  18891. ' o.Bird:=v;',
  18892. ' o.Bird:=o.Bird;',
  18893. ' v:=o.Items[1];',
  18894. ' o.Items[2]:=v;',
  18895. ' o.Items[3]:=o.Items[4];',
  18896. ' v:=o[5];',
  18897. ' o[6]:=v;',
  18898. ' o[7]:=o[8];',
  18899. 'end;',
  18900. 'begin',
  18901. '']);
  18902. ConvertProgram;
  18903. CheckSource('TestClassInterface_COM_Property',
  18904. LinesToStr([ // statements
  18905. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18906. 'rtl.createClass($mod, "TObject", null, function () {',
  18907. ' this.$init = function () {',
  18908. ' this.FAnt = null;',
  18909. ' };',
  18910. ' this.$final = function () {',
  18911. ' this.FAnt = undefined;',
  18912. ' };',
  18913. ' rtl.addIntf(this, $mod.IUnknown);',
  18914. '});',
  18915. 'this.DoIt = function () {',
  18916. ' var o = null;',
  18917. ' var v = null;',
  18918. ' var $ir = rtl.createIntfRefs();',
  18919. ' try {',
  18920. ' v = rtl.setIntfL(v, o.FAnt);',
  18921. ' rtl.setIntfP(o, "FAnt", v);',
  18922. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  18923. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  18924. ' o.SetBird(v);',
  18925. ' o.SetBird($ir.ref(1, o.GetBird()));',
  18926. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  18927. ' o.SetItems(2, v);',
  18928. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  18929. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  18930. ' o.SetItems(6, v);',
  18931. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  18932. ' } finally {',
  18933. ' $ir.free();',
  18934. ' rtl._Release(v);',
  18935. ' };',
  18936. '};',
  18937. '']),
  18938. LinesToStr([ // $mod.$main
  18939. '']));
  18940. end;
  18941. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  18942. begin
  18943. StartProgram(false);
  18944. Add([
  18945. '{$interfaces com}',
  18946. 'type',
  18947. ' IUnknown = interface',
  18948. ' function _AddRef: longint;',
  18949. ' function _Release: longint;',
  18950. ' function GetBird: IUnknown;',
  18951. ' procedure SetBird(Value: IUnknown);',
  18952. ' function GetItems(Index: longint): IUnknown;',
  18953. ' procedure SetItems(Index: longint; Value: IUnknown);',
  18954. ' property Bird: IUnknown read GetBird write SetBird;',
  18955. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18956. ' end;',
  18957. ' TObject = class(IUnknown)',
  18958. ' function _AddRef: longint; virtual; abstract;',
  18959. ' function _Release: longint; virtual; abstract;',
  18960. ' function GetBird: IUnknown; virtual; abstract;',
  18961. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18962. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18963. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18964. ' end;',
  18965. 'procedure DoIt;',
  18966. 'var',
  18967. ' o: TObject;',
  18968. ' v: IUnknown;',
  18969. 'begin',
  18970. ' v:=v.Items[1];',
  18971. ' v.Items[2]:=v;',
  18972. ' v.Items[3]:=v.Items[4];',
  18973. ' v:=v[5];',
  18974. ' v[6]:=v;',
  18975. ' v[7]:=v[8];',
  18976. ' v[9].Bird.Bird:=v;',
  18977. ' v:=v.Bird[10].Bird',
  18978. 'end;',
  18979. 'begin',
  18980. '']);
  18981. ConvertProgram;
  18982. CheckSource('TestClassInterface_COM_IntfProperty',
  18983. LinesToStr([ // statements
  18984. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  18985. ' "_AddRef",',
  18986. ' "_Release",',
  18987. ' "GetBird",',
  18988. ' "SetBird",',
  18989. ' "GetItems",',
  18990. ' "SetItems"',
  18991. '], null);',
  18992. 'rtl.createClass($mod, "TObject", null, function () {',
  18993. ' this.$init = function () {',
  18994. ' };',
  18995. ' this.$final = function () {',
  18996. ' };',
  18997. ' rtl.addIntf(this, $mod.IUnknown);',
  18998. '});',
  18999. 'this.DoIt = function () {',
  19000. ' var o = null;',
  19001. ' var v = null;',
  19002. ' var $ir = rtl.createIntfRefs();',
  19003. ' try {',
  19004. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  19005. ' v.SetItems(2, v);',
  19006. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  19007. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  19008. ' v.SetItems(6, v);',
  19009. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  19010. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  19011. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  19012. ' } finally {',
  19013. ' $ir.free();',
  19014. ' rtl._Release(v);',
  19015. ' };',
  19016. '};',
  19017. '']),
  19018. LinesToStr([ // $mod.$main
  19019. '']));
  19020. end;
  19021. procedure TTestModule.TestClassInterface_COM_Delegation;
  19022. begin
  19023. StartProgram(false);
  19024. Add([
  19025. '{$interfaces com}',
  19026. 'type',
  19027. ' IUnknown = interface',
  19028. ' function _AddRef: longint;',
  19029. ' function _Release: longint;',
  19030. ' end;',
  19031. ' IBird = interface(IUnknown)',
  19032. ' procedure Fly(s: string);',
  19033. ' end;',
  19034. ' IEagle = interface(IBird) end;',
  19035. ' IDove = interface(IBird) end;',
  19036. ' ISwallow = interface(IBird) end;',
  19037. ' TObject = class',
  19038. ' end;',
  19039. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19040. ' function _AddRef: longint; virtual; abstract;',
  19041. ' function _Release: longint; virtual; abstract;',
  19042. ' procedure Fly(s: string); virtual; abstract;',
  19043. ' end;',
  19044. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19045. ' function _AddRef: longint; virtual; abstract;',
  19046. ' function _Release: longint; virtual; abstract;',
  19047. ' FBirdIntf: IBird;',
  19048. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19049. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19050. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19051. ' FDoveObj: TBird;',
  19052. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19053. ' function GetSwallowObj: TBird; virtual; abstract;',
  19054. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19055. ' end;',
  19056. 'begin',
  19057. '']);
  19058. ConvertProgram;
  19059. CheckSource('TestClassInterface_COM_Delegation',
  19060. LinesToStr([ // statements
  19061. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19062. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  19063. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  19064. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  19065. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  19066. 'rtl.createClass($mod, "TObject", null, function () {',
  19067. ' this.$init = function () {',
  19068. ' };',
  19069. ' this.$final = function () {',
  19070. ' };',
  19071. '});',
  19072. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19073. ' rtl.addIntf(this, $mod.IBird);',
  19074. ' rtl.addIntf(this, $mod.IEagle);',
  19075. ' rtl.addIntf(this, $mod.IDove);',
  19076. ' rtl.addIntf(this, $mod.ISwallow);',
  19077. '});',
  19078. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  19079. ' this.$init = function () {',
  19080. ' $mod.TObject.$init.call(this);',
  19081. ' this.FBirdIntf = null;',
  19082. ' this.FDoveObj = null;',
  19083. ' };',
  19084. ' this.$final = function () {',
  19085. ' this.FBirdIntf = undefined;',
  19086. ' this.FDoveObj = undefined;',
  19087. ' $mod.TObject.$final.call(this);',
  19088. ' };',
  19089. ' this.$intfmaps = {',
  19090. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  19091. ' return rtl._AddRef(this.FBirdIntf);',
  19092. ' },',
  19093. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  19094. ' return this.GetEagleIntf();',
  19095. ' },',
  19096. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  19097. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  19098. ' },',
  19099. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  19100. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19101. ' }',
  19102. ' };',
  19103. '});',
  19104. '']),
  19105. LinesToStr([ // $mod.$main
  19106. '']));
  19107. end;
  19108. procedure TTestModule.TestClassInterface_COM_With;
  19109. begin
  19110. StartProgram(false);
  19111. Add([
  19112. '{$interfaces com}',
  19113. 'type',
  19114. ' IUnknown = interface',
  19115. ' function _AddRef: longint;',
  19116. ' function _Release: longint;',
  19117. ' function GetAnt: IUnknown;',
  19118. ' property Ant: IUnknown read GetAnt;',
  19119. ' end;',
  19120. ' TObject = class(IUnknown)',
  19121. ' function _AddRef: longint; virtual; abstract;',
  19122. ' function _Release: longint; virtual; abstract;',
  19123. ' function GetAnt: IUnknown; virtual; abstract;',
  19124. ' property Ant: IUnknown read GetAnt;',
  19125. ' end;',
  19126. 'procedure DoIt;',
  19127. 'var',
  19128. ' i: IUnknown;',
  19129. 'begin',
  19130. ' with i do ',
  19131. ' GetAnt;',
  19132. ' with i.Ant, Ant do ',
  19133. ' GetAnt;',
  19134. 'end;',
  19135. 'begin',
  19136. '']);
  19137. ConvertProgram;
  19138. CheckSource('TestClassInterface_COM_With',
  19139. LinesToStr([ // statements
  19140. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  19141. 'rtl.createClass($mod, "TObject", null, function () {',
  19142. ' this.$init = function () {',
  19143. ' };',
  19144. ' this.$final = function () {',
  19145. ' };',
  19146. ' rtl.addIntf(this, $mod.IUnknown);',
  19147. '});',
  19148. 'this.DoIt = function () {',
  19149. ' var i = null;',
  19150. ' var $ir = rtl.createIntfRefs();',
  19151. ' try {',
  19152. ' $ir.ref(1, i.GetAnt());',
  19153. ' var $with1 = $ir.ref(2, i.GetAnt());',
  19154. ' var $with2 = $ir.ref(3, $with1.GetAnt());',
  19155. ' $ir.ref(4, $with2.GetAnt());',
  19156. ' } finally {',
  19157. ' $ir.free();',
  19158. ' };',
  19159. '};',
  19160. '']),
  19161. LinesToStr([ // $mod.$main
  19162. '']));
  19163. end;
  19164. procedure TTestModule.TestClassInterface_COM_ForIn;
  19165. begin
  19166. StartProgram(false);
  19167. Add([
  19168. '{$interfaces com}',
  19169. 'type',
  19170. ' IUnknown = interface end;',
  19171. ' TObject = class',
  19172. ' Id: longint;',
  19173. ' end;',
  19174. ' IEnumerator = interface(IUnknown)',
  19175. ' function GetCurrent: TObject;',
  19176. ' function MoveNext: Boolean;',
  19177. ' property Current: TObject read GetCurrent;',
  19178. ' end;',
  19179. ' IEnumerable = interface(IUnknown)',
  19180. ' function GetEnumerator: IEnumerator;',
  19181. ' end;',
  19182. 'var',
  19183. ' o: TObject;',
  19184. ' i: IEnumerable;',
  19185. 'begin',
  19186. ' for o in i do o.Id:=3;',
  19187. '']);
  19188. ConvertProgram;
  19189. CheckSource('TestClassInterface_COM_ForIn',
  19190. LinesToStr([ // statements
  19191. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19192. 'rtl.createClass($mod, "TObject", null, function () {',
  19193. ' this.$init = function () {',
  19194. ' this.Id = 0;',
  19195. ' };',
  19196. ' this.$final = function () {',
  19197. ' };',
  19198. '});',
  19199. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  19200. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  19201. 'this.o = null;',
  19202. 'this.i = null;',
  19203. '']),
  19204. LinesToStr([ // $mod.$main
  19205. 'var $in1 = $mod.i.GetEnumerator();',
  19206. 'try {',
  19207. ' while ($in1.MoveNext()) {',
  19208. ' $mod.o = $in1.GetCurrent();',
  19209. ' $mod.o.Id = 3;',
  19210. ' }',
  19211. '} finally {',
  19212. ' rtl._Release($in1)',
  19213. '};',
  19214. '']));
  19215. end;
  19216. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  19217. begin
  19218. StartProgram(false);
  19219. Add([
  19220. '{$interfaces com}',
  19221. 'type',
  19222. ' IUnknown = interface',
  19223. ' function _AddRef: longint;',
  19224. ' function _Release: longint;',
  19225. ' end;',
  19226. ' TObject = class',
  19227. ' end;',
  19228. ' TArrOfIntf = array of IUnknown;',
  19229. 'begin',
  19230. '']);
  19231. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  19232. ConvertProgram;
  19233. end;
  19234. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  19235. begin
  19236. StartProgram(false);
  19237. Add([
  19238. '{$interfaces com}',
  19239. 'type',
  19240. ' IUnknown = interface',
  19241. ' function _AddRef: longint;',
  19242. ' function _Release: longint;',
  19243. ' end;',
  19244. ' TRec = record',
  19245. ' i: IUnknown;',
  19246. ' end;',
  19247. 'begin',
  19248. '']);
  19249. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  19250. ConvertProgram;
  19251. end;
  19252. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  19253. begin
  19254. StartUnit(false);
  19255. Add([
  19256. '{$interfaces com}',
  19257. 'interface',
  19258. 'implementation',
  19259. 'type',
  19260. ' IUnknown = interface',
  19261. ' function _AddRef: longint;',
  19262. ' end;',
  19263. ' TObject = class(IUnknown)',
  19264. ' function _AddRef: longint;',
  19265. ' end;',
  19266. 'function TObject._AddRef: longint; begin end;',
  19267. 'var i: IUnknown;',
  19268. ' o: TObject;',
  19269. 'initialization',
  19270. ' i:=nil;',
  19271. ' i:=i;',
  19272. ' i:=o;',
  19273. ' if (o as IUnknown)=nil then ;',
  19274. '']);
  19275. ConvertUnit;
  19276. CheckSource('TestClassInterface_COM_UnitInitialization',
  19277. LinesToStr([ // statements
  19278. 'var $impl = $mod.$impl;',
  19279. '']),
  19280. LinesToStr([ // this.$init
  19281. 'var $ir = rtl.createIntfRefs();',
  19282. 'try {',
  19283. ' rtl.setIntfP($impl, "i", null);',
  19284. ' rtl.setIntfP($impl, "i", $impl.i);',
  19285. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  19286. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  19287. '} finally {',
  19288. ' $ir.free();',
  19289. '};',
  19290. '']),
  19291. LinesToStr([ // implementation
  19292. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  19293. 'rtl.createClass($impl, "TObject", null, function () {',
  19294. ' this.$init = function () {',
  19295. ' };',
  19296. ' this.$final = function () {',
  19297. ' };',
  19298. ' this._AddRef = function () {',
  19299. ' var Result = 0;',
  19300. ' return Result;',
  19301. ' };',
  19302. ' rtl.addIntf(this, $impl.IUnknown);',
  19303. '});',
  19304. '$impl.i = null;',
  19305. '$impl.o = null;',
  19306. ''])
  19307. );
  19308. end;
  19309. procedure TTestModule.TestClassInterface_GUID;
  19310. begin
  19311. StartProgram(false);
  19312. Add([
  19313. '{$interfaces corba}',
  19314. 'type',
  19315. ' IUnknown = interface',
  19316. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19317. ' end;',
  19318. ' TObject = class end;',
  19319. ' TGUID = record D1, D2, D3, D4: word; end;',
  19320. ' TAliasGUID = TGUID;',
  19321. ' TGUIDString = type string;',
  19322. ' TAliasGUIDString = TGUIDString;',
  19323. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  19324. 'begin end;',
  19325. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  19326. 'begin end;',
  19327. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  19328. 'begin end;',
  19329. 'var',
  19330. ' i: IUnknown;',
  19331. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  19332. ' s: TAliasGUIDString;',
  19333. 'begin',
  19334. ' DoConstGUIDIt(IUnknown);',
  19335. ' DoDefGUID(IUnknown);',
  19336. ' DoStr(IUnknown);',
  19337. ' DoConstGUIDIt(i);',
  19338. ' DoDefGUID(i);',
  19339. ' DoStr(i);',
  19340. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19341. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19342. ' DoStr(g);',
  19343. ' g:=i;',
  19344. ' g:=IUnknown;',
  19345. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19346. ' s:=i;',
  19347. ' s:=IUnknown;',
  19348. ' s:=g;',
  19349. ' if g=i then ;',
  19350. ' if i=g then ;',
  19351. ' if g=IUnknown then ;',
  19352. ' if IUnknown=g then ;',
  19353. ' if s=i then ;',
  19354. ' if i=s then ;',
  19355. ' if s=IUnknown then ;',
  19356. ' if IUnknown=s then ;',
  19357. ' if s=g then ;',
  19358. ' if g=s then ;',
  19359. '']);
  19360. ConvertProgram;
  19361. CheckSource('TestClassInterface_GUID',
  19362. LinesToStr([ // statements
  19363. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19364. 'rtl.createClass($mod, "TObject", null, function () {',
  19365. ' this.$init = function () {',
  19366. ' };',
  19367. ' this.$final = function () {',
  19368. ' };',
  19369. '});',
  19370. 'rtl.recNewT($mod, "TGUID", function () {',
  19371. ' this.D1 = 0;',
  19372. ' this.D2 = 0;',
  19373. ' this.D3 = 0;',
  19374. ' this.D4 = 0;',
  19375. ' this.$eq = function (b) {',
  19376. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19377. ' };',
  19378. ' this.$assign = function (s) {',
  19379. ' this.D1 = s.D1;',
  19380. ' this.D2 = s.D2;',
  19381. ' this.D3 = s.D3;',
  19382. ' this.D4 = s.D4;',
  19383. ' return this;',
  19384. ' };',
  19385. '});',
  19386. 'this.DoConstGUIDIt = function (g) {',
  19387. '};',
  19388. 'this.DoDefGUID = function (g) {',
  19389. '};',
  19390. 'this.DoStr = function (s) {',
  19391. '};',
  19392. 'this.i = null;',
  19393. 'this.g = $mod.TGUID.$clone({',
  19394. ' D1: 0xD91C9AF4,',
  19395. ' D2: 0x3C93,',
  19396. ' D3: 0x420F,',
  19397. ' D4: [',
  19398. ' 0xA3,',
  19399. ' 0x03,',
  19400. ' 0xBF,',
  19401. ' 0x5B,',
  19402. ' 0xA8,',
  19403. ' 0x2B,',
  19404. ' 0xFD,',
  19405. ' 0x23',
  19406. ' ]',
  19407. '});',
  19408. 'this.s = "";',
  19409. '']),
  19410. LinesToStr([ // $mod.$main
  19411. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  19412. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  19413. '$mod.DoStr($mod.IUnknown.$guid);',
  19414. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  19415. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  19416. '$mod.DoStr($mod.i.$guid);',
  19417. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19418. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19419. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  19420. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  19421. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  19422. '$mod.g.$assign({',
  19423. ' D1: 0xD91C9AF4,',
  19424. ' D2: 0x3C93,',
  19425. ' D3: 0x420F,',
  19426. ' D4: [',
  19427. ' 0xA3,',
  19428. ' 0x03,',
  19429. ' 0xBF,',
  19430. ' 0x5B,',
  19431. ' 0xA8,',
  19432. ' 0x2B,',
  19433. ' 0xFD,',
  19434. ' 0x23',
  19435. ' ]',
  19436. '});',
  19437. '$mod.s = $mod.i.$guid;',
  19438. '$mod.s = $mod.IUnknown.$guid;',
  19439. '$mod.s = rtl.guidrToStr($mod.g);',
  19440. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19441. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19442. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19443. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19444. 'if ($mod.s === $mod.i.$guid) ;',
  19445. 'if ($mod.i.$guid === $mod.s) ;',
  19446. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  19447. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  19448. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19449. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19450. '']));
  19451. end;
  19452. procedure TTestModule.TestClassInterface_GUIDProperty;
  19453. begin
  19454. StartProgram(false);
  19455. Add([
  19456. '{$interfaces corba}',
  19457. 'type',
  19458. ' IUnknown = interface',
  19459. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19460. ' end;',
  19461. ' TGUID = record D1, D2, D3, D4: word; end;',
  19462. ' TAliasGUID = TGUID;',
  19463. ' TGUIDString = type string;',
  19464. ' TAliasGUIDString = TGUIDString;',
  19465. ' TObject = class',
  19466. ' function GetG: TAliasGUID; virtual; abstract;',
  19467. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  19468. ' function GetS: TAliasGUIDString; virtual; abstract;',
  19469. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  19470. ' property g: TAliasGUID read GetG write SetG;',
  19471. ' property s: TAliasGUIDString read GetS write SetS;',
  19472. ' end;',
  19473. 'var o: TObject;',
  19474. 'begin',
  19475. ' o.g:=IUnknown;',
  19476. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19477. ' o.s:=IUnknown;',
  19478. ' o.s:=o.g;',
  19479. '']);
  19480. ConvertProgram;
  19481. CheckSource('TestClassInterface_GUIDProperty',
  19482. LinesToStr([ // statements
  19483. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19484. 'rtl.recNewT($mod, "TGUID", function () {',
  19485. ' this.D1 = 0;',
  19486. ' this.D2 = 0;',
  19487. ' this.D3 = 0;',
  19488. ' this.D4 = 0;',
  19489. ' this.$eq = function (b) {',
  19490. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19491. ' };',
  19492. ' this.$assign = function (s) {',
  19493. ' this.D1 = s.D1;',
  19494. ' this.D2 = s.D2;',
  19495. ' this.D3 = s.D3;',
  19496. ' this.D4 = s.D4;',
  19497. ' return this;',
  19498. ' };',
  19499. '});',
  19500. 'rtl.createClass($mod, "TObject", null, function () {',
  19501. ' this.$init = function () {',
  19502. ' };',
  19503. ' this.$final = function () {',
  19504. ' };',
  19505. '});',
  19506. 'this.o = null;',
  19507. '']),
  19508. LinesToStr([ // $mod.$main
  19509. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  19510. '$mod.o.SetG({',
  19511. ' D1: 0xD91C9AF4,',
  19512. ' D2: 0x3C93,',
  19513. ' D3: 0x420F,',
  19514. ' D4: [',
  19515. ' 0xA3,',
  19516. ' 0x03,',
  19517. ' 0xBF,',
  19518. ' 0x5B,',
  19519. ' 0xA8,',
  19520. ' 0x2B,',
  19521. ' 0xFD,',
  19522. ' 0x23',
  19523. ' ]',
  19524. '});',
  19525. '$mod.o.SetS($mod.IUnknown.$guid);',
  19526. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  19527. '']));
  19528. end;
  19529. procedure TTestModule.TestClassHelper_ClassVar;
  19530. begin
  19531. StartProgram(false);
  19532. Add([
  19533. 'type',
  19534. ' TObject = class',
  19535. ' end;',
  19536. ' THelper = class helper for TObject',
  19537. ' const',
  19538. ' One = 1;',
  19539. ' Two: word = 2;',
  19540. ' class var',
  19541. ' Glob: word;',
  19542. ' function Foo(w: word): word;',
  19543. ' class function Bar(w: word): word;',
  19544. ' end;',
  19545. 'function THelper.foo(w: word): word;',
  19546. 'begin',
  19547. ' Result:=w;',
  19548. ' Two:=One+w;',
  19549. ' Glob:=Glob;',
  19550. ' Result:=Self.Glob;',
  19551. ' Self.Glob:=Self.Glob;',
  19552. ' with Self do Glob:=Glob;',
  19553. 'end;',
  19554. 'class function THelper.bar(w: word): word;',
  19555. 'begin',
  19556. ' Result:=w;',
  19557. ' Two:=One;',
  19558. ' Glob:=Glob;',
  19559. ' Self.Glob:=Self.Glob;',
  19560. ' with Self do Glob:=Glob;',
  19561. 'end;',
  19562. 'var o: TObject;',
  19563. 'begin',
  19564. ' tobject.two:=tobject.one;',
  19565. ' tobject.Glob:=tobject.Glob;',
  19566. ' with tobject do begin',
  19567. ' two:=one;',
  19568. ' Glob:=Glob;',
  19569. ' end;',
  19570. ' o.two:=o.one;',
  19571. ' o.Glob:=o.Glob;',
  19572. ' with o do begin',
  19573. ' two:=one;',
  19574. ' Glob:=Glob;',
  19575. ' end;',
  19576. '']);
  19577. ConvertProgram;
  19578. CheckSource('TestClassHelper_ClassVar',
  19579. LinesToStr([ // statements
  19580. 'rtl.createClass($mod, "TObject", null, function () {',
  19581. ' this.$init = function () {',
  19582. ' };',
  19583. ' this.$final = function () {',
  19584. ' };',
  19585. '});',
  19586. 'rtl.createHelper($mod, "THelper", null, function () {',
  19587. ' this.One = 1;',
  19588. ' this.Two = 2;',
  19589. ' this.Glob = 0;',
  19590. ' this.Foo = function (w) {',
  19591. ' var Result = 0;',
  19592. ' Result = w;',
  19593. ' $mod.THelper.Two = 1 + w;',
  19594. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19595. ' Result = $mod.THelper.Glob;',
  19596. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19597. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19598. ' return Result;',
  19599. ' };',
  19600. ' this.Bar = function (w) {',
  19601. ' var Result = 0;',
  19602. ' Result = w;',
  19603. ' $mod.THelper.Two = 1;',
  19604. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19605. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19606. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19607. ' return Result;',
  19608. ' };',
  19609. '});',
  19610. 'this.o = null;',
  19611. '']),
  19612. LinesToStr([ // $mod.$main
  19613. '$mod.THelper.Two = 1;',
  19614. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19615. 'var $with1 = $mod.TObject;',
  19616. '$mod.THelper.Two = 1;',
  19617. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19618. '$mod.THelper.Two = 1;',
  19619. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19620. 'var $with2 = $mod.o;',
  19621. '$mod.THelper.Two = 1;',
  19622. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19623. '']));
  19624. end;
  19625. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  19626. begin
  19627. StartProgram(false);
  19628. Add([
  19629. 'type',
  19630. ' TObject = class',
  19631. ' FSize: word;',
  19632. ' property Size: word read FSize write FSize;',
  19633. ' end;',
  19634. ' THelper = class helper for TObject',
  19635. ' function Foo(w: word = 1): word;',
  19636. ' end;',
  19637. 'function THelper.foo(w: word): word;',
  19638. 'begin',
  19639. ' Result:=Size;',
  19640. ' Size:=Size+2;',
  19641. ' Self.Size:=Self.Size+3;',
  19642. ' FSize:=FSize+4;',
  19643. ' Self.FSize:=Self.FSize+5;',
  19644. ' with Self do begin',
  19645. ' Size:=Size+6;',
  19646. ' FSize:=FSize+7;',
  19647. ' FSize:=FSize+8;',
  19648. ' end;',
  19649. 'end;',
  19650. 'begin',
  19651. '']);
  19652. ConvertProgram;
  19653. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  19654. LinesToStr([ // statements
  19655. 'rtl.createClass($mod, "TObject", null, function () {',
  19656. ' this.$init = function () {',
  19657. ' this.FSize = 0;',
  19658. ' };',
  19659. ' this.$final = function () {',
  19660. ' };',
  19661. '});',
  19662. 'rtl.createHelper($mod, "THelper", null, function () {',
  19663. ' this.Foo = function (w) {',
  19664. ' var Result = 0;',
  19665. ' Result = this.FSize;',
  19666. ' this.FSize = this.FSize + 2;',
  19667. ' this.FSize = this.FSize + 3;',
  19668. ' this.FSize = this.FSize + 4;',
  19669. ' this.FSize = this.FSize + 5;',
  19670. ' this.FSize = this.FSize + 6;',
  19671. ' this.FSize = this.FSize + 7;',
  19672. ' this.FSize = this.FSize + 8;',
  19673. ' return Result;',
  19674. ' };',
  19675. '});',
  19676. '']),
  19677. LinesToStr([ // $mod.$main
  19678. '']));
  19679. end;
  19680. procedure TTestModule.TestClassHelper_Method_Call;
  19681. begin
  19682. StartProgram(false);
  19683. Add([
  19684. 'type',
  19685. ' TObject = class',
  19686. ' procedure Run(w: word = 10);',
  19687. ' end;',
  19688. ' THelper = class helper for TObject',
  19689. ' function Foo(w: word = 1): word;',
  19690. ' end;',
  19691. 'procedure TObject.Run(w: word);',
  19692. 'var o: TObject;',
  19693. 'begin',
  19694. ' Foo;',
  19695. ' Foo();',
  19696. ' Foo(2);',
  19697. ' Self.Foo;',
  19698. ' Self.Foo();',
  19699. ' Self.Foo(3);',
  19700. ' with Self do begin',
  19701. ' Foo;',
  19702. ' Foo();',
  19703. ' Foo(4);',
  19704. ' end;',
  19705. ' with o do Foo(5);',
  19706. 'end;',
  19707. 'function THelper.foo(w: word): word;',
  19708. 'begin',
  19709. ' Run;',
  19710. ' Run();',
  19711. ' Run(11);',
  19712. ' Foo;',
  19713. ' Foo();',
  19714. ' Foo(12);',
  19715. ' Self.Foo;',
  19716. ' Self.Foo();',
  19717. ' Self.Foo(13);',
  19718. ' with Self do begin',
  19719. ' Foo;',
  19720. ' Foo();',
  19721. ' Foo(14);',
  19722. ' end;',
  19723. 'end;',
  19724. 'var Obj: TObject;',
  19725. 'begin',
  19726. ' obj.Foo;',
  19727. ' obj.Foo();',
  19728. ' obj.Foo(21);',
  19729. ' with obj do begin',
  19730. ' Foo;',
  19731. ' Foo();',
  19732. ' Foo(22);',
  19733. ' end;',
  19734. '']);
  19735. ConvertProgram;
  19736. CheckSource('TestClassHelper_Method_Call',
  19737. LinesToStr([ // statements
  19738. 'rtl.createClass($mod, "TObject", null, function () {',
  19739. ' this.$init = function () {',
  19740. ' };',
  19741. ' this.$final = function () {',
  19742. ' };',
  19743. ' this.Run = function (w) {',
  19744. ' var o = null;',
  19745. ' $mod.THelper.Foo.call(this, 1);',
  19746. ' $mod.THelper.Foo.call(this, 1);',
  19747. ' $mod.THelper.Foo.call(this, 2);',
  19748. ' $mod.THelper.Foo.call(this, 1);',
  19749. ' $mod.THelper.Foo.call(this, 1);',
  19750. ' $mod.THelper.Foo.call(this, 3);',
  19751. ' $mod.THelper.Foo.call(this, 1);',
  19752. ' $mod.THelper.Foo.call(this, 1);',
  19753. ' $mod.THelper.Foo.call(this, 4);',
  19754. ' $mod.THelper.Foo.call(o, 5);',
  19755. ' };',
  19756. '});',
  19757. 'rtl.createHelper($mod, "THelper", null, function () {',
  19758. ' this.Foo = function (w) {',
  19759. ' var Result = 0;',
  19760. ' this.Run(10);',
  19761. ' this.Run(10);',
  19762. ' this.Run(11);',
  19763. ' $mod.THelper.Foo.call(this, 1);',
  19764. ' $mod.THelper.Foo.call(this, 1);',
  19765. ' $mod.THelper.Foo.call(this, 12);',
  19766. ' $mod.THelper.Foo.call(this, 1);',
  19767. ' $mod.THelper.Foo.call(this, 1);',
  19768. ' $mod.THelper.Foo.call(this, 13);',
  19769. ' $mod.THelper.Foo.call(this, 1);',
  19770. ' $mod.THelper.Foo.call(this, 1);',
  19771. ' $mod.THelper.Foo.call(this, 14);',
  19772. ' return Result;',
  19773. ' };',
  19774. '});',
  19775. 'this.Obj = null;',
  19776. '']),
  19777. LinesToStr([ // $mod.$main
  19778. '$mod.THelper.Foo.call($mod.Obj, 1);',
  19779. '$mod.THelper.Foo.call($mod.Obj, 1);',
  19780. '$mod.THelper.Foo.call($mod.Obj, 21);',
  19781. 'var $with1 = $mod.Obj;',
  19782. '$mod.THelper.Foo.call($with1, 1);',
  19783. '$mod.THelper.Foo.call($with1, 1);',
  19784. '$mod.THelper.Foo.call($with1, 22);',
  19785. '']));
  19786. end;
  19787. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  19788. begin
  19789. StartProgram(false);
  19790. Add([
  19791. 'type',
  19792. ' TObject = class',
  19793. ' procedure Run(w: word = 10);',
  19794. ' end;',
  19795. ' THelper = class helper for TObject',
  19796. ' function Foo(w: word = 1): word;',
  19797. ' end;',
  19798. 'procedure TObject.Run(w: word);',
  19799. ' procedure Sub(Self: TObject);',
  19800. ' begin',
  19801. ' Foo;',
  19802. ' Foo();',
  19803. ' Self.Foo;',
  19804. ' Self.Foo();',
  19805. ' with Self do begin',
  19806. ' Foo;',
  19807. ' Foo();',
  19808. ' end;',
  19809. ' end;',
  19810. 'begin',
  19811. 'end;',
  19812. 'function THelper.foo(w: word): word;',
  19813. ' procedure Sub(Self: TObject);',
  19814. ' begin',
  19815. ' Run;',
  19816. ' Run();',
  19817. ' Foo;',
  19818. ' Foo();',
  19819. ' Self.Foo;',
  19820. ' Self.Foo();',
  19821. ' with Self do begin',
  19822. ' Foo;',
  19823. ' Foo();',
  19824. ' end;',
  19825. ' end;',
  19826. 'begin',
  19827. 'end;',
  19828. 'begin',
  19829. '']);
  19830. ConvertProgram;
  19831. CheckSource('TestClassHelper_Method_Nested_Call',
  19832. LinesToStr([ // statements
  19833. 'rtl.createClass($mod, "TObject", null, function () {',
  19834. ' this.$init = function () {',
  19835. ' };',
  19836. ' this.$final = function () {',
  19837. ' };',
  19838. ' this.Run = function (w) {',
  19839. ' var $Self = this;',
  19840. ' function Sub(Self) {',
  19841. ' $mod.THelper.Foo.call($Self, 1);',
  19842. ' $mod.THelper.Foo.call($Self, 1);',
  19843. ' $mod.THelper.Foo.call(Self, 1);',
  19844. ' $mod.THelper.Foo.call(Self, 1);',
  19845. ' $mod.THelper.Foo.call(Self, 1);',
  19846. ' $mod.THelper.Foo.call(Self, 1);',
  19847. ' };',
  19848. ' };',
  19849. '});',
  19850. 'rtl.createHelper($mod, "THelper", null, function () {',
  19851. ' this.Foo = function (w) {',
  19852. ' var $Self = this;',
  19853. ' var Result = 0;',
  19854. ' function Sub(Self) {',
  19855. ' $Self.Run(10);',
  19856. ' $Self.Run(10);',
  19857. ' $mod.THelper.Foo.call($Self, 1);',
  19858. ' $mod.THelper.Foo.call($Self, 1);',
  19859. ' $mod.THelper.Foo.call(Self, 1);',
  19860. ' $mod.THelper.Foo.call(Self, 1);',
  19861. ' $mod.THelper.Foo.call(Self, 1);',
  19862. ' $mod.THelper.Foo.call(Self, 1);',
  19863. ' };',
  19864. ' return Result;',
  19865. ' };',
  19866. '});',
  19867. '']),
  19868. LinesToStr([ // $mod.$main
  19869. '']));
  19870. end;
  19871. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  19872. begin
  19873. StartProgram(false);
  19874. Add([
  19875. 'type',
  19876. ' TObject = class',
  19877. ' class procedure Run(w: word = 10);',
  19878. ' end;',
  19879. ' THelper = class helper for TObject',
  19880. ' class function Foo(w: word = 1): word;',
  19881. ' end;',
  19882. 'class procedure TObject.Run(w: word);',
  19883. 'begin',
  19884. ' Foo;',
  19885. ' Foo();',
  19886. ' Self.Foo;',
  19887. ' Self.Foo();',
  19888. ' with Self do begin',
  19889. ' Foo;',
  19890. ' Foo();',
  19891. ' end;',
  19892. 'end;',
  19893. 'class function THelper.foo(w: word): word;',
  19894. 'begin',
  19895. ' Run;',
  19896. ' Run();',
  19897. ' Foo;',
  19898. ' Foo();',
  19899. ' Self.Foo;',
  19900. ' Self.Foo();',
  19901. ' with Self do begin',
  19902. ' Foo;',
  19903. ' Foo();',
  19904. ' end;',
  19905. 'end;',
  19906. 'var',
  19907. ' Obj: TObject;',
  19908. 'begin',
  19909. ' obj.Foo;',
  19910. ' obj.Foo();',
  19911. ' with obj do begin',
  19912. ' Foo;',
  19913. ' Foo();',
  19914. ' end;',
  19915. ' tobject.Foo;',
  19916. ' tobject.Foo();',
  19917. ' with tobject do begin',
  19918. ' Foo;',
  19919. ' Foo();',
  19920. ' end;',
  19921. '']);
  19922. ConvertProgram;
  19923. CheckSource('TestClassHelper_ClassMethod_Call',
  19924. LinesToStr([ // statements
  19925. 'rtl.createClass($mod, "TObject", null, function () {',
  19926. ' this.$init = function () {',
  19927. ' };',
  19928. ' this.$final = function () {',
  19929. ' };',
  19930. ' this.Run = function (w) {',
  19931. ' $mod.THelper.Foo.call(this, 1);',
  19932. ' $mod.THelper.Foo.call(this, 1);',
  19933. ' $mod.THelper.Foo.call(this, 1);',
  19934. ' $mod.THelper.Foo.call(this, 1);',
  19935. ' $mod.THelper.Foo.call(this, 1);',
  19936. ' $mod.THelper.Foo.call(this, 1);',
  19937. ' };',
  19938. '});',
  19939. 'rtl.createHelper($mod, "THelper", null, function () {',
  19940. ' this.Foo = function (w) {',
  19941. ' var Result = 0;',
  19942. ' this.Run(10);',
  19943. ' this.Run(10);',
  19944. ' $mod.THelper.Foo.call(this, 1);',
  19945. ' $mod.THelper.Foo.call(this, 1);',
  19946. ' $mod.THelper.Foo.call(this, 1);',
  19947. ' $mod.THelper.Foo.call(this, 1);',
  19948. ' $mod.THelper.Foo.call(this, 1);',
  19949. ' $mod.THelper.Foo.call(this, 1);',
  19950. ' return Result;',
  19951. ' };',
  19952. '});',
  19953. 'this.Obj = null;',
  19954. '']),
  19955. LinesToStr([ // $mod.$main
  19956. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19957. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19958. 'var $with1 = $mod.Obj;',
  19959. '$mod.THelper.Foo.call($with1.$class, 1);',
  19960. '$mod.THelper.Foo.call($with1.$class, 1);',
  19961. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19962. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19963. 'var $with2 = $mod.TObject;',
  19964. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19965. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19966. '']));
  19967. end;
  19968. procedure TTestModule.TestClassHelper_ClassOf;
  19969. begin
  19970. StartProgram(false);
  19971. Add([
  19972. 'type',
  19973. ' TObject = class',
  19974. ' end;',
  19975. ' TClass = class of TObject;',
  19976. ' THelper = class helper for TObject',
  19977. ' class function Foo(w: word = 1): word;',
  19978. ' end;',
  19979. 'class function THelper.foo(w: word): word;',
  19980. 'begin',
  19981. 'end;',
  19982. 'var',
  19983. ' c: TClass;',
  19984. 'begin',
  19985. ' c.Foo;',
  19986. ' c.Foo();',
  19987. ' with c do begin',
  19988. ' Foo;',
  19989. ' Foo();',
  19990. ' end;',
  19991. '']);
  19992. ConvertProgram;
  19993. CheckSource('TestClassHelper_ClassOf',
  19994. LinesToStr([ // statements
  19995. 'rtl.createClass($mod, "TObject", null, function () {',
  19996. ' this.$init = function () {',
  19997. ' };',
  19998. ' this.$final = function () {',
  19999. ' };',
  20000. '});',
  20001. 'rtl.createHelper($mod, "THelper", null, function () {',
  20002. ' this.Foo = function (w) {',
  20003. ' var Result = 0;',
  20004. ' return Result;',
  20005. ' };',
  20006. '});',
  20007. 'this.c = null;',
  20008. '']),
  20009. LinesToStr([ // $mod.$main
  20010. '$mod.THelper.Foo.call($mod.c, 1);',
  20011. '$mod.THelper.Foo.call($mod.c, 1);',
  20012. 'var $with1 = $mod.c;',
  20013. '$mod.THelper.Foo.call($with1, 1);',
  20014. '$mod.THelper.Foo.call($with1, 1);',
  20015. '']));
  20016. end;
  20017. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  20018. begin
  20019. StartProgram(false);
  20020. Add([
  20021. '{$mode objfpc}',
  20022. 'type',
  20023. ' TObject = class',
  20024. ' procedure DoIt;',
  20025. ' end;',
  20026. ' THelper = class helper for TObject',
  20027. ' procedure Fly(w: word = 1);',
  20028. ' class procedure Glide(w: word = 1);',
  20029. ' class procedure Run(w: word = 1); static;',
  20030. ' end;',
  20031. ' TFly = procedure(w: word) of object;',
  20032. ' TGlide = TFly;',
  20033. ' TRun = procedure(w: word);',
  20034. 'var',
  20035. ' f: TFly;',
  20036. ' g: TGlide;',
  20037. ' r: TRun;',
  20038. 'procedure TObject.DoIt;',
  20039. 'begin',
  20040. ' f:=@fly;',
  20041. ' g:=@glide;',
  20042. ' r:=@run;',
  20043. ' f:[email protected];',
  20044. ' g:[email protected];',
  20045. ' r:[email protected];',
  20046. ' with self do begin',
  20047. ' f:=@fly;',
  20048. ' g:=@glide;',
  20049. ' r:=@run;',
  20050. ' end;',
  20051. 'end;',
  20052. 'procedure THelper.fly(w: word);',
  20053. 'begin',
  20054. ' f:=@fly;',
  20055. ' g:=@glide;',
  20056. ' r:=@run;',
  20057. 'end;',
  20058. 'class procedure THelper.glide(w: word);',
  20059. 'begin',
  20060. ' g:=@glide;',
  20061. ' r:=@run;',
  20062. 'end;',
  20063. 'class procedure THelper.run(w: word);',
  20064. 'begin',
  20065. ' g:=@glide;',
  20066. ' r:=@run;',
  20067. 'end;',
  20068. 'var',
  20069. ' Obj: TObject;',
  20070. 'begin',
  20071. ' f:[email protected];',
  20072. ' g:[email protected];',
  20073. ' r:[email protected];',
  20074. ' with obj do begin',
  20075. ' f:=@fly;',
  20076. ' g:=@glide;',
  20077. ' r:=@run;',
  20078. ' end;',
  20079. ' g:[email protected];',
  20080. ' r:[email protected];',
  20081. ' with tobject do begin',
  20082. ' g:=@glide;',
  20083. ' r:=@run;',
  20084. ' end;',
  20085. '']);
  20086. ConvertProgram;
  20087. CheckSource('TestClassHelper_MethodRefObjFPC',
  20088. LinesToStr([ // statements
  20089. 'rtl.createClass($mod, "TObject", null, function () {',
  20090. ' this.$init = function () {',
  20091. ' };',
  20092. ' this.$final = function () {',
  20093. ' };',
  20094. ' this.DoIt = function () {',
  20095. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20096. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20097. ' $mod.r = $mod.THelper.Run;',
  20098. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20099. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20100. ' $mod.r = $mod.THelper.Run;',
  20101. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20102. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20103. ' $mod.r = $mod.THelper.Run;',
  20104. ' };',
  20105. '});',
  20106. 'rtl.createHelper($mod, "THelper", null, function () {',
  20107. ' this.Fly = function (w) {',
  20108. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20109. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20110. ' $mod.r = $mod.THelper.Run;',
  20111. ' };',
  20112. ' this.Glide = function (w) {',
  20113. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  20114. ' $mod.r = $mod.THelper.Run;',
  20115. ' };',
  20116. ' this.Run = function (w) {',
  20117. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  20118. ' $mod.r = $mod.THelper.Run;',
  20119. ' };',
  20120. '});',
  20121. 'this.f = null;',
  20122. 'this.g = null;',
  20123. 'this.r = null;',
  20124. 'this.Obj = null;',
  20125. '']),
  20126. LinesToStr([ // $mod.$main
  20127. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  20128. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  20129. '$mod.r = $mod.THelper.Run;',
  20130. 'var $with1 = $mod.Obj;',
  20131. '$mod.f = rtl.createCallback($with1, $mod.THelper.Fly);',
  20132. '$mod.g = rtl.createCallback($with1.$class, $mod.THelper.Glide);',
  20133. '$mod.r = $mod.THelper.Run;',
  20134. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  20135. '$mod.r = $mod.THelper.Run;',
  20136. 'var $with2 = $mod.TObject;',
  20137. '$mod.g = rtl.createCallback($with2, $mod.THelper.Glide);',
  20138. '$mod.r = $mod.THelper.Run;',
  20139. '']));
  20140. end;
  20141. procedure TTestModule.TestClassHelper_Constructor;
  20142. begin
  20143. StartProgram(false);
  20144. Add([
  20145. 'type',
  20146. ' TObject = class',
  20147. ' constructor Create;',
  20148. ' end;',
  20149. ' TClass = class of TObject;',
  20150. ' THelper = class helper for TObject',
  20151. ' constructor NewHlp(w: word);',
  20152. ' end;',
  20153. 'var',
  20154. ' obj: TObject;',
  20155. ' c: TClass;',
  20156. 'constructor TObject.Create;',
  20157. 'begin',
  20158. ' NewHlp(2);', // normal call
  20159. ' tobject.NewHlp(3);', // new instance
  20160. ' c.newhlp(4);', // new instance
  20161. 'end;',
  20162. 'constructor THelper.NewHlp(w: word);',
  20163. 'begin',
  20164. ' create;', // normal call
  20165. ' tobject.create;', // new instance
  20166. ' NewHlp(2);', // normal call
  20167. ' tobject.NewHlp(3);', // new instance
  20168. ' c.newhlp(4);', // new instance
  20169. 'end;',
  20170. 'begin',
  20171. ' obj.newhlp(2);', // normal call
  20172. ' with Obj do newhlp(12);', // normal call
  20173. ' tobject.newhlp(3);', // new instance
  20174. ' with tobject do newhlp(13);', // new instance
  20175. ' c.newhlp(4);', // new instance
  20176. ' with c do newhlp(14);', // new instance
  20177. '']);
  20178. ConvertProgram;
  20179. CheckSource('TestClassHelper_Constructor',
  20180. LinesToStr([ // statements
  20181. 'rtl.createClass($mod, "TObject", null, function () {',
  20182. ' this.$init = function () {',
  20183. ' };',
  20184. ' this.$final = function () {',
  20185. ' };',
  20186. ' this.Create = function () {',
  20187. ' $mod.THelper.NewHlp.call(this, 2);',
  20188. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20189. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20190. ' return this;',
  20191. ' };',
  20192. '});',
  20193. 'rtl.createHelper($mod, "THelper", null, function () {',
  20194. ' this.NewHlp = function (w) {',
  20195. ' this.Create();',
  20196. ' $mod.TObject.$create("Create");',
  20197. ' $mod.THelper.NewHlp.call(this, 2);',
  20198. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20199. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20200. ' return this;',
  20201. ' };',
  20202. '});',
  20203. 'this.obj = null;',
  20204. 'this.c = null;',
  20205. '']),
  20206. LinesToStr([ // $mod.$main
  20207. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  20208. 'var $with1 = $mod.obj;',
  20209. '$mod.THelper.NewHlp.call($with1, 12);',
  20210. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20211. 'var $with2 = $mod.TObject;',
  20212. '$with2.$create($mod.THelper.NewHlp, [13]);',
  20213. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  20214. 'var $with3 = $mod.c;',
  20215. '$with3.$create($mod.THelper.NewHlp, [14]);',
  20216. '']));
  20217. end;
  20218. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  20219. begin
  20220. StartProgram(false);
  20221. Add([
  20222. 'type',
  20223. ' TObject = class',
  20224. ' procedure Fly;',
  20225. ' end;',
  20226. ' TObjHelper = class helper for TObject',
  20227. ' procedure Fly;',
  20228. ' end;',
  20229. ' TBird = class',
  20230. ' procedure Fly;',
  20231. ' end;',
  20232. ' TBirdHelper = class helper for TBird',
  20233. ' procedure Fly;',
  20234. ' procedure Walk(w: word);',
  20235. ' end;',
  20236. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  20237. ' procedure Fly;',
  20238. ' procedure Walk(w: word);',
  20239. ' end;',
  20240. 'procedure Tobject.fly;',
  20241. 'begin',
  20242. ' inherited;', // ignore
  20243. 'end;',
  20244. 'procedure Tobjhelper.fly;',
  20245. 'begin',
  20246. ' {@TObject_Fly}inherited;',
  20247. ' inherited {@TObject_Fly}Fly;',
  20248. 'end;',
  20249. 'procedure Tbird.fly;',
  20250. 'begin',
  20251. ' {@TObjHelper_Fly}inherited;',
  20252. ' inherited {@TObjHelper_Fly}Fly;',
  20253. 'end;',
  20254. 'procedure Tbirdhelper.fly;',
  20255. 'begin',
  20256. ' {@TBird_Fly}inherited;',
  20257. ' inherited {@TBird_Fly}Fly;',
  20258. 'end;',
  20259. 'procedure Tbirdhelper.walk(w: word);',
  20260. 'begin',
  20261. 'end;',
  20262. 'procedure teagleHelper.fly;',
  20263. 'begin',
  20264. ' {@TBird_Fly}inherited;',
  20265. ' inherited {@TBird_Fly}Fly;',
  20266. 'end;',
  20267. 'procedure teagleHelper.walk(w: word);',
  20268. 'begin',
  20269. ' {@TBirdHelper_Walk}inherited;',
  20270. ' inherited {@TBirdHelper_Walk}Walk(3);',
  20271. 'end;',
  20272. 'begin',
  20273. '']);
  20274. ConvertProgram;
  20275. CheckSource('TestClassHelper_InheritedObjFPC',
  20276. LinesToStr([ // statements
  20277. 'rtl.createClass($mod, "TObject", null, function () {',
  20278. ' this.$init = function () {',
  20279. ' };',
  20280. ' this.$final = function () {',
  20281. ' };',
  20282. ' this.Fly = function () {',
  20283. ' };',
  20284. '});',
  20285. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20286. ' this.Fly = function () {',
  20287. ' $mod.TObject.Fly.call(this);',
  20288. ' $mod.TObject.Fly.call(this);',
  20289. ' };',
  20290. '});',
  20291. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20292. ' this.Fly$1 = function () {',
  20293. ' $mod.TObjHelper.Fly.call(this);',
  20294. ' $mod.TObjHelper.Fly.call(this);',
  20295. ' };',
  20296. '});',
  20297. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20298. ' this.Fly = function () {',
  20299. ' $mod.TBird.Fly$1.call(this);',
  20300. ' $mod.TBird.Fly$1.call(this);',
  20301. ' };',
  20302. ' this.Walk = function (w) {',
  20303. ' };',
  20304. '});',
  20305. 'rtl.createHelper($mod, "TEagleHelper", $mod.TBirdHelper, function () {',
  20306. ' this.Fly$1 = function () {',
  20307. ' $mod.TBird.Fly$1.call(this);',
  20308. ' $mod.TBird.Fly$1.call(this);',
  20309. ' };',
  20310. ' this.Walk$1 = function (w) {',
  20311. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  20312. ' $mod.TBirdHelper.Walk.call(this, 3);',
  20313. ' };',
  20314. '});',
  20315. '']),
  20316. LinesToStr([ // $mod.$main
  20317. '']));
  20318. end;
  20319. procedure TTestModule.TestClassHelper_Property;
  20320. begin
  20321. StartProgram(false);
  20322. Add([
  20323. 'type',
  20324. ' TObject = class',
  20325. ' FSize: word;',
  20326. ' function GetSpeed: word;',
  20327. ' procedure SetSpeed(Value: word);',
  20328. ' end;',
  20329. ' TObjHelper = class helper for TObject',
  20330. ' function GetLeft: word;',
  20331. ' procedure SetLeft(Value: word);',
  20332. ' property Size: word read FSize write FSize;',
  20333. ' property Speed: word read GetSpeed write SetSpeed;',
  20334. ' property Left: word read GetLeft write SetLeft;',
  20335. ' end;',
  20336. ' TBird = class',
  20337. ' property NotRight: word read GetLeft write SetLeft;',
  20338. ' procedure DoIt;',
  20339. ' end;',
  20340. 'var',
  20341. ' b: TBird;',
  20342. 'function Tobject.GetSpeed: word;',
  20343. 'begin',
  20344. ' Size:=Size+11;',
  20345. ' Speed:=Speed+12;',
  20346. ' Result:=Left+13;',
  20347. ' Left:=13;',
  20348. ' Left:=Left+13;',
  20349. ' Self.Size:=Self.Size+21;',
  20350. ' Self.Speed:=Self.Speed+22;',
  20351. ' Self.Left:=Self.Left+23;',
  20352. ' with Self do begin',
  20353. ' Size:=Size+31;',
  20354. ' Speed:=Speed+32;',
  20355. ' Left:=Left+33;',
  20356. ' end;',
  20357. 'end;',
  20358. 'procedure Tobject.SetSpeed(Value: word);',
  20359. 'begin',
  20360. 'end;',
  20361. 'function TObjHelper.GetLeft: word;',
  20362. 'begin',
  20363. ' Size:=Size+11;',
  20364. ' Speed:=Speed+12;',
  20365. ' Left:=Left+13;',
  20366. ' Self.Size:=Self.Size+21;',
  20367. ' Self.Speed:=Self.Speed+22;',
  20368. ' Self.Left:=Self.Left+23;',
  20369. ' with Self do begin',
  20370. ' Size:=Size+31;',
  20371. ' Speed:=Speed+32;',
  20372. ' Left:=Left+33;',
  20373. ' end;',
  20374. 'end;',
  20375. 'procedure TObjHelper.SetLeft(Value: word);',
  20376. 'begin',
  20377. 'end;',
  20378. 'procedure TBird.DoIt;',
  20379. 'begin',
  20380. ' NotRight:=NotRight+11;',
  20381. ' Self.NotRight:=Self.NotRight+21;',
  20382. ' with Self do begin',
  20383. ' NotRight:=NotRight+31;',
  20384. ' end;',
  20385. 'end;',
  20386. 'begin',
  20387. ' b.Size:=b.Size+11;',
  20388. ' b.Speed:=b.Speed+12;',
  20389. ' b.Left:=b.Left+13;',
  20390. ' b.NotRight:=b.NotRight+14;',
  20391. ' with b do begin',
  20392. ' Size:=Size+31;',
  20393. ' Speed:=Speed+32;',
  20394. ' Left:=Left+33;',
  20395. ' NotRight:=NotRight+34;',
  20396. ' end;',
  20397. '']);
  20398. ConvertProgram;
  20399. CheckSource('TestClassHelper_Property',
  20400. LinesToStr([ // statements
  20401. 'rtl.createClass($mod, "TObject", null, function () {',
  20402. ' this.$init = function () {',
  20403. ' this.FSize = 0;',
  20404. ' };',
  20405. ' this.$final = function () {',
  20406. ' };',
  20407. ' this.GetSpeed = function () {',
  20408. ' var Result = 0;',
  20409. ' this.FSize = this.FSize + 11;',
  20410. ' this.SetSpeed(this.GetSpeed() + 12);',
  20411. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  20412. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  20413. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20414. ' this.FSize = this.FSize + 21;',
  20415. ' this.SetSpeed(this.GetSpeed() + 22);',
  20416. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20417. ' this.FSize = this.FSize + 31;',
  20418. ' this.SetSpeed(this.GetSpeed() + 32);',
  20419. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20420. ' return Result;',
  20421. ' };',
  20422. ' this.SetSpeed = function (Value) {',
  20423. ' };',
  20424. '});',
  20425. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20426. ' this.GetLeft = function () {',
  20427. ' var Result = 0;',
  20428. ' this.FSize = this.FSize + 11;',
  20429. ' this.SetSpeed(this.GetSpeed() + 12);',
  20430. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20431. ' this.FSize = this.FSize + 21;',
  20432. ' this.SetSpeed(this.GetSpeed() + 22);',
  20433. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20434. ' this.FSize = this.FSize + 31;',
  20435. ' this.SetSpeed(this.GetSpeed() + 32);',
  20436. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20437. ' return Result;',
  20438. ' };',
  20439. ' this.SetLeft = function (Value) {',
  20440. ' };',
  20441. '});',
  20442. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20443. ' this.DoIt = function () {',
  20444. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  20445. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  20446. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  20447. ' };',
  20448. '});',
  20449. 'this.b = null;',
  20450. '']),
  20451. LinesToStr([ // $mod.$main
  20452. '$mod.b.FSize = $mod.b.FSize + 11;',
  20453. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20454. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  20455. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  20456. 'var $with1 = $mod.b;',
  20457. '$with1.FSize = $with1.FSize + 31;',
  20458. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  20459. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  20460. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  20461. '']));
  20462. end;
  20463. procedure TTestModule.TestClassHelper_Property_Array;
  20464. begin
  20465. StartProgram(false);
  20466. Add([
  20467. 'type',
  20468. ' TObject = class',
  20469. ' function GetSpeed(Index: boolean): word;',
  20470. ' procedure SetSpeed(Index: boolean; Value: word);',
  20471. ' end;',
  20472. ' TObjHelper = class helper for TObject',
  20473. ' function GetSize(Index: boolean): word;',
  20474. ' procedure SetSize(Index: boolean; Value: word);',
  20475. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  20476. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20477. ' end;',
  20478. ' TBird = class',
  20479. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  20480. ' procedure DoIt;',
  20481. ' end;',
  20482. 'var',
  20483. ' b: TBird;',
  20484. 'function Tobject.GetSpeed(Index: boolean): word;',
  20485. 'begin',
  20486. ' Result:=Size[false];',
  20487. ' Size[true]:=Size[false]+11;',
  20488. ' Speed[true]:=Speed[false]+12;',
  20489. ' Self.Size[true]:=Self.Size[false]+21;',
  20490. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20491. ' with Self do begin',
  20492. ' Size[true]:=Size[false]+31;',
  20493. ' Speed[true]:=Speed[false]+32;',
  20494. ' end;',
  20495. 'end;',
  20496. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20497. 'begin',
  20498. 'end;',
  20499. 'function TObjHelper.GetSize(Index: boolean): word;',
  20500. 'begin',
  20501. ' Size[true]:=Size[false]+11;',
  20502. ' Speed[true]:=Speed[false]+12;',
  20503. ' Self.Size[true]:=Self.Size[false]+21;',
  20504. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20505. ' with Self do begin',
  20506. ' Size[true]:=Size[false]+31;',
  20507. ' Speed[true]:=Speed[false]+32;',
  20508. ' end;',
  20509. 'end;',
  20510. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20511. 'begin',
  20512. 'end;',
  20513. 'procedure TBird.DoIt;',
  20514. 'begin',
  20515. ' Items[true]:=Items[false]+11;',
  20516. ' Self.Items[true]:=Self.Items[false]+21;',
  20517. ' with Self do Items[true]:=Items[false]+31;',
  20518. 'end;',
  20519. 'begin',
  20520. ' b.Size[true]:=b.Size[false]+11;',
  20521. ' b.Speed[true]:=b.Speed[false]+12;',
  20522. ' b.Items[true]:=b.Items[false]+13;',
  20523. ' with b do begin',
  20524. ' Size[true]:=Size[false]+21;',
  20525. ' Speed[true]:=Speed[false]+22;',
  20526. ' Items[true]:=Items[false]+23;',
  20527. ' end;',
  20528. '']);
  20529. ConvertProgram;
  20530. CheckSource('TestClassHelper_Property_Array',
  20531. LinesToStr([ // statements
  20532. 'rtl.createClass($mod, "TObject", null, function () {',
  20533. ' this.$init = function () {',
  20534. ' };',
  20535. ' this.$final = function () {',
  20536. ' };',
  20537. ' this.GetSpeed = function (Index) {',
  20538. ' var Result = 0;',
  20539. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  20540. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20541. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20542. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20543. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20544. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20545. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20546. ' return Result;',
  20547. ' };',
  20548. ' this.SetSpeed = function (Index, Value) {',
  20549. ' };',
  20550. '});',
  20551. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20552. ' this.GetSize = function (Index) {',
  20553. ' var Result = 0;',
  20554. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20555. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20556. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20557. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20558. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20559. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20560. ' return Result;',
  20561. ' };',
  20562. ' this.SetSize = function (Index, Value) {',
  20563. ' };',
  20564. '});',
  20565. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20566. ' this.DoIt = function () {',
  20567. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20568. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20569. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20570. ' };',
  20571. '});',
  20572. 'this.b = null;',
  20573. '']),
  20574. LinesToStr([ // $mod.$main
  20575. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  20576. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  20577. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  20578. 'var $with1 = $mod.b;',
  20579. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  20580. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  20581. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  20582. '']));
  20583. end;
  20584. procedure TTestModule.TestClassHelper_Property_Array_Default;
  20585. begin
  20586. StartProgram(false);
  20587. Add([
  20588. 'type',
  20589. ' TObject = class',
  20590. ' function GetSpeed(Index: boolean): word;',
  20591. ' procedure SetSpeed(Index: boolean; Value: word);',
  20592. ' end;',
  20593. ' TObjHelper = class helper for TObject',
  20594. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  20595. ' end;',
  20596. ' TBird = class',
  20597. ' end;',
  20598. ' TBirdHelper = class helper for TBird',
  20599. ' function GetSize(Index: word): boolean;',
  20600. ' procedure SetSize(Index: word; Value: boolean);',
  20601. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  20602. ' end;',
  20603. 'function Tobject.GetSpeed(Index: boolean): word;',
  20604. 'begin',
  20605. ' Self[true]:=Self[false]+1;',
  20606. 'end;',
  20607. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20608. 'begin',
  20609. 'end;',
  20610. 'function TBirdHelper.GetSize(Index: word): boolean;',
  20611. 'begin',
  20612. ' Self[1]:=not Self[2];',
  20613. 'end;',
  20614. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  20615. 'begin',
  20616. 'end;',
  20617. 'var',
  20618. ' o: TObject;',
  20619. ' b: TBird;',
  20620. 'begin',
  20621. ' o[true]:=o[false]+1;',
  20622. ' b[3]:=not b[4];',
  20623. '']);
  20624. ConvertProgram;
  20625. CheckSource('TestClassHelper_Property_Array_Default',
  20626. LinesToStr([ // statements
  20627. 'rtl.createClass($mod, "TObject", null, function () {',
  20628. ' this.$init = function () {',
  20629. ' };',
  20630. ' this.$final = function () {',
  20631. ' };',
  20632. ' this.GetSpeed = function (Index) {',
  20633. ' var Result = 0;',
  20634. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  20635. ' return Result;',
  20636. ' };',
  20637. ' this.SetSpeed = function (Index, Value) {',
  20638. ' };',
  20639. '});',
  20640. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20641. '});',
  20642. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20643. '});',
  20644. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20645. ' this.GetSize = function (Index) {',
  20646. ' var Result = false;',
  20647. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  20648. ' return Result;',
  20649. ' };',
  20650. ' this.SetSize = function (Index, Value) {',
  20651. ' };',
  20652. '});',
  20653. 'this.o = null;',
  20654. 'this.b = null;',
  20655. '']),
  20656. LinesToStr([ // $mod.$main
  20657. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  20658. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  20659. '']));
  20660. end;
  20661. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  20662. begin
  20663. StartProgram(false);
  20664. Add([
  20665. 'type',
  20666. ' TObject = class',
  20667. ' end;',
  20668. ' TObjHelper = class helper for TObject',
  20669. ' function GetItems(Index: word): TObject;',
  20670. ' procedure SetItems(Index: word; Value: TObject);',
  20671. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  20672. ' end;',
  20673. 'function Tobjhelper.GetItems(Index: word): TObject;',
  20674. 'begin',
  20675. ' Self[1][2]:=Self[3][4];',
  20676. 'end;',
  20677. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  20678. 'begin',
  20679. 'end;',
  20680. 'var',
  20681. ' o: TObject;',
  20682. 'begin',
  20683. ' o[1][2]:=o[3][4];',
  20684. '']);
  20685. ConvertProgram;
  20686. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  20687. LinesToStr([ // statements
  20688. 'rtl.createClass($mod, "TObject", null, function () {',
  20689. ' this.$init = function () {',
  20690. ' };',
  20691. ' this.$final = function () {',
  20692. ' };',
  20693. '});',
  20694. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20695. ' this.GetItems = function (Index) {',
  20696. ' var Result = null;',
  20697. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  20698. ' return Result;',
  20699. ' };',
  20700. ' this.SetItems = function (Index, Value) {',
  20701. ' };',
  20702. '});',
  20703. 'this.o = null;',
  20704. '']),
  20705. LinesToStr([ // $mod.$main
  20706. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  20707. '']));
  20708. end;
  20709. procedure TTestModule.TestClassHelper_ClassProperty;
  20710. begin
  20711. StartProgram(false);
  20712. Add([
  20713. 'type',
  20714. ' TObject = class',
  20715. ' class var FSize: word;',
  20716. ' class function GetSpeed: word;',
  20717. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  20718. ' end;',
  20719. ' TObjHelper = class helper for TObject',
  20720. ' class function GetLeft: word;',
  20721. ' class procedure SetLeft(Value: word);',
  20722. ' class property Size: word read FSize write FSize;',
  20723. ' class property Speed: word read GetSpeed write SetSpeed;',
  20724. ' class property Left: word read GetLeft write SetLeft;',
  20725. ' end;',
  20726. ' TBird = class',
  20727. ' class property NotRight: word read GetLeft write SetLeft;',
  20728. ' class procedure DoIt;',
  20729. ' end;',
  20730. ' TBirdClass = class of TBird;',
  20731. 'class function Tobject.GetSpeed: word;',
  20732. 'begin',
  20733. ' Size:=Size+11;',
  20734. ' Speed:=Speed+12;',
  20735. ' Left:=Left+13;',
  20736. ' Self.Size:=Self.Size+21;',
  20737. ' Self.Speed:=Self.Speed+22;',
  20738. ' Self.Left:=Self.Left+23;',
  20739. ' with Self do begin',
  20740. ' Size:=Size+31;',
  20741. ' Speed:=Speed+32;',
  20742. ' Left:=Left+33;',
  20743. ' end;',
  20744. 'end;',
  20745. 'class function TObjHelper.GetLeft: word;',
  20746. 'begin',
  20747. ' Size:=Size+11;',
  20748. ' Speed:=Speed+12;',
  20749. ' Left:=Left+13;',
  20750. ' Self.Size:=Self.Size+21;',
  20751. ' Self.Speed:=Self.Speed+22;',
  20752. ' Self.Left:=Self.Left+23;',
  20753. ' with Self do begin',
  20754. ' Size:=Size+31;',
  20755. ' Speed:=Speed+32;',
  20756. ' Left:=Left+33;',
  20757. ' end;',
  20758. 'end;',
  20759. 'class procedure TObjHelper.SetLeft(Value: word);',
  20760. 'begin',
  20761. 'end;',
  20762. 'class procedure TBird.DoIt;',
  20763. 'begin',
  20764. ' NotRight:=NotRight+11;',
  20765. ' Self.NotRight:=Self.NotRight+21;',
  20766. ' with Self do NotRight:=NotRight+31;',
  20767. 'end;',
  20768. 'var',
  20769. ' b: TBird;',
  20770. ' c: TBirdClass;',
  20771. 'begin',
  20772. ' b.Size:=b.Size+11;',
  20773. ' b.Speed:=b.Speed+12;',
  20774. ' b.Left:=b.Left+13;',
  20775. ' b.NotRight:=b.NotRight+14;',
  20776. ' with b do begin',
  20777. ' Size:=Size+31;',
  20778. ' Speed:=Speed+32;',
  20779. ' Left:=Left+33;',
  20780. ' NotRight:=NotRight+34;',
  20781. ' end;',
  20782. ' c.Size:=c.Size+11;',
  20783. ' c.Speed:=c.Speed+12;',
  20784. ' c.Left:=c.Left+13;',
  20785. ' c.NotRight:=c.NotRight+14;',
  20786. ' with c do begin',
  20787. ' Size:=Size+31;',
  20788. ' Speed:=Speed+32;',
  20789. ' Left:=Left+33;',
  20790. ' NotRight:=NotRight+34;',
  20791. ' end;',
  20792. ' tbird.Size:=tbird.Size+11;',
  20793. ' tbird.Speed:=tbird.Speed+12;',
  20794. ' tbird.Left:=tbird.Left+13;',
  20795. ' tbird.NotRight:=tbird.NotRight+14;',
  20796. ' with tbird do begin',
  20797. ' Size:=Size+31;',
  20798. ' Speed:=Speed+32;',
  20799. ' Left:=Left+33;',
  20800. ' NotRight:=NotRight+34;',
  20801. ' end;',
  20802. '']);
  20803. ConvertProgram;
  20804. CheckSource('TestClassHelper_ClassProperty',
  20805. LinesToStr([ // statements
  20806. 'rtl.createClass($mod, "TObject", null, function () {',
  20807. ' this.FSize = 0;',
  20808. ' this.$init = function () {',
  20809. ' };',
  20810. ' this.$final = function () {',
  20811. ' };',
  20812. ' this.GetSpeed = function () {',
  20813. ' var Result = 0;',
  20814. ' $mod.TObject.FSize = this.FSize + 11;',
  20815. ' this.SetSpeed(this.GetSpeed() + 12);',
  20816. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20817. ' $mod.TObject.FSize = this.FSize + 21;',
  20818. ' this.SetSpeed(this.GetSpeed() + 22);',
  20819. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20820. ' $mod.TObject.FSize = this.FSize + 31;',
  20821. ' this.SetSpeed(this.GetSpeed() + 32);',
  20822. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20823. ' return Result;',
  20824. ' };',
  20825. '});',
  20826. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20827. ' this.GetLeft = function () {',
  20828. ' var Result = 0;',
  20829. ' $mod.TObject.FSize = this.FSize + 11;',
  20830. ' this.SetSpeed(this.GetSpeed() + 12);',
  20831. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20832. ' $mod.TObject.FSize = this.FSize + 21;',
  20833. ' this.SetSpeed(this.GetSpeed() + 22);',
  20834. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20835. ' $mod.TObject.FSize = this.FSize + 31;',
  20836. ' this.SetSpeed(this.GetSpeed() + 32);',
  20837. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20838. ' return Result;',
  20839. ' };',
  20840. ' this.SetLeft = function (Value) {',
  20841. ' };',
  20842. '});',
  20843. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20844. ' this.DoIt = function () {',
  20845. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  20846. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  20847. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  20848. ' };',
  20849. '});',
  20850. 'this.b = null;',
  20851. 'this.c = null;',
  20852. '']),
  20853. LinesToStr([ // $mod.$main
  20854. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  20855. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  20856. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  20857. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  20858. 'var $with1 = $mod.b;',
  20859. '$mod.TObject.FSize = $with1.FSize + 31;',
  20860. '$with1.$class.SetSpeed($with1.$class.GetSpeed() + 32);',
  20861. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 33);',
  20862. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 34);',
  20863. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  20864. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  20865. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  20866. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  20867. 'var $with2 = $mod.c;',
  20868. '$mod.TObject.FSize = $with2.FSize + 31;',
  20869. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  20870. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 33);',
  20871. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 34);',
  20872. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  20873. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  20874. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  20875. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  20876. 'var $with3 = $mod.TBird;',
  20877. '$mod.TObject.FSize = $with3.FSize + 31;',
  20878. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  20879. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  20880. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  20881. '']));
  20882. end;
  20883. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  20884. begin
  20885. StartProgram(false);
  20886. Add([
  20887. 'type',
  20888. ' TObject = class',
  20889. ' class function GetSpeed: word; static;',
  20890. ' class procedure SetSpeed(Value: word); static;',
  20891. ' end;',
  20892. ' TObjHelper = class helper for TObject',
  20893. ' class function GetLeft: word; static;',
  20894. ' class procedure SetLeft(Value: word); static;',
  20895. ' class property Speed: word read GetSpeed write SetSpeed;',
  20896. ' class property Left: word read GetLeft write SetLeft;',
  20897. ' end;',
  20898. ' TBird = class',
  20899. ' class property NotRight: word read GetLeft write SetLeft;',
  20900. ' class procedure DoIt; static;',
  20901. ' class procedure DoSome;',
  20902. ' end;',
  20903. ' TBirdClass = class of TBird;',
  20904. 'class function Tobject.GetSpeed: word;',
  20905. 'begin',
  20906. ' Speed:=Speed+12;',
  20907. ' Left:=Left+13;',
  20908. 'end;',
  20909. 'class procedure TObject.SetSpeed(Value: word);',
  20910. 'begin',
  20911. 'end;',
  20912. 'class function TObjHelper.GetLeft: word;',
  20913. 'begin',
  20914. ' Speed:=Speed+12;',
  20915. ' Left:=Left+13;',
  20916. 'end;',
  20917. 'class procedure TObjHelper.SetLeft(Value: word);',
  20918. 'begin',
  20919. 'end;',
  20920. 'class procedure TBird.DoIt;',
  20921. 'begin',
  20922. ' NotRight:=NotRight+11;',
  20923. 'end;',
  20924. 'class procedure TBird.DoSome;',
  20925. 'begin',
  20926. ' Speed:=Speed+12;',
  20927. ' Left:=Left+13;',
  20928. ' Self.Speed:=Self.Speed+22;',
  20929. ' Self.Left:=Self.Left+23;',
  20930. ' with Self do begin',
  20931. ' Speed:=Speed+32;',
  20932. ' Left:=Left+33;',
  20933. ' end;',
  20934. ' NotRight:=NotRight+11;',
  20935. ' Self.NotRight:=Self.NotRight+21;',
  20936. ' with Self do NotRight:=NotRight+31;',
  20937. 'end;',
  20938. 'var',
  20939. ' b: TBird;',
  20940. ' c: TBirdClass;',
  20941. 'begin',
  20942. ' b.Speed:=b.Speed+12;',
  20943. ' b.Left:=b.Left+13;',
  20944. ' b.NotRight:=b.NotRight+14;',
  20945. ' with b do begin',
  20946. ' Speed:=Speed+32;',
  20947. ' Left:=Left+33;',
  20948. ' NotRight:=NotRight+34;',
  20949. ' end;',
  20950. ' c.Speed:=c.Speed+12;',
  20951. ' c.Left:=c.Left+13;',
  20952. ' c.NotRight:=c.NotRight+14;',
  20953. ' with c do begin',
  20954. ' Speed:=Speed+32;',
  20955. ' Left:=Left+33;',
  20956. ' NotRight:=NotRight+34;',
  20957. ' end;',
  20958. ' tbird.Speed:=tbird.Speed+12;',
  20959. ' tbird.Left:=tbird.Left+13;',
  20960. ' tbird.NotRight:=tbird.NotRight+14;',
  20961. ' with tbird do begin',
  20962. ' Speed:=Speed+32;',
  20963. ' Left:=Left+33;',
  20964. ' NotRight:=NotRight+34;',
  20965. ' end;',
  20966. '']);
  20967. ConvertProgram;
  20968. CheckSource('TestClassHelper_ClassPropertyStatic',
  20969. LinesToStr([ // statements
  20970. 'rtl.createClass($mod, "TObject", null, function () {',
  20971. ' this.$init = function () {',
  20972. ' };',
  20973. ' this.$final = function () {',
  20974. ' };',
  20975. ' this.GetSpeed = function () {',
  20976. ' var Result = 0;',
  20977. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  20978. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20979. ' return Result;',
  20980. ' };',
  20981. ' this.SetSpeed = function (Value) {',
  20982. ' };',
  20983. '});',
  20984. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20985. ' this.GetLeft = function () {',
  20986. ' var Result = 0;',
  20987. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  20988. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20989. ' return Result;',
  20990. ' };',
  20991. ' this.SetLeft = function (Value) {',
  20992. ' };',
  20993. '});',
  20994. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20995. ' this.DoIt = function () {',
  20996. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20997. ' };',
  20998. ' this.DoSome = function () {',
  20999. ' this.SetSpeed(this.GetSpeed() + 12);',
  21000. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21001. ' this.SetSpeed(this.GetSpeed() + 22);',
  21002. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  21003. ' this.SetSpeed(this.GetSpeed() + 32);',
  21004. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21005. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21006. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  21007. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  21008. ' };',
  21009. '});',
  21010. 'this.b = null;',
  21011. 'this.c = null;',
  21012. '']),
  21013. LinesToStr([ // $mod.$main
  21014. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21015. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21016. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21017. 'var $with1 = $mod.b;',
  21018. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21019. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21020. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21021. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21022. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21023. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21024. 'var $with2 = $mod.c;',
  21025. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21026. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21027. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21028. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21029. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21030. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21031. 'var $with3 = $mod.TBird;',
  21032. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  21033. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21034. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21035. '']));
  21036. end;
  21037. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  21038. begin
  21039. StartProgram(false);
  21040. Add([
  21041. 'type',
  21042. ' TObject = class',
  21043. ' class function GetSpeed(Index: boolean): word;',
  21044. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  21045. ' end;',
  21046. ' TObjHelper = class helper for TObject',
  21047. ' class function GetSize(Index: boolean): word;',
  21048. ' class procedure SetSize(Index: boolean; Value: word);',
  21049. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  21050. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21051. ' end;',
  21052. ' TBird = class',
  21053. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  21054. ' class procedure DoIt;',
  21055. ' end;',
  21056. ' TBirdClass = class of TBird;',
  21057. 'class function Tobject.GetSpeed(Index: boolean): word;',
  21058. 'begin',
  21059. ' Size[true]:=Size[false]+11;',
  21060. ' Speed[true]:=Speed[false]+12;',
  21061. ' Self.Size[true]:=Self.Size[false]+21;',
  21062. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21063. ' with Self do begin',
  21064. ' Size[true]:=Size[false]+31;',
  21065. ' Speed[true]:=Speed[false]+32;',
  21066. ' end;',
  21067. 'end;',
  21068. 'class function TObjHelper.GetSize(Index: boolean): word;',
  21069. 'begin',
  21070. ' Size[true]:=Size[false]+11;',
  21071. ' Speed[true]:=Speed[false]+12;',
  21072. ' Self.Size[true]:=Self.Size[false]+21;',
  21073. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21074. ' with Self do begin',
  21075. ' Size[true]:=Size[false]+31;',
  21076. ' Speed[true]:=Speed[false]+32;',
  21077. ' end;',
  21078. 'end;',
  21079. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21080. 'begin',
  21081. 'end;',
  21082. 'class procedure TBird.DoIt;',
  21083. 'begin',
  21084. ' Items[true]:=Items[false]+11;',
  21085. ' Self.Items[true]:=Self.Items[false]+21;',
  21086. ' with Self do Items[true]:=Items[false]+31;',
  21087. 'end;',
  21088. 'var',
  21089. ' b: TBird;',
  21090. ' c: TBirdClass;',
  21091. 'begin',
  21092. ' b.Size[true]:=b.Size[false]+11;',
  21093. ' b.Speed[true]:=b.Speed[false]+12;',
  21094. ' b.Items[true]:=b.Items[false]+13;',
  21095. ' with b do begin',
  21096. ' Size[true]:=Size[false]+21;',
  21097. ' Speed[true]:=Speed[false]+22;',
  21098. ' Items[true]:=Items[false]+23;',
  21099. ' end;',
  21100. ' c.Size[true]:=c.Size[false]+11;',
  21101. ' c.Speed[true]:=c.Speed[false]+12;',
  21102. ' c.Items[true]:=c.Items[false]+13;',
  21103. ' with c do begin',
  21104. ' Size[true]:=Size[false]+21;',
  21105. ' Speed[true]:=Speed[false]+22;',
  21106. ' Items[true]:=Items[false]+23;',
  21107. ' end;',
  21108. ' TBird.Size[true]:=TBird.Size[false]+11;',
  21109. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  21110. ' TBird.Items[true]:=TBird.Items[false]+13;',
  21111. ' with TBird do begin',
  21112. ' Size[true]:=Size[false]+21;',
  21113. ' Speed[true]:=Speed[false]+22;',
  21114. ' Items[true]:=Items[false]+23;',
  21115. ' end;',
  21116. '']);
  21117. ConvertProgram;
  21118. CheckSource('TestClassHelper_ClassProperty_Array',
  21119. LinesToStr([ // statements
  21120. 'rtl.createClass($mod, "TObject", null, function () {',
  21121. ' this.$init = function () {',
  21122. ' };',
  21123. ' this.$final = function () {',
  21124. ' };',
  21125. ' this.GetSpeed = function (Index) {',
  21126. ' var Result = 0;',
  21127. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21128. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21129. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21130. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21131. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21132. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21133. ' return Result;',
  21134. ' };',
  21135. '});',
  21136. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21137. ' this.GetSize = function (Index) {',
  21138. ' var Result = 0;',
  21139. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21140. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21141. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21142. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21143. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21144. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21145. ' return Result;',
  21146. ' };',
  21147. ' this.SetSize = function (Index, Value) {',
  21148. ' };',
  21149. '});',
  21150. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21151. ' this.DoIt = function () {',
  21152. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21153. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21154. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21155. ' };',
  21156. '});',
  21157. 'this.b = null;',
  21158. 'this.c = null;',
  21159. '']),
  21160. LinesToStr([ // $mod.$main
  21161. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  21162. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  21163. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  21164. 'var $with1 = $mod.b;',
  21165. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 21);',
  21166. '$with1.$class.SetSpeed(true, $with1.$class.GetSpeed(false) + 22);',
  21167. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 23);',
  21168. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  21169. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  21170. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  21171. 'var $with2 = $mod.c;',
  21172. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 21);',
  21173. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  21174. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 23);',
  21175. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  21176. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  21177. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  21178. 'var $with3 = $mod.TBird;',
  21179. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  21180. '$with3.SetSpeed(true, $with3.GetSpeed(false) + 22);',
  21181. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  21182. '']));
  21183. end;
  21184. procedure TTestModule.TestClassHelper_ForIn;
  21185. begin
  21186. StartProgram(false);
  21187. Add([
  21188. 'type',
  21189. ' TObject = class end;',
  21190. ' TItem = TObject;',
  21191. ' TEnumerator = class',
  21192. ' FCurrent: TItem;',
  21193. ' property Current: TItem read FCurrent;',
  21194. ' function MoveNext: boolean;',
  21195. ' end;',
  21196. ' TBird = class',
  21197. ' end;',
  21198. ' TBirdHelper = class helper for TBird',
  21199. ' function GetEnumerator: TEnumerator;',
  21200. ' end;',
  21201. 'function TEnumerator.MoveNext: boolean;',
  21202. 'begin',
  21203. 'end;',
  21204. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  21205. 'begin',
  21206. 'end;',
  21207. 'var',
  21208. ' b: TBird;',
  21209. ' i, i2: TItem;',
  21210. 'begin',
  21211. ' for i in b do i2:=i;']);
  21212. ConvertProgram;
  21213. CheckSource('TestClassHelper_ForIn',
  21214. LinesToStr([ // statements
  21215. 'rtl.createClass($mod, "TObject", null, function () {',
  21216. ' this.$init = function () {',
  21217. ' };',
  21218. ' this.$final = function () {',
  21219. ' };',
  21220. '});',
  21221. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  21222. ' this.$init = function () {',
  21223. ' $mod.TObject.$init.call(this);',
  21224. ' this.FCurrent = null;',
  21225. ' };',
  21226. ' this.$final = function () {',
  21227. ' this.FCurrent = undefined;',
  21228. ' $mod.TObject.$final.call(this);',
  21229. ' };',
  21230. ' this.MoveNext = function () {',
  21231. ' var Result = false;',
  21232. ' return Result;',
  21233. ' };',
  21234. '});',
  21235. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21236. '});',
  21237. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  21238. ' this.GetEnumerator = function () {',
  21239. ' var Result = null;',
  21240. ' return Result;',
  21241. ' };',
  21242. '});',
  21243. 'this.b = null;',
  21244. 'this.i = null;',
  21245. 'this.i2 = null;'
  21246. ]),
  21247. LinesToStr([ // $mod.$main
  21248. 'var $in1 = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  21249. 'try {',
  21250. ' while ($in1.MoveNext()){',
  21251. ' $mod.i = $in1.FCurrent;',
  21252. ' $mod.i2 = $mod.i;',
  21253. ' }',
  21254. '} finally {',
  21255. ' $in1 = rtl.freeLoc($in1)',
  21256. '};',
  21257. '']));
  21258. end;
  21259. procedure TTestModule.TestClassHelper_PassProperty;
  21260. begin
  21261. StartProgram(false);
  21262. Add([
  21263. 'type',
  21264. ' TObject = class',
  21265. ' FField: TObject;',
  21266. ' property Field: TObject read FField write FField;',
  21267. ' end;',
  21268. ' THelper = class helper for TObject',
  21269. ' procedure Fly;',
  21270. ' class procedure Run;',
  21271. ' class procedure Jump; static;',
  21272. ' end;',
  21273. 'procedure THelper.Fly;',
  21274. 'begin',
  21275. ' Field.Fly;',
  21276. ' Field.Run;',
  21277. ' Field.Jump;',
  21278. ' with Field do begin',
  21279. ' Fly;',
  21280. ' Run;',
  21281. ' Jump;',
  21282. ' end;',
  21283. 'end;',
  21284. 'class procedure THelper.Run;',
  21285. 'begin',
  21286. 'end;',
  21287. 'class procedure THelper.Jump;',
  21288. 'begin',
  21289. 'end;',
  21290. 'var',
  21291. ' b: TObject;',
  21292. 'begin',
  21293. ' b.Field.Fly;',
  21294. ' b.Field.Run;',
  21295. ' b.Field.Jump;',
  21296. ' with b do begin',
  21297. ' Field.Run;',
  21298. ' Field.Fly;',
  21299. ' Field.Jump;',
  21300. ' end;',
  21301. ' with b.Field do begin',
  21302. ' Run;',
  21303. ' Fly;',
  21304. ' Jump;',
  21305. ' end;',
  21306. '']);
  21307. ConvertProgram;
  21308. CheckSource('TestClassHelper_PassProperty',
  21309. LinesToStr([ // statements
  21310. 'rtl.createClass($mod, "TObject", null, function () {',
  21311. ' this.$init = function () {',
  21312. ' this.FField = null;',
  21313. ' };',
  21314. ' this.$final = function () {',
  21315. ' this.FField = undefined;',
  21316. ' };',
  21317. '});',
  21318. 'rtl.createHelper($mod, "THelper", null, function () {',
  21319. ' this.Fly = function () {',
  21320. ' $mod.THelper.Fly.call(this.FField);',
  21321. ' $mod.THelper.Run.call(this.FField.$class);',
  21322. ' $mod.THelper.Jump();',
  21323. ' var $with1 = this.FField;',
  21324. ' $mod.THelper.Fly.call($with1);',
  21325. ' $mod.THelper.Run.call($with1.$class);',
  21326. ' $mod.THelper.Jump();',
  21327. ' };',
  21328. ' this.Run = function () {',
  21329. ' };',
  21330. ' this.Jump = function () {',
  21331. ' };',
  21332. '});',
  21333. 'this.b = null;',
  21334. '']),
  21335. LinesToStr([ // $mod.$main
  21336. '$mod.THelper.Fly.call($mod.b.FField);',
  21337. '$mod.THelper.Run.call($mod.b.FField.$class);',
  21338. '$mod.THelper.Jump();',
  21339. 'var $with1 = $mod.b;',
  21340. '$mod.THelper.Run.call($with1.FField.$class);',
  21341. '$mod.THelper.Fly.call($with1.FField);',
  21342. '$mod.THelper.Jump();',
  21343. 'var $with2 = $mod.b.FField;',
  21344. '$mod.THelper.Run.call($with2.$class);',
  21345. '$mod.THelper.Fly.call($with2);',
  21346. '$mod.THelper.Jump();',
  21347. '']));
  21348. end;
  21349. procedure TTestModule.TestExtClassHelper_ClassVar;
  21350. begin
  21351. StartProgram(false);
  21352. Add([
  21353. '{$modeswitch externalclass}',
  21354. 'type',
  21355. ' TExtA = class external name ''ExtObj''',
  21356. ' end;',
  21357. ' THelper = class helper for TExtA',
  21358. ' const',
  21359. ' One = 1;',
  21360. ' Two: word = 2;',
  21361. ' class var',
  21362. ' Glob: word;',
  21363. ' function Foo(w: word): word;',
  21364. ' class function Bar(w: word): word; static;',
  21365. ' end;',
  21366. 'function THelper.foo(w: word): word;',
  21367. 'begin',
  21368. ' Result:=w;',
  21369. ' Two:=One+w;',
  21370. ' Glob:=Glob;',
  21371. ' Result:=Self.Glob;',
  21372. ' Self.Glob:=Self.Glob;',
  21373. ' with Self do Glob:=Glob;',
  21374. 'end;',
  21375. 'class function THelper.bar(w: word): word;',
  21376. 'begin',
  21377. ' Result:=w;',
  21378. ' Two:=One;',
  21379. ' Glob:=Glob;',
  21380. 'end;',
  21381. 'var o: TExtA;',
  21382. 'begin',
  21383. ' texta.two:=texta.one;',
  21384. ' texta.Glob:=texta.Glob;',
  21385. ' with texta do begin',
  21386. ' two:=one;',
  21387. ' Glob:=Glob;',
  21388. ' end;',
  21389. ' o.two:=o.one;',
  21390. ' o.Glob:=o.Glob;',
  21391. ' with o do begin',
  21392. ' two:=one;',
  21393. ' Glob:=Glob;',
  21394. ' end;',
  21395. '']);
  21396. ConvertProgram;
  21397. CheckSource('TestExtClassHelper_ClassVar',
  21398. LinesToStr([ // statements
  21399. 'rtl.createHelper($mod, "THelper", null, function () {',
  21400. ' this.One = 1;',
  21401. ' this.Two = 2;',
  21402. ' this.Glob = 0;',
  21403. ' this.Foo = function (w) {',
  21404. ' var Result = 0;',
  21405. ' Result = w;',
  21406. ' $mod.THelper.Two = 1 + w;',
  21407. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21408. ' Result = $mod.THelper.Glob;',
  21409. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21410. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21411. ' return Result;',
  21412. ' };',
  21413. ' this.Bar = function (w) {',
  21414. ' var Result = 0;',
  21415. ' Result = w;',
  21416. ' $mod.THelper.Two = 1;',
  21417. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21418. ' return Result;',
  21419. ' };',
  21420. '});',
  21421. 'this.o = null;',
  21422. '']),
  21423. LinesToStr([ // $mod.$main
  21424. '$mod.THelper.Two = 1;',
  21425. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21426. '$mod.THelper.Two = 1;',
  21427. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21428. '$mod.THelper.Two = 1;',
  21429. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21430. 'var $with1 = $mod.o;',
  21431. '$mod.THelper.Two = 1;',
  21432. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21433. '']));
  21434. end;
  21435. procedure TTestModule.TestExtClassHelper_Method_Call;
  21436. begin
  21437. StartProgram(false);
  21438. Add([
  21439. '{$modeswitch externalclass}',
  21440. 'type',
  21441. ' TFly = function(w: word): word of object;',
  21442. ' TExtA = class external name ''ExtObj''',
  21443. ' procedure Run(w: word = 10);',
  21444. ' end;',
  21445. ' THelper = class helper for TExtA',
  21446. ' function Foo(w: word = 1): word;',
  21447. ' function Fly(w: word = 2): word; external name ''Fly'';',
  21448. ' end;',
  21449. 'var p: TFly;',
  21450. 'function THelper.foo(w: word): word;',
  21451. 'begin',
  21452. ' Run;',
  21453. ' Run();',
  21454. ' Run(11);',
  21455. ' Foo;',
  21456. ' Foo();',
  21457. ' Foo(12);',
  21458. ' Self.Foo;',
  21459. ' Self.Foo();',
  21460. ' Self.Foo(13);',
  21461. ' Fly;',
  21462. ' Fly();',
  21463. ' with Self do begin',
  21464. ' Foo;',
  21465. ' Foo();',
  21466. ' Foo(14);',
  21467. ' Fly;',
  21468. ' Fly();',
  21469. ' end;',
  21470. ' p:=@Fly;',
  21471. 'end;',
  21472. 'var Obj: TExtA;',
  21473. 'begin',
  21474. ' obj.Foo;',
  21475. ' obj.Foo();',
  21476. ' obj.Foo(21);',
  21477. ' obj.Fly;',
  21478. ' obj.Fly();',
  21479. ' with obj do begin',
  21480. ' Foo;',
  21481. ' Foo();',
  21482. ' Foo(22);',
  21483. ' Fly;',
  21484. ' Fly();',
  21485. ' end;',
  21486. ' p:[email protected];',
  21487. '']);
  21488. ConvertProgram;
  21489. CheckSource('TestExtClassHelper_Method_Call',
  21490. LinesToStr([ // statements
  21491. 'rtl.createHelper($mod, "THelper", null, function () {',
  21492. ' this.Foo = function (w) {',
  21493. ' var Result = 0;',
  21494. ' this.Run(10);',
  21495. ' this.Run(10);',
  21496. ' this.Run(11);',
  21497. ' $mod.THelper.Foo.call(this, 1);',
  21498. ' $mod.THelper.Foo.call(this, 1);',
  21499. ' $mod.THelper.Foo.call(this, 12);',
  21500. ' $mod.THelper.Foo.call(this, 1);',
  21501. ' $mod.THelper.Foo.call(this, 1);',
  21502. ' $mod.THelper.Foo.call(this, 13);',
  21503. ' this.Fly(2);',
  21504. ' this.Fly(2);',
  21505. ' $mod.THelper.Foo.call(this, 1);',
  21506. ' $mod.THelper.Foo.call(this, 1);',
  21507. ' $mod.THelper.Foo.call(this, 14);',
  21508. ' this.Fly(2);',
  21509. ' this.Fly(2);',
  21510. ' $mod.p = rtl.createCallback(this, "Fly");',
  21511. ' return Result;',
  21512. ' };',
  21513. '});',
  21514. 'this.p = null;',
  21515. 'this.Obj = null;',
  21516. '']),
  21517. LinesToStr([ // $mod.$main
  21518. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21519. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21520. '$mod.THelper.Foo.call($mod.Obj, 21);',
  21521. '$mod.Obj.Fly(2);',
  21522. '$mod.Obj.Fly(2);',
  21523. 'var $with1 = $mod.Obj;',
  21524. '$mod.THelper.Foo.call($with1, 1);',
  21525. '$mod.THelper.Foo.call($with1, 1);',
  21526. '$mod.THelper.Foo.call($with1, 22);',
  21527. '$with1.Fly(2);',
  21528. '$with1.Fly(2);',
  21529. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  21530. '']));
  21531. end;
  21532. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  21533. begin
  21534. StartProgram(false);
  21535. Add([
  21536. '{$modeswitch externalclass}',
  21537. 'type',
  21538. ' TExtA = class external name ''ExtObj''',
  21539. ' procedure Run(w: word = 10);',
  21540. ' end;',
  21541. ' THelper = class helper for TExtA',
  21542. ' class procedure Fly;',
  21543. ' end;',
  21544. 'class procedure THelper.Fly;',
  21545. 'begin end;',
  21546. 'begin',
  21547. '']);
  21548. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  21549. nHelperClassMethodForExtClassMustBeStatic);
  21550. ConvertProgram;
  21551. end;
  21552. procedure TTestModule.TestRecordHelper_ClassVar;
  21553. begin
  21554. StartProgram(false);
  21555. Add([
  21556. 'type',
  21557. ' TRec = record',
  21558. ' end;',
  21559. ' THelper = record helper for TRec',
  21560. ' const',
  21561. ' One = 1;',
  21562. ' Two: word = 2;',
  21563. ' class var',
  21564. ' Glob: word;',
  21565. ' function Foo(w: word): word;',
  21566. ' class function Bar(w: word): word; static;',
  21567. ' end;',
  21568. 'function THelper.foo(w: word): word;',
  21569. 'begin',
  21570. ' Result:=w;',
  21571. ' Two:=One+w;',
  21572. ' Glob:=Glob;',
  21573. ' Result:=Self.Glob;',
  21574. ' Self.Glob:=Self.Glob;',
  21575. ' with Self do Glob:=Glob;',
  21576. ' Self:=Self;',
  21577. 'end;',
  21578. 'class function THelper.bar(w: word): word;',
  21579. 'begin',
  21580. ' Result:=w;',
  21581. ' Two:=One;',
  21582. ' Glob:=Glob;',
  21583. 'end;',
  21584. 'var r: TRec;',
  21585. 'begin',
  21586. ' trec.two:=trec.one;',
  21587. ' trec.Glob:=trec.Glob;',
  21588. ' with trec do begin',
  21589. ' two:=one;',
  21590. ' Glob:=Glob;',
  21591. ' end;',
  21592. ' r.two:=r.one;',
  21593. ' r.Glob:=r.Glob;',
  21594. ' with r do begin',
  21595. ' two:=one;',
  21596. ' Glob:=Glob;',
  21597. ' end;',
  21598. '']);
  21599. ConvertProgram;
  21600. CheckSource('TestRecordHelper_ClassVar',
  21601. LinesToStr([ // statements
  21602. 'rtl.recNewT($mod, "TRec", function () {',
  21603. ' this.$eq = function (b) {',
  21604. ' return true;',
  21605. ' };',
  21606. ' this.$assign = function (s) {',
  21607. ' return this;',
  21608. ' };',
  21609. '});',
  21610. 'rtl.createHelper($mod, "THelper", null, function () {',
  21611. ' this.One = 1;',
  21612. ' this.Two = 2;',
  21613. ' this.Glob = 0;',
  21614. ' this.Foo = function (w) {',
  21615. ' var Result = 0;',
  21616. ' Result = w;',
  21617. ' $mod.THelper.Two = 1 + w;',
  21618. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21619. ' Result = $mod.THelper.Glob;',
  21620. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21621. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21622. ' this.$assign(this);',
  21623. ' return Result;',
  21624. ' };',
  21625. ' this.Bar = function (w) {',
  21626. ' var Result = 0;',
  21627. ' Result = w;',
  21628. ' $mod.THelper.Two = 1;',
  21629. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21630. ' return Result;',
  21631. ' };',
  21632. '});',
  21633. 'this.r = $mod.TRec.$new();',
  21634. '']),
  21635. LinesToStr([ // $mod.$main
  21636. '$mod.THelper.Two = 1;',
  21637. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21638. 'var $with1 = $mod.TRec;',
  21639. '$mod.THelper.Two = 1;',
  21640. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21641. '$mod.THelper.Two = 1;',
  21642. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21643. 'var $with2 = $mod.r;',
  21644. '$mod.THelper.Two = 1;',
  21645. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21646. '']));
  21647. end;
  21648. procedure TTestModule.TestRecordHelper_Method_Call;
  21649. begin
  21650. StartProgram(false);
  21651. Add([
  21652. '{$modeswitch AdvancedRecords}',
  21653. 'type',
  21654. ' TRec = record',
  21655. ' procedure Run(w: word = 10);',
  21656. ' end;',
  21657. ' THelper = record helper for TRec',
  21658. ' function Foo(w: word = 1): word;',
  21659. ' end;',
  21660. 'procedure TRec.Run(w: word);',
  21661. 'begin',
  21662. ' Foo;',
  21663. ' Foo();',
  21664. ' Foo(2);',
  21665. ' Self.Foo;',
  21666. ' Self.Foo();',
  21667. ' Self.Foo(3);',
  21668. ' with Self do begin',
  21669. ' Foo;',
  21670. ' Foo();',
  21671. ' Foo(4);',
  21672. ' end;',
  21673. 'end;',
  21674. 'function THelper.foo(w: word): word;',
  21675. 'begin',
  21676. ' Run;',
  21677. ' Run();',
  21678. ' Run(11);',
  21679. ' Foo;',
  21680. ' Foo();',
  21681. ' Foo(12);',
  21682. ' Self.Foo;',
  21683. ' Self.Foo();',
  21684. ' Self.Foo(13);',
  21685. ' with Self do begin',
  21686. ' Foo;',
  21687. ' Foo();',
  21688. ' Foo(14);',
  21689. ' end;',
  21690. 'end;',
  21691. 'var Rec: TRec;',
  21692. 'begin',
  21693. ' Rec.Foo;',
  21694. ' Rec.Foo();',
  21695. ' Rec.Foo(21);',
  21696. ' with Rec do begin',
  21697. ' Foo;',
  21698. ' Foo();',
  21699. ' Foo(22);',
  21700. ' end;',
  21701. '']);
  21702. ConvertProgram;
  21703. CheckSource('TestRecordHelper_Method_Call',
  21704. LinesToStr([ // statements
  21705. 'rtl.recNewT($mod, "TRec", function () {',
  21706. ' this.$eq = function (b) {',
  21707. ' return true;',
  21708. ' };',
  21709. ' this.$assign = function (s) {',
  21710. ' return this;',
  21711. ' };',
  21712. ' this.Run = function (w) {',
  21713. ' $mod.THelper.Foo.call(this, 1);',
  21714. ' $mod.THelper.Foo.call(this, 1);',
  21715. ' $mod.THelper.Foo.call(this, 2);',
  21716. ' $mod.THelper.Foo.call(this, 1);',
  21717. ' $mod.THelper.Foo.call(this, 1);',
  21718. ' $mod.THelper.Foo.call(this, 3);',
  21719. ' $mod.THelper.Foo.call(this, 1);',
  21720. ' $mod.THelper.Foo.call(this, 1);',
  21721. ' $mod.THelper.Foo.call(this, 4);',
  21722. ' };',
  21723. '});',
  21724. 'rtl.createHelper($mod, "THelper", null, function () {',
  21725. ' this.Foo = function (w) {',
  21726. ' var Result = 0;',
  21727. ' this.Run(10);',
  21728. ' this.Run(10);',
  21729. ' this.Run(11);',
  21730. ' $mod.THelper.Foo.call(this, 1);',
  21731. ' $mod.THelper.Foo.call(this, 1);',
  21732. ' $mod.THelper.Foo.call(this, 12);',
  21733. ' $mod.THelper.Foo.call(this, 1);',
  21734. ' $mod.THelper.Foo.call(this, 1);',
  21735. ' $mod.THelper.Foo.call(this, 13);',
  21736. ' $mod.THelper.Foo.call(this, 1);',
  21737. ' $mod.THelper.Foo.call(this, 1);',
  21738. ' $mod.THelper.Foo.call(this, 14);',
  21739. ' return Result;',
  21740. ' };',
  21741. '});',
  21742. 'this.Rec = $mod.TRec.$new();',
  21743. '']),
  21744. LinesToStr([ // $mod.$main
  21745. '$mod.THelper.Foo.call($mod.Rec, 1);',
  21746. '$mod.THelper.Foo.call($mod.Rec, 1);',
  21747. '$mod.THelper.Foo.call($mod.Rec, 21);',
  21748. 'var $with1 = $mod.Rec;',
  21749. '$mod.THelper.Foo.call($with1, 1);',
  21750. '$mod.THelper.Foo.call($with1, 1);',
  21751. '$mod.THelper.Foo.call($with1, 22);',
  21752. '']));
  21753. end;
  21754. procedure TTestModule.TestRecordHelper_Constructor;
  21755. begin
  21756. StartProgram(false);
  21757. Add([
  21758. '{$modeswitch AdvancedRecords}',
  21759. 'type',
  21760. ' TRec = record',
  21761. ' constructor Create(w: word);',
  21762. ' end;',
  21763. ' THelper = record helper for TRec',
  21764. ' constructor NewHlp(w: word);',
  21765. ' end;',
  21766. 'var',
  21767. ' Rec: TRec;',
  21768. 'constructor TRec.Create(w: word);',
  21769. 'begin',
  21770. ' NewHlp(2);', // normal call
  21771. ' trec.NewHlp(3);', // new instance
  21772. 'end;',
  21773. 'constructor THelper.NewHlp(w: word);',
  21774. 'begin',
  21775. ' create(2);', // normal call
  21776. ' trec.create(3);', // new instance
  21777. ' NewHlp(4);', // normal call
  21778. ' trec.NewHlp(5);', // new instance
  21779. 'end;',
  21780. 'begin',
  21781. ' rec.newhlp(2);', // normal call
  21782. ' with rec do newhlp(12);', // normal call
  21783. ' trec.newhlp(3);', // new instance
  21784. ' with trec do newhlp(13);', // new instance
  21785. '']);
  21786. ConvertProgram;
  21787. CheckSource('TestRecordHelper_Constructor',
  21788. LinesToStr([ // statements
  21789. 'rtl.recNewT($mod, "TRec", function () {',
  21790. ' this.$eq = function (b) {',
  21791. ' return true;',
  21792. ' };',
  21793. ' this.$assign = function (s) {',
  21794. ' return this;',
  21795. ' };',
  21796. ' this.Create = function (w) {',
  21797. ' $mod.THelper.NewHlp.call(this, 2);',
  21798. ' $mod.THelper.$new("NewHlp", [3]);',
  21799. ' return this;',
  21800. ' };',
  21801. '}, true);',
  21802. 'rtl.createHelper($mod, "THelper", null, function () {',
  21803. ' this.NewHlp = function (w) {',
  21804. ' this.Create(2);',
  21805. ' $mod.TRec.$new().Create(3);',
  21806. ' $mod.THelper.NewHlp.call(this, 4);',
  21807. ' $mod.THelper.$new("NewHlp", [5]);',
  21808. ' return this;',
  21809. ' };',
  21810. ' this.$new = function (fn, args) {',
  21811. ' return this[fn].apply($mod.TRec.$new(), args);',
  21812. ' };',
  21813. '});',
  21814. 'this.Rec = $mod.TRec.$new();',
  21815. '']),
  21816. LinesToStr([ // $mod.$main
  21817. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  21818. 'var $with1 = $mod.Rec;',
  21819. '$mod.THelper.NewHlp.call($with1, 12);',
  21820. '$mod.THelper.$new("NewHlp", [3]);',
  21821. 'var $with2 = $mod.TRec;',
  21822. '$mod.THelper.$new("NewHlp", [13]);',
  21823. '']));
  21824. end;
  21825. procedure TTestModule.TestTypeHelper_ClassVar;
  21826. begin
  21827. StartProgram(false);
  21828. Add([
  21829. '{$modeswitch typehelpers}',
  21830. 'type',
  21831. ' THelper = type helper for byte',
  21832. ' const',
  21833. ' One = 1;',
  21834. ' Two: word = 2;',
  21835. ' class var',
  21836. ' Glob: word;',
  21837. ' function Foo(w: word): word;',
  21838. ' class function Bar(w: word): word; static;',
  21839. ' end;',
  21840. 'function THelper.foo(w: word): word;',
  21841. 'begin',
  21842. ' Result:=w;',
  21843. ' Two:=One+w;',
  21844. ' Glob:=Glob;',
  21845. ' Result:=Self.Glob;',
  21846. ' Self.Glob:=Self.Glob;',
  21847. ' with Self do Glob:=Glob;',
  21848. 'end;',
  21849. 'class function THelper.bar(w: word): word;',
  21850. 'begin',
  21851. ' Result:=w;',
  21852. ' Two:=One;',
  21853. ' Glob:=Glob;',
  21854. 'end;',
  21855. 'var b: byte;',
  21856. 'begin',
  21857. ' byte.two:=byte.one;',
  21858. ' byte.Glob:=byte.Glob;',
  21859. ' with byte do begin',
  21860. ' two:=one;',
  21861. ' Glob:=Glob;',
  21862. ' end;',
  21863. ' b.two:=b.one;',
  21864. ' b.Glob:=b.Glob;',
  21865. ' with b do begin',
  21866. ' two:=one;',
  21867. ' Glob:=Glob;',
  21868. ' end;',
  21869. '']);
  21870. ConvertProgram;
  21871. CheckSource('TestTypeHelper_ClassVar',
  21872. LinesToStr([ // statements
  21873. 'rtl.createHelper($mod, "THelper", null, function () {',
  21874. ' this.One = 1;',
  21875. ' this.Two = 2;',
  21876. ' this.Glob = 0;',
  21877. ' this.Foo = function (w) {',
  21878. ' var Result = 0;',
  21879. ' Result = w;',
  21880. ' $mod.THelper.Two = 1 + w;',
  21881. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21882. ' Result = $mod.THelper.Glob;',
  21883. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21884. ' var $with1 = this.get();',
  21885. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21886. ' return Result;',
  21887. ' };',
  21888. ' this.Bar = function (w) {',
  21889. ' var Result = 0;',
  21890. ' Result = w;',
  21891. ' $mod.THelper.Two = 1;',
  21892. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21893. ' return Result;',
  21894. ' };',
  21895. '});',
  21896. 'this.b = 0;',
  21897. '']),
  21898. LinesToStr([ // $mod.$main
  21899. '$mod.THelper.Two = 1;',
  21900. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21901. '$mod.THelper.Two = 1;',
  21902. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21903. '$mod.THelper.Two = 1;',
  21904. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21905. 'var $with1 = $mod.b;',
  21906. '$mod.THelper.Two = 1;',
  21907. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21908. '']));
  21909. end;
  21910. procedure TTestModule.TestTypeHelper_PassResultElement;
  21911. begin
  21912. StartProgram(false);
  21913. Add([
  21914. '{$modeswitch typehelpers}',
  21915. 'type',
  21916. ' THelper = type helper for word',
  21917. ' procedure DoIt(e: byte = 123);',
  21918. ' class procedure DoSome(e: byte = 456); static;',
  21919. ' end;',
  21920. 'procedure THelper.DoIt(e: byte);',
  21921. 'begin',
  21922. 'end;',
  21923. 'class procedure THelper.DoSome(e: byte);',
  21924. 'begin',
  21925. 'end;',
  21926. 'function Foo(w: word): word;',
  21927. 'begin',
  21928. ' Result.DoIt;',
  21929. ' Result.DoIt();',
  21930. ' Result.DoSome;',
  21931. ' Result.DoSome();',
  21932. ' with Result do begin',
  21933. ' DoIt;',
  21934. ' DoIt();',
  21935. ' DoSome;',
  21936. ' DoSome();',
  21937. ' end;',
  21938. 'end;',
  21939. 'begin',
  21940. '']);
  21941. ConvertProgram;
  21942. CheckSource('TestTypeHelper_PassResultElement',
  21943. LinesToStr([ // statements
  21944. 'rtl.createHelper($mod, "THelper", null, function () {',
  21945. ' this.DoIt = function (e) {',
  21946. ' };',
  21947. ' this.DoSome = function (e) {',
  21948. ' };',
  21949. '});',
  21950. 'this.Foo = function (w) {',
  21951. ' var Result = 0;',
  21952. ' $mod.THelper.DoIt.call({',
  21953. ' get: function () {',
  21954. ' return Result;',
  21955. ' },',
  21956. ' set: function (v) {',
  21957. ' Result = v;',
  21958. ' }',
  21959. ' }, 123);',
  21960. ' $mod.THelper.DoIt.call({',
  21961. ' get: function () {',
  21962. ' return Result;',
  21963. ' },',
  21964. ' set: function (v) {',
  21965. ' Result = v;',
  21966. ' }',
  21967. ' }, 123);',
  21968. ' $mod.THelper.DoSome(456);',
  21969. ' $mod.THelper.DoSome(456);',
  21970. ' $mod.THelper.DoIt.call({',
  21971. ' get: function () {',
  21972. ' return Result;',
  21973. ' },',
  21974. ' set: function (v) {',
  21975. ' Result = v;',
  21976. ' }',
  21977. ' }, 123);',
  21978. ' $mod.THelper.DoIt.call({',
  21979. ' get: function () {',
  21980. ' return Result;',
  21981. ' },',
  21982. ' set: function (v) {',
  21983. ' Result = v;',
  21984. ' }',
  21985. ' }, 123);',
  21986. ' $mod.THelper.DoSome(456);',
  21987. ' $mod.THelper.DoSome(456);',
  21988. ' return Result;',
  21989. '};',
  21990. '']),
  21991. LinesToStr([ // $mod.$main
  21992. '']));
  21993. end;
  21994. procedure TTestModule.TestTypeHelper_PassArgs;
  21995. begin
  21996. StartProgram(false);
  21997. Add([
  21998. '{$modeswitch typehelpers}',
  21999. 'type',
  22000. ' THelper = type helper for word',
  22001. ' procedure DoIt(e: byte = 123);',
  22002. ' end;',
  22003. 'procedure THelper.DoIt(e: byte);',
  22004. 'begin',
  22005. 'end;',
  22006. 'procedure FooDefault(a: word);',
  22007. 'begin',
  22008. ' a.DoIt;',
  22009. ' with a do DoIt;',
  22010. 'end;',
  22011. 'procedure FooConst(const a: word);',
  22012. 'begin',
  22013. ' a.DoIt;',
  22014. ' with a do DoIt;',
  22015. 'end;',
  22016. 'procedure FooVar(var a: word);',
  22017. 'begin',
  22018. ' a.DoIt;',
  22019. ' with a do DoIt;',
  22020. 'end;',
  22021. 'begin',
  22022. '']);
  22023. ConvertProgram;
  22024. CheckSource('TestTypeHelper_PassArgs',
  22025. LinesToStr([ // statements
  22026. 'rtl.createHelper($mod, "THelper", null, function () {',
  22027. ' this.DoIt = function (e) {',
  22028. ' };',
  22029. '});',
  22030. 'this.FooDefault = function (a) {',
  22031. ' $mod.THelper.DoIt.call({',
  22032. ' get: function () {',
  22033. ' return a;',
  22034. ' },',
  22035. ' set: function (v) {',
  22036. ' a = v;',
  22037. ' }',
  22038. ' }, 123);',
  22039. ' $mod.THelper.DoIt.call({',
  22040. ' get: function () {',
  22041. ' return a;',
  22042. ' },',
  22043. ' set: function (v) {',
  22044. ' a = v;',
  22045. ' }',
  22046. ' }, 123);',
  22047. '};',
  22048. 'this.FooConst = function (a) {',
  22049. ' $mod.THelper.DoIt.call({',
  22050. ' get: function () {',
  22051. ' return a;',
  22052. ' },',
  22053. ' set: function (v) {',
  22054. ' rtl.raiseE("EPropReadOnly");',
  22055. ' }',
  22056. ' }, 123);',
  22057. ' $mod.THelper.DoIt.call({',
  22058. ' get: function () {',
  22059. ' return a;',
  22060. ' },',
  22061. ' set: function () {',
  22062. ' rtl.raiseE("EPropReadOnly");',
  22063. ' }',
  22064. ' }, 123);',
  22065. '};',
  22066. 'this.FooVar = function (a) {',
  22067. ' $mod.THelper.DoIt.call(a, 123);',
  22068. ' var $with1 = a.get();',
  22069. ' $mod.THelper.DoIt.call(a, 123);',
  22070. '};',
  22071. '']),
  22072. LinesToStr([ // $mod.$main
  22073. '']));
  22074. end;
  22075. procedure TTestModule.TestTypeHelper_PassVarConst;
  22076. begin
  22077. StartProgram(false);
  22078. Add([
  22079. '{$modeswitch typehelpers}',
  22080. 'type',
  22081. ' THelper = type helper for word',
  22082. ' procedure DoIt(e: byte = 123);',
  22083. ' end;',
  22084. 'procedure THelper.DoIt(e: byte);',
  22085. 'begin',
  22086. 'end;',
  22087. 'var a: word;',
  22088. 'const c: word = 2;',
  22089. '{$writeableconst off}',
  22090. 'const r: word = 3;',
  22091. 'begin',
  22092. ' a.DoIt;',
  22093. ' with a do DoIt;',
  22094. ' c.DoIt;',
  22095. ' with c do DoIt;',
  22096. ' r.DoIt;',
  22097. ' with r do DoIt;',
  22098. '']);
  22099. ConvertProgram;
  22100. CheckSource('TestTypeHelper_PassVarConst',
  22101. LinesToStr([ // statements
  22102. 'rtl.createHelper($mod, "THelper", null, function () {',
  22103. ' this.DoIt = function (e) {',
  22104. ' };',
  22105. '});',
  22106. 'this.a = 0;',
  22107. 'this.c = 2;',
  22108. 'this.r = 3;',
  22109. '']),
  22110. LinesToStr([ // $mod.$main
  22111. '$mod.THelper.DoIt.call({',
  22112. ' p: $mod,',
  22113. ' get: function () {',
  22114. ' return this.p.a;',
  22115. ' },',
  22116. ' set: function (v) {',
  22117. ' this.p.a = v;',
  22118. ' }',
  22119. '}, 123);',
  22120. 'var $with1 = $mod.a;',
  22121. '$mod.THelper.DoIt.call({',
  22122. ' get: function () {',
  22123. ' return $with1;',
  22124. ' },',
  22125. ' set: function (v) {',
  22126. ' $with1 = v;',
  22127. ' }',
  22128. '}, 123);',
  22129. '$mod.THelper.DoIt.call({',
  22130. ' p: $mod,',
  22131. ' get: function () {',
  22132. ' return this.p.c;',
  22133. ' },',
  22134. ' set: function (v) {',
  22135. ' this.p.c = v;',
  22136. ' }',
  22137. '}, 123);',
  22138. 'var $with2 = $mod.c;',
  22139. '$mod.THelper.DoIt.call({',
  22140. ' get: function () {',
  22141. ' return $with2;',
  22142. ' },',
  22143. ' set: function (v) {',
  22144. ' $with2 = v;',
  22145. ' }',
  22146. '}, 123);',
  22147. '$mod.THelper.DoIt.call({',
  22148. ' get: function () {',
  22149. ' return 3;',
  22150. ' },',
  22151. ' set: function (v) {',
  22152. ' rtl.raiseE("EPropReadOnly");',
  22153. ' }',
  22154. '}, 123);',
  22155. 'var $with3 = 3;',
  22156. ' $mod.THelper.DoIt.call({',
  22157. ' get: function () {',
  22158. ' return $with3;',
  22159. ' },',
  22160. ' set: function () {',
  22161. ' rtl.raiseE("EPropReadOnly");',
  22162. ' }',
  22163. ' }, 123);',
  22164. '']));
  22165. end;
  22166. procedure TTestModule.TestTypeHelper_PassFuncResult;
  22167. begin
  22168. StartProgram(false);
  22169. Add([
  22170. '{$modeswitch typehelpers}',
  22171. 'type',
  22172. ' THelper = type helper for word',
  22173. ' procedure DoIt(e: byte = 123);',
  22174. ' end;',
  22175. 'procedure THelper.DoIt(e: byte);',
  22176. 'begin',
  22177. 'end;',
  22178. 'function Foo(b: byte = 1): word;',
  22179. 'begin',
  22180. 'end;',
  22181. 'begin',
  22182. ' Foo.DoIt;',
  22183. ' Foo().DoIt;',
  22184. ' with Foo do DoIt;',
  22185. ' with Foo() do DoIt;',
  22186. '']);
  22187. ConvertProgram;
  22188. CheckSource('TestTypeHelper_PassFuncResult',
  22189. LinesToStr([ // statements
  22190. 'rtl.createHelper($mod, "THelper", null, function () {',
  22191. ' this.DoIt = function (e) {',
  22192. ' };',
  22193. '});',
  22194. 'this.Foo = function (b) {',
  22195. ' var Result = 0;',
  22196. ' return Result;',
  22197. '};',
  22198. '']),
  22199. LinesToStr([ // $mod.$main
  22200. '$mod.THelper.DoIt.call({',
  22201. ' a: $mod.Foo(1),',
  22202. ' get: function () {',
  22203. ' return this.a;',
  22204. ' },',
  22205. ' set: function (v) {',
  22206. ' this.a = v;',
  22207. ' }',
  22208. '}, 123);',
  22209. '$mod.THelper.DoIt.call({',
  22210. ' a: $mod.Foo(1),',
  22211. ' get: function () {',
  22212. ' return this.a;',
  22213. ' },',
  22214. ' set: function (v) {',
  22215. ' this.a = v;',
  22216. ' }',
  22217. '}, 123);',
  22218. 'var $with1 = $mod.Foo(1);',
  22219. '$mod.THelper.DoIt.call({',
  22220. ' get: function () {',
  22221. ' return $with1;',
  22222. ' },',
  22223. ' set: function (v) {',
  22224. ' $with1 = v;',
  22225. ' }',
  22226. '}, 123);',
  22227. 'var $with2 = $mod.Foo(1);',
  22228. '$mod.THelper.DoIt.call({',
  22229. ' get: function () {',
  22230. ' return $with2;',
  22231. ' },',
  22232. ' set: function (v) {',
  22233. ' $with2 = v;',
  22234. ' }',
  22235. '}, 123);',
  22236. '']));
  22237. end;
  22238. procedure TTestModule.TestTypeHelper_PassPropertyField;
  22239. begin
  22240. StartProgram(false);
  22241. Add([
  22242. '{$modeswitch typehelpers}',
  22243. 'type',
  22244. ' TObject = class',
  22245. ' FField: word;',
  22246. ' procedure SetField(Value: word);',
  22247. ' property Field: word read FField write SetField;',
  22248. ' end;',
  22249. ' THelper = type helper for word',
  22250. ' procedure Fly;',
  22251. ' class procedure Run; static;',
  22252. ' end;',
  22253. 'procedure TObject.SetField(Value: word);',
  22254. 'begin',
  22255. ' Field.Fly;',
  22256. ' Field.Run;',
  22257. ' Self.Field.Fly;',
  22258. ' Self.Field.Run;',
  22259. ' with Self do begin',
  22260. ' Field.Fly;',
  22261. ' Field.Run;',
  22262. ' end;',
  22263. ' with Self.Field do begin',
  22264. ' Fly;',
  22265. ' Run;',
  22266. ' end;',
  22267. 'end;',
  22268. 'procedure THelper.Fly;',
  22269. 'begin',
  22270. 'end;',
  22271. 'class procedure THelper.Run;',
  22272. 'begin',
  22273. 'end;',
  22274. 'var',
  22275. ' o: TObject;',
  22276. 'begin',
  22277. ' o.Field.Fly;',
  22278. ' o.Field.Run;',
  22279. ' with o do begin',
  22280. ' Field.Fly;',
  22281. ' Field.Run;',
  22282. ' end;',
  22283. ' with o.Field do begin',
  22284. ' Fly;',
  22285. ' Run;',
  22286. ' end;',
  22287. '']);
  22288. ConvertProgram;
  22289. CheckSource('TestTypeHelper_PassPropertyField',
  22290. LinesToStr([ // statements
  22291. 'rtl.createClass($mod, "TObject", null, function () {',
  22292. ' this.$init = function () {',
  22293. ' this.FField = 0;',
  22294. ' };',
  22295. ' this.$final = function () {',
  22296. ' };',
  22297. ' this.SetField = function (Value) {',
  22298. ' $mod.THelper.Fly.call({',
  22299. ' p: this,',
  22300. ' get: function () {',
  22301. ' return this.p.FField;',
  22302. ' },',
  22303. ' set: function (v) {',
  22304. ' this.p.FField = v;',
  22305. ' }',
  22306. ' });',
  22307. ' $mod.THelper.Run();',
  22308. ' $mod.THelper.Fly.call({',
  22309. ' p: this,',
  22310. ' get: function () {',
  22311. ' return this.p.FField;',
  22312. ' },',
  22313. ' set: function (v) {',
  22314. ' this.p.FField = v;',
  22315. ' }',
  22316. ' });',
  22317. ' $mod.THelper.Run();',
  22318. ' $mod.THelper.Fly.call({',
  22319. ' p: this,',
  22320. ' get: function () {',
  22321. ' return this.p.FField;',
  22322. ' },',
  22323. ' set: function (v) {',
  22324. ' this.p.FField = v;',
  22325. ' }',
  22326. ' });',
  22327. ' $mod.THelper.Run();',
  22328. ' var $with1 = this.FField;',
  22329. ' $mod.THelper.Fly.call({',
  22330. ' get: function () {',
  22331. ' return $with1;',
  22332. ' },',
  22333. ' set: function (v) {',
  22334. ' $with1 = v;',
  22335. ' }',
  22336. ' });',
  22337. ' $mod.THelper.Run();',
  22338. ' };',
  22339. '});',
  22340. 'rtl.createHelper($mod, "THelper", null, function () {',
  22341. ' this.Fly = function () {',
  22342. ' };',
  22343. ' this.Run = function () {',
  22344. ' };',
  22345. '});',
  22346. 'this.o = null;',
  22347. '']),
  22348. LinesToStr([ // $mod.$main
  22349. '$mod.THelper.Fly.call({',
  22350. ' p: $mod.o,',
  22351. ' get: function () {',
  22352. ' return this.p.FField;',
  22353. ' },',
  22354. ' set: function (v) {',
  22355. ' this.p.FField = v;',
  22356. ' }',
  22357. '});',
  22358. '$mod.THelper.Run();',
  22359. 'var $with1 = $mod.o;',
  22360. '$mod.THelper.Fly.call({',
  22361. ' p: $with1,',
  22362. ' get: function () {',
  22363. ' return this.p.FField;',
  22364. ' },',
  22365. ' set: function (v) {',
  22366. ' this.p.FField = v;',
  22367. ' }',
  22368. '});',
  22369. '$mod.THelper.Run();',
  22370. 'var $with2 = $mod.o.FField;',
  22371. '$mod.THelper.Fly.call({',
  22372. ' get: function () {',
  22373. ' return $with2;',
  22374. ' },',
  22375. ' set: function (v) {',
  22376. ' $with2 = v;',
  22377. ' }',
  22378. '});',
  22379. '$mod.THelper.Run();',
  22380. '']));
  22381. end;
  22382. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  22383. begin
  22384. StartProgram(false);
  22385. Add([
  22386. '{$modeswitch typehelpers}',
  22387. 'type',
  22388. ' TObject = class',
  22389. ' FField: word;',
  22390. ' function GetField: word;',
  22391. ' property Field: word read GetField write FField;',
  22392. ' end;',
  22393. ' THelper = type helper for word',
  22394. ' procedure Fly;',
  22395. ' class procedure Run; static;',
  22396. ' end;',
  22397. 'function TObject.GetField: word;',
  22398. 'begin',
  22399. ' Field.Fly;',
  22400. ' Field.Run;',
  22401. ' Self.Field.Fly;',
  22402. ' Self.Field.Run;',
  22403. ' with Self do begin',
  22404. ' Field.Fly;',
  22405. ' Field.Run;',
  22406. ' end;',
  22407. ' with Self.Field do begin',
  22408. ' Fly;',
  22409. ' Run;',
  22410. ' end;',
  22411. 'end;',
  22412. 'procedure THelper.Fly;',
  22413. 'begin',
  22414. 'end;',
  22415. 'class procedure THelper.Run;',
  22416. 'begin',
  22417. 'end;',
  22418. 'var',
  22419. ' o: TObject;',
  22420. 'begin',
  22421. ' o.Field.Fly;',
  22422. ' o.Field.Run;',
  22423. ' with o do begin',
  22424. ' Field.Fly;',
  22425. ' Field.Run;',
  22426. ' end;',
  22427. ' with o.Field do begin',
  22428. ' Fly;',
  22429. ' Run;',
  22430. ' end;',
  22431. '']);
  22432. ConvertProgram;
  22433. CheckSource('TestTypeHelper_PassPropertyGetter',
  22434. LinesToStr([ // statements
  22435. 'rtl.createClass($mod, "TObject", null, function () {',
  22436. ' this.$init = function () {',
  22437. ' this.FField = 0;',
  22438. ' };',
  22439. ' this.$final = function () {',
  22440. ' };',
  22441. ' this.GetField = function () {',
  22442. ' var Result = 0;',
  22443. ' $mod.THelper.Fly.call({',
  22444. ' p: this.GetField(),',
  22445. ' get: function () {',
  22446. ' return this.p;',
  22447. ' },',
  22448. ' set: function (v) {',
  22449. ' this.p = v;',
  22450. ' }',
  22451. ' });',
  22452. ' $mod.THelper.Run();',
  22453. ' $mod.THelper.Fly.call({',
  22454. ' p: this.GetField(),',
  22455. ' get: function () {',
  22456. ' return this.p;',
  22457. ' },',
  22458. ' set: function (v) {',
  22459. ' this.p = v;',
  22460. ' }',
  22461. ' });',
  22462. ' $mod.THelper.Run();',
  22463. ' $mod.THelper.Fly.call({',
  22464. ' p: this.GetField(),',
  22465. ' get: function () {',
  22466. ' return this.p;',
  22467. ' },',
  22468. ' set: function (v) {',
  22469. ' this.p = v;',
  22470. ' }',
  22471. ' });',
  22472. ' $mod.THelper.Run();',
  22473. ' var $with1 = this.GetField();',
  22474. ' $mod.THelper.Fly.call({',
  22475. ' get: function () {',
  22476. ' return $with1;',
  22477. ' },',
  22478. ' set: function (v) {',
  22479. ' $with1 = v;',
  22480. ' }',
  22481. ' });',
  22482. ' $mod.THelper.Run();',
  22483. ' return Result;',
  22484. ' };',
  22485. '});',
  22486. 'rtl.createHelper($mod, "THelper", null, function () {',
  22487. ' this.Fly = function () {',
  22488. ' };',
  22489. ' this.Run = function () {',
  22490. ' };',
  22491. '});',
  22492. 'this.o = null;',
  22493. '']),
  22494. LinesToStr([ // $mod.$main
  22495. '$mod.THelper.Fly.call({',
  22496. ' p: $mod.o.GetField(),',
  22497. ' get: function () {',
  22498. ' return this.p;',
  22499. ' },',
  22500. ' set: function (v) {',
  22501. ' this.p = v;',
  22502. ' }',
  22503. '});',
  22504. '$mod.THelper.Run();',
  22505. 'var $with1 = $mod.o;',
  22506. '$mod.THelper.Fly.call({',
  22507. ' p: $with1.GetField(),',
  22508. ' get: function () {',
  22509. ' return this.p;',
  22510. ' },',
  22511. ' set: function (v) {',
  22512. ' this.p = v;',
  22513. ' }',
  22514. '});',
  22515. '$mod.THelper.Run();',
  22516. 'var $with2 = $mod.o.GetField();',
  22517. '$mod.THelper.Fly.call({',
  22518. ' get: function () {',
  22519. ' return $with2;',
  22520. ' },',
  22521. ' set: function (v) {',
  22522. ' $with2 = v;',
  22523. ' }',
  22524. '});',
  22525. '$mod.THelper.Run();',
  22526. '']));
  22527. end;
  22528. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  22529. begin
  22530. StartProgram(false);
  22531. Add([
  22532. '{$modeswitch typehelpers}',
  22533. 'type',
  22534. ' TObject = class',
  22535. ' class var FField: word;',
  22536. ' class procedure SetField(Value: word);',
  22537. ' class property Field: word read FField write SetField;',
  22538. ' end;',
  22539. ' THelper = type helper for word',
  22540. ' procedure Fly(n: byte);',
  22541. ' end;',
  22542. 'class procedure TObject.SetField(Value: word);',
  22543. 'begin',
  22544. ' Field.Fly(1);',
  22545. ' Self.Field.Fly(2);',
  22546. ' with Self do Field.Fly(3);',
  22547. ' with Self.Field do Fly(4);',
  22548. ' TObject.Field.Fly(5);',
  22549. ' with TObject do Field.Fly(6);',
  22550. ' with TObject.Field do Fly(7);',
  22551. 'end;',
  22552. 'procedure THelper.Fly(n: byte);',
  22553. 'begin',
  22554. 'end;',
  22555. 'var',
  22556. ' o: TObject;',
  22557. 'begin',
  22558. ' o.Field.Fly(11);',
  22559. ' with o do Field.Fly(12);',
  22560. ' with o.Field do Fly(13);',
  22561. ' TObject.Field.Fly(14);',
  22562. ' with TObject do Field.Fly(15);',
  22563. ' with TObject.Field do Fly(16);',
  22564. '']);
  22565. ConvertProgram;
  22566. CheckSource('TestTypeHelper_PassClassPropertyField',
  22567. LinesToStr([ // statements
  22568. 'rtl.createClass($mod, "TObject", null, function () {',
  22569. ' this.FField = 0;',
  22570. ' this.$init = function () {',
  22571. ' };',
  22572. ' this.$final = function () {',
  22573. ' };',
  22574. ' this.SetField = function (Value) {',
  22575. ' $mod.THelper.Fly.call({',
  22576. ' p: this,',
  22577. ' get: function () {',
  22578. ' return this.p.FField;',
  22579. ' },',
  22580. ' set: function (v) {',
  22581. ' $mod.TObject.FField = v;',
  22582. ' }',
  22583. ' }, 1);',
  22584. ' $mod.THelper.Fly.call({',
  22585. ' p: this,',
  22586. ' get: function () {',
  22587. ' return this.p.FField;',
  22588. ' },',
  22589. ' set: function (v) {',
  22590. ' $mod.TObject.FField = v;',
  22591. ' }',
  22592. ' }, 2);',
  22593. ' $mod.THelper.Fly.call({',
  22594. ' p: this,',
  22595. ' get: function () {',
  22596. ' return this.p.FField;',
  22597. ' },',
  22598. ' set: function (v) {',
  22599. ' $mod.TObject.FField = v;',
  22600. ' }',
  22601. ' }, 3);',
  22602. ' var $with1 = this.FField;',
  22603. ' $mod.THelper.Fly.call({',
  22604. ' get: function () {',
  22605. ' return $with1;',
  22606. ' },',
  22607. ' set: function (v) {',
  22608. ' $with1 = v;',
  22609. ' }',
  22610. ' }, 4);',
  22611. ' $mod.THelper.Fly.call({',
  22612. ' p: $mod.TObject,',
  22613. ' get: function () {',
  22614. ' return this.p.FField;',
  22615. ' },',
  22616. ' set: function (v) {',
  22617. ' $mod.TObject.FField = v;',
  22618. ' }',
  22619. ' }, 5);',
  22620. ' var $with2 = $mod.TObject;',
  22621. ' $mod.THelper.Fly.call({',
  22622. ' p: $with2,',
  22623. ' get: function () {',
  22624. ' return this.p.FField;',
  22625. ' },',
  22626. ' set: function (v) {',
  22627. ' $mod.TObject.FField = v;',
  22628. ' }',
  22629. ' }, 6);',
  22630. ' var $with3 = $mod.TObject.FField;',
  22631. ' $mod.THelper.Fly.call({',
  22632. ' get: function () {',
  22633. ' return $with3;',
  22634. ' },',
  22635. ' set: function (v) {',
  22636. ' $with3 = v;',
  22637. ' }',
  22638. ' }, 7);',
  22639. ' };',
  22640. '});',
  22641. 'rtl.createHelper($mod, "THelper", null, function () {',
  22642. ' this.Fly = function (n) {',
  22643. ' };',
  22644. '});',
  22645. 'this.o = null;',
  22646. '']),
  22647. LinesToStr([ // $mod.$main
  22648. '$mod.THelper.Fly.call({',
  22649. ' p: $mod.o,',
  22650. ' get: function () {',
  22651. ' return this.p.FField;',
  22652. ' },',
  22653. ' set: function (v) {',
  22654. ' $mod.TObject.FField = v;',
  22655. ' }',
  22656. '}, 11);',
  22657. 'var $with1 = $mod.o;',
  22658. '$mod.THelper.Fly.call({',
  22659. ' p: $with1,',
  22660. ' get: function () {',
  22661. ' return this.p.FField;',
  22662. ' },',
  22663. ' set: function (v) {',
  22664. ' $mod.TObject.FField = v;',
  22665. ' }',
  22666. '}, 12);',
  22667. 'var $with2 = $mod.o.FField;',
  22668. '$mod.THelper.Fly.call({',
  22669. ' get: function () {',
  22670. ' return $with2;',
  22671. ' },',
  22672. ' set: function (v) {',
  22673. ' $with2 = v;',
  22674. ' }',
  22675. '}, 13);',
  22676. '$mod.THelper.Fly.call({',
  22677. ' p: $mod.TObject,',
  22678. ' get: function () {',
  22679. ' return this.p.FField;',
  22680. ' },',
  22681. ' set: function (v) {',
  22682. ' $mod.TObject.FField = v;',
  22683. ' }',
  22684. '}, 14);',
  22685. 'var $with3 = $mod.TObject;',
  22686. '$mod.THelper.Fly.call({',
  22687. ' p: $with3,',
  22688. ' get: function () {',
  22689. ' return this.p.FField;',
  22690. ' },',
  22691. ' set: function (v) {',
  22692. ' $mod.TObject.FField = v;',
  22693. ' }',
  22694. '}, 15);',
  22695. 'var $with4 = $mod.TObject.FField;',
  22696. '$mod.THelper.Fly.call({',
  22697. ' get: function () {',
  22698. ' return $with4;',
  22699. ' },',
  22700. ' set: function (v) {',
  22701. ' $with4 = v;',
  22702. ' }',
  22703. '}, 16);',
  22704. '']));
  22705. end;
  22706. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  22707. begin
  22708. StartProgram(false);
  22709. Add([
  22710. '{$modeswitch typehelpers}',
  22711. 'type',
  22712. ' TObject = class',
  22713. ' class var FField: word;',
  22714. ' class function GetField: word; static;',
  22715. ' class property Field: word read GetField write FField;',
  22716. ' end;',
  22717. ' THelper = type helper for word',
  22718. ' procedure Fly(n: byte);',
  22719. ' end;',
  22720. 'class function TObject.GetField: word;',
  22721. 'begin',
  22722. ' Field.Fly(1);',
  22723. ' TObject.Field.Fly(5);',
  22724. ' with TObject do Field.Fly(6);',
  22725. ' with TObject.Field do Fly(7);',
  22726. 'end;',
  22727. 'procedure THelper.Fly(n: byte);',
  22728. 'begin',
  22729. 'end;',
  22730. 'var',
  22731. ' o: TObject;',
  22732. 'begin',
  22733. ' o.Field.Fly(11);',
  22734. ' with o do Field.Fly(12);',
  22735. ' with o.Field do Fly(13);',
  22736. '']);
  22737. ConvertProgram;
  22738. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  22739. LinesToStr([ // statements
  22740. 'rtl.createClass($mod, "TObject", null, function () {',
  22741. ' this.FField = 0;',
  22742. ' this.$init = function () {',
  22743. ' };',
  22744. ' this.$final = function () {',
  22745. ' };',
  22746. ' this.GetField = function () {',
  22747. ' var Result = 0;',
  22748. ' $mod.THelper.Fly.call({',
  22749. ' p: $mod.TObject.GetField(),',
  22750. ' get: function () {',
  22751. ' return this.p;',
  22752. ' },',
  22753. ' set: function (v) {',
  22754. ' this.p = v;',
  22755. ' }',
  22756. ' }, 1);',
  22757. ' $mod.THelper.Fly.call({',
  22758. ' p: $mod.TObject.GetField(),',
  22759. ' get: function () {',
  22760. ' return this.p;',
  22761. ' },',
  22762. ' set: function (v) {',
  22763. ' this.p = v;',
  22764. ' }',
  22765. ' }, 5);',
  22766. ' var $with1 = $mod.TObject;',
  22767. ' $mod.THelper.Fly.call({',
  22768. ' p: $with1.GetField(),',
  22769. ' get: function () {',
  22770. ' return this.p;',
  22771. ' },',
  22772. ' set: function (v) {',
  22773. ' this.p = v;',
  22774. ' }',
  22775. ' }, 6);',
  22776. ' var $with2 = $mod.TObject.GetField();',
  22777. ' $mod.THelper.Fly.call({',
  22778. ' get: function () {',
  22779. ' return $with2;',
  22780. ' },',
  22781. ' set: function (v) {',
  22782. ' $with2 = v;',
  22783. ' }',
  22784. ' }, 7);',
  22785. ' return Result;',
  22786. ' };',
  22787. '});',
  22788. 'rtl.createHelper($mod, "THelper", null, function () {',
  22789. ' this.Fly = function (n) {',
  22790. ' };',
  22791. '});',
  22792. 'this.o = null;',
  22793. '']),
  22794. LinesToStr([ // $mod.$main
  22795. '$mod.THelper.Fly.call({',
  22796. ' p: $mod.o.GetField(),',
  22797. ' get: function () {',
  22798. ' return this.p;',
  22799. ' },',
  22800. ' set: function (v) {',
  22801. ' this.p = v;',
  22802. ' }',
  22803. '}, 11);',
  22804. 'var $with1 = $mod.o;',
  22805. '$mod.THelper.Fly.call({',
  22806. ' p: $with1.GetField(),',
  22807. ' get: function () {',
  22808. ' return this.p;',
  22809. ' },',
  22810. ' set: function (v) {',
  22811. ' this.p = v;',
  22812. ' }',
  22813. '}, 12);',
  22814. 'var $with2 = $mod.o.GetField();',
  22815. '$mod.THelper.Fly.call({',
  22816. ' get: function () {',
  22817. ' return $with2;',
  22818. ' },',
  22819. ' set: function (v) {',
  22820. ' $with2 = v;',
  22821. ' }',
  22822. '}, 13);',
  22823. '']));
  22824. end;
  22825. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  22826. begin
  22827. StartProgram(false);
  22828. Add([
  22829. '{$modeswitch typehelpers}',
  22830. 'type',
  22831. ' TObject = class',
  22832. ' class var FField: word;',
  22833. ' class function GetField: word;',
  22834. ' class property Field: word read GetField write FField;',
  22835. ' end;',
  22836. ' TClass = class of TObject;',
  22837. ' THelper = type helper for word',
  22838. ' procedure Fly(n: byte);',
  22839. ' end;',
  22840. 'class function TObject.GetField: word;',
  22841. 'begin',
  22842. ' Field.Fly(1);',
  22843. ' Self.Field.Fly(5);',
  22844. ' with Self do Field.Fly(6);',
  22845. ' with Self.Field do Fly(7);',
  22846. 'end;',
  22847. 'procedure THelper.Fly(n: byte);',
  22848. 'begin',
  22849. 'end;',
  22850. 'var',
  22851. ' o: TObject;',
  22852. ' c: TClass;',
  22853. 'begin',
  22854. ' o.Field.Fly(11);',
  22855. ' with o do Field.Fly(12);',
  22856. ' with o.Field do Fly(13);',
  22857. ' c.Field.Fly(14);',
  22858. ' with c do Field.Fly(15);',
  22859. ' with c.Field do Fly(16);',
  22860. '']);
  22861. ConvertProgram;
  22862. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  22863. LinesToStr([ // statements
  22864. 'rtl.createClass($mod, "TObject", null, function () {',
  22865. ' this.FField = 0;',
  22866. ' this.$init = function () {',
  22867. ' };',
  22868. ' this.$final = function () {',
  22869. ' };',
  22870. ' this.GetField = function () {',
  22871. ' var Result = 0;',
  22872. ' $mod.THelper.Fly.call({',
  22873. ' p: this.GetField(),',
  22874. ' get: function () {',
  22875. ' return this.p;',
  22876. ' },',
  22877. ' set: function (v) {',
  22878. ' this.p = v;',
  22879. ' }',
  22880. ' }, 1);',
  22881. ' $mod.THelper.Fly.call({',
  22882. ' p: this.GetField(),',
  22883. ' get: function () {',
  22884. ' return this.p;',
  22885. ' },',
  22886. ' set: function (v) {',
  22887. ' this.p = v;',
  22888. ' }',
  22889. ' }, 5);',
  22890. ' $mod.THelper.Fly.call({',
  22891. ' p: this.GetField(),',
  22892. ' get: function () {',
  22893. ' return this.p;',
  22894. ' },',
  22895. ' set: function (v) {',
  22896. ' this.p = v;',
  22897. ' }',
  22898. ' }, 6);',
  22899. ' var $with1 = this.GetField();',
  22900. ' $mod.THelper.Fly.call({',
  22901. ' get: function () {',
  22902. ' return $with1;',
  22903. ' },',
  22904. ' set: function (v) {',
  22905. ' $with1 = v;',
  22906. ' }',
  22907. ' }, 7);',
  22908. ' return Result;',
  22909. ' };',
  22910. '});',
  22911. 'rtl.createHelper($mod, "THelper", null, function () {',
  22912. ' this.Fly = function (n) {',
  22913. ' };',
  22914. '});',
  22915. 'this.o = null;',
  22916. 'this.c = null;',
  22917. '']),
  22918. LinesToStr([ // $mod.$main
  22919. '$mod.THelper.Fly.call({',
  22920. ' p: $mod.o.$class.GetField(),',
  22921. ' get: function () {',
  22922. ' return this.p;',
  22923. ' },',
  22924. ' set: function (v) {',
  22925. ' this.p = v;',
  22926. ' }',
  22927. '}, 11);',
  22928. 'var $with1 = $mod.o;',
  22929. '$mod.THelper.Fly.call({',
  22930. ' p: $with1.$class.GetField(),',
  22931. ' get: function () {',
  22932. ' return this.p;',
  22933. ' },',
  22934. ' set: function (v) {',
  22935. ' this.p = v;',
  22936. ' }',
  22937. '}, 12);',
  22938. 'var $with2 = $mod.o.$class.GetField();',
  22939. '$mod.THelper.Fly.call({',
  22940. ' get: function () {',
  22941. ' return $with2;',
  22942. ' },',
  22943. ' set: function (v) {',
  22944. ' $with2 = v;',
  22945. ' }',
  22946. '}, 13);',
  22947. '$mod.THelper.Fly.call({',
  22948. ' p: $mod.c.GetField(),',
  22949. ' get: function () {',
  22950. ' return this.p;',
  22951. ' },',
  22952. ' set: function (v) {',
  22953. ' this.p = v;',
  22954. ' }',
  22955. '}, 14);',
  22956. 'var $with3 = $mod.c;',
  22957. '$mod.THelper.Fly.call({',
  22958. ' p: $with3.GetField(),',
  22959. ' get: function () {',
  22960. ' return this.p;',
  22961. ' },',
  22962. ' set: function (v) {',
  22963. ' this.p = v;',
  22964. ' }',
  22965. '}, 15);',
  22966. 'var $with4 = $mod.c.GetField();',
  22967. '$mod.THelper.Fly.call({',
  22968. ' get: function () {',
  22969. ' return $with4;',
  22970. ' },',
  22971. ' set: function (v) {',
  22972. ' $with4 = v;',
  22973. ' }',
  22974. '}, 16);',
  22975. '']));
  22976. end;
  22977. procedure TTestModule.TestTypeHelper_Property;
  22978. begin
  22979. StartProgram(false);
  22980. Add([
  22981. '{$modeswitch typehelpers}',
  22982. 'type',
  22983. ' THelper = type helper for word',
  22984. ' function GetSize: longint;',
  22985. ' procedure SetSize(Value: longint);',
  22986. ' property Size: longint read GetSize write SetSize;',
  22987. ' end;',
  22988. 'function THelper.GetSize: longint;',
  22989. 'begin',
  22990. ' Result:=Size+1;',
  22991. ' Size:=2;',
  22992. ' Result:=Self.Size+3;',
  22993. ' Self.Size:=4;',
  22994. ' with Self do begin',
  22995. ' Result:=Size+5;',
  22996. ' Size:=6;',
  22997. ' end;',
  22998. 'end;',
  22999. 'procedure THelper.SetSize(Value: longint);',
  23000. 'begin',
  23001. 'end;',
  23002. 'var w: word;',
  23003. 'begin',
  23004. ' w:=w.Size+7;',
  23005. ' w.Size:=w+8;',
  23006. ' with w do begin',
  23007. ' w:=Size+9;',
  23008. ' Size:=w+10;',
  23009. ' end;',
  23010. '']);
  23011. ConvertProgram;
  23012. CheckSource('TestTypeHelper_Property',
  23013. LinesToStr([ // statements
  23014. 'rtl.createHelper($mod, "THelper", null, function () {',
  23015. ' this.GetSize = function () {',
  23016. ' var Result = 0;',
  23017. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  23018. ' $mod.THelper.SetSize.call(this, 2);',
  23019. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  23020. ' $mod.THelper.SetSize.call(this, 4);',
  23021. ' var $with1 = this.get();',
  23022. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  23023. ' $mod.THelper.SetSize.call(this, 6);',
  23024. ' return Result;',
  23025. ' };',
  23026. ' this.SetSize = function (Value) {',
  23027. ' };',
  23028. '});',
  23029. 'this.w = 0;',
  23030. '']),
  23031. LinesToStr([ // $mod.$main
  23032. '$mod.w = $mod.THelper.GetSize.call({',
  23033. ' p: $mod,',
  23034. ' get: function () {',
  23035. ' return this.p.w;',
  23036. ' },',
  23037. ' set: function (v) {',
  23038. ' this.p.w = v;',
  23039. ' }',
  23040. '}) + 7;',
  23041. '$mod.THelper.SetSize.call({',
  23042. ' p: $mod,',
  23043. ' get: function () {',
  23044. ' return this.p.w;',
  23045. ' },',
  23046. ' set: function (v) {',
  23047. ' this.p.w = v;',
  23048. ' }',
  23049. '}, $mod.w + 8);',
  23050. 'var $with1 = $mod.w;',
  23051. '$mod.w = $mod.THelper.GetSize.call({',
  23052. ' get: function () {',
  23053. ' return $with1;',
  23054. ' },',
  23055. ' set: function (v) {',
  23056. ' $with1 = v;',
  23057. ' }',
  23058. '}) + 9;',
  23059. '$mod.THelper.SetSize.call({',
  23060. ' get: function () {',
  23061. ' return $with1;',
  23062. ' },',
  23063. ' set: function (v) {',
  23064. ' $with1 = v;',
  23065. ' }',
  23066. '}, $mod.w + 10);',
  23067. '']));
  23068. end;
  23069. procedure TTestModule.TestTypeHelper_Property_Array;
  23070. begin
  23071. StartProgram(false);
  23072. Add([
  23073. '{$modeswitch typehelpers}',
  23074. 'type',
  23075. ' THelper = type helper for word',
  23076. ' function GetItems(Index: byte): boolean;',
  23077. ' procedure SetItems(Index: byte; Value: boolean);',
  23078. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  23079. ' end;',
  23080. 'function THelper.GetItems(Index: byte): boolean;',
  23081. 'begin',
  23082. ' Result:=Items[1];',
  23083. ' Items[2]:=false;',
  23084. ' Result:=Self.Items[3];',
  23085. ' Self.Items[4]:=true;',
  23086. ' with Self do begin',
  23087. ' Result:=Items[5];',
  23088. ' Items[6]:=false;',
  23089. ' end;',
  23090. 'end;',
  23091. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  23092. 'begin',
  23093. 'end;',
  23094. 'var',
  23095. ' w: word;',
  23096. ' b: boolean;',
  23097. 'begin',
  23098. ' b:=w.Items[1];',
  23099. ' w.Items[2]:=b;',
  23100. ' with w do begin',
  23101. ' b:=Items[3];',
  23102. ' Items[4]:=b;',
  23103. ' end;',
  23104. '']);
  23105. ConvertProgram;
  23106. CheckSource('TestTypeHelper_Property_Array',
  23107. LinesToStr([ // statements
  23108. 'rtl.createHelper($mod, "THelper", null, function () {',
  23109. ' this.GetItems = function (Index) {',
  23110. ' var Result = false;',
  23111. ' Result = $mod.THelper.GetItems.call(this, 1);',
  23112. ' $mod.THelper.SetItems.call(this, 2, false);',
  23113. ' Result = $mod.THelper.GetItems.call(this, 3);',
  23114. ' $mod.THelper.SetItems.call(this, 4, true);',
  23115. ' var $with1 = this.get();',
  23116. ' Result = $mod.THelper.GetItems.call(this, 5);',
  23117. ' $mod.THelper.SetItems.call(this, 6, false);',
  23118. ' return Result;',
  23119. ' };',
  23120. ' this.SetItems = function (Index, Value) {',
  23121. ' };',
  23122. '});',
  23123. 'this.w = 0;',
  23124. 'this.b = false;',
  23125. '']),
  23126. LinesToStr([ // $mod.$main
  23127. '$mod.b = $mod.THelper.GetItems.call({',
  23128. ' p: $mod,',
  23129. ' get: function () {',
  23130. ' return this.p.w;',
  23131. ' },',
  23132. ' set: function (v) {',
  23133. ' this.p.w = v;',
  23134. ' }',
  23135. '}, 1);',
  23136. '$mod.THelper.SetItems.call({',
  23137. ' p: $mod,',
  23138. ' get: function () {',
  23139. ' return this.p.w;',
  23140. ' },',
  23141. ' set: function (v) {',
  23142. ' this.p.w = v;',
  23143. ' }',
  23144. '}, 2, $mod.b);',
  23145. 'var $with1 = $mod.w;',
  23146. '$mod.b = $mod.THelper.GetItems.call({',
  23147. ' get: function () {',
  23148. ' return $with1;',
  23149. ' },',
  23150. ' set: function (v) {',
  23151. ' $with1 = v;',
  23152. ' }',
  23153. '}, 3);',
  23154. '$mod.THelper.SetItems.call({',
  23155. ' get: function () {',
  23156. ' return $with1;',
  23157. ' },',
  23158. ' set: function (v) {',
  23159. ' $with1 = v;',
  23160. ' }',
  23161. '}, 4, $mod.b);',
  23162. '']));
  23163. end;
  23164. procedure TTestModule.TestTypeHelper_ClassProperty;
  23165. begin
  23166. StartProgram(false);
  23167. Add([
  23168. '{$modeswitch typehelpers}',
  23169. 'type',
  23170. ' THelper = type helper for word',
  23171. ' class function GetSize: longint; static;',
  23172. ' class procedure SetSize(Value: longint); static;',
  23173. ' class property Size: longint read GetSize write SetSize;',
  23174. ' end;',
  23175. 'class function THelper.GetSize: longint;',
  23176. 'begin',
  23177. ' Result:=Size+1;',
  23178. ' Size:=2;',
  23179. 'end;',
  23180. 'class procedure THelper.SetSize(Value: longint);',
  23181. 'begin',
  23182. 'end;',
  23183. 'begin',
  23184. '']);
  23185. ConvertProgram;
  23186. CheckSource('TestTypeHelper_ClassProperty',
  23187. LinesToStr([ // statements
  23188. 'rtl.createHelper($mod, "THelper", null, function () {',
  23189. ' this.GetSize = function () {',
  23190. ' var Result = 0;',
  23191. ' Result = $mod.THelper.GetSize() + 1;',
  23192. ' $mod.THelper.SetSize(2);',
  23193. ' return Result;',
  23194. ' };',
  23195. ' this.SetSize = function (Value) {',
  23196. ' };',
  23197. '});',
  23198. '']),
  23199. LinesToStr([ // $mod.$main
  23200. '']));
  23201. end;
  23202. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  23203. begin
  23204. StartProgram(false);
  23205. Add([
  23206. '{$modeswitch typehelpers}',
  23207. 'type',
  23208. ' THelper = type helper for word',
  23209. ' class function GetItems(Index: byte): boolean; static;',
  23210. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  23211. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  23212. ' end;',
  23213. 'class function THelper.GetItems(Index: byte): boolean;',
  23214. 'begin',
  23215. ' Result:=Items[1];',
  23216. ' Items[2]:=false;',
  23217. 'end;',
  23218. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  23219. 'begin',
  23220. 'end;',
  23221. 'var',
  23222. ' w: word;',
  23223. ' b: boolean;',
  23224. 'begin',
  23225. ' b:=w.Items[1];',
  23226. ' w.Items[2]:=b;',
  23227. ' with w do begin',
  23228. ' b:=Items[3];',
  23229. ' Items[4]:=b;',
  23230. ' end;',
  23231. '']);
  23232. ConvertProgram;
  23233. CheckSource('TestTypeHelper_ClassProperty_Array',
  23234. LinesToStr([ // statements
  23235. 'rtl.createHelper($mod, "THelper", null, function () {',
  23236. ' this.GetItems = function (Index) {',
  23237. ' var Result = false;',
  23238. ' Result = $mod.THelper.GetItems(1);',
  23239. ' $mod.THelper.SetItems(2, false);',
  23240. ' return Result;',
  23241. ' };',
  23242. ' this.SetItems = function (Index, Value) {',
  23243. ' };',
  23244. '});',
  23245. 'this.w = 0;',
  23246. 'this.b = false;',
  23247. '']),
  23248. LinesToStr([ // $mod.$main
  23249. '$mod.b = $mod.THelper.GetItems(1);',
  23250. '$mod.THelper.SetItems(2, $mod.b);',
  23251. 'var $with1 = $mod.w;',
  23252. '$mod.b = $mod.THelper.GetItems(3);',
  23253. '$mod.THelper.SetItems(4, $mod.b);',
  23254. '']));
  23255. end;
  23256. procedure TTestModule.TestTypeHelper_ClassMethod;
  23257. begin
  23258. StartProgram(false);
  23259. Add([
  23260. '{$modeswitch typehelpers}',
  23261. 'type',
  23262. ' THelper = type helper for word',
  23263. ' class procedure DoStatic; static;',
  23264. ' end;',
  23265. 'class procedure THelper.DoStatic;',
  23266. 'begin',
  23267. ' DoStatic;',
  23268. ' DoStatic();',
  23269. 'end;',
  23270. 'var w: word;',
  23271. 'begin',
  23272. ' w.DoStatic;',
  23273. ' w.DoStatic();',
  23274. '']);
  23275. ConvertProgram;
  23276. CheckSource('TestTypeHelper_ClassMethod',
  23277. LinesToStr([ // statements
  23278. 'rtl.createHelper($mod, "THelper", null, function () {',
  23279. ' this.DoStatic = function () {',
  23280. ' $mod.THelper.DoStatic();',
  23281. ' $mod.THelper.DoStatic();',
  23282. ' };',
  23283. '});',
  23284. 'this.w = 0;',
  23285. '']),
  23286. LinesToStr([ // $mod.$main
  23287. '$mod.THelper.DoStatic();',
  23288. '$mod.THelper.DoStatic();',
  23289. '']));
  23290. end;
  23291. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  23292. begin
  23293. StartProgram(false);
  23294. Add([
  23295. '{$modeswitch typehelpers}',
  23296. 'type',
  23297. ' THelper = type helper for word',
  23298. ' procedure Run; external name ''Run'';',
  23299. ' end;',
  23300. 'var w: word;',
  23301. 'begin',
  23302. ' w.Run;',
  23303. '']);
  23304. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  23305. ConvertProgram;
  23306. end;
  23307. procedure TTestModule.TestTypeHelper_Constructor;
  23308. begin
  23309. StartProgram(false);
  23310. Add([
  23311. '{$modeswitch typehelpers}',
  23312. 'type',
  23313. ' THelper = type helper for word',
  23314. ' constructor Init(e: longint);',
  23315. ' end;',
  23316. 'constructor THelper.Init(e: longint);',
  23317. 'begin',
  23318. ' Self:=e;',
  23319. ' Init(e+1);',
  23320. 'end;',
  23321. 'var w: word;',
  23322. 'begin',
  23323. ' w:=word.Init(2);',
  23324. ' w:=w.Init(3);',
  23325. ' with word do w:=Init(4);',
  23326. ' with w do w:=Init(5);',
  23327. '']);
  23328. ConvertProgram;
  23329. CheckSource('TestTypeHelper_Constructor',
  23330. LinesToStr([ // statements
  23331. 'rtl.createHelper($mod, "THelper", null, function () {',
  23332. ' this.Init = function (e) {',
  23333. ' this.set(e);',
  23334. ' $mod.THelper.Init.call(this, e + 1);',
  23335. ' return this.get();',
  23336. ' };',
  23337. ' this.$new = function (fn, args) {',
  23338. ' return this[fn].apply({',
  23339. ' p: 0,',
  23340. ' get: function () {',
  23341. ' return this.p;',
  23342. ' },',
  23343. ' set: function (v) {',
  23344. ' this.p = v;',
  23345. ' }',
  23346. ' }, args);',
  23347. ' };',
  23348. '});',
  23349. 'this.w = 0;',
  23350. '']),
  23351. LinesToStr([ // $mod.$main
  23352. '$mod.w = $mod.THelper.$new("Init", [2]);',
  23353. '$mod.w = $mod.THelper.Init.call({',
  23354. ' p: $mod,',
  23355. ' get: function () {',
  23356. ' return this.p.w;',
  23357. ' },',
  23358. ' set: function (v) {',
  23359. ' this.p.w = v;',
  23360. ' }',
  23361. '}, 3);',
  23362. '$mod.w = $mod.THelper.$new("Init", [4]);',
  23363. 'var $with1 = $mod.w;',
  23364. '$mod.w = $mod.THelper.Init.call({',
  23365. ' get: function () {',
  23366. ' return $with1;',
  23367. ' },',
  23368. ' set: function (v) {',
  23369. ' $with1 = v;',
  23370. ' }',
  23371. '}, 5);',
  23372. '']));
  23373. end;
  23374. procedure TTestModule.TestTypeHelper_Word;
  23375. begin
  23376. StartProgram(false);
  23377. Add([
  23378. '{$modeswitch typehelpers}',
  23379. 'type',
  23380. ' THelper = type helper for word',
  23381. ' procedure DoIt(e: byte = 123);',
  23382. ' end;',
  23383. 'procedure THelper.DoIt(e: byte);',
  23384. 'begin',
  23385. ' Self:=e;',
  23386. ' Self:=Self+1;',
  23387. ' with Self do Doit;',
  23388. 'end;',
  23389. 'begin',
  23390. ' word(3).DoIt;',
  23391. '']);
  23392. ConvertProgram;
  23393. CheckSource('TestTypeHelper_Word',
  23394. LinesToStr([ // statements
  23395. 'rtl.createHelper($mod, "THelper", null, function () {',
  23396. ' this.DoIt = function (e) {',
  23397. ' this.set(e);',
  23398. ' this.set(this.get() + 1);',
  23399. ' var $with1 = this.get();',
  23400. ' $mod.THelper.DoIt.call(this, 123);',
  23401. ' };',
  23402. '});',
  23403. '']),
  23404. LinesToStr([ // $mod.$main
  23405. '$mod.THelper.DoIt.call({',
  23406. ' get: function () {',
  23407. ' return 3;',
  23408. ' },',
  23409. ' set: function (v) {',
  23410. ' rtl.raiseE("EPropReadOnly");',
  23411. ' }',
  23412. '}, 123);',
  23413. '']));
  23414. end;
  23415. procedure TTestModule.TestTypeHelper_Double;
  23416. begin
  23417. StartProgram(false);
  23418. Add([
  23419. '{$modeswitch typehelpers}',
  23420. 'type',
  23421. ' Float = type double;',
  23422. ' THelper = type helper for Float',
  23423. ' const NPI = 3.141592;',
  23424. ' function ToStr: String;',
  23425. ' end;',
  23426. 'function THelper.ToStr: String;',
  23427. 'begin',
  23428. 'end;',
  23429. 'procedure DoIt(s: string);',
  23430. 'begin',
  23431. 'end;',
  23432. 'var f: Float;',
  23433. 'begin',
  23434. ' DoIt(f.toStr);',
  23435. ' DoIt(f.toStr());',
  23436. ' (f*f).toStr;',
  23437. ' DoIt((f*f).toStr);',
  23438. '']);
  23439. ConvertProgram;
  23440. CheckSource('TestTypeHelper_Double',
  23441. LinesToStr([ // statements
  23442. 'rtl.createHelper($mod, "THelper", null, function () {',
  23443. ' this.NPI = 3.141592;',
  23444. ' this.ToStr = function () {',
  23445. ' var Result = "";',
  23446. ' return Result;',
  23447. ' };',
  23448. '});',
  23449. 'this.DoIt = function (s) {',
  23450. '};',
  23451. 'this.f = 0.0;',
  23452. '']),
  23453. LinesToStr([ // $mod.$main
  23454. '$mod.DoIt($mod.THelper.ToStr.call({',
  23455. ' p: $mod,',
  23456. ' get: function () {',
  23457. ' return this.p.f;',
  23458. ' },',
  23459. ' set: function (v) {',
  23460. ' this.p.f = v;',
  23461. ' }',
  23462. '}));',
  23463. '$mod.DoIt($mod.THelper.ToStr.call({',
  23464. ' p: $mod,',
  23465. ' get: function () {',
  23466. ' return this.p.f;',
  23467. ' },',
  23468. ' set: function (v) {',
  23469. ' this.p.f = v;',
  23470. ' }',
  23471. '}));',
  23472. '$mod.THelper.ToStr.call({',
  23473. ' a: $mod.f * $mod.f,',
  23474. ' get: function () {',
  23475. ' return this.a;',
  23476. ' },',
  23477. ' set: function (v) {',
  23478. ' rtl.raiseE("EPropReadOnly");',
  23479. ' }',
  23480. '});',
  23481. '$mod.DoIt($mod.THelper.ToStr.call({',
  23482. ' a: $mod.f * $mod.f,',
  23483. ' get: function () {',
  23484. ' return this.a;',
  23485. ' },',
  23486. ' set: function (v) {',
  23487. ' rtl.raiseE("EPropReadOnly");',
  23488. ' }',
  23489. '}));',
  23490. '']));
  23491. end;
  23492. procedure TTestModule.TestTypeHelper_StringChar;
  23493. begin
  23494. StartProgram(false);
  23495. Add([
  23496. '{$modeswitch typehelpers}',
  23497. 'type',
  23498. ' TStringHelper = type helper for string',
  23499. ' procedure DoIt(e: byte = 123);',
  23500. ' end;',
  23501. ' TCharHelper = type helper for char',
  23502. ' procedure Fly;',
  23503. ' end;',
  23504. 'procedure TStringHelper.DoIt(e: byte);',
  23505. 'begin',
  23506. ' Self[1]:=''c'';',
  23507. ' Self[2]:=Self[3];',
  23508. 'end;',
  23509. 'procedure TCharHelper.Fly;',
  23510. 'begin',
  23511. ' Self:=''c'';',
  23512. 'end;',
  23513. 'begin',
  23514. ' ''abc''.DoIt;',
  23515. ' ''xyz''.DoIt();',
  23516. ' ''c''.Fly();',
  23517. '']);
  23518. ConvertProgram;
  23519. CheckSource('TestTypeHelper_StringChar',
  23520. LinesToStr([ // statements
  23521. 'rtl.createHelper($mod, "TStringHelper", null, function () {',
  23522. ' this.DoIt = function (e) {',
  23523. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  23524. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  23525. ' };',
  23526. '});',
  23527. 'rtl.createHelper($mod, "TCharHelper", null, function () {',
  23528. ' this.Fly = function () {',
  23529. ' this.set("c");',
  23530. ' };',
  23531. '});',
  23532. '']),
  23533. LinesToStr([ // $mod.$main
  23534. '$mod.TStringHelper.DoIt.call({',
  23535. ' get: function () {',
  23536. ' return "abc";',
  23537. ' },',
  23538. ' set: function (v) {',
  23539. ' rtl.raiseE("EPropReadOnly");',
  23540. ' }',
  23541. '}, 123);',
  23542. '$mod.TStringHelper.DoIt.call({',
  23543. ' get: function () {',
  23544. ' return "xyz";',
  23545. ' },',
  23546. ' set: function (v) {',
  23547. ' rtl.raiseE("EPropReadOnly");',
  23548. ' }',
  23549. '}, 123);',
  23550. '$mod.TCharHelper.Fly.call({',
  23551. ' get: function () {',
  23552. ' return "c";',
  23553. ' },',
  23554. ' set: function (v) {',
  23555. ' rtl.raiseE("EPropReadOnly");',
  23556. ' }',
  23557. '});',
  23558. '']));
  23559. end;
  23560. procedure TTestModule.TestTypeHelper_JSValue;
  23561. begin
  23562. StartProgram(false);
  23563. Add([
  23564. '{$modeswitch typehelpers}',
  23565. 'type',
  23566. ' TExtValue = type jsvalue;',
  23567. ' THelper = type helper for TExtValue',
  23568. ' function ToStr: String;',
  23569. ' end;',
  23570. 'function THelper.ToStr: String;',
  23571. 'begin',
  23572. 'end;',
  23573. 'var',
  23574. ' s: string;',
  23575. ' v: TExtValue;',
  23576. 'begin',
  23577. ' s:=v.toStr;',
  23578. ' s:=v.toStr();',
  23579. ' TExtValue(s).toStr;',
  23580. '']);
  23581. ConvertProgram;
  23582. CheckSource('TestTypeHelper_JSValue',
  23583. LinesToStr([ // statements
  23584. 'rtl.createHelper($mod, "THelper", null, function () {',
  23585. ' this.ToStr = function () {',
  23586. ' var Result = "";',
  23587. ' return Result;',
  23588. ' };',
  23589. '});',
  23590. 'this.s = "";',
  23591. 'this.v = undefined;',
  23592. '']),
  23593. LinesToStr([ // $mod.$main
  23594. '$mod.s = $mod.THelper.ToStr.call({',
  23595. ' p: $mod,',
  23596. ' get: function () {',
  23597. ' return this.p.v;',
  23598. ' },',
  23599. ' set: function (v) {',
  23600. ' this.p.v = v;',
  23601. ' }',
  23602. '});',
  23603. '$mod.s = $mod.THelper.ToStr.call({',
  23604. ' p: $mod,',
  23605. ' get: function () {',
  23606. ' return this.p.v;',
  23607. ' },',
  23608. ' set: function (v) {',
  23609. ' this.p.v = v;',
  23610. ' }',
  23611. '});',
  23612. '$mod.THelper.ToStr.call({',
  23613. ' p: $mod,',
  23614. ' get: function () {',
  23615. ' return this.p.s;',
  23616. ' },',
  23617. ' set: function (v) {',
  23618. ' rtl.raiseE("EPropReadOnly");',
  23619. ' }',
  23620. '});',
  23621. '']));
  23622. end;
  23623. procedure TTestModule.TestTypeHelper_Array;
  23624. begin
  23625. StartProgram(false);
  23626. Add([
  23627. '{$modeswitch typehelpers}',
  23628. 'type',
  23629. ' TArrOfBool = array of boolean;',
  23630. ' TArrOfJS = array of jsvalue;',
  23631. ' THelper = type helper for TArrOfBool',
  23632. ' procedure DoIt(e: byte = 123);',
  23633. ' end;',
  23634. 'procedure THelper.DoIt(e: byte);',
  23635. 'begin',
  23636. ' Self[1]:=true;',
  23637. ' Self[2]:=not Self[3];',
  23638. ' SetLength(Self,4);',
  23639. 'end;',
  23640. 'var',
  23641. ' b: TArrOfBool;',
  23642. ' j: TArrOfJS;',
  23643. 'begin',
  23644. ' b.DoIt;',
  23645. ' TArrOfBool(j).DoIt();',
  23646. '']);
  23647. ConvertProgram;
  23648. CheckSource('TestTypeHelper_Array',
  23649. LinesToStr([ // statements
  23650. 'rtl.createHelper($mod, "THelper", null, function () {',
  23651. ' this.DoIt = function (e) {',
  23652. ' this.get()[1] = true;',
  23653. ' this.get()[2] = !this.get()[3];',
  23654. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  23655. ' };',
  23656. '});',
  23657. 'this.b = [];',
  23658. 'this.j = [];',
  23659. '']),
  23660. LinesToStr([ // $mod.$main
  23661. '$mod.THelper.DoIt.call({',
  23662. ' p: $mod,',
  23663. ' get: function () {',
  23664. ' return this.p.b;',
  23665. ' },',
  23666. ' set: function (v) {',
  23667. ' this.p.b = v;',
  23668. ' }',
  23669. '}, 123);',
  23670. '$mod.THelper.DoIt.call({',
  23671. ' p: $mod,',
  23672. ' get: function () {',
  23673. ' return this.p.j;',
  23674. ' },',
  23675. ' set: function (v) {',
  23676. ' this.p.j = v;',
  23677. ' }',
  23678. '}, 123);',
  23679. '']));
  23680. end;
  23681. procedure TTestModule.TestTypeHelper_EnumType;
  23682. begin
  23683. StartProgram(false);
  23684. Add([
  23685. '{$modeswitch typehelpers}',
  23686. 'type',
  23687. ' TEnum = (red,blue);',
  23688. ' THelper = type helper for TEnum',
  23689. ' procedure DoIt(e: byte = 123);',
  23690. ' class procedure Swing(w: word); static;',
  23691. ' end;',
  23692. 'procedure THelper.DoIt(e: byte);',
  23693. 'begin',
  23694. ' Self:=red;',
  23695. ' Self:=succ(Self);',
  23696. ' with Self do Doit;',
  23697. 'end;',
  23698. 'class procedure THelper.Swing(w: word);',
  23699. 'begin',
  23700. 'end;',
  23701. 'var e: TEnum;',
  23702. 'begin',
  23703. ' e.DoIt;',
  23704. ' red.DoIt;',
  23705. ' TEnum.blue.DoIt;',
  23706. ' TEnum(1).DoIt;',
  23707. ' TEnum.Swing(3);',
  23708. '']);
  23709. ConvertProgram;
  23710. CheckSource('TestTypeHelper_EnumType',
  23711. LinesToStr([ // statements
  23712. 'this.TEnum = {',
  23713. ' "0": "red",',
  23714. ' red: 0,',
  23715. ' "1": "blue",',
  23716. ' blue: 1',
  23717. '};',
  23718. 'rtl.createHelper($mod, "THelper", null, function () {',
  23719. ' this.DoIt = function (e) {',
  23720. ' this.set($mod.TEnum.red);',
  23721. ' this.set(this.get() + 1);',
  23722. ' var $with1 = this.get();',
  23723. ' $mod.THelper.DoIt.call(this, 123);',
  23724. ' };',
  23725. ' this.Swing = function (w) {',
  23726. ' };',
  23727. '});',
  23728. 'this.e = 0;',
  23729. '']),
  23730. LinesToStr([ // $mod.$main
  23731. '$mod.THelper.DoIt.call({',
  23732. ' p: $mod,',
  23733. ' get: function () {',
  23734. ' return this.p.e;',
  23735. ' },',
  23736. ' set: function (v) {',
  23737. ' this.p.e = v;',
  23738. ' }',
  23739. '}, 123);',
  23740. '$mod.THelper.DoIt.call({',
  23741. ' p: $mod.TEnum,',
  23742. ' get: function () {',
  23743. ' return this.p.red;',
  23744. ' },',
  23745. ' set: function (v) {',
  23746. ' rtl.raiseE("EPropReadOnly");',
  23747. ' }',
  23748. '}, 123);',
  23749. '$mod.THelper.DoIt.call({',
  23750. ' p: $mod.TEnum,',
  23751. ' get: function () {',
  23752. ' return this.p.blue;',
  23753. ' },',
  23754. ' set: function (v) {',
  23755. ' rtl.raiseE("EPropReadOnly");',
  23756. ' }',
  23757. '}, 123);',
  23758. '$mod.THelper.DoIt.call({',
  23759. ' get: function () {',
  23760. ' return 1;',
  23761. ' },',
  23762. ' set: function (v) {',
  23763. ' rtl.raiseE("EPropReadOnly");',
  23764. ' }',
  23765. '}, 123);',
  23766. '$mod.THelper.Swing(3);',
  23767. '']));
  23768. end;
  23769. procedure TTestModule.TestTypeHelper_SetType;
  23770. begin
  23771. StartProgram(false);
  23772. Add([
  23773. '{$modeswitch typehelpers}',
  23774. 'type',
  23775. ' TEnum = (red,blue);',
  23776. ' TSetOfEnum = set of TEnum;',
  23777. ' THelper = type helper for TSetOfEnum',
  23778. ' procedure DoIt(e: byte = 123);',
  23779. ' constructor Init(e: TEnum);',
  23780. ' constructor InitEmpty;',
  23781. ' end;',
  23782. 'procedure THelper.DoIt(e: byte);',
  23783. 'begin',
  23784. ' Self:=[];',
  23785. ' Self:=[red];',
  23786. ' Include(Self,blue);',
  23787. 'end;',
  23788. 'constructor THelper.Init(e: TEnum);',
  23789. 'begin',
  23790. ' Self:=[];',
  23791. ' Self:=[e];',
  23792. ' Include(Self,blue);',
  23793. 'end;',
  23794. 'constructor THelper.InitEmpty;',
  23795. 'begin',
  23796. 'end;',
  23797. 'var s: TSetOfEnum;',
  23798. 'begin',
  23799. ' s.DoIt;',
  23800. //' [red].DoIt;',
  23801. //' with s do DoIt;',
  23802. //' with [red,blue] do DoIt;',
  23803. ' s:=TSetOfEnum.Init(blue);',
  23804. ' s:=s.Init(blue);',
  23805. '']);
  23806. ConvertProgram;
  23807. CheckSource('TestTypeHelper_SetType',
  23808. LinesToStr([ // statements
  23809. 'this.TEnum = {',
  23810. ' "0": "red",',
  23811. ' red: 0,',
  23812. ' "1": "blue",',
  23813. ' blue: 1',
  23814. '};',
  23815. 'rtl.createHelper($mod, "THelper", null, function () {',
  23816. ' this.DoIt = function (e) {',
  23817. ' this.set({});',
  23818. ' this.set(rtl.createSet($mod.TEnum.red));',
  23819. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  23820. ' };',
  23821. ' this.Init = function (e) {',
  23822. ' this.set({});',
  23823. ' this.set(rtl.createSet(e));',
  23824. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  23825. ' return this.get();',
  23826. ' };',
  23827. ' this.InitEmpty = function () {',
  23828. ' return this.get();',
  23829. ' };',
  23830. ' this.$new = function (fn, args) {',
  23831. ' return this[fn].apply({',
  23832. ' p: {},',
  23833. ' get: function () {',
  23834. ' return this.p;',
  23835. ' },',
  23836. ' set: function (v) {',
  23837. ' this.p = v;',
  23838. ' }',
  23839. ' }, args);',
  23840. ' };',
  23841. '});',
  23842. 'this.s = {};',
  23843. '']),
  23844. LinesToStr([ // $mod.$main
  23845. '$mod.THelper.DoIt.call({',
  23846. ' p: $mod,',
  23847. ' get: function () {',
  23848. ' return this.p.s;',
  23849. ' },',
  23850. ' set: function (v) {',
  23851. ' this.p.s = v;',
  23852. ' }',
  23853. '}, 123);',
  23854. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  23855. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  23856. ' p: $mod,',
  23857. ' get: function () {',
  23858. ' return this.p.s;',
  23859. ' },',
  23860. ' set: function (v) {',
  23861. ' this.p.s = v;',
  23862. ' }',
  23863. '}, $mod.TEnum.blue));',
  23864. '']));
  23865. end;
  23866. procedure TTestModule.TestTypeHelper_InterfaceType;
  23867. begin
  23868. StartProgram(false);
  23869. Add([
  23870. '{$interfaces com}',
  23871. '{$modeswitch typehelpers}',
  23872. 'type',
  23873. ' IUnknown = interface',
  23874. ' function _AddRef: longint;',
  23875. ' function _Release: longint;',
  23876. ' end;',
  23877. ' TObject = class(IUnknown)',
  23878. ' function _AddRef: longint; virtual; abstract;',
  23879. ' function _Release: longint; virtual; abstract;',
  23880. ' end;',
  23881. ' THelper = type helper for IUnknown',
  23882. ' procedure Fly(e: byte = 123);',
  23883. ' class procedure Run; static;',
  23884. ' end;',
  23885. 'var',
  23886. ' i: IUnknown;',
  23887. ' o: TObject;',
  23888. 'procedure THelper.Fly(e: byte);',
  23889. 'begin',
  23890. ' i:=Self;',
  23891. ' o:=Self as TObject;',
  23892. ' Self:=nil;',
  23893. ' Self:=i;',
  23894. ' Self:=o;',
  23895. ' with Self do begin',
  23896. ' Fly;',
  23897. ' Fly();',
  23898. ' end;',
  23899. 'end;',
  23900. 'class procedure THelper.Run;',
  23901. 'var l: IUnknown;',
  23902. 'begin',
  23903. ' l.Fly;',
  23904. ' l.Fly();',
  23905. 'end;',
  23906. 'begin',
  23907. ' i.Fly;',
  23908. ' i.Fly();',
  23909. ' i.Run;',
  23910. ' i.Run();',
  23911. ' IUnknown.Run;',
  23912. ' IUnknown.Run();',
  23913. '']);
  23914. ConvertProgram;
  23915. CheckSource('TestTypeHelper_InterfaceType',
  23916. LinesToStr([ // statements
  23917. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  23918. 'rtl.createClass($mod, "TObject", null, function () {',
  23919. ' this.$init = function () {',
  23920. ' };',
  23921. ' this.$final = function () {',
  23922. ' };',
  23923. ' rtl.addIntf(this, $mod.IUnknown);',
  23924. '});',
  23925. 'rtl.createHelper($mod, "THelper", null, function () {',
  23926. ' this.Fly = function (e) {',
  23927. ' var $ir = rtl.createIntfRefs();',
  23928. ' try {',
  23929. ' rtl.setIntfP($mod, "i", this.get());',
  23930. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  23931. ' this.set(null);',
  23932. ' this.set($mod.i);',
  23933. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  23934. ' var $with1 = this.get();',
  23935. ' $mod.THelper.Fly.call(this, 123);',
  23936. ' $mod.THelper.Fly.call(this, 123);',
  23937. ' } finally {',
  23938. ' $ir.free();',
  23939. ' };',
  23940. ' };',
  23941. ' this.Run = function () {',
  23942. ' var l = null;',
  23943. ' try {',
  23944. ' $mod.THelper.Fly.call({',
  23945. ' get: function () {',
  23946. ' return l;',
  23947. ' },',
  23948. ' set: function (v) {',
  23949. ' l = rtl.setIntfL(l, v);',
  23950. ' }',
  23951. ' }, 123);',
  23952. ' $mod.THelper.Fly.call({',
  23953. ' get: function () {',
  23954. ' return l;',
  23955. ' },',
  23956. ' set: function (v) {',
  23957. ' l = rtl.setIntfL(l, v);',
  23958. ' }',
  23959. ' }, 123);',
  23960. ' } finally {',
  23961. ' rtl._Release(l);',
  23962. ' };',
  23963. ' };',
  23964. '});',
  23965. 'this.i = null;',
  23966. 'this.o = null;',
  23967. '']),
  23968. LinesToStr([ // $mod.$main
  23969. '$mod.THelper.Fly.call({',
  23970. ' p: $mod,',
  23971. ' get: function () {',
  23972. ' return this.p.i;',
  23973. ' },',
  23974. ' set: function (v) {',
  23975. ' rtl.setIntfP(this.p, "i", v);',
  23976. ' }',
  23977. '}, 123);',
  23978. '$mod.THelper.Fly.call({',
  23979. ' p: $mod,',
  23980. ' get: function () {',
  23981. ' return this.p.i;',
  23982. ' },',
  23983. ' set: function (v) {',
  23984. ' rtl.setIntfP(this.p, "i", v);',
  23985. ' }',
  23986. '}, 123);',
  23987. '$mod.THelper.Run();',
  23988. '$mod.THelper.Run();',
  23989. '$mod.THelper.Run();',
  23990. '$mod.THelper.Run();',
  23991. '']));
  23992. end;
  23993. procedure TTestModule.TestProcType;
  23994. begin
  23995. StartProgram(false);
  23996. Add([
  23997. 'type',
  23998. ' TProcInt = procedure(vI: longint = 1);',
  23999. 'procedure DoIt(vJ: longint);',
  24000. 'begin end;',
  24001. 'var',
  24002. ' b: boolean;',
  24003. ' vP, vQ: tprocint;',
  24004. 'begin',
  24005. ' vp:=nil;',
  24006. ' vp:=vp;',
  24007. ' vp:=@doit;',
  24008. ' vp;',
  24009. ' vp();',
  24010. ' vp(2);',
  24011. ' b:=vp=nil;',
  24012. ' b:=nil=vp;',
  24013. ' b:=vp=vq;',
  24014. ' b:=vp=@doit;',
  24015. ' b:=@doit=vp;',
  24016. ' b:=vp<>nil;',
  24017. ' b:=nil<>vp;',
  24018. ' b:=vp<>vq;',
  24019. ' b:=vp<>@doit;',
  24020. ' b:=@doit<>vp;',
  24021. ' b:=Assigned(vp);',
  24022. ' if Assigned(vp) then ;']);
  24023. ConvertProgram;
  24024. CheckSource('TestProcType',
  24025. LinesToStr([ // statements
  24026. 'this.DoIt = function(vJ) {',
  24027. '};',
  24028. 'this.b = false;',
  24029. 'this.vP = null;',
  24030. 'this.vQ = null;'
  24031. ]),
  24032. LinesToStr([ // $mod.$main
  24033. '$mod.vP = null;',
  24034. '$mod.vP = $mod.vP;',
  24035. '$mod.vP = $mod.DoIt;',
  24036. '$mod.vP(1);',
  24037. '$mod.vP(1);',
  24038. '$mod.vP(2);',
  24039. '$mod.b = $mod.vP === null;',
  24040. '$mod.b = null === $mod.vP;',
  24041. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  24042. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24043. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24044. '$mod.b = $mod.vP !== null;',
  24045. '$mod.b = null !== $mod.vP;',
  24046. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  24047. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24048. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24049. '$mod.b = $mod.vP != null;',
  24050. 'if ($mod.vP != null) ;',
  24051. '']));
  24052. end;
  24053. procedure TTestModule.TestProcType_Arg;
  24054. begin
  24055. StartProgram(false);
  24056. Add([
  24057. 'type',
  24058. ' TProcInt = procedure(vI: longint = 1);',
  24059. 'procedure DoIt(vJ: longint); begin end;',
  24060. 'procedure DoSome(vP, vQ: TProcInt);',
  24061. 'var',
  24062. ' b: boolean;',
  24063. 'begin',
  24064. ' vp:=nil;',
  24065. ' vp:=vp;',
  24066. ' vp:=@doit;',
  24067. ' vp;',
  24068. ' vp();',
  24069. ' vp(2);',
  24070. ' b:=vp=nil;',
  24071. ' b:=nil=vp;',
  24072. ' b:=vp=vq;',
  24073. ' b:=vp=@doit;',
  24074. ' b:=@doit=vp;',
  24075. ' b:=vp<>nil;',
  24076. ' b:=nil<>vp;',
  24077. ' b:=vp<>vq;',
  24078. ' b:=vp<>@doit;',
  24079. ' b:=@doit<>vp;',
  24080. ' b:=Assigned(vp);',
  24081. ' if Assigned(vp) then ;',
  24082. 'end;',
  24083. 'begin',
  24084. ' DoSome(@DoIt,nil);']);
  24085. ConvertProgram;
  24086. CheckSource('TestProcType_Arg',
  24087. LinesToStr([ // statements
  24088. 'this.DoIt = function(vJ) {',
  24089. '};',
  24090. 'this.DoSome = function(vP, vQ) {',
  24091. ' var b = false;',
  24092. ' vP = null;',
  24093. ' vP = vP;',
  24094. ' vP = $mod.DoIt;',
  24095. ' vP(1);',
  24096. ' vP(1);',
  24097. ' vP(2);',
  24098. ' b = vP === null;',
  24099. ' b = null === vP;',
  24100. ' b = rtl.eqCallback(vP,vQ);',
  24101. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  24102. ' b = rtl.eqCallback($mod.DoIt, vP);',
  24103. ' b = vP !== null;',
  24104. ' b = null !== vP;',
  24105. ' b = !rtl.eqCallback(vP, vQ);',
  24106. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  24107. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  24108. ' b = vP != null;',
  24109. ' if (vP != null) ;',
  24110. '};',
  24111. '']),
  24112. LinesToStr([ // $mod.$main
  24113. '$mod.DoSome($mod.DoIt,null);',
  24114. '']));
  24115. end;
  24116. procedure TTestModule.TestProcType_FunctionFPC;
  24117. begin
  24118. StartProgram(false);
  24119. Add('type');
  24120. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24121. Add('function DoIt(vI: longint): longint;');
  24122. Add('begin end;');
  24123. Add('var');
  24124. Add(' b: boolean;');
  24125. Add(' vP, vQ: tfuncint;');
  24126. Add('begin');
  24127. Add(' vp:=nil;');
  24128. Add(' vp:=vp;');
  24129. Add(' vp:=@doit;'); // ok in fpc and delphi
  24130. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24131. Add(' vp;'); // ok in fpc and delphi
  24132. Add(' vp();');
  24133. Add(' vp(2);');
  24134. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24135. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24136. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24137. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24138. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24139. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  24140. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  24141. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24142. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24143. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24144. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24145. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24146. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  24147. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  24148. Add(' b:=Assigned(vp);');
  24149. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  24150. Add(' doit(vp());'); // ok in fpc and delphi
  24151. Add(' doit(vp(2));'); // ok in fpc and delphi
  24152. ConvertProgram;
  24153. CheckSource('TestProcType_FunctionFPC',
  24154. LinesToStr([ // statements
  24155. 'this.DoIt = function(vI) {',
  24156. ' var Result = 0;',
  24157. ' return Result;',
  24158. '};',
  24159. 'this.b = false;',
  24160. 'this.vP = null;',
  24161. 'this.vQ = null;'
  24162. ]),
  24163. LinesToStr([ // $mod.$main
  24164. '$mod.vP = null;',
  24165. '$mod.vP = $mod.vP;',
  24166. '$mod.vP = $mod.DoIt;',
  24167. '$mod.vP(1);',
  24168. '$mod.vP(1);',
  24169. '$mod.vP(2);',
  24170. '$mod.b = $mod.vP === null;',
  24171. '$mod.b = null === $mod.vP;',
  24172. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  24173. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24174. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24175. '$mod.b = 4 === $mod.vP(1);',
  24176. '$mod.b = $mod.vP !== null;',
  24177. '$mod.b = null !== $mod.vP;',
  24178. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  24179. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24180. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24181. '$mod.b = 6 !== $mod.vP(1);',
  24182. '$mod.b = $mod.vP != null;',
  24183. '$mod.DoIt($mod.vP(1));',
  24184. '$mod.DoIt($mod.vP(2));',
  24185. '']));
  24186. end;
  24187. procedure TTestModule.TestProcType_FunctionDelphi;
  24188. begin
  24189. StartProgram(false);
  24190. Add('{$mode Delphi}');
  24191. Add('type');
  24192. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24193. Add('function DoIt(vI: longint): longint;');
  24194. Add('begin end;');
  24195. Add('var');
  24196. Add(' b: boolean;');
  24197. Add(' vP, vQ: tfuncint;');
  24198. Add('begin');
  24199. Add(' vp:=nil;');
  24200. Add(' vp:=vp;');
  24201. Add(' vp:=@doit;'); // ok in fpc and delphi
  24202. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24203. Add(' vp;'); // ok in fpc and delphi
  24204. Add(' vp();');
  24205. Add(' vp(2);');
  24206. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24207. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24208. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24209. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24210. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24211. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  24212. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  24213. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24214. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24215. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24216. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24217. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24218. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  24219. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  24220. Add(' b:=Assigned(vp);');
  24221. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  24222. Add(' doit(vp());'); // ok in fpc and delphi
  24223. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  24224. ConvertProgram;
  24225. CheckSource('TestProcType_FunctionDelphi',
  24226. LinesToStr([ // statements
  24227. 'this.DoIt = function(vI) {',
  24228. ' var Result = 0;',
  24229. ' return Result;',
  24230. '};',
  24231. 'this.b = false;',
  24232. 'this.vP = null;',
  24233. 'this.vQ = null;'
  24234. ]),
  24235. LinesToStr([ // $mod.$main
  24236. '$mod.vP = null;',
  24237. '$mod.vP = $mod.vP;',
  24238. '$mod.vP = $mod.DoIt;',
  24239. '$mod.vP = $mod.DoIt;',
  24240. '$mod.vP(1);',
  24241. '$mod.vP(1);',
  24242. '$mod.vP(2);',
  24243. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  24244. '$mod.b = $mod.vP(1) === 3;',
  24245. '$mod.b = 4 === $mod.vP(1);',
  24246. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  24247. '$mod.b = $mod.vP(1) !== 5;',
  24248. '$mod.b = 6 !== $mod.vP(1);',
  24249. '$mod.b = $mod.vP != null;',
  24250. '$mod.DoIt($mod.vP(1));',
  24251. '$mod.DoIt($mod.vP(1));',
  24252. '$mod.DoIt($mod.vP(2));',
  24253. '']));
  24254. end;
  24255. procedure TTestModule.TestProcType_ProcedureDelphi;
  24256. begin
  24257. StartProgram(false);
  24258. Add('{$mode Delphi}');
  24259. Add('type');
  24260. Add(' TProc = procedure;');
  24261. Add('procedure DoIt;');
  24262. Add('begin end;');
  24263. Add('var');
  24264. Add(' b: boolean;');
  24265. Add(' vP, vQ: tproc;');
  24266. Add('begin');
  24267. Add(' vp:=nil;');
  24268. Add(' vp:=vp;');
  24269. Add(' vp:=vq;');
  24270. 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
  24271. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24272. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  24273. Add(' vp;'); // ok in fpc and delphi
  24274. Add(' vp();');
  24275. // equal
  24276. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24277. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  24278. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24279. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  24280. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  24281. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24282. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24283. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  24284. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24285. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  24286. // unequal
  24287. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24288. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  24289. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24290. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  24291. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24292. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  24293. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24294. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  24295. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24296. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  24297. Add(' b:=Assigned(vp);');
  24298. ConvertProgram;
  24299. CheckSource('TestProcType_ProcedureDelphi',
  24300. LinesToStr([ // statements
  24301. 'this.DoIt = function() {',
  24302. '};',
  24303. 'this.b = false;',
  24304. 'this.vP = null;',
  24305. 'this.vQ = null;'
  24306. ]),
  24307. LinesToStr([ // $mod.$main
  24308. '$mod.vP = null;',
  24309. '$mod.vP = $mod.vP;',
  24310. '$mod.vP = $mod.vQ;',
  24311. '$mod.vP = $mod.DoIt;',
  24312. '$mod.vP = $mod.DoIt;',
  24313. '$mod.vP();',
  24314. '$mod.vP();',
  24315. '$mod.b = $mod.vP === null;',
  24316. '$mod.b = null === $mod.vP;',
  24317. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  24318. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24319. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24320. '$mod.b = $mod.vP !== null;',
  24321. '$mod.b = null !== $mod.vP;',
  24322. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  24323. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24324. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24325. '$mod.b = $mod.vP != null;',
  24326. '']));
  24327. end;
  24328. procedure TTestModule.TestProcType_AsParam;
  24329. begin
  24330. StartProgram(false);
  24331. Add('type');
  24332. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24333. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  24334. Add('var vJ: tfuncint;');
  24335. Add('begin');
  24336. Add(' vg:=vg;');
  24337. Add(' vj:=vh;');
  24338. Add(' vi:=vi;');
  24339. Add(' doit(vg,vg,vg);');
  24340. Add(' doit(vh,vh,vj);');
  24341. Add(' doit(vi,vi,vi);');
  24342. Add(' doit(vj,vj,vj);');
  24343. Add('end;');
  24344. Add('var i: tfuncint;');
  24345. Add('begin');
  24346. Add(' doit(i,i,i);');
  24347. ConvertProgram;
  24348. CheckSource('TestProcType_AsParam',
  24349. LinesToStr([ // statements
  24350. 'this.DoIt = function (vG,vH,vI) {',
  24351. ' var vJ = null;',
  24352. ' vG = vG;',
  24353. ' vJ = vH;',
  24354. ' vI.set(vI.get());',
  24355. ' $mod.DoIt(vG, vG, {',
  24356. ' get: function () {',
  24357. ' return vG;',
  24358. ' },',
  24359. ' set: function (v) {',
  24360. ' vG = v;',
  24361. ' }',
  24362. ' });',
  24363. ' $mod.DoIt(vH, vH, {',
  24364. ' get: function () {',
  24365. ' return vJ;',
  24366. ' },',
  24367. ' set: function (v) {',
  24368. ' vJ = v;',
  24369. ' }',
  24370. ' });',
  24371. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  24372. ' $mod.DoIt(vJ, vJ, {',
  24373. ' get: function () {',
  24374. ' return vJ;',
  24375. ' },',
  24376. ' set: function (v) {',
  24377. ' vJ = v;',
  24378. ' }',
  24379. ' });',
  24380. '};',
  24381. 'this.i = null;'
  24382. ]),
  24383. LinesToStr([
  24384. '$mod.DoIt($mod.i,$mod.i,{',
  24385. ' p: $mod,',
  24386. ' get: function () {',
  24387. ' return this.p.i;',
  24388. ' },',
  24389. ' set: function (v) {',
  24390. ' this.p.i = v;',
  24391. ' }',
  24392. '});'
  24393. ]));
  24394. end;
  24395. procedure TTestModule.TestProcType_MethodFPC;
  24396. begin
  24397. StartProgram(false);
  24398. Add('type');
  24399. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24400. Add(' TObject = class');
  24401. Add(' function DoIt(vA: longint = 1): longint;');
  24402. Add(' end;');
  24403. Add('function TObject.DoIt(vA: longint = 1): longint;');
  24404. Add('begin');
  24405. Add('end;');
  24406. Add('var');
  24407. Add(' Obj: TObject;');
  24408. Add(' vP: tfuncint;');
  24409. Add(' b: boolean;');
  24410. Add('begin');
  24411. Add(' vp:[email protected];'); // ok in fpc and delphi
  24412. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  24413. Add(' vp;'); // ok in fpc and delphi
  24414. Add(' vp();');
  24415. Add(' vp(2);');
  24416. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  24417. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  24418. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  24419. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  24420. ConvertProgram;
  24421. CheckSource('TestProcType_MethodFPC',
  24422. LinesToStr([ // statements
  24423. 'rtl.createClass($mod, "TObject", null, function () {',
  24424. ' this.$init = function () {',
  24425. ' };',
  24426. ' this.$final = function () {',
  24427. ' };',
  24428. ' this.DoIt = function (vA) {',
  24429. ' var Result = 0;',
  24430. ' return Result;',
  24431. ' };',
  24432. '});',
  24433. 'this.Obj = null;',
  24434. 'this.vP = null;',
  24435. 'this.b = false;'
  24436. ]),
  24437. LinesToStr([
  24438. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  24439. '$mod.vP(1);',
  24440. '$mod.vP(1);',
  24441. '$mod.vP(2);',
  24442. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  24443. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  24444. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  24445. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  24446. '']));
  24447. end;
  24448. procedure TTestModule.TestProcType_MethodDelphi;
  24449. begin
  24450. StartProgram(false);
  24451. Add('{$mode delphi}');
  24452. Add('type');
  24453. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24454. Add(' TObject = class');
  24455. Add(' function DoIt(vA: longint = 1): longint;');
  24456. Add(' end;');
  24457. Add('function TObject.DoIt(vA: longint = 1): longint;');
  24458. Add('begin');
  24459. Add('end;');
  24460. Add('var');
  24461. Add(' Obj: TObject;');
  24462. Add(' vP: tfuncint;');
  24463. Add(' b: boolean;');
  24464. Add('begin');
  24465. Add(' vp:[email protected];'); // ok in fpc and delphi
  24466. Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  24467. Add(' vp;'); // ok in fpc and delphi
  24468. Add(' vp();');
  24469. Add(' vp(2);');
  24470. //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  24471. //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  24472. //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  24473. //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  24474. ConvertProgram;
  24475. CheckSource('TestProcType_MethodDelphi',
  24476. LinesToStr([ // statements
  24477. 'rtl.createClass($mod, "TObject", null, function () {',
  24478. ' this.$init = function () {',
  24479. ' };',
  24480. ' this.$final = function () {',
  24481. ' };',
  24482. ' this.DoIt = function (vA) {',
  24483. ' var Result = 0;',
  24484. ' return Result;',
  24485. ' };',
  24486. '});',
  24487. 'this.Obj = null;',
  24488. 'this.vP = null;',
  24489. 'this.b = false;'
  24490. ]),
  24491. LinesToStr([
  24492. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  24493. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  24494. '$mod.vP(1);',
  24495. '$mod.vP(1);',
  24496. '$mod.vP(2);',
  24497. '']));
  24498. end;
  24499. procedure TTestModule.TestProcType_PropertyFPC;
  24500. begin
  24501. StartProgram(false);
  24502. Add('type');
  24503. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24504. Add(' TObject = class');
  24505. Add(' FOnFoo: TFuncInt;');
  24506. Add(' function DoIt(vA: longint = 1): longint;');
  24507. Add(' function GetFoo: TFuncInt;');
  24508. Add(' procedure SetFoo(const Value: TFuncInt);');
  24509. Add(' function GetEvents(Index: longint): TFuncInt;');
  24510. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  24511. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24512. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24513. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  24514. Add(' end;');
  24515. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24516. Add('function tobject.getfoo: tfuncint; begin end;');
  24517. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24518. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  24519. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  24520. Add('var');
  24521. Add(' Obj: TObject;');
  24522. Add(' vP: tfuncint;');
  24523. Add(' b: boolean;');
  24524. Add('begin');
  24525. Add(' obj.onfoo:=nil;');
  24526. Add(' obj.onbar:=nil;');
  24527. Add(' obj.events[1]:=nil;');
  24528. Add(' obj.onfoo:=obj.onfoo;');
  24529. Add(' obj.onbar:=obj.onbar;');
  24530. Add(' obj.events[2]:=obj.events[3];');
  24531. Add(' obj.onfoo:[email protected];');
  24532. Add(' obj.onbar:[email protected];');
  24533. Add(' obj.events[4]:[email protected];');
  24534. //Add(' obj.onfoo:=obj.doit;'); // delphi
  24535. //Add(' obj.onbar:=obj.doit;'); // delphi
  24536. //Add(' obj.events[4]:=obj.doit;'); // delphi
  24537. Add(' obj.onfoo;');
  24538. Add(' obj.onbar;');
  24539. //Add(' obj.events[5];'); ToDo in pasresolver
  24540. Add(' obj.onfoo();');
  24541. Add(' obj.onbar();');
  24542. Add(' obj.events[6]();');
  24543. Add(' b:=obj.onfoo=nil;');
  24544. Add(' b:=obj.onbar=nil;');
  24545. Add(' b:=obj.events[7]=nil;');
  24546. Add(' b:=obj.onfoo<>nil;');
  24547. Add(' b:=obj.onbar<>nil;');
  24548. Add(' b:=obj.events[8]<>nil;');
  24549. Add(' b:=obj.onfoo=vp;');
  24550. Add(' b:=obj.onbar=vp;');
  24551. Add(' b:=obj.events[9]=vp;');
  24552. Add(' b:=obj.onfoo=obj.onfoo;');
  24553. Add(' b:=obj.onbar=obj.onfoo;');
  24554. Add(' b:=obj.events[10]=obj.onfoo;');
  24555. Add(' b:=obj.onfoo<>obj.onfoo;');
  24556. Add(' b:=obj.onbar<>obj.onfoo;');
  24557. Add(' b:=obj.events[11]<>obj.onfoo;');
  24558. Add(' b:[email protected];');
  24559. Add(' b:[email protected];');
  24560. Add(' b:=obj.events[12][email protected];');
  24561. Add(' b:=obj.onfoo<>@obj.doit;');
  24562. Add(' b:=obj.onbar<>@obj.doit;');
  24563. Add(' b:=obj.events[12]<>@obj.doit;');
  24564. Add(' b:=Assigned(obj.onfoo);');
  24565. Add(' b:=Assigned(obj.onbar);');
  24566. Add(' b:=Assigned(obj.events[13]);');
  24567. ConvertProgram;
  24568. CheckSource('TestProcType_PropertyFPC',
  24569. LinesToStr([ // statements
  24570. 'rtl.createClass($mod, "TObject", null, function () {',
  24571. ' this.$init = function () {',
  24572. ' this.FOnFoo = null;',
  24573. ' };',
  24574. ' this.$final = function () {',
  24575. ' this.FOnFoo = undefined;',
  24576. ' };',
  24577. ' this.DoIt = function (vA) {',
  24578. ' var Result = 0;',
  24579. ' return Result;',
  24580. ' };',
  24581. 'this.GetFoo = function () {',
  24582. ' var Result = null;',
  24583. ' return Result;',
  24584. '};',
  24585. 'this.SetFoo = function (Value) {',
  24586. '};',
  24587. 'this.GetEvents = function (Index) {',
  24588. ' var Result = null;',
  24589. ' return Result;',
  24590. '};',
  24591. 'this.SetEvents = function (Index, Value) {',
  24592. '};',
  24593. '});',
  24594. 'this.Obj = null;',
  24595. 'this.vP = null;',
  24596. 'this.b = false;'
  24597. ]),
  24598. LinesToStr([
  24599. '$mod.Obj.FOnFoo = null;',
  24600. '$mod.Obj.SetFoo(null);',
  24601. '$mod.Obj.SetEvents(1, null);',
  24602. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  24603. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  24604. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  24605. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24606. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24607. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24608. '$mod.Obj.FOnFoo(1);',
  24609. '$mod.Obj.GetFoo();',
  24610. '$mod.Obj.FOnFoo(1);',
  24611. '$mod.Obj.GetFoo()(1);',
  24612. '$mod.Obj.GetEvents(6)(1);',
  24613. '$mod.b = $mod.Obj.FOnFoo === null;',
  24614. '$mod.b = $mod.Obj.GetFoo() === null;',
  24615. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  24616. '$mod.b = $mod.Obj.FOnFoo !== null;',
  24617. '$mod.b = $mod.Obj.GetFoo() !== null;',
  24618. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  24619. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  24620. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  24621. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  24622. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  24623. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  24624. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  24625. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  24626. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  24627. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  24628. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  24629. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  24630. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  24631. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  24632. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  24633. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  24634. '$mod.b = $mod.Obj.FOnFoo != null;',
  24635. '$mod.b = $mod.Obj.GetFoo() != null;',
  24636. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  24637. '']));
  24638. end;
  24639. procedure TTestModule.TestProcType_PropertyDelphi;
  24640. begin
  24641. StartProgram(false);
  24642. Add('{$mode delphi}');
  24643. Add('type');
  24644. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24645. Add(' TObject = class');
  24646. Add(' FOnFoo: TFuncInt;');
  24647. Add(' function DoIt(vA: longint = 1): longint;');
  24648. Add(' function GetFoo: TFuncInt;');
  24649. Add(' procedure SetFoo(const Value: TFuncInt);');
  24650. Add(' function GetEvents(Index: longint): TFuncInt;');
  24651. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  24652. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24653. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24654. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  24655. Add(' end;');
  24656. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24657. Add('function tobject.getfoo: tfuncint; begin end;');
  24658. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24659. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  24660. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  24661. Add('var');
  24662. Add(' Obj: TObject;');
  24663. Add(' vP: tfuncint;');
  24664. Add(' b: boolean;');
  24665. Add('begin');
  24666. Add(' obj.onfoo:=nil;');
  24667. Add(' obj.onbar:=nil;');
  24668. Add(' obj.events[1]:=nil;');
  24669. Add(' obj.onfoo:=obj.onfoo;');
  24670. Add(' obj.onbar:=obj.onbar;');
  24671. Add(' obj.events[2]:=obj.events[3];');
  24672. Add(' obj.onfoo:[email protected];');
  24673. Add(' obj.onbar:[email protected];');
  24674. Add(' obj.events[4]:[email protected];');
  24675. Add(' obj.onfoo:=obj.doit;'); // delphi
  24676. Add(' obj.onbar:=obj.doit;'); // delphi
  24677. Add(' obj.events[4]:=obj.doit;'); // delphi
  24678. Add(' obj.onfoo;');
  24679. Add(' obj.onbar;');
  24680. //Add(' obj.events[5];'); ToDo in pasresolver
  24681. Add(' obj.onfoo();');
  24682. Add(' obj.onbar();');
  24683. Add(' obj.events[6]();');
  24684. //Add(' b:=obj.onfoo=nil;'); // fpc
  24685. //Add(' b:=obj.onbar=nil;'); // fpc
  24686. //Add(' b:=obj.events[7]=nil;'); // fpc
  24687. //Add(' b:=obj.onfoo<>nil;'); // fpc
  24688. //Add(' b:=obj.onbar<>nil;'); // fpc
  24689. //Add(' b:=obj.events[8]<>nil;'); // fpc
  24690. Add(' b:=obj.onfoo=vp;');
  24691. Add(' b:=obj.onbar=vp;');
  24692. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  24693. Add(' b:=obj.onfoo=obj.onfoo;');
  24694. Add(' b:=obj.onbar=obj.onfoo;');
  24695. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  24696. Add(' b:=obj.onfoo<>obj.onfoo;');
  24697. Add(' b:=obj.onbar<>obj.onfoo;');
  24698. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  24699. //Add(' b:[email protected];'); // fpc
  24700. //Add(' b:[email protected];'); // fpc
  24701. //Add(' b:=obj.events[12][email protected];'); // fpc
  24702. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  24703. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  24704. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  24705. Add(' b:=Assigned(obj.onfoo);');
  24706. Add(' b:=Assigned(obj.onbar);');
  24707. Add(' b:=Assigned(obj.events[13]);');
  24708. ConvertProgram;
  24709. CheckSource('TestProcType_PropertyDelphi',
  24710. LinesToStr([ // statements
  24711. 'rtl.createClass($mod, "TObject", null, function () {',
  24712. ' this.$init = function () {',
  24713. ' this.FOnFoo = null;',
  24714. ' };',
  24715. ' this.$final = function () {',
  24716. ' this.FOnFoo = undefined;',
  24717. ' };',
  24718. ' this.DoIt = function (vA) {',
  24719. ' var Result = 0;',
  24720. ' return Result;',
  24721. ' };',
  24722. 'this.GetFoo = function () {',
  24723. ' var Result = null;',
  24724. ' return Result;',
  24725. '};',
  24726. 'this.SetFoo = function (Value) {',
  24727. '};',
  24728. 'this.GetEvents = function (Index) {',
  24729. ' var Result = null;',
  24730. ' return Result;',
  24731. '};',
  24732. 'this.SetEvents = function (Index, Value) {',
  24733. '};',
  24734. '});',
  24735. 'this.Obj = null;',
  24736. 'this.vP = null;',
  24737. 'this.b = false;'
  24738. ]),
  24739. LinesToStr([
  24740. '$mod.Obj.FOnFoo = null;',
  24741. '$mod.Obj.SetFoo(null);',
  24742. '$mod.Obj.SetEvents(1, null);',
  24743. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  24744. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  24745. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  24746. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24747. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24748. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24749. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24750. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24751. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24752. '$mod.Obj.FOnFoo(1);',
  24753. '$mod.Obj.GetFoo();',
  24754. '$mod.Obj.FOnFoo(1);',
  24755. '$mod.Obj.GetFoo()(1);',
  24756. '$mod.Obj.GetEvents(6)(1);',
  24757. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  24758. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  24759. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  24760. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  24761. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  24762. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  24763. '$mod.b = $mod.Obj.FOnFoo != null;',
  24764. '$mod.b = $mod.Obj.GetFoo() != null;',
  24765. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  24766. '']));
  24767. end;
  24768. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  24769. begin
  24770. StartProgram(false);
  24771. Add('type');
  24772. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24773. Add(' TObject = class');
  24774. Add(' FOnFoo: TFuncInt;');
  24775. Add(' function DoIt(vA: longint = 1): longint;');
  24776. Add(' function GetFoo: TFuncInt;');
  24777. Add(' procedure SetFoo(const Value: TFuncInt);');
  24778. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24779. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24780. Add(' end;');
  24781. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24782. Add('function tobject.getfoo: tfuncint; begin end;');
  24783. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24784. Add('var');
  24785. Add(' Obj: TObject;');
  24786. Add(' vP: tfuncint;');
  24787. Add(' b: boolean;');
  24788. Add('begin');
  24789. Add('with obj do begin');
  24790. Add(' fonfoo:=nil;');
  24791. Add(' onfoo:=nil;');
  24792. Add(' onbar:=nil;');
  24793. Add(' fonfoo:=fonfoo;');
  24794. Add(' onfoo:=onfoo;');
  24795. Add(' onbar:=onbar;');
  24796. Add(' fonfoo:=@doit;');
  24797. Add(' onfoo:=@doit;');
  24798. Add(' onbar:=@doit;');
  24799. //Add(' fonfoo:=doit;'); // delphi
  24800. //Add(' onfoo:=doit;'); // delphi
  24801. //Add(' onbar:=doit;'); // delphi
  24802. Add(' fonfoo;');
  24803. Add(' onfoo;');
  24804. Add(' onbar;');
  24805. Add(' fonfoo();');
  24806. Add(' onfoo();');
  24807. Add(' onbar();');
  24808. Add(' b:=fonfoo=nil;');
  24809. Add(' b:=onfoo=nil;');
  24810. Add(' b:=onbar=nil;');
  24811. Add(' b:=fonfoo<>nil;');
  24812. Add(' b:=onfoo<>nil;');
  24813. Add(' b:=onbar<>nil;');
  24814. Add(' b:=fonfoo=vp;');
  24815. Add(' b:=onfoo=vp;');
  24816. Add(' b:=onbar=vp;');
  24817. Add(' b:=fonfoo=fonfoo;');
  24818. Add(' b:=onfoo=onfoo;');
  24819. Add(' b:=onbar=onfoo;');
  24820. Add(' b:=fonfoo<>fonfoo;');
  24821. Add(' b:=onfoo<>onfoo;');
  24822. Add(' b:=onbar<>onfoo;');
  24823. Add(' b:=fonfoo=@doit;');
  24824. Add(' b:=onfoo=@doit;');
  24825. Add(' b:=onbar=@doit;');
  24826. Add(' b:=fonfoo<>@doit;');
  24827. Add(' b:=onfoo<>@doit;');
  24828. Add(' b:=onbar<>@doit;');
  24829. Add(' b:=Assigned(fonfoo);');
  24830. Add(' b:=Assigned(onfoo);');
  24831. Add(' b:=Assigned(onbar);');
  24832. Add('end;');
  24833. ConvertProgram;
  24834. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  24835. LinesToStr([ // statements
  24836. 'rtl.createClass($mod, "TObject", null, function () {',
  24837. ' this.$init = function () {',
  24838. ' this.FOnFoo = null;',
  24839. ' };',
  24840. ' this.$final = function () {',
  24841. ' this.FOnFoo = undefined;',
  24842. ' };',
  24843. ' this.DoIt = function (vA) {',
  24844. ' var Result = 0;',
  24845. ' return Result;',
  24846. ' };',
  24847. ' this.GetFoo = function () {',
  24848. ' var Result = null;',
  24849. ' return Result;',
  24850. ' };',
  24851. ' this.SetFoo = function (Value) {',
  24852. ' };',
  24853. '});',
  24854. 'this.Obj = null;',
  24855. 'this.vP = null;',
  24856. 'this.b = false;'
  24857. ]),
  24858. LinesToStr([
  24859. 'var $with1 = $mod.Obj;',
  24860. '$with1.FOnFoo = null;',
  24861. '$with1.FOnFoo = null;',
  24862. '$with1.SetFoo(null);',
  24863. '$with1.FOnFoo = $with1.FOnFoo;',
  24864. '$with1.FOnFoo = $with1.FOnFoo;',
  24865. '$with1.SetFoo($with1.GetFoo());',
  24866. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  24867. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  24868. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  24869. '$with1.FOnFoo(1);',
  24870. '$with1.FOnFoo(1);',
  24871. '$with1.GetFoo();',
  24872. '$with1.FOnFoo(1);',
  24873. '$with1.FOnFoo(1);',
  24874. '$with1.GetFoo()(1);',
  24875. '$mod.b = $with1.FOnFoo === null;',
  24876. '$mod.b = $with1.FOnFoo === null;',
  24877. '$mod.b = $with1.GetFoo() === null;',
  24878. '$mod.b = $with1.FOnFoo !== null;',
  24879. '$mod.b = $with1.FOnFoo !== null;',
  24880. '$mod.b = $with1.GetFoo() !== null;',
  24881. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  24882. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  24883. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  24884. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24885. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24886. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  24887. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24888. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24889. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  24890. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24891. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24892. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  24893. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24894. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24895. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  24896. '$mod.b = $with1.FOnFoo != null;',
  24897. '$mod.b = $with1.FOnFoo != null;',
  24898. '$mod.b = $with1.GetFoo() != null;',
  24899. '']));
  24900. end;
  24901. procedure TTestModule.TestProcType_Nested;
  24902. begin
  24903. StartProgram(false);
  24904. Add([
  24905. 'type',
  24906. ' TProcInt = procedure(vI: longint = 1);',
  24907. 'procedure DoIt(vJ: longint);',
  24908. 'var aProc: TProcInt;',
  24909. ' b: boolean;',
  24910. ' procedure Sub(vK: longint);',
  24911. ' var aSub: TProcInt;',
  24912. ' procedure SubSub(vK: longint);',
  24913. ' var aSubSub: TProcInt;',
  24914. ' begin;',
  24915. ' aProc:=@DoIt;',
  24916. ' aSub:=@DoIt;',
  24917. ' aSubSub:=@DoIt;',
  24918. ' aProc:=@Sub;',
  24919. ' aSub:=@Sub;',
  24920. ' aSubSub:=@Sub;',
  24921. ' aProc:=@SubSub;',
  24922. ' aSub:=@SubSub;',
  24923. ' aSubSub:=@SubSub;',
  24924. ' end;',
  24925. ' begin;',
  24926. ' end;',
  24927. 'begin;',
  24928. ' aProc:=@Sub;',
  24929. ' b:=aProc=@Sub;',
  24930. ' b:=@Sub=aProc;',
  24931. 'end;',
  24932. 'begin',
  24933. '']);
  24934. ConvertProgram;
  24935. CheckSource('TestProcType_Nested',
  24936. LinesToStr([ // statements
  24937. 'this.DoIt = function (vJ) {',
  24938. ' var aProc = null;',
  24939. ' var b = false;',
  24940. ' function Sub(vK) {',
  24941. ' var aSub = null;',
  24942. ' function SubSub(vK) {',
  24943. ' var aSubSub = null;',
  24944. ' aProc = $mod.DoIt;',
  24945. ' aSub = $mod.DoIt;',
  24946. ' aSubSub = $mod.DoIt;',
  24947. ' aProc = Sub;',
  24948. ' aSub = Sub;',
  24949. ' aSubSub = Sub;',
  24950. ' aProc = SubSub;',
  24951. ' aSub = SubSub;',
  24952. ' aSubSub = SubSub;',
  24953. ' };',
  24954. ' };',
  24955. ' aProc = Sub;',
  24956. ' b = rtl.eqCallback(aProc, Sub);',
  24957. ' b = rtl.eqCallback(Sub, aProc);',
  24958. '};',
  24959. '']),
  24960. LinesToStr([ // $mod.$main
  24961. '']));
  24962. end;
  24963. procedure TTestModule.TestProcType_NestedOfObject;
  24964. begin
  24965. StartProgram(false);
  24966. Add([
  24967. 'type',
  24968. ' TProcInt = procedure(vI: longint = 1) of object;',
  24969. ' TObject = class',
  24970. ' procedure DoIt(vJ: longint);',
  24971. ' end;',
  24972. 'procedure TObject.DoIt(vJ: longint);',
  24973. 'var aProc: TProcInt;',
  24974. ' b: boolean;',
  24975. ' procedure Sub(vK: longint);',
  24976. ' var aSub: TProcInt;',
  24977. ' procedure SubSub(vK: longint);',
  24978. ' var aSubSub: TProcInt;',
  24979. ' begin;',
  24980. ' aProc:=@DoIt;',
  24981. ' aSub:=@DoIt;',
  24982. ' aSubSub:=@DoIt;',
  24983. ' aProc:=@Sub;',
  24984. ' aSub:=@Sub;',
  24985. ' aSubSub:=@Sub;',
  24986. ' aProc:=@SubSub;',
  24987. ' aSub:=@SubSub;',
  24988. ' aSubSub:=@SubSub;',
  24989. ' end;',
  24990. ' begin;',
  24991. ' end;',
  24992. 'begin;',
  24993. ' aProc:=@Sub;',
  24994. ' b:=aProc=@Sub;',
  24995. ' b:=@Sub=aProc;',
  24996. 'end;',
  24997. 'begin',
  24998. '']);
  24999. ConvertProgram;
  25000. CheckSource('TestProcType_Nested',
  25001. LinesToStr([ // statements
  25002. 'rtl.createClass($mod, "TObject", null, function () {',
  25003. ' this.$init = function () {',
  25004. ' };',
  25005. ' this.$final = function () {',
  25006. ' };',
  25007. ' this.DoIt = function (vJ) {',
  25008. ' var $Self = this;',
  25009. ' var aProc = null;',
  25010. ' var b = false;',
  25011. ' function Sub(vK) {',
  25012. ' var aSub = null;',
  25013. ' function SubSub(vK) {',
  25014. ' var aSubSub = null;',
  25015. ' aProc = rtl.createCallback($Self, "DoIt");',
  25016. ' aSub = rtl.createCallback($Self, "DoIt");',
  25017. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  25018. ' aProc = Sub;',
  25019. ' aSub = Sub;',
  25020. ' aSubSub = Sub;',
  25021. ' aProc = SubSub;',
  25022. ' aSub = SubSub;',
  25023. ' aSubSub = SubSub;',
  25024. ' };',
  25025. ' };',
  25026. ' aProc = Sub;',
  25027. ' b = rtl.eqCallback(aProc, Sub);',
  25028. ' b = rtl.eqCallback(Sub, aProc);',
  25029. ' };',
  25030. '});',
  25031. '']),
  25032. LinesToStr([ // $mod.$main
  25033. '']));
  25034. end;
  25035. procedure TTestModule.TestProcType_ReferenceToProc;
  25036. begin
  25037. StartProgram(false);
  25038. Add([
  25039. 'type',
  25040. ' TProcRef = reference to procedure(i: longint = 0);',
  25041. ' TFuncRef = reference to function(i: longint = 0): longint;',
  25042. 'var',
  25043. ' p: TProcRef;',
  25044. ' f: TFuncRef;',
  25045. 'procedure DoIt(i: longint);',
  25046. 'begin',
  25047. 'end;',
  25048. 'function GetIt(i: longint): longint;',
  25049. 'begin',
  25050. ' p:=@DoIt;',
  25051. ' f:=@GetIt;',
  25052. ' f;',
  25053. ' f();',
  25054. ' f(1);',
  25055. 'end;',
  25056. 'begin',
  25057. ' p:=@DoIt;',
  25058. ' f:=@GetIt;',
  25059. ' f;',
  25060. ' f();',
  25061. ' f(1);',
  25062. ' p:=TProcRef(f);',
  25063. '']);
  25064. ConvertProgram;
  25065. CheckSource('TestProcType_ReferenceToProc',
  25066. LinesToStr([ // statements
  25067. 'this.p = null;',
  25068. 'this.f = null;',
  25069. 'this.DoIt = function (i) {',
  25070. '};',
  25071. 'this.GetIt = function (i) {',
  25072. ' var Result = 0;',
  25073. ' $mod.p = $mod.DoIt;',
  25074. ' $mod.f = $mod.GetIt;',
  25075. ' $mod.f(0);',
  25076. ' $mod.f(0);',
  25077. ' $mod.f(1);',
  25078. ' return Result;',
  25079. '};',
  25080. '']),
  25081. LinesToStr([ // $mod.$main
  25082. '$mod.p = $mod.DoIt;',
  25083. '$mod.f = $mod.GetIt;',
  25084. '$mod.f(0);',
  25085. '$mod.f(0);',
  25086. '$mod.f(1);',
  25087. '$mod.p = $mod.f;',
  25088. '']));
  25089. end;
  25090. procedure TTestModule.TestProcType_ReferenceToMethod;
  25091. begin
  25092. StartProgram(false);
  25093. Add([
  25094. 'type',
  25095. ' TFuncRef = reference to function(i: longint = 5): longint;',
  25096. ' TObject = class',
  25097. ' function Grow(s: longint): longint;',
  25098. ' end;',
  25099. 'var',
  25100. ' f: tfuncref;',
  25101. 'function tobject.grow(s: longint): longint;',
  25102. ' function GrowSub(i: longint): longint;',
  25103. ' begin',
  25104. ' f:=@grow;',
  25105. ' f:=@growsub;',
  25106. ' end;',
  25107. 'begin',
  25108. ' f:=@grow;',
  25109. ' f:=@growsub;',
  25110. 'end;',
  25111. 'begin',
  25112. '']);
  25113. ConvertProgram;
  25114. CheckSource('TestProcType_ReferenceToMethod',
  25115. LinesToStr([ // statements
  25116. 'rtl.createClass($mod, "TObject", null, function () {',
  25117. ' this.$init = function () {',
  25118. ' };',
  25119. ' this.$final = function () {',
  25120. ' };',
  25121. ' this.Grow = function (s) {',
  25122. ' var $Self = this;',
  25123. ' var Result = 0;',
  25124. ' function GrowSub(i) {',
  25125. ' var Result = 0;',
  25126. ' $mod.f = rtl.createCallback($Self, "Grow");',
  25127. ' $mod.f = GrowSub;',
  25128. ' return Result;',
  25129. ' };',
  25130. ' $mod.f = rtl.createCallback($Self, "Grow");',
  25131. ' $mod.f = GrowSub;',
  25132. ' return Result;',
  25133. ' };',
  25134. '});',
  25135. 'this.f = null;',
  25136. '']),
  25137. LinesToStr([ // $mod.$main
  25138. '']));
  25139. end;
  25140. procedure TTestModule.TestProcType_Typecast;
  25141. begin
  25142. StartProgram(false);
  25143. Add([
  25144. 'type',
  25145. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  25146. ' TEvent = procedure of object;',
  25147. ' TGetter = function:longint of object;',
  25148. ' TProcA = procedure(i: longint);',
  25149. ' TFuncB = function(i, j: longint): longint;',
  25150. 'procedure DoIt(); varargs; begin end;',
  25151. 'var',
  25152. ' Notify: tnotifyevent;',
  25153. ' Event: tevent;',
  25154. ' Getter: tgetter;',
  25155. ' ProcA: tproca;',
  25156. ' FuncB: tfuncb;',
  25157. ' p: pointer;',
  25158. 'begin',
  25159. ' notify:=tnotifyevent(event);',
  25160. ' event:=tevent(event);',
  25161. ' event:=tevent(notify);',
  25162. ' event:=tevent(getter);',
  25163. ' event:=tevent(proca);',
  25164. ' proca:=tproca(funcb);',
  25165. ' funcb:=tfuncb(funcb);',
  25166. ' funcb:=tfuncb(proca);',
  25167. ' funcb:=tfuncb(getter);',
  25168. ' proca:=tproca(p);',
  25169. ' funcb:=tfuncb(p);',
  25170. ' getter:=tgetter(p);',
  25171. ' p:=pointer(notify);',
  25172. ' p:=notify;',
  25173. ' p:=pointer(proca);',
  25174. ' p:=proca;',
  25175. ' p:=pointer(funcb);',
  25176. ' p:=funcb;',
  25177. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  25178. '']);
  25179. ConvertProgram;
  25180. CheckSource('TestProcType_Typecast',
  25181. LinesToStr([ // statements
  25182. 'this.DoIt = function () {',
  25183. '};',
  25184. 'this.Notify = null;',
  25185. 'this.Event = null;',
  25186. 'this.Getter = null;',
  25187. 'this.ProcA = null;',
  25188. 'this.FuncB = null;',
  25189. 'this.p = null;',
  25190. '']),
  25191. LinesToStr([ // $mod.$main
  25192. '$mod.Notify = $mod.Event;',
  25193. '$mod.Event = $mod.Event;',
  25194. '$mod.Event = $mod.Notify;',
  25195. '$mod.Event = $mod.Getter;',
  25196. '$mod.Event = $mod.ProcA;',
  25197. '$mod.ProcA = $mod.FuncB;',
  25198. '$mod.FuncB = $mod.FuncB;',
  25199. '$mod.FuncB = $mod.ProcA;',
  25200. '$mod.FuncB = $mod.Getter;',
  25201. '$mod.ProcA = $mod.p;',
  25202. '$mod.FuncB = $mod.p;',
  25203. '$mod.Getter = $mod.p;',
  25204. '$mod.p = $mod.Notify;',
  25205. '$mod.p = $mod.Notify;',
  25206. '$mod.p = $mod.ProcA;',
  25207. '$mod.p = $mod.ProcA;',
  25208. '$mod.p = $mod.FuncB;',
  25209. '$mod.p = $mod.FuncB;',
  25210. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  25211. '']));
  25212. end;
  25213. procedure TTestModule.TestProcType_PassProcToUntyped;
  25214. begin
  25215. StartProgram(false);
  25216. Add([
  25217. 'type',
  25218. ' TEvent = procedure of object;',
  25219. ' TFunc = function: longint;',
  25220. 'procedure DoIt(); varargs; begin end;',
  25221. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  25222. 'var',
  25223. ' Event: tevent;',
  25224. ' Func: TFunc;',
  25225. 'begin',
  25226. ' doit(event,func);',
  25227. ' dosome(event,event,event);',
  25228. ' dosome(func,func,func);',
  25229. '']);
  25230. ConvertProgram;
  25231. CheckSource('TestProcType_PassProcToUntyped',
  25232. LinesToStr([ // statements
  25233. 'this.DoIt = function () {',
  25234. '};',
  25235. 'this.DoSome = function (a, b, p) {',
  25236. '};',
  25237. 'this.Event = null;',
  25238. 'this.Func = null;',
  25239. '']),
  25240. LinesToStr([ // $mod.$main
  25241. '$mod.DoIt($mod.Event, $mod.Func);',
  25242. '$mod.DoSome($mod.Event, {',
  25243. ' p: $mod,',
  25244. ' get: function () {',
  25245. ' return this.p.Event;',
  25246. ' },',
  25247. ' set: function (v) {',
  25248. ' this.p.Event = v;',
  25249. ' }',
  25250. '}, $mod.Event);',
  25251. '$mod.DoSome($mod.Func, {',
  25252. ' p: $mod,',
  25253. ' get: function () {',
  25254. ' return this.p.Func;',
  25255. ' },',
  25256. ' set: function (v) {',
  25257. ' this.p.Func = v;',
  25258. ' }',
  25259. '}, $mod.Func);',
  25260. '']));
  25261. end;
  25262. procedure TTestModule.TestProcType_PassProcToArray;
  25263. begin
  25264. StartProgram(false);
  25265. Add([
  25266. 'type',
  25267. ' TFunc = function: longint;',
  25268. ' TArrFunc = array of TFunc;',
  25269. 'procedure DoIt(Arr: TArrFunc); begin end;',
  25270. 'function GetIt: longint; begin end;',
  25271. 'var',
  25272. ' Func: tfunc;',
  25273. 'begin',
  25274. ' doit([]);',
  25275. ' doit([@GetIt]);',
  25276. ' doit([Func]);',
  25277. '']);
  25278. ConvertProgram;
  25279. CheckSource('TestProcType_PassProcToArray',
  25280. LinesToStr([ // statements
  25281. 'this.DoIt = function (Arr) {',
  25282. '};',
  25283. 'this.GetIt = function () {',
  25284. ' var Result = 0;',
  25285. ' return Result;',
  25286. '};',
  25287. 'this.Func = null;',
  25288. '']),
  25289. LinesToStr([ // $mod.$main
  25290. '$mod.DoIt([]);',
  25291. '$mod.DoIt([$mod.GetIt]);',
  25292. '$mod.DoIt([$mod.Func]);',
  25293. '']));
  25294. end;
  25295. procedure TTestModule.TestPointer;
  25296. begin
  25297. StartProgram(false);
  25298. Add(['type',
  25299. ' TObject = class end;',
  25300. ' TClass = class of TObject;',
  25301. ' TArrInt = array of longint;',
  25302. 'const',
  25303. ' n = nil;',
  25304. 'var',
  25305. ' v: jsvalue;',
  25306. ' Obj: tobject;',
  25307. ' C: tclass;',
  25308. ' a: tarrint;',
  25309. ' p: Pointer = nil;',
  25310. ' s: string;',
  25311. 'begin',
  25312. ' p:=p;',
  25313. ' p:=nil;',
  25314. ' if p=nil then;',
  25315. ' if nil=p then;',
  25316. ' if Assigned(p) then;',
  25317. ' p:=Pointer(v);',
  25318. ' p:=obj;',
  25319. ' p:=c;',
  25320. ' p:=a;',
  25321. ' p:=tobject;',
  25322. ' obj:=TObject(p);',
  25323. ' c:=TClass(p);',
  25324. ' a:=TArrInt(p);',
  25325. ' p:=n;',
  25326. ' p:=Pointer(a);',
  25327. ' p:=pointer(s);',
  25328. ' s:=string(p);',
  25329. '']);
  25330. ConvertProgram;
  25331. CheckSource('TestPointer',
  25332. LinesToStr([ // statements
  25333. 'rtl.createClass($mod, "TObject", null, function () {',
  25334. ' this.$init = function () {',
  25335. ' };',
  25336. ' this.$final = function () {',
  25337. ' };',
  25338. '});',
  25339. 'this.n = null;',
  25340. 'this.v = undefined;',
  25341. 'this.Obj = null;',
  25342. 'this.C = null;',
  25343. 'this.a = [];',
  25344. 'this.p = null;',
  25345. 'this.s = "";',
  25346. '']),
  25347. LinesToStr([ // $mod.$main
  25348. '$mod.p = $mod.p;',
  25349. '$mod.p = null;',
  25350. 'if ($mod.p === null) ;',
  25351. 'if (null === $mod.p) ;',
  25352. 'if ($mod.p != null) ;',
  25353. '$mod.p = $mod.v;',
  25354. '$mod.p = $mod.Obj;',
  25355. '$mod.p = $mod.C;',
  25356. '$mod.p = $mod.a;',
  25357. '$mod.p = $mod.TObject;',
  25358. '$mod.Obj = $mod.p;',
  25359. '$mod.C = $mod.p;',
  25360. '$mod.a = $mod.p;',
  25361. '$mod.p = null;',
  25362. '$mod.p = $mod.a;',
  25363. '$mod.p = $mod.s;',
  25364. '$mod.s = $mod.p;',
  25365. '']));
  25366. end;
  25367. procedure TTestModule.TestPointer_Proc;
  25368. begin
  25369. StartProgram(false);
  25370. Add('type');
  25371. Add(' TObject = class');
  25372. Add(' procedure DoIt; virtual; abstract;');
  25373. Add(' end;');
  25374. Add('procedure DoSome; begin end;');
  25375. Add('var');
  25376. Add(' o: TObject;');
  25377. Add(' p: Pointer;');
  25378. Add('begin');
  25379. Add(' p:=@DoSome;');
  25380. Add(' p:[email protected];');
  25381. ConvertProgram;
  25382. CheckSource('TestPointer_Proc',
  25383. LinesToStr([ // statements
  25384. 'rtl.createClass($mod, "TObject", null, function () {',
  25385. ' this.$init = function () {',
  25386. ' };',
  25387. ' this.$final = function () {',
  25388. ' };',
  25389. '});',
  25390. 'this.DoSome = function () {',
  25391. '};',
  25392. 'this.o = null;',
  25393. 'this.p = null;',
  25394. '']),
  25395. LinesToStr([ // $mod.$main
  25396. '$mod.p = $mod.DoSome;',
  25397. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  25398. '']));
  25399. end;
  25400. procedure TTestModule.TestPointer_AssignRecordFail;
  25401. begin
  25402. StartProgram(false);
  25403. Add('type');
  25404. Add(' TRec = record end;');
  25405. Add('var');
  25406. Add(' p: Pointer;');
  25407. Add(' r: TRec;');
  25408. Add('begin');
  25409. Add(' p:=r;');
  25410. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  25411. nIncompatibleTypesGotExpected);
  25412. ConvertProgram;
  25413. end;
  25414. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  25415. begin
  25416. StartProgram(false);
  25417. Add('type');
  25418. Add(' TArr = array[boolean] of longint;');
  25419. Add('var');
  25420. Add(' p: Pointer;');
  25421. Add(' a: TArr;');
  25422. Add('begin');
  25423. Add(' p:=a;');
  25424. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  25425. nIncompatibleTypesGotExpected);
  25426. ConvertProgram;
  25427. end;
  25428. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  25429. begin
  25430. StartProgram(false);
  25431. Add([
  25432. 'procedure DoIt(args: array of jsvalue); begin end;',
  25433. 'procedure DoAll; varargs; begin end;',
  25434. 'var',
  25435. ' v: jsvalue;',
  25436. 'begin',
  25437. ' DoIt([pointer(v)]);',
  25438. ' DoAll(pointer(v));',
  25439. '']);
  25440. ConvertProgram;
  25441. CheckSource('TestPointer_TypeCastJSValueToPointer',
  25442. LinesToStr([ // statements
  25443. 'this.DoIt = function (args) {',
  25444. '};',
  25445. 'this.DoAll = function () {',
  25446. '};',
  25447. 'this.v = undefined;',
  25448. '']),
  25449. LinesToStr([ // $mod.$main
  25450. '$mod.DoIt([$mod.v]);',
  25451. '$mod.DoAll($mod.v);',
  25452. '']));
  25453. end;
  25454. procedure TTestModule.TestPointer_NonRecordFail;
  25455. begin
  25456. StartProgram(false);
  25457. Add([
  25458. 'type',
  25459. ' p = ^longint;',
  25460. 'begin',
  25461. '']);
  25462. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  25463. ConvertProgram;
  25464. end;
  25465. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  25466. begin
  25467. StartProgram(false);
  25468. Add([
  25469. 'procedure DoIt(p: ^longint); begin end;',
  25470. 'begin',
  25471. '']);
  25472. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  25473. ConvertProgram;
  25474. end;
  25475. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  25476. begin
  25477. StartProgram(false);
  25478. Add([
  25479. 'var p: ^longint;',
  25480. 'begin',
  25481. '']);
  25482. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  25483. ConvertProgram;
  25484. end;
  25485. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  25486. begin
  25487. StartProgram(false);
  25488. Add([
  25489. 'function DoIt: ^longint; begin end;',
  25490. 'begin',
  25491. '']);
  25492. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  25493. ConvertProgram;
  25494. end;
  25495. procedure TTestModule.TestPointer_AddrOperatorFail;
  25496. begin
  25497. StartProgram(false);
  25498. Add([
  25499. 'var i: longint;',
  25500. 'begin',
  25501. ' if @i=nil then ;',
  25502. '']);
  25503. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  25504. ConvertProgram;
  25505. end;
  25506. procedure TTestModule.TestPointer_ArrayParamsFail;
  25507. begin
  25508. StartProgram(false);
  25509. Add([
  25510. 'var',
  25511. ' p: Pointer;',
  25512. 'begin',
  25513. ' p:=p[1];',
  25514. '']);
  25515. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  25516. ConvertProgram;
  25517. end;
  25518. procedure TTestModule.TestPointer_PointerAddFail;
  25519. begin
  25520. StartProgram(false);
  25521. Add([
  25522. 'var',
  25523. ' p: Pointer;',
  25524. 'begin',
  25525. ' p:=p+1;',
  25526. '']);
  25527. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  25528. ConvertProgram;
  25529. end;
  25530. procedure TTestModule.TestPointer_IncPointerFail;
  25531. begin
  25532. StartProgram(false);
  25533. Add([
  25534. 'var',
  25535. ' p: Pointer;',
  25536. 'begin',
  25537. ' inc(p,1);',
  25538. '']);
  25539. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  25540. nIncompatibleTypeArgNo);
  25541. ConvertProgram;
  25542. end;
  25543. procedure TTestModule.TestPointer_Record;
  25544. begin
  25545. StartProgram(false);
  25546. Add([
  25547. 'type',
  25548. ' TRec = record x: longint; end;',
  25549. ' PRec = ^TRec;',
  25550. 'var',
  25551. ' r: TRec;',
  25552. ' p: PRec;',
  25553. ' q: ^TRec;',
  25554. ' Ptr: pointer;',
  25555. 'begin',
  25556. ' new(p);',
  25557. ' p:=@r;',
  25558. ' r:=p^;',
  25559. ' r.x:=p^.x;',
  25560. ' p^.x:=r.x;',
  25561. ' if p^.x=3 then ;',
  25562. ' if 4=p^.x then ;',
  25563. ' dispose(p);',
  25564. ' new(q);',
  25565. ' dispose(q);',
  25566. ' Ptr:=p;',
  25567. ' p:=PRec(ptr);',
  25568. '']);
  25569. ConvertProgram;
  25570. CheckSource('TestPointer_Record',
  25571. LinesToStr([ // statements
  25572. 'rtl.recNewT($mod, "TRec", function () {',
  25573. ' this.x = 0;',
  25574. ' this.$eq = function (b) {',
  25575. ' return this.x === b.x;',
  25576. ' };',
  25577. ' this.$assign = function (s) {',
  25578. ' this.x = s.x;',
  25579. ' return this;',
  25580. ' };',
  25581. '});',
  25582. 'this.r = $mod.TRec.$new();',
  25583. 'this.p = null;',
  25584. 'this.q = null;',
  25585. 'this.Ptr = null;',
  25586. '']),
  25587. LinesToStr([ // $mod.$main
  25588. '$mod.p = $mod.TRec.$new();',
  25589. '$mod.p = $mod.r;',
  25590. '$mod.r.$assign($mod.p);',
  25591. '$mod.r.x = $mod.p.x;',
  25592. '$mod.p.x = $mod.r.x;',
  25593. 'if ($mod.p.x === 3) ;',
  25594. 'if (4 === $mod.p.x) ;',
  25595. '$mod.p = null;',
  25596. '$mod.q = $mod.TRec.$new();',
  25597. '$mod.q = null;',
  25598. '$mod.Ptr = $mod.p;',
  25599. '$mod.p = $mod.Ptr;',
  25600. '']));
  25601. end;
  25602. procedure TTestModule.TestPointer_RecordArg;
  25603. begin
  25604. StartProgram(false);
  25605. Add([
  25606. '{$modeswitch autoderef}',
  25607. 'type',
  25608. ' TRec = record x: longint; end;',
  25609. ' PRec = ^TRec;',
  25610. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  25611. 'begin',
  25612. ' a.x:=a.x;',
  25613. ' a^.x:=a^.x;',
  25614. ' with a^ do',
  25615. ' x:=x;',
  25616. 'end;',
  25617. 'function GetIt(p: PRec): PRec;',
  25618. 'begin',
  25619. ' p.x:=p.x;',
  25620. ' p^.x:=p^.x;',
  25621. ' with p^ do',
  25622. ' x:=x;',
  25623. 'end;',
  25624. 'var',
  25625. ' r: TRec;',
  25626. ' p: PRec;',
  25627. 'begin',
  25628. ' p:=GetIt(p);',
  25629. ' p^:=GetIt(@r)^;',
  25630. ' DoIt(p,p,p);',
  25631. ' DoIt(@r,p,p);',
  25632. '']);
  25633. ConvertProgram;
  25634. CheckSource('TestPointer_Record',
  25635. LinesToStr([ // statements
  25636. 'rtl.recNewT($mod, "TRec", function () {',
  25637. ' this.x = 0;',
  25638. ' this.$eq = function (b) {',
  25639. ' return this.x === b.x;',
  25640. ' };',
  25641. ' this.$assign = function (s) {',
  25642. ' this.x = s.x;',
  25643. ' return this;',
  25644. ' };',
  25645. '});',
  25646. 'this.DoIt = function (a, b, c) {',
  25647. ' var Result = $mod.TRec.$new();',
  25648. ' a.x = a.x;',
  25649. ' a.x = a.x;',
  25650. ' a.x = a.x;',
  25651. ' return Result;',
  25652. '};',
  25653. 'this.GetIt = function (p) {',
  25654. ' var Result = null;',
  25655. ' p.x = p.x;',
  25656. ' p.x = p.x;',
  25657. ' p.x = p.x;',
  25658. ' return Result;',
  25659. '};',
  25660. 'this.r = $mod.TRec.$new();',
  25661. 'this.p = null;',
  25662. '']),
  25663. LinesToStr([ // $mod.$main
  25664. '$mod.p = $mod.GetIt($mod.p);',
  25665. '$mod.p.$assign($mod.GetIt($mod.r));',
  25666. '$mod.DoIt($mod.p, {',
  25667. ' p: $mod,',
  25668. ' get: function () {',
  25669. ' return this.p.p;',
  25670. ' },',
  25671. ' set: function (v) {',
  25672. ' this.p.p = v;',
  25673. ' }',
  25674. '}, {',
  25675. ' p: $mod,',
  25676. ' get: function () {',
  25677. ' return this.p.p;',
  25678. ' },',
  25679. ' set: function (v) {',
  25680. ' this.p.p = v;',
  25681. ' }',
  25682. '});',
  25683. '$mod.DoIt($mod.r, {',
  25684. ' p: $mod,',
  25685. ' get: function () {',
  25686. ' return this.p.p;',
  25687. ' },',
  25688. ' set: function (v) {',
  25689. ' this.p.p = v;',
  25690. ' }',
  25691. '}, {',
  25692. ' p: $mod,',
  25693. ' get: function () {',
  25694. ' return this.p.p;',
  25695. ' },',
  25696. ' set: function (v) {',
  25697. ' this.p.p = v;',
  25698. ' }',
  25699. '});',
  25700. '']));
  25701. end;
  25702. procedure TTestModule.TestJSValue_AssignToJSValue;
  25703. begin
  25704. StartProgram(false);
  25705. Add('var');
  25706. Add(' v: jsvalue;');
  25707. Add(' i: longint;');
  25708. Add(' s: string;');
  25709. Add(' b: boolean;');
  25710. Add(' d: double;');
  25711. Add(' p: pointer;');
  25712. Add('begin');
  25713. Add(' v:=v;');
  25714. Add(' v:=1;');
  25715. Add(' v:=i;');
  25716. Add(' v:='''';');
  25717. Add(' v:=''c'';');
  25718. Add(' v:=''foo'';');
  25719. Add(' v:=s;');
  25720. Add(' v:=false;');
  25721. Add(' v:=true;');
  25722. Add(' v:=b;');
  25723. Add(' v:=0.1;');
  25724. Add(' v:=d;');
  25725. Add(' v:=nil;');
  25726. Add(' v:=p;');
  25727. ConvertProgram;
  25728. CheckSource('TestJSValue_AssignToJSValue',
  25729. LinesToStr([ // statements
  25730. 'this.v = undefined;',
  25731. 'this.i = 0;',
  25732. 'this.s = "";',
  25733. 'this.b = false;',
  25734. 'this.d = 0.0;',
  25735. 'this.p = null;',
  25736. '']),
  25737. LinesToStr([ // $mod.$main
  25738. '$mod.v = $mod.v;',
  25739. '$mod.v = 1;',
  25740. '$mod.v = $mod.i;',
  25741. '$mod.v = "";',
  25742. '$mod.v = "c";',
  25743. '$mod.v = "foo";',
  25744. '$mod.v = $mod.s;',
  25745. '$mod.v = false;',
  25746. '$mod.v = true;',
  25747. '$mod.v = $mod.b;',
  25748. '$mod.v = 0.1;',
  25749. '$mod.v = $mod.d;',
  25750. '$mod.v = null;',
  25751. '$mod.v = $mod.p;',
  25752. '']));
  25753. end;
  25754. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  25755. begin
  25756. StartProgram(false);
  25757. Add('type');
  25758. Add(' integer = longint;');
  25759. Add(' TYesNo = boolean;');
  25760. Add(' TFloat = double;');
  25761. Add(' TCaption = string;');
  25762. Add(' TChar = char;');
  25763. Add('var');
  25764. Add(' v: jsvalue;');
  25765. Add(' i: integer;');
  25766. Add(' s: TCaption;');
  25767. Add(' b: TYesNo;');
  25768. Add(' d: TFloat;');
  25769. Add(' c: char;');
  25770. Add('begin');
  25771. Add(' i:=longint(v);');
  25772. Add(' i:=integer(v);');
  25773. Add(' s:=string(v);');
  25774. Add(' s:=TCaption(v);');
  25775. Add(' b:=boolean(v);');
  25776. Add(' b:=TYesNo(v);');
  25777. Add(' d:=double(v);');
  25778. Add(' d:=TFloat(v);');
  25779. Add(' c:=char(v);');
  25780. Add(' c:=TChar(v);');
  25781. ConvertProgram;
  25782. CheckSource('TestJSValue_TypeCastToBaseType',
  25783. LinesToStr([ // statements
  25784. 'this.v = undefined;',
  25785. 'this.i = 0;',
  25786. 'this.s = "";',
  25787. 'this.b = false;',
  25788. 'this.d = 0.0;',
  25789. 'this.c = "";',
  25790. '']),
  25791. LinesToStr([ // $mod.$main
  25792. '$mod.i = Math.floor($mod.v);',
  25793. '$mod.i = Math.floor($mod.v);',
  25794. '$mod.s = "" + $mod.v;',
  25795. '$mod.s = "" + $mod.v;',
  25796. '$mod.b = !($mod.v == false);',
  25797. '$mod.b = !($mod.v == false);',
  25798. '$mod.d = rtl.getNumber($mod.v);',
  25799. '$mod.d = rtl.getNumber($mod.v);',
  25800. '$mod.c = rtl.getChar($mod.v);',
  25801. '$mod.c = rtl.getChar($mod.v);',
  25802. '']));
  25803. end;
  25804. procedure TTestModule.TestJSValue_TypecastToJSValue;
  25805. begin
  25806. StartProgram(false);
  25807. Add([
  25808. 'type',
  25809. ' TArr = array of word;',
  25810. ' TRec = record end;',
  25811. ' TSet = set of boolean;',
  25812. 'procedure Fly(v: jsvalue);',
  25813. 'begin',
  25814. 'end;',
  25815. 'var',
  25816. ' a: TArr;',
  25817. ' r: TRec;',
  25818. ' s: TSet;',
  25819. 'begin',
  25820. ' Fly(jsvalue(a));',
  25821. ' Fly(jsvalue(r));',
  25822. ' Fly(jsvalue(s));',
  25823. '']);
  25824. ConvertProgram;
  25825. CheckSource('TestJSValue_TypecastToJSValue',
  25826. LinesToStr([ // statements
  25827. 'rtl.recNewT($mod, "TRec", function () {',
  25828. ' this.$eq = function (b) {',
  25829. ' return true;',
  25830. ' };',
  25831. ' this.$assign = function (s) {',
  25832. ' return this;',
  25833. ' };',
  25834. '});',
  25835. 'this.Fly = function (v) {',
  25836. '};',
  25837. 'this.a = [];',
  25838. 'this.r = $mod.TRec.$new();',
  25839. 'this.s = {};',
  25840. '']),
  25841. LinesToStr([ // $mod.$main
  25842. '$mod.Fly($mod.a);',
  25843. '$mod.Fly($mod.r);',
  25844. '$mod.Fly($mod.s);',
  25845. '']));
  25846. end;
  25847. procedure TTestModule.TestJSValue_Equal;
  25848. begin
  25849. StartProgram(false);
  25850. Add('type');
  25851. Add(' integer = longint;');
  25852. Add(' TYesNo = boolean;');
  25853. Add(' TFloat = double;');
  25854. Add(' TCaption = string;');
  25855. Add(' TChar = char;');
  25856. Add(' TMulti = JSValue;');
  25857. Add('var');
  25858. Add(' v: jsvalue;');
  25859. Add(' i: integer;');
  25860. Add(' s: TCaption;');
  25861. Add(' b: TYesNo;');
  25862. Add(' d: TFloat;');
  25863. Add(' c: char;');
  25864. Add(' m: TMulti;');
  25865. Add('begin');
  25866. Add(' b:=v=v;');
  25867. Add(' b:=v<>v;');
  25868. Add(' b:=v=1;');
  25869. Add(' b:=v<>1;');
  25870. Add(' b:=2=v;');
  25871. Add(' b:=2<>v;');
  25872. Add(' b:=v=i;');
  25873. Add(' b:=i=v;');
  25874. Add(' b:=v=nil;');
  25875. Add(' b:=nil=v;');
  25876. Add(' b:=v=false;');
  25877. Add(' b:=true=v;');
  25878. Add(' b:=v=b;');
  25879. Add(' b:=b=v;');
  25880. Add(' b:=v=s;');
  25881. Add(' b:=s=v;');
  25882. Add(' b:=v=''foo'';');
  25883. Add(' b:=''''=v;');
  25884. Add(' b:=v=d;');
  25885. Add(' b:=d=v;');
  25886. Add(' b:=v=3.4;');
  25887. Add(' b:=5.6=v;');
  25888. Add(' b:=v=c;');
  25889. Add(' b:=c=v;');
  25890. Add(' b:=m=m;');
  25891. Add(' b:=v=m;');
  25892. Add(' b:=m=v;');
  25893. ConvertProgram;
  25894. CheckSource('TestJSValue_Equal',
  25895. LinesToStr([ // statements
  25896. 'this.v = undefined;',
  25897. 'this.i = 0;',
  25898. 'this.s = "";',
  25899. 'this.b = false;',
  25900. 'this.d = 0.0;',
  25901. 'this.c = "";',
  25902. 'this.m = undefined;',
  25903. '']),
  25904. LinesToStr([ // $mod.$main
  25905. '$mod.b = $mod.v == $mod.v;',
  25906. '$mod.b = $mod.v != $mod.v;',
  25907. '$mod.b = $mod.v == 1;',
  25908. '$mod.b = $mod.v != 1;',
  25909. '$mod.b = 2 == $mod.v;',
  25910. '$mod.b = 2 != $mod.v;',
  25911. '$mod.b = $mod.v == $mod.i;',
  25912. '$mod.b = $mod.i == $mod.v;',
  25913. '$mod.b = $mod.v == null;',
  25914. '$mod.b = null == $mod.v;',
  25915. '$mod.b = $mod.v == false;',
  25916. '$mod.b = true == $mod.v;',
  25917. '$mod.b = $mod.v == $mod.b;',
  25918. '$mod.b = $mod.b == $mod.v;',
  25919. '$mod.b = $mod.v == $mod.s;',
  25920. '$mod.b = $mod.s == $mod.v;',
  25921. '$mod.b = $mod.v == "foo";',
  25922. '$mod.b = "" == $mod.v;',
  25923. '$mod.b = $mod.v == $mod.d;',
  25924. '$mod.b = $mod.d == $mod.v;',
  25925. '$mod.b = $mod.v == 3.4;',
  25926. '$mod.b = 5.6 == $mod.v;',
  25927. '$mod.b = $mod.v == $mod.c;',
  25928. '$mod.b = $mod.c == $mod.v;',
  25929. '$mod.b = $mod.m == $mod.m;',
  25930. '$mod.b = $mod.v == $mod.m;',
  25931. '$mod.b = $mod.m == $mod.v;',
  25932. '']));
  25933. end;
  25934. procedure TTestModule.TestJSValue_If;
  25935. begin
  25936. StartProgram(false);
  25937. Add([
  25938. 'procedure Fly(var u);',
  25939. 'begin',
  25940. ' if jsvalue(u) then ;',
  25941. 'end;',
  25942. 'var',
  25943. ' v: jsvalue;',
  25944. 'begin',
  25945. ' if v then ;',
  25946. ' while v do ;',
  25947. ' repeat until v;',
  25948. '']);
  25949. ConvertProgram;
  25950. CheckSource('TestJSValue_If',
  25951. LinesToStr([ // statements
  25952. 'this.Fly = function (u) {',
  25953. ' if (u.get()) ;',
  25954. '};',
  25955. 'this.v = undefined;',
  25956. '']),
  25957. LinesToStr([ // $mod.$main
  25958. 'if ($mod.v) ;',
  25959. 'while($mod.v){',
  25960. '};',
  25961. 'do{',
  25962. '} while(!$mod.v);',
  25963. '']));
  25964. end;
  25965. procedure TTestModule.TestJSValue_Not;
  25966. begin
  25967. StartProgram(false);
  25968. Add([
  25969. 'var',
  25970. ' v: jsvalue;',
  25971. ' b: boolean;',
  25972. 'begin',
  25973. ' b:=not v;',
  25974. ' if not v then ;',
  25975. ' while not v do ;',
  25976. ' repeat until not v;',
  25977. '']);
  25978. ConvertProgram;
  25979. CheckSource('TestJSValue_If',
  25980. LinesToStr([ // statements
  25981. 'this.v = undefined;',
  25982. 'this.b = false;',
  25983. '']),
  25984. LinesToStr([ // $mod.$main
  25985. '$mod.b=!$mod.v;',
  25986. 'if (!$mod.v) ;',
  25987. 'while(!$mod.v){',
  25988. '};',
  25989. 'do{',
  25990. '} while($mod.v);',
  25991. '']));
  25992. end;
  25993. procedure TTestModule.TestJSValue_Enum;
  25994. begin
  25995. StartProgram(false);
  25996. Add('type');
  25997. Add(' TColor = (red, blue);');
  25998. Add(' TRedBlue = TColor;');
  25999. Add('var');
  26000. Add(' v: jsvalue;');
  26001. Add(' e: TColor;');
  26002. Add('begin');
  26003. Add(' v:=e;');
  26004. Add(' v:=TColor(e);');
  26005. Add(' v:=TRedBlue(e);');
  26006. Add(' e:=TColor(v);');
  26007. Add(' e:=TRedBlue(v);');
  26008. ConvertProgram;
  26009. CheckSource('TestJSValue_Enum',
  26010. LinesToStr([ // statements
  26011. 'this.TColor = {',
  26012. ' "0": "red",',
  26013. ' red: 0,',
  26014. ' "1": "blue",',
  26015. ' blue: 1',
  26016. '};',
  26017. 'this.v = undefined;',
  26018. 'this.e = 0;',
  26019. '']),
  26020. LinesToStr([ // $mod.$main
  26021. '$mod.v = $mod.e;',
  26022. '$mod.v = $mod.e;',
  26023. '$mod.v = $mod.e;',
  26024. '$mod.e = $mod.v;',
  26025. '$mod.e = $mod.v;',
  26026. '']));
  26027. end;
  26028. procedure TTestModule.TestJSValue_ClassInstance;
  26029. begin
  26030. StartProgram(false);
  26031. Add([
  26032. 'type',
  26033. ' TObject = class',
  26034. ' end;',
  26035. ' TBirdObject = TObject;',
  26036. 'var',
  26037. ' v: jsvalue;',
  26038. ' o: TObject;',
  26039. 'begin',
  26040. ' v:=o;',
  26041. ' v:=TObject(o);',
  26042. ' v:=TBirdObject(o);',
  26043. ' o:=TObject(v);',
  26044. ' o:=TBirdObject(v);',
  26045. ' if v is TObject then ;',
  26046. '']);
  26047. ConvertProgram;
  26048. CheckSource('TestJSValue_ClassInstance',
  26049. LinesToStr([ // statements
  26050. 'rtl.createClass($mod, "TObject", null, function () {',
  26051. ' this.$init = function () {',
  26052. ' };',
  26053. ' this.$final = function () {',
  26054. ' };',
  26055. '});',
  26056. 'this.v = undefined;',
  26057. 'this.o = null;',
  26058. '']),
  26059. LinesToStr([ // $mod.$main
  26060. '$mod.v = $mod.o;',
  26061. '$mod.v = $mod.o;',
  26062. '$mod.v = $mod.o;',
  26063. '$mod.o = rtl.getObject($mod.v);',
  26064. '$mod.o = rtl.getObject($mod.v);',
  26065. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  26066. '']));
  26067. end;
  26068. procedure TTestModule.TestJSValue_ClassOf;
  26069. begin
  26070. StartProgram(false);
  26071. Add([
  26072. 'type',
  26073. ' TClass = class of TObject;',
  26074. ' TObject = class',
  26075. ' end;',
  26076. ' TBirds = class of TBird;',
  26077. ' TBird = class(TObject) end;',
  26078. 'var',
  26079. ' v: jsvalue;',
  26080. ' c: TClass;',
  26081. 'begin',
  26082. ' v:=c;',
  26083. ' v:=TObject;',
  26084. ' v:=TClass(c);',
  26085. ' v:=TBirds(c);',
  26086. ' c:=TClass(v);',
  26087. ' c:=TBirds(v);',
  26088. ' if v is TClass then ;',
  26089. '']);
  26090. ConvertProgram;
  26091. CheckSource('TestJSValue_ClassOf',
  26092. LinesToStr([ // statements
  26093. 'rtl.createClass($mod, "TObject", null, function () {',
  26094. ' this.$init = function () {',
  26095. ' };',
  26096. ' this.$final = function () {',
  26097. ' };',
  26098. '});',
  26099. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  26100. '});',
  26101. 'this.v = undefined;',
  26102. 'this.c = null;',
  26103. '']),
  26104. LinesToStr([ // $mod.$main
  26105. '$mod.v = $mod.c;',
  26106. '$mod.v = $mod.TObject;',
  26107. '$mod.v = $mod.c;',
  26108. '$mod.v = $mod.c;',
  26109. '$mod.c = rtl.getObject($mod.v);',
  26110. '$mod.c = rtl.getObject($mod.v);',
  26111. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  26112. '']));
  26113. end;
  26114. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  26115. begin
  26116. StartProgram(false);
  26117. Add([
  26118. 'type',
  26119. ' integer = longint;',
  26120. ' TArray = array of JSValue;',
  26121. ' TArrgh = tarray;',
  26122. ' TArrInt = array of integer;',
  26123. 'var',
  26124. ' v: jsvalue;',
  26125. ' TheArray: tarray = (1,''2'');',
  26126. ' Arr: tarrgh;',
  26127. ' i: integer;',
  26128. ' ArrInt: tarrint;',
  26129. 'begin',
  26130. ' arr:=thearray;',
  26131. ' thearray:=arr;',
  26132. ' setlength(arr,2);',
  26133. ' setlength(thearray,3);',
  26134. ' arr[4]:=v;',
  26135. ' arr[5]:=length(thearray);',
  26136. ' arr[6]:=nil;',
  26137. ' arr[7]:=thearray[8];',
  26138. ' arr[low(arr)]:=high(thearray);',
  26139. ' arr:=arrint;',
  26140. ' arrInt:=tarrint(arr);',
  26141. ' if TheArray = nil then ;',
  26142. ' if nil = TheArray then ;',
  26143. ' if TheArray <> nil then ;',
  26144. ' if nil <> TheArray then ;',
  26145. '']);
  26146. ConvertProgram;
  26147. CheckSource('TestJSValue_ArrayOfJSValue',
  26148. LinesToStr([ // statements
  26149. 'this.v = undefined;',
  26150. 'this.TheArray = [1, "2"];',
  26151. 'this.Arr = [];',
  26152. 'this.i = 0;',
  26153. 'this.ArrInt = [];',
  26154. '']),
  26155. LinesToStr([ // $mod.$main
  26156. '$mod.Arr = $mod.TheArray;',
  26157. '$mod.TheArray = $mod.Arr;',
  26158. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  26159. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  26160. '$mod.Arr[4] = $mod.v;',
  26161. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  26162. '$mod.Arr[6] = null;',
  26163. '$mod.Arr[7] = $mod.TheArray[8];',
  26164. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  26165. '$mod.Arr = $mod.ArrInt;',
  26166. '$mod.ArrInt = $mod.Arr;',
  26167. 'if (rtl.length($mod.TheArray) === 0) ;',
  26168. 'if (rtl.length($mod.TheArray) === 0) ;',
  26169. 'if (rtl.length($mod.TheArray) > 0) ;',
  26170. 'if (rtl.length($mod.TheArray) > 0) ;',
  26171. '']));
  26172. end;
  26173. procedure TTestModule.TestJSValue_ArrayLit;
  26174. begin
  26175. StartProgram(false);
  26176. Add([
  26177. 'type',
  26178. ' TFlag = (big,small);',
  26179. ' TArray = array of JSValue;',
  26180. ' TObject = class end;',
  26181. ' TClass = class of TObject;',
  26182. 'var',
  26183. ' v: jsvalue;',
  26184. ' a: TArray;',
  26185. ' o: TObject;',
  26186. 'begin',
  26187. ' a:=[];',
  26188. ' a:=[1];',
  26189. ' a:=[1,2];',
  26190. ' a:=[big];',
  26191. ' a:=[1,big];',
  26192. ' a:=[o,nil];',
  26193. '']);
  26194. ConvertProgram;
  26195. CheckSource('TestJSValue_ArrayLit',
  26196. LinesToStr([ // statements
  26197. 'this.TFlag = {',
  26198. ' "0": "big",',
  26199. ' big: 0,',
  26200. ' "1": "small",',
  26201. ' small: 1',
  26202. '};',
  26203. 'rtl.createClass($mod, "TObject", null, function () {',
  26204. ' this.$init = function () {',
  26205. ' };',
  26206. ' this.$final = function () {',
  26207. ' };',
  26208. '});',
  26209. 'this.v = undefined;',
  26210. 'this.a = [];',
  26211. 'this.o = null;',
  26212. '']),
  26213. LinesToStr([ // $mod.$main
  26214. '$mod.a = [];',
  26215. '$mod.a = [1];',
  26216. '$mod.a = [1, 2];',
  26217. '$mod.a = [$mod.TFlag.big];',
  26218. '$mod.a = [1, $mod.TFlag.big];',
  26219. '$mod.a = [$mod.o, null];',
  26220. '']));
  26221. end;
  26222. procedure TTestModule.TestJSValue_Params;
  26223. begin
  26224. StartProgram(false);
  26225. Add('type');
  26226. Add(' integer = longint;');
  26227. Add(' TYesNo = boolean;');
  26228. Add(' TFloat = double;');
  26229. Add(' TCaption = string;');
  26230. Add(' TChar = char;');
  26231. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  26232. Add('var');
  26233. Add(' l: jsvalue;');
  26234. Add('begin');
  26235. Add(' a:=a;');
  26236. Add(' l:=b;');
  26237. Add(' c:=c;');
  26238. Add(' d:=d;');
  26239. Add(' Result:=l;');
  26240. Add('end;');
  26241. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  26242. Add('var');
  26243. Add(' v: jsvalue;');
  26244. Add(' i: integer;');
  26245. Add(' b: TYesNo;');
  26246. Add(' d: TFloat;');
  26247. Add(' s: TCaption;');
  26248. Add(' c: TChar;');
  26249. Add('begin');
  26250. Add(' v:=doit(v,v,v,v);');
  26251. Add(' i:=integer(dosome(i,i));');
  26252. Add(' b:=TYesNo(dosome(b,b));');
  26253. Add(' d:=TFloat(dosome(d,d));');
  26254. Add(' s:=TCaption(dosome(s,s));');
  26255. Add(' c:=TChar(dosome(c,c));');
  26256. ConvertProgram;
  26257. CheckSource('TestJSValue_Params',
  26258. LinesToStr([ // statements
  26259. 'this.DoIt = function (a, b, c, d) {',
  26260. ' var Result = undefined;',
  26261. ' var l = undefined;',
  26262. ' a = a;',
  26263. ' l = b;',
  26264. ' c.set(c.get());',
  26265. ' d.set(d.get());',
  26266. ' Result = l;',
  26267. ' return Result;',
  26268. '};',
  26269. 'this.DoSome = function (a, b) {',
  26270. ' var Result = undefined;',
  26271. ' return Result;',
  26272. '};',
  26273. 'this.v = undefined;',
  26274. 'this.i = 0;',
  26275. 'this.b = false;',
  26276. 'this.d = 0.0;',
  26277. 'this.s = "";',
  26278. 'this.c = "";',
  26279. '']),
  26280. LinesToStr([ // $mod.$main
  26281. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  26282. ' p: $mod,',
  26283. ' get: function () {',
  26284. ' return this.p.v;',
  26285. ' },',
  26286. ' set: function (v) {',
  26287. ' this.p.v = v;',
  26288. ' }',
  26289. '}, {',
  26290. ' p: $mod,',
  26291. ' get: function () {',
  26292. ' return this.p.v;',
  26293. ' },',
  26294. ' set: function (v) {',
  26295. ' this.p.v = v;',
  26296. ' }',
  26297. '});',
  26298. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  26299. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  26300. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  26301. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  26302. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  26303. '']));
  26304. end;
  26305. procedure TTestModule.TestJSValue_UntypedParam;
  26306. begin
  26307. StartProgram(false);
  26308. Add('function DoIt(const a; var b; out c): jsvalue;');
  26309. Add('begin');
  26310. Add(' Result:=a;');
  26311. Add(' Result:=b;');
  26312. Add(' Result:=c;');
  26313. Add(' b:=Result;');
  26314. Add(' c:=Result;');
  26315. Add('end;');
  26316. Add('var i: longint;');
  26317. Add('begin');
  26318. Add(' doit(i,i,i);');
  26319. ConvertProgram;
  26320. CheckSource('TestJSValue_UntypedParam',
  26321. LinesToStr([ // statements
  26322. 'this.DoIt = function (a, b, c) {',
  26323. ' var Result = undefined;',
  26324. ' Result = a;',
  26325. ' Result = b.get();',
  26326. ' Result = c.get();',
  26327. ' b.set(Result);',
  26328. ' c.set(Result);',
  26329. ' return Result;',
  26330. '};',
  26331. 'this.i = 0;',
  26332. '']),
  26333. LinesToStr([ // $mod.$main
  26334. '$mod.DoIt($mod.i, {',
  26335. ' p: $mod,',
  26336. ' get: function () {',
  26337. ' return this.p.i;',
  26338. ' },',
  26339. ' set: function (v) {',
  26340. ' this.p.i = v;',
  26341. ' }',
  26342. '}, {',
  26343. ' p: $mod,',
  26344. ' get: function () {',
  26345. ' return this.p.i;',
  26346. ' },',
  26347. ' set: function (v) {',
  26348. ' this.p.i = v;',
  26349. ' }',
  26350. '});',
  26351. '']));
  26352. end;
  26353. procedure TTestModule.TestJSValue_FuncResultType;
  26354. begin
  26355. StartProgram(false);
  26356. Add('type');
  26357. Add(' integer = longint;');
  26358. Add(' TJSValueArray = array of JSValue;');
  26359. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  26360. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  26361. Add('begin');
  26362. Add(' while Compare(P,aList[0])>0 do ;');
  26363. Add('end;');
  26364. Add('var');
  26365. Add(' Compare: TListSortCompare;');
  26366. Add(' V: JSValue;');
  26367. Add(' i: integer;');
  26368. Add('begin');
  26369. Add(' if Compare(V,V)>0 then ;');
  26370. Add(' if Compare(i,i)>1 then ;');
  26371. Add(' if Compare(nil,false)>2 then ;');
  26372. Add(' if Compare(1,true)>3 then ;');
  26373. ConvertProgram;
  26374. CheckSource('TestJSValue_UntypedParam',
  26375. LinesToStr([ // statements
  26376. 'this.Sort = function (P, aList, Compare) {',
  26377. ' while (Compare(P, aList[0]) > 0) {',
  26378. ' };',
  26379. '};',
  26380. 'this.Compare = null;',
  26381. 'this.V = undefined;',
  26382. 'this.i = 0;',
  26383. '']),
  26384. LinesToStr([ // $mod.$main
  26385. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  26386. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  26387. 'if ($mod.Compare(null, false) > 2) ;',
  26388. 'if ($mod.Compare(1, true) > 3) ;',
  26389. '']));
  26390. end;
  26391. procedure TTestModule.TestJSValue_ProcType_Assign;
  26392. begin
  26393. StartProgram(false);
  26394. Add('type');
  26395. Add(' integer = longint;');
  26396. Add(' TObject = class');
  26397. Add(' class function GetGlob: integer;');
  26398. Add(' function Getter: integer;');
  26399. Add(' end;');
  26400. Add('class function TObject.GetGlob: integer;');
  26401. Add('var v1: jsvalue;');
  26402. Add('begin');
  26403. Add(' v1:=@GetGlob;');
  26404. Add(' v1:[email protected];');
  26405. Add('end;');
  26406. Add('function TObject.Getter: integer;');
  26407. Add('var v2: jsvalue;');
  26408. Add('begin');
  26409. Add(' v2:=@Getter;');
  26410. Add(' v2:[email protected];');
  26411. Add(' v2:=@GetGlob;');
  26412. Add(' v2:[email protected];');
  26413. Add('end;');
  26414. Add('function GetIt(i: integer): integer;');
  26415. Add('var v3: jsvalue;');
  26416. Add('begin');
  26417. Add(' v3:=@GetIt;');
  26418. Add('end;');
  26419. Add('var');
  26420. Add(' V: JSValue;');
  26421. Add(' o: TObject;');
  26422. Add('begin');
  26423. Add(' v:=@GetIt;');
  26424. Add(' v:[email protected];');
  26425. Add(' v:[email protected];');
  26426. ConvertProgram;
  26427. CheckSource('TestJSValue_ProcType_Assign',
  26428. LinesToStr([ // statements
  26429. 'rtl.createClass($mod, "TObject", null, function () {',
  26430. ' this.$init = function () {',
  26431. ' };',
  26432. ' this.$final = function () {',
  26433. ' };',
  26434. ' this.GetGlob = function () {',
  26435. ' var Result = 0;',
  26436. ' var v1 = undefined;',
  26437. ' v1 = rtl.createCallback(this, "GetGlob");',
  26438. ' v1 = rtl.createCallback(this, "GetGlob");',
  26439. ' return Result;',
  26440. ' };',
  26441. ' this.Getter = function () {',
  26442. ' var Result = 0;',
  26443. ' var v2 = undefined;',
  26444. ' v2 = rtl.createCallback(this, "Getter");',
  26445. ' v2 = rtl.createCallback(this, "Getter");',
  26446. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  26447. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  26448. ' return Result;',
  26449. ' };',
  26450. '});',
  26451. 'this.GetIt = function (i) {',
  26452. ' var Result = 0;',
  26453. ' var v3 = undefined;',
  26454. ' v3 = $mod.GetIt;',
  26455. ' return Result;',
  26456. '};',
  26457. 'this.V = undefined;',
  26458. 'this.o = null;',
  26459. '']),
  26460. LinesToStr([ // $mod.$main
  26461. '$mod.V = $mod.GetIt;',
  26462. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  26463. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  26464. '']));
  26465. end;
  26466. procedure TTestModule.TestJSValue_ProcType_Equal;
  26467. begin
  26468. StartProgram(false);
  26469. Add('type');
  26470. Add(' integer = longint;');
  26471. Add(' TObject = class');
  26472. Add(' class function GetGlob: integer;');
  26473. Add(' function Getter: integer;');
  26474. Add(' end;');
  26475. Add('class function TObject.GetGlob: integer;');
  26476. Add('var v1: jsvalue;');
  26477. Add('begin');
  26478. Add(' if v1=@GetGlob then;');
  26479. Add(' if [email protected] then ;');
  26480. Add('end;');
  26481. Add('function TObject.Getter: integer;');
  26482. Add('var v2: jsvalue;');
  26483. Add('begin');
  26484. Add(' if v2=@Getter then;');
  26485. Add(' if [email protected] then ;');
  26486. Add(' if v2=@GetGlob then;');
  26487. Add(' if [email protected] then;');
  26488. Add('end;');
  26489. Add('function GetIt(i: integer): integer;');
  26490. Add('var v3: jsvalue;');
  26491. Add('begin');
  26492. Add(' if v3=@GetIt then;');
  26493. Add('end;');
  26494. Add('var');
  26495. Add(' V: JSValue;');
  26496. Add(' o: TObject;');
  26497. Add('begin');
  26498. Add(' if v=@GetIt then;');
  26499. Add(' if [email protected] then;');
  26500. Add(' if [email protected] then;');
  26501. Add(' if @GetIt=v then;');
  26502. Add(' if @o.Getter=v then;');
  26503. Add(' if @o.GetGlob=v then;');
  26504. ConvertProgram;
  26505. CheckSource('TestJSValue_ProcType_Equal',
  26506. LinesToStr([ // statements
  26507. 'rtl.createClass($mod, "TObject", null, function () {',
  26508. ' this.$init = function () {',
  26509. ' };',
  26510. ' this.$final = function () {',
  26511. ' };',
  26512. ' this.GetGlob = function () {',
  26513. ' var Result = 0;',
  26514. ' var v1 = undefined;',
  26515. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  26516. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  26517. ' return Result;',
  26518. ' };',
  26519. ' this.Getter = function () {',
  26520. ' var Result = 0;',
  26521. ' var v2 = undefined;',
  26522. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  26523. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  26524. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  26525. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  26526. ' return Result;',
  26527. ' };',
  26528. '});',
  26529. 'this.GetIt = function (i) {',
  26530. ' var Result = 0;',
  26531. ' var v3 = undefined;',
  26532. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  26533. ' return Result;',
  26534. '};',
  26535. 'this.V = undefined;',
  26536. 'this.o = null;',
  26537. '']),
  26538. LinesToStr([ // $mod.$main
  26539. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  26540. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  26541. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  26542. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  26543. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  26544. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  26545. '']));
  26546. end;
  26547. procedure TTestModule.TestJSValue_ProcType_Param;
  26548. begin
  26549. StartProgram(false);
  26550. Add([
  26551. 'type',
  26552. ' variant = jsvalue;',
  26553. ' TArrVariant = array of variant;',
  26554. ' TArrVar2 = TArrVariant;',
  26555. ' TFuncInt = function: longint;',
  26556. 'function GetIt: longint;',
  26557. 'begin',
  26558. 'end;',
  26559. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  26560. 'var v: variant;',
  26561. 'begin',
  26562. ' v:=arr[1];',
  26563. 'end;',
  26564. 'var s: string;',
  26565. 'begin',
  26566. ' DoIt(GetIt,[]);',
  26567. ' DoIt(@GetIt,[]);',
  26568. ' DoIt(1,[s,GetIt]);',
  26569. ' DoIt(1,[s,@GetIt]);',
  26570. '']);
  26571. ConvertProgram;
  26572. CheckSource('TestJSValue_ProcType_Param',
  26573. LinesToStr([ // statements
  26574. 'this.GetIt = function () {',
  26575. ' var Result = 0;',
  26576. ' return Result;',
  26577. '};',
  26578. 'this.DoIt = function (p, Arr) {',
  26579. ' var v = undefined;',
  26580. ' v = Arr[1];',
  26581. '};',
  26582. 'this.s = "";',
  26583. '']),
  26584. LinesToStr([ // $mod.$main
  26585. '$mod.DoIt($mod.GetIt(), []);',
  26586. '$mod.DoIt($mod.GetIt, []);',
  26587. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  26588. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  26589. '']));
  26590. end;
  26591. procedure TTestModule.TestJSValue_AssignToPointerFail;
  26592. begin
  26593. StartProgram(false);
  26594. Add([
  26595. 'var',
  26596. ' v: JSValue;',
  26597. ' p: Pointer;',
  26598. 'begin',
  26599. ' p:=v;',
  26600. '']);
  26601. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  26602. nIncompatibleTypesGotExpected);
  26603. ConvertProgram;
  26604. end;
  26605. procedure TTestModule.TestJSValue_OverloadDouble;
  26606. begin
  26607. StartProgram(false);
  26608. Add([
  26609. 'type',
  26610. ' integer = longint;',
  26611. ' tdatetime = double;',
  26612. 'procedure DoIt(d: double); begin end;',
  26613. 'procedure DoIt(v: jsvalue); begin end;',
  26614. 'var',
  26615. ' d: double;',
  26616. ' dt: tdatetime;',
  26617. ' i: integer;',
  26618. ' b: byte;',
  26619. ' shi: shortint;',
  26620. ' w: word;',
  26621. ' smi: smallint;',
  26622. ' lw: longword;',
  26623. ' li: longint;',
  26624. ' ni: nativeint;',
  26625. ' nu: nativeuint;',
  26626. 'begin',
  26627. ' DoIt(d);',
  26628. ' DoIt(dt);',
  26629. ' DoIt(i);',
  26630. ' DoIt(b);',
  26631. ' DoIt(shi);',
  26632. ' DoIt(w);',
  26633. ' DoIt(smi);',
  26634. ' DoIt(lw);',
  26635. ' DoIt(li);',
  26636. ' DoIt(ni);',
  26637. ' DoIt(nu);',
  26638. '']);
  26639. ConvertProgram;
  26640. CheckSource('TestJSValue_OverloadDouble',
  26641. LinesToStr([ // statements
  26642. 'this.DoIt = function (d) {',
  26643. '};',
  26644. 'this.DoIt$1 = function (v) {',
  26645. '};',
  26646. 'this.d = 0.0;',
  26647. 'this.dt = 0.0;',
  26648. 'this.i = 0;',
  26649. 'this.b = 0;',
  26650. 'this.shi = 0;',
  26651. 'this.w = 0;',
  26652. 'this.smi = 0;',
  26653. 'this.lw = 0;',
  26654. 'this.li = 0;',
  26655. 'this.ni = 0;',
  26656. 'this.nu = 0;',
  26657. '']),
  26658. LinesToStr([ // $mod.$main
  26659. '$mod.DoIt($mod.d);',
  26660. '$mod.DoIt($mod.dt);',
  26661. '$mod.DoIt$1($mod.i);',
  26662. '$mod.DoIt$1($mod.b);',
  26663. '$mod.DoIt$1($mod.shi);',
  26664. '$mod.DoIt$1($mod.w);',
  26665. '$mod.DoIt$1($mod.smi);',
  26666. '$mod.DoIt$1($mod.lw);',
  26667. '$mod.DoIt$1($mod.li);',
  26668. '$mod.DoIt$1($mod.ni);',
  26669. '$mod.DoIt$1($mod.nu);',
  26670. '']));
  26671. end;
  26672. procedure TTestModule.TestJSValue_OverloadNativeInt;
  26673. begin
  26674. StartProgram(false);
  26675. Add([
  26676. 'type',
  26677. ' integer = longint;',
  26678. ' int53 = nativeint;',
  26679. ' tdatetime = double;',
  26680. 'procedure DoIt(n: nativeint); begin end;',
  26681. 'procedure DoIt(v: jsvalue); begin end;',
  26682. 'var',
  26683. ' d: double;',
  26684. ' dt: tdatetime;',
  26685. ' i: integer;',
  26686. ' b: byte;',
  26687. ' shi: shortint;',
  26688. ' w: word;',
  26689. ' smi: smallint;',
  26690. ' lw: longword;',
  26691. ' li: longint;',
  26692. ' ni: nativeint;',
  26693. ' nu: nativeuint;',
  26694. 'begin',
  26695. ' DoIt(d);',
  26696. ' DoIt(dt);',
  26697. ' DoIt(i);',
  26698. ' DoIt(b);',
  26699. ' DoIt(shi);',
  26700. ' DoIt(w);',
  26701. ' DoIt(smi);',
  26702. ' DoIt(lw);',
  26703. ' DoIt(li);',
  26704. ' DoIt(ni);',
  26705. ' DoIt(nu);',
  26706. '']);
  26707. ConvertProgram;
  26708. CheckSource('TestJSValue_OverloadNativeInt',
  26709. LinesToStr([ // statements
  26710. 'this.DoIt = function (n) {',
  26711. '};',
  26712. 'this.DoIt$1 = function (v) {',
  26713. '};',
  26714. 'this.d = 0.0;',
  26715. 'this.dt = 0.0;',
  26716. 'this.i = 0;',
  26717. 'this.b = 0;',
  26718. 'this.shi = 0;',
  26719. 'this.w = 0;',
  26720. 'this.smi = 0;',
  26721. 'this.lw = 0;',
  26722. 'this.li = 0;',
  26723. 'this.ni = 0;',
  26724. 'this.nu = 0;',
  26725. '']),
  26726. LinesToStr([ // $mod.$main
  26727. '$mod.DoIt$1($mod.d);',
  26728. '$mod.DoIt$1($mod.dt);',
  26729. '$mod.DoIt($mod.i);',
  26730. '$mod.DoIt($mod.b);',
  26731. '$mod.DoIt($mod.shi);',
  26732. '$mod.DoIt($mod.w);',
  26733. '$mod.DoIt($mod.smi);',
  26734. '$mod.DoIt($mod.lw);',
  26735. '$mod.DoIt($mod.li);',
  26736. '$mod.DoIt($mod.ni);',
  26737. '$mod.DoIt($mod.nu);',
  26738. '']));
  26739. end;
  26740. procedure TTestModule.TestJSValue_OverloadWord;
  26741. begin
  26742. StartProgram(false);
  26743. Add([
  26744. 'type',
  26745. ' integer = longint;',
  26746. ' int53 = nativeint;',
  26747. ' tdatetime = double;',
  26748. 'procedure DoIt(w: word); begin end;',
  26749. 'procedure DoIt(v: jsvalue); begin end;',
  26750. 'var',
  26751. ' d: double;',
  26752. ' dt: tdatetime;',
  26753. ' i: integer;',
  26754. ' b: byte;',
  26755. ' shi: shortint;',
  26756. ' w: word;',
  26757. ' smi: smallint;',
  26758. ' lw: longword;',
  26759. ' li: longint;',
  26760. ' ni: nativeint;',
  26761. ' nu: nativeuint;',
  26762. 'begin',
  26763. ' DoIt(d);',
  26764. ' DoIt(dt);',
  26765. ' DoIt(i);',
  26766. ' DoIt(b);',
  26767. ' DoIt(shi);',
  26768. ' DoIt(w);',
  26769. ' DoIt(smi);',
  26770. ' DoIt(lw);',
  26771. ' DoIt(li);',
  26772. ' DoIt(ni);',
  26773. ' DoIt(nu);',
  26774. '']);
  26775. ConvertProgram;
  26776. CheckSource('TestJSValue_OverloadWord',
  26777. LinesToStr([ // statements
  26778. 'this.DoIt = function (w) {',
  26779. '};',
  26780. 'this.DoIt$1 = function (v) {',
  26781. '};',
  26782. 'this.d = 0.0;',
  26783. 'this.dt = 0.0;',
  26784. 'this.i = 0;',
  26785. 'this.b = 0;',
  26786. 'this.shi = 0;',
  26787. 'this.w = 0;',
  26788. 'this.smi = 0;',
  26789. 'this.lw = 0;',
  26790. 'this.li = 0;',
  26791. 'this.ni = 0;',
  26792. 'this.nu = 0;',
  26793. '']),
  26794. LinesToStr([ // $mod.$main
  26795. '$mod.DoIt$1($mod.d);',
  26796. '$mod.DoIt$1($mod.dt);',
  26797. '$mod.DoIt$1($mod.i);',
  26798. '$mod.DoIt($mod.b);',
  26799. '$mod.DoIt($mod.shi);',
  26800. '$mod.DoIt($mod.w);',
  26801. '$mod.DoIt$1($mod.smi);',
  26802. '$mod.DoIt$1($mod.lw);',
  26803. '$mod.DoIt$1($mod.li);',
  26804. '$mod.DoIt$1($mod.ni);',
  26805. '$mod.DoIt$1($mod.nu);',
  26806. '']));
  26807. end;
  26808. procedure TTestModule.TestJSValue_OverloadString;
  26809. begin
  26810. StartProgram(false);
  26811. Add([
  26812. 'type',
  26813. ' uni = string;',
  26814. ' WChar = char;',
  26815. 'procedure DoIt(s: string); begin end;',
  26816. 'procedure DoIt(v: jsvalue); begin end;',
  26817. 'var',
  26818. ' s: string;',
  26819. ' c: char;',
  26820. ' u: uni;',
  26821. 'begin',
  26822. ' DoIt(s);',
  26823. ' DoIt(c);',
  26824. ' DoIt(u);',
  26825. '']);
  26826. ConvertProgram;
  26827. CheckSource('TestJSValue_OverloadString',
  26828. LinesToStr([ // statements
  26829. 'this.DoIt = function (s) {',
  26830. '};',
  26831. 'this.DoIt$1 = function (v) {',
  26832. '};',
  26833. 'this.s = "";',
  26834. 'this.c = "";',
  26835. 'this.u = "";',
  26836. '']),
  26837. LinesToStr([ // $mod.$main
  26838. '$mod.DoIt($mod.s);',
  26839. '$mod.DoIt($mod.c);',
  26840. '$mod.DoIt($mod.u);',
  26841. '']));
  26842. end;
  26843. procedure TTestModule.TestJSValue_OverloadChar;
  26844. begin
  26845. StartProgram(false);
  26846. Add([
  26847. 'type',
  26848. ' uni = string;',
  26849. ' WChar = char;',
  26850. 'procedure DoIt(c: char); begin end;',
  26851. 'procedure DoIt(v: jsvalue); begin end;',
  26852. 'var',
  26853. ' s: string;',
  26854. ' c: char;',
  26855. ' u: uni;',
  26856. 'begin',
  26857. ' DoIt(s);',
  26858. ' DoIt(c);',
  26859. ' DoIt(u);',
  26860. '']);
  26861. ConvertProgram;
  26862. CheckSource('TestJSValue_OverloadChar',
  26863. LinesToStr([ // statements
  26864. 'this.DoIt = function (c) {',
  26865. '};',
  26866. 'this.DoIt$1 = function (v) {',
  26867. '};',
  26868. 'this.s = "";',
  26869. 'this.c = "";',
  26870. 'this.u = "";',
  26871. '']),
  26872. LinesToStr([ // $mod.$main
  26873. '$mod.DoIt$1($mod.s);',
  26874. '$mod.DoIt($mod.c);',
  26875. '$mod.DoIt$1($mod.u);',
  26876. '']));
  26877. end;
  26878. procedure TTestModule.TestJSValue_OverloadPointer;
  26879. begin
  26880. StartProgram(false);
  26881. Add([
  26882. 'type',
  26883. ' TObject = class end;',
  26884. 'procedure DoIt(p: pointer); begin end;',
  26885. 'procedure DoIt(v: jsvalue); begin end;',
  26886. 'var',
  26887. ' o: TObject;',
  26888. 'begin',
  26889. ' DoIt(o);',
  26890. '']);
  26891. ConvertProgram;
  26892. CheckSource('TestJSValue_OverloadPointer',
  26893. LinesToStr([ // statements
  26894. 'rtl.createClass($mod, "TObject", null, function () {',
  26895. ' this.$init = function () {',
  26896. ' };',
  26897. ' this.$final = function () {',
  26898. ' };',
  26899. '});',
  26900. 'this.DoIt = function (p) {',
  26901. '};',
  26902. 'this.DoIt$1 = function (v) {',
  26903. '};',
  26904. 'this.o = null;',
  26905. '']),
  26906. LinesToStr([ // $mod.$main
  26907. '$mod.DoIt($mod.o);',
  26908. '']));
  26909. end;
  26910. procedure TTestModule.TestJSValue_ForIn;
  26911. begin
  26912. StartProgram(false);
  26913. Add([
  26914. 'var',
  26915. ' v: JSValue;',
  26916. ' key: string;',
  26917. 'begin',
  26918. ' for key in v do begin',
  26919. ' if key=''abc'' then ;',
  26920. ' end;',
  26921. '']);
  26922. ConvertProgram;
  26923. CheckSource('TestJSValue_ForIn',
  26924. LinesToStr([ // statements
  26925. 'this.v = undefined;',
  26926. 'this.key = "";',
  26927. '']),
  26928. LinesToStr([ // $mod.$main
  26929. 'for ($mod.key in $mod.v) {',
  26930. ' if ($mod.key === "abc") ;',
  26931. '};',
  26932. '']));
  26933. end;
  26934. procedure TTestModule.TestRTTI_IntRange;
  26935. begin
  26936. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26937. StartProgram(true,[supTypeInfo]);
  26938. Add([
  26939. '{$modeswitch externalclass}',
  26940. 'type',
  26941. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  26942. ' TColor = type TGraphicsColor;',
  26943. 'var',
  26944. ' p: TTypeInfo;',
  26945. 'begin',
  26946. ' p:=typeinfo(TGraphicsColor);',
  26947. ' p:=typeinfo(TColor);',
  26948. '']);
  26949. ConvertProgram;
  26950. CheckSource('TestRTTI_IntRange',
  26951. LinesToStr([ // statements
  26952. '$mod.$rtti.$Int("TGraphicsColor", {',
  26953. ' minvalue: -2147483648,',
  26954. ' maxvalue: 2147483647,',
  26955. ' ordtype: 4',
  26956. '});',
  26957. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  26958. 'this.p = null;',
  26959. '']),
  26960. LinesToStr([ // $mod.$main
  26961. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  26962. '$mod.p = $mod.$rtti["TColor"];',
  26963. '']));
  26964. end;
  26965. procedure TTestModule.TestRTTI_Double;
  26966. begin
  26967. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26968. StartProgram(true,[supTypeInfo]);
  26969. Add([
  26970. '{$modeswitch externalclass}',
  26971. 'type',
  26972. ' TFloat = type double;',
  26973. 'var',
  26974. ' p: TTypeInfo;',
  26975. 'begin',
  26976. ' p:=typeinfo(double);',
  26977. ' p:=typeinfo(TFloat);',
  26978. '']);
  26979. ConvertProgram;
  26980. CheckSource('TestRTTI_Double',
  26981. LinesToStr([ // statements
  26982. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  26983. 'this.p = null;',
  26984. '']),
  26985. LinesToStr([ // $mod.$main
  26986. '$mod.p = rtl.double;',
  26987. '$mod.p = $mod.$rtti["TFloat"];',
  26988. '']));
  26989. end;
  26990. procedure TTestModule.TestRTTI_ProcType;
  26991. begin
  26992. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26993. StartProgram(false);
  26994. Add('type');
  26995. Add(' TProcA = procedure;');
  26996. Add(' TMethodB = procedure of object;');
  26997. Add(' TProcC = procedure; varargs;');
  26998. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  26999. Add(' TProcE = function: nativeint;');
  27000. Add(' TProcF = function(const p: TProcA): nativeuint;');
  27001. Add('var p: pointer;');
  27002. Add('begin');
  27003. Add(' p:=typeinfo(tproca);');
  27004. ConvertProgram;
  27005. CheckSource('TestRTTI_ProcType',
  27006. LinesToStr([ // statements
  27007. '$mod.$rtti.$ProcVar("TProcA", {',
  27008. ' procsig: rtl.newTIProcSig(null)',
  27009. '});',
  27010. '$mod.$rtti.$MethodVar("TMethodB", {',
  27011. ' procsig: rtl.newTIProcSig(null),',
  27012. ' methodkind: 0',
  27013. '});',
  27014. '$mod.$rtti.$ProcVar("TProcC", {',
  27015. ' procsig: rtl.newTIProcSig(null, 2)',
  27016. '});',
  27017. '$mod.$rtti.$ProcVar("TProcD", {',
  27018. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  27019. '});',
  27020. '$mod.$rtti.$ProcVar("TProcE", {',
  27021. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  27022. '});',
  27023. '$mod.$rtti.$ProcVar("TProcF", {',
  27024. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  27025. '});',
  27026. 'this.p = null;',
  27027. '']),
  27028. LinesToStr([ // $mod.$main
  27029. '$mod.p = $mod.$rtti["TProcA"];',
  27030. '']));
  27031. end;
  27032. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  27033. begin
  27034. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27035. AddModuleWithIntfImplSrc('unit2.pas',
  27036. LinesToStr([
  27037. 'type',
  27038. ' TObject = class end;'
  27039. ]),
  27040. '');
  27041. StartUnit(true);
  27042. Add('interface');
  27043. Add('uses unit2;');
  27044. Add('type');
  27045. Add(' TProcA = function(o: tobject): tobject;');
  27046. Add('implementation');
  27047. Add('type');
  27048. Add(' TProcB = function(o: tobject): tobject;');
  27049. Add('var p: Pointer;');
  27050. Add('initialization');
  27051. Add(' p:=typeinfo(tproca);');
  27052. Add(' p:=typeinfo(tprocb);');
  27053. ConvertUnit;
  27054. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  27055. LinesToStr([ // statements
  27056. 'var $impl = $mod.$impl;',
  27057. '$mod.$rtti.$ProcVar("TProcA", {',
  27058. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  27059. '});',
  27060. '']),
  27061. LinesToStr([ // this.$init
  27062. '$impl.p = $mod.$rtti["TProcA"];',
  27063. '$impl.p = $mod.$rtti["TProcB"];',
  27064. '']),
  27065. LinesToStr([ // implementation
  27066. '$mod.$rtti.$ProcVar("TProcB", {',
  27067. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  27068. '});',
  27069. '$impl.p = null;',
  27070. '']) );
  27071. end;
  27072. procedure TTestModule.TestRTTI_EnumAndSetType;
  27073. begin
  27074. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27075. StartProgram(false);
  27076. Add('type');
  27077. Add(' TFlag = (light,dark);');
  27078. Add(' TFlags = set of TFlag;');
  27079. Add(' TProc = function(f: TFlags): TFlag;');
  27080. Add('var p: pointer;');
  27081. Add('begin');
  27082. Add(' p:=typeinfo(tflag);');
  27083. Add(' p:=typeinfo(tflags);');
  27084. ConvertProgram;
  27085. CheckSource('TestRTTI_EnumAndType',
  27086. LinesToStr([ // statements
  27087. 'this.TFlag = {',
  27088. ' "0": "light",',
  27089. ' light: 0,',
  27090. ' "1": "dark",',
  27091. ' dark: 1',
  27092. '};',
  27093. '$mod.$rtti.$Enum("TFlag", {',
  27094. ' minvalue: 0,',
  27095. ' maxvalue: 1,',
  27096. ' ordtype: 1,',
  27097. ' enumtype: this.TFlag',
  27098. '});',
  27099. '$mod.$rtti.$Set("TFlags", {',
  27100. ' comptype: $mod.$rtti["TFlag"]',
  27101. '});',
  27102. '$mod.$rtti.$ProcVar("TProc", {',
  27103. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  27104. '});',
  27105. 'this.p = null;',
  27106. '']),
  27107. LinesToStr([ // $mod.$main
  27108. '$mod.p = $mod.$rtti["TFlag"];',
  27109. '$mod.p = $mod.$rtti["TFlags"];',
  27110. '']));
  27111. end;
  27112. procedure TTestModule.TestRTTI_EnumRange;
  27113. begin
  27114. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27115. StartProgram(false);
  27116. Add([
  27117. 'type',
  27118. ' TCol = (red,green,blue);',
  27119. ' TColRg = green..blue;',
  27120. ' TSetOfColRg = set of TColRg;',
  27121. 'var p: pointer;',
  27122. 'begin',
  27123. ' p:=typeinfo(tcolrg);',
  27124. ' p:=typeinfo(tsetofcolrg);',
  27125. '']);
  27126. ConvertProgram;
  27127. end;
  27128. procedure TTestModule.TestRTTI_AnonymousEnumType;
  27129. begin
  27130. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27131. StartProgram(false);
  27132. Add('type');
  27133. Add(' TFlags = set of (red, green);');
  27134. Add('var');
  27135. Add(' f: TFlags;');
  27136. Add('begin');
  27137. Add(' Include(f,red);');
  27138. ConvertProgram;
  27139. CheckSource('TestRTTI_AnonymousEnumType',
  27140. LinesToStr([ // statements
  27141. 'this.TFlags$a = {',
  27142. ' "0": "red",',
  27143. ' red: 0,',
  27144. ' "1": "green",',
  27145. ' green: 1',
  27146. '};',
  27147. '$mod.$rtti.$Enum("TFlags$a", {',
  27148. ' minvalue: 0,',
  27149. ' maxvalue: 1,',
  27150. ' ordtype: 1,',
  27151. ' enumtype: this.TFlags$a',
  27152. '});',
  27153. '$mod.$rtti.$Set("TFlags", {',
  27154. ' comptype: $mod.$rtti["TFlags$a"]',
  27155. '});',
  27156. 'this.f = {};',
  27157. '']),
  27158. LinesToStr([
  27159. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  27160. '']));
  27161. end;
  27162. procedure TTestModule.TestRTTI_StaticArray;
  27163. begin
  27164. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27165. StartProgram(false);
  27166. Add('type');
  27167. Add(' TFlag = (light,dark);');
  27168. Add(' TFlagNames = array[TFlag] of string;');
  27169. Add(' TBoolNames = array[boolean] of string;');
  27170. Add(' TByteArray = array[1..32768] of byte;');
  27171. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  27172. Add('var p: pointer;');
  27173. Add('begin');
  27174. Add(' p:=typeinfo(TFlagNames);');
  27175. Add(' p:=typeinfo(TBoolNames);');
  27176. ConvertProgram;
  27177. CheckSource('TestRTTI_StaticArray',
  27178. LinesToStr([ // statements
  27179. 'this.TFlag = {',
  27180. ' "0": "light",',
  27181. ' light: 0,',
  27182. ' "1": "dark",',
  27183. ' dark: 1',
  27184. '};',
  27185. '$mod.$rtti.$Enum("TFlag", {',
  27186. ' minvalue: 0,',
  27187. ' maxvalue: 1,',
  27188. ' ordtype: 1,',
  27189. ' enumtype: this.TFlag',
  27190. '});',
  27191. '$mod.$rtti.$StaticArray("TFlagNames", {',
  27192. ' dims: [2],',
  27193. ' eltype: rtl.string',
  27194. '});',
  27195. '$mod.$rtti.$StaticArray("TBoolNames", {',
  27196. ' dims: [2],',
  27197. ' eltype: rtl.string',
  27198. '});',
  27199. '$mod.$rtti.$StaticArray("TByteArray", {',
  27200. ' dims: [32768],',
  27201. ' eltype: rtl.byte',
  27202. '});',
  27203. '$mod.$rtti.$ProcVar("TProc", {',
  27204. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  27205. '});',
  27206. 'this.p = null;',
  27207. '']),
  27208. LinesToStr([ // $mod.$main
  27209. '$mod.p = $mod.$rtti["TFlagNames"];',
  27210. '$mod.p = $mod.$rtti["TBoolNames"];',
  27211. '']));
  27212. end;
  27213. procedure TTestModule.TestRTTI_DynArray;
  27214. begin
  27215. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27216. StartProgram(false);
  27217. Add('type');
  27218. Add(' TArrStr = array of string;');
  27219. Add(' TArr2Dim = array of tarrstr;');
  27220. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  27221. Add('var p: pointer;');
  27222. Add('begin');
  27223. Add(' p:=typeinfo(tarrstr);');
  27224. Add(' p:=typeinfo(tarr2dim);');
  27225. ConvertProgram;
  27226. CheckSource('TestRTTI_DynArray',
  27227. LinesToStr([ // statements
  27228. '$mod.$rtti.$DynArray("TArrStr", {',
  27229. ' eltype: rtl.string',
  27230. '});',
  27231. '$mod.$rtti.$DynArray("TArr2Dim", {',
  27232. ' eltype: $mod.$rtti["TArrStr"]',
  27233. '});',
  27234. '$mod.$rtti.$ProcVar("TProc", {',
  27235. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  27236. '});',
  27237. 'this.p = null;',
  27238. '']),
  27239. LinesToStr([ // $mod.$main
  27240. '$mod.p = $mod.$rtti["TArrStr"];',
  27241. '$mod.p = $mod.$rtti["TArr2Dim"];',
  27242. '']));
  27243. end;
  27244. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  27245. begin
  27246. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27247. StartProgram(false);
  27248. Add('type');
  27249. Add(' TArr = array of array of longint;');
  27250. Add('var a: TArr;');
  27251. Add('begin');
  27252. ConvertProgram;
  27253. CheckSource('TestRTTI_ArrayNestedAnonymous',
  27254. LinesToStr([ // statements
  27255. '$mod.$rtti.$DynArray("TArr$a", {',
  27256. ' eltype: rtl.longint',
  27257. '});',
  27258. '$mod.$rtti.$DynArray("TArr", {',
  27259. ' eltype: $mod.$rtti["TArr$a"]',
  27260. '});',
  27261. 'this.a = [];',
  27262. '']),
  27263. LinesToStr([ // $mod.$main
  27264. ]));
  27265. end;
  27266. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  27267. begin
  27268. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27269. StartProgram(false);
  27270. Add('type');
  27271. Add(' TObject = class');
  27272. Add(' published');
  27273. Add(' procedure Proc; virtual; abstract;');
  27274. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  27275. Add(' end;');
  27276. Add('begin');
  27277. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  27278. nDuplicatePublishedMethodXAtY);
  27279. ConvertProgram;
  27280. end;
  27281. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  27282. begin
  27283. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27284. StartProgram(false);
  27285. Add('type');
  27286. Add(' TObject = class');
  27287. Add(' published');
  27288. Add(' procedure Proc; external name ''foo'';');
  27289. Add(' end;');
  27290. Add('begin');
  27291. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  27292. nPublishedNameMustMatchExternal);
  27293. ConvertProgram;
  27294. end;
  27295. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  27296. begin
  27297. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27298. StartProgram(false);
  27299. Add('type');
  27300. Add(' TObject = class');
  27301. Add(' class var FA: longint;');
  27302. Add(' published');
  27303. Add(' class property A: longint read FA;');
  27304. Add(' end;');
  27305. Add('begin');
  27306. SetExpectedPasResolverError('Invalid published property modifier "class"',
  27307. nInvalidXModifierY);
  27308. ConvertProgram;
  27309. end;
  27310. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  27311. begin
  27312. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27313. StartProgram(false);
  27314. Add('type');
  27315. Add(' TObject = class');
  27316. Add(' published');
  27317. Add(' class var FA: longint;');
  27318. Add(' end;');
  27319. Add('begin');
  27320. SetExpectedPasResolverError(sSymbolCannotBePublished,
  27321. nSymbolCannotBePublished);
  27322. ConvertProgram;
  27323. end;
  27324. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  27325. begin
  27326. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27327. StartProgram(false);
  27328. Add('{$modeswitch externalclass}');
  27329. Add('type');
  27330. Add(' TObject = class');
  27331. Add(' published');
  27332. Add(' V: longint; external name ''foo'';');
  27333. Add(' end;');
  27334. Add('begin');
  27335. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  27336. nPublishedNameMustMatchExternal);
  27337. ConvertProgram;
  27338. end;
  27339. procedure TTestModule.TestRTTI_Class_Field;
  27340. begin
  27341. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27342. StartProgram(false);
  27343. Add('{$modeswitch externalclass}');
  27344. Add('type');
  27345. Add(' TObject = class');
  27346. Add(' private');
  27347. Add(' FPropA: string;');
  27348. Add(' published');
  27349. Add(' VarLI: longint;');
  27350. Add(' VarC: char;');
  27351. Add(' VarS: string;');
  27352. Add(' VarD: double;');
  27353. Add(' VarB: boolean;');
  27354. Add(' VarLW: longword;');
  27355. Add(' VarSmI: smallint;');
  27356. Add(' VarW: word;');
  27357. Add(' VarShI: shortint;');
  27358. Add(' VarBy: byte;');
  27359. Add(' VarExt: longint external name ''VarExt'';');
  27360. Add(' end;');
  27361. Add('var p: pointer;');
  27362. Add(' Obj: tobject;');
  27363. Add('begin');
  27364. Add(' p:=typeinfo(tobject);');
  27365. Add(' p:=typeinfo(p);');
  27366. Add(' p:=typeinfo(obj);');
  27367. ConvertProgram;
  27368. CheckSource('TestRTTI_Class_Field',
  27369. LinesToStr([ // statements
  27370. 'rtl.createClass($mod, "TObject", null, function () {',
  27371. ' this.$init = function () {',
  27372. ' this.FPropA = "";',
  27373. ' this.VarLI = 0;',
  27374. ' this.VarC = "";',
  27375. ' this.VarS = "";',
  27376. ' this.VarD = 0.0;',
  27377. ' this.VarB = false;',
  27378. ' this.VarLW = 0;',
  27379. ' this.VarSmI = 0;',
  27380. ' this.VarW = 0;',
  27381. ' this.VarShI = 0;',
  27382. ' this.VarBy = 0;',
  27383. ' };',
  27384. ' this.$final = function () {',
  27385. ' };',
  27386. ' var $r = this.$rtti;',
  27387. ' $r.addField("VarLI", rtl.longint);',
  27388. ' $r.addField("VarC", rtl.char);',
  27389. ' $r.addField("VarS", rtl.string);',
  27390. ' $r.addField("VarD", rtl.double);',
  27391. ' $r.addField("VarB", rtl.boolean);',
  27392. ' $r.addField("VarLW", rtl.longword);',
  27393. ' $r.addField("VarSmI", rtl.smallint);',
  27394. ' $r.addField("VarW", rtl.word);',
  27395. ' $r.addField("VarShI", rtl.shortint);',
  27396. ' $r.addField("VarBy", rtl.byte);',
  27397. ' $r.addField("VarExt", rtl.longint);',
  27398. '});',
  27399. 'this.p = null;',
  27400. 'this.Obj = null;',
  27401. '']),
  27402. LinesToStr([ // $mod.$main
  27403. '$mod.p = $mod.$rtti["TObject"];',
  27404. '$mod.p = rtl.pointer;',
  27405. '$mod.p = $mod.Obj.$rtti;',
  27406. '']));
  27407. end;
  27408. procedure TTestModule.TestRTTI_Class_Method;
  27409. begin
  27410. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27411. StartProgram(false);
  27412. Add('type');
  27413. Add(' TObject = class');
  27414. Add(' private');
  27415. Add(' procedure Internal; external name ''$intern'';');
  27416. Add(' published');
  27417. Add(' procedure Click; virtual; abstract;');
  27418. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  27419. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  27420. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  27421. Add(' end;');
  27422. Add('begin');
  27423. ConvertProgram;
  27424. CheckSource('TestRTTI_Class_Method',
  27425. LinesToStr([ // statements
  27426. 'rtl.createClass($mod, "TObject", null, function () {',
  27427. ' this.$init = function () {',
  27428. ' };',
  27429. ' this.$final = function () {',
  27430. ' };',
  27431. ' var $r = this.$rtti;',
  27432. ' $r.addMethod("Click", 0, null);',
  27433. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  27434. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  27435. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  27436. ' flags: 2',
  27437. ' });',
  27438. '});',
  27439. '']),
  27440. LinesToStr([ // $mod.$main
  27441. '']));
  27442. end;
  27443. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  27444. begin
  27445. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27446. StartProgram(false);
  27447. Add('type');
  27448. Add(' TObject = class');
  27449. Add(' published');
  27450. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  27451. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  27452. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  27453. Add(' end;');
  27454. Add('begin');
  27455. ConvertProgram;
  27456. CheckSource('TestRTTI_Class_MethodOpenArray',
  27457. LinesToStr([ // statements
  27458. 'rtl.createClass($mod, "TObject", null, function () {',
  27459. ' this.$init = function () {',
  27460. ' };',
  27461. ' this.$final = function () {',
  27462. ' };',
  27463. ' var $r = this.$rtti;',
  27464. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  27465. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  27466. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  27467. '});',
  27468. '']),
  27469. LinesToStr([ // $mod.$main
  27470. '']));
  27471. end;
  27472. procedure TTestModule.TestRTTI_Class_Property;
  27473. begin
  27474. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27475. StartProgram(false);
  27476. Add('{$modeswitch externalclass}');
  27477. Add('type');
  27478. Add(' TObject = class');
  27479. Add(' private');
  27480. Add(' FColor: longint;');
  27481. Add(' FColorStored: boolean;');
  27482. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  27483. Add(' function GetColor: longint; virtual; abstract;');
  27484. Add(' function GetColorStored: boolean; virtual; abstract;');
  27485. Add(' FExtSize: longint external name ''$extSize'';');
  27486. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  27487. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  27488. Add(' function GetExtSize: longint; external name ''$getSize'';');
  27489. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  27490. Add(' published');
  27491. Add(' property ColorA: longint read FColor;');
  27492. Add(' property ColorB: longint write FColor;');
  27493. Add(' property ColorC: longint read GetColor write SetColor;');
  27494. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  27495. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  27496. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  27497. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  27498. Add(' end;');
  27499. Add('begin');
  27500. ConvertProgram;
  27501. CheckSource('TestRTTI_Class_Property',
  27502. LinesToStr([ // statements
  27503. 'rtl.createClass($mod, "TObject", null, function () {',
  27504. ' this.$init = function () {',
  27505. ' this.FColor = 0;',
  27506. ' this.FColorStored = false;',
  27507. ' };',
  27508. ' this.$final = function () {',
  27509. ' };',
  27510. ' var $r = this.$rtti;',
  27511. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  27512. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  27513. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  27514. ' $r.addProperty(',
  27515. ' "ColorD",',
  27516. ' 8,',
  27517. ' rtl.longint,',
  27518. ' "FColor",',
  27519. ' "FColor",',
  27520. ' {',
  27521. ' stored: "FColorStored"',
  27522. ' }',
  27523. ' );',
  27524. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  27525. ' $r.addProperty(',
  27526. ' "ExtSizeB",',
  27527. ' 11,',
  27528. ' rtl.longint,',
  27529. ' "$getSize",',
  27530. ' "$setSize",',
  27531. ' {',
  27532. ' stored: "$extSizeStored"',
  27533. ' }',
  27534. ' );',
  27535. ' $r.addProperty(',
  27536. ' "ExtSizeC",',
  27537. ' 12,',
  27538. ' rtl.longint,',
  27539. ' "$extSize",',
  27540. ' "$extSize",',
  27541. ' {',
  27542. ' stored: "$getExtSizeStored"',
  27543. ' }',
  27544. ' );',
  27545. '});',
  27546. '']),
  27547. LinesToStr([ // $mod.$main
  27548. '']));
  27549. end;
  27550. procedure TTestModule.TestRTTI_Class_PropertyParams;
  27551. begin
  27552. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27553. StartProgram(false);
  27554. Add('{$modeswitch externalclass}');
  27555. Add('type');
  27556. Add(' integer = longint;');
  27557. Add(' TObject = class');
  27558. Add(' private');
  27559. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  27560. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  27561. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  27562. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  27563. Add(' published');
  27564. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  27565. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  27566. Add(' end;');
  27567. Add('begin');
  27568. ConvertProgram;
  27569. CheckSource('TestRTTI_Class_PropertyParams',
  27570. LinesToStr([ // statements
  27571. 'rtl.createClass($mod, "TObject", null, function () {',
  27572. ' this.$init = function () {',
  27573. ' };',
  27574. ' this.$final = function () {',
  27575. ' };',
  27576. ' var $r = this.$rtti;',
  27577. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  27578. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  27579. '});',
  27580. '']),
  27581. LinesToStr([ // $mod.$main
  27582. '']));
  27583. end;
  27584. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  27585. begin
  27586. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27587. AddModuleWithIntfImplSrc('unit1.pas',
  27588. 'type TColor = -5..5;',
  27589. '');
  27590. StartProgram(true);
  27591. Add([
  27592. 'uses unit1;',
  27593. 'type',
  27594. ' TColorAlias = TColor;',
  27595. ' TColorTypeAlias = type TColor;',
  27596. ' TObject = class',
  27597. ' private',
  27598. ' fColor: TColor;',
  27599. ' fAlias: TColorAlias;',
  27600. ' fTypeAlias: TColorTypeAlias;',
  27601. ' published',
  27602. ' property Color: TColor read fcolor;',
  27603. ' property Alias: TColorAlias read falias;',
  27604. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  27605. ' end;',
  27606. 'begin',
  27607. '']);
  27608. ConvertProgram;
  27609. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  27610. LinesToStr([ // statements
  27611. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  27612. 'rtl.createClass($mod, "TObject", null, function () {',
  27613. ' this.$init = function () {',
  27614. ' this.fColor = 0;',
  27615. ' this.fAlias = 0;',
  27616. ' this.fTypeAlias = 0;',
  27617. ' };',
  27618. ' this.$final = function () {',
  27619. ' };',
  27620. ' var $r = this.$rtti;',
  27621. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  27622. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  27623. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  27624. '});',
  27625. '']),
  27626. LinesToStr([ // $mod.$main
  27627. '']));
  27628. end;
  27629. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  27630. begin
  27631. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27632. StartProgram(false);
  27633. Add([
  27634. '{$modeswitch omitrtti}',
  27635. 'type',
  27636. ' TObject = class',
  27637. ' private',
  27638. ' FA: byte;',
  27639. ' published',
  27640. ' property A: byte read FA write FA;',
  27641. ' end;',
  27642. 'begin']);
  27643. ConvertProgram;
  27644. CheckSource('TestRTTI_Class_OmitRTTI',
  27645. LinesToStr([ // statements
  27646. 'rtl.createClass($mod, "TObject", null, function () {',
  27647. ' this.$init = function () {',
  27648. ' this.FA = 0;',
  27649. ' };',
  27650. ' this.$final = function () {',
  27651. ' };',
  27652. '});',
  27653. '']),
  27654. LinesToStr([ // $mod.$main
  27655. '']));
  27656. end;
  27657. procedure TTestModule.TestRTTI_IndexModifier;
  27658. begin
  27659. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27660. StartProgram(false);
  27661. Add([
  27662. 'type',
  27663. ' TEnum = (red, blue);',
  27664. ' TObject = class',
  27665. ' FB: boolean;',
  27666. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  27667. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  27668. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  27669. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  27670. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  27671. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  27672. ' published',
  27673. ' property B1: boolean index 1 read FB write SetIntBool;',
  27674. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  27675. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  27676. ' end;',
  27677. 'begin']);
  27678. ConvertProgram;
  27679. CheckSource('TestRTTI_IndexModifier',
  27680. LinesToStr([ // statements
  27681. 'this.TEnum = {',
  27682. ' "0": "red",',
  27683. ' red: 0,',
  27684. ' "1": "blue",',
  27685. ' blue: 1',
  27686. '};',
  27687. '$mod.$rtti.$Enum("TEnum", {',
  27688. ' minvalue: 0,',
  27689. ' maxvalue: 1,',
  27690. ' ordtype: 1,',
  27691. ' enumtype: this.TEnum',
  27692. '});',
  27693. 'rtl.createClass($mod, "TObject", null, function () {',
  27694. ' this.$init = function () {',
  27695. ' this.FB = false;',
  27696. ' };',
  27697. ' this.$final = function () {',
  27698. ' };',
  27699. ' var $r = this.$rtti;',
  27700. ' $r.addProperty(',
  27701. ' "B1",',
  27702. ' 18,',
  27703. ' rtl.boolean,',
  27704. ' "FB",',
  27705. ' "SetIntBool",',
  27706. ' {',
  27707. ' index: 1',
  27708. ' }',
  27709. ' );',
  27710. ' $r.addProperty(',
  27711. ' "B2",',
  27712. ' 17,',
  27713. ' rtl.boolean,',
  27714. ' "GetEnumBool",',
  27715. ' "FB",',
  27716. ' {',
  27717. ' index: $mod.TEnum.blue',
  27718. ' }',
  27719. ' );',
  27720. ' $r.addProperty(',
  27721. ' "I1",',
  27722. ' 19,',
  27723. ' rtl.boolean,',
  27724. ' "GetStrIntBool",',
  27725. ' "SetStrIntBool",',
  27726. ' {',
  27727. ' index: 2',
  27728. ' }',
  27729. ' );',
  27730. '});',
  27731. '']),
  27732. LinesToStr([ // $mod.$main
  27733. '']));
  27734. end;
  27735. procedure TTestModule.TestRTTI_StoredModifier;
  27736. begin
  27737. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27738. StartProgram(false);
  27739. Add([
  27740. 'const',
  27741. ' ConstB = true;',
  27742. 'type',
  27743. ' TObject = class',
  27744. ' private',
  27745. ' FB: boolean;',
  27746. ' function IsBStored: boolean; virtual; abstract;',
  27747. ' published',
  27748. ' property BoolA: boolean read FB stored true;',
  27749. ' property BoolB: boolean read FB stored false;',
  27750. ' property BoolC: boolean read FB stored FB;',
  27751. ' property BoolD: boolean read FB stored ConstB;',
  27752. ' property BoolE: boolean read FB stored IsBStored;',
  27753. ' end;',
  27754. 'begin']);
  27755. ConvertProgram;
  27756. CheckSource('TestRTTI_StoredModifier',
  27757. LinesToStr([ // statements
  27758. 'this.ConstB = true;',
  27759. 'rtl.createClass($mod, "TObject", null, function () {',
  27760. ' this.$init = function () {',
  27761. ' this.FB = false;',
  27762. ' };',
  27763. ' this.$final = function () {',
  27764. ' };',
  27765. ' var $r = this.$rtti;',
  27766. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  27767. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  27768. ' $r.addProperty(',
  27769. ' "BoolC",',
  27770. ' 8,',
  27771. ' rtl.boolean,',
  27772. ' "FB",',
  27773. ' "",',
  27774. ' {',
  27775. ' stored: "FB"',
  27776. ' }',
  27777. ' );',
  27778. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  27779. ' $r.addProperty(',
  27780. ' "BoolE",',
  27781. ' 12,',
  27782. ' rtl.boolean,',
  27783. ' "FB",',
  27784. ' "",',
  27785. ' {',
  27786. ' stored: "IsBStored"',
  27787. ' }',
  27788. ' );',
  27789. '});',
  27790. '']),
  27791. LinesToStr([ // $mod.$main
  27792. '']));
  27793. end;
  27794. procedure TTestModule.TestRTTI_DefaultValue;
  27795. begin
  27796. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27797. StartProgram(false);
  27798. Add([
  27799. 'type',
  27800. ' TEnum = (red, blue);',
  27801. 'const',
  27802. ' CB = true or false;',
  27803. ' CI = 1+2;',
  27804. 'type',
  27805. ' TObject = class',
  27806. ' FB: boolean;',
  27807. ' FI: longint;',
  27808. ' FE: TEnum;',
  27809. ' published',
  27810. ' property B1: boolean read FB default true;',
  27811. ' property B2: boolean read FB default CB;',
  27812. ' property B3: boolean read FB default test1.cb;',
  27813. ' property I1: longint read FI default 2;',
  27814. ' property I2: longint read FI default CI;',
  27815. ' property E1: TEnum read FE default red;',
  27816. ' property E2: TEnum read FE default TEnum.blue;',
  27817. ' end;',
  27818. 'begin']);
  27819. ConvertProgram;
  27820. CheckSource('TestRTTI_DefaultValue',
  27821. LinesToStr([ // statements
  27822. 'this.TEnum = {',
  27823. ' "0": "red",',
  27824. ' red: 0,',
  27825. ' "1": "blue",',
  27826. ' blue: 1',
  27827. '};',
  27828. '$mod.$rtti.$Enum("TEnum", {',
  27829. ' minvalue: 0,',
  27830. ' maxvalue: 1,',
  27831. ' ordtype: 1,',
  27832. ' enumtype: this.TEnum',
  27833. '});',
  27834. 'this.CB = true || false;',
  27835. 'this.CI = 1 + 2;',
  27836. 'rtl.createClass($mod, "TObject", null, function () {',
  27837. ' this.$init = function () {',
  27838. ' this.FB = false;',
  27839. ' this.FI = 0;',
  27840. ' this.FE = 0;',
  27841. ' };',
  27842. ' this.$final = function () {',
  27843. ' };',
  27844. ' var $r = this.$rtti;',
  27845. ' $r.addProperty(',
  27846. ' "B1",',
  27847. ' 0,',
  27848. ' rtl.boolean,',
  27849. ' "FB",',
  27850. ' "",',
  27851. ' {',
  27852. ' Default: true',
  27853. ' }',
  27854. ' );',
  27855. ' $r.addProperty(',
  27856. ' "B2",',
  27857. ' 0,',
  27858. ' rtl.boolean,',
  27859. ' "FB",',
  27860. ' "",',
  27861. ' {',
  27862. ' Default: true',
  27863. ' }',
  27864. ' );',
  27865. ' $r.addProperty(',
  27866. ' "B3",',
  27867. ' 0,',
  27868. ' rtl.boolean,',
  27869. ' "FB",',
  27870. ' "",',
  27871. ' {',
  27872. ' Default: true',
  27873. ' }',
  27874. ' );',
  27875. ' $r.addProperty(',
  27876. ' "I1",',
  27877. ' 0,',
  27878. ' rtl.longint,',
  27879. ' "FI",',
  27880. ' "",',
  27881. ' {',
  27882. ' Default: 2',
  27883. ' }',
  27884. ' );',
  27885. ' $r.addProperty(',
  27886. ' "I2",',
  27887. ' 0,',
  27888. ' rtl.longint,',
  27889. ' "FI",',
  27890. ' "",',
  27891. ' {',
  27892. ' Default: 3',
  27893. ' }',
  27894. ' );',
  27895. ' $r.addProperty(',
  27896. ' "E1",',
  27897. ' 0,',
  27898. ' $mod.$rtti["TEnum"],',
  27899. ' "FE",',
  27900. ' "",',
  27901. ' {',
  27902. ' Default: $mod.TEnum.red',
  27903. ' }',
  27904. ' );',
  27905. ' $r.addProperty(',
  27906. ' "E2",',
  27907. ' 0,',
  27908. ' $mod.$rtti["TEnum"],',
  27909. ' "FE",',
  27910. ' "",',
  27911. ' {',
  27912. ' Default: $mod.TEnum.blue',
  27913. ' }',
  27914. ' );',
  27915. '});',
  27916. '']),
  27917. LinesToStr([ // $mod.$main
  27918. '']));
  27919. end;
  27920. procedure TTestModule.TestRTTI_DefaultValueSet;
  27921. begin
  27922. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27923. StartProgram(false);
  27924. Add([
  27925. 'type',
  27926. ' TEnum = (red, blue);',
  27927. ' TSet = set of TEnum;',
  27928. 'const',
  27929. ' CSet = [red,blue];',
  27930. 'type',
  27931. ' TObject = class',
  27932. ' FSet: TSet;',
  27933. ' published',
  27934. ' property Set1: TSet read FSet default [];',
  27935. ' property Set2: TSet read FSet default [red];',
  27936. ' property Set3: TSet read FSet default [red,blue];',
  27937. ' property Set4: TSet read FSet default CSet;',
  27938. ' end;',
  27939. 'begin']);
  27940. ConvertProgram;
  27941. CheckSource('TestRTTI_DefaultValueSet',
  27942. LinesToStr([ // statements
  27943. 'this.TEnum = {',
  27944. ' "0": "red",',
  27945. ' red: 0,',
  27946. ' "1": "blue",',
  27947. ' blue: 1',
  27948. '};',
  27949. '$mod.$rtti.$Enum("TEnum", {',
  27950. ' minvalue: 0,',
  27951. ' maxvalue: 1,',
  27952. ' ordtype: 1,',
  27953. ' enumtype: this.TEnum',
  27954. '});',
  27955. '$mod.$rtti.$Set("TSet", {',
  27956. ' comptype: $mod.$rtti["TEnum"]',
  27957. '});',
  27958. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  27959. 'rtl.createClass($mod, "TObject", null, function () {',
  27960. ' this.$init = function () {',
  27961. ' this.FSet = {};',
  27962. ' };',
  27963. ' this.$final = function () {',
  27964. ' this.FSet = undefined;',
  27965. ' };',
  27966. ' var $r = this.$rtti;',
  27967. ' $r.addProperty(',
  27968. ' "Set1",',
  27969. ' 0,',
  27970. ' $mod.$rtti["TSet"],',
  27971. ' "FSet",',
  27972. ' "",',
  27973. ' {',
  27974. ' Default: {}',
  27975. ' }',
  27976. ' );',
  27977. ' $r.addProperty(',
  27978. ' "Set2",',
  27979. ' 0,',
  27980. ' $mod.$rtti["TSet"],',
  27981. ' "FSet",',
  27982. ' "",',
  27983. ' {',
  27984. ' Default: rtl.createSet($mod.TEnum.red)',
  27985. ' }',
  27986. ' );',
  27987. ' $r.addProperty(',
  27988. ' "Set3",',
  27989. ' 0,',
  27990. ' $mod.$rtti["TSet"],',
  27991. ' "FSet",',
  27992. ' "",',
  27993. ' {',
  27994. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  27995. ' }',
  27996. ' );',
  27997. ' $r.addProperty(',
  27998. ' "Set4",',
  27999. ' 0,',
  28000. ' $mod.$rtti["TSet"],',
  28001. ' "FSet",',
  28002. ' "",',
  28003. ' {',
  28004. ' Default: $mod.CSet',
  28005. ' }',
  28006. ' );',
  28007. '});',
  28008. '']),
  28009. LinesToStr([ // $mod.$main
  28010. '']));
  28011. end;
  28012. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  28013. begin
  28014. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28015. StartProgram(false);
  28016. Add([
  28017. 'type',
  28018. ' TRg = -1..1;',
  28019. 'const',
  28020. ' l = low(TRg);',
  28021. ' h = high(TRg);',
  28022. 'type',
  28023. ' TObject = class',
  28024. ' FV: TRg;',
  28025. ' published',
  28026. ' property V1: TRg read FV default -1;',
  28027. ' end;',
  28028. 'begin']);
  28029. ConvertProgram;
  28030. CheckSource('TestRTTI_DefaultValueRangeType',
  28031. LinesToStr([ // statements
  28032. '$mod.$rtti.$Int("TRg", {',
  28033. ' minvalue: -1,',
  28034. ' maxvalue: 1,',
  28035. ' ordtype: 0',
  28036. '});',
  28037. 'this.l = -1;',
  28038. 'this.h = 1;',
  28039. 'rtl.createClass($mod, "TObject", null, function () {',
  28040. ' this.$init = function () {',
  28041. ' this.FV = 0;',
  28042. ' };',
  28043. ' this.$final = function () {',
  28044. ' };',
  28045. ' var $r = this.$rtti;',
  28046. ' $r.addProperty(',
  28047. ' "V1",',
  28048. ' 0,',
  28049. ' $mod.$rtti["TRg"],',
  28050. ' "FV",',
  28051. ' "",',
  28052. ' {',
  28053. ' Default: -1',
  28054. ' }',
  28055. ' );',
  28056. '});',
  28057. '']),
  28058. LinesToStr([ // $mod.$main
  28059. '']));
  28060. end;
  28061. procedure TTestModule.TestRTTI_DefaultValueInherit;
  28062. begin
  28063. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28064. StartProgram(false);
  28065. Add([
  28066. 'type',
  28067. ' TObject = class',
  28068. ' FA, FB: byte;',
  28069. ' property A: byte read FA default 1;',
  28070. ' property B: byte read FB default 2;',
  28071. ' end;',
  28072. ' TBird = class',
  28073. ' published',
  28074. ' property A;',
  28075. ' property B nodefault;',
  28076. ' end;',
  28077. 'begin']);
  28078. ConvertProgram;
  28079. CheckSource('TestRTTI_DefaultValueInherit',
  28080. LinesToStr([ // statements
  28081. 'rtl.createClass($mod, "TObject", null, function () {',
  28082. ' this.$init = function () {',
  28083. ' this.FA = 0;',
  28084. ' this.FB = 0;',
  28085. ' };',
  28086. ' this.$final = function () {',
  28087. ' };',
  28088. '});',
  28089. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  28090. ' var $r = this.$rtti;',
  28091. ' $r.addProperty(',
  28092. ' "A",',
  28093. ' 0,',
  28094. ' rtl.byte,',
  28095. ' "FA",',
  28096. ' "",',
  28097. ' {',
  28098. ' Default: 1',
  28099. ' }',
  28100. ' );',
  28101. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  28102. '});',
  28103. '']),
  28104. LinesToStr([ // $mod.$main
  28105. '']));
  28106. end;
  28107. procedure TTestModule.TestRTTI_OverrideMethod;
  28108. begin
  28109. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28110. StartProgram(false);
  28111. Add('type');
  28112. Add(' TObject = class');
  28113. Add(' published');
  28114. Add(' procedure DoIt; virtual; abstract;');
  28115. Add(' end;');
  28116. Add(' TSky = class');
  28117. Add(' published');
  28118. Add(' procedure DoIt; override;');
  28119. Add(' end;');
  28120. Add('procedure TSky.DoIt; begin end;');
  28121. Add('begin');
  28122. ConvertProgram;
  28123. CheckSource('TestRTTI_OverrideMethod',
  28124. LinesToStr([ // statements
  28125. 'rtl.createClass($mod, "TObject", null, function () {',
  28126. ' this.$init = function () {',
  28127. ' };',
  28128. ' this.$final = function () {',
  28129. ' };',
  28130. ' var $r = this.$rtti;',
  28131. ' $r.addMethod("DoIt", 0, null);',
  28132. '});',
  28133. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  28134. ' this.DoIt = function () {',
  28135. ' };',
  28136. '});',
  28137. '']),
  28138. LinesToStr([ // $mod.$main
  28139. '']));
  28140. end;
  28141. procedure TTestModule.TestRTTI_ReintroduceMethod;
  28142. begin
  28143. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28144. StartProgram(false);
  28145. Add([
  28146. 'type',
  28147. ' TObject = class',
  28148. ' published',
  28149. ' procedure DoIt;',
  28150. ' end;',
  28151. ' TSky = class',
  28152. ' published',
  28153. ' procedure DoIt; reintroduce;',
  28154. ' end;',
  28155. 'procedure TObject.DoIt; begin end;',
  28156. 'procedure TSky.DoIt;',
  28157. 'begin',
  28158. ' inherited DoIt;',
  28159. 'end;',
  28160. 'begin']);
  28161. ConvertProgram;
  28162. CheckSource('TestRTTI_ReintroduceMethod',
  28163. LinesToStr([ // statements
  28164. 'rtl.createClass($mod, "TObject", null, function () {',
  28165. ' this.$init = function () {',
  28166. ' };',
  28167. ' this.$final = function () {',
  28168. ' };',
  28169. ' this.DoIt = function () {',
  28170. ' };',
  28171. ' var $r = this.$rtti;',
  28172. ' $r.addMethod("DoIt", 0, null);',
  28173. '});',
  28174. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  28175. ' this.DoIt = function () {',
  28176. ' $mod.TObject.DoIt.call(this);',
  28177. ' };',
  28178. ' var $r = this.$rtti;',
  28179. ' $r.addMethod("DoIt", 0, null);',
  28180. '});',
  28181. '']),
  28182. LinesToStr([ // $mod.$main
  28183. '']));
  28184. end;
  28185. procedure TTestModule.TestRTTI_OverloadProperty;
  28186. begin
  28187. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28188. StartProgram(false);
  28189. Add('type');
  28190. Add(' TObject = class');
  28191. Add(' protected');
  28192. Add(' FFlag: longint;');
  28193. Add(' published');
  28194. Add(' property Flag: longint read fflag;');
  28195. Add(' end;');
  28196. Add(' TSky = class');
  28197. Add(' published');
  28198. Add(' property FLAG: longint write fflag;');
  28199. Add(' end;');
  28200. Add('begin');
  28201. ConvertProgram;
  28202. CheckSource('TestRTTI_OverrideMethod',
  28203. LinesToStr([ // statements
  28204. 'rtl.createClass($mod, "TObject", null, function () {',
  28205. ' this.$init = function () {',
  28206. ' this.FFlag = 0;',
  28207. ' };',
  28208. ' this.$final = function () {',
  28209. ' };',
  28210. ' var $r = this.$rtti;',
  28211. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  28212. '});',
  28213. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  28214. ' var $r = this.$rtti;',
  28215. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  28216. '});',
  28217. '']),
  28218. LinesToStr([ // $mod.$main
  28219. '']));
  28220. end;
  28221. procedure TTestModule.TestRTTI_ClassForward;
  28222. begin
  28223. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28224. StartProgram(false);
  28225. Add('type');
  28226. Add(' TObject = class end;');
  28227. Add(' tbridge = class;');
  28228. Add(' TProc = function: tbridge;');
  28229. Add(' TOger = class');
  28230. Add(' published');
  28231. Add(' FBridge: tbridge;');
  28232. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  28233. Add(' property Bridge: tbridge read fbridge write setbridge;');
  28234. Add(' end;');
  28235. Add(' TBridge = class');
  28236. Add(' FOger: toger;');
  28237. Add(' end;');
  28238. Add('var p: Pointer;');
  28239. Add(' b: tbridge;');
  28240. Add('begin');
  28241. Add(' p:=typeinfo(tbridge);');
  28242. Add(' p:=typeinfo(b);');
  28243. ConvertProgram;
  28244. CheckSource('TestRTTI_ClassForward',
  28245. LinesToStr([ // statements
  28246. 'rtl.createClass($mod, "TObject", null, function () {',
  28247. ' this.$init = function () {',
  28248. ' };',
  28249. ' this.$final = function () {',
  28250. ' };',
  28251. '});',
  28252. '$mod.$rtti.$Class("TBridge");',
  28253. '$mod.$rtti.$ProcVar("TProc", {',
  28254. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  28255. '});',
  28256. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  28257. ' this.$init = function () {',
  28258. ' $mod.TObject.$init.call(this);',
  28259. ' this.FBridge = null;',
  28260. ' };',
  28261. ' this.$final = function () {',
  28262. ' this.FBridge = undefined;',
  28263. ' $mod.TObject.$final.call(this);',
  28264. ' };',
  28265. ' var $r = this.$rtti;',
  28266. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  28267. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  28268. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  28269. '});',
  28270. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  28271. ' this.$init = function () {',
  28272. ' $mod.TObject.$init.call(this);',
  28273. ' this.FOger = null;',
  28274. ' };',
  28275. ' this.$final = function () {',
  28276. ' this.FOger = undefined;',
  28277. ' $mod.TObject.$final.call(this);',
  28278. ' };',
  28279. '});',
  28280. 'this.p = null;',
  28281. 'this.b = null;',
  28282. '']),
  28283. LinesToStr([ // $mod.$main
  28284. '$mod.p = $mod.$rtti["TBridge"];',
  28285. '$mod.p = $mod.b.$rtti;',
  28286. '']));
  28287. end;
  28288. procedure TTestModule.TestRTTI_ClassOf;
  28289. begin
  28290. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28291. StartProgram(false);
  28292. Add('type');
  28293. Add(' TClass = class of tobject;');
  28294. Add(' TProcA = function: TClass;');
  28295. Add(' TObject = class');
  28296. Add(' published');
  28297. Add(' C: tclass;');
  28298. Add(' end;');
  28299. Add(' tfox = class;');
  28300. Add(' TBird = class end;');
  28301. Add(' TBirds = class of tbird;');
  28302. Add(' TFox = class end;');
  28303. Add(' TFoxes = class of tfox;');
  28304. Add(' TCows = class of TCow;');
  28305. Add(' TCow = class;');
  28306. Add(' TCow = class end;');
  28307. Add('begin');
  28308. ConvertProgram;
  28309. CheckSource('TestRTTI_ClassOf',
  28310. LinesToStr([ // statements
  28311. '$mod.$rtti.$Class("TObject");',
  28312. '$mod.$rtti.$ClassRef("TClass", {',
  28313. ' instancetype: $mod.$rtti["TObject"]',
  28314. '});',
  28315. '$mod.$rtti.$ProcVar("TProcA", {',
  28316. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  28317. '});',
  28318. 'rtl.createClass($mod, "TObject", null, function () {',
  28319. ' this.$init = function () {',
  28320. ' this.C = null;',
  28321. ' };',
  28322. ' this.$final = function () {',
  28323. ' this.C = undefined;',
  28324. ' };',
  28325. ' var $r = this.$rtti;',
  28326. ' $r.addField("C", $mod.$rtti["TClass"]);',
  28327. '});',
  28328. '$mod.$rtti.$Class("TFox");',
  28329. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  28330. '});',
  28331. '$mod.$rtti.$ClassRef("TBirds", {',
  28332. ' instancetype: $mod.$rtti["TBird"]',
  28333. '});',
  28334. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  28335. '});',
  28336. '$mod.$rtti.$ClassRef("TFoxes", {',
  28337. ' instancetype: $mod.$rtti["TFox"]',
  28338. '});',
  28339. '$mod.$rtti.$Class("TCow");',
  28340. '$mod.$rtti.$ClassRef("TCows", {',
  28341. ' instancetype: $mod.$rtti["TCow"]',
  28342. '});',
  28343. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  28344. '});',
  28345. '']),
  28346. LinesToStr([ // $mod.$main
  28347. '']));
  28348. end;
  28349. procedure TTestModule.TestRTTI_Record;
  28350. begin
  28351. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28352. StartProgram(false);
  28353. Add('type');
  28354. Add(' integer = longint;');
  28355. Add(' TPoint = record');
  28356. Add(' x,y: integer;');
  28357. Add(' end;');
  28358. Add('var p: pointer;');
  28359. Add(' r: tpoint;');
  28360. Add('begin');
  28361. Add(' p:=typeinfo(tpoint);');
  28362. Add(' p:=typeinfo(r);');
  28363. Add(' p:=typeinfo(r.x);');
  28364. ConvertProgram;
  28365. CheckSource('TestRTTI_Record',
  28366. LinesToStr([ // statements
  28367. 'rtl.recNewT($mod, "TPoint", function () {',
  28368. ' this.x = 0;',
  28369. ' this.y = 0;',
  28370. ' this.$eq = function (b) {',
  28371. ' return (this.x === b.x) && (this.y === b.y);',
  28372. ' };',
  28373. ' this.$assign = function (s) {',
  28374. ' this.x = s.x;',
  28375. ' this.y = s.y;',
  28376. ' return this;',
  28377. ' };',
  28378. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  28379. ' $r.addField("x", rtl.longint);',
  28380. ' $r.addField("y", rtl.longint);',
  28381. '});',
  28382. 'this.p = null;',
  28383. 'this.r = $mod.TPoint.$new();',
  28384. '']),
  28385. LinesToStr([ // $mod.$main
  28386. '$mod.p = $mod.$rtti["TPoint"];',
  28387. '$mod.p = $mod.$rtti["TPoint"];',
  28388. '$mod.p = rtl.longint;',
  28389. '']));
  28390. end;
  28391. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  28392. begin
  28393. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28394. StartProgram(false);
  28395. Add('type');
  28396. Add(' TFloatRec = record');
  28397. Add(' d: array of char;');
  28398. // Add(' i: array of array of longint;');
  28399. Add(' end;');
  28400. Add('var p: pointer;');
  28401. Add(' r: tfloatrec;');
  28402. Add('begin');
  28403. Add(' p:=typeinfo(tfloatrec);');
  28404. Add(' p:=typeinfo(r);');
  28405. Add(' p:=typeinfo(r.d);');
  28406. ConvertProgram;
  28407. CheckSource('TestRTTI_Record',
  28408. LinesToStr([ // statements
  28409. 'rtl.recNewT($mod, "TFloatRec", function () {',
  28410. ' this.d = [];',
  28411. ' this.$eq = function (b) {',
  28412. ' return this.d === b.d;',
  28413. ' };',
  28414. ' this.$assign = function (s) {',
  28415. ' this.d = s.d;',
  28416. ' return this;',
  28417. ' };',
  28418. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  28419. ' eltype: rtl.char',
  28420. ' });',
  28421. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  28422. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  28423. '});',
  28424. 'this.p = null;',
  28425. 'this.r = $mod.TFloatRec.$new();',
  28426. '']),
  28427. LinesToStr([ // $mod.$main
  28428. '$mod.p = $mod.$rtti["TFloatRec"];',
  28429. '$mod.p = $mod.$rtti["TFloatRec"];',
  28430. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  28431. '']));
  28432. end;
  28433. procedure TTestModule.TestRTTI_LocalTypes;
  28434. begin
  28435. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28436. StartProgram(false);
  28437. Add([
  28438. 'procedure DoIt;',
  28439. 'type',
  28440. ' integer = longint;',
  28441. ' TPoint = record',
  28442. ' x,y: integer;',
  28443. ' end;',
  28444. 'var p: TPoint;',
  28445. 'begin',
  28446. 'end;',
  28447. 'begin']);
  28448. ConvertProgram;
  28449. CheckSource('TestRTTI_LocalTypes',
  28450. LinesToStr([ // statements
  28451. 'var TPoint = rtl.recNewT(null, "", function () {',
  28452. ' this.x = 0;',
  28453. ' this.y = 0;',
  28454. ' this.$eq = function (b) {',
  28455. ' return (this.x === b.x) && (this.y === b.y);',
  28456. ' };',
  28457. ' this.$assign = function (s) {',
  28458. ' this.x = s.x;',
  28459. ' this.y = s.y;',
  28460. ' return this;',
  28461. ' };',
  28462. '});',
  28463. 'this.DoIt = function () {',
  28464. ' var p = TPoint.$new();',
  28465. '};',
  28466. '']),
  28467. LinesToStr([ // $mod.$main
  28468. '']));
  28469. end;
  28470. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  28471. begin
  28472. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28473. StartProgram(false);
  28474. Add([
  28475. 'type',
  28476. ' TCaption = string;',
  28477. ' TYesNo = boolean;',
  28478. ' TLetter = char;',
  28479. ' TFloat = double;',
  28480. ' TPtr = pointer;',
  28481. ' TShortInt = shortint;',
  28482. ' TByte = byte;',
  28483. ' TSmallInt = smallint;',
  28484. ' TWord = word;',
  28485. ' TInt32 = longint;',
  28486. ' TDWord = longword;',
  28487. ' TValue = jsvalue;',
  28488. 'var p: TPtr;',
  28489. 'begin',
  28490. ' p:=typeinfo(string);',
  28491. ' p:=typeinfo(tcaption);',
  28492. ' p:=typeinfo(boolean);',
  28493. ' p:=typeinfo(tyesno);',
  28494. ' p:=typeinfo(char);',
  28495. ' p:=typeinfo(tletter);',
  28496. ' p:=typeinfo(double);',
  28497. ' p:=typeinfo(tfloat);',
  28498. ' p:=typeinfo(pointer);',
  28499. ' p:=typeinfo(tptr);',
  28500. ' p:=typeinfo(shortint);',
  28501. ' p:=typeinfo(tshortint);',
  28502. ' p:=typeinfo(byte);',
  28503. ' p:=typeinfo(tbyte);',
  28504. ' p:=typeinfo(smallint);',
  28505. ' p:=typeinfo(tsmallint);',
  28506. ' p:=typeinfo(word);',
  28507. ' p:=typeinfo(tword);',
  28508. ' p:=typeinfo(longword);',
  28509. ' p:=typeinfo(tdword);',
  28510. ' p:=typeinfo(jsvalue);',
  28511. ' p:=typeinfo(tvalue);',
  28512. '']);
  28513. ConvertProgram;
  28514. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  28515. LinesToStr([ // statements
  28516. 'this.p = null;',
  28517. '']),
  28518. LinesToStr([ // $mod.$main
  28519. '$mod.p = rtl.string;',
  28520. '$mod.p = rtl.string;',
  28521. '$mod.p = rtl.boolean;',
  28522. '$mod.p = rtl.boolean;',
  28523. '$mod.p = rtl.char;',
  28524. '$mod.p = rtl.char;',
  28525. '$mod.p = rtl.double;',
  28526. '$mod.p = rtl.double;',
  28527. '$mod.p = rtl.pointer;',
  28528. '$mod.p = rtl.pointer;',
  28529. '$mod.p = rtl.shortint;',
  28530. '$mod.p = rtl.shortint;',
  28531. '$mod.p = rtl.byte;',
  28532. '$mod.p = rtl.byte;',
  28533. '$mod.p = rtl.smallint;',
  28534. '$mod.p = rtl.smallint;',
  28535. '$mod.p = rtl.word;',
  28536. '$mod.p = rtl.word;',
  28537. '$mod.p = rtl.longword;',
  28538. '$mod.p = rtl.longword;',
  28539. '$mod.p = rtl.jsvalue;',
  28540. '$mod.p = rtl.jsvalue;',
  28541. '']));
  28542. end;
  28543. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  28544. begin
  28545. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28546. StartProgram(false);
  28547. Add([
  28548. 'type',
  28549. ' TCaption = type string;',
  28550. ' TYesNo = type boolean;',
  28551. ' TLetter = type char;',
  28552. ' TFloat = type double;',
  28553. ' TPtr = type pointer;',
  28554. ' TShortInt = type shortint;',
  28555. ' TByte = type byte;',
  28556. ' TSmallInt = type smallint;',
  28557. ' TWord = type word;',
  28558. ' TInt32 = type longint;',
  28559. ' TDWord = type longword;',
  28560. ' TValue = type jsvalue;',
  28561. ' TAliasValue = type TValue;',
  28562. 'var',
  28563. ' p: TPtr;',
  28564. ' a: TAliasValue;',
  28565. 'begin',
  28566. ' p:=typeinfo(tcaption);',
  28567. ' p:=typeinfo(tyesno);',
  28568. ' p:=typeinfo(tletter);',
  28569. ' p:=typeinfo(tfloat);',
  28570. ' p:=typeinfo(tptr);',
  28571. ' p:=typeinfo(tshortint);',
  28572. ' p:=typeinfo(tbyte);',
  28573. ' p:=typeinfo(tsmallint);',
  28574. ' p:=typeinfo(tword);',
  28575. ' p:=typeinfo(tdword);',
  28576. ' p:=typeinfo(tvalue);',
  28577. ' p:=typeinfo(taliasvalue);',
  28578. ' p:=typeinfo(a);',
  28579. '']);
  28580. ConvertProgram;
  28581. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  28582. LinesToStr([ // statements
  28583. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  28584. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  28585. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  28586. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  28587. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  28588. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  28589. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  28590. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  28591. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  28592. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  28593. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  28594. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  28595. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  28596. 'this.p = null;',
  28597. 'this.a = undefined;',
  28598. '']),
  28599. LinesToStr([ // $mod.$main
  28600. '$mod.p = $mod.$rtti["TCaption"];',
  28601. '$mod.p = $mod.$rtti["TYesNo"];',
  28602. '$mod.p = $mod.$rtti["TLetter"];',
  28603. '$mod.p = $mod.$rtti["TFloat"];',
  28604. '$mod.p = $mod.$rtti["TPtr"];',
  28605. '$mod.p = $mod.$rtti["TShortInt"];',
  28606. '$mod.p = $mod.$rtti["TByte"];',
  28607. '$mod.p = $mod.$rtti["TSmallInt"];',
  28608. '$mod.p = $mod.$rtti["TWord"];',
  28609. '$mod.p = $mod.$rtti["TDWord"];',
  28610. '$mod.p = $mod.$rtti["TValue"];',
  28611. '$mod.p = $mod.$rtti["TAliasValue"];',
  28612. '$mod.p = $mod.$rtti["TAliasValue"];',
  28613. '']));
  28614. end;
  28615. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  28616. begin
  28617. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28618. StartProgram(false);
  28619. Add('procedure DoIt;');
  28620. Add('type');
  28621. Add(' integer = longint;');
  28622. Add(' TPoint = record');
  28623. Add(' x,y: integer;');
  28624. Add(' end;');
  28625. Add('var p: pointer;');
  28626. Add('begin');
  28627. Add(' p:=typeinfo(tpoint);');
  28628. Add('end;');
  28629. Add('begin');
  28630. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  28631. ConvertProgram;
  28632. end;
  28633. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  28634. begin
  28635. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28636. StartProgram(true,[supTypeInfo]);
  28637. Add([
  28638. '{$modeswitch externalclass}',
  28639. 'type',
  28640. ' TFlag = (up,down);',
  28641. ' TFlags = set of TFlag;',
  28642. 'var',
  28643. ' ti: TTypeInfo;',
  28644. ' tiInt: TTypeInfoInteger;',
  28645. ' tiEnum: TTypeInfoEnum;',
  28646. ' tiSet: TTypeInfoSet;',
  28647. 'begin',
  28648. ' ti:=typeinfo(string);',
  28649. ' ti:=typeinfo(boolean);',
  28650. ' ti:=typeinfo(char);',
  28651. ' ti:=typeinfo(double);',
  28652. ' tiInt:=typeinfo(shortint);',
  28653. ' tiInt:=typeinfo(byte);',
  28654. ' tiInt:=typeinfo(smallint);',
  28655. ' tiInt:=typeinfo(word);',
  28656. ' tiInt:=typeinfo(longint);',
  28657. ' tiInt:=typeinfo(longword);',
  28658. ' ti:=typeinfo(jsvalue);',
  28659. ' tiEnum:=typeinfo(tflag);',
  28660. ' tiSet:=typeinfo(tflags);']);
  28661. ConvertProgram;
  28662. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  28663. LinesToStr([ // statements
  28664. 'this.TFlag = {',
  28665. ' "0": "up",',
  28666. ' up: 0,',
  28667. ' "1": "down",',
  28668. ' down: 1',
  28669. '};',
  28670. '$mod.$rtti.$Enum("TFlag", {',
  28671. ' minvalue: 0,',
  28672. ' maxvalue: 1,',
  28673. ' ordtype: 1,',
  28674. ' enumtype: this.TFlag',
  28675. '});',
  28676. '$mod.$rtti.$Set("TFlags", {',
  28677. ' comptype: $mod.$rtti["TFlag"]',
  28678. '});',
  28679. 'this.ti = null;',
  28680. 'this.tiInt = null;',
  28681. 'this.tiEnum = null;',
  28682. 'this.tiSet = null;',
  28683. '']),
  28684. LinesToStr([ // $mod.$main
  28685. '$mod.ti = rtl.string;',
  28686. '$mod.ti = rtl.boolean;',
  28687. '$mod.ti = rtl.char;',
  28688. '$mod.ti = rtl.double;',
  28689. '$mod.tiInt = rtl.shortint;',
  28690. '$mod.tiInt = rtl.byte;',
  28691. '$mod.tiInt = rtl.smallint;',
  28692. '$mod.tiInt = rtl.word;',
  28693. '$mod.tiInt = rtl.longint;',
  28694. '$mod.tiInt = rtl.longword;',
  28695. '$mod.ti = rtl.jsvalue;',
  28696. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  28697. '$mod.tiSet = $mod.$rtti["TFlags"];',
  28698. '']));
  28699. end;
  28700. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  28701. begin
  28702. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28703. StartProgram(true,[supTypeInfo]);
  28704. Add('{$modeswitch externalclass}');
  28705. Add('type');
  28706. Add(' TStaticArr = array[boolean] of string;');
  28707. Add(' TDynArr = array of string;');
  28708. Add(' TProc = procedure;');
  28709. Add(' TMethod = procedure of object;');
  28710. Add('var');
  28711. Add(' StaticArray: TStaticArr;');
  28712. Add(' tiStaticArray: TTypeInfoStaticArray;');
  28713. Add(' DynArray: TDynArr;');
  28714. Add(' tiDynArray: TTypeInfoDynArray;');
  28715. Add(' ProcVar: TProc;');
  28716. Add(' tiProcVar: TTypeInfoProcVar;');
  28717. Add(' MethodVar: TMethod;');
  28718. Add(' tiMethodVar: TTypeInfoMethodVar;');
  28719. Add('begin');
  28720. Add(' tiStaticArray:=typeinfo(StaticArray);');
  28721. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  28722. Add(' tiDynArray:=typeinfo(DynArray);');
  28723. Add(' tiDynArray:=typeinfo(TDynArr);');
  28724. Add(' tiProcVar:=typeinfo(ProcVar);');
  28725. Add(' tiProcVar:=typeinfo(TProc);');
  28726. Add(' tiMethodVar:=typeinfo(MethodVar);');
  28727. Add(' tiMethodVar:=typeinfo(TMethod);');
  28728. ConvertProgram;
  28729. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  28730. LinesToStr([ // statements
  28731. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  28732. ' dims: [2],',
  28733. ' eltype: rtl.string',
  28734. '});',
  28735. '$mod.$rtti.$DynArray("TDynArr", {',
  28736. ' eltype: rtl.string',
  28737. '});',
  28738. '$mod.$rtti.$ProcVar("TProc", {',
  28739. ' procsig: rtl.newTIProcSig(null)',
  28740. '});',
  28741. '$mod.$rtti.$MethodVar("TMethod", {',
  28742. ' procsig: rtl.newTIProcSig(null),',
  28743. ' methodkind: 0',
  28744. '});',
  28745. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  28746. 'this.tiStaticArray = null;',
  28747. 'this.DynArray = [];',
  28748. 'this.tiDynArray = null;',
  28749. 'this.ProcVar = null;',
  28750. 'this.tiProcVar = null;',
  28751. 'this.MethodVar = null;',
  28752. 'this.tiMethodVar = null;',
  28753. '']),
  28754. LinesToStr([ // $mod.$main
  28755. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  28756. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  28757. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  28758. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  28759. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  28760. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  28761. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  28762. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  28763. '']));
  28764. end;
  28765. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  28766. begin
  28767. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28768. StartProgram(true,[supTypeInfo]);
  28769. Add('{$modeswitch externalclass}');
  28770. Add('type');
  28771. Add(' TRec = record end;');
  28772. // ToDo: ^PRec
  28773. Add(' TObject = class end;');
  28774. Add(' TClass = class of tobject;');
  28775. Add('var');
  28776. Add(' Rec: trec;');
  28777. Add(' tiRecord: ttypeinforecord;');
  28778. Add(' Obj: tobject;');
  28779. Add(' tiClass: ttypeinfoclass;');
  28780. Add(' aClass: tclass;');
  28781. Add(' tiClassRef: ttypeinfoclassref;');
  28782. // ToDo: ^PRec
  28783. Add(' tiPointer: ttypeinfopointer;');
  28784. Add('begin');
  28785. Add(' tirecord:=typeinfo(trec);');
  28786. Add(' tirecord:=typeinfo(trec);');
  28787. Add(' ticlass:=typeinfo(obj);');
  28788. Add(' ticlass:=typeinfo(tobject);');
  28789. Add(' ticlass:=typeinfo(aclass);');
  28790. Add(' ticlassref:=typeinfo(tclass);');
  28791. ConvertProgram;
  28792. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  28793. LinesToStr([ // statements
  28794. 'rtl.recNewT($mod, "TRec", function () {',
  28795. ' this.$eq = function (b) {',
  28796. ' return true;',
  28797. ' };',
  28798. ' this.$assign = function (s) {',
  28799. ' return this;',
  28800. ' };',
  28801. ' $mod.$rtti.$Record("TRec", {});',
  28802. '});',
  28803. 'rtl.createClass($mod, "TObject", null, function () {',
  28804. ' this.$init = function () {',
  28805. ' };',
  28806. ' this.$final = function () {',
  28807. ' };',
  28808. '});',
  28809. '$mod.$rtti.$ClassRef("TClass", {',
  28810. ' instancetype: $mod.$rtti["TObject"]',
  28811. '});',
  28812. 'this.Rec = $mod.TRec.$new();',
  28813. 'this.tiRecord = null;',
  28814. 'this.Obj = null;',
  28815. 'this.tiClass = null;',
  28816. 'this.aClass = null;',
  28817. 'this.tiClassRef = null;',
  28818. 'this.tiPointer = null;',
  28819. '']),
  28820. LinesToStr([ // $mod.$main
  28821. '$mod.tiRecord = $mod.$rtti["TRec"];',
  28822. '$mod.tiRecord = $mod.$rtti["TRec"];',
  28823. '$mod.tiClass = $mod.Obj.$rtti;',
  28824. '$mod.tiClass = $mod.$rtti["TObject"];',
  28825. '$mod.tiClass = $mod.aClass.$rtti;',
  28826. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  28827. '']));
  28828. end;
  28829. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  28830. begin
  28831. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28832. StartProgram(true,[supTypeInfo]);
  28833. Add([
  28834. '{$modeswitch externalclass}',
  28835. 'type',
  28836. ' TClass = class of tobject;',
  28837. ' TObject = class',
  28838. ' function MyClass: TClass;',
  28839. ' class function ClassType: TClass;',
  28840. ' end;',
  28841. 'function TObject.MyClass: TClass;',
  28842. 'var t: TTypeInfoClass;',
  28843. 'begin',
  28844. ' t:=TypeInfo(Self);',
  28845. ' t:=TypeInfo(Result);',
  28846. ' t:=TypeInfo(TObject);',
  28847. 'end;',
  28848. 'class function TObject.ClassType: TClass;',
  28849. 'var t: TTypeInfoClass;',
  28850. 'begin',
  28851. ' t:=TypeInfo(Self);',
  28852. ' t:=TypeInfo(Result);',
  28853. 'end;',
  28854. 'var',
  28855. ' Obj: TObject;',
  28856. ' t: TTypeInfoClass;',
  28857. 'begin',
  28858. ' t:=TypeInfo(TObject.ClassType);',
  28859. ' t:=TypeInfo(Obj.ClassType);',
  28860. ' t:=TypeInfo(Obj.MyClass);',
  28861. '']);
  28862. ConvertProgram;
  28863. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  28864. LinesToStr([ // statements
  28865. '$mod.$rtti.$Class("TObject");',
  28866. '$mod.$rtti.$ClassRef("TClass", {',
  28867. ' instancetype: $mod.$rtti["TObject"]',
  28868. '});',
  28869. 'rtl.createClass($mod, "TObject", null, function () {',
  28870. ' this.$init = function () {',
  28871. ' };',
  28872. ' this.$final = function () {',
  28873. ' };',
  28874. ' this.MyClass = function () {',
  28875. ' var Result = null;',
  28876. ' var t = null;',
  28877. ' t = this.$rtti;',
  28878. ' t = Result.$rtti;',
  28879. ' t = $mod.$rtti["TObject"];',
  28880. ' return Result;',
  28881. ' };',
  28882. ' this.ClassType = function () {',
  28883. ' var Result = null;',
  28884. ' var t = null;',
  28885. ' t = this.$rtti;',
  28886. ' t = Result.$rtti;',
  28887. ' return Result;',
  28888. ' };',
  28889. '});',
  28890. 'this.Obj = null;',
  28891. 'this.t = null;',
  28892. '']),
  28893. LinesToStr([ // $mod.$main
  28894. '$mod.t = $mod.TObject.ClassType().$rtti;',
  28895. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  28896. '$mod.t = $mod.Obj.MyClass().$rtti;',
  28897. '']));
  28898. end;
  28899. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  28900. begin
  28901. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28902. AddModuleWithIntfImplSrc('typinfo.pas',
  28903. LinesToStr([
  28904. '{$modeswitch externalclass}',
  28905. 'type',
  28906. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28907. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  28908. '']),
  28909. '');
  28910. AddModuleWithIntfImplSrc('unit2.pas',
  28911. LinesToStr([
  28912. 'uses typinfo;',
  28913. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  28914. 'procedure DoPtr(p: PTypeInfo);',
  28915. 'procedure DoInfo(t: TTypeInfo);',
  28916. 'procedure DoInt(t: TTypeInfoInteger);',
  28917. '']),
  28918. LinesToStr([
  28919. 'procedure DoPtr(p: PTypeInfo);',
  28920. 'begin end;',
  28921. 'procedure DoInfo(t: TTypeInfo);',
  28922. 'begin end;',
  28923. 'procedure DoInt(t: TTypeInfoInteger);',
  28924. 'begin end;',
  28925. '']));
  28926. StartUnit(true);
  28927. Add([
  28928. 'interface',
  28929. 'uses unit2;', // does not use unit typinfo
  28930. 'implementation',
  28931. 'var',
  28932. ' i: byte;',
  28933. ' p: pointer;',
  28934. ' t: PTypeInfo;',
  28935. 'initialization',
  28936. ' p:=typeinfo(i);',
  28937. ' t:=typeinfo(i);',
  28938. ' if p=t then ;',
  28939. ' if p=typeinfo(i) then ;',
  28940. ' if typeinfo(i)=p then ;',
  28941. ' if t=typeinfo(i) then ;',
  28942. ' if typeinfo(i)=t then ;',
  28943. ' DoPtr(p);',
  28944. ' DoPtr(t);',
  28945. ' DoPtr(typeinfo(i));',
  28946. ' DoInfo(p);',
  28947. ' DoInfo(t);',
  28948. ' DoInfo(typeinfo(i));',
  28949. ' DoInt(typeinfo(i));',
  28950. '']);
  28951. ConvertUnit;
  28952. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  28953. LinesToStr([ // statements
  28954. 'var $impl = $mod.$impl;',
  28955. '']),
  28956. LinesToStr([ // this.$init
  28957. '$impl.p = rtl.byte;',
  28958. '$impl.t = rtl.byte;',
  28959. 'if ($impl.p === $impl.t) ;',
  28960. 'if ($impl.p === rtl.byte) ;',
  28961. 'if (rtl.byte === $impl.p) ;',
  28962. 'if ($impl.t === rtl.byte) ;',
  28963. 'if (rtl.byte === $impl.t) ;',
  28964. 'pas.unit2.DoPtr($impl.p);',
  28965. 'pas.unit2.DoPtr($impl.t);',
  28966. 'pas.unit2.DoPtr(rtl.byte);',
  28967. 'pas.unit2.DoInfo($impl.p);',
  28968. 'pas.unit2.DoInfo($impl.t);',
  28969. 'pas.unit2.DoInfo(rtl.byte);',
  28970. 'pas.unit2.DoInt(rtl.byte);',
  28971. '']),
  28972. LinesToStr([ // implementation
  28973. '$impl.i = 0;',
  28974. '$impl.p = null;',
  28975. '$impl.t = null;',
  28976. '']) );
  28977. end;
  28978. procedure TTestModule.TestRTTI_Interface_Corba;
  28979. begin
  28980. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28981. StartProgram(true,[supTypeInfo]);
  28982. Add([
  28983. '{$interfaces corba}',
  28984. '{$modeswitch externalclass}',
  28985. 'type',
  28986. ' IUnknown = interface',
  28987. ' end;',
  28988. ' IBird = interface',
  28989. ' function GetItem: longint;',
  28990. ' procedure SetItem(Value: longint);',
  28991. ' property Item: longint read GetItem write SetItem;',
  28992. ' end;',
  28993. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  28994. 'var',
  28995. ' i: IBird;',
  28996. ' t: TTypeInfoInterface;',
  28997. 'begin',
  28998. ' t:=TypeInfo(IBird);',
  28999. ' t:=TypeInfo(i);',
  29000. ' DoIt(t);',
  29001. ' DoIt(TypeInfo(IBird));',
  29002. '']);
  29003. ConvertProgram;
  29004. CheckSource('TestRTTI_Interface_Corba',
  29005. LinesToStr([ // statements
  29006. 'rtl.createInterface(',
  29007. ' $mod,',
  29008. ' "IUnknown",',
  29009. ' "{B92D5841-758A-322B-B800-000000000000}",',
  29010. ' [],',
  29011. ' null,',
  29012. ' function () {',
  29013. ' }',
  29014. ');',
  29015. 'rtl.createInterface(',
  29016. ' $mod,',
  29017. ' "IBird",',
  29018. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  29019. ' ["GetItem", "SetItem"],',
  29020. ' null,',
  29021. ' function () {',
  29022. ' var $r = this.$rtti;',
  29023. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  29024. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  29025. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  29026. ' }',
  29027. ');',
  29028. 'this.DoIt = function (t) {',
  29029. '}; ',
  29030. 'this.i = null;',
  29031. 'this.t = null;',
  29032. '']),
  29033. LinesToStr([ // $mod.$main
  29034. '$mod.t = $mod.$rtti["IBird"];',
  29035. '$mod.t = $mod.i.$rtti;',
  29036. '$mod.DoIt($mod.t);',
  29037. '$mod.DoIt($mod.$rtti["IBird"]);',
  29038. '']));
  29039. end;
  29040. procedure TTestModule.TestRTTI_Interface_COM;
  29041. begin
  29042. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29043. StartProgram(true,[supTypeInfo]);
  29044. Add([
  29045. '{$interfaces com}',
  29046. '{$modeswitch externalclass}',
  29047. 'type',
  29048. ' TGuid = record end;',
  29049. ' integer = longint;',
  29050. ' IUnknown = interface',
  29051. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  29052. ' function _AddRef: Integer;',
  29053. ' function _Release: Integer;',
  29054. ' end;',
  29055. ' IBird = interface',
  29056. ' function GetItem: longint;',
  29057. ' procedure SetItem(Value: longint);',
  29058. ' property Item: longint read GetItem write SetItem;',
  29059. ' end;',
  29060. 'var',
  29061. ' i: IBird;',
  29062. ' t: TTypeInfoInterface;',
  29063. 'begin',
  29064. ' t:=TypeInfo(IBird);',
  29065. ' t:=TypeInfo(i);',
  29066. '']);
  29067. ConvertProgram;
  29068. CheckSource('TestRTTI_Interface_COM',
  29069. LinesToStr([ // statements
  29070. 'rtl.recNewT($mod, "TGuid", function () {',
  29071. ' this.$eq = function (b) {',
  29072. ' return true;',
  29073. ' };',
  29074. ' this.$assign = function (s) {',
  29075. ' return this;',
  29076. ' };',
  29077. ' $mod.$rtti.$Record("TGuid", {});',
  29078. '});',
  29079. 'rtl.createInterface(',
  29080. ' $mod,',
  29081. ' "IUnknown",',
  29082. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  29083. ' ["QueryInterface", "_AddRef", "_Release"],',
  29084. ' null,',
  29085. ' function () {',
  29086. ' this.$kind = "com";',
  29087. ' var $r = this.$rtti;',
  29088. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  29089. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  29090. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  29091. ' }',
  29092. ');',
  29093. 'rtl.createInterface(',
  29094. ' $mod,',
  29095. ' "IBird",',
  29096. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  29097. ' ["GetItem", "SetItem"],',
  29098. ' $mod.IUnknown,',
  29099. ' function () {',
  29100. ' var $r = this.$rtti;',
  29101. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  29102. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  29103. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  29104. ' }',
  29105. ');',
  29106. 'this.i = null;',
  29107. 'this.t = null;',
  29108. '']),
  29109. LinesToStr([ // $mod.$main
  29110. '$mod.t = $mod.$rtti["IBird"];',
  29111. '$mod.t = $mod.i.$rtti;',
  29112. '']));
  29113. end;
  29114. procedure TTestModule.TestRTTI_ClassHelper;
  29115. begin
  29116. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29117. StartProgram(true,[supTypeInfo]);
  29118. Add([
  29119. '{$interfaces com}',
  29120. '{$modeswitch externalclass}',
  29121. 'type',
  29122. ' TObject = class',
  29123. ' end;',
  29124. ' THelper = class helper for TObject',
  29125. ' published',
  29126. ' function GetItem: longint;',
  29127. ' property Item: longint read GetItem;',
  29128. ' end;',
  29129. 'function THelper.GetItem: longint;',
  29130. 'begin',
  29131. 'end;',
  29132. 'var',
  29133. ' t: TTypeInfoHelper;',
  29134. 'begin',
  29135. ' t:=TypeInfo(THelper);',
  29136. '']);
  29137. ConvertProgram;
  29138. CheckSource('TestRTTI_ClassHelper',
  29139. LinesToStr([ // statements
  29140. 'rtl.createClass($mod, "TObject", null, function () {',
  29141. ' this.$init = function () {',
  29142. ' };',
  29143. ' this.$final = function () {',
  29144. ' };',
  29145. '});',
  29146. 'rtl.createHelper($mod, "THelper", null, function () {',
  29147. ' this.GetItem = function () {',
  29148. ' var Result = 0;',
  29149. ' return Result;',
  29150. ' };',
  29151. ' var $r = this.$rtti;',
  29152. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  29153. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  29154. '});',
  29155. 'this.t = null;',
  29156. '']),
  29157. LinesToStr([ // $mod.$main
  29158. '$mod.t = $mod.$rtti["THelper"];',
  29159. '']));
  29160. end;
  29161. procedure TTestModule.TestRTTI_ExternalClass;
  29162. begin
  29163. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29164. StartProgram(true,[supTypeInfo]);
  29165. Add([
  29166. '{$modeswitch externalclass}',
  29167. 'type',
  29168. ' TJSObject = class external name ''Object''',
  29169. ' end;',
  29170. ' TJSArray = class external name ''Array'' (TJSObject)',
  29171. ' end;',
  29172. 'var',
  29173. ' p: Pointer;',
  29174. ' tc: TTypeInfoExtClass;',
  29175. 'begin',
  29176. ' p:=typeinfo(TJSArray);']);
  29177. ConvertProgram;
  29178. CheckSource('TestRTTI_ExternalClass',
  29179. LinesToStr([ // statements
  29180. '$mod.$rtti.$ExtClass("TJSObject", {',
  29181. ' jsclass: "Object"',
  29182. '});',
  29183. '$mod.$rtti.$ExtClass("TJSArray", {',
  29184. ' ancestor: $mod.$rtti["TJSObject"],',
  29185. ' jsclass: "Array"',
  29186. '});',
  29187. 'this.p = null;',
  29188. 'this.tc = null;',
  29189. '']),
  29190. LinesToStr([ // $mod.$main
  29191. '$mod.p = $mod.$rtti["TJSArray"];',
  29192. '']));
  29193. end;
  29194. procedure TTestModule.TestResourcestringProgram;
  29195. begin
  29196. StartProgram(false);
  29197. Add([
  29198. 'const Bar = ''bar'';',
  29199. 'resourcestring',
  29200. ' Red = ''red'';',
  29201. ' Foobar = ''fOo''+bar;',
  29202. 'var s: string;',
  29203. ' c: char;',
  29204. 'begin',
  29205. ' s:=red;',
  29206. ' s:=test1.red;',
  29207. ' c:=red[1];',
  29208. ' c:=test1.red[2];',
  29209. ' if red=foobar then ;',
  29210. ' if red[3]=red[4] then ;']);
  29211. ConvertProgram;
  29212. CheckSource('TestResourcestringProgram',
  29213. LinesToStr([ // statements
  29214. 'this.Bar = "bar";',
  29215. 'this.s = "";',
  29216. 'this.c = "";',
  29217. '$mod.$resourcestrings = {',
  29218. ' Red: {',
  29219. ' org: "red"',
  29220. ' },',
  29221. ' Foobar: {',
  29222. ' org: "fOobar"',
  29223. ' }',
  29224. '};',
  29225. '']),
  29226. LinesToStr([ // $mod.$main
  29227. '$mod.s = rtl.getResStr(pas.program, "Red");',
  29228. '$mod.s = rtl.getResStr(pas.program, "Red");',
  29229. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
  29230. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
  29231. 'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
  29232. 'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
  29233. '']));
  29234. end;
  29235. procedure TTestModule.TestResourcestringUnit;
  29236. begin
  29237. StartUnit(false);
  29238. Add([
  29239. 'interface',
  29240. 'const Red = ''rEd'';',
  29241. 'resourcestring',
  29242. ' Blue = ''blue'';',
  29243. ' NotRed = ''not''+Red;',
  29244. 'var s: string;',
  29245. 'implementation',
  29246. 'resourcestring',
  29247. ' ImplGreen = ''green'';',
  29248. 'initialization',
  29249. ' s:=blue+ImplGreen;',
  29250. ' s:=test1.blue+test1.implgreen;',
  29251. ' s:=blue[1]+implgreen[2];']);
  29252. ConvertUnit;
  29253. CheckSource('TestResourcestringUnit',
  29254. LinesToStr([ // statements
  29255. 'this.Red = "rEd";',
  29256. 'this.s = "";',
  29257. '$mod.$resourcestrings = {',
  29258. ' Blue: {',
  29259. ' org: "blue"',
  29260. ' },',
  29261. ' NotRed: {',
  29262. ' org: "notrEd"',
  29263. ' },',
  29264. ' ImplGreen: {',
  29265. ' org: "green"',
  29266. ' }',
  29267. '};',
  29268. '']),
  29269. LinesToStr([ // $mod.$main
  29270. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  29271. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  29272. '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
  29273. '']));
  29274. end;
  29275. procedure TTestModule.TestResourcestringImplementation;
  29276. begin
  29277. StartUnit(false);
  29278. Add([
  29279. 'interface',
  29280. 'implementation',
  29281. 'resourcestring',
  29282. ' ImplRed = ''red'';']);
  29283. ConvertUnit;
  29284. CheckSource('TestResourcestringImplementation',
  29285. LinesToStr([ // intf statements
  29286. 'var $impl = $mod.$impl;']),
  29287. LinesToStr([ // $mod.$init
  29288. '']),
  29289. LinesToStr([ // impl statements
  29290. '$mod.$resourcestrings = {',
  29291. ' ImplRed: {',
  29292. ' org: "red"',
  29293. ' }',
  29294. '};',
  29295. '']));
  29296. end;
  29297. procedure TTestModule.TestAttributes_Members;
  29298. begin
  29299. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29300. StartProgram(false);
  29301. Add([
  29302. '{$modeswitch PrefixedAttributes}',
  29303. 'type',
  29304. ' TObject = class',
  29305. ' constructor Create;',
  29306. ' end;',
  29307. ' TCustomAttribute = class',
  29308. ' constructor Create(Id: word);',
  29309. ' end;',
  29310. ' [Missing]',
  29311. ' TBird = class',
  29312. ' published',
  29313. ' [Tcustom]',
  29314. ' FField: word;',
  29315. ' [tcustom(14)]',
  29316. ' property Size: word read FField;',
  29317. ' [Tcustom(15)]',
  29318. ' procedure Fly; virtual; abstract;',
  29319. ' end;',
  29320. ' TRec = record',
  29321. ' [Tcustom,tcustom(14)]',
  29322. ' Size: word;',
  29323. ' end;',
  29324. 'constructor TObject.Create; begin end;',
  29325. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  29326. 'begin',
  29327. '']);
  29328. ConvertProgram;
  29329. CheckSource('TestAttributes_Members',
  29330. LinesToStr([ // statements
  29331. 'rtl.createClass($mod, "TObject", null, function () {',
  29332. ' this.$init = function () {',
  29333. ' };',
  29334. ' this.$final = function () {',
  29335. ' };',
  29336. ' this.Create = function () {',
  29337. ' return this;',
  29338. ' };',
  29339. '});',
  29340. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  29341. ' this.Create$1 = function (Id) {',
  29342. ' return this;',
  29343. ' };',
  29344. '});',
  29345. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  29346. ' this.$init = function () {',
  29347. ' $mod.TObject.$init.call(this);',
  29348. ' this.FField = 0;',
  29349. ' };',
  29350. ' var $r = this.$rtti;',
  29351. ' $r.addField("FField", rtl.word, {',
  29352. ' attr: [$mod.TCustomAttribute, "Create"]',
  29353. ' });',
  29354. ' $r.addProperty(',
  29355. ' "Size",',
  29356. ' 0,',
  29357. ' rtl.word,',
  29358. ' "FField",',
  29359. ' "",',
  29360. ' {',
  29361. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  29362. ' }',
  29363. ' );',
  29364. ' $r.addMethod("Fly", 0, null, null, {',
  29365. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  29366. ' });',
  29367. '});',
  29368. 'rtl.recNewT($mod, "TRec", function () {',
  29369. ' this.Size = 0;',
  29370. ' this.$eq = function (b) {',
  29371. ' return this.Size === b.Size;',
  29372. ' };',
  29373. ' this.$assign = function (s) {',
  29374. ' this.Size = s.Size;',
  29375. ' return this;',
  29376. ' };',
  29377. ' var $r = $mod.$rtti.$Record("TRec", {});',
  29378. ' $r.addField("Size", rtl.word, {',
  29379. ' attr: [',
  29380. ' $mod.TCustomAttribute,',
  29381. ' "Create",',
  29382. ' $mod.TCustomAttribute,',
  29383. ' "Create$1",',
  29384. ' [14]',
  29385. ' ]',
  29386. ' });',
  29387. '});',
  29388. '']),
  29389. LinesToStr([ // $mod.$main
  29390. '']));
  29391. end;
  29392. procedure TTestModule.TestAttributes_Types;
  29393. begin
  29394. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29395. StartProgram(false);
  29396. Add([
  29397. '{$modeswitch PrefixedAttributes}',
  29398. 'type',
  29399. ' TObject = class',
  29400. ' constructor Create(Id: word);',
  29401. ' end;',
  29402. ' TCustomAttribute = class',
  29403. ' end;',
  29404. ' [TCustom(1)]',
  29405. ' TMyClass = class',
  29406. ' end;',
  29407. ' [TCustom(2)]',
  29408. ' TRec = record',
  29409. ' end;',
  29410. ' [TCustom(3)]',
  29411. ' TInt = type word;',
  29412. 'constructor TObject.Create(Id: word);',
  29413. 'begin',
  29414. 'end;',
  29415. 'var p: pointer;',
  29416. 'begin',
  29417. ' p:=typeinfo(TMyClass);',
  29418. ' p:=typeinfo(TRec);',
  29419. ' p:=typeinfo(TInt);',
  29420. '']);
  29421. ConvertProgram;
  29422. CheckSource('TestAttributes_Types',
  29423. LinesToStr([ // statements
  29424. 'rtl.createClass($mod, "TObject", null, function () {',
  29425. ' this.$init = function () {',
  29426. ' };',
  29427. ' this.$final = function () {',
  29428. ' };',
  29429. ' this.Create = function (Id) {',
  29430. ' return this;',
  29431. ' };',
  29432. '});',
  29433. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  29434. '});',
  29435. 'rtl.createClass($mod, "TMyClass", $mod.TObject, function () {',
  29436. ' var $r = this.$rtti;',
  29437. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  29438. '});',
  29439. 'rtl.recNewT($mod, "TRec", function () {',
  29440. ' this.$eq = function (b) {',
  29441. ' return true;',
  29442. ' };',
  29443. ' this.$assign = function (s) {',
  29444. ' return this;',
  29445. ' };',
  29446. ' $mod.$rtti.$Record("TRec", {',
  29447. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  29448. ' });',
  29449. '});',
  29450. '$mod.$rtti.$inherited("TInt", rtl.word, {',
  29451. ' attr: [$mod.TCustomAttribute, "Create", [3]]',
  29452. '});',
  29453. 'this.p = null;',
  29454. '']),
  29455. LinesToStr([ // $mod.$main
  29456. '$mod.p = $mod.$rtti["TMyClass"];',
  29457. '$mod.p = $mod.$rtti["TRec"];',
  29458. '$mod.p = $mod.$rtti["TInt"];',
  29459. '']));
  29460. end;
  29461. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  29462. begin
  29463. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29464. StartProgram(false);
  29465. Add([
  29466. '{$modeswitch PrefixedAttributes}',
  29467. 'type',
  29468. ' TObject = class',
  29469. ' constructor Create;',
  29470. ' end;',
  29471. ' TCustomAttribute = class',
  29472. ' end;',
  29473. ' THelper = class helper for TCustomAttribute',
  29474. ' constructor Create(Id: word);',
  29475. ' end;',
  29476. ' [TCustom(3)]',
  29477. ' TMyInt = word;',
  29478. 'constructor TObject.Create; begin end;',
  29479. 'constructor THelper.Create(Id: word); begin end;',
  29480. 'begin',
  29481. ' if typeinfo(TMyInt)=nil then ;']);
  29482. ConvertProgram;
  29483. end;
  29484. procedure TTestModule.TestAssert;
  29485. begin
  29486. StartProgram(false);
  29487. Add([
  29488. 'procedure DoIt;',
  29489. 'var',
  29490. ' b: boolean;',
  29491. ' s: string;',
  29492. 'begin',
  29493. ' {$Assertions on}',
  29494. ' Assert(b);',
  29495. 'end;',
  29496. 'begin',
  29497. ' DoIt;',
  29498. '']);
  29499. ConvertProgram;
  29500. CheckSource('TestAssert',
  29501. LinesToStr([ // statements
  29502. 'this.DoIt = function () {',
  29503. ' var b = false;',
  29504. ' var s = "";',
  29505. ' if (!b) throw "assert failed";',
  29506. '};',
  29507. '']),
  29508. LinesToStr([ // $mod.$main
  29509. '$mod.DoIt();',
  29510. '']));
  29511. end;
  29512. procedure TTestModule.TestAssert_SysUtils;
  29513. begin
  29514. AddModuleWithIntfImplSrc('SysUtils.pas',
  29515. LinesToStr([
  29516. 'type',
  29517. ' TObject = class',
  29518. ' constructor Create;',
  29519. ' end;',
  29520. ' EAssertionFailed = class',
  29521. ' constructor Create(s: string);',
  29522. ' end;',
  29523. '']),
  29524. LinesToStr([
  29525. 'constructor TObject.Create;',
  29526. 'begin end;',
  29527. 'constructor EAssertionFailed.Create(s: string);',
  29528. 'begin end;',
  29529. '']) );
  29530. StartProgram(true);
  29531. Add([
  29532. 'uses sysutils;',
  29533. 'procedure DoIt;',
  29534. 'var',
  29535. ' b: boolean;',
  29536. ' s: string;',
  29537. 'begin',
  29538. ' {$Assertions on}',
  29539. ' Assert(b);',
  29540. ' Assert(b,''msg'');',
  29541. 'end;',
  29542. 'begin',
  29543. ' DoIt;',
  29544. '']);
  29545. ConvertProgram;
  29546. CheckSource('TestAssert_SysUtils',
  29547. LinesToStr([ // statements
  29548. 'this.DoIt = function () {',
  29549. ' var b = false;',
  29550. ' var s = "";',
  29551. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  29552. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  29553. '};',
  29554. '']),
  29555. LinesToStr([ // $mod.$main
  29556. '$mod.DoIt();',
  29557. '']));
  29558. end;
  29559. procedure TTestModule.TestObjectChecks;
  29560. begin
  29561. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  29562. StartProgram(false);
  29563. Add([
  29564. 'type',
  29565. ' TObject = class',
  29566. ' procedure DoIt;',
  29567. ' end;',
  29568. ' TClass = class of tobject;',
  29569. ' TBird = class',
  29570. ' end;',
  29571. ' TBirdClass = class of TBird;',
  29572. 'var',
  29573. ' o : TObject;',
  29574. ' c: TClass;',
  29575. ' b: TBird;',
  29576. ' bc: TBirdClass;',
  29577. 'procedure TObject.DoIt;',
  29578. 'begin',
  29579. ' b:=TBird(o);',
  29580. 'end;',
  29581. 'begin',
  29582. ' o.DoIt;',
  29583. ' b:=TBird(o);',
  29584. ' bc:=TBirdClass(c);',
  29585. '']);
  29586. ConvertProgram;
  29587. CheckSource('TestCheckMethodCall',
  29588. LinesToStr([ // statements
  29589. 'rtl.createClass($mod, "TObject", null, function () {',
  29590. ' this.$init = function () {',
  29591. ' };',
  29592. ' this.$final = function () {',
  29593. ' };',
  29594. ' this.DoIt = function () {',
  29595. ' rtl.checkMethodCall(this,$mod.TObject);',
  29596. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  29597. ' };',
  29598. '});',
  29599. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  29600. '});',
  29601. 'this.o = null;',
  29602. 'this.c = null;',
  29603. 'this.b = null;',
  29604. 'this.bc = null;',
  29605. '']),
  29606. LinesToStr([ // $mod.$main
  29607. '$mod.o.DoIt();',
  29608. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  29609. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  29610. '']));
  29611. end;
  29612. procedure TTestModule.TestOverflowChecks_Int;
  29613. begin
  29614. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  29615. StartProgram(false);
  29616. Add([
  29617. 'procedure DoIt;',
  29618. 'var',
  29619. ' b: byte;',
  29620. ' n: nativeint;',
  29621. ' u: nativeuint;',
  29622. ' c: currency;',
  29623. 'begin',
  29624. ' n:=n+n;',
  29625. ' n:=n-n;',
  29626. ' n:=n+b;',
  29627. ' n:=b-n;',
  29628. ' n:=n*n;',
  29629. ' n:=n*u;',
  29630. ' c:=c+b;',
  29631. ' c:=b+c;',
  29632. ' c:=c*b;',
  29633. ' c:=b*c;',
  29634. 'end;',
  29635. 'begin',
  29636. '']);
  29637. ConvertProgram;
  29638. CheckSource('TestOverflowChecks_Int',
  29639. LinesToStr([ // statements
  29640. 'this.DoIt = function () {',
  29641. ' var b = 0;',
  29642. ' var n = 0;',
  29643. ' var u = 0;',
  29644. ' var c = 0;',
  29645. ' n = rtl.oc(n + n);',
  29646. ' n = rtl.oc(n - n);',
  29647. ' n = rtl.oc(n + b);',
  29648. ' n = rtl.oc(b - n);',
  29649. ' n = rtl.oc(n * n);',
  29650. ' n = rtl.oc(n * u);',
  29651. ' c = rtl.oc(c + (b * 10000));',
  29652. ' c = rtl.oc((b * 10000) + c);',
  29653. ' c = rtl.oc(c * b);',
  29654. ' c = rtl.oc(b * c);',
  29655. '};',
  29656. '']),
  29657. LinesToStr([ // $mod.$main
  29658. '']));
  29659. end;
  29660. procedure TTestModule.TestRangeChecks_AssignInt;
  29661. begin
  29662. Scanner.Options:=Scanner.Options+[po_CAssignments];
  29663. StartProgram(false);
  29664. Add([
  29665. '{$R+}',
  29666. 'var',
  29667. ' b: byte = 2;',
  29668. ' w: word = 3;',
  29669. 'procedure DoIt(p: byte);',
  29670. 'begin',
  29671. ' b:=w;',
  29672. ' b+=w;',
  29673. ' b:=1;',
  29674. 'end;',
  29675. '{$R-}',
  29676. 'procedure DoSome;',
  29677. 'begin',
  29678. ' DoIt(w);',
  29679. ' b:=w;',
  29680. ' b:=2;',
  29681. 'end;',
  29682. 'begin',
  29683. '{$R+}',
  29684. '']);
  29685. ConvertProgram;
  29686. CheckSource('TestRangeChecks_AssignInt',
  29687. LinesToStr([ // statements
  29688. 'this.b = 2;',
  29689. 'this.w = 3;',
  29690. 'this.DoIt = function (p) {',
  29691. ' rtl.rc(p, 0, 255);',
  29692. ' $mod.b = rtl.rc($mod.w,0,255);',
  29693. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  29694. ' $mod.b = 1;',
  29695. '};',
  29696. 'this.DoSome = function () {',
  29697. ' $mod.DoIt($mod.w);',
  29698. ' $mod.b = $mod.w;',
  29699. ' $mod.b = 2;',
  29700. '};',
  29701. '']),
  29702. LinesToStr([ // $mod.$main
  29703. '']));
  29704. end;
  29705. procedure TTestModule.TestRangeChecks_AssignIntRange;
  29706. begin
  29707. Scanner.Options:=Scanner.Options+[po_CAssignments];
  29708. StartProgram(false);
  29709. Add([
  29710. '{$R+}',
  29711. 'type Ten = 1..10;',
  29712. 'var',
  29713. ' b: Ten = 2;',
  29714. ' w: Ten = 3;',
  29715. 'procedure DoIt(p: Ten);',
  29716. 'begin',
  29717. ' b:=w;',
  29718. ' b+=w;',
  29719. ' b:=1;',
  29720. 'end;',
  29721. '{$R-}',
  29722. 'procedure DoSome;',
  29723. 'begin',
  29724. ' DoIt(w);',
  29725. ' b:=w;',
  29726. ' b:=2;',
  29727. 'end;',
  29728. 'begin',
  29729. '{$R+}',
  29730. '']);
  29731. ConvertProgram;
  29732. CheckSource('TestRangeChecks_AssignIntRange',
  29733. LinesToStr([ // statements
  29734. 'this.b = 2;',
  29735. 'this.w = 3;',
  29736. 'this.DoIt = function (p) {',
  29737. ' rtl.rc(p, 1, 10);',
  29738. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  29739. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  29740. ' $mod.b = 1;',
  29741. '};',
  29742. 'this.DoSome = function () {',
  29743. ' $mod.DoIt($mod.w);',
  29744. ' $mod.b = $mod.w;',
  29745. ' $mod.b = 2;',
  29746. '};',
  29747. '']),
  29748. LinesToStr([ // $mod.$main
  29749. '']));
  29750. end;
  29751. procedure TTestModule.TestRangeChecks_AssignEnum;
  29752. begin
  29753. StartProgram(false);
  29754. Add([
  29755. '{$R+}',
  29756. 'type TEnum = (red,green);',
  29757. 'var',
  29758. ' e: TEnum = red;',
  29759. 'procedure DoIt(p: TEnum);',
  29760. 'begin',
  29761. ' e:=p;',
  29762. ' p:=TEnum(0);',
  29763. ' p:=succ(e);',
  29764. 'end;',
  29765. '{$R-}',
  29766. 'procedure DoSome;',
  29767. 'begin',
  29768. ' DoIt(e);',
  29769. ' e:=TEnum(1);',
  29770. ' e:=pred(e);',
  29771. 'end;',
  29772. 'begin',
  29773. '{$R+}',
  29774. '']);
  29775. ConvertProgram;
  29776. CheckSource('TestRangeChecks_AssignEnum',
  29777. LinesToStr([ // statements
  29778. 'this.TEnum = {',
  29779. ' "0": "red",',
  29780. ' red: 0,',
  29781. ' "1": "green",',
  29782. ' green: 1',
  29783. '};',
  29784. 'this.e = $mod.TEnum.red;',
  29785. 'this.DoIt = function (p) {',
  29786. ' rtl.rc(p, 0, 1);',
  29787. ' $mod.e = rtl.rc(p, 0, 1);',
  29788. ' p = 0;',
  29789. ' p = rtl.rc($mod.e + 1, 0, 1);',
  29790. '};',
  29791. 'this.DoSome = function () {',
  29792. ' $mod.DoIt($mod.e);',
  29793. ' $mod.e = 1;',
  29794. ' $mod.e = $mod.e - 1;',
  29795. '};',
  29796. '']),
  29797. LinesToStr([ // $mod.$main
  29798. '']));
  29799. end;
  29800. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  29801. begin
  29802. StartProgram(false);
  29803. Add([
  29804. '{$R+}',
  29805. 'type',
  29806. ' TEnum = (red,green);',
  29807. ' TEnumRg = red..green;',
  29808. 'var',
  29809. ' e: TEnumRg = red;',
  29810. 'procedure DoIt(p: TEnumRg);',
  29811. 'begin',
  29812. ' e:=p;',
  29813. ' p:=TEnumRg(0);',
  29814. ' p:=succ(e);',
  29815. 'end;',
  29816. '{$R-}',
  29817. 'procedure DoSome;',
  29818. 'begin',
  29819. ' DoIt(e);',
  29820. ' e:=TEnum(1);',
  29821. ' e:=pred(e);',
  29822. 'end;',
  29823. 'begin',
  29824. '{$R+}',
  29825. '']);
  29826. ConvertProgram;
  29827. CheckSource('TestRangeChecks_AssignEnumRange',
  29828. LinesToStr([ // statements
  29829. 'this.TEnum = {',
  29830. ' "0": "red",',
  29831. ' red: 0,',
  29832. ' "1": "green",',
  29833. ' green: 1',
  29834. '};',
  29835. 'this.e = $mod.TEnum.red;',
  29836. 'this.DoIt = function (p) {',
  29837. ' rtl.rc(p, 0, 1);',
  29838. ' $mod.e = rtl.rc(p, 0, 1);',
  29839. ' p = 0;',
  29840. ' p = rtl.rc($mod.e + 1, 0, 1);',
  29841. '};',
  29842. 'this.DoSome = function () {',
  29843. ' $mod.DoIt($mod.e);',
  29844. ' $mod.e = 1;',
  29845. ' $mod.e = $mod.e - 1;',
  29846. '};',
  29847. '']),
  29848. LinesToStr([ // $mod.$main
  29849. '']));
  29850. end;
  29851. procedure TTestModule.TestRangeChecks_AssignChar;
  29852. begin
  29853. StartProgram(false);
  29854. Add([
  29855. '{$R+}',
  29856. 'type',
  29857. ' TLetter = char;',
  29858. 'var',
  29859. ' b: TLetter = ''2'';',
  29860. ' w: TLetter = ''3'';',
  29861. 'procedure DoIt(p: TLetter);',
  29862. 'begin',
  29863. ' b:=w;',
  29864. ' b:=''1'';',
  29865. 'end;',
  29866. '{$R-}',
  29867. 'procedure DoSome;',
  29868. 'begin',
  29869. ' DoIt(w);',
  29870. ' b:=w;',
  29871. ' b:=''2'';',
  29872. 'end;',
  29873. 'begin',
  29874. '{$R+}',
  29875. '']);
  29876. ConvertProgram;
  29877. CheckSource('TestRangeChecks_AssignChar',
  29878. LinesToStr([ // statements
  29879. 'this.b = "2";',
  29880. 'this.w = "3";',
  29881. 'this.DoIt = function (p) {',
  29882. ' rtl.rcc(p, 0, 65535);',
  29883. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  29884. ' $mod.b = "1";',
  29885. '};',
  29886. 'this.DoSome = function () {',
  29887. ' $mod.DoIt($mod.w);',
  29888. ' $mod.b = $mod.w;',
  29889. ' $mod.b = "2";',
  29890. '};',
  29891. '']),
  29892. LinesToStr([ // $mod.$main
  29893. '']));
  29894. end;
  29895. procedure TTestModule.TestRangeChecks_AssignCharRange;
  29896. begin
  29897. StartProgram(false);
  29898. Add([
  29899. '{$R+}',
  29900. 'type TDigit = ''0''..''9'';',
  29901. 'var',
  29902. ' b: TDigit = ''2'';',
  29903. ' w: TDigit = ''3'';',
  29904. 'procedure DoIt(p: TDigit);',
  29905. 'begin',
  29906. ' b:=w;',
  29907. ' b:=''1'';',
  29908. 'end;',
  29909. '{$R-}',
  29910. 'procedure DoSome;',
  29911. 'begin',
  29912. ' DoIt(w);',
  29913. ' b:=w;',
  29914. ' b:=''2'';',
  29915. 'end;',
  29916. 'begin',
  29917. '{$R+}',
  29918. '']);
  29919. ConvertProgram;
  29920. CheckSource('TestRangeChecks_AssignCharRange',
  29921. LinesToStr([ // statements
  29922. 'this.b = "2";',
  29923. 'this.w = "3";',
  29924. 'this.DoIt = function (p) {',
  29925. ' rtl.rcc(p, 48, 57);',
  29926. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  29927. ' $mod.b = "1";',
  29928. '};',
  29929. 'this.DoSome = function () {',
  29930. ' $mod.DoIt($mod.w);',
  29931. ' $mod.b = $mod.w;',
  29932. ' $mod.b = "2";',
  29933. '};',
  29934. '']),
  29935. LinesToStr([ // $mod.$main
  29936. '']));
  29937. end;
  29938. procedure TTestModule.TestRangeChecks_ArrayIndex;
  29939. begin
  29940. StartProgram(false);
  29941. Add([
  29942. '{$R+}',
  29943. 'type',
  29944. ' Ten = 1..10;',
  29945. ' TArr = array of Ten;',
  29946. ' TArrArr = array of TArr;',
  29947. ' TArrByte = array[byte] of Ten;',
  29948. ' TArrChar = array[''0''..''9''] of Ten;',
  29949. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  29950. ' TObject = class',
  29951. ' A: TArr;',
  29952. ' end;',
  29953. 'procedure DoIt;',
  29954. 'var',
  29955. ' Arr: TArr;',
  29956. ' ArrArr: TArrArr;',
  29957. ' ArrByte: TArrByte;',
  29958. ' ArrChar: TArrChar;',
  29959. ' ArrByteChar: TArrByteChar;',
  29960. ' i: Ten;',
  29961. ' c: char;',
  29962. ' o: tobject;',
  29963. 'begin',
  29964. ' i:=Arr[1];',
  29965. ' i:=ArrByteChar[1,''2''];',
  29966. ' Arr[1]:=Arr[1];',
  29967. ' Arr[i]:=Arr[i];',
  29968. ' ArrByte[3]:=ArrByte[3];',
  29969. ' ArrByte[i]:=ArrByte[i];',
  29970. ' ArrChar[''5'']:=ArrChar[''5''];',
  29971. ' ArrChar[c]:=ArrChar[c];',
  29972. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  29973. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  29974. ' o.a[i]:=o.a[i];',
  29975. 'end;',
  29976. 'begin',
  29977. '']);
  29978. ConvertProgram;
  29979. CheckSource('TestRangeChecks_ArrayIndex',
  29980. LinesToStr([ // statements
  29981. 'rtl.createClass($mod, "TObject", null, function () {',
  29982. ' this.$init = function () {',
  29983. ' this.A = [];',
  29984. ' };',
  29985. ' this.$final = function () {',
  29986. ' this.A = undefined;',
  29987. ' };',
  29988. '});',
  29989. 'this.DoIt = function () {',
  29990. ' var Arr = [];',
  29991. ' var ArrArr = [];',
  29992. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  29993. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  29994. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  29995. ' var i = 0;',
  29996. ' var c = "";',
  29997. ' var o = null;',
  29998. ' i = rtl.rc(Arr[1], 1, 10);',
  29999. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  30000. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  30001. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  30002. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  30003. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  30004. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  30005. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  30006. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  30007. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  30008. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  30009. '};',
  30010. '']),
  30011. LinesToStr([ // $mod.$main
  30012. '']));
  30013. end;
  30014. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  30015. begin
  30016. StartProgram(false);
  30017. Add([
  30018. '{$R+}',
  30019. 'type',
  30020. ' Ten = 1..10;',
  30021. ' TRec = record x: Ten end;',
  30022. ' TArr = array of TRec;',
  30023. ' TArrArr = array of TArr;',
  30024. ' TObject = class',
  30025. ' A: TArr;',
  30026. ' end;',
  30027. 'procedure DoIt;',
  30028. 'var',
  30029. ' Arr: TArr;',
  30030. ' ArrArr: TArrArr;',
  30031. ' i: Ten;',
  30032. ' o: tobject;',
  30033. 'begin',
  30034. ' Arr[1]:=Arr[1];',
  30035. ' Arr[i]:=Arr[i+1];',
  30036. ' o.a[i]:=o.a[i+2];',
  30037. 'end;',
  30038. 'begin',
  30039. '']);
  30040. ConvertProgram;
  30041. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  30042. LinesToStr([ // statements
  30043. 'rtl.recNewT($mod, "TRec", function () {',
  30044. ' this.x = 0;',
  30045. ' this.$eq = function (b) {',
  30046. ' return this.x === b.x;',
  30047. ' };',
  30048. ' this.$assign = function (s) {',
  30049. ' this.x = s.x;',
  30050. ' return this;',
  30051. ' };',
  30052. '});',
  30053. 'rtl.createClass($mod, "TObject", null, function () {',
  30054. ' this.$init = function () {',
  30055. ' this.A = [];',
  30056. ' };',
  30057. ' this.$final = function () {',
  30058. ' this.A = undefined;',
  30059. ' };',
  30060. '});',
  30061. 'this.DoIt = function () {',
  30062. ' var Arr = [];',
  30063. ' var ArrArr = [];',
  30064. ' var i = 0;',
  30065. ' var o = null;',
  30066. ' Arr[1].$assign(Arr[1]);',
  30067. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  30068. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  30069. '};',
  30070. '']),
  30071. LinesToStr([ // $mod.$main
  30072. '']));
  30073. end;
  30074. procedure TTestModule.TestRangeChecks_StringIndex;
  30075. begin
  30076. StartProgram(false);
  30077. Add([
  30078. 'type',
  30079. ' TObject = class',
  30080. ' S: string;',
  30081. ' end;',
  30082. '{$R+}',
  30083. 'procedure DoIt(var h: string);',
  30084. 'var',
  30085. ' s: string;',
  30086. ' i: longint;',
  30087. ' c: char;',
  30088. ' o: tobject;',
  30089. 'begin',
  30090. ' c:=s[1];',
  30091. ' s[i]:=s[i];',
  30092. ' h[i]:=h[i];',
  30093. ' c:=o.s[i];',
  30094. ' o.s[i]:=c;',
  30095. 'end;',
  30096. 'begin',
  30097. '']);
  30098. ConvertProgram;
  30099. CheckSource('TestRangeChecks_StringIndex',
  30100. LinesToStr([ // statements
  30101. 'rtl.createClass($mod, "TObject", null, function () {',
  30102. ' this.$init = function () {',
  30103. ' this.S = "";',
  30104. ' };',
  30105. ' this.$final = function () {',
  30106. ' };',
  30107. '});',
  30108. 'this.DoIt = function (h) {',
  30109. ' var s = "";',
  30110. ' var i = 0;',
  30111. ' var c = "";',
  30112. ' var o = null;',
  30113. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  30114. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  30115. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  30116. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  30117. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  30118. '};',
  30119. '']),
  30120. LinesToStr([ // $mod.$main
  30121. '']));
  30122. end;
  30123. procedure TTestModule.TestRangeChecks_TypecastInt;
  30124. begin
  30125. StartProgram(false);
  30126. Add([
  30127. '{$R+}',
  30128. 'var',
  30129. ' i: nativeint;',
  30130. ' b: byte;',
  30131. ' sh: shortint;',
  30132. ' w: word;',
  30133. ' sm: smallint;',
  30134. ' lw: longword;',
  30135. ' li: longint;',
  30136. 'begin',
  30137. ' b:=12+byte(i);',
  30138. ' sh:=12+shortint(i);',
  30139. ' w:=12+word(i);',
  30140. ' sm:=12+smallint(i);',
  30141. ' lw:=12+longword(i);',
  30142. ' li:=12+longint(i);',
  30143. '']);
  30144. ConvertProgram;
  30145. CheckSource('TestRangeChecks_TypecastInt',
  30146. LinesToStr([
  30147. 'this.i = 0;',
  30148. 'this.b = 0;',
  30149. 'this.sh = 0;',
  30150. 'this.w = 0;',
  30151. 'this.sm = 0;',
  30152. 'this.lw = 0;',
  30153. 'this.li = 0;',
  30154. '']),
  30155. LinesToStr([
  30156. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  30157. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  30158. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  30159. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  30160. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  30161. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  30162. '']));
  30163. end;
  30164. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  30165. begin
  30166. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30167. StartProgram(false);
  30168. Add([
  30169. '{$modeswitch typehelpers}',
  30170. '{$R+}',
  30171. 'type',
  30172. ' TObject = class',
  30173. ' FSize: byte;',
  30174. ' property Size: byte read FSize;',
  30175. ' end;',
  30176. ' THelper = type helper for byte',
  30177. ' procedure SetIt(w: word);',
  30178. ' end;',
  30179. 'procedure THelper.SetIt(w: word);',
  30180. 'begin',
  30181. ' Self:=w;',
  30182. 'end;',
  30183. 'function GetIt: byte;',
  30184. 'begin',
  30185. ' Result.SetIt(2);',
  30186. 'end;',
  30187. 'var',
  30188. ' b: byte = 3;',
  30189. ' o: TObject;',
  30190. 'begin',
  30191. ' b.SetIt(14);',
  30192. ' with b do SetIt(15);',
  30193. ' o.Size.SetIt(16);',
  30194. '']);
  30195. ConvertProgram;
  30196. CheckSource('TestRangeChecks_AssignInt',
  30197. LinesToStr([ // statements
  30198. 'rtl.createClass($mod, "TObject", null, function () {',
  30199. ' this.$init = function () {',
  30200. ' this.FSize = 0;',
  30201. ' };',
  30202. ' this.$final = function () {',
  30203. ' };',
  30204. '});',
  30205. 'rtl.createHelper($mod, "THelper", null, function () {',
  30206. ' this.SetIt = function (w) {',
  30207. ' rtl.rc(w, 0, 65535);',
  30208. ' this.set(w);',
  30209. ' };',
  30210. '});',
  30211. 'this.GetIt = function () {',
  30212. ' var Result = 0;',
  30213. ' $mod.THelper.SetIt.call({',
  30214. ' get: function () {',
  30215. ' return Result;',
  30216. ' },',
  30217. ' set: function (v) {',
  30218. ' rtl.rc(v, 0, 255);',
  30219. ' Result = v;',
  30220. ' }',
  30221. ' }, 2);',
  30222. ' return Result;',
  30223. '};',
  30224. 'this.b = 3;',
  30225. 'this.o = null;',
  30226. '']),
  30227. LinesToStr([ // $mod.$main
  30228. '$mod.THelper.SetIt.call({',
  30229. ' p: $mod,',
  30230. ' get: function () {',
  30231. ' return this.p.b;',
  30232. ' },',
  30233. ' set: function (v) {',
  30234. ' rtl.rc(v, 0, 255);',
  30235. ' this.p.b = v;',
  30236. ' }',
  30237. '}, 14);',
  30238. 'var $with1 = $mod.b;',
  30239. '$mod.THelper.SetIt.call({',
  30240. ' get: function () {',
  30241. ' return $with1;',
  30242. ' },',
  30243. ' set: function (v) {',
  30244. ' rtl.rc(v, 0, 255);',
  30245. ' $with1 = v;',
  30246. ' }',
  30247. '}, 15);',
  30248. '$mod.THelper.SetIt.call({',
  30249. ' p: $mod.o,',
  30250. ' get: function () {',
  30251. ' return this.p.FSize;',
  30252. ' },',
  30253. ' set: function (v) {',
  30254. ' rtl.rc(v, 0, 255);',
  30255. ' this.p.FSize = v;',
  30256. ' }',
  30257. '}, 16);',
  30258. '']));
  30259. end;
  30260. Initialization
  30261. RegisterTests([TTestModule]);
  30262. end.