tcmodules.pas 780 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434
  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 TestInteger_BitwiseShrNativeInt;
  243. Procedure TestInteger_BitwiseShlNativeInt;
  244. Procedure TestCurrency;
  245. Procedure TestForBoolDo;
  246. Procedure TestForIntDo;
  247. Procedure TestForIntInDo;
  248. // strings
  249. Procedure TestCharConst;
  250. Procedure TestChar_Compare;
  251. Procedure TestChar_BuiltInProcs;
  252. Procedure TestStringConst;
  253. Procedure TestStringConstSurrogate;
  254. Procedure TestString_Length;
  255. Procedure TestString_Compare;
  256. Procedure TestString_SetLength;
  257. Procedure TestString_CharAt;
  258. Procedure TestStringHMinusFail;
  259. Procedure TestStr;
  260. Procedure TestBaseType_AnsiStringFail;
  261. Procedure TestBaseType_WideStringFail;
  262. Procedure TestBaseType_ShortStringFail;
  263. Procedure TestBaseType_RawByteStringFail;
  264. Procedure TestTypeShortstring_Fail;
  265. Procedure TestCharSet_Custom;
  266. Procedure TestForCharDo;
  267. Procedure TestForCharInDo;
  268. // alias types
  269. Procedure TestAliasTypeRef;
  270. Procedure TestTypeCast_BaseTypes;
  271. Procedure TestTypeCast_AliasBaseTypes;
  272. // functions
  273. Procedure TestEmptyProc;
  274. Procedure TestProcOneParam;
  275. Procedure TestFunctionWithoutParams;
  276. Procedure TestProcedureWithoutParams;
  277. Procedure TestPrgProcVar;
  278. Procedure TestProcTwoArgs;
  279. Procedure TestProc_DefaultValue;
  280. Procedure TestUnitProcVar;
  281. Procedure TestImplProc;
  282. Procedure TestFunctionResult;
  283. Procedure TestNestedProc;
  284. Procedure TestNestedProc_ResultString;
  285. Procedure TestForwardProc;
  286. Procedure TestNestedForwardProc;
  287. Procedure TestAssignFunctionResult;
  288. Procedure TestFunctionResultInCondition;
  289. Procedure TestFunctionResultInForLoop;
  290. Procedure TestFunctionResultInTypeCast;
  291. Procedure TestExit;
  292. Procedure TestBreak;
  293. Procedure TestBreakAsVar;
  294. Procedure TestContinue;
  295. Procedure TestProc_External;
  296. Procedure TestProc_ExternalOtherUnit;
  297. Procedure TestProc_Asm;
  298. Procedure TestProc_Assembler;
  299. Procedure TestProc_VarParam;
  300. Procedure TestProc_VarParamString;
  301. Procedure TestProc_VarParamV;
  302. Procedure TestProc_Overload;
  303. Procedure TestProc_OverloadForward;
  304. Procedure TestProc_OverloadIntfImpl;
  305. Procedure TestProc_OverloadNested;
  306. Procedure TestProc_OverloadUnitCycle;
  307. Procedure TestProc_Varargs;
  308. Procedure TestProc_ConstOrder;
  309. Procedure TestProc_DuplicateConst;
  310. Procedure TestProc_LocalVarAbsolute;
  311. Procedure TestProc_LocalVarInit;
  312. Procedure TestProc_ReservedWords;
  313. // anonymous functions
  314. Procedure TestAnonymousProc_Assign_ObjFPC;
  315. Procedure TestAnonymousProc_Assign_Delphi;
  316. Procedure TestAnonymousProc_Arg;
  317. Procedure TestAnonymousProc_Typecast;
  318. Procedure TestAnonymousProc_With;
  319. Procedure TestAnonymousProc_ExceptOn;
  320. Procedure TestAnonymousProc_Nested;
  321. Procedure TestAnonymousProc_NestedAssignResult;
  322. Procedure TestAnonymousProc_Class;
  323. Procedure TestAnonymousProc_ForLoop;
  324. // enums, sets
  325. Procedure TestEnum_Name;
  326. Procedure TestEnum_Number;
  327. Procedure TestEnum_ConstFail;
  328. Procedure TestEnum_Functions;
  329. Procedure TestEnum_AsParams;
  330. Procedure TestEnumRange_Array;
  331. Procedure TestEnum_ForIn;
  332. Procedure TestEnum_ScopedNumber;
  333. Procedure TestEnum_InFunction;
  334. Procedure TestSet_Enum;
  335. Procedure TestSet_Operators;
  336. Procedure TestSet_Operator_In;
  337. Procedure TestSet_Functions;
  338. Procedure TestSet_PassAsArgClone;
  339. Procedure TestSet_AsParams;
  340. Procedure TestSet_Property;
  341. Procedure TestSet_EnumConst;
  342. Procedure TestSet_IntConst;
  343. Procedure TestSet_AnonymousEnumType;
  344. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  345. Procedure TestSet_ConstEnum;
  346. Procedure TestSet_ConstChar;
  347. Procedure TestSet_ConstInt;
  348. Procedure TestSet_InFunction;
  349. Procedure TestSet_ForIn;
  350. // statements
  351. Procedure TestNestBegin;
  352. Procedure TestIncDec;
  353. Procedure TestLoHiFpcMode;
  354. Procedure TestLoHiDelphiMode;
  355. Procedure TestAssignments;
  356. Procedure TestArithmeticOperators1;
  357. Procedure TestLogicalOperators;
  358. Procedure TestBitwiseOperators;
  359. Procedure TestFunctionInt;
  360. Procedure TestFunctionString;
  361. Procedure TestIfThen;
  362. Procedure TestForLoop;
  363. Procedure TestForLoopInsideFunction;
  364. Procedure TestForLoop_ReadVarAfter;
  365. Procedure TestForLoop_Nested;
  366. Procedure TestRepeatUntil;
  367. Procedure TestAsmBlock;
  368. Procedure TestAsmPas_Impl; // ToDo
  369. Procedure TestTryFinally;
  370. Procedure TestTryExcept;
  371. Procedure TestTryExcept_ReservedWords;
  372. Procedure TestIfThenRaiseElse;
  373. Procedure TestCaseOf;
  374. Procedure TestCaseOf_UseSwitch;
  375. Procedure TestCaseOfNoElse;
  376. Procedure TestCaseOfNoElse_UseSwitch;
  377. Procedure TestCaseOfRange;
  378. Procedure TestCaseOfString;
  379. Procedure TestCaseOfChar;
  380. Procedure TestCaseOfExternalClassConst;
  381. Procedure TestDebugger;
  382. // arrays
  383. Procedure TestArray_Dynamic;
  384. Procedure TestArray_Dynamic_Nil;
  385. Procedure TestArray_DynMultiDimensional;
  386. Procedure TestArray_StaticInt;
  387. Procedure TestArray_StaticBool;
  388. Procedure TestArray_StaticChar;
  389. Procedure TestArray_StaticMultiDim;
  390. Procedure TestArray_StaticInFunction;
  391. Procedure TestArrayOfRecord;
  392. Procedure TestArray_StaticRecord;
  393. Procedure TestArrayOfSet;
  394. Procedure TestArray_DynAsParam;
  395. Procedure TestArray_StaticAsParam;
  396. Procedure TestArrayElement_AsParams;
  397. Procedure TestArrayElementFromFuncResult_AsParams;
  398. Procedure TestArrayEnumTypeRange;
  399. Procedure TestArray_SetLengthOutArg;
  400. Procedure TestArray_SetLengthProperty;
  401. Procedure TestArray_SetLengthMultiDim;
  402. Procedure TestArray_OpenArrayOfString;
  403. Procedure TestArray_Concat;
  404. Procedure TestArray_Copy;
  405. Procedure TestArray_InsertDelete;
  406. Procedure TestArray_DynArrayConstObjFPC;
  407. Procedure TestArray_DynArrayConstDelphi;
  408. Procedure TestArray_ArrayLitAsParam;
  409. Procedure TestArray_ArrayLitMultiDimAsParam;
  410. Procedure TestArray_ArrayLitStaticAsParam;
  411. Procedure TestArray_ForInArrOfString;
  412. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  413. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  414. Procedure TestArrayOfConst_TVarRec;
  415. Procedure TestArrayOfConst_PassBaseTypes;
  416. Procedure TestArrayOfConst_PassObj;
  417. // ToDo: tcfiler TPasModuleScope.SystemTVarRec TPas2JSModuleScope.SystemVarRecs
  418. // record
  419. Procedure TestRecord_Empty;
  420. Procedure TestRecord_Var;
  421. Procedure TestRecord_VarExternal;
  422. Procedure TestRecord_WithDo;
  423. Procedure TestRecord_Assign;
  424. Procedure TestRecord_AsParams;
  425. Procedure TestRecordElement_AsParams;
  426. Procedure TestRecordElementFromFuncResult_AsParams;
  427. Procedure TestRecordElementFromWith_AsParams;
  428. Procedure TestRecord_Equal;
  429. Procedure TestRecord_TypeCastJSValueToRecord;
  430. Procedure TestRecord_VariantFail;
  431. Procedure TestRecord_FieldArray;
  432. Procedure TestRecord_Const;
  433. Procedure TestRecord_TypecastFail;
  434. Procedure TestRecord_InFunction;
  435. Procedure TestRecord_AnonymousFail;
  436. // ToDo: RTTI of local record
  437. // ToDo: pcu local record, name clash and rtti
  438. // advanced record
  439. Procedure TestAdvRecord_Function;
  440. Procedure TestAdvRecord_Property;
  441. Procedure TestAdvRecord_PropertyDefault;
  442. Procedure TestAdvRecord_Property_ClassMethod;
  443. Procedure TestAdvRecord_Const;
  444. Procedure TestAdvRecord_ExternalField;
  445. Procedure TestAdvRecord_SubRecord;
  446. Procedure TestAdvRecord_SubClass;
  447. Procedure TestAdvRecord_SubInterfaceFail;
  448. Procedure TestAdvRecord_Constructor;
  449. // ToDo: class constructor
  450. // classes
  451. Procedure TestClass_TObjectDefaultConstructor;
  452. Procedure TestClass_TObjectConstructorWithParams;
  453. Procedure TestClass_TObjectConstructorWithDefaultParam;
  454. Procedure TestClass_Var;
  455. Procedure TestClass_Method;
  456. Procedure TestClass_Implementation;
  457. Procedure TestClass_Inheritance;
  458. Procedure TestClass_TypeAlias;
  459. Procedure TestClass_AbstractMethod;
  460. Procedure TestClass_CallInherited_ProcNoParams;
  461. Procedure TestClass_CallInherited_WithParams;
  462. Procedure TestClasS_CallInheritedConstructor;
  463. Procedure TestClass_ClassVar_Assign;
  464. Procedure TestClass_CallClassMethod;
  465. Procedure TestClass_Property;
  466. Procedure TestClass_Property_ClassMethod;
  467. Procedure TestClass_Property_Indexed;
  468. Procedure TestClass_Property_IndexSpec;
  469. Procedure TestClass_PropertyOfTypeArray;
  470. Procedure TestClass_PropertyDefault;
  471. Procedure TestClass_PropertyDefault2;
  472. Procedure TestClass_PropertyOverride;
  473. Procedure TestClass_PropertyIncVisibility;
  474. Procedure TestClass_Assigned;
  475. Procedure TestClass_WithClassDoCreate;
  476. Procedure TestClass_WithClassInstDoProperty;
  477. Procedure TestClass_WithClassInstDoPropertyWithParams;
  478. Procedure TestClass_WithClassInstDoFunc;
  479. Procedure TestClass_TypeCast;
  480. Procedure TestClass_TypeCastUntypedParam;
  481. Procedure TestClass_Overloads;
  482. Procedure TestClass_OverloadsAncestor;
  483. Procedure TestClass_OverloadConstructor;
  484. Procedure TestClass_OverloadDelphiOverride;
  485. Procedure TestClass_ReintroducedVar;
  486. Procedure TestClass_RaiseDescendant;
  487. Procedure TestClass_ExternalMethod;
  488. Procedure TestClass_ExternalVirtualNameMismatchFail;
  489. Procedure TestClass_ExternalOverrideFail;
  490. Procedure TestClass_ExternalVar;
  491. Procedure TestClass_Const;
  492. Procedure TestClass_LocalVarSelfFail;
  493. Procedure TestClass_ArgSelfFail;
  494. Procedure TestClass_NestedProcSelf;
  495. Procedure TestClass_NestedProcSelf2;
  496. Procedure TestClass_NestedProcClassSelf;
  497. Procedure TestClass_NestedProcCallInherited;
  498. Procedure TestClass_TObjectFree;
  499. Procedure TestClass_TObjectFreeNewInstance;
  500. Procedure TestClass_TObjectFreeLowerCase;
  501. Procedure TestClass_TObjectFreeFunctionFail;
  502. Procedure TestClass_TObjectFreePropertyFail;
  503. Procedure TestClass_ForIn;
  504. // class of
  505. Procedure TestClassOf_Create;
  506. Procedure TestClassOf_Call;
  507. Procedure TestClassOf_Assign;
  508. Procedure TestClassOf_Is;
  509. Procedure TestClassOf_Compare;
  510. Procedure TestClassOf_ClassVar;
  511. Procedure TestClassOf_ClassMethod;
  512. Procedure TestClassOf_ClassProperty;
  513. Procedure TestClassOf_ClassMethodSelf;
  514. Procedure TestClassOf_TypeCast;
  515. Procedure TestClassOf_ImplicitFunctionCall;
  516. Procedure TestClassOf_Const;
  517. // nested class
  518. Procedure TestNestedClass_Alias;
  519. Procedure TestNestedClass_Record;
  520. Procedure TestNestedClass_Class;
  521. // external class
  522. Procedure TestExternalClass_Var;
  523. Procedure TestExternalClass_Const;
  524. Procedure TestExternalClass_Dollar;
  525. Procedure TestExternalClass_DuplicateVarFail;
  526. Procedure TestExternalClass_Method;
  527. Procedure TestExternalClass_ClassMethod;
  528. Procedure TestExternalClass_FunctionResultInTypeCast;
  529. Procedure TestExternalClass_NonExternalOverride;
  530. Procedure TestExternalClass_OverloadHint;
  531. Procedure TestExternalClass_SameNamePublishedProperty;
  532. Procedure TestExternalClass_Property;
  533. Procedure TestExternalClass_PropertyDate;
  534. Procedure TestExternalClass_ClassProperty;
  535. Procedure TestExternalClass_ClassOf;
  536. Procedure TestExternalClass_ClassOtherUnit;
  537. Procedure TestExternalClass_Is;
  538. Procedure TestExternalClass_As;
  539. Procedure TestExternalClass_DestructorFail;
  540. Procedure TestExternalClass_New;
  541. Procedure TestExternalClass_ClassOf_New;
  542. Procedure TestExternalClass_FuncClassOf_New;
  543. Procedure TestExternalClass_New_PasClassFail;
  544. Procedure TestExternalClass_New_PasClassBracketsFail;
  545. Procedure TestExternalClass_LocalConstSameName;
  546. Procedure TestExternalClass_ReintroduceOverload;
  547. Procedure TestExternalClass_Inherited;
  548. Procedure TestExternalClass_PascalAncestorFail;
  549. Procedure TestExternalClass_NewInstance;
  550. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  551. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  552. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  553. Procedure TestExternalClass_PascalProperty;
  554. Procedure TestExternalClass_TypeCastToRootClass;
  555. Procedure TestExternalClass_TypeCastToJSObject;
  556. Procedure TestExternalClass_TypeCastStringToExternalString;
  557. Procedure TestExternalClass_TypeCastToJSFunction;
  558. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  559. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  560. Procedure TestExternalClass_BracketAccessor;
  561. Procedure TestExternalClass_BracketAccessor_Call;
  562. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  563. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  564. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  565. Procedure TestExternalClass_BracketAccessor_MultiType;
  566. Procedure TestExternalClass_BracketAccessor_Index;
  567. Procedure TestExternalClass_ForInJSObject;
  568. Procedure TestExternalClass_ForInJSArray;
  569. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  570. // class interfaces
  571. Procedure TestClassInterface_Corba;
  572. Procedure TestClassInterface_ProcExternalFail;
  573. Procedure TestClassInterface_Overloads;
  574. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  575. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  576. Procedure TestClassInterface_AncestorImpl;
  577. Procedure TestClassInterface_ImplReintroduce;
  578. Procedure TestClassInterface_MethodResolution;
  579. Procedure TestClassInterface_AncestorMoreInterfaces;
  580. Procedure TestClassInterface_MethodOverride;
  581. Procedure TestClassInterface_Corba_Delegation;
  582. Procedure TestClassInterface_Corba_DelegationStatic;
  583. Procedure TestClassInterface_Corba_Operators;
  584. Procedure TestClassInterface_Corba_Args;
  585. Procedure TestClassInterface_Corba_ForIn;
  586. Procedure TestClassInterface_COM_AssignVar;
  587. Procedure TestClassInterface_COM_AssignArg;
  588. Procedure TestClassInterface_COM_FunctionResult;
  589. Procedure TestClassInterface_COM_InheritedFuncResult;
  590. Procedure TestClassInterface_COM_IsAsTypeCasts;
  591. Procedure TestClassInterface_COM_PassAsArg;
  592. Procedure TestClassInterface_COM_PassToUntypedParam;
  593. Procedure TestClassInterface_COM_FunctionInExpr;
  594. Procedure TestClassInterface_COM_Property;
  595. Procedure TestClassInterface_COM_IntfProperty;
  596. Procedure TestClassInterface_COM_Delegation;
  597. Procedure TestClassInterface_COM_With;
  598. Procedure TestClassInterface_COM_ForIn;
  599. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  600. Procedure TestClassInterface_COM_RecordIntfFail;
  601. Procedure TestClassInterface_COM_UnitInitialization;
  602. Procedure TestClassInterface_GUID;
  603. Procedure TestClassInterface_GUIDProperty;
  604. // helpers
  605. Procedure TestClassHelper_ClassVar;
  606. Procedure TestClassHelper_Method_AccessInstanceFields;
  607. Procedure TestClassHelper_Method_Call;
  608. Procedure TestClassHelper_Method_Nested_Call;
  609. Procedure TestClassHelper_ClassMethod_Call;
  610. Procedure TestClassHelper_ClassOf;
  611. Procedure TestClassHelper_MethodRefObjFPC;
  612. Procedure TestClassHelper_Constructor;
  613. Procedure TestClassHelper_InheritedObjFPC;
  614. Procedure TestClassHelper_Property;
  615. Procedure TestClassHelper_Property_Array;
  616. Procedure TestClassHelper_Property_Array_Default;
  617. Procedure TestClassHelper_Property_Array_DefaultDefault;
  618. Procedure TestClassHelper_ClassProperty;
  619. Procedure TestClassHelper_ClassPropertyStatic;
  620. Procedure TestClassHelper_ClassProperty_Array;
  621. Procedure TestClassHelper_ForIn;
  622. Procedure TestClassHelper_PassProperty;
  623. Procedure TestExtClassHelper_ClassVar;
  624. Procedure TestExtClassHelper_Method_Call;
  625. Procedure TestRecordHelper_ClassVar;
  626. Procedure TestRecordHelper_Method_Call;
  627. Procedure TestRecordHelper_Constructor;
  628. Procedure TestTypeHelper_ClassVar;
  629. Procedure TestTypeHelper_PassResultElement;
  630. Procedure TestTypeHelper_PassArgs;
  631. Procedure TestTypeHelper_PassVarConst;
  632. Procedure TestTypeHelper_PassFuncResult;
  633. Procedure TestTypeHelper_PassPropertyField;
  634. Procedure TestTypeHelper_PassPropertyGetter;
  635. Procedure TestTypeHelper_PassClassPropertyField;
  636. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  637. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  638. Procedure TestTypeHelper_Property;
  639. Procedure TestTypeHelper_Property_Array;
  640. Procedure TestTypeHelper_ClassProperty;
  641. Procedure TestTypeHelper_ClassProperty_Array;
  642. Procedure TestTypeHelper_ClassMethod;
  643. Procedure TestTypeHelper_ExtClassMethodFail;
  644. Procedure TestTypeHelper_Constructor;
  645. Procedure TestTypeHelper_Word;
  646. Procedure TestTypeHelper_Double;
  647. Procedure TestTypeHelper_StringChar;
  648. Procedure TestTypeHelper_Array;
  649. Procedure TestTypeHelper_EnumType;
  650. Procedure TestTypeHelper_SetType;
  651. // proc types
  652. Procedure TestProcType;
  653. Procedure TestProcType_Arg;
  654. Procedure TestProcType_FunctionFPC;
  655. Procedure TestProcType_FunctionDelphi;
  656. Procedure TestProcType_ProcedureDelphi;
  657. Procedure TestProcType_AsParam;
  658. Procedure TestProcType_MethodFPC;
  659. Procedure TestProcType_MethodDelphi;
  660. Procedure TestProcType_PropertyFPC;
  661. Procedure TestProcType_PropertyDelphi;
  662. Procedure TestProcType_WithClassInstDoPropertyFPC;
  663. Procedure TestProcType_Nested;
  664. Procedure TestProcType_NestedOfObject;
  665. Procedure TestProcType_ReferenceToProc;
  666. Procedure TestProcType_ReferenceToMethod;
  667. Procedure TestProcType_Typecast;
  668. Procedure TestProcType_PassProcToUntyped;
  669. Procedure TestProcType_PassProcToArray;
  670. // pointer
  671. Procedure TestPointer;
  672. Procedure TestPointer_Proc;
  673. Procedure TestPointer_AssignRecordFail;
  674. Procedure TestPointer_AssignStaticArrayFail;
  675. Procedure TestPointer_TypeCastJSValueToPointer;
  676. Procedure TestPointer_NonRecordFail;
  677. Procedure TestPointer_AnonymousArgTypeFail;
  678. Procedure TestPointer_AnonymousVarTypeFail;
  679. Procedure TestPointer_AnonymousResultTypeFail;
  680. Procedure TestPointer_AddrOperatorFail;
  681. Procedure TestPointer_ArrayParamsFail;
  682. Procedure TestPointer_PointerAddFail;
  683. Procedure TestPointer_IncPointerFail;
  684. Procedure TestPointer_Record;
  685. Procedure TestPointer_RecordArg;
  686. // jsvalue
  687. Procedure TestJSValue_AssignToJSValue;
  688. Procedure TestJSValue_TypeCastToBaseType;
  689. Procedure TestJSValue_TypecastToJSValue;
  690. Procedure TestJSValue_Equal;
  691. Procedure TestJSValue_If;
  692. Procedure TestJSValue_Not;
  693. Procedure TestJSValue_Enum;
  694. Procedure TestJSValue_ClassInstance;
  695. Procedure TestJSValue_ClassOf;
  696. Procedure TestJSValue_ArrayOfJSValue;
  697. Procedure TestJSValue_ArrayLit;
  698. Procedure TestJSValue_Params;
  699. Procedure TestJSValue_UntypedParam;
  700. Procedure TestJSValue_FuncResultType;
  701. Procedure TestJSValue_ProcType_Assign;
  702. Procedure TestJSValue_ProcType_Equal;
  703. Procedure TestJSValue_ProcType_Param;
  704. Procedure TestJSValue_AssignToPointerFail;
  705. Procedure TestJSValue_OverloadDouble;
  706. Procedure TestJSValue_OverloadNativeInt;
  707. Procedure TestJSValue_OverloadWord;
  708. Procedure TestJSValue_OverloadString;
  709. Procedure TestJSValue_OverloadChar;
  710. Procedure TestJSValue_OverloadPointer;
  711. Procedure TestJSValue_ForIn;
  712. // RTTI
  713. Procedure TestRTTI_IntRange;
  714. Procedure TestRTTI_Double;
  715. Procedure TestRTTI_ProcType;
  716. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  717. Procedure TestRTTI_EnumAndSetType;
  718. Procedure TestRTTI_EnumRange;
  719. Procedure TestRTTI_AnonymousEnumType;
  720. Procedure TestRTTI_StaticArray;
  721. Procedure TestRTTI_DynArray;
  722. Procedure TestRTTI_ArrayNestedAnonymous;
  723. Procedure TestRTTI_PublishedMethodOverloadFail;
  724. Procedure TestRTTI_PublishedMethodExternalFail;
  725. Procedure TestRTTI_PublishedClassPropertyFail;
  726. Procedure TestRTTI_PublishedClassFieldFail;
  727. Procedure TestRTTI_PublishedFieldExternalFail;
  728. Procedure TestRTTI_Class_Field;
  729. Procedure TestRTTI_Class_Method;
  730. Procedure TestRTTI_Class_MethodArgFlags;
  731. Procedure TestRTTI_Class_Property;
  732. Procedure TestRTTI_Class_PropertyParams;
  733. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  734. Procedure TestRTTI_Class_OmitRTTI;
  735. Procedure TestRTTI_IndexModifier;
  736. Procedure TestRTTI_StoredModifier;
  737. Procedure TestRTTI_DefaultValue;
  738. Procedure TestRTTI_DefaultValueSet;
  739. Procedure TestRTTI_DefaultValueRangeType;
  740. Procedure TestRTTI_DefaultValueInherit;
  741. Procedure TestRTTI_OverrideMethod;
  742. Procedure TestRTTI_OverloadProperty;
  743. // ToDo: array argument
  744. Procedure TestRTTI_ClassForward;
  745. Procedure TestRTTI_ClassOf;
  746. Procedure TestRTTI_Record;
  747. Procedure TestRTTI_RecordAnonymousArray;
  748. Procedure TestRTTI_LocalTypes;
  749. Procedure TestRTTI_TypeInfo_BaseTypes;
  750. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  751. Procedure TestRTTI_TypeInfo_LocalFail;
  752. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  753. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  754. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  755. Procedure TestRTTI_TypeInfo_FunctionClassType;
  756. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  757. Procedure TestRTTI_Interface_Corba;
  758. Procedure TestRTTI_Interface_COM;
  759. Procedure TestRTTI_ClassHelper;
  760. // Resourcestring
  761. Procedure TestResourcestringProgram;
  762. Procedure TestResourcestringUnit;
  763. Procedure TestResourcestringImplementation;
  764. // Attributes
  765. Procedure TestAtributes_Ignore;
  766. // Assertions, checks
  767. procedure TestAssert;
  768. procedure TestAssert_SysUtils;
  769. procedure TestObjectChecks;
  770. procedure TestRangeChecks_AssignInt;
  771. procedure TestRangeChecks_AssignIntRange;
  772. procedure TestRangeChecks_AssignEnum;
  773. procedure TestRangeChecks_AssignEnumRange;
  774. procedure TestRangeChecks_AssignChar;
  775. procedure TestRangeChecks_AssignCharRange;
  776. procedure TestRangeChecks_ArrayIndex;
  777. procedure TestRangeChecks_ArrayOfRecIndex;
  778. procedure TestRangeChecks_StringIndex;
  779. procedure TestRangeChecks_TypecastInt;
  780. end;
  781. function LinesToStr(Args: array of const): string;
  782. function ExtractFileUnitName(aFilename: string): string;
  783. function JSToStr(El: TJSElement): string;
  784. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  785. implementation
  786. function LinesToStr(Args: array of const): string;
  787. var
  788. s: String;
  789. i: Integer;
  790. begin
  791. s:='';
  792. for i:=Low(Args) to High(Args) do
  793. case Args[i].VType of
  794. vtChar: s += Args[i].VChar+LineEnding;
  795. vtString: s += Args[i].VString^+LineEnding;
  796. vtPChar: s += Args[i].VPChar+LineEnding;
  797. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  798. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  799. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  800. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  801. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  802. end;
  803. Result:=s;
  804. end;
  805. function ExtractFileUnitName(aFilename: string): string;
  806. var
  807. p: Integer;
  808. begin
  809. Result:=ExtractFileName(aFilename);
  810. if Result='' then exit;
  811. for p:=length(Result) downto 1 do
  812. case Result[p] of
  813. '/','\': exit;
  814. '.':
  815. begin
  816. Delete(Result,p,length(Result));
  817. exit;
  818. end;
  819. end;
  820. end;
  821. function JSToStr(El: TJSElement): string;
  822. var
  823. aWriter: TBufferWriter;
  824. aJSWriter: TJSWriter;
  825. begin
  826. aJSWriter:=nil;
  827. aWriter:=TBufferWriter.Create(1000);
  828. try
  829. aJSWriter:=TJSWriter.Create(aWriter);
  830. aJSWriter.IndentSize:=2;
  831. aJSWriter.WriteJS(El);
  832. Result:=aWriter.AsString;
  833. finally
  834. aJSWriter.Free;
  835. aWriter.Free;
  836. end;
  837. end;
  838. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  839. // search diff, ignore changes in spaces
  840. const
  841. SpaceChars = [#9,#10,#13,' '];
  842. var
  843. ExpectedP, ActualP: PChar;
  844. function FindLineEnd(p: PChar): PChar;
  845. begin
  846. Result:=p;
  847. while not (Result^ in [#0,#10,#13]) do inc(Result);
  848. end;
  849. function FindLineStart(p, MinP: PChar): PChar;
  850. begin
  851. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  852. Result:=p;
  853. end;
  854. procedure SkipLineEnd(var p: PChar);
  855. begin
  856. if p^ in [#10,#13] then
  857. begin
  858. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  859. inc(p,2)
  860. else
  861. inc(p);
  862. end;
  863. end;
  864. procedure DiffFound;
  865. var
  866. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  867. ExpLine, ActLine: String;
  868. i, LineNo, DiffLineNo: Integer;
  869. begin
  870. writeln('Diff found "',Msg,'". Lines:');
  871. // write correct lines
  872. p:=PChar(Expected);
  873. LineNo:=0;
  874. DiffLineNo:=0;
  875. repeat
  876. StartPos:=p;
  877. while not (p^ in [#0,#10,#13]) do inc(p);
  878. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  879. SkipLineEnd(p);
  880. inc(LineNo);
  881. if (p<=ExpectedP) and (p^<>#0) then
  882. begin
  883. writeln('= ',ExpLine);
  884. end else begin
  885. // diff line
  886. if DiffLineNo=0 then DiffLineNo:=LineNo;
  887. // write actual line
  888. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  889. ActLineEndP:=FindLineEnd(ActualP);
  890. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  891. writeln('- ',ActLine);
  892. // write expected line
  893. writeln('+ ',ExpLine);
  894. // write empty line with pointer ^
  895. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  896. writeln('^');
  897. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  898. CheckSrcDiff:=false;
  899. // write up to three following actual lines to get some context
  900. for i:=1 to 3 do begin
  901. ActLineStartP:=ActLineEndP;
  902. SkipLineEnd(ActLineStartP);
  903. if ActLineStartP^=#0 then break;
  904. ActLineEndP:=FindLineEnd(ActLineStartP);
  905. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  906. writeln('~ ',ActLine);
  907. end;
  908. exit;
  909. end;
  910. until p^=#0;
  911. writeln('DiffFound Actual:-----------------------');
  912. writeln(Actual);
  913. writeln('DiffFound Expected:---------------------');
  914. writeln(Expected);
  915. writeln('DiffFound ------------------------------');
  916. Msg:='diff found, but lines are the same, internal error';
  917. CheckSrcDiff:=false;
  918. end;
  919. var
  920. IsSpaceNeeded: Boolean;
  921. LastChar, Quote: Char;
  922. begin
  923. Result:=true;
  924. Msg:='';
  925. if Expected='' then Expected:=' ';
  926. if Actual='' then Actual:=' ';
  927. ExpectedP:=PChar(Expected);
  928. ActualP:=PChar(Actual);
  929. repeat
  930. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  931. case ExpectedP^ of
  932. #0:
  933. begin
  934. // check that rest of Actual has only spaces
  935. while ActualP^ in SpaceChars do inc(ActualP);
  936. if ActualP^<>#0 then
  937. begin
  938. DiffFound;
  939. exit;
  940. end;
  941. exit(true);
  942. end;
  943. ' ',#9,#10,#13:
  944. begin
  945. // skip space in Expected
  946. IsSpaceNeeded:=false;
  947. if ExpectedP>PChar(Expected) then
  948. LastChar:=ExpectedP[-1]
  949. else
  950. LastChar:=#0;
  951. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  952. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  953. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  954. IsSpaceNeeded:=true;
  955. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  956. begin
  957. DiffFound;
  958. exit;
  959. end;
  960. while ActualP^ in SpaceChars do inc(ActualP);
  961. end;
  962. '''','"':
  963. begin
  964. while ActualP^ in SpaceChars do inc(ActualP);
  965. if ExpectedP^<>ActualP^ then
  966. begin
  967. DiffFound;
  968. exit;
  969. end;
  970. Quote:=ExpectedP^;
  971. repeat
  972. inc(ExpectedP);
  973. inc(ActualP);
  974. if ExpectedP^<>ActualP^ then
  975. begin
  976. DiffFound;
  977. exit;
  978. end;
  979. if (ExpectedP^ in [#0,#10,#13]) then
  980. break
  981. else if (ExpectedP^=Quote) then
  982. begin
  983. inc(ExpectedP);
  984. inc(ActualP);
  985. break;
  986. end;
  987. until false;
  988. end;
  989. else
  990. while ActualP^ in SpaceChars do inc(ActualP);
  991. if ExpectedP^<>ActualP^ then
  992. begin
  993. DiffFound;
  994. exit;
  995. end;
  996. inc(ExpectedP);
  997. inc(ActualP);
  998. end;
  999. until false;
  1000. end;
  1001. { TTestEnginePasResolver }
  1002. destructor TTestEnginePasResolver.Destroy;
  1003. begin
  1004. FreeAndNil(FStreamResolver);
  1005. FreeAndNil(FParser);
  1006. FreeAndNil(FScanner);
  1007. FreeAndNil(FStreamResolver);
  1008. if Module<>nil then
  1009. begin
  1010. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1011. FModule:=nil;
  1012. end;
  1013. inherited Destroy;
  1014. end;
  1015. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1016. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1017. begin
  1018. Result:=nil;
  1019. if InFilename<>'' then
  1020. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1021. if Assigned(OnFindUnit) then
  1022. Result:=OnFindUnit(AName);
  1023. if NameExpr=nil then ;
  1024. end;
  1025. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1026. begin
  1027. // do not parse recursively
  1028. // parse via the queue
  1029. if Section=nil then ;
  1030. end;
  1031. { TCustomTestModule }
  1032. function TCustomTestModule.GetMsgCount: integer;
  1033. begin
  1034. Result:=FHintMsgs.Count;
  1035. end;
  1036. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1037. begin
  1038. Result:=TTestHintMessage(FHintMsgs[Index]);
  1039. end;
  1040. function TCustomTestModule.GetResolverCount: integer;
  1041. begin
  1042. Result:=FModules.Count;
  1043. end;
  1044. function TCustomTestModule.GetResolvers(Index: integer
  1045. ): TTestEnginePasResolver;
  1046. begin
  1047. Result:=TTestEnginePasResolver(FModules[Index]);
  1048. end;
  1049. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1050. ): TPasModule;
  1051. var
  1052. DefNamespace: String;
  1053. begin
  1054. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1055. if (Pos('.',aUnitName)<1) then
  1056. begin
  1057. DefNamespace:=GetDefaultNamespace;
  1058. if DefNamespace<>'' then
  1059. begin
  1060. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1061. if Result<>nil then exit;
  1062. end;
  1063. end;
  1064. Result:=LoadUnit(aUnitName);
  1065. if Result<>nil then exit;
  1066. {$IFDEF VerbosePas2JS}
  1067. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1068. {$ENDIF}
  1069. Fail('can''t find unit "'+aUnitName+'"');
  1070. end;
  1071. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1072. var
  1073. aParser: TPasParser;
  1074. Item: TTestHintMessage;
  1075. begin
  1076. aParser:=Sender as TPasParser;
  1077. Item:=TTestHintMessage.Create;
  1078. Item.Id:=aParser.LastMsgNumber;
  1079. Item.MsgType:=aParser.LastMsgType;
  1080. Item.MsgNumber:=aParser.LastMsgNumber;
  1081. Item.Msg:=Msg;
  1082. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1083. {$IFDEF VerbosePas2JS}
  1084. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1085. {$ENDIF}
  1086. FHintMsgs.Add(Item);
  1087. end;
  1088. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1089. );
  1090. var
  1091. aResolver: TTestEnginePasResolver;
  1092. Item: TTestHintMessage;
  1093. begin
  1094. aResolver:=Sender as TTestEnginePasResolver;
  1095. Item:=TTestHintMessage.Create;
  1096. Item.Id:=aResolver.LastMsgId;
  1097. Item.MsgType:=aResolver.LastMsgType;
  1098. Item.MsgNumber:=aResolver.LastMsgNumber;
  1099. Item.Msg:=Msg;
  1100. Item.SourcePos:=aResolver.LastSourcePos;
  1101. {$IFDEF VerbosePas2JS}
  1102. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1103. {$ENDIF}
  1104. FHintMsgs.Add(Item);
  1105. end;
  1106. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1107. var
  1108. Item: TTestHintMessage;
  1109. aScanner: TPas2jsPasScanner;
  1110. begin
  1111. aScanner:=Sender as TPas2jsPasScanner;
  1112. Item:=TTestHintMessage.Create;
  1113. Item.Id:=aScanner.LastMsgNumber;
  1114. Item.MsgType:=aScanner.LastMsgType;
  1115. Item.MsgNumber:=aScanner.LastMsgNumber;
  1116. Item.Msg:=Msg;
  1117. Item.SourcePos:=aScanner.CurSourcePos;
  1118. {$IFDEF VerbosePas2JS}
  1119. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1120. {$ENDIF}
  1121. FHintMsgs.Add(Item);
  1122. end;
  1123. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1124. var
  1125. i: Integer;
  1126. CurEngine: TTestEnginePasResolver;
  1127. CurUnitName: String;
  1128. begin
  1129. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1130. Result:=nil;
  1131. if (Module.ClassType=TPasModule)
  1132. and (CompareText(Module.Name,aUnitName)=0) then
  1133. exit(Module);
  1134. for i:=0 to ResolverCount-1 do
  1135. begin
  1136. CurEngine:=Resolvers[i];
  1137. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1138. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1139. if CompareText(aUnitName,CurUnitName)=0 then
  1140. begin
  1141. Result:=CurEngine.Module;
  1142. if Result<>nil then exit;
  1143. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1144. FileResolver.FindSourceFile(aUnitName);
  1145. CurEngine.StreamResolver:=TStreamResolver.Create;
  1146. CurEngine.StreamResolver.OwnsStreams:=True;
  1147. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1148. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1149. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1150. InitScanner(CurEngine.Scanner);
  1151. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1152. CurEngine.Parser.Options:=po_tcmodules;
  1153. if CompareText(CurUnitName,'System')=0 then
  1154. CurEngine.Parser.ImplicitUses.Clear;
  1155. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1156. try
  1157. CurEngine.Parser.NextToken;
  1158. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1159. except
  1160. on E: Exception do
  1161. HandleException(E);
  1162. end;
  1163. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1164. Result:=CurEngine.Module;
  1165. exit;
  1166. end;
  1167. end;
  1168. end;
  1169. procedure TCustomTestModule.SetUp;
  1170. begin
  1171. {$IFDEF EnablePasTreeGlobalRefCount}
  1172. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1173. {$ENDIF}
  1174. if FModules<>nil then
  1175. begin
  1176. writeln('TCustomTestModule.SetUp FModules<>nil');
  1177. Halt;
  1178. end;
  1179. inherited SetUp;
  1180. FSkipTests:=false;
  1181. FSource:=TStringList.Create;
  1182. FModules:=TObjectList.Create(true);
  1183. FFilename:='test1.pp';
  1184. FFileResolver:=TStreamResolver.Create;
  1185. FFileResolver.OwnsStreams:=True;
  1186. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1187. InitScanner(FScanner);
  1188. FEngine:=AddModule(Filename);
  1189. FEngine.Scanner:=FScanner;
  1190. FScanner.Resolver:=FEngine;
  1191. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1192. FParser.OnLog:=@OnParserLog;
  1193. FEngine.Parser:=FParser;
  1194. Parser.Options:=po_tcmodules;
  1195. FModule:=Nil;
  1196. FConverter:=CreateConverter;
  1197. FExpectedErrorClass:=nil;
  1198. end;
  1199. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1200. begin
  1201. Result:=TPasToJSConverter.Create;
  1202. Result.Options:=co_tcmodules;
  1203. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1204. end;
  1205. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1206. begin
  1207. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1208. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1209. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1210. aScanner.AllowedBoolSwitches:=msAllPas2jsBoolSwitches;
  1211. aScanner.ReadOnlyBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly;
  1212. aScanner.CurrentBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1213. aScanner.OnLog:=@OnScannerLog;
  1214. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1215. end;
  1216. procedure TCustomTestModule.TearDown;
  1217. {$IFDEF CheckPasTreeRefCount}
  1218. var
  1219. El: TPasElement;
  1220. {$ENDIF}
  1221. var
  1222. i: Integer;
  1223. CurModule: TPasModule;
  1224. begin
  1225. FHintMsgs.Clear;
  1226. FHintMsgsGood.Clear;
  1227. FSkipTests:=false;
  1228. FJSRegModuleCall:=nil;
  1229. FJSModuleCallArgs:=nil;
  1230. FJSImplentationUses:=nil;
  1231. FJSInterfaceUses:=nil;
  1232. FJSModuleSrc:=nil;
  1233. FJSInitBody:=nil;
  1234. FreeAndNil(FJSSource);
  1235. FreeAndNil(FJSModule);
  1236. FreeAndNil(FConverter);
  1237. Engine.Clear;
  1238. FreeAndNil(FSource);
  1239. FreeAndNil(FFileResolver);
  1240. if FModules<>nil then
  1241. begin
  1242. for i:=0 to FModules.Count-1 do
  1243. begin
  1244. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1245. if CurModule=nil then continue;
  1246. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1247. CurModule.ReleaseUsedUnits;
  1248. end;
  1249. if FModule<>nil then
  1250. FModule.ReleaseUsedUnits;
  1251. for i:=0 to FModules.Count-1 do
  1252. begin
  1253. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1254. if CurModule=nil then continue;
  1255. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1256. end;
  1257. FreeAndNil(FModules);
  1258. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1259. FEngine:=nil;
  1260. end;
  1261. inherited TearDown;
  1262. {$IFDEF EnablePasTreeGlobalRefCount}
  1263. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1264. begin
  1265. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1266. {$IFDEF CheckPasTreeRefCount}
  1267. El:=TPasElement.FirstRefEl;
  1268. while El<>nil do
  1269. begin
  1270. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1271. for i:=0 to El.RefIds.Count-1 do
  1272. writeln(' ',El.RefIds[i]);
  1273. El:=El.NextRefEl;
  1274. end;
  1275. {$ENDIF}
  1276. Halt;
  1277. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1278. end;
  1279. {$ENDIF}
  1280. end;
  1281. procedure TCustomTestModule.Add(Line: string);
  1282. begin
  1283. Source.Add(Line);
  1284. end;
  1285. procedure TCustomTestModule.Add(const Lines: array of string);
  1286. var
  1287. i: Integer;
  1288. begin
  1289. for i:=low(Lines) to high(Lines) do
  1290. Add(Lines[i]);
  1291. end;
  1292. procedure TCustomTestModule.StartParsing;
  1293. var
  1294. Src: String;
  1295. begin
  1296. Src:=Source.Text;
  1297. FEngine.Source:=Src;
  1298. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1299. Scanner.OpenFile(FileName);
  1300. Writeln('// Test : ',Self.TestName);
  1301. Writeln(Src);
  1302. end;
  1303. procedure TCustomTestModule.ParseModuleQueue;
  1304. var
  1305. i: Integer;
  1306. CurResolver: TTestEnginePasResolver;
  1307. Found: Boolean;
  1308. Section: TPasSection;
  1309. begin
  1310. // parse til exception or all modules finished
  1311. while not SkipTests do
  1312. begin
  1313. Found:=false;
  1314. for i:=0 to ResolverCount-1 do
  1315. begin
  1316. CurResolver:=Resolvers[i];
  1317. if CurResolver.CurrentParser=nil then continue;
  1318. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1319. continue;
  1320. CurResolver.Parser.ParseContinue;
  1321. Found:=true;
  1322. break;
  1323. end;
  1324. if not Found then break;
  1325. end;
  1326. for i:=0 to ResolverCount-1 do
  1327. begin
  1328. CurResolver:=Resolvers[i];
  1329. if CurResolver.Parser=nil then
  1330. begin
  1331. if CurResolver.CurrentParser<>nil then
  1332. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1333. continue;
  1334. end;
  1335. if CurResolver.Parser.CurModule<>nil then
  1336. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1337. end;
  1338. end;
  1339. procedure TCustomTestModule.ParseModule;
  1340. begin
  1341. if SkipTests then exit;
  1342. FFirstPasStatement:=nil;
  1343. try
  1344. StartParsing;
  1345. Parser.ParseMain(FModule);
  1346. ParseModuleQueue;
  1347. except
  1348. on E: Exception do
  1349. HandleException(E);
  1350. end;
  1351. if SkipTests then exit;
  1352. AssertNotNull('Module resulted in Module',Module);
  1353. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1354. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1355. end;
  1356. procedure TCustomTestModule.ParseProgram;
  1357. begin
  1358. if SkipTests then exit;
  1359. ParseModule;
  1360. if SkipTests then exit;
  1361. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1362. FPasProgram:=TPasProgram(Module);
  1363. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1364. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1365. if (PasProgram.InitializationSection.Elements.Count>0) then
  1366. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1367. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1368. end;
  1369. procedure TCustomTestModule.ParseUnit;
  1370. begin
  1371. if SkipTests then exit;
  1372. ParseModule;
  1373. if SkipTests then exit;
  1374. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1375. AssertNotNull('Has interface section',Module.InterfaceSection);
  1376. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1377. if (Module.InitializationSection<>nil)
  1378. and (Module.InitializationSection.Elements.Count>0)
  1379. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1380. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1381. end;
  1382. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1383. ): TTestEnginePasResolver;
  1384. var
  1385. i: Integer;
  1386. begin
  1387. for i:=0 to ResolverCount-1 do
  1388. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1389. exit(Resolvers[i]);
  1390. Result:=nil;
  1391. end;
  1392. function TCustomTestModule.AddModule(aFilename: string
  1393. ): TTestEnginePasResolver;
  1394. begin
  1395. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1396. if FindModuleWithFilename(aFilename)<>nil then
  1397. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1398. Result:=TTestEnginePasResolver.Create;
  1399. Result.Filename:=aFilename;
  1400. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1401. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1402. Result.OnLog:=@OnPasResolverLog;
  1403. FModules.Add(Result);
  1404. end;
  1405. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1406. ): TTestEnginePasResolver;
  1407. begin
  1408. Result:=AddModule(aFilename);
  1409. Result.Source:=Src;
  1410. end;
  1411. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1412. ImplementationSrc: string): TTestEnginePasResolver;
  1413. var
  1414. Src: String;
  1415. begin
  1416. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1417. Src+=LineEnding;
  1418. Src+='interface'+LineEnding;
  1419. Src+=LineEnding;
  1420. Src+=InterfaceSrc;
  1421. Src+='implementation'+LineEnding;
  1422. Src+=LineEnding;
  1423. Src+=ImplementationSrc;
  1424. Src+='end.'+LineEnding;
  1425. Result:=AddModuleWithSrc(aFilename,Src);
  1426. end;
  1427. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1428. var
  1429. Intf, Impl: TStringList;
  1430. begin
  1431. Intf:=TStringList.Create;
  1432. // interface
  1433. if supTVarRec in Parts then
  1434. Intf.Add('{$modeswitch externalclass}');
  1435. Intf.Add('type');
  1436. Intf.Add(' integer=longint;');
  1437. Intf.Add(' sizeint=nativeint;');
  1438. //'const',
  1439. //' LineEnding = #10;',
  1440. //' DirectorySeparator = ''/'';',
  1441. //' DriveSeparator = '''';',
  1442. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1443. //' AllowDriveSeparators : set of char = [];',
  1444. if supTObject in Parts then
  1445. Intf.AddStrings([
  1446. 'type',
  1447. ' TClass = class of TObject;',
  1448. ' TObject = class',
  1449. ' constructor Create;',
  1450. ' destructor Destroy; virtual;',
  1451. ' class function ClassType: TClass; assembler;',
  1452. ' class function ClassName: String; assembler;',
  1453. ' class function ClassNameIs(const Name: string): boolean;',
  1454. ' class function ClassParent: TClass; assembler;',
  1455. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1456. ' class function UnitName: String; assembler;',
  1457. ' procedure AfterConstruction; virtual;',
  1458. ' procedure BeforeDestruction;virtual;',
  1459. ' function Equals(Obj: TObject): boolean; virtual;',
  1460. ' function ToString: String; virtual;',
  1461. ' end;']);
  1462. if supTVarRec in Parts then
  1463. Intf.AddStrings([
  1464. 'const',
  1465. ' vtInteger = 0;',
  1466. ' vtBoolean = 1;',
  1467. ' vtJSValue = 19;',
  1468. 'type',
  1469. ' PVarRec = ^TVarRec;',
  1470. ' TVarRec = record',
  1471. ' VType : byte;',
  1472. ' VJSValue: JSValue;',
  1473. ' vInteger: longint external name ''VJSValue'';',
  1474. ' vBoolean: boolean external name ''VJSValue'';',
  1475. ' end;',
  1476. ' TVarRecArray = array of TVarRec;',
  1477. 'function VarRecs: TVarRecArray; varargs;',
  1478. '']);
  1479. Intf.Add('var');
  1480. Intf.Add(' ExitCode: Longint = 0;');
  1481. // implementation
  1482. Impl:=TStringList.Create;
  1483. if supTObject in Parts then
  1484. Impl.AddStrings([
  1485. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1486. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1487. 'asm',
  1488. 'end;',
  1489. 'constructor TObject.Create; begin end;',
  1490. 'destructor TObject.Destroy; begin end;',
  1491. 'class function TObject.ClassType: TClass; assembler;',
  1492. 'asm',
  1493. 'end;',
  1494. 'class function TObject.ClassName: String; assembler;',
  1495. 'asm',
  1496. 'end;',
  1497. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1498. 'begin',
  1499. ' Result:=SameText(Name,ClassName);',
  1500. 'end;',
  1501. 'class function TObject.ClassParent: TClass; assembler;',
  1502. 'asm',
  1503. 'end;',
  1504. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1505. 'asm',
  1506. 'end;',
  1507. 'class function TObject.UnitName: String; assembler;',
  1508. 'asm',
  1509. 'end;',
  1510. 'procedure TObject.AfterConstruction; begin end;',
  1511. 'procedure TObject.BeforeDestruction; begin end;',
  1512. 'function TObject.Equals(Obj: TObject): boolean;',
  1513. 'begin',
  1514. ' Result:=Obj=Self;',
  1515. 'end;',
  1516. 'function TObject.ToString: String;',
  1517. 'begin',
  1518. ' Result:=ClassName;',
  1519. 'end;'
  1520. ]);
  1521. if supTVarRec in Parts then
  1522. Impl.AddStrings([
  1523. 'function VarRecs: TVarRecArray; varargs;',
  1524. 'var',
  1525. ' v: PVarRec;',
  1526. 'begin',
  1527. ' v^.VType:=1;',
  1528. ' v^.VJSValue:=2;',
  1529. 'end;',
  1530. '']);
  1531. try
  1532. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1533. finally
  1534. Intf.Free;
  1535. Impl.Free;
  1536. end;
  1537. end;
  1538. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1539. SystemUnitParts: TSystemUnitParts);
  1540. begin
  1541. if NeedSystemUnit then
  1542. AddSystemUnit(SystemUnitParts)
  1543. else
  1544. Parser.ImplicitUses.Clear;
  1545. Add('program '+ExtractFileUnitName(Filename)+';');
  1546. Add('');
  1547. end;
  1548. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1549. SystemUnitParts: TSystemUnitParts);
  1550. begin
  1551. if NeedSystemUnit then
  1552. AddSystemUnit(SystemUnitParts)
  1553. else
  1554. Parser.ImplicitUses.Clear;
  1555. Add('unit Test1;');
  1556. Add('');
  1557. end;
  1558. procedure TCustomTestModule.ConvertModule;
  1559. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1560. out UsesLit: TJSArrayLiteral);
  1561. var
  1562. i: Integer;
  1563. Item: TJSElement;
  1564. Lit: TJSLiteral;
  1565. begin
  1566. UsesLit:=nil;
  1567. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1568. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1569. exit; // null is ok
  1570. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1571. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1572. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1573. begin
  1574. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1575. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1576. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1577. Lit:=TJSLiteral(Item);
  1578. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1579. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1580. end;
  1581. end;
  1582. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1583. out Src: TJSSourceElements);
  1584. var
  1585. FunDecl: TJSFunctionDeclarationStatement;
  1586. FunDef: TJSFuncDef;
  1587. FunBody: TJSFunctionBody;
  1588. begin
  1589. Src:=nil;
  1590. AssertNotNull(ParamName,Arg.Expr);
  1591. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1592. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1593. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1594. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1595. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1596. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1597. AssertNotNull(ParamName+' body',FunDef.Body);
  1598. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1599. FunBody:=FunDef.Body as TJSFunctionBody;
  1600. AssertNotNull(ParamName+' body.A',FunBody.A);
  1601. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1602. Src:=FunBody.A as TJSSourceElements;
  1603. end;
  1604. var
  1605. ModuleNameExpr: TJSLiteral;
  1606. InitFunction: TJSFunctionDeclarationStatement;
  1607. InitAssign: TJSSimpleAssignStatement;
  1608. InitName: String;
  1609. LastNode: TJSElement;
  1610. Arg: TJSArrayLiteralElement;
  1611. begin
  1612. if SkipTests then exit;
  1613. try
  1614. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1615. except
  1616. on E: Exception do
  1617. HandleException(E);
  1618. end;
  1619. if SkipTests then exit;
  1620. if ExpectedErrorClass<>nil then
  1621. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1622. FJSSource:=TStringList.Create;
  1623. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1624. {$IFDEF VerbosePas2JS}
  1625. writeln('TTestModule.ConvertModule JS:');
  1626. write(FJSSource.Text);
  1627. {$ENDIF}
  1628. // rtl.module(...
  1629. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1630. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1631. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1632. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1633. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1634. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1635. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1636. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1637. // parameter 'unitname'
  1638. if JSModuleCallArgs.Elements.Count<1 then
  1639. Fail('rtl.module first param unit missing');
  1640. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1641. AssertNotNull('module name param',Arg.Expr);
  1642. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1643. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1644. if Module is TPasProgram then
  1645. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1646. else
  1647. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1648. // main uses section
  1649. if JSModuleCallArgs.Elements.Count<2 then
  1650. Fail('rtl.module second param main uses missing');
  1651. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1652. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1653. // program/library/interface function()
  1654. if JSModuleCallArgs.Elements.Count<3 then
  1655. Fail('rtl.module third param intf-function missing');
  1656. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1657. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1658. // search for $mod.$init or $mod.$main - the last statement
  1659. if Module is TPasProgram then
  1660. begin
  1661. InitName:='$main';
  1662. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1663. end
  1664. else
  1665. InitName:='$init';
  1666. FJSInitBody:=nil;
  1667. if JSModuleSrc.Statements.Count>0 then
  1668. begin
  1669. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1670. if LastNode is TJSSimpleAssignStatement then
  1671. begin
  1672. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1673. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1674. begin
  1675. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1676. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1677. end
  1678. else if Module is TPasProgram then
  1679. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1680. end;
  1681. end;
  1682. // optional: implementation uses section
  1683. if JSModuleCallArgs.Elements.Count<4 then
  1684. exit;
  1685. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1686. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1687. // optional: implementation function()
  1688. if JSModuleCallArgs.Elements.Count<5 then
  1689. exit;
  1690. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1691. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1692. end;
  1693. procedure TCustomTestModule.ConvertProgram;
  1694. begin
  1695. Add('end.');
  1696. ParseProgram;
  1697. ConvertModule;
  1698. end;
  1699. procedure TCustomTestModule.ConvertUnit;
  1700. begin
  1701. Add('end.');
  1702. ParseUnit;
  1703. ConvertModule;
  1704. end;
  1705. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1706. begin
  1707. Result:=tcmodules.JSToStr(El);
  1708. end;
  1709. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1710. DottedName: string);
  1711. begin
  1712. if DottedName='' then
  1713. begin
  1714. AssertNull(Msg,El);
  1715. end
  1716. else
  1717. begin
  1718. AssertNotNull(Msg,El);
  1719. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1720. end;
  1721. end;
  1722. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1723. begin
  1724. if El=nil then
  1725. Result:=''
  1726. else if El is TJSPrimaryExpressionIdent then
  1727. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1728. else if El is TJSDotMemberExpression then
  1729. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1730. else
  1731. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1732. end;
  1733. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1734. InitStatements: string; ImplStatements: string);
  1735. var
  1736. ActualSrc, ExpectedSrc, InitName: String;
  1737. begin
  1738. ActualSrc:=JSToStr(JSModuleSrc);
  1739. ExpectedSrc:=
  1740. 'var $mod = this;'+LineEnding
  1741. +Statements;
  1742. if coUseStrict in Converter.Options then
  1743. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1744. if Module is TPasProgram then
  1745. InitName:='$main'
  1746. else
  1747. InitName:='$init';
  1748. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1749. ExpectedSrc:=ExpectedSrc+LineEnding
  1750. +'$mod.'+InitName+' = function () {'+LineEnding
  1751. +InitStatements
  1752. +'};'+LineEnding;
  1753. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1754. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1755. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1756. if (JSImplementationSrc<>nil) then
  1757. begin
  1758. ActualSrc:=JSToStr(JSImplementationSrc);
  1759. ExpectedSrc:=
  1760. 'var $mod = this;'+LineEnding
  1761. +'var $impl = $mod.$impl;'+LineEnding
  1762. +ImplStatements;
  1763. end
  1764. else
  1765. begin
  1766. ActualSrc:='';
  1767. ExpectedSrc:=ImplStatements;
  1768. end;
  1769. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1770. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1771. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1772. end;
  1773. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1774. // search diff, ignore changes in spaces
  1775. var
  1776. s: string;
  1777. begin
  1778. if CheckSrcDiff(Expected,Actual,s) then exit;
  1779. Fail(Msg+': '+s);
  1780. end;
  1781. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1782. var
  1783. aResolver: TTestEnginePasResolver;
  1784. aConverter: TPasToJSConverter;
  1785. aJSModule: TJSSourceElements;
  1786. ActualSrc: String;
  1787. begin
  1788. aResolver:=GetResolver(Filename);
  1789. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1790. {$IFDEF VerbosePas2JS}
  1791. writeln('CheckUnit '+Filename+' converting ...');
  1792. {$ENDIF}
  1793. aConverter:=CreateConverter;
  1794. aJSModule:=nil;
  1795. try
  1796. try
  1797. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1798. except
  1799. on E: Exception do
  1800. HandleException(E);
  1801. end;
  1802. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1803. {$IFDEF VerbosePas2JS}
  1804. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1805. write(aResolver.Source);
  1806. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1807. write(ActualSrc);
  1808. {$ENDIF}
  1809. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1810. finally
  1811. aJSModule.Free;
  1812. aConverter.Free;
  1813. end;
  1814. end;
  1815. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1816. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1817. var
  1818. i: Integer;
  1819. Item: TTestHintMessage;
  1820. Expected,Actual: string;
  1821. begin
  1822. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1823. for i:=0 to MsgCount-1 do
  1824. begin
  1825. Item:=Msgs[i];
  1826. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1827. if (Marker<>nil) then
  1828. begin
  1829. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1830. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1831. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1832. end;
  1833. // found
  1834. FHintMsgsGood.Add(Item);
  1835. str(Item.MsgType,Actual);
  1836. str(MsgType,Expected);
  1837. AssertEquals('MsgType',Expected,Actual);
  1838. exit;
  1839. end;
  1840. // needed message missing -> show emitted messages
  1841. WriteSources('',0,0);
  1842. for i:=0 to MsgCount-1 do
  1843. begin
  1844. Item:=Msgs[i];
  1845. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1846. ' ('+IntToStr(Item.MsgNumber),')');
  1847. if Marker<>nil then
  1848. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1849. writeln(' {',Item.Msg,'}');
  1850. end;
  1851. str(MsgType,Expected);
  1852. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1853. if Marker<>nil then
  1854. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1855. Actual:=Actual+' '+Msg;
  1856. Fail(Actual);
  1857. end;
  1858. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1859. );
  1860. var
  1861. i: Integer;
  1862. s, Txt: String;
  1863. Msg: TTestHintMessage;
  1864. begin
  1865. for i:=0 to MsgCount-1 do
  1866. begin
  1867. Msg:=Msgs[i];
  1868. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1869. s:='';
  1870. str(Msg.MsgType,s);
  1871. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1872. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1873. if WithSourcePos then
  1874. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1875. Txt:=Txt+' {'+Msg.Msg+'}';
  1876. Fail(Txt);
  1877. end;
  1878. end;
  1879. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1880. MsgNumber: integer);
  1881. begin
  1882. ExpectedErrorClass:=EScannerError;
  1883. ExpectedErrorMsg:=Msg;
  1884. ExpectedErrorNumber:=MsgNumber;
  1885. end;
  1886. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1887. MsgNumber: integer);
  1888. begin
  1889. ExpectedErrorClass:=EParserError;
  1890. ExpectedErrorMsg:=Msg;
  1891. ExpectedErrorNumber:=MsgNumber;
  1892. end;
  1893. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1894. MsgNumber: integer);
  1895. begin
  1896. ExpectedErrorClass:=EPasResolve;
  1897. ExpectedErrorMsg:=Msg;
  1898. ExpectedErrorNumber:=MsgNumber;
  1899. end;
  1900. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1901. MsgNumber: integer);
  1902. begin
  1903. ExpectedErrorClass:=EPas2JS;
  1904. ExpectedErrorMsg:=Msg;
  1905. ExpectedErrorNumber:=MsgNumber;
  1906. end;
  1907. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1908. var
  1909. MsgNumber: Integer;
  1910. Msg: String;
  1911. begin
  1912. Result:=false;
  1913. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1914. Msg:=E.Message;
  1915. if E is EPas2JS then
  1916. MsgNumber:=EPas2JS(E).MsgNumber
  1917. else if E is EPasResolve then
  1918. MsgNumber:=EPasResolve(E).MsgNumber
  1919. else if E is EParserError then
  1920. MsgNumber:=Parser.LastMsgNumber
  1921. else if E is EScannerError then
  1922. begin
  1923. MsgNumber:=Scanner.LastMsgNumber;
  1924. Msg:=Scanner.LastMsg;
  1925. end
  1926. else
  1927. MsgNumber:=0;
  1928. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  1929. if Result then
  1930. SkipTests:=true;
  1931. end;
  1932. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1933. begin
  1934. if IsErrorExpected(E) then exit;
  1935. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  1936. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  1937. +' '+Scanner.CurFilename
  1938. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  1939. FailException(E);
  1940. end;
  1941. procedure TCustomTestModule.HandleParserError(E: EParserError);
  1942. begin
  1943. if IsErrorExpected(E) then exit;
  1944. WriteSources(E.Filename,E.Row,E.Column);
  1945. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  1946. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  1947. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  1948. );
  1949. FailException(E);
  1950. end;
  1951. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  1952. var
  1953. P: TPasSourcePos;
  1954. begin
  1955. if IsErrorExpected(E) then exit;
  1956. P:=E.SourcePos;
  1957. WriteSources(P.FileName,P.Row,P.Column);
  1958. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  1959. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  1960. FailException(E);
  1961. end;
  1962. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  1963. var
  1964. Row, Col: integer;
  1965. begin
  1966. if IsErrorExpected(E) then exit;
  1967. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  1968. WriteSources(E.PasElement.SourceFilename,Row,Col);
  1969. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  1970. +' '+E.PasElement.SourceFilename
  1971. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  1972. FailException(E);
  1973. end;
  1974. procedure TCustomTestModule.HandleException(E: Exception);
  1975. begin
  1976. if E is EScannerError then
  1977. HandleScannerError(EScannerError(E))
  1978. else if E is EParserError then
  1979. HandleParserError(EParserError(E))
  1980. else if E is EPasResolve then
  1981. HandlePasResolveError(EPasResolve(E))
  1982. else if E is EPas2JS then
  1983. HandlePas2JSError(EPas2JS(E))
  1984. else
  1985. begin
  1986. if IsErrorExpected(E) then exit;
  1987. if not (E is EAssertionFailedError) then
  1988. begin
  1989. WriteSources('',0,0);
  1990. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  1991. end;
  1992. FailException(E);
  1993. end;
  1994. end;
  1995. procedure TCustomTestModule.FailException(E: Exception);
  1996. var
  1997. MsgNumber: Integer;
  1998. begin
  1999. if ExpectedErrorClass<>nil then
  2000. begin
  2001. if FExpectedErrorClass=E.ClassType then
  2002. begin
  2003. if E is EPas2JS then
  2004. MsgNumber:=EPas2JS(E).MsgNumber
  2005. else if E is EPasResolve then
  2006. MsgNumber:=EPasResolve(E).MsgNumber
  2007. else if E is EParserError then
  2008. MsgNumber:=Parser.LastMsgNumber
  2009. else if E is EScannerError then
  2010. MsgNumber:=Scanner.LastMsgNumber
  2011. else
  2012. MsgNumber:=0;
  2013. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2014. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2015. ExpectedErrorNumber,MsgNumber);
  2016. end else begin
  2017. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2018. end;
  2019. end;
  2020. Fail(E.Message);
  2021. end;
  2022. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2023. aCol: integer);
  2024. var
  2025. IsSrc: Boolean;
  2026. i, j: Integer;
  2027. SrcLines: TStringList;
  2028. Line: string;
  2029. aModule: TTestEnginePasResolver;
  2030. begin
  2031. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2032. for i:=0 to ResolverCount-1 do
  2033. begin
  2034. aModule:=Resolvers[i];
  2035. SrcLines:=TStringList.Create;
  2036. try
  2037. SrcLines.Text:=aModule.Source;
  2038. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2039. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2040. for j:=1 to SrcLines.Count do
  2041. begin
  2042. Line:=SrcLines[j-1];
  2043. if IsSrc and (j=aRow) then
  2044. begin
  2045. write('*');
  2046. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2047. end;
  2048. writeln(Format('%:4d: ',[j]),Line);
  2049. end;
  2050. finally
  2051. SrcLines.Free;
  2052. end;
  2053. end;
  2054. end;
  2055. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2056. var
  2057. i: Integer;
  2058. begin
  2059. for i:=0 to ResolverCount-1 do
  2060. if Filename=Resolvers[i].Filename then exit(i);
  2061. Result:=-1;
  2062. end;
  2063. function TCustomTestModule.GetResolver(const Filename: string
  2064. ): TTestEnginePasResolver;
  2065. var
  2066. i: Integer;
  2067. begin
  2068. i:=IndexOfResolver(Filename);
  2069. if i<0 then exit(nil);
  2070. Result:=Resolvers[i];
  2071. end;
  2072. function TCustomTestModule.GetDefaultNamespace: string;
  2073. var
  2074. C: TClass;
  2075. begin
  2076. Result:='';
  2077. if FModule=nil then exit;
  2078. C:=FModule.ClassType;
  2079. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2080. Result:=Engine.DefaultNameSpace;
  2081. end;
  2082. constructor TCustomTestModule.Create;
  2083. begin
  2084. inherited Create;
  2085. FHintMsgs:=TObjectList.Create(true);
  2086. FHintMsgsGood:=TFPList.Create;
  2087. end;
  2088. destructor TCustomTestModule.Destroy;
  2089. begin
  2090. FreeAndNil(FHintMsgs);
  2091. FreeAndNil(FHintMsgsGood);
  2092. inherited Destroy;
  2093. end;
  2094. { TTestModule }
  2095. procedure TTestModule.TestReservedWords;
  2096. var
  2097. i: integer;
  2098. begin
  2099. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2100. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2101. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2102. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2103. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2104. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2105. end;
  2106. procedure TTestModule.TestEmptyProgram;
  2107. begin
  2108. StartProgram(false);
  2109. Add('begin');
  2110. ConvertProgram;
  2111. CheckSource('TestEmptyProgram','','');
  2112. end;
  2113. procedure TTestModule.TestEmptyProgramUseStrict;
  2114. begin
  2115. Converter.Options:=Converter.Options+[coUseStrict];
  2116. StartProgram(false);
  2117. Add('begin');
  2118. ConvertProgram;
  2119. CheckSource('TestEmptyProgramUseStrict','','');
  2120. end;
  2121. procedure TTestModule.TestEmptyUnit;
  2122. begin
  2123. StartUnit(false);
  2124. Add('interface');
  2125. Add('implementation');
  2126. ConvertUnit;
  2127. CheckSource('TestEmptyUnit',
  2128. LinesToStr([
  2129. ]),
  2130. '');
  2131. end;
  2132. procedure TTestModule.TestEmptyUnitUseStrict;
  2133. begin
  2134. Converter.Options:=Converter.Options+[coUseStrict];
  2135. StartUnit(false);
  2136. Add('interface');
  2137. Add('implementation');
  2138. ConvertUnit;
  2139. CheckSource('TestEmptyUnitUseStrict',
  2140. LinesToStr([
  2141. ''
  2142. ]),
  2143. '');
  2144. end;
  2145. procedure TTestModule.TestDottedUnitNames;
  2146. begin
  2147. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2148. LinesToStr([
  2149. 'var iV: longint;'
  2150. ]),
  2151. '');
  2152. FFilename:='ns1.test1.pp';
  2153. StartProgram(true);
  2154. Add('uses unIt2;');
  2155. Add('implementation');
  2156. Add('var');
  2157. Add(' i: longint;');
  2158. Add('begin');
  2159. Add(' i:=iv;');
  2160. Add(' i:=uNit2.iv;');
  2161. Add(' i:=Ns1.TEst1.i;');
  2162. ConvertProgram;
  2163. CheckSource('TestDottedUnitNames',
  2164. LinesToStr([
  2165. 'this.i = 0;',
  2166. '']),
  2167. LinesToStr([ // this.$init
  2168. '$mod.i = pas["NS1.Unit2"].iV;',
  2169. '$mod.i = pas["NS1.Unit2"].iV;',
  2170. '$mod.i = $mod.i;',
  2171. '']) );
  2172. end;
  2173. procedure TTestModule.TestDottedUnitNameImpl;
  2174. begin
  2175. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2176. LinesToStr([
  2177. 'type',
  2178. ' TObject = class end;',
  2179. ' TTestA = class',
  2180. ' end;'
  2181. ]),
  2182. LinesToStr(['uses TEST.UnitB;'])
  2183. );
  2184. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2185. LinesToStr([
  2186. 'uses TEST.UnitA;',
  2187. 'type TTestB = class(TTestA);'
  2188. ]),
  2189. ''
  2190. );
  2191. StartProgram(true);
  2192. Add('uses TEST.UnitA;');
  2193. Add('begin');
  2194. ConvertProgram;
  2195. CheckSource('TestDottedUnitNameImpl',
  2196. LinesToStr([
  2197. '']),
  2198. LinesToStr([ // this.$init
  2199. '']) );
  2200. CheckUnit('TEST.UnitA.pas',
  2201. LinesToStr([
  2202. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2203. ' var $mod = this;',
  2204. ' rtl.createClass($mod, "TObject", null, function () {',
  2205. ' this.$init = function () {',
  2206. ' };',
  2207. ' this.$final = function () {',
  2208. ' };',
  2209. ' });',
  2210. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2211. ' });',
  2212. '}, ["TEST.UnitB"]);'
  2213. ]));
  2214. CheckUnit('TEST.UnitB.pas',
  2215. LinesToStr([
  2216. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2217. ' var $mod = this;',
  2218. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2219. ' });',
  2220. '});'
  2221. ]));
  2222. end;
  2223. procedure TTestModule.TestDottedUnitExpr;
  2224. begin
  2225. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2226. LinesToStr([
  2227. 'procedure DoIt;'
  2228. ]),
  2229. 'procedure DoIt; begin end;');
  2230. FFilename:='Ns1.SubNs1.Test1.pp';
  2231. StartProgram(true);
  2232. Add('uses Ns2.sUbnS2.unIt2;');
  2233. Add('implementation');
  2234. Add('var');
  2235. Add(' i: longint;');
  2236. Add('begin');
  2237. Add(' ns2.subns2.unit2.doit;');
  2238. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2239. ConvertProgram;
  2240. CheckSource('TestDottedUnitExpr',
  2241. LinesToStr([
  2242. 'this.i = 0;',
  2243. '']),
  2244. LinesToStr([ // this.$init
  2245. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2246. '$mod.i = $mod.i;',
  2247. '']) );
  2248. end;
  2249. procedure TTestModule.Test_ModeFPCFail;
  2250. begin
  2251. StartProgram(false);
  2252. Add('{$mode FPC}');
  2253. Add('begin');
  2254. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2255. ConvertProgram;
  2256. end;
  2257. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2258. begin
  2259. StartProgram(false);
  2260. Add('{$modeswitch cblocks-}');
  2261. Add('begin');
  2262. SetExpectedScannerError('Invalid mode switch: "cblocks-"',nErrInvalidModeSwitch);
  2263. ConvertProgram;
  2264. end;
  2265. procedure TTestModule.TestUnit_UseSystem;
  2266. begin
  2267. StartUnit(true);
  2268. Add([
  2269. 'interface',
  2270. 'var i: integer;',
  2271. 'implementation']);
  2272. ConvertUnit;
  2273. CheckSource('TestUnit_UseSystem',
  2274. LinesToStr([
  2275. 'this.i = 0;',
  2276. '']),
  2277. LinesToStr([
  2278. '']) );
  2279. end;
  2280. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2281. begin
  2282. AddModuleWithIntfImplSrc('unit1.pp',
  2283. LinesToStr([
  2284. 'type number = longint;']),
  2285. LinesToStr([
  2286. 'uses test1;',
  2287. 'procedure DoIt;',
  2288. 'begin',
  2289. ' i:=3;',
  2290. 'end;']));
  2291. StartUnit(true);
  2292. Add([
  2293. 'interface',
  2294. 'uses unit1;',
  2295. 'var i: number;',
  2296. 'implementation']);
  2297. ConvertUnit;
  2298. CheckSource('TestUnit_Intf1Impl2Intf1',
  2299. LinesToStr([
  2300. 'this.i = 0;',
  2301. '']),
  2302. LinesToStr([
  2303. '']) );
  2304. end;
  2305. procedure TTestModule.TestIncludeVersion;
  2306. begin
  2307. StartProgram(false);
  2308. Add([
  2309. 'var',
  2310. ' s: string;',
  2311. ' i: word;',
  2312. 'begin',
  2313. ' s:={$I %line%};',
  2314. ' i:={$I %linenum%};',
  2315. ' s:={$I %currentroutine%};',
  2316. ' s:={$I %pas2jsversion%};',
  2317. ' s:={$I %pas2jstarget%};',
  2318. ' s:={$I %pas2jstargetos%};',
  2319. ' s:={$I %pas2jstargetcpu%};',
  2320. ' s:={$I %file%};',
  2321. '']);
  2322. ConvertProgram;
  2323. CheckSource('TestIncludeVersion',
  2324. LinesToStr([
  2325. 'this.s="";',
  2326. 'this.i = 0;']),
  2327. LinesToStr([
  2328. '$mod.s = "7";',
  2329. '$mod.i = 8;',
  2330. '$mod.s = "<anonymous>";',
  2331. '$mod.s = "Comp.Ver.tcmodules";',
  2332. '$mod.s = "Browser";',
  2333. '$mod.s = "Browser";',
  2334. '$mod.s = "ECMAScript5";',
  2335. '$mod.s = "test1.pp";',
  2336. '']));
  2337. end;
  2338. procedure TTestModule.TestVarInt;
  2339. begin
  2340. StartProgram(false);
  2341. Add('var MyI: longint;');
  2342. Add('begin');
  2343. ConvertProgram;
  2344. CheckSource('TestVarInt','this.MyI=0;','');
  2345. end;
  2346. procedure TTestModule.TestVarBaseTypes;
  2347. begin
  2348. StartProgram(false);
  2349. Add('var');
  2350. Add(' i: longint;');
  2351. Add(' s: string;');
  2352. Add(' c: char;');
  2353. Add(' b: boolean;');
  2354. Add(' d: double;');
  2355. Add(' i2: longint = 3;');
  2356. Add(' s2: string = ''foo'';');
  2357. Add(' c2: char = ''4'';');
  2358. Add(' b2: boolean = true;');
  2359. Add(' d2: double = 5.6;');
  2360. Add(' i3: longint = $707;');
  2361. Add(' i4: nativeint = 4503599627370495;');
  2362. Add(' i5: nativeint = -4503599627370495-1;');
  2363. Add(' i6: nativeint = $fffffffffffff;');
  2364. Add(' i7: nativeint = -$fffffffffffff-1;');
  2365. Add(' i8: byte = 00;');
  2366. Add(' u8: nativeuint = $fffffffffffff;');
  2367. Add(' u9: nativeuint = $0000000000000;');
  2368. Add(' u10: nativeuint = $00ff00;');
  2369. Add('begin');
  2370. ConvertProgram;
  2371. CheckSource('TestVarBaseTypes',
  2372. LinesToStr([
  2373. 'this.i = 0;',
  2374. 'this.s = "";',
  2375. 'this.c = "";',
  2376. 'this.b = false;',
  2377. 'this.d = 0.0;',
  2378. 'this.i2 = 3;',
  2379. 'this.s2 = "foo";',
  2380. 'this.c2 = "4";',
  2381. 'this.b2 = true;',
  2382. 'this.d2 = 5.6;',
  2383. 'this.i3 = 0x707;',
  2384. 'this.i4 = 4503599627370495;',
  2385. 'this.i5 = -4503599627370495-1;',
  2386. 'this.i6 = 0xfffffffffffff;',
  2387. 'this.i7 =-0xfffffffffffff-1;',
  2388. 'this.i8 = 0;',
  2389. 'this.u8 = 0xfffffffffffff;',
  2390. 'this.u9 = 0x0;',
  2391. 'this.u10 = 0xff00;'
  2392. ]),
  2393. '');
  2394. end;
  2395. procedure TTestModule.TestBaseTypeSingleFail;
  2396. begin
  2397. StartProgram(false);
  2398. Add('var s: single;');
  2399. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2400. ConvertProgram;
  2401. end;
  2402. procedure TTestModule.TestBaseTypeExtendedFail;
  2403. begin
  2404. StartProgram(false);
  2405. Add('var e: extended;');
  2406. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2407. ConvertProgram;
  2408. end;
  2409. procedure TTestModule.TestConstBaseTypes;
  2410. begin
  2411. StartProgram(false);
  2412. Add('const');
  2413. Add(' i: longint = 3;');
  2414. Add(' s: string = ''foo'';');
  2415. Add(' c: char = ''4'';');
  2416. Add(' b: boolean = true;');
  2417. Add(' d: double = 5.6;');
  2418. Add(' e = low(word);');
  2419. Add(' f = high(word);');
  2420. Add('begin');
  2421. ConvertProgram;
  2422. CheckSource('TestVarBaseTypes',
  2423. LinesToStr([
  2424. 'this.i=3;',
  2425. 'this.s="foo";',
  2426. 'this.c="4";',
  2427. 'this.b=true;',
  2428. 'this.d=5.6;',
  2429. 'this.e = 0;',
  2430. 'this.f = 65535;'
  2431. ]),
  2432. '');
  2433. end;
  2434. procedure TTestModule.TestAliasTypeRef;
  2435. begin
  2436. StartProgram(false);
  2437. Add('type');
  2438. Add(' a=longint;');
  2439. Add(' b=a;');
  2440. Add('var');
  2441. Add(' c: A;');
  2442. Add(' d: B;');
  2443. Add('begin');
  2444. ConvertProgram;
  2445. CheckSource('TestAliasTypeRef',
  2446. LinesToStr([ // statements
  2447. 'this.c = 0;',
  2448. 'this.d = 0;'
  2449. ]),
  2450. LinesToStr([ // this.$main
  2451. ''
  2452. ]));
  2453. end;
  2454. procedure TTestModule.TestTypeCast_BaseTypes;
  2455. begin
  2456. StartProgram(false);
  2457. Add([
  2458. 'var',
  2459. ' i: longint;',
  2460. ' b: boolean;',
  2461. ' d: double;',
  2462. ' s: string;',
  2463. ' c: char;',
  2464. 'begin',
  2465. ' i:=longint(i);',
  2466. ' i:=longint(b);',
  2467. ' b:=boolean(b);',
  2468. ' b:=boolean(i);',
  2469. ' d:=double(d);',
  2470. ' d:=double(i);',
  2471. ' s:=string(s);',
  2472. ' s:=string(c);',
  2473. ' c:=char(c);',
  2474. ' c:=char(i);',
  2475. ' c:=char(65);',
  2476. ' c:=char(#10);',
  2477. ' c:=char(#$E000);',
  2478. '']);
  2479. ConvertProgram;
  2480. CheckSource('TestAliasTypeRef',
  2481. LinesToStr([ // statements
  2482. 'this.i = 0;',
  2483. 'this.b = false;',
  2484. 'this.d = 0.0;',
  2485. 'this.s = "";',
  2486. 'this.c = "";',
  2487. '']),
  2488. LinesToStr([ // this.$main
  2489. '$mod.i = $mod.i;',
  2490. '$mod.i = ($mod.b ? 1 : 0);',
  2491. '$mod.b = $mod.b;',
  2492. '$mod.b = $mod.i != 0;',
  2493. '$mod.d = $mod.d;',
  2494. '$mod.d = $mod.i;',
  2495. '$mod.s = $mod.s;',
  2496. '$mod.s = $mod.c;',
  2497. '$mod.c = $mod.c;',
  2498. '$mod.c = String.fromCharCode($mod.i);',
  2499. '$mod.c = "A";',
  2500. '$mod.c = "\n";',
  2501. '$mod.c = "";',
  2502. '']));
  2503. end;
  2504. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2505. begin
  2506. StartProgram(false);
  2507. Add('type');
  2508. Add(' integer = longint;');
  2509. Add(' TYesNo = boolean;');
  2510. Add(' TFloat = double;');
  2511. Add(' TCaption = string;');
  2512. Add(' TChar = char;');
  2513. Add('var');
  2514. Add(' i: integer;');
  2515. Add(' b: TYesNo;');
  2516. Add(' d: TFloat;');
  2517. Add(' s: TCaption;');
  2518. Add(' c: TChar;');
  2519. Add('begin');
  2520. Add(' i:=integer(i);');
  2521. Add(' i:=integer(b);');
  2522. Add(' b:=TYesNo(b);');
  2523. Add(' b:=TYesNo(i);');
  2524. Add(' d:=TFloat(d);');
  2525. Add(' d:=TFloat(i);');
  2526. Add(' s:=TCaption(s);');
  2527. Add(' s:=TCaption(c);');
  2528. Add(' c:=TChar(c);');
  2529. ConvertProgram;
  2530. CheckSource('TestAliasTypeRef',
  2531. LinesToStr([ // statements
  2532. 'this.i = 0;',
  2533. 'this.b = false;',
  2534. 'this.d = 0.0;',
  2535. 'this.s = "";',
  2536. 'this.c = "";',
  2537. '']),
  2538. LinesToStr([ // this.$main
  2539. '$mod.i = $mod.i;',
  2540. '$mod.i = ($mod.b ? 1 : 0);',
  2541. '$mod.b = $mod.b;',
  2542. '$mod.b = $mod.i != 0;',
  2543. '$mod.d = $mod.d;',
  2544. '$mod.d = $mod.i;',
  2545. '$mod.s = $mod.s;',
  2546. '$mod.s = $mod.c;',
  2547. '$mod.c = $mod.c;',
  2548. '']));
  2549. end;
  2550. procedure TTestModule.TestEmptyProc;
  2551. begin
  2552. StartProgram(false);
  2553. Add('procedure Test;');
  2554. Add('begin');
  2555. Add('end;');
  2556. Add('begin');
  2557. ConvertProgram;
  2558. CheckSource('TestEmptyProc',
  2559. LinesToStr([ // statements
  2560. 'this.Test = function () {',
  2561. '};'
  2562. ]),
  2563. LinesToStr([ // this.$main
  2564. ''
  2565. ]));
  2566. end;
  2567. procedure TTestModule.TestProcOneParam;
  2568. begin
  2569. StartProgram(false);
  2570. Add('procedure ProcA(i: longint);');
  2571. Add('begin');
  2572. Add('end;');
  2573. Add('begin');
  2574. Add(' PROCA(3);');
  2575. ConvertProgram;
  2576. CheckSource('TestProcOneParam',
  2577. LinesToStr([ // statements
  2578. 'this.ProcA = function (i) {',
  2579. '};'
  2580. ]),
  2581. LinesToStr([ // this.$main
  2582. '$mod.ProcA(3);'
  2583. ]));
  2584. end;
  2585. procedure TTestModule.TestFunctionWithoutParams;
  2586. begin
  2587. StartProgram(false);
  2588. Add('function FuncA: longint;');
  2589. Add('begin');
  2590. Add('end;');
  2591. Add('var i: longint;');
  2592. Add('begin');
  2593. Add(' I:=FUNCA();');
  2594. Add(' I:=FUNCA;');
  2595. Add(' FUNCA();');
  2596. Add(' FUNCA;');
  2597. ConvertProgram;
  2598. CheckSource('TestProcWithoutParams',
  2599. LinesToStr([ // statements
  2600. 'this.FuncA = function () {',
  2601. ' var Result = 0;',
  2602. ' return Result;',
  2603. '};',
  2604. 'this.i=0;'
  2605. ]),
  2606. LinesToStr([ // this.$main
  2607. '$mod.i=$mod.FuncA();',
  2608. '$mod.i=$mod.FuncA();',
  2609. '$mod.FuncA();',
  2610. '$mod.FuncA();'
  2611. ]));
  2612. end;
  2613. procedure TTestModule.TestProcedureWithoutParams;
  2614. begin
  2615. StartProgram(false);
  2616. Add('procedure ProcA;');
  2617. Add('begin');
  2618. Add('end;');
  2619. Add('begin');
  2620. Add(' PROCA();');
  2621. Add(' PROCA;');
  2622. ConvertProgram;
  2623. CheckSource('TestProcWithoutParams',
  2624. LinesToStr([ // statements
  2625. 'this.ProcA = function () {',
  2626. '};'
  2627. ]),
  2628. LinesToStr([ // this.$main
  2629. '$mod.ProcA();',
  2630. '$mod.ProcA();'
  2631. ]));
  2632. end;
  2633. procedure TTestModule.TestIncDec;
  2634. begin
  2635. StartProgram(false);
  2636. Add([
  2637. 'procedure DoIt(var i: longint);',
  2638. 'begin',
  2639. ' inc(i);',
  2640. ' inc(i,2);',
  2641. 'end;',
  2642. 'var',
  2643. ' Bar: longint;',
  2644. 'begin',
  2645. ' inc(bar);',
  2646. ' inc(bar,2);',
  2647. ' dec(bar);',
  2648. ' dec(bar,3);',
  2649. '']);
  2650. ConvertProgram;
  2651. CheckSource('TestIncDec',
  2652. LinesToStr([ // statements
  2653. 'this.DoIt = function (i) {',
  2654. ' i.set(i.get()+1);',
  2655. ' i.set(i.get()+2);',
  2656. '};',
  2657. 'this.Bar = 0;'
  2658. ]),
  2659. LinesToStr([ // this.$main
  2660. '$mod.Bar+=1;',
  2661. '$mod.Bar+=2;',
  2662. '$mod.Bar-=1;',
  2663. '$mod.Bar-=3;'
  2664. ]));
  2665. end;
  2666. procedure TTestModule.TestLoHiFpcMode;
  2667. begin
  2668. StartProgram(false);
  2669. Add([
  2670. '{$mode objfpc}',
  2671. 'const',
  2672. ' LoByte1 = Lo(Word($1234));',
  2673. ' HiByte1 = Hi(Word($1234));',
  2674. ' LoByte2 = Lo(SmallInt($1234));',
  2675. ' HiByte2 = Hi(SmallInt($1234));',
  2676. ' LoWord1 = Lo($1234CDEF);',
  2677. ' HiWord1 = Hi($1234CDEF);',
  2678. ' LoWord2 = Lo(-$1234CDEF);',
  2679. ' HiWord2 = Hi(-$1234CDEF);',
  2680. ' lo4:byte=lo(byte($34));',
  2681. ' hi4:byte=hi(byte($34));',
  2682. ' lo5:byte=lo(shortint(-$34));',
  2683. ' hi5:byte=hi(shortint(-$34));',
  2684. ' lo6:longword=lo($123456789ABCD);',
  2685. ' hi6:longword=hi($123456789ABCD);',
  2686. ' lo7:longword=lo(-$123456789ABCD);',
  2687. ' hi7:longword=hi(-$123456789ABCD);',
  2688. 'var',
  2689. ' b: Byte;',
  2690. ' ss: shortint;',
  2691. ' w: Word;',
  2692. ' si: SmallInt;',
  2693. ' lw: LongWord;',
  2694. ' li: LongInt;',
  2695. ' b2: Byte;',
  2696. ' ni: nativeint;',
  2697. 'begin',
  2698. ' w := $1234;',
  2699. ' ss := -$12;',
  2700. ' b := lo(ss);',
  2701. ' b := HI(ss);',
  2702. ' b := lo(w);',
  2703. ' b := HI(w);',
  2704. ' b2 := lo(b);',
  2705. ' b2 := hi(b);',
  2706. ' lw := $1234CDEF;',
  2707. ' w := lo(lw);',
  2708. ' w := hi(lw);',
  2709. ' ni := $123456789ABCD;',
  2710. ' lw := lo(ni);',
  2711. ' lw := hi(ni);',
  2712. '']);
  2713. ConvertProgram;
  2714. CheckSource('TestLoHiFpcMode',
  2715. LinesToStr([ // statements
  2716. 'this.LoByte1 = 0x1234 & 0xFF;',
  2717. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2718. 'this.LoByte2 = 0x1234 & 0xFF;',
  2719. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2720. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2721. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2722. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2723. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2724. 'this.lo4 = 0x34 & 0xF;',
  2725. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2726. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2727. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2728. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2729. 'this.hi6 = 74565 >>> 0;',
  2730. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2731. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2732. 'this.b = 0;',
  2733. 'this.ss = 0;',
  2734. 'this.w = 0;',
  2735. 'this.si = 0;',
  2736. 'this.lw = 0;',
  2737. 'this.li = 0;',
  2738. 'this.b2 = 0;',
  2739. 'this.ni = 0;',
  2740. '']),
  2741. LinesToStr([ // this.$main
  2742. '$mod.w = 0x1234;',
  2743. '$mod.ss = -0x12;',
  2744. '$mod.b = $mod.ss & 0xFF;',
  2745. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2746. '$mod.b = $mod.w & 0xFF;',
  2747. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2748. '$mod.b2 = $mod.b & 0xF;',
  2749. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2750. '$mod.lw = 0x1234CDEF;',
  2751. '$mod.w = $mod.lw & 0xFFFF;',
  2752. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2753. '$mod.ni = 0x123456789ABCD;',
  2754. '$mod.lw = $mod.ni >>> 0;',
  2755. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2756. '']));
  2757. end;
  2758. procedure TTestModule.TestLoHiDelphiMode;
  2759. begin
  2760. StartProgram(false);
  2761. Add([
  2762. '{$mode delphi}',
  2763. 'const',
  2764. ' LoByte1 = Lo(Word($1234));',
  2765. ' HiByte1 = Hi(Word($1234));',
  2766. ' LoByte2 = Lo(SmallInt($1234));',
  2767. ' HiByte2 = Hi(SmallInt($1234));',
  2768. ' LoByte3 = Lo($1234CDEF);',
  2769. ' HiByte3 = Hi($1234CDEF);',
  2770. ' LoByte4 = Lo(-$1234CDEF);',
  2771. ' HiByte4 = Hi(-$1234CDEF);',
  2772. 'var',
  2773. ' b: Byte;',
  2774. ' w: Word;',
  2775. ' si: SmallInt;',
  2776. ' lw: LongWord;',
  2777. ' li: LongInt;',
  2778. 'begin',
  2779. ' w := $1234;',
  2780. ' b := lo(w);',
  2781. ' b := HI(w);',
  2782. ' lw := $1234CDEF;',
  2783. ' b := lo(lw);',
  2784. ' b := hi(lw);',
  2785. '']);
  2786. ConvertProgram;
  2787. CheckSource('TestLoHiDelphiMode',
  2788. LinesToStr([ // statements
  2789. 'this.LoByte1 = 0x1234 & 0xFF;',
  2790. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2791. 'this.LoByte2 = 0x1234 & 0xFF;',
  2792. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2793. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2794. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2795. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2796. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2797. 'this.b = 0;',
  2798. 'this.w = 0;',
  2799. 'this.si = 0;',
  2800. 'this.lw = 0;',
  2801. 'this.li = 0;'
  2802. ]),
  2803. LinesToStr([ // this.$main
  2804. '$mod.w = 0x1234;',
  2805. '$mod.b = $mod.w & 0xFF;',
  2806. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2807. '$mod.lw = 0x1234CDEF;',
  2808. '$mod.b = $mod.lw & 0xFF;',
  2809. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2810. ]));
  2811. end;
  2812. procedure TTestModule.TestAssignments;
  2813. begin
  2814. StartProgram(false);
  2815. Parser.Options:=Parser.Options+[po_cassignments];
  2816. Add('var');
  2817. Add(' Bar:longint;');
  2818. Add('begin');
  2819. Add(' bar:=3;');
  2820. Add(' bar+=4;');
  2821. Add(' bar-=5;');
  2822. Add(' bar*=6;');
  2823. ConvertProgram;
  2824. CheckSource('TestAssignments',
  2825. LinesToStr([ // statements
  2826. 'this.Bar = 0;'
  2827. ]),
  2828. LinesToStr([ // this.$main
  2829. '$mod.Bar=3;',
  2830. '$mod.Bar+=4;',
  2831. '$mod.Bar-=5;',
  2832. '$mod.Bar*=6;'
  2833. ]));
  2834. end;
  2835. procedure TTestModule.TestArithmeticOperators1;
  2836. begin
  2837. StartProgram(false);
  2838. Add('var');
  2839. Add(' vA,vB,vC:longint;');
  2840. Add('begin');
  2841. Add(' va:=1;');
  2842. Add(' vb:=va+va;');
  2843. Add(' vb:=va div vb;');
  2844. Add(' vb:=va mod vb;');
  2845. Add(' vb:=va+va*vb+va div vb;');
  2846. Add(' vc:=-va;');
  2847. Add(' va:=va-vb;');
  2848. Add(' vb:=va;');
  2849. Add(' if va<vb then vc:=va else vc:=vb;');
  2850. ConvertProgram;
  2851. CheckSource('TestArithmeticOperators1',
  2852. LinesToStr([ // statements
  2853. 'this.vA = 0;',
  2854. 'this.vB = 0;',
  2855. 'this.vC = 0;'
  2856. ]),
  2857. LinesToStr([ // this.$main
  2858. '$mod.vA = 1;',
  2859. '$mod.vB = $mod.vA + $mod.vA;',
  2860. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  2861. '$mod.vB = $mod.vA % $mod.vB;',
  2862. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  2863. '$mod.vC = -$mod.vA;',
  2864. '$mod.vA = $mod.vA - $mod.vB;',
  2865. '$mod.vB = $mod.vA;',
  2866. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2867. ]));
  2868. end;
  2869. procedure TTestModule.TestLogicalOperators;
  2870. begin
  2871. StartProgram(false);
  2872. Add('var');
  2873. Add(' vA,vB,vC:boolean;');
  2874. Add('begin');
  2875. Add(' va:=vb and vc;');
  2876. Add(' va:=vb or vc;');
  2877. Add(' va:=vb xor vc;');
  2878. Add(' va:=true and vc;');
  2879. Add(' va:=(vb and vc) or (va and vb);');
  2880. Add(' va:=not vb;');
  2881. ConvertProgram;
  2882. CheckSource('TestLogicalOperators',
  2883. LinesToStr([ // statements
  2884. 'this.vA = false;',
  2885. 'this.vB = false;',
  2886. 'this.vC = false;'
  2887. ]),
  2888. LinesToStr([ // this.$main
  2889. '$mod.vA = $mod.vB && $mod.vC;',
  2890. '$mod.vA = $mod.vB || $mod.vC;',
  2891. '$mod.vA = $mod.vB ^ $mod.vC;',
  2892. '$mod.vA = true && $mod.vC;',
  2893. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  2894. '$mod.vA = !$mod.vB;'
  2895. ]));
  2896. end;
  2897. procedure TTestModule.TestBitwiseOperators;
  2898. begin
  2899. StartProgram(false);
  2900. Add([
  2901. 'var',
  2902. ' vA,vB,vC:longint;',
  2903. ' X,Y,Z: nativeint;',
  2904. 'begin',
  2905. ' va:=vb and vc;',
  2906. ' va:=vb or vc;',
  2907. ' va:=vb xor vc;',
  2908. ' va:=vb shl vc;',
  2909. ' va:=vb shr vc;',
  2910. ' va:=3 and vc;',
  2911. ' va:=(vb and vc) or (va and vb);',
  2912. ' va:=not vb;',
  2913. ' X:=Y and Z;',
  2914. ' X:=Y and va;',
  2915. ' X:=Y or Z;',
  2916. ' X:=Y or va;',
  2917. ' X:=Y xor Z;',
  2918. ' X:=Y xor va;',
  2919. '']);
  2920. ConvertProgram;
  2921. CheckSource('TestBitwiseOperators',
  2922. LinesToStr([ // statements
  2923. 'this.vA = 0;',
  2924. 'this.vB = 0;',
  2925. 'this.vC = 0;',
  2926. 'this.X = 0;',
  2927. 'this.Y = 0;',
  2928. 'this.Z = 0;',
  2929. '']),
  2930. LinesToStr([ // this.$main
  2931. '$mod.vA = $mod.vB & $mod.vC;',
  2932. '$mod.vA = $mod.vB | $mod.vC;',
  2933. '$mod.vA = $mod.vB ^ $mod.vC;',
  2934. '$mod.vA = $mod.vB << $mod.vC;',
  2935. '$mod.vA = $mod.vB >>> $mod.vC;',
  2936. '$mod.vA = 3 & $mod.vC;',
  2937. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  2938. '$mod.vA = ~$mod.vB;',
  2939. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  2940. '$mod.X = $mod.Y & $mod.vA;',
  2941. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  2942. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  2943. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  2944. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  2945. '']));
  2946. end;
  2947. procedure TTestModule.TestPrgProcVar;
  2948. begin
  2949. StartProgram(false);
  2950. Add('procedure Proc1;');
  2951. Add('type');
  2952. Add(' t1=longint;');
  2953. Add('var');
  2954. Add(' vA:t1;');
  2955. Add('begin');
  2956. Add('end;');
  2957. Add('begin');
  2958. ConvertProgram;
  2959. CheckSource('TestPrgProcVar',
  2960. LinesToStr([ // statements
  2961. 'this.Proc1 = function () {',
  2962. ' var vA=0;',
  2963. '};'
  2964. ]),
  2965. LinesToStr([ // this.$main
  2966. ''
  2967. ]));
  2968. end;
  2969. procedure TTestModule.TestUnitProcVar;
  2970. begin
  2971. StartUnit(false);
  2972. Add('interface');
  2973. Add('');
  2974. Add('type tA=string; // unit scope');
  2975. Add('procedure Proc1;');
  2976. Add('');
  2977. Add('implementation');
  2978. Add('');
  2979. Add('procedure Proc1;');
  2980. Add('type tA=longint; // local proc scope');
  2981. Add('var v1:tA; // using local tA');
  2982. Add('begin');
  2983. Add('end;');
  2984. Add('var v2:tA; // using interface tA');
  2985. ConvertUnit;
  2986. CheckSource('TestUnitProcVar',
  2987. LinesToStr([ // statements
  2988. 'var $impl = $mod.$impl;',
  2989. 'this.Proc1 = function () {',
  2990. ' var v1 = 0;',
  2991. '};',
  2992. '']),
  2993. // this.$init
  2994. '',
  2995. // implementation
  2996. LinesToStr([
  2997. '$impl.v2 = "";',
  2998. '']));
  2999. end;
  3000. procedure TTestModule.TestImplProc;
  3001. begin
  3002. StartUnit(false);
  3003. Add('interface');
  3004. Add('');
  3005. Add('procedure Proc1;');
  3006. Add('');
  3007. Add('implementation');
  3008. Add('');
  3009. Add('procedure Proc1; begin end;');
  3010. Add('procedure Proc2; begin end;');
  3011. Add('initialization');
  3012. Add(' Proc1;');
  3013. Add(' Proc2;');
  3014. ConvertUnit;
  3015. CheckSource('TestImplProc',
  3016. LinesToStr([ // statements
  3017. 'var $impl = $mod.$impl;',
  3018. 'this.Proc1 = function () {',
  3019. '};',
  3020. '']),
  3021. LinesToStr([ // this.$init
  3022. '$mod.Proc1();',
  3023. '$impl.Proc2();',
  3024. '']),
  3025. LinesToStr([ // implementation
  3026. '$impl.Proc2 = function () {',
  3027. '};',
  3028. ''])
  3029. );
  3030. end;
  3031. procedure TTestModule.TestFunctionResult;
  3032. begin
  3033. StartProgram(false);
  3034. Add('function Func1: longint;');
  3035. Add('begin');
  3036. Add(' Result:=3;');
  3037. Add(' Func1:=4;');
  3038. Add('end;');
  3039. Add('begin');
  3040. ConvertProgram;
  3041. CheckSource('TestFunctionResult',
  3042. LinesToStr([ // statements
  3043. 'this.Func1 = function () {',
  3044. ' var Result = 0;',
  3045. ' Result = 3;',
  3046. ' Result = 4;',
  3047. ' return Result;',
  3048. '};'
  3049. ]),
  3050. '');
  3051. end;
  3052. procedure TTestModule.TestNestedProc;
  3053. begin
  3054. StartProgram(false);
  3055. Add([
  3056. 'var vInUnit: longint;',
  3057. 'function DoIt(pA,pD: longint): longint;',
  3058. 'var',
  3059. ' vB: longint;',
  3060. ' vC: longint;',
  3061. ' function Nesty(pA: longint): longint; ',
  3062. ' var vB: longint;',
  3063. ' begin',
  3064. ' Result:=pa+vb+vc+pd+vInUnit;',
  3065. ' nesty:=3;',
  3066. ' doit:=4;',
  3067. ' exit;',
  3068. ' end;',
  3069. 'begin',
  3070. ' Result:=pa+vb+vc;',
  3071. ' doit:=6;',
  3072. ' exit;',
  3073. 'end;',
  3074. 'begin']);
  3075. ConvertProgram;
  3076. CheckSource('TestNestedProc',
  3077. LinesToStr([ // statements
  3078. 'this.vInUnit = 0;',
  3079. 'this.DoIt = function (pA, pD) {',
  3080. ' var Result = 0;',
  3081. ' var vB = 0;',
  3082. ' var vC = 0;',
  3083. ' function Nesty(pA) {',
  3084. ' var Result$1 = 0;',
  3085. ' var vB = 0;',
  3086. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3087. ' Result$1 = 3;',
  3088. ' Result = 4;',
  3089. ' return Result$1;',
  3090. ' return Result$1;',
  3091. ' };',
  3092. ' Result = pA + vB + vC;',
  3093. ' Result = 6;',
  3094. ' return Result;',
  3095. ' return Result;',
  3096. '};'
  3097. ]),
  3098. '');
  3099. end;
  3100. procedure TTestModule.TestNestedProc_ResultString;
  3101. begin
  3102. StartProgram(false);
  3103. Add([
  3104. 'function DoIt: string;',
  3105. ' function Nesty: string; ',
  3106. ' begin',
  3107. ' nesty:=#65#66;',
  3108. ' nesty[1]:=#67;',
  3109. ' doit:=#68;',
  3110. ' doit[2]:=#69;',
  3111. ' end;',
  3112. 'begin',
  3113. ' doit:=#70;',
  3114. ' doit[3]:=#71;',
  3115. 'end;',
  3116. 'begin']);
  3117. ConvertProgram;
  3118. CheckSource('TestNestedProc_ResultString',
  3119. LinesToStr([ // statements
  3120. 'this.DoIt = function () {',
  3121. ' var Result = "";',
  3122. ' function Nesty() {',
  3123. ' var Result$1 = "";',
  3124. ' Result$1 = "AB";',
  3125. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3126. ' Result = "D";',
  3127. ' Result = rtl.setCharAt(Result, 1, "E");',
  3128. ' return Result$1;',
  3129. ' };',
  3130. ' Result = "F";',
  3131. ' Result = rtl.setCharAt(Result, 2, "G");',
  3132. ' return Result;',
  3133. '};'
  3134. ]),
  3135. '');
  3136. end;
  3137. procedure TTestModule.TestForwardProc;
  3138. begin
  3139. StartProgram(false);
  3140. Add('procedure FuncA(Bar: longint); forward;');
  3141. Add('procedure FuncB(Bar: longint);');
  3142. Add('begin');
  3143. Add(' funca(bar);');
  3144. Add('end;');
  3145. Add('procedure funca(bar: longint);');
  3146. Add('begin');
  3147. Add(' if bar=3 then ;');
  3148. Add('end;');
  3149. Add('begin');
  3150. Add(' funca(4);');
  3151. Add(' funcb(5);');
  3152. ConvertProgram;
  3153. CheckSource('TestForwardProc',
  3154. LinesToStr([ // statements'
  3155. 'this.FuncB = function (Bar) {',
  3156. ' $mod.FuncA(Bar);',
  3157. '};',
  3158. 'this.FuncA = function (Bar) {',
  3159. ' if (Bar === 3);',
  3160. '};'
  3161. ]),
  3162. LinesToStr([
  3163. '$mod.FuncA(4);',
  3164. '$mod.FuncB(5);'
  3165. ])
  3166. );
  3167. end;
  3168. procedure TTestModule.TestNestedForwardProc;
  3169. begin
  3170. StartProgram(false);
  3171. Add('procedure FuncA;');
  3172. Add(' procedure FuncB(i: longint); forward;');
  3173. Add(' procedure FuncC(i: longint);');
  3174. Add(' begin');
  3175. Add(' funcb(i);');
  3176. Add(' end;');
  3177. Add(' procedure FuncB(i: longint);');
  3178. Add(' begin');
  3179. Add(' if i=3 then ;');
  3180. Add(' end;');
  3181. Add('begin');
  3182. Add(' funcc(4)');
  3183. Add('end;');
  3184. Add('begin');
  3185. Add(' funca;');
  3186. ConvertProgram;
  3187. CheckSource('TestNestedForwardProc',
  3188. LinesToStr([ // statements'
  3189. 'this.FuncA = function () {',
  3190. ' function FuncC(i) {',
  3191. ' FuncB(i);',
  3192. ' };',
  3193. ' function FuncB(i) {',
  3194. ' if (i === 3);',
  3195. ' };',
  3196. ' FuncC(4);',
  3197. '};'
  3198. ]),
  3199. LinesToStr([
  3200. '$mod.FuncA();'
  3201. ])
  3202. );
  3203. end;
  3204. procedure TTestModule.TestAssignFunctionResult;
  3205. begin
  3206. StartProgram(false);
  3207. Add('function Func1: longint;');
  3208. Add('begin');
  3209. Add('end;');
  3210. Add('var i: longint;');
  3211. Add('begin');
  3212. Add(' i:=func1();');
  3213. Add(' i:=func1()+func1();');
  3214. ConvertProgram;
  3215. CheckSource('TestAssignFunctionResult',
  3216. LinesToStr([ // statements
  3217. 'this.Func1 = function () {',
  3218. ' var Result = 0;',
  3219. ' return Result;',
  3220. '};',
  3221. 'this.i = 0;'
  3222. ]),
  3223. LinesToStr([
  3224. '$mod.i = $mod.Func1();',
  3225. '$mod.i = $mod.Func1() + $mod.Func1();'
  3226. ]));
  3227. end;
  3228. procedure TTestModule.TestFunctionResultInCondition;
  3229. begin
  3230. StartProgram(false);
  3231. Add('function Func1: longint;');
  3232. Add('begin');
  3233. Add('end;');
  3234. Add('function Func2: boolean;');
  3235. Add('begin');
  3236. Add('end;');
  3237. Add('var i: longint;');
  3238. Add('begin');
  3239. Add(' if func2 then ;');
  3240. Add(' if i=func1() then ;');
  3241. Add(' if i=func1 then ;');
  3242. ConvertProgram;
  3243. CheckSource('TestFunctionResultInCondition',
  3244. LinesToStr([ // statements
  3245. 'this.Func1 = function () {',
  3246. ' var Result = 0;',
  3247. ' return Result;',
  3248. '};',
  3249. 'this.Func2 = function () {',
  3250. ' var Result = false;',
  3251. ' return Result;',
  3252. '};',
  3253. 'this.i = 0;'
  3254. ]),
  3255. LinesToStr([
  3256. 'if ($mod.Func2());',
  3257. 'if ($mod.i === $mod.Func1());',
  3258. 'if ($mod.i === $mod.Func1());'
  3259. ]));
  3260. end;
  3261. procedure TTestModule.TestFunctionResultInForLoop;
  3262. begin
  3263. StartProgram(false);
  3264. Add([
  3265. 'function Func1(a: array of longint): longint;',
  3266. 'begin',
  3267. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3268. ' for Result in a do if a[Result]=0 then exit;',
  3269. 'end;',
  3270. 'begin',
  3271. ' Func1([1,2,3])']);
  3272. ConvertProgram;
  3273. CheckSource('TestFunctionResultInForLoop',
  3274. LinesToStr([ // statements
  3275. 'this.Func1 = function (a) {',
  3276. ' var Result = 0;',
  3277. ' for (var $l1 = rtl.length(a) - 1; $l1 >= 0; $l1--) {',
  3278. ' Result = $l1;',
  3279. ' if (a[Result] === 0) return Result;',
  3280. ' };',
  3281. ' for (var $in2 = a, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) {',
  3282. ' Result = $in2[$l3];',
  3283. ' if (a[Result] === 0) return Result;',
  3284. ' };',
  3285. ' return Result;',
  3286. '};',
  3287. '']),
  3288. LinesToStr([
  3289. '$mod.Func1([1, 2, 3]);'
  3290. ]));
  3291. end;
  3292. procedure TTestModule.TestFunctionResultInTypeCast;
  3293. begin
  3294. StartProgram(false);
  3295. Add([
  3296. 'function GetInt: longint;',
  3297. 'begin',
  3298. 'end;',
  3299. 'begin',
  3300. ' if Byte(GetInt)=0 then ;',
  3301. '']);
  3302. ConvertProgram;
  3303. CheckSource('TestFunctionResultInTypeCast',
  3304. LinesToStr([ // statements
  3305. 'this.GetInt = function () {',
  3306. ' var Result = 0;',
  3307. ' return Result;',
  3308. '};',
  3309. '']),
  3310. LinesToStr([
  3311. 'if (($mod.GetInt() & 255) === 0) ;'
  3312. ]));
  3313. end;
  3314. procedure TTestModule.TestExit;
  3315. begin
  3316. StartProgram(false);
  3317. Add('procedure ProcA;');
  3318. Add('begin');
  3319. Add(' exit;');
  3320. Add('end;');
  3321. Add('function FuncB: longint;');
  3322. Add('begin');
  3323. Add(' exit;');
  3324. Add(' exit(3);');
  3325. Add('end;');
  3326. Add('function FuncC: string;');
  3327. Add('begin');
  3328. Add(' exit;');
  3329. Add(' exit(''a'');');
  3330. Add(' exit(''abc'');');
  3331. Add('end;');
  3332. Add('begin');
  3333. Add(' exit;');
  3334. Add(' exit(1);');
  3335. ConvertProgram;
  3336. CheckSource('TestExit',
  3337. LinesToStr([ // statements
  3338. 'this.ProcA = function () {',
  3339. ' return;',
  3340. '};',
  3341. 'this.FuncB = function () {',
  3342. ' var Result = 0;',
  3343. ' return Result;',
  3344. ' return 3;',
  3345. ' return Result;',
  3346. '};',
  3347. 'this.FuncC = function () {',
  3348. ' var Result = "";',
  3349. ' return Result;',
  3350. ' return "a";',
  3351. ' return "abc";',
  3352. ' return Result;',
  3353. '};'
  3354. ]),
  3355. LinesToStr([
  3356. 'return;',
  3357. 'return 1;',
  3358. '']));
  3359. end;
  3360. procedure TTestModule.TestBreak;
  3361. begin
  3362. StartProgram(false);
  3363. Add([
  3364. 'var',
  3365. ' i: longint;',
  3366. 'begin',
  3367. ' repeat',
  3368. ' break;',
  3369. ' until true;',
  3370. ' while true do',
  3371. ' break;',
  3372. ' for i:=1 to 2 do',
  3373. ' break;']);
  3374. ConvertProgram;
  3375. CheckSource('TestBreak',
  3376. LinesToStr([ // statements
  3377. 'this.i = 0;'
  3378. ]),
  3379. LinesToStr([
  3380. 'do {',
  3381. ' break;',
  3382. '} while (!true);',
  3383. 'while (true) break;',
  3384. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3385. '']));
  3386. end;
  3387. procedure TTestModule.TestBreakAsVar;
  3388. begin
  3389. StartProgram(false);
  3390. Add([
  3391. 'procedure DoIt(break: boolean);',
  3392. 'begin',
  3393. ' if break then ;',
  3394. 'end;',
  3395. 'var',
  3396. ' break: boolean;',
  3397. 'begin',
  3398. ' if break then ;']);
  3399. ConvertProgram;
  3400. CheckSource('TestBreakAsVar',
  3401. LinesToStr([ // statements
  3402. 'this.DoIt = function (Break) {',
  3403. ' if (Break) ;',
  3404. '};',
  3405. 'this.Break = false;',
  3406. '']),
  3407. LinesToStr([
  3408. 'if($mod.Break) ;',
  3409. '']));
  3410. end;
  3411. procedure TTestModule.TestContinue;
  3412. begin
  3413. StartProgram(false);
  3414. Add('var i: longint;');
  3415. Add('begin');
  3416. Add(' repeat');
  3417. Add(' continue;');
  3418. Add(' until true;');
  3419. Add(' while true do');
  3420. Add(' continue;');
  3421. Add(' for i:=1 to 2 do');
  3422. Add(' continue;');
  3423. ConvertProgram;
  3424. CheckSource('TestContinue',
  3425. LinesToStr([ // statements
  3426. 'this.i = 0;'
  3427. ]),
  3428. LinesToStr([
  3429. 'do {',
  3430. ' continue;',
  3431. '} while (!true);',
  3432. 'while (true) continue;',
  3433. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3434. '']));
  3435. end;
  3436. procedure TTestModule.TestProc_External;
  3437. begin
  3438. StartProgram(false);
  3439. Add('procedure Foo; external name ''console.log'';');
  3440. Add('function Bar: longint; external name ''get.item'';');
  3441. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3442. Add('var');
  3443. Add(' i: longint;');
  3444. Add('begin');
  3445. Add(' Foo;');
  3446. Add(' i:=Bar;');
  3447. Add(' i:=Bla(''abc'');');
  3448. ConvertProgram;
  3449. CheckSource('TestProc_External',
  3450. LinesToStr([ // statements
  3451. 'this.i = 0;'
  3452. ]),
  3453. LinesToStr([
  3454. 'console.log();',
  3455. '$mod.i = get.item();',
  3456. '$mod.i = apply.something("abc");'
  3457. ]));
  3458. end;
  3459. procedure TTestModule.TestProc_ExternalOtherUnit;
  3460. begin
  3461. AddModuleWithIntfImplSrc('unit2.pas',
  3462. LinesToStr([
  3463. 'procedure Now; external name ''Date.now'';',
  3464. 'procedure DoIt;'
  3465. ]),
  3466. 'procedure doit; begin end;');
  3467. StartUnit(true);
  3468. Add('interface');
  3469. Add('uses unit2;');
  3470. Add('implementation');
  3471. Add('begin');
  3472. Add(' now;');
  3473. Add(' now();');
  3474. Add(' uNit2.now;');
  3475. Add(' uNit2.now();');
  3476. Add(' doit;');
  3477. Add(' uNit2.doit;');
  3478. ConvertUnit;
  3479. CheckSource('TestProc_ExternalOtherUnit',
  3480. LinesToStr([
  3481. '']),
  3482. LinesToStr([
  3483. 'Date.now();',
  3484. 'Date.now();',
  3485. 'Date.now();',
  3486. 'Date.now();',
  3487. 'pas.unit2.DoIt();',
  3488. 'pas.unit2.DoIt();',
  3489. '']));
  3490. end;
  3491. procedure TTestModule.TestProc_Asm;
  3492. begin
  3493. StartProgram(false);
  3494. Add([
  3495. '{$mode delphi}',
  3496. 'function DoIt: longint;',
  3497. 'begin;',
  3498. ' asm',
  3499. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3500. ' end;',
  3501. ' asm console.log(); end;',
  3502. ' asm',
  3503. ' s = "'' ";',
  3504. ' s = ''" '';',
  3505. ' s = s + "world" + "''";',
  3506. ' // end',
  3507. ' s = ''end'';',
  3508. ' s = "end";',
  3509. ' s = "foo\"bar";',
  3510. ' s = ''a\''b'';',
  3511. ' s = `${expr}\`-"-''-`;',
  3512. ' s = `multi',
  3513. 'line`;',
  3514. ' end;',
  3515. 'end;',
  3516. 'procedure Fly;',
  3517. 'asm',
  3518. ' return;',
  3519. 'end;',
  3520. 'begin']);
  3521. ConvertProgram;
  3522. CheckSource('TestProc_Asm',
  3523. LinesToStr([ // statements
  3524. 'this.DoIt = function () {',
  3525. ' var Result = 0;',
  3526. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3527. ' console.log();',
  3528. ' s = "'' ";',
  3529. ' s = ''" '';',
  3530. ' s = s + "world" + "''";',
  3531. ' // end',
  3532. ' s = ''end'';',
  3533. ' s = "end";',
  3534. ' s = "foo\"bar";',
  3535. ' s = ''a\''b'';',
  3536. ' s = `${expr}\`-"-''-`;',
  3537. ' s = `multi',
  3538. 'line`;',
  3539. ' return Result;',
  3540. '};',
  3541. 'this.Fly = function () {',
  3542. ' return;',
  3543. '};',
  3544. '']),
  3545. LinesToStr([
  3546. ''
  3547. ]));
  3548. end;
  3549. procedure TTestModule.TestProc_Assembler;
  3550. begin
  3551. StartProgram(false);
  3552. Add('function DoIt: longint; assembler;');
  3553. Add('asm');
  3554. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3555. Add('end;');
  3556. Add('begin');
  3557. ConvertProgram;
  3558. CheckSource('TestProc_Assembler',
  3559. LinesToStr([ // statements
  3560. 'this.DoIt = function () {',
  3561. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3562. '};'
  3563. ]),
  3564. LinesToStr([
  3565. ''
  3566. ]));
  3567. end;
  3568. procedure TTestModule.TestProc_VarParam;
  3569. begin
  3570. StartProgram(false);
  3571. Add('type integer = longint;');
  3572. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3573. Add('var vJ: integer;');
  3574. Add('begin');
  3575. Add(' vg:=vg+1;');
  3576. Add(' vj:=vh+2;');
  3577. Add(' vi:=vi+3;');
  3578. Add(' doit(vg,vg,vg);');
  3579. Add(' doit(vh,vh,vj);');
  3580. Add(' doit(vi,vi,vi);');
  3581. Add(' doit(vj,vj,vj);');
  3582. Add('end;');
  3583. Add('var i: integer;');
  3584. Add('begin');
  3585. Add(' doit(i,i,i);');
  3586. ConvertProgram;
  3587. CheckSource('TestProc_VarParam',
  3588. LinesToStr([ // statements
  3589. 'this.DoIt = function (vG,vH,vI) {',
  3590. ' var vJ = 0;',
  3591. ' vG = vG + 1;',
  3592. ' vJ = vH + 2;',
  3593. ' vI.set(vI.get()+3);',
  3594. ' $mod.DoIt(vG, vG, {',
  3595. ' get: function () {',
  3596. ' return vG;',
  3597. ' },',
  3598. ' set: function (v) {',
  3599. ' vG = v;',
  3600. ' }',
  3601. ' });',
  3602. ' $mod.DoIt(vH, vH, {',
  3603. ' get: function () {',
  3604. ' return vJ;',
  3605. ' },',
  3606. ' set: function (v) {',
  3607. ' vJ = v;',
  3608. ' }',
  3609. ' });',
  3610. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3611. ' $mod.DoIt(vJ, vJ, {',
  3612. ' get: function () {',
  3613. ' return vJ;',
  3614. ' },',
  3615. ' set: function (v) {',
  3616. ' vJ = v;',
  3617. ' }',
  3618. ' });',
  3619. '};',
  3620. 'this.i = 0;'
  3621. ]),
  3622. LinesToStr([
  3623. '$mod.DoIt($mod.i,$mod.i,{',
  3624. ' p: $mod,',
  3625. ' get: function () {',
  3626. ' return this.p.i;',
  3627. ' },',
  3628. ' set: function (v) {',
  3629. ' this.p.i = v;',
  3630. ' }',
  3631. '});'
  3632. ]));
  3633. end;
  3634. procedure TTestModule.TestProc_VarParamString;
  3635. begin
  3636. StartProgram(false);
  3637. Add(['type TCaption = string;',
  3638. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3639. 'var c: char;',
  3640. 'begin',
  3641. ' va[1]:=c;',
  3642. ' vb[2]:=c;',
  3643. ' vc[3]:=c;',
  3644. 'end;',
  3645. 'begin']);
  3646. ConvertProgram;
  3647. CheckSource('TestProc_VarParamString',
  3648. LinesToStr([ // statements
  3649. 'this.DoIt = function (vA,vB,vC) {',
  3650. ' var c = "";',
  3651. ' vA = rtl.setCharAt(vA, 0, c);',
  3652. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3653. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3654. '};',
  3655. '']),
  3656. LinesToStr([
  3657. ]));
  3658. end;
  3659. procedure TTestModule.TestProc_VarParamV;
  3660. begin
  3661. StartProgram(false);
  3662. Add([
  3663. 'procedure Inc2(var i: longint);',
  3664. 'begin',
  3665. ' i:=i+2;',
  3666. 'end;',
  3667. 'procedure DoIt(v: longint);',
  3668. 'var p: array of longint;',
  3669. 'begin',
  3670. ' Inc2(v);',
  3671. ' Inc2(p[v]);',
  3672. 'end;',
  3673. 'begin']);
  3674. ConvertProgram;
  3675. CheckSource('TestProc_VarParamV',
  3676. LinesToStr([ // statements
  3677. 'this.Inc2 = function (i) {',
  3678. ' i.set(i.get()+2);',
  3679. '};',
  3680. 'this.DoIt = function (v) {',
  3681. ' var p = [];',
  3682. ' $mod.Inc2({get: function () {',
  3683. ' return v;',
  3684. ' }, set: function (w) {',
  3685. ' v = w;',
  3686. ' }});',
  3687. ' $mod.Inc2({',
  3688. ' a: v,',
  3689. ' p: p,',
  3690. ' get: function () {',
  3691. ' return this.p[this.a];',
  3692. ' },',
  3693. ' set: function (v) {',
  3694. ' this.p[this.a] = v;',
  3695. ' }',
  3696. ' });',
  3697. '};',
  3698. '']),
  3699. LinesToStr([
  3700. '']));
  3701. end;
  3702. procedure TTestModule.TestProc_Overload;
  3703. begin
  3704. StartProgram(false);
  3705. Add('procedure DoIt(vI: longint); begin end;');
  3706. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3707. Add('procedure DoIt(vD: double); begin end;');
  3708. Add('begin');
  3709. Add(' DoIt(1);');
  3710. Add(' DoIt(2,3);');
  3711. Add(' DoIt(4.5);');
  3712. ConvertProgram;
  3713. CheckSource('TestProcedureOverload',
  3714. LinesToStr([ // statements
  3715. 'this.DoIt = function (vI) {',
  3716. '};',
  3717. 'this.DoIt$1 = function (vI, vJ) {',
  3718. '};',
  3719. 'this.DoIt$2 = function (vD) {',
  3720. '};',
  3721. '']),
  3722. LinesToStr([
  3723. '$mod.DoIt(1);',
  3724. '$mod.DoIt$1(2, 3);',
  3725. '$mod.DoIt$2(4.5);',
  3726. '']));
  3727. end;
  3728. procedure TTestModule.TestProc_OverloadForward;
  3729. begin
  3730. StartProgram(false);
  3731. Add('procedure DoIt(vI: longint); forward;');
  3732. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3733. Add('procedure doit(vi: longint); begin end;');
  3734. Add('begin');
  3735. Add(' doit(1);');
  3736. Add(' doit(2,3);');
  3737. ConvertProgram;
  3738. CheckSource('TestProcedureOverloadForward',
  3739. LinesToStr([ // statements
  3740. 'this.DoIt$1 = function (vI, vJ) {',
  3741. '};',
  3742. 'this.DoIt = function (vI) {',
  3743. '};',
  3744. '']),
  3745. LinesToStr([
  3746. '$mod.DoIt(1);',
  3747. '$mod.DoIt$1(2, 3);',
  3748. '']));
  3749. end;
  3750. procedure TTestModule.TestProc_OverloadIntfImpl;
  3751. begin
  3752. StartUnit(false);
  3753. Add('interface');
  3754. Add('procedure DoIt(vI: longint);');
  3755. Add('procedure DoIt(vI, vJ: longint);');
  3756. Add('implementation');
  3757. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  3758. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  3759. Add('procedure DoIt(vi: longint); begin end;');
  3760. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  3761. Add('procedure DoIt(vi, vj: longint); begin end;');
  3762. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  3763. Add('begin');
  3764. Add(' doit(1);');
  3765. Add(' doit(2,3);');
  3766. Add(' doit(4,5,6);');
  3767. Add(' doit(7,8,9,10);');
  3768. Add(' doit(11,12,13,14,15);');
  3769. ConvertUnit;
  3770. CheckSource('TestProcedureOverloadUnit',
  3771. LinesToStr([ // statements
  3772. 'var $impl = $mod.$impl;',
  3773. 'this.DoIt = function (vI) {',
  3774. '};',
  3775. 'this.DoIt$1 = function (vI, vJ) {',
  3776. '};',
  3777. '']),
  3778. LinesToStr([ // this.$init
  3779. '$mod.DoIt(1);',
  3780. '$mod.DoIt$1(2, 3);',
  3781. '$impl.DoIt$3(4,5,6);',
  3782. '$impl.DoIt$4(7,8,9,10);',
  3783. '$impl.DoIt$2(11,12,13,14,15);',
  3784. '']),
  3785. LinesToStr([ // implementation
  3786. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  3787. '};',
  3788. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  3789. '};',
  3790. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  3791. '};',
  3792. '']));
  3793. end;
  3794. procedure TTestModule.TestProc_OverloadNested;
  3795. begin
  3796. StartProgram(false);
  3797. Add([
  3798. 'procedure DoIt(vA: longint); overload; forward;',
  3799. 'procedure DoIt(vB, vC: longint); overload;',
  3800. 'begin // 2 param overload',
  3801. ' doit(1);',
  3802. ' doit(1,2);',
  3803. 'end;',
  3804. 'procedure doit(vA: longint);',
  3805. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  3806. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  3807. ' begin // 4 param overload',
  3808. ' doit(1);',
  3809. ' doit(1,2);',
  3810. ' doit(1,2,3);',
  3811. ' doit(1,2,3,4);',
  3812. ' end;',
  3813. ' procedure doit(vA, vB, vC: longint);',
  3814. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  3815. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  3816. ' begin // 6 param overload',
  3817. ' doit(1);',
  3818. ' doit(1,2);',
  3819. ' doit(1,2,3);',
  3820. ' doit(1,2,3,4);',
  3821. ' doit(1,2,3,4,5);',
  3822. ' doit(1,2,3,4,5,6);',
  3823. ' end;',
  3824. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  3825. ' begin // 5 param overload',
  3826. ' doit(1);',
  3827. ' doit(1,2);',
  3828. ' doit(1,2,3);',
  3829. ' doit(1,2,3,4);',
  3830. ' doit(1,2,3,4,5);',
  3831. ' doit(1,2,3,4,5,6);',
  3832. ' end;',
  3833. ' begin // 3 param overload',
  3834. ' doit(1);',
  3835. ' doit(1,2);',
  3836. ' doit(1,2,3);',
  3837. ' doit(1,2,3,4);',
  3838. ' doit(1,2,3,4,5);',
  3839. ' doit(1,2,3,4,5,6);',
  3840. ' end;',
  3841. 'begin // 1 param overload',
  3842. ' doit(1);',
  3843. ' doit(1,2);',
  3844. ' doit(1,2,3);',
  3845. ' doit(1,2,3,4);',
  3846. 'end;',
  3847. 'begin // main',
  3848. ' doit(1);',
  3849. ' doit(1,2);']);
  3850. ConvertProgram;
  3851. CheckSource('TestProcedureOverloadNested',
  3852. LinesToStr([ // statements
  3853. 'this.DoIt$1 = function (vB, vC) {',
  3854. ' $mod.DoIt(1);',
  3855. ' $mod.DoIt$1(1, 2);',
  3856. '};',
  3857. 'this.DoIt = function (vA) {',
  3858. ' function DoIt$3(vA, vB, vC, vD) {',
  3859. ' $mod.DoIt(1);',
  3860. ' $mod.DoIt$1(1, 2);',
  3861. ' DoIt$2(1, 2, 3);',
  3862. ' DoIt$3(1, 2, 3, 4);',
  3863. ' };',
  3864. ' function DoIt$2(vA, vB, vC) {',
  3865. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  3866. ' $mod.DoIt(1);',
  3867. ' $mod.DoIt$1(1, 2);',
  3868. ' DoIt$2(1, 2, 3);',
  3869. ' DoIt$3(1, 2, 3, 4);',
  3870. ' DoIt$4(1, 2, 3, 4, 5);',
  3871. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3872. ' };',
  3873. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  3874. ' $mod.DoIt(1);',
  3875. ' $mod.DoIt$1(1, 2);',
  3876. ' DoIt$2(1, 2, 3);',
  3877. ' DoIt$3(1, 2, 3, 4);',
  3878. ' DoIt$4(1, 2, 3, 4, 5);',
  3879. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3880. ' };',
  3881. ' $mod.DoIt(1);',
  3882. ' $mod.DoIt$1(1, 2);',
  3883. ' DoIt$2(1, 2, 3);',
  3884. ' DoIt$3(1, 2, 3, 4);',
  3885. ' DoIt$4(1, 2, 3, 4, 5);',
  3886. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3887. ' };',
  3888. ' $mod.DoIt(1);',
  3889. ' $mod.DoIt$1(1, 2);',
  3890. ' DoIt$2(1, 2, 3);',
  3891. ' DoIt$3(1, 2, 3, 4);',
  3892. '};',
  3893. '']),
  3894. LinesToStr([
  3895. '$mod.DoIt(1);',
  3896. '$mod.DoIt$1(1, 2);',
  3897. '']));
  3898. end;
  3899. procedure TTestModule.TestProc_OverloadUnitCycle;
  3900. begin
  3901. AddModuleWithIntfImplSrc('Unit2.pas',
  3902. LinesToStr([
  3903. 'type',
  3904. ' TObject = class',
  3905. ' procedure DoIt(b: boolean); virtual; abstract;',
  3906. ' procedure DoIt(i: longint); virtual; abstract;',
  3907. ' end;',
  3908. '']),
  3909. 'uses test1;');
  3910. StartUnit(true);
  3911. Add([
  3912. 'interface',
  3913. 'uses unit2;',
  3914. 'type',
  3915. ' TEagle = class(TObject)',
  3916. ' procedure DoIt(b: boolean); override;',
  3917. ' procedure DoIt(i: longint); override;',
  3918. ' end;',
  3919. 'implementation',
  3920. 'procedure TEagle.DoIt(b: boolean); begin end;',
  3921. 'procedure TEagle.DoIt(i: longint); begin end;',
  3922. '']);
  3923. ConvertUnit;
  3924. CheckSource('TestProc_OverloadUnitCycle',
  3925. LinesToStr([ // statements
  3926. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  3927. ' this.DoIt = function (b) {',
  3928. ' };',
  3929. ' this.DoIt$1 = function (i) {',
  3930. ' };',
  3931. '});',
  3932. '']),
  3933. '',
  3934. LinesToStr([
  3935. '']));
  3936. end;
  3937. procedure TTestModule.TestProc_Varargs;
  3938. begin
  3939. StartProgram(false);
  3940. Add([
  3941. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  3942. 'procedure ProcB; varargs; external name ''ProcB'';',
  3943. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  3944. 'function GetIt: longint; begin end;',
  3945. 'begin',
  3946. ' ProcA(1);',
  3947. ' ProcA(1,2);',
  3948. ' ProcA(1,2.0);',
  3949. ' ProcA(1,2,3);',
  3950. ' ProcA(1,''2'');',
  3951. ' ProcA(2,'''');',
  3952. ' ProcA(3,false);',
  3953. ' ProcB;',
  3954. ' ProcB();',
  3955. ' ProcB(4);',
  3956. ' ProcB(''foo'');',
  3957. ' ProcC;',
  3958. ' ProcC();',
  3959. ' ProcC(4);',
  3960. ' ProcC(5,''foo'');',
  3961. ' ProcB(GetIt);',
  3962. ' ProcB(GetIt());',
  3963. ' ProcB(GetIt,GetIt());']);
  3964. ConvertProgram;
  3965. CheckSource('TestProc_Varargs',
  3966. LinesToStr([ // statements
  3967. 'this.GetIt = function () {',
  3968. ' var Result = 0;',
  3969. ' return Result;',
  3970. '};',
  3971. '']),
  3972. LinesToStr([
  3973. 'ProcA(1);',
  3974. 'ProcA(1, 2);',
  3975. 'ProcA(1, 2.0);',
  3976. 'ProcA(1, 2, 3);',
  3977. 'ProcA(1, "2");',
  3978. 'ProcA(2, "");',
  3979. 'ProcA(3, false);',
  3980. 'ProcB();',
  3981. 'ProcB();',
  3982. 'ProcB(4);',
  3983. 'ProcB("foo");',
  3984. 'ProcC(17);',
  3985. 'ProcC(17);',
  3986. 'ProcC(4);',
  3987. 'ProcC(5, "foo");',
  3988. 'ProcB($mod.GetIt());',
  3989. 'ProcB($mod.GetIt());',
  3990. 'ProcB($mod.GetIt(), $mod.GetIt());',
  3991. '']));
  3992. end;
  3993. procedure TTestModule.TestProc_ConstOrder;
  3994. begin
  3995. StartProgram(false);
  3996. Add([
  3997. 'const A = 3;',
  3998. 'const B = A+1;',
  3999. 'procedure DoIt;',
  4000. 'const C = A+1;',
  4001. 'const D = B+1;',
  4002. 'const E = D+C+B+A;',
  4003. 'begin',
  4004. 'end;',
  4005. 'begin'
  4006. ]);
  4007. ConvertProgram;
  4008. CheckSource('TestProc_ConstOrder',
  4009. LinesToStr([ // statements
  4010. 'this.A = 3;',
  4011. 'this.B = 3 + 1;',
  4012. 'var C = 3 + 1;',
  4013. 'var D = 4 + 1;',
  4014. 'var E = 5 + 4 + 4 + 3;',
  4015. 'this.DoIt = function () {',
  4016. '};',
  4017. '']),
  4018. LinesToStr([
  4019. ''
  4020. ]));
  4021. end;
  4022. procedure TTestModule.TestProc_DuplicateConst;
  4023. begin
  4024. StartProgram(false);
  4025. Add([
  4026. 'const A = 1;',
  4027. 'procedure DoIt;',
  4028. 'const A = 2;',
  4029. ' procedure SubIt;',
  4030. ' const A = 21;',
  4031. ' begin',
  4032. ' end;',
  4033. 'begin',
  4034. 'end;',
  4035. 'procedure DoSome;',
  4036. 'const A = 3;',
  4037. 'begin',
  4038. 'end;',
  4039. 'begin'
  4040. ]);
  4041. ConvertProgram;
  4042. CheckSource('TestProc_DuplicateConst',
  4043. LinesToStr([ // statements
  4044. 'this.A = 1;',
  4045. 'var A$1 = 2;',
  4046. 'var A$2 = 21;',
  4047. 'this.DoIt = function () {',
  4048. ' function SubIt() {',
  4049. ' };',
  4050. '};',
  4051. 'var A$3 = 3;',
  4052. 'this.DoSome = function () {',
  4053. '};',
  4054. '']),
  4055. LinesToStr([
  4056. ''
  4057. ]));
  4058. end;
  4059. procedure TTestModule.TestProc_LocalVarAbsolute;
  4060. begin
  4061. StartProgram(false);
  4062. Add([
  4063. 'type',
  4064. ' TObject = class',
  4065. ' Index: longint;',
  4066. ' procedure DoAbs(Item: pointer);',
  4067. ' end;',
  4068. 'procedure TObject.DoAbs(Item: pointer);',
  4069. 'var',
  4070. ' o: TObject absolute Item;',
  4071. 'begin',
  4072. ' if o.Index<o.Index then o.Index:=o.Index;',
  4073. 'end;',
  4074. 'procedure DoIt(i: longint; p: pointer);',
  4075. 'var',
  4076. ' d: double absolute i;',
  4077. ' s: string absolute d;',
  4078. ' oi: TObject absolute i;',
  4079. ' op: TObject absolute p;',
  4080. 'begin',
  4081. ' if d=d then d:=d;',
  4082. ' if s=s then s:=s;',
  4083. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4084. ' if op.Index=op.Index then op.Index:=op.Index;',
  4085. 'end;',
  4086. 'begin']);
  4087. ConvertProgram;
  4088. CheckSource('TestProc_LocalVarAbsolute',
  4089. LinesToStr([ // statements
  4090. 'rtl.createClass($mod, "TObject", null, function () {',
  4091. ' this.$init = function () {',
  4092. ' this.Index = 0;',
  4093. ' };',
  4094. ' this.$final = function () {',
  4095. ' };',
  4096. ' this.DoAbs = function (Item) {',
  4097. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4098. ' };',
  4099. '});',
  4100. 'this.DoIt = function (i, p) {',
  4101. ' if (i === i) i = i;',
  4102. ' if (i === i) i = i;',
  4103. ' if (i.Index < i.Index) i.Index = i.Index;',
  4104. ' if (p.Index === p.Index) p.Index = p.Index;',
  4105. '};'
  4106. ]),
  4107. LinesToStr([
  4108. ]));
  4109. end;
  4110. procedure TTestModule.TestProc_LocalVarInit;
  4111. begin
  4112. StartProgram(false);
  4113. Add([
  4114. 'type TBytes = array of byte;',
  4115. 'procedure DoIt;',
  4116. 'const c = 4;',
  4117. 'var',
  4118. ' b: byte = 1;',
  4119. ' w: word = 2+c;',
  4120. ' p: pointer = nil;',
  4121. ' Buffer: TBytes = nil;',
  4122. 'begin',
  4123. 'end;',
  4124. 'begin']);
  4125. ConvertProgram;
  4126. CheckSource('TestProc_LocalVarInit',
  4127. LinesToStr([ // statements
  4128. 'var c = 4;',
  4129. 'this.DoIt = function () {',
  4130. ' var b = 1;',
  4131. ' var w = 2 + 4;',
  4132. ' var p = null;',
  4133. ' var Buffer = [];',
  4134. '};',
  4135. '']),
  4136. LinesToStr([
  4137. ]));
  4138. end;
  4139. procedure TTestModule.TestProc_ReservedWords;
  4140. begin
  4141. StartProgram(false);
  4142. Add([
  4143. 'procedure Date(ArrayBuffer: longint);',
  4144. 'const',
  4145. ' NaN: longint = 3;',
  4146. 'var',
  4147. ' &Boolean: longint;',
  4148. ' procedure Error(ArrayBuffer: longint);',
  4149. ' begin',
  4150. ' end;',
  4151. 'begin',
  4152. ' Nan:=&bOolean;',
  4153. 'end;',
  4154. 'begin',
  4155. ' Date(1);']);
  4156. ConvertProgram;
  4157. CheckSource('TestProc_ReservedWords',
  4158. LinesToStr([ // statements
  4159. 'var naN = 3;',
  4160. 'this.Date = function (arrayBuffer) {',
  4161. ' var boolean = 0;',
  4162. ' function error(arrayBuffer) {',
  4163. ' };',
  4164. ' naN = boolean;',
  4165. '};',
  4166. '']),
  4167. LinesToStr([
  4168. ' $mod.Date(1);'
  4169. ]));
  4170. end;
  4171. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4172. begin
  4173. StartProgram(false);
  4174. Add([
  4175. '{$mode objfpc}',
  4176. 'type',
  4177. ' TFunc = reference to function(x: word): word;',
  4178. 'var Func: TFunc;',
  4179. 'procedure DoIt(a: word);',
  4180. 'begin',
  4181. ' Func:=function(b:word): word',
  4182. ' begin',
  4183. ' Result:=a+b;',
  4184. ' exit(b);',
  4185. ' exit(Result);',
  4186. ' end;',// test semicolon
  4187. ' a:=3;',
  4188. 'end;',
  4189. 'begin',
  4190. ' Func:=function(c:word):word begin',
  4191. ' Result:=3+c;',
  4192. ' exit(c);',
  4193. ' exit(Result);',
  4194. ' end;']);
  4195. ConvertProgram;
  4196. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4197. LinesToStr([ // statements
  4198. 'this.Func = null;',
  4199. 'this.DoIt = function (a) {',
  4200. ' $mod.Func = function (b) {',
  4201. ' var Result = 0;',
  4202. ' Result = a + b;',
  4203. ' return b;',
  4204. ' return Result;',
  4205. ' return Result;',
  4206. ' };',
  4207. ' a = 3;',
  4208. '};',
  4209. '']),
  4210. LinesToStr([
  4211. '$mod.Func = function (c) {',
  4212. ' var Result = 0;',
  4213. ' Result = 3 + c;',
  4214. ' return c;',
  4215. ' return Result;',
  4216. ' return Result;',
  4217. '};',
  4218. '']));
  4219. end;
  4220. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4221. begin
  4222. StartProgram(false);
  4223. Add([
  4224. '{$mode delphi}',
  4225. 'type',
  4226. ' TProc = reference to procedure(x: word);',
  4227. 'procedure DoIt(a: word);',
  4228. 'var Proc: TProc;',
  4229. 'begin',
  4230. ' Proc:=procedure(b:word) begin end;',
  4231. 'end;',
  4232. 'var Proc: TProc;',
  4233. 'begin',
  4234. ' Proc:=procedure(c:word) begin end;',
  4235. '']);
  4236. ConvertProgram;
  4237. CheckSource('TestAnonymousProc_Assign_Delphi',
  4238. LinesToStr([ // statements
  4239. 'this.DoIt = function (a) {',
  4240. ' var Proc = null;',
  4241. ' Proc = function (b) {',
  4242. ' };',
  4243. '};',
  4244. 'this.Proc = null;',
  4245. '']),
  4246. LinesToStr([
  4247. '$mod.Proc = function (c) {',
  4248. '};',
  4249. '']));
  4250. end;
  4251. procedure TTestModule.TestAnonymousProc_Arg;
  4252. begin
  4253. StartProgram(false);
  4254. Add([
  4255. 'type',
  4256. ' TProc = reference to procedure;',
  4257. ' TFunc = reference to function(x: word): word;',
  4258. 'procedure DoMore(f,g: TProc);',
  4259. 'begin',
  4260. 'end;',
  4261. 'procedure DoOdd(v: jsvalue);',
  4262. 'begin',
  4263. 'end;',
  4264. 'procedure DoIt(f: TFunc);',
  4265. 'begin',
  4266. ' DoIt(function(b:word): word',
  4267. ' begin',
  4268. ' Result:=1+b;',
  4269. ' end);',
  4270. ' DoMore(procedure begin end, procedure begin end);',
  4271. ' DoOdd(procedure begin end);',
  4272. 'end;',
  4273. 'begin',
  4274. ' DoMore(procedure begin end,',
  4275. ' procedure assembler asm',
  4276. ' console.log("c");',
  4277. ' end);',
  4278. '']);
  4279. ConvertProgram;
  4280. CheckSource('TestAnonymousProc_Arg',
  4281. LinesToStr([ // statements
  4282. 'this.DoMore = function (f, g) {',
  4283. '};',
  4284. 'this.DoOdd = function (v) {',
  4285. '};',
  4286. 'this.DoIt = function (f) {',
  4287. ' $mod.DoIt(function (b) {',
  4288. ' var Result = 0;',
  4289. ' Result = 1 + b;',
  4290. ' return Result;',
  4291. ' });',
  4292. ' $mod.DoMore(function () {',
  4293. ' }, function () {',
  4294. ' });',
  4295. ' $mod.DoOdd(function () {',
  4296. ' });',
  4297. '};',
  4298. '']),
  4299. LinesToStr([
  4300. '$mod.DoMore(function () {',
  4301. '}, function () {',
  4302. ' console.log("c");',
  4303. '});',
  4304. '']));
  4305. end;
  4306. procedure TTestModule.TestAnonymousProc_Typecast;
  4307. begin
  4308. StartProgram(false);
  4309. Add([
  4310. 'type',
  4311. ' TProc = reference to procedure(w: word);',
  4312. ' TArr = array of word;',
  4313. ' TFuncArr = reference to function: TArr;',
  4314. 'procedure DoIt(p: TProc);',
  4315. 'var',
  4316. ' w: word;',
  4317. ' a: TArr;',
  4318. 'begin',
  4319. ' p:=TProc(procedure(b: smallint) begin end);',
  4320. ' a:=TFuncArr(function: TArr begin end)();',
  4321. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4322. 'end;',
  4323. 'begin']);
  4324. ConvertProgram;
  4325. CheckSource('TestAnonymousProc_Typecast',
  4326. LinesToStr([ // statements
  4327. 'this.DoIt = function (p) {',
  4328. ' var w = 0;',
  4329. ' var a = [];',
  4330. ' p = function (b) {',
  4331. ' };',
  4332. ' a = function () {',
  4333. ' var Result = [];',
  4334. ' return Result;',
  4335. ' }();',
  4336. ' w = function () {',
  4337. ' var Result = [];',
  4338. ' return Result;',
  4339. ' }()[3];',
  4340. '};',
  4341. '']),
  4342. LinesToStr([
  4343. '']));
  4344. end;
  4345. procedure TTestModule.TestAnonymousProc_With;
  4346. begin
  4347. StartProgram(false);
  4348. Add([
  4349. 'type',
  4350. ' TProc = reference to procedure(w: word);',
  4351. ' TObject = class',
  4352. ' b: boolean;',
  4353. ' end;',
  4354. 'var',
  4355. ' p: TProc;',
  4356. ' bird: TObject;',
  4357. 'begin',
  4358. ' with bird do',
  4359. ' p:=procedure(w: word)',
  4360. ' begin',
  4361. ' b:=w>2;',
  4362. ' end;',
  4363. '']);
  4364. ConvertProgram;
  4365. CheckSource('TestAnonymousProc_With',
  4366. LinesToStr([ // statements
  4367. 'rtl.createClass($mod, "TObject", null, function () {',
  4368. ' this.$init = function () {',
  4369. ' this.b = false;',
  4370. ' };',
  4371. ' this.$final = function () {',
  4372. ' };',
  4373. '});',
  4374. 'this.p = null;',
  4375. 'this.bird = null;',
  4376. '']),
  4377. LinesToStr([
  4378. 'var $with1 = $mod.bird;',
  4379. '$mod.p = function (w) {',
  4380. ' $with1.b = w > 2;',
  4381. '};',
  4382. '']));
  4383. end;
  4384. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4385. begin
  4386. StartProgram(false);
  4387. Add([
  4388. 'type',
  4389. ' TProc = reference to procedure;',
  4390. ' TObject = class',
  4391. ' b: boolean;',
  4392. ' end;',
  4393. 'procedure DoIt;',
  4394. 'var',
  4395. ' p: TProc;',
  4396. 'begin',
  4397. ' try',
  4398. ' except',
  4399. ' on E: TObject do',
  4400. ' p:=procedure',
  4401. ' begin',
  4402. ' E.b:=true;',
  4403. ' end;',
  4404. ' end;',
  4405. 'end;',
  4406. 'begin']);
  4407. ConvertProgram;
  4408. CheckSource('TestAnonymousProc_ExceptOn',
  4409. LinesToStr([ // statements
  4410. 'rtl.createClass($mod, "TObject", null, function () {',
  4411. ' this.$init = function () {',
  4412. ' this.b = false;',
  4413. ' };',
  4414. ' this.$final = function () {',
  4415. ' };',
  4416. '});',
  4417. 'this.DoIt = function () {',
  4418. ' var p = null;',
  4419. ' try {} catch ($e) {',
  4420. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4421. ' var E = $e;',
  4422. ' p = function () {',
  4423. ' E.b = true;',
  4424. ' };',
  4425. ' } else throw $e',
  4426. ' };',
  4427. '};',
  4428. '']),
  4429. LinesToStr([
  4430. '']));
  4431. end;
  4432. procedure TTestModule.TestAnonymousProc_Nested;
  4433. begin
  4434. StartProgram(false);
  4435. Add([
  4436. 'type',
  4437. ' TProc = reference to procedure;',
  4438. ' TObject = class',
  4439. ' i: byte;',
  4440. ' procedure DoIt;',
  4441. ' end;',
  4442. 'procedure TObject.DoIt;',
  4443. 'var',
  4444. ' p: TProc;',
  4445. ' procedure Sub;',
  4446. ' begin',
  4447. ' p:=procedure',
  4448. ' begin',
  4449. ' i:=3;',
  4450. ' Self.i:=4;',
  4451. ' p:=procedure',
  4452. ' procedure SubSub;',
  4453. ' begin',
  4454. ' i:=13;',
  4455. ' Self.i:=14;',
  4456. ' end;',
  4457. ' begin',
  4458. ' i:=13;',
  4459. ' Self.i:=14;',
  4460. ' end;',
  4461. ' end;',
  4462. ' end;',
  4463. 'begin',
  4464. 'end;',
  4465. 'begin']);
  4466. ConvertProgram;
  4467. CheckSource('TestAnonymousProc_Nested',
  4468. LinesToStr([ // statements
  4469. 'rtl.createClass($mod, "TObject", null, function () {',
  4470. ' this.$init = function () {',
  4471. ' this.i = 0;',
  4472. ' };',
  4473. ' this.$final = function () {',
  4474. ' };',
  4475. ' this.DoIt = function () {',
  4476. ' var $Self = this;',
  4477. ' var p = null;',
  4478. ' function Sub() {',
  4479. ' p = function () {',
  4480. ' $Self.i = 3;',
  4481. ' $Self.i = 4;',
  4482. ' p = function () {',
  4483. ' function SubSub() {',
  4484. ' $Self.i = 13;',
  4485. ' $Self.i = 14;',
  4486. ' };',
  4487. ' $Self.i = 13;',
  4488. ' $Self.i = 14;',
  4489. ' };',
  4490. ' };',
  4491. ' };',
  4492. ' };',
  4493. '});',
  4494. '']),
  4495. LinesToStr([
  4496. '']));
  4497. end;
  4498. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4499. begin
  4500. StartProgram(false);
  4501. Add([
  4502. 'type',
  4503. ' TProc = reference to procedure;',
  4504. 'function DoIt: TProc;',
  4505. ' function Sub: TProc;',
  4506. ' begin',
  4507. ' Result:=procedure',
  4508. ' begin',
  4509. ' Sub:=procedure',
  4510. ' procedure SubSub;',
  4511. ' begin',
  4512. ' Result:=nil;',
  4513. ' Sub:=nil;',
  4514. ' DoIt:=nil;',
  4515. ' end;',
  4516. ' begin',
  4517. ' Result:=nil;',
  4518. ' Sub:=nil;',
  4519. ' DoIt:=nil;',
  4520. ' end;',
  4521. ' end;',
  4522. ' end;',
  4523. 'begin',
  4524. 'end;',
  4525. 'begin']);
  4526. ConvertProgram;
  4527. CheckSource('TestAnonymousProc_NestedAssignResult',
  4528. LinesToStr([ // statements
  4529. 'this.DoIt = function () {',
  4530. ' var Result = null;',
  4531. ' function Sub() {',
  4532. ' var Result$1 = null;',
  4533. ' Result$1 = function () {',
  4534. ' Result$1 = function () {',
  4535. ' function SubSub() {',
  4536. ' Result$1 = null;',
  4537. ' Result$1 = null;',
  4538. ' Result = null;',
  4539. ' };',
  4540. ' Result$1 = null;',
  4541. ' Result$1 = null;',
  4542. ' Result = null;',
  4543. ' };',
  4544. ' };',
  4545. ' return Result$1;',
  4546. ' };',
  4547. ' return Result;',
  4548. '};',
  4549. '']),
  4550. LinesToStr([
  4551. '']));
  4552. end;
  4553. procedure TTestModule.TestAnonymousProc_Class;
  4554. begin
  4555. StartProgram(false);
  4556. Add([
  4557. 'type',
  4558. ' TProc = reference to procedure;',
  4559. ' TObject = class',
  4560. ' Size: word;',
  4561. ' function GetIt: TProc;',
  4562. ' end;',
  4563. 'function TObject.GetIt: TProc;',
  4564. 'begin',
  4565. ' Result:=procedure',
  4566. ' begin',
  4567. ' Size:=Size;',
  4568. ' end;',
  4569. 'end;',
  4570. 'begin']);
  4571. ConvertProgram;
  4572. CheckSource('TestAnonymousProc_Class',
  4573. LinesToStr([ // statements
  4574. 'rtl.createClass($mod, "TObject", null, function () {',
  4575. ' this.$init = function () {',
  4576. ' this.Size = 0;',
  4577. ' };',
  4578. ' this.$final = function () {',
  4579. ' };',
  4580. ' this.GetIt = function () {',
  4581. ' var $Self = this;',
  4582. ' var Result = null;',
  4583. ' Result = function () {',
  4584. ' $Self.Size = $Self.Size;',
  4585. ' };',
  4586. ' return Result;',
  4587. ' };',
  4588. '});',
  4589. '']),
  4590. LinesToStr([
  4591. '']));
  4592. end;
  4593. procedure TTestModule.TestAnonymousProc_ForLoop;
  4594. begin
  4595. StartProgram(false);
  4596. Add([
  4597. 'type TProc = reference to procedure;',
  4598. 'procedure Foo(p: TProc);',
  4599. 'begin',
  4600. 'end;',
  4601. 'procedure DoIt;',
  4602. 'var i: word;',
  4603. ' a: word;',
  4604. 'begin',
  4605. ' for i:=1 to 10 do begin',
  4606. ' Foo(procedure begin a:=3; end);',
  4607. ' end;',
  4608. 'end;',
  4609. 'begin',
  4610. ' DoIt;']);
  4611. ConvertProgram;
  4612. CheckSource('TestAnonymousProc_ForLoop',
  4613. LinesToStr([ // statements
  4614. 'this.Foo = function (p) {',
  4615. '};',
  4616. 'this.DoIt = function () {',
  4617. ' var i = 0;',
  4618. ' var a = 0;',
  4619. ' for (i = 1; i <= 10; i++) {',
  4620. ' $mod.Foo(function () {',
  4621. ' a = 3;',
  4622. ' });',
  4623. ' };',
  4624. '};',
  4625. '']),
  4626. LinesToStr([
  4627. '$mod.DoIt();'
  4628. ]));
  4629. end;
  4630. procedure TTestModule.TestEnum_Name;
  4631. begin
  4632. StartProgram(false);
  4633. Add('type TMyEnum = (Red, Green, Blue);');
  4634. Add('var e: TMyEnum;');
  4635. Add('var f: TMyEnum = Blue;');
  4636. Add('begin');
  4637. Add(' e:=green;');
  4638. Add(' e:=default(TMyEnum);');
  4639. ConvertProgram;
  4640. CheckSource('TestEnumName',
  4641. LinesToStr([ // statements
  4642. 'this.TMyEnum = {',
  4643. ' "0":"Red",',
  4644. ' Red:0,',
  4645. ' "1":"Green",',
  4646. ' Green:1,',
  4647. ' "2":"Blue",',
  4648. ' Blue:2',
  4649. ' };',
  4650. 'this.e = 0;',
  4651. 'this.f = $mod.TMyEnum.Blue;'
  4652. ]),
  4653. LinesToStr([
  4654. '$mod.e=$mod.TMyEnum.Green;',
  4655. '$mod.e=$mod.TMyEnum.Red;'
  4656. ]));
  4657. end;
  4658. procedure TTestModule.TestEnum_Number;
  4659. begin
  4660. Converter.Options:=Converter.Options+[coEnumNumbers];
  4661. StartProgram(false);
  4662. Add('type TMyEnum = (Red, Green);');
  4663. Add('var');
  4664. Add(' e: TMyEnum;');
  4665. Add(' f: TMyEnum = Green;');
  4666. Add(' i: longint;');
  4667. Add('begin');
  4668. Add(' e:=green;');
  4669. Add(' i:=longint(e);');
  4670. ConvertProgram;
  4671. CheckSource('TestEnumNumber',
  4672. LinesToStr([ // statements
  4673. 'this.TMyEnum = {',
  4674. ' "0":"Red",',
  4675. ' Red:0,',
  4676. ' "1":"Green",',
  4677. ' Green:1',
  4678. ' };',
  4679. 'this.e = 0;',
  4680. 'this.f = 1;',
  4681. 'this.i = 0;'
  4682. ]),
  4683. LinesToStr([
  4684. '$mod.e=1;',
  4685. '$mod.i=$mod.e;'
  4686. ]));
  4687. end;
  4688. procedure TTestModule.TestEnum_ConstFail;
  4689. begin
  4690. StartProgram(false);
  4691. Add([
  4692. 'type TMyEnum = (Red = 100, Green = 101);',
  4693. 'var',
  4694. ' e: TMyEnum;',
  4695. ' f: TMyEnum = Green;',
  4696. 'begin',
  4697. ' e:=green;']);
  4698. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] enum const',3002);
  4699. ConvertProgram;
  4700. end;
  4701. procedure TTestModule.TestEnum_Functions;
  4702. begin
  4703. StartProgram(false);
  4704. Add([
  4705. 'type TMyEnum = (Red, Green);',
  4706. 'procedure DoIt(var e: TMyEnum; var i: word);',
  4707. 'var',
  4708. ' v: longint;',
  4709. ' s: string;',
  4710. 'begin',
  4711. ' val(s,e,v);',
  4712. ' val(s,e,i);',
  4713. 'end;',
  4714. 'var',
  4715. ' e: TMyEnum;',
  4716. ' i: longint;',
  4717. ' s: string;',
  4718. ' b: boolean;',
  4719. 'begin',
  4720. ' i:=ord(red);',
  4721. ' i:=ord(green);',
  4722. ' i:=ord(e);',
  4723. ' i:=ord(b);',
  4724. ' e:=low(tmyenum);',
  4725. ' e:=low(e);',
  4726. ' b:=low(boolean);',
  4727. ' e:=high(tmyenum);',
  4728. ' e:=high(e);',
  4729. ' b:=high(boolean);',
  4730. ' e:=pred(green);',
  4731. ' e:=pred(e);',
  4732. ' b:=pred(b);',
  4733. ' e:=succ(red);',
  4734. ' e:=succ(e);',
  4735. ' b:=succ(b);',
  4736. ' e:=tmyenum(1);',
  4737. ' e:=tmyenum(i);',
  4738. ' s:=str(e);',
  4739. ' str(e,s);',
  4740. ' str(red,s);',
  4741. ' s:=str(e:3);',
  4742. ' writestr(s,e:3,red);',
  4743. ' val(s,e,i);',
  4744. ' e:=TMyEnum(i);',
  4745. ' i:=longint(e);']);
  4746. ConvertProgram;
  4747. CheckSource('TestEnum_Functions',
  4748. LinesToStr([ // statements
  4749. 'this.TMyEnum = {',
  4750. ' "0":"Red",',
  4751. ' Red:0,',
  4752. ' "1":"Green",',
  4753. ' Green:1',
  4754. ' };',
  4755. 'this.DoIt = function (e, i) {',
  4756. ' var v = 0;',
  4757. ' var s = "";',
  4758. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  4759. ' v = w;',
  4760. ' }));',
  4761. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  4762. '};',
  4763. 'this.e = 0;',
  4764. 'this.i = 0;',
  4765. 'this.s = "";',
  4766. 'this.b = false;',
  4767. '']),
  4768. LinesToStr([
  4769. '$mod.i=$mod.TMyEnum.Red;',
  4770. '$mod.i=$mod.TMyEnum.Green;',
  4771. '$mod.i=$mod.e;',
  4772. '$mod.i=$mod.b+0;',
  4773. '$mod.e=$mod.TMyEnum.Red;',
  4774. '$mod.e=$mod.TMyEnum.Red;',
  4775. '$mod.b=false;',
  4776. '$mod.e=$mod.TMyEnum.Green;',
  4777. '$mod.e=$mod.TMyEnum.Green;',
  4778. '$mod.b=true;',
  4779. '$mod.e=$mod.TMyEnum.Green-1;',
  4780. '$mod.e=$mod.e-1;',
  4781. '$mod.b=false;',
  4782. '$mod.e=$mod.TMyEnum.Red+1;',
  4783. '$mod.e=$mod.e+1;',
  4784. '$mod.b=true;',
  4785. '$mod.e=1;',
  4786. '$mod.e=$mod.i;',
  4787. '$mod.s = $mod.TMyEnum[$mod.e];',
  4788. '$mod.s = $mod.TMyEnum[$mod.e];',
  4789. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  4790. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  4791. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  4792. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  4793. ' $mod.i = v;',
  4794. '});',
  4795. '$mod.e=$mod.i;',
  4796. '$mod.i=$mod.e;',
  4797. '']));
  4798. end;
  4799. procedure TTestModule.TestEnum_AsParams;
  4800. begin
  4801. StartProgram(false);
  4802. Add('type TEnum = (Red,Blue);');
  4803. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  4804. Add('var vJ: TEnum;');
  4805. Add('begin');
  4806. Add(' vg:=vg;');
  4807. Add(' vj:=vh;');
  4808. Add(' vi:=vi;');
  4809. Add(' doit(vg,vg,vg);');
  4810. Add(' doit(vh,vh,vj);');
  4811. Add(' doit(vi,vi,vi);');
  4812. Add(' doit(vj,vj,vj);');
  4813. Add('end;');
  4814. Add('var i: TEnum;');
  4815. Add('begin');
  4816. Add(' doit(i,i,i);');
  4817. ConvertProgram;
  4818. CheckSource('TestEnum_AsParams',
  4819. LinesToStr([ // statements
  4820. 'this.TEnum = {',
  4821. ' "0": "Red",',
  4822. ' Red: 0,',
  4823. ' "1": "Blue",',
  4824. ' Blue: 1',
  4825. '};',
  4826. 'this.DoIt = function (vG,vH,vI) {',
  4827. ' var vJ = 0;',
  4828. ' vG = vG;',
  4829. ' vJ = vH;',
  4830. ' vI.set(vI.get());',
  4831. ' $mod.DoIt(vG, vG, {',
  4832. ' get: function () {',
  4833. ' return vG;',
  4834. ' },',
  4835. ' set: function (v) {',
  4836. ' vG = v;',
  4837. ' }',
  4838. ' });',
  4839. ' $mod.DoIt(vH, vH, {',
  4840. ' get: function () {',
  4841. ' return vJ;',
  4842. ' },',
  4843. ' set: function (v) {',
  4844. ' vJ = v;',
  4845. ' }',
  4846. ' });',
  4847. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4848. ' $mod.DoIt(vJ, vJ, {',
  4849. ' get: function () {',
  4850. ' return vJ;',
  4851. ' },',
  4852. ' set: function (v) {',
  4853. ' vJ = v;',
  4854. ' }',
  4855. ' });',
  4856. '};',
  4857. 'this.i = 0;'
  4858. ]),
  4859. LinesToStr([
  4860. '$mod.DoIt($mod.i,$mod.i,{',
  4861. ' p: $mod,',
  4862. ' get: function () {',
  4863. ' return this.p.i;',
  4864. ' },',
  4865. ' set: function (v) {',
  4866. ' this.p.i = v;',
  4867. ' }',
  4868. '});'
  4869. ]));
  4870. end;
  4871. procedure TTestModule.TestEnumRange_Array;
  4872. begin
  4873. StartProgram(false);
  4874. Add([
  4875. 'type',
  4876. ' TEnum = (Red, Green, Blue);',
  4877. ' TEnumRg = green..blue;',
  4878. ' TArr = array[TEnumRg] of byte;',
  4879. ' TArr2 = array[green..blue] of byte;',
  4880. 'var',
  4881. ' a: TArr;',
  4882. ' b: TArr = (3,4);',
  4883. ' c: TArr2 = (5,6);',
  4884. 'begin',
  4885. ' a[green] := b[blue];',
  4886. ' c[green] := c[blue];',
  4887. '']);
  4888. ConvertProgram;
  4889. CheckSource('TestEnumRange_Array',
  4890. LinesToStr([ // statements
  4891. 'this.TEnum = {',
  4892. ' "0": "Red",',
  4893. ' Red: 0,',
  4894. ' "1": "Green",',
  4895. ' Green: 1,',
  4896. ' "2": "Blue",',
  4897. ' Blue: 2',
  4898. '};',
  4899. 'this.a = rtl.arraySetLength(null, 0, 2);',
  4900. 'this.b = [3, 4];',
  4901. 'this.c = [5, 6];',
  4902. '']),
  4903. LinesToStr([
  4904. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  4905. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  4906. '']));
  4907. end;
  4908. procedure TTestModule.TestEnum_ForIn;
  4909. begin
  4910. StartProgram(false);
  4911. Add([
  4912. 'type',
  4913. ' TEnum = (Red, Green, Blue);',
  4914. ' TEnumRg = green..blue;',
  4915. ' TArr = array[TEnum] of byte;',
  4916. ' TArrRg = array[TEnumRg] of byte;',
  4917. 'var',
  4918. ' e: TEnum;',
  4919. ' a1: TArr = (3,4,5);',
  4920. ' a2: TArrRg = (11,12);',
  4921. ' b: byte;',
  4922. 'begin',
  4923. ' for e in TEnum do ;',
  4924. ' for e in TEnumRg do ;',
  4925. ' for e in TArr do ;',
  4926. ' for e in TArrRg do ;',
  4927. ' for b in a1 do ;',
  4928. ' for b in a2 do ;',
  4929. '']);
  4930. ConvertProgram;
  4931. CheckSource('TestEnum_ForIn',
  4932. LinesToStr([ // statements
  4933. 'this.TEnum = {',
  4934. ' "0": "Red",',
  4935. ' Red: 0,',
  4936. ' "1": "Green",',
  4937. ' Green: 1,',
  4938. ' "2": "Blue",',
  4939. ' Blue: 2',
  4940. '};',
  4941. 'this.e = 0;',
  4942. 'this.a1 = [3, 4, 5];',
  4943. 'this.a2 = [11, 12];',
  4944. 'this.b = 0;',
  4945. '']),
  4946. LinesToStr([
  4947. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  4948. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  4949. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  4950. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  4951. ' for (var $in1 = $mod.a1, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.b = $in1[$l2];',
  4952. ' for (var $in4 = $mod.a2, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.b = $in4[$l5];',
  4953. '']));
  4954. end;
  4955. procedure TTestModule.TestEnum_ScopedNumber;
  4956. begin
  4957. Converter.Options:=Converter.Options+[coEnumNumbers];
  4958. StartProgram(false);
  4959. Add([
  4960. 'type',
  4961. ' TEnum = (Red, Green);',
  4962. 'var',
  4963. ' e: TEnum;',
  4964. 'begin',
  4965. ' e:=TEnum.Green;',
  4966. '']);
  4967. ConvertProgram;
  4968. CheckSource('TestEnum_ScopedNumber',
  4969. LinesToStr([ // statements
  4970. 'this.TEnum = {',
  4971. ' "0": "Red",',
  4972. ' Red: 0,',
  4973. ' "1": "Green",',
  4974. ' Green: 1',
  4975. '};',
  4976. 'this.e = 0;',
  4977. '']),
  4978. LinesToStr([
  4979. '$mod.e = 1;']));
  4980. end;
  4981. procedure TTestModule.TestEnum_InFunction;
  4982. begin
  4983. StartProgram(false);
  4984. Add([
  4985. 'const TEnum = 3;',
  4986. 'procedure DoIt;',
  4987. 'type',
  4988. ' TEnum = (Red, Green, Blue);',
  4989. ' procedure Sub;',
  4990. ' type',
  4991. ' TEnumSub = (Left, Right);',
  4992. ' var',
  4993. ' es: TEnumSub;',
  4994. ' begin',
  4995. ' es:=Left;',
  4996. ' end;',
  4997. 'var',
  4998. ' e, e2: TEnum;',
  4999. 'begin',
  5000. ' if e in [red,blue] then e2:=e;',
  5001. 'end;',
  5002. 'begin']);
  5003. ConvertProgram;
  5004. CheckSource('TestEnum_InFunction',
  5005. LinesToStr([ // statements
  5006. 'this.TEnum = 3;',
  5007. 'var TEnum$1 = {',
  5008. ' "0":"Red",',
  5009. ' Red:0,',
  5010. ' "1":"Green",',
  5011. ' Green:1,',
  5012. ' "2":"Blue",',
  5013. ' Blue:2',
  5014. ' };',
  5015. 'var TEnumSub = {',
  5016. ' "0": "Left",',
  5017. ' Left: 0,',
  5018. ' "1": "Right",',
  5019. ' Right: 1',
  5020. '};',
  5021. 'this.DoIt = function () {',
  5022. ' function Sub() {',
  5023. ' var es = 0;',
  5024. ' es = TEnumSub.Left;',
  5025. ' };',
  5026. ' var e = 0;',
  5027. ' var e2 = 0;',
  5028. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5029. '};',
  5030. '']),
  5031. LinesToStr([
  5032. '']));
  5033. end;
  5034. procedure TTestModule.TestSet_Enum;
  5035. begin
  5036. StartProgram(false);
  5037. Add([
  5038. 'type',
  5039. ' TColor = (Red, Green, Blue);',
  5040. ' TColors = set of TColor;',
  5041. 'var',
  5042. ' c: TColor;',
  5043. ' s: TColors;',
  5044. ' t: TColors = [];',
  5045. ' u: TColors = [Red];',
  5046. 'begin',
  5047. ' s:=[];',
  5048. ' s:=[Green];',
  5049. ' s:=[Green,Blue];',
  5050. ' s:=[Red..Blue];',
  5051. ' s:=[Red,Green..Blue];',
  5052. ' s:=[Red,c];',
  5053. ' s:=t;',
  5054. ' s:=default(TColors);',
  5055. '']);
  5056. ConvertProgram;
  5057. CheckSource('TestSet',
  5058. LinesToStr([ // statements
  5059. 'this.TColor = {',
  5060. ' "0":"Red",',
  5061. ' Red:0,',
  5062. ' "1":"Green",',
  5063. ' Green:1,',
  5064. ' "2":"Blue",',
  5065. ' Blue:2',
  5066. ' };',
  5067. 'this.c = 0;',
  5068. 'this.s = {};',
  5069. 'this.t = {};',
  5070. 'this.u = rtl.createSet($mod.TColor.Red);'
  5071. ]),
  5072. LinesToStr([
  5073. '$mod.s={};',
  5074. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5075. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5076. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5077. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5078. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5079. '$mod.s=rtl.refSet($mod.t);',
  5080. '$mod.s={};',
  5081. '']));
  5082. end;
  5083. procedure TTestModule.TestSet_Operators;
  5084. begin
  5085. StartProgram(false);
  5086. Add('type');
  5087. Add(' TColor = (Red, Green, Blue);');
  5088. Add(' TColors = set of tcolor;');
  5089. Add('var');
  5090. Add(' vC: TColor;');
  5091. Add(' vS: TColors;');
  5092. Add(' vT: TColors;');
  5093. Add(' vU: TColors;');
  5094. Add(' B: boolean;');
  5095. Add('begin');
  5096. Add(' include(vs,green);');
  5097. Add(' exclude(vs,vc);');
  5098. Add(' vs:=vt+vu;');
  5099. Add(' vs:=vt+[red];');
  5100. Add(' vs:=[red]+vt;');
  5101. Add(' vs:=[red]+[green];');
  5102. Add(' vs:=vt-vu;');
  5103. Add(' vs:=vt-[red];');
  5104. Add(' vs:=[red]-vt;');
  5105. Add(' vs:=[red]-[green];');
  5106. Add(' vs:=vt*vu;');
  5107. Add(' vs:=vt*[red];');
  5108. Add(' vs:=[red]*vt;');
  5109. Add(' vs:=[red]*[green];');
  5110. Add(' vs:=vt><vu;');
  5111. Add(' vs:=vt><[red];');
  5112. Add(' vs:=[red]><vt;');
  5113. Add(' vs:=[red]><[green];');
  5114. Add(' b:=vt=vu;');
  5115. Add(' b:=vt=[red];');
  5116. Add(' b:=[red]=vt;');
  5117. Add(' b:=[red]=[green];');
  5118. Add(' b:=vt<>vu;');
  5119. Add(' b:=vt<>[red];');
  5120. Add(' b:=[red]<>vt;');
  5121. Add(' b:=[red]<>[green];');
  5122. Add(' b:=vt<=vu;');
  5123. Add(' b:=vt<=[red];');
  5124. Add(' b:=[red]<=vt;');
  5125. Add(' b:=[red]<=[green];');
  5126. Add(' b:=vt>=vu;');
  5127. Add(' b:=vt>=[red];');
  5128. Add(' b:=[red]>=vt;');
  5129. Add(' b:=[red]>=[green];');
  5130. ConvertProgram;
  5131. CheckSource('TestSet_Operators',
  5132. LinesToStr([ // statements
  5133. 'this.TColor = {',
  5134. ' "0":"Red",',
  5135. ' Red:0,',
  5136. ' "1":"Green",',
  5137. ' Green:1,',
  5138. ' "2":"Blue",',
  5139. ' Blue:2',
  5140. ' };',
  5141. 'this.vC = 0;',
  5142. 'this.vS = {};',
  5143. 'this.vT = {};',
  5144. 'this.vU = {};',
  5145. 'this.B = false;'
  5146. ]),
  5147. LinesToStr([
  5148. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5149. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5150. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5151. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5152. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5153. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5154. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5155. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5156. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5157. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5158. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5159. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5160. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5161. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5162. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5163. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5164. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5165. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5166. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5167. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5168. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5169. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5170. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5171. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5172. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5173. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5174. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5175. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5176. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5177. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5178. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5179. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5180. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5181. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5182. '']));
  5183. end;
  5184. procedure TTestModule.TestSet_Operator_In;
  5185. begin
  5186. StartProgram(false);
  5187. Add('type');
  5188. Add(' TColor = (Red, Green, Blue);');
  5189. Add(' TColors = set of tcolor;');
  5190. Add('var');
  5191. Add(' vC: tcolor;');
  5192. Add(' vT: tcolors;');
  5193. Add(' B: boolean;');
  5194. Add('begin');
  5195. Add(' b:=red in vt;');
  5196. Add(' b:=vc in vt;');
  5197. Add(' b:=green in [red..blue];');
  5198. Add(' b:=vc in [red..blue];');
  5199. Add(' ');
  5200. Add(' if red in vt then ;');
  5201. Add(' while vC in vt do ;');
  5202. Add(' repeat');
  5203. Add(' until vC in vt;');
  5204. ConvertProgram;
  5205. CheckSource('TestSet_Operator_In',
  5206. LinesToStr([ // statements
  5207. 'this.TColor = {',
  5208. ' "0":"Red",',
  5209. ' Red:0,',
  5210. ' "1":"Green",',
  5211. ' Green:1,',
  5212. ' "2":"Blue",',
  5213. ' Blue:2',
  5214. ' };',
  5215. 'this.vC = 0;',
  5216. 'this.vT = {};',
  5217. 'this.B = false;'
  5218. ]),
  5219. LinesToStr([
  5220. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5221. '$mod.B = $mod.vC in $mod.vT;',
  5222. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5223. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5224. 'if ($mod.TColor.Red in $mod.vT) ;',
  5225. 'while ($mod.vC in $mod.vT) {',
  5226. '};',
  5227. 'do {',
  5228. '} while (!($mod.vC in $mod.vT));',
  5229. '']));
  5230. end;
  5231. procedure TTestModule.TestSet_Functions;
  5232. begin
  5233. StartProgram(false);
  5234. Add('type');
  5235. Add(' TMyEnum = (Red, Green);');
  5236. Add(' TMyEnums = set of TMyEnum;');
  5237. Add('var');
  5238. Add(' e: TMyEnum;');
  5239. Add(' s: TMyEnums;');
  5240. Add('begin');
  5241. Add(' e:=Low(TMyEnums);');
  5242. Add(' e:=Low(s);');
  5243. Add(' e:=High(TMyEnums);');
  5244. Add(' e:=High(s);');
  5245. ConvertProgram;
  5246. CheckSource('TestSetFunctions',
  5247. LinesToStr([ // statements
  5248. 'this.TMyEnum = {',
  5249. ' "0":"Red",',
  5250. ' Red:0,',
  5251. ' "1":"Green",',
  5252. ' Green:1',
  5253. ' };',
  5254. 'this.e = 0;',
  5255. 'this.s = {};'
  5256. ]),
  5257. LinesToStr([
  5258. '$mod.e=$mod.TMyEnum.Red;',
  5259. '$mod.e=$mod.TMyEnum.Red;',
  5260. '$mod.e=$mod.TMyEnum.Green;',
  5261. '$mod.e=$mod.TMyEnum.Green;',
  5262. '']));
  5263. end;
  5264. procedure TTestModule.TestSet_PassAsArgClone;
  5265. begin
  5266. StartProgram(false);
  5267. Add('type');
  5268. Add(' TMyEnum = (Red, Green);');
  5269. Add(' TMyEnums = set of TMyEnum;');
  5270. Add('procedure DoDefault(s: tmyenums); begin end;');
  5271. Add('procedure DoConst(const s: tmyenums); begin end;');
  5272. Add('var');
  5273. Add(' aSet: tmyenums;');
  5274. Add('begin');
  5275. Add(' dodefault(aset);');
  5276. Add(' doconst(aset);');
  5277. ConvertProgram;
  5278. CheckSource('TestSetFunctions',
  5279. LinesToStr([ // statements
  5280. 'this.TMyEnum = {',
  5281. ' "0":"Red",',
  5282. ' Red:0,',
  5283. ' "1":"Green",',
  5284. ' Green:1',
  5285. ' };',
  5286. 'this.DoDefault = function (s) {',
  5287. '};',
  5288. 'this.DoConst = function (s) {',
  5289. '};',
  5290. 'this.aSet = {};'
  5291. ]),
  5292. LinesToStr([
  5293. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5294. '$mod.DoConst($mod.aSet);',
  5295. '']));
  5296. end;
  5297. procedure TTestModule.TestSet_AsParams;
  5298. begin
  5299. StartProgram(false);
  5300. Add([
  5301. 'type TEnum = (Red,Blue);',
  5302. 'type TEnums = set of TEnum;',
  5303. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5304. 'var vJ: TEnums;',
  5305. 'begin',
  5306. ' Include(vg,red);',
  5307. ' Include(result,blue);',
  5308. ' vg:=vg;',
  5309. ' vj:=vh;',
  5310. ' vi:=vi;',
  5311. ' doit(vg,vg,vg);',
  5312. ' doit(vh,vh,vj);',
  5313. ' doit(vi,vi,vi);',
  5314. ' doit(vj,vj,vj);',
  5315. 'end;',
  5316. 'var i: TEnums;',
  5317. 'begin',
  5318. ' doit(i,i,i);']);
  5319. ConvertProgram;
  5320. CheckSource('TestSet_AsParams',
  5321. LinesToStr([ // statements
  5322. 'this.TEnum = {',
  5323. ' "0": "Red",',
  5324. ' Red: 0,',
  5325. ' "1": "Blue",',
  5326. ' Blue: 1',
  5327. '};',
  5328. 'this.DoIt = function (vG,vH,vI) {',
  5329. ' var Result = {};',
  5330. ' var vJ = {};',
  5331. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5332. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5333. ' vG = rtl.refSet(vG);',
  5334. ' vJ = rtl.refSet(vH);',
  5335. ' vI.set(rtl.refSet(vI.get()));',
  5336. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5337. ' get: function () {',
  5338. ' return vG;',
  5339. ' },',
  5340. ' set: function (v) {',
  5341. ' vG = v;',
  5342. ' }',
  5343. ' });',
  5344. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5345. ' get: function () {',
  5346. ' return vJ;',
  5347. ' },',
  5348. ' set: function (v) {',
  5349. ' vJ = v;',
  5350. ' }',
  5351. ' });',
  5352. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5353. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5354. ' get: function () {',
  5355. ' return vJ;',
  5356. ' },',
  5357. ' set: function (v) {',
  5358. ' vJ = v;',
  5359. ' }',
  5360. ' });',
  5361. ' return Result;',
  5362. '};',
  5363. 'this.i = {};'
  5364. ]),
  5365. LinesToStr([
  5366. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5367. ' p: $mod,',
  5368. ' get: function () {',
  5369. ' return this.p.i;',
  5370. ' },',
  5371. ' set: function (v) {',
  5372. ' this.p.i = v;',
  5373. ' }',
  5374. '});'
  5375. ]));
  5376. end;
  5377. procedure TTestModule.TestSet_Property;
  5378. begin
  5379. StartProgram(false);
  5380. Add('type');
  5381. Add(' TEnum = (Red,Blue);');
  5382. Add(' TEnums = set of TEnum;');
  5383. Add(' TObject = class');
  5384. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5385. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5386. Add(' property Colors: TEnums read GetColors write SetColors;');
  5387. Add(' end;');
  5388. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5389. Add('begin end;');
  5390. Add('var Obj: TObject;');
  5391. Add('begin');
  5392. Add(' Include(Obj.Colors,Red);');
  5393. Add(' Exclude(Obj.Colors,Red);');
  5394. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5395. ConvertProgram;
  5396. CheckSource('TestSet_Property',
  5397. LinesToStr([ // statements
  5398. 'this.TEnum = {',
  5399. ' "0": "Red",',
  5400. ' Red: 0,',
  5401. ' "1": "Blue",',
  5402. ' Blue: 1',
  5403. '};',
  5404. 'rtl.createClass($mod, "TObject", null, function () {',
  5405. ' this.$init = function () {',
  5406. ' };',
  5407. ' this.$final = function () {',
  5408. ' };',
  5409. '});',
  5410. 'this.DoIt = function (i, j, k, l) {',
  5411. '};',
  5412. 'this.Obj = null;',
  5413. '']),
  5414. LinesToStr([
  5415. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5416. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5417. '']));
  5418. end;
  5419. procedure TTestModule.TestSet_EnumConst;
  5420. begin
  5421. StartProgram(false);
  5422. Add([
  5423. 'type',
  5424. ' TEnum = (Red,Blue);',
  5425. ' TEnums = set of TEnum;',
  5426. 'const',
  5427. ' Orange = red;',
  5428. 'var',
  5429. ' Enum: tenum;',
  5430. ' Enums: tenums;',
  5431. 'begin',
  5432. ' Include(enums,orange);',
  5433. ' Exclude(enums,orange);',
  5434. ' if orange in enums then;',
  5435. ' if orange in [orange,red] then;']);
  5436. ConvertProgram;
  5437. CheckSource('TestSet_EnumConst',
  5438. LinesToStr([ // statements
  5439. 'this.TEnum = {',
  5440. ' "0": "Red",',
  5441. ' Red: 0,',
  5442. ' "1": "Blue",',
  5443. ' Blue: 1',
  5444. '};',
  5445. 'this.Orange = $mod.TEnum.Red;',
  5446. 'this.Enum = 0;',
  5447. 'this.Enums = {};',
  5448. '']),
  5449. LinesToStr([
  5450. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5451. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5452. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5453. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5454. '']));
  5455. end;
  5456. procedure TTestModule.TestSet_IntConst;
  5457. begin
  5458. StartProgram(false);
  5459. Add([
  5460. 'type',
  5461. ' TEnums = set of Byte;',
  5462. 'const',
  5463. ' Orange = 0;',
  5464. 'var',
  5465. ' Enum: byte;',
  5466. ' Enums: tenums;',
  5467. 'begin',
  5468. ' Enums:=[];',
  5469. ' Enums:=[0];',
  5470. ' Enums:=[1..2];',
  5471. //' Include(enums,orange);',
  5472. //' Exclude(enums,orange);',
  5473. ' if orange in enums then;',
  5474. ' if orange in [orange,1] then;']);
  5475. ConvertProgram;
  5476. CheckSource('TestSet_IntConst',
  5477. LinesToStr([ // statements
  5478. 'this.Orange = 0;',
  5479. 'this.Enum = 0;',
  5480. 'this.Enums = {};',
  5481. '']),
  5482. LinesToStr([
  5483. '$mod.Enums = {};',
  5484. '$mod.Enums = rtl.createSet(0);',
  5485. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5486. 'if (0 in $mod.Enums) ;',
  5487. 'if (0 in rtl.createSet(0, 1)) ;',
  5488. '']));
  5489. end;
  5490. procedure TTestModule.TestSet_AnonymousEnumType;
  5491. begin
  5492. StartProgram(false);
  5493. Add('type');
  5494. Add(' TFlags = set of (red, green);');
  5495. Add('const');
  5496. Add(' favorite = red;');
  5497. Add('var');
  5498. Add(' f: TFlags;');
  5499. Add(' i: longint;');
  5500. Add('begin');
  5501. Add(' Include(f,red);');
  5502. Add(' Include(f,favorite);');
  5503. Add(' i:=ord(red);');
  5504. Add(' i:=ord(favorite);');
  5505. Add(' i:=ord(low(TFlags));');
  5506. Add(' i:=ord(low(f));');
  5507. Add(' i:=ord(low(favorite));');
  5508. Add(' i:=ord(high(TFlags));');
  5509. Add(' i:=ord(high(f));');
  5510. Add(' i:=ord(high(favorite));');
  5511. Add(' f:=[green,favorite];');
  5512. ConvertProgram;
  5513. CheckSource('TestSet_AnonymousEnumType',
  5514. LinesToStr([ // statements
  5515. 'this.TFlags$a = {',
  5516. ' "0": "red",',
  5517. ' red: 0,',
  5518. ' "1": "green",',
  5519. ' green: 1',
  5520. '};',
  5521. 'this.favorite = $mod.TFlags$a.red;',
  5522. 'this.f = {};',
  5523. 'this.i = 0;',
  5524. '']),
  5525. LinesToStr([
  5526. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5527. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5528. '$mod.i = $mod.TFlags$a.red;',
  5529. '$mod.i = $mod.TFlags$a.red;',
  5530. '$mod.i = $mod.TFlags$a.red;',
  5531. '$mod.i = $mod.TFlags$a.red;',
  5532. '$mod.i = $mod.TFlags$a.red;',
  5533. '$mod.i = $mod.TFlags$a.green;',
  5534. '$mod.i = $mod.TFlags$a.green;',
  5535. '$mod.i = $mod.TFlags$a.green;',
  5536. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5537. '']));
  5538. end;
  5539. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5540. begin
  5541. exit;
  5542. StartProgram(false);
  5543. Add([
  5544. 'type',
  5545. ' TAtoZ = ''A''..''Z'';',
  5546. ' TSetOfAZ = set of TAtoZ;',
  5547. 'var',
  5548. ' c: char;',
  5549. ' a: TAtoZ;',
  5550. ' s: TSetOfAZ = [''P'',''A''];',
  5551. ' i: longint;',
  5552. 'begin',
  5553. ' Include(s,''S'');',
  5554. ' Include(s,c);',
  5555. ' Include(s,a);',
  5556. ' c:=low(TAtoZ);',
  5557. ' i:=ord(low(TAtoZ));',
  5558. ' a:=high(TAtoZ);',
  5559. ' a:=high(TSetOfAtoZ);',
  5560. ' s:=[a,c,''M''];',
  5561. '']);
  5562. ConvertProgram;
  5563. CheckSource('TestSet_AnonymousEnumTypeChar',
  5564. LinesToStr([ // statements
  5565. '']),
  5566. LinesToStr([
  5567. '']));
  5568. end;
  5569. procedure TTestModule.TestSet_ConstEnum;
  5570. begin
  5571. StartProgram(false);
  5572. Add([
  5573. 'type',
  5574. ' TEnum = (red,blue,green);',
  5575. ' TEnums = set of TEnum;',
  5576. 'const',
  5577. ' teAny = [low(TEnum)..high(TEnum)];',
  5578. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5579. 'var',
  5580. ' e: TEnum;',
  5581. ' s: TEnums;',
  5582. 'begin',
  5583. ' if blue in teAny then;',
  5584. ' if blue in teAny+[e] then;',
  5585. ' if blue in teAny+teRedBlue then;',
  5586. ' if e in [red,blue] then;',
  5587. ' s:=teAny;',
  5588. ' s:=teAny+[e];',
  5589. ' s:=[e]+teAny;',
  5590. ' s:=teAny+teRedBlue;',
  5591. ' s:=teAny+teRedBlue+[e];',
  5592. '']);
  5593. ConvertProgram;
  5594. CheckSource('TestSet_ConstEnum',
  5595. LinesToStr([ // statements
  5596. 'this.TEnum = {',
  5597. ' "0": "red",',
  5598. ' red: 0,',
  5599. ' "1": "blue",',
  5600. ' blue: 1,',
  5601. ' "2": "green",',
  5602. ' green: 2',
  5603. '};',
  5604. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5605. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5606. 'this.e = 0;',
  5607. 'this.s = {};',
  5608. '']),
  5609. LinesToStr([
  5610. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5611. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5612. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5613. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5614. '$mod.s = rtl.refSet($mod.teAny);',
  5615. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5616. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5617. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5618. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5619. '']));
  5620. end;
  5621. procedure TTestModule.TestSet_ConstChar;
  5622. begin
  5623. StartProgram(false);
  5624. Add([
  5625. 'const',
  5626. ' LowChars = [''a''..''z''];',
  5627. ' Chars = LowChars+[''A''..''Z''];',
  5628. ' sc = [''А'', ''Я''];',
  5629. 'var',
  5630. ' c: char;',
  5631. ' s: string;',
  5632. 'begin',
  5633. ' if c in lowchars then ;',
  5634. ' if ''a'' in lowchars then ;',
  5635. ' if s[1] in lowchars then ;',
  5636. ' if c in chars then ;',
  5637. ' if c in [''a''..''z'',''_''] then ;',
  5638. ' if ''b'' in [''a''..''z'',''_''] then ;',
  5639. ' if ''Я'' in sc then ;',
  5640. '']);
  5641. ConvertProgram;
  5642. CheckSource('TestSet_ConstChar',
  5643. LinesToStr([ // statements
  5644. 'this.LowChars = rtl.createSet(null, 97, 122);',
  5645. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  5646. 'this.sc = rtl.createSet(1040, 1071);',
  5647. 'this.c = "";',
  5648. 'this.s = "";',
  5649. '']),
  5650. LinesToStr([
  5651. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  5652. 'if (97 in $mod.LowChars) ;',
  5653. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  5654. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  5655. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  5656. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  5657. 'if (1071 in $mod.sc) ;',
  5658. '']));
  5659. end;
  5660. procedure TTestModule.TestSet_ConstInt;
  5661. begin
  5662. StartProgram(false);
  5663. Add([
  5664. 'const',
  5665. ' Months = [1..12];',
  5666. ' Mirror = [-12..-1]+Months;',
  5667. 'var',
  5668. ' i: smallint;',
  5669. 'begin',
  5670. ' if 3 in Months then;',
  5671. ' if i in Months+[i] then;',
  5672. ' if i in Months+Mirror then;',
  5673. ' if i in [4..6,8] then;',
  5674. '']);
  5675. ConvertProgram;
  5676. CheckSource('TestSet_ConstInt',
  5677. LinesToStr([ // statements
  5678. 'this.Months = rtl.createSet(null, 1, 12);',
  5679. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  5680. 'this.i = 0;',
  5681. '']),
  5682. LinesToStr([
  5683. 'if (3 in $mod.Months) ;',
  5684. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  5685. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  5686. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  5687. '']));
  5688. end;
  5689. procedure TTestModule.TestSet_InFunction;
  5690. begin
  5691. StartProgram(false);
  5692. Add([
  5693. 'const',
  5694. ' TEnum = 3;',
  5695. ' TSetOfEnum = 4;',
  5696. ' TSetOfAno = 5;',
  5697. 'procedure DoIt;',
  5698. 'type',
  5699. ' TEnum = (red, blue);',
  5700. ' TSetOfEnum = set of TEnum;',
  5701. ' TSetOfAno = set of (up,down);',
  5702. 'var',
  5703. ' e: TEnum;',
  5704. ' se: TSetOfEnum;',
  5705. ' sa: TSetOfAno;',
  5706. 'begin',
  5707. ' se:=[e];',
  5708. ' sa:=[up];',
  5709. 'end;',
  5710. 'begin',
  5711. '']);
  5712. ConvertProgram;
  5713. CheckSource('TestSet_InFunction',
  5714. LinesToStr([ // statements
  5715. 'this.TEnum = 3;',
  5716. 'this.TSetOfEnum = 4;',
  5717. 'this.TSetOfAno = 5;',
  5718. 'var TEnum$1 = {',
  5719. ' "0": "red",',
  5720. ' red: 0,',
  5721. ' "1": "blue",',
  5722. ' blue: 1',
  5723. '};',
  5724. 'var TSetOfAno$a = {',
  5725. ' "0": "up",',
  5726. ' up: 0,',
  5727. ' "1": "down",',
  5728. ' down: 1',
  5729. '};',
  5730. 'this.DoIt = function () {',
  5731. ' var e = 0;',
  5732. ' var se = {};',
  5733. ' var sa = {};',
  5734. ' se = rtl.createSet(e);',
  5735. ' sa = rtl.createSet(TSetOfAno$a.up);',
  5736. '};',
  5737. '']),
  5738. LinesToStr([
  5739. '']));
  5740. end;
  5741. procedure TTestModule.TestSet_ForIn;
  5742. begin
  5743. StartProgram(false);
  5744. Add([
  5745. 'type',
  5746. ' TEnum = (Red, Green, Blue);',
  5747. ' TEnumRg = green..blue;',
  5748. ' TSetOfEnum = set of TEnum;',
  5749. ' TSetOfEnumRg = set of TEnumRg;',
  5750. 'var',
  5751. ' e, e2: TEnum;',
  5752. ' er: TEnum;',
  5753. ' s: TSetOfEnum;',
  5754. 'begin',
  5755. ' for e in TSetOfEnum do ;',
  5756. ' for e in TSetOfEnumRg do ;',
  5757. ' for e in [] do e2:=e;',
  5758. ' for e in [red..green] do e2:=e;',
  5759. ' for e in [green,blue] do e2:=e;',
  5760. ' for e in [red,blue] do e2:=e;',
  5761. ' for e in s do e2:=e;',
  5762. ' for er in TSetOfEnumRg do ;',
  5763. '']);
  5764. ConvertProgram;
  5765. CheckSource('TestSet_ForIn',
  5766. LinesToStr([ // statements
  5767. 'this.TEnum = {',
  5768. ' "0":"Red",',
  5769. ' Red:0,',
  5770. ' "1":"Green",',
  5771. ' Green:1,',
  5772. ' "2":"Blue",',
  5773. ' Blue:2',
  5774. ' };',
  5775. 'this.e = 0;',
  5776. 'this.e2 = 0;',
  5777. 'this.er = 0;',
  5778. 'this.s = {};',
  5779. '']),
  5780. LinesToStr([
  5781. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5782. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5783. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  5784. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  5785. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  5786. 'for (var $l1 in $mod.s){',
  5787. ' $mod.e = +$l1;',
  5788. ' $mod.e2 = $mod.e;',
  5789. '};',
  5790. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  5791. '']));
  5792. end;
  5793. procedure TTestModule.TestNestBegin;
  5794. begin
  5795. StartProgram(false);
  5796. Add('begin');
  5797. Add(' begin');
  5798. Add(' begin');
  5799. Add(' end;');
  5800. Add(' begin');
  5801. Add(' if true then ;');
  5802. Add(' end;');
  5803. Add(' end;');
  5804. ConvertProgram;
  5805. CheckSource('TestNestBegin',
  5806. '',
  5807. 'if (true) ;');
  5808. end;
  5809. procedure TTestModule.TestUnitImplVars;
  5810. begin
  5811. StartUnit(false);
  5812. Add('interface');
  5813. Add('implementation');
  5814. Add('var');
  5815. Add(' V1:longint;');
  5816. Add(' V2:longint = 3;');
  5817. Add(' V3:string = ''abc'';');
  5818. ConvertUnit;
  5819. CheckSource('TestUnitImplVars',
  5820. LinesToStr([ // statements
  5821. 'var $impl = $mod.$impl;',
  5822. '']),
  5823. '', // this.$init
  5824. LinesToStr([ // implementation
  5825. '$impl.V1 = 0;',
  5826. '$impl.V2 = 3;',
  5827. '$impl.V3 = "abc";',
  5828. '']) );
  5829. end;
  5830. procedure TTestModule.TestUnitImplConsts;
  5831. begin
  5832. StartUnit(false);
  5833. Add('interface');
  5834. Add('implementation');
  5835. Add('const');
  5836. Add(' v1 = 3;');
  5837. Add(' v2:longint = 4;');
  5838. Add(' v3:string = ''abc'';');
  5839. ConvertUnit;
  5840. CheckSource('TestUnitImplConsts',
  5841. LinesToStr([ // statements
  5842. 'var $impl = $mod.$impl;',
  5843. '']),
  5844. '', // this.$init
  5845. LinesToStr([ // implementation
  5846. '$impl.v1 = 3;',
  5847. '$impl.v2 = 4;',
  5848. '$impl.v3 = "abc";',
  5849. '']) );
  5850. end;
  5851. procedure TTestModule.TestUnitImplRecord;
  5852. begin
  5853. StartUnit(false);
  5854. Add('interface');
  5855. Add('implementation');
  5856. Add('type');
  5857. Add(' TMyRecord = record');
  5858. Add(' i: longint;');
  5859. Add(' end;');
  5860. Add('var aRec: TMyRecord;');
  5861. Add('initialization');
  5862. Add(' arec.i:=3;');
  5863. ConvertUnit;
  5864. CheckSource('TestUnitImplRecord',
  5865. LinesToStr([ // statements
  5866. 'var $impl = $mod.$impl;',
  5867. '']),
  5868. // this.$init
  5869. '$impl.aRec.i = 3;',
  5870. LinesToStr([ // implementation
  5871. 'rtl.recNewT($impl, "TMyRecord", function () {',
  5872. ' this.i = 0;',
  5873. ' this.$eq = function (b) {',
  5874. ' return this.i === b.i;',
  5875. ' };',
  5876. ' this.$assign = function (s) {',
  5877. ' this.i = s.i;',
  5878. ' return this;',
  5879. ' };',
  5880. '});',
  5881. '$impl.aRec = $impl.TMyRecord.$new();',
  5882. '']) );
  5883. end;
  5884. procedure TTestModule.TestRenameJSNameConflict;
  5885. begin
  5886. StartProgram(false);
  5887. Add('var apply: longint;');
  5888. Add('var bind: longint;');
  5889. Add('var call: longint;');
  5890. Add('begin');
  5891. ConvertProgram;
  5892. CheckSource('TestRenameJSNameConflict',
  5893. LinesToStr([ // statements
  5894. 'this.Apply = 0;',
  5895. 'this.Bind = 0;',
  5896. 'this.Call = 0;'
  5897. ]),
  5898. LinesToStr([ // this.$main
  5899. ''
  5900. ]));
  5901. end;
  5902. procedure TTestModule.TestLocalConst;
  5903. begin
  5904. StartProgram(false);
  5905. Add('procedure DoIt;');
  5906. Add('const');
  5907. Add(' cA: longint = 1;');
  5908. Add(' cB = 2;');
  5909. Add(' procedure Sub;');
  5910. Add(' const');
  5911. Add(' csA = 3;');
  5912. Add(' cB: double = 4;');
  5913. Add(' begin');
  5914. Add(' cb:=cb+csa;');
  5915. Add(' ca:=ca+csa+5;');
  5916. Add(' end;');
  5917. Add('begin');
  5918. Add(' ca:=ca+cb+6;');
  5919. Add('end;');
  5920. Add('begin');
  5921. ConvertProgram;
  5922. CheckSource('TestLocalConst',
  5923. LinesToStr([
  5924. 'var cA = 1;',
  5925. 'var cB = 2;',
  5926. 'var csA = 3;',
  5927. 'var cB$1 = 4;',
  5928. 'this.DoIt = function () {',
  5929. ' function Sub() {',
  5930. ' cB$1 = cB$1 + 3;',
  5931. ' cA = cA + 3 + 5;',
  5932. ' };',
  5933. ' cA = cA + 2 + 6;',
  5934. '};'
  5935. ]),
  5936. LinesToStr([
  5937. ]));
  5938. end;
  5939. procedure TTestModule.TestVarExternal;
  5940. begin
  5941. StartProgram(false);
  5942. Add('var');
  5943. Add(' NaN: double; external name ''Global.NaN'';');
  5944. Add(' d: double;');
  5945. Add('begin');
  5946. Add(' d:=NaN;');
  5947. ConvertProgram;
  5948. CheckSource('TestVarExternal',
  5949. LinesToStr([
  5950. 'this.d = 0.0;'
  5951. ]),
  5952. LinesToStr([
  5953. '$mod.d = Global.NaN;'
  5954. ]));
  5955. end;
  5956. procedure TTestModule.TestVarExternalOtherUnit;
  5957. begin
  5958. AddModuleWithIntfImplSrc('unit2.pas',
  5959. LinesToStr([
  5960. 'var NaN: double; external name ''Global.NaN'';',
  5961. 'var iV: longint;'
  5962. ]),
  5963. '');
  5964. StartUnit(true);
  5965. Add('interface');
  5966. Add('uses unit2;');
  5967. Add('implementation');
  5968. Add('var');
  5969. Add(' d: double;');
  5970. Add(' i: longint; external name ''$i'';');
  5971. Add('begin');
  5972. Add(' d:=nan;');
  5973. Add(' d:=uNit2.nan;');
  5974. Add(' d:=test1.d;');
  5975. Add(' i:=iv;');
  5976. Add(' i:=uNit2.iv;');
  5977. Add(' i:=test1.i;');
  5978. ConvertUnit;
  5979. CheckSource('TestVarExternalOtherUnit',
  5980. LinesToStr([
  5981. 'var $impl = $mod.$impl;',
  5982. '']),
  5983. LinesToStr([ // this.$init
  5984. '$impl.d = Global.NaN;',
  5985. '$impl.d = Global.NaN;',
  5986. '$impl.d = $impl.d;',
  5987. '$i = pas.unit2.iV;',
  5988. '$i = pas.unit2.iV;',
  5989. '$i = $i;',
  5990. '']),
  5991. LinesToStr([ // implementation
  5992. '$impl.d = 0.0;',
  5993. '']) );
  5994. end;
  5995. procedure TTestModule.TestVarAbsoluteFail;
  5996. begin
  5997. StartProgram(false);
  5998. Add([
  5999. 'var',
  6000. ' a: longint;',
  6001. ' b: longword absolute a;',
  6002. 'begin']);
  6003. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6004. ConvertProgram;
  6005. end;
  6006. procedure TTestModule.TestConstExternal;
  6007. begin
  6008. StartProgram(false);
  6009. Add([
  6010. 'const',
  6011. ' PI: double; external name ''Global.PI'';',
  6012. ' Tau = 2*pi;',
  6013. 'var d: double;',
  6014. 'begin',
  6015. ' d:=pi;',
  6016. ' d:=tau+pi;']);
  6017. ConvertProgram;
  6018. CheckSource('TestConstExternal',
  6019. LinesToStr([
  6020. 'this.Tau = 2*Global.PI;',
  6021. 'this.d = 0.0;'
  6022. ]),
  6023. LinesToStr([
  6024. '$mod.d = Global.PI;',
  6025. '$mod.d = $mod.Tau + Global.PI;'
  6026. ]));
  6027. end;
  6028. procedure TTestModule.TestDouble;
  6029. begin
  6030. StartProgram(false);
  6031. Add([
  6032. 'type',
  6033. ' TDateTime = double;',
  6034. 'const',
  6035. ' a = TDateTime(2.7);',
  6036. ' b = a + TDateTime(1.7);',
  6037. ' c = 0.9 + 0.1;',
  6038. ' f0_1 = 0.1;',
  6039. ' f0_3 = 0.3;',
  6040. ' fn0_1 = -0.1;',
  6041. ' fn0_3 = -0.3;',
  6042. ' fn0_003 = -0.003;',
  6043. ' fn0_123456789 = -0.123456789;',
  6044. ' fn300_0 = -300.0;',
  6045. ' fn123456_0 = -123456.0;',
  6046. ' fn1234567_8 = -1234567.8;',
  6047. ' fn12345678_9 = -12345678.9;',
  6048. ' f1_0En12 = 1E-12;',
  6049. ' fn1_0En12 = -1E-12;',
  6050. ' maxdouble = 1.7e+308;',
  6051. ' mindouble = -1.7e+308;',
  6052. ' MinSafeIntDouble = -$10000000000000;',
  6053. ' MinSafeIntDouble2 = -$fffffffffffff-1;',
  6054. ' MaxSafeIntDouble = $fffffffffffff;',
  6055. ' DZeroResolution = 1E-12;',
  6056. ' Minus1 = -1E-12;',
  6057. ' EPS = 1E-9;',
  6058. ' DELTA = 0.001;',
  6059. ' Big = 129.789E+100;',
  6060. ' Test0_15 = 0.15;',
  6061. ' Test999 = 2.9999999999999;',
  6062. ' Test111999 = 211199999999999000.0;',
  6063. ' TestMinus111999 = -211199999999999000.0;',
  6064. 'var',
  6065. ' d: double = b;',
  6066. 'begin',
  6067. ' d:=1.0;',
  6068. ' d:=1.0/3.0;',
  6069. ' d:=1/3;',
  6070. ' d:=5.0E-324;',
  6071. ' d:=1.7E308;',
  6072. ' d:=001.00E00;',
  6073. ' d:=002.00E001;',
  6074. ' d:=003.000E000;',
  6075. ' d:=-004.00E-00;',
  6076. ' d:=-005.00E-001;',
  6077. ' d:=10**3;',
  6078. ' d:=10 mod 3;',
  6079. ' d:=10 div 3;',
  6080. ' d:=c;',
  6081. ' d:=f0_1;',
  6082. ' d:=f0_3;',
  6083. ' d:=fn0_1;',
  6084. ' d:=fn0_3;',
  6085. ' d:=fn0_003;',
  6086. ' d:=fn0_123456789;',
  6087. ' d:=fn300_0;',
  6088. ' d:=fn123456_0;',
  6089. ' d:=fn1234567_8;',
  6090. ' d:=fn12345678_9;',
  6091. ' d:=f1_0En12;',
  6092. ' d:=fn1_0En12;',
  6093. ' d:=maxdouble;',
  6094. ' d:=mindouble;',
  6095. ' d:=MinSafeIntDouble;',
  6096. ' d:=double(MinSafeIntDouble);',
  6097. ' d:=MinSafeIntDouble2;',
  6098. ' d:=double(MinSafeIntDouble2);',
  6099. ' d:=MaxSafeIntDouble;',
  6100. ' d:=default(double);',
  6101. '']);
  6102. ConvertProgram;
  6103. CheckSource('TestDouble',
  6104. LinesToStr([
  6105. 'this.a = 2.7;',
  6106. 'this.b = 2.7 + 1.7;',
  6107. 'this.c = 0.9 + 0.1;',
  6108. 'this.f0_1 = 0.1;',
  6109. 'this.f0_3 = 0.3;',
  6110. 'this.fn0_1 = -0.1;',
  6111. 'this.fn0_3 = -0.3;',
  6112. 'this.fn0_003 = -0.003;',
  6113. 'this.fn0_123456789 = -0.123456789;',
  6114. 'this.fn300_0 = -300.0;',
  6115. 'this.fn123456_0 = -123456.0;',
  6116. 'this.fn1234567_8 = -1234567.8;',
  6117. 'this.fn12345678_9 = -12345678.9;',
  6118. 'this.f1_0En12 = 1E-12;',
  6119. 'this.fn1_0En12 = -1E-12;',
  6120. 'this.maxdouble = 1.7e+308;',
  6121. 'this.mindouble = -1.7e+308;',
  6122. 'this.MinSafeIntDouble = -0x10000000000000;',
  6123. 'this.MinSafeIntDouble2 = -0xfffffffffffff - 1;',
  6124. 'this.MaxSafeIntDouble = 0xfffffffffffff;',
  6125. 'this.DZeroResolution = 1E-12;',
  6126. 'this.Minus1 = -1E-12;',
  6127. 'this.EPS = 1E-9;',
  6128. 'this.DELTA = 0.001;',
  6129. 'this.Big = 129.789E+100;',
  6130. 'this.Test0_15 = 0.15;',
  6131. 'this.Test999 = 2.9999999999999;',
  6132. 'this.Test111999 = 211199999999999000.0;',
  6133. 'this.TestMinus111999 = -211199999999999000.0;',
  6134. 'this.d = 4.4;'
  6135. ]),
  6136. LinesToStr([
  6137. '$mod.d = 1.0;',
  6138. '$mod.d = 1.0 / 3.0;',
  6139. '$mod.d = 1 / 3;',
  6140. '$mod.d = 5.0E-324;',
  6141. '$mod.d = 1.7E308;',
  6142. '$mod.d = 1.00E0;',
  6143. '$mod.d = 2.00E1;',
  6144. '$mod.d = 3.000E0;',
  6145. '$mod.d = -4.00E-0;',
  6146. '$mod.d = -5.00E-1;',
  6147. '$mod.d = Math.pow(10, 3);',
  6148. '$mod.d = 10 % 3;',
  6149. '$mod.d = Math.floor(10 / 3);',
  6150. '$mod.d = 1;',
  6151. '$mod.d = 0.1;',
  6152. '$mod.d = 0.3;',
  6153. '$mod.d = -0.1;',
  6154. '$mod.d = -0.3;',
  6155. '$mod.d = -0.003;',
  6156. '$mod.d = -0.123456789;',
  6157. '$mod.d = -300;',
  6158. '$mod.d = -123456;',
  6159. '$mod.d = -1234567.8;',
  6160. '$mod.d = -1.23456789E7;',
  6161. '$mod.d = 1E-12;',
  6162. '$mod.d = -1E-12;',
  6163. '$mod.d = 1.7E308;',
  6164. '$mod.d = -1.7E308;',
  6165. '$mod.d = -4503599627370496;',
  6166. '$mod.d = -4503599627370496;',
  6167. '$mod.d = -4503599627370496;',
  6168. '$mod.d = -4503599627370496;',
  6169. '$mod.d = 4503599627370495;',
  6170. '$mod.d = 0.0;',
  6171. '']));
  6172. end;
  6173. procedure TTestModule.TestInteger;
  6174. begin
  6175. StartProgram(false);
  6176. Add([
  6177. 'const',
  6178. ' MinInt = low(NativeInt);',
  6179. ' MaxInt = high(NativeInt);',
  6180. 'type',
  6181. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6182. 'const',
  6183. ' a = low(TMyInt)+High(TMyInt);',
  6184. 'var',
  6185. ' i: TMyInt;',
  6186. 'begin',
  6187. ' i:=-MinInt;',
  6188. ' i:=default(TMyInt);',
  6189. ' i:=low(i)+high(i);',
  6190. '']);
  6191. ConvertProgram;
  6192. CheckSource('TestIntegerRange',
  6193. LinesToStr([
  6194. 'this.MinInt = -4503599627370496;',
  6195. 'this.MaxInt = 4503599627370495;',
  6196. 'this.a = -4503599627370496 + 4503599627370495;',
  6197. 'this.i = 0;',
  6198. '']),
  6199. LinesToStr([
  6200. '$mod.i = - -4503599627370496;',
  6201. '$mod.i = -4503599627370496;',
  6202. '$mod.i = -4503599627370496 + 4503599627370495;',
  6203. '']));
  6204. end;
  6205. procedure TTestModule.TestIntegerRange;
  6206. begin
  6207. StartProgram(false);
  6208. Add([
  6209. 'const',
  6210. ' MinInt = -1;',
  6211. ' MaxInt = +1;',
  6212. 'type',
  6213. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6214. ' TInt2 = 1..3;',
  6215. 'const',
  6216. ' a = low(TMyInt)+High(TMyInt);',
  6217. ' b = low(TInt2)+High(TInt2);',
  6218. ' s1 = [1];',
  6219. ' s2 = [1,2];',
  6220. ' s3 = [1..3];',
  6221. ' s4 = [low(shortint)..high(shortint)];',
  6222. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6223. ' s6 = 1 in s2;',
  6224. 'var',
  6225. ' i: TMyInt;',
  6226. ' i2: TInt2;',
  6227. 'begin',
  6228. ' i:=i2;',
  6229. ' i:=default(TMyInt);',
  6230. ' if i=i2 then ;']);
  6231. ConvertProgram;
  6232. CheckSource('TestIntegerRange',
  6233. LinesToStr([
  6234. 'this.MinInt = -1;',
  6235. 'this.MaxInt = +1;',
  6236. 'this.a = -1 + 1;',
  6237. 'this.b = 1 + 3;',
  6238. 'this.s1 = rtl.createSet(1);',
  6239. 'this.s2 = rtl.createSet(1, 2);',
  6240. 'this.s3 = rtl.createSet(null, 1, 3);',
  6241. 'this.s4 = rtl.createSet(null, -128, 127);',
  6242. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6243. 'this.s6 = 1 in $mod.s2;',
  6244. 'this.i = 0;',
  6245. 'this.i2 = 0;',
  6246. '']),
  6247. LinesToStr([
  6248. '$mod.i = $mod.i2;',
  6249. '$mod.i = -1;',
  6250. 'if ($mod.i === $mod.i2) ;',
  6251. '']));
  6252. end;
  6253. procedure TTestModule.TestIntegerTypecasts;
  6254. begin
  6255. StartProgram(false);
  6256. Add([
  6257. 'var',
  6258. ' i: nativeint;',
  6259. ' b: byte;',
  6260. ' sh: shortint;',
  6261. ' w: word;',
  6262. ' sm: smallint;',
  6263. ' lw: longword;',
  6264. ' li: longint;',
  6265. 'begin',
  6266. ' b:=byte(i);',
  6267. ' sh:=shortint(i);',
  6268. ' w:=word(i);',
  6269. ' sm:=smallint(i);',
  6270. ' lw:=longword(i);',
  6271. ' li:=longint(i);',
  6272. '']);
  6273. ConvertProgram;
  6274. CheckSource('TestIntegerTypecasts',
  6275. LinesToStr([
  6276. 'this.i = 0;',
  6277. 'this.b = 0;',
  6278. 'this.sh = 0;',
  6279. 'this.w = 0;',
  6280. 'this.sm = 0;',
  6281. 'this.lw = 0;',
  6282. 'this.li = 0;',
  6283. '']),
  6284. LinesToStr([
  6285. '$mod.b = $mod.i & 255;',
  6286. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6287. '$mod.w = $mod.i & 65535;',
  6288. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6289. '$mod.lw = $mod.i >>> 0;',
  6290. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6291. '']));
  6292. end;
  6293. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6294. begin
  6295. StartProgram(false);
  6296. Add([
  6297. 'var',
  6298. ' i,j: nativeint;',
  6299. 'begin',
  6300. ' i:=i shr 0;',
  6301. ' i:=i shr 1;',
  6302. ' i:=i shr 3;',
  6303. ' i:=i shr 54;',
  6304. ' i:=j shr i;',
  6305. '']);
  6306. ConvertProgram;
  6307. CheckResolverUnexpectedHints;
  6308. CheckSource('TestInteger_BitwiseShrNativeInt',
  6309. LinesToStr([
  6310. 'this.i = 0;',
  6311. 'this.j = 0;',
  6312. '']),
  6313. LinesToStr([
  6314. '$mod.i = $mod.i;',
  6315. '$mod.i = Math.floor($mod.i / 2);',
  6316. '$mod.i = Math.floor($mod.i / 8);',
  6317. '$mod.i = 0;',
  6318. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6319. '']));
  6320. end;
  6321. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6322. begin
  6323. StartProgram(false);
  6324. Add([
  6325. 'var',
  6326. ' i: nativeint;',
  6327. 'begin',
  6328. ' i:=i shl 0;',
  6329. ' i:=i shl 54;',
  6330. ' i:=123456789012 shl 1;',
  6331. ' i:=i shl 1;',
  6332. '']);
  6333. ConvertProgram;
  6334. CheckResolverUnexpectedHints;
  6335. CheckSource('TestInteger_BitwiseShrNativeInt',
  6336. LinesToStr([
  6337. 'this.i = 0;',
  6338. '']),
  6339. LinesToStr([
  6340. '$mod.i = $mod.i;',
  6341. '$mod.i = 0;',
  6342. '$mod.i = 246913578024;',
  6343. '$mod.i = rtl.shl($mod.i, 1);',
  6344. '']));
  6345. end;
  6346. procedure TTestModule.TestCurrency;
  6347. begin
  6348. StartProgram(false);
  6349. Add([
  6350. 'type',
  6351. ' TCoin = currency;',
  6352. 'const',
  6353. ' a = TCoin(2.7);',
  6354. ' b = a + TCoin(1.7);',
  6355. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6356. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6357. 'var',
  6358. ' c: TCoin = b;',
  6359. ' i: nativeint;',
  6360. ' d: double;',
  6361. ' j: jsvalue;',
  6362. 'function DoIt(c: currency): currency; begin end;',
  6363. 'function GetIt(d: double): double; begin end;',
  6364. 'procedure Write(v: jsvalue); begin end;',
  6365. 'begin',
  6366. ' c:=1.0;',
  6367. ' c:=0.1;',
  6368. ' c:=1.0/3.0;',
  6369. ' c:=1/3;',
  6370. ' c:=a;',
  6371. ' d:=c;',
  6372. ' c:=d;',
  6373. ' c:=currency(c);',
  6374. ' c:=currency(d);',
  6375. ' d:=double(c);',
  6376. ' c:=i;',
  6377. ' c:=currency(i);',
  6378. //' i:=c;', not allowed
  6379. ' i:=nativeint(c);',
  6380. ' c:=c+a;',
  6381. ' c:=-c-a;',
  6382. ' c:=d+c;',
  6383. ' c:=c+d;',
  6384. ' c:=d-c;',
  6385. ' c:=c-d;',
  6386. ' c:=c*a;',
  6387. ' c:=a*c;',
  6388. ' c:=d*c;',
  6389. ' c:=c*d;',
  6390. ' c:=c/a;',
  6391. ' c:=a/c;',
  6392. ' c:=d/c;',
  6393. ' c:=c/d;',
  6394. ' c:=c**a;',
  6395. ' c:=a**c;',
  6396. ' c:=d**c;',
  6397. ' c:=c**d;',
  6398. ' if c=c then ;',
  6399. ' if c=a then ;',
  6400. ' if a=c then ;',
  6401. ' if d=c then ;',
  6402. ' if c=d then ;',
  6403. ' c:=DoIt(c);',
  6404. ' c:=DoIt(i);',
  6405. ' c:=DoIt(d);',
  6406. ' c:=GetIt(c);',
  6407. ' j:=c;',
  6408. ' Write(c);',
  6409. ' c:=default(currency);',
  6410. ' j:=str(c);',
  6411. ' j:=str(c:0:3);',
  6412. '']);
  6413. ConvertProgram;
  6414. CheckSource('TestCurrency',
  6415. LinesToStr([
  6416. 'this.a = 27000;',
  6417. 'this.b = $mod.a + 17000;',
  6418. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6419. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6420. 'this.c = $mod.b;',
  6421. 'this.i = 0;',
  6422. 'this.d = 0.0;',
  6423. 'this.j = undefined;',
  6424. 'this.DoIt = function (c) {',
  6425. ' var Result = 0;',
  6426. ' return Result;',
  6427. '};',
  6428. 'this.GetIt = function (d) {',
  6429. ' var Result = 0.0;',
  6430. ' return Result;',
  6431. '};',
  6432. 'this.Write = function (v) {',
  6433. '};',
  6434. '']),
  6435. LinesToStr([
  6436. '$mod.c = 10000;',
  6437. '$mod.c = 1000;',
  6438. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6439. '$mod.c = Math.floor((1 / 3) * 10000);',
  6440. '$mod.c = $mod.a;',
  6441. '$mod.d = $mod.c / 10000;',
  6442. '$mod.c = Math.floor($mod.d * 10000);',
  6443. '$mod.c = $mod.c;',
  6444. '$mod.c = $mod.d * 10000;',
  6445. '$mod.d = $mod.c / 10000;',
  6446. '$mod.c = $mod.i * 10000;',
  6447. '$mod.c = $mod.i * 10000;',
  6448. '$mod.i = Math.floor($mod.c / 10000);',
  6449. '$mod.c = $mod.c + $mod.a;',
  6450. '$mod.c = -$mod.c - $mod.a;',
  6451. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6452. '$mod.c = $mod.c + ($mod.d * 10000);',
  6453. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6454. '$mod.c = $mod.c - ($mod.d * 10000);',
  6455. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6456. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6457. '$mod.c = $mod.d * $mod.c;',
  6458. '$mod.c = $mod.c * $mod.d;',
  6459. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6460. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6461. '$mod.c = Math.floor($mod.d / $mod.c);',
  6462. '$mod.c = Math.floor($mod.c / $mod.d);',
  6463. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6464. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6465. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6466. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6467. 'if ($mod.c === $mod.c) ;',
  6468. 'if ($mod.c === $mod.a) ;',
  6469. 'if ($mod.a === $mod.c) ;',
  6470. 'if (($mod.d * 10000) === $mod.c) ;',
  6471. 'if ($mod.c === ($mod.d * 10000)) ;',
  6472. '$mod.c = $mod.DoIt($mod.c);',
  6473. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6474. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6475. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6476. '$mod.j = $mod.c / 10000;',
  6477. '$mod.Write($mod.c / 10000);',
  6478. '$mod.c = 0;',
  6479. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6480. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6481. '']));
  6482. end;
  6483. procedure TTestModule.TestForBoolDo;
  6484. begin
  6485. StartProgram(false);
  6486. Add([
  6487. 'var b: boolean;',
  6488. 'begin',
  6489. ' for b:=false to true do ;',
  6490. ' for b:=b downto false do ;',
  6491. ' for b in boolean do ;',
  6492. '']);
  6493. ConvertProgram;
  6494. CheckSource('TestForBoolDo',
  6495. LinesToStr([ // statements
  6496. 'this.b = false;']),
  6497. LinesToStr([ // this.$main
  6498. 'for (var $l1 = 0; $l1 <= 1; $l1++) $mod.b = $l1 !== 0;',
  6499. 'for (var $l2 = +$mod.b; $l2 >= 0; $l2--) $mod.b = $l2 !== 0;',
  6500. 'for (var $l3 = 0; $l3 <= 1; $l3++) $mod.b = $l3 !== 0;',
  6501. '']));
  6502. end;
  6503. procedure TTestModule.TestForIntDo;
  6504. begin
  6505. StartProgram(false);
  6506. Add([
  6507. 'var i: longint;',
  6508. 'begin',
  6509. ' for i:=3 to 5 do ;',
  6510. ' for i:=i downto 2 do ;',
  6511. ' for i in byte do ;',
  6512. '']);
  6513. ConvertProgram;
  6514. CheckSource('TestForIntDo',
  6515. LinesToStr([ // statements
  6516. 'this.i = 0;']),
  6517. LinesToStr([ // this.$main
  6518. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6519. 'for (var $l1 = $mod.i; $l1 >= 2; $l1--) $mod.i = $l1;',
  6520. 'for (var $l2 = 0; $l2 <= 255; $l2++) $mod.i = $l2;',
  6521. '']));
  6522. end;
  6523. procedure TTestModule.TestForIntInDo;
  6524. begin
  6525. StartProgram(false);
  6526. Add([
  6527. 'type',
  6528. ' TSetOfInt = set of byte;',
  6529. ' TIntRg = 3..7;',
  6530. ' TSetOfIntRg = set of TIntRg;',
  6531. 'var',
  6532. ' i,i2: longint;',
  6533. ' a1: array of byte;',
  6534. ' a2: array[1..3] of byte;',
  6535. ' soi: TSetOfInt;',
  6536. ' soir: TSetOfIntRg;',
  6537. ' ir: TIntRg;',
  6538. 'begin',
  6539. ' for i in byte do ;',
  6540. ' for i in a1 do ;',
  6541. ' for i in a2 do ;',
  6542. ' for i in [11..13] do ;',
  6543. ' for i in TSetOfInt do ;',
  6544. ' for i in TIntRg do ;',
  6545. ' for i in soi do i2:=i;',
  6546. ' for i in TSetOfIntRg do ;',
  6547. ' for i in soir do ;',
  6548. ' for ir in TIntRg do ;',
  6549. ' for ir in TSetOfIntRg do ;',
  6550. ' for ir in soir do ;',
  6551. '']);
  6552. ConvertProgram;
  6553. CheckSource('TestForIntInDo',
  6554. LinesToStr([ // statements
  6555. 'this.i = 0;',
  6556. 'this.i2 = 0;',
  6557. 'this.a1 = [];',
  6558. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6559. 'this.soi = {};',
  6560. 'this.soir = {};',
  6561. 'this.ir = 0;',
  6562. '']),
  6563. LinesToStr([ // this.$main
  6564. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6565. 'for (var $in2 = $mod.a1, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) $mod.i = $in2[$l3];',
  6566. 'for (var $in5 = $mod.a2, $l6 = 0, $end7 = rtl.length($in5) - 1; $l6 <= $end7; $l6++) $mod.i = $in5[$l6];',
  6567. 'for (var $l8 = 11; $l8 <= 13; $l8++) $mod.i = $l8;',
  6568. 'for (var $l9 = 0; $l9 <= 255; $l9++) $mod.i = $l9;',
  6569. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.i = $l10;',
  6570. 'for (var $l11 in $mod.soi) {',
  6571. ' $mod.i = +$l11;',
  6572. ' $mod.i2 = $mod.i;',
  6573. '};',
  6574. 'for (var $l12 = 3; $l12 <= 7; $l12++) $mod.i = $l12;',
  6575. 'for (var $l13 in $mod.soir) $mod.i = +$l13;',
  6576. 'for (var $l14 = 3; $l14 <= 7; $l14++) $mod.ir = $l14;',
  6577. 'for (var $l15 = 3; $l15 <= 7; $l15++) $mod.ir = $l15;',
  6578. 'for (var $l16 in $mod.soir) $mod.ir = +$l16;',
  6579. '']));
  6580. end;
  6581. procedure TTestModule.TestCharConst;
  6582. begin
  6583. StartProgram(false);
  6584. Add([
  6585. 'const',
  6586. ' a = #$00F3;',
  6587. ' c: char = ''1'';',
  6588. 'begin',
  6589. ' c:=#0;',
  6590. ' c:=#1;',
  6591. ' c:=#9;',
  6592. ' c:=#10;',
  6593. ' c:=#13;',
  6594. ' c:=#31;',
  6595. ' c:=#32;',
  6596. ' c:=#$A;',
  6597. ' c:=#$0A;',
  6598. ' c:=#$b;',
  6599. ' c:=#$0b;',
  6600. ' c:=^A;',
  6601. ' c:=''"'';',
  6602. ' c:=default(char);',
  6603. ' c:=#$00E4;', // ä
  6604. ' c:=''ä'';',
  6605. ' c:=#$E4;', // ä
  6606. ' c:=#$D800;', // invalid UTF-16
  6607. ' c:=#$DFFF;', // invalid UTF-16
  6608. ' c:=#$FFFF;', // last UCS-2
  6609. ' c:=high(c);', // last UCS-2
  6610. '']);
  6611. ConvertProgram;
  6612. CheckSource('TestCharConst',
  6613. LinesToStr([
  6614. 'this.a="ó";',
  6615. 'this.c="1";'
  6616. ]),
  6617. LinesToStr([
  6618. '$mod.c="\x00";',
  6619. '$mod.c="\x01";',
  6620. '$mod.c="\t";',
  6621. '$mod.c="\n";',
  6622. '$mod.c="\r";',
  6623. '$mod.c="\x1F";',
  6624. '$mod.c=" ";',
  6625. '$mod.c="\n";',
  6626. '$mod.c="\n";',
  6627. '$mod.c="\x0B";',
  6628. '$mod.c="\x0B";',
  6629. '$mod.c="\x01";',
  6630. '$mod.c=''"'';',
  6631. '$mod.c="\x00";',
  6632. '$mod.c = "ä";',
  6633. '$mod.c = "ä";',
  6634. '$mod.c = "ä";',
  6635. '$mod.c="\uD800";',
  6636. '$mod.c="\uDFFF";',
  6637. '$mod.c="\uFFFF";',
  6638. '$mod.c="\uFFFF";',
  6639. '']));
  6640. end;
  6641. procedure TTestModule.TestChar_Compare;
  6642. begin
  6643. StartProgram(false);
  6644. Add('var');
  6645. Add(' c: char;');
  6646. Add(' b: boolean;');
  6647. Add('begin');
  6648. Add(' b:=c=''1'';');
  6649. Add(' b:=''2''=c;');
  6650. Add(' b:=''3''=''4'';');
  6651. Add(' b:=c<>''5'';');
  6652. Add(' b:=''6''<>c;');
  6653. Add(' b:=c>''7'';');
  6654. Add(' b:=''8''>c;');
  6655. Add(' b:=c>=''9'';');
  6656. Add(' b:=''A''>=c;');
  6657. Add(' b:=c<''B'';');
  6658. Add(' b:=''C''<c;');
  6659. Add(' b:=c<=''D'';');
  6660. Add(' b:=''E''<=c;');
  6661. ConvertProgram;
  6662. CheckSource('TestChar_Compare',
  6663. LinesToStr([
  6664. 'this.c="";',
  6665. 'this.b = false;'
  6666. ]),
  6667. LinesToStr([
  6668. '$mod.b = $mod.c === "1";',
  6669. '$mod.b = "2" === $mod.c;',
  6670. '$mod.b = "3" === "4";',
  6671. '$mod.b = $mod.c !== "5";',
  6672. '$mod.b = "6" !== $mod.c;',
  6673. '$mod.b = $mod.c > "7";',
  6674. '$mod.b = "8" > $mod.c;',
  6675. '$mod.b = $mod.c >= "9";',
  6676. '$mod.b = "A" >= $mod.c;',
  6677. '$mod.b = $mod.c < "B";',
  6678. '$mod.b = "C" < $mod.c;',
  6679. '$mod.b = $mod.c <= "D";',
  6680. '$mod.b = "E" <= $mod.c;',
  6681. '']));
  6682. end;
  6683. procedure TTestModule.TestChar_BuiltInProcs;
  6684. begin
  6685. StartProgram(false);
  6686. Add([
  6687. 'var',
  6688. ' c: char;',
  6689. ' i: longint;',
  6690. ' s: string;',
  6691. 'begin',
  6692. ' i:=ord(c);',
  6693. ' i:=ord(s[i]);',
  6694. ' c:=chr(i);',
  6695. ' c:=pred(c);',
  6696. ' c:=succ(c);',
  6697. ' c:=low(c);',
  6698. ' c:=high(c);',
  6699. ' i:=byte(c);',
  6700. ' i:=word(c);',
  6701. ' i:=longint(c);',
  6702. '']);
  6703. ConvertProgram;
  6704. CheckSource('TestChar_BuiltInProcs',
  6705. LinesToStr([
  6706. 'this.c = "";',
  6707. 'this.i = 0;',
  6708. 'this.s = "";'
  6709. ]),
  6710. LinesToStr([
  6711. '$mod.i = $mod.c.charCodeAt();',
  6712. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  6713. '$mod.c = String.fromCharCode($mod.i);',
  6714. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  6715. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  6716. '$mod.c = "\x00";',
  6717. '$mod.c = "\uFFFF";',
  6718. '$mod.i = $mod.c.charCodeAt() & 255;',
  6719. '$mod.i = $mod.c.charCodeAt();',
  6720. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  6721. '']));
  6722. end;
  6723. procedure TTestModule.TestStringConst;
  6724. begin
  6725. StartProgram(false);
  6726. Add([
  6727. '{$H+}',
  6728. 'const',
  6729. ' a = #$00F3#$017C;', // first <256, then >=256
  6730. ' b = string(''a'');',
  6731. ' c = string(''ä'');',
  6732. ' d = UnicodeString(''b'');',
  6733. ' e = UnicodeString(''ö'');',
  6734. 'var',
  6735. ' s: string = ''abc'';',
  6736. 'begin',
  6737. ' s:='''';',
  6738. ' s:=#13#10;',
  6739. ' s:=#9''foo'';',
  6740. ' s:=#$A9;',
  6741. ' s:=''foo''#13''bar'';',
  6742. ' s:=''"'';',
  6743. ' s:=''"''''"'';',
  6744. ' s:=#$20AC;', // euro
  6745. ' s:=#$10437;', // outside BMP
  6746. ' s:=default(string);',
  6747. ' s:=concat(s);',
  6748. ' s:=concat(s,''a'',s)',
  6749. '']);
  6750. ConvertProgram;
  6751. CheckSource('TestStringConst',
  6752. LinesToStr([
  6753. 'this.a = "óż";',
  6754. 'this.b = "a";',
  6755. 'this.c = "ä";',
  6756. 'this.d = "b";',
  6757. 'this.e = "ö";',
  6758. 'this.s="abc";',
  6759. '']),
  6760. LinesToStr([
  6761. '$mod.s="";',
  6762. '$mod.s="\r\n";',
  6763. '$mod.s="\tfoo";',
  6764. '$mod.s="©";',
  6765. '$mod.s="foo\rbar";',
  6766. '$mod.s=''"'';',
  6767. '$mod.s=''"\''"'';',
  6768. '$mod.s="€";',
  6769. '$mod.s="'#$F0#$90#$90#$B7'";',
  6770. '$mod.s="";',
  6771. '$mod.s = $mod.s;',
  6772. '$mod.s = $mod.s.concat("a", $mod.s);',
  6773. '']));
  6774. end;
  6775. procedure TTestModule.TestStringConstSurrogate;
  6776. begin
  6777. StartProgram(false);
  6778. Add([
  6779. 'var',
  6780. ' s: string;',
  6781. 'begin',
  6782. ' s:=''😊'';', // 1F60A
  6783. '']);
  6784. ConvertProgram;
  6785. CheckSource('TestStringConstSurrogate',
  6786. LinesToStr([
  6787. 'this.s="";'
  6788. ]),
  6789. LinesToStr([
  6790. '$mod.s="😊";'
  6791. ]));
  6792. end;
  6793. procedure TTestModule.TestString_Length;
  6794. begin
  6795. StartProgram(false);
  6796. Add('const c = ''foo'';');
  6797. Add('var');
  6798. Add(' s: string;');
  6799. Add(' i: longint;');
  6800. Add('begin');
  6801. Add(' i:=length(s);');
  6802. Add(' i:=length(s+s);');
  6803. Add(' i:=length(''abc'');');
  6804. Add(' i:=length(c);');
  6805. ConvertProgram;
  6806. CheckSource('TestString_Length',
  6807. LinesToStr([
  6808. 'this.c = "foo";',
  6809. 'this.s = "";',
  6810. 'this.i = 0;',
  6811. '']),
  6812. LinesToStr([
  6813. '$mod.i = $mod.s.length;',
  6814. '$mod.i = ($mod.s+$mod.s).length;',
  6815. '$mod.i = "abc".length;',
  6816. '$mod.i = $mod.c.length;',
  6817. '']));
  6818. end;
  6819. procedure TTestModule.TestString_Compare;
  6820. begin
  6821. StartProgram(false);
  6822. Add('var');
  6823. Add(' s, t: string;');
  6824. Add(' b: boolean;');
  6825. Add('begin');
  6826. Add(' b:=s=t;');
  6827. Add(' b:=s<>t;');
  6828. Add(' b:=s>t;');
  6829. Add(' b:=s>=t;');
  6830. Add(' b:=s<t;');
  6831. Add(' b:=s<=t;');
  6832. ConvertProgram;
  6833. CheckSource('TestString_Compare',
  6834. LinesToStr([ // statements
  6835. 'this.s = "";',
  6836. 'this.t = "";',
  6837. 'this.b =false;'
  6838. ]),
  6839. LinesToStr([ // this.$main
  6840. '$mod.b = $mod.s === $mod.t;',
  6841. '$mod.b = $mod.s !== $mod.t;',
  6842. '$mod.b = $mod.s > $mod.t;',
  6843. '$mod.b = $mod.s >= $mod.t;',
  6844. '$mod.b = $mod.s < $mod.t;',
  6845. '$mod.b = $mod.s <= $mod.t;',
  6846. '']));
  6847. end;
  6848. procedure TTestModule.TestString_SetLength;
  6849. begin
  6850. StartProgram(false);
  6851. Add([
  6852. 'procedure DoIt(var s: string);',
  6853. 'begin',
  6854. ' SetLength(s,2);',
  6855. 'end;',
  6856. 'var s: string;',
  6857. 'begin',
  6858. ' SetLength(s,3);',
  6859. '']);
  6860. ConvertProgram;
  6861. CheckSource('TestString_SetLength',
  6862. LinesToStr([ // statements
  6863. 'this.DoIt = function (s) {',
  6864. ' s.set(rtl.strSetLength(s.get(), 2));',
  6865. '};',
  6866. 'this.s = "";',
  6867. '']),
  6868. LinesToStr([ // this.$main
  6869. '$mod.s = rtl.strSetLength($mod.s, 3);'
  6870. ]));
  6871. end;
  6872. procedure TTestModule.TestString_CharAt;
  6873. begin
  6874. StartProgram(false);
  6875. Add([
  6876. 'var',
  6877. ' s: string;',
  6878. ' c: char;',
  6879. ' b: boolean;',
  6880. 'begin',
  6881. ' b:= s[1] = c;',
  6882. ' b:= c = s[1];',
  6883. ' b:= c <> s[1];',
  6884. ' b:= c > s[1];',
  6885. ' b:= c >= s[1];',
  6886. ' b:= c < s[2];',
  6887. ' b:= c <= s[1];',
  6888. ' s[1] := c;',
  6889. ' s[2+3] := c;']);
  6890. ConvertProgram;
  6891. CheckSource('TestString_CharAt',
  6892. LinesToStr([ // statements
  6893. 'this.s = "";',
  6894. 'this.c = "";',
  6895. 'this.b = false;'
  6896. ]),
  6897. LinesToStr([ // this.$main
  6898. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  6899. '$mod.b = $mod.c === $mod.s.charAt(0);',
  6900. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  6901. '$mod.b = $mod.c > $mod.s.charAt(0);',
  6902. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  6903. '$mod.b = $mod.c < $mod.s.charAt(1);',
  6904. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  6905. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  6906. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  6907. '']));
  6908. end;
  6909. procedure TTestModule.TestStringHMinusFail;
  6910. begin
  6911. StartProgram(false);
  6912. Add([
  6913. '{$H-}',
  6914. 'var s: string;',
  6915. 'begin']);
  6916. ConvertProgram;
  6917. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  6918. end;
  6919. procedure TTestModule.TestStr;
  6920. begin
  6921. StartProgram(false);
  6922. Add('var');
  6923. Add(' b: boolean;');
  6924. Add(' i: longint;');
  6925. Add(' d: double;');
  6926. Add(' s: string;');
  6927. Add('begin');
  6928. Add(' str(b,s);');
  6929. Add(' str(i,s);');
  6930. Add(' str(d,s);');
  6931. Add(' str(i:3,s);');
  6932. Add(' str(d:3:2,s);');
  6933. Add(' Str(12.456:12:1,s);');
  6934. Add(' Str(12.456:12,s);');
  6935. Add(' s:=str(b);');
  6936. Add(' s:=str(i);');
  6937. Add(' s:=str(d);');
  6938. Add(' s:=str(i,i);');
  6939. Add(' s:=str(i:3);');
  6940. Add(' s:=str(d:3:2);');
  6941. Add(' s:=str(i:4,i);');
  6942. Add(' s:=str(i,i:5);');
  6943. Add(' s:=str(i:4,i:5);');
  6944. Add(' s:=str(s,s);');
  6945. Add(' s:=str(s,''foo'');');
  6946. ConvertProgram;
  6947. CheckSource('TestStr',
  6948. LinesToStr([ // statements
  6949. 'this.b = false;',
  6950. 'this.i = 0;',
  6951. 'this.d = 0.0;',
  6952. 'this.s = "";',
  6953. '']),
  6954. LinesToStr([ // this.$main
  6955. '$mod.s = ""+$mod.b;',
  6956. '$mod.s = ""+$mod.i;',
  6957. '$mod.s = rtl.floatToStr($mod.d);',
  6958. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  6959. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  6960. '$mod.s = rtl.floatToStr(12.456,12,1);',
  6961. '$mod.s = rtl.floatToStr(12.456,12);',
  6962. '$mod.s = ""+$mod.b;',
  6963. '$mod.s = ""+$mod.i;',
  6964. '$mod.s = rtl.floatToStr($mod.d);',
  6965. '$mod.s = ""+$mod.i+$mod.i;',
  6966. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  6967. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  6968. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  6969. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  6970. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  6971. '$mod.s = $mod.s + $mod.s;',
  6972. '$mod.s = $mod.s + "foo";',
  6973. '']));
  6974. end;
  6975. procedure TTestModule.TestBaseType_AnsiStringFail;
  6976. begin
  6977. StartProgram(false);
  6978. Add('var s: AnsiString');
  6979. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  6980. ConvertProgram;
  6981. end;
  6982. procedure TTestModule.TestBaseType_WideStringFail;
  6983. begin
  6984. StartProgram(false);
  6985. Add('var s: WideString');
  6986. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  6987. ConvertProgram;
  6988. end;
  6989. procedure TTestModule.TestBaseType_ShortStringFail;
  6990. begin
  6991. StartProgram(false);
  6992. Add('var s: ShortString');
  6993. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  6994. ConvertProgram;
  6995. end;
  6996. procedure TTestModule.TestBaseType_RawByteStringFail;
  6997. begin
  6998. StartProgram(false);
  6999. Add('var s: RawByteString');
  7000. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7001. ConvertProgram;
  7002. end;
  7003. procedure TTestModule.TestTypeShortstring_Fail;
  7004. begin
  7005. StartProgram(false);
  7006. Add('type t = string[12];');
  7007. Add('var s: t;');
  7008. Add('begin');
  7009. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7010. ConvertProgram;
  7011. end;
  7012. procedure TTestModule.TestCharSet_Custom;
  7013. begin
  7014. StartProgram(false);
  7015. Add([
  7016. 'type',
  7017. ' TCharRg = ''a''..''z'';',
  7018. ' TSetOfCharRg = set of TCharRg;',
  7019. ' TCharRg2 = ''m''..''p'';',
  7020. 'const',
  7021. ' crg: TCharRg = ''b'';',
  7022. 'var',
  7023. ' c: char;',
  7024. ' crg2: TCharRg2;',
  7025. ' s: TSetOfCharRg;',
  7026. 'begin',
  7027. ' c:=crg;',
  7028. ' crg:=c;',
  7029. ' crg2:=crg;',
  7030. ' if c=crg then ;',
  7031. ' if crg=c then ;',
  7032. ' if crg=crg2 then ;',
  7033. ' if c in s then ;',
  7034. ' if crg2 in s then ;',
  7035. ' c:=default(TCharRg);',
  7036. '']);
  7037. ConvertProgram;
  7038. CheckSource('TestCharSet_Custom',
  7039. LinesToStr([ // statements
  7040. 'this.crg = "b";',
  7041. 'this.c = "";',
  7042. 'this.crg2 = "m";',
  7043. 'this.s = {};',
  7044. '']),
  7045. LinesToStr([ // this.$main
  7046. '$mod.c = $mod.crg;',
  7047. '$mod.crg = $mod.c;',
  7048. '$mod.crg2 = $mod.crg;',
  7049. 'if ($mod.c === $mod.crg) ;',
  7050. 'if ($mod.crg === $mod.c) ;',
  7051. 'if ($mod.crg === $mod.crg2) ;',
  7052. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7053. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7054. '$mod.c = "a";',
  7055. '']));
  7056. end;
  7057. procedure TTestModule.TestForCharDo;
  7058. begin
  7059. StartProgram(false);
  7060. Add([
  7061. 'var c: char;',
  7062. 'begin',
  7063. ' for c:=''a'' to ''c'' do ;',
  7064. ' for c:=c downto ''a'' do ;',
  7065. ' for c:=''Б'' to ''Я'' do ;',
  7066. '']);
  7067. ConvertProgram;
  7068. CheckSource('TestForCharDo',
  7069. LinesToStr([ // statements
  7070. 'this.c = "";']),
  7071. LinesToStr([ // this.$main
  7072. 'for (var $l1 = 97; $l1 <= 99; $l1++) $mod.c = String.fromCharCode($l1);',
  7073. 'for (var $l2 = $mod.c.charCodeAt(); $l2 >= 97; $l2--) $mod.c = String.fromCharCode($l2);',
  7074. 'for (var $l3 = 1041; $l3 <= 1071; $l3++) $mod.c = String.fromCharCode($l3);',
  7075. '']));
  7076. end;
  7077. procedure TTestModule.TestForCharInDo;
  7078. begin
  7079. StartProgram(false);
  7080. Add([
  7081. 'type',
  7082. ' TSetOfChar = set of char;',
  7083. ' TCharRg = ''a''..''z'';',
  7084. ' TSetOfCharRg = set of TCharRg;',
  7085. 'const Foo = ''foo'';',
  7086. 'var',
  7087. ' c,c2: char;',
  7088. ' s: string;',
  7089. ' a1: array of char;',
  7090. ' a2: array[1..3] of char;',
  7091. ' soc: TSetOfChar;',
  7092. ' socr: TSetOfCharRg;',
  7093. ' cr: TCharRg;',
  7094. 'begin',
  7095. ' for c in foo do ;',
  7096. ' for c in s do ;',
  7097. ' for c in char do ;',
  7098. ' for c in a1 do ;',
  7099. ' for c in a2 do ;',
  7100. ' for c in [''1''..''3''] do ;',
  7101. ' for c in TSetOfChar do ;',
  7102. ' for c in TCharRg do ;',
  7103. ' for c in soc do c2:=c;',
  7104. ' for c in TSetOfCharRg do ;',
  7105. ' for c in socr do ;',
  7106. ' for cr in TCharRg do ;',
  7107. ' for cr in TSetOfCharRg do ;',
  7108. ' for cr in socr do ;',
  7109. '']);
  7110. ConvertProgram;
  7111. CheckSource('TestForCharInDo',
  7112. LinesToStr([ // statements
  7113. 'this.Foo = "foo";',
  7114. 'this.c = "";',
  7115. 'this.c2 = "";',
  7116. 'this.s = "";',
  7117. 'this.a1 = [];',
  7118. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7119. 'this.soc = {};',
  7120. 'this.socr = {};',
  7121. 'this.cr = "a";',
  7122. '']),
  7123. LinesToStr([ // this.$main
  7124. 'for (var $in1 = $mod.Foo, $l2 = 0, $end3 = $in1.length - 1; $l2 <= $end3; $l2++) $mod.c = $in1.charAt($l2);',
  7125. 'for (var $in4 = $mod.s, $l5 = 0, $end6 = $in4.length - 1; $l5 <= $end6; $l5++) $mod.c = $in4.charAt($l5);',
  7126. 'for (var $l7 = 0; $l7 <= 65535; $l7++) $mod.c = String.fromCharCode($l7);',
  7127. 'for (var $in8 = $mod.a1, $l9 = 0, $end10 = rtl.length($in8) - 1; $l9 <= $end10; $l9++) $mod.c = $in8[$l9];',
  7128. 'for (var $in11 = $mod.a2, $l12 = 0, $end13 = rtl.length($in11) - 1; $l12 <= $end13; $l12++) $mod.c = $in11[$l12];',
  7129. 'for (var $l14 = 49; $l14 <= 51; $l14++) $mod.c = String.fromCharCode($l14);',
  7130. 'for (var $l15 = 0; $l15 <= 65535; $l15++) $mod.c = String.fromCharCode($l15);',
  7131. 'for (var $l16 = 97; $l16 <= 122; $l16++) $mod.c = String.fromCharCode($l16);',
  7132. 'for (var $l17 in $mod.soc) {',
  7133. ' $mod.c = String.fromCharCode($l17);',
  7134. ' $mod.c2 = $mod.c;',
  7135. '};',
  7136. 'for (var $l18 = 97; $l18 <= 122; $l18++) $mod.c = String.fromCharCode($l18);',
  7137. 'for (var $l19 in $mod.socr) $mod.c = String.fromCharCode($l19);',
  7138. 'for (var $l20 = 97; $l20 <= 122; $l20++) $mod.cr = String.fromCharCode($l20);',
  7139. 'for (var $l21 = 97; $l21 <= 122; $l21++) $mod.cr = String.fromCharCode($l21);',
  7140. 'for (var $l22 in $mod.socr) $mod.cr = String.fromCharCode($l22);',
  7141. '']));
  7142. end;
  7143. procedure TTestModule.TestProcTwoArgs;
  7144. begin
  7145. StartProgram(false);
  7146. Add('procedure Test(a,b: longint);');
  7147. Add('begin');
  7148. Add('end;');
  7149. Add('begin');
  7150. ConvertProgram;
  7151. CheckSource('TestProcTwoArgs',
  7152. LinesToStr([ // statements
  7153. 'this.Test = function (a,b) {',
  7154. '};'
  7155. ]),
  7156. LinesToStr([ // this.$main
  7157. ''
  7158. ]));
  7159. end;
  7160. procedure TTestModule.TestProc_DefaultValue;
  7161. begin
  7162. StartProgram(false);
  7163. Add('procedure p1(i: longint = 1);');
  7164. Add('begin');
  7165. Add('end;');
  7166. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7167. Add('begin');
  7168. Add('end;');
  7169. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7170. Add('begin');
  7171. Add('end;');
  7172. Add('begin');
  7173. Add(' p1;');
  7174. Add(' p1();');
  7175. Add(' p1(11);');
  7176. Add(' p2;');
  7177. Add(' p2();');
  7178. Add(' p2(12);');
  7179. Add(' p2(13,''b'');');
  7180. Add(' p3();');
  7181. ConvertProgram;
  7182. CheckSource('TestProc_DefaultValue',
  7183. LinesToStr([ // statements
  7184. 'this.p1 = function (i) {',
  7185. '};',
  7186. 'this.p2 = function (i,c) {',
  7187. '};',
  7188. 'this.p3 = function (d,b,s) {',
  7189. '};'
  7190. ]),
  7191. LinesToStr([ // this.$main
  7192. ' $mod.p1(1);',
  7193. ' $mod.p1(1);',
  7194. ' $mod.p1(11);',
  7195. ' $mod.p2(1,"a");',
  7196. ' $mod.p2(1,"a");',
  7197. ' $mod.p2(12,"a");',
  7198. ' $mod.p2(13,"b");',
  7199. ' $mod.p3(1.0,false,"abc");'
  7200. ]));
  7201. end;
  7202. procedure TTestModule.TestFunctionInt;
  7203. begin
  7204. StartProgram(false);
  7205. Add('function MyTest(Bar: longint): longint;');
  7206. Add('begin');
  7207. Add(' Result:=2*bar');
  7208. Add('end;');
  7209. Add('begin');
  7210. ConvertProgram;
  7211. CheckSource('TestFunctionInt',
  7212. LinesToStr([ // statements
  7213. 'this.MyTest = function (Bar) {',
  7214. ' var Result = 0;',
  7215. ' Result = 2*Bar;',
  7216. ' return Result;',
  7217. '};'
  7218. ]),
  7219. LinesToStr([ // this.$main
  7220. ''
  7221. ]));
  7222. end;
  7223. procedure TTestModule.TestFunctionString;
  7224. begin
  7225. StartProgram(false);
  7226. Add('function Test(Bar: string): string;');
  7227. Add('begin');
  7228. Add(' Result:=bar+BAR');
  7229. Add('end;');
  7230. Add('begin');
  7231. ConvertProgram;
  7232. CheckSource('TestFunctionString',
  7233. LinesToStr([ // statements
  7234. 'this.Test = function (Bar) {',
  7235. ' var Result = "";',
  7236. ' Result = Bar+Bar;',
  7237. ' return Result;',
  7238. '};'
  7239. ]),
  7240. LinesToStr([ // this.$main
  7241. ''
  7242. ]));
  7243. end;
  7244. procedure TTestModule.TestIfThen;
  7245. begin
  7246. StartProgram(false);
  7247. Add([
  7248. 'var b: boolean;',
  7249. 'begin',
  7250. ' if b then ;',
  7251. ' if b then else ;']);
  7252. ConvertProgram;
  7253. CheckSource('TestIfThen',
  7254. LinesToStr([ // statements
  7255. 'this.b = false;',
  7256. '']),
  7257. LinesToStr([ // this.$main
  7258. 'if ($mod.b) ;',
  7259. 'if ($mod.b) ;',
  7260. '']));
  7261. end;
  7262. procedure TTestModule.TestForLoop;
  7263. begin
  7264. StartProgram(false);
  7265. Add('var');
  7266. Add(' vI, vJ, vN: longint;');
  7267. Add('begin');
  7268. Add(' VJ:=0;');
  7269. Add(' VN:=3;');
  7270. Add(' for VI:=1 to VN do');
  7271. Add(' begin');
  7272. Add(' VJ:=VJ+VI;');
  7273. Add(' end;');
  7274. ConvertProgram;
  7275. CheckSource('TestForLoop',
  7276. LinesToStr([ // statements
  7277. 'this.vI = 0;',
  7278. 'this.vJ = 0;',
  7279. 'this.vN = 0;'
  7280. ]),
  7281. LinesToStr([ // this.$main
  7282. ' $mod.vJ = 0;',
  7283. ' $mod.vN = 3;',
  7284. ' for (var $l1 = 1, $end2 = $mod.vN; $l1 <= $end2; $l1++) {',
  7285. ' $mod.vI = $l1;',
  7286. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7287. ' };',
  7288. '']));
  7289. end;
  7290. procedure TTestModule.TestForLoopInsideFunction;
  7291. begin
  7292. StartProgram(false);
  7293. Add('function SumNumbers(Count: longint): longint;');
  7294. Add('var');
  7295. Add(' vI, vJ: longint;');
  7296. Add('begin');
  7297. Add(' vj:=0;');
  7298. Add(' for vi:=1 to count do');
  7299. Add(' begin');
  7300. Add(' vj:=vj+vi;');
  7301. Add(' end;');
  7302. Add('end;');
  7303. Add('begin');
  7304. Add(' sumnumbers(3);');
  7305. ConvertProgram;
  7306. CheckSource('TestForLoopInsideFunction',
  7307. LinesToStr([ // statements
  7308. 'this.SumNumbers = function (Count) {',
  7309. ' var Result = 0;',
  7310. ' var vI = 0;',
  7311. ' var vJ = 0;',
  7312. ' vJ = 0;',
  7313. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7314. ' vI = $l1;',
  7315. ' vJ = vJ + vI;',
  7316. ' };',
  7317. ' return Result;',
  7318. '};'
  7319. ]),
  7320. LinesToStr([ // $mod.$main
  7321. ' $mod.SumNumbers(3);'
  7322. ]));
  7323. end;
  7324. procedure TTestModule.TestForLoop_ReadVarAfter;
  7325. begin
  7326. StartProgram(false);
  7327. Add('var');
  7328. Add(' vI: longint;');
  7329. Add('begin');
  7330. Add(' for vi:=1 to 2 do ;');
  7331. Add(' if vi=3 then ;');
  7332. ConvertProgram;
  7333. CheckSource('TestForLoop',
  7334. LinesToStr([ // statements
  7335. 'this.vI = 0;'
  7336. ]),
  7337. LinesToStr([ // this.$main
  7338. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7339. ' if ($mod.vI===3) ;'
  7340. ]));
  7341. end;
  7342. procedure TTestModule.TestForLoop_Nested;
  7343. begin
  7344. StartProgram(false);
  7345. Add('function SumNumbers(Count: longint): longint;');
  7346. Add('var');
  7347. Add(' vI, vJ, vK: longint;');
  7348. Add('begin');
  7349. Add(' VK:=0;');
  7350. Add(' for VI:=1 to count do');
  7351. Add(' begin');
  7352. Add(' for vj:=1 to vi do');
  7353. Add(' begin');
  7354. Add(' vk:=VK+VI;');
  7355. Add(' end;');
  7356. Add(' end;');
  7357. Add('end;');
  7358. Add('begin');
  7359. Add(' sumnumbers(3);');
  7360. ConvertProgram;
  7361. CheckSource('TestForLoopInFunction',
  7362. LinesToStr([ // statements
  7363. 'this.SumNumbers = function (Count) {',
  7364. ' var Result = 0;',
  7365. ' var vI = 0;',
  7366. ' var vJ = 0;',
  7367. ' var vK = 0;',
  7368. ' vK = 0;',
  7369. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7370. ' vI = $l1;',
  7371. ' for (var $l3 = 1, $end4 = vI; $l3 <= $end4; $l3++) {',
  7372. ' vJ = $l3;',
  7373. ' vK = vK + vI;',
  7374. ' };',
  7375. ' };',
  7376. ' return Result;',
  7377. '};'
  7378. ]),
  7379. LinesToStr([ // $mod.$main
  7380. ' $mod.SumNumbers(3);'
  7381. ]));
  7382. end;
  7383. procedure TTestModule.TestRepeatUntil;
  7384. begin
  7385. StartProgram(false);
  7386. Add('var');
  7387. Add(' vI, vJ, vN: longint;');
  7388. Add('begin');
  7389. Add(' vn:=3;');
  7390. Add(' vj:=0;');
  7391. Add(' VI:=0;');
  7392. Add(' repeat');
  7393. Add(' VI:=vi+1;');
  7394. Add(' vj:=VJ+vI;');
  7395. Add(' until vi>=vn');
  7396. ConvertProgram;
  7397. CheckSource('TestRepeatUntil',
  7398. LinesToStr([ // statements
  7399. 'this.vI = 0;',
  7400. 'this.vJ = 0;',
  7401. 'this.vN = 0;'
  7402. ]),
  7403. LinesToStr([ // $mod.$main
  7404. ' $mod.vN = 3;',
  7405. ' $mod.vJ = 0;',
  7406. ' $mod.vI = 0;',
  7407. ' do{',
  7408. ' $mod.vI = $mod.vI + 1;',
  7409. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7410. ' }while(!($mod.vI>=$mod.vN));'
  7411. ]));
  7412. end;
  7413. procedure TTestModule.TestAsmBlock;
  7414. begin
  7415. StartProgram(false);
  7416. Add([
  7417. 'var',
  7418. ' vI: longint;',
  7419. 'begin',
  7420. ' vi:=1;',
  7421. ' asm',
  7422. ' if (vI===1) {',
  7423. ' vI=2;',
  7424. //' console.log(''end;'');', ToDo
  7425. ' }',
  7426. ' if (vI===2){ vI=3; }',
  7427. ' end;',
  7428. ' VI:=4;']);
  7429. ConvertProgram;
  7430. CheckSource('TestAsmBlock',
  7431. LinesToStr([ // statements
  7432. 'this.vI = 0;'
  7433. ]),
  7434. LinesToStr([ // $mod.$main
  7435. '$mod.vI = 1;',
  7436. 'if (vI===1) {',
  7437. ' vI=2;',
  7438. '}',
  7439. 'if (vI===2){ vI=3; }',
  7440. ';',
  7441. '$mod.vI = 4;'
  7442. ]));
  7443. end;
  7444. procedure TTestModule.TestAsmPas_Impl;
  7445. begin
  7446. StartUnit(false);
  7447. Add('interface');
  7448. Add('const cIntf: longint = 1;');
  7449. Add('var vIntf: longint;');
  7450. Add('implementation');
  7451. Add('const cImpl: longint = 2;');
  7452. Add('var vImpl: longint;');
  7453. Add('procedure DoIt;');
  7454. Add('const cLoc: longint = 3;');
  7455. Add('var vLoc: longint;');
  7456. Add('begin;');
  7457. Add(' asm');
  7458. //Add(' pas(vIntf)=pas(cIntf);');
  7459. //Add(' pas(vImpl)=pas(cImpl);');
  7460. //Add(' pas(vLoc)=pas(cLoc);');
  7461. Add(' end;');
  7462. Add('end;');
  7463. ConvertUnit;
  7464. CheckSource('TestAsmPas_Impl',
  7465. LinesToStr([
  7466. 'var $impl = $mod.$impl;',
  7467. 'this.cIntf = 1;',
  7468. 'this.vIntf = 0;',
  7469. '']),
  7470. '', // this.$init
  7471. LinesToStr([ // implementation
  7472. '$impl.cImpl = 2;',
  7473. '$impl.vImpl = 0;',
  7474. 'var cLoc = 3;',
  7475. '$impl.DoIt = function () {',
  7476. ' var vLoc = 0;',
  7477. '};',
  7478. '']) );
  7479. end;
  7480. procedure TTestModule.TestTryFinally;
  7481. begin
  7482. StartProgram(false);
  7483. Add('var i: longint;');
  7484. Add('begin');
  7485. Add(' try');
  7486. Add(' i:=0; i:=2 div i;');
  7487. Add(' finally');
  7488. Add(' i:=3');
  7489. Add(' end;');
  7490. ConvertProgram;
  7491. CheckSource('TestTryFinally',
  7492. LinesToStr([ // statements
  7493. 'this.i = 0;'
  7494. ]),
  7495. LinesToStr([ // $mod.$main
  7496. 'try {',
  7497. ' $mod.i = 0;',
  7498. ' $mod.i = Math.floor(2 / $mod.i);',
  7499. '} finally {',
  7500. ' $mod.i = 3;',
  7501. '};'
  7502. ]));
  7503. end;
  7504. procedure TTestModule.TestTryExcept;
  7505. begin
  7506. StartProgram(false);
  7507. Add('type');
  7508. Add(' TObject = class end;');
  7509. Add(' Exception = class Msg: string; end;');
  7510. Add(' EInvalidCast = class(Exception) end;');
  7511. Add('var vI: longint;');
  7512. Add('begin');
  7513. Add(' try');
  7514. Add(' vi:=1;');
  7515. Add(' except');
  7516. Add(' vi:=2');
  7517. Add(' end;');
  7518. Add(' try');
  7519. Add(' vi:=3;');
  7520. Add(' except');
  7521. Add(' raise;');
  7522. Add(' end;');
  7523. Add(' try');
  7524. Add(' VI:=4;');
  7525. Add(' except');
  7526. Add(' on einvalidcast do');
  7527. Add(' raise;');
  7528. Add(' on E: exception do');
  7529. Add(' if e.msg='''' then');
  7530. Add(' raise e;');
  7531. Add(' else');
  7532. Add(' vi:=5');
  7533. Add(' end;');
  7534. Add(' try');
  7535. Add(' VI:=6;');
  7536. Add(' except');
  7537. Add(' on einvalidcast do ;');
  7538. Add(' end;');
  7539. ConvertProgram;
  7540. CheckSource('TestTryExcept',
  7541. LinesToStr([ // statements
  7542. 'rtl.createClass($mod, "TObject", null, function () {',
  7543. ' this.$init = function () {',
  7544. ' };',
  7545. ' this.$final = function () {',
  7546. ' };',
  7547. '});',
  7548. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7549. ' this.$init = function () {',
  7550. ' $mod.TObject.$init.call(this);',
  7551. ' this.Msg = "";',
  7552. ' };',
  7553. '});',
  7554. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7555. '});',
  7556. 'this.vI = 0;'
  7557. ]),
  7558. LinesToStr([ // $mod.$main
  7559. 'try {',
  7560. ' $mod.vI = 1;',
  7561. '} catch ($e) {',
  7562. ' $mod.vI = 2;',
  7563. '};',
  7564. 'try {',
  7565. ' $mod.vI = 3;',
  7566. '} catch ($e) {',
  7567. ' throw $e;',
  7568. '};',
  7569. 'try {',
  7570. ' $mod.vI = 4;',
  7571. '} catch ($e) {',
  7572. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7573. ' throw $e',
  7574. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7575. ' var E = $e;',
  7576. ' if (E.Msg === "") throw E;',
  7577. ' } else {',
  7578. ' $mod.vI = 5;',
  7579. ' }',
  7580. '};',
  7581. 'try {',
  7582. ' $mod.vI = 6;',
  7583. '} catch ($e) {',
  7584. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7585. ' } else throw $e',
  7586. '};',
  7587. '']));
  7588. end;
  7589. procedure TTestModule.TestTryExcept_ReservedWords;
  7590. begin
  7591. StartProgram(false);
  7592. Add([
  7593. 'type',
  7594. ' TObject = class end;',
  7595. ' Exception = class',
  7596. ' Symbol: string;',
  7597. ' end;',
  7598. 'var &try: longint;',
  7599. 'begin',
  7600. ' try',
  7601. ' &try:=4;',
  7602. ' except',
  7603. ' on Error: exception do',
  7604. ' if errOR.symBol='''' then',
  7605. ' raise ERRor;',
  7606. ' end;',
  7607. '']);
  7608. ConvertProgram;
  7609. CheckSource('TestTryExcept_ReservedWords',
  7610. LinesToStr([ // statements
  7611. 'rtl.createClass($mod, "TObject", null, function () {',
  7612. ' this.$init = function () {',
  7613. ' };',
  7614. ' this.$final = function () {',
  7615. ' };',
  7616. '});',
  7617. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7618. ' this.$init = function () {',
  7619. ' $mod.TObject.$init.call(this);',
  7620. ' this.Symbol = "";',
  7621. ' };',
  7622. '});',
  7623. 'this.Try = 0;',
  7624. '']),
  7625. LinesToStr([ // $mod.$main
  7626. 'try {',
  7627. ' $mod.Try = 4;',
  7628. '} catch ($e) {',
  7629. ' if ($mod.Exception.isPrototypeOf($e)) {',
  7630. ' var error = $e;',
  7631. ' if (error.Symbol === "") throw error;',
  7632. ' } else throw $e',
  7633. '};',
  7634. '']));
  7635. end;
  7636. procedure TTestModule.TestIfThenRaiseElse;
  7637. begin
  7638. StartProgram(false);
  7639. Add([
  7640. 'type',
  7641. ' TObject = class',
  7642. ' constructor Create;',
  7643. ' end;',
  7644. 'constructor TObject.Create;',
  7645. 'begin',
  7646. 'end;',
  7647. 'var b: boolean;',
  7648. 'begin',
  7649. ' if b then',
  7650. ' raise TObject.Create',
  7651. ' else',
  7652. ' b:=false;',
  7653. '']);
  7654. ConvertProgram;
  7655. CheckSource('TestIfThenRaiseElse',
  7656. LinesToStr([ // statements
  7657. 'rtl.createClass($mod, "TObject", null, function () {',
  7658. ' this.$init = function () {',
  7659. ' };',
  7660. ' this.$final = function () {',
  7661. ' };',
  7662. ' this.Create = function () {',
  7663. ' return this;',
  7664. ' };',
  7665. '});',
  7666. 'this.b = false;',
  7667. '']),
  7668. LinesToStr([ // $mod.$main
  7669. 'if ($mod.b) {',
  7670. ' throw $mod.TObject.$create("Create")}',
  7671. ' else $mod.b = false;',
  7672. '']));
  7673. end;
  7674. procedure TTestModule.TestCaseOf;
  7675. begin
  7676. StartProgram(false);
  7677. Add([
  7678. 'const e: longint; external name ''$e'';',
  7679. 'var vI: longint;',
  7680. 'begin',
  7681. ' case vi of',
  7682. ' 1: ;',
  7683. ' 2: vi:=3;',
  7684. ' e: ;',
  7685. ' else',
  7686. ' VI:=4',
  7687. ' end;']);
  7688. ConvertProgram;
  7689. CheckSource('TestCaseOf',
  7690. LinesToStr([ // statements
  7691. 'this.vI = 0;'
  7692. ]),
  7693. LinesToStr([ // $mod.$main
  7694. 'var $tmp1 = $mod.vI;',
  7695. 'if ($tmp1 === 1) {}',
  7696. 'else if ($tmp1 === 2) {',
  7697. ' $mod.vI = 3}',
  7698. ' else if ($tmp1 === $e) {}',
  7699. 'else {',
  7700. ' $mod.vI = 4;',
  7701. '};'
  7702. ]));
  7703. end;
  7704. procedure TTestModule.TestCaseOf_UseSwitch;
  7705. begin
  7706. StartProgram(false);
  7707. Converter.UseSwitchStatement:=true;
  7708. Add('var Vi: longint;');
  7709. Add('begin');
  7710. Add(' case vi of');
  7711. Add(' 1: ;');
  7712. Add(' 2: VI:=3;');
  7713. Add(' else');
  7714. Add(' vi:=4');
  7715. Add(' end;');
  7716. ConvertProgram;
  7717. CheckSource('TestCaseOf_UseSwitch',
  7718. LinesToStr([ // statements
  7719. 'this.Vi = 0;'
  7720. ]),
  7721. LinesToStr([ // $mod.$main
  7722. 'switch ($mod.Vi) {',
  7723. 'case 1:',
  7724. ' break;',
  7725. 'case 2:',
  7726. ' $mod.Vi = 3;',
  7727. ' break;',
  7728. 'default:',
  7729. ' $mod.Vi = 4;',
  7730. '};'
  7731. ]));
  7732. end;
  7733. procedure TTestModule.TestCaseOfNoElse;
  7734. begin
  7735. StartProgram(false);
  7736. Add('var Vi: longint;');
  7737. Add('begin');
  7738. Add(' case vi of');
  7739. Add(' 1: begin vi:=2; VI:=3; end;');
  7740. Add(' end;');
  7741. ConvertProgram;
  7742. CheckSource('TestCaseOfNoElse',
  7743. LinesToStr([ // statements
  7744. 'this.Vi = 0;'
  7745. ]),
  7746. LinesToStr([ // $mod.$main
  7747. 'var $tmp1 = $mod.Vi;',
  7748. 'if ($tmp1 === 1) {',
  7749. ' $mod.Vi = 2;',
  7750. ' $mod.Vi = 3;',
  7751. '};'
  7752. ]));
  7753. end;
  7754. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  7755. begin
  7756. StartProgram(false);
  7757. Converter.UseSwitchStatement:=true;
  7758. Add('var vI: longint;');
  7759. Add('begin');
  7760. Add(' case vi of');
  7761. Add(' 1: begin VI:=2; vi:=3; end;');
  7762. Add(' end;');
  7763. ConvertProgram;
  7764. CheckSource('TestCaseOfNoElse_UseSwitch',
  7765. LinesToStr([ // statements
  7766. 'this.vI = 0;'
  7767. ]),
  7768. LinesToStr([ // $mod.$main
  7769. 'switch ($mod.vI) {',
  7770. 'case 1:',
  7771. ' $mod.vI = 2;',
  7772. ' $mod.vI = 3;',
  7773. ' break;',
  7774. '};'
  7775. ]));
  7776. end;
  7777. procedure TTestModule.TestCaseOfRange;
  7778. begin
  7779. StartProgram(false);
  7780. Add('var vI: longint;');
  7781. Add('begin');
  7782. Add(' case vi of');
  7783. Add(' 1..3: vi:=14;');
  7784. Add(' 4,5: vi:=16;');
  7785. Add(' 6..7,9..10: ;');
  7786. Add(' else ;');
  7787. Add(' end;');
  7788. ConvertProgram;
  7789. CheckSource('TestCaseOfRange',
  7790. LinesToStr([ // statements
  7791. 'this.vI = 0;'
  7792. ]),
  7793. LinesToStr([ // $mod.$main
  7794. 'var $tmp1 = $mod.vI;',
  7795. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  7796. ' $mod.vI = 14',
  7797. '} else if (($tmp1 === 4) || ($tmp1 === 5)){',
  7798. ' $mod.vI = 16',
  7799. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  7800. ]));
  7801. end;
  7802. procedure TTestModule.TestCaseOfString;
  7803. begin
  7804. StartProgram(false);
  7805. Add([
  7806. 'var s,h: string;',
  7807. 'begin',
  7808. ' case s of',
  7809. ' ''foo'': s:=h;',
  7810. ' ''a''..''z'': h:=s;',
  7811. ' ''ў'', ''ё'': ;',
  7812. ' ''Б''..''Я'': ;',
  7813. ' end;',
  7814. '']);
  7815. ConvertProgram;
  7816. CheckSource('TestCaseOfString',
  7817. LinesToStr([ // statements
  7818. 'this.s = "";',
  7819. 'this.h = "";',
  7820. '']),
  7821. LinesToStr([ // $mod.$main
  7822. 'var $tmp1 = $mod.s;',
  7823. 'if ($tmp1 === "foo") {',
  7824. ' $mod.s = $mod.h}',
  7825. ' else if (($tmp1.length === 1) && ($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7826. ' $mod.h = $mod.s}',
  7827. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  7828. ' else if (($tmp1.length === 1) && ($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7829. '']));
  7830. end;
  7831. procedure TTestModule.TestCaseOfChar;
  7832. begin
  7833. StartProgram(false);
  7834. Add([
  7835. 'var s,h: char;',
  7836. 'begin',
  7837. ' case s of',
  7838. ' ''a''..''z'': h:=s;',
  7839. ' ''ä'': ;',
  7840. ' ''ў'', ''ё'': ;',
  7841. ' ''Б''..''Я'': ;',
  7842. ' end;',
  7843. '']);
  7844. ConvertProgram;
  7845. CheckSource('TestCaseOfString',
  7846. LinesToStr([ // statements
  7847. 'this.s = "";',
  7848. 'this.h = "";',
  7849. '']),
  7850. LinesToStr([ // $mod.$main
  7851. 'var $tmp1 = $mod.s;',
  7852. 'if (($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7853. ' $mod.h = $mod.s}',
  7854. ' else if ($tmp1 === "ä") {}',
  7855. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  7856. ' else if (($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7857. '']));
  7858. end;
  7859. procedure TTestModule.TestCaseOfExternalClassConst;
  7860. begin
  7861. StartProgram(false);
  7862. Add([
  7863. '{$modeswitch externalclass}',
  7864. 'type',
  7865. ' TBird = class external name ''Bird''',
  7866. ' const e: longint;',
  7867. ' end;',
  7868. 'var vI: longint;',
  7869. 'begin',
  7870. ' case vi of',
  7871. ' 1: vi:=3;',
  7872. ' TBird.e: ;',
  7873. ' end;']);
  7874. ConvertProgram;
  7875. CheckSource('TestCaseOfExternalClassConst',
  7876. LinesToStr([ // statements
  7877. 'this.vI = 0;'
  7878. ]),
  7879. LinesToStr([ // $mod.$main
  7880. 'var $tmp1 = $mod.vI;',
  7881. 'if ($tmp1 === 1) {',
  7882. ' $mod.vI = 3}',
  7883. ' else if ($tmp1 === Bird.e) ;'
  7884. ]));
  7885. end;
  7886. procedure TTestModule.TestDebugger;
  7887. begin
  7888. StartProgram(false);
  7889. Add([
  7890. 'procedure DoIt;',
  7891. 'begin',
  7892. ' deBugger;',
  7893. ' DeBugger();',
  7894. 'end;',
  7895. 'begin',
  7896. ' Debugger;']);
  7897. ConvertProgram;
  7898. CheckSource('TestDebugger',
  7899. LinesToStr([ // statements
  7900. 'this.DoIt = function () {',
  7901. ' debugger;',
  7902. ' debugger;',
  7903. '};',
  7904. '']),
  7905. LinesToStr([ // $mod.$main
  7906. 'debugger;',
  7907. '']));
  7908. end;
  7909. procedure TTestModule.TestArray_Dynamic;
  7910. begin
  7911. StartProgram(false);
  7912. Add(['type',
  7913. ' TArrayInt = array of longint;',
  7914. 'var',
  7915. ' Arr: TArrayInt;',
  7916. ' i: longint;',
  7917. ' b: boolean;',
  7918. 'begin',
  7919. ' SetLength(arr,3);',
  7920. ' arr[0]:=4;',
  7921. ' arr[1]:=length(arr)+arr[0];',
  7922. ' arr[i]:=5;',
  7923. ' arr[arr[i]]:=arr[6];',
  7924. ' i:=low(arr);',
  7925. ' i:=high(arr);',
  7926. ' b:=Assigned(arr);',
  7927. ' Arr:=default(TArrayInt);']);
  7928. ConvertProgram;
  7929. CheckSource('TestArray_Dynamic',
  7930. LinesToStr([ // statements
  7931. 'this.Arr = [];',
  7932. 'this.i = 0;',
  7933. 'this.b = false;'
  7934. ]),
  7935. LinesToStr([ // $mod.$main
  7936. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  7937. '$mod.Arr[0] = 4;',
  7938. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  7939. '$mod.Arr[$mod.i] = 5;',
  7940. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  7941. '$mod.i = 0;',
  7942. '$mod.i = rtl.length($mod.Arr) - 1;',
  7943. '$mod.b = rtl.length($mod.Arr) > 0;',
  7944. '$mod.Arr = [];',
  7945. '']));
  7946. end;
  7947. procedure TTestModule.TestArray_Dynamic_Nil;
  7948. begin
  7949. StartProgram(false);
  7950. Add('type');
  7951. Add(' TArrayInt = array of longint;');
  7952. Add('var');
  7953. Add(' Arr: TArrayInt;');
  7954. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  7955. Add('begin');
  7956. Add(' arr:=nil;');
  7957. Add(' if arr=nil then;');
  7958. Add(' if nil=arr then;');
  7959. Add(' if arr<>nil then;');
  7960. Add(' if nil<>arr then;');
  7961. Add(' DoIt(nil,nil);');
  7962. ConvertProgram;
  7963. CheckSource('TestArray_Dynamic',
  7964. LinesToStr([ // statements
  7965. 'this.Arr = [];',
  7966. 'this.DoIt = function(i,j){',
  7967. '};'
  7968. ]),
  7969. LinesToStr([ // $mod.$main
  7970. '$mod.Arr = [];',
  7971. 'if (rtl.length($mod.Arr) === 0) ;',
  7972. 'if (rtl.length($mod.Arr) === 0) ;',
  7973. 'if (rtl.length($mod.Arr) > 0) ;',
  7974. 'if (rtl.length($mod.Arr) > 0) ;',
  7975. '$mod.DoIt([],[]);',
  7976. '']));
  7977. end;
  7978. procedure TTestModule.TestArray_DynMultiDimensional;
  7979. begin
  7980. StartProgram(false);
  7981. Add('type');
  7982. Add(' TArrayInt = array of longint;');
  7983. Add(' TArrayArrayInt = array of TArrayInt;');
  7984. Add('var');
  7985. Add(' Arr: TArrayInt;');
  7986. Add(' Arr2: TArrayArrayInt;');
  7987. Add(' i: longint;');
  7988. Add('begin');
  7989. Add(' arr2:=nil;');
  7990. Add(' if arr2=nil then;');
  7991. Add(' if nil=arr2 then;');
  7992. Add(' i:=low(arr2);');
  7993. Add(' i:=low(arr2[1]);');
  7994. Add(' i:=high(arr2);');
  7995. Add(' i:=high(arr2[2]);');
  7996. Add(' arr2[3]:=arr;');
  7997. Add(' arr2[4][5]:=i;');
  7998. Add(' i:=arr2[6][7];');
  7999. Add(' arr2[8,9]:=i;');
  8000. Add(' i:=arr2[10,11];');
  8001. Add(' SetLength(arr2,14);');
  8002. Add(' SetLength(arr2[15],16);');
  8003. ConvertProgram;
  8004. CheckSource('TestArray_Dynamic',
  8005. LinesToStr([ // statements
  8006. 'this.Arr = [];',
  8007. 'this.Arr2 = [];',
  8008. 'this.i = 0;'
  8009. ]),
  8010. LinesToStr([ // $mod.$main
  8011. '$mod.Arr2 = [];',
  8012. 'if (rtl.length($mod.Arr2) === 0) ;',
  8013. 'if (rtl.length($mod.Arr2) === 0) ;',
  8014. '$mod.i = 0;',
  8015. '$mod.i = 0;',
  8016. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8017. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8018. '$mod.Arr2[3] = $mod.Arr;',
  8019. '$mod.Arr2[4][5] = $mod.i;',
  8020. '$mod.i = $mod.Arr2[6][7];',
  8021. '$mod.Arr2[8][9] = $mod.i;',
  8022. '$mod.i = $mod.Arr2[10][11];',
  8023. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8024. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8025. '']));
  8026. end;
  8027. procedure TTestModule.TestArray_StaticInt;
  8028. begin
  8029. StartProgram(false);
  8030. Add('type');
  8031. Add(' TArrayInt = array[2..4] of longint;');
  8032. Add('var');
  8033. Add(' Arr: TArrayInt;');
  8034. Add(' Arr2: TArrayInt = (5,6,7);');
  8035. Add(' i: longint;');
  8036. Add(' b: boolean;');
  8037. Add('begin');
  8038. Add(' arr[2]:=4;');
  8039. Add(' arr[3]:=arr[2]+arr[3];');
  8040. Add(' arr[i]:=5;');
  8041. Add(' arr[arr[i]]:=arr[high(arr)];');
  8042. Add(' i:=low(arr);');
  8043. Add(' i:=high(arr);');
  8044. Add(' b:=arr[2]=arr[3];');
  8045. Add(' arr:=default(TArrayInt);');
  8046. ConvertProgram;
  8047. CheckSource('TestArray_StaticInt',
  8048. LinesToStr([ // statements
  8049. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8050. 'this.Arr2 = [5, 6, 7];',
  8051. 'this.i = 0;',
  8052. 'this.b = false;'
  8053. ]),
  8054. LinesToStr([ // $mod.$main
  8055. '$mod.Arr[0] = 4;',
  8056. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8057. '$mod.Arr[$mod.i-2] = 5;',
  8058. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8059. '$mod.i = 2;',
  8060. '$mod.i = 4;',
  8061. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8062. '$mod.Arr = rtl.arraySetLength(null,0,3).slice(0);',
  8063. '']));
  8064. end;
  8065. procedure TTestModule.TestArray_StaticBool;
  8066. begin
  8067. StartProgram(false);
  8068. Add('type');
  8069. Add(' TBools = array[boolean] of boolean;');
  8070. Add(' TBool2 = array[true..true] of boolean;');
  8071. Add('var');
  8072. Add(' Arr: TBools;');
  8073. Add(' Arr2: TBool2;');
  8074. Add(' Arr3: TBools = (true,false);');
  8075. Add(' b: boolean;');
  8076. Add('begin');
  8077. Add(' b:=low(arr);');
  8078. Add(' b:=high(arr);');
  8079. Add(' arr[true]:=false;');
  8080. Add(' arr[false]:=arr[b] or arr[true];');
  8081. Add(' arr[b]:=true;');
  8082. Add(' arr[arr[b]]:=arr[high(arr)];');
  8083. Add(' b:=arr[false]=arr[true];');
  8084. Add(' b:=low(arr2);');
  8085. Add(' b:=high(arr2);');
  8086. Add(' arr2[true]:=true;');
  8087. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8088. Add(' arr2[b]:=false;');
  8089. ConvertProgram;
  8090. CheckSource('TestArray_StaticBool',
  8091. LinesToStr([ // statements
  8092. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8093. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8094. 'this.Arr3 = [true, false];',
  8095. 'this.b = false;'
  8096. ]),
  8097. LinesToStr([ // $mod.$main
  8098. '$mod.b = false;',
  8099. '$mod.b = true;',
  8100. '$mod.Arr[1] = false;',
  8101. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8102. '$mod.Arr[+$mod.b] = true;',
  8103. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8104. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8105. '$mod.b = true;',
  8106. '$mod.b = true;',
  8107. '$mod.Arr2[0] = true;',
  8108. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8109. '$mod.Arr2[1-$mod.b] = false;',
  8110. '']));
  8111. end;
  8112. procedure TTestModule.TestArray_StaticChar;
  8113. begin
  8114. StartProgram(false);
  8115. Add([
  8116. 'type',
  8117. ' TChars = array[char] of char;',
  8118. ' TChars2 = array[''a''..''z''] of char;',
  8119. 'var',
  8120. ' Arr: TChars;',
  8121. ' Arr2: TChars2;',
  8122. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8123. ' Arr4: array[11..13] of char = ''pas'';',
  8124. ' Arr5: array[21..22] of char = ''äö'';',
  8125. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8126. ' c: char;',
  8127. ' b: boolean;',
  8128. 'begin',
  8129. ' c:=low(arr);',
  8130. ' c:=high(arr);',
  8131. ' arr[''B'']:=''a'';',
  8132. ' arr[''D'']:=arr[c];',
  8133. ' arr[c]:=arr[''d''];',
  8134. ' arr[arr[c]]:=arr[high(arr)];',
  8135. ' b:=arr[low(arr)]=arr[''e''];',
  8136. ' c:=low(arr2);',
  8137. ' c:=high(arr2);',
  8138. ' arr2[''b'']:=''f'';',
  8139. ' arr2[''a'']:=arr2[c];',
  8140. ' arr2[c]:=arr2[''g''];']);
  8141. ConvertProgram;
  8142. CheckSource('TestArray_StaticChar',
  8143. LinesToStr([ // statements
  8144. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8145. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8146. 'this.Arr3 = ["p", "a", "s"];',
  8147. 'this.Arr4 = ["p", "a", "s"];',
  8148. 'this.Arr5 = ["ä", "ö"];',
  8149. 'this.Arr6 = ["ä", "ö"];',
  8150. 'this.c = "";',
  8151. 'this.b = false;',
  8152. '']),
  8153. LinesToStr([ // $mod.$main
  8154. '$mod.c = "\x00";',
  8155. '$mod.c = "\uFFFF";',
  8156. '$mod.Arr[66] = "a";',
  8157. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8158. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8159. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8160. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8161. '$mod.c = "a";',
  8162. '$mod.c = "z";',
  8163. '$mod.Arr2[1] = "f";',
  8164. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8165. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8166. '']));
  8167. end;
  8168. procedure TTestModule.TestArray_StaticMultiDim;
  8169. begin
  8170. StartProgram(false);
  8171. Add([
  8172. 'type',
  8173. ' TArrayInt = array[1..3] of longint;',
  8174. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8175. 'var',
  8176. ' Arr: TArrayInt;',
  8177. ' Arr2: TArrayArrayInt;',
  8178. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8179. ' i: longint;',
  8180. 'begin',
  8181. ' i:=low(arr);',
  8182. ' i:=low(arr2);',
  8183. ' i:=low(arr2[5]);',
  8184. ' i:=high(arr);',
  8185. ' i:=high(arr2);',
  8186. ' i:=high(arr2[6]);',
  8187. ' arr2[5]:=arr;',
  8188. ' arr2[6][2]:=i;',
  8189. ' i:=arr2[6][3];',
  8190. ' arr2[6,3]:=i;',
  8191. ' i:=arr2[5,2];',
  8192. ' arr2:=arr2;',// clone multi dim static array
  8193. //' arr3:=arr3;',// clone anonymous multi dim static array
  8194. '']);
  8195. ConvertProgram;
  8196. CheckSource('TestArray_StaticMultiDim',
  8197. LinesToStr([ // statements
  8198. 'this.TArrayArrayInt$clone = function (a) {',
  8199. ' var r = [];',
  8200. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8201. ' return r;',
  8202. '};',
  8203. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8204. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8205. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8206. 'this.i = 0;'
  8207. ]),
  8208. LinesToStr([ // $mod.$main
  8209. '$mod.i = 1;',
  8210. '$mod.i = 5;',
  8211. '$mod.i = 1;',
  8212. '$mod.i = 3;',
  8213. '$mod.i = 6;',
  8214. '$mod.i = 3;',
  8215. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8216. '$mod.Arr2[1][1] = $mod.i;',
  8217. '$mod.i = $mod.Arr2[1][2];',
  8218. '$mod.Arr2[1][2] = $mod.i;',
  8219. '$mod.i = $mod.Arr2[0][1];',
  8220. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8221. '']));
  8222. end;
  8223. procedure TTestModule.TestArray_StaticInFunction;
  8224. begin
  8225. StartProgram(false);
  8226. Add([
  8227. 'const TArrayInt = 3;',
  8228. 'const TArrayArrayInt = 4;',
  8229. 'procedure DoIt;',
  8230. 'type',
  8231. ' TArrayInt = array[1..3] of longint;',
  8232. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8233. 'var',
  8234. ' Arr: TArrayInt;',
  8235. ' Arr2: TArrayArrayInt;',
  8236. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8237. ' i: longint;',
  8238. 'begin',
  8239. ' arr2[5]:=arr;',
  8240. ' arr2:=arr2;',// clone multi dim static array
  8241. 'end;',
  8242. 'begin',
  8243. '']);
  8244. ConvertProgram;
  8245. CheckSource('TestArray_StaticInFunction',
  8246. LinesToStr([ // statements
  8247. 'this.TArrayInt = 3;',
  8248. 'this.TArrayArrayInt = 4;',
  8249. 'var TArrayArrayInt$1$clone = function (a) {',
  8250. ' var r = [];',
  8251. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8252. ' return r;',
  8253. '};',
  8254. 'this.DoIt = function () {',
  8255. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8256. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8257. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8258. ' var i = 0;',
  8259. ' Arr2[0] = Arr.slice(0);',
  8260. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8261. '};',
  8262. '']),
  8263. LinesToStr([ // $mod.$main
  8264. '']));
  8265. end;
  8266. procedure TTestModule.TestArrayOfRecord;
  8267. begin
  8268. StartProgram(false);
  8269. Add([
  8270. 'type',
  8271. ' TRec = record',
  8272. ' Int: longint;',
  8273. ' end;',
  8274. ' TArrayRec = array of TRec;',
  8275. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8276. 'begin',
  8277. 'end;',
  8278. 'var',
  8279. ' Arr: TArrayRec;',
  8280. ' r: TRec;',
  8281. ' i: longint;',
  8282. 'begin',
  8283. ' SetLength(arr,3);',
  8284. ' arr[0].int:=4;',
  8285. ' arr[1].int:=length(arr)+arr[2].int;',
  8286. ' arr[arr[i].int].int:=arr[5].int;',
  8287. ' arr[7]:=r;',
  8288. ' r:=arr[8];',
  8289. ' i:=low(arr);',
  8290. ' i:=high(arr);',
  8291. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8292. ConvertProgram;
  8293. CheckSource('TestArrayOfRecord',
  8294. LinesToStr([ // statements
  8295. 'rtl.recNewT($mod, "TRec", function () {',
  8296. ' this.Int = 0;',
  8297. ' this.$eq = function (b) {',
  8298. ' return this.Int === b.Int;',
  8299. ' };',
  8300. ' this.$assign = function (s) {',
  8301. ' this.Int = s.Int;',
  8302. ' return this;',
  8303. ' };',
  8304. '});',
  8305. 'this.DoIt = function (vd, vc, vv) {',
  8306. '};',
  8307. 'this.Arr = [];',
  8308. 'this.r = $mod.TRec.$new();',
  8309. 'this.i = 0;'
  8310. ]),
  8311. LinesToStr([ // $mod.$main
  8312. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8313. '$mod.Arr[0].Int = 4;',
  8314. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8315. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8316. '$mod.Arr[7].$assign($mod.r);',
  8317. '$mod.r.$assign($mod.Arr[8]);',
  8318. '$mod.i = 0;',
  8319. '$mod.i = rtl.length($mod.Arr)-1;',
  8320. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8321. '']));
  8322. end;
  8323. procedure TTestModule.TestArray_StaticRecord;
  8324. begin
  8325. StartProgram(false);
  8326. Add([
  8327. 'type',
  8328. ' TRec = record',
  8329. ' Int: longint;',
  8330. ' end;',
  8331. ' TArrayRec = array[1..2] of TRec;',
  8332. 'var',
  8333. ' Arr: TArrayRec;',
  8334. 'begin',
  8335. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8336. '']);
  8337. ConvertProgram;
  8338. CheckSource('TestArray_StaticRecord',
  8339. LinesToStr([ // statements
  8340. 'rtl.recNewT($mod, "TRec", function () {',
  8341. ' this.Int = 0;',
  8342. ' this.$eq = function (b) {',
  8343. ' return this.Int === b.Int;',
  8344. ' };',
  8345. ' this.$assign = function (s) {',
  8346. ' this.Int = s.Int;',
  8347. ' return this;',
  8348. ' };',
  8349. '});',
  8350. 'this.TArrayRec$clone = function (a) {',
  8351. ' var r = [];',
  8352. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8353. ' return r;',
  8354. '};',
  8355. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8356. '']),
  8357. LinesToStr([ // $mod.$main
  8358. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8359. end;
  8360. procedure TTestModule.TestArrayOfSet;
  8361. begin
  8362. StartProgram(false);
  8363. Add([
  8364. 'type',
  8365. ' TFlag = (big,small);',
  8366. ' TSetOfFlag = set of tflag;',
  8367. ' TArrayFlag = array of TSetOfFlag;',
  8368. 'procedure DoIt(const a: Tarrayflag);',
  8369. 'begin',
  8370. 'end;',
  8371. 'var',
  8372. ' f: TFlag;',
  8373. ' s: TSetOfFlag;',
  8374. ' Arr: TArrayFlag;',
  8375. ' i: longint;',
  8376. 'begin',
  8377. ' SetLength(arr,3);',
  8378. ' arr[0]:=s;',
  8379. ' arr[1]:=[big];',
  8380. ' arr[2]:=[big]+s;',
  8381. ' arr[3]:=s+[big];',
  8382. ' arr[4]:=arr[5];',
  8383. ' s:=arr[6];',
  8384. ' i:=low(arr);',
  8385. ' i:=high(arr);',
  8386. ' DoIt(arr);',
  8387. ' DoIt([s]);',
  8388. ' DoIt([[],s]);',
  8389. ' DoIt([s,[]]);',
  8390. '']);
  8391. ConvertProgram;
  8392. CheckSource('TestArrayOfSet',
  8393. LinesToStr([ // statements
  8394. 'this.TFlag = {',
  8395. ' "0": "big",',
  8396. ' big: 0,',
  8397. ' "1": "small",',
  8398. ' small: 1',
  8399. '};',
  8400. 'this.DoIt = function (a) {',
  8401. '};',
  8402. 'this.f = 0;',
  8403. 'this.s = {};',
  8404. 'this.Arr = [];',
  8405. 'this.i = 0;',
  8406. '']),
  8407. LinesToStr([ // $mod.$main
  8408. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8409. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8410. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8411. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8412. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8413. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8414. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8415. '$mod.i = 0;',
  8416. '$mod.i = rtl.length($mod.Arr) - 1;',
  8417. '$mod.DoIt($mod.Arr);',
  8418. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8419. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8420. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8421. '']));
  8422. end;
  8423. procedure TTestModule.TestArray_DynAsParam;
  8424. begin
  8425. StartProgram(false);
  8426. Add([
  8427. 'type integer = longint;',
  8428. 'type TArrInt = array of integer;',
  8429. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8430. 'var vJ: TArrInt;',
  8431. 'begin',
  8432. ' vg:=vg;',
  8433. ' vj:=vh;',
  8434. ' vi:=vi;',
  8435. ' doit(vg,vg,vg);',
  8436. ' doit(vh,vh,vj);',
  8437. ' doit(vi,vi,vi);',
  8438. ' doit(vj,vj,vj);',
  8439. 'end;',
  8440. 'var i: TArrInt;',
  8441. 'begin',
  8442. ' doit(i,i,i);']);
  8443. ConvertProgram;
  8444. CheckSource('TestArray_DynAsParams',
  8445. LinesToStr([ // statements
  8446. 'this.DoIt = function (vG,vH,vI) {',
  8447. ' var vJ = [];',
  8448. ' vG = vG;',
  8449. ' vJ = vH;',
  8450. ' vI.set(vI.get());',
  8451. ' $mod.DoIt(vG, vG, {',
  8452. ' get: function () {',
  8453. ' return vG;',
  8454. ' },',
  8455. ' set: function (v) {',
  8456. ' vG = v;',
  8457. ' }',
  8458. ' });',
  8459. ' $mod.DoIt(vH, vH, {',
  8460. ' get: function () {',
  8461. ' return vJ;',
  8462. ' },',
  8463. ' set: function (v) {',
  8464. ' vJ = v;',
  8465. ' }',
  8466. ' });',
  8467. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8468. ' $mod.DoIt(vJ, vJ, {',
  8469. ' get: function () {',
  8470. ' return vJ;',
  8471. ' },',
  8472. ' set: function (v) {',
  8473. ' vJ = v;',
  8474. ' }',
  8475. ' });',
  8476. '};',
  8477. 'this.i = [];'
  8478. ]),
  8479. LinesToStr([
  8480. '$mod.DoIt($mod.i,$mod.i,{',
  8481. ' p: $mod,',
  8482. ' get: function () {',
  8483. ' return this.p.i;',
  8484. ' },',
  8485. ' set: function (v) {',
  8486. ' this.p.i = v;',
  8487. ' }',
  8488. '});'
  8489. ]));
  8490. end;
  8491. procedure TTestModule.TestArray_StaticAsParam;
  8492. begin
  8493. StartProgram(false);
  8494. Add([
  8495. 'type integer = longint;',
  8496. 'type TArrInt = array[1..2] of integer;',
  8497. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8498. 'var vJ: TArrInt;',
  8499. 'begin',
  8500. ' vg:=vg;',
  8501. ' vj:=vh;',
  8502. ' vi:=vi;',
  8503. ' doit(vg,vg,vg);',
  8504. ' doit(vh,vh,vj);',
  8505. ' doit(vi,vi,vi);',
  8506. ' doit(vj,vj,vj);',
  8507. 'end;',
  8508. 'var i: TArrInt;',
  8509. 'begin',
  8510. ' doit(i,i,i);']);
  8511. ConvertProgram;
  8512. CheckSource('TestArray_StaticAsParams',
  8513. LinesToStr([ // statements
  8514. 'this.DoIt = function (vG,vH,vI) {',
  8515. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  8516. ' vG = vG.slice(0);',
  8517. ' vJ = vH.slice(0);',
  8518. ' vI.set(vI.get().slice(0));',
  8519. ' $mod.DoIt(vG.slice(0), vG, {',
  8520. ' get: function () {',
  8521. ' return vG;',
  8522. ' },',
  8523. ' set: function (v) {',
  8524. ' vG = v;',
  8525. ' }',
  8526. ' });',
  8527. ' $mod.DoIt(vH.slice(0), vH, {',
  8528. ' get: function () {',
  8529. ' return vJ;',
  8530. ' },',
  8531. ' set: function (v) {',
  8532. ' vJ = v;',
  8533. ' }',
  8534. ' });',
  8535. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  8536. ' $mod.DoIt(vJ.slice(0), vJ, {',
  8537. ' get: function () {',
  8538. ' return vJ;',
  8539. ' },',
  8540. ' set: function (v) {',
  8541. ' vJ = v;',
  8542. ' }',
  8543. ' });',
  8544. '};',
  8545. 'this.i = rtl.arraySetLength(null, 0, 2);'
  8546. ]),
  8547. LinesToStr([
  8548. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  8549. ' p: $mod,',
  8550. ' get: function () {',
  8551. ' return this.p.i;',
  8552. ' },',
  8553. ' set: function (v) {',
  8554. ' this.p.i = v;',
  8555. ' }',
  8556. '});'
  8557. ]));
  8558. end;
  8559. procedure TTestModule.TestArrayElement_AsParams;
  8560. begin
  8561. StartProgram(false);
  8562. Add('type integer = longint;');
  8563. Add('type TArrayInt = array of integer;');
  8564. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  8565. Add('var vJ: tarrayint;');
  8566. Add('begin');
  8567. Add(' vi:=vi;');
  8568. Add(' doit(vi,vi,vi);');
  8569. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  8570. Add('end;');
  8571. Add('var a: TArrayInt;');
  8572. Add('begin');
  8573. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  8574. ConvertProgram;
  8575. CheckSource('TestArrayElement_AsParams',
  8576. LinesToStr([ // statements
  8577. 'this.DoIt = function (vG,vH,vI) {',
  8578. ' var vJ = [];',
  8579. ' vI.set(vI.get());',
  8580. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8581. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  8582. ' a:1+3,',
  8583. ' p:vJ,',
  8584. ' get: function () {',
  8585. ' return this.p[this.a];',
  8586. ' },',
  8587. ' set: function (v) {',
  8588. ' this.p[this.a] = v;',
  8589. ' }',
  8590. ' });',
  8591. '};',
  8592. 'this.a = [];'
  8593. ]),
  8594. LinesToStr([
  8595. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  8596. ' a: 1+6,',
  8597. ' p: $mod.a,',
  8598. ' get: function () {',
  8599. ' return this.p[this.a];',
  8600. ' },',
  8601. ' set: function (v) {',
  8602. ' this.p[this.a] = v;',
  8603. ' }',
  8604. '});'
  8605. ]));
  8606. end;
  8607. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  8608. begin
  8609. StartProgram(false);
  8610. Add('type Integer = longint;');
  8611. Add('type TArrayInt = array of integer;');
  8612. Add('function GetArr(vB: integer = 0): tarrayint;');
  8613. Add('begin');
  8614. Add('end;');
  8615. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  8616. Add('begin');
  8617. Add('end;');
  8618. Add('begin');
  8619. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  8620. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  8621. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  8622. ConvertProgram;
  8623. CheckSource('TestArrayElementFromFuncResult_AsParams',
  8624. LinesToStr([ // statements
  8625. 'this.GetArr = function (vB) {',
  8626. ' var Result = [];',
  8627. ' return Result;',
  8628. '};',
  8629. 'this.DoIt = function (vG,vH,vI) {',
  8630. '};'
  8631. ]),
  8632. LinesToStr([
  8633. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  8634. ' a: 1+3,',
  8635. ' p: $mod.GetArr(0),',
  8636. ' get: function () {',
  8637. ' return this.p[this.a];',
  8638. ' },',
  8639. ' set: function (v) {',
  8640. ' this.p[this.a] = v;',
  8641. ' }',
  8642. '});',
  8643. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  8644. ' a: 2+3,',
  8645. ' p: $mod.GetArr(0),',
  8646. ' get: function () {',
  8647. ' return this.p[this.a];',
  8648. ' },',
  8649. ' set: function (v) {',
  8650. ' this.p[this.a] = v;',
  8651. ' }',
  8652. '});',
  8653. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  8654. ' a: 3+3,',
  8655. ' p: $mod.GetArr(9),',
  8656. ' get: function () {',
  8657. ' return this.p[this.a];',
  8658. ' },',
  8659. ' set: function (v) {',
  8660. ' this.p[this.a] = v;',
  8661. ' }',
  8662. '});',
  8663. '']));
  8664. end;
  8665. procedure TTestModule.TestArrayEnumTypeRange;
  8666. begin
  8667. StartProgram(false);
  8668. Add([
  8669. 'type',
  8670. ' TEnum = (red,blue);',
  8671. ' TEnumArray = array[TEnum] of longint;',
  8672. 'var',
  8673. ' e: TEnum;',
  8674. ' i: longint;',
  8675. ' a: TEnumArray;',
  8676. ' numbers: TEnumArray = (1,2);',
  8677. ' names: array[TEnum] of string = (''red'',''blue'');',
  8678. 'begin',
  8679. ' e:=low(a);',
  8680. ' e:=high(a);',
  8681. ' i:=a[red];',
  8682. ' a[e]:=a[e];']);
  8683. ConvertProgram;
  8684. CheckSource('TestArrayEnumTypeRange',
  8685. LinesToStr([ // statements
  8686. ' this.TEnum = {',
  8687. ' "0": "red",',
  8688. ' red: 0,',
  8689. ' "1": "blue",',
  8690. ' blue: 1',
  8691. '};',
  8692. 'this.e = 0;',
  8693. 'this.i = 0;',
  8694. 'this.a = rtl.arraySetLength(null,0,2);',
  8695. 'this.numbers = [1, 2];',
  8696. 'this.names = ["red", "blue"];',
  8697. '']),
  8698. LinesToStr([ // $mod.$main
  8699. '$mod.e = $mod.TEnum.red;',
  8700. '$mod.e = $mod.TEnum.blue;',
  8701. '$mod.i = $mod.a[$mod.TEnum.red];',
  8702. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  8703. '']));
  8704. end;
  8705. procedure TTestModule.TestArray_SetLengthOutArg;
  8706. begin
  8707. StartProgram(false);
  8708. Add([
  8709. 'type TArrInt = array of longint;',
  8710. 'procedure DoIt(out a: TArrInt);',
  8711. 'begin',
  8712. ' SetLength(a,2);',
  8713. 'end;',
  8714. 'begin',
  8715. '']);
  8716. ConvertProgram;
  8717. CheckSource('TestArray_SetLengthOutArg',
  8718. LinesToStr([ // statements
  8719. 'this.DoIt = function (a) {',
  8720. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  8721. '};',
  8722. '']),
  8723. LinesToStr([
  8724. '']));
  8725. end;
  8726. procedure TTestModule.TestArray_SetLengthProperty;
  8727. begin
  8728. StartProgram(false);
  8729. Add('type');
  8730. Add(' TArrInt = array of longint;');
  8731. Add(' TObject = class');
  8732. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  8733. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  8734. Add(' property Colors: TArrInt read GetColors write SetColors;');
  8735. Add(' end;');
  8736. Add('var Obj: TObject;');
  8737. Add('begin');
  8738. Add(' SetLength(Obj.Colors,2);');
  8739. ConvertProgram;
  8740. CheckSource('TestArray_SetLengthProperty',
  8741. LinesToStr([ // statements
  8742. 'rtl.createClass($mod, "TObject", null, function () {',
  8743. ' this.$init = function () {',
  8744. ' };',
  8745. ' this.$final = function () {',
  8746. ' };',
  8747. '});',
  8748. 'this.Obj = null;',
  8749. '']),
  8750. LinesToStr([
  8751. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  8752. '']));
  8753. end;
  8754. procedure TTestModule.TestArray_SetLengthMultiDim;
  8755. begin
  8756. StartProgram(false);
  8757. Add([
  8758. 'type',
  8759. ' TArrArrInt = array of array of longint;',
  8760. ' TArrStaInt = array of array[1..2] of longint;',
  8761. 'var',
  8762. ' a: TArrArrInt;',
  8763. ' b: TArrStaInt;',
  8764. 'begin',
  8765. ' SetLength(a,2);',
  8766. ' SetLength(a,3,4);',
  8767. ' SetLength(b,5);',
  8768. '']);
  8769. ConvertProgram;
  8770. CheckSource('TestArray_SetLengthMultiDim',
  8771. LinesToStr([ // statements
  8772. 'this.a = [];',
  8773. 'this.b = [];',
  8774. '']),
  8775. LinesToStr([
  8776. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  8777. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  8778. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, 2);',
  8779. '']));
  8780. end;
  8781. procedure TTestModule.TestArray_OpenArrayOfString;
  8782. begin
  8783. StartProgram(false);
  8784. Add('procedure DoIt(const a: array of String);');
  8785. Add('var');
  8786. Add(' i: longint;');
  8787. Add(' s: string;');
  8788. Add('begin');
  8789. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  8790. Add('end;');
  8791. Add('var s: string;');
  8792. Add('begin');
  8793. Add(' DoIt([]);');
  8794. Add(' DoIt([s,''foo'','''',s+s]);');
  8795. ConvertProgram;
  8796. CheckSource('TestArray_OpenArrayOfString',
  8797. LinesToStr([ // statements
  8798. 'this.DoIt = function (a) {',
  8799. ' var i = 0;',
  8800. ' var s = "";',
  8801. ' for (var $l1 = 0, $end2 = rtl.length(a) - 1; $l1 <= $end2; $l1++) {',
  8802. ' i = $l1;',
  8803. ' s = a[rtl.length(a) - i - 1];',
  8804. ' };',
  8805. '};',
  8806. 'this.s = "";',
  8807. '']),
  8808. LinesToStr([
  8809. '$mod.DoIt([]);',
  8810. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  8811. '']));
  8812. end;
  8813. procedure TTestModule.TestArray_Concat;
  8814. begin
  8815. StartProgram(false);
  8816. Add([
  8817. 'type',
  8818. ' integer = longint;',
  8819. ' TFlag = (big,small);',
  8820. ' TFlags = set of TFlag;',
  8821. ' TRec = record',
  8822. ' i: integer;',
  8823. ' end;',
  8824. ' TArrInt = array of integer;',
  8825. ' TArrRec = array of TRec;',
  8826. ' TArrFlag = array of TFlag;',
  8827. ' TArrSet = array of TFlags;',
  8828. ' TArrJSValue = array of jsvalue;',
  8829. 'var',
  8830. ' ArrInt: tarrint;',
  8831. ' ArrRec: tarrrec;',
  8832. ' ArrFlag: tarrflag;',
  8833. ' ArrSet: tarrset;',
  8834. ' ArrJSValue: tarrjsvalue;',
  8835. 'begin',
  8836. ' arrint:=concat(arrint);',
  8837. ' arrint:=concat(arrint,arrint);',
  8838. ' arrint:=concat(arrint,arrint,arrint);',
  8839. ' arrrec:=concat(arrrec);',
  8840. ' arrrec:=concat(arrrec,arrrec);',
  8841. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  8842. ' arrset:=concat(arrset);',
  8843. ' arrset:=concat(arrset,arrset);',
  8844. ' arrset:=concat(arrset,arrset,arrset);',
  8845. ' arrjsvalue:=concat(arrjsvalue);',
  8846. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  8847. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  8848. ' arrint:=concat([1],arrint);',
  8849. ' arrflag:=concat([big]);',
  8850. ' arrflag:=concat([big],arrflag);',
  8851. ' arrflag:=concat(arrflag,[small]);',
  8852. '']);
  8853. ConvertProgram;
  8854. CheckSource('TestArray_Concat',
  8855. LinesToStr([ // statements
  8856. 'this.TFlag = {',
  8857. ' "0": "big",',
  8858. ' big: 0,',
  8859. ' "1": "small",',
  8860. ' small: 1',
  8861. '};',
  8862. 'rtl.recNewT($mod, "TRec", function () {',
  8863. ' this.i = 0;',
  8864. ' this.$eq = function (b) {',
  8865. ' return this.i === b.i;',
  8866. ' };',
  8867. ' this.$assign = function (s) {',
  8868. ' this.i = s.i;',
  8869. ' return this;',
  8870. ' };',
  8871. '});',
  8872. 'this.ArrInt = [];',
  8873. 'this.ArrRec = [];',
  8874. 'this.ArrFlag = [];',
  8875. 'this.ArrSet = [];',
  8876. 'this.ArrJSValue = [];',
  8877. '']),
  8878. LinesToStr([ // $mod.$main
  8879. '$mod.ArrInt = $mod.ArrInt;',
  8880. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  8881. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  8882. '$mod.ArrRec = $mod.ArrRec;',
  8883. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  8884. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  8885. '$mod.ArrSet = $mod.ArrSet;',
  8886. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  8887. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  8888. '$mod.ArrJSValue = $mod.ArrJSValue;',
  8889. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  8890. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  8891. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  8892. '$mod.ArrFlag = [$mod.TFlag.big];',
  8893. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  8894. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  8895. '']));
  8896. end;
  8897. procedure TTestModule.TestArray_Copy;
  8898. begin
  8899. StartProgram(false);
  8900. Add([
  8901. 'type',
  8902. ' integer = longint;',
  8903. ' TFlag = (big,small);',
  8904. ' TFlags = set of TFlag;',
  8905. ' TRec = record',
  8906. ' i: integer;',
  8907. ' end;',
  8908. ' TArrInt = array of integer;',
  8909. ' TArrRec = array of TRec;',
  8910. ' TArrSet = array of TFlags;',
  8911. ' TArrJSValue = array of jsvalue;',
  8912. 'var',
  8913. ' ArrInt: tarrint;',
  8914. ' ArrRec: tarrrec;',
  8915. ' ArrSet: tarrset;',
  8916. ' ArrJSValue: tarrjsvalue;',
  8917. 'begin',
  8918. ' arrint:=copy(arrint);',
  8919. ' arrint:=copy(arrint,2);',
  8920. ' arrint:=copy(arrint,3,4);',
  8921. ' arrint:=copy([1,1],1,2);',
  8922. ' arrrec:=copy(arrrec);',
  8923. ' arrrec:=copy(arrrec,5);',
  8924. ' arrrec:=copy(arrrec,6,7);',
  8925. ' arrset:=copy(arrset);',
  8926. ' arrset:=copy(arrset,8);',
  8927. ' arrset:=copy(arrset,9,10);',
  8928. ' arrjsvalue:=copy(arrjsvalue);',
  8929. ' arrjsvalue:=copy(arrjsvalue,11);',
  8930. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  8931. ' ']);
  8932. ConvertProgram;
  8933. CheckSource('TestArray_Copy',
  8934. LinesToStr([ // statements
  8935. 'this.TFlag = {',
  8936. ' "0": "big",',
  8937. ' big: 0,',
  8938. ' "1": "small",',
  8939. ' small: 1',
  8940. '};',
  8941. 'rtl.recNewT($mod, "TRec", function () {',
  8942. ' this.i = 0;',
  8943. ' this.$eq = function (b) {',
  8944. ' return this.i === b.i;',
  8945. ' };',
  8946. ' this.$assign = function (s) {',
  8947. ' this.i = s.i;',
  8948. ' return this;',
  8949. ' };',
  8950. '});',
  8951. 'this.ArrInt = [];',
  8952. 'this.ArrRec = [];',
  8953. 'this.ArrSet = [];',
  8954. 'this.ArrJSValue = [];',
  8955. '']),
  8956. LinesToStr([ // $mod.$main
  8957. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  8958. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  8959. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  8960. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  8961. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  8962. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  8963. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  8964. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  8965. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  8966. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  8967. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  8968. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  8969. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  8970. '']));
  8971. end;
  8972. procedure TTestModule.TestArray_InsertDelete;
  8973. begin
  8974. StartProgram(false);
  8975. Add([
  8976. 'type',
  8977. ' integer = longint;',
  8978. ' TFlag = (big,small);',
  8979. ' TFlags = set of TFlag;',
  8980. ' TRec = record',
  8981. ' i: integer;',
  8982. ' end;',
  8983. ' TArrInt = array of integer;',
  8984. ' TArrRec = array of TRec;',
  8985. ' TArrSet = array of TFlags;',
  8986. ' TArrJSValue = array of jsvalue;',
  8987. ' TArrArrInt = array of TArrInt;',
  8988. 'var',
  8989. ' ArrInt: tarrint;',
  8990. ' ArrRec: tarrrec;',
  8991. ' ArrSet: tarrset;',
  8992. ' ArrJSValue: tarrjsvalue;',
  8993. ' ArrArrInt: TArrArrInt;',
  8994. 'begin',
  8995. ' Insert(1,arrint,2);',
  8996. ' Insert(arrint[3],arrint,4);',
  8997. ' Insert(arrrec[5],arrrec,6);',
  8998. ' Insert(arrset[7],arrset,7);',
  8999. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9000. ' Insert(10,arrjsvalue,11);',
  9001. ' Insert([23],arrarrint,22);',
  9002. ' Delete(arrint,12,13);',
  9003. ' Delete(arrrec,14,15);',
  9004. ' Delete(arrset,17,18);',
  9005. ' Delete(arrjsvalue,19,10);']);
  9006. ConvertProgram;
  9007. CheckSource('TestArray_InsertDelete',
  9008. LinesToStr([ // statements
  9009. 'this.TFlag = {',
  9010. ' "0": "big",',
  9011. ' big: 0,',
  9012. ' "1": "small",',
  9013. ' small: 1',
  9014. '};',
  9015. 'rtl.recNewT($mod, "TRec", function () {',
  9016. ' this.i = 0;',
  9017. ' this.$eq = function (b) {',
  9018. ' return this.i === b.i;',
  9019. ' };',
  9020. ' this.$assign = function (s) {',
  9021. ' this.i = s.i;',
  9022. ' return this;',
  9023. ' };',
  9024. '});',
  9025. 'this.ArrInt = [];',
  9026. 'this.ArrRec = [];',
  9027. 'this.ArrSet = [];',
  9028. 'this.ArrJSValue = [];',
  9029. 'this.ArrArrInt = [];',
  9030. '']),
  9031. LinesToStr([ // $mod.$main
  9032. '$mod.ArrInt.splice(2, 0, 1);',
  9033. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9034. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9035. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9036. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9037. '$mod.ArrJSValue.splice(11, 0, 10);',
  9038. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9039. '$mod.ArrInt.splice(12, 13);',
  9040. '$mod.ArrRec.splice(14, 15);',
  9041. '$mod.ArrSet.splice(17, 18);',
  9042. '$mod.ArrJSValue.splice(19, 10);',
  9043. '']));
  9044. end;
  9045. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9046. begin
  9047. StartProgram(false);
  9048. Add([
  9049. '{$modeswitch arrayoperators}',
  9050. 'type',
  9051. ' integer = longint;',
  9052. ' TArrInt = array of integer;',
  9053. ' TArrStr = array of string;',
  9054. 'const',
  9055. ' Ints: TArrInt = (1,2,3);',
  9056. ' Aliases: TarrStr = (''foo'',''b'');',
  9057. ' OneInt: TArrInt = (7);',
  9058. ' OneStr: array of integer = (7);',
  9059. ' Chars: array of char = ''aoc'';',
  9060. ' Names: array of string = (''a'',''foo'');',
  9061. ' NameCount = low(Names)+high(Names)+length(Names);',
  9062. 'var i: integer;',
  9063. 'begin',
  9064. ' Ints:=[];',
  9065. ' Ints:=[1,1];',
  9066. ' Ints:=[1]+[2];',
  9067. ' Ints:=[2];',
  9068. ' Ints:=[]+ints;',
  9069. ' Ints:=Ints+[];',
  9070. ' Ints:=Ints+OneInt;',
  9071. ' Ints:=Ints+[1,1];',
  9072. ' Ints:=[i,i]+Ints;',
  9073. ' Ints:=[1]+[i]+[3];',
  9074. '']);
  9075. ConvertProgram;
  9076. CheckSource('TestArray_DynArrayConstObjFPC',
  9077. LinesToStr([ // statements
  9078. 'this.Ints = [1, 2, 3];',
  9079. 'this.Aliases = ["foo", "b"];',
  9080. 'this.OneInt = [7];',
  9081. 'this.OneStr = [7];',
  9082. 'this.Chars = ["a", "o", "c"];',
  9083. 'this.Names = ["a", "foo"];',
  9084. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9085. 'this.i = 0;',
  9086. '']),
  9087. LinesToStr([ // $mod.$main
  9088. '$mod.Ints = [];',
  9089. '$mod.Ints = [1, 1];',
  9090. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9091. '$mod.Ints = [2];',
  9092. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9093. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9094. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9095. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9096. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9097. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9098. '']));
  9099. end;
  9100. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9101. begin
  9102. StartProgram(false);
  9103. // Note: const c = [1,1]; defines a set!
  9104. Add([
  9105. '{$mode delphi}',
  9106. 'type',
  9107. ' integer = longint;',
  9108. ' TArrInt = array of integer;',
  9109. ' TArrStr = array of string;',
  9110. 'const',
  9111. ' Ints: TArrInt = [1,1,2];',
  9112. ' Aliases: TarrStr = [''foo'',''b''];',
  9113. ' OneInt: TArrInt = [7];',
  9114. ' OneStr: array of integer = [7]+[8];',
  9115. ' Chars: array of char = ''aoc'';',
  9116. ' Names: array of string = [''a'',''a''];',
  9117. ' NameCount = low(Names)+high(Names)+length(Names);',
  9118. 'begin',
  9119. '']);
  9120. ConvertProgram;
  9121. CheckSource('TestArray_DynArrayConstDelphi',
  9122. LinesToStr([ // statements
  9123. 'this.Ints = [1, 1, 2];',
  9124. 'this.Aliases = ["foo", "b"];',
  9125. 'this.OneInt = [7];',
  9126. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9127. 'this.Chars = ["a", "o", "c"];',
  9128. 'this.Names = ["a", "a"];',
  9129. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9130. '']),
  9131. LinesToStr([ // $mod.$main
  9132. '']));
  9133. end;
  9134. procedure TTestModule.TestArray_ArrayLitAsParam;
  9135. begin
  9136. StartProgram(false);
  9137. Add([
  9138. '{$modeswitch arrayoperators}',
  9139. 'type',
  9140. ' integer = longint;',
  9141. ' TArrInt = array of integer;',
  9142. ' TArrSet = array of (red,green,blue);',
  9143. 'procedure DoOpenInt(a: array of integer); forward;',
  9144. 'procedure DoInt(a: TArrInt);',
  9145. 'begin',
  9146. ' DoInt(a+[1]);',
  9147. ' DoInt([1]+a);',
  9148. ' DoOpenInt(a);',
  9149. ' DoOpenInt(a+[1]);',
  9150. ' DoOpenInt([1]+a);',
  9151. 'end;',
  9152. 'procedure DoOpenInt(a: array of integer);',
  9153. 'begin',
  9154. ' DoOpenInt(a+[1]);',
  9155. ' DoOpenInt([1]+a);',
  9156. ' DoInt(a);',
  9157. ' DoInt(a+[1]);',
  9158. ' DoInt([1]+a);',
  9159. 'end;',
  9160. 'procedure DoSet(a: TArrSet);',
  9161. 'begin',
  9162. ' DoSet(a+[red]);',
  9163. ' DoSet([blue]+a);',
  9164. 'end;',
  9165. 'var',
  9166. ' i: TArrInt;',
  9167. ' s: TArrSet;',
  9168. 'begin',
  9169. ' DoInt([1]);',
  9170. ' DoInt([1]+[2]);',
  9171. ' DoInt(i+[1]);',
  9172. ' DoInt([1]+i);',
  9173. ' DoOpenInt([1]);',
  9174. ' DoOpenInt([1]+[2]);',
  9175. ' DoOpenInt(i+[1]);',
  9176. ' DoOpenInt([1]+i);',
  9177. ' DoSet([red]);',
  9178. ' DoSet([blue]+[green]);',
  9179. ' DoSet(s+[blue]);',
  9180. ' DoSet([red]+s);',
  9181. '']);
  9182. ConvertProgram;
  9183. CheckSource('TestArray_ArrayLitAsParam',
  9184. LinesToStr([ // statements
  9185. 'this.TArrSet$a = {',
  9186. ' "0": "red",',
  9187. ' red: 0,',
  9188. ' "1": "green",',
  9189. ' green: 1,',
  9190. ' "2": "blue",',
  9191. ' blue: 2',
  9192. '};',
  9193. 'this.DoInt = function (a) {',
  9194. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9195. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9196. ' $mod.DoOpenInt(a);',
  9197. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9198. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9199. '};',
  9200. 'this.DoOpenInt = function (a) {',
  9201. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9202. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9203. ' $mod.DoInt(a);',
  9204. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9205. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9206. '};',
  9207. 'this.DoSet = function (a) {',
  9208. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9209. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9210. '};',
  9211. 'this.i = [];',
  9212. 'this.s = [];',
  9213. '']),
  9214. LinesToStr([ // $mod.$main
  9215. '$mod.DoInt([1]);',
  9216. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9217. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9218. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9219. '$mod.DoOpenInt([1]);',
  9220. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9221. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9222. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9223. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9224. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9225. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9226. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9227. '']));
  9228. end;
  9229. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9230. begin
  9231. StartProgram(false);
  9232. Add([
  9233. '{$modeswitch arrayoperators}',
  9234. 'type',
  9235. ' integer = longint;',
  9236. ' TArrInt = array of integer;',
  9237. ' TArrArrInt = array of TArrInt;',
  9238. 'procedure DoInt(a: TArrArrInt);',
  9239. 'begin',
  9240. ' DoInt(a+[[1]]);',
  9241. ' DoInt([[1]]+a);',
  9242. ' DoInt(a);',
  9243. 'end;',
  9244. 'var',
  9245. ' i: TArrInt;',
  9246. ' a: TArrArrInt;',
  9247. 'begin',
  9248. ' a:=[[1]];',
  9249. ' a:=[i];',
  9250. ' a:=a+[i];',
  9251. ' a:=[i]+a;',
  9252. ' a:=[[1]+i];',
  9253. ' a:=[[1]+[2]];',
  9254. ' a:=[i+[2]];',
  9255. ' DoInt([[1]]);',
  9256. ' DoInt([[1]+[2],[3,4],[5]]);',
  9257. ' DoInt([i+[1]]+a);',
  9258. ' DoInt([i]+a);',
  9259. '']);
  9260. ConvertProgram;
  9261. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9262. LinesToStr([ // statements
  9263. 'this.DoInt = function (a) {',
  9264. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9265. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9266. ' $mod.DoInt(a);',
  9267. '};',
  9268. 'this.i = [];',
  9269. 'this.a = [];',
  9270. '']),
  9271. LinesToStr([ // $mod.$main
  9272. '$mod.a = [[1]];',
  9273. '$mod.a = [$mod.i];',
  9274. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9275. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9276. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9277. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9278. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9279. '$mod.DoInt([[1]]);',
  9280. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9281. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  9282. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  9283. '']));
  9284. end;
  9285. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  9286. begin
  9287. StartProgram(false);
  9288. Add([
  9289. '{$modeswitch arrayoperators}',
  9290. 'type',
  9291. ' integer = longint;',
  9292. ' TArrInt = array[1..2] of integer;',
  9293. ' TArrArrInt = array of TArrInt;',
  9294. 'procedure DoInt(a: TArrArrInt);',
  9295. 'begin',
  9296. ' DoInt(a+[[1,2]]);',
  9297. ' DoInt([[1,2]]+a);',
  9298. ' DoInt(a);',
  9299. 'end;',
  9300. 'var',
  9301. ' i: TArrInt;',
  9302. ' a: TArrArrInt;',
  9303. 'begin',
  9304. ' a:=[[1,1]];',
  9305. ' a:=[i];',
  9306. ' a:=a+[i];',
  9307. ' a:=[i]+a;',
  9308. ' DoInt([[1,1]]);',
  9309. ' DoInt([[1,2],[3,4]]);',
  9310. '']);
  9311. ConvertProgram;
  9312. CheckSource('TestArray_ArrayLitStaticAsParam',
  9313. LinesToStr([ // statements
  9314. 'this.DoInt = function (a) {',
  9315. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  9316. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  9317. ' $mod.DoInt(a);',
  9318. '};',
  9319. 'this.i = rtl.arraySetLength(null, 0, 2);',
  9320. 'this.a = [];',
  9321. '']),
  9322. LinesToStr([ // $mod.$main
  9323. '$mod.a = [[1, 1]];',
  9324. '$mod.a = [$mod.i.slice(0)];',
  9325. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  9326. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  9327. '$mod.DoInt([[1, 1]]);',
  9328. '$mod.DoInt([[1, 2], [3, 4]]);',
  9329. '']));
  9330. end;
  9331. procedure TTestModule.TestArray_ForInArrOfString;
  9332. begin
  9333. StartProgram(false);
  9334. Add([
  9335. 'type',
  9336. 'type',
  9337. ' TMonthNameArray = array [1..12] of string;',
  9338. ' TMonthNames = TMonthNameArray;',
  9339. ' TObject = class',
  9340. ' private',
  9341. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  9342. ' public',
  9343. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9344. ' end;',
  9345. 'var f: TObject;',
  9346. ' Month: string;',
  9347. 'begin',
  9348. ' for Month in f.LongMonthNames do ;',
  9349. '']);
  9350. ConvertProgram;
  9351. CheckSource('TestArray_ForInArrOfString',
  9352. LinesToStr([ // statements
  9353. 'rtl.createClass($mod, "TObject", null, function () {',
  9354. ' this.$init = function () {',
  9355. ' };',
  9356. ' this.$final = function () {',
  9357. ' };',
  9358. '});',
  9359. 'this.f = null;',
  9360. 'this.Month = "";',
  9361. '']),
  9362. LinesToStr([ // $mod.$main
  9363. 'for (var $in1 = $mod.f.GetLongMonthNames(), $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.Month = $in1[$l2];',
  9364. '']));
  9365. end;
  9366. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9367. begin
  9368. StartProgram(false);
  9369. Add([
  9370. '{$modeswitch externalclass}',
  9371. 'type',
  9372. ' TJSObject = class external name ''Object''',
  9373. ' end;',
  9374. ' TJSArray = class external name ''Array''',
  9375. ' class function isArray(Value: JSValue) : boolean;',
  9376. ' function concat() : TJSArray; varargs;',
  9377. ' end;',
  9378. 'var',
  9379. ' aObj: TJSArray;',
  9380. ' a: array of longint;',
  9381. ' o: TJSObject;',
  9382. 'begin',
  9383. ' if TJSArray.isArray(65) then ;',
  9384. ' aObj:=TJSArray(a).concat(a);',
  9385. ' o:=TJSObject(a);']);
  9386. ConvertProgram;
  9387. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  9388. LinesToStr([ // statements
  9389. 'this.aObj = null;',
  9390. 'this.a = [];',
  9391. 'this.o = null;',
  9392. '']),
  9393. LinesToStr([ // $mod.$main
  9394. 'if (Array.isArray(65)) ;',
  9395. '$mod.aObj = $mod.a.concat($mod.a);',
  9396. '$mod.o = $mod.a;',
  9397. '']));
  9398. end;
  9399. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  9400. begin
  9401. StartProgram(false);
  9402. Add([
  9403. '{$modeswitch externalclass}',
  9404. 'type',
  9405. ' TArrStr = array of string;',
  9406. ' TJSArray = class external name ''Array''',
  9407. ' end;',
  9408. ' TJSObject = class external name ''Object''',
  9409. ' end;',
  9410. 'var',
  9411. ' aObj: TJSArray;',
  9412. ' a: TArrStr;',
  9413. ' jo: TJSObject;',
  9414. 'begin',
  9415. ' a:=TArrStr(aObj);',
  9416. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  9417. ' a:=TarrStr(jo);',
  9418. '']);
  9419. ConvertProgram;
  9420. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  9421. LinesToStr([ // statements
  9422. 'this.aObj = null;',
  9423. 'this.a = [];',
  9424. 'this.jo = null;',
  9425. '']),
  9426. LinesToStr([ // $mod.$main
  9427. '$mod.a = $mod.aObj;',
  9428. '$mod.aObj[1] = $mod.aObj[2];',
  9429. '$mod.a = $mod.jo;',
  9430. '']));
  9431. end;
  9432. procedure TTestModule.TestArrayOfConst_TVarRec;
  9433. begin
  9434. StartProgram(true,[supTVarRec]);
  9435. Add([
  9436. 'procedure Say(args: array of const);',
  9437. 'var',
  9438. ' i: longint;',
  9439. ' v: TVarRec;',
  9440. 'begin',
  9441. ' for i:=low(args) to high(args) do begin',
  9442. ' v:=args[i];',
  9443. ' case v.vtype of',
  9444. ' vtInteger: if length(args)=args[i].vInteger then ;',
  9445. ' end;',
  9446. ' end;',
  9447. ' for v in args do ;',
  9448. ' args:=nil;',
  9449. ' SetLength(args,2);',
  9450. 'end;',
  9451. 'begin']);
  9452. ConvertProgram;
  9453. CheckSource('TestArrayOfConst_TVarRec',
  9454. LinesToStr([ // statements
  9455. 'this.Say = function (args) {',
  9456. ' var i = 0;',
  9457. ' var v = pas.system.TVarRec.$new();',
  9458. ' for (var $l1 = 0, $end2 = rtl.length(args) - 1; $l1 <= $end2; $l1++) {',
  9459. ' i = $l1;',
  9460. ' v.$assign(args[i]);',
  9461. ' var $tmp3 = v.VType;',
  9462. ' if ($tmp3 === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  9463. ' };',
  9464. ' for (var $in4 = args, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) v = $in4[$l5];',
  9465. ' args = [];',
  9466. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  9467. '};',
  9468. '']),
  9469. LinesToStr([ // $mod.$main
  9470. ]));
  9471. end;
  9472. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  9473. begin
  9474. StartProgram(true,[supTVarRec]);
  9475. Add([
  9476. 'procedure Say(args: array of const);',
  9477. 'begin',
  9478. ' Say(args);',
  9479. 'end;',
  9480. 'var',
  9481. ' p: Pointer;',
  9482. ' j: jsvalue;',
  9483. ' c: currency;',
  9484. 'begin',
  9485. ' Say([]);',
  9486. ' Say([1]);',
  9487. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  9488. '']);
  9489. ConvertProgram;
  9490. CheckSource('TestArrayOfConst_PassBaseTypes',
  9491. LinesToStr([ // statements
  9492. 'this.Say = function (args) {',
  9493. ' $mod.Say(args);',
  9494. '};',
  9495. 'this.p = null;',
  9496. 'this.j = undefined;',
  9497. 'this.c = 0;',
  9498. '']),
  9499. LinesToStr([ // $mod.$main
  9500. '$mod.Say([]);',
  9501. '$mod.Say(pas.system.VarRecs(0, 1));',
  9502. '$mod.Say(pas.system.VarRecs(',
  9503. ' 9,',
  9504. ' "c",',
  9505. ' 18,',
  9506. ' "foo",',
  9507. ' 5,',
  9508. ' null,',
  9509. ' 1,',
  9510. ' true,',
  9511. ' 3,',
  9512. ' 1.3,',
  9513. ' 5,',
  9514. ' $mod.p,',
  9515. ' 20,',
  9516. ' $mod.j,',
  9517. ' 12,',
  9518. ' $mod.c',
  9519. ' ));',
  9520. '']));
  9521. end;
  9522. procedure TTestModule.TestArrayOfConst_PassObj;
  9523. begin
  9524. StartProgram(true,[supTVarRec]);
  9525. Add([
  9526. '{$interfaces corba}',
  9527. 'type',
  9528. ' TObject = class',
  9529. ' end;',
  9530. ' TClass = class of TObject;',
  9531. ' IUnknown = interface',
  9532. ' end;',
  9533. 'procedure Say(args: array of const);',
  9534. 'begin',
  9535. 'end;',
  9536. 'var',
  9537. ' o: TObject;',
  9538. ' c: TClass;',
  9539. ' i: IUnknown;',
  9540. 'begin',
  9541. ' Say([o,c,TObject]);',
  9542. ' Say([nil,i]);',
  9543. '']);
  9544. ConvertProgram;
  9545. CheckSource('TestArrayOfConst_PassObj',
  9546. LinesToStr([ // statements
  9547. 'rtl.createClass($mod, "TObject", null, function () {',
  9548. ' this.$init = function () {',
  9549. ' };',
  9550. ' this.$final = function () {',
  9551. ' };',
  9552. '});',
  9553. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  9554. 'this.Say = function (args) {',
  9555. '};',
  9556. 'this.o = null;',
  9557. 'this.c = null;',
  9558. 'this.i = null;',
  9559. '']),
  9560. LinesToStr([ // $mod.$main
  9561. '$mod.Say(pas.system.VarRecs(',
  9562. ' 7,',
  9563. ' $mod.o,',
  9564. ' 8,',
  9565. ' $mod.c,',
  9566. ' 8,',
  9567. ' $mod.TObject',
  9568. '));',
  9569. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  9570. '']));
  9571. end;
  9572. procedure TTestModule.TestRecord_Empty;
  9573. begin
  9574. StartProgram(false);
  9575. Add([
  9576. 'type',
  9577. ' TRecA = record',
  9578. ' end;',
  9579. 'var a,b: TRecA;',
  9580. 'begin',
  9581. ' if a=b then ;']);
  9582. ConvertProgram;
  9583. CheckSource('TestRecord_Empty',
  9584. LinesToStr([ // statements
  9585. 'rtl.recNewT($mod, "TRecA", function () {',
  9586. ' this.$eq = function (b) {',
  9587. ' return true;',
  9588. ' };',
  9589. ' this.$assign = function (s) {',
  9590. ' return this;',
  9591. ' };',
  9592. '});',
  9593. 'this.a = $mod.TRecA.$new();',
  9594. 'this.b = $mod.TRecA.$new();',
  9595. '']),
  9596. LinesToStr([ // $mod.$main
  9597. 'if ($mod.a.$eq($mod.b)) ;'
  9598. ]));
  9599. end;
  9600. procedure TTestModule.TestRecord_Var;
  9601. begin
  9602. StartProgram(false);
  9603. Add('type');
  9604. Add(' TRecA = record');
  9605. Add(' Bold: longint;');
  9606. Add(' end;');
  9607. Add('var Rec: TRecA;');
  9608. Add('begin');
  9609. Add(' rec.bold:=123');
  9610. ConvertProgram;
  9611. CheckSource('TestRecord_Var',
  9612. LinesToStr([ // statements
  9613. 'rtl.recNewT($mod, "TRecA", function () {',
  9614. ' this.Bold = 0;',
  9615. ' this.$eq = function (b) {',
  9616. ' return this.Bold === b.Bold;',
  9617. ' };',
  9618. ' this.$assign = function (s) {',
  9619. ' this.Bold = s.Bold;',
  9620. ' return this;',
  9621. ' };',
  9622. '});',
  9623. 'this.Rec = $mod.TRecA.$new();',
  9624. '']),
  9625. LinesToStr([ // $mod.$main
  9626. '$mod.Rec.Bold = 123;'
  9627. ]));
  9628. end;
  9629. procedure TTestModule.TestRecord_VarExternal;
  9630. begin
  9631. StartProgram(false);
  9632. Add([
  9633. '{$modeswitch externalclass}',
  9634. 'type',
  9635. ' TRecA = record',
  9636. ' i: byte;',
  9637. ' length_: longint external name ''length'';',
  9638. ' end;',
  9639. 'var Rec: TRecA;',
  9640. 'begin',
  9641. ' rec.length_ := rec.length_',
  9642. '']);
  9643. ConvertProgram;
  9644. CheckSource('TestRecord_VarExternal',
  9645. LinesToStr([ // statements
  9646. 'rtl.recNewT($mod, "TRecA", function () {',
  9647. ' this.i = 0;',
  9648. ' this.$eq = function (b) {',
  9649. ' return (this.i === b.i) && (this.length === b.length);',
  9650. ' };',
  9651. ' this.$assign = function (s) {',
  9652. ' this.i = s.i;',
  9653. ' this.length = s.length;',
  9654. ' return this;',
  9655. ' };',
  9656. '});',
  9657. 'this.Rec = $mod.TRecA.$new();',
  9658. '']),
  9659. LinesToStr([ // $mod.$main
  9660. '$mod.Rec.length = $mod.Rec.length;'
  9661. ]));
  9662. end;
  9663. procedure TTestModule.TestRecord_WithDo;
  9664. begin
  9665. StartProgram(false);
  9666. Add('type');
  9667. Add(' TRec = record');
  9668. Add(' vI: longint;');
  9669. Add(' end;');
  9670. Add('var');
  9671. Add(' Int: longint;');
  9672. Add(' r: TRec;');
  9673. Add('begin');
  9674. Add(' with r do');
  9675. Add(' int:=vi;');
  9676. Add(' with r do begin');
  9677. Add(' int:=vi;');
  9678. Add(' vi:=int;');
  9679. Add(' end;');
  9680. ConvertProgram;
  9681. CheckSource('TestWithRecordDo',
  9682. LinesToStr([ // statements
  9683. 'rtl.recNewT($mod, "TRec", function () {',
  9684. ' this.vI = 0;',
  9685. ' this.$eq = function (b) {',
  9686. ' return this.vI === b.vI;',
  9687. ' };',
  9688. ' this.$assign = function (s) {',
  9689. ' this.vI = s.vI;',
  9690. ' return this;',
  9691. ' };',
  9692. '});',
  9693. 'this.Int = 0;',
  9694. 'this.r = $mod.TRec.$new();',
  9695. '']),
  9696. LinesToStr([ // $mod.$main
  9697. 'var $with1 = $mod.r;',
  9698. '$mod.Int = $with1.vI;',
  9699. 'var $with2 = $mod.r;',
  9700. '$mod.Int = $with2.vI;',
  9701. '$with2.vI = $mod.Int;'
  9702. ]));
  9703. end;
  9704. procedure TTestModule.TestRecord_Assign;
  9705. begin
  9706. StartProgram(false);
  9707. Add('type');
  9708. Add(' TEnum = (red,green);');
  9709. Add(' TEnums = set of TEnum;');
  9710. Add(' TSmallRec = record');
  9711. Add(' N: longint;');
  9712. Add(' end;');
  9713. Add(' TBigRec = record');
  9714. Add(' Int: longint;');
  9715. Add(' D: double;');
  9716. Add(' Arr: array of longint;');
  9717. Add(' Arr2: array[1..2] of longint;');
  9718. Add(' Small: TSmallRec;');
  9719. Add(' Enums: TEnums;');
  9720. Add(' end;');
  9721. Add('var');
  9722. Add(' r, s: TBigRec;');
  9723. Add('begin');
  9724. Add(' r:=s;');
  9725. Add(' r:=default(TBigRec);');
  9726. Add(' r:=default(s);');
  9727. ConvertProgram;
  9728. CheckSource('TestRecord_Assign',
  9729. LinesToStr([ // statements
  9730. 'this.TEnum = {',
  9731. ' "0": "red",',
  9732. ' red: 0,',
  9733. ' "1": "green",',
  9734. ' green: 1',
  9735. '};',
  9736. 'rtl.recNewT($mod, "TSmallRec", function () {',
  9737. ' this.N = 0;',
  9738. ' this.$eq = function (b) {',
  9739. ' return this.N === b.N;',
  9740. ' };',
  9741. ' this.$assign = function (s) {',
  9742. ' this.N = s.N;',
  9743. ' return this;',
  9744. ' };',
  9745. '});',
  9746. 'rtl.recNewT($mod, "TBigRec", function () {',
  9747. ' this.Int = 0;',
  9748. ' this.D = 0.0;',
  9749. ' this.Arr = [];',
  9750. ' this.$new = function () {',
  9751. ' var r = Object.create(this);',
  9752. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  9753. ' r.Small = $mod.TSmallRec.$new();',
  9754. ' r.Enums = {};',
  9755. ' return r;',
  9756. ' };',
  9757. ' this.$eq = function (b) {',
  9758. ' 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);',
  9759. ' };',
  9760. ' this.$assign = function (s) {',
  9761. ' this.Int = s.Int;',
  9762. ' this.D = s.D;',
  9763. ' this.Arr = s.Arr;',
  9764. ' this.Arr2 = s.Arr2.slice(0);',
  9765. ' this.Small.$assign(s.Small);',
  9766. ' this.Enums = rtl.refSet(s.Enums);',
  9767. ' return this;',
  9768. ' };',
  9769. '});',
  9770. 'this.r = $mod.TBigRec.$new();',
  9771. 'this.s = $mod.TBigRec.$new();',
  9772. '']),
  9773. LinesToStr([ // $mod.$main
  9774. '$mod.r.$assign($mod.s);',
  9775. '$mod.r.$assign($mod.TBigRec.$new());',
  9776. '$mod.r.$assign($mod.TBigRec.$new());',
  9777. '']));
  9778. end;
  9779. procedure TTestModule.TestRecord_AsParams;
  9780. begin
  9781. StartProgram(false);
  9782. Add([
  9783. 'type',
  9784. ' integer = longint;',
  9785. ' TRecord = record',
  9786. ' i: integer;',
  9787. ' end;',
  9788. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  9789. 'var vL: TRecord;',
  9790. 'begin',
  9791. ' vd:=vd;',
  9792. ' vd.i:=vd.i;',
  9793. ' vl:=vc;',
  9794. ' vv:=vv;',
  9795. ' vv.i:=vv.i;',
  9796. ' U:=vl;',
  9797. ' U:=vd;',
  9798. ' U:=vc;',
  9799. ' U:=vv;',
  9800. ' doit(vd,vd,vd,vd);',
  9801. ' doit(vc,vc,vl,vl);',
  9802. ' doit(vv,vv,vv,vv);',
  9803. ' doit(vl,vl,vl,vl);',
  9804. 'end;',
  9805. 'var i: TRecord;',
  9806. 'begin',
  9807. ' doit(i,i,i,i);']);
  9808. ConvertProgram;
  9809. CheckSource('TestRecord_AsParams',
  9810. LinesToStr([ // statements
  9811. 'rtl.recNewT($mod, "TRecord", function () {',
  9812. ' this.i = 0;',
  9813. ' this.$eq = function (b) {',
  9814. ' return this.i === b.i;',
  9815. ' };',
  9816. ' this.$assign = function (s) {',
  9817. ' this.i = s.i;',
  9818. ' return this;',
  9819. ' };',
  9820. '});',
  9821. 'this.DoIt = function (vD, vC, vV, U) {',
  9822. ' var vL = $mod.TRecord.$new();',
  9823. ' vD.$assign(vD);',
  9824. ' vD.i = vD.i;',
  9825. ' vL.$assign(vC);',
  9826. ' vV.$assign(vV);',
  9827. ' vV.i = vV.i;',
  9828. ' U.set(vL);',
  9829. ' U.set(vD);',
  9830. ' U.set(vC);',
  9831. ' U.set(vV);',
  9832. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, {',
  9833. ' get: function () {',
  9834. ' return vD;',
  9835. ' },',
  9836. ' set: function (v) {',
  9837. ' vD.$assign(v);',
  9838. ' }',
  9839. ' });',
  9840. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, {',
  9841. ' get: function () {',
  9842. ' return vL;',
  9843. ' },',
  9844. ' set: function (v) {',
  9845. ' vL.$assign(v);',
  9846. ' }',
  9847. ' });',
  9848. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, {',
  9849. ' get: function () {',
  9850. ' return vV;',
  9851. ' },',
  9852. ' set: function (v) {',
  9853. ' vV.$assign(v);',
  9854. ' }',
  9855. ' });',
  9856. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, {',
  9857. ' get: function () {',
  9858. ' return vL;',
  9859. ' },',
  9860. ' set: function (v) {',
  9861. ' vL.$assign(v);',
  9862. ' }',
  9863. ' });',
  9864. '};',
  9865. 'this.i = $mod.TRecord.$new();'
  9866. ]),
  9867. LinesToStr([
  9868. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, {',
  9869. ' p: $mod,',
  9870. ' get: function () {',
  9871. ' return this.p.i;',
  9872. ' },',
  9873. ' set: function (v) {',
  9874. ' this.p.i.$assign(v);',
  9875. ' }',
  9876. '});',
  9877. '']));
  9878. end;
  9879. procedure TTestModule.TestRecordElement_AsParams;
  9880. begin
  9881. StartProgram(false);
  9882. Add('type');
  9883. Add(' integer = longint;');
  9884. Add(' TRecord = record');
  9885. Add(' i: integer;');
  9886. Add(' end;');
  9887. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9888. Add('var vJ: TRecord;');
  9889. Add('begin');
  9890. Add(' doit(vj.i,vj.i,vj.i);');
  9891. Add('end;');
  9892. Add('var r: TRecord;');
  9893. Add('begin');
  9894. Add(' doit(r.i,r.i,r.i);');
  9895. ConvertProgram;
  9896. CheckSource('TestRecordElement_AsParams',
  9897. LinesToStr([ // statements
  9898. 'rtl.recNewT($mod, "TRecord", function () {',
  9899. ' this.i = 0;',
  9900. ' this.$eq = function (b) {',
  9901. ' return this.i === b.i;',
  9902. ' };',
  9903. ' this.$assign = function (s) {',
  9904. ' this.i = s.i;',
  9905. ' return this;',
  9906. ' };',
  9907. '});',
  9908. 'this.DoIt = function (vG,vH,vI) {',
  9909. ' var vJ = $mod.TRecord.$new();',
  9910. ' $mod.DoIt(vJ.i, vJ.i, {',
  9911. ' p: vJ,',
  9912. ' get: function () {',
  9913. ' return this.p.i;',
  9914. ' },',
  9915. ' set: function (v) {',
  9916. ' this.p.i = v;',
  9917. ' }',
  9918. ' });',
  9919. '};',
  9920. 'this.r = $mod.TRecord.$new();'
  9921. ]),
  9922. LinesToStr([
  9923. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  9924. ' p: $mod.r,',
  9925. ' get: function () {',
  9926. ' return this.p.i;',
  9927. ' },',
  9928. ' set: function (v) {',
  9929. ' this.p.i = v;',
  9930. ' }',
  9931. '});'
  9932. ]));
  9933. end;
  9934. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  9935. begin
  9936. StartProgram(false);
  9937. Add('type');
  9938. Add(' integer = longint;');
  9939. Add(' TRecord = record');
  9940. Add(' i: integer;');
  9941. Add(' end;');
  9942. Add('function GetRec(vB: integer = 0): TRecord;');
  9943. Add('begin');
  9944. Add('end;');
  9945. Add('procedure DoIt(vG: integer; const vH: integer);');
  9946. Add('begin');
  9947. Add('end;');
  9948. Add('begin');
  9949. Add(' doit(getrec.i,getrec.i);');
  9950. Add(' doit(getrec().i,getrec().i);');
  9951. Add(' doit(getrec(1).i,getrec(2).i);');
  9952. ConvertProgram;
  9953. CheckSource('TestRecordElementFromFuncResult_AsParams',
  9954. LinesToStr([ // statements
  9955. 'rtl.recNewT($mod, "TRecord", function () {',
  9956. ' this.i = 0;',
  9957. ' this.$eq = function (b) {',
  9958. ' return this.i === b.i;',
  9959. ' };',
  9960. ' this.$assign = function (s) {',
  9961. ' this.i = s.i;',
  9962. ' return this;',
  9963. ' };',
  9964. '});',
  9965. 'this.GetRec = function (vB) {',
  9966. ' var Result = $mod.TRecord.$new();',
  9967. ' return Result;',
  9968. '};',
  9969. 'this.DoIt = function (vG, vH) {',
  9970. '};',
  9971. '']),
  9972. LinesToStr([
  9973. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  9974. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  9975. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  9976. '']));
  9977. end;
  9978. procedure TTestModule.TestRecordElementFromWith_AsParams;
  9979. begin
  9980. StartProgram(false);
  9981. Add('type');
  9982. Add(' integer = longint;');
  9983. Add(' TRecord = record');
  9984. Add(' i: integer;');
  9985. Add(' end;');
  9986. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9987. Add('begin');
  9988. Add('end;');
  9989. Add('var r: trecord;');
  9990. Add('begin');
  9991. Add(' with r do ');
  9992. Add(' doit(i,i,i);');
  9993. ConvertProgram;
  9994. CheckSource('TestRecordElementFromWith_AsParams',
  9995. LinesToStr([ // statements
  9996. 'rtl.recNewT($mod, "TRecord", function () {',
  9997. ' this.i = 0;',
  9998. ' this.$eq = function (b) {',
  9999. ' return this.i === b.i;',
  10000. ' };',
  10001. ' this.$assign = function (s) {',
  10002. ' this.i = s.i;',
  10003. ' return this;',
  10004. ' };',
  10005. '});',
  10006. 'this.DoIt = function (vG,vH,vI) {',
  10007. '};',
  10008. 'this.r = $mod.TRecord.$new();'
  10009. ]),
  10010. LinesToStr([
  10011. 'var $with1 = $mod.r;',
  10012. '$mod.DoIt($with1.i,$with1.i,{',
  10013. ' p: $with1,',
  10014. ' get: function () {',
  10015. ' return this.p.i;',
  10016. ' },',
  10017. ' set: function (v) {',
  10018. ' this.p.i = v;',
  10019. ' }',
  10020. '});',
  10021. '']));
  10022. end;
  10023. procedure TTestModule.TestRecord_Equal;
  10024. begin
  10025. StartProgram(false);
  10026. Add('type');
  10027. Add(' integer = longint;');
  10028. Add(' TFlag = (red,blue);');
  10029. Add(' TFlags = set of TFlag;');
  10030. Add(' TProc = procedure;');
  10031. Add(' TRecord = record');
  10032. Add(' i: integer;');
  10033. Add(' Event: TProc;');
  10034. Add(' f: TFlags;');
  10035. Add(' end;');
  10036. Add(' TNested = record');
  10037. Add(' r: TRecord;');
  10038. Add(' end;');
  10039. Add('var');
  10040. Add(' b: boolean;');
  10041. Add(' r,s: trecord;');
  10042. Add('begin');
  10043. Add(' b:=r=s;');
  10044. Add(' b:=r<>s;');
  10045. ConvertProgram;
  10046. CheckSource('TestRecord_Equal',
  10047. LinesToStr([ // statements
  10048. 'this.TFlag = {',
  10049. ' "0": "red",',
  10050. ' red: 0,',
  10051. ' "1": "blue",',
  10052. ' blue: 1',
  10053. '};',
  10054. 'rtl.recNewT($mod, "TRecord", function () {',
  10055. ' this.i = 0;',
  10056. ' this.Event = null;',
  10057. ' this.$new = function () {',
  10058. ' var r = Object.create(this);',
  10059. ' r.f = {};',
  10060. ' return r;',
  10061. ' };',
  10062. ' this.$eq = function (b) {',
  10063. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  10064. ' };',
  10065. ' this.$assign = function (s) {',
  10066. ' this.i = s.i;',
  10067. ' this.Event = s.Event;',
  10068. ' this.f = rtl.refSet(s.f);',
  10069. ' return this;',
  10070. ' };',
  10071. '});',
  10072. 'rtl.recNewT($mod, "TNested", function () {',
  10073. ' this.$new = function () {',
  10074. ' var r = Object.create(this);',
  10075. ' r.r = $mod.TRecord.$new();',
  10076. ' return r;',
  10077. ' };',
  10078. ' this.$eq = function (b) {',
  10079. ' return this.r.$eq(b.r);',
  10080. ' };',
  10081. ' this.$assign = function (s) {',
  10082. ' this.r.$assign(s.r);',
  10083. ' return this;',
  10084. ' };',
  10085. '});',
  10086. 'this.b = false;',
  10087. 'this.r = $mod.TRecord.$new();',
  10088. 'this.s = $mod.TRecord.$new();',
  10089. '']),
  10090. LinesToStr([
  10091. '$mod.b = $mod.r.$eq($mod.s);',
  10092. '$mod.b = !$mod.r.$eq($mod.s);',
  10093. '']));
  10094. end;
  10095. procedure TTestModule.TestRecord_TypeCastJSValueToRecord;
  10096. begin
  10097. StartProgram(false);
  10098. Add('type');
  10099. Add(' TRecord = record');
  10100. Add(' i: longint;');
  10101. Add(' end;');
  10102. Add('var');
  10103. Add(' Jv: jsvalue;');
  10104. Add(' Rec: trecord;');
  10105. Add('begin');
  10106. Add(' rec:=trecord(jv);');
  10107. ConvertProgram;
  10108. CheckSource('TestRecord_TypeCastJSValueToRecord',
  10109. LinesToStr([ // statements
  10110. 'rtl.recNewT($mod, "TRecord", function () {',
  10111. ' this.i = 0;',
  10112. ' this.$eq = function (b) {',
  10113. ' return this.i === b.i;',
  10114. ' };',
  10115. ' this.$assign = function (s) {',
  10116. ' this.i = s.i;',
  10117. ' return this;',
  10118. ' };',
  10119. '});',
  10120. 'this.Jv = undefined;',
  10121. 'this.Rec = $mod.TRecord.$new();',
  10122. '']),
  10123. LinesToStr([
  10124. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  10125. '']));
  10126. end;
  10127. procedure TTestModule.TestRecord_VariantFail;
  10128. begin
  10129. StartProgram(false);
  10130. Add([
  10131. 'type',
  10132. ' TRec = record',
  10133. ' case word of',
  10134. ' 0: (b0, b1: Byte);',
  10135. ' 1: (i: word);',
  10136. ' end;',
  10137. 'begin']);
  10138. SetExpectedPasResolverError('variant record is not supported',
  10139. nXIsNotSupported);
  10140. ConvertProgram;
  10141. end;
  10142. procedure TTestModule.TestRecord_FieldArray;
  10143. begin
  10144. StartProgram(false);
  10145. Add([
  10146. 'type',
  10147. ' TArrInt = array[3..4] of longint;',
  10148. ' TArrArrInt = array[3..4] of longint;',
  10149. ' TRec = record',
  10150. ' a: array of longint;',
  10151. ' s: array[1..2] of longint;',
  10152. ' m: array[1..2,3..4] of longint;',
  10153. ' o: TArrArrInt;',
  10154. ' end;',
  10155. 'begin']);
  10156. ConvertProgram;
  10157. CheckSource('TestRecord_FieldArray',
  10158. LinesToStr([ // statements
  10159. 'rtl.recNewT($mod, "TRec", function () {',
  10160. ' this.a = [];',
  10161. ' this.$new = function () {',
  10162. ' var r = Object.create(this);',
  10163. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10164. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10165. ' r.o = rtl.arraySetLength(null, 0, 2);',
  10166. ' return r;',
  10167. ' };',
  10168. ' this.$eq = function (b) {',
  10169. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  10170. ' };',
  10171. ' this.$assign = function (s) {',
  10172. ' this.a = s.a;',
  10173. ' this.s = s.s.slice(0);',
  10174. ' this.m = s.m.slice(0);',
  10175. ' this.o = s.o.slice(0);',
  10176. ' return this;',
  10177. ' };',
  10178. '});',
  10179. '']),
  10180. LinesToStr([ // $mod.$main
  10181. '']));
  10182. end;
  10183. procedure TTestModule.TestRecord_Const;
  10184. begin
  10185. StartProgram(false);
  10186. Add([
  10187. 'type',
  10188. ' TArrInt = array[3..4] of longint;',
  10189. ' TPoint = record x,y: longint; end;',
  10190. ' TRec = record',
  10191. ' i: longint;',
  10192. ' a: array of longint;',
  10193. ' s: array[1..2] of longint;',
  10194. ' m: array[1..2,3..4] of longint;',
  10195. ' p: TPoint;',
  10196. ' end;',
  10197. ' TPoints = array of TPoint;',
  10198. 'const',
  10199. ' r: TRec = (',
  10200. ' i:1;',
  10201. ' a:(2,3);',
  10202. ' s:(4,5);',
  10203. ' m:( (11,12), (13,14) );',
  10204. ' p: (x:21; y:22)',
  10205. ' );',
  10206. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10207. 'begin']);
  10208. ConvertProgram;
  10209. CheckSource('TestRecord_Const',
  10210. LinesToStr([ // statements
  10211. 'rtl.recNewT($mod, "TPoint", function () {',
  10212. ' this.x = 0;',
  10213. ' this.y = 0;',
  10214. ' this.$eq = function (b) {',
  10215. ' return (this.x === b.x) && (this.y === b.y);',
  10216. ' };',
  10217. ' this.$assign = function (s) {',
  10218. ' this.x = s.x;',
  10219. ' this.y = s.y;',
  10220. ' return this;',
  10221. ' };',
  10222. '});',
  10223. 'rtl.recNewT($mod, "TRec", function () {',
  10224. ' this.i = 0;',
  10225. ' this.a = [];',
  10226. ' this.$new = function () {',
  10227. ' var r = Object.create(this);',
  10228. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10229. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10230. ' r.p = $mod.TPoint.$new();',
  10231. ' return r;',
  10232. ' };',
  10233. ' this.$eq = function (b) {',
  10234. ' 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);',
  10235. ' };',
  10236. ' this.$assign = function (s) {',
  10237. ' this.i = s.i;',
  10238. ' this.a = s.a;',
  10239. ' this.s = s.s.slice(0);',
  10240. ' this.m = s.m.slice(0);',
  10241. ' this.p.$assign(s.p);',
  10242. ' return this;',
  10243. ' };',
  10244. '});',
  10245. 'this.r = $mod.TRec.$clone({',
  10246. ' i: 1,',
  10247. ' a: [2, 3],',
  10248. ' s: [4, 5],',
  10249. ' m: [[11, 12], [13, 14]],',
  10250. ' p: $mod.TPoint.$clone({',
  10251. ' x: 21,',
  10252. ' y: 22',
  10253. ' })',
  10254. '});',
  10255. 'this.p = [$mod.TPoint.$clone({',
  10256. ' x: 1,',
  10257. ' y: 2',
  10258. '}), $mod.TPoint.$clone({',
  10259. ' x: 3,',
  10260. ' y: 4',
  10261. '})];',
  10262. '']),
  10263. LinesToStr([ // $mod.$main
  10264. '']));
  10265. end;
  10266. procedure TTestModule.TestRecord_TypecastFail;
  10267. begin
  10268. StartProgram(false);
  10269. Add([
  10270. 'type',
  10271. ' TPoint = record x,y: longint; end;',
  10272. ' TRec = record l: longint end;',
  10273. 'var p: TPoint;',
  10274. 'begin',
  10275. ' if TRec(p).l=2 then ;']);
  10276. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  10277. nIllegalTypeConversionTo);
  10278. ConvertProgram;
  10279. end;
  10280. procedure TTestModule.TestRecord_InFunction;
  10281. begin
  10282. StartProgram(false);
  10283. Add([
  10284. 'var TPoint: longint = 3;',
  10285. 'procedure DoIt;',
  10286. 'type',
  10287. ' TPoint = record x,y: longint; end;',
  10288. ' TPoints = array of TPoint;',
  10289. 'var',
  10290. ' r: TPoint;',
  10291. ' p: TPoints;',
  10292. 'begin',
  10293. ' SetLength(p,2);',
  10294. 'end;',
  10295. 'begin']);
  10296. ConvertProgram;
  10297. CheckSource('TestRecord_InFunction',
  10298. LinesToStr([ // statements
  10299. 'this.TPoint = 3;',
  10300. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  10301. ' this.x = 0;',
  10302. ' this.y = 0;',
  10303. ' this.$eq = function (b) {',
  10304. ' return (this.x === b.x) && (this.y === b.y);',
  10305. ' };',
  10306. ' this.$assign = function (s) {',
  10307. ' this.x = s.x;',
  10308. ' this.y = s.y;',
  10309. ' return this;',
  10310. ' };',
  10311. '});',
  10312. 'this.DoIt = function () {',
  10313. ' var r = TPoint$1.$new();',
  10314. ' var p = [];',
  10315. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  10316. '};',
  10317. '']),
  10318. LinesToStr([ // $mod.$main
  10319. '']));
  10320. end;
  10321. procedure TTestModule.TestRecord_AnonymousFail;
  10322. begin
  10323. StartProgram(false);
  10324. Add([
  10325. 'var',
  10326. ' r: record x: word end;',
  10327. 'begin']);
  10328. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] anonymous record type',
  10329. nNotYetImplemented);
  10330. ConvertProgram;
  10331. end;
  10332. procedure TTestModule.TestAdvRecord_Function;
  10333. begin
  10334. StartProgram(false);
  10335. Parser.Options:=Parser.Options+[po_cassignments];
  10336. Add([
  10337. '{$modeswitch AdvancedRecords}',
  10338. 'type',
  10339. ' TPoint = record',
  10340. ' x,y: word;',
  10341. ' function Add(const apt: TPoint): TPoint;',
  10342. ' end;',
  10343. 'function TPoint.Add(const apt: TPoint): TPoint;',
  10344. 'begin',
  10345. ' Result:=Self;',
  10346. ' Result.x+=apt.x;',
  10347. ' Result.y:=Result.y+apt.y;',
  10348. ' Self:=apt;',
  10349. 'end;',
  10350. 'var p,q: TPoint;',
  10351. 'begin',
  10352. ' p.add(q);',
  10353. ' p:=default(TPoint);',
  10354. ' p:=q;',
  10355. '']);
  10356. ConvertProgram;
  10357. CheckSource('TestAdvRecord_Function',
  10358. LinesToStr([ // statements
  10359. 'rtl.recNewT($mod, "TPoint", function () {',
  10360. ' this.x = 0;',
  10361. ' this.y = 0;',
  10362. ' this.$eq = function (b) {',
  10363. ' return (this.x === b.x) && (this.y === b.y);',
  10364. ' };',
  10365. ' this.$assign = function (s) {',
  10366. ' this.x = s.x;',
  10367. ' this.y = s.y;',
  10368. ' return this;',
  10369. ' };',
  10370. ' this.Add = function (apt) {',
  10371. ' var Result = $mod.TPoint.$new();',
  10372. ' Result.$assign(this);',
  10373. ' Result.x += apt.x;',
  10374. ' Result.y = Result.y + apt.y;',
  10375. ' this.$assign(apt);',
  10376. ' return Result;',
  10377. ' };',
  10378. '});',
  10379. 'this.p = $mod.TPoint.$new();',
  10380. 'this.q = $mod.TPoint.$new();',
  10381. '']),
  10382. LinesToStr([ // $mod.$main
  10383. '$mod.p.Add($mod.q);',
  10384. '$mod.p.$assign($mod.TPoint.$new());',
  10385. '$mod.p.$assign($mod.q);',
  10386. '']));
  10387. end;
  10388. procedure TTestModule.TestAdvRecord_Property;
  10389. begin
  10390. StartProgram(false);
  10391. Add([
  10392. '{$modeswitch AdvancedRecords}',
  10393. 'type',
  10394. ' TPoint = record',
  10395. ' x,y: word;',
  10396. ' strict private',
  10397. ' function GetSize: longword;',
  10398. ' procedure SetSize(Value: longword);',
  10399. ' public',
  10400. ' property Size: longword read GetSize write SetSize;',
  10401. ' property Left: word read x write y;',
  10402. ' end;',
  10403. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  10404. 'function TPoint.GetSize: longword;',
  10405. 'begin',
  10406. ' x:=y;',
  10407. ' Size:=Size;',
  10408. ' Left:=Left;',
  10409. 'end;',
  10410. 'procedure TPoint.SetSize(Value: longword);',
  10411. 'begin',
  10412. 'end;',
  10413. 'var p,q: TPoint;',
  10414. 'begin',
  10415. ' p.Size:=q.Size;',
  10416. ' p.Left:=q.Left;',
  10417. '']);
  10418. ConvertProgram;
  10419. CheckSource('TestAdvRecord_Property',
  10420. LinesToStr([ // statements
  10421. 'rtl.recNewT($mod, "TPoint", function () {',
  10422. ' this.x = 0;',
  10423. ' this.y = 0;',
  10424. ' this.$eq = function (b) {',
  10425. ' return (this.x === b.x) && (this.y === b.y);',
  10426. ' };',
  10427. ' this.$assign = function (s) {',
  10428. ' this.x = s.x;',
  10429. ' this.y = s.y;',
  10430. ' return this;',
  10431. ' };',
  10432. ' this.GetSize = function () {',
  10433. ' var Result = 0;',
  10434. ' this.x = this.y;',
  10435. ' this.SetSize(this.GetSize());',
  10436. ' this.y = this.x;',
  10437. ' return Result;',
  10438. ' };',
  10439. ' this.SetSize = function (Value) {',
  10440. ' };',
  10441. '});',
  10442. 'this.SetSize = function (Value) {',
  10443. '};',
  10444. 'this.p = $mod.TPoint.$new();',
  10445. 'this.q = $mod.TPoint.$new();',
  10446. '']),
  10447. LinesToStr([ // $mod.$main
  10448. '$mod.p.SetSize($mod.q.GetSize());',
  10449. '$mod.p.y = $mod.q.x;',
  10450. '']));
  10451. end;
  10452. procedure TTestModule.TestAdvRecord_PropertyDefault;
  10453. begin
  10454. StartProgram(false);
  10455. Add([
  10456. '{$modeswitch AdvancedRecords}',
  10457. 'type',
  10458. ' TPoint = record',
  10459. ' strict private',
  10460. ' function GetItems(Index: word): word;',
  10461. ' procedure SetItems(Index: word; Value: word);',
  10462. ' public',
  10463. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  10464. ' end;',
  10465. 'function TPoint.GetItems(Index: word): word;',
  10466. 'begin',
  10467. ' Items[index]:=Items[index];',
  10468. ' self.Items[index]:=self.Items[index];',
  10469. 'end;',
  10470. 'procedure TPoint.SetItems(Index: word; Value: word);',
  10471. 'begin',
  10472. 'end;',
  10473. 'var p: TPoint;',
  10474. 'begin',
  10475. ' p[1]:=p[2];',
  10476. ' p.Items[3]:=p.Items[4];',
  10477. '']);
  10478. ConvertProgram;
  10479. CheckSource('TestAdvRecord_PropertyDefault',
  10480. LinesToStr([ // statements
  10481. 'rtl.recNewT($mod, "TPoint", function () {',
  10482. ' this.$eq = function (b) {',
  10483. ' return true;',
  10484. ' };',
  10485. ' this.$assign = function (s) {',
  10486. ' return this;',
  10487. ' };',
  10488. ' this.GetItems = function (Index) {',
  10489. ' var Result = 0;',
  10490. ' this.SetItems(Index, this.GetItems(Index));',
  10491. ' this.SetItems(Index, this.GetItems(Index));',
  10492. ' return Result;',
  10493. ' };',
  10494. ' this.SetItems = function (Index, Value) {',
  10495. ' };',
  10496. '});',
  10497. 'this.p = $mod.TPoint.$new();',
  10498. '']),
  10499. LinesToStr([ // $mod.$main
  10500. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  10501. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  10502. '']));
  10503. end;
  10504. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  10505. begin
  10506. StartProgram(false);
  10507. Add([
  10508. '{$modeswitch AdvancedRecords}',
  10509. 'type',
  10510. ' TRec = record',
  10511. ' class var Fx: longint;',
  10512. ' class var Fy: longint;',
  10513. ' class function GetInt: longint; static;',
  10514. ' class procedure SetInt(Value: longint); static;',
  10515. ' class procedure DoIt; static;',
  10516. ' class property IntA: longint read Fx write Fy;',
  10517. ' class property IntB: longint read GetInt write SetInt;',
  10518. ' end;',
  10519. 'class function trec.getint: longint;',
  10520. 'begin',
  10521. ' result:=fx;',
  10522. 'end;',
  10523. 'class procedure trec.setint(value: longint);',
  10524. 'begin',
  10525. 'end;',
  10526. 'class procedure trec.doit;',
  10527. 'begin',
  10528. ' IntA:=IntA+1;',
  10529. ' IntB:=IntB+1;',
  10530. 'end;',
  10531. 'var r: trec;',
  10532. 'begin',
  10533. ' trec.inta:=trec.inta+1;',
  10534. ' if trec.intb=2 then;',
  10535. ' trec.intb:=trec.intb+2;',
  10536. ' trec.setint(trec.inta);',
  10537. ' r.inta:=r.inta+1;',
  10538. ' if r.intb=2 then;',
  10539. ' r.intb:=r.intb+2;',
  10540. ' r.setint(r.inta);']);
  10541. ConvertProgram;
  10542. CheckSource('TestAdvRecord_Property_ClassMethod',
  10543. LinesToStr([ // statements
  10544. 'rtl.recNewT($mod, "TRec", function () {',
  10545. ' this.Fx = 0;',
  10546. ' this.Fy = 0;',
  10547. ' this.$eq = function (b) {',
  10548. ' return true;',
  10549. ' };',
  10550. ' this.$assign = function (s) {',
  10551. ' return this;',
  10552. ' };',
  10553. ' this.GetInt = function () {',
  10554. ' var Result = 0;',
  10555. ' Result = this.Fx;',
  10556. ' return Result;',
  10557. ' };',
  10558. ' this.SetInt = function (Value) {',
  10559. ' };',
  10560. ' this.DoIt = function () {',
  10561. ' $mod.TRec.Fy = this.Fx + 1;',
  10562. ' this.SetInt(this.GetInt() + 1);',
  10563. ' };',
  10564. '}, true);',
  10565. 'this.r = $mod.TRec.$new();',
  10566. '']),
  10567. LinesToStr([ // $mod.$main
  10568. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  10569. 'if ($mod.TRec.GetInt() === 2) ;',
  10570. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  10571. '$mod.TRec.SetInt($mod.TRec.Fx);',
  10572. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  10573. 'if ($mod.r.GetInt() === 2) ;',
  10574. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  10575. '$mod.r.SetInt($mod.r.Fx);',
  10576. '']));
  10577. end;
  10578. procedure TTestModule.TestAdvRecord_Const;
  10579. begin
  10580. StartProgram(false);
  10581. Add([
  10582. '{$modeswitch AdvancedRecords}',
  10583. 'type',
  10584. ' TArrInt = array[3..4] of longint;',
  10585. ' TPoint = record',
  10586. ' x,y: longint;',
  10587. ' class var Count: nativeint;',
  10588. ' end;',
  10589. ' TRec = record',
  10590. ' i: longint;',
  10591. ' a: array of longint;',
  10592. ' s: array[1..2] of longint;',
  10593. ' m: array[1..2,3..4] of longint;',
  10594. ' p: TPoint;',
  10595. ' end;',
  10596. ' TPoints = array of TPoint;',
  10597. 'const',
  10598. ' r: TRec = (',
  10599. ' i:1;',
  10600. ' a:(2,3);',
  10601. ' s:(4,5);',
  10602. ' m:( (11,12), (13,14) );',
  10603. ' p: (x:21)',
  10604. ' );',
  10605. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10606. 'begin']);
  10607. ConvertProgram;
  10608. CheckSource('TestAdvRecord_Const',
  10609. LinesToStr([ // statements
  10610. 'rtl.recNewT($mod, "TPoint", function () {',
  10611. ' this.x = 0;',
  10612. ' this.y = 0;',
  10613. ' this.Count = 0;',
  10614. ' this.$eq = function (b) {',
  10615. ' return (this.x === b.x) && (this.y === b.y);',
  10616. ' };',
  10617. ' this.$assign = function (s) {',
  10618. ' this.x = s.x;',
  10619. ' this.y = s.y;',
  10620. ' return this;',
  10621. ' };',
  10622. '}, true);',
  10623. 'rtl.recNewT($mod, "TRec", function () {',
  10624. ' this.i = 0;',
  10625. ' this.a = [];',
  10626. ' this.$new = function () {',
  10627. ' var r = Object.create(this);',
  10628. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10629. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10630. ' r.p = $mod.TPoint.$new();',
  10631. ' return r;',
  10632. ' };',
  10633. ' this.$eq = function (b) {',
  10634. ' 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);',
  10635. ' };',
  10636. ' this.$assign = function (s) {',
  10637. ' this.i = s.i;',
  10638. ' this.a = s.a;',
  10639. ' this.s = s.s.slice(0);',
  10640. ' this.m = s.m.slice(0);',
  10641. ' this.p.$assign(s.p);',
  10642. ' return this;',
  10643. ' };',
  10644. '});',
  10645. 'this.r = $mod.TRec.$clone({',
  10646. ' i: 1,',
  10647. ' a: [2, 3],',
  10648. ' s: [4, 5],',
  10649. ' m: [[11, 12], [13, 14]],',
  10650. ' p: $mod.TPoint.$clone({',
  10651. ' x: 21,',
  10652. ' y: 0',
  10653. ' })',
  10654. '});',
  10655. 'this.p = [$mod.TPoint.$clone({',
  10656. ' x: 1,',
  10657. ' y: 2',
  10658. '}), $mod.TPoint.$clone({',
  10659. ' x: 3,',
  10660. ' y: 4',
  10661. '})];',
  10662. '']),
  10663. LinesToStr([ // $mod.$main
  10664. '']));
  10665. end;
  10666. procedure TTestModule.TestAdvRecord_ExternalField;
  10667. begin
  10668. StartProgram(false);
  10669. Add([
  10670. '{$modeswitch AdvancedRecords}',
  10671. '{$modeswitch externalclass}',
  10672. 'type',
  10673. ' TCar = record',
  10674. ' public',
  10675. ' Intern: longint external name ''$Intern'';',
  10676. ' Intern2: longint external name ''$Intern2'';',
  10677. ' Bracket: longint external name ''["A B"]'';',
  10678. ' procedure DoIt;',
  10679. ' end;',
  10680. 'implementation',
  10681. 'procedure tcar.doit;',
  10682. 'begin',
  10683. ' Intern:=Intern+1;',
  10684. ' Intern2:=Intern2+2;',
  10685. ' Bracket:=Bracket+3;',
  10686. 'end;',
  10687. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  10688. 'begin',
  10689. ' Rec.intern:=Rec.intern+1;',
  10690. ' Rec.intern2:=Rec.intern2+2;',
  10691. ' Rec.Bracket:=Rec.Bracket+3;',
  10692. ' with Rec do begin',
  10693. ' intern:=intern+1;',
  10694. ' intern2:=intern2+2;',
  10695. ' Bracket:=Bracket+3;',
  10696. ' end;']);
  10697. ConvertProgram;
  10698. CheckSource('TestAdvRecord_ExternalField',
  10699. LinesToStr([ // statements
  10700. 'rtl.recNewT($mod, "TCar", function () {',
  10701. ' this.$eq = function (b) {',
  10702. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  10703. ' };',
  10704. ' this.$assign = function (s) {',
  10705. ' this.$Intern = s.$Intern;',
  10706. ' this.$Intern2 = s.$Intern2;',
  10707. ' this["A B"] = s["A B"];',
  10708. ' return this;',
  10709. ' };',
  10710. ' this.DoIt = function () {',
  10711. ' this.$Intern = this.$Intern + 1;',
  10712. ' this.$Intern2 = this.$Intern2 + 2;',
  10713. ' this["A B"] = this["A B"] + 3;',
  10714. ' };',
  10715. '});',
  10716. 'this.Rec = $mod.TCar.$clone({',
  10717. ' $Intern: 11,',
  10718. ' $Intern2: 12,',
  10719. ' "A B": 13',
  10720. '});',
  10721. '']),
  10722. LinesToStr([ // $mod.$main
  10723. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  10724. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  10725. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  10726. 'var $with1 = $mod.Rec;',
  10727. '$with1.$Intern = $with1.$Intern + 1;',
  10728. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  10729. '$with1["A B"] = $with1["A B"] + 3;',
  10730. '']));
  10731. end;
  10732. procedure TTestModule.TestAdvRecord_SubRecord;
  10733. begin
  10734. StartProgram(false);
  10735. Add([
  10736. '{$modeswitch AdvancedRecords}',
  10737. 'type',
  10738. ' TRec = record',
  10739. ' type',
  10740. ' TPoint = record',
  10741. ' x,y: longint;',
  10742. ' class var Count: nativeint;',
  10743. ' procedure DoIt;',
  10744. ' class procedure DoThat; static;',
  10745. ' end;',
  10746. ' var',
  10747. ' i: longint;',
  10748. ' p: TPoint;',
  10749. ' procedure DoSome;',
  10750. ' end;',
  10751. 'const',
  10752. ' r: TRec = (',
  10753. ' i:1;',
  10754. ' p: (x:21;y:22)',
  10755. ' );',
  10756. 'procedure TRec.DoSome;',
  10757. 'begin',
  10758. ' p.x:=p.y+1;',
  10759. ' p.Count:=p.Count+2;',
  10760. 'end;',
  10761. 'procedure TRec.TPoint.DoIt;',
  10762. 'begin',
  10763. ' Count:=Count+3;',
  10764. 'end;',
  10765. 'class procedure TRec.TPoint.DoThat;',
  10766. 'begin',
  10767. ' Count:=Count+4;',
  10768. 'end;',
  10769. 'begin']);
  10770. ConvertProgram;
  10771. CheckSource('TestAdvRecord_SubRecord',
  10772. LinesToStr([ // statements
  10773. 'rtl.recNewT($mod, "TRec", function () {',
  10774. ' rtl.recNewT(this, "TPoint", function () {',
  10775. ' this.x = 0;',
  10776. ' this.y = 0;',
  10777. ' this.Count = 0;',
  10778. ' this.$eq = function (b) {',
  10779. ' return (this.x === b.x) && (this.y === b.y);',
  10780. ' };',
  10781. ' this.$assign = function (s) {',
  10782. ' this.x = s.x;',
  10783. ' this.y = s.y;',
  10784. ' return this;',
  10785. ' };',
  10786. ' this.DoIt = function () {',
  10787. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  10788. ' };',
  10789. ' this.DoThat = function () {',
  10790. ' $mod.TRec.TPoint.Count = this.Count + 4;',
  10791. ' };',
  10792. ' }, true);',
  10793. ' this.i = 0;',
  10794. ' this.$new = function () {',
  10795. ' var r = Object.create(this);',
  10796. ' r.p = this.TPoint.$new();',
  10797. ' return r;',
  10798. ' };',
  10799. ' this.$eq = function (b) {',
  10800. ' return (this.i === b.i) && this.p.$eq(b.p);',
  10801. ' };',
  10802. ' this.$assign = function (s) {',
  10803. ' this.i = s.i;',
  10804. ' this.p.$assign(s.p);',
  10805. ' return this;',
  10806. ' };',
  10807. ' this.DoSome = function () {',
  10808. ' this.p.x = this.p.y + 1;',
  10809. ' this.TPoint.Count = this.p.Count + 2;',
  10810. ' };',
  10811. '}, true);',
  10812. 'this.r = $mod.TRec.$clone({',
  10813. ' i: 1,',
  10814. ' p: $mod.TRec.TPoint.$clone({',
  10815. ' x: 21,',
  10816. ' y: 22',
  10817. ' })',
  10818. '});',
  10819. '']),
  10820. LinesToStr([ // $mod.$main
  10821. '']));
  10822. end;
  10823. procedure TTestModule.TestAdvRecord_SubClass;
  10824. begin
  10825. StartProgram(false);
  10826. Add([
  10827. '{$modeswitch AdvancedRecords}',
  10828. 'type',
  10829. ' TObject = class end;',
  10830. ' TPoint = record',
  10831. ' type',
  10832. ' TBird = class',
  10833. ' procedure DoIt;',
  10834. ' class procedure Glob;',
  10835. ' end;',
  10836. ' procedure DoIt(b: TBird);',
  10837. ' end;',
  10838. 'procedure TPoint.TBird.DoIt;',
  10839. 'begin',
  10840. ' doit;',
  10841. ' self.doit;',
  10842. ' glob;',
  10843. ' self.glob;',
  10844. 'end;',
  10845. 'class procedure TPoint.TBird.Glob;',
  10846. 'begin',
  10847. ' glob;',
  10848. ' self.glob;',
  10849. 'end;',
  10850. 'procedure TPoint.DoIt(b: TBird);',
  10851. 'begin',
  10852. ' b.doit;',
  10853. ' b.glob;',
  10854. ' TBird.glob;',
  10855. 'end;',
  10856. 'begin',
  10857. '']);
  10858. ConvertProgram;
  10859. CheckSource('TestAdvRecord_SubClass',
  10860. LinesToStr([ // statements
  10861. 'rtl.createClass($mod, "TObject", null, function () {',
  10862. ' this.$init = function () {',
  10863. ' };',
  10864. ' this.$final = function () {',
  10865. ' };',
  10866. '});',
  10867. 'rtl.recNewT($mod, "TPoint", function () {',
  10868. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  10869. ' this.DoIt = function () {',
  10870. ' this.DoIt();',
  10871. ' this.DoIt();',
  10872. ' this.$class.Glob();',
  10873. ' this.$class.Glob();',
  10874. ' };',
  10875. ' this.Glob = function () {',
  10876. ' this.Glob();',
  10877. ' this.Glob();',
  10878. ' };',
  10879. ' });',
  10880. ' this.$eq = function (b) {',
  10881. ' return true;',
  10882. ' };',
  10883. ' this.$assign = function (s) {',
  10884. ' return this;',
  10885. ' };',
  10886. ' this.DoIt = function (b) {',
  10887. ' b.DoIt();',
  10888. ' b.$class.Glob();',
  10889. ' this.TBird.Glob();',
  10890. ' };',
  10891. '}, true);',
  10892. '']),
  10893. LinesToStr([ // $mod.$main
  10894. '']));
  10895. end;
  10896. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  10897. begin
  10898. StartProgram(false);
  10899. Add([
  10900. '{$modeswitch AdvancedRecords}',
  10901. 'type',
  10902. ' IUnknown = interface end;',
  10903. ' TPoint = record',
  10904. ' type IBird = interface end;',
  10905. ' end;',
  10906. 'begin',
  10907. '']);
  10908. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] interface inside record',
  10909. nNotYetImplemented);
  10910. ParseProgram;
  10911. end;
  10912. procedure TTestModule.TestAdvRecord_Constructor;
  10913. begin
  10914. StartProgram(false);
  10915. Add([
  10916. '{$modeswitch AdvancedRecords}',
  10917. 'type',
  10918. ' TPoint = record',
  10919. ' x,y: longint;',
  10920. ' constructor Create(ax: longint; ay: longint = -1);',
  10921. ' end;',
  10922. 'constructor tpoint.create(ax,ay: longint);',
  10923. 'begin',
  10924. ' x:=ax;',
  10925. ' self.y:=ay;',
  10926. 'end;',
  10927. 'var r: TPoint;',
  10928. 'begin',
  10929. ' r:=TPoint.Create(1,2);',
  10930. ' with TPoint do r:=Create(1,2);',
  10931. ' r.Create(3);',
  10932. ' r:=r.Create(4);',
  10933. '']);
  10934. ConvertProgram;
  10935. CheckSource('TestAdvRecord_Constructor',
  10936. LinesToStr([ // statements
  10937. 'rtl.recNewT($mod, "TPoint", function () {',
  10938. ' this.x = 0;',
  10939. ' this.y = 0;',
  10940. ' this.$eq = function (b) {',
  10941. ' return (this.x === b.x) && (this.y === b.y);',
  10942. ' };',
  10943. ' this.$assign = function (s) {',
  10944. ' this.x = s.x;',
  10945. ' this.y = s.y;',
  10946. ' return this;',
  10947. ' };',
  10948. ' this.Create = function (ax, ay) {',
  10949. ' this.x = ax;',
  10950. ' this.y = ay;',
  10951. ' return this;',
  10952. ' };',
  10953. '}, true);',
  10954. 'this.r = $mod.TPoint.$new();',
  10955. '']),
  10956. LinesToStr([ // $mod.$main
  10957. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  10958. 'var $with1 = $mod.TPoint;',
  10959. '$mod.r.$assign($with1.$new().Create(1, 2));',
  10960. '$mod.r.Create(3, -1);',
  10961. '$mod.r.$assign($mod.r.Create(4, -1));',
  10962. '']));
  10963. end;
  10964. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  10965. begin
  10966. StartProgram(false);
  10967. Add(['type',
  10968. ' TObject = class',
  10969. ' public',
  10970. ' constructor Create;',
  10971. ' destructor Destroy;',
  10972. ' end;',
  10973. ' TBird = TObject;',
  10974. 'constructor tobject.create;',
  10975. 'begin end;',
  10976. 'destructor tobject.destroy;',
  10977. 'begin end;',
  10978. 'var Obj: tobject;',
  10979. 'begin',
  10980. ' obj:=tobject.create;',
  10981. ' obj:=tobject.create();',
  10982. ' obj:=tbird.create;',
  10983. ' obj:=tbird.create();',
  10984. ' obj:=obj.create();',
  10985. ' obj.destroy;',
  10986. '']);
  10987. ConvertProgram;
  10988. CheckSource('TestClass_TObjectDefaultConstructor',
  10989. LinesToStr([ // statements
  10990. 'rtl.createClass($mod,"TObject",null,function(){',
  10991. ' this.$init = function () {',
  10992. ' };',
  10993. ' this.$final = function () {',
  10994. ' };',
  10995. ' this.Create = function(){',
  10996. ' return this;',
  10997. ' };',
  10998. ' this.Destroy = function(){',
  10999. ' };',
  11000. '});',
  11001. 'this.Obj = null;'
  11002. ]),
  11003. LinesToStr([ // $mod.$main
  11004. '$mod.Obj = $mod.TObject.$create("Create");',
  11005. '$mod.Obj = $mod.TObject.$create("Create");',
  11006. '$mod.Obj = $mod.TObject.$create("Create");',
  11007. '$mod.Obj = $mod.TObject.$create("Create");',
  11008. '$mod.Obj = $mod.Obj.Create();',
  11009. '$mod.Obj.$destroy("Destroy");',
  11010. '']));
  11011. end;
  11012. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  11013. begin
  11014. StartProgram(false);
  11015. Add('type');
  11016. Add(' TObject = class');
  11017. Add(' public');
  11018. Add(' constructor Create(Par: longint);');
  11019. Add(' end;');
  11020. Add('constructor tobject.create(par: longint);');
  11021. Add('begin end;');
  11022. Add('var Obj: tobject;');
  11023. Add('begin');
  11024. Add(' obj:=tobject.create(3);');
  11025. ConvertProgram;
  11026. CheckSource('TestClass_TObjectConstructorWithParams',
  11027. LinesToStr([ // statements
  11028. 'rtl.createClass($mod,"TObject",null,function(){',
  11029. ' this.$init = function () {',
  11030. ' };',
  11031. ' this.$final = function () {',
  11032. ' };',
  11033. ' this.Create = function(Par){',
  11034. ' return this;',
  11035. ' };',
  11036. '});',
  11037. 'this.Obj = null;'
  11038. ]),
  11039. LinesToStr([ // $mod.$main
  11040. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  11041. ]));
  11042. end;
  11043. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  11044. begin
  11045. StartProgram(false);
  11046. Add('type');
  11047. Add(' TObject = class');
  11048. Add(' public');
  11049. Add(' constructor Create;');
  11050. Add(' end;');
  11051. Add(' TTest = class(TObject)');
  11052. Add(' public');
  11053. Add(' constructor Create(const Par: longint = 1);');
  11054. Add(' end;');
  11055. Add('constructor tobject.create;');
  11056. Add('begin end;');
  11057. Add('constructor ttest.create(const par: longint);');
  11058. Add('begin end;');
  11059. Add('var t: ttest;');
  11060. Add('begin');
  11061. Add(' t:=ttest.create;');
  11062. Add(' t:=ttest.create(2);');
  11063. ConvertProgram;
  11064. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  11065. LinesToStr([ // statements
  11066. 'rtl.createClass($mod,"TObject",null,function(){',
  11067. ' this.$init = function () {',
  11068. ' };',
  11069. ' this.$final = function () {',
  11070. ' };',
  11071. ' this.Create = function(){',
  11072. ' return this;',
  11073. ' };',
  11074. '});',
  11075. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  11076. ' this.Create$1 = function (Par) {',
  11077. ' return this;',
  11078. ' };',
  11079. '});',
  11080. 'this.t = null;'
  11081. ]),
  11082. LinesToStr([ // $mod.$main
  11083. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  11084. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  11085. ]));
  11086. end;
  11087. procedure TTestModule.TestClass_Var;
  11088. begin
  11089. StartProgram(false);
  11090. Add([
  11091. 'type',
  11092. ' TObject = class',
  11093. ' public',
  11094. ' vI: longint;',
  11095. ' constructor Create(Par: longint);',
  11096. ' end;',
  11097. 'constructor tobject.create(par: longint);',
  11098. 'begin',
  11099. ' vi:=par+3',
  11100. 'end;',
  11101. 'var Obj: tobject;',
  11102. 'begin',
  11103. ' obj:=tobject.create(4);',
  11104. ' obj.vi:=obj.VI+5;']);
  11105. ConvertProgram;
  11106. CheckSource('TestClass_Var',
  11107. LinesToStr([ // statements
  11108. 'rtl.createClass($mod,"TObject",null,function(){',
  11109. ' this.$init = function () {',
  11110. ' this.vI = 0;',
  11111. ' };',
  11112. ' this.$final = function () {',
  11113. ' };',
  11114. ' this.Create = function(Par){',
  11115. ' this.vI = Par+3;',
  11116. ' return this;',
  11117. ' };',
  11118. '});',
  11119. 'this.Obj = null;'
  11120. ]),
  11121. LinesToStr([ // $mod.$main
  11122. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  11123. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  11124. ]));
  11125. end;
  11126. procedure TTestModule.TestClass_Method;
  11127. begin
  11128. StartProgram(false);
  11129. Add('type');
  11130. Add(' TObject = class');
  11131. Add(' public');
  11132. Add(' vI: longint;');
  11133. Add(' Sub: TObject;');
  11134. Add(' constructor Create;');
  11135. Add(' function GetIt(Par: longint): tobject;');
  11136. Add(' end;');
  11137. Add('constructor tobject.create; begin end;');
  11138. Add('function tobject.getit(par: longint): tobject;');
  11139. Add('begin');
  11140. Add(' Self.vi:=par+3;');
  11141. Add(' Result:=self.sub;');
  11142. Add('end;');
  11143. Add('var Obj: tobject;');
  11144. Add('begin');
  11145. Add(' obj:=tobject.create;');
  11146. Add(' obj.getit(4);');
  11147. Add(' obj.sub.sub:=nil;');
  11148. Add(' obj.sub.getit(5);');
  11149. Add(' obj.sub.getit(6).SUB:=nil;');
  11150. Add(' obj.sub.getit(7).GETIT(8);');
  11151. Add(' obj.sub.getit(9).SuB.getit(10);');
  11152. ConvertProgram;
  11153. CheckSource('TestClass_Method',
  11154. LinesToStr([ // statements
  11155. 'rtl.createClass($mod,"TObject",null,function(){',
  11156. ' this.$init = function () {',
  11157. ' this.vI = 0;',
  11158. ' this.Sub = null;',
  11159. ' };',
  11160. ' this.$final = function () {',
  11161. ' this.Sub = undefined;',
  11162. ' };',
  11163. ' this.Create = function(){',
  11164. ' return this;',
  11165. ' };',
  11166. ' this.GetIt = function(Par){',
  11167. ' var Result = null;',
  11168. ' this.vI = Par + 3;',
  11169. ' Result = this.Sub;',
  11170. ' return Result;',
  11171. ' };',
  11172. '});',
  11173. 'this.Obj = null;'
  11174. ]),
  11175. LinesToStr([ // $mod.$main
  11176. '$mod.Obj = $mod.TObject.$create("Create");',
  11177. '$mod.Obj.GetIt(4);',
  11178. '$mod.Obj.Sub.Sub=null;',
  11179. '$mod.Obj.Sub.GetIt(5);',
  11180. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  11181. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  11182. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  11183. ]));
  11184. end;
  11185. procedure TTestModule.TestClass_Implementation;
  11186. begin
  11187. StartUnit(false);
  11188. Add([
  11189. 'interface',
  11190. 'type',
  11191. ' TObject = class',
  11192. ' constructor Create;',
  11193. ' end;',
  11194. 'implementation',
  11195. 'type',
  11196. ' TIntClass = class',
  11197. ' constructor Create; reintroduce;',
  11198. ' class procedure DoGlob;',
  11199. ' end;',
  11200. 'constructor tintclass.create;',
  11201. 'begin',
  11202. ' inherited;',
  11203. ' inherited create;',
  11204. ' doglob;',
  11205. 'end;',
  11206. 'class procedure tintclass.doglob;',
  11207. 'begin',
  11208. 'end;',
  11209. 'constructor tobject.create;',
  11210. 'var',
  11211. ' iC: tintclass;',
  11212. 'begin',
  11213. ' ic:=tintclass.create;',
  11214. ' tintclass.doglob;',
  11215. ' ic.doglob;',
  11216. 'end;',
  11217. 'initialization',
  11218. ' tintclass.doglob;',
  11219. '']);
  11220. ConvertUnit;
  11221. CheckSource('TestClass_Implementation',
  11222. LinesToStr([ // statements
  11223. 'var $impl = $mod.$impl;',
  11224. 'rtl.createClass($mod, "TObject", null, function () {',
  11225. ' this.$init = function () {',
  11226. ' };',
  11227. ' this.$final = function () {',
  11228. ' };',
  11229. ' this.Create = function () {',
  11230. ' var iC = null;',
  11231. ' iC = $impl.TIntClass.$create("Create$1");',
  11232. ' $impl.TIntClass.DoGlob();',
  11233. ' iC.$class.DoGlob();',
  11234. ' return this;',
  11235. ' };',
  11236. '});',
  11237. '']),
  11238. LinesToStr([ // $mod.$main
  11239. '$impl.TIntClass.DoGlob();',
  11240. '']),
  11241. LinesToStr([
  11242. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  11243. ' this.Create$1 = function () {',
  11244. ' $mod.TObject.Create.call(this);',
  11245. ' $mod.TObject.Create.call(this);',
  11246. ' this.$class.DoGlob();',
  11247. ' return this;',
  11248. ' };',
  11249. ' this.DoGlob = function () {',
  11250. ' };',
  11251. '});',
  11252. '']));
  11253. end;
  11254. procedure TTestModule.TestClass_Inheritance;
  11255. begin
  11256. StartProgram(false);
  11257. Add('type');
  11258. Add(' TObject = class');
  11259. Add(' public');
  11260. Add(' constructor Create;');
  11261. Add(' end;');
  11262. Add(' TClassA = class');
  11263. Add(' end;');
  11264. Add(' TClassB = class(TObject)');
  11265. Add(' procedure ProcB;');
  11266. Add(' end;');
  11267. Add('constructor tobject.create; begin end;');
  11268. Add('procedure tclassb.procb; begin end;');
  11269. Add('var');
  11270. Add(' oO: TObject;');
  11271. Add(' oA: TClassA;');
  11272. Add(' oB: TClassB;');
  11273. Add('begin');
  11274. Add(' oO:=tobject.Create;');
  11275. Add(' oA:=tclassa.Create;');
  11276. Add(' ob:=tclassb.Create;');
  11277. Add(' if oo is tclassa then ;');
  11278. Add(' ob:=oo as tclassb;');
  11279. Add(' (oo as tclassb).procb;');
  11280. ConvertProgram;
  11281. CheckSource('TestClass_Inheritance',
  11282. LinesToStr([ // statements
  11283. 'rtl.createClass($mod,"TObject",null,function(){',
  11284. ' this.$init = function () {',
  11285. ' };',
  11286. ' this.$final = function () {',
  11287. ' };',
  11288. ' this.Create = function () {',
  11289. ' return this;',
  11290. ' };',
  11291. '});',
  11292. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  11293. '});',
  11294. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  11295. ' this.ProcB = function () {',
  11296. ' };',
  11297. '});',
  11298. 'this.oO = null;',
  11299. 'this.oA = null;',
  11300. 'this.oB = null;'
  11301. ]),
  11302. LinesToStr([ // $mod.$main
  11303. '$mod.oO = $mod.TObject.$create("Create");',
  11304. '$mod.oA = $mod.TClassA.$create("Create");',
  11305. '$mod.oB = $mod.TClassB.$create("Create");',
  11306. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  11307. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  11308. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  11309. ]));
  11310. end;
  11311. procedure TTestModule.TestClass_TypeAlias;
  11312. begin
  11313. StartProgram(false);
  11314. Add([
  11315. '{$interfaces corba}',
  11316. 'type',
  11317. ' IObject = interface',
  11318. ' end;',
  11319. ' IBird = type IObject;',
  11320. ' TObject = class',
  11321. ' end;',
  11322. ' TBird = type TObject;',
  11323. 'var',
  11324. ' oObj: TObject;',
  11325. ' oBird: TBird;',
  11326. ' IntfObj: IObject;',
  11327. ' IntfBird: IBird;',
  11328. 'begin',
  11329. ' oObj:=oBird;',
  11330. '']);
  11331. ConvertProgram;
  11332. CheckSource('TestClass_TypeAlias',
  11333. LinesToStr([ // statements
  11334. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  11335. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  11336. 'rtl.createClass($mod, "TObject", null, function () {',
  11337. ' this.$init = function () {',
  11338. ' };',
  11339. ' this.$final = function () {',
  11340. ' };',
  11341. '});',
  11342. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11343. '});',
  11344. 'this.oObj = null;',
  11345. 'this.oBird = null;',
  11346. 'this.IntfObj = null;',
  11347. 'this.IntfBird = null;',
  11348. '']),
  11349. LinesToStr([ // $mod.$main
  11350. '$mod.oObj = $mod.oBird;',
  11351. '']));
  11352. end;
  11353. procedure TTestModule.TestClass_AbstractMethod;
  11354. begin
  11355. StartProgram(false);
  11356. Add('type');
  11357. Add(' TObject = class');
  11358. Add(' public');
  11359. Add(' procedure DoIt; virtual; abstract;');
  11360. Add(' end;');
  11361. Add('begin');
  11362. ConvertProgram;
  11363. CheckSource('TestClass_AbstractMethod',
  11364. LinesToStr([ // statements
  11365. 'rtl.createClass($mod,"TObject",null,function(){',
  11366. ' this.$init = function () {',
  11367. ' };',
  11368. ' this.$final = function () {',
  11369. ' };',
  11370. '});'
  11371. ]),
  11372. LinesToStr([ // this.$main
  11373. ''
  11374. ]));
  11375. end;
  11376. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  11377. begin
  11378. StartProgram(false);
  11379. Add([
  11380. 'type',
  11381. ' TObject = class',
  11382. ' procedure DoAbstract; virtual; abstract;',
  11383. ' procedure DoVirtual; virtual;',
  11384. ' procedure DoIt;',
  11385. ' end;',
  11386. ' TA = class',
  11387. ' procedure doabstract; override;',
  11388. ' procedure dovirtual; override;',
  11389. ' procedure DoSome;',
  11390. ' end;',
  11391. 'procedure tobject.dovirtual;',
  11392. 'begin',
  11393. ' inherited; // call non existing ancestor -> ignore silently',
  11394. 'end;',
  11395. 'procedure tobject.doit;',
  11396. 'begin',
  11397. 'end;',
  11398. 'procedure ta.doabstract;',
  11399. 'begin',
  11400. ' inherited dovirtual; // call TObject.DoVirtual',
  11401. 'end;',
  11402. 'procedure ta.dovirtual;',
  11403. 'begin',
  11404. ' inherited; // call TObject.DoVirtual',
  11405. ' inherited dovirtual; // call TObject.DoVirtual',
  11406. ' inherited dovirtual(); // call TObject.DoVirtual',
  11407. ' doit;',
  11408. ' doit();',
  11409. 'end;',
  11410. 'procedure ta.dosome;',
  11411. 'begin',
  11412. ' inherited; // call non existing ancestor method -> silently ignore',
  11413. 'end;',
  11414. 'begin']);
  11415. ConvertProgram;
  11416. CheckSource('TestClass_CallInherited_ProcNoParams',
  11417. LinesToStr([ // statements
  11418. 'rtl.createClass($mod,"TObject",null,function(){',
  11419. ' this.$init = function () {',
  11420. ' };',
  11421. ' this.$final = function () {',
  11422. ' };',
  11423. ' this.DoVirtual = function () {',
  11424. ' };',
  11425. ' this.DoIt = function () {',
  11426. ' };',
  11427. '});',
  11428. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11429. ' this.DoAbstract = function () {',
  11430. ' $mod.TObject.DoVirtual.call(this);',
  11431. ' };',
  11432. ' this.DoVirtual = function () {',
  11433. ' $mod.TObject.DoVirtual.call(this);',
  11434. ' $mod.TObject.DoVirtual.call(this);',
  11435. ' $mod.TObject.DoVirtual.call(this);',
  11436. ' this.DoIt();',
  11437. ' this.DoIt();',
  11438. ' };',
  11439. ' this.DoSome = function () {',
  11440. ' };',
  11441. '});'
  11442. ]),
  11443. LinesToStr([ // this.$main
  11444. ''
  11445. ]));
  11446. end;
  11447. procedure TTestModule.TestClass_CallInherited_WithParams;
  11448. begin
  11449. StartProgram(false);
  11450. Add([
  11451. 'type',
  11452. ' TObject = class',
  11453. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  11454. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  11455. ' procedure DoIt(pA: longint; pB: longint = 0);',
  11456. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  11457. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11458. ' end;',
  11459. ' TClassA = class',
  11460. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  11461. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  11462. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11463. ' end;',
  11464. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  11465. 'begin',
  11466. 'end;',
  11467. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  11468. 'begin',
  11469. 'end;',
  11470. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  11471. 'begin',
  11472. 'end;',
  11473. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  11474. 'begin',
  11475. 'end;',
  11476. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  11477. 'begin',
  11478. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11479. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11480. 'end;',
  11481. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  11482. 'begin',
  11483. ' inherited; // call TObject.DoVirtual(pA,pB)',
  11484. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11485. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11486. ' doit(pa,pb);',
  11487. ' doit(pa);',
  11488. ' doit2(pa);',
  11489. ' doit2;',
  11490. 'end;',
  11491. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  11492. 'begin',
  11493. ' pa:=inherited;',
  11494. 'end;',
  11495. 'begin']);
  11496. ConvertProgram;
  11497. CheckSource('TestClass_CallInherited_WithParams',
  11498. LinesToStr([ // statements
  11499. 'rtl.createClass($mod,"TObject",null,function(){',
  11500. ' this.$init = function () {',
  11501. ' };',
  11502. ' this.$final = function () {',
  11503. ' };',
  11504. ' this.DoVirtual = function (pA,pB) {',
  11505. ' };',
  11506. ' this.DoIt = function (pA,pB) {',
  11507. ' };',
  11508. ' this.DoIt2 = function (pA,pB) {',
  11509. ' };',
  11510. ' this.GetIt = function (pA, pB) {',
  11511. ' var Result = 0;',
  11512. ' return Result;',
  11513. ' };',
  11514. '});',
  11515. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  11516. ' this.DoAbstract = function (pA,pB) {',
  11517. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11518. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11519. ' };',
  11520. ' this.DoVirtual = function (pA,pB) {',
  11521. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  11522. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11523. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11524. ' this.DoIt(pA,pB);',
  11525. ' this.DoIt(pA,0);',
  11526. ' this.DoIt2(pA,2);',
  11527. ' this.DoIt2(1,2);',
  11528. ' };',
  11529. ' this.GetIt$1 = function (pA, pB) {',
  11530. ' var Result = 0;',
  11531. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  11532. ' return Result;',
  11533. ' };',
  11534. '});'
  11535. ]),
  11536. LinesToStr([ // this.$main
  11537. ''
  11538. ]));
  11539. end;
  11540. procedure TTestModule.TestClasS_CallInheritedConstructor;
  11541. begin
  11542. StartProgram(false);
  11543. Add('type');
  11544. Add(' TObject = class');
  11545. Add(' constructor Create; virtual;');
  11546. Add(' constructor CreateWithB(b: boolean);');
  11547. Add(' end;');
  11548. Add(' TA = class');
  11549. Add(' constructor Create; override;');
  11550. Add(' constructor CreateWithC(c: char);');
  11551. Add(' procedure DoIt;');
  11552. Add(' class function DoSome: TObject;');
  11553. Add(' end;');
  11554. Add('constructor tobject.create;');
  11555. Add('begin');
  11556. Add(' inherited; // call non existing ancestor -> ignore silently');
  11557. Add('end;');
  11558. Add('constructor tobject.createwithb(b: boolean);');
  11559. Add('begin');
  11560. Add(' inherited; // call non existing ancestor -> ignore silently');
  11561. Add(' create; // normal call');
  11562. Add('end;');
  11563. Add('constructor ta.create;');
  11564. Add('begin');
  11565. Add(' inherited; // normal call TObject.Create');
  11566. Add(' inherited create; // normal call TObject.Create');
  11567. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  11568. Add('end;');
  11569. Add('constructor ta.createwithc(c: char);');
  11570. Add('begin');
  11571. Add(' inherited create; // call TObject.Create');
  11572. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  11573. Add(' doit;');
  11574. Add(' doit();');
  11575. Add(' dosome;');
  11576. Add('end;');
  11577. Add('procedure ta.doit;');
  11578. Add('begin');
  11579. Add(' create; // normal call');
  11580. Add(' createwithb(false); // normal call');
  11581. Add(' createwithc(''c''); // normal call');
  11582. Add('end;');
  11583. Add('class function ta.dosome: TObject;');
  11584. Add('begin');
  11585. Add(' Result:=create; // constructor');
  11586. Add(' Result:=createwithb(true); // constructor');
  11587. Add(' Result:=createwithc(''c''); // constructor');
  11588. Add('end;');
  11589. Add('begin');
  11590. ConvertProgram;
  11591. CheckSource('TestClass_CallInheritedConstructor',
  11592. LinesToStr([ // statements
  11593. 'rtl.createClass($mod,"TObject",null,function(){',
  11594. ' this.$init = function () {',
  11595. ' };',
  11596. ' this.$final = function () {',
  11597. ' };',
  11598. ' this.Create = function () {',
  11599. ' return this;',
  11600. ' };',
  11601. ' this.CreateWithB = function (b) {',
  11602. ' this.Create();',
  11603. ' return this;',
  11604. ' };',
  11605. '});',
  11606. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11607. ' this.Create = function () {',
  11608. ' $mod.TObject.Create.call(this);',
  11609. ' $mod.TObject.Create.call(this);',
  11610. ' $mod.TObject.CreateWithB.call(this, false);',
  11611. ' return this;',
  11612. ' };',
  11613. ' this.CreateWithC = function (c) {',
  11614. ' $mod.TObject.Create.call(this);',
  11615. ' $mod.TObject.CreateWithB.call(this, true);',
  11616. ' this.DoIt();',
  11617. ' this.DoIt();',
  11618. ' this.$class.DoSome();',
  11619. ' return this;',
  11620. ' };',
  11621. ' this.DoIt = function () {',
  11622. ' this.Create();',
  11623. ' this.CreateWithB(false);',
  11624. ' this.CreateWithC("c");',
  11625. ' };',
  11626. ' this.DoSome = function () {',
  11627. ' var Result = null;',
  11628. ' Result = this.$create("Create");',
  11629. ' Result = this.$create("CreateWithB", [true]);',
  11630. ' Result = this.$create("CreateWithC", ["c"]);',
  11631. ' return Result;',
  11632. ' };',
  11633. '});'
  11634. ]),
  11635. LinesToStr([ // this.$main
  11636. ''
  11637. ]));
  11638. end;
  11639. procedure TTestModule.TestClass_ClassVar_Assign;
  11640. begin
  11641. StartProgram(false);
  11642. Add([
  11643. 'type',
  11644. ' TObject = class',
  11645. ' public',
  11646. ' class var vI: longint;',
  11647. ' class var Sub: TObject;',
  11648. ' constructor Create;',
  11649. ' class function GetIt(var Par: longint): tobject;',
  11650. ' end;',
  11651. 'constructor tobject.create;',
  11652. 'begin',
  11653. ' vi:=vi+1;',
  11654. ' Self.vi:=Self.vi+1;',
  11655. ' inc(vi);',
  11656. 'end;',
  11657. 'class function tobject.getit(var par: longint): tobject;',
  11658. 'begin',
  11659. ' vi:=vi+3;',
  11660. ' Self.vi:=Self.vi+4;',
  11661. ' inc(vi);',
  11662. ' Result:=self.sub;',
  11663. ' GetIt(vi);',
  11664. 'end;',
  11665. 'var Obj: tobject;',
  11666. 'begin',
  11667. ' obj:=tobject.create;',
  11668. ' tobject.vi:=3;',
  11669. ' if tobject.vi=4 then ;',
  11670. ' tobject.sub:=nil;',
  11671. ' obj.sub:=nil;',
  11672. ' obj.sub.sub:=nil;']);
  11673. ConvertProgram;
  11674. CheckSource('TestClass_ClassVar_Assign',
  11675. LinesToStr([ // statements
  11676. 'rtl.createClass($mod,"TObject",null,function(){',
  11677. ' this.vI = 0;',
  11678. ' this.Sub = null;',
  11679. ' this.$init = function () {',
  11680. ' };',
  11681. ' this.$final = function () {',
  11682. ' };',
  11683. ' this.Create = function(){',
  11684. ' $mod.TObject.vI = this.vI+1;',
  11685. ' $mod.TObject.vI = this.vI+1;',
  11686. ' $mod.TObject.vI += 1;',
  11687. ' return this;',
  11688. ' };',
  11689. ' this.GetIt = function(Par){',
  11690. ' var Result = null;',
  11691. ' $mod.TObject.vI = this.vI + 3;',
  11692. ' $mod.TObject.vI = this.vI + 4;',
  11693. ' $mod.TObject.vI += 1;',
  11694. ' Result = this.Sub;',
  11695. ' this.GetIt({',
  11696. ' p: $mod.TObject,',
  11697. ' get: function () {',
  11698. ' return this.p.vI;',
  11699. ' },',
  11700. ' set: function (v) {',
  11701. ' this.p.vI = v;',
  11702. ' }',
  11703. ' });',
  11704. ' return Result;',
  11705. ' };',
  11706. '});',
  11707. 'this.Obj = null;'
  11708. ]),
  11709. LinesToStr([ // $mod.$main
  11710. '$mod.Obj = $mod.TObject.$create("Create");',
  11711. '$mod.TObject.vI = 3;',
  11712. 'if ($mod.TObject.vI === 4);',
  11713. '$mod.TObject.Sub=null;',
  11714. '$mod.TObject.Sub=null;',
  11715. '$mod.TObject.Sub=null;',
  11716. '']));
  11717. end;
  11718. procedure TTestModule.TestClass_CallClassMethod;
  11719. begin
  11720. StartProgram(false);
  11721. Add('type');
  11722. Add(' TObject = class');
  11723. Add(' public');
  11724. Add(' class var vI: longint;');
  11725. Add(' class var Sub: TObject;');
  11726. Add(' constructor Create;');
  11727. Add(' function GetMore(Par: longint): longint;');
  11728. Add(' class function GetIt(Par: longint): tobject;');
  11729. Add(' end;');
  11730. Add('constructor tobject.create;');
  11731. Add('begin');
  11732. Add(' sub:=getit(3);');
  11733. Add(' vi:=getmore(4);');
  11734. Add(' sub:=Self.getit(5);');
  11735. Add(' vi:=Self.getmore(6);');
  11736. Add('end;');
  11737. Add('function tobject.getmore(par: longint): longint;');
  11738. Add('begin');
  11739. Add(' sub:=getit(11);');
  11740. Add(' vi:=getmore(12);');
  11741. Add(' sub:=self.getit(13);');
  11742. Add(' vi:=self.getmore(14);');
  11743. Add('end;');
  11744. Add('class function tobject.getit(par: longint): tobject;');
  11745. Add('begin');
  11746. Add(' sub:=getit(21);');
  11747. Add(' vi:=sub.getmore(22);');
  11748. Add(' sub:=self.getit(23);');
  11749. Add(' vi:=self.sub.getmore(24);');
  11750. Add('end;');
  11751. Add('var Obj: tobject;');
  11752. Add('begin');
  11753. Add(' obj:=tobject.create;');
  11754. Add(' tobject.getit(5);');
  11755. Add(' obj.getit(6);');
  11756. Add(' obj.sub.getit(7);');
  11757. Add(' obj.sub.getit(8).SUB:=nil;');
  11758. Add(' obj.sub.getit(9).GETIT(10);');
  11759. Add(' obj.sub.getit(11).SuB.getit(12);');
  11760. ConvertProgram;
  11761. CheckSource('TestClass_CallClassMethod',
  11762. LinesToStr([ // statements
  11763. 'rtl.createClass($mod,"TObject",null,function(){',
  11764. ' this.vI = 0;',
  11765. ' this.Sub = null;',
  11766. ' this.$init = function () {',
  11767. ' };',
  11768. ' this.$final = function () {',
  11769. ' };',
  11770. ' this.Create = function(){',
  11771. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  11772. ' $mod.TObject.vI = this.GetMore(4);',
  11773. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  11774. ' $mod.TObject.vI = this.GetMore(6);',
  11775. ' return this;',
  11776. ' };',
  11777. ' this.GetMore = function(Par){',
  11778. ' var Result = 0;',
  11779. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  11780. ' $mod.TObject.vI = this.GetMore(12);',
  11781. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  11782. ' $mod.TObject.vI = this.GetMore(14);',
  11783. ' return Result;',
  11784. ' };',
  11785. ' this.GetIt = function(Par){',
  11786. ' var Result = null;',
  11787. ' $mod.TObject.Sub = this.GetIt(21);',
  11788. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  11789. ' $mod.TObject.Sub = this.GetIt(23);',
  11790. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  11791. ' return Result;',
  11792. ' };',
  11793. '});',
  11794. 'this.Obj = null;'
  11795. ]),
  11796. LinesToStr([ // $mod.$main
  11797. '$mod.Obj = $mod.TObject.$create("Create");',
  11798. '$mod.TObject.GetIt(5);',
  11799. '$mod.Obj.$class.GetIt(6);',
  11800. '$mod.Obj.Sub.$class.GetIt(7);',
  11801. '$mod.TObject.Sub=null;',
  11802. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  11803. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  11804. '']));
  11805. end;
  11806. procedure TTestModule.TestClass_Property;
  11807. begin
  11808. StartProgram(false);
  11809. Add('type');
  11810. Add(' TObject = class');
  11811. Add(' Fx: longint;');
  11812. Add(' Fy: longint;');
  11813. Add(' function GetInt: longint;');
  11814. Add(' procedure SetInt(Value: longint);');
  11815. Add(' procedure DoIt;');
  11816. Add(' property IntA: longint read Fx write Fy;');
  11817. Add(' property IntB: longint read GetInt write SetInt;');
  11818. Add(' end;');
  11819. Add('function tobject.getint: longint;');
  11820. Add('begin');
  11821. Add(' result:=fx;');
  11822. Add('end;');
  11823. Add('procedure tobject.setint(value: longint);');
  11824. Add('begin');
  11825. Add(' if value=fy then exit;');
  11826. Add(' fy:=value;');
  11827. Add('end;');
  11828. Add('procedure tobject.doit;');
  11829. Add('begin');
  11830. Add(' IntA:=IntA+1;');
  11831. Add(' Self.IntA:=Self.IntA+1;');
  11832. Add(' IntB:=IntB+1;');
  11833. Add(' Self.IntB:=Self.IntB+1;');
  11834. Add('end;');
  11835. Add('var Obj: tobject;');
  11836. Add('begin');
  11837. Add(' obj.inta:=obj.inta+1;');
  11838. Add(' if obj.intb=2 then;');
  11839. Add(' obj.intb:=obj.intb+2;');
  11840. Add(' obj.setint(obj.inta);');
  11841. ConvertProgram;
  11842. CheckSource('TestClass_Property',
  11843. LinesToStr([ // statements
  11844. 'rtl.createClass($mod, "TObject", null, function () {',
  11845. ' this.$init = function () {',
  11846. ' this.Fx = 0;',
  11847. ' this.Fy = 0;',
  11848. ' };',
  11849. ' this.$final = function () {',
  11850. ' };',
  11851. ' this.GetInt = function () {',
  11852. ' var Result = 0;',
  11853. ' Result = this.Fx;',
  11854. ' return Result;',
  11855. ' };',
  11856. ' this.SetInt = function (Value) {',
  11857. ' if (Value === this.Fy) return;',
  11858. ' this.Fy = Value;',
  11859. ' };',
  11860. ' this.DoIt = function () {',
  11861. ' this.Fy = this.Fx + 1;',
  11862. ' this.Fy = this.Fx + 1;',
  11863. ' this.SetInt(this.GetInt() + 1);',
  11864. ' this.SetInt(this.GetInt() + 1);',
  11865. ' };',
  11866. '});',
  11867. 'this.Obj = null;'
  11868. ]),
  11869. LinesToStr([ // $mod.$main
  11870. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  11871. 'if ($mod.Obj.GetInt() === 2);',
  11872. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  11873. '$mod.Obj.SetInt($mod.Obj.Fx);'
  11874. ]));
  11875. end;
  11876. procedure TTestModule.TestClass_Property_ClassMethod;
  11877. begin
  11878. StartProgram(false);
  11879. Add([
  11880. 'type',
  11881. ' TObject = class',
  11882. ' class var Fx: longint;',
  11883. ' class var Fy: longint;',
  11884. ' class function GetInt: longint;',
  11885. ' class procedure SetInt(Value: longint);',
  11886. ' end;',
  11887. ' TBird = class',
  11888. ' class procedure DoIt;',
  11889. ' class property IntA: longint read Fx write Fy;',
  11890. ' class property IntB: longint read GetInt write SetInt;',
  11891. ' end;',
  11892. 'class function tobject.getint: longint;',
  11893. 'begin',
  11894. ' result:=fx;',
  11895. 'end;',
  11896. 'class procedure tobject.setint(value: longint);',
  11897. 'begin',
  11898. 'end;',
  11899. 'class procedure tbird.doit;',
  11900. 'begin',
  11901. ' FX:=3;',
  11902. ' IntA:=IntA+1;',
  11903. ' Self.IntA:=Self.IntA+1;',
  11904. ' IntB:=IntB+1;',
  11905. ' Self.IntB:=Self.IntB+1;',
  11906. ' with Self do begin',
  11907. ' FX:=11;',
  11908. ' IntA:=IntA+12;',
  11909. ' IntB:=IntB+13;',
  11910. ' end;',
  11911. 'end;',
  11912. 'var Obj: tbird;',
  11913. 'begin',
  11914. ' tbird.fx:=tbird.fx+1;',
  11915. ' tbird.inta:=tbird.inta+1;',
  11916. ' if tbird.intb=2 then;',
  11917. ' tbird.intb:=tbird.intb+2;',
  11918. ' tbird.setint(tbird.inta);',
  11919. ' obj.inta:=obj.inta+1;',
  11920. ' if obj.intb=2 then;',
  11921. ' obj.intb:=obj.intb+2;',
  11922. ' obj.setint(obj.inta);',
  11923. ' with Tbird do begin',
  11924. ' FX:=FY+1;',
  11925. ' inta:=inta+2;',
  11926. ' intb:=intb+3;',
  11927. ' end;',
  11928. ' with Obj do begin',
  11929. ' FX:=FY+1;',
  11930. ' inta:=inta+2;',
  11931. ' intb:=intb+3;',
  11932. ' end;',
  11933. '']);
  11934. ConvertProgram;
  11935. CheckSource('TestClass_Property_ClassMethod',
  11936. LinesToStr([ // statements
  11937. 'rtl.createClass($mod, "TObject", null, function () {',
  11938. ' this.Fx = 0;',
  11939. ' this.Fy = 0;',
  11940. ' this.$init = function () {',
  11941. ' };',
  11942. ' this.$final = function () {',
  11943. ' };',
  11944. ' this.GetInt = function () {',
  11945. ' var Result = 0;',
  11946. ' Result = this.Fx;',
  11947. ' return Result;',
  11948. ' };',
  11949. ' this.SetInt = function (Value) {',
  11950. ' };',
  11951. '});',
  11952. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11953. ' this.DoIt = function () {',
  11954. ' $mod.TObject.Fx = 3;',
  11955. ' $mod.TObject.Fy = this.Fx + 1;',
  11956. ' $mod.TObject.Fy = this.Fx + 1;',
  11957. ' this.SetInt(this.GetInt() + 1);',
  11958. ' this.SetInt(this.GetInt() + 1);',
  11959. ' $mod.TObject.Fx = 11;',
  11960. ' $mod.TObject.Fy = this.Fx + 12;',
  11961. ' this.SetInt(this.GetInt() + 13);',
  11962. ' };',
  11963. '});',
  11964. 'this.Obj = null;'
  11965. ]),
  11966. LinesToStr([ // $mod.$main
  11967. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  11968. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  11969. 'if ($mod.TBird.GetInt() === 2);',
  11970. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  11971. '$mod.TBird.SetInt($mod.TBird.Fx);',
  11972. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  11973. 'if ($mod.Obj.$class.GetInt() === 2);',
  11974. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  11975. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  11976. 'var $with1 = $mod.TBird;',
  11977. '$mod.TObject.Fx = $with1.Fy + 1;',
  11978. '$mod.TObject.Fy = $with1.Fx + 2;',
  11979. '$with1.SetInt($with1.GetInt() + 3);',
  11980. 'var $with2 = $mod.Obj;',
  11981. '$mod.TObject.Fx = $with2.Fy + 1;',
  11982. '$mod.TObject.Fy = $with2.Fx + 2;',
  11983. '$with2.$class.SetInt($with2.$class.GetInt() + 3);',
  11984. '']));
  11985. end;
  11986. procedure TTestModule.TestClass_Property_Indexed;
  11987. begin
  11988. StartProgram(false);
  11989. Add('type');
  11990. Add(' TObject = class');
  11991. Add(' FItems: array of longint;');
  11992. Add(' function GetItems(Index: longint): longint;');
  11993. Add(' procedure SetItems(Index: longint; Value: longint);');
  11994. Add(' procedure DoIt;');
  11995. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  11996. Add(' end;');
  11997. Add('function tobject.getitems(index: longint): longint;');
  11998. Add('begin');
  11999. Add(' Result:=fitems[index];');
  12000. Add('end;');
  12001. Add('procedure tobject.setitems(index: longint; value: longint);');
  12002. Add('begin');
  12003. Add(' fitems[index]:=value;');
  12004. Add('end;');
  12005. Add('procedure tobject.doit;');
  12006. Add('begin');
  12007. Add(' items[1]:=2;');
  12008. Add(' items[3]:=items[4];');
  12009. Add(' self.items[5]:=self.items[6];');
  12010. Add(' items[items[7]]:=items[items[8]];');
  12011. Add('end;');
  12012. Add('var Obj: tobject;');
  12013. Add('begin');
  12014. Add(' obj.Items[11]:=obj.Items[12];');
  12015. ConvertProgram;
  12016. CheckSource('TestClass_Property_Indexed',
  12017. LinesToStr([ // statements
  12018. 'rtl.createClass($mod, "TObject", null, function () {',
  12019. ' this.$init = function () {',
  12020. ' this.FItems = [];',
  12021. ' };',
  12022. ' this.$final = function () {',
  12023. ' this.FItems = undefined;',
  12024. ' };',
  12025. ' this.GetItems = function (Index) {',
  12026. ' var Result = 0;',
  12027. ' Result = this.FItems[Index];',
  12028. ' return Result;',
  12029. ' };',
  12030. ' this.SetItems = function (Index, Value) {',
  12031. ' this.FItems[Index] = Value;',
  12032. ' };',
  12033. ' this.DoIt = function () {',
  12034. ' this.SetItems(1, 2);',
  12035. ' this.SetItems(3,this.GetItems(4));',
  12036. ' this.SetItems(5,this.GetItems(6));',
  12037. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  12038. ' };',
  12039. '});',
  12040. 'this.Obj = null;'
  12041. ]),
  12042. LinesToStr([ // $mod.$main
  12043. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  12044. ]));
  12045. end;
  12046. procedure TTestModule.TestClass_Property_IndexSpec;
  12047. begin
  12048. StartProgram(false);
  12049. Add([
  12050. 'type',
  12051. ' TEnum = (red, blue);',
  12052. ' TObject = class',
  12053. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  12054. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  12055. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  12056. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  12057. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  12058. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  12059. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  12060. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  12061. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  12062. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  12063. ' end;',
  12064. 'procedure DoIt(b: boolean); begin end;',
  12065. 'var',
  12066. ' o: TObject;',
  12067. 'begin',
  12068. ' o.B1:=o.B1;',
  12069. ' o.B2:=o.B2;',
  12070. ' o.B3:=o.B3;',
  12071. ' o.I1[''a'']:=o.I1[''b''];',
  12072. ' doit(o.b1);',
  12073. ' doit(o.b2);',
  12074. ' doit(o.i1[''c'']);',
  12075. '']);
  12076. ConvertProgram;
  12077. CheckSource('TestClass_Property_IndexSpec',
  12078. LinesToStr([ // statements
  12079. 'this.TEnum = {',
  12080. ' "0": "red",',
  12081. ' red: 0,',
  12082. ' "1": "blue",',
  12083. ' blue: 1',
  12084. '};',
  12085. 'rtl.createClass($mod, "TObject", null, function () {',
  12086. ' this.$init = function () {',
  12087. ' };',
  12088. ' this.$final = function () {',
  12089. ' };',
  12090. '});',
  12091. 'this.DoIt = function (b) {',
  12092. '};',
  12093. 'this.o = null;',
  12094. '']),
  12095. LinesToStr([ // $mod.$main
  12096. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  12097. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  12098. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  12099. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  12100. '$mod.DoIt($mod.o.GetIntBool(1));',
  12101. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  12102. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  12103. '']));
  12104. end;
  12105. procedure TTestModule.TestClass_PropertyOfTypeArray;
  12106. begin
  12107. StartProgram(false);
  12108. Add('type');
  12109. Add(' TArray = array of longint;');
  12110. Add(' TObject = class');
  12111. Add(' FItems: TArray;');
  12112. Add(' function GetItems: tarray;');
  12113. Add(' procedure SetItems(Value: tarray);');
  12114. Add(' property Items: tarray read getitems write setitems;');
  12115. Add(' end;');
  12116. Add('function tobject.getitems: tarray;');
  12117. Add('begin');
  12118. Add(' Result:=fitems;');
  12119. Add('end;');
  12120. Add('procedure tobject.setitems(value: tarray);');
  12121. Add('begin');
  12122. Add(' fitems:=value;');
  12123. Add(' fitems:=nil;');
  12124. Add(' Items:=nil;');
  12125. Add(' Items:=Items;');
  12126. Add(' Items[1]:=2;');
  12127. Add(' fitems[3]:=Items[4];');
  12128. Add(' Items[5]:=Items[6];');
  12129. Add(' Self.Items[7]:=8;');
  12130. Add(' Self.Items[9]:=Self.Items[10];');
  12131. Add(' Items[Items[11]]:=Items[Items[12]];');
  12132. Add('end;');
  12133. Add('var Obj: tobject;');
  12134. Add('begin');
  12135. Add(' obj.items:=nil;');
  12136. Add(' obj.items:=obj.items;');
  12137. Add(' obj.items[11]:=obj.items[12];');
  12138. ConvertProgram;
  12139. CheckSource('TestClass_PropertyOfTypeArray',
  12140. LinesToStr([ // statements
  12141. 'rtl.createClass($mod, "TObject", null, function () {',
  12142. ' this.$init = function () {',
  12143. ' this.FItems = [];',
  12144. ' };',
  12145. ' this.$final = function () {',
  12146. ' this.FItems = undefined;',
  12147. ' };',
  12148. ' this.GetItems = function () {',
  12149. ' var Result = [];',
  12150. ' Result = this.FItems;',
  12151. ' return Result;',
  12152. ' };',
  12153. ' this.SetItems = function (Value) {',
  12154. ' this.FItems = Value;',
  12155. ' this.FItems = [];',
  12156. ' this.SetItems([]);',
  12157. ' this.SetItems(this.GetItems());',
  12158. ' this.GetItems()[1] = 2;',
  12159. ' this.FItems[3] = this.GetItems()[4];',
  12160. ' this.GetItems()[5] = this.GetItems()[6];',
  12161. ' this.GetItems()[7] = 8;',
  12162. ' this.GetItems()[9] = this.GetItems()[10];',
  12163. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  12164. ' };',
  12165. '});',
  12166. 'this.Obj = null;'
  12167. ]),
  12168. LinesToStr([ // $mod.$main
  12169. '$mod.Obj.SetItems([]);',
  12170. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  12171. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  12172. ]));
  12173. end;
  12174. procedure TTestModule.TestClass_PropertyDefault;
  12175. begin
  12176. StartProgram(false);
  12177. Add([
  12178. 'type',
  12179. ' TArray = array of longint;',
  12180. ' TObject = class',
  12181. ' FItems: TArray;',
  12182. ' function GetItems(Index: longint): longint;',
  12183. ' procedure SetItems(Index, Value: longint);',
  12184. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  12185. ' end;',
  12186. 'function tobject.getitems(index: longint): longint;',
  12187. 'begin',
  12188. 'end;',
  12189. 'procedure tobject.setitems(index, value: longint);',
  12190. 'begin',
  12191. ' Self[1]:=2;',
  12192. ' Self[3]:=Self[index];',
  12193. ' Self[index]:=Self[Self[value]];',
  12194. ' Self[Self[4]]:=value;',
  12195. 'end;',
  12196. 'var Obj: tobject;',
  12197. 'begin',
  12198. ' obj[11]:=12;',
  12199. ' obj[13]:=obj[14];',
  12200. ' obj[obj[15]]:=obj[obj[15]];',
  12201. ' TObject(obj)[16]:=TObject(obj)[17];']);
  12202. ConvertProgram;
  12203. CheckSource('TestClass_PropertyDefault',
  12204. LinesToStr([ // statements
  12205. 'rtl.createClass($mod, "TObject", null, function () {',
  12206. ' this.$init = function () {',
  12207. ' this.FItems = [];',
  12208. ' };',
  12209. ' this.$final = function () {',
  12210. ' this.FItems = undefined;',
  12211. ' };',
  12212. ' this.GetItems = function (Index) {',
  12213. ' var Result = 0;',
  12214. ' return Result;',
  12215. ' };',
  12216. ' this.SetItems = function (Index, Value) {',
  12217. ' this.SetItems(1, 2);',
  12218. ' this.SetItems(3, this.GetItems(Index));',
  12219. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  12220. ' this.SetItems(this.GetItems(4), Value);',
  12221. ' };',
  12222. '});',
  12223. 'this.Obj = null;'
  12224. ]),
  12225. LinesToStr([ // $mod.$main
  12226. '$mod.Obj.SetItems(11, 12);',
  12227. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  12228. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));',
  12229. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  12230. '']));
  12231. end;
  12232. procedure TTestModule.TestClass_PropertyDefault2;
  12233. begin
  12234. StartProgram(false);
  12235. Add([
  12236. 'type',
  12237. ' TObject = class end;',
  12238. ' TAlphaList = class',
  12239. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  12240. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  12241. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  12242. ' end;',
  12243. ' TBetaList = class',
  12244. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  12245. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  12246. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  12247. ' end;',
  12248. ' TBird = class',
  12249. ' procedure DoIt;',
  12250. ' end;',
  12251. 'procedure TBird.DoIt;',
  12252. 'var',
  12253. ' List: TAlphaList;',
  12254. 'begin',
  12255. ' if TBetaList(List[true])[3]=nil then ;',
  12256. ' TBetaList(List[false])[5]:=nil;',
  12257. 'end;',
  12258. 'var',
  12259. ' List: TAlphaList;',
  12260. 'begin',
  12261. ' if TBetaList(List[true])[3]=nil then ;',
  12262. ' TBetaList(List[false])[5]:=nil;',
  12263. '']);
  12264. ConvertProgram;
  12265. CheckSource('TestClass_PropertyDefault2',
  12266. LinesToStr([ // statements
  12267. 'rtl.createClass($mod, "TObject", null, function () {',
  12268. ' this.$init = function () {',
  12269. ' };',
  12270. ' this.$final = function () {',
  12271. ' };',
  12272. '});',
  12273. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  12274. '});',
  12275. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  12276. '});',
  12277. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12278. ' this.DoIt = function () {',
  12279. ' var List = null;',
  12280. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  12281. ' List.GetAlphas(false).SetBetas(5, null);',
  12282. ' };',
  12283. '});',
  12284. 'this.List = null;',
  12285. '']),
  12286. LinesToStr([ // $mod.$main
  12287. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  12288. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  12289. '']));
  12290. end;
  12291. procedure TTestModule.TestClass_PropertyOverride;
  12292. begin
  12293. StartProgram(false);
  12294. Add('type');
  12295. Add(' integer = longint;');
  12296. Add(' TObject = class');
  12297. Add(' FItem: integer;');
  12298. Add(' function GetItem: integer; external name ''GetItem'';');
  12299. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  12300. Add(' property Item: integer read getitem write setitem;');
  12301. Add(' end;');
  12302. Add(' TCar = class');
  12303. Add(' FBag: integer;');
  12304. Add(' function GetBag: integer; external name ''GetBag'';');
  12305. Add(' property Item read getbag;');
  12306. Add(' end;');
  12307. Add('var');
  12308. Add(' Obj: tobject;');
  12309. Add(' Car: tcar;');
  12310. Add('begin');
  12311. Add(' Obj.Item:=Obj.Item;');
  12312. Add(' Car.Item:=Car.Item;');
  12313. ConvertProgram;
  12314. CheckSource('TestClass_PropertyOverride',
  12315. LinesToStr([ // statements
  12316. 'rtl.createClass($mod, "TObject", null, function () {',
  12317. ' this.$init = function () {',
  12318. ' this.FItem = 0;',
  12319. ' };',
  12320. ' this.$final = function () {',
  12321. ' };',
  12322. '});',
  12323. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12324. ' this.$init = function () {',
  12325. ' $mod.TObject.$init.call(this);',
  12326. ' this.FBag = 0;',
  12327. ' };',
  12328. '});',
  12329. 'this.Obj = null;',
  12330. 'this.Car = null;',
  12331. '']),
  12332. LinesToStr([ // $mod.$main
  12333. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  12334. '$mod.Car.SetItem($mod.Car.GetBag());',
  12335. '']));
  12336. end;
  12337. procedure TTestModule.TestClass_PropertyIncVisibility;
  12338. begin
  12339. AddModuleWithIntfImplSrc('unit1.pp',
  12340. LinesToStr([
  12341. 'type',
  12342. ' TNumber = longint;',
  12343. ' TInteger = longint;',
  12344. ' TObject = class',
  12345. ' private',
  12346. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  12347. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  12348. ' protected',
  12349. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  12350. ' end;']),
  12351. LinesToStr([
  12352. '']));
  12353. StartProgram(true);
  12354. Add([
  12355. 'uses unit1;',
  12356. 'type',
  12357. ' TBird = class',
  12358. ' public',
  12359. ' property Items;',
  12360. ' end;',
  12361. 'procedure DoIt(i: TInteger);',
  12362. 'begin',
  12363. 'end;',
  12364. 'var b: TBird;',
  12365. 'begin',
  12366. ' b.Items[1]:=2;',
  12367. ' b.Items[3]:=b.Items[4];',
  12368. ' DoIt(b.Items[5]);',
  12369. '']);
  12370. ConvertProgram;
  12371. CheckSource('TestClass_PropertyIncVisibility',
  12372. LinesToStr([ // statements
  12373. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  12374. '});',
  12375. 'this.DoIt = function (i) {',
  12376. '};',
  12377. 'this.b = null;'
  12378. ]),
  12379. LinesToStr([ // $mod.$main
  12380. '$mod.b.SetItems(1, 2);',
  12381. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  12382. '$mod.DoIt($mod.b.GetItems(5));'
  12383. ]));
  12384. end;
  12385. procedure TTestModule.TestClass_Assigned;
  12386. begin
  12387. StartProgram(false);
  12388. Add('type');
  12389. Add(' TObject = class');
  12390. Add(' end;');
  12391. Add('var');
  12392. Add(' Obj: tobject;');
  12393. Add(' b: boolean;');
  12394. Add('begin');
  12395. Add(' if Assigned(obj) then ;');
  12396. Add(' b:=Assigned(obj) or false;');
  12397. ConvertProgram;
  12398. CheckSource('TestClass_Assigned',
  12399. LinesToStr([ // statements
  12400. 'rtl.createClass($mod, "TObject", null, function () {',
  12401. ' this.$init = function () {',
  12402. ' };',
  12403. ' this.$final = function () {',
  12404. ' };',
  12405. '});',
  12406. 'this.Obj = null;',
  12407. 'this.b = false;'
  12408. ]),
  12409. LinesToStr([ // $mod.$main
  12410. 'if ($mod.Obj != null);',
  12411. '$mod.b = ($mod.Obj != null) || false;'
  12412. ]));
  12413. end;
  12414. procedure TTestModule.TestClass_WithClassDoCreate;
  12415. begin
  12416. StartProgram(false);
  12417. Add('type');
  12418. Add(' TObject = class');
  12419. Add(' aBool: boolean;');
  12420. Add(' Arr: array of boolean;');
  12421. Add(' constructor Create;');
  12422. Add(' end;');
  12423. Add('constructor TObject.Create; begin end;');
  12424. Add('var');
  12425. Add(' Obj: tobject;');
  12426. Add(' b: boolean;');
  12427. Add('begin');
  12428. Add(' with tobject.create do begin');
  12429. Add(' b:=abool;');
  12430. Add(' abool:=b;');
  12431. Add(' b:=arr[1];');
  12432. Add(' arr[2]:=b;');
  12433. Add(' end;');
  12434. Add(' with tobject do');
  12435. Add(' obj:=create;');
  12436. Add(' with obj do begin');
  12437. Add(' create;');
  12438. Add(' b:=abool;');
  12439. Add(' abool:=b;');
  12440. Add(' b:=arr[3];');
  12441. Add(' arr[4]:=b;');
  12442. Add(' end;');
  12443. ConvertProgram;
  12444. CheckSource('TestClass_WithClassDoCreate',
  12445. LinesToStr([ // statements
  12446. 'rtl.createClass($mod, "TObject", null, function () {',
  12447. ' this.$init = function () {',
  12448. ' this.aBool = false;',
  12449. ' this.Arr = [];',
  12450. ' };',
  12451. ' this.$final = function () {',
  12452. ' this.Arr = undefined;',
  12453. ' };',
  12454. ' this.Create = function () {',
  12455. ' return this;',
  12456. ' };',
  12457. '});',
  12458. 'this.Obj = null;',
  12459. 'this.b = false;'
  12460. ]),
  12461. LinesToStr([ // $mod.$main
  12462. 'var $with1 = $mod.TObject.$create("Create");',
  12463. '$mod.b = $with1.aBool;',
  12464. '$with1.aBool = $mod.b;',
  12465. '$mod.b = $with1.Arr[1];',
  12466. '$with1.Arr[2] = $mod.b;',
  12467. 'var $with2 = $mod.TObject;',
  12468. '$mod.Obj = $with2.$create("Create");',
  12469. 'var $with3 = $mod.Obj;',
  12470. '$with3.Create();',
  12471. '$mod.b = $with3.aBool;',
  12472. '$with3.aBool = $mod.b;',
  12473. '$mod.b = $with3.Arr[3];',
  12474. '$with3.Arr[4] = $mod.b;',
  12475. '']));
  12476. end;
  12477. procedure TTestModule.TestClass_WithClassInstDoProperty;
  12478. begin
  12479. StartProgram(false);
  12480. Add('type');
  12481. Add(' TObject = class');
  12482. Add(' FInt: longint;');
  12483. Add(' constructor Create;');
  12484. Add(' function GetSize: longint;');
  12485. Add(' procedure SetSize(Value: longint);');
  12486. Add(' property Int: longint read FInt write FInt;');
  12487. Add(' property Size: longint read GetSize write SetSize;');
  12488. Add(' end;');
  12489. Add('constructor TObject.Create; begin end;');
  12490. Add('function TObject.GetSize: longint; begin; end;');
  12491. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12492. Add('var');
  12493. Add(' Obj: tobject;');
  12494. Add(' i: longint;');
  12495. Add('begin');
  12496. Add(' with TObject.Create do begin');
  12497. Add(' i:=int;');
  12498. Add(' int:=i;');
  12499. Add(' i:=size;');
  12500. Add(' size:=i;');
  12501. Add(' end;');
  12502. Add(' with obj do begin');
  12503. Add(' i:=int;');
  12504. Add(' int:=i;');
  12505. Add(' i:=size;');
  12506. Add(' size:=i;');
  12507. Add(' end;');
  12508. ConvertProgram;
  12509. CheckSource('TestClass_WithClassInstDoProperty',
  12510. LinesToStr([ // statements
  12511. 'rtl.createClass($mod, "TObject", null, function () {',
  12512. ' this.$init = function () {',
  12513. ' this.FInt = 0;',
  12514. ' };',
  12515. ' this.$final = function () {',
  12516. ' };',
  12517. ' this.Create = function () {',
  12518. ' return this;',
  12519. ' };',
  12520. ' this.GetSize = function () {',
  12521. ' var Result = 0;',
  12522. ' return Result;',
  12523. ' };',
  12524. ' this.SetSize = function (Value) {',
  12525. ' };',
  12526. '});',
  12527. 'this.Obj = null;',
  12528. 'this.i = 0;'
  12529. ]),
  12530. LinesToStr([ // $mod.$main
  12531. 'var $with1 = $mod.TObject.$create("Create");',
  12532. '$mod.i = $with1.FInt;',
  12533. '$with1.FInt = $mod.i;',
  12534. '$mod.i = $with1.GetSize();',
  12535. '$with1.SetSize($mod.i);',
  12536. 'var $with2 = $mod.Obj;',
  12537. '$mod.i = $with2.FInt;',
  12538. '$with2.FInt = $mod.i;',
  12539. '$mod.i = $with2.GetSize();',
  12540. '$with2.SetSize($mod.i);',
  12541. '']));
  12542. end;
  12543. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  12544. begin
  12545. StartProgram(false);
  12546. Add('type');
  12547. Add(' TObject = class');
  12548. Add(' constructor Create;');
  12549. Add(' function GetItems(Index: longint): longint;');
  12550. Add(' procedure SetItems(Index, Value: longint);');
  12551. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  12552. Add(' end;');
  12553. Add('constructor TObject.Create; begin end;');
  12554. Add('function tobject.getitems(index: longint): longint; begin; end;');
  12555. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  12556. Add('var');
  12557. Add(' Obj: tobject;');
  12558. Add(' i: longint;');
  12559. Add('begin');
  12560. Add(' with TObject.Create do begin');
  12561. Add(' i:=Items[1];');
  12562. Add(' Items[2]:=i;');
  12563. Add(' end;');
  12564. Add(' with obj do begin');
  12565. Add(' i:=Items[3];');
  12566. Add(' Items[4]:=i;');
  12567. Add(' end;');
  12568. ConvertProgram;
  12569. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  12570. LinesToStr([ // statements
  12571. 'rtl.createClass($mod, "TObject", null, function () {',
  12572. ' this.$init = function () {',
  12573. ' };',
  12574. ' this.$final = function () {',
  12575. ' };',
  12576. ' this.Create = function () {',
  12577. ' return this;',
  12578. ' };',
  12579. ' this.GetItems = function (Index) {',
  12580. ' var Result = 0;',
  12581. ' return Result;',
  12582. ' };',
  12583. ' this.SetItems = function (Index, Value) {',
  12584. ' };',
  12585. '});',
  12586. 'this.Obj = null;',
  12587. 'this.i = 0;'
  12588. ]),
  12589. LinesToStr([ // $mod.$main
  12590. 'var $with1 = $mod.TObject.$create("Create");',
  12591. '$mod.i = $with1.GetItems(1);',
  12592. '$with1.SetItems(2, $mod.i);',
  12593. 'var $with2 = $mod.Obj;',
  12594. '$mod.i = $with2.GetItems(3);',
  12595. '$with2.SetItems(4, $mod.i);',
  12596. '']));
  12597. end;
  12598. procedure TTestModule.TestClass_WithClassInstDoFunc;
  12599. begin
  12600. StartProgram(false);
  12601. Add('type');
  12602. Add(' TObject = class');
  12603. Add(' constructor Create;');
  12604. Add(' function GetSize: longint;');
  12605. Add(' procedure SetSize(Value: longint);');
  12606. Add(' end;');
  12607. Add('constructor TObject.Create; begin end;');
  12608. Add('function TObject.GetSize: longint; begin; end;');
  12609. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12610. Add('var');
  12611. Add(' Obj: tobject;');
  12612. Add(' i: longint;');
  12613. Add('begin');
  12614. Add(' with TObject.Create do begin');
  12615. Add(' i:=GetSize;');
  12616. Add(' i:=GetSize();');
  12617. Add(' SetSize(i);');
  12618. Add(' end;');
  12619. Add(' with obj do begin');
  12620. Add(' i:=GetSize;');
  12621. Add(' i:=GetSize();');
  12622. Add(' SetSize(i);');
  12623. Add(' end;');
  12624. ConvertProgram;
  12625. CheckSource('TestClass_WithClassInstDoFunc',
  12626. LinesToStr([ // statements
  12627. 'rtl.createClass($mod, "TObject", null, function () {',
  12628. ' this.$init = function () {',
  12629. ' };',
  12630. ' this.$final = function () {',
  12631. ' };',
  12632. ' this.Create = function () {',
  12633. ' return this;',
  12634. ' };',
  12635. ' this.GetSize = function () {',
  12636. ' var Result = 0;',
  12637. ' return Result;',
  12638. ' };',
  12639. ' this.SetSize = function (Value) {',
  12640. ' };',
  12641. '});',
  12642. 'this.Obj = null;',
  12643. 'this.i = 0;'
  12644. ]),
  12645. LinesToStr([ // $mod.$main
  12646. 'var $with1 = $mod.TObject.$create("Create");',
  12647. '$mod.i = $with1.GetSize();',
  12648. '$mod.i = $with1.GetSize();',
  12649. '$with1.SetSize($mod.i);',
  12650. 'var $with2 = $mod.Obj;',
  12651. '$mod.i = $with2.GetSize();',
  12652. '$mod.i = $with2.GetSize();',
  12653. '$with2.SetSize($mod.i);',
  12654. '']));
  12655. end;
  12656. procedure TTestModule.TestClass_TypeCast;
  12657. begin
  12658. StartProgram(false);
  12659. Add('type');
  12660. Add(' TObject = class');
  12661. Add(' Next: TObject;');
  12662. Add(' constructor Create;');
  12663. Add(' end;');
  12664. Add(' TControl = class(TObject)');
  12665. Add(' Arr: array of TObject;');
  12666. Add(' function GetIt(vI: longint = 0): TObject;');
  12667. Add(' end;');
  12668. Add('constructor tobject.create; begin end;');
  12669. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  12670. Add('var');
  12671. Add(' Obj: tobject;');
  12672. Add('begin');
  12673. Add(' obj:=tcontrol(obj).next;');
  12674. Add(' tcontrol(obj):=nil;');
  12675. Add(' obj:=tcontrol(obj);');
  12676. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  12677. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  12678. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  12679. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  12680. ConvertProgram;
  12681. CheckSource('TestClass_TypeCast',
  12682. LinesToStr([ // statements
  12683. 'rtl.createClass($mod, "TObject", null, function () {',
  12684. ' this.$init = function () {',
  12685. ' this.Next = null;',
  12686. ' };',
  12687. ' this.$final = function () {',
  12688. ' this.Next = undefined;',
  12689. ' };',
  12690. ' this.Create = function () {',
  12691. ' return this;',
  12692. ' };',
  12693. '});',
  12694. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  12695. ' this.$init = function () {',
  12696. ' $mod.TObject.$init.call(this);',
  12697. ' this.Arr = [];',
  12698. ' };',
  12699. ' this.$final = function () {',
  12700. ' this.Arr = undefined;',
  12701. ' $mod.TObject.$final.call(this);',
  12702. ' };',
  12703. ' this.GetIt = function (vI) {',
  12704. ' var Result = null;',
  12705. ' return Result;',
  12706. ' };',
  12707. '});',
  12708. 'this.Obj = null;'
  12709. ]),
  12710. LinesToStr([ // $mod.$main
  12711. '$mod.Obj = $mod.Obj.Next;',
  12712. '$mod.Obj = null;',
  12713. '$mod.Obj = $mod.Obj;',
  12714. '$mod.Obj = $mod.Obj.GetIt(0);',
  12715. '$mod.Obj = $mod.Obj.GetIt(0);',
  12716. '$mod.Obj = $mod.Obj.GetIt(1);',
  12717. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  12718. '']));
  12719. end;
  12720. procedure TTestModule.TestClass_TypeCastUntypedParam;
  12721. begin
  12722. StartProgram(false);
  12723. Add('type');
  12724. Add(' TObject = class end;');
  12725. Add('procedure ProcA(var A);');
  12726. Add('begin');
  12727. Add(' TObject(A):=nil;');
  12728. Add(' TObject(A):=TObject(A);');
  12729. Add(' if TObject(A)=nil then ;');
  12730. Add(' if nil=TObject(A) then ;');
  12731. Add('end;');
  12732. Add('procedure ProcB(out A);');
  12733. Add('begin');
  12734. Add(' TObject(A):=nil;');
  12735. Add(' TObject(A):=TObject(A);');
  12736. Add(' if TObject(A)=nil then ;');
  12737. Add(' if nil=TObject(A) then ;');
  12738. Add('end;');
  12739. Add('procedure ProcC(const A);');
  12740. Add('begin');
  12741. Add(' if TObject(A)=nil then ;');
  12742. Add(' if nil=TObject(A) then ;');
  12743. Add('end;');
  12744. Add('var o: TObject;');
  12745. Add('begin');
  12746. Add(' ProcA(o);');
  12747. Add(' ProcB(o);');
  12748. Add(' ProcC(o);');
  12749. ConvertProgram;
  12750. CheckSource('TestClass_TypeCastUntypedParam',
  12751. LinesToStr([ // statements
  12752. 'rtl.createClass($mod, "TObject", null, function () {',
  12753. ' this.$init = function () {',
  12754. ' };',
  12755. ' this.$final = function () {',
  12756. ' };',
  12757. '});',
  12758. 'this.ProcA = function (A) {',
  12759. ' A.set(null);',
  12760. ' A.set(A.get());',
  12761. ' if (A.get() === null);',
  12762. ' if (null === A.get());',
  12763. '};',
  12764. 'this.ProcB = function (A) {',
  12765. ' A.set(null);',
  12766. ' A.set(A.get());',
  12767. ' if (A.get() === null);',
  12768. ' if (null === A.get());',
  12769. '};',
  12770. 'this.ProcC = function (A) {',
  12771. ' if (A === null);',
  12772. ' if (null === A);',
  12773. '};',
  12774. 'this.o = null;',
  12775. '']),
  12776. LinesToStr([ // $mod.$main
  12777. '$mod.ProcA({',
  12778. ' p: $mod,',
  12779. ' get: function () {',
  12780. ' return this.p.o;',
  12781. ' },',
  12782. ' set: function (v) {',
  12783. ' this.p.o = v;',
  12784. ' }',
  12785. '});',
  12786. '$mod.ProcB({',
  12787. ' p: $mod,',
  12788. ' get: function () {',
  12789. ' return this.p.o;',
  12790. ' },',
  12791. ' set: function (v) {',
  12792. ' this.p.o = v;',
  12793. ' }',
  12794. '});',
  12795. '$mod.ProcC($mod.o);',
  12796. '']));
  12797. end;
  12798. procedure TTestModule.TestClass_Overloads;
  12799. begin
  12800. StartProgram(false);
  12801. Add('type');
  12802. Add(' TObject = class');
  12803. Add(' procedure DoIt;');
  12804. Add(' procedure DoIt(vI: longint);');
  12805. Add(' end;');
  12806. Add('procedure TObject.DoIt;');
  12807. Add('begin');
  12808. Add(' DoIt;');
  12809. Add(' DoIt(1);');
  12810. Add('end;');
  12811. Add('procedure TObject.DoIt(vI: longint); begin end;');
  12812. Add('begin');
  12813. ConvertProgram;
  12814. CheckSource('TestClass_Overloads',
  12815. LinesToStr([ // statements
  12816. 'rtl.createClass($mod, "TObject", null, function () {',
  12817. ' this.$init = function () {',
  12818. ' };',
  12819. ' this.$final = function () {',
  12820. ' };',
  12821. ' this.DoIt = function () {',
  12822. ' this.DoIt();',
  12823. ' this.DoIt$1(1);',
  12824. ' };',
  12825. ' this.DoIt$1 = function (vI) {',
  12826. ' };',
  12827. '});',
  12828. '']),
  12829. LinesToStr([ // $mod.$main
  12830. '']));
  12831. end;
  12832. procedure TTestModule.TestClass_OverloadsAncestor;
  12833. begin
  12834. StartProgram(false);
  12835. Add('type');
  12836. Add(' TObject = class;');
  12837. Add(' TObject = class');
  12838. Add(' procedure DoIt(vA: longint);');
  12839. Add(' procedure DoIt(vA, vB: longint);');
  12840. Add(' end;');
  12841. Add(' TCar = class;');
  12842. Add(' TCar = class');
  12843. Add(' procedure DoIt(vA: longint);');
  12844. Add(' procedure DoIt(vA, vB: longint);');
  12845. Add(' end;');
  12846. Add('procedure tobject.doit(va: longint);');
  12847. Add('begin');
  12848. Add(' doit(1);');
  12849. Add(' doit(1,2);');
  12850. Add('end;');
  12851. Add('procedure tobject.doit(va, vb: longint); begin end;');
  12852. Add('procedure tcar.doit(va: longint);');
  12853. Add('begin');
  12854. Add(' doit(1);');
  12855. Add(' doit(1,2);');
  12856. Add(' inherited doit(1);');
  12857. Add(' inherited doit(1,2);');
  12858. Add('end;');
  12859. Add('procedure tcar.doit(va, vb: longint); begin end;');
  12860. Add('begin');
  12861. ConvertProgram;
  12862. CheckSource('TestClass_OverloadsAncestor',
  12863. LinesToStr([ // statements
  12864. 'rtl.createClass($mod, "TObject", null, function () {',
  12865. ' this.$init = function () {',
  12866. ' };',
  12867. ' this.$final = function () {',
  12868. ' };',
  12869. ' this.DoIt = function (vA) {',
  12870. ' this.DoIt(1);',
  12871. ' this.DoIt$1(1,2);',
  12872. ' };',
  12873. ' this.DoIt$1 = function (vA, vB) {',
  12874. ' };',
  12875. '});',
  12876. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12877. ' this.DoIt$2 = function (vA) {',
  12878. ' this.DoIt$2(1);',
  12879. ' this.DoIt$3(1, 2);',
  12880. ' $mod.TObject.DoIt.call(this, 1);',
  12881. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  12882. ' };',
  12883. ' this.DoIt$3 = function (vA, vB) {',
  12884. ' };',
  12885. '});',
  12886. '']),
  12887. LinesToStr([ // $mod.$main
  12888. '']));
  12889. end;
  12890. procedure TTestModule.TestClass_OverloadConstructor;
  12891. begin
  12892. StartProgram(false);
  12893. Add('type');
  12894. Add(' TObject = class');
  12895. Add(' constructor Create(vA: longint);');
  12896. Add(' constructor Create(vA, vB: longint);');
  12897. Add(' end;');
  12898. Add(' TCar = class');
  12899. Add(' constructor Create(vA: longint);');
  12900. Add(' constructor Create(vA, vB: longint);');
  12901. Add(' end;');
  12902. Add('constructor tobject.create(va: longint);');
  12903. Add('begin');
  12904. Add(' create(1);');
  12905. Add(' create(1,2);');
  12906. Add('end;');
  12907. Add('constructor tobject.create(va, vb: longint); begin end;');
  12908. Add('constructor tcar.create(va: longint);');
  12909. Add('begin');
  12910. Add(' create(1);');
  12911. Add(' create(1,2);');
  12912. Add(' inherited create(1);');
  12913. Add(' inherited create(1,2);');
  12914. Add('end;');
  12915. Add('constructor tcar.create(va, vb: longint); begin end;');
  12916. Add('begin');
  12917. Add(' tobject.create(1);');
  12918. Add(' tobject.create(1,2);');
  12919. Add(' tcar.create(1);');
  12920. Add(' tcar.create(1,2);');
  12921. ConvertProgram;
  12922. CheckSource('TestClass_OverloadConstructor',
  12923. LinesToStr([ // statements
  12924. 'rtl.createClass($mod, "TObject", null, function () {',
  12925. ' this.$init = function () {',
  12926. ' };',
  12927. ' this.$final = function () {',
  12928. ' };',
  12929. ' this.Create = function (vA) {',
  12930. ' this.Create(1);',
  12931. ' this.Create$1(1,2);',
  12932. ' return this;',
  12933. ' };',
  12934. ' this.Create$1 = function (vA, vB) {',
  12935. ' return this;',
  12936. ' };',
  12937. '});',
  12938. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12939. ' this.Create$2 = function (vA) {',
  12940. ' this.Create$2(1);',
  12941. ' this.Create$3(1, 2);',
  12942. ' $mod.TObject.Create.call(this, 1);',
  12943. ' $mod.TObject.Create$1.call(this, 1, 2);',
  12944. ' return this;',
  12945. ' };',
  12946. ' this.Create$3 = function (vA, vB) {',
  12947. ' return this;',
  12948. ' };',
  12949. '});',
  12950. '']),
  12951. LinesToStr([ // $mod.$main
  12952. '$mod.TObject.$create("Create", [1]);',
  12953. '$mod.TObject.$create("Create$1", [1, 2]);',
  12954. '$mod.TCar.$create("Create$2", [1]);',
  12955. '$mod.TCar.$create("Create$3", [1, 2]);',
  12956. '']));
  12957. end;
  12958. procedure TTestModule.TestClass_OverloadDelphiOverride;
  12959. begin
  12960. StartProgram(false);
  12961. Add([
  12962. '{$mode delphi}',
  12963. 'type',
  12964. ' TObject = class end;',
  12965. ' TBird = class',
  12966. ' function {#a}GetValue: longint; overload; virtual;',
  12967. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  12968. ' end;',
  12969. ' TEagle = class(TBird)',
  12970. ' function {#c}GetValue: longint; overload; override;',
  12971. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  12972. ' end;',
  12973. 'function TBird.GetValue: longint;',
  12974. 'begin',
  12975. ' if 3={@a}GetValue then ;',
  12976. ' if 4={@b}GetValue(5) then ;',
  12977. 'end;',
  12978. 'function TBird.GetValue(AValue: longint): longint;',
  12979. 'begin',
  12980. 'end;',
  12981. 'function TEagle.GetValue: longint;',
  12982. 'begin',
  12983. ' if 13={@c}GetValue then ;',
  12984. ' if 14={@d}GetValue(15) then ;',
  12985. ' if 15=inherited {@a}GetValue then ;',
  12986. ' if 16=inherited {@b}GetValue(17) then ;',
  12987. 'end;',
  12988. 'function TEagle.GetValue(AValue: longint): longint;',
  12989. 'begin',
  12990. 'end;',
  12991. 'var',
  12992. ' e: TEagle;',
  12993. 'begin',
  12994. ' if 23=e.{@c}GetValue then ;',
  12995. ' if 24=e.{@d}GetValue(25) then ;']);
  12996. ConvertProgram;
  12997. CheckSource('TestClass_OverloadDelphiOverride',
  12998. LinesToStr([ // statements
  12999. 'rtl.createClass($mod, "TObject", null, function () {',
  13000. ' this.$init = function () {',
  13001. ' };',
  13002. ' this.$final = function () {',
  13003. ' };',
  13004. '});',
  13005. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13006. ' this.GetValue = function () {',
  13007. ' var Result = 0;',
  13008. ' if (3 === this.GetValue()) ;',
  13009. ' if (4 === this.GetValue$1(5)) ;',
  13010. ' return Result;',
  13011. ' };',
  13012. ' this.GetValue$1 = function (AValue) {',
  13013. ' var Result = 0;',
  13014. ' return Result;',
  13015. ' };',
  13016. '});',
  13017. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13018. ' this.GetValue = function () {',
  13019. ' var Result = 0;',
  13020. ' if (13 === this.GetValue()) ;',
  13021. ' if (14 === this.GetValue$1(15)) ;',
  13022. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  13023. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  13024. ' return Result;',
  13025. ' };',
  13026. ' this.GetValue$1 = function (AValue) {',
  13027. ' var Result = 0;',
  13028. ' return Result;',
  13029. ' };',
  13030. '});',
  13031. 'this.e = null;',
  13032. '']),
  13033. LinesToStr([ // $mod.$main
  13034. 'if (23 === $mod.e.GetValue()) ;',
  13035. 'if (24 === $mod.e.GetValue$1(25)) ;',
  13036. '']));
  13037. end;
  13038. procedure TTestModule.TestClass_ReintroducedVar;
  13039. begin
  13040. StartProgram(false);
  13041. Add('type');
  13042. Add(' TObject = class');
  13043. Add(' strict private');
  13044. Add(' Some: longint;');
  13045. Add(' end;');
  13046. Add(' TMobile = class');
  13047. Add(' strict private');
  13048. Add(' Some: string;');
  13049. Add(' end;');
  13050. Add(' TCar = class(tmobile)');
  13051. Add(' procedure Some;');
  13052. Add(' procedure Some(vA: longint);');
  13053. Add(' end;');
  13054. Add('procedure tcar.some;');
  13055. Add('begin');
  13056. Add(' Some;');
  13057. Add(' Some(1);');
  13058. Add('end;');
  13059. Add('procedure tcar.some(va: longint); begin end;');
  13060. Add('begin');
  13061. ConvertProgram;
  13062. CheckSource('TestClass_ReintroducedVar',
  13063. LinesToStr([ // statements
  13064. 'rtl.createClass($mod, "TObject", null, function () {',
  13065. ' this.$init = function () {',
  13066. ' this.Some = 0;',
  13067. ' };',
  13068. ' this.$final = function () {',
  13069. ' };',
  13070. '});',
  13071. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13072. ' this.$init = function () {',
  13073. ' $mod.TObject.$init.call(this);',
  13074. ' this.Some$1 = "";',
  13075. ' };',
  13076. '});',
  13077. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13078. ' this.Some$2 = function () {',
  13079. ' this.Some$2();',
  13080. ' this.Some$3(1);',
  13081. ' };',
  13082. ' this.Some$3 = function (vA) {',
  13083. ' };',
  13084. '});',
  13085. '']),
  13086. LinesToStr([ // $mod.$main
  13087. '']));
  13088. end;
  13089. procedure TTestModule.TestClass_RaiseDescendant;
  13090. begin
  13091. StartProgram(false);
  13092. Add([
  13093. 'type',
  13094. ' TObject = class',
  13095. ' constructor Create(Msg: string);',
  13096. ' end;',
  13097. ' Exception = class',
  13098. ' end;',
  13099. ' EConvertError = class(Exception)',
  13100. ' end;',
  13101. 'constructor TObject.Create(Msg: string); begin end;',
  13102. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  13103. 'begin',
  13104. ' raise Exception.Create(''Bar1'');',
  13105. ' raise EConvertError.Create(''Bar2'');',
  13106. ' raise AssertConv(''Bar2'');',
  13107. ' raise AssertConv;',
  13108. '']);
  13109. ConvertProgram;
  13110. CheckSource('TestClass_RaiseDescendant',
  13111. LinesToStr([ // statements
  13112. 'rtl.createClass($mod, "TObject", null, function () {',
  13113. ' this.$init = function () {',
  13114. ' };',
  13115. ' this.$final = function () {',
  13116. ' };',
  13117. ' this.Create = function (Msg) {',
  13118. ' return this;',
  13119. ' };',
  13120. '});',
  13121. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  13122. '});',
  13123. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  13124. '});',
  13125. 'this.AssertConv = function (Msg) {',
  13126. ' var Result = null;',
  13127. ' return Result;',
  13128. '};',
  13129. '']),
  13130. LinesToStr([ // $mod.$main
  13131. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  13132. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  13133. 'throw $mod.AssertConv("Bar2");',
  13134. 'throw $mod.AssertConv("def");',
  13135. '']));
  13136. end;
  13137. procedure TTestModule.TestClass_ExternalMethod;
  13138. begin
  13139. AddModuleWithIntfImplSrc('unit2.pas',
  13140. LinesToStr([
  13141. 'type',
  13142. ' TObject = class',
  13143. ' public',
  13144. ' procedure Intern; external name ''$DoIntern'';',
  13145. ' end;',
  13146. '']),
  13147. LinesToStr([
  13148. '']));
  13149. StartUnit(true);
  13150. Add('interface');
  13151. Add('uses unit2;');
  13152. Add('type');
  13153. Add(' TCar = class(TObject)');
  13154. Add(' public');
  13155. Add(' procedure Intern2; external name ''$DoIntern2'';');
  13156. Add(' procedure DoIt;');
  13157. Add(' end;');
  13158. Add('implementation');
  13159. Add('procedure tcar.doit;');
  13160. Add('begin');
  13161. Add(' Intern;');
  13162. Add(' Intern();');
  13163. Add(' Intern2;');
  13164. Add(' Intern2();');
  13165. Add('end;');
  13166. Add('var Obj: TCar;');
  13167. Add('begin');
  13168. Add(' obj.intern;');
  13169. Add(' obj.intern();');
  13170. Add(' obj.intern2;');
  13171. Add(' obj.intern2();');
  13172. Add(' obj.doit;');
  13173. Add(' obj.doit();');
  13174. Add(' with obj do begin');
  13175. Add(' Intern;');
  13176. Add(' Intern();');
  13177. Add(' Intern2;');
  13178. Add(' Intern2();');
  13179. Add(' end;');
  13180. ConvertUnit;
  13181. CheckSource('TestClass_ExternalMethod',
  13182. LinesToStr([
  13183. 'var $impl = $mod.$impl;',
  13184. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13185. ' this.DoIt = function () {',
  13186. ' this.$DoIntern();',
  13187. ' this.$DoIntern();',
  13188. ' this.$DoIntern2();',
  13189. ' this.$DoIntern2();',
  13190. ' };',
  13191. ' });',
  13192. '']),
  13193. LinesToStr([ // this.$init
  13194. '$impl.Obj.$DoIntern();',
  13195. '$impl.Obj.$DoIntern();',
  13196. '$impl.Obj.$DoIntern2();',
  13197. '$impl.Obj.$DoIntern2();',
  13198. '$impl.Obj.DoIt();',
  13199. '$impl.Obj.DoIt();',
  13200. 'var $with1 = $impl.Obj;',
  13201. '$with1.$DoIntern();',
  13202. '$with1.$DoIntern();',
  13203. '$with1.$DoIntern2();',
  13204. '$with1.$DoIntern2();',
  13205. '']),
  13206. LinesToStr([ // implementation
  13207. '$impl.Obj = null;',
  13208. '']) );
  13209. end;
  13210. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  13211. begin
  13212. StartProgram(false);
  13213. Add('type');
  13214. Add(' TObject = class');
  13215. Add(' procedure DoIt; virtual; external name ''Foo'';');
  13216. Add(' end;');
  13217. Add('begin');
  13218. SetExpectedPasResolverError('Virtual method name must match external',
  13219. nVirtualMethodNameMustMatchExternal);
  13220. ConvertProgram;
  13221. end;
  13222. procedure TTestModule.TestClass_ExternalOverrideFail;
  13223. begin
  13224. StartProgram(false);
  13225. Add('type');
  13226. Add(' TObject = class');
  13227. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  13228. Add(' end;');
  13229. Add(' TCar = class');
  13230. Add(' procedure DoIt; override; external name ''DoIt'';');
  13231. Add(' end;');
  13232. Add('begin');
  13233. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  13234. nInvalidXModifierY);
  13235. ConvertProgram;
  13236. end;
  13237. procedure TTestModule.TestClass_ExternalVar;
  13238. begin
  13239. AddModuleWithIntfImplSrc('unit2.pas',
  13240. LinesToStr([
  13241. '{$modeswitch externalclass}',
  13242. 'type',
  13243. ' TObject = class',
  13244. ' public',
  13245. ' Intern: longint external name ''$Intern'';',
  13246. ' Bracket: longint external name ''["A B"]'';',
  13247. ' end;',
  13248. '']),
  13249. LinesToStr([
  13250. '']));
  13251. StartUnit(true);
  13252. Add([
  13253. 'interface',
  13254. 'uses unit2;',
  13255. '{$modeswitch externalclass}',
  13256. 'type',
  13257. ' TCar = class(tobject)',
  13258. ' public',
  13259. ' Intern2: longint external name ''$Intern2'';',
  13260. ' procedure DoIt;',
  13261. ' end;',
  13262. 'implementation',
  13263. 'procedure tcar.doit;',
  13264. 'begin',
  13265. ' Intern:=Intern+1;',
  13266. ' Intern2:=Intern2+2;',
  13267. ' Bracket:=Bracket+3;',
  13268. 'end;',
  13269. 'var Obj: TCar;',
  13270. 'begin',
  13271. ' obj.intern:=obj.intern+1;',
  13272. ' obj.intern2:=obj.intern2+2;',
  13273. ' obj.Bracket:=obj.Bracket+3;',
  13274. ' with obj do begin',
  13275. ' intern:=intern+1;',
  13276. ' intern2:=intern2+2;',
  13277. ' Bracket:=Bracket+3;',
  13278. ' end;']);
  13279. ConvertUnit;
  13280. CheckSource('TestClass_ExternalVar',
  13281. LinesToStr([
  13282. 'var $impl = $mod.$impl;',
  13283. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13284. ' this.DoIt = function () {',
  13285. ' this.$Intern = this.$Intern + 1;',
  13286. ' this.$Intern2 = this.$Intern2 + 2;',
  13287. ' this["A B"] = this["A B"] + 3;',
  13288. ' };',
  13289. ' });',
  13290. '']),
  13291. LinesToStr([
  13292. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  13293. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  13294. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  13295. 'var $with1 = $impl.Obj;',
  13296. '$with1.$Intern = $with1.$Intern + 1;',
  13297. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  13298. '$with1["A B"] = $with1["A B"] + 3;',
  13299. '']),
  13300. LinesToStr([ // implementation
  13301. '$impl.Obj = null;',
  13302. '']));
  13303. end;
  13304. procedure TTestModule.TestClass_Const;
  13305. begin
  13306. StartProgram(false);
  13307. Add('type');
  13308. Add(' integer = longint;');
  13309. Add(' TClass = class of TObject;');
  13310. Add(' TObject = class');
  13311. Add(' public');
  13312. Add(' const cI: integer = 3;');
  13313. Add(' procedure DoIt;');
  13314. Add(' class procedure DoMore;');
  13315. Add(' end;');
  13316. Add('implementation');
  13317. Add('procedure tobject.doit;');
  13318. Add('begin');
  13319. Add(' if cI=4 then;');
  13320. Add(' if 5=cI then;');
  13321. Add(' if Self.cI=6 then;');
  13322. Add(' if 7=Self.cI then;');
  13323. Add(' with Self do begin');
  13324. Add(' if cI=11 then;');
  13325. Add(' if 12=cI then;');
  13326. Add(' end;');
  13327. Add('end;');
  13328. Add('class procedure tobject.domore;');
  13329. Add('begin');
  13330. Add(' if cI=8 then;');
  13331. Add(' if Self.cI=9 then;');
  13332. Add(' if 10=cI then;');
  13333. Add(' if 11=Self.cI then;');
  13334. Add(' with Self do begin');
  13335. Add(' if cI=13 then;');
  13336. Add(' if 14=cI then;');
  13337. Add(' end;');
  13338. Add('end;');
  13339. Add('var');
  13340. Add(' Obj: TObject;');
  13341. Add(' Cla: TClass;');
  13342. Add('begin');
  13343. Add(' if TObject.cI=21 then ;');
  13344. Add(' if Obj.cI=22 then ;');
  13345. Add(' if Cla.cI=23 then ;');
  13346. Add(' with obj do if ci=24 then;');
  13347. Add(' with TObject do if ci=25 then;');
  13348. Add(' with Cla do if ci=26 then;');
  13349. ConvertProgram;
  13350. CheckSource('TestClass_Const',
  13351. LinesToStr([
  13352. 'rtl.createClass($mod, "TObject", null, function () {',
  13353. ' this.cI = 3;',
  13354. ' this.$init = function () {',
  13355. ' };',
  13356. ' this.$final = function () {',
  13357. ' };',
  13358. ' this.DoIt = function () {',
  13359. ' if (this.cI === 4) ;',
  13360. ' if (5 === this.cI) ;',
  13361. ' if (this.cI === 6) ;',
  13362. ' if (7 === this.cI) ;',
  13363. ' if (this.cI === 11) ;',
  13364. ' if (12 === this.cI) ;',
  13365. ' };',
  13366. ' this.DoMore = function () {',
  13367. ' if (this.cI === 8) ;',
  13368. ' if (this.cI === 9) ;',
  13369. ' if (10 === this.cI) ;',
  13370. ' if (11 === this.cI) ;',
  13371. ' if (this.cI === 13) ;',
  13372. ' if (14 === this.cI) ;',
  13373. ' };',
  13374. '});',
  13375. 'this.Obj = null;',
  13376. 'this.Cla = null;',
  13377. '']),
  13378. LinesToStr([
  13379. 'if ($mod.TObject.cI === 21) ;',
  13380. 'if ($mod.Obj.cI === 22) ;',
  13381. 'if ($mod.Cla.cI === 23) ;',
  13382. 'var $with1 = $mod.Obj;',
  13383. 'if ($with1.cI === 24) ;',
  13384. 'var $with2 = $mod.TObject;',
  13385. 'if ($with2.cI === 25) ;',
  13386. 'var $with3 = $mod.Cla;',
  13387. 'if ($with3.cI === 26) ;',
  13388. '']));
  13389. end;
  13390. procedure TTestModule.TestClass_LocalVarSelfFail;
  13391. begin
  13392. StartProgram(false);
  13393. Add([
  13394. 'type',
  13395. ' TObject = class',
  13396. ' constructor Create;',
  13397. ' end;',
  13398. 'constructor tobject.create;',
  13399. 'var self: longint;',
  13400. 'begin',
  13401. 'end',
  13402. 'begin',
  13403. '']);
  13404. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  13405. ConvertProgram;
  13406. end;
  13407. procedure TTestModule.TestClass_ArgSelfFail;
  13408. begin
  13409. StartProgram(false);
  13410. Add([
  13411. 'type',
  13412. ' TObject = class',
  13413. ' procedure DoIt(Self: longint);',
  13414. ' end;',
  13415. 'procedure tobject.doit(self: longint);',
  13416. 'begin',
  13417. 'end',
  13418. 'begin',
  13419. '']);
  13420. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  13421. ConvertProgram;
  13422. end;
  13423. procedure TTestModule.TestClass_NestedProcSelf;
  13424. begin
  13425. StartProgram(false);
  13426. Add([
  13427. 'type',
  13428. ' TObject = class',
  13429. ' Key: longint;',
  13430. ' class var State: longint;',
  13431. ' procedure DoIt;',
  13432. ' function GetSize: longint; virtual; abstract;',
  13433. ' procedure SetSize(Value: longint); virtual; abstract;',
  13434. ' property Size: longint read GetSize write SetSize;',
  13435. ' end;',
  13436. 'procedure tobject.doit;',
  13437. ' procedure Sub;',
  13438. ' begin',
  13439. ' key:=key+2;',
  13440. ' self.key:=self.key+3;',
  13441. ' state:=state+4;',
  13442. ' self.state:=self.state+5;',
  13443. ' tobject.state:=tobject.state+6;',
  13444. ' size:=size+7;',
  13445. ' self.size:=self.size+8;',
  13446. ' end;',
  13447. 'begin',
  13448. ' sub;',
  13449. ' key:=key+12;',
  13450. ' self.key:=self.key+13;',
  13451. ' state:=state+14;',
  13452. ' self.state:=self.state+15;',
  13453. ' tobject.state:=tobject.state+16;',
  13454. ' size:=size+17;',
  13455. ' self.size:=self.size+18;',
  13456. 'end;',
  13457. 'begin',
  13458. '']);
  13459. ConvertProgram;
  13460. CheckSource('TestClass_NestedProcSelf',
  13461. LinesToStr([ // statements
  13462. 'rtl.createClass($mod, "TObject", null, function () {',
  13463. ' this.State = 0;',
  13464. ' this.$init = function () {',
  13465. ' this.Key = 0;',
  13466. ' };',
  13467. ' this.$final = function () {',
  13468. ' };',
  13469. ' this.DoIt = function () {',
  13470. ' var $Self = this;',
  13471. ' function Sub() {',
  13472. ' $Self.Key = $Self.Key + 2;',
  13473. ' $Self.Key = $Self.Key + 3;',
  13474. ' $mod.TObject.State = $Self.State + 4;',
  13475. ' $mod.TObject.State = $Self.State + 5;',
  13476. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13477. ' $Self.SetSize($Self.GetSize() + 7);',
  13478. ' $Self.SetSize($Self.GetSize() + 8);',
  13479. ' };',
  13480. ' Sub();',
  13481. ' $Self.Key = $Self.Key + 12;',
  13482. ' $Self.Key = $Self.Key + 13;',
  13483. ' $mod.TObject.State = $Self.State + 14;',
  13484. ' $mod.TObject.State = $Self.State + 15;',
  13485. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13486. ' $Self.SetSize($Self.GetSize() + 17);',
  13487. ' $Self.SetSize($Self.GetSize() + 18);',
  13488. ' };',
  13489. '});',
  13490. '']),
  13491. LinesToStr([ // $mod.$main
  13492. '']));
  13493. end;
  13494. procedure TTestModule.TestClass_NestedProcSelf2;
  13495. begin
  13496. StartProgram(false);
  13497. Add([
  13498. 'type',
  13499. ' TObject = class',
  13500. ' Key: longint;',
  13501. ' class var State: longint;',
  13502. ' function GetSize: longint; virtual; abstract;',
  13503. ' procedure SetSize(Value: longint); virtual; abstract;',
  13504. ' property Size: longint read GetSize write SetSize;',
  13505. ' end;',
  13506. ' TBird = class',
  13507. ' procedure DoIt;',
  13508. ' end;',
  13509. 'procedure tbird.doit;',
  13510. ' procedure Sub;',
  13511. ' begin',
  13512. ' key:=key+2;',
  13513. ' self.key:=self.key+3;',
  13514. ' state:=state+4;',
  13515. ' self.state:=self.state+5;',
  13516. ' tobject.state:=tobject.state+6;',
  13517. ' size:=size+7;',
  13518. ' self.size:=self.size+8;',
  13519. ' end;',
  13520. 'begin',
  13521. ' sub;',
  13522. ' key:=key+12;',
  13523. ' self.key:=self.key+13;',
  13524. ' state:=state+14;',
  13525. ' self.state:=self.state+15;',
  13526. ' tobject.state:=tobject.state+16;',
  13527. ' size:=size+17;',
  13528. ' self.size:=self.size+18;',
  13529. 'end;',
  13530. 'begin',
  13531. '']);
  13532. ConvertProgram;
  13533. CheckSource('TestClass_NestedProcSelf2',
  13534. LinesToStr([ // statements
  13535. 'rtl.createClass($mod, "TObject", null, function () {',
  13536. ' this.State = 0;',
  13537. ' this.$init = function () {',
  13538. ' this.Key = 0;',
  13539. ' };',
  13540. ' this.$final = function () {',
  13541. ' };',
  13542. '});',
  13543. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13544. ' this.DoIt = function () {',
  13545. ' var $Self = this;',
  13546. ' function Sub() {',
  13547. ' $Self.Key = $Self.Key + 2;',
  13548. ' $Self.Key = $Self.Key + 3;',
  13549. ' $mod.TObject.State = $Self.State + 4;',
  13550. ' $mod.TObject.State = $Self.State + 5;',
  13551. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13552. ' $Self.SetSize($Self.GetSize() + 7);',
  13553. ' $Self.SetSize($Self.GetSize() + 8);',
  13554. ' };',
  13555. ' Sub();',
  13556. ' $Self.Key = $Self.Key + 12;',
  13557. ' $Self.Key = $Self.Key + 13;',
  13558. ' $mod.TObject.State = $Self.State + 14;',
  13559. ' $mod.TObject.State = $Self.State + 15;',
  13560. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13561. ' $Self.SetSize($Self.GetSize() + 17);',
  13562. ' $Self.SetSize($Self.GetSize() + 18);',
  13563. ' };',
  13564. '});',
  13565. '']),
  13566. LinesToStr([ // $mod.$main
  13567. '']));
  13568. end;
  13569. procedure TTestModule.TestClass_NestedProcClassSelf;
  13570. begin
  13571. StartProgram(false);
  13572. Add([
  13573. 'type',
  13574. ' TObject = class',
  13575. ' class var State: longint;',
  13576. ' class procedure DoIt;',
  13577. ' class function GetSize: longint; virtual; abstract;',
  13578. ' class procedure SetSize(Value: longint); virtual; abstract;',
  13579. ' class property Size: longint read GetSize write SetSize;',
  13580. ' end;',
  13581. 'class procedure tobject.doit;',
  13582. ' procedure Sub;',
  13583. ' begin',
  13584. ' state:=state+2;',
  13585. ' self.state:=self.state+3;',
  13586. ' tobject.state:=tobject.state+4;',
  13587. ' size:=size+5;',
  13588. ' self.size:=self.size+6;',
  13589. ' tobject.size:=tobject.size+7;',
  13590. ' end;',
  13591. 'begin',
  13592. ' sub;',
  13593. ' state:=state+12;',
  13594. ' self.state:=self.state+13;',
  13595. ' tobject.state:=tobject.state+14;',
  13596. ' size:=size+15;',
  13597. ' self.size:=self.size+16;',
  13598. ' tobject.size:=tobject.size+17;',
  13599. 'end;',
  13600. 'begin',
  13601. '']);
  13602. ConvertProgram;
  13603. CheckSource('TestClass_NestedProcClassSelf',
  13604. LinesToStr([ // statements
  13605. 'rtl.createClass($mod, "TObject", null, function () {',
  13606. ' this.State = 0;',
  13607. ' this.$init = function () {',
  13608. ' };',
  13609. ' this.$final = function () {',
  13610. ' };',
  13611. ' this.DoIt = function () {',
  13612. ' var $Self = this;',
  13613. ' function Sub() {',
  13614. ' $mod.TObject.State = $Self.State + 2;',
  13615. ' $mod.TObject.State = $Self.State + 3;',
  13616. ' $mod.TObject.State = $mod.TObject.State + 4;',
  13617. ' $Self.SetSize($Self.GetSize() + 5);',
  13618. ' $Self.SetSize($Self.GetSize() + 6);',
  13619. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  13620. ' };',
  13621. ' Sub();',
  13622. ' $mod.TObject.State = $Self.State + 12;',
  13623. ' $mod.TObject.State = $Self.State + 13;',
  13624. ' $mod.TObject.State = $mod.TObject.State + 14;',
  13625. ' $Self.SetSize($Self.GetSize() + 15);',
  13626. ' $Self.SetSize($Self.GetSize() + 16);',
  13627. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  13628. ' };',
  13629. '});',
  13630. '']),
  13631. LinesToStr([ // $mod.$main
  13632. '']));
  13633. end;
  13634. procedure TTestModule.TestClass_NestedProcCallInherited;
  13635. begin
  13636. StartProgram(false);
  13637. Add([
  13638. 'type',
  13639. ' TObject = class',
  13640. ' function DoIt(k: boolean): longint; virtual;',
  13641. ' end;',
  13642. ' TBird = class',
  13643. ' function DoIt(k: boolean): longint; override;',
  13644. ' end;',
  13645. 'function tobject.doit(k: boolean): longint;',
  13646. 'begin',
  13647. 'end;',
  13648. 'function tbird.doit(k: boolean): longint;',
  13649. ' procedure Sub;',
  13650. ' begin',
  13651. ' inherited DoIt(true);',
  13652. //' if inherited DoIt(false)=4 then ;',
  13653. ' end;',
  13654. 'begin',
  13655. ' Sub;',
  13656. ' inherited;',
  13657. ' inherited DoIt(true);',
  13658. //' if inherited DoIt(false)=14 then ;',
  13659. 'end;',
  13660. 'begin',
  13661. '']);
  13662. ConvertProgram;
  13663. CheckSource('TestClass_NestedProcCallInherited',
  13664. LinesToStr([ // statements
  13665. 'rtl.createClass($mod, "TObject", null, function () {',
  13666. ' this.$init = function () {',
  13667. ' };',
  13668. ' this.$final = function () {',
  13669. ' };',
  13670. ' this.DoIt = function (k) {',
  13671. ' var Result = 0;',
  13672. ' return Result;',
  13673. ' };',
  13674. '});',
  13675. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13676. ' this.DoIt = function (k) {',
  13677. ' var $Self = this;',
  13678. ' var Result = 0;',
  13679. ' function Sub() {',
  13680. ' $mod.TObject.DoIt.call($Self, true);',
  13681. ' };',
  13682. ' Sub();',
  13683. ' $mod.TObject.DoIt.apply($Self, arguments);',
  13684. ' $mod.TObject.DoIt.call($Self, true);',
  13685. ' return Result;',
  13686. ' };',
  13687. '});',
  13688. '']),
  13689. LinesToStr([ // $mod.$main
  13690. '']));
  13691. end;
  13692. procedure TTestModule.TestClass_TObjectFree;
  13693. begin
  13694. StartProgram(false);
  13695. Add([
  13696. 'type',
  13697. ' TObject = class',
  13698. ' Obj: tobject;',
  13699. ' procedure Free;',
  13700. ' procedure Release;',
  13701. ' end;',
  13702. 'procedure tobject.free;',
  13703. 'begin',
  13704. 'end;',
  13705. 'procedure tobject.release;',
  13706. 'begin',
  13707. ' free;',
  13708. ' if true then free;',
  13709. 'end;',
  13710. 'function DoIt(o: tobject): tobject;',
  13711. 'var l: tobject;',
  13712. 'begin',
  13713. ' o.free;',
  13714. ' o.free();',
  13715. ' l.free;',
  13716. ' l.free();',
  13717. ' o.obj.free;',
  13718. ' o.obj.free();',
  13719. ' with o do obj.free;',
  13720. ' with o do obj.free();',
  13721. ' result.Free;',
  13722. ' result.Free();',
  13723. 'end;',
  13724. 'var o: tobject;',
  13725. ' a: array of tobject;',
  13726. 'begin',
  13727. ' o.free;',
  13728. ' o.obj.free;',
  13729. ' a[1+2].free;',
  13730. '']);
  13731. ConvertProgram;
  13732. CheckSource('TestClass_TObjectFree',
  13733. LinesToStr([ // statements
  13734. 'rtl.createClass($mod, "TObject", null, function () {',
  13735. ' this.$init = function () {',
  13736. ' this.Obj = null;',
  13737. ' };',
  13738. ' this.$final = function () {',
  13739. ' this.Obj = undefined;',
  13740. ' };',
  13741. ' this.Free = function () {',
  13742. ' };',
  13743. ' this.Release = function () {',
  13744. ' this.Free();',
  13745. ' if (true) this.Free();',
  13746. ' };',
  13747. '});',
  13748. 'this.DoIt = function (o) {',
  13749. ' var Result = null;',
  13750. ' var l = null;',
  13751. ' o = rtl.freeLoc(o);',
  13752. ' o = rtl.freeLoc(o);',
  13753. ' l = rtl.freeLoc(l);',
  13754. ' l = rtl.freeLoc(l);',
  13755. ' rtl.free(o, "Obj");',
  13756. ' rtl.free(o, "Obj");',
  13757. ' rtl.free(o, "Obj");',
  13758. ' rtl.free(o, "Obj");',
  13759. ' Result = rtl.freeLoc(Result);',
  13760. ' Result = rtl.freeLoc(Result);',
  13761. ' return Result;',
  13762. '};',
  13763. 'this.o = null;',
  13764. 'this.a = [];',
  13765. '']),
  13766. LinesToStr([ // $mod.$main
  13767. 'rtl.free($mod, "o");',
  13768. 'rtl.free($mod.o, "Obj");',
  13769. 'rtl.free($mod.a, 1 + 2);',
  13770. '']));
  13771. end;
  13772. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  13773. begin
  13774. StartProgram(false);
  13775. Add([
  13776. 'type',
  13777. ' TObject = class',
  13778. ' constructor Create;',
  13779. ' procedure Free;',
  13780. ' end;',
  13781. 'constructor TObject.Create; begin end;',
  13782. 'procedure tobject.free; begin end;',
  13783. 'begin',
  13784. ' with tobject.create do free;',
  13785. '']);
  13786. ConvertProgram;
  13787. CheckSource('TestClass_TObjectFreeNewInstance',
  13788. LinesToStr([ // statements
  13789. 'rtl.createClass($mod, "TObject", null, function () {',
  13790. ' this.$init = function () {',
  13791. ' };',
  13792. ' this.$final = function () {',
  13793. ' };',
  13794. ' this.Create = function () {',
  13795. ' return this;',
  13796. ' };',
  13797. ' this.Free = function () {',
  13798. ' };',
  13799. '});',
  13800. '']),
  13801. LinesToStr([ // $mod.$main
  13802. 'var $with1 = $mod.TObject.$create("Create");',
  13803. '$with1=rtl.freeLoc($with1);',
  13804. '']));
  13805. end;
  13806. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  13807. begin
  13808. StartProgram(false);
  13809. Add([
  13810. 'type',
  13811. ' TObject = class',
  13812. ' destructor Destroy;',
  13813. ' procedure Free;',
  13814. ' end;',
  13815. 'destructor TObject.Destroy; begin end;',
  13816. 'procedure tobject.free; begin end;',
  13817. 'var o: tobject;',
  13818. 'begin',
  13819. ' o.free;',
  13820. '']);
  13821. Converter.UseLowerCase:=true;
  13822. ConvertProgram;
  13823. CheckSource('TestClass_TObjectFreeLowerCase',
  13824. LinesToStr([ // statements
  13825. 'rtl.createClass($mod, "tobject", null, function () {',
  13826. ' this.$init = function () {',
  13827. ' };',
  13828. ' this.$final = function () {',
  13829. ' };',
  13830. ' rtl.tObjectDestroy = "destroy";',
  13831. ' this.destroy = function () {',
  13832. ' };',
  13833. ' this.free = function () {',
  13834. ' };',
  13835. '});',
  13836. 'this.o = null;',
  13837. '']),
  13838. LinesToStr([ // $mod.$main
  13839. 'rtl.free($mod, "o");',
  13840. '']));
  13841. end;
  13842. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  13843. begin
  13844. StartProgram(false);
  13845. Add([
  13846. 'type',
  13847. ' TObject = class',
  13848. ' procedure Free;',
  13849. ' function GetObj: tobject; virtual; abstract;',
  13850. ' end;',
  13851. 'procedure tobject.free;',
  13852. 'begin',
  13853. 'end;',
  13854. 'var o: tobject;',
  13855. 'begin',
  13856. ' o.getobj.free;',
  13857. '']);
  13858. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  13859. ConvertProgram;
  13860. end;
  13861. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  13862. begin
  13863. StartProgram(false);
  13864. Add([
  13865. 'type',
  13866. ' TObject = class',
  13867. ' procedure Free;',
  13868. ' FObj: TObject;',
  13869. ' property Obj: tobject read FObj write FObj;',
  13870. ' end;',
  13871. 'procedure tobject.free;',
  13872. 'begin',
  13873. 'end;',
  13874. 'var o: tobject;',
  13875. 'begin',
  13876. ' o.obj.free;',
  13877. '']);
  13878. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  13879. ConvertProgram;
  13880. end;
  13881. procedure TTestModule.TestClass_ForIn;
  13882. begin
  13883. StartProgram(false);
  13884. Add([
  13885. 'type',
  13886. ' TObject = class end;',
  13887. ' TItem = TObject;',
  13888. ' TEnumerator = class',
  13889. ' FCurrent: TItem;',
  13890. ' property Current: TItem read FCurrent;',
  13891. ' function MoveNext: boolean;',
  13892. ' end;',
  13893. ' TBird = class',
  13894. ' function GetEnumerator: TEnumerator;',
  13895. ' end;',
  13896. 'function TEnumerator.MoveNext: boolean;',
  13897. 'begin',
  13898. 'end;',
  13899. 'function TBird.GetEnumerator: TEnumerator;',
  13900. 'begin',
  13901. 'end;',
  13902. 'var',
  13903. ' b: TBird;',
  13904. ' i, i2: TItem;',
  13905. 'begin',
  13906. ' for i in b do i2:=i;']);
  13907. ConvertProgram;
  13908. CheckSource('TestClass_ForIn',
  13909. LinesToStr([ // statements
  13910. 'rtl.createClass($mod, "TObject", null, function () {',
  13911. ' this.$init = function () {',
  13912. ' };',
  13913. ' this.$final = function () {',
  13914. ' };',
  13915. '});',
  13916. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  13917. ' this.$init = function () {',
  13918. ' $mod.TObject.$init.call(this);',
  13919. ' this.FCurrent = null;',
  13920. ' };',
  13921. ' this.$final = function () {',
  13922. ' this.FCurrent = undefined;',
  13923. ' $mod.TObject.$final.call(this);',
  13924. ' };',
  13925. ' this.MoveNext = function () {',
  13926. ' var Result = false;',
  13927. ' return Result;',
  13928. ' };',
  13929. '});',
  13930. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13931. ' this.GetEnumerator = function () {',
  13932. ' var Result = null;',
  13933. ' return Result;',
  13934. ' };',
  13935. '});',
  13936. 'this.b = null;',
  13937. 'this.i = null;',
  13938. 'this.i2 = null;'
  13939. ]),
  13940. LinesToStr([ // $mod.$main
  13941. 'var $in1 = $mod.b.GetEnumerator();',
  13942. 'try {',
  13943. ' while ($in1.MoveNext()){',
  13944. ' $mod.i = $in1.FCurrent;',
  13945. ' $mod.i2 = $mod.i;',
  13946. ' }',
  13947. '} finally {',
  13948. ' $in1 = rtl.freeLoc($in1)',
  13949. '};',
  13950. '']));
  13951. end;
  13952. procedure TTestModule.TestClassOf_Create;
  13953. begin
  13954. StartProgram(false);
  13955. Add('type');
  13956. Add(' TObject = class');
  13957. Add(' constructor Create;');
  13958. Add(' end;');
  13959. Add(' TClass = class of TObject;');
  13960. Add('constructor tobject.create; begin end;');
  13961. Add('var');
  13962. Add(' Obj: tobject;');
  13963. Add(' C: tclass;');
  13964. Add('begin');
  13965. Add(' obj:=C.create;');
  13966. Add(' with c do obj:=create;');
  13967. ConvertProgram;
  13968. CheckSource('TestClassOf_Create',
  13969. LinesToStr([ // statements
  13970. 'rtl.createClass($mod, "TObject", null, function () {',
  13971. ' this.$init = function () {',
  13972. ' };',
  13973. ' this.$final = function () {',
  13974. ' };',
  13975. ' this.Create = function () {',
  13976. ' return this;',
  13977. ' };',
  13978. '});',
  13979. 'this.Obj = null;',
  13980. 'this.C = null;'
  13981. ]),
  13982. LinesToStr([ // $mod.$main
  13983. '$mod.Obj = $mod.C.$create("Create");',
  13984. 'var $with1 = $mod.C;',
  13985. '$mod.Obj = $with1.$create("Create");',
  13986. '']));
  13987. end;
  13988. procedure TTestModule.TestClassOf_Call;
  13989. begin
  13990. StartProgram(false);
  13991. Add('type');
  13992. Add(' TObject = class');
  13993. Add(' class procedure DoIt;');
  13994. Add(' end;');
  13995. Add(' TClass = class of TObject;');
  13996. Add('class procedure tobject.doit; begin end;');
  13997. Add('var');
  13998. Add(' C: tclass;');
  13999. Add('begin');
  14000. Add(' c.doit;');
  14001. Add(' with c do doit;');
  14002. ConvertProgram;
  14003. CheckSource('TestClassOf_Call',
  14004. LinesToStr([ // statements
  14005. 'rtl.createClass($mod, "TObject", null, function () {',
  14006. ' this.$init = function () {',
  14007. ' };',
  14008. ' this.$final = function () {',
  14009. ' };',
  14010. ' this.DoIt = function () {',
  14011. ' };',
  14012. '});',
  14013. 'this.C = null;'
  14014. ]),
  14015. LinesToStr([ // $mod.$main
  14016. '$mod.C.DoIt();',
  14017. 'var $with1 = $mod.C;',
  14018. '$with1.DoIt();',
  14019. '']));
  14020. end;
  14021. procedure TTestModule.TestClassOf_Assign;
  14022. begin
  14023. StartProgram(false);
  14024. Add('type');
  14025. Add(' TClass = class of TObject;');
  14026. Add(' TObject = class');
  14027. Add(' ClassType: TClass; ');
  14028. Add(' end;');
  14029. Add('var');
  14030. Add(' Obj: tobject;');
  14031. Add(' C: tclass;');
  14032. Add('begin');
  14033. Add(' c:=nil;');
  14034. Add(' c:=obj.classtype;');
  14035. ConvertProgram;
  14036. CheckSource('TestClassOf_Assign',
  14037. LinesToStr([ // statements
  14038. 'rtl.createClass($mod, "TObject", null, function () {',
  14039. ' this.$init = function () {',
  14040. ' this.ClassType = null;',
  14041. ' };',
  14042. ' this.$final = function () {',
  14043. ' this.ClassType = undefined;',
  14044. ' };',
  14045. '});',
  14046. 'this.Obj = null;',
  14047. 'this.C = null;'
  14048. ]),
  14049. LinesToStr([ // $mod.$main
  14050. '$mod.C = null;',
  14051. '$mod.C = $mod.Obj.ClassType;',
  14052. '']));
  14053. end;
  14054. procedure TTestModule.TestClassOf_Is;
  14055. begin
  14056. StartProgram(false);
  14057. Add('type');
  14058. Add(' TClass = class of TObject;');
  14059. Add(' TObject = class');
  14060. Add(' end;');
  14061. Add(' TCar = class');
  14062. Add(' end;');
  14063. Add(' TCars = class of TCar;');
  14064. Add('var');
  14065. Add(' Obj: tobject;');
  14066. Add(' C: tclass;');
  14067. Add(' Cars: tcars;');
  14068. Add('begin');
  14069. Add(' if c is tcar then ;');
  14070. Add(' if c is tcars then ;');
  14071. ConvertProgram;
  14072. CheckSource('TestClassOf_Is',
  14073. LinesToStr([ // statements
  14074. 'rtl.createClass($mod, "TObject", null, function () {',
  14075. ' this.$init = function () {',
  14076. ' };',
  14077. ' this.$final = function () {',
  14078. ' };',
  14079. '});',
  14080. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  14081. '});',
  14082. 'this.Obj = null;',
  14083. 'this.C = null;',
  14084. 'this.Cars = null;'
  14085. ]),
  14086. LinesToStr([ // $mod.$main
  14087. 'if(rtl.is($mod.C,$mod.TCar));',
  14088. 'if(rtl.is($mod.C,$mod.TCar));',
  14089. '']));
  14090. end;
  14091. procedure TTestModule.TestClassOf_Compare;
  14092. begin
  14093. StartProgram(false);
  14094. Add('type');
  14095. Add(' TClass = class of TObject;');
  14096. Add(' TObject = class');
  14097. Add(' ClassType: TClass; ');
  14098. Add(' end;');
  14099. Add('var');
  14100. Add(' b: boolean;');
  14101. Add(' Obj: tobject;');
  14102. Add(' C: tclass;');
  14103. Add('begin');
  14104. Add(' b:=c=nil;');
  14105. Add(' b:=nil=c;');
  14106. Add(' b:=c=obj.classtype;');
  14107. Add(' b:=obj.classtype=c;');
  14108. Add(' b:=c=TObject;');
  14109. Add(' b:=TObject=c;');
  14110. Add(' b:=c<>nil;');
  14111. Add(' b:=nil<>c;');
  14112. Add(' b:=c<>obj.classtype;');
  14113. Add(' b:=obj.classtype<>c;');
  14114. Add(' b:=c<>TObject;');
  14115. Add(' b:=TObject<>c;');
  14116. ConvertProgram;
  14117. CheckSource('TestClassOf_Compare',
  14118. LinesToStr([ // statements
  14119. 'rtl.createClass($mod, "TObject", null, function () {',
  14120. ' this.$init = function () {',
  14121. ' this.ClassType = null;',
  14122. ' };',
  14123. ' this.$final = function () {',
  14124. ' this.ClassType = undefined;',
  14125. ' };',
  14126. '});',
  14127. 'this.b = false;',
  14128. 'this.Obj = null;',
  14129. 'this.C = null;'
  14130. ]),
  14131. LinesToStr([ // $mod.$main
  14132. '$mod.b = $mod.C === null;',
  14133. '$mod.b = null === $mod.C;',
  14134. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  14135. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  14136. '$mod.b = $mod.C === $mod.TObject;',
  14137. '$mod.b = $mod.TObject === $mod.C;',
  14138. '$mod.b = $mod.C !== null;',
  14139. '$mod.b = null !== $mod.C;',
  14140. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  14141. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  14142. '$mod.b = $mod.C !== $mod.TObject;',
  14143. '$mod.b = $mod.TObject !== $mod.C;',
  14144. '']));
  14145. end;
  14146. procedure TTestModule.TestClassOf_ClassVar;
  14147. begin
  14148. StartProgram(false);
  14149. Add('type');
  14150. Add(' TObject = class');
  14151. Add(' class var id: longint;');
  14152. Add(' end;');
  14153. Add(' TClass = class of TObject;');
  14154. Add('var');
  14155. Add(' C: tclass;');
  14156. Add('begin');
  14157. Add(' C.id:=C.id;');
  14158. ConvertProgram;
  14159. CheckSource('TestClassOf_ClassVar',
  14160. LinesToStr([ // statements
  14161. 'rtl.createClass($mod, "TObject", null, function () {',
  14162. ' this.id = 0;',
  14163. ' this.$init = function () {',
  14164. ' };',
  14165. ' this.$final = function () {',
  14166. ' };',
  14167. '});',
  14168. 'this.C = null;'
  14169. ]),
  14170. LinesToStr([ // $mod.$main
  14171. '$mod.TObject.id = $mod.C.id;',
  14172. '']));
  14173. end;
  14174. procedure TTestModule.TestClassOf_ClassMethod;
  14175. begin
  14176. StartProgram(false);
  14177. Add('type');
  14178. Add(' TObject = class');
  14179. Add(' class function DoIt(i: longint = 0): longint;');
  14180. Add(' end;');
  14181. Add(' TClass = class of TObject;');
  14182. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  14183. Add('var');
  14184. Add(' i: longint;');
  14185. Add(' C: tclass;');
  14186. Add('begin');
  14187. Add(' C.DoIt;');
  14188. Add(' C.DoIt();');
  14189. Add(' i:=C.DoIt;');
  14190. Add(' i:=C.DoIt();');
  14191. ConvertProgram;
  14192. CheckSource('TestClassOf_ClassMethod',
  14193. LinesToStr([ // statements
  14194. 'rtl.createClass($mod, "TObject", null, function () {',
  14195. ' this.$init = function () {',
  14196. ' };',
  14197. ' this.$final = function () {',
  14198. ' };',
  14199. ' this.DoIt = function (i) {',
  14200. ' var Result = 0;',
  14201. ' return Result;',
  14202. ' };',
  14203. '});',
  14204. 'this.i = 0;',
  14205. 'this.C = null;'
  14206. ]),
  14207. LinesToStr([ // $mod.$main
  14208. '$mod.C.DoIt(0);',
  14209. '$mod.C.DoIt(0);',
  14210. '$mod.i = $mod.C.DoIt(0);',
  14211. '$mod.i = $mod.C.DoIt(0);',
  14212. '']));
  14213. end;
  14214. procedure TTestModule.TestClassOf_ClassProperty;
  14215. begin
  14216. StartProgram(false);
  14217. Add([
  14218. 'type',
  14219. ' TObject = class',
  14220. ' class var FA: longint;',
  14221. ' class function GetA: longint;',
  14222. ' class procedure SetA(Value: longint);',
  14223. ' class property pA: longint read fa write fa;',
  14224. ' class property pB: longint read geta write seta;',
  14225. ' end;',
  14226. ' TObjectClass = class of tobject;',
  14227. 'class function tobject.geta: longint; begin end;',
  14228. 'class procedure tobject.seta(value: longint); begin end;',
  14229. 'var',
  14230. ' b: boolean;',
  14231. ' Obj: tobject;',
  14232. ' Cla: tobjectclass;',
  14233. 'begin',
  14234. ' obj.pa:=obj.pa;',
  14235. ' obj.pb:=obj.pb;',
  14236. ' b:=obj.pa=4;',
  14237. ' b:=obj.pb=obj.pb;',
  14238. ' b:=5=obj.pa;',
  14239. ' cla.pa:=6;',
  14240. ' cla.pa:=cla.pa;',
  14241. ' cla.pb:=cla.pb;',
  14242. ' b:=cla.pa=7;',
  14243. ' b:=cla.pb=cla.pb;',
  14244. ' b:=8=cla.pa;',
  14245. ' tobject.pa:=9;',
  14246. ' tobject.pb:=tobject.pb;',
  14247. ' b:=tobject.pa=10;',
  14248. ' b:=11=tobject.pa;',
  14249. '']);
  14250. ConvertProgram;
  14251. CheckSource('TestClassOf_ClassProperty',
  14252. LinesToStr([ // statements
  14253. 'rtl.createClass($mod, "TObject", null, function () {',
  14254. ' this.FA = 0;',
  14255. ' this.$init = function () {',
  14256. ' };',
  14257. ' this.$final = function () {',
  14258. ' };',
  14259. ' this.GetA = function () {',
  14260. ' var Result = 0;',
  14261. ' return Result;',
  14262. ' };',
  14263. ' this.SetA = function (Value) {',
  14264. ' };',
  14265. '});',
  14266. 'this.b = false;',
  14267. 'this.Obj = null;',
  14268. 'this.Cla = null;'
  14269. ]),
  14270. LinesToStr([ // $mod.$main
  14271. '$mod.TObject.FA = $mod.Obj.FA;',
  14272. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  14273. '$mod.b = $mod.Obj.FA === 4;',
  14274. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  14275. '$mod.b = 5 === $mod.Obj.FA;',
  14276. '$mod.TObject.FA = 6;',
  14277. '$mod.TObject.FA = $mod.Cla.FA;',
  14278. '$mod.Cla.SetA($mod.Cla.GetA());',
  14279. '$mod.b = $mod.Cla.FA === 7;',
  14280. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  14281. '$mod.b = 8 === $mod.Cla.FA;',
  14282. '$mod.TObject.FA = 9;',
  14283. '$mod.TObject.SetA($mod.TObject.GetA());',
  14284. '$mod.b = $mod.TObject.FA === 10;',
  14285. '$mod.b = 11 === $mod.TObject.FA;',
  14286. '']));
  14287. end;
  14288. procedure TTestModule.TestClassOf_ClassMethodSelf;
  14289. begin
  14290. StartProgram(false);
  14291. Add('type');
  14292. Add(' TObject = class');
  14293. Add(' class var GlobalId: longint;');
  14294. Add(' class procedure ProcA;');
  14295. Add(' end;');
  14296. Add('class procedure tobject.proca;');
  14297. Add('var b: boolean;');
  14298. Add('begin');
  14299. Add(' b:=self=nil;');
  14300. Add(' b:=self.globalid=3;');
  14301. Add(' b:=4=self.globalid;');
  14302. Add(' self.globalid:=5;');
  14303. Add(' self.proca;');
  14304. Add('end;');
  14305. Add('begin');
  14306. ConvertProgram;
  14307. CheckSource('TestClassOf_ClassMethodSelf',
  14308. LinesToStr([ // statements
  14309. 'rtl.createClass($mod, "TObject", null, function () {',
  14310. ' this.GlobalId = 0;',
  14311. ' this.$init = function () {',
  14312. ' };',
  14313. ' this.$final = function () {',
  14314. ' };',
  14315. ' this.ProcA = function () {',
  14316. ' var b = false;',
  14317. ' b = this === null;',
  14318. ' b = this.GlobalId === 3;',
  14319. ' b = 4 === this.GlobalId;',
  14320. ' $mod.TObject.GlobalId = 5;',
  14321. ' this.ProcA();',
  14322. ' };',
  14323. '});'
  14324. ]),
  14325. LinesToStr([ // $mod.$main
  14326. '']));
  14327. end;
  14328. procedure TTestModule.TestClassOf_TypeCast;
  14329. begin
  14330. StartProgram(false);
  14331. Add('type');
  14332. Add(' TObject = class');
  14333. Add(' class procedure {#TObject_DoIt}DoIt;');
  14334. Add(' end;');
  14335. Add(' TClass = class of TObject;');
  14336. Add(' TMobile = class');
  14337. Add(' class procedure {#TMobile_DoIt}DoIt;');
  14338. Add(' end;');
  14339. Add(' TMobileClass = class of TMobile;');
  14340. Add(' TCar = class(TMobile)');
  14341. Add(' class procedure {#TCar_DoIt}DoIt;');
  14342. Add(' end;');
  14343. Add(' TCarClass = class of TCar;');
  14344. Add('class procedure TObject.DoIt;');
  14345. Add('begin');
  14346. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14347. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14348. Add('end;');
  14349. Add('class procedure TMobile.DoIt;');
  14350. Add('begin');
  14351. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14352. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14353. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  14354. Add('end;');
  14355. Add('class procedure TCar.DoIt; begin end;');
  14356. Add('var');
  14357. Add(' ObjC: TClass;');
  14358. Add(' MobileC: TMobileClass;');
  14359. Add(' CarC: TCarClass;');
  14360. Add('begin');
  14361. Add(' ObjC.{@TObject_DoIt}DoIt;');
  14362. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  14363. Add(' CarC.{@TCar_DoIt}DoIt;');
  14364. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  14365. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  14366. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  14367. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  14368. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  14369. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  14370. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  14371. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  14372. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  14373. ConvertProgram;
  14374. CheckSource('TestClassOf_TypeCast',
  14375. LinesToStr([ // statements
  14376. 'rtl.createClass($mod, "TObject", null, function () {',
  14377. ' this.$init = function () {',
  14378. ' };',
  14379. ' this.$final = function () {',
  14380. ' };',
  14381. ' this.DoIt = function () {',
  14382. ' this.DoIt();',
  14383. ' this.DoIt$1();',
  14384. ' };',
  14385. '});',
  14386. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  14387. ' this.DoIt$1 = function () {',
  14388. ' this.DoIt();',
  14389. ' this.DoIt$1();',
  14390. ' this.DoIt$2();',
  14391. ' };',
  14392. '});',
  14393. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  14394. ' this.DoIt$2 = function () {',
  14395. ' };',
  14396. '});',
  14397. 'this.ObjC = null;',
  14398. 'this.MobileC = null;',
  14399. 'this.CarC = null;',
  14400. '']),
  14401. LinesToStr([ // $mod.$main
  14402. '$mod.ObjC.DoIt();',
  14403. '$mod.MobileC.DoIt$1();',
  14404. '$mod.CarC.DoIt$2();',
  14405. '$mod.ObjC.DoIt();',
  14406. '$mod.ObjC.DoIt$1();',
  14407. '$mod.ObjC.DoIt$2();',
  14408. '$mod.MobileC.DoIt();',
  14409. '$mod.MobileC.DoIt$1();',
  14410. '$mod.MobileC.DoIt$2();',
  14411. '$mod.CarC.DoIt();',
  14412. '$mod.CarC.DoIt$1();',
  14413. '$mod.CarC.DoIt$2();',
  14414. '']));
  14415. end;
  14416. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  14417. begin
  14418. StartProgram(false);
  14419. Add('type');
  14420. Add(' TObject = class');
  14421. Add(' function CurNow: longint; ');
  14422. Add(' class function Now: longint; ');
  14423. Add(' end;');
  14424. Add('function TObject.CurNow: longint; begin end;');
  14425. Add('class function TObject.Now: longint; begin end;');
  14426. Add('var');
  14427. Add(' Obj: tobject;');
  14428. Add(' vI: longint;');
  14429. Add('begin');
  14430. Add(' obj.curnow;');
  14431. Add(' vi:=obj.curnow;');
  14432. Add(' tobject.now;');
  14433. Add(' vi:=tobject.now;');
  14434. ConvertProgram;
  14435. CheckSource('TestClassOf_ImplicitFunctionCall',
  14436. LinesToStr([ // statements
  14437. 'rtl.createClass($mod, "TObject", null, function () {',
  14438. ' this.$init = function () {',
  14439. ' };',
  14440. ' this.$final = function () {',
  14441. ' };',
  14442. ' this.CurNow = function () {',
  14443. ' var Result = 0;',
  14444. ' return Result;',
  14445. ' };',
  14446. ' this.Now = function () {',
  14447. ' var Result = 0;',
  14448. ' return Result;',
  14449. ' };',
  14450. '});',
  14451. 'this.Obj = null;',
  14452. 'this.vI = 0;',
  14453. '']),
  14454. LinesToStr([ // $mod.$main
  14455. '$mod.Obj.CurNow();',
  14456. '$mod.vI = $mod.Obj.CurNow();',
  14457. '$mod.TObject.Now();',
  14458. '$mod.vI = $mod.TObject.Now();',
  14459. '']));
  14460. end;
  14461. procedure TTestModule.TestClassOf_Const;
  14462. begin
  14463. StartProgram(false);
  14464. Add([
  14465. 'type',
  14466. ' TObject = class',
  14467. ' end;',
  14468. ' TBird = TObject;',
  14469. ' TBirds = class of TBird;',
  14470. ' TEagles = TBirds;',
  14471. ' THawk = class(TBird);',
  14472. 'const',
  14473. ' Hawk: TEagles = THawk;',
  14474. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  14475. ' TBird,',
  14476. ' THawk',
  14477. ' );',
  14478. 'begin']);
  14479. ConvertProgram;
  14480. CheckSource('TestClassOf_Const',
  14481. LinesToStr([ // statements
  14482. 'rtl.createClass($mod, "TObject", null, function () {',
  14483. ' this.$init = function () {',
  14484. ' };',
  14485. ' this.$final = function () {',
  14486. ' };',
  14487. '});',
  14488. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  14489. '});',
  14490. 'this.Hawk = $mod.THawk;',
  14491. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  14492. '']),
  14493. LinesToStr([ // $mod.$main
  14494. '']));
  14495. end;
  14496. procedure TTestModule.TestNestedClass_Alias;
  14497. begin
  14498. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14499. StartProgram(false);
  14500. Add([
  14501. 'type',
  14502. ' TObject = class',
  14503. ' type TNested = type longint;',
  14504. ' end;',
  14505. 'type TAlias = type tobject.tnested;',
  14506. 'var i: tobject.tnested = 3;',
  14507. 'var j: TAlias = 4;',
  14508. 'begin',
  14509. ' if typeinfo(TAlias)=nil then ;',
  14510. ' if typeinfo(tobject.tnested)=nil then ;',
  14511. '']);
  14512. ConvertProgram;
  14513. CheckSource('TestNestedClass_Alias',
  14514. LinesToStr([ // statements
  14515. 'rtl.createClass($mod, "TObject", null, function () {',
  14516. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  14517. ' this.$init = function () {',
  14518. ' };',
  14519. ' this.$final = function () {',
  14520. ' };',
  14521. '});',
  14522. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  14523. 'this.i = 3;',
  14524. 'this.j = 4;',
  14525. '']),
  14526. LinesToStr([ // $mod.$main
  14527. 'if ($mod.$rtti["TAlias"] === null) ;',
  14528. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  14529. '']));
  14530. end;
  14531. procedure TTestModule.TestNestedClass_Record;
  14532. begin
  14533. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14534. StartProgram(false);
  14535. Add([
  14536. 'type',
  14537. ' TObject = class',
  14538. ' type TPoint = record',
  14539. ' x,y: byte;',
  14540. ' end;',
  14541. ' procedure DoIt(t: TPoint);',
  14542. ' end;',
  14543. 'procedure tobject.DoIt(t: TPoint);',
  14544. 'var p: TPoint;',
  14545. 'begin',
  14546. ' t.x:=t.y;',
  14547. ' p:=t;',
  14548. 'end;',
  14549. 'var',
  14550. ' p: tobject.tpoint = (x:2; y:4);',
  14551. ' o: TObject;',
  14552. 'begin',
  14553. ' p:=p;',
  14554. ' o.doit(p);',
  14555. '']);
  14556. ConvertProgram;
  14557. CheckSource('TestNestedClass_Record',
  14558. LinesToStr([ // statements
  14559. 'rtl.createClass($mod, "TObject", null, function () {',
  14560. ' rtl.recNewT(this, "TPoint", function () {',
  14561. ' this.x = 0;',
  14562. ' this.y = 0;',
  14563. ' this.$eq = function (b) {',
  14564. ' return (this.x === b.x) && (this.y === b.y);',
  14565. ' };',
  14566. ' this.$assign = function (s) {',
  14567. ' this.x = s.x;',
  14568. ' this.y = s.y;',
  14569. ' return this;',
  14570. ' };',
  14571. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  14572. ' $r.addField("x", rtl.byte);',
  14573. ' $r.addField("y", rtl.byte);',
  14574. ' });',
  14575. ' this.$init = function () {',
  14576. ' };',
  14577. ' this.$final = function () {',
  14578. ' };',
  14579. ' this.DoIt = function (t) {',
  14580. ' var p = this.TPoint.$new();',
  14581. ' t.x = t.y;',
  14582. ' p.$assign(t);',
  14583. ' };',
  14584. '});',
  14585. 'this.p = $mod.TObject.TPoint.$clone({',
  14586. ' x: 2,',
  14587. ' y: 4',
  14588. '});',
  14589. 'this.o = null;',
  14590. '']),
  14591. LinesToStr([ // $mod.$main
  14592. '$mod.p.$assign($mod.p);',
  14593. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  14594. '']));
  14595. end;
  14596. procedure TTestModule.TestNestedClass_Class;
  14597. begin
  14598. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14599. StartProgram(false);
  14600. Add([
  14601. 'type',
  14602. ' TObject = class end;',
  14603. ' TBird = class',
  14604. ' type TLeg = class',
  14605. ' FId: longint;',
  14606. ' constructor Create;',
  14607. ' function Create(i: longint): TLeg;',
  14608. ' end;',
  14609. ' function DoIt(b: TBird): Tleg;',
  14610. ' end;',
  14611. 'constructor tbird.tleg.create;',
  14612. 'begin',
  14613. ' FId:=3;',
  14614. 'end;',
  14615. 'function tbird.tleg.Create(i: longint): TLeg;',
  14616. 'begin',
  14617. ' Create;',
  14618. ' Result:=TLeg.Create;',
  14619. ' Result:=TBird.TLeg.Create;',
  14620. ' Result:=Create(3);',
  14621. ' FId:=i;',
  14622. 'end;',
  14623. 'function tbird.DoIt(b: tbird): tleg;',
  14624. 'begin',
  14625. ' Result.Create;',
  14626. ' Result:=TLeg.Create;',
  14627. ' Result:=TBird.TLeg.Create;',
  14628. ' Result:=Result.Create(3);',
  14629. 'end;',
  14630. 'var',
  14631. ' b: Tbird.tleg;',
  14632. 'begin',
  14633. ' b.Create;',
  14634. ' b:=TBird.TLeg.Create;',
  14635. ' b:=b.Create(3);',
  14636. '']);
  14637. ConvertProgram;
  14638. CheckSource('TestNestedClass_Class',
  14639. LinesToStr([ // statements
  14640. 'rtl.createClass($mod, "TObject", null, function () {',
  14641. ' this.$init = function () {',
  14642. ' };',
  14643. ' this.$final = function () {',
  14644. ' };',
  14645. '});',
  14646. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14647. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  14648. ' this.$init = function () {',
  14649. ' $mod.TObject.$init.call(this);',
  14650. ' this.FId = 0;',
  14651. ' };',
  14652. ' this.Create = function () {',
  14653. ' this.FId = 3;',
  14654. ' return this;',
  14655. ' };',
  14656. ' this.Create$1 = function (i) {',
  14657. ' var Result = null;',
  14658. ' this.Create();',
  14659. ' Result = $mod.TBird.TLeg.$create("Create");',
  14660. ' Result = $mod.TBird.TLeg.$create("Create");',
  14661. ' Result = this.Create$1(3);',
  14662. ' this.FId = i;',
  14663. ' return Result;',
  14664. ' };',
  14665. ' });',
  14666. ' this.DoIt = function (b) {',
  14667. ' var Result = null;',
  14668. ' Result.Create();',
  14669. ' Result = this.TLeg.$create("Create");',
  14670. ' Result = $mod.TBird.TLeg.$create("Create");',
  14671. ' Result = Result.Create$1(3);',
  14672. ' return Result;',
  14673. ' };',
  14674. '});',
  14675. 'this.b = null;',
  14676. '']),
  14677. LinesToStr([ // $mod.$main
  14678. '$mod.b.Create();',
  14679. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  14680. '$mod.b = $mod.b.Create$1(3);',
  14681. '']));
  14682. end;
  14683. procedure TTestModule.TestExternalClass_Var;
  14684. begin
  14685. StartProgram(false);
  14686. Add([
  14687. '{$modeswitch externalclass}',
  14688. 'type',
  14689. ' TExtA = class external name ''ExtObj''',
  14690. ' Id: longint external name ''$Id'';',
  14691. ' B: longint;',
  14692. ' end;',
  14693. 'var Obj: TExtA;',
  14694. 'begin',
  14695. ' obj.id:=obj.id+1;',
  14696. ' obj.B:=obj.B+1;']);
  14697. ConvertProgram;
  14698. CheckSource('TestExternalClass_Var',
  14699. LinesToStr([ // statements
  14700. 'this.Obj = null;',
  14701. '']),
  14702. LinesToStr([ // $mod.$main
  14703. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  14704. '$mod.Obj.B = $mod.Obj.B + 1;',
  14705. '']));
  14706. end;
  14707. procedure TTestModule.TestExternalClass_Const;
  14708. begin
  14709. StartProgram(false);
  14710. Add([
  14711. '{$modeswitch externalclass}',
  14712. 'type',
  14713. ' TExtA = class external name ''ExtObj''',
  14714. ' const Two: longint = 2;',
  14715. ' const Three = 3;',
  14716. ' const Id: longint;',
  14717. ' end;',
  14718. ' TExtB = class external name ''ExtB''',
  14719. ' A: TExtA;',
  14720. ' end;',
  14721. 'var',
  14722. ' A: texta;',
  14723. ' B: textb;',
  14724. ' i: longint;',
  14725. 'begin',
  14726. ' i:=a.two;',
  14727. ' i:=texta.two;',
  14728. ' i:=a.three;',
  14729. ' i:=texta.three;',
  14730. ' i:=a.id;',
  14731. ' i:=texta.id;',
  14732. '']);
  14733. ConvertProgram;
  14734. CheckSource('TestExternalClass_Const',
  14735. LinesToStr([ // statements
  14736. 'this.A = null;',
  14737. 'this.B = null;',
  14738. 'this.i = 0;',
  14739. '']),
  14740. LinesToStr([ // $mod.$main
  14741. '$mod.i = 2;',
  14742. '$mod.i = 2;',
  14743. '$mod.i = 3;',
  14744. '$mod.i = 3;',
  14745. '$mod.i = $mod.A.Id;',
  14746. '$mod.i = ExtObj.Id;',
  14747. '']));
  14748. end;
  14749. procedure TTestModule.TestExternalClass_Dollar;
  14750. begin
  14751. StartProgram(false);
  14752. Add([
  14753. '{$modeswitch externalclass}',
  14754. 'type',
  14755. ' TExtA = class external name ''$''',
  14756. ' Id: longint external name ''$'';',
  14757. ' function Bla(i: longint): longint; external name ''$'';',
  14758. ' end;',
  14759. 'function dollar(k: longint): longint; external name ''$'';',
  14760. 'var Obj: TExtA;',
  14761. 'begin',
  14762. ' dollar(1);',
  14763. ' obj.id:=obj.id+2;',
  14764. ' obj.Bla(3);',
  14765. '']);
  14766. ConvertProgram;
  14767. CheckSource('TestExternalClass_Dollar',
  14768. LinesToStr([ // statements
  14769. 'this.Obj = null;',
  14770. '']),
  14771. LinesToStr([ // $mod.$main
  14772. '$(1);',
  14773. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  14774. '$mod.Obj.$(3);',
  14775. '']));
  14776. end;
  14777. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  14778. begin
  14779. StartProgram(false);
  14780. Add('{$modeswitch externalclass}');
  14781. Add('type');
  14782. Add(' TExtA = class external name ''ExtA''');
  14783. Add(' Id: longint external name ''$Id'';');
  14784. Add(' end;');
  14785. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  14786. Add(' Id: longint;');
  14787. Add(' end;');
  14788. Add('begin');
  14789. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  14790. ConvertProgram;
  14791. end;
  14792. procedure TTestModule.TestExternalClass_Method;
  14793. begin
  14794. StartProgram(false);
  14795. Add('{$modeswitch externalclass}');
  14796. Add('type');
  14797. Add(' TExtA = class external name ''ExtObj''');
  14798. Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
  14799. Add(' procedure DoSome(Id: longint = 1);');
  14800. Add(' end;');
  14801. Add('var Obj: texta;');
  14802. Add('begin');
  14803. Add(' obj.doit;');
  14804. Add(' obj.doit();');
  14805. Add(' obj.doit(2);');
  14806. Add(' with obj do begin');
  14807. Add(' doit;');
  14808. Add(' doit();');
  14809. Add(' doit(3);');
  14810. Add(' end;');
  14811. ConvertProgram;
  14812. CheckSource('TestExternalClass_Method',
  14813. LinesToStr([ // statements
  14814. 'this.Obj = null;',
  14815. '']),
  14816. LinesToStr([ // $mod.$main
  14817. '$mod.Obj.$Execute(1);',
  14818. '$mod.Obj.$Execute(1);',
  14819. '$mod.Obj.$Execute(2);',
  14820. 'var $with1 = $mod.Obj;',
  14821. '$with1.$Execute(1);',
  14822. '$with1.$Execute(1);',
  14823. '$with1.$Execute(3);',
  14824. '']));
  14825. end;
  14826. procedure TTestModule.TestExternalClass_ClassMethod;
  14827. begin
  14828. StartProgram(false);
  14829. Add([
  14830. '{$modeswitch externalclass}',
  14831. 'type',
  14832. ' TExtA = class external name ''ExtObj''',
  14833. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  14834. ' end;',
  14835. ' TExtB = TExtA;',
  14836. 'begin',
  14837. ' texta.doit;',
  14838. ' texta.doit();',
  14839. ' texta.doit(2);',
  14840. ' with texta do begin',
  14841. ' doit;',
  14842. ' doit();',
  14843. ' doit(3);',
  14844. ' end;',
  14845. ' textb.doit;',
  14846. ' textb.doit();',
  14847. ' textb.doit(4);',
  14848. ' with textb do begin',
  14849. ' doit;',
  14850. ' doit();',
  14851. ' doit(5);',
  14852. ' end;',
  14853. '']);
  14854. ConvertProgram;
  14855. CheckSource('TestExternalClass_ClassMethod',
  14856. LinesToStr([ // statements
  14857. '']),
  14858. LinesToStr([ // $mod.$main
  14859. 'ExtObj.$Execute(1);',
  14860. 'ExtObj.$Execute(1);',
  14861. 'ExtObj.$Execute(2);',
  14862. 'ExtObj.$Execute(1);',
  14863. 'ExtObj.$Execute(1);',
  14864. 'ExtObj.$Execute(3);',
  14865. 'ExtObj.$Execute(1);',
  14866. 'ExtObj.$Execute(1);',
  14867. 'ExtObj.$Execute(4);',
  14868. 'ExtObj.$Execute(1);',
  14869. 'ExtObj.$Execute(1);',
  14870. 'ExtObj.$Execute(5);',
  14871. '']));
  14872. end;
  14873. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  14874. begin
  14875. StartProgram(false);
  14876. Add([
  14877. '{$modeswitch externalclass}',
  14878. 'type',
  14879. ' TBird = class external name ''Array''',
  14880. ' end;',
  14881. 'function GetPtr: Pointer;',
  14882. 'begin',
  14883. 'end;',
  14884. 'procedure Write(const p);',
  14885. 'begin',
  14886. 'end;',
  14887. 'procedure WriteLn; varargs;',
  14888. 'begin',
  14889. 'end;',
  14890. 'begin',
  14891. ' if TBird(GetPtr)=nil then ;',
  14892. ' Write(GetPtr);',
  14893. ' WriteLn(GetPtr);',
  14894. ' Write(TBird(GetPtr));',
  14895. ' WriteLn(TBird(GetPtr));',
  14896. '']);
  14897. ConvertProgram;
  14898. CheckSource('TestFunctionResultInTypeCast',
  14899. LinesToStr([ // statements
  14900. 'this.GetPtr = function () {',
  14901. ' var Result = null;',
  14902. ' return Result;',
  14903. '};',
  14904. 'this.Write = function (p) {',
  14905. '};',
  14906. 'this.WriteLn = function () {',
  14907. '};',
  14908. '']),
  14909. LinesToStr([
  14910. 'if ($mod.GetPtr() === null) ;',
  14911. '$mod.Write($mod.GetPtr());',
  14912. '$mod.WriteLn($mod.GetPtr());',
  14913. '$mod.Write($mod.GetPtr());',
  14914. '$mod.WriteLn($mod.GetPtr());',
  14915. '']));
  14916. end;
  14917. procedure TTestModule.TestExternalClass_NonExternalOverride;
  14918. begin
  14919. StartProgram(false);
  14920. Add('{$modeswitch externalclass}');
  14921. Add('type');
  14922. Add(' TExtA = class external name ''ExtObjA''');
  14923. Add(' procedure ProcA; virtual;');
  14924. Add(' procedure ProcB; virtual;');
  14925. Add(' end;');
  14926. Add(' TExtB = class external name ''ExtObjB'' (TExtA)');
  14927. Add(' end;');
  14928. Add(' TExtC = class (TExtB)');
  14929. Add(' procedure ProcA; override;');
  14930. Add(' end;');
  14931. Add('procedure TExtC.ProcA;');
  14932. Add('begin');
  14933. Add(' ProcA;');
  14934. Add(' Self.ProcA;');
  14935. Add(' ProcB;');
  14936. Add(' Self.ProcB;');
  14937. Add('end;');
  14938. Add('var');
  14939. Add(' A: texta;');
  14940. Add(' B: textb;');
  14941. Add(' C: textc;');
  14942. Add('begin');
  14943. Add(' a.proca;');
  14944. Add(' b.proca;');
  14945. Add(' c.proca;');
  14946. ConvertProgram;
  14947. CheckSource('TestExternalClass_NonExternalOverride',
  14948. LinesToStr([ // statements
  14949. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  14950. ' this.$init = function () {',
  14951. ' };',
  14952. ' this.$final = function () {',
  14953. ' };',
  14954. ' this.ProcA = function () {',
  14955. ' this.ProcA();',
  14956. ' this.ProcA();',
  14957. ' this.ProcB();',
  14958. ' this.ProcB();',
  14959. ' };',
  14960. '});',
  14961. 'this.A = null;',
  14962. 'this.B = null;',
  14963. 'this.C = null;',
  14964. '']),
  14965. LinesToStr([ // $mod.$main
  14966. '$mod.A.ProcA();',
  14967. '$mod.B.ProcA();',
  14968. '$mod.C.ProcA();',
  14969. '']));
  14970. end;
  14971. procedure TTestModule.TestExternalClass_OverloadHint;
  14972. begin
  14973. StartProgram(false);
  14974. Add([
  14975. '{$modeswitch externalclass}',
  14976. 'type',
  14977. ' TExtA = class external name ''ExtObjA''',
  14978. ' procedure DoIt;',
  14979. ' procedure DoIt(i: longint);',
  14980. ' end;',
  14981. 'begin',
  14982. '']);
  14983. ConvertProgram;
  14984. CheckResolverUnexpectedHints(true);
  14985. CheckSource('TestExternalClass_OverloadHint',
  14986. LinesToStr([ // statements
  14987. '']),
  14988. LinesToStr([ // $mod.$main
  14989. '']));
  14990. end;
  14991. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  14992. begin
  14993. StartProgram(false);
  14994. Add([
  14995. '{$modeswitch externalclass}',
  14996. 'type',
  14997. ' JSwiper = class external name ''Swiper''',
  14998. ' constructor New;',
  14999. ' end;',
  15000. ' TObject = class',
  15001. ' private',
  15002. ' FSwiper: JSwiper;',
  15003. ' published',
  15004. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  15005. ' end;',
  15006. 'begin',
  15007. ' JSwiper.new;',
  15008. '']);
  15009. ConvertProgram;
  15010. CheckSource('TestExternalClass_SameNamePublishedProperty',
  15011. LinesToStr([ // statements
  15012. 'rtl.createClass($mod, "TObject", null, function () {',
  15013. ' this.$init = function () {',
  15014. ' this.FSwiper = null;',
  15015. ' };',
  15016. ' this.$final = function () {',
  15017. ' this.FSwiper = undefined;',
  15018. ' };',
  15019. ' var $r = this.$rtti;',
  15020. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  15021. '});',
  15022. '']),
  15023. LinesToStr([ // $mod.$main
  15024. 'new Swiper();',
  15025. '']));
  15026. end;
  15027. procedure TTestModule.TestExternalClass_Property;
  15028. begin
  15029. StartProgram(false);
  15030. Add([
  15031. '{$modeswitch externalclass}',
  15032. 'type',
  15033. ' TExtA = class external name ''ExtA''',
  15034. ' function getYear: longint;',
  15035. ' procedure setYear(Value: longint);',
  15036. ' property Year: longint read getyear write setyear;',
  15037. ' end;',
  15038. ' TExtB = class (TExtA)',
  15039. ' procedure OtherSetYear(Value: longint);',
  15040. ' property year write othersetyear;',
  15041. ' end;',
  15042. 'procedure textb.othersetyear(value: longint);',
  15043. 'begin',
  15044. ' setYear(Value+4);',
  15045. 'end;',
  15046. 'var',
  15047. ' A: texta;',
  15048. ' B: textb;',
  15049. 'begin',
  15050. ' a.year:=a.year+1;',
  15051. ' b.year:=b.year+2;']);
  15052. ConvertProgram;
  15053. CheckSource('TestExternalClass_NonExternalOverride',
  15054. LinesToStr([ // statements
  15055. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15056. ' this.$init = function () {',
  15057. ' };',
  15058. ' this.$final = function () {',
  15059. ' };',
  15060. ' this.OtherSetYear = function (Value) {',
  15061. ' this.setYear(Value+4);',
  15062. ' };',
  15063. '});',
  15064. 'this.A = null;',
  15065. 'this.B = null;',
  15066. '']),
  15067. LinesToStr([ // $mod.$main
  15068. '$mod.A.setYear($mod.A.getYear()+1);',
  15069. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  15070. '']));
  15071. end;
  15072. procedure TTestModule.TestExternalClass_PropertyDate;
  15073. begin
  15074. StartProgram(false);
  15075. Add([
  15076. '{$modeswitch externalclass}',
  15077. 'type',
  15078. ' TExtA = class external name ''ExtA''',
  15079. ' end;',
  15080. ' TExtB = class (TExtA)',
  15081. ' FDate: string;',
  15082. ' property Date: string read FDate write FDate;',
  15083. ' property ExtA: string read FDate write FDate;',
  15084. ' end;',
  15085. ' {$M+}',
  15086. ' TObject = class',
  15087. ' FDate: string;',
  15088. ' published',
  15089. ' property Date: string read FDate write FDate;',
  15090. ' property ExtA: string read FDate write FDate;',
  15091. ' end;',
  15092. 'var',
  15093. ' B: textb;',
  15094. ' o: TObject;',
  15095. 'begin',
  15096. ' b.date:=b.exta;',
  15097. ' o.date:=o.exta;']);
  15098. ConvertProgram;
  15099. CheckSource('TestExternalClass_PropertyDate',
  15100. LinesToStr([ // statements
  15101. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15102. ' this.$init = function () {',
  15103. ' this.FDate = "";',
  15104. ' };',
  15105. ' this.$final = function () {',
  15106. ' };',
  15107. '});',
  15108. 'rtl.createClass($mod, "TObject", null, function () {',
  15109. ' this.$init = function () {',
  15110. ' this.FDate = "";',
  15111. ' };',
  15112. ' this.$final = function () {',
  15113. ' };',
  15114. ' var $r = this.$rtti;',
  15115. ' $r.addField("FDate", rtl.string);',
  15116. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  15117. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  15118. '});',
  15119. 'this.B = null;',
  15120. 'this.o = null;',
  15121. '']),
  15122. LinesToStr([ // $mod.$main
  15123. '$mod.B.FDate = $mod.B.FDate;',
  15124. '$mod.o.FDate = $mod.o.FDate;',
  15125. '']));
  15126. end;
  15127. procedure TTestModule.TestExternalClass_ClassProperty;
  15128. begin
  15129. StartProgram(false);
  15130. Add('{$modeswitch externalclass}');
  15131. Add('type');
  15132. Add(' TExtA = class external name ''ExtA''');
  15133. Add(' class function getYear: longint;');
  15134. Add(' class procedure setYear(Value: longint);');
  15135. Add(' class property Year: longint read getyear write setyear;');
  15136. Add(' end;');
  15137. Add(' TExtB = class (TExtA)');
  15138. Add(' class function GetCentury: longint;');
  15139. Add(' class procedure SetCentury(Value: longint);');
  15140. Add(' class property Century: longint read getcentury write setcentury;');
  15141. Add(' end;');
  15142. Add('class function textb.getcentury: longint;');
  15143. Add('begin');
  15144. Add('end;');
  15145. Add('class procedure textb.setcentury(value: longint);');
  15146. Add('begin');
  15147. Add(' setyear(value+11);');
  15148. Add(' texta.year:=texta.year+12;');
  15149. Add(' year:=year+13;');
  15150. Add(' textb.century:=textb.century+14;');
  15151. Add(' century:=century+15;');
  15152. Add('end;');
  15153. Add('var');
  15154. Add(' A: texta;');
  15155. Add(' B: textb;');
  15156. Add('begin');
  15157. Add(' texta.year:=texta.year+1;');
  15158. Add(' textb.year:=textb.year+2;');
  15159. Add(' TextA.year:=TextA.year+3;');
  15160. Add(' b.year:=b.year+4;');
  15161. Add(' textb.century:=textb.century+5;');
  15162. Add(' b.century:=b.century+6;');
  15163. ConvertProgram;
  15164. CheckSource('TestExternalClass_ClassProperty',
  15165. LinesToStr([ // statements
  15166. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15167. ' this.$init = function () {',
  15168. ' };',
  15169. ' this.$final = function () {',
  15170. ' };',
  15171. ' this.GetCentury = function () {',
  15172. ' var Result = 0;',
  15173. ' return Result;',
  15174. ' };',
  15175. ' this.SetCentury = function (Value) {',
  15176. ' this.setYear(Value + 11);',
  15177. ' ExtA.setYear(ExtA.getYear() + 12);',
  15178. ' this.setYear(this.getYear() + 13);',
  15179. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  15180. ' this.SetCentury(this.GetCentury() + 15);',
  15181. ' };',
  15182. '});',
  15183. 'this.A = null;',
  15184. 'this.B = null;',
  15185. '']),
  15186. LinesToStr([ // $mod.$main
  15187. 'ExtA.setYear(ExtA.getYear() + 1);',
  15188. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  15189. 'ExtA.setYear(ExtA.getYear() + 3);',
  15190. '$mod.B.setYear($mod.B.getYear() + 4);',
  15191. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  15192. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  15193. '']));
  15194. end;
  15195. procedure TTestModule.TestExternalClass_ClassOf;
  15196. begin
  15197. StartProgram(false);
  15198. Add('{$modeswitch externalclass}');
  15199. Add('type');
  15200. Add(' TExtA = class external name ''ExtA''');
  15201. Add(' procedure ProcA; virtual;');
  15202. Add(' procedure ProcB; virtual;');
  15203. Add(' end;');
  15204. Add(' TExtAClass = class of TExtA;');
  15205. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15206. Add(' end;');
  15207. Add(' TExtBClass = class of TExtB;');
  15208. Add(' TExtC = class (TExtB)');
  15209. Add(' procedure ProcA; override;');
  15210. Add(' end;');
  15211. Add(' TExtCClass = class of TExtC;');
  15212. Add('procedure TExtC.ProcA; begin end;');
  15213. Add('var');
  15214. Add(' A: texta; ClA: TExtAClass;');
  15215. Add(' B: textb; ClB: TExtBClass;');
  15216. Add(' C: textc; ClC: TExtCClass;');
  15217. Add('begin');
  15218. Add(' ClA:=texta;');
  15219. Add(' ClA:=textb;');
  15220. Add(' ClA:=textc;');
  15221. Add(' ClB:=textb;');
  15222. Add(' ClB:=textc;');
  15223. Add(' ClC:=textc;');
  15224. ConvertProgram;
  15225. CheckSource('TestExternalClass_ClassOf',
  15226. LinesToStr([ // statements
  15227. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15228. ' this.$init = function () {',
  15229. ' };',
  15230. ' this.$final = function () {',
  15231. ' };',
  15232. ' this.ProcA = function () {',
  15233. ' };',
  15234. '});',
  15235. 'this.A = null;',
  15236. 'this.ClA = null;',
  15237. 'this.B = null;',
  15238. 'this.ClB = null;',
  15239. 'this.C = null;',
  15240. 'this.ClC = null;',
  15241. '']),
  15242. LinesToStr([ // $mod.$main
  15243. '$mod.ClA = ExtA;',
  15244. '$mod.ClA = ExtB;',
  15245. '$mod.ClA = $mod.TExtC;',
  15246. '$mod.ClB = ExtB;',
  15247. '$mod.ClB = $mod.TExtC;',
  15248. '$mod.ClC = $mod.TExtC;',
  15249. '']));
  15250. end;
  15251. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  15252. begin
  15253. AddModuleWithIntfImplSrc('unit2.pas',
  15254. LinesToStr([
  15255. '{$modeswitch externalclass}',
  15256. 'type',
  15257. ' TExtA = class external name ''ExtA''',
  15258. ' class var Id: longint;',
  15259. ' end;',
  15260. '']),
  15261. '');
  15262. StartUnit(true);
  15263. Add('interface');
  15264. Add('uses unit2;');
  15265. Add('implementation');
  15266. Add('begin');
  15267. Add(' unit2.texta.id:=unit2.texta.id+1;');
  15268. ConvertUnit;
  15269. CheckSource('TestExternalClass_ClassOtherUnit',
  15270. LinesToStr([
  15271. '']),
  15272. LinesToStr([
  15273. 'ExtA.Id = ExtA.Id + 1;',
  15274. '']));
  15275. end;
  15276. procedure TTestModule.TestExternalClass_Is;
  15277. begin
  15278. StartProgram(false);
  15279. Add('{$modeswitch externalclass}');
  15280. Add('type');
  15281. Add(' TExtA = class external name ''ExtA''');
  15282. Add(' end;');
  15283. Add(' TExtAClass = class of TExtA;');
  15284. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15285. Add(' end;');
  15286. Add(' TExtBClass = class of TExtB;');
  15287. Add(' TExtC = class (TExtB)');
  15288. Add(' end;');
  15289. Add(' TExtCClass = class of TExtC;');
  15290. Add('var');
  15291. Add(' A: texta; ClA: TExtAClass;');
  15292. Add(' B: textb; ClB: TExtBClass;');
  15293. Add(' C: textc; ClC: TExtCClass;');
  15294. Add('begin');
  15295. Add(' if a is textb then ;');
  15296. Add(' if a is textc then ;');
  15297. Add(' if b is textc then ;');
  15298. Add(' if cla is textb then ;');
  15299. Add(' if cla is textc then ;');
  15300. Add(' if clb is textc then ;');
  15301. ConvertProgram;
  15302. CheckSource('TestExternalClass_Is',
  15303. LinesToStr([ // statements
  15304. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15305. ' this.$init = function () {',
  15306. ' };',
  15307. ' this.$final = function () {',
  15308. ' };',
  15309. '});',
  15310. 'this.A = null;',
  15311. 'this.ClA = null;',
  15312. 'this.B = null;',
  15313. 'this.ClB = null;',
  15314. 'this.C = null;',
  15315. 'this.ClC = null;',
  15316. '']),
  15317. LinesToStr([ // $mod.$main
  15318. 'if (rtl.isExt($mod.A, ExtB)) ;',
  15319. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  15320. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  15321. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  15322. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  15323. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  15324. '']));
  15325. end;
  15326. procedure TTestModule.TestExternalClass_As;
  15327. begin
  15328. StartProgram(false);
  15329. Add('{$modeswitch externalclass}');
  15330. Add('type');
  15331. Add(' TExtA = class external name ''ExtA''');
  15332. Add(' end;');
  15333. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15334. Add(' end;');
  15335. Add(' TExtC = class (TExtB)');
  15336. Add(' end;');
  15337. Add('var');
  15338. Add(' A: texta;');
  15339. Add(' B: textb;');
  15340. Add(' C: textc;');
  15341. Add('begin');
  15342. Add(' b:=a as textb;');
  15343. Add(' c:=a as textc;');
  15344. Add(' c:=b as textc;');
  15345. ConvertProgram;
  15346. CheckSource('TestExternalClass_Is',
  15347. LinesToStr([ // statements
  15348. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15349. ' this.$init = function () {',
  15350. ' };',
  15351. ' this.$final = function () {',
  15352. ' };',
  15353. '});',
  15354. 'this.A = null;',
  15355. 'this.B = null;',
  15356. 'this.C = null;',
  15357. '']),
  15358. LinesToStr([ // $mod.$main
  15359. '$mod.B = rtl.asExt($mod.A, ExtB);',
  15360. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  15361. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  15362. '']));
  15363. end;
  15364. procedure TTestModule.TestExternalClass_DestructorFail;
  15365. begin
  15366. StartProgram(false);
  15367. Add('{$modeswitch externalclass}');
  15368. Add('type');
  15369. Add(' TExtA = class external name ''ExtA''');
  15370. Add(' destructor Free;');
  15371. Add(' end;');
  15372. SetExpectedPasResolverError('Pascal element not supported: destructor',
  15373. nPasElementNotSupported);
  15374. ConvertProgram;
  15375. end;
  15376. procedure TTestModule.TestExternalClass_New;
  15377. begin
  15378. StartProgram(false);
  15379. Add('{$modeswitch externalclass}');
  15380. Add('type');
  15381. Add(' TExtA = class external name ''ExtA''');
  15382. Add(' constructor New;');
  15383. Add(' constructor New(i: longint; j: longint = 2);');
  15384. Add(' end;');
  15385. Add('var');
  15386. Add(' A: texta;');
  15387. Add('begin');
  15388. Add(' a:=texta.new;');
  15389. Add(' a:=texta(texta.new);');
  15390. Add(' a:=texta.new();');
  15391. Add(' a:=texta.new(1);');
  15392. Add(' with texta do begin');
  15393. Add(' a:=new;');
  15394. Add(' a:=new();');
  15395. Add(' a:=new(2);');
  15396. Add(' end;');
  15397. Add(' a:=test1.texta.new;');
  15398. Add(' a:=test1.texta.new();');
  15399. Add(' a:=test1.texta.new(3);');
  15400. ConvertProgram;
  15401. CheckSource('TestExternalClass_New',
  15402. LinesToStr([ // statements
  15403. 'this.A = null;',
  15404. '']),
  15405. LinesToStr([ // $mod.$main
  15406. '$mod.A = new ExtA();',
  15407. '$mod.A = new ExtA();',
  15408. '$mod.A = new ExtA();',
  15409. '$mod.A = new ExtA(1,2);',
  15410. '$mod.A = new ExtA();',
  15411. '$mod.A = new ExtA();',
  15412. '$mod.A = new ExtA(2,2);',
  15413. '$mod.A = new ExtA();',
  15414. '$mod.A = new ExtA();',
  15415. '$mod.A = new ExtA(3,2);',
  15416. '']));
  15417. end;
  15418. procedure TTestModule.TestExternalClass_ClassOf_New;
  15419. begin
  15420. StartProgram(false);
  15421. Add('{$modeswitch externalclass}');
  15422. Add('type');
  15423. Add(' TExtAClass = class of TExtA;');
  15424. Add(' TExtA = class external name ''ExtA''');
  15425. Add(' C: TExtAClass;');
  15426. Add(' constructor New;');
  15427. Add(' end;');
  15428. Add('var');
  15429. Add(' A: texta;');
  15430. Add(' C: textaclass;');
  15431. Add('begin');
  15432. Add(' a:=c.new;');
  15433. Add(' a:=c.new();');
  15434. Add(' with C do begin');
  15435. Add(' a:=new;');
  15436. Add(' a:=new();');
  15437. Add(' end;');
  15438. Add(' a:=test1.c.new;');
  15439. Add(' a:=test1.c.new();');
  15440. Add(' a:=A.c.new();');
  15441. ConvertProgram;
  15442. CheckSource('TestExternalClass_ClassOf_New',
  15443. LinesToStr([ // statements
  15444. 'this.A = null;',
  15445. 'this.C = null;',
  15446. '']),
  15447. LinesToStr([ // $mod.$main
  15448. '$mod.A = new $mod.C();',
  15449. '$mod.A = new $mod.C();',
  15450. 'var $with1 = $mod.C;',
  15451. '$mod.A = new $with1();',
  15452. '$mod.A = new $with1();',
  15453. '$mod.A = new $mod.C();',
  15454. '$mod.A = new $mod.C();',
  15455. '$mod.A = new $mod.A.C();',
  15456. '']));
  15457. end;
  15458. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  15459. begin
  15460. StartProgram(false);
  15461. Add([
  15462. '{$modeswitch externalclass}',
  15463. 'type',
  15464. ' TExtAClass = class of TExtA;',
  15465. ' TExtA = class external name ''ExtA''',
  15466. ' constructor New;',
  15467. ' end;',
  15468. 'function GetCreator: TExtAClass;',
  15469. 'begin',
  15470. ' Result:=TExtA;',
  15471. 'end;',
  15472. 'var',
  15473. ' A: texta;',
  15474. 'begin',
  15475. ' a:=getcreator.new;',
  15476. ' a:=getcreator().new;',
  15477. ' a:=getcreator().new();',
  15478. ' a:=getcreator.new();',
  15479. ' with getcreator do begin',
  15480. ' a:=new;',
  15481. ' a:=new();',
  15482. ' end;']);
  15483. ConvertProgram;
  15484. CheckSource('TestExternalClass_FuncClassOf_New',
  15485. LinesToStr([ // statements
  15486. 'this.GetCreator = function () {',
  15487. ' var Result = null;',
  15488. ' Result = ExtA;',
  15489. ' return Result;',
  15490. '};',
  15491. 'this.A = null;',
  15492. '']),
  15493. LinesToStr([ // $mod.$main
  15494. '$mod.A = new ($mod.GetCreator())();',
  15495. '$mod.A = new ($mod.GetCreator())();',
  15496. '$mod.A = new ($mod.GetCreator())();',
  15497. '$mod.A = new ($mod.GetCreator())();',
  15498. 'var $with1 = $mod.GetCreator();',
  15499. '$mod.A = new $with1();',
  15500. '$mod.A = new $with1();',
  15501. '']));
  15502. end;
  15503. procedure TTestModule.TestExternalClass_New_PasClassFail;
  15504. begin
  15505. StartProgram(false);
  15506. Add([
  15507. '{$modeswitch externalclass}',
  15508. 'type',
  15509. ' TExtA = class external name ''ExtA''',
  15510. ' constructor New;',
  15511. ' end;',
  15512. ' TBird = class(TExtA)',
  15513. ' end;',
  15514. 'begin',
  15515. ' TBird.new;',
  15516. '']);
  15517. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  15518. ConvertProgram;
  15519. end;
  15520. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  15521. begin
  15522. StartProgram(false);
  15523. Add([
  15524. '{$modeswitch externalclass}',
  15525. 'type',
  15526. ' TExtA = class external name ''ExtA''',
  15527. ' constructor New;',
  15528. ' end;',
  15529. ' TBird = class(TExtA)',
  15530. ' end;',
  15531. 'begin',
  15532. ' TBird.new();',
  15533. '']);
  15534. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  15535. ConvertProgram;
  15536. end;
  15537. procedure TTestModule.TestExternalClass_LocalConstSameName;
  15538. begin
  15539. StartProgram(false);
  15540. Add('{$modeswitch externalclass}');
  15541. Add('type');
  15542. Add(' TExtA = class external name ''ExtA''');
  15543. Add(' constructor New;');
  15544. Add(' end;');
  15545. Add('function DoIt: longint;');
  15546. Add('const ExtA: longint = 3;');
  15547. Add('begin');
  15548. Add(' Result:=ExtA;');
  15549. Add('end;');
  15550. Add('var');
  15551. Add(' A: texta;');
  15552. Add('begin');
  15553. Add(' a:=texta.new;');
  15554. ConvertProgram;
  15555. CheckSource('TestExternalClass_LocalConstSameName',
  15556. LinesToStr([ // statements
  15557. 'var ExtA$1 = 3;',
  15558. 'this.DoIt = function () {',
  15559. ' var Result = 0;',
  15560. ' Result = ExtA$1;',
  15561. ' return Result;',
  15562. '};',
  15563. 'this.A = null;',
  15564. '']),
  15565. LinesToStr([ // $mod.$main
  15566. '$mod.A = new ExtA();',
  15567. '']));
  15568. end;
  15569. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  15570. begin
  15571. StartProgram(false);
  15572. Add('{$modeswitch externalclass}');
  15573. Add('type');
  15574. Add(' TExtA = class external name ''ExtA''');
  15575. Add(' procedure DoIt;');
  15576. Add(' end;');
  15577. Add(' TMyA = class(TExtA)');
  15578. Add(' procedure DoIt;');
  15579. Add(' end;');
  15580. Add('procedure TMyA.DoIt; begin end;');
  15581. Add('begin');
  15582. ConvertProgram;
  15583. CheckSource('TestExternalClass_ReintroduceOverload',
  15584. LinesToStr([ // statements
  15585. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  15586. ' this.$init = function () {',
  15587. ' };',
  15588. ' this.$final = function () {',
  15589. ' };',
  15590. ' this.DoIt$1 = function () {',
  15591. ' };',
  15592. '});',
  15593. '']),
  15594. LinesToStr([ // $mod.$main
  15595. '']));
  15596. end;
  15597. procedure TTestModule.TestExternalClass_Inherited;
  15598. begin
  15599. StartProgram(false);
  15600. Add('{$modeswitch externalclass}');
  15601. Add('type');
  15602. Add(' TExtA = class external name ''ExtA''');
  15603. Add(' procedure DoIt(i: longint = 1); virtual;');
  15604. Add(' procedure DoSome(j: longint = 2);');
  15605. Add(' end;');
  15606. Add(' TExtB = class external name ''ExtB''(TExtA)');
  15607. Add(' end;');
  15608. Add(' TMyC = class(TExtB)');
  15609. Add(' procedure DoIt(i: longint = 1); override;');
  15610. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  15611. Add(' end;');
  15612. Add('procedure TMyC.DoIt(i: longint);');
  15613. Add('begin');
  15614. Add(' inherited;');
  15615. Add(' inherited DoIt;');
  15616. Add(' inherited DoIt();');
  15617. Add(' inherited DoIt(3);');
  15618. Add(' inherited DoSome;');
  15619. Add(' inherited DoSome();');
  15620. Add(' inherited DoSome(4);');
  15621. Add('end;');
  15622. Add('procedure TMyC.DoSome(j: longint);');
  15623. Add('begin');
  15624. Add(' inherited;');
  15625. Add('end;');
  15626. Add('begin');
  15627. ConvertProgram;
  15628. CheckSource('TestExternalClass_ReintroduceOverload',
  15629. LinesToStr([ // statements
  15630. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  15631. ' this.$init = function () {',
  15632. ' };',
  15633. ' this.$final = function () {',
  15634. ' };',
  15635. ' this.DoIt = function (i) {',
  15636. ' ExtB.DoIt.apply(this, arguments);',
  15637. ' ExtB.DoIt.call(this, 1);',
  15638. ' ExtB.DoIt.call(this, 1);',
  15639. ' ExtB.DoIt.call(this, 3);',
  15640. ' ExtB.DoSome.call(this, 2);',
  15641. ' ExtB.DoSome.call(this, 2);',
  15642. ' ExtB.DoSome.call(this, 4);',
  15643. ' };',
  15644. ' this.DoSome$1 = function (j) {',
  15645. ' ExtB.DoSome.apply(this, arguments);',
  15646. ' };',
  15647. '});',
  15648. '']),
  15649. LinesToStr([ // $mod.$main
  15650. '']));
  15651. end;
  15652. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  15653. begin
  15654. StartProgram(false);
  15655. Add('{$modeswitch externalclass}');
  15656. Add('type');
  15657. Add(' TObject = class');
  15658. Add(' end;');
  15659. Add(' TExtA = class external name ''ExtA''(TObject)');
  15660. Add(' end;');
  15661. Add('begin');
  15662. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  15663. ConvertProgram;
  15664. end;
  15665. procedure TTestModule.TestExternalClass_NewInstance;
  15666. begin
  15667. StartProgram(false);
  15668. Add('{$modeswitch externalclass}');
  15669. Add('type');
  15670. Add(' TExtA = class external name ''ExtA''');
  15671. Add(' end;');
  15672. Add(' TMyB = class(TExtA)');
  15673. Add(' protected');
  15674. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  15675. Add(' end;');
  15676. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  15677. Add('begin end;');
  15678. Add('begin');
  15679. ConvertProgram;
  15680. CheckSource('TestExternalClass_NewInstance',
  15681. LinesToStr([ // statements
  15682. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  15683. ' this.$init = function () {',
  15684. ' };',
  15685. ' this.$final = function () {',
  15686. ' };',
  15687. ' this.NewInstance = function (fnname, paramarray) {',
  15688. ' var Result = null;',
  15689. ' return Result;',
  15690. ' };',
  15691. '});',
  15692. '']),
  15693. LinesToStr([ // $mod.$main
  15694. '']));
  15695. end;
  15696. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  15697. begin
  15698. StartProgram(false);
  15699. Add('{$modeswitch externalclass}');
  15700. Add('type');
  15701. Add(' TExtA = class external name ''ExtA''');
  15702. Add(' end;');
  15703. Add(' TMyB = class(TExtA)');
  15704. Add(' protected');
  15705. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  15706. Add(' end;');
  15707. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  15708. Add('begin end;');
  15709. Add('begin');
  15710. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  15711. ConvertProgram;
  15712. end;
  15713. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  15714. begin
  15715. StartProgram(false);
  15716. Add('{$modeswitch externalclass}');
  15717. Add('type');
  15718. Add(' TExtA = class external name ''ExtA''');
  15719. Add(' end;');
  15720. Add(' TMyB = class(TExtA)');
  15721. Add(' protected');
  15722. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  15723. Add(' end;');
  15724. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  15725. Add('begin end;');
  15726. Add('begin');
  15727. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  15728. nIncompatibleTypeArgNo);
  15729. ConvertProgram;
  15730. end;
  15731. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  15732. begin
  15733. StartProgram(false);
  15734. Add('{$modeswitch externalclass}');
  15735. Add('type');
  15736. Add(' TExtA = class external name ''ExtA''');
  15737. Add(' end;');
  15738. Add(' TMyB = class(TExtA)');
  15739. Add(' protected');
  15740. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  15741. Add(' end;');
  15742. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  15743. Add('begin end;');
  15744. Add('begin');
  15745. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  15746. nIncompatibleTypeArgNo);
  15747. ConvertProgram;
  15748. end;
  15749. procedure TTestModule.TestExternalClass_PascalProperty;
  15750. begin
  15751. StartProgram(false);
  15752. Add('{$modeswitch externalclass}');
  15753. Add('type');
  15754. Add(' TJSElement = class;');
  15755. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  15756. Add(' TJSElement = class external name ''ExtA''');
  15757. Add(' end;');
  15758. Add(' TControl = class(TJSElement)');
  15759. Add(' private');
  15760. Add(' FOnClick: TJSNotifyEvent;');
  15761. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  15762. Add(' procedure Click(Sender: TJSElement);');
  15763. Add(' end;');
  15764. Add('procedure TControl.Click(Sender: TJSElement);');
  15765. Add('begin');
  15766. Add(' OnClick(Self);');
  15767. Add('end;');
  15768. Add('var');
  15769. Add(' Ctrl: TControl;');
  15770. Add('begin');
  15771. Add(' Ctrl.OnClick:[email protected];');
  15772. Add(' Ctrl.OnClick(Ctrl);');
  15773. ConvertProgram;
  15774. CheckSource('TestExternalClass_PascalProperty',
  15775. LinesToStr([ // statements
  15776. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  15777. ' this.$init = function () {',
  15778. ' this.FOnClick = null;',
  15779. ' };',
  15780. ' this.$final = function () {',
  15781. ' this.FOnClick = undefined;',
  15782. ' };',
  15783. ' this.Click = function (Sender) {',
  15784. ' this.FOnClick(this);',
  15785. ' };',
  15786. '});',
  15787. 'this.Ctrl = null;',
  15788. '']),
  15789. LinesToStr([ // $mod.$main
  15790. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  15791. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  15792. '']));
  15793. end;
  15794. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  15795. begin
  15796. StartProgram(false);
  15797. Add([
  15798. '{$modeswitch externalclass}',
  15799. 'type',
  15800. ' IUnknown = interface end;',
  15801. ' TObject = class',
  15802. ' end;',
  15803. ' TChild = class',
  15804. ' end;',
  15805. ' TExtRootA = class external name ''ExtRootA''',
  15806. ' end;',
  15807. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  15808. ' end;',
  15809. ' TExtRootB = class external name ''ExtRootB''',
  15810. ' end;',
  15811. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  15812. ' end;',
  15813. 'var',
  15814. ' Obj: TObject;',
  15815. ' Child: TChild;',
  15816. ' RootA: TExtRootA;',
  15817. ' ChildA: TExtChildA;',
  15818. ' RootB: TExtRootB;',
  15819. ' ChildB: TExtChildB;',
  15820. ' i: IUnknown;',
  15821. 'begin',
  15822. ' obj:=tobject(roota);',
  15823. ' obj:=tobject(childa);',
  15824. ' child:=tchild(tobject(roota));',
  15825. ' roota:=textroota(obj);',
  15826. ' roota:=textroota(child);',
  15827. ' roota:=textroota(rootb);',
  15828. ' roota:=textroota(childb);',
  15829. ' childa:=textchilda(textroota(obj));',
  15830. ' roota:=TExtRootA(i)',
  15831. '']);
  15832. ConvertProgram;
  15833. CheckSource('TestExternalClass_TypeCastToRootClass',
  15834. LinesToStr([ // statements
  15835. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  15836. 'rtl.createClass($mod, "TObject", null, function () {',
  15837. ' this.$init = function () {',
  15838. ' };',
  15839. ' this.$final = function () {',
  15840. ' };',
  15841. '});',
  15842. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  15843. '});',
  15844. 'this.Obj = null;',
  15845. 'this.Child = null;',
  15846. 'this.RootA = null;',
  15847. 'this.ChildA = null;',
  15848. 'this.RootB = null;',
  15849. 'this.ChildB = null;',
  15850. 'this.i = null;',
  15851. '']),
  15852. LinesToStr([ // $mod.$main
  15853. '$mod.Obj = $mod.RootA;',
  15854. '$mod.Obj = $mod.ChildA;',
  15855. '$mod.Child = $mod.RootA;',
  15856. '$mod.RootA = $mod.Obj;',
  15857. '$mod.RootA = $mod.Child;',
  15858. '$mod.RootA = $mod.RootB;',
  15859. '$mod.RootA = $mod.ChildB;',
  15860. '$mod.ChildA = $mod.Obj;',
  15861. '$mod.RootA = $mod.i;',
  15862. '']));
  15863. end;
  15864. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  15865. begin
  15866. StartProgram(false);
  15867. Add([
  15868. '{$modeswitch externalclass}',
  15869. 'type',
  15870. ' IUnknown = interface end;',
  15871. ' IBird = interface(IUnknown) end;',
  15872. ' TClass = class of TObject;',
  15873. ' TObject = class',
  15874. ' end;',
  15875. ' TChild = class',
  15876. ' end;',
  15877. ' TJSObject = class external name ''Object''',
  15878. ' end;',
  15879. ' TRec = record end;',
  15880. 'var',
  15881. ' Obj: TObject;',
  15882. ' Child: TChild;',
  15883. ' i: IUnknown;',
  15884. ' Bird: IBird;',
  15885. ' j: TJSObject;',
  15886. ' r: TRec;',
  15887. ' c: TClass;',
  15888. 'begin',
  15889. ' j:=tjsobject(IUnknown);',
  15890. ' j:=tjsobject(IBird);',
  15891. ' j:=tjsobject(TObject);',
  15892. ' j:=tjsobject(TChild);',
  15893. ' j:=tjsobject(TRec);',
  15894. ' j:=tjsobject(Obj);',
  15895. ' j:=tjsobject(Child);',
  15896. ' j:=tjsobject(i);',
  15897. ' j:=tjsobject(Bird);',
  15898. ' j:=tjsobject(r);',
  15899. ' j:=tjsobject(c);',
  15900. '']);
  15901. ConvertProgram;
  15902. CheckSource('TestExternalClass_TypeCastToJSObject',
  15903. LinesToStr([ // statements
  15904. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  15905. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  15906. 'rtl.createClass($mod, "TObject", null, function () {',
  15907. ' this.$init = function () {',
  15908. ' };',
  15909. ' this.$final = function () {',
  15910. ' };',
  15911. '});',
  15912. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  15913. '});',
  15914. 'rtl.recNewT($mod, "TRec", function () {',
  15915. ' this.$eq = function (b) {',
  15916. ' return true;',
  15917. ' };',
  15918. ' this.$assign = function (s) {',
  15919. ' return this;',
  15920. ' };',
  15921. '});',
  15922. 'this.Obj = null;',
  15923. 'this.Child = null;',
  15924. 'this.i = null;',
  15925. 'this.Bird = null;',
  15926. 'this.j = null;',
  15927. 'this.r = $mod.TRec.$new();',
  15928. 'this.c = null;',
  15929. '']),
  15930. LinesToStr([ // $mod.$main
  15931. '$mod.j = $mod.IUnknown;',
  15932. '$mod.j = $mod.IBird;',
  15933. '$mod.j = $mod.TObject;',
  15934. '$mod.j = $mod.TChild;',
  15935. '$mod.j = $mod.TRec;',
  15936. '$mod.j = $mod.Obj;',
  15937. '$mod.j = $mod.Child;',
  15938. '$mod.j = $mod.i;',
  15939. '$mod.j = $mod.Bird;',
  15940. '$mod.j = $mod.r;',
  15941. '$mod.j = $mod.c;',
  15942. '']));
  15943. end;
  15944. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  15945. begin
  15946. StartProgram(false);
  15947. Add('{$modeswitch externalclass}');
  15948. Add('type');
  15949. Add(' TJSString = class external name ''String''');
  15950. Add(' class function fromCharCode() : string; varargs;');
  15951. Add(' function anchor(const aName : string) : string;');
  15952. Add(' end;');
  15953. Add('var');
  15954. Add(' s: string;');
  15955. Add('begin');
  15956. Add(' s:=TJSString.fromCharCode(65,66);');
  15957. Add(' s:=TJSString(s).anchor(s);');
  15958. Add(' s:=TJSString(''foo'').anchor(s);');
  15959. ConvertProgram;
  15960. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  15961. LinesToStr([ // statements
  15962. 'this.s = "";',
  15963. '']),
  15964. LinesToStr([ // $mod.$main
  15965. '$mod.s = String.fromCharCode(65, 66);',
  15966. '$mod.s = $mod.s.anchor($mod.s);',
  15967. '$mod.s = "foo".anchor($mod.s);',
  15968. '']));
  15969. end;
  15970. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  15971. begin
  15972. StartProgram(false);
  15973. Add([
  15974. '{$modeswitch externalclass}',
  15975. 'type',
  15976. ' TJSObject = class external name ''Object'' end;',
  15977. ' TJSFunction = class external name ''Function''',
  15978. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  15979. ' function call(thisArg: TJSObject): JSValue; varargs;',
  15980. ' end;',
  15981. ' TObject = class',
  15982. ' procedure DoIt(i: longint);',
  15983. ' end;',
  15984. ' TFuncInt = function(o: TObject): longint;',
  15985. 'function GetIt(o: TObject): longint;',
  15986. ' procedure Sub; begin end;',
  15987. 'var',
  15988. ' f: TJSFunction;',
  15989. ' fi: TFuncInt;',
  15990. 'begin',
  15991. ' fi:=TFuncInt(f);',
  15992. ' f:=TJSFunction(fi);',
  15993. ' f:=TJSFunction(@GetIt);',
  15994. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  15995. ' f:=TJSFunction(@Sub);',
  15996. ' f:=TJSFunction(@o.doit);',
  15997. ' f:=TJSFunction(fi).bind(nil,4)',
  15998. 'end;',
  15999. 'procedure TObject.DoIt(i: longint);',
  16000. ' procedure Sub; begin end;',
  16001. 'var f: TJSFunction;',
  16002. 'begin',
  16003. ' f:=TJSFunction(@DoIt);',
  16004. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  16005. ' f:=TJSFunction(@Sub);',
  16006. ' f:=TJSFunction(@GetIt);',
  16007. 'end;',
  16008. 'begin']);
  16009. ConvertProgram;
  16010. CheckSource('TestExternalClass_TypeCastToJSFunction',
  16011. LinesToStr([ // statements
  16012. 'rtl.createClass($mod, "TObject", null, function () {',
  16013. ' this.$init = function () {',
  16014. ' };',
  16015. ' this.$final = function () {',
  16016. ' };',
  16017. ' this.DoIt = function (i) {',
  16018. ' var $Self = this;',
  16019. ' function Sub() {',
  16020. ' };',
  16021. ' var f = null;',
  16022. ' f = rtl.createCallback($Self, "DoIt");',
  16023. ' f = rtl.createCallback($Self, "DoIt").bind(null, 13);',
  16024. ' f = Sub;',
  16025. ' f = $mod.GetIt;',
  16026. ' };',
  16027. '});',
  16028. 'this.GetIt = function (o) {',
  16029. ' var Result = 0;',
  16030. ' function Sub() {',
  16031. ' };',
  16032. ' var f = null;',
  16033. ' var fi = null;',
  16034. ' fi = f;',
  16035. ' f = fi;',
  16036. ' f = $mod.GetIt;',
  16037. ' f = $mod.GetIt.bind(null, 3);',
  16038. ' f = Sub;',
  16039. ' f = rtl.createCallback(o, "DoIt");',
  16040. ' f = fi.bind(null, 4);',
  16041. ' return Result;',
  16042. '};',
  16043. '']),
  16044. LinesToStr([ // $mod.$main
  16045. '']));
  16046. end;
  16047. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  16048. begin
  16049. StartProgram(false);
  16050. Add([
  16051. '{$mode delphi}',
  16052. '{$modeswitch externalclass}',
  16053. 'type',
  16054. ' TJSObject = class external name ''Object'' end;',
  16055. ' TJSWindow = class external name ''Window''(TJSObject)',
  16056. ' procedure Open;',
  16057. ' end;',
  16058. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  16059. ' procedure Execute;',
  16060. ' end;',
  16061. 'procedure Fly;',
  16062. 'var',
  16063. ' w: TJSWindow;',
  16064. ' e: TJSEventTarget;',
  16065. 'begin',
  16066. ' w:=TJSWindow(e);',
  16067. ' e:=TJSEventTarget(w);',
  16068. 'end;',
  16069. 'begin']);
  16070. ConvertProgram;
  16071. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  16072. LinesToStr([ // statements
  16073. 'this.Fly = function () {',
  16074. ' var w = null;',
  16075. ' var e = null;',
  16076. ' w = e;',
  16077. ' e = w;',
  16078. '};',
  16079. '']),
  16080. LinesToStr([ // $mod.$main
  16081. '']));
  16082. end;
  16083. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  16084. begin
  16085. StartProgram(false);
  16086. Add('{$modeswitch externalclass}');
  16087. Add('type');
  16088. Add(' TJSString = class external name ''String''');
  16089. Add(' class function fromCharCode() : string; varargs;');
  16090. Add(' end;');
  16091. Add('var');
  16092. Add(' s: string;');
  16093. Add(' sObj: TJSString;');
  16094. Add('begin');
  16095. Add(' s:=sObj.fromCharCode(65,66);');
  16096. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  16097. nExternalClassInstanceCannotAccessStaticX);
  16098. ConvertProgram;
  16099. end;
  16100. procedure TTestModule.TestExternalClass_BracketAccessor;
  16101. begin
  16102. StartProgram(false);
  16103. Add([
  16104. '{$modeswitch externalclass}',
  16105. 'type',
  16106. ' TJSArray = class external name ''Array2''',
  16107. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  16108. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  16109. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  16110. ' end;',
  16111. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  16112. 'begin end;',
  16113. 'var',
  16114. ' Arr: tjsarray;',
  16115. ' s: string;',
  16116. ' i: longint;',
  16117. ' v: jsvalue;',
  16118. 'begin',
  16119. ' v:=arr[0];',
  16120. ' v:=arr.items[1];',
  16121. ' arr[2]:=s;',
  16122. ' arr.items[3]:=s;',
  16123. ' arr[4]:=i;',
  16124. ' arr[5]:=arr[6];',
  16125. ' arr.items[7]:=arr.items[8];',
  16126. ' with arr do items[9]:=items[10];',
  16127. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  16128. ' with arr do begin',
  16129. ' v:=GetItems(14);',
  16130. ' setitems(15,16);',
  16131. ' end;',
  16132. ' v:=test1.arr.items[17];',
  16133. ' test1.arr.items[18]:=v;',
  16134. '']);
  16135. ConvertProgram;
  16136. CheckSource('TestExternalClass_BracketAccessor',
  16137. LinesToStr([ // statements
  16138. 'this.DoIt = function (vI, vJ, vK, vL) {',
  16139. '};',
  16140. 'this.Arr = null;',
  16141. 'this.s = "";',
  16142. 'this.i = 0;',
  16143. 'this.v = undefined;',
  16144. '']),
  16145. LinesToStr([ // $mod.$main
  16146. '$mod.v = $mod.Arr[0];',
  16147. '$mod.v = $mod.Arr[1];',
  16148. '$mod.Arr[2] = $mod.s;',
  16149. '$mod.Arr[3] = $mod.s;',
  16150. '$mod.Arr[4] = $mod.i;',
  16151. '$mod.Arr[5] = $mod.Arr[6];',
  16152. '$mod.Arr[7] = $mod.Arr[8];',
  16153. 'var $with1 = $mod.Arr;',
  16154. '$with1[9] = $with1[10];',
  16155. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  16156. ' a: 9,',
  16157. ' p: $mod.Arr,',
  16158. ' get: function () {',
  16159. ' return this.p[this.a];',
  16160. ' },',
  16161. ' set: function (v) {',
  16162. ' this.p[this.a] = v;',
  16163. ' }',
  16164. '}, {',
  16165. ' a: 10,',
  16166. ' p: $mod.Arr,',
  16167. ' get: function () {',
  16168. ' return this.p[this.a];',
  16169. ' },',
  16170. ' set: function (v) {',
  16171. ' this.p[this.a] = v;',
  16172. ' }',
  16173. '});',
  16174. 'var $with2 = $mod.Arr;',
  16175. '$mod.v = $with2[14];',
  16176. '$with2[15] = 16;',
  16177. '$mod.v = $mod.Arr[17];',
  16178. '$mod.Arr[18] = $mod.v;',
  16179. '']));
  16180. end;
  16181. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  16182. begin
  16183. StartProgram(false);
  16184. Add([
  16185. '{$modeswitch externalclass}',
  16186. 'type',
  16187. ' TJSArray = class external name ''Array2''',
  16188. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  16189. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  16190. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  16191. ' end;',
  16192. ' TMyArr = class(TJSArray)',
  16193. ' procedure DoIt;',
  16194. ' end;',
  16195. 'procedure tmyarr.DoIt;',
  16196. 'begin',
  16197. ' Items[1]:=Items[2];',
  16198. ' SetItems(3,getItems(4));',
  16199. 'end;',
  16200. 'var',
  16201. ' Arr: tmyarr;',
  16202. ' s: string;',
  16203. ' i: longint;',
  16204. ' v: jsvalue;',
  16205. 'begin',
  16206. ' v:=arr[0];',
  16207. ' v:=arr.items[1];',
  16208. ' arr[2]:=s;',
  16209. ' arr.items[3]:=s;',
  16210. ' arr[4]:=i;',
  16211. ' arr[5]:=arr[6];',
  16212. ' arr.items[7]:=arr.items[8];',
  16213. ' with arr do items[9]:=items[10];',
  16214. ' with arr do begin',
  16215. ' v:=GetItems(14);',
  16216. ' setitems(15,16);',
  16217. ' end;',
  16218. '']);
  16219. ConvertProgram;
  16220. CheckSource('TestExternalClass_BracketAccessor_Call',
  16221. LinesToStr([ // statements
  16222. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  16223. ' this.$init = function () {',
  16224. ' };',
  16225. ' this.$final = function () {',
  16226. ' };',
  16227. ' this.DoIt = function () {',
  16228. ' this[1] = this[2];',
  16229. ' this[3] = this[4];',
  16230. ' };',
  16231. '});',
  16232. 'this.Arr = null;',
  16233. 'this.s = "";',
  16234. 'this.i = 0;',
  16235. 'this.v = undefined;',
  16236. '']),
  16237. LinesToStr([ // $mod.$main
  16238. '$mod.v = $mod.Arr[0];',
  16239. '$mod.v = $mod.Arr[1];',
  16240. '$mod.Arr[2] = $mod.s;',
  16241. '$mod.Arr[3] = $mod.s;',
  16242. '$mod.Arr[4] = $mod.i;',
  16243. '$mod.Arr[5] = $mod.Arr[6];',
  16244. '$mod.Arr[7] = $mod.Arr[8];',
  16245. 'var $with1 = $mod.Arr;',
  16246. '$with1[9] = $with1[10];',
  16247. 'var $with2 = $mod.Arr;',
  16248. '$mod.v = $with2[14];',
  16249. '$with2[15] = 16;',
  16250. '']));
  16251. end;
  16252. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  16253. begin
  16254. StartProgram(false);
  16255. Add('{$modeswitch externalclass}');
  16256. Add('type');
  16257. Add(' TJSArray = class external name ''Array2''');
  16258. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  16259. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  16260. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  16261. Add(' end;');
  16262. Add('begin');
  16263. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  16264. nBracketAccessorOfExternalClassMustHaveOneParameter);
  16265. ConvertProgram;
  16266. end;
  16267. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  16268. begin
  16269. StartProgram(false);
  16270. Add('{$modeswitch externalclass}');
  16271. Add('type');
  16272. Add(' TJSArray = class external name ''Array2''');
  16273. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16274. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  16275. Add(' end;');
  16276. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  16277. Add('begin end;');
  16278. Add('var');
  16279. Add(' Arr: tjsarray;');
  16280. Add(' v: jsvalue;');
  16281. Add('begin');
  16282. Add(' v:=arr[0];');
  16283. Add(' v:=arr.items[1];');
  16284. Add(' with arr do v:=items[2];');
  16285. Add(' doit(arr[3],arr[4]);');
  16286. ConvertProgram;
  16287. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  16288. LinesToStr([ // statements
  16289. 'this.DoIt = function (vI, vJ) {',
  16290. '};',
  16291. 'this.Arr = null;',
  16292. 'this.v = undefined;',
  16293. '']),
  16294. LinesToStr([ // $mod.$main
  16295. '$mod.v = $mod.Arr[0];',
  16296. '$mod.v = $mod.Arr[1];',
  16297. 'var $with1 = $mod.Arr;',
  16298. '$mod.v = $with1[2];',
  16299. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  16300. '']));
  16301. end;
  16302. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  16303. begin
  16304. StartProgram(false);
  16305. Add('{$modeswitch externalclass}');
  16306. Add('type');
  16307. Add(' TJSArray = class external name ''Array2''');
  16308. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16309. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16310. Add(' end;');
  16311. Add('var');
  16312. Add(' Arr: tjsarray;');
  16313. Add(' s: string;');
  16314. Add(' i: longint;');
  16315. Add(' v: jsvalue;');
  16316. Add('begin');
  16317. Add(' arr[2]:=s;');
  16318. Add(' arr.items[3]:=s;');
  16319. Add(' arr[4]:=i;');
  16320. Add(' with arr do items[5]:=i;');
  16321. ConvertProgram;
  16322. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  16323. LinesToStr([ // statements
  16324. 'this.Arr = null;',
  16325. 'this.s = "";',
  16326. 'this.i = 0;',
  16327. 'this.v = undefined;',
  16328. '']),
  16329. LinesToStr([ // $mod.$main
  16330. '$mod.Arr[2] = $mod.s;',
  16331. '$mod.Arr[3] = $mod.s;',
  16332. '$mod.Arr[4] = $mod.i;',
  16333. 'var $with1 = $mod.Arr;',
  16334. '$with1[5] = $mod.i;',
  16335. '']));
  16336. end;
  16337. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  16338. begin
  16339. StartProgram(false);
  16340. Add('{$modeswitch externalclass}');
  16341. Add('type');
  16342. Add(' TJSArray = class external name ''Array2''');
  16343. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16344. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16345. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  16346. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  16347. Add(' end;');
  16348. Add('var');
  16349. Add(' Arr: tjsarray;');
  16350. Add(' s: string;');
  16351. Add(' i: longint;');
  16352. Add(' v: jsvalue;');
  16353. Add('begin');
  16354. Add(' arr[2]:=s;');
  16355. Add(' arr.items[3]:=s;');
  16356. Add(' arr.numbers[4]:=i;');
  16357. Add(' with arr do items[5]:=i;');
  16358. Add(' with arr do numbers[6]:=i;');
  16359. ConvertProgram;
  16360. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  16361. LinesToStr([ // statements
  16362. 'this.Arr = null;',
  16363. 'this.s = "";',
  16364. 'this.i = 0;',
  16365. 'this.v = undefined;',
  16366. '']),
  16367. LinesToStr([ // $mod.$main
  16368. '$mod.Arr[2] = $mod.s;',
  16369. '$mod.Arr[3] = $mod.s;',
  16370. '$mod.Arr[4] = $mod.i;',
  16371. 'var $with1 = $mod.Arr;',
  16372. '$with1[5] = $mod.i;',
  16373. 'var $with2 = $mod.Arr;',
  16374. '$with2[6] = $mod.i;',
  16375. '']));
  16376. end;
  16377. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  16378. begin
  16379. StartProgram(false);
  16380. Add('{$modeswitch externalclass}');
  16381. Add('type');
  16382. Add(' TJSArray = class external name ''Array2''');
  16383. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16384. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16385. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  16386. Add(' end;');
  16387. Add('var');
  16388. Add(' Arr: tjsarray;');
  16389. Add(' i: longint;');
  16390. Add(' IntArr: array of longint;');
  16391. Add(' v: jsvalue;');
  16392. Add('begin');
  16393. Add(' v:=arr.items[i];');
  16394. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  16395. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  16396. ConvertProgram;
  16397. CheckSource('TestExternalClass_BracketAccessor_Index',
  16398. LinesToStr([ // statements
  16399. 'this.Arr = null;',
  16400. 'this.i = 0;',
  16401. 'this.IntArr = [];',
  16402. 'this.v = undefined;',
  16403. '']),
  16404. LinesToStr([ // $mod.$main
  16405. '$mod.v = $mod.Arr[$mod.i];',
  16406. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  16407. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  16408. '']));
  16409. end;
  16410. procedure TTestModule.TestExternalClass_ForInJSObject;
  16411. begin
  16412. StartProgram(false);
  16413. Add([
  16414. '{$modeswitch externalclass}',
  16415. 'type',
  16416. ' TJSObject = class external name ''Object''',
  16417. ' end;',
  16418. 'var',
  16419. ' o: TJSObject;',
  16420. ' key: string;',
  16421. 'begin',
  16422. ' for key in o do',
  16423. ' if key=''abc'' then ;',
  16424. '']);
  16425. ConvertProgram;
  16426. CheckSource('TestExternalClass_ForInJSObject',
  16427. LinesToStr([ // statements
  16428. 'this.o = null;',
  16429. 'this.key = "";',
  16430. '']),
  16431. LinesToStr([ // $mod.$main
  16432. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  16433. '']));
  16434. end;
  16435. procedure TTestModule.TestExternalClass_ForInJSArray;
  16436. begin
  16437. StartProgram(false);
  16438. Add([
  16439. '{$modeswitch externalclass}',
  16440. 'type',
  16441. ' TJSInt8Array = class external name ''Int8Array''',
  16442. ' private',
  16443. ' flength: NativeInt external name ''length'';',
  16444. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  16445. ' public',
  16446. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  16447. ' property Length: NativeInt read flength;',
  16448. ' end;',
  16449. 'var',
  16450. ' a: TJSInt8Array;',
  16451. ' value: shortint;',
  16452. 'begin',
  16453. ' for value in a do',
  16454. ' if value=3 then ;',
  16455. '']);
  16456. ConvertProgram;
  16457. CheckSource('TestExternalClass_ForInJSArray',
  16458. LinesToStr([ // statements
  16459. 'this.a = null;',
  16460. 'this.value = 0;',
  16461. '']),
  16462. LinesToStr([ // $mod.$main
  16463. 'for (var $in1 = $mod.a, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) {',
  16464. ' $mod.value = $in1[$l2];',
  16465. ' if ($mod.value === 3) ;',
  16466. '};',
  16467. '']));
  16468. end;
  16469. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  16470. begin
  16471. AddModuleWithIntfImplSrc('unit2.pas',
  16472. LinesToStr([
  16473. '{$modeswitch externalclass}',
  16474. 'type',
  16475. ' TJSBufferSource = class external name ''BufferSource''',
  16476. ' end;',
  16477. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  16478. '']),
  16479. '');
  16480. AddModuleWithIntfImplSrc('unit3.pas',
  16481. LinesToStr([
  16482. '{$modeswitch externalclass}',
  16483. 'type',
  16484. ' TJSBufferSource = class external name ''BufferSource''',
  16485. ' end;',
  16486. '']),
  16487. '');
  16488. StartUnit(true);
  16489. Add([
  16490. 'interface',
  16491. 'uses unit2, unit3;',
  16492. 'procedure DoSome(s: TJSBufferSource);',
  16493. 'implementation',
  16494. 'procedure DoSome(s: TJSBufferSource);',
  16495. 'begin',
  16496. ' DoIt(s);',
  16497. 'end;',
  16498. '']);
  16499. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  16500. nIncompatibleTypeArgNo);
  16501. ConvertUnit;
  16502. end;
  16503. procedure TTestModule.TestClassInterface_Corba;
  16504. begin
  16505. StartProgram(false);
  16506. Add([
  16507. '{$interfaces corba}',
  16508. 'type',
  16509. ' IUnknown = interface;',
  16510. ' IUnknown = interface',
  16511. ' [''{00000000-0000-0000-C000-000000000046}'']',
  16512. ' end;',
  16513. ' IInterface = IUnknown;',
  16514. ' IBird = interface(IInterface)',
  16515. ' function GetSize: longint;',
  16516. ' procedure SetSize(i: longint);',
  16517. ' property Size: longint read GetSize write SetSize;',
  16518. ' procedure DoIt(i: longint);',
  16519. ' end;',
  16520. ' TObject = class',
  16521. ' end;',
  16522. ' TBird = class(TObject,IBird)',
  16523. ' function GetSize: longint; virtual; abstract;',
  16524. ' procedure SetSize(i: longint); virtual; abstract;',
  16525. ' procedure DoIt(i: longint); virtual; abstract;',
  16526. ' end;',
  16527. 'var',
  16528. ' BirdIntf: IBird;',
  16529. 'begin',
  16530. ' BirdIntf.Size:=BirdIntf.Size;',
  16531. '']);
  16532. ConvertProgram;
  16533. CheckSource('TestClassInterface_Corba',
  16534. LinesToStr([ // statements
  16535. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  16536. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  16537. 'rtl.createClass($mod, "TObject", null, function () {',
  16538. ' this.$init = function () {',
  16539. ' };',
  16540. ' this.$final = function () {',
  16541. ' };',
  16542. '});',
  16543. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16544. ' rtl.addIntf(this, $mod.IBird);',
  16545. '});',
  16546. 'this.BirdIntf = null;',
  16547. '']),
  16548. LinesToStr([ // $mod.$main
  16549. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  16550. '']));
  16551. end;
  16552. procedure TTestModule.TestClassInterface_ProcExternalFail;
  16553. begin
  16554. StartProgram(false);
  16555. Add([
  16556. '{$interfaces corba}',
  16557. 'type',
  16558. ' IUnknown = interface',
  16559. ' procedure DoIt; external name ''foo'';',
  16560. ' end;',
  16561. 'begin']);
  16562. SetExpectedParserError(
  16563. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  16564. nParserNoFieldsAllowed);
  16565. ConvertProgram;
  16566. end;
  16567. procedure TTestModule.TestClassInterface_Overloads;
  16568. begin
  16569. StartProgram(false);
  16570. Add([
  16571. '{$interfaces corba}',
  16572. 'type',
  16573. ' integer = longint;',
  16574. ' IUnknown = interface',
  16575. ' procedure DoIt(i: integer);',
  16576. ' procedure DoIt(s: string);',
  16577. ' end;',
  16578. ' IBird = interface(IUnknown)',
  16579. ' procedure DoIt(b: boolean); overload;',
  16580. ' end;',
  16581. ' TObject = class',
  16582. ' end;',
  16583. ' TBird = class(TObject,IBird)',
  16584. ' procedure DoIt(o: TObject);',
  16585. ' procedure DoIt(s: string);',
  16586. ' procedure DoIt(i: integer);',
  16587. ' procedure DoIt(b: boolean);',
  16588. ' end;',
  16589. 'procedure TBird.DoIt(o: TObject); begin end;',
  16590. 'procedure TBird.DoIt(s: string); begin end;',
  16591. 'procedure TBird.DoIt(i: integer); begin end;',
  16592. 'procedure TBird.DoIt(b: boolean); begin end;',
  16593. 'var',
  16594. ' BirdIntf: IBird;',
  16595. 'begin',
  16596. ' BirdIntf.DoIt(3);',
  16597. ' BirdIntf.DoIt(''abc'');',
  16598. ' BirdIntf.DoIt(true);',
  16599. '']);
  16600. ConvertProgram;
  16601. CheckSource('TestClassInterface_Overloads',
  16602. LinesToStr([ // statements
  16603. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  16604. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  16605. 'rtl.createClass($mod, "TObject", null, function () {',
  16606. ' this.$init = function () {',
  16607. ' };',
  16608. ' this.$final = function () {',
  16609. ' };',
  16610. '});',
  16611. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16612. ' this.DoIt = function (o) {',
  16613. ' };',
  16614. ' this.DoIt$1 = function (s) {',
  16615. ' };',
  16616. ' this.DoIt$2 = function (i) {',
  16617. ' };',
  16618. ' this.DoIt$3 = function (b) {',
  16619. ' };',
  16620. ' rtl.addIntf(this, $mod.IBird, {',
  16621. ' DoIt$2: "DoIt$3",',
  16622. ' DoIt: "DoIt$2"',
  16623. ' });',
  16624. '});',
  16625. 'this.BirdIntf = null;',
  16626. '']),
  16627. LinesToStr([ // $mod.$main
  16628. '$mod.BirdIntf.DoIt(3);',
  16629. '$mod.BirdIntf.DoIt$1("abc");',
  16630. '$mod.BirdIntf.DoIt$2(true);',
  16631. '']));
  16632. end;
  16633. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  16634. begin
  16635. StartProgram(false);
  16636. Add([
  16637. '{$interfaces corba}',
  16638. 'type',
  16639. ' IBird = interface',
  16640. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16641. ' end;',
  16642. ' IDog = interface',
  16643. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16644. ' end;',
  16645. ' TObject = class(IBird,IDog)',
  16646. ' end;',
  16647. 'begin']);
  16648. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  16649. nDuplicateGUIDXInYZ);
  16650. ConvertProgram;
  16651. end;
  16652. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  16653. begin
  16654. StartProgram(false);
  16655. Add([
  16656. '{$interfaces corba}',
  16657. 'type',
  16658. ' IAnimal = interface',
  16659. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16660. ' end;',
  16661. ' IBird = interface(IAnimal)',
  16662. ' end;',
  16663. ' IHawk = interface(IBird)',
  16664. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16665. ' end;',
  16666. 'begin']);
  16667. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  16668. nDuplicateGUIDXInYZ);
  16669. ConvertProgram;
  16670. end;
  16671. procedure TTestModule.TestClassInterface_AncestorImpl;
  16672. begin
  16673. StartProgram(false);
  16674. Add([
  16675. '{$interfaces corba}',
  16676. 'type',
  16677. ' integer = longint;',
  16678. ' IUnknown = interface',
  16679. ' procedure DoIt(i: integer);',
  16680. ' end;',
  16681. ' IBird = interface',
  16682. ' procedure Fly(i: integer);',
  16683. ' end;',
  16684. ' TObject = class(IUnknown)',
  16685. ' procedure DoIt(i: integer);',
  16686. ' end;',
  16687. ' TBird = class(IBird)',
  16688. ' procedure Fly(i: integer);',
  16689. ' end;',
  16690. 'procedure TObject.DoIt(i: integer); begin end;',
  16691. 'procedure TBird.Fly(i: integer); begin end;',
  16692. 'begin',
  16693. '']);
  16694. ConvertProgram;
  16695. CheckSource('TestClassInterface_AncestorIntf',
  16696. LinesToStr([ // statements
  16697. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  16698. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  16699. 'rtl.createClass($mod, "TObject", null, function () {',
  16700. ' this.$init = function () {',
  16701. ' };',
  16702. ' this.$final = function () {',
  16703. ' };',
  16704. ' this.DoIt = function (i) {',
  16705. ' };',
  16706. ' rtl.addIntf(this, $mod.IUnknown);',
  16707. '});',
  16708. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16709. ' this.Fly = function (i) {',
  16710. ' };',
  16711. ' rtl.addIntf(this, $mod.IBird);',
  16712. ' rtl.addIntf(this, $mod.IUnknown);',
  16713. '});',
  16714. '']),
  16715. LinesToStr([ // $mod.$main
  16716. '']));
  16717. end;
  16718. procedure TTestModule.TestClassInterface_ImplReintroduce;
  16719. begin
  16720. StartProgram(false);
  16721. Add([
  16722. '{$interfaces corba}',
  16723. 'type',
  16724. ' integer = longint;',
  16725. ' IBird = interface',
  16726. ' procedure DoIt(i: integer);',
  16727. ' end;',
  16728. ' TObject = class',
  16729. ' procedure DoIt(i: integer);',
  16730. ' end;',
  16731. ' TBird = class(IBird)',
  16732. ' procedure DoIt(i: integer); virtual; reintroduce;',
  16733. ' end;',
  16734. 'procedure TObject.DoIt(i: integer); begin end;',
  16735. 'procedure TBird.DoIt(i: integer); begin end;',
  16736. 'begin',
  16737. '']);
  16738. ConvertProgram;
  16739. CheckSource('TestClassInterface_ImplReintroduce',
  16740. LinesToStr([ // statements
  16741. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  16742. 'rtl.createClass($mod, "TObject", null, function () {',
  16743. ' this.$init = function () {',
  16744. ' };',
  16745. ' this.$final = function () {',
  16746. ' };',
  16747. ' this.DoIt = function (i) {',
  16748. ' };',
  16749. '});',
  16750. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16751. ' this.DoIt$1 = function (i) {',
  16752. ' };',
  16753. ' rtl.addIntf(this, $mod.IBird, {',
  16754. ' DoIt: "DoIt$1"',
  16755. ' });',
  16756. '});',
  16757. '']),
  16758. LinesToStr([ // $mod.$main
  16759. '']));
  16760. end;
  16761. procedure TTestModule.TestClassInterface_MethodResolution;
  16762. begin
  16763. StartProgram(false);
  16764. Add([
  16765. '{$interfaces corba}',
  16766. 'type',
  16767. ' IUnknown = interface',
  16768. ' procedure Walk(i: longint);',
  16769. ' end;',
  16770. ' IBird = interface(IUnknown)',
  16771. ' procedure Walk(b: boolean); overload;',
  16772. ' procedure Fly(s: string);',
  16773. ' end;',
  16774. ' TObject = class',
  16775. ' end;',
  16776. ' TBird = class(TObject,IBird)',
  16777. ' procedure IBird.Fly = Move;',
  16778. ' procedure IBird.Walk = Hop;',
  16779. ' procedure Hop(i: longint);',
  16780. ' procedure Move(s: string);',
  16781. ' procedure Hop(b: boolean);',
  16782. ' end;',
  16783. 'procedure TBird.Move(s: string); begin end;',
  16784. 'procedure TBird.Hop(i: longint); begin end;',
  16785. 'procedure TBird.Hop(b: boolean); begin end;',
  16786. 'var',
  16787. ' BirdIntf: IBird;',
  16788. 'begin',
  16789. ' BirdIntf.Walk(3);',
  16790. ' BirdIntf.Walk(true);',
  16791. ' BirdIntf.Fly(''abc'');',
  16792. '']);
  16793. ConvertProgram;
  16794. CheckSource('TestClassInterface_MethodResolution',
  16795. LinesToStr([ // statements
  16796. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  16797. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  16798. 'rtl.createClass($mod, "TObject", null, function () {',
  16799. ' this.$init = function () {',
  16800. ' };',
  16801. ' this.$final = function () {',
  16802. ' };',
  16803. '});',
  16804. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16805. ' this.Hop = function (i) {',
  16806. ' };',
  16807. ' this.Move = function (s) {',
  16808. ' };',
  16809. ' this.Hop$1 = function (b) {',
  16810. ' };',
  16811. ' rtl.addIntf(this, $mod.IBird, {',
  16812. ' Walk$1: "Hop$1",',
  16813. ' Fly: "Move",',
  16814. ' Walk: "Hop"',
  16815. ' });',
  16816. '});',
  16817. 'this.BirdIntf = null;',
  16818. '']),
  16819. LinesToStr([ // $mod.$main
  16820. '$mod.BirdIntf.Walk(3);',
  16821. '$mod.BirdIntf.Walk$1(true);',
  16822. '$mod.BirdIntf.Fly("abc");',
  16823. '']));
  16824. end;
  16825. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  16826. begin
  16827. StartProgram(false);
  16828. Add([
  16829. '{$interfaces com}',
  16830. 'type',
  16831. ' IUnknown = interface',
  16832. ' function _AddRef: longint;',
  16833. ' procedure Walk;',
  16834. ' end;',
  16835. ' IBird = interface end;',
  16836. ' IDog = interface end;',
  16837. ' TObject = class(IBird,IDog)',
  16838. ' function _AddRef: longint; virtual; abstract;',
  16839. ' procedure Walk; virtual; abstract;',
  16840. ' end;',
  16841. ' TBird = class(IUnknown)',
  16842. ' end;',
  16843. 'begin',
  16844. '']);
  16845. ConvertProgram;
  16846. CheckSource('TestClassInterface_COM_AncestorLess',
  16847. LinesToStr([ // statements
  16848. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  16849. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  16850. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  16851. 'rtl.createClass($mod, "TObject", null, function () {',
  16852. ' this.$init = function () {',
  16853. ' };',
  16854. ' this.$final = function () {',
  16855. ' };',
  16856. ' rtl.addIntf(this, $mod.IBird);',
  16857. ' rtl.addIntf(this, $mod.IDog);',
  16858. '});',
  16859. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16860. ' rtl.addIntf(this, $mod.IUnknown);',
  16861. ' rtl.addIntf(this, $mod.IBird);',
  16862. ' rtl.addIntf(this, $mod.IDog);',
  16863. '});',
  16864. '']),
  16865. LinesToStr([ // $mod.$main
  16866. '']));
  16867. end;
  16868. procedure TTestModule.TestClassInterface_MethodOverride;
  16869. begin
  16870. StartProgram(false);
  16871. Add([
  16872. '{$interfaces corba}',
  16873. 'type',
  16874. ' IUnknown = interface',
  16875. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  16876. ' procedure Go;',
  16877. ' end;',
  16878. ' TObject = class(IUnknown)',
  16879. ' procedure Go; virtual; abstract;',
  16880. ' end;',
  16881. ' TBird = class',
  16882. ' procedure Go; override;',
  16883. ' end;',
  16884. ' TCat = class(TObject)',
  16885. ' procedure Go; override;',
  16886. ' end;',
  16887. ' TDog = class(TObject, IUnknown)',
  16888. ' procedure Go; override;',
  16889. ' end;',
  16890. 'procedure TBird.Go; begin end;',
  16891. 'procedure TCat.Go; begin end;',
  16892. 'procedure TDog.Go; begin end;',
  16893. 'begin',
  16894. '']);
  16895. ConvertProgram;
  16896. CheckSource('TestClassInterface_MethodOverride',
  16897. LinesToStr([ // statements
  16898. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  16899. 'rtl.createClass($mod, "TObject", null, function () {',
  16900. ' this.$init = function () {',
  16901. ' };',
  16902. ' this.$final = function () {',
  16903. ' };',
  16904. ' rtl.addIntf(this, $mod.IUnknown);',
  16905. '});',
  16906. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16907. ' this.Go = function () {',
  16908. ' };',
  16909. ' rtl.addIntf(this, $mod.IUnknown);',
  16910. '});',
  16911. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  16912. ' this.Go = function () {',
  16913. ' };',
  16914. ' rtl.addIntf(this, $mod.IUnknown);',
  16915. '});',
  16916. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  16917. ' this.Go = function () {',
  16918. ' };',
  16919. ' rtl.addIntf(this, $mod.IUnknown);',
  16920. '});',
  16921. '']),
  16922. LinesToStr([ // $mod.$main
  16923. '']));
  16924. end;
  16925. procedure TTestModule.TestClassInterface_Corba_Delegation;
  16926. begin
  16927. StartProgram(false);
  16928. Add([
  16929. '{$interfaces corba}',
  16930. 'type',
  16931. ' IUnknown = interface',
  16932. ' end;',
  16933. ' IBird = interface(IUnknown)',
  16934. ' procedure Fly(s: string);',
  16935. ' end;',
  16936. ' IEagle = interface(IBird)',
  16937. ' end;',
  16938. ' IDove = interface(IBird)',
  16939. ' end;',
  16940. ' ISwallow = interface(IBird)',
  16941. ' end;',
  16942. ' TObject = class',
  16943. ' end;',
  16944. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  16945. ' procedure Fly(s: string); virtual; abstract;',
  16946. ' end;',
  16947. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  16948. ' FBirdIntf: IBird;',
  16949. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  16950. ' function GetEagleIntf: IEagle; virtual; abstract;',
  16951. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  16952. ' FDoveObj: TBird;',
  16953. ' property DoveObj: TBird read FDoveObj implements IDove;',
  16954. ' function GetSwallowObj: TBird; virtual; abstract;',
  16955. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  16956. ' end;',
  16957. 'begin',
  16958. '']);
  16959. ConvertProgram;
  16960. CheckSource('TestClassInterface_Delegation',
  16961. LinesToStr([ // statements
  16962. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16963. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  16964. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  16965. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  16966. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  16967. 'rtl.createClass($mod, "TObject", null, function () {',
  16968. ' this.$init = function () {',
  16969. ' };',
  16970. ' this.$final = function () {',
  16971. ' };',
  16972. '});',
  16973. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16974. ' rtl.addIntf(this, $mod.IBird);',
  16975. ' rtl.addIntf(this, $mod.IEagle);',
  16976. ' rtl.addIntf(this, $mod.IDove);',
  16977. ' rtl.addIntf(this, $mod.ISwallow);',
  16978. '});',
  16979. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  16980. ' this.$init = function () {',
  16981. ' $mod.TObject.$init.call(this);',
  16982. ' this.FBirdIntf = null;',
  16983. ' this.FDoveObj = null;',
  16984. ' };',
  16985. ' this.$final = function () {',
  16986. ' this.FBirdIntf = undefined;',
  16987. ' this.FDoveObj = undefined;',
  16988. ' $mod.TObject.$final.call(this);',
  16989. ' };',
  16990. ' this.$intfmaps = {',
  16991. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  16992. ' return this.FBirdIntf;',
  16993. ' },',
  16994. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  16995. ' return this.GetEagleIntf();',
  16996. ' },',
  16997. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  16998. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  16999. ' },',
  17000. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17001. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  17002. ' }',
  17003. ' };',
  17004. '});',
  17005. '']),
  17006. LinesToStr([ // $mod.$main
  17007. '']));
  17008. end;
  17009. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  17010. begin
  17011. StartProgram(false);
  17012. Add([
  17013. '{$interfaces corba}',
  17014. 'type',
  17015. ' IUnknown = interface',
  17016. ' end;',
  17017. ' IBird = interface(IUnknown)',
  17018. ' procedure Fly(s: string);',
  17019. ' end;',
  17020. ' IEagle = interface(IBird)',
  17021. ' end;',
  17022. ' IDove = interface(IBird)',
  17023. ' end;',
  17024. ' ISwallow = interface(IBird)',
  17025. ' end;',
  17026. ' TObject = class',
  17027. ' end;',
  17028. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17029. ' procedure Fly(s: string); virtual; abstract;',
  17030. ' end;',
  17031. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17032. ' private',
  17033. ' class var FBirdIntf: IBird;',
  17034. ' class var FDoveObj: TBird;',
  17035. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  17036. ' class function GetSwallowObj: TBird; virtual; abstract;',
  17037. ' protected',
  17038. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  17039. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17040. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  17041. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17042. ' end;',
  17043. 'begin',
  17044. '']);
  17045. ConvertProgram;
  17046. CheckSource('TestClassInterface_DelegationStatic',
  17047. LinesToStr([ // statements
  17048. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17049. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  17050. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  17051. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  17052. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  17053. 'rtl.createClass($mod, "TObject", null, function () {',
  17054. ' this.$init = function () {',
  17055. ' };',
  17056. ' this.$final = function () {',
  17057. ' };',
  17058. '});',
  17059. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17060. ' rtl.addIntf(this, $mod.IBird);',
  17061. ' rtl.addIntf(this, $mod.IEagle);',
  17062. ' rtl.addIntf(this, $mod.IDove);',
  17063. ' rtl.addIntf(this, $mod.ISwallow);',
  17064. '});',
  17065. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17066. ' this.FBirdIntf = null;',
  17067. ' this.FDoveObj = null;',
  17068. ' this.$intfmaps = {',
  17069. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  17070. ' return this.FBirdIntf;',
  17071. ' },',
  17072. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  17073. ' return this.$class.GetEagleIntf();',
  17074. ' },',
  17075. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  17076. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  17077. ' },',
  17078. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17079. ' return rtl.getIntfT(this.$class.GetSwallowObj(), $mod.ISwallow);',
  17080. ' }',
  17081. ' };',
  17082. '});',
  17083. '']),
  17084. LinesToStr([ // $mod.$main
  17085. '']));
  17086. end;
  17087. procedure TTestModule.TestClassInterface_Corba_Operators;
  17088. begin
  17089. StartProgram(false);
  17090. Add([
  17091. '{$interfaces corba}',
  17092. 'type',
  17093. ' IUnknown = interface',
  17094. ' end;',
  17095. ' IBird = interface(IUnknown)',
  17096. ' function GetItems(Index: longint): longint;',
  17097. ' procedure SetItems(Index: longint; Value: longint);',
  17098. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  17099. ' end;',
  17100. ' TObject = class',
  17101. ' end;',
  17102. ' TBird = class(TObject,IBird)',
  17103. ' function GetItems(Index: longint): longint; virtual; abstract;',
  17104. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  17105. ' end;',
  17106. 'var',
  17107. ' IntfVar: IBird = nil;',
  17108. ' IntfVar2: IBird;',
  17109. ' ObjVar: TBird;',
  17110. ' v: JSValue;',
  17111. 'begin',
  17112. ' IntfVar:=nil;',
  17113. ' IntfVar[3]:=IntfVar[4];',
  17114. ' if Assigned(IntfVar) then ;',
  17115. ' IntfVar:=IntfVar2;',
  17116. ' IntfVar:=ObjVar;',
  17117. ' if IntfVar=IntfVar2 then ;',
  17118. ' if IntfVar<>IntfVar2 then ;',
  17119. ' if IntfVar is IBird then ;',
  17120. ' if IntfVar is TBird then ;',
  17121. ' if ObjVar is IBird then ;',
  17122. ' IntfVar:=IntfVar2 as IBird;',
  17123. ' ObjVar:=IntfVar2 as TBird;',
  17124. ' IntfVar:=ObjVar as IBird;',
  17125. ' IntfVar:=IBird(IntfVar2);',
  17126. ' ObjVar:=TBird(IntfVar);',
  17127. ' IntfVar:=IBird(ObjVar);',
  17128. ' v:=IntfVar;',
  17129. ' IntfVar:=IBird(v);',
  17130. ' if v is IBird then ;',
  17131. ' v:=JSValue(IntfVar);',
  17132. ' v:=IBird;',
  17133. '']);
  17134. ConvertProgram;
  17135. CheckSource('TestClassInterface_Corba_Operators',
  17136. LinesToStr([ // statements
  17137. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17138. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  17139. 'rtl.createClass($mod, "TObject", null, function () {',
  17140. ' this.$init = function () {',
  17141. ' };',
  17142. ' this.$final = function () {',
  17143. ' };',
  17144. '});',
  17145. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17146. ' rtl.addIntf(this, $mod.IBird);',
  17147. '});',
  17148. 'this.IntfVar = null;',
  17149. 'this.IntfVar2 = null;',
  17150. 'this.ObjVar = null;',
  17151. 'this.v = undefined;',
  17152. '']),
  17153. LinesToStr([ // $mod.$main
  17154. '$mod.IntfVar = null;',
  17155. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  17156. 'if ($mod.IntfVar != null) ;',
  17157. '$mod.IntfVar = $mod.IntfVar2;',
  17158. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  17159. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  17160. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  17161. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  17162. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  17163. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  17164. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  17165. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  17166. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  17167. '$mod.IntfVar = $mod.IntfVar2;',
  17168. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  17169. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  17170. '$mod.v = $mod.IntfVar;',
  17171. '$mod.IntfVar = rtl.getObject($mod.v);',
  17172. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  17173. '$mod.v = $mod.IntfVar;',
  17174. '$mod.v = $mod.IBird;',
  17175. '']));
  17176. end;
  17177. procedure TTestModule.TestClassInterface_Corba_Args;
  17178. begin
  17179. StartProgram(false);
  17180. Add([
  17181. '{$interfaces corba}',
  17182. 'type',
  17183. ' IUnknown = interface',
  17184. ' end;',
  17185. ' IBird = interface(IUnknown)',
  17186. ' end;',
  17187. ' TObject = class',
  17188. ' end;',
  17189. ' TBird = class(TObject,IBird)',
  17190. ' end;',
  17191. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  17192. 'begin',
  17193. ' DoIt(i,i,i);',
  17194. 'end;',
  17195. 'procedure Change(var i: IBird; out j: IBird);',
  17196. 'begin',
  17197. ' DoIt(i,i,i);',
  17198. ' Change(i,i);',
  17199. 'end;',
  17200. 'var',
  17201. ' i: IBird;',
  17202. ' o: TBird;',
  17203. 'begin',
  17204. ' DoIt(i,i,i);',
  17205. ' Change(i,i);',
  17206. ' DoIt(o,o,o);',
  17207. '']);
  17208. ConvertProgram;
  17209. CheckSource('TestClassInterface_Corba_Args',
  17210. LinesToStr([ // statements
  17211. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17212. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  17213. 'rtl.createClass($mod, "TObject", null, function () {',
  17214. ' this.$init = function () {',
  17215. ' };',
  17216. ' this.$final = function () {',
  17217. ' };',
  17218. '});',
  17219. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17220. ' rtl.addIntf(this, $mod.IBird);',
  17221. '});',
  17222. 'this.DoIt = function (u, i, j) {',
  17223. ' $mod.DoIt({',
  17224. ' get: function () {',
  17225. ' return i;',
  17226. ' },',
  17227. ' set: function (v) {',
  17228. ' i = v;',
  17229. ' }',
  17230. ' }, i, i);',
  17231. '};',
  17232. 'this.Change = function (i, j) {',
  17233. ' $mod.DoIt(i, i.get(), i.get());',
  17234. ' $mod.Change(i, i);',
  17235. '};',
  17236. 'this.i = null;',
  17237. 'this.o = null;',
  17238. '']),
  17239. LinesToStr([ // $mod.$main
  17240. '$mod.DoIt({',
  17241. ' p: $mod,',
  17242. ' get: function () {',
  17243. ' return this.p.i;',
  17244. ' },',
  17245. ' set: function (v) {',
  17246. ' this.p.i = v;',
  17247. ' }',
  17248. '}, $mod.i, $mod.i);',
  17249. '$mod.Change({',
  17250. ' p: $mod,',
  17251. ' get: function () {',
  17252. ' return this.p.i;',
  17253. ' },',
  17254. ' set: function (v) {',
  17255. ' this.p.i = v;',
  17256. ' }',
  17257. '}, {',
  17258. ' p: $mod,',
  17259. ' get: function () {',
  17260. ' return this.p.i;',
  17261. ' },',
  17262. ' set: function (v) {',
  17263. ' this.p.i = v;',
  17264. ' }',
  17265. '});',
  17266. '$mod.DoIt({',
  17267. ' p: $mod,',
  17268. ' get: function () {',
  17269. ' return this.p.o;',
  17270. ' },',
  17271. ' set: function (v) {',
  17272. ' this.p.o = v;',
  17273. ' }',
  17274. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  17275. '']));
  17276. end;
  17277. procedure TTestModule.TestClassInterface_Corba_ForIn;
  17278. begin
  17279. StartProgram(false);
  17280. Add([
  17281. '{$interfaces corba}',
  17282. 'type',
  17283. ' IUnknown = interface end;',
  17284. ' TObject = class',
  17285. ' Id: longint;',
  17286. ' end;',
  17287. ' IEnumerator = interface(IUnknown)',
  17288. ' function GetCurrent: TObject;',
  17289. ' function MoveNext: Boolean;',
  17290. ' property Current: TObject read GetCurrent;',
  17291. ' end;',
  17292. ' IEnumerable = interface(IUnknown)',
  17293. ' function GetEnumerator: IEnumerator;',
  17294. ' end;',
  17295. 'var',
  17296. ' o: TObject;',
  17297. ' i: IEnumerable;',
  17298. 'begin',
  17299. ' for o in i do o.Id:=3;',
  17300. '']);
  17301. ConvertProgram;
  17302. CheckSource('TestClassInterface_Corba_ForIn',
  17303. LinesToStr([ // statements
  17304. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17305. 'rtl.createClass($mod, "TObject", null, function () {',
  17306. ' this.$init = function () {',
  17307. ' this.Id = 0;',
  17308. ' };',
  17309. ' this.$final = function () {',
  17310. ' };',
  17311. '});',
  17312. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  17313. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  17314. 'this.o = null;',
  17315. 'this.i = null;',
  17316. '']),
  17317. LinesToStr([ // $mod.$main
  17318. 'var $in1 = $mod.i.GetEnumerator();',
  17319. 'while ($in1.MoveNext()) {',
  17320. ' $mod.o = $in1.GetCurrent();',
  17321. ' $mod.o.Id = 3;',
  17322. '};',
  17323. '']));
  17324. end;
  17325. procedure TTestModule.TestClassInterface_COM_AssignVar;
  17326. begin
  17327. StartProgram(false);
  17328. Add([
  17329. '{$interfaces com}',
  17330. 'type',
  17331. ' IUnknown = interface',
  17332. ' function _AddRef: longint;',
  17333. ' function _Release: longint;',
  17334. ' end;',
  17335. ' TObject = class(IUnknown)',
  17336. ' function _AddRef: longint; virtual; abstract;',
  17337. ' function _Release: longint; virtual; abstract;',
  17338. ' end;',
  17339. 'var',
  17340. ' i: IUnknown;',
  17341. 'procedure DoGlobal(o: TObject);',
  17342. 'begin',
  17343. ' i:=nil;',
  17344. ' i:=o;',
  17345. ' i:=i;',
  17346. 'end;',
  17347. 'procedure DoLocal(o: TObject);',
  17348. 'const k: IUnknown = nil;',
  17349. 'var j: IUnknown;',
  17350. 'begin',
  17351. ' k:=o;',
  17352. ' k:=i;',
  17353. ' j:=o;',
  17354. ' j:=i;',
  17355. 'end;',
  17356. 'var o: TObject;',
  17357. 'begin',
  17358. ' i:=nil;',
  17359. ' i:=o;',
  17360. '']);
  17361. ConvertProgram;
  17362. CheckSource('TestClassInterface_COM_AssignVar',
  17363. LinesToStr([ // statements
  17364. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17365. 'rtl.createClass($mod, "TObject", null, function () {',
  17366. ' this.$init = function () {',
  17367. ' };',
  17368. ' this.$final = function () {',
  17369. ' };',
  17370. ' rtl.addIntf(this, $mod.IUnknown);',
  17371. '});',
  17372. 'this.i = null;',
  17373. 'this.DoGlobal = function (o) {',
  17374. ' rtl.setIntfP($mod, "i", null);',
  17375. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  17376. ' rtl.setIntfP($mod, "i", $mod.i);',
  17377. '};',
  17378. 'var k = null;',
  17379. 'this.DoLocal = function (o) {',
  17380. ' var j = null;',
  17381. ' try{',
  17382. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17383. ' k = rtl.setIntfL(k, $mod.i);',
  17384. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17385. ' j = rtl.setIntfL(j, $mod.i);',
  17386. ' }finally{',
  17387. ' rtl._Release(j);',
  17388. ' };',
  17389. '};',
  17390. 'this.o = null;',
  17391. '']),
  17392. LinesToStr([ // $mod.$main
  17393. 'rtl.setIntfP($mod, "i", null);',
  17394. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  17395. '']));
  17396. end;
  17397. procedure TTestModule.TestClassInterface_COM_AssignArg;
  17398. begin
  17399. StartProgram(false);
  17400. Add([
  17401. '{$interfaces com}',
  17402. 'type',
  17403. ' IUnknown = interface',
  17404. ' function _AddRef: longint;',
  17405. ' function _Release: longint;',
  17406. ' end;',
  17407. ' TObject = class(IUnknown)',
  17408. ' function _AddRef: longint; virtual; abstract;',
  17409. ' function _Release: longint; virtual; abstract;',
  17410. ' end;',
  17411. 'procedure DoDefault(i, j: IUnknown);',
  17412. 'begin',
  17413. ' i:=nil;',
  17414. ' i:=j;',
  17415. 'end;',
  17416. 'begin',
  17417. '']);
  17418. ConvertProgram;
  17419. CheckSource('TestClassInterface_COM_AssignArg',
  17420. LinesToStr([ // statements
  17421. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17422. 'rtl.createClass($mod, "TObject", null, function () {',
  17423. ' this.$init = function () {',
  17424. ' };',
  17425. ' this.$final = function () {',
  17426. ' };',
  17427. ' rtl.addIntf(this, $mod.IUnknown);',
  17428. '});',
  17429. 'this.DoDefault = function (i, j) {',
  17430. ' rtl._AddRef(i);',
  17431. ' try {',
  17432. ' i = rtl.setIntfL(i, null);',
  17433. ' i = rtl.setIntfL(i, j);',
  17434. ' } finally {',
  17435. ' rtl._Release(i);',
  17436. ' };',
  17437. '};',
  17438. '']),
  17439. LinesToStr([ // $mod.$main
  17440. '']));
  17441. end;
  17442. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  17443. begin
  17444. StartProgram(false);
  17445. Add([
  17446. '{$interfaces com}',
  17447. 'type',
  17448. ' IUnknown = interface',
  17449. ' function _AddRef: longint;',
  17450. ' function _Release: longint;',
  17451. ' end;',
  17452. ' TObject = class(IUnknown)',
  17453. ' function _AddRef: longint; virtual; abstract;',
  17454. ' function _Release: longint; virtual; abstract;',
  17455. ' end;',
  17456. 'function DoDefault(i: IUnknown): IUnknown;',
  17457. 'begin',
  17458. ' Result:=i;',
  17459. ' if Result<>nil then exit;',
  17460. 'end;',
  17461. 'begin',
  17462. '']);
  17463. ConvertProgram;
  17464. CheckSource('TestClassInterface_COM_FunctionResult',
  17465. LinesToStr([ // statements
  17466. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17467. 'rtl.createClass($mod, "TObject", null, function () {',
  17468. ' this.$init = function () {',
  17469. ' };',
  17470. ' this.$final = function () {',
  17471. ' };',
  17472. ' rtl.addIntf(this, $mod.IUnknown);',
  17473. '});',
  17474. 'this.DoDefault = function (i) {',
  17475. ' var Result = null;',
  17476. ' var $ok = false;',
  17477. ' try {',
  17478. ' Result = rtl.setIntfL(Result, i);',
  17479. ' if(Result !== null){',
  17480. ' $ok = true;',
  17481. ' return Result;',
  17482. ' };',
  17483. ' $ok = true;',
  17484. ' } finally {',
  17485. ' if(!$ok) rtl._Release(Result);',
  17486. ' };',
  17487. ' return Result;',
  17488. '};',
  17489. '']),
  17490. LinesToStr([ // $mod.$main
  17491. '']));
  17492. end;
  17493. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  17494. begin
  17495. StartProgram(false);
  17496. Add([
  17497. '{$interfaces com}',
  17498. 'type',
  17499. ' IUnknown = interface',
  17500. ' function _AddRef: longint;',
  17501. ' function _Release: longint;',
  17502. ' end;',
  17503. ' TObject = class(IUnknown)',
  17504. ' function _AddRef: longint; virtual; abstract;',
  17505. ' function _Release: longint; virtual; abstract;',
  17506. ' function GetIntf: IUnknown; virtual;',
  17507. ' end;',
  17508. ' TMouse = class',
  17509. ' function GetIntf: IUnknown; override;',
  17510. ' end;',
  17511. 'function TObject.GetIntf: IUnknown; begin end;',
  17512. 'function TMouse.GetIntf: IUnknown;',
  17513. 'var i: IUnknown;',
  17514. 'begin',
  17515. ' inherited;',
  17516. ' inherited GetIntf;',
  17517. ' inherited GetIntf();',
  17518. ' Result:=inherited GetIntf;',
  17519. ' Result:=inherited GetIntf();',
  17520. ' i:=inherited GetIntf;',
  17521. ' i:=inherited GetIntf();',
  17522. 'end;',
  17523. 'begin',
  17524. '']);
  17525. ConvertProgram;
  17526. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  17527. LinesToStr([ // statements
  17528. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17529. 'rtl.createClass($mod, "TObject", null, function () {',
  17530. ' this.$init = function () {',
  17531. ' };',
  17532. ' this.$final = function () {',
  17533. ' };',
  17534. ' this.GetIntf = function () {',
  17535. ' var Result = null;',
  17536. ' return Result;',
  17537. ' };',
  17538. ' rtl.addIntf(this, $mod.IUnknown);',
  17539. '});',
  17540. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  17541. ' this.GetIntf = function () {',
  17542. ' var Result = null;',
  17543. ' var i = null;',
  17544. ' var $ir = rtl.createIntfRefs();',
  17545. ' var $ok = false;',
  17546. ' try {',
  17547. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  17548. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  17549. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  17550. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  17551. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  17552. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  17553. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  17554. ' $ok = true;',
  17555. ' } finally {',
  17556. ' $ir.free();',
  17557. ' rtl._Release(i);',
  17558. ' if (!$ok) rtl._Release(Result);',
  17559. ' };',
  17560. ' return Result;',
  17561. ' };',
  17562. ' rtl.addIntf(this, $mod.IUnknown);',
  17563. '});',
  17564. '']),
  17565. LinesToStr([ // $mod.$main
  17566. '']));
  17567. end;
  17568. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  17569. begin
  17570. StartProgram(false);
  17571. Add([
  17572. '{$interfaces com}',
  17573. 'type',
  17574. ' IUnknown = interface',
  17575. ' function _AddRef: longint;',
  17576. ' function _Release: longint;',
  17577. ' end;',
  17578. ' TObject = class(IUnknown)',
  17579. ' function _AddRef: longint; virtual; abstract;',
  17580. ' function _Release: longint; virtual; abstract;',
  17581. ' end;',
  17582. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  17583. 'begin',
  17584. ' if i is IUnknown then ;',
  17585. ' if o is IUnknown then ;',
  17586. ' if i is TObject then ;',
  17587. ' i:=j as IUnknown;',
  17588. ' i:=o as IUnknown;',
  17589. ' o:=j as TObject;',
  17590. ' i:=IUnknown(j);',
  17591. ' i:=IUnknown(o);',
  17592. ' o:=TObject(i);',
  17593. 'end;',
  17594. 'begin',
  17595. '']);
  17596. ConvertProgram;
  17597. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  17598. LinesToStr([ // statements
  17599. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17600. 'rtl.createClass($mod, "TObject", null, function () {',
  17601. ' this.$init = function () {',
  17602. ' };',
  17603. ' this.$final = function () {',
  17604. ' };',
  17605. ' rtl.addIntf(this, $mod.IUnknown);',
  17606. '});',
  17607. 'this.DoDefault = function (i, j, o) {',
  17608. ' rtl._AddRef(i);',
  17609. ' try {',
  17610. ' if ($mod.IUnknown.isPrototypeOf(i)) ;',
  17611. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  17612. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  17613. ' i = rtl.setIntfL(i, rtl.as(j, $mod.IUnknown));',
  17614. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17615. ' o = rtl.intfAsClass(j, $mod.TObject);',
  17616. ' i = rtl.setIntfL(i, j);',
  17617. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17618. ' o = rtl.intfToClass(i, $mod.TObject);',
  17619. ' } finally {',
  17620. ' rtl._Release(i);',
  17621. ' };',
  17622. '};',
  17623. '']),
  17624. LinesToStr([ // $mod.$main
  17625. '']));
  17626. end;
  17627. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  17628. begin
  17629. StartProgram(false);
  17630. Add([
  17631. '{$interfaces com}',
  17632. 'type',
  17633. ' IUnknown = interface',
  17634. ' function _AddRef: longint;',
  17635. ' function _Release: longint;',
  17636. ' end;',
  17637. ' TObject = class(IUnknown)',
  17638. ' function _AddRef: longint; virtual; abstract;',
  17639. ' function _Release: longint; virtual; abstract;',
  17640. ' end;',
  17641. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  17642. 'var o: TObject;',
  17643. 'begin',
  17644. ' DoIt(v,v,v,v);',
  17645. ' DoIt(o,o,k,k);',
  17646. 'end;',
  17647. 'procedure DoSome;',
  17648. 'var v: IUnknown;',
  17649. 'begin',
  17650. ' DoIt(v,v,v,v);',
  17651. 'end;',
  17652. 'var i: IUnknown;',
  17653. 'begin',
  17654. ' DoIt(i,i,i,i);',
  17655. '']);
  17656. ConvertProgram;
  17657. CheckSource('TestClassInterface_COM_PassAsArg',
  17658. LinesToStr([ // statements
  17659. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17660. 'rtl.createClass($mod, "TObject", null, function () {',
  17661. ' this.$init = function () {',
  17662. ' };',
  17663. ' this.$final = function () {',
  17664. ' };',
  17665. ' rtl.addIntf(this, $mod.IUnknown);',
  17666. '});',
  17667. 'this.DoIt = function (v, j, k, l) {',
  17668. ' var o = null;',
  17669. ' var $ir = rtl.createIntfRefs();',
  17670. ' rtl._AddRef(v);',
  17671. ' try {',
  17672. ' $mod.DoIt(v, v, {',
  17673. ' get: function () {',
  17674. ' return v;',
  17675. ' },',
  17676. ' set: function (w) {',
  17677. ' v = rtl.setIntfL(v, w);',
  17678. ' }',
  17679. ' }, {',
  17680. ' get: function () {',
  17681. ' return v;',
  17682. ' },',
  17683. ' set: function (w) {',
  17684. ' v = rtl.setIntfL(v, w);',
  17685. ' }',
  17686. ' });',
  17687. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  17688. ' } finally {',
  17689. ' $ir.free();',
  17690. ' rtl._Release(v);',
  17691. ' };',
  17692. '};',
  17693. 'this.DoSome = function () {',
  17694. ' var v = null;',
  17695. ' try {',
  17696. ' $mod.DoIt(v, v, {',
  17697. ' get: function () {',
  17698. ' return v;',
  17699. ' },',
  17700. ' set: function (w) {',
  17701. ' v = rtl.setIntfL(v, w);',
  17702. ' }',
  17703. ' }, {',
  17704. ' get: function () {',
  17705. ' return v;',
  17706. ' },',
  17707. ' set: function (w) {',
  17708. ' v = rtl.setIntfL(v, w);',
  17709. ' }',
  17710. ' });',
  17711. ' } finally {',
  17712. ' rtl._Release(v);',
  17713. ' };',
  17714. '};',
  17715. 'this.i = null;',
  17716. '']),
  17717. LinesToStr([ // $mod.$main
  17718. '$mod.DoIt($mod.i, $mod.i, {',
  17719. ' p: $mod,',
  17720. ' get: function () {',
  17721. ' return this.p.i;',
  17722. ' },',
  17723. ' set: function (v) {',
  17724. ' rtl.setIntfP(this.p, "i", v);',
  17725. ' }',
  17726. '}, {',
  17727. ' p: $mod,',
  17728. ' get: function () {',
  17729. ' return this.p.i;',
  17730. ' },',
  17731. ' set: function (v) {',
  17732. ' rtl.setIntfP(this.p, "i", v);',
  17733. ' }',
  17734. '});',
  17735. '']));
  17736. end;
  17737. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  17738. begin
  17739. StartProgram(false);
  17740. Add([
  17741. '{$interfaces com}',
  17742. 'type',
  17743. ' IUnknown = interface',
  17744. ' function _AddRef: longint;',
  17745. ' function _Release: longint;',
  17746. ' end;',
  17747. ' TObject = class(IUnknown)',
  17748. ' function _AddRef: longint; virtual; abstract;',
  17749. ' function _Release: longint; virtual; abstract;',
  17750. ' end;',
  17751. 'procedure DoIt(out i);',
  17752. 'begin end;',
  17753. 'procedure DoSome;',
  17754. 'var v: IUnknown;',
  17755. 'begin',
  17756. ' DoIt(v);',
  17757. 'end;',
  17758. 'function GetIt: IUnknown;',
  17759. 'begin',
  17760. ' DoIt(Result);',
  17761. 'end;',
  17762. 'var i: IUnknown;',
  17763. 'begin',
  17764. ' DoIt(i);',
  17765. '']);
  17766. ConvertProgram;
  17767. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  17768. LinesToStr([ // statements
  17769. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17770. 'rtl.createClass($mod, "TObject", null, function () {',
  17771. ' this.$init = function () {',
  17772. ' };',
  17773. ' this.$final = function () {',
  17774. ' };',
  17775. ' rtl.addIntf(this, $mod.IUnknown);',
  17776. '});',
  17777. 'this.DoIt = function (i) {',
  17778. '};',
  17779. 'this.DoSome = function () {',
  17780. ' var v = null;',
  17781. ' try {',
  17782. ' $mod.DoIt({',
  17783. ' get: function () {',
  17784. ' return v;',
  17785. ' },',
  17786. ' set: function (w) {',
  17787. ' v = w;',
  17788. ' }',
  17789. ' });',
  17790. ' } finally {',
  17791. ' rtl._Release(v);',
  17792. ' };',
  17793. '};',
  17794. 'this.GetIt = function () {',
  17795. ' var Result = null;',
  17796. ' var $ok = false;',
  17797. ' try {',
  17798. ' $mod.DoIt({',
  17799. ' get: function () {',
  17800. ' return Result;',
  17801. ' },',
  17802. ' set: function (v) {',
  17803. ' Result = v;',
  17804. ' }',
  17805. ' });',
  17806. ' $ok = true;',
  17807. ' } finally {',
  17808. ' if (!$ok) rtl._Release(Result);',
  17809. ' };',
  17810. ' return Result;',
  17811. '};',
  17812. 'this.i = null;',
  17813. '']),
  17814. LinesToStr([ // $mod.$main
  17815. 'try {',
  17816. ' $mod.DoIt({',
  17817. ' p: $mod,',
  17818. ' get: function () {',
  17819. ' return this.p.i;',
  17820. ' },',
  17821. ' set: function (v) {',
  17822. ' this.p.i = v;',
  17823. ' }',
  17824. ' });',
  17825. '} finally {',
  17826. ' rtl._Release($mod.i);',
  17827. '};',
  17828. '']));
  17829. end;
  17830. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  17831. begin
  17832. StartProgram(false);
  17833. Add([
  17834. '{$interfaces com}',
  17835. 'type',
  17836. ' IUnknown = interface',
  17837. ' function _AddRef: longint;',
  17838. ' function _Release: longint;',
  17839. ' end;',
  17840. ' TObject = class(IUnknown)',
  17841. ' function _AddRef: longint; virtual; abstract;',
  17842. ' function _Release: longint; virtual; abstract;',
  17843. ' end;',
  17844. 'function GetIt: IUnknown;',
  17845. 'begin',
  17846. 'end;',
  17847. 'procedure DoSome;',
  17848. 'var v: IUnknown;',
  17849. ' i: longint;',
  17850. 'begin',
  17851. ' v:=GetIt;',
  17852. ' v:=GetIt();',
  17853. ' GetIt()._AddRef;',
  17854. ' i:=GetIt()._AddRef;',
  17855. 'end;',
  17856. 'var v: IUnknown;',
  17857. ' i: longint;',
  17858. 'begin',
  17859. ' v:=GetIt;',
  17860. ' v:=GetIt();',
  17861. ' GetIt()._AddRef;',
  17862. ' i:=GetIt()._AddRef;',
  17863. '']);
  17864. ConvertProgram;
  17865. CheckSource('TestClassInterface_COM_FunctionInExpr',
  17866. LinesToStr([ // statements
  17867. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17868. 'rtl.createClass($mod, "TObject", null, function () {',
  17869. ' this.$init = function () {',
  17870. ' };',
  17871. ' this.$final = function () {',
  17872. ' };',
  17873. ' rtl.addIntf(this, $mod.IUnknown);',
  17874. '});',
  17875. 'this.GetIt = function () {',
  17876. ' var Result = null;',
  17877. ' return Result;',
  17878. '};',
  17879. 'this.DoSome = function () {',
  17880. ' var v = null;',
  17881. ' var i = 0;',
  17882. ' var $ir = rtl.createIntfRefs();',
  17883. ' try {',
  17884. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  17885. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  17886. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  17887. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  17888. ' } finally {',
  17889. ' $ir.free();',
  17890. ' rtl._Release(v);',
  17891. ' };',
  17892. '};',
  17893. 'this.v = null;',
  17894. 'this.i = 0;',
  17895. '']),
  17896. LinesToStr([ // $mod.$main
  17897. 'var $ir = rtl.createIntfRefs();',
  17898. 'try {',
  17899. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  17900. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  17901. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  17902. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  17903. '} finally {',
  17904. ' $ir.free();',
  17905. '};',
  17906. '']));
  17907. end;
  17908. procedure TTestModule.TestClassInterface_COM_Property;
  17909. begin
  17910. StartProgram(false);
  17911. Add([
  17912. '{$interfaces com}',
  17913. 'type',
  17914. ' IUnknown = interface',
  17915. ' function _AddRef: longint;',
  17916. ' function _Release: longint;',
  17917. ' end;',
  17918. ' TObject = class(IUnknown)',
  17919. ' FAnt: IUnknown;',
  17920. ' function _AddRef: longint; virtual; abstract;',
  17921. ' function _Release: longint; virtual; abstract;',
  17922. ' function GetBird: IUnknown; virtual; abstract;',
  17923. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  17924. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  17925. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  17926. ' property Ant: IUnknown read FAnt write FAnt;',
  17927. ' property Bird: IUnknown read GetBird write SetBird;',
  17928. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  17929. ' end;',
  17930. 'procedure DoIt;',
  17931. 'var',
  17932. ' o: TObject;',
  17933. ' v: IUnknown;',
  17934. 'begin',
  17935. ' v:=o.Ant;',
  17936. ' o.Ant:=v;',
  17937. ' o.Ant:=o.Ant;',
  17938. ' v:=o.Bird;',
  17939. ' o.Bird:=v;',
  17940. ' o.Bird:=o.Bird;',
  17941. ' v:=o.Items[1];',
  17942. ' o.Items[2]:=v;',
  17943. ' o.Items[3]:=o.Items[4];',
  17944. ' v:=o[5];',
  17945. ' o[6]:=v;',
  17946. ' o[7]:=o[8];',
  17947. 'end;',
  17948. 'begin',
  17949. '']);
  17950. ConvertProgram;
  17951. CheckSource('TestClassInterface_COM_Property',
  17952. LinesToStr([ // statements
  17953. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17954. 'rtl.createClass($mod, "TObject", null, function () {',
  17955. ' this.$init = function () {',
  17956. ' this.FAnt = null;',
  17957. ' };',
  17958. ' this.$final = function () {',
  17959. ' this.FAnt = undefined;',
  17960. ' };',
  17961. ' rtl.addIntf(this, $mod.IUnknown);',
  17962. '});',
  17963. 'this.DoIt = function () {',
  17964. ' var o = null;',
  17965. ' var v = null;',
  17966. ' var $ir = rtl.createIntfRefs();',
  17967. ' try {',
  17968. ' v = rtl.setIntfL(v, o.FAnt);',
  17969. ' rtl.setIntfP(o, "FAnt", v);',
  17970. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  17971. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  17972. ' o.SetBird(v);',
  17973. ' o.SetBird($ir.ref(1, o.GetBird()));',
  17974. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  17975. ' o.SetItems(2, v);',
  17976. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  17977. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  17978. ' o.SetItems(6, v);',
  17979. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  17980. ' } finally {',
  17981. ' $ir.free();',
  17982. ' rtl._Release(v);',
  17983. ' };',
  17984. '};',
  17985. '']),
  17986. LinesToStr([ // $mod.$main
  17987. '']));
  17988. end;
  17989. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  17990. begin
  17991. StartProgram(false);
  17992. Add([
  17993. '{$interfaces com}',
  17994. 'type',
  17995. ' IUnknown = interface',
  17996. ' function _AddRef: longint;',
  17997. ' function _Release: longint;',
  17998. ' function GetBird: IUnknown;',
  17999. ' procedure SetBird(Value: IUnknown);',
  18000. ' function GetItems(Index: longint): IUnknown;',
  18001. ' procedure SetItems(Index: longint; Value: IUnknown);',
  18002. ' property Bird: IUnknown read GetBird write SetBird;',
  18003. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18004. ' end;',
  18005. ' TObject = class(IUnknown)',
  18006. ' function _AddRef: longint; virtual; abstract;',
  18007. ' function _Release: longint; virtual; abstract;',
  18008. ' function GetBird: IUnknown; virtual; abstract;',
  18009. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18010. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18011. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18012. ' end;',
  18013. 'procedure DoIt;',
  18014. 'var',
  18015. ' o: TObject;',
  18016. ' v: IUnknown;',
  18017. 'begin',
  18018. ' v:=v.Items[1];',
  18019. ' v.Items[2]:=v;',
  18020. ' v.Items[3]:=v.Items[4];',
  18021. ' v:=v[5];',
  18022. ' v[6]:=v;',
  18023. ' v[7]:=v[8];',
  18024. ' v[9].Bird.Bird:=v;',
  18025. ' v:=v.Bird[10].Bird',
  18026. 'end;',
  18027. 'begin',
  18028. '']);
  18029. ConvertProgram;
  18030. CheckSource('TestClassInterface_COM_IntfProperty',
  18031. LinesToStr([ // statements
  18032. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  18033. ' "_AddRef",',
  18034. ' "_Release",',
  18035. ' "GetBird",',
  18036. ' "SetBird",',
  18037. ' "GetItems",',
  18038. ' "SetItems"',
  18039. '], null);',
  18040. 'rtl.createClass($mod, "TObject", null, function () {',
  18041. ' this.$init = function () {',
  18042. ' };',
  18043. ' this.$final = function () {',
  18044. ' };',
  18045. ' rtl.addIntf(this, $mod.IUnknown);',
  18046. '});',
  18047. 'this.DoIt = function () {',
  18048. ' var o = null;',
  18049. ' var v = null;',
  18050. ' var $ir = rtl.createIntfRefs();',
  18051. ' try {',
  18052. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  18053. ' v.SetItems(2, v);',
  18054. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  18055. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  18056. ' v.SetItems(6, v);',
  18057. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  18058. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  18059. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  18060. ' } finally {',
  18061. ' $ir.free();',
  18062. ' rtl._Release(v);',
  18063. ' };',
  18064. '};',
  18065. '']),
  18066. LinesToStr([ // $mod.$main
  18067. '']));
  18068. end;
  18069. procedure TTestModule.TestClassInterface_COM_Delegation;
  18070. begin
  18071. StartProgram(false);
  18072. Add([
  18073. '{$interfaces com}',
  18074. 'type',
  18075. ' IUnknown = interface',
  18076. ' function _AddRef: longint;',
  18077. ' function _Release: longint;',
  18078. ' end;',
  18079. ' IBird = interface(IUnknown)',
  18080. ' procedure Fly(s: string);',
  18081. ' end;',
  18082. ' IEagle = interface(IBird) end;',
  18083. ' IDove = interface(IBird) end;',
  18084. ' ISwallow = interface(IBird) end;',
  18085. ' TObject = class',
  18086. ' end;',
  18087. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18088. ' function _AddRef: longint; virtual; abstract;',
  18089. ' function _Release: longint; virtual; abstract;',
  18090. ' procedure Fly(s: string); virtual; abstract;',
  18091. ' end;',
  18092. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18093. ' function _AddRef: longint; virtual; abstract;',
  18094. ' function _Release: longint; virtual; abstract;',
  18095. ' FBirdIntf: IBird;',
  18096. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18097. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18098. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18099. ' FDoveObj: TBird;',
  18100. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18101. ' function GetSwallowObj: TBird; virtual; abstract;',
  18102. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18103. ' end;',
  18104. 'begin',
  18105. '']);
  18106. ConvertProgram;
  18107. CheckSource('TestClassInterface_COM_Delegation',
  18108. LinesToStr([ // statements
  18109. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18110. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  18111. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  18112. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  18113. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  18114. 'rtl.createClass($mod, "TObject", null, function () {',
  18115. ' this.$init = function () {',
  18116. ' };',
  18117. ' this.$final = function () {',
  18118. ' };',
  18119. '});',
  18120. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18121. ' rtl.addIntf(this, $mod.IBird);',
  18122. ' rtl.addIntf(this, $mod.IEagle);',
  18123. ' rtl.addIntf(this, $mod.IDove);',
  18124. ' rtl.addIntf(this, $mod.ISwallow);',
  18125. '});',
  18126. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18127. ' this.$init = function () {',
  18128. ' $mod.TObject.$init.call(this);',
  18129. ' this.FBirdIntf = null;',
  18130. ' this.FDoveObj = null;',
  18131. ' };',
  18132. ' this.$final = function () {',
  18133. ' this.FBirdIntf = undefined;',
  18134. ' this.FDoveObj = undefined;',
  18135. ' $mod.TObject.$final.call(this);',
  18136. ' };',
  18137. ' this.$intfmaps = {',
  18138. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  18139. ' return rtl._AddRef(this.FBirdIntf);',
  18140. ' },',
  18141. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  18142. ' return this.GetEagleIntf();',
  18143. ' },',
  18144. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  18145. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  18146. ' },',
  18147. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  18148. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18149. ' }',
  18150. ' };',
  18151. '});',
  18152. '']),
  18153. LinesToStr([ // $mod.$main
  18154. '']));
  18155. end;
  18156. procedure TTestModule.TestClassInterface_COM_With;
  18157. begin
  18158. StartProgram(false);
  18159. Add([
  18160. '{$interfaces com}',
  18161. 'type',
  18162. ' IUnknown = interface',
  18163. ' function _AddRef: longint;',
  18164. ' function _Release: longint;',
  18165. ' function GetAnt: IUnknown;',
  18166. ' property Ant: IUnknown read GetAnt;',
  18167. ' end;',
  18168. ' TObject = class(IUnknown)',
  18169. ' function _AddRef: longint; virtual; abstract;',
  18170. ' function _Release: longint; virtual; abstract;',
  18171. ' function GetAnt: IUnknown; virtual; abstract;',
  18172. ' property Ant: IUnknown read GetAnt;',
  18173. ' end;',
  18174. 'procedure DoIt;',
  18175. 'var',
  18176. ' i: IUnknown;',
  18177. 'begin',
  18178. ' with i do ',
  18179. ' GetAnt;',
  18180. ' with i.Ant, Ant do ',
  18181. ' GetAnt;',
  18182. 'end;',
  18183. 'begin',
  18184. '']);
  18185. ConvertProgram;
  18186. CheckSource('TestClassInterface_COM_With',
  18187. LinesToStr([ // statements
  18188. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  18189. 'rtl.createClass($mod, "TObject", null, function () {',
  18190. ' this.$init = function () {',
  18191. ' };',
  18192. ' this.$final = function () {',
  18193. ' };',
  18194. ' rtl.addIntf(this, $mod.IUnknown);',
  18195. '});',
  18196. 'this.DoIt = function () {',
  18197. ' var i = null;',
  18198. ' var $ir = rtl.createIntfRefs();',
  18199. ' try {',
  18200. ' $ir.ref(1, i.GetAnt());',
  18201. ' var $with1 = $ir.ref(2, i.GetAnt());',
  18202. ' var $with2 = $ir.ref(3, $with1.GetAnt());',
  18203. ' $ir.ref(4, $with2.GetAnt());',
  18204. ' } finally {',
  18205. ' $ir.free();',
  18206. ' };',
  18207. '};',
  18208. '']),
  18209. LinesToStr([ // $mod.$main
  18210. '']));
  18211. end;
  18212. procedure TTestModule.TestClassInterface_COM_ForIn;
  18213. begin
  18214. StartProgram(false);
  18215. Add([
  18216. '{$interfaces com}',
  18217. 'type',
  18218. ' IUnknown = interface end;',
  18219. ' TObject = class',
  18220. ' Id: longint;',
  18221. ' end;',
  18222. ' IEnumerator = interface(IUnknown)',
  18223. ' function GetCurrent: TObject;',
  18224. ' function MoveNext: Boolean;',
  18225. ' property Current: TObject read GetCurrent;',
  18226. ' end;',
  18227. ' IEnumerable = interface(IUnknown)',
  18228. ' function GetEnumerator: IEnumerator;',
  18229. ' end;',
  18230. 'var',
  18231. ' o: TObject;',
  18232. ' i: IEnumerable;',
  18233. 'begin',
  18234. ' for o in i do o.Id:=3;',
  18235. '']);
  18236. ConvertProgram;
  18237. CheckSource('TestClassInterface_COM_ForIn',
  18238. LinesToStr([ // statements
  18239. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18240. 'rtl.createClass($mod, "TObject", null, function () {',
  18241. ' this.$init = function () {',
  18242. ' this.Id = 0;',
  18243. ' };',
  18244. ' this.$final = function () {',
  18245. ' };',
  18246. '});',
  18247. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  18248. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  18249. 'this.o = null;',
  18250. 'this.i = null;',
  18251. '']),
  18252. LinesToStr([ // $mod.$main
  18253. 'var $in1 = $mod.i.GetEnumerator();',
  18254. 'try {',
  18255. ' while ($in1.MoveNext()) {',
  18256. ' $mod.o = $in1.GetCurrent();',
  18257. ' $mod.o.Id = 3;',
  18258. ' }',
  18259. '} finally {',
  18260. ' rtl._Release($in1)',
  18261. '};',
  18262. '']));
  18263. end;
  18264. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  18265. begin
  18266. StartProgram(false);
  18267. Add([
  18268. '{$interfaces com}',
  18269. 'type',
  18270. ' IUnknown = interface',
  18271. ' function _AddRef: longint;',
  18272. ' function _Release: longint;',
  18273. ' end;',
  18274. ' TObject = class',
  18275. ' end;',
  18276. ' TArrOfIntf = array of IUnknown;',
  18277. 'begin',
  18278. '']);
  18279. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  18280. ConvertProgram;
  18281. end;
  18282. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  18283. begin
  18284. StartProgram(false);
  18285. Add([
  18286. '{$interfaces com}',
  18287. 'type',
  18288. ' IUnknown = interface',
  18289. ' function _AddRef: longint;',
  18290. ' function _Release: longint;',
  18291. ' end;',
  18292. ' TRec = record',
  18293. ' i: IUnknown;',
  18294. ' end;',
  18295. 'begin',
  18296. '']);
  18297. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  18298. ConvertProgram;
  18299. end;
  18300. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  18301. begin
  18302. StartUnit(false);
  18303. Add([
  18304. '{$interfaces com}',
  18305. 'interface',
  18306. 'implementation',
  18307. 'type',
  18308. ' IUnknown = interface',
  18309. ' function _AddRef: longint;',
  18310. ' end;',
  18311. ' TObject = class(IUnknown)',
  18312. ' function _AddRef: longint;',
  18313. ' end;',
  18314. 'function TObject._AddRef: longint; begin end;',
  18315. 'var i: IUnknown;',
  18316. ' o: TObject;',
  18317. 'initialization',
  18318. ' i:=nil;',
  18319. ' i:=i;',
  18320. ' i:=o;',
  18321. ' if (o as IUnknown)=nil then ;',
  18322. '']);
  18323. ConvertUnit;
  18324. CheckSource('TestClassInterface_COM_UnitInitialization',
  18325. LinesToStr([ // statements
  18326. 'var $impl = $mod.$impl;',
  18327. '']),
  18328. LinesToStr([ // this.$init
  18329. 'var $ir = rtl.createIntfRefs();',
  18330. 'try {',
  18331. ' rtl.setIntfP($impl, "i", null);',
  18332. ' rtl.setIntfP($impl, "i", $impl.i);',
  18333. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  18334. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  18335. '} finally {',
  18336. ' $ir.free();',
  18337. '};',
  18338. '']),
  18339. LinesToStr([ // implementation
  18340. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  18341. 'rtl.createClass($impl, "TObject", null, function () {',
  18342. ' this.$init = function () {',
  18343. ' };',
  18344. ' this.$final = function () {',
  18345. ' };',
  18346. ' this._AddRef = function () {',
  18347. ' var Result = 0;',
  18348. ' return Result;',
  18349. ' };',
  18350. ' rtl.addIntf(this, $impl.IUnknown);',
  18351. '});',
  18352. '$impl.i = null;',
  18353. '$impl.o = null;',
  18354. ''])
  18355. );
  18356. end;
  18357. procedure TTestModule.TestClassInterface_GUID;
  18358. begin
  18359. StartProgram(false);
  18360. Add([
  18361. '{$interfaces corba}',
  18362. 'type',
  18363. ' IUnknown = interface',
  18364. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  18365. ' end;',
  18366. ' TObject = class end;',
  18367. ' TGUID = record D1, D2, D3, D4: word; end;',
  18368. ' TAliasGUID = TGUID;',
  18369. ' TGUIDString = type string;',
  18370. ' TAliasGUIDString = TGUIDString;',
  18371. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  18372. 'begin end;',
  18373. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  18374. 'begin end;',
  18375. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  18376. 'begin end;',
  18377. 'var',
  18378. ' i: IUnknown;',
  18379. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  18380. ' s: TAliasGUIDString;',
  18381. 'begin',
  18382. ' DoConstGUIDIt(IUnknown);',
  18383. ' DoDefGUID(IUnknown);',
  18384. ' DoStr(IUnknown);',
  18385. ' DoConstGUIDIt(i);',
  18386. ' DoDefGUID(i);',
  18387. ' DoStr(i);',
  18388. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  18389. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  18390. ' DoStr(g);',
  18391. ' g:=i;',
  18392. ' g:=IUnknown;',
  18393. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  18394. ' s:=i;',
  18395. ' s:=IUnknown;',
  18396. ' s:=g;',
  18397. ' if g=i then ;',
  18398. ' if i=g then ;',
  18399. ' if g=IUnknown then ;',
  18400. ' if IUnknown=g then ;',
  18401. ' if s=i then ;',
  18402. ' if i=s then ;',
  18403. ' if s=IUnknown then ;',
  18404. ' if IUnknown=s then ;',
  18405. ' if s=g then ;',
  18406. ' if g=s then ;',
  18407. '']);
  18408. ConvertProgram;
  18409. CheckSource('TestClassInterface_GUID',
  18410. LinesToStr([ // statements
  18411. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  18412. 'rtl.createClass($mod, "TObject", null, function () {',
  18413. ' this.$init = function () {',
  18414. ' };',
  18415. ' this.$final = function () {',
  18416. ' };',
  18417. '});',
  18418. 'rtl.recNewT($mod, "TGUID", function () {',
  18419. ' this.D1 = 0;',
  18420. ' this.D2 = 0;',
  18421. ' this.D3 = 0;',
  18422. ' this.D4 = 0;',
  18423. ' this.$eq = function (b) {',
  18424. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  18425. ' };',
  18426. ' this.$assign = function (s) {',
  18427. ' this.D1 = s.D1;',
  18428. ' this.D2 = s.D2;',
  18429. ' this.D3 = s.D3;',
  18430. ' this.D4 = s.D4;',
  18431. ' return this;',
  18432. ' };',
  18433. '});',
  18434. 'this.DoConstGUIDIt = function (g) {',
  18435. '};',
  18436. 'this.DoDefGUID = function (g) {',
  18437. '};',
  18438. 'this.DoStr = function (s) {',
  18439. '};',
  18440. 'this.i = null;',
  18441. 'this.g = $mod.TGUID.$clone({',
  18442. ' D1: 0xD91C9AF4,',
  18443. ' D2: 0x3C93,',
  18444. ' D3: 0x420F,',
  18445. ' D4: [',
  18446. ' 0xA3,',
  18447. ' 0x03,',
  18448. ' 0xBF,',
  18449. ' 0x5B,',
  18450. ' 0xA8,',
  18451. ' 0x2B,',
  18452. ' 0xFD,',
  18453. ' 0x23',
  18454. ' ]',
  18455. '});',
  18456. 'this.s = "";',
  18457. '']),
  18458. LinesToStr([ // $mod.$main
  18459. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  18460. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  18461. '$mod.DoStr($mod.IUnknown.$guid);',
  18462. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  18463. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  18464. '$mod.DoStr($mod.i.$guid);',
  18465. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  18466. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  18467. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  18468. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  18469. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  18470. '$mod.g.$assign({',
  18471. ' D1: 0xD91C9AF4,',
  18472. ' D2: 0x3C93,',
  18473. ' D3: 0x420F,',
  18474. ' D4: [',
  18475. ' 0xA3,',
  18476. ' 0x03,',
  18477. ' 0xBF,',
  18478. ' 0x5B,',
  18479. ' 0xA8,',
  18480. ' 0x2B,',
  18481. ' 0xFD,',
  18482. ' 0x23',
  18483. ' ]',
  18484. '});',
  18485. '$mod.s = $mod.i.$guid;',
  18486. '$mod.s = $mod.IUnknown.$guid;',
  18487. '$mod.s = rtl.guidrToStr($mod.g);',
  18488. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  18489. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  18490. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  18491. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  18492. 'if ($mod.s === $mod.i.$guid) ;',
  18493. 'if ($mod.i.$guid === $mod.s) ;',
  18494. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  18495. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  18496. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  18497. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  18498. '']));
  18499. end;
  18500. procedure TTestModule.TestClassInterface_GUIDProperty;
  18501. begin
  18502. StartProgram(false);
  18503. Add([
  18504. '{$interfaces corba}',
  18505. 'type',
  18506. ' IUnknown = interface',
  18507. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  18508. ' end;',
  18509. ' TGUID = record D1, D2, D3, D4: word; end;',
  18510. ' TAliasGUID = TGUID;',
  18511. ' TGUIDString = type string;',
  18512. ' TAliasGUIDString = TGUIDString;',
  18513. ' TObject = class',
  18514. ' function GetG: TAliasGUID; virtual; abstract;',
  18515. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  18516. ' function GetS: TAliasGUIDString; virtual; abstract;',
  18517. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  18518. ' property g: TAliasGUID read GetG write SetG;',
  18519. ' property s: TAliasGUIDString read GetS write SetS;',
  18520. ' end;',
  18521. 'var o: TObject;',
  18522. 'begin',
  18523. ' o.g:=IUnknown;',
  18524. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  18525. ' o.s:=IUnknown;',
  18526. ' o.s:=o.g;',
  18527. '']);
  18528. ConvertProgram;
  18529. CheckSource('TestClassInterface_GUIDProperty',
  18530. LinesToStr([ // statements
  18531. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  18532. 'rtl.recNewT($mod, "TGUID", function () {',
  18533. ' this.D1 = 0;',
  18534. ' this.D2 = 0;',
  18535. ' this.D3 = 0;',
  18536. ' this.D4 = 0;',
  18537. ' this.$eq = function (b) {',
  18538. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  18539. ' };',
  18540. ' this.$assign = function (s) {',
  18541. ' this.D1 = s.D1;',
  18542. ' this.D2 = s.D2;',
  18543. ' this.D3 = s.D3;',
  18544. ' this.D4 = s.D4;',
  18545. ' return this;',
  18546. ' };',
  18547. '});',
  18548. 'rtl.createClass($mod, "TObject", null, function () {',
  18549. ' this.$init = function () {',
  18550. ' };',
  18551. ' this.$final = function () {',
  18552. ' };',
  18553. '});',
  18554. 'this.o = null;',
  18555. '']),
  18556. LinesToStr([ // $mod.$main
  18557. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  18558. '$mod.o.SetG({',
  18559. ' D1: 0xD91C9AF4,',
  18560. ' D2: 0x3C93,',
  18561. ' D3: 0x420F,',
  18562. ' D4: [',
  18563. ' 0xA3,',
  18564. ' 0x03,',
  18565. ' 0xBF,',
  18566. ' 0x5B,',
  18567. ' 0xA8,',
  18568. ' 0x2B,',
  18569. ' 0xFD,',
  18570. ' 0x23',
  18571. ' ]',
  18572. '});',
  18573. '$mod.o.SetS($mod.IUnknown.$guid);',
  18574. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  18575. '']));
  18576. end;
  18577. procedure TTestModule.TestClassHelper_ClassVar;
  18578. begin
  18579. StartProgram(false);
  18580. Add([
  18581. 'type',
  18582. ' TObject = class',
  18583. ' end;',
  18584. ' THelper = class helper for TObject',
  18585. ' const',
  18586. ' One = 1;',
  18587. ' Two: word = 2;',
  18588. ' class var',
  18589. ' Glob: word;',
  18590. ' function Foo(w: word): word;',
  18591. ' class function Bar(w: word): word;',
  18592. ' end;',
  18593. 'function THelper.foo(w: word): word;',
  18594. 'begin',
  18595. ' Result:=w;',
  18596. ' Two:=One+w;',
  18597. ' Glob:=Glob;',
  18598. ' Result:=Self.Glob;',
  18599. ' Self.Glob:=Self.Glob;',
  18600. ' with Self do Glob:=Glob;',
  18601. 'end;',
  18602. 'class function THelper.bar(w: word): word;',
  18603. 'begin',
  18604. ' Result:=w;',
  18605. ' Two:=One;',
  18606. ' Glob:=Glob;',
  18607. ' Self.Glob:=Self.Glob;',
  18608. ' with Self do Glob:=Glob;',
  18609. 'end;',
  18610. 'var o: TObject;',
  18611. 'begin',
  18612. ' tobject.two:=tobject.one;',
  18613. ' tobject.Glob:=tobject.Glob;',
  18614. ' with tobject do begin',
  18615. ' two:=one;',
  18616. ' Glob:=Glob;',
  18617. ' end;',
  18618. ' o.two:=o.one;',
  18619. ' o.Glob:=o.Glob;',
  18620. ' with o do begin',
  18621. ' two:=one;',
  18622. ' Glob:=Glob;',
  18623. ' end;',
  18624. '']);
  18625. ConvertProgram;
  18626. CheckSource('TestClassHelper_ClassVar',
  18627. LinesToStr([ // statements
  18628. 'rtl.createClass($mod, "TObject", null, function () {',
  18629. ' this.$init = function () {',
  18630. ' };',
  18631. ' this.$final = function () {',
  18632. ' };',
  18633. '});',
  18634. 'rtl.createHelper($mod, "THelper", null, function () {',
  18635. ' this.One = 1;',
  18636. ' this.Two = 2;',
  18637. ' this.Glob = 0;',
  18638. ' this.Foo = function (w) {',
  18639. ' var Result = 0;',
  18640. ' Result = w;',
  18641. ' $mod.THelper.Two = 1 + w;',
  18642. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18643. ' Result = $mod.THelper.Glob;',
  18644. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18645. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18646. ' return Result;',
  18647. ' };',
  18648. ' this.Bar = function (w) {',
  18649. ' var Result = 0;',
  18650. ' Result = w;',
  18651. ' $mod.THelper.Two = 1;',
  18652. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18653. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18654. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18655. ' return Result;',
  18656. ' };',
  18657. '});',
  18658. 'this.o = null;',
  18659. '']),
  18660. LinesToStr([ // $mod.$main
  18661. '$mod.THelper.Two = 1;',
  18662. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18663. 'var $with1 = $mod.TObject;',
  18664. '$mod.THelper.Two = 1;',
  18665. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18666. '$mod.THelper.Two = 1;',
  18667. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18668. 'var $with2 = $mod.o;',
  18669. '$mod.THelper.Two = 1;',
  18670. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18671. '']));
  18672. end;
  18673. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  18674. begin
  18675. StartProgram(false);
  18676. Add([
  18677. 'type',
  18678. ' TObject = class',
  18679. ' FSize: word;',
  18680. ' property Size: word read FSize write FSize;',
  18681. ' end;',
  18682. ' THelper = class helper for TObject',
  18683. ' function Foo(w: word = 1): word;',
  18684. ' end;',
  18685. 'function THelper.foo(w: word): word;',
  18686. 'begin',
  18687. ' Result:=Size;',
  18688. ' Size:=Size+2;',
  18689. ' Self.Size:=Self.Size+3;',
  18690. ' FSize:=FSize+4;',
  18691. ' Self.FSize:=Self.FSize+5;',
  18692. ' with Self do begin',
  18693. ' Size:=Size+6;',
  18694. ' FSize:=FSize+7;',
  18695. ' FSize:=FSize+8;',
  18696. ' end;',
  18697. 'end;',
  18698. 'begin',
  18699. '']);
  18700. ConvertProgram;
  18701. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  18702. LinesToStr([ // statements
  18703. 'rtl.createClass($mod, "TObject", null, function () {',
  18704. ' this.$init = function () {',
  18705. ' this.FSize = 0;',
  18706. ' };',
  18707. ' this.$final = function () {',
  18708. ' };',
  18709. '});',
  18710. 'rtl.createHelper($mod, "THelper", null, function () {',
  18711. ' this.Foo = function (w) {',
  18712. ' var Result = 0;',
  18713. ' Result = this.FSize;',
  18714. ' this.FSize = this.FSize + 2;',
  18715. ' this.FSize = this.FSize + 3;',
  18716. ' this.FSize = this.FSize + 4;',
  18717. ' this.FSize = this.FSize + 5;',
  18718. ' this.FSize = this.FSize + 6;',
  18719. ' this.FSize = this.FSize + 7;',
  18720. ' this.FSize = this.FSize + 8;',
  18721. ' return Result;',
  18722. ' };',
  18723. '});',
  18724. '']),
  18725. LinesToStr([ // $mod.$main
  18726. '']));
  18727. end;
  18728. procedure TTestModule.TestClassHelper_Method_Call;
  18729. begin
  18730. StartProgram(false);
  18731. Add([
  18732. 'type',
  18733. ' TObject = class',
  18734. ' procedure Run(w: word = 10);',
  18735. ' end;',
  18736. ' THelper = class helper for TObject',
  18737. ' function Foo(w: word = 1): word;',
  18738. ' end;',
  18739. 'procedure TObject.Run(w: word);',
  18740. 'begin',
  18741. ' Foo;',
  18742. ' Foo();',
  18743. ' Foo(2);',
  18744. ' Self.Foo;',
  18745. ' Self.Foo();',
  18746. ' Self.Foo(3);',
  18747. ' with Self do begin',
  18748. ' Foo;',
  18749. ' Foo();',
  18750. ' Foo(4);',
  18751. ' end;',
  18752. 'end;',
  18753. 'function THelper.foo(w: word): word;',
  18754. 'begin',
  18755. ' Run;',
  18756. ' Run();',
  18757. ' Run(11);',
  18758. ' Foo;',
  18759. ' Foo();',
  18760. ' Foo(12);',
  18761. ' Self.Foo;',
  18762. ' Self.Foo();',
  18763. ' Self.Foo(13);',
  18764. ' with Self do begin',
  18765. ' Foo;',
  18766. ' Foo();',
  18767. ' Foo(14);',
  18768. ' end;',
  18769. 'end;',
  18770. 'var Obj: TObject;',
  18771. 'begin',
  18772. ' obj.Foo;',
  18773. ' obj.Foo();',
  18774. ' obj.Foo(21);',
  18775. ' with obj do begin',
  18776. ' Foo;',
  18777. ' Foo();',
  18778. ' Foo(22);',
  18779. ' end;',
  18780. '']);
  18781. ConvertProgram;
  18782. CheckSource('TestClassHelper_Method_Call',
  18783. LinesToStr([ // statements
  18784. 'rtl.createClass($mod, "TObject", null, function () {',
  18785. ' this.$init = function () {',
  18786. ' };',
  18787. ' this.$final = function () {',
  18788. ' };',
  18789. ' this.Run = function (w) {',
  18790. ' $mod.THelper.Foo.call(this, 1);',
  18791. ' $mod.THelper.Foo.call(this, 1);',
  18792. ' $mod.THelper.Foo.call(this, 2);',
  18793. ' $mod.THelper.Foo.call(this, 1);',
  18794. ' $mod.THelper.Foo.call(this, 1);',
  18795. ' $mod.THelper.Foo.call(this, 3);',
  18796. ' $mod.THelper.Foo.call(this, 1);',
  18797. ' $mod.THelper.Foo.call(this, 1);',
  18798. ' $mod.THelper.Foo.call(this, 4);',
  18799. ' };',
  18800. '});',
  18801. 'rtl.createHelper($mod, "THelper", null, function () {',
  18802. ' this.Foo = function (w) {',
  18803. ' var Result = 0;',
  18804. ' this.Run(10);',
  18805. ' this.Run(10);',
  18806. ' this.Run(11);',
  18807. ' $mod.THelper.Foo.call(this, 1);',
  18808. ' $mod.THelper.Foo.call(this, 1);',
  18809. ' $mod.THelper.Foo.call(this, 12);',
  18810. ' $mod.THelper.Foo.call(this, 1);',
  18811. ' $mod.THelper.Foo.call(this, 1);',
  18812. ' $mod.THelper.Foo.call(this, 13);',
  18813. ' $mod.THelper.Foo.call(this, 1);',
  18814. ' $mod.THelper.Foo.call(this, 1);',
  18815. ' $mod.THelper.Foo.call(this, 14);',
  18816. ' return Result;',
  18817. ' };',
  18818. '});',
  18819. 'this.Obj = null;',
  18820. '']),
  18821. LinesToStr([ // $mod.$main
  18822. '$mod.THelper.Foo.call($mod.Obj, 1);',
  18823. '$mod.THelper.Foo.call($mod.Obj, 1);',
  18824. '$mod.THelper.Foo.call($mod.Obj, 21);',
  18825. 'var $with1 = $mod.Obj;',
  18826. '$mod.THelper.Foo.call($with1, 1);',
  18827. '$mod.THelper.Foo.call($with1, 1);',
  18828. '$mod.THelper.Foo.call($with1, 22);',
  18829. '']));
  18830. end;
  18831. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  18832. begin
  18833. StartProgram(false);
  18834. Add([
  18835. 'type',
  18836. ' TObject = class',
  18837. ' procedure Run(w: word = 10);',
  18838. ' end;',
  18839. ' THelper = class helper for TObject',
  18840. ' function Foo(w: word = 1): word;',
  18841. ' end;',
  18842. 'procedure TObject.Run(w: word);',
  18843. ' procedure Sub(Self: TObject);',
  18844. ' begin',
  18845. ' Foo;',
  18846. ' Foo();',
  18847. ' Self.Foo;',
  18848. ' Self.Foo();',
  18849. ' with Self do begin',
  18850. ' Foo;',
  18851. ' Foo();',
  18852. ' end;',
  18853. ' end;',
  18854. 'begin',
  18855. 'end;',
  18856. 'function THelper.foo(w: word): word;',
  18857. ' procedure Sub(Self: TObject);',
  18858. ' begin',
  18859. ' Run;',
  18860. ' Run();',
  18861. ' Foo;',
  18862. ' Foo();',
  18863. ' Self.Foo;',
  18864. ' Self.Foo();',
  18865. ' with Self do begin',
  18866. ' Foo;',
  18867. ' Foo();',
  18868. ' end;',
  18869. ' end;',
  18870. 'begin',
  18871. 'end;',
  18872. 'begin',
  18873. '']);
  18874. ConvertProgram;
  18875. CheckSource('TestClassHelper_Method_Nested_Call',
  18876. LinesToStr([ // statements
  18877. 'rtl.createClass($mod, "TObject", null, function () {',
  18878. ' this.$init = function () {',
  18879. ' };',
  18880. ' this.$final = function () {',
  18881. ' };',
  18882. ' this.Run = function (w) {',
  18883. ' var $Self = this;',
  18884. ' function Sub(Self) {',
  18885. ' $mod.THelper.Foo.call($Self, 1);',
  18886. ' $mod.THelper.Foo.call($Self, 1);',
  18887. ' $mod.THelper.Foo.call(Self, 1);',
  18888. ' $mod.THelper.Foo.call(Self, 1);',
  18889. ' $mod.THelper.Foo.call(Self, 1);',
  18890. ' $mod.THelper.Foo.call($Self, 1);',
  18891. ' };',
  18892. ' };',
  18893. '});',
  18894. 'rtl.createHelper($mod, "THelper", null, function () {',
  18895. ' this.Foo = function (w) {',
  18896. ' var $Self = this;',
  18897. ' var Result = 0;',
  18898. ' function Sub(Self) {',
  18899. ' $Self.Run(10);',
  18900. ' $Self.Run(10);',
  18901. ' $mod.THelper.Foo.call($Self, 1);',
  18902. ' $mod.THelper.Foo.call($Self, 1);',
  18903. ' $mod.THelper.Foo.call(Self, 1);',
  18904. ' $mod.THelper.Foo.call(Self, 1);',
  18905. ' $mod.THelper.Foo.call(Self, 1);',
  18906. ' $mod.THelper.Foo.call($Self, 1);',
  18907. ' };',
  18908. ' return Result;',
  18909. ' };',
  18910. '});',
  18911. '']),
  18912. LinesToStr([ // $mod.$main
  18913. '']));
  18914. end;
  18915. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  18916. begin
  18917. StartProgram(false);
  18918. Add([
  18919. 'type',
  18920. ' TObject = class',
  18921. ' class procedure Run(w: word = 10);',
  18922. ' end;',
  18923. ' THelper = class helper for TObject',
  18924. ' class function Foo(w: word = 1): word;',
  18925. ' end;',
  18926. 'class procedure TObject.Run(w: word);',
  18927. 'begin',
  18928. ' Foo;',
  18929. ' Foo();',
  18930. ' Self.Foo;',
  18931. ' Self.Foo();',
  18932. ' with Self do begin',
  18933. ' Foo;',
  18934. ' Foo();',
  18935. ' end;',
  18936. 'end;',
  18937. 'class function THelper.foo(w: word): word;',
  18938. 'begin',
  18939. ' Run;',
  18940. ' Run();',
  18941. ' Foo;',
  18942. ' Foo();',
  18943. ' Self.Foo;',
  18944. ' Self.Foo();',
  18945. ' with Self do begin',
  18946. ' Foo;',
  18947. ' Foo();',
  18948. ' end;',
  18949. 'end;',
  18950. 'var',
  18951. ' Obj: TObject;',
  18952. 'begin',
  18953. ' obj.Foo;',
  18954. ' obj.Foo();',
  18955. ' with obj do begin',
  18956. ' Foo;',
  18957. ' Foo();',
  18958. ' end;',
  18959. ' tobject.Foo;',
  18960. ' tobject.Foo();',
  18961. ' with tobject do begin',
  18962. ' Foo;',
  18963. ' Foo();',
  18964. ' end;',
  18965. '']);
  18966. ConvertProgram;
  18967. CheckSource('TestClassHelper_ClassMethod_Call',
  18968. LinesToStr([ // statements
  18969. 'rtl.createClass($mod, "TObject", null, function () {',
  18970. ' this.$init = function () {',
  18971. ' };',
  18972. ' this.$final = function () {',
  18973. ' };',
  18974. ' this.Run = function (w) {',
  18975. ' $mod.THelper.Foo.call(this, 1);',
  18976. ' $mod.THelper.Foo.call(this, 1);',
  18977. ' $mod.THelper.Foo.call(this, 1);',
  18978. ' $mod.THelper.Foo.call(this, 1);',
  18979. ' $mod.THelper.Foo.call(this, 1);',
  18980. ' $mod.THelper.Foo.call(this, 1);',
  18981. ' };',
  18982. '});',
  18983. 'rtl.createHelper($mod, "THelper", null, function () {',
  18984. ' this.Foo = function (w) {',
  18985. ' var Result = 0;',
  18986. ' this.Run(10);',
  18987. ' this.Run(10);',
  18988. ' $mod.THelper.Foo.call(this, 1);',
  18989. ' $mod.THelper.Foo.call(this, 1);',
  18990. ' $mod.THelper.Foo.call(this, 1);',
  18991. ' $mod.THelper.Foo.call(this, 1);',
  18992. ' $mod.THelper.Foo.call(this, 1);',
  18993. ' $mod.THelper.Foo.call(this, 1);',
  18994. ' return Result;',
  18995. ' };',
  18996. '});',
  18997. 'this.Obj = null;',
  18998. '']),
  18999. LinesToStr([ // $mod.$main
  19000. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19001. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19002. 'var $with1 = $mod.Obj;',
  19003. '$mod.THelper.Foo.call($with1.$class, 1);',
  19004. '$mod.THelper.Foo.call($with1.$class, 1);',
  19005. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19006. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19007. 'var $with2 = $mod.TObject;',
  19008. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19009. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19010. '']));
  19011. end;
  19012. procedure TTestModule.TestClassHelper_ClassOf;
  19013. begin
  19014. StartProgram(false);
  19015. Add([
  19016. 'type',
  19017. ' TObject = class',
  19018. ' end;',
  19019. ' TClass = class of TObject;',
  19020. ' THelper = class helper for TObject',
  19021. ' class function Foo(w: word = 1): word;',
  19022. ' end;',
  19023. 'class function THelper.foo(w: word): word;',
  19024. 'begin',
  19025. 'end;',
  19026. 'var',
  19027. ' c: TClass;',
  19028. 'begin',
  19029. ' c.Foo;',
  19030. ' c.Foo();',
  19031. ' with c do begin',
  19032. ' Foo;',
  19033. ' Foo();',
  19034. ' end;',
  19035. '']);
  19036. ConvertProgram;
  19037. CheckSource('TestClassHelper_ClassOf',
  19038. LinesToStr([ // statements
  19039. 'rtl.createClass($mod, "TObject", null, function () {',
  19040. ' this.$init = function () {',
  19041. ' };',
  19042. ' this.$final = function () {',
  19043. ' };',
  19044. '});',
  19045. 'rtl.createHelper($mod, "THelper", null, function () {',
  19046. ' this.Foo = function (w) {',
  19047. ' var Result = 0;',
  19048. ' return Result;',
  19049. ' };',
  19050. '});',
  19051. 'this.c = null;',
  19052. '']),
  19053. LinesToStr([ // $mod.$main
  19054. '$mod.THelper.Foo.call($mod.c, 1);',
  19055. '$mod.THelper.Foo.call($mod.c, 1);',
  19056. 'var $with1 = $mod.c;',
  19057. '$mod.THelper.Foo.call($with1, 1);',
  19058. '$mod.THelper.Foo.call($with1, 1);',
  19059. '']));
  19060. end;
  19061. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  19062. begin
  19063. StartProgram(false);
  19064. Add([
  19065. '{$mode objfpc}',
  19066. 'type',
  19067. ' TObject = class',
  19068. ' procedure DoIt;',
  19069. ' end;',
  19070. ' THelper = class helper for TObject',
  19071. ' procedure Fly(w: word = 1);',
  19072. ' class procedure Glide(w: word = 1);',
  19073. ' class procedure Run(w: word = 1); static;',
  19074. ' end;',
  19075. ' TFly = procedure(w: word) of object;',
  19076. ' TGlide = TFly;',
  19077. ' TRun = procedure(w: word);',
  19078. 'var',
  19079. ' f: TFly;',
  19080. ' g: TGlide;',
  19081. ' r: TRun;',
  19082. 'procedure TObject.DoIt;',
  19083. 'begin',
  19084. ' f:=@fly;',
  19085. ' g:=@glide;',
  19086. ' r:=@run;',
  19087. ' f:[email protected];',
  19088. ' g:[email protected];',
  19089. ' r:[email protected];',
  19090. ' with self do begin',
  19091. ' f:=@fly;',
  19092. ' g:=@glide;',
  19093. ' r:=@run;',
  19094. ' end;',
  19095. 'end;',
  19096. 'procedure THelper.fly(w: word);',
  19097. 'begin',
  19098. ' f:=@fly;',
  19099. ' g:=@glide;',
  19100. ' r:=@run;',
  19101. 'end;',
  19102. 'class procedure THelper.glide(w: word);',
  19103. 'begin',
  19104. ' g:=@glide;',
  19105. ' r:=@run;',
  19106. 'end;',
  19107. 'class procedure THelper.run(w: word);',
  19108. 'begin',
  19109. ' g:=@glide;',
  19110. ' r:=@run;',
  19111. 'end;',
  19112. 'var',
  19113. ' Obj: TObject;',
  19114. 'begin',
  19115. ' f:[email protected];',
  19116. ' g:[email protected];',
  19117. ' r:[email protected];',
  19118. ' with obj do begin',
  19119. ' f:=@fly;',
  19120. ' g:=@glide;',
  19121. ' r:=@run;',
  19122. ' end;',
  19123. ' g:[email protected];',
  19124. ' r:[email protected];',
  19125. ' with tobject do begin',
  19126. ' g:=@glide;',
  19127. ' r:=@run;',
  19128. ' end;',
  19129. '']);
  19130. ConvertProgram;
  19131. CheckSource('TestClassHelper_MethodRefObjFPC',
  19132. LinesToStr([ // statements
  19133. 'rtl.createClass($mod, "TObject", null, function () {',
  19134. ' this.$init = function () {',
  19135. ' };',
  19136. ' this.$final = function () {',
  19137. ' };',
  19138. ' this.DoIt = function () {',
  19139. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19140. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19141. ' $mod.r = $mod.THelper.Run;',
  19142. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19143. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19144. ' $mod.r = $mod.THelper.Run;',
  19145. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19146. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19147. ' $mod.r = $mod.THelper.Run;',
  19148. ' };',
  19149. '});',
  19150. 'rtl.createHelper($mod, "THelper", null, function () {',
  19151. ' this.Fly = function (w) {',
  19152. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19153. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19154. ' $mod.r = $mod.THelper.Run;',
  19155. ' };',
  19156. ' this.Glide = function (w) {',
  19157. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  19158. ' $mod.r = $mod.THelper.Run;',
  19159. ' };',
  19160. ' this.Run = function (w) {',
  19161. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  19162. ' $mod.r = $mod.THelper.Run;',
  19163. ' };',
  19164. '});',
  19165. 'this.f = null;',
  19166. 'this.g = null;',
  19167. 'this.r = null;',
  19168. 'this.Obj = null;',
  19169. '']),
  19170. LinesToStr([ // $mod.$main
  19171. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  19172. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  19173. '$mod.r = $mod.THelper.Run;',
  19174. 'var $with1 = $mod.Obj;',
  19175. '$mod.f = rtl.createCallback($with1, $mod.THelper.Fly);',
  19176. '$mod.g = rtl.createCallback($with1.$class, $mod.THelper.Glide);',
  19177. '$mod.r = $mod.THelper.Run;',
  19178. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  19179. '$mod.r = $mod.THelper.Run;',
  19180. 'var $with2 = $mod.TObject;',
  19181. '$mod.g = rtl.createCallback($with2, $mod.THelper.Glide);',
  19182. '$mod.r = $mod.THelper.Run;',
  19183. '']));
  19184. end;
  19185. procedure TTestModule.TestClassHelper_Constructor;
  19186. begin
  19187. StartProgram(false);
  19188. Add([
  19189. 'type',
  19190. ' TObject = class',
  19191. ' constructor Create;',
  19192. ' end;',
  19193. ' TClass = class of TObject;',
  19194. ' THelper = class helper for TObject',
  19195. ' constructor NewHlp(w: word);',
  19196. ' end;',
  19197. 'var',
  19198. ' obj: TObject;',
  19199. ' c: TClass;',
  19200. 'constructor TObject.Create;',
  19201. 'begin',
  19202. ' NewHlp(2);', // normal call
  19203. ' tobject.NewHlp(3);', // new instance
  19204. ' c.newhlp(4);', // new instance
  19205. 'end;',
  19206. 'constructor THelper.NewHlp(w: word);',
  19207. 'begin',
  19208. ' create;', // normal call
  19209. ' tobject.create;', // new instance
  19210. ' NewHlp(2);', // normal call
  19211. ' tobject.NewHlp(3);', // new instance
  19212. ' c.newhlp(4);', // new instance
  19213. 'end;',
  19214. 'begin',
  19215. ' obj.newhlp(2);', // normal call
  19216. ' with Obj do newhlp(12);', // normal call
  19217. ' tobject.newhlp(3);', // new instance
  19218. ' with tobject do newhlp(13);', // new instance
  19219. ' c.newhlp(4);', // new instance
  19220. ' with c do newhlp(14);', // new instance
  19221. '']);
  19222. ConvertProgram;
  19223. CheckSource('TestClassHelper_Constructor',
  19224. LinesToStr([ // statements
  19225. 'rtl.createClass($mod, "TObject", null, function () {',
  19226. ' this.$init = function () {',
  19227. ' };',
  19228. ' this.$final = function () {',
  19229. ' };',
  19230. ' this.Create = function () {',
  19231. ' $mod.THelper.NewHlp.call(this, 2);',
  19232. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19233. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  19234. ' return this;',
  19235. ' };',
  19236. '});',
  19237. 'rtl.createHelper($mod, "THelper", null, function () {',
  19238. ' this.NewHlp = function (w) {',
  19239. ' this.Create();',
  19240. ' $mod.TObject.$create("Create");',
  19241. ' $mod.THelper.NewHlp.call(this, 2);',
  19242. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19243. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  19244. ' return this;',
  19245. ' };',
  19246. '});',
  19247. 'this.obj = null;',
  19248. 'this.c = null;',
  19249. '']),
  19250. LinesToStr([ // $mod.$main
  19251. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  19252. 'var $with1 = $mod.obj;',
  19253. '$mod.THelper.NewHlp.call($with1, 12);',
  19254. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19255. 'var $with2 = $mod.TObject;',
  19256. '$with2.$create($mod.THelper.NewHlp, [13]);',
  19257. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  19258. 'var $with3 = $mod.c;',
  19259. '$with3.$create($mod.THelper.NewHlp, [14]);',
  19260. '']));
  19261. end;
  19262. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  19263. begin
  19264. StartProgram(false);
  19265. Add([
  19266. 'type',
  19267. ' TObject = class',
  19268. ' procedure Fly;',
  19269. ' end;',
  19270. ' TObjHelper = class helper for TObject',
  19271. ' procedure Fly;',
  19272. ' end;',
  19273. ' TBird = class',
  19274. ' procedure Fly;',
  19275. ' end;',
  19276. ' TBirdHelper = class helper for TBird',
  19277. ' procedure Fly;',
  19278. ' procedure Walk(w: word);',
  19279. ' end;',
  19280. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  19281. ' procedure Fly;',
  19282. ' procedure Walk(w: word);',
  19283. ' end;',
  19284. 'procedure Tobject.fly;',
  19285. 'begin',
  19286. ' inherited;', // ignore
  19287. 'end;',
  19288. 'procedure Tobjhelper.fly;',
  19289. 'begin',
  19290. ' {@TObject_Fly}inherited;',
  19291. ' inherited {@TObject_Fly}Fly;',
  19292. 'end;',
  19293. 'procedure Tbird.fly;',
  19294. 'begin',
  19295. ' {@TObjHelper_Fly}inherited;',
  19296. ' inherited {@TObjHelper_Fly}Fly;',
  19297. 'end;',
  19298. 'procedure Tbirdhelper.fly;',
  19299. 'begin',
  19300. ' {@TBird_Fly}inherited;',
  19301. ' inherited {@TBird_Fly}Fly;',
  19302. 'end;',
  19303. 'procedure Tbirdhelper.walk(w: word);',
  19304. 'begin',
  19305. 'end;',
  19306. 'procedure teagleHelper.fly;',
  19307. 'begin',
  19308. ' {@TBird_Fly}inherited;',
  19309. ' inherited {@TBird_Fly}Fly;',
  19310. 'end;',
  19311. 'procedure teagleHelper.walk(w: word);',
  19312. 'begin',
  19313. ' {@TBirdHelper_Walk}inherited;',
  19314. ' inherited {@TBirdHelper_Walk}Walk(3);',
  19315. 'end;',
  19316. 'begin',
  19317. '']);
  19318. ConvertProgram;
  19319. CheckSource('TestClassHelper_InheritedObjFPC',
  19320. LinesToStr([ // statements
  19321. 'rtl.createClass($mod, "TObject", null, function () {',
  19322. ' this.$init = function () {',
  19323. ' };',
  19324. ' this.$final = function () {',
  19325. ' };',
  19326. ' this.Fly = function () {',
  19327. ' };',
  19328. '});',
  19329. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19330. ' this.Fly = function () {',
  19331. ' $mod.TObject.Fly.call(this);',
  19332. ' $mod.TObject.Fly.call(this);',
  19333. ' };',
  19334. '});',
  19335. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19336. ' this.Fly$1 = function () {',
  19337. ' $mod.TObjHelper.Fly.call(this);',
  19338. ' $mod.TObjHelper.Fly.call(this);',
  19339. ' };',
  19340. '});',
  19341. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  19342. ' this.Fly = function () {',
  19343. ' $mod.TBird.Fly$1.call(this);',
  19344. ' $mod.TBird.Fly$1.call(this);',
  19345. ' };',
  19346. ' this.Walk = function (w) {',
  19347. ' };',
  19348. '});',
  19349. 'rtl.createHelper($mod, "TEagleHelper", $mod.TBirdHelper, function () {',
  19350. ' this.Fly$1 = function () {',
  19351. ' $mod.TBird.Fly$1.call(this);',
  19352. ' $mod.TBird.Fly$1.call(this);',
  19353. ' };',
  19354. ' this.Walk$1 = function (w) {',
  19355. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  19356. ' $mod.TBirdHelper.Walk.call(this, 3);',
  19357. ' };',
  19358. '});',
  19359. '']),
  19360. LinesToStr([ // $mod.$main
  19361. '']));
  19362. end;
  19363. procedure TTestModule.TestClassHelper_Property;
  19364. begin
  19365. StartProgram(false);
  19366. Add([
  19367. 'type',
  19368. ' TObject = class',
  19369. ' FSize: word;',
  19370. ' function GetSpeed: word;',
  19371. ' procedure SetSpeed(Value: word);',
  19372. ' end;',
  19373. ' TObjHelper = class helper for TObject',
  19374. ' function GetLeft: word;',
  19375. ' procedure SetLeft(Value: word);',
  19376. ' property Size: word read FSize write FSize;',
  19377. ' property Speed: word read GetSpeed write SetSpeed;',
  19378. ' property Left: word read GetLeft write SetLeft;',
  19379. ' end;',
  19380. ' TBird = class',
  19381. ' property NotRight: word read GetLeft write SetLeft;',
  19382. ' procedure DoIt;',
  19383. ' end;',
  19384. 'var',
  19385. ' b: TBird;',
  19386. 'function Tobject.GetSpeed: word;',
  19387. 'begin',
  19388. ' Size:=Size+11;',
  19389. ' Speed:=Speed+12;',
  19390. ' Result:=Left+13;',
  19391. ' Left:=13;',
  19392. ' Left:=Left+13;',
  19393. ' Self.Size:=Self.Size+21;',
  19394. ' Self.Speed:=Self.Speed+22;',
  19395. ' Self.Left:=Self.Left+23;',
  19396. ' with Self do begin',
  19397. ' Size:=Size+31;',
  19398. ' Speed:=Speed+32;',
  19399. ' Left:=Left+33;',
  19400. ' end;',
  19401. 'end;',
  19402. 'procedure Tobject.SetSpeed(Value: word);',
  19403. 'begin',
  19404. 'end;',
  19405. 'function TObjHelper.GetLeft: word;',
  19406. 'begin',
  19407. ' Size:=Size+11;',
  19408. ' Speed:=Speed+12;',
  19409. ' Left:=Left+13;',
  19410. ' Self.Size:=Self.Size+21;',
  19411. ' Self.Speed:=Self.Speed+22;',
  19412. ' Self.Left:=Self.Left+23;',
  19413. ' with Self do begin',
  19414. ' Size:=Size+31;',
  19415. ' Speed:=Speed+32;',
  19416. ' Left:=Left+33;',
  19417. ' end;',
  19418. 'end;',
  19419. 'procedure TObjHelper.SetLeft(Value: word);',
  19420. 'begin',
  19421. 'end;',
  19422. 'procedure TBird.DoIt;',
  19423. 'begin',
  19424. ' NotRight:=NotRight+11;',
  19425. ' Self.NotRight:=Self.NotRight+21;',
  19426. ' with Self do begin',
  19427. ' NotRight:=NotRight+31;',
  19428. ' end;',
  19429. 'end;',
  19430. 'begin',
  19431. ' b.Size:=b.Size+11;',
  19432. ' b.Speed:=b.Speed+12;',
  19433. ' b.Left:=b.Left+13;',
  19434. ' b.NotRight:=b.NotRight+14;',
  19435. ' with b do begin',
  19436. ' Size:=Size+31;',
  19437. ' Speed:=Speed+32;',
  19438. ' Left:=Left+33;',
  19439. ' NotRight:=NotRight+34;',
  19440. ' end;',
  19441. '']);
  19442. ConvertProgram;
  19443. CheckSource('TestClassHelper_Property',
  19444. LinesToStr([ // statements
  19445. 'rtl.createClass($mod, "TObject", null, function () {',
  19446. ' this.$init = function () {',
  19447. ' this.FSize = 0;',
  19448. ' };',
  19449. ' this.$final = function () {',
  19450. ' };',
  19451. ' this.GetSpeed = function () {',
  19452. ' var Result = 0;',
  19453. ' this.FSize = this.FSize + 11;',
  19454. ' this.SetSpeed(this.GetSpeed() + 12);',
  19455. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  19456. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  19457. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19458. ' this.FSize = this.FSize + 21;',
  19459. ' this.SetSpeed(this.GetSpeed() + 22);',
  19460. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19461. ' this.FSize = this.FSize + 31;',
  19462. ' this.SetSpeed(this.GetSpeed() + 32);',
  19463. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19464. ' return Result;',
  19465. ' };',
  19466. ' this.SetSpeed = function (Value) {',
  19467. ' };',
  19468. '});',
  19469. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19470. ' this.GetLeft = function () {',
  19471. ' var Result = 0;',
  19472. ' this.FSize = this.FSize + 11;',
  19473. ' this.SetSpeed(this.GetSpeed() + 12);',
  19474. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19475. ' this.FSize = this.FSize + 21;',
  19476. ' this.SetSpeed(this.GetSpeed() + 22);',
  19477. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19478. ' this.FSize = this.FSize + 31;',
  19479. ' this.SetSpeed(this.GetSpeed() + 32);',
  19480. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19481. ' return Result;',
  19482. ' };',
  19483. ' this.SetLeft = function (Value) {',
  19484. ' };',
  19485. '});',
  19486. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19487. ' this.DoIt = function () {',
  19488. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  19489. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  19490. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  19491. ' };',
  19492. '});',
  19493. 'this.b = null;',
  19494. '']),
  19495. LinesToStr([ // $mod.$main
  19496. '$mod.b.FSize = $mod.b.FSize + 11;',
  19497. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  19498. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  19499. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  19500. 'var $with1 = $mod.b;',
  19501. '$with1.FSize = $with1.FSize + 31;',
  19502. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  19503. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  19504. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  19505. '']));
  19506. end;
  19507. procedure TTestModule.TestClassHelper_Property_Array;
  19508. begin
  19509. StartProgram(false);
  19510. Add([
  19511. 'type',
  19512. ' TObject = class',
  19513. ' function GetSpeed(Index: boolean): word;',
  19514. ' procedure SetSpeed(Index: boolean; Value: word);',
  19515. ' end;',
  19516. ' TObjHelper = class helper for TObject',
  19517. ' function GetSize(Index: boolean): word;',
  19518. ' procedure SetSize(Index: boolean; Value: word);',
  19519. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  19520. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  19521. ' end;',
  19522. ' TBird = class',
  19523. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  19524. ' procedure DoIt;',
  19525. ' end;',
  19526. 'var',
  19527. ' b: TBird;',
  19528. 'function Tobject.GetSpeed(Index: boolean): word;',
  19529. 'begin',
  19530. ' Result:=Size[false];',
  19531. ' Size[true]:=Size[false]+11;',
  19532. ' Speed[true]:=Speed[false]+12;',
  19533. ' Self.Size[true]:=Self.Size[false]+21;',
  19534. ' Self.Speed[true]:=Self.Speed[false]+22;',
  19535. ' with Self do begin',
  19536. ' Size[true]:=Size[false]+31;',
  19537. ' Speed[true]:=Speed[false]+32;',
  19538. ' end;',
  19539. 'end;',
  19540. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  19541. 'begin',
  19542. 'end;',
  19543. 'function TObjHelper.GetSize(Index: boolean): word;',
  19544. 'begin',
  19545. ' Size[true]:=Size[false]+11;',
  19546. ' Speed[true]:=Speed[false]+12;',
  19547. ' Self.Size[true]:=Self.Size[false]+21;',
  19548. ' Self.Speed[true]:=Self.Speed[false]+22;',
  19549. ' with Self do begin',
  19550. ' Size[true]:=Size[false]+31;',
  19551. ' Speed[true]:=Speed[false]+32;',
  19552. ' end;',
  19553. 'end;',
  19554. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  19555. 'begin',
  19556. 'end;',
  19557. 'procedure TBird.DoIt;',
  19558. 'begin',
  19559. ' Items[true]:=Items[false]+11;',
  19560. ' Self.Items[true]:=Self.Items[false]+21;',
  19561. ' with Self do Items[true]:=Items[false]+31;',
  19562. 'end;',
  19563. 'begin',
  19564. ' b.Size[true]:=b.Size[false]+11;',
  19565. ' b.Speed[true]:=b.Speed[false]+12;',
  19566. ' b.Items[true]:=b.Items[false]+13;',
  19567. ' with b do begin',
  19568. ' Size[true]:=Size[false]+21;',
  19569. ' Speed[true]:=Speed[false]+22;',
  19570. ' Items[true]:=Items[false]+23;',
  19571. ' end;',
  19572. '']);
  19573. ConvertProgram;
  19574. CheckSource('TestClassHelper_Property_Array',
  19575. LinesToStr([ // statements
  19576. 'rtl.createClass($mod, "TObject", null, function () {',
  19577. ' this.$init = function () {',
  19578. ' };',
  19579. ' this.$final = function () {',
  19580. ' };',
  19581. ' this.GetSpeed = function (Index) {',
  19582. ' var Result = 0;',
  19583. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  19584. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19585. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  19586. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19587. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  19588. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19589. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  19590. ' return Result;',
  19591. ' };',
  19592. ' this.SetSpeed = function (Index, Value) {',
  19593. ' };',
  19594. '});',
  19595. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19596. ' this.GetSize = function (Index) {',
  19597. ' var Result = 0;',
  19598. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19599. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  19600. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19601. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  19602. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19603. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  19604. ' return Result;',
  19605. ' };',
  19606. ' this.SetSize = function (Index, Value) {',
  19607. ' };',
  19608. '});',
  19609. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19610. ' this.DoIt = function () {',
  19611. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  19612. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  19613. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  19614. ' };',
  19615. '});',
  19616. 'this.b = null;',
  19617. '']),
  19618. LinesToStr([ // $mod.$main
  19619. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  19620. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  19621. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  19622. 'var $with1 = $mod.b;',
  19623. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  19624. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  19625. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  19626. '']));
  19627. end;
  19628. procedure TTestModule.TestClassHelper_Property_Array_Default;
  19629. begin
  19630. StartProgram(false);
  19631. Add([
  19632. 'type',
  19633. ' TObject = class',
  19634. ' function GetSpeed(Index: boolean): word;',
  19635. ' procedure SetSpeed(Index: boolean; Value: word);',
  19636. ' end;',
  19637. ' TObjHelper = class helper for TObject',
  19638. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  19639. ' end;',
  19640. ' TBird = class',
  19641. ' end;',
  19642. ' TBirdHelper = class helper for TBird',
  19643. ' function GetSize(Index: word): boolean;',
  19644. ' procedure SetSize(Index: word; Value: boolean);',
  19645. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  19646. ' end;',
  19647. 'function Tobject.GetSpeed(Index: boolean): word;',
  19648. 'begin',
  19649. ' Self[true]:=Self[false]+1;',
  19650. 'end;',
  19651. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  19652. 'begin',
  19653. 'end;',
  19654. 'function TBirdHelper.GetSize(Index: word): boolean;',
  19655. 'begin',
  19656. ' Self[1]:=not Self[2];',
  19657. 'end;',
  19658. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  19659. 'begin',
  19660. 'end;',
  19661. 'var',
  19662. ' o: TObject;',
  19663. ' b: TBird;',
  19664. 'begin',
  19665. ' o[true]:=o[false]+1;',
  19666. ' b[3]:=not b[4];',
  19667. '']);
  19668. ConvertProgram;
  19669. CheckSource('TestClassHelper_Property_Array_Default',
  19670. LinesToStr([ // statements
  19671. 'rtl.createClass($mod, "TObject", null, function () {',
  19672. ' this.$init = function () {',
  19673. ' };',
  19674. ' this.$final = function () {',
  19675. ' };',
  19676. ' this.GetSpeed = function (Index) {',
  19677. ' var Result = 0;',
  19678. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  19679. ' return Result;',
  19680. ' };',
  19681. ' this.SetSpeed = function (Index, Value) {',
  19682. ' };',
  19683. '});',
  19684. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19685. '});',
  19686. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19687. '});',
  19688. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  19689. ' this.GetSize = function (Index) {',
  19690. ' var Result = false;',
  19691. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  19692. ' return Result;',
  19693. ' };',
  19694. ' this.SetSize = function (Index, Value) {',
  19695. ' };',
  19696. '});',
  19697. 'this.o = null;',
  19698. 'this.b = null;',
  19699. '']),
  19700. LinesToStr([ // $mod.$main
  19701. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  19702. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  19703. '']));
  19704. end;
  19705. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  19706. begin
  19707. StartProgram(false);
  19708. Add([
  19709. 'type',
  19710. ' TObject = class',
  19711. ' end;',
  19712. ' TObjHelper = class helper for TObject',
  19713. ' function GetItems(Index: word): TObject;',
  19714. ' procedure SetItems(Index: word; Value: TObject);',
  19715. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  19716. ' end;',
  19717. 'function Tobjhelper.GetItems(Index: word): TObject;',
  19718. 'begin',
  19719. ' Self[1][2]:=Self[3][4];',
  19720. 'end;',
  19721. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  19722. 'begin',
  19723. 'end;',
  19724. 'var',
  19725. ' o: TObject;',
  19726. 'begin',
  19727. ' o[1][2]:=o[3][4];',
  19728. '']);
  19729. ConvertProgram;
  19730. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  19731. LinesToStr([ // statements
  19732. 'rtl.createClass($mod, "TObject", null, function () {',
  19733. ' this.$init = function () {',
  19734. ' };',
  19735. ' this.$final = function () {',
  19736. ' };',
  19737. '});',
  19738. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19739. ' this.GetItems = function (Index) {',
  19740. ' var Result = null;',
  19741. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  19742. ' return Result;',
  19743. ' };',
  19744. ' this.SetItems = function (Index, Value) {',
  19745. ' };',
  19746. '});',
  19747. 'this.o = null;',
  19748. '']),
  19749. LinesToStr([ // $mod.$main
  19750. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  19751. '']));
  19752. end;
  19753. procedure TTestModule.TestClassHelper_ClassProperty;
  19754. begin
  19755. StartProgram(false);
  19756. Add([
  19757. 'type',
  19758. ' TObject = class',
  19759. ' class var FSize: word;',
  19760. ' class function GetSpeed: word;',
  19761. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  19762. ' end;',
  19763. ' TObjHelper = class helper for TObject',
  19764. ' class function GetLeft: word;',
  19765. ' class procedure SetLeft(Value: word);',
  19766. ' class property Size: word read FSize write FSize;',
  19767. ' class property Speed: word read GetSpeed write SetSpeed;',
  19768. ' class property Left: word read GetLeft write SetLeft;',
  19769. ' end;',
  19770. ' TBird = class',
  19771. ' class property NotRight: word read GetLeft write SetLeft;',
  19772. ' class procedure DoIt;',
  19773. ' end;',
  19774. ' TBirdClass = class of TBird;',
  19775. 'class function Tobject.GetSpeed: word;',
  19776. 'begin',
  19777. ' Size:=Size+11;',
  19778. ' Speed:=Speed+12;',
  19779. ' Left:=Left+13;',
  19780. ' Self.Size:=Self.Size+21;',
  19781. ' Self.Speed:=Self.Speed+22;',
  19782. ' Self.Left:=Self.Left+23;',
  19783. ' with Self do begin',
  19784. ' Size:=Size+31;',
  19785. ' Speed:=Speed+32;',
  19786. ' Left:=Left+33;',
  19787. ' end;',
  19788. 'end;',
  19789. 'class function TObjHelper.GetLeft: word;',
  19790. 'begin',
  19791. ' Size:=Size+11;',
  19792. ' Speed:=Speed+12;',
  19793. ' Left:=Left+13;',
  19794. ' Self.Size:=Self.Size+21;',
  19795. ' Self.Speed:=Self.Speed+22;',
  19796. ' Self.Left:=Self.Left+23;',
  19797. ' with Self do begin',
  19798. ' Size:=Size+31;',
  19799. ' Speed:=Speed+32;',
  19800. ' Left:=Left+33;',
  19801. ' end;',
  19802. 'end;',
  19803. 'class procedure TObjHelper.SetLeft(Value: word);',
  19804. 'begin',
  19805. 'end;',
  19806. 'class procedure TBird.DoIt;',
  19807. 'begin',
  19808. ' NotRight:=NotRight+11;',
  19809. ' Self.NotRight:=Self.NotRight+21;',
  19810. ' with Self do NotRight:=NotRight+31;',
  19811. 'end;',
  19812. 'var',
  19813. ' b: TBird;',
  19814. ' c: TBirdClass;',
  19815. 'begin',
  19816. ' b.Size:=b.Size+11;',
  19817. ' b.Speed:=b.Speed+12;',
  19818. ' b.Left:=b.Left+13;',
  19819. ' b.NotRight:=b.NotRight+14;',
  19820. ' with b do begin',
  19821. ' Size:=Size+31;',
  19822. ' Speed:=Speed+32;',
  19823. ' Left:=Left+33;',
  19824. ' NotRight:=NotRight+34;',
  19825. ' end;',
  19826. ' c.Size:=c.Size+11;',
  19827. ' c.Speed:=c.Speed+12;',
  19828. ' c.Left:=c.Left+13;',
  19829. ' c.NotRight:=c.NotRight+14;',
  19830. ' with c do begin',
  19831. ' Size:=Size+31;',
  19832. ' Speed:=Speed+32;',
  19833. ' Left:=Left+33;',
  19834. ' NotRight:=NotRight+34;',
  19835. ' end;',
  19836. ' tbird.Size:=tbird.Size+11;',
  19837. ' tbird.Speed:=tbird.Speed+12;',
  19838. ' tbird.Left:=tbird.Left+13;',
  19839. ' tbird.NotRight:=tbird.NotRight+14;',
  19840. ' with tbird do begin',
  19841. ' Size:=Size+31;',
  19842. ' Speed:=Speed+32;',
  19843. ' Left:=Left+33;',
  19844. ' NotRight:=NotRight+34;',
  19845. ' end;',
  19846. '']);
  19847. ConvertProgram;
  19848. CheckSource('TestClassHelper_ClassProperty',
  19849. LinesToStr([ // statements
  19850. 'rtl.createClass($mod, "TObject", null, function () {',
  19851. ' this.FSize = 0;',
  19852. ' this.$init = function () {',
  19853. ' };',
  19854. ' this.$final = function () {',
  19855. ' };',
  19856. ' this.GetSpeed = function () {',
  19857. ' var Result = 0;',
  19858. ' $mod.TObject.FSize = this.FSize + 11;',
  19859. ' this.SetSpeed(this.GetSpeed() + 12);',
  19860. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19861. ' $mod.TObject.FSize = this.FSize + 21;',
  19862. ' this.SetSpeed(this.GetSpeed() + 22);',
  19863. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19864. ' $mod.TObject.FSize = this.FSize + 31;',
  19865. ' this.SetSpeed(this.GetSpeed() + 32);',
  19866. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19867. ' return Result;',
  19868. ' };',
  19869. '});',
  19870. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19871. ' this.GetLeft = function () {',
  19872. ' var Result = 0;',
  19873. ' $mod.TObject.FSize = this.FSize + 11;',
  19874. ' this.SetSpeed(this.GetSpeed() + 12);',
  19875. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19876. ' $mod.TObject.FSize = this.FSize + 21;',
  19877. ' this.SetSpeed(this.GetSpeed() + 22);',
  19878. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19879. ' $mod.TObject.FSize = this.FSize + 31;',
  19880. ' this.SetSpeed(this.GetSpeed() + 32);',
  19881. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19882. ' return Result;',
  19883. ' };',
  19884. ' this.SetLeft = function (Value) {',
  19885. ' };',
  19886. '});',
  19887. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19888. ' this.DoIt = function () {',
  19889. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  19890. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  19891. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  19892. ' };',
  19893. '});',
  19894. 'this.b = null;',
  19895. 'this.c = null;',
  19896. '']),
  19897. LinesToStr([ // $mod.$main
  19898. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  19899. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  19900. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  19901. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  19902. 'var $with1 = $mod.b;',
  19903. '$mod.TObject.FSize = $with1.FSize + 31;',
  19904. '$with1.$class.SetSpeed($with1.$class.GetSpeed() + 32);',
  19905. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 33);',
  19906. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 34);',
  19907. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  19908. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  19909. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  19910. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  19911. 'var $with2 = $mod.c;',
  19912. '$mod.TObject.FSize = $with2.FSize + 31;',
  19913. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  19914. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 33);',
  19915. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 34);',
  19916. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  19917. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  19918. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  19919. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  19920. 'var $with3 = $mod.TBird;',
  19921. '$mod.TObject.FSize = $with3.FSize + 31;',
  19922. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  19923. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  19924. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  19925. '']));
  19926. end;
  19927. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  19928. begin
  19929. StartProgram(false);
  19930. Add([
  19931. 'type',
  19932. ' TObject = class',
  19933. ' class function GetSpeed: word; static;',
  19934. ' class procedure SetSpeed(Value: word); static;',
  19935. ' end;',
  19936. ' TObjHelper = class helper for TObject',
  19937. ' class function GetLeft: word; static;',
  19938. ' class procedure SetLeft(Value: word); static;',
  19939. ' class property Speed: word read GetSpeed write SetSpeed;',
  19940. ' class property Left: word read GetLeft write SetLeft;',
  19941. ' end;',
  19942. ' TBird = class',
  19943. ' class property NotRight: word read GetLeft write SetLeft;',
  19944. ' class procedure DoIt; static;',
  19945. ' class procedure DoSome;',
  19946. ' end;',
  19947. ' TBirdClass = class of TBird;',
  19948. 'class function Tobject.GetSpeed: word;',
  19949. 'begin',
  19950. ' Speed:=Speed+12;',
  19951. ' Left:=Left+13;',
  19952. 'end;',
  19953. 'class procedure TObject.SetSpeed(Value: word);',
  19954. 'begin',
  19955. 'end;',
  19956. 'class function TObjHelper.GetLeft: word;',
  19957. 'begin',
  19958. ' Speed:=Speed+12;',
  19959. ' Left:=Left+13;',
  19960. 'end;',
  19961. 'class procedure TObjHelper.SetLeft(Value: word);',
  19962. 'begin',
  19963. 'end;',
  19964. 'class procedure TBird.DoIt;',
  19965. 'begin',
  19966. ' NotRight:=NotRight+11;',
  19967. 'end;',
  19968. 'class procedure TBird.DoSome;',
  19969. 'begin',
  19970. ' Speed:=Speed+12;',
  19971. ' Left:=Left+13;',
  19972. ' Self.Speed:=Self.Speed+22;',
  19973. ' Self.Left:=Self.Left+23;',
  19974. ' with Self do begin',
  19975. ' Speed:=Speed+32;',
  19976. ' Left:=Left+33;',
  19977. ' end;',
  19978. ' NotRight:=NotRight+11;',
  19979. ' Self.NotRight:=Self.NotRight+21;',
  19980. ' with Self do NotRight:=NotRight+31;',
  19981. 'end;',
  19982. 'var',
  19983. ' b: TBird;',
  19984. ' c: TBirdClass;',
  19985. 'begin',
  19986. ' b.Speed:=b.Speed+12;',
  19987. ' b.Left:=b.Left+13;',
  19988. ' b.NotRight:=b.NotRight+14;',
  19989. ' with b do begin',
  19990. ' Speed:=Speed+32;',
  19991. ' Left:=Left+33;',
  19992. ' NotRight:=NotRight+34;',
  19993. ' end;',
  19994. ' c.Speed:=c.Speed+12;',
  19995. ' c.Left:=c.Left+13;',
  19996. ' c.NotRight:=c.NotRight+14;',
  19997. ' with c do begin',
  19998. ' Speed:=Speed+32;',
  19999. ' Left:=Left+33;',
  20000. ' NotRight:=NotRight+34;',
  20001. ' end;',
  20002. ' tbird.Speed:=tbird.Speed+12;',
  20003. ' tbird.Left:=tbird.Left+13;',
  20004. ' tbird.NotRight:=tbird.NotRight+14;',
  20005. ' with tbird do begin',
  20006. ' Speed:=Speed+32;',
  20007. ' Left:=Left+33;',
  20008. ' NotRight:=NotRight+34;',
  20009. ' end;',
  20010. '']);
  20011. ConvertProgram;
  20012. CheckSource('TestClassHelper_ClassPropertyStatic',
  20013. LinesToStr([ // statements
  20014. 'rtl.createClass($mod, "TObject", null, function () {',
  20015. ' this.$init = function () {',
  20016. ' };',
  20017. ' this.$final = function () {',
  20018. ' };',
  20019. ' this.GetSpeed = function () {',
  20020. ' var Result = 0;',
  20021. ' this.SetSpeed(this.GetSpeed() + 12);',
  20022. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20023. ' return Result;',
  20024. ' };',
  20025. ' this.SetSpeed = function (Value) {',
  20026. ' };',
  20027. '});',
  20028. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20029. ' this.GetLeft = function () {',
  20030. ' var Result = 0;',
  20031. ' this.SetSpeed(this.GetSpeed() + 12);',
  20032. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20033. ' return Result;',
  20034. ' };',
  20035. ' this.SetLeft = function (Value) {',
  20036. ' };',
  20037. '});',
  20038. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20039. ' this.DoIt = function () {',
  20040. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20041. ' };',
  20042. ' this.DoSome = function () {',
  20043. ' this.SetSpeed(this.GetSpeed() + 12);',
  20044. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20045. ' this.SetSpeed(this.GetSpeed() + 22);',
  20046. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  20047. ' this.SetSpeed(this.GetSpeed() + 32);',
  20048. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20049. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20050. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  20051. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  20052. ' };',
  20053. '});',
  20054. 'this.b = null;',
  20055. 'this.c = null;',
  20056. '']),
  20057. LinesToStr([ // $mod.$main
  20058. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20059. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20060. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20061. 'var $with1 = $mod.b;',
  20062. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  20063. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20064. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20065. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  20066. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20067. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20068. 'var $with2 = $mod.c;',
  20069. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  20070. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20071. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20072. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  20073. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20074. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20075. 'var $with3 = $mod.TBird;',
  20076. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  20077. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20078. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20079. '']));
  20080. end;
  20081. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  20082. begin
  20083. StartProgram(false);
  20084. Add([
  20085. 'type',
  20086. ' TObject = class',
  20087. ' class function GetSpeed(Index: boolean): word;',
  20088. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  20089. ' end;',
  20090. ' TObjHelper = class helper for TObject',
  20091. ' class function GetSize(Index: boolean): word;',
  20092. ' class procedure SetSize(Index: boolean; Value: word);',
  20093. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  20094. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20095. ' end;',
  20096. ' TBird = class',
  20097. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  20098. ' class procedure DoIt;',
  20099. ' end;',
  20100. ' TBirdClass = class of TBird;',
  20101. 'class function Tobject.GetSpeed(Index: boolean): word;',
  20102. 'begin',
  20103. ' Size[true]:=Size[false]+11;',
  20104. ' Speed[true]:=Speed[false]+12;',
  20105. ' Self.Size[true]:=Self.Size[false]+21;',
  20106. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20107. ' with Self do begin',
  20108. ' Size[true]:=Size[false]+31;',
  20109. ' Speed[true]:=Speed[false]+32;',
  20110. ' end;',
  20111. 'end;',
  20112. 'class function TObjHelper.GetSize(Index: boolean): word;',
  20113. 'begin',
  20114. ' Size[true]:=Size[false]+11;',
  20115. ' Speed[true]:=Speed[false]+12;',
  20116. ' Self.Size[true]:=Self.Size[false]+21;',
  20117. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20118. ' with Self do begin',
  20119. ' Size[true]:=Size[false]+31;',
  20120. ' Speed[true]:=Speed[false]+32;',
  20121. ' end;',
  20122. 'end;',
  20123. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20124. 'begin',
  20125. 'end;',
  20126. 'class procedure TBird.DoIt;',
  20127. 'begin',
  20128. ' Items[true]:=Items[false]+11;',
  20129. ' Self.Items[true]:=Self.Items[false]+21;',
  20130. ' with Self do Items[true]:=Items[false]+31;',
  20131. 'end;',
  20132. 'var',
  20133. ' b: TBird;',
  20134. ' c: TBirdClass;',
  20135. 'begin',
  20136. ' b.Size[true]:=b.Size[false]+11;',
  20137. ' b.Speed[true]:=b.Speed[false]+12;',
  20138. ' b.Items[true]:=b.Items[false]+13;',
  20139. ' with b do begin',
  20140. ' Size[true]:=Size[false]+21;',
  20141. ' Speed[true]:=Speed[false]+22;',
  20142. ' Items[true]:=Items[false]+23;',
  20143. ' end;',
  20144. ' c.Size[true]:=c.Size[false]+11;',
  20145. ' c.Speed[true]:=c.Speed[false]+12;',
  20146. ' c.Items[true]:=c.Items[false]+13;',
  20147. ' with c do begin',
  20148. ' Size[true]:=Size[false]+21;',
  20149. ' Speed[true]:=Speed[false]+22;',
  20150. ' Items[true]:=Items[false]+23;',
  20151. ' end;',
  20152. ' TBird.Size[true]:=TBird.Size[false]+11;',
  20153. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  20154. ' TBird.Items[true]:=TBird.Items[false]+13;',
  20155. ' with TBird do begin',
  20156. ' Size[true]:=Size[false]+21;',
  20157. ' Speed[true]:=Speed[false]+22;',
  20158. ' Items[true]:=Items[false]+23;',
  20159. ' end;',
  20160. '']);
  20161. ConvertProgram;
  20162. CheckSource('TestClassHelper_ClassProperty_Array',
  20163. LinesToStr([ // statements
  20164. 'rtl.createClass($mod, "TObject", null, function () {',
  20165. ' this.$init = function () {',
  20166. ' };',
  20167. ' this.$final = function () {',
  20168. ' };',
  20169. ' this.GetSpeed = function (Index) {',
  20170. ' var Result = 0;',
  20171. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20172. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20173. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20174. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20175. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20176. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20177. ' return Result;',
  20178. ' };',
  20179. '});',
  20180. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20181. ' this.GetSize = function (Index) {',
  20182. ' var Result = 0;',
  20183. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20184. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20185. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20186. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20187. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20188. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20189. ' return Result;',
  20190. ' };',
  20191. ' this.SetSize = function (Index, Value) {',
  20192. ' };',
  20193. '});',
  20194. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20195. ' this.DoIt = function () {',
  20196. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20197. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20198. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20199. ' };',
  20200. '});',
  20201. 'this.b = null;',
  20202. 'this.c = null;',
  20203. '']),
  20204. LinesToStr([ // $mod.$main
  20205. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  20206. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  20207. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  20208. 'var $with1 = $mod.b;',
  20209. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 21);',
  20210. '$with1.$class.SetSpeed(true, $with1.$class.GetSpeed(false) + 22);',
  20211. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 23);',
  20212. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  20213. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  20214. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  20215. 'var $with2 = $mod.c;',
  20216. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 21);',
  20217. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  20218. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 23);',
  20219. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  20220. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  20221. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  20222. 'var $with3 = $mod.TBird;',
  20223. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  20224. '$with3.SetSpeed(true, $with3.GetSpeed(false) + 22);',
  20225. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  20226. '']));
  20227. end;
  20228. procedure TTestModule.TestClassHelper_ForIn;
  20229. begin
  20230. StartProgram(false);
  20231. Add([
  20232. 'type',
  20233. ' TObject = class end;',
  20234. ' TItem = TObject;',
  20235. ' TEnumerator = class',
  20236. ' FCurrent: TItem;',
  20237. ' property Current: TItem read FCurrent;',
  20238. ' function MoveNext: boolean;',
  20239. ' end;',
  20240. ' TBird = class',
  20241. ' end;',
  20242. ' TBirdHelper = class helper for TBird',
  20243. ' function GetEnumerator: TEnumerator;',
  20244. ' end;',
  20245. 'function TEnumerator.MoveNext: boolean;',
  20246. 'begin',
  20247. 'end;',
  20248. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  20249. 'begin',
  20250. 'end;',
  20251. 'var',
  20252. ' b: TBird;',
  20253. ' i, i2: TItem;',
  20254. 'begin',
  20255. ' for i in b do i2:=i;']);
  20256. ConvertProgram;
  20257. CheckSource('TestClassHelper_ForIn',
  20258. LinesToStr([ // statements
  20259. 'rtl.createClass($mod, "TObject", null, function () {',
  20260. ' this.$init = function () {',
  20261. ' };',
  20262. ' this.$final = function () {',
  20263. ' };',
  20264. '});',
  20265. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  20266. ' this.$init = function () {',
  20267. ' $mod.TObject.$init.call(this);',
  20268. ' this.FCurrent = null;',
  20269. ' };',
  20270. ' this.$final = function () {',
  20271. ' this.FCurrent = undefined;',
  20272. ' $mod.TObject.$final.call(this);',
  20273. ' };',
  20274. ' this.MoveNext = function () {',
  20275. ' var Result = false;',
  20276. ' return Result;',
  20277. ' };',
  20278. '});',
  20279. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20280. '});',
  20281. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20282. ' this.GetEnumerator = function () {',
  20283. ' var Result = null;',
  20284. ' return Result;',
  20285. ' };',
  20286. '});',
  20287. 'this.b = null;',
  20288. 'this.i = null;',
  20289. 'this.i2 = null;'
  20290. ]),
  20291. LinesToStr([ // $mod.$main
  20292. 'var $in1 = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  20293. 'try {',
  20294. ' while ($in1.MoveNext()){',
  20295. ' $mod.i = $in1.FCurrent;',
  20296. ' $mod.i2 = $mod.i;',
  20297. ' }',
  20298. '} finally {',
  20299. ' $in1 = rtl.freeLoc($in1)',
  20300. '};',
  20301. '']));
  20302. end;
  20303. procedure TTestModule.TestClassHelper_PassProperty;
  20304. begin
  20305. StartProgram(false);
  20306. Add([
  20307. 'type',
  20308. ' TObject = class',
  20309. ' FField: TObject;',
  20310. ' property Field: TObject read FField write FField;',
  20311. ' end;',
  20312. ' THelper = class helper for TObject',
  20313. ' procedure Fly;',
  20314. ' class procedure Run;',
  20315. ' class procedure Jump; static;',
  20316. ' end;',
  20317. 'procedure THelper.Fly;',
  20318. 'begin',
  20319. ' Field.Fly;',
  20320. ' Field.Run;',
  20321. ' Field.Jump;',
  20322. ' with Field do begin',
  20323. ' Fly;',
  20324. ' Run;',
  20325. ' Jump;',
  20326. ' end;',
  20327. 'end;',
  20328. 'class procedure THelper.Run;',
  20329. 'begin',
  20330. 'end;',
  20331. 'class procedure THelper.Jump;',
  20332. 'begin',
  20333. 'end;',
  20334. 'var',
  20335. ' b: TObject;',
  20336. 'begin',
  20337. ' b.Field.Fly;',
  20338. ' b.Field.Run;',
  20339. ' b.Field.Jump;',
  20340. ' with b do begin',
  20341. ' Field.Run;',
  20342. ' Field.Fly;',
  20343. ' Field.Jump;',
  20344. ' end;',
  20345. ' with b.Field do begin',
  20346. ' Run;',
  20347. ' Fly;',
  20348. ' Jump;',
  20349. ' end;',
  20350. '']);
  20351. ConvertProgram;
  20352. CheckSource('TestClassHelper_PassProperty',
  20353. LinesToStr([ // statements
  20354. 'rtl.createClass($mod, "TObject", null, function () {',
  20355. ' this.$init = function () {',
  20356. ' this.FField = null;',
  20357. ' };',
  20358. ' this.$final = function () {',
  20359. ' this.FField = undefined;',
  20360. ' };',
  20361. '});',
  20362. 'rtl.createHelper($mod, "THelper", null, function () {',
  20363. ' this.Fly = function () {',
  20364. ' $mod.THelper.Fly.call(this.FField);',
  20365. ' $mod.THelper.Run.call(this.FField.$class);',
  20366. ' $mod.THelper.Jump();',
  20367. ' var $with1 = this.FField;',
  20368. ' $mod.THelper.Fly.call($with1);',
  20369. ' $mod.THelper.Run.call($with1.$class);',
  20370. ' $mod.THelper.Jump();',
  20371. ' };',
  20372. ' this.Run = function () {',
  20373. ' };',
  20374. ' this.Jump = function () {',
  20375. ' };',
  20376. '});',
  20377. 'this.b = null;',
  20378. '']),
  20379. LinesToStr([ // $mod.$main
  20380. '$mod.THelper.Fly.call($mod.b.FField);',
  20381. '$mod.THelper.Run.call($mod.b.FField.$class);',
  20382. '$mod.THelper.Jump();',
  20383. 'var $with1 = $mod.b;',
  20384. '$mod.THelper.Run.call($with1.FField.$class);',
  20385. '$mod.THelper.Fly.call($with1.FField);',
  20386. '$mod.THelper.Jump();',
  20387. 'var $with2 = $mod.b.FField;',
  20388. '$mod.THelper.Run.call($with2.$class);',
  20389. '$mod.THelper.Fly.call($with2);',
  20390. '$mod.THelper.Jump();',
  20391. '']));
  20392. end;
  20393. procedure TTestModule.TestExtClassHelper_ClassVar;
  20394. begin
  20395. StartProgram(false);
  20396. Add([
  20397. '{$modeswitch externalclass}',
  20398. 'type',
  20399. ' TExtA = class external name ''ExtObj''',
  20400. ' end;',
  20401. ' THelper = class helper for TExtA',
  20402. ' const',
  20403. ' One = 1;',
  20404. ' Two: word = 2;',
  20405. ' class var',
  20406. ' Glob: word;',
  20407. ' function Foo(w: word): word;',
  20408. ' class function Bar(w: word): word; static;',
  20409. ' end;',
  20410. 'function THelper.foo(w: word): word;',
  20411. 'begin',
  20412. ' Result:=w;',
  20413. ' Two:=One+w;',
  20414. ' Glob:=Glob;',
  20415. ' Result:=Self.Glob;',
  20416. ' Self.Glob:=Self.Glob;',
  20417. ' with Self do Glob:=Glob;',
  20418. 'end;',
  20419. 'class function THelper.bar(w: word): word;',
  20420. 'begin',
  20421. ' Result:=w;',
  20422. ' Two:=One;',
  20423. ' Glob:=Glob;',
  20424. 'end;',
  20425. 'var o: TExtA;',
  20426. 'begin',
  20427. ' texta.two:=texta.one;',
  20428. ' texta.Glob:=texta.Glob;',
  20429. ' with texta do begin',
  20430. ' two:=one;',
  20431. ' Glob:=Glob;',
  20432. ' end;',
  20433. ' o.two:=o.one;',
  20434. ' o.Glob:=o.Glob;',
  20435. ' with o do begin',
  20436. ' two:=one;',
  20437. ' Glob:=Glob;',
  20438. ' end;',
  20439. '']);
  20440. ConvertProgram;
  20441. CheckSource('TestExtClassHelper_ClassVar',
  20442. LinesToStr([ // statements
  20443. 'rtl.createHelper($mod, "THelper", null, function () {',
  20444. ' this.One = 1;',
  20445. ' this.Two = 2;',
  20446. ' this.Glob = 0;',
  20447. ' this.Foo = function (w) {',
  20448. ' var Result = 0;',
  20449. ' Result = w;',
  20450. ' $mod.THelper.Two = 1 + w;',
  20451. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20452. ' Result = $mod.THelper.Glob;',
  20453. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20454. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20455. ' return Result;',
  20456. ' };',
  20457. ' this.Bar = function (w) {',
  20458. ' var Result = 0;',
  20459. ' Result = w;',
  20460. ' $mod.THelper.Two = 1;',
  20461. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20462. ' return Result;',
  20463. ' };',
  20464. '});',
  20465. 'this.o = null;',
  20466. '']),
  20467. LinesToStr([ // $mod.$main
  20468. '$mod.THelper.Two = 1;',
  20469. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20470. '$mod.THelper.Two = 1;',
  20471. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20472. '$mod.THelper.Two = 1;',
  20473. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20474. 'var $with1 = $mod.o;',
  20475. '$mod.THelper.Two = 1;',
  20476. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20477. '']));
  20478. end;
  20479. procedure TTestModule.TestExtClassHelper_Method_Call;
  20480. begin
  20481. StartProgram(false);
  20482. Add([
  20483. '{$modeswitch externalclass}',
  20484. 'type',
  20485. ' TFly = function(w: word): word of object;',
  20486. ' TExtA = class external name ''ExtObj''',
  20487. ' procedure Run(w: word = 10);',
  20488. ' end;',
  20489. ' THelper = class helper for TExtA',
  20490. ' function Foo(w: word = 1): word;',
  20491. ' function Fly(w: word = 2): word; external name ''Fly'';',
  20492. ' end;',
  20493. 'var p: TFly;',
  20494. 'function THelper.foo(w: word): word;',
  20495. 'begin',
  20496. ' Run;',
  20497. ' Run();',
  20498. ' Run(11);',
  20499. ' Foo;',
  20500. ' Foo();',
  20501. ' Foo(12);',
  20502. ' Self.Foo;',
  20503. ' Self.Foo();',
  20504. ' Self.Foo(13);',
  20505. ' Fly;',
  20506. ' Fly();',
  20507. ' with Self do begin',
  20508. ' Foo;',
  20509. ' Foo();',
  20510. ' Foo(14);',
  20511. ' Fly;',
  20512. ' Fly();',
  20513. ' end;',
  20514. ' p:=@Fly;',
  20515. 'end;',
  20516. 'var Obj: TExtA;',
  20517. 'begin',
  20518. ' obj.Foo;',
  20519. ' obj.Foo();',
  20520. ' obj.Foo(21);',
  20521. ' obj.Fly;',
  20522. ' obj.Fly();',
  20523. ' with obj do begin',
  20524. ' Foo;',
  20525. ' Foo();',
  20526. ' Foo(22);',
  20527. ' Fly;',
  20528. ' Fly();',
  20529. ' end;',
  20530. ' p:[email protected];',
  20531. '']);
  20532. ConvertProgram;
  20533. CheckSource('TestExtClassHelper_Method_Call',
  20534. LinesToStr([ // statements
  20535. 'rtl.createHelper($mod, "THelper", null, function () {',
  20536. ' this.Foo = function (w) {',
  20537. ' var Result = 0;',
  20538. ' this.Run(10);',
  20539. ' this.Run(10);',
  20540. ' this.Run(11);',
  20541. ' $mod.THelper.Foo.call(this, 1);',
  20542. ' $mod.THelper.Foo.call(this, 1);',
  20543. ' $mod.THelper.Foo.call(this, 12);',
  20544. ' $mod.THelper.Foo.call(this, 1);',
  20545. ' $mod.THelper.Foo.call(this, 1);',
  20546. ' $mod.THelper.Foo.call(this, 13);',
  20547. ' this.Fly(2);',
  20548. ' this.Fly(2);',
  20549. ' $mod.THelper.Foo.call(this, 1);',
  20550. ' $mod.THelper.Foo.call(this, 1);',
  20551. ' $mod.THelper.Foo.call(this, 14);',
  20552. ' this.Fly(2);',
  20553. ' this.Fly(2);',
  20554. ' $mod.p = rtl.createCallback(this, "Fly");',
  20555. ' return Result;',
  20556. ' };',
  20557. '});',
  20558. 'this.p = null;',
  20559. 'this.Obj = null;',
  20560. '']),
  20561. LinesToStr([ // $mod.$main
  20562. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20563. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20564. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20565. '$mod.Obj.Fly(2);',
  20566. '$mod.Obj.Fly(2);',
  20567. 'var $with1 = $mod.Obj;',
  20568. '$mod.THelper.Foo.call($with1, 1);',
  20569. '$mod.THelper.Foo.call($with1, 1);',
  20570. '$mod.THelper.Foo.call($with1, 22);',
  20571. '$with1.Fly(2);',
  20572. '$with1.Fly(2);',
  20573. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  20574. '']));
  20575. end;
  20576. procedure TTestModule.TestRecordHelper_ClassVar;
  20577. begin
  20578. StartProgram(false);
  20579. Add([
  20580. 'type',
  20581. ' TRec = record',
  20582. ' end;',
  20583. ' THelper = record helper for TRec',
  20584. ' const',
  20585. ' One = 1;',
  20586. ' Two: word = 2;',
  20587. ' class var',
  20588. ' Glob: word;',
  20589. ' function Foo(w: word): word;',
  20590. ' class function Bar(w: word): word; static;',
  20591. ' end;',
  20592. 'function THelper.foo(w: word): word;',
  20593. 'begin',
  20594. ' Result:=w;',
  20595. ' Two:=One+w;',
  20596. ' Glob:=Glob;',
  20597. ' Result:=Self.Glob;',
  20598. ' Self.Glob:=Self.Glob;',
  20599. ' with Self do Glob:=Glob;',
  20600. ' Self:=Self;',
  20601. 'end;',
  20602. 'class function THelper.bar(w: word): word;',
  20603. 'begin',
  20604. ' Result:=w;',
  20605. ' Two:=One;',
  20606. ' Glob:=Glob;',
  20607. 'end;',
  20608. 'var r: TRec;',
  20609. 'begin',
  20610. ' trec.two:=trec.one;',
  20611. ' trec.Glob:=trec.Glob;',
  20612. ' with trec do begin',
  20613. ' two:=one;',
  20614. ' Glob:=Glob;',
  20615. ' end;',
  20616. ' r.two:=r.one;',
  20617. ' r.Glob:=r.Glob;',
  20618. ' with r do begin',
  20619. ' two:=one;',
  20620. ' Glob:=Glob;',
  20621. ' end;',
  20622. '']);
  20623. ConvertProgram;
  20624. CheckSource('TestRecordHelper_ClassVar',
  20625. LinesToStr([ // statements
  20626. 'rtl.recNewT($mod, "TRec", function () {',
  20627. ' this.$eq = function (b) {',
  20628. ' return true;',
  20629. ' };',
  20630. ' this.$assign = function (s) {',
  20631. ' return this;',
  20632. ' };',
  20633. '});',
  20634. 'rtl.createHelper($mod, "THelper", null, function () {',
  20635. ' this.One = 1;',
  20636. ' this.Two = 2;',
  20637. ' this.Glob = 0;',
  20638. ' this.Foo = function (w) {',
  20639. ' var Result = 0;',
  20640. ' Result = w;',
  20641. ' $mod.THelper.Two = 1 + w;',
  20642. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20643. ' Result = $mod.THelper.Glob;',
  20644. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20645. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20646. ' this.$assign(this);',
  20647. ' return Result;',
  20648. ' };',
  20649. ' this.Bar = function (w) {',
  20650. ' var Result = 0;',
  20651. ' Result = w;',
  20652. ' $mod.THelper.Two = 1;',
  20653. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20654. ' return Result;',
  20655. ' };',
  20656. '});',
  20657. 'this.r = $mod.TRec.$new();',
  20658. '']),
  20659. LinesToStr([ // $mod.$main
  20660. '$mod.THelper.Two = 1;',
  20661. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20662. 'var $with1 = $mod.TRec;',
  20663. '$mod.THelper.Two = 1;',
  20664. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20665. '$mod.THelper.Two = 1;',
  20666. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20667. 'var $with2 = $mod.r;',
  20668. '$mod.THelper.Two = 1;',
  20669. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20670. '']));
  20671. end;
  20672. procedure TTestModule.TestRecordHelper_Method_Call;
  20673. begin
  20674. StartProgram(false);
  20675. Add([
  20676. '{$modeswitch AdvancedRecords}',
  20677. 'type',
  20678. ' TRec = record',
  20679. ' procedure Run(w: word = 10);',
  20680. ' end;',
  20681. ' THelper = record helper for TRec',
  20682. ' function Foo(w: word = 1): word;',
  20683. ' end;',
  20684. 'procedure TRec.Run(w: word);',
  20685. 'begin',
  20686. ' Foo;',
  20687. ' Foo();',
  20688. ' Foo(2);',
  20689. ' Self.Foo;',
  20690. ' Self.Foo();',
  20691. ' Self.Foo(3);',
  20692. ' with Self do begin',
  20693. ' Foo;',
  20694. ' Foo();',
  20695. ' Foo(4);',
  20696. ' end;',
  20697. 'end;',
  20698. 'function THelper.foo(w: word): word;',
  20699. 'begin',
  20700. ' Run;',
  20701. ' Run();',
  20702. ' Run(11);',
  20703. ' Foo;',
  20704. ' Foo();',
  20705. ' Foo(12);',
  20706. ' Self.Foo;',
  20707. ' Self.Foo();',
  20708. ' Self.Foo(13);',
  20709. ' with Self do begin',
  20710. ' Foo;',
  20711. ' Foo();',
  20712. ' Foo(14);',
  20713. ' end;',
  20714. 'end;',
  20715. 'var Rec: TRec;',
  20716. 'begin',
  20717. ' Rec.Foo;',
  20718. ' Rec.Foo();',
  20719. ' Rec.Foo(21);',
  20720. ' with Rec do begin',
  20721. ' Foo;',
  20722. ' Foo();',
  20723. ' Foo(22);',
  20724. ' end;',
  20725. '']);
  20726. ConvertProgram;
  20727. CheckSource('TestRecordHelper_Method_Call',
  20728. LinesToStr([ // statements
  20729. 'rtl.recNewT($mod, "TRec", function () {',
  20730. ' this.$eq = function (b) {',
  20731. ' return true;',
  20732. ' };',
  20733. ' this.$assign = function (s) {',
  20734. ' return this;',
  20735. ' };',
  20736. ' this.Run = function (w) {',
  20737. ' $mod.THelper.Foo.call(this, 1);',
  20738. ' $mod.THelper.Foo.call(this, 1);',
  20739. ' $mod.THelper.Foo.call(this, 2);',
  20740. ' $mod.THelper.Foo.call(this, 1);',
  20741. ' $mod.THelper.Foo.call(this, 1);',
  20742. ' $mod.THelper.Foo.call(this, 3);',
  20743. ' $mod.THelper.Foo.call(this, 1);',
  20744. ' $mod.THelper.Foo.call(this, 1);',
  20745. ' $mod.THelper.Foo.call(this, 4);',
  20746. ' };',
  20747. '});',
  20748. 'rtl.createHelper($mod, "THelper", null, function () {',
  20749. ' this.Foo = function (w) {',
  20750. ' var Result = 0;',
  20751. ' this.Run(10);',
  20752. ' this.Run(10);',
  20753. ' this.Run(11);',
  20754. ' $mod.THelper.Foo.call(this, 1);',
  20755. ' $mod.THelper.Foo.call(this, 1);',
  20756. ' $mod.THelper.Foo.call(this, 12);',
  20757. ' $mod.THelper.Foo.call(this, 1);',
  20758. ' $mod.THelper.Foo.call(this, 1);',
  20759. ' $mod.THelper.Foo.call(this, 13);',
  20760. ' $mod.THelper.Foo.call(this, 1);',
  20761. ' $mod.THelper.Foo.call(this, 1);',
  20762. ' $mod.THelper.Foo.call(this, 14);',
  20763. ' return Result;',
  20764. ' };',
  20765. '});',
  20766. 'this.Rec = $mod.TRec.$new();',
  20767. '']),
  20768. LinesToStr([ // $mod.$main
  20769. '$mod.THelper.Foo.call($mod.Rec, 1);',
  20770. '$mod.THelper.Foo.call($mod.Rec, 1);',
  20771. '$mod.THelper.Foo.call($mod.Rec, 21);',
  20772. 'var $with1 = $mod.Rec;',
  20773. '$mod.THelper.Foo.call($with1, 1);',
  20774. '$mod.THelper.Foo.call($with1, 1);',
  20775. '$mod.THelper.Foo.call($with1, 22);',
  20776. '']));
  20777. end;
  20778. procedure TTestModule.TestRecordHelper_Constructor;
  20779. begin
  20780. StartProgram(false);
  20781. Add([
  20782. '{$modeswitch AdvancedRecords}',
  20783. 'type',
  20784. ' TRec = record',
  20785. ' constructor Create(w: word);',
  20786. ' end;',
  20787. ' THelper = record helper for TRec',
  20788. ' constructor NewHlp(w: word);',
  20789. ' end;',
  20790. 'var',
  20791. ' Rec: TRec;',
  20792. 'constructor TRec.Create(w: word);',
  20793. 'begin',
  20794. ' NewHlp(2);', // normal call
  20795. ' trec.NewHlp(3);', // new instance
  20796. 'end;',
  20797. 'constructor THelper.NewHlp(w: word);',
  20798. 'begin',
  20799. ' create(2);', // normal call
  20800. ' trec.create(3);', // new instance
  20801. ' NewHlp(4);', // normal call
  20802. ' trec.NewHlp(5);', // new instance
  20803. 'end;',
  20804. 'begin',
  20805. ' rec.newhlp(2);', // normal call
  20806. ' with rec do newhlp(12);', // normal call
  20807. ' trec.newhlp(3);', // new instance
  20808. ' with trec do newhlp(13);', // new instance
  20809. '']);
  20810. ConvertProgram;
  20811. CheckSource('TestRecordHelper_Constructor',
  20812. LinesToStr([ // statements
  20813. 'rtl.recNewT($mod, "TRec", function () {',
  20814. ' this.$eq = function (b) {',
  20815. ' return true;',
  20816. ' };',
  20817. ' this.$assign = function (s) {',
  20818. ' return this;',
  20819. ' };',
  20820. ' this.Create = function (w) {',
  20821. ' $mod.THelper.NewHlp.call(this, 2);',
  20822. ' $mod.THelper.$new("NewHlp", [3]);',
  20823. ' return this;',
  20824. ' };',
  20825. '}, true);',
  20826. 'rtl.createHelper($mod, "THelper", null, function () {',
  20827. ' this.NewHlp = function (w) {',
  20828. ' this.Create(2);',
  20829. ' $mod.TRec.$new().Create(3);',
  20830. ' $mod.THelper.NewHlp.call(this, 4);',
  20831. ' $mod.THelper.$new("NewHlp", [5]);',
  20832. ' return this;',
  20833. ' };',
  20834. ' this.$new = function (fn, args) {',
  20835. ' return this[fn].apply($mod.TRec.$new(), args);',
  20836. ' };',
  20837. '});',
  20838. 'this.Rec = $mod.TRec.$new();',
  20839. '']),
  20840. LinesToStr([ // $mod.$main
  20841. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  20842. 'var $with1 = $mod.Rec;',
  20843. '$mod.THelper.NewHlp.call($with1, 12);',
  20844. '$mod.THelper.$new("NewHlp", [3]);',
  20845. 'var $with2 = $mod.TRec;',
  20846. '$mod.THelper.$new("NewHlp", [13]);',
  20847. '']));
  20848. end;
  20849. procedure TTestModule.TestTypeHelper_ClassVar;
  20850. begin
  20851. StartProgram(false);
  20852. Add([
  20853. '{$modeswitch typehelpers}',
  20854. 'type',
  20855. ' THelper = type helper for byte',
  20856. ' const',
  20857. ' One = 1;',
  20858. ' Two: word = 2;',
  20859. ' class var',
  20860. ' Glob: word;',
  20861. ' function Foo(w: word): word;',
  20862. ' class function Bar(w: word): word; static;',
  20863. ' end;',
  20864. 'function THelper.foo(w: word): word;',
  20865. 'begin',
  20866. ' Result:=w;',
  20867. ' Two:=One+w;',
  20868. ' Glob:=Glob;',
  20869. ' Result:=Self.Glob;',
  20870. ' Self.Glob:=Self.Glob;',
  20871. ' with Self do Glob:=Glob;',
  20872. 'end;',
  20873. 'class function THelper.bar(w: word): word;',
  20874. 'begin',
  20875. ' Result:=w;',
  20876. ' Two:=One;',
  20877. ' Glob:=Glob;',
  20878. 'end;',
  20879. 'var b: byte;',
  20880. 'begin',
  20881. ' byte.two:=byte.one;',
  20882. ' byte.Glob:=byte.Glob;',
  20883. ' with byte do begin',
  20884. ' two:=one;',
  20885. ' Glob:=Glob;',
  20886. ' end;',
  20887. ' b.two:=b.one;',
  20888. ' b.Glob:=b.Glob;',
  20889. ' with b do begin',
  20890. ' two:=one;',
  20891. ' Glob:=Glob;',
  20892. ' end;',
  20893. '']);
  20894. ConvertProgram;
  20895. CheckSource('TestTypeHelper_ClassVar',
  20896. LinesToStr([ // statements
  20897. 'rtl.createHelper($mod, "THelper", null, function () {',
  20898. ' this.One = 1;',
  20899. ' this.Two = 2;',
  20900. ' this.Glob = 0;',
  20901. ' this.Foo = function (w) {',
  20902. ' var Result = 0;',
  20903. ' Result = w;',
  20904. ' $mod.THelper.Two = 1 + w;',
  20905. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20906. ' Result = $mod.THelper.Glob;',
  20907. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20908. ' var $with1 = this.get();',
  20909. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20910. ' return Result;',
  20911. ' };',
  20912. ' this.Bar = function (w) {',
  20913. ' var Result = 0;',
  20914. ' Result = w;',
  20915. ' $mod.THelper.Two = 1;',
  20916. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20917. ' return Result;',
  20918. ' };',
  20919. '});',
  20920. 'this.b = 0;',
  20921. '']),
  20922. LinesToStr([ // $mod.$main
  20923. '$mod.THelper.Two = 1;',
  20924. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20925. '$mod.THelper.Two = 1;',
  20926. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20927. '$mod.THelper.Two = 1;',
  20928. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20929. 'var $with1 = $mod.b;',
  20930. '$mod.THelper.Two = 1;',
  20931. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20932. '']));
  20933. end;
  20934. procedure TTestModule.TestTypeHelper_PassResultElement;
  20935. begin
  20936. StartProgram(false);
  20937. Add([
  20938. '{$modeswitch typehelpers}',
  20939. 'type',
  20940. ' THelper = type helper for word',
  20941. ' procedure DoIt(e: byte = 123);',
  20942. ' class procedure DoSome(e: byte = 456); static;',
  20943. ' end;',
  20944. 'procedure THelper.DoIt(e: byte);',
  20945. 'begin',
  20946. 'end;',
  20947. 'class procedure THelper.DoSome(e: byte);',
  20948. 'begin',
  20949. 'end;',
  20950. 'function Foo(w: word): word;',
  20951. 'begin',
  20952. ' Result.DoIt;',
  20953. ' Result.DoIt();',
  20954. ' Result.DoSome;',
  20955. ' Result.DoSome();',
  20956. ' with Result do begin',
  20957. ' DoIt;',
  20958. ' DoIt();',
  20959. ' DoSome;',
  20960. ' DoSome();',
  20961. ' end;',
  20962. 'end;',
  20963. 'begin',
  20964. '']);
  20965. ConvertProgram;
  20966. CheckSource('TestTypeHelper_PassResultElement',
  20967. LinesToStr([ // statements
  20968. 'rtl.createHelper($mod, "THelper", null, function () {',
  20969. ' this.DoIt = function (e) {',
  20970. ' };',
  20971. ' this.DoSome = function (e) {',
  20972. ' };',
  20973. '});',
  20974. 'this.Foo = function (w) {',
  20975. ' var Result = 0;',
  20976. ' $mod.THelper.DoIt.call({',
  20977. ' get: function () {',
  20978. ' return Result;',
  20979. ' },',
  20980. ' set: function (v) {',
  20981. ' Result = v;',
  20982. ' }',
  20983. ' }, 123);',
  20984. ' $mod.THelper.DoIt.call({',
  20985. ' get: function () {',
  20986. ' return Result;',
  20987. ' },',
  20988. ' set: function (v) {',
  20989. ' Result = v;',
  20990. ' }',
  20991. ' }, 123);',
  20992. ' $mod.THelper.DoSome(456);',
  20993. ' $mod.THelper.DoSome(456);',
  20994. ' $mod.THelper.DoIt.call({',
  20995. ' get: function () {',
  20996. ' return Result;',
  20997. ' },',
  20998. ' set: function (v) {',
  20999. ' Result = v;',
  21000. ' }',
  21001. ' }, 123);',
  21002. ' $mod.THelper.DoIt.call({',
  21003. ' get: function () {',
  21004. ' return Result;',
  21005. ' },',
  21006. ' set: function (v) {',
  21007. ' Result = v;',
  21008. ' }',
  21009. ' }, 123);',
  21010. ' $mod.THelper.DoSome(456);',
  21011. ' $mod.THelper.DoSome(456);',
  21012. ' return Result;',
  21013. '};',
  21014. '']),
  21015. LinesToStr([ // $mod.$main
  21016. '']));
  21017. end;
  21018. procedure TTestModule.TestTypeHelper_PassArgs;
  21019. begin
  21020. StartProgram(false);
  21021. Add([
  21022. '{$modeswitch typehelpers}',
  21023. 'type',
  21024. ' THelper = type helper for word',
  21025. ' procedure DoIt(e: byte = 123);',
  21026. ' end;',
  21027. 'procedure THelper.DoIt(e: byte);',
  21028. 'begin',
  21029. 'end;',
  21030. 'procedure FooDefault(a: word);',
  21031. 'begin',
  21032. ' a.DoIt;',
  21033. ' with a do DoIt;',
  21034. 'end;',
  21035. 'procedure FooConst(const a: word);',
  21036. 'begin',
  21037. ' a.DoIt;',
  21038. ' with a do DoIt;',
  21039. 'end;',
  21040. 'procedure FooVar(var a: word);',
  21041. 'begin',
  21042. ' a.DoIt;',
  21043. ' with a do DoIt;',
  21044. 'end;',
  21045. 'begin',
  21046. '']);
  21047. ConvertProgram;
  21048. CheckSource('TestTypeHelper_PassArgs',
  21049. LinesToStr([ // statements
  21050. 'rtl.createHelper($mod, "THelper", null, function () {',
  21051. ' this.DoIt = function (e) {',
  21052. ' };',
  21053. '});',
  21054. 'this.FooDefault = function (a) {',
  21055. ' $mod.THelper.DoIt.call({',
  21056. ' get: function () {',
  21057. ' return a;',
  21058. ' },',
  21059. ' set: function (v) {',
  21060. ' a = v;',
  21061. ' }',
  21062. ' }, 123);',
  21063. ' $mod.THelper.DoIt.call({',
  21064. ' get: function () {',
  21065. ' return a;',
  21066. ' },',
  21067. ' set: function (v) {',
  21068. ' a = v;',
  21069. ' }',
  21070. ' }, 123);',
  21071. '};',
  21072. 'this.FooConst = function (a) {',
  21073. ' $mod.THelper.DoIt.call({',
  21074. ' get: function () {',
  21075. ' return a;',
  21076. ' },',
  21077. ' set: function (v) {',
  21078. ' rtl.raiseE("EPropReadOnly");',
  21079. ' }',
  21080. ' }, 123);',
  21081. ' $mod.THelper.DoIt.call({',
  21082. ' get: function () {',
  21083. ' return a;',
  21084. ' },',
  21085. ' set: function () {',
  21086. ' rtl.raiseE("EPropReadOnly");',
  21087. ' }',
  21088. ' }, 123);',
  21089. '};',
  21090. 'this.FooVar = function (a) {',
  21091. ' $mod.THelper.DoIt.call(a, 123);',
  21092. ' var $with1 = a.get();',
  21093. ' $mod.THelper.DoIt.call(a, 123);',
  21094. '};',
  21095. '']),
  21096. LinesToStr([ // $mod.$main
  21097. '']));
  21098. end;
  21099. procedure TTestModule.TestTypeHelper_PassVarConst;
  21100. begin
  21101. StartProgram(false);
  21102. Add([
  21103. '{$modeswitch typehelpers}',
  21104. 'type',
  21105. ' THelper = type helper for word',
  21106. ' procedure DoIt(e: byte = 123);',
  21107. ' end;',
  21108. 'procedure THelper.DoIt(e: byte);',
  21109. 'begin',
  21110. 'end;',
  21111. 'var a: word;',
  21112. 'const c: word = 2;',
  21113. '{$writeableconst off}',
  21114. 'const r: word = 3;',
  21115. 'begin',
  21116. ' a.DoIt;',
  21117. ' with a do DoIt;',
  21118. ' c.DoIt;',
  21119. ' with c do DoIt;',
  21120. ' r.DoIt;',
  21121. ' with r do DoIt;',
  21122. '']);
  21123. ConvertProgram;
  21124. CheckSource('TestTypeHelper_PassVarConst',
  21125. LinesToStr([ // statements
  21126. 'rtl.createHelper($mod, "THelper", null, function () {',
  21127. ' this.DoIt = function (e) {',
  21128. ' };',
  21129. '});',
  21130. 'this.a = 0;',
  21131. 'this.c = 2;',
  21132. 'this.r = 3;',
  21133. '']),
  21134. LinesToStr([ // $mod.$main
  21135. '$mod.THelper.DoIt.call({',
  21136. ' p: $mod,',
  21137. ' get: function () {',
  21138. ' return this.p.a;',
  21139. ' },',
  21140. ' set: function (v) {',
  21141. ' this.p.a = v;',
  21142. ' }',
  21143. '}, 123);',
  21144. 'var $with1 = $mod.a;',
  21145. '$mod.THelper.DoIt.call({',
  21146. ' get: function () {',
  21147. ' return $with1;',
  21148. ' },',
  21149. ' set: function (v) {',
  21150. ' $with1 = v;',
  21151. ' }',
  21152. '}, 123);',
  21153. '$mod.THelper.DoIt.call({',
  21154. ' p: $mod,',
  21155. ' get: function () {',
  21156. ' return this.p.c;',
  21157. ' },',
  21158. ' set: function (v) {',
  21159. ' this.p.c = v;',
  21160. ' }',
  21161. '}, 123);',
  21162. 'var $with2 = $mod.c;',
  21163. '$mod.THelper.DoIt.call({',
  21164. ' get: function () {',
  21165. ' return $with2;',
  21166. ' },',
  21167. ' set: function (v) {',
  21168. ' $with2 = v;',
  21169. ' }',
  21170. '}, 123);',
  21171. '$mod.THelper.DoIt.call({',
  21172. ' get: function () {',
  21173. ' return 3;',
  21174. ' },',
  21175. ' set: function (v) {',
  21176. ' rtl.raiseE("EPropReadOnly");',
  21177. ' }',
  21178. '}, 123);',
  21179. 'var $with3 = 3;',
  21180. ' $mod.THelper.DoIt.call({',
  21181. ' get: function () {',
  21182. ' return $with3;',
  21183. ' },',
  21184. ' set: function () {',
  21185. ' rtl.raiseE("EPropReadOnly");',
  21186. ' }',
  21187. ' }, 123);',
  21188. '']));
  21189. end;
  21190. procedure TTestModule.TestTypeHelper_PassFuncResult;
  21191. begin
  21192. StartProgram(false);
  21193. Add([
  21194. '{$modeswitch typehelpers}',
  21195. 'type',
  21196. ' THelper = type helper for word',
  21197. ' procedure DoIt(e: byte = 123);',
  21198. ' end;',
  21199. 'procedure THelper.DoIt(e: byte);',
  21200. 'begin',
  21201. 'end;',
  21202. 'function Foo(b: byte = 1): word;',
  21203. 'begin',
  21204. 'end;',
  21205. 'begin',
  21206. ' Foo.DoIt;',
  21207. ' Foo().DoIt;',
  21208. ' with Foo do DoIt;',
  21209. ' with Foo() do DoIt;',
  21210. '']);
  21211. ConvertProgram;
  21212. CheckSource('TestTypeHelper_PassFuncResult',
  21213. LinesToStr([ // statements
  21214. 'rtl.createHelper($mod, "THelper", null, function () {',
  21215. ' this.DoIt = function (e) {',
  21216. ' };',
  21217. '});',
  21218. 'this.Foo = function (b) {',
  21219. ' var Result = 0;',
  21220. ' return Result;',
  21221. '};',
  21222. '']),
  21223. LinesToStr([ // $mod.$main
  21224. '$mod.THelper.DoIt.call({',
  21225. ' a: $mod.Foo(1),',
  21226. ' get: function () {',
  21227. ' return this.a;',
  21228. ' },',
  21229. ' set: function (v) {',
  21230. ' this.a = v;',
  21231. ' }',
  21232. '}, 123);',
  21233. '$mod.THelper.DoIt.call({',
  21234. ' a: $mod.Foo(1),',
  21235. ' get: function () {',
  21236. ' return this.a;',
  21237. ' },',
  21238. ' set: function (v) {',
  21239. ' this.a = v;',
  21240. ' }',
  21241. '}, 123);',
  21242. 'var $with1 = $mod.Foo(1);',
  21243. '$mod.THelper.DoIt.call({',
  21244. ' get: function () {',
  21245. ' return $with1;',
  21246. ' },',
  21247. ' set: function (v) {',
  21248. ' $with1 = v;',
  21249. ' }',
  21250. '}, 123);',
  21251. 'var $with2 = $mod.Foo(1);',
  21252. '$mod.THelper.DoIt.call({',
  21253. ' get: function () {',
  21254. ' return $with2;',
  21255. ' },',
  21256. ' set: function (v) {',
  21257. ' $with2 = v;',
  21258. ' }',
  21259. '}, 123);',
  21260. '']));
  21261. end;
  21262. procedure TTestModule.TestTypeHelper_PassPropertyField;
  21263. begin
  21264. StartProgram(false);
  21265. Add([
  21266. '{$modeswitch typehelpers}',
  21267. 'type',
  21268. ' TObject = class',
  21269. ' FField: word;',
  21270. ' procedure SetField(Value: word);',
  21271. ' property Field: word read FField write SetField;',
  21272. ' end;',
  21273. ' THelper = type helper for word',
  21274. ' procedure Fly;',
  21275. ' class procedure Run; static;',
  21276. ' end;',
  21277. 'procedure TObject.SetField(Value: word);',
  21278. 'begin',
  21279. ' Field.Fly;',
  21280. ' Field.Run;',
  21281. ' Self.Field.Fly;',
  21282. ' Self.Field.Run;',
  21283. ' with Self do begin',
  21284. ' Field.Fly;',
  21285. ' Field.Run;',
  21286. ' end;',
  21287. ' with Self.Field do begin',
  21288. ' Fly;',
  21289. ' Run;',
  21290. ' end;',
  21291. 'end;',
  21292. 'procedure THelper.Fly;',
  21293. 'begin',
  21294. 'end;',
  21295. 'class procedure THelper.Run;',
  21296. 'begin',
  21297. 'end;',
  21298. 'var',
  21299. ' o: TObject;',
  21300. 'begin',
  21301. ' o.Field.Fly;',
  21302. ' o.Field.Run;',
  21303. ' with o do begin',
  21304. ' Field.Fly;',
  21305. ' Field.Run;',
  21306. ' end;',
  21307. ' with o.Field do begin',
  21308. ' Fly;',
  21309. ' Run;',
  21310. ' end;',
  21311. '']);
  21312. ConvertProgram;
  21313. CheckSource('TestTypeHelper_PassPropertyField',
  21314. LinesToStr([ // statements
  21315. 'rtl.createClass($mod, "TObject", null, function () {',
  21316. ' this.$init = function () {',
  21317. ' this.FField = 0;',
  21318. ' };',
  21319. ' this.$final = function () {',
  21320. ' };',
  21321. ' this.SetField = function (Value) {',
  21322. ' $mod.THelper.Fly.call({',
  21323. ' p: this,',
  21324. ' get: function () {',
  21325. ' return this.p.FField;',
  21326. ' },',
  21327. ' set: function (v) {',
  21328. ' this.p.FField = v;',
  21329. ' }',
  21330. ' });',
  21331. ' $mod.THelper.Run();',
  21332. ' $mod.THelper.Fly.call({',
  21333. ' p: this,',
  21334. ' get: function () {',
  21335. ' return this.p.FField;',
  21336. ' },',
  21337. ' set: function (v) {',
  21338. ' this.p.FField = v;',
  21339. ' }',
  21340. ' });',
  21341. ' $mod.THelper.Run();',
  21342. ' $mod.THelper.Fly.call({',
  21343. ' p: this,',
  21344. ' get: function () {',
  21345. ' return this.p.FField;',
  21346. ' },',
  21347. ' set: function (v) {',
  21348. ' this.p.FField = v;',
  21349. ' }',
  21350. ' });',
  21351. ' $mod.THelper.Run();',
  21352. ' var $with1 = this.FField;',
  21353. ' $mod.THelper.Fly.call({',
  21354. ' get: function () {',
  21355. ' return $with1;',
  21356. ' },',
  21357. ' set: function (v) {',
  21358. ' $with1 = v;',
  21359. ' }',
  21360. ' });',
  21361. ' $mod.THelper.Run();',
  21362. ' };',
  21363. '});',
  21364. 'rtl.createHelper($mod, "THelper", null, function () {',
  21365. ' this.Fly = function () {',
  21366. ' };',
  21367. ' this.Run = function () {',
  21368. ' };',
  21369. '});',
  21370. 'this.o = null;',
  21371. '']),
  21372. LinesToStr([ // $mod.$main
  21373. '$mod.THelper.Fly.call({',
  21374. ' p: $mod.o,',
  21375. ' get: function () {',
  21376. ' return this.p.FField;',
  21377. ' },',
  21378. ' set: function (v) {',
  21379. ' this.p.FField = v;',
  21380. ' }',
  21381. '});',
  21382. '$mod.THelper.Run();',
  21383. 'var $with1 = $mod.o;',
  21384. '$mod.THelper.Fly.call({',
  21385. ' p: $with1,',
  21386. ' get: function () {',
  21387. ' return this.p.FField;',
  21388. ' },',
  21389. ' set: function (v) {',
  21390. ' this.p.FField = v;',
  21391. ' }',
  21392. '});',
  21393. '$mod.THelper.Run();',
  21394. 'var $with2 = $mod.o.FField;',
  21395. '$mod.THelper.Fly.call({',
  21396. ' get: function () {',
  21397. ' return $with2;',
  21398. ' },',
  21399. ' set: function (v) {',
  21400. ' $with2 = v;',
  21401. ' }',
  21402. '});',
  21403. '$mod.THelper.Run();',
  21404. '']));
  21405. end;
  21406. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  21407. begin
  21408. StartProgram(false);
  21409. Add([
  21410. '{$modeswitch typehelpers}',
  21411. 'type',
  21412. ' TObject = class',
  21413. ' FField: word;',
  21414. ' function GetField: word;',
  21415. ' property Field: word read GetField write FField;',
  21416. ' end;',
  21417. ' THelper = type helper for word',
  21418. ' procedure Fly;',
  21419. ' class procedure Run; static;',
  21420. ' end;',
  21421. 'function TObject.GetField: word;',
  21422. 'begin',
  21423. ' Field.Fly;',
  21424. ' Field.Run;',
  21425. ' Self.Field.Fly;',
  21426. ' Self.Field.Run;',
  21427. ' with Self do begin',
  21428. ' Field.Fly;',
  21429. ' Field.Run;',
  21430. ' end;',
  21431. ' with Self.Field do begin',
  21432. ' Fly;',
  21433. ' Run;',
  21434. ' end;',
  21435. 'end;',
  21436. 'procedure THelper.Fly;',
  21437. 'begin',
  21438. 'end;',
  21439. 'class procedure THelper.Run;',
  21440. 'begin',
  21441. 'end;',
  21442. 'var',
  21443. ' o: TObject;',
  21444. 'begin',
  21445. ' o.Field.Fly;',
  21446. ' o.Field.Run;',
  21447. ' with o do begin',
  21448. ' Field.Fly;',
  21449. ' Field.Run;',
  21450. ' end;',
  21451. ' with o.Field do begin',
  21452. ' Fly;',
  21453. ' Run;',
  21454. ' end;',
  21455. '']);
  21456. ConvertProgram;
  21457. CheckSource('TestTypeHelper_PassPropertyGetter',
  21458. LinesToStr([ // statements
  21459. 'rtl.createClass($mod, "TObject", null, function () {',
  21460. ' this.$init = function () {',
  21461. ' this.FField = 0;',
  21462. ' };',
  21463. ' this.$final = function () {',
  21464. ' };',
  21465. ' this.GetField = function () {',
  21466. ' var Result = 0;',
  21467. ' $mod.THelper.Fly.call({',
  21468. ' p: this.GetField(),',
  21469. ' get: function () {',
  21470. ' return this.p;',
  21471. ' },',
  21472. ' set: function (v) {',
  21473. ' this.p = v;',
  21474. ' }',
  21475. ' });',
  21476. ' $mod.THelper.Run();',
  21477. ' $mod.THelper.Fly.call({',
  21478. ' p: this.GetField(),',
  21479. ' get: function () {',
  21480. ' return this.p;',
  21481. ' },',
  21482. ' set: function (v) {',
  21483. ' this.p = v;',
  21484. ' }',
  21485. ' });',
  21486. ' $mod.THelper.Run();',
  21487. ' $mod.THelper.Fly.call({',
  21488. ' p: this.GetField(),',
  21489. ' get: function () {',
  21490. ' return this.p;',
  21491. ' },',
  21492. ' set: function (v) {',
  21493. ' this.p = v;',
  21494. ' }',
  21495. ' });',
  21496. ' $mod.THelper.Run();',
  21497. ' var $with1 = this.GetField();',
  21498. ' $mod.THelper.Fly.call({',
  21499. ' get: function () {',
  21500. ' return $with1;',
  21501. ' },',
  21502. ' set: function (v) {',
  21503. ' $with1 = v;',
  21504. ' }',
  21505. ' });',
  21506. ' $mod.THelper.Run();',
  21507. ' return Result;',
  21508. ' };',
  21509. '});',
  21510. 'rtl.createHelper($mod, "THelper", null, function () {',
  21511. ' this.Fly = function () {',
  21512. ' };',
  21513. ' this.Run = function () {',
  21514. ' };',
  21515. '});',
  21516. 'this.o = null;',
  21517. '']),
  21518. LinesToStr([ // $mod.$main
  21519. '$mod.THelper.Fly.call({',
  21520. ' p: $mod.o.GetField(),',
  21521. ' get: function () {',
  21522. ' return this.p;',
  21523. ' },',
  21524. ' set: function (v) {',
  21525. ' this.p = v;',
  21526. ' }',
  21527. '});',
  21528. '$mod.THelper.Run();',
  21529. 'var $with1 = $mod.o;',
  21530. '$mod.THelper.Fly.call({',
  21531. ' p: $with1.GetField(),',
  21532. ' get: function () {',
  21533. ' return this.p;',
  21534. ' },',
  21535. ' set: function (v) {',
  21536. ' this.p = v;',
  21537. ' }',
  21538. '});',
  21539. '$mod.THelper.Run();',
  21540. 'var $with2 = $mod.o.GetField();',
  21541. '$mod.THelper.Fly.call({',
  21542. ' get: function () {',
  21543. ' return $with2;',
  21544. ' },',
  21545. ' set: function (v) {',
  21546. ' $with2 = v;',
  21547. ' }',
  21548. '});',
  21549. '$mod.THelper.Run();',
  21550. '']));
  21551. end;
  21552. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  21553. begin
  21554. StartProgram(false);
  21555. Add([
  21556. '{$modeswitch typehelpers}',
  21557. 'type',
  21558. ' TObject = class',
  21559. ' class var FField: word;',
  21560. ' class procedure SetField(Value: word);',
  21561. ' class property Field: word read FField write SetField;',
  21562. ' end;',
  21563. ' THelper = type helper for word',
  21564. ' procedure Fly(n: byte);',
  21565. ' end;',
  21566. 'class procedure TObject.SetField(Value: word);',
  21567. 'begin',
  21568. ' Field.Fly(1);',
  21569. ' Self.Field.Fly(2);',
  21570. ' with Self do Field.Fly(3);',
  21571. ' with Self.Field do Fly(4);',
  21572. ' TObject.Field.Fly(5);',
  21573. ' with TObject do Field.Fly(6);',
  21574. ' with TObject.Field do Fly(7);',
  21575. 'end;',
  21576. 'procedure THelper.Fly(n: byte);',
  21577. 'begin',
  21578. 'end;',
  21579. 'var',
  21580. ' o: TObject;',
  21581. 'begin',
  21582. ' o.Field.Fly(11);',
  21583. ' with o do Field.Fly(12);',
  21584. ' with o.Field do Fly(13);',
  21585. ' TObject.Field.Fly(14);',
  21586. ' with TObject do Field.Fly(15);',
  21587. ' with TObject.Field do Fly(16);',
  21588. '']);
  21589. ConvertProgram;
  21590. CheckSource('TestTypeHelper_PassClassPropertyField',
  21591. LinesToStr([ // statements
  21592. 'rtl.createClass($mod, "TObject", null, function () {',
  21593. ' this.FField = 0;',
  21594. ' this.$init = function () {',
  21595. ' };',
  21596. ' this.$final = function () {',
  21597. ' };',
  21598. ' this.SetField = function (Value) {',
  21599. ' $mod.THelper.Fly.call({',
  21600. ' p: this,',
  21601. ' get: function () {',
  21602. ' return this.p.FField;',
  21603. ' },',
  21604. ' set: function (v) {',
  21605. ' $mod.TObject.FField = v;',
  21606. ' }',
  21607. ' }, 1);',
  21608. ' $mod.THelper.Fly.call({',
  21609. ' p: this,',
  21610. ' get: function () {',
  21611. ' return this.p.FField;',
  21612. ' },',
  21613. ' set: function (v) {',
  21614. ' $mod.TObject.FField = v;',
  21615. ' }',
  21616. ' }, 2);',
  21617. ' $mod.THelper.Fly.call({',
  21618. ' p: this,',
  21619. ' get: function () {',
  21620. ' return this.p.FField;',
  21621. ' },',
  21622. ' set: function (v) {',
  21623. ' $mod.TObject.FField = v;',
  21624. ' }',
  21625. ' }, 3);',
  21626. ' var $with1 = this.FField;',
  21627. ' $mod.THelper.Fly.call({',
  21628. ' get: function () {',
  21629. ' return $with1;',
  21630. ' },',
  21631. ' set: function (v) {',
  21632. ' $with1 = v;',
  21633. ' }',
  21634. ' }, 4);',
  21635. ' $mod.THelper.Fly.call({',
  21636. ' p: $mod.TObject,',
  21637. ' get: function () {',
  21638. ' return this.p.FField;',
  21639. ' },',
  21640. ' set: function (v) {',
  21641. ' $mod.TObject.FField = v;',
  21642. ' }',
  21643. ' }, 5);',
  21644. ' var $with2 = $mod.TObject;',
  21645. ' $mod.THelper.Fly.call({',
  21646. ' p: $with2,',
  21647. ' get: function () {',
  21648. ' return this.p.FField;',
  21649. ' },',
  21650. ' set: function (v) {',
  21651. ' $mod.TObject.FField = v;',
  21652. ' }',
  21653. ' }, 6);',
  21654. ' var $with3 = $mod.TObject.FField;',
  21655. ' $mod.THelper.Fly.call({',
  21656. ' get: function () {',
  21657. ' return $with3;',
  21658. ' },',
  21659. ' set: function (v) {',
  21660. ' $with3 = v;',
  21661. ' }',
  21662. ' }, 7);',
  21663. ' };',
  21664. '});',
  21665. 'rtl.createHelper($mod, "THelper", null, function () {',
  21666. ' this.Fly = function (n) {',
  21667. ' };',
  21668. '});',
  21669. 'this.o = null;',
  21670. '']),
  21671. LinesToStr([ // $mod.$main
  21672. '$mod.THelper.Fly.call({',
  21673. ' p: $mod.o,',
  21674. ' get: function () {',
  21675. ' return this.p.FField;',
  21676. ' },',
  21677. ' set: function (v) {',
  21678. ' $mod.TObject.FField = v;',
  21679. ' }',
  21680. '}, 11);',
  21681. 'var $with1 = $mod.o;',
  21682. '$mod.THelper.Fly.call({',
  21683. ' p: $with1,',
  21684. ' get: function () {',
  21685. ' return this.p.FField;',
  21686. ' },',
  21687. ' set: function (v) {',
  21688. ' $mod.TObject.FField = v;',
  21689. ' }',
  21690. '}, 12);',
  21691. 'var $with2 = $mod.o.FField;',
  21692. '$mod.THelper.Fly.call({',
  21693. ' get: function () {',
  21694. ' return $with2;',
  21695. ' },',
  21696. ' set: function (v) {',
  21697. ' $with2 = v;',
  21698. ' }',
  21699. '}, 13);',
  21700. '$mod.THelper.Fly.call({',
  21701. ' p: $mod.TObject,',
  21702. ' get: function () {',
  21703. ' return this.p.FField;',
  21704. ' },',
  21705. ' set: function (v) {',
  21706. ' $mod.TObject.FField = v;',
  21707. ' }',
  21708. '}, 14);',
  21709. 'var $with3 = $mod.TObject;',
  21710. '$mod.THelper.Fly.call({',
  21711. ' p: $with3,',
  21712. ' get: function () {',
  21713. ' return this.p.FField;',
  21714. ' },',
  21715. ' set: function (v) {',
  21716. ' $mod.TObject.FField = v;',
  21717. ' }',
  21718. '}, 15);',
  21719. 'var $with4 = $mod.TObject.FField;',
  21720. '$mod.THelper.Fly.call({',
  21721. ' get: function () {',
  21722. ' return $with4;',
  21723. ' },',
  21724. ' set: function (v) {',
  21725. ' $with4 = v;',
  21726. ' }',
  21727. '}, 16);',
  21728. '']));
  21729. end;
  21730. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  21731. begin
  21732. StartProgram(false);
  21733. Add([
  21734. '{$modeswitch typehelpers}',
  21735. 'type',
  21736. ' TObject = class',
  21737. ' class var FField: word;',
  21738. ' class function GetField: word; static;',
  21739. ' class property Field: word read GetField write FField;',
  21740. ' end;',
  21741. ' THelper = type helper for word',
  21742. ' procedure Fly(n: byte);',
  21743. ' end;',
  21744. 'class function TObject.GetField: word;',
  21745. 'begin',
  21746. ' Field.Fly(1);',
  21747. ' TObject.Field.Fly(5);',
  21748. ' with TObject do Field.Fly(6);',
  21749. ' with TObject.Field do Fly(7);',
  21750. 'end;',
  21751. 'procedure THelper.Fly(n: byte);',
  21752. 'begin',
  21753. 'end;',
  21754. 'var',
  21755. ' o: TObject;',
  21756. 'begin',
  21757. ' o.Field.Fly(11);',
  21758. ' with o do Field.Fly(12);',
  21759. ' with o.Field do Fly(13);',
  21760. '']);
  21761. ConvertProgram;
  21762. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  21763. LinesToStr([ // statements
  21764. 'rtl.createClass($mod, "TObject", null, function () {',
  21765. ' this.FField = 0;',
  21766. ' this.$init = function () {',
  21767. ' };',
  21768. ' this.$final = function () {',
  21769. ' };',
  21770. ' this.GetField = function () {',
  21771. ' var Result = 0;',
  21772. ' $mod.THelper.Fly.call({',
  21773. ' p: this.GetField(),',
  21774. ' get: function () {',
  21775. ' return this.p;',
  21776. ' },',
  21777. ' set: function (v) {',
  21778. ' this.p = v;',
  21779. ' }',
  21780. ' }, 1);',
  21781. ' $mod.THelper.Fly.call({',
  21782. ' p: $mod.TObject.GetField(),',
  21783. ' get: function () {',
  21784. ' return this.p;',
  21785. ' },',
  21786. ' set: function (v) {',
  21787. ' this.p = v;',
  21788. ' }',
  21789. ' }, 5);',
  21790. ' var $with1 = $mod.TObject;',
  21791. ' $mod.THelper.Fly.call({',
  21792. ' p: $with1.GetField(),',
  21793. ' get: function () {',
  21794. ' return this.p;',
  21795. ' },',
  21796. ' set: function (v) {',
  21797. ' this.p = v;',
  21798. ' }',
  21799. ' }, 6);',
  21800. ' var $with2 = $mod.TObject.GetField();',
  21801. ' $mod.THelper.Fly.call({',
  21802. ' get: function () {',
  21803. ' return $with2;',
  21804. ' },',
  21805. ' set: function (v) {',
  21806. ' $with2 = v;',
  21807. ' }',
  21808. ' }, 7);',
  21809. ' return Result;',
  21810. ' };',
  21811. '});',
  21812. 'rtl.createHelper($mod, "THelper", null, function () {',
  21813. ' this.Fly = function (n) {',
  21814. ' };',
  21815. '});',
  21816. 'this.o = null;',
  21817. '']),
  21818. LinesToStr([ // $mod.$main
  21819. '$mod.THelper.Fly.call({',
  21820. ' p: $mod.o.GetField(),',
  21821. ' get: function () {',
  21822. ' return this.p;',
  21823. ' },',
  21824. ' set: function (v) {',
  21825. ' this.p = v;',
  21826. ' }',
  21827. '}, 11);',
  21828. 'var $with1 = $mod.o;',
  21829. '$mod.THelper.Fly.call({',
  21830. ' p: $with1.GetField(),',
  21831. ' get: function () {',
  21832. ' return this.p;',
  21833. ' },',
  21834. ' set: function (v) {',
  21835. ' this.p = v;',
  21836. ' }',
  21837. '}, 12);',
  21838. 'var $with2 = $mod.o.GetField();',
  21839. '$mod.THelper.Fly.call({',
  21840. ' get: function () {',
  21841. ' return $with2;',
  21842. ' },',
  21843. ' set: function (v) {',
  21844. ' $with2 = v;',
  21845. ' }',
  21846. '}, 13);',
  21847. '']));
  21848. end;
  21849. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  21850. begin
  21851. StartProgram(false);
  21852. Add([
  21853. '{$modeswitch typehelpers}',
  21854. 'type',
  21855. ' TObject = class',
  21856. ' class var FField: word;',
  21857. ' class function GetField: word;',
  21858. ' class property Field: word read GetField write FField;',
  21859. ' end;',
  21860. ' TClass = class of TObject;',
  21861. ' THelper = type helper for word',
  21862. ' procedure Fly(n: byte);',
  21863. ' end;',
  21864. 'class function TObject.GetField: word;',
  21865. 'begin',
  21866. ' Field.Fly(1);',
  21867. ' Self.Field.Fly(5);',
  21868. ' with Self do Field.Fly(6);',
  21869. ' with Self.Field do Fly(7);',
  21870. 'end;',
  21871. 'procedure THelper.Fly(n: byte);',
  21872. 'begin',
  21873. 'end;',
  21874. 'var',
  21875. ' o: TObject;',
  21876. ' c: TClass;',
  21877. 'begin',
  21878. ' o.Field.Fly(11);',
  21879. ' with o do Field.Fly(12);',
  21880. ' with o.Field do Fly(13);',
  21881. ' c.Field.Fly(14);',
  21882. ' with c do Field.Fly(15);',
  21883. ' with c.Field do Fly(16);',
  21884. '']);
  21885. ConvertProgram;
  21886. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  21887. LinesToStr([ // statements
  21888. 'rtl.createClass($mod, "TObject", null, function () {',
  21889. ' this.FField = 0;',
  21890. ' this.$init = function () {',
  21891. ' };',
  21892. ' this.$final = function () {',
  21893. ' };',
  21894. ' this.GetField = function () {',
  21895. ' var Result = 0;',
  21896. ' $mod.THelper.Fly.call({',
  21897. ' p: this.GetField(),',
  21898. ' get: function () {',
  21899. ' return this.p;',
  21900. ' },',
  21901. ' set: function (v) {',
  21902. ' this.p = v;',
  21903. ' }',
  21904. ' }, 1);',
  21905. ' $mod.THelper.Fly.call({',
  21906. ' p: this.GetField(),',
  21907. ' get: function () {',
  21908. ' return this.p;',
  21909. ' },',
  21910. ' set: function (v) {',
  21911. ' this.p = v;',
  21912. ' }',
  21913. ' }, 5);',
  21914. ' $mod.THelper.Fly.call({',
  21915. ' p: this.GetField(),',
  21916. ' get: function () {',
  21917. ' return this.p;',
  21918. ' },',
  21919. ' set: function (v) {',
  21920. ' this.p = v;',
  21921. ' }',
  21922. ' }, 6);',
  21923. ' var $with1 = this.GetField();',
  21924. ' $mod.THelper.Fly.call({',
  21925. ' get: function () {',
  21926. ' return $with1;',
  21927. ' },',
  21928. ' set: function (v) {',
  21929. ' $with1 = v;',
  21930. ' }',
  21931. ' }, 7);',
  21932. ' return Result;',
  21933. ' };',
  21934. '});',
  21935. 'rtl.createHelper($mod, "THelper", null, function () {',
  21936. ' this.Fly = function (n) {',
  21937. ' };',
  21938. '});',
  21939. 'this.o = null;',
  21940. 'this.c = null;',
  21941. '']),
  21942. LinesToStr([ // $mod.$main
  21943. '$mod.THelper.Fly.call({',
  21944. ' p: $mod.o.$class.GetField(),',
  21945. ' get: function () {',
  21946. ' return this.p;',
  21947. ' },',
  21948. ' set: function (v) {',
  21949. ' this.p = v;',
  21950. ' }',
  21951. '}, 11);',
  21952. 'var $with1 = $mod.o;',
  21953. '$mod.THelper.Fly.call({',
  21954. ' p: $with1.$class.GetField(),',
  21955. ' get: function () {',
  21956. ' return this.p;',
  21957. ' },',
  21958. ' set: function (v) {',
  21959. ' this.p = v;',
  21960. ' }',
  21961. '}, 12);',
  21962. 'var $with2 = $mod.o.$class.GetField();',
  21963. '$mod.THelper.Fly.call({',
  21964. ' get: function () {',
  21965. ' return $with2;',
  21966. ' },',
  21967. ' set: function (v) {',
  21968. ' $with2 = v;',
  21969. ' }',
  21970. '}, 13);',
  21971. '$mod.THelper.Fly.call({',
  21972. ' p: $mod.c.GetField(),',
  21973. ' get: function () {',
  21974. ' return this.p;',
  21975. ' },',
  21976. ' set: function (v) {',
  21977. ' this.p = v;',
  21978. ' }',
  21979. '}, 14);',
  21980. 'var $with3 = $mod.c;',
  21981. '$mod.THelper.Fly.call({',
  21982. ' p: $with3.GetField(),',
  21983. ' get: function () {',
  21984. ' return this.p;',
  21985. ' },',
  21986. ' set: function (v) {',
  21987. ' this.p = v;',
  21988. ' }',
  21989. '}, 15);',
  21990. 'var $with4 = $mod.c.GetField();',
  21991. '$mod.THelper.Fly.call({',
  21992. ' get: function () {',
  21993. ' return $with4;',
  21994. ' },',
  21995. ' set: function (v) {',
  21996. ' $with4 = v;',
  21997. ' }',
  21998. '}, 16);',
  21999. '']));
  22000. end;
  22001. procedure TTestModule.TestTypeHelper_Property;
  22002. begin
  22003. StartProgram(false);
  22004. Add([
  22005. '{$modeswitch typehelpers}',
  22006. 'type',
  22007. ' THelper = type helper for word',
  22008. ' function GetSize: longint;',
  22009. ' procedure SetSize(Value: longint);',
  22010. ' property Size: longint read GetSize write SetSize;',
  22011. ' end;',
  22012. 'function THelper.GetSize: longint;',
  22013. 'begin',
  22014. ' Result:=Size+1;',
  22015. ' Size:=2;',
  22016. ' Result:=Self.Size+3;',
  22017. ' Self.Size:=4;',
  22018. ' with Self do begin',
  22019. ' Result:=Size+5;',
  22020. ' Size:=6;',
  22021. ' end;',
  22022. 'end;',
  22023. 'procedure THelper.SetSize(Value: longint);',
  22024. 'begin',
  22025. 'end;',
  22026. 'var w: word;',
  22027. 'begin',
  22028. ' w:=w.Size+7;',
  22029. ' w.Size:=w+8;',
  22030. ' with w do begin',
  22031. ' w:=Size+9;',
  22032. ' Size:=w+10;',
  22033. ' end;',
  22034. '']);
  22035. ConvertProgram;
  22036. CheckSource('TestTypeHelper_Property',
  22037. LinesToStr([ // statements
  22038. 'rtl.createHelper($mod, "THelper", null, function () {',
  22039. ' this.GetSize = function () {',
  22040. ' var Result = 0;',
  22041. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  22042. ' $mod.THelper.SetSize.call(this, 2);',
  22043. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  22044. ' $mod.THelper.SetSize.call(this, 4);',
  22045. ' var $with1 = this.get();',
  22046. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  22047. ' $mod.THelper.SetSize.call(this, 6);',
  22048. ' return Result;',
  22049. ' };',
  22050. ' this.SetSize = function (Value) {',
  22051. ' };',
  22052. '});',
  22053. 'this.w = 0;',
  22054. '']),
  22055. LinesToStr([ // $mod.$main
  22056. '$mod.w = $mod.THelper.GetSize.call({',
  22057. ' p: $mod,',
  22058. ' get: function () {',
  22059. ' return this.p.w;',
  22060. ' },',
  22061. ' set: function (v) {',
  22062. ' this.p.w = v;',
  22063. ' }',
  22064. '}) + 7;',
  22065. '$mod.THelper.SetSize.call({',
  22066. ' p: $mod,',
  22067. ' get: function () {',
  22068. ' return this.p.w;',
  22069. ' },',
  22070. ' set: function (v) {',
  22071. ' this.p.w = v;',
  22072. ' }',
  22073. '}, $mod.w + 8);',
  22074. 'var $with1 = $mod.w;',
  22075. '$mod.w = $mod.THelper.GetSize.call({',
  22076. ' get: function () {',
  22077. ' return $with1;',
  22078. ' },',
  22079. ' set: function (v) {',
  22080. ' $with1 = v;',
  22081. ' }',
  22082. '}) + 9;',
  22083. '$mod.THelper.SetSize.call({',
  22084. ' get: function () {',
  22085. ' return $with1;',
  22086. ' },',
  22087. ' set: function (v) {',
  22088. ' $with1 = v;',
  22089. ' }',
  22090. '}, $mod.w + 10);',
  22091. '']));
  22092. end;
  22093. procedure TTestModule.TestTypeHelper_Property_Array;
  22094. begin
  22095. StartProgram(false);
  22096. Add([
  22097. '{$modeswitch typehelpers}',
  22098. 'type',
  22099. ' THelper = type helper for word',
  22100. ' function GetItems(Index: byte): boolean;',
  22101. ' procedure SetItems(Index: byte; Value: boolean);',
  22102. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  22103. ' end;',
  22104. 'function THelper.GetItems(Index: byte): boolean;',
  22105. 'begin',
  22106. ' Result:=Items[1];',
  22107. ' Items[2]:=false;',
  22108. ' Result:=Self.Items[3];',
  22109. ' Self.Items[4]:=true;',
  22110. ' with Self do begin',
  22111. ' Result:=Items[5];',
  22112. ' Items[6]:=false;',
  22113. ' end;',
  22114. 'end;',
  22115. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  22116. 'begin',
  22117. 'end;',
  22118. 'var',
  22119. ' w: word;',
  22120. ' b: boolean;',
  22121. 'begin',
  22122. ' b:=w.Items[1];',
  22123. ' w.Items[2]:=b;',
  22124. ' with w do begin',
  22125. ' b:=Items[3];',
  22126. ' Items[4]:=b;',
  22127. ' end;',
  22128. '']);
  22129. ConvertProgram;
  22130. CheckSource('TestTypeHelper_Property_Array',
  22131. LinesToStr([ // statements
  22132. 'rtl.createHelper($mod, "THelper", null, function () {',
  22133. ' this.GetItems = function (Index) {',
  22134. ' var Result = false;',
  22135. ' Result = $mod.THelper.GetItems.call(this, 1);',
  22136. ' $mod.THelper.SetItems.call(this, 2, false);',
  22137. ' Result = $mod.THelper.GetItems.call(this, 3);',
  22138. ' $mod.THelper.SetItems.call(this, 4, true);',
  22139. ' var $with1 = this.get();',
  22140. ' Result = $mod.THelper.GetItems.call(this, 5);',
  22141. ' $mod.THelper.SetItems.call(this, 6, false);',
  22142. ' return Result;',
  22143. ' };',
  22144. ' this.SetItems = function (Index, Value) {',
  22145. ' };',
  22146. '});',
  22147. 'this.w = 0;',
  22148. 'this.b = false;',
  22149. '']),
  22150. LinesToStr([ // $mod.$main
  22151. '$mod.b = $mod.THelper.GetItems.call({',
  22152. ' p: $mod,',
  22153. ' get: function () {',
  22154. ' return this.p.w;',
  22155. ' },',
  22156. ' set: function (v) {',
  22157. ' this.p.w = v;',
  22158. ' }',
  22159. '}, 1);',
  22160. '$mod.THelper.SetItems.call({',
  22161. ' p: $mod,',
  22162. ' get: function () {',
  22163. ' return this.p.w;',
  22164. ' },',
  22165. ' set: function (v) {',
  22166. ' this.p.w = v;',
  22167. ' }',
  22168. '}, 2, $mod.b);',
  22169. 'var $with1 = $mod.w;',
  22170. '$mod.b = $mod.THelper.GetItems.call({',
  22171. ' get: function () {',
  22172. ' return $with1;',
  22173. ' },',
  22174. ' set: function (v) {',
  22175. ' $with1 = v;',
  22176. ' }',
  22177. '}, 3);',
  22178. '$mod.THelper.SetItems.call({',
  22179. ' get: function () {',
  22180. ' return $with1;',
  22181. ' },',
  22182. ' set: function (v) {',
  22183. ' $with1 = v;',
  22184. ' }',
  22185. '}, 4, $mod.b);',
  22186. '']));
  22187. end;
  22188. procedure TTestModule.TestTypeHelper_ClassProperty;
  22189. begin
  22190. StartProgram(false);
  22191. Add([
  22192. '{$modeswitch typehelpers}',
  22193. 'type',
  22194. ' THelper = type helper for word',
  22195. ' class function GetSize: longint; static;',
  22196. ' class procedure SetSize(Value: longint); static;',
  22197. ' class property Size: longint read GetSize write SetSize;',
  22198. ' end;',
  22199. 'class function THelper.GetSize: longint;',
  22200. 'begin',
  22201. ' Result:=Size+1;',
  22202. ' Size:=2;',
  22203. 'end;',
  22204. 'class procedure THelper.SetSize(Value: longint);',
  22205. 'begin',
  22206. 'end;',
  22207. 'begin',
  22208. '']);
  22209. ConvertProgram;
  22210. CheckSource('TestTypeHelper_ClassProperty',
  22211. LinesToStr([ // statements
  22212. 'rtl.createHelper($mod, "THelper", null, function () {',
  22213. ' this.GetSize = function () {',
  22214. ' var Result = 0;',
  22215. ' Result = $mod.THelper.GetSize() + 1;',
  22216. ' $mod.THelper.SetSize(2);',
  22217. ' return Result;',
  22218. ' };',
  22219. ' this.SetSize = function (Value) {',
  22220. ' };',
  22221. '});',
  22222. '']),
  22223. LinesToStr([ // $mod.$main
  22224. '']));
  22225. end;
  22226. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  22227. begin
  22228. StartProgram(false);
  22229. Add([
  22230. '{$modeswitch typehelpers}',
  22231. 'type',
  22232. ' THelper = type helper for word',
  22233. ' class function GetItems(Index: byte): boolean; static;',
  22234. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  22235. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  22236. ' end;',
  22237. 'class function THelper.GetItems(Index: byte): boolean;',
  22238. 'begin',
  22239. ' Result:=Items[1];',
  22240. ' Items[2]:=false;',
  22241. 'end;',
  22242. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  22243. 'begin',
  22244. 'end;',
  22245. 'var',
  22246. ' w: word;',
  22247. ' b: boolean;',
  22248. 'begin',
  22249. ' b:=w.Items[1];',
  22250. ' w.Items[2]:=b;',
  22251. ' with w do begin',
  22252. ' b:=Items[3];',
  22253. ' Items[4]:=b;',
  22254. ' end;',
  22255. '']);
  22256. ConvertProgram;
  22257. CheckSource('TestTypeHelper_ClassProperty_Array',
  22258. LinesToStr([ // statements
  22259. 'rtl.createHelper($mod, "THelper", null, function () {',
  22260. ' this.GetItems = function (Index) {',
  22261. ' var Result = false;',
  22262. ' Result = $mod.THelper.GetItems(1);',
  22263. ' $mod.THelper.SetItems(2, false);',
  22264. ' return Result;',
  22265. ' };',
  22266. ' this.SetItems = function (Index, Value) {',
  22267. ' };',
  22268. '});',
  22269. 'this.w = 0;',
  22270. 'this.b = false;',
  22271. '']),
  22272. LinesToStr([ // $mod.$main
  22273. '$mod.b = $mod.THelper.GetItems(1);',
  22274. '$mod.THelper.SetItems(2, $mod.b);',
  22275. 'var $with1 = $mod.w;',
  22276. '$mod.b = $mod.THelper.GetItems(3);',
  22277. '$mod.THelper.SetItems(4, $mod.b);',
  22278. '']));
  22279. end;
  22280. procedure TTestModule.TestTypeHelper_ClassMethod;
  22281. begin
  22282. StartProgram(false);
  22283. Add([
  22284. '{$modeswitch typehelpers}',
  22285. 'type',
  22286. ' THelper = type helper for word',
  22287. ' class procedure DoStatic; static;',
  22288. ' end;',
  22289. 'class procedure THelper.DoStatic;',
  22290. 'begin',
  22291. ' DoStatic;',
  22292. ' DoStatic();',
  22293. 'end;',
  22294. 'var w: word;',
  22295. 'begin',
  22296. ' w.DoStatic;',
  22297. ' w.DoStatic();',
  22298. '']);
  22299. ConvertProgram;
  22300. CheckSource('TestTypeHelper_ClassMethod',
  22301. LinesToStr([ // statements
  22302. 'rtl.createHelper($mod, "THelper", null, function () {',
  22303. ' this.DoStatic = function () {',
  22304. ' $mod.THelper.DoStatic();',
  22305. ' $mod.THelper.DoStatic();',
  22306. ' };',
  22307. '});',
  22308. 'this.w = 0;',
  22309. '']),
  22310. LinesToStr([ // $mod.$main
  22311. '$mod.THelper.DoStatic();',
  22312. '$mod.THelper.DoStatic();',
  22313. '']));
  22314. end;
  22315. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  22316. begin
  22317. StartProgram(false);
  22318. Add([
  22319. '{$modeswitch typehelpers}',
  22320. 'type',
  22321. ' THelper = type helper for word',
  22322. ' procedure Run; external name ''Run'';',
  22323. ' end;',
  22324. 'var w: word;',
  22325. 'begin',
  22326. ' w.Run;',
  22327. '']);
  22328. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  22329. ConvertProgram;
  22330. end;
  22331. procedure TTestModule.TestTypeHelper_Constructor;
  22332. begin
  22333. StartProgram(false);
  22334. Add([
  22335. '{$modeswitch typehelpers}',
  22336. 'type',
  22337. ' THelper = type helper for word',
  22338. ' constructor Init(e: longint);',
  22339. ' end;',
  22340. 'constructor THelper.Init(e: longint);',
  22341. 'begin',
  22342. ' Self:=e;',
  22343. ' Init(e+1);',
  22344. 'end;',
  22345. 'var w: word;',
  22346. 'begin',
  22347. ' w:=word.Init(2);',
  22348. ' w:=w.Init(3);',
  22349. ' with word do w:=Init(4);',
  22350. ' with w do w:=Init(5);',
  22351. '']);
  22352. ConvertProgram;
  22353. CheckSource('TestTypeHelper_Constructor',
  22354. LinesToStr([ // statements
  22355. 'rtl.createHelper($mod, "THelper", null, function () {',
  22356. ' this.Init = function (e) {',
  22357. ' this.set(e);',
  22358. ' $mod.THelper.Init.call(this, e + 1);',
  22359. ' return this.get();',
  22360. ' };',
  22361. ' this.$new = function (fn, args) {',
  22362. ' return this[fn].apply({',
  22363. ' p: 0,',
  22364. ' get: function () {',
  22365. ' return this.p;',
  22366. ' },',
  22367. ' set: function (v) {',
  22368. ' this.p = v;',
  22369. ' }',
  22370. ' }, args);',
  22371. ' };',
  22372. '});',
  22373. 'this.w = 0;',
  22374. '']),
  22375. LinesToStr([ // $mod.$main
  22376. '$mod.w = $mod.THelper.$new("Init", [2]);',
  22377. '$mod.w = $mod.THelper.Init.call({',
  22378. ' p: $mod,',
  22379. ' get: function () {',
  22380. ' return this.p.w;',
  22381. ' },',
  22382. ' set: function (v) {',
  22383. ' this.p.w = v;',
  22384. ' }',
  22385. '}, 3);',
  22386. '$mod.w = $mod.THelper.$new("Init", [4]);',
  22387. 'var $with1 = $mod.w;',
  22388. '$mod.w = $mod.THelper.Init.call({',
  22389. ' get: function () {',
  22390. ' return $with1;',
  22391. ' },',
  22392. ' set: function (v) {',
  22393. ' $with1 = v;',
  22394. ' }',
  22395. '}, 5);',
  22396. '']));
  22397. end;
  22398. procedure TTestModule.TestTypeHelper_Word;
  22399. begin
  22400. StartProgram(false);
  22401. Add([
  22402. '{$modeswitch typehelpers}',
  22403. 'type',
  22404. ' THelper = type helper for word',
  22405. ' procedure DoIt(e: byte = 123);',
  22406. ' end;',
  22407. 'procedure THelper.DoIt(e: byte);',
  22408. 'begin',
  22409. ' Self:=e;',
  22410. ' Self:=Self+1;',
  22411. ' with Self do Doit;',
  22412. 'end;',
  22413. 'begin',
  22414. ' word(3).DoIt;',
  22415. '']);
  22416. ConvertProgram;
  22417. CheckSource('TestTypeHelper_Word',
  22418. LinesToStr([ // statements
  22419. 'rtl.createHelper($mod, "THelper", null, function () {',
  22420. ' this.DoIt = function (e) {',
  22421. ' this.set(e);',
  22422. ' this.set(this.get() + 1);',
  22423. ' var $with1 = this.get();',
  22424. ' $mod.THelper.DoIt.call(this, 123);',
  22425. ' };',
  22426. '});',
  22427. '']),
  22428. LinesToStr([ // $mod.$main
  22429. '$mod.THelper.DoIt.call({',
  22430. ' get: function () {',
  22431. ' return 3;',
  22432. ' },',
  22433. ' set: function (v) {',
  22434. ' rtl.raiseE("EPropReadOnly");',
  22435. ' }',
  22436. '}, 123);',
  22437. '']));
  22438. end;
  22439. procedure TTestModule.TestTypeHelper_Double;
  22440. begin
  22441. StartProgram(false);
  22442. Add([
  22443. '{$modeswitch typehelpers}',
  22444. 'type',
  22445. ' Float = type double;',
  22446. ' THelper = type helper for double',
  22447. ' const NPI = 3.141592;',
  22448. ' function ToStr: String;',
  22449. ' end;',
  22450. 'function THelper.ToStr: String;',
  22451. 'begin',
  22452. 'end;',
  22453. 'procedure DoIt(s: string);',
  22454. 'begin',
  22455. 'end;',
  22456. 'var f: Float;',
  22457. 'begin',
  22458. ' DoIt(f.toStr);',
  22459. ' DoIt(f.toStr());',
  22460. '']);
  22461. ConvertProgram;
  22462. CheckSource('TestTypeHelper_Double',
  22463. LinesToStr([ // statements
  22464. 'rtl.createHelper($mod, "THelper", null, function () {',
  22465. ' this.NPI = 3.141592;',
  22466. ' this.ToStr = function () {',
  22467. ' var Result = "";',
  22468. ' return Result;',
  22469. ' };',
  22470. '});',
  22471. 'this.DoIt = function (s) {',
  22472. '};',
  22473. 'this.f = 0.0;',
  22474. '']),
  22475. LinesToStr([ // $mod.$main
  22476. '$mod.DoIt($mod.THelper.ToStr.call({',
  22477. ' p: $mod,',
  22478. ' get: function () {',
  22479. ' return this.p.f;',
  22480. ' },',
  22481. ' set: function (v) {',
  22482. ' this.p.f = v;',
  22483. ' }',
  22484. '}));',
  22485. '$mod.DoIt($mod.THelper.ToStr.call({',
  22486. ' p: $mod,',
  22487. ' get: function () {',
  22488. ' return this.p.f;',
  22489. ' },',
  22490. ' set: function (v) {',
  22491. ' this.p.f = v;',
  22492. ' }',
  22493. '}));',
  22494. '']));
  22495. end;
  22496. procedure TTestModule.TestTypeHelper_StringChar;
  22497. begin
  22498. StartProgram(false);
  22499. Add([
  22500. '{$modeswitch typehelpers}',
  22501. 'type',
  22502. ' TStringHelper = type helper for string',
  22503. ' procedure DoIt(e: byte = 123);',
  22504. ' end;',
  22505. ' TCharHelper = type helper for char',
  22506. ' procedure Fly;',
  22507. ' end;',
  22508. 'procedure TStringHelper.DoIt(e: byte);',
  22509. 'begin',
  22510. ' Self[1]:=''c'';',
  22511. ' Self[2]:=Self[3];',
  22512. 'end;',
  22513. 'procedure TCharHelper.Fly;',
  22514. 'begin',
  22515. ' Self:=''c'';',
  22516. 'end;',
  22517. 'begin',
  22518. ' ''abc''.DoIt;',
  22519. ' ''xyz''.DoIt();',
  22520. ' ''c''.Fly();',
  22521. '']);
  22522. ConvertProgram;
  22523. CheckSource('TestTypeHelper_StringChar',
  22524. LinesToStr([ // statements
  22525. 'rtl.createHelper($mod, "TStringHelper", null, function () {',
  22526. ' this.DoIt = function (e) {',
  22527. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  22528. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  22529. ' };',
  22530. '});',
  22531. 'rtl.createHelper($mod, "TCharHelper", null, function () {',
  22532. ' this.Fly = function () {',
  22533. ' this.set("c");',
  22534. ' };',
  22535. '});',
  22536. '']),
  22537. LinesToStr([ // $mod.$main
  22538. '$mod.TStringHelper.DoIt.call({',
  22539. ' get: function () {',
  22540. ' return "abc";',
  22541. ' },',
  22542. ' set: function (v) {',
  22543. ' rtl.raiseE("EPropReadOnly");',
  22544. ' }',
  22545. '}, 123);',
  22546. '$mod.TStringHelper.DoIt.call({',
  22547. ' get: function () {',
  22548. ' return "xyz";',
  22549. ' },',
  22550. ' set: function (v) {',
  22551. ' rtl.raiseE("EPropReadOnly");',
  22552. ' }',
  22553. '}, 123);',
  22554. '$mod.TCharHelper.Fly.call({',
  22555. ' get: function () {',
  22556. ' return "c";',
  22557. ' },',
  22558. ' set: function (v) {',
  22559. ' rtl.raiseE("EPropReadOnly");',
  22560. ' }',
  22561. '});',
  22562. '']));
  22563. end;
  22564. procedure TTestModule.TestTypeHelper_Array;
  22565. begin
  22566. StartProgram(false);
  22567. Add([
  22568. '{$modeswitch typehelpers}',
  22569. 'type',
  22570. ' TArrOfBool = array of boolean;',
  22571. ' TArrOfJS = array of jsvalue;',
  22572. ' THelper = type helper for TArrOfBool',
  22573. ' procedure DoIt(e: byte = 123);',
  22574. ' end;',
  22575. 'procedure THelper.DoIt(e: byte);',
  22576. 'begin',
  22577. ' Self[1]:=true;',
  22578. ' Self[2]:=not Self[3];',
  22579. ' SetLength(Self,4);',
  22580. 'end;',
  22581. 'var',
  22582. ' b: TArrOfBool;',
  22583. ' j: TArrOfJS;',
  22584. 'begin',
  22585. ' b.DoIt;',
  22586. ' TArrOfBool(j).DoIt();',
  22587. '']);
  22588. ConvertProgram;
  22589. CheckSource('TestTypeHelper_Array',
  22590. LinesToStr([ // statements
  22591. 'rtl.createHelper($mod, "THelper", null, function () {',
  22592. ' this.DoIt = function (e) {',
  22593. ' this.get()[1] = true;',
  22594. ' this.get()[2] = !this.get()[3];',
  22595. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  22596. ' };',
  22597. '});',
  22598. 'this.b = [];',
  22599. 'this.j = [];',
  22600. '']),
  22601. LinesToStr([ // $mod.$main
  22602. '$mod.THelper.DoIt.call({',
  22603. ' p: $mod,',
  22604. ' get: function () {',
  22605. ' return this.p.b;',
  22606. ' },',
  22607. ' set: function (v) {',
  22608. ' this.p.b = v;',
  22609. ' }',
  22610. '}, 123);',
  22611. '$mod.THelper.DoIt.call({',
  22612. ' p: $mod,',
  22613. ' get: function () {',
  22614. ' return this.p.j;',
  22615. ' },',
  22616. ' set: function (v) {',
  22617. ' this.p.j = v;',
  22618. ' }',
  22619. '}, 123);',
  22620. '']));
  22621. end;
  22622. procedure TTestModule.TestTypeHelper_EnumType;
  22623. begin
  22624. StartProgram(false);
  22625. Add([
  22626. '{$modeswitch typehelpers}',
  22627. 'type',
  22628. ' TEnum = (red,blue);',
  22629. ' THelper = type helper for TEnum',
  22630. ' procedure DoIt(e: byte = 123);',
  22631. ' class procedure Swing(w: word); static;',
  22632. ' end;',
  22633. 'procedure THelper.DoIt(e: byte);',
  22634. 'begin',
  22635. ' Self:=red;',
  22636. ' Self:=succ(Self);',
  22637. ' with Self do Doit;',
  22638. 'end;',
  22639. 'class procedure THelper.Swing(w: word);',
  22640. 'begin',
  22641. 'end;',
  22642. 'var e: TEnum;',
  22643. 'begin',
  22644. ' e.DoIt;',
  22645. ' red.DoIt;',
  22646. ' TEnum.blue.DoIt;',
  22647. ' TEnum(1).DoIt;',
  22648. ' TEnum.Swing(3);',
  22649. '']);
  22650. ConvertProgram;
  22651. CheckSource('TestTypeHelper_EnumType',
  22652. LinesToStr([ // statements
  22653. 'this.TEnum = {',
  22654. ' "0": "red",',
  22655. ' red: 0,',
  22656. ' "1": "blue",',
  22657. ' blue: 1',
  22658. '};',
  22659. 'rtl.createHelper($mod, "THelper", null, function () {',
  22660. ' this.DoIt = function (e) {',
  22661. ' this.set($mod.TEnum.red);',
  22662. ' this.set(this.get() + 1);',
  22663. ' var $with1 = this.get();',
  22664. ' $mod.THelper.DoIt.call(this, 123);',
  22665. ' };',
  22666. ' this.Swing = function (w) {',
  22667. ' };',
  22668. '});',
  22669. 'this.e = 0;',
  22670. '']),
  22671. LinesToStr([ // $mod.$main
  22672. '$mod.THelper.DoIt.call({',
  22673. ' p: $mod,',
  22674. ' get: function () {',
  22675. ' return this.p.e;',
  22676. ' },',
  22677. ' set: function (v) {',
  22678. ' this.p.e = v;',
  22679. ' }',
  22680. '}, 123);',
  22681. '$mod.THelper.DoIt.call({',
  22682. ' p: $mod.TEnum,',
  22683. ' get: function () {',
  22684. ' return this.p.red;',
  22685. ' },',
  22686. ' set: function (v) {',
  22687. ' rtl.raiseE("EPropReadOnly");',
  22688. ' }',
  22689. '}, 123);',
  22690. '$mod.THelper.DoIt.call({',
  22691. ' p: $mod.TEnum,',
  22692. ' get: function () {',
  22693. ' return this.p.blue;',
  22694. ' },',
  22695. ' set: function (v) {',
  22696. ' rtl.raiseE("EPropReadOnly");',
  22697. ' }',
  22698. '}, 123);',
  22699. '$mod.THelper.DoIt.call({',
  22700. ' get: function () {',
  22701. ' return 1;',
  22702. ' },',
  22703. ' set: function (v) {',
  22704. ' rtl.raiseE("EPropReadOnly");',
  22705. ' }',
  22706. '}, 123);',
  22707. '$mod.THelper.Swing(3);',
  22708. '']));
  22709. end;
  22710. procedure TTestModule.TestTypeHelper_SetType;
  22711. begin
  22712. StartProgram(false);
  22713. Add([
  22714. '{$modeswitch typehelpers}',
  22715. 'type',
  22716. ' TEnum = (red,blue);',
  22717. ' TSetOfEnum = set of TEnum;',
  22718. ' THelper = type helper for TSetOfEnum',
  22719. ' procedure DoIt(e: byte = 123);',
  22720. ' constructor Init(e: TEnum);',
  22721. ' constructor InitEmpty;',
  22722. ' end;',
  22723. 'procedure THelper.DoIt(e: byte);',
  22724. 'begin',
  22725. ' Self:=[];',
  22726. ' Self:=[red];',
  22727. ' Include(Self,blue);',
  22728. 'end;',
  22729. 'constructor THelper.Init(e: TEnum);',
  22730. 'begin',
  22731. ' Self:=[];',
  22732. ' Self:=[e];',
  22733. ' Include(Self,blue);',
  22734. 'end;',
  22735. 'constructor THelper.InitEmpty;',
  22736. 'begin',
  22737. 'end;',
  22738. 'var s: TSetOfEnum;',
  22739. 'begin',
  22740. ' s.DoIt;',
  22741. //' [red].DoIt;',
  22742. //' with s do DoIt;',
  22743. //' with [red,blue] do DoIt;',
  22744. ' s:=TSetOfEnum.Init(blue);',
  22745. ' s:=s.Init(blue);',
  22746. '']);
  22747. ConvertProgram;
  22748. CheckSource('TestTypeHelper_SetType',
  22749. LinesToStr([ // statements
  22750. 'this.TEnum = {',
  22751. ' "0": "red",',
  22752. ' red: 0,',
  22753. ' "1": "blue",',
  22754. ' blue: 1',
  22755. '};',
  22756. 'rtl.createHelper($mod, "THelper", null, function () {',
  22757. ' this.DoIt = function (e) {',
  22758. ' this.set({});',
  22759. ' this.set(rtl.createSet($mod.TEnum.red));',
  22760. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  22761. ' };',
  22762. ' this.Init = function (e) {',
  22763. ' this.set({});',
  22764. ' this.set(rtl.createSet(e));',
  22765. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  22766. ' return this.get();',
  22767. ' };',
  22768. ' this.InitEmpty = function () {',
  22769. ' return this.get();',
  22770. ' };',
  22771. ' this.$new = function (fn, args) {',
  22772. ' return this[fn].apply({',
  22773. ' p: {},',
  22774. ' get: function () {',
  22775. ' return this.p;',
  22776. ' },',
  22777. ' set: function (v) {',
  22778. ' this.p = v;',
  22779. ' }',
  22780. ' }, args);',
  22781. ' };',
  22782. '});',
  22783. 'this.s = {};',
  22784. '']),
  22785. LinesToStr([ // $mod.$main
  22786. '$mod.THelper.DoIt.call({',
  22787. ' p: $mod,',
  22788. ' get: function () {',
  22789. ' return this.p.s;',
  22790. ' },',
  22791. ' set: function (v) {',
  22792. ' this.p.s = v;',
  22793. ' }',
  22794. '}, 123);',
  22795. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  22796. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  22797. ' p: $mod,',
  22798. ' get: function () {',
  22799. ' return this.p.s;',
  22800. ' },',
  22801. ' set: function (v) {',
  22802. ' this.p.s = v;',
  22803. ' }',
  22804. '}, $mod.TEnum.blue));',
  22805. '']));
  22806. end;
  22807. procedure TTestModule.TestProcType;
  22808. begin
  22809. StartProgram(false);
  22810. Add([
  22811. 'type',
  22812. ' TProcInt = procedure(vI: longint = 1);',
  22813. 'procedure DoIt(vJ: longint);',
  22814. 'begin end;',
  22815. 'var',
  22816. ' b: boolean;',
  22817. ' vP, vQ: tprocint;',
  22818. 'begin',
  22819. ' vp:=nil;',
  22820. ' vp:=vp;',
  22821. ' vp:=@doit;',
  22822. ' vp;',
  22823. ' vp();',
  22824. ' vp(2);',
  22825. ' b:=vp=nil;',
  22826. ' b:=nil=vp;',
  22827. ' b:=vp=vq;',
  22828. ' b:=vp=@doit;',
  22829. ' b:=@doit=vp;',
  22830. ' b:=vp<>nil;',
  22831. ' b:=nil<>vp;',
  22832. ' b:=vp<>vq;',
  22833. ' b:=vp<>@doit;',
  22834. ' b:=@doit<>vp;',
  22835. ' b:=Assigned(vp);',
  22836. ' if Assigned(vp) then ;']);
  22837. ConvertProgram;
  22838. CheckSource('TestProcType',
  22839. LinesToStr([ // statements
  22840. 'this.DoIt = function(vJ) {',
  22841. '};',
  22842. 'this.b = false;',
  22843. 'this.vP = null;',
  22844. 'this.vQ = null;'
  22845. ]),
  22846. LinesToStr([ // $mod.$main
  22847. '$mod.vP = null;',
  22848. '$mod.vP = $mod.vP;',
  22849. '$mod.vP = $mod.DoIt;',
  22850. '$mod.vP(1);',
  22851. '$mod.vP(1);',
  22852. '$mod.vP(2);',
  22853. '$mod.b = $mod.vP === null;',
  22854. '$mod.b = null === $mod.vP;',
  22855. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  22856. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  22857. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  22858. '$mod.b = $mod.vP !== null;',
  22859. '$mod.b = null !== $mod.vP;',
  22860. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  22861. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  22862. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  22863. '$mod.b = $mod.vP != null;',
  22864. 'if ($mod.vP != null) ;',
  22865. '']));
  22866. end;
  22867. procedure TTestModule.TestProcType_Arg;
  22868. begin
  22869. StartProgram(false);
  22870. Add([
  22871. 'type',
  22872. ' TProcInt = procedure(vI: longint = 1);',
  22873. 'procedure DoIt(vJ: longint); begin end;',
  22874. 'procedure DoSome(vP, vQ: TProcInt);',
  22875. 'var',
  22876. ' b: boolean;',
  22877. 'begin',
  22878. ' vp:=nil;',
  22879. ' vp:=vp;',
  22880. ' vp:=@doit;',
  22881. ' vp;',
  22882. ' vp();',
  22883. ' vp(2);',
  22884. ' b:=vp=nil;',
  22885. ' b:=nil=vp;',
  22886. ' b:=vp=vq;',
  22887. ' b:=vp=@doit;',
  22888. ' b:=@doit=vp;',
  22889. ' b:=vp<>nil;',
  22890. ' b:=nil<>vp;',
  22891. ' b:=vp<>vq;',
  22892. ' b:=vp<>@doit;',
  22893. ' b:=@doit<>vp;',
  22894. ' b:=Assigned(vp);',
  22895. ' if Assigned(vp) then ;',
  22896. 'end;',
  22897. 'begin',
  22898. ' DoSome(@DoIt,nil);']);
  22899. ConvertProgram;
  22900. CheckSource('TestProcType_Arg',
  22901. LinesToStr([ // statements
  22902. 'this.DoIt = function(vJ) {',
  22903. '};',
  22904. 'this.DoSome = function(vP, vQ) {',
  22905. ' var b = false;',
  22906. ' vP = null;',
  22907. ' vP = vP;',
  22908. ' vP = $mod.DoIt;',
  22909. ' vP(1);',
  22910. ' vP(1);',
  22911. ' vP(2);',
  22912. ' b = vP === null;',
  22913. ' b = null === vP;',
  22914. ' b = rtl.eqCallback(vP,vQ);',
  22915. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  22916. ' b = rtl.eqCallback($mod.DoIt, vP);',
  22917. ' b = vP !== null;',
  22918. ' b = null !== vP;',
  22919. ' b = !rtl.eqCallback(vP, vQ);',
  22920. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  22921. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  22922. ' b = vP != null;',
  22923. ' if (vP != null) ;',
  22924. '};',
  22925. '']),
  22926. LinesToStr([ // $mod.$main
  22927. '$mod.DoSome($mod.DoIt,null);',
  22928. '']));
  22929. end;
  22930. procedure TTestModule.TestProcType_FunctionFPC;
  22931. begin
  22932. StartProgram(false);
  22933. Add('type');
  22934. Add(' TFuncInt = function(vA: longint = 1): longint;');
  22935. Add('function DoIt(vI: longint): longint;');
  22936. Add('begin end;');
  22937. Add('var');
  22938. Add(' b: boolean;');
  22939. Add(' vP, vQ: tfuncint;');
  22940. Add('begin');
  22941. Add(' vp:=nil;');
  22942. Add(' vp:=vp;');
  22943. Add(' vp:=@doit;'); // ok in fpc and delphi
  22944. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  22945. Add(' vp;'); // ok in fpc and delphi
  22946. Add(' vp();');
  22947. Add(' vp(2);');
  22948. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  22949. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  22950. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  22951. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  22952. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  22953. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  22954. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  22955. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  22956. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  22957. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  22958. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  22959. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  22960. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  22961. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  22962. Add(' b:=Assigned(vp);');
  22963. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  22964. Add(' doit(vp());'); // ok in fpc and delphi
  22965. Add(' doit(vp(2));'); // ok in fpc and delphi
  22966. ConvertProgram;
  22967. CheckSource('TestProcType_FunctionFPC',
  22968. LinesToStr([ // statements
  22969. 'this.DoIt = function(vI) {',
  22970. ' var Result = 0;',
  22971. ' return Result;',
  22972. '};',
  22973. 'this.b = false;',
  22974. 'this.vP = null;',
  22975. 'this.vQ = null;'
  22976. ]),
  22977. LinesToStr([ // $mod.$main
  22978. '$mod.vP = null;',
  22979. '$mod.vP = $mod.vP;',
  22980. '$mod.vP = $mod.DoIt;',
  22981. '$mod.vP(1);',
  22982. '$mod.vP(1);',
  22983. '$mod.vP(2);',
  22984. '$mod.b = $mod.vP === null;',
  22985. '$mod.b = null === $mod.vP;',
  22986. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  22987. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  22988. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  22989. '$mod.b = 4 === $mod.vP(1);',
  22990. '$mod.b = $mod.vP !== null;',
  22991. '$mod.b = null !== $mod.vP;',
  22992. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  22993. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  22994. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  22995. '$mod.b = 6 !== $mod.vP(1);',
  22996. '$mod.b = $mod.vP != null;',
  22997. '$mod.DoIt($mod.vP(1));',
  22998. '$mod.DoIt($mod.vP(2));',
  22999. '']));
  23000. end;
  23001. procedure TTestModule.TestProcType_FunctionDelphi;
  23002. begin
  23003. StartProgram(false);
  23004. Add('{$mode Delphi}');
  23005. Add('type');
  23006. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23007. Add('function DoIt(vI: longint): longint;');
  23008. Add('begin end;');
  23009. Add('var');
  23010. Add(' b: boolean;');
  23011. Add(' vP, vQ: tfuncint;');
  23012. Add('begin');
  23013. Add(' vp:=nil;');
  23014. Add(' vp:=vp;');
  23015. Add(' vp:=@doit;'); // ok in fpc and delphi
  23016. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23017. Add(' vp;'); // ok in fpc and delphi
  23018. Add(' vp();');
  23019. Add(' vp(2);');
  23020. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23021. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23022. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23023. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23024. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23025. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  23026. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  23027. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23028. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23029. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23030. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23031. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23032. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  23033. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  23034. Add(' b:=Assigned(vp);');
  23035. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  23036. Add(' doit(vp());'); // ok in fpc and delphi
  23037. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  23038. ConvertProgram;
  23039. CheckSource('TestProcType_FunctionDelphi',
  23040. LinesToStr([ // statements
  23041. 'this.DoIt = function(vI) {',
  23042. ' var Result = 0;',
  23043. ' return Result;',
  23044. '};',
  23045. 'this.b = false;',
  23046. 'this.vP = null;',
  23047. 'this.vQ = null;'
  23048. ]),
  23049. LinesToStr([ // $mod.$main
  23050. '$mod.vP = null;',
  23051. '$mod.vP = $mod.vP;',
  23052. '$mod.vP = $mod.DoIt;',
  23053. '$mod.vP = $mod.DoIt;',
  23054. '$mod.vP(1);',
  23055. '$mod.vP(1);',
  23056. '$mod.vP(2);',
  23057. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  23058. '$mod.b = $mod.vP(1) === 3;',
  23059. '$mod.b = 4 === $mod.vP(1);',
  23060. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  23061. '$mod.b = $mod.vP(1) !== 5;',
  23062. '$mod.b = 6 !== $mod.vP(1);',
  23063. '$mod.b = $mod.vP != null;',
  23064. '$mod.DoIt($mod.vP(1));',
  23065. '$mod.DoIt($mod.vP(1));',
  23066. '$mod.DoIt($mod.vP(2));',
  23067. '']));
  23068. end;
  23069. procedure TTestModule.TestProcType_ProcedureDelphi;
  23070. begin
  23071. StartProgram(false);
  23072. Add('{$mode Delphi}');
  23073. Add('type');
  23074. Add(' TProc = procedure;');
  23075. Add('procedure DoIt;');
  23076. Add('begin end;');
  23077. Add('var');
  23078. Add(' b: boolean;');
  23079. Add(' vP, vQ: tproc;');
  23080. Add('begin');
  23081. Add(' vp:=nil;');
  23082. Add(' vp:=vp;');
  23083. Add(' vp:=vq;');
  23084. 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
  23085. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23086. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  23087. Add(' vp;'); // ok in fpc and delphi
  23088. Add(' vp();');
  23089. // equal
  23090. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23091. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  23092. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23093. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  23094. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  23095. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23096. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23097. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  23098. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23099. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  23100. // unequal
  23101. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23102. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  23103. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23104. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  23105. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23106. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  23107. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23108. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  23109. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23110. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  23111. Add(' b:=Assigned(vp);');
  23112. ConvertProgram;
  23113. CheckSource('TestProcType_ProcedureDelphi',
  23114. LinesToStr([ // statements
  23115. 'this.DoIt = function() {',
  23116. '};',
  23117. 'this.b = false;',
  23118. 'this.vP = null;',
  23119. 'this.vQ = null;'
  23120. ]),
  23121. LinesToStr([ // $mod.$main
  23122. '$mod.vP = null;',
  23123. '$mod.vP = $mod.vP;',
  23124. '$mod.vP = $mod.vQ;',
  23125. '$mod.vP = $mod.DoIt;',
  23126. '$mod.vP = $mod.DoIt;',
  23127. '$mod.vP();',
  23128. '$mod.vP();',
  23129. '$mod.b = $mod.vP === null;',
  23130. '$mod.b = null === $mod.vP;',
  23131. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  23132. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23133. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23134. '$mod.b = $mod.vP !== null;',
  23135. '$mod.b = null !== $mod.vP;',
  23136. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  23137. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23138. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23139. '$mod.b = $mod.vP != null;',
  23140. '']));
  23141. end;
  23142. procedure TTestModule.TestProcType_AsParam;
  23143. begin
  23144. StartProgram(false);
  23145. Add('type');
  23146. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23147. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  23148. Add('var vJ: tfuncint;');
  23149. Add('begin');
  23150. Add(' vg:=vg;');
  23151. Add(' vj:=vh;');
  23152. Add(' vi:=vi;');
  23153. Add(' doit(vg,vg,vg);');
  23154. Add(' doit(vh,vh,vj);');
  23155. Add(' doit(vi,vi,vi);');
  23156. Add(' doit(vj,vj,vj);');
  23157. Add('end;');
  23158. Add('var i: tfuncint;');
  23159. Add('begin');
  23160. Add(' doit(i,i,i);');
  23161. ConvertProgram;
  23162. CheckSource('TestProcType_AsParam',
  23163. LinesToStr([ // statements
  23164. 'this.DoIt = function (vG,vH,vI) {',
  23165. ' var vJ = null;',
  23166. ' vG = vG;',
  23167. ' vJ = vH;',
  23168. ' vI.set(vI.get());',
  23169. ' $mod.DoIt(vG, vG, {',
  23170. ' get: function () {',
  23171. ' return vG;',
  23172. ' },',
  23173. ' set: function (v) {',
  23174. ' vG = v;',
  23175. ' }',
  23176. ' });',
  23177. ' $mod.DoIt(vH, vH, {',
  23178. ' get: function () {',
  23179. ' return vJ;',
  23180. ' },',
  23181. ' set: function (v) {',
  23182. ' vJ = v;',
  23183. ' }',
  23184. ' });',
  23185. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  23186. ' $mod.DoIt(vJ, vJ, {',
  23187. ' get: function () {',
  23188. ' return vJ;',
  23189. ' },',
  23190. ' set: function (v) {',
  23191. ' vJ = v;',
  23192. ' }',
  23193. ' });',
  23194. '};',
  23195. 'this.i = null;'
  23196. ]),
  23197. LinesToStr([
  23198. '$mod.DoIt($mod.i,$mod.i,{',
  23199. ' p: $mod,',
  23200. ' get: function () {',
  23201. ' return this.p.i;',
  23202. ' },',
  23203. ' set: function (v) {',
  23204. ' this.p.i = v;',
  23205. ' }',
  23206. '});'
  23207. ]));
  23208. end;
  23209. procedure TTestModule.TestProcType_MethodFPC;
  23210. begin
  23211. StartProgram(false);
  23212. Add('type');
  23213. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23214. Add(' TObject = class');
  23215. Add(' function DoIt(vA: longint = 1): longint;');
  23216. Add(' end;');
  23217. Add('function TObject.DoIt(vA: longint = 1): longint;');
  23218. Add('begin');
  23219. Add('end;');
  23220. Add('var');
  23221. Add(' Obj: TObject;');
  23222. Add(' vP: tfuncint;');
  23223. Add(' b: boolean;');
  23224. Add('begin');
  23225. Add(' vp:[email protected];'); // ok in fpc and delphi
  23226. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  23227. Add(' vp;'); // ok in fpc and delphi
  23228. Add(' vp();');
  23229. Add(' vp(2);');
  23230. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  23231. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  23232. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  23233. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  23234. ConvertProgram;
  23235. CheckSource('TestProcType_MethodFPC',
  23236. LinesToStr([ // statements
  23237. 'rtl.createClass($mod, "TObject", null, function () {',
  23238. ' this.$init = function () {',
  23239. ' };',
  23240. ' this.$final = function () {',
  23241. ' };',
  23242. ' this.DoIt = function (vA) {',
  23243. ' var Result = 0;',
  23244. ' return Result;',
  23245. ' };',
  23246. '});',
  23247. 'this.Obj = null;',
  23248. 'this.vP = null;',
  23249. 'this.b = false;'
  23250. ]),
  23251. LinesToStr([
  23252. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  23253. '$mod.vP(1);',
  23254. '$mod.vP(1);',
  23255. '$mod.vP(2);',
  23256. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  23257. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  23258. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  23259. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  23260. '']));
  23261. end;
  23262. procedure TTestModule.TestProcType_MethodDelphi;
  23263. begin
  23264. StartProgram(false);
  23265. Add('{$mode delphi}');
  23266. Add('type');
  23267. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23268. Add(' TObject = class');
  23269. Add(' function DoIt(vA: longint = 1): longint;');
  23270. Add(' end;');
  23271. Add('function TObject.DoIt(vA: longint = 1): longint;');
  23272. Add('begin');
  23273. Add('end;');
  23274. Add('var');
  23275. Add(' Obj: TObject;');
  23276. Add(' vP: tfuncint;');
  23277. Add(' b: boolean;');
  23278. Add('begin');
  23279. Add(' vp:[email protected];'); // ok in fpc and delphi
  23280. Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  23281. Add(' vp;'); // ok in fpc and delphi
  23282. Add(' vp();');
  23283. Add(' vp(2);');
  23284. //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  23285. //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  23286. //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  23287. //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  23288. ConvertProgram;
  23289. CheckSource('TestProcType_MethodDelphi',
  23290. LinesToStr([ // statements
  23291. 'rtl.createClass($mod, "TObject", null, function () {',
  23292. ' this.$init = function () {',
  23293. ' };',
  23294. ' this.$final = function () {',
  23295. ' };',
  23296. ' this.DoIt = function (vA) {',
  23297. ' var Result = 0;',
  23298. ' return Result;',
  23299. ' };',
  23300. '});',
  23301. 'this.Obj = null;',
  23302. 'this.vP = null;',
  23303. 'this.b = false;'
  23304. ]),
  23305. LinesToStr([
  23306. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  23307. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  23308. '$mod.vP(1);',
  23309. '$mod.vP(1);',
  23310. '$mod.vP(2);',
  23311. '']));
  23312. end;
  23313. procedure TTestModule.TestProcType_PropertyFPC;
  23314. begin
  23315. StartProgram(false);
  23316. Add('type');
  23317. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23318. Add(' TObject = class');
  23319. Add(' FOnFoo: TFuncInt;');
  23320. Add(' function DoIt(vA: longint = 1): longint;');
  23321. Add(' function GetFoo: TFuncInt;');
  23322. Add(' procedure SetFoo(const Value: TFuncInt);');
  23323. Add(' function GetEvents(Index: longint): TFuncInt;');
  23324. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  23325. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  23326. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  23327. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  23328. Add(' end;');
  23329. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  23330. Add('function tobject.getfoo: tfuncint; begin end;');
  23331. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  23332. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  23333. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  23334. Add('var');
  23335. Add(' Obj: TObject;');
  23336. Add(' vP: tfuncint;');
  23337. Add(' b: boolean;');
  23338. Add('begin');
  23339. Add(' obj.onfoo:=nil;');
  23340. Add(' obj.onbar:=nil;');
  23341. Add(' obj.events[1]:=nil;');
  23342. Add(' obj.onfoo:=obj.onfoo;');
  23343. Add(' obj.onbar:=obj.onbar;');
  23344. Add(' obj.events[2]:=obj.events[3];');
  23345. Add(' obj.onfoo:[email protected];');
  23346. Add(' obj.onbar:[email protected];');
  23347. Add(' obj.events[4]:[email protected];');
  23348. //Add(' obj.onfoo:=obj.doit;'); // delphi
  23349. //Add(' obj.onbar:=obj.doit;'); // delphi
  23350. //Add(' obj.events[4]:=obj.doit;'); // delphi
  23351. Add(' obj.onfoo;');
  23352. Add(' obj.onbar;');
  23353. //Add(' obj.events[5];'); ToDo in pasresolver
  23354. Add(' obj.onfoo();');
  23355. Add(' obj.onbar();');
  23356. Add(' obj.events[6]();');
  23357. Add(' b:=obj.onfoo=nil;');
  23358. Add(' b:=obj.onbar=nil;');
  23359. Add(' b:=obj.events[7]=nil;');
  23360. Add(' b:=obj.onfoo<>nil;');
  23361. Add(' b:=obj.onbar<>nil;');
  23362. Add(' b:=obj.events[8]<>nil;');
  23363. Add(' b:=obj.onfoo=vp;');
  23364. Add(' b:=obj.onbar=vp;');
  23365. Add(' b:=obj.events[9]=vp;');
  23366. Add(' b:=obj.onfoo=obj.onfoo;');
  23367. Add(' b:=obj.onbar=obj.onfoo;');
  23368. Add(' b:=obj.events[10]=obj.onfoo;');
  23369. Add(' b:=obj.onfoo<>obj.onfoo;');
  23370. Add(' b:=obj.onbar<>obj.onfoo;');
  23371. Add(' b:=obj.events[11]<>obj.onfoo;');
  23372. Add(' b:[email protected];');
  23373. Add(' b:[email protected];');
  23374. Add(' b:=obj.events[12][email protected];');
  23375. Add(' b:=obj.onfoo<>@obj.doit;');
  23376. Add(' b:=obj.onbar<>@obj.doit;');
  23377. Add(' b:=obj.events[12]<>@obj.doit;');
  23378. Add(' b:=Assigned(obj.onfoo);');
  23379. Add(' b:=Assigned(obj.onbar);');
  23380. Add(' b:=Assigned(obj.events[13]);');
  23381. ConvertProgram;
  23382. CheckSource('TestProcType_PropertyFPC',
  23383. LinesToStr([ // statements
  23384. 'rtl.createClass($mod, "TObject", null, function () {',
  23385. ' this.$init = function () {',
  23386. ' this.FOnFoo = null;',
  23387. ' };',
  23388. ' this.$final = function () {',
  23389. ' this.FOnFoo = undefined;',
  23390. ' };',
  23391. ' this.DoIt = function (vA) {',
  23392. ' var Result = 0;',
  23393. ' return Result;',
  23394. ' };',
  23395. 'this.GetFoo = function () {',
  23396. ' var Result = null;',
  23397. ' return Result;',
  23398. '};',
  23399. 'this.SetFoo = function (Value) {',
  23400. '};',
  23401. 'this.GetEvents = function (Index) {',
  23402. ' var Result = null;',
  23403. ' return Result;',
  23404. '};',
  23405. 'this.SetEvents = function (Index, Value) {',
  23406. '};',
  23407. '});',
  23408. 'this.Obj = null;',
  23409. 'this.vP = null;',
  23410. 'this.b = false;'
  23411. ]),
  23412. LinesToStr([
  23413. '$mod.Obj.FOnFoo = null;',
  23414. '$mod.Obj.SetFoo(null);',
  23415. '$mod.Obj.SetEvents(1, null);',
  23416. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  23417. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  23418. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  23419. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  23420. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  23421. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  23422. '$mod.Obj.FOnFoo(1);',
  23423. '$mod.Obj.GetFoo();',
  23424. '$mod.Obj.FOnFoo(1);',
  23425. '$mod.Obj.GetFoo()(1);',
  23426. '$mod.Obj.GetEvents(6)(1);',
  23427. '$mod.b = $mod.Obj.FOnFoo === null;',
  23428. '$mod.b = $mod.Obj.GetFoo() === null;',
  23429. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  23430. '$mod.b = $mod.Obj.FOnFoo !== null;',
  23431. '$mod.b = $mod.Obj.GetFoo() !== null;',
  23432. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  23433. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  23434. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  23435. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  23436. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  23437. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  23438. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  23439. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  23440. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  23441. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  23442. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  23443. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  23444. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  23445. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  23446. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  23447. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  23448. '$mod.b = $mod.Obj.FOnFoo != null;',
  23449. '$mod.b = $mod.Obj.GetFoo() != null;',
  23450. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  23451. '']));
  23452. end;
  23453. procedure TTestModule.TestProcType_PropertyDelphi;
  23454. begin
  23455. StartProgram(false);
  23456. Add('{$mode delphi}');
  23457. Add('type');
  23458. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23459. Add(' TObject = class');
  23460. Add(' FOnFoo: TFuncInt;');
  23461. Add(' function DoIt(vA: longint = 1): longint;');
  23462. Add(' function GetFoo: TFuncInt;');
  23463. Add(' procedure SetFoo(const Value: TFuncInt);');
  23464. Add(' function GetEvents(Index: longint): TFuncInt;');
  23465. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  23466. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  23467. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  23468. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  23469. Add(' end;');
  23470. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  23471. Add('function tobject.getfoo: tfuncint; begin end;');
  23472. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  23473. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  23474. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  23475. Add('var');
  23476. Add(' Obj: TObject;');
  23477. Add(' vP: tfuncint;');
  23478. Add(' b: boolean;');
  23479. Add('begin');
  23480. Add(' obj.onfoo:=nil;');
  23481. Add(' obj.onbar:=nil;');
  23482. Add(' obj.events[1]:=nil;');
  23483. Add(' obj.onfoo:=obj.onfoo;');
  23484. Add(' obj.onbar:=obj.onbar;');
  23485. Add(' obj.events[2]:=obj.events[3];');
  23486. Add(' obj.onfoo:[email protected];');
  23487. Add(' obj.onbar:[email protected];');
  23488. Add(' obj.events[4]:[email protected];');
  23489. Add(' obj.onfoo:=obj.doit;'); // delphi
  23490. Add(' obj.onbar:=obj.doit;'); // delphi
  23491. Add(' obj.events[4]:=obj.doit;'); // delphi
  23492. Add(' obj.onfoo;');
  23493. Add(' obj.onbar;');
  23494. //Add(' obj.events[5];'); ToDo in pasresolver
  23495. Add(' obj.onfoo();');
  23496. Add(' obj.onbar();');
  23497. Add(' obj.events[6]();');
  23498. //Add(' b:=obj.onfoo=nil;'); // fpc
  23499. //Add(' b:=obj.onbar=nil;'); // fpc
  23500. //Add(' b:=obj.events[7]=nil;'); // fpc
  23501. //Add(' b:=obj.onfoo<>nil;'); // fpc
  23502. //Add(' b:=obj.onbar<>nil;'); // fpc
  23503. //Add(' b:=obj.events[8]<>nil;'); // fpc
  23504. Add(' b:=obj.onfoo=vp;');
  23505. Add(' b:=obj.onbar=vp;');
  23506. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  23507. Add(' b:=obj.onfoo=obj.onfoo;');
  23508. Add(' b:=obj.onbar=obj.onfoo;');
  23509. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  23510. Add(' b:=obj.onfoo<>obj.onfoo;');
  23511. Add(' b:=obj.onbar<>obj.onfoo;');
  23512. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  23513. //Add(' b:[email protected];'); // fpc
  23514. //Add(' b:[email protected];'); // fpc
  23515. //Add(' b:=obj.events[12][email protected];'); // fpc
  23516. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  23517. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  23518. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  23519. Add(' b:=Assigned(obj.onfoo);');
  23520. Add(' b:=Assigned(obj.onbar);');
  23521. Add(' b:=Assigned(obj.events[13]);');
  23522. ConvertProgram;
  23523. CheckSource('TestProcType_PropertyDelphi',
  23524. LinesToStr([ // statements
  23525. 'rtl.createClass($mod, "TObject", null, function () {',
  23526. ' this.$init = function () {',
  23527. ' this.FOnFoo = null;',
  23528. ' };',
  23529. ' this.$final = function () {',
  23530. ' this.FOnFoo = undefined;',
  23531. ' };',
  23532. ' this.DoIt = function (vA) {',
  23533. ' var Result = 0;',
  23534. ' return Result;',
  23535. ' };',
  23536. 'this.GetFoo = function () {',
  23537. ' var Result = null;',
  23538. ' return Result;',
  23539. '};',
  23540. 'this.SetFoo = function (Value) {',
  23541. '};',
  23542. 'this.GetEvents = function (Index) {',
  23543. ' var Result = null;',
  23544. ' return Result;',
  23545. '};',
  23546. 'this.SetEvents = function (Index, Value) {',
  23547. '};',
  23548. '});',
  23549. 'this.Obj = null;',
  23550. 'this.vP = null;',
  23551. 'this.b = false;'
  23552. ]),
  23553. LinesToStr([
  23554. '$mod.Obj.FOnFoo = null;',
  23555. '$mod.Obj.SetFoo(null);',
  23556. '$mod.Obj.SetEvents(1, null);',
  23557. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  23558. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  23559. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  23560. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  23561. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  23562. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  23563. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  23564. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  23565. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  23566. '$mod.Obj.FOnFoo(1);',
  23567. '$mod.Obj.GetFoo();',
  23568. '$mod.Obj.FOnFoo(1);',
  23569. '$mod.Obj.GetFoo()(1);',
  23570. '$mod.Obj.GetEvents(6)(1);',
  23571. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  23572. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  23573. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  23574. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  23575. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  23576. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  23577. '$mod.b = $mod.Obj.FOnFoo != null;',
  23578. '$mod.b = $mod.Obj.GetFoo() != null;',
  23579. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  23580. '']));
  23581. end;
  23582. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  23583. begin
  23584. StartProgram(false);
  23585. Add('type');
  23586. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23587. Add(' TObject = class');
  23588. Add(' FOnFoo: TFuncInt;');
  23589. Add(' function DoIt(vA: longint = 1): longint;');
  23590. Add(' function GetFoo: TFuncInt;');
  23591. Add(' procedure SetFoo(const Value: TFuncInt);');
  23592. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  23593. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  23594. Add(' end;');
  23595. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  23596. Add('function tobject.getfoo: tfuncint; begin end;');
  23597. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  23598. Add('var');
  23599. Add(' Obj: TObject;');
  23600. Add(' vP: tfuncint;');
  23601. Add(' b: boolean;');
  23602. Add('begin');
  23603. Add('with obj do begin');
  23604. Add(' fonfoo:=nil;');
  23605. Add(' onfoo:=nil;');
  23606. Add(' onbar:=nil;');
  23607. Add(' fonfoo:=fonfoo;');
  23608. Add(' onfoo:=onfoo;');
  23609. Add(' onbar:=onbar;');
  23610. Add(' fonfoo:=@doit;');
  23611. Add(' onfoo:=@doit;');
  23612. Add(' onbar:=@doit;');
  23613. //Add(' fonfoo:=doit;'); // delphi
  23614. //Add(' onfoo:=doit;'); // delphi
  23615. //Add(' onbar:=doit;'); // delphi
  23616. Add(' fonfoo;');
  23617. Add(' onfoo;');
  23618. Add(' onbar;');
  23619. Add(' fonfoo();');
  23620. Add(' onfoo();');
  23621. Add(' onbar();');
  23622. Add(' b:=fonfoo=nil;');
  23623. Add(' b:=onfoo=nil;');
  23624. Add(' b:=onbar=nil;');
  23625. Add(' b:=fonfoo<>nil;');
  23626. Add(' b:=onfoo<>nil;');
  23627. Add(' b:=onbar<>nil;');
  23628. Add(' b:=fonfoo=vp;');
  23629. Add(' b:=onfoo=vp;');
  23630. Add(' b:=onbar=vp;');
  23631. Add(' b:=fonfoo=fonfoo;');
  23632. Add(' b:=onfoo=onfoo;');
  23633. Add(' b:=onbar=onfoo;');
  23634. Add(' b:=fonfoo<>fonfoo;');
  23635. Add(' b:=onfoo<>onfoo;');
  23636. Add(' b:=onbar<>onfoo;');
  23637. Add(' b:=fonfoo=@doit;');
  23638. Add(' b:=onfoo=@doit;');
  23639. Add(' b:=onbar=@doit;');
  23640. Add(' b:=fonfoo<>@doit;');
  23641. Add(' b:=onfoo<>@doit;');
  23642. Add(' b:=onbar<>@doit;');
  23643. Add(' b:=Assigned(fonfoo);');
  23644. Add(' b:=Assigned(onfoo);');
  23645. Add(' b:=Assigned(onbar);');
  23646. Add('end;');
  23647. ConvertProgram;
  23648. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  23649. LinesToStr([ // statements
  23650. 'rtl.createClass($mod, "TObject", null, function () {',
  23651. ' this.$init = function () {',
  23652. ' this.FOnFoo = null;',
  23653. ' };',
  23654. ' this.$final = function () {',
  23655. ' this.FOnFoo = undefined;',
  23656. ' };',
  23657. ' this.DoIt = function (vA) {',
  23658. ' var Result = 0;',
  23659. ' return Result;',
  23660. ' };',
  23661. ' this.GetFoo = function () {',
  23662. ' var Result = null;',
  23663. ' return Result;',
  23664. ' };',
  23665. ' this.SetFoo = function (Value) {',
  23666. ' };',
  23667. '});',
  23668. 'this.Obj = null;',
  23669. 'this.vP = null;',
  23670. 'this.b = false;'
  23671. ]),
  23672. LinesToStr([
  23673. 'var $with1 = $mod.Obj;',
  23674. '$with1.FOnFoo = null;',
  23675. '$with1.FOnFoo = null;',
  23676. '$with1.SetFoo(null);',
  23677. '$with1.FOnFoo = $with1.FOnFoo;',
  23678. '$with1.FOnFoo = $with1.FOnFoo;',
  23679. '$with1.SetFoo($with1.GetFoo());',
  23680. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  23681. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  23682. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  23683. '$with1.FOnFoo(1);',
  23684. '$with1.FOnFoo(1);',
  23685. '$with1.GetFoo();',
  23686. '$with1.FOnFoo(1);',
  23687. '$with1.FOnFoo(1);',
  23688. '$with1.GetFoo()(1);',
  23689. '$mod.b = $with1.FOnFoo === null;',
  23690. '$mod.b = $with1.FOnFoo === null;',
  23691. '$mod.b = $with1.GetFoo() === null;',
  23692. '$mod.b = $with1.FOnFoo !== null;',
  23693. '$mod.b = $with1.FOnFoo !== null;',
  23694. '$mod.b = $with1.GetFoo() !== null;',
  23695. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  23696. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  23697. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  23698. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23699. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23700. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  23701. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23702. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  23703. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  23704. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23705. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23706. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  23707. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23708. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  23709. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  23710. '$mod.b = $with1.FOnFoo != null;',
  23711. '$mod.b = $with1.FOnFoo != null;',
  23712. '$mod.b = $with1.GetFoo() != null;',
  23713. '']));
  23714. end;
  23715. procedure TTestModule.TestProcType_Nested;
  23716. begin
  23717. StartProgram(false);
  23718. Add([
  23719. 'type',
  23720. ' TProcInt = procedure(vI: longint = 1);',
  23721. 'procedure DoIt(vJ: longint);',
  23722. 'var aProc: TProcInt;',
  23723. ' b: boolean;',
  23724. ' procedure Sub(vK: longint);',
  23725. ' var aSub: TProcInt;',
  23726. ' procedure SubSub(vK: longint);',
  23727. ' var aSubSub: TProcInt;',
  23728. ' begin;',
  23729. ' aProc:=@DoIt;',
  23730. ' aSub:=@DoIt;',
  23731. ' aSubSub:=@DoIt;',
  23732. ' aProc:=@Sub;',
  23733. ' aSub:=@Sub;',
  23734. ' aSubSub:=@Sub;',
  23735. ' aProc:=@SubSub;',
  23736. ' aSub:=@SubSub;',
  23737. ' aSubSub:=@SubSub;',
  23738. ' end;',
  23739. ' begin;',
  23740. ' end;',
  23741. 'begin;',
  23742. ' aProc:=@Sub;',
  23743. ' b:=aProc=@Sub;',
  23744. ' b:=@Sub=aProc;',
  23745. 'end;',
  23746. 'begin',
  23747. '']);
  23748. ConvertProgram;
  23749. CheckSource('TestProcType_Nested',
  23750. LinesToStr([ // statements
  23751. 'this.DoIt = function (vJ) {',
  23752. ' var aProc = null;',
  23753. ' var b = false;',
  23754. ' function Sub(vK) {',
  23755. ' var aSub = null;',
  23756. ' function SubSub(vK) {',
  23757. ' var aSubSub = null;',
  23758. ' aProc = $mod.DoIt;',
  23759. ' aSub = $mod.DoIt;',
  23760. ' aSubSub = $mod.DoIt;',
  23761. ' aProc = Sub;',
  23762. ' aSub = Sub;',
  23763. ' aSubSub = Sub;',
  23764. ' aProc = SubSub;',
  23765. ' aSub = SubSub;',
  23766. ' aSubSub = SubSub;',
  23767. ' };',
  23768. ' };',
  23769. ' aProc = Sub;',
  23770. ' b = rtl.eqCallback(aProc, Sub);',
  23771. ' b = rtl.eqCallback(Sub, aProc);',
  23772. '};',
  23773. '']),
  23774. LinesToStr([ // $mod.$main
  23775. '']));
  23776. end;
  23777. procedure TTestModule.TestProcType_NestedOfObject;
  23778. begin
  23779. StartProgram(false);
  23780. Add([
  23781. 'type',
  23782. ' TProcInt = procedure(vI: longint = 1) of object;',
  23783. ' TObject = class',
  23784. ' procedure DoIt(vJ: longint);',
  23785. ' end;',
  23786. 'procedure TObject.DoIt(vJ: longint);',
  23787. 'var aProc: TProcInt;',
  23788. ' b: boolean;',
  23789. ' procedure Sub(vK: longint);',
  23790. ' var aSub: TProcInt;',
  23791. ' procedure SubSub(vK: longint);',
  23792. ' var aSubSub: TProcInt;',
  23793. ' begin;',
  23794. ' aProc:=@DoIt;',
  23795. ' aSub:=@DoIt;',
  23796. ' aSubSub:=@DoIt;',
  23797. ' aProc:=@Sub;',
  23798. ' aSub:=@Sub;',
  23799. ' aSubSub:=@Sub;',
  23800. ' aProc:=@SubSub;',
  23801. ' aSub:=@SubSub;',
  23802. ' aSubSub:=@SubSub;',
  23803. ' end;',
  23804. ' begin;',
  23805. ' end;',
  23806. 'begin;',
  23807. ' aProc:=@Sub;',
  23808. ' b:=aProc=@Sub;',
  23809. ' b:=@Sub=aProc;',
  23810. 'end;',
  23811. 'begin',
  23812. '']);
  23813. ConvertProgram;
  23814. CheckSource('TestProcType_Nested',
  23815. LinesToStr([ // statements
  23816. 'rtl.createClass($mod, "TObject", null, function () {',
  23817. ' this.$init = function () {',
  23818. ' };',
  23819. ' this.$final = function () {',
  23820. ' };',
  23821. ' this.DoIt = function (vJ) {',
  23822. ' var $Self = this;',
  23823. ' var aProc = null;',
  23824. ' var b = false;',
  23825. ' function Sub(vK) {',
  23826. ' var aSub = null;',
  23827. ' function SubSub(vK) {',
  23828. ' var aSubSub = null;',
  23829. ' aProc = rtl.createCallback($Self, "DoIt");',
  23830. ' aSub = rtl.createCallback($Self, "DoIt");',
  23831. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  23832. ' aProc = Sub;',
  23833. ' aSub = Sub;',
  23834. ' aSubSub = Sub;',
  23835. ' aProc = SubSub;',
  23836. ' aSub = SubSub;',
  23837. ' aSubSub = SubSub;',
  23838. ' };',
  23839. ' };',
  23840. ' aProc = Sub;',
  23841. ' b = rtl.eqCallback(aProc, Sub);',
  23842. ' b = rtl.eqCallback(Sub, aProc);',
  23843. ' };',
  23844. '});',
  23845. '']),
  23846. LinesToStr([ // $mod.$main
  23847. '']));
  23848. end;
  23849. procedure TTestModule.TestProcType_ReferenceToProc;
  23850. begin
  23851. StartProgram(false);
  23852. Add([
  23853. 'type',
  23854. ' TProcRef = reference to procedure(i: longint = 0);',
  23855. ' TFuncRef = reference to function(i: longint = 0): longint;',
  23856. 'var',
  23857. ' p: TProcRef;',
  23858. ' f: TFuncRef;',
  23859. 'procedure DoIt(i: longint);',
  23860. 'begin',
  23861. 'end;',
  23862. 'function GetIt(i: longint): longint;',
  23863. 'begin',
  23864. ' p:=@DoIt;',
  23865. ' f:=@GetIt;',
  23866. ' f;',
  23867. ' f();',
  23868. ' f(1);',
  23869. 'end;',
  23870. 'begin',
  23871. ' p:=@DoIt;',
  23872. ' f:=@GetIt;',
  23873. ' f;',
  23874. ' f();',
  23875. ' f(1);',
  23876. ' p:=TProcRef(f);',
  23877. '']);
  23878. ConvertProgram;
  23879. CheckSource('TestProcType_ReferenceToProc',
  23880. LinesToStr([ // statements
  23881. 'this.p = null;',
  23882. 'this.f = null;',
  23883. 'this.DoIt = function (i) {',
  23884. '};',
  23885. 'this.GetIt = function (i) {',
  23886. ' var Result = 0;',
  23887. ' $mod.p = $mod.DoIt;',
  23888. ' $mod.f = $mod.GetIt;',
  23889. ' $mod.f(0);',
  23890. ' $mod.f(0);',
  23891. ' $mod.f(1);',
  23892. ' return Result;',
  23893. '};',
  23894. '']),
  23895. LinesToStr([ // $mod.$main
  23896. '$mod.p = $mod.DoIt;',
  23897. '$mod.f = $mod.GetIt;',
  23898. '$mod.f(0);',
  23899. '$mod.f(0);',
  23900. '$mod.f(1);',
  23901. '$mod.p = $mod.f;',
  23902. '']));
  23903. end;
  23904. procedure TTestModule.TestProcType_ReferenceToMethod;
  23905. begin
  23906. StartProgram(false);
  23907. Add([
  23908. 'type',
  23909. ' TFuncRef = reference to function(i: longint = 5): longint;',
  23910. ' TObject = class',
  23911. ' function Grow(s: longint): longint;',
  23912. ' end;',
  23913. 'var',
  23914. ' f: tfuncref;',
  23915. 'function tobject.grow(s: longint): longint;',
  23916. ' function GrowSub(i: longint): longint;',
  23917. ' begin',
  23918. ' f:=@grow;',
  23919. ' f:=@growsub;',
  23920. ' end;',
  23921. 'begin',
  23922. ' f:=@grow;',
  23923. ' f:=@growsub;',
  23924. 'end;',
  23925. 'begin',
  23926. '']);
  23927. ConvertProgram;
  23928. CheckSource('TestProcType_ReferenceToMethod',
  23929. LinesToStr([ // statements
  23930. 'rtl.createClass($mod, "TObject", null, function () {',
  23931. ' this.$init = function () {',
  23932. ' };',
  23933. ' this.$final = function () {',
  23934. ' };',
  23935. ' this.Grow = function (s) {',
  23936. ' var $Self = this;',
  23937. ' var Result = 0;',
  23938. ' function GrowSub(i) {',
  23939. ' var Result = 0;',
  23940. ' $mod.f = rtl.createCallback($Self, "Grow");',
  23941. ' $mod.f = GrowSub;',
  23942. ' return Result;',
  23943. ' };',
  23944. ' $mod.f = rtl.createCallback($Self, "Grow");',
  23945. ' $mod.f = GrowSub;',
  23946. ' return Result;',
  23947. ' };',
  23948. '});',
  23949. 'this.f = null;',
  23950. '']),
  23951. LinesToStr([ // $mod.$main
  23952. '']));
  23953. end;
  23954. procedure TTestModule.TestProcType_Typecast;
  23955. begin
  23956. StartProgram(false);
  23957. Add([
  23958. 'type',
  23959. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  23960. ' TEvent = procedure of object;',
  23961. ' TGetter = function:longint of object;',
  23962. ' TProcA = procedure(i: longint);',
  23963. ' TFuncB = function(i, j: longint): longint;',
  23964. 'procedure DoIt(); varargs; begin end;',
  23965. 'var',
  23966. ' Notify: tnotifyevent;',
  23967. ' Event: tevent;',
  23968. ' Getter: tgetter;',
  23969. ' ProcA: tproca;',
  23970. ' FuncB: tfuncb;',
  23971. ' p: pointer;',
  23972. 'begin',
  23973. ' notify:=tnotifyevent(event);',
  23974. ' event:=tevent(event);',
  23975. ' event:=tevent(notify);',
  23976. ' event:=tevent(getter);',
  23977. ' event:=tevent(proca);',
  23978. ' proca:=tproca(funcb);',
  23979. ' funcb:=tfuncb(funcb);',
  23980. ' funcb:=tfuncb(proca);',
  23981. ' funcb:=tfuncb(getter);',
  23982. ' proca:=tproca(p);',
  23983. ' funcb:=tfuncb(p);',
  23984. ' getter:=tgetter(p);',
  23985. ' p:=pointer(notify);',
  23986. ' p:=notify;',
  23987. ' p:=pointer(proca);',
  23988. ' p:=proca;',
  23989. ' p:=pointer(funcb);',
  23990. ' p:=funcb;',
  23991. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  23992. '']);
  23993. ConvertProgram;
  23994. CheckSource('TestProcType_Typecast',
  23995. LinesToStr([ // statements
  23996. 'this.DoIt = function () {',
  23997. '};',
  23998. 'this.Notify = null;',
  23999. 'this.Event = null;',
  24000. 'this.Getter = null;',
  24001. 'this.ProcA = null;',
  24002. 'this.FuncB = null;',
  24003. 'this.p = null;',
  24004. '']),
  24005. LinesToStr([ // $mod.$main
  24006. '$mod.Notify = $mod.Event;',
  24007. '$mod.Event = $mod.Event;',
  24008. '$mod.Event = $mod.Notify;',
  24009. '$mod.Event = $mod.Getter;',
  24010. '$mod.Event = $mod.ProcA;',
  24011. '$mod.ProcA = $mod.FuncB;',
  24012. '$mod.FuncB = $mod.FuncB;',
  24013. '$mod.FuncB = $mod.ProcA;',
  24014. '$mod.FuncB = $mod.Getter;',
  24015. '$mod.ProcA = $mod.p;',
  24016. '$mod.FuncB = $mod.p;',
  24017. '$mod.Getter = $mod.p;',
  24018. '$mod.p = $mod.Notify;',
  24019. '$mod.p = $mod.Notify;',
  24020. '$mod.p = $mod.ProcA;',
  24021. '$mod.p = $mod.ProcA;',
  24022. '$mod.p = $mod.FuncB;',
  24023. '$mod.p = $mod.FuncB;',
  24024. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  24025. '']));
  24026. end;
  24027. procedure TTestModule.TestProcType_PassProcToUntyped;
  24028. begin
  24029. StartProgram(false);
  24030. Add([
  24031. 'type',
  24032. ' TEvent = procedure of object;',
  24033. ' TFunc = function: longint;',
  24034. 'procedure DoIt(); varargs; begin end;',
  24035. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  24036. 'var',
  24037. ' Event: tevent;',
  24038. ' Func: TFunc;',
  24039. 'begin',
  24040. ' doit(event,func);',
  24041. ' dosome(event,event,event);',
  24042. ' dosome(func,func,func);',
  24043. '']);
  24044. ConvertProgram;
  24045. CheckSource('TestProcType_PassProcToUntyped',
  24046. LinesToStr([ // statements
  24047. 'this.DoIt = function () {',
  24048. '};',
  24049. 'this.DoSome = function (a, b, p) {',
  24050. '};',
  24051. 'this.Event = null;',
  24052. 'this.Func = null;',
  24053. '']),
  24054. LinesToStr([ // $mod.$main
  24055. '$mod.DoIt($mod.Event, $mod.Func);',
  24056. '$mod.DoSome($mod.Event, {',
  24057. ' p: $mod,',
  24058. ' get: function () {',
  24059. ' return this.p.Event;',
  24060. ' },',
  24061. ' set: function (v) {',
  24062. ' this.p.Event = v;',
  24063. ' }',
  24064. '}, $mod.Event);',
  24065. '$mod.DoSome($mod.Func, {',
  24066. ' p: $mod,',
  24067. ' get: function () {',
  24068. ' return this.p.Func;',
  24069. ' },',
  24070. ' set: function (v) {',
  24071. ' this.p.Func = v;',
  24072. ' }',
  24073. '}, $mod.Func);',
  24074. '']));
  24075. end;
  24076. procedure TTestModule.TestProcType_PassProcToArray;
  24077. begin
  24078. StartProgram(false);
  24079. Add([
  24080. 'type',
  24081. ' TFunc = function: longint;',
  24082. ' TArrFunc = array of TFunc;',
  24083. 'procedure DoIt(Arr: TArrFunc); begin end;',
  24084. 'function GetIt: longint; begin end;',
  24085. 'var',
  24086. ' Func: tfunc;',
  24087. 'begin',
  24088. ' doit([]);',
  24089. ' doit([@GetIt]);',
  24090. ' doit([Func]);',
  24091. '']);
  24092. ConvertProgram;
  24093. CheckSource('TestProcType_PassProcToArray',
  24094. LinesToStr([ // statements
  24095. 'this.DoIt = function (Arr) {',
  24096. '};',
  24097. 'this.GetIt = function () {',
  24098. ' var Result = 0;',
  24099. ' return Result;',
  24100. '};',
  24101. 'this.Func = null;',
  24102. '']),
  24103. LinesToStr([ // $mod.$main
  24104. '$mod.DoIt([]);',
  24105. '$mod.DoIt([$mod.GetIt]);',
  24106. '$mod.DoIt([$mod.Func]);',
  24107. '']));
  24108. end;
  24109. procedure TTestModule.TestPointer;
  24110. begin
  24111. StartProgram(false);
  24112. Add(['type',
  24113. ' TObject = class end;',
  24114. ' TClass = class of TObject;',
  24115. ' TArrInt = array of longint;',
  24116. 'const',
  24117. ' n = nil;',
  24118. 'var',
  24119. ' v: jsvalue;',
  24120. ' Obj: tobject;',
  24121. ' C: tclass;',
  24122. ' a: tarrint;',
  24123. ' p: Pointer = nil;',
  24124. ' s: string;',
  24125. 'begin',
  24126. ' p:=p;',
  24127. ' p:=nil;',
  24128. ' if p=nil then;',
  24129. ' if nil=p then;',
  24130. ' if Assigned(p) then;',
  24131. ' p:=Pointer(v);',
  24132. ' p:=obj;',
  24133. ' p:=c;',
  24134. ' p:=a;',
  24135. ' p:=tobject;',
  24136. ' obj:=TObject(p);',
  24137. ' c:=TClass(p);',
  24138. ' a:=TArrInt(p);',
  24139. ' p:=n;',
  24140. ' p:=Pointer(a);',
  24141. ' p:=pointer(s);',
  24142. ' s:=string(p);',
  24143. '']);
  24144. ConvertProgram;
  24145. CheckSource('TestPointer',
  24146. LinesToStr([ // statements
  24147. 'rtl.createClass($mod, "TObject", null, function () {',
  24148. ' this.$init = function () {',
  24149. ' };',
  24150. ' this.$final = function () {',
  24151. ' };',
  24152. '});',
  24153. 'this.n = null;',
  24154. 'this.v = undefined;',
  24155. 'this.Obj = null;',
  24156. 'this.C = null;',
  24157. 'this.a = [];',
  24158. 'this.p = null;',
  24159. 'this.s = "";',
  24160. '']),
  24161. LinesToStr([ // $mod.$main
  24162. '$mod.p = $mod.p;',
  24163. '$mod.p = null;',
  24164. 'if ($mod.p === null) ;',
  24165. 'if (null === $mod.p) ;',
  24166. 'if ($mod.p != null) ;',
  24167. '$mod.p = $mod.v;',
  24168. '$mod.p = $mod.Obj;',
  24169. '$mod.p = $mod.C;',
  24170. '$mod.p = $mod.a;',
  24171. '$mod.p = $mod.TObject;',
  24172. '$mod.Obj = $mod.p;',
  24173. '$mod.C = $mod.p;',
  24174. '$mod.a = $mod.p;',
  24175. '$mod.p = null;',
  24176. '$mod.p = $mod.a;',
  24177. '$mod.p = $mod.s;',
  24178. '$mod.s = $mod.p;',
  24179. '']));
  24180. end;
  24181. procedure TTestModule.TestPointer_Proc;
  24182. begin
  24183. StartProgram(false);
  24184. Add('type');
  24185. Add(' TObject = class');
  24186. Add(' procedure DoIt; virtual; abstract;');
  24187. Add(' end;');
  24188. Add('procedure DoSome; begin end;');
  24189. Add('var');
  24190. Add(' o: TObject;');
  24191. Add(' p: Pointer;');
  24192. Add('begin');
  24193. Add(' p:=@DoSome;');
  24194. Add(' p:[email protected];');
  24195. ConvertProgram;
  24196. CheckSource('TestPointer_Proc',
  24197. LinesToStr([ // statements
  24198. 'rtl.createClass($mod, "TObject", null, function () {',
  24199. ' this.$init = function () {',
  24200. ' };',
  24201. ' this.$final = function () {',
  24202. ' };',
  24203. '});',
  24204. 'this.DoSome = function () {',
  24205. '};',
  24206. 'this.o = null;',
  24207. 'this.p = null;',
  24208. '']),
  24209. LinesToStr([ // $mod.$main
  24210. '$mod.p = $mod.DoSome;',
  24211. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  24212. '']));
  24213. end;
  24214. procedure TTestModule.TestPointer_AssignRecordFail;
  24215. begin
  24216. StartProgram(false);
  24217. Add('type');
  24218. Add(' TRec = record end;');
  24219. Add('var');
  24220. Add(' p: Pointer;');
  24221. Add(' r: TRec;');
  24222. Add('begin');
  24223. Add(' p:=r;');
  24224. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  24225. nIncompatibleTypesGotExpected);
  24226. ConvertProgram;
  24227. end;
  24228. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  24229. begin
  24230. StartProgram(false);
  24231. Add('type');
  24232. Add(' TArr = array[boolean] of longint;');
  24233. Add('var');
  24234. Add(' p: Pointer;');
  24235. Add(' a: TArr;');
  24236. Add('begin');
  24237. Add(' p:=a;');
  24238. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  24239. nIncompatibleTypesGotExpected);
  24240. ConvertProgram;
  24241. end;
  24242. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  24243. begin
  24244. StartProgram(false);
  24245. Add([
  24246. 'procedure DoIt(args: array of jsvalue); begin end;',
  24247. 'procedure DoAll; varargs; begin end;',
  24248. 'var',
  24249. ' v: jsvalue;',
  24250. 'begin',
  24251. ' DoIt([pointer(v)]);',
  24252. ' DoAll(pointer(v));',
  24253. '']);
  24254. ConvertProgram;
  24255. CheckSource('TestPointer_TypeCastJSValueToPointer',
  24256. LinesToStr([ // statements
  24257. 'this.DoIt = function (args) {',
  24258. '};',
  24259. 'this.DoAll = function () {',
  24260. '};',
  24261. 'this.v = undefined;',
  24262. '']),
  24263. LinesToStr([ // $mod.$main
  24264. '$mod.DoIt([$mod.v]);',
  24265. '$mod.DoAll($mod.v);',
  24266. '']));
  24267. end;
  24268. procedure TTestModule.TestPointer_NonRecordFail;
  24269. begin
  24270. StartProgram(false);
  24271. Add([
  24272. 'type',
  24273. ' p = ^longint;',
  24274. 'begin',
  24275. '']);
  24276. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  24277. ConvertProgram;
  24278. end;
  24279. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  24280. begin
  24281. StartProgram(false);
  24282. Add([
  24283. 'procedure DoIt(p: ^longint); begin end;',
  24284. 'begin',
  24285. '']);
  24286. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  24287. ConvertProgram;
  24288. end;
  24289. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  24290. begin
  24291. StartProgram(false);
  24292. Add([
  24293. 'var p: ^longint;',
  24294. 'begin',
  24295. '']);
  24296. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  24297. ConvertProgram;
  24298. end;
  24299. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  24300. begin
  24301. StartProgram(false);
  24302. Add([
  24303. 'function DoIt: ^longint; begin end;',
  24304. 'begin',
  24305. '']);
  24306. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  24307. ConvertProgram;
  24308. end;
  24309. procedure TTestModule.TestPointer_AddrOperatorFail;
  24310. begin
  24311. StartProgram(false);
  24312. Add([
  24313. 'var i: longint;',
  24314. 'begin',
  24315. ' if @i=nil then ;',
  24316. '']);
  24317. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  24318. ConvertProgram;
  24319. end;
  24320. procedure TTestModule.TestPointer_ArrayParamsFail;
  24321. begin
  24322. StartProgram(false);
  24323. Add([
  24324. 'var',
  24325. ' p: Pointer;',
  24326. 'begin',
  24327. ' p:=p[1];',
  24328. '']);
  24329. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  24330. ConvertProgram;
  24331. end;
  24332. procedure TTestModule.TestPointer_PointerAddFail;
  24333. begin
  24334. StartProgram(false);
  24335. Add([
  24336. 'var',
  24337. ' p: Pointer;',
  24338. 'begin',
  24339. ' p:=p+1;',
  24340. '']);
  24341. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  24342. ConvertProgram;
  24343. end;
  24344. procedure TTestModule.TestPointer_IncPointerFail;
  24345. begin
  24346. StartProgram(false);
  24347. Add([
  24348. 'var',
  24349. ' p: Pointer;',
  24350. 'begin',
  24351. ' inc(p,1);',
  24352. '']);
  24353. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  24354. nIncompatibleTypeArgNo);
  24355. ConvertProgram;
  24356. end;
  24357. procedure TTestModule.TestPointer_Record;
  24358. begin
  24359. StartProgram(false);
  24360. Add([
  24361. 'type',
  24362. ' TRec = record x: longint; end;',
  24363. ' PRec = ^TRec;',
  24364. 'var',
  24365. ' r: TRec;',
  24366. ' p: PRec;',
  24367. ' q: ^TRec;',
  24368. ' Ptr: pointer;',
  24369. 'begin',
  24370. ' new(p);',
  24371. ' p:=@r;',
  24372. ' r:=p^;',
  24373. ' r.x:=p^.x;',
  24374. ' p^.x:=r.x;',
  24375. ' if p^.x=3 then ;',
  24376. ' if 4=p^.x then ;',
  24377. ' dispose(p);',
  24378. ' new(q);',
  24379. ' dispose(q);',
  24380. ' Ptr:=p;',
  24381. ' p:=PRec(ptr);',
  24382. '']);
  24383. ConvertProgram;
  24384. CheckSource('TestPointer_Record',
  24385. LinesToStr([ // statements
  24386. 'rtl.recNewT($mod, "TRec", function () {',
  24387. ' this.x = 0;',
  24388. ' this.$eq = function (b) {',
  24389. ' return this.x === b.x;',
  24390. ' };',
  24391. ' this.$assign = function (s) {',
  24392. ' this.x = s.x;',
  24393. ' return this;',
  24394. ' };',
  24395. '});',
  24396. 'this.r = $mod.TRec.$new();',
  24397. 'this.p = null;',
  24398. 'this.q = null;',
  24399. 'this.Ptr = null;',
  24400. '']),
  24401. LinesToStr([ // $mod.$main
  24402. '$mod.p = $mod.TRec.$new();',
  24403. '$mod.p = $mod.r;',
  24404. '$mod.r.$assign($mod.p);',
  24405. '$mod.r.x = $mod.p.x;',
  24406. '$mod.p.x = $mod.r.x;',
  24407. 'if ($mod.p.x === 3) ;',
  24408. 'if (4 === $mod.p.x) ;',
  24409. '$mod.p = null;',
  24410. '$mod.q = $mod.TRec.$new();',
  24411. '$mod.q = null;',
  24412. '$mod.Ptr = $mod.p;',
  24413. '$mod.p = $mod.Ptr;',
  24414. '']));
  24415. end;
  24416. procedure TTestModule.TestPointer_RecordArg;
  24417. begin
  24418. StartProgram(false);
  24419. Add([
  24420. '{$modeswitch autoderef}',
  24421. 'type',
  24422. ' TRec = record x: longint; end;',
  24423. ' PRec = ^TRec;',
  24424. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  24425. 'begin',
  24426. ' a.x:=a.x;',
  24427. ' a^.x:=a^.x;',
  24428. ' with a^ do',
  24429. ' x:=x;',
  24430. 'end;',
  24431. 'function GetIt(p: PRec): PRec;',
  24432. 'begin',
  24433. ' p.x:=p.x;',
  24434. ' p^.x:=p^.x;',
  24435. ' with p^ do',
  24436. ' x:=x;',
  24437. 'end;',
  24438. 'var',
  24439. ' r: TRec;',
  24440. ' p: PRec;',
  24441. 'begin',
  24442. ' p:=GetIt(p);',
  24443. ' p^:=GetIt(@r)^;',
  24444. ' DoIt(p,p,p);',
  24445. ' DoIt(@r,p,p);',
  24446. '']);
  24447. ConvertProgram;
  24448. CheckSource('TestPointer_Record',
  24449. LinesToStr([ // statements
  24450. 'rtl.recNewT($mod, "TRec", function () {',
  24451. ' this.x = 0;',
  24452. ' this.$eq = function (b) {',
  24453. ' return this.x === b.x;',
  24454. ' };',
  24455. ' this.$assign = function (s) {',
  24456. ' this.x = s.x;',
  24457. ' return this;',
  24458. ' };',
  24459. '});',
  24460. 'this.DoIt = function (a, b, c) {',
  24461. ' var Result = $mod.TRec.$new();',
  24462. ' a.x = a.x;',
  24463. ' a.x = a.x;',
  24464. ' a.x = a.x;',
  24465. ' return Result;',
  24466. '};',
  24467. 'this.GetIt = function (p) {',
  24468. ' var Result = null;',
  24469. ' p.x = p.x;',
  24470. ' p.x = p.x;',
  24471. ' p.x = p.x;',
  24472. ' return Result;',
  24473. '};',
  24474. 'this.r = $mod.TRec.$new();',
  24475. 'this.p = null;',
  24476. '']),
  24477. LinesToStr([ // $mod.$main
  24478. '$mod.p = $mod.GetIt($mod.p);',
  24479. '$mod.p.$assign($mod.GetIt($mod.r));',
  24480. '$mod.DoIt($mod.p, {',
  24481. ' p: $mod,',
  24482. ' get: function () {',
  24483. ' return this.p.p;',
  24484. ' },',
  24485. ' set: function (v) {',
  24486. ' this.p.p = v;',
  24487. ' }',
  24488. '}, {',
  24489. ' p: $mod,',
  24490. ' get: function () {',
  24491. ' return this.p.p;',
  24492. ' },',
  24493. ' set: function (v) {',
  24494. ' this.p.p = v;',
  24495. ' }',
  24496. '});',
  24497. '$mod.DoIt($mod.r, {',
  24498. ' p: $mod,',
  24499. ' get: function () {',
  24500. ' return this.p.p;',
  24501. ' },',
  24502. ' set: function (v) {',
  24503. ' this.p.p = v;',
  24504. ' }',
  24505. '}, {',
  24506. ' p: $mod,',
  24507. ' get: function () {',
  24508. ' return this.p.p;',
  24509. ' },',
  24510. ' set: function (v) {',
  24511. ' this.p.p = v;',
  24512. ' }',
  24513. '});',
  24514. '']));
  24515. end;
  24516. procedure TTestModule.TestJSValue_AssignToJSValue;
  24517. begin
  24518. StartProgram(false);
  24519. Add('var');
  24520. Add(' v: jsvalue;');
  24521. Add(' i: longint;');
  24522. Add(' s: string;');
  24523. Add(' b: boolean;');
  24524. Add(' d: double;');
  24525. Add(' p: pointer;');
  24526. Add('begin');
  24527. Add(' v:=v;');
  24528. Add(' v:=1;');
  24529. Add(' v:=i;');
  24530. Add(' v:='''';');
  24531. Add(' v:=''c'';');
  24532. Add(' v:=''foo'';');
  24533. Add(' v:=s;');
  24534. Add(' v:=false;');
  24535. Add(' v:=true;');
  24536. Add(' v:=b;');
  24537. Add(' v:=0.1;');
  24538. Add(' v:=d;');
  24539. Add(' v:=nil;');
  24540. Add(' v:=p;');
  24541. ConvertProgram;
  24542. CheckSource('TestJSValue_AssignToJSValue',
  24543. LinesToStr([ // statements
  24544. 'this.v = undefined;',
  24545. 'this.i = 0;',
  24546. 'this.s = "";',
  24547. 'this.b = false;',
  24548. 'this.d = 0.0;',
  24549. 'this.p = null;',
  24550. '']),
  24551. LinesToStr([ // $mod.$main
  24552. '$mod.v = $mod.v;',
  24553. '$mod.v = 1;',
  24554. '$mod.v = $mod.i;',
  24555. '$mod.v = "";',
  24556. '$mod.v = "c";',
  24557. '$mod.v = "foo";',
  24558. '$mod.v = $mod.s;',
  24559. '$mod.v = false;',
  24560. '$mod.v = true;',
  24561. '$mod.v = $mod.b;',
  24562. '$mod.v = 0.1;',
  24563. '$mod.v = $mod.d;',
  24564. '$mod.v = null;',
  24565. '$mod.v = $mod.p;',
  24566. '']));
  24567. end;
  24568. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  24569. begin
  24570. StartProgram(false);
  24571. Add('type');
  24572. Add(' integer = longint;');
  24573. Add(' TYesNo = boolean;');
  24574. Add(' TFloat = double;');
  24575. Add(' TCaption = string;');
  24576. Add(' TChar = char;');
  24577. Add('var');
  24578. Add(' v: jsvalue;');
  24579. Add(' i: integer;');
  24580. Add(' s: TCaption;');
  24581. Add(' b: TYesNo;');
  24582. Add(' d: TFloat;');
  24583. Add(' c: char;');
  24584. Add('begin');
  24585. Add(' i:=longint(v);');
  24586. Add(' i:=integer(v);');
  24587. Add(' s:=string(v);');
  24588. Add(' s:=TCaption(v);');
  24589. Add(' b:=boolean(v);');
  24590. Add(' b:=TYesNo(v);');
  24591. Add(' d:=double(v);');
  24592. Add(' d:=TFloat(v);');
  24593. Add(' c:=char(v);');
  24594. Add(' c:=TChar(v);');
  24595. ConvertProgram;
  24596. CheckSource('TestJSValue_TypeCastToBaseType',
  24597. LinesToStr([ // statements
  24598. 'this.v = undefined;',
  24599. 'this.i = 0;',
  24600. 'this.s = "";',
  24601. 'this.b = false;',
  24602. 'this.d = 0.0;',
  24603. 'this.c = "";',
  24604. '']),
  24605. LinesToStr([ // $mod.$main
  24606. '$mod.i = Math.floor($mod.v);',
  24607. '$mod.i = Math.floor($mod.v);',
  24608. '$mod.s = "" + $mod.v;',
  24609. '$mod.s = "" + $mod.v;',
  24610. '$mod.b = !($mod.v == false);',
  24611. '$mod.b = !($mod.v == false);',
  24612. '$mod.d = rtl.getNumber($mod.v);',
  24613. '$mod.d = rtl.getNumber($mod.v);',
  24614. '$mod.c = rtl.getChar($mod.v);',
  24615. '$mod.c = rtl.getChar($mod.v);',
  24616. '']));
  24617. end;
  24618. procedure TTestModule.TestJSValue_TypecastToJSValue;
  24619. begin
  24620. StartProgram(false);
  24621. Add([
  24622. 'type',
  24623. ' TArr = array of word;',
  24624. ' TRec = record end;',
  24625. ' TSet = set of boolean;',
  24626. 'procedure Fly(v: jsvalue);',
  24627. 'begin',
  24628. 'end;',
  24629. 'var',
  24630. ' a: TArr;',
  24631. ' r: TRec;',
  24632. ' s: TSet;',
  24633. 'begin',
  24634. ' Fly(jsvalue(a));',
  24635. ' Fly(jsvalue(r));',
  24636. ' Fly(jsvalue(s));',
  24637. '']);
  24638. ConvertProgram;
  24639. CheckSource('TestJSValue_TypecastToJSValue',
  24640. LinesToStr([ // statements
  24641. 'rtl.recNewT($mod, "TRec", function () {',
  24642. ' this.$eq = function (b) {',
  24643. ' return true;',
  24644. ' };',
  24645. ' this.$assign = function (s) {',
  24646. ' return this;',
  24647. ' };',
  24648. '});',
  24649. 'this.Fly = function (v) {',
  24650. '};',
  24651. 'this.a = [];',
  24652. 'this.r = $mod.TRec.$new();',
  24653. 'this.s = {};',
  24654. '']),
  24655. LinesToStr([ // $mod.$main
  24656. '$mod.Fly($mod.a);',
  24657. '$mod.Fly($mod.r);',
  24658. '$mod.Fly($mod.s);',
  24659. '']));
  24660. end;
  24661. procedure TTestModule.TestJSValue_Equal;
  24662. begin
  24663. StartProgram(false);
  24664. Add('type');
  24665. Add(' integer = longint;');
  24666. Add(' TYesNo = boolean;');
  24667. Add(' TFloat = double;');
  24668. Add(' TCaption = string;');
  24669. Add(' TChar = char;');
  24670. Add(' TMulti = JSValue;');
  24671. Add('var');
  24672. Add(' v: jsvalue;');
  24673. Add(' i: integer;');
  24674. Add(' s: TCaption;');
  24675. Add(' b: TYesNo;');
  24676. Add(' d: TFloat;');
  24677. Add(' c: char;');
  24678. Add(' m: TMulti;');
  24679. Add('begin');
  24680. Add(' b:=v=v;');
  24681. Add(' b:=v<>v;');
  24682. Add(' b:=v=1;');
  24683. Add(' b:=v<>1;');
  24684. Add(' b:=2=v;');
  24685. Add(' b:=2<>v;');
  24686. Add(' b:=v=i;');
  24687. Add(' b:=i=v;');
  24688. Add(' b:=v=nil;');
  24689. Add(' b:=nil=v;');
  24690. Add(' b:=v=false;');
  24691. Add(' b:=true=v;');
  24692. Add(' b:=v=b;');
  24693. Add(' b:=b=v;');
  24694. Add(' b:=v=s;');
  24695. Add(' b:=s=v;');
  24696. Add(' b:=v=''foo'';');
  24697. Add(' b:=''''=v;');
  24698. Add(' b:=v=d;');
  24699. Add(' b:=d=v;');
  24700. Add(' b:=v=3.4;');
  24701. Add(' b:=5.6=v;');
  24702. Add(' b:=v=c;');
  24703. Add(' b:=c=v;');
  24704. Add(' b:=m=m;');
  24705. Add(' b:=v=m;');
  24706. Add(' b:=m=v;');
  24707. ConvertProgram;
  24708. CheckSource('TestJSValue_Equal',
  24709. LinesToStr([ // statements
  24710. 'this.v = undefined;',
  24711. 'this.i = 0;',
  24712. 'this.s = "";',
  24713. 'this.b = false;',
  24714. 'this.d = 0.0;',
  24715. 'this.c = "";',
  24716. 'this.m = undefined;',
  24717. '']),
  24718. LinesToStr([ // $mod.$main
  24719. '$mod.b = $mod.v == $mod.v;',
  24720. '$mod.b = $mod.v != $mod.v;',
  24721. '$mod.b = $mod.v == 1;',
  24722. '$mod.b = $mod.v != 1;',
  24723. '$mod.b = 2 == $mod.v;',
  24724. '$mod.b = 2 != $mod.v;',
  24725. '$mod.b = $mod.v == $mod.i;',
  24726. '$mod.b = $mod.i == $mod.v;',
  24727. '$mod.b = $mod.v == null;',
  24728. '$mod.b = null == $mod.v;',
  24729. '$mod.b = $mod.v == false;',
  24730. '$mod.b = true == $mod.v;',
  24731. '$mod.b = $mod.v == $mod.b;',
  24732. '$mod.b = $mod.b == $mod.v;',
  24733. '$mod.b = $mod.v == $mod.s;',
  24734. '$mod.b = $mod.s == $mod.v;',
  24735. '$mod.b = $mod.v == "foo";',
  24736. '$mod.b = "" == $mod.v;',
  24737. '$mod.b = $mod.v == $mod.d;',
  24738. '$mod.b = $mod.d == $mod.v;',
  24739. '$mod.b = $mod.v == 3.4;',
  24740. '$mod.b = 5.6 == $mod.v;',
  24741. '$mod.b = $mod.v == $mod.c;',
  24742. '$mod.b = $mod.c == $mod.v;',
  24743. '$mod.b = $mod.m == $mod.m;',
  24744. '$mod.b = $mod.v == $mod.m;',
  24745. '$mod.b = $mod.m == $mod.v;',
  24746. '']));
  24747. end;
  24748. procedure TTestModule.TestJSValue_If;
  24749. begin
  24750. StartProgram(false);
  24751. Add([
  24752. 'var',
  24753. ' v: jsvalue;',
  24754. 'begin',
  24755. ' if v then ;',
  24756. ' while v do ;',
  24757. ' repeat until v;',
  24758. '']);
  24759. ConvertProgram;
  24760. CheckSource('TestJSValue_If',
  24761. LinesToStr([ // statements
  24762. 'this.v = undefined;',
  24763. '']),
  24764. LinesToStr([ // $mod.$main
  24765. 'if ($mod.v) ;',
  24766. 'while($mod.v){',
  24767. '};',
  24768. 'do{',
  24769. '} while(!$mod.v);',
  24770. '']));
  24771. end;
  24772. procedure TTestModule.TestJSValue_Not;
  24773. begin
  24774. StartProgram(false);
  24775. Add([
  24776. 'var',
  24777. ' v: jsvalue;',
  24778. ' b: boolean;',
  24779. 'begin',
  24780. ' b:=not v;',
  24781. ' if not v then ;',
  24782. ' while not v do ;',
  24783. ' repeat until not v;',
  24784. '']);
  24785. ConvertProgram;
  24786. CheckSource('TestJSValue_If',
  24787. LinesToStr([ // statements
  24788. 'this.v = undefined;',
  24789. 'this.b = false;',
  24790. '']),
  24791. LinesToStr([ // $mod.$main
  24792. '$mod.b=!$mod.v;',
  24793. 'if (!$mod.v) ;',
  24794. 'while(!$mod.v){',
  24795. '};',
  24796. 'do{',
  24797. '} while($mod.v);',
  24798. '']));
  24799. end;
  24800. procedure TTestModule.TestJSValue_Enum;
  24801. begin
  24802. StartProgram(false);
  24803. Add('type');
  24804. Add(' TColor = (red, blue);');
  24805. Add(' TRedBlue = TColor;');
  24806. Add('var');
  24807. Add(' v: jsvalue;');
  24808. Add(' e: TColor;');
  24809. Add('begin');
  24810. Add(' v:=e;');
  24811. Add(' v:=TColor(e);');
  24812. Add(' v:=TRedBlue(e);');
  24813. Add(' e:=TColor(v);');
  24814. Add(' e:=TRedBlue(v);');
  24815. ConvertProgram;
  24816. CheckSource('TestJSValue_Enum',
  24817. LinesToStr([ // statements
  24818. 'this.TColor = {',
  24819. ' "0": "red",',
  24820. ' red: 0,',
  24821. ' "1": "blue",',
  24822. ' blue: 1',
  24823. '};',
  24824. 'this.v = undefined;',
  24825. 'this.e = 0;',
  24826. '']),
  24827. LinesToStr([ // $mod.$main
  24828. '$mod.v = $mod.e;',
  24829. '$mod.v = $mod.e;',
  24830. '$mod.v = $mod.e;',
  24831. '$mod.e = $mod.v;',
  24832. '$mod.e = $mod.v;',
  24833. '']));
  24834. end;
  24835. procedure TTestModule.TestJSValue_ClassInstance;
  24836. begin
  24837. StartProgram(false);
  24838. Add([
  24839. 'type',
  24840. ' TObject = class',
  24841. ' end;',
  24842. ' TBirdObject = TObject;',
  24843. 'var',
  24844. ' v: jsvalue;',
  24845. ' o: TObject;',
  24846. 'begin',
  24847. ' v:=o;',
  24848. ' v:=TObject(o);',
  24849. ' v:=TBirdObject(o);',
  24850. ' o:=TObject(v);',
  24851. ' o:=TBirdObject(v);',
  24852. ' if v is TObject then ;',
  24853. '']);
  24854. ConvertProgram;
  24855. CheckSource('TestJSValue_ClassInstance',
  24856. LinesToStr([ // statements
  24857. 'rtl.createClass($mod, "TObject", null, function () {',
  24858. ' this.$init = function () {',
  24859. ' };',
  24860. ' this.$final = function () {',
  24861. ' };',
  24862. '});',
  24863. 'this.v = undefined;',
  24864. 'this.o = null;',
  24865. '']),
  24866. LinesToStr([ // $mod.$main
  24867. '$mod.v = $mod.o;',
  24868. '$mod.v = $mod.o;',
  24869. '$mod.v = $mod.o;',
  24870. '$mod.o = rtl.getObject($mod.v);',
  24871. '$mod.o = rtl.getObject($mod.v);',
  24872. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  24873. '']));
  24874. end;
  24875. procedure TTestModule.TestJSValue_ClassOf;
  24876. begin
  24877. StartProgram(false);
  24878. Add([
  24879. 'type',
  24880. ' TClass = class of TObject;',
  24881. ' TObject = class',
  24882. ' end;',
  24883. ' TBirds = class of TBird;',
  24884. ' TBird = class(TObject) end;',
  24885. 'var',
  24886. ' v: jsvalue;',
  24887. ' c: TClass;',
  24888. 'begin',
  24889. ' v:=c;',
  24890. ' v:=TObject;',
  24891. ' v:=TClass(c);',
  24892. ' v:=TBirds(c);',
  24893. ' c:=TClass(v);',
  24894. ' c:=TBirds(v);',
  24895. ' if v is TClass then ;',
  24896. '']);
  24897. ConvertProgram;
  24898. CheckSource('TestJSValue_ClassOf',
  24899. LinesToStr([ // statements
  24900. 'rtl.createClass($mod, "TObject", null, function () {',
  24901. ' this.$init = function () {',
  24902. ' };',
  24903. ' this.$final = function () {',
  24904. ' };',
  24905. '});',
  24906. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  24907. '});',
  24908. 'this.v = undefined;',
  24909. 'this.c = null;',
  24910. '']),
  24911. LinesToStr([ // $mod.$main
  24912. '$mod.v = $mod.c;',
  24913. '$mod.v = $mod.TObject;',
  24914. '$mod.v = $mod.c;',
  24915. '$mod.v = $mod.c;',
  24916. '$mod.c = rtl.getObject($mod.v);',
  24917. '$mod.c = rtl.getObject($mod.v);',
  24918. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  24919. '']));
  24920. end;
  24921. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  24922. begin
  24923. StartProgram(false);
  24924. Add([
  24925. 'type',
  24926. ' integer = longint;',
  24927. ' TArray = array of JSValue;',
  24928. ' TArrgh = tarray;',
  24929. ' TArrInt = array of integer;',
  24930. 'var',
  24931. ' v: jsvalue;',
  24932. ' TheArray: tarray = (1,''2'');',
  24933. ' Arr: tarrgh;',
  24934. ' i: integer;',
  24935. ' ArrInt: tarrint;',
  24936. 'begin',
  24937. ' arr:=thearray;',
  24938. ' thearray:=arr;',
  24939. ' setlength(arr,2);',
  24940. ' setlength(thearray,3);',
  24941. ' arr[4]:=v;',
  24942. ' arr[5]:=length(thearray);',
  24943. ' arr[6]:=nil;',
  24944. ' arr[7]:=thearray[8];',
  24945. ' arr[low(arr)]:=high(thearray);',
  24946. ' arr:=arrint;',
  24947. ' arrInt:=tarrint(arr);',
  24948. ' if TheArray = nil then ;',
  24949. ' if nil = TheArray then ;',
  24950. ' if TheArray <> nil then ;',
  24951. ' if nil <> TheArray then ;',
  24952. '']);
  24953. ConvertProgram;
  24954. CheckSource('TestJSValue_ArrayOfJSValue',
  24955. LinesToStr([ // statements
  24956. 'this.v = undefined;',
  24957. 'this.TheArray = [1, "2"];',
  24958. 'this.Arr = [];',
  24959. 'this.i = 0;',
  24960. 'this.ArrInt = [];',
  24961. '']),
  24962. LinesToStr([ // $mod.$main
  24963. '$mod.Arr = $mod.TheArray;',
  24964. '$mod.TheArray = $mod.Arr;',
  24965. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  24966. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  24967. '$mod.Arr[4] = $mod.v;',
  24968. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  24969. '$mod.Arr[6] = null;',
  24970. '$mod.Arr[7] = $mod.TheArray[8];',
  24971. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  24972. '$mod.Arr = $mod.ArrInt;',
  24973. '$mod.ArrInt = $mod.Arr;',
  24974. 'if (rtl.length($mod.TheArray) === 0) ;',
  24975. 'if (rtl.length($mod.TheArray) === 0) ;',
  24976. 'if (rtl.length($mod.TheArray) > 0) ;',
  24977. 'if (rtl.length($mod.TheArray) > 0) ;',
  24978. '']));
  24979. end;
  24980. procedure TTestModule.TestJSValue_ArrayLit;
  24981. begin
  24982. StartProgram(false);
  24983. Add([
  24984. 'type',
  24985. ' TFlag = (big,small);',
  24986. ' TArray = array of JSValue;',
  24987. ' TObject = class end;',
  24988. ' TClass = class of TObject;',
  24989. 'var',
  24990. ' v: jsvalue;',
  24991. ' a: TArray;',
  24992. ' o: TObject;',
  24993. 'begin',
  24994. ' a:=[];',
  24995. ' a:=[1];',
  24996. ' a:=[1,2];',
  24997. ' a:=[big];',
  24998. ' a:=[1,big];',
  24999. ' a:=[o,nil];',
  25000. '']);
  25001. ConvertProgram;
  25002. CheckSource('TestJSValue_ArrayLit',
  25003. LinesToStr([ // statements
  25004. 'this.TFlag = {',
  25005. ' "0": "big",',
  25006. ' big: 0,',
  25007. ' "1": "small",',
  25008. ' small: 1',
  25009. '};',
  25010. 'rtl.createClass($mod, "TObject", null, function () {',
  25011. ' this.$init = function () {',
  25012. ' };',
  25013. ' this.$final = function () {',
  25014. ' };',
  25015. '});',
  25016. 'this.v = undefined;',
  25017. 'this.a = [];',
  25018. 'this.o = null;',
  25019. '']),
  25020. LinesToStr([ // $mod.$main
  25021. '$mod.a = [];',
  25022. '$mod.a = [1];',
  25023. '$mod.a = [1, 2];',
  25024. '$mod.a = [$mod.TFlag.big];',
  25025. '$mod.a = [1, $mod.TFlag.big];',
  25026. '$mod.a = [$mod.o, null];',
  25027. '']));
  25028. end;
  25029. procedure TTestModule.TestJSValue_Params;
  25030. begin
  25031. StartProgram(false);
  25032. Add('type');
  25033. Add(' integer = longint;');
  25034. Add(' TYesNo = boolean;');
  25035. Add(' TFloat = double;');
  25036. Add(' TCaption = string;');
  25037. Add(' TChar = char;');
  25038. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  25039. Add('var');
  25040. Add(' l: jsvalue;');
  25041. Add('begin');
  25042. Add(' a:=a;');
  25043. Add(' l:=b;');
  25044. Add(' c:=c;');
  25045. Add(' d:=d;');
  25046. Add(' Result:=l;');
  25047. Add('end;');
  25048. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  25049. Add('var');
  25050. Add(' v: jsvalue;');
  25051. Add(' i: integer;');
  25052. Add(' b: TYesNo;');
  25053. Add(' d: TFloat;');
  25054. Add(' s: TCaption;');
  25055. Add(' c: TChar;');
  25056. Add('begin');
  25057. Add(' v:=doit(v,v,v,v);');
  25058. Add(' i:=integer(dosome(i,i));');
  25059. Add(' b:=TYesNo(dosome(b,b));');
  25060. Add(' d:=TFloat(dosome(d,d));');
  25061. Add(' s:=TCaption(dosome(s,s));');
  25062. Add(' c:=TChar(dosome(c,c));');
  25063. ConvertProgram;
  25064. CheckSource('TestJSValue_Params',
  25065. LinesToStr([ // statements
  25066. 'this.DoIt = function (a, b, c, d) {',
  25067. ' var Result = undefined;',
  25068. ' var l = undefined;',
  25069. ' a = a;',
  25070. ' l = b;',
  25071. ' c.set(c.get());',
  25072. ' d.set(d.get());',
  25073. ' Result = l;',
  25074. ' return Result;',
  25075. '};',
  25076. 'this.DoSome = function (a, b) {',
  25077. ' var Result = undefined;',
  25078. ' return Result;',
  25079. '};',
  25080. 'this.v = undefined;',
  25081. 'this.i = 0;',
  25082. 'this.b = false;',
  25083. 'this.d = 0.0;',
  25084. 'this.s = "";',
  25085. 'this.c = "";',
  25086. '']),
  25087. LinesToStr([ // $mod.$main
  25088. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  25089. ' p: $mod,',
  25090. ' get: function () {',
  25091. ' return this.p.v;',
  25092. ' },',
  25093. ' set: function (v) {',
  25094. ' this.p.v = v;',
  25095. ' }',
  25096. '}, {',
  25097. ' p: $mod,',
  25098. ' get: function () {',
  25099. ' return this.p.v;',
  25100. ' },',
  25101. ' set: function (v) {',
  25102. ' this.p.v = v;',
  25103. ' }',
  25104. '});',
  25105. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  25106. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  25107. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  25108. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  25109. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  25110. '']));
  25111. end;
  25112. procedure TTestModule.TestJSValue_UntypedParam;
  25113. begin
  25114. StartProgram(false);
  25115. Add('function DoIt(const a; var b; out c): jsvalue;');
  25116. Add('begin');
  25117. Add(' Result:=a;');
  25118. Add(' Result:=b;');
  25119. Add(' Result:=c;');
  25120. Add(' b:=Result;');
  25121. Add(' c:=Result;');
  25122. Add('end;');
  25123. Add('var i: longint;');
  25124. Add('begin');
  25125. Add(' doit(i,i,i);');
  25126. ConvertProgram;
  25127. CheckSource('TestJSValue_UntypedParam',
  25128. LinesToStr([ // statements
  25129. 'this.DoIt = function (a, b, c) {',
  25130. ' var Result = undefined;',
  25131. ' Result = a;',
  25132. ' Result = b.get();',
  25133. ' Result = c.get();',
  25134. ' b.set(Result);',
  25135. ' c.set(Result);',
  25136. ' return Result;',
  25137. '};',
  25138. 'this.i = 0;',
  25139. '']),
  25140. LinesToStr([ // $mod.$main
  25141. '$mod.DoIt($mod.i, {',
  25142. ' p: $mod,',
  25143. ' get: function () {',
  25144. ' return this.p.i;',
  25145. ' },',
  25146. ' set: function (v) {',
  25147. ' this.p.i = v;',
  25148. ' }',
  25149. '}, {',
  25150. ' p: $mod,',
  25151. ' get: function () {',
  25152. ' return this.p.i;',
  25153. ' },',
  25154. ' set: function (v) {',
  25155. ' this.p.i = v;',
  25156. ' }',
  25157. '});',
  25158. '']));
  25159. end;
  25160. procedure TTestModule.TestJSValue_FuncResultType;
  25161. begin
  25162. StartProgram(false);
  25163. Add('type');
  25164. Add(' integer = longint;');
  25165. Add(' TJSValueArray = array of JSValue;');
  25166. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  25167. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  25168. Add('begin');
  25169. Add(' while Compare(P,aList[0])>0 do ;');
  25170. Add('end;');
  25171. Add('var');
  25172. Add(' Compare: TListSortCompare;');
  25173. Add(' V: JSValue;');
  25174. Add(' i: integer;');
  25175. Add('begin');
  25176. Add(' if Compare(V,V)>0 then ;');
  25177. Add(' if Compare(i,i)>1 then ;');
  25178. Add(' if Compare(nil,false)>2 then ;');
  25179. Add(' if Compare(1,true)>3 then ;');
  25180. ConvertProgram;
  25181. CheckSource('TestJSValue_UntypedParam',
  25182. LinesToStr([ // statements
  25183. 'this.Sort = function (P, aList, Compare) {',
  25184. ' while (Compare(P, aList[0]) > 0) {',
  25185. ' };',
  25186. '};',
  25187. 'this.Compare = null;',
  25188. 'this.V = undefined;',
  25189. 'this.i = 0;',
  25190. '']),
  25191. LinesToStr([ // $mod.$main
  25192. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  25193. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  25194. 'if ($mod.Compare(null, false) > 2) ;',
  25195. 'if ($mod.Compare(1, true) > 3) ;',
  25196. '']));
  25197. end;
  25198. procedure TTestModule.TestJSValue_ProcType_Assign;
  25199. begin
  25200. StartProgram(false);
  25201. Add('type');
  25202. Add(' integer = longint;');
  25203. Add(' TObject = class');
  25204. Add(' class function GetGlob: integer;');
  25205. Add(' function Getter: integer;');
  25206. Add(' end;');
  25207. Add('class function TObject.GetGlob: integer;');
  25208. Add('var v1: jsvalue;');
  25209. Add('begin');
  25210. Add(' v1:=@GetGlob;');
  25211. Add(' v1:[email protected];');
  25212. Add('end;');
  25213. Add('function TObject.Getter: integer;');
  25214. Add('var v2: jsvalue;');
  25215. Add('begin');
  25216. Add(' v2:=@Getter;');
  25217. Add(' v2:[email protected];');
  25218. Add(' v2:=@GetGlob;');
  25219. Add(' v2:[email protected];');
  25220. Add('end;');
  25221. Add('function GetIt(i: integer): integer;');
  25222. Add('var v3: jsvalue;');
  25223. Add('begin');
  25224. Add(' v3:=@GetIt;');
  25225. Add('end;');
  25226. Add('var');
  25227. Add(' V: JSValue;');
  25228. Add(' o: TObject;');
  25229. Add('begin');
  25230. Add(' v:=@GetIt;');
  25231. Add(' v:[email protected];');
  25232. Add(' v:[email protected];');
  25233. ConvertProgram;
  25234. CheckSource('TestJSValue_ProcType_Assign',
  25235. LinesToStr([ // statements
  25236. 'rtl.createClass($mod, "TObject", null, function () {',
  25237. ' this.$init = function () {',
  25238. ' };',
  25239. ' this.$final = function () {',
  25240. ' };',
  25241. ' this.GetGlob = function () {',
  25242. ' var Result = 0;',
  25243. ' var v1 = undefined;',
  25244. ' v1 = rtl.createCallback(this, "GetGlob");',
  25245. ' v1 = rtl.createCallback(this, "GetGlob");',
  25246. ' return Result;',
  25247. ' };',
  25248. ' this.Getter = function () {',
  25249. ' var Result = 0;',
  25250. ' var v2 = undefined;',
  25251. ' v2 = rtl.createCallback(this, "Getter");',
  25252. ' v2 = rtl.createCallback(this, "Getter");',
  25253. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  25254. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  25255. ' return Result;',
  25256. ' };',
  25257. '});',
  25258. 'this.GetIt = function (i) {',
  25259. ' var Result = 0;',
  25260. ' var v3 = undefined;',
  25261. ' v3 = $mod.GetIt;',
  25262. ' return Result;',
  25263. '};',
  25264. 'this.V = undefined;',
  25265. 'this.o = null;',
  25266. '']),
  25267. LinesToStr([ // $mod.$main
  25268. '$mod.V = $mod.GetIt;',
  25269. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  25270. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  25271. '']));
  25272. end;
  25273. procedure TTestModule.TestJSValue_ProcType_Equal;
  25274. begin
  25275. StartProgram(false);
  25276. Add('type');
  25277. Add(' integer = longint;');
  25278. Add(' TObject = class');
  25279. Add(' class function GetGlob: integer;');
  25280. Add(' function Getter: integer;');
  25281. Add(' end;');
  25282. Add('class function TObject.GetGlob: integer;');
  25283. Add('var v1: jsvalue;');
  25284. Add('begin');
  25285. Add(' if v1=@GetGlob then;');
  25286. Add(' if [email protected] then ;');
  25287. Add('end;');
  25288. Add('function TObject.Getter: integer;');
  25289. Add('var v2: jsvalue;');
  25290. Add('begin');
  25291. Add(' if v2=@Getter then;');
  25292. Add(' if [email protected] then ;');
  25293. Add(' if v2=@GetGlob then;');
  25294. Add(' if [email protected] then;');
  25295. Add('end;');
  25296. Add('function GetIt(i: integer): integer;');
  25297. Add('var v3: jsvalue;');
  25298. Add('begin');
  25299. Add(' if v3=@GetIt then;');
  25300. Add('end;');
  25301. Add('var');
  25302. Add(' V: JSValue;');
  25303. Add(' o: TObject;');
  25304. Add('begin');
  25305. Add(' if v=@GetIt then;');
  25306. Add(' if [email protected] then;');
  25307. Add(' if [email protected] then;');
  25308. Add(' if @GetIt=v then;');
  25309. Add(' if @o.Getter=v then;');
  25310. Add(' if @o.GetGlob=v then;');
  25311. ConvertProgram;
  25312. CheckSource('TestJSValue_ProcType_Equal',
  25313. LinesToStr([ // statements
  25314. 'rtl.createClass($mod, "TObject", null, function () {',
  25315. ' this.$init = function () {',
  25316. ' };',
  25317. ' this.$final = function () {',
  25318. ' };',
  25319. ' this.GetGlob = function () {',
  25320. ' var Result = 0;',
  25321. ' var v1 = undefined;',
  25322. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  25323. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  25324. ' return Result;',
  25325. ' };',
  25326. ' this.Getter = function () {',
  25327. ' var Result = 0;',
  25328. ' var v2 = undefined;',
  25329. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  25330. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  25331. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  25332. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  25333. ' return Result;',
  25334. ' };',
  25335. '});',
  25336. 'this.GetIt = function (i) {',
  25337. ' var Result = 0;',
  25338. ' var v3 = undefined;',
  25339. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  25340. ' return Result;',
  25341. '};',
  25342. 'this.V = undefined;',
  25343. 'this.o = null;',
  25344. '']),
  25345. LinesToStr([ // $mod.$main
  25346. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  25347. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  25348. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  25349. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  25350. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  25351. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  25352. '']));
  25353. end;
  25354. procedure TTestModule.TestJSValue_ProcType_Param;
  25355. begin
  25356. StartProgram(false);
  25357. Add([
  25358. 'type',
  25359. ' variant = jsvalue;',
  25360. ' TArrVariant = array of variant;',
  25361. ' TArrVar2 = TArrVariant;',
  25362. ' TFuncInt = function: longint;',
  25363. 'function GetIt: longint;',
  25364. 'begin',
  25365. 'end;',
  25366. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  25367. 'var v: variant;',
  25368. 'begin',
  25369. ' v:=arr[1];',
  25370. 'end;',
  25371. 'var s: string;',
  25372. 'begin',
  25373. ' DoIt(GetIt,[]);',
  25374. ' DoIt(@GetIt,[]);',
  25375. ' DoIt(1,[s,GetIt]);',
  25376. ' DoIt(1,[s,@GetIt]);',
  25377. '']);
  25378. ConvertProgram;
  25379. CheckSource('TestJSValue_ProcType_Param',
  25380. LinesToStr([ // statements
  25381. 'this.GetIt = function () {',
  25382. ' var Result = 0;',
  25383. ' return Result;',
  25384. '};',
  25385. 'this.DoIt = function (p, Arr) {',
  25386. ' var v = undefined;',
  25387. ' v = Arr[1];',
  25388. '};',
  25389. 'this.s = "";',
  25390. '']),
  25391. LinesToStr([ // $mod.$main
  25392. '$mod.DoIt($mod.GetIt(), []);',
  25393. '$mod.DoIt($mod.GetIt, []);',
  25394. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  25395. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  25396. '']));
  25397. end;
  25398. procedure TTestModule.TestJSValue_AssignToPointerFail;
  25399. begin
  25400. StartProgram(false);
  25401. Add([
  25402. 'var',
  25403. ' v: JSValue;',
  25404. ' p: Pointer;',
  25405. 'begin',
  25406. ' p:=v;',
  25407. '']);
  25408. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  25409. nIncompatibleTypesGotExpected);
  25410. ConvertProgram;
  25411. end;
  25412. procedure TTestModule.TestJSValue_OverloadDouble;
  25413. begin
  25414. StartProgram(false);
  25415. Add([
  25416. 'type',
  25417. ' integer = longint;',
  25418. ' tdatetime = double;',
  25419. 'procedure DoIt(d: double); begin end;',
  25420. 'procedure DoIt(v: jsvalue); begin end;',
  25421. 'var',
  25422. ' d: double;',
  25423. ' dt: tdatetime;',
  25424. ' i: integer;',
  25425. ' b: byte;',
  25426. ' shi: shortint;',
  25427. ' w: word;',
  25428. ' smi: smallint;',
  25429. ' lw: longword;',
  25430. ' li: longint;',
  25431. ' ni: nativeint;',
  25432. ' nu: nativeuint;',
  25433. 'begin',
  25434. ' DoIt(d);',
  25435. ' DoIt(dt);',
  25436. ' DoIt(i);',
  25437. ' DoIt(b);',
  25438. ' DoIt(shi);',
  25439. ' DoIt(w);',
  25440. ' DoIt(smi);',
  25441. ' DoIt(lw);',
  25442. ' DoIt(li);',
  25443. ' DoIt(ni);',
  25444. ' DoIt(nu);',
  25445. '']);
  25446. ConvertProgram;
  25447. CheckSource('TestJSValue_OverloadDouble',
  25448. LinesToStr([ // statements
  25449. 'this.DoIt = function (d) {',
  25450. '};',
  25451. 'this.DoIt$1 = function (v) {',
  25452. '};',
  25453. 'this.d = 0.0;',
  25454. 'this.dt = 0.0;',
  25455. 'this.i = 0;',
  25456. 'this.b = 0;',
  25457. 'this.shi = 0;',
  25458. 'this.w = 0;',
  25459. 'this.smi = 0;',
  25460. 'this.lw = 0;',
  25461. 'this.li = 0;',
  25462. 'this.ni = 0;',
  25463. 'this.nu = 0;',
  25464. '']),
  25465. LinesToStr([ // $mod.$main
  25466. '$mod.DoIt($mod.d);',
  25467. '$mod.DoIt($mod.dt);',
  25468. '$mod.DoIt$1($mod.i);',
  25469. '$mod.DoIt$1($mod.b);',
  25470. '$mod.DoIt$1($mod.shi);',
  25471. '$mod.DoIt$1($mod.w);',
  25472. '$mod.DoIt$1($mod.smi);',
  25473. '$mod.DoIt$1($mod.lw);',
  25474. '$mod.DoIt$1($mod.li);',
  25475. '$mod.DoIt$1($mod.ni);',
  25476. '$mod.DoIt$1($mod.nu);',
  25477. '']));
  25478. end;
  25479. procedure TTestModule.TestJSValue_OverloadNativeInt;
  25480. begin
  25481. StartProgram(false);
  25482. Add([
  25483. 'type',
  25484. ' integer = longint;',
  25485. ' int53 = nativeint;',
  25486. ' tdatetime = double;',
  25487. 'procedure DoIt(n: nativeint); begin end;',
  25488. 'procedure DoIt(v: jsvalue); begin end;',
  25489. 'var',
  25490. ' d: double;',
  25491. ' dt: tdatetime;',
  25492. ' i: integer;',
  25493. ' b: byte;',
  25494. ' shi: shortint;',
  25495. ' w: word;',
  25496. ' smi: smallint;',
  25497. ' lw: longword;',
  25498. ' li: longint;',
  25499. ' ni: nativeint;',
  25500. ' nu: nativeuint;',
  25501. 'begin',
  25502. ' DoIt(d);',
  25503. ' DoIt(dt);',
  25504. ' DoIt(i);',
  25505. ' DoIt(b);',
  25506. ' DoIt(shi);',
  25507. ' DoIt(w);',
  25508. ' DoIt(smi);',
  25509. ' DoIt(lw);',
  25510. ' DoIt(li);',
  25511. ' DoIt(ni);',
  25512. ' DoIt(nu);',
  25513. '']);
  25514. ConvertProgram;
  25515. CheckSource('TestJSValue_OverloadNativeInt',
  25516. LinesToStr([ // statements
  25517. 'this.DoIt = function (n) {',
  25518. '};',
  25519. 'this.DoIt$1 = function (v) {',
  25520. '};',
  25521. 'this.d = 0.0;',
  25522. 'this.dt = 0.0;',
  25523. 'this.i = 0;',
  25524. 'this.b = 0;',
  25525. 'this.shi = 0;',
  25526. 'this.w = 0;',
  25527. 'this.smi = 0;',
  25528. 'this.lw = 0;',
  25529. 'this.li = 0;',
  25530. 'this.ni = 0;',
  25531. 'this.nu = 0;',
  25532. '']),
  25533. LinesToStr([ // $mod.$main
  25534. '$mod.DoIt$1($mod.d);',
  25535. '$mod.DoIt$1($mod.dt);',
  25536. '$mod.DoIt($mod.i);',
  25537. '$mod.DoIt($mod.b);',
  25538. '$mod.DoIt($mod.shi);',
  25539. '$mod.DoIt($mod.w);',
  25540. '$mod.DoIt($mod.smi);',
  25541. '$mod.DoIt($mod.lw);',
  25542. '$mod.DoIt($mod.li);',
  25543. '$mod.DoIt($mod.ni);',
  25544. '$mod.DoIt($mod.nu);',
  25545. '']));
  25546. end;
  25547. procedure TTestModule.TestJSValue_OverloadWord;
  25548. begin
  25549. StartProgram(false);
  25550. Add([
  25551. 'type',
  25552. ' integer = longint;',
  25553. ' int53 = nativeint;',
  25554. ' tdatetime = double;',
  25555. 'procedure DoIt(w: word); begin end;',
  25556. 'procedure DoIt(v: jsvalue); begin end;',
  25557. 'var',
  25558. ' d: double;',
  25559. ' dt: tdatetime;',
  25560. ' i: integer;',
  25561. ' b: byte;',
  25562. ' shi: shortint;',
  25563. ' w: word;',
  25564. ' smi: smallint;',
  25565. ' lw: longword;',
  25566. ' li: longint;',
  25567. ' ni: nativeint;',
  25568. ' nu: nativeuint;',
  25569. 'begin',
  25570. ' DoIt(d);',
  25571. ' DoIt(dt);',
  25572. ' DoIt(i);',
  25573. ' DoIt(b);',
  25574. ' DoIt(shi);',
  25575. ' DoIt(w);',
  25576. ' DoIt(smi);',
  25577. ' DoIt(lw);',
  25578. ' DoIt(li);',
  25579. ' DoIt(ni);',
  25580. ' DoIt(nu);',
  25581. '']);
  25582. ConvertProgram;
  25583. CheckSource('TestJSValue_OverloadWord',
  25584. LinesToStr([ // statements
  25585. 'this.DoIt = function (w) {',
  25586. '};',
  25587. 'this.DoIt$1 = function (v) {',
  25588. '};',
  25589. 'this.d = 0.0;',
  25590. 'this.dt = 0.0;',
  25591. 'this.i = 0;',
  25592. 'this.b = 0;',
  25593. 'this.shi = 0;',
  25594. 'this.w = 0;',
  25595. 'this.smi = 0;',
  25596. 'this.lw = 0;',
  25597. 'this.li = 0;',
  25598. 'this.ni = 0;',
  25599. 'this.nu = 0;',
  25600. '']),
  25601. LinesToStr([ // $mod.$main
  25602. '$mod.DoIt$1($mod.d);',
  25603. '$mod.DoIt$1($mod.dt);',
  25604. '$mod.DoIt$1($mod.i);',
  25605. '$mod.DoIt($mod.b);',
  25606. '$mod.DoIt($mod.shi);',
  25607. '$mod.DoIt($mod.w);',
  25608. '$mod.DoIt$1($mod.smi);',
  25609. '$mod.DoIt$1($mod.lw);',
  25610. '$mod.DoIt$1($mod.li);',
  25611. '$mod.DoIt$1($mod.ni);',
  25612. '$mod.DoIt$1($mod.nu);',
  25613. '']));
  25614. end;
  25615. procedure TTestModule.TestJSValue_OverloadString;
  25616. begin
  25617. StartProgram(false);
  25618. Add([
  25619. 'type',
  25620. ' uni = string;',
  25621. ' WChar = char;',
  25622. 'procedure DoIt(s: string); begin end;',
  25623. 'procedure DoIt(v: jsvalue); begin end;',
  25624. 'var',
  25625. ' s: string;',
  25626. ' c: char;',
  25627. ' u: uni;',
  25628. 'begin',
  25629. ' DoIt(s);',
  25630. ' DoIt(c);',
  25631. ' DoIt(u);',
  25632. '']);
  25633. ConvertProgram;
  25634. CheckSource('TestJSValue_OverloadString',
  25635. LinesToStr([ // statements
  25636. 'this.DoIt = function (s) {',
  25637. '};',
  25638. 'this.DoIt$1 = function (v) {',
  25639. '};',
  25640. 'this.s = "";',
  25641. 'this.c = "";',
  25642. 'this.u = "";',
  25643. '']),
  25644. LinesToStr([ // $mod.$main
  25645. '$mod.DoIt($mod.s);',
  25646. '$mod.DoIt($mod.c);',
  25647. '$mod.DoIt($mod.u);',
  25648. '']));
  25649. end;
  25650. procedure TTestModule.TestJSValue_OverloadChar;
  25651. begin
  25652. StartProgram(false);
  25653. Add([
  25654. 'type',
  25655. ' uni = string;',
  25656. ' WChar = char;',
  25657. 'procedure DoIt(c: char); begin end;',
  25658. 'procedure DoIt(v: jsvalue); begin end;',
  25659. 'var',
  25660. ' s: string;',
  25661. ' c: char;',
  25662. ' u: uni;',
  25663. 'begin',
  25664. ' DoIt(s);',
  25665. ' DoIt(c);',
  25666. ' DoIt(u);',
  25667. '']);
  25668. ConvertProgram;
  25669. CheckSource('TestJSValue_OverloadChar',
  25670. LinesToStr([ // statements
  25671. 'this.DoIt = function (c) {',
  25672. '};',
  25673. 'this.DoIt$1 = function (v) {',
  25674. '};',
  25675. 'this.s = "";',
  25676. 'this.c = "";',
  25677. 'this.u = "";',
  25678. '']),
  25679. LinesToStr([ // $mod.$main
  25680. '$mod.DoIt$1($mod.s);',
  25681. '$mod.DoIt($mod.c);',
  25682. '$mod.DoIt$1($mod.u);',
  25683. '']));
  25684. end;
  25685. procedure TTestModule.TestJSValue_OverloadPointer;
  25686. begin
  25687. StartProgram(false);
  25688. Add([
  25689. 'type',
  25690. ' TObject = class end;',
  25691. 'procedure DoIt(p: pointer); begin end;',
  25692. 'procedure DoIt(v: jsvalue); begin end;',
  25693. 'var',
  25694. ' o: TObject;',
  25695. 'begin',
  25696. ' DoIt(o);',
  25697. '']);
  25698. ConvertProgram;
  25699. CheckSource('TestJSValue_OverloadPointer',
  25700. LinesToStr([ // statements
  25701. 'rtl.createClass($mod, "TObject", null, function () {',
  25702. ' this.$init = function () {',
  25703. ' };',
  25704. ' this.$final = function () {',
  25705. ' };',
  25706. '});',
  25707. 'this.DoIt = function (p) {',
  25708. '};',
  25709. 'this.DoIt$1 = function (v) {',
  25710. '};',
  25711. 'this.o = null;',
  25712. '']),
  25713. LinesToStr([ // $mod.$main
  25714. '$mod.DoIt($mod.o);',
  25715. '']));
  25716. end;
  25717. procedure TTestModule.TestJSValue_ForIn;
  25718. begin
  25719. StartProgram(false);
  25720. Add([
  25721. 'var',
  25722. ' v: JSValue;',
  25723. ' key: string;',
  25724. 'begin',
  25725. ' for key in v do begin',
  25726. ' if key=''abc'' then ;',
  25727. ' end;',
  25728. '']);
  25729. ConvertProgram;
  25730. CheckSource('TestJSValue_ForIn',
  25731. LinesToStr([ // statements
  25732. 'this.v = undefined;',
  25733. 'this.key = "";',
  25734. '']),
  25735. LinesToStr([ // $mod.$main
  25736. 'for ($mod.key in $mod.v) {',
  25737. ' if ($mod.key === "abc") ;',
  25738. '};',
  25739. '']));
  25740. end;
  25741. procedure TTestModule.TestRTTI_IntRange;
  25742. begin
  25743. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25744. StartProgram(false);
  25745. Add([
  25746. '{$modeswitch externalclass}',
  25747. 'type',
  25748. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  25749. ' end;',
  25750. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  25751. ' end;',
  25752. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  25753. ' TColor = type TGraphicsColor;',
  25754. 'var',
  25755. ' p: TTypeInfo;',
  25756. 'begin',
  25757. ' p:=typeinfo(TGraphicsColor);',
  25758. ' p:=typeinfo(TColor);',
  25759. '']);
  25760. ConvertProgram;
  25761. CheckSource('TestRTTI_IntRange',
  25762. LinesToStr([ // statements
  25763. '$mod.$rtti.$Int("TGraphicsColor", {',
  25764. ' minvalue: -2147483648,',
  25765. ' maxvalue: 2147483647,',
  25766. ' ordtype: 4',
  25767. '});',
  25768. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  25769. 'this.p = null;',
  25770. '']),
  25771. LinesToStr([ // $mod.$main
  25772. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  25773. '$mod.p = $mod.$rtti["TColor"];',
  25774. '']));
  25775. end;
  25776. procedure TTestModule.TestRTTI_Double;
  25777. begin
  25778. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25779. StartProgram(false);
  25780. Add([
  25781. '{$modeswitch externalclass}',
  25782. 'type',
  25783. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  25784. ' end;',
  25785. ' TFloat = type double;',
  25786. 'var',
  25787. ' p: TTypeInfo;',
  25788. 'begin',
  25789. ' p:=typeinfo(double);',
  25790. ' p:=typeinfo(TFloat);',
  25791. '']);
  25792. ConvertProgram;
  25793. CheckSource('TestRTTI_Double',
  25794. LinesToStr([ // statements
  25795. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  25796. 'this.p = null;',
  25797. '']),
  25798. LinesToStr([ // $mod.$main
  25799. '$mod.p = rtl.double;',
  25800. '$mod.p = $mod.$rtti["TFloat"];',
  25801. '']));
  25802. end;
  25803. procedure TTestModule.TestRTTI_ProcType;
  25804. begin
  25805. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25806. StartProgram(false);
  25807. Add('type');
  25808. Add(' TProcA = procedure;');
  25809. Add(' TMethodB = procedure of object;');
  25810. Add(' TProcC = procedure; varargs;');
  25811. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  25812. Add(' TProcE = function: nativeint;');
  25813. Add(' TProcF = function(const p: TProcA): nativeuint;');
  25814. Add('var p: pointer;');
  25815. Add('begin');
  25816. Add(' p:=typeinfo(tproca);');
  25817. ConvertProgram;
  25818. CheckSource('TestRTTI_ProcType',
  25819. LinesToStr([ // statements
  25820. '$mod.$rtti.$ProcVar("TProcA", {',
  25821. ' procsig: rtl.newTIProcSig(null)',
  25822. '});',
  25823. '$mod.$rtti.$MethodVar("TMethodB", {',
  25824. ' procsig: rtl.newTIProcSig(null),',
  25825. ' methodkind: 0',
  25826. '});',
  25827. '$mod.$rtti.$ProcVar("TProcC", {',
  25828. ' procsig: rtl.newTIProcSig(null, 2)',
  25829. '});',
  25830. '$mod.$rtti.$ProcVar("TProcD", {',
  25831. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  25832. '});',
  25833. '$mod.$rtti.$ProcVar("TProcE", {',
  25834. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  25835. '});',
  25836. '$mod.$rtti.$ProcVar("TProcF", {',
  25837. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  25838. '});',
  25839. 'this.p = null;',
  25840. '']),
  25841. LinesToStr([ // $mod.$main
  25842. '$mod.p = $mod.$rtti["TProcA"];',
  25843. '']));
  25844. end;
  25845. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  25846. begin
  25847. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25848. AddModuleWithIntfImplSrc('unit2.pas',
  25849. LinesToStr([
  25850. 'type',
  25851. ' TObject = class end;'
  25852. ]),
  25853. '');
  25854. StartUnit(true);
  25855. Add('interface');
  25856. Add('uses unit2;');
  25857. Add('type');
  25858. Add(' TProcA = function(o: tobject): tobject;');
  25859. Add('implementation');
  25860. Add('type');
  25861. Add(' TProcB = function(o: tobject): tobject;');
  25862. Add('var p: Pointer;');
  25863. Add('initialization');
  25864. Add(' p:=typeinfo(tproca);');
  25865. Add(' p:=typeinfo(tprocb);');
  25866. ConvertUnit;
  25867. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  25868. LinesToStr([ // statements
  25869. 'var $impl = $mod.$impl;',
  25870. '$mod.$rtti.$ProcVar("TProcA", {',
  25871. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  25872. '});',
  25873. '']),
  25874. LinesToStr([ // this.$init
  25875. '$impl.p = $mod.$rtti["TProcA"];',
  25876. '$impl.p = $mod.$rtti["TProcB"];',
  25877. '']),
  25878. LinesToStr([ // implementation
  25879. '$mod.$rtti.$ProcVar("TProcB", {',
  25880. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  25881. '});',
  25882. '$impl.p = null;',
  25883. '']) );
  25884. end;
  25885. procedure TTestModule.TestRTTI_EnumAndSetType;
  25886. begin
  25887. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25888. StartProgram(false);
  25889. Add('type');
  25890. Add(' TFlag = (light,dark);');
  25891. Add(' TFlags = set of TFlag;');
  25892. Add(' TProc = function(f: TFlags): TFlag;');
  25893. Add('var p: pointer;');
  25894. Add('begin');
  25895. Add(' p:=typeinfo(tflag);');
  25896. Add(' p:=typeinfo(tflags);');
  25897. ConvertProgram;
  25898. CheckSource('TestRTTI_EnumAndType',
  25899. LinesToStr([ // statements
  25900. 'this.TFlag = {',
  25901. ' "0": "light",',
  25902. ' light: 0,',
  25903. ' "1": "dark",',
  25904. ' dark: 1',
  25905. '};',
  25906. '$mod.$rtti.$Enum("TFlag", {',
  25907. ' minvalue: 0,',
  25908. ' maxvalue: 1,',
  25909. ' ordtype: 1,',
  25910. ' enumtype: this.TFlag',
  25911. '});',
  25912. '$mod.$rtti.$Set("TFlags", {',
  25913. ' comptype: $mod.$rtti["TFlag"]',
  25914. '});',
  25915. '$mod.$rtti.$ProcVar("TProc", {',
  25916. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  25917. '});',
  25918. 'this.p = null;',
  25919. '']),
  25920. LinesToStr([ // $mod.$main
  25921. '$mod.p = $mod.$rtti["TFlag"];',
  25922. '$mod.p = $mod.$rtti["TFlags"];',
  25923. '']));
  25924. end;
  25925. procedure TTestModule.TestRTTI_EnumRange;
  25926. begin
  25927. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25928. StartProgram(false);
  25929. Add([
  25930. 'type',
  25931. ' TCol = (red,green,blue);',
  25932. ' TColRg = green..blue;',
  25933. ' TSetOfColRg = set of TColRg;',
  25934. 'var p: pointer;',
  25935. 'begin',
  25936. ' p:=typeinfo(tcolrg);',
  25937. ' p:=typeinfo(tsetofcolrg);',
  25938. '']);
  25939. ConvertProgram;
  25940. end;
  25941. procedure TTestModule.TestRTTI_AnonymousEnumType;
  25942. begin
  25943. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25944. StartProgram(false);
  25945. Add('type');
  25946. Add(' TFlags = set of (red, green);');
  25947. Add('var');
  25948. Add(' f: TFlags;');
  25949. Add('begin');
  25950. Add(' Include(f,red);');
  25951. ConvertProgram;
  25952. CheckSource('TestRTTI_AnonymousEnumType',
  25953. LinesToStr([ // statements
  25954. 'this.TFlags$a = {',
  25955. ' "0": "red",',
  25956. ' red: 0,',
  25957. ' "1": "green",',
  25958. ' green: 1',
  25959. '};',
  25960. '$mod.$rtti.$Enum("TFlags$a", {',
  25961. ' minvalue: 0,',
  25962. ' maxvalue: 1,',
  25963. ' ordtype: 1,',
  25964. ' enumtype: this.TFlags$a',
  25965. '});',
  25966. '$mod.$rtti.$Set("TFlags", {',
  25967. ' comptype: $mod.$rtti["TFlags$a"]',
  25968. '});',
  25969. 'this.f = {};',
  25970. '']),
  25971. LinesToStr([
  25972. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  25973. '']));
  25974. end;
  25975. procedure TTestModule.TestRTTI_StaticArray;
  25976. begin
  25977. Converter.Options:=Converter.Options-[coNoTypeInfo];
  25978. StartProgram(false);
  25979. Add('type');
  25980. Add(' TFlag = (light,dark);');
  25981. Add(' TFlagNames = array[TFlag] of string;');
  25982. Add(' TBoolNames = array[boolean] of string;');
  25983. Add(' TByteArray = array[1..32768] of byte;');
  25984. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  25985. Add('var p: pointer;');
  25986. Add('begin');
  25987. Add(' p:=typeinfo(TFlagNames);');
  25988. Add(' p:=typeinfo(TBoolNames);');
  25989. ConvertProgram;
  25990. CheckSource('TestRTTI_StaticArray',
  25991. LinesToStr([ // statements
  25992. 'this.TFlag = {',
  25993. ' "0": "light",',
  25994. ' light: 0,',
  25995. ' "1": "dark",',
  25996. ' dark: 1',
  25997. '};',
  25998. '$mod.$rtti.$Enum("TFlag", {',
  25999. ' minvalue: 0,',
  26000. ' maxvalue: 1,',
  26001. ' ordtype: 1,',
  26002. ' enumtype: this.TFlag',
  26003. '});',
  26004. '$mod.$rtti.$StaticArray("TFlagNames", {',
  26005. ' dims: [2],',
  26006. ' eltype: rtl.string',
  26007. '});',
  26008. '$mod.$rtti.$StaticArray("TBoolNames", {',
  26009. ' dims: [2],',
  26010. ' eltype: rtl.string',
  26011. '});',
  26012. '$mod.$rtti.$StaticArray("TByteArray", {',
  26013. ' dims: [32768],',
  26014. ' eltype: rtl.byte',
  26015. '});',
  26016. '$mod.$rtti.$ProcVar("TProc", {',
  26017. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  26018. '});',
  26019. 'this.p = null;',
  26020. '']),
  26021. LinesToStr([ // $mod.$main
  26022. '$mod.p = $mod.$rtti["TFlagNames"];',
  26023. '$mod.p = $mod.$rtti["TBoolNames"];',
  26024. '']));
  26025. end;
  26026. procedure TTestModule.TestRTTI_DynArray;
  26027. begin
  26028. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26029. StartProgram(false);
  26030. Add('type');
  26031. Add(' TArrStr = array of string;');
  26032. Add(' TArr2Dim = array of tarrstr;');
  26033. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  26034. Add('var p: pointer;');
  26035. Add('begin');
  26036. Add(' p:=typeinfo(tarrstr);');
  26037. Add(' p:=typeinfo(tarr2dim);');
  26038. ConvertProgram;
  26039. CheckSource('TestRTTI_DynArray',
  26040. LinesToStr([ // statements
  26041. '$mod.$rtti.$DynArray("TArrStr", {',
  26042. ' eltype: rtl.string',
  26043. '});',
  26044. '$mod.$rtti.$DynArray("TArr2Dim", {',
  26045. ' eltype: $mod.$rtti["TArrStr"]',
  26046. '});',
  26047. '$mod.$rtti.$ProcVar("TProc", {',
  26048. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  26049. '});',
  26050. 'this.p = null;',
  26051. '']),
  26052. LinesToStr([ // $mod.$main
  26053. '$mod.p = $mod.$rtti["TArrStr"];',
  26054. '$mod.p = $mod.$rtti["TArr2Dim"];',
  26055. '']));
  26056. end;
  26057. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  26058. begin
  26059. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26060. StartProgram(false);
  26061. Add('type');
  26062. Add(' TArr = array of array of longint;');
  26063. Add('var a: TArr;');
  26064. Add('begin');
  26065. ConvertProgram;
  26066. CheckSource('TestRTTI_ArrayNestedAnonymous',
  26067. LinesToStr([ // statements
  26068. '$mod.$rtti.$DynArray("TArr$a", {',
  26069. ' eltype: rtl.longint',
  26070. '});',
  26071. '$mod.$rtti.$DynArray("TArr", {',
  26072. ' eltype: $mod.$rtti["TArr$a"]',
  26073. '});',
  26074. 'this.a = [];',
  26075. '']),
  26076. LinesToStr([ // $mod.$main
  26077. ]));
  26078. end;
  26079. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  26080. begin
  26081. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26082. StartProgram(false);
  26083. Add('type');
  26084. Add(' TObject = class');
  26085. Add(' published');
  26086. Add(' procedure Proc; virtual; abstract;');
  26087. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  26088. Add(' end;');
  26089. Add('begin');
  26090. SetExpectedPasResolverError('Duplicate identifier "Proc" at test1.pp(6,19)',
  26091. nDuplicateIdentifier);
  26092. ConvertProgram;
  26093. end;
  26094. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  26095. begin
  26096. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26097. StartProgram(false);
  26098. Add('type');
  26099. Add(' TObject = class');
  26100. Add(' published');
  26101. Add(' procedure Proc; external name ''foo'';');
  26102. Add(' end;');
  26103. Add('begin');
  26104. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  26105. nPublishedNameMustMatchExternal);
  26106. ConvertProgram;
  26107. end;
  26108. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  26109. begin
  26110. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26111. StartProgram(false);
  26112. Add('type');
  26113. Add(' TObject = class');
  26114. Add(' class var FA: longint;');
  26115. Add(' published');
  26116. Add(' class property A: longint read FA;');
  26117. Add(' end;');
  26118. Add('begin');
  26119. SetExpectedPasResolverError('Invalid published property modifier "class"',
  26120. nInvalidXModifierY);
  26121. ConvertProgram;
  26122. end;
  26123. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  26124. begin
  26125. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26126. StartProgram(false);
  26127. Add('type');
  26128. Add(' TObject = class');
  26129. Add(' published');
  26130. Add(' class var FA: longint;');
  26131. Add(' end;');
  26132. Add('begin');
  26133. SetExpectedPasResolverError(sSymbolCannotBePublished,
  26134. nSymbolCannotBePublished);
  26135. ConvertProgram;
  26136. end;
  26137. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  26138. begin
  26139. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26140. StartProgram(false);
  26141. Add('{$modeswitch externalclass}');
  26142. Add('type');
  26143. Add(' TObject = class');
  26144. Add(' published');
  26145. Add(' V: longint; external name ''foo'';');
  26146. Add(' end;');
  26147. Add('begin');
  26148. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  26149. nPublishedNameMustMatchExternal);
  26150. ConvertProgram;
  26151. end;
  26152. procedure TTestModule.TestRTTI_Class_Field;
  26153. begin
  26154. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26155. StartProgram(false);
  26156. Add('{$modeswitch externalclass}');
  26157. Add('type');
  26158. Add(' TObject = class');
  26159. Add(' private');
  26160. Add(' FPropA: string;');
  26161. Add(' published');
  26162. Add(' VarLI: longint;');
  26163. Add(' VarC: char;');
  26164. Add(' VarS: string;');
  26165. Add(' VarD: double;');
  26166. Add(' VarB: boolean;');
  26167. Add(' VarLW: longword;');
  26168. Add(' VarSmI: smallint;');
  26169. Add(' VarW: word;');
  26170. Add(' VarShI: shortint;');
  26171. Add(' VarBy: byte;');
  26172. Add(' VarExt: longint external name ''VarExt'';');
  26173. Add(' end;');
  26174. Add('var p: pointer;');
  26175. Add(' Obj: tobject;');
  26176. Add('begin');
  26177. Add(' p:=typeinfo(tobject);');
  26178. Add(' p:=typeinfo(p);');
  26179. Add(' p:=typeinfo(obj);');
  26180. ConvertProgram;
  26181. CheckSource('TestRTTI_Class_Field',
  26182. LinesToStr([ // statements
  26183. 'rtl.createClass($mod, "TObject", null, function () {',
  26184. ' this.$init = function () {',
  26185. ' this.FPropA = "";',
  26186. ' this.VarLI = 0;',
  26187. ' this.VarC = "";',
  26188. ' this.VarS = "";',
  26189. ' this.VarD = 0.0;',
  26190. ' this.VarB = false;',
  26191. ' this.VarLW = 0;',
  26192. ' this.VarSmI = 0;',
  26193. ' this.VarW = 0;',
  26194. ' this.VarShI = 0;',
  26195. ' this.VarBy = 0;',
  26196. ' };',
  26197. ' this.$final = function () {',
  26198. ' };',
  26199. ' var $r = this.$rtti;',
  26200. ' $r.addField("VarLI", rtl.longint);',
  26201. ' $r.addField("VarC", rtl.char);',
  26202. ' $r.addField("VarS", rtl.string);',
  26203. ' $r.addField("VarD", rtl.double);',
  26204. ' $r.addField("VarB", rtl.boolean);',
  26205. ' $r.addField("VarLW", rtl.longword);',
  26206. ' $r.addField("VarSmI", rtl.smallint);',
  26207. ' $r.addField("VarW", rtl.word);',
  26208. ' $r.addField("VarShI", rtl.shortint);',
  26209. ' $r.addField("VarBy", rtl.byte);',
  26210. ' $r.addField("VarExt", rtl.longint);',
  26211. '});',
  26212. 'this.p = null;',
  26213. 'this.Obj = null;',
  26214. '']),
  26215. LinesToStr([ // $mod.$main
  26216. '$mod.p = $mod.$rtti["TObject"];',
  26217. '$mod.p = rtl.pointer;',
  26218. '$mod.p = $mod.Obj.$rtti;',
  26219. '']));
  26220. end;
  26221. procedure TTestModule.TestRTTI_Class_Method;
  26222. begin
  26223. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26224. StartProgram(false);
  26225. Add('type');
  26226. Add(' TObject = class');
  26227. Add(' private');
  26228. Add(' procedure Internal; external name ''$intern'';');
  26229. Add(' published');
  26230. Add(' procedure Click; virtual; abstract;');
  26231. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  26232. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  26233. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  26234. Add(' end;');
  26235. Add('begin');
  26236. ConvertProgram;
  26237. CheckSource('TestRTTI_Class_Method',
  26238. LinesToStr([ // statements
  26239. 'rtl.createClass($mod, "TObject", null, function () {',
  26240. ' this.$init = function () {',
  26241. ' };',
  26242. ' this.$final = function () {',
  26243. ' };',
  26244. ' var $r = this.$rtti;',
  26245. ' $r.addMethod("Click", 0, null);',
  26246. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  26247. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  26248. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  26249. ' flags: 2',
  26250. ' });',
  26251. '});',
  26252. '']),
  26253. LinesToStr([ // $mod.$main
  26254. '']));
  26255. end;
  26256. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  26257. begin
  26258. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26259. StartProgram(false);
  26260. Add('type');
  26261. Add(' TObject = class');
  26262. Add(' published');
  26263. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  26264. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  26265. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  26266. Add(' end;');
  26267. Add('begin');
  26268. ConvertProgram;
  26269. CheckSource('TestRTTI_Class_MethodOpenArray',
  26270. LinesToStr([ // statements
  26271. 'rtl.createClass($mod, "TObject", null, function () {',
  26272. ' this.$init = function () {',
  26273. ' };',
  26274. ' this.$final = function () {',
  26275. ' };',
  26276. ' var $r = this.$rtti;',
  26277. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  26278. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  26279. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  26280. '});',
  26281. '']),
  26282. LinesToStr([ // $mod.$main
  26283. '']));
  26284. end;
  26285. procedure TTestModule.TestRTTI_Class_Property;
  26286. begin
  26287. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26288. StartProgram(false);
  26289. Add('{$modeswitch externalclass}');
  26290. Add('type');
  26291. Add(' TObject = class');
  26292. Add(' private');
  26293. Add(' FColor: longint;');
  26294. Add(' FColorStored: boolean;');
  26295. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  26296. Add(' function GetColor: longint; virtual; abstract;');
  26297. Add(' function GetColorStored: boolean; virtual; abstract;');
  26298. Add(' FExtSize: longint external name ''$extSize'';');
  26299. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  26300. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  26301. Add(' function GetExtSize: longint; external name ''$getSize'';');
  26302. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  26303. Add(' published');
  26304. Add(' property ColorA: longint read FColor;');
  26305. Add(' property ColorB: longint write FColor;');
  26306. Add(' property ColorC: longint read GetColor write SetColor;');
  26307. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  26308. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  26309. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  26310. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  26311. Add(' end;');
  26312. Add('begin');
  26313. ConvertProgram;
  26314. CheckSource('TestRTTI_Class_Property',
  26315. LinesToStr([ // statements
  26316. 'rtl.createClass($mod, "TObject", null, function () {',
  26317. ' this.$init = function () {',
  26318. ' this.FColor = 0;',
  26319. ' this.FColorStored = false;',
  26320. ' };',
  26321. ' this.$final = function () {',
  26322. ' };',
  26323. ' var $r = this.$rtti;',
  26324. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  26325. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  26326. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  26327. ' $r.addProperty(',
  26328. ' "ColorD",',
  26329. ' 8,',
  26330. ' rtl.longint,',
  26331. ' "FColor",',
  26332. ' "FColor",',
  26333. ' {',
  26334. ' stored: "FColorStored"',
  26335. ' }',
  26336. ' );',
  26337. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  26338. ' $r.addProperty(',
  26339. ' "ExtSizeB",',
  26340. ' 11,',
  26341. ' rtl.longint,',
  26342. ' "$getSize",',
  26343. ' "$setSize",',
  26344. ' {',
  26345. ' stored: "$extSizeStored"',
  26346. ' }',
  26347. ' );',
  26348. ' $r.addProperty(',
  26349. ' "ExtSizeC",',
  26350. ' 12,',
  26351. ' rtl.longint,',
  26352. ' "$extSize",',
  26353. ' "$extSize",',
  26354. ' {',
  26355. ' stored: "$getExtSizeStored"',
  26356. ' }',
  26357. ' );',
  26358. '});',
  26359. '']),
  26360. LinesToStr([ // $mod.$main
  26361. '']));
  26362. end;
  26363. procedure TTestModule.TestRTTI_Class_PropertyParams;
  26364. begin
  26365. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26366. StartProgram(false);
  26367. Add('{$modeswitch externalclass}');
  26368. Add('type');
  26369. Add(' integer = longint;');
  26370. Add(' TObject = class');
  26371. Add(' private');
  26372. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  26373. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  26374. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  26375. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  26376. Add(' published');
  26377. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  26378. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  26379. Add(' end;');
  26380. Add('begin');
  26381. ConvertProgram;
  26382. CheckSource('TestRTTI_Class_PropertyParams',
  26383. LinesToStr([ // statements
  26384. 'rtl.createClass($mod, "TObject", null, function () {',
  26385. ' this.$init = function () {',
  26386. ' };',
  26387. ' this.$final = function () {',
  26388. ' };',
  26389. ' var $r = this.$rtti;',
  26390. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  26391. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  26392. '});',
  26393. '']),
  26394. LinesToStr([ // $mod.$main
  26395. '']));
  26396. end;
  26397. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  26398. begin
  26399. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26400. AddModuleWithIntfImplSrc('unit1.pas',
  26401. 'type TColor = -5..5;',
  26402. '');
  26403. StartProgram(true);
  26404. Add([
  26405. 'uses unit1;',
  26406. 'type',
  26407. ' TColorAlias = TColor;',
  26408. ' TColorTypeAlias = type TColor;',
  26409. ' TObject = class',
  26410. ' private',
  26411. ' fColor: TColor;',
  26412. ' fAlias: TColorAlias;',
  26413. ' fTypeAlias: TColorTypeAlias;',
  26414. ' published',
  26415. ' property Color: TColor read fcolor;',
  26416. ' property Alias: TColorAlias read falias;',
  26417. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  26418. ' end;',
  26419. 'begin',
  26420. '']);
  26421. ConvertProgram;
  26422. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  26423. LinesToStr([ // statements
  26424. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  26425. 'rtl.createClass($mod, "TObject", null, function () {',
  26426. ' this.$init = function () {',
  26427. ' this.fColor = 0;',
  26428. ' this.fAlias = 0;',
  26429. ' this.fTypeAlias = 0;',
  26430. ' };',
  26431. ' this.$final = function () {',
  26432. ' };',
  26433. ' var $r = this.$rtti;',
  26434. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  26435. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  26436. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  26437. '});',
  26438. '']),
  26439. LinesToStr([ // $mod.$main
  26440. '']));
  26441. end;
  26442. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  26443. begin
  26444. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26445. StartProgram(false);
  26446. Add([
  26447. '{$modeswitch omitrtti}',
  26448. 'type',
  26449. ' TObject = class',
  26450. ' private',
  26451. ' FA: byte;',
  26452. ' published',
  26453. ' property A: byte read FA write FA;',
  26454. ' end;',
  26455. 'begin']);
  26456. ConvertProgram;
  26457. CheckSource('TestRTTI_Class_OmitRTTI',
  26458. LinesToStr([ // statements
  26459. 'rtl.createClass($mod, "TObject", null, function () {',
  26460. ' this.$init = function () {',
  26461. ' this.FA = 0;',
  26462. ' };',
  26463. ' this.$final = function () {',
  26464. ' };',
  26465. '});',
  26466. '']),
  26467. LinesToStr([ // $mod.$main
  26468. '']));
  26469. end;
  26470. procedure TTestModule.TestRTTI_IndexModifier;
  26471. begin
  26472. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26473. StartProgram(false);
  26474. Add([
  26475. 'type',
  26476. ' TEnum = (red, blue);',
  26477. ' TObject = class',
  26478. ' FB: boolean;',
  26479. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  26480. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  26481. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  26482. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  26483. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  26484. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  26485. ' published',
  26486. ' property B1: boolean index 1 read FB write SetIntBool;',
  26487. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  26488. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  26489. ' end;',
  26490. 'begin']);
  26491. ConvertProgram;
  26492. CheckSource('TestRTTI_IndexModifier',
  26493. LinesToStr([ // statements
  26494. 'this.TEnum = {',
  26495. ' "0": "red",',
  26496. ' red: 0,',
  26497. ' "1": "blue",',
  26498. ' blue: 1',
  26499. '};',
  26500. '$mod.$rtti.$Enum("TEnum", {',
  26501. ' minvalue: 0,',
  26502. ' maxvalue: 1,',
  26503. ' ordtype: 1,',
  26504. ' enumtype: this.TEnum',
  26505. '});',
  26506. 'rtl.createClass($mod, "TObject", null, function () {',
  26507. ' this.$init = function () {',
  26508. ' this.FB = false;',
  26509. ' };',
  26510. ' this.$final = function () {',
  26511. ' };',
  26512. ' var $r = this.$rtti;',
  26513. ' $r.addProperty(',
  26514. ' "B1",',
  26515. ' 18,',
  26516. ' rtl.boolean,',
  26517. ' "FB",',
  26518. ' "SetIntBool",',
  26519. ' {',
  26520. ' index: 1',
  26521. ' }',
  26522. ' );',
  26523. ' $r.addProperty(',
  26524. ' "B2",',
  26525. ' 17,',
  26526. ' rtl.boolean,',
  26527. ' "GetEnumBool",',
  26528. ' "FB",',
  26529. ' {',
  26530. ' index: $mod.TEnum.blue',
  26531. ' }',
  26532. ' );',
  26533. ' $r.addProperty(',
  26534. ' "I1",',
  26535. ' 19,',
  26536. ' rtl.boolean,',
  26537. ' "GetStrIntBool",',
  26538. ' "SetStrIntBool",',
  26539. ' {',
  26540. ' index: 2',
  26541. ' }',
  26542. ' );',
  26543. '});',
  26544. '']),
  26545. LinesToStr([ // $mod.$main
  26546. '']));
  26547. end;
  26548. procedure TTestModule.TestRTTI_StoredModifier;
  26549. begin
  26550. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26551. StartProgram(false);
  26552. Add([
  26553. 'const',
  26554. ' ConstB = true;',
  26555. 'type',
  26556. ' TObject = class',
  26557. ' private',
  26558. ' FB: boolean;',
  26559. ' function IsBStored: boolean; virtual; abstract;',
  26560. ' published',
  26561. ' property BoolA: boolean read FB stored true;',
  26562. ' property BoolB: boolean read FB stored false;',
  26563. ' property BoolC: boolean read FB stored FB;',
  26564. ' property BoolD: boolean read FB stored ConstB;',
  26565. ' property BoolE: boolean read FB stored IsBStored;',
  26566. ' end;',
  26567. 'begin']);
  26568. ConvertProgram;
  26569. CheckSource('TestRTTI_StoredModifier',
  26570. LinesToStr([ // statements
  26571. 'this.ConstB = true;',
  26572. 'rtl.createClass($mod, "TObject", null, function () {',
  26573. ' this.$init = function () {',
  26574. ' this.FB = false;',
  26575. ' };',
  26576. ' this.$final = function () {',
  26577. ' };',
  26578. ' var $r = this.$rtti;',
  26579. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  26580. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  26581. ' $r.addProperty(',
  26582. ' "BoolC",',
  26583. ' 8,',
  26584. ' rtl.boolean,',
  26585. ' "FB",',
  26586. ' "",',
  26587. ' {',
  26588. ' stored: "FB"',
  26589. ' }',
  26590. ' );',
  26591. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  26592. ' $r.addProperty(',
  26593. ' "BoolE",',
  26594. ' 12,',
  26595. ' rtl.boolean,',
  26596. ' "FB",',
  26597. ' "",',
  26598. ' {',
  26599. ' stored: "IsBStored"',
  26600. ' }',
  26601. ' );',
  26602. '});',
  26603. '']),
  26604. LinesToStr([ // $mod.$main
  26605. '']));
  26606. end;
  26607. procedure TTestModule.TestRTTI_DefaultValue;
  26608. begin
  26609. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26610. StartProgram(false);
  26611. Add([
  26612. 'type',
  26613. ' TEnum = (red, blue);',
  26614. 'const',
  26615. ' CB = true or false;',
  26616. ' CI = 1+2;',
  26617. 'type',
  26618. ' TObject = class',
  26619. ' FB: boolean;',
  26620. ' FI: longint;',
  26621. ' FE: TEnum;',
  26622. ' published',
  26623. ' property B1: boolean read FB default true;',
  26624. ' property B2: boolean read FB default CB;',
  26625. ' property B3: boolean read FB default test1.cb;',
  26626. ' property I1: longint read FI default 2;',
  26627. ' property I2: longint read FI default CI;',
  26628. ' property E1: TEnum read FE default red;',
  26629. ' property E2: TEnum read FE default TEnum.blue;',
  26630. ' end;',
  26631. 'begin']);
  26632. ConvertProgram;
  26633. CheckSource('TestRTTI_DefaultValue',
  26634. LinesToStr([ // statements
  26635. 'this.TEnum = {',
  26636. ' "0": "red",',
  26637. ' red: 0,',
  26638. ' "1": "blue",',
  26639. ' blue: 1',
  26640. '};',
  26641. '$mod.$rtti.$Enum("TEnum", {',
  26642. ' minvalue: 0,',
  26643. ' maxvalue: 1,',
  26644. ' ordtype: 1,',
  26645. ' enumtype: this.TEnum',
  26646. '});',
  26647. 'this.CB = true || false;',
  26648. 'this.CI = 1 + 2;',
  26649. 'rtl.createClass($mod, "TObject", null, function () {',
  26650. ' this.$init = function () {',
  26651. ' this.FB = false;',
  26652. ' this.FI = 0;',
  26653. ' this.FE = 0;',
  26654. ' };',
  26655. ' this.$final = function () {',
  26656. ' };',
  26657. ' var $r = this.$rtti;',
  26658. ' $r.addProperty(',
  26659. ' "B1",',
  26660. ' 0,',
  26661. ' rtl.boolean,',
  26662. ' "FB",',
  26663. ' "",',
  26664. ' {',
  26665. ' Default: true',
  26666. ' }',
  26667. ' );',
  26668. ' $r.addProperty(',
  26669. ' "B2",',
  26670. ' 0,',
  26671. ' rtl.boolean,',
  26672. ' "FB",',
  26673. ' "",',
  26674. ' {',
  26675. ' Default: true',
  26676. ' }',
  26677. ' );',
  26678. ' $r.addProperty(',
  26679. ' "B3",',
  26680. ' 0,',
  26681. ' rtl.boolean,',
  26682. ' "FB",',
  26683. ' "",',
  26684. ' {',
  26685. ' Default: true',
  26686. ' }',
  26687. ' );',
  26688. ' $r.addProperty(',
  26689. ' "I1",',
  26690. ' 0,',
  26691. ' rtl.longint,',
  26692. ' "FI",',
  26693. ' "",',
  26694. ' {',
  26695. ' Default: 2',
  26696. ' }',
  26697. ' );',
  26698. ' $r.addProperty(',
  26699. ' "I2",',
  26700. ' 0,',
  26701. ' rtl.longint,',
  26702. ' "FI",',
  26703. ' "",',
  26704. ' {',
  26705. ' Default: 3',
  26706. ' }',
  26707. ' );',
  26708. ' $r.addProperty(',
  26709. ' "E1",',
  26710. ' 0,',
  26711. ' $mod.$rtti["TEnum"],',
  26712. ' "FE",',
  26713. ' "",',
  26714. ' {',
  26715. ' Default: $mod.TEnum.red',
  26716. ' }',
  26717. ' );',
  26718. ' $r.addProperty(',
  26719. ' "E2",',
  26720. ' 0,',
  26721. ' $mod.$rtti["TEnum"],',
  26722. ' "FE",',
  26723. ' "",',
  26724. ' {',
  26725. ' Default: $mod.TEnum.blue',
  26726. ' }',
  26727. ' );',
  26728. '});',
  26729. '']),
  26730. LinesToStr([ // $mod.$main
  26731. '']));
  26732. end;
  26733. procedure TTestModule.TestRTTI_DefaultValueSet;
  26734. begin
  26735. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26736. StartProgram(false);
  26737. Add([
  26738. 'type',
  26739. ' TEnum = (red, blue);',
  26740. ' TSet = set of TEnum;',
  26741. 'const',
  26742. ' CSet = [red,blue];',
  26743. 'type',
  26744. ' TObject = class',
  26745. ' FSet: TSet;',
  26746. ' published',
  26747. ' property Set1: TSet read FSet default [];',
  26748. ' property Set2: TSet read FSet default [red];',
  26749. ' property Set3: TSet read FSet default [red,blue];',
  26750. ' property Set4: TSet read FSet default CSet;',
  26751. ' end;',
  26752. 'begin']);
  26753. ConvertProgram;
  26754. CheckSource('TestRTTI_DefaultValueSet',
  26755. LinesToStr([ // statements
  26756. 'this.TEnum = {',
  26757. ' "0": "red",',
  26758. ' red: 0,',
  26759. ' "1": "blue",',
  26760. ' blue: 1',
  26761. '};',
  26762. '$mod.$rtti.$Enum("TEnum", {',
  26763. ' minvalue: 0,',
  26764. ' maxvalue: 1,',
  26765. ' ordtype: 1,',
  26766. ' enumtype: this.TEnum',
  26767. '});',
  26768. '$mod.$rtti.$Set("TSet", {',
  26769. ' comptype: $mod.$rtti["TEnum"]',
  26770. '});',
  26771. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  26772. 'rtl.createClass($mod, "TObject", null, function () {',
  26773. ' this.$init = function () {',
  26774. ' this.FSet = {};',
  26775. ' };',
  26776. ' this.$final = function () {',
  26777. ' this.FSet = undefined;',
  26778. ' };',
  26779. ' var $r = this.$rtti;',
  26780. ' $r.addProperty(',
  26781. ' "Set1",',
  26782. ' 0,',
  26783. ' $mod.$rtti["TSet"],',
  26784. ' "FSet",',
  26785. ' "",',
  26786. ' {',
  26787. ' Default: {}',
  26788. ' }',
  26789. ' );',
  26790. ' $r.addProperty(',
  26791. ' "Set2",',
  26792. ' 0,',
  26793. ' $mod.$rtti["TSet"],',
  26794. ' "FSet",',
  26795. ' "",',
  26796. ' {',
  26797. ' Default: rtl.createSet($mod.TEnum.red)',
  26798. ' }',
  26799. ' );',
  26800. ' $r.addProperty(',
  26801. ' "Set3",',
  26802. ' 0,',
  26803. ' $mod.$rtti["TSet"],',
  26804. ' "FSet",',
  26805. ' "",',
  26806. ' {',
  26807. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  26808. ' }',
  26809. ' );',
  26810. ' $r.addProperty(',
  26811. ' "Set4",',
  26812. ' 0,',
  26813. ' $mod.$rtti["TSet"],',
  26814. ' "FSet",',
  26815. ' "",',
  26816. ' {',
  26817. ' Default: $mod.CSet',
  26818. ' }',
  26819. ' );',
  26820. '});',
  26821. '']),
  26822. LinesToStr([ // $mod.$main
  26823. '']));
  26824. end;
  26825. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  26826. begin
  26827. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26828. StartProgram(false);
  26829. Add([
  26830. 'type',
  26831. ' TRg = -1..1;',
  26832. 'const',
  26833. ' l = low(TRg);',
  26834. ' h = high(TRg);',
  26835. 'type',
  26836. ' TObject = class',
  26837. ' FV: TRg;',
  26838. ' published',
  26839. ' property V1: TRg read FV default -1;',
  26840. ' end;',
  26841. 'begin']);
  26842. ConvertProgram;
  26843. CheckSource('TestRTTI_DefaultValueRangeType',
  26844. LinesToStr([ // statements
  26845. '$mod.$rtti.$Int("TRg", {',
  26846. ' minvalue: -1,',
  26847. ' maxvalue: 1,',
  26848. ' ordtype: 0',
  26849. '});',
  26850. 'this.l = -1;',
  26851. 'this.h = 1;',
  26852. 'rtl.createClass($mod, "TObject", null, function () {',
  26853. ' this.$init = function () {',
  26854. ' this.FV = 0;',
  26855. ' };',
  26856. ' this.$final = function () {',
  26857. ' };',
  26858. ' var $r = this.$rtti;',
  26859. ' $r.addProperty(',
  26860. ' "V1",',
  26861. ' 0,',
  26862. ' $mod.$rtti["TRg"],',
  26863. ' "FV",',
  26864. ' "",',
  26865. ' {',
  26866. ' Default: -1',
  26867. ' }',
  26868. ' );',
  26869. '});',
  26870. '']),
  26871. LinesToStr([ // $mod.$main
  26872. '']));
  26873. end;
  26874. procedure TTestModule.TestRTTI_DefaultValueInherit;
  26875. begin
  26876. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26877. StartProgram(false);
  26878. Add([
  26879. 'type',
  26880. ' TObject = class',
  26881. ' FA, FB: byte;',
  26882. ' property A: byte read FA default 1;',
  26883. ' property B: byte read FB default 2;',
  26884. ' end;',
  26885. ' TBird = class',
  26886. ' published',
  26887. ' property A;',
  26888. ' property B nodefault;',
  26889. ' end;',
  26890. 'begin']);
  26891. ConvertProgram;
  26892. CheckSource('TestRTTI_DefaultValueInherit',
  26893. LinesToStr([ // statements
  26894. 'rtl.createClass($mod, "TObject", null, function () {',
  26895. ' this.$init = function () {',
  26896. ' this.FA = 0;',
  26897. ' this.FB = 0;',
  26898. ' };',
  26899. ' this.$final = function () {',
  26900. ' };',
  26901. '});',
  26902. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  26903. ' var $r = this.$rtti;',
  26904. ' $r.addProperty(',
  26905. ' "A",',
  26906. ' 0,',
  26907. ' rtl.byte,',
  26908. ' "FA",',
  26909. ' "",',
  26910. ' {',
  26911. ' Default: 1',
  26912. ' }',
  26913. ' );',
  26914. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  26915. '});',
  26916. '']),
  26917. LinesToStr([ // $mod.$main
  26918. '']));
  26919. end;
  26920. procedure TTestModule.TestRTTI_OverrideMethod;
  26921. begin
  26922. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26923. StartProgram(false);
  26924. Add('type');
  26925. Add(' TObject = class');
  26926. Add(' published');
  26927. Add(' procedure DoIt; virtual; abstract;');
  26928. Add(' end;');
  26929. Add(' TSky = class');
  26930. Add(' published');
  26931. Add(' procedure DoIt; override;');
  26932. Add(' end;');
  26933. Add('procedure TSky.DoIt; begin end;');
  26934. Add('begin');
  26935. ConvertProgram;
  26936. CheckSource('TestRTTI_OverrideMethod',
  26937. LinesToStr([ // statements
  26938. 'rtl.createClass($mod, "TObject", null, function () {',
  26939. ' this.$init = function () {',
  26940. ' };',
  26941. ' this.$final = function () {',
  26942. ' };',
  26943. ' var $r = this.$rtti;',
  26944. ' $r.addMethod("DoIt", 0, null);',
  26945. '});',
  26946. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  26947. ' this.DoIt = function () {',
  26948. ' };',
  26949. '});',
  26950. '']),
  26951. LinesToStr([ // $mod.$main
  26952. '']));
  26953. end;
  26954. procedure TTestModule.TestRTTI_OverloadProperty;
  26955. begin
  26956. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26957. StartProgram(false);
  26958. Add('type');
  26959. Add(' TObject = class');
  26960. Add(' protected');
  26961. Add(' FFlag: longint;');
  26962. Add(' published');
  26963. Add(' property Flag: longint read fflag;');
  26964. Add(' end;');
  26965. Add(' TSky = class');
  26966. Add(' published');
  26967. Add(' property FLAG: longint write fflag;');
  26968. Add(' end;');
  26969. Add('begin');
  26970. ConvertProgram;
  26971. CheckSource('TestRTTI_OverrideMethod',
  26972. LinesToStr([ // statements
  26973. 'rtl.createClass($mod, "TObject", null, function () {',
  26974. ' this.$init = function () {',
  26975. ' this.FFlag = 0;',
  26976. ' };',
  26977. ' this.$final = function () {',
  26978. ' };',
  26979. ' var $r = this.$rtti;',
  26980. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  26981. '});',
  26982. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  26983. ' var $r = this.$rtti;',
  26984. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  26985. '});',
  26986. '']),
  26987. LinesToStr([ // $mod.$main
  26988. '']));
  26989. end;
  26990. procedure TTestModule.TestRTTI_ClassForward;
  26991. begin
  26992. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26993. StartProgram(false);
  26994. Add('type');
  26995. Add(' TObject = class end;');
  26996. Add(' tbridge = class;');
  26997. Add(' TProc = function: tbridge;');
  26998. Add(' TOger = class');
  26999. Add(' published');
  27000. Add(' FBridge: tbridge;');
  27001. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  27002. Add(' property Bridge: tbridge read fbridge write setbridge;');
  27003. Add(' end;');
  27004. Add(' TBridge = class');
  27005. Add(' FOger: toger;');
  27006. Add(' end;');
  27007. Add('var p: Pointer;');
  27008. Add(' b: tbridge;');
  27009. Add('begin');
  27010. Add(' p:=typeinfo(tbridge);');
  27011. Add(' p:=typeinfo(b);');
  27012. ConvertProgram;
  27013. CheckSource('TestRTTI_ClassForward',
  27014. LinesToStr([ // statements
  27015. 'rtl.createClass($mod, "TObject", null, function () {',
  27016. ' this.$init = function () {',
  27017. ' };',
  27018. ' this.$final = function () {',
  27019. ' };',
  27020. '});',
  27021. '$mod.$rtti.$Class("TBridge");',
  27022. '$mod.$rtti.$ProcVar("TProc", {',
  27023. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  27024. '});',
  27025. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  27026. ' this.$init = function () {',
  27027. ' $mod.TObject.$init.call(this);',
  27028. ' this.FBridge = null;',
  27029. ' };',
  27030. ' this.$final = function () {',
  27031. ' this.FBridge = undefined;',
  27032. ' $mod.TObject.$final.call(this);',
  27033. ' };',
  27034. ' var $r = this.$rtti;',
  27035. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  27036. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  27037. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  27038. '});',
  27039. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  27040. ' this.$init = function () {',
  27041. ' $mod.TObject.$init.call(this);',
  27042. ' this.FOger = null;',
  27043. ' };',
  27044. ' this.$final = function () {',
  27045. ' this.FOger = undefined;',
  27046. ' $mod.TObject.$final.call(this);',
  27047. ' };',
  27048. '});',
  27049. 'this.p = null;',
  27050. 'this.b = null;',
  27051. '']),
  27052. LinesToStr([ // $mod.$main
  27053. '$mod.p = $mod.$rtti["TBridge"];',
  27054. '$mod.p = $mod.b.$rtti;',
  27055. '']));
  27056. end;
  27057. procedure TTestModule.TestRTTI_ClassOf;
  27058. begin
  27059. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27060. StartProgram(false);
  27061. Add('type');
  27062. Add(' TClass = class of tobject;');
  27063. Add(' TProcA = function: TClass;');
  27064. Add(' TObject = class');
  27065. Add(' published');
  27066. Add(' C: tclass;');
  27067. Add(' end;');
  27068. Add(' tfox = class;');
  27069. Add(' TBird = class end;');
  27070. Add(' TBirds = class of tbird;');
  27071. Add(' TFox = class end;');
  27072. Add(' TFoxes = class of tfox;');
  27073. Add(' TCows = class of TCow;');
  27074. Add(' TCow = class;');
  27075. Add(' TCow = class end;');
  27076. Add('begin');
  27077. ConvertProgram;
  27078. CheckSource('TestRTTI_ClassOf',
  27079. LinesToStr([ // statements
  27080. '$mod.$rtti.$Class("TObject");',
  27081. '$mod.$rtti.$ClassRef("TClass", {',
  27082. ' instancetype: $mod.$rtti["TObject"]',
  27083. '});',
  27084. '$mod.$rtti.$ProcVar("TProcA", {',
  27085. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  27086. '});',
  27087. 'rtl.createClass($mod, "TObject", null, function () {',
  27088. ' this.$init = function () {',
  27089. ' this.C = null;',
  27090. ' };',
  27091. ' this.$final = function () {',
  27092. ' this.C = undefined;',
  27093. ' };',
  27094. ' var $r = this.$rtti;',
  27095. ' $r.addField("C", $mod.$rtti["TClass"]);',
  27096. '});',
  27097. '$mod.$rtti.$Class("TFox");',
  27098. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  27099. '});',
  27100. '$mod.$rtti.$ClassRef("TBirds", {',
  27101. ' instancetype: $mod.$rtti["TBird"]',
  27102. '});',
  27103. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  27104. '});',
  27105. '$mod.$rtti.$ClassRef("TFoxes", {',
  27106. ' instancetype: $mod.$rtti["TFox"]',
  27107. '});',
  27108. '$mod.$rtti.$Class("TCow");',
  27109. '$mod.$rtti.$ClassRef("TCows", {',
  27110. ' instancetype: $mod.$rtti["TCow"]',
  27111. '});',
  27112. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  27113. '});',
  27114. '']),
  27115. LinesToStr([ // $mod.$main
  27116. '']));
  27117. end;
  27118. procedure TTestModule.TestRTTI_Record;
  27119. begin
  27120. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27121. StartProgram(false);
  27122. Add('type');
  27123. Add(' integer = longint;');
  27124. Add(' TPoint = record');
  27125. Add(' x,y: integer;');
  27126. Add(' end;');
  27127. Add('var p: pointer;');
  27128. Add(' r: tpoint;');
  27129. Add('begin');
  27130. Add(' p:=typeinfo(tpoint);');
  27131. Add(' p:=typeinfo(r);');
  27132. Add(' p:=typeinfo(r.x);');
  27133. ConvertProgram;
  27134. CheckSource('TestRTTI_Record',
  27135. LinesToStr([ // statements
  27136. 'rtl.recNewT($mod, "TPoint", function () {',
  27137. ' this.x = 0;',
  27138. ' this.y = 0;',
  27139. ' this.$eq = function (b) {',
  27140. ' return (this.x === b.x) && (this.y === b.y);',
  27141. ' };',
  27142. ' this.$assign = function (s) {',
  27143. ' this.x = s.x;',
  27144. ' this.y = s.y;',
  27145. ' return this;',
  27146. ' };',
  27147. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  27148. ' $r.addField("x", rtl.longint);',
  27149. ' $r.addField("y", rtl.longint);',
  27150. '});',
  27151. 'this.p = null;',
  27152. 'this.r = $mod.TPoint.$new();',
  27153. '']),
  27154. LinesToStr([ // $mod.$main
  27155. '$mod.p = $mod.$rtti["TPoint"];',
  27156. '$mod.p = $mod.$rtti["TPoint"];',
  27157. '$mod.p = rtl.longint;',
  27158. '']));
  27159. end;
  27160. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  27161. begin
  27162. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27163. StartProgram(false);
  27164. Add('type');
  27165. Add(' TFloatRec = record');
  27166. Add(' d: array of char;');
  27167. // Add(' i: array of array of longint;');
  27168. Add(' end;');
  27169. Add('var p: pointer;');
  27170. Add(' r: tfloatrec;');
  27171. Add('begin');
  27172. Add(' p:=typeinfo(tfloatrec);');
  27173. Add(' p:=typeinfo(r);');
  27174. Add(' p:=typeinfo(r.d);');
  27175. ConvertProgram;
  27176. CheckSource('TestRTTI_Record',
  27177. LinesToStr([ // statements
  27178. 'rtl.recNewT($mod, "TFloatRec", function () {',
  27179. ' this.d = [];',
  27180. ' this.$eq = function (b) {',
  27181. ' return this.d === b.d;',
  27182. ' };',
  27183. ' this.$assign = function (s) {',
  27184. ' this.d = s.d;',
  27185. ' return this;',
  27186. ' };',
  27187. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  27188. ' eltype: rtl.char',
  27189. ' });',
  27190. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  27191. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  27192. '});',
  27193. 'this.p = null;',
  27194. 'this.r = $mod.TFloatRec.$new();',
  27195. '']),
  27196. LinesToStr([ // $mod.$main
  27197. '$mod.p = $mod.$rtti["TFloatRec"];',
  27198. '$mod.p = $mod.$rtti["TFloatRec"];',
  27199. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  27200. '']));
  27201. end;
  27202. procedure TTestModule.TestRTTI_LocalTypes;
  27203. begin
  27204. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27205. StartProgram(false);
  27206. Add([
  27207. 'procedure DoIt;',
  27208. 'type',
  27209. ' integer = longint;',
  27210. ' TPoint = record',
  27211. ' x,y: integer;',
  27212. ' end;',
  27213. 'var p: TPoint;',
  27214. 'begin',
  27215. 'end;',
  27216. 'begin']);
  27217. ConvertProgram;
  27218. CheckSource('TestRTTI_LocalTypes',
  27219. LinesToStr([ // statements
  27220. 'var TPoint = rtl.recNewT(null, "", function () {',
  27221. ' this.x = 0;',
  27222. ' this.y = 0;',
  27223. ' this.$eq = function (b) {',
  27224. ' return (this.x === b.x) && (this.y === b.y);',
  27225. ' };',
  27226. ' this.$assign = function (s) {',
  27227. ' this.x = s.x;',
  27228. ' this.y = s.y;',
  27229. ' return this;',
  27230. ' };',
  27231. '});',
  27232. 'this.DoIt = function () {',
  27233. ' var p = TPoint.$new();',
  27234. '};',
  27235. '']),
  27236. LinesToStr([ // $mod.$main
  27237. '']));
  27238. end;
  27239. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  27240. begin
  27241. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27242. StartProgram(false);
  27243. Add([
  27244. 'type',
  27245. ' TCaption = string;',
  27246. ' TYesNo = boolean;',
  27247. ' TLetter = char;',
  27248. ' TFloat = double;',
  27249. ' TPtr = pointer;',
  27250. ' TShortInt = shortint;',
  27251. ' TByte = byte;',
  27252. ' TSmallInt = smallint;',
  27253. ' TWord = word;',
  27254. ' TInt32 = longint;',
  27255. ' TDWord = longword;',
  27256. ' TValue = jsvalue;',
  27257. 'var p: TPtr;',
  27258. 'begin',
  27259. ' p:=typeinfo(string);',
  27260. ' p:=typeinfo(tcaption);',
  27261. ' p:=typeinfo(boolean);',
  27262. ' p:=typeinfo(tyesno);',
  27263. ' p:=typeinfo(char);',
  27264. ' p:=typeinfo(tletter);',
  27265. ' p:=typeinfo(double);',
  27266. ' p:=typeinfo(tfloat);',
  27267. ' p:=typeinfo(pointer);',
  27268. ' p:=typeinfo(tptr);',
  27269. ' p:=typeinfo(shortint);',
  27270. ' p:=typeinfo(tshortint);',
  27271. ' p:=typeinfo(byte);',
  27272. ' p:=typeinfo(tbyte);',
  27273. ' p:=typeinfo(smallint);',
  27274. ' p:=typeinfo(tsmallint);',
  27275. ' p:=typeinfo(word);',
  27276. ' p:=typeinfo(tword);',
  27277. ' p:=typeinfo(longword);',
  27278. ' p:=typeinfo(tdword);',
  27279. ' p:=typeinfo(jsvalue);',
  27280. ' p:=typeinfo(tvalue);',
  27281. '']);
  27282. ConvertProgram;
  27283. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  27284. LinesToStr([ // statements
  27285. 'this.p = null;',
  27286. '']),
  27287. LinesToStr([ // $mod.$main
  27288. '$mod.p = rtl.string;',
  27289. '$mod.p = rtl.string;',
  27290. '$mod.p = rtl.boolean;',
  27291. '$mod.p = rtl.boolean;',
  27292. '$mod.p = rtl.char;',
  27293. '$mod.p = rtl.char;',
  27294. '$mod.p = rtl.double;',
  27295. '$mod.p = rtl.double;',
  27296. '$mod.p = rtl.pointer;',
  27297. '$mod.p = rtl.pointer;',
  27298. '$mod.p = rtl.shortint;',
  27299. '$mod.p = rtl.shortint;',
  27300. '$mod.p = rtl.byte;',
  27301. '$mod.p = rtl.byte;',
  27302. '$mod.p = rtl.smallint;',
  27303. '$mod.p = rtl.smallint;',
  27304. '$mod.p = rtl.word;',
  27305. '$mod.p = rtl.word;',
  27306. '$mod.p = rtl.longword;',
  27307. '$mod.p = rtl.longword;',
  27308. '$mod.p = rtl.jsvalue;',
  27309. '$mod.p = rtl.jsvalue;',
  27310. '']));
  27311. end;
  27312. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  27313. begin
  27314. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27315. StartProgram(false);
  27316. Add([
  27317. 'type',
  27318. ' TCaption = type string;',
  27319. ' TYesNo = type boolean;',
  27320. ' TLetter = type char;',
  27321. ' TFloat = type double;',
  27322. ' TPtr = type pointer;',
  27323. ' TShortInt = type shortint;',
  27324. ' TByte = type byte;',
  27325. ' TSmallInt = type smallint;',
  27326. ' TWord = type word;',
  27327. ' TInt32 = type longint;',
  27328. ' TDWord = type longword;',
  27329. ' TValue = type jsvalue;',
  27330. ' TAliasValue = type TValue;',
  27331. 'var',
  27332. ' p: TPtr;',
  27333. ' a: TAliasValue;',
  27334. 'begin',
  27335. ' p:=typeinfo(tcaption);',
  27336. ' p:=typeinfo(tyesno);',
  27337. ' p:=typeinfo(tletter);',
  27338. ' p:=typeinfo(tfloat);',
  27339. ' p:=typeinfo(tptr);',
  27340. ' p:=typeinfo(tshortint);',
  27341. ' p:=typeinfo(tbyte);',
  27342. ' p:=typeinfo(tsmallint);',
  27343. ' p:=typeinfo(tword);',
  27344. ' p:=typeinfo(tdword);',
  27345. ' p:=typeinfo(tvalue);',
  27346. ' p:=typeinfo(taliasvalue);',
  27347. ' p:=typeinfo(a);',
  27348. '']);
  27349. ConvertProgram;
  27350. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  27351. LinesToStr([ // statements
  27352. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  27353. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  27354. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  27355. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  27356. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  27357. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  27358. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  27359. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  27360. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  27361. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  27362. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  27363. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  27364. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  27365. 'this.p = null;',
  27366. 'this.a = undefined;',
  27367. '']),
  27368. LinesToStr([ // $mod.$main
  27369. '$mod.p = $mod.$rtti["TCaption"];',
  27370. '$mod.p = $mod.$rtti["TYesNo"];',
  27371. '$mod.p = $mod.$rtti["TLetter"];',
  27372. '$mod.p = $mod.$rtti["TFloat"];',
  27373. '$mod.p = $mod.$rtti["TPtr"];',
  27374. '$mod.p = $mod.$rtti["TShortInt"];',
  27375. '$mod.p = $mod.$rtti["TByte"];',
  27376. '$mod.p = $mod.$rtti["TSmallInt"];',
  27377. '$mod.p = $mod.$rtti["TWord"];',
  27378. '$mod.p = $mod.$rtti["TDWord"];',
  27379. '$mod.p = $mod.$rtti["TValue"];',
  27380. '$mod.p = $mod.$rtti["TAliasValue"];',
  27381. '$mod.p = $mod.$rtti["TAliasValue"];',
  27382. '']));
  27383. end;
  27384. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  27385. begin
  27386. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27387. StartProgram(false);
  27388. Add('procedure DoIt;');
  27389. Add('type');
  27390. Add(' integer = longint;');
  27391. Add(' TPoint = record');
  27392. Add(' x,y: integer;');
  27393. Add(' end;');
  27394. Add('var p: pointer;');
  27395. Add('begin');
  27396. Add(' p:=typeinfo(tpoint);');
  27397. Add('end;');
  27398. Add('begin');
  27399. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  27400. ConvertProgram;
  27401. end;
  27402. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  27403. begin
  27404. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27405. StartProgram(false);
  27406. Add([
  27407. '{$modeswitch externalclass}',
  27408. 'type',
  27409. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27410. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  27411. ' TFlag = (up,down);',
  27412. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  27413. ' TFlags = set of TFlag;',
  27414. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  27415. 'var',
  27416. ' ti: TTypeInfo;',
  27417. ' tiInt: TTypeInfoInteger;',
  27418. ' tiEnum: TTypeInfoEnum;',
  27419. ' tiSet: TTypeInfoSet;',
  27420. 'begin',
  27421. ' ti:=typeinfo(string);',
  27422. ' ti:=typeinfo(boolean);',
  27423. ' ti:=typeinfo(char);',
  27424. ' ti:=typeinfo(double);',
  27425. ' tiInt:=typeinfo(shortint);',
  27426. ' tiInt:=typeinfo(byte);',
  27427. ' tiInt:=typeinfo(smallint);',
  27428. ' tiInt:=typeinfo(word);',
  27429. ' tiInt:=typeinfo(longint);',
  27430. ' tiInt:=typeinfo(longword);',
  27431. ' ti:=typeinfo(jsvalue);',
  27432. ' tiEnum:=typeinfo(tflag);',
  27433. ' tiSet:=typeinfo(tflags);']);
  27434. ConvertProgram;
  27435. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  27436. LinesToStr([ // statements
  27437. 'this.TFlag = {',
  27438. ' "0": "up",',
  27439. ' up: 0,',
  27440. ' "1": "down",',
  27441. ' down: 1',
  27442. '};',
  27443. '$mod.$rtti.$Enum("TFlag", {',
  27444. ' minvalue: 0,',
  27445. ' maxvalue: 1,',
  27446. ' ordtype: 1,',
  27447. ' enumtype: this.TFlag',
  27448. '});',
  27449. '$mod.$rtti.$Set("TFlags", {',
  27450. ' comptype: $mod.$rtti["TFlag"]',
  27451. '});',
  27452. 'this.ti = null;',
  27453. 'this.tiInt = null;',
  27454. 'this.tiEnum = null;',
  27455. 'this.tiSet = null;',
  27456. '']),
  27457. LinesToStr([ // $mod.$main
  27458. '$mod.ti = rtl.string;',
  27459. '$mod.ti = rtl.boolean;',
  27460. '$mod.ti = rtl.char;',
  27461. '$mod.ti = rtl.double;',
  27462. '$mod.tiInt = rtl.shortint;',
  27463. '$mod.tiInt = rtl.byte;',
  27464. '$mod.tiInt = rtl.smallint;',
  27465. '$mod.tiInt = rtl.word;',
  27466. '$mod.tiInt = rtl.longint;',
  27467. '$mod.tiInt = rtl.longword;',
  27468. '$mod.ti = rtl.jsvalue;',
  27469. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  27470. '$mod.tiSet = $mod.$rtti["TFlags"];',
  27471. '']));
  27472. end;
  27473. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  27474. begin
  27475. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27476. StartProgram(false);
  27477. Add('{$modeswitch externalclass}');
  27478. Add('type');
  27479. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  27480. Add(' TStaticArr = array[boolean] of string;');
  27481. Add(' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;');
  27482. Add(' TDynArr = array of string;');
  27483. Add(' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;');
  27484. Add(' TProc = procedure;');
  27485. Add(' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;');
  27486. Add(' TMethod = procedure of object;');
  27487. Add(' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;');
  27488. Add('var');
  27489. Add(' StaticArray: TStaticArr;');
  27490. Add(' tiStaticArray: TTypeInfoStaticArray;');
  27491. Add(' DynArray: TDynArr;');
  27492. Add(' tiDynArray: TTypeInfoDynArray;');
  27493. Add(' ProcVar: TProc;');
  27494. Add(' tiProcVar: TTypeInfoProcVar;');
  27495. Add(' MethodVar: TMethod;');
  27496. Add(' tiMethodVar: TTypeInfoMethodVar;');
  27497. Add('begin');
  27498. Add(' tiStaticArray:=typeinfo(StaticArray);');
  27499. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  27500. Add(' tiDynArray:=typeinfo(DynArray);');
  27501. Add(' tiDynArray:=typeinfo(TDynArr);');
  27502. Add(' tiProcVar:=typeinfo(ProcVar);');
  27503. Add(' tiProcVar:=typeinfo(TProc);');
  27504. Add(' tiMethodVar:=typeinfo(MethodVar);');
  27505. Add(' tiMethodVar:=typeinfo(TMethod);');
  27506. ConvertProgram;
  27507. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  27508. LinesToStr([ // statements
  27509. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  27510. ' dims: [2],',
  27511. ' eltype: rtl.string',
  27512. '});',
  27513. '$mod.$rtti.$DynArray("TDynArr", {',
  27514. ' eltype: rtl.string',
  27515. '});',
  27516. '$mod.$rtti.$ProcVar("TProc", {',
  27517. ' procsig: rtl.newTIProcSig(null)',
  27518. '});',
  27519. '$mod.$rtti.$MethodVar("TMethod", {',
  27520. ' procsig: rtl.newTIProcSig(null),',
  27521. ' methodkind: 0',
  27522. '});',
  27523. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  27524. 'this.tiStaticArray = null;',
  27525. 'this.DynArray = [];',
  27526. 'this.tiDynArray = null;',
  27527. 'this.ProcVar = null;',
  27528. 'this.tiProcVar = null;',
  27529. 'this.MethodVar = null;',
  27530. 'this.tiMethodVar = null;',
  27531. '']),
  27532. LinesToStr([ // $mod.$main
  27533. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  27534. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  27535. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  27536. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  27537. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  27538. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  27539. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  27540. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  27541. '']));
  27542. end;
  27543. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  27544. begin
  27545. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27546. StartProgram(false);
  27547. Add('{$modeswitch externalclass}');
  27548. Add('type');
  27549. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  27550. Add(' TRec = record end;');
  27551. Add(' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;');
  27552. // ToDo: ^PRec
  27553. Add(' TObject = class end;');
  27554. Add(' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;');
  27555. Add(' TClass = class of tobject;');
  27556. Add(' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;');
  27557. Add(' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;');
  27558. Add('var');
  27559. Add(' Rec: trec;');
  27560. Add(' tiRecord: ttypeinforecord;');
  27561. Add(' Obj: tobject;');
  27562. Add(' tiClass: ttypeinfoclass;');
  27563. Add(' aClass: tclass;');
  27564. Add(' tiClassRef: ttypeinfoclassref;');
  27565. // ToDo: ^PRec
  27566. Add(' tiPointer: ttypeinfopointer;');
  27567. Add('begin');
  27568. Add(' tirecord:=typeinfo(trec);');
  27569. Add(' tirecord:=typeinfo(trec);');
  27570. Add(' ticlass:=typeinfo(obj);');
  27571. Add(' ticlass:=typeinfo(tobject);');
  27572. Add(' ticlass:=typeinfo(aclass);');
  27573. Add(' ticlassref:=typeinfo(tclass);');
  27574. ConvertProgram;
  27575. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  27576. LinesToStr([ // statements
  27577. 'rtl.recNewT($mod, "TRec", function () {',
  27578. ' this.$eq = function (b) {',
  27579. ' return true;',
  27580. ' };',
  27581. ' this.$assign = function (s) {',
  27582. ' return this;',
  27583. ' };',
  27584. ' $mod.$rtti.$Record("TRec", {});',
  27585. '});',
  27586. 'rtl.createClass($mod, "TObject", null, function () {',
  27587. ' this.$init = function () {',
  27588. ' };',
  27589. ' this.$final = function () {',
  27590. ' };',
  27591. '});',
  27592. '$mod.$rtti.$ClassRef("TClass", {',
  27593. ' instancetype: $mod.$rtti["TObject"]',
  27594. '});',
  27595. 'this.Rec = $mod.TRec.$new();',
  27596. 'this.tiRecord = null;',
  27597. 'this.Obj = null;',
  27598. 'this.tiClass = null;',
  27599. 'this.aClass = null;',
  27600. 'this.tiClassRef = null;',
  27601. 'this.tiPointer = null;',
  27602. '']),
  27603. LinesToStr([ // $mod.$main
  27604. '$mod.tiRecord = $mod.$rtti["TRec"];',
  27605. '$mod.tiRecord = $mod.$rtti["TRec"];',
  27606. '$mod.tiClass = $mod.Obj.$rtti;',
  27607. '$mod.tiClass = $mod.$rtti["TObject"];',
  27608. '$mod.tiClass = $mod.aClass.$rtti;',
  27609. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  27610. '']));
  27611. end;
  27612. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  27613. begin
  27614. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27615. StartProgram(false);
  27616. Add([
  27617. '{$modeswitch externalclass}',
  27618. 'type',
  27619. ' TClass = class of tobject;',
  27620. ' TObject = class',
  27621. ' function MyClass: TClass;',
  27622. ' class function ClassType: TClass;',
  27623. ' end;',
  27624. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27625. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  27626. 'function TObject.MyClass: TClass;',
  27627. 'var t: TTypeInfoClass;',
  27628. 'begin',
  27629. ' t:=TypeInfo(Self);',
  27630. ' t:=TypeInfo(Result);',
  27631. ' t:=TypeInfo(TObject);',
  27632. 'end;',
  27633. 'class function TObject.ClassType: TClass;',
  27634. 'var t: TTypeInfoClass;',
  27635. 'begin',
  27636. ' t:=TypeInfo(Self);',
  27637. ' t:=TypeInfo(Result);',
  27638. 'end;',
  27639. 'var',
  27640. ' Obj: TObject;',
  27641. ' t: TTypeInfoClass;',
  27642. 'begin',
  27643. ' t:=TypeInfo(TObject.ClassType);',
  27644. ' t:=TypeInfo(Obj.ClassType);',
  27645. ' t:=TypeInfo(Obj.MyClass);',
  27646. '']);
  27647. ConvertProgram;
  27648. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  27649. LinesToStr([ // statements
  27650. '$mod.$rtti.$Class("TObject");',
  27651. '$mod.$rtti.$ClassRef("TClass", {',
  27652. ' instancetype: $mod.$rtti["TObject"]',
  27653. '});',
  27654. 'rtl.createClass($mod, "TObject", null, function () {',
  27655. ' this.$init = function () {',
  27656. ' };',
  27657. ' this.$final = function () {',
  27658. ' };',
  27659. ' this.MyClass = function () {',
  27660. ' var Result = null;',
  27661. ' var t = null;',
  27662. ' t = this.$rtti;',
  27663. ' t = Result.$rtti;',
  27664. ' t = $mod.$rtti["TObject"];',
  27665. ' return Result;',
  27666. ' };',
  27667. ' this.ClassType = function () {',
  27668. ' var Result = null;',
  27669. ' var t = null;',
  27670. ' t = this.$rtti;',
  27671. ' t = Result.$rtti;',
  27672. ' return Result;',
  27673. ' };',
  27674. '});',
  27675. 'this.Obj = null;',
  27676. 'this.t = null;',
  27677. '']),
  27678. LinesToStr([ // $mod.$main
  27679. '$mod.t = $mod.TObject.ClassType().$rtti;',
  27680. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  27681. '$mod.t = $mod.Obj.MyClass().$rtti;',
  27682. '']));
  27683. end;
  27684. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  27685. begin
  27686. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27687. AddModuleWithIntfImplSrc('typinfo.pas',
  27688. LinesToStr([
  27689. '{$modeswitch externalclass}',
  27690. 'type',
  27691. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27692. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  27693. '']),
  27694. '');
  27695. AddModuleWithIntfImplSrc('unit2.pas',
  27696. LinesToStr([
  27697. 'uses typinfo;',
  27698. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  27699. 'procedure DoPtr(p: PTypeInfo);',
  27700. 'procedure DoInfo(t: TTypeInfo);',
  27701. 'procedure DoInt(t: TTypeInfoInteger);',
  27702. '']),
  27703. LinesToStr([
  27704. 'procedure DoPtr(p: PTypeInfo);',
  27705. 'begin end;',
  27706. 'procedure DoInfo(t: TTypeInfo);',
  27707. 'begin end;',
  27708. 'procedure DoInt(t: TTypeInfoInteger);',
  27709. 'begin end;',
  27710. '']));
  27711. StartUnit(true);
  27712. Add([
  27713. 'interface',
  27714. 'uses unit2;', // does not use unit typinfo
  27715. 'implementation',
  27716. 'var',
  27717. ' i: byte;',
  27718. ' p: pointer;',
  27719. ' t: PTypeInfo;',
  27720. 'initialization',
  27721. ' p:=typeinfo(i);',
  27722. ' t:=typeinfo(i);',
  27723. ' if p=t then ;',
  27724. ' if p=typeinfo(i) then ;',
  27725. ' if typeinfo(i)=p then ;',
  27726. ' if t=typeinfo(i) then ;',
  27727. ' if typeinfo(i)=t then ;',
  27728. ' DoPtr(p);',
  27729. ' DoPtr(t);',
  27730. ' DoPtr(typeinfo(i));',
  27731. ' DoInfo(p);',
  27732. ' DoInfo(t);',
  27733. ' DoInfo(typeinfo(i));',
  27734. ' DoInt(typeinfo(i));',
  27735. '']);
  27736. ConvertUnit;
  27737. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  27738. LinesToStr([ // statements
  27739. 'var $impl = $mod.$impl;',
  27740. '']),
  27741. LinesToStr([ // this.$init
  27742. '$impl.p = rtl.byte;',
  27743. '$impl.t = rtl.byte;',
  27744. 'if ($impl.p === $impl.t) ;',
  27745. 'if ($impl.p === rtl.byte) ;',
  27746. 'if (rtl.byte === $impl.p) ;',
  27747. 'if ($impl.t === rtl.byte) ;',
  27748. 'if (rtl.byte === $impl.t) ;',
  27749. 'pas.unit2.DoPtr($impl.p);',
  27750. 'pas.unit2.DoPtr($impl.t);',
  27751. 'pas.unit2.DoPtr(rtl.byte);',
  27752. 'pas.unit2.DoInfo($impl.p);',
  27753. 'pas.unit2.DoInfo($impl.t);',
  27754. 'pas.unit2.DoInfo(rtl.byte);',
  27755. 'pas.unit2.DoInt(rtl.byte);',
  27756. '']),
  27757. LinesToStr([ // implementation
  27758. '$impl.i = 0;',
  27759. '$impl.p = null;',
  27760. '$impl.t = null;',
  27761. '']) );
  27762. end;
  27763. procedure TTestModule.TestRTTI_Interface_Corba;
  27764. begin
  27765. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27766. StartProgram(false);
  27767. Add([
  27768. '{$interfaces corba}',
  27769. '{$modeswitch externalclass}',
  27770. 'type',
  27771. ' IUnknown = interface',
  27772. ' end;',
  27773. ' IBird = interface',
  27774. ' function GetItem: longint;',
  27775. ' procedure SetItem(Value: longint);',
  27776. ' property Item: longint read GetItem write SetItem;',
  27777. ' end;',
  27778. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27779. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  27780. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  27781. 'var',
  27782. ' i: IBird;',
  27783. ' t: TTypeInfoInterface;',
  27784. 'begin',
  27785. ' t:=TypeInfo(IBird);',
  27786. ' t:=TypeInfo(i);',
  27787. ' DoIt(t);',
  27788. ' DoIt(TypeInfo(IBird));',
  27789. '']);
  27790. ConvertProgram;
  27791. CheckSource('TestRTTI_Interface_Corba',
  27792. LinesToStr([ // statements
  27793. 'rtl.createInterface(',
  27794. ' $mod,',
  27795. ' "IUnknown",',
  27796. ' "{B92D5841-758A-322B-B800-000000000000}",',
  27797. ' [],',
  27798. ' null,',
  27799. ' function () {',
  27800. ' }',
  27801. ');',
  27802. 'rtl.createInterface(',
  27803. ' $mod,',
  27804. ' "IBird",',
  27805. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  27806. ' ["GetItem", "SetItem"],',
  27807. ' null,',
  27808. ' function () {',
  27809. ' var $r = this.$rtti;',
  27810. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  27811. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  27812. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  27813. ' }',
  27814. ');',
  27815. 'this.DoIt = function (t) {',
  27816. '}; ',
  27817. 'this.i = null;',
  27818. 'this.t = null;',
  27819. '']),
  27820. LinesToStr([ // $mod.$main
  27821. '$mod.t = $mod.$rtti["IBird"];',
  27822. '$mod.t = $mod.i.$rtti;',
  27823. '$mod.DoIt($mod.t);',
  27824. '$mod.DoIt($mod.$rtti["IBird"]);',
  27825. '']));
  27826. end;
  27827. procedure TTestModule.TestRTTI_Interface_COM;
  27828. begin
  27829. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27830. StartProgram(false);
  27831. Add([
  27832. '{$interfaces com}',
  27833. '{$modeswitch externalclass}',
  27834. 'type',
  27835. ' TGuid = record end;',
  27836. ' integer = longint;',
  27837. ' IUnknown = interface',
  27838. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  27839. ' function _AddRef: Integer;',
  27840. ' function _Release: Integer;',
  27841. ' end;',
  27842. ' IBird = interface',
  27843. ' function GetItem: longint;',
  27844. ' procedure SetItem(Value: longint);',
  27845. ' property Item: longint read GetItem write SetItem;',
  27846. ' end;',
  27847. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27848. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  27849. 'var',
  27850. ' i: IBird;',
  27851. ' t: TTypeInfoInterface;',
  27852. 'begin',
  27853. ' t:=TypeInfo(IBird);',
  27854. ' t:=TypeInfo(i);',
  27855. '']);
  27856. ConvertProgram;
  27857. CheckSource('TestRTTI_Interface_COM',
  27858. LinesToStr([ // statements
  27859. 'rtl.recNewT($mod, "TGuid", function () {',
  27860. ' this.$eq = function (b) {',
  27861. ' return true;',
  27862. ' };',
  27863. ' this.$assign = function (s) {',
  27864. ' return this;',
  27865. ' };',
  27866. ' $mod.$rtti.$Record("TGuid", {});',
  27867. '});',
  27868. 'rtl.createInterface(',
  27869. ' $mod,',
  27870. ' "IUnknown",',
  27871. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  27872. ' ["QueryInterface", "_AddRef", "_Release"],',
  27873. ' null,',
  27874. ' function () {',
  27875. ' this.$kind = "com";',
  27876. ' var $r = this.$rtti;',
  27877. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  27878. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  27879. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  27880. ' }',
  27881. ');',
  27882. 'rtl.createInterface(',
  27883. ' $mod,',
  27884. ' "IBird",',
  27885. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  27886. ' ["GetItem", "SetItem"],',
  27887. ' $mod.IUnknown,',
  27888. ' function () {',
  27889. ' var $r = this.$rtti;',
  27890. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  27891. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  27892. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  27893. ' }',
  27894. ');',
  27895. 'this.i = null;',
  27896. 'this.t = null;',
  27897. '']),
  27898. LinesToStr([ // $mod.$main
  27899. '$mod.t = $mod.$rtti["IBird"];',
  27900. '$mod.t = $mod.i.$rtti;',
  27901. '']));
  27902. end;
  27903. procedure TTestModule.TestRTTI_ClassHelper;
  27904. begin
  27905. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27906. StartProgram(false);
  27907. Add([
  27908. '{$interfaces com}',
  27909. '{$modeswitch externalclass}',
  27910. 'type',
  27911. ' TObject = class',
  27912. ' end;',
  27913. ' THelper = class helper for TObject',
  27914. ' published',
  27915. ' function GetItem: longint;',
  27916. ' property Item: longint read GetItem;',
  27917. ' end;',
  27918. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  27919. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  27920. 'function THelper.GetItem: longint;',
  27921. 'begin',
  27922. 'end;',
  27923. 'var',
  27924. ' t: TTypeInfoHelper;',
  27925. 'begin',
  27926. ' t:=TypeInfo(THelper);',
  27927. '']);
  27928. ConvertProgram;
  27929. CheckSource('TestRTTI_ClassHelper',
  27930. LinesToStr([ // statements
  27931. 'rtl.createClass($mod, "TObject", null, function () {',
  27932. ' this.$init = function () {',
  27933. ' };',
  27934. ' this.$final = function () {',
  27935. ' };',
  27936. '});',
  27937. 'rtl.createHelper($mod, "THelper", null, function () {',
  27938. ' this.GetItem = function () {',
  27939. ' var Result = 0;',
  27940. ' return Result;',
  27941. ' };',
  27942. ' var $r = this.$rtti;',
  27943. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  27944. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  27945. '});',
  27946. 'this.t = null;',
  27947. '']),
  27948. LinesToStr([ // $mod.$main
  27949. '$mod.t = $mod.$rtti["THelper"];',
  27950. '']));
  27951. end;
  27952. procedure TTestModule.TestResourcestringProgram;
  27953. begin
  27954. StartProgram(false);
  27955. Add([
  27956. 'const Bar = ''bar'';',
  27957. 'resourcestring',
  27958. ' Red = ''red'';',
  27959. ' Foobar = ''fOo''+bar;',
  27960. 'var s: string;',
  27961. ' c: char;',
  27962. 'begin',
  27963. ' s:=red;',
  27964. ' s:=test1.red;',
  27965. ' c:=red[1];',
  27966. ' c:=test1.red[2];',
  27967. ' if red=foobar then ;',
  27968. ' if red[3]=red[4] then ;']);
  27969. ConvertProgram;
  27970. CheckSource('TestResourcestringProgram',
  27971. LinesToStr([ // statements
  27972. 'this.Bar = "bar";',
  27973. 'this.s = "";',
  27974. 'this.c = "";',
  27975. '$mod.$resourcestrings = {',
  27976. ' Red: {',
  27977. ' org: "red"',
  27978. ' },',
  27979. ' Foobar: {',
  27980. ' org: "fOobar"',
  27981. ' }',
  27982. '};',
  27983. '']),
  27984. LinesToStr([ // $mod.$main
  27985. '$mod.s = rtl.getResStr(pas.program, "Red");',
  27986. '$mod.s = rtl.getResStr(pas.program, "Red");',
  27987. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
  27988. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
  27989. 'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
  27990. 'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
  27991. '']));
  27992. end;
  27993. procedure TTestModule.TestResourcestringUnit;
  27994. begin
  27995. StartUnit(false);
  27996. Add([
  27997. 'interface',
  27998. 'const Red = ''rEd'';',
  27999. 'resourcestring',
  28000. ' Blue = ''blue'';',
  28001. ' NotRed = ''not''+Red;',
  28002. 'var s: string;',
  28003. 'implementation',
  28004. 'resourcestring',
  28005. ' ImplGreen = ''green'';',
  28006. 'initialization',
  28007. ' s:=blue+ImplGreen;',
  28008. ' s:=test1.blue+test1.implgreen;',
  28009. ' s:=blue[1]+implgreen[2];']);
  28010. ConvertUnit;
  28011. CheckSource('TestResourcestringUnit',
  28012. LinesToStr([ // statements
  28013. 'this.Red = "rEd";',
  28014. 'this.s = "";',
  28015. '$mod.$resourcestrings = {',
  28016. ' Blue: {',
  28017. ' org: "blue"',
  28018. ' },',
  28019. ' NotRed: {',
  28020. ' org: "notrEd"',
  28021. ' },',
  28022. ' ImplGreen: {',
  28023. ' org: "green"',
  28024. ' }',
  28025. '};',
  28026. '']),
  28027. LinesToStr([ // $mod.$main
  28028. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  28029. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  28030. '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
  28031. '']));
  28032. end;
  28033. procedure TTestModule.TestResourcestringImplementation;
  28034. begin
  28035. StartUnit(false);
  28036. Add([
  28037. 'interface',
  28038. 'implementation',
  28039. 'resourcestring',
  28040. ' ImplRed = ''red'';']);
  28041. ConvertUnit;
  28042. CheckSource('TestResourcestringImplementation',
  28043. LinesToStr([ // intf statements
  28044. 'var $impl = $mod.$impl;']),
  28045. LinesToStr([ // $mod.$init
  28046. '']),
  28047. LinesToStr([ // impl statements
  28048. '$mod.$resourcestrings = {',
  28049. ' ImplRed: {',
  28050. ' org: "red"',
  28051. ' }',
  28052. '};',
  28053. '']));
  28054. end;
  28055. procedure TTestModule.TestAtributes_Ignore;
  28056. begin
  28057. StartProgram(false);
  28058. Add([
  28059. '{$modeswitch ignoreattributes}',
  28060. 'type',
  28061. ' [custom1, custom2(1+3,''foo'')] [mod1.custom3]',
  28062. ' TObject = class',
  28063. ' [custom5()] FS: string;',
  28064. ' [customProp] property S: string read FS;',
  28065. ' end;',
  28066. 'var',
  28067. ' [custom6]',
  28068. ' o: TObject;',
  28069. 'begin',
  28070. '']);
  28071. ConvertProgram;
  28072. CheckSource('TestAtributes_Ignore',
  28073. LinesToStr([ // statements
  28074. 'rtl.createClass($mod, "TObject", null, function () {',
  28075. ' this.$init = function () {',
  28076. ' this.FS = "";',
  28077. ' };',
  28078. ' this.$final = function () {',
  28079. ' };',
  28080. '});',
  28081. 'this.o = null;',
  28082. '']),
  28083. LinesToStr([ // $mod.$main
  28084. '']));
  28085. end;
  28086. procedure TTestModule.TestAssert;
  28087. begin
  28088. StartProgram(false);
  28089. Add([
  28090. 'procedure DoIt;',
  28091. 'var',
  28092. ' b: boolean;',
  28093. ' s: string;',
  28094. 'begin',
  28095. ' {$Assertions on}',
  28096. ' Assert(b);',
  28097. 'end;',
  28098. 'begin',
  28099. ' DoIt;',
  28100. '']);
  28101. ConvertProgram;
  28102. CheckSource('TestAssert',
  28103. LinesToStr([ // statements
  28104. 'this.DoIt = function () {',
  28105. ' var b = false;',
  28106. ' var s = "";',
  28107. ' if (!b) throw "assert failed";',
  28108. '};',
  28109. '']),
  28110. LinesToStr([ // $mod.$main
  28111. '$mod.DoIt();',
  28112. '']));
  28113. end;
  28114. procedure TTestModule.TestAssert_SysUtils;
  28115. begin
  28116. AddModuleWithIntfImplSrc('SysUtils.pas',
  28117. LinesToStr([
  28118. 'type',
  28119. ' TObject = class',
  28120. ' constructor Create;',
  28121. ' end;',
  28122. ' EAssertionFailed = class',
  28123. ' constructor Create(s: string);',
  28124. ' end;',
  28125. '']),
  28126. LinesToStr([
  28127. 'constructor TObject.Create;',
  28128. 'begin end;',
  28129. 'constructor EAssertionFailed.Create(s: string);',
  28130. 'begin end;',
  28131. '']) );
  28132. StartProgram(true);
  28133. Add([
  28134. 'uses sysutils;',
  28135. 'procedure DoIt;',
  28136. 'var',
  28137. ' b: boolean;',
  28138. ' s: string;',
  28139. 'begin',
  28140. ' {$Assertions on}',
  28141. ' Assert(b);',
  28142. ' Assert(b,''msg'');',
  28143. 'end;',
  28144. 'begin',
  28145. ' DoIt;',
  28146. '']);
  28147. ConvertProgram;
  28148. CheckSource('TestAssert_SysUtils',
  28149. LinesToStr([ // statements
  28150. 'this.DoIt = function () {',
  28151. ' var b = false;',
  28152. ' var s = "";',
  28153. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  28154. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  28155. '};',
  28156. '']),
  28157. LinesToStr([ // $mod.$main
  28158. '$mod.DoIt();',
  28159. '']));
  28160. end;
  28161. procedure TTestModule.TestObjectChecks;
  28162. begin
  28163. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  28164. StartProgram(false);
  28165. Add([
  28166. 'type',
  28167. ' TObject = class',
  28168. ' procedure DoIt;',
  28169. ' end;',
  28170. ' TClass = class of tobject;',
  28171. ' TBird = class',
  28172. ' end;',
  28173. ' TBirdClass = class of TBird;',
  28174. 'var',
  28175. ' o : TObject;',
  28176. ' c: TClass;',
  28177. ' b: TBird;',
  28178. ' bc: TBirdClass;',
  28179. 'procedure TObject.DoIt;',
  28180. 'begin',
  28181. ' b:=TBird(o);',
  28182. 'end;',
  28183. 'begin',
  28184. ' o.DoIt;',
  28185. ' b:=TBird(o);',
  28186. ' bc:=TBirdClass(c);',
  28187. '']);
  28188. ConvertProgram;
  28189. CheckSource('TestCheckMethodCall',
  28190. LinesToStr([ // statements
  28191. 'rtl.createClass($mod, "TObject", null, function () {',
  28192. ' this.$init = function () {',
  28193. ' };',
  28194. ' this.$final = function () {',
  28195. ' };',
  28196. ' this.DoIt = function () {',
  28197. ' rtl.checkMethodCall(this,$mod.TObject);',
  28198. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  28199. ' };',
  28200. '});',
  28201. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  28202. '});',
  28203. 'this.o = null;',
  28204. 'this.c = null;',
  28205. 'this.b = null;',
  28206. 'this.bc = null;',
  28207. '']),
  28208. LinesToStr([ // $mod.$main
  28209. '$mod.o.DoIt();',
  28210. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  28211. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  28212. '']));
  28213. end;
  28214. procedure TTestModule.TestRangeChecks_AssignInt;
  28215. begin
  28216. Scanner.Options:=Scanner.Options+[po_CAssignments];
  28217. StartProgram(false);
  28218. Add([
  28219. '{$R+}',
  28220. 'var',
  28221. ' b: byte = 2;',
  28222. ' w: word = 3;',
  28223. 'procedure DoIt(p: byte);',
  28224. 'begin',
  28225. ' b:=w;',
  28226. ' b+=w;',
  28227. ' b:=1;',
  28228. 'end;',
  28229. '{$R-}',
  28230. 'procedure DoSome;',
  28231. 'begin',
  28232. ' DoIt(w);',
  28233. ' b:=w;',
  28234. ' b:=2;',
  28235. 'end;',
  28236. 'begin',
  28237. '{$R+}',
  28238. '']);
  28239. ConvertProgram;
  28240. CheckSource('TestRangeChecks_AssignInt',
  28241. LinesToStr([ // statements
  28242. 'this.b = 2;',
  28243. 'this.w = 3;',
  28244. 'this.DoIt = function (p) {',
  28245. ' rtl.rc(p, 0, 255);',
  28246. ' $mod.b = rtl.rc($mod.w,0,255);',
  28247. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  28248. ' $mod.b = 1;',
  28249. '};',
  28250. 'this.DoSome = function () {',
  28251. ' $mod.DoIt($mod.w);',
  28252. ' $mod.b = $mod.w;',
  28253. ' $mod.b = 2;',
  28254. '};',
  28255. '']),
  28256. LinesToStr([ // $mod.$main
  28257. '']));
  28258. end;
  28259. procedure TTestModule.TestRangeChecks_AssignIntRange;
  28260. begin
  28261. Scanner.Options:=Scanner.Options+[po_CAssignments];
  28262. StartProgram(false);
  28263. Add([
  28264. '{$R+}',
  28265. 'type Ten = 1..10;',
  28266. 'var',
  28267. ' b: Ten = 2;',
  28268. ' w: Ten = 3;',
  28269. 'procedure DoIt(p: Ten);',
  28270. 'begin',
  28271. ' b:=w;',
  28272. ' b+=w;',
  28273. ' b:=1;',
  28274. 'end;',
  28275. '{$R-}',
  28276. 'procedure DoSome;',
  28277. 'begin',
  28278. ' DoIt(w);',
  28279. ' b:=w;',
  28280. ' b:=2;',
  28281. 'end;',
  28282. 'begin',
  28283. '{$R+}',
  28284. '']);
  28285. ConvertProgram;
  28286. CheckSource('TestRangeChecks_AssignIntRange',
  28287. LinesToStr([ // statements
  28288. 'this.b = 2;',
  28289. 'this.w = 3;',
  28290. 'this.DoIt = function (p) {',
  28291. ' rtl.rc(p, 1, 10);',
  28292. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  28293. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  28294. ' $mod.b = 1;',
  28295. '};',
  28296. 'this.DoSome = function () {',
  28297. ' $mod.DoIt($mod.w);',
  28298. ' $mod.b = $mod.w;',
  28299. ' $mod.b = 2;',
  28300. '};',
  28301. '']),
  28302. LinesToStr([ // $mod.$main
  28303. '']));
  28304. end;
  28305. procedure TTestModule.TestRangeChecks_AssignEnum;
  28306. begin
  28307. StartProgram(false);
  28308. Add([
  28309. '{$R+}',
  28310. 'type TEnum = (red,green);',
  28311. 'var',
  28312. ' e: TEnum = red;',
  28313. 'procedure DoIt(p: TEnum);',
  28314. 'begin',
  28315. ' e:=p;',
  28316. ' p:=TEnum(0);',
  28317. ' p:=succ(e);',
  28318. 'end;',
  28319. '{$R-}',
  28320. 'procedure DoSome;',
  28321. 'begin',
  28322. ' DoIt(e);',
  28323. ' e:=TEnum(1);',
  28324. ' e:=pred(e);',
  28325. 'end;',
  28326. 'begin',
  28327. '{$R+}',
  28328. '']);
  28329. ConvertProgram;
  28330. CheckSource('TestRangeChecks_AssignEnum',
  28331. LinesToStr([ // statements
  28332. 'this.TEnum = {',
  28333. ' "0": "red",',
  28334. ' red: 0,',
  28335. ' "1": "green",',
  28336. ' green: 1',
  28337. '};',
  28338. 'this.e = $mod.TEnum.red;',
  28339. 'this.DoIt = function (p) {',
  28340. ' rtl.rc(p, 0, 1);',
  28341. ' $mod.e = rtl.rc(p, 0, 1);',
  28342. ' p = 0;',
  28343. ' p = rtl.rc($mod.e + 1, 0, 1);',
  28344. '};',
  28345. 'this.DoSome = function () {',
  28346. ' $mod.DoIt($mod.e);',
  28347. ' $mod.e = 1;',
  28348. ' $mod.e = $mod.e - 1;',
  28349. '};',
  28350. '']),
  28351. LinesToStr([ // $mod.$main
  28352. '']));
  28353. end;
  28354. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  28355. begin
  28356. StartProgram(false);
  28357. Add([
  28358. '{$R+}',
  28359. 'type',
  28360. ' TEnum = (red,green);',
  28361. ' TEnumRg = red..green;',
  28362. 'var',
  28363. ' e: TEnumRg = red;',
  28364. 'procedure DoIt(p: TEnumRg);',
  28365. 'begin',
  28366. ' e:=p;',
  28367. ' p:=TEnumRg(0);',
  28368. ' p:=succ(e);',
  28369. 'end;',
  28370. '{$R-}',
  28371. 'procedure DoSome;',
  28372. 'begin',
  28373. ' DoIt(e);',
  28374. ' e:=TEnum(1);',
  28375. ' e:=pred(e);',
  28376. 'end;',
  28377. 'begin',
  28378. '{$R+}',
  28379. '']);
  28380. ConvertProgram;
  28381. CheckSource('TestRangeChecks_AssignEnumRange',
  28382. LinesToStr([ // statements
  28383. 'this.TEnum = {',
  28384. ' "0": "red",',
  28385. ' red: 0,',
  28386. ' "1": "green",',
  28387. ' green: 1',
  28388. '};',
  28389. 'this.e = $mod.TEnum.red;',
  28390. 'this.DoIt = function (p) {',
  28391. ' rtl.rc(p, 0, 1);',
  28392. ' $mod.e = rtl.rc(p, 0, 1);',
  28393. ' p = 0;',
  28394. ' p = rtl.rc($mod.e + 1, 0, 1);',
  28395. '};',
  28396. 'this.DoSome = function () {',
  28397. ' $mod.DoIt($mod.e);',
  28398. ' $mod.e = 1;',
  28399. ' $mod.e = $mod.e - 1;',
  28400. '};',
  28401. '']),
  28402. LinesToStr([ // $mod.$main
  28403. '']));
  28404. end;
  28405. procedure TTestModule.TestRangeChecks_AssignChar;
  28406. begin
  28407. StartProgram(false);
  28408. Add([
  28409. '{$R+}',
  28410. 'type',
  28411. ' TLetter = char;',
  28412. 'var',
  28413. ' b: TLetter = ''2'';',
  28414. ' w: TLetter = ''3'';',
  28415. 'procedure DoIt(p: TLetter);',
  28416. 'begin',
  28417. ' b:=w;',
  28418. ' b:=''1'';',
  28419. 'end;',
  28420. '{$R-}',
  28421. 'procedure DoSome;',
  28422. 'begin',
  28423. ' DoIt(w);',
  28424. ' b:=w;',
  28425. ' b:=''2'';',
  28426. 'end;',
  28427. 'begin',
  28428. '{$R+}',
  28429. '']);
  28430. ConvertProgram;
  28431. CheckSource('TestRangeChecks_AssignChar',
  28432. LinesToStr([ // statements
  28433. 'this.b = "2";',
  28434. 'this.w = "3";',
  28435. 'this.DoIt = function (p) {',
  28436. ' rtl.rcc(p, 0, 65535);',
  28437. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  28438. ' $mod.b = "1";',
  28439. '};',
  28440. 'this.DoSome = function () {',
  28441. ' $mod.DoIt($mod.w);',
  28442. ' $mod.b = $mod.w;',
  28443. ' $mod.b = "2";',
  28444. '};',
  28445. '']),
  28446. LinesToStr([ // $mod.$main
  28447. '']));
  28448. end;
  28449. procedure TTestModule.TestRangeChecks_AssignCharRange;
  28450. begin
  28451. StartProgram(false);
  28452. Add([
  28453. '{$R+}',
  28454. 'type TDigit = ''0''..''9'';',
  28455. 'var',
  28456. ' b: TDigit = ''2'';',
  28457. ' w: TDigit = ''3'';',
  28458. 'procedure DoIt(p: TDigit);',
  28459. 'begin',
  28460. ' b:=w;',
  28461. ' b:=''1'';',
  28462. 'end;',
  28463. '{$R-}',
  28464. 'procedure DoSome;',
  28465. 'begin',
  28466. ' DoIt(w);',
  28467. ' b:=w;',
  28468. ' b:=''2'';',
  28469. 'end;',
  28470. 'begin',
  28471. '{$R+}',
  28472. '']);
  28473. ConvertProgram;
  28474. CheckSource('TestRangeChecks_AssignCharRange',
  28475. LinesToStr([ // statements
  28476. 'this.b = "2";',
  28477. 'this.w = "3";',
  28478. 'this.DoIt = function (p) {',
  28479. ' rtl.rcc(p, 48, 57);',
  28480. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  28481. ' $mod.b = "1";',
  28482. '};',
  28483. 'this.DoSome = function () {',
  28484. ' $mod.DoIt($mod.w);',
  28485. ' $mod.b = $mod.w;',
  28486. ' $mod.b = "2";',
  28487. '};',
  28488. '']),
  28489. LinesToStr([ // $mod.$main
  28490. '']));
  28491. end;
  28492. procedure TTestModule.TestRangeChecks_ArrayIndex;
  28493. begin
  28494. StartProgram(false);
  28495. Add([
  28496. '{$R+}',
  28497. 'type',
  28498. ' Ten = 1..10;',
  28499. ' TArr = array of Ten;',
  28500. ' TArrArr = array of TArr;',
  28501. ' TArrByte = array[byte] of Ten;',
  28502. ' TArrChar = array[''0''..''9''] of Ten;',
  28503. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  28504. ' TObject = class',
  28505. ' A: TArr;',
  28506. ' end;',
  28507. 'procedure DoIt;',
  28508. 'var',
  28509. ' Arr: TArr;',
  28510. ' ArrArr: TArrArr;',
  28511. ' ArrByte: TArrByte;',
  28512. ' ArrChar: TArrChar;',
  28513. ' ArrByteChar: TArrByteChar;',
  28514. ' i: Ten;',
  28515. ' c: char;',
  28516. ' o: tobject;',
  28517. 'begin',
  28518. ' i:=Arr[1];',
  28519. ' i:=ArrByteChar[1,''2''];',
  28520. ' Arr[1]:=Arr[1];',
  28521. ' Arr[i]:=Arr[i];',
  28522. ' ArrByte[3]:=ArrByte[3];',
  28523. ' ArrByte[i]:=ArrByte[i];',
  28524. ' ArrChar[''5'']:=ArrChar[''5''];',
  28525. ' ArrChar[c]:=ArrChar[c];',
  28526. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  28527. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  28528. ' o.a[i]:=o.a[i];',
  28529. 'end;',
  28530. 'begin',
  28531. '']);
  28532. ConvertProgram;
  28533. CheckSource('TestRangeChecks_ArrayIndex',
  28534. LinesToStr([ // statements
  28535. 'rtl.createClass($mod, "TObject", null, function () {',
  28536. ' this.$init = function () {',
  28537. ' this.A = [];',
  28538. ' };',
  28539. ' this.$final = function () {',
  28540. ' this.A = undefined;',
  28541. ' };',
  28542. '});',
  28543. 'this.DoIt = function () {',
  28544. ' var Arr = [];',
  28545. ' var ArrArr = [];',
  28546. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  28547. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  28548. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  28549. ' var i = 0;',
  28550. ' var c = "";',
  28551. ' var o = null;',
  28552. ' i = rtl.rc(Arr[1], 1, 10);',
  28553. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  28554. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  28555. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  28556. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  28557. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  28558. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  28559. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  28560. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  28561. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  28562. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  28563. '};',
  28564. '']),
  28565. LinesToStr([ // $mod.$main
  28566. '']));
  28567. end;
  28568. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  28569. begin
  28570. StartProgram(false);
  28571. Add([
  28572. '{$R+}',
  28573. 'type',
  28574. ' Ten = 1..10;',
  28575. ' TRec = record x: Ten end;',
  28576. ' TArr = array of TRec;',
  28577. ' TArrArr = array of TArr;',
  28578. ' TObject = class',
  28579. ' A: TArr;',
  28580. ' end;',
  28581. 'procedure DoIt;',
  28582. 'var',
  28583. ' Arr: TArr;',
  28584. ' ArrArr: TArrArr;',
  28585. ' i: Ten;',
  28586. ' o: tobject;',
  28587. 'begin',
  28588. ' Arr[1]:=Arr[1];',
  28589. ' Arr[i]:=Arr[i+1];',
  28590. ' o.a[i]:=o.a[i+2];',
  28591. 'end;',
  28592. 'begin',
  28593. '']);
  28594. ConvertProgram;
  28595. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  28596. LinesToStr([ // statements
  28597. 'rtl.recNewT($mod, "TRec", function () {',
  28598. ' this.x = 0;',
  28599. ' this.$eq = function (b) {',
  28600. ' return this.x === b.x;',
  28601. ' };',
  28602. ' this.$assign = function (s) {',
  28603. ' this.x = s.x;',
  28604. ' return this;',
  28605. ' };',
  28606. '});',
  28607. 'rtl.createClass($mod, "TObject", null, function () {',
  28608. ' this.$init = function () {',
  28609. ' this.A = [];',
  28610. ' };',
  28611. ' this.$final = function () {',
  28612. ' this.A = undefined;',
  28613. ' };',
  28614. '});',
  28615. 'this.DoIt = function () {',
  28616. ' var Arr = [];',
  28617. ' var ArrArr = [];',
  28618. ' var i = 0;',
  28619. ' var o = null;',
  28620. ' Arr[1].$assign(Arr[1]);',
  28621. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  28622. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  28623. '};',
  28624. '']),
  28625. LinesToStr([ // $mod.$main
  28626. '']));
  28627. end;
  28628. procedure TTestModule.TestRangeChecks_StringIndex;
  28629. begin
  28630. StartProgram(false);
  28631. Add([
  28632. 'type',
  28633. ' TObject = class',
  28634. ' S: string;',
  28635. ' end;',
  28636. '{$R+}',
  28637. 'procedure DoIt(var h: string);',
  28638. 'var',
  28639. ' s: string;',
  28640. ' i: longint;',
  28641. ' c: char;',
  28642. ' o: tobject;',
  28643. 'begin',
  28644. ' c:=s[1];',
  28645. ' s[i]:=s[i];',
  28646. ' h[i]:=h[i];',
  28647. ' c:=o.s[i];',
  28648. ' o.s[i]:=c;',
  28649. 'end;',
  28650. 'begin',
  28651. '']);
  28652. ConvertProgram;
  28653. CheckSource('TestRangeChecks_StringIndex',
  28654. LinesToStr([ // statements
  28655. 'rtl.createClass($mod, "TObject", null, function () {',
  28656. ' this.$init = function () {',
  28657. ' this.S = "";',
  28658. ' };',
  28659. ' this.$final = function () {',
  28660. ' };',
  28661. '});',
  28662. 'this.DoIt = function (h) {',
  28663. ' var s = "";',
  28664. ' var i = 0;',
  28665. ' var c = "";',
  28666. ' var o = null;',
  28667. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  28668. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  28669. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  28670. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  28671. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  28672. '};',
  28673. '']),
  28674. LinesToStr([ // $mod.$main
  28675. '']));
  28676. end;
  28677. procedure TTestModule.TestRangeChecks_TypecastInt;
  28678. begin
  28679. StartProgram(false);
  28680. Add([
  28681. '{$R+}',
  28682. 'var',
  28683. ' i: nativeint;',
  28684. ' b: byte;',
  28685. ' sh: shortint;',
  28686. ' w: word;',
  28687. ' sm: smallint;',
  28688. ' lw: longword;',
  28689. ' li: longint;',
  28690. 'begin',
  28691. ' b:=12+byte(i);',
  28692. ' sh:=12+shortint(i);',
  28693. ' w:=12+word(i);',
  28694. ' sm:=12+smallint(i);',
  28695. ' lw:=12+longword(i);',
  28696. ' li:=12+longint(i);',
  28697. '']);
  28698. ConvertProgram;
  28699. CheckSource('TestRangeChecks_TypecastInt',
  28700. LinesToStr([
  28701. 'this.i = 0;',
  28702. 'this.b = 0;',
  28703. 'this.sh = 0;',
  28704. 'this.w = 0;',
  28705. 'this.sm = 0;',
  28706. 'this.lw = 0;',
  28707. 'this.li = 0;',
  28708. '']),
  28709. LinesToStr([
  28710. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  28711. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  28712. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  28713. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  28714. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  28715. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  28716. '']));
  28717. end;
  28718. Initialization
  28719. RegisterTests([TTestModule]);
  28720. end.