tcmodules.pas 772 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078
  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. );
  46. TSystemUnitParts = set of TSystemUnitPart;
  47. { TTestHintMessage }
  48. TTestHintMessage = class
  49. public
  50. Id: int64;
  51. MsgType: TMessageType;
  52. MsgNumber: integer;
  53. Msg: string;
  54. SourcePos: TPasSourcePos;
  55. end;
  56. { TTestPasParser }
  57. TTestPasParser = Class(TPasParser)
  58. end;
  59. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  60. { TTestEnginePasResolver }
  61. TTestEnginePasResolver = class(TPas2JsResolver)
  62. private
  63. FFilename: string;
  64. FModule: TPasModule;
  65. FOnFindUnit: TOnFindUnit;
  66. FParser: TTestPasParser;
  67. FStreamResolver: TStreamResolver;
  68. FScanner: TPas2jsPasScanner;
  69. FSource: string;
  70. public
  71. destructor Destroy; override;
  72. function FindUnit(const AName, InFilename: String; NameExpr,
  73. InFileExpr: TPasExpr): TPasModule; override;
  74. procedure UsedInterfacesFinished(Section: TPasSection); override;
  75. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  76. property Filename: string read FFilename write FFilename;
  77. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  78. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  79. property Parser: TTestPasParser read FParser write FParser;
  80. property Source: string read FSource write FSource;
  81. property Module: TPasModule read FModule;
  82. end;
  83. { TCustomTestModule }
  84. TCustomTestModule = Class(TTestCase)
  85. private
  86. FConverter: TPasToJSConverter;
  87. FEngine: TTestEnginePasResolver;
  88. FExpectedErrorClass: ExceptClass;
  89. FExpectedErrorMsg: string;
  90. FExpectedErrorNumber: integer;
  91. FFilename: string;
  92. FFileResolver: TStreamResolver;
  93. FJSImplementationSrc: TJSSourceElements;
  94. FJSImplementationUses: TJSArrayLiteral;
  95. FJSInitBody: TJSFunctionBody;
  96. FJSImplentationUses: TJSArrayLiteral;
  97. FJSInterfaceUses: TJSArrayLiteral;
  98. FJSModule: TJSSourceElements;
  99. FJSModuleSrc: TJSSourceElements;
  100. FJSSource: TStringList;
  101. FModule: TPasModule;
  102. FJSModuleCallArgs: TJSArguments;
  103. FModules: TObjectList;// list of TTestEnginePasResolver
  104. FParser: TTestPasParser;
  105. FPasProgram: TPasProgram;
  106. FHintMsgs: TObjectList; // list of TTestHintMessage
  107. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  108. FJSRegModuleCall: TJSCallExpression;
  109. FScanner: TPas2jsPasScanner;
  110. FSkipTests: boolean;
  111. FSource: TStringList;
  112. FFirstPasStatement: TPasImplBlock;
  113. {$IFDEF EnablePasTreeGlobalRefCount}
  114. FElementRefCountAtSetup: int64;
  115. {$ENDIF}
  116. function GetMsgCount: integer;
  117. function GetMsgs(Index: integer): TTestHintMessage;
  118. function GetResolverCount: integer;
  119. function GetResolvers(Index: integer): TTestEnginePasResolver;
  120. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  121. procedure OnParserLog(Sender: TObject; const Msg: String);
  122. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  123. procedure OnScannerLog(Sender: TObject; const Msg: String);
  124. protected
  125. procedure SetUp; override;
  126. function CreateConverter: TPasToJSConverter; virtual;
  127. function LoadUnit(const aUnitName: String): TPasModule;
  128. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  129. procedure TearDown; override;
  130. Procedure Add(Line: string); virtual;
  131. Procedure Add(const Lines: array of string);
  132. Procedure StartParsing; virtual;
  133. procedure ParseModuleQueue; virtual;
  134. procedure ParseModule; virtual;
  135. procedure ParseProgram; virtual;
  136. procedure ParseUnit; virtual;
  137. protected
  138. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  139. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  140. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  141. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  142. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  143. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  144. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  145. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  146. procedure ConvertModule; virtual;
  147. procedure ConvertProgram; virtual;
  148. procedure ConvertUnit; virtual;
  149. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  150. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  151. function GetDottedIdentifier(El: TJSElement): string;
  152. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  153. ImplStatements: string = ''); virtual;
  154. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  155. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  156. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  157. Msg: string; Marker: PSrcMarker = nil); virtual;
  158. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  159. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  160. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  161. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  162. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  163. function IsErrorExpected(E: Exception): boolean;
  164. procedure HandleScannerError(E: EScannerError);
  165. procedure HandleParserError(E: EParserError);
  166. procedure HandlePasResolveError(E: EPasResolve);
  167. procedure HandlePas2JSError(E: EPas2JS);
  168. procedure HandleException(E: Exception);
  169. procedure FailException(E: Exception);
  170. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  171. function IndexOfResolver(const Filename: string): integer;
  172. function GetResolver(const Filename: string): TTestEnginePasResolver;
  173. function GetDefaultNamespace: string;
  174. property PasProgram: TPasProgram Read FPasProgram;
  175. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  176. property ResolverCount: integer read GetResolverCount;
  177. property Engine: TTestEnginePasResolver read FEngine;
  178. property Filename: string read FFilename;
  179. Property Module: TPasModule Read FModule;
  180. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  181. property Converter: TPasToJSConverter read FConverter;
  182. property JSSource: TStringList read FJSSource;
  183. property JSModule: TJSSourceElements read FJSModule;
  184. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  185. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  186. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  187. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  188. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  189. property JSInitBody: TJSFunctionBody read FJSInitBody;
  190. property JSImplementationSrc: TJSSourceElements read FJSImplementationSrc;
  191. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  192. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  193. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  194. property SkipTests: boolean read FSkipTests write FSkipTests;
  195. public
  196. constructor Create; override;
  197. destructor Destroy; override;
  198. property Source: TStringList read FSource;
  199. property FileResolver: TStreamResolver read FFileResolver;
  200. property Scanner: TPas2jsPasScanner read FScanner;
  201. property Parser: TTestPasParser read FParser;
  202. property MsgCount: integer read GetMsgCount;
  203. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  204. end;
  205. { TTestModule }
  206. TTestModule = class(TCustomTestModule)
  207. Published
  208. Procedure TestReservedWords;
  209. // program/units
  210. Procedure TestEmptyProgram;
  211. Procedure TestEmptyProgramUseStrict;
  212. Procedure TestEmptyUnit;
  213. Procedure TestEmptyUnitUseStrict;
  214. Procedure TestDottedUnitNames;
  215. Procedure TestDottedUnitNameImpl;
  216. Procedure TestDottedUnitExpr;
  217. Procedure Test_ModeFPCFail;
  218. Procedure Test_ModeSwitchCBlocksFail;
  219. Procedure TestUnit_UseSystem;
  220. Procedure TestUnit_Intf1Impl2Intf1;
  221. Procedure TestIncludeVersion;
  222. // vars/const
  223. Procedure TestVarInt;
  224. Procedure TestVarBaseTypes;
  225. Procedure TestBaseTypeSingleFail;
  226. Procedure TestBaseTypeExtendedFail;
  227. Procedure TestConstBaseTypes;
  228. Procedure TestUnitImplVars;
  229. Procedure TestUnitImplConsts;
  230. Procedure TestUnitImplRecord;
  231. Procedure TestRenameJSNameConflict;
  232. Procedure TestLocalConst;
  233. Procedure TestVarExternal;
  234. Procedure TestVarExternalOtherUnit;
  235. Procedure TestVarAbsoluteFail;
  236. Procedure TestConstExternal;
  237. // numbers
  238. Procedure TestDouble;
  239. Procedure TestInteger;
  240. Procedure TestIntegerRange;
  241. Procedure TestIntegerTypecasts;
  242. Procedure TestBitwiseAndNativeIntWarn;
  243. Procedure TestCurrency;
  244. Procedure TestForBoolDo;
  245. Procedure TestForIntDo;
  246. Procedure TestForIntInDo;
  247. // strings
  248. Procedure TestCharConst;
  249. Procedure TestChar_Compare;
  250. Procedure TestChar_BuiltInProcs;
  251. Procedure TestStringConst;
  252. Procedure TestStringConstSurrogate;
  253. Procedure TestString_Length;
  254. Procedure TestString_Compare;
  255. Procedure TestString_SetLength;
  256. Procedure TestString_CharAt;
  257. Procedure TestStringHMinusFail;
  258. Procedure TestStr;
  259. Procedure TestBaseType_AnsiStringFail;
  260. Procedure TestBaseType_WideStringFail;
  261. Procedure TestBaseType_ShortStringFail;
  262. Procedure TestBaseType_RawByteStringFail;
  263. Procedure TestTypeShortstring_Fail;
  264. Procedure TestCharSet_Custom;
  265. Procedure TestForCharDo;
  266. Procedure TestForCharInDo;
  267. // alias types
  268. Procedure TestAliasTypeRef;
  269. Procedure TestTypeCast_BaseTypes;
  270. Procedure TestTypeCast_AliasBaseTypes;
  271. // functions
  272. Procedure TestEmptyProc;
  273. Procedure TestProcOneParam;
  274. Procedure TestFunctionWithoutParams;
  275. Procedure TestProcedureWithoutParams;
  276. Procedure TestPrgProcVar;
  277. Procedure TestProcTwoArgs;
  278. Procedure TestProc_DefaultValue;
  279. Procedure TestUnitProcVar;
  280. Procedure TestImplProc;
  281. Procedure TestFunctionResult;
  282. Procedure TestNestedProc;
  283. Procedure TestNestedProc_ResultString;
  284. Procedure TestForwardProc;
  285. Procedure TestNestedForwardProc;
  286. Procedure TestAssignFunctionResult;
  287. Procedure TestFunctionResultInCondition;
  288. Procedure TestFunctionResultInForLoop;
  289. Procedure TestFunctionResultInTypeCast;
  290. Procedure TestExit;
  291. Procedure TestBreak;
  292. Procedure TestBreakAsVar;
  293. Procedure TestContinue;
  294. Procedure TestProc_External;
  295. Procedure TestProc_ExternalOtherUnit;
  296. Procedure TestProc_Asm;
  297. Procedure TestProc_Assembler;
  298. Procedure TestProc_VarParam;
  299. Procedure TestProc_VarParamString;
  300. Procedure TestProc_VarParamV;
  301. Procedure TestProc_Overload;
  302. Procedure TestProc_OverloadForward;
  303. Procedure TestProc_OverloadIntfImpl;
  304. Procedure TestProc_OverloadNested;
  305. Procedure TestProc_OverloadUnitCycle;
  306. Procedure TestProc_Varargs;
  307. Procedure TestProc_ConstOrder;
  308. Procedure TestProc_DuplicateConst;
  309. Procedure TestProc_LocalVarAbsolute;
  310. Procedure TestProc_ReservedWords;
  311. // anonymous functions
  312. Procedure TestAnonymousProc_Assign_ObjFPC;
  313. Procedure TestAnonymousProc_Assign_Delphi;
  314. Procedure TestAnonymousProc_Arg;
  315. Procedure TestAnonymousProc_Typecast;
  316. Procedure TestAnonymousProc_With;
  317. Procedure TestAnonymousProc_ExceptOn;
  318. Procedure TestAnonymousProc_Nested;
  319. Procedure TestAnonymousProc_NestedAssignResult;
  320. // enums, sets
  321. Procedure TestEnum_Name;
  322. Procedure TestEnum_Number;
  323. Procedure TestEnum_ConstFail;
  324. Procedure TestEnum_Functions;
  325. Procedure TestEnum_AsParams;
  326. Procedure TestEnumRange_Array;
  327. Procedure TestEnum_ForIn;
  328. Procedure TestEnum_ScopedNumber;
  329. Procedure TestEnum_InFunction;
  330. Procedure TestSet_Enum;
  331. Procedure TestSet_Operators;
  332. Procedure TestSet_Operator_In;
  333. Procedure TestSet_Functions;
  334. Procedure TestSet_PassAsArgClone;
  335. Procedure TestSet_AsParams;
  336. Procedure TestSet_Property;
  337. Procedure TestSet_EnumConst;
  338. Procedure TestSet_IntConst;
  339. Procedure TestSet_AnonymousEnumType;
  340. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  341. Procedure TestSet_ConstEnum;
  342. Procedure TestSet_ConstChar;
  343. Procedure TestSet_ConstInt;
  344. Procedure TestSet_InFunction;
  345. Procedure TestSet_ForIn;
  346. // statements
  347. Procedure TestNestBegin;
  348. Procedure TestIncDec;
  349. Procedure TestLoHiFpcMode;
  350. Procedure TestLoHiDelphiMode;
  351. Procedure TestAssignments;
  352. Procedure TestArithmeticOperators1;
  353. Procedure TestLogicalOperators;
  354. Procedure TestBitwiseOperators;
  355. Procedure TestFunctionInt;
  356. Procedure TestFunctionString;
  357. Procedure TestIfThen;
  358. Procedure TestForLoop;
  359. Procedure TestForLoopInsideFunction;
  360. Procedure TestForLoop_ReadVarAfter;
  361. Procedure TestForLoop_Nested;
  362. Procedure TestRepeatUntil;
  363. Procedure TestAsmBlock;
  364. Procedure TestAsmPas_Impl; // ToDo
  365. Procedure TestTryFinally;
  366. Procedure TestTryExcept;
  367. Procedure TestTryExcept_ReservedWords;
  368. Procedure TestIfThenRaiseElse;
  369. Procedure TestCaseOf;
  370. Procedure TestCaseOf_UseSwitch;
  371. Procedure TestCaseOfNoElse;
  372. Procedure TestCaseOfNoElse_UseSwitch;
  373. Procedure TestCaseOfRange;
  374. Procedure TestCaseOfString;
  375. Procedure TestCaseOfChar;
  376. Procedure TestCaseOfExternalClassConst;
  377. Procedure TestDebugger;
  378. // arrays
  379. Procedure TestArray_Dynamic;
  380. Procedure TestArray_Dynamic_Nil;
  381. Procedure TestArray_DynMultiDimensional;
  382. Procedure TestArray_StaticInt;
  383. Procedure TestArray_StaticBool;
  384. Procedure TestArray_StaticChar;
  385. Procedure TestArray_StaticMultiDim;
  386. Procedure TestArray_StaticInFunction;
  387. Procedure TestArrayOfRecord;
  388. Procedure TestArray_StaticRecord;
  389. Procedure TestArrayOfSet;
  390. Procedure TestArray_DynAsParam;
  391. Procedure TestArray_StaticAsParam;
  392. Procedure TestArrayElement_AsParams;
  393. Procedure TestArrayElementFromFuncResult_AsParams;
  394. Procedure TestArrayEnumTypeRange;
  395. Procedure TestArray_SetLengthOutArg;
  396. Procedure TestArray_SetLengthProperty;
  397. Procedure TestArray_SetLengthMultiDim;
  398. Procedure TestArray_OpenArrayOfString;
  399. Procedure TestArray_Concat;
  400. Procedure TestArray_Copy;
  401. Procedure TestArray_InsertDelete;
  402. Procedure TestArray_DynArrayConstObjFPC;
  403. Procedure TestArray_DynArrayConstDelphi;
  404. Procedure TestArray_ArrayLitAsParam;
  405. Procedure TestArray_ArrayLitMultiDimAsParam;
  406. Procedure TestArray_ArrayLitStaticAsParam;
  407. Procedure TestArray_ForInArrOfString;
  408. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  409. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  410. Procedure TestArrayOfConst_TVarRec;
  411. Procedure TestArrayOfConst_PassBaseTypes;
  412. Procedure TestArrayOfConst_PassObj;
  413. // ToDo: tcfiler TPasModuleScope.SystemTVarRec TPas2JSModuleScope.SystemVarRecs
  414. // record
  415. Procedure TestRecord_Empty;
  416. Procedure TestRecord_Var;
  417. Procedure TestRecord_VarExternal;
  418. Procedure TestRecord_WithDo;
  419. Procedure TestRecord_Assign;
  420. Procedure TestRecord_AsParams;
  421. Procedure TestRecordElement_AsParams;
  422. Procedure TestRecordElementFromFuncResult_AsParams;
  423. Procedure TestRecordElementFromWith_AsParams;
  424. Procedure TestRecord_Equal;
  425. Procedure TestRecord_TypeCastJSValueToRecord;
  426. Procedure TestRecord_VariantFail;
  427. Procedure TestRecord_FieldArray;
  428. Procedure TestRecord_Const;
  429. Procedure TestRecord_TypecastFail;
  430. Procedure TestRecord_InFunction;
  431. // ToDo: RTTI of local record
  432. // ToDo: pcu local record, name clash and rtti
  433. // advanced record
  434. Procedure TestAdvRecord_Function;
  435. Procedure TestAdvRecord_Property;
  436. Procedure TestAdvRecord_PropertyDefault;
  437. Procedure TestAdvRecord_Property_ClassMethod;
  438. Procedure TestAdvRecord_Const;
  439. Procedure TestAdvRecord_ExternalField;
  440. Procedure TestAdvRecord_SubRecord;
  441. Procedure TestAdvRecord_SubClass;
  442. Procedure TestAdvRecord_SubInterfaceFail;
  443. Procedure TestAdvRecord_Constructor;
  444. // ToDo: class constructor
  445. // classes
  446. Procedure TestClass_TObjectDefaultConstructor;
  447. Procedure TestClass_TObjectConstructorWithParams;
  448. Procedure TestClass_TObjectConstructorWithDefaultParam;
  449. Procedure TestClass_Var;
  450. Procedure TestClass_Method;
  451. Procedure TestClass_Implementation;
  452. Procedure TestClass_Inheritance;
  453. Procedure TestClass_TypeAlias;
  454. Procedure TestClass_AbstractMethod;
  455. Procedure TestClass_CallInherited_ProcNoParams;
  456. Procedure TestClass_CallInherited_WithParams;
  457. Procedure TestClasS_CallInheritedConstructor;
  458. Procedure TestClass_ClassVar_Assign;
  459. Procedure TestClass_CallClassMethod;
  460. Procedure TestClass_Property;
  461. Procedure TestClass_Property_ClassMethod;
  462. Procedure TestClass_Property_Indexed;
  463. Procedure TestClass_Property_IndexSpec;
  464. Procedure TestClass_PropertyOfTypeArray;
  465. Procedure TestClass_PropertyDefault;
  466. Procedure TestClass_PropertyDefault2;
  467. Procedure TestClass_PropertyOverride;
  468. Procedure TestClass_PropertyIncVisibility;
  469. Procedure TestClass_Assigned;
  470. Procedure TestClass_WithClassDoCreate;
  471. Procedure TestClass_WithClassInstDoProperty;
  472. Procedure TestClass_WithClassInstDoPropertyWithParams;
  473. Procedure TestClass_WithClassInstDoFunc;
  474. Procedure TestClass_TypeCast;
  475. Procedure TestClass_TypeCastUntypedParam;
  476. Procedure TestClass_Overloads;
  477. Procedure TestClass_OverloadsAncestor;
  478. Procedure TestClass_OverloadConstructor;
  479. Procedure TestClass_OverloadDelphiOverride;
  480. Procedure TestClass_ReintroducedVar;
  481. Procedure TestClass_RaiseDescendant;
  482. Procedure TestClass_ExternalMethod;
  483. Procedure TestClass_ExternalVirtualNameMismatchFail;
  484. Procedure TestClass_ExternalOverrideFail;
  485. Procedure TestClass_ExternalVar;
  486. Procedure TestClass_Const;
  487. Procedure TestClass_LocalVarSelfFail;
  488. Procedure TestClass_ArgSelfFail;
  489. Procedure TestClass_NestedProcSelf;
  490. Procedure TestClass_NestedProcSelf2;
  491. Procedure TestClass_NestedProcClassSelf;
  492. Procedure TestClass_NestedProcCallInherited;
  493. Procedure TestClass_TObjectFree;
  494. Procedure TestClass_TObjectFreeNewInstance;
  495. Procedure TestClass_TObjectFreeLowerCase;
  496. Procedure TestClass_TObjectFreeFunctionFail;
  497. Procedure TestClass_TObjectFreePropertyFail;
  498. Procedure TestClass_ForIn;
  499. // class of
  500. Procedure TestClassOf_Create;
  501. Procedure TestClassOf_Call;
  502. Procedure TestClassOf_Assign;
  503. Procedure TestClassOf_Is;
  504. Procedure TestClassOf_Compare;
  505. Procedure TestClassOf_ClassVar;
  506. Procedure TestClassOf_ClassMethod;
  507. Procedure TestClassOf_ClassProperty;
  508. Procedure TestClassOf_ClassMethodSelf;
  509. Procedure TestClassOf_TypeCast;
  510. Procedure TestClassOf_ImplicitFunctionCall;
  511. Procedure TestClassOf_Const;
  512. // nested class
  513. Procedure TestNestedClass_Alias;
  514. Procedure TestNestedClass_Record;
  515. Procedure TestNestedClass_Class;
  516. // external class
  517. Procedure TestExternalClass_Var;
  518. Procedure TestExternalClass_Const;
  519. Procedure TestExternalClass_Dollar;
  520. Procedure TestExternalClass_DuplicateVarFail;
  521. Procedure TestExternalClass_Method;
  522. Procedure TestExternalClass_ClassMethod;
  523. Procedure TestExternalClass_FunctionResultInTypeCast;
  524. Procedure TestExternalClass_NonExternalOverride;
  525. Procedure TestExternalClass_OverloadHint;
  526. Procedure TestExternalClass_SameNamePublishedProperty;
  527. Procedure TestExternalClass_Property;
  528. Procedure TestExternalClass_PropertyDate;
  529. Procedure TestExternalClass_ClassProperty;
  530. Procedure TestExternalClass_ClassOf;
  531. Procedure TestExternalClass_ClassOtherUnit;
  532. Procedure TestExternalClass_Is;
  533. Procedure TestExternalClass_As;
  534. Procedure TestExternalClass_DestructorFail;
  535. Procedure TestExternalClass_New;
  536. Procedure TestExternalClass_ClassOf_New;
  537. Procedure TestExternalClass_FuncClassOf_New;
  538. Procedure TestExternalClass_New_PasClassFail;
  539. Procedure TestExternalClass_New_PasClassBracketsFail;
  540. Procedure TestExternalClass_LocalConstSameName;
  541. Procedure TestExternalClass_ReintroduceOverload;
  542. Procedure TestExternalClass_Inherited;
  543. Procedure TestExternalClass_PascalAncestorFail;
  544. Procedure TestExternalClass_NewInstance;
  545. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  546. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  547. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  548. Procedure TestExternalClass_PascalProperty;
  549. Procedure TestExternalClass_TypeCastToRootClass;
  550. Procedure TestExternalClass_TypeCastToJSObject;
  551. Procedure TestExternalClass_TypeCastStringToExternalString;
  552. Procedure TestExternalClass_TypeCastToJSFunction;
  553. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  554. Procedure TestExternalClass_BracketAccessor;
  555. Procedure TestExternalClass_BracketAccessor_Call;
  556. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  557. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  558. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  559. Procedure TestExternalClass_BracketAccessor_MultiType;
  560. Procedure TestExternalClass_BracketAccessor_Index;
  561. Procedure TestExternalClass_ForInJSObject;
  562. Procedure TestExternalClass_ForInJSArray;
  563. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  564. // class interfaces
  565. Procedure TestClassInterface_Corba;
  566. Procedure TestClassInterface_ProcExternalFail;
  567. Procedure TestClassInterface_Overloads;
  568. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  569. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  570. Procedure TestClassInterface_AncestorImpl;
  571. Procedure TestClassInterface_ImplReintroduce;
  572. Procedure TestClassInterface_MethodResolution;
  573. Procedure TestClassInterface_AncestorMoreInterfaces;
  574. Procedure TestClassInterface_MethodOverride;
  575. Procedure TestClassInterface_Corba_Delegation;
  576. Procedure TestClassInterface_Corba_DelegationStatic;
  577. Procedure TestClassInterface_Corba_Operators;
  578. Procedure TestClassInterface_Corba_Args;
  579. Procedure TestClassInterface_Corba_ForIn;
  580. Procedure TestClassInterface_COM_AssignVar;
  581. Procedure TestClassInterface_COM_AssignArg;
  582. Procedure TestClassInterface_COM_FunctionResult;
  583. Procedure TestClassInterface_COM_InheritedFuncResult;
  584. Procedure TestClassInterface_COM_IsAsTypeCasts;
  585. Procedure TestClassInterface_COM_PassAsArg;
  586. Procedure TestClassInterface_COM_PassToUntypedParam;
  587. Procedure TestClassInterface_COM_FunctionInExpr;
  588. Procedure TestClassInterface_COM_Property;
  589. Procedure TestClassInterface_COM_IntfProperty;
  590. Procedure TestClassInterface_COM_Delegation;
  591. Procedure TestClassInterface_COM_With;
  592. Procedure TestClassInterface_COM_ForIn;
  593. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  594. Procedure TestClassInterface_COM_RecordIntfFail;
  595. Procedure TestClassInterface_COM_UnitInitialization;
  596. Procedure TestClassInterface_GUID;
  597. Procedure TestClassInterface_GUIDProperty;
  598. // helpers
  599. Procedure TestClassHelper_ClassVar;
  600. Procedure TestClassHelper_Method_AccessInstanceFields;
  601. Procedure TestClassHelper_Method_Call;
  602. Procedure TestClassHelper_Method_Nested_Call;
  603. Procedure TestClassHelper_ClassMethod_Call;
  604. Procedure TestClassHelper_ClassOf;
  605. Procedure TestClassHelper_MethodRefObjFPC;
  606. Procedure TestClassHelper_Constructor;
  607. Procedure TestClassHelper_InheritedObjFPC;
  608. Procedure TestClassHelper_Property;
  609. Procedure TestClassHelper_Property_Array;
  610. Procedure TestClassHelper_Property_Array_Default;
  611. Procedure TestClassHelper_Property_Array_DefaultDefault;
  612. Procedure TestClassHelper_ClassProperty;
  613. Procedure TestClassHelper_ClassPropertyStatic;
  614. Procedure TestClassHelper_ClassProperty_Array;
  615. Procedure TestClassHelper_ForIn;
  616. Procedure TestClassHelper_PassProperty;
  617. Procedure TestExtClassHelper_ClassVar;
  618. Procedure TestExtClassHelper_Method_Call;
  619. Procedure TestRecordHelper_ClassVar;
  620. Procedure TestRecordHelper_Method_Call;
  621. Procedure TestRecordHelper_Constructor;
  622. Procedure TestTypeHelper_ClassVar;
  623. Procedure TestTypeHelper_PassResultElement;
  624. Procedure TestTypeHelper_PassArgs;
  625. Procedure TestTypeHelper_PassVarConst;
  626. Procedure TestTypeHelper_PassFuncResult;
  627. Procedure TestTypeHelper_PassPropertyField;
  628. Procedure TestTypeHelper_PassPropertyGetter;
  629. Procedure TestTypeHelper_PassClassPropertyField;
  630. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  631. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  632. Procedure TestTypeHelper_Property;
  633. Procedure TestTypeHelper_Property_Array;
  634. Procedure TestTypeHelper_ClassProperty;
  635. Procedure TestTypeHelper_ClassProperty_Array;
  636. Procedure TestTypeHelper_ClassMethod;
  637. Procedure TestTypeHelper_Constructor;
  638. Procedure TestTypeHelper_Word;
  639. Procedure TestTypeHelper_StringChar;
  640. Procedure TestTypeHelper_Array;
  641. Procedure TestTypeHelper_EnumType;
  642. Procedure TestTypeHelper_SetType;
  643. // proc types
  644. Procedure TestProcType;
  645. Procedure TestProcType_Arg;
  646. Procedure TestProcType_FunctionFPC;
  647. Procedure TestProcType_FunctionDelphi;
  648. Procedure TestProcType_ProcedureDelphi;
  649. Procedure TestProcType_AsParam;
  650. Procedure TestProcType_MethodFPC;
  651. Procedure TestProcType_MethodDelphi;
  652. Procedure TestProcType_PropertyFPC;
  653. Procedure TestProcType_PropertyDelphi;
  654. Procedure TestProcType_WithClassInstDoPropertyFPC;
  655. Procedure TestProcType_Nested;
  656. Procedure TestProcType_NestedOfObject;
  657. Procedure TestProcType_ReferenceToProc;
  658. Procedure TestProcType_ReferenceToMethod;
  659. Procedure TestProcType_Typecast;
  660. Procedure TestProcType_PassProcToUntyped;
  661. Procedure TestProcType_PassProcToArray;
  662. // pointer
  663. Procedure TestPointer;
  664. Procedure TestPointer_Proc;
  665. Procedure TestPointer_AssignRecordFail;
  666. Procedure TestPointer_AssignStaticArrayFail;
  667. Procedure TestPointer_TypeCastJSValueToPointer;
  668. Procedure TestPointer_NonRecordFail;
  669. Procedure TestPointer_AnonymousArgTypeFail;
  670. Procedure TestPointer_AnonymousVarTypeFail;
  671. Procedure TestPointer_AnonymousResultTypeFail;
  672. Procedure TestPointer_AddrOperatorFail;
  673. Procedure TestPointer_ArrayParamsFail;
  674. Procedure TestPointer_PointerAddFail;
  675. Procedure TestPointer_IncPointerFail;
  676. Procedure TestPointer_Record;
  677. Procedure TestPointer_RecordArg;
  678. // jsvalue
  679. Procedure TestJSValue_AssignToJSValue;
  680. Procedure TestJSValue_TypeCastToBaseType;
  681. Procedure TestJSValue_TypecastToJSValue;
  682. Procedure TestJSValue_Equal;
  683. Procedure TestJSValue_If;
  684. Procedure TestJSValue_Not;
  685. Procedure TestJSValue_Enum;
  686. Procedure TestJSValue_ClassInstance;
  687. Procedure TestJSValue_ClassOf;
  688. Procedure TestJSValue_ArrayOfJSValue;
  689. Procedure TestJSValue_ArrayLit;
  690. Procedure TestJSValue_Params;
  691. Procedure TestJSValue_UntypedParam;
  692. Procedure TestJSValue_FuncResultType;
  693. Procedure TestJSValue_ProcType_Assign;
  694. Procedure TestJSValue_ProcType_Equal;
  695. Procedure TestJSValue_ProcType_Param;
  696. Procedure TestJSValue_AssignToPointerFail;
  697. Procedure TestJSValue_OverloadDouble;
  698. Procedure TestJSValue_OverloadNativeInt;
  699. Procedure TestJSValue_OverloadWord;
  700. Procedure TestJSValue_OverloadString;
  701. Procedure TestJSValue_OverloadChar;
  702. Procedure TestJSValue_OverloadPointer;
  703. Procedure TestJSValue_ForIn;
  704. // RTTI
  705. Procedure TestRTTI_IntRange;
  706. Procedure TestRTTI_Double;
  707. Procedure TestRTTI_ProcType;
  708. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  709. Procedure TestRTTI_EnumAndSetType;
  710. Procedure TestRTTI_EnumRange;
  711. Procedure TestRTTI_AnonymousEnumType;
  712. Procedure TestRTTI_StaticArray;
  713. Procedure TestRTTI_DynArray;
  714. Procedure TestRTTI_ArrayNestedAnonymous;
  715. Procedure TestRTTI_PublishedMethodOverloadFail;
  716. Procedure TestRTTI_PublishedMethodExternalFail;
  717. Procedure TestRTTI_PublishedClassPropertyFail;
  718. Procedure TestRTTI_PublishedClassFieldFail;
  719. Procedure TestRTTI_PublishedFieldExternalFail;
  720. Procedure TestRTTI_Class_Field;
  721. Procedure TestRTTI_Class_Method;
  722. Procedure TestRTTI_Class_MethodArgFlags;
  723. Procedure TestRTTI_Class_Property;
  724. Procedure TestRTTI_Class_PropertyParams;
  725. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  726. Procedure TestRTTI_Class_OmitRTTI;
  727. Procedure TestRTTI_IndexModifier;
  728. Procedure TestRTTI_StoredModifier;
  729. Procedure TestRTTI_DefaultValue;
  730. Procedure TestRTTI_DefaultValueSet;
  731. Procedure TestRTTI_DefaultValueRangeType;
  732. Procedure TestRTTI_DefaultValueInherit;
  733. Procedure TestRTTI_OverrideMethod;
  734. Procedure TestRTTI_OverloadProperty;
  735. // ToDo: array argument
  736. Procedure TestRTTI_ClassForward;
  737. Procedure TestRTTI_ClassOf;
  738. Procedure TestRTTI_Record;
  739. Procedure TestRTTI_RecordAnonymousArray;
  740. Procedure TestRTTI_LocalTypes;
  741. Procedure TestRTTI_TypeInfo_BaseTypes;
  742. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  743. Procedure TestRTTI_TypeInfo_LocalFail;
  744. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  745. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  746. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  747. Procedure TestRTTI_TypeInfo_FunctionClassType;
  748. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  749. Procedure TestRTTI_Interface_Corba;
  750. Procedure TestRTTI_Interface_COM;
  751. Procedure TestRTTI_ClassHelper;
  752. // Resourcestring
  753. Procedure TestResourcestringProgram;
  754. Procedure TestResourcestringUnit;
  755. Procedure TestResourcestringImplementation;
  756. // Attributes
  757. Procedure TestAtributes_Ignore;
  758. // Assertions, checks
  759. procedure TestAssert;
  760. procedure TestAssert_SysUtils;
  761. procedure TestObjectChecks;
  762. procedure TestRangeChecks_AssignInt;
  763. procedure TestRangeChecks_AssignIntRange;
  764. procedure TestRangeChecks_AssignEnum;
  765. procedure TestRangeChecks_AssignEnumRange;
  766. procedure TestRangeChecks_AssignChar;
  767. procedure TestRangeChecks_AssignCharRange;
  768. procedure TestRangeChecks_ArrayIndex;
  769. procedure TestRangeChecks_ArrayOfRecIndex;
  770. procedure TestRangeChecks_StringIndex;
  771. procedure TestRangeChecks_TypecastInt;
  772. end;
  773. function LinesToStr(Args: array of const): string;
  774. function ExtractFileUnitName(aFilename: string): string;
  775. function JSToStr(El: TJSElement): string;
  776. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  777. implementation
  778. function LinesToStr(Args: array of const): string;
  779. var
  780. s: String;
  781. i: Integer;
  782. begin
  783. s:='';
  784. for i:=Low(Args) to High(Args) do
  785. case Args[i].VType of
  786. vtChar: s += Args[i].VChar+LineEnding;
  787. vtString: s += Args[i].VString^+LineEnding;
  788. vtPChar: s += Args[i].VPChar+LineEnding;
  789. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  790. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  791. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  792. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  793. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  794. end;
  795. Result:=s;
  796. end;
  797. function ExtractFileUnitName(aFilename: string): string;
  798. var
  799. p: Integer;
  800. begin
  801. Result:=ExtractFileName(aFilename);
  802. if Result='' then exit;
  803. for p:=length(Result) downto 1 do
  804. case Result[p] of
  805. '/','\': exit;
  806. '.':
  807. begin
  808. Delete(Result,p,length(Result));
  809. exit;
  810. end;
  811. end;
  812. end;
  813. function JSToStr(El: TJSElement): string;
  814. var
  815. aWriter: TBufferWriter;
  816. aJSWriter: TJSWriter;
  817. begin
  818. aJSWriter:=nil;
  819. aWriter:=TBufferWriter.Create(1000);
  820. try
  821. aJSWriter:=TJSWriter.Create(aWriter);
  822. aJSWriter.IndentSize:=2;
  823. aJSWriter.WriteJS(El);
  824. Result:=aWriter.AsString;
  825. finally
  826. aJSWriter.Free;
  827. aWriter.Free;
  828. end;
  829. end;
  830. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  831. // search diff, ignore changes in spaces
  832. const
  833. SpaceChars = [#9,#10,#13,' '];
  834. var
  835. ExpectedP, ActualP: PChar;
  836. function FindLineEnd(p: PChar): PChar;
  837. begin
  838. Result:=p;
  839. while not (Result^ in [#0,#10,#13]) do inc(Result);
  840. end;
  841. function FindLineStart(p, MinP: PChar): PChar;
  842. begin
  843. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  844. Result:=p;
  845. end;
  846. procedure SkipLineEnd(var p: PChar);
  847. begin
  848. if p^ in [#10,#13] then
  849. begin
  850. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  851. inc(p,2)
  852. else
  853. inc(p);
  854. end;
  855. end;
  856. procedure DiffFound;
  857. var
  858. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  859. ExpLine, ActLine: String;
  860. i, LineNo, DiffLineNo: Integer;
  861. begin
  862. writeln('Diff found "',Msg,'". Lines:');
  863. // write correct lines
  864. p:=PChar(Expected);
  865. LineNo:=0;
  866. DiffLineNo:=0;
  867. repeat
  868. StartPos:=p;
  869. while not (p^ in [#0,#10,#13]) do inc(p);
  870. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  871. SkipLineEnd(p);
  872. inc(LineNo);
  873. if (p<=ExpectedP) and (p^<>#0) then
  874. begin
  875. writeln('= ',ExpLine);
  876. end else begin
  877. // diff line
  878. if DiffLineNo=0 then DiffLineNo:=LineNo;
  879. // write actual line
  880. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  881. ActLineEndP:=FindLineEnd(ActualP);
  882. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  883. writeln('- ',ActLine);
  884. // write expected line
  885. writeln('+ ',ExpLine);
  886. // write empty line with pointer ^
  887. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  888. writeln('^');
  889. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  890. CheckSrcDiff:=false;
  891. // write up to three following actual lines to get some context
  892. for i:=1 to 3 do begin
  893. ActLineStartP:=ActLineEndP;
  894. SkipLineEnd(ActLineStartP);
  895. if ActLineStartP^=#0 then break;
  896. ActLineEndP:=FindLineEnd(ActLineStartP);
  897. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  898. writeln('~ ',ActLine);
  899. end;
  900. exit;
  901. end;
  902. until p^=#0;
  903. writeln('DiffFound Actual:-----------------------');
  904. writeln(Actual);
  905. writeln('DiffFound Expected:---------------------');
  906. writeln(Expected);
  907. writeln('DiffFound ------------------------------');
  908. Msg:='diff found, but lines are the same, internal error';
  909. CheckSrcDiff:=false;
  910. end;
  911. var
  912. IsSpaceNeeded: Boolean;
  913. LastChar, Quote: Char;
  914. begin
  915. Result:=true;
  916. Msg:='';
  917. if Expected='' then Expected:=' ';
  918. if Actual='' then Actual:=' ';
  919. ExpectedP:=PChar(Expected);
  920. ActualP:=PChar(Actual);
  921. repeat
  922. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  923. case ExpectedP^ of
  924. #0:
  925. begin
  926. // check that rest of Actual has only spaces
  927. while ActualP^ in SpaceChars do inc(ActualP);
  928. if ActualP^<>#0 then
  929. begin
  930. DiffFound;
  931. exit;
  932. end;
  933. exit(true);
  934. end;
  935. ' ',#9,#10,#13:
  936. begin
  937. // skip space in Expected
  938. IsSpaceNeeded:=false;
  939. if ExpectedP>PChar(Expected) then
  940. LastChar:=ExpectedP[-1]
  941. else
  942. LastChar:=#0;
  943. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  944. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  945. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  946. IsSpaceNeeded:=true;
  947. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  948. begin
  949. DiffFound;
  950. exit;
  951. end;
  952. while ActualP^ in SpaceChars do inc(ActualP);
  953. end;
  954. '''','"':
  955. begin
  956. while ActualP^ in SpaceChars do inc(ActualP);
  957. if ExpectedP^<>ActualP^ then
  958. begin
  959. DiffFound;
  960. exit;
  961. end;
  962. Quote:=ExpectedP^;
  963. repeat
  964. inc(ExpectedP);
  965. inc(ActualP);
  966. if ExpectedP^<>ActualP^ then
  967. begin
  968. DiffFound;
  969. exit;
  970. end;
  971. if (ExpectedP^ in [#0,#10,#13]) then
  972. break
  973. else if (ExpectedP^=Quote) then
  974. begin
  975. inc(ExpectedP);
  976. inc(ActualP);
  977. break;
  978. end;
  979. until false;
  980. end;
  981. else
  982. while ActualP^ in SpaceChars do inc(ActualP);
  983. if ExpectedP^<>ActualP^ then
  984. begin
  985. DiffFound;
  986. exit;
  987. end;
  988. inc(ExpectedP);
  989. inc(ActualP);
  990. end;
  991. until false;
  992. end;
  993. { TTestEnginePasResolver }
  994. destructor TTestEnginePasResolver.Destroy;
  995. begin
  996. FreeAndNil(FStreamResolver);
  997. FreeAndNil(FParser);
  998. FreeAndNil(FScanner);
  999. FreeAndNil(FStreamResolver);
  1000. if Module<>nil then
  1001. begin
  1002. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1003. FModule:=nil;
  1004. end;
  1005. inherited Destroy;
  1006. end;
  1007. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1008. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1009. begin
  1010. Result:=nil;
  1011. if InFilename<>'' then
  1012. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1013. if Assigned(OnFindUnit) then
  1014. Result:=OnFindUnit(AName);
  1015. if NameExpr=nil then ;
  1016. end;
  1017. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1018. begin
  1019. // do not parse recursively
  1020. // parse via the queue
  1021. if Section=nil then ;
  1022. end;
  1023. { TCustomTestModule }
  1024. function TCustomTestModule.GetMsgCount: integer;
  1025. begin
  1026. Result:=FHintMsgs.Count;
  1027. end;
  1028. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1029. begin
  1030. Result:=TTestHintMessage(FHintMsgs[Index]);
  1031. end;
  1032. function TCustomTestModule.GetResolverCount: integer;
  1033. begin
  1034. Result:=FModules.Count;
  1035. end;
  1036. function TCustomTestModule.GetResolvers(Index: integer
  1037. ): TTestEnginePasResolver;
  1038. begin
  1039. Result:=TTestEnginePasResolver(FModules[Index]);
  1040. end;
  1041. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1042. ): TPasModule;
  1043. var
  1044. DefNamespace: String;
  1045. begin
  1046. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1047. if (Pos('.',aUnitName)<1) then
  1048. begin
  1049. DefNamespace:=GetDefaultNamespace;
  1050. if DefNamespace<>'' then
  1051. begin
  1052. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1053. if Result<>nil then exit;
  1054. end;
  1055. end;
  1056. Result:=LoadUnit(aUnitName);
  1057. if Result<>nil then exit;
  1058. {$IFDEF VerbosePas2JS}
  1059. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1060. {$ENDIF}
  1061. Fail('can''t find unit "'+aUnitName+'"');
  1062. end;
  1063. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1064. var
  1065. aParser: TPasParser;
  1066. Item: TTestHintMessage;
  1067. begin
  1068. aParser:=Sender as TPasParser;
  1069. Item:=TTestHintMessage.Create;
  1070. Item.Id:=aParser.LastMsgNumber;
  1071. Item.MsgType:=aParser.LastMsgType;
  1072. Item.MsgNumber:=aParser.LastMsgNumber;
  1073. Item.Msg:=Msg;
  1074. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1075. {$IFDEF VerbosePas2JS}
  1076. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1077. {$ENDIF}
  1078. FHintMsgs.Add(Item);
  1079. end;
  1080. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1081. );
  1082. var
  1083. aResolver: TTestEnginePasResolver;
  1084. Item: TTestHintMessage;
  1085. begin
  1086. aResolver:=Sender as TTestEnginePasResolver;
  1087. Item:=TTestHintMessage.Create;
  1088. Item.Id:=aResolver.LastMsgId;
  1089. Item.MsgType:=aResolver.LastMsgType;
  1090. Item.MsgNumber:=aResolver.LastMsgNumber;
  1091. Item.Msg:=Msg;
  1092. Item.SourcePos:=aResolver.LastSourcePos;
  1093. {$IFDEF VerbosePas2JS}
  1094. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1095. {$ENDIF}
  1096. FHintMsgs.Add(Item);
  1097. end;
  1098. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1099. var
  1100. Item: TTestHintMessage;
  1101. aScanner: TPas2jsPasScanner;
  1102. begin
  1103. aScanner:=Sender as TPas2jsPasScanner;
  1104. Item:=TTestHintMessage.Create;
  1105. Item.Id:=aScanner.LastMsgNumber;
  1106. Item.MsgType:=aScanner.LastMsgType;
  1107. Item.MsgNumber:=aScanner.LastMsgNumber;
  1108. Item.Msg:=Msg;
  1109. Item.SourcePos:=aScanner.CurSourcePos;
  1110. {$IFDEF VerbosePas2JS}
  1111. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1112. {$ENDIF}
  1113. FHintMsgs.Add(Item);
  1114. end;
  1115. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1116. var
  1117. i: Integer;
  1118. CurEngine: TTestEnginePasResolver;
  1119. CurUnitName: String;
  1120. begin
  1121. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1122. Result:=nil;
  1123. if (Module.ClassType=TPasModule)
  1124. and (CompareText(Module.Name,aUnitName)=0) then
  1125. exit(Module);
  1126. for i:=0 to ResolverCount-1 do
  1127. begin
  1128. CurEngine:=Resolvers[i];
  1129. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1130. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1131. if CompareText(aUnitName,CurUnitName)=0 then
  1132. begin
  1133. Result:=CurEngine.Module;
  1134. if Result<>nil then exit;
  1135. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1136. FileResolver.FindSourceFile(aUnitName);
  1137. CurEngine.StreamResolver:=TStreamResolver.Create;
  1138. CurEngine.StreamResolver.OwnsStreams:=True;
  1139. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1140. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1141. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1142. InitScanner(CurEngine.Scanner);
  1143. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1144. CurEngine.Parser.Options:=po_tcmodules;
  1145. if CompareText(CurUnitName,'System')=0 then
  1146. CurEngine.Parser.ImplicitUses.Clear;
  1147. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1148. try
  1149. CurEngine.Parser.NextToken;
  1150. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1151. except
  1152. on E: Exception do
  1153. HandleException(E);
  1154. end;
  1155. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1156. Result:=CurEngine.Module;
  1157. exit;
  1158. end;
  1159. end;
  1160. end;
  1161. procedure TCustomTestModule.SetUp;
  1162. begin
  1163. {$IFDEF EnablePasTreeGlobalRefCount}
  1164. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1165. {$ENDIF}
  1166. if FModules<>nil then
  1167. begin
  1168. writeln('TCustomTestModule.SetUp FModules<>nil');
  1169. Halt;
  1170. end;
  1171. inherited SetUp;
  1172. FSkipTests:=false;
  1173. FSource:=TStringList.Create;
  1174. FModules:=TObjectList.Create(true);
  1175. FFilename:='test1.pp';
  1176. FFileResolver:=TStreamResolver.Create;
  1177. FFileResolver.OwnsStreams:=True;
  1178. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1179. InitScanner(FScanner);
  1180. FEngine:=AddModule(Filename);
  1181. FEngine.Scanner:=FScanner;
  1182. FScanner.Resolver:=FEngine;
  1183. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1184. FParser.OnLog:=@OnParserLog;
  1185. FEngine.Parser:=FParser;
  1186. Parser.Options:=po_tcmodules;
  1187. FModule:=Nil;
  1188. FConverter:=CreateConverter;
  1189. FExpectedErrorClass:=nil;
  1190. end;
  1191. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1192. begin
  1193. Result:=TPasToJSConverter.Create;
  1194. Result.Options:=co_tcmodules;
  1195. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1196. end;
  1197. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1198. begin
  1199. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1200. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1201. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1202. aScanner.AllowedBoolSwitches:=msAllPas2jsBoolSwitches;
  1203. aScanner.ReadOnlyBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly;
  1204. aScanner.CurrentBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1205. aScanner.OnLog:=@OnScannerLog;
  1206. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1207. end;
  1208. procedure TCustomTestModule.TearDown;
  1209. {$IFDEF CheckPasTreeRefCount}
  1210. var
  1211. El: TPasElement;
  1212. {$ENDIF}
  1213. var
  1214. i: Integer;
  1215. CurModule: TPasModule;
  1216. begin
  1217. FHintMsgs.Clear;
  1218. FHintMsgsGood.Clear;
  1219. FSkipTests:=false;
  1220. FJSRegModuleCall:=nil;
  1221. FJSModuleCallArgs:=nil;
  1222. FJSImplentationUses:=nil;
  1223. FJSInterfaceUses:=nil;
  1224. FJSModuleSrc:=nil;
  1225. FJSInitBody:=nil;
  1226. FreeAndNil(FJSSource);
  1227. FreeAndNil(FJSModule);
  1228. FreeAndNil(FConverter);
  1229. Engine.Clear;
  1230. FreeAndNil(FSource);
  1231. FreeAndNil(FFileResolver);
  1232. if FModules<>nil then
  1233. begin
  1234. for i:=0 to FModules.Count-1 do
  1235. begin
  1236. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1237. if CurModule=nil then continue;
  1238. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1239. CurModule.ReleaseUsedUnits;
  1240. end;
  1241. if FModule<>nil then
  1242. FModule.ReleaseUsedUnits;
  1243. for i:=0 to FModules.Count-1 do
  1244. begin
  1245. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1246. if CurModule=nil then continue;
  1247. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1248. end;
  1249. FreeAndNil(FModules);
  1250. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1251. FEngine:=nil;
  1252. end;
  1253. inherited TearDown;
  1254. {$IFDEF EnablePasTreeGlobalRefCount}
  1255. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1256. begin
  1257. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1258. {$IFDEF CheckPasTreeRefCount}
  1259. El:=TPasElement.FirstRefEl;
  1260. while El<>nil do
  1261. begin
  1262. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1263. for i:=0 to El.RefIds.Count-1 do
  1264. writeln(' ',El.RefIds[i]);
  1265. El:=El.NextRefEl;
  1266. end;
  1267. {$ENDIF}
  1268. Halt;
  1269. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1270. end;
  1271. {$ENDIF}
  1272. end;
  1273. procedure TCustomTestModule.Add(Line: string);
  1274. begin
  1275. Source.Add(Line);
  1276. end;
  1277. procedure TCustomTestModule.Add(const Lines: array of string);
  1278. var
  1279. i: Integer;
  1280. begin
  1281. for i:=low(Lines) to high(Lines) do
  1282. Add(Lines[i]);
  1283. end;
  1284. procedure TCustomTestModule.StartParsing;
  1285. var
  1286. Src: String;
  1287. begin
  1288. Src:=Source.Text;
  1289. FEngine.Source:=Src;
  1290. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1291. Scanner.OpenFile(FileName);
  1292. Writeln('// Test : ',Self.TestName);
  1293. Writeln(Src);
  1294. end;
  1295. procedure TCustomTestModule.ParseModuleQueue;
  1296. var
  1297. i: Integer;
  1298. CurResolver: TTestEnginePasResolver;
  1299. Found: Boolean;
  1300. Section: TPasSection;
  1301. begin
  1302. // parse til exception or all modules finished
  1303. while not SkipTests do
  1304. begin
  1305. Found:=false;
  1306. for i:=0 to ResolverCount-1 do
  1307. begin
  1308. CurResolver:=Resolvers[i];
  1309. if CurResolver.CurrentParser=nil then continue;
  1310. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1311. continue;
  1312. CurResolver.Parser.ParseContinue;
  1313. Found:=true;
  1314. break;
  1315. end;
  1316. if not Found then break;
  1317. end;
  1318. for i:=0 to ResolverCount-1 do
  1319. begin
  1320. CurResolver:=Resolvers[i];
  1321. if CurResolver.Parser=nil then
  1322. begin
  1323. if CurResolver.CurrentParser<>nil then
  1324. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1325. continue;
  1326. end;
  1327. if CurResolver.Parser.CurModule<>nil then
  1328. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1329. end;
  1330. end;
  1331. procedure TCustomTestModule.ParseModule;
  1332. begin
  1333. if SkipTests then exit;
  1334. FFirstPasStatement:=nil;
  1335. try
  1336. StartParsing;
  1337. Parser.ParseMain(FModule);
  1338. ParseModuleQueue;
  1339. except
  1340. on E: Exception do
  1341. HandleException(E);
  1342. end;
  1343. if SkipTests then exit;
  1344. AssertNotNull('Module resulted in Module',Module);
  1345. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1346. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1347. end;
  1348. procedure TCustomTestModule.ParseProgram;
  1349. begin
  1350. if SkipTests then exit;
  1351. ParseModule;
  1352. if SkipTests then exit;
  1353. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1354. FPasProgram:=TPasProgram(Module);
  1355. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1356. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1357. if (PasProgram.InitializationSection.Elements.Count>0) then
  1358. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1359. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1360. end;
  1361. procedure TCustomTestModule.ParseUnit;
  1362. begin
  1363. if SkipTests then exit;
  1364. ParseModule;
  1365. if SkipTests then exit;
  1366. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1367. AssertNotNull('Has interface section',Module.InterfaceSection);
  1368. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1369. if (Module.InitializationSection<>nil)
  1370. and (Module.InitializationSection.Elements.Count>0)
  1371. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1372. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1373. end;
  1374. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1375. ): TTestEnginePasResolver;
  1376. var
  1377. i: Integer;
  1378. begin
  1379. for i:=0 to ResolverCount-1 do
  1380. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1381. exit(Resolvers[i]);
  1382. Result:=nil;
  1383. end;
  1384. function TCustomTestModule.AddModule(aFilename: string
  1385. ): TTestEnginePasResolver;
  1386. begin
  1387. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1388. if FindModuleWithFilename(aFilename)<>nil then
  1389. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1390. Result:=TTestEnginePasResolver.Create;
  1391. Result.Filename:=aFilename;
  1392. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1393. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1394. Result.OnLog:=@OnPasResolverLog;
  1395. FModules.Add(Result);
  1396. end;
  1397. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1398. ): TTestEnginePasResolver;
  1399. begin
  1400. Result:=AddModule(aFilename);
  1401. Result.Source:=Src;
  1402. end;
  1403. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1404. ImplementationSrc: string): TTestEnginePasResolver;
  1405. var
  1406. Src: String;
  1407. begin
  1408. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1409. Src+=LineEnding;
  1410. Src+='interface'+LineEnding;
  1411. Src+=LineEnding;
  1412. Src+=InterfaceSrc;
  1413. Src+='implementation'+LineEnding;
  1414. Src+=LineEnding;
  1415. Src+=ImplementationSrc;
  1416. Src+='end.'+LineEnding;
  1417. Result:=AddModuleWithSrc(aFilename,Src);
  1418. end;
  1419. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1420. var
  1421. Intf, Impl: TStringList;
  1422. begin
  1423. Intf:=TStringList.Create;
  1424. // interface
  1425. if supTVarRec in Parts then
  1426. Intf.Add('{$modeswitch externalclass}');
  1427. Intf.Add('type');
  1428. Intf.Add(' integer=longint;');
  1429. Intf.Add(' sizeint=nativeint;');
  1430. //'const',
  1431. //' LineEnding = #10;',
  1432. //' DirectorySeparator = ''/'';',
  1433. //' DriveSeparator = '''';',
  1434. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1435. //' AllowDriveSeparators : set of char = [];',
  1436. if supTObject in Parts then
  1437. Intf.AddStrings([
  1438. 'type',
  1439. ' TClass = class of TObject;',
  1440. ' TObject = class',
  1441. ' constructor Create;',
  1442. ' destructor Destroy; virtual;',
  1443. ' class function ClassType: TClass; assembler;',
  1444. ' class function ClassName: String; assembler;',
  1445. ' class function ClassNameIs(const Name: string): boolean;',
  1446. ' class function ClassParent: TClass; assembler;',
  1447. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1448. ' class function UnitName: String; assembler;',
  1449. ' procedure AfterConstruction; virtual;',
  1450. ' procedure BeforeDestruction;virtual;',
  1451. ' function Equals(Obj: TObject): boolean; virtual;',
  1452. ' function ToString: String; virtual;',
  1453. ' end;']);
  1454. if supTVarRec in Parts then
  1455. Intf.AddStrings([
  1456. 'const',
  1457. ' vtInteger = 0;',
  1458. ' vtBoolean = 1;',
  1459. ' vtJSValue = 19;',
  1460. 'type',
  1461. ' PVarRec = ^TVarRec;',
  1462. ' TVarRec = record',
  1463. ' VType : byte;',
  1464. ' VJSValue: JSValue;',
  1465. ' vInteger: longint external name ''VJSValue'';',
  1466. ' vBoolean: boolean external name ''VJSValue'';',
  1467. ' end;',
  1468. ' TVarRecArray = array of TVarRec;',
  1469. 'function VarRecs: TVarRecArray; varargs;',
  1470. '']);
  1471. Intf.Add('var');
  1472. Intf.Add(' ExitCode: Longint = 0;');
  1473. // implementation
  1474. Impl:=TStringList.Create;
  1475. if supTObject in Parts then
  1476. Impl.AddStrings([
  1477. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1478. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1479. 'asm',
  1480. 'end;',
  1481. 'constructor TObject.Create; begin end;',
  1482. 'destructor TObject.Destroy; begin end;',
  1483. 'class function TObject.ClassType: TClass; assembler;',
  1484. 'asm',
  1485. 'end;',
  1486. 'class function TObject.ClassName: String; assembler;',
  1487. 'asm',
  1488. 'end;',
  1489. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1490. 'begin',
  1491. ' Result:=SameText(Name,ClassName);',
  1492. 'end;',
  1493. 'class function TObject.ClassParent: TClass; assembler;',
  1494. 'asm',
  1495. 'end;',
  1496. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1497. 'asm',
  1498. 'end;',
  1499. 'class function TObject.UnitName: String; assembler;',
  1500. 'asm',
  1501. 'end;',
  1502. 'procedure TObject.AfterConstruction; begin end;',
  1503. 'procedure TObject.BeforeDestruction; begin end;',
  1504. 'function TObject.Equals(Obj: TObject): boolean;',
  1505. 'begin',
  1506. ' Result:=Obj=Self;',
  1507. 'end;',
  1508. 'function TObject.ToString: String;',
  1509. 'begin',
  1510. ' Result:=ClassName;',
  1511. 'end;'
  1512. ]);
  1513. if supTVarRec in Parts then
  1514. Impl.AddStrings([
  1515. 'function VarRecs: TVarRecArray; varargs;',
  1516. 'var',
  1517. ' v: PVarRec;',
  1518. 'begin',
  1519. ' v^.VType:=1;',
  1520. ' v^.VJSValue:=2;',
  1521. 'end;',
  1522. '']);
  1523. try
  1524. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1525. finally
  1526. Intf.Free;
  1527. Impl.Free;
  1528. end;
  1529. end;
  1530. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1531. SystemUnitParts: TSystemUnitParts);
  1532. begin
  1533. if NeedSystemUnit then
  1534. AddSystemUnit(SystemUnitParts)
  1535. else
  1536. Parser.ImplicitUses.Clear;
  1537. Add('program '+ExtractFileUnitName(Filename)+';');
  1538. Add('');
  1539. end;
  1540. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1541. SystemUnitParts: TSystemUnitParts);
  1542. begin
  1543. if NeedSystemUnit then
  1544. AddSystemUnit(SystemUnitParts)
  1545. else
  1546. Parser.ImplicitUses.Clear;
  1547. Add('unit Test1;');
  1548. Add('');
  1549. end;
  1550. procedure TCustomTestModule.ConvertModule;
  1551. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1552. out UsesLit: TJSArrayLiteral);
  1553. var
  1554. i: Integer;
  1555. Item: TJSElement;
  1556. Lit: TJSLiteral;
  1557. begin
  1558. UsesLit:=nil;
  1559. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1560. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1561. exit; // null is ok
  1562. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1563. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1564. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1565. begin
  1566. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1567. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1568. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1569. Lit:=TJSLiteral(Item);
  1570. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1571. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1572. end;
  1573. end;
  1574. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1575. out Src: TJSSourceElements);
  1576. var
  1577. FunDecl: TJSFunctionDeclarationStatement;
  1578. FunDef: TJSFuncDef;
  1579. FunBody: TJSFunctionBody;
  1580. begin
  1581. Src:=nil;
  1582. AssertNotNull(ParamName,Arg.Expr);
  1583. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1584. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1585. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1586. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1587. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1588. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1589. AssertNotNull(ParamName+' body',FunDef.Body);
  1590. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1591. FunBody:=FunDef.Body as TJSFunctionBody;
  1592. AssertNotNull(ParamName+' body.A',FunBody.A);
  1593. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1594. Src:=FunBody.A as TJSSourceElements;
  1595. end;
  1596. var
  1597. ModuleNameExpr: TJSLiteral;
  1598. InitFunction: TJSFunctionDeclarationStatement;
  1599. InitAssign: TJSSimpleAssignStatement;
  1600. InitName: String;
  1601. LastNode: TJSElement;
  1602. Arg: TJSArrayLiteralElement;
  1603. begin
  1604. if SkipTests then exit;
  1605. try
  1606. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1607. except
  1608. on E: Exception do
  1609. HandleException(E);
  1610. end;
  1611. if SkipTests then exit;
  1612. if ExpectedErrorClass<>nil then
  1613. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1614. FJSSource:=TStringList.Create;
  1615. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1616. {$IFDEF VerbosePas2JS}
  1617. writeln('TTestModule.ConvertModule JS:');
  1618. write(FJSSource.Text);
  1619. {$ENDIF}
  1620. // rtl.module(...
  1621. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1622. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1623. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1624. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1625. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1626. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1627. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1628. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1629. // parameter 'unitname'
  1630. if JSModuleCallArgs.Elements.Count<1 then
  1631. Fail('rtl.module first param unit missing');
  1632. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1633. AssertNotNull('module name param',Arg.Expr);
  1634. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1635. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1636. if Module is TPasProgram then
  1637. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1638. else
  1639. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1640. // main uses section
  1641. if JSModuleCallArgs.Elements.Count<2 then
  1642. Fail('rtl.module second param main uses missing');
  1643. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1644. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1645. // program/library/interface function()
  1646. if JSModuleCallArgs.Elements.Count<3 then
  1647. Fail('rtl.module third param intf-function missing');
  1648. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1649. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1650. // search for $mod.$init or $mod.$main - the last statement
  1651. if Module is TPasProgram then
  1652. begin
  1653. InitName:='$main';
  1654. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1655. end
  1656. else
  1657. InitName:='$init';
  1658. FJSInitBody:=nil;
  1659. if JSModuleSrc.Statements.Count>0 then
  1660. begin
  1661. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1662. if LastNode is TJSSimpleAssignStatement then
  1663. begin
  1664. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1665. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1666. begin
  1667. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1668. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1669. end
  1670. else if Module is TPasProgram then
  1671. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1672. end;
  1673. end;
  1674. // optional: implementation uses section
  1675. if JSModuleCallArgs.Elements.Count<4 then
  1676. exit;
  1677. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1678. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1679. // optional: implementation function()
  1680. if JSModuleCallArgs.Elements.Count<5 then
  1681. exit;
  1682. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1683. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1684. end;
  1685. procedure TCustomTestModule.ConvertProgram;
  1686. begin
  1687. Add('end.');
  1688. ParseProgram;
  1689. ConvertModule;
  1690. end;
  1691. procedure TCustomTestModule.ConvertUnit;
  1692. begin
  1693. Add('end.');
  1694. ParseUnit;
  1695. ConvertModule;
  1696. end;
  1697. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1698. begin
  1699. Result:=tcmodules.JSToStr(El);
  1700. end;
  1701. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1702. DottedName: string);
  1703. begin
  1704. if DottedName='' then
  1705. begin
  1706. AssertNull(Msg,El);
  1707. end
  1708. else
  1709. begin
  1710. AssertNotNull(Msg,El);
  1711. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1712. end;
  1713. end;
  1714. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1715. begin
  1716. if El=nil then
  1717. Result:=''
  1718. else if El is TJSPrimaryExpressionIdent then
  1719. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1720. else if El is TJSDotMemberExpression then
  1721. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1722. else
  1723. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1724. end;
  1725. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1726. InitStatements: string; ImplStatements: string);
  1727. var
  1728. ActualSrc, ExpectedSrc, InitName: String;
  1729. begin
  1730. ActualSrc:=JSToStr(JSModuleSrc);
  1731. ExpectedSrc:=
  1732. 'var $mod = this;'+LineEnding
  1733. +Statements;
  1734. if coUseStrict in Converter.Options then
  1735. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1736. if Module is TPasProgram then
  1737. InitName:='$main'
  1738. else
  1739. InitName:='$init';
  1740. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1741. ExpectedSrc:=ExpectedSrc+LineEnding
  1742. +'$mod.'+InitName+' = function () {'+LineEnding
  1743. +InitStatements
  1744. +'};'+LineEnding;
  1745. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1746. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1747. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1748. if (JSImplementationSrc<>nil) then
  1749. begin
  1750. ActualSrc:=JSToStr(JSImplementationSrc);
  1751. ExpectedSrc:=
  1752. 'var $mod = this;'+LineEnding
  1753. +'var $impl = $mod.$impl;'+LineEnding
  1754. +ImplStatements;
  1755. end
  1756. else
  1757. begin
  1758. ActualSrc:='';
  1759. ExpectedSrc:=ImplStatements;
  1760. end;
  1761. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1762. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1763. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1764. end;
  1765. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1766. // search diff, ignore changes in spaces
  1767. var
  1768. s: string;
  1769. begin
  1770. if CheckSrcDiff(Expected,Actual,s) then exit;
  1771. Fail(Msg+': '+s);
  1772. end;
  1773. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1774. var
  1775. aResolver: TTestEnginePasResolver;
  1776. aConverter: TPasToJSConverter;
  1777. aJSModule: TJSSourceElements;
  1778. ActualSrc: String;
  1779. begin
  1780. aResolver:=GetResolver(Filename);
  1781. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1782. {$IFDEF VerbosePas2JS}
  1783. writeln('CheckUnit '+Filename+' converting ...');
  1784. {$ENDIF}
  1785. aConverter:=CreateConverter;
  1786. aJSModule:=nil;
  1787. try
  1788. try
  1789. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1790. except
  1791. on E: Exception do
  1792. HandleException(E);
  1793. end;
  1794. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1795. {$IFDEF VerbosePas2JS}
  1796. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1797. write(aResolver.Source);
  1798. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1799. write(ActualSrc);
  1800. {$ENDIF}
  1801. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1802. finally
  1803. aJSModule.Free;
  1804. aConverter.Free;
  1805. end;
  1806. end;
  1807. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1808. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1809. var
  1810. i: Integer;
  1811. Item: TTestHintMessage;
  1812. Expected,Actual: string;
  1813. begin
  1814. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1815. for i:=0 to MsgCount-1 do
  1816. begin
  1817. Item:=Msgs[i];
  1818. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1819. if (Marker<>nil) then
  1820. begin
  1821. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1822. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1823. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1824. end;
  1825. // found
  1826. FHintMsgsGood.Add(Item);
  1827. str(Item.MsgType,Actual);
  1828. str(MsgType,Expected);
  1829. AssertEquals('MsgType',Expected,Actual);
  1830. exit;
  1831. end;
  1832. // needed message missing -> show emitted messages
  1833. WriteSources('',0,0);
  1834. for i:=0 to MsgCount-1 do
  1835. begin
  1836. Item:=Msgs[i];
  1837. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1838. ' ('+IntToStr(Item.MsgNumber),')');
  1839. if Marker<>nil then
  1840. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1841. writeln(' {',Item.Msg,'}');
  1842. end;
  1843. str(MsgType,Expected);
  1844. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1845. if Marker<>nil then
  1846. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1847. Actual:=Actual+' '+Msg;
  1848. Fail(Actual);
  1849. end;
  1850. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1851. );
  1852. var
  1853. i: Integer;
  1854. s, Txt: String;
  1855. Msg: TTestHintMessage;
  1856. begin
  1857. for i:=0 to MsgCount-1 do
  1858. begin
  1859. Msg:=Msgs[i];
  1860. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1861. s:='';
  1862. str(Msg.MsgType,s);
  1863. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1864. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1865. if WithSourcePos then
  1866. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1867. Txt:=Txt+' {'+Msg.Msg+'}';
  1868. Fail(Txt);
  1869. end;
  1870. end;
  1871. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1872. MsgNumber: integer);
  1873. begin
  1874. ExpectedErrorClass:=EScannerError;
  1875. ExpectedErrorMsg:=Msg;
  1876. ExpectedErrorNumber:=MsgNumber;
  1877. end;
  1878. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1879. MsgNumber: integer);
  1880. begin
  1881. ExpectedErrorClass:=EParserError;
  1882. ExpectedErrorMsg:=Msg;
  1883. ExpectedErrorNumber:=MsgNumber;
  1884. end;
  1885. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1886. MsgNumber: integer);
  1887. begin
  1888. ExpectedErrorClass:=EPasResolve;
  1889. ExpectedErrorMsg:=Msg;
  1890. ExpectedErrorNumber:=MsgNumber;
  1891. end;
  1892. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1893. MsgNumber: integer);
  1894. begin
  1895. ExpectedErrorClass:=EPas2JS;
  1896. ExpectedErrorMsg:=Msg;
  1897. ExpectedErrorNumber:=MsgNumber;
  1898. end;
  1899. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1900. var
  1901. MsgNumber: Integer;
  1902. Msg: String;
  1903. begin
  1904. Result:=false;
  1905. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1906. Msg:=E.Message;
  1907. if E is EPas2JS then
  1908. MsgNumber:=EPas2JS(E).MsgNumber
  1909. else if E is EPasResolve then
  1910. MsgNumber:=EPasResolve(E).MsgNumber
  1911. else if E is EParserError then
  1912. MsgNumber:=Parser.LastMsgNumber
  1913. else if E is EScannerError then
  1914. begin
  1915. MsgNumber:=Scanner.LastMsgNumber;
  1916. Msg:=Scanner.LastMsg;
  1917. end
  1918. else
  1919. MsgNumber:=0;
  1920. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  1921. if Result then
  1922. SkipTests:=true;
  1923. end;
  1924. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1925. begin
  1926. if IsErrorExpected(E) then exit;
  1927. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  1928. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  1929. +' '+Scanner.CurFilename
  1930. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  1931. FailException(E);
  1932. end;
  1933. procedure TCustomTestModule.HandleParserError(E: EParserError);
  1934. begin
  1935. if IsErrorExpected(E) then exit;
  1936. WriteSources(E.Filename,E.Row,E.Column);
  1937. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  1938. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  1939. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  1940. );
  1941. FailException(E);
  1942. end;
  1943. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  1944. var
  1945. P: TPasSourcePos;
  1946. begin
  1947. if IsErrorExpected(E) then exit;
  1948. P:=E.SourcePos;
  1949. WriteSources(P.FileName,P.Row,P.Column);
  1950. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  1951. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  1952. FailException(E);
  1953. end;
  1954. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  1955. var
  1956. Row, Col: integer;
  1957. begin
  1958. if IsErrorExpected(E) then exit;
  1959. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  1960. WriteSources(E.PasElement.SourceFilename,Row,Col);
  1961. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  1962. +' '+E.PasElement.SourceFilename
  1963. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  1964. FailException(E);
  1965. end;
  1966. procedure TCustomTestModule.HandleException(E: Exception);
  1967. begin
  1968. if E is EScannerError then
  1969. HandleScannerError(EScannerError(E))
  1970. else if E is EParserError then
  1971. HandleParserError(EParserError(E))
  1972. else if E is EPasResolve then
  1973. HandlePasResolveError(EPasResolve(E))
  1974. else if E is EPas2JS then
  1975. HandlePas2JSError(EPas2JS(E))
  1976. else
  1977. begin
  1978. if IsErrorExpected(E) then exit;
  1979. if not (E is EAssertionFailedError) then
  1980. begin
  1981. WriteSources('',0,0);
  1982. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  1983. end;
  1984. FailException(E);
  1985. end;
  1986. end;
  1987. procedure TCustomTestModule.FailException(E: Exception);
  1988. var
  1989. MsgNumber: Integer;
  1990. begin
  1991. if ExpectedErrorClass<>nil then
  1992. begin
  1993. if FExpectedErrorClass=E.ClassType then
  1994. begin
  1995. if E is EPas2JS then
  1996. MsgNumber:=EPas2JS(E).MsgNumber
  1997. else if E is EPasResolve then
  1998. MsgNumber:=EPasResolve(E).MsgNumber
  1999. else if E is EParserError then
  2000. MsgNumber:=Parser.LastMsgNumber
  2001. else if E is EScannerError then
  2002. MsgNumber:=Scanner.LastMsgNumber
  2003. else
  2004. MsgNumber:=0;
  2005. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2006. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2007. ExpectedErrorNumber,MsgNumber);
  2008. end else begin
  2009. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2010. end;
  2011. end;
  2012. Fail(E.Message);
  2013. end;
  2014. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2015. aCol: integer);
  2016. var
  2017. IsSrc: Boolean;
  2018. i, j: Integer;
  2019. SrcLines: TStringList;
  2020. Line: string;
  2021. aModule: TTestEnginePasResolver;
  2022. begin
  2023. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2024. for i:=0 to ResolverCount-1 do
  2025. begin
  2026. aModule:=Resolvers[i];
  2027. SrcLines:=TStringList.Create;
  2028. try
  2029. SrcLines.Text:=aModule.Source;
  2030. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2031. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2032. for j:=1 to SrcLines.Count do
  2033. begin
  2034. Line:=SrcLines[j-1];
  2035. if IsSrc and (j=aRow) then
  2036. begin
  2037. write('*');
  2038. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2039. end;
  2040. writeln(Format('%:4d: ',[j]),Line);
  2041. end;
  2042. finally
  2043. SrcLines.Free;
  2044. end;
  2045. end;
  2046. end;
  2047. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2048. var
  2049. i: Integer;
  2050. begin
  2051. for i:=0 to ResolverCount-1 do
  2052. if Filename=Resolvers[i].Filename then exit(i);
  2053. Result:=-1;
  2054. end;
  2055. function TCustomTestModule.GetResolver(const Filename: string
  2056. ): TTestEnginePasResolver;
  2057. var
  2058. i: Integer;
  2059. begin
  2060. i:=IndexOfResolver(Filename);
  2061. if i<0 then exit(nil);
  2062. Result:=Resolvers[i];
  2063. end;
  2064. function TCustomTestModule.GetDefaultNamespace: string;
  2065. var
  2066. C: TClass;
  2067. begin
  2068. Result:='';
  2069. if FModule=nil then exit;
  2070. C:=FModule.ClassType;
  2071. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2072. Result:=Engine.DefaultNameSpace;
  2073. end;
  2074. constructor TCustomTestModule.Create;
  2075. begin
  2076. inherited Create;
  2077. FHintMsgs:=TObjectList.Create(true);
  2078. FHintMsgsGood:=TFPList.Create;
  2079. end;
  2080. destructor TCustomTestModule.Destroy;
  2081. begin
  2082. FreeAndNil(FHintMsgs);
  2083. FreeAndNil(FHintMsgsGood);
  2084. inherited Destroy;
  2085. end;
  2086. { TTestModule }
  2087. procedure TTestModule.TestReservedWords;
  2088. var
  2089. i: integer;
  2090. begin
  2091. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2092. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2093. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2094. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2095. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2096. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2097. end;
  2098. procedure TTestModule.TestEmptyProgram;
  2099. begin
  2100. StartProgram(false);
  2101. Add('begin');
  2102. ConvertProgram;
  2103. CheckSource('TestEmptyProgram','','');
  2104. end;
  2105. procedure TTestModule.TestEmptyProgramUseStrict;
  2106. begin
  2107. Converter.Options:=Converter.Options+[coUseStrict];
  2108. StartProgram(false);
  2109. Add('begin');
  2110. ConvertProgram;
  2111. CheckSource('TestEmptyProgramUseStrict','','');
  2112. end;
  2113. procedure TTestModule.TestEmptyUnit;
  2114. begin
  2115. StartUnit(false);
  2116. Add('interface');
  2117. Add('implementation');
  2118. ConvertUnit;
  2119. CheckSource('TestEmptyUnit',
  2120. LinesToStr([
  2121. ]),
  2122. '');
  2123. end;
  2124. procedure TTestModule.TestEmptyUnitUseStrict;
  2125. begin
  2126. Converter.Options:=Converter.Options+[coUseStrict];
  2127. StartUnit(false);
  2128. Add('interface');
  2129. Add('implementation');
  2130. ConvertUnit;
  2131. CheckSource('TestEmptyUnitUseStrict',
  2132. LinesToStr([
  2133. ''
  2134. ]),
  2135. '');
  2136. end;
  2137. procedure TTestModule.TestDottedUnitNames;
  2138. begin
  2139. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2140. LinesToStr([
  2141. 'var iV: longint;'
  2142. ]),
  2143. '');
  2144. FFilename:='ns1.test1.pp';
  2145. StartProgram(true);
  2146. Add('uses unIt2;');
  2147. Add('implementation');
  2148. Add('var');
  2149. Add(' i: longint;');
  2150. Add('begin');
  2151. Add(' i:=iv;');
  2152. Add(' i:=uNit2.iv;');
  2153. Add(' i:=Ns1.TEst1.i;');
  2154. ConvertProgram;
  2155. CheckSource('TestDottedUnitNames',
  2156. LinesToStr([
  2157. 'this.i = 0;',
  2158. '']),
  2159. LinesToStr([ // this.$init
  2160. '$mod.i = pas["NS1.Unit2"].iV;',
  2161. '$mod.i = pas["NS1.Unit2"].iV;',
  2162. '$mod.i = $mod.i;',
  2163. '']) );
  2164. end;
  2165. procedure TTestModule.TestDottedUnitNameImpl;
  2166. begin
  2167. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2168. LinesToStr([
  2169. 'type',
  2170. ' TObject = class end;',
  2171. ' TTestA = class',
  2172. ' end;'
  2173. ]),
  2174. LinesToStr(['uses TEST.UnitB;'])
  2175. );
  2176. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2177. LinesToStr([
  2178. 'uses TEST.UnitA;',
  2179. 'type TTestB = class(TTestA);'
  2180. ]),
  2181. ''
  2182. );
  2183. StartProgram(true);
  2184. Add('uses TEST.UnitA;');
  2185. Add('begin');
  2186. ConvertProgram;
  2187. CheckSource('TestDottedUnitNameImpl',
  2188. LinesToStr([
  2189. '']),
  2190. LinesToStr([ // this.$init
  2191. '']) );
  2192. CheckUnit('TEST.UnitA.pas',
  2193. LinesToStr([
  2194. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2195. ' var $mod = this;',
  2196. ' rtl.createClass($mod, "TObject", null, function () {',
  2197. ' this.$init = function () {',
  2198. ' };',
  2199. ' this.$final = function () {',
  2200. ' };',
  2201. ' });',
  2202. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2203. ' });',
  2204. '}, ["TEST.UnitB"]);'
  2205. ]));
  2206. CheckUnit('TEST.UnitB.pas',
  2207. LinesToStr([
  2208. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2209. ' var $mod = this;',
  2210. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2211. ' });',
  2212. '});'
  2213. ]));
  2214. end;
  2215. procedure TTestModule.TestDottedUnitExpr;
  2216. begin
  2217. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2218. LinesToStr([
  2219. 'procedure DoIt;'
  2220. ]),
  2221. 'procedure DoIt; begin end;');
  2222. FFilename:='Ns1.SubNs1.Test1.pp';
  2223. StartProgram(true);
  2224. Add('uses Ns2.sUbnS2.unIt2;');
  2225. Add('implementation');
  2226. Add('var');
  2227. Add(' i: longint;');
  2228. Add('begin');
  2229. Add(' ns2.subns2.unit2.doit;');
  2230. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2231. ConvertProgram;
  2232. CheckSource('TestDottedUnitExpr',
  2233. LinesToStr([
  2234. 'this.i = 0;',
  2235. '']),
  2236. LinesToStr([ // this.$init
  2237. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2238. '$mod.i = $mod.i;',
  2239. '']) );
  2240. end;
  2241. procedure TTestModule.Test_ModeFPCFail;
  2242. begin
  2243. StartProgram(false);
  2244. Add('{$mode FPC}');
  2245. Add('begin');
  2246. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2247. ConvertProgram;
  2248. end;
  2249. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2250. begin
  2251. StartProgram(false);
  2252. Add('{$modeswitch cblocks-}');
  2253. Add('begin');
  2254. SetExpectedScannerError('Invalid mode switch: "cblocks-"',nErrInvalidModeSwitch);
  2255. ConvertProgram;
  2256. end;
  2257. procedure TTestModule.TestUnit_UseSystem;
  2258. begin
  2259. StartUnit(true);
  2260. Add([
  2261. 'interface',
  2262. 'var i: integer;',
  2263. 'implementation']);
  2264. ConvertUnit;
  2265. CheckSource('TestUnit_UseSystem',
  2266. LinesToStr([
  2267. 'this.i = 0;',
  2268. '']),
  2269. LinesToStr([
  2270. '']) );
  2271. end;
  2272. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2273. begin
  2274. AddModuleWithIntfImplSrc('unit1.pp',
  2275. LinesToStr([
  2276. 'type number = longint;']),
  2277. LinesToStr([
  2278. 'uses test1;',
  2279. 'procedure DoIt;',
  2280. 'begin',
  2281. ' i:=3;',
  2282. 'end;']));
  2283. StartUnit(true);
  2284. Add([
  2285. 'interface',
  2286. 'uses unit1;',
  2287. 'var i: number;',
  2288. 'implementation']);
  2289. ConvertUnit;
  2290. CheckSource('TestUnit_Intf1Impl2Intf1',
  2291. LinesToStr([
  2292. 'this.i = 0;',
  2293. '']),
  2294. LinesToStr([
  2295. '']) );
  2296. end;
  2297. procedure TTestModule.TestIncludeVersion;
  2298. begin
  2299. StartProgram(false);
  2300. Add([
  2301. 'var',
  2302. ' s: string;',
  2303. ' i: word;',
  2304. 'begin',
  2305. ' s:={$I %line%};',
  2306. ' i:={$I %linenum%};',
  2307. ' s:={$I %currentroutine%};',
  2308. ' s:={$I %pas2jsversion%};',
  2309. ' s:={$I %pas2jstarget%};',
  2310. ' s:={$I %pas2jstargetos%};',
  2311. ' s:={$I %pas2jstargetcpu%};',
  2312. ' s:={$I %file%};',
  2313. '']);
  2314. ConvertProgram;
  2315. CheckSource('TestIncludeVersion',
  2316. LinesToStr([
  2317. 'this.s="";',
  2318. 'this.i = 0;']),
  2319. LinesToStr([
  2320. '$mod.s = "7";',
  2321. '$mod.i = 8;',
  2322. '$mod.s = "<anonymous>";',
  2323. '$mod.s = "Comp.Ver.tcmodules";',
  2324. '$mod.s = "Browser";',
  2325. '$mod.s = "Browser";',
  2326. '$mod.s = "ECMAScript5";',
  2327. '$mod.s = "test1.pp";',
  2328. '']));
  2329. end;
  2330. procedure TTestModule.TestVarInt;
  2331. begin
  2332. StartProgram(false);
  2333. Add('var MyI: longint;');
  2334. Add('begin');
  2335. ConvertProgram;
  2336. CheckSource('TestVarInt','this.MyI=0;','');
  2337. end;
  2338. procedure TTestModule.TestVarBaseTypes;
  2339. begin
  2340. StartProgram(false);
  2341. Add('var');
  2342. Add(' i: longint;');
  2343. Add(' s: string;');
  2344. Add(' c: char;');
  2345. Add(' b: boolean;');
  2346. Add(' d: double;');
  2347. Add(' i2: longint = 3;');
  2348. Add(' s2: string = ''foo'';');
  2349. Add(' c2: char = ''4'';');
  2350. Add(' b2: boolean = true;');
  2351. Add(' d2: double = 5.6;');
  2352. Add(' i3: longint = $707;');
  2353. Add(' i4: nativeint = 4503599627370495;');
  2354. Add(' i5: nativeint = -4503599627370495-1;');
  2355. Add(' i6: nativeint = $fffffffffffff;');
  2356. Add(' i7: nativeint = -$fffffffffffff-1;');
  2357. Add(' i8: byte = 00;');
  2358. Add(' u8: nativeuint = $fffffffffffff;');
  2359. Add(' u9: nativeuint = $0000000000000;');
  2360. Add(' u10: nativeuint = $00ff00;');
  2361. Add('begin');
  2362. ConvertProgram;
  2363. CheckSource('TestVarBaseTypes',
  2364. LinesToStr([
  2365. 'this.i = 0;',
  2366. 'this.s = "";',
  2367. 'this.c = "";',
  2368. 'this.b = false;',
  2369. 'this.d = 0.0;',
  2370. 'this.i2 = 3;',
  2371. 'this.s2 = "foo";',
  2372. 'this.c2 = "4";',
  2373. 'this.b2 = true;',
  2374. 'this.d2 = 5.6;',
  2375. 'this.i3 = 0x707;',
  2376. 'this.i4 = 4503599627370495;',
  2377. 'this.i5 = -4503599627370495-1;',
  2378. 'this.i6 = 0xfffffffffffff;',
  2379. 'this.i7 =-0xfffffffffffff-1;',
  2380. 'this.i8 = 0;',
  2381. 'this.u8 = 0xfffffffffffff;',
  2382. 'this.u9 = 0x0;',
  2383. 'this.u10 = 0xff00;'
  2384. ]),
  2385. '');
  2386. end;
  2387. procedure TTestModule.TestBaseTypeSingleFail;
  2388. begin
  2389. StartProgram(false);
  2390. Add('var s: single;');
  2391. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2392. ConvertProgram;
  2393. end;
  2394. procedure TTestModule.TestBaseTypeExtendedFail;
  2395. begin
  2396. StartProgram(false);
  2397. Add('var e: extended;');
  2398. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2399. ConvertProgram;
  2400. end;
  2401. procedure TTestModule.TestConstBaseTypes;
  2402. begin
  2403. StartProgram(false);
  2404. Add('const');
  2405. Add(' i: longint = 3;');
  2406. Add(' s: string = ''foo'';');
  2407. Add(' c: char = ''4'';');
  2408. Add(' b: boolean = true;');
  2409. Add(' d: double = 5.6;');
  2410. Add(' e = low(word);');
  2411. Add(' f = high(word);');
  2412. Add('begin');
  2413. ConvertProgram;
  2414. CheckSource('TestVarBaseTypes',
  2415. LinesToStr([
  2416. 'this.i=3;',
  2417. 'this.s="foo";',
  2418. 'this.c="4";',
  2419. 'this.b=true;',
  2420. 'this.d=5.6;',
  2421. 'this.e = 0;',
  2422. 'this.f = 65535;'
  2423. ]),
  2424. '');
  2425. end;
  2426. procedure TTestModule.TestAliasTypeRef;
  2427. begin
  2428. StartProgram(false);
  2429. Add('type');
  2430. Add(' a=longint;');
  2431. Add(' b=a;');
  2432. Add('var');
  2433. Add(' c: A;');
  2434. Add(' d: B;');
  2435. Add('begin');
  2436. ConvertProgram;
  2437. CheckSource('TestAliasTypeRef',
  2438. LinesToStr([ // statements
  2439. 'this.c = 0;',
  2440. 'this.d = 0;'
  2441. ]),
  2442. LinesToStr([ // this.$main
  2443. ''
  2444. ]));
  2445. end;
  2446. procedure TTestModule.TestTypeCast_BaseTypes;
  2447. begin
  2448. StartProgram(false);
  2449. Add([
  2450. 'var',
  2451. ' i: longint;',
  2452. ' b: boolean;',
  2453. ' d: double;',
  2454. ' s: string;',
  2455. ' c: char;',
  2456. 'begin',
  2457. ' i:=longint(i);',
  2458. ' i:=longint(b);',
  2459. ' b:=boolean(b);',
  2460. ' b:=boolean(i);',
  2461. ' d:=double(d);',
  2462. ' d:=double(i);',
  2463. ' s:=string(s);',
  2464. ' s:=string(c);',
  2465. ' c:=char(c);',
  2466. ' c:=char(i);',
  2467. ' c:=char(65);',
  2468. ' c:=char(#10);',
  2469. ' c:=char(#$E000);',
  2470. '']);
  2471. ConvertProgram;
  2472. CheckSource('TestAliasTypeRef',
  2473. LinesToStr([ // statements
  2474. 'this.i = 0;',
  2475. 'this.b = false;',
  2476. 'this.d = 0.0;',
  2477. 'this.s = "";',
  2478. 'this.c = "";',
  2479. '']),
  2480. LinesToStr([ // this.$main
  2481. '$mod.i = $mod.i;',
  2482. '$mod.i = ($mod.b ? 1 : 0);',
  2483. '$mod.b = $mod.b;',
  2484. '$mod.b = $mod.i != 0;',
  2485. '$mod.d = $mod.d;',
  2486. '$mod.d = $mod.i;',
  2487. '$mod.s = $mod.s;',
  2488. '$mod.s = $mod.c;',
  2489. '$mod.c = $mod.c;',
  2490. '$mod.c = String.fromCharCode($mod.i);',
  2491. '$mod.c = "A";',
  2492. '$mod.c = "\n";',
  2493. '$mod.c = "";',
  2494. '']));
  2495. end;
  2496. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2497. begin
  2498. StartProgram(false);
  2499. Add('type');
  2500. Add(' integer = longint;');
  2501. Add(' TYesNo = boolean;');
  2502. Add(' TFloat = double;');
  2503. Add(' TCaption = string;');
  2504. Add(' TChar = char;');
  2505. Add('var');
  2506. Add(' i: integer;');
  2507. Add(' b: TYesNo;');
  2508. Add(' d: TFloat;');
  2509. Add(' s: TCaption;');
  2510. Add(' c: TChar;');
  2511. Add('begin');
  2512. Add(' i:=integer(i);');
  2513. Add(' i:=integer(b);');
  2514. Add(' b:=TYesNo(b);');
  2515. Add(' b:=TYesNo(i);');
  2516. Add(' d:=TFloat(d);');
  2517. Add(' d:=TFloat(i);');
  2518. Add(' s:=TCaption(s);');
  2519. Add(' s:=TCaption(c);');
  2520. Add(' c:=TChar(c);');
  2521. ConvertProgram;
  2522. CheckSource('TestAliasTypeRef',
  2523. LinesToStr([ // statements
  2524. 'this.i = 0;',
  2525. 'this.b = false;',
  2526. 'this.d = 0.0;',
  2527. 'this.s = "";',
  2528. 'this.c = "";',
  2529. '']),
  2530. LinesToStr([ // this.$main
  2531. '$mod.i = $mod.i;',
  2532. '$mod.i = ($mod.b ? 1 : 0);',
  2533. '$mod.b = $mod.b;',
  2534. '$mod.b = $mod.i != 0;',
  2535. '$mod.d = $mod.d;',
  2536. '$mod.d = $mod.i;',
  2537. '$mod.s = $mod.s;',
  2538. '$mod.s = $mod.c;',
  2539. '$mod.c = $mod.c;',
  2540. '']));
  2541. end;
  2542. procedure TTestModule.TestEmptyProc;
  2543. begin
  2544. StartProgram(false);
  2545. Add('procedure Test;');
  2546. Add('begin');
  2547. Add('end;');
  2548. Add('begin');
  2549. ConvertProgram;
  2550. CheckSource('TestEmptyProc',
  2551. LinesToStr([ // statements
  2552. 'this.Test = function () {',
  2553. '};'
  2554. ]),
  2555. LinesToStr([ // this.$main
  2556. ''
  2557. ]));
  2558. end;
  2559. procedure TTestModule.TestProcOneParam;
  2560. begin
  2561. StartProgram(false);
  2562. Add('procedure ProcA(i: longint);');
  2563. Add('begin');
  2564. Add('end;');
  2565. Add('begin');
  2566. Add(' PROCA(3);');
  2567. ConvertProgram;
  2568. CheckSource('TestProcOneParam',
  2569. LinesToStr([ // statements
  2570. 'this.ProcA = function (i) {',
  2571. '};'
  2572. ]),
  2573. LinesToStr([ // this.$main
  2574. '$mod.ProcA(3);'
  2575. ]));
  2576. end;
  2577. procedure TTestModule.TestFunctionWithoutParams;
  2578. begin
  2579. StartProgram(false);
  2580. Add('function FuncA: longint;');
  2581. Add('begin');
  2582. Add('end;');
  2583. Add('var i: longint;');
  2584. Add('begin');
  2585. Add(' I:=FUNCA();');
  2586. Add(' I:=FUNCA;');
  2587. Add(' FUNCA();');
  2588. Add(' FUNCA;');
  2589. ConvertProgram;
  2590. CheckSource('TestProcWithoutParams',
  2591. LinesToStr([ // statements
  2592. 'this.FuncA = function () {',
  2593. ' var Result = 0;',
  2594. ' return Result;',
  2595. '};',
  2596. 'this.i=0;'
  2597. ]),
  2598. LinesToStr([ // this.$main
  2599. '$mod.i=$mod.FuncA();',
  2600. '$mod.i=$mod.FuncA();',
  2601. '$mod.FuncA();',
  2602. '$mod.FuncA();'
  2603. ]));
  2604. end;
  2605. procedure TTestModule.TestProcedureWithoutParams;
  2606. begin
  2607. StartProgram(false);
  2608. Add('procedure ProcA;');
  2609. Add('begin');
  2610. Add('end;');
  2611. Add('begin');
  2612. Add(' PROCA();');
  2613. Add(' PROCA;');
  2614. ConvertProgram;
  2615. CheckSource('TestProcWithoutParams',
  2616. LinesToStr([ // statements
  2617. 'this.ProcA = function () {',
  2618. '};'
  2619. ]),
  2620. LinesToStr([ // this.$main
  2621. '$mod.ProcA();',
  2622. '$mod.ProcA();'
  2623. ]));
  2624. end;
  2625. procedure TTestModule.TestIncDec;
  2626. begin
  2627. StartProgram(false);
  2628. Add([
  2629. 'procedure DoIt(var i: longint);',
  2630. 'begin',
  2631. ' inc(i);',
  2632. ' inc(i,2);',
  2633. 'end;',
  2634. 'var',
  2635. ' Bar: longint;',
  2636. 'begin',
  2637. ' inc(bar);',
  2638. ' inc(bar,2);',
  2639. ' dec(bar);',
  2640. ' dec(bar,3);',
  2641. '']);
  2642. ConvertProgram;
  2643. CheckSource('TestIncDec',
  2644. LinesToStr([ // statements
  2645. 'this.DoIt = function (i) {',
  2646. ' i.set(i.get()+1);',
  2647. ' i.set(i.get()+2);',
  2648. '};',
  2649. 'this.Bar = 0;'
  2650. ]),
  2651. LinesToStr([ // this.$main
  2652. '$mod.Bar+=1;',
  2653. '$mod.Bar+=2;',
  2654. '$mod.Bar-=1;',
  2655. '$mod.Bar-=3;'
  2656. ]));
  2657. end;
  2658. procedure TTestModule.TestLoHiFpcMode;
  2659. begin
  2660. StartProgram(false);
  2661. Add([
  2662. '{$mode objfpc}',
  2663. 'const',
  2664. ' LoByte1 = Lo(Word($1234));',
  2665. ' HiByte1 = Hi(Word($1234));',
  2666. ' LoByte2 = Lo(SmallInt($1234));',
  2667. ' HiByte2 = Hi(SmallInt($1234));',
  2668. ' LoWord1 = Lo($1234CDEF);',
  2669. ' HiWord1 = Hi($1234CDEF);',
  2670. ' LoWord2 = Lo(-$1234CDEF);',
  2671. ' HiWord2 = Hi(-$1234CDEF);',
  2672. ' lo4:byte=lo(byte($34));',
  2673. ' hi4:byte=hi(byte($34));',
  2674. ' lo5:byte=lo(shortint(-$34));',
  2675. ' hi5:byte=hi(shortint(-$34));',
  2676. ' lo6:longword=lo($123456789ABCD);',
  2677. ' hi6:longword=hi($123456789ABCD);',
  2678. ' lo7:longword=lo(-$123456789ABCD);',
  2679. ' hi7:longword=hi(-$123456789ABCD);',
  2680. 'var',
  2681. ' b: Byte;',
  2682. ' ss: shortint;',
  2683. ' w: Word;',
  2684. ' si: SmallInt;',
  2685. ' lw: LongWord;',
  2686. ' li: LongInt;',
  2687. ' b2: Byte;',
  2688. ' ni: nativeint;',
  2689. 'begin',
  2690. ' w := $1234;',
  2691. ' ss := -$12;',
  2692. ' b := lo(ss);',
  2693. ' b := HI(ss);',
  2694. ' b := lo(w);',
  2695. ' b := HI(w);',
  2696. ' b2 := lo(b);',
  2697. ' b2 := hi(b);',
  2698. ' lw := $1234CDEF;',
  2699. ' w := lo(lw);',
  2700. ' w := hi(lw);',
  2701. ' ni := $123456789ABCD;',
  2702. ' lw := lo(ni);',
  2703. ' lw := hi(ni);',
  2704. '']);
  2705. ConvertProgram;
  2706. CheckSource('TestLoHiFpcMode',
  2707. LinesToStr([ // statements
  2708. 'this.LoByte1 = 0x1234 & 0xFF;',
  2709. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2710. 'this.LoByte2 = 0x1234 & 0xFF;',
  2711. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2712. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2713. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2714. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2715. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2716. 'this.lo4 = 0x34 & 0xF;',
  2717. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2718. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2719. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2720. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2721. 'this.hi6 = 74565 >>> 0;',
  2722. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2723. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2724. 'this.b = 0;',
  2725. 'this.ss = 0;',
  2726. 'this.w = 0;',
  2727. 'this.si = 0;',
  2728. 'this.lw = 0;',
  2729. 'this.li = 0;',
  2730. 'this.b2 = 0;',
  2731. 'this.ni = 0;',
  2732. '']),
  2733. LinesToStr([ // this.$main
  2734. '$mod.w = 0x1234;',
  2735. '$mod.ss = -0x12;',
  2736. '$mod.b = $mod.ss & 0xFF;',
  2737. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2738. '$mod.b = $mod.w & 0xFF;',
  2739. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2740. '$mod.b2 = $mod.b & 0xF;',
  2741. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2742. '$mod.lw = 0x1234CDEF;',
  2743. '$mod.w = $mod.lw & 0xFFFF;',
  2744. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2745. '$mod.ni = 0x123456789ABCD;',
  2746. '$mod.lw = $mod.ni >>> 0;',
  2747. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2748. '']));
  2749. end;
  2750. procedure TTestModule.TestLoHiDelphiMode;
  2751. begin
  2752. StartProgram(false);
  2753. Add([
  2754. '{$mode delphi}',
  2755. 'const',
  2756. ' LoByte1 = Lo(Word($1234));',
  2757. ' HiByte1 = Hi(Word($1234));',
  2758. ' LoByte2 = Lo(SmallInt($1234));',
  2759. ' HiByte2 = Hi(SmallInt($1234));',
  2760. ' LoByte3 = Lo($1234CDEF);',
  2761. ' HiByte3 = Hi($1234CDEF);',
  2762. ' LoByte4 = Lo(-$1234CDEF);',
  2763. ' HiByte4 = Hi(-$1234CDEF);',
  2764. 'var',
  2765. ' b: Byte;',
  2766. ' w: Word;',
  2767. ' si: SmallInt;',
  2768. ' lw: LongWord;',
  2769. ' li: LongInt;',
  2770. 'begin',
  2771. ' w := $1234;',
  2772. ' b := lo(w);',
  2773. ' b := HI(w);',
  2774. ' lw := $1234CDEF;',
  2775. ' b := lo(lw);',
  2776. ' b := hi(lw);',
  2777. '']);
  2778. ConvertProgram;
  2779. CheckSource('TestLoHiDelphiMode',
  2780. LinesToStr([ // statements
  2781. 'this.LoByte1 = 0x1234 & 0xFF;',
  2782. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2783. 'this.LoByte2 = 0x1234 & 0xFF;',
  2784. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2785. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2786. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2787. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2788. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2789. 'this.b = 0;',
  2790. 'this.w = 0;',
  2791. 'this.si = 0;',
  2792. 'this.lw = 0;',
  2793. 'this.li = 0;'
  2794. ]),
  2795. LinesToStr([ // this.$main
  2796. '$mod.w = 0x1234;',
  2797. '$mod.b = $mod.w & 0xFF;',
  2798. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2799. '$mod.lw = 0x1234CDEF;',
  2800. '$mod.b = $mod.lw & 0xFF;',
  2801. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2802. ]));
  2803. end;
  2804. procedure TTestModule.TestAssignments;
  2805. begin
  2806. StartProgram(false);
  2807. Parser.Options:=Parser.Options+[po_cassignments];
  2808. Add('var');
  2809. Add(' Bar:longint;');
  2810. Add('begin');
  2811. Add(' bar:=3;');
  2812. Add(' bar+=4;');
  2813. Add(' bar-=5;');
  2814. Add(' bar*=6;');
  2815. ConvertProgram;
  2816. CheckSource('TestAssignments',
  2817. LinesToStr([ // statements
  2818. 'this.Bar = 0;'
  2819. ]),
  2820. LinesToStr([ // this.$main
  2821. '$mod.Bar=3;',
  2822. '$mod.Bar+=4;',
  2823. '$mod.Bar-=5;',
  2824. '$mod.Bar*=6;'
  2825. ]));
  2826. end;
  2827. procedure TTestModule.TestArithmeticOperators1;
  2828. begin
  2829. StartProgram(false);
  2830. Add('var');
  2831. Add(' vA,vB,vC:longint;');
  2832. Add('begin');
  2833. Add(' va:=1;');
  2834. Add(' vb:=va+va;');
  2835. Add(' vb:=va div vb;');
  2836. Add(' vb:=va mod vb;');
  2837. Add(' vb:=va+va*vb+va div vb;');
  2838. Add(' vc:=-va;');
  2839. Add(' va:=va-vb;');
  2840. Add(' vb:=va;');
  2841. Add(' if va<vb then vc:=va else vc:=vb;');
  2842. ConvertProgram;
  2843. CheckSource('TestArithmeticOperators1',
  2844. LinesToStr([ // statements
  2845. 'this.vA = 0;',
  2846. 'this.vB = 0;',
  2847. 'this.vC = 0;'
  2848. ]),
  2849. LinesToStr([ // this.$main
  2850. '$mod.vA = 1;',
  2851. '$mod.vB = $mod.vA + $mod.vA;',
  2852. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  2853. '$mod.vB = $mod.vA % $mod.vB;',
  2854. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  2855. '$mod.vC = -$mod.vA;',
  2856. '$mod.vA = $mod.vA - $mod.vB;',
  2857. '$mod.vB = $mod.vA;',
  2858. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2859. ]));
  2860. end;
  2861. procedure TTestModule.TestLogicalOperators;
  2862. begin
  2863. StartProgram(false);
  2864. Add('var');
  2865. Add(' vA,vB,vC:boolean;');
  2866. Add('begin');
  2867. Add(' va:=vb and vc;');
  2868. Add(' va:=vb or vc;');
  2869. Add(' va:=vb xor vc;');
  2870. Add(' va:=true and vc;');
  2871. Add(' va:=(vb and vc) or (va and vb);');
  2872. Add(' va:=not vb;');
  2873. ConvertProgram;
  2874. CheckSource('TestLogicalOperators',
  2875. LinesToStr([ // statements
  2876. 'this.vA = false;',
  2877. 'this.vB = false;',
  2878. 'this.vC = false;'
  2879. ]),
  2880. LinesToStr([ // this.$main
  2881. '$mod.vA = $mod.vB && $mod.vC;',
  2882. '$mod.vA = $mod.vB || $mod.vC;',
  2883. '$mod.vA = $mod.vB ^ $mod.vC;',
  2884. '$mod.vA = true && $mod.vC;',
  2885. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  2886. '$mod.vA = !$mod.vB;'
  2887. ]));
  2888. end;
  2889. procedure TTestModule.TestBitwiseOperators;
  2890. begin
  2891. StartProgram(false);
  2892. Add('var');
  2893. Add(' vA,vB,vC:longint;');
  2894. Add('begin');
  2895. Add(' va:=vb and vc;');
  2896. Add(' va:=vb or vc;');
  2897. Add(' va:=vb xor vc;');
  2898. Add(' va:=vb shl vc;');
  2899. Add(' va:=vb shr vc;');
  2900. Add(' va:=3 and vc;');
  2901. Add(' va:=(vb and vc) or (va and vb);');
  2902. Add(' va:=not vb;');
  2903. ConvertProgram;
  2904. CheckSource('TestBitwiseOperators',
  2905. LinesToStr([ // statements
  2906. 'this.vA = 0;',
  2907. 'this.vB = 0;',
  2908. 'this.vC = 0;'
  2909. ]),
  2910. LinesToStr([ // this.$main
  2911. '$mod.vA = $mod.vB & $mod.vC;',
  2912. '$mod.vA = $mod.vB | $mod.vC;',
  2913. '$mod.vA = $mod.vB ^ $mod.vC;',
  2914. '$mod.vA = $mod.vB << $mod.vC;',
  2915. '$mod.vA = $mod.vB >>> $mod.vC;',
  2916. '$mod.vA = 3 & $mod.vC;',
  2917. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  2918. '$mod.vA = ~$mod.vB;'
  2919. ]));
  2920. end;
  2921. procedure TTestModule.TestPrgProcVar;
  2922. begin
  2923. StartProgram(false);
  2924. Add('procedure Proc1;');
  2925. Add('type');
  2926. Add(' t1=longint;');
  2927. Add('var');
  2928. Add(' vA:t1;');
  2929. Add('begin');
  2930. Add('end;');
  2931. Add('begin');
  2932. ConvertProgram;
  2933. CheckSource('TestPrgProcVar',
  2934. LinesToStr([ // statements
  2935. 'this.Proc1 = function () {',
  2936. ' var vA=0;',
  2937. '};'
  2938. ]),
  2939. LinesToStr([ // this.$main
  2940. ''
  2941. ]));
  2942. end;
  2943. procedure TTestModule.TestUnitProcVar;
  2944. begin
  2945. StartUnit(false);
  2946. Add('interface');
  2947. Add('');
  2948. Add('type tA=string; // unit scope');
  2949. Add('procedure Proc1;');
  2950. Add('');
  2951. Add('implementation');
  2952. Add('');
  2953. Add('procedure Proc1;');
  2954. Add('type tA=longint; // local proc scope');
  2955. Add('var v1:tA; // using local tA');
  2956. Add('begin');
  2957. Add('end;');
  2958. Add('var v2:tA; // using interface tA');
  2959. ConvertUnit;
  2960. CheckSource('TestUnitProcVar',
  2961. LinesToStr([ // statements
  2962. 'var $impl = $mod.$impl;',
  2963. 'this.Proc1 = function () {',
  2964. ' var v1 = 0;',
  2965. '};',
  2966. '']),
  2967. // this.$init
  2968. '',
  2969. // implementation
  2970. LinesToStr([
  2971. '$impl.v2 = "";',
  2972. '']));
  2973. end;
  2974. procedure TTestModule.TestImplProc;
  2975. begin
  2976. StartUnit(false);
  2977. Add('interface');
  2978. Add('');
  2979. Add('procedure Proc1;');
  2980. Add('');
  2981. Add('implementation');
  2982. Add('');
  2983. Add('procedure Proc1; begin end;');
  2984. Add('procedure Proc2; begin end;');
  2985. Add('initialization');
  2986. Add(' Proc1;');
  2987. Add(' Proc2;');
  2988. ConvertUnit;
  2989. CheckSource('TestImplProc',
  2990. LinesToStr([ // statements
  2991. 'var $impl = $mod.$impl;',
  2992. 'this.Proc1 = function () {',
  2993. '};',
  2994. '']),
  2995. LinesToStr([ // this.$init
  2996. '$mod.Proc1();',
  2997. '$impl.Proc2();',
  2998. '']),
  2999. LinesToStr([ // implementation
  3000. '$impl.Proc2 = function () {',
  3001. '};',
  3002. ''])
  3003. );
  3004. end;
  3005. procedure TTestModule.TestFunctionResult;
  3006. begin
  3007. StartProgram(false);
  3008. Add('function Func1: longint;');
  3009. Add('begin');
  3010. Add(' Result:=3;');
  3011. Add(' Func1:=4;');
  3012. Add('end;');
  3013. Add('begin');
  3014. ConvertProgram;
  3015. CheckSource('TestFunctionResult',
  3016. LinesToStr([ // statements
  3017. 'this.Func1 = function () {',
  3018. ' var Result = 0;',
  3019. ' Result = 3;',
  3020. ' Result = 4;',
  3021. ' return Result;',
  3022. '};'
  3023. ]),
  3024. '');
  3025. end;
  3026. procedure TTestModule.TestNestedProc;
  3027. begin
  3028. StartProgram(false);
  3029. Add([
  3030. 'var vInUnit: longint;',
  3031. 'function DoIt(pA,pD: longint): longint;',
  3032. 'var',
  3033. ' vB: longint;',
  3034. ' vC: longint;',
  3035. ' function Nesty(pA: longint): longint; ',
  3036. ' var vB: longint;',
  3037. ' begin',
  3038. ' Result:=pa+vb+vc+pd+vInUnit;',
  3039. ' nesty:=3;',
  3040. ' doit:=4;',
  3041. ' exit;',
  3042. ' end;',
  3043. 'begin',
  3044. ' Result:=pa+vb+vc;',
  3045. ' doit:=6;',
  3046. ' exit;',
  3047. 'end;',
  3048. 'begin']);
  3049. ConvertProgram;
  3050. CheckSource('TestNestedProc',
  3051. LinesToStr([ // statements
  3052. 'this.vInUnit = 0;',
  3053. 'this.DoIt = function (pA, pD) {',
  3054. ' var Result = 0;',
  3055. ' var vB = 0;',
  3056. ' var vC = 0;',
  3057. ' function Nesty(pA) {',
  3058. ' var Result$1 = 0;',
  3059. ' var vB = 0;',
  3060. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3061. ' Result$1 = 3;',
  3062. ' Result = 4;',
  3063. ' return Result$1;',
  3064. ' return Result$1;',
  3065. ' };',
  3066. ' Result = pA + vB + vC;',
  3067. ' Result = 6;',
  3068. ' return Result;',
  3069. ' return Result;',
  3070. '};'
  3071. ]),
  3072. '');
  3073. end;
  3074. procedure TTestModule.TestNestedProc_ResultString;
  3075. begin
  3076. StartProgram(false);
  3077. Add([
  3078. 'function DoIt: string;',
  3079. ' function Nesty: string; ',
  3080. ' begin',
  3081. ' nesty:=#65#66;',
  3082. ' nesty[1]:=#67;',
  3083. ' doit:=#68;',
  3084. ' doit[2]:=#69;',
  3085. ' end;',
  3086. 'begin',
  3087. ' doit:=#70;',
  3088. ' doit[3]:=#71;',
  3089. 'end;',
  3090. 'begin']);
  3091. ConvertProgram;
  3092. CheckSource('TestNestedProc_ResultString',
  3093. LinesToStr([ // statements
  3094. 'this.DoIt = function () {',
  3095. ' var Result = "";',
  3096. ' function Nesty() {',
  3097. ' var Result$1 = "";',
  3098. ' Result$1 = "AB";',
  3099. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3100. ' Result = "D";',
  3101. ' Result = rtl.setCharAt(Result, 1, "E");',
  3102. ' return Result$1;',
  3103. ' };',
  3104. ' Result = "F";',
  3105. ' Result = rtl.setCharAt(Result, 2, "G");',
  3106. ' return Result;',
  3107. '};'
  3108. ]),
  3109. '');
  3110. end;
  3111. procedure TTestModule.TestForwardProc;
  3112. begin
  3113. StartProgram(false);
  3114. Add('procedure FuncA(Bar: longint); forward;');
  3115. Add('procedure FuncB(Bar: longint);');
  3116. Add('begin');
  3117. Add(' funca(bar);');
  3118. Add('end;');
  3119. Add('procedure funca(bar: longint);');
  3120. Add('begin');
  3121. Add(' if bar=3 then ;');
  3122. Add('end;');
  3123. Add('begin');
  3124. Add(' funca(4);');
  3125. Add(' funcb(5);');
  3126. ConvertProgram;
  3127. CheckSource('TestForwardProc',
  3128. LinesToStr([ // statements'
  3129. 'this.FuncB = function (Bar) {',
  3130. ' $mod.FuncA(Bar);',
  3131. '};',
  3132. 'this.FuncA = function (Bar) {',
  3133. ' if (Bar === 3);',
  3134. '};'
  3135. ]),
  3136. LinesToStr([
  3137. '$mod.FuncA(4);',
  3138. '$mod.FuncB(5);'
  3139. ])
  3140. );
  3141. end;
  3142. procedure TTestModule.TestNestedForwardProc;
  3143. begin
  3144. StartProgram(false);
  3145. Add('procedure FuncA;');
  3146. Add(' procedure FuncB(i: longint); forward;');
  3147. Add(' procedure FuncC(i: longint);');
  3148. Add(' begin');
  3149. Add(' funcb(i);');
  3150. Add(' end;');
  3151. Add(' procedure FuncB(i: longint);');
  3152. Add(' begin');
  3153. Add(' if i=3 then ;');
  3154. Add(' end;');
  3155. Add('begin');
  3156. Add(' funcc(4)');
  3157. Add('end;');
  3158. Add('begin');
  3159. Add(' funca;');
  3160. ConvertProgram;
  3161. CheckSource('TestNestedForwardProc',
  3162. LinesToStr([ // statements'
  3163. 'this.FuncA = function () {',
  3164. ' function FuncC(i) {',
  3165. ' FuncB(i);',
  3166. ' };',
  3167. ' function FuncB(i) {',
  3168. ' if (i === 3);',
  3169. ' };',
  3170. ' FuncC(4);',
  3171. '};'
  3172. ]),
  3173. LinesToStr([
  3174. '$mod.FuncA();'
  3175. ])
  3176. );
  3177. end;
  3178. procedure TTestModule.TestAssignFunctionResult;
  3179. begin
  3180. StartProgram(false);
  3181. Add('function Func1: longint;');
  3182. Add('begin');
  3183. Add('end;');
  3184. Add('var i: longint;');
  3185. Add('begin');
  3186. Add(' i:=func1();');
  3187. Add(' i:=func1()+func1();');
  3188. ConvertProgram;
  3189. CheckSource('TestAssignFunctionResult',
  3190. LinesToStr([ // statements
  3191. 'this.Func1 = function () {',
  3192. ' var Result = 0;',
  3193. ' return Result;',
  3194. '};',
  3195. 'this.i = 0;'
  3196. ]),
  3197. LinesToStr([
  3198. '$mod.i = $mod.Func1();',
  3199. '$mod.i = $mod.Func1() + $mod.Func1();'
  3200. ]));
  3201. end;
  3202. procedure TTestModule.TestFunctionResultInCondition;
  3203. begin
  3204. StartProgram(false);
  3205. Add('function Func1: longint;');
  3206. Add('begin');
  3207. Add('end;');
  3208. Add('function Func2: boolean;');
  3209. Add('begin');
  3210. Add('end;');
  3211. Add('var i: longint;');
  3212. Add('begin');
  3213. Add(' if func2 then ;');
  3214. Add(' if i=func1() then ;');
  3215. Add(' if i=func1 then ;');
  3216. ConvertProgram;
  3217. CheckSource('TestFunctionResultInCondition',
  3218. LinesToStr([ // statements
  3219. 'this.Func1 = function () {',
  3220. ' var Result = 0;',
  3221. ' return Result;',
  3222. '};',
  3223. 'this.Func2 = function () {',
  3224. ' var Result = false;',
  3225. ' return Result;',
  3226. '};',
  3227. 'this.i = 0;'
  3228. ]),
  3229. LinesToStr([
  3230. 'if ($mod.Func2());',
  3231. 'if ($mod.i === $mod.Func1());',
  3232. 'if ($mod.i === $mod.Func1());'
  3233. ]));
  3234. end;
  3235. procedure TTestModule.TestFunctionResultInForLoop;
  3236. begin
  3237. StartProgram(false);
  3238. Add([
  3239. 'function Func1(a: array of longint): longint;',
  3240. 'begin',
  3241. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3242. ' for Result in a do if a[Result]=0 then exit;',
  3243. 'end;',
  3244. 'begin',
  3245. ' Func1([1,2,3])']);
  3246. ConvertProgram;
  3247. CheckSource('TestFunctionResultInForLoop',
  3248. LinesToStr([ // statements
  3249. 'this.Func1 = function (a) {',
  3250. ' var Result = 0;',
  3251. ' for (var $l1 = rtl.length(a) - 1; $l1 >= 0; $l1--) {',
  3252. ' Result = $l1;',
  3253. ' if (a[Result] === 0) return Result;',
  3254. ' };',
  3255. ' for (var $in2 = a, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) {',
  3256. ' Result = $in2[$l3];',
  3257. ' if (a[Result] === 0) return Result;',
  3258. ' };',
  3259. ' return Result;',
  3260. '};',
  3261. '']),
  3262. LinesToStr([
  3263. '$mod.Func1([1, 2, 3]);'
  3264. ]));
  3265. end;
  3266. procedure TTestModule.TestFunctionResultInTypeCast;
  3267. begin
  3268. StartProgram(false);
  3269. Add([
  3270. 'function GetInt: longint;',
  3271. 'begin',
  3272. 'end;',
  3273. 'begin',
  3274. ' if Byte(GetInt)=0 then ;',
  3275. '']);
  3276. ConvertProgram;
  3277. CheckSource('TestFunctionResultInTypeCast',
  3278. LinesToStr([ // statements
  3279. 'this.GetInt = function () {',
  3280. ' var Result = 0;',
  3281. ' return Result;',
  3282. '};',
  3283. '']),
  3284. LinesToStr([
  3285. 'if (($mod.GetInt() & 255) === 0) ;'
  3286. ]));
  3287. end;
  3288. procedure TTestModule.TestExit;
  3289. begin
  3290. StartProgram(false);
  3291. Add('procedure ProcA;');
  3292. Add('begin');
  3293. Add(' exit;');
  3294. Add('end;');
  3295. Add('function FuncB: longint;');
  3296. Add('begin');
  3297. Add(' exit;');
  3298. Add(' exit(3);');
  3299. Add('end;');
  3300. Add('function FuncC: string;');
  3301. Add('begin');
  3302. Add(' exit;');
  3303. Add(' exit(''a'');');
  3304. Add(' exit(''abc'');');
  3305. Add('end;');
  3306. Add('begin');
  3307. Add(' exit;');
  3308. Add(' exit(1);');
  3309. ConvertProgram;
  3310. CheckSource('TestExit',
  3311. LinesToStr([ // statements
  3312. 'this.ProcA = function () {',
  3313. ' return;',
  3314. '};',
  3315. 'this.FuncB = function () {',
  3316. ' var Result = 0;',
  3317. ' return Result;',
  3318. ' return 3;',
  3319. ' return Result;',
  3320. '};',
  3321. 'this.FuncC = function () {',
  3322. ' var Result = "";',
  3323. ' return Result;',
  3324. ' return "a";',
  3325. ' return "abc";',
  3326. ' return Result;',
  3327. '};'
  3328. ]),
  3329. LinesToStr([
  3330. 'return;',
  3331. 'return 1;',
  3332. '']));
  3333. end;
  3334. procedure TTestModule.TestBreak;
  3335. begin
  3336. StartProgram(false);
  3337. Add([
  3338. 'var',
  3339. ' i: longint;',
  3340. 'begin',
  3341. ' repeat',
  3342. ' break;',
  3343. ' until true;',
  3344. ' while true do',
  3345. ' break;',
  3346. ' for i:=1 to 2 do',
  3347. ' break;']);
  3348. ConvertProgram;
  3349. CheckSource('TestBreak',
  3350. LinesToStr([ // statements
  3351. 'this.i = 0;'
  3352. ]),
  3353. LinesToStr([
  3354. 'do {',
  3355. ' break;',
  3356. '} while (!true);',
  3357. 'while (true) break;',
  3358. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3359. '']));
  3360. end;
  3361. procedure TTestModule.TestBreakAsVar;
  3362. begin
  3363. StartProgram(false);
  3364. Add([
  3365. 'procedure DoIt(break: boolean);',
  3366. 'begin',
  3367. ' if break then ;',
  3368. 'end;',
  3369. 'var',
  3370. ' break: boolean;',
  3371. 'begin',
  3372. ' if break then ;']);
  3373. ConvertProgram;
  3374. CheckSource('TestBreakAsVar',
  3375. LinesToStr([ // statements
  3376. 'this.DoIt = function (Break) {',
  3377. ' if (Break) ;',
  3378. '};',
  3379. 'this.Break = false;',
  3380. '']),
  3381. LinesToStr([
  3382. 'if($mod.Break) ;',
  3383. '']));
  3384. end;
  3385. procedure TTestModule.TestContinue;
  3386. begin
  3387. StartProgram(false);
  3388. Add('var i: longint;');
  3389. Add('begin');
  3390. Add(' repeat');
  3391. Add(' continue;');
  3392. Add(' until true;');
  3393. Add(' while true do');
  3394. Add(' continue;');
  3395. Add(' for i:=1 to 2 do');
  3396. Add(' continue;');
  3397. ConvertProgram;
  3398. CheckSource('TestContinue',
  3399. LinesToStr([ // statements
  3400. 'this.i = 0;'
  3401. ]),
  3402. LinesToStr([
  3403. 'do {',
  3404. ' continue;',
  3405. '} while (!true);',
  3406. 'while (true) continue;',
  3407. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3408. '']));
  3409. end;
  3410. procedure TTestModule.TestProc_External;
  3411. begin
  3412. StartProgram(false);
  3413. Add('procedure Foo; external name ''console.log'';');
  3414. Add('function Bar: longint; external name ''get.item'';');
  3415. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3416. Add('var');
  3417. Add(' i: longint;');
  3418. Add('begin');
  3419. Add(' Foo;');
  3420. Add(' i:=Bar;');
  3421. Add(' i:=Bla(''abc'');');
  3422. ConvertProgram;
  3423. CheckSource('TestProc_External',
  3424. LinesToStr([ // statements
  3425. 'this.i = 0;'
  3426. ]),
  3427. LinesToStr([
  3428. 'console.log();',
  3429. '$mod.i = get.item();',
  3430. '$mod.i = apply.something("abc");'
  3431. ]));
  3432. end;
  3433. procedure TTestModule.TestProc_ExternalOtherUnit;
  3434. begin
  3435. AddModuleWithIntfImplSrc('unit2.pas',
  3436. LinesToStr([
  3437. 'procedure Now; external name ''Date.now'';',
  3438. 'procedure DoIt;'
  3439. ]),
  3440. 'procedure doit; begin end;');
  3441. StartUnit(true);
  3442. Add('interface');
  3443. Add('uses unit2;');
  3444. Add('implementation');
  3445. Add('begin');
  3446. Add(' now;');
  3447. Add(' now();');
  3448. Add(' uNit2.now;');
  3449. Add(' uNit2.now();');
  3450. Add(' doit;');
  3451. Add(' uNit2.doit;');
  3452. ConvertUnit;
  3453. CheckSource('TestProc_ExternalOtherUnit',
  3454. LinesToStr([
  3455. '']),
  3456. LinesToStr([
  3457. 'Date.now();',
  3458. 'Date.now();',
  3459. 'Date.now();',
  3460. 'Date.now();',
  3461. 'pas.unit2.DoIt();',
  3462. 'pas.unit2.DoIt();',
  3463. '']));
  3464. end;
  3465. procedure TTestModule.TestProc_Asm;
  3466. begin
  3467. StartProgram(false);
  3468. Add([
  3469. 'function DoIt: longint;',
  3470. 'begin;',
  3471. ' asm',
  3472. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3473. ' end;',
  3474. ' asm console.log(); end;',
  3475. ' asm',
  3476. ' s = "'' ";',
  3477. ' s = ''" '';',
  3478. ' s = s + "world" + "''";',
  3479. ' // end',
  3480. ' s = ''end'';',
  3481. ' s = "end";',
  3482. ' end;',
  3483. 'end;',
  3484. 'begin']);
  3485. ConvertProgram;
  3486. CheckSource('TestProc_Asm',
  3487. LinesToStr([ // statements
  3488. 'this.DoIt = function () {',
  3489. ' var Result = 0;',
  3490. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3491. ' console.log();',
  3492. ' s = "'' ";',
  3493. ' s = ''" '';',
  3494. ' s = s + "world" + "''";',
  3495. ' // end',
  3496. ' s = ''end'';',
  3497. ' s = "end";',
  3498. ' return Result;',
  3499. '};'
  3500. ]),
  3501. LinesToStr([
  3502. ''
  3503. ]));
  3504. end;
  3505. procedure TTestModule.TestProc_Assembler;
  3506. begin
  3507. StartProgram(false);
  3508. Add('function DoIt: longint; assembler;');
  3509. Add('asm');
  3510. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3511. Add('end;');
  3512. Add('begin');
  3513. ConvertProgram;
  3514. CheckSource('TestProc_Assembler',
  3515. LinesToStr([ // statements
  3516. 'this.DoIt = function () {',
  3517. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3518. '};'
  3519. ]),
  3520. LinesToStr([
  3521. ''
  3522. ]));
  3523. end;
  3524. procedure TTestModule.TestProc_VarParam;
  3525. begin
  3526. StartProgram(false);
  3527. Add('type integer = longint;');
  3528. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3529. Add('var vJ: integer;');
  3530. Add('begin');
  3531. Add(' vg:=vg+1;');
  3532. Add(' vj:=vh+2;');
  3533. Add(' vi:=vi+3;');
  3534. Add(' doit(vg,vg,vg);');
  3535. Add(' doit(vh,vh,vj);');
  3536. Add(' doit(vi,vi,vi);');
  3537. Add(' doit(vj,vj,vj);');
  3538. Add('end;');
  3539. Add('var i: integer;');
  3540. Add('begin');
  3541. Add(' doit(i,i,i);');
  3542. ConvertProgram;
  3543. CheckSource('TestProc_VarParam',
  3544. LinesToStr([ // statements
  3545. 'this.DoIt = function (vG,vH,vI) {',
  3546. ' var vJ = 0;',
  3547. ' vG = vG + 1;',
  3548. ' vJ = vH + 2;',
  3549. ' vI.set(vI.get()+3);',
  3550. ' $mod.DoIt(vG, vG, {',
  3551. ' get: function () {',
  3552. ' return vG;',
  3553. ' },',
  3554. ' set: function (v) {',
  3555. ' vG = v;',
  3556. ' }',
  3557. ' });',
  3558. ' $mod.DoIt(vH, vH, {',
  3559. ' get: function () {',
  3560. ' return vJ;',
  3561. ' },',
  3562. ' set: function (v) {',
  3563. ' vJ = v;',
  3564. ' }',
  3565. ' });',
  3566. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3567. ' $mod.DoIt(vJ, vJ, {',
  3568. ' get: function () {',
  3569. ' return vJ;',
  3570. ' },',
  3571. ' set: function (v) {',
  3572. ' vJ = v;',
  3573. ' }',
  3574. ' });',
  3575. '};',
  3576. 'this.i = 0;'
  3577. ]),
  3578. LinesToStr([
  3579. '$mod.DoIt($mod.i,$mod.i,{',
  3580. ' p: $mod,',
  3581. ' get: function () {',
  3582. ' return this.p.i;',
  3583. ' },',
  3584. ' set: function (v) {',
  3585. ' this.p.i = v;',
  3586. ' }',
  3587. '});'
  3588. ]));
  3589. end;
  3590. procedure TTestModule.TestProc_VarParamString;
  3591. begin
  3592. StartProgram(false);
  3593. Add(['type TCaption = string;',
  3594. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3595. 'var c: char;',
  3596. 'begin',
  3597. ' va[1]:=c;',
  3598. ' vb[2]:=c;',
  3599. ' vc[3]:=c;',
  3600. 'end;',
  3601. 'begin']);
  3602. ConvertProgram;
  3603. CheckSource('TestProc_VarParamString',
  3604. LinesToStr([ // statements
  3605. 'this.DoIt = function (vA,vB,vC) {',
  3606. ' var c = "";',
  3607. ' vA = rtl.setCharAt(vA, 0, c);',
  3608. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3609. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3610. '};',
  3611. '']),
  3612. LinesToStr([
  3613. ]));
  3614. end;
  3615. procedure TTestModule.TestProc_VarParamV;
  3616. begin
  3617. StartProgram(false);
  3618. Add([
  3619. 'procedure Inc2(var i: longint);',
  3620. 'begin',
  3621. ' i:=i+2;',
  3622. 'end;',
  3623. 'procedure DoIt(v: longint);',
  3624. 'var p: array of longint;',
  3625. 'begin',
  3626. ' Inc2(v);',
  3627. ' Inc2(p[v]);',
  3628. 'end;',
  3629. 'begin']);
  3630. ConvertProgram;
  3631. CheckSource('TestProc_VarParamV',
  3632. LinesToStr([ // statements
  3633. 'this.Inc2 = function (i) {',
  3634. ' i.set(i.get()+2);',
  3635. '};',
  3636. 'this.DoIt = function (v) {',
  3637. ' var p = [];',
  3638. ' $mod.Inc2({get: function () {',
  3639. ' return v;',
  3640. ' }, set: function (w) {',
  3641. ' v = w;',
  3642. ' }});',
  3643. ' $mod.Inc2({',
  3644. ' a: v,',
  3645. ' p: p,',
  3646. ' get: function () {',
  3647. ' return this.p[this.a];',
  3648. ' },',
  3649. ' set: function (v) {',
  3650. ' this.p[this.a] = v;',
  3651. ' }',
  3652. ' });',
  3653. '};',
  3654. '']),
  3655. LinesToStr([
  3656. '']));
  3657. end;
  3658. procedure TTestModule.TestProc_Overload;
  3659. begin
  3660. StartProgram(false);
  3661. Add('procedure DoIt(vI: longint); begin end;');
  3662. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3663. Add('procedure DoIt(vD: double); begin end;');
  3664. Add('begin');
  3665. Add(' DoIt(1);');
  3666. Add(' DoIt(2,3);');
  3667. Add(' DoIt(4.5);');
  3668. ConvertProgram;
  3669. CheckSource('TestProcedureOverload',
  3670. LinesToStr([ // statements
  3671. 'this.DoIt = function (vI) {',
  3672. '};',
  3673. 'this.DoIt$1 = function (vI, vJ) {',
  3674. '};',
  3675. 'this.DoIt$2 = function (vD) {',
  3676. '};',
  3677. '']),
  3678. LinesToStr([
  3679. '$mod.DoIt(1);',
  3680. '$mod.DoIt$1(2, 3);',
  3681. '$mod.DoIt$2(4.5);',
  3682. '']));
  3683. end;
  3684. procedure TTestModule.TestProc_OverloadForward;
  3685. begin
  3686. StartProgram(false);
  3687. Add('procedure DoIt(vI: longint); forward;');
  3688. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3689. Add('procedure doit(vi: longint); begin end;');
  3690. Add('begin');
  3691. Add(' doit(1);');
  3692. Add(' doit(2,3);');
  3693. ConvertProgram;
  3694. CheckSource('TestProcedureOverloadForward',
  3695. LinesToStr([ // statements
  3696. 'this.DoIt$1 = function (vI, vJ) {',
  3697. '};',
  3698. 'this.DoIt = function (vI) {',
  3699. '};',
  3700. '']),
  3701. LinesToStr([
  3702. '$mod.DoIt(1);',
  3703. '$mod.DoIt$1(2, 3);',
  3704. '']));
  3705. end;
  3706. procedure TTestModule.TestProc_OverloadIntfImpl;
  3707. begin
  3708. StartUnit(false);
  3709. Add('interface');
  3710. Add('procedure DoIt(vI: longint);');
  3711. Add('procedure DoIt(vI, vJ: longint);');
  3712. Add('implementation');
  3713. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  3714. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  3715. Add('procedure DoIt(vi: longint); begin end;');
  3716. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  3717. Add('procedure DoIt(vi, vj: longint); begin end;');
  3718. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  3719. Add('begin');
  3720. Add(' doit(1);');
  3721. Add(' doit(2,3);');
  3722. Add(' doit(4,5,6);');
  3723. Add(' doit(7,8,9,10);');
  3724. Add(' doit(11,12,13,14,15);');
  3725. ConvertUnit;
  3726. CheckSource('TestProcedureOverloadUnit',
  3727. LinesToStr([ // statements
  3728. 'var $impl = $mod.$impl;',
  3729. 'this.DoIt = function (vI) {',
  3730. '};',
  3731. 'this.DoIt$1 = function (vI, vJ) {',
  3732. '};',
  3733. '']),
  3734. LinesToStr([ // this.$init
  3735. '$mod.DoIt(1);',
  3736. '$mod.DoIt$1(2, 3);',
  3737. '$impl.DoIt$3(4,5,6);',
  3738. '$impl.DoIt$4(7,8,9,10);',
  3739. '$impl.DoIt$2(11,12,13,14,15);',
  3740. '']),
  3741. LinesToStr([ // implementation
  3742. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  3743. '};',
  3744. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  3745. '};',
  3746. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  3747. '};',
  3748. '']));
  3749. end;
  3750. procedure TTestModule.TestProc_OverloadNested;
  3751. begin
  3752. StartProgram(false);
  3753. Add([
  3754. 'procedure DoIt(vA: longint); overload; forward;',
  3755. 'procedure DoIt(vB, vC: longint); overload;',
  3756. 'begin // 2 param overload',
  3757. ' doit(1);',
  3758. ' doit(1,2);',
  3759. 'end;',
  3760. 'procedure doit(vA: longint);',
  3761. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  3762. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  3763. ' begin // 4 param overload',
  3764. ' doit(1);',
  3765. ' doit(1,2);',
  3766. ' doit(1,2,3);',
  3767. ' doit(1,2,3,4);',
  3768. ' end;',
  3769. ' procedure doit(vA, vB, vC: longint);',
  3770. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  3771. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  3772. ' begin // 6 param overload',
  3773. ' doit(1);',
  3774. ' doit(1,2);',
  3775. ' doit(1,2,3);',
  3776. ' doit(1,2,3,4);',
  3777. ' doit(1,2,3,4,5);',
  3778. ' doit(1,2,3,4,5,6);',
  3779. ' end;',
  3780. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  3781. ' begin // 5 param overload',
  3782. ' doit(1);',
  3783. ' doit(1,2);',
  3784. ' doit(1,2,3);',
  3785. ' doit(1,2,3,4);',
  3786. ' doit(1,2,3,4,5);',
  3787. ' doit(1,2,3,4,5,6);',
  3788. ' end;',
  3789. ' begin // 3 param overload',
  3790. ' doit(1);',
  3791. ' doit(1,2);',
  3792. ' doit(1,2,3);',
  3793. ' doit(1,2,3,4);',
  3794. ' doit(1,2,3,4,5);',
  3795. ' doit(1,2,3,4,5,6);',
  3796. ' end;',
  3797. 'begin // 1 param overload',
  3798. ' doit(1);',
  3799. ' doit(1,2);',
  3800. ' doit(1,2,3);',
  3801. ' doit(1,2,3,4);',
  3802. 'end;',
  3803. 'begin // main',
  3804. ' doit(1);',
  3805. ' doit(1,2);']);
  3806. ConvertProgram;
  3807. CheckSource('TestProcedureOverloadNested',
  3808. LinesToStr([ // statements
  3809. 'this.DoIt$1 = function (vB, vC) {',
  3810. ' $mod.DoIt(1);',
  3811. ' $mod.DoIt$1(1, 2);',
  3812. '};',
  3813. 'this.DoIt = function (vA) {',
  3814. ' function DoIt$3(vA, vB, vC, vD) {',
  3815. ' $mod.DoIt(1);',
  3816. ' $mod.DoIt$1(1, 2);',
  3817. ' DoIt$2(1, 2, 3);',
  3818. ' DoIt$3(1, 2, 3, 4);',
  3819. ' };',
  3820. ' function DoIt$2(vA, vB, vC) {',
  3821. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  3822. ' $mod.DoIt(1);',
  3823. ' $mod.DoIt$1(1, 2);',
  3824. ' DoIt$2(1, 2, 3);',
  3825. ' DoIt$3(1, 2, 3, 4);',
  3826. ' DoIt$4(1, 2, 3, 4, 5);',
  3827. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3828. ' };',
  3829. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  3830. ' $mod.DoIt(1);',
  3831. ' $mod.DoIt$1(1, 2);',
  3832. ' DoIt$2(1, 2, 3);',
  3833. ' DoIt$3(1, 2, 3, 4);',
  3834. ' DoIt$4(1, 2, 3, 4, 5);',
  3835. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3836. ' };',
  3837. ' $mod.DoIt(1);',
  3838. ' $mod.DoIt$1(1, 2);',
  3839. ' DoIt$2(1, 2, 3);',
  3840. ' DoIt$3(1, 2, 3, 4);',
  3841. ' DoIt$4(1, 2, 3, 4, 5);',
  3842. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3843. ' };',
  3844. ' $mod.DoIt(1);',
  3845. ' $mod.DoIt$1(1, 2);',
  3846. ' DoIt$2(1, 2, 3);',
  3847. ' DoIt$3(1, 2, 3, 4);',
  3848. '};',
  3849. '']),
  3850. LinesToStr([
  3851. '$mod.DoIt(1);',
  3852. '$mod.DoIt$1(1, 2);',
  3853. '']));
  3854. end;
  3855. procedure TTestModule.TestProc_OverloadUnitCycle;
  3856. begin
  3857. AddModuleWithIntfImplSrc('Unit2.pas',
  3858. LinesToStr([
  3859. 'type',
  3860. ' TObject = class',
  3861. ' procedure DoIt(b: boolean); virtual; abstract;',
  3862. ' procedure DoIt(i: longint); virtual; abstract;',
  3863. ' end;',
  3864. '']),
  3865. 'uses test1;');
  3866. StartUnit(true);
  3867. Add([
  3868. 'interface',
  3869. 'uses unit2;',
  3870. 'type',
  3871. ' TEagle = class(TObject)',
  3872. ' procedure DoIt(b: boolean); override;',
  3873. ' procedure DoIt(i: longint); override;',
  3874. ' end;',
  3875. 'implementation',
  3876. 'procedure TEagle.DoIt(b: boolean); begin end;',
  3877. 'procedure TEagle.DoIt(i: longint); begin end;',
  3878. '']);
  3879. ConvertUnit;
  3880. CheckSource('TestProc_OverloadUnitCycle',
  3881. LinesToStr([ // statements
  3882. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  3883. ' this.DoIt = function (b) {',
  3884. ' };',
  3885. ' this.DoIt$1 = function (i) {',
  3886. ' };',
  3887. '});',
  3888. '']),
  3889. '',
  3890. LinesToStr([
  3891. '']));
  3892. end;
  3893. procedure TTestModule.TestProc_Varargs;
  3894. begin
  3895. StartProgram(false);
  3896. Add([
  3897. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  3898. 'procedure ProcB; varargs; external name ''ProcB'';',
  3899. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  3900. 'function GetIt: longint; begin end;',
  3901. 'begin',
  3902. ' ProcA(1);',
  3903. ' ProcA(1,2);',
  3904. ' ProcA(1,2.0);',
  3905. ' ProcA(1,2,3);',
  3906. ' ProcA(1,''2'');',
  3907. ' ProcA(2,'''');',
  3908. ' ProcA(3,false);',
  3909. ' ProcB;',
  3910. ' ProcB();',
  3911. ' ProcB(4);',
  3912. ' ProcB(''foo'');',
  3913. ' ProcC;',
  3914. ' ProcC();',
  3915. ' ProcC(4);',
  3916. ' ProcC(5,''foo'');',
  3917. ' ProcB(GetIt);',
  3918. ' ProcB(GetIt());',
  3919. ' ProcB(GetIt,GetIt());']);
  3920. ConvertProgram;
  3921. CheckSource('TestProc_Varargs',
  3922. LinesToStr([ // statements
  3923. 'this.GetIt = function () {',
  3924. ' var Result = 0;',
  3925. ' return Result;',
  3926. '};',
  3927. '']),
  3928. LinesToStr([
  3929. 'ProcA(1);',
  3930. 'ProcA(1, 2);',
  3931. 'ProcA(1, 2.0);',
  3932. 'ProcA(1, 2, 3);',
  3933. 'ProcA(1, "2");',
  3934. 'ProcA(2, "");',
  3935. 'ProcA(3, false);',
  3936. 'ProcB();',
  3937. 'ProcB();',
  3938. 'ProcB(4);',
  3939. 'ProcB("foo");',
  3940. 'ProcC(17);',
  3941. 'ProcC(17);',
  3942. 'ProcC(4);',
  3943. 'ProcC(5, "foo");',
  3944. 'ProcB($mod.GetIt());',
  3945. 'ProcB($mod.GetIt());',
  3946. 'ProcB($mod.GetIt(), $mod.GetIt());',
  3947. '']));
  3948. end;
  3949. procedure TTestModule.TestProc_ConstOrder;
  3950. begin
  3951. StartProgram(false);
  3952. Add([
  3953. 'const A = 3;',
  3954. 'const B = A+1;',
  3955. 'procedure DoIt;',
  3956. 'const C = A+1;',
  3957. 'const D = B+1;',
  3958. 'const E = D+C+B+A;',
  3959. 'begin',
  3960. 'end;',
  3961. 'begin'
  3962. ]);
  3963. ConvertProgram;
  3964. CheckSource('TestProc_ConstOrder',
  3965. LinesToStr([ // statements
  3966. 'this.A = 3;',
  3967. 'this.B = 3 + 1;',
  3968. 'var C = 3 + 1;',
  3969. 'var D = 4 + 1;',
  3970. 'var E = 5 + 4 + 4 + 3;',
  3971. 'this.DoIt = function () {',
  3972. '};',
  3973. '']),
  3974. LinesToStr([
  3975. ''
  3976. ]));
  3977. end;
  3978. procedure TTestModule.TestProc_DuplicateConst;
  3979. begin
  3980. StartProgram(false);
  3981. Add([
  3982. 'const A = 1;',
  3983. 'procedure DoIt;',
  3984. 'const A = 2;',
  3985. ' procedure SubIt;',
  3986. ' const A = 21;',
  3987. ' begin',
  3988. ' end;',
  3989. 'begin',
  3990. 'end;',
  3991. 'procedure DoSome;',
  3992. 'const A = 3;',
  3993. 'begin',
  3994. 'end;',
  3995. 'begin'
  3996. ]);
  3997. ConvertProgram;
  3998. CheckSource('TestProc_DuplicateConst',
  3999. LinesToStr([ // statements
  4000. 'this.A = 1;',
  4001. 'var A$1 = 2;',
  4002. 'var A$2 = 21;',
  4003. 'this.DoIt = function () {',
  4004. ' function SubIt() {',
  4005. ' };',
  4006. '};',
  4007. 'var A$3 = 3;',
  4008. 'this.DoSome = function () {',
  4009. '};',
  4010. '']),
  4011. LinesToStr([
  4012. ''
  4013. ]));
  4014. end;
  4015. procedure TTestModule.TestProc_LocalVarAbsolute;
  4016. begin
  4017. StartProgram(false);
  4018. Add([
  4019. 'type',
  4020. ' TObject = class',
  4021. ' Index: longint;',
  4022. ' procedure DoAbs(Item: pointer);',
  4023. ' end;',
  4024. 'procedure TObject.DoAbs(Item: pointer);',
  4025. 'var',
  4026. ' o: TObject absolute Item;',
  4027. 'begin',
  4028. ' if o.Index<o.Index then o.Index:=o.Index;',
  4029. 'end;',
  4030. 'procedure DoIt(i: longint; p: pointer);',
  4031. 'var',
  4032. ' d: double absolute i;',
  4033. ' s: string absolute d;',
  4034. ' oi: TObject absolute i;',
  4035. ' op: TObject absolute p;',
  4036. 'begin',
  4037. ' if d=d then d:=d;',
  4038. ' if s=s then s:=s;',
  4039. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4040. ' if op.Index=op.Index then op.Index:=op.Index;',
  4041. 'end;',
  4042. 'begin']);
  4043. ConvertProgram;
  4044. CheckSource('TestProc_LocalVarAbsolute',
  4045. LinesToStr([ // statements
  4046. 'rtl.createClass($mod, "TObject", null, function () {',
  4047. ' this.$init = function () {',
  4048. ' this.Index = 0;',
  4049. ' };',
  4050. ' this.$final = function () {',
  4051. ' };',
  4052. ' this.DoAbs = function (Item) {',
  4053. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4054. ' };',
  4055. '});',
  4056. 'this.DoIt = function (i, p) {',
  4057. ' if (i === i) i = i;',
  4058. ' if (i === i) i = i;',
  4059. ' if (i.Index < i.Index) i.Index = i.Index;',
  4060. ' if (p.Index === p.Index) p.Index = p.Index;',
  4061. '};'
  4062. ]),
  4063. LinesToStr([
  4064. ]));
  4065. end;
  4066. procedure TTestModule.TestProc_ReservedWords;
  4067. begin
  4068. StartProgram(false);
  4069. Add([
  4070. 'procedure Date(ArrayBuffer: longint);',
  4071. 'const',
  4072. ' NaN: longint = 3;',
  4073. 'var',
  4074. ' &Boolean: longint;',
  4075. ' procedure Error(ArrayBuffer: longint);',
  4076. ' begin',
  4077. ' end;',
  4078. 'begin',
  4079. ' Nan:=&bOolean;',
  4080. 'end;',
  4081. 'begin',
  4082. ' Date(1);']);
  4083. ConvertProgram;
  4084. CheckSource('TestProc_ReservedWords',
  4085. LinesToStr([ // statements
  4086. 'var naN = 3;',
  4087. 'this.Date = function (arrayBuffer) {',
  4088. ' var boolean = 0;',
  4089. ' function error(arrayBuffer) {',
  4090. ' };',
  4091. ' naN = boolean;',
  4092. '};',
  4093. '']),
  4094. LinesToStr([
  4095. ' $mod.Date(1);'
  4096. ]));
  4097. end;
  4098. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4099. begin
  4100. StartProgram(false);
  4101. Add([
  4102. '{$mode objfpc}',
  4103. 'type',
  4104. ' TFunc = reference to function(x: word): word;',
  4105. 'var Func: TFunc;',
  4106. 'procedure DoIt(a: word);',
  4107. 'begin',
  4108. ' Func:=function(b:word): word',
  4109. ' begin',
  4110. ' Result:=a+b;',
  4111. ' exit(b);',
  4112. ' exit(Result);',
  4113. ' end;',// test semicolon
  4114. ' a:=3;',
  4115. 'end;',
  4116. 'begin',
  4117. ' Func:=function(c:word):word begin',
  4118. ' Result:=3+c;',
  4119. ' exit(c);',
  4120. ' exit(Result);',
  4121. ' end;']);
  4122. ConvertProgram;
  4123. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4124. LinesToStr([ // statements
  4125. 'this.Func = null;',
  4126. 'this.DoIt = function (a) {',
  4127. ' $mod.Func = function (b) {',
  4128. ' var Result = 0;',
  4129. ' Result = a + b;',
  4130. ' return b;',
  4131. ' return Result;',
  4132. ' return Result;',
  4133. ' };',
  4134. ' a = 3;',
  4135. '};',
  4136. '']),
  4137. LinesToStr([
  4138. '$mod.Func = function (c) {',
  4139. ' var Result = 0;',
  4140. ' Result = 3 + c;',
  4141. ' return c;',
  4142. ' return Result;',
  4143. ' return Result;',
  4144. '};',
  4145. '']));
  4146. end;
  4147. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4148. begin
  4149. StartProgram(false);
  4150. Add([
  4151. '{$mode delphi}',
  4152. 'type',
  4153. ' TProc = reference to procedure(x: word);',
  4154. 'procedure DoIt(a: word);',
  4155. 'var Proc: TProc;',
  4156. 'begin',
  4157. ' Proc:=procedure(b:word) begin end;',
  4158. 'end;',
  4159. 'var Proc: TProc;',
  4160. 'begin',
  4161. ' Proc:=procedure(c:word) begin end;',
  4162. '']);
  4163. ConvertProgram;
  4164. CheckSource('TestAnonymousProc_Assign_Delphi',
  4165. LinesToStr([ // statements
  4166. 'this.DoIt = function (a) {',
  4167. ' var Proc = null;',
  4168. ' Proc = function (b) {',
  4169. ' };',
  4170. '};',
  4171. 'this.Proc = null;',
  4172. '']),
  4173. LinesToStr([
  4174. '$mod.Proc = function (c) {',
  4175. '};',
  4176. '']));
  4177. end;
  4178. procedure TTestModule.TestAnonymousProc_Arg;
  4179. begin
  4180. StartProgram(false);
  4181. Add([
  4182. 'type',
  4183. ' TProc = reference to procedure;',
  4184. ' TFunc = reference to function(x: word): word;',
  4185. 'procedure DoMore(f,g: TProc);',
  4186. 'begin',
  4187. 'end;',
  4188. 'procedure DoOdd(v: jsvalue);',
  4189. 'begin',
  4190. 'end;',
  4191. 'procedure DoIt(f: TFunc);',
  4192. 'begin',
  4193. ' DoIt(function(b:word): word',
  4194. ' begin',
  4195. ' Result:=1+b;',
  4196. ' end);',
  4197. ' DoMore(procedure begin end, procedure begin end);',
  4198. ' DoOdd(procedure begin end);',
  4199. 'end;',
  4200. 'begin',
  4201. ' DoMore(procedure begin end,',
  4202. ' procedure assembler asm',
  4203. ' console.log("c");',
  4204. ' end);',
  4205. '']);
  4206. ConvertProgram;
  4207. CheckSource('TestAnonymousProc_Arg',
  4208. LinesToStr([ // statements
  4209. 'this.DoMore = function (f, g) {',
  4210. '};',
  4211. 'this.DoOdd = function (v) {',
  4212. '};',
  4213. 'this.DoIt = function (f) {',
  4214. ' $mod.DoIt(function (b) {',
  4215. ' var Result = 0;',
  4216. ' Result = 1 + b;',
  4217. ' return Result;',
  4218. ' });',
  4219. ' $mod.DoMore(function () {',
  4220. ' }, function () {',
  4221. ' });',
  4222. ' $mod.DoOdd(function () {',
  4223. ' });',
  4224. '};',
  4225. '']),
  4226. LinesToStr([
  4227. '$mod.DoMore(function () {',
  4228. '}, function () {',
  4229. ' console.log("c");',
  4230. '});',
  4231. '']));
  4232. end;
  4233. procedure TTestModule.TestAnonymousProc_Typecast;
  4234. begin
  4235. StartProgram(false);
  4236. Add([
  4237. 'type',
  4238. ' TProc = reference to procedure(w: word);',
  4239. ' TArr = array of word;',
  4240. ' TFuncArr = reference to function: TArr;',
  4241. 'procedure DoIt(p: TProc);',
  4242. 'var',
  4243. ' w: word;',
  4244. ' a: TArr;',
  4245. 'begin',
  4246. ' p:=TProc(procedure(b: smallint) begin end);',
  4247. ' a:=TFuncArr(function: TArr begin end)();',
  4248. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4249. 'end;',
  4250. 'begin']);
  4251. ConvertProgram;
  4252. CheckSource('TestAnonymousProc_Typecast',
  4253. LinesToStr([ // statements
  4254. 'this.DoIt = function (p) {',
  4255. ' var w = 0;',
  4256. ' var a = [];',
  4257. ' p = function (b) {',
  4258. ' };',
  4259. ' a = function () {',
  4260. ' var Result = [];',
  4261. ' return Result;',
  4262. ' }();',
  4263. ' w = function () {',
  4264. ' var Result = [];',
  4265. ' return Result;',
  4266. ' }()[3];',
  4267. '};',
  4268. '']),
  4269. LinesToStr([
  4270. '']));
  4271. end;
  4272. procedure TTestModule.TestAnonymousProc_With;
  4273. begin
  4274. StartProgram(false);
  4275. Add([
  4276. 'type',
  4277. ' TProc = reference to procedure(w: word);',
  4278. ' TObject = class',
  4279. ' b: boolean;',
  4280. ' end;',
  4281. 'var',
  4282. ' p: TProc;',
  4283. ' bird: TObject;',
  4284. 'begin',
  4285. ' with bird do',
  4286. ' p:=procedure(w: word)',
  4287. ' begin',
  4288. ' b:=w>2;',
  4289. ' end;',
  4290. '']);
  4291. ConvertProgram;
  4292. CheckSource('TestAnonymousProc_With',
  4293. LinesToStr([ // statements
  4294. 'rtl.createClass($mod, "TObject", null, function () {',
  4295. ' this.$init = function () {',
  4296. ' this.b = false;',
  4297. ' };',
  4298. ' this.$final = function () {',
  4299. ' };',
  4300. '});',
  4301. 'this.p = null;',
  4302. 'this.bird = null;',
  4303. '']),
  4304. LinesToStr([
  4305. 'var $with1 = $mod.bird;',
  4306. '$mod.p = function (w) {',
  4307. ' $with1.b = w > 2;',
  4308. '};',
  4309. '']));
  4310. end;
  4311. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4312. begin
  4313. StartProgram(false);
  4314. Add([
  4315. 'type',
  4316. ' TProc = reference to procedure;',
  4317. ' TObject = class',
  4318. ' b: boolean;',
  4319. ' end;',
  4320. 'procedure DoIt;',
  4321. 'var',
  4322. ' p: TProc;',
  4323. 'begin',
  4324. ' try',
  4325. ' except',
  4326. ' on E: TObject do',
  4327. ' p:=procedure',
  4328. ' begin',
  4329. ' E.b:=true;',
  4330. ' end;',
  4331. ' end;',
  4332. 'end;',
  4333. 'begin']);
  4334. ConvertProgram;
  4335. CheckSource('TestAnonymousProc_ExceptOn',
  4336. LinesToStr([ // statements
  4337. 'rtl.createClass($mod, "TObject", null, function () {',
  4338. ' this.$init = function () {',
  4339. ' this.b = false;',
  4340. ' };',
  4341. ' this.$final = function () {',
  4342. ' };',
  4343. '});',
  4344. 'this.DoIt = function () {',
  4345. ' var p = null;',
  4346. ' try {} catch ($e) {',
  4347. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4348. ' var E = $e;',
  4349. ' p = function () {',
  4350. ' E.b = true;',
  4351. ' };',
  4352. ' } else throw $e',
  4353. ' };',
  4354. '};',
  4355. '']),
  4356. LinesToStr([
  4357. '']));
  4358. end;
  4359. procedure TTestModule.TestAnonymousProc_Nested;
  4360. begin
  4361. StartProgram(false);
  4362. Add([
  4363. 'type',
  4364. ' TProc = reference to procedure;',
  4365. ' TObject = class',
  4366. ' i: byte;',
  4367. ' procedure DoIt;',
  4368. ' end;',
  4369. 'procedure TObject.DoIt;',
  4370. 'var',
  4371. ' p: TProc;',
  4372. ' procedure Sub;',
  4373. ' begin',
  4374. ' p:=procedure',
  4375. ' begin',
  4376. ' i:=3;',
  4377. ' Self.i:=4;',
  4378. ' p:=procedure',
  4379. ' procedure SubSub;',
  4380. ' begin',
  4381. ' i:=13;',
  4382. ' Self.i:=14;',
  4383. ' end;',
  4384. ' begin',
  4385. ' i:=13;',
  4386. ' Self.i:=14;',
  4387. ' end;',
  4388. ' end;',
  4389. ' end;',
  4390. 'begin',
  4391. 'end;',
  4392. 'begin']);
  4393. ConvertProgram;
  4394. CheckSource('TestAnonymousProc_Nested',
  4395. LinesToStr([ // statements
  4396. 'rtl.createClass($mod, "TObject", null, function () {',
  4397. ' this.$init = function () {',
  4398. ' this.i = 0;',
  4399. ' };',
  4400. ' this.$final = function () {',
  4401. ' };',
  4402. ' this.DoIt = function () {',
  4403. ' var $Self = this;',
  4404. ' var p = null;',
  4405. ' function Sub() {',
  4406. ' p = function () {',
  4407. ' $Self.i = 3;',
  4408. ' $Self.i = 4;',
  4409. ' p = function () {',
  4410. ' function SubSub() {',
  4411. ' $Self.i = 13;',
  4412. ' $Self.i = 14;',
  4413. ' };',
  4414. ' $Self.i = 13;',
  4415. ' $Self.i = 14;',
  4416. ' };',
  4417. ' };',
  4418. ' };',
  4419. ' };',
  4420. '});',
  4421. '']),
  4422. LinesToStr([
  4423. '']));
  4424. end;
  4425. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4426. begin
  4427. StartProgram(false);
  4428. Add([
  4429. 'type',
  4430. ' TProc = reference to procedure;',
  4431. 'function DoIt: TProc;',
  4432. ' function Sub: TProc;',
  4433. ' begin',
  4434. ' Result:=procedure',
  4435. ' begin',
  4436. ' Sub:=procedure',
  4437. ' procedure SubSub;',
  4438. ' begin',
  4439. ' Result:=nil;',
  4440. ' Sub:=nil;',
  4441. ' DoIt:=nil;',
  4442. ' end;',
  4443. ' begin',
  4444. ' Result:=nil;',
  4445. ' Sub:=nil;',
  4446. ' DoIt:=nil;',
  4447. ' end;',
  4448. ' end;',
  4449. ' end;',
  4450. 'begin',
  4451. 'end;',
  4452. 'begin']);
  4453. ConvertProgram;
  4454. CheckSource('TestAnonymousProc_NestedAssignResult',
  4455. LinesToStr([ // statements
  4456. 'this.DoIt = function () {',
  4457. ' var Result = null;',
  4458. ' function Sub() {',
  4459. ' var Result$1 = null;',
  4460. ' Result$1 = function () {',
  4461. ' Result$1 = function () {',
  4462. ' function SubSub() {',
  4463. ' Result$1 = null;',
  4464. ' Result$1 = null;',
  4465. ' Result = null;',
  4466. ' };',
  4467. ' Result$1 = null;',
  4468. ' Result$1 = null;',
  4469. ' Result = null;',
  4470. ' };',
  4471. ' };',
  4472. ' return Result$1;',
  4473. ' };',
  4474. ' return Result;',
  4475. '};',
  4476. '']),
  4477. LinesToStr([
  4478. '']));
  4479. end;
  4480. procedure TTestModule.TestEnum_Name;
  4481. begin
  4482. StartProgram(false);
  4483. Add('type TMyEnum = (Red, Green, Blue);');
  4484. Add('var e: TMyEnum;');
  4485. Add('var f: TMyEnum = Blue;');
  4486. Add('begin');
  4487. Add(' e:=green;');
  4488. Add(' e:=default(TMyEnum);');
  4489. ConvertProgram;
  4490. CheckSource('TestEnumName',
  4491. LinesToStr([ // statements
  4492. 'this.TMyEnum = {',
  4493. ' "0":"Red",',
  4494. ' Red:0,',
  4495. ' "1":"Green",',
  4496. ' Green:1,',
  4497. ' "2":"Blue",',
  4498. ' Blue:2',
  4499. ' };',
  4500. 'this.e = 0;',
  4501. 'this.f = $mod.TMyEnum.Blue;'
  4502. ]),
  4503. LinesToStr([
  4504. '$mod.e=$mod.TMyEnum.Green;',
  4505. '$mod.e=$mod.TMyEnum.Red;'
  4506. ]));
  4507. end;
  4508. procedure TTestModule.TestEnum_Number;
  4509. begin
  4510. Converter.Options:=Converter.Options+[coEnumNumbers];
  4511. StartProgram(false);
  4512. Add('type TMyEnum = (Red, Green);');
  4513. Add('var');
  4514. Add(' e: TMyEnum;');
  4515. Add(' f: TMyEnum = Green;');
  4516. Add(' i: longint;');
  4517. Add('begin');
  4518. Add(' e:=green;');
  4519. Add(' i:=longint(e);');
  4520. ConvertProgram;
  4521. CheckSource('TestEnumNumber',
  4522. LinesToStr([ // statements
  4523. 'this.TMyEnum = {',
  4524. ' "0":"Red",',
  4525. ' Red:0,',
  4526. ' "1":"Green",',
  4527. ' Green:1',
  4528. ' };',
  4529. 'this.e = 0;',
  4530. 'this.f = 1;',
  4531. 'this.i = 0;'
  4532. ]),
  4533. LinesToStr([
  4534. '$mod.e=1;',
  4535. '$mod.i=$mod.e;'
  4536. ]));
  4537. end;
  4538. procedure TTestModule.TestEnum_ConstFail;
  4539. begin
  4540. StartProgram(false);
  4541. Add([
  4542. 'type TMyEnum = (Red = 100, Green = 101);',
  4543. 'var',
  4544. ' e: TMyEnum;',
  4545. ' f: TMyEnum = Green;',
  4546. 'begin',
  4547. ' e:=green;']);
  4548. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] enum const',3002);
  4549. ConvertProgram;
  4550. end;
  4551. procedure TTestModule.TestEnum_Functions;
  4552. begin
  4553. StartProgram(false);
  4554. Add([
  4555. 'type TMyEnum = (Red, Green);',
  4556. 'procedure DoIt(var e: TMyEnum; var i: word);',
  4557. 'var',
  4558. ' v: longint;',
  4559. ' s: string;',
  4560. 'begin',
  4561. ' val(s,e,v);',
  4562. ' val(s,e,i);',
  4563. 'end;',
  4564. 'var',
  4565. ' e: TMyEnum;',
  4566. ' i: longint;',
  4567. ' s: string;',
  4568. ' b: boolean;',
  4569. 'begin',
  4570. ' i:=ord(red);',
  4571. ' i:=ord(green);',
  4572. ' i:=ord(e);',
  4573. ' i:=ord(b);',
  4574. ' e:=low(tmyenum);',
  4575. ' e:=low(e);',
  4576. ' b:=low(boolean);',
  4577. ' e:=high(tmyenum);',
  4578. ' e:=high(e);',
  4579. ' b:=high(boolean);',
  4580. ' e:=pred(green);',
  4581. ' e:=pred(e);',
  4582. ' b:=pred(b);',
  4583. ' e:=succ(red);',
  4584. ' e:=succ(e);',
  4585. ' b:=succ(b);',
  4586. ' e:=tmyenum(1);',
  4587. ' e:=tmyenum(i);',
  4588. ' s:=str(e);',
  4589. ' str(e,s);',
  4590. ' str(red,s);',
  4591. ' s:=str(e:3);',
  4592. ' writestr(s,e:3,red);',
  4593. ' val(s,e,i);',
  4594. ' e:=TMyEnum(i);',
  4595. ' i:=longint(e);']);
  4596. ConvertProgram;
  4597. CheckSource('TestEnum_Functions',
  4598. LinesToStr([ // statements
  4599. 'this.TMyEnum = {',
  4600. ' "0":"Red",',
  4601. ' Red:0,',
  4602. ' "1":"Green",',
  4603. ' Green:1',
  4604. ' };',
  4605. 'this.DoIt = function (e, i) {',
  4606. ' var v = 0;',
  4607. ' var s = "";',
  4608. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  4609. ' v = w;',
  4610. ' }));',
  4611. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  4612. '};',
  4613. 'this.e = 0;',
  4614. 'this.i = 0;',
  4615. 'this.s = "";',
  4616. 'this.b = false;',
  4617. '']),
  4618. LinesToStr([
  4619. '$mod.i=$mod.TMyEnum.Red;',
  4620. '$mod.i=$mod.TMyEnum.Green;',
  4621. '$mod.i=$mod.e;',
  4622. '$mod.i=$mod.b+0;',
  4623. '$mod.e=$mod.TMyEnum.Red;',
  4624. '$mod.e=$mod.TMyEnum.Red;',
  4625. '$mod.b=false;',
  4626. '$mod.e=$mod.TMyEnum.Green;',
  4627. '$mod.e=$mod.TMyEnum.Green;',
  4628. '$mod.b=true;',
  4629. '$mod.e=$mod.TMyEnum.Green-1;',
  4630. '$mod.e=$mod.e-1;',
  4631. '$mod.b=false;',
  4632. '$mod.e=$mod.TMyEnum.Red+1;',
  4633. '$mod.e=$mod.e+1;',
  4634. '$mod.b=true;',
  4635. '$mod.e=1;',
  4636. '$mod.e=$mod.i;',
  4637. '$mod.s = $mod.TMyEnum[$mod.e];',
  4638. '$mod.s = $mod.TMyEnum[$mod.e];',
  4639. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  4640. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  4641. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  4642. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  4643. ' $mod.i = v;',
  4644. '});',
  4645. '$mod.e=$mod.i;',
  4646. '$mod.i=$mod.e;',
  4647. '']));
  4648. end;
  4649. procedure TTestModule.TestEnum_AsParams;
  4650. begin
  4651. StartProgram(false);
  4652. Add('type TEnum = (Red,Blue);');
  4653. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  4654. Add('var vJ: TEnum;');
  4655. Add('begin');
  4656. Add(' vg:=vg;');
  4657. Add(' vj:=vh;');
  4658. Add(' vi:=vi;');
  4659. Add(' doit(vg,vg,vg);');
  4660. Add(' doit(vh,vh,vj);');
  4661. Add(' doit(vi,vi,vi);');
  4662. Add(' doit(vj,vj,vj);');
  4663. Add('end;');
  4664. Add('var i: TEnum;');
  4665. Add('begin');
  4666. Add(' doit(i,i,i);');
  4667. ConvertProgram;
  4668. CheckSource('TestEnum_AsParams',
  4669. LinesToStr([ // statements
  4670. 'this.TEnum = {',
  4671. ' "0": "Red",',
  4672. ' Red: 0,',
  4673. ' "1": "Blue",',
  4674. ' Blue: 1',
  4675. '};',
  4676. 'this.DoIt = function (vG,vH,vI) {',
  4677. ' var vJ = 0;',
  4678. ' vG = vG;',
  4679. ' vJ = vH;',
  4680. ' vI.set(vI.get());',
  4681. ' $mod.DoIt(vG, vG, {',
  4682. ' get: function () {',
  4683. ' return vG;',
  4684. ' },',
  4685. ' set: function (v) {',
  4686. ' vG = v;',
  4687. ' }',
  4688. ' });',
  4689. ' $mod.DoIt(vH, vH, {',
  4690. ' get: function () {',
  4691. ' return vJ;',
  4692. ' },',
  4693. ' set: function (v) {',
  4694. ' vJ = v;',
  4695. ' }',
  4696. ' });',
  4697. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4698. ' $mod.DoIt(vJ, vJ, {',
  4699. ' get: function () {',
  4700. ' return vJ;',
  4701. ' },',
  4702. ' set: function (v) {',
  4703. ' vJ = v;',
  4704. ' }',
  4705. ' });',
  4706. '};',
  4707. 'this.i = 0;'
  4708. ]),
  4709. LinesToStr([
  4710. '$mod.DoIt($mod.i,$mod.i,{',
  4711. ' p: $mod,',
  4712. ' get: function () {',
  4713. ' return this.p.i;',
  4714. ' },',
  4715. ' set: function (v) {',
  4716. ' this.p.i = v;',
  4717. ' }',
  4718. '});'
  4719. ]));
  4720. end;
  4721. procedure TTestModule.TestEnumRange_Array;
  4722. begin
  4723. StartProgram(false);
  4724. Add([
  4725. 'type',
  4726. ' TEnum = (Red, Green, Blue);',
  4727. ' TEnumRg = green..blue;',
  4728. ' TArr = array[TEnumRg] of byte;',
  4729. ' TArr2 = array[green..blue] of byte;',
  4730. 'var',
  4731. ' a: TArr;',
  4732. ' b: TArr = (3,4);',
  4733. ' c: TArr2 = (5,6);',
  4734. 'begin',
  4735. ' a[green] := b[blue];',
  4736. ' c[green] := c[blue];',
  4737. '']);
  4738. ConvertProgram;
  4739. CheckSource('TestEnumRange_Array',
  4740. LinesToStr([ // statements
  4741. 'this.TEnum = {',
  4742. ' "0": "Red",',
  4743. ' Red: 0,',
  4744. ' "1": "Green",',
  4745. ' Green: 1,',
  4746. ' "2": "Blue",',
  4747. ' Blue: 2',
  4748. '};',
  4749. 'this.a = rtl.arraySetLength(null, 0, 2);',
  4750. 'this.b = [3, 4];',
  4751. 'this.c = [5, 6];',
  4752. '']),
  4753. LinesToStr([
  4754. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  4755. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  4756. '']));
  4757. end;
  4758. procedure TTestModule.TestEnum_ForIn;
  4759. begin
  4760. StartProgram(false);
  4761. Add([
  4762. 'type',
  4763. ' TEnum = (Red, Green, Blue);',
  4764. ' TEnumRg = green..blue;',
  4765. ' TArr = array[TEnum] of byte;',
  4766. ' TArrRg = array[TEnumRg] of byte;',
  4767. 'var',
  4768. ' e: TEnum;',
  4769. ' a1: TArr = (3,4,5);',
  4770. ' a2: TArrRg = (11,12);',
  4771. ' b: byte;',
  4772. 'begin',
  4773. ' for e in TEnum do ;',
  4774. ' for e in TEnumRg do ;',
  4775. ' for e in TArr do ;',
  4776. ' for e in TArrRg do ;',
  4777. ' for b in a1 do ;',
  4778. ' for b in a2 do ;',
  4779. '']);
  4780. ConvertProgram;
  4781. CheckSource('TestEnum_ForIn',
  4782. LinesToStr([ // statements
  4783. 'this.TEnum = {',
  4784. ' "0": "Red",',
  4785. ' Red: 0,',
  4786. ' "1": "Green",',
  4787. ' Green: 1,',
  4788. ' "2": "Blue",',
  4789. ' Blue: 2',
  4790. '};',
  4791. 'this.e = 0;',
  4792. 'this.a1 = [3, 4, 5];',
  4793. 'this.a2 = [11, 12];',
  4794. 'this.b = 0;',
  4795. '']),
  4796. LinesToStr([
  4797. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  4798. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  4799. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  4800. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  4801. ' for (var $in1 = $mod.a1, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.b = $in1[$l2];',
  4802. ' for (var $in4 = $mod.a2, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.b = $in4[$l5];',
  4803. '']));
  4804. end;
  4805. procedure TTestModule.TestEnum_ScopedNumber;
  4806. begin
  4807. Converter.Options:=Converter.Options+[coEnumNumbers];
  4808. StartProgram(false);
  4809. Add([
  4810. 'type',
  4811. ' TEnum = (Red, Green);',
  4812. 'var',
  4813. ' e: TEnum;',
  4814. 'begin',
  4815. ' e:=TEnum.Green;',
  4816. '']);
  4817. ConvertProgram;
  4818. CheckSource('TestEnum_ScopedNumber',
  4819. LinesToStr([ // statements
  4820. 'this.TEnum = {',
  4821. ' "0": "Red",',
  4822. ' Red: 0,',
  4823. ' "1": "Green",',
  4824. ' Green: 1',
  4825. '};',
  4826. 'this.e = 0;',
  4827. '']),
  4828. LinesToStr([
  4829. '$mod.e = 1;']));
  4830. end;
  4831. procedure TTestModule.TestEnum_InFunction;
  4832. begin
  4833. StartProgram(false);
  4834. Add([
  4835. 'const TEnum = 3;',
  4836. 'procedure DoIt;',
  4837. 'type',
  4838. ' TEnum = (Red, Green, Blue);',
  4839. ' procedure Sub;',
  4840. ' type',
  4841. ' TEnumSub = (Left, Right);',
  4842. ' var',
  4843. ' es: TEnumSub;',
  4844. ' begin',
  4845. ' es:=Left;',
  4846. ' end;',
  4847. 'var',
  4848. ' e, e2: TEnum;',
  4849. 'begin',
  4850. ' if e in [red,blue] then e2:=e;',
  4851. 'end;',
  4852. 'begin']);
  4853. ConvertProgram;
  4854. CheckSource('TestEnum_InFunction',
  4855. LinesToStr([ // statements
  4856. 'this.TEnum = 3;',
  4857. 'var TEnum$1 = {',
  4858. ' "0":"Red",',
  4859. ' Red:0,',
  4860. ' "1":"Green",',
  4861. ' Green:1,',
  4862. ' "2":"Blue",',
  4863. ' Blue:2',
  4864. ' };',
  4865. 'var TEnumSub = {',
  4866. ' "0": "Left",',
  4867. ' Left: 0,',
  4868. ' "1": "Right",',
  4869. ' Right: 1',
  4870. '};',
  4871. 'this.DoIt = function () {',
  4872. ' function Sub() {',
  4873. ' var es = 0;',
  4874. ' es = TEnumSub.Left;',
  4875. ' };',
  4876. ' var e = 0;',
  4877. ' var e2 = 0;',
  4878. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  4879. '};',
  4880. '']),
  4881. LinesToStr([
  4882. '']));
  4883. end;
  4884. procedure TTestModule.TestSet_Enum;
  4885. begin
  4886. StartProgram(false);
  4887. Add([
  4888. 'type',
  4889. ' TColor = (Red, Green, Blue);',
  4890. ' TColors = set of TColor;',
  4891. 'var',
  4892. ' c: TColor;',
  4893. ' s: TColors;',
  4894. ' t: TColors = [];',
  4895. ' u: TColors = [Red];',
  4896. 'begin',
  4897. ' s:=[];',
  4898. ' s:=[Green];',
  4899. ' s:=[Green,Blue];',
  4900. ' s:=[Red..Blue];',
  4901. ' s:=[Red,Green..Blue];',
  4902. ' s:=[Red,c];',
  4903. ' s:=t;',
  4904. ' s:=default(TColors);',
  4905. '']);
  4906. ConvertProgram;
  4907. CheckSource('TestSet',
  4908. LinesToStr([ // statements
  4909. 'this.TColor = {',
  4910. ' "0":"Red",',
  4911. ' Red:0,',
  4912. ' "1":"Green",',
  4913. ' Green:1,',
  4914. ' "2":"Blue",',
  4915. ' Blue:2',
  4916. ' };',
  4917. 'this.c = 0;',
  4918. 'this.s = {};',
  4919. 'this.t = {};',
  4920. 'this.u = rtl.createSet($mod.TColor.Red);'
  4921. ]),
  4922. LinesToStr([
  4923. '$mod.s={};',
  4924. '$mod.s=rtl.createSet($mod.TColor.Green);',
  4925. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  4926. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  4927. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  4928. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  4929. '$mod.s=rtl.refSet($mod.t);',
  4930. '$mod.s={};',
  4931. '']));
  4932. end;
  4933. procedure TTestModule.TestSet_Operators;
  4934. begin
  4935. StartProgram(false);
  4936. Add('type');
  4937. Add(' TColor = (Red, Green, Blue);');
  4938. Add(' TColors = set of tcolor;');
  4939. Add('var');
  4940. Add(' vC: TColor;');
  4941. Add(' vS: TColors;');
  4942. Add(' vT: TColors;');
  4943. Add(' vU: TColors;');
  4944. Add(' B: boolean;');
  4945. Add('begin');
  4946. Add(' include(vs,green);');
  4947. Add(' exclude(vs,vc);');
  4948. Add(' vs:=vt+vu;');
  4949. Add(' vs:=vt+[red];');
  4950. Add(' vs:=[red]+vt;');
  4951. Add(' vs:=[red]+[green];');
  4952. Add(' vs:=vt-vu;');
  4953. Add(' vs:=vt-[red];');
  4954. Add(' vs:=[red]-vt;');
  4955. Add(' vs:=[red]-[green];');
  4956. Add(' vs:=vt*vu;');
  4957. Add(' vs:=vt*[red];');
  4958. Add(' vs:=[red]*vt;');
  4959. Add(' vs:=[red]*[green];');
  4960. Add(' vs:=vt><vu;');
  4961. Add(' vs:=vt><[red];');
  4962. Add(' vs:=[red]><vt;');
  4963. Add(' vs:=[red]><[green];');
  4964. Add(' b:=vt=vu;');
  4965. Add(' b:=vt=[red];');
  4966. Add(' b:=[red]=vt;');
  4967. Add(' b:=[red]=[green];');
  4968. Add(' b:=vt<>vu;');
  4969. Add(' b:=vt<>[red];');
  4970. Add(' b:=[red]<>vt;');
  4971. Add(' b:=[red]<>[green];');
  4972. Add(' b:=vt<=vu;');
  4973. Add(' b:=vt<=[red];');
  4974. Add(' b:=[red]<=vt;');
  4975. Add(' b:=[red]<=[green];');
  4976. Add(' b:=vt>=vu;');
  4977. Add(' b:=vt>=[red];');
  4978. Add(' b:=[red]>=vt;');
  4979. Add(' b:=[red]>=[green];');
  4980. ConvertProgram;
  4981. CheckSource('TestSet_Operators',
  4982. LinesToStr([ // statements
  4983. 'this.TColor = {',
  4984. ' "0":"Red",',
  4985. ' Red:0,',
  4986. ' "1":"Green",',
  4987. ' Green:1,',
  4988. ' "2":"Blue",',
  4989. ' Blue:2',
  4990. ' };',
  4991. 'this.vC = 0;',
  4992. 'this.vS = {};',
  4993. 'this.vT = {};',
  4994. 'this.vU = {};',
  4995. 'this.B = false;'
  4996. ]),
  4997. LinesToStr([
  4998. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  4999. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5000. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5001. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5002. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5003. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5004. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5005. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5006. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5007. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5008. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5009. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5010. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5011. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5012. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5013. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5014. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5015. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5016. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5017. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5018. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5019. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5020. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5021. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5022. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5023. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5024. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5025. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5026. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5027. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5028. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5029. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5030. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5031. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5032. '']));
  5033. end;
  5034. procedure TTestModule.TestSet_Operator_In;
  5035. begin
  5036. StartProgram(false);
  5037. Add('type');
  5038. Add(' TColor = (Red, Green, Blue);');
  5039. Add(' TColors = set of tcolor;');
  5040. Add('var');
  5041. Add(' vC: tcolor;');
  5042. Add(' vT: tcolors;');
  5043. Add(' B: boolean;');
  5044. Add('begin');
  5045. Add(' b:=red in vt;');
  5046. Add(' b:=vc in vt;');
  5047. Add(' b:=green in [red..blue];');
  5048. Add(' b:=vc in [red..blue];');
  5049. Add(' ');
  5050. Add(' if red in vt then ;');
  5051. Add(' while vC in vt do ;');
  5052. Add(' repeat');
  5053. Add(' until vC in vt;');
  5054. ConvertProgram;
  5055. CheckSource('TestSet_Operator_In',
  5056. LinesToStr([ // statements
  5057. 'this.TColor = {',
  5058. ' "0":"Red",',
  5059. ' Red:0,',
  5060. ' "1":"Green",',
  5061. ' Green:1,',
  5062. ' "2":"Blue",',
  5063. ' Blue:2',
  5064. ' };',
  5065. 'this.vC = 0;',
  5066. 'this.vT = {};',
  5067. 'this.B = false;'
  5068. ]),
  5069. LinesToStr([
  5070. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5071. '$mod.B = $mod.vC in $mod.vT;',
  5072. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5073. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5074. 'if ($mod.TColor.Red in $mod.vT) ;',
  5075. 'while ($mod.vC in $mod.vT) {',
  5076. '};',
  5077. 'do {',
  5078. '} while (!($mod.vC in $mod.vT));',
  5079. '']));
  5080. end;
  5081. procedure TTestModule.TestSet_Functions;
  5082. begin
  5083. StartProgram(false);
  5084. Add('type');
  5085. Add(' TMyEnum = (Red, Green);');
  5086. Add(' TMyEnums = set of TMyEnum;');
  5087. Add('var');
  5088. Add(' e: TMyEnum;');
  5089. Add(' s: TMyEnums;');
  5090. Add('begin');
  5091. Add(' e:=Low(TMyEnums);');
  5092. Add(' e:=Low(s);');
  5093. Add(' e:=High(TMyEnums);');
  5094. Add(' e:=High(s);');
  5095. ConvertProgram;
  5096. CheckSource('TestSetFunctions',
  5097. LinesToStr([ // statements
  5098. 'this.TMyEnum = {',
  5099. ' "0":"Red",',
  5100. ' Red:0,',
  5101. ' "1":"Green",',
  5102. ' Green:1',
  5103. ' };',
  5104. 'this.e = 0;',
  5105. 'this.s = {};'
  5106. ]),
  5107. LinesToStr([
  5108. '$mod.e=$mod.TMyEnum.Red;',
  5109. '$mod.e=$mod.TMyEnum.Red;',
  5110. '$mod.e=$mod.TMyEnum.Green;',
  5111. '$mod.e=$mod.TMyEnum.Green;',
  5112. '']));
  5113. end;
  5114. procedure TTestModule.TestSet_PassAsArgClone;
  5115. begin
  5116. StartProgram(false);
  5117. Add('type');
  5118. Add(' TMyEnum = (Red, Green);');
  5119. Add(' TMyEnums = set of TMyEnum;');
  5120. Add('procedure DoDefault(s: tmyenums); begin end;');
  5121. Add('procedure DoConst(const s: tmyenums); begin end;');
  5122. Add('var');
  5123. Add(' aSet: tmyenums;');
  5124. Add('begin');
  5125. Add(' dodefault(aset);');
  5126. Add(' doconst(aset);');
  5127. ConvertProgram;
  5128. CheckSource('TestSetFunctions',
  5129. LinesToStr([ // statements
  5130. 'this.TMyEnum = {',
  5131. ' "0":"Red",',
  5132. ' Red:0,',
  5133. ' "1":"Green",',
  5134. ' Green:1',
  5135. ' };',
  5136. 'this.DoDefault = function (s) {',
  5137. '};',
  5138. 'this.DoConst = function (s) {',
  5139. '};',
  5140. 'this.aSet = {};'
  5141. ]),
  5142. LinesToStr([
  5143. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5144. '$mod.DoConst($mod.aSet);',
  5145. '']));
  5146. end;
  5147. procedure TTestModule.TestSet_AsParams;
  5148. begin
  5149. StartProgram(false);
  5150. Add([
  5151. 'type TEnum = (Red,Blue);',
  5152. 'type TEnums = set of TEnum;',
  5153. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5154. 'var vJ: TEnums;',
  5155. 'begin',
  5156. ' Include(vg,red);',
  5157. ' Include(result,blue);',
  5158. ' vg:=vg;',
  5159. ' vj:=vh;',
  5160. ' vi:=vi;',
  5161. ' doit(vg,vg,vg);',
  5162. ' doit(vh,vh,vj);',
  5163. ' doit(vi,vi,vi);',
  5164. ' doit(vj,vj,vj);',
  5165. 'end;',
  5166. 'var i: TEnums;',
  5167. 'begin',
  5168. ' doit(i,i,i);']);
  5169. ConvertProgram;
  5170. CheckSource('TestSet_AsParams',
  5171. LinesToStr([ // statements
  5172. 'this.TEnum = {',
  5173. ' "0": "Red",',
  5174. ' Red: 0,',
  5175. ' "1": "Blue",',
  5176. ' Blue: 1',
  5177. '};',
  5178. 'this.DoIt = function (vG,vH,vI) {',
  5179. ' var Result = {};',
  5180. ' var vJ = {};',
  5181. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5182. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5183. ' vG = rtl.refSet(vG);',
  5184. ' vJ = rtl.refSet(vH);',
  5185. ' vI.set(rtl.refSet(vI.get()));',
  5186. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5187. ' get: function () {',
  5188. ' return vG;',
  5189. ' },',
  5190. ' set: function (v) {',
  5191. ' vG = v;',
  5192. ' }',
  5193. ' });',
  5194. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5195. ' get: function () {',
  5196. ' return vJ;',
  5197. ' },',
  5198. ' set: function (v) {',
  5199. ' vJ = v;',
  5200. ' }',
  5201. ' });',
  5202. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5203. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5204. ' get: function () {',
  5205. ' return vJ;',
  5206. ' },',
  5207. ' set: function (v) {',
  5208. ' vJ = v;',
  5209. ' }',
  5210. ' });',
  5211. ' return Result;',
  5212. '};',
  5213. 'this.i = {};'
  5214. ]),
  5215. LinesToStr([
  5216. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5217. ' p: $mod,',
  5218. ' get: function () {',
  5219. ' return this.p.i;',
  5220. ' },',
  5221. ' set: function (v) {',
  5222. ' this.p.i = v;',
  5223. ' }',
  5224. '});'
  5225. ]));
  5226. end;
  5227. procedure TTestModule.TestSet_Property;
  5228. begin
  5229. StartProgram(false);
  5230. Add('type');
  5231. Add(' TEnum = (Red,Blue);');
  5232. Add(' TEnums = set of TEnum;');
  5233. Add(' TObject = class');
  5234. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5235. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5236. Add(' property Colors: TEnums read GetColors write SetColors;');
  5237. Add(' end;');
  5238. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5239. Add('begin end;');
  5240. Add('var Obj: TObject;');
  5241. Add('begin');
  5242. Add(' Include(Obj.Colors,Red);');
  5243. Add(' Exclude(Obj.Colors,Red);');
  5244. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5245. ConvertProgram;
  5246. CheckSource('TestSet_Property',
  5247. LinesToStr([ // statements
  5248. 'this.TEnum = {',
  5249. ' "0": "Red",',
  5250. ' Red: 0,',
  5251. ' "1": "Blue",',
  5252. ' Blue: 1',
  5253. '};',
  5254. 'rtl.createClass($mod, "TObject", null, function () {',
  5255. ' this.$init = function () {',
  5256. ' };',
  5257. ' this.$final = function () {',
  5258. ' };',
  5259. '});',
  5260. 'this.DoIt = function (i, j, k, l) {',
  5261. '};',
  5262. 'this.Obj = null;',
  5263. '']),
  5264. LinesToStr([
  5265. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5266. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5267. '']));
  5268. end;
  5269. procedure TTestModule.TestSet_EnumConst;
  5270. begin
  5271. StartProgram(false);
  5272. Add([
  5273. 'type',
  5274. ' TEnum = (Red,Blue);',
  5275. ' TEnums = set of TEnum;',
  5276. 'const',
  5277. ' Orange = red;',
  5278. 'var',
  5279. ' Enum: tenum;',
  5280. ' Enums: tenums;',
  5281. 'begin',
  5282. ' Include(enums,orange);',
  5283. ' Exclude(enums,orange);',
  5284. ' if orange in enums then;',
  5285. ' if orange in [orange,red] then;']);
  5286. ConvertProgram;
  5287. CheckSource('TestSet_EnumConst',
  5288. LinesToStr([ // statements
  5289. 'this.TEnum = {',
  5290. ' "0": "Red",',
  5291. ' Red: 0,',
  5292. ' "1": "Blue",',
  5293. ' Blue: 1',
  5294. '};',
  5295. 'this.Orange = $mod.TEnum.Red;',
  5296. 'this.Enum = 0;',
  5297. 'this.Enums = {};',
  5298. '']),
  5299. LinesToStr([
  5300. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5301. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5302. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5303. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5304. '']));
  5305. end;
  5306. procedure TTestModule.TestSet_IntConst;
  5307. begin
  5308. StartProgram(false);
  5309. Add([
  5310. 'type',
  5311. ' TEnums = set of Byte;',
  5312. 'const',
  5313. ' Orange = 0;',
  5314. 'var',
  5315. ' Enum: byte;',
  5316. ' Enums: tenums;',
  5317. 'begin',
  5318. ' Enums:=[];',
  5319. ' Enums:=[0];',
  5320. ' Enums:=[1..2];',
  5321. //' Include(enums,orange);',
  5322. //' Exclude(enums,orange);',
  5323. ' if orange in enums then;',
  5324. ' if orange in [orange,1] then;']);
  5325. ConvertProgram;
  5326. CheckSource('TestSet_IntConst',
  5327. LinesToStr([ // statements
  5328. 'this.Orange = 0;',
  5329. 'this.Enum = 0;',
  5330. 'this.Enums = {};',
  5331. '']),
  5332. LinesToStr([
  5333. '$mod.Enums = {};',
  5334. '$mod.Enums = rtl.createSet(0);',
  5335. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5336. 'if (0 in $mod.Enums) ;',
  5337. 'if (0 in rtl.createSet(0, 1)) ;',
  5338. '']));
  5339. end;
  5340. procedure TTestModule.TestSet_AnonymousEnumType;
  5341. begin
  5342. StartProgram(false);
  5343. Add('type');
  5344. Add(' TFlags = set of (red, green);');
  5345. Add('const');
  5346. Add(' favorite = red;');
  5347. Add('var');
  5348. Add(' f: TFlags;');
  5349. Add(' i: longint;');
  5350. Add('begin');
  5351. Add(' Include(f,red);');
  5352. Add(' Include(f,favorite);');
  5353. Add(' i:=ord(red);');
  5354. Add(' i:=ord(favorite);');
  5355. Add(' i:=ord(low(TFlags));');
  5356. Add(' i:=ord(low(f));');
  5357. Add(' i:=ord(low(favorite));');
  5358. Add(' i:=ord(high(TFlags));');
  5359. Add(' i:=ord(high(f));');
  5360. Add(' i:=ord(high(favorite));');
  5361. Add(' f:=[green,favorite];');
  5362. ConvertProgram;
  5363. CheckSource('TestSet_AnonymousEnumType',
  5364. LinesToStr([ // statements
  5365. 'this.TFlags$a = {',
  5366. ' "0": "red",',
  5367. ' red: 0,',
  5368. ' "1": "green",',
  5369. ' green: 1',
  5370. '};',
  5371. 'this.favorite = $mod.TFlags$a.red;',
  5372. 'this.f = {};',
  5373. 'this.i = 0;',
  5374. '']),
  5375. LinesToStr([
  5376. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5377. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5378. '$mod.i = $mod.TFlags$a.red;',
  5379. '$mod.i = $mod.TFlags$a.red;',
  5380. '$mod.i = $mod.TFlags$a.red;',
  5381. '$mod.i = $mod.TFlags$a.red;',
  5382. '$mod.i = $mod.TFlags$a.red;',
  5383. '$mod.i = $mod.TFlags$a.green;',
  5384. '$mod.i = $mod.TFlags$a.green;',
  5385. '$mod.i = $mod.TFlags$a.green;',
  5386. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5387. '']));
  5388. end;
  5389. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5390. begin
  5391. exit;
  5392. StartProgram(false);
  5393. Add([
  5394. 'type',
  5395. ' TAtoZ = ''A''..''Z'';',
  5396. ' TSetOfAZ = set of TAtoZ;',
  5397. 'var',
  5398. ' c: char;',
  5399. ' a: TAtoZ;',
  5400. ' s: TSetOfAZ = [''P'',''A''];',
  5401. ' i: longint;',
  5402. 'begin',
  5403. ' Include(s,''S'');',
  5404. ' Include(s,c);',
  5405. ' Include(s,a);',
  5406. ' c:=low(TAtoZ);',
  5407. ' i:=ord(low(TAtoZ));',
  5408. ' a:=high(TAtoZ);',
  5409. ' a:=high(TSetOfAtoZ);',
  5410. ' s:=[a,c,''M''];',
  5411. '']);
  5412. ConvertProgram;
  5413. CheckSource('TestSet_AnonymousEnumTypeChar',
  5414. LinesToStr([ // statements
  5415. '']),
  5416. LinesToStr([
  5417. '']));
  5418. end;
  5419. procedure TTestModule.TestSet_ConstEnum;
  5420. begin
  5421. StartProgram(false);
  5422. Add([
  5423. 'type',
  5424. ' TEnum = (red,blue,green);',
  5425. ' TEnums = set of TEnum;',
  5426. 'const',
  5427. ' teAny = [low(TEnum)..high(TEnum)];',
  5428. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5429. 'var',
  5430. ' e: TEnum;',
  5431. ' s: TEnums;',
  5432. 'begin',
  5433. ' if blue in teAny then;',
  5434. ' if blue in teAny+[e] then;',
  5435. ' if blue in teAny+teRedBlue then;',
  5436. ' if e in [red,blue] then;',
  5437. ' s:=teAny;',
  5438. ' s:=teAny+[e];',
  5439. ' s:=[e]+teAny;',
  5440. ' s:=teAny+teRedBlue;',
  5441. ' s:=teAny+teRedBlue+[e];',
  5442. '']);
  5443. ConvertProgram;
  5444. CheckSource('TestSet_ConstEnum',
  5445. LinesToStr([ // statements
  5446. 'this.TEnum = {',
  5447. ' "0": "red",',
  5448. ' red: 0,',
  5449. ' "1": "blue",',
  5450. ' blue: 1,',
  5451. ' "2": "green",',
  5452. ' green: 2',
  5453. '};',
  5454. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5455. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5456. 'this.e = 0;',
  5457. 'this.s = {};',
  5458. '']),
  5459. LinesToStr([
  5460. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5461. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5462. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5463. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5464. '$mod.s = rtl.refSet($mod.teAny);',
  5465. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5466. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5467. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5468. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5469. '']));
  5470. end;
  5471. procedure TTestModule.TestSet_ConstChar;
  5472. begin
  5473. StartProgram(false);
  5474. Add([
  5475. 'const',
  5476. ' LowChars = [''a''..''z''];',
  5477. ' Chars = LowChars+[''A''..''Z''];',
  5478. ' sc = [''А'', ''Я''];',
  5479. 'var',
  5480. ' c: char;',
  5481. ' s: string;',
  5482. 'begin',
  5483. ' if c in lowchars then ;',
  5484. ' if ''a'' in lowchars then ;',
  5485. ' if s[1] in lowchars then ;',
  5486. ' if c in chars then ;',
  5487. ' if c in [''a''..''z'',''_''] then ;',
  5488. ' if ''b'' in [''a''..''z'',''_''] then ;',
  5489. ' if ''Я'' in sc then ;',
  5490. '']);
  5491. ConvertProgram;
  5492. CheckSource('TestSet_ConstChar',
  5493. LinesToStr([ // statements
  5494. 'this.LowChars = rtl.createSet(null, 97, 122);',
  5495. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  5496. 'this.sc = rtl.createSet(1040, 1071);',
  5497. 'this.c = "";',
  5498. 'this.s = "";',
  5499. '']),
  5500. LinesToStr([
  5501. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  5502. 'if (97 in $mod.LowChars) ;',
  5503. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  5504. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  5505. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  5506. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  5507. 'if (1071 in $mod.sc) ;',
  5508. '']));
  5509. end;
  5510. procedure TTestModule.TestSet_ConstInt;
  5511. begin
  5512. StartProgram(false);
  5513. Add([
  5514. 'const',
  5515. ' Months = [1..12];',
  5516. ' Mirror = [-12..-1]+Months;',
  5517. 'var',
  5518. ' i: smallint;',
  5519. 'begin',
  5520. ' if 3 in Months then;',
  5521. ' if i in Months+[i] then;',
  5522. ' if i in Months+Mirror then;',
  5523. ' if i in [4..6,8] then;',
  5524. '']);
  5525. ConvertProgram;
  5526. CheckSource('TestSet_ConstInt',
  5527. LinesToStr([ // statements
  5528. 'this.Months = rtl.createSet(null, 1, 12);',
  5529. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  5530. 'this.i = 0;',
  5531. '']),
  5532. LinesToStr([
  5533. 'if (3 in $mod.Months) ;',
  5534. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  5535. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  5536. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  5537. '']));
  5538. end;
  5539. procedure TTestModule.TestSet_InFunction;
  5540. begin
  5541. StartProgram(false);
  5542. Add([
  5543. 'const',
  5544. ' TEnum = 3;',
  5545. ' TSetOfEnum = 4;',
  5546. ' TSetOfAno = 5;',
  5547. 'procedure DoIt;',
  5548. 'type',
  5549. ' TEnum = (red, blue);',
  5550. ' TSetOfEnum = set of TEnum;',
  5551. ' TSetOfAno = set of (up,down);',
  5552. 'var',
  5553. ' e: TEnum;',
  5554. ' se: TSetOfEnum;',
  5555. ' sa: TSetOfAno;',
  5556. 'begin',
  5557. ' se:=[e];',
  5558. ' sa:=[up];',
  5559. 'end;',
  5560. 'begin',
  5561. '']);
  5562. ConvertProgram;
  5563. CheckSource('TestSet_InFunction',
  5564. LinesToStr([ // statements
  5565. 'this.TEnum = 3;',
  5566. 'this.TSetOfEnum = 4;',
  5567. 'this.TSetOfAno = 5;',
  5568. 'var TEnum$1 = {',
  5569. ' "0": "red",',
  5570. ' red: 0,',
  5571. ' "1": "blue",',
  5572. ' blue: 1',
  5573. '};',
  5574. 'var TSetOfAno$a = {',
  5575. ' "0": "up",',
  5576. ' up: 0,',
  5577. ' "1": "down",',
  5578. ' down: 1',
  5579. '};',
  5580. 'this.DoIt = function () {',
  5581. ' var e = 0;',
  5582. ' var se = {};',
  5583. ' var sa = {};',
  5584. ' se = rtl.createSet(e);',
  5585. ' sa = rtl.createSet(TSetOfAno$a.up);',
  5586. '};',
  5587. '']),
  5588. LinesToStr([
  5589. '']));
  5590. end;
  5591. procedure TTestModule.TestSet_ForIn;
  5592. begin
  5593. StartProgram(false);
  5594. Add([
  5595. 'type',
  5596. ' TEnum = (Red, Green, Blue);',
  5597. ' TEnumRg = green..blue;',
  5598. ' TSetOfEnum = set of TEnum;',
  5599. ' TSetOfEnumRg = set of TEnumRg;',
  5600. 'var',
  5601. ' e, e2: TEnum;',
  5602. ' er: TEnum;',
  5603. ' s: TSetOfEnum;',
  5604. 'begin',
  5605. ' for e in TSetOfEnum do ;',
  5606. ' for e in TSetOfEnumRg do ;',
  5607. ' for e in [] do e2:=e;',
  5608. ' for e in [red..green] do e2:=e;',
  5609. ' for e in [green,blue] do e2:=e;',
  5610. ' for e in [red,blue] do e2:=e;',
  5611. ' for e in s do e2:=e;',
  5612. ' for er in TSetOfEnumRg do ;',
  5613. '']);
  5614. ConvertProgram;
  5615. CheckSource('TestSet_ForIn',
  5616. LinesToStr([ // statements
  5617. 'this.TEnum = {',
  5618. ' "0":"Red",',
  5619. ' Red:0,',
  5620. ' "1":"Green",',
  5621. ' Green:1,',
  5622. ' "2":"Blue",',
  5623. ' Blue:2',
  5624. ' };',
  5625. 'this.e = 0;',
  5626. 'this.e2 = 0;',
  5627. 'this.er = 0;',
  5628. 'this.s = {};',
  5629. '']),
  5630. LinesToStr([
  5631. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5632. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5633. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  5634. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  5635. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  5636. 'for (var $l1 in $mod.s){',
  5637. ' $mod.e = +$l1;',
  5638. ' $mod.e2 = $mod.e;',
  5639. '};',
  5640. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  5641. '']));
  5642. end;
  5643. procedure TTestModule.TestNestBegin;
  5644. begin
  5645. StartProgram(false);
  5646. Add('begin');
  5647. Add(' begin');
  5648. Add(' begin');
  5649. Add(' end;');
  5650. Add(' begin');
  5651. Add(' if true then ;');
  5652. Add(' end;');
  5653. Add(' end;');
  5654. ConvertProgram;
  5655. CheckSource('TestNestBegin',
  5656. '',
  5657. 'if (true) ;');
  5658. end;
  5659. procedure TTestModule.TestUnitImplVars;
  5660. begin
  5661. StartUnit(false);
  5662. Add('interface');
  5663. Add('implementation');
  5664. Add('var');
  5665. Add(' V1:longint;');
  5666. Add(' V2:longint = 3;');
  5667. Add(' V3:string = ''abc'';');
  5668. ConvertUnit;
  5669. CheckSource('TestUnitImplVars',
  5670. LinesToStr([ // statements
  5671. 'var $impl = $mod.$impl;',
  5672. '']),
  5673. '', // this.$init
  5674. LinesToStr([ // implementation
  5675. '$impl.V1 = 0;',
  5676. '$impl.V2 = 3;',
  5677. '$impl.V3 = "abc";',
  5678. '']) );
  5679. end;
  5680. procedure TTestModule.TestUnitImplConsts;
  5681. begin
  5682. StartUnit(false);
  5683. Add('interface');
  5684. Add('implementation');
  5685. Add('const');
  5686. Add(' v1 = 3;');
  5687. Add(' v2:longint = 4;');
  5688. Add(' v3:string = ''abc'';');
  5689. ConvertUnit;
  5690. CheckSource('TestUnitImplConsts',
  5691. LinesToStr([ // statements
  5692. 'var $impl = $mod.$impl;',
  5693. '']),
  5694. '', // this.$init
  5695. LinesToStr([ // implementation
  5696. '$impl.v1 = 3;',
  5697. '$impl.v2 = 4;',
  5698. '$impl.v3 = "abc";',
  5699. '']) );
  5700. end;
  5701. procedure TTestModule.TestUnitImplRecord;
  5702. begin
  5703. StartUnit(false);
  5704. Add('interface');
  5705. Add('implementation');
  5706. Add('type');
  5707. Add(' TMyRecord = record');
  5708. Add(' i: longint;');
  5709. Add(' end;');
  5710. Add('var aRec: TMyRecord;');
  5711. Add('initialization');
  5712. Add(' arec.i:=3;');
  5713. ConvertUnit;
  5714. CheckSource('TestUnitImplRecord',
  5715. LinesToStr([ // statements
  5716. 'var $impl = $mod.$impl;',
  5717. '']),
  5718. // this.$init
  5719. '$impl.aRec.i = 3;',
  5720. LinesToStr([ // implementation
  5721. 'rtl.recNewT($impl, "TMyRecord", function () {',
  5722. ' this.i = 0;',
  5723. ' this.$eq = function (b) {',
  5724. ' return this.i === b.i;',
  5725. ' };',
  5726. ' this.$assign = function (s) {',
  5727. ' this.i = s.i;',
  5728. ' return this;',
  5729. ' };',
  5730. '});',
  5731. '$impl.aRec = $impl.TMyRecord.$new();',
  5732. '']) );
  5733. end;
  5734. procedure TTestModule.TestRenameJSNameConflict;
  5735. begin
  5736. StartProgram(false);
  5737. Add('var apply: longint;');
  5738. Add('var bind: longint;');
  5739. Add('var call: longint;');
  5740. Add('begin');
  5741. ConvertProgram;
  5742. CheckSource('TestRenameJSNameConflict',
  5743. LinesToStr([ // statements
  5744. 'this.Apply = 0;',
  5745. 'this.Bind = 0;',
  5746. 'this.Call = 0;'
  5747. ]),
  5748. LinesToStr([ // this.$main
  5749. ''
  5750. ]));
  5751. end;
  5752. procedure TTestModule.TestLocalConst;
  5753. begin
  5754. StartProgram(false);
  5755. Add('procedure DoIt;');
  5756. Add('const');
  5757. Add(' cA: longint = 1;');
  5758. Add(' cB = 2;');
  5759. Add(' procedure Sub;');
  5760. Add(' const');
  5761. Add(' csA = 3;');
  5762. Add(' cB: double = 4;');
  5763. Add(' begin');
  5764. Add(' cb:=cb+csa;');
  5765. Add(' ca:=ca+csa+5;');
  5766. Add(' end;');
  5767. Add('begin');
  5768. Add(' ca:=ca+cb+6;');
  5769. Add('end;');
  5770. Add('begin');
  5771. ConvertProgram;
  5772. CheckSource('TestLocalConst',
  5773. LinesToStr([
  5774. 'var cA = 1;',
  5775. 'var cB = 2;',
  5776. 'var csA = 3;',
  5777. 'var cB$1 = 4;',
  5778. 'this.DoIt = function () {',
  5779. ' function Sub() {',
  5780. ' cB$1 = cB$1 + 3;',
  5781. ' cA = cA + 3 + 5;',
  5782. ' };',
  5783. ' cA = cA + 2 + 6;',
  5784. '};'
  5785. ]),
  5786. LinesToStr([
  5787. ]));
  5788. end;
  5789. procedure TTestModule.TestVarExternal;
  5790. begin
  5791. StartProgram(false);
  5792. Add('var');
  5793. Add(' NaN: double; external name ''Global.NaN'';');
  5794. Add(' d: double;');
  5795. Add('begin');
  5796. Add(' d:=NaN;');
  5797. ConvertProgram;
  5798. CheckSource('TestVarExternal',
  5799. LinesToStr([
  5800. 'this.d = 0.0;'
  5801. ]),
  5802. LinesToStr([
  5803. '$mod.d = Global.NaN;'
  5804. ]));
  5805. end;
  5806. procedure TTestModule.TestVarExternalOtherUnit;
  5807. begin
  5808. AddModuleWithIntfImplSrc('unit2.pas',
  5809. LinesToStr([
  5810. 'var NaN: double; external name ''Global.NaN'';',
  5811. 'var iV: longint;'
  5812. ]),
  5813. '');
  5814. StartUnit(true);
  5815. Add('interface');
  5816. Add('uses unit2;');
  5817. Add('implementation');
  5818. Add('var');
  5819. Add(' d: double;');
  5820. Add(' i: longint; external name ''$i'';');
  5821. Add('begin');
  5822. Add(' d:=nan;');
  5823. Add(' d:=uNit2.nan;');
  5824. Add(' d:=test1.d;');
  5825. Add(' i:=iv;');
  5826. Add(' i:=uNit2.iv;');
  5827. Add(' i:=test1.i;');
  5828. ConvertUnit;
  5829. CheckSource('TestVarExternalOtherUnit',
  5830. LinesToStr([
  5831. 'var $impl = $mod.$impl;',
  5832. '']),
  5833. LinesToStr([ // this.$init
  5834. '$impl.d = Global.NaN;',
  5835. '$impl.d = Global.NaN;',
  5836. '$impl.d = $impl.d;',
  5837. '$i = pas.unit2.iV;',
  5838. '$i = pas.unit2.iV;',
  5839. '$i = $i;',
  5840. '']),
  5841. LinesToStr([ // implementation
  5842. '$impl.d = 0.0;',
  5843. '']) );
  5844. end;
  5845. procedure TTestModule.TestVarAbsoluteFail;
  5846. begin
  5847. StartProgram(false);
  5848. Add([
  5849. 'var',
  5850. ' a: longint;',
  5851. ' b: longword absolute a;',
  5852. 'begin']);
  5853. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  5854. ConvertProgram;
  5855. end;
  5856. procedure TTestModule.TestConstExternal;
  5857. begin
  5858. StartProgram(false);
  5859. Add([
  5860. 'const',
  5861. ' PI: double; external name ''Global.PI'';',
  5862. ' Tau = 2*pi;',
  5863. 'var d: double;',
  5864. 'begin',
  5865. ' d:=pi;',
  5866. ' d:=tau+pi;']);
  5867. ConvertProgram;
  5868. CheckSource('TestConstExternal',
  5869. LinesToStr([
  5870. 'this.Tau = 2*Global.PI;',
  5871. 'this.d = 0.0;'
  5872. ]),
  5873. LinesToStr([
  5874. '$mod.d = Global.PI;',
  5875. '$mod.d = $mod.Tau + Global.PI;'
  5876. ]));
  5877. end;
  5878. procedure TTestModule.TestDouble;
  5879. begin
  5880. StartProgram(false);
  5881. Add([
  5882. 'type',
  5883. ' TDateTime = double;',
  5884. 'const',
  5885. ' a = TDateTime(2.7);',
  5886. ' b = a + TDateTime(1.7);',
  5887. ' c = 0.9 + 0.1;',
  5888. ' f0_1 = 0.1;',
  5889. ' f0_3 = 0.3;',
  5890. ' fn0_1 = -0.1;',
  5891. ' fn0_3 = -0.3;',
  5892. ' fn0_003 = -0.003;',
  5893. ' fn0_123456789 = -0.123456789;',
  5894. ' fn300_0 = -300.0;',
  5895. ' fn123456_0 = -123456.0;',
  5896. ' fn1234567_8 = -1234567.8;',
  5897. ' fn12345678_9 = -12345678.9;',
  5898. ' f1_0En12 = 1E-12;',
  5899. ' fn1_0En12 = -1E-12;',
  5900. ' maxdouble = 1.7e+308;',
  5901. ' mindouble = -1.7e+308;',
  5902. ' MinSafeIntDouble = -$10000000000000;',
  5903. ' MinSafeIntDouble2 = -$fffffffffffff-1;',
  5904. ' MaxSafeIntDouble = $fffffffffffff;',
  5905. ' DZeroResolution = 1E-12;',
  5906. ' Minus1 = -1E-12;',
  5907. ' EPS = 1E-9;',
  5908. ' DELTA = 0.001;',
  5909. ' Big = 129.789E+100;',
  5910. ' Test0_15 = 0.15;',
  5911. ' Test999 = 2.9999999999999;',
  5912. ' Test111999 = 211199999999999000.0;',
  5913. ' TestMinus111999 = -211199999999999000.0;',
  5914. 'var',
  5915. ' d: double = b;',
  5916. 'begin',
  5917. ' d:=1.0;',
  5918. ' d:=1.0/3.0;',
  5919. ' d:=1/3;',
  5920. ' d:=5.0E-324;',
  5921. ' d:=1.7E308;',
  5922. ' d:=001.00E00;',
  5923. ' d:=002.00E001;',
  5924. ' d:=003.000E000;',
  5925. ' d:=-004.00E-00;',
  5926. ' d:=-005.00E-001;',
  5927. ' d:=10**3;',
  5928. ' d:=10 mod 3;',
  5929. ' d:=10 div 3;',
  5930. ' d:=c;',
  5931. ' d:=f0_1;',
  5932. ' d:=f0_3;',
  5933. ' d:=fn0_1;',
  5934. ' d:=fn0_3;',
  5935. ' d:=fn0_003;',
  5936. ' d:=fn0_123456789;',
  5937. ' d:=fn300_0;',
  5938. ' d:=fn123456_0;',
  5939. ' d:=fn1234567_8;',
  5940. ' d:=fn12345678_9;',
  5941. ' d:=f1_0En12;',
  5942. ' d:=fn1_0En12;',
  5943. ' d:=maxdouble;',
  5944. ' d:=mindouble;',
  5945. ' d:=MinSafeIntDouble;',
  5946. ' d:=double(MinSafeIntDouble);',
  5947. ' d:=MinSafeIntDouble2;',
  5948. ' d:=double(MinSafeIntDouble2);',
  5949. ' d:=MaxSafeIntDouble;',
  5950. ' d:=default(double);',
  5951. '']);
  5952. ConvertProgram;
  5953. CheckSource('TestDouble',
  5954. LinesToStr([
  5955. 'this.a = 2.7;',
  5956. 'this.b = 2.7 + 1.7;',
  5957. 'this.c = 0.9 + 0.1;',
  5958. 'this.f0_1 = 0.1;',
  5959. 'this.f0_3 = 0.3;',
  5960. 'this.fn0_1 = -0.1;',
  5961. 'this.fn0_3 = -0.3;',
  5962. 'this.fn0_003 = -0.003;',
  5963. 'this.fn0_123456789 = -0.123456789;',
  5964. 'this.fn300_0 = -300.0;',
  5965. 'this.fn123456_0 = -123456.0;',
  5966. 'this.fn1234567_8 = -1234567.8;',
  5967. 'this.fn12345678_9 = -12345678.9;',
  5968. 'this.f1_0En12 = 1E-12;',
  5969. 'this.fn1_0En12 = -1E-12;',
  5970. 'this.maxdouble = 1.7e+308;',
  5971. 'this.mindouble = -1.7e+308;',
  5972. 'this.MinSafeIntDouble = -0x10000000000000;',
  5973. 'this.MinSafeIntDouble2 = -0xfffffffffffff - 1;',
  5974. 'this.MaxSafeIntDouble = 0xfffffffffffff;',
  5975. 'this.DZeroResolution = 1E-12;',
  5976. 'this.Minus1 = -1E-12;',
  5977. 'this.EPS = 1E-9;',
  5978. 'this.DELTA = 0.001;',
  5979. 'this.Big = 129.789E+100;',
  5980. 'this.Test0_15 = 0.15;',
  5981. 'this.Test999 = 2.9999999999999;',
  5982. 'this.Test111999 = 211199999999999000.0;',
  5983. 'this.TestMinus111999 = -211199999999999000.0;',
  5984. 'this.d = 4.4;'
  5985. ]),
  5986. LinesToStr([
  5987. '$mod.d = 1.0;',
  5988. '$mod.d = 1.0 / 3.0;',
  5989. '$mod.d = 1 / 3;',
  5990. '$mod.d = 5.0E-324;',
  5991. '$mod.d = 1.7E308;',
  5992. '$mod.d = 1.00E0;',
  5993. '$mod.d = 2.00E1;',
  5994. '$mod.d = 3.000E0;',
  5995. '$mod.d = -4.00E-0;',
  5996. '$mod.d = -5.00E-1;',
  5997. '$mod.d = Math.pow(10, 3);',
  5998. '$mod.d = 10 % 3;',
  5999. '$mod.d = Math.floor(10 / 3);',
  6000. '$mod.d = 1;',
  6001. '$mod.d = 0.1;',
  6002. '$mod.d = 0.3;',
  6003. '$mod.d = -0.1;',
  6004. '$mod.d = -0.3;',
  6005. '$mod.d = -0.003;',
  6006. '$mod.d = -0.123456789;',
  6007. '$mod.d = -300;',
  6008. '$mod.d = -123456;',
  6009. '$mod.d = -1234567.8;',
  6010. '$mod.d = -1.23456789E7;',
  6011. '$mod.d = 1E-12;',
  6012. '$mod.d = -1E-12;',
  6013. '$mod.d = 1.7E308;',
  6014. '$mod.d = -1.7E308;',
  6015. '$mod.d = -4503599627370496;',
  6016. '$mod.d = -4503599627370496;',
  6017. '$mod.d = -4503599627370496;',
  6018. '$mod.d = -4503599627370496;',
  6019. '$mod.d = 4503599627370495;',
  6020. '$mod.d = 0.0;',
  6021. '']));
  6022. end;
  6023. procedure TTestModule.TestInteger;
  6024. begin
  6025. StartProgram(false);
  6026. Add([
  6027. 'const',
  6028. ' MinInt = low(NativeInt);',
  6029. ' MaxInt = high(NativeInt);',
  6030. 'type',
  6031. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6032. 'const',
  6033. ' a = low(TMyInt)+High(TMyInt);',
  6034. 'var',
  6035. ' i: TMyInt;',
  6036. 'begin',
  6037. ' i:=-MinInt;',
  6038. ' i:=default(TMyInt);',
  6039. ' i:=low(i)+high(i);',
  6040. '']);
  6041. ConvertProgram;
  6042. CheckSource('TestIntegerRange',
  6043. LinesToStr([
  6044. 'this.MinInt = -4503599627370496;',
  6045. 'this.MaxInt = 4503599627370495;',
  6046. 'this.a = -4503599627370496 + 4503599627370495;',
  6047. 'this.i = 0;',
  6048. '']),
  6049. LinesToStr([
  6050. '$mod.i = - -4503599627370496;',
  6051. '$mod.i = -4503599627370496;',
  6052. '$mod.i = -4503599627370496 + 4503599627370495;',
  6053. '']));
  6054. end;
  6055. procedure TTestModule.TestIntegerRange;
  6056. begin
  6057. StartProgram(false);
  6058. Add([
  6059. 'const',
  6060. ' MinInt = -1;',
  6061. ' MaxInt = +1;',
  6062. 'type',
  6063. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6064. ' TInt2 = 1..3;',
  6065. 'const',
  6066. ' a = low(TMyInt)+High(TMyInt);',
  6067. ' b = low(TInt2)+High(TInt2);',
  6068. ' s1 = [1];',
  6069. ' s2 = [1,2];',
  6070. ' s3 = [1..3];',
  6071. ' s4 = [low(shortint)..high(shortint)];',
  6072. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6073. ' s6 = 1 in s2;',
  6074. 'var',
  6075. ' i: TMyInt;',
  6076. ' i2: TInt2;',
  6077. 'begin',
  6078. ' i:=i2;',
  6079. ' i:=default(TMyInt);',
  6080. ' if i=i2 then ;']);
  6081. ConvertProgram;
  6082. CheckSource('TestIntegerRange',
  6083. LinesToStr([
  6084. 'this.MinInt = -1;',
  6085. 'this.MaxInt = +1;',
  6086. 'this.a = -1 + 1;',
  6087. 'this.b = 1 + 3;',
  6088. 'this.s1 = rtl.createSet(1);',
  6089. 'this.s2 = rtl.createSet(1, 2);',
  6090. 'this.s3 = rtl.createSet(null, 1, 3);',
  6091. 'this.s4 = rtl.createSet(null, -128, 127);',
  6092. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6093. 'this.s6 = 1 in $mod.s2;',
  6094. 'this.i = 0;',
  6095. 'this.i2 = 0;',
  6096. '']),
  6097. LinesToStr([
  6098. '$mod.i = $mod.i2;',
  6099. '$mod.i = -1;',
  6100. 'if ($mod.i === $mod.i2) ;',
  6101. '']));
  6102. end;
  6103. procedure TTestModule.TestIntegerTypecasts;
  6104. begin
  6105. StartProgram(false);
  6106. Add([
  6107. 'var',
  6108. ' i: nativeint;',
  6109. ' b: byte;',
  6110. ' sh: shortint;',
  6111. ' w: word;',
  6112. ' sm: smallint;',
  6113. ' lw: longword;',
  6114. ' li: longint;',
  6115. 'begin',
  6116. ' b:=byte(i);',
  6117. ' sh:=shortint(i);',
  6118. ' w:=word(i);',
  6119. ' sm:=smallint(i);',
  6120. ' lw:=longword(i);',
  6121. ' li:=longint(i);',
  6122. '']);
  6123. ConvertProgram;
  6124. CheckSource('TestIntegerTypecasts',
  6125. LinesToStr([
  6126. 'this.i = 0;',
  6127. 'this.b = 0;',
  6128. 'this.sh = 0;',
  6129. 'this.w = 0;',
  6130. 'this.sm = 0;',
  6131. 'this.lw = 0;',
  6132. 'this.li = 0;',
  6133. '']),
  6134. LinesToStr([
  6135. '$mod.b = $mod.i & 255;',
  6136. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6137. '$mod.w = $mod.i & 65535;',
  6138. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6139. '$mod.lw = $mod.i >>> 0;',
  6140. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6141. '']));
  6142. end;
  6143. procedure TTestModule.TestBitwiseAndNativeIntWarn;
  6144. begin
  6145. StartProgram(false);
  6146. Add([
  6147. 'var',
  6148. ' i,j: nativeint;',
  6149. 'begin',
  6150. ' i:=i and j;',
  6151. '']);
  6152. ConvertProgram;
  6153. CheckSource('TestBitwiseAndNativeIntWarn',
  6154. LinesToStr([
  6155. 'this.i = 0;',
  6156. 'this.j = 0;',
  6157. '']),
  6158. LinesToStr([
  6159. '$mod.i = $mod.i & $mod.j;',
  6160. '']));
  6161. CheckHint(mtWarning,nBitWiseOperationsAre32Bit,sBitWiseOperationsAre32Bit);
  6162. end;
  6163. procedure TTestModule.TestCurrency;
  6164. begin
  6165. StartProgram(false);
  6166. Add([
  6167. 'type',
  6168. ' TCoin = currency;',
  6169. 'const',
  6170. ' a = TCoin(2.7);',
  6171. ' b = a + TCoin(1.7);',
  6172. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6173. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6174. 'var',
  6175. ' c: TCoin = b;',
  6176. ' i: nativeint;',
  6177. ' d: double;',
  6178. ' j: jsvalue;',
  6179. 'function DoIt(c: currency): currency; begin end;',
  6180. 'function GetIt(d: double): double; begin end;',
  6181. 'procedure Write(v: jsvalue); begin end;',
  6182. 'begin',
  6183. ' c:=1.0;',
  6184. ' c:=0.1;',
  6185. ' c:=1.0/3.0;',
  6186. ' c:=1/3;',
  6187. ' c:=a;',
  6188. ' d:=c;',
  6189. ' c:=d;',
  6190. ' c:=currency(c);',
  6191. ' c:=currency(d);',
  6192. ' d:=double(c);',
  6193. ' c:=i;',
  6194. ' c:=currency(i);',
  6195. //' i:=c;', not allowed
  6196. ' i:=nativeint(c);',
  6197. ' c:=c+a;',
  6198. ' c:=-c-a;',
  6199. ' c:=d+c;',
  6200. ' c:=c+d;',
  6201. ' c:=d-c;',
  6202. ' c:=c-d;',
  6203. ' c:=c*a;',
  6204. ' c:=a*c;',
  6205. ' c:=d*c;',
  6206. ' c:=c*d;',
  6207. ' c:=c/a;',
  6208. ' c:=a/c;',
  6209. ' c:=d/c;',
  6210. ' c:=c/d;',
  6211. ' c:=c**a;',
  6212. ' c:=a**c;',
  6213. ' c:=d**c;',
  6214. ' c:=c**d;',
  6215. ' if c=c then ;',
  6216. ' if c=a then ;',
  6217. ' if a=c then ;',
  6218. ' if d=c then ;',
  6219. ' if c=d then ;',
  6220. ' c:=DoIt(c);',
  6221. ' c:=DoIt(i);',
  6222. ' c:=DoIt(d);',
  6223. ' c:=GetIt(c);',
  6224. ' j:=c;',
  6225. ' Write(c);',
  6226. ' c:=default(currency);',
  6227. ' j:=str(c);',
  6228. ' j:=str(c:0:3);',
  6229. '']);
  6230. ConvertProgram;
  6231. CheckSource('TestCurrency',
  6232. LinesToStr([
  6233. 'this.a = 27000;',
  6234. 'this.b = $mod.a + 17000;',
  6235. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6236. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6237. 'this.c = $mod.b;',
  6238. 'this.i = 0;',
  6239. 'this.d = 0.0;',
  6240. 'this.j = undefined;',
  6241. 'this.DoIt = function (c) {',
  6242. ' var Result = 0;',
  6243. ' return Result;',
  6244. '};',
  6245. 'this.GetIt = function (d) {',
  6246. ' var Result = 0.0;',
  6247. ' return Result;',
  6248. '};',
  6249. 'this.Write = function (v) {',
  6250. '};',
  6251. '']),
  6252. LinesToStr([
  6253. '$mod.c = 10000;',
  6254. '$mod.c = 1000;',
  6255. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6256. '$mod.c = Math.floor((1 / 3) * 10000);',
  6257. '$mod.c = $mod.a;',
  6258. '$mod.d = $mod.c / 10000;',
  6259. '$mod.c = Math.floor($mod.d * 10000);',
  6260. '$mod.c = $mod.c;',
  6261. '$mod.c = $mod.d * 10000;',
  6262. '$mod.d = $mod.c / 10000;',
  6263. '$mod.c = $mod.i * 10000;',
  6264. '$mod.c = $mod.i * 10000;',
  6265. '$mod.i = Math.floor($mod.c / 10000);',
  6266. '$mod.c = $mod.c + $mod.a;',
  6267. '$mod.c = -$mod.c - $mod.a;',
  6268. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6269. '$mod.c = $mod.c + ($mod.d * 10000);',
  6270. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6271. '$mod.c = $mod.c - ($mod.d * 10000);',
  6272. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6273. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6274. '$mod.c = $mod.d * $mod.c;',
  6275. '$mod.c = $mod.c * $mod.d;',
  6276. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6277. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6278. '$mod.c = Math.floor($mod.d / $mod.c);',
  6279. '$mod.c = Math.floor($mod.c / $mod.d);',
  6280. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6281. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6282. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6283. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6284. 'if ($mod.c === $mod.c) ;',
  6285. 'if ($mod.c === $mod.a) ;',
  6286. 'if ($mod.a === $mod.c) ;',
  6287. 'if (($mod.d * 10000) === $mod.c) ;',
  6288. 'if ($mod.c === ($mod.d * 10000)) ;',
  6289. '$mod.c = $mod.DoIt($mod.c);',
  6290. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6291. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6292. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6293. '$mod.j = $mod.c / 10000;',
  6294. '$mod.Write($mod.c / 10000);',
  6295. '$mod.c = 0;',
  6296. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6297. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6298. '']));
  6299. end;
  6300. procedure TTestModule.TestForBoolDo;
  6301. begin
  6302. StartProgram(false);
  6303. Add([
  6304. 'var b: boolean;',
  6305. 'begin',
  6306. ' for b:=false to true do ;',
  6307. ' for b:=b downto false do ;',
  6308. ' for b in boolean do ;',
  6309. '']);
  6310. ConvertProgram;
  6311. CheckSource('TestForBoolDo',
  6312. LinesToStr([ // statements
  6313. 'this.b = false;']),
  6314. LinesToStr([ // this.$main
  6315. 'for (var $l1 = 0; $l1 <= 1; $l1++) $mod.b = $l1 !== 0;',
  6316. 'for (var $l2 = +$mod.b; $l2 >= 0; $l2--) $mod.b = $l2 !== 0;',
  6317. 'for (var $l3 = 0; $l3 <= 1; $l3++) $mod.b = $l3 !== 0;',
  6318. '']));
  6319. end;
  6320. procedure TTestModule.TestForIntDo;
  6321. begin
  6322. StartProgram(false);
  6323. Add([
  6324. 'var i: longint;',
  6325. 'begin',
  6326. ' for i:=3 to 5 do ;',
  6327. ' for i:=i downto 2 do ;',
  6328. ' for i in byte do ;',
  6329. '']);
  6330. ConvertProgram;
  6331. CheckSource('TestForIntDo',
  6332. LinesToStr([ // statements
  6333. 'this.i = 0;']),
  6334. LinesToStr([ // this.$main
  6335. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6336. 'for (var $l1 = $mod.i; $l1 >= 2; $l1--) $mod.i = $l1;',
  6337. 'for (var $l2 = 0; $l2 <= 255; $l2++) $mod.i = $l2;',
  6338. '']));
  6339. end;
  6340. procedure TTestModule.TestForIntInDo;
  6341. begin
  6342. StartProgram(false);
  6343. Add([
  6344. 'type',
  6345. ' TSetOfInt = set of byte;',
  6346. ' TIntRg = 3..7;',
  6347. ' TSetOfIntRg = set of TIntRg;',
  6348. 'var',
  6349. ' i,i2: longint;',
  6350. ' a1: array of byte;',
  6351. ' a2: array[1..3] of byte;',
  6352. ' soi: TSetOfInt;',
  6353. ' soir: TSetOfIntRg;',
  6354. ' ir: TIntRg;',
  6355. 'begin',
  6356. ' for i in byte do ;',
  6357. ' for i in a1 do ;',
  6358. ' for i in a2 do ;',
  6359. ' for i in [11..13] do ;',
  6360. ' for i in TSetOfInt do ;',
  6361. ' for i in TIntRg do ;',
  6362. ' for i in soi do i2:=i;',
  6363. ' for i in TSetOfIntRg do ;',
  6364. ' for i in soir do ;',
  6365. ' for ir in TIntRg do ;',
  6366. ' for ir in TSetOfIntRg do ;',
  6367. ' for ir in soir do ;',
  6368. '']);
  6369. ConvertProgram;
  6370. CheckSource('TestForIntInDo',
  6371. LinesToStr([ // statements
  6372. 'this.i = 0;',
  6373. 'this.i2 = 0;',
  6374. 'this.a1 = [];',
  6375. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6376. 'this.soi = {};',
  6377. 'this.soir = {};',
  6378. 'this.ir = 0;',
  6379. '']),
  6380. LinesToStr([ // this.$main
  6381. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6382. 'for (var $in2 = $mod.a1, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) $mod.i = $in2[$l3];',
  6383. 'for (var $in5 = $mod.a2, $l6 = 0, $end7 = rtl.length($in5) - 1; $l6 <= $end7; $l6++) $mod.i = $in5[$l6];',
  6384. 'for (var $l8 = 11; $l8 <= 13; $l8++) $mod.i = $l8;',
  6385. 'for (var $l9 = 0; $l9 <= 255; $l9++) $mod.i = $l9;',
  6386. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.i = $l10;',
  6387. 'for (var $l11 in $mod.soi) {',
  6388. ' $mod.i = +$l11;',
  6389. ' $mod.i2 = $mod.i;',
  6390. '};',
  6391. 'for (var $l12 = 3; $l12 <= 7; $l12++) $mod.i = $l12;',
  6392. 'for (var $l13 in $mod.soir) $mod.i = +$l13;',
  6393. 'for (var $l14 = 3; $l14 <= 7; $l14++) $mod.ir = $l14;',
  6394. 'for (var $l15 = 3; $l15 <= 7; $l15++) $mod.ir = $l15;',
  6395. 'for (var $l16 in $mod.soir) $mod.ir = +$l16;',
  6396. '']));
  6397. end;
  6398. procedure TTestModule.TestCharConst;
  6399. begin
  6400. StartProgram(false);
  6401. Add([
  6402. 'const',
  6403. ' a = #$00F3;',
  6404. ' c: char = ''1'';',
  6405. 'begin',
  6406. ' c:=#0;',
  6407. ' c:=#1;',
  6408. ' c:=#9;',
  6409. ' c:=#10;',
  6410. ' c:=#13;',
  6411. ' c:=#31;',
  6412. ' c:=#32;',
  6413. ' c:=#$A;',
  6414. ' c:=#$0A;',
  6415. ' c:=#$b;',
  6416. ' c:=#$0b;',
  6417. ' c:=^A;',
  6418. ' c:=''"'';',
  6419. ' c:=default(char);',
  6420. ' c:=#$00E4;', // ä
  6421. ' c:=''ä'';',
  6422. ' c:=#$E4;', // ä
  6423. ' c:=#$D800;', // invalid UTF-16
  6424. ' c:=#$DFFF;', // invalid UTF-16
  6425. ' c:=#$FFFF;', // last UCS-2
  6426. ' c:=high(c);', // last UCS-2
  6427. '']);
  6428. ConvertProgram;
  6429. CheckSource('TestCharConst',
  6430. LinesToStr([
  6431. 'this.a="ó";',
  6432. 'this.c="1";'
  6433. ]),
  6434. LinesToStr([
  6435. '$mod.c="\x00";',
  6436. '$mod.c="\x01";',
  6437. '$mod.c="\t";',
  6438. '$mod.c="\n";',
  6439. '$mod.c="\r";',
  6440. '$mod.c="\x1F";',
  6441. '$mod.c=" ";',
  6442. '$mod.c="\n";',
  6443. '$mod.c="\n";',
  6444. '$mod.c="\x0B";',
  6445. '$mod.c="\x0B";',
  6446. '$mod.c="\x01";',
  6447. '$mod.c=''"'';',
  6448. '$mod.c="\x00";',
  6449. '$mod.c = "ä";',
  6450. '$mod.c = "ä";',
  6451. '$mod.c = "ä";',
  6452. '$mod.c="\uD800";',
  6453. '$mod.c="\uDFFF";',
  6454. '$mod.c="\uFFFF";',
  6455. '$mod.c="\uFFFF";',
  6456. '']));
  6457. end;
  6458. procedure TTestModule.TestChar_Compare;
  6459. begin
  6460. StartProgram(false);
  6461. Add('var');
  6462. Add(' c: char;');
  6463. Add(' b: boolean;');
  6464. Add('begin');
  6465. Add(' b:=c=''1'';');
  6466. Add(' b:=''2''=c;');
  6467. Add(' b:=''3''=''4'';');
  6468. Add(' b:=c<>''5'';');
  6469. Add(' b:=''6''<>c;');
  6470. Add(' b:=c>''7'';');
  6471. Add(' b:=''8''>c;');
  6472. Add(' b:=c>=''9'';');
  6473. Add(' b:=''A''>=c;');
  6474. Add(' b:=c<''B'';');
  6475. Add(' b:=''C''<c;');
  6476. Add(' b:=c<=''D'';');
  6477. Add(' b:=''E''<=c;');
  6478. ConvertProgram;
  6479. CheckSource('TestChar_Compare',
  6480. LinesToStr([
  6481. 'this.c="";',
  6482. 'this.b = false;'
  6483. ]),
  6484. LinesToStr([
  6485. '$mod.b = $mod.c === "1";',
  6486. '$mod.b = "2" === $mod.c;',
  6487. '$mod.b = "3" === "4";',
  6488. '$mod.b = $mod.c !== "5";',
  6489. '$mod.b = "6" !== $mod.c;',
  6490. '$mod.b = $mod.c > "7";',
  6491. '$mod.b = "8" > $mod.c;',
  6492. '$mod.b = $mod.c >= "9";',
  6493. '$mod.b = "A" >= $mod.c;',
  6494. '$mod.b = $mod.c < "B";',
  6495. '$mod.b = "C" < $mod.c;',
  6496. '$mod.b = $mod.c <= "D";',
  6497. '$mod.b = "E" <= $mod.c;',
  6498. '']));
  6499. end;
  6500. procedure TTestModule.TestChar_BuiltInProcs;
  6501. begin
  6502. StartProgram(false);
  6503. Add([
  6504. 'var',
  6505. ' c: char;',
  6506. ' i: longint;',
  6507. ' s: string;',
  6508. 'begin',
  6509. ' i:=ord(c);',
  6510. ' i:=ord(s[i]);',
  6511. ' c:=chr(i);',
  6512. ' c:=pred(c);',
  6513. ' c:=succ(c);',
  6514. ' c:=low(c);',
  6515. ' c:=high(c);',
  6516. ' i:=byte(c);',
  6517. ' i:=word(c);',
  6518. ' i:=longint(c);',
  6519. '']);
  6520. ConvertProgram;
  6521. CheckSource('TestChar_BuiltInProcs',
  6522. LinesToStr([
  6523. 'this.c = "";',
  6524. 'this.i = 0;',
  6525. 'this.s = "";'
  6526. ]),
  6527. LinesToStr([
  6528. '$mod.i = $mod.c.charCodeAt();',
  6529. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  6530. '$mod.c = String.fromCharCode($mod.i);',
  6531. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  6532. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  6533. '$mod.c = "\x00";',
  6534. '$mod.c = "\uFFFF";',
  6535. '$mod.i = $mod.c.charCodeAt() & 255;',
  6536. '$mod.i = $mod.c.charCodeAt();',
  6537. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  6538. '']));
  6539. end;
  6540. procedure TTestModule.TestStringConst;
  6541. begin
  6542. StartProgram(false);
  6543. Add([
  6544. '{$H+}',
  6545. 'const',
  6546. ' a = #$00F3#$017C;', // first <256, then >=256
  6547. ' b = string(''a'');',
  6548. ' c = string(''ä'');',
  6549. ' d = UnicodeString(''b'');',
  6550. ' e = UnicodeString(''ö'');',
  6551. 'var',
  6552. ' s: string = ''abc'';',
  6553. 'begin',
  6554. ' s:='''';',
  6555. ' s:=#13#10;',
  6556. ' s:=#9''foo'';',
  6557. ' s:=#$A9;',
  6558. ' s:=''foo''#13''bar'';',
  6559. ' s:=''"'';',
  6560. ' s:=''"''''"'';',
  6561. ' s:=#$20AC;', // euro
  6562. ' s:=#$10437;', // outside BMP
  6563. ' s:=default(string);',
  6564. ' s:=concat(s);',
  6565. ' s:=concat(s,''a'',s)',
  6566. '']);
  6567. ConvertProgram;
  6568. CheckSource('TestStringConst',
  6569. LinesToStr([
  6570. 'this.a = "óż";',
  6571. 'this.b = "a";',
  6572. 'this.c = "ä";',
  6573. 'this.d = "b";',
  6574. 'this.e = "ö";',
  6575. 'this.s="abc";',
  6576. '']),
  6577. LinesToStr([
  6578. '$mod.s="";',
  6579. '$mod.s="\r\n";',
  6580. '$mod.s="\tfoo";',
  6581. '$mod.s="©";',
  6582. '$mod.s="foo\rbar";',
  6583. '$mod.s=''"'';',
  6584. '$mod.s=''"\''"'';',
  6585. '$mod.s="€";',
  6586. '$mod.s="'#$F0#$90#$90#$B7'";',
  6587. '$mod.s="";',
  6588. '$mod.s = $mod.s;',
  6589. '$mod.s = $mod.s.concat("a", $mod.s);',
  6590. '']));
  6591. end;
  6592. procedure TTestModule.TestStringConstSurrogate;
  6593. begin
  6594. StartProgram(false);
  6595. Add([
  6596. 'var',
  6597. ' s: string;',
  6598. 'begin',
  6599. ' s:=''😊'';', // 1F60A
  6600. '']);
  6601. ConvertProgram;
  6602. CheckSource('TestStringConstSurrogate',
  6603. LinesToStr([
  6604. 'this.s="";'
  6605. ]),
  6606. LinesToStr([
  6607. '$mod.s="😊";'
  6608. ]));
  6609. end;
  6610. procedure TTestModule.TestString_Length;
  6611. begin
  6612. StartProgram(false);
  6613. Add('const c = ''foo'';');
  6614. Add('var');
  6615. Add(' s: string;');
  6616. Add(' i: longint;');
  6617. Add('begin');
  6618. Add(' i:=length(s);');
  6619. Add(' i:=length(s+s);');
  6620. Add(' i:=length(''abc'');');
  6621. Add(' i:=length(c);');
  6622. ConvertProgram;
  6623. CheckSource('TestString_Length',
  6624. LinesToStr([
  6625. 'this.c = "foo";',
  6626. 'this.s = "";',
  6627. 'this.i = 0;',
  6628. '']),
  6629. LinesToStr([
  6630. '$mod.i = $mod.s.length;',
  6631. '$mod.i = ($mod.s+$mod.s).length;',
  6632. '$mod.i = "abc".length;',
  6633. '$mod.i = $mod.c.length;',
  6634. '']));
  6635. end;
  6636. procedure TTestModule.TestString_Compare;
  6637. begin
  6638. StartProgram(false);
  6639. Add('var');
  6640. Add(' s, t: string;');
  6641. Add(' b: boolean;');
  6642. Add('begin');
  6643. Add(' b:=s=t;');
  6644. Add(' b:=s<>t;');
  6645. Add(' b:=s>t;');
  6646. Add(' b:=s>=t;');
  6647. Add(' b:=s<t;');
  6648. Add(' b:=s<=t;');
  6649. ConvertProgram;
  6650. CheckSource('TestString_Compare',
  6651. LinesToStr([ // statements
  6652. 'this.s = "";',
  6653. 'this.t = "";',
  6654. 'this.b =false;'
  6655. ]),
  6656. LinesToStr([ // this.$main
  6657. '$mod.b = $mod.s === $mod.t;',
  6658. '$mod.b = $mod.s !== $mod.t;',
  6659. '$mod.b = $mod.s > $mod.t;',
  6660. '$mod.b = $mod.s >= $mod.t;',
  6661. '$mod.b = $mod.s < $mod.t;',
  6662. '$mod.b = $mod.s <= $mod.t;',
  6663. '']));
  6664. end;
  6665. procedure TTestModule.TestString_SetLength;
  6666. begin
  6667. StartProgram(false);
  6668. Add([
  6669. 'procedure DoIt(var s: string);',
  6670. 'begin',
  6671. ' SetLength(s,2);',
  6672. 'end;',
  6673. 'var s: string;',
  6674. 'begin',
  6675. ' SetLength(s,3);',
  6676. '']);
  6677. ConvertProgram;
  6678. CheckSource('TestString_SetLength',
  6679. LinesToStr([ // statements
  6680. 'this.DoIt = function (s) {',
  6681. ' s.set(rtl.strSetLength(s.get(), 2));',
  6682. '};',
  6683. 'this.s = "";',
  6684. '']),
  6685. LinesToStr([ // this.$main
  6686. '$mod.s = rtl.strSetLength($mod.s, 3);'
  6687. ]));
  6688. end;
  6689. procedure TTestModule.TestString_CharAt;
  6690. begin
  6691. StartProgram(false);
  6692. Add([
  6693. 'var',
  6694. ' s: string;',
  6695. ' c: char;',
  6696. ' b: boolean;',
  6697. 'begin',
  6698. ' b:= s[1] = c;',
  6699. ' b:= c = s[1];',
  6700. ' b:= c <> s[1];',
  6701. ' b:= c > s[1];',
  6702. ' b:= c >= s[1];',
  6703. ' b:= c < s[2];',
  6704. ' b:= c <= s[1];',
  6705. ' s[1] := c;',
  6706. ' s[2+3] := c;']);
  6707. ConvertProgram;
  6708. CheckSource('TestString_CharAt',
  6709. LinesToStr([ // statements
  6710. 'this.s = "";',
  6711. 'this.c = "";',
  6712. 'this.b = false;'
  6713. ]),
  6714. LinesToStr([ // this.$main
  6715. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  6716. '$mod.b = $mod.c === $mod.s.charAt(0);',
  6717. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  6718. '$mod.b = $mod.c > $mod.s.charAt(0);',
  6719. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  6720. '$mod.b = $mod.c < $mod.s.charAt(1);',
  6721. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  6722. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  6723. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  6724. '']));
  6725. end;
  6726. procedure TTestModule.TestStringHMinusFail;
  6727. begin
  6728. StartProgram(false);
  6729. Add([
  6730. '{$H-}',
  6731. 'var s: string;',
  6732. 'begin']);
  6733. ConvertProgram;
  6734. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  6735. end;
  6736. procedure TTestModule.TestStr;
  6737. begin
  6738. StartProgram(false);
  6739. Add('var');
  6740. Add(' b: boolean;');
  6741. Add(' i: longint;');
  6742. Add(' d: double;');
  6743. Add(' s: string;');
  6744. Add('begin');
  6745. Add(' str(b,s);');
  6746. Add(' str(i,s);');
  6747. Add(' str(d,s);');
  6748. Add(' str(i:3,s);');
  6749. Add(' str(d:3:2,s);');
  6750. Add(' Str(12.456:12:1,s);');
  6751. Add(' Str(12.456:12,s);');
  6752. Add(' s:=str(b);');
  6753. Add(' s:=str(i);');
  6754. Add(' s:=str(d);');
  6755. Add(' s:=str(i,i);');
  6756. Add(' s:=str(i:3);');
  6757. Add(' s:=str(d:3:2);');
  6758. Add(' s:=str(i:4,i);');
  6759. Add(' s:=str(i,i:5);');
  6760. Add(' s:=str(i:4,i:5);');
  6761. Add(' s:=str(s,s);');
  6762. Add(' s:=str(s,''foo'');');
  6763. ConvertProgram;
  6764. CheckSource('TestStr',
  6765. LinesToStr([ // statements
  6766. 'this.b = false;',
  6767. 'this.i = 0;',
  6768. 'this.d = 0.0;',
  6769. 'this.s = "";',
  6770. '']),
  6771. LinesToStr([ // this.$main
  6772. '$mod.s = ""+$mod.b;',
  6773. '$mod.s = ""+$mod.i;',
  6774. '$mod.s = rtl.floatToStr($mod.d);',
  6775. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  6776. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  6777. '$mod.s = rtl.floatToStr(12.456,12,1);',
  6778. '$mod.s = rtl.floatToStr(12.456,12);',
  6779. '$mod.s = ""+$mod.b;',
  6780. '$mod.s = ""+$mod.i;',
  6781. '$mod.s = rtl.floatToStr($mod.d);',
  6782. '$mod.s = ""+$mod.i+$mod.i;',
  6783. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  6784. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  6785. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  6786. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  6787. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  6788. '$mod.s = $mod.s + $mod.s;',
  6789. '$mod.s = $mod.s + "foo";',
  6790. '']));
  6791. end;
  6792. procedure TTestModule.TestBaseType_AnsiStringFail;
  6793. begin
  6794. StartProgram(false);
  6795. Add('var s: AnsiString');
  6796. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  6797. ConvertProgram;
  6798. end;
  6799. procedure TTestModule.TestBaseType_WideStringFail;
  6800. begin
  6801. StartProgram(false);
  6802. Add('var s: WideString');
  6803. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  6804. ConvertProgram;
  6805. end;
  6806. procedure TTestModule.TestBaseType_ShortStringFail;
  6807. begin
  6808. StartProgram(false);
  6809. Add('var s: ShortString');
  6810. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  6811. ConvertProgram;
  6812. end;
  6813. procedure TTestModule.TestBaseType_RawByteStringFail;
  6814. begin
  6815. StartProgram(false);
  6816. Add('var s: RawByteString');
  6817. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  6818. ConvertProgram;
  6819. end;
  6820. procedure TTestModule.TestTypeShortstring_Fail;
  6821. begin
  6822. StartProgram(false);
  6823. Add('type t = string[12];');
  6824. Add('var s: t;');
  6825. Add('begin');
  6826. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  6827. ConvertProgram;
  6828. end;
  6829. procedure TTestModule.TestCharSet_Custom;
  6830. begin
  6831. StartProgram(false);
  6832. Add([
  6833. 'type',
  6834. ' TCharRg = ''a''..''z'';',
  6835. ' TSetOfCharRg = set of TCharRg;',
  6836. ' TCharRg2 = ''m''..''p'';',
  6837. 'const',
  6838. ' crg: TCharRg = ''b'';',
  6839. 'var',
  6840. ' c: char;',
  6841. ' crg2: TCharRg2;',
  6842. ' s: TSetOfCharRg;',
  6843. 'begin',
  6844. ' c:=crg;',
  6845. ' crg:=c;',
  6846. ' crg2:=crg;',
  6847. ' if c=crg then ;',
  6848. ' if crg=c then ;',
  6849. ' if crg=crg2 then ;',
  6850. ' if c in s then ;',
  6851. ' if crg2 in s then ;',
  6852. ' c:=default(TCharRg);',
  6853. '']);
  6854. ConvertProgram;
  6855. CheckSource('TestCharSet_Custom',
  6856. LinesToStr([ // statements
  6857. 'this.crg = "b";',
  6858. 'this.c = "";',
  6859. 'this.crg2 = "m";',
  6860. 'this.s = {};',
  6861. '']),
  6862. LinesToStr([ // this.$main
  6863. '$mod.c = $mod.crg;',
  6864. '$mod.crg = $mod.c;',
  6865. '$mod.crg2 = $mod.crg;',
  6866. 'if ($mod.c === $mod.crg) ;',
  6867. 'if ($mod.crg === $mod.c) ;',
  6868. 'if ($mod.crg === $mod.crg2) ;',
  6869. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  6870. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  6871. '$mod.c = "a";',
  6872. '']));
  6873. end;
  6874. procedure TTestModule.TestForCharDo;
  6875. begin
  6876. StartProgram(false);
  6877. Add([
  6878. 'var c: char;',
  6879. 'begin',
  6880. ' for c:=''a'' to ''c'' do ;',
  6881. ' for c:=c downto ''a'' do ;',
  6882. ' for c:=''Б'' to ''Я'' do ;',
  6883. '']);
  6884. ConvertProgram;
  6885. CheckSource('TestForCharDo',
  6886. LinesToStr([ // statements
  6887. 'this.c = "";']),
  6888. LinesToStr([ // this.$main
  6889. 'for (var $l1 = 97; $l1 <= 99; $l1++) $mod.c = String.fromCharCode($l1);',
  6890. 'for (var $l2 = $mod.c.charCodeAt(); $l2 >= 97; $l2--) $mod.c = String.fromCharCode($l2);',
  6891. 'for (var $l3 = 1041; $l3 <= 1071; $l3++) $mod.c = String.fromCharCode($l3);',
  6892. '']));
  6893. end;
  6894. procedure TTestModule.TestForCharInDo;
  6895. begin
  6896. StartProgram(false);
  6897. Add([
  6898. 'type',
  6899. ' TSetOfChar = set of char;',
  6900. ' TCharRg = ''a''..''z'';',
  6901. ' TSetOfCharRg = set of TCharRg;',
  6902. 'const Foo = ''foo'';',
  6903. 'var',
  6904. ' c,c2: char;',
  6905. ' s: string;',
  6906. ' a1: array of char;',
  6907. ' a2: array[1..3] of char;',
  6908. ' soc: TSetOfChar;',
  6909. ' socr: TSetOfCharRg;',
  6910. ' cr: TCharRg;',
  6911. 'begin',
  6912. ' for c in foo do ;',
  6913. ' for c in s do ;',
  6914. ' for c in char do ;',
  6915. ' for c in a1 do ;',
  6916. ' for c in a2 do ;',
  6917. ' for c in [''1''..''3''] do ;',
  6918. ' for c in TSetOfChar do ;',
  6919. ' for c in TCharRg do ;',
  6920. ' for c in soc do c2:=c;',
  6921. ' for c in TSetOfCharRg do ;',
  6922. ' for c in socr do ;',
  6923. ' for cr in TCharRg do ;',
  6924. ' for cr in TSetOfCharRg do ;',
  6925. ' for cr in socr do ;',
  6926. '']);
  6927. ConvertProgram;
  6928. CheckSource('TestForCharInDo',
  6929. LinesToStr([ // statements
  6930. 'this.Foo = "foo";',
  6931. 'this.c = "";',
  6932. 'this.c2 = "";',
  6933. 'this.s = "";',
  6934. 'this.a1 = [];',
  6935. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  6936. 'this.soc = {};',
  6937. 'this.socr = {};',
  6938. 'this.cr = "a";',
  6939. '']),
  6940. LinesToStr([ // this.$main
  6941. 'for (var $in1 = $mod.Foo, $l2 = 0, $end3 = $in1.length - 1; $l2 <= $end3; $l2++) $mod.c = $in1.charAt($l2);',
  6942. 'for (var $in4 = $mod.s, $l5 = 0, $end6 = $in4.length - 1; $l5 <= $end6; $l5++) $mod.c = $in4.charAt($l5);',
  6943. 'for (var $l7 = 0; $l7 <= 65535; $l7++) $mod.c = String.fromCharCode($l7);',
  6944. 'for (var $in8 = $mod.a1, $l9 = 0, $end10 = rtl.length($in8) - 1; $l9 <= $end10; $l9++) $mod.c = $in8[$l9];',
  6945. 'for (var $in11 = $mod.a2, $l12 = 0, $end13 = rtl.length($in11) - 1; $l12 <= $end13; $l12++) $mod.c = $in11[$l12];',
  6946. 'for (var $l14 = 49; $l14 <= 51; $l14++) $mod.c = String.fromCharCode($l14);',
  6947. 'for (var $l15 = 0; $l15 <= 65535; $l15++) $mod.c = String.fromCharCode($l15);',
  6948. 'for (var $l16 = 97; $l16 <= 122; $l16++) $mod.c = String.fromCharCode($l16);',
  6949. 'for (var $l17 in $mod.soc) {',
  6950. ' $mod.c = String.fromCharCode($l17);',
  6951. ' $mod.c2 = $mod.c;',
  6952. '};',
  6953. 'for (var $l18 = 97; $l18 <= 122; $l18++) $mod.c = String.fromCharCode($l18);',
  6954. 'for (var $l19 in $mod.socr) $mod.c = String.fromCharCode($l19);',
  6955. 'for (var $l20 = 97; $l20 <= 122; $l20++) $mod.cr = String.fromCharCode($l20);',
  6956. 'for (var $l21 = 97; $l21 <= 122; $l21++) $mod.cr = String.fromCharCode($l21);',
  6957. 'for (var $l22 in $mod.socr) $mod.cr = String.fromCharCode($l22);',
  6958. '']));
  6959. end;
  6960. procedure TTestModule.TestProcTwoArgs;
  6961. begin
  6962. StartProgram(false);
  6963. Add('procedure Test(a,b: longint);');
  6964. Add('begin');
  6965. Add('end;');
  6966. Add('begin');
  6967. ConvertProgram;
  6968. CheckSource('TestProcTwoArgs',
  6969. LinesToStr([ // statements
  6970. 'this.Test = function (a,b) {',
  6971. '};'
  6972. ]),
  6973. LinesToStr([ // this.$main
  6974. ''
  6975. ]));
  6976. end;
  6977. procedure TTestModule.TestProc_DefaultValue;
  6978. begin
  6979. StartProgram(false);
  6980. Add('procedure p1(i: longint = 1);');
  6981. Add('begin');
  6982. Add('end;');
  6983. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  6984. Add('begin');
  6985. Add('end;');
  6986. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  6987. Add('begin');
  6988. Add('end;');
  6989. Add('begin');
  6990. Add(' p1;');
  6991. Add(' p1();');
  6992. Add(' p1(11);');
  6993. Add(' p2;');
  6994. Add(' p2();');
  6995. Add(' p2(12);');
  6996. Add(' p2(13,''b'');');
  6997. Add(' p3();');
  6998. ConvertProgram;
  6999. CheckSource('TestProc_DefaultValue',
  7000. LinesToStr([ // statements
  7001. 'this.p1 = function (i) {',
  7002. '};',
  7003. 'this.p2 = function (i,c) {',
  7004. '};',
  7005. 'this.p3 = function (d,b,s) {',
  7006. '};'
  7007. ]),
  7008. LinesToStr([ // this.$main
  7009. ' $mod.p1(1);',
  7010. ' $mod.p1(1);',
  7011. ' $mod.p1(11);',
  7012. ' $mod.p2(1,"a");',
  7013. ' $mod.p2(1,"a");',
  7014. ' $mod.p2(12,"a");',
  7015. ' $mod.p2(13,"b");',
  7016. ' $mod.p3(1.0,false,"abc");'
  7017. ]));
  7018. end;
  7019. procedure TTestModule.TestFunctionInt;
  7020. begin
  7021. StartProgram(false);
  7022. Add('function MyTest(Bar: longint): longint;');
  7023. Add('begin');
  7024. Add(' Result:=2*bar');
  7025. Add('end;');
  7026. Add('begin');
  7027. ConvertProgram;
  7028. CheckSource('TestFunctionInt',
  7029. LinesToStr([ // statements
  7030. 'this.MyTest = function (Bar) {',
  7031. ' var Result = 0;',
  7032. ' Result = 2*Bar;',
  7033. ' return Result;',
  7034. '};'
  7035. ]),
  7036. LinesToStr([ // this.$main
  7037. ''
  7038. ]));
  7039. end;
  7040. procedure TTestModule.TestFunctionString;
  7041. begin
  7042. StartProgram(false);
  7043. Add('function Test(Bar: string): string;');
  7044. Add('begin');
  7045. Add(' Result:=bar+BAR');
  7046. Add('end;');
  7047. Add('begin');
  7048. ConvertProgram;
  7049. CheckSource('TestFunctionString',
  7050. LinesToStr([ // statements
  7051. 'this.Test = function (Bar) {',
  7052. ' var Result = "";',
  7053. ' Result = Bar+Bar;',
  7054. ' return Result;',
  7055. '};'
  7056. ]),
  7057. LinesToStr([ // this.$main
  7058. ''
  7059. ]));
  7060. end;
  7061. procedure TTestModule.TestIfThen;
  7062. begin
  7063. StartProgram(false);
  7064. Add([
  7065. 'var b: boolean;',
  7066. 'begin',
  7067. ' if b then ;',
  7068. ' if b then else ;']);
  7069. ConvertProgram;
  7070. CheckSource('TestIfThen',
  7071. LinesToStr([ // statements
  7072. 'this.b = false;',
  7073. '']),
  7074. LinesToStr([ // this.$main
  7075. 'if ($mod.b) ;',
  7076. 'if ($mod.b) ;',
  7077. '']));
  7078. end;
  7079. procedure TTestModule.TestForLoop;
  7080. begin
  7081. StartProgram(false);
  7082. Add('var');
  7083. Add(' vI, vJ, vN: longint;');
  7084. Add('begin');
  7085. Add(' VJ:=0;');
  7086. Add(' VN:=3;');
  7087. Add(' for VI:=1 to VN do');
  7088. Add(' begin');
  7089. Add(' VJ:=VJ+VI;');
  7090. Add(' end;');
  7091. ConvertProgram;
  7092. CheckSource('TestForLoop',
  7093. LinesToStr([ // statements
  7094. 'this.vI = 0;',
  7095. 'this.vJ = 0;',
  7096. 'this.vN = 0;'
  7097. ]),
  7098. LinesToStr([ // this.$main
  7099. ' $mod.vJ = 0;',
  7100. ' $mod.vN = 3;',
  7101. ' for (var $l1 = 1, $end2 = $mod.vN; $l1 <= $end2; $l1++) {',
  7102. ' $mod.vI = $l1;',
  7103. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7104. ' };',
  7105. '']));
  7106. end;
  7107. procedure TTestModule.TestForLoopInsideFunction;
  7108. begin
  7109. StartProgram(false);
  7110. Add('function SumNumbers(Count: longint): longint;');
  7111. Add('var');
  7112. Add(' vI, vJ: longint;');
  7113. Add('begin');
  7114. Add(' vj:=0;');
  7115. Add(' for vi:=1 to count do');
  7116. Add(' begin');
  7117. Add(' vj:=vj+vi;');
  7118. Add(' end;');
  7119. Add('end;');
  7120. Add('begin');
  7121. Add(' sumnumbers(3);');
  7122. ConvertProgram;
  7123. CheckSource('TestForLoopInsideFunction',
  7124. LinesToStr([ // statements
  7125. 'this.SumNumbers = function (Count) {',
  7126. ' var Result = 0;',
  7127. ' var vI = 0;',
  7128. ' var vJ = 0;',
  7129. ' vJ = 0;',
  7130. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7131. ' vI = $l1;',
  7132. ' vJ = vJ + vI;',
  7133. ' };',
  7134. ' return Result;',
  7135. '};'
  7136. ]),
  7137. LinesToStr([ // $mod.$main
  7138. ' $mod.SumNumbers(3);'
  7139. ]));
  7140. end;
  7141. procedure TTestModule.TestForLoop_ReadVarAfter;
  7142. begin
  7143. StartProgram(false);
  7144. Add('var');
  7145. Add(' vI: longint;');
  7146. Add('begin');
  7147. Add(' for vi:=1 to 2 do ;');
  7148. Add(' if vi=3 then ;');
  7149. ConvertProgram;
  7150. CheckSource('TestForLoop',
  7151. LinesToStr([ // statements
  7152. 'this.vI = 0;'
  7153. ]),
  7154. LinesToStr([ // this.$main
  7155. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7156. ' if ($mod.vI===3) ;'
  7157. ]));
  7158. end;
  7159. procedure TTestModule.TestForLoop_Nested;
  7160. begin
  7161. StartProgram(false);
  7162. Add('function SumNumbers(Count: longint): longint;');
  7163. Add('var');
  7164. Add(' vI, vJ, vK: longint;');
  7165. Add('begin');
  7166. Add(' VK:=0;');
  7167. Add(' for VI:=1 to count do');
  7168. Add(' begin');
  7169. Add(' for vj:=1 to vi do');
  7170. Add(' begin');
  7171. Add(' vk:=VK+VI;');
  7172. Add(' end;');
  7173. Add(' end;');
  7174. Add('end;');
  7175. Add('begin');
  7176. Add(' sumnumbers(3);');
  7177. ConvertProgram;
  7178. CheckSource('TestForLoopInFunction',
  7179. LinesToStr([ // statements
  7180. 'this.SumNumbers = function (Count) {',
  7181. ' var Result = 0;',
  7182. ' var vI = 0;',
  7183. ' var vJ = 0;',
  7184. ' var vK = 0;',
  7185. ' vK = 0;',
  7186. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7187. ' vI = $l1;',
  7188. ' for (var $l3 = 1, $end4 = vI; $l3 <= $end4; $l3++) {',
  7189. ' vJ = $l3;',
  7190. ' vK = vK + vI;',
  7191. ' };',
  7192. ' };',
  7193. ' return Result;',
  7194. '};'
  7195. ]),
  7196. LinesToStr([ // $mod.$main
  7197. ' $mod.SumNumbers(3);'
  7198. ]));
  7199. end;
  7200. procedure TTestModule.TestRepeatUntil;
  7201. begin
  7202. StartProgram(false);
  7203. Add('var');
  7204. Add(' vI, vJ, vN: longint;');
  7205. Add('begin');
  7206. Add(' vn:=3;');
  7207. Add(' vj:=0;');
  7208. Add(' VI:=0;');
  7209. Add(' repeat');
  7210. Add(' VI:=vi+1;');
  7211. Add(' vj:=VJ+vI;');
  7212. Add(' until vi>=vn');
  7213. ConvertProgram;
  7214. CheckSource('TestRepeatUntil',
  7215. LinesToStr([ // statements
  7216. 'this.vI = 0;',
  7217. 'this.vJ = 0;',
  7218. 'this.vN = 0;'
  7219. ]),
  7220. LinesToStr([ // $mod.$main
  7221. ' $mod.vN = 3;',
  7222. ' $mod.vJ = 0;',
  7223. ' $mod.vI = 0;',
  7224. ' do{',
  7225. ' $mod.vI = $mod.vI + 1;',
  7226. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7227. ' }while(!($mod.vI>=$mod.vN));'
  7228. ]));
  7229. end;
  7230. procedure TTestModule.TestAsmBlock;
  7231. begin
  7232. StartProgram(false);
  7233. Add([
  7234. 'var',
  7235. ' vI: longint;',
  7236. 'begin',
  7237. ' vi:=1;',
  7238. ' asm',
  7239. ' if (vI===1) {',
  7240. ' vI=2;',
  7241. //' console.log(''end;'');', ToDo
  7242. ' }',
  7243. ' if (vI===2){ vI=3; }',
  7244. ' end;',
  7245. ' VI:=4;']);
  7246. ConvertProgram;
  7247. CheckSource('TestAsmBlock',
  7248. LinesToStr([ // statements
  7249. 'this.vI = 0;'
  7250. ]),
  7251. LinesToStr([ // $mod.$main
  7252. '$mod.vI = 1;',
  7253. 'if (vI===1) {',
  7254. ' vI=2;',
  7255. '}',
  7256. 'if (vI===2){ vI=3; }',
  7257. ';',
  7258. '$mod.vI = 4;'
  7259. ]));
  7260. end;
  7261. procedure TTestModule.TestAsmPas_Impl;
  7262. begin
  7263. StartUnit(false);
  7264. Add('interface');
  7265. Add('const cIntf: longint = 1;');
  7266. Add('var vIntf: longint;');
  7267. Add('implementation');
  7268. Add('const cImpl: longint = 2;');
  7269. Add('var vImpl: longint;');
  7270. Add('procedure DoIt;');
  7271. Add('const cLoc: longint = 3;');
  7272. Add('var vLoc: longint;');
  7273. Add('begin;');
  7274. Add(' asm');
  7275. //Add(' pas(vIntf)=pas(cIntf);');
  7276. //Add(' pas(vImpl)=pas(cImpl);');
  7277. //Add(' pas(vLoc)=pas(cLoc);');
  7278. Add(' end;');
  7279. Add('end;');
  7280. ConvertUnit;
  7281. CheckSource('TestAsmPas_Impl',
  7282. LinesToStr([
  7283. 'var $impl = $mod.$impl;',
  7284. 'this.cIntf = 1;',
  7285. 'this.vIntf = 0;',
  7286. '']),
  7287. '', // this.$init
  7288. LinesToStr([ // implementation
  7289. '$impl.cImpl = 2;',
  7290. '$impl.vImpl = 0;',
  7291. 'var cLoc = 3;',
  7292. '$impl.DoIt = function () {',
  7293. ' var vLoc = 0;',
  7294. '};',
  7295. '']) );
  7296. end;
  7297. procedure TTestModule.TestTryFinally;
  7298. begin
  7299. StartProgram(false);
  7300. Add('var i: longint;');
  7301. Add('begin');
  7302. Add(' try');
  7303. Add(' i:=0; i:=2 div i;');
  7304. Add(' finally');
  7305. Add(' i:=3');
  7306. Add(' end;');
  7307. ConvertProgram;
  7308. CheckSource('TestTryFinally',
  7309. LinesToStr([ // statements
  7310. 'this.i = 0;'
  7311. ]),
  7312. LinesToStr([ // $mod.$main
  7313. 'try {',
  7314. ' $mod.i = 0;',
  7315. ' $mod.i = Math.floor(2 / $mod.i);',
  7316. '} finally {',
  7317. ' $mod.i = 3;',
  7318. '};'
  7319. ]));
  7320. end;
  7321. procedure TTestModule.TestTryExcept;
  7322. begin
  7323. StartProgram(false);
  7324. Add('type');
  7325. Add(' TObject = class end;');
  7326. Add(' Exception = class Msg: string; end;');
  7327. Add(' EInvalidCast = class(Exception) end;');
  7328. Add('var vI: longint;');
  7329. Add('begin');
  7330. Add(' try');
  7331. Add(' vi:=1;');
  7332. Add(' except');
  7333. Add(' vi:=2');
  7334. Add(' end;');
  7335. Add(' try');
  7336. Add(' vi:=3;');
  7337. Add(' except');
  7338. Add(' raise;');
  7339. Add(' end;');
  7340. Add(' try');
  7341. Add(' VI:=4;');
  7342. Add(' except');
  7343. Add(' on einvalidcast do');
  7344. Add(' raise;');
  7345. Add(' on E: exception do');
  7346. Add(' if e.msg='''' then');
  7347. Add(' raise e;');
  7348. Add(' else');
  7349. Add(' vi:=5');
  7350. Add(' end;');
  7351. Add(' try');
  7352. Add(' VI:=6;');
  7353. Add(' except');
  7354. Add(' on einvalidcast do ;');
  7355. Add(' end;');
  7356. ConvertProgram;
  7357. CheckSource('TestTryExcept',
  7358. LinesToStr([ // statements
  7359. 'rtl.createClass($mod, "TObject", null, function () {',
  7360. ' this.$init = function () {',
  7361. ' };',
  7362. ' this.$final = function () {',
  7363. ' };',
  7364. '});',
  7365. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7366. ' this.$init = function () {',
  7367. ' $mod.TObject.$init.call(this);',
  7368. ' this.Msg = "";',
  7369. ' };',
  7370. '});',
  7371. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7372. '});',
  7373. 'this.vI = 0;'
  7374. ]),
  7375. LinesToStr([ // $mod.$main
  7376. 'try {',
  7377. ' $mod.vI = 1;',
  7378. '} catch ($e) {',
  7379. ' $mod.vI = 2;',
  7380. '};',
  7381. 'try {',
  7382. ' $mod.vI = 3;',
  7383. '} catch ($e) {',
  7384. ' throw $e;',
  7385. '};',
  7386. 'try {',
  7387. ' $mod.vI = 4;',
  7388. '} catch ($e) {',
  7389. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7390. ' throw $e',
  7391. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7392. ' var E = $e;',
  7393. ' if (E.Msg === "") throw E;',
  7394. ' } else {',
  7395. ' $mod.vI = 5;',
  7396. ' }',
  7397. '};',
  7398. 'try {',
  7399. ' $mod.vI = 6;',
  7400. '} catch ($e) {',
  7401. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7402. ' } else throw $e',
  7403. '};',
  7404. '']));
  7405. end;
  7406. procedure TTestModule.TestTryExcept_ReservedWords;
  7407. begin
  7408. StartProgram(false);
  7409. Add([
  7410. 'type',
  7411. ' TObject = class end;',
  7412. ' Exception = class',
  7413. ' Symbol: string;',
  7414. ' end;',
  7415. 'var &try: longint;',
  7416. 'begin',
  7417. ' try',
  7418. ' &try:=4;',
  7419. ' except',
  7420. ' on Error: exception do',
  7421. ' if errOR.symBol='''' then',
  7422. ' raise ERRor;',
  7423. ' end;',
  7424. '']);
  7425. ConvertProgram;
  7426. CheckSource('TestTryExcept_ReservedWords',
  7427. LinesToStr([ // statements
  7428. 'rtl.createClass($mod, "TObject", null, function () {',
  7429. ' this.$init = function () {',
  7430. ' };',
  7431. ' this.$final = function () {',
  7432. ' };',
  7433. '});',
  7434. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7435. ' this.$init = function () {',
  7436. ' $mod.TObject.$init.call(this);',
  7437. ' this.Symbol = "";',
  7438. ' };',
  7439. '});',
  7440. 'this.Try = 0;',
  7441. '']),
  7442. LinesToStr([ // $mod.$main
  7443. 'try {',
  7444. ' $mod.Try = 4;',
  7445. '} catch ($e) {',
  7446. ' if ($mod.Exception.isPrototypeOf($e)) {',
  7447. ' var error = $e;',
  7448. ' if (error.Symbol === "") throw error;',
  7449. ' } else throw $e',
  7450. '};',
  7451. '']));
  7452. end;
  7453. procedure TTestModule.TestIfThenRaiseElse;
  7454. begin
  7455. StartProgram(false);
  7456. Add([
  7457. 'type',
  7458. ' TObject = class',
  7459. ' constructor Create;',
  7460. ' end;',
  7461. 'constructor TObject.Create;',
  7462. 'begin',
  7463. 'end;',
  7464. 'var b: boolean;',
  7465. 'begin',
  7466. ' if b then',
  7467. ' raise TObject.Create',
  7468. ' else',
  7469. ' b:=false;',
  7470. '']);
  7471. ConvertProgram;
  7472. CheckSource('TestIfThenRaiseElse',
  7473. LinesToStr([ // statements
  7474. 'rtl.createClass($mod, "TObject", null, function () {',
  7475. ' this.$init = function () {',
  7476. ' };',
  7477. ' this.$final = function () {',
  7478. ' };',
  7479. ' this.Create = function () {',
  7480. ' return this;',
  7481. ' };',
  7482. '});',
  7483. 'this.b = false;',
  7484. '']),
  7485. LinesToStr([ // $mod.$main
  7486. 'if ($mod.b) {',
  7487. ' throw $mod.TObject.$create("Create")}',
  7488. ' else $mod.b = false;',
  7489. '']));
  7490. end;
  7491. procedure TTestModule.TestCaseOf;
  7492. begin
  7493. StartProgram(false);
  7494. Add([
  7495. 'const e: longint; external name ''$e'';',
  7496. 'var vI: longint;',
  7497. 'begin',
  7498. ' case vi of',
  7499. ' 1: ;',
  7500. ' 2: vi:=3;',
  7501. ' e: ;',
  7502. ' else',
  7503. ' VI:=4',
  7504. ' end;']);
  7505. ConvertProgram;
  7506. CheckSource('TestCaseOf',
  7507. LinesToStr([ // statements
  7508. 'this.vI = 0;'
  7509. ]),
  7510. LinesToStr([ // $mod.$main
  7511. 'var $tmp1 = $mod.vI;',
  7512. 'if ($tmp1 === 1) {}',
  7513. 'else if ($tmp1 === 2) {',
  7514. ' $mod.vI = 3}',
  7515. ' else if ($tmp1 === $e) {}',
  7516. 'else {',
  7517. ' $mod.vI = 4;',
  7518. '};'
  7519. ]));
  7520. end;
  7521. procedure TTestModule.TestCaseOf_UseSwitch;
  7522. begin
  7523. StartProgram(false);
  7524. Converter.UseSwitchStatement:=true;
  7525. Add('var Vi: longint;');
  7526. Add('begin');
  7527. Add(' case vi of');
  7528. Add(' 1: ;');
  7529. Add(' 2: VI:=3;');
  7530. Add(' else');
  7531. Add(' vi:=4');
  7532. Add(' end;');
  7533. ConvertProgram;
  7534. CheckSource('TestCaseOf_UseSwitch',
  7535. LinesToStr([ // statements
  7536. 'this.Vi = 0;'
  7537. ]),
  7538. LinesToStr([ // $mod.$main
  7539. 'switch ($mod.Vi) {',
  7540. 'case 1:',
  7541. ' break;',
  7542. 'case 2:',
  7543. ' $mod.Vi = 3;',
  7544. ' break;',
  7545. 'default:',
  7546. ' $mod.Vi = 4;',
  7547. '};'
  7548. ]));
  7549. end;
  7550. procedure TTestModule.TestCaseOfNoElse;
  7551. begin
  7552. StartProgram(false);
  7553. Add('var Vi: longint;');
  7554. Add('begin');
  7555. Add(' case vi of');
  7556. Add(' 1: begin vi:=2; VI:=3; end;');
  7557. Add(' end;');
  7558. ConvertProgram;
  7559. CheckSource('TestCaseOfNoElse',
  7560. LinesToStr([ // statements
  7561. 'this.Vi = 0;'
  7562. ]),
  7563. LinesToStr([ // $mod.$main
  7564. 'var $tmp1 = $mod.Vi;',
  7565. 'if ($tmp1 === 1) {',
  7566. ' $mod.Vi = 2;',
  7567. ' $mod.Vi = 3;',
  7568. '};'
  7569. ]));
  7570. end;
  7571. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  7572. begin
  7573. StartProgram(false);
  7574. Converter.UseSwitchStatement:=true;
  7575. Add('var vI: longint;');
  7576. Add('begin');
  7577. Add(' case vi of');
  7578. Add(' 1: begin VI:=2; vi:=3; end;');
  7579. Add(' end;');
  7580. ConvertProgram;
  7581. CheckSource('TestCaseOfNoElse_UseSwitch',
  7582. LinesToStr([ // statements
  7583. 'this.vI = 0;'
  7584. ]),
  7585. LinesToStr([ // $mod.$main
  7586. 'switch ($mod.vI) {',
  7587. 'case 1:',
  7588. ' $mod.vI = 2;',
  7589. ' $mod.vI = 3;',
  7590. ' break;',
  7591. '};'
  7592. ]));
  7593. end;
  7594. procedure TTestModule.TestCaseOfRange;
  7595. begin
  7596. StartProgram(false);
  7597. Add('var vI: longint;');
  7598. Add('begin');
  7599. Add(' case vi of');
  7600. Add(' 1..3: vi:=14;');
  7601. Add(' 4,5: vi:=16;');
  7602. Add(' 6..7,9..10: ;');
  7603. Add(' else ;');
  7604. Add(' end;');
  7605. ConvertProgram;
  7606. CheckSource('TestCaseOfRange',
  7607. LinesToStr([ // statements
  7608. 'this.vI = 0;'
  7609. ]),
  7610. LinesToStr([ // $mod.$main
  7611. 'var $tmp1 = $mod.vI;',
  7612. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  7613. ' $mod.vI = 14',
  7614. '} else if (($tmp1 === 4) || ($tmp1 === 5)){',
  7615. ' $mod.vI = 16',
  7616. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  7617. ]));
  7618. end;
  7619. procedure TTestModule.TestCaseOfString;
  7620. begin
  7621. StartProgram(false);
  7622. Add([
  7623. 'var s,h: string;',
  7624. 'begin',
  7625. ' case s of',
  7626. ' ''foo'': s:=h;',
  7627. ' ''a''..''z'': h:=s;',
  7628. ' ''ў'', ''ё'': ;',
  7629. ' ''Б''..''Я'': ;',
  7630. ' end;',
  7631. '']);
  7632. ConvertProgram;
  7633. CheckSource('TestCaseOfString',
  7634. LinesToStr([ // statements
  7635. 'this.s = "";',
  7636. 'this.h = "";',
  7637. '']),
  7638. LinesToStr([ // $mod.$main
  7639. 'var $tmp1 = $mod.s;',
  7640. 'if ($tmp1 === "foo") {',
  7641. ' $mod.s = $mod.h}',
  7642. ' else if (($tmp1.length === 1) && ($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7643. ' $mod.h = $mod.s}',
  7644. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  7645. ' else if (($tmp1.length === 1) && ($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7646. '']));
  7647. end;
  7648. procedure TTestModule.TestCaseOfChar;
  7649. begin
  7650. StartProgram(false);
  7651. Add([
  7652. 'var s,h: char;',
  7653. 'begin',
  7654. ' case s of',
  7655. ' ''a''..''z'': h:=s;',
  7656. ' ''ä'': ;',
  7657. ' ''ў'', ''ё'': ;',
  7658. ' ''Б''..''Я'': ;',
  7659. ' end;',
  7660. '']);
  7661. ConvertProgram;
  7662. CheckSource('TestCaseOfString',
  7663. LinesToStr([ // statements
  7664. 'this.s = "";',
  7665. 'this.h = "";',
  7666. '']),
  7667. LinesToStr([ // $mod.$main
  7668. 'var $tmp1 = $mod.s;',
  7669. 'if (($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7670. ' $mod.h = $mod.s}',
  7671. ' else if ($tmp1 === "ä") {}',
  7672. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  7673. ' else if (($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7674. '']));
  7675. end;
  7676. procedure TTestModule.TestCaseOfExternalClassConst;
  7677. begin
  7678. StartProgram(false);
  7679. Add([
  7680. '{$modeswitch externalclass}',
  7681. 'type',
  7682. ' TBird = class external name ''Bird''',
  7683. ' const e: longint;',
  7684. ' end;',
  7685. 'var vI: longint;',
  7686. 'begin',
  7687. ' case vi of',
  7688. ' 1: vi:=3;',
  7689. ' TBird.e: ;',
  7690. ' end;']);
  7691. ConvertProgram;
  7692. CheckSource('TestCaseOfExternalClassConst',
  7693. LinesToStr([ // statements
  7694. 'this.vI = 0;'
  7695. ]),
  7696. LinesToStr([ // $mod.$main
  7697. 'var $tmp1 = $mod.vI;',
  7698. 'if ($tmp1 === 1) {',
  7699. ' $mod.vI = 3}',
  7700. ' else if ($tmp1 === Bird.e) ;'
  7701. ]));
  7702. end;
  7703. procedure TTestModule.TestDebugger;
  7704. begin
  7705. StartProgram(false);
  7706. Add([
  7707. 'procedure DoIt;',
  7708. 'begin',
  7709. ' deBugger;',
  7710. ' DeBugger();',
  7711. 'end;',
  7712. 'begin',
  7713. ' Debugger;']);
  7714. ConvertProgram;
  7715. CheckSource('TestDebugger',
  7716. LinesToStr([ // statements
  7717. 'this.DoIt = function () {',
  7718. ' debugger;',
  7719. ' debugger;',
  7720. '};',
  7721. '']),
  7722. LinesToStr([ // $mod.$main
  7723. 'debugger;',
  7724. '']));
  7725. end;
  7726. procedure TTestModule.TestArray_Dynamic;
  7727. begin
  7728. StartProgram(false);
  7729. Add(['type',
  7730. ' TArrayInt = array of longint;',
  7731. 'var',
  7732. ' Arr: TArrayInt;',
  7733. ' i: longint;',
  7734. ' b: boolean;',
  7735. 'begin',
  7736. ' SetLength(arr,3);',
  7737. ' arr[0]:=4;',
  7738. ' arr[1]:=length(arr)+arr[0];',
  7739. ' arr[i]:=5;',
  7740. ' arr[arr[i]]:=arr[6];',
  7741. ' i:=low(arr);',
  7742. ' i:=high(arr);',
  7743. ' b:=Assigned(arr);',
  7744. ' Arr:=default(TArrayInt);']);
  7745. ConvertProgram;
  7746. CheckSource('TestArray_Dynamic',
  7747. LinesToStr([ // statements
  7748. 'this.Arr = [];',
  7749. 'this.i = 0;',
  7750. 'this.b = false;'
  7751. ]),
  7752. LinesToStr([ // $mod.$main
  7753. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  7754. '$mod.Arr[0] = 4;',
  7755. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  7756. '$mod.Arr[$mod.i] = 5;',
  7757. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  7758. '$mod.i = 0;',
  7759. '$mod.i = rtl.length($mod.Arr) - 1;',
  7760. '$mod.b = rtl.length($mod.Arr) > 0;',
  7761. '$mod.Arr = [];',
  7762. '']));
  7763. end;
  7764. procedure TTestModule.TestArray_Dynamic_Nil;
  7765. begin
  7766. StartProgram(false);
  7767. Add('type');
  7768. Add(' TArrayInt = array of longint;');
  7769. Add('var');
  7770. Add(' Arr: TArrayInt;');
  7771. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  7772. Add('begin');
  7773. Add(' arr:=nil;');
  7774. Add(' if arr=nil then;');
  7775. Add(' if nil=arr then;');
  7776. Add(' if arr<>nil then;');
  7777. Add(' if nil<>arr then;');
  7778. Add(' DoIt(nil,nil);');
  7779. ConvertProgram;
  7780. CheckSource('TestArray_Dynamic',
  7781. LinesToStr([ // statements
  7782. 'this.Arr = [];',
  7783. 'this.DoIt = function(i,j){',
  7784. '};'
  7785. ]),
  7786. LinesToStr([ // $mod.$main
  7787. '$mod.Arr = [];',
  7788. 'if (rtl.length($mod.Arr) === 0) ;',
  7789. 'if (rtl.length($mod.Arr) === 0) ;',
  7790. 'if (rtl.length($mod.Arr) > 0) ;',
  7791. 'if (rtl.length($mod.Arr) > 0) ;',
  7792. '$mod.DoIt([],[]);',
  7793. '']));
  7794. end;
  7795. procedure TTestModule.TestArray_DynMultiDimensional;
  7796. begin
  7797. StartProgram(false);
  7798. Add('type');
  7799. Add(' TArrayInt = array of longint;');
  7800. Add(' TArrayArrayInt = array of TArrayInt;');
  7801. Add('var');
  7802. Add(' Arr: TArrayInt;');
  7803. Add(' Arr2: TArrayArrayInt;');
  7804. Add(' i: longint;');
  7805. Add('begin');
  7806. Add(' arr2:=nil;');
  7807. Add(' if arr2=nil then;');
  7808. Add(' if nil=arr2 then;');
  7809. Add(' i:=low(arr2);');
  7810. Add(' i:=low(arr2[1]);');
  7811. Add(' i:=high(arr2);');
  7812. Add(' i:=high(arr2[2]);');
  7813. Add(' arr2[3]:=arr;');
  7814. Add(' arr2[4][5]:=i;');
  7815. Add(' i:=arr2[6][7];');
  7816. Add(' arr2[8,9]:=i;');
  7817. Add(' i:=arr2[10,11];');
  7818. Add(' SetLength(arr2,14);');
  7819. Add(' SetLength(arr2[15],16);');
  7820. ConvertProgram;
  7821. CheckSource('TestArray_Dynamic',
  7822. LinesToStr([ // statements
  7823. 'this.Arr = [];',
  7824. 'this.Arr2 = [];',
  7825. 'this.i = 0;'
  7826. ]),
  7827. LinesToStr([ // $mod.$main
  7828. '$mod.Arr2 = [];',
  7829. 'if (rtl.length($mod.Arr2) === 0) ;',
  7830. 'if (rtl.length($mod.Arr2) === 0) ;',
  7831. '$mod.i = 0;',
  7832. '$mod.i = 0;',
  7833. '$mod.i = rtl.length($mod.Arr2) - 1;',
  7834. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  7835. '$mod.Arr2[3] = $mod.Arr;',
  7836. '$mod.Arr2[4][5] = $mod.i;',
  7837. '$mod.i = $mod.Arr2[6][7];',
  7838. '$mod.Arr2[8][9] = $mod.i;',
  7839. '$mod.i = $mod.Arr2[10][11];',
  7840. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  7841. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  7842. '']));
  7843. end;
  7844. procedure TTestModule.TestArray_StaticInt;
  7845. begin
  7846. StartProgram(false);
  7847. Add('type');
  7848. Add(' TArrayInt = array[2..4] of longint;');
  7849. Add('var');
  7850. Add(' Arr: TArrayInt;');
  7851. Add(' Arr2: TArrayInt = (5,6,7);');
  7852. Add(' i: longint;');
  7853. Add(' b: boolean;');
  7854. Add('begin');
  7855. Add(' arr[2]:=4;');
  7856. Add(' arr[3]:=arr[2]+arr[3];');
  7857. Add(' arr[i]:=5;');
  7858. Add(' arr[arr[i]]:=arr[high(arr)];');
  7859. Add(' i:=low(arr);');
  7860. Add(' i:=high(arr);');
  7861. Add(' b:=arr[2]=arr[3];');
  7862. Add(' arr:=default(TArrayInt);');
  7863. ConvertProgram;
  7864. CheckSource('TestArray_StaticInt',
  7865. LinesToStr([ // statements
  7866. 'this.Arr = rtl.arraySetLength(null,0,3);',
  7867. 'this.Arr2 = [5, 6, 7];',
  7868. 'this.i = 0;',
  7869. 'this.b = false;'
  7870. ]),
  7871. LinesToStr([ // $mod.$main
  7872. '$mod.Arr[0] = 4;',
  7873. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  7874. '$mod.Arr[$mod.i-2] = 5;',
  7875. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  7876. '$mod.i = 2;',
  7877. '$mod.i = 4;',
  7878. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  7879. '$mod.Arr = rtl.arraySetLength(null,0,3).slice(0);',
  7880. '']));
  7881. end;
  7882. procedure TTestModule.TestArray_StaticBool;
  7883. begin
  7884. StartProgram(false);
  7885. Add('type');
  7886. Add(' TBools = array[boolean] of boolean;');
  7887. Add(' TBool2 = array[true..true] of boolean;');
  7888. Add('var');
  7889. Add(' Arr: TBools;');
  7890. Add(' Arr2: TBool2;');
  7891. Add(' Arr3: TBools = (true,false);');
  7892. Add(' b: boolean;');
  7893. Add('begin');
  7894. Add(' b:=low(arr);');
  7895. Add(' b:=high(arr);');
  7896. Add(' arr[true]:=false;');
  7897. Add(' arr[false]:=arr[b] or arr[true];');
  7898. Add(' arr[b]:=true;');
  7899. Add(' arr[arr[b]]:=arr[high(arr)];');
  7900. Add(' b:=arr[false]=arr[true];');
  7901. Add(' b:=low(arr2);');
  7902. Add(' b:=high(arr2);');
  7903. Add(' arr2[true]:=true;');
  7904. Add(' arr2[true]:=arr2[true] and arr2[b];');
  7905. Add(' arr2[b]:=false;');
  7906. ConvertProgram;
  7907. CheckSource('TestArray_StaticBool',
  7908. LinesToStr([ // statements
  7909. 'this.Arr = rtl.arraySetLength(null,false,2);',
  7910. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  7911. 'this.Arr3 = [true, false];',
  7912. 'this.b = false;'
  7913. ]),
  7914. LinesToStr([ // $mod.$main
  7915. '$mod.b = false;',
  7916. '$mod.b = true;',
  7917. '$mod.Arr[1] = false;',
  7918. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  7919. '$mod.Arr[+$mod.b] = true;',
  7920. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  7921. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  7922. '$mod.b = true;',
  7923. '$mod.b = true;',
  7924. '$mod.Arr2[0] = true;',
  7925. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  7926. '$mod.Arr2[1-$mod.b] = false;',
  7927. '']));
  7928. end;
  7929. procedure TTestModule.TestArray_StaticChar;
  7930. begin
  7931. StartProgram(false);
  7932. Add([
  7933. 'type',
  7934. ' TChars = array[char] of char;',
  7935. ' TChars2 = array[''a''..''z''] of char;',
  7936. 'var',
  7937. ' Arr: TChars;',
  7938. ' Arr2: TChars2;',
  7939. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  7940. ' Arr4: array[11..13] of char = ''pas'';',
  7941. ' Arr5: array[21..22] of char = ''äö'';',
  7942. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  7943. ' c: char;',
  7944. ' b: boolean;',
  7945. 'begin',
  7946. ' c:=low(arr);',
  7947. ' c:=high(arr);',
  7948. ' arr[''B'']:=''a'';',
  7949. ' arr[''D'']:=arr[c];',
  7950. ' arr[c]:=arr[''d''];',
  7951. ' arr[arr[c]]:=arr[high(arr)];',
  7952. ' b:=arr[low(arr)]=arr[''e''];',
  7953. ' c:=low(arr2);',
  7954. ' c:=high(arr2);',
  7955. ' arr2[''b'']:=''f'';',
  7956. ' arr2[''a'']:=arr2[c];',
  7957. ' arr2[c]:=arr2[''g''];']);
  7958. ConvertProgram;
  7959. CheckSource('TestArray_StaticChar',
  7960. LinesToStr([ // statements
  7961. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  7962. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  7963. 'this.Arr3 = ["p", "a", "s"];',
  7964. 'this.Arr4 = ["p", "a", "s"];',
  7965. 'this.Arr5 = ["ä", "ö"];',
  7966. 'this.Arr6 = ["ä", "ö"];',
  7967. 'this.c = "";',
  7968. 'this.b = false;',
  7969. '']),
  7970. LinesToStr([ // $mod.$main
  7971. '$mod.c = "\x00";',
  7972. '$mod.c = "\uFFFF";',
  7973. '$mod.Arr[66] = "a";',
  7974. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  7975. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  7976. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  7977. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  7978. '$mod.c = "a";',
  7979. '$mod.c = "z";',
  7980. '$mod.Arr2[1] = "f";',
  7981. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  7982. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  7983. '']));
  7984. end;
  7985. procedure TTestModule.TestArray_StaticMultiDim;
  7986. begin
  7987. StartProgram(false);
  7988. Add([
  7989. 'type',
  7990. ' TArrayInt = array[1..3] of longint;',
  7991. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  7992. 'var',
  7993. ' Arr: TArrayInt;',
  7994. ' Arr2: TArrayArrayInt;',
  7995. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  7996. ' i: longint;',
  7997. 'begin',
  7998. ' i:=low(arr);',
  7999. ' i:=low(arr2);',
  8000. ' i:=low(arr2[5]);',
  8001. ' i:=high(arr);',
  8002. ' i:=high(arr2);',
  8003. ' i:=high(arr2[6]);',
  8004. ' arr2[5]:=arr;',
  8005. ' arr2[6][2]:=i;',
  8006. ' i:=arr2[6][3];',
  8007. ' arr2[6,3]:=i;',
  8008. ' i:=arr2[5,2];',
  8009. ' arr2:=arr2;',// clone multi dim static array
  8010. //' arr3:=arr3;',// clone anonymous multi dim static array
  8011. '']);
  8012. ConvertProgram;
  8013. CheckSource('TestArray_StaticMultiDim',
  8014. LinesToStr([ // statements
  8015. 'this.TArrayArrayInt$clone = function (a) {',
  8016. ' var r = [];',
  8017. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8018. ' return r;',
  8019. '};',
  8020. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8021. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8022. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8023. 'this.i = 0;'
  8024. ]),
  8025. LinesToStr([ // $mod.$main
  8026. '$mod.i = 1;',
  8027. '$mod.i = 5;',
  8028. '$mod.i = 1;',
  8029. '$mod.i = 3;',
  8030. '$mod.i = 6;',
  8031. '$mod.i = 3;',
  8032. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8033. '$mod.Arr2[1][1] = $mod.i;',
  8034. '$mod.i = $mod.Arr2[1][2];',
  8035. '$mod.Arr2[1][2] = $mod.i;',
  8036. '$mod.i = $mod.Arr2[0][1];',
  8037. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8038. '']));
  8039. end;
  8040. procedure TTestModule.TestArray_StaticInFunction;
  8041. begin
  8042. StartProgram(false);
  8043. Add([
  8044. 'const TArrayInt = 3;',
  8045. 'const TArrayArrayInt = 4;',
  8046. 'procedure DoIt;',
  8047. 'type',
  8048. ' TArrayInt = array[1..3] of longint;',
  8049. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8050. 'var',
  8051. ' Arr: TArrayInt;',
  8052. ' Arr2: TArrayArrayInt;',
  8053. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8054. ' i: longint;',
  8055. 'begin',
  8056. ' arr2[5]:=arr;',
  8057. ' arr2:=arr2;',// clone multi dim static array
  8058. 'end;',
  8059. 'begin',
  8060. '']);
  8061. ConvertProgram;
  8062. CheckSource('TestArray_StaticInFunction',
  8063. LinesToStr([ // statements
  8064. 'this.TArrayInt = 3;',
  8065. 'this.TArrayArrayInt = 4;',
  8066. 'var TArrayArrayInt$1$clone = function (a) {',
  8067. ' var r = [];',
  8068. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8069. ' return r;',
  8070. '};',
  8071. 'this.DoIt = function () {',
  8072. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8073. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8074. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8075. ' var i = 0;',
  8076. ' Arr2[0] = Arr.slice(0);',
  8077. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8078. '};',
  8079. '']),
  8080. LinesToStr([ // $mod.$main
  8081. '']));
  8082. end;
  8083. procedure TTestModule.TestArrayOfRecord;
  8084. begin
  8085. StartProgram(false);
  8086. Add([
  8087. 'type',
  8088. ' TRec = record',
  8089. ' Int: longint;',
  8090. ' end;',
  8091. ' TArrayRec = array of TRec;',
  8092. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8093. 'begin',
  8094. 'end;',
  8095. 'var',
  8096. ' Arr: TArrayRec;',
  8097. ' r: TRec;',
  8098. ' i: longint;',
  8099. 'begin',
  8100. ' SetLength(arr,3);',
  8101. ' arr[0].int:=4;',
  8102. ' arr[1].int:=length(arr)+arr[2].int;',
  8103. ' arr[arr[i].int].int:=arr[5].int;',
  8104. ' arr[7]:=r;',
  8105. ' r:=arr[8];',
  8106. ' i:=low(arr);',
  8107. ' i:=high(arr);',
  8108. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8109. ConvertProgram;
  8110. CheckSource('TestArrayOfRecord',
  8111. LinesToStr([ // statements
  8112. 'rtl.recNewT($mod, "TRec", function () {',
  8113. ' this.Int = 0;',
  8114. ' this.$eq = function (b) {',
  8115. ' return this.Int === b.Int;',
  8116. ' };',
  8117. ' this.$assign = function (s) {',
  8118. ' this.Int = s.Int;',
  8119. ' return this;',
  8120. ' };',
  8121. '});',
  8122. 'this.DoIt = function (vd, vc, vv) {',
  8123. '};',
  8124. 'this.Arr = [];',
  8125. 'this.r = $mod.TRec.$new();',
  8126. 'this.i = 0;'
  8127. ]),
  8128. LinesToStr([ // $mod.$main
  8129. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8130. '$mod.Arr[0].Int = 4;',
  8131. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8132. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8133. '$mod.Arr[7].$assign($mod.r);',
  8134. '$mod.r.$assign($mod.Arr[8]);',
  8135. '$mod.i = 0;',
  8136. '$mod.i = rtl.length($mod.Arr)-1;',
  8137. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8138. '']));
  8139. end;
  8140. procedure TTestModule.TestArray_StaticRecord;
  8141. begin
  8142. StartProgram(false);
  8143. Add([
  8144. 'type',
  8145. ' TRec = record',
  8146. ' Int: longint;',
  8147. ' end;',
  8148. ' TArrayRec = array[1..2] of TRec;',
  8149. 'var',
  8150. ' Arr: TArrayRec;',
  8151. 'begin',
  8152. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8153. '']);
  8154. ConvertProgram;
  8155. CheckSource('TestArray_StaticRecord',
  8156. LinesToStr([ // statements
  8157. 'rtl.recNewT($mod, "TRec", function () {',
  8158. ' this.Int = 0;',
  8159. ' this.$eq = function (b) {',
  8160. ' return this.Int === b.Int;',
  8161. ' };',
  8162. ' this.$assign = function (s) {',
  8163. ' this.Int = s.Int;',
  8164. ' return this;',
  8165. ' };',
  8166. '});',
  8167. 'this.TArrayRec$clone = function (a) {',
  8168. ' var r = [];',
  8169. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8170. ' return r;',
  8171. '};',
  8172. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8173. '']),
  8174. LinesToStr([ // $mod.$main
  8175. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8176. end;
  8177. procedure TTestModule.TestArrayOfSet;
  8178. begin
  8179. StartProgram(false);
  8180. Add([
  8181. 'type',
  8182. ' TFlag = (big,small);',
  8183. ' TSetOfFlag = set of tflag;',
  8184. ' TArrayFlag = array of TSetOfFlag;',
  8185. 'procedure DoIt(const a: Tarrayflag);',
  8186. 'begin',
  8187. 'end;',
  8188. 'var',
  8189. ' f: TFlag;',
  8190. ' s: TSetOfFlag;',
  8191. ' Arr: TArrayFlag;',
  8192. ' i: longint;',
  8193. 'begin',
  8194. ' SetLength(arr,3);',
  8195. ' arr[0]:=s;',
  8196. ' arr[1]:=[big];',
  8197. ' arr[2]:=[big]+s;',
  8198. ' arr[3]:=s+[big];',
  8199. ' arr[4]:=arr[5];',
  8200. ' s:=arr[6];',
  8201. ' i:=low(arr);',
  8202. ' i:=high(arr);',
  8203. ' DoIt(arr);',
  8204. ' DoIt([s]);',
  8205. ' DoIt([[],s]);',
  8206. ' DoIt([s,[]]);',
  8207. '']);
  8208. ConvertProgram;
  8209. CheckSource('TestArrayOfSet',
  8210. LinesToStr([ // statements
  8211. 'this.TFlag = {',
  8212. ' "0": "big",',
  8213. ' big: 0,',
  8214. ' "1": "small",',
  8215. ' small: 1',
  8216. '};',
  8217. 'this.DoIt = function (a) {',
  8218. '};',
  8219. 'this.f = 0;',
  8220. 'this.s = {};',
  8221. 'this.Arr = [];',
  8222. 'this.i = 0;',
  8223. '']),
  8224. LinesToStr([ // $mod.$main
  8225. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8226. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8227. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8228. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8229. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8230. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8231. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8232. '$mod.i = 0;',
  8233. '$mod.i = rtl.length($mod.Arr) - 1;',
  8234. '$mod.DoIt($mod.Arr);',
  8235. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8236. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8237. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8238. '']));
  8239. end;
  8240. procedure TTestModule.TestArray_DynAsParam;
  8241. begin
  8242. StartProgram(false);
  8243. Add([
  8244. 'type integer = longint;',
  8245. 'type TArrInt = array of integer;',
  8246. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8247. 'var vJ: TArrInt;',
  8248. 'begin',
  8249. ' vg:=vg;',
  8250. ' vj:=vh;',
  8251. ' vi:=vi;',
  8252. ' doit(vg,vg,vg);',
  8253. ' doit(vh,vh,vj);',
  8254. ' doit(vi,vi,vi);',
  8255. ' doit(vj,vj,vj);',
  8256. 'end;',
  8257. 'var i: TArrInt;',
  8258. 'begin',
  8259. ' doit(i,i,i);']);
  8260. ConvertProgram;
  8261. CheckSource('TestArray_DynAsParams',
  8262. LinesToStr([ // statements
  8263. 'this.DoIt = function (vG,vH,vI) {',
  8264. ' var vJ = [];',
  8265. ' vG = vG;',
  8266. ' vJ = vH;',
  8267. ' vI.set(vI.get());',
  8268. ' $mod.DoIt(vG, vG, {',
  8269. ' get: function () {',
  8270. ' return vG;',
  8271. ' },',
  8272. ' set: function (v) {',
  8273. ' vG = v;',
  8274. ' }',
  8275. ' });',
  8276. ' $mod.DoIt(vH, vH, {',
  8277. ' get: function () {',
  8278. ' return vJ;',
  8279. ' },',
  8280. ' set: function (v) {',
  8281. ' vJ = v;',
  8282. ' }',
  8283. ' });',
  8284. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8285. ' $mod.DoIt(vJ, vJ, {',
  8286. ' get: function () {',
  8287. ' return vJ;',
  8288. ' },',
  8289. ' set: function (v) {',
  8290. ' vJ = v;',
  8291. ' }',
  8292. ' });',
  8293. '};',
  8294. 'this.i = [];'
  8295. ]),
  8296. LinesToStr([
  8297. '$mod.DoIt($mod.i,$mod.i,{',
  8298. ' p: $mod,',
  8299. ' get: function () {',
  8300. ' return this.p.i;',
  8301. ' },',
  8302. ' set: function (v) {',
  8303. ' this.p.i = v;',
  8304. ' }',
  8305. '});'
  8306. ]));
  8307. end;
  8308. procedure TTestModule.TestArray_StaticAsParam;
  8309. begin
  8310. StartProgram(false);
  8311. Add([
  8312. 'type integer = longint;',
  8313. 'type TArrInt = array[1..2] of integer;',
  8314. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8315. 'var vJ: TArrInt;',
  8316. 'begin',
  8317. ' vg:=vg;',
  8318. ' vj:=vh;',
  8319. ' vi:=vi;',
  8320. ' doit(vg,vg,vg);',
  8321. ' doit(vh,vh,vj);',
  8322. ' doit(vi,vi,vi);',
  8323. ' doit(vj,vj,vj);',
  8324. 'end;',
  8325. 'var i: TArrInt;',
  8326. 'begin',
  8327. ' doit(i,i,i);']);
  8328. ConvertProgram;
  8329. CheckSource('TestArray_StaticAsParams',
  8330. LinesToStr([ // statements
  8331. 'this.DoIt = function (vG,vH,vI) {',
  8332. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  8333. ' vG = vG.slice(0);',
  8334. ' vJ = vH.slice(0);',
  8335. ' vI.set(vI.get().slice(0));',
  8336. ' $mod.DoIt(vG.slice(0), vG, {',
  8337. ' get: function () {',
  8338. ' return vG;',
  8339. ' },',
  8340. ' set: function (v) {',
  8341. ' vG = v;',
  8342. ' }',
  8343. ' });',
  8344. ' $mod.DoIt(vH.slice(0), vH, {',
  8345. ' get: function () {',
  8346. ' return vJ;',
  8347. ' },',
  8348. ' set: function (v) {',
  8349. ' vJ = v;',
  8350. ' }',
  8351. ' });',
  8352. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  8353. ' $mod.DoIt(vJ.slice(0), vJ, {',
  8354. ' get: function () {',
  8355. ' return vJ;',
  8356. ' },',
  8357. ' set: function (v) {',
  8358. ' vJ = v;',
  8359. ' }',
  8360. ' });',
  8361. '};',
  8362. 'this.i = rtl.arraySetLength(null, 0, 2);'
  8363. ]),
  8364. LinesToStr([
  8365. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  8366. ' p: $mod,',
  8367. ' get: function () {',
  8368. ' return this.p.i;',
  8369. ' },',
  8370. ' set: function (v) {',
  8371. ' this.p.i = v;',
  8372. ' }',
  8373. '});'
  8374. ]));
  8375. end;
  8376. procedure TTestModule.TestArrayElement_AsParams;
  8377. begin
  8378. StartProgram(false);
  8379. Add('type integer = longint;');
  8380. Add('type TArrayInt = array of integer;');
  8381. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  8382. Add('var vJ: tarrayint;');
  8383. Add('begin');
  8384. Add(' vi:=vi;');
  8385. Add(' doit(vi,vi,vi);');
  8386. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  8387. Add('end;');
  8388. Add('var a: TArrayInt;');
  8389. Add('begin');
  8390. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  8391. ConvertProgram;
  8392. CheckSource('TestArrayElement_AsParams',
  8393. LinesToStr([ // statements
  8394. 'this.DoIt = function (vG,vH,vI) {',
  8395. ' var vJ = [];',
  8396. ' vI.set(vI.get());',
  8397. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8398. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  8399. ' a:1+3,',
  8400. ' p:vJ,',
  8401. ' get: function () {',
  8402. ' return this.p[this.a];',
  8403. ' },',
  8404. ' set: function (v) {',
  8405. ' this.p[this.a] = v;',
  8406. ' }',
  8407. ' });',
  8408. '};',
  8409. 'this.a = [];'
  8410. ]),
  8411. LinesToStr([
  8412. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  8413. ' a: 1+6,',
  8414. ' p: $mod.a,',
  8415. ' get: function () {',
  8416. ' return this.p[this.a];',
  8417. ' },',
  8418. ' set: function (v) {',
  8419. ' this.p[this.a] = v;',
  8420. ' }',
  8421. '});'
  8422. ]));
  8423. end;
  8424. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  8425. begin
  8426. StartProgram(false);
  8427. Add('type Integer = longint;');
  8428. Add('type TArrayInt = array of integer;');
  8429. Add('function GetArr(vB: integer = 0): tarrayint;');
  8430. Add('begin');
  8431. Add('end;');
  8432. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  8433. Add('begin');
  8434. Add('end;');
  8435. Add('begin');
  8436. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  8437. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  8438. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  8439. ConvertProgram;
  8440. CheckSource('TestArrayElementFromFuncResult_AsParams',
  8441. LinesToStr([ // statements
  8442. 'this.GetArr = function (vB) {',
  8443. ' var Result = [];',
  8444. ' return Result;',
  8445. '};',
  8446. 'this.DoIt = function (vG,vH,vI) {',
  8447. '};'
  8448. ]),
  8449. LinesToStr([
  8450. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  8451. ' a: 1+3,',
  8452. ' p: $mod.GetArr(0),',
  8453. ' get: function () {',
  8454. ' return this.p[this.a];',
  8455. ' },',
  8456. ' set: function (v) {',
  8457. ' this.p[this.a] = v;',
  8458. ' }',
  8459. '});',
  8460. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  8461. ' a: 2+3,',
  8462. ' p: $mod.GetArr(0),',
  8463. ' get: function () {',
  8464. ' return this.p[this.a];',
  8465. ' },',
  8466. ' set: function (v) {',
  8467. ' this.p[this.a] = v;',
  8468. ' }',
  8469. '});',
  8470. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  8471. ' a: 3+3,',
  8472. ' p: $mod.GetArr(9),',
  8473. ' get: function () {',
  8474. ' return this.p[this.a];',
  8475. ' },',
  8476. ' set: function (v) {',
  8477. ' this.p[this.a] = v;',
  8478. ' }',
  8479. '});',
  8480. '']));
  8481. end;
  8482. procedure TTestModule.TestArrayEnumTypeRange;
  8483. begin
  8484. StartProgram(false);
  8485. Add([
  8486. 'type',
  8487. ' TEnum = (red,blue);',
  8488. ' TEnumArray = array[TEnum] of longint;',
  8489. 'var',
  8490. ' e: TEnum;',
  8491. ' i: longint;',
  8492. ' a: TEnumArray;',
  8493. ' numbers: TEnumArray = (1,2);',
  8494. ' names: array[TEnum] of string = (''red'',''blue'');',
  8495. 'begin',
  8496. ' e:=low(a);',
  8497. ' e:=high(a);',
  8498. ' i:=a[red];',
  8499. ' a[e]:=a[e];']);
  8500. ConvertProgram;
  8501. CheckSource('TestArrayEnumTypeRange',
  8502. LinesToStr([ // statements
  8503. ' this.TEnum = {',
  8504. ' "0": "red",',
  8505. ' red: 0,',
  8506. ' "1": "blue",',
  8507. ' blue: 1',
  8508. '};',
  8509. 'this.e = 0;',
  8510. 'this.i = 0;',
  8511. 'this.a = rtl.arraySetLength(null,0,2);',
  8512. 'this.numbers = [1, 2];',
  8513. 'this.names = ["red", "blue"];',
  8514. '']),
  8515. LinesToStr([ // $mod.$main
  8516. '$mod.e = $mod.TEnum.red;',
  8517. '$mod.e = $mod.TEnum.blue;',
  8518. '$mod.i = $mod.a[$mod.TEnum.red];',
  8519. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  8520. '']));
  8521. end;
  8522. procedure TTestModule.TestArray_SetLengthOutArg;
  8523. begin
  8524. StartProgram(false);
  8525. Add([
  8526. 'type TArrInt = array of longint;',
  8527. 'procedure DoIt(out a: TArrInt);',
  8528. 'begin',
  8529. ' SetLength(a,2);',
  8530. 'end;',
  8531. 'begin',
  8532. '']);
  8533. ConvertProgram;
  8534. CheckSource('TestArray_SetLengthOutArg',
  8535. LinesToStr([ // statements
  8536. 'this.DoIt = function (a) {',
  8537. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  8538. '};',
  8539. '']),
  8540. LinesToStr([
  8541. '']));
  8542. end;
  8543. procedure TTestModule.TestArray_SetLengthProperty;
  8544. begin
  8545. StartProgram(false);
  8546. Add('type');
  8547. Add(' TArrInt = array of longint;');
  8548. Add(' TObject = class');
  8549. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  8550. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  8551. Add(' property Colors: TArrInt read GetColors write SetColors;');
  8552. Add(' end;');
  8553. Add('var Obj: TObject;');
  8554. Add('begin');
  8555. Add(' SetLength(Obj.Colors,2);');
  8556. ConvertProgram;
  8557. CheckSource('TestArray_SetLengthProperty',
  8558. LinesToStr([ // statements
  8559. 'rtl.createClass($mod, "TObject", null, function () {',
  8560. ' this.$init = function () {',
  8561. ' };',
  8562. ' this.$final = function () {',
  8563. ' };',
  8564. '});',
  8565. 'this.Obj = null;',
  8566. '']),
  8567. LinesToStr([
  8568. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  8569. '']));
  8570. end;
  8571. procedure TTestModule.TestArray_SetLengthMultiDim;
  8572. begin
  8573. StartProgram(false);
  8574. Add([
  8575. 'type',
  8576. ' TArrArrInt = array of array of longint;',
  8577. 'var',
  8578. ' a: TArrArrInt;',
  8579. 'begin',
  8580. ' SetLength(a,2);',
  8581. ' SetLength(a,3,4);',
  8582. '']);
  8583. ConvertProgram;
  8584. CheckSource('TestArray_SetLengthMultiDim',
  8585. LinesToStr([ // statements
  8586. 'this.a = [];']),
  8587. LinesToStr([
  8588. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  8589. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  8590. '']));
  8591. end;
  8592. procedure TTestModule.TestArray_OpenArrayOfString;
  8593. begin
  8594. StartProgram(false);
  8595. Add('procedure DoIt(const a: array of String);');
  8596. Add('var');
  8597. Add(' i: longint;');
  8598. Add(' s: string;');
  8599. Add('begin');
  8600. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  8601. Add('end;');
  8602. Add('var s: string;');
  8603. Add('begin');
  8604. Add(' DoIt([]);');
  8605. Add(' DoIt([s,''foo'','''',s+s]);');
  8606. ConvertProgram;
  8607. CheckSource('TestArray_OpenArrayOfString',
  8608. LinesToStr([ // statements
  8609. 'this.DoIt = function (a) {',
  8610. ' var i = 0;',
  8611. ' var s = "";',
  8612. ' for (var $l1 = 0, $end2 = rtl.length(a) - 1; $l1 <= $end2; $l1++) {',
  8613. ' i = $l1;',
  8614. ' s = a[rtl.length(a) - i - 1];',
  8615. ' };',
  8616. '};',
  8617. 'this.s = "";',
  8618. '']),
  8619. LinesToStr([
  8620. '$mod.DoIt([]);',
  8621. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  8622. '']));
  8623. end;
  8624. procedure TTestModule.TestArray_Concat;
  8625. begin
  8626. StartProgram(false);
  8627. Add([
  8628. 'type',
  8629. ' integer = longint;',
  8630. ' TFlag = (big,small);',
  8631. ' TFlags = set of TFlag;',
  8632. ' TRec = record',
  8633. ' i: integer;',
  8634. ' end;',
  8635. ' TArrInt = array of integer;',
  8636. ' TArrRec = array of TRec;',
  8637. ' TArrFlag = array of TFlag;',
  8638. ' TArrSet = array of TFlags;',
  8639. ' TArrJSValue = array of jsvalue;',
  8640. 'var',
  8641. ' ArrInt: tarrint;',
  8642. ' ArrRec: tarrrec;',
  8643. ' ArrFlag: tarrflag;',
  8644. ' ArrSet: tarrset;',
  8645. ' ArrJSValue: tarrjsvalue;',
  8646. 'begin',
  8647. ' arrint:=concat(arrint);',
  8648. ' arrint:=concat(arrint,arrint);',
  8649. ' arrint:=concat(arrint,arrint,arrint);',
  8650. ' arrrec:=concat(arrrec);',
  8651. ' arrrec:=concat(arrrec,arrrec);',
  8652. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  8653. ' arrset:=concat(arrset);',
  8654. ' arrset:=concat(arrset,arrset);',
  8655. ' arrset:=concat(arrset,arrset,arrset);',
  8656. ' arrjsvalue:=concat(arrjsvalue);',
  8657. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  8658. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  8659. ' arrint:=concat([1],arrint);',
  8660. ' arrflag:=concat([big]);',
  8661. ' arrflag:=concat([big],arrflag);',
  8662. ' arrflag:=concat(arrflag,[small]);',
  8663. '']);
  8664. ConvertProgram;
  8665. CheckSource('TestArray_Concat',
  8666. LinesToStr([ // statements
  8667. 'this.TFlag = {',
  8668. ' "0": "big",',
  8669. ' big: 0,',
  8670. ' "1": "small",',
  8671. ' small: 1',
  8672. '};',
  8673. 'rtl.recNewT($mod, "TRec", function () {',
  8674. ' this.i = 0;',
  8675. ' this.$eq = function (b) {',
  8676. ' return this.i === b.i;',
  8677. ' };',
  8678. ' this.$assign = function (s) {',
  8679. ' this.i = s.i;',
  8680. ' return this;',
  8681. ' };',
  8682. '});',
  8683. 'this.ArrInt = [];',
  8684. 'this.ArrRec = [];',
  8685. 'this.ArrFlag = [];',
  8686. 'this.ArrSet = [];',
  8687. 'this.ArrJSValue = [];',
  8688. '']),
  8689. LinesToStr([ // $mod.$main
  8690. '$mod.ArrInt = $mod.ArrInt;',
  8691. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  8692. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  8693. '$mod.ArrRec = $mod.ArrRec;',
  8694. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  8695. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  8696. '$mod.ArrSet = $mod.ArrSet;',
  8697. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  8698. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  8699. '$mod.ArrJSValue = $mod.ArrJSValue;',
  8700. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  8701. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  8702. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  8703. '$mod.ArrFlag = [$mod.TFlag.big];',
  8704. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  8705. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  8706. '']));
  8707. end;
  8708. procedure TTestModule.TestArray_Copy;
  8709. begin
  8710. StartProgram(false);
  8711. Add([
  8712. 'type',
  8713. ' integer = longint;',
  8714. ' TFlag = (big,small);',
  8715. ' TFlags = set of TFlag;',
  8716. ' TRec = record',
  8717. ' i: integer;',
  8718. ' end;',
  8719. ' TArrInt = array of integer;',
  8720. ' TArrRec = array of TRec;',
  8721. ' TArrSet = array of TFlags;',
  8722. ' TArrJSValue = array of jsvalue;',
  8723. 'var',
  8724. ' ArrInt: tarrint;',
  8725. ' ArrRec: tarrrec;',
  8726. ' ArrSet: tarrset;',
  8727. ' ArrJSValue: tarrjsvalue;',
  8728. 'begin',
  8729. ' arrint:=copy(arrint);',
  8730. ' arrint:=copy(arrint,2);',
  8731. ' arrint:=copy(arrint,3,4);',
  8732. ' arrint:=copy([1,1],1,2);',
  8733. ' arrrec:=copy(arrrec);',
  8734. ' arrrec:=copy(arrrec,5);',
  8735. ' arrrec:=copy(arrrec,6,7);',
  8736. ' arrset:=copy(arrset);',
  8737. ' arrset:=copy(arrset,8);',
  8738. ' arrset:=copy(arrset,9,10);',
  8739. ' arrjsvalue:=copy(arrjsvalue);',
  8740. ' arrjsvalue:=copy(arrjsvalue,11);',
  8741. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  8742. ' ']);
  8743. ConvertProgram;
  8744. CheckSource('TestArray_Copy',
  8745. LinesToStr([ // statements
  8746. 'this.TFlag = {',
  8747. ' "0": "big",',
  8748. ' big: 0,',
  8749. ' "1": "small",',
  8750. ' small: 1',
  8751. '};',
  8752. 'rtl.recNewT($mod, "TRec", function () {',
  8753. ' this.i = 0;',
  8754. ' this.$eq = function (b) {',
  8755. ' return this.i === b.i;',
  8756. ' };',
  8757. ' this.$assign = function (s) {',
  8758. ' this.i = s.i;',
  8759. ' return this;',
  8760. ' };',
  8761. '});',
  8762. 'this.ArrInt = [];',
  8763. 'this.ArrRec = [];',
  8764. 'this.ArrSet = [];',
  8765. 'this.ArrJSValue = [];',
  8766. '']),
  8767. LinesToStr([ // $mod.$main
  8768. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  8769. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  8770. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  8771. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  8772. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  8773. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  8774. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  8775. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  8776. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  8777. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  8778. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  8779. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  8780. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  8781. '']));
  8782. end;
  8783. procedure TTestModule.TestArray_InsertDelete;
  8784. begin
  8785. StartProgram(false);
  8786. Add([
  8787. 'type',
  8788. ' integer = longint;',
  8789. ' TFlag = (big,small);',
  8790. ' TFlags = set of TFlag;',
  8791. ' TRec = record',
  8792. ' i: integer;',
  8793. ' end;',
  8794. ' TArrInt = array of integer;',
  8795. ' TArrRec = array of TRec;',
  8796. ' TArrSet = array of TFlags;',
  8797. ' TArrJSValue = array of jsvalue;',
  8798. ' TArrArrInt = array of TArrInt;',
  8799. 'var',
  8800. ' ArrInt: tarrint;',
  8801. ' ArrRec: tarrrec;',
  8802. ' ArrSet: tarrset;',
  8803. ' ArrJSValue: tarrjsvalue;',
  8804. ' ArrArrInt: TArrArrInt;',
  8805. 'begin',
  8806. ' Insert(1,arrint,2);',
  8807. ' Insert(arrint[3],arrint,4);',
  8808. ' Insert(arrrec[5],arrrec,6);',
  8809. ' Insert(arrset[7],arrset,7);',
  8810. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  8811. ' Insert(10,arrjsvalue,11);',
  8812. ' Insert([23],arrarrint,22);',
  8813. ' Delete(arrint,12,13);',
  8814. ' Delete(arrrec,14,15);',
  8815. ' Delete(arrset,17,18);',
  8816. ' Delete(arrjsvalue,19,10);']);
  8817. ConvertProgram;
  8818. CheckSource('TestArray_InsertDelete',
  8819. LinesToStr([ // statements
  8820. 'this.TFlag = {',
  8821. ' "0": "big",',
  8822. ' big: 0,',
  8823. ' "1": "small",',
  8824. ' small: 1',
  8825. '};',
  8826. 'rtl.recNewT($mod, "TRec", function () {',
  8827. ' this.i = 0;',
  8828. ' this.$eq = function (b) {',
  8829. ' return this.i === b.i;',
  8830. ' };',
  8831. ' this.$assign = function (s) {',
  8832. ' this.i = s.i;',
  8833. ' return this;',
  8834. ' };',
  8835. '});',
  8836. 'this.ArrInt = [];',
  8837. 'this.ArrRec = [];',
  8838. 'this.ArrSet = [];',
  8839. 'this.ArrJSValue = [];',
  8840. 'this.ArrArrInt = [];',
  8841. '']),
  8842. LinesToStr([ // $mod.$main
  8843. '$mod.ArrInt.splice(2, 0, 1);',
  8844. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  8845. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  8846. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  8847. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  8848. '$mod.ArrJSValue.splice(11, 0, 10);',
  8849. '$mod.ArrArrInt.splice(22, 0, [23]);',
  8850. '$mod.ArrInt.splice(12, 13);',
  8851. '$mod.ArrRec.splice(14, 15);',
  8852. '$mod.ArrSet.splice(17, 18);',
  8853. '$mod.ArrJSValue.splice(19, 10);',
  8854. '']));
  8855. end;
  8856. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  8857. begin
  8858. StartProgram(false);
  8859. Add([
  8860. '{$modeswitch arrayoperators}',
  8861. 'type',
  8862. ' integer = longint;',
  8863. ' TArrInt = array of integer;',
  8864. ' TArrStr = array of string;',
  8865. 'const',
  8866. ' Ints: TArrInt = (1,2,3);',
  8867. ' Aliases: TarrStr = (''foo'',''b'');',
  8868. ' OneInt: TArrInt = (7);',
  8869. ' OneStr: array of integer = (7);',
  8870. ' Chars: array of char = ''aoc'';',
  8871. ' Names: array of string = (''a'',''foo'');',
  8872. ' NameCount = low(Names)+high(Names)+length(Names);',
  8873. 'var i: integer;',
  8874. 'begin',
  8875. ' Ints:=[];',
  8876. ' Ints:=[1,1];',
  8877. ' Ints:=[1]+[2];',
  8878. ' Ints:=[2];',
  8879. ' Ints:=[]+ints;',
  8880. ' Ints:=Ints+[];',
  8881. ' Ints:=Ints+OneInt;',
  8882. ' Ints:=Ints+[1,1];',
  8883. ' Ints:=[i,i]+Ints;',
  8884. ' Ints:=[1]+[i]+[3];',
  8885. '']);
  8886. ConvertProgram;
  8887. CheckSource('TestArray_DynArrayConstObjFPC',
  8888. LinesToStr([ // statements
  8889. 'this.Ints = [1, 2, 3];',
  8890. 'this.Aliases = ["foo", "b"];',
  8891. 'this.OneInt = [7];',
  8892. 'this.OneStr = [7];',
  8893. 'this.Chars = ["a", "o", "c"];',
  8894. 'this.Names = ["a", "foo"];',
  8895. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  8896. 'this.i = 0;',
  8897. '']),
  8898. LinesToStr([ // $mod.$main
  8899. '$mod.Ints = [];',
  8900. '$mod.Ints = [1, 1];',
  8901. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  8902. '$mod.Ints = [2];',
  8903. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  8904. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  8905. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  8906. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  8907. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  8908. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  8909. '']));
  8910. end;
  8911. procedure TTestModule.TestArray_DynArrayConstDelphi;
  8912. begin
  8913. StartProgram(false);
  8914. // Note: const c = [1,1]; defines a set!
  8915. Add([
  8916. '{$mode delphi}',
  8917. 'type',
  8918. ' integer = longint;',
  8919. ' TArrInt = array of integer;',
  8920. ' TArrStr = array of string;',
  8921. 'const',
  8922. ' Ints: TArrInt = [1,1,2];',
  8923. ' Aliases: TarrStr = [''foo'',''b''];',
  8924. ' OneInt: TArrInt = [7];',
  8925. ' OneStr: array of integer = [7]+[8];',
  8926. ' Chars: array of char = ''aoc'';',
  8927. ' Names: array of string = [''a'',''a''];',
  8928. ' NameCount = low(Names)+high(Names)+length(Names);',
  8929. 'begin',
  8930. '']);
  8931. ConvertProgram;
  8932. CheckSource('TestArray_DynArrayConstDelphi',
  8933. LinesToStr([ // statements
  8934. 'this.Ints = [1, 1, 2];',
  8935. 'this.Aliases = ["foo", "b"];',
  8936. 'this.OneInt = [7];',
  8937. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  8938. 'this.Chars = ["a", "o", "c"];',
  8939. 'this.Names = ["a", "a"];',
  8940. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  8941. '']),
  8942. LinesToStr([ // $mod.$main
  8943. '']));
  8944. end;
  8945. procedure TTestModule.TestArray_ArrayLitAsParam;
  8946. begin
  8947. StartProgram(false);
  8948. Add([
  8949. '{$modeswitch arrayoperators}',
  8950. 'type',
  8951. ' integer = longint;',
  8952. ' TArrInt = array of integer;',
  8953. ' TArrSet = array of (red,green,blue);',
  8954. 'procedure DoOpenInt(a: array of integer); forward;',
  8955. 'procedure DoInt(a: TArrInt);',
  8956. 'begin',
  8957. ' DoInt(a+[1]);',
  8958. ' DoInt([1]+a);',
  8959. ' DoOpenInt(a);',
  8960. ' DoOpenInt(a+[1]);',
  8961. ' DoOpenInt([1]+a);',
  8962. 'end;',
  8963. 'procedure DoOpenInt(a: array of integer);',
  8964. 'begin',
  8965. ' DoOpenInt(a+[1]);',
  8966. ' DoOpenInt([1]+a);',
  8967. ' DoInt(a);',
  8968. ' DoInt(a+[1]);',
  8969. ' DoInt([1]+a);',
  8970. 'end;',
  8971. 'procedure DoSet(a: TArrSet);',
  8972. 'begin',
  8973. ' DoSet(a+[red]);',
  8974. ' DoSet([blue]+a);',
  8975. 'end;',
  8976. 'var',
  8977. ' i: TArrInt;',
  8978. ' s: TArrSet;',
  8979. 'begin',
  8980. ' DoInt([1]);',
  8981. ' DoInt([1]+[2]);',
  8982. ' DoInt(i+[1]);',
  8983. ' DoInt([1]+i);',
  8984. ' DoOpenInt([1]);',
  8985. ' DoOpenInt([1]+[2]);',
  8986. ' DoOpenInt(i+[1]);',
  8987. ' DoOpenInt([1]+i);',
  8988. ' DoSet([red]);',
  8989. ' DoSet([blue]+[green]);',
  8990. ' DoSet(s+[blue]);',
  8991. ' DoSet([red]+s);',
  8992. '']);
  8993. ConvertProgram;
  8994. CheckSource('TestArray_ArrayLitAsParam',
  8995. LinesToStr([ // statements
  8996. 'this.TArrSet$a = {',
  8997. ' "0": "red",',
  8998. ' red: 0,',
  8999. ' "1": "green",',
  9000. ' green: 1,',
  9001. ' "2": "blue",',
  9002. ' blue: 2',
  9003. '};',
  9004. 'this.DoInt = function (a) {',
  9005. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9006. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9007. ' $mod.DoOpenInt(a);',
  9008. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9009. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9010. '};',
  9011. 'this.DoOpenInt = function (a) {',
  9012. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9013. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9014. ' $mod.DoInt(a);',
  9015. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9016. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9017. '};',
  9018. 'this.DoSet = function (a) {',
  9019. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9020. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9021. '};',
  9022. 'this.i = [];',
  9023. 'this.s = [];',
  9024. '']),
  9025. LinesToStr([ // $mod.$main
  9026. '$mod.DoInt([1]);',
  9027. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9028. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9029. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9030. '$mod.DoOpenInt([1]);',
  9031. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9032. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9033. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9034. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9035. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9036. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9037. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9038. '']));
  9039. end;
  9040. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9041. begin
  9042. StartProgram(false);
  9043. Add([
  9044. '{$modeswitch arrayoperators}',
  9045. 'type',
  9046. ' integer = longint;',
  9047. ' TArrInt = array of integer;',
  9048. ' TArrArrInt = array of TArrInt;',
  9049. 'procedure DoInt(a: TArrArrInt);',
  9050. 'begin',
  9051. ' DoInt(a+[[1]]);',
  9052. ' DoInt([[1]]+a);',
  9053. ' DoInt(a);',
  9054. 'end;',
  9055. 'var',
  9056. ' i: TArrInt;',
  9057. ' a: TArrArrInt;',
  9058. 'begin',
  9059. ' a:=[[1]];',
  9060. ' a:=[i];',
  9061. ' a:=a+[i];',
  9062. ' a:=[i]+a;',
  9063. ' a:=[[1]+i];',
  9064. ' a:=[[1]+[2]];',
  9065. ' a:=[i+[2]];',
  9066. ' DoInt([[1]]);',
  9067. ' DoInt([[1]+[2],[3,4],[5]]);',
  9068. ' DoInt([i+[1]]+a);',
  9069. ' DoInt([i]+a);',
  9070. '']);
  9071. ConvertProgram;
  9072. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9073. LinesToStr([ // statements
  9074. 'this.DoInt = function (a) {',
  9075. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9076. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9077. ' $mod.DoInt(a);',
  9078. '};',
  9079. 'this.i = [];',
  9080. 'this.a = [];',
  9081. '']),
  9082. LinesToStr([ // $mod.$main
  9083. '$mod.a = [[1]];',
  9084. '$mod.a = [$mod.i];',
  9085. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9086. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9087. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9088. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9089. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9090. '$mod.DoInt([[1]]);',
  9091. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9092. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  9093. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  9094. '']));
  9095. end;
  9096. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  9097. begin
  9098. StartProgram(false);
  9099. Add([
  9100. '{$modeswitch arrayoperators}',
  9101. 'type',
  9102. ' integer = longint;',
  9103. ' TArrInt = array[1..2] of integer;',
  9104. ' TArrArrInt = array of TArrInt;',
  9105. 'procedure DoInt(a: TArrArrInt);',
  9106. 'begin',
  9107. ' DoInt(a+[[1,2]]);',
  9108. ' DoInt([[1,2]]+a);',
  9109. ' DoInt(a);',
  9110. 'end;',
  9111. 'var',
  9112. ' i: TArrInt;',
  9113. ' a: TArrArrInt;',
  9114. 'begin',
  9115. ' a:=[[1,1]];',
  9116. ' a:=[i];',
  9117. ' a:=a+[i];',
  9118. ' a:=[i]+a;',
  9119. ' DoInt([[1,1]]);',
  9120. ' DoInt([[1,2],[3,4]]);',
  9121. '']);
  9122. ConvertProgram;
  9123. CheckSource('TestArray_ArrayLitStaticAsParam',
  9124. LinesToStr([ // statements
  9125. 'this.DoInt = function (a) {',
  9126. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  9127. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  9128. ' $mod.DoInt(a);',
  9129. '};',
  9130. 'this.i = rtl.arraySetLength(null, 0, 2);',
  9131. 'this.a = [];',
  9132. '']),
  9133. LinesToStr([ // $mod.$main
  9134. '$mod.a = [[1, 1]];',
  9135. '$mod.a = [$mod.i.slice(0)];',
  9136. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  9137. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  9138. '$mod.DoInt([[1, 1]]);',
  9139. '$mod.DoInt([[1, 2], [3, 4]]);',
  9140. '']));
  9141. end;
  9142. procedure TTestModule.TestArray_ForInArrOfString;
  9143. begin
  9144. StartProgram(false);
  9145. Add([
  9146. 'type',
  9147. 'type',
  9148. ' TMonthNameArray = array [1..12] of string;',
  9149. ' TMonthNames = TMonthNameArray;',
  9150. ' TObject = class',
  9151. ' private',
  9152. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  9153. ' public',
  9154. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9155. ' end;',
  9156. 'var f: TObject;',
  9157. ' Month: string;',
  9158. 'begin',
  9159. ' for Month in f.LongMonthNames do ;',
  9160. '']);
  9161. ConvertProgram;
  9162. CheckSource('TestArray_ForInArrOfString',
  9163. LinesToStr([ // statements
  9164. 'rtl.createClass($mod, "TObject", null, function () {',
  9165. ' this.$init = function () {',
  9166. ' };',
  9167. ' this.$final = function () {',
  9168. ' };',
  9169. '});',
  9170. 'this.f = null;',
  9171. 'this.Month = "";',
  9172. '']),
  9173. LinesToStr([ // $mod.$main
  9174. 'for (var $in1 = $mod.f.GetLongMonthNames(), $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.Month = $in1[$l2];',
  9175. '']));
  9176. end;
  9177. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9178. begin
  9179. StartProgram(false);
  9180. Add([
  9181. '{$modeswitch externalclass}',
  9182. 'type',
  9183. ' TJSObject = class external name ''Object''',
  9184. ' end;',
  9185. ' TJSArray = class external name ''Array''',
  9186. ' class function isArray(Value: JSValue) : boolean;',
  9187. ' function concat() : TJSArray; varargs;',
  9188. ' end;',
  9189. 'var',
  9190. ' aObj: TJSArray;',
  9191. ' a: array of longint;',
  9192. ' o: TJSObject;',
  9193. 'begin',
  9194. ' if TJSArray.isArray(65) then ;',
  9195. ' aObj:=TJSArray(a).concat(a);',
  9196. ' o:=TJSObject(a);']);
  9197. ConvertProgram;
  9198. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  9199. LinesToStr([ // statements
  9200. 'this.aObj = null;',
  9201. 'this.a = [];',
  9202. 'this.o = null;',
  9203. '']),
  9204. LinesToStr([ // $mod.$main
  9205. 'if (Array.isArray(65)) ;',
  9206. '$mod.aObj = $mod.a.concat($mod.a);',
  9207. '$mod.o = $mod.a;',
  9208. '']));
  9209. end;
  9210. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  9211. begin
  9212. StartProgram(false);
  9213. Add([
  9214. '{$modeswitch externalclass}',
  9215. 'type',
  9216. ' TArrStr = array of string;',
  9217. ' TJSArray = class external name ''Array''',
  9218. ' end;',
  9219. ' TJSObject = class external name ''Object''',
  9220. ' end;',
  9221. 'var',
  9222. ' aObj: TJSArray;',
  9223. ' a: TArrStr;',
  9224. ' jo: TJSObject;',
  9225. 'begin',
  9226. ' a:=TArrStr(aObj);',
  9227. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  9228. ' a:=TarrStr(jo);',
  9229. '']);
  9230. ConvertProgram;
  9231. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  9232. LinesToStr([ // statements
  9233. 'this.aObj = null;',
  9234. 'this.a = [];',
  9235. 'this.jo = null;',
  9236. '']),
  9237. LinesToStr([ // $mod.$main
  9238. '$mod.a = $mod.aObj;',
  9239. '$mod.aObj[1] = $mod.aObj[2];',
  9240. '$mod.a = $mod.jo;',
  9241. '']));
  9242. end;
  9243. procedure TTestModule.TestArrayOfConst_TVarRec;
  9244. begin
  9245. StartProgram(true,[supTVarRec]);
  9246. Add([
  9247. 'procedure Say(args: array of const);',
  9248. 'var',
  9249. ' i: longint;',
  9250. ' v: TVarRec;',
  9251. 'begin',
  9252. ' for i:=low(args) to high(args) do begin',
  9253. ' v:=args[i];',
  9254. ' case v.vtype of',
  9255. ' vtInteger: if length(args)=args[i].vInteger then ;',
  9256. ' end;',
  9257. ' end;',
  9258. ' for v in args do ;',
  9259. ' args:=nil;',
  9260. ' SetLength(args,2);',
  9261. 'end;',
  9262. 'begin']);
  9263. ConvertProgram;
  9264. CheckSource('TestArrayOfConst_TVarRec',
  9265. LinesToStr([ // statements
  9266. 'this.Say = function (args) {',
  9267. ' var i = 0;',
  9268. ' var v = pas.system.TVarRec.$new();',
  9269. ' for (var $l1 = 0, $end2 = rtl.length(args) - 1; $l1 <= $end2; $l1++) {',
  9270. ' i = $l1;',
  9271. ' v.$assign(args[i]);',
  9272. ' var $tmp3 = v.VType;',
  9273. ' if ($tmp3 === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  9274. ' };',
  9275. ' for (var $in4 = args, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) v = $in4[$l5];',
  9276. ' args = [];',
  9277. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  9278. '};',
  9279. '']),
  9280. LinesToStr([ // $mod.$main
  9281. ]));
  9282. end;
  9283. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  9284. begin
  9285. StartProgram(true,[supTVarRec]);
  9286. Add([
  9287. 'procedure Say(args: array of const);',
  9288. 'begin',
  9289. ' Say(args);',
  9290. 'end;',
  9291. 'var',
  9292. ' p: Pointer;',
  9293. ' j: jsvalue;',
  9294. ' c: currency;',
  9295. 'begin',
  9296. ' Say([]);',
  9297. ' Say([1]);',
  9298. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  9299. '']);
  9300. ConvertProgram;
  9301. CheckSource('TestArrayOfConst_PassBaseTypes',
  9302. LinesToStr([ // statements
  9303. 'this.Say = function (args) {',
  9304. ' $mod.Say(args);',
  9305. '};',
  9306. 'this.p = null;',
  9307. 'this.j = undefined;',
  9308. 'this.c = 0;',
  9309. '']),
  9310. LinesToStr([ // $mod.$main
  9311. '$mod.Say([]);',
  9312. '$mod.Say(pas.system.VarRecs(0, 1));',
  9313. '$mod.Say(pas.system.VarRecs(',
  9314. ' 9,',
  9315. ' "c",',
  9316. ' 18,',
  9317. ' "foo",',
  9318. ' 5,',
  9319. ' null,',
  9320. ' 1,',
  9321. ' true,',
  9322. ' 3,',
  9323. ' 1.3,',
  9324. ' 5,',
  9325. ' $mod.p,',
  9326. ' 20,',
  9327. ' $mod.j,',
  9328. ' 12,',
  9329. ' $mod.c',
  9330. ' ));',
  9331. '']));
  9332. end;
  9333. procedure TTestModule.TestArrayOfConst_PassObj;
  9334. begin
  9335. StartProgram(true,[supTVarRec]);
  9336. Add([
  9337. '{$interfaces corba}',
  9338. 'type',
  9339. ' TObject = class',
  9340. ' end;',
  9341. ' TClass = class of TObject;',
  9342. ' IUnknown = interface',
  9343. ' end;',
  9344. 'procedure Say(args: array of const);',
  9345. 'begin',
  9346. 'end;',
  9347. 'var',
  9348. ' o: TObject;',
  9349. ' c: TClass;',
  9350. ' i: IUnknown;',
  9351. 'begin',
  9352. ' Say([o,c,TObject]);',
  9353. ' Say([nil,i]);',
  9354. '']);
  9355. ConvertProgram;
  9356. CheckSource('TestArrayOfConst_PassObj',
  9357. LinesToStr([ // statements
  9358. 'rtl.createClass($mod, "TObject", null, function () {',
  9359. ' this.$init = function () {',
  9360. ' };',
  9361. ' this.$final = function () {',
  9362. ' };',
  9363. '});',
  9364. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  9365. 'this.Say = function (args) {',
  9366. '};',
  9367. 'this.o = null;',
  9368. 'this.c = null;',
  9369. 'this.i = null;',
  9370. '']),
  9371. LinesToStr([ // $mod.$main
  9372. '$mod.Say(pas.system.VarRecs(',
  9373. ' 7,',
  9374. ' $mod.o,',
  9375. ' 8,',
  9376. ' $mod.c,',
  9377. ' 8,',
  9378. ' $mod.TObject',
  9379. '));',
  9380. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  9381. '']));
  9382. end;
  9383. procedure TTestModule.TestRecord_Empty;
  9384. begin
  9385. StartProgram(false);
  9386. Add([
  9387. 'type',
  9388. ' TRecA = record',
  9389. ' end;',
  9390. 'var a,b: TRecA;',
  9391. 'begin',
  9392. ' if a=b then ;']);
  9393. ConvertProgram;
  9394. CheckSource('TestRecord_Empty',
  9395. LinesToStr([ // statements
  9396. 'rtl.recNewT($mod, "TRecA", function () {',
  9397. ' this.$eq = function (b) {',
  9398. ' return true;',
  9399. ' };',
  9400. ' this.$assign = function (s) {',
  9401. ' return this;',
  9402. ' };',
  9403. '});',
  9404. 'this.a = $mod.TRecA.$new();',
  9405. 'this.b = $mod.TRecA.$new();',
  9406. '']),
  9407. LinesToStr([ // $mod.$main
  9408. 'if ($mod.a.$eq($mod.b)) ;'
  9409. ]));
  9410. end;
  9411. procedure TTestModule.TestRecord_Var;
  9412. begin
  9413. StartProgram(false);
  9414. Add('type');
  9415. Add(' TRecA = record');
  9416. Add(' Bold: longint;');
  9417. Add(' end;');
  9418. Add('var Rec: TRecA;');
  9419. Add('begin');
  9420. Add(' rec.bold:=123');
  9421. ConvertProgram;
  9422. CheckSource('TestRecord_Var',
  9423. LinesToStr([ // statements
  9424. 'rtl.recNewT($mod, "TRecA", function () {',
  9425. ' this.Bold = 0;',
  9426. ' this.$eq = function (b) {',
  9427. ' return this.Bold === b.Bold;',
  9428. ' };',
  9429. ' this.$assign = function (s) {',
  9430. ' this.Bold = s.Bold;',
  9431. ' return this;',
  9432. ' };',
  9433. '});',
  9434. 'this.Rec = $mod.TRecA.$new();',
  9435. '']),
  9436. LinesToStr([ // $mod.$main
  9437. '$mod.Rec.Bold = 123;'
  9438. ]));
  9439. end;
  9440. procedure TTestModule.TestRecord_VarExternal;
  9441. begin
  9442. StartProgram(false);
  9443. Add([
  9444. '{$modeswitch externalclass}',
  9445. 'type',
  9446. ' TRecA = record',
  9447. ' i: byte;',
  9448. ' length_: longint external name ''length'';',
  9449. ' end;',
  9450. 'var Rec: TRecA;',
  9451. 'begin',
  9452. ' rec.length_ := rec.length_',
  9453. '']);
  9454. ConvertProgram;
  9455. CheckSource('TestRecord_VarExternal',
  9456. LinesToStr([ // statements
  9457. 'rtl.recNewT($mod, "TRecA", function () {',
  9458. ' this.i = 0;',
  9459. ' this.$eq = function (b) {',
  9460. ' return (this.i === b.i) && (this.length === b.length);',
  9461. ' };',
  9462. ' this.$assign = function (s) {',
  9463. ' this.i = s.i;',
  9464. ' this.length = s.length;',
  9465. ' return this;',
  9466. ' };',
  9467. '});',
  9468. 'this.Rec = $mod.TRecA.$new();',
  9469. '']),
  9470. LinesToStr([ // $mod.$main
  9471. '$mod.Rec.length = $mod.Rec.length;'
  9472. ]));
  9473. end;
  9474. procedure TTestModule.TestRecord_WithDo;
  9475. begin
  9476. StartProgram(false);
  9477. Add('type');
  9478. Add(' TRec = record');
  9479. Add(' vI: longint;');
  9480. Add(' end;');
  9481. Add('var');
  9482. Add(' Int: longint;');
  9483. Add(' r: TRec;');
  9484. Add('begin');
  9485. Add(' with r do');
  9486. Add(' int:=vi;');
  9487. Add(' with r do begin');
  9488. Add(' int:=vi;');
  9489. Add(' vi:=int;');
  9490. Add(' end;');
  9491. ConvertProgram;
  9492. CheckSource('TestWithRecordDo',
  9493. LinesToStr([ // statements
  9494. 'rtl.recNewT($mod, "TRec", function () {',
  9495. ' this.vI = 0;',
  9496. ' this.$eq = function (b) {',
  9497. ' return this.vI === b.vI;',
  9498. ' };',
  9499. ' this.$assign = function (s) {',
  9500. ' this.vI = s.vI;',
  9501. ' return this;',
  9502. ' };',
  9503. '});',
  9504. 'this.Int = 0;',
  9505. 'this.r = $mod.TRec.$new();',
  9506. '']),
  9507. LinesToStr([ // $mod.$main
  9508. 'var $with1 = $mod.r;',
  9509. '$mod.Int = $with1.vI;',
  9510. 'var $with2 = $mod.r;',
  9511. '$mod.Int = $with2.vI;',
  9512. '$with2.vI = $mod.Int;'
  9513. ]));
  9514. end;
  9515. procedure TTestModule.TestRecord_Assign;
  9516. begin
  9517. StartProgram(false);
  9518. Add('type');
  9519. Add(' TEnum = (red,green);');
  9520. Add(' TEnums = set of TEnum;');
  9521. Add(' TSmallRec = record');
  9522. Add(' N: longint;');
  9523. Add(' end;');
  9524. Add(' TBigRec = record');
  9525. Add(' Int: longint;');
  9526. Add(' D: double;');
  9527. Add(' Arr: array of longint;');
  9528. Add(' Arr2: array[1..2] of longint;');
  9529. Add(' Small: TSmallRec;');
  9530. Add(' Enums: TEnums;');
  9531. Add(' end;');
  9532. Add('var');
  9533. Add(' r, s: TBigRec;');
  9534. Add('begin');
  9535. Add(' r:=s;');
  9536. Add(' r:=default(TBigRec);');
  9537. Add(' r:=default(s);');
  9538. ConvertProgram;
  9539. CheckSource('TestRecord_Assign',
  9540. LinesToStr([ // statements
  9541. 'this.TEnum = {',
  9542. ' "0": "red",',
  9543. ' red: 0,',
  9544. ' "1": "green",',
  9545. ' green: 1',
  9546. '};',
  9547. 'rtl.recNewT($mod, "TSmallRec", function () {',
  9548. ' this.N = 0;',
  9549. ' this.$eq = function (b) {',
  9550. ' return this.N === b.N;',
  9551. ' };',
  9552. ' this.$assign = function (s) {',
  9553. ' this.N = s.N;',
  9554. ' return this;',
  9555. ' };',
  9556. '});',
  9557. 'rtl.recNewT($mod, "TBigRec", function () {',
  9558. ' this.Int = 0;',
  9559. ' this.D = 0.0;',
  9560. ' this.Arr = [];',
  9561. ' this.$new = function () {',
  9562. ' var r = Object.create(this);',
  9563. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  9564. ' r.Small = $mod.TSmallRec.$new();',
  9565. ' r.Enums = {};',
  9566. ' return r;',
  9567. ' };',
  9568. ' this.$eq = function (b) {',
  9569. ' 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);',
  9570. ' };',
  9571. ' this.$assign = function (s) {',
  9572. ' this.Int = s.Int;',
  9573. ' this.D = s.D;',
  9574. ' this.Arr = s.Arr;',
  9575. ' this.Arr2 = s.Arr2.slice(0);',
  9576. ' this.Small.$assign(s.Small);',
  9577. ' this.Enums = rtl.refSet(s.Enums);',
  9578. ' return this;',
  9579. ' };',
  9580. '});',
  9581. 'this.r = $mod.TBigRec.$new();',
  9582. 'this.s = $mod.TBigRec.$new();',
  9583. '']),
  9584. LinesToStr([ // $mod.$main
  9585. '$mod.r.$assign($mod.s);',
  9586. '$mod.r.$assign($mod.TBigRec.$new());',
  9587. '$mod.r.$assign($mod.TBigRec.$new());',
  9588. '']));
  9589. end;
  9590. procedure TTestModule.TestRecord_AsParams;
  9591. begin
  9592. StartProgram(false);
  9593. Add([
  9594. 'type',
  9595. ' integer = longint;',
  9596. ' TRecord = record',
  9597. ' i: integer;',
  9598. ' end;',
  9599. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  9600. 'var vL: TRecord;',
  9601. 'begin',
  9602. ' vd:=vd;',
  9603. ' vd.i:=vd.i;',
  9604. ' vl:=vc;',
  9605. ' vv:=vv;',
  9606. ' vv.i:=vv.i;',
  9607. ' U:=vl;',
  9608. ' U:=vd;',
  9609. ' U:=vc;',
  9610. ' U:=vv;',
  9611. ' doit(vd,vd,vd,vd);',
  9612. ' doit(vc,vc,vl,vl);',
  9613. ' doit(vv,vv,vv,vv);',
  9614. ' doit(vl,vl,vl,vl);',
  9615. 'end;',
  9616. 'var i: TRecord;',
  9617. 'begin',
  9618. ' doit(i,i,i,i);']);
  9619. ConvertProgram;
  9620. CheckSource('TestRecord_AsParams',
  9621. LinesToStr([ // statements
  9622. 'rtl.recNewT($mod, "TRecord", function () {',
  9623. ' this.i = 0;',
  9624. ' this.$eq = function (b) {',
  9625. ' return this.i === b.i;',
  9626. ' };',
  9627. ' this.$assign = function (s) {',
  9628. ' this.i = s.i;',
  9629. ' return this;',
  9630. ' };',
  9631. '});',
  9632. 'this.DoIt = function (vD, vC, vV, U) {',
  9633. ' var vL = $mod.TRecord.$new();',
  9634. ' vD.$assign(vD);',
  9635. ' vD.i = vD.i;',
  9636. ' vL.$assign(vC);',
  9637. ' vV.$assign(vV);',
  9638. ' vV.i = vV.i;',
  9639. ' U.set(vL);',
  9640. ' U.set(vD);',
  9641. ' U.set(vC);',
  9642. ' U.set(vV);',
  9643. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, {',
  9644. ' get: function () {',
  9645. ' return vD;',
  9646. ' },',
  9647. ' set: function (v) {',
  9648. ' vD.$assign(v);',
  9649. ' }',
  9650. ' });',
  9651. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, {',
  9652. ' get: function () {',
  9653. ' return vL;',
  9654. ' },',
  9655. ' set: function (v) {',
  9656. ' vL.$assign(v);',
  9657. ' }',
  9658. ' });',
  9659. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, {',
  9660. ' get: function () {',
  9661. ' return vV;',
  9662. ' },',
  9663. ' set: function (v) {',
  9664. ' vV.$assign(v);',
  9665. ' }',
  9666. ' });',
  9667. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, {',
  9668. ' get: function () {',
  9669. ' return vL;',
  9670. ' },',
  9671. ' set: function (v) {',
  9672. ' vL.$assign(v);',
  9673. ' }',
  9674. ' });',
  9675. '};',
  9676. 'this.i = $mod.TRecord.$new();'
  9677. ]),
  9678. LinesToStr([
  9679. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, {',
  9680. ' p: $mod,',
  9681. ' get: function () {',
  9682. ' return this.p.i;',
  9683. ' },',
  9684. ' set: function (v) {',
  9685. ' this.p.i.$assign(v);',
  9686. ' }',
  9687. '});',
  9688. '']));
  9689. end;
  9690. procedure TTestModule.TestRecordElement_AsParams;
  9691. begin
  9692. StartProgram(false);
  9693. Add('type');
  9694. Add(' integer = longint;');
  9695. Add(' TRecord = record');
  9696. Add(' i: integer;');
  9697. Add(' end;');
  9698. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9699. Add('var vJ: TRecord;');
  9700. Add('begin');
  9701. Add(' doit(vj.i,vj.i,vj.i);');
  9702. Add('end;');
  9703. Add('var r: TRecord;');
  9704. Add('begin');
  9705. Add(' doit(r.i,r.i,r.i);');
  9706. ConvertProgram;
  9707. CheckSource('TestRecordElement_AsParams',
  9708. LinesToStr([ // statements
  9709. 'rtl.recNewT($mod, "TRecord", function () {',
  9710. ' this.i = 0;',
  9711. ' this.$eq = function (b) {',
  9712. ' return this.i === b.i;',
  9713. ' };',
  9714. ' this.$assign = function (s) {',
  9715. ' this.i = s.i;',
  9716. ' return this;',
  9717. ' };',
  9718. '});',
  9719. 'this.DoIt = function (vG,vH,vI) {',
  9720. ' var vJ = $mod.TRecord.$new();',
  9721. ' $mod.DoIt(vJ.i, vJ.i, {',
  9722. ' p: vJ,',
  9723. ' get: function () {',
  9724. ' return this.p.i;',
  9725. ' },',
  9726. ' set: function (v) {',
  9727. ' this.p.i = v;',
  9728. ' }',
  9729. ' });',
  9730. '};',
  9731. 'this.r = $mod.TRecord.$new();'
  9732. ]),
  9733. LinesToStr([
  9734. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  9735. ' p: $mod.r,',
  9736. ' get: function () {',
  9737. ' return this.p.i;',
  9738. ' },',
  9739. ' set: function (v) {',
  9740. ' this.p.i = v;',
  9741. ' }',
  9742. '});'
  9743. ]));
  9744. end;
  9745. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  9746. begin
  9747. StartProgram(false);
  9748. Add('type');
  9749. Add(' integer = longint;');
  9750. Add(' TRecord = record');
  9751. Add(' i: integer;');
  9752. Add(' end;');
  9753. Add('function GetRec(vB: integer = 0): TRecord;');
  9754. Add('begin');
  9755. Add('end;');
  9756. Add('procedure DoIt(vG: integer; const vH: integer);');
  9757. Add('begin');
  9758. Add('end;');
  9759. Add('begin');
  9760. Add(' doit(getrec.i,getrec.i);');
  9761. Add(' doit(getrec().i,getrec().i);');
  9762. Add(' doit(getrec(1).i,getrec(2).i);');
  9763. ConvertProgram;
  9764. CheckSource('TestRecordElementFromFuncResult_AsParams',
  9765. LinesToStr([ // statements
  9766. 'rtl.recNewT($mod, "TRecord", function () {',
  9767. ' this.i = 0;',
  9768. ' this.$eq = function (b) {',
  9769. ' return this.i === b.i;',
  9770. ' };',
  9771. ' this.$assign = function (s) {',
  9772. ' this.i = s.i;',
  9773. ' return this;',
  9774. ' };',
  9775. '});',
  9776. 'this.GetRec = function (vB) {',
  9777. ' var Result = $mod.TRecord.$new();',
  9778. ' return Result;',
  9779. '};',
  9780. 'this.DoIt = function (vG, vH) {',
  9781. '};',
  9782. '']),
  9783. LinesToStr([
  9784. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  9785. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  9786. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  9787. '']));
  9788. end;
  9789. procedure TTestModule.TestRecordElementFromWith_AsParams;
  9790. begin
  9791. StartProgram(false);
  9792. Add('type');
  9793. Add(' integer = longint;');
  9794. Add(' TRecord = record');
  9795. Add(' i: integer;');
  9796. Add(' end;');
  9797. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9798. Add('begin');
  9799. Add('end;');
  9800. Add('var r: trecord;');
  9801. Add('begin');
  9802. Add(' with r do ');
  9803. Add(' doit(i,i,i);');
  9804. ConvertProgram;
  9805. CheckSource('TestRecordElementFromWith_AsParams',
  9806. LinesToStr([ // statements
  9807. 'rtl.recNewT($mod, "TRecord", function () {',
  9808. ' this.i = 0;',
  9809. ' this.$eq = function (b) {',
  9810. ' return this.i === b.i;',
  9811. ' };',
  9812. ' this.$assign = function (s) {',
  9813. ' this.i = s.i;',
  9814. ' return this;',
  9815. ' };',
  9816. '});',
  9817. 'this.DoIt = function (vG,vH,vI) {',
  9818. '};',
  9819. 'this.r = $mod.TRecord.$new();'
  9820. ]),
  9821. LinesToStr([
  9822. 'var $with1 = $mod.r;',
  9823. '$mod.DoIt($with1.i,$with1.i,{',
  9824. ' p: $with1,',
  9825. ' get: function () {',
  9826. ' return this.p.i;',
  9827. ' },',
  9828. ' set: function (v) {',
  9829. ' this.p.i = v;',
  9830. ' }',
  9831. '});',
  9832. '']));
  9833. end;
  9834. procedure TTestModule.TestRecord_Equal;
  9835. begin
  9836. StartProgram(false);
  9837. Add('type');
  9838. Add(' integer = longint;');
  9839. Add(' TFlag = (red,blue);');
  9840. Add(' TFlags = set of TFlag;');
  9841. Add(' TProc = procedure;');
  9842. Add(' TRecord = record');
  9843. Add(' i: integer;');
  9844. Add(' Event: TProc;');
  9845. Add(' f: TFlags;');
  9846. Add(' end;');
  9847. Add(' TNested = record');
  9848. Add(' r: TRecord;');
  9849. Add(' end;');
  9850. Add('var');
  9851. Add(' b: boolean;');
  9852. Add(' r,s: trecord;');
  9853. Add('begin');
  9854. Add(' b:=r=s;');
  9855. Add(' b:=r<>s;');
  9856. ConvertProgram;
  9857. CheckSource('TestRecord_Equal',
  9858. LinesToStr([ // statements
  9859. 'this.TFlag = {',
  9860. ' "0": "red",',
  9861. ' red: 0,',
  9862. ' "1": "blue",',
  9863. ' blue: 1',
  9864. '};',
  9865. 'rtl.recNewT($mod, "TRecord", function () {',
  9866. ' this.i = 0;',
  9867. ' this.Event = null;',
  9868. ' this.$new = function () {',
  9869. ' var r = Object.create(this);',
  9870. ' r.f = {};',
  9871. ' return r;',
  9872. ' };',
  9873. ' this.$eq = function (b) {',
  9874. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  9875. ' };',
  9876. ' this.$assign = function (s) {',
  9877. ' this.i = s.i;',
  9878. ' this.Event = s.Event;',
  9879. ' this.f = rtl.refSet(s.f);',
  9880. ' return this;',
  9881. ' };',
  9882. '});',
  9883. 'rtl.recNewT($mod, "TNested", function () {',
  9884. ' this.$new = function () {',
  9885. ' var r = Object.create(this);',
  9886. ' r.r = $mod.TRecord.$new();',
  9887. ' return r;',
  9888. ' };',
  9889. ' this.$eq = function (b) {',
  9890. ' return this.r.$eq(b.r);',
  9891. ' };',
  9892. ' this.$assign = function (s) {',
  9893. ' this.r.$assign(s.r);',
  9894. ' return this;',
  9895. ' };',
  9896. '});',
  9897. 'this.b = false;',
  9898. 'this.r = $mod.TRecord.$new();',
  9899. 'this.s = $mod.TRecord.$new();',
  9900. '']),
  9901. LinesToStr([
  9902. '$mod.b = $mod.r.$eq($mod.s);',
  9903. '$mod.b = !$mod.r.$eq($mod.s);',
  9904. '']));
  9905. end;
  9906. procedure TTestModule.TestRecord_TypeCastJSValueToRecord;
  9907. begin
  9908. StartProgram(false);
  9909. Add('type');
  9910. Add(' TRecord = record');
  9911. Add(' i: longint;');
  9912. Add(' end;');
  9913. Add('var');
  9914. Add(' Jv: jsvalue;');
  9915. Add(' Rec: trecord;');
  9916. Add('begin');
  9917. Add(' rec:=trecord(jv);');
  9918. ConvertProgram;
  9919. CheckSource('TestRecord_TypeCastJSValueToRecord',
  9920. LinesToStr([ // statements
  9921. 'rtl.recNewT($mod, "TRecord", function () {',
  9922. ' this.i = 0;',
  9923. ' this.$eq = function (b) {',
  9924. ' return this.i === b.i;',
  9925. ' };',
  9926. ' this.$assign = function (s) {',
  9927. ' this.i = s.i;',
  9928. ' return this;',
  9929. ' };',
  9930. '});',
  9931. 'this.Jv = undefined;',
  9932. 'this.Rec = $mod.TRecord.$new();',
  9933. '']),
  9934. LinesToStr([
  9935. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  9936. '']));
  9937. end;
  9938. procedure TTestModule.TestRecord_VariantFail;
  9939. begin
  9940. StartProgram(false);
  9941. Add([
  9942. 'type',
  9943. ' TRec = record',
  9944. ' case word of',
  9945. ' 0: (b0, b1: Byte);',
  9946. ' 1: (i: word);',
  9947. ' end;',
  9948. 'begin']);
  9949. SetExpectedPasResolverError('variant record is not supported',
  9950. nXIsNotSupported);
  9951. ConvertProgram;
  9952. end;
  9953. procedure TTestModule.TestRecord_FieldArray;
  9954. begin
  9955. StartProgram(false);
  9956. Add([
  9957. 'type',
  9958. ' TArrInt = array[3..4] of longint;',
  9959. ' TArrArrInt = array[3..4] of longint;',
  9960. ' TRec = record',
  9961. ' a: array of longint;',
  9962. ' s: array[1..2] of longint;',
  9963. ' m: array[1..2,3..4] of longint;',
  9964. ' o: TArrArrInt;',
  9965. ' end;',
  9966. 'begin']);
  9967. ConvertProgram;
  9968. CheckSource('TestRecord_FieldArray',
  9969. LinesToStr([ // statements
  9970. 'rtl.recNewT($mod, "TRec", function () {',
  9971. ' this.a = [];',
  9972. ' this.$new = function () {',
  9973. ' var r = Object.create(this);',
  9974. ' r.s = rtl.arraySetLength(null, 0, 2);',
  9975. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  9976. ' r.o = rtl.arraySetLength(null, 0, 2);',
  9977. ' return r;',
  9978. ' };',
  9979. ' this.$eq = function (b) {',
  9980. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  9981. ' };',
  9982. ' this.$assign = function (s) {',
  9983. ' this.a = s.a;',
  9984. ' this.s = s.s.slice(0);',
  9985. ' this.m = s.m.slice(0);',
  9986. ' this.o = s.o.slice(0);',
  9987. ' return this;',
  9988. ' };',
  9989. '});',
  9990. '']),
  9991. LinesToStr([ // $mod.$main
  9992. '']));
  9993. end;
  9994. procedure TTestModule.TestRecord_Const;
  9995. begin
  9996. StartProgram(false);
  9997. Add([
  9998. 'type',
  9999. ' TArrInt = array[3..4] of longint;',
  10000. ' TPoint = record x,y: longint; end;',
  10001. ' TRec = record',
  10002. ' i: longint;',
  10003. ' a: array of longint;',
  10004. ' s: array[1..2] of longint;',
  10005. ' m: array[1..2,3..4] of longint;',
  10006. ' p: TPoint;',
  10007. ' end;',
  10008. ' TPoints = array of TPoint;',
  10009. 'const',
  10010. ' r: TRec = (',
  10011. ' i:1;',
  10012. ' a:(2,3);',
  10013. ' s:(4,5);',
  10014. ' m:( (11,12), (13,14) );',
  10015. ' p: (x:21; y:22)',
  10016. ' );',
  10017. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10018. 'begin']);
  10019. ConvertProgram;
  10020. CheckSource('TestRecord_Const',
  10021. LinesToStr([ // statements
  10022. 'rtl.recNewT($mod, "TPoint", function () {',
  10023. ' this.x = 0;',
  10024. ' this.y = 0;',
  10025. ' this.$eq = function (b) {',
  10026. ' return (this.x === b.x) && (this.y === b.y);',
  10027. ' };',
  10028. ' this.$assign = function (s) {',
  10029. ' this.x = s.x;',
  10030. ' this.y = s.y;',
  10031. ' return this;',
  10032. ' };',
  10033. '});',
  10034. 'rtl.recNewT($mod, "TRec", function () {',
  10035. ' this.i = 0;',
  10036. ' this.a = [];',
  10037. ' this.$new = function () {',
  10038. ' var r = Object.create(this);',
  10039. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10040. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10041. ' r.p = $mod.TPoint.$new();',
  10042. ' return r;',
  10043. ' };',
  10044. ' this.$eq = function (b) {',
  10045. ' 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);',
  10046. ' };',
  10047. ' this.$assign = function (s) {',
  10048. ' this.i = s.i;',
  10049. ' this.a = s.a;',
  10050. ' this.s = s.s.slice(0);',
  10051. ' this.m = s.m.slice(0);',
  10052. ' this.p.$assign(s.p);',
  10053. ' return this;',
  10054. ' };',
  10055. '});',
  10056. 'this.r = $mod.TRec.$clone({',
  10057. ' i: 1,',
  10058. ' a: [2, 3],',
  10059. ' s: [4, 5],',
  10060. ' m: [[11, 12], [13, 14]],',
  10061. ' p: $mod.TPoint.$clone({',
  10062. ' x: 21,',
  10063. ' y: 22',
  10064. ' })',
  10065. '});',
  10066. 'this.p = [$mod.TPoint.$clone({',
  10067. ' x: 1,',
  10068. ' y: 2',
  10069. '}), $mod.TPoint.$clone({',
  10070. ' x: 3,',
  10071. ' y: 4',
  10072. '})];',
  10073. '']),
  10074. LinesToStr([ // $mod.$main
  10075. '']));
  10076. end;
  10077. procedure TTestModule.TestRecord_TypecastFail;
  10078. begin
  10079. StartProgram(false);
  10080. Add([
  10081. 'type',
  10082. ' TPoint = record x,y: longint; end;',
  10083. ' TRec = record l: longint end;',
  10084. 'var p: TPoint;',
  10085. 'begin',
  10086. ' if TRec(p).l=2 then ;']);
  10087. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  10088. nIllegalTypeConversionTo);
  10089. ConvertProgram;
  10090. end;
  10091. procedure TTestModule.TestRecord_InFunction;
  10092. begin
  10093. StartProgram(false);
  10094. Add([
  10095. 'var TPoint: longint = 3;',
  10096. 'procedure DoIt;',
  10097. 'type',
  10098. ' TPoint = record x,y: longint; end;',
  10099. ' TPoints = array of TPoint;',
  10100. 'var',
  10101. ' r: TPoint;',
  10102. ' p: TPoints;',
  10103. 'begin',
  10104. ' SetLength(p,2);',
  10105. 'end;',
  10106. 'begin']);
  10107. ConvertProgram;
  10108. CheckSource('TestRecord_InFunction',
  10109. LinesToStr([ // statements
  10110. 'this.TPoint = 3;',
  10111. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  10112. ' this.x = 0;',
  10113. ' this.y = 0;',
  10114. ' this.$eq = function (b) {',
  10115. ' return (this.x === b.x) && (this.y === b.y);',
  10116. ' };',
  10117. ' this.$assign = function (s) {',
  10118. ' this.x = s.x;',
  10119. ' this.y = s.y;',
  10120. ' return this;',
  10121. ' };',
  10122. '});',
  10123. 'this.DoIt = function () {',
  10124. ' var r = TPoint$1.$new();',
  10125. ' var p = [];',
  10126. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  10127. '};',
  10128. '']),
  10129. LinesToStr([ // $mod.$main
  10130. '']));
  10131. end;
  10132. procedure TTestModule.TestAdvRecord_Function;
  10133. begin
  10134. StartProgram(false);
  10135. Parser.Options:=Parser.Options+[po_cassignments];
  10136. Add([
  10137. '{$modeswitch AdvancedRecords}',
  10138. 'type',
  10139. ' TPoint = record',
  10140. ' x,y: word;',
  10141. ' function Add(const apt: TPoint): TPoint;',
  10142. ' end;',
  10143. 'function TPoint.Add(const apt: TPoint): TPoint;',
  10144. 'begin',
  10145. ' Result:=Self;',
  10146. ' Result.x+=apt.x;',
  10147. ' Result.y:=Result.y+apt.y;',
  10148. ' Self:=apt;',
  10149. 'end;',
  10150. 'var p,q: TPoint;',
  10151. 'begin',
  10152. ' p.add(q);',
  10153. ' p:=default(TPoint);',
  10154. ' p:=q;',
  10155. '']);
  10156. ConvertProgram;
  10157. CheckSource('TestAdvRecord_Function',
  10158. LinesToStr([ // statements
  10159. 'rtl.recNewT($mod, "TPoint", function () {',
  10160. ' this.x = 0;',
  10161. ' this.y = 0;',
  10162. ' this.$eq = function (b) {',
  10163. ' return (this.x === b.x) && (this.y === b.y);',
  10164. ' };',
  10165. ' this.$assign = function (s) {',
  10166. ' this.x = s.x;',
  10167. ' this.y = s.y;',
  10168. ' return this;',
  10169. ' };',
  10170. ' this.Add = function (apt) {',
  10171. ' var Result = $mod.TPoint.$new();',
  10172. ' Result.$assign(this);',
  10173. ' Result.x += apt.x;',
  10174. ' Result.y = Result.y + apt.y;',
  10175. ' this.$assign(apt);',
  10176. ' return Result;',
  10177. ' };',
  10178. '});',
  10179. 'this.p = $mod.TPoint.$new();',
  10180. 'this.q = $mod.TPoint.$new();',
  10181. '']),
  10182. LinesToStr([ // $mod.$main
  10183. '$mod.p.Add($mod.q);',
  10184. '$mod.p.$assign($mod.TPoint.$new());',
  10185. '$mod.p.$assign($mod.q);',
  10186. '']));
  10187. end;
  10188. procedure TTestModule.TestAdvRecord_Property;
  10189. begin
  10190. StartProgram(false);
  10191. Add([
  10192. '{$modeswitch AdvancedRecords}',
  10193. 'type',
  10194. ' TPoint = record',
  10195. ' x,y: word;',
  10196. ' strict private',
  10197. ' function GetSize: longword;',
  10198. ' procedure SetSize(Value: longword);',
  10199. ' public',
  10200. ' property Size: longword read GetSize write SetSize;',
  10201. ' property Left: word read x write y;',
  10202. ' end;',
  10203. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  10204. 'function TPoint.GetSize: longword;',
  10205. 'begin',
  10206. ' x:=y;',
  10207. ' Size:=Size;',
  10208. ' Left:=Left;',
  10209. 'end;',
  10210. 'procedure TPoint.SetSize(Value: longword);',
  10211. 'begin',
  10212. 'end;',
  10213. 'var p,q: TPoint;',
  10214. 'begin',
  10215. ' p.Size:=q.Size;',
  10216. ' p.Left:=q.Left;',
  10217. '']);
  10218. ConvertProgram;
  10219. CheckSource('TestAdvRecord_Property',
  10220. LinesToStr([ // statements
  10221. 'rtl.recNewT($mod, "TPoint", function () {',
  10222. ' this.x = 0;',
  10223. ' this.y = 0;',
  10224. ' this.$eq = function (b) {',
  10225. ' return (this.x === b.x) && (this.y === b.y);',
  10226. ' };',
  10227. ' this.$assign = function (s) {',
  10228. ' this.x = s.x;',
  10229. ' this.y = s.y;',
  10230. ' return this;',
  10231. ' };',
  10232. ' this.GetSize = function () {',
  10233. ' var Result = 0;',
  10234. ' this.x = this.y;',
  10235. ' this.SetSize(this.GetSize());',
  10236. ' this.y = this.x;',
  10237. ' return Result;',
  10238. ' };',
  10239. ' this.SetSize = function (Value) {',
  10240. ' };',
  10241. '});',
  10242. 'this.SetSize = function (Value) {',
  10243. '};',
  10244. 'this.p = $mod.TPoint.$new();',
  10245. 'this.q = $mod.TPoint.$new();',
  10246. '']),
  10247. LinesToStr([ // $mod.$main
  10248. '$mod.p.SetSize($mod.q.GetSize());',
  10249. '$mod.p.y = $mod.q.x;',
  10250. '']));
  10251. end;
  10252. procedure TTestModule.TestAdvRecord_PropertyDefault;
  10253. begin
  10254. StartProgram(false);
  10255. Add([
  10256. '{$modeswitch AdvancedRecords}',
  10257. 'type',
  10258. ' TPoint = record',
  10259. ' strict private',
  10260. ' function GetItems(Index: word): word;',
  10261. ' procedure SetItems(Index: word; Value: word);',
  10262. ' public',
  10263. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  10264. ' end;',
  10265. 'function TPoint.GetItems(Index: word): word;',
  10266. 'begin',
  10267. ' Items[index]:=Items[index];',
  10268. ' self.Items[index]:=self.Items[index];',
  10269. 'end;',
  10270. 'procedure TPoint.SetItems(Index: word; Value: word);',
  10271. 'begin',
  10272. 'end;',
  10273. 'var p: TPoint;',
  10274. 'begin',
  10275. ' p[1]:=p[2];',
  10276. ' p.Items[3]:=p.Items[4];',
  10277. '']);
  10278. ConvertProgram;
  10279. CheckSource('TestAdvRecord_PropertyDefault',
  10280. LinesToStr([ // statements
  10281. 'rtl.recNewT($mod, "TPoint", function () {',
  10282. ' this.$eq = function (b) {',
  10283. ' return true;',
  10284. ' };',
  10285. ' this.$assign = function (s) {',
  10286. ' return this;',
  10287. ' };',
  10288. ' this.GetItems = function (Index) {',
  10289. ' var Result = 0;',
  10290. ' this.SetItems(Index, this.GetItems(Index));',
  10291. ' this.SetItems(Index, this.GetItems(Index));',
  10292. ' return Result;',
  10293. ' };',
  10294. ' this.SetItems = function (Index, Value) {',
  10295. ' };',
  10296. '});',
  10297. 'this.p = $mod.TPoint.$new();',
  10298. '']),
  10299. LinesToStr([ // $mod.$main
  10300. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  10301. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  10302. '']));
  10303. end;
  10304. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  10305. begin
  10306. StartProgram(false);
  10307. Add([
  10308. '{$modeswitch AdvancedRecords}',
  10309. 'type',
  10310. ' TRec = record',
  10311. ' class var Fx: longint;',
  10312. ' class var Fy: longint;',
  10313. ' class function GetInt: longint; static;',
  10314. ' class procedure SetInt(Value: longint); static;',
  10315. ' class procedure DoIt; static;',
  10316. ' class property IntA: longint read Fx write Fy;',
  10317. ' class property IntB: longint read GetInt write SetInt;',
  10318. ' end;',
  10319. 'class function trec.getint: longint;',
  10320. 'begin',
  10321. ' result:=fx;',
  10322. 'end;',
  10323. 'class procedure trec.setint(value: longint);',
  10324. 'begin',
  10325. 'end;',
  10326. 'class procedure trec.doit;',
  10327. 'begin',
  10328. ' IntA:=IntA+1;',
  10329. ' IntB:=IntB+1;',
  10330. 'end;',
  10331. 'var r: trec;',
  10332. 'begin',
  10333. ' trec.inta:=trec.inta+1;',
  10334. ' if trec.intb=2 then;',
  10335. ' trec.intb:=trec.intb+2;',
  10336. ' trec.setint(trec.inta);',
  10337. ' r.inta:=r.inta+1;',
  10338. ' if r.intb=2 then;',
  10339. ' r.intb:=r.intb+2;',
  10340. ' r.setint(r.inta);']);
  10341. ConvertProgram;
  10342. CheckSource('TestAdvRecord_Property_ClassMethod',
  10343. LinesToStr([ // statements
  10344. 'rtl.recNewT($mod, "TRec", function () {',
  10345. ' this.Fx = 0;',
  10346. ' this.Fy = 0;',
  10347. ' this.$eq = function (b) {',
  10348. ' return true;',
  10349. ' };',
  10350. ' this.$assign = function (s) {',
  10351. ' return this;',
  10352. ' };',
  10353. ' this.GetInt = function () {',
  10354. ' var Result = 0;',
  10355. ' Result = this.Fx;',
  10356. ' return Result;',
  10357. ' };',
  10358. ' this.SetInt = function (Value) {',
  10359. ' };',
  10360. ' this.DoIt = function () {',
  10361. ' $mod.TRec.Fy = this.Fx + 1;',
  10362. ' this.SetInt(this.GetInt() + 1);',
  10363. ' };',
  10364. '}, true);',
  10365. 'this.r = $mod.TRec.$new();',
  10366. '']),
  10367. LinesToStr([ // $mod.$main
  10368. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  10369. 'if ($mod.TRec.GetInt() === 2) ;',
  10370. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  10371. '$mod.TRec.SetInt($mod.TRec.Fx);',
  10372. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  10373. 'if ($mod.r.GetInt() === 2) ;',
  10374. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  10375. '$mod.r.SetInt($mod.r.Fx);',
  10376. '']));
  10377. end;
  10378. procedure TTestModule.TestAdvRecord_Const;
  10379. begin
  10380. StartProgram(false);
  10381. Add([
  10382. '{$modeswitch AdvancedRecords}',
  10383. 'type',
  10384. ' TArrInt = array[3..4] of longint;',
  10385. ' TPoint = record',
  10386. ' x,y: longint;',
  10387. ' class var Count: nativeint;',
  10388. ' end;',
  10389. ' TRec = record',
  10390. ' i: longint;',
  10391. ' a: array of longint;',
  10392. ' s: array[1..2] of longint;',
  10393. ' m: array[1..2,3..4] of longint;',
  10394. ' p: TPoint;',
  10395. ' end;',
  10396. ' TPoints = array of TPoint;',
  10397. 'const',
  10398. ' r: TRec = (',
  10399. ' i:1;',
  10400. ' a:(2,3);',
  10401. ' s:(4,5);',
  10402. ' m:( (11,12), (13,14) );',
  10403. ' p: (x:21)',
  10404. ' );',
  10405. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10406. 'begin']);
  10407. ConvertProgram;
  10408. CheckSource('TestAdvRecord_Const',
  10409. LinesToStr([ // statements
  10410. 'rtl.recNewT($mod, "TPoint", function () {',
  10411. ' this.x = 0;',
  10412. ' this.y = 0;',
  10413. ' this.Count = 0;',
  10414. ' this.$eq = function (b) {',
  10415. ' return (this.x === b.x) && (this.y === b.y);',
  10416. ' };',
  10417. ' this.$assign = function (s) {',
  10418. ' this.x = s.x;',
  10419. ' this.y = s.y;',
  10420. ' return this;',
  10421. ' };',
  10422. '}, true);',
  10423. 'rtl.recNewT($mod, "TRec", function () {',
  10424. ' this.i = 0;',
  10425. ' this.a = [];',
  10426. ' this.$new = function () {',
  10427. ' var r = Object.create(this);',
  10428. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10429. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10430. ' r.p = $mod.TPoint.$new();',
  10431. ' return r;',
  10432. ' };',
  10433. ' this.$eq = function (b) {',
  10434. ' 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);',
  10435. ' };',
  10436. ' this.$assign = function (s) {',
  10437. ' this.i = s.i;',
  10438. ' this.a = s.a;',
  10439. ' this.s = s.s.slice(0);',
  10440. ' this.m = s.m.slice(0);',
  10441. ' this.p.$assign(s.p);',
  10442. ' return this;',
  10443. ' };',
  10444. '});',
  10445. 'this.r = $mod.TRec.$clone({',
  10446. ' i: 1,',
  10447. ' a: [2, 3],',
  10448. ' s: [4, 5],',
  10449. ' m: [[11, 12], [13, 14]],',
  10450. ' p: $mod.TPoint.$clone({',
  10451. ' x: 21,',
  10452. ' y: 0',
  10453. ' })',
  10454. '});',
  10455. 'this.p = [$mod.TPoint.$clone({',
  10456. ' x: 1,',
  10457. ' y: 2',
  10458. '}), $mod.TPoint.$clone({',
  10459. ' x: 3,',
  10460. ' y: 4',
  10461. '})];',
  10462. '']),
  10463. LinesToStr([ // $mod.$main
  10464. '']));
  10465. end;
  10466. procedure TTestModule.TestAdvRecord_ExternalField;
  10467. begin
  10468. StartProgram(false);
  10469. Add([
  10470. '{$modeswitch AdvancedRecords}',
  10471. '{$modeswitch externalclass}',
  10472. 'type',
  10473. ' TCar = record',
  10474. ' public',
  10475. ' Intern: longint external name ''$Intern'';',
  10476. ' Intern2: longint external name ''$Intern2'';',
  10477. ' Bracket: longint external name ''["A B"]'';',
  10478. ' procedure DoIt;',
  10479. ' end;',
  10480. 'implementation',
  10481. 'procedure tcar.doit;',
  10482. 'begin',
  10483. ' Intern:=Intern+1;',
  10484. ' Intern2:=Intern2+2;',
  10485. ' Bracket:=Bracket+3;',
  10486. 'end;',
  10487. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  10488. 'begin',
  10489. ' Rec.intern:=Rec.intern+1;',
  10490. ' Rec.intern2:=Rec.intern2+2;',
  10491. ' Rec.Bracket:=Rec.Bracket+3;',
  10492. ' with Rec do begin',
  10493. ' intern:=intern+1;',
  10494. ' intern2:=intern2+2;',
  10495. ' Bracket:=Bracket+3;',
  10496. ' end;']);
  10497. ConvertProgram;
  10498. CheckSource('TestAdvRecord_ExternalField',
  10499. LinesToStr([ // statements
  10500. 'rtl.recNewT($mod, "TCar", function () {',
  10501. ' this.$eq = function (b) {',
  10502. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  10503. ' };',
  10504. ' this.$assign = function (s) {',
  10505. ' this.$Intern = s.$Intern;',
  10506. ' this.$Intern2 = s.$Intern2;',
  10507. ' this["A B"] = s["A B"];',
  10508. ' return this;',
  10509. ' };',
  10510. ' this.DoIt = function () {',
  10511. ' this.$Intern = this.$Intern + 1;',
  10512. ' this.$Intern2 = this.$Intern2 + 2;',
  10513. ' this["A B"] = this["A B"] + 3;',
  10514. ' };',
  10515. '});',
  10516. 'this.Rec = $mod.TCar.$clone({',
  10517. ' $Intern: 11,',
  10518. ' $Intern2: 12,',
  10519. ' "A B": 13',
  10520. '});',
  10521. '']),
  10522. LinesToStr([ // $mod.$main
  10523. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  10524. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  10525. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  10526. 'var $with1 = $mod.Rec;',
  10527. '$with1.$Intern = $with1.$Intern + 1;',
  10528. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  10529. '$with1["A B"] = $with1["A B"] + 3;',
  10530. '']));
  10531. end;
  10532. procedure TTestModule.TestAdvRecord_SubRecord;
  10533. begin
  10534. StartProgram(false);
  10535. Add([
  10536. '{$modeswitch AdvancedRecords}',
  10537. 'type',
  10538. ' TRec = record',
  10539. ' type',
  10540. ' TPoint = record',
  10541. ' x,y: longint;',
  10542. ' class var Count: nativeint;',
  10543. ' procedure DoIt;',
  10544. ' class procedure DoThat; static;',
  10545. ' end;',
  10546. ' var',
  10547. ' i: longint;',
  10548. ' p: TPoint;',
  10549. ' procedure DoSome;',
  10550. ' end;',
  10551. 'const',
  10552. ' r: TRec = (',
  10553. ' i:1;',
  10554. ' p: (x:21;y:22)',
  10555. ' );',
  10556. 'procedure TRec.DoSome;',
  10557. 'begin',
  10558. ' p.x:=p.y+1;',
  10559. ' p.Count:=p.Count+2;',
  10560. 'end;',
  10561. 'procedure TRec.TPoint.DoIt;',
  10562. 'begin',
  10563. ' Count:=Count+3;',
  10564. 'end;',
  10565. 'class procedure TRec.TPoint.DoThat;',
  10566. 'begin',
  10567. ' Count:=Count+4;',
  10568. 'end;',
  10569. 'begin']);
  10570. ConvertProgram;
  10571. CheckSource('TestAdvRecord_SubRecord',
  10572. LinesToStr([ // statements
  10573. 'rtl.recNewT($mod, "TRec", function () {',
  10574. ' rtl.recNewT(this, "TPoint", function () {',
  10575. ' this.x = 0;',
  10576. ' this.y = 0;',
  10577. ' this.Count = 0;',
  10578. ' this.$eq = function (b) {',
  10579. ' return (this.x === b.x) && (this.y === b.y);',
  10580. ' };',
  10581. ' this.$assign = function (s) {',
  10582. ' this.x = s.x;',
  10583. ' this.y = s.y;',
  10584. ' return this;',
  10585. ' };',
  10586. ' this.DoIt = function () {',
  10587. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  10588. ' };',
  10589. ' this.DoThat = function () {',
  10590. ' $mod.TRec.TPoint.Count = this.Count + 4;',
  10591. ' };',
  10592. ' }, true);',
  10593. ' this.i = 0;',
  10594. ' this.$new = function () {',
  10595. ' var r = Object.create(this);',
  10596. ' r.p = this.TPoint.$new();',
  10597. ' return r;',
  10598. ' };',
  10599. ' this.$eq = function (b) {',
  10600. ' return (this.i === b.i) && this.p.$eq(b.p);',
  10601. ' };',
  10602. ' this.$assign = function (s) {',
  10603. ' this.i = s.i;',
  10604. ' this.p.$assign(s.p);',
  10605. ' return this;',
  10606. ' };',
  10607. ' this.DoSome = function () {',
  10608. ' this.p.x = this.p.y + 1;',
  10609. ' this.TPoint.Count = this.p.Count + 2;',
  10610. ' };',
  10611. '}, true);',
  10612. 'this.r = $mod.TRec.$clone({',
  10613. ' i: 1,',
  10614. ' p: $mod.TRec.TPoint.$clone({',
  10615. ' x: 21,',
  10616. ' y: 22',
  10617. ' })',
  10618. '});',
  10619. '']),
  10620. LinesToStr([ // $mod.$main
  10621. '']));
  10622. end;
  10623. procedure TTestModule.TestAdvRecord_SubClass;
  10624. begin
  10625. StartProgram(false);
  10626. Add([
  10627. '{$modeswitch AdvancedRecords}',
  10628. 'type',
  10629. ' TObject = class end;',
  10630. ' TPoint = record',
  10631. ' type',
  10632. ' TBird = class',
  10633. ' procedure DoIt;',
  10634. ' class procedure Glob;',
  10635. ' end;',
  10636. ' procedure DoIt(b: TBird);',
  10637. ' end;',
  10638. 'procedure TPoint.TBird.DoIt;',
  10639. 'begin',
  10640. ' doit;',
  10641. ' self.doit;',
  10642. ' glob;',
  10643. ' self.glob;',
  10644. 'end;',
  10645. 'class procedure TPoint.TBird.Glob;',
  10646. 'begin',
  10647. ' glob;',
  10648. ' self.glob;',
  10649. 'end;',
  10650. 'procedure TPoint.DoIt(b: TBird);',
  10651. 'begin',
  10652. ' b.doit;',
  10653. ' b.glob;',
  10654. ' TBird.glob;',
  10655. 'end;',
  10656. 'begin',
  10657. '']);
  10658. ConvertProgram;
  10659. CheckSource('TestAdvRecord_SubClass',
  10660. LinesToStr([ // statements
  10661. 'rtl.createClass($mod, "TObject", null, function () {',
  10662. ' this.$init = function () {',
  10663. ' };',
  10664. ' this.$final = function () {',
  10665. ' };',
  10666. '});',
  10667. 'rtl.recNewT($mod, "TPoint", function () {',
  10668. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  10669. ' this.DoIt = function () {',
  10670. ' this.DoIt();',
  10671. ' this.DoIt();',
  10672. ' this.$class.Glob();',
  10673. ' this.$class.Glob();',
  10674. ' };',
  10675. ' this.Glob = function () {',
  10676. ' this.Glob();',
  10677. ' this.Glob();',
  10678. ' };',
  10679. ' });',
  10680. ' this.$eq = function (b) {',
  10681. ' return true;',
  10682. ' };',
  10683. ' this.$assign = function (s) {',
  10684. ' return this;',
  10685. ' };',
  10686. ' this.DoIt = function (b) {',
  10687. ' b.DoIt();',
  10688. ' b.$class.Glob();',
  10689. ' this.TBird.Glob();',
  10690. ' };',
  10691. '}, true);',
  10692. '']),
  10693. LinesToStr([ // $mod.$main
  10694. '']));
  10695. end;
  10696. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  10697. begin
  10698. StartProgram(false);
  10699. Add([
  10700. '{$modeswitch AdvancedRecords}',
  10701. 'type',
  10702. ' IUnknown = interface end;',
  10703. ' TPoint = record',
  10704. ' type IBird = interface end;',
  10705. ' end;',
  10706. 'begin',
  10707. '']);
  10708. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] interface inside record',
  10709. nNotYetImplemented);
  10710. ParseProgram;
  10711. end;
  10712. procedure TTestModule.TestAdvRecord_Constructor;
  10713. begin
  10714. StartProgram(false);
  10715. Add([
  10716. '{$modeswitch AdvancedRecords}',
  10717. 'type',
  10718. ' TPoint = record',
  10719. ' x,y: longint;',
  10720. ' constructor Create(ax: longint; ay: longint = -1);',
  10721. ' end;',
  10722. 'constructor tpoint.create(ax,ay: longint);',
  10723. 'begin',
  10724. ' x:=ax;',
  10725. ' self.y:=ay;',
  10726. 'end;',
  10727. 'var r: TPoint;',
  10728. 'begin',
  10729. ' r:=TPoint.Create(1,2);',
  10730. ' r.Create(3);',
  10731. ' r:=r.Create(4);',
  10732. '']);
  10733. ConvertProgram;
  10734. CheckSource('TestAdvRecord_Constructor',
  10735. LinesToStr([ // statements
  10736. 'rtl.recNewT($mod, "TPoint", function () {',
  10737. ' this.x = 0;',
  10738. ' this.y = 0;',
  10739. ' this.$eq = function (b) {',
  10740. ' return (this.x === b.x) && (this.y === b.y);',
  10741. ' };',
  10742. ' this.$assign = function (s) {',
  10743. ' this.x = s.x;',
  10744. ' this.y = s.y;',
  10745. ' return this;',
  10746. ' };',
  10747. ' this.Create = function (ax, ay) {',
  10748. ' this.x = ax;',
  10749. ' this.y = ay;',
  10750. ' return this;',
  10751. ' };',
  10752. '}, true);',
  10753. 'this.r = $mod.TPoint.$new();',
  10754. '']),
  10755. LinesToStr([ // $mod.$main
  10756. '$mod.r.$assign($mod.TPoint.$create("Create", [1, 2]));',
  10757. '$mod.r.Create(3, -1);',
  10758. '$mod.r.$assign($mod.r.Create(4, -1));',
  10759. '']));
  10760. end;
  10761. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  10762. begin
  10763. StartProgram(false);
  10764. Add(['type',
  10765. ' TObject = class',
  10766. ' public',
  10767. ' constructor Create;',
  10768. ' destructor Destroy;',
  10769. ' end;',
  10770. ' TBird = TObject;',
  10771. 'constructor tobject.create;',
  10772. 'begin end;',
  10773. 'destructor tobject.destroy;',
  10774. 'begin end;',
  10775. 'var Obj: tobject;',
  10776. 'begin',
  10777. ' obj:=tobject.create;',
  10778. ' obj:=tobject.create();',
  10779. ' obj:=tbird.create;',
  10780. ' obj:=tbird.create();',
  10781. ' obj:=obj.create();',
  10782. ' obj.destroy;',
  10783. '']);
  10784. ConvertProgram;
  10785. CheckSource('TestClass_TObjectDefaultConstructor',
  10786. LinesToStr([ // statements
  10787. 'rtl.createClass($mod,"TObject",null,function(){',
  10788. ' this.$init = function () {',
  10789. ' };',
  10790. ' this.$final = function () {',
  10791. ' };',
  10792. ' this.Create = function(){',
  10793. ' return this;',
  10794. ' };',
  10795. ' this.Destroy = function(){',
  10796. ' };',
  10797. '});',
  10798. 'this.Obj = null;'
  10799. ]),
  10800. LinesToStr([ // $mod.$main
  10801. '$mod.Obj = $mod.TObject.$create("Create");',
  10802. '$mod.Obj = $mod.TObject.$create("Create");',
  10803. '$mod.Obj = $mod.TObject.$create("Create");',
  10804. '$mod.Obj = $mod.TObject.$create("Create");',
  10805. '$mod.Obj = $mod.Obj.Create();',
  10806. '$mod.Obj.$destroy("Destroy");',
  10807. '']));
  10808. end;
  10809. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  10810. begin
  10811. StartProgram(false);
  10812. Add('type');
  10813. Add(' TObject = class');
  10814. Add(' public');
  10815. Add(' constructor Create(Par: longint);');
  10816. Add(' end;');
  10817. Add('constructor tobject.create(par: longint);');
  10818. Add('begin end;');
  10819. Add('var Obj: tobject;');
  10820. Add('begin');
  10821. Add(' obj:=tobject.create(3);');
  10822. ConvertProgram;
  10823. CheckSource('TestClass_TObjectConstructorWithParams',
  10824. LinesToStr([ // statements
  10825. 'rtl.createClass($mod,"TObject",null,function(){',
  10826. ' this.$init = function () {',
  10827. ' };',
  10828. ' this.$final = function () {',
  10829. ' };',
  10830. ' this.Create = function(Par){',
  10831. ' return this;',
  10832. ' };',
  10833. '});',
  10834. 'this.Obj = null;'
  10835. ]),
  10836. LinesToStr([ // $mod.$main
  10837. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  10838. ]));
  10839. end;
  10840. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  10841. begin
  10842. StartProgram(false);
  10843. Add('type');
  10844. Add(' TObject = class');
  10845. Add(' public');
  10846. Add(' constructor Create;');
  10847. Add(' end;');
  10848. Add(' TTest = class(TObject)');
  10849. Add(' public');
  10850. Add(' constructor Create(const Par: longint = 1);');
  10851. Add(' end;');
  10852. Add('constructor tobject.create;');
  10853. Add('begin end;');
  10854. Add('constructor ttest.create(const par: longint);');
  10855. Add('begin end;');
  10856. Add('var t: ttest;');
  10857. Add('begin');
  10858. Add(' t:=ttest.create;');
  10859. Add(' t:=ttest.create(2);');
  10860. ConvertProgram;
  10861. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  10862. LinesToStr([ // statements
  10863. 'rtl.createClass($mod,"TObject",null,function(){',
  10864. ' this.$init = function () {',
  10865. ' };',
  10866. ' this.$final = function () {',
  10867. ' };',
  10868. ' this.Create = function(){',
  10869. ' return this;',
  10870. ' };',
  10871. '});',
  10872. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  10873. ' this.Create$1 = function (Par) {',
  10874. ' return this;',
  10875. ' };',
  10876. '});',
  10877. 'this.t = null;'
  10878. ]),
  10879. LinesToStr([ // $mod.$main
  10880. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  10881. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  10882. ]));
  10883. end;
  10884. procedure TTestModule.TestClass_Var;
  10885. begin
  10886. StartProgram(false);
  10887. Add([
  10888. 'type',
  10889. ' TObject = class',
  10890. ' public',
  10891. ' vI: longint;',
  10892. ' constructor Create(Par: longint);',
  10893. ' end;',
  10894. 'constructor tobject.create(par: longint);',
  10895. 'begin',
  10896. ' vi:=par+3',
  10897. 'end;',
  10898. 'var Obj: tobject;',
  10899. 'begin',
  10900. ' obj:=tobject.create(4);',
  10901. ' obj.vi:=obj.VI+5;']);
  10902. ConvertProgram;
  10903. CheckSource('TestClass_Var',
  10904. LinesToStr([ // statements
  10905. 'rtl.createClass($mod,"TObject",null,function(){',
  10906. ' this.$init = function () {',
  10907. ' this.vI = 0;',
  10908. ' };',
  10909. ' this.$final = function () {',
  10910. ' };',
  10911. ' this.Create = function(Par){',
  10912. ' this.vI = Par+3;',
  10913. ' return this;',
  10914. ' };',
  10915. '});',
  10916. 'this.Obj = null;'
  10917. ]),
  10918. LinesToStr([ // $mod.$main
  10919. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  10920. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  10921. ]));
  10922. end;
  10923. procedure TTestModule.TestClass_Method;
  10924. begin
  10925. StartProgram(false);
  10926. Add('type');
  10927. Add(' TObject = class');
  10928. Add(' public');
  10929. Add(' vI: longint;');
  10930. Add(' Sub: TObject;');
  10931. Add(' constructor Create;');
  10932. Add(' function GetIt(Par: longint): tobject;');
  10933. Add(' end;');
  10934. Add('constructor tobject.create; begin end;');
  10935. Add('function tobject.getit(par: longint): tobject;');
  10936. Add('begin');
  10937. Add(' Self.vi:=par+3;');
  10938. Add(' Result:=self.sub;');
  10939. Add('end;');
  10940. Add('var Obj: tobject;');
  10941. Add('begin');
  10942. Add(' obj:=tobject.create;');
  10943. Add(' obj.getit(4);');
  10944. Add(' obj.sub.sub:=nil;');
  10945. Add(' obj.sub.getit(5);');
  10946. Add(' obj.sub.getit(6).SUB:=nil;');
  10947. Add(' obj.sub.getit(7).GETIT(8);');
  10948. Add(' obj.sub.getit(9).SuB.getit(10);');
  10949. ConvertProgram;
  10950. CheckSource('TestClass_Method',
  10951. LinesToStr([ // statements
  10952. 'rtl.createClass($mod,"TObject",null,function(){',
  10953. ' this.$init = function () {',
  10954. ' this.vI = 0;',
  10955. ' this.Sub = null;',
  10956. ' };',
  10957. ' this.$final = function () {',
  10958. ' this.Sub = undefined;',
  10959. ' };',
  10960. ' this.Create = function(){',
  10961. ' return this;',
  10962. ' };',
  10963. ' this.GetIt = function(Par){',
  10964. ' var Result = null;',
  10965. ' this.vI = Par + 3;',
  10966. ' Result = this.Sub;',
  10967. ' return Result;',
  10968. ' };',
  10969. '});',
  10970. 'this.Obj = null;'
  10971. ]),
  10972. LinesToStr([ // $mod.$main
  10973. '$mod.Obj = $mod.TObject.$create("Create");',
  10974. '$mod.Obj.GetIt(4);',
  10975. '$mod.Obj.Sub.Sub=null;',
  10976. '$mod.Obj.Sub.GetIt(5);',
  10977. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  10978. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  10979. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  10980. ]));
  10981. end;
  10982. procedure TTestModule.TestClass_Implementation;
  10983. begin
  10984. StartUnit(false);
  10985. Add([
  10986. 'interface',
  10987. 'type',
  10988. ' TObject = class',
  10989. ' constructor Create;',
  10990. ' end;',
  10991. 'implementation',
  10992. 'type',
  10993. ' TIntClass = class',
  10994. ' constructor Create; reintroduce;',
  10995. ' class procedure DoGlob;',
  10996. ' end;',
  10997. 'constructor tintclass.create;',
  10998. 'begin',
  10999. ' inherited;',
  11000. ' inherited create;',
  11001. ' doglob;',
  11002. 'end;',
  11003. 'class procedure tintclass.doglob;',
  11004. 'begin',
  11005. 'end;',
  11006. 'constructor tobject.create;',
  11007. 'var',
  11008. ' iC: tintclass;',
  11009. 'begin',
  11010. ' ic:=tintclass.create;',
  11011. ' tintclass.doglob;',
  11012. ' ic.doglob;',
  11013. 'end;',
  11014. 'initialization',
  11015. ' tintclass.doglob;',
  11016. '']);
  11017. ConvertUnit;
  11018. CheckSource('TestClass_Implementation',
  11019. LinesToStr([ // statements
  11020. 'var $impl = $mod.$impl;',
  11021. 'rtl.createClass($mod, "TObject", null, function () {',
  11022. ' this.$init = function () {',
  11023. ' };',
  11024. ' this.$final = function () {',
  11025. ' };',
  11026. ' this.Create = function () {',
  11027. ' var iC = null;',
  11028. ' iC = $impl.TIntClass.$create("Create$1");',
  11029. ' $impl.TIntClass.DoGlob();',
  11030. ' iC.$class.DoGlob();',
  11031. ' return this;',
  11032. ' };',
  11033. '});',
  11034. '']),
  11035. LinesToStr([ // $mod.$main
  11036. '$impl.TIntClass.DoGlob();',
  11037. '']),
  11038. LinesToStr([
  11039. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  11040. ' this.Create$1 = function () {',
  11041. ' $mod.TObject.Create.call(this);',
  11042. ' $mod.TObject.Create.call(this);',
  11043. ' this.$class.DoGlob();',
  11044. ' return this;',
  11045. ' };',
  11046. ' this.DoGlob = function () {',
  11047. ' };',
  11048. '});',
  11049. '']));
  11050. end;
  11051. procedure TTestModule.TestClass_Inheritance;
  11052. begin
  11053. StartProgram(false);
  11054. Add('type');
  11055. Add(' TObject = class');
  11056. Add(' public');
  11057. Add(' constructor Create;');
  11058. Add(' end;');
  11059. Add(' TClassA = class');
  11060. Add(' end;');
  11061. Add(' TClassB = class(TObject)');
  11062. Add(' procedure ProcB;');
  11063. Add(' end;');
  11064. Add('constructor tobject.create; begin end;');
  11065. Add('procedure tclassb.procb; begin end;');
  11066. Add('var');
  11067. Add(' oO: TObject;');
  11068. Add(' oA: TClassA;');
  11069. Add(' oB: TClassB;');
  11070. Add('begin');
  11071. Add(' oO:=tobject.Create;');
  11072. Add(' oA:=tclassa.Create;');
  11073. Add(' ob:=tclassb.Create;');
  11074. Add(' if oo is tclassa then ;');
  11075. Add(' ob:=oo as tclassb;');
  11076. Add(' (oo as tclassb).procb;');
  11077. ConvertProgram;
  11078. CheckSource('TestClass_Inheritance',
  11079. LinesToStr([ // statements
  11080. 'rtl.createClass($mod,"TObject",null,function(){',
  11081. ' this.$init = function () {',
  11082. ' };',
  11083. ' this.$final = function () {',
  11084. ' };',
  11085. ' this.Create = function () {',
  11086. ' return this;',
  11087. ' };',
  11088. '});',
  11089. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  11090. '});',
  11091. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  11092. ' this.ProcB = function () {',
  11093. ' };',
  11094. '});',
  11095. 'this.oO = null;',
  11096. 'this.oA = null;',
  11097. 'this.oB = null;'
  11098. ]),
  11099. LinesToStr([ // $mod.$main
  11100. '$mod.oO = $mod.TObject.$create("Create");',
  11101. '$mod.oA = $mod.TClassA.$create("Create");',
  11102. '$mod.oB = $mod.TClassB.$create("Create");',
  11103. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  11104. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  11105. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  11106. ]));
  11107. end;
  11108. procedure TTestModule.TestClass_TypeAlias;
  11109. begin
  11110. StartProgram(false);
  11111. Add([
  11112. '{$interfaces corba}',
  11113. 'type',
  11114. ' IObject = interface',
  11115. ' end;',
  11116. ' IBird = type IObject;',
  11117. ' TObject = class',
  11118. ' end;',
  11119. ' TBird = type TObject;',
  11120. 'var',
  11121. ' oObj: TObject;',
  11122. ' oBird: TBird;',
  11123. ' IntfObj: IObject;',
  11124. ' IntfBird: IBird;',
  11125. 'begin',
  11126. ' oObj:=oBird;',
  11127. '']);
  11128. ConvertProgram;
  11129. CheckSource('TestClass_TypeAlias',
  11130. LinesToStr([ // statements
  11131. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  11132. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  11133. 'rtl.createClass($mod, "TObject", null, function () {',
  11134. ' this.$init = function () {',
  11135. ' };',
  11136. ' this.$final = function () {',
  11137. ' };',
  11138. '});',
  11139. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11140. '});',
  11141. 'this.oObj = null;',
  11142. 'this.oBird = null;',
  11143. 'this.IntfObj = null;',
  11144. 'this.IntfBird = null;',
  11145. '']),
  11146. LinesToStr([ // $mod.$main
  11147. '$mod.oObj = $mod.oBird;',
  11148. '']));
  11149. end;
  11150. procedure TTestModule.TestClass_AbstractMethod;
  11151. begin
  11152. StartProgram(false);
  11153. Add('type');
  11154. Add(' TObject = class');
  11155. Add(' public');
  11156. Add(' procedure DoIt; virtual; abstract;');
  11157. Add(' end;');
  11158. Add('begin');
  11159. ConvertProgram;
  11160. CheckSource('TestClass_AbstractMethod',
  11161. LinesToStr([ // statements
  11162. 'rtl.createClass($mod,"TObject",null,function(){',
  11163. ' this.$init = function () {',
  11164. ' };',
  11165. ' this.$final = function () {',
  11166. ' };',
  11167. '});'
  11168. ]),
  11169. LinesToStr([ // this.$main
  11170. ''
  11171. ]));
  11172. end;
  11173. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  11174. begin
  11175. StartProgram(false);
  11176. Add([
  11177. 'type',
  11178. ' TObject = class',
  11179. ' procedure DoAbstract; virtual; abstract;',
  11180. ' procedure DoVirtual; virtual;',
  11181. ' procedure DoIt;',
  11182. ' end;',
  11183. ' TA = class',
  11184. ' procedure doabstract; override;',
  11185. ' procedure dovirtual; override;',
  11186. ' procedure DoSome;',
  11187. ' end;',
  11188. 'procedure tobject.dovirtual;',
  11189. 'begin',
  11190. ' inherited; // call non existing ancestor -> ignore silently',
  11191. 'end;',
  11192. 'procedure tobject.doit;',
  11193. 'begin',
  11194. 'end;',
  11195. 'procedure ta.doabstract;',
  11196. 'begin',
  11197. ' inherited dovirtual; // call TObject.DoVirtual',
  11198. 'end;',
  11199. 'procedure ta.dovirtual;',
  11200. 'begin',
  11201. ' inherited; // call TObject.DoVirtual',
  11202. ' inherited dovirtual; // call TObject.DoVirtual',
  11203. ' inherited dovirtual(); // call TObject.DoVirtual',
  11204. ' doit;',
  11205. ' doit();',
  11206. 'end;',
  11207. 'procedure ta.dosome;',
  11208. 'begin',
  11209. ' inherited; // call non existing ancestor method -> silently ignore',
  11210. 'end;',
  11211. 'begin']);
  11212. ConvertProgram;
  11213. CheckSource('TestClass_CallInherited_ProcNoParams',
  11214. LinesToStr([ // statements
  11215. 'rtl.createClass($mod,"TObject",null,function(){',
  11216. ' this.$init = function () {',
  11217. ' };',
  11218. ' this.$final = function () {',
  11219. ' };',
  11220. ' this.DoVirtual = function () {',
  11221. ' };',
  11222. ' this.DoIt = function () {',
  11223. ' };',
  11224. '});',
  11225. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11226. ' this.DoAbstract = function () {',
  11227. ' $mod.TObject.DoVirtual.call(this);',
  11228. ' };',
  11229. ' this.DoVirtual = function () {',
  11230. ' $mod.TObject.DoVirtual.call(this);',
  11231. ' $mod.TObject.DoVirtual.call(this);',
  11232. ' $mod.TObject.DoVirtual.call(this);',
  11233. ' this.DoIt();',
  11234. ' this.DoIt();',
  11235. ' };',
  11236. ' this.DoSome = function () {',
  11237. ' };',
  11238. '});'
  11239. ]),
  11240. LinesToStr([ // this.$main
  11241. ''
  11242. ]));
  11243. end;
  11244. procedure TTestModule.TestClass_CallInherited_WithParams;
  11245. begin
  11246. StartProgram(false);
  11247. Add([
  11248. 'type',
  11249. ' TObject = class',
  11250. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  11251. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  11252. ' procedure DoIt(pA: longint; pB: longint = 0);',
  11253. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  11254. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11255. ' end;',
  11256. ' TClassA = class',
  11257. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  11258. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  11259. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11260. ' end;',
  11261. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  11262. 'begin',
  11263. 'end;',
  11264. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  11265. 'begin',
  11266. 'end;',
  11267. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  11268. 'begin',
  11269. 'end;',
  11270. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  11271. 'begin',
  11272. 'end;',
  11273. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  11274. 'begin',
  11275. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11276. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11277. 'end;',
  11278. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  11279. 'begin',
  11280. ' inherited; // call TObject.DoVirtual(pA,pB)',
  11281. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11282. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11283. ' doit(pa,pb);',
  11284. ' doit(pa);',
  11285. ' doit2(pa);',
  11286. ' doit2;',
  11287. 'end;',
  11288. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  11289. 'begin',
  11290. ' pa:=inherited;',
  11291. 'end;',
  11292. 'begin']);
  11293. ConvertProgram;
  11294. CheckSource('TestClass_CallInherited_WithParams',
  11295. LinesToStr([ // statements
  11296. 'rtl.createClass($mod,"TObject",null,function(){',
  11297. ' this.$init = function () {',
  11298. ' };',
  11299. ' this.$final = function () {',
  11300. ' };',
  11301. ' this.DoVirtual = function (pA,pB) {',
  11302. ' };',
  11303. ' this.DoIt = function (pA,pB) {',
  11304. ' };',
  11305. ' this.DoIt2 = function (pA,pB) {',
  11306. ' };',
  11307. ' this.GetIt = function (pA, pB) {',
  11308. ' var Result = 0;',
  11309. ' return Result;',
  11310. ' };',
  11311. '});',
  11312. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  11313. ' this.DoAbstract = function (pA,pB) {',
  11314. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11315. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11316. ' };',
  11317. ' this.DoVirtual = function (pA,pB) {',
  11318. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  11319. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11320. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11321. ' this.DoIt(pA,pB);',
  11322. ' this.DoIt(pA,0);',
  11323. ' this.DoIt2(pA,2);',
  11324. ' this.DoIt2(1,2);',
  11325. ' };',
  11326. ' this.GetIt$1 = function (pA, pB) {',
  11327. ' var Result = 0;',
  11328. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  11329. ' return Result;',
  11330. ' };',
  11331. '});'
  11332. ]),
  11333. LinesToStr([ // this.$main
  11334. ''
  11335. ]));
  11336. end;
  11337. procedure TTestModule.TestClasS_CallInheritedConstructor;
  11338. begin
  11339. StartProgram(false);
  11340. Add('type');
  11341. Add(' TObject = class');
  11342. Add(' constructor Create; virtual;');
  11343. Add(' constructor CreateWithB(b: boolean);');
  11344. Add(' end;');
  11345. Add(' TA = class');
  11346. Add(' constructor Create; override;');
  11347. Add(' constructor CreateWithC(c: char);');
  11348. Add(' procedure DoIt;');
  11349. Add(' class function DoSome: TObject;');
  11350. Add(' end;');
  11351. Add('constructor tobject.create;');
  11352. Add('begin');
  11353. Add(' inherited; // call non existing ancestor -> ignore silently');
  11354. Add('end;');
  11355. Add('constructor tobject.createwithb(b: boolean);');
  11356. Add('begin');
  11357. Add(' inherited; // call non existing ancestor -> ignore silently');
  11358. Add(' create; // normal call');
  11359. Add('end;');
  11360. Add('constructor ta.create;');
  11361. Add('begin');
  11362. Add(' inherited; // normal call TObject.Create');
  11363. Add(' inherited create; // normal call TObject.Create');
  11364. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  11365. Add('end;');
  11366. Add('constructor ta.createwithc(c: char);');
  11367. Add('begin');
  11368. Add(' inherited create; // call TObject.Create');
  11369. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  11370. Add(' doit;');
  11371. Add(' doit();');
  11372. Add(' dosome;');
  11373. Add('end;');
  11374. Add('procedure ta.doit;');
  11375. Add('begin');
  11376. Add(' create; // normal call');
  11377. Add(' createwithb(false); // normal call');
  11378. Add(' createwithc(''c''); // normal call');
  11379. Add('end;');
  11380. Add('class function ta.dosome: TObject;');
  11381. Add('begin');
  11382. Add(' Result:=create; // constructor');
  11383. Add(' Result:=createwithb(true); // constructor');
  11384. Add(' Result:=createwithc(''c''); // constructor');
  11385. Add('end;');
  11386. Add('begin');
  11387. ConvertProgram;
  11388. CheckSource('TestClass_CallInheritedConstructor',
  11389. LinesToStr([ // statements
  11390. 'rtl.createClass($mod,"TObject",null,function(){',
  11391. ' this.$init = function () {',
  11392. ' };',
  11393. ' this.$final = function () {',
  11394. ' };',
  11395. ' this.Create = function () {',
  11396. ' return this;',
  11397. ' };',
  11398. ' this.CreateWithB = function (b) {',
  11399. ' this.Create();',
  11400. ' return this;',
  11401. ' };',
  11402. '});',
  11403. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11404. ' this.Create = function () {',
  11405. ' $mod.TObject.Create.call(this);',
  11406. ' $mod.TObject.Create.call(this);',
  11407. ' $mod.TObject.CreateWithB.call(this, false);',
  11408. ' return this;',
  11409. ' };',
  11410. ' this.CreateWithC = function (c) {',
  11411. ' $mod.TObject.Create.call(this);',
  11412. ' $mod.TObject.CreateWithB.call(this, true);',
  11413. ' this.DoIt();',
  11414. ' this.DoIt();',
  11415. ' this.$class.DoSome();',
  11416. ' return this;',
  11417. ' };',
  11418. ' this.DoIt = function () {',
  11419. ' this.Create();',
  11420. ' this.CreateWithB(false);',
  11421. ' this.CreateWithC("c");',
  11422. ' };',
  11423. ' this.DoSome = function () {',
  11424. ' var Result = null;',
  11425. ' Result = this.$create("Create");',
  11426. ' Result = this.$create("CreateWithB", [true]);',
  11427. ' Result = this.$create("CreateWithC", ["c"]);',
  11428. ' return Result;',
  11429. ' };',
  11430. '});'
  11431. ]),
  11432. LinesToStr([ // this.$main
  11433. ''
  11434. ]));
  11435. end;
  11436. procedure TTestModule.TestClass_ClassVar_Assign;
  11437. begin
  11438. StartProgram(false);
  11439. Add([
  11440. 'type',
  11441. ' TObject = class',
  11442. ' public',
  11443. ' class var vI: longint;',
  11444. ' class var Sub: TObject;',
  11445. ' constructor Create;',
  11446. ' class function GetIt(Par: longint): tobject;',
  11447. ' end;',
  11448. 'constructor tobject.create;',
  11449. 'begin',
  11450. ' vi:=vi+1;',
  11451. ' Self.vi:=Self.vi+1;',
  11452. ' inc(vi);',
  11453. 'end;',
  11454. 'class function tobject.getit(par: longint): tobject;',
  11455. 'begin',
  11456. ' vi:=vi+par;',
  11457. ' Self.vi:=Self.vi+par;',
  11458. ' inc(vi);',
  11459. ' Result:=self.sub;',
  11460. 'end;',
  11461. 'var Obj: tobject;',
  11462. 'begin',
  11463. ' obj:=tobject.create;',
  11464. ' tobject.vi:=3;',
  11465. ' if tobject.vi=4 then ;',
  11466. ' tobject.sub:=nil;',
  11467. ' obj.sub:=nil;',
  11468. ' obj.sub.sub:=nil;']);
  11469. ConvertProgram;
  11470. CheckSource('TestClass_ClassVar_Assign',
  11471. LinesToStr([ // statements
  11472. 'rtl.createClass($mod,"TObject",null,function(){',
  11473. ' this.vI = 0;',
  11474. ' this.Sub = null;',
  11475. ' this.$init = function () {',
  11476. ' };',
  11477. ' this.$final = function () {',
  11478. ' };',
  11479. ' this.Create = function(){',
  11480. ' $mod.TObject.vI = this.vI+1;',
  11481. ' $mod.TObject.vI = this.vI+1;',
  11482. ' $mod.TObject.vI += 1;',
  11483. ' return this;',
  11484. ' };',
  11485. ' this.GetIt = function(Par){',
  11486. ' var Result = null;',
  11487. ' $mod.TObject.vI = this.vI + Par;',
  11488. ' $mod.TObject.vI = this.vI + Par;',
  11489. ' $mod.TObject.vI += 1;',
  11490. ' Result = this.Sub;',
  11491. ' return Result;',
  11492. ' };',
  11493. '});',
  11494. 'this.Obj = null;'
  11495. ]),
  11496. LinesToStr([ // $mod.$main
  11497. '$mod.Obj = $mod.TObject.$create("Create");',
  11498. '$mod.TObject.vI = 3;',
  11499. 'if ($mod.TObject.vI === 4);',
  11500. '$mod.TObject.Sub=null;',
  11501. '$mod.TObject.Sub=null;',
  11502. '$mod.TObject.Sub=null;',
  11503. '']));
  11504. end;
  11505. procedure TTestModule.TestClass_CallClassMethod;
  11506. begin
  11507. StartProgram(false);
  11508. Add('type');
  11509. Add(' TObject = class');
  11510. Add(' public');
  11511. Add(' class var vI: longint;');
  11512. Add(' class var Sub: TObject;');
  11513. Add(' constructor Create;');
  11514. Add(' function GetMore(Par: longint): longint;');
  11515. Add(' class function GetIt(Par: longint): tobject;');
  11516. Add(' end;');
  11517. Add('constructor tobject.create;');
  11518. Add('begin');
  11519. Add(' sub:=getit(3);');
  11520. Add(' vi:=getmore(4);');
  11521. Add(' sub:=Self.getit(5);');
  11522. Add(' vi:=Self.getmore(6);');
  11523. Add('end;');
  11524. Add('function tobject.getmore(par: longint): longint;');
  11525. Add('begin');
  11526. Add(' sub:=getit(11);');
  11527. Add(' vi:=getmore(12);');
  11528. Add(' sub:=self.getit(13);');
  11529. Add(' vi:=self.getmore(14);');
  11530. Add('end;');
  11531. Add('class function tobject.getit(par: longint): tobject;');
  11532. Add('begin');
  11533. Add(' sub:=getit(21);');
  11534. Add(' vi:=sub.getmore(22);');
  11535. Add(' sub:=self.getit(23);');
  11536. Add(' vi:=self.sub.getmore(24);');
  11537. Add('end;');
  11538. Add('var Obj: tobject;');
  11539. Add('begin');
  11540. Add(' obj:=tobject.create;');
  11541. Add(' tobject.getit(5);');
  11542. Add(' obj.getit(6);');
  11543. Add(' obj.sub.getit(7);');
  11544. Add(' obj.sub.getit(8).SUB:=nil;');
  11545. Add(' obj.sub.getit(9).GETIT(10);');
  11546. Add(' obj.sub.getit(11).SuB.getit(12);');
  11547. ConvertProgram;
  11548. CheckSource('TestClass_CallClassMethod',
  11549. LinesToStr([ // statements
  11550. 'rtl.createClass($mod,"TObject",null,function(){',
  11551. ' this.vI = 0;',
  11552. ' this.Sub = null;',
  11553. ' this.$init = function () {',
  11554. ' };',
  11555. ' this.$final = function () {',
  11556. ' };',
  11557. ' this.Create = function(){',
  11558. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  11559. ' $mod.TObject.vI = this.GetMore(4);',
  11560. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  11561. ' $mod.TObject.vI = this.GetMore(6);',
  11562. ' return this;',
  11563. ' };',
  11564. ' this.GetMore = function(Par){',
  11565. ' var Result = 0;',
  11566. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  11567. ' $mod.TObject.vI = this.GetMore(12);',
  11568. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  11569. ' $mod.TObject.vI = this.GetMore(14);',
  11570. ' return Result;',
  11571. ' };',
  11572. ' this.GetIt = function(Par){',
  11573. ' var Result = null;',
  11574. ' $mod.TObject.Sub = this.GetIt(21);',
  11575. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  11576. ' $mod.TObject.Sub = this.GetIt(23);',
  11577. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  11578. ' return Result;',
  11579. ' };',
  11580. '});',
  11581. 'this.Obj = null;'
  11582. ]),
  11583. LinesToStr([ // $mod.$main
  11584. '$mod.Obj = $mod.TObject.$create("Create");',
  11585. '$mod.TObject.GetIt(5);',
  11586. '$mod.Obj.$class.GetIt(6);',
  11587. '$mod.Obj.Sub.$class.GetIt(7);',
  11588. '$mod.TObject.Sub=null;',
  11589. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  11590. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  11591. '']));
  11592. end;
  11593. procedure TTestModule.TestClass_Property;
  11594. begin
  11595. StartProgram(false);
  11596. Add('type');
  11597. Add(' TObject = class');
  11598. Add(' Fx: longint;');
  11599. Add(' Fy: longint;');
  11600. Add(' function GetInt: longint;');
  11601. Add(' procedure SetInt(Value: longint);');
  11602. Add(' procedure DoIt;');
  11603. Add(' property IntA: longint read Fx write Fy;');
  11604. Add(' property IntB: longint read GetInt write SetInt;');
  11605. Add(' end;');
  11606. Add('function tobject.getint: longint;');
  11607. Add('begin');
  11608. Add(' result:=fx;');
  11609. Add('end;');
  11610. Add('procedure tobject.setint(value: longint);');
  11611. Add('begin');
  11612. Add(' if value=fy then exit;');
  11613. Add(' fy:=value;');
  11614. Add('end;');
  11615. Add('procedure tobject.doit;');
  11616. Add('begin');
  11617. Add(' IntA:=IntA+1;');
  11618. Add(' Self.IntA:=Self.IntA+1;');
  11619. Add(' IntB:=IntB+1;');
  11620. Add(' Self.IntB:=Self.IntB+1;');
  11621. Add('end;');
  11622. Add('var Obj: tobject;');
  11623. Add('begin');
  11624. Add(' obj.inta:=obj.inta+1;');
  11625. Add(' if obj.intb=2 then;');
  11626. Add(' obj.intb:=obj.intb+2;');
  11627. Add(' obj.setint(obj.inta);');
  11628. ConvertProgram;
  11629. CheckSource('TestClass_Property',
  11630. LinesToStr([ // statements
  11631. 'rtl.createClass($mod, "TObject", null, function () {',
  11632. ' this.$init = function () {',
  11633. ' this.Fx = 0;',
  11634. ' this.Fy = 0;',
  11635. ' };',
  11636. ' this.$final = function () {',
  11637. ' };',
  11638. ' this.GetInt = function () {',
  11639. ' var Result = 0;',
  11640. ' Result = this.Fx;',
  11641. ' return Result;',
  11642. ' };',
  11643. ' this.SetInt = function (Value) {',
  11644. ' if (Value === this.Fy) return;',
  11645. ' this.Fy = Value;',
  11646. ' };',
  11647. ' this.DoIt = function () {',
  11648. ' this.Fy = this.Fx + 1;',
  11649. ' this.Fy = this.Fx + 1;',
  11650. ' this.SetInt(this.GetInt() + 1);',
  11651. ' this.SetInt(this.GetInt() + 1);',
  11652. ' };',
  11653. '});',
  11654. 'this.Obj = null;'
  11655. ]),
  11656. LinesToStr([ // $mod.$main
  11657. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  11658. 'if ($mod.Obj.GetInt() === 2);',
  11659. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  11660. '$mod.Obj.SetInt($mod.Obj.Fx);'
  11661. ]));
  11662. end;
  11663. procedure TTestModule.TestClass_Property_ClassMethod;
  11664. begin
  11665. StartProgram(false);
  11666. Add([
  11667. 'type',
  11668. ' TObject = class',
  11669. ' class var Fx: longint;',
  11670. ' class var Fy: longint;',
  11671. ' class function GetInt: longint;',
  11672. ' class procedure SetInt(Value: longint);',
  11673. ' end;',
  11674. ' TBird = class',
  11675. ' class procedure DoIt;',
  11676. ' class property IntA: longint read Fx write Fy;',
  11677. ' class property IntB: longint read GetInt write SetInt;',
  11678. ' end;',
  11679. 'class function tobject.getint: longint;',
  11680. 'begin',
  11681. ' result:=fx;',
  11682. 'end;',
  11683. 'class procedure tobject.setint(value: longint);',
  11684. 'begin',
  11685. 'end;',
  11686. 'class procedure tbird.doit;',
  11687. 'begin',
  11688. ' FX:=3;',
  11689. ' IntA:=IntA+1;',
  11690. ' Self.IntA:=Self.IntA+1;',
  11691. ' IntB:=IntB+1;',
  11692. ' Self.IntB:=Self.IntB+1;',
  11693. ' with Self do begin',
  11694. ' FX:=11;',
  11695. ' IntA:=IntA+12;',
  11696. ' IntB:=IntB+13;',
  11697. ' end;',
  11698. 'end;',
  11699. 'var Obj: tbird;',
  11700. 'begin',
  11701. ' tbird.fx:=tbird.fx+1;',
  11702. ' tbird.inta:=tbird.inta+1;',
  11703. ' if tbird.intb=2 then;',
  11704. ' tbird.intb:=tbird.intb+2;',
  11705. ' tbird.setint(tbird.inta);',
  11706. ' obj.inta:=obj.inta+1;',
  11707. ' if obj.intb=2 then;',
  11708. ' obj.intb:=obj.intb+2;',
  11709. ' obj.setint(obj.inta);',
  11710. ' with Tbird do begin',
  11711. ' FX:=FY+1;',
  11712. ' inta:=inta+2;',
  11713. ' intb:=intb+3;',
  11714. ' end;',
  11715. ' with Obj do begin',
  11716. ' FX:=FY+1;',
  11717. ' inta:=inta+2;',
  11718. ' intb:=intb+3;',
  11719. ' end;',
  11720. '']);
  11721. ConvertProgram;
  11722. CheckSource('TestClass_Property_ClassMethod',
  11723. LinesToStr([ // statements
  11724. 'rtl.createClass($mod, "TObject", null, function () {',
  11725. ' this.Fx = 0;',
  11726. ' this.Fy = 0;',
  11727. ' this.$init = function () {',
  11728. ' };',
  11729. ' this.$final = function () {',
  11730. ' };',
  11731. ' this.GetInt = function () {',
  11732. ' var Result = 0;',
  11733. ' Result = this.Fx;',
  11734. ' return Result;',
  11735. ' };',
  11736. ' this.SetInt = function (Value) {',
  11737. ' };',
  11738. '});',
  11739. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11740. ' this.DoIt = function () {',
  11741. ' $mod.TObject.Fx = 3;',
  11742. ' $mod.TObject.Fy = this.Fx + 1;',
  11743. ' $mod.TObject.Fy = this.Fx + 1;',
  11744. ' this.SetInt(this.GetInt() + 1);',
  11745. ' this.SetInt(this.GetInt() + 1);',
  11746. ' $mod.TObject.Fx = 11;',
  11747. ' $mod.TObject.Fy = this.Fx + 12;',
  11748. ' this.SetInt(this.GetInt() + 13);',
  11749. ' };',
  11750. '});',
  11751. 'this.Obj = null;'
  11752. ]),
  11753. LinesToStr([ // $mod.$main
  11754. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  11755. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  11756. 'if ($mod.TBird.GetInt() === 2);',
  11757. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  11758. '$mod.TBird.SetInt($mod.TBird.Fx);',
  11759. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  11760. 'if ($mod.Obj.$class.GetInt() === 2);',
  11761. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  11762. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  11763. 'var $with1 = $mod.TBird;',
  11764. '$mod.TObject.Fx = $with1.Fy + 1;',
  11765. '$mod.TObject.Fy = $with1.Fx + 2;',
  11766. '$with1.SetInt($with1.GetInt() + 3);',
  11767. 'var $with2 = $mod.Obj;',
  11768. '$mod.TObject.Fx = $with2.Fy + 1;',
  11769. '$mod.TObject.Fy = $with2.Fx + 2;',
  11770. '$with2.$class.SetInt($with2.$class.GetInt() + 3);',
  11771. '']));
  11772. end;
  11773. procedure TTestModule.TestClass_Property_Indexed;
  11774. begin
  11775. StartProgram(false);
  11776. Add('type');
  11777. Add(' TObject = class');
  11778. Add(' FItems: array of longint;');
  11779. Add(' function GetItems(Index: longint): longint;');
  11780. Add(' procedure SetItems(Index: longint; Value: longint);');
  11781. Add(' procedure DoIt;');
  11782. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  11783. Add(' end;');
  11784. Add('function tobject.getitems(index: longint): longint;');
  11785. Add('begin');
  11786. Add(' Result:=fitems[index];');
  11787. Add('end;');
  11788. Add('procedure tobject.setitems(index: longint; value: longint);');
  11789. Add('begin');
  11790. Add(' fitems[index]:=value;');
  11791. Add('end;');
  11792. Add('procedure tobject.doit;');
  11793. Add('begin');
  11794. Add(' items[1]:=2;');
  11795. Add(' items[3]:=items[4];');
  11796. Add(' self.items[5]:=self.items[6];');
  11797. Add(' items[items[7]]:=items[items[8]];');
  11798. Add('end;');
  11799. Add('var Obj: tobject;');
  11800. Add('begin');
  11801. Add(' obj.Items[11]:=obj.Items[12];');
  11802. ConvertProgram;
  11803. CheckSource('TestClass_Property_Indexed',
  11804. LinesToStr([ // statements
  11805. 'rtl.createClass($mod, "TObject", null, function () {',
  11806. ' this.$init = function () {',
  11807. ' this.FItems = [];',
  11808. ' };',
  11809. ' this.$final = function () {',
  11810. ' this.FItems = undefined;',
  11811. ' };',
  11812. ' this.GetItems = function (Index) {',
  11813. ' var Result = 0;',
  11814. ' Result = this.FItems[Index];',
  11815. ' return Result;',
  11816. ' };',
  11817. ' this.SetItems = function (Index, Value) {',
  11818. ' this.FItems[Index] = Value;',
  11819. ' };',
  11820. ' this.DoIt = function () {',
  11821. ' this.SetItems(1, 2);',
  11822. ' this.SetItems(3,this.GetItems(4));',
  11823. ' this.SetItems(5,this.GetItems(6));',
  11824. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  11825. ' };',
  11826. '});',
  11827. 'this.Obj = null;'
  11828. ]),
  11829. LinesToStr([ // $mod.$main
  11830. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  11831. ]));
  11832. end;
  11833. procedure TTestModule.TestClass_Property_IndexSpec;
  11834. begin
  11835. StartProgram(false);
  11836. Add([
  11837. 'type',
  11838. ' TEnum = (red, blue);',
  11839. ' TObject = class',
  11840. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  11841. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  11842. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  11843. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  11844. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  11845. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  11846. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  11847. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  11848. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  11849. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  11850. ' end;',
  11851. 'procedure DoIt(b: boolean); begin end;',
  11852. 'var',
  11853. ' o: TObject;',
  11854. 'begin',
  11855. ' o.B1:=o.B1;',
  11856. ' o.B2:=o.B2;',
  11857. ' o.B3:=o.B3;',
  11858. ' o.I1[''a'']:=o.I1[''b''];',
  11859. ' doit(o.b1);',
  11860. ' doit(o.b2);',
  11861. ' doit(o.i1[''c'']);',
  11862. '']);
  11863. ConvertProgram;
  11864. CheckSource('TestClass_Property_IndexSpec',
  11865. LinesToStr([ // statements
  11866. 'this.TEnum = {',
  11867. ' "0": "red",',
  11868. ' red: 0,',
  11869. ' "1": "blue",',
  11870. ' blue: 1',
  11871. '};',
  11872. 'rtl.createClass($mod, "TObject", null, function () {',
  11873. ' this.$init = function () {',
  11874. ' };',
  11875. ' this.$final = function () {',
  11876. ' };',
  11877. '});',
  11878. 'this.DoIt = function (b) {',
  11879. '};',
  11880. 'this.o = null;',
  11881. '']),
  11882. LinesToStr([ // $mod.$main
  11883. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  11884. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  11885. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  11886. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  11887. '$mod.DoIt($mod.o.GetIntBool(1));',
  11888. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  11889. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  11890. '']));
  11891. end;
  11892. procedure TTestModule.TestClass_PropertyOfTypeArray;
  11893. begin
  11894. StartProgram(false);
  11895. Add('type');
  11896. Add(' TArray = array of longint;');
  11897. Add(' TObject = class');
  11898. Add(' FItems: TArray;');
  11899. Add(' function GetItems: tarray;');
  11900. Add(' procedure SetItems(Value: tarray);');
  11901. Add(' property Items: tarray read getitems write setitems;');
  11902. Add(' end;');
  11903. Add('function tobject.getitems: tarray;');
  11904. Add('begin');
  11905. Add(' Result:=fitems;');
  11906. Add('end;');
  11907. Add('procedure tobject.setitems(value: tarray);');
  11908. Add('begin');
  11909. Add(' fitems:=value;');
  11910. Add(' fitems:=nil;');
  11911. Add(' Items:=nil;');
  11912. Add(' Items:=Items;');
  11913. Add(' Items[1]:=2;');
  11914. Add(' fitems[3]:=Items[4];');
  11915. Add(' Items[5]:=Items[6];');
  11916. Add(' Self.Items[7]:=8;');
  11917. Add(' Self.Items[9]:=Self.Items[10];');
  11918. Add(' Items[Items[11]]:=Items[Items[12]];');
  11919. Add('end;');
  11920. Add('var Obj: tobject;');
  11921. Add('begin');
  11922. Add(' obj.items:=nil;');
  11923. Add(' obj.items:=obj.items;');
  11924. Add(' obj.items[11]:=obj.items[12];');
  11925. ConvertProgram;
  11926. CheckSource('TestClass_PropertyOfTypeArray',
  11927. LinesToStr([ // statements
  11928. 'rtl.createClass($mod, "TObject", null, function () {',
  11929. ' this.$init = function () {',
  11930. ' this.FItems = [];',
  11931. ' };',
  11932. ' this.$final = function () {',
  11933. ' this.FItems = undefined;',
  11934. ' };',
  11935. ' this.GetItems = function () {',
  11936. ' var Result = [];',
  11937. ' Result = this.FItems;',
  11938. ' return Result;',
  11939. ' };',
  11940. ' this.SetItems = function (Value) {',
  11941. ' this.FItems = Value;',
  11942. ' this.FItems = [];',
  11943. ' this.SetItems([]);',
  11944. ' this.SetItems(this.GetItems());',
  11945. ' this.GetItems()[1] = 2;',
  11946. ' this.FItems[3] = this.GetItems()[4];',
  11947. ' this.GetItems()[5] = this.GetItems()[6];',
  11948. ' this.GetItems()[7] = 8;',
  11949. ' this.GetItems()[9] = this.GetItems()[10];',
  11950. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  11951. ' };',
  11952. '});',
  11953. 'this.Obj = null;'
  11954. ]),
  11955. LinesToStr([ // $mod.$main
  11956. '$mod.Obj.SetItems([]);',
  11957. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  11958. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  11959. ]));
  11960. end;
  11961. procedure TTestModule.TestClass_PropertyDefault;
  11962. begin
  11963. StartProgram(false);
  11964. Add([
  11965. 'type',
  11966. ' TArray = array of longint;',
  11967. ' TObject = class',
  11968. ' FItems: TArray;',
  11969. ' function GetItems(Index: longint): longint;',
  11970. ' procedure SetItems(Index, Value: longint);',
  11971. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  11972. ' end;',
  11973. 'function tobject.getitems(index: longint): longint;',
  11974. 'begin',
  11975. 'end;',
  11976. 'procedure tobject.setitems(index, value: longint);',
  11977. 'begin',
  11978. ' Self[1]:=2;',
  11979. ' Self[3]:=Self[index];',
  11980. ' Self[index]:=Self[Self[value]];',
  11981. ' Self[Self[4]]:=value;',
  11982. 'end;',
  11983. 'var Obj: tobject;',
  11984. 'begin',
  11985. ' obj[11]:=12;',
  11986. ' obj[13]:=obj[14];',
  11987. ' obj[obj[15]]:=obj[obj[15]];',
  11988. ' TObject(obj)[16]:=TObject(obj)[17];']);
  11989. ConvertProgram;
  11990. CheckSource('TestClass_PropertyDefault',
  11991. LinesToStr([ // statements
  11992. 'rtl.createClass($mod, "TObject", null, function () {',
  11993. ' this.$init = function () {',
  11994. ' this.FItems = [];',
  11995. ' };',
  11996. ' this.$final = function () {',
  11997. ' this.FItems = undefined;',
  11998. ' };',
  11999. ' this.GetItems = function (Index) {',
  12000. ' var Result = 0;',
  12001. ' return Result;',
  12002. ' };',
  12003. ' this.SetItems = function (Index, Value) {',
  12004. ' this.SetItems(1, 2);',
  12005. ' this.SetItems(3, this.GetItems(Index));',
  12006. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  12007. ' this.SetItems(this.GetItems(4), Value);',
  12008. ' };',
  12009. '});',
  12010. 'this.Obj = null;'
  12011. ]),
  12012. LinesToStr([ // $mod.$main
  12013. '$mod.Obj.SetItems(11, 12);',
  12014. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  12015. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));',
  12016. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  12017. '']));
  12018. end;
  12019. procedure TTestModule.TestClass_PropertyDefault2;
  12020. begin
  12021. StartProgram(false);
  12022. Add([
  12023. 'type',
  12024. ' TObject = class end;',
  12025. ' TAlphaList = class',
  12026. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  12027. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  12028. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  12029. ' end;',
  12030. ' TBetaList = class',
  12031. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  12032. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  12033. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  12034. ' end;',
  12035. ' TBird = class',
  12036. ' procedure DoIt;',
  12037. ' end;',
  12038. 'procedure TBird.DoIt;',
  12039. 'var',
  12040. ' List: TAlphaList;',
  12041. 'begin',
  12042. ' if TBetaList(List[true])[3]=nil then ;',
  12043. ' TBetaList(List[false])[5]:=nil;',
  12044. 'end;',
  12045. 'var',
  12046. ' List: TAlphaList;',
  12047. 'begin',
  12048. ' if TBetaList(List[true])[3]=nil then ;',
  12049. ' TBetaList(List[false])[5]:=nil;',
  12050. '']);
  12051. ConvertProgram;
  12052. CheckSource('TestClass_PropertyDefault2',
  12053. LinesToStr([ // statements
  12054. 'rtl.createClass($mod, "TObject", null, function () {',
  12055. ' this.$init = function () {',
  12056. ' };',
  12057. ' this.$final = function () {',
  12058. ' };',
  12059. '});',
  12060. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  12061. '});',
  12062. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  12063. '});',
  12064. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12065. ' this.DoIt = function () {',
  12066. ' var List = null;',
  12067. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  12068. ' List.GetAlphas(false).SetBetas(5, null);',
  12069. ' };',
  12070. '});',
  12071. 'this.List = null;',
  12072. '']),
  12073. LinesToStr([ // $mod.$main
  12074. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  12075. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  12076. '']));
  12077. end;
  12078. procedure TTestModule.TestClass_PropertyOverride;
  12079. begin
  12080. StartProgram(false);
  12081. Add('type');
  12082. Add(' integer = longint;');
  12083. Add(' TObject = class');
  12084. Add(' FItem: integer;');
  12085. Add(' function GetItem: integer; external name ''GetItem'';');
  12086. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  12087. Add(' property Item: integer read getitem write setitem;');
  12088. Add(' end;');
  12089. Add(' TCar = class');
  12090. Add(' FBag: integer;');
  12091. Add(' function GetBag: integer; external name ''GetBag'';');
  12092. Add(' property Item read getbag;');
  12093. Add(' end;');
  12094. Add('var');
  12095. Add(' Obj: tobject;');
  12096. Add(' Car: tcar;');
  12097. Add('begin');
  12098. Add(' Obj.Item:=Obj.Item;');
  12099. Add(' Car.Item:=Car.Item;');
  12100. ConvertProgram;
  12101. CheckSource('TestClass_PropertyOverride',
  12102. LinesToStr([ // statements
  12103. 'rtl.createClass($mod, "TObject", null, function () {',
  12104. ' this.$init = function () {',
  12105. ' this.FItem = 0;',
  12106. ' };',
  12107. ' this.$final = function () {',
  12108. ' };',
  12109. '});',
  12110. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12111. ' this.$init = function () {',
  12112. ' $mod.TObject.$init.call(this);',
  12113. ' this.FBag = 0;',
  12114. ' };',
  12115. '});',
  12116. 'this.Obj = null;',
  12117. 'this.Car = null;',
  12118. '']),
  12119. LinesToStr([ // $mod.$main
  12120. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  12121. '$mod.Car.SetItem($mod.Car.GetBag());',
  12122. '']));
  12123. end;
  12124. procedure TTestModule.TestClass_PropertyIncVisibility;
  12125. begin
  12126. AddModuleWithIntfImplSrc('unit1.pp',
  12127. LinesToStr([
  12128. 'type',
  12129. ' TNumber = longint;',
  12130. ' TInteger = longint;',
  12131. ' TObject = class',
  12132. ' private',
  12133. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  12134. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  12135. ' protected',
  12136. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  12137. ' end;']),
  12138. LinesToStr([
  12139. '']));
  12140. StartProgram(true);
  12141. Add([
  12142. 'uses unit1;',
  12143. 'type',
  12144. ' TBird = class',
  12145. ' public',
  12146. ' property Items;',
  12147. ' end;',
  12148. 'procedure DoIt(i: TInteger);',
  12149. 'begin',
  12150. 'end;',
  12151. 'var b: TBird;',
  12152. 'begin',
  12153. ' b.Items[1]:=2;',
  12154. ' b.Items[3]:=b.Items[4];',
  12155. ' DoIt(b.Items[5]);',
  12156. '']);
  12157. ConvertProgram;
  12158. CheckSource('TestClass_PropertyIncVisibility',
  12159. LinesToStr([ // statements
  12160. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  12161. '});',
  12162. 'this.DoIt = function (i) {',
  12163. '};',
  12164. 'this.b = null;'
  12165. ]),
  12166. LinesToStr([ // $mod.$main
  12167. '$mod.b.SetItems(1, 2);',
  12168. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  12169. '$mod.DoIt($mod.b.GetItems(5));'
  12170. ]));
  12171. end;
  12172. procedure TTestModule.TestClass_Assigned;
  12173. begin
  12174. StartProgram(false);
  12175. Add('type');
  12176. Add(' TObject = class');
  12177. Add(' end;');
  12178. Add('var');
  12179. Add(' Obj: tobject;');
  12180. Add(' b: boolean;');
  12181. Add('begin');
  12182. Add(' if Assigned(obj) then ;');
  12183. Add(' b:=Assigned(obj) or false;');
  12184. ConvertProgram;
  12185. CheckSource('TestClass_Assigned',
  12186. LinesToStr([ // statements
  12187. 'rtl.createClass($mod, "TObject", null, function () {',
  12188. ' this.$init = function () {',
  12189. ' };',
  12190. ' this.$final = function () {',
  12191. ' };',
  12192. '});',
  12193. 'this.Obj = null;',
  12194. 'this.b = false;'
  12195. ]),
  12196. LinesToStr([ // $mod.$main
  12197. 'if ($mod.Obj != null);',
  12198. '$mod.b = ($mod.Obj != null) || false;'
  12199. ]));
  12200. end;
  12201. procedure TTestModule.TestClass_WithClassDoCreate;
  12202. begin
  12203. StartProgram(false);
  12204. Add('type');
  12205. Add(' TObject = class');
  12206. Add(' aBool: boolean;');
  12207. Add(' Arr: array of boolean;');
  12208. Add(' constructor Create;');
  12209. Add(' end;');
  12210. Add('constructor TObject.Create; begin end;');
  12211. Add('var');
  12212. Add(' Obj: tobject;');
  12213. Add(' b: boolean;');
  12214. Add('begin');
  12215. Add(' with tobject.create do begin');
  12216. Add(' b:=abool;');
  12217. Add(' abool:=b;');
  12218. Add(' b:=arr[1];');
  12219. Add(' arr[2]:=b;');
  12220. Add(' end;');
  12221. Add(' with tobject do');
  12222. Add(' obj:=create;');
  12223. Add(' with obj do begin');
  12224. Add(' create;');
  12225. Add(' b:=abool;');
  12226. Add(' abool:=b;');
  12227. Add(' b:=arr[3];');
  12228. Add(' arr[4]:=b;');
  12229. Add(' end;');
  12230. ConvertProgram;
  12231. CheckSource('TestClass_WithClassDoCreate',
  12232. LinesToStr([ // statements
  12233. 'rtl.createClass($mod, "TObject", null, function () {',
  12234. ' this.$init = function () {',
  12235. ' this.aBool = false;',
  12236. ' this.Arr = [];',
  12237. ' };',
  12238. ' this.$final = function () {',
  12239. ' this.Arr = undefined;',
  12240. ' };',
  12241. ' this.Create = function () {',
  12242. ' return this;',
  12243. ' };',
  12244. '});',
  12245. 'this.Obj = null;',
  12246. 'this.b = false;'
  12247. ]),
  12248. LinesToStr([ // $mod.$main
  12249. 'var $with1 = $mod.TObject.$create("Create");',
  12250. '$mod.b = $with1.aBool;',
  12251. '$with1.aBool = $mod.b;',
  12252. '$mod.b = $with1.Arr[1];',
  12253. '$with1.Arr[2] = $mod.b;',
  12254. 'var $with2 = $mod.TObject;',
  12255. '$mod.Obj = $with2.$create("Create");',
  12256. 'var $with3 = $mod.Obj;',
  12257. '$with3.Create();',
  12258. '$mod.b = $with3.aBool;',
  12259. '$with3.aBool = $mod.b;',
  12260. '$mod.b = $with3.Arr[3];',
  12261. '$with3.Arr[4] = $mod.b;',
  12262. '']));
  12263. end;
  12264. procedure TTestModule.TestClass_WithClassInstDoProperty;
  12265. begin
  12266. StartProgram(false);
  12267. Add('type');
  12268. Add(' TObject = class');
  12269. Add(' FInt: longint;');
  12270. Add(' constructor Create;');
  12271. Add(' function GetSize: longint;');
  12272. Add(' procedure SetSize(Value: longint);');
  12273. Add(' property Int: longint read FInt write FInt;');
  12274. Add(' property Size: longint read GetSize write SetSize;');
  12275. Add(' end;');
  12276. Add('constructor TObject.Create; begin end;');
  12277. Add('function TObject.GetSize: longint; begin; end;');
  12278. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12279. Add('var');
  12280. Add(' Obj: tobject;');
  12281. Add(' i: longint;');
  12282. Add('begin');
  12283. Add(' with TObject.Create do begin');
  12284. Add(' i:=int;');
  12285. Add(' int:=i;');
  12286. Add(' i:=size;');
  12287. Add(' size:=i;');
  12288. Add(' end;');
  12289. Add(' with obj do begin');
  12290. Add(' i:=int;');
  12291. Add(' int:=i;');
  12292. Add(' i:=size;');
  12293. Add(' size:=i;');
  12294. Add(' end;');
  12295. ConvertProgram;
  12296. CheckSource('TestClass_WithClassInstDoProperty',
  12297. LinesToStr([ // statements
  12298. 'rtl.createClass($mod, "TObject", null, function () {',
  12299. ' this.$init = function () {',
  12300. ' this.FInt = 0;',
  12301. ' };',
  12302. ' this.$final = function () {',
  12303. ' };',
  12304. ' this.Create = function () {',
  12305. ' return this;',
  12306. ' };',
  12307. ' this.GetSize = function () {',
  12308. ' var Result = 0;',
  12309. ' return Result;',
  12310. ' };',
  12311. ' this.SetSize = function (Value) {',
  12312. ' };',
  12313. '});',
  12314. 'this.Obj = null;',
  12315. 'this.i = 0;'
  12316. ]),
  12317. LinesToStr([ // $mod.$main
  12318. 'var $with1 = $mod.TObject.$create("Create");',
  12319. '$mod.i = $with1.FInt;',
  12320. '$with1.FInt = $mod.i;',
  12321. '$mod.i = $with1.GetSize();',
  12322. '$with1.SetSize($mod.i);',
  12323. 'var $with2 = $mod.Obj;',
  12324. '$mod.i = $with2.FInt;',
  12325. '$with2.FInt = $mod.i;',
  12326. '$mod.i = $with2.GetSize();',
  12327. '$with2.SetSize($mod.i);',
  12328. '']));
  12329. end;
  12330. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  12331. begin
  12332. StartProgram(false);
  12333. Add('type');
  12334. Add(' TObject = class');
  12335. Add(' constructor Create;');
  12336. Add(' function GetItems(Index: longint): longint;');
  12337. Add(' procedure SetItems(Index, Value: longint);');
  12338. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  12339. Add(' end;');
  12340. Add('constructor TObject.Create; begin end;');
  12341. Add('function tobject.getitems(index: longint): longint; begin; end;');
  12342. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  12343. Add('var');
  12344. Add(' Obj: tobject;');
  12345. Add(' i: longint;');
  12346. Add('begin');
  12347. Add(' with TObject.Create do begin');
  12348. Add(' i:=Items[1];');
  12349. Add(' Items[2]:=i;');
  12350. Add(' end;');
  12351. Add(' with obj do begin');
  12352. Add(' i:=Items[3];');
  12353. Add(' Items[4]:=i;');
  12354. Add(' end;');
  12355. ConvertProgram;
  12356. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  12357. LinesToStr([ // statements
  12358. 'rtl.createClass($mod, "TObject", null, function () {',
  12359. ' this.$init = function () {',
  12360. ' };',
  12361. ' this.$final = function () {',
  12362. ' };',
  12363. ' this.Create = function () {',
  12364. ' return this;',
  12365. ' };',
  12366. ' this.GetItems = function (Index) {',
  12367. ' var Result = 0;',
  12368. ' return Result;',
  12369. ' };',
  12370. ' this.SetItems = function (Index, Value) {',
  12371. ' };',
  12372. '});',
  12373. 'this.Obj = null;',
  12374. 'this.i = 0;'
  12375. ]),
  12376. LinesToStr([ // $mod.$main
  12377. 'var $with1 = $mod.TObject.$create("Create");',
  12378. '$mod.i = $with1.GetItems(1);',
  12379. '$with1.SetItems(2, $mod.i);',
  12380. 'var $with2 = $mod.Obj;',
  12381. '$mod.i = $with2.GetItems(3);',
  12382. '$with2.SetItems(4, $mod.i);',
  12383. '']));
  12384. end;
  12385. procedure TTestModule.TestClass_WithClassInstDoFunc;
  12386. begin
  12387. StartProgram(false);
  12388. Add('type');
  12389. Add(' TObject = class');
  12390. Add(' constructor Create;');
  12391. Add(' function GetSize: longint;');
  12392. Add(' procedure SetSize(Value: longint);');
  12393. Add(' end;');
  12394. Add('constructor TObject.Create; begin end;');
  12395. Add('function TObject.GetSize: longint; begin; end;');
  12396. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12397. Add('var');
  12398. Add(' Obj: tobject;');
  12399. Add(' i: longint;');
  12400. Add('begin');
  12401. Add(' with TObject.Create do begin');
  12402. Add(' i:=GetSize;');
  12403. Add(' i:=GetSize();');
  12404. Add(' SetSize(i);');
  12405. Add(' end;');
  12406. Add(' with obj do begin');
  12407. Add(' i:=GetSize;');
  12408. Add(' i:=GetSize();');
  12409. Add(' SetSize(i);');
  12410. Add(' end;');
  12411. ConvertProgram;
  12412. CheckSource('TestClass_WithClassInstDoFunc',
  12413. LinesToStr([ // statements
  12414. 'rtl.createClass($mod, "TObject", null, function () {',
  12415. ' this.$init = function () {',
  12416. ' };',
  12417. ' this.$final = function () {',
  12418. ' };',
  12419. ' this.Create = function () {',
  12420. ' return this;',
  12421. ' };',
  12422. ' this.GetSize = function () {',
  12423. ' var Result = 0;',
  12424. ' return Result;',
  12425. ' };',
  12426. ' this.SetSize = function (Value) {',
  12427. ' };',
  12428. '});',
  12429. 'this.Obj = null;',
  12430. 'this.i = 0;'
  12431. ]),
  12432. LinesToStr([ // $mod.$main
  12433. 'var $with1 = $mod.TObject.$create("Create");',
  12434. '$mod.i = $with1.GetSize();',
  12435. '$mod.i = $with1.GetSize();',
  12436. '$with1.SetSize($mod.i);',
  12437. 'var $with2 = $mod.Obj;',
  12438. '$mod.i = $with2.GetSize();',
  12439. '$mod.i = $with2.GetSize();',
  12440. '$with2.SetSize($mod.i);',
  12441. '']));
  12442. end;
  12443. procedure TTestModule.TestClass_TypeCast;
  12444. begin
  12445. StartProgram(false);
  12446. Add('type');
  12447. Add(' TObject = class');
  12448. Add(' Next: TObject;');
  12449. Add(' constructor Create;');
  12450. Add(' end;');
  12451. Add(' TControl = class(TObject)');
  12452. Add(' Arr: array of TObject;');
  12453. Add(' function GetIt(vI: longint = 0): TObject;');
  12454. Add(' end;');
  12455. Add('constructor tobject.create; begin end;');
  12456. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  12457. Add('var');
  12458. Add(' Obj: tobject;');
  12459. Add('begin');
  12460. Add(' obj:=tcontrol(obj).next;');
  12461. Add(' tcontrol(obj):=nil;');
  12462. Add(' obj:=tcontrol(obj);');
  12463. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  12464. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  12465. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  12466. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  12467. ConvertProgram;
  12468. CheckSource('TestClass_TypeCast',
  12469. LinesToStr([ // statements
  12470. 'rtl.createClass($mod, "TObject", null, function () {',
  12471. ' this.$init = function () {',
  12472. ' this.Next = null;',
  12473. ' };',
  12474. ' this.$final = function () {',
  12475. ' this.Next = undefined;',
  12476. ' };',
  12477. ' this.Create = function () {',
  12478. ' return this;',
  12479. ' };',
  12480. '});',
  12481. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  12482. ' this.$init = function () {',
  12483. ' $mod.TObject.$init.call(this);',
  12484. ' this.Arr = [];',
  12485. ' };',
  12486. ' this.$final = function () {',
  12487. ' this.Arr = undefined;',
  12488. ' $mod.TObject.$final.call(this);',
  12489. ' };',
  12490. ' this.GetIt = function (vI) {',
  12491. ' var Result = null;',
  12492. ' return Result;',
  12493. ' };',
  12494. '});',
  12495. 'this.Obj = null;'
  12496. ]),
  12497. LinesToStr([ // $mod.$main
  12498. '$mod.Obj = $mod.Obj.Next;',
  12499. '$mod.Obj = null;',
  12500. '$mod.Obj = $mod.Obj;',
  12501. '$mod.Obj = $mod.Obj.GetIt(0);',
  12502. '$mod.Obj = $mod.Obj.GetIt(0);',
  12503. '$mod.Obj = $mod.Obj.GetIt(1);',
  12504. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  12505. '']));
  12506. end;
  12507. procedure TTestModule.TestClass_TypeCastUntypedParam;
  12508. begin
  12509. StartProgram(false);
  12510. Add('type');
  12511. Add(' TObject = class end;');
  12512. Add('procedure ProcA(var A);');
  12513. Add('begin');
  12514. Add(' TObject(A):=nil;');
  12515. Add(' TObject(A):=TObject(A);');
  12516. Add(' if TObject(A)=nil then ;');
  12517. Add(' if nil=TObject(A) then ;');
  12518. Add('end;');
  12519. Add('procedure ProcB(out A);');
  12520. Add('begin');
  12521. Add(' TObject(A):=nil;');
  12522. Add(' TObject(A):=TObject(A);');
  12523. Add(' if TObject(A)=nil then ;');
  12524. Add(' if nil=TObject(A) then ;');
  12525. Add('end;');
  12526. Add('procedure ProcC(const A);');
  12527. Add('begin');
  12528. Add(' if TObject(A)=nil then ;');
  12529. Add(' if nil=TObject(A) then ;');
  12530. Add('end;');
  12531. Add('var o: TObject;');
  12532. Add('begin');
  12533. Add(' ProcA(o);');
  12534. Add(' ProcB(o);');
  12535. Add(' ProcC(o);');
  12536. ConvertProgram;
  12537. CheckSource('TestClass_TypeCastUntypedParam',
  12538. LinesToStr([ // statements
  12539. 'rtl.createClass($mod, "TObject", null, function () {',
  12540. ' this.$init = function () {',
  12541. ' };',
  12542. ' this.$final = function () {',
  12543. ' };',
  12544. '});',
  12545. 'this.ProcA = function (A) {',
  12546. ' A.set(null);',
  12547. ' A.set(A.get());',
  12548. ' if (A.get() === null);',
  12549. ' if (null === A.get());',
  12550. '};',
  12551. 'this.ProcB = function (A) {',
  12552. ' A.set(null);',
  12553. ' A.set(A.get());',
  12554. ' if (A.get() === null);',
  12555. ' if (null === A.get());',
  12556. '};',
  12557. 'this.ProcC = function (A) {',
  12558. ' if (A === null);',
  12559. ' if (null === A);',
  12560. '};',
  12561. 'this.o = null;',
  12562. '']),
  12563. LinesToStr([ // $mod.$main
  12564. '$mod.ProcA({',
  12565. ' p: $mod,',
  12566. ' get: function () {',
  12567. ' return this.p.o;',
  12568. ' },',
  12569. ' set: function (v) {',
  12570. ' this.p.o = v;',
  12571. ' }',
  12572. '});',
  12573. '$mod.ProcB({',
  12574. ' p: $mod,',
  12575. ' get: function () {',
  12576. ' return this.p.o;',
  12577. ' },',
  12578. ' set: function (v) {',
  12579. ' this.p.o = v;',
  12580. ' }',
  12581. '});',
  12582. '$mod.ProcC($mod.o);',
  12583. '']));
  12584. end;
  12585. procedure TTestModule.TestClass_Overloads;
  12586. begin
  12587. StartProgram(false);
  12588. Add('type');
  12589. Add(' TObject = class');
  12590. Add(' procedure DoIt;');
  12591. Add(' procedure DoIt(vI: longint);');
  12592. Add(' end;');
  12593. Add('procedure TObject.DoIt;');
  12594. Add('begin');
  12595. Add(' DoIt;');
  12596. Add(' DoIt(1);');
  12597. Add('end;');
  12598. Add('procedure TObject.DoIt(vI: longint); begin end;');
  12599. Add('begin');
  12600. ConvertProgram;
  12601. CheckSource('TestClass_Overloads',
  12602. LinesToStr([ // statements
  12603. 'rtl.createClass($mod, "TObject", null, function () {',
  12604. ' this.$init = function () {',
  12605. ' };',
  12606. ' this.$final = function () {',
  12607. ' };',
  12608. ' this.DoIt = function () {',
  12609. ' this.DoIt();',
  12610. ' this.DoIt$1(1);',
  12611. ' };',
  12612. ' this.DoIt$1 = function (vI) {',
  12613. ' };',
  12614. '});',
  12615. '']),
  12616. LinesToStr([ // $mod.$main
  12617. '']));
  12618. end;
  12619. procedure TTestModule.TestClass_OverloadsAncestor;
  12620. begin
  12621. StartProgram(false);
  12622. Add('type');
  12623. Add(' TObject = class;');
  12624. Add(' TObject = class');
  12625. Add(' procedure DoIt(vA: longint);');
  12626. Add(' procedure DoIt(vA, vB: longint);');
  12627. Add(' end;');
  12628. Add(' TCar = class;');
  12629. Add(' TCar = class');
  12630. Add(' procedure DoIt(vA: longint);');
  12631. Add(' procedure DoIt(vA, vB: longint);');
  12632. Add(' end;');
  12633. Add('procedure tobject.doit(va: longint);');
  12634. Add('begin');
  12635. Add(' doit(1);');
  12636. Add(' doit(1,2);');
  12637. Add('end;');
  12638. Add('procedure tobject.doit(va, vb: longint); begin end;');
  12639. Add('procedure tcar.doit(va: longint);');
  12640. Add('begin');
  12641. Add(' doit(1);');
  12642. Add(' doit(1,2);');
  12643. Add(' inherited doit(1);');
  12644. Add(' inherited doit(1,2);');
  12645. Add('end;');
  12646. Add('procedure tcar.doit(va, vb: longint); begin end;');
  12647. Add('begin');
  12648. ConvertProgram;
  12649. CheckSource('TestClass_OverloadsAncestor',
  12650. LinesToStr([ // statements
  12651. 'rtl.createClass($mod, "TObject", null, function () {',
  12652. ' this.$init = function () {',
  12653. ' };',
  12654. ' this.$final = function () {',
  12655. ' };',
  12656. ' this.DoIt = function (vA) {',
  12657. ' this.DoIt(1);',
  12658. ' this.DoIt$1(1,2);',
  12659. ' };',
  12660. ' this.DoIt$1 = function (vA, vB) {',
  12661. ' };',
  12662. '});',
  12663. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12664. ' this.DoIt$2 = function (vA) {',
  12665. ' this.DoIt$2(1);',
  12666. ' this.DoIt$3(1, 2);',
  12667. ' $mod.TObject.DoIt.call(this, 1);',
  12668. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  12669. ' };',
  12670. ' this.DoIt$3 = function (vA, vB) {',
  12671. ' };',
  12672. '});',
  12673. '']),
  12674. LinesToStr([ // $mod.$main
  12675. '']));
  12676. end;
  12677. procedure TTestModule.TestClass_OverloadConstructor;
  12678. begin
  12679. StartProgram(false);
  12680. Add('type');
  12681. Add(' TObject = class');
  12682. Add(' constructor Create(vA: longint);');
  12683. Add(' constructor Create(vA, vB: longint);');
  12684. Add(' end;');
  12685. Add(' TCar = class');
  12686. Add(' constructor Create(vA: longint);');
  12687. Add(' constructor Create(vA, vB: longint);');
  12688. Add(' end;');
  12689. Add('constructor tobject.create(va: longint);');
  12690. Add('begin');
  12691. Add(' create(1);');
  12692. Add(' create(1,2);');
  12693. Add('end;');
  12694. Add('constructor tobject.create(va, vb: longint); begin end;');
  12695. Add('constructor tcar.create(va: longint);');
  12696. Add('begin');
  12697. Add(' create(1);');
  12698. Add(' create(1,2);');
  12699. Add(' inherited create(1);');
  12700. Add(' inherited create(1,2);');
  12701. Add('end;');
  12702. Add('constructor tcar.create(va, vb: longint); begin end;');
  12703. Add('begin');
  12704. Add(' tobject.create(1);');
  12705. Add(' tobject.create(1,2);');
  12706. Add(' tcar.create(1);');
  12707. Add(' tcar.create(1,2);');
  12708. ConvertProgram;
  12709. CheckSource('TestClass_OverloadConstructor',
  12710. LinesToStr([ // statements
  12711. 'rtl.createClass($mod, "TObject", null, function () {',
  12712. ' this.$init = function () {',
  12713. ' };',
  12714. ' this.$final = function () {',
  12715. ' };',
  12716. ' this.Create = function (vA) {',
  12717. ' this.Create(1);',
  12718. ' this.Create$1(1,2);',
  12719. ' return this;',
  12720. ' };',
  12721. ' this.Create$1 = function (vA, vB) {',
  12722. ' return this;',
  12723. ' };',
  12724. '});',
  12725. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12726. ' this.Create$2 = function (vA) {',
  12727. ' this.Create$2(1);',
  12728. ' this.Create$3(1, 2);',
  12729. ' $mod.TObject.Create.call(this, 1);',
  12730. ' $mod.TObject.Create$1.call(this, 1, 2);',
  12731. ' return this;',
  12732. ' };',
  12733. ' this.Create$3 = function (vA, vB) {',
  12734. ' return this;',
  12735. ' };',
  12736. '});',
  12737. '']),
  12738. LinesToStr([ // $mod.$main
  12739. '$mod.TObject.$create("Create", [1]);',
  12740. '$mod.TObject.$create("Create$1", [1, 2]);',
  12741. '$mod.TCar.$create("Create$2", [1]);',
  12742. '$mod.TCar.$create("Create$3", [1, 2]);',
  12743. '']));
  12744. end;
  12745. procedure TTestModule.TestClass_OverloadDelphiOverride;
  12746. begin
  12747. StartProgram(false);
  12748. Add([
  12749. '{$mode delphi}',
  12750. 'type',
  12751. ' TObject = class end;',
  12752. ' TBird = class',
  12753. ' function {#a}GetValue: longint; overload; virtual;',
  12754. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  12755. ' end;',
  12756. ' TEagle = class(TBird)',
  12757. ' function {#c}GetValue: longint; overload; override;',
  12758. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  12759. ' end;',
  12760. 'function TBird.GetValue: longint;',
  12761. 'begin',
  12762. ' if 3={@a}GetValue then ;',
  12763. ' if 4={@b}GetValue(5) then ;',
  12764. 'end;',
  12765. 'function TBird.GetValue(AValue: longint): longint;',
  12766. 'begin',
  12767. 'end;',
  12768. 'function TEagle.GetValue: longint;',
  12769. 'begin',
  12770. ' if 13={@c}GetValue then ;',
  12771. ' if 14={@d}GetValue(15) then ;',
  12772. ' if 15=inherited {@a}GetValue then ;',
  12773. ' if 16=inherited {@b}GetValue(17) then ;',
  12774. 'end;',
  12775. 'function TEagle.GetValue(AValue: longint): longint;',
  12776. 'begin',
  12777. 'end;',
  12778. 'var',
  12779. ' e: TEagle;',
  12780. 'begin',
  12781. ' if 23=e.{@c}GetValue then ;',
  12782. ' if 24=e.{@d}GetValue(25) then ;']);
  12783. ConvertProgram;
  12784. CheckSource('TestClass_OverloadDelphiOverride',
  12785. LinesToStr([ // statements
  12786. 'rtl.createClass($mod, "TObject", null, function () {',
  12787. ' this.$init = function () {',
  12788. ' };',
  12789. ' this.$final = function () {',
  12790. ' };',
  12791. '});',
  12792. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12793. ' this.GetValue = function () {',
  12794. ' var Result = 0;',
  12795. ' if (3 === this.GetValue()) ;',
  12796. ' if (4 === this.GetValue$1(5)) ;',
  12797. ' return Result;',
  12798. ' };',
  12799. ' this.GetValue$1 = function (AValue) {',
  12800. ' var Result = 0;',
  12801. ' return Result;',
  12802. ' };',
  12803. '});',
  12804. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  12805. ' this.GetValue = function () {',
  12806. ' var Result = 0;',
  12807. ' if (13 === this.GetValue()) ;',
  12808. ' if (14 === this.GetValue$1(15)) ;',
  12809. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  12810. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  12811. ' return Result;',
  12812. ' };',
  12813. ' this.GetValue$1 = function (AValue) {',
  12814. ' var Result = 0;',
  12815. ' return Result;',
  12816. ' };',
  12817. '});',
  12818. 'this.e = null;',
  12819. '']),
  12820. LinesToStr([ // $mod.$main
  12821. 'if (23 === $mod.e.GetValue()) ;',
  12822. 'if (24 === $mod.e.GetValue$1(25)) ;',
  12823. '']));
  12824. end;
  12825. procedure TTestModule.TestClass_ReintroducedVar;
  12826. begin
  12827. StartProgram(false);
  12828. Add('type');
  12829. Add(' TObject = class');
  12830. Add(' strict private');
  12831. Add(' Some: longint;');
  12832. Add(' end;');
  12833. Add(' TMobile = class');
  12834. Add(' strict private');
  12835. Add(' Some: string;');
  12836. Add(' end;');
  12837. Add(' TCar = class(tmobile)');
  12838. Add(' procedure Some;');
  12839. Add(' procedure Some(vA: longint);');
  12840. Add(' end;');
  12841. Add('procedure tcar.some;');
  12842. Add('begin');
  12843. Add(' Some;');
  12844. Add(' Some(1);');
  12845. Add('end;');
  12846. Add('procedure tcar.some(va: longint); begin end;');
  12847. Add('begin');
  12848. ConvertProgram;
  12849. CheckSource('TestClass_ReintroducedVar',
  12850. LinesToStr([ // statements
  12851. 'rtl.createClass($mod, "TObject", null, function () {',
  12852. ' this.$init = function () {',
  12853. ' this.Some = 0;',
  12854. ' };',
  12855. ' this.$final = function () {',
  12856. ' };',
  12857. '});',
  12858. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  12859. ' this.$init = function () {',
  12860. ' $mod.TObject.$init.call(this);',
  12861. ' this.Some$1 = "";',
  12862. ' };',
  12863. '});',
  12864. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  12865. ' this.Some$2 = function () {',
  12866. ' this.Some$2();',
  12867. ' this.Some$3(1);',
  12868. ' };',
  12869. ' this.Some$3 = function (vA) {',
  12870. ' };',
  12871. '});',
  12872. '']),
  12873. LinesToStr([ // $mod.$main
  12874. '']));
  12875. end;
  12876. procedure TTestModule.TestClass_RaiseDescendant;
  12877. begin
  12878. StartProgram(false);
  12879. Add([
  12880. 'type',
  12881. ' TObject = class',
  12882. ' constructor Create(Msg: string);',
  12883. ' end;',
  12884. ' Exception = class',
  12885. ' end;',
  12886. ' EConvertError = class(Exception)',
  12887. ' end;',
  12888. 'constructor TObject.Create(Msg: string); begin end;',
  12889. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  12890. 'begin',
  12891. ' raise Exception.Create(''Bar1'');',
  12892. ' raise EConvertError.Create(''Bar2'');',
  12893. ' raise AssertConv(''Bar2'');',
  12894. ' raise AssertConv;',
  12895. '']);
  12896. ConvertProgram;
  12897. CheckSource('TestClass_RaiseDescendant',
  12898. LinesToStr([ // statements
  12899. 'rtl.createClass($mod, "TObject", null, function () {',
  12900. ' this.$init = function () {',
  12901. ' };',
  12902. ' this.$final = function () {',
  12903. ' };',
  12904. ' this.Create = function (Msg) {',
  12905. ' return this;',
  12906. ' };',
  12907. '});',
  12908. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  12909. '});',
  12910. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  12911. '});',
  12912. 'this.AssertConv = function (Msg) {',
  12913. ' var Result = null;',
  12914. ' return Result;',
  12915. '};',
  12916. '']),
  12917. LinesToStr([ // $mod.$main
  12918. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  12919. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  12920. 'throw $mod.AssertConv("Bar2");',
  12921. 'throw $mod.AssertConv("def");',
  12922. '']));
  12923. end;
  12924. procedure TTestModule.TestClass_ExternalMethod;
  12925. begin
  12926. AddModuleWithIntfImplSrc('unit2.pas',
  12927. LinesToStr([
  12928. 'type',
  12929. ' TObject = class',
  12930. ' public',
  12931. ' procedure Intern; external name ''$DoIntern'';',
  12932. ' end;',
  12933. '']),
  12934. LinesToStr([
  12935. '']));
  12936. StartUnit(true);
  12937. Add('interface');
  12938. Add('uses unit2;');
  12939. Add('type');
  12940. Add(' TCar = class(TObject)');
  12941. Add(' public');
  12942. Add(' procedure Intern2; external name ''$DoIntern2'';');
  12943. Add(' procedure DoIt;');
  12944. Add(' end;');
  12945. Add('implementation');
  12946. Add('procedure tcar.doit;');
  12947. Add('begin');
  12948. Add(' Intern;');
  12949. Add(' Intern();');
  12950. Add(' Intern2;');
  12951. Add(' Intern2();');
  12952. Add('end;');
  12953. Add('var Obj: TCar;');
  12954. Add('begin');
  12955. Add(' obj.intern;');
  12956. Add(' obj.intern();');
  12957. Add(' obj.intern2;');
  12958. Add(' obj.intern2();');
  12959. Add(' obj.doit;');
  12960. Add(' obj.doit();');
  12961. Add(' with obj do begin');
  12962. Add(' Intern;');
  12963. Add(' Intern();');
  12964. Add(' Intern2;');
  12965. Add(' Intern2();');
  12966. Add(' end;');
  12967. ConvertUnit;
  12968. CheckSource('TestClass_ExternalMethod',
  12969. LinesToStr([
  12970. 'var $impl = $mod.$impl;',
  12971. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  12972. ' this.DoIt = function () {',
  12973. ' this.$DoIntern();',
  12974. ' this.$DoIntern();',
  12975. ' this.$DoIntern2();',
  12976. ' this.$DoIntern2();',
  12977. ' };',
  12978. ' });',
  12979. '']),
  12980. LinesToStr([ // this.$init
  12981. '$impl.Obj.$DoIntern();',
  12982. '$impl.Obj.$DoIntern();',
  12983. '$impl.Obj.$DoIntern2();',
  12984. '$impl.Obj.$DoIntern2();',
  12985. '$impl.Obj.DoIt();',
  12986. '$impl.Obj.DoIt();',
  12987. 'var $with1 = $impl.Obj;',
  12988. '$with1.$DoIntern();',
  12989. '$with1.$DoIntern();',
  12990. '$with1.$DoIntern2();',
  12991. '$with1.$DoIntern2();',
  12992. '']),
  12993. LinesToStr([ // implementation
  12994. '$impl.Obj = null;',
  12995. '']) );
  12996. end;
  12997. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  12998. begin
  12999. StartProgram(false);
  13000. Add('type');
  13001. Add(' TObject = class');
  13002. Add(' procedure DoIt; virtual; external name ''Foo'';');
  13003. Add(' end;');
  13004. Add('begin');
  13005. SetExpectedPasResolverError('Virtual method name must match external',
  13006. nVirtualMethodNameMustMatchExternal);
  13007. ConvertProgram;
  13008. end;
  13009. procedure TTestModule.TestClass_ExternalOverrideFail;
  13010. begin
  13011. StartProgram(false);
  13012. Add('type');
  13013. Add(' TObject = class');
  13014. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  13015. Add(' end;');
  13016. Add(' TCar = class');
  13017. Add(' procedure DoIt; override; external name ''DoIt'';');
  13018. Add(' end;');
  13019. Add('begin');
  13020. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  13021. nInvalidXModifierY);
  13022. ConvertProgram;
  13023. end;
  13024. procedure TTestModule.TestClass_ExternalVar;
  13025. begin
  13026. AddModuleWithIntfImplSrc('unit2.pas',
  13027. LinesToStr([
  13028. '{$modeswitch externalclass}',
  13029. 'type',
  13030. ' TObject = class',
  13031. ' public',
  13032. ' Intern: longint external name ''$Intern'';',
  13033. ' Bracket: longint external name ''["A B"]'';',
  13034. ' end;',
  13035. '']),
  13036. LinesToStr([
  13037. '']));
  13038. StartUnit(true);
  13039. Add([
  13040. 'interface',
  13041. 'uses unit2;',
  13042. '{$modeswitch externalclass}',
  13043. 'type',
  13044. ' TCar = class(tobject)',
  13045. ' public',
  13046. ' Intern2: longint external name ''$Intern2'';',
  13047. ' procedure DoIt;',
  13048. ' end;',
  13049. 'implementation',
  13050. 'procedure tcar.doit;',
  13051. 'begin',
  13052. ' Intern:=Intern+1;',
  13053. ' Intern2:=Intern2+2;',
  13054. ' Bracket:=Bracket+3;',
  13055. 'end;',
  13056. 'var Obj: TCar;',
  13057. 'begin',
  13058. ' obj.intern:=obj.intern+1;',
  13059. ' obj.intern2:=obj.intern2+2;',
  13060. ' obj.Bracket:=obj.Bracket+3;',
  13061. ' with obj do begin',
  13062. ' intern:=intern+1;',
  13063. ' intern2:=intern2+2;',
  13064. ' Bracket:=Bracket+3;',
  13065. ' end;']);
  13066. ConvertUnit;
  13067. CheckSource('TestClass_ExternalVar',
  13068. LinesToStr([
  13069. 'var $impl = $mod.$impl;',
  13070. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13071. ' this.DoIt = function () {',
  13072. ' this.$Intern = this.$Intern + 1;',
  13073. ' this.$Intern2 = this.$Intern2 + 2;',
  13074. ' this["A B"] = this["A B"] + 3;',
  13075. ' };',
  13076. ' });',
  13077. '']),
  13078. LinesToStr([
  13079. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  13080. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  13081. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  13082. 'var $with1 = $impl.Obj;',
  13083. '$with1.$Intern = $with1.$Intern + 1;',
  13084. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  13085. '$with1["A B"] = $with1["A B"] + 3;',
  13086. '']),
  13087. LinesToStr([ // implementation
  13088. '$impl.Obj = null;',
  13089. '']));
  13090. end;
  13091. procedure TTestModule.TestClass_Const;
  13092. begin
  13093. StartProgram(false);
  13094. Add('type');
  13095. Add(' integer = longint;');
  13096. Add(' TClass = class of TObject;');
  13097. Add(' TObject = class');
  13098. Add(' public');
  13099. Add(' const cI: integer = 3;');
  13100. Add(' procedure DoIt;');
  13101. Add(' class procedure DoMore;');
  13102. Add(' end;');
  13103. Add('implementation');
  13104. Add('procedure tobject.doit;');
  13105. Add('begin');
  13106. Add(' if cI=4 then;');
  13107. Add(' if 5=cI then;');
  13108. Add(' if Self.cI=6 then;');
  13109. Add(' if 7=Self.cI then;');
  13110. Add(' with Self do begin');
  13111. Add(' if cI=11 then;');
  13112. Add(' if 12=cI then;');
  13113. Add(' end;');
  13114. Add('end;');
  13115. Add('class procedure tobject.domore;');
  13116. Add('begin');
  13117. Add(' if cI=8 then;');
  13118. Add(' if Self.cI=9 then;');
  13119. Add(' if 10=cI then;');
  13120. Add(' if 11=Self.cI then;');
  13121. Add(' with Self do begin');
  13122. Add(' if cI=13 then;');
  13123. Add(' if 14=cI then;');
  13124. Add(' end;');
  13125. Add('end;');
  13126. Add('var');
  13127. Add(' Obj: TObject;');
  13128. Add(' Cla: TClass;');
  13129. Add('begin');
  13130. Add(' if TObject.cI=21 then ;');
  13131. Add(' if Obj.cI=22 then ;');
  13132. Add(' if Cla.cI=23 then ;');
  13133. Add(' with obj do if ci=24 then;');
  13134. Add(' with TObject do if ci=25 then;');
  13135. Add(' with Cla do if ci=26 then;');
  13136. ConvertProgram;
  13137. CheckSource('TestClass_Const',
  13138. LinesToStr([
  13139. 'rtl.createClass($mod, "TObject", null, function () {',
  13140. ' this.cI = 3;',
  13141. ' this.$init = function () {',
  13142. ' };',
  13143. ' this.$final = function () {',
  13144. ' };',
  13145. ' this.DoIt = function () {',
  13146. ' if (this.cI === 4) ;',
  13147. ' if (5 === this.cI) ;',
  13148. ' if (this.cI === 6) ;',
  13149. ' if (7 === this.cI) ;',
  13150. ' if (this.cI === 11) ;',
  13151. ' if (12 === this.cI) ;',
  13152. ' };',
  13153. ' this.DoMore = function () {',
  13154. ' if (this.cI === 8) ;',
  13155. ' if (this.cI === 9) ;',
  13156. ' if (10 === this.cI) ;',
  13157. ' if (11 === this.cI) ;',
  13158. ' if (this.cI === 13) ;',
  13159. ' if (14 === this.cI) ;',
  13160. ' };',
  13161. '});',
  13162. 'this.Obj = null;',
  13163. 'this.Cla = null;',
  13164. '']),
  13165. LinesToStr([
  13166. 'if ($mod.TObject.cI === 21) ;',
  13167. 'if ($mod.Obj.cI === 22) ;',
  13168. 'if ($mod.Cla.cI === 23) ;',
  13169. 'var $with1 = $mod.Obj;',
  13170. 'if ($with1.cI === 24) ;',
  13171. 'var $with2 = $mod.TObject;',
  13172. 'if ($with2.cI === 25) ;',
  13173. 'var $with3 = $mod.Cla;',
  13174. 'if ($with3.cI === 26) ;',
  13175. '']));
  13176. end;
  13177. procedure TTestModule.TestClass_LocalVarSelfFail;
  13178. begin
  13179. StartProgram(false);
  13180. Add([
  13181. 'type',
  13182. ' TObject = class',
  13183. ' constructor Create;',
  13184. ' end;',
  13185. 'constructor tobject.create;',
  13186. 'var self: longint;',
  13187. 'begin',
  13188. 'end',
  13189. 'begin',
  13190. '']);
  13191. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  13192. ConvertProgram;
  13193. end;
  13194. procedure TTestModule.TestClass_ArgSelfFail;
  13195. begin
  13196. StartProgram(false);
  13197. Add([
  13198. 'type',
  13199. ' TObject = class',
  13200. ' procedure DoIt(Self: longint);',
  13201. ' end;',
  13202. 'procedure tobject.doit(self: longint);',
  13203. 'begin',
  13204. 'end',
  13205. 'begin',
  13206. '']);
  13207. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  13208. ConvertProgram;
  13209. end;
  13210. procedure TTestModule.TestClass_NestedProcSelf;
  13211. begin
  13212. StartProgram(false);
  13213. Add([
  13214. 'type',
  13215. ' TObject = class',
  13216. ' Key: longint;',
  13217. ' class var State: longint;',
  13218. ' procedure DoIt;',
  13219. ' function GetSize: longint; virtual; abstract;',
  13220. ' procedure SetSize(Value: longint); virtual; abstract;',
  13221. ' property Size: longint read GetSize write SetSize;',
  13222. ' end;',
  13223. 'procedure tobject.doit;',
  13224. ' procedure Sub;',
  13225. ' begin',
  13226. ' key:=key+2;',
  13227. ' self.key:=self.key+3;',
  13228. ' state:=state+4;',
  13229. ' self.state:=self.state+5;',
  13230. ' tobject.state:=tobject.state+6;',
  13231. ' size:=size+7;',
  13232. ' self.size:=self.size+8;',
  13233. ' end;',
  13234. 'begin',
  13235. ' sub;',
  13236. ' key:=key+12;',
  13237. ' self.key:=self.key+13;',
  13238. ' state:=state+14;',
  13239. ' self.state:=self.state+15;',
  13240. ' tobject.state:=tobject.state+16;',
  13241. ' size:=size+17;',
  13242. ' self.size:=self.size+18;',
  13243. 'end;',
  13244. 'begin',
  13245. '']);
  13246. ConvertProgram;
  13247. CheckSource('TestClass_NestedProcSelf',
  13248. LinesToStr([ // statements
  13249. 'rtl.createClass($mod, "TObject", null, function () {',
  13250. ' this.State = 0;',
  13251. ' this.$init = function () {',
  13252. ' this.Key = 0;',
  13253. ' };',
  13254. ' this.$final = function () {',
  13255. ' };',
  13256. ' this.DoIt = function () {',
  13257. ' var $Self = this;',
  13258. ' function Sub() {',
  13259. ' $Self.Key = $Self.Key + 2;',
  13260. ' $Self.Key = $Self.Key + 3;',
  13261. ' $mod.TObject.State = $Self.State + 4;',
  13262. ' $mod.TObject.State = $Self.State + 5;',
  13263. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13264. ' $Self.SetSize($Self.GetSize() + 7);',
  13265. ' $Self.SetSize($Self.GetSize() + 8);',
  13266. ' };',
  13267. ' Sub();',
  13268. ' $Self.Key = $Self.Key + 12;',
  13269. ' $Self.Key = $Self.Key + 13;',
  13270. ' $mod.TObject.State = $Self.State + 14;',
  13271. ' $mod.TObject.State = $Self.State + 15;',
  13272. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13273. ' $Self.SetSize($Self.GetSize() + 17);',
  13274. ' $Self.SetSize($Self.GetSize() + 18);',
  13275. ' };',
  13276. '});',
  13277. '']),
  13278. LinesToStr([ // $mod.$main
  13279. '']));
  13280. end;
  13281. procedure TTestModule.TestClass_NestedProcSelf2;
  13282. begin
  13283. StartProgram(false);
  13284. Add([
  13285. 'type',
  13286. ' TObject = class',
  13287. ' Key: longint;',
  13288. ' class var State: longint;',
  13289. ' function GetSize: longint; virtual; abstract;',
  13290. ' procedure SetSize(Value: longint); virtual; abstract;',
  13291. ' property Size: longint read GetSize write SetSize;',
  13292. ' end;',
  13293. ' TBird = class',
  13294. ' procedure DoIt;',
  13295. ' end;',
  13296. 'procedure tbird.doit;',
  13297. ' procedure Sub;',
  13298. ' begin',
  13299. ' key:=key+2;',
  13300. ' self.key:=self.key+3;',
  13301. ' state:=state+4;',
  13302. ' self.state:=self.state+5;',
  13303. ' tobject.state:=tobject.state+6;',
  13304. ' size:=size+7;',
  13305. ' self.size:=self.size+8;',
  13306. ' end;',
  13307. 'begin',
  13308. ' sub;',
  13309. ' key:=key+12;',
  13310. ' self.key:=self.key+13;',
  13311. ' state:=state+14;',
  13312. ' self.state:=self.state+15;',
  13313. ' tobject.state:=tobject.state+16;',
  13314. ' size:=size+17;',
  13315. ' self.size:=self.size+18;',
  13316. 'end;',
  13317. 'begin',
  13318. '']);
  13319. ConvertProgram;
  13320. CheckSource('TestClass_NestedProcSelf2',
  13321. LinesToStr([ // statements
  13322. 'rtl.createClass($mod, "TObject", null, function () {',
  13323. ' this.State = 0;',
  13324. ' this.$init = function () {',
  13325. ' this.Key = 0;',
  13326. ' };',
  13327. ' this.$final = function () {',
  13328. ' };',
  13329. '});',
  13330. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13331. ' this.DoIt = function () {',
  13332. ' var $Self = this;',
  13333. ' function Sub() {',
  13334. ' $Self.Key = $Self.Key + 2;',
  13335. ' $Self.Key = $Self.Key + 3;',
  13336. ' $mod.TObject.State = $Self.State + 4;',
  13337. ' $mod.TObject.State = $Self.State + 5;',
  13338. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13339. ' $Self.SetSize($Self.GetSize() + 7);',
  13340. ' $Self.SetSize($Self.GetSize() + 8);',
  13341. ' };',
  13342. ' Sub();',
  13343. ' $Self.Key = $Self.Key + 12;',
  13344. ' $Self.Key = $Self.Key + 13;',
  13345. ' $mod.TObject.State = $Self.State + 14;',
  13346. ' $mod.TObject.State = $Self.State + 15;',
  13347. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13348. ' $Self.SetSize($Self.GetSize() + 17);',
  13349. ' $Self.SetSize($Self.GetSize() + 18);',
  13350. ' };',
  13351. '});',
  13352. '']),
  13353. LinesToStr([ // $mod.$main
  13354. '']));
  13355. end;
  13356. procedure TTestModule.TestClass_NestedProcClassSelf;
  13357. begin
  13358. StartProgram(false);
  13359. Add([
  13360. 'type',
  13361. ' TObject = class',
  13362. ' class var State: longint;',
  13363. ' class procedure DoIt;',
  13364. ' class function GetSize: longint; virtual; abstract;',
  13365. ' class procedure SetSize(Value: longint); virtual; abstract;',
  13366. ' class property Size: longint read GetSize write SetSize;',
  13367. ' end;',
  13368. 'class procedure tobject.doit;',
  13369. ' procedure Sub;',
  13370. ' begin',
  13371. ' state:=state+2;',
  13372. ' self.state:=self.state+3;',
  13373. ' tobject.state:=tobject.state+4;',
  13374. ' size:=size+5;',
  13375. ' self.size:=self.size+6;',
  13376. ' tobject.size:=tobject.size+7;',
  13377. ' end;',
  13378. 'begin',
  13379. ' sub;',
  13380. ' state:=state+12;',
  13381. ' self.state:=self.state+13;',
  13382. ' tobject.state:=tobject.state+14;',
  13383. ' size:=size+15;',
  13384. ' self.size:=self.size+16;',
  13385. ' tobject.size:=tobject.size+17;',
  13386. 'end;',
  13387. 'begin',
  13388. '']);
  13389. ConvertProgram;
  13390. CheckSource('TestClass_NestedProcClassSelf',
  13391. LinesToStr([ // statements
  13392. 'rtl.createClass($mod, "TObject", null, function () {',
  13393. ' this.State = 0;',
  13394. ' this.$init = function () {',
  13395. ' };',
  13396. ' this.$final = function () {',
  13397. ' };',
  13398. ' this.DoIt = function () {',
  13399. ' var $Self = this;',
  13400. ' function Sub() {',
  13401. ' $mod.TObject.State = $Self.State + 2;',
  13402. ' $mod.TObject.State = $Self.State + 3;',
  13403. ' $mod.TObject.State = $mod.TObject.State + 4;',
  13404. ' $Self.SetSize($Self.GetSize() + 5);',
  13405. ' $Self.SetSize($Self.GetSize() + 6);',
  13406. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  13407. ' };',
  13408. ' Sub();',
  13409. ' $mod.TObject.State = $Self.State + 12;',
  13410. ' $mod.TObject.State = $Self.State + 13;',
  13411. ' $mod.TObject.State = $mod.TObject.State + 14;',
  13412. ' $Self.SetSize($Self.GetSize() + 15);',
  13413. ' $Self.SetSize($Self.GetSize() + 16);',
  13414. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  13415. ' };',
  13416. '});',
  13417. '']),
  13418. LinesToStr([ // $mod.$main
  13419. '']));
  13420. end;
  13421. procedure TTestModule.TestClass_NestedProcCallInherited;
  13422. begin
  13423. StartProgram(false);
  13424. Add([
  13425. 'type',
  13426. ' TObject = class',
  13427. ' function DoIt(k: boolean): longint; virtual;',
  13428. ' end;',
  13429. ' TBird = class',
  13430. ' function DoIt(k: boolean): longint; override;',
  13431. ' end;',
  13432. 'function tobject.doit(k: boolean): longint;',
  13433. 'begin',
  13434. 'end;',
  13435. 'function tbird.doit(k: boolean): longint;',
  13436. ' procedure Sub;',
  13437. ' begin',
  13438. ' inherited DoIt(true);',
  13439. //' if inherited DoIt(false)=4 then ;',
  13440. ' end;',
  13441. 'begin',
  13442. ' Sub;',
  13443. ' inherited;',
  13444. ' inherited DoIt(true);',
  13445. //' if inherited DoIt(false)=14 then ;',
  13446. 'end;',
  13447. 'begin',
  13448. '']);
  13449. ConvertProgram;
  13450. CheckSource('TestClass_NestedProcCallInherited',
  13451. LinesToStr([ // statements
  13452. 'rtl.createClass($mod, "TObject", null, function () {',
  13453. ' this.$init = function () {',
  13454. ' };',
  13455. ' this.$final = function () {',
  13456. ' };',
  13457. ' this.DoIt = function (k) {',
  13458. ' var Result = 0;',
  13459. ' return Result;',
  13460. ' };',
  13461. '});',
  13462. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13463. ' this.DoIt = function (k) {',
  13464. ' var $Self = this;',
  13465. ' var Result = 0;',
  13466. ' function Sub() {',
  13467. ' $mod.TObject.DoIt.call($Self, true);',
  13468. ' };',
  13469. ' Sub();',
  13470. ' $mod.TObject.DoIt.apply($Self, arguments);',
  13471. ' $mod.TObject.DoIt.call($Self, true);',
  13472. ' return Result;',
  13473. ' };',
  13474. '});',
  13475. '']),
  13476. LinesToStr([ // $mod.$main
  13477. '']));
  13478. end;
  13479. procedure TTestModule.TestClass_TObjectFree;
  13480. begin
  13481. StartProgram(false);
  13482. Add([
  13483. 'type',
  13484. ' TObject = class',
  13485. ' Obj: tobject;',
  13486. ' procedure Free;',
  13487. ' procedure Release;',
  13488. ' end;',
  13489. 'procedure tobject.free;',
  13490. 'begin',
  13491. 'end;',
  13492. 'procedure tobject.release;',
  13493. 'begin',
  13494. ' free;',
  13495. ' if true then free;',
  13496. 'end;',
  13497. 'function DoIt(o: tobject): tobject;',
  13498. 'var l: tobject;',
  13499. 'begin',
  13500. ' o.free;',
  13501. ' o.free();',
  13502. ' l.free;',
  13503. ' l.free();',
  13504. ' o.obj.free;',
  13505. ' o.obj.free();',
  13506. ' with o do obj.free;',
  13507. ' with o do obj.free();',
  13508. ' result.Free;',
  13509. ' result.Free();',
  13510. 'end;',
  13511. 'var o: tobject;',
  13512. ' a: array of tobject;',
  13513. 'begin',
  13514. ' o.free;',
  13515. ' o.obj.free;',
  13516. ' a[1+2].free;',
  13517. '']);
  13518. ConvertProgram;
  13519. CheckSource('TestClass_TObjectFree',
  13520. LinesToStr([ // statements
  13521. 'rtl.createClass($mod, "TObject", null, function () {',
  13522. ' this.$init = function () {',
  13523. ' this.Obj = null;',
  13524. ' };',
  13525. ' this.$final = function () {',
  13526. ' this.Obj = undefined;',
  13527. ' };',
  13528. ' this.Free = function () {',
  13529. ' };',
  13530. ' this.Release = function () {',
  13531. ' this.Free();',
  13532. ' if (true) this.Free();',
  13533. ' };',
  13534. '});',
  13535. 'this.DoIt = function (o) {',
  13536. ' var Result = null;',
  13537. ' var l = null;',
  13538. ' o = rtl.freeLoc(o);',
  13539. ' o = rtl.freeLoc(o);',
  13540. ' l = rtl.freeLoc(l);',
  13541. ' l = rtl.freeLoc(l);',
  13542. ' rtl.free(o, "Obj");',
  13543. ' rtl.free(o, "Obj");',
  13544. ' rtl.free(o, "Obj");',
  13545. ' rtl.free(o, "Obj");',
  13546. ' Result = rtl.freeLoc(Result);',
  13547. ' Result = rtl.freeLoc(Result);',
  13548. ' return Result;',
  13549. '};',
  13550. 'this.o = null;',
  13551. 'this.a = [];',
  13552. '']),
  13553. LinesToStr([ // $mod.$main
  13554. 'rtl.free($mod, "o");',
  13555. 'rtl.free($mod.o, "Obj");',
  13556. 'rtl.free($mod.a, 1 + 2);',
  13557. '']));
  13558. end;
  13559. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  13560. begin
  13561. StartProgram(false);
  13562. Add([
  13563. 'type',
  13564. ' TObject = class',
  13565. ' constructor Create;',
  13566. ' procedure Free;',
  13567. ' end;',
  13568. 'constructor TObject.Create; begin end;',
  13569. 'procedure tobject.free; begin end;',
  13570. 'begin',
  13571. ' with tobject.create do free;',
  13572. '']);
  13573. ConvertProgram;
  13574. CheckSource('TestClass_TObjectFreeNewInstance',
  13575. LinesToStr([ // statements
  13576. 'rtl.createClass($mod, "TObject", null, function () {',
  13577. ' this.$init = function () {',
  13578. ' };',
  13579. ' this.$final = function () {',
  13580. ' };',
  13581. ' this.Create = function () {',
  13582. ' return this;',
  13583. ' };',
  13584. ' this.Free = function () {',
  13585. ' };',
  13586. '});',
  13587. '']),
  13588. LinesToStr([ // $mod.$main
  13589. 'var $with1 = $mod.TObject.$create("Create");',
  13590. '$with1=rtl.freeLoc($with1);',
  13591. '']));
  13592. end;
  13593. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  13594. begin
  13595. StartProgram(false);
  13596. Add([
  13597. 'type',
  13598. ' TObject = class',
  13599. ' destructor Destroy;',
  13600. ' procedure Free;',
  13601. ' end;',
  13602. 'destructor TObject.Destroy; begin end;',
  13603. 'procedure tobject.free; begin end;',
  13604. 'var o: tobject;',
  13605. 'begin',
  13606. ' o.free;',
  13607. '']);
  13608. Converter.UseLowerCase:=true;
  13609. ConvertProgram;
  13610. CheckSource('TestClass_TObjectFreeLowerCase',
  13611. LinesToStr([ // statements
  13612. 'rtl.createClass($mod, "tobject", null, function () {',
  13613. ' this.$init = function () {',
  13614. ' };',
  13615. ' this.$final = function () {',
  13616. ' };',
  13617. ' rtl.tObjectDestroy = "destroy";',
  13618. ' this.destroy = function () {',
  13619. ' };',
  13620. ' this.free = function () {',
  13621. ' };',
  13622. '});',
  13623. 'this.o = null;',
  13624. '']),
  13625. LinesToStr([ // $mod.$main
  13626. 'rtl.free($mod, "o");',
  13627. '']));
  13628. end;
  13629. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  13630. begin
  13631. StartProgram(false);
  13632. Add([
  13633. 'type',
  13634. ' TObject = class',
  13635. ' procedure Free;',
  13636. ' function GetObj: tobject; virtual; abstract;',
  13637. ' end;',
  13638. 'procedure tobject.free;',
  13639. 'begin',
  13640. 'end;',
  13641. 'var o: tobject;',
  13642. 'begin',
  13643. ' o.getobj.free;',
  13644. '']);
  13645. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  13646. ConvertProgram;
  13647. end;
  13648. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  13649. begin
  13650. StartProgram(false);
  13651. Add([
  13652. 'type',
  13653. ' TObject = class',
  13654. ' procedure Free;',
  13655. ' FObj: TObject;',
  13656. ' property Obj: tobject read FObj write FObj;',
  13657. ' end;',
  13658. 'procedure tobject.free;',
  13659. 'begin',
  13660. 'end;',
  13661. 'var o: tobject;',
  13662. 'begin',
  13663. ' o.obj.free;',
  13664. '']);
  13665. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  13666. ConvertProgram;
  13667. end;
  13668. procedure TTestModule.TestClass_ForIn;
  13669. begin
  13670. StartProgram(false);
  13671. Add([
  13672. 'type',
  13673. ' TObject = class end;',
  13674. ' TItem = TObject;',
  13675. ' TEnumerator = class',
  13676. ' FCurrent: TItem;',
  13677. ' property Current: TItem read FCurrent;',
  13678. ' function MoveNext: boolean;',
  13679. ' end;',
  13680. ' TBird = class',
  13681. ' function GetEnumerator: TEnumerator;',
  13682. ' end;',
  13683. 'function TEnumerator.MoveNext: boolean;',
  13684. 'begin',
  13685. 'end;',
  13686. 'function TBird.GetEnumerator: TEnumerator;',
  13687. 'begin',
  13688. 'end;',
  13689. 'var',
  13690. ' b: TBird;',
  13691. ' i, i2: TItem;',
  13692. 'begin',
  13693. ' for i in b do i2:=i;']);
  13694. ConvertProgram;
  13695. CheckSource('TestClass_ForIn',
  13696. LinesToStr([ // statements
  13697. 'rtl.createClass($mod, "TObject", null, function () {',
  13698. ' this.$init = function () {',
  13699. ' };',
  13700. ' this.$final = function () {',
  13701. ' };',
  13702. '});',
  13703. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  13704. ' this.$init = function () {',
  13705. ' $mod.TObject.$init.call(this);',
  13706. ' this.FCurrent = null;',
  13707. ' };',
  13708. ' this.$final = function () {',
  13709. ' this.FCurrent = undefined;',
  13710. ' $mod.TObject.$final.call(this);',
  13711. ' };',
  13712. ' this.MoveNext = function () {',
  13713. ' var Result = false;',
  13714. ' return Result;',
  13715. ' };',
  13716. '});',
  13717. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13718. ' this.GetEnumerator = function () {',
  13719. ' var Result = null;',
  13720. ' return Result;',
  13721. ' };',
  13722. '});',
  13723. 'this.b = null;',
  13724. 'this.i = null;',
  13725. 'this.i2 = null;'
  13726. ]),
  13727. LinesToStr([ // $mod.$main
  13728. 'var $in1 = $mod.b.GetEnumerator();',
  13729. 'try {',
  13730. ' while ($in1.MoveNext()){',
  13731. ' $mod.i = $in1.FCurrent;',
  13732. ' $mod.i2 = $mod.i;',
  13733. ' }',
  13734. '} finally {',
  13735. ' $in1 = rtl.freeLoc($in1)',
  13736. '};',
  13737. '']));
  13738. end;
  13739. procedure TTestModule.TestClassOf_Create;
  13740. begin
  13741. StartProgram(false);
  13742. Add('type');
  13743. Add(' TObject = class');
  13744. Add(' constructor Create;');
  13745. Add(' end;');
  13746. Add(' TClass = class of TObject;');
  13747. Add('constructor tobject.create; begin end;');
  13748. Add('var');
  13749. Add(' Obj: tobject;');
  13750. Add(' C: tclass;');
  13751. Add('begin');
  13752. Add(' obj:=C.create;');
  13753. Add(' with c do obj:=create;');
  13754. ConvertProgram;
  13755. CheckSource('TestClassOf_Create',
  13756. LinesToStr([ // statements
  13757. 'rtl.createClass($mod, "TObject", null, function () {',
  13758. ' this.$init = function () {',
  13759. ' };',
  13760. ' this.$final = function () {',
  13761. ' };',
  13762. ' this.Create = function () {',
  13763. ' return this;',
  13764. ' };',
  13765. '});',
  13766. 'this.Obj = null;',
  13767. 'this.C = null;'
  13768. ]),
  13769. LinesToStr([ // $mod.$main
  13770. '$mod.Obj = $mod.C.$create("Create");',
  13771. 'var $with1 = $mod.C;',
  13772. '$mod.Obj = $with1.$create("Create");',
  13773. '']));
  13774. end;
  13775. procedure TTestModule.TestClassOf_Call;
  13776. begin
  13777. StartProgram(false);
  13778. Add('type');
  13779. Add(' TObject = class');
  13780. Add(' class procedure DoIt;');
  13781. Add(' end;');
  13782. Add(' TClass = class of TObject;');
  13783. Add('class procedure tobject.doit; begin end;');
  13784. Add('var');
  13785. Add(' C: tclass;');
  13786. Add('begin');
  13787. Add(' c.doit;');
  13788. Add(' with c do doit;');
  13789. ConvertProgram;
  13790. CheckSource('TestClassOf_Call',
  13791. LinesToStr([ // statements
  13792. 'rtl.createClass($mod, "TObject", null, function () {',
  13793. ' this.$init = function () {',
  13794. ' };',
  13795. ' this.$final = function () {',
  13796. ' };',
  13797. ' this.DoIt = function () {',
  13798. ' };',
  13799. '});',
  13800. 'this.C = null;'
  13801. ]),
  13802. LinesToStr([ // $mod.$main
  13803. '$mod.C.DoIt();',
  13804. 'var $with1 = $mod.C;',
  13805. '$with1.DoIt();',
  13806. '']));
  13807. end;
  13808. procedure TTestModule.TestClassOf_Assign;
  13809. begin
  13810. StartProgram(false);
  13811. Add('type');
  13812. Add(' TClass = class of TObject;');
  13813. Add(' TObject = class');
  13814. Add(' ClassType: TClass; ');
  13815. Add(' end;');
  13816. Add('var');
  13817. Add(' Obj: tobject;');
  13818. Add(' C: tclass;');
  13819. Add('begin');
  13820. Add(' c:=nil;');
  13821. Add(' c:=obj.classtype;');
  13822. ConvertProgram;
  13823. CheckSource('TestClassOf_Assign',
  13824. LinesToStr([ // statements
  13825. 'rtl.createClass($mod, "TObject", null, function () {',
  13826. ' this.$init = function () {',
  13827. ' this.ClassType = null;',
  13828. ' };',
  13829. ' this.$final = function () {',
  13830. ' this.ClassType = undefined;',
  13831. ' };',
  13832. '});',
  13833. 'this.Obj = null;',
  13834. 'this.C = null;'
  13835. ]),
  13836. LinesToStr([ // $mod.$main
  13837. '$mod.C = null;',
  13838. '$mod.C = $mod.Obj.ClassType;',
  13839. '']));
  13840. end;
  13841. procedure TTestModule.TestClassOf_Is;
  13842. begin
  13843. StartProgram(false);
  13844. Add('type');
  13845. Add(' TClass = class of TObject;');
  13846. Add(' TObject = class');
  13847. Add(' end;');
  13848. Add(' TCar = class');
  13849. Add(' end;');
  13850. Add(' TCars = class of TCar;');
  13851. Add('var');
  13852. Add(' Obj: tobject;');
  13853. Add(' C: tclass;');
  13854. Add(' Cars: tcars;');
  13855. Add('begin');
  13856. Add(' if c is tcar then ;');
  13857. Add(' if c is tcars then ;');
  13858. ConvertProgram;
  13859. CheckSource('TestClassOf_Is',
  13860. LinesToStr([ // statements
  13861. 'rtl.createClass($mod, "TObject", null, function () {',
  13862. ' this.$init = function () {',
  13863. ' };',
  13864. ' this.$final = function () {',
  13865. ' };',
  13866. '});',
  13867. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13868. '});',
  13869. 'this.Obj = null;',
  13870. 'this.C = null;',
  13871. 'this.Cars = null;'
  13872. ]),
  13873. LinesToStr([ // $mod.$main
  13874. 'if(rtl.is($mod.C,$mod.TCar));',
  13875. 'if(rtl.is($mod.C,$mod.TCar));',
  13876. '']));
  13877. end;
  13878. procedure TTestModule.TestClassOf_Compare;
  13879. begin
  13880. StartProgram(false);
  13881. Add('type');
  13882. Add(' TClass = class of TObject;');
  13883. Add(' TObject = class');
  13884. Add(' ClassType: TClass; ');
  13885. Add(' end;');
  13886. Add('var');
  13887. Add(' b: boolean;');
  13888. Add(' Obj: tobject;');
  13889. Add(' C: tclass;');
  13890. Add('begin');
  13891. Add(' b:=c=nil;');
  13892. Add(' b:=nil=c;');
  13893. Add(' b:=c=obj.classtype;');
  13894. Add(' b:=obj.classtype=c;');
  13895. Add(' b:=c=TObject;');
  13896. Add(' b:=TObject=c;');
  13897. Add(' b:=c<>nil;');
  13898. Add(' b:=nil<>c;');
  13899. Add(' b:=c<>obj.classtype;');
  13900. Add(' b:=obj.classtype<>c;');
  13901. Add(' b:=c<>TObject;');
  13902. Add(' b:=TObject<>c;');
  13903. ConvertProgram;
  13904. CheckSource('TestClassOf_Compare',
  13905. LinesToStr([ // statements
  13906. 'rtl.createClass($mod, "TObject", null, function () {',
  13907. ' this.$init = function () {',
  13908. ' this.ClassType = null;',
  13909. ' };',
  13910. ' this.$final = function () {',
  13911. ' this.ClassType = undefined;',
  13912. ' };',
  13913. '});',
  13914. 'this.b = false;',
  13915. 'this.Obj = null;',
  13916. 'this.C = null;'
  13917. ]),
  13918. LinesToStr([ // $mod.$main
  13919. '$mod.b = $mod.C === null;',
  13920. '$mod.b = null === $mod.C;',
  13921. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  13922. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  13923. '$mod.b = $mod.C === $mod.TObject;',
  13924. '$mod.b = $mod.TObject === $mod.C;',
  13925. '$mod.b = $mod.C !== null;',
  13926. '$mod.b = null !== $mod.C;',
  13927. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  13928. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  13929. '$mod.b = $mod.C !== $mod.TObject;',
  13930. '$mod.b = $mod.TObject !== $mod.C;',
  13931. '']));
  13932. end;
  13933. procedure TTestModule.TestClassOf_ClassVar;
  13934. begin
  13935. StartProgram(false);
  13936. Add('type');
  13937. Add(' TObject = class');
  13938. Add(' class var id: longint;');
  13939. Add(' end;');
  13940. Add(' TClass = class of TObject;');
  13941. Add('var');
  13942. Add(' C: tclass;');
  13943. Add('begin');
  13944. Add(' C.id:=C.id;');
  13945. ConvertProgram;
  13946. CheckSource('TestClassOf_ClassVar',
  13947. LinesToStr([ // statements
  13948. 'rtl.createClass($mod, "TObject", null, function () {',
  13949. ' this.id = 0;',
  13950. ' this.$init = function () {',
  13951. ' };',
  13952. ' this.$final = function () {',
  13953. ' };',
  13954. '});',
  13955. 'this.C = null;'
  13956. ]),
  13957. LinesToStr([ // $mod.$main
  13958. '$mod.TObject.id = $mod.C.id;',
  13959. '']));
  13960. end;
  13961. procedure TTestModule.TestClassOf_ClassMethod;
  13962. begin
  13963. StartProgram(false);
  13964. Add('type');
  13965. Add(' TObject = class');
  13966. Add(' class function DoIt(i: longint = 0): longint;');
  13967. Add(' end;');
  13968. Add(' TClass = class of TObject;');
  13969. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  13970. Add('var');
  13971. Add(' i: longint;');
  13972. Add(' C: tclass;');
  13973. Add('begin');
  13974. Add(' C.DoIt;');
  13975. Add(' C.DoIt();');
  13976. Add(' i:=C.DoIt;');
  13977. Add(' i:=C.DoIt();');
  13978. ConvertProgram;
  13979. CheckSource('TestClassOf_ClassMethod',
  13980. LinesToStr([ // statements
  13981. 'rtl.createClass($mod, "TObject", null, function () {',
  13982. ' this.$init = function () {',
  13983. ' };',
  13984. ' this.$final = function () {',
  13985. ' };',
  13986. ' this.DoIt = function (i) {',
  13987. ' var Result = 0;',
  13988. ' return Result;',
  13989. ' };',
  13990. '});',
  13991. 'this.i = 0;',
  13992. 'this.C = null;'
  13993. ]),
  13994. LinesToStr([ // $mod.$main
  13995. '$mod.C.DoIt(0);',
  13996. '$mod.C.DoIt(0);',
  13997. '$mod.i = $mod.C.DoIt(0);',
  13998. '$mod.i = $mod.C.DoIt(0);',
  13999. '']));
  14000. end;
  14001. procedure TTestModule.TestClassOf_ClassProperty;
  14002. begin
  14003. StartProgram(false);
  14004. Add([
  14005. 'type',
  14006. ' TObject = class',
  14007. ' class var FA: longint;',
  14008. ' class function GetA: longint;',
  14009. ' class procedure SetA(Value: longint);',
  14010. ' class property pA: longint read fa write fa;',
  14011. ' class property pB: longint read geta write seta;',
  14012. ' end;',
  14013. ' TObjectClass = class of tobject;',
  14014. 'class function tobject.geta: longint; begin end;',
  14015. 'class procedure tobject.seta(value: longint); begin end;',
  14016. 'var',
  14017. ' b: boolean;',
  14018. ' Obj: tobject;',
  14019. ' Cla: tobjectclass;',
  14020. 'begin',
  14021. ' obj.pa:=obj.pa;',
  14022. ' obj.pb:=obj.pb;',
  14023. ' b:=obj.pa=4;',
  14024. ' b:=obj.pb=obj.pb;',
  14025. ' b:=5=obj.pa;',
  14026. ' cla.pa:=6;',
  14027. ' cla.pa:=cla.pa;',
  14028. ' cla.pb:=cla.pb;',
  14029. ' b:=cla.pa=7;',
  14030. ' b:=cla.pb=cla.pb;',
  14031. ' b:=8=cla.pa;',
  14032. ' tobject.pa:=9;',
  14033. ' tobject.pb:=tobject.pb;',
  14034. ' b:=tobject.pa=10;',
  14035. ' b:=11=tobject.pa;',
  14036. '']);
  14037. ConvertProgram;
  14038. CheckSource('TestClassOf_ClassProperty',
  14039. LinesToStr([ // statements
  14040. 'rtl.createClass($mod, "TObject", null, function () {',
  14041. ' this.FA = 0;',
  14042. ' this.$init = function () {',
  14043. ' };',
  14044. ' this.$final = function () {',
  14045. ' };',
  14046. ' this.GetA = function () {',
  14047. ' var Result = 0;',
  14048. ' return Result;',
  14049. ' };',
  14050. ' this.SetA = function (Value) {',
  14051. ' };',
  14052. '});',
  14053. 'this.b = false;',
  14054. 'this.Obj = null;',
  14055. 'this.Cla = null;'
  14056. ]),
  14057. LinesToStr([ // $mod.$main
  14058. '$mod.TObject.FA = $mod.Obj.FA;',
  14059. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  14060. '$mod.b = $mod.Obj.FA === 4;',
  14061. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  14062. '$mod.b = 5 === $mod.Obj.FA;',
  14063. '$mod.TObject.FA = 6;',
  14064. '$mod.TObject.FA = $mod.Cla.FA;',
  14065. '$mod.Cla.SetA($mod.Cla.GetA());',
  14066. '$mod.b = $mod.Cla.FA === 7;',
  14067. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  14068. '$mod.b = 8 === $mod.Cla.FA;',
  14069. '$mod.TObject.FA = 9;',
  14070. '$mod.TObject.SetA($mod.TObject.GetA());',
  14071. '$mod.b = $mod.TObject.FA === 10;',
  14072. '$mod.b = 11 === $mod.TObject.FA;',
  14073. '']));
  14074. end;
  14075. procedure TTestModule.TestClassOf_ClassMethodSelf;
  14076. begin
  14077. StartProgram(false);
  14078. Add('type');
  14079. Add(' TObject = class');
  14080. Add(' class var GlobalId: longint;');
  14081. Add(' class procedure ProcA;');
  14082. Add(' end;');
  14083. Add('class procedure tobject.proca;');
  14084. Add('var b: boolean;');
  14085. Add('begin');
  14086. Add(' b:=self=nil;');
  14087. Add(' b:=self.globalid=3;');
  14088. Add(' b:=4=self.globalid;');
  14089. Add(' self.globalid:=5;');
  14090. Add(' self.proca;');
  14091. Add('end;');
  14092. Add('begin');
  14093. ConvertProgram;
  14094. CheckSource('TestClassOf_ClassMethodSelf',
  14095. LinesToStr([ // statements
  14096. 'rtl.createClass($mod, "TObject", null, function () {',
  14097. ' this.GlobalId = 0;',
  14098. ' this.$init = function () {',
  14099. ' };',
  14100. ' this.$final = function () {',
  14101. ' };',
  14102. ' this.ProcA = function () {',
  14103. ' var b = false;',
  14104. ' b = this === null;',
  14105. ' b = this.GlobalId === 3;',
  14106. ' b = 4 === this.GlobalId;',
  14107. ' $mod.TObject.GlobalId = 5;',
  14108. ' this.ProcA();',
  14109. ' };',
  14110. '});'
  14111. ]),
  14112. LinesToStr([ // $mod.$main
  14113. '']));
  14114. end;
  14115. procedure TTestModule.TestClassOf_TypeCast;
  14116. begin
  14117. StartProgram(false);
  14118. Add('type');
  14119. Add(' TObject = class');
  14120. Add(' class procedure {#TObject_DoIt}DoIt;');
  14121. Add(' end;');
  14122. Add(' TClass = class of TObject;');
  14123. Add(' TMobile = class');
  14124. Add(' class procedure {#TMobile_DoIt}DoIt;');
  14125. Add(' end;');
  14126. Add(' TMobileClass = class of TMobile;');
  14127. Add(' TCar = class(TMobile)');
  14128. Add(' class procedure {#TCar_DoIt}DoIt;');
  14129. Add(' end;');
  14130. Add(' TCarClass = class of TCar;');
  14131. Add('class procedure TObject.DoIt;');
  14132. Add('begin');
  14133. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14134. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14135. Add('end;');
  14136. Add('class procedure TMobile.DoIt;');
  14137. Add('begin');
  14138. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14139. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14140. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  14141. Add('end;');
  14142. Add('class procedure TCar.DoIt; begin end;');
  14143. Add('var');
  14144. Add(' ObjC: TClass;');
  14145. Add(' MobileC: TMobileClass;');
  14146. Add(' CarC: TCarClass;');
  14147. Add('begin');
  14148. Add(' ObjC.{@TObject_DoIt}DoIt;');
  14149. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  14150. Add(' CarC.{@TCar_DoIt}DoIt;');
  14151. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  14152. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  14153. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  14154. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  14155. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  14156. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  14157. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  14158. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  14159. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  14160. ConvertProgram;
  14161. CheckSource('TestClassOf_TypeCast',
  14162. LinesToStr([ // statements
  14163. 'rtl.createClass($mod, "TObject", null, function () {',
  14164. ' this.$init = function () {',
  14165. ' };',
  14166. ' this.$final = function () {',
  14167. ' };',
  14168. ' this.DoIt = function () {',
  14169. ' this.DoIt();',
  14170. ' this.DoIt$1();',
  14171. ' };',
  14172. '});',
  14173. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  14174. ' this.DoIt$1 = function () {',
  14175. ' this.DoIt();',
  14176. ' this.DoIt$1();',
  14177. ' this.DoIt$2();',
  14178. ' };',
  14179. '});',
  14180. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  14181. ' this.DoIt$2 = function () {',
  14182. ' };',
  14183. '});',
  14184. 'this.ObjC = null;',
  14185. 'this.MobileC = null;',
  14186. 'this.CarC = null;',
  14187. '']),
  14188. LinesToStr([ // $mod.$main
  14189. '$mod.ObjC.DoIt();',
  14190. '$mod.MobileC.DoIt$1();',
  14191. '$mod.CarC.DoIt$2();',
  14192. '$mod.ObjC.DoIt();',
  14193. '$mod.ObjC.DoIt$1();',
  14194. '$mod.ObjC.DoIt$2();',
  14195. '$mod.MobileC.DoIt();',
  14196. '$mod.MobileC.DoIt$1();',
  14197. '$mod.MobileC.DoIt$2();',
  14198. '$mod.CarC.DoIt();',
  14199. '$mod.CarC.DoIt$1();',
  14200. '$mod.CarC.DoIt$2();',
  14201. '']));
  14202. end;
  14203. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  14204. begin
  14205. StartProgram(false);
  14206. Add('type');
  14207. Add(' TObject = class');
  14208. Add(' function CurNow: longint; ');
  14209. Add(' class function Now: longint; ');
  14210. Add(' end;');
  14211. Add('function TObject.CurNow: longint; begin end;');
  14212. Add('class function TObject.Now: longint; begin end;');
  14213. Add('var');
  14214. Add(' Obj: tobject;');
  14215. Add(' vI: longint;');
  14216. Add('begin');
  14217. Add(' obj.curnow;');
  14218. Add(' vi:=obj.curnow;');
  14219. Add(' tobject.now;');
  14220. Add(' vi:=tobject.now;');
  14221. ConvertProgram;
  14222. CheckSource('TestClassOf_ImplicitFunctionCall',
  14223. LinesToStr([ // statements
  14224. 'rtl.createClass($mod, "TObject", null, function () {',
  14225. ' this.$init = function () {',
  14226. ' };',
  14227. ' this.$final = function () {',
  14228. ' };',
  14229. ' this.CurNow = function () {',
  14230. ' var Result = 0;',
  14231. ' return Result;',
  14232. ' };',
  14233. ' this.Now = function () {',
  14234. ' var Result = 0;',
  14235. ' return Result;',
  14236. ' };',
  14237. '});',
  14238. 'this.Obj = null;',
  14239. 'this.vI = 0;',
  14240. '']),
  14241. LinesToStr([ // $mod.$main
  14242. '$mod.Obj.CurNow();',
  14243. '$mod.vI = $mod.Obj.CurNow();',
  14244. '$mod.TObject.Now();',
  14245. '$mod.vI = $mod.TObject.Now();',
  14246. '']));
  14247. end;
  14248. procedure TTestModule.TestClassOf_Const;
  14249. begin
  14250. StartProgram(false);
  14251. Add([
  14252. 'type',
  14253. ' TObject = class',
  14254. ' end;',
  14255. ' TBird = TObject;',
  14256. ' TBirds = class of TBird;',
  14257. ' TEagles = TBirds;',
  14258. ' THawk = class(TBird);',
  14259. 'const',
  14260. ' Hawk: TEagles = THawk;',
  14261. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  14262. ' TBird,',
  14263. ' THawk',
  14264. ' );',
  14265. 'begin']);
  14266. ConvertProgram;
  14267. CheckSource('TestClassOf_Const',
  14268. LinesToStr([ // statements
  14269. 'rtl.createClass($mod, "TObject", null, function () {',
  14270. ' this.$init = function () {',
  14271. ' };',
  14272. ' this.$final = function () {',
  14273. ' };',
  14274. '});',
  14275. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  14276. '});',
  14277. 'this.Hawk = $mod.THawk;',
  14278. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  14279. '']),
  14280. LinesToStr([ // $mod.$main
  14281. '']));
  14282. end;
  14283. procedure TTestModule.TestNestedClass_Alias;
  14284. begin
  14285. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14286. StartProgram(false);
  14287. Add([
  14288. 'type',
  14289. ' TObject = class',
  14290. ' type TNested = type longint;',
  14291. ' end;',
  14292. 'type TAlias = type tobject.tnested;',
  14293. 'var i: tobject.tnested = 3;',
  14294. 'var j: TAlias = 4;',
  14295. 'begin',
  14296. ' if typeinfo(TAlias)=nil then ;',
  14297. ' if typeinfo(tobject.tnested)=nil then ;',
  14298. '']);
  14299. ConvertProgram;
  14300. CheckSource('TestNestedClass_Alias',
  14301. LinesToStr([ // statements
  14302. 'rtl.createClass($mod, "TObject", null, function () {',
  14303. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  14304. ' this.$init = function () {',
  14305. ' };',
  14306. ' this.$final = function () {',
  14307. ' };',
  14308. '});',
  14309. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  14310. 'this.i = 3;',
  14311. 'this.j = 4;',
  14312. '']),
  14313. LinesToStr([ // $mod.$main
  14314. 'if ($mod.$rtti["TAlias"] === null) ;',
  14315. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  14316. '']));
  14317. end;
  14318. procedure TTestModule.TestNestedClass_Record;
  14319. begin
  14320. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14321. StartProgram(false);
  14322. Add([
  14323. 'type',
  14324. ' TObject = class',
  14325. ' type TPoint = record',
  14326. ' x,y: byte;',
  14327. ' end;',
  14328. ' procedure DoIt(t: TPoint);',
  14329. ' end;',
  14330. 'procedure tobject.DoIt(t: TPoint);',
  14331. 'var p: TPoint;',
  14332. 'begin',
  14333. ' t.x:=t.y;',
  14334. ' p:=t;',
  14335. 'end;',
  14336. 'var',
  14337. ' p: tobject.tpoint = (x:2; y:4);',
  14338. ' o: TObject;',
  14339. 'begin',
  14340. ' p:=p;',
  14341. ' o.doit(p);',
  14342. '']);
  14343. ConvertProgram;
  14344. CheckSource('TestNestedClass_Record',
  14345. LinesToStr([ // statements
  14346. 'rtl.createClass($mod, "TObject", null, function () {',
  14347. ' rtl.recNewT(this, "TPoint", function () {',
  14348. ' this.x = 0;',
  14349. ' this.y = 0;',
  14350. ' this.$eq = function (b) {',
  14351. ' return (this.x === b.x) && (this.y === b.y);',
  14352. ' };',
  14353. ' this.$assign = function (s) {',
  14354. ' this.x = s.x;',
  14355. ' this.y = s.y;',
  14356. ' return this;',
  14357. ' };',
  14358. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  14359. ' $r.addField("x", rtl.byte);',
  14360. ' $r.addField("y", rtl.byte);',
  14361. ' });',
  14362. ' this.$init = function () {',
  14363. ' };',
  14364. ' this.$final = function () {',
  14365. ' };',
  14366. ' this.DoIt = function (t) {',
  14367. ' var p = this.TPoint.$new();',
  14368. ' t.x = t.y;',
  14369. ' p.$assign(t);',
  14370. ' };',
  14371. '});',
  14372. 'this.p = $mod.TObject.TPoint.$clone({',
  14373. ' x: 2,',
  14374. ' y: 4',
  14375. '});',
  14376. 'this.o = null;',
  14377. '']),
  14378. LinesToStr([ // $mod.$main
  14379. '$mod.p.$assign($mod.p);',
  14380. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  14381. '']));
  14382. end;
  14383. procedure TTestModule.TestNestedClass_Class;
  14384. begin
  14385. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14386. StartProgram(false);
  14387. Add([
  14388. 'type',
  14389. ' TObject = class end;',
  14390. ' TBird = class',
  14391. ' type TLeg = class',
  14392. ' FId: longint;',
  14393. ' constructor Create;',
  14394. ' function Create(i: longint): TLeg;',
  14395. ' end;',
  14396. ' function DoIt(b: TBird): Tleg;',
  14397. ' end;',
  14398. 'constructor tbird.tleg.create;',
  14399. 'begin',
  14400. ' FId:=3;',
  14401. 'end;',
  14402. 'function tbird.tleg.Create(i: longint): TLeg;',
  14403. 'begin',
  14404. ' Create;',
  14405. ' Result:=TLeg.Create;',
  14406. ' Result:=TBird.TLeg.Create;',
  14407. ' Result:=Create(3);',
  14408. ' FId:=i;',
  14409. 'end;',
  14410. 'function tbird.DoIt(b: tbird): tleg;',
  14411. 'begin',
  14412. ' Result.Create;',
  14413. ' Result:=TLeg.Create;',
  14414. ' Result:=TBird.TLeg.Create;',
  14415. ' Result:=Result.Create(3);',
  14416. 'end;',
  14417. 'var',
  14418. ' b: Tbird.tleg;',
  14419. 'begin',
  14420. ' b.Create;',
  14421. ' b:=TBird.TLeg.Create;',
  14422. ' b:=b.Create(3);',
  14423. '']);
  14424. ConvertProgram;
  14425. CheckSource('TestNestedClass_Class',
  14426. LinesToStr([ // statements
  14427. 'rtl.createClass($mod, "TObject", null, function () {',
  14428. ' this.$init = function () {',
  14429. ' };',
  14430. ' this.$final = function () {',
  14431. ' };',
  14432. '});',
  14433. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14434. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  14435. ' this.$init = function () {',
  14436. ' $mod.TObject.$init.call(this);',
  14437. ' this.FId = 0;',
  14438. ' };',
  14439. ' this.Create = function () {',
  14440. ' this.FId = 3;',
  14441. ' return this;',
  14442. ' };',
  14443. ' this.Create$1 = function (i) {',
  14444. ' var Result = null;',
  14445. ' this.Create();',
  14446. ' Result = $mod.TBird.TLeg.$create("Create");',
  14447. ' Result = $mod.TBird.TLeg.$create("Create");',
  14448. ' Result = this.Create$1(3);',
  14449. ' this.FId = i;',
  14450. ' return Result;',
  14451. ' };',
  14452. ' });',
  14453. ' this.DoIt = function (b) {',
  14454. ' var Result = null;',
  14455. ' Result.Create();',
  14456. ' Result = this.TLeg.$create("Create");',
  14457. ' Result = $mod.TBird.TLeg.$create("Create");',
  14458. ' Result = Result.Create$1(3);',
  14459. ' return Result;',
  14460. ' };',
  14461. '});',
  14462. 'this.b = null;',
  14463. '']),
  14464. LinesToStr([ // $mod.$main
  14465. '$mod.b.Create();',
  14466. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  14467. '$mod.b = $mod.b.Create$1(3);',
  14468. '']));
  14469. end;
  14470. procedure TTestModule.TestExternalClass_Var;
  14471. begin
  14472. StartProgram(false);
  14473. Add([
  14474. '{$modeswitch externalclass}',
  14475. 'type',
  14476. ' TExtA = class external name ''ExtObj''',
  14477. ' Id: longint external name ''$Id'';',
  14478. ' B: longint;',
  14479. ' end;',
  14480. 'var Obj: TExtA;',
  14481. 'begin',
  14482. ' obj.id:=obj.id+1;',
  14483. ' obj.B:=obj.B+1;']);
  14484. ConvertProgram;
  14485. CheckSource('TestExternalClass_Var',
  14486. LinesToStr([ // statements
  14487. 'this.Obj = null;',
  14488. '']),
  14489. LinesToStr([ // $mod.$main
  14490. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  14491. '$mod.Obj.B = $mod.Obj.B + 1;',
  14492. '']));
  14493. end;
  14494. procedure TTestModule.TestExternalClass_Const;
  14495. begin
  14496. StartProgram(false);
  14497. Add([
  14498. '{$modeswitch externalclass}',
  14499. 'type',
  14500. ' TExtA = class external name ''ExtObj''',
  14501. ' const Two: longint = 2;',
  14502. ' const Three = 3;',
  14503. ' const Id: longint;',
  14504. ' end;',
  14505. ' TExtB = class external name ''ExtB''',
  14506. ' A: TExtA;',
  14507. ' end;',
  14508. 'var',
  14509. ' A: texta;',
  14510. ' B: textb;',
  14511. ' i: longint;',
  14512. 'begin',
  14513. ' i:=a.two;',
  14514. ' i:=texta.two;',
  14515. ' i:=a.three;',
  14516. ' i:=texta.three;',
  14517. ' i:=a.id;',
  14518. ' i:=texta.id;',
  14519. '']);
  14520. ConvertProgram;
  14521. CheckSource('TestExternalClass_Const',
  14522. LinesToStr([ // statements
  14523. 'this.A = null;',
  14524. 'this.B = null;',
  14525. 'this.i = 0;',
  14526. '']),
  14527. LinesToStr([ // $mod.$main
  14528. '$mod.i = 2;',
  14529. '$mod.i = 2;',
  14530. '$mod.i = 3;',
  14531. '$mod.i = 3;',
  14532. '$mod.i = $mod.A.Id;',
  14533. '$mod.i = ExtObj.Id;',
  14534. '']));
  14535. end;
  14536. procedure TTestModule.TestExternalClass_Dollar;
  14537. begin
  14538. StartProgram(false);
  14539. Add([
  14540. '{$modeswitch externalclass}',
  14541. 'type',
  14542. ' TExtA = class external name ''$''',
  14543. ' Id: longint external name ''$'';',
  14544. ' function Bla(i: longint): longint; external name ''$'';',
  14545. ' end;',
  14546. 'function dollar(k: longint): longint; external name ''$'';',
  14547. 'var Obj: TExtA;',
  14548. 'begin',
  14549. ' dollar(1);',
  14550. ' obj.id:=obj.id+2;',
  14551. ' obj.Bla(3);',
  14552. '']);
  14553. ConvertProgram;
  14554. CheckSource('TestExternalClass_Dollar',
  14555. LinesToStr([ // statements
  14556. 'this.Obj = null;',
  14557. '']),
  14558. LinesToStr([ // $mod.$main
  14559. '$(1);',
  14560. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  14561. '$mod.Obj.$(3);',
  14562. '']));
  14563. end;
  14564. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  14565. begin
  14566. StartProgram(false);
  14567. Add('{$modeswitch externalclass}');
  14568. Add('type');
  14569. Add(' TExtA = class external name ''ExtA''');
  14570. Add(' Id: longint external name ''$Id'';');
  14571. Add(' end;');
  14572. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  14573. Add(' Id: longint;');
  14574. Add(' end;');
  14575. Add('begin');
  14576. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  14577. ConvertProgram;
  14578. end;
  14579. procedure TTestModule.TestExternalClass_Method;
  14580. begin
  14581. StartProgram(false);
  14582. Add('{$modeswitch externalclass}');
  14583. Add('type');
  14584. Add(' TExtA = class external name ''ExtObj''');
  14585. Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
  14586. Add(' procedure DoSome(Id: longint = 1);');
  14587. Add(' end;');
  14588. Add('var Obj: texta;');
  14589. Add('begin');
  14590. Add(' obj.doit;');
  14591. Add(' obj.doit();');
  14592. Add(' obj.doit(2);');
  14593. Add(' with obj do begin');
  14594. Add(' doit;');
  14595. Add(' doit();');
  14596. Add(' doit(3);');
  14597. Add(' end;');
  14598. ConvertProgram;
  14599. CheckSource('TestExternalClass_Method',
  14600. LinesToStr([ // statements
  14601. 'this.Obj = null;',
  14602. '']),
  14603. LinesToStr([ // $mod.$main
  14604. '$mod.Obj.$Execute(1);',
  14605. '$mod.Obj.$Execute(1);',
  14606. '$mod.Obj.$Execute(2);',
  14607. 'var $with1 = $mod.Obj;',
  14608. '$with1.$Execute(1);',
  14609. '$with1.$Execute(1);',
  14610. '$with1.$Execute(3);',
  14611. '']));
  14612. end;
  14613. procedure TTestModule.TestExternalClass_ClassMethod;
  14614. begin
  14615. StartProgram(false);
  14616. Add([
  14617. '{$modeswitch externalclass}',
  14618. 'type',
  14619. ' TExtA = class external name ''ExtObj''',
  14620. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  14621. ' end;',
  14622. ' TExtB = TExtA;',
  14623. 'begin',
  14624. ' texta.doit;',
  14625. ' texta.doit();',
  14626. ' texta.doit(2);',
  14627. ' with texta do begin',
  14628. ' doit;',
  14629. ' doit();',
  14630. ' doit(3);',
  14631. ' end;',
  14632. ' textb.doit;',
  14633. ' textb.doit();',
  14634. ' textb.doit(4);',
  14635. ' with textb do begin',
  14636. ' doit;',
  14637. ' doit();',
  14638. ' doit(5);',
  14639. ' end;',
  14640. '']);
  14641. ConvertProgram;
  14642. CheckSource('TestExternalClass_ClassMethod',
  14643. LinesToStr([ // statements
  14644. '']),
  14645. LinesToStr([ // $mod.$main
  14646. 'ExtObj.$Execute(1);',
  14647. 'ExtObj.$Execute(1);',
  14648. 'ExtObj.$Execute(2);',
  14649. 'ExtObj.$Execute(1);',
  14650. 'ExtObj.$Execute(1);',
  14651. 'ExtObj.$Execute(3);',
  14652. 'ExtObj.$Execute(1);',
  14653. 'ExtObj.$Execute(1);',
  14654. 'ExtObj.$Execute(4);',
  14655. 'ExtObj.$Execute(1);',
  14656. 'ExtObj.$Execute(1);',
  14657. 'ExtObj.$Execute(5);',
  14658. '']));
  14659. end;
  14660. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  14661. begin
  14662. StartProgram(false);
  14663. Add([
  14664. '{$modeswitch externalclass}',
  14665. 'type',
  14666. ' TBird = class external name ''Array''',
  14667. ' end;',
  14668. 'function GetPtr: Pointer;',
  14669. 'begin',
  14670. 'end;',
  14671. 'procedure Write(const p);',
  14672. 'begin',
  14673. 'end;',
  14674. 'procedure WriteLn; varargs;',
  14675. 'begin',
  14676. 'end;',
  14677. 'begin',
  14678. ' if TBird(GetPtr)=nil then ;',
  14679. ' Write(GetPtr);',
  14680. ' WriteLn(GetPtr);',
  14681. ' Write(TBird(GetPtr));',
  14682. ' WriteLn(TBird(GetPtr));',
  14683. '']);
  14684. ConvertProgram;
  14685. CheckSource('TestFunctionResultInTypeCast',
  14686. LinesToStr([ // statements
  14687. 'this.GetPtr = function () {',
  14688. ' var Result = null;',
  14689. ' return Result;',
  14690. '};',
  14691. 'this.Write = function (p) {',
  14692. '};',
  14693. 'this.WriteLn = function () {',
  14694. '};',
  14695. '']),
  14696. LinesToStr([
  14697. 'if ($mod.GetPtr() === null) ;',
  14698. '$mod.Write($mod.GetPtr());',
  14699. '$mod.WriteLn($mod.GetPtr());',
  14700. '$mod.Write($mod.GetPtr());',
  14701. '$mod.WriteLn($mod.GetPtr());',
  14702. '']));
  14703. end;
  14704. procedure TTestModule.TestExternalClass_NonExternalOverride;
  14705. begin
  14706. StartProgram(false);
  14707. Add('{$modeswitch externalclass}');
  14708. Add('type');
  14709. Add(' TExtA = class external name ''ExtObjA''');
  14710. Add(' procedure ProcA; virtual;');
  14711. Add(' procedure ProcB; virtual;');
  14712. Add(' end;');
  14713. Add(' TExtB = class external name ''ExtObjB'' (TExtA)');
  14714. Add(' end;');
  14715. Add(' TExtC = class (TExtB)');
  14716. Add(' procedure ProcA; override;');
  14717. Add(' end;');
  14718. Add('procedure TExtC.ProcA;');
  14719. Add('begin');
  14720. Add(' ProcA;');
  14721. Add(' Self.ProcA;');
  14722. Add(' ProcB;');
  14723. Add(' Self.ProcB;');
  14724. Add('end;');
  14725. Add('var');
  14726. Add(' A: texta;');
  14727. Add(' B: textb;');
  14728. Add(' C: textc;');
  14729. Add('begin');
  14730. Add(' a.proca;');
  14731. Add(' b.proca;');
  14732. Add(' c.proca;');
  14733. ConvertProgram;
  14734. CheckSource('TestExternalClass_NonExternalOverride',
  14735. LinesToStr([ // statements
  14736. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  14737. ' this.$init = function () {',
  14738. ' };',
  14739. ' this.$final = function () {',
  14740. ' };',
  14741. ' this.ProcA = function () {',
  14742. ' this.ProcA();',
  14743. ' this.ProcA();',
  14744. ' this.ProcB();',
  14745. ' this.ProcB();',
  14746. ' };',
  14747. '});',
  14748. 'this.A = null;',
  14749. 'this.B = null;',
  14750. 'this.C = null;',
  14751. '']),
  14752. LinesToStr([ // $mod.$main
  14753. '$mod.A.ProcA();',
  14754. '$mod.B.ProcA();',
  14755. '$mod.C.ProcA();',
  14756. '']));
  14757. end;
  14758. procedure TTestModule.TestExternalClass_OverloadHint;
  14759. begin
  14760. StartProgram(false);
  14761. Add([
  14762. '{$modeswitch externalclass}',
  14763. 'type',
  14764. ' TExtA = class external name ''ExtObjA''',
  14765. ' procedure DoIt;',
  14766. ' procedure DoIt(i: longint);',
  14767. ' end;',
  14768. 'begin',
  14769. '']);
  14770. ConvertProgram;
  14771. CheckResolverUnexpectedHints(true);
  14772. CheckSource('TestExternalClass_OverloadHint',
  14773. LinesToStr([ // statements
  14774. '']),
  14775. LinesToStr([ // $mod.$main
  14776. '']));
  14777. end;
  14778. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  14779. begin
  14780. StartProgram(false);
  14781. Add([
  14782. '{$modeswitch externalclass}',
  14783. 'type',
  14784. ' JSwiper = class external name ''Swiper''',
  14785. ' constructor New;',
  14786. ' end;',
  14787. ' TObject = class',
  14788. ' private',
  14789. ' FSwiper: JSwiper;',
  14790. ' published',
  14791. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  14792. ' end;',
  14793. 'begin',
  14794. ' JSwiper.new;',
  14795. '']);
  14796. ConvertProgram;
  14797. CheckSource('TestExternalClass_SameNamePublishedProperty',
  14798. LinesToStr([ // statements
  14799. 'rtl.createClass($mod, "TObject", null, function () {',
  14800. ' this.$init = function () {',
  14801. ' this.FSwiper = null;',
  14802. ' };',
  14803. ' this.$final = function () {',
  14804. ' this.FSwiper = undefined;',
  14805. ' };',
  14806. ' var $r = this.$rtti;',
  14807. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  14808. '});',
  14809. '']),
  14810. LinesToStr([ // $mod.$main
  14811. 'new Swiper();',
  14812. '']));
  14813. end;
  14814. procedure TTestModule.TestExternalClass_Property;
  14815. begin
  14816. StartProgram(false);
  14817. Add([
  14818. '{$modeswitch externalclass}',
  14819. 'type',
  14820. ' TExtA = class external name ''ExtA''',
  14821. ' function getYear: longint;',
  14822. ' procedure setYear(Value: longint);',
  14823. ' property Year: longint read getyear write setyear;',
  14824. ' end;',
  14825. ' TExtB = class (TExtA)',
  14826. ' procedure OtherSetYear(Value: longint);',
  14827. ' property year write othersetyear;',
  14828. ' end;',
  14829. 'procedure textb.othersetyear(value: longint);',
  14830. 'begin',
  14831. ' setYear(Value+4);',
  14832. 'end;',
  14833. 'var',
  14834. ' A: texta;',
  14835. ' B: textb;',
  14836. 'begin',
  14837. ' a.year:=a.year+1;',
  14838. ' b.year:=b.year+2;']);
  14839. ConvertProgram;
  14840. CheckSource('TestExternalClass_NonExternalOverride',
  14841. LinesToStr([ // statements
  14842. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  14843. ' this.$init = function () {',
  14844. ' };',
  14845. ' this.$final = function () {',
  14846. ' };',
  14847. ' this.OtherSetYear = function (Value) {',
  14848. ' this.setYear(Value+4);',
  14849. ' };',
  14850. '});',
  14851. 'this.A = null;',
  14852. 'this.B = null;',
  14853. '']),
  14854. LinesToStr([ // $mod.$main
  14855. '$mod.A.setYear($mod.A.getYear()+1);',
  14856. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  14857. '']));
  14858. end;
  14859. procedure TTestModule.TestExternalClass_PropertyDate;
  14860. begin
  14861. StartProgram(false);
  14862. Add([
  14863. '{$modeswitch externalclass}',
  14864. 'type',
  14865. ' TExtA = class external name ''ExtA''',
  14866. ' end;',
  14867. ' TExtB = class (TExtA)',
  14868. ' FDate: string;',
  14869. ' property Date: string read FDate write FDate;',
  14870. ' property ExtA: string read FDate write FDate;',
  14871. ' end;',
  14872. ' {$M+}',
  14873. ' TObject = class',
  14874. ' FDate: string;',
  14875. ' published',
  14876. ' property Date: string read FDate write FDate;',
  14877. ' property ExtA: string read FDate write FDate;',
  14878. ' end;',
  14879. 'var',
  14880. ' B: textb;',
  14881. ' o: TObject;',
  14882. 'begin',
  14883. ' b.date:=b.exta;',
  14884. ' o.date:=o.exta;']);
  14885. ConvertProgram;
  14886. CheckSource('TestExternalClass_PropertyDate',
  14887. LinesToStr([ // statements
  14888. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  14889. ' this.$init = function () {',
  14890. ' this.FDate = "";',
  14891. ' };',
  14892. ' this.$final = function () {',
  14893. ' };',
  14894. '});',
  14895. 'rtl.createClass($mod, "TObject", null, function () {',
  14896. ' this.$init = function () {',
  14897. ' this.FDate = "";',
  14898. ' };',
  14899. ' this.$final = function () {',
  14900. ' };',
  14901. ' var $r = this.$rtti;',
  14902. ' $r.addField("FDate", rtl.string);',
  14903. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  14904. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  14905. '});',
  14906. 'this.B = null;',
  14907. 'this.o = null;',
  14908. '']),
  14909. LinesToStr([ // $mod.$main
  14910. '$mod.B.FDate = $mod.B.FDate;',
  14911. '$mod.o.FDate = $mod.o.FDate;',
  14912. '']));
  14913. end;
  14914. procedure TTestModule.TestExternalClass_ClassProperty;
  14915. begin
  14916. StartProgram(false);
  14917. Add('{$modeswitch externalclass}');
  14918. Add('type');
  14919. Add(' TExtA = class external name ''ExtA''');
  14920. Add(' class function getYear: longint;');
  14921. Add(' class procedure setYear(Value: longint);');
  14922. Add(' class property Year: longint read getyear write setyear;');
  14923. Add(' end;');
  14924. Add(' TExtB = class (TExtA)');
  14925. Add(' class function GetCentury: longint;');
  14926. Add(' class procedure SetCentury(Value: longint);');
  14927. Add(' class property Century: longint read getcentury write setcentury;');
  14928. Add(' end;');
  14929. Add('class function textb.getcentury: longint;');
  14930. Add('begin');
  14931. Add('end;');
  14932. Add('class procedure textb.setcentury(value: longint);');
  14933. Add('begin');
  14934. Add(' setyear(value+11);');
  14935. Add(' texta.year:=texta.year+12;');
  14936. Add(' year:=year+13;');
  14937. Add(' textb.century:=textb.century+14;');
  14938. Add(' century:=century+15;');
  14939. Add('end;');
  14940. Add('var');
  14941. Add(' A: texta;');
  14942. Add(' B: textb;');
  14943. Add('begin');
  14944. Add(' texta.year:=texta.year+1;');
  14945. Add(' textb.year:=textb.year+2;');
  14946. Add(' TextA.year:=TextA.year+3;');
  14947. Add(' b.year:=b.year+4;');
  14948. Add(' textb.century:=textb.century+5;');
  14949. Add(' b.century:=b.century+6;');
  14950. ConvertProgram;
  14951. CheckSource('TestExternalClass_ClassProperty',
  14952. LinesToStr([ // statements
  14953. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  14954. ' this.$init = function () {',
  14955. ' };',
  14956. ' this.$final = function () {',
  14957. ' };',
  14958. ' this.GetCentury = function () {',
  14959. ' var Result = 0;',
  14960. ' return Result;',
  14961. ' };',
  14962. ' this.SetCentury = function (Value) {',
  14963. ' this.setYear(Value + 11);',
  14964. ' ExtA.setYear(ExtA.getYear() + 12);',
  14965. ' this.setYear(this.getYear() + 13);',
  14966. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  14967. ' this.SetCentury(this.GetCentury() + 15);',
  14968. ' };',
  14969. '});',
  14970. 'this.A = null;',
  14971. 'this.B = null;',
  14972. '']),
  14973. LinesToStr([ // $mod.$main
  14974. 'ExtA.setYear(ExtA.getYear() + 1);',
  14975. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  14976. 'ExtA.setYear(ExtA.getYear() + 3);',
  14977. '$mod.B.setYear($mod.B.getYear() + 4);',
  14978. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  14979. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  14980. '']));
  14981. end;
  14982. procedure TTestModule.TestExternalClass_ClassOf;
  14983. begin
  14984. StartProgram(false);
  14985. Add('{$modeswitch externalclass}');
  14986. Add('type');
  14987. Add(' TExtA = class external name ''ExtA''');
  14988. Add(' procedure ProcA; virtual;');
  14989. Add(' procedure ProcB; virtual;');
  14990. Add(' end;');
  14991. Add(' TExtAClass = class of TExtA;');
  14992. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  14993. Add(' end;');
  14994. Add(' TExtBClass = class of TExtB;');
  14995. Add(' TExtC = class (TExtB)');
  14996. Add(' procedure ProcA; override;');
  14997. Add(' end;');
  14998. Add(' TExtCClass = class of TExtC;');
  14999. Add('procedure TExtC.ProcA; begin end;');
  15000. Add('var');
  15001. Add(' A: texta; ClA: TExtAClass;');
  15002. Add(' B: textb; ClB: TExtBClass;');
  15003. Add(' C: textc; ClC: TExtCClass;');
  15004. Add('begin');
  15005. Add(' ClA:=texta;');
  15006. Add(' ClA:=textb;');
  15007. Add(' ClA:=textc;');
  15008. Add(' ClB:=textb;');
  15009. Add(' ClB:=textc;');
  15010. Add(' ClC:=textc;');
  15011. ConvertProgram;
  15012. CheckSource('TestExternalClass_ClassOf',
  15013. LinesToStr([ // statements
  15014. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15015. ' this.$init = function () {',
  15016. ' };',
  15017. ' this.$final = function () {',
  15018. ' };',
  15019. ' this.ProcA = function () {',
  15020. ' };',
  15021. '});',
  15022. 'this.A = null;',
  15023. 'this.ClA = null;',
  15024. 'this.B = null;',
  15025. 'this.ClB = null;',
  15026. 'this.C = null;',
  15027. 'this.ClC = null;',
  15028. '']),
  15029. LinesToStr([ // $mod.$main
  15030. '$mod.ClA = ExtA;',
  15031. '$mod.ClA = ExtB;',
  15032. '$mod.ClA = $mod.TExtC;',
  15033. '$mod.ClB = ExtB;',
  15034. '$mod.ClB = $mod.TExtC;',
  15035. '$mod.ClC = $mod.TExtC;',
  15036. '']));
  15037. end;
  15038. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  15039. begin
  15040. AddModuleWithIntfImplSrc('unit2.pas',
  15041. LinesToStr([
  15042. '{$modeswitch externalclass}',
  15043. 'type',
  15044. ' TExtA = class external name ''ExtA''',
  15045. ' class var Id: longint;',
  15046. ' end;',
  15047. '']),
  15048. '');
  15049. StartUnit(true);
  15050. Add('interface');
  15051. Add('uses unit2;');
  15052. Add('implementation');
  15053. Add('begin');
  15054. Add(' unit2.texta.id:=unit2.texta.id+1;');
  15055. ConvertUnit;
  15056. CheckSource('TestExternalClass_ClassOtherUnit',
  15057. LinesToStr([
  15058. '']),
  15059. LinesToStr([
  15060. 'ExtA.Id = ExtA.Id + 1;',
  15061. '']));
  15062. end;
  15063. procedure TTestModule.TestExternalClass_Is;
  15064. begin
  15065. StartProgram(false);
  15066. Add('{$modeswitch externalclass}');
  15067. Add('type');
  15068. Add(' TExtA = class external name ''ExtA''');
  15069. Add(' end;');
  15070. Add(' TExtAClass = class of TExtA;');
  15071. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15072. Add(' end;');
  15073. Add(' TExtBClass = class of TExtB;');
  15074. Add(' TExtC = class (TExtB)');
  15075. Add(' end;');
  15076. Add(' TExtCClass = class of TExtC;');
  15077. Add('var');
  15078. Add(' A: texta; ClA: TExtAClass;');
  15079. Add(' B: textb; ClB: TExtBClass;');
  15080. Add(' C: textc; ClC: TExtCClass;');
  15081. Add('begin');
  15082. Add(' if a is textb then ;');
  15083. Add(' if a is textc then ;');
  15084. Add(' if b is textc then ;');
  15085. Add(' if cla is textb then ;');
  15086. Add(' if cla is textc then ;');
  15087. Add(' if clb is textc then ;');
  15088. ConvertProgram;
  15089. CheckSource('TestExternalClass_Is',
  15090. LinesToStr([ // statements
  15091. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15092. ' this.$init = function () {',
  15093. ' };',
  15094. ' this.$final = function () {',
  15095. ' };',
  15096. '});',
  15097. 'this.A = null;',
  15098. 'this.ClA = null;',
  15099. 'this.B = null;',
  15100. 'this.ClB = null;',
  15101. 'this.C = null;',
  15102. 'this.ClC = null;',
  15103. '']),
  15104. LinesToStr([ // $mod.$main
  15105. 'if (rtl.isExt($mod.A, ExtB)) ;',
  15106. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  15107. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  15108. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  15109. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  15110. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  15111. '']));
  15112. end;
  15113. procedure TTestModule.TestExternalClass_As;
  15114. begin
  15115. StartProgram(false);
  15116. Add('{$modeswitch externalclass}');
  15117. Add('type');
  15118. Add(' TExtA = class external name ''ExtA''');
  15119. Add(' end;');
  15120. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15121. Add(' end;');
  15122. Add(' TExtC = class (TExtB)');
  15123. Add(' end;');
  15124. Add('var');
  15125. Add(' A: texta;');
  15126. Add(' B: textb;');
  15127. Add(' C: textc;');
  15128. Add('begin');
  15129. Add(' b:=a as textb;');
  15130. Add(' c:=a as textc;');
  15131. Add(' c:=b as textc;');
  15132. ConvertProgram;
  15133. CheckSource('TestExternalClass_Is',
  15134. LinesToStr([ // statements
  15135. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15136. ' this.$init = function () {',
  15137. ' };',
  15138. ' this.$final = function () {',
  15139. ' };',
  15140. '});',
  15141. 'this.A = null;',
  15142. 'this.B = null;',
  15143. 'this.C = null;',
  15144. '']),
  15145. LinesToStr([ // $mod.$main
  15146. '$mod.B = rtl.asExt($mod.A, ExtB);',
  15147. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  15148. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  15149. '']));
  15150. end;
  15151. procedure TTestModule.TestExternalClass_DestructorFail;
  15152. begin
  15153. StartProgram(false);
  15154. Add('{$modeswitch externalclass}');
  15155. Add('type');
  15156. Add(' TExtA = class external name ''ExtA''');
  15157. Add(' destructor Free;');
  15158. Add(' end;');
  15159. SetExpectedPasResolverError('Pascal element not supported: destructor',
  15160. nPasElementNotSupported);
  15161. ConvertProgram;
  15162. end;
  15163. procedure TTestModule.TestExternalClass_New;
  15164. begin
  15165. StartProgram(false);
  15166. Add('{$modeswitch externalclass}');
  15167. Add('type');
  15168. Add(' TExtA = class external name ''ExtA''');
  15169. Add(' constructor New;');
  15170. Add(' constructor New(i: longint; j: longint = 2);');
  15171. Add(' end;');
  15172. Add('var');
  15173. Add(' A: texta;');
  15174. Add('begin');
  15175. Add(' a:=texta.new;');
  15176. Add(' a:=texta.new();');
  15177. Add(' a:=texta.new(1);');
  15178. Add(' with texta do begin');
  15179. Add(' a:=new;');
  15180. Add(' a:=new();');
  15181. Add(' a:=new(2);');
  15182. Add(' end;');
  15183. Add(' a:=test1.texta.new;');
  15184. Add(' a:=test1.texta.new();');
  15185. Add(' a:=test1.texta.new(3);');
  15186. ConvertProgram;
  15187. CheckSource('TestExternalClass_New',
  15188. LinesToStr([ // statements
  15189. 'this.A = null;',
  15190. '']),
  15191. LinesToStr([ // $mod.$main
  15192. '$mod.A = new ExtA();',
  15193. '$mod.A = new ExtA();',
  15194. '$mod.A = new ExtA(1,2);',
  15195. '$mod.A = new ExtA();',
  15196. '$mod.A = new ExtA();',
  15197. '$mod.A = new ExtA(2,2);',
  15198. '$mod.A = new ExtA();',
  15199. '$mod.A = new ExtA();',
  15200. '$mod.A = new ExtA(3,2);',
  15201. '']));
  15202. end;
  15203. procedure TTestModule.TestExternalClass_ClassOf_New;
  15204. begin
  15205. StartProgram(false);
  15206. Add('{$modeswitch externalclass}');
  15207. Add('type');
  15208. Add(' TExtAClass = class of TExtA;');
  15209. Add(' TExtA = class external name ''ExtA''');
  15210. Add(' C: TExtAClass;');
  15211. Add(' constructor New;');
  15212. Add(' end;');
  15213. Add('var');
  15214. Add(' A: texta;');
  15215. Add(' C: textaclass;');
  15216. Add('begin');
  15217. Add(' a:=c.new;');
  15218. Add(' a:=c.new();');
  15219. Add(' with C do begin');
  15220. Add(' a:=new;');
  15221. Add(' a:=new();');
  15222. Add(' end;');
  15223. Add(' a:=test1.c.new;');
  15224. Add(' a:=test1.c.new();');
  15225. Add(' a:=A.c.new();');
  15226. ConvertProgram;
  15227. CheckSource('TestExternalClass_ClassOf_New',
  15228. LinesToStr([ // statements
  15229. 'this.A = null;',
  15230. 'this.C = null;',
  15231. '']),
  15232. LinesToStr([ // $mod.$main
  15233. '$mod.A = new $mod.C();',
  15234. '$mod.A = new $mod.C();',
  15235. 'var $with1 = $mod.C;',
  15236. '$mod.A = new $with1();',
  15237. '$mod.A = new $with1();',
  15238. '$mod.A = new $mod.C();',
  15239. '$mod.A = new $mod.C();',
  15240. '$mod.A = new $mod.A.C();',
  15241. '']));
  15242. end;
  15243. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  15244. begin
  15245. StartProgram(false);
  15246. Add([
  15247. '{$modeswitch externalclass}',
  15248. 'type',
  15249. ' TExtAClass = class of TExtA;',
  15250. ' TExtA = class external name ''ExtA''',
  15251. ' constructor New;',
  15252. ' end;',
  15253. 'function GetCreator: TExtAClass;',
  15254. 'begin',
  15255. ' Result:=TExtA;',
  15256. 'end;',
  15257. 'var',
  15258. ' A: texta;',
  15259. 'begin',
  15260. ' a:=getcreator.new;',
  15261. ' a:=getcreator().new;',
  15262. ' a:=getcreator().new();',
  15263. ' a:=getcreator.new();',
  15264. ' with getcreator do begin',
  15265. ' a:=new;',
  15266. ' a:=new();',
  15267. ' end;']);
  15268. ConvertProgram;
  15269. CheckSource('TestExternalClass_FuncClassOf_New',
  15270. LinesToStr([ // statements
  15271. 'this.GetCreator = function () {',
  15272. ' var Result = null;',
  15273. ' Result = ExtA;',
  15274. ' return Result;',
  15275. '};',
  15276. 'this.A = null;',
  15277. '']),
  15278. LinesToStr([ // $mod.$main
  15279. '$mod.A = new ($mod.GetCreator())();',
  15280. '$mod.A = new ($mod.GetCreator())();',
  15281. '$mod.A = new ($mod.GetCreator())();',
  15282. '$mod.A = new ($mod.GetCreator())();',
  15283. 'var $with1 = $mod.GetCreator();',
  15284. '$mod.A = new $with1();',
  15285. '$mod.A = new $with1();',
  15286. '']));
  15287. end;
  15288. procedure TTestModule.TestExternalClass_New_PasClassFail;
  15289. begin
  15290. StartProgram(false);
  15291. Add([
  15292. '{$modeswitch externalclass}',
  15293. 'type',
  15294. ' TExtA = class external name ''ExtA''',
  15295. ' constructor New;',
  15296. ' end;',
  15297. ' TBird = class(TExtA)',
  15298. ' end;',
  15299. 'begin',
  15300. ' TBird.new;',
  15301. '']);
  15302. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  15303. ConvertProgram;
  15304. end;
  15305. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  15306. begin
  15307. StartProgram(false);
  15308. Add([
  15309. '{$modeswitch externalclass}',
  15310. 'type',
  15311. ' TExtA = class external name ''ExtA''',
  15312. ' constructor New;',
  15313. ' end;',
  15314. ' TBird = class(TExtA)',
  15315. ' end;',
  15316. 'begin',
  15317. ' TBird.new();',
  15318. '']);
  15319. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  15320. ConvertProgram;
  15321. end;
  15322. procedure TTestModule.TestExternalClass_LocalConstSameName;
  15323. begin
  15324. StartProgram(false);
  15325. Add('{$modeswitch externalclass}');
  15326. Add('type');
  15327. Add(' TExtA = class external name ''ExtA''');
  15328. Add(' constructor New;');
  15329. Add(' end;');
  15330. Add('function DoIt: longint;');
  15331. Add('const ExtA: longint = 3;');
  15332. Add('begin');
  15333. Add(' Result:=ExtA;');
  15334. Add('end;');
  15335. Add('var');
  15336. Add(' A: texta;');
  15337. Add('begin');
  15338. Add(' a:=texta.new;');
  15339. ConvertProgram;
  15340. CheckSource('TestExternalClass_LocalConstSameName',
  15341. LinesToStr([ // statements
  15342. 'var ExtA$1 = 3;',
  15343. 'this.DoIt = function () {',
  15344. ' var Result = 0;',
  15345. ' Result = ExtA$1;',
  15346. ' return Result;',
  15347. '};',
  15348. 'this.A = null;',
  15349. '']),
  15350. LinesToStr([ // $mod.$main
  15351. '$mod.A = new ExtA();',
  15352. '']));
  15353. end;
  15354. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  15355. begin
  15356. StartProgram(false);
  15357. Add('{$modeswitch externalclass}');
  15358. Add('type');
  15359. Add(' TExtA = class external name ''ExtA''');
  15360. Add(' procedure DoIt;');
  15361. Add(' end;');
  15362. Add(' TMyA = class(TExtA)');
  15363. Add(' procedure DoIt;');
  15364. Add(' end;');
  15365. Add('procedure TMyA.DoIt; begin end;');
  15366. Add('begin');
  15367. ConvertProgram;
  15368. CheckSource('TestExternalClass_ReintroduceOverload',
  15369. LinesToStr([ // statements
  15370. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  15371. ' this.$init = function () {',
  15372. ' };',
  15373. ' this.$final = function () {',
  15374. ' };',
  15375. ' this.DoIt$1 = function () {',
  15376. ' };',
  15377. '});',
  15378. '']),
  15379. LinesToStr([ // $mod.$main
  15380. '']));
  15381. end;
  15382. procedure TTestModule.TestExternalClass_Inherited;
  15383. begin
  15384. StartProgram(false);
  15385. Add('{$modeswitch externalclass}');
  15386. Add('type');
  15387. Add(' TExtA = class external name ''ExtA''');
  15388. Add(' procedure DoIt(i: longint = 1); virtual;');
  15389. Add(' procedure DoSome(j: longint = 2);');
  15390. Add(' end;');
  15391. Add(' TExtB = class external name ''ExtB''(TExtA)');
  15392. Add(' end;');
  15393. Add(' TMyC = class(TExtB)');
  15394. Add(' procedure DoIt(i: longint = 1); override;');
  15395. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  15396. Add(' end;');
  15397. Add('procedure TMyC.DoIt(i: longint);');
  15398. Add('begin');
  15399. Add(' inherited;');
  15400. Add(' inherited DoIt;');
  15401. Add(' inherited DoIt();');
  15402. Add(' inherited DoIt(3);');
  15403. Add(' inherited DoSome;');
  15404. Add(' inherited DoSome();');
  15405. Add(' inherited DoSome(4);');
  15406. Add('end;');
  15407. Add('procedure TMyC.DoSome(j: longint);');
  15408. Add('begin');
  15409. Add(' inherited;');
  15410. Add('end;');
  15411. Add('begin');
  15412. ConvertProgram;
  15413. CheckSource('TestExternalClass_ReintroduceOverload',
  15414. LinesToStr([ // statements
  15415. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  15416. ' this.$init = function () {',
  15417. ' };',
  15418. ' this.$final = function () {',
  15419. ' };',
  15420. ' this.DoIt = function (i) {',
  15421. ' ExtB.DoIt.apply(this, arguments);',
  15422. ' ExtB.DoIt.call(this, 1);',
  15423. ' ExtB.DoIt.call(this, 1);',
  15424. ' ExtB.DoIt.call(this, 3);',
  15425. ' ExtB.DoSome.call(this, 2);',
  15426. ' ExtB.DoSome.call(this, 2);',
  15427. ' ExtB.DoSome.call(this, 4);',
  15428. ' };',
  15429. ' this.DoSome$1 = function (j) {',
  15430. ' ExtB.DoSome.apply(this, arguments);',
  15431. ' };',
  15432. '});',
  15433. '']),
  15434. LinesToStr([ // $mod.$main
  15435. '']));
  15436. end;
  15437. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  15438. begin
  15439. StartProgram(false);
  15440. Add('{$modeswitch externalclass}');
  15441. Add('type');
  15442. Add(' TObject = class');
  15443. Add(' end;');
  15444. Add(' TExtA = class external name ''ExtA''(TObject)');
  15445. Add(' end;');
  15446. Add('begin');
  15447. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  15448. ConvertProgram;
  15449. end;
  15450. procedure TTestModule.TestExternalClass_NewInstance;
  15451. begin
  15452. StartProgram(false);
  15453. Add('{$modeswitch externalclass}');
  15454. Add('type');
  15455. Add(' TExtA = class external name ''ExtA''');
  15456. Add(' end;');
  15457. Add(' TMyB = class(TExtA)');
  15458. Add(' protected');
  15459. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  15460. Add(' end;');
  15461. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  15462. Add('begin end;');
  15463. Add('begin');
  15464. ConvertProgram;
  15465. CheckSource('TestExternalClass_NewInstance',
  15466. LinesToStr([ // statements
  15467. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  15468. ' this.$init = function () {',
  15469. ' };',
  15470. ' this.$final = function () {',
  15471. ' };',
  15472. ' this.NewInstance = function (fnname, paramarray) {',
  15473. ' var Result = null;',
  15474. ' return Result;',
  15475. ' };',
  15476. '});',
  15477. '']),
  15478. LinesToStr([ // $mod.$main
  15479. '']));
  15480. end;
  15481. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  15482. begin
  15483. StartProgram(false);
  15484. Add('{$modeswitch externalclass}');
  15485. Add('type');
  15486. Add(' TExtA = class external name ''ExtA''');
  15487. Add(' end;');
  15488. Add(' TMyB = class(TExtA)');
  15489. Add(' protected');
  15490. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  15491. Add(' end;');
  15492. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  15493. Add('begin end;');
  15494. Add('begin');
  15495. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  15496. ConvertProgram;
  15497. end;
  15498. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  15499. begin
  15500. StartProgram(false);
  15501. Add('{$modeswitch externalclass}');
  15502. Add('type');
  15503. Add(' TExtA = class external name ''ExtA''');
  15504. Add(' end;');
  15505. Add(' TMyB = class(TExtA)');
  15506. Add(' protected');
  15507. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  15508. Add(' end;');
  15509. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  15510. Add('begin end;');
  15511. Add('begin');
  15512. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  15513. nIncompatibleTypeArgNo);
  15514. ConvertProgram;
  15515. end;
  15516. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  15517. begin
  15518. StartProgram(false);
  15519. Add('{$modeswitch externalclass}');
  15520. Add('type');
  15521. Add(' TExtA = class external name ''ExtA''');
  15522. Add(' end;');
  15523. Add(' TMyB = class(TExtA)');
  15524. Add(' protected');
  15525. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  15526. Add(' end;');
  15527. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  15528. Add('begin end;');
  15529. Add('begin');
  15530. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  15531. nIncompatibleTypeArgNo);
  15532. ConvertProgram;
  15533. end;
  15534. procedure TTestModule.TestExternalClass_PascalProperty;
  15535. begin
  15536. StartProgram(false);
  15537. Add('{$modeswitch externalclass}');
  15538. Add('type');
  15539. Add(' TJSElement = class;');
  15540. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  15541. Add(' TJSElement = class external name ''ExtA''');
  15542. Add(' end;');
  15543. Add(' TControl = class(TJSElement)');
  15544. Add(' private');
  15545. Add(' FOnClick: TJSNotifyEvent;');
  15546. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  15547. Add(' procedure Click(Sender: TJSElement);');
  15548. Add(' end;');
  15549. Add('procedure TControl.Click(Sender: TJSElement);');
  15550. Add('begin');
  15551. Add(' OnClick(Self);');
  15552. Add('end;');
  15553. Add('var');
  15554. Add(' Ctrl: TControl;');
  15555. Add('begin');
  15556. Add(' Ctrl.OnClick:[email protected];');
  15557. Add(' Ctrl.OnClick(Ctrl);');
  15558. ConvertProgram;
  15559. CheckSource('TestExternalClass_PascalProperty',
  15560. LinesToStr([ // statements
  15561. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  15562. ' this.$init = function () {',
  15563. ' this.FOnClick = null;',
  15564. ' };',
  15565. ' this.$final = function () {',
  15566. ' this.FOnClick = undefined;',
  15567. ' };',
  15568. ' this.Click = function (Sender) {',
  15569. ' this.FOnClick(this);',
  15570. ' };',
  15571. '});',
  15572. 'this.Ctrl = null;',
  15573. '']),
  15574. LinesToStr([ // $mod.$main
  15575. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  15576. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  15577. '']));
  15578. end;
  15579. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  15580. begin
  15581. StartProgram(false);
  15582. Add([
  15583. '{$modeswitch externalclass}',
  15584. 'type',
  15585. ' IUnknown = interface end;',
  15586. ' TObject = class',
  15587. ' end;',
  15588. ' TChild = class',
  15589. ' end;',
  15590. ' TExtRootA = class external name ''ExtRootA''',
  15591. ' end;',
  15592. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  15593. ' end;',
  15594. ' TExtRootB = class external name ''ExtRootB''',
  15595. ' end;',
  15596. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  15597. ' end;',
  15598. 'var',
  15599. ' Obj: TObject;',
  15600. ' Child: TChild;',
  15601. ' RootA: TExtRootA;',
  15602. ' ChildA: TExtChildA;',
  15603. ' RootB: TExtRootB;',
  15604. ' ChildB: TExtChildB;',
  15605. ' i: IUnknown;',
  15606. 'begin',
  15607. ' obj:=tobject(roota);',
  15608. ' obj:=tobject(childa);',
  15609. ' child:=tchild(tobject(roota));',
  15610. ' roota:=textroota(obj);',
  15611. ' roota:=textroota(child);',
  15612. ' roota:=textroota(rootb);',
  15613. ' roota:=textroota(childb);',
  15614. ' childa:=textchilda(textroota(obj));',
  15615. ' roota:=TExtRootA(i)',
  15616. '']);
  15617. ConvertProgram;
  15618. CheckSource('TestExternalClass_TypeCastToRootClass',
  15619. LinesToStr([ // statements
  15620. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  15621. 'rtl.createClass($mod, "TObject", null, function () {',
  15622. ' this.$init = function () {',
  15623. ' };',
  15624. ' this.$final = function () {',
  15625. ' };',
  15626. '});',
  15627. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  15628. '});',
  15629. 'this.Obj = null;',
  15630. 'this.Child = null;',
  15631. 'this.RootA = null;',
  15632. 'this.ChildA = null;',
  15633. 'this.RootB = null;',
  15634. 'this.ChildB = null;',
  15635. 'this.i = null;',
  15636. '']),
  15637. LinesToStr([ // $mod.$main
  15638. '$mod.Obj = $mod.RootA;',
  15639. '$mod.Obj = $mod.ChildA;',
  15640. '$mod.Child = $mod.RootA;',
  15641. '$mod.RootA = $mod.Obj;',
  15642. '$mod.RootA = $mod.Child;',
  15643. '$mod.RootA = $mod.RootB;',
  15644. '$mod.RootA = $mod.ChildB;',
  15645. '$mod.ChildA = $mod.Obj;',
  15646. '$mod.RootA = $mod.i;',
  15647. '']));
  15648. end;
  15649. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  15650. begin
  15651. StartProgram(false);
  15652. Add([
  15653. '{$modeswitch externalclass}',
  15654. 'type',
  15655. ' IUnknown = interface end;',
  15656. ' IBird = interface(IUnknown) end;',
  15657. ' TClass = class of TObject;',
  15658. ' TObject = class',
  15659. ' end;',
  15660. ' TChild = class',
  15661. ' end;',
  15662. ' TJSObject = class external name ''Object''',
  15663. ' end;',
  15664. ' TRec = record end;',
  15665. 'var',
  15666. ' Obj: TObject;',
  15667. ' Child: TChild;',
  15668. ' i: IUnknown;',
  15669. ' Bird: IBird;',
  15670. ' j: TJSObject;',
  15671. ' r: TRec;',
  15672. ' c: TClass;',
  15673. 'begin',
  15674. ' j:=tjsobject(IUnknown);',
  15675. ' j:=tjsobject(IBird);',
  15676. ' j:=tjsobject(TObject);',
  15677. ' j:=tjsobject(TChild);',
  15678. ' j:=tjsobject(TRec);',
  15679. ' j:=tjsobject(Obj);',
  15680. ' j:=tjsobject(Child);',
  15681. ' j:=tjsobject(i);',
  15682. ' j:=tjsobject(Bird);',
  15683. ' j:=tjsobject(r);',
  15684. ' j:=tjsobject(c);',
  15685. '']);
  15686. ConvertProgram;
  15687. CheckSource('TestExternalClass_TypeCastToJSObject',
  15688. LinesToStr([ // statements
  15689. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  15690. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  15691. 'rtl.createClass($mod, "TObject", null, function () {',
  15692. ' this.$init = function () {',
  15693. ' };',
  15694. ' this.$final = function () {',
  15695. ' };',
  15696. '});',
  15697. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  15698. '});',
  15699. 'rtl.recNewT($mod, "TRec", function () {',
  15700. ' this.$eq = function (b) {',
  15701. ' return true;',
  15702. ' };',
  15703. ' this.$assign = function (s) {',
  15704. ' return this;',
  15705. ' };',
  15706. '});',
  15707. 'this.Obj = null;',
  15708. 'this.Child = null;',
  15709. 'this.i = null;',
  15710. 'this.Bird = null;',
  15711. 'this.j = null;',
  15712. 'this.r = $mod.TRec.$new();',
  15713. 'this.c = null;',
  15714. '']),
  15715. LinesToStr([ // $mod.$main
  15716. '$mod.j = $mod.IUnknown;',
  15717. '$mod.j = $mod.IBird;',
  15718. '$mod.j = $mod.TObject;',
  15719. '$mod.j = $mod.TChild;',
  15720. '$mod.j = $mod.TRec;',
  15721. '$mod.j = $mod.Obj;',
  15722. '$mod.j = $mod.Child;',
  15723. '$mod.j = $mod.i;',
  15724. '$mod.j = $mod.Bird;',
  15725. '$mod.j = $mod.r;',
  15726. '$mod.j = $mod.c;',
  15727. '']));
  15728. end;
  15729. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  15730. begin
  15731. StartProgram(false);
  15732. Add('{$modeswitch externalclass}');
  15733. Add('type');
  15734. Add(' TJSString = class external name ''String''');
  15735. Add(' class function fromCharCode() : string; varargs;');
  15736. Add(' function anchor(const aName : string) : string;');
  15737. Add(' end;');
  15738. Add('var');
  15739. Add(' s: string;');
  15740. Add('begin');
  15741. Add(' s:=TJSString.fromCharCode(65,66);');
  15742. Add(' s:=TJSString(s).anchor(s);');
  15743. Add(' s:=TJSString(''foo'').anchor(s);');
  15744. ConvertProgram;
  15745. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  15746. LinesToStr([ // statements
  15747. 'this.s = "";',
  15748. '']),
  15749. LinesToStr([ // $mod.$main
  15750. '$mod.s = String.fromCharCode(65, 66);',
  15751. '$mod.s = $mod.s.anchor($mod.s);',
  15752. '$mod.s = "foo".anchor($mod.s);',
  15753. '']));
  15754. end;
  15755. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  15756. begin
  15757. StartProgram(false);
  15758. Add([
  15759. '{$modeswitch externalclass}',
  15760. 'type',
  15761. ' TJSObject = class external name ''Object'' end;',
  15762. ' TJSFunction = class external name ''Function''',
  15763. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  15764. ' function call(thisArg: TJSObject): JSValue; varargs;',
  15765. ' end;',
  15766. ' TObject = class',
  15767. ' procedure DoIt(i: longint);',
  15768. ' end;',
  15769. ' TFuncInt = function(o: TObject): longint;',
  15770. 'function GetIt(o: TObject): longint;',
  15771. ' procedure Sub; begin end;',
  15772. 'var',
  15773. ' f: TJSFunction;',
  15774. ' fi: TFuncInt;',
  15775. 'begin',
  15776. ' fi:=TFuncInt(f);',
  15777. ' f:=TJSFunction(fi);',
  15778. ' f:=TJSFunction(@GetIt);',
  15779. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  15780. ' f:=TJSFunction(@Sub);',
  15781. ' f:=TJSFunction(@o.doit);',
  15782. ' f:=TJSFunction(fi).bind(nil,4)',
  15783. 'end;',
  15784. 'procedure TObject.DoIt(i: longint);',
  15785. ' procedure Sub; begin end;',
  15786. 'var f: TJSFunction;',
  15787. 'begin',
  15788. ' f:=TJSFunction(@DoIt);',
  15789. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  15790. ' f:=TJSFunction(@Sub);',
  15791. ' f:=TJSFunction(@GetIt);',
  15792. 'end;',
  15793. 'begin']);
  15794. ConvertProgram;
  15795. CheckSource('TestExternalClass_TypeCastToJSFunction',
  15796. LinesToStr([ // statements
  15797. 'rtl.createClass($mod, "TObject", null, function () {',
  15798. ' this.$init = function () {',
  15799. ' };',
  15800. ' this.$final = function () {',
  15801. ' };',
  15802. ' this.DoIt = function (i) {',
  15803. ' var $Self = this;',
  15804. ' function Sub() {',
  15805. ' };',
  15806. ' var f = null;',
  15807. ' f = rtl.createCallback($Self, "DoIt");',
  15808. ' f = rtl.createCallback($Self, "DoIt").bind(null, 13);',
  15809. ' f = Sub;',
  15810. ' f = $mod.GetIt;',
  15811. ' };',
  15812. '});',
  15813. 'this.GetIt = function (o) {',
  15814. ' var Result = 0;',
  15815. ' function Sub() {',
  15816. ' };',
  15817. ' var f = null;',
  15818. ' var fi = null;',
  15819. ' fi = f;',
  15820. ' f = fi;',
  15821. ' f = $mod.GetIt;',
  15822. ' f = $mod.GetIt.bind(null, 3);',
  15823. ' f = Sub;',
  15824. ' f = rtl.createCallback(o, "DoIt");',
  15825. ' f = fi.bind(null, 4);',
  15826. ' return Result;',
  15827. '};',
  15828. '']),
  15829. LinesToStr([ // $mod.$main
  15830. '']));
  15831. end;
  15832. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  15833. begin
  15834. StartProgram(false);
  15835. Add('{$modeswitch externalclass}');
  15836. Add('type');
  15837. Add(' TJSString = class external name ''String''');
  15838. Add(' class function fromCharCode() : string; varargs;');
  15839. Add(' end;');
  15840. Add('var');
  15841. Add(' s: string;');
  15842. Add(' sObj: TJSString;');
  15843. Add('begin');
  15844. Add(' s:=sObj.fromCharCode(65,66);');
  15845. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  15846. nExternalClassInstanceCannotAccessStaticX);
  15847. ConvertProgram;
  15848. end;
  15849. procedure TTestModule.TestExternalClass_BracketAccessor;
  15850. begin
  15851. StartProgram(false);
  15852. Add([
  15853. '{$modeswitch externalclass}',
  15854. 'type',
  15855. ' TJSArray = class external name ''Array2''',
  15856. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  15857. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  15858. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  15859. ' end;',
  15860. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  15861. 'begin end;',
  15862. 'var',
  15863. ' Arr: tjsarray;',
  15864. ' s: string;',
  15865. ' i: longint;',
  15866. ' v: jsvalue;',
  15867. 'begin',
  15868. ' v:=arr[0];',
  15869. ' v:=arr.items[1];',
  15870. ' arr[2]:=s;',
  15871. ' arr.items[3]:=s;',
  15872. ' arr[4]:=i;',
  15873. ' arr[5]:=arr[6];',
  15874. ' arr.items[7]:=arr.items[8];',
  15875. ' with arr do items[9]:=items[10];',
  15876. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  15877. ' with arr do begin',
  15878. ' v:=GetItems(14);',
  15879. ' setitems(15,16);',
  15880. ' end;',
  15881. ' v:=test1.arr.items[17];',
  15882. ' test1.arr.items[18]:=v;',
  15883. '']);
  15884. ConvertProgram;
  15885. CheckSource('TestExternalClass_BracketAccessor',
  15886. LinesToStr([ // statements
  15887. 'this.DoIt = function (vI, vJ, vK, vL) {',
  15888. '};',
  15889. 'this.Arr = null;',
  15890. 'this.s = "";',
  15891. 'this.i = 0;',
  15892. 'this.v = undefined;',
  15893. '']),
  15894. LinesToStr([ // $mod.$main
  15895. '$mod.v = $mod.Arr[0];',
  15896. '$mod.v = $mod.Arr[1];',
  15897. '$mod.Arr[2] = $mod.s;',
  15898. '$mod.Arr[3] = $mod.s;',
  15899. '$mod.Arr[4] = $mod.i;',
  15900. '$mod.Arr[5] = $mod.Arr[6];',
  15901. '$mod.Arr[7] = $mod.Arr[8];',
  15902. 'var $with1 = $mod.Arr;',
  15903. '$with1[9] = $with1[10];',
  15904. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  15905. ' a: 9,',
  15906. ' p: $mod.Arr,',
  15907. ' get: function () {',
  15908. ' return this.p[this.a];',
  15909. ' },',
  15910. ' set: function (v) {',
  15911. ' this.p[this.a] = v;',
  15912. ' }',
  15913. '}, {',
  15914. ' a: 10,',
  15915. ' p: $mod.Arr,',
  15916. ' get: function () {',
  15917. ' return this.p[this.a];',
  15918. ' },',
  15919. ' set: function (v) {',
  15920. ' this.p[this.a] = v;',
  15921. ' }',
  15922. '});',
  15923. 'var $with2 = $mod.Arr;',
  15924. '$mod.v = $with2[14];',
  15925. '$with2[15] = 16;',
  15926. '$mod.v = $mod.Arr[17];',
  15927. '$mod.Arr[18] = $mod.v;',
  15928. '']));
  15929. end;
  15930. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  15931. begin
  15932. StartProgram(false);
  15933. Add([
  15934. '{$modeswitch externalclass}',
  15935. 'type',
  15936. ' TJSArray = class external name ''Array2''',
  15937. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  15938. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  15939. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  15940. ' end;',
  15941. ' TMyArr = class(TJSArray)',
  15942. ' procedure DoIt;',
  15943. ' end;',
  15944. 'procedure tmyarr.DoIt;',
  15945. 'begin',
  15946. ' Items[1]:=Items[2];',
  15947. ' SetItems(3,getItems(4));',
  15948. 'end;',
  15949. 'var',
  15950. ' Arr: tmyarr;',
  15951. ' s: string;',
  15952. ' i: longint;',
  15953. ' v: jsvalue;',
  15954. 'begin',
  15955. ' v:=arr[0];',
  15956. ' v:=arr.items[1];',
  15957. ' arr[2]:=s;',
  15958. ' arr.items[3]:=s;',
  15959. ' arr[4]:=i;',
  15960. ' arr[5]:=arr[6];',
  15961. ' arr.items[7]:=arr.items[8];',
  15962. ' with arr do items[9]:=items[10];',
  15963. ' with arr do begin',
  15964. ' v:=GetItems(14);',
  15965. ' setitems(15,16);',
  15966. ' end;',
  15967. '']);
  15968. ConvertProgram;
  15969. CheckSource('TestExternalClass_BracketAccessor_Call',
  15970. LinesToStr([ // statements
  15971. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  15972. ' this.$init = function () {',
  15973. ' };',
  15974. ' this.$final = function () {',
  15975. ' };',
  15976. ' this.DoIt = function () {',
  15977. ' this[1] = this[2];',
  15978. ' this[3] = this[4];',
  15979. ' };',
  15980. '});',
  15981. 'this.Arr = null;',
  15982. 'this.s = "";',
  15983. 'this.i = 0;',
  15984. 'this.v = undefined;',
  15985. '']),
  15986. LinesToStr([ // $mod.$main
  15987. '$mod.v = $mod.Arr[0];',
  15988. '$mod.v = $mod.Arr[1];',
  15989. '$mod.Arr[2] = $mod.s;',
  15990. '$mod.Arr[3] = $mod.s;',
  15991. '$mod.Arr[4] = $mod.i;',
  15992. '$mod.Arr[5] = $mod.Arr[6];',
  15993. '$mod.Arr[7] = $mod.Arr[8];',
  15994. 'var $with1 = $mod.Arr;',
  15995. '$with1[9] = $with1[10];',
  15996. 'var $with2 = $mod.Arr;',
  15997. '$mod.v = $with2[14];',
  15998. '$with2[15] = 16;',
  15999. '']));
  16000. end;
  16001. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  16002. begin
  16003. StartProgram(false);
  16004. Add('{$modeswitch externalclass}');
  16005. Add('type');
  16006. Add(' TJSArray = class external name ''Array2''');
  16007. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  16008. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  16009. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  16010. Add(' end;');
  16011. Add('begin');
  16012. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  16013. nBracketAccessorOfExternalClassMustHaveOneParameter);
  16014. ConvertProgram;
  16015. end;
  16016. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  16017. begin
  16018. StartProgram(false);
  16019. Add('{$modeswitch externalclass}');
  16020. Add('type');
  16021. Add(' TJSArray = class external name ''Array2''');
  16022. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16023. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  16024. Add(' end;');
  16025. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  16026. Add('begin end;');
  16027. Add('var');
  16028. Add(' Arr: tjsarray;');
  16029. Add(' v: jsvalue;');
  16030. Add('begin');
  16031. Add(' v:=arr[0];');
  16032. Add(' v:=arr.items[1];');
  16033. Add(' with arr do v:=items[2];');
  16034. Add(' doit(arr[3],arr[4]);');
  16035. ConvertProgram;
  16036. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  16037. LinesToStr([ // statements
  16038. 'this.DoIt = function (vI, vJ) {',
  16039. '};',
  16040. 'this.Arr = null;',
  16041. 'this.v = undefined;',
  16042. '']),
  16043. LinesToStr([ // $mod.$main
  16044. '$mod.v = $mod.Arr[0];',
  16045. '$mod.v = $mod.Arr[1];',
  16046. 'var $with1 = $mod.Arr;',
  16047. '$mod.v = $with1[2];',
  16048. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  16049. '']));
  16050. end;
  16051. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  16052. begin
  16053. StartProgram(false);
  16054. Add('{$modeswitch externalclass}');
  16055. Add('type');
  16056. Add(' TJSArray = class external name ''Array2''');
  16057. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16058. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16059. Add(' end;');
  16060. Add('var');
  16061. Add(' Arr: tjsarray;');
  16062. Add(' s: string;');
  16063. Add(' i: longint;');
  16064. Add(' v: jsvalue;');
  16065. Add('begin');
  16066. Add(' arr[2]:=s;');
  16067. Add(' arr.items[3]:=s;');
  16068. Add(' arr[4]:=i;');
  16069. Add(' with arr do items[5]:=i;');
  16070. ConvertProgram;
  16071. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  16072. LinesToStr([ // statements
  16073. 'this.Arr = null;',
  16074. 'this.s = "";',
  16075. 'this.i = 0;',
  16076. 'this.v = undefined;',
  16077. '']),
  16078. LinesToStr([ // $mod.$main
  16079. '$mod.Arr[2] = $mod.s;',
  16080. '$mod.Arr[3] = $mod.s;',
  16081. '$mod.Arr[4] = $mod.i;',
  16082. 'var $with1 = $mod.Arr;',
  16083. '$with1[5] = $mod.i;',
  16084. '']));
  16085. end;
  16086. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  16087. begin
  16088. StartProgram(false);
  16089. Add('{$modeswitch externalclass}');
  16090. Add('type');
  16091. Add(' TJSArray = class external name ''Array2''');
  16092. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16093. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16094. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  16095. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  16096. Add(' end;');
  16097. Add('var');
  16098. Add(' Arr: tjsarray;');
  16099. Add(' s: string;');
  16100. Add(' i: longint;');
  16101. Add(' v: jsvalue;');
  16102. Add('begin');
  16103. Add(' arr[2]:=s;');
  16104. Add(' arr.items[3]:=s;');
  16105. Add(' arr.numbers[4]:=i;');
  16106. Add(' with arr do items[5]:=i;');
  16107. Add(' with arr do numbers[6]:=i;');
  16108. ConvertProgram;
  16109. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  16110. LinesToStr([ // statements
  16111. 'this.Arr = null;',
  16112. 'this.s = "";',
  16113. 'this.i = 0;',
  16114. 'this.v = undefined;',
  16115. '']),
  16116. LinesToStr([ // $mod.$main
  16117. '$mod.Arr[2] = $mod.s;',
  16118. '$mod.Arr[3] = $mod.s;',
  16119. '$mod.Arr[4] = $mod.i;',
  16120. 'var $with1 = $mod.Arr;',
  16121. '$with1[5] = $mod.i;',
  16122. 'var $with2 = $mod.Arr;',
  16123. '$with2[6] = $mod.i;',
  16124. '']));
  16125. end;
  16126. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  16127. begin
  16128. StartProgram(false);
  16129. Add('{$modeswitch externalclass}');
  16130. Add('type');
  16131. Add(' TJSArray = class external name ''Array2''');
  16132. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16133. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16134. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  16135. Add(' end;');
  16136. Add('var');
  16137. Add(' Arr: tjsarray;');
  16138. Add(' i: longint;');
  16139. Add(' IntArr: array of longint;');
  16140. Add(' v: jsvalue;');
  16141. Add('begin');
  16142. Add(' v:=arr.items[i];');
  16143. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  16144. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  16145. ConvertProgram;
  16146. CheckSource('TestExternalClass_BracketAccessor_Index',
  16147. LinesToStr([ // statements
  16148. 'this.Arr = null;',
  16149. 'this.i = 0;',
  16150. 'this.IntArr = [];',
  16151. 'this.v = undefined;',
  16152. '']),
  16153. LinesToStr([ // $mod.$main
  16154. '$mod.v = $mod.Arr[$mod.i];',
  16155. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  16156. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  16157. '']));
  16158. end;
  16159. procedure TTestModule.TestExternalClass_ForInJSObject;
  16160. begin
  16161. StartProgram(false);
  16162. Add([
  16163. '{$modeswitch externalclass}',
  16164. 'type',
  16165. ' TJSObject = class external name ''Object''',
  16166. ' end;',
  16167. 'var',
  16168. ' o: TJSObject;',
  16169. ' key: string;',
  16170. 'begin',
  16171. ' for key in o do',
  16172. ' if key=''abc'' then ;',
  16173. '']);
  16174. ConvertProgram;
  16175. CheckSource('TestExternalClass_ForInJSObject',
  16176. LinesToStr([ // statements
  16177. 'this.o = null;',
  16178. 'this.key = "";',
  16179. '']),
  16180. LinesToStr([ // $mod.$main
  16181. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  16182. '']));
  16183. end;
  16184. procedure TTestModule.TestExternalClass_ForInJSArray;
  16185. begin
  16186. StartProgram(false);
  16187. Add([
  16188. '{$modeswitch externalclass}',
  16189. 'type',
  16190. ' TJSInt8Array = class external name ''Int8Array''',
  16191. ' private',
  16192. ' flength: NativeInt external name ''length'';',
  16193. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  16194. ' public',
  16195. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  16196. ' property Length: NativeInt read flength;',
  16197. ' end;',
  16198. 'var',
  16199. ' a: TJSInt8Array;',
  16200. ' value: shortint;',
  16201. 'begin',
  16202. ' for value in a do',
  16203. ' if value=3 then ;',
  16204. '']);
  16205. ConvertProgram;
  16206. CheckSource('TestExternalClass_ForInJSArray',
  16207. LinesToStr([ // statements
  16208. 'this.a = null;',
  16209. 'this.value = 0;',
  16210. '']),
  16211. LinesToStr([ // $mod.$main
  16212. 'for (var $in1 = $mod.a, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) {',
  16213. ' $mod.value = $in1[$l2];',
  16214. ' if ($mod.value === 3) ;',
  16215. '};',
  16216. '']));
  16217. end;
  16218. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  16219. begin
  16220. AddModuleWithIntfImplSrc('unit2.pas',
  16221. LinesToStr([
  16222. '{$modeswitch externalclass}',
  16223. 'type',
  16224. ' TJSBufferSource = class external name ''BufferSource''',
  16225. ' end;',
  16226. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  16227. '']),
  16228. '');
  16229. AddModuleWithIntfImplSrc('unit3.pas',
  16230. LinesToStr([
  16231. '{$modeswitch externalclass}',
  16232. 'type',
  16233. ' TJSBufferSource = class external name ''BufferSource''',
  16234. ' end;',
  16235. '']),
  16236. '');
  16237. StartUnit(true);
  16238. Add([
  16239. 'interface',
  16240. 'uses unit2, unit3;',
  16241. 'procedure DoSome(s: TJSBufferSource);',
  16242. 'implementation',
  16243. 'procedure DoSome(s: TJSBufferSource);',
  16244. 'begin',
  16245. ' DoIt(s);',
  16246. 'end;',
  16247. '']);
  16248. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  16249. nIncompatibleTypeArgNo);
  16250. ConvertUnit;
  16251. end;
  16252. procedure TTestModule.TestClassInterface_Corba;
  16253. begin
  16254. StartProgram(false);
  16255. Add([
  16256. '{$interfaces corba}',
  16257. 'type',
  16258. ' IUnknown = interface;',
  16259. ' IUnknown = interface',
  16260. ' [''{00000000-0000-0000-C000-000000000046}'']',
  16261. ' end;',
  16262. ' IInterface = IUnknown;',
  16263. ' IBird = interface(IInterface)',
  16264. ' function GetSize: longint;',
  16265. ' procedure SetSize(i: longint);',
  16266. ' property Size: longint read GetSize write SetSize;',
  16267. ' procedure DoIt(i: longint);',
  16268. ' end;',
  16269. ' TObject = class',
  16270. ' end;',
  16271. ' TBird = class(TObject,IBird)',
  16272. ' function GetSize: longint; virtual; abstract;',
  16273. ' procedure SetSize(i: longint); virtual; abstract;',
  16274. ' procedure DoIt(i: longint); virtual; abstract;',
  16275. ' end;',
  16276. 'var',
  16277. ' BirdIntf: IBird;',
  16278. 'begin',
  16279. ' BirdIntf.Size:=BirdIntf.Size;',
  16280. '']);
  16281. ConvertProgram;
  16282. CheckSource('TestClassInterface_Corba',
  16283. LinesToStr([ // statements
  16284. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  16285. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  16286. 'rtl.createClass($mod, "TObject", null, function () {',
  16287. ' this.$init = function () {',
  16288. ' };',
  16289. ' this.$final = function () {',
  16290. ' };',
  16291. '});',
  16292. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16293. ' rtl.addIntf(this, $mod.IBird);',
  16294. '});',
  16295. 'this.BirdIntf = null;',
  16296. '']),
  16297. LinesToStr([ // $mod.$main
  16298. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  16299. '']));
  16300. end;
  16301. procedure TTestModule.TestClassInterface_ProcExternalFail;
  16302. begin
  16303. StartProgram(false);
  16304. Add([
  16305. '{$interfaces corba}',
  16306. 'type',
  16307. ' IUnknown = interface',
  16308. ' procedure DoIt; external name ''foo'';',
  16309. ' end;',
  16310. 'begin']);
  16311. SetExpectedParserError(
  16312. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  16313. nParserNoFieldsAllowed);
  16314. ConvertProgram;
  16315. end;
  16316. procedure TTestModule.TestClassInterface_Overloads;
  16317. begin
  16318. StartProgram(false);
  16319. Add([
  16320. '{$interfaces corba}',
  16321. 'type',
  16322. ' integer = longint;',
  16323. ' IUnknown = interface',
  16324. ' procedure DoIt(i: integer);',
  16325. ' procedure DoIt(s: string);',
  16326. ' end;',
  16327. ' IBird = interface(IUnknown)',
  16328. ' procedure DoIt(b: boolean); overload;',
  16329. ' end;',
  16330. ' TObject = class',
  16331. ' end;',
  16332. ' TBird = class(TObject,IBird)',
  16333. ' procedure DoIt(o: TObject);',
  16334. ' procedure DoIt(s: string);',
  16335. ' procedure DoIt(i: integer);',
  16336. ' procedure DoIt(b: boolean);',
  16337. ' end;',
  16338. 'procedure TBird.DoIt(o: TObject); begin end;',
  16339. 'procedure TBird.DoIt(s: string); begin end;',
  16340. 'procedure TBird.DoIt(i: integer); begin end;',
  16341. 'procedure TBird.DoIt(b: boolean); begin end;',
  16342. 'var',
  16343. ' BirdIntf: IBird;',
  16344. 'begin',
  16345. ' BirdIntf.DoIt(3);',
  16346. ' BirdIntf.DoIt(''abc'');',
  16347. ' BirdIntf.DoIt(true);',
  16348. '']);
  16349. ConvertProgram;
  16350. CheckSource('TestClassInterface_Overloads',
  16351. LinesToStr([ // statements
  16352. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  16353. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  16354. 'rtl.createClass($mod, "TObject", null, function () {',
  16355. ' this.$init = function () {',
  16356. ' };',
  16357. ' this.$final = function () {',
  16358. ' };',
  16359. '});',
  16360. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16361. ' this.DoIt = function (o) {',
  16362. ' };',
  16363. ' this.DoIt$1 = function (s) {',
  16364. ' };',
  16365. ' this.DoIt$2 = function (i) {',
  16366. ' };',
  16367. ' this.DoIt$3 = function (b) {',
  16368. ' };',
  16369. ' rtl.addIntf(this, $mod.IBird, {',
  16370. ' DoIt$2: "DoIt$3",',
  16371. ' DoIt: "DoIt$2"',
  16372. ' });',
  16373. '});',
  16374. 'this.BirdIntf = null;',
  16375. '']),
  16376. LinesToStr([ // $mod.$main
  16377. '$mod.BirdIntf.DoIt(3);',
  16378. '$mod.BirdIntf.DoIt$1("abc");',
  16379. '$mod.BirdIntf.DoIt$2(true);',
  16380. '']));
  16381. end;
  16382. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  16383. begin
  16384. StartProgram(false);
  16385. Add([
  16386. '{$interfaces corba}',
  16387. 'type',
  16388. ' IBird = interface',
  16389. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16390. ' end;',
  16391. ' IDog = interface',
  16392. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16393. ' end;',
  16394. ' TObject = class(IBird,IDog)',
  16395. ' end;',
  16396. 'begin']);
  16397. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  16398. nDuplicateGUIDXInYZ);
  16399. ConvertProgram;
  16400. end;
  16401. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  16402. begin
  16403. StartProgram(false);
  16404. Add([
  16405. '{$interfaces corba}',
  16406. 'type',
  16407. ' IAnimal = interface',
  16408. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16409. ' end;',
  16410. ' IBird = interface(IAnimal)',
  16411. ' end;',
  16412. ' IHawk = interface(IBird)',
  16413. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16414. ' end;',
  16415. 'begin']);
  16416. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  16417. nDuplicateGUIDXInYZ);
  16418. ConvertProgram;
  16419. end;
  16420. procedure TTestModule.TestClassInterface_AncestorImpl;
  16421. begin
  16422. StartProgram(false);
  16423. Add([
  16424. '{$interfaces corba}',
  16425. 'type',
  16426. ' integer = longint;',
  16427. ' IUnknown = interface',
  16428. ' procedure DoIt(i: integer);',
  16429. ' end;',
  16430. ' IBird = interface',
  16431. ' procedure Fly(i: integer);',
  16432. ' end;',
  16433. ' TObject = class(IUnknown)',
  16434. ' procedure DoIt(i: integer);',
  16435. ' end;',
  16436. ' TBird = class(IBird)',
  16437. ' procedure Fly(i: integer);',
  16438. ' end;',
  16439. 'procedure TObject.DoIt(i: integer); begin end;',
  16440. 'procedure TBird.Fly(i: integer); begin end;',
  16441. 'begin',
  16442. '']);
  16443. ConvertProgram;
  16444. CheckSource('TestClassInterface_AncestorIntf',
  16445. LinesToStr([ // statements
  16446. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  16447. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  16448. 'rtl.createClass($mod, "TObject", null, function () {',
  16449. ' this.$init = function () {',
  16450. ' };',
  16451. ' this.$final = function () {',
  16452. ' };',
  16453. ' this.DoIt = function (i) {',
  16454. ' };',
  16455. ' rtl.addIntf(this, $mod.IUnknown);',
  16456. '});',
  16457. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16458. ' this.Fly = function (i) {',
  16459. ' };',
  16460. ' rtl.addIntf(this, $mod.IBird);',
  16461. ' rtl.addIntf(this, $mod.IUnknown);',
  16462. '});',
  16463. '']),
  16464. LinesToStr([ // $mod.$main
  16465. '']));
  16466. end;
  16467. procedure TTestModule.TestClassInterface_ImplReintroduce;
  16468. begin
  16469. StartProgram(false);
  16470. Add([
  16471. '{$interfaces corba}',
  16472. 'type',
  16473. ' integer = longint;',
  16474. ' IBird = interface',
  16475. ' procedure DoIt(i: integer);',
  16476. ' end;',
  16477. ' TObject = class',
  16478. ' procedure DoIt(i: integer);',
  16479. ' end;',
  16480. ' TBird = class(IBird)',
  16481. ' procedure DoIt(i: integer); virtual; reintroduce;',
  16482. ' end;',
  16483. 'procedure TObject.DoIt(i: integer); begin end;',
  16484. 'procedure TBird.DoIt(i: integer); begin end;',
  16485. 'begin',
  16486. '']);
  16487. ConvertProgram;
  16488. CheckSource('TestClassInterface_ImplReintroduce',
  16489. LinesToStr([ // statements
  16490. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  16491. 'rtl.createClass($mod, "TObject", null, function () {',
  16492. ' this.$init = function () {',
  16493. ' };',
  16494. ' this.$final = function () {',
  16495. ' };',
  16496. ' this.DoIt = function (i) {',
  16497. ' };',
  16498. '});',
  16499. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16500. ' this.DoIt$1 = function (i) {',
  16501. ' };',
  16502. ' rtl.addIntf(this, $mod.IBird, {',
  16503. ' DoIt: "DoIt$1"',
  16504. ' });',
  16505. '});',
  16506. '']),
  16507. LinesToStr([ // $mod.$main
  16508. '']));
  16509. end;
  16510. procedure TTestModule.TestClassInterface_MethodResolution;
  16511. begin
  16512. StartProgram(false);
  16513. Add([
  16514. '{$interfaces corba}',
  16515. 'type',
  16516. ' IUnknown = interface',
  16517. ' procedure Walk(i: longint);',
  16518. ' end;',
  16519. ' IBird = interface(IUnknown)',
  16520. ' procedure Walk(b: boolean); overload;',
  16521. ' procedure Fly(s: string);',
  16522. ' end;',
  16523. ' TObject = class',
  16524. ' end;',
  16525. ' TBird = class(TObject,IBird)',
  16526. ' procedure IBird.Fly = Move;',
  16527. ' procedure IBird.Walk = Hop;',
  16528. ' procedure Hop(i: longint);',
  16529. ' procedure Move(s: string);',
  16530. ' procedure Hop(b: boolean);',
  16531. ' end;',
  16532. 'procedure TBird.Move(s: string); begin end;',
  16533. 'procedure TBird.Hop(i: longint); begin end;',
  16534. 'procedure TBird.Hop(b: boolean); begin end;',
  16535. 'var',
  16536. ' BirdIntf: IBird;',
  16537. 'begin',
  16538. ' BirdIntf.Walk(3);',
  16539. ' BirdIntf.Walk(true);',
  16540. ' BirdIntf.Fly(''abc'');',
  16541. '']);
  16542. ConvertProgram;
  16543. CheckSource('TestClassInterface_MethodResolution',
  16544. LinesToStr([ // statements
  16545. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  16546. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  16547. 'rtl.createClass($mod, "TObject", null, function () {',
  16548. ' this.$init = function () {',
  16549. ' };',
  16550. ' this.$final = function () {',
  16551. ' };',
  16552. '});',
  16553. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16554. ' this.Hop = function (i) {',
  16555. ' };',
  16556. ' this.Move = function (s) {',
  16557. ' };',
  16558. ' this.Hop$1 = function (b) {',
  16559. ' };',
  16560. ' rtl.addIntf(this, $mod.IBird, {',
  16561. ' Walk$1: "Hop$1",',
  16562. ' Fly: "Move",',
  16563. ' Walk: "Hop"',
  16564. ' });',
  16565. '});',
  16566. 'this.BirdIntf = null;',
  16567. '']),
  16568. LinesToStr([ // $mod.$main
  16569. '$mod.BirdIntf.Walk(3);',
  16570. '$mod.BirdIntf.Walk$1(true);',
  16571. '$mod.BirdIntf.Fly("abc");',
  16572. '']));
  16573. end;
  16574. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  16575. begin
  16576. StartProgram(false);
  16577. Add([
  16578. '{$interfaces com}',
  16579. 'type',
  16580. ' IUnknown = interface',
  16581. ' function _AddRef: longint;',
  16582. ' procedure Walk;',
  16583. ' end;',
  16584. ' IBird = interface end;',
  16585. ' IDog = interface end;',
  16586. ' TObject = class(IBird,IDog)',
  16587. ' function _AddRef: longint; virtual; abstract;',
  16588. ' procedure Walk; virtual; abstract;',
  16589. ' end;',
  16590. ' TBird = class(IUnknown)',
  16591. ' end;',
  16592. 'begin',
  16593. '']);
  16594. ConvertProgram;
  16595. CheckSource('TestClassInterface_COM_AncestorLess',
  16596. LinesToStr([ // statements
  16597. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  16598. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  16599. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  16600. 'rtl.createClass($mod, "TObject", null, function () {',
  16601. ' this.$init = function () {',
  16602. ' };',
  16603. ' this.$final = function () {',
  16604. ' };',
  16605. ' rtl.addIntf(this, $mod.IBird);',
  16606. ' rtl.addIntf(this, $mod.IDog);',
  16607. '});',
  16608. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16609. ' rtl.addIntf(this, $mod.IUnknown);',
  16610. ' rtl.addIntf(this, $mod.IBird);',
  16611. ' rtl.addIntf(this, $mod.IDog);',
  16612. '});',
  16613. '']),
  16614. LinesToStr([ // $mod.$main
  16615. '']));
  16616. end;
  16617. procedure TTestModule.TestClassInterface_MethodOverride;
  16618. begin
  16619. StartProgram(false);
  16620. Add([
  16621. '{$interfaces corba}',
  16622. 'type',
  16623. ' IUnknown = interface',
  16624. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  16625. ' procedure Go;',
  16626. ' end;',
  16627. ' TObject = class(IUnknown)',
  16628. ' procedure Go; virtual; abstract;',
  16629. ' end;',
  16630. ' TBird = class',
  16631. ' procedure Go; override;',
  16632. ' end;',
  16633. ' TCat = class(TObject)',
  16634. ' procedure Go; override;',
  16635. ' end;',
  16636. ' TDog = class(TObject, IUnknown)',
  16637. ' procedure Go; override;',
  16638. ' end;',
  16639. 'procedure TBird.Go; begin end;',
  16640. 'procedure TCat.Go; begin end;',
  16641. 'procedure TDog.Go; begin end;',
  16642. 'begin',
  16643. '']);
  16644. ConvertProgram;
  16645. CheckSource('TestClassInterface_MethodOverride',
  16646. LinesToStr([ // statements
  16647. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  16648. 'rtl.createClass($mod, "TObject", null, function () {',
  16649. ' this.$init = function () {',
  16650. ' };',
  16651. ' this.$final = function () {',
  16652. ' };',
  16653. ' rtl.addIntf(this, $mod.IUnknown);',
  16654. '});',
  16655. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16656. ' this.Go = function () {',
  16657. ' };',
  16658. ' rtl.addIntf(this, $mod.IUnknown);',
  16659. '});',
  16660. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  16661. ' this.Go = function () {',
  16662. ' };',
  16663. ' rtl.addIntf(this, $mod.IUnknown);',
  16664. '});',
  16665. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  16666. ' this.Go = function () {',
  16667. ' };',
  16668. ' rtl.addIntf(this, $mod.IUnknown);',
  16669. '});',
  16670. '']),
  16671. LinesToStr([ // $mod.$main
  16672. '']));
  16673. end;
  16674. procedure TTestModule.TestClassInterface_Corba_Delegation;
  16675. begin
  16676. StartProgram(false);
  16677. Add([
  16678. '{$interfaces corba}',
  16679. 'type',
  16680. ' IUnknown = interface',
  16681. ' end;',
  16682. ' IBird = interface(IUnknown)',
  16683. ' procedure Fly(s: string);',
  16684. ' end;',
  16685. ' IEagle = interface(IBird)',
  16686. ' end;',
  16687. ' IDove = interface(IBird)',
  16688. ' end;',
  16689. ' ISwallow = interface(IBird)',
  16690. ' end;',
  16691. ' TObject = class',
  16692. ' end;',
  16693. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  16694. ' procedure Fly(s: string); virtual; abstract;',
  16695. ' end;',
  16696. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  16697. ' FBirdIntf: IBird;',
  16698. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  16699. ' function GetEagleIntf: IEagle; virtual; abstract;',
  16700. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  16701. ' FDoveObj: TBird;',
  16702. ' property DoveObj: TBird read FDoveObj implements IDove;',
  16703. ' function GetSwallowObj: TBird; virtual; abstract;',
  16704. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  16705. ' end;',
  16706. 'begin',
  16707. '']);
  16708. ConvertProgram;
  16709. CheckSource('TestClassInterface_Delegation',
  16710. LinesToStr([ // statements
  16711. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16712. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  16713. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  16714. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  16715. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  16716. 'rtl.createClass($mod, "TObject", null, function () {',
  16717. ' this.$init = function () {',
  16718. ' };',
  16719. ' this.$final = function () {',
  16720. ' };',
  16721. '});',
  16722. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16723. ' rtl.addIntf(this, $mod.IBird);',
  16724. ' rtl.addIntf(this, $mod.IEagle);',
  16725. ' rtl.addIntf(this, $mod.IDove);',
  16726. ' rtl.addIntf(this, $mod.ISwallow);',
  16727. '});',
  16728. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  16729. ' this.$init = function () {',
  16730. ' $mod.TObject.$init.call(this);',
  16731. ' this.FBirdIntf = null;',
  16732. ' this.FDoveObj = null;',
  16733. ' };',
  16734. ' this.$final = function () {',
  16735. ' this.FBirdIntf = undefined;',
  16736. ' this.FDoveObj = undefined;',
  16737. ' $mod.TObject.$final.call(this);',
  16738. ' };',
  16739. ' this.$intfmaps = {',
  16740. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  16741. ' return this.FBirdIntf;',
  16742. ' },',
  16743. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  16744. ' return this.GetEagleIntf();',
  16745. ' },',
  16746. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  16747. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  16748. ' },',
  16749. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  16750. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  16751. ' }',
  16752. ' };',
  16753. '});',
  16754. '']),
  16755. LinesToStr([ // $mod.$main
  16756. '']));
  16757. end;
  16758. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  16759. begin
  16760. StartProgram(false);
  16761. Add([
  16762. '{$interfaces corba}',
  16763. 'type',
  16764. ' IUnknown = interface',
  16765. ' end;',
  16766. ' IBird = interface(IUnknown)',
  16767. ' procedure Fly(s: string);',
  16768. ' end;',
  16769. ' IEagle = interface(IBird)',
  16770. ' end;',
  16771. ' IDove = interface(IBird)',
  16772. ' end;',
  16773. ' ISwallow = interface(IBird)',
  16774. ' end;',
  16775. ' TObject = class',
  16776. ' end;',
  16777. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  16778. ' procedure Fly(s: string); virtual; abstract;',
  16779. ' end;',
  16780. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  16781. ' private',
  16782. ' class var FBirdIntf: IBird;',
  16783. ' class var FDoveObj: TBird;',
  16784. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  16785. ' class function GetSwallowObj: TBird; virtual; abstract;',
  16786. ' protected',
  16787. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  16788. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  16789. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  16790. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  16791. ' end;',
  16792. 'begin',
  16793. '']);
  16794. ConvertProgram;
  16795. CheckSource('TestClassInterface_DelegationStatic',
  16796. LinesToStr([ // statements
  16797. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16798. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  16799. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  16800. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  16801. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  16802. 'rtl.createClass($mod, "TObject", null, function () {',
  16803. ' this.$init = function () {',
  16804. ' };',
  16805. ' this.$final = function () {',
  16806. ' };',
  16807. '});',
  16808. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16809. ' rtl.addIntf(this, $mod.IBird);',
  16810. ' rtl.addIntf(this, $mod.IEagle);',
  16811. ' rtl.addIntf(this, $mod.IDove);',
  16812. ' rtl.addIntf(this, $mod.ISwallow);',
  16813. '});',
  16814. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  16815. ' this.FBirdIntf = null;',
  16816. ' this.FDoveObj = null;',
  16817. ' this.$intfmaps = {',
  16818. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  16819. ' return this.FBirdIntf;',
  16820. ' },',
  16821. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  16822. ' return this.$class.GetEagleIntf();',
  16823. ' },',
  16824. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  16825. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  16826. ' },',
  16827. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  16828. ' return rtl.getIntfT(this.$class.GetSwallowObj(), $mod.ISwallow);',
  16829. ' }',
  16830. ' };',
  16831. '});',
  16832. '']),
  16833. LinesToStr([ // $mod.$main
  16834. '']));
  16835. end;
  16836. procedure TTestModule.TestClassInterface_Corba_Operators;
  16837. begin
  16838. StartProgram(false);
  16839. Add([
  16840. '{$interfaces corba}',
  16841. 'type',
  16842. ' IUnknown = interface',
  16843. ' end;',
  16844. ' IBird = interface(IUnknown)',
  16845. ' function GetItems(Index: longint): longint;',
  16846. ' procedure SetItems(Index: longint; Value: longint);',
  16847. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  16848. ' end;',
  16849. ' TObject = class',
  16850. ' end;',
  16851. ' TBird = class(TObject,IBird)',
  16852. ' function GetItems(Index: longint): longint; virtual; abstract;',
  16853. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  16854. ' end;',
  16855. 'var',
  16856. ' IntfVar: IBird = nil;',
  16857. ' IntfVar2: IBird;',
  16858. ' ObjVar: TBird;',
  16859. ' v: JSValue;',
  16860. 'begin',
  16861. ' IntfVar:=nil;',
  16862. ' IntfVar[3]:=IntfVar[4];',
  16863. ' if Assigned(IntfVar) then ;',
  16864. ' IntfVar:=IntfVar2;',
  16865. ' IntfVar:=ObjVar;',
  16866. ' if IntfVar=IntfVar2 then ;',
  16867. ' if IntfVar<>IntfVar2 then ;',
  16868. ' if IntfVar is IBird then ;',
  16869. ' if IntfVar is TBird then ;',
  16870. ' if ObjVar is IBird then ;',
  16871. ' IntfVar:=IntfVar2 as IBird;',
  16872. ' ObjVar:=IntfVar2 as TBird;',
  16873. ' IntfVar:=ObjVar as IBird;',
  16874. ' IntfVar:=IBird(IntfVar2);',
  16875. ' ObjVar:=TBird(IntfVar);',
  16876. ' IntfVar:=IBird(ObjVar);',
  16877. ' v:=IntfVar;',
  16878. ' IntfVar:=IBird(v);',
  16879. ' if v is IBird then ;',
  16880. ' v:=JSValue(IntfVar);',
  16881. ' v:=IBird;',
  16882. '']);
  16883. ConvertProgram;
  16884. CheckSource('TestClassInterface_Corba_Operators',
  16885. LinesToStr([ // statements
  16886. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16887. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  16888. 'rtl.createClass($mod, "TObject", null, function () {',
  16889. ' this.$init = function () {',
  16890. ' };',
  16891. ' this.$final = function () {',
  16892. ' };',
  16893. '});',
  16894. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16895. ' rtl.addIntf(this, $mod.IBird);',
  16896. '});',
  16897. 'this.IntfVar = null;',
  16898. 'this.IntfVar2 = null;',
  16899. 'this.ObjVar = null;',
  16900. 'this.v = undefined;',
  16901. '']),
  16902. LinesToStr([ // $mod.$main
  16903. '$mod.IntfVar = null;',
  16904. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  16905. 'if ($mod.IntfVar != null) ;',
  16906. '$mod.IntfVar = $mod.IntfVar2;',
  16907. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  16908. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  16909. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  16910. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  16911. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  16912. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  16913. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  16914. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  16915. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  16916. '$mod.IntfVar = $mod.IntfVar2;',
  16917. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  16918. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  16919. '$mod.v = $mod.IntfVar;',
  16920. '$mod.IntfVar = rtl.getObject($mod.v);',
  16921. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  16922. '$mod.v = $mod.IntfVar;',
  16923. '$mod.v = $mod.IBird;',
  16924. '']));
  16925. end;
  16926. procedure TTestModule.TestClassInterface_Corba_Args;
  16927. begin
  16928. StartProgram(false);
  16929. Add([
  16930. '{$interfaces corba}',
  16931. 'type',
  16932. ' IUnknown = interface',
  16933. ' end;',
  16934. ' IBird = interface(IUnknown)',
  16935. ' end;',
  16936. ' TObject = class',
  16937. ' end;',
  16938. ' TBird = class(TObject,IBird)',
  16939. ' end;',
  16940. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  16941. 'begin',
  16942. ' DoIt(i,i,i);',
  16943. 'end;',
  16944. 'procedure Change(var i: IBird; out j: IBird);',
  16945. 'begin',
  16946. ' DoIt(i,i,i);',
  16947. ' Change(i,i);',
  16948. 'end;',
  16949. 'var',
  16950. ' i: IBird;',
  16951. ' o: TBird;',
  16952. 'begin',
  16953. ' DoIt(i,i,i);',
  16954. ' Change(i,i);',
  16955. ' DoIt(o,o,o);',
  16956. '']);
  16957. ConvertProgram;
  16958. CheckSource('TestClassInterface_Corba_Args',
  16959. LinesToStr([ // statements
  16960. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16961. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  16962. 'rtl.createClass($mod, "TObject", null, function () {',
  16963. ' this.$init = function () {',
  16964. ' };',
  16965. ' this.$final = function () {',
  16966. ' };',
  16967. '});',
  16968. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16969. ' rtl.addIntf(this, $mod.IBird);',
  16970. '});',
  16971. 'this.DoIt = function (u, i, j) {',
  16972. ' $mod.DoIt({',
  16973. ' get: function () {',
  16974. ' return i;',
  16975. ' },',
  16976. ' set: function (v) {',
  16977. ' i = v;',
  16978. ' }',
  16979. ' }, i, i);',
  16980. '};',
  16981. 'this.Change = function (i, j) {',
  16982. ' $mod.DoIt(i, i.get(), i.get());',
  16983. ' $mod.Change(i, i);',
  16984. '};',
  16985. 'this.i = null;',
  16986. 'this.o = null;',
  16987. '']),
  16988. LinesToStr([ // $mod.$main
  16989. '$mod.DoIt({',
  16990. ' p: $mod,',
  16991. ' get: function () {',
  16992. ' return this.p.i;',
  16993. ' },',
  16994. ' set: function (v) {',
  16995. ' this.p.i = v;',
  16996. ' }',
  16997. '}, $mod.i, $mod.i);',
  16998. '$mod.Change({',
  16999. ' p: $mod,',
  17000. ' get: function () {',
  17001. ' return this.p.i;',
  17002. ' },',
  17003. ' set: function (v) {',
  17004. ' this.p.i = v;',
  17005. ' }',
  17006. '}, {',
  17007. ' p: $mod,',
  17008. ' get: function () {',
  17009. ' return this.p.i;',
  17010. ' },',
  17011. ' set: function (v) {',
  17012. ' this.p.i = v;',
  17013. ' }',
  17014. '});',
  17015. '$mod.DoIt({',
  17016. ' p: $mod,',
  17017. ' get: function () {',
  17018. ' return this.p.o;',
  17019. ' },',
  17020. ' set: function (v) {',
  17021. ' this.p.o = v;',
  17022. ' }',
  17023. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  17024. '']));
  17025. end;
  17026. procedure TTestModule.TestClassInterface_Corba_ForIn;
  17027. begin
  17028. StartProgram(false);
  17029. Add([
  17030. '{$interfaces corba}',
  17031. 'type',
  17032. ' IUnknown = interface end;',
  17033. ' TObject = class',
  17034. ' Id: longint;',
  17035. ' end;',
  17036. ' IEnumerator = interface(IUnknown)',
  17037. ' function GetCurrent: TObject;',
  17038. ' function MoveNext: Boolean;',
  17039. ' property Current: TObject read GetCurrent;',
  17040. ' end;',
  17041. ' IEnumerable = interface(IUnknown)',
  17042. ' function GetEnumerator: IEnumerator;',
  17043. ' end;',
  17044. 'var',
  17045. ' o: TObject;',
  17046. ' i: IEnumerable;',
  17047. 'begin',
  17048. ' for o in i do o.Id:=3;',
  17049. '']);
  17050. ConvertProgram;
  17051. CheckSource('TestClassInterface_Corba_ForIn',
  17052. LinesToStr([ // statements
  17053. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17054. 'rtl.createClass($mod, "TObject", null, function () {',
  17055. ' this.$init = function () {',
  17056. ' this.Id = 0;',
  17057. ' };',
  17058. ' this.$final = function () {',
  17059. ' };',
  17060. '});',
  17061. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  17062. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  17063. 'this.o = null;',
  17064. 'this.i = null;',
  17065. '']),
  17066. LinesToStr([ // $mod.$main
  17067. 'var $in1 = $mod.i.GetEnumerator();',
  17068. 'while ($in1.MoveNext()) {',
  17069. ' $mod.o = $in1.GetCurrent();',
  17070. ' $mod.o.Id = 3;',
  17071. '};',
  17072. '']));
  17073. end;
  17074. procedure TTestModule.TestClassInterface_COM_AssignVar;
  17075. begin
  17076. StartProgram(false);
  17077. Add([
  17078. '{$interfaces com}',
  17079. 'type',
  17080. ' IUnknown = interface',
  17081. ' function _AddRef: longint;',
  17082. ' function _Release: longint;',
  17083. ' end;',
  17084. ' TObject = class(IUnknown)',
  17085. ' function _AddRef: longint; virtual; abstract;',
  17086. ' function _Release: longint; virtual; abstract;',
  17087. ' end;',
  17088. 'var',
  17089. ' i: IUnknown;',
  17090. 'procedure DoGlobal(o: TObject);',
  17091. 'begin',
  17092. ' i:=nil;',
  17093. ' i:=o;',
  17094. ' i:=i;',
  17095. 'end;',
  17096. 'procedure DoLocal(o: TObject);',
  17097. 'const k: IUnknown = nil;',
  17098. 'var j: IUnknown;',
  17099. 'begin',
  17100. ' k:=o;',
  17101. ' k:=i;',
  17102. ' j:=o;',
  17103. ' j:=i;',
  17104. 'end;',
  17105. 'var o: TObject;',
  17106. 'begin',
  17107. ' i:=nil;',
  17108. ' i:=o;',
  17109. '']);
  17110. ConvertProgram;
  17111. CheckSource('TestClassInterface_COM_AssignVar',
  17112. LinesToStr([ // statements
  17113. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17114. 'rtl.createClass($mod, "TObject", null, function () {',
  17115. ' this.$init = function () {',
  17116. ' };',
  17117. ' this.$final = function () {',
  17118. ' };',
  17119. ' rtl.addIntf(this, $mod.IUnknown);',
  17120. '});',
  17121. 'this.i = null;',
  17122. 'this.DoGlobal = function (o) {',
  17123. ' rtl.setIntfP($mod, "i", null);',
  17124. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  17125. ' rtl.setIntfP($mod, "i", $mod.i);',
  17126. '};',
  17127. 'var k = null;',
  17128. 'this.DoLocal = function (o) {',
  17129. ' var j = null;',
  17130. ' try{',
  17131. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17132. ' k = rtl.setIntfL(k, $mod.i);',
  17133. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17134. ' j = rtl.setIntfL(j, $mod.i);',
  17135. ' }finally{',
  17136. ' rtl._Release(j);',
  17137. ' };',
  17138. '};',
  17139. 'this.o = null;',
  17140. '']),
  17141. LinesToStr([ // $mod.$main
  17142. 'rtl.setIntfP($mod, "i", null);',
  17143. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  17144. '']));
  17145. end;
  17146. procedure TTestModule.TestClassInterface_COM_AssignArg;
  17147. begin
  17148. StartProgram(false);
  17149. Add([
  17150. '{$interfaces com}',
  17151. 'type',
  17152. ' IUnknown = interface',
  17153. ' function _AddRef: longint;',
  17154. ' function _Release: longint;',
  17155. ' end;',
  17156. ' TObject = class(IUnknown)',
  17157. ' function _AddRef: longint; virtual; abstract;',
  17158. ' function _Release: longint; virtual; abstract;',
  17159. ' end;',
  17160. 'procedure DoDefault(i, j: IUnknown);',
  17161. 'begin',
  17162. ' i:=nil;',
  17163. ' i:=j;',
  17164. 'end;',
  17165. 'begin',
  17166. '']);
  17167. ConvertProgram;
  17168. CheckSource('TestClassInterface_COM_AssignArg',
  17169. LinesToStr([ // statements
  17170. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17171. 'rtl.createClass($mod, "TObject", null, function () {',
  17172. ' this.$init = function () {',
  17173. ' };',
  17174. ' this.$final = function () {',
  17175. ' };',
  17176. ' rtl.addIntf(this, $mod.IUnknown);',
  17177. '});',
  17178. 'this.DoDefault = function (i, j) {',
  17179. ' rtl._AddRef(i);',
  17180. ' try {',
  17181. ' i = rtl.setIntfL(i, null);',
  17182. ' i = rtl.setIntfL(i, j);',
  17183. ' } finally {',
  17184. ' rtl._Release(i);',
  17185. ' };',
  17186. '};',
  17187. '']),
  17188. LinesToStr([ // $mod.$main
  17189. '']));
  17190. end;
  17191. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  17192. begin
  17193. StartProgram(false);
  17194. Add([
  17195. '{$interfaces com}',
  17196. 'type',
  17197. ' IUnknown = interface',
  17198. ' function _AddRef: longint;',
  17199. ' function _Release: longint;',
  17200. ' end;',
  17201. ' TObject = class(IUnknown)',
  17202. ' function _AddRef: longint; virtual; abstract;',
  17203. ' function _Release: longint; virtual; abstract;',
  17204. ' end;',
  17205. 'function DoDefault(i: IUnknown): IUnknown;',
  17206. 'begin',
  17207. ' Result:=i;',
  17208. ' if Result<>nil then exit;',
  17209. 'end;',
  17210. 'begin',
  17211. '']);
  17212. ConvertProgram;
  17213. CheckSource('TestClassInterface_COM_FunctionResult',
  17214. LinesToStr([ // statements
  17215. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17216. 'rtl.createClass($mod, "TObject", null, function () {',
  17217. ' this.$init = function () {',
  17218. ' };',
  17219. ' this.$final = function () {',
  17220. ' };',
  17221. ' rtl.addIntf(this, $mod.IUnknown);',
  17222. '});',
  17223. 'this.DoDefault = function (i) {',
  17224. ' var Result = null;',
  17225. ' var $ok = false;',
  17226. ' try {',
  17227. ' Result = rtl.setIntfL(Result, i);',
  17228. ' if(Result !== null){',
  17229. ' $ok = true;',
  17230. ' return Result;',
  17231. ' };',
  17232. ' $ok = true;',
  17233. ' } finally {',
  17234. ' if(!$ok) rtl._Release(Result);',
  17235. ' };',
  17236. ' return Result;',
  17237. '};',
  17238. '']),
  17239. LinesToStr([ // $mod.$main
  17240. '']));
  17241. end;
  17242. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  17243. begin
  17244. StartProgram(false);
  17245. Add([
  17246. '{$interfaces com}',
  17247. 'type',
  17248. ' IUnknown = interface',
  17249. ' function _AddRef: longint;',
  17250. ' function _Release: longint;',
  17251. ' end;',
  17252. ' TObject = class(IUnknown)',
  17253. ' function _AddRef: longint; virtual; abstract;',
  17254. ' function _Release: longint; virtual; abstract;',
  17255. ' function GetIntf: IUnknown; virtual;',
  17256. ' end;',
  17257. ' TMouse = class',
  17258. ' function GetIntf: IUnknown; override;',
  17259. ' end;',
  17260. 'function TObject.GetIntf: IUnknown; begin end;',
  17261. 'function TMouse.GetIntf: IUnknown;',
  17262. 'var i: IUnknown;',
  17263. 'begin',
  17264. ' inherited;',
  17265. ' inherited GetIntf;',
  17266. ' inherited GetIntf();',
  17267. ' Result:=inherited GetIntf;',
  17268. ' Result:=inherited GetIntf();',
  17269. ' i:=inherited GetIntf;',
  17270. ' i:=inherited GetIntf();',
  17271. 'end;',
  17272. 'begin',
  17273. '']);
  17274. ConvertProgram;
  17275. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  17276. LinesToStr([ // statements
  17277. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17278. 'rtl.createClass($mod, "TObject", null, function () {',
  17279. ' this.$init = function () {',
  17280. ' };',
  17281. ' this.$final = function () {',
  17282. ' };',
  17283. ' this.GetIntf = function () {',
  17284. ' var Result = null;',
  17285. ' return Result;',
  17286. ' };',
  17287. ' rtl.addIntf(this, $mod.IUnknown);',
  17288. '});',
  17289. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  17290. ' this.GetIntf = function () {',
  17291. ' var Result = null;',
  17292. ' var i = null;',
  17293. ' var $ir = rtl.createIntfRefs();',
  17294. ' var $ok = false;',
  17295. ' try {',
  17296. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  17297. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  17298. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  17299. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  17300. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  17301. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  17302. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  17303. ' $ok = true;',
  17304. ' } finally {',
  17305. ' $ir.free();',
  17306. ' rtl._Release(i);',
  17307. ' if (!$ok) rtl._Release(Result);',
  17308. ' };',
  17309. ' return Result;',
  17310. ' };',
  17311. ' rtl.addIntf(this, $mod.IUnknown);',
  17312. '});',
  17313. '']),
  17314. LinesToStr([ // $mod.$main
  17315. '']));
  17316. end;
  17317. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  17318. begin
  17319. StartProgram(false);
  17320. Add([
  17321. '{$interfaces com}',
  17322. 'type',
  17323. ' IUnknown = interface',
  17324. ' function _AddRef: longint;',
  17325. ' function _Release: longint;',
  17326. ' end;',
  17327. ' TObject = class(IUnknown)',
  17328. ' function _AddRef: longint; virtual; abstract;',
  17329. ' function _Release: longint; virtual; abstract;',
  17330. ' end;',
  17331. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  17332. 'begin',
  17333. ' if i is IUnknown then ;',
  17334. ' if o is IUnknown then ;',
  17335. ' if i is TObject then ;',
  17336. ' i:=j as IUnknown;',
  17337. ' i:=o as IUnknown;',
  17338. ' o:=j as TObject;',
  17339. ' i:=IUnknown(j);',
  17340. ' i:=IUnknown(o);',
  17341. ' o:=TObject(i);',
  17342. 'end;',
  17343. 'begin',
  17344. '']);
  17345. ConvertProgram;
  17346. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  17347. LinesToStr([ // statements
  17348. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17349. 'rtl.createClass($mod, "TObject", null, function () {',
  17350. ' this.$init = function () {',
  17351. ' };',
  17352. ' this.$final = function () {',
  17353. ' };',
  17354. ' rtl.addIntf(this, $mod.IUnknown);',
  17355. '});',
  17356. 'this.DoDefault = function (i, j, o) {',
  17357. ' rtl._AddRef(i);',
  17358. ' try {',
  17359. ' if ($mod.IUnknown.isPrototypeOf(i)) ;',
  17360. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  17361. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  17362. ' i = rtl.setIntfL(i, rtl.as(j, $mod.IUnknown));',
  17363. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17364. ' o = rtl.intfAsClass(j, $mod.TObject);',
  17365. ' i = rtl.setIntfL(i, j);',
  17366. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17367. ' o = rtl.intfToClass(i, $mod.TObject);',
  17368. ' } finally {',
  17369. ' rtl._Release(i);',
  17370. ' };',
  17371. '};',
  17372. '']),
  17373. LinesToStr([ // $mod.$main
  17374. '']));
  17375. end;
  17376. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  17377. begin
  17378. StartProgram(false);
  17379. Add([
  17380. '{$interfaces com}',
  17381. 'type',
  17382. ' IUnknown = interface',
  17383. ' function _AddRef: longint;',
  17384. ' function _Release: longint;',
  17385. ' end;',
  17386. ' TObject = class(IUnknown)',
  17387. ' function _AddRef: longint; virtual; abstract;',
  17388. ' function _Release: longint; virtual; abstract;',
  17389. ' end;',
  17390. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  17391. 'var o: TObject;',
  17392. 'begin',
  17393. ' DoIt(v,v,v,v);',
  17394. ' DoIt(o,o,k,k);',
  17395. 'end;',
  17396. 'procedure DoSome;',
  17397. 'var v: IUnknown;',
  17398. 'begin',
  17399. ' DoIt(v,v,v,v);',
  17400. 'end;',
  17401. 'var i: IUnknown;',
  17402. 'begin',
  17403. ' DoIt(i,i,i,i);',
  17404. '']);
  17405. ConvertProgram;
  17406. CheckSource('TestClassInterface_COM_PassAsArg',
  17407. LinesToStr([ // statements
  17408. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17409. 'rtl.createClass($mod, "TObject", null, function () {',
  17410. ' this.$init = function () {',
  17411. ' };',
  17412. ' this.$final = function () {',
  17413. ' };',
  17414. ' rtl.addIntf(this, $mod.IUnknown);',
  17415. '});',
  17416. 'this.DoIt = function (v, j, k, l) {',
  17417. ' var o = null;',
  17418. ' var $ir = rtl.createIntfRefs();',
  17419. ' rtl._AddRef(v);',
  17420. ' try {',
  17421. ' $mod.DoIt(v, v, {',
  17422. ' get: function () {',
  17423. ' return v;',
  17424. ' },',
  17425. ' set: function (w) {',
  17426. ' v = rtl.setIntfL(v, w);',
  17427. ' }',
  17428. ' }, {',
  17429. ' get: function () {',
  17430. ' return v;',
  17431. ' },',
  17432. ' set: function (w) {',
  17433. ' v = rtl.setIntfL(v, w);',
  17434. ' }',
  17435. ' });',
  17436. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  17437. ' } finally {',
  17438. ' $ir.free();',
  17439. ' rtl._Release(v);',
  17440. ' };',
  17441. '};',
  17442. 'this.DoSome = function () {',
  17443. ' var v = null;',
  17444. ' try {',
  17445. ' $mod.DoIt(v, v, {',
  17446. ' get: function () {',
  17447. ' return v;',
  17448. ' },',
  17449. ' set: function (w) {',
  17450. ' v = rtl.setIntfL(v, w);',
  17451. ' }',
  17452. ' }, {',
  17453. ' get: function () {',
  17454. ' return v;',
  17455. ' },',
  17456. ' set: function (w) {',
  17457. ' v = rtl.setIntfL(v, w);',
  17458. ' }',
  17459. ' });',
  17460. ' } finally {',
  17461. ' rtl._Release(v);',
  17462. ' };',
  17463. '};',
  17464. 'this.i = null;',
  17465. '']),
  17466. LinesToStr([ // $mod.$main
  17467. '$mod.DoIt($mod.i, $mod.i, {',
  17468. ' p: $mod,',
  17469. ' get: function () {',
  17470. ' return this.p.i;',
  17471. ' },',
  17472. ' set: function (v) {',
  17473. ' rtl.setIntfP(this.p, "i", v);',
  17474. ' }',
  17475. '}, {',
  17476. ' p: $mod,',
  17477. ' get: function () {',
  17478. ' return this.p.i;',
  17479. ' },',
  17480. ' set: function (v) {',
  17481. ' rtl.setIntfP(this.p, "i", v);',
  17482. ' }',
  17483. '});',
  17484. '']));
  17485. end;
  17486. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  17487. begin
  17488. StartProgram(false);
  17489. Add([
  17490. '{$interfaces com}',
  17491. 'type',
  17492. ' IUnknown = interface',
  17493. ' function _AddRef: longint;',
  17494. ' function _Release: longint;',
  17495. ' end;',
  17496. ' TObject = class(IUnknown)',
  17497. ' function _AddRef: longint; virtual; abstract;',
  17498. ' function _Release: longint; virtual; abstract;',
  17499. ' end;',
  17500. 'procedure DoIt(out i);',
  17501. 'begin end;',
  17502. 'procedure DoSome;',
  17503. 'var v: IUnknown;',
  17504. 'begin',
  17505. ' DoIt(v);',
  17506. 'end;',
  17507. 'function GetIt: IUnknown;',
  17508. 'begin',
  17509. ' DoIt(Result);',
  17510. 'end;',
  17511. 'var i: IUnknown;',
  17512. 'begin',
  17513. ' DoIt(i);',
  17514. '']);
  17515. ConvertProgram;
  17516. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  17517. LinesToStr([ // statements
  17518. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17519. 'rtl.createClass($mod, "TObject", null, function () {',
  17520. ' this.$init = function () {',
  17521. ' };',
  17522. ' this.$final = function () {',
  17523. ' };',
  17524. ' rtl.addIntf(this, $mod.IUnknown);',
  17525. '});',
  17526. 'this.DoIt = function (i) {',
  17527. '};',
  17528. 'this.DoSome = function () {',
  17529. ' var v = null;',
  17530. ' try {',
  17531. ' $mod.DoIt({',
  17532. ' get: function () {',
  17533. ' return v;',
  17534. ' },',
  17535. ' set: function (w) {',
  17536. ' v = w;',
  17537. ' }',
  17538. ' });',
  17539. ' } finally {',
  17540. ' rtl._Release(v);',
  17541. ' };',
  17542. '};',
  17543. 'this.GetIt = function () {',
  17544. ' var Result = null;',
  17545. ' var $ok = false;',
  17546. ' try {',
  17547. ' $mod.DoIt({',
  17548. ' get: function () {',
  17549. ' return Result;',
  17550. ' },',
  17551. ' set: function (v) {',
  17552. ' Result = v;',
  17553. ' }',
  17554. ' });',
  17555. ' $ok = true;',
  17556. ' } finally {',
  17557. ' if (!$ok) rtl._Release(Result);',
  17558. ' };',
  17559. ' return Result;',
  17560. '};',
  17561. 'this.i = null;',
  17562. '']),
  17563. LinesToStr([ // $mod.$main
  17564. 'try {',
  17565. ' $mod.DoIt({',
  17566. ' p: $mod,',
  17567. ' get: function () {',
  17568. ' return this.p.i;',
  17569. ' },',
  17570. ' set: function (v) {',
  17571. ' this.p.i = v;',
  17572. ' }',
  17573. ' });',
  17574. '} finally {',
  17575. ' rtl._Release($mod.i);',
  17576. '};',
  17577. '']));
  17578. end;
  17579. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  17580. begin
  17581. StartProgram(false);
  17582. Add([
  17583. '{$interfaces com}',
  17584. 'type',
  17585. ' IUnknown = interface',
  17586. ' function _AddRef: longint;',
  17587. ' function _Release: longint;',
  17588. ' end;',
  17589. ' TObject = class(IUnknown)',
  17590. ' function _AddRef: longint; virtual; abstract;',
  17591. ' function _Release: longint; virtual; abstract;',
  17592. ' end;',
  17593. 'function GetIt: IUnknown;',
  17594. 'begin',
  17595. 'end;',
  17596. 'procedure DoSome;',
  17597. 'var v: IUnknown;',
  17598. ' i: longint;',
  17599. 'begin',
  17600. ' v:=GetIt;',
  17601. ' v:=GetIt();',
  17602. ' GetIt()._AddRef;',
  17603. ' i:=GetIt()._AddRef;',
  17604. 'end;',
  17605. 'var v: IUnknown;',
  17606. ' i: longint;',
  17607. 'begin',
  17608. ' v:=GetIt;',
  17609. ' v:=GetIt();',
  17610. ' GetIt()._AddRef;',
  17611. ' i:=GetIt()._AddRef;',
  17612. '']);
  17613. ConvertProgram;
  17614. CheckSource('TestClassInterface_COM_FunctionInExpr',
  17615. LinesToStr([ // statements
  17616. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17617. 'rtl.createClass($mod, "TObject", null, function () {',
  17618. ' this.$init = function () {',
  17619. ' };',
  17620. ' this.$final = function () {',
  17621. ' };',
  17622. ' rtl.addIntf(this, $mod.IUnknown);',
  17623. '});',
  17624. 'this.GetIt = function () {',
  17625. ' var Result = null;',
  17626. ' return Result;',
  17627. '};',
  17628. 'this.DoSome = function () {',
  17629. ' var v = null;',
  17630. ' var i = 0;',
  17631. ' var $ir = rtl.createIntfRefs();',
  17632. ' try {',
  17633. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  17634. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  17635. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  17636. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  17637. ' } finally {',
  17638. ' $ir.free();',
  17639. ' rtl._Release(v);',
  17640. ' };',
  17641. '};',
  17642. 'this.v = null;',
  17643. 'this.i = 0;',
  17644. '']),
  17645. LinesToStr([ // $mod.$main
  17646. 'var $ir = rtl.createIntfRefs();',
  17647. 'try {',
  17648. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  17649. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  17650. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  17651. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  17652. '} finally {',
  17653. ' $ir.free();',
  17654. '};',
  17655. '']));
  17656. end;
  17657. procedure TTestModule.TestClassInterface_COM_Property;
  17658. begin
  17659. StartProgram(false);
  17660. Add([
  17661. '{$interfaces com}',
  17662. 'type',
  17663. ' IUnknown = interface',
  17664. ' function _AddRef: longint;',
  17665. ' function _Release: longint;',
  17666. ' end;',
  17667. ' TObject = class(IUnknown)',
  17668. ' FAnt: IUnknown;',
  17669. ' function _AddRef: longint; virtual; abstract;',
  17670. ' function _Release: longint; virtual; abstract;',
  17671. ' function GetBird: IUnknown; virtual; abstract;',
  17672. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  17673. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  17674. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  17675. ' property Ant: IUnknown read FAnt write FAnt;',
  17676. ' property Bird: IUnknown read GetBird write SetBird;',
  17677. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  17678. ' end;',
  17679. 'procedure DoIt;',
  17680. 'var',
  17681. ' o: TObject;',
  17682. ' v: IUnknown;',
  17683. 'begin',
  17684. ' v:=o.Ant;',
  17685. ' o.Ant:=v;',
  17686. ' o.Ant:=o.Ant;',
  17687. ' v:=o.Bird;',
  17688. ' o.Bird:=v;',
  17689. ' o.Bird:=o.Bird;',
  17690. ' v:=o.Items[1];',
  17691. ' o.Items[2]:=v;',
  17692. ' o.Items[3]:=o.Items[4];',
  17693. ' v:=o[5];',
  17694. ' o[6]:=v;',
  17695. ' o[7]:=o[8];',
  17696. 'end;',
  17697. 'begin',
  17698. '']);
  17699. ConvertProgram;
  17700. CheckSource('TestClassInterface_COM_Property',
  17701. LinesToStr([ // statements
  17702. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17703. 'rtl.createClass($mod, "TObject", null, function () {',
  17704. ' this.$init = function () {',
  17705. ' this.FAnt = null;',
  17706. ' };',
  17707. ' this.$final = function () {',
  17708. ' this.FAnt = undefined;',
  17709. ' };',
  17710. ' rtl.addIntf(this, $mod.IUnknown);',
  17711. '});',
  17712. 'this.DoIt = function () {',
  17713. ' var o = null;',
  17714. ' var v = null;',
  17715. ' var $ir = rtl.createIntfRefs();',
  17716. ' try {',
  17717. ' v = rtl.setIntfL(v, o.FAnt);',
  17718. ' rtl.setIntfP(o, "FAnt", v);',
  17719. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  17720. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  17721. ' o.SetBird(v);',
  17722. ' o.SetBird($ir.ref(1, o.GetBird()));',
  17723. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  17724. ' o.SetItems(2, v);',
  17725. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  17726. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  17727. ' o.SetItems(6, v);',
  17728. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  17729. ' } finally {',
  17730. ' $ir.free();',
  17731. ' rtl._Release(v);',
  17732. ' };',
  17733. '};',
  17734. '']),
  17735. LinesToStr([ // $mod.$main
  17736. '']));
  17737. end;
  17738. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  17739. begin
  17740. StartProgram(false);
  17741. Add([
  17742. '{$interfaces com}',
  17743. 'type',
  17744. ' IUnknown = interface',
  17745. ' function _AddRef: longint;',
  17746. ' function _Release: longint;',
  17747. ' function GetBird: IUnknown;',
  17748. ' procedure SetBird(Value: IUnknown);',
  17749. ' function GetItems(Index: longint): IUnknown;',
  17750. ' procedure SetItems(Index: longint; Value: IUnknown);',
  17751. ' property Bird: IUnknown read GetBird write SetBird;',
  17752. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  17753. ' end;',
  17754. ' TObject = class(IUnknown)',
  17755. ' function _AddRef: longint; virtual; abstract;',
  17756. ' function _Release: longint; virtual; abstract;',
  17757. ' function GetBird: IUnknown; virtual; abstract;',
  17758. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  17759. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  17760. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  17761. ' end;',
  17762. 'procedure DoIt;',
  17763. 'var',
  17764. ' o: TObject;',
  17765. ' v: IUnknown;',
  17766. 'begin',
  17767. ' v:=v.Items[1];',
  17768. ' v.Items[2]:=v;',
  17769. ' v.Items[3]:=v.Items[4];',
  17770. ' v:=v[5];',
  17771. ' v[6]:=v;',
  17772. ' v[7]:=v[8];',
  17773. ' v[9].Bird.Bird:=v;',
  17774. ' v:=v.Bird[10].Bird',
  17775. 'end;',
  17776. 'begin',
  17777. '']);
  17778. ConvertProgram;
  17779. CheckSource('TestClassInterface_COM_IntfProperty',
  17780. LinesToStr([ // statements
  17781. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  17782. ' "_AddRef",',
  17783. ' "_Release",',
  17784. ' "GetBird",',
  17785. ' "SetBird",',
  17786. ' "GetItems",',
  17787. ' "SetItems"',
  17788. '], null);',
  17789. 'rtl.createClass($mod, "TObject", null, function () {',
  17790. ' this.$init = function () {',
  17791. ' };',
  17792. ' this.$final = function () {',
  17793. ' };',
  17794. ' rtl.addIntf(this, $mod.IUnknown);',
  17795. '});',
  17796. 'this.DoIt = function () {',
  17797. ' var o = null;',
  17798. ' var v = null;',
  17799. ' var $ir = rtl.createIntfRefs();',
  17800. ' try {',
  17801. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  17802. ' v.SetItems(2, v);',
  17803. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  17804. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  17805. ' v.SetItems(6, v);',
  17806. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  17807. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  17808. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  17809. ' } finally {',
  17810. ' $ir.free();',
  17811. ' rtl._Release(v);',
  17812. ' };',
  17813. '};',
  17814. '']),
  17815. LinesToStr([ // $mod.$main
  17816. '']));
  17817. end;
  17818. procedure TTestModule.TestClassInterface_COM_Delegation;
  17819. begin
  17820. StartProgram(false);
  17821. Add([
  17822. '{$interfaces com}',
  17823. 'type',
  17824. ' IUnknown = interface',
  17825. ' function _AddRef: longint;',
  17826. ' function _Release: longint;',
  17827. ' end;',
  17828. ' IBird = interface(IUnknown)',
  17829. ' procedure Fly(s: string);',
  17830. ' end;',
  17831. ' IEagle = interface(IBird) end;',
  17832. ' IDove = interface(IBird) end;',
  17833. ' ISwallow = interface(IBird) end;',
  17834. ' TObject = class',
  17835. ' end;',
  17836. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17837. ' function _AddRef: longint; virtual; abstract;',
  17838. ' function _Release: longint; virtual; abstract;',
  17839. ' procedure Fly(s: string); virtual; abstract;',
  17840. ' end;',
  17841. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17842. ' function _AddRef: longint; virtual; abstract;',
  17843. ' function _Release: longint; virtual; abstract;',
  17844. ' FBirdIntf: IBird;',
  17845. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  17846. ' function GetEagleIntf: IEagle; virtual; abstract;',
  17847. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17848. ' FDoveObj: TBird;',
  17849. ' property DoveObj: TBird read FDoveObj implements IDove;',
  17850. ' function GetSwallowObj: TBird; virtual; abstract;',
  17851. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17852. ' end;',
  17853. 'begin',
  17854. '']);
  17855. ConvertProgram;
  17856. CheckSource('TestClassInterface_COM_Delegation',
  17857. LinesToStr([ // statements
  17858. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17859. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  17860. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  17861. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  17862. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  17863. 'rtl.createClass($mod, "TObject", null, function () {',
  17864. ' this.$init = function () {',
  17865. ' };',
  17866. ' this.$final = function () {',
  17867. ' };',
  17868. '});',
  17869. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17870. ' rtl.addIntf(this, $mod.IBird);',
  17871. ' rtl.addIntf(this, $mod.IEagle);',
  17872. ' rtl.addIntf(this, $mod.IDove);',
  17873. ' rtl.addIntf(this, $mod.ISwallow);',
  17874. '});',
  17875. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17876. ' this.$init = function () {',
  17877. ' $mod.TObject.$init.call(this);',
  17878. ' this.FBirdIntf = null;',
  17879. ' this.FDoveObj = null;',
  17880. ' };',
  17881. ' this.$final = function () {',
  17882. ' this.FBirdIntf = undefined;',
  17883. ' this.FDoveObj = undefined;',
  17884. ' $mod.TObject.$final.call(this);',
  17885. ' };',
  17886. ' this.$intfmaps = {',
  17887. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  17888. ' return rtl._AddRef(this.FBirdIntf);',
  17889. ' },',
  17890. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  17891. ' return this.GetEagleIntf();',
  17892. ' },',
  17893. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  17894. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  17895. ' },',
  17896. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  17897. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  17898. ' }',
  17899. ' };',
  17900. '});',
  17901. '']),
  17902. LinesToStr([ // $mod.$main
  17903. '']));
  17904. end;
  17905. procedure TTestModule.TestClassInterface_COM_With;
  17906. begin
  17907. StartProgram(false);
  17908. Add([
  17909. '{$interfaces com}',
  17910. 'type',
  17911. ' IUnknown = interface',
  17912. ' function _AddRef: longint;',
  17913. ' function _Release: longint;',
  17914. ' function GetAnt: IUnknown;',
  17915. ' property Ant: IUnknown read GetAnt;',
  17916. ' end;',
  17917. ' TObject = class(IUnknown)',
  17918. ' function _AddRef: longint; virtual; abstract;',
  17919. ' function _Release: longint; virtual; abstract;',
  17920. ' function GetAnt: IUnknown; virtual; abstract;',
  17921. ' property Ant: IUnknown read GetAnt;',
  17922. ' end;',
  17923. 'procedure DoIt;',
  17924. 'var',
  17925. ' i: IUnknown;',
  17926. 'begin',
  17927. ' with i do ',
  17928. ' GetAnt;',
  17929. ' with i.Ant, Ant do ',
  17930. ' GetAnt;',
  17931. 'end;',
  17932. 'begin',
  17933. '']);
  17934. ConvertProgram;
  17935. CheckSource('TestClassInterface_COM_With',
  17936. LinesToStr([ // statements
  17937. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  17938. 'rtl.createClass($mod, "TObject", null, function () {',
  17939. ' this.$init = function () {',
  17940. ' };',
  17941. ' this.$final = function () {',
  17942. ' };',
  17943. ' rtl.addIntf(this, $mod.IUnknown);',
  17944. '});',
  17945. 'this.DoIt = function () {',
  17946. ' var i = null;',
  17947. ' var $ir = rtl.createIntfRefs();',
  17948. ' try {',
  17949. ' $ir.ref(1, i.GetAnt());',
  17950. ' var $with1 = $ir.ref(2, i.GetAnt());',
  17951. ' var $with2 = $ir.ref(3, $with1.GetAnt());',
  17952. ' $ir.ref(4, $with2.GetAnt());',
  17953. ' } finally {',
  17954. ' $ir.free();',
  17955. ' };',
  17956. '};',
  17957. '']),
  17958. LinesToStr([ // $mod.$main
  17959. '']));
  17960. end;
  17961. procedure TTestModule.TestClassInterface_COM_ForIn;
  17962. begin
  17963. StartProgram(false);
  17964. Add([
  17965. '{$interfaces com}',
  17966. 'type',
  17967. ' IUnknown = interface end;',
  17968. ' TObject = class',
  17969. ' Id: longint;',
  17970. ' end;',
  17971. ' IEnumerator = interface(IUnknown)',
  17972. ' function GetCurrent: TObject;',
  17973. ' function MoveNext: Boolean;',
  17974. ' property Current: TObject read GetCurrent;',
  17975. ' end;',
  17976. ' IEnumerable = interface(IUnknown)',
  17977. ' function GetEnumerator: IEnumerator;',
  17978. ' end;',
  17979. 'var',
  17980. ' o: TObject;',
  17981. ' i: IEnumerable;',
  17982. 'begin',
  17983. ' for o in i do o.Id:=3;',
  17984. '']);
  17985. ConvertProgram;
  17986. CheckSource('TestClassInterface_COM_ForIn',
  17987. LinesToStr([ // statements
  17988. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17989. 'rtl.createClass($mod, "TObject", null, function () {',
  17990. ' this.$init = function () {',
  17991. ' this.Id = 0;',
  17992. ' };',
  17993. ' this.$final = function () {',
  17994. ' };',
  17995. '});',
  17996. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  17997. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  17998. 'this.o = null;',
  17999. 'this.i = null;',
  18000. '']),
  18001. LinesToStr([ // $mod.$main
  18002. 'var $in1 = $mod.i.GetEnumerator();',
  18003. 'try {',
  18004. ' while ($in1.MoveNext()) {',
  18005. ' $mod.o = $in1.GetCurrent();',
  18006. ' $mod.o.Id = 3;',
  18007. ' }',
  18008. '} finally {',
  18009. ' rtl._Release($in1)',
  18010. '};',
  18011. '']));
  18012. end;
  18013. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  18014. begin
  18015. StartProgram(false);
  18016. Add([
  18017. '{$interfaces com}',
  18018. 'type',
  18019. ' IUnknown = interface',
  18020. ' function _AddRef: longint;',
  18021. ' function _Release: longint;',
  18022. ' end;',
  18023. ' TObject = class',
  18024. ' end;',
  18025. ' TArrOfIntf = array of IUnknown;',
  18026. 'begin',
  18027. '']);
  18028. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  18029. ConvertProgram;
  18030. end;
  18031. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  18032. begin
  18033. StartProgram(false);
  18034. Add([
  18035. '{$interfaces com}',
  18036. 'type',
  18037. ' IUnknown = interface',
  18038. ' function _AddRef: longint;',
  18039. ' function _Release: longint;',
  18040. ' end;',
  18041. ' TRec = record',
  18042. ' i: IUnknown;',
  18043. ' end;',
  18044. 'begin',
  18045. '']);
  18046. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  18047. ConvertProgram;
  18048. end;
  18049. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  18050. begin
  18051. StartUnit(false);
  18052. Add([
  18053. '{$interfaces com}',
  18054. 'interface',
  18055. 'implementation',
  18056. 'type',
  18057. ' IUnknown = interface',
  18058. ' function _AddRef: longint;',
  18059. ' end;',
  18060. ' TObject = class(IUnknown)',
  18061. ' function _AddRef: longint;',
  18062. ' end;',
  18063. 'function TObject._AddRef: longint; begin end;',
  18064. 'var i: IUnknown;',
  18065. ' o: TObject;',
  18066. 'initialization',
  18067. ' i:=nil;',
  18068. ' i:=i;',
  18069. ' i:=o;',
  18070. ' if (o as IUnknown)=nil then ;',
  18071. '']);
  18072. ConvertUnit;
  18073. CheckSource('TestClassInterface_COM_UnitInitialization',
  18074. LinesToStr([ // statements
  18075. 'var $impl = $mod.$impl;',
  18076. '']),
  18077. LinesToStr([ // this.$init
  18078. 'var $ir = rtl.createIntfRefs();',
  18079. 'try {',
  18080. ' rtl.setIntfP($impl, "i", null);',
  18081. ' rtl.setIntfP($impl, "i", $impl.i);',
  18082. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  18083. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  18084. '} finally {',
  18085. ' $ir.free();',
  18086. '};',
  18087. '']),
  18088. LinesToStr([ // implementation
  18089. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  18090. 'rtl.createClass($impl, "TObject", null, function () {',
  18091. ' this.$init = function () {',
  18092. ' };',
  18093. ' this.$final = function () {',
  18094. ' };',
  18095. ' this._AddRef = function () {',
  18096. ' var Result = 0;',
  18097. ' return Result;',
  18098. ' };',
  18099. ' rtl.addIntf(this, $impl.IUnknown);',
  18100. '});',
  18101. '$impl.i = null;',
  18102. '$impl.o = null;',
  18103. ''])
  18104. );
  18105. end;
  18106. procedure TTestModule.TestClassInterface_GUID;
  18107. begin
  18108. StartProgram(false);
  18109. Add([
  18110. '{$interfaces corba}',
  18111. 'type',
  18112. ' IUnknown = interface',
  18113. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  18114. ' end;',
  18115. ' TObject = class end;',
  18116. ' TGUID = record D1, D2, D3, D4: word; end;',
  18117. ' TAliasGUID = TGUID;',
  18118. ' TGUIDString = type string;',
  18119. ' TAliasGUIDString = TGUIDString;',
  18120. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  18121. 'begin end;',
  18122. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  18123. 'begin end;',
  18124. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  18125. 'begin end;',
  18126. 'var',
  18127. ' i: IUnknown;',
  18128. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  18129. ' s: TAliasGUIDString;',
  18130. 'begin',
  18131. ' DoConstGUIDIt(IUnknown);',
  18132. ' DoDefGUID(IUnknown);',
  18133. ' DoStr(IUnknown);',
  18134. ' DoConstGUIDIt(i);',
  18135. ' DoDefGUID(i);',
  18136. ' DoStr(i);',
  18137. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  18138. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  18139. ' DoStr(g);',
  18140. ' g:=i;',
  18141. ' g:=IUnknown;',
  18142. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  18143. ' s:=i;',
  18144. ' s:=IUnknown;',
  18145. ' s:=g;',
  18146. ' if g=i then ;',
  18147. ' if i=g then ;',
  18148. ' if g=IUnknown then ;',
  18149. ' if IUnknown=g then ;',
  18150. ' if s=i then ;',
  18151. ' if i=s then ;',
  18152. ' if s=IUnknown then ;',
  18153. ' if IUnknown=s then ;',
  18154. ' if s=g then ;',
  18155. ' if g=s then ;',
  18156. '']);
  18157. ConvertProgram;
  18158. CheckSource('TestClassInterface_GUID',
  18159. LinesToStr([ // statements
  18160. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  18161. 'rtl.createClass($mod, "TObject", null, function () {',
  18162. ' this.$init = function () {',
  18163. ' };',
  18164. ' this.$final = function () {',
  18165. ' };',
  18166. '});',
  18167. 'rtl.recNewT($mod, "TGUID", function () {',
  18168. ' this.D1 = 0;',
  18169. ' this.D2 = 0;',
  18170. ' this.D3 = 0;',
  18171. ' this.D4 = 0;',
  18172. ' this.$eq = function (b) {',
  18173. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  18174. ' };',
  18175. ' this.$assign = function (s) {',
  18176. ' this.D1 = s.D1;',
  18177. ' this.D2 = s.D2;',
  18178. ' this.D3 = s.D3;',
  18179. ' this.D4 = s.D4;',
  18180. ' return this;',
  18181. ' };',
  18182. '});',
  18183. 'this.DoConstGUIDIt = function (g) {',
  18184. '};',
  18185. 'this.DoDefGUID = function (g) {',
  18186. '};',
  18187. 'this.DoStr = function (s) {',
  18188. '};',
  18189. 'this.i = null;',
  18190. 'this.g = $mod.TGUID.$clone({',
  18191. ' D1: 0xD91C9AF4,',
  18192. ' D2: 0x3C93,',
  18193. ' D3: 0x420F,',
  18194. ' D4: [',
  18195. ' 0xA3,',
  18196. ' 0x03,',
  18197. ' 0xBF,',
  18198. ' 0x5B,',
  18199. ' 0xA8,',
  18200. ' 0x2B,',
  18201. ' 0xFD,',
  18202. ' 0x23',
  18203. ' ]',
  18204. '});',
  18205. 'this.s = "";',
  18206. '']),
  18207. LinesToStr([ // $mod.$main
  18208. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  18209. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  18210. '$mod.DoStr($mod.IUnknown.$guid);',
  18211. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  18212. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  18213. '$mod.DoStr($mod.i.$guid);',
  18214. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  18215. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  18216. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  18217. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  18218. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  18219. '$mod.g.$assign({',
  18220. ' D1: 0xD91C9AF4,',
  18221. ' D2: 0x3C93,',
  18222. ' D3: 0x420F,',
  18223. ' D4: [',
  18224. ' 0xA3,',
  18225. ' 0x03,',
  18226. ' 0xBF,',
  18227. ' 0x5B,',
  18228. ' 0xA8,',
  18229. ' 0x2B,',
  18230. ' 0xFD,',
  18231. ' 0x23',
  18232. ' ]',
  18233. '});',
  18234. '$mod.s = $mod.i.$guid;',
  18235. '$mod.s = $mod.IUnknown.$guid;',
  18236. '$mod.s = rtl.guidrToStr($mod.g);',
  18237. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  18238. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  18239. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  18240. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  18241. 'if ($mod.s === $mod.i.$guid) ;',
  18242. 'if ($mod.i.$guid === $mod.s) ;',
  18243. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  18244. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  18245. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  18246. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  18247. '']));
  18248. end;
  18249. procedure TTestModule.TestClassInterface_GUIDProperty;
  18250. begin
  18251. StartProgram(false);
  18252. Add([
  18253. '{$interfaces corba}',
  18254. 'type',
  18255. ' IUnknown = interface',
  18256. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  18257. ' end;',
  18258. ' TGUID = record D1, D2, D3, D4: word; end;',
  18259. ' TAliasGUID = TGUID;',
  18260. ' TGUIDString = type string;',
  18261. ' TAliasGUIDString = TGUIDString;',
  18262. ' TObject = class',
  18263. ' function GetG: TAliasGUID; virtual; abstract;',
  18264. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  18265. ' function GetS: TAliasGUIDString; virtual; abstract;',
  18266. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  18267. ' property g: TAliasGUID read GetG write SetG;',
  18268. ' property s: TAliasGUIDString read GetS write SetS;',
  18269. ' end;',
  18270. 'var o: TObject;',
  18271. 'begin',
  18272. ' o.g:=IUnknown;',
  18273. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  18274. ' o.s:=IUnknown;',
  18275. ' o.s:=o.g;',
  18276. '']);
  18277. ConvertProgram;
  18278. CheckSource('TestClassInterface_GUIDProperty',
  18279. LinesToStr([ // statements
  18280. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  18281. 'rtl.recNewT($mod, "TGUID", function () {',
  18282. ' this.D1 = 0;',
  18283. ' this.D2 = 0;',
  18284. ' this.D3 = 0;',
  18285. ' this.D4 = 0;',
  18286. ' this.$eq = function (b) {',
  18287. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  18288. ' };',
  18289. ' this.$assign = function (s) {',
  18290. ' this.D1 = s.D1;',
  18291. ' this.D2 = s.D2;',
  18292. ' this.D3 = s.D3;',
  18293. ' this.D4 = s.D4;',
  18294. ' return this;',
  18295. ' };',
  18296. '});',
  18297. 'rtl.createClass($mod, "TObject", null, function () {',
  18298. ' this.$init = function () {',
  18299. ' };',
  18300. ' this.$final = function () {',
  18301. ' };',
  18302. '});',
  18303. 'this.o = null;',
  18304. '']),
  18305. LinesToStr([ // $mod.$main
  18306. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  18307. '$mod.o.SetG({',
  18308. ' D1: 0xD91C9AF4,',
  18309. ' D2: 0x3C93,',
  18310. ' D3: 0x420F,',
  18311. ' D4: [',
  18312. ' 0xA3,',
  18313. ' 0x03,',
  18314. ' 0xBF,',
  18315. ' 0x5B,',
  18316. ' 0xA8,',
  18317. ' 0x2B,',
  18318. ' 0xFD,',
  18319. ' 0x23',
  18320. ' ]',
  18321. '});',
  18322. '$mod.o.SetS($mod.IUnknown.$guid);',
  18323. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  18324. '']));
  18325. end;
  18326. procedure TTestModule.TestClassHelper_ClassVar;
  18327. begin
  18328. StartProgram(false);
  18329. Add([
  18330. 'type',
  18331. ' TObject = class',
  18332. ' end;',
  18333. ' THelper = class helper for TObject',
  18334. ' const',
  18335. ' One = 1;',
  18336. ' Two: word = 2;',
  18337. ' class var',
  18338. ' Glob: word;',
  18339. ' function Foo(w: word): word;',
  18340. ' class function Bar(w: word): word;',
  18341. ' end;',
  18342. 'function THelper.foo(w: word): word;',
  18343. 'begin',
  18344. ' Result:=w;',
  18345. ' Two:=One+w;',
  18346. ' Glob:=Glob;',
  18347. ' Result:=Self.Glob;',
  18348. ' Self.Glob:=Self.Glob;',
  18349. ' with Self do Glob:=Glob;',
  18350. 'end;',
  18351. 'class function THelper.bar(w: word): word;',
  18352. 'begin',
  18353. ' Result:=w;',
  18354. ' Two:=One;',
  18355. ' Glob:=Glob;',
  18356. ' Self.Glob:=Self.Glob;',
  18357. ' with Self do Glob:=Glob;',
  18358. 'end;',
  18359. 'var o: TObject;',
  18360. 'begin',
  18361. ' tobject.two:=tobject.one;',
  18362. ' tobject.Glob:=tobject.Glob;',
  18363. ' with tobject do begin',
  18364. ' two:=one;',
  18365. ' Glob:=Glob;',
  18366. ' end;',
  18367. ' o.two:=o.one;',
  18368. ' o.Glob:=o.Glob;',
  18369. ' with o do begin',
  18370. ' two:=one;',
  18371. ' Glob:=Glob;',
  18372. ' end;',
  18373. '']);
  18374. ConvertProgram;
  18375. CheckSource('TestClassHelper_ClassVar',
  18376. LinesToStr([ // statements
  18377. 'rtl.createClass($mod, "TObject", null, function () {',
  18378. ' this.$init = function () {',
  18379. ' };',
  18380. ' this.$final = function () {',
  18381. ' };',
  18382. '});',
  18383. 'rtl.createHelper($mod, "THelper", null, function () {',
  18384. ' this.One = 1;',
  18385. ' this.Two = 2;',
  18386. ' this.Glob = 0;',
  18387. ' this.Foo = function (w) {',
  18388. ' var Result = 0;',
  18389. ' Result = w;',
  18390. ' $mod.THelper.Two = 1 + w;',
  18391. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18392. ' Result = $mod.THelper.Glob;',
  18393. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18394. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18395. ' return Result;',
  18396. ' };',
  18397. ' this.Bar = function (w) {',
  18398. ' var Result = 0;',
  18399. ' Result = w;',
  18400. ' $mod.THelper.Two = 1;',
  18401. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18402. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18403. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18404. ' return Result;',
  18405. ' };',
  18406. '});',
  18407. 'this.o = null;',
  18408. '']),
  18409. LinesToStr([ // $mod.$main
  18410. '$mod.THelper.Two = 1;',
  18411. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18412. 'var $with1 = $mod.TObject;',
  18413. '$mod.THelper.Two = 1;',
  18414. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18415. '$mod.THelper.Two = 1;',
  18416. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18417. 'var $with2 = $mod.o;',
  18418. '$mod.THelper.Two = 1;',
  18419. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18420. '']));
  18421. end;
  18422. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  18423. begin
  18424. StartProgram(false);
  18425. Add([
  18426. 'type',
  18427. ' TObject = class',
  18428. ' FSize: word;',
  18429. ' property Size: word read FSize write FSize;',
  18430. ' end;',
  18431. ' THelper = class helper for TObject',
  18432. ' function Foo(w: word = 1): word;',
  18433. ' end;',
  18434. 'function THelper.foo(w: word): word;',
  18435. 'begin',
  18436. ' Result:=Size;',
  18437. ' Size:=Size+2;',
  18438. ' Self.Size:=Self.Size+3;',
  18439. ' FSize:=FSize+4;',
  18440. ' Self.FSize:=Self.FSize+5;',
  18441. ' with Self do begin',
  18442. ' Size:=Size+6;',
  18443. ' FSize:=FSize+7;',
  18444. ' FSize:=FSize+8;',
  18445. ' end;',
  18446. 'end;',
  18447. 'begin',
  18448. '']);
  18449. ConvertProgram;
  18450. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  18451. LinesToStr([ // statements
  18452. 'rtl.createClass($mod, "TObject", null, function () {',
  18453. ' this.$init = function () {',
  18454. ' this.FSize = 0;',
  18455. ' };',
  18456. ' this.$final = function () {',
  18457. ' };',
  18458. '});',
  18459. 'rtl.createHelper($mod, "THelper", null, function () {',
  18460. ' this.Foo = function (w) {',
  18461. ' var Result = 0;',
  18462. ' Result = this.FSize;',
  18463. ' this.FSize = this.FSize + 2;',
  18464. ' this.FSize = this.FSize + 3;',
  18465. ' this.FSize = this.FSize + 4;',
  18466. ' this.FSize = this.FSize + 5;',
  18467. ' this.FSize = this.FSize + 6;',
  18468. ' this.FSize = this.FSize + 7;',
  18469. ' this.FSize = this.FSize + 8;',
  18470. ' return Result;',
  18471. ' };',
  18472. '});',
  18473. '']),
  18474. LinesToStr([ // $mod.$main
  18475. '']));
  18476. end;
  18477. procedure TTestModule.TestClassHelper_Method_Call;
  18478. begin
  18479. StartProgram(false);
  18480. Add([
  18481. 'type',
  18482. ' TObject = class',
  18483. ' procedure Run(w: word = 10);',
  18484. ' end;',
  18485. ' THelper = class helper for TObject',
  18486. ' function Foo(w: word = 1): word;',
  18487. ' end;',
  18488. 'procedure TObject.Run(w: word);',
  18489. 'begin',
  18490. ' Foo;',
  18491. ' Foo();',
  18492. ' Foo(2);',
  18493. ' Self.Foo;',
  18494. ' Self.Foo();',
  18495. ' Self.Foo(3);',
  18496. ' with Self do begin',
  18497. ' Foo;',
  18498. ' Foo();',
  18499. ' Foo(4);',
  18500. ' end;',
  18501. 'end;',
  18502. 'function THelper.foo(w: word): word;',
  18503. 'begin',
  18504. ' Run;',
  18505. ' Run();',
  18506. ' Run(11);',
  18507. ' Foo;',
  18508. ' Foo();',
  18509. ' Foo(12);',
  18510. ' Self.Foo;',
  18511. ' Self.Foo();',
  18512. ' Self.Foo(13);',
  18513. ' with Self do begin',
  18514. ' Foo;',
  18515. ' Foo();',
  18516. ' Foo(14);',
  18517. ' end;',
  18518. 'end;',
  18519. 'var Obj: TObject;',
  18520. 'begin',
  18521. ' obj.Foo;',
  18522. ' obj.Foo();',
  18523. ' obj.Foo(21);',
  18524. ' with obj do begin',
  18525. ' Foo;',
  18526. ' Foo();',
  18527. ' Foo(22);',
  18528. ' end;',
  18529. '']);
  18530. ConvertProgram;
  18531. CheckSource('TestClassHelper_Method_Call',
  18532. LinesToStr([ // statements
  18533. 'rtl.createClass($mod, "TObject", null, function () {',
  18534. ' this.$init = function () {',
  18535. ' };',
  18536. ' this.$final = function () {',
  18537. ' };',
  18538. ' this.Run = function (w) {',
  18539. ' $mod.THelper.Foo.call(this, 1);',
  18540. ' $mod.THelper.Foo.call(this, 1);',
  18541. ' $mod.THelper.Foo.call(this, 2);',
  18542. ' $mod.THelper.Foo.call(this, 1);',
  18543. ' $mod.THelper.Foo.call(this, 1);',
  18544. ' $mod.THelper.Foo.call(this, 3);',
  18545. ' $mod.THelper.Foo.call(this, 1);',
  18546. ' $mod.THelper.Foo.call(this, 1);',
  18547. ' $mod.THelper.Foo.call(this, 4);',
  18548. ' };',
  18549. '});',
  18550. 'rtl.createHelper($mod, "THelper", null, function () {',
  18551. ' this.Foo = function (w) {',
  18552. ' var Result = 0;',
  18553. ' this.Run(10);',
  18554. ' this.Run(10);',
  18555. ' this.Run(11);',
  18556. ' $mod.THelper.Foo.call(this, 1);',
  18557. ' $mod.THelper.Foo.call(this, 1);',
  18558. ' $mod.THelper.Foo.call(this, 12);',
  18559. ' $mod.THelper.Foo.call(this, 1);',
  18560. ' $mod.THelper.Foo.call(this, 1);',
  18561. ' $mod.THelper.Foo.call(this, 13);',
  18562. ' $mod.THelper.Foo.call(this, 1);',
  18563. ' $mod.THelper.Foo.call(this, 1);',
  18564. ' $mod.THelper.Foo.call(this, 14);',
  18565. ' return Result;',
  18566. ' };',
  18567. '});',
  18568. 'this.Obj = null;',
  18569. '']),
  18570. LinesToStr([ // $mod.$main
  18571. '$mod.THelper.Foo.call($mod.Obj, 1);',
  18572. '$mod.THelper.Foo.call($mod.Obj, 1);',
  18573. '$mod.THelper.Foo.call($mod.Obj, 21);',
  18574. 'var $with1 = $mod.Obj;',
  18575. '$mod.THelper.Foo.call($with1, 1);',
  18576. '$mod.THelper.Foo.call($with1, 1);',
  18577. '$mod.THelper.Foo.call($with1, 22);',
  18578. '']));
  18579. end;
  18580. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  18581. begin
  18582. StartProgram(false);
  18583. Add([
  18584. 'type',
  18585. ' TObject = class',
  18586. ' procedure Run(w: word = 10);',
  18587. ' end;',
  18588. ' THelper = class helper for TObject',
  18589. ' function Foo(w: word = 1): word;',
  18590. ' end;',
  18591. 'procedure TObject.Run(w: word);',
  18592. ' procedure Sub(Self: TObject);',
  18593. ' begin',
  18594. ' Foo;',
  18595. ' Foo();',
  18596. ' Self.Foo;',
  18597. ' Self.Foo();',
  18598. ' with Self do begin',
  18599. ' Foo;',
  18600. ' Foo();',
  18601. ' end;',
  18602. ' end;',
  18603. 'begin',
  18604. 'end;',
  18605. 'function THelper.foo(w: word): word;',
  18606. ' procedure Sub(Self: TObject);',
  18607. ' begin',
  18608. ' Run;',
  18609. ' Run();',
  18610. ' Foo;',
  18611. ' Foo();',
  18612. ' Self.Foo;',
  18613. ' Self.Foo();',
  18614. ' with Self do begin',
  18615. ' Foo;',
  18616. ' Foo();',
  18617. ' end;',
  18618. ' end;',
  18619. 'begin',
  18620. 'end;',
  18621. 'begin',
  18622. '']);
  18623. ConvertProgram;
  18624. CheckSource('TestClassHelper_Method_Nested_Call',
  18625. LinesToStr([ // statements
  18626. 'rtl.createClass($mod, "TObject", null, function () {',
  18627. ' this.$init = function () {',
  18628. ' };',
  18629. ' this.$final = function () {',
  18630. ' };',
  18631. ' this.Run = function (w) {',
  18632. ' var $Self = this;',
  18633. ' function Sub(Self) {',
  18634. ' $mod.THelper.Foo.call($Self, 1);',
  18635. ' $mod.THelper.Foo.call($Self, 1);',
  18636. ' $mod.THelper.Foo.call(Self, 1);',
  18637. ' $mod.THelper.Foo.call(Self, 1);',
  18638. ' $mod.THelper.Foo.call(Self, 1);',
  18639. ' $mod.THelper.Foo.call($Self, 1);',
  18640. ' };',
  18641. ' };',
  18642. '});',
  18643. 'rtl.createHelper($mod, "THelper", null, function () {',
  18644. ' this.Foo = function (w) {',
  18645. ' var $Self = this;',
  18646. ' var Result = 0;',
  18647. ' function Sub(Self) {',
  18648. ' $Self.Run(10);',
  18649. ' $Self.Run(10);',
  18650. ' $mod.THelper.Foo.call($Self, 1);',
  18651. ' $mod.THelper.Foo.call($Self, 1);',
  18652. ' $mod.THelper.Foo.call(Self, 1);',
  18653. ' $mod.THelper.Foo.call(Self, 1);',
  18654. ' $mod.THelper.Foo.call(Self, 1);',
  18655. ' $mod.THelper.Foo.call($Self, 1);',
  18656. ' };',
  18657. ' return Result;',
  18658. ' };',
  18659. '});',
  18660. '']),
  18661. LinesToStr([ // $mod.$main
  18662. '']));
  18663. end;
  18664. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  18665. begin
  18666. StartProgram(false);
  18667. Add([
  18668. 'type',
  18669. ' TObject = class',
  18670. ' class procedure Run(w: word = 10);',
  18671. ' end;',
  18672. ' THelper = class helper for TObject',
  18673. ' class function Foo(w: word = 1): word;',
  18674. ' end;',
  18675. 'class procedure TObject.Run(w: word);',
  18676. 'begin',
  18677. ' Foo;',
  18678. ' Foo();',
  18679. ' Self.Foo;',
  18680. ' Self.Foo();',
  18681. ' with Self do begin',
  18682. ' Foo;',
  18683. ' Foo();',
  18684. ' end;',
  18685. 'end;',
  18686. 'class function THelper.foo(w: word): word;',
  18687. 'begin',
  18688. ' Run;',
  18689. ' Run();',
  18690. ' Foo;',
  18691. ' Foo();',
  18692. ' Self.Foo;',
  18693. ' Self.Foo();',
  18694. ' with Self do begin',
  18695. ' Foo;',
  18696. ' Foo();',
  18697. ' end;',
  18698. 'end;',
  18699. 'var',
  18700. ' Obj: TObject;',
  18701. 'begin',
  18702. ' obj.Foo;',
  18703. ' obj.Foo();',
  18704. ' with obj do begin',
  18705. ' Foo;',
  18706. ' Foo();',
  18707. ' end;',
  18708. ' tobject.Foo;',
  18709. ' tobject.Foo();',
  18710. ' with tobject do begin',
  18711. ' Foo;',
  18712. ' Foo();',
  18713. ' end;',
  18714. '']);
  18715. ConvertProgram;
  18716. CheckSource('TestClassHelper_ClassMethod_Call',
  18717. LinesToStr([ // statements
  18718. 'rtl.createClass($mod, "TObject", null, function () {',
  18719. ' this.$init = function () {',
  18720. ' };',
  18721. ' this.$final = function () {',
  18722. ' };',
  18723. ' this.Run = function (w) {',
  18724. ' $mod.THelper.Foo.call(this, 1);',
  18725. ' $mod.THelper.Foo.call(this, 1);',
  18726. ' $mod.THelper.Foo.call(this, 1);',
  18727. ' $mod.THelper.Foo.call(this, 1);',
  18728. ' $mod.THelper.Foo.call(this, 1);',
  18729. ' $mod.THelper.Foo.call(this, 1);',
  18730. ' };',
  18731. '});',
  18732. 'rtl.createHelper($mod, "THelper", null, function () {',
  18733. ' this.Foo = function (w) {',
  18734. ' var Result = 0;',
  18735. ' this.Run(10);',
  18736. ' this.Run(10);',
  18737. ' $mod.THelper.Foo.call(this, 1);',
  18738. ' $mod.THelper.Foo.call(this, 1);',
  18739. ' $mod.THelper.Foo.call(this, 1);',
  18740. ' $mod.THelper.Foo.call(this, 1);',
  18741. ' $mod.THelper.Foo.call(this, 1);',
  18742. ' $mod.THelper.Foo.call(this, 1);',
  18743. ' return Result;',
  18744. ' };',
  18745. '});',
  18746. 'this.Obj = null;',
  18747. '']),
  18748. LinesToStr([ // $mod.$main
  18749. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  18750. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  18751. 'var $with1 = $mod.Obj;',
  18752. '$mod.THelper.Foo.call($with1.$class, 1);',
  18753. '$mod.THelper.Foo.call($with1.$class, 1);',
  18754. '$mod.THelper.Foo.call($mod.TObject, 1);',
  18755. '$mod.THelper.Foo.call($mod.TObject, 1);',
  18756. 'var $with2 = $mod.TObject;',
  18757. '$mod.THelper.Foo.call($mod.TObject, 1);',
  18758. '$mod.THelper.Foo.call($mod.TObject, 1);',
  18759. '']));
  18760. end;
  18761. procedure TTestModule.TestClassHelper_ClassOf;
  18762. begin
  18763. StartProgram(false);
  18764. Add([
  18765. 'type',
  18766. ' TObject = class',
  18767. ' end;',
  18768. ' TClass = class of TObject;',
  18769. ' THelper = class helper for TObject',
  18770. ' class function Foo(w: word = 1): word;',
  18771. ' end;',
  18772. 'class function THelper.foo(w: word): word;',
  18773. 'begin',
  18774. 'end;',
  18775. 'var',
  18776. ' c: TClass;',
  18777. 'begin',
  18778. ' c.Foo;',
  18779. ' c.Foo();',
  18780. ' with c do begin',
  18781. ' Foo;',
  18782. ' Foo();',
  18783. ' end;',
  18784. '']);
  18785. ConvertProgram;
  18786. CheckSource('TestClassHelper_ClassOf',
  18787. LinesToStr([ // statements
  18788. 'rtl.createClass($mod, "TObject", null, function () {',
  18789. ' this.$init = function () {',
  18790. ' };',
  18791. ' this.$final = function () {',
  18792. ' };',
  18793. '});',
  18794. 'rtl.createHelper($mod, "THelper", null, function () {',
  18795. ' this.Foo = function (w) {',
  18796. ' var Result = 0;',
  18797. ' return Result;',
  18798. ' };',
  18799. '});',
  18800. 'this.c = null;',
  18801. '']),
  18802. LinesToStr([ // $mod.$main
  18803. '$mod.THelper.Foo.call($mod.c, 1);',
  18804. '$mod.THelper.Foo.call($mod.c, 1);',
  18805. 'var $with1 = $mod.c;',
  18806. '$mod.THelper.Foo.call($with1, 1);',
  18807. '$mod.THelper.Foo.call($with1, 1);',
  18808. '']));
  18809. end;
  18810. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  18811. begin
  18812. StartProgram(false);
  18813. Add([
  18814. '{$mode objfpc}',
  18815. 'type',
  18816. ' TObject = class',
  18817. ' procedure DoIt;',
  18818. ' end;',
  18819. ' THelper = class helper for TObject',
  18820. ' procedure Fly(w: word = 1);',
  18821. ' class procedure Glide(w: word = 1);',
  18822. ' class procedure Run(w: word = 1); static;',
  18823. ' end;',
  18824. ' TFly = procedure(w: word) of object;',
  18825. ' TGlide = TFly;',
  18826. ' TRun = procedure(w: word);',
  18827. 'var',
  18828. ' f: TFly;',
  18829. ' g: TGlide;',
  18830. ' r: TRun;',
  18831. 'procedure TObject.DoIt;',
  18832. 'begin',
  18833. ' f:=@fly;',
  18834. ' g:=@glide;',
  18835. ' r:=@run;',
  18836. ' f:[email protected];',
  18837. ' g:[email protected];',
  18838. ' r:[email protected];',
  18839. ' with self do begin',
  18840. ' f:=@fly;',
  18841. ' g:=@glide;',
  18842. ' r:=@run;',
  18843. ' end;',
  18844. 'end;',
  18845. 'procedure THelper.fly(w: word);',
  18846. 'begin',
  18847. ' f:=@fly;',
  18848. ' g:=@glide;',
  18849. ' r:=@run;',
  18850. 'end;',
  18851. 'class procedure THelper.glide(w: word);',
  18852. 'begin',
  18853. ' g:=@glide;',
  18854. ' r:=@run;',
  18855. 'end;',
  18856. 'class procedure THelper.run(w: word);',
  18857. 'begin',
  18858. ' g:=@glide;',
  18859. ' r:=@run;',
  18860. 'end;',
  18861. 'var',
  18862. ' Obj: TObject;',
  18863. 'begin',
  18864. ' f:[email protected];',
  18865. ' g:[email protected];',
  18866. ' r:[email protected];',
  18867. ' with obj do begin',
  18868. ' f:=@fly;',
  18869. ' g:=@glide;',
  18870. ' r:=@run;',
  18871. ' end;',
  18872. ' g:[email protected];',
  18873. ' r:[email protected];',
  18874. ' with tobject do begin',
  18875. ' g:=@glide;',
  18876. ' r:=@run;',
  18877. ' end;',
  18878. '']);
  18879. ConvertProgram;
  18880. CheckSource('TestClassHelper_MethodRefObjFPC',
  18881. LinesToStr([ // statements
  18882. 'rtl.createClass($mod, "TObject", null, function () {',
  18883. ' this.$init = function () {',
  18884. ' };',
  18885. ' this.$final = function () {',
  18886. ' };',
  18887. ' this.DoIt = function () {',
  18888. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  18889. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  18890. ' $mod.r = $mod.THelper.Run;',
  18891. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  18892. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  18893. ' $mod.r = $mod.THelper.Run;',
  18894. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  18895. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  18896. ' $mod.r = $mod.THelper.Run;',
  18897. ' };',
  18898. '});',
  18899. 'rtl.createHelper($mod, "THelper", null, function () {',
  18900. ' this.Fly = function (w) {',
  18901. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  18902. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  18903. ' $mod.r = $mod.THelper.Run;',
  18904. ' };',
  18905. ' this.Glide = function (w) {',
  18906. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  18907. ' $mod.r = $mod.THelper.Run;',
  18908. ' };',
  18909. ' this.Run = function (w) {',
  18910. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  18911. ' $mod.r = $mod.THelper.Run;',
  18912. ' };',
  18913. '});',
  18914. 'this.f = null;',
  18915. 'this.g = null;',
  18916. 'this.r = null;',
  18917. 'this.Obj = null;',
  18918. '']),
  18919. LinesToStr([ // $mod.$main
  18920. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  18921. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  18922. '$mod.r = $mod.THelper.Run;',
  18923. 'var $with1 = $mod.Obj;',
  18924. '$mod.f = rtl.createCallback($with1, $mod.THelper.Fly);',
  18925. '$mod.g = rtl.createCallback($with1.$class, $mod.THelper.Glide);',
  18926. '$mod.r = $mod.THelper.Run;',
  18927. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  18928. '$mod.r = $mod.THelper.Run;',
  18929. 'var $with2 = $mod.TObject;',
  18930. '$mod.g = rtl.createCallback($with2, $mod.THelper.Glide);',
  18931. '$mod.r = $mod.THelper.Run;',
  18932. '']));
  18933. end;
  18934. procedure TTestModule.TestClassHelper_Constructor;
  18935. begin
  18936. StartProgram(false);
  18937. Add([
  18938. 'type',
  18939. ' TObject = class',
  18940. ' constructor Create;',
  18941. ' end;',
  18942. ' TClass = class of TObject;',
  18943. ' THelper = class helper for TObject',
  18944. ' constructor NewHlp(w: word);',
  18945. ' end;',
  18946. 'var',
  18947. ' obj: TObject;',
  18948. ' c: TClass;',
  18949. 'constructor TObject.Create;',
  18950. 'begin',
  18951. ' NewHlp(2);', // normal call
  18952. ' tobject.NewHlp(3);', // new instance
  18953. ' c.newhlp(4);', // new instance
  18954. 'end;',
  18955. 'constructor THelper.NewHlp(w: word);',
  18956. 'begin',
  18957. ' create;', // normal call
  18958. ' tobject.create;', // new instance
  18959. ' NewHlp(2);', // normal call
  18960. ' tobject.NewHlp(3);', // new instance
  18961. ' c.newhlp(4);', // new instance
  18962. 'end;',
  18963. 'begin',
  18964. ' obj.newhlp(2);', // normal call
  18965. ' with Obj do newhlp(12);', // normal call
  18966. ' tobject.newhlp(3);', // new instance
  18967. ' with tobject do newhlp(13);', // new instance
  18968. ' c.newhlp(4);', // new instance
  18969. ' with c do newhlp(14);', // new instance
  18970. '']);
  18971. ConvertProgram;
  18972. CheckSource('TestClassHelper_Constructor',
  18973. LinesToStr([ // statements
  18974. 'rtl.createClass($mod, "TObject", null, function () {',
  18975. ' this.$init = function () {',
  18976. ' };',
  18977. ' this.$final = function () {',
  18978. ' };',
  18979. ' this.Create = function () {',
  18980. ' $mod.THelper.NewHlp.call(this, 2);',
  18981. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  18982. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  18983. ' return this;',
  18984. ' };',
  18985. '});',
  18986. 'rtl.createHelper($mod, "THelper", null, function () {',
  18987. ' this.NewHlp = function (w) {',
  18988. ' this.Create();',
  18989. ' $mod.TObject.$create("Create");',
  18990. ' $mod.THelper.NewHlp.call(this, 2);',
  18991. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  18992. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  18993. ' return this;',
  18994. ' };',
  18995. '});',
  18996. 'this.obj = null;',
  18997. 'this.c = null;',
  18998. '']),
  18999. LinesToStr([ // $mod.$main
  19000. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  19001. 'var $with1 = $mod.obj;',
  19002. '$mod.THelper.NewHlp.call($with1, 12);',
  19003. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19004. 'var $with2 = $mod.TObject;',
  19005. '$with2.$create($mod.THelper.NewHlp, [13]);',
  19006. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  19007. 'var $with3 = $mod.c;',
  19008. '$with3.$create($mod.THelper.NewHlp, [14]);',
  19009. '']));
  19010. end;
  19011. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  19012. begin
  19013. StartProgram(false);
  19014. Add([
  19015. 'type',
  19016. ' TObject = class',
  19017. ' procedure Fly;',
  19018. ' end;',
  19019. ' TObjHelper = class helper for TObject',
  19020. ' procedure Fly;',
  19021. ' end;',
  19022. ' TBird = class',
  19023. ' procedure Fly;',
  19024. ' end;',
  19025. ' TBirdHelper = class helper for TBird',
  19026. ' procedure Fly;',
  19027. ' procedure Walk(w: word);',
  19028. ' end;',
  19029. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  19030. ' procedure Fly;',
  19031. ' procedure Walk(w: word);',
  19032. ' end;',
  19033. 'procedure Tobject.fly;',
  19034. 'begin',
  19035. ' inherited;', // ignore
  19036. 'end;',
  19037. 'procedure Tobjhelper.fly;',
  19038. 'begin',
  19039. ' {@TObject_Fly}inherited;',
  19040. ' inherited {@TObject_Fly}Fly;',
  19041. 'end;',
  19042. 'procedure Tbird.fly;',
  19043. 'begin',
  19044. ' {@TObjHelper_Fly}inherited;',
  19045. ' inherited {@TObjHelper_Fly}Fly;',
  19046. 'end;',
  19047. 'procedure Tbirdhelper.fly;',
  19048. 'begin',
  19049. ' {@TBird_Fly}inherited;',
  19050. ' inherited {@TBird_Fly}Fly;',
  19051. 'end;',
  19052. 'procedure Tbirdhelper.walk(w: word);',
  19053. 'begin',
  19054. 'end;',
  19055. 'procedure teagleHelper.fly;',
  19056. 'begin',
  19057. ' {@TBird_Fly}inherited;',
  19058. ' inherited {@TBird_Fly}Fly;',
  19059. 'end;',
  19060. 'procedure teagleHelper.walk(w: word);',
  19061. 'begin',
  19062. ' {@TBirdHelper_Walk}inherited;',
  19063. ' inherited {@TBirdHelper_Walk}Walk(3);',
  19064. 'end;',
  19065. 'begin',
  19066. '']);
  19067. ConvertProgram;
  19068. CheckSource('TestClassHelper_InheritedObjFPC',
  19069. LinesToStr([ // statements
  19070. 'rtl.createClass($mod, "TObject", null, function () {',
  19071. ' this.$init = function () {',
  19072. ' };',
  19073. ' this.$final = function () {',
  19074. ' };',
  19075. ' this.Fly = function () {',
  19076. ' };',
  19077. '});',
  19078. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19079. ' this.Fly = function () {',
  19080. ' $mod.TObject.Fly.call(this);',
  19081. ' $mod.TObject.Fly.call(this);',
  19082. ' };',
  19083. '});',
  19084. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19085. ' this.Fly$1 = function () {',
  19086. ' $mod.TObjHelper.Fly.call(this);',
  19087. ' $mod.TObjHelper.Fly.call(this);',
  19088. ' };',
  19089. '});',
  19090. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  19091. ' this.Fly = function () {',
  19092. ' $mod.TBird.Fly$1.call(this);',
  19093. ' $mod.TBird.Fly$1.call(this);',
  19094. ' };',
  19095. ' this.Walk = function (w) {',
  19096. ' };',
  19097. '});',
  19098. 'rtl.createHelper($mod, "TEagleHelper", $mod.TBirdHelper, function () {',
  19099. ' this.Fly$1 = function () {',
  19100. ' $mod.TBird.Fly$1.call(this);',
  19101. ' $mod.TBird.Fly$1.call(this);',
  19102. ' };',
  19103. ' this.Walk$1 = function (w) {',
  19104. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  19105. ' $mod.TBirdHelper.Walk.call(this, 3);',
  19106. ' };',
  19107. '});',
  19108. '']),
  19109. LinesToStr([ // $mod.$main
  19110. '']));
  19111. end;
  19112. procedure TTestModule.TestClassHelper_Property;
  19113. begin
  19114. StartProgram(false);
  19115. Add([
  19116. 'type',
  19117. ' TObject = class',
  19118. ' FSize: word;',
  19119. ' function GetSpeed: word;',
  19120. ' procedure SetSpeed(Value: word);',
  19121. ' end;',
  19122. ' TObjHelper = class helper for TObject',
  19123. ' function GetLeft: word;',
  19124. ' procedure SetLeft(Value: word);',
  19125. ' property Size: word read FSize write FSize;',
  19126. ' property Speed: word read GetSpeed write SetSpeed;',
  19127. ' property Left: word read GetLeft write SetLeft;',
  19128. ' end;',
  19129. ' TBird = class',
  19130. ' property NotRight: word read GetLeft write SetLeft;',
  19131. ' procedure DoIt;',
  19132. ' end;',
  19133. 'var',
  19134. ' b: TBird;',
  19135. 'function Tobject.GetSpeed: word;',
  19136. 'begin',
  19137. ' Size:=Size+11;',
  19138. ' Speed:=Speed+12;',
  19139. ' Result:=Left+13;',
  19140. ' Left:=13;',
  19141. ' Left:=Left+13;',
  19142. ' Self.Size:=Self.Size+21;',
  19143. ' Self.Speed:=Self.Speed+22;',
  19144. ' Self.Left:=Self.Left+23;',
  19145. ' with Self do begin',
  19146. ' Size:=Size+31;',
  19147. ' Speed:=Speed+32;',
  19148. ' Left:=Left+33;',
  19149. ' end;',
  19150. 'end;',
  19151. 'procedure Tobject.SetSpeed(Value: word);',
  19152. 'begin',
  19153. 'end;',
  19154. 'function TObjHelper.GetLeft: word;',
  19155. 'begin',
  19156. ' Size:=Size+11;',
  19157. ' Speed:=Speed+12;',
  19158. ' Left:=Left+13;',
  19159. ' Self.Size:=Self.Size+21;',
  19160. ' Self.Speed:=Self.Speed+22;',
  19161. ' Self.Left:=Self.Left+23;',
  19162. ' with Self do begin',
  19163. ' Size:=Size+31;',
  19164. ' Speed:=Speed+32;',
  19165. ' Left:=Left+33;',
  19166. ' end;',
  19167. 'end;',
  19168. 'procedure TObjHelper.SetLeft(Value: word);',
  19169. 'begin',
  19170. 'end;',
  19171. 'procedure TBird.DoIt;',
  19172. 'begin',
  19173. ' NotRight:=NotRight+11;',
  19174. ' Self.NotRight:=Self.NotRight+21;',
  19175. ' with Self do begin',
  19176. ' NotRight:=NotRight+31;',
  19177. ' end;',
  19178. 'end;',
  19179. 'begin',
  19180. ' b.Size:=b.Size+11;',
  19181. ' b.Speed:=b.Speed+12;',
  19182. ' b.Left:=b.Left+13;',
  19183. ' b.NotRight:=b.NotRight+14;',
  19184. ' with b do begin',
  19185. ' Size:=Size+31;',
  19186. ' Speed:=Speed+32;',
  19187. ' Left:=Left+33;',
  19188. ' NotRight:=NotRight+34;',
  19189. ' end;',
  19190. '']);
  19191. ConvertProgram;
  19192. CheckSource('TestClassHelper_Property',
  19193. LinesToStr([ // statements
  19194. 'rtl.createClass($mod, "TObject", null, function () {',
  19195. ' this.$init = function () {',
  19196. ' this.FSize = 0;',
  19197. ' };',
  19198. ' this.$final = function () {',
  19199. ' };',
  19200. ' this.GetSpeed = function () {',
  19201. ' var Result = 0;',
  19202. ' this.FSize = this.FSize + 11;',
  19203. ' this.SetSpeed(this.GetSpeed() + 12);',
  19204. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  19205. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  19206. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19207. ' this.FSize = this.FSize + 21;',
  19208. ' this.SetSpeed(this.GetSpeed() + 22);',
  19209. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19210. ' this.FSize = this.FSize + 31;',
  19211. ' this.SetSpeed(this.GetSpeed() + 32);',
  19212. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19213. ' return Result;',
  19214. ' };',
  19215. ' this.SetSpeed = function (Value) {',
  19216. ' };',
  19217. '});',
  19218. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19219. ' this.GetLeft = function () {',
  19220. ' var Result = 0;',
  19221. ' this.FSize = this.FSize + 11;',
  19222. ' this.SetSpeed(this.GetSpeed() + 12);',
  19223. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19224. ' this.FSize = this.FSize + 21;',
  19225. ' this.SetSpeed(this.GetSpeed() + 22);',
  19226. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19227. ' this.FSize = this.FSize + 31;',
  19228. ' this.SetSpeed(this.GetSpeed() + 32);',
  19229. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19230. ' return Result;',
  19231. ' };',
  19232. ' this.SetLeft = function (Value) {',
  19233. ' };',
  19234. '});',
  19235. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19236. ' this.DoIt = function () {',
  19237. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  19238. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  19239. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  19240. ' };',
  19241. '});',
  19242. 'this.b = null;',
  19243. '']),
  19244. LinesToStr([ // $mod.$main
  19245. '$mod.b.FSize = $mod.b.FSize + 11;',
  19246. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  19247. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  19248. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  19249. 'var $with1 = $mod.b;',
  19250. '$with1.FSize = $with1.FSize + 31;',
  19251. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  19252. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  19253. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  19254. '']));
  19255. end;
  19256. procedure TTestModule.TestClassHelper_Property_Array;
  19257. begin
  19258. StartProgram(false);
  19259. Add([
  19260. 'type',
  19261. ' TObject = class',
  19262. ' function GetSpeed(Index: boolean): word;',
  19263. ' procedure SetSpeed(Index: boolean; Value: word);',
  19264. ' end;',
  19265. ' TObjHelper = class helper for TObject',
  19266. ' function GetSize(Index: boolean): word;',
  19267. ' procedure SetSize(Index: boolean; Value: word);',
  19268. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  19269. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  19270. ' end;',
  19271. ' TBird = class',
  19272. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  19273. ' procedure DoIt;',
  19274. ' end;',
  19275. 'var',
  19276. ' b: TBird;',
  19277. 'function Tobject.GetSpeed(Index: boolean): word;',
  19278. 'begin',
  19279. ' Result:=Size[false];',
  19280. ' Size[true]:=Size[false]+11;',
  19281. ' Speed[true]:=Speed[false]+12;',
  19282. ' Self.Size[true]:=Self.Size[false]+21;',
  19283. ' Self.Speed[true]:=Self.Speed[false]+22;',
  19284. ' with Self do begin',
  19285. ' Size[true]:=Size[false]+31;',
  19286. ' Speed[true]:=Speed[false]+32;',
  19287. ' end;',
  19288. 'end;',
  19289. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  19290. 'begin',
  19291. 'end;',
  19292. 'function TObjHelper.GetSize(Index: boolean): word;',
  19293. 'begin',
  19294. ' Size[true]:=Size[false]+11;',
  19295. ' Speed[true]:=Speed[false]+12;',
  19296. ' Self.Size[true]:=Self.Size[false]+21;',
  19297. ' Self.Speed[true]:=Self.Speed[false]+22;',
  19298. ' with Self do begin',
  19299. ' Size[true]:=Size[false]+31;',
  19300. ' Speed[true]:=Speed[false]+32;',
  19301. ' end;',
  19302. 'end;',
  19303. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  19304. 'begin',
  19305. 'end;',
  19306. 'procedure TBird.DoIt;',
  19307. 'begin',
  19308. ' Items[true]:=Items[false]+11;',
  19309. ' Self.Items[true]:=Self.Items[false]+21;',
  19310. ' with Self do Items[true]:=Items[false]+31;',
  19311. 'end;',
  19312. 'begin',
  19313. ' b.Size[true]:=b.Size[false]+11;',
  19314. ' b.Speed[true]:=b.Speed[false]+12;',
  19315. ' b.Items[true]:=b.Items[false]+13;',
  19316. ' with b do begin',
  19317. ' Size[true]:=Size[false]+21;',
  19318. ' Speed[true]:=Speed[false]+22;',
  19319. ' Items[true]:=Items[false]+23;',
  19320. ' end;',
  19321. '']);
  19322. ConvertProgram;
  19323. CheckSource('TestClassHelper_Property_Array',
  19324. LinesToStr([ // statements
  19325. 'rtl.createClass($mod, "TObject", null, function () {',
  19326. ' this.$init = function () {',
  19327. ' };',
  19328. ' this.$final = function () {',
  19329. ' };',
  19330. ' this.GetSpeed = function (Index) {',
  19331. ' var Result = 0;',
  19332. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  19333. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19334. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  19335. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19336. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  19337. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19338. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  19339. ' return Result;',
  19340. ' };',
  19341. ' this.SetSpeed = function (Index, Value) {',
  19342. ' };',
  19343. '});',
  19344. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19345. ' this.GetSize = function (Index) {',
  19346. ' var Result = 0;',
  19347. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19348. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  19349. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19350. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  19351. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19352. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  19353. ' return Result;',
  19354. ' };',
  19355. ' this.SetSize = function (Index, Value) {',
  19356. ' };',
  19357. '});',
  19358. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19359. ' this.DoIt = function () {',
  19360. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19361. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19362. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19363. ' };',
  19364. '});',
  19365. 'this.b = null;',
  19366. '']),
  19367. LinesToStr([ // $mod.$main
  19368. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  19369. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  19370. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  19371. 'var $with1 = $mod.b;',
  19372. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  19373. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  19374. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  19375. '']));
  19376. end;
  19377. procedure TTestModule.TestClassHelper_Property_Array_Default;
  19378. begin
  19379. StartProgram(false);
  19380. Add([
  19381. 'type',
  19382. ' TObject = class',
  19383. ' function GetSpeed(Index: boolean): word;',
  19384. ' procedure SetSpeed(Index: boolean; Value: word);',
  19385. ' end;',
  19386. ' TObjHelper = class helper for TObject',
  19387. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  19388. ' end;',
  19389. ' TBird = class',
  19390. ' end;',
  19391. ' TBirdHelper = class helper for TBird',
  19392. ' function GetSize(Index: word): boolean;',
  19393. ' procedure SetSize(Index: word; Value: boolean);',
  19394. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  19395. ' end;',
  19396. 'function Tobject.GetSpeed(Index: boolean): word;',
  19397. 'begin',
  19398. ' Self[true]:=Self[false]+1;',
  19399. 'end;',
  19400. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  19401. 'begin',
  19402. 'end;',
  19403. 'function TBirdHelper.GetSize(Index: word): boolean;',
  19404. 'begin',
  19405. ' Self[1]:=not Self[2];',
  19406. 'end;',
  19407. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  19408. 'begin',
  19409. 'end;',
  19410. 'var',
  19411. ' o: TObject;',
  19412. ' b: TBird;',
  19413. 'begin',
  19414. ' o[true]:=o[false]+1;',
  19415. ' b[3]:=not b[4];',
  19416. '']);
  19417. ConvertProgram;
  19418. CheckSource('TestClassHelper_Property_Array_Default',
  19419. LinesToStr([ // statements
  19420. 'rtl.createClass($mod, "TObject", null, function () {',
  19421. ' this.$init = function () {',
  19422. ' };',
  19423. ' this.$final = function () {',
  19424. ' };',
  19425. ' this.GetSpeed = function (Index) {',
  19426. ' var Result = 0;',
  19427. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  19428. ' return Result;',
  19429. ' };',
  19430. ' this.SetSpeed = function (Index, Value) {',
  19431. ' };',
  19432. '});',
  19433. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19434. '});',
  19435. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19436. '});',
  19437. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  19438. ' this.GetSize = function (Index) {',
  19439. ' var Result = false;',
  19440. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  19441. ' return Result;',
  19442. ' };',
  19443. ' this.SetSize = function (Index, Value) {',
  19444. ' };',
  19445. '});',
  19446. 'this.o = null;',
  19447. 'this.b = null;',
  19448. '']),
  19449. LinesToStr([ // $mod.$main
  19450. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  19451. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  19452. '']));
  19453. end;
  19454. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  19455. begin
  19456. StartProgram(false);
  19457. Add([
  19458. 'type',
  19459. ' TObject = class',
  19460. ' end;',
  19461. ' TObjHelper = class helper for TObject',
  19462. ' function GetItems(Index: word): TObject;',
  19463. ' procedure SetItems(Index: word; Value: TObject);',
  19464. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  19465. ' end;',
  19466. 'function Tobjhelper.GetItems(Index: word): TObject;',
  19467. 'begin',
  19468. ' Self[1][2]:=Self[3][4];',
  19469. 'end;',
  19470. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  19471. 'begin',
  19472. 'end;',
  19473. 'var',
  19474. ' o: TObject;',
  19475. 'begin',
  19476. ' o[1][2]:=o[3][4];',
  19477. '']);
  19478. ConvertProgram;
  19479. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  19480. LinesToStr([ // statements
  19481. 'rtl.createClass($mod, "TObject", null, function () {',
  19482. ' this.$init = function () {',
  19483. ' };',
  19484. ' this.$final = function () {',
  19485. ' };',
  19486. '});',
  19487. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19488. ' this.GetItems = function (Index) {',
  19489. ' var Result = null;',
  19490. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  19491. ' return Result;',
  19492. ' };',
  19493. ' this.SetItems = function (Index, Value) {',
  19494. ' };',
  19495. '});',
  19496. 'this.o = null;',
  19497. '']),
  19498. LinesToStr([ // $mod.$main
  19499. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  19500. '']));
  19501. end;
  19502. procedure TTestModule.TestClassHelper_ClassProperty;
  19503. begin
  19504. StartProgram(false);
  19505. Add([
  19506. 'type',
  19507. ' TObject = class',
  19508. ' class var FSize: word;',
  19509. ' class function GetSpeed: word;',
  19510. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  19511. ' end;',
  19512. ' TObjHelper = class helper for TObject',
  19513. ' class function GetLeft: word;',
  19514. ' class procedure SetLeft(Value: word);',
  19515. ' class property Size: word read FSize write FSize;',
  19516. ' class property Speed: word read GetSpeed write SetSpeed;',
  19517. ' class property Left: word read GetLeft write SetLeft;',
  19518. ' end;',
  19519. ' TBird = class',
  19520. ' class property NotRight: word read GetLeft write SetLeft;',
  19521. ' class procedure DoIt;',
  19522. ' end;',
  19523. ' TBirdClass = class of TBird;',
  19524. 'class function Tobject.GetSpeed: word;',
  19525. 'begin',
  19526. ' Size:=Size+11;',
  19527. ' Speed:=Speed+12;',
  19528. ' Left:=Left+13;',
  19529. ' Self.Size:=Self.Size+21;',
  19530. ' Self.Speed:=Self.Speed+22;',
  19531. ' Self.Left:=Self.Left+23;',
  19532. ' with Self do begin',
  19533. ' Size:=Size+31;',
  19534. ' Speed:=Speed+32;',
  19535. ' Left:=Left+33;',
  19536. ' end;',
  19537. 'end;',
  19538. 'class function TObjHelper.GetLeft: word;',
  19539. 'begin',
  19540. ' Size:=Size+11;',
  19541. ' Speed:=Speed+12;',
  19542. ' Left:=Left+13;',
  19543. ' Self.Size:=Self.Size+21;',
  19544. ' Self.Speed:=Self.Speed+22;',
  19545. ' Self.Left:=Self.Left+23;',
  19546. ' with Self do begin',
  19547. ' Size:=Size+31;',
  19548. ' Speed:=Speed+32;',
  19549. ' Left:=Left+33;',
  19550. ' end;',
  19551. 'end;',
  19552. 'class procedure TObjHelper.SetLeft(Value: word);',
  19553. 'begin',
  19554. 'end;',
  19555. 'class procedure TBird.DoIt;',
  19556. 'begin',
  19557. ' NotRight:=NotRight+11;',
  19558. ' Self.NotRight:=Self.NotRight+21;',
  19559. ' with Self do NotRight:=NotRight+31;',
  19560. 'end;',
  19561. 'var',
  19562. ' b: TBird;',
  19563. ' c: TBirdClass;',
  19564. 'begin',
  19565. ' b.Size:=b.Size+11;',
  19566. ' b.Speed:=b.Speed+12;',
  19567. ' b.Left:=b.Left+13;',
  19568. ' b.NotRight:=b.NotRight+14;',
  19569. ' with b do begin',
  19570. ' Size:=Size+31;',
  19571. ' Speed:=Speed+32;',
  19572. ' Left:=Left+33;',
  19573. ' NotRight:=NotRight+34;',
  19574. ' end;',
  19575. ' c.Size:=c.Size+11;',
  19576. ' c.Speed:=c.Speed+12;',
  19577. ' c.Left:=c.Left+13;',
  19578. ' c.NotRight:=c.NotRight+14;',
  19579. ' with c do begin',
  19580. ' Size:=Size+31;',
  19581. ' Speed:=Speed+32;',
  19582. ' Left:=Left+33;',
  19583. ' NotRight:=NotRight+34;',
  19584. ' end;',
  19585. ' tbird.Size:=tbird.Size+11;',
  19586. ' tbird.Speed:=tbird.Speed+12;',
  19587. ' tbird.Left:=tbird.Left+13;',
  19588. ' tbird.NotRight:=tbird.NotRight+14;',
  19589. ' with tbird do begin',
  19590. ' Size:=Size+31;',
  19591. ' Speed:=Speed+32;',
  19592. ' Left:=Left+33;',
  19593. ' NotRight:=NotRight+34;',
  19594. ' end;',
  19595. '']);
  19596. ConvertProgram;
  19597. CheckSource('TestClassHelper_ClassProperty',
  19598. LinesToStr([ // statements
  19599. 'rtl.createClass($mod, "TObject", null, function () {',
  19600. ' this.FSize = 0;',
  19601. ' this.$init = function () {',
  19602. ' };',
  19603. ' this.$final = function () {',
  19604. ' };',
  19605. ' this.GetSpeed = function () {',
  19606. ' var Result = 0;',
  19607. ' $mod.TObject.FSize = this.FSize + 11;',
  19608. ' this.SetSpeed(this.GetSpeed() + 12);',
  19609. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19610. ' $mod.TObject.FSize = this.FSize + 21;',
  19611. ' this.SetSpeed(this.GetSpeed() + 22);',
  19612. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19613. ' $mod.TObject.FSize = this.FSize + 31;',
  19614. ' this.SetSpeed(this.GetSpeed() + 32);',
  19615. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19616. ' return Result;',
  19617. ' };',
  19618. '});',
  19619. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19620. ' this.GetLeft = function () {',
  19621. ' var Result = 0;',
  19622. ' $mod.TObject.FSize = this.FSize + 11;',
  19623. ' this.SetSpeed(this.GetSpeed() + 12);',
  19624. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19625. ' $mod.TObject.FSize = this.FSize + 21;',
  19626. ' this.SetSpeed(this.GetSpeed() + 22);',
  19627. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19628. ' $mod.TObject.FSize = this.FSize + 31;',
  19629. ' this.SetSpeed(this.GetSpeed() + 32);',
  19630. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19631. ' return Result;',
  19632. ' };',
  19633. ' this.SetLeft = function (Value) {',
  19634. ' };',
  19635. '});',
  19636. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19637. ' this.DoIt = function () {',
  19638. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  19639. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  19640. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  19641. ' };',
  19642. '});',
  19643. 'this.b = null;',
  19644. 'this.c = null;',
  19645. '']),
  19646. LinesToStr([ // $mod.$main
  19647. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  19648. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  19649. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  19650. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  19651. 'var $with1 = $mod.b;',
  19652. '$mod.TObject.FSize = $with1.FSize + 31;',
  19653. '$with1.$class.SetSpeed($with1.$class.GetSpeed() + 32);',
  19654. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 33);',
  19655. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 34);',
  19656. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  19657. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  19658. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  19659. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  19660. 'var $with2 = $mod.c;',
  19661. '$mod.TObject.FSize = $with2.FSize + 31;',
  19662. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  19663. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 33);',
  19664. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 34);',
  19665. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  19666. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  19667. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  19668. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  19669. 'var $with3 = $mod.TBird;',
  19670. '$mod.TObject.FSize = $with3.FSize + 31;',
  19671. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  19672. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  19673. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  19674. '']));
  19675. end;
  19676. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  19677. begin
  19678. StartProgram(false);
  19679. Add([
  19680. 'type',
  19681. ' TObject = class',
  19682. ' class function GetSpeed: word; static;',
  19683. ' class procedure SetSpeed(Value: word); static;',
  19684. ' end;',
  19685. ' TObjHelper = class helper for TObject',
  19686. ' class function GetLeft: word; static;',
  19687. ' class procedure SetLeft(Value: word); static;',
  19688. ' class property Speed: word read GetSpeed write SetSpeed;',
  19689. ' class property Left: word read GetLeft write SetLeft;',
  19690. ' end;',
  19691. ' TBird = class',
  19692. ' class property NotRight: word read GetLeft write SetLeft;',
  19693. ' class procedure DoIt; static;',
  19694. ' class procedure DoSome;',
  19695. ' end;',
  19696. ' TBirdClass = class of TBird;',
  19697. 'class function Tobject.GetSpeed: word;',
  19698. 'begin',
  19699. ' Speed:=Speed+12;',
  19700. ' Left:=Left+13;',
  19701. 'end;',
  19702. 'class procedure TObject.SetSpeed(Value: word);',
  19703. 'begin',
  19704. 'end;',
  19705. 'class function TObjHelper.GetLeft: word;',
  19706. 'begin',
  19707. ' Speed:=Speed+12;',
  19708. ' Left:=Left+13;',
  19709. 'end;',
  19710. 'class procedure TObjHelper.SetLeft(Value: word);',
  19711. 'begin',
  19712. 'end;',
  19713. 'class procedure TBird.DoIt;',
  19714. 'begin',
  19715. ' NotRight:=NotRight+11;',
  19716. 'end;',
  19717. 'class procedure TBird.DoSome;',
  19718. 'begin',
  19719. ' Speed:=Speed+12;',
  19720. ' Left:=Left+13;',
  19721. ' Self.Speed:=Self.Speed+22;',
  19722. ' Self.Left:=Self.Left+23;',
  19723. ' with Self do begin',
  19724. ' Speed:=Speed+32;',
  19725. ' Left:=Left+33;',
  19726. ' end;',
  19727. ' NotRight:=NotRight+11;',
  19728. ' Self.NotRight:=Self.NotRight+21;',
  19729. ' with Self do NotRight:=NotRight+31;',
  19730. 'end;',
  19731. 'var',
  19732. ' b: TBird;',
  19733. ' c: TBirdClass;',
  19734. 'begin',
  19735. ' b.Speed:=b.Speed+12;',
  19736. ' b.Left:=b.Left+13;',
  19737. ' b.NotRight:=b.NotRight+14;',
  19738. ' with b do begin',
  19739. ' Speed:=Speed+32;',
  19740. ' Left:=Left+33;',
  19741. ' NotRight:=NotRight+34;',
  19742. ' end;',
  19743. ' c.Speed:=c.Speed+12;',
  19744. ' c.Left:=c.Left+13;',
  19745. ' c.NotRight:=c.NotRight+14;',
  19746. ' with c do begin',
  19747. ' Speed:=Speed+32;',
  19748. ' Left:=Left+33;',
  19749. ' NotRight:=NotRight+34;',
  19750. ' end;',
  19751. ' tbird.Speed:=tbird.Speed+12;',
  19752. ' tbird.Left:=tbird.Left+13;',
  19753. ' tbird.NotRight:=tbird.NotRight+14;',
  19754. ' with tbird do begin',
  19755. ' Speed:=Speed+32;',
  19756. ' Left:=Left+33;',
  19757. ' NotRight:=NotRight+34;',
  19758. ' end;',
  19759. '']);
  19760. ConvertProgram;
  19761. CheckSource('TestClassHelper_ClassPropertyStatic',
  19762. LinesToStr([ // statements
  19763. 'rtl.createClass($mod, "TObject", null, function () {',
  19764. ' this.$init = function () {',
  19765. ' };',
  19766. ' this.$final = function () {',
  19767. ' };',
  19768. ' this.GetSpeed = function () {',
  19769. ' var Result = 0;',
  19770. ' this.SetSpeed(this.GetSpeed() + 12);',
  19771. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  19772. ' return Result;',
  19773. ' };',
  19774. ' this.SetSpeed = function (Value) {',
  19775. ' };',
  19776. '});',
  19777. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19778. ' this.GetLeft = function () {',
  19779. ' var Result = 0;',
  19780. ' this.SetSpeed(this.GetSpeed() + 12);',
  19781. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  19782. ' return Result;',
  19783. ' };',
  19784. ' this.SetLeft = function (Value) {',
  19785. ' };',
  19786. '});',
  19787. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19788. ' this.DoIt = function () {',
  19789. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  19790. ' };',
  19791. ' this.DoSome = function () {',
  19792. ' this.SetSpeed(this.GetSpeed() + 12);',
  19793. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  19794. ' this.SetSpeed(this.GetSpeed() + 22);',
  19795. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  19796. ' this.SetSpeed(this.GetSpeed() + 32);',
  19797. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  19798. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  19799. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  19800. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  19801. ' };',
  19802. '});',
  19803. 'this.b = null;',
  19804. 'this.c = null;',
  19805. '']),
  19806. LinesToStr([ // $mod.$main
  19807. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  19808. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  19809. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  19810. 'var $with1 = $mod.b;',
  19811. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  19812. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  19813. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  19814. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  19815. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  19816. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  19817. 'var $with2 = $mod.c;',
  19818. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  19819. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  19820. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  19821. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  19822. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  19823. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  19824. 'var $with3 = $mod.TBird;',
  19825. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  19826. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  19827. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  19828. '']));
  19829. end;
  19830. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  19831. begin
  19832. StartProgram(false);
  19833. Add([
  19834. 'type',
  19835. ' TObject = class',
  19836. ' class function GetSpeed(Index: boolean): word;',
  19837. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  19838. ' end;',
  19839. ' TObjHelper = class helper for TObject',
  19840. ' class function GetSize(Index: boolean): word;',
  19841. ' class procedure SetSize(Index: boolean; Value: word);',
  19842. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  19843. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  19844. ' end;',
  19845. ' TBird = class',
  19846. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  19847. ' class procedure DoIt;',
  19848. ' end;',
  19849. ' TBirdClass = class of TBird;',
  19850. 'class function Tobject.GetSpeed(Index: boolean): word;',
  19851. 'begin',
  19852. ' Size[true]:=Size[false]+11;',
  19853. ' Speed[true]:=Speed[false]+12;',
  19854. ' Self.Size[true]:=Self.Size[false]+21;',
  19855. ' Self.Speed[true]:=Self.Speed[false]+22;',
  19856. ' with Self do begin',
  19857. ' Size[true]:=Size[false]+31;',
  19858. ' Speed[true]:=Speed[false]+32;',
  19859. ' end;',
  19860. 'end;',
  19861. 'class function TObjHelper.GetSize(Index: boolean): word;',
  19862. 'begin',
  19863. ' Size[true]:=Size[false]+11;',
  19864. ' Speed[true]:=Speed[false]+12;',
  19865. ' Self.Size[true]:=Self.Size[false]+21;',
  19866. ' Self.Speed[true]:=Self.Speed[false]+22;',
  19867. ' with Self do begin',
  19868. ' Size[true]:=Size[false]+31;',
  19869. ' Speed[true]:=Speed[false]+32;',
  19870. ' end;',
  19871. 'end;',
  19872. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  19873. 'begin',
  19874. 'end;',
  19875. 'class procedure TBird.DoIt;',
  19876. 'begin',
  19877. ' Items[true]:=Items[false]+11;',
  19878. ' Self.Items[true]:=Self.Items[false]+21;',
  19879. ' with Self do Items[true]:=Items[false]+31;',
  19880. 'end;',
  19881. 'var',
  19882. ' b: TBird;',
  19883. ' c: TBirdClass;',
  19884. 'begin',
  19885. ' b.Size[true]:=b.Size[false]+11;',
  19886. ' b.Speed[true]:=b.Speed[false]+12;',
  19887. ' b.Items[true]:=b.Items[false]+13;',
  19888. ' with b do begin',
  19889. ' Size[true]:=Size[false]+21;',
  19890. ' Speed[true]:=Speed[false]+22;',
  19891. ' Items[true]:=Items[false]+23;',
  19892. ' end;',
  19893. ' c.Size[true]:=c.Size[false]+11;',
  19894. ' c.Speed[true]:=c.Speed[false]+12;',
  19895. ' c.Items[true]:=c.Items[false]+13;',
  19896. ' with c do begin',
  19897. ' Size[true]:=Size[false]+21;',
  19898. ' Speed[true]:=Speed[false]+22;',
  19899. ' Items[true]:=Items[false]+23;',
  19900. ' end;',
  19901. ' TBird.Size[true]:=TBird.Size[false]+11;',
  19902. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  19903. ' TBird.Items[true]:=TBird.Items[false]+13;',
  19904. ' with TBird do begin',
  19905. ' Size[true]:=Size[false]+21;',
  19906. ' Speed[true]:=Speed[false]+22;',
  19907. ' Items[true]:=Items[false]+23;',
  19908. ' end;',
  19909. '']);
  19910. ConvertProgram;
  19911. CheckSource('TestClassHelper_ClassProperty_Array',
  19912. LinesToStr([ // statements
  19913. 'rtl.createClass($mod, "TObject", null, function () {',
  19914. ' this.$init = function () {',
  19915. ' };',
  19916. ' this.$final = function () {',
  19917. ' };',
  19918. ' this.GetSpeed = function (Index) {',
  19919. ' var Result = 0;',
  19920. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19921. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  19922. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19923. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  19924. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19925. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  19926. ' return Result;',
  19927. ' };',
  19928. '});',
  19929. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19930. ' this.GetSize = function (Index) {',
  19931. ' var Result = 0;',
  19932. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19933. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  19934. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19935. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  19936. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19937. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  19938. ' return Result;',
  19939. ' };',
  19940. ' this.SetSize = function (Index, Value) {',
  19941. ' };',
  19942. '});',
  19943. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19944. ' this.DoIt = function () {',
  19945. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19946. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19947. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19948. ' };',
  19949. '});',
  19950. 'this.b = null;',
  19951. 'this.c = null;',
  19952. '']),
  19953. LinesToStr([ // $mod.$main
  19954. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  19955. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  19956. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  19957. 'var $with1 = $mod.b;',
  19958. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 21);',
  19959. '$with1.$class.SetSpeed(true, $with1.$class.GetSpeed(false) + 22);',
  19960. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 23);',
  19961. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  19962. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  19963. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  19964. 'var $with2 = $mod.c;',
  19965. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 21);',
  19966. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  19967. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 23);',
  19968. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  19969. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  19970. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  19971. 'var $with3 = $mod.TBird;',
  19972. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  19973. '$with3.SetSpeed(true, $with3.GetSpeed(false) + 22);',
  19974. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  19975. '']));
  19976. end;
  19977. procedure TTestModule.TestClassHelper_ForIn;
  19978. begin
  19979. StartProgram(false);
  19980. Add([
  19981. 'type',
  19982. ' TObject = class end;',
  19983. ' TItem = TObject;',
  19984. ' TEnumerator = class',
  19985. ' FCurrent: TItem;',
  19986. ' property Current: TItem read FCurrent;',
  19987. ' function MoveNext: boolean;',
  19988. ' end;',
  19989. ' TBird = class',
  19990. ' end;',
  19991. ' TBirdHelper = class helper for TBird',
  19992. ' function GetEnumerator: TEnumerator;',
  19993. ' end;',
  19994. 'function TEnumerator.MoveNext: boolean;',
  19995. 'begin',
  19996. 'end;',
  19997. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  19998. 'begin',
  19999. 'end;',
  20000. 'var',
  20001. ' b: TBird;',
  20002. ' i, i2: TItem;',
  20003. 'begin',
  20004. ' for i in b do i2:=i;']);
  20005. ConvertProgram;
  20006. CheckSource('TestClassHelper_ForIn',
  20007. LinesToStr([ // statements
  20008. 'rtl.createClass($mod, "TObject", null, function () {',
  20009. ' this.$init = function () {',
  20010. ' };',
  20011. ' this.$final = function () {',
  20012. ' };',
  20013. '});',
  20014. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  20015. ' this.$init = function () {',
  20016. ' $mod.TObject.$init.call(this);',
  20017. ' this.FCurrent = null;',
  20018. ' };',
  20019. ' this.$final = function () {',
  20020. ' this.FCurrent = undefined;',
  20021. ' $mod.TObject.$final.call(this);',
  20022. ' };',
  20023. ' this.MoveNext = function () {',
  20024. ' var Result = false;',
  20025. ' return Result;',
  20026. ' };',
  20027. '});',
  20028. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20029. '});',
  20030. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20031. ' this.GetEnumerator = function () {',
  20032. ' var Result = null;',
  20033. ' return Result;',
  20034. ' };',
  20035. '});',
  20036. 'this.b = null;',
  20037. 'this.i = null;',
  20038. 'this.i2 = null;'
  20039. ]),
  20040. LinesToStr([ // $mod.$main
  20041. 'var $in1 = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  20042. 'try {',
  20043. ' while ($in1.MoveNext()){',
  20044. ' $mod.i = $in1.FCurrent;',
  20045. ' $mod.i2 = $mod.i;',
  20046. ' }',
  20047. '} finally {',
  20048. ' $in1 = rtl.freeLoc($in1)',
  20049. '};',
  20050. '']));
  20051. end;
  20052. procedure TTestModule.TestClassHelper_PassProperty;
  20053. begin
  20054. StartProgram(false);
  20055. Add([
  20056. 'type',
  20057. ' TObject = class',
  20058. ' FField: TObject;',
  20059. ' property Field: TObject read FField write FField;',
  20060. ' end;',
  20061. ' THelper = class helper for TObject',
  20062. ' procedure Fly;',
  20063. ' class procedure Run;',
  20064. ' class procedure Jump; static;',
  20065. ' end;',
  20066. 'procedure THelper.Fly;',
  20067. 'begin',
  20068. ' Field.Fly;',
  20069. ' Field.Run;',
  20070. ' Field.Jump;',
  20071. ' with Field do begin',
  20072. ' Fly;',
  20073. ' Run;',
  20074. ' Jump;',
  20075. ' end;',
  20076. 'end;',
  20077. 'class procedure THelper.Run;',
  20078. 'begin',
  20079. 'end;',
  20080. 'class procedure THelper.Jump;',
  20081. 'begin',
  20082. 'end;',
  20083. 'var',
  20084. ' b: TObject;',
  20085. 'begin',
  20086. ' b.Field.Fly;',
  20087. ' b.Field.Run;',
  20088. ' b.Field.Jump;',
  20089. ' with b do begin',
  20090. ' Field.Run;',
  20091. ' Field.Fly;',
  20092. ' Field.Jump;',
  20093. ' end;',
  20094. ' with b.Field do begin',
  20095. ' Run;',
  20096. ' Fly;',
  20097. ' Jump;',
  20098. ' end;',
  20099. '']);
  20100. ConvertProgram;
  20101. CheckSource('TestClassHelper_PassProperty',
  20102. LinesToStr([ // statements
  20103. 'rtl.createClass($mod, "TObject", null, function () {',
  20104. ' this.$init = function () {',
  20105. ' this.FField = null;',
  20106. ' };',
  20107. ' this.$final = function () {',
  20108. ' this.FField = undefined;',
  20109. ' };',
  20110. '});',
  20111. 'rtl.createHelper($mod, "THelper", null, function () {',
  20112. ' this.Fly = function () {',
  20113. ' $mod.THelper.Fly.call(this.FField);',
  20114. ' $mod.THelper.Run.call(this.FField.$class);',
  20115. ' $mod.THelper.Jump();',
  20116. ' var $with1 = this.FField;',
  20117. ' $mod.THelper.Fly.call($with1);',
  20118. ' $mod.THelper.Run.call($with1.$class);',
  20119. ' $mod.THelper.Jump();',
  20120. ' };',
  20121. ' this.Run = function () {',
  20122. ' };',
  20123. ' this.Jump = function () {',
  20124. ' };',
  20125. '});',
  20126. 'this.b = null;',
  20127. '']),
  20128. LinesToStr([ // $mod.$main
  20129. '$mod.THelper.Fly.call($mod.b.FField);',
  20130. '$mod.THelper.Run.call($mod.b.FField.$class);',
  20131. '$mod.THelper.Jump();',
  20132. 'var $with1 = $mod.b;',
  20133. '$mod.THelper.Run.call($with1.FField.$class);',
  20134. '$mod.THelper.Fly.call($with1.FField);',
  20135. '$mod.THelper.Jump();',
  20136. 'var $with2 = $mod.b.FField;',
  20137. '$mod.THelper.Run.call($with2.$class);',
  20138. '$mod.THelper.Fly.call($with2);',
  20139. '$mod.THelper.Jump();',
  20140. '']));
  20141. end;
  20142. procedure TTestModule.TestExtClassHelper_ClassVar;
  20143. begin
  20144. StartProgram(false);
  20145. Add([
  20146. '{$modeswitch externalclass}',
  20147. 'type',
  20148. ' TExtA = class external name ''ExtObj''',
  20149. ' end;',
  20150. ' THelper = class helper for TExtA',
  20151. ' const',
  20152. ' One = 1;',
  20153. ' Two: word = 2;',
  20154. ' class var',
  20155. ' Glob: word;',
  20156. ' function Foo(w: word): word;',
  20157. ' class function Bar(w: word): word; static;',
  20158. ' end;',
  20159. 'function THelper.foo(w: word): word;',
  20160. 'begin',
  20161. ' Result:=w;',
  20162. ' Two:=One+w;',
  20163. ' Glob:=Glob;',
  20164. ' Result:=Self.Glob;',
  20165. ' Self.Glob:=Self.Glob;',
  20166. ' with Self do Glob:=Glob;',
  20167. 'end;',
  20168. 'class function THelper.bar(w: word): word;',
  20169. 'begin',
  20170. ' Result:=w;',
  20171. ' Two:=One;',
  20172. ' Glob:=Glob;',
  20173. 'end;',
  20174. 'var o: TExtA;',
  20175. 'begin',
  20176. ' texta.two:=texta.one;',
  20177. ' texta.Glob:=texta.Glob;',
  20178. ' with texta do begin',
  20179. ' two:=one;',
  20180. ' Glob:=Glob;',
  20181. ' end;',
  20182. ' o.two:=o.one;',
  20183. ' o.Glob:=o.Glob;',
  20184. ' with o do begin',
  20185. ' two:=one;',
  20186. ' Glob:=Glob;',
  20187. ' end;',
  20188. '']);
  20189. ConvertProgram;
  20190. CheckSource('TestExtClassHelper_ClassVar',
  20191. LinesToStr([ // statements
  20192. 'rtl.createHelper($mod, "THelper", null, function () {',
  20193. ' this.One = 1;',
  20194. ' this.Two = 2;',
  20195. ' this.Glob = 0;',
  20196. ' this.Foo = function (w) {',
  20197. ' var Result = 0;',
  20198. ' Result = w;',
  20199. ' $mod.THelper.Two = 1 + w;',
  20200. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20201. ' Result = $mod.THelper.Glob;',
  20202. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20203. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20204. ' return Result;',
  20205. ' };',
  20206. ' this.Bar = function (w) {',
  20207. ' var Result = 0;',
  20208. ' Result = w;',
  20209. ' $mod.THelper.Two = 1;',
  20210. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20211. ' return Result;',
  20212. ' };',
  20213. '});',
  20214. 'this.o = null;',
  20215. '']),
  20216. LinesToStr([ // $mod.$main
  20217. '$mod.THelper.Two = 1;',
  20218. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20219. '$mod.THelper.Two = 1;',
  20220. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20221. '$mod.THelper.Two = 1;',
  20222. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20223. 'var $with1 = $mod.o;',
  20224. '$mod.THelper.Two = 1;',
  20225. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20226. '']));
  20227. end;
  20228. procedure TTestModule.TestExtClassHelper_Method_Call;
  20229. begin
  20230. StartProgram(false);
  20231. Add([
  20232. '{$modeswitch externalclass}',
  20233. 'type',
  20234. ' TExtA = class external name ''ExtObj''',
  20235. ' procedure Run(w: word = 10);',
  20236. ' end;',
  20237. ' THelper = class helper for TExtA',
  20238. ' function Foo(w: word = 1): word;',
  20239. ' end;',
  20240. 'function THelper.foo(w: word): word;',
  20241. 'begin',
  20242. ' Run;',
  20243. ' Run();',
  20244. ' Run(11);',
  20245. ' Foo;',
  20246. ' Foo();',
  20247. ' Foo(12);',
  20248. ' Self.Foo;',
  20249. ' Self.Foo();',
  20250. ' Self.Foo(13);',
  20251. ' with Self do begin',
  20252. ' Foo;',
  20253. ' Foo();',
  20254. ' Foo(14);',
  20255. ' end;',
  20256. 'end;',
  20257. 'var Obj: TExtA;',
  20258. 'begin',
  20259. ' obj.Foo;',
  20260. ' obj.Foo();',
  20261. ' obj.Foo(21);',
  20262. ' with obj do begin',
  20263. ' Foo;',
  20264. ' Foo();',
  20265. ' Foo(22);',
  20266. ' end;',
  20267. '']);
  20268. ConvertProgram;
  20269. CheckSource('TestExtClassHelper_Method_Call',
  20270. LinesToStr([ // statements
  20271. 'rtl.createHelper($mod, "THelper", null, function () {',
  20272. ' this.Foo = function (w) {',
  20273. ' var Result = 0;',
  20274. ' this.Run(10);',
  20275. ' this.Run(10);',
  20276. ' this.Run(11);',
  20277. ' $mod.THelper.Foo.call(this, 1);',
  20278. ' $mod.THelper.Foo.call(this, 1);',
  20279. ' $mod.THelper.Foo.call(this, 12);',
  20280. ' $mod.THelper.Foo.call(this, 1);',
  20281. ' $mod.THelper.Foo.call(this, 1);',
  20282. ' $mod.THelper.Foo.call(this, 13);',
  20283. ' $mod.THelper.Foo.call(this, 1);',
  20284. ' $mod.THelper.Foo.call(this, 1);',
  20285. ' $mod.THelper.Foo.call(this, 14);',
  20286. ' return Result;',
  20287. ' };',
  20288. '});',
  20289. 'this.Obj = null;',
  20290. '']),
  20291. LinesToStr([ // $mod.$main
  20292. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20293. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20294. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20295. 'var $with1 = $mod.Obj;',
  20296. '$mod.THelper.Foo.call($with1, 1);',
  20297. '$mod.THelper.Foo.call($with1, 1);',
  20298. '$mod.THelper.Foo.call($with1, 22);',
  20299. '']));
  20300. end;
  20301. procedure TTestModule.TestRecordHelper_ClassVar;
  20302. begin
  20303. StartProgram(false);
  20304. Add([
  20305. 'type',
  20306. ' TRec = record',
  20307. ' end;',
  20308. ' THelper = record helper for TRec',
  20309. ' const',
  20310. ' One = 1;',
  20311. ' Two: word = 2;',
  20312. ' class var',
  20313. ' Glob: word;',
  20314. ' function Foo(w: word): word;',
  20315. ' class function Bar(w: word): word; static;',
  20316. ' end;',
  20317. 'function THelper.foo(w: word): word;',
  20318. 'begin',
  20319. ' Result:=w;',
  20320. ' Two:=One+w;',
  20321. ' Glob:=Glob;',
  20322. ' Result:=Self.Glob;',
  20323. ' Self.Glob:=Self.Glob;',
  20324. ' with Self do Glob:=Glob;',
  20325. ' Self:=Self;',
  20326. 'end;',
  20327. 'class function THelper.bar(w: word): word;',
  20328. 'begin',
  20329. ' Result:=w;',
  20330. ' Two:=One;',
  20331. ' Glob:=Glob;',
  20332. 'end;',
  20333. 'var r: TRec;',
  20334. 'begin',
  20335. ' trec.two:=trec.one;',
  20336. ' trec.Glob:=trec.Glob;',
  20337. ' with trec do begin',
  20338. ' two:=one;',
  20339. ' Glob:=Glob;',
  20340. ' end;',
  20341. ' r.two:=r.one;',
  20342. ' r.Glob:=r.Glob;',
  20343. ' with r do begin',
  20344. ' two:=one;',
  20345. ' Glob:=Glob;',
  20346. ' end;',
  20347. '']);
  20348. ConvertProgram;
  20349. CheckSource('TestRecordHelper_ClassVar',
  20350. LinesToStr([ // statements
  20351. 'rtl.recNewT($mod, "TRec", function () {',
  20352. ' this.$eq = function (b) {',
  20353. ' return true;',
  20354. ' };',
  20355. ' this.$assign = function (s) {',
  20356. ' return this;',
  20357. ' };',
  20358. '});',
  20359. 'rtl.createHelper($mod, "THelper", null, function () {',
  20360. ' this.One = 1;',
  20361. ' this.Two = 2;',
  20362. ' this.Glob = 0;',
  20363. ' this.Foo = function (w) {',
  20364. ' var Result = 0;',
  20365. ' Result = w;',
  20366. ' $mod.THelper.Two = 1 + w;',
  20367. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20368. ' Result = $mod.THelper.Glob;',
  20369. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20370. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20371. ' this.$assign(this);',
  20372. ' return Result;',
  20373. ' };',
  20374. ' this.Bar = function (w) {',
  20375. ' var Result = 0;',
  20376. ' Result = w;',
  20377. ' $mod.THelper.Two = 1;',
  20378. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20379. ' return Result;',
  20380. ' };',
  20381. '});',
  20382. 'this.r = $mod.TRec.$new();',
  20383. '']),
  20384. LinesToStr([ // $mod.$main
  20385. '$mod.THelper.Two = 1;',
  20386. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20387. 'var $with1 = $mod.TRec;',
  20388. '$mod.THelper.Two = 1;',
  20389. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20390. '$mod.THelper.Two = 1;',
  20391. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20392. 'var $with2 = $mod.r;',
  20393. '$mod.THelper.Two = 1;',
  20394. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20395. '']));
  20396. end;
  20397. procedure TTestModule.TestRecordHelper_Method_Call;
  20398. begin
  20399. StartProgram(false);
  20400. Add([
  20401. '{$modeswitch AdvancedRecords}',
  20402. 'type',
  20403. ' TRec = record',
  20404. ' procedure Run(w: word = 10);',
  20405. ' end;',
  20406. ' THelper = record helper for TRec',
  20407. ' function Foo(w: word = 1): word;',
  20408. ' end;',
  20409. 'procedure TRec.Run(w: word);',
  20410. 'begin',
  20411. ' Foo;',
  20412. ' Foo();',
  20413. ' Foo(2);',
  20414. ' Self.Foo;',
  20415. ' Self.Foo();',
  20416. ' Self.Foo(3);',
  20417. ' with Self do begin',
  20418. ' Foo;',
  20419. ' Foo();',
  20420. ' Foo(4);',
  20421. ' end;',
  20422. 'end;',
  20423. 'function THelper.foo(w: word): word;',
  20424. 'begin',
  20425. ' Run;',
  20426. ' Run();',
  20427. ' Run(11);',
  20428. ' Foo;',
  20429. ' Foo();',
  20430. ' Foo(12);',
  20431. ' Self.Foo;',
  20432. ' Self.Foo();',
  20433. ' Self.Foo(13);',
  20434. ' with Self do begin',
  20435. ' Foo;',
  20436. ' Foo();',
  20437. ' Foo(14);',
  20438. ' end;',
  20439. 'end;',
  20440. 'var Rec: TRec;',
  20441. 'begin',
  20442. ' Rec.Foo;',
  20443. ' Rec.Foo();',
  20444. ' Rec.Foo(21);',
  20445. ' with Rec do begin',
  20446. ' Foo;',
  20447. ' Foo();',
  20448. ' Foo(22);',
  20449. ' end;',
  20450. '']);
  20451. ConvertProgram;
  20452. CheckSource('TestRecordHelper_Method_Call',
  20453. LinesToStr([ // statements
  20454. 'rtl.recNewT($mod, "TRec", function () {',
  20455. ' this.$eq = function (b) {',
  20456. ' return true;',
  20457. ' };',
  20458. ' this.$assign = function (s) {',
  20459. ' return this;',
  20460. ' };',
  20461. ' this.Run = function (w) {',
  20462. ' $mod.THelper.Foo.call(this, 1);',
  20463. ' $mod.THelper.Foo.call(this, 1);',
  20464. ' $mod.THelper.Foo.call(this, 2);',
  20465. ' $mod.THelper.Foo.call(this, 1);',
  20466. ' $mod.THelper.Foo.call(this, 1);',
  20467. ' $mod.THelper.Foo.call(this, 3);',
  20468. ' $mod.THelper.Foo.call(this, 1);',
  20469. ' $mod.THelper.Foo.call(this, 1);',
  20470. ' $mod.THelper.Foo.call(this, 4);',
  20471. ' };',
  20472. '});',
  20473. 'rtl.createHelper($mod, "THelper", null, function () {',
  20474. ' this.Foo = function (w) {',
  20475. ' var Result = 0;',
  20476. ' this.Run(10);',
  20477. ' this.Run(10);',
  20478. ' this.Run(11);',
  20479. ' $mod.THelper.Foo.call(this, 1);',
  20480. ' $mod.THelper.Foo.call(this, 1);',
  20481. ' $mod.THelper.Foo.call(this, 12);',
  20482. ' $mod.THelper.Foo.call(this, 1);',
  20483. ' $mod.THelper.Foo.call(this, 1);',
  20484. ' $mod.THelper.Foo.call(this, 13);',
  20485. ' $mod.THelper.Foo.call(this, 1);',
  20486. ' $mod.THelper.Foo.call(this, 1);',
  20487. ' $mod.THelper.Foo.call(this, 14);',
  20488. ' return Result;',
  20489. ' };',
  20490. '});',
  20491. 'this.Rec = $mod.TRec.$new();',
  20492. '']),
  20493. LinesToStr([ // $mod.$main
  20494. '$mod.THelper.Foo.call($mod.Rec, 1);',
  20495. '$mod.THelper.Foo.call($mod.Rec, 1);',
  20496. '$mod.THelper.Foo.call($mod.Rec, 21);',
  20497. 'var $with1 = $mod.Rec;',
  20498. '$mod.THelper.Foo.call($with1, 1);',
  20499. '$mod.THelper.Foo.call($with1, 1);',
  20500. '$mod.THelper.Foo.call($with1, 22);',
  20501. '']));
  20502. end;
  20503. procedure TTestModule.TestRecordHelper_Constructor;
  20504. begin
  20505. StartProgram(false);
  20506. Add([
  20507. '{$modeswitch AdvancedRecords}',
  20508. 'type',
  20509. ' TRec = record',
  20510. ' constructor Create(w: word);',
  20511. ' end;',
  20512. ' THelper = record helper for TRec',
  20513. ' constructor NewHlp(w: word);',
  20514. ' end;',
  20515. 'var',
  20516. ' Rec: TRec;',
  20517. 'constructor TRec.Create(w: word);',
  20518. 'begin',
  20519. ' NewHlp(2);', // normal call
  20520. ' trec.NewHlp(3);', // new instance
  20521. 'end;',
  20522. 'constructor THelper.NewHlp(w: word);',
  20523. 'begin',
  20524. ' create(2);', // normal call
  20525. ' trec.create(3);', // new instance
  20526. ' NewHlp(4);', // normal call
  20527. ' trec.NewHlp(5);', // new instance
  20528. 'end;',
  20529. 'begin',
  20530. ' rec.newhlp(2);', // normal call
  20531. ' with rec do newhlp(12);', // normal call
  20532. ' trec.newhlp(3);', // new instance
  20533. ' with trec do newhlp(13);', // new instance
  20534. '']);
  20535. ConvertProgram;
  20536. CheckSource('TestRecordHelper_Constructor',
  20537. LinesToStr([ // statements
  20538. 'rtl.recNewT($mod, "TRec", function () {',
  20539. ' this.$eq = function (b) {',
  20540. ' return true;',
  20541. ' };',
  20542. ' this.$assign = function (s) {',
  20543. ' return this;',
  20544. ' };',
  20545. ' this.Create = function (w) {',
  20546. ' $mod.THelper.NewHlp.call(this, 2);',
  20547. ' $mod.THelper.$new("NewHlp", [3]);',
  20548. ' return this;',
  20549. ' };',
  20550. '}, true);',
  20551. 'rtl.createHelper($mod, "THelper", null, function () {',
  20552. ' this.NewHlp = function (w) {',
  20553. ' this.Create(2);',
  20554. ' $mod.TRec.$create("Create", [3]);',
  20555. ' $mod.THelper.NewHlp.call(this, 4);',
  20556. ' $mod.THelper.$new("NewHlp", [5]);',
  20557. ' return this;',
  20558. ' };',
  20559. ' this.$new = function (fn, args) {',
  20560. ' return this[fn].apply($mod.TRec.$new(), args);',
  20561. ' };',
  20562. '});',
  20563. 'this.Rec = $mod.TRec.$new();',
  20564. '']),
  20565. LinesToStr([ // $mod.$main
  20566. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  20567. 'var $with1 = $mod.Rec;',
  20568. '$mod.THelper.NewHlp.call($with1, 12);',
  20569. '$mod.THelper.$new("NewHlp", [3]);',
  20570. 'var $with2 = $mod.TRec;',
  20571. '$mod.THelper.$new("NewHlp", [13]);',
  20572. '']));
  20573. end;
  20574. procedure TTestModule.TestTypeHelper_ClassVar;
  20575. begin
  20576. StartProgram(false);
  20577. Add([
  20578. '{$modeswitch typehelpers}',
  20579. 'type',
  20580. ' THelper = type helper for byte',
  20581. ' const',
  20582. ' One = 1;',
  20583. ' Two: word = 2;',
  20584. ' class var',
  20585. ' Glob: word;',
  20586. ' function Foo(w: word): word;',
  20587. ' class function Bar(w: word): word; static;',
  20588. ' end;',
  20589. 'function THelper.foo(w: word): word;',
  20590. 'begin',
  20591. ' Result:=w;',
  20592. ' Two:=One+w;',
  20593. ' Glob:=Glob;',
  20594. ' Result:=Self.Glob;',
  20595. ' Self.Glob:=Self.Glob;',
  20596. ' with Self do Glob:=Glob;',
  20597. 'end;',
  20598. 'class function THelper.bar(w: word): word;',
  20599. 'begin',
  20600. ' Result:=w;',
  20601. ' Two:=One;',
  20602. ' Glob:=Glob;',
  20603. 'end;',
  20604. 'var b: byte;',
  20605. 'begin',
  20606. ' byte.two:=byte.one;',
  20607. ' byte.Glob:=byte.Glob;',
  20608. ' with byte do begin',
  20609. ' two:=one;',
  20610. ' Glob:=Glob;',
  20611. ' end;',
  20612. ' b.two:=b.one;',
  20613. ' b.Glob:=b.Glob;',
  20614. ' with b do begin',
  20615. ' two:=one;',
  20616. ' Glob:=Glob;',
  20617. ' end;',
  20618. '']);
  20619. ConvertProgram;
  20620. CheckSource('TestTypeHelper_ClassVar',
  20621. LinesToStr([ // statements
  20622. 'rtl.createHelper($mod, "THelper", null, function () {',
  20623. ' this.One = 1;',
  20624. ' this.Two = 2;',
  20625. ' this.Glob = 0;',
  20626. ' this.Foo = function (w) {',
  20627. ' var Result = 0;',
  20628. ' Result = w;',
  20629. ' $mod.THelper.Two = 1 + w;',
  20630. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20631. ' Result = $mod.THelper.Glob;',
  20632. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20633. ' var $with1 = this.get();',
  20634. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20635. ' return Result;',
  20636. ' };',
  20637. ' this.Bar = function (w) {',
  20638. ' var Result = 0;',
  20639. ' Result = w;',
  20640. ' $mod.THelper.Two = 1;',
  20641. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20642. ' return Result;',
  20643. ' };',
  20644. '});',
  20645. 'this.b = 0;',
  20646. '']),
  20647. LinesToStr([ // $mod.$main
  20648. '$mod.THelper.Two = 1;',
  20649. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20650. '$mod.THelper.Two = 1;',
  20651. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20652. '$mod.THelper.Two = 1;',
  20653. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20654. 'var $with1 = $mod.b;',
  20655. '$mod.THelper.Two = 1;',
  20656. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20657. '']));
  20658. end;
  20659. procedure TTestModule.TestTypeHelper_PassResultElement;
  20660. begin
  20661. StartProgram(false);
  20662. Add([
  20663. '{$modeswitch typehelpers}',
  20664. 'type',
  20665. ' THelper = type helper for word',
  20666. ' procedure DoIt(e: byte = 123);',
  20667. ' class procedure DoSome(e: byte = 456); static;',
  20668. ' end;',
  20669. 'procedure THelper.DoIt(e: byte);',
  20670. 'begin',
  20671. 'end;',
  20672. 'class procedure THelper.DoSome(e: byte);',
  20673. 'begin',
  20674. 'end;',
  20675. 'function Foo(w: word): word;',
  20676. 'begin',
  20677. ' Result.DoIt;',
  20678. ' Result.DoIt();',
  20679. ' Result.DoSome;',
  20680. ' Result.DoSome();',
  20681. ' with Result do begin',
  20682. ' DoIt;',
  20683. ' DoIt();',
  20684. ' DoSome;',
  20685. ' DoSome();',
  20686. ' end;',
  20687. 'end;',
  20688. 'begin',
  20689. '']);
  20690. ConvertProgram;
  20691. CheckSource('TestTypeHelper_PassResultElement',
  20692. LinesToStr([ // statements
  20693. 'rtl.createHelper($mod, "THelper", null, function () {',
  20694. ' this.DoIt = function (e) {',
  20695. ' };',
  20696. ' this.DoSome = function (e) {',
  20697. ' };',
  20698. '});',
  20699. 'this.Foo = function (w) {',
  20700. ' var Result = 0;',
  20701. ' $mod.THelper.DoIt.call({',
  20702. ' get: function () {',
  20703. ' return Result;',
  20704. ' },',
  20705. ' set: function (v) {',
  20706. ' Result = v;',
  20707. ' }',
  20708. ' }, 123);',
  20709. ' $mod.THelper.DoIt.call({',
  20710. ' get: function () {',
  20711. ' return Result;',
  20712. ' },',
  20713. ' set: function (v) {',
  20714. ' Result = v;',
  20715. ' }',
  20716. ' }, 123);',
  20717. ' $mod.THelper.DoSome(456);',
  20718. ' $mod.THelper.DoSome(456);',
  20719. ' $mod.THelper.DoIt.call({',
  20720. ' get: function () {',
  20721. ' return Result;',
  20722. ' },',
  20723. ' set: function (v) {',
  20724. ' Result = v;',
  20725. ' }',
  20726. ' }, 123);',
  20727. ' $mod.THelper.DoIt.call({',
  20728. ' get: function () {',
  20729. ' return Result;',
  20730. ' },',
  20731. ' set: function (v) {',
  20732. ' Result = v;',
  20733. ' }',
  20734. ' }, 123);',
  20735. ' $mod.THelper.DoSome(456);',
  20736. ' $mod.THelper.DoSome(456);',
  20737. ' return Result;',
  20738. '};',
  20739. '']),
  20740. LinesToStr([ // $mod.$main
  20741. '']));
  20742. end;
  20743. procedure TTestModule.TestTypeHelper_PassArgs;
  20744. begin
  20745. StartProgram(false);
  20746. Add([
  20747. '{$modeswitch typehelpers}',
  20748. 'type',
  20749. ' THelper = type helper for word',
  20750. ' procedure DoIt(e: byte = 123);',
  20751. ' end;',
  20752. 'procedure THelper.DoIt(e: byte);',
  20753. 'begin',
  20754. 'end;',
  20755. 'procedure FooDefault(a: word);',
  20756. 'begin',
  20757. ' a.DoIt;',
  20758. ' with a do DoIt;',
  20759. 'end;',
  20760. 'procedure FooConst(const a: word);',
  20761. 'begin',
  20762. ' a.DoIt;',
  20763. ' with a do DoIt;',
  20764. 'end;',
  20765. 'procedure FooVar(var a: word);',
  20766. 'begin',
  20767. ' a.DoIt;',
  20768. ' with a do DoIt;',
  20769. 'end;',
  20770. 'begin',
  20771. '']);
  20772. ConvertProgram;
  20773. CheckSource('TestTypeHelper_PassArgs',
  20774. LinesToStr([ // statements
  20775. 'rtl.createHelper($mod, "THelper", null, function () {',
  20776. ' this.DoIt = function (e) {',
  20777. ' };',
  20778. '});',
  20779. 'this.FooDefault = function (a) {',
  20780. ' $mod.THelper.DoIt.call({',
  20781. ' get: function () {',
  20782. ' return a;',
  20783. ' },',
  20784. ' set: function (v) {',
  20785. ' a = v;',
  20786. ' }',
  20787. ' }, 123);',
  20788. ' $mod.THelper.DoIt.call({',
  20789. ' get: function () {',
  20790. ' return a;',
  20791. ' },',
  20792. ' set: function (v) {',
  20793. ' a = v;',
  20794. ' }',
  20795. ' }, 123);',
  20796. '};',
  20797. 'this.FooConst = function (a) {',
  20798. ' $mod.THelper.DoIt.call({',
  20799. ' get: function () {',
  20800. ' return a;',
  20801. ' },',
  20802. ' set: function (v) {',
  20803. ' rtl.raiseE("EPropReadOnly");',
  20804. ' }',
  20805. ' }, 123);',
  20806. ' $mod.THelper.DoIt.call({',
  20807. ' get: function () {',
  20808. ' return a;',
  20809. ' },',
  20810. ' set: function () {',
  20811. ' rtl.raiseE("EPropReadOnly");',
  20812. ' }',
  20813. ' }, 123);',
  20814. '};',
  20815. 'this.FooVar = function (a) {',
  20816. ' $mod.THelper.DoIt.call(a, 123);',
  20817. ' var $with1 = a.get();',
  20818. ' $mod.THelper.DoIt.call(a, 123);',
  20819. '};',
  20820. '']),
  20821. LinesToStr([ // $mod.$main
  20822. '']));
  20823. end;
  20824. procedure TTestModule.TestTypeHelper_PassVarConst;
  20825. begin
  20826. StartProgram(false);
  20827. Add([
  20828. '{$modeswitch typehelpers}',
  20829. 'type',
  20830. ' THelper = type helper for word',
  20831. ' procedure DoIt(e: byte = 123);',
  20832. ' end;',
  20833. 'procedure THelper.DoIt(e: byte);',
  20834. 'begin',
  20835. 'end;',
  20836. 'var a: word;',
  20837. 'const c: word = 2;',
  20838. '{$writeableconst off}',
  20839. 'const r: word = 3;',
  20840. 'begin',
  20841. ' a.DoIt;',
  20842. ' with a do DoIt;',
  20843. ' c.DoIt;',
  20844. ' with c do DoIt;',
  20845. ' r.DoIt;',
  20846. ' with r do DoIt;',
  20847. '']);
  20848. ConvertProgram;
  20849. CheckSource('TestTypeHelper_PassVarConst',
  20850. LinesToStr([ // statements
  20851. 'rtl.createHelper($mod, "THelper", null, function () {',
  20852. ' this.DoIt = function (e) {',
  20853. ' };',
  20854. '});',
  20855. 'this.a = 0;',
  20856. 'this.c = 2;',
  20857. 'this.r = 3;',
  20858. '']),
  20859. LinesToStr([ // $mod.$main
  20860. '$mod.THelper.DoIt.call({',
  20861. ' p: $mod,',
  20862. ' get: function () {',
  20863. ' return this.p.a;',
  20864. ' },',
  20865. ' set: function (v) {',
  20866. ' this.p.a = v;',
  20867. ' }',
  20868. '}, 123);',
  20869. 'var $with1 = $mod.a;',
  20870. '$mod.THelper.DoIt.call({',
  20871. ' get: function () {',
  20872. ' return $with1;',
  20873. ' },',
  20874. ' set: function (v) {',
  20875. ' $with1 = v;',
  20876. ' }',
  20877. '}, 123);',
  20878. '$mod.THelper.DoIt.call({',
  20879. ' p: $mod,',
  20880. ' get: function () {',
  20881. ' return this.p.c;',
  20882. ' },',
  20883. ' set: function (v) {',
  20884. ' this.p.c = v;',
  20885. ' }',
  20886. '}, 123);',
  20887. 'var $with2 = $mod.c;',
  20888. '$mod.THelper.DoIt.call({',
  20889. ' get: function () {',
  20890. ' return $with2;',
  20891. ' },',
  20892. ' set: function (v) {',
  20893. ' $with2 = v;',
  20894. ' }',
  20895. '}, 123);',
  20896. '$mod.THelper.DoIt.call({',
  20897. ' get: function () {',
  20898. ' return 3;',
  20899. ' },',
  20900. ' set: function (v) {',
  20901. ' rtl.raiseE("EPropReadOnly");',
  20902. ' }',
  20903. '}, 123);',
  20904. 'var $with3 = 3;',
  20905. ' $mod.THelper.DoIt.call({',
  20906. ' get: function () {',
  20907. ' return $with3;',
  20908. ' },',
  20909. ' set: function () {',
  20910. ' rtl.raiseE("EPropReadOnly");',
  20911. ' }',
  20912. ' }, 123);',
  20913. '']));
  20914. end;
  20915. procedure TTestModule.TestTypeHelper_PassFuncResult;
  20916. begin
  20917. StartProgram(false);
  20918. Add([
  20919. '{$modeswitch typehelpers}',
  20920. 'type',
  20921. ' THelper = type helper for word',
  20922. ' procedure DoIt(e: byte = 123);',
  20923. ' end;',
  20924. 'procedure THelper.DoIt(e: byte);',
  20925. 'begin',
  20926. 'end;',
  20927. 'function Foo(b: byte = 1): word;',
  20928. 'begin',
  20929. 'end;',
  20930. 'begin',
  20931. ' Foo.DoIt;',
  20932. ' Foo().DoIt;',
  20933. ' with Foo do DoIt;',
  20934. ' with Foo() do DoIt;',
  20935. '']);
  20936. ConvertProgram;
  20937. CheckSource('TestTypeHelper_PassFuncResult',
  20938. LinesToStr([ // statements
  20939. 'rtl.createHelper($mod, "THelper", null, function () {',
  20940. ' this.DoIt = function (e) {',
  20941. ' };',
  20942. '});',
  20943. 'this.Foo = function (b) {',
  20944. ' var Result = 0;',
  20945. ' return Result;',
  20946. '};',
  20947. '']),
  20948. LinesToStr([ // $mod.$main
  20949. '$mod.THelper.DoIt.call({',
  20950. ' a: $mod.Foo(1),',
  20951. ' get: function () {',
  20952. ' return this.a;',
  20953. ' },',
  20954. ' set: function (v) {',
  20955. ' this.a = v;',
  20956. ' }',
  20957. '}, 123);',
  20958. '$mod.THelper.DoIt.call({',
  20959. ' a: $mod.Foo(1),',
  20960. ' get: function () {',
  20961. ' return this.a;',
  20962. ' },',
  20963. ' set: function (v) {',
  20964. ' this.a = v;',
  20965. ' }',
  20966. '}, 123);',
  20967. 'var $with1 = $mod.Foo(1);',
  20968. '$mod.THelper.DoIt.call({',
  20969. ' get: function () {',
  20970. ' return $with1;',
  20971. ' },',
  20972. ' set: function (v) {',
  20973. ' $with1 = v;',
  20974. ' }',
  20975. '}, 123);',
  20976. 'var $with2 = $mod.Foo(1);',
  20977. '$mod.THelper.DoIt.call({',
  20978. ' get: function () {',
  20979. ' return $with2;',
  20980. ' },',
  20981. ' set: function (v) {',
  20982. ' $with2 = v;',
  20983. ' }',
  20984. '}, 123);',
  20985. '']));
  20986. end;
  20987. procedure TTestModule.TestTypeHelper_PassPropertyField;
  20988. begin
  20989. StartProgram(false);
  20990. Add([
  20991. '{$modeswitch typehelpers}',
  20992. 'type',
  20993. ' TObject = class',
  20994. ' FField: word;',
  20995. ' procedure SetField(Value: word);',
  20996. ' property Field: word read FField write SetField;',
  20997. ' end;',
  20998. ' THelper = type helper for word',
  20999. ' procedure Fly;',
  21000. ' class procedure Run; static;',
  21001. ' end;',
  21002. 'procedure TObject.SetField(Value: word);',
  21003. 'begin',
  21004. ' Field.Fly;',
  21005. ' Field.Run;',
  21006. ' Self.Field.Fly;',
  21007. ' Self.Field.Run;',
  21008. ' with Self do begin',
  21009. ' Field.Fly;',
  21010. ' Field.Run;',
  21011. ' end;',
  21012. ' with Self.Field do begin',
  21013. ' Fly;',
  21014. ' Run;',
  21015. ' end;',
  21016. 'end;',
  21017. 'procedure THelper.Fly;',
  21018. 'begin',
  21019. 'end;',
  21020. 'class procedure THelper.Run;',
  21021. 'begin',
  21022. 'end;',
  21023. 'var',
  21024. ' o: TObject;',
  21025. 'begin',
  21026. ' o.Field.Fly;',
  21027. ' o.Field.Run;',
  21028. ' with o do begin',
  21029. ' Field.Fly;',
  21030. ' Field.Run;',
  21031. ' end;',
  21032. ' with o.Field do begin',
  21033. ' Fly;',
  21034. ' Run;',
  21035. ' end;',
  21036. '']);
  21037. ConvertProgram;
  21038. CheckSource('TestTypeHelper_PassPropertyField',
  21039. LinesToStr([ // statements
  21040. 'rtl.createClass($mod, "TObject", null, function () {',
  21041. ' this.$init = function () {',
  21042. ' this.FField = 0;',
  21043. ' };',
  21044. ' this.$final = function () {',
  21045. ' };',
  21046. ' this.SetField = function (Value) {',
  21047. ' $mod.THelper.Fly.call({',
  21048. ' p: this,',
  21049. ' get: function () {',
  21050. ' return this.p.FField;',
  21051. ' },',
  21052. ' set: function (v) {',
  21053. ' this.p.FField = v;',
  21054. ' }',
  21055. ' });',
  21056. ' $mod.THelper.Run();',
  21057. ' $mod.THelper.Fly.call({',
  21058. ' p: this,',
  21059. ' get: function () {',
  21060. ' return this.p.FField;',
  21061. ' },',
  21062. ' set: function (v) {',
  21063. ' this.p.FField = v;',
  21064. ' }',
  21065. ' });',
  21066. ' $mod.THelper.Run();',
  21067. ' $mod.THelper.Fly.call({',
  21068. ' p: this,',
  21069. ' get: function () {',
  21070. ' return this.p.FField;',
  21071. ' },',
  21072. ' set: function (v) {',
  21073. ' this.p.FField = v;',
  21074. ' }',
  21075. ' });',
  21076. ' $mod.THelper.Run();',
  21077. ' var $with1 = this.FField;',
  21078. ' $mod.THelper.Fly.call({',
  21079. ' get: function () {',
  21080. ' return $with1;',
  21081. ' },',
  21082. ' set: function (v) {',
  21083. ' $with1 = v;',
  21084. ' }',
  21085. ' });',
  21086. ' $mod.THelper.Run();',
  21087. ' };',
  21088. '});',
  21089. 'rtl.createHelper($mod, "THelper", null, function () {',
  21090. ' this.Fly = function () {',
  21091. ' };',
  21092. ' this.Run = function () {',
  21093. ' };',
  21094. '});',
  21095. 'this.o = null;',
  21096. '']),
  21097. LinesToStr([ // $mod.$main
  21098. '$mod.THelper.Fly.call({',
  21099. ' p: $mod.o,',
  21100. ' get: function () {',
  21101. ' return this.p.FField;',
  21102. ' },',
  21103. ' set: function (v) {',
  21104. ' this.p.FField = v;',
  21105. ' }',
  21106. '});',
  21107. '$mod.THelper.Run();',
  21108. 'var $with1 = $mod.o;',
  21109. '$mod.THelper.Fly.call({',
  21110. ' p: $with1,',
  21111. ' get: function () {',
  21112. ' return this.p.FField;',
  21113. ' },',
  21114. ' set: function (v) {',
  21115. ' this.p.FField = v;',
  21116. ' }',
  21117. '});',
  21118. '$mod.THelper.Run();',
  21119. 'var $with2 = $mod.o.FField;',
  21120. '$mod.THelper.Fly.call({',
  21121. ' get: function () {',
  21122. ' return $with2;',
  21123. ' },',
  21124. ' set: function (v) {',
  21125. ' $with2 = v;',
  21126. ' }',
  21127. '});',
  21128. '$mod.THelper.Run();',
  21129. '']));
  21130. end;
  21131. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  21132. begin
  21133. StartProgram(false);
  21134. Add([
  21135. '{$modeswitch typehelpers}',
  21136. 'type',
  21137. ' TObject = class',
  21138. ' FField: word;',
  21139. ' function GetField: word;',
  21140. ' property Field: word read GetField write FField;',
  21141. ' end;',
  21142. ' THelper = type helper for word',
  21143. ' procedure Fly;',
  21144. ' class procedure Run; static;',
  21145. ' end;',
  21146. 'function TObject.GetField: word;',
  21147. 'begin',
  21148. ' Field.Fly;',
  21149. ' Field.Run;',
  21150. ' Self.Field.Fly;',
  21151. ' Self.Field.Run;',
  21152. ' with Self do begin',
  21153. ' Field.Fly;',
  21154. ' Field.Run;',
  21155. ' end;',
  21156. ' with Self.Field do begin',
  21157. ' Fly;',
  21158. ' Run;',
  21159. ' end;',
  21160. 'end;',
  21161. 'procedure THelper.Fly;',
  21162. 'begin',
  21163. 'end;',
  21164. 'class procedure THelper.Run;',
  21165. 'begin',
  21166. 'end;',
  21167. 'var',
  21168. ' o: TObject;',
  21169. 'begin',
  21170. ' o.Field.Fly;',
  21171. ' o.Field.Run;',
  21172. ' with o do begin',
  21173. ' Field.Fly;',
  21174. ' Field.Run;',
  21175. ' end;',
  21176. ' with o.Field do begin',
  21177. ' Fly;',
  21178. ' Run;',
  21179. ' end;',
  21180. '']);
  21181. ConvertProgram;
  21182. CheckSource('TestTypeHelper_PassPropertyGetter',
  21183. LinesToStr([ // statements
  21184. 'rtl.createClass($mod, "TObject", null, function () {',
  21185. ' this.$init = function () {',
  21186. ' this.FField = 0;',
  21187. ' };',
  21188. ' this.$final = function () {',
  21189. ' };',
  21190. ' this.GetField = function () {',
  21191. ' var Result = 0;',
  21192. ' $mod.THelper.Fly.call({',
  21193. ' p: this.GetField(),',
  21194. ' get: function () {',
  21195. ' return this.p;',
  21196. ' },',
  21197. ' set: function (v) {',
  21198. ' this.p = v;',
  21199. ' }',
  21200. ' });',
  21201. ' $mod.THelper.Run();',
  21202. ' $mod.THelper.Fly.call({',
  21203. ' p: this.GetField(),',
  21204. ' get: function () {',
  21205. ' return this.p;',
  21206. ' },',
  21207. ' set: function (v) {',
  21208. ' this.p = v;',
  21209. ' }',
  21210. ' });',
  21211. ' $mod.THelper.Run();',
  21212. ' $mod.THelper.Fly.call({',
  21213. ' p: this.GetField(),',
  21214. ' get: function () {',
  21215. ' return this.p;',
  21216. ' },',
  21217. ' set: function (v) {',
  21218. ' this.p = v;',
  21219. ' }',
  21220. ' });',
  21221. ' $mod.THelper.Run();',
  21222. ' var $with1 = this.GetField();',
  21223. ' $mod.THelper.Fly.call({',
  21224. ' get: function () {',
  21225. ' return $with1;',
  21226. ' },',
  21227. ' set: function (v) {',
  21228. ' $with1 = v;',
  21229. ' }',
  21230. ' });',
  21231. ' $mod.THelper.Run();',
  21232. ' return Result;',
  21233. ' };',
  21234. '});',
  21235. 'rtl.createHelper($mod, "THelper", null, function () {',
  21236. ' this.Fly = function () {',
  21237. ' };',
  21238. ' this.Run = function () {',
  21239. ' };',
  21240. '});',
  21241. 'this.o = null;',
  21242. '']),
  21243. LinesToStr([ // $mod.$main
  21244. '$mod.THelper.Fly.call({',
  21245. ' p: $mod.o.GetField(),',
  21246. ' get: function () {',
  21247. ' return this.p;',
  21248. ' },',
  21249. ' set: function (v) {',
  21250. ' this.p = v;',
  21251. ' }',
  21252. '});',
  21253. '$mod.THelper.Run();',
  21254. 'var $with1 = $mod.o;',
  21255. '$mod.THelper.Fly.call({',
  21256. ' p: $with1.GetField(),',
  21257. ' get: function () {',
  21258. ' return this.p;',
  21259. ' },',
  21260. ' set: function (v) {',
  21261. ' this.p = v;',
  21262. ' }',
  21263. '});',
  21264. '$mod.THelper.Run();',
  21265. 'var $with2 = $mod.o.GetField();',
  21266. '$mod.THelper.Fly.call({',
  21267. ' get: function () {',
  21268. ' return $with2;',
  21269. ' },',
  21270. ' set: function (v) {',
  21271. ' $with2 = v;',
  21272. ' }',
  21273. '});',
  21274. '$mod.THelper.Run();',
  21275. '']));
  21276. end;
  21277. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  21278. begin
  21279. StartProgram(false);
  21280. Add([
  21281. '{$modeswitch typehelpers}',
  21282. 'type',
  21283. ' TObject = class',
  21284. ' class var FField: word;',
  21285. ' class procedure SetField(Value: word);',
  21286. ' class property Field: word read FField write SetField;',
  21287. ' end;',
  21288. ' THelper = type helper for word',
  21289. ' procedure Fly(n: byte);',
  21290. ' end;',
  21291. 'class procedure TObject.SetField(Value: word);',
  21292. 'begin',
  21293. ' Field.Fly(1);',
  21294. ' Self.Field.Fly(2);',
  21295. ' with Self do Field.Fly(3);',
  21296. ' with Self.Field do Fly(4);',
  21297. ' TObject.Field.Fly(5);',
  21298. ' with TObject do Field.Fly(6);',
  21299. ' with TObject.Field do Fly(7);',
  21300. 'end;',
  21301. 'procedure THelper.Fly(n: byte);',
  21302. 'begin',
  21303. 'end;',
  21304. 'var',
  21305. ' o: TObject;',
  21306. 'begin',
  21307. ' o.Field.Fly(11);',
  21308. ' with o do Field.Fly(12);',
  21309. ' with o.Field do Fly(13);',
  21310. ' TObject.Field.Fly(14);',
  21311. ' with TObject do Field.Fly(15);',
  21312. ' with TObject.Field do Fly(16);',
  21313. '']);
  21314. ConvertProgram;
  21315. CheckSource('TestTypeHelper_PassClassPropertyField',
  21316. LinesToStr([ // statements
  21317. 'rtl.createClass($mod, "TObject", null, function () {',
  21318. ' this.FField = 0;',
  21319. ' this.$init = function () {',
  21320. ' };',
  21321. ' this.$final = function () {',
  21322. ' };',
  21323. ' this.SetField = function (Value) {',
  21324. ' $mod.THelper.Fly.call({',
  21325. ' p: this,',
  21326. ' get: function () {',
  21327. ' return this.p.FField;',
  21328. ' },',
  21329. ' set: function (v) {',
  21330. ' $mod.TObject.FField = v;',
  21331. ' }',
  21332. ' }, 1);',
  21333. ' $mod.THelper.Fly.call({',
  21334. ' p: this,',
  21335. ' get: function () {',
  21336. ' return this.p.FField;',
  21337. ' },',
  21338. ' set: function (v) {',
  21339. ' $mod.TObject.FField = v;',
  21340. ' }',
  21341. ' }, 2);',
  21342. ' $mod.THelper.Fly.call({',
  21343. ' p: this,',
  21344. ' get: function () {',
  21345. ' return this.p.FField;',
  21346. ' },',
  21347. ' set: function (v) {',
  21348. ' $mod.TObject.FField = v;',
  21349. ' }',
  21350. ' }, 3);',
  21351. ' var $with1 = this.FField;',
  21352. ' $mod.THelper.Fly.call({',
  21353. ' get: function () {',
  21354. ' return $with1;',
  21355. ' },',
  21356. ' set: function (v) {',
  21357. ' $with1 = v;',
  21358. ' }',
  21359. ' }, 4);',
  21360. ' $mod.THelper.Fly.call({',
  21361. ' p: $mod.TObject,',
  21362. ' get: function () {',
  21363. ' return this.p.FField;',
  21364. ' },',
  21365. ' set: function (v) {',
  21366. ' $mod.TObject.FField = v;',
  21367. ' }',
  21368. ' }, 5);',
  21369. ' var $with2 = $mod.TObject;',
  21370. ' $mod.THelper.Fly.call({',
  21371. ' p: $with2,',
  21372. ' get: function () {',
  21373. ' return this.p.FField;',
  21374. ' },',
  21375. ' set: function (v) {',
  21376. ' $mod.TObject.FField = v;',
  21377. ' }',
  21378. ' }, 6);',
  21379. ' var $with3 = $mod.TObject.FField;',
  21380. ' $mod.THelper.Fly.call({',
  21381. ' get: function () {',
  21382. ' return $with3;',
  21383. ' },',
  21384. ' set: function (v) {',
  21385. ' $with3 = v;',
  21386. ' }',
  21387. ' }, 7);',
  21388. ' };',
  21389. '});',
  21390. 'rtl.createHelper($mod, "THelper", null, function () {',
  21391. ' this.Fly = function (n) {',
  21392. ' };',
  21393. '});',
  21394. 'this.o = null;',
  21395. '']),
  21396. LinesToStr([ // $mod.$main
  21397. '$mod.THelper.Fly.call({',
  21398. ' p: $mod.o,',
  21399. ' get: function () {',
  21400. ' return this.p.FField;',
  21401. ' },',
  21402. ' set: function (v) {',
  21403. ' $mod.TObject.FField = v;',
  21404. ' }',
  21405. '}, 11);',
  21406. 'var $with1 = $mod.o;',
  21407. '$mod.THelper.Fly.call({',
  21408. ' p: $with1,',
  21409. ' get: function () {',
  21410. ' return this.p.FField;',
  21411. ' },',
  21412. ' set: function (v) {',
  21413. ' $mod.TObject.FField = v;',
  21414. ' }',
  21415. '}, 12);',
  21416. 'var $with2 = $mod.o.FField;',
  21417. '$mod.THelper.Fly.call({',
  21418. ' get: function () {',
  21419. ' return $with2;',
  21420. ' },',
  21421. ' set: function (v) {',
  21422. ' $with2 = v;',
  21423. ' }',
  21424. '}, 13);',
  21425. '$mod.THelper.Fly.call({',
  21426. ' p: $mod.TObject,',
  21427. ' get: function () {',
  21428. ' return this.p.FField;',
  21429. ' },',
  21430. ' set: function (v) {',
  21431. ' $mod.TObject.FField = v;',
  21432. ' }',
  21433. '}, 14);',
  21434. 'var $with3 = $mod.TObject;',
  21435. '$mod.THelper.Fly.call({',
  21436. ' p: $with3,',
  21437. ' get: function () {',
  21438. ' return this.p.FField;',
  21439. ' },',
  21440. ' set: function (v) {',
  21441. ' $mod.TObject.FField = v;',
  21442. ' }',
  21443. '}, 15);',
  21444. 'var $with4 = $mod.TObject.FField;',
  21445. '$mod.THelper.Fly.call({',
  21446. ' get: function () {',
  21447. ' return $with4;',
  21448. ' },',
  21449. ' set: function (v) {',
  21450. ' $with4 = v;',
  21451. ' }',
  21452. '}, 16);',
  21453. '']));
  21454. end;
  21455. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  21456. begin
  21457. StartProgram(false);
  21458. Add([
  21459. '{$modeswitch typehelpers}',
  21460. 'type',
  21461. ' TObject = class',
  21462. ' class var FField: word;',
  21463. ' class function GetField: word; static;',
  21464. ' class property Field: word read GetField write FField;',
  21465. ' end;',
  21466. ' THelper = type helper for word',
  21467. ' procedure Fly(n: byte);',
  21468. ' end;',
  21469. 'class function TObject.GetField: word;',
  21470. 'begin',
  21471. ' Field.Fly(1);',
  21472. ' TObject.Field.Fly(5);',
  21473. ' with TObject do Field.Fly(6);',
  21474. ' with TObject.Field do Fly(7);',
  21475. 'end;',
  21476. 'procedure THelper.Fly(n: byte);',
  21477. 'begin',
  21478. 'end;',
  21479. 'var',
  21480. ' o: TObject;',
  21481. 'begin',
  21482. ' o.Field.Fly(11);',
  21483. ' with o do Field.Fly(12);',
  21484. ' with o.Field do Fly(13);',
  21485. '']);
  21486. ConvertProgram;
  21487. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  21488. LinesToStr([ // statements
  21489. 'rtl.createClass($mod, "TObject", null, function () {',
  21490. ' this.FField = 0;',
  21491. ' this.$init = function () {',
  21492. ' };',
  21493. ' this.$final = function () {',
  21494. ' };',
  21495. ' this.GetField = function () {',
  21496. ' var Result = 0;',
  21497. ' $mod.THelper.Fly.call({',
  21498. ' p: this.GetField(),',
  21499. ' get: function () {',
  21500. ' return this.p;',
  21501. ' },',
  21502. ' set: function (v) {',
  21503. ' this.p = v;',
  21504. ' }',
  21505. ' }, 1);',
  21506. ' $mod.THelper.Fly.call({',
  21507. ' p: $mod.TObject.GetField(),',
  21508. ' get: function () {',
  21509. ' return this.p;',
  21510. ' },',
  21511. ' set: function (v) {',
  21512. ' this.p = v;',
  21513. ' }',
  21514. ' }, 5);',
  21515. ' var $with1 = $mod.TObject;',
  21516. ' $mod.THelper.Fly.call({',
  21517. ' p: $with1.GetField(),',
  21518. ' get: function () {',
  21519. ' return this.p;',
  21520. ' },',
  21521. ' set: function (v) {',
  21522. ' this.p = v;',
  21523. ' }',
  21524. ' }, 6);',
  21525. ' var $with2 = $mod.TObject.GetField();',
  21526. ' $mod.THelper.Fly.call({',
  21527. ' get: function () {',
  21528. ' return $with2;',
  21529. ' },',
  21530. ' set: function (v) {',
  21531. ' $with2 = v;',
  21532. ' }',
  21533. ' }, 7);',
  21534. ' return Result;',
  21535. ' };',
  21536. '});',
  21537. 'rtl.createHelper($mod, "THelper", null, function () {',
  21538. ' this.Fly = function (n) {',
  21539. ' };',
  21540. '});',
  21541. 'this.o = null;',
  21542. '']),
  21543. LinesToStr([ // $mod.$main
  21544. '$mod.THelper.Fly.call({',
  21545. ' p: $mod.o.GetField(),',
  21546. ' get: function () {',
  21547. ' return this.p;',
  21548. ' },',
  21549. ' set: function (v) {',
  21550. ' this.p = v;',
  21551. ' }',
  21552. '}, 11);',
  21553. 'var $with1 = $mod.o;',
  21554. '$mod.THelper.Fly.call({',
  21555. ' p: $with1.GetField(),',
  21556. ' get: function () {',
  21557. ' return this.p;',
  21558. ' },',
  21559. ' set: function (v) {',
  21560. ' this.p = v;',
  21561. ' }',
  21562. '}, 12);',
  21563. 'var $with2 = $mod.o.GetField();',
  21564. '$mod.THelper.Fly.call({',
  21565. ' get: function () {',
  21566. ' return $with2;',
  21567. ' },',
  21568. ' set: function (v) {',
  21569. ' $with2 = v;',
  21570. ' }',
  21571. '}, 13);',
  21572. '']));
  21573. end;
  21574. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  21575. begin
  21576. StartProgram(false);
  21577. Add([
  21578. '{$modeswitch typehelpers}',
  21579. 'type',
  21580. ' TObject = class',
  21581. ' class var FField: word;',
  21582. ' class function GetField: word;',
  21583. ' class property Field: word read GetField write FField;',
  21584. ' end;',
  21585. ' TClass = class of TObject;',
  21586. ' THelper = type helper for word',
  21587. ' procedure Fly(n: byte);',
  21588. ' end;',
  21589. 'class function TObject.GetField: word;',
  21590. 'begin',
  21591. ' Field.Fly(1);',
  21592. ' Self.Field.Fly(5);',
  21593. ' with Self do Field.Fly(6);',
  21594. ' with Self.Field do Fly(7);',
  21595. 'end;',
  21596. 'procedure THelper.Fly(n: byte);',
  21597. 'begin',
  21598. 'end;',
  21599. 'var',
  21600. ' o: TObject;',
  21601. ' c: TClass;',
  21602. 'begin',
  21603. ' o.Field.Fly(11);',
  21604. ' with o do Field.Fly(12);',
  21605. ' with o.Field do Fly(13);',
  21606. ' c.Field.Fly(14);',
  21607. ' with c do Field.Fly(15);',
  21608. ' with c.Field do Fly(16);',
  21609. '']);
  21610. ConvertProgram;
  21611. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  21612. LinesToStr([ // statements
  21613. 'rtl.createClass($mod, "TObject", null, function () {',
  21614. ' this.FField = 0;',
  21615. ' this.$init = function () {',
  21616. ' };',
  21617. ' this.$final = function () {',
  21618. ' };',
  21619. ' this.GetField = function () {',
  21620. ' var Result = 0;',
  21621. ' $mod.THelper.Fly.call({',
  21622. ' p: this.GetField(),',
  21623. ' get: function () {',
  21624. ' return this.p;',
  21625. ' },',
  21626. ' set: function (v) {',
  21627. ' this.p = v;',
  21628. ' }',
  21629. ' }, 1);',
  21630. ' $mod.THelper.Fly.call({',
  21631. ' p: this.GetField(),',
  21632. ' get: function () {',
  21633. ' return this.p;',
  21634. ' },',
  21635. ' set: function (v) {',
  21636. ' this.p = v;',
  21637. ' }',
  21638. ' }, 5);',
  21639. ' $mod.THelper.Fly.call({',
  21640. ' p: this.GetField(),',
  21641. ' get: function () {',
  21642. ' return this.p;',
  21643. ' },',
  21644. ' set: function (v) {',
  21645. ' this.p = v;',
  21646. ' }',
  21647. ' }, 6);',
  21648. ' var $with1 = this.GetField();',
  21649. ' $mod.THelper.Fly.call({',
  21650. ' get: function () {',
  21651. ' return $with1;',
  21652. ' },',
  21653. ' set: function (v) {',
  21654. ' $with1 = v;',
  21655. ' }',
  21656. ' }, 7);',
  21657. ' return Result;',
  21658. ' };',
  21659. '});',
  21660. 'rtl.createHelper($mod, "THelper", null, function () {',
  21661. ' this.Fly = function (n) {',
  21662. ' };',
  21663. '});',
  21664. 'this.o = null;',
  21665. 'this.c = null;',
  21666. '']),
  21667. LinesToStr([ // $mod.$main
  21668. '$mod.THelper.Fly.call({',
  21669. ' p: $mod.o.$class.GetField(),',
  21670. ' get: function () {',
  21671. ' return this.p;',
  21672. ' },',
  21673. ' set: function (v) {',
  21674. ' this.p = v;',
  21675. ' }',
  21676. '}, 11);',
  21677. 'var $with1 = $mod.o;',
  21678. '$mod.THelper.Fly.call({',
  21679. ' p: $with1.$class.GetField(),',
  21680. ' get: function () {',
  21681. ' return this.p;',
  21682. ' },',
  21683. ' set: function (v) {',
  21684. ' this.p = v;',
  21685. ' }',
  21686. '}, 12);',
  21687. 'var $with2 = $mod.o.$class.GetField();',
  21688. '$mod.THelper.Fly.call({',
  21689. ' get: function () {',
  21690. ' return $with2;',
  21691. ' },',
  21692. ' set: function (v) {',
  21693. ' $with2 = v;',
  21694. ' }',
  21695. '}, 13);',
  21696. '$mod.THelper.Fly.call({',
  21697. ' p: $mod.c.GetField(),',
  21698. ' get: function () {',
  21699. ' return this.p;',
  21700. ' },',
  21701. ' set: function (v) {',
  21702. ' this.p = v;',
  21703. ' }',
  21704. '}, 14);',
  21705. 'var $with3 = $mod.c;',
  21706. '$mod.THelper.Fly.call({',
  21707. ' p: $with3.GetField(),',
  21708. ' get: function () {',
  21709. ' return this.p;',
  21710. ' },',
  21711. ' set: function (v) {',
  21712. ' this.p = v;',
  21713. ' }',
  21714. '}, 15);',
  21715. 'var $with4 = $mod.c.GetField();',
  21716. '$mod.THelper.Fly.call({',
  21717. ' get: function () {',
  21718. ' return $with4;',
  21719. ' },',
  21720. ' set: function (v) {',
  21721. ' $with4 = v;',
  21722. ' }',
  21723. '}, 16);',
  21724. '']));
  21725. end;
  21726. procedure TTestModule.TestTypeHelper_Property;
  21727. begin
  21728. StartProgram(false);
  21729. Add([
  21730. '{$modeswitch typehelpers}',
  21731. 'type',
  21732. ' THelper = type helper for word',
  21733. ' function GetSize: longint;',
  21734. ' procedure SetSize(Value: longint);',
  21735. ' property Size: longint read GetSize write SetSize;',
  21736. ' end;',
  21737. 'function THelper.GetSize: longint;',
  21738. 'begin',
  21739. ' Result:=Size+1;',
  21740. ' Size:=2;',
  21741. ' Result:=Self.Size+3;',
  21742. ' Self.Size:=4;',
  21743. ' with Self do begin',
  21744. ' Result:=Size+5;',
  21745. ' Size:=6;',
  21746. ' end;',
  21747. 'end;',
  21748. 'procedure THelper.SetSize(Value: longint);',
  21749. 'begin',
  21750. 'end;',
  21751. 'var w: word;',
  21752. 'begin',
  21753. ' w:=w.Size+7;',
  21754. ' w.Size:=w+8;',
  21755. ' with w do begin',
  21756. ' w:=Size+9;',
  21757. ' Size:=w+10;',
  21758. ' end;',
  21759. '']);
  21760. ConvertProgram;
  21761. CheckSource('TestTypeHelper_Property',
  21762. LinesToStr([ // statements
  21763. 'rtl.createHelper($mod, "THelper", null, function () {',
  21764. ' this.GetSize = function () {',
  21765. ' var Result = 0;',
  21766. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  21767. ' $mod.THelper.SetSize.call(this, 2);',
  21768. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  21769. ' $mod.THelper.SetSize.call(this, 4);',
  21770. ' var $with1 = this.get();',
  21771. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  21772. ' $mod.THelper.SetSize.call(this, 6);',
  21773. ' return Result;',
  21774. ' };',
  21775. ' this.SetSize = function (Value) {',
  21776. ' };',
  21777. '});',
  21778. 'this.w = 0;',
  21779. '']),
  21780. LinesToStr([ // $mod.$main
  21781. '$mod.w = $mod.THelper.GetSize.call({',
  21782. ' p: $mod,',
  21783. ' get: function () {',
  21784. ' return this.p.w;',
  21785. ' },',
  21786. ' set: function (v) {',
  21787. ' this.p.w = v;',
  21788. ' }',
  21789. '}) + 7;',
  21790. '$mod.THelper.SetSize.call({',
  21791. ' p: $mod,',
  21792. ' get: function () {',
  21793. ' return this.p.w;',
  21794. ' },',
  21795. ' set: function (v) {',
  21796. ' this.p.w = v;',
  21797. ' }',
  21798. '}, $mod.w + 8);',
  21799. 'var $with1 = $mod.w;',
  21800. '$mod.w = $mod.THelper.GetSize.call({',
  21801. ' get: function () {',
  21802. ' return $with1;',
  21803. ' },',
  21804. ' set: function (v) {',
  21805. ' $with1 = v;',
  21806. ' }',
  21807. '}) + 9;',
  21808. '$mod.THelper.SetSize.call({',
  21809. ' get: function () {',
  21810. ' return $with1;',
  21811. ' },',
  21812. ' set: function (v) {',
  21813. ' $with1 = v;',
  21814. ' }',
  21815. '}, $mod.w + 10);',
  21816. '']));
  21817. end;
  21818. procedure TTestModule.TestTypeHelper_Property_Array;
  21819. begin
  21820. StartProgram(false);
  21821. Add([
  21822. '{$modeswitch typehelpers}',
  21823. 'type',
  21824. ' THelper = type helper for word',
  21825. ' function GetItems(Index: byte): boolean;',
  21826. ' procedure SetItems(Index: byte; Value: boolean);',
  21827. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  21828. ' end;',
  21829. 'function THelper.GetItems(Index: byte): boolean;',
  21830. 'begin',
  21831. ' Result:=Items[1];',
  21832. ' Items[2]:=false;',
  21833. ' Result:=Self.Items[3];',
  21834. ' Self.Items[4]:=true;',
  21835. ' with Self do begin',
  21836. ' Result:=Items[5];',
  21837. ' Items[6]:=false;',
  21838. ' end;',
  21839. 'end;',
  21840. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  21841. 'begin',
  21842. 'end;',
  21843. 'var',
  21844. ' w: word;',
  21845. ' b: boolean;',
  21846. 'begin',
  21847. ' b:=w.Items[1];',
  21848. ' w.Items[2]:=b;',
  21849. ' with w do begin',
  21850. ' b:=Items[3];',
  21851. ' Items[4]:=b;',
  21852. ' end;',
  21853. '']);
  21854. ConvertProgram;
  21855. CheckSource('TestTypeHelper_Property_Array',
  21856. LinesToStr([ // statements
  21857. 'rtl.createHelper($mod, "THelper", null, function () {',
  21858. ' this.GetItems = function (Index) {',
  21859. ' var Result = false;',
  21860. ' Result = $mod.THelper.GetItems.call(this, 1);',
  21861. ' $mod.THelper.SetItems.call(this, 2, false);',
  21862. ' Result = $mod.THelper.GetItems.call(this, 3);',
  21863. ' $mod.THelper.SetItems.call(this, 4, true);',
  21864. ' var $with1 = this.get();',
  21865. ' Result = $mod.THelper.GetItems.call(this, 5);',
  21866. ' $mod.THelper.SetItems.call(this, 6, false);',
  21867. ' return Result;',
  21868. ' };',
  21869. ' this.SetItems = function (Index, Value) {',
  21870. ' };',
  21871. '});',
  21872. 'this.w = 0;',
  21873. 'this.b = false;',
  21874. '']),
  21875. LinesToStr([ // $mod.$main
  21876. '$mod.b = $mod.THelper.GetItems.call({',
  21877. ' p: $mod,',
  21878. ' get: function () {',
  21879. ' return this.p.w;',
  21880. ' },',
  21881. ' set: function (v) {',
  21882. ' this.p.w = v;',
  21883. ' }',
  21884. '}, 1);',
  21885. '$mod.THelper.SetItems.call({',
  21886. ' p: $mod,',
  21887. ' get: function () {',
  21888. ' return this.p.w;',
  21889. ' },',
  21890. ' set: function (v) {',
  21891. ' this.p.w = v;',
  21892. ' }',
  21893. '}, 2, $mod.b);',
  21894. 'var $with1 = $mod.w;',
  21895. '$mod.b = $mod.THelper.GetItems.call({',
  21896. ' get: function () {',
  21897. ' return $with1;',
  21898. ' },',
  21899. ' set: function (v) {',
  21900. ' $with1 = v;',
  21901. ' }',
  21902. '}, 3);',
  21903. '$mod.THelper.SetItems.call({',
  21904. ' get: function () {',
  21905. ' return $with1;',
  21906. ' },',
  21907. ' set: function (v) {',
  21908. ' $with1 = v;',
  21909. ' }',
  21910. '}, 4, $mod.b);',
  21911. '']));
  21912. end;
  21913. procedure TTestModule.TestTypeHelper_ClassProperty;
  21914. begin
  21915. StartProgram(false);
  21916. Add([
  21917. '{$modeswitch typehelpers}',
  21918. 'type',
  21919. ' THelper = type helper for word',
  21920. ' class function GetSize: longint; static;',
  21921. ' class procedure SetSize(Value: longint); static;',
  21922. ' class property Size: longint read GetSize write SetSize;',
  21923. ' end;',
  21924. 'class function THelper.GetSize: longint;',
  21925. 'begin',
  21926. ' Result:=Size+1;',
  21927. ' Size:=2;',
  21928. 'end;',
  21929. 'class procedure THelper.SetSize(Value: longint);',
  21930. 'begin',
  21931. 'end;',
  21932. 'begin',
  21933. '']);
  21934. ConvertProgram;
  21935. CheckSource('TestTypeHelper_ClassProperty',
  21936. LinesToStr([ // statements
  21937. 'rtl.createHelper($mod, "THelper", null, function () {',
  21938. ' this.GetSize = function () {',
  21939. ' var Result = 0;',
  21940. ' Result = $mod.THelper.GetSize() + 1;',
  21941. ' $mod.THelper.SetSize(2);',
  21942. ' return Result;',
  21943. ' };',
  21944. ' this.SetSize = function (Value) {',
  21945. ' };',
  21946. '});',
  21947. '']),
  21948. LinesToStr([ // $mod.$main
  21949. '']));
  21950. end;
  21951. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  21952. begin
  21953. StartProgram(false);
  21954. Add([
  21955. '{$modeswitch typehelpers}',
  21956. 'type',
  21957. ' THelper = type helper for word',
  21958. ' class function GetItems(Index: byte): boolean; static;',
  21959. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  21960. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  21961. ' end;',
  21962. 'class function THelper.GetItems(Index: byte): boolean;',
  21963. 'begin',
  21964. ' Result:=Items[1];',
  21965. ' Items[2]:=false;',
  21966. 'end;',
  21967. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  21968. 'begin',
  21969. 'end;',
  21970. 'var',
  21971. ' w: word;',
  21972. ' b: boolean;',
  21973. 'begin',
  21974. ' b:=w.Items[1];',
  21975. ' w.Items[2]:=b;',
  21976. ' with w do begin',
  21977. ' b:=Items[3];',
  21978. ' Items[4]:=b;',
  21979. ' end;',
  21980. '']);
  21981. ConvertProgram;
  21982. CheckSource('TestTypeHelper_ClassProperty_Array',
  21983. LinesToStr([ // statements
  21984. 'rtl.createHelper($mod, "THelper", null, function () {',
  21985. ' this.GetItems = function (Index) {',
  21986. ' var Result = false;',
  21987. ' Result = $mod.THelper.GetItems(1);',
  21988. ' $mod.THelper.SetItems(2, false);',
  21989. ' return Result;',
  21990. ' };',
  21991. ' this.SetItems = function (Index, Value) {',
  21992. ' };',
  21993. '});',
  21994. 'this.w = 0;',
  21995. 'this.b = false;',
  21996. '']),
  21997. LinesToStr([ // $mod.$main
  21998. '$mod.b = $mod.THelper.GetItems(1);',
  21999. '$mod.THelper.SetItems(2, $mod.b);',
  22000. 'var $with1 = $mod.w;',
  22001. '$mod.b = $mod.THelper.GetItems(3);',
  22002. '$mod.THelper.SetItems(4, $mod.b);',
  22003. '']));
  22004. end;
  22005. procedure TTestModule.TestTypeHelper_ClassMethod;
  22006. begin
  22007. StartProgram(false);
  22008. Add([
  22009. '{$modeswitch typehelpers}',
  22010. 'type',
  22011. ' THelper = type helper for word',
  22012. ' class procedure DoStatic; static;',
  22013. ' end;',
  22014. 'class procedure THelper.DoStatic;',
  22015. 'begin',
  22016. ' DoStatic;',
  22017. ' DoStatic();',
  22018. 'end;',
  22019. 'var w: word;',
  22020. 'begin',
  22021. ' w.DoStatic;',
  22022. ' w.DoStatic();',
  22023. '']);
  22024. ConvertProgram;
  22025. CheckSource('TestTypeHelper_ClassMethod',
  22026. LinesToStr([ // statements
  22027. 'rtl.createHelper($mod, "THelper", null, function () {',
  22028. ' this.DoStatic = function () {',
  22029. ' $mod.THelper.DoStatic();',
  22030. ' $mod.THelper.DoStatic();',
  22031. ' };',
  22032. '});',
  22033. 'this.w = 0;',
  22034. '']),
  22035. LinesToStr([ // $mod.$main
  22036. '$mod.THelper.DoStatic();',
  22037. '$mod.THelper.DoStatic();',
  22038. '']));
  22039. end;
  22040. procedure TTestModule.TestTypeHelper_Constructor;
  22041. begin
  22042. StartProgram(false);
  22043. Add([
  22044. '{$modeswitch typehelpers}',
  22045. 'type',
  22046. ' THelper = type helper for word',
  22047. ' constructor Init(e: longint);',
  22048. ' end;',
  22049. 'constructor THelper.Init(e: longint);',
  22050. 'begin',
  22051. ' Self:=e;',
  22052. ' Init(e+1);',
  22053. 'end;',
  22054. 'var w: word;',
  22055. 'begin',
  22056. ' w:=word.Init(2);',
  22057. ' w:=w.Init(3);',
  22058. ' with word do w:=Init(4);',
  22059. ' with w do w:=Init(5);',
  22060. '']);
  22061. ConvertProgram;
  22062. CheckSource('TestTypeHelper_Constructor',
  22063. LinesToStr([ // statements
  22064. 'rtl.createHelper($mod, "THelper", null, function () {',
  22065. ' this.Init = function (e) {',
  22066. ' this.set(e);',
  22067. ' $mod.THelper.Init.call(this, e + 1);',
  22068. ' return this.get();',
  22069. ' };',
  22070. ' this.$new = function (fn, args) {',
  22071. ' return this[fn].apply({',
  22072. ' p: 0,',
  22073. ' get: function () {',
  22074. ' return this.p;',
  22075. ' },',
  22076. ' set: function (v) {',
  22077. ' this.p = v;',
  22078. ' }',
  22079. ' }, args);',
  22080. ' };',
  22081. '});',
  22082. 'this.w = 0;',
  22083. '']),
  22084. LinesToStr([ // $mod.$main
  22085. '$mod.w = $mod.THelper.$new("Init", [2]);',
  22086. '$mod.w = $mod.THelper.Init.call({',
  22087. ' p: $mod,',
  22088. ' get: function () {',
  22089. ' return this.p.w;',
  22090. ' },',
  22091. ' set: function (v) {',
  22092. ' this.p.w = v;',
  22093. ' }',
  22094. '}, 3);',
  22095. '$mod.w = $mod.THelper.$new("Init", [4]);',
  22096. 'var $with1 = $mod.w;',
  22097. '$mod.w = $mod.THelper.Init.call({',
  22098. ' get: function () {',
  22099. ' return $with1;',
  22100. ' },',
  22101. ' set: function (v) {',
  22102. ' $with1 = v;',
  22103. ' }',
  22104. '}, 5);',
  22105. '']));
  22106. end;
  22107. procedure TTestModule.TestTypeHelper_Word;
  22108. begin
  22109. StartProgram(false);
  22110. Add([
  22111. '{$modeswitch typehelpers}',
  22112. 'type',
  22113. ' THelper = type helper for word',
  22114. ' procedure DoIt(e: byte = 123);',
  22115. ' end;',
  22116. 'procedure THelper.DoIt(e: byte);',
  22117. 'begin',
  22118. ' Self:=e;',
  22119. ' Self:=Self+1;',
  22120. ' with Self do Doit;',
  22121. 'end;',
  22122. 'begin',
  22123. ' word(3).DoIt;',
  22124. '']);
  22125. ConvertProgram;
  22126. CheckSource('TestTypeHelper_Word',
  22127. LinesToStr([ // statements
  22128. 'rtl.createHelper($mod, "THelper", null, function () {',
  22129. ' this.DoIt = function (e) {',
  22130. ' this.set(e);',
  22131. ' this.set(this.get() + 1);',
  22132. ' var $with1 = this.get();',
  22133. ' $mod.THelper.DoIt.call(this, 123);',
  22134. ' };',
  22135. '});',
  22136. '']),
  22137. LinesToStr([ // $mod.$main
  22138. '$mod.THelper.DoIt.call({',
  22139. ' get: function () {',
  22140. ' return 3;',
  22141. ' },',
  22142. ' set: function (v) {',
  22143. ' rtl.raiseE("EPropReadOnly");',
  22144. ' }',
  22145. '}, 123);',
  22146. '']));
  22147. end;
  22148. procedure TTestModule.TestTypeHelper_StringChar;
  22149. begin
  22150. StartProgram(false);
  22151. Add([
  22152. '{$modeswitch typehelpers}',
  22153. 'type',
  22154. ' TStringHelper = type helper for string',
  22155. ' procedure DoIt(e: byte = 123);',
  22156. ' end;',
  22157. ' TCharHelper = type helper for char',
  22158. ' procedure Fly;',
  22159. ' end;',
  22160. 'procedure TStringHelper.DoIt(e: byte);',
  22161. 'begin',
  22162. ' Self[1]:=''c'';',
  22163. ' Self[2]:=Self[3];',
  22164. 'end;',
  22165. 'procedure TCharHelper.Fly;',
  22166. 'begin',
  22167. ' Self:=''c'';',
  22168. 'end;',
  22169. 'begin',
  22170. ' ''abc''.DoIt;',
  22171. ' ''xyz''.DoIt();',
  22172. ' ''c''.Fly();',
  22173. '']);
  22174. ConvertProgram;
  22175. CheckSource('TestTypeHelper_StringChar',
  22176. LinesToStr([ // statements
  22177. 'rtl.createHelper($mod, "TStringHelper", null, function () {',
  22178. ' this.DoIt = function (e) {',
  22179. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  22180. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  22181. ' };',
  22182. '});',
  22183. 'rtl.createHelper($mod, "TCharHelper", null, function () {',
  22184. ' this.Fly = function () {',
  22185. ' this.set("c");',
  22186. ' };',
  22187. '});',
  22188. '']),
  22189. LinesToStr([ // $mod.$main
  22190. '$mod.TStringHelper.DoIt.call({',
  22191. ' get: function () {',
  22192. ' return "abc";',
  22193. ' },',
  22194. ' set: function (v) {',
  22195. ' rtl.raiseE("EPropReadOnly");',
  22196. ' }',
  22197. '}, 123);',
  22198. '$mod.TStringHelper.DoIt.call({',
  22199. ' get: function () {',
  22200. ' return "xyz";',
  22201. ' },',
  22202. ' set: function (v) {',
  22203. ' rtl.raiseE("EPropReadOnly");',
  22204. ' }',
  22205. '}, 123);',
  22206. '$mod.TCharHelper.Fly.call({',
  22207. ' get: function () {',
  22208. ' return "c";',
  22209. ' },',
  22210. ' set: function (v) {',
  22211. ' rtl.raiseE("EPropReadOnly");',
  22212. ' }',
  22213. '});',
  22214. '']));
  22215. end;
  22216. procedure TTestModule.TestTypeHelper_Array;
  22217. begin
  22218. StartProgram(false);
  22219. Add([
  22220. '{$modeswitch typehelpers}',
  22221. 'type',
  22222. ' TArrOfBool = array of boolean;',
  22223. ' TArrOfJS = array of jsvalue;',
  22224. ' THelper = type helper for TArrOfBool',
  22225. ' procedure DoIt(e: byte = 123);',
  22226. ' end;',
  22227. 'procedure THelper.DoIt(e: byte);',
  22228. 'begin',
  22229. ' Self[1]:=true;',
  22230. ' Self[2]:=not Self[3];',
  22231. ' SetLength(Self,4);',
  22232. 'end;',
  22233. 'var',
  22234. ' b: TArrOfBool;',
  22235. ' j: TArrOfJS;',
  22236. 'begin',
  22237. ' b.DoIt;',
  22238. ' TArrOfBool(j).DoIt();',
  22239. '']);
  22240. ConvertProgram;
  22241. CheckSource('TestTypeHelper_Array',
  22242. LinesToStr([ // statements
  22243. 'rtl.createHelper($mod, "THelper", null, function () {',
  22244. ' this.DoIt = function (e) {',
  22245. ' this.get()[1] = true;',
  22246. ' this.get()[2] = !this.get()[3];',
  22247. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  22248. ' };',
  22249. '});',
  22250. 'this.b = [];',
  22251. 'this.j = [];',
  22252. '']),
  22253. LinesToStr([ // $mod.$main
  22254. '$mod.THelper.DoIt.call({',
  22255. ' p: $mod,',
  22256. ' get: function () {',
  22257. ' return this.p.b;',
  22258. ' },',
  22259. ' set: function (v) {',
  22260. ' this.p.b = v;',
  22261. ' }',
  22262. '}, 123);',
  22263. '$mod.THelper.DoIt.call({',
  22264. ' p: $mod,',
  22265. ' get: function () {',
  22266. ' return this.p.j;',
  22267. ' },',
  22268. ' set: function (v) {',
  22269. ' this.p.j = v;',
  22270. ' }',
  22271. '}, 123);',
  22272. '']));
  22273. end;
  22274. procedure TTestModule.TestTypeHelper_EnumType;
  22275. begin
  22276. StartProgram(false);
  22277. Add([
  22278. '{$modeswitch typehelpers}',
  22279. 'type',
  22280. ' TEnum = (red,blue);',
  22281. ' THelper = type helper for TEnum',
  22282. ' procedure DoIt(e: byte = 123);',
  22283. ' class procedure Swing(w: word); static;',
  22284. ' end;',
  22285. 'procedure THelper.DoIt(e: byte);',
  22286. 'begin',
  22287. ' Self:=red;',
  22288. ' Self:=succ(Self);',
  22289. ' with Self do Doit;',
  22290. 'end;',
  22291. 'class procedure THelper.Swing(w: word);',
  22292. 'begin',
  22293. 'end;',
  22294. 'var e: TEnum;',
  22295. 'begin',
  22296. ' e.DoIt;',
  22297. ' red.DoIt;',
  22298. ' TEnum.blue.DoIt;',
  22299. ' TEnum(1).DoIt;',
  22300. ' TEnum.Swing(3);',
  22301. '']);
  22302. ConvertProgram;
  22303. CheckSource('TestTypeHelper_EnumType',
  22304. LinesToStr([ // statements
  22305. 'this.TEnum = {',
  22306. ' "0": "red",',
  22307. ' red: 0,',
  22308. ' "1": "blue",',
  22309. ' blue: 1',
  22310. '};',
  22311. 'rtl.createHelper($mod, "THelper", null, function () {',
  22312. ' this.DoIt = function (e) {',
  22313. ' this.set($mod.TEnum.red);',
  22314. ' this.set(this.get() + 1);',
  22315. ' var $with1 = this.get();',
  22316. ' $mod.THelper.DoIt.call(this, 123);',
  22317. ' };',
  22318. ' this.Swing = function (w) {',
  22319. ' };',
  22320. '});',
  22321. 'this.e = 0;',
  22322. '']),
  22323. LinesToStr([ // $mod.$main
  22324. '$mod.THelper.DoIt.call({',
  22325. ' p: $mod,',
  22326. ' get: function () {',
  22327. ' return this.p.e;',
  22328. ' },',
  22329. ' set: function (v) {',
  22330. ' this.p.e = v;',
  22331. ' }',
  22332. '}, 123);',
  22333. '$mod.THelper.DoIt.call({',
  22334. ' p: $mod.TEnum,',
  22335. ' get: function () {',
  22336. ' return this.p.red;',
  22337. ' },',
  22338. ' set: function (v) {',
  22339. ' rtl.raiseE("EPropReadOnly");',
  22340. ' }',
  22341. '}, 123);',
  22342. '$mod.THelper.DoIt.call({',
  22343. ' p: $mod.TEnum,',
  22344. ' get: function () {',
  22345. ' return this.p.blue;',
  22346. ' },',
  22347. ' set: function (v) {',
  22348. ' rtl.raiseE("EPropReadOnly");',
  22349. ' }',
  22350. '}, 123);',
  22351. '$mod.THelper.DoIt.call({',
  22352. ' get: function () {',
  22353. ' return 1;',
  22354. ' },',
  22355. ' set: function (v) {',
  22356. ' rtl.raiseE("EPropReadOnly");',
  22357. ' }',
  22358. '}, 123);',
  22359. '$mod.THelper.Swing(3);',
  22360. '']));
  22361. end;
  22362. procedure TTestModule.TestTypeHelper_SetType;
  22363. begin
  22364. StartProgram(false);
  22365. Add([
  22366. '{$modeswitch typehelpers}',
  22367. 'type',
  22368. ' TEnum = (red,blue);',
  22369. ' TSetOfEnum = set of TEnum;',
  22370. ' THelper = type helper for TSetOfEnum',
  22371. ' procedure DoIt(e: byte = 123);',
  22372. ' constructor Init(e: TEnum);',
  22373. ' constructor InitEmpty;',
  22374. ' end;',
  22375. 'procedure THelper.DoIt(e: byte);',
  22376. 'begin',
  22377. ' Self:=[];',
  22378. ' Self:=[red];',
  22379. ' Include(Self,blue);',
  22380. 'end;',
  22381. 'constructor THelper.Init(e: TEnum);',
  22382. 'begin',
  22383. ' Self:=[];',
  22384. ' Self:=[e];',
  22385. ' Include(Self,blue);',
  22386. 'end;',
  22387. 'constructor THelper.InitEmpty;',
  22388. 'begin',
  22389. 'end;',
  22390. 'var s: TSetOfEnum;',
  22391. 'begin',
  22392. ' s.DoIt;',
  22393. //' [red].DoIt;',
  22394. //' with s do DoIt;',
  22395. //' with [red,blue] do DoIt;',
  22396. ' s:=TSetOfEnum.Init(blue);',
  22397. ' s:=s.Init(blue);',
  22398. '']);
  22399. ConvertProgram;
  22400. CheckSource('TestTypeHelper_SetType',
  22401. LinesToStr([ // statements
  22402. 'this.TEnum = {',
  22403. ' "0": "red",',
  22404. ' red: 0,',
  22405. ' "1": "blue",',
  22406. ' blue: 1',
  22407. '};',
  22408. 'rtl.createHelper($mod, "THelper", null, function () {',
  22409. ' this.DoIt = function (e) {',
  22410. ' this.set({});',
  22411. ' this.set(rtl.createSet($mod.TEnum.red));',
  22412. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  22413. ' };',
  22414. ' this.Init = function (e) {',
  22415. ' this.set({});',
  22416. ' this.set(rtl.createSet(e));',
  22417. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  22418. ' return this.get();',
  22419. ' };',
  22420. ' this.InitEmpty = function () {',
  22421. ' return this.get();',
  22422. ' };',
  22423. ' this.$new = function (fn, args) {',
  22424. ' return this[fn].apply({',
  22425. ' p: {},',
  22426. ' get: function () {',
  22427. ' return this.p;',
  22428. ' },',
  22429. ' set: function (v) {',
  22430. ' this.p = v;',
  22431. ' }',
  22432. ' }, args);',
  22433. ' };',
  22434. '});',
  22435. 'this.s = {};',
  22436. '']),
  22437. LinesToStr([ // $mod.$main
  22438. '$mod.THelper.DoIt.call({',
  22439. ' p: $mod,',
  22440. ' get: function () {',
  22441. ' return this.p.s;',
  22442. ' },',
  22443. ' set: function (v) {',
  22444. ' this.p.s = v;',
  22445. ' }',
  22446. '}, 123);',
  22447. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  22448. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  22449. ' p: $mod,',
  22450. ' get: function () {',
  22451. ' return this.p.s;',
  22452. ' },',
  22453. ' set: function (v) {',
  22454. ' this.p.s = v;',
  22455. ' }',
  22456. '}, $mod.TEnum.blue));',
  22457. '']));
  22458. end;
  22459. procedure TTestModule.TestProcType;
  22460. begin
  22461. StartProgram(false);
  22462. Add([
  22463. 'type',
  22464. ' TProcInt = procedure(vI: longint = 1);',
  22465. 'procedure DoIt(vJ: longint);',
  22466. 'begin end;',
  22467. 'var',
  22468. ' b: boolean;',
  22469. ' vP, vQ: tprocint;',
  22470. 'begin',
  22471. ' vp:=nil;',
  22472. ' vp:=vp;',
  22473. ' vp:=@doit;',
  22474. ' vp;',
  22475. ' vp();',
  22476. ' vp(2);',
  22477. ' b:=vp=nil;',
  22478. ' b:=nil=vp;',
  22479. ' b:=vp=vq;',
  22480. ' b:=vp=@doit;',
  22481. ' b:=@doit=vp;',
  22482. ' b:=vp<>nil;',
  22483. ' b:=nil<>vp;',
  22484. ' b:=vp<>vq;',
  22485. ' b:=vp<>@doit;',
  22486. ' b:=@doit<>vp;',
  22487. ' b:=Assigned(vp);',
  22488. ' if Assigned(vp) then ;']);
  22489. ConvertProgram;
  22490. CheckSource('TestProcType',
  22491. LinesToStr([ // statements
  22492. 'this.DoIt = function(vJ) {',
  22493. '};',
  22494. 'this.b = false;',
  22495. 'this.vP = null;',
  22496. 'this.vQ = null;'
  22497. ]),
  22498. LinesToStr([ // $mod.$main
  22499. '$mod.vP = null;',
  22500. '$mod.vP = $mod.vP;',
  22501. '$mod.vP = $mod.DoIt;',
  22502. '$mod.vP(1);',
  22503. '$mod.vP(1);',
  22504. '$mod.vP(2);',
  22505. '$mod.b = $mod.vP === null;',
  22506. '$mod.b = null === $mod.vP;',
  22507. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  22508. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  22509. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  22510. '$mod.b = $mod.vP !== null;',
  22511. '$mod.b = null !== $mod.vP;',
  22512. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  22513. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  22514. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  22515. '$mod.b = $mod.vP != null;',
  22516. 'if ($mod.vP != null) ;',
  22517. '']));
  22518. end;
  22519. procedure TTestModule.TestProcType_Arg;
  22520. begin
  22521. StartProgram(false);
  22522. Add([
  22523. 'type',
  22524. ' TProcInt = procedure(vI: longint = 1);',
  22525. 'procedure DoIt(vJ: longint); begin end;',
  22526. 'procedure DoSome(vP, vQ: TProcInt);',
  22527. 'var',
  22528. ' b: boolean;',
  22529. 'begin',
  22530. ' vp:=nil;',
  22531. ' vp:=vp;',
  22532. ' vp:=@doit;',
  22533. ' vp;',
  22534. ' vp();',
  22535. ' vp(2);',
  22536. ' b:=vp=nil;',
  22537. ' b:=nil=vp;',
  22538. ' b:=vp=vq;',
  22539. ' b:=vp=@doit;',
  22540. ' b:=@doit=vp;',
  22541. ' b:=vp<>nil;',
  22542. ' b:=nil<>vp;',
  22543. ' b:=vp<>vq;',
  22544. ' b:=vp<>@doit;',
  22545. ' b:=@doit<>vp;',
  22546. ' b:=Assigned(vp);',
  22547. ' if Assigned(vp) then ;',
  22548. 'end;',
  22549. 'begin',
  22550. ' DoSome(@DoIt,nil);']);
  22551. ConvertProgram;
  22552. CheckSource('TestProcType_Arg',
  22553. LinesToStr([ // statements
  22554. 'this.DoIt = function(vJ) {',
  22555. '};',
  22556. 'this.DoSome = function(vP, vQ) {',
  22557. ' var b = false;',
  22558. ' vP = null;',
  22559. ' vP = vP;',
  22560. ' vP = $mod.DoIt;',
  22561. ' vP(1);',
  22562. ' vP(1);',
  22563. ' vP(2);',
  22564. ' b = vP === null;',
  22565. ' b = null === vP;',
  22566. ' b = rtl.eqCallback(vP,vQ);',
  22567. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  22568. ' b = rtl.eqCallback($mod.DoIt, vP);',
  22569. ' b = vP !== null;',
  22570. ' b = null !== vP;',
  22571. ' b = !rtl.eqCallback(vP, vQ);',
  22572. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  22573. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  22574. ' b = vP != null;',
  22575. ' if (vP != null) ;',
  22576. '};',
  22577. '']),
  22578. LinesToStr([ // $mod.$main
  22579. '$mod.DoSome($mod.DoIt,null);',
  22580. '']));
  22581. end;
  22582. procedure TTestModule.TestProcType_FunctionFPC;
  22583. begin
  22584. StartProgram(false);
  22585. Add('type');
  22586. Add(' TFuncInt = function(vA: longint = 1): longint;');
  22587. Add('function DoIt(vI: longint): longint;');
  22588. Add('begin end;');
  22589. Add('var');
  22590. Add(' b: boolean;');
  22591. Add(' vP, vQ: tfuncint;');
  22592. Add('begin');
  22593. Add(' vp:=nil;');
  22594. Add(' vp:=vp;');
  22595. Add(' vp:=@doit;'); // ok in fpc and delphi
  22596. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  22597. Add(' vp;'); // ok in fpc and delphi
  22598. Add(' vp();');
  22599. Add(' vp(2);');
  22600. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  22601. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  22602. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  22603. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  22604. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  22605. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  22606. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  22607. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  22608. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  22609. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  22610. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  22611. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  22612. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  22613. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  22614. Add(' b:=Assigned(vp);');
  22615. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  22616. Add(' doit(vp());'); // ok in fpc and delphi
  22617. Add(' doit(vp(2));'); // ok in fpc and delphi
  22618. ConvertProgram;
  22619. CheckSource('TestProcType_FunctionFPC',
  22620. LinesToStr([ // statements
  22621. 'this.DoIt = function(vI) {',
  22622. ' var Result = 0;',
  22623. ' return Result;',
  22624. '};',
  22625. 'this.b = false;',
  22626. 'this.vP = null;',
  22627. 'this.vQ = null;'
  22628. ]),
  22629. LinesToStr([ // $mod.$main
  22630. '$mod.vP = null;',
  22631. '$mod.vP = $mod.vP;',
  22632. '$mod.vP = $mod.DoIt;',
  22633. '$mod.vP(1);',
  22634. '$mod.vP(1);',
  22635. '$mod.vP(2);',
  22636. '$mod.b = $mod.vP === null;',
  22637. '$mod.b = null === $mod.vP;',
  22638. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  22639. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  22640. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  22641. '$mod.b = 4 === $mod.vP(1);',
  22642. '$mod.b = $mod.vP !== null;',
  22643. '$mod.b = null !== $mod.vP;',
  22644. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  22645. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  22646. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  22647. '$mod.b = 6 !== $mod.vP(1);',
  22648. '$mod.b = $mod.vP != null;',
  22649. '$mod.DoIt($mod.vP(1));',
  22650. '$mod.DoIt($mod.vP(2));',
  22651. '']));
  22652. end;
  22653. procedure TTestModule.TestProcType_FunctionDelphi;
  22654. begin
  22655. StartProgram(false);
  22656. Add('{$mode Delphi}');
  22657. Add('type');
  22658. Add(' TFuncInt = function(vA: longint = 1): longint;');
  22659. Add('function DoIt(vI: longint): longint;');
  22660. Add('begin end;');
  22661. Add('var');
  22662. Add(' b: boolean;');
  22663. Add(' vP, vQ: tfuncint;');
  22664. Add('begin');
  22665. Add(' vp:=nil;');
  22666. Add(' vp:=vp;');
  22667. Add(' vp:=@doit;'); // ok in fpc and delphi
  22668. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  22669. Add(' vp;'); // ok in fpc and delphi
  22670. Add(' vp();');
  22671. Add(' vp(2);');
  22672. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  22673. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  22674. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  22675. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  22676. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  22677. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  22678. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  22679. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  22680. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  22681. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  22682. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  22683. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  22684. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  22685. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  22686. Add(' b:=Assigned(vp);');
  22687. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  22688. Add(' doit(vp());'); // ok in fpc and delphi
  22689. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  22690. ConvertProgram;
  22691. CheckSource('TestProcType_FunctionDelphi',
  22692. LinesToStr([ // statements
  22693. 'this.DoIt = function(vI) {',
  22694. ' var Result = 0;',
  22695. ' return Result;',
  22696. '};',
  22697. 'this.b = false;',
  22698. 'this.vP = null;',
  22699. 'this.vQ = null;'
  22700. ]),
  22701. LinesToStr([ // $mod.$main
  22702. '$mod.vP = null;',
  22703. '$mod.vP = $mod.vP;',
  22704. '$mod.vP = $mod.DoIt;',
  22705. '$mod.vP = $mod.DoIt;',
  22706. '$mod.vP(1);',
  22707. '$mod.vP(1);',
  22708. '$mod.vP(2);',
  22709. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  22710. '$mod.b = $mod.vP(1) === 3;',
  22711. '$mod.b = 4 === $mod.vP(1);',
  22712. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  22713. '$mod.b = $mod.vP(1) !== 5;',
  22714. '$mod.b = 6 !== $mod.vP(1);',
  22715. '$mod.b = $mod.vP != null;',
  22716. '$mod.DoIt($mod.vP(1));',
  22717. '$mod.DoIt($mod.vP(1));',
  22718. '$mod.DoIt($mod.vP(2));',
  22719. '']));
  22720. end;
  22721. procedure TTestModule.TestProcType_ProcedureDelphi;
  22722. begin
  22723. StartProgram(false);
  22724. Add('{$mode Delphi}');
  22725. Add('type');
  22726. Add(' TProc = procedure;');
  22727. Add('procedure DoIt;');
  22728. Add('begin end;');
  22729. Add('var');
  22730. Add(' b: boolean;');
  22731. Add(' vP, vQ: tproc;');
  22732. Add('begin');
  22733. Add(' vp:=nil;');
  22734. Add(' vp:=vp;');
  22735. Add(' vp:=vq;');
  22736. 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
  22737. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  22738. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  22739. Add(' vp;'); // ok in fpc and delphi
  22740. Add(' vp();');
  22741. // equal
  22742. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  22743. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  22744. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  22745. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  22746. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  22747. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  22748. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  22749. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  22750. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  22751. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  22752. // unequal
  22753. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  22754. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  22755. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  22756. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  22757. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  22758. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  22759. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  22760. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  22761. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  22762. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  22763. Add(' b:=Assigned(vp);');
  22764. ConvertProgram;
  22765. CheckSource('TestProcType_ProcedureDelphi',
  22766. LinesToStr([ // statements
  22767. 'this.DoIt = function() {',
  22768. '};',
  22769. 'this.b = false;',
  22770. 'this.vP = null;',
  22771. 'this.vQ = null;'
  22772. ]),
  22773. LinesToStr([ // $mod.$main
  22774. '$mod.vP = null;',
  22775. '$mod.vP = $mod.vP;',
  22776. '$mod.vP = $mod.vQ;',
  22777. '$mod.vP = $mod.DoIt;',
  22778. '$mod.vP = $mod.DoIt;',
  22779. '$mod.vP();',
  22780. '$mod.vP();',
  22781. '$mod.b = $mod.vP === null;',
  22782. '$mod.b = null === $mod.vP;',
  22783. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  22784. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  22785. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  22786. '$mod.b = $mod.vP !== null;',
  22787. '$mod.b = null !== $mod.vP;',
  22788. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  22789. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  22790. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  22791. '$mod.b = $mod.vP != null;',
  22792. '']));
  22793. end;
  22794. procedure TTestModule.TestProcType_AsParam;
  22795. begin
  22796. StartProgram(false);
  22797. Add('type');
  22798. Add(' TFuncInt = function(vA: longint = 1): longint;');
  22799. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  22800. Add('var vJ: tfuncint;');
  22801. Add('begin');
  22802. Add(' vg:=vg;');
  22803. Add(' vj:=vh;');
  22804. Add(' vi:=vi;');
  22805. Add(' doit(vg,vg,vg);');
  22806. Add(' doit(vh,vh,vj);');
  22807. Add(' doit(vi,vi,vi);');
  22808. Add(' doit(vj,vj,vj);');
  22809. Add('end;');
  22810. Add('var i: tfuncint;');
  22811. Add('begin');
  22812. Add(' doit(i,i,i);');
  22813. ConvertProgram;
  22814. CheckSource('TestProcType_AsParam',
  22815. LinesToStr([ // statements
  22816. 'this.DoIt = function (vG,vH,vI) {',
  22817. ' var vJ = null;',
  22818. ' vG = vG;',
  22819. ' vJ = vH;',
  22820. ' vI.set(vI.get());',
  22821. ' $mod.DoIt(vG, vG, {',
  22822. ' get: function () {',
  22823. ' return vG;',
  22824. ' },',
  22825. ' set: function (v) {',
  22826. ' vG = v;',
  22827. ' }',
  22828. ' });',
  22829. ' $mod.DoIt(vH, vH, {',
  22830. ' get: function () {',
  22831. ' return vJ;',
  22832. ' },',
  22833. ' set: function (v) {',
  22834. ' vJ = v;',
  22835. ' }',
  22836. ' });',
  22837. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  22838. ' $mod.DoIt(vJ, vJ, {',
  22839. ' get: function () {',
  22840. ' return vJ;',
  22841. ' },',
  22842. ' set: function (v) {',
  22843. ' vJ = v;',
  22844. ' }',
  22845. ' });',
  22846. '};',
  22847. 'this.i = null;'
  22848. ]),
  22849. LinesToStr([
  22850. '$mod.DoIt($mod.i,$mod.i,{',
  22851. ' p: $mod,',
  22852. ' get: function () {',
  22853. ' return this.p.i;',
  22854. ' },',
  22855. ' set: function (v) {',
  22856. ' this.p.i = v;',
  22857. ' }',
  22858. '});'
  22859. ]));
  22860. end;
  22861. procedure TTestModule.TestProcType_MethodFPC;
  22862. begin
  22863. StartProgram(false);
  22864. Add('type');
  22865. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  22866. Add(' TObject = class');
  22867. Add(' function DoIt(vA: longint = 1): longint;');
  22868. Add(' end;');
  22869. Add('function TObject.DoIt(vA: longint = 1): longint;');
  22870. Add('begin');
  22871. Add('end;');
  22872. Add('var');
  22873. Add(' Obj: TObject;');
  22874. Add(' vP: tfuncint;');
  22875. Add(' b: boolean;');
  22876. Add('begin');
  22877. Add(' vp:[email protected];'); // ok in fpc and delphi
  22878. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  22879. Add(' vp;'); // ok in fpc and delphi
  22880. Add(' vp();');
  22881. Add(' vp(2);');
  22882. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  22883. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  22884. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  22885. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  22886. ConvertProgram;
  22887. CheckSource('TestProcType_MethodFPC',
  22888. LinesToStr([ // statements
  22889. 'rtl.createClass($mod, "TObject", null, function () {',
  22890. ' this.$init = function () {',
  22891. ' };',
  22892. ' this.$final = function () {',
  22893. ' };',
  22894. ' this.DoIt = function (vA) {',
  22895. ' var Result = 0;',
  22896. ' return Result;',
  22897. ' };',
  22898. '});',
  22899. 'this.Obj = null;',
  22900. 'this.vP = null;',
  22901. 'this.b = false;'
  22902. ]),
  22903. LinesToStr([
  22904. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  22905. '$mod.vP(1);',
  22906. '$mod.vP(1);',
  22907. '$mod.vP(2);',
  22908. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  22909. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  22910. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  22911. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  22912. '']));
  22913. end;
  22914. procedure TTestModule.TestProcType_MethodDelphi;
  22915. begin
  22916. StartProgram(false);
  22917. Add('{$mode delphi}');
  22918. Add('type');
  22919. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  22920. Add(' TObject = class');
  22921. Add(' function DoIt(vA: longint = 1): longint;');
  22922. Add(' end;');
  22923. Add('function TObject.DoIt(vA: longint = 1): longint;');
  22924. Add('begin');
  22925. Add('end;');
  22926. Add('var');
  22927. Add(' Obj: TObject;');
  22928. Add(' vP: tfuncint;');
  22929. Add(' b: boolean;');
  22930. Add('begin');
  22931. Add(' vp:[email protected];'); // ok in fpc and delphi
  22932. Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  22933. Add(' vp;'); // ok in fpc and delphi
  22934. Add(' vp();');
  22935. Add(' vp(2);');
  22936. //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  22937. //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  22938. //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  22939. //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  22940. ConvertProgram;
  22941. CheckSource('TestProcType_MethodDelphi',
  22942. LinesToStr([ // statements
  22943. 'rtl.createClass($mod, "TObject", null, function () {',
  22944. ' this.$init = function () {',
  22945. ' };',
  22946. ' this.$final = function () {',
  22947. ' };',
  22948. ' this.DoIt = function (vA) {',
  22949. ' var Result = 0;',
  22950. ' return Result;',
  22951. ' };',
  22952. '});',
  22953. 'this.Obj = null;',
  22954. 'this.vP = null;',
  22955. 'this.b = false;'
  22956. ]),
  22957. LinesToStr([
  22958. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  22959. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  22960. '$mod.vP(1);',
  22961. '$mod.vP(1);',
  22962. '$mod.vP(2);',
  22963. '']));
  22964. end;
  22965. procedure TTestModule.TestProcType_PropertyFPC;
  22966. begin
  22967. StartProgram(false);
  22968. Add('type');
  22969. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  22970. Add(' TObject = class');
  22971. Add(' FOnFoo: TFuncInt;');
  22972. Add(' function DoIt(vA: longint = 1): longint;');
  22973. Add(' function GetFoo: TFuncInt;');
  22974. Add(' procedure SetFoo(const Value: TFuncInt);');
  22975. Add(' function GetEvents(Index: longint): TFuncInt;');
  22976. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  22977. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  22978. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  22979. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  22980. Add(' end;');
  22981. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  22982. Add('function tobject.getfoo: tfuncint; begin end;');
  22983. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  22984. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  22985. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  22986. Add('var');
  22987. Add(' Obj: TObject;');
  22988. Add(' vP: tfuncint;');
  22989. Add(' b: boolean;');
  22990. Add('begin');
  22991. Add(' obj.onfoo:=nil;');
  22992. Add(' obj.onbar:=nil;');
  22993. Add(' obj.events[1]:=nil;');
  22994. Add(' obj.onfoo:=obj.onfoo;');
  22995. Add(' obj.onbar:=obj.onbar;');
  22996. Add(' obj.events[2]:=obj.events[3];');
  22997. Add(' obj.onfoo:[email protected];');
  22998. Add(' obj.onbar:[email protected];');
  22999. Add(' obj.events[4]:[email protected];');
  23000. //Add(' obj.onfoo:=obj.doit;'); // delphi
  23001. //Add(' obj.onbar:=obj.doit;'); // delphi
  23002. //Add(' obj.events[4]:=obj.doit;'); // delphi
  23003. Add(' obj.onfoo;');
  23004. Add(' obj.onbar;');
  23005. //Add(' obj.events[5];'); ToDo in pasresolver
  23006. Add(' obj.onfoo();');
  23007. Add(' obj.onbar();');
  23008. Add(' obj.events[6]();');
  23009. Add(' b:=obj.onfoo=nil;');
  23010. Add(' b:=obj.onbar=nil;');
  23011. Add(' b:=obj.events[7]=nil;');
  23012. Add(' b:=obj.onfoo<>nil;');
  23013. Add(' b:=obj.onbar<>nil;');
  23014. Add(' b:=obj.events[8]<>nil;');
  23015. Add(' b:=obj.onfoo=vp;');
  23016. Add(' b:=obj.onbar=vp;');
  23017. Add(' b:=obj.events[9]=vp;');
  23018. Add(' b:=obj.onfoo=obj.onfoo;');
  23019. Add(' b:=obj.onbar=obj.onfoo;');
  23020. Add(' b:=obj.events[10]=obj.onfoo;');
  23021. Add(' b:=obj.onfoo<>obj.onfoo;');
  23022. Add(' b:=obj.onbar<>obj.onfoo;');
  23023. Add(' b:=obj.events[11]<>obj.onfoo;');
  23024. Add(' b:[email protected];');
  23025. Add(' b:[email protected];');
  23026. Add(' b:=obj.events[12][email protected];');
  23027. Add(' b:=obj.onfoo<>@obj.doit;');
  23028. Add(' b:=obj.onbar<>@obj.doit;');
  23029. Add(' b:=obj.events[12]<>@obj.doit;');
  23030. Add(' b:=Assigned(obj.onfoo);');
  23031. Add(' b:=Assigned(obj.onbar);');
  23032. Add(' b:=Assigned(obj.events[13]);');
  23033. ConvertProgram;
  23034. CheckSource('TestProcType_PropertyFPC',
  23035. LinesToStr([ // statements
  23036. 'rtl.createClass($mod, "TObject", null, function () {',
  23037. ' this.$init = function () {',
  23038. ' this.FOnFoo = null;',
  23039. ' };',
  23040. ' this.$final = function () {',
  23041. ' this.FOnFoo = undefined;',
  23042. ' };',
  23043. ' this.DoIt = function (vA) {',
  23044. ' var Result = 0;',
  23045. ' return Result;',
  23046. ' };',
  23047. 'this.GetFoo = function () {',
  23048. ' var Result = null;',
  23049. ' return Result;',
  23050. '};',
  23051. 'this.SetFoo = function (Value) {',
  23052. '};',
  23053. 'this.GetEvents = function (Index) {',
  23054. ' var Result = null;',
  23055. ' return Result;',
  23056. '};',
  23057. 'this.SetEvents = function (Index, Value) {',
  23058. '};',
  23059. '});',
  23060. 'this.Obj = null;',
  23061. 'this.vP = null;',
  23062. 'this.b = false;'
  23063. ]),
  23064. LinesToStr([
  23065. '$mod.Obj.FOnFoo = null;',
  23066. '$mod.Obj.SetFoo(null);',
  23067. '$mod.Obj.SetEvents(1, null);',
  23068. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  23069. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  23070. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  23071. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  23072. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  23073. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  23074. '$mod.Obj.FOnFoo(1);',
  23075. '$mod.Obj.GetFoo();',
  23076. '$mod.Obj.FOnFoo(1);',
  23077. '$mod.Obj.GetFoo()(1);',
  23078. '$mod.Obj.GetEvents(6)(1);',
  23079. '$mod.b = $mod.Obj.FOnFoo === null;',
  23080. '$mod.b = $mod.Obj.GetFoo() === null;',
  23081. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  23082. '$mod.b = $mod.Obj.FOnFoo !== null;',
  23083. '$mod.b = $mod.Obj.GetFoo() !== null;',
  23084. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  23085. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  23086. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  23087. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  23088. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  23089. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  23090. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  23091. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  23092. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  23093. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  23094. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  23095. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  23096. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  23097. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  23098. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  23099. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  23100. '$mod.b = $mod.Obj.FOnFoo != null;',
  23101. '$mod.b = $mod.Obj.GetFoo() != null;',
  23102. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  23103. '']));
  23104. end;
  23105. procedure TTestModule.TestProcType_PropertyDelphi;
  23106. begin
  23107. StartProgram(false);
  23108. Add('{$mode delphi}');
  23109. Add('type');
  23110. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23111. Add(' TObject = class');
  23112. Add(' FOnFoo: TFuncInt;');
  23113. Add(' function DoIt(vA: longint = 1): longint;');
  23114. Add(' function GetFoo: TFuncInt;');
  23115. Add(' procedure SetFoo(const Value: TFuncInt);');
  23116. Add(' function GetEvents(Index: longint): TFuncInt;');
  23117. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  23118. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  23119. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  23120. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  23121. Add(' end;');
  23122. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  23123. Add('function tobject.getfoo: tfuncint; begin end;');
  23124. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  23125. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  23126. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  23127. Add('var');
  23128. Add(' Obj: TObject;');
  23129. Add(' vP: tfuncint;');
  23130. Add(' b: boolean;');
  23131. Add('begin');
  23132. Add(' obj.onfoo:=nil;');
  23133. Add(' obj.onbar:=nil;');
  23134. Add(' obj.events[1]:=nil;');
  23135. Add(' obj.onfoo:=obj.onfoo;');
  23136. Add(' obj.onbar:=obj.onbar;');
  23137. Add(' obj.events[2]:=obj.events[3];');
  23138. Add(' obj.onfoo:[email protected];');
  23139. Add(' obj.onbar:[email protected];');
  23140. Add(' obj.events[4]:[email protected];');
  23141. Add(' obj.onfoo:=obj.doit;'); // delphi
  23142. Add(' obj.onbar:=obj.doit;'); // delphi
  23143. Add(' obj.events[4]:=obj.doit;'); // delphi
  23144. Add(' obj.onfoo;');
  23145. Add(' obj.onbar;');
  23146. //Add(' obj.events[5];'); ToDo in pasresolver
  23147. Add(' obj.onfoo();');
  23148. Add(' obj.onbar();');
  23149. Add(' obj.events[6]();');
  23150. //Add(' b:=obj.onfoo=nil;'); // fpc
  23151. //Add(' b:=obj.onbar=nil;'); // fpc
  23152. //Add(' b:=obj.events[7]=nil;'); // fpc
  23153. //Add(' b:=obj.onfoo<>nil;'); // fpc
  23154. //Add(' b:=obj.onbar<>nil;'); // fpc
  23155. //Add(' b:=obj.events[8]<>nil;'); // fpc
  23156. Add(' b:=obj.onfoo=vp;');
  23157. Add(' b:=obj.onbar=vp;');
  23158. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  23159. Add(' b:=obj.onfoo=obj.onfoo;');
  23160. Add(' b:=obj.onbar=obj.onfoo;');
  23161. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  23162. Add(' b:=obj.onfoo<>obj.onfoo;');
  23163. Add(' b:=obj.onbar<>obj.onfoo;');
  23164. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  23165. //Add(' b:[email protected];'); // fpc
  23166. //Add(' b:[email protected];'); // fpc
  23167. //Add(' b:=obj.events[12][email protected];'); // fpc
  23168. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  23169. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  23170. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  23171. Add(' b:=Assigned(obj.onfoo);');
  23172. Add(' b:=Assigned(obj.onbar);');
  23173. Add(' b:=Assigned(obj.events[13]);');
  23174. ConvertProgram;
  23175. CheckSource('TestProcType_PropertyDelphi',
  23176. LinesToStr([ // statements
  23177. 'rtl.createClass($mod, "TObject", null, function () {',
  23178. ' this.$init = function () {',
  23179. ' this.FOnFoo = null;',
  23180. ' };',
  23181. ' this.$final = function () {',
  23182. ' this.FOnFoo = undefined;',
  23183. ' };',
  23184. ' this.DoIt = function (vA) {',
  23185. ' var Result = 0;',
  23186. ' return Result;',
  23187. ' };',
  23188. 'this.GetFoo = function () {',
  23189. ' var Result = null;',
  23190. ' return Result;',
  23191. '};',
  23192. 'this.SetFoo = function (Value) {',
  23193. '};',
  23194. 'this.GetEvents = function (Index) {',
  23195. ' var Result = null;',
  23196. ' return Result;',
  23197. '};',
  23198. 'this.SetEvents = function (Index, Value) {',
  23199. '};',
  23200. '});',
  23201. 'this.Obj = null;',
  23202. 'this.vP = null;',
  23203. 'this.b = false;'
  23204. ]),
  23205. LinesToStr([
  23206. '$mod.Obj.FOnFoo = null;',
  23207. '$mod.Obj.SetFoo(null);',
  23208. '$mod.Obj.SetEvents(1, null);',
  23209. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  23210. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  23211. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  23212. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  23213. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  23214. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  23215. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  23216. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  23217. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  23218. '$mod.Obj.FOnFoo(1);',
  23219. '$mod.Obj.GetFoo();',
  23220. '$mod.Obj.FOnFoo(1);',
  23221. '$mod.Obj.GetFoo()(1);',
  23222. '$mod.Obj.GetEvents(6)(1);',
  23223. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  23224. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  23225. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  23226. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  23227. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  23228. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  23229. '$mod.b = $mod.Obj.FOnFoo != null;',
  23230. '$mod.b = $mod.Obj.GetFoo() != null;',
  23231. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  23232. '']));
  23233. end;
  23234. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  23235. begin
  23236. StartProgram(false);
  23237. Add('type');
  23238. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23239. Add(' TObject = class');
  23240. Add(' FOnFoo: TFuncInt;');
  23241. Add(' function DoIt(vA: longint = 1): longint;');
  23242. Add(' function GetFoo: TFuncInt;');
  23243. Add(' procedure SetFoo(const Value: TFuncInt);');
  23244. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  23245. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  23246. Add(' end;');
  23247. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  23248. Add('function tobject.getfoo: tfuncint; begin end;');
  23249. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  23250. Add('var');
  23251. Add(' Obj: TObject;');
  23252. Add(' vP: tfuncint;');
  23253. Add(' b: boolean;');
  23254. Add('begin');
  23255. Add('with obj do begin');
  23256. Add(' fonfoo:=nil;');
  23257. Add(' onfoo:=nil;');
  23258. Add(' onbar:=nil;');
  23259. Add(' fonfoo:=fonfoo;');
  23260. Add(' onfoo:=onfoo;');
  23261. Add(' onbar:=onbar;');
  23262. Add(' fonfoo:=@doit;');
  23263. Add(' onfoo:=@doit;');
  23264. Add(' onbar:=@doit;');
  23265. //Add(' fonfoo:=doit;'); // delphi
  23266. //Add(' onfoo:=doit;'); // delphi
  23267. //Add(' onbar:=doit;'); // delphi
  23268. Add(' fonfoo;');
  23269. Add(' onfoo;');
  23270. Add(' onbar;');
  23271. Add(' fonfoo();');
  23272. Add(' onfoo();');
  23273. Add(' onbar();');
  23274. Add(' b:=fonfoo=nil;');
  23275. Add(' b:=onfoo=nil;');
  23276. Add(' b:=onbar=nil;');
  23277. Add(' b:=fonfoo<>nil;');
  23278. Add(' b:=onfoo<>nil;');
  23279. Add(' b:=onbar<>nil;');
  23280. Add(' b:=fonfoo=vp;');
  23281. Add(' b:=onfoo=vp;');
  23282. Add(' b:=onbar=vp;');
  23283. Add(' b:=fonfoo=fonfoo;');
  23284. Add(' b:=onfoo=onfoo;');
  23285. Add(' b:=onbar=onfoo;');
  23286. Add(' b:=fonfoo<>fonfoo;');
  23287. Add(' b:=onfoo<>onfoo;');
  23288. Add(' b:=onbar<>onfoo;');
  23289. Add(' b:=fonfoo=@doit;');
  23290. Add(' b:=onfoo=@doit;');
  23291. Add(' b:=onbar=@doit;');
  23292. Add(' b:=fonfoo<>@doit;');
  23293. Add(' b:=onfoo<>@doit;');
  23294. Add(' b:=onbar<>@doit;');
  23295. Add(' b:=Assigned(fonfoo);');
  23296. Add(' b:=Assigned(onfoo);');
  23297. Add(' b:=Assigned(onbar);');
  23298. Add('end;');
  23299. ConvertProgram;
  23300. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  23301. LinesToStr([ // statements
  23302. 'rtl.createClass($mod, "TObject", null, function () {',
  23303. ' this.$init = function () {',
  23304. ' this.FOnFoo = null;',
  23305. ' };',
  23306. ' this.$final = function () {',
  23307. ' this.FOnFoo = undefined;',
  23308. ' };',
  23309. ' this.DoIt = function (vA) {',
  23310. ' var Result = 0;',
  23311. ' return Result;',
  23312. ' };',
  23313. ' this.GetFoo = function () {',
  23314. ' var Result = null;',
  23315. ' return Result;',
  23316. ' };',
  23317. ' this.SetFoo = function (Value) {',
  23318. ' };',
  23319. '});',
  23320. 'this.Obj = null;',
  23321. 'this.vP = null;',
  23322. 'this.b = false;'
  23323. ]),
  23324. LinesToStr([
  23325. 'var $with1 = $mod.Obj;',
  23326. '$with1.FOnFoo = null;',
  23327. '$with1.FOnFoo = null;',
  23328. '$with1.SetFoo(null);',
  23329. '$with1.FOnFoo = $with1.FOnFoo;',
  23330. '$with1.FOnFoo = $with1.FOnFoo;',
  23331. '$with1.SetFoo($with1.GetFoo());',
  23332. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  23333. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  23334. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  23335. '$with1.FOnFoo(1);',
  23336. '$with1.FOnFoo(1);',
  23337. '$with1.GetFoo();',
  23338. '$with1.FOnFoo(1);',
  23339. '$with1.FOnFoo(1);',
  23340. '$with1.GetFoo()(1);',
  23341. '$mod.b = $with1.FOnFoo === null;',
  23342. '$mod.b = $with1.FOnFoo === null;',
  23343. '$mod.b = $with1.GetFoo() === null;',
  23344. '$mod.b = $with1.FOnFoo !== null;',
  23345. '$mod.b = $with1.FOnFoo !== null;',
  23346. '$mod.b = $with1.GetFoo() !== null;',
  23347. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  23348. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  23349. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  23350. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23351. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23352. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  23353. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23354. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23355. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  23356. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23357. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23358. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  23359. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23360. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23361. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  23362. '$mod.b = $with1.FOnFoo != null;',
  23363. '$mod.b = $with1.FOnFoo != null;',
  23364. '$mod.b = $with1.GetFoo() != null;',
  23365. '']));
  23366. end;
  23367. procedure TTestModule.TestProcType_Nested;
  23368. begin
  23369. StartProgram(false);
  23370. Add([
  23371. 'type',
  23372. ' TProcInt = procedure(vI: longint = 1);',
  23373. 'procedure DoIt(vJ: longint);',
  23374. 'var aProc: TProcInt;',
  23375. ' b: boolean;',
  23376. ' procedure Sub(vK: longint);',
  23377. ' var aSub: TProcInt;',
  23378. ' procedure SubSub(vK: longint);',
  23379. ' var aSubSub: TProcInt;',
  23380. ' begin;',
  23381. ' aProc:=@DoIt;',
  23382. ' aSub:=@DoIt;',
  23383. ' aSubSub:=@DoIt;',
  23384. ' aProc:=@Sub;',
  23385. ' aSub:=@Sub;',
  23386. ' aSubSub:=@Sub;',
  23387. ' aProc:=@SubSub;',
  23388. ' aSub:=@SubSub;',
  23389. ' aSubSub:=@SubSub;',
  23390. ' end;',
  23391. ' begin;',
  23392. ' end;',
  23393. 'begin;',
  23394. ' aProc:=@Sub;',
  23395. ' b:=aProc=@Sub;',
  23396. ' b:=@Sub=aProc;',
  23397. 'end;',
  23398. 'begin',
  23399. '']);
  23400. ConvertProgram;
  23401. CheckSource('TestProcType_Nested',
  23402. LinesToStr([ // statements
  23403. 'this.DoIt = function (vJ) {',
  23404. ' var aProc = null;',
  23405. ' var b = false;',
  23406. ' function Sub(vK) {',
  23407. ' var aSub = null;',
  23408. ' function SubSub(vK) {',
  23409. ' var aSubSub = null;',
  23410. ' aProc = $mod.DoIt;',
  23411. ' aSub = $mod.DoIt;',
  23412. ' aSubSub = $mod.DoIt;',
  23413. ' aProc = Sub;',
  23414. ' aSub = Sub;',
  23415. ' aSubSub = Sub;',
  23416. ' aProc = SubSub;',
  23417. ' aSub = SubSub;',
  23418. ' aSubSub = SubSub;',
  23419. ' };',
  23420. ' };',
  23421. ' aProc = Sub;',
  23422. ' b = rtl.eqCallback(aProc, Sub);',
  23423. ' b = rtl.eqCallback(Sub, aProc);',
  23424. '};',
  23425. '']),
  23426. LinesToStr([ // $mod.$main
  23427. '']));
  23428. end;
  23429. procedure TTestModule.TestProcType_NestedOfObject;
  23430. begin
  23431. StartProgram(false);
  23432. Add([
  23433. 'type',
  23434. ' TProcInt = procedure(vI: longint = 1) of object;',
  23435. ' TObject = class',
  23436. ' procedure DoIt(vJ: longint);',
  23437. ' end;',
  23438. 'procedure TObject.DoIt(vJ: longint);',
  23439. 'var aProc: TProcInt;',
  23440. ' b: boolean;',
  23441. ' procedure Sub(vK: longint);',
  23442. ' var aSub: TProcInt;',
  23443. ' procedure SubSub(vK: longint);',
  23444. ' var aSubSub: TProcInt;',
  23445. ' begin;',
  23446. ' aProc:=@DoIt;',
  23447. ' aSub:=@DoIt;',
  23448. ' aSubSub:=@DoIt;',
  23449. ' aProc:=@Sub;',
  23450. ' aSub:=@Sub;',
  23451. ' aSubSub:=@Sub;',
  23452. ' aProc:=@SubSub;',
  23453. ' aSub:=@SubSub;',
  23454. ' aSubSub:=@SubSub;',
  23455. ' end;',
  23456. ' begin;',
  23457. ' end;',
  23458. 'begin;',
  23459. ' aProc:=@Sub;',
  23460. ' b:=aProc=@Sub;',
  23461. ' b:=@Sub=aProc;',
  23462. 'end;',
  23463. 'begin',
  23464. '']);
  23465. ConvertProgram;
  23466. CheckSource('TestProcType_Nested',
  23467. LinesToStr([ // statements
  23468. 'rtl.createClass($mod, "TObject", null, function () {',
  23469. ' this.$init = function () {',
  23470. ' };',
  23471. ' this.$final = function () {',
  23472. ' };',
  23473. ' this.DoIt = function (vJ) {',
  23474. ' var $Self = this;',
  23475. ' var aProc = null;',
  23476. ' var b = false;',
  23477. ' function Sub(vK) {',
  23478. ' var aSub = null;',
  23479. ' function SubSub(vK) {',
  23480. ' var aSubSub = null;',
  23481. ' aProc = rtl.createCallback($Self, "DoIt");',
  23482. ' aSub = rtl.createCallback($Self, "DoIt");',
  23483. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  23484. ' aProc = Sub;',
  23485. ' aSub = Sub;',
  23486. ' aSubSub = Sub;',
  23487. ' aProc = SubSub;',
  23488. ' aSub = SubSub;',
  23489. ' aSubSub = SubSub;',
  23490. ' };',
  23491. ' };',
  23492. ' aProc = Sub;',
  23493. ' b = rtl.eqCallback(aProc, Sub);',
  23494. ' b = rtl.eqCallback(Sub, aProc);',
  23495. ' };',
  23496. '});',
  23497. '']),
  23498. LinesToStr([ // $mod.$main
  23499. '']));
  23500. end;
  23501. procedure TTestModule.TestProcType_ReferenceToProc;
  23502. begin
  23503. StartProgram(false);
  23504. Add([
  23505. 'type',
  23506. ' TProcRef = reference to procedure(i: longint = 0);',
  23507. ' TFuncRef = reference to function(i: longint = 0): longint;',
  23508. 'var',
  23509. ' p: TProcRef;',
  23510. ' f: TFuncRef;',
  23511. 'procedure DoIt(i: longint);',
  23512. 'begin',
  23513. 'end;',
  23514. 'function GetIt(i: longint): longint;',
  23515. 'begin',
  23516. ' p:=@DoIt;',
  23517. ' f:=@GetIt;',
  23518. ' f;',
  23519. ' f();',
  23520. ' f(1);',
  23521. 'end;',
  23522. 'begin',
  23523. ' p:=@DoIt;',
  23524. ' f:=@GetIt;',
  23525. ' f;',
  23526. ' f();',
  23527. ' f(1);',
  23528. ' p:=TProcRef(f);',
  23529. '']);
  23530. ConvertProgram;
  23531. CheckSource('TestProcType_ReferenceToProc',
  23532. LinesToStr([ // statements
  23533. 'this.p = null;',
  23534. 'this.f = null;',
  23535. 'this.DoIt = function (i) {',
  23536. '};',
  23537. 'this.GetIt = function (i) {',
  23538. ' var Result = 0;',
  23539. ' $mod.p = $mod.DoIt;',
  23540. ' $mod.f = $mod.GetIt;',
  23541. ' $mod.f(0);',
  23542. ' $mod.f(0);',
  23543. ' $mod.f(1);',
  23544. ' return Result;',
  23545. '};',
  23546. '']),
  23547. LinesToStr([ // $mod.$main
  23548. '$mod.p = $mod.DoIt;',
  23549. '$mod.f = $mod.GetIt;',
  23550. '$mod.f(0);',
  23551. '$mod.f(0);',
  23552. '$mod.f(1);',
  23553. '$mod.p = $mod.f;',
  23554. '']));
  23555. end;
  23556. procedure TTestModule.TestProcType_ReferenceToMethod;
  23557. begin
  23558. StartProgram(false);
  23559. Add([
  23560. 'type',
  23561. ' TFuncRef = reference to function(i: longint = 5): longint;',
  23562. ' TObject = class',
  23563. ' function Grow(s: longint): longint;',
  23564. ' end;',
  23565. 'var',
  23566. ' f: tfuncref;',
  23567. 'function tobject.grow(s: longint): longint;',
  23568. ' function GrowSub(i: longint): longint;',
  23569. ' begin',
  23570. ' f:=@grow;',
  23571. ' f:=@growsub;',
  23572. ' end;',
  23573. 'begin',
  23574. ' f:=@grow;',
  23575. ' f:=@growsub;',
  23576. 'end;',
  23577. 'begin',
  23578. '']);
  23579. ConvertProgram;
  23580. CheckSource('TestProcType_ReferenceToMethod',
  23581. LinesToStr([ // statements
  23582. 'rtl.createClass($mod, "TObject", null, function () {',
  23583. ' this.$init = function () {',
  23584. ' };',
  23585. ' this.$final = function () {',
  23586. ' };',
  23587. ' this.Grow = function (s) {',
  23588. ' var $Self = this;',
  23589. ' var Result = 0;',
  23590. ' function GrowSub(i) {',
  23591. ' var Result = 0;',
  23592. ' $mod.f = rtl.createCallback($Self, "Grow");',
  23593. ' $mod.f = GrowSub;',
  23594. ' return Result;',
  23595. ' };',
  23596. ' $mod.f = rtl.createCallback($Self, "Grow");',
  23597. ' $mod.f = GrowSub;',
  23598. ' return Result;',
  23599. ' };',
  23600. '});',
  23601. 'this.f = null;',
  23602. '']),
  23603. LinesToStr([ // $mod.$main
  23604. '']));
  23605. end;
  23606. procedure TTestModule.TestProcType_Typecast;
  23607. begin
  23608. StartProgram(false);
  23609. Add([
  23610. 'type',
  23611. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  23612. ' TEvent = procedure of object;',
  23613. ' TGetter = function:longint of object;',
  23614. ' TProcA = procedure(i: longint);',
  23615. ' TFuncB = function(i, j: longint): longint;',
  23616. 'procedure DoIt(); varargs; begin end;',
  23617. 'var',
  23618. ' Notify: tnotifyevent;',
  23619. ' Event: tevent;',
  23620. ' Getter: tgetter;',
  23621. ' ProcA: tproca;',
  23622. ' FuncB: tfuncb;',
  23623. ' p: pointer;',
  23624. 'begin',
  23625. ' notify:=tnotifyevent(event);',
  23626. ' event:=tevent(event);',
  23627. ' event:=tevent(notify);',
  23628. ' event:=tevent(getter);',
  23629. ' event:=tevent(proca);',
  23630. ' proca:=tproca(funcb);',
  23631. ' funcb:=tfuncb(funcb);',
  23632. ' funcb:=tfuncb(proca);',
  23633. ' funcb:=tfuncb(getter);',
  23634. ' proca:=tproca(p);',
  23635. ' funcb:=tfuncb(p);',
  23636. ' getter:=tgetter(p);',
  23637. ' p:=pointer(notify);',
  23638. ' p:=notify;',
  23639. ' p:=pointer(proca);',
  23640. ' p:=proca;',
  23641. ' p:=pointer(funcb);',
  23642. ' p:=funcb;',
  23643. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  23644. '']);
  23645. ConvertProgram;
  23646. CheckSource('TestProcType_Typecast',
  23647. LinesToStr([ // statements
  23648. 'this.DoIt = function () {',
  23649. '};',
  23650. 'this.Notify = null;',
  23651. 'this.Event = null;',
  23652. 'this.Getter = null;',
  23653. 'this.ProcA = null;',
  23654. 'this.FuncB = null;',
  23655. 'this.p = null;',
  23656. '']),
  23657. LinesToStr([ // $mod.$main
  23658. '$mod.Notify = $mod.Event;',
  23659. '$mod.Event = $mod.Event;',
  23660. '$mod.Event = $mod.Notify;',
  23661. '$mod.Event = $mod.Getter;',
  23662. '$mod.Event = $mod.ProcA;',
  23663. '$mod.ProcA = $mod.FuncB;',
  23664. '$mod.FuncB = $mod.FuncB;',
  23665. '$mod.FuncB = $mod.ProcA;',
  23666. '$mod.FuncB = $mod.Getter;',
  23667. '$mod.ProcA = $mod.p;',
  23668. '$mod.FuncB = $mod.p;',
  23669. '$mod.Getter = $mod.p;',
  23670. '$mod.p = $mod.Notify;',
  23671. '$mod.p = $mod.Notify;',
  23672. '$mod.p = $mod.ProcA;',
  23673. '$mod.p = $mod.ProcA;',
  23674. '$mod.p = $mod.FuncB;',
  23675. '$mod.p = $mod.FuncB;',
  23676. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  23677. '']));
  23678. end;
  23679. procedure TTestModule.TestProcType_PassProcToUntyped;
  23680. begin
  23681. StartProgram(false);
  23682. Add([
  23683. 'type',
  23684. ' TEvent = procedure of object;',
  23685. ' TFunc = function: longint;',
  23686. 'procedure DoIt(); varargs; begin end;',
  23687. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  23688. 'var',
  23689. ' Event: tevent;',
  23690. ' Func: TFunc;',
  23691. 'begin',
  23692. ' doit(event,func);',
  23693. ' dosome(event,event,event);',
  23694. ' dosome(func,func,func);',
  23695. '']);
  23696. ConvertProgram;
  23697. CheckSource('TestProcType_PassProcToUntyped',
  23698. LinesToStr([ // statements
  23699. 'this.DoIt = function () {',
  23700. '};',
  23701. 'this.DoSome = function (a, b, p) {',
  23702. '};',
  23703. 'this.Event = null;',
  23704. 'this.Func = null;',
  23705. '']),
  23706. LinesToStr([ // $mod.$main
  23707. '$mod.DoIt($mod.Event, $mod.Func);',
  23708. '$mod.DoSome($mod.Event, {',
  23709. ' p: $mod,',
  23710. ' get: function () {',
  23711. ' return this.p.Event;',
  23712. ' },',
  23713. ' set: function (v) {',
  23714. ' this.p.Event = v;',
  23715. ' }',
  23716. '}, $mod.Event);',
  23717. '$mod.DoSome($mod.Func, {',
  23718. ' p: $mod,',
  23719. ' get: function () {',
  23720. ' return this.p.Func;',
  23721. ' },',
  23722. ' set: function (v) {',
  23723. ' this.p.Func = v;',
  23724. ' }',
  23725. '}, $mod.Func);',
  23726. '']));
  23727. end;
  23728. procedure TTestModule.TestProcType_PassProcToArray;
  23729. begin
  23730. StartProgram(false);
  23731. Add([
  23732. 'type',
  23733. ' TFunc = function: longint;',
  23734. ' TArrFunc = array of TFunc;',
  23735. 'procedure DoIt(Arr: TArrFunc); begin end;',
  23736. 'function GetIt: longint; begin end;',
  23737. 'var',
  23738. ' Func: tfunc;',
  23739. 'begin',
  23740. ' doit([]);',
  23741. ' doit([@GetIt]);',
  23742. ' doit([Func]);',
  23743. '']);
  23744. ConvertProgram;
  23745. CheckSource('TestProcType_PassProcToArray',
  23746. LinesToStr([ // statements
  23747. 'this.DoIt = function (Arr) {',
  23748. '};',
  23749. 'this.GetIt = function () {',
  23750. ' var Result = 0;',
  23751. ' return Result;',
  23752. '};',
  23753. 'this.Func = null;',
  23754. '']),
  23755. LinesToStr([ // $mod.$main
  23756. '$mod.DoIt([]);',
  23757. '$mod.DoIt([$mod.GetIt]);',
  23758. '$mod.DoIt([$mod.Func]);',
  23759. '']));
  23760. end;
  23761. procedure TTestModule.TestPointer;
  23762. begin
  23763. StartProgram(false);
  23764. Add(['type',
  23765. ' TObject = class end;',
  23766. ' TClass = class of TObject;',
  23767. ' TArrInt = array of longint;',
  23768. 'const',
  23769. ' n = nil;',
  23770. 'var',
  23771. ' v: jsvalue;',
  23772. ' Obj: tobject;',
  23773. ' C: tclass;',
  23774. ' a: tarrint;',
  23775. ' p: Pointer = nil;',
  23776. ' s: string;',
  23777. 'begin',
  23778. ' p:=p;',
  23779. ' p:=nil;',
  23780. ' if p=nil then;',
  23781. ' if nil=p then;',
  23782. ' if Assigned(p) then;',
  23783. ' p:=Pointer(v);',
  23784. ' p:=obj;',
  23785. ' p:=c;',
  23786. ' p:=a;',
  23787. ' p:=tobject;',
  23788. ' obj:=TObject(p);',
  23789. ' c:=TClass(p);',
  23790. ' a:=TArrInt(p);',
  23791. ' p:=n;',
  23792. ' p:=Pointer(a);',
  23793. ' p:=pointer(s);',
  23794. ' s:=string(p);',
  23795. '']);
  23796. ConvertProgram;
  23797. CheckSource('TestPointer',
  23798. LinesToStr([ // statements
  23799. 'rtl.createClass($mod, "TObject", null, function () {',
  23800. ' this.$init = function () {',
  23801. ' };',
  23802. ' this.$final = function () {',
  23803. ' };',
  23804. '});',
  23805. 'this.n = null;',
  23806. 'this.v = undefined;',
  23807. 'this.Obj = null;',
  23808. 'this.C = null;',
  23809. 'this.a = [];',
  23810. 'this.p = null;',
  23811. 'this.s = "";',
  23812. '']),
  23813. LinesToStr([ // $mod.$main
  23814. '$mod.p = $mod.p;',
  23815. '$mod.p = null;',
  23816. 'if ($mod.p === null) ;',
  23817. 'if (null === $mod.p) ;',
  23818. 'if ($mod.p != null) ;',
  23819. '$mod.p = $mod.v;',
  23820. '$mod.p = $mod.Obj;',
  23821. '$mod.p = $mod.C;',
  23822. '$mod.p = $mod.a;',
  23823. '$mod.p = $mod.TObject;',
  23824. '$mod.Obj = $mod.p;',
  23825. '$mod.C = $mod.p;',
  23826. '$mod.a = $mod.p;',
  23827. '$mod.p = null;',
  23828. '$mod.p = $mod.a;',
  23829. '$mod.p = $mod.s;',
  23830. '$mod.s = $mod.p;',
  23831. '']));
  23832. end;
  23833. procedure TTestModule.TestPointer_Proc;
  23834. begin
  23835. StartProgram(false);
  23836. Add('type');
  23837. Add(' TObject = class');
  23838. Add(' procedure DoIt; virtual; abstract;');
  23839. Add(' end;');
  23840. Add('procedure DoSome; begin end;');
  23841. Add('var');
  23842. Add(' o: TObject;');
  23843. Add(' p: Pointer;');
  23844. Add('begin');
  23845. Add(' p:=@DoSome;');
  23846. Add(' p:[email protected];');
  23847. ConvertProgram;
  23848. CheckSource('TestPointer_Proc',
  23849. LinesToStr([ // statements
  23850. 'rtl.createClass($mod, "TObject", null, function () {',
  23851. ' this.$init = function () {',
  23852. ' };',
  23853. ' this.$final = function () {',
  23854. ' };',
  23855. '});',
  23856. 'this.DoSome = function () {',
  23857. '};',
  23858. 'this.o = null;',
  23859. 'this.p = null;',
  23860. '']),
  23861. LinesToStr([ // $mod.$main
  23862. '$mod.p = $mod.DoSome;',
  23863. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  23864. '']));
  23865. end;
  23866. procedure TTestModule.TestPointer_AssignRecordFail;
  23867. begin
  23868. StartProgram(false);
  23869. Add('type');
  23870. Add(' TRec = record end;');
  23871. Add('var');
  23872. Add(' p: Pointer;');
  23873. Add(' r: TRec;');
  23874. Add('begin');
  23875. Add(' p:=r;');
  23876. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  23877. nIncompatibleTypesGotExpected);
  23878. ConvertProgram;
  23879. end;
  23880. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  23881. begin
  23882. StartProgram(false);
  23883. Add('type');
  23884. Add(' TArr = array[boolean] of longint;');
  23885. Add('var');
  23886. Add(' p: Pointer;');
  23887. Add(' a: TArr;');
  23888. Add('begin');
  23889. Add(' p:=a;');
  23890. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  23891. nIncompatibleTypesGotExpected);
  23892. ConvertProgram;
  23893. end;
  23894. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  23895. begin
  23896. StartProgram(false);
  23897. Add([
  23898. 'procedure DoIt(args: array of jsvalue); begin end;',
  23899. 'procedure DoAll; varargs; begin end;',
  23900. 'var',
  23901. ' v: jsvalue;',
  23902. 'begin',
  23903. ' DoIt([pointer(v)]);',
  23904. ' DoAll(pointer(v));',
  23905. '']);
  23906. ConvertProgram;
  23907. CheckSource('TestPointer_TypeCastJSValueToPointer',
  23908. LinesToStr([ // statements
  23909. 'this.DoIt = function (args) {',
  23910. '};',
  23911. 'this.DoAll = function () {',
  23912. '};',
  23913. 'this.v = undefined;',
  23914. '']),
  23915. LinesToStr([ // $mod.$main
  23916. '$mod.DoIt([$mod.v]);',
  23917. '$mod.DoAll($mod.v);',
  23918. '']));
  23919. end;
  23920. procedure TTestModule.TestPointer_NonRecordFail;
  23921. begin
  23922. StartProgram(false);
  23923. Add([
  23924. 'type',
  23925. ' p = ^longint;',
  23926. 'begin',
  23927. '']);
  23928. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  23929. ConvertProgram;
  23930. end;
  23931. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  23932. begin
  23933. StartProgram(false);
  23934. Add([
  23935. 'procedure DoIt(p: ^longint); begin end;',
  23936. 'begin',
  23937. '']);
  23938. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  23939. ConvertProgram;
  23940. end;
  23941. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  23942. begin
  23943. StartProgram(false);
  23944. Add([
  23945. 'var p: ^longint;',
  23946. 'begin',
  23947. '']);
  23948. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  23949. ConvertProgram;
  23950. end;
  23951. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  23952. begin
  23953. StartProgram(false);
  23954. Add([
  23955. 'function DoIt: ^longint; begin end;',
  23956. 'begin',
  23957. '']);
  23958. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  23959. ConvertProgram;
  23960. end;
  23961. procedure TTestModule.TestPointer_AddrOperatorFail;
  23962. begin
  23963. StartProgram(false);
  23964. Add([
  23965. 'var i: longint;',
  23966. 'begin',
  23967. ' if @i=nil then ;',
  23968. '']);
  23969. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  23970. ConvertProgram;
  23971. end;
  23972. procedure TTestModule.TestPointer_ArrayParamsFail;
  23973. begin
  23974. StartProgram(false);
  23975. Add([
  23976. 'var',
  23977. ' p: Pointer;',
  23978. 'begin',
  23979. ' p:=p[1];',
  23980. '']);
  23981. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  23982. ConvertProgram;
  23983. end;
  23984. procedure TTestModule.TestPointer_PointerAddFail;
  23985. begin
  23986. StartProgram(false);
  23987. Add([
  23988. 'var',
  23989. ' p: Pointer;',
  23990. 'begin',
  23991. ' p:=p+1;',
  23992. '']);
  23993. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  23994. ConvertProgram;
  23995. end;
  23996. procedure TTestModule.TestPointer_IncPointerFail;
  23997. begin
  23998. StartProgram(false);
  23999. Add([
  24000. 'var',
  24001. ' p: Pointer;',
  24002. 'begin',
  24003. ' inc(p,1);',
  24004. '']);
  24005. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  24006. nIncompatibleTypeArgNo);
  24007. ConvertProgram;
  24008. end;
  24009. procedure TTestModule.TestPointer_Record;
  24010. begin
  24011. StartProgram(false);
  24012. Add([
  24013. 'type',
  24014. ' TRec = record x: longint; end;',
  24015. ' PRec = ^TRec;',
  24016. 'var',
  24017. ' r: TRec;',
  24018. ' p: PRec;',
  24019. ' q: ^TRec;',
  24020. ' Ptr: pointer;',
  24021. 'begin',
  24022. ' new(p);',
  24023. ' p:=@r;',
  24024. ' r:=p^;',
  24025. ' r.x:=p^.x;',
  24026. ' p^.x:=r.x;',
  24027. ' if p^.x=3 then ;',
  24028. ' if 4=p^.x then ;',
  24029. ' dispose(p);',
  24030. ' new(q);',
  24031. ' dispose(q);',
  24032. ' Ptr:=p;',
  24033. ' p:=PRec(ptr);',
  24034. '']);
  24035. ConvertProgram;
  24036. CheckSource('TestPointer_Record',
  24037. LinesToStr([ // statements
  24038. 'rtl.recNewT($mod, "TRec", function () {',
  24039. ' this.x = 0;',
  24040. ' this.$eq = function (b) {',
  24041. ' return this.x === b.x;',
  24042. ' };',
  24043. ' this.$assign = function (s) {',
  24044. ' this.x = s.x;',
  24045. ' return this;',
  24046. ' };',
  24047. '});',
  24048. 'this.r = $mod.TRec.$new();',
  24049. 'this.p = null;',
  24050. 'this.q = null;',
  24051. 'this.Ptr = null;',
  24052. '']),
  24053. LinesToStr([ // $mod.$main
  24054. '$mod.p = $mod.TRec.$new();',
  24055. '$mod.p = $mod.r;',
  24056. '$mod.r.$assign($mod.p);',
  24057. '$mod.r.x = $mod.p.x;',
  24058. '$mod.p.x = $mod.r.x;',
  24059. 'if ($mod.p.x === 3) ;',
  24060. 'if (4 === $mod.p.x) ;',
  24061. '$mod.p = null;',
  24062. '$mod.q = $mod.TRec.$new();',
  24063. '$mod.q = null;',
  24064. '$mod.Ptr = $mod.p;',
  24065. '$mod.p = $mod.Ptr;',
  24066. '']));
  24067. end;
  24068. procedure TTestModule.TestPointer_RecordArg;
  24069. begin
  24070. StartProgram(false);
  24071. Add([
  24072. '{$modeswitch autoderef}',
  24073. 'type',
  24074. ' TRec = record x: longint; end;',
  24075. ' PRec = ^TRec;',
  24076. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  24077. 'begin',
  24078. ' a.x:=a.x;',
  24079. ' a^.x:=a^.x;',
  24080. ' with a^ do',
  24081. ' x:=x;',
  24082. 'end;',
  24083. 'function GetIt(p: PRec): PRec;',
  24084. 'begin',
  24085. ' p.x:=p.x;',
  24086. ' p^.x:=p^.x;',
  24087. ' with p^ do',
  24088. ' x:=x;',
  24089. 'end;',
  24090. 'var',
  24091. ' r: TRec;',
  24092. ' p: PRec;',
  24093. 'begin',
  24094. ' p:=GetIt(p);',
  24095. ' p^:=GetIt(@r)^;',
  24096. ' DoIt(p,p,p);',
  24097. ' DoIt(@r,p,p);',
  24098. '']);
  24099. ConvertProgram;
  24100. CheckSource('TestPointer_Record',
  24101. LinesToStr([ // statements
  24102. 'rtl.recNewT($mod, "TRec", function () {',
  24103. ' this.x = 0;',
  24104. ' this.$eq = function (b) {',
  24105. ' return this.x === b.x;',
  24106. ' };',
  24107. ' this.$assign = function (s) {',
  24108. ' this.x = s.x;',
  24109. ' return this;',
  24110. ' };',
  24111. '});',
  24112. 'this.DoIt = function (a, b, c) {',
  24113. ' var Result = $mod.TRec.$new();',
  24114. ' a.x = a.x;',
  24115. ' a.x = a.x;',
  24116. ' a.x = a.x;',
  24117. ' return Result;',
  24118. '};',
  24119. 'this.GetIt = function (p) {',
  24120. ' var Result = null;',
  24121. ' p.x = p.x;',
  24122. ' p.x = p.x;',
  24123. ' p.x = p.x;',
  24124. ' return Result;',
  24125. '};',
  24126. 'this.r = $mod.TRec.$new();',
  24127. 'this.p = null;',
  24128. '']),
  24129. LinesToStr([ // $mod.$main
  24130. '$mod.p = $mod.GetIt($mod.p);',
  24131. '$mod.p.$assign($mod.GetIt($mod.r));',
  24132. '$mod.DoIt($mod.p, {',
  24133. ' p: $mod,',
  24134. ' get: function () {',
  24135. ' return this.p.p;',
  24136. ' },',
  24137. ' set: function (v) {',
  24138. ' this.p.p = v;',
  24139. ' }',
  24140. '}, {',
  24141. ' p: $mod,',
  24142. ' get: function () {',
  24143. ' return this.p.p;',
  24144. ' },',
  24145. ' set: function (v) {',
  24146. ' this.p.p = v;',
  24147. ' }',
  24148. '});',
  24149. '$mod.DoIt($mod.r, {',
  24150. ' p: $mod,',
  24151. ' get: function () {',
  24152. ' return this.p.p;',
  24153. ' },',
  24154. ' set: function (v) {',
  24155. ' this.p.p = v;',
  24156. ' }',
  24157. '}, {',
  24158. ' p: $mod,',
  24159. ' get: function () {',
  24160. ' return this.p.p;',
  24161. ' },',
  24162. ' set: function (v) {',
  24163. ' this.p.p = v;',
  24164. ' }',
  24165. '});',
  24166. '']));
  24167. end;
  24168. procedure TTestModule.TestJSValue_AssignToJSValue;
  24169. begin
  24170. StartProgram(false);
  24171. Add('var');
  24172. Add(' v: jsvalue;');
  24173. Add(' i: longint;');
  24174. Add(' s: string;');
  24175. Add(' b: boolean;');
  24176. Add(' d: double;');
  24177. Add(' p: pointer;');
  24178. Add('begin');
  24179. Add(' v:=v;');
  24180. Add(' v:=1;');
  24181. Add(' v:=i;');
  24182. Add(' v:='''';');
  24183. Add(' v:=''c'';');
  24184. Add(' v:=''foo'';');
  24185. Add(' v:=s;');
  24186. Add(' v:=false;');
  24187. Add(' v:=true;');
  24188. Add(' v:=b;');
  24189. Add(' v:=0.1;');
  24190. Add(' v:=d;');
  24191. Add(' v:=nil;');
  24192. Add(' v:=p;');
  24193. ConvertProgram;
  24194. CheckSource('TestJSValue_AssignToJSValue',
  24195. LinesToStr([ // statements
  24196. 'this.v = undefined;',
  24197. 'this.i = 0;',
  24198. 'this.s = "";',
  24199. 'this.b = false;',
  24200. 'this.d = 0.0;',
  24201. 'this.p = null;',
  24202. '']),
  24203. LinesToStr([ // $mod.$main
  24204. '$mod.v = $mod.v;',
  24205. '$mod.v = 1;',
  24206. '$mod.v = $mod.i;',
  24207. '$mod.v = "";',
  24208. '$mod.v = "c";',
  24209. '$mod.v = "foo";',
  24210. '$mod.v = $mod.s;',
  24211. '$mod.v = false;',
  24212. '$mod.v = true;',
  24213. '$mod.v = $mod.b;',
  24214. '$mod.v = 0.1;',
  24215. '$mod.v = $mod.d;',
  24216. '$mod.v = null;',
  24217. '$mod.v = $mod.p;',
  24218. '']));
  24219. end;
  24220. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  24221. begin
  24222. StartProgram(false);
  24223. Add('type');
  24224. Add(' integer = longint;');
  24225. Add(' TYesNo = boolean;');
  24226. Add(' TFloat = double;');
  24227. Add(' TCaption = string;');
  24228. Add(' TChar = char;');
  24229. Add('var');
  24230. Add(' v: jsvalue;');
  24231. Add(' i: integer;');
  24232. Add(' s: TCaption;');
  24233. Add(' b: TYesNo;');
  24234. Add(' d: TFloat;');
  24235. Add(' c: char;');
  24236. Add('begin');
  24237. Add(' i:=longint(v);');
  24238. Add(' i:=integer(v);');
  24239. Add(' s:=string(v);');
  24240. Add(' s:=TCaption(v);');
  24241. Add(' b:=boolean(v);');
  24242. Add(' b:=TYesNo(v);');
  24243. Add(' d:=double(v);');
  24244. Add(' d:=TFloat(v);');
  24245. Add(' c:=char(v);');
  24246. Add(' c:=TChar(v);');
  24247. ConvertProgram;
  24248. CheckSource('TestJSValue_TypeCastToBaseType',
  24249. LinesToStr([ // statements
  24250. 'this.v = undefined;',
  24251. 'this.i = 0;',
  24252. 'this.s = "";',
  24253. 'this.b = false;',
  24254. 'this.d = 0.0;',
  24255. 'this.c = "";',
  24256. '']),
  24257. LinesToStr([ // $mod.$main
  24258. '$mod.i = Math.floor($mod.v);',
  24259. '$mod.i = Math.floor($mod.v);',
  24260. '$mod.s = "" + $mod.v;',
  24261. '$mod.s = "" + $mod.v;',
  24262. '$mod.b = !($mod.v == false);',
  24263. '$mod.b = !($mod.v == false);',
  24264. '$mod.d = rtl.getNumber($mod.v);',
  24265. '$mod.d = rtl.getNumber($mod.v);',
  24266. '$mod.c = rtl.getChar($mod.v);',
  24267. '$mod.c = rtl.getChar($mod.v);',
  24268. '']));
  24269. end;
  24270. procedure TTestModule.TestJSValue_TypecastToJSValue;
  24271. begin
  24272. StartProgram(false);
  24273. Add([
  24274. 'type',
  24275. ' TArr = array of word;',
  24276. ' TRec = record end;',
  24277. ' TSet = set of boolean;',
  24278. 'procedure Fly(v: jsvalue);',
  24279. 'begin',
  24280. 'end;',
  24281. 'var',
  24282. ' a: TArr;',
  24283. ' r: TRec;',
  24284. ' s: TSet;',
  24285. 'begin',
  24286. ' Fly(jsvalue(a));',
  24287. ' Fly(jsvalue(r));',
  24288. ' Fly(jsvalue(s));',
  24289. '']);
  24290. ConvertProgram;
  24291. CheckSource('TestJSValue_TypecastToJSValue',
  24292. LinesToStr([ // statements
  24293. 'rtl.recNewT($mod, "TRec", function () {',
  24294. ' this.$eq = function (b) {',
  24295. ' return true;',
  24296. ' };',
  24297. ' this.$assign = function (s) {',
  24298. ' return this;',
  24299. ' };',
  24300. '});',
  24301. 'this.Fly = function (v) {',
  24302. '};',
  24303. 'this.a = [];',
  24304. 'this.r = $mod.TRec.$new();',
  24305. 'this.s = {};',
  24306. '']),
  24307. LinesToStr([ // $mod.$main
  24308. '$mod.Fly($mod.a);',
  24309. '$mod.Fly($mod.r);',
  24310. '$mod.Fly($mod.s);',
  24311. '']));
  24312. end;
  24313. procedure TTestModule.TestJSValue_Equal;
  24314. begin
  24315. StartProgram(false);
  24316. Add('type');
  24317. Add(' integer = longint;');
  24318. Add(' TYesNo = boolean;');
  24319. Add(' TFloat = double;');
  24320. Add(' TCaption = string;');
  24321. Add(' TChar = char;');
  24322. Add(' TMulti = JSValue;');
  24323. Add('var');
  24324. Add(' v: jsvalue;');
  24325. Add(' i: integer;');
  24326. Add(' s: TCaption;');
  24327. Add(' b: TYesNo;');
  24328. Add(' d: TFloat;');
  24329. Add(' c: char;');
  24330. Add(' m: TMulti;');
  24331. Add('begin');
  24332. Add(' b:=v=v;');
  24333. Add(' b:=v<>v;');
  24334. Add(' b:=v=1;');
  24335. Add(' b:=v<>1;');
  24336. Add(' b:=2=v;');
  24337. Add(' b:=2<>v;');
  24338. Add(' b:=v=i;');
  24339. Add(' b:=i=v;');
  24340. Add(' b:=v=nil;');
  24341. Add(' b:=nil=v;');
  24342. Add(' b:=v=false;');
  24343. Add(' b:=true=v;');
  24344. Add(' b:=v=b;');
  24345. Add(' b:=b=v;');
  24346. Add(' b:=v=s;');
  24347. Add(' b:=s=v;');
  24348. Add(' b:=v=''foo'';');
  24349. Add(' b:=''''=v;');
  24350. Add(' b:=v=d;');
  24351. Add(' b:=d=v;');
  24352. Add(' b:=v=3.4;');
  24353. Add(' b:=5.6=v;');
  24354. Add(' b:=v=c;');
  24355. Add(' b:=c=v;');
  24356. Add(' b:=m=m;');
  24357. Add(' b:=v=m;');
  24358. Add(' b:=m=v;');
  24359. ConvertProgram;
  24360. CheckSource('TestJSValue_Equal',
  24361. LinesToStr([ // statements
  24362. 'this.v = undefined;',
  24363. 'this.i = 0;',
  24364. 'this.s = "";',
  24365. 'this.b = false;',
  24366. 'this.d = 0.0;',
  24367. 'this.c = "";',
  24368. 'this.m = undefined;',
  24369. '']),
  24370. LinesToStr([ // $mod.$main
  24371. '$mod.b = $mod.v == $mod.v;',
  24372. '$mod.b = $mod.v != $mod.v;',
  24373. '$mod.b = $mod.v == 1;',
  24374. '$mod.b = $mod.v != 1;',
  24375. '$mod.b = 2 == $mod.v;',
  24376. '$mod.b = 2 != $mod.v;',
  24377. '$mod.b = $mod.v == $mod.i;',
  24378. '$mod.b = $mod.i == $mod.v;',
  24379. '$mod.b = $mod.v == null;',
  24380. '$mod.b = null == $mod.v;',
  24381. '$mod.b = $mod.v == false;',
  24382. '$mod.b = true == $mod.v;',
  24383. '$mod.b = $mod.v == $mod.b;',
  24384. '$mod.b = $mod.b == $mod.v;',
  24385. '$mod.b = $mod.v == $mod.s;',
  24386. '$mod.b = $mod.s == $mod.v;',
  24387. '$mod.b = $mod.v == "foo";',
  24388. '$mod.b = "" == $mod.v;',
  24389. '$mod.b = $mod.v == $mod.d;',
  24390. '$mod.b = $mod.d == $mod.v;',
  24391. '$mod.b = $mod.v == 3.4;',
  24392. '$mod.b = 5.6 == $mod.v;',
  24393. '$mod.b = $mod.v == $mod.c;',
  24394. '$mod.b = $mod.c == $mod.v;',
  24395. '$mod.b = $mod.m == $mod.m;',
  24396. '$mod.b = $mod.v == $mod.m;',
  24397. '$mod.b = $mod.m == $mod.v;',
  24398. '']));
  24399. end;
  24400. procedure TTestModule.TestJSValue_If;
  24401. begin
  24402. StartProgram(false);
  24403. Add([
  24404. 'var',
  24405. ' v: jsvalue;',
  24406. 'begin',
  24407. ' if v then ;',
  24408. ' while v do ;',
  24409. ' repeat until v;',
  24410. '']);
  24411. ConvertProgram;
  24412. CheckSource('TestJSValue_If',
  24413. LinesToStr([ // statements
  24414. 'this.v = undefined;',
  24415. '']),
  24416. LinesToStr([ // $mod.$main
  24417. 'if ($mod.v) ;',
  24418. 'while($mod.v){',
  24419. '};',
  24420. 'do{',
  24421. '} while(!$mod.v);',
  24422. '']));
  24423. end;
  24424. procedure TTestModule.TestJSValue_Not;
  24425. begin
  24426. StartProgram(false);
  24427. Add([
  24428. 'var',
  24429. ' v: jsvalue;',
  24430. ' b: boolean;',
  24431. 'begin',
  24432. ' b:=not v;',
  24433. ' if not v then ;',
  24434. ' while not v do ;',
  24435. ' repeat until not v;',
  24436. '']);
  24437. ConvertProgram;
  24438. CheckSource('TestJSValue_If',
  24439. LinesToStr([ // statements
  24440. 'this.v = undefined;',
  24441. 'this.b = false;',
  24442. '']),
  24443. LinesToStr([ // $mod.$main
  24444. '$mod.b=!$mod.v;',
  24445. 'if (!$mod.v) ;',
  24446. 'while(!$mod.v){',
  24447. '};',
  24448. 'do{',
  24449. '} while($mod.v);',
  24450. '']));
  24451. end;
  24452. procedure TTestModule.TestJSValue_Enum;
  24453. begin
  24454. StartProgram(false);
  24455. Add('type');
  24456. Add(' TColor = (red, blue);');
  24457. Add(' TRedBlue = TColor;');
  24458. Add('var');
  24459. Add(' v: jsvalue;');
  24460. Add(' e: TColor;');
  24461. Add('begin');
  24462. Add(' v:=e;');
  24463. Add(' v:=TColor(e);');
  24464. Add(' v:=TRedBlue(e);');
  24465. Add(' e:=TColor(v);');
  24466. Add(' e:=TRedBlue(v);');
  24467. ConvertProgram;
  24468. CheckSource('TestJSValue_Enum',
  24469. LinesToStr([ // statements
  24470. 'this.TColor = {',
  24471. ' "0": "red",',
  24472. ' red: 0,',
  24473. ' "1": "blue",',
  24474. ' blue: 1',
  24475. '};',
  24476. 'this.v = undefined;',
  24477. 'this.e = 0;',
  24478. '']),
  24479. LinesToStr([ // $mod.$main
  24480. '$mod.v = $mod.e;',
  24481. '$mod.v = $mod.e;',
  24482. '$mod.v = $mod.e;',
  24483. '$mod.e = $mod.v;',
  24484. '$mod.e = $mod.v;',
  24485. '']));
  24486. end;
  24487. procedure TTestModule.TestJSValue_ClassInstance;
  24488. begin
  24489. StartProgram(false);
  24490. Add([
  24491. 'type',
  24492. ' TObject = class',
  24493. ' end;',
  24494. ' TBirdObject = TObject;',
  24495. 'var',
  24496. ' v: jsvalue;',
  24497. ' o: TObject;',
  24498. 'begin',
  24499. ' v:=o;',
  24500. ' v:=TObject(o);',
  24501. ' v:=TBirdObject(o);',
  24502. ' o:=TObject(v);',
  24503. ' o:=TBirdObject(v);',
  24504. ' if v is TObject then ;',
  24505. '']);
  24506. ConvertProgram;
  24507. CheckSource('TestJSValue_ClassInstance',
  24508. LinesToStr([ // statements
  24509. 'rtl.createClass($mod, "TObject", null, function () {',
  24510. ' this.$init = function () {',
  24511. ' };',
  24512. ' this.$final = function () {',
  24513. ' };',
  24514. '});',
  24515. 'this.v = undefined;',
  24516. 'this.o = null;',
  24517. '']),
  24518. LinesToStr([ // $mod.$main
  24519. '$mod.v = $mod.o;',
  24520. '$mod.v = $mod.o;',
  24521. '$mod.v = $mod.o;',
  24522. '$mod.o = rtl.getObject($mod.v);',
  24523. '$mod.o = rtl.getObject($mod.v);',
  24524. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  24525. '']));
  24526. end;
  24527. procedure TTestModule.TestJSValue_ClassOf;
  24528. begin
  24529. StartProgram(false);
  24530. Add([
  24531. 'type',
  24532. ' TClass = class of TObject;',
  24533. ' TObject = class',
  24534. ' end;',
  24535. ' TBirds = class of TBird;',
  24536. ' TBird = class(TObject) end;',
  24537. 'var',
  24538. ' v: jsvalue;',
  24539. ' c: TClass;',
  24540. 'begin',
  24541. ' v:=c;',
  24542. ' v:=TObject;',
  24543. ' v:=TClass(c);',
  24544. ' v:=TBirds(c);',
  24545. ' c:=TClass(v);',
  24546. ' c:=TBirds(v);',
  24547. ' if v is TClass then ;',
  24548. '']);
  24549. ConvertProgram;
  24550. CheckSource('TestJSValue_ClassOf',
  24551. LinesToStr([ // statements
  24552. 'rtl.createClass($mod, "TObject", null, function () {',
  24553. ' this.$init = function () {',
  24554. ' };',
  24555. ' this.$final = function () {',
  24556. ' };',
  24557. '});',
  24558. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  24559. '});',
  24560. 'this.v = undefined;',
  24561. 'this.c = null;',
  24562. '']),
  24563. LinesToStr([ // $mod.$main
  24564. '$mod.v = $mod.c;',
  24565. '$mod.v = $mod.TObject;',
  24566. '$mod.v = $mod.c;',
  24567. '$mod.v = $mod.c;',
  24568. '$mod.c = rtl.getObject($mod.v);',
  24569. '$mod.c = rtl.getObject($mod.v);',
  24570. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  24571. '']));
  24572. end;
  24573. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  24574. begin
  24575. StartProgram(false);
  24576. Add([
  24577. 'type',
  24578. ' integer = longint;',
  24579. ' TArray = array of JSValue;',
  24580. ' TArrgh = tarray;',
  24581. ' TArrInt = array of integer;',
  24582. 'var',
  24583. ' v: jsvalue;',
  24584. ' TheArray: tarray = (1,''2'');',
  24585. ' Arr: tarrgh;',
  24586. ' i: integer;',
  24587. ' ArrInt: tarrint;',
  24588. 'begin',
  24589. ' arr:=thearray;',
  24590. ' thearray:=arr;',
  24591. ' setlength(arr,2);',
  24592. ' setlength(thearray,3);',
  24593. ' arr[4]:=v;',
  24594. ' arr[5]:=length(thearray);',
  24595. ' arr[6]:=nil;',
  24596. ' arr[7]:=thearray[8];',
  24597. ' arr[low(arr)]:=high(thearray);',
  24598. ' arr:=arrint;',
  24599. ' arrInt:=tarrint(arr);',
  24600. ' if TheArray = nil then ;',
  24601. ' if nil = TheArray then ;',
  24602. ' if TheArray <> nil then ;',
  24603. ' if nil <> TheArray then ;',
  24604. '']);
  24605. ConvertProgram;
  24606. CheckSource('TestJSValue_ArrayOfJSValue',
  24607. LinesToStr([ // statements
  24608. 'this.v = undefined;',
  24609. 'this.TheArray = [1, "2"];',
  24610. 'this.Arr = [];',
  24611. 'this.i = 0;',
  24612. 'this.ArrInt = [];',
  24613. '']),
  24614. LinesToStr([ // $mod.$main
  24615. '$mod.Arr = $mod.TheArray;',
  24616. '$mod.TheArray = $mod.Arr;',
  24617. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  24618. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  24619. '$mod.Arr[4] = $mod.v;',
  24620. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  24621. '$mod.Arr[6] = null;',
  24622. '$mod.Arr[7] = $mod.TheArray[8];',
  24623. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  24624. '$mod.Arr = $mod.ArrInt;',
  24625. '$mod.ArrInt = $mod.Arr;',
  24626. 'if (rtl.length($mod.TheArray) === 0) ;',
  24627. 'if (rtl.length($mod.TheArray) === 0) ;',
  24628. 'if (rtl.length($mod.TheArray) > 0) ;',
  24629. 'if (rtl.length($mod.TheArray) > 0) ;',
  24630. '']));
  24631. end;
  24632. procedure TTestModule.TestJSValue_ArrayLit;
  24633. begin
  24634. StartProgram(false);
  24635. Add([
  24636. 'type',
  24637. ' TFlag = (big,small);',
  24638. ' TArray = array of JSValue;',
  24639. ' TObject = class end;',
  24640. ' TClass = class of TObject;',
  24641. 'var',
  24642. ' v: jsvalue;',
  24643. ' a: TArray;',
  24644. ' o: TObject;',
  24645. 'begin',
  24646. ' a:=[];',
  24647. ' a:=[1];',
  24648. ' a:=[1,2];',
  24649. ' a:=[big];',
  24650. ' a:=[1,big];',
  24651. ' a:=[o,nil];',
  24652. '']);
  24653. ConvertProgram;
  24654. CheckSource('TestJSValue_ArrayLit',
  24655. LinesToStr([ // statements
  24656. 'this.TFlag = {',
  24657. ' "0": "big",',
  24658. ' big: 0,',
  24659. ' "1": "small",',
  24660. ' small: 1',
  24661. '};',
  24662. 'rtl.createClass($mod, "TObject", null, function () {',
  24663. ' this.$init = function () {',
  24664. ' };',
  24665. ' this.$final = function () {',
  24666. ' };',
  24667. '});',
  24668. 'this.v = undefined;',
  24669. 'this.a = [];',
  24670. 'this.o = null;',
  24671. '']),
  24672. LinesToStr([ // $mod.$main
  24673. '$mod.a = [];',
  24674. '$mod.a = [1];',
  24675. '$mod.a = [1, 2];',
  24676. '$mod.a = [$mod.TFlag.big];',
  24677. '$mod.a = [1, $mod.TFlag.big];',
  24678. '$mod.a = [$mod.o, null];',
  24679. '']));
  24680. end;
  24681. procedure TTestModule.TestJSValue_Params;
  24682. begin
  24683. StartProgram(false);
  24684. Add('type');
  24685. Add(' integer = longint;');
  24686. Add(' TYesNo = boolean;');
  24687. Add(' TFloat = double;');
  24688. Add(' TCaption = string;');
  24689. Add(' TChar = char;');
  24690. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  24691. Add('var');
  24692. Add(' l: jsvalue;');
  24693. Add('begin');
  24694. Add(' a:=a;');
  24695. Add(' l:=b;');
  24696. Add(' c:=c;');
  24697. Add(' d:=d;');
  24698. Add(' Result:=l;');
  24699. Add('end;');
  24700. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  24701. Add('var');
  24702. Add(' v: jsvalue;');
  24703. Add(' i: integer;');
  24704. Add(' b: TYesNo;');
  24705. Add(' d: TFloat;');
  24706. Add(' s: TCaption;');
  24707. Add(' c: TChar;');
  24708. Add('begin');
  24709. Add(' v:=doit(v,v,v,v);');
  24710. Add(' i:=integer(dosome(i,i));');
  24711. Add(' b:=TYesNo(dosome(b,b));');
  24712. Add(' d:=TFloat(dosome(d,d));');
  24713. Add(' s:=TCaption(dosome(s,s));');
  24714. Add(' c:=TChar(dosome(c,c));');
  24715. ConvertProgram;
  24716. CheckSource('TestJSValue_Params',
  24717. LinesToStr([ // statements
  24718. 'this.DoIt = function (a, b, c, d) {',
  24719. ' var Result = undefined;',
  24720. ' var l = undefined;',
  24721. ' a = a;',
  24722. ' l = b;',
  24723. ' c.set(c.get());',
  24724. ' d.set(d.get());',
  24725. ' Result = l;',
  24726. ' return Result;',
  24727. '};',
  24728. 'this.DoSome = function (a, b) {',
  24729. ' var Result = undefined;',
  24730. ' return Result;',
  24731. '};',
  24732. 'this.v = undefined;',
  24733. 'this.i = 0;',
  24734. 'this.b = false;',
  24735. 'this.d = 0.0;',
  24736. 'this.s = "";',
  24737. 'this.c = "";',
  24738. '']),
  24739. LinesToStr([ // $mod.$main
  24740. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  24741. ' p: $mod,',
  24742. ' get: function () {',
  24743. ' return this.p.v;',
  24744. ' },',
  24745. ' set: function (v) {',
  24746. ' this.p.v = v;',
  24747. ' }',
  24748. '}, {',
  24749. ' p: $mod,',
  24750. ' get: function () {',
  24751. ' return this.p.v;',
  24752. ' },',
  24753. ' set: function (v) {',
  24754. ' this.p.v = v;',
  24755. ' }',
  24756. '});',
  24757. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  24758. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  24759. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  24760. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  24761. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  24762. '']));
  24763. end;
  24764. procedure TTestModule.TestJSValue_UntypedParam;
  24765. begin
  24766. StartProgram(false);
  24767. Add('function DoIt(const a; var b; out c): jsvalue;');
  24768. Add('begin');
  24769. Add(' Result:=a;');
  24770. Add(' Result:=b;');
  24771. Add(' Result:=c;');
  24772. Add(' b:=Result;');
  24773. Add(' c:=Result;');
  24774. Add('end;');
  24775. Add('var i: longint;');
  24776. Add('begin');
  24777. Add(' doit(i,i,i);');
  24778. ConvertProgram;
  24779. CheckSource('TestJSValue_UntypedParam',
  24780. LinesToStr([ // statements
  24781. 'this.DoIt = function (a, b, c) {',
  24782. ' var Result = undefined;',
  24783. ' Result = a;',
  24784. ' Result = b.get();',
  24785. ' Result = c.get();',
  24786. ' b.set(Result);',
  24787. ' c.set(Result);',
  24788. ' return Result;',
  24789. '};',
  24790. 'this.i = 0;',
  24791. '']),
  24792. LinesToStr([ // $mod.$main
  24793. '$mod.DoIt($mod.i, {',
  24794. ' p: $mod,',
  24795. ' get: function () {',
  24796. ' return this.p.i;',
  24797. ' },',
  24798. ' set: function (v) {',
  24799. ' this.p.i = v;',
  24800. ' }',
  24801. '}, {',
  24802. ' p: $mod,',
  24803. ' get: function () {',
  24804. ' return this.p.i;',
  24805. ' },',
  24806. ' set: function (v) {',
  24807. ' this.p.i = v;',
  24808. ' }',
  24809. '});',
  24810. '']));
  24811. end;
  24812. procedure TTestModule.TestJSValue_FuncResultType;
  24813. begin
  24814. StartProgram(false);
  24815. Add('type');
  24816. Add(' integer = longint;');
  24817. Add(' TJSValueArray = array of JSValue;');
  24818. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  24819. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  24820. Add('begin');
  24821. Add(' while Compare(P,aList[0])>0 do ;');
  24822. Add('end;');
  24823. Add('var');
  24824. Add(' Compare: TListSortCompare;');
  24825. Add(' V: JSValue;');
  24826. Add(' i: integer;');
  24827. Add('begin');
  24828. Add(' if Compare(V,V)>0 then ;');
  24829. Add(' if Compare(i,i)>1 then ;');
  24830. Add(' if Compare(nil,false)>2 then ;');
  24831. Add(' if Compare(1,true)>3 then ;');
  24832. ConvertProgram;
  24833. CheckSource('TestJSValue_UntypedParam',
  24834. LinesToStr([ // statements
  24835. 'this.Sort = function (P, aList, Compare) {',
  24836. ' while (Compare(P, aList[0]) > 0) {',
  24837. ' };',
  24838. '};',
  24839. 'this.Compare = null;',
  24840. 'this.V = undefined;',
  24841. 'this.i = 0;',
  24842. '']),
  24843. LinesToStr([ // $mod.$main
  24844. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  24845. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  24846. 'if ($mod.Compare(null, false) > 2) ;',
  24847. 'if ($mod.Compare(1, true) > 3) ;',
  24848. '']));
  24849. end;
  24850. procedure TTestModule.TestJSValue_ProcType_Assign;
  24851. begin
  24852. StartProgram(false);
  24853. Add('type');
  24854. Add(' integer = longint;');
  24855. Add(' TObject = class');
  24856. Add(' class function GetGlob: integer;');
  24857. Add(' function Getter: integer;');
  24858. Add(' end;');
  24859. Add('class function TObject.GetGlob: integer;');
  24860. Add('var v1: jsvalue;');
  24861. Add('begin');
  24862. Add(' v1:=@GetGlob;');
  24863. Add(' v1:[email protected];');
  24864. Add('end;');
  24865. Add('function TObject.Getter: integer;');
  24866. Add('var v2: jsvalue;');
  24867. Add('begin');
  24868. Add(' v2:=@Getter;');
  24869. Add(' v2:[email protected];');
  24870. Add(' v2:=@GetGlob;');
  24871. Add(' v2:[email protected];');
  24872. Add('end;');
  24873. Add('function GetIt(i: integer): integer;');
  24874. Add('var v3: jsvalue;');
  24875. Add('begin');
  24876. Add(' v3:=@GetIt;');
  24877. Add('end;');
  24878. Add('var');
  24879. Add(' V: JSValue;');
  24880. Add(' o: TObject;');
  24881. Add('begin');
  24882. Add(' v:=@GetIt;');
  24883. Add(' v:[email protected];');
  24884. Add(' v:[email protected];');
  24885. ConvertProgram;
  24886. CheckSource('TestJSValue_ProcType_Assign',
  24887. LinesToStr([ // statements
  24888. 'rtl.createClass($mod, "TObject", null, function () {',
  24889. ' this.$init = function () {',
  24890. ' };',
  24891. ' this.$final = function () {',
  24892. ' };',
  24893. ' this.GetGlob = function () {',
  24894. ' var Result = 0;',
  24895. ' var v1 = undefined;',
  24896. ' v1 = rtl.createCallback(this, "GetGlob");',
  24897. ' v1 = rtl.createCallback(this, "GetGlob");',
  24898. ' return Result;',
  24899. ' };',
  24900. ' this.Getter = function () {',
  24901. ' var Result = 0;',
  24902. ' var v2 = undefined;',
  24903. ' v2 = rtl.createCallback(this, "Getter");',
  24904. ' v2 = rtl.createCallback(this, "Getter");',
  24905. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  24906. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  24907. ' return Result;',
  24908. ' };',
  24909. '});',
  24910. 'this.GetIt = function (i) {',
  24911. ' var Result = 0;',
  24912. ' var v3 = undefined;',
  24913. ' v3 = $mod.GetIt;',
  24914. ' return Result;',
  24915. '};',
  24916. 'this.V = undefined;',
  24917. 'this.o = null;',
  24918. '']),
  24919. LinesToStr([ // $mod.$main
  24920. '$mod.V = $mod.GetIt;',
  24921. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  24922. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  24923. '']));
  24924. end;
  24925. procedure TTestModule.TestJSValue_ProcType_Equal;
  24926. begin
  24927. StartProgram(false);
  24928. Add('type');
  24929. Add(' integer = longint;');
  24930. Add(' TObject = class');
  24931. Add(' class function GetGlob: integer;');
  24932. Add(' function Getter: integer;');
  24933. Add(' end;');
  24934. Add('class function TObject.GetGlob: integer;');
  24935. Add('var v1: jsvalue;');
  24936. Add('begin');
  24937. Add(' if v1=@GetGlob then;');
  24938. Add(' if [email protected] then ;');
  24939. Add('end;');
  24940. Add('function TObject.Getter: integer;');
  24941. Add('var v2: jsvalue;');
  24942. Add('begin');
  24943. Add(' if v2=@Getter then;');
  24944. Add(' if [email protected] then ;');
  24945. Add(' if v2=@GetGlob then;');
  24946. Add(' if [email protected] then;');
  24947. Add('end;');
  24948. Add('function GetIt(i: integer): integer;');
  24949. Add('var v3: jsvalue;');
  24950. Add('begin');
  24951. Add(' if v3=@GetIt then;');
  24952. Add('end;');
  24953. Add('var');
  24954. Add(' V: JSValue;');
  24955. Add(' o: TObject;');
  24956. Add('begin');
  24957. Add(' if v=@GetIt then;');
  24958. Add(' if [email protected] then;');
  24959. Add(' if [email protected] then;');
  24960. Add(' if @GetIt=v then;');
  24961. Add(' if @o.Getter=v then;');
  24962. Add(' if @o.GetGlob=v then;');
  24963. ConvertProgram;
  24964. CheckSource('TestJSValue_ProcType_Equal',
  24965. LinesToStr([ // statements
  24966. 'rtl.createClass($mod, "TObject", null, function () {',
  24967. ' this.$init = function () {',
  24968. ' };',
  24969. ' this.$final = function () {',
  24970. ' };',
  24971. ' this.GetGlob = function () {',
  24972. ' var Result = 0;',
  24973. ' var v1 = undefined;',
  24974. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  24975. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  24976. ' return Result;',
  24977. ' };',
  24978. ' this.Getter = function () {',
  24979. ' var Result = 0;',
  24980. ' var v2 = undefined;',
  24981. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  24982. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  24983. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  24984. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  24985. ' return Result;',
  24986. ' };',
  24987. '});',
  24988. 'this.GetIt = function (i) {',
  24989. ' var Result = 0;',
  24990. ' var v3 = undefined;',
  24991. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  24992. ' return Result;',
  24993. '};',
  24994. 'this.V = undefined;',
  24995. 'this.o = null;',
  24996. '']),
  24997. LinesToStr([ // $mod.$main
  24998. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  24999. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  25000. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  25001. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  25002. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  25003. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  25004. '']));
  25005. end;
  25006. procedure TTestModule.TestJSValue_ProcType_Param;
  25007. begin
  25008. StartProgram(false);
  25009. Add([
  25010. 'type',
  25011. ' variant = jsvalue;',
  25012. ' TArrVariant = array of variant;',
  25013. ' TArrVar2 = TArrVariant;',
  25014. ' TFuncInt = function: longint;',
  25015. 'function GetIt: longint;',
  25016. 'begin',
  25017. 'end;',
  25018. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  25019. 'var v: variant;',
  25020. 'begin',
  25021. ' v:=arr[1];',
  25022. 'end;',
  25023. 'var s: string;',
  25024. 'begin',
  25025. ' DoIt(GetIt,[]);',
  25026. ' DoIt(@GetIt,[]);',
  25027. ' DoIt(1,[s,GetIt]);',
  25028. ' DoIt(1,[s,@GetIt]);',
  25029. '']);
  25030. ConvertProgram;
  25031. CheckSource('TestJSValue_ProcType_Param',
  25032. LinesToStr([ // statements
  25033. 'this.GetIt = function () {',
  25034. ' var Result = 0;',
  25035. ' return Result;',
  25036. '};',
  25037. 'this.DoIt = function (p, Arr) {',
  25038. ' var v = undefined;',
  25039. ' v = Arr[1];',
  25040. '};',
  25041. 'this.s = "";',
  25042. '']),
  25043. LinesToStr([ // $mod.$main
  25044. '$mod.DoIt($mod.GetIt(), []);',
  25045. '$mod.DoIt($mod.GetIt, []);',
  25046. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  25047. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  25048. '']));
  25049. end;
  25050. procedure TTestModule.TestJSValue_AssignToPointerFail;
  25051. begin
  25052. StartProgram(false);
  25053. Add([
  25054. 'var',
  25055. ' v: JSValue;',
  25056. ' p: Pointer;',
  25057. 'begin',
  25058. ' p:=v;',
  25059. '']);
  25060. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  25061. nIncompatibleTypesGotExpected);
  25062. ConvertProgram;
  25063. end;
  25064. procedure TTestModule.TestJSValue_OverloadDouble;
  25065. begin
  25066. StartProgram(false);
  25067. Add([
  25068. 'type',
  25069. ' integer = longint;',
  25070. ' tdatetime = double;',
  25071. 'procedure DoIt(d: double); begin end;',
  25072. 'procedure DoIt(v: jsvalue); begin end;',
  25073. 'var',
  25074. ' d: double;',
  25075. ' dt: tdatetime;',
  25076. ' i: integer;',
  25077. ' b: byte;',
  25078. ' shi: shortint;',
  25079. ' w: word;',
  25080. ' smi: smallint;',
  25081. ' lw: longword;',
  25082. ' li: longint;',
  25083. ' ni: nativeint;',
  25084. ' nu: nativeuint;',
  25085. 'begin',
  25086. ' DoIt(d);',
  25087. ' DoIt(dt);',
  25088. ' DoIt(i);',
  25089. ' DoIt(b);',
  25090. ' DoIt(shi);',
  25091. ' DoIt(w);',
  25092. ' DoIt(smi);',
  25093. ' DoIt(lw);',
  25094. ' DoIt(li);',
  25095. ' DoIt(ni);',
  25096. ' DoIt(nu);',
  25097. '']);
  25098. ConvertProgram;
  25099. CheckSource('TestJSValue_OverloadDouble',
  25100. LinesToStr([ // statements
  25101. 'this.DoIt = function (d) {',
  25102. '};',
  25103. 'this.DoIt$1 = function (v) {',
  25104. '};',
  25105. 'this.d = 0.0;',
  25106. 'this.dt = 0.0;',
  25107. 'this.i = 0;',
  25108. 'this.b = 0;',
  25109. 'this.shi = 0;',
  25110. 'this.w = 0;',
  25111. 'this.smi = 0;',
  25112. 'this.lw = 0;',
  25113. 'this.li = 0;',
  25114. 'this.ni = 0;',
  25115. 'this.nu = 0;',
  25116. '']),
  25117. LinesToStr([ // $mod.$main
  25118. '$mod.DoIt($mod.d);',
  25119. '$mod.DoIt($mod.dt);',
  25120. '$mod.DoIt$1($mod.i);',
  25121. '$mod.DoIt$1($mod.b);',
  25122. '$mod.DoIt$1($mod.shi);',
  25123. '$mod.DoIt$1($mod.w);',
  25124. '$mod.DoIt$1($mod.smi);',
  25125. '$mod.DoIt$1($mod.lw);',
  25126. '$mod.DoIt$1($mod.li);',
  25127. '$mod.DoIt$1($mod.ni);',
  25128. '$mod.DoIt$1($mod.nu);',
  25129. '']));
  25130. end;
  25131. procedure TTestModule.TestJSValue_OverloadNativeInt;
  25132. begin
  25133. StartProgram(false);
  25134. Add([
  25135. 'type',
  25136. ' integer = longint;',
  25137. ' int53 = nativeint;',
  25138. ' tdatetime = double;',
  25139. 'procedure DoIt(n: nativeint); begin end;',
  25140. 'procedure DoIt(v: jsvalue); begin end;',
  25141. 'var',
  25142. ' d: double;',
  25143. ' dt: tdatetime;',
  25144. ' i: integer;',
  25145. ' b: byte;',
  25146. ' shi: shortint;',
  25147. ' w: word;',
  25148. ' smi: smallint;',
  25149. ' lw: longword;',
  25150. ' li: longint;',
  25151. ' ni: nativeint;',
  25152. ' nu: nativeuint;',
  25153. 'begin',
  25154. ' DoIt(d);',
  25155. ' DoIt(dt);',
  25156. ' DoIt(i);',
  25157. ' DoIt(b);',
  25158. ' DoIt(shi);',
  25159. ' DoIt(w);',
  25160. ' DoIt(smi);',
  25161. ' DoIt(lw);',
  25162. ' DoIt(li);',
  25163. ' DoIt(ni);',
  25164. ' DoIt(nu);',
  25165. '']);
  25166. ConvertProgram;
  25167. CheckSource('TestJSValue_OverloadNativeInt',
  25168. LinesToStr([ // statements
  25169. 'this.DoIt = function (n) {',
  25170. '};',
  25171. 'this.DoIt$1 = function (v) {',
  25172. '};',
  25173. 'this.d = 0.0;',
  25174. 'this.dt = 0.0;',
  25175. 'this.i = 0;',
  25176. 'this.b = 0;',
  25177. 'this.shi = 0;',
  25178. 'this.w = 0;',
  25179. 'this.smi = 0;',
  25180. 'this.lw = 0;',
  25181. 'this.li = 0;',
  25182. 'this.ni = 0;',
  25183. 'this.nu = 0;',
  25184. '']),
  25185. LinesToStr([ // $mod.$main
  25186. '$mod.DoIt$1($mod.d);',
  25187. '$mod.DoIt$1($mod.dt);',
  25188. '$mod.DoIt($mod.i);',
  25189. '$mod.DoIt($mod.b);',
  25190. '$mod.DoIt($mod.shi);',
  25191. '$mod.DoIt($mod.w);',
  25192. '$mod.DoIt($mod.smi);',
  25193. '$mod.DoIt($mod.lw);',
  25194. '$mod.DoIt($mod.li);',
  25195. '$mod.DoIt($mod.ni);',
  25196. '$mod.DoIt($mod.nu);',
  25197. '']));
  25198. end;
  25199. procedure TTestModule.TestJSValue_OverloadWord;
  25200. begin
  25201. StartProgram(false);
  25202. Add([
  25203. 'type',
  25204. ' integer = longint;',
  25205. ' int53 = nativeint;',
  25206. ' tdatetime = double;',
  25207. 'procedure DoIt(w: word); begin end;',
  25208. 'procedure DoIt(v: jsvalue); begin end;',
  25209. 'var',
  25210. ' d: double;',
  25211. ' dt: tdatetime;',
  25212. ' i: integer;',
  25213. ' b: byte;',
  25214. ' shi: shortint;',
  25215. ' w: word;',
  25216. ' smi: smallint;',
  25217. ' lw: longword;',
  25218. ' li: longint;',
  25219. ' ni: nativeint;',
  25220. ' nu: nativeuint;',
  25221. 'begin',
  25222. ' DoIt(d);',
  25223. ' DoIt(dt);',
  25224. ' DoIt(i);',
  25225. ' DoIt(b);',
  25226. ' DoIt(shi);',
  25227. ' DoIt(w);',
  25228. ' DoIt(smi);',
  25229. ' DoIt(lw);',
  25230. ' DoIt(li);',
  25231. ' DoIt(ni);',
  25232. ' DoIt(nu);',
  25233. '']);
  25234. ConvertProgram;
  25235. CheckSource('TestJSValue_OverloadWord',
  25236. LinesToStr([ // statements
  25237. 'this.DoIt = function (w) {',
  25238. '};',
  25239. 'this.DoIt$1 = function (v) {',
  25240. '};',
  25241. 'this.d = 0.0;',
  25242. 'this.dt = 0.0;',
  25243. 'this.i = 0;',
  25244. 'this.b = 0;',
  25245. 'this.shi = 0;',
  25246. 'this.w = 0;',
  25247. 'this.smi = 0;',
  25248. 'this.lw = 0;',
  25249. 'this.li = 0;',
  25250. 'this.ni = 0;',
  25251. 'this.nu = 0;',
  25252. '']),
  25253. LinesToStr([ // $mod.$main
  25254. '$mod.DoIt$1($mod.d);',
  25255. '$mod.DoIt$1($mod.dt);',
  25256. '$mod.DoIt$1($mod.i);',
  25257. '$mod.DoIt($mod.b);',
  25258. '$mod.DoIt($mod.shi);',
  25259. '$mod.DoIt($mod.w);',
  25260. '$mod.DoIt$1($mod.smi);',
  25261. '$mod.DoIt$1($mod.lw);',
  25262. '$mod.DoIt$1($mod.li);',
  25263. '$mod.DoIt$1($mod.ni);',
  25264. '$mod.DoIt$1($mod.nu);',
  25265. '']));
  25266. end;
  25267. procedure TTestModule.TestJSValue_OverloadString;
  25268. begin
  25269. StartProgram(false);
  25270. Add([
  25271. 'type',
  25272. ' uni = string;',
  25273. ' WChar = char;',
  25274. 'procedure DoIt(s: string); begin end;',
  25275. 'procedure DoIt(v: jsvalue); begin end;',
  25276. 'var',
  25277. ' s: string;',
  25278. ' c: char;',
  25279. ' u: uni;',
  25280. 'begin',
  25281. ' DoIt(s);',
  25282. ' DoIt(c);',
  25283. ' DoIt(u);',
  25284. '']);
  25285. ConvertProgram;
  25286. CheckSource('TestJSValue_OverloadString',
  25287. LinesToStr([ // statements
  25288. 'this.DoIt = function (s) {',
  25289. '};',
  25290. 'this.DoIt$1 = function (v) {',
  25291. '};',
  25292. 'this.s = "";',
  25293. 'this.c = "";',
  25294. 'this.u = "";',
  25295. '']),
  25296. LinesToStr([ // $mod.$main
  25297. '$mod.DoIt($mod.s);',
  25298. '$mod.DoIt($mod.c);',
  25299. '$mod.DoIt($mod.u);',
  25300. '']));
  25301. end;
  25302. procedure TTestModule.TestJSValue_OverloadChar;
  25303. begin
  25304. StartProgram(false);
  25305. Add([
  25306. 'type',
  25307. ' uni = string;',
  25308. ' WChar = char;',
  25309. 'procedure DoIt(c: char); begin end;',
  25310. 'procedure DoIt(v: jsvalue); begin end;',
  25311. 'var',
  25312. ' s: string;',
  25313. ' c: char;',
  25314. ' u: uni;',
  25315. 'begin',
  25316. ' DoIt(s);',
  25317. ' DoIt(c);',
  25318. ' DoIt(u);',
  25319. '']);
  25320. ConvertProgram;
  25321. CheckSource('TestJSValue_OverloadChar',
  25322. LinesToStr([ // statements
  25323. 'this.DoIt = function (c) {',
  25324. '};',
  25325. 'this.DoIt$1 = function (v) {',
  25326. '};',
  25327. 'this.s = "";',
  25328. 'this.c = "";',
  25329. 'this.u = "";',
  25330. '']),
  25331. LinesToStr([ // $mod.$main
  25332. '$mod.DoIt$1($mod.s);',
  25333. '$mod.DoIt($mod.c);',
  25334. '$mod.DoIt$1($mod.u);',
  25335. '']));
  25336. end;
  25337. procedure TTestModule.TestJSValue_OverloadPointer;
  25338. begin
  25339. StartProgram(false);
  25340. Add([
  25341. 'type',
  25342. ' TObject = class end;',
  25343. 'procedure DoIt(p: pointer); begin end;',
  25344. 'procedure DoIt(v: jsvalue); begin end;',
  25345. 'var',
  25346. ' o: TObject;',
  25347. 'begin',
  25348. ' DoIt(o);',
  25349. '']);
  25350. ConvertProgram;
  25351. CheckSource('TestJSValue_OverloadPointer',
  25352. LinesToStr([ // statements
  25353. 'rtl.createClass($mod, "TObject", null, function () {',
  25354. ' this.$init = function () {',
  25355. ' };',
  25356. ' this.$final = function () {',
  25357. ' };',
  25358. '});',
  25359. 'this.DoIt = function (p) {',
  25360. '};',
  25361. 'this.DoIt$1 = function (v) {',
  25362. '};',
  25363. 'this.o = null;',
  25364. '']),
  25365. LinesToStr([ // $mod.$main
  25366. '$mod.DoIt($mod.o);',
  25367. '']));
  25368. end;
  25369. procedure TTestModule.TestJSValue_ForIn;
  25370. begin
  25371. StartProgram(false);
  25372. Add([
  25373. 'var',
  25374. ' v: JSValue;',
  25375. ' key: string;',
  25376. 'begin',
  25377. ' for key in v do begin',
  25378. ' if key=''abc'' then ;',
  25379. ' end;',
  25380. '']);
  25381. ConvertProgram;
  25382. CheckSource('TestJSValue_ForIn',
  25383. LinesToStr([ // statements
  25384. 'this.v = undefined;',
  25385. 'this.key = "";',
  25386. '']),
  25387. LinesToStr([ // $mod.$main
  25388. 'for ($mod.key in $mod.v) {',
  25389. ' if ($mod.key === "abc") ;',
  25390. '};',
  25391. '']));
  25392. end;
  25393. procedure TTestModule.TestRTTI_IntRange;
  25394. begin
  25395. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25396. StartProgram(false);
  25397. Add([
  25398. '{$modeswitch externalclass}',
  25399. 'type',
  25400. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  25401. ' end;',
  25402. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  25403. ' end;',
  25404. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  25405. ' TColor = type TGraphicsColor;',
  25406. 'var',
  25407. ' p: TTypeInfo;',
  25408. 'begin',
  25409. ' p:=typeinfo(TGraphicsColor);',
  25410. ' p:=typeinfo(TColor);',
  25411. '']);
  25412. ConvertProgram;
  25413. CheckSource('TestRTTI_IntRange',
  25414. LinesToStr([ // statements
  25415. '$mod.$rtti.$Int("TGraphicsColor", {',
  25416. ' minvalue: -2147483648,',
  25417. ' maxvalue: 2147483647,',
  25418. ' ordtype: 4',
  25419. '});',
  25420. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  25421. 'this.p = null;',
  25422. '']),
  25423. LinesToStr([ // $mod.$main
  25424. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  25425. '$mod.p = $mod.$rtti["TColor"];',
  25426. '']));
  25427. end;
  25428. procedure TTestModule.TestRTTI_Double;
  25429. begin
  25430. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25431. StartProgram(false);
  25432. Add([
  25433. '{$modeswitch externalclass}',
  25434. 'type',
  25435. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  25436. ' end;',
  25437. ' TFloat = type double;',
  25438. 'var',
  25439. ' p: TTypeInfo;',
  25440. 'begin',
  25441. ' p:=typeinfo(double);',
  25442. ' p:=typeinfo(TFloat);',
  25443. '']);
  25444. ConvertProgram;
  25445. CheckSource('TestRTTI_Double',
  25446. LinesToStr([ // statements
  25447. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  25448. 'this.p = null;',
  25449. '']),
  25450. LinesToStr([ // $mod.$main
  25451. '$mod.p = rtl.double;',
  25452. '$mod.p = $mod.$rtti["TFloat"];',
  25453. '']));
  25454. end;
  25455. procedure TTestModule.TestRTTI_ProcType;
  25456. begin
  25457. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25458. StartProgram(false);
  25459. Add('type');
  25460. Add(' TProcA = procedure;');
  25461. Add(' TMethodB = procedure of object;');
  25462. Add(' TProcC = procedure; varargs;');
  25463. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  25464. Add(' TProcE = function: nativeint;');
  25465. Add(' TProcF = function(const p: TProcA): nativeuint;');
  25466. Add('var p: pointer;');
  25467. Add('begin');
  25468. Add(' p:=typeinfo(tproca);');
  25469. ConvertProgram;
  25470. CheckSource('TestRTTI_ProcType',
  25471. LinesToStr([ // statements
  25472. '$mod.$rtti.$ProcVar("TProcA", {',
  25473. ' procsig: rtl.newTIProcSig(null)',
  25474. '});',
  25475. '$mod.$rtti.$MethodVar("TMethodB", {',
  25476. ' procsig: rtl.newTIProcSig(null),',
  25477. ' methodkind: 0',
  25478. '});',
  25479. '$mod.$rtti.$ProcVar("TProcC", {',
  25480. ' procsig: rtl.newTIProcSig(null, 2)',
  25481. '});',
  25482. '$mod.$rtti.$ProcVar("TProcD", {',
  25483. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  25484. '});',
  25485. '$mod.$rtti.$ProcVar("TProcE", {',
  25486. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  25487. '});',
  25488. '$mod.$rtti.$ProcVar("TProcF", {',
  25489. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  25490. '});',
  25491. 'this.p = null;',
  25492. '']),
  25493. LinesToStr([ // $mod.$main
  25494. '$mod.p = $mod.$rtti["TProcA"];',
  25495. '']));
  25496. end;
  25497. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  25498. begin
  25499. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25500. AddModuleWithIntfImplSrc('unit2.pas',
  25501. LinesToStr([
  25502. 'type',
  25503. ' TObject = class end;'
  25504. ]),
  25505. '');
  25506. StartUnit(true);
  25507. Add('interface');
  25508. Add('uses unit2;');
  25509. Add('type');
  25510. Add(' TProcA = function(o: tobject): tobject;');
  25511. Add('implementation');
  25512. Add('type');
  25513. Add(' TProcB = function(o: tobject): tobject;');
  25514. Add('var p: Pointer;');
  25515. Add('initialization');
  25516. Add(' p:=typeinfo(tproca);');
  25517. Add(' p:=typeinfo(tprocb);');
  25518. ConvertUnit;
  25519. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  25520. LinesToStr([ // statements
  25521. 'var $impl = $mod.$impl;',
  25522. '$mod.$rtti.$ProcVar("TProcA", {',
  25523. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  25524. '});',
  25525. '']),
  25526. LinesToStr([ // this.$init
  25527. '$impl.p = $mod.$rtti["TProcA"];',
  25528. '$impl.p = $mod.$rtti["TProcB"];',
  25529. '']),
  25530. LinesToStr([ // implementation
  25531. '$mod.$rtti.$ProcVar("TProcB", {',
  25532. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  25533. '});',
  25534. '$impl.p = null;',
  25535. '']) );
  25536. end;
  25537. procedure TTestModule.TestRTTI_EnumAndSetType;
  25538. begin
  25539. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25540. StartProgram(false);
  25541. Add('type');
  25542. Add(' TFlag = (light,dark);');
  25543. Add(' TFlags = set of TFlag;');
  25544. Add(' TProc = function(f: TFlags): TFlag;');
  25545. Add('var p: pointer;');
  25546. Add('begin');
  25547. Add(' p:=typeinfo(tflag);');
  25548. Add(' p:=typeinfo(tflags);');
  25549. ConvertProgram;
  25550. CheckSource('TestRTTI_EnumAndType',
  25551. LinesToStr([ // statements
  25552. 'this.TFlag = {',
  25553. ' "0": "light",',
  25554. ' light: 0,',
  25555. ' "1": "dark",',
  25556. ' dark: 1',
  25557. '};',
  25558. '$mod.$rtti.$Enum("TFlag", {',
  25559. ' minvalue: 0,',
  25560. ' maxvalue: 1,',
  25561. ' ordtype: 1,',
  25562. ' enumtype: this.TFlag',
  25563. '});',
  25564. '$mod.$rtti.$Set("TFlags", {',
  25565. ' comptype: $mod.$rtti["TFlag"]',
  25566. '});',
  25567. '$mod.$rtti.$ProcVar("TProc", {',
  25568. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  25569. '});',
  25570. 'this.p = null;',
  25571. '']),
  25572. LinesToStr([ // $mod.$main
  25573. '$mod.p = $mod.$rtti["TFlag"];',
  25574. '$mod.p = $mod.$rtti["TFlags"];',
  25575. '']));
  25576. end;
  25577. procedure TTestModule.TestRTTI_EnumRange;
  25578. begin
  25579. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25580. StartProgram(false);
  25581. Add([
  25582. 'type',
  25583. ' TCol = (red,green,blue);',
  25584. ' TColRg = green..blue;',
  25585. ' TSetOfColRg = set of TColRg;',
  25586. 'var p: pointer;',
  25587. 'begin',
  25588. ' p:=typeinfo(tcolrg);',
  25589. ' p:=typeinfo(tsetofcolrg);',
  25590. '']);
  25591. ConvertProgram;
  25592. end;
  25593. procedure TTestModule.TestRTTI_AnonymousEnumType;
  25594. begin
  25595. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25596. StartProgram(false);
  25597. Add('type');
  25598. Add(' TFlags = set of (red, green);');
  25599. Add('var');
  25600. Add(' f: TFlags;');
  25601. Add('begin');
  25602. Add(' Include(f,red);');
  25603. ConvertProgram;
  25604. CheckSource('TestRTTI_AnonymousEnumType',
  25605. LinesToStr([ // statements
  25606. 'this.TFlags$a = {',
  25607. ' "0": "red",',
  25608. ' red: 0,',
  25609. ' "1": "green",',
  25610. ' green: 1',
  25611. '};',
  25612. '$mod.$rtti.$Enum("TFlags$a", {',
  25613. ' minvalue: 0,',
  25614. ' maxvalue: 1,',
  25615. ' ordtype: 1,',
  25616. ' enumtype: this.TFlags$a',
  25617. '});',
  25618. '$mod.$rtti.$Set("TFlags", {',
  25619. ' comptype: $mod.$rtti["TFlags$a"]',
  25620. '});',
  25621. 'this.f = {};',
  25622. '']),
  25623. LinesToStr([
  25624. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  25625. '']));
  25626. end;
  25627. procedure TTestModule.TestRTTI_StaticArray;
  25628. begin
  25629. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25630. StartProgram(false);
  25631. Add('type');
  25632. Add(' TFlag = (light,dark);');
  25633. Add(' TFlagNames = array[TFlag] of string;');
  25634. Add(' TBoolNames = array[boolean] of string;');
  25635. Add(' TByteArray = array[1..32768] of byte;');
  25636. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  25637. Add('var p: pointer;');
  25638. Add('begin');
  25639. Add(' p:=typeinfo(TFlagNames);');
  25640. Add(' p:=typeinfo(TBoolNames);');
  25641. ConvertProgram;
  25642. CheckSource('TestRTTI_StaticArray',
  25643. LinesToStr([ // statements
  25644. 'this.TFlag = {',
  25645. ' "0": "light",',
  25646. ' light: 0,',
  25647. ' "1": "dark",',
  25648. ' dark: 1',
  25649. '};',
  25650. '$mod.$rtti.$Enum("TFlag", {',
  25651. ' minvalue: 0,',
  25652. ' maxvalue: 1,',
  25653. ' ordtype: 1,',
  25654. ' enumtype: this.TFlag',
  25655. '});',
  25656. '$mod.$rtti.$StaticArray("TFlagNames", {',
  25657. ' dims: [2],',
  25658. ' eltype: rtl.string',
  25659. '});',
  25660. '$mod.$rtti.$StaticArray("TBoolNames", {',
  25661. ' dims: [2],',
  25662. ' eltype: rtl.string',
  25663. '});',
  25664. '$mod.$rtti.$StaticArray("TByteArray", {',
  25665. ' dims: [32768],',
  25666. ' eltype: rtl.byte',
  25667. '});',
  25668. '$mod.$rtti.$ProcVar("TProc", {',
  25669. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  25670. '});',
  25671. 'this.p = null;',
  25672. '']),
  25673. LinesToStr([ // $mod.$main
  25674. '$mod.p = $mod.$rtti["TFlagNames"];',
  25675. '$mod.p = $mod.$rtti["TBoolNames"];',
  25676. '']));
  25677. end;
  25678. procedure TTestModule.TestRTTI_DynArray;
  25679. begin
  25680. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25681. StartProgram(false);
  25682. Add('type');
  25683. Add(' TArrStr = array of string;');
  25684. Add(' TArr2Dim = array of tarrstr;');
  25685. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  25686. Add('var p: pointer;');
  25687. Add('begin');
  25688. Add(' p:=typeinfo(tarrstr);');
  25689. Add(' p:=typeinfo(tarr2dim);');
  25690. ConvertProgram;
  25691. CheckSource('TestRTTI_DynArray',
  25692. LinesToStr([ // statements
  25693. '$mod.$rtti.$DynArray("TArrStr", {',
  25694. ' eltype: rtl.string',
  25695. '});',
  25696. '$mod.$rtti.$DynArray("TArr2Dim", {',
  25697. ' eltype: $mod.$rtti["TArrStr"]',
  25698. '});',
  25699. '$mod.$rtti.$ProcVar("TProc", {',
  25700. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  25701. '});',
  25702. 'this.p = null;',
  25703. '']),
  25704. LinesToStr([ // $mod.$main
  25705. '$mod.p = $mod.$rtti["TArrStr"];',
  25706. '$mod.p = $mod.$rtti["TArr2Dim"];',
  25707. '']));
  25708. end;
  25709. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  25710. begin
  25711. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25712. StartProgram(false);
  25713. Add('type');
  25714. Add(' TArr = array of array of longint;');
  25715. Add('var a: TArr;');
  25716. Add('begin');
  25717. ConvertProgram;
  25718. CheckSource('TestRTTI_ArrayNestedAnonymous',
  25719. LinesToStr([ // statements
  25720. '$mod.$rtti.$DynArray("TArr$a", {',
  25721. ' eltype: rtl.longint',
  25722. '});',
  25723. '$mod.$rtti.$DynArray("TArr", {',
  25724. ' eltype: $mod.$rtti["TArr$a"]',
  25725. '});',
  25726. 'this.a = [];',
  25727. '']),
  25728. LinesToStr([ // $mod.$main
  25729. ]));
  25730. end;
  25731. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  25732. begin
  25733. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25734. StartProgram(false);
  25735. Add('type');
  25736. Add(' TObject = class');
  25737. Add(' published');
  25738. Add(' procedure Proc; virtual; abstract;');
  25739. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  25740. Add(' end;');
  25741. Add('begin');
  25742. SetExpectedPasResolverError('Duplicate identifier "Proc" at test1.pp(6,19)',
  25743. nDuplicateIdentifier);
  25744. ConvertProgram;
  25745. end;
  25746. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  25747. begin
  25748. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25749. StartProgram(false);
  25750. Add('type');
  25751. Add(' TObject = class');
  25752. Add(' published');
  25753. Add(' procedure Proc; external name ''foo'';');
  25754. Add(' end;');
  25755. Add('begin');
  25756. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  25757. nPublishedNameMustMatchExternal);
  25758. ConvertProgram;
  25759. end;
  25760. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  25761. begin
  25762. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25763. StartProgram(false);
  25764. Add('type');
  25765. Add(' TObject = class');
  25766. Add(' class var FA: longint;');
  25767. Add(' published');
  25768. Add(' class property A: longint read FA;');
  25769. Add(' end;');
  25770. Add('begin');
  25771. SetExpectedPasResolverError('Invalid published property modifier "class"',
  25772. nInvalidXModifierY);
  25773. ConvertProgram;
  25774. end;
  25775. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  25776. begin
  25777. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25778. StartProgram(false);
  25779. Add('type');
  25780. Add(' TObject = class');
  25781. Add(' published');
  25782. Add(' class var FA: longint;');
  25783. Add(' end;');
  25784. Add('begin');
  25785. SetExpectedPasResolverError(sSymbolCannotBePublished,
  25786. nSymbolCannotBePublished);
  25787. ConvertProgram;
  25788. end;
  25789. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  25790. begin
  25791. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25792. StartProgram(false);
  25793. Add('{$modeswitch externalclass}');
  25794. Add('type');
  25795. Add(' TObject = class');
  25796. Add(' published');
  25797. Add(' V: longint; external name ''foo'';');
  25798. Add(' end;');
  25799. Add('begin');
  25800. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  25801. nPublishedNameMustMatchExternal);
  25802. ConvertProgram;
  25803. end;
  25804. procedure TTestModule.TestRTTI_Class_Field;
  25805. begin
  25806. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25807. StartProgram(false);
  25808. Add('{$modeswitch externalclass}');
  25809. Add('type');
  25810. Add(' TObject = class');
  25811. Add(' private');
  25812. Add(' FPropA: string;');
  25813. Add(' published');
  25814. Add(' VarLI: longint;');
  25815. Add(' VarC: char;');
  25816. Add(' VarS: string;');
  25817. Add(' VarD: double;');
  25818. Add(' VarB: boolean;');
  25819. Add(' VarLW: longword;');
  25820. Add(' VarSmI: smallint;');
  25821. Add(' VarW: word;');
  25822. Add(' VarShI: shortint;');
  25823. Add(' VarBy: byte;');
  25824. Add(' VarExt: longint external name ''VarExt'';');
  25825. Add(' end;');
  25826. Add('var p: pointer;');
  25827. Add(' Obj: tobject;');
  25828. Add('begin');
  25829. Add(' p:=typeinfo(tobject);');
  25830. Add(' p:=typeinfo(p);');
  25831. Add(' p:=typeinfo(obj);');
  25832. ConvertProgram;
  25833. CheckSource('TestRTTI_Class_Field',
  25834. LinesToStr([ // statements
  25835. 'rtl.createClass($mod, "TObject", null, function () {',
  25836. ' this.$init = function () {',
  25837. ' this.FPropA = "";',
  25838. ' this.VarLI = 0;',
  25839. ' this.VarC = "";',
  25840. ' this.VarS = "";',
  25841. ' this.VarD = 0.0;',
  25842. ' this.VarB = false;',
  25843. ' this.VarLW = 0;',
  25844. ' this.VarSmI = 0;',
  25845. ' this.VarW = 0;',
  25846. ' this.VarShI = 0;',
  25847. ' this.VarBy = 0;',
  25848. ' };',
  25849. ' this.$final = function () {',
  25850. ' };',
  25851. ' var $r = this.$rtti;',
  25852. ' $r.addField("VarLI", rtl.longint);',
  25853. ' $r.addField("VarC", rtl.char);',
  25854. ' $r.addField("VarS", rtl.string);',
  25855. ' $r.addField("VarD", rtl.double);',
  25856. ' $r.addField("VarB", rtl.boolean);',
  25857. ' $r.addField("VarLW", rtl.longword);',
  25858. ' $r.addField("VarSmI", rtl.smallint);',
  25859. ' $r.addField("VarW", rtl.word);',
  25860. ' $r.addField("VarShI", rtl.shortint);',
  25861. ' $r.addField("VarBy", rtl.byte);',
  25862. ' $r.addField("VarExt", rtl.longint);',
  25863. '});',
  25864. 'this.p = null;',
  25865. 'this.Obj = null;',
  25866. '']),
  25867. LinesToStr([ // $mod.$main
  25868. '$mod.p = $mod.$rtti["TObject"];',
  25869. '$mod.p = rtl.pointer;',
  25870. '$mod.p = $mod.Obj.$rtti;',
  25871. '']));
  25872. end;
  25873. procedure TTestModule.TestRTTI_Class_Method;
  25874. begin
  25875. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25876. StartProgram(false);
  25877. Add('type');
  25878. Add(' TObject = class');
  25879. Add(' private');
  25880. Add(' procedure Internal; external name ''$intern'';');
  25881. Add(' published');
  25882. Add(' procedure Click; virtual; abstract;');
  25883. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  25884. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  25885. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  25886. Add(' end;');
  25887. Add('begin');
  25888. ConvertProgram;
  25889. CheckSource('TestRTTI_Class_Method',
  25890. LinesToStr([ // statements
  25891. 'rtl.createClass($mod, "TObject", null, function () {',
  25892. ' this.$init = function () {',
  25893. ' };',
  25894. ' this.$final = function () {',
  25895. ' };',
  25896. ' var $r = this.$rtti;',
  25897. ' $r.addMethod("Click", 0, null);',
  25898. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  25899. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  25900. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  25901. ' flags: 2',
  25902. ' });',
  25903. '});',
  25904. '']),
  25905. LinesToStr([ // $mod.$main
  25906. '']));
  25907. end;
  25908. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  25909. begin
  25910. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25911. StartProgram(false);
  25912. Add('type');
  25913. Add(' TObject = class');
  25914. Add(' published');
  25915. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  25916. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  25917. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  25918. Add(' end;');
  25919. Add('begin');
  25920. ConvertProgram;
  25921. CheckSource('TestRTTI_Class_MethodOpenArray',
  25922. LinesToStr([ // statements
  25923. 'rtl.createClass($mod, "TObject", null, function () {',
  25924. ' this.$init = function () {',
  25925. ' };',
  25926. ' this.$final = function () {',
  25927. ' };',
  25928. ' var $r = this.$rtti;',
  25929. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  25930. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  25931. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  25932. '});',
  25933. '']),
  25934. LinesToStr([ // $mod.$main
  25935. '']));
  25936. end;
  25937. procedure TTestModule.TestRTTI_Class_Property;
  25938. begin
  25939. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25940. StartProgram(false);
  25941. Add('{$modeswitch externalclass}');
  25942. Add('type');
  25943. Add(' TObject = class');
  25944. Add(' private');
  25945. Add(' FColor: longint;');
  25946. Add(' FColorStored: boolean;');
  25947. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  25948. Add(' function GetColor: longint; virtual; abstract;');
  25949. Add(' function GetColorStored: boolean; virtual; abstract;');
  25950. Add(' FExtSize: longint external name ''$extSize'';');
  25951. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  25952. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  25953. Add(' function GetExtSize: longint; external name ''$getSize'';');
  25954. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  25955. Add(' published');
  25956. Add(' property ColorA: longint read FColor;');
  25957. Add(' property ColorB: longint write FColor;');
  25958. Add(' property ColorC: longint read GetColor write SetColor;');
  25959. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  25960. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  25961. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  25962. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  25963. Add(' end;');
  25964. Add('begin');
  25965. ConvertProgram;
  25966. CheckSource('TestRTTI_Class_Property',
  25967. LinesToStr([ // statements
  25968. 'rtl.createClass($mod, "TObject", null, function () {',
  25969. ' this.$init = function () {',
  25970. ' this.FColor = 0;',
  25971. ' this.FColorStored = false;',
  25972. ' };',
  25973. ' this.$final = function () {',
  25974. ' };',
  25975. ' var $r = this.$rtti;',
  25976. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  25977. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  25978. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  25979. ' $r.addProperty(',
  25980. ' "ColorD",',
  25981. ' 8,',
  25982. ' rtl.longint,',
  25983. ' "FColor",',
  25984. ' "FColor",',
  25985. ' {',
  25986. ' stored: "FColorStored"',
  25987. ' }',
  25988. ' );',
  25989. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  25990. ' $r.addProperty(',
  25991. ' "ExtSizeB",',
  25992. ' 11,',
  25993. ' rtl.longint,',
  25994. ' "$getSize",',
  25995. ' "$setSize",',
  25996. ' {',
  25997. ' stored: "$extSizeStored"',
  25998. ' }',
  25999. ' );',
  26000. ' $r.addProperty(',
  26001. ' "ExtSizeC",',
  26002. ' 12,',
  26003. ' rtl.longint,',
  26004. ' "$extSize",',
  26005. ' "$extSize",',
  26006. ' {',
  26007. ' stored: "$getExtSizeStored"',
  26008. ' }',
  26009. ' );',
  26010. '});',
  26011. '']),
  26012. LinesToStr([ // $mod.$main
  26013. '']));
  26014. end;
  26015. procedure TTestModule.TestRTTI_Class_PropertyParams;
  26016. begin
  26017. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26018. StartProgram(false);
  26019. Add('{$modeswitch externalclass}');
  26020. Add('type');
  26021. Add(' integer = longint;');
  26022. Add(' TObject = class');
  26023. Add(' private');
  26024. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  26025. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  26026. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  26027. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  26028. Add(' published');
  26029. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  26030. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  26031. Add(' end;');
  26032. Add('begin');
  26033. ConvertProgram;
  26034. CheckSource('TestRTTI_Class_PropertyParams',
  26035. LinesToStr([ // statements
  26036. 'rtl.createClass($mod, "TObject", null, function () {',
  26037. ' this.$init = function () {',
  26038. ' };',
  26039. ' this.$final = function () {',
  26040. ' };',
  26041. ' var $r = this.$rtti;',
  26042. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  26043. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  26044. '});',
  26045. '']),
  26046. LinesToStr([ // $mod.$main
  26047. '']));
  26048. end;
  26049. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  26050. begin
  26051. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26052. AddModuleWithIntfImplSrc('unit1.pas',
  26053. 'type TColor = -5..5;',
  26054. '');
  26055. StartProgram(true);
  26056. Add([
  26057. 'uses unit1;',
  26058. 'type',
  26059. ' TColorAlias = TColor;',
  26060. ' TColorTypeAlias = type TColor;',
  26061. ' TObject = class',
  26062. ' private',
  26063. ' fColor: TColor;',
  26064. ' fAlias: TColorAlias;',
  26065. ' fTypeAlias: TColorTypeAlias;',
  26066. ' published',
  26067. ' property Color: TColor read fcolor;',
  26068. ' property Alias: TColorAlias read falias;',
  26069. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  26070. ' end;',
  26071. 'begin',
  26072. '']);
  26073. ConvertProgram;
  26074. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  26075. LinesToStr([ // statements
  26076. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  26077. 'rtl.createClass($mod, "TObject", null, function () {',
  26078. ' this.$init = function () {',
  26079. ' this.fColor = 0;',
  26080. ' this.fAlias = 0;',
  26081. ' this.fTypeAlias = 0;',
  26082. ' };',
  26083. ' this.$final = function () {',
  26084. ' };',
  26085. ' var $r = this.$rtti;',
  26086. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  26087. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  26088. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  26089. '});',
  26090. '']),
  26091. LinesToStr([ // $mod.$main
  26092. '']));
  26093. end;
  26094. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  26095. begin
  26096. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26097. StartProgram(false);
  26098. Add([
  26099. '{$modeswitch omitrtti}',
  26100. 'type',
  26101. ' TObject = class',
  26102. ' private',
  26103. ' FA: byte;',
  26104. ' published',
  26105. ' property A: byte read FA write FA;',
  26106. ' end;',
  26107. 'begin']);
  26108. ConvertProgram;
  26109. CheckSource('TestRTTI_Class_OmitRTTI',
  26110. LinesToStr([ // statements
  26111. 'rtl.createClass($mod, "TObject", null, function () {',
  26112. ' this.$init = function () {',
  26113. ' this.FA = 0;',
  26114. ' };',
  26115. ' this.$final = function () {',
  26116. ' };',
  26117. '});',
  26118. '']),
  26119. LinesToStr([ // $mod.$main
  26120. '']));
  26121. end;
  26122. procedure TTestModule.TestRTTI_IndexModifier;
  26123. begin
  26124. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26125. StartProgram(false);
  26126. Add([
  26127. 'type',
  26128. ' TEnum = (red, blue);',
  26129. ' TObject = class',
  26130. ' FB: boolean;',
  26131. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  26132. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  26133. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  26134. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  26135. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  26136. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  26137. ' published',
  26138. ' property B1: boolean index 1 read FB write SetIntBool;',
  26139. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  26140. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  26141. ' end;',
  26142. 'begin']);
  26143. ConvertProgram;
  26144. CheckSource('TestRTTI_IndexModifier',
  26145. LinesToStr([ // statements
  26146. 'this.TEnum = {',
  26147. ' "0": "red",',
  26148. ' red: 0,',
  26149. ' "1": "blue",',
  26150. ' blue: 1',
  26151. '};',
  26152. '$mod.$rtti.$Enum("TEnum", {',
  26153. ' minvalue: 0,',
  26154. ' maxvalue: 1,',
  26155. ' ordtype: 1,',
  26156. ' enumtype: this.TEnum',
  26157. '});',
  26158. 'rtl.createClass($mod, "TObject", null, function () {',
  26159. ' this.$init = function () {',
  26160. ' this.FB = false;',
  26161. ' };',
  26162. ' this.$final = function () {',
  26163. ' };',
  26164. ' var $r = this.$rtti;',
  26165. ' $r.addProperty(',
  26166. ' "B1",',
  26167. ' 18,',
  26168. ' rtl.boolean,',
  26169. ' "FB",',
  26170. ' "SetIntBool",',
  26171. ' {',
  26172. ' index: 1',
  26173. ' }',
  26174. ' );',
  26175. ' $r.addProperty(',
  26176. ' "B2",',
  26177. ' 17,',
  26178. ' rtl.boolean,',
  26179. ' "GetEnumBool",',
  26180. ' "FB",',
  26181. ' {',
  26182. ' index: $mod.TEnum.blue',
  26183. ' }',
  26184. ' );',
  26185. ' $r.addProperty(',
  26186. ' "I1",',
  26187. ' 19,',
  26188. ' rtl.boolean,',
  26189. ' "GetStrIntBool",',
  26190. ' "SetStrIntBool",',
  26191. ' {',
  26192. ' index: 2',
  26193. ' }',
  26194. ' );',
  26195. '});',
  26196. '']),
  26197. LinesToStr([ // $mod.$main
  26198. '']));
  26199. end;
  26200. procedure TTestModule.TestRTTI_StoredModifier;
  26201. begin
  26202. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26203. StartProgram(false);
  26204. Add([
  26205. 'const',
  26206. ' ConstB = true;',
  26207. 'type',
  26208. ' TObject = class',
  26209. ' private',
  26210. ' FB: boolean;',
  26211. ' function IsBStored: boolean; virtual; abstract;',
  26212. ' published',
  26213. ' property BoolA: boolean read FB stored true;',
  26214. ' property BoolB: boolean read FB stored false;',
  26215. ' property BoolC: boolean read FB stored FB;',
  26216. ' property BoolD: boolean read FB stored ConstB;',
  26217. ' property BoolE: boolean read FB stored IsBStored;',
  26218. ' end;',
  26219. 'begin']);
  26220. ConvertProgram;
  26221. CheckSource('TestRTTI_StoredModifier',
  26222. LinesToStr([ // statements
  26223. 'this.ConstB = true;',
  26224. 'rtl.createClass($mod, "TObject", null, function () {',
  26225. ' this.$init = function () {',
  26226. ' this.FB = false;',
  26227. ' };',
  26228. ' this.$final = function () {',
  26229. ' };',
  26230. ' var $r = this.$rtti;',
  26231. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  26232. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  26233. ' $r.addProperty(',
  26234. ' "BoolC",',
  26235. ' 8,',
  26236. ' rtl.boolean,',
  26237. ' "FB",',
  26238. ' "",',
  26239. ' {',
  26240. ' stored: "FB"',
  26241. ' }',
  26242. ' );',
  26243. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  26244. ' $r.addProperty(',
  26245. ' "BoolE",',
  26246. ' 12,',
  26247. ' rtl.boolean,',
  26248. ' "FB",',
  26249. ' "",',
  26250. ' {',
  26251. ' stored: "IsBStored"',
  26252. ' }',
  26253. ' );',
  26254. '});',
  26255. '']),
  26256. LinesToStr([ // $mod.$main
  26257. '']));
  26258. end;
  26259. procedure TTestModule.TestRTTI_DefaultValue;
  26260. begin
  26261. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26262. StartProgram(false);
  26263. Add([
  26264. 'type',
  26265. ' TEnum = (red, blue);',
  26266. 'const',
  26267. ' CB = true or false;',
  26268. ' CI = 1+2;',
  26269. 'type',
  26270. ' TObject = class',
  26271. ' FB: boolean;',
  26272. ' FI: longint;',
  26273. ' FE: TEnum;',
  26274. ' published',
  26275. ' property B1: boolean read FB default true;',
  26276. ' property B2: boolean read FB default CB;',
  26277. ' property B3: boolean read FB default test1.cb;',
  26278. ' property I1: longint read FI default 2;',
  26279. ' property I2: longint read FI default CI;',
  26280. ' property E1: TEnum read FE default red;',
  26281. ' property E2: TEnum read FE default TEnum.blue;',
  26282. ' end;',
  26283. 'begin']);
  26284. ConvertProgram;
  26285. CheckSource('TestRTTI_DefaultValue',
  26286. LinesToStr([ // statements
  26287. 'this.TEnum = {',
  26288. ' "0": "red",',
  26289. ' red: 0,',
  26290. ' "1": "blue",',
  26291. ' blue: 1',
  26292. '};',
  26293. '$mod.$rtti.$Enum("TEnum", {',
  26294. ' minvalue: 0,',
  26295. ' maxvalue: 1,',
  26296. ' ordtype: 1,',
  26297. ' enumtype: this.TEnum',
  26298. '});',
  26299. 'this.CB = true || false;',
  26300. 'this.CI = 1 + 2;',
  26301. 'rtl.createClass($mod, "TObject", null, function () {',
  26302. ' this.$init = function () {',
  26303. ' this.FB = false;',
  26304. ' this.FI = 0;',
  26305. ' this.FE = 0;',
  26306. ' };',
  26307. ' this.$final = function () {',
  26308. ' };',
  26309. ' var $r = this.$rtti;',
  26310. ' $r.addProperty(',
  26311. ' "B1",',
  26312. ' 0,',
  26313. ' rtl.boolean,',
  26314. ' "FB",',
  26315. ' "",',
  26316. ' {',
  26317. ' Default: true',
  26318. ' }',
  26319. ' );',
  26320. ' $r.addProperty(',
  26321. ' "B2",',
  26322. ' 0,',
  26323. ' rtl.boolean,',
  26324. ' "FB",',
  26325. ' "",',
  26326. ' {',
  26327. ' Default: true',
  26328. ' }',
  26329. ' );',
  26330. ' $r.addProperty(',
  26331. ' "B3",',
  26332. ' 0,',
  26333. ' rtl.boolean,',
  26334. ' "FB",',
  26335. ' "",',
  26336. ' {',
  26337. ' Default: true',
  26338. ' }',
  26339. ' );',
  26340. ' $r.addProperty(',
  26341. ' "I1",',
  26342. ' 0,',
  26343. ' rtl.longint,',
  26344. ' "FI",',
  26345. ' "",',
  26346. ' {',
  26347. ' Default: 2',
  26348. ' }',
  26349. ' );',
  26350. ' $r.addProperty(',
  26351. ' "I2",',
  26352. ' 0,',
  26353. ' rtl.longint,',
  26354. ' "FI",',
  26355. ' "",',
  26356. ' {',
  26357. ' Default: 3',
  26358. ' }',
  26359. ' );',
  26360. ' $r.addProperty(',
  26361. ' "E1",',
  26362. ' 0,',
  26363. ' $mod.$rtti["TEnum"],',
  26364. ' "FE",',
  26365. ' "",',
  26366. ' {',
  26367. ' Default: $mod.TEnum.red',
  26368. ' }',
  26369. ' );',
  26370. ' $r.addProperty(',
  26371. ' "E2",',
  26372. ' 0,',
  26373. ' $mod.$rtti["TEnum"],',
  26374. ' "FE",',
  26375. ' "",',
  26376. ' {',
  26377. ' Default: $mod.TEnum.blue',
  26378. ' }',
  26379. ' );',
  26380. '});',
  26381. '']),
  26382. LinesToStr([ // $mod.$main
  26383. '']));
  26384. end;
  26385. procedure TTestModule.TestRTTI_DefaultValueSet;
  26386. begin
  26387. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26388. StartProgram(false);
  26389. Add([
  26390. 'type',
  26391. ' TEnum = (red, blue);',
  26392. ' TSet = set of TEnum;',
  26393. 'const',
  26394. ' CSet = [red,blue];',
  26395. 'type',
  26396. ' TObject = class',
  26397. ' FSet: TSet;',
  26398. ' published',
  26399. ' property Set1: TSet read FSet default [];',
  26400. ' property Set2: TSet read FSet default [red];',
  26401. ' property Set3: TSet read FSet default [red,blue];',
  26402. ' property Set4: TSet read FSet default CSet;',
  26403. ' end;',
  26404. 'begin']);
  26405. ConvertProgram;
  26406. CheckSource('TestRTTI_DefaultValueSet',
  26407. LinesToStr([ // statements
  26408. 'this.TEnum = {',
  26409. ' "0": "red",',
  26410. ' red: 0,',
  26411. ' "1": "blue",',
  26412. ' blue: 1',
  26413. '};',
  26414. '$mod.$rtti.$Enum("TEnum", {',
  26415. ' minvalue: 0,',
  26416. ' maxvalue: 1,',
  26417. ' ordtype: 1,',
  26418. ' enumtype: this.TEnum',
  26419. '});',
  26420. '$mod.$rtti.$Set("TSet", {',
  26421. ' comptype: $mod.$rtti["TEnum"]',
  26422. '});',
  26423. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  26424. 'rtl.createClass($mod, "TObject", null, function () {',
  26425. ' this.$init = function () {',
  26426. ' this.FSet = {};',
  26427. ' };',
  26428. ' this.$final = function () {',
  26429. ' this.FSet = undefined;',
  26430. ' };',
  26431. ' var $r = this.$rtti;',
  26432. ' $r.addProperty(',
  26433. ' "Set1",',
  26434. ' 0,',
  26435. ' $mod.$rtti["TSet"],',
  26436. ' "FSet",',
  26437. ' "",',
  26438. ' {',
  26439. ' Default: {}',
  26440. ' }',
  26441. ' );',
  26442. ' $r.addProperty(',
  26443. ' "Set2",',
  26444. ' 0,',
  26445. ' $mod.$rtti["TSet"],',
  26446. ' "FSet",',
  26447. ' "",',
  26448. ' {',
  26449. ' Default: rtl.createSet($mod.TEnum.red)',
  26450. ' }',
  26451. ' );',
  26452. ' $r.addProperty(',
  26453. ' "Set3",',
  26454. ' 0,',
  26455. ' $mod.$rtti["TSet"],',
  26456. ' "FSet",',
  26457. ' "",',
  26458. ' {',
  26459. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  26460. ' }',
  26461. ' );',
  26462. ' $r.addProperty(',
  26463. ' "Set4",',
  26464. ' 0,',
  26465. ' $mod.$rtti["TSet"],',
  26466. ' "FSet",',
  26467. ' "",',
  26468. ' {',
  26469. ' Default: $mod.CSet',
  26470. ' }',
  26471. ' );',
  26472. '});',
  26473. '']),
  26474. LinesToStr([ // $mod.$main
  26475. '']));
  26476. end;
  26477. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  26478. begin
  26479. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26480. StartProgram(false);
  26481. Add([
  26482. 'type',
  26483. ' TRg = -1..1;',
  26484. 'const',
  26485. ' l = low(TRg);',
  26486. ' h = high(TRg);',
  26487. 'type',
  26488. ' TObject = class',
  26489. ' FV: TRg;',
  26490. ' published',
  26491. ' property V1: TRg read FV default -1;',
  26492. ' end;',
  26493. 'begin']);
  26494. ConvertProgram;
  26495. CheckSource('TestRTTI_DefaultValueRangeType',
  26496. LinesToStr([ // statements
  26497. '$mod.$rtti.$Int("TRg", {',
  26498. ' minvalue: -1,',
  26499. ' maxvalue: 1,',
  26500. ' ordtype: 0',
  26501. '});',
  26502. 'this.l = -1;',
  26503. 'this.h = 1;',
  26504. 'rtl.createClass($mod, "TObject", null, function () {',
  26505. ' this.$init = function () {',
  26506. ' this.FV = 0;',
  26507. ' };',
  26508. ' this.$final = function () {',
  26509. ' };',
  26510. ' var $r = this.$rtti;',
  26511. ' $r.addProperty(',
  26512. ' "V1",',
  26513. ' 0,',
  26514. ' $mod.$rtti["TRg"],',
  26515. ' "FV",',
  26516. ' "",',
  26517. ' {',
  26518. ' Default: -1',
  26519. ' }',
  26520. ' );',
  26521. '});',
  26522. '']),
  26523. LinesToStr([ // $mod.$main
  26524. '']));
  26525. end;
  26526. procedure TTestModule.TestRTTI_DefaultValueInherit;
  26527. begin
  26528. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26529. StartProgram(false);
  26530. Add([
  26531. 'type',
  26532. ' TObject = class',
  26533. ' FA, FB: byte;',
  26534. ' property A: byte read FA default 1;',
  26535. ' property B: byte read FB default 2;',
  26536. ' end;',
  26537. ' TBird = class',
  26538. ' published',
  26539. ' property A;',
  26540. ' property B nodefault;',
  26541. ' end;',
  26542. 'begin']);
  26543. ConvertProgram;
  26544. CheckSource('TestRTTI_DefaultValueInherit',
  26545. LinesToStr([ // statements
  26546. 'rtl.createClass($mod, "TObject", null, function () {',
  26547. ' this.$init = function () {',
  26548. ' this.FA = 0;',
  26549. ' this.FB = 0;',
  26550. ' };',
  26551. ' this.$final = function () {',
  26552. ' };',
  26553. '});',
  26554. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  26555. ' var $r = this.$rtti;',
  26556. ' $r.addProperty(',
  26557. ' "A",',
  26558. ' 0,',
  26559. ' rtl.byte,',
  26560. ' "FA",',
  26561. ' "",',
  26562. ' {',
  26563. ' Default: 1',
  26564. ' }',
  26565. ' );',
  26566. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  26567. '});',
  26568. '']),
  26569. LinesToStr([ // $mod.$main
  26570. '']));
  26571. end;
  26572. procedure TTestModule.TestRTTI_OverrideMethod;
  26573. begin
  26574. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26575. StartProgram(false);
  26576. Add('type');
  26577. Add(' TObject = class');
  26578. Add(' published');
  26579. Add(' procedure DoIt; virtual; abstract;');
  26580. Add(' end;');
  26581. Add(' TSky = class');
  26582. Add(' published');
  26583. Add(' procedure DoIt; override;');
  26584. Add(' end;');
  26585. Add('procedure TSky.DoIt; begin end;');
  26586. Add('begin');
  26587. ConvertProgram;
  26588. CheckSource('TestRTTI_OverrideMethod',
  26589. LinesToStr([ // statements
  26590. 'rtl.createClass($mod, "TObject", null, function () {',
  26591. ' this.$init = function () {',
  26592. ' };',
  26593. ' this.$final = function () {',
  26594. ' };',
  26595. ' var $r = this.$rtti;',
  26596. ' $r.addMethod("DoIt", 0, null);',
  26597. '});',
  26598. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  26599. ' this.DoIt = function () {',
  26600. ' };',
  26601. '});',
  26602. '']),
  26603. LinesToStr([ // $mod.$main
  26604. '']));
  26605. end;
  26606. procedure TTestModule.TestRTTI_OverloadProperty;
  26607. begin
  26608. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26609. StartProgram(false);
  26610. Add('type');
  26611. Add(' TObject = class');
  26612. Add(' protected');
  26613. Add(' FFlag: longint;');
  26614. Add(' published');
  26615. Add(' property Flag: longint read fflag;');
  26616. Add(' end;');
  26617. Add(' TSky = class');
  26618. Add(' published');
  26619. Add(' property FLAG: longint write fflag;');
  26620. Add(' end;');
  26621. Add('begin');
  26622. ConvertProgram;
  26623. CheckSource('TestRTTI_OverrideMethod',
  26624. LinesToStr([ // statements
  26625. 'rtl.createClass($mod, "TObject", null, function () {',
  26626. ' this.$init = function () {',
  26627. ' this.FFlag = 0;',
  26628. ' };',
  26629. ' this.$final = function () {',
  26630. ' };',
  26631. ' var $r = this.$rtti;',
  26632. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  26633. '});',
  26634. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  26635. ' var $r = this.$rtti;',
  26636. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  26637. '});',
  26638. '']),
  26639. LinesToStr([ // $mod.$main
  26640. '']));
  26641. end;
  26642. procedure TTestModule.TestRTTI_ClassForward;
  26643. begin
  26644. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26645. StartProgram(false);
  26646. Add('type');
  26647. Add(' TObject = class end;');
  26648. Add(' tbridge = class;');
  26649. Add(' TProc = function: tbridge;');
  26650. Add(' TOger = class');
  26651. Add(' published');
  26652. Add(' FBridge: tbridge;');
  26653. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  26654. Add(' property Bridge: tbridge read fbridge write setbridge;');
  26655. Add(' end;');
  26656. Add(' TBridge = class');
  26657. Add(' FOger: toger;');
  26658. Add(' end;');
  26659. Add('var p: Pointer;');
  26660. Add(' b: tbridge;');
  26661. Add('begin');
  26662. Add(' p:=typeinfo(tbridge);');
  26663. Add(' p:=typeinfo(b);');
  26664. ConvertProgram;
  26665. CheckSource('TestRTTI_ClassForward',
  26666. LinesToStr([ // statements
  26667. 'rtl.createClass($mod, "TObject", null, function () {',
  26668. ' this.$init = function () {',
  26669. ' };',
  26670. ' this.$final = function () {',
  26671. ' };',
  26672. '});',
  26673. '$mod.$rtti.$Class("TBridge");',
  26674. '$mod.$rtti.$ProcVar("TProc", {',
  26675. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  26676. '});',
  26677. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  26678. ' this.$init = function () {',
  26679. ' $mod.TObject.$init.call(this);',
  26680. ' this.FBridge = null;',
  26681. ' };',
  26682. ' this.$final = function () {',
  26683. ' this.FBridge = undefined;',
  26684. ' $mod.TObject.$final.call(this);',
  26685. ' };',
  26686. ' var $r = this.$rtti;',
  26687. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  26688. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  26689. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  26690. '});',
  26691. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  26692. ' this.$init = function () {',
  26693. ' $mod.TObject.$init.call(this);',
  26694. ' this.FOger = null;',
  26695. ' };',
  26696. ' this.$final = function () {',
  26697. ' this.FOger = undefined;',
  26698. ' $mod.TObject.$final.call(this);',
  26699. ' };',
  26700. '});',
  26701. 'this.p = null;',
  26702. 'this.b = null;',
  26703. '']),
  26704. LinesToStr([ // $mod.$main
  26705. '$mod.p = $mod.$rtti["TBridge"];',
  26706. '$mod.p = $mod.b.$rtti;',
  26707. '']));
  26708. end;
  26709. procedure TTestModule.TestRTTI_ClassOf;
  26710. begin
  26711. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26712. StartProgram(false);
  26713. Add('type');
  26714. Add(' TClass = class of tobject;');
  26715. Add(' TProcA = function: TClass;');
  26716. Add(' TObject = class');
  26717. Add(' published');
  26718. Add(' C: tclass;');
  26719. Add(' end;');
  26720. Add(' tfox = class;');
  26721. Add(' TBird = class end;');
  26722. Add(' TBirds = class of tbird;');
  26723. Add(' TFox = class end;');
  26724. Add(' TFoxes = class of tfox;');
  26725. Add(' TCows = class of TCow;');
  26726. Add(' TCow = class;');
  26727. Add(' TCow = class end;');
  26728. Add('begin');
  26729. ConvertProgram;
  26730. CheckSource('TestRTTI_ClassOf',
  26731. LinesToStr([ // statements
  26732. '$mod.$rtti.$Class("TObject");',
  26733. '$mod.$rtti.$ClassRef("TClass", {',
  26734. ' instancetype: $mod.$rtti["TObject"]',
  26735. '});',
  26736. '$mod.$rtti.$ProcVar("TProcA", {',
  26737. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  26738. '});',
  26739. 'rtl.createClass($mod, "TObject", null, function () {',
  26740. ' this.$init = function () {',
  26741. ' this.C = null;',
  26742. ' };',
  26743. ' this.$final = function () {',
  26744. ' this.C = undefined;',
  26745. ' };',
  26746. ' var $r = this.$rtti;',
  26747. ' $r.addField("C", $mod.$rtti["TClass"]);',
  26748. '});',
  26749. '$mod.$rtti.$Class("TFox");',
  26750. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  26751. '});',
  26752. '$mod.$rtti.$ClassRef("TBirds", {',
  26753. ' instancetype: $mod.$rtti["TBird"]',
  26754. '});',
  26755. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  26756. '});',
  26757. '$mod.$rtti.$ClassRef("TFoxes", {',
  26758. ' instancetype: $mod.$rtti["TFox"]',
  26759. '});',
  26760. '$mod.$rtti.$Class("TCow");',
  26761. '$mod.$rtti.$ClassRef("TCows", {',
  26762. ' instancetype: $mod.$rtti["TCow"]',
  26763. '});',
  26764. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  26765. '});',
  26766. '']),
  26767. LinesToStr([ // $mod.$main
  26768. '']));
  26769. end;
  26770. procedure TTestModule.TestRTTI_Record;
  26771. begin
  26772. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26773. StartProgram(false);
  26774. Add('type');
  26775. Add(' integer = longint;');
  26776. Add(' TPoint = record');
  26777. Add(' x,y: integer;');
  26778. Add(' end;');
  26779. Add('var p: pointer;');
  26780. Add(' r: tpoint;');
  26781. Add('begin');
  26782. Add(' p:=typeinfo(tpoint);');
  26783. Add(' p:=typeinfo(r);');
  26784. Add(' p:=typeinfo(r.x);');
  26785. ConvertProgram;
  26786. CheckSource('TestRTTI_Record',
  26787. LinesToStr([ // statements
  26788. 'rtl.recNewT($mod, "TPoint", function () {',
  26789. ' this.x = 0;',
  26790. ' this.y = 0;',
  26791. ' this.$eq = function (b) {',
  26792. ' return (this.x === b.x) && (this.y === b.y);',
  26793. ' };',
  26794. ' this.$assign = function (s) {',
  26795. ' this.x = s.x;',
  26796. ' this.y = s.y;',
  26797. ' return this;',
  26798. ' };',
  26799. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  26800. ' $r.addField("x", rtl.longint);',
  26801. ' $r.addField("y", rtl.longint);',
  26802. '});',
  26803. 'this.p = null;',
  26804. 'this.r = $mod.TPoint.$new();',
  26805. '']),
  26806. LinesToStr([ // $mod.$main
  26807. '$mod.p = $mod.$rtti["TPoint"];',
  26808. '$mod.p = $mod.$rtti["TPoint"];',
  26809. '$mod.p = rtl.longint;',
  26810. '']));
  26811. end;
  26812. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  26813. begin
  26814. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26815. StartProgram(false);
  26816. Add('type');
  26817. Add(' TFloatRec = record');
  26818. Add(' d: array of char;');
  26819. // Add(' i: array of array of longint;');
  26820. Add(' end;');
  26821. Add('var p: pointer;');
  26822. Add(' r: tfloatrec;');
  26823. Add('begin');
  26824. Add(' p:=typeinfo(tfloatrec);');
  26825. Add(' p:=typeinfo(r);');
  26826. Add(' p:=typeinfo(r.d);');
  26827. ConvertProgram;
  26828. CheckSource('TestRTTI_Record',
  26829. LinesToStr([ // statements
  26830. 'rtl.recNewT($mod, "TFloatRec", function () {',
  26831. ' this.d = [];',
  26832. ' this.$eq = function (b) {',
  26833. ' return this.d === b.d;',
  26834. ' };',
  26835. ' this.$assign = function (s) {',
  26836. ' this.d = s.d;',
  26837. ' return this;',
  26838. ' };',
  26839. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  26840. ' eltype: rtl.char',
  26841. ' });',
  26842. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  26843. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  26844. '});',
  26845. 'this.p = null;',
  26846. 'this.r = $mod.TFloatRec.$new();',
  26847. '']),
  26848. LinesToStr([ // $mod.$main
  26849. '$mod.p = $mod.$rtti["TFloatRec"];',
  26850. '$mod.p = $mod.$rtti["TFloatRec"];',
  26851. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  26852. '']));
  26853. end;
  26854. procedure TTestModule.TestRTTI_LocalTypes;
  26855. begin
  26856. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26857. StartProgram(false);
  26858. Add([
  26859. 'procedure DoIt;',
  26860. 'type',
  26861. ' integer = longint;',
  26862. ' TPoint = record',
  26863. ' x,y: integer;',
  26864. ' end;',
  26865. 'var p: TPoint;',
  26866. 'begin',
  26867. 'end;',
  26868. 'begin']);
  26869. ConvertProgram;
  26870. CheckSource('TestRTTI_LocalTypes',
  26871. LinesToStr([ // statements
  26872. 'var TPoint = rtl.recNewT(null, "", function () {',
  26873. ' this.x = 0;',
  26874. ' this.y = 0;',
  26875. ' this.$eq = function (b) {',
  26876. ' return (this.x === b.x) && (this.y === b.y);',
  26877. ' };',
  26878. ' this.$assign = function (s) {',
  26879. ' this.x = s.x;',
  26880. ' this.y = s.y;',
  26881. ' return this;',
  26882. ' };',
  26883. '});',
  26884. 'this.DoIt = function () {',
  26885. ' var p = TPoint.$new();',
  26886. '};',
  26887. '']),
  26888. LinesToStr([ // $mod.$main
  26889. '']));
  26890. end;
  26891. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  26892. begin
  26893. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26894. StartProgram(false);
  26895. Add([
  26896. 'type',
  26897. ' TCaption = string;',
  26898. ' TYesNo = boolean;',
  26899. ' TLetter = char;',
  26900. ' TFloat = double;',
  26901. ' TPtr = pointer;',
  26902. ' TShortInt = shortint;',
  26903. ' TByte = byte;',
  26904. ' TSmallInt = smallint;',
  26905. ' TWord = word;',
  26906. ' TInt32 = longint;',
  26907. ' TDWord = longword;',
  26908. ' TValue = jsvalue;',
  26909. 'var p: TPtr;',
  26910. 'begin',
  26911. ' p:=typeinfo(string);',
  26912. ' p:=typeinfo(tcaption);',
  26913. ' p:=typeinfo(boolean);',
  26914. ' p:=typeinfo(tyesno);',
  26915. ' p:=typeinfo(char);',
  26916. ' p:=typeinfo(tletter);',
  26917. ' p:=typeinfo(double);',
  26918. ' p:=typeinfo(tfloat);',
  26919. ' p:=typeinfo(pointer);',
  26920. ' p:=typeinfo(tptr);',
  26921. ' p:=typeinfo(shortint);',
  26922. ' p:=typeinfo(tshortint);',
  26923. ' p:=typeinfo(byte);',
  26924. ' p:=typeinfo(tbyte);',
  26925. ' p:=typeinfo(smallint);',
  26926. ' p:=typeinfo(tsmallint);',
  26927. ' p:=typeinfo(word);',
  26928. ' p:=typeinfo(tword);',
  26929. ' p:=typeinfo(longword);',
  26930. ' p:=typeinfo(tdword);',
  26931. ' p:=typeinfo(jsvalue);',
  26932. ' p:=typeinfo(tvalue);',
  26933. '']);
  26934. ConvertProgram;
  26935. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  26936. LinesToStr([ // statements
  26937. 'this.p = null;',
  26938. '']),
  26939. LinesToStr([ // $mod.$main
  26940. '$mod.p = rtl.string;',
  26941. '$mod.p = rtl.string;',
  26942. '$mod.p = rtl.boolean;',
  26943. '$mod.p = rtl.boolean;',
  26944. '$mod.p = rtl.char;',
  26945. '$mod.p = rtl.char;',
  26946. '$mod.p = rtl.double;',
  26947. '$mod.p = rtl.double;',
  26948. '$mod.p = rtl.pointer;',
  26949. '$mod.p = rtl.pointer;',
  26950. '$mod.p = rtl.shortint;',
  26951. '$mod.p = rtl.shortint;',
  26952. '$mod.p = rtl.byte;',
  26953. '$mod.p = rtl.byte;',
  26954. '$mod.p = rtl.smallint;',
  26955. '$mod.p = rtl.smallint;',
  26956. '$mod.p = rtl.word;',
  26957. '$mod.p = rtl.word;',
  26958. '$mod.p = rtl.longword;',
  26959. '$mod.p = rtl.longword;',
  26960. '$mod.p = rtl.jsvalue;',
  26961. '$mod.p = rtl.jsvalue;',
  26962. '']));
  26963. end;
  26964. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  26965. begin
  26966. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26967. StartProgram(false);
  26968. Add([
  26969. 'type',
  26970. ' TCaption = type string;',
  26971. ' TYesNo = type boolean;',
  26972. ' TLetter = type char;',
  26973. ' TFloat = type double;',
  26974. ' TPtr = type pointer;',
  26975. ' TShortInt = type shortint;',
  26976. ' TByte = type byte;',
  26977. ' TSmallInt = type smallint;',
  26978. ' TWord = type word;',
  26979. ' TInt32 = type longint;',
  26980. ' TDWord = type longword;',
  26981. ' TValue = type jsvalue;',
  26982. ' TAliasValue = type TValue;',
  26983. 'var',
  26984. ' p: TPtr;',
  26985. ' a: TAliasValue;',
  26986. 'begin',
  26987. ' p:=typeinfo(tcaption);',
  26988. ' p:=typeinfo(tyesno);',
  26989. ' p:=typeinfo(tletter);',
  26990. ' p:=typeinfo(tfloat);',
  26991. ' p:=typeinfo(tptr);',
  26992. ' p:=typeinfo(tshortint);',
  26993. ' p:=typeinfo(tbyte);',
  26994. ' p:=typeinfo(tsmallint);',
  26995. ' p:=typeinfo(tword);',
  26996. ' p:=typeinfo(tdword);',
  26997. ' p:=typeinfo(tvalue);',
  26998. ' p:=typeinfo(taliasvalue);',
  26999. ' p:=typeinfo(a);',
  27000. '']);
  27001. ConvertProgram;
  27002. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  27003. LinesToStr([ // statements
  27004. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  27005. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  27006. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  27007. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  27008. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  27009. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  27010. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  27011. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  27012. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  27013. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  27014. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  27015. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  27016. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  27017. 'this.p = null;',
  27018. 'this.a = undefined;',
  27019. '']),
  27020. LinesToStr([ // $mod.$main
  27021. '$mod.p = $mod.$rtti["TCaption"];',
  27022. '$mod.p = $mod.$rtti["TYesNo"];',
  27023. '$mod.p = $mod.$rtti["TLetter"];',
  27024. '$mod.p = $mod.$rtti["TFloat"];',
  27025. '$mod.p = $mod.$rtti["TPtr"];',
  27026. '$mod.p = $mod.$rtti["TShortInt"];',
  27027. '$mod.p = $mod.$rtti["TByte"];',
  27028. '$mod.p = $mod.$rtti["TSmallInt"];',
  27029. '$mod.p = $mod.$rtti["TWord"];',
  27030. '$mod.p = $mod.$rtti["TDWord"];',
  27031. '$mod.p = $mod.$rtti["TValue"];',
  27032. '$mod.p = $mod.$rtti["TAliasValue"];',
  27033. '$mod.p = $mod.$rtti["TAliasValue"];',
  27034. '']));
  27035. end;
  27036. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  27037. begin
  27038. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27039. StartProgram(false);
  27040. Add('procedure DoIt;');
  27041. Add('type');
  27042. Add(' integer = longint;');
  27043. Add(' TPoint = record');
  27044. Add(' x,y: integer;');
  27045. Add(' end;');
  27046. Add('var p: pointer;');
  27047. Add('begin');
  27048. Add(' p:=typeinfo(tpoint);');
  27049. Add('end;');
  27050. Add('begin');
  27051. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  27052. ConvertProgram;
  27053. end;
  27054. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  27055. begin
  27056. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27057. StartProgram(false);
  27058. Add([
  27059. '{$modeswitch externalclass}',
  27060. 'type',
  27061. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27062. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  27063. ' TFlag = (up,down);',
  27064. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  27065. ' TFlags = set of TFlag;',
  27066. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  27067. 'var',
  27068. ' ti: TTypeInfo;',
  27069. ' tiInt: TTypeInfoInteger;',
  27070. ' tiEnum: TTypeInfoEnum;',
  27071. ' tiSet: TTypeInfoSet;',
  27072. 'begin',
  27073. ' ti:=typeinfo(string);',
  27074. ' ti:=typeinfo(boolean);',
  27075. ' ti:=typeinfo(char);',
  27076. ' ti:=typeinfo(double);',
  27077. ' tiInt:=typeinfo(shortint);',
  27078. ' tiInt:=typeinfo(byte);',
  27079. ' tiInt:=typeinfo(smallint);',
  27080. ' tiInt:=typeinfo(word);',
  27081. ' tiInt:=typeinfo(longint);',
  27082. ' tiInt:=typeinfo(longword);',
  27083. ' ti:=typeinfo(jsvalue);',
  27084. ' tiEnum:=typeinfo(tflag);',
  27085. ' tiSet:=typeinfo(tflags);']);
  27086. ConvertProgram;
  27087. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  27088. LinesToStr([ // statements
  27089. 'this.TFlag = {',
  27090. ' "0": "up",',
  27091. ' up: 0,',
  27092. ' "1": "down",',
  27093. ' down: 1',
  27094. '};',
  27095. '$mod.$rtti.$Enum("TFlag", {',
  27096. ' minvalue: 0,',
  27097. ' maxvalue: 1,',
  27098. ' ordtype: 1,',
  27099. ' enumtype: this.TFlag',
  27100. '});',
  27101. '$mod.$rtti.$Set("TFlags", {',
  27102. ' comptype: $mod.$rtti["TFlag"]',
  27103. '});',
  27104. 'this.ti = null;',
  27105. 'this.tiInt = null;',
  27106. 'this.tiEnum = null;',
  27107. 'this.tiSet = null;',
  27108. '']),
  27109. LinesToStr([ // $mod.$main
  27110. '$mod.ti = rtl.string;',
  27111. '$mod.ti = rtl.boolean;',
  27112. '$mod.ti = rtl.char;',
  27113. '$mod.ti = rtl.double;',
  27114. '$mod.tiInt = rtl.shortint;',
  27115. '$mod.tiInt = rtl.byte;',
  27116. '$mod.tiInt = rtl.smallint;',
  27117. '$mod.tiInt = rtl.word;',
  27118. '$mod.tiInt = rtl.longint;',
  27119. '$mod.tiInt = rtl.longword;',
  27120. '$mod.ti = rtl.jsvalue;',
  27121. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  27122. '$mod.tiSet = $mod.$rtti["TFlags"];',
  27123. '']));
  27124. end;
  27125. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  27126. begin
  27127. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27128. StartProgram(false);
  27129. Add('{$modeswitch externalclass}');
  27130. Add('type');
  27131. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  27132. Add(' TStaticArr = array[boolean] of string;');
  27133. Add(' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;');
  27134. Add(' TDynArr = array of string;');
  27135. Add(' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;');
  27136. Add(' TProc = procedure;');
  27137. Add(' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;');
  27138. Add(' TMethod = procedure of object;');
  27139. Add(' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;');
  27140. Add('var');
  27141. Add(' StaticArray: TStaticArr;');
  27142. Add(' tiStaticArray: TTypeInfoStaticArray;');
  27143. Add(' DynArray: TDynArr;');
  27144. Add(' tiDynArray: TTypeInfoDynArray;');
  27145. Add(' ProcVar: TProc;');
  27146. Add(' tiProcVar: TTypeInfoProcVar;');
  27147. Add(' MethodVar: TMethod;');
  27148. Add(' tiMethodVar: TTypeInfoMethodVar;');
  27149. Add('begin');
  27150. Add(' tiStaticArray:=typeinfo(StaticArray);');
  27151. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  27152. Add(' tiDynArray:=typeinfo(DynArray);');
  27153. Add(' tiDynArray:=typeinfo(TDynArr);');
  27154. Add(' tiProcVar:=typeinfo(ProcVar);');
  27155. Add(' tiProcVar:=typeinfo(TProc);');
  27156. Add(' tiMethodVar:=typeinfo(MethodVar);');
  27157. Add(' tiMethodVar:=typeinfo(TMethod);');
  27158. ConvertProgram;
  27159. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  27160. LinesToStr([ // statements
  27161. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  27162. ' dims: [2],',
  27163. ' eltype: rtl.string',
  27164. '});',
  27165. '$mod.$rtti.$DynArray("TDynArr", {',
  27166. ' eltype: rtl.string',
  27167. '});',
  27168. '$mod.$rtti.$ProcVar("TProc", {',
  27169. ' procsig: rtl.newTIProcSig(null)',
  27170. '});',
  27171. '$mod.$rtti.$MethodVar("TMethod", {',
  27172. ' procsig: rtl.newTIProcSig(null),',
  27173. ' methodkind: 0',
  27174. '});',
  27175. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  27176. 'this.tiStaticArray = null;',
  27177. 'this.DynArray = [];',
  27178. 'this.tiDynArray = null;',
  27179. 'this.ProcVar = null;',
  27180. 'this.tiProcVar = null;',
  27181. 'this.MethodVar = null;',
  27182. 'this.tiMethodVar = null;',
  27183. '']),
  27184. LinesToStr([ // $mod.$main
  27185. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  27186. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  27187. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  27188. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  27189. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  27190. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  27191. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  27192. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  27193. '']));
  27194. end;
  27195. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  27196. begin
  27197. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27198. StartProgram(false);
  27199. Add('{$modeswitch externalclass}');
  27200. Add('type');
  27201. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  27202. Add(' TRec = record end;');
  27203. Add(' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;');
  27204. // ToDo: ^PRec
  27205. Add(' TObject = class end;');
  27206. Add(' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;');
  27207. Add(' TClass = class of tobject;');
  27208. Add(' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;');
  27209. Add(' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;');
  27210. Add('var');
  27211. Add(' Rec: trec;');
  27212. Add(' tiRecord: ttypeinforecord;');
  27213. Add(' Obj: tobject;');
  27214. Add(' tiClass: ttypeinfoclass;');
  27215. Add(' aClass: tclass;');
  27216. Add(' tiClassRef: ttypeinfoclassref;');
  27217. // ToDo: ^PRec
  27218. Add(' tiPointer: ttypeinfopointer;');
  27219. Add('begin');
  27220. Add(' tirecord:=typeinfo(trec);');
  27221. Add(' tirecord:=typeinfo(trec);');
  27222. Add(' ticlass:=typeinfo(obj);');
  27223. Add(' ticlass:=typeinfo(tobject);');
  27224. Add(' ticlass:=typeinfo(aclass);');
  27225. Add(' ticlassref:=typeinfo(tclass);');
  27226. ConvertProgram;
  27227. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  27228. LinesToStr([ // statements
  27229. 'rtl.recNewT($mod, "TRec", function () {',
  27230. ' this.$eq = function (b) {',
  27231. ' return true;',
  27232. ' };',
  27233. ' this.$assign = function (s) {',
  27234. ' return this;',
  27235. ' };',
  27236. ' $mod.$rtti.$Record("TRec", {});',
  27237. '});',
  27238. 'rtl.createClass($mod, "TObject", null, function () {',
  27239. ' this.$init = function () {',
  27240. ' };',
  27241. ' this.$final = function () {',
  27242. ' };',
  27243. '});',
  27244. '$mod.$rtti.$ClassRef("TClass", {',
  27245. ' instancetype: $mod.$rtti["TObject"]',
  27246. '});',
  27247. 'this.Rec = $mod.TRec.$new();',
  27248. 'this.tiRecord = null;',
  27249. 'this.Obj = null;',
  27250. 'this.tiClass = null;',
  27251. 'this.aClass = null;',
  27252. 'this.tiClassRef = null;',
  27253. 'this.tiPointer = null;',
  27254. '']),
  27255. LinesToStr([ // $mod.$main
  27256. '$mod.tiRecord = $mod.$rtti["TRec"];',
  27257. '$mod.tiRecord = $mod.$rtti["TRec"];',
  27258. '$mod.tiClass = $mod.Obj.$rtti;',
  27259. '$mod.tiClass = $mod.$rtti["TObject"];',
  27260. '$mod.tiClass = $mod.aClass.$rtti;',
  27261. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  27262. '']));
  27263. end;
  27264. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  27265. begin
  27266. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27267. StartProgram(false);
  27268. Add([
  27269. '{$modeswitch externalclass}',
  27270. 'type',
  27271. ' TClass = class of tobject;',
  27272. ' TObject = class',
  27273. ' function MyClass: TClass;',
  27274. ' class function ClassType: TClass;',
  27275. ' end;',
  27276. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27277. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  27278. 'function TObject.MyClass: TClass;',
  27279. 'var t: TTypeInfoClass;',
  27280. 'begin',
  27281. ' t:=TypeInfo(Self);',
  27282. ' t:=TypeInfo(Result);',
  27283. ' t:=TypeInfo(TObject);',
  27284. 'end;',
  27285. 'class function TObject.ClassType: TClass;',
  27286. 'var t: TTypeInfoClass;',
  27287. 'begin',
  27288. ' t:=TypeInfo(Self);',
  27289. ' t:=TypeInfo(Result);',
  27290. 'end;',
  27291. 'var',
  27292. ' Obj: TObject;',
  27293. ' t: TTypeInfoClass;',
  27294. 'begin',
  27295. ' t:=TypeInfo(TObject.ClassType);',
  27296. ' t:=TypeInfo(Obj.ClassType);',
  27297. ' t:=TypeInfo(Obj.MyClass);',
  27298. '']);
  27299. ConvertProgram;
  27300. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  27301. LinesToStr([ // statements
  27302. '$mod.$rtti.$Class("TObject");',
  27303. '$mod.$rtti.$ClassRef("TClass", {',
  27304. ' instancetype: $mod.$rtti["TObject"]',
  27305. '});',
  27306. 'rtl.createClass($mod, "TObject", null, function () {',
  27307. ' this.$init = function () {',
  27308. ' };',
  27309. ' this.$final = function () {',
  27310. ' };',
  27311. ' this.MyClass = function () {',
  27312. ' var Result = null;',
  27313. ' var t = null;',
  27314. ' t = this.$rtti;',
  27315. ' t = Result.$rtti;',
  27316. ' t = $mod.$rtti["TObject"];',
  27317. ' return Result;',
  27318. ' };',
  27319. ' this.ClassType = function () {',
  27320. ' var Result = null;',
  27321. ' var t = null;',
  27322. ' t = this.$rtti;',
  27323. ' t = Result.$rtti;',
  27324. ' return Result;',
  27325. ' };',
  27326. '});',
  27327. 'this.Obj = null;',
  27328. 'this.t = null;',
  27329. '']),
  27330. LinesToStr([ // $mod.$main
  27331. '$mod.t = $mod.TObject.ClassType().$rtti;',
  27332. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  27333. '$mod.t = $mod.Obj.MyClass().$rtti;',
  27334. '']));
  27335. end;
  27336. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  27337. begin
  27338. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27339. AddModuleWithIntfImplSrc('typinfo.pas',
  27340. LinesToStr([
  27341. '{$modeswitch externalclass}',
  27342. 'type',
  27343. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27344. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  27345. '']),
  27346. '');
  27347. AddModuleWithIntfImplSrc('unit2.pas',
  27348. LinesToStr([
  27349. 'uses typinfo;',
  27350. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  27351. 'procedure DoPtr(p: PTypeInfo);',
  27352. 'procedure DoInfo(t: TTypeInfo);',
  27353. 'procedure DoInt(t: TTypeInfoInteger);',
  27354. '']),
  27355. LinesToStr([
  27356. 'procedure DoPtr(p: PTypeInfo);',
  27357. 'begin end;',
  27358. 'procedure DoInfo(t: TTypeInfo);',
  27359. 'begin end;',
  27360. 'procedure DoInt(t: TTypeInfoInteger);',
  27361. 'begin end;',
  27362. '']));
  27363. StartUnit(true);
  27364. Add([
  27365. 'interface',
  27366. 'uses unit2;', // does not use unit typinfo
  27367. 'implementation',
  27368. 'var',
  27369. ' i: byte;',
  27370. ' p: pointer;',
  27371. ' t: PTypeInfo;',
  27372. 'initialization',
  27373. ' p:=typeinfo(i);',
  27374. ' t:=typeinfo(i);',
  27375. ' if p=t then ;',
  27376. ' if p=typeinfo(i) then ;',
  27377. ' if typeinfo(i)=p then ;',
  27378. ' if t=typeinfo(i) then ;',
  27379. ' if typeinfo(i)=t then ;',
  27380. ' DoPtr(p);',
  27381. ' DoPtr(t);',
  27382. ' DoPtr(typeinfo(i));',
  27383. ' DoInfo(p);',
  27384. ' DoInfo(t);',
  27385. ' DoInfo(typeinfo(i));',
  27386. ' DoInt(typeinfo(i));',
  27387. '']);
  27388. ConvertUnit;
  27389. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  27390. LinesToStr([ // statements
  27391. 'var $impl = $mod.$impl;',
  27392. '']),
  27393. LinesToStr([ // this.$init
  27394. '$impl.p = rtl.byte;',
  27395. '$impl.t = rtl.byte;',
  27396. 'if ($impl.p === $impl.t) ;',
  27397. 'if ($impl.p === rtl.byte) ;',
  27398. 'if (rtl.byte === $impl.p) ;',
  27399. 'if ($impl.t === rtl.byte) ;',
  27400. 'if (rtl.byte === $impl.t) ;',
  27401. 'pas.unit2.DoPtr($impl.p);',
  27402. 'pas.unit2.DoPtr($impl.t);',
  27403. 'pas.unit2.DoPtr(rtl.byte);',
  27404. 'pas.unit2.DoInfo($impl.p);',
  27405. 'pas.unit2.DoInfo($impl.t);',
  27406. 'pas.unit2.DoInfo(rtl.byte);',
  27407. 'pas.unit2.DoInt(rtl.byte);',
  27408. '']),
  27409. LinesToStr([ // implementation
  27410. '$impl.i = 0;',
  27411. '$impl.p = null;',
  27412. '$impl.t = null;',
  27413. '']) );
  27414. end;
  27415. procedure TTestModule.TestRTTI_Interface_Corba;
  27416. begin
  27417. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27418. StartProgram(false);
  27419. Add([
  27420. '{$interfaces corba}',
  27421. '{$modeswitch externalclass}',
  27422. 'type',
  27423. ' IUnknown = interface',
  27424. ' end;',
  27425. ' IBird = interface',
  27426. ' function GetItem: longint;',
  27427. ' procedure SetItem(Value: longint);',
  27428. ' property Item: longint read GetItem write SetItem;',
  27429. ' end;',
  27430. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27431. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  27432. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  27433. 'var',
  27434. ' i: IBird;',
  27435. ' t: TTypeInfoInterface;',
  27436. 'begin',
  27437. ' t:=TypeInfo(IBird);',
  27438. ' t:=TypeInfo(i);',
  27439. ' DoIt(t);',
  27440. ' DoIt(TypeInfo(IBird));',
  27441. '']);
  27442. ConvertProgram;
  27443. CheckSource('TestRTTI_Interface_Corba',
  27444. LinesToStr([ // statements
  27445. 'rtl.createInterface(',
  27446. ' $mod,',
  27447. ' "IUnknown",',
  27448. ' "{B92D5841-758A-322B-B800-000000000000}",',
  27449. ' [],',
  27450. ' null,',
  27451. ' function () {',
  27452. ' }',
  27453. ');',
  27454. 'rtl.createInterface(',
  27455. ' $mod,',
  27456. ' "IBird",',
  27457. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  27458. ' ["GetItem", "SetItem"],',
  27459. ' null,',
  27460. ' function () {',
  27461. ' var $r = this.$rtti;',
  27462. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  27463. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  27464. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  27465. ' }',
  27466. ');',
  27467. 'this.DoIt = function (t) {',
  27468. '}; ',
  27469. 'this.i = null;',
  27470. 'this.t = null;',
  27471. '']),
  27472. LinesToStr([ // $mod.$main
  27473. '$mod.t = $mod.$rtti["IBird"];',
  27474. '$mod.t = $mod.i.$rtti;',
  27475. '$mod.DoIt($mod.t);',
  27476. '$mod.DoIt($mod.$rtti["IBird"]);',
  27477. '']));
  27478. end;
  27479. procedure TTestModule.TestRTTI_Interface_COM;
  27480. begin
  27481. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27482. StartProgram(false);
  27483. Add([
  27484. '{$interfaces com}',
  27485. '{$modeswitch externalclass}',
  27486. 'type',
  27487. ' TGuid = record end;',
  27488. ' integer = longint;',
  27489. ' IUnknown = interface',
  27490. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  27491. ' function _AddRef: Integer;',
  27492. ' function _Release: Integer;',
  27493. ' end;',
  27494. ' IBird = interface',
  27495. ' function GetItem: longint;',
  27496. ' procedure SetItem(Value: longint);',
  27497. ' property Item: longint read GetItem write SetItem;',
  27498. ' end;',
  27499. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27500. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  27501. 'var',
  27502. ' i: IBird;',
  27503. ' t: TTypeInfoInterface;',
  27504. 'begin',
  27505. ' t:=TypeInfo(IBird);',
  27506. ' t:=TypeInfo(i);',
  27507. '']);
  27508. ConvertProgram;
  27509. CheckSource('TestRTTI_Interface_COM',
  27510. LinesToStr([ // statements
  27511. 'rtl.recNewT($mod, "TGuid", function () {',
  27512. ' this.$eq = function (b) {',
  27513. ' return true;',
  27514. ' };',
  27515. ' this.$assign = function (s) {',
  27516. ' return this;',
  27517. ' };',
  27518. ' $mod.$rtti.$Record("TGuid", {});',
  27519. '});',
  27520. 'rtl.createInterface(',
  27521. ' $mod,',
  27522. ' "IUnknown",',
  27523. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  27524. ' ["QueryInterface", "_AddRef", "_Release"],',
  27525. ' null,',
  27526. ' function () {',
  27527. ' this.$kind = "com";',
  27528. ' var $r = this.$rtti;',
  27529. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  27530. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  27531. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  27532. ' }',
  27533. ');',
  27534. 'rtl.createInterface(',
  27535. ' $mod,',
  27536. ' "IBird",',
  27537. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  27538. ' ["GetItem", "SetItem"],',
  27539. ' $mod.IUnknown,',
  27540. ' function () {',
  27541. ' var $r = this.$rtti;',
  27542. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  27543. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  27544. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  27545. ' }',
  27546. ');',
  27547. 'this.i = null;',
  27548. 'this.t = null;',
  27549. '']),
  27550. LinesToStr([ // $mod.$main
  27551. '$mod.t = $mod.$rtti["IBird"];',
  27552. '$mod.t = $mod.i.$rtti;',
  27553. '']));
  27554. end;
  27555. procedure TTestModule.TestRTTI_ClassHelper;
  27556. begin
  27557. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27558. StartProgram(false);
  27559. Add([
  27560. '{$interfaces com}',
  27561. '{$modeswitch externalclass}',
  27562. 'type',
  27563. ' TObject = class',
  27564. ' end;',
  27565. ' THelper = class helper for TObject',
  27566. ' published',
  27567. ' function GetItem: longint;',
  27568. ' property Item: longint read GetItem;',
  27569. ' end;',
  27570. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27571. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  27572. 'function THelper.GetItem: longint;',
  27573. 'begin',
  27574. 'end;',
  27575. 'var',
  27576. ' t: TTypeInfoHelper;',
  27577. 'begin',
  27578. ' t:=TypeInfo(THelper);',
  27579. '']);
  27580. ConvertProgram;
  27581. CheckSource('TestRTTI_ClassHelper',
  27582. LinesToStr([ // statements
  27583. 'rtl.createClass($mod, "TObject", null, function () {',
  27584. ' this.$init = function () {',
  27585. ' };',
  27586. ' this.$final = function () {',
  27587. ' };',
  27588. '});',
  27589. 'rtl.createHelper($mod, "THelper", null, function () {',
  27590. ' this.GetItem = function () {',
  27591. ' var Result = 0;',
  27592. ' return Result;',
  27593. ' };',
  27594. ' var $r = this.$rtti;',
  27595. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  27596. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  27597. '});',
  27598. 'this.t = null;',
  27599. '']),
  27600. LinesToStr([ // $mod.$main
  27601. '$mod.t = $mod.$rtti["THelper"];',
  27602. '']));
  27603. end;
  27604. procedure TTestModule.TestResourcestringProgram;
  27605. begin
  27606. StartProgram(false);
  27607. Add([
  27608. 'const Bar = ''bar'';',
  27609. 'resourcestring',
  27610. ' Red = ''red'';',
  27611. ' Foobar = ''fOo''+bar;',
  27612. 'var s: string;',
  27613. ' c: char;',
  27614. 'begin',
  27615. ' s:=red;',
  27616. ' s:=test1.red;',
  27617. ' c:=red[1];',
  27618. ' c:=test1.red[2];',
  27619. ' if red=foobar then ;',
  27620. ' if red[3]=red[4] then ;']);
  27621. ConvertProgram;
  27622. CheckSource('TestResourcestringProgram',
  27623. LinesToStr([ // statements
  27624. 'this.Bar = "bar";',
  27625. 'this.s = "";',
  27626. 'this.c = "";',
  27627. '$mod.$resourcestrings = {',
  27628. ' Red: {',
  27629. ' org: "red"',
  27630. ' },',
  27631. ' Foobar: {',
  27632. ' org: "fOobar"',
  27633. ' }',
  27634. '};',
  27635. '']),
  27636. LinesToStr([ // $mod.$main
  27637. '$mod.s = rtl.getResStr(pas.program, "Red");',
  27638. '$mod.s = rtl.getResStr(pas.program, "Red");',
  27639. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
  27640. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
  27641. 'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
  27642. 'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
  27643. '']));
  27644. end;
  27645. procedure TTestModule.TestResourcestringUnit;
  27646. begin
  27647. StartUnit(false);
  27648. Add([
  27649. 'interface',
  27650. 'const Red = ''rEd'';',
  27651. 'resourcestring',
  27652. ' Blue = ''blue'';',
  27653. ' NotRed = ''not''+Red;',
  27654. 'var s: string;',
  27655. 'implementation',
  27656. 'resourcestring',
  27657. ' ImplGreen = ''green'';',
  27658. 'initialization',
  27659. ' s:=blue+ImplGreen;',
  27660. ' s:=test1.blue+test1.implgreen;',
  27661. ' s:=blue[1]+implgreen[2];']);
  27662. ConvertUnit;
  27663. CheckSource('TestResourcestringUnit',
  27664. LinesToStr([ // statements
  27665. 'this.Red = "rEd";',
  27666. 'this.s = "";',
  27667. '$mod.$resourcestrings = {',
  27668. ' Blue: {',
  27669. ' org: "blue"',
  27670. ' },',
  27671. ' NotRed: {',
  27672. ' org: "notrEd"',
  27673. ' },',
  27674. ' ImplGreen: {',
  27675. ' org: "green"',
  27676. ' }',
  27677. '};',
  27678. '']),
  27679. LinesToStr([ // $mod.$main
  27680. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  27681. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  27682. '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
  27683. '']));
  27684. end;
  27685. procedure TTestModule.TestResourcestringImplementation;
  27686. begin
  27687. StartUnit(false);
  27688. Add([
  27689. 'interface',
  27690. 'implementation',
  27691. 'resourcestring',
  27692. ' ImplRed = ''red'';']);
  27693. ConvertUnit;
  27694. CheckSource('TestResourcestringImplementation',
  27695. LinesToStr([ // intf statements
  27696. 'var $impl = $mod.$impl;']),
  27697. LinesToStr([ // $mod.$init
  27698. '']),
  27699. LinesToStr([ // impl statements
  27700. '$mod.$resourcestrings = {',
  27701. ' ImplRed: {',
  27702. ' org: "red"',
  27703. ' }',
  27704. '};',
  27705. '']));
  27706. end;
  27707. procedure TTestModule.TestAtributes_Ignore;
  27708. begin
  27709. StartProgram(false);
  27710. Add([
  27711. '{$modeswitch ignoreattributes}',
  27712. 'type',
  27713. ' [custom1, custom2(1+3,''foo'')] [mod1.custom3]',
  27714. ' TObject = class',
  27715. ' [custom5()] FS: string;',
  27716. ' [customProp] property S: string read FS;',
  27717. ' end;',
  27718. 'var',
  27719. ' [custom6]',
  27720. ' o: TObject;',
  27721. 'begin',
  27722. '']);
  27723. ConvertProgram;
  27724. CheckSource('TestAtributes_Ignore',
  27725. LinesToStr([ // statements
  27726. 'rtl.createClass($mod, "TObject", null, function () {',
  27727. ' this.$init = function () {',
  27728. ' this.FS = "";',
  27729. ' };',
  27730. ' this.$final = function () {',
  27731. ' };',
  27732. '});',
  27733. 'this.o = null;',
  27734. '']),
  27735. LinesToStr([ // $mod.$main
  27736. '']));
  27737. end;
  27738. procedure TTestModule.TestAssert;
  27739. begin
  27740. StartProgram(false);
  27741. Add([
  27742. 'procedure DoIt;',
  27743. 'var',
  27744. ' b: boolean;',
  27745. ' s: string;',
  27746. 'begin',
  27747. ' {$Assertions on}',
  27748. ' Assert(b);',
  27749. 'end;',
  27750. 'begin',
  27751. ' DoIt;',
  27752. '']);
  27753. ConvertProgram;
  27754. CheckSource('TestAssert',
  27755. LinesToStr([ // statements
  27756. 'this.DoIt = function () {',
  27757. ' var b = false;',
  27758. ' var s = "";',
  27759. ' if (!b) throw "assert failed";',
  27760. '};',
  27761. '']),
  27762. LinesToStr([ // $mod.$main
  27763. '$mod.DoIt();',
  27764. '']));
  27765. end;
  27766. procedure TTestModule.TestAssert_SysUtils;
  27767. begin
  27768. AddModuleWithIntfImplSrc('SysUtils.pas',
  27769. LinesToStr([
  27770. 'type',
  27771. ' TObject = class',
  27772. ' constructor Create;',
  27773. ' end;',
  27774. ' EAssertionFailed = class',
  27775. ' constructor Create(s: string);',
  27776. ' end;',
  27777. '']),
  27778. LinesToStr([
  27779. 'constructor TObject.Create;',
  27780. 'begin end;',
  27781. 'constructor EAssertionFailed.Create(s: string);',
  27782. 'begin end;',
  27783. '']) );
  27784. StartProgram(true);
  27785. Add([
  27786. 'uses sysutils;',
  27787. 'procedure DoIt;',
  27788. 'var',
  27789. ' b: boolean;',
  27790. ' s: string;',
  27791. 'begin',
  27792. ' {$Assertions on}',
  27793. ' Assert(b);',
  27794. ' Assert(b,''msg'');',
  27795. 'end;',
  27796. 'begin',
  27797. ' DoIt;',
  27798. '']);
  27799. ConvertProgram;
  27800. CheckSource('TestAssert_SysUtils',
  27801. LinesToStr([ // statements
  27802. 'this.DoIt = function () {',
  27803. ' var b = false;',
  27804. ' var s = "";',
  27805. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  27806. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  27807. '};',
  27808. '']),
  27809. LinesToStr([ // $mod.$main
  27810. '$mod.DoIt();',
  27811. '']));
  27812. end;
  27813. procedure TTestModule.TestObjectChecks;
  27814. begin
  27815. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  27816. StartProgram(false);
  27817. Add([
  27818. 'type',
  27819. ' TObject = class',
  27820. ' procedure DoIt;',
  27821. ' end;',
  27822. ' TClass = class of tobject;',
  27823. ' TBird = class',
  27824. ' end;',
  27825. ' TBirdClass = class of TBird;',
  27826. 'var',
  27827. ' o : TObject;',
  27828. ' c: TClass;',
  27829. ' b: TBird;',
  27830. ' bc: TBirdClass;',
  27831. 'procedure TObject.DoIt;',
  27832. 'begin',
  27833. ' b:=TBird(o);',
  27834. 'end;',
  27835. 'begin',
  27836. ' o.DoIt;',
  27837. ' b:=TBird(o);',
  27838. ' bc:=TBirdClass(c);',
  27839. '']);
  27840. ConvertProgram;
  27841. CheckSource('TestCheckMethodCall',
  27842. LinesToStr([ // statements
  27843. 'rtl.createClass($mod, "TObject", null, function () {',
  27844. ' this.$init = function () {',
  27845. ' };',
  27846. ' this.$final = function () {',
  27847. ' };',
  27848. ' this.DoIt = function () {',
  27849. ' rtl.checkMethodCall(this,$mod.TObject);',
  27850. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  27851. ' };',
  27852. '});',
  27853. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  27854. '});',
  27855. 'this.o = null;',
  27856. 'this.c = null;',
  27857. 'this.b = null;',
  27858. 'this.bc = null;',
  27859. '']),
  27860. LinesToStr([ // $mod.$main
  27861. '$mod.o.DoIt();',
  27862. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  27863. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  27864. '']));
  27865. end;
  27866. procedure TTestModule.TestRangeChecks_AssignInt;
  27867. begin
  27868. Scanner.Options:=Scanner.Options+[po_CAssignments];
  27869. StartProgram(false);
  27870. Add([
  27871. '{$R+}',
  27872. 'var',
  27873. ' b: byte = 2;',
  27874. ' w: word = 3;',
  27875. 'procedure DoIt(p: byte);',
  27876. 'begin',
  27877. ' b:=w;',
  27878. ' b+=w;',
  27879. ' b:=1;',
  27880. 'end;',
  27881. '{$R-}',
  27882. 'procedure DoSome;',
  27883. 'begin',
  27884. ' DoIt(w);',
  27885. ' b:=w;',
  27886. ' b:=2;',
  27887. 'end;',
  27888. 'begin',
  27889. '{$R+}',
  27890. '']);
  27891. ConvertProgram;
  27892. CheckSource('TestRangeChecks_AssignInt',
  27893. LinesToStr([ // statements
  27894. 'this.b = 2;',
  27895. 'this.w = 3;',
  27896. 'this.DoIt = function (p) {',
  27897. ' rtl.rc(p, 0, 255);',
  27898. ' $mod.b = rtl.rc($mod.w,0,255);',
  27899. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  27900. ' $mod.b = 1;',
  27901. '};',
  27902. 'this.DoSome = function () {',
  27903. ' $mod.DoIt($mod.w);',
  27904. ' $mod.b = $mod.w;',
  27905. ' $mod.b = 2;',
  27906. '};',
  27907. '']),
  27908. LinesToStr([ // $mod.$main
  27909. '']));
  27910. end;
  27911. procedure TTestModule.TestRangeChecks_AssignIntRange;
  27912. begin
  27913. Scanner.Options:=Scanner.Options+[po_CAssignments];
  27914. StartProgram(false);
  27915. Add([
  27916. '{$R+}',
  27917. 'type Ten = 1..10;',
  27918. 'var',
  27919. ' b: Ten = 2;',
  27920. ' w: Ten = 3;',
  27921. 'procedure DoIt(p: Ten);',
  27922. 'begin',
  27923. ' b:=w;',
  27924. ' b+=w;',
  27925. ' b:=1;',
  27926. 'end;',
  27927. '{$R-}',
  27928. 'procedure DoSome;',
  27929. 'begin',
  27930. ' DoIt(w);',
  27931. ' b:=w;',
  27932. ' b:=2;',
  27933. 'end;',
  27934. 'begin',
  27935. '{$R+}',
  27936. '']);
  27937. ConvertProgram;
  27938. CheckSource('TestRangeChecks_AssignIntRange',
  27939. LinesToStr([ // statements
  27940. 'this.b = 2;',
  27941. 'this.w = 3;',
  27942. 'this.DoIt = function (p) {',
  27943. ' rtl.rc(p, 1, 10);',
  27944. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  27945. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  27946. ' $mod.b = 1;',
  27947. '};',
  27948. 'this.DoSome = function () {',
  27949. ' $mod.DoIt($mod.w);',
  27950. ' $mod.b = $mod.w;',
  27951. ' $mod.b = 2;',
  27952. '};',
  27953. '']),
  27954. LinesToStr([ // $mod.$main
  27955. '']));
  27956. end;
  27957. procedure TTestModule.TestRangeChecks_AssignEnum;
  27958. begin
  27959. StartProgram(false);
  27960. Add([
  27961. '{$R+}',
  27962. 'type TEnum = (red,green);',
  27963. 'var',
  27964. ' e: TEnum = red;',
  27965. 'procedure DoIt(p: TEnum);',
  27966. 'begin',
  27967. ' e:=p;',
  27968. ' p:=TEnum(0);',
  27969. ' p:=succ(e);',
  27970. 'end;',
  27971. '{$R-}',
  27972. 'procedure DoSome;',
  27973. 'begin',
  27974. ' DoIt(e);',
  27975. ' e:=TEnum(1);',
  27976. ' e:=pred(e);',
  27977. 'end;',
  27978. 'begin',
  27979. '{$R+}',
  27980. '']);
  27981. ConvertProgram;
  27982. CheckSource('TestRangeChecks_AssignEnum',
  27983. LinesToStr([ // statements
  27984. 'this.TEnum = {',
  27985. ' "0": "red",',
  27986. ' red: 0,',
  27987. ' "1": "green",',
  27988. ' green: 1',
  27989. '};',
  27990. 'this.e = $mod.TEnum.red;',
  27991. 'this.DoIt = function (p) {',
  27992. ' rtl.rc(p, 0, 1);',
  27993. ' $mod.e = rtl.rc(p, 0, 1);',
  27994. ' p = 0;',
  27995. ' p = rtl.rc($mod.e + 1, 0, 1);',
  27996. '};',
  27997. 'this.DoSome = function () {',
  27998. ' $mod.DoIt($mod.e);',
  27999. ' $mod.e = 1;',
  28000. ' $mod.e = $mod.e - 1;',
  28001. '};',
  28002. '']),
  28003. LinesToStr([ // $mod.$main
  28004. '']));
  28005. end;
  28006. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  28007. begin
  28008. StartProgram(false);
  28009. Add([
  28010. '{$R+}',
  28011. 'type',
  28012. ' TEnum = (red,green);',
  28013. ' TEnumRg = red..green;',
  28014. 'var',
  28015. ' e: TEnumRg = red;',
  28016. 'procedure DoIt(p: TEnumRg);',
  28017. 'begin',
  28018. ' e:=p;',
  28019. ' p:=TEnumRg(0);',
  28020. ' p:=succ(e);',
  28021. 'end;',
  28022. '{$R-}',
  28023. 'procedure DoSome;',
  28024. 'begin',
  28025. ' DoIt(e);',
  28026. ' e:=TEnum(1);',
  28027. ' e:=pred(e);',
  28028. 'end;',
  28029. 'begin',
  28030. '{$R+}',
  28031. '']);
  28032. ConvertProgram;
  28033. CheckSource('TestRangeChecks_AssignEnumRange',
  28034. LinesToStr([ // statements
  28035. 'this.TEnum = {',
  28036. ' "0": "red",',
  28037. ' red: 0,',
  28038. ' "1": "green",',
  28039. ' green: 1',
  28040. '};',
  28041. 'this.e = $mod.TEnum.red;',
  28042. 'this.DoIt = function (p) {',
  28043. ' rtl.rc(p, 0, 1);',
  28044. ' $mod.e = rtl.rc(p, 0, 1);',
  28045. ' p = 0;',
  28046. ' p = rtl.rc($mod.e + 1, 0, 1);',
  28047. '};',
  28048. 'this.DoSome = function () {',
  28049. ' $mod.DoIt($mod.e);',
  28050. ' $mod.e = 1;',
  28051. ' $mod.e = $mod.e - 1;',
  28052. '};',
  28053. '']),
  28054. LinesToStr([ // $mod.$main
  28055. '']));
  28056. end;
  28057. procedure TTestModule.TestRangeChecks_AssignChar;
  28058. begin
  28059. StartProgram(false);
  28060. Add([
  28061. '{$R+}',
  28062. 'type',
  28063. ' TLetter = char;',
  28064. 'var',
  28065. ' b: TLetter = ''2'';',
  28066. ' w: TLetter = ''3'';',
  28067. 'procedure DoIt(p: TLetter);',
  28068. 'begin',
  28069. ' b:=w;',
  28070. ' b:=''1'';',
  28071. 'end;',
  28072. '{$R-}',
  28073. 'procedure DoSome;',
  28074. 'begin',
  28075. ' DoIt(w);',
  28076. ' b:=w;',
  28077. ' b:=''2'';',
  28078. 'end;',
  28079. 'begin',
  28080. '{$R+}',
  28081. '']);
  28082. ConvertProgram;
  28083. CheckSource('TestRangeChecks_AssignChar',
  28084. LinesToStr([ // statements
  28085. 'this.b = "2";',
  28086. 'this.w = "3";',
  28087. 'this.DoIt = function (p) {',
  28088. ' rtl.rcc(p, 0, 65535);',
  28089. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  28090. ' $mod.b = "1";',
  28091. '};',
  28092. 'this.DoSome = function () {',
  28093. ' $mod.DoIt($mod.w);',
  28094. ' $mod.b = $mod.w;',
  28095. ' $mod.b = "2";',
  28096. '};',
  28097. '']),
  28098. LinesToStr([ // $mod.$main
  28099. '']));
  28100. end;
  28101. procedure TTestModule.TestRangeChecks_AssignCharRange;
  28102. begin
  28103. StartProgram(false);
  28104. Add([
  28105. '{$R+}',
  28106. 'type TDigit = ''0''..''9'';',
  28107. 'var',
  28108. ' b: TDigit = ''2'';',
  28109. ' w: TDigit = ''3'';',
  28110. 'procedure DoIt(p: TDigit);',
  28111. 'begin',
  28112. ' b:=w;',
  28113. ' b:=''1'';',
  28114. 'end;',
  28115. '{$R-}',
  28116. 'procedure DoSome;',
  28117. 'begin',
  28118. ' DoIt(w);',
  28119. ' b:=w;',
  28120. ' b:=''2'';',
  28121. 'end;',
  28122. 'begin',
  28123. '{$R+}',
  28124. '']);
  28125. ConvertProgram;
  28126. CheckSource('TestRangeChecks_AssignCharRange',
  28127. LinesToStr([ // statements
  28128. 'this.b = "2";',
  28129. 'this.w = "3";',
  28130. 'this.DoIt = function (p) {',
  28131. ' rtl.rcc(p, 48, 57);',
  28132. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  28133. ' $mod.b = "1";',
  28134. '};',
  28135. 'this.DoSome = function () {',
  28136. ' $mod.DoIt($mod.w);',
  28137. ' $mod.b = $mod.w;',
  28138. ' $mod.b = "2";',
  28139. '};',
  28140. '']),
  28141. LinesToStr([ // $mod.$main
  28142. '']));
  28143. end;
  28144. procedure TTestModule.TestRangeChecks_ArrayIndex;
  28145. begin
  28146. StartProgram(false);
  28147. Add([
  28148. '{$R+}',
  28149. 'type',
  28150. ' Ten = 1..10;',
  28151. ' TArr = array of Ten;',
  28152. ' TArrArr = array of TArr;',
  28153. ' TArrByte = array[byte] of Ten;',
  28154. ' TArrChar = array[''0''..''9''] of Ten;',
  28155. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  28156. ' TObject = class',
  28157. ' A: TArr;',
  28158. ' end;',
  28159. 'procedure DoIt;',
  28160. 'var',
  28161. ' Arr: TArr;',
  28162. ' ArrArr: TArrArr;',
  28163. ' ArrByte: TArrByte;',
  28164. ' ArrChar: TArrChar;',
  28165. ' ArrByteChar: TArrByteChar;',
  28166. ' i: Ten;',
  28167. ' c: char;',
  28168. ' o: tobject;',
  28169. 'begin',
  28170. ' i:=Arr[1];',
  28171. ' i:=ArrByteChar[1,''2''];',
  28172. ' Arr[1]:=Arr[1];',
  28173. ' Arr[i]:=Arr[i];',
  28174. ' ArrByte[3]:=ArrByte[3];',
  28175. ' ArrByte[i]:=ArrByte[i];',
  28176. ' ArrChar[''5'']:=ArrChar[''5''];',
  28177. ' ArrChar[c]:=ArrChar[c];',
  28178. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  28179. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  28180. ' o.a[i]:=o.a[i];',
  28181. 'end;',
  28182. 'begin',
  28183. '']);
  28184. ConvertProgram;
  28185. CheckSource('TestRangeChecks_ArrayIndex',
  28186. LinesToStr([ // statements
  28187. 'rtl.createClass($mod, "TObject", null, function () {',
  28188. ' this.$init = function () {',
  28189. ' this.A = [];',
  28190. ' };',
  28191. ' this.$final = function () {',
  28192. ' this.A = undefined;',
  28193. ' };',
  28194. '});',
  28195. 'this.DoIt = function () {',
  28196. ' var Arr = [];',
  28197. ' var ArrArr = [];',
  28198. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  28199. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  28200. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  28201. ' var i = 0;',
  28202. ' var c = "";',
  28203. ' var o = null;',
  28204. ' i = rtl.rc(Arr[1], 1, 10);',
  28205. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  28206. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  28207. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  28208. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  28209. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  28210. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  28211. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  28212. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  28213. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  28214. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  28215. '};',
  28216. '']),
  28217. LinesToStr([ // $mod.$main
  28218. '']));
  28219. end;
  28220. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  28221. begin
  28222. StartProgram(false);
  28223. Add([
  28224. '{$R+}',
  28225. 'type',
  28226. ' Ten = 1..10;',
  28227. ' TRec = record x: Ten end;',
  28228. ' TArr = array of TRec;',
  28229. ' TArrArr = array of TArr;',
  28230. ' TObject = class',
  28231. ' A: TArr;',
  28232. ' end;',
  28233. 'procedure DoIt;',
  28234. 'var',
  28235. ' Arr: TArr;',
  28236. ' ArrArr: TArrArr;',
  28237. ' i: Ten;',
  28238. ' o: tobject;',
  28239. 'begin',
  28240. ' Arr[1]:=Arr[1];',
  28241. ' Arr[i]:=Arr[i+1];',
  28242. ' o.a[i]:=o.a[i+2];',
  28243. 'end;',
  28244. 'begin',
  28245. '']);
  28246. ConvertProgram;
  28247. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  28248. LinesToStr([ // statements
  28249. 'rtl.recNewT($mod, "TRec", function () {',
  28250. ' this.x = 0;',
  28251. ' this.$eq = function (b) {',
  28252. ' return this.x === b.x;',
  28253. ' };',
  28254. ' this.$assign = function (s) {',
  28255. ' this.x = s.x;',
  28256. ' return this;',
  28257. ' };',
  28258. '});',
  28259. 'rtl.createClass($mod, "TObject", null, function () {',
  28260. ' this.$init = function () {',
  28261. ' this.A = [];',
  28262. ' };',
  28263. ' this.$final = function () {',
  28264. ' this.A = undefined;',
  28265. ' };',
  28266. '});',
  28267. 'this.DoIt = function () {',
  28268. ' var Arr = [];',
  28269. ' var ArrArr = [];',
  28270. ' var i = 0;',
  28271. ' var o = null;',
  28272. ' Arr[1].$assign(Arr[1]);',
  28273. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  28274. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  28275. '};',
  28276. '']),
  28277. LinesToStr([ // $mod.$main
  28278. '']));
  28279. end;
  28280. procedure TTestModule.TestRangeChecks_StringIndex;
  28281. begin
  28282. StartProgram(false);
  28283. Add([
  28284. 'type',
  28285. ' TObject = class',
  28286. ' S: string;',
  28287. ' end;',
  28288. '{$R+}',
  28289. 'procedure DoIt(var h: string);',
  28290. 'var',
  28291. ' s: string;',
  28292. ' i: longint;',
  28293. ' c: char;',
  28294. ' o: tobject;',
  28295. 'begin',
  28296. ' c:=s[1];',
  28297. ' s[i]:=s[i];',
  28298. ' h[i]:=h[i];',
  28299. ' c:=o.s[i];',
  28300. ' o.s[i]:=c;',
  28301. 'end;',
  28302. 'begin',
  28303. '']);
  28304. ConvertProgram;
  28305. CheckSource('TestRangeChecks_StringIndex',
  28306. LinesToStr([ // statements
  28307. 'rtl.createClass($mod, "TObject", null, function () {',
  28308. ' this.$init = function () {',
  28309. ' this.S = "";',
  28310. ' };',
  28311. ' this.$final = function () {',
  28312. ' };',
  28313. '});',
  28314. 'this.DoIt = function (h) {',
  28315. ' var s = "";',
  28316. ' var i = 0;',
  28317. ' var c = "";',
  28318. ' var o = null;',
  28319. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  28320. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  28321. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  28322. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  28323. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  28324. '};',
  28325. '']),
  28326. LinesToStr([ // $mod.$main
  28327. '']));
  28328. end;
  28329. procedure TTestModule.TestRangeChecks_TypecastInt;
  28330. begin
  28331. StartProgram(false);
  28332. Add([
  28333. '{$R+}',
  28334. 'var',
  28335. ' i: nativeint;',
  28336. ' b: byte;',
  28337. ' sh: shortint;',
  28338. ' w: word;',
  28339. ' sm: smallint;',
  28340. ' lw: longword;',
  28341. ' li: longint;',
  28342. 'begin',
  28343. ' b:=12+byte(i);',
  28344. ' sh:=12+shortint(i);',
  28345. ' w:=12+word(i);',
  28346. ' sm:=12+smallint(i);',
  28347. ' lw:=12+longword(i);',
  28348. ' li:=12+longint(i);',
  28349. '']);
  28350. ConvertProgram;
  28351. CheckSource('TestRangeChecks_TypecastInt',
  28352. LinesToStr([
  28353. 'this.i = 0;',
  28354. 'this.b = 0;',
  28355. 'this.sh = 0;',
  28356. 'this.w = 0;',
  28357. 'this.sm = 0;',
  28358. 'this.lw = 0;',
  28359. 'this.li = 0;',
  28360. '']),
  28361. LinesToStr([
  28362. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  28363. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  28364. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  28365. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  28366. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  28367. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  28368. '']));
  28369. end;
  28370. Initialization
  28371. RegisterTests([TTestModule]);
  28372. end.